Hex Artifact Content
Not logged in

Artifact 6575b1e5b3ee39b9b00a467d31b28300998d820e:

File src/sqlite3.c part of check-in [dec2c22d9d] - Update the version of SQLite used internally to 3.6.6.1. by drh on 2008-11-22 15:40:39.

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 36 2e 31 2e 20  ersion 3.6.6.1. 
00b0: 20 42 79 20 63 6f 6d 62 69 6e 69 6e 67 20 61 6c   By combining al
00c0: 6c 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c  l the individual
00d0: 20 43 20 63 6f 64 65 20 66 69 6c 65 73 20 69 6e   C code files in
00e0: 74 6f 20 74 68 69 73 20 0a 2a 2a 20 73 69 6e 67  to this .** sing
00f0: 6c 65 20 6c 61 72 67 65 20 66 69 6c 65 2c 20 74  le large file, t
0100: 68 65 20 65 6e 74 69 72 65 20 63 6f 64 65 20 63  he entire code c
0110: 61 6e 20 62 65 20 63 6f 6d 70 69 6c 65 64 20 61  an be compiled a
0120: 73 20 61 20 6f 6e 65 20 74 72 61 6e 73 6c 61 74  s a one translat
0130: 69 6f 6e 0a 2a 2a 20 75 6e 69 74 2e 20 20 54 68  ion.** unit.  Th
0140: 69 73 20 61 6c 6c 6f 77 73 20 6d 61 6e 79 20 63  is allows many c
0150: 6f 6d 70 69 6c 65 72 73 20 74 6f 20 64 6f 20 6f  ompilers to do o
0160: 70 74 69 6d 69 7a 61 74 69 6f 6e 73 20 74 68 61  ptimizations tha
0170: 74 20 77 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a  t would not be.*
0180: 2a 20 70 6f 73 73 69 62 6c 65 20 69 66 20 74 68  * possible if th
0190: 65 20 66 69 6c 65 73 20 77 65 72 65 20 63 6f 6d  e files were com
01a0: 70 69 6c 65 64 20 73 65 70 61 72 61 74 65 6c 79  piled separately
01b0: 2e 20 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 69  .  Performance i
01c0: 6d 70 72 6f 76 65 6d 65 6e 74 73 0a 2a 2a 20 6f  mprovements.** o
01d0: 66 20 35 25 20 61 72 65 20 6d 6f 72 65 20 61 72  f 5% are more ar
01e0: 65 20 63 6f 6d 6d 6f 6e 6c 79 20 73 65 65 6e 20  e commonly seen 
01f0: 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
0200: 6f 6d 70 69 6c 65 64 20 61 73 20 61 20 73 69 6e  ompiled as a sin
0210: 67 6c 65 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 69  gle.** translati
0220: 6f 6e 20 75 6e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54  on unit..**.** T
0230: 68 69 73 20 66 69 6c 65 20 69 73 20 61 6c 6c 20  his file is all 
0240: 79 6f 75 20 6e 65 65 64 20 74 6f 20 63 6f 6d 70  you need to comp
0250: 69 6c 65 20 53 51 4c 69 74 65 2e 20 20 54 6f 20  ile SQLite.  To 
0260: 75 73 65 20 53 51 4c 69 74 65 20 69 6e 20 6f 74  use SQLite in ot
0270: 68 65 72 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 2c  her.** programs,
0280: 20 79 6f 75 20 6e 65 65 64 20 74 68 69 73 20 66   you need this f
0290: 69 6c 65 20 61 6e 64 20 74 68 65 20 22 73 71 6c  ile and the "sql
02a0: 69 74 65 33 2e 68 22 20 68 65 61 64 65 72 20 66  ite3.h" header f
02b0: 69 6c 65 20 74 68 61 74 20 64 65 66 69 6e 65 73  ile that defines
02c0: 0a 2a 2a 20 74 68 65 20 70 72 6f 67 72 61 6d 6d  .** the programm
02d0: 69 6e 67 20 69 6e 74 65 72 66 61 63 65 20 74 6f  ing interface to
02e0: 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72   the SQLite libr
02f0: 61 72 79 2e 20 20 28 49 66 20 79 6f 75 20 64 6f  ary.  (If you do
0300: 20 6e 6f 74 20 68 61 76 65 20 0a 2a 2a 20 74 68   not have .** th
0310: 65 20 22 73 71 6c 69 74 65 33 2e 68 22 20 68 65  e "sqlite3.h" he
0320: 61 64 65 72 20 66 69 6c 65 20 61 74 20 68 61 6e  ader file at han
0330: 64 2c 20 79 6f 75 20 77 69 6c 6c 20 66 69 6e 64  d, you will find
0340: 20 61 20 63 6f 70 79 20 69 6e 20 74 68 65 20 66   a copy in the f
0350: 69 72 73 74 0a 2a 2a 20 36 37 32 38 20 6c 69 6e  irst.** 6728 lin
0360: 65 73 20 70 61 73 74 20 74 68 69 73 20 68 65 61  es past this hea
0370: 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 29 20 20 41  der comment.)  A
0380: 64 64 69 74 69 6f 6e 61 6c 20 63 6f 64 65 20 66  dditional code f
0390: 69 6c 65 73 20 6d 61 79 20 62 65 0a 2a 2a 20 6e  iles may be.** n
03a0: 65 65 64 65 64 20 69 66 20 79 6f 75 20 77 61 6e  eeded if you wan
03b0: 74 20 61 20 77 72 61 70 70 65 72 20 74 6f 20 69  t a wrapper to i
03c0: 6e 74 65 72 66 61 63 65 20 53 51 4c 69 74 65 20  nterface SQLite 
03d0: 77 69 74 68 20 79 6f 75 72 20 63 68 6f 69 63 65  with your choice
03e0: 20 6f 66 0a 2a 2a 20 70 72 6f 67 72 61 6d 6d 69   of.** programmi
03f0: 6e 67 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68  ng language.  Th
0400: 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 22  e code for the "
0410: 73 71 6c 69 74 65 33 22 20 63 6f 6d 6d 61 6e 64  sqlite3" command
0420: 2d 6c 69 6e 65 20 73 68 65 6c 6c 0a 2a 2a 20 69  -line shell.** i
0430: 73 20 61 6c 73 6f 20 69 6e 20 61 20 73 65 70 61  s also in a sepa
0440: 72 61 74 65 20 66 69 6c 65 2e 20 20 54 68 69 73  rate file.  This
0450: 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 6f   file contains o
0460: 6e 6c 79 20 63 6f 64 65 20 66 6f 72 20 74 68 65  nly code for the
0470: 20 63 6f 72 65 0a 2a 2a 20 53 51 4c 69 74 65 20   core.** SQLite 
0480: 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54  library..**.** T
0490: 68 69 73 20 61 6d 61 6c 67 61 6d 61 74 69 6f 6e  his amalgamation
04a0: 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20 6f   was generated o
04b0: 6e 20 32 30 30 38 2d 31 31 2d 32 32 20 31 34 3a  n 2008-11-22 14:
04c0: 33 31 3a 33 32 20 55 54 43 2e 0a 2a 2f 0a 23 64  31:32 UTC..*/.#d
04d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52  efine SQLITE_COR
04e0: 45 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  E 1.#define SQLI
04f0: 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e 20  TE_AMALGAMATION 
0500: 31 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  1.#ifndef SQLITE
0510: 5f 50 52 49 56 41 54 45 0a 23 20 64 65 66 69 6e  _PRIVATE.# defin
0520: 65 20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  e SQLITE_PRIVATE
0530: 20 73 74 61 74 69 63 0a 23 65 6e 64 69 66 0a 23   static.#endif.#
0540: 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 50  ifndef SQLITE_AP
0550: 49 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  I.# define SQLIT
0560: 45 5f 41 50 49 0a 23 65 6e 64 69 66 0a 2f 2a 2a  E_API.#endif./**
0570: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
0580: 69 6e 20 66 69 6c 65 20 73 71 6c 69 74 65 49 6e  in file sqliteIn
0590: 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
05a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
05b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
05c0: 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
05d0: 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
05e0: 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
05f0: 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
0600: 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
0610: 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
0620: 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
0630: 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
0640: 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
0650: 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
0660: 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
0670: 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
0680: 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
0690: 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
06a0: 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
06b0: 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
06c0: 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
06d0: 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
06e0: 20 67 69 76 65 2e 0a 2a 2a 0a 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 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20  ***.** Internal 
0740: 69 6e 74 65 72 66 61 63 65 20 64 65 66 69 6e 69  interface defini
0750: 74 69 6f 6e 73 20 66 6f 72 20 53 51 4c 69 74 65  tions for SQLite
0760: 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64  ..**.** @(#) $Id
0770: 3a 20 73 71 6c 69 74 65 49 6e 74 2e 68 2c 76 20  : sqliteInt.h,v 
0780: 31 2e 37 39 38 20 32 30 30 38 2f 31 31 2f 31 39  1.798 2008/11/19
0790: 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c   16:52:44 daniel
07a0: 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23  k1977 Exp $.*/.#
07b0: 69 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 49 4e  ifndef _SQLITEIN
07c0: 54 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51  T_H_.#define _SQ
07d0: 4c 49 54 45 49 4e 54 5f 48 5f 0a 0a 2f 2a 0a 2a  LITEINT_H_../*.*
07e0: 2a 20 49 6e 63 6c 75 64 65 20 74 68 65 20 63 6f  * Include the co
07f0: 6e 66 69 67 75 72 61 74 69 6f 6e 20 68 65 61 64  nfiguration head
0800: 65 72 20 6f 75 74 70 75 74 20 62 79 20 27 63 6f  er output by 'co
0810: 6e 66 69 67 75 72 65 27 20 69 66 20 77 65 27 72  nfigure' if we'r
0820: 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 61  e using the.** a
0830: 75 74 6f 63 6f 6e 66 2d 62 61 73 65 64 20 62 75  utoconf-based bu
0840: 69 6c 64 0a 2a 2f 0a 23 69 66 64 65 66 20 5f 48  ild.*/.#ifdef _H
0850: 41 56 45 5f 53 51 4c 49 54 45 5f 43 4f 4e 46 49  AVE_SQLITE_CONFI
0860: 47 5f 48 0a 23 69 6e 63 6c 75 64 65 20 22 63 6f  G_H.#include "co
0870: 6e 66 69 67 2e 68 22 0a 23 65 6e 64 69 66 0a 0a  nfig.h".#endif..
0880: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
0890: 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 4c 69  Include sqliteLi
08a0: 6d 69 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  mit.h in the mid
08b0: 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
08c0: 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  .h ***********/.
08d0: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
08e0: 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74  Begin file sqlit
08f0: 65 4c 69 6d 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  eLimit.h *******
0900: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0910: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
0920: 2f 2a 0a 2a 2a 20 32 30 30 37 20 4d 61 79 20 37  /*.** 2007 May 7
0930: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
0940: 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
0950: 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
0960: 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
0970: 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
0980: 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
0990: 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
09a0: 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
09b0: 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
09c0: 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
09d0: 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
09e0: 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
09f0: 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
0a00: 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
0a10: 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
0a20: 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
0a30: 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
0a40: 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
0a50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a60: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0a80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
0a90: 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  ** .** This file
0aa0: 20 64 65 66 69 6e 65 73 20 76 61 72 69 6f 75 73   defines various
0ab0: 20 6c 69 6d 69 74 73 20 6f 66 20 77 68 61 74 20   limits of what 
0ac0: 53 51 4c 69 74 65 20 63 61 6e 20 70 72 6f 63 65  SQLite can proce
0ad0: 73 73 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24  ss..**.** @(#) $
0ae0: 49 64 3a 20 73 71 6c 69 74 65 4c 69 6d 69 74 2e  Id: sqliteLimit.
0af0: 68 2c 76 20 31 2e 38 20 32 30 30 38 2f 30 33 2f  h,v 1.8 2008/03/
0b00: 32 36 20 31 35 3a 35 36 3a 32 32 20 64 72 68 20  26 15:56:22 drh 
0b10: 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
0b20: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
0b30: 74 68 20 6f 66 20 61 20 54 45 58 54 20 6f 72 20  th of a TEXT or 
0b40: 42 4c 4f 42 20 69 6e 20 62 79 74 65 73 2e 20 20  BLOB in bytes.  
0b50: 20 54 68 69 73 20 61 6c 73 6f 0a 2a 2a 20 6c 69   This also.** li
0b60: 6d 69 74 73 20 74 68 65 20 73 69 7a 65 20 6f 66  mits the size of
0b70: 20 61 20 72 6f 77 20 69 6e 20 61 20 74 61 62 6c   a row in a tabl
0b80: 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  e or index..**.*
0b90: 2a 20 54 68 65 20 68 61 72 64 20 6c 69 6d 69 74  * The hard limit
0ba0: 20 69 73 20 74 68 65 20 61 62 69 6c 69 74 79 20   is the ability 
0bb0: 6f 66 20 61 20 33 32 2d 62 69 74 20 73 69 67 6e  of a 32-bit sign
0bc0: 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 74 6f  ed integer.** to
0bd0: 20 63 6f 75 6e 74 20 74 68 65 20 73 69 7a 65 3a   count the size:
0be0: 20 32 5e 33 31 2d 31 20 6f 72 20 32 31 34 37 34   2^31-1 or 21474
0bf0: 38 33 36 34 37 2e 0a 2a 2f 0a 23 69 66 6e 64 65  83647..*/.#ifnde
0c00: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  f SQLITE_MAX_LEN
0c10: 47 54 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  GTH.# define SQL
0c20: 49 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 31  ITE_MAX_LENGTH 1
0c30: 30 30 30 30 30 30 30 30 30 0a 23 65 6e 64 69 66  000000000.#endif
0c40: 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20  ../*.** This is 
0c50: 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62  the maximum numb
0c60: 65 72 20 6f 66 0a 2a 2a 0a 2a 2a 20 20 20 20 2a  er of.**.**    *
0c70: 20 43 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 74 61   Columns in a ta
0c80: 62 6c 65 0a 2a 2a 20 20 20 20 2a 20 43 6f 6c 75  ble.**    * Colu
0c90: 6d 6e 73 20 69 6e 20 61 6e 20 69 6e 64 65 78 0a  mns in an index.
0ca0: 2a 2a 20 20 20 20 2a 20 43 6f 6c 75 6d 6e 73 20  **    * Columns 
0cb0: 69 6e 20 61 20 76 69 65 77 0a 2a 2a 20 20 20 20  in a view.**    
0cc0: 2a 20 54 65 72 6d 73 20 69 6e 20 74 68 65 20 53  * Terms in the S
0cd0: 45 54 20 63 6c 61 75 73 65 20 6f 66 20 61 6e 20  ET clause of an 
0ce0: 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74  UPDATE statement
0cf0: 0a 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69  .**    * Terms i
0d00: 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
0d10: 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
0d20: 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 2a 20 54  tement.**    * T
0d30: 65 72 6d 73 20 69 6e 20 74 68 65 20 47 52 4f 55  erms in the GROU
0d40: 50 20 42 59 20 6f 72 20 4f 52 44 45 52 20 42 59  P BY or ORDER BY
0d50: 20 63 6c 61 75 73 65 73 20 6f 66 20 61 20 53 45   clauses of a SE
0d60: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
0d70: 2a 2a 20 20 20 20 2a 20 54 65 72 6d 73 20 69 6e  **    * Terms in
0d80: 20 74 68 65 20 56 41 4c 55 45 53 20 63 6c 61 75   the VALUES clau
0d90: 73 65 20 6f 66 20 61 6e 20 49 4e 53 45 52 54 20  se of an INSERT 
0da0: 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 0a 2a 2a 20  statement.**.** 
0db0: 54 68 65 20 68 61 72 64 20 75 70 70 65 72 20 6c  The hard upper l
0dc0: 69 6d 69 74 20 68 65 72 65 20 69 73 20 33 32 36  imit here is 326
0dd0: 37 36 2e 20 20 4d 6f 73 74 20 64 61 74 61 62 61  76.  Most databa
0de0: 73 65 20 70 65 6f 70 6c 65 20 77 69 6c 6c 0a 2a  se people will.*
0df0: 2a 20 74 65 6c 6c 20 79 6f 75 20 74 68 61 74 20  * tell you that 
0e00: 69 6e 20 61 20 77 65 6c 6c 2d 6e 6f 72 6d 61 6c  in a well-normal
0e10: 69 7a 65 64 20 64 61 74 61 62 61 73 65 2c 20 79  ized database, y
0e20: 6f 75 20 75 73 75 61 6c 6c 79 20 73 68 6f 75 6c  ou usually shoul
0e30: 64 0a 2a 2a 20 6e 6f 74 20 68 61 76 65 20 6d 6f  d.** not have mo
0e40: 72 65 20 74 68 61 6e 20 61 20 64 6f 7a 65 6e 20  re than a dozen 
0e50: 6f 72 20 73 6f 20 63 6f 6c 75 6d 6e 73 20 69 6e  or so columns in
0e60: 20 61 6e 79 20 74 61 62 6c 65 2e 20 20 41 6e 64   any table.  And
0e70: 20 69 66 0a 2a 2a 20 74 68 61 74 20 69 73 20 74   if.** that is t
0e80: 68 65 20 63 61 73 65 2c 20 74 68 65 72 65 20 69  he case, there i
0e90: 73 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 68 61  s no point in ha
0ea0: 76 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 61  ving more than a
0eb0: 20 66 65 77 0a 2a 2a 20 64 6f 7a 65 6e 20 76 61   few.** dozen va
0ec0: 6c 75 65 73 20 69 6e 20 61 6e 79 20 6f 66 20 74  lues in any of t
0ed0: 68 65 20 6f 74 68 65 72 20 73 69 74 75 61 74 69  he other situati
0ee0: 6f 6e 73 20 64 65 73 63 72 69 62 65 64 20 61 62  ons described ab
0ef0: 6f 76 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ove..*/.#ifndef 
0f00: 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c 55 4d  SQLITE_MAX_COLUM
0f10: 4e 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  N.# define SQLIT
0f20: 45 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 32 30 30  E_MAX_COLUMN 200
0f30: 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
0f40: 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
0f50: 74 68 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 53  th of a single S
0f60: 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  QL statement in 
0f70: 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 74 20  bytes..**.** It 
0f80: 75 73 65 64 20 74 6f 20 62 65 20 74 68 65 20 63  used to be the c
0f90: 61 73 65 20 74 68 61 74 20 73 65 74 74 69 6e 67  ase that setting
0fa0: 20 74 68 69 73 20 76 61 6c 75 65 20 74 6f 20 7a   this value to z
0fb0: 65 72 6f 20 77 6f 75 6c 64 0a 2a 2a 20 74 75 72  ero would.** tur
0fc0: 6e 20 74 68 65 20 6c 69 6d 69 74 20 6f 66 66 2e  n the limit off.
0fd0: 20 20 54 68 61 74 20 69 73 20 6e 6f 20 6c 6f 6e    That is no lon
0fe0: 67 65 72 20 74 72 75 65 2e 20 20 49 74 20 69 73  ger true.  It is
0ff0: 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a 2a   not possible.**
1000: 20 74 6f 20 74 75 72 6e 20 74 68 69 73 20 6c 69   to turn this li
1010: 6d 69 74 20 6f 66 66 2e 0a 2a 2f 0a 23 69 66 6e  mit off..*/.#ifn
1020: 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53  def SQLITE_MAX_S
1030: 51 4c 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69  QL_LENGTH.# defi
1040: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51  ne SQLITE_MAX_SQ
1050: 4c 5f 4c 45 4e 47 54 48 20 31 30 30 30 30 30 30  L_LENGTH 1000000
1060: 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  000.#endif../*.*
1070: 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 64 65  * The maximum de
1080: 70 74 68 20 6f 66 20 61 6e 20 65 78 70 72 65 73  pth of an expres
1090: 73 69 6f 6e 20 74 72 65 65 2e 20 54 68 69 73 20  sion tree. This 
10a0: 69 73 20 6c 69 6d 69 74 65 64 20 74 6f 20 0a 2a  is limited to .*
10b0: 2a 20 73 6f 6d 65 20 65 78 74 65 6e 74 20 62 79  * some extent by
10c0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
10d0: 4c 45 4e 47 54 48 2e 20 42 75 74 20 73 6f 6d 65  LENGTH. But some
10e0: 74 69 6d 65 20 79 6f 75 20 6d 69 67 68 74 20 0a  time you might .
10f0: 2a 2a 20 77 61 6e 74 20 74 6f 20 70 6c 61 63 65  ** want to place
1100: 20 6d 6f 72 65 20 73 65 76 65 72 65 20 6c 69 6d   more severe lim
1110: 69 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 6c  its on the compl
1120: 65 78 69 74 79 20 6f 66 20 61 6e 20 0a 2a 2a 20  exity of an .** 
1130: 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 0a 2a  expression..**.*
1140: 2a 20 41 20 76 61 6c 75 65 20 6f 66 20 30 20 75  * A value of 0 u
1150: 73 65 64 20 74 6f 20 6d 65 61 6e 20 74 68 61 74  sed to mean that
1160: 20 74 68 65 20 6c 69 6d 69 74 20 77 61 73 20 6e   the limit was n
1170: 6f 74 20 65 6e 66 6f 72 63 65 64 2e 0a 2a 2a 20  ot enforced..** 
1180: 42 75 74 20 74 68 61 74 20 69 73 20 6e 6f 20 6c  But that is no l
1190: 6f 6e 67 65 72 20 74 72 75 65 2e 20 20 54 68 65  onger true.  The
11a0: 20 6c 69 6d 69 74 20 69 73 20 6e 6f 77 20 73 74   limit is now st
11b0: 72 69 63 74 6c 79 20 65 6e 66 6f 72 63 65 64 0a  rictly enforced.
11c0: 2a 2a 20 61 74 20 61 6c 6c 20 74 69 6d 65 73 2e  ** at all times.
11d0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
11e0: 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54  TE_MAX_EXPR_DEPT
11f0: 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
1200: 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48  E_MAX_EXPR_DEPTH
1210: 20 31 30 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a   1000.#endif../*
1220: 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d 20  .** The maximum 
1230: 6e 75 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20  number of terms 
1240: 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45  in a compound SE
1250: 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
1260: 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  ** The code gene
1270: 72 61 74 6f 72 20 66 6f 72 20 63 6f 6d 70 6f 75  rator for compou
1280: 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nd SELECT statem
1290: 65 6e 74 73 20 64 6f 65 73 20 6f 6e 65 0a 2a 2a  ents does one.**
12a0: 20 6c 65 76 65 6c 20 6f 66 20 72 65 63 75 72 73   level of recurs
12b0: 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 74 65 72  ion for each ter
12c0: 6d 2e 20 20 41 20 73 74 61 63 6b 20 6f 76 65 72  m.  A stack over
12d0: 66 6c 6f 77 20 63 61 6e 20 72 65 73 75 6c 74 0a  flow can result.
12e0: 2a 2a 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72  ** if the number
12f0: 20 6f 66 20 74 65 72 6d 73 20 69 73 20 74 6f 6f   of terms is too
1300: 20 6c 61 72 67 65 2e 20 20 49 6e 20 70 72 61 63   large.  In prac
1310: 74 69 63 65 2c 20 6d 6f 73 74 20 53 51 4c 0a 2a  tice, most SQL.*
1320: 2a 20 6e 65 76 65 72 20 68 61 73 20 6d 6f 72 65  * never has more
1330: 20 74 68 61 6e 20 33 20 6f 72 20 34 20 74 65 72   than 3 or 4 ter
1340: 6d 73 2e 20 20 55 73 65 20 61 20 76 61 6c 75 65  ms.  Use a value
1350: 20 6f 66 20 30 20 74 6f 20 64 69 73 61 62 6c 65   of 0 to disable
1360: 0a 2a 2a 20 61 6e 79 20 6c 69 6d 69 74 20 6f 6e  .** any limit on
1370: 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 74   the number of t
1380: 65 72 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75  erms in a compou
1390: 6e 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 23 69  nt SELECT..*/.#i
13a0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
13b0: 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54  _COMPOUND_SELECT
13c0: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
13d0: 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  _MAX_COMPOUND_SE
13e0: 4c 45 43 54 20 35 30 30 0a 23 65 6e 64 69 66 0a  LECT 500.#endif.
13f0: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1400: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 63  um number of opc
1410: 6f 64 65 73 20 69 6e 20 61 20 56 44 42 45 20 70  odes in a VDBE p
1420: 72 6f 67 72 61 6d 2e 0a 2a 2a 20 4e 6f 74 20 63  rogram..** Not c
1430: 75 72 72 65 6e 74 6c 79 20 65 6e 66 6f 72 63 65  urrently enforce
1440: 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  d..*/.#ifndef SQ
1450: 4c 49 54 45 5f 4d 41 58 5f 56 44 42 45 5f 4f 50  LITE_MAX_VDBE_OP
1460: 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
1470: 5f 4d 41 58 5f 56 44 42 45 5f 4f 50 20 32 35 30  _MAX_VDBE_OP 250
1480: 30 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  00.#endif../*.**
1490: 20 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d   The maximum num
14a0: 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ber of arguments
14b0: 20 74 6f 20 61 6e 20 53 51 4c 20 66 75 6e 63 74   to an SQL funct
14c0: 69 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ion..*/.#ifndef 
14d0: 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43 54  SQLITE_MAX_FUNCT
14e0: 49 4f 4e 5f 41 52 47 0a 23 20 64 65 66 69 6e 65  ION_ARG.# define
14f0: 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43   SQLITE_MAX_FUNC
1500: 54 49 4f 4e 5f 41 52 47 20 31 30 30 0a 23 65 6e  TION_ARG 100.#en
1510: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d  dif../*.** The m
1520: 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66  aximum number of
1530: 20 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73   in-memory pages
1540: 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65 20   to use for the 
1550: 6d 61 69 6e 20 64 61 74 61 62 61 73 65 0a 2a 2a  main database.**
1560: 20 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 74   table and for t
1570: 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
1580: 20 20 54 68 65 20 53 51 4c 49 54 45 5f 44 45 46    The SQLITE_DEF
1590: 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45 0a  AULT_CACHE_SIZE.
15a0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
15b0: 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f  E_DEFAULT_CACHE_
15c0: 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SIZE.# define SQ
15d0: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43  LITE_DEFAULT_CAC
15e0: 48 45 5f 53 49 5a 45 20 20 32 30 30 30 0a 23 65  HE_SIZE  2000.#e
15f0: 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c  ndif.#ifndef SQL
1600: 49 54 45 5f 44 45 46 41 55 4c 54 5f 54 45 4d 50  ITE_DEFAULT_TEMP
1610: 5f 43 41 43 48 45 5f 53 49 5a 45 0a 23 20 64 65  _CACHE_SIZE.# de
1620: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
1630: 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f 53  ULT_TEMP_CACHE_S
1640: 49 5a 45 20 20 35 30 30 0a 23 65 6e 64 69 66 0a  IZE  500.#endif.
1650: 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d  ./*.** The maxim
1660: 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 61 74 74  um number of att
1670: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e  ached databases.
1680: 20 20 54 68 69 73 20 6d 75 73 74 20 62 65 20 62    This must be b
1690: 65 74 77 65 65 6e 20 30 0a 2a 2a 20 61 6e 64 20  etween 0.** and 
16a0: 33 30 2e 20 20 54 68 65 20 75 70 70 65 72 20 62  30.  The upper b
16b0: 6f 75 6e 64 20 6f 6e 20 33 30 20 69 73 20 62 65  ound on 30 is be
16c0: 63 61 75 73 65 20 61 20 33 32 2d 62 69 74 20 69  cause a 32-bit i
16d0: 6e 74 65 67 65 72 20 62 69 74 6d 61 70 0a 2a 2a  nteger bitmap.**
16e0: 20 69 73 20 75 73 65 64 20 69 6e 74 65 72 6e 61   is used interna
16f0: 6c 6c 79 20 74 6f 20 74 72 61 63 6b 20 61 74 74  lly to track att
1700: 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2e  ached databases.
1710: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1720: 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45 44 0a  TE_MAX_ATTACHED.
1730: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1740: 4d 41 58 5f 41 54 54 41 43 48 45 44 20 31 30 0a  MAX_ATTACHED 10.
1750: 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54  #endif.../*.** T
1760: 68 65 20 6d 61 78 69 6d 75 6d 20 76 61 6c 75 65  he maximum value
1770: 20 6f 66 20 61 20 3f 6e 6e 6e 20 77 69 6c 64 63   of a ?nnn wildc
1780: 61 72 64 20 74 68 61 74 20 74 68 65 20 70 61 72  ard that the par
1790: 73 65 72 20 77 69 6c 6c 20 61 63 63 65 70 74 2e  ser will accept.
17a0: 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
17b0: 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
17c0: 4e 55 4d 42 45 52 0a 23 20 64 65 66 69 6e 65 20  NUMBER.# define 
17d0: 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
17e0: 42 4c 45 5f 4e 55 4d 42 45 52 20 39 39 39 0a 23  BLE_NUMBER 999.#
17f0: 65 6e 64 69 66 0a 0a 2f 2a 20 4d 61 78 69 6d 75  endif../* Maximu
1800: 6d 20 70 61 67 65 20 73 69 7a 65 2e 20 20 54 68  m page size.  Th
1810: 65 20 75 70 70 65 72 20 62 6f 75 6e 64 20 6f 6e  e upper bound on
1820: 20 74 68 69 73 20 76 61 6c 75 65 20 69 73 20 33   this value is 3
1830: 32 37 36 38 2e 20 20 54 68 69 73 20 61 20 6c 69  2768.  This a li
1840: 6d 69 74 0a 2a 2a 20 69 6d 70 6f 73 65 64 20 62  mit.** imposed b
1850: 79 20 74 68 65 20 6e 65 63 65 73 73 69 74 79 20  y the necessity 
1860: 6f 66 20 73 74 6f 72 69 6e 67 20 74 68 65 20 76  of storing the v
1870: 61 6c 75 65 20 69 6e 20 61 20 32 2d 62 79 74 65  alue in a 2-byte
1880: 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65   unsigned intege
1890: 72 0a 2a 2a 20 61 6e 64 20 74 68 65 20 66 61 63  r.** and the fac
18a0: 74 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  t that the page 
18b0: 73 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70  size must be a p
18c0: 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2f 0a 23 69  ower of 2..*/.#i
18d0: 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58  fndef SQLITE_MAX
18e0: 5f 50 41 47 45 5f 53 49 5a 45 0a 23 20 64 65 66  _PAGE_SIZE.# def
18f0: 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 50  ine SQLITE_MAX_P
1900: 41 47 45 5f 53 49 5a 45 20 33 32 37 36 38 0a 23  AGE_SIZE 32768.#
1910: 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68  endif.../*.** Th
1920: 65 20 64 65 66 61 75 6c 74 20 73 69 7a 65 20 6f  e default size o
1930: 66 20 61 20 64 61 74 61 62 61 73 65 20 70 61 67  f a database pag
1940: 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
1950: 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41 47  LITE_DEFAULT_PAG
1960: 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65 20  E_SIZE.# define 
1970: 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50  SQLITE_DEFAULT_P
1980: 41 47 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 65  AGE_SIZE 1024.#e
1990: 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f  ndif.#if SQLITE_
19a0: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
19b0: 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47  E>SQLITE_MAX_PAG
19c0: 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65 66 20 53  E_SIZE.# undef S
19d0: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 50 41  QLITE_DEFAULT_PA
19e0: 47 45 5f 53 49 5a 45 0a 23 20 64 65 66 69 6e 65  GE_SIZE.# define
19f0: 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
1a00: 50 41 47 45 5f 53 49 5a 45 20 53 51 4c 49 54 45  PAGE_SIZE SQLITE
1a10: 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a 23  _MAX_PAGE_SIZE.#
1a20: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 72 64  endif../*.** Ord
1a30: 69 6e 61 72 69 6c 79 2c 20 69 66 20 6e 6f 20 76  inarily, if no v
1a40: 61 6c 75 65 20 69 73 20 65 78 70 6c 69 63 69 74  alue is explicit
1a50: 6c 79 20 70 72 6f 76 69 64 65 64 2c 20 53 51 4c  ly provided, SQL
1a60: 69 74 65 20 63 72 65 61 74 65 73 20 64 61 74 61  ite creates data
1a70: 62 61 73 65 73 0a 2a 2a 20 77 69 74 68 20 70 61  bases.** with pa
1a80: 67 65 20 73 69 7a 65 20 53 51 4c 49 54 45 5f 44  ge size SQLITE_D
1a90: 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45  EFAULT_PAGE_SIZE
1aa0: 2e 20 48 6f 77 65 76 65 72 2c 20 62 61 73 65 64  . However, based
1ab0: 20 6f 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 64   on certain.** d
1ac0: 65 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69  evice characteri
1ad0: 73 74 69 63 73 20 28 73 65 63 74 6f 72 2d 73 69  stics (sector-si
1ae0: 7a 65 20 61 6e 64 20 61 74 6f 6d 69 63 20 77 72  ze and atomic wr
1af0: 69 74 65 28 29 20 73 75 70 70 6f 72 74 29 2c 0a  ite() support),.
1b00: 2a 2a 20 53 51 4c 69 74 65 20 6d 61 79 20 63 68  ** SQLite may ch
1b10: 6f 6f 73 65 20 61 20 6c 61 72 67 65 72 20 76 61  oose a larger va
1b20: 6c 75 65 2e 20 54 68 69 73 20 63 6f 6e 73 74 61  lue. This consta
1b30: 6e 74 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75  nt is the maximu
1b40: 6d 20 76 61 6c 75 65 0a 2a 2a 20 53 51 4c 69 74  m value.** SQLit
1b50: 65 20 77 69 6c 6c 20 63 68 6f 6f 73 65 20 6f 6e  e will choose on
1b60: 20 69 74 73 20 6f 77 6e 2e 0a 2a 2f 0a 23 69 66   its own..*/.#if
1b70: 6e 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  ndef SQLITE_MAX_
1b80: 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a  DEFAULT_PAGE_SIZ
1b90: 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
1ba0: 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
1bb0: 47 45 5f 53 49 5a 45 20 38 31 39 32 0a 23 65 6e  GE_SIZE 8192.#en
1bc0: 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
1bd0: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1be0: 53 49 5a 45 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  SIZE>SQLITE_MAX_
1bf0: 50 41 47 45 5f 53 49 5a 45 0a 23 20 75 6e 64 65  PAGE_SIZE.# unde
1c00: 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  f SQLITE_MAX_DEF
1c10: 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 0a 23  AULT_PAGE_SIZE.#
1c20: 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d   define SQLITE_M
1c30: 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f  AX_DEFAULT_PAGE_
1c40: 53 49 5a 45 20 53 51 4c 49 54 45 5f 4d 41 58 5f  SIZE SQLITE_MAX_
1c50: 50 41 47 45 5f 53 49 5a 45 0a 23 65 6e 64 69 66  PAGE_SIZE.#endif
1c60: 0a 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d  .../*.** Maximum
1c70: 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1c80: 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65   in one database
1c90: 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   file..**.** Thi
1ca0: 73 20 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74  s is really just
1cb0: 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
1cc0: 75 65 20 66 6f 72 20 74 68 65 20 6d 61 78 5f 70  ue for the max_p
1cd0: 61 67 65 5f 63 6f 75 6e 74 20 70 72 61 67 6d 61  age_count pragma
1ce0: 2e 0a 2a 2a 20 54 68 69 73 20 76 61 6c 75 65 20  ..** This value 
1cf0: 63 61 6e 20 62 65 20 6c 6f 77 65 72 65 64 20 28  can be lowered (
1d00: 6f 72 20 72 61 69 73 65 64 29 20 61 74 20 72 75  or raised) at ru
1d10: 6e 2d 74 69 6d 65 20 75 73 69 6e 67 20 74 68 61  n-time using tha
1d20: 74 20 74 68 65 0a 2a 2a 20 6d 61 78 5f 70 61 67  t the.** max_pag
1d30: 65 5f 63 6f 75 6e 74 20 6d 61 63 72 6f 2e 0a 2a  e_count macro..*
1d40: 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1d50: 5f 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 0a  _MAX_PAGE_COUNT.
1d60: 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
1d70: 4d 41 58 5f 50 41 47 45 5f 43 4f 55 4e 54 20 31  MAX_PAGE_COUNT 1
1d80: 30 37 33 37 34 31 38 32 33 0a 23 65 6e 64 69 66  073741823.#endif
1d90: 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20  ../*.** Maximum 
1da0: 6c 65 6e 67 74 68 20 28 69 6e 20 62 79 74 65 73  length (in bytes
1db0: 29 20 6f 66 20 74 68 65 20 70 61 74 74 65 72 6e  ) of the pattern
1dc0: 20 69 6e 20 61 20 4c 49 4b 45 20 6f 72 20 47 4c   in a LIKE or GL
1dd0: 4f 42 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 0a  OB.** operator..
1de0: 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
1df0: 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
1e00: 52 4e 5f 4c 45 4e 47 54 48 0a 23 20 64 65 66 69  RN_LENGTH.# defi
1e10: 6e 65 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  ne SQLITE_MAX_LI
1e20: 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
1e30: 48 20 35 30 30 30 30 0a 23 65 6e 64 69 66 0a 0a  H 50000.#endif..
1e40: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1e50: 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 4c 69 6d  End of sqliteLim
1e60: 69 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  it.h ***********
1e70: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e80: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1e90: 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1ea0: 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
1eb0: 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
1ec0: 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
1ed0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1ee0: 0a 2f 2a 20 44 69 73 61 62 6c 65 20 6e 75 69 73  ./* Disable nuis
1ef0: 61 6e 63 65 20 77 61 72 6e 69 6e 67 73 20 6f 6e  ance warnings on
1f00: 20 42 6f 72 6c 61 6e 64 20 63 6f 6d 70 69 6c 65   Borland compile
1f10: 72 73 20 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65  rs */.#if define
1f20: 64 28 5f 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a  d(__BORLANDC__).
1f30: 23 70 72 61 67 6d 61 20 77 61 72 6e 20 2d 72 63  #pragma warn -rc
1f40: 68 20 2f 2a 20 75 6e 72 65 61 63 68 61 62 6c 65  h /* unreachable
1f50: 20 63 6f 64 65 20 2a 2f 0a 23 70 72 61 67 6d 61   code */.#pragma
1f60: 20 77 61 72 6e 20 2d 63 63 63 20 2f 2a 20 43 6f   warn -ccc /* Co
1f70: 6e 64 69 74 69 6f 6e 20 69 73 20 61 6c 77 61 79  ndition is alway
1f80: 73 20 74 72 75 65 20 6f 72 20 66 61 6c 73 65 20  s true or false 
1f90: 2a 2f 0a 23 70 72 61 67 6d 61 20 77 61 72 6e 20  */.#pragma warn 
1fa0: 2d 61 75 73 20 2f 2a 20 41 73 73 69 67 6e 65 64  -aus /* Assigned
1fb0: 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72 20   value is never 
1fc0: 75 73 65 64 20 2a 2f 0a 23 70 72 61 67 6d 61 20  used */.#pragma 
1fd0: 77 61 72 6e 20 2d 63 73 75 20 2f 2a 20 43 6f 6d  warn -csu /* Com
1fe0: 70 61 72 69 6e 67 20 73 69 67 6e 65 64 20 61 6e  paring signed an
1ff0: 64 20 75 6e 73 69 67 6e 65 64 20 2a 2f 0a 23 70  d unsigned */.#p
2000: 72 61 67 6d 61 20 77 61 72 6e 20 2d 73 70 61 20  ragma warn -spa 
2010: 2f 2a 20 53 75 73 70 69 63 6f 75 73 20 70 6f 69  /* Suspicous poi
2020: 6e 74 65 72 20 61 72 69 74 68 6d 65 74 69 63 20  nter arithmetic 
2030: 2a 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4e 65  */.#endif../* Ne
2040: 65 64 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73  eded for various
2050: 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e 2e 2e 20   definitions... 
2060: 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 47 4e 55 5f  */.#ifndef _GNU_
2070: 53 4f 55 52 43 45 0a 23 20 64 65 66 69 6e 65 20  SOURCE.# define 
2080: 5f 47 4e 55 5f 53 4f 55 52 43 45 0a 23 65 6e 64  _GNU_SOURCE.#end
2090: 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 6c 75 64  if../*.** Includ
20a0: 65 20 73 74 61 6e 64 61 72 64 20 68 65 61 64 65  e standard heade
20b0: 72 20 66 69 6c 65 73 20 61 73 20 6e 65 63 65 73  r files as neces
20c0: 73 61 72 79 0a 2a 2f 0a 23 69 66 64 65 66 20 48  sary.*/.#ifdef H
20d0: 41 56 45 5f 53 54 44 49 4e 54 5f 48 0a 23 69 6e  AVE_STDINT_H.#in
20e0: 63 6c 75 64 65 20 3c 73 74 64 69 6e 74 2e 68 3e  clude <stdint.h>
20f0: 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 48  .#endif.#ifdef H
2100: 41 56 45 5f 49 4e 54 54 59 50 45 53 5f 48 0a 23  AVE_INTTYPES_H.#
2110: 69 6e 63 6c 75 64 65 20 3c 69 6e 74 74 79 70 65  include <inttype
2120: 73 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  s.h>.#endif../*.
2130: 2a 2a 20 41 20 6d 61 63 72 6f 20 75 73 65 64 20  ** A macro used 
2140: 74 6f 20 61 69 64 20 69 6e 20 63 6f 76 65 72 61  to aid in covera
2150: 67 65 20 74 65 73 74 69 6e 67 2e 20 20 57 68 65  ge testing.  Whe
2160: 6e 20 64 6f 69 6e 67 20 63 6f 76 65 72 61 67 65  n doing coverage
2170: 0a 2a 2a 20 74 65 73 74 69 6e 67 2c 20 74 68 65  .** testing, the
2180: 20 63 6f 6e 64 69 74 69 6f 6e 20 69 6e 73 69 64   condition insid
2190: 65 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6d  e the argument m
21a0: 75 73 74 20 62 65 20 65 76 61 6c 75 61 74 65 64  ust be evaluated
21b0: 20 0a 2a 2a 20 62 6f 74 68 20 74 72 75 65 20 61   .** both true a
21c0: 6e 64 20 66 61 6c 73 65 20 69 6e 20 6f 72 64 65  nd false in orde
21d0: 72 20 74 6f 20 67 65 74 20 66 75 6c 6c 20 62 72  r to get full br
21e0: 61 6e 63 68 20 63 6f 76 65 72 61 67 65 2e 0a 2a  anch coverage..*
21f0: 2a 20 54 68 69 73 20 6d 61 63 72 6f 20 63 61 6e  * This macro can
2200: 20 62 65 20 69 6e 73 65 72 74 65 64 20 74 6f 20   be inserted to 
2210: 65 6e 73 75 72 65 20 61 64 65 71 75 61 74 65 20  ensure adequate 
2220: 74 65 73 74 20 63 6f 76 65 72 61 67 65 0a 2a 2a  test coverage.**
2230: 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65   in places where
2240: 20 73 69 6d 70 6c 65 20 63 6f 6e 64 69 74 69 6f   simple conditio
2250: 6e 2f 64 65 63 69 73 69 6f 6e 20 63 6f 76 65 72  n/decision cover
2260: 61 67 65 20 69 73 20 69 6e 61 64 65 71 75 61 74  age is inadequat
2270: 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
2280: 49 54 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53  ITE_COVERAGE_TES
2290: 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  T.SQLITE_PRIVATE
22a0: 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43     void sqlite3C
22b0: 6f 76 65 72 61 67 65 28 69 6e 74 29 3b 0a 23 20  overage(int);.# 
22c0: 64 65 66 69 6e 65 20 74 65 73 74 63 61 73 65 28  define testcase(
22d0: 58 29 20 20 69 66 28 20 58 20 29 7b 20 73 71 6c  X)  if( X ){ sql
22e0: 69 74 65 33 43 6f 76 65 72 61 67 65 28 5f 5f 4c  ite3Coverage(__L
22f0: 49 4e 45 5f 5f 29 3b 20 7d 0a 23 65 6c 73 65 0a  INE__); }.#else.
2300: 23 20 64 65 66 69 6e 65 20 74 65 73 74 63 61 73  # define testcas
2310: 65 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  e(X).#endif../*.
2320: 2a 2a 20 54 68 65 20 41 4c 57 41 59 53 20 61 6e  ** The ALWAYS an
2330: 64 20 4e 45 56 45 52 20 6d 61 63 72 6f 73 20 73  d NEVER macros s
2340: 75 72 72 6f 75 6e 64 20 62 6f 6f 6c 65 61 6e 20  urround boolean 
2350: 65 78 70 72 65 73 73 69 6f 6e 73 20 77 68 69 63  expressions whic
2360: 68 20 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64  h .** are intend
2370: 65 64 20 74 6f 20 61 6c 77 61 79 73 20 62 65 20  ed to always be 
2380: 74 72 75 65 20 6f 72 20 66 61 6c 73 65 2c 20 72  true or false, r
2390: 65 73 70 65 63 74 69 76 65 6c 79 2e 20 20 53 75  espectively.  Su
23a0: 63 68 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e  ch.** expression
23b0: 73 20 63 6f 75 6c 64 20 62 65 20 6f 6d 69 74 74  s could be omitt
23c0: 65 64 20 66 72 6f 6d 20 74 68 65 20 63 6f 64 65  ed from the code
23d0: 20 63 6f 6d 70 6c 65 74 65 6c 79 2e 20 20 42 75   completely.  Bu
23e0: 74 20 74 68 65 79 0a 2a 2a 20 61 72 65 20 69 6e  t they.** are in
23f0: 63 6c 75 64 65 64 20 69 6e 20 61 20 66 65 77 20  cluded in a few 
2400: 63 61 73 65 73 20 69 6e 20 6f 72 64 65 72 20 74  cases in order t
2410: 6f 20 65 6e 68 61 6e 63 65 20 74 68 65 20 72 65  o enhance the re
2420: 73 69 6c 69 65 6e 63 65 0a 2a 2a 20 6f 66 20 53  silience.** of S
2430: 51 4c 69 74 65 20 74 6f 20 75 6e 65 78 70 65 63  QLite to unexpec
2440: 74 65 64 20 62 65 68 61 76 69 6f 72 20 2d 20 74  ted behavior - t
2450: 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f 64 65 20  o make the code 
2460: 22 73 65 6c 66 2d 68 65 61 6c 69 6e 67 22 0a 2a  "self-healing".*
2470: 2a 20 6f 72 20 22 64 75 63 74 69 6c 65 22 20 72  * or "ductile" r
2480: 61 74 68 65 72 20 74 68 61 6e 20 62 65 69 6e 67  ather than being
2490: 20 22 62 72 69 74 74 6c 65 22 20 61 6e 64 20 63   "brittle" and c
24a0: 72 61 73 68 69 6e 67 20 61 74 20 74 68 65 20 66  rashing at the f
24b0: 69 72 73 74 0a 2a 2a 20 68 69 6e 74 20 6f 66 20  irst.** hint of 
24c0: 75 6e 70 6c 61 6e 6e 65 64 20 62 65 68 61 76 69  unplanned behavi
24d0: 6f 72 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 64  or..**.** When d
24e0: 6f 69 6e 67 20 63 6f 76 65 72 61 67 65 20 74 65  oing coverage te
24f0: 73 74 69 6e 67 20 41 4c 57 41 59 53 20 61 6e 64  sting ALWAYS and
2500: 20 4e 45 56 45 52 20 61 72 65 20 68 61 72 64 2d   NEVER are hard-
2510: 63 6f 64 65 64 20 74 6f 0a 2a 2a 20 62 65 20 74  coded to.** be t
2520: 72 75 65 20 61 6e 64 20 66 61 6c 73 65 20 73 6f  rue and false so
2530: 20 74 68 61 74 20 74 68 65 20 75 6e 72 65 61 63   that the unreac
2540: 68 61 62 6c 65 20 63 6f 64 65 20 74 68 65 6e 20  hable code then 
2550: 73 70 65 63 69 66 79 20 77 69 6c 6c 0a 2a 2a 20  specify will.** 
2560: 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64 20 61  not be counted a
2570: 73 20 75 6e 74 65 73 74 65 64 20 63 6f 64 65 2e  s untested code.
2580: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
2590: 45 5f 43 4f 56 45 52 41 47 45 5f 54 45 53 54 0a  E_COVERAGE_TEST.
25a0: 23 20 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28  # define ALWAYS(
25b0: 58 29 20 20 20 20 20 20 28 31 29 0a 23 20 64 65  X)      (1).# de
25c0: 66 69 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20  fine NEVER(X)   
25d0: 20 20 20 20 28 30 29 0a 23 65 6c 73 65 0a 23 20      (0).#else.# 
25e0: 64 65 66 69 6e 65 20 41 4c 57 41 59 53 28 58 29  define ALWAYS(X)
25f0: 20 20 20 20 20 20 28 58 29 0a 23 20 64 65 66 69        (X).# defi
2600: 6e 65 20 4e 45 56 45 52 28 58 29 20 20 20 20 20  ne NEVER(X)     
2610: 20 20 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a    (X).#endif../*
2620: 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 75 6e  .** The macro un
2630: 6c 69 6b 65 6c 79 28 29 20 69 73 20 61 20 68 69  likely() is a hi
2640: 6e 74 20 74 68 61 74 20 73 75 72 72 6f 75 6e 64  nt that surround
2650: 73 20 61 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 65  s a boolean.** e
2660: 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 69  xpression that i
2670: 73 20 75 73 75 61 6c 6c 79 20 66 61 6c 73 65 2e  s usually false.
2680: 20 20 4d 61 63 72 6f 20 6c 69 6b 65 6c 79 28 29    Macro likely()
2690: 20 73 75 72 72 6f 75 6e 64 73 0a 2a 2a 20 61 20   surrounds.** a 
26a0: 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73 69  boolean expressi
26b0: 6f 6e 20 74 68 61 74 20 69 73 20 75 73 75 61 6c  on that is usual
26c0: 6c 79 20 74 72 75 65 2e 20 20 47 43 43 20 69 73  ly true.  GCC is
26d0: 20 61 62 6c 65 20 74 6f 0a 2a 2a 20 75 73 65 20   able to.** use 
26e0: 74 68 65 73 65 20 68 69 6e 74 73 20 74 6f 20 67  these hints to g
26f0: 65 6e 65 72 61 74 65 20 62 65 74 74 65 72 20 63  enerate better c
2700: 6f 64 65 2c 20 73 6f 6d 65 74 69 6d 65 73 2e 0a  ode, sometimes..
2710: 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f  */.#if defined(_
2720: 5f 47 4e 55 43 5f 5f 29 20 26 26 20 30 0a 23 20  _GNUC__) && 0.# 
2730: 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28 58 29  define likely(X)
2740: 20 20 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78      __builtin_ex
2750: 70 65 63 74 28 28 58 29 2c 31 29 0a 23 20 64 65  pect((X),1).# de
2760: 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29  fine unlikely(X)
2770: 20 20 5f 5f 62 75 69 6c 74 69 6e 5f 65 78 70 65    __builtin_expe
2780: 63 74 28 28 58 29 2c 30 29 0a 23 65 6c 73 65 0a  ct((X),0).#else.
2790: 23 20 64 65 66 69 6e 65 20 6c 69 6b 65 6c 79 28  # define likely(
27a0: 58 29 20 20 20 20 21 21 28 58 29 0a 23 20 64 65  X)    !!(X).# de
27b0: 66 69 6e 65 20 75 6e 6c 69 6b 65 6c 79 28 58 29  fine unlikely(X)
27c0: 20 20 21 21 28 58 29 0a 23 65 6e 64 69 66 0a 0a    !!(X).#endif..
27d0: 2f 2a 0a 20 2a 20 54 68 69 73 20 6d 61 63 72 6f  /*. * This macro
27e0: 20 69 73 20 75 73 65 64 20 74 6f 20 22 68 69 64   is used to "hid
27f0: 65 22 20 73 6f 6d 65 20 75 67 6c 69 6e 65 73 73  e" some ugliness
2800: 20 69 6e 20 63 61 73 74 69 6e 67 20 61 6e 20 69   in casting an i
2810: 6e 74 0a 20 2a 20 76 61 6c 75 65 20 74 6f 20 61  nt. * value to a
2820: 20 70 74 72 20 76 61 6c 75 65 20 75 6e 64 65 72   ptr value under
2830: 20 74 68 65 20 4d 53 56 43 20 36 34 2d 62 69 74   the MSVC 64-bit
2840: 20 63 6f 6d 70 69 6c 65 72 2e 20 20 20 43 61 73   compiler.   Cas
2850: 74 69 6e 67 0a 20 2a 20 6e 6f 6e 20 36 34 2d 62  ting. * non 64-b
2860: 69 74 20 76 61 6c 75 65 73 20 74 6f 20 70 74 72  it values to ptr
2870: 20 74 79 70 65 73 20 72 65 73 75 6c 74 73 20 69   types results i
2880: 6e 20 61 20 22 68 61 72 64 22 20 65 72 72 6f 72  n a "hard" error
2890: 20 77 69 74 68 20 0a 20 2a 20 74 68 65 20 4d 53   with . * the MS
28a0: 56 43 20 36 34 2d 62 69 74 20 63 6f 6d 70 69 6c  VC 64-bit compil
28b0: 65 72 20 77 68 69 63 68 20 74 68 69 73 20 61 74  er which this at
28c0: 74 65 6d 70 74 73 20 74 6f 20 61 76 6f 69 64 2e  tempts to avoid.
28d0: 20 20 0a 20 2a 0a 20 2a 20 41 20 73 69 6d 70 6c    . *. * A simpl
28e0: 65 20 63 6f 6d 70 69 6c 65 72 20 70 72 61 67 6d  e compiler pragm
28f0: 61 20 6f 72 20 63 61 73 74 69 6e 67 20 73 65 71  a or casting seq
2900: 75 65 6e 63 65 20 63 6f 75 6c 64 20 6e 6f 74 20  uence could not 
2910: 62 65 20 66 6f 75 6e 64 0a 20 2a 20 74 6f 20 63  be found. * to c
2920: 6f 72 72 65 63 74 20 74 68 69 73 20 69 6e 20 61  orrect this in a
2930: 6c 6c 20 73 69 74 75 61 74 69 6f 6e 73 2c 20 73  ll situations, s
2940: 6f 20 74 68 69 73 20 6d 61 63 72 6f 20 77 61 73  o this macro was
2950: 20 69 6e 74 72 6f 64 75 63 65 64 2e 0a 20 2a 0a   introduced.. *.
2960: 20 2a 20 49 74 20 63 6f 75 6c 64 20 62 65 20 61   * It could be a
2970: 72 67 75 65 64 20 74 68 61 74 20 74 68 65 20 69  rgued that the i
2980: 6e 74 70 74 72 5f 74 20 74 79 70 65 20 63 6f 75  ntptr_t type cou
2990: 6c 64 20 62 65 20 75 73 65 64 20 69 6e 20 74 68  ld be used in th
29a0: 69 73 0a 20 2a 20 63 61 73 65 2c 20 62 75 74 20  is. * case, but 
29b0: 74 68 61 74 20 74 79 70 65 20 69 73 20 6e 6f 74  that type is not
29c0: 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20 61 6c   available on al
29d0: 6c 20 63 6f 6d 70 69 6c 65 72 73 2c 20 6f 72 20  l compilers, or 
29e0: 0a 20 2a 20 72 65 71 75 69 72 65 73 20 74 68 65  . * requires the
29f0: 20 23 69 6e 63 6c 75 64 65 20 6f 66 20 73 70 65   #include of spe
2a00: 63 69 66 69 63 20 68 65 61 64 65 72 73 20 77 68  cific headers wh
2a10: 69 63 68 20 64 69 66 66 65 72 73 20 62 65 74 77  ich differs betw
2a20: 65 65 6e 0a 20 2a 20 70 6c 61 74 66 6f 72 6d 73  een. * platforms
2a30: 2e 0a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  .. */.#define SQ
2a40: 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
2a50: 58 29 20 20 20 28 28 76 6f 69 64 2a 29 26 28 28  X)   ((void*)&((
2a60: 63 68 61 72 2a 29 30 29 5b 58 5d 29 0a 23 64 65  char*)0)[X]).#de
2a70: 66 69 6e 65 20 53 51 4c 49 54 45 5f 50 54 52 5f  fine SQLITE_PTR_
2a80: 54 4f 5f 49 4e 54 28 58 29 20 20 20 28 28 69 6e  TO_INT(X)   ((in
2a90: 74 29 28 28 28 63 68 61 72 2a 29 58 29 2d 28 63  t)(((char*)X)-(c
2aa0: 68 61 72 2a 29 30 29 29 0a 0a 2f 2a 0a 2a 2a 20  har*)0))../*.** 
2ab0: 54 68 65 73 65 20 23 64 65 66 69 6e 65 73 20 73  These #defines s
2ac0: 68 6f 75 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47  hould enable >2G
2ad0: 42 20 66 69 6c 65 20 73 75 70 70 6f 72 74 20 6f  B file support o
2ae0: 6e 20 50 6f 73 69 78 20 69 66 20 74 68 65 0a 2a  n Posix if the.*
2af0: 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65  * underlying ope
2b00: 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 73 75  rating system su
2b10: 70 70 6f 72 74 73 20 69 74 2e 20 20 49 66 20 74  pports it.  If t
2b20: 68 65 20 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c  he OS lacks.** l
2b30: 61 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72  arge file suppor
2b40: 74 2c 20 6f 72 20 69 66 20 74 68 65 20 4f 53 20  t, or if the OS 
2b50: 69 73 20 77 69 6e 64 6f 77 73 2c 20 74 68 65 73  is windows, thes
2b60: 65 20 73 68 6f 75 6c 64 20 62 65 20 6e 6f 2d 6f  e should be no-o
2b70: 70 73 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74  ps..**.** Ticket
2b80: 20 23 32 37 33 39 3a 20 20 54 68 65 20 5f 4c 41   #2739:  The _LA
2b90: 52 47 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 6d  RGEFILE_SOURCE m
2ba0: 61 63 72 6f 20 6d 75 73 74 20 61 70 70 65 61 72  acro must appear
2bb0: 20 62 65 66 6f 72 65 20 61 6e 79 0a 2a 2a 20 73   before any.** s
2bc0: 79 73 74 65 6d 20 23 69 6e 63 6c 75 64 65 73 2e  ystem #includes.
2bd0: 20 20 48 65 6e 63 65 2c 20 74 68 69 73 20 62 6c    Hence, this bl
2be0: 6f 63 6b 20 6f 66 20 63 6f 64 65 20 6d 75 73 74  ock of code must
2bf0: 20 62 65 20 74 68 65 20 76 65 72 79 20 66 69 72   be the very fir
2c00: 73 74 0a 2a 2a 20 63 6f 64 65 20 69 6e 20 61 6c  st.** code in al
2c10: 6c 20 73 6f 75 72 63 65 20 66 69 6c 65 73 2e 0a  l source files..
2c20: 2a 2a 0a 2a 2a 20 4c 61 72 67 65 20 66 69 6c 65  **.** Large file
2c30: 20 73 75 70 70 6f 72 74 20 63 61 6e 20 62 65 20   support can be 
2c40: 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67 20 74  disabled using t
2c50: 68 65 20 2d 44 53 51 4c 49 54 45 5f 44 49 53 41  he -DSQLITE_DISA
2c60: 42 4c 45 5f 4c 46 53 20 73 77 69 74 63 68 0a 2a  BLE_LFS switch.*
2c70: 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  * on the compile
2c80: 72 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 20  r command line. 
2c90: 20 54 68 69 73 20 69 73 20 6e 65 63 65 73 73 61   This is necessa
2ca0: 72 79 20 69 66 20 79 6f 75 20 61 72 65 20 63 6f  ry if you are co
2cb0: 6d 70 69 6c 69 6e 67 0a 2a 2a 20 6f 6e 20 61 20  mpiling.** on a 
2cc0: 72 65 63 65 6e 74 20 6d 61 63 68 69 6e 65 20 28  recent machine (
2cd0: 65 78 3a 20 52 65 64 48 61 74 20 37 2e 32 29 20  ex: RedHat 7.2) 
2ce0: 62 75 74 20 79 6f 75 20 77 61 6e 74 20 79 6f 75  but you want you
2cf0: 72 20 63 6f 64 65 20 74 6f 20 77 6f 72 6b 0a 2a  r code to work.*
2d00: 2a 20 6f 6e 20 61 6e 20 6f 6c 64 65 72 20 6d 61  * on an older ma
2d10: 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48 61  chine (ex: RedHa
2d20: 74 20 36 2e 30 29 2e 20 20 49 66 20 79 6f 75 20  t 6.0).  If you 
2d30: 63 6f 6d 70 69 6c 65 20 6f 6e 20 52 65 64 48 61  compile on RedHa
2d40: 74 20 37 2e 32 0a 2a 2a 20 77 69 74 68 6f 75 74  t 7.2.** without
2d50: 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20 4c 46   this option, LF
2d60: 53 20 69 73 20 65 6e 61 62 6c 65 2e 20 20 42 75  S is enable.  Bu
2d70: 74 20 4c 46 53 20 64 6f 65 73 20 6e 6f 74 20 65  t LFS does not e
2d80: 78 69 73 74 20 69 6e 20 74 68 65 20 6b 65 72 6e  xist in the kern
2d90: 65 6c 0a 2a 2a 20 69 6e 20 52 65 64 48 61 74 20  el.** in RedHat 
2da0: 36 2e 30 2c 20 73 6f 20 74 68 65 20 63 6f 64 65  6.0, so the code
2db0: 20 77 6f 6e 27 74 20 77 6f 72 6b 2e 20 20 48 65   won't work.  He
2dc0: 6e 63 65 2c 20 66 6f 72 20 6d 61 78 69 6d 75 6d  nce, for maximum
2dd0: 20 62 69 6e 61 72 79 0a 2a 2a 20 70 6f 72 74 61   binary.** porta
2de0: 62 69 6c 69 74 79 20 79 6f 75 20 73 68 6f 75 6c  bility you shoul
2df0: 64 20 6f 6d 69 74 20 4c 46 53 2e 0a 2a 2a 0a 2a  d omit LFS..**.*
2e00: 2a 20 53 69 6d 69 6c 61 72 20 69 73 20 74 72 75  * Similar is tru
2e10: 65 20 66 6f 72 20 4d 61 63 4f 53 2e 20 20 4c 46  e for MacOS.  LF
2e20: 53 20 69 73 20 6f 6e 6c 79 20 73 75 70 70 6f 72  S is only suppor
2e30: 74 65 64 20 6f 6e 20 4d 61 63 4f 53 20 39 20 61  ted on MacOS 9 a
2e40: 6e 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 23 69 66  nd later..*/.#if
2e50: 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41  ndef SQLITE_DISA
2e60: 42 4c 45 5f 4c 46 53 0a 23 20 64 65 66 69 6e 65  BLE_LFS.# define
2e70: 20 5f 4c 41 52 47 45 5f 46 49 4c 45 20 20 20 20   _LARGE_FILE    
2e80: 20 20 20 31 0a 23 20 69 66 6e 64 65 66 20 5f 46     1.# ifndef _F
2e90: 49 4c 45 5f 4f 46 46 53 45 54 5f 42 49 54 53 0a  ILE_OFFSET_BITS.
2ea0: 23 20 20 20 64 65 66 69 6e 65 20 5f 46 49 4c 45  #   define _FILE
2eb0: 5f 4f 46 46 53 45 54 5f 42 49 54 53 20 36 34 0a  _OFFSET_BITS 64.
2ec0: 23 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65  # endif.# define
2ed0: 20 5f 4c 41 52 47 45 46 49 4c 45 5f 53 4f 55 52   _LARGEFILE_SOUR
2ee0: 43 45 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  CE 1.#endif.../*
2ef0: 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 54  .** The SQLITE_T
2f00: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 20  HREADSAFE macro 
2f10: 6d 75 73 74 20 62 65 20 64 65 66 69 6e 65 64 20  must be defined 
2f20: 61 73 20 65 69 74 68 65 72 20 30 20 6f 72 20 31  as either 0 or 1
2f30: 2e 0a 2a 2a 20 4f 6c 64 65 72 20 76 65 72 73 69  ..** Older versi
2f40: 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65 20 75 73  ons of SQLite us
2f50: 65 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 54  ed an optional T
2f60: 48 52 45 41 44 53 41 46 45 20 6d 61 63 72 6f 2e  HREADSAFE macro.
2f70: 0a 2a 2a 20 57 65 20 73 75 70 70 6f 72 74 20 74  .** We support t
2f80: 68 61 74 20 66 6f 72 20 6c 65 67 61 63 79 0a 2a  hat for legacy.*
2f90: 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
2fa0: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
2fb0: 29 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48  ).#if defined(TH
2fc0: 52 45 41 44 53 41 46 45 29 0a 23 20 64 65 66 69  READSAFE).# defi
2fd0: 6e 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  ne SQLITE_THREAD
2fe0: 53 41 46 45 20 54 48 52 45 41 44 53 41 46 45 0a  SAFE THREADSAFE.
2ff0: 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 53  #else.# define S
3000: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
3010: 20 31 0a 23 65 6e 64 69 66 0a 23 65 6e 64 69 66   1.#endif.#endif
3020: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  ../*.** The SQLI
3030: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
3040: 41 54 55 53 20 6d 61 63 72 6f 20 6d 75 73 74 20  ATUS macro must 
3050: 62 65 20 64 65 66 69 6e 65 64 20 61 73 20 65 69  be defined as ei
3060: 74 68 65 72 20 30 20 6f 72 20 31 2e 0a 2a 2a 20  ther 0 or 1..** 
3070: 49 74 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  It determines wh
3080: 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65  ether or not the
3090: 20 66 65 61 74 75 72 65 73 20 72 65 6c 61 74 65   features relate
30a0: 64 20 74 6f 20 0a 2a 2a 20 53 51 4c 49 54 45 5f  d to .** SQLITE_
30b0: 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41 54 55 53  CONFIG_MEMSTATUS
30c0: 20 61 72 65 20 61 76 61 69 6c 61 62 65 20 62 79   are availabe by
30d0: 20 64 65 66 61 75 6c 74 20 6f 72 20 6e 6f 74 2e   default or not.
30e0: 20 54 68 69 73 20 76 61 6c 75 65 20 63 61 6e 0a   This value can.
30f0: 2a 2a 20 62 65 20 6f 76 65 72 72 69 64 64 65 6e  ** be overridden
3100: 20 61 74 20 72 75 6e 74 69 6d 65 20 75 73 69 6e   at runtime usin
3110: 67 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f  g the sqlite3_co
3120: 6e 66 69 67 28 29 20 41 50 49 2e 0a 2a 2f 0a 23  nfig() API..*/.#
3130: 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
3140: 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45 4d 53 54  TE_DEFAULT_MEMST
3150: 41 54 55 53 29 0a 23 20 64 65 66 69 6e 65 20 53  ATUS).# define S
3160: 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4d 45  QLITE_DEFAULT_ME
3170: 4d 53 54 41 54 55 53 20 31 0a 23 65 6e 64 69 66  MSTATUS 1.#endif
3180: 0a 0a 2f 2a 0a 2a 2a 20 45 78 61 63 74 6c 79 20  ../*.** Exactly 
3190: 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  one of the follo
31a0: 77 69 6e 67 20 6d 61 63 72 6f 73 20 6d 75 73 74  wing macros must
31b0: 20 62 65 20 64 65 66 69 6e 65 64 20 69 6e 20 6f   be defined in o
31c0: 72 64 65 72 20 74 6f 0a 2a 2a 20 73 70 65 63 69  rder to.** speci
31d0: 66 79 20 77 68 69 63 68 20 6d 65 6d 6f 72 79 20  fy which memory 
31e0: 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
31f0: 73 74 65 6d 20 74 6f 20 75 73 65 2e 0a 2a 2a 0a  stem to use..**.
3200: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 53 59  **     SQLITE_SY
3210: 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  STEM_MALLOC     
3220: 20 20 20 20 20 2f 2f 20 55 73 65 20 6e 6f 72 6d       // Use norm
3230: 61 6c 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  al system malloc
3240: 28 29 0a 2a 2a 20 20 20 20 20 53 51 4c 49 54 45  ().**     SQLITE
3250: 5f 4d 45 4d 44 45 42 55 47 20 20 20 20 20 20 20  _MEMDEBUG       
3260: 20 20 20 20 20 20 20 20 2f 2f 20 44 65 62 75 67          // Debug
3270: 67 69 6e 67 20 76 65 72 73 69 6f 6e 20 6f 66 20  ging version of 
3280: 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63 28 29 0a  system malloc().
3290: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 45  **     SQLITE_ME
32a0: 4d 4f 52 59 5f 53 49 5a 45 20 20 20 20 20 20 20  MORY_SIZE       
32b0: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c       // internal
32c0: 20 61 6c 6c 6f 63 61 74 6f 72 20 23 31 0a 2a 2a   allocator #1.**
32d0: 20 20 20 20 20 53 51 4c 49 54 45 5f 4d 4d 41 50       SQLITE_MMAP
32e0: 5f 48 45 41 50 5f 53 49 5a 45 20 20 20 20 20 20  _HEAP_SIZE      
32f0: 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c 20 6d     // internal m
3300: 6d 61 70 28 29 20 61 6c 6c 6f 63 61 74 6f 72 0a  map() allocator.
3310: 2a 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 50 4f  **     SQLITE_PO
3320: 57 32 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 20 20  W2_MEMORY_SIZE  
3330: 20 20 20 20 20 2f 2f 20 69 6e 74 65 72 6e 61 6c       // internal
3340: 20 70 6f 77 65 72 2d 6f 66 2d 74 77 6f 20 61 6c   power-of-two al
3350: 6c 6f 63 61 74 6f 72 0a 2a 2a 0a 2a 2a 20 49 66  locator.**.** If
3360: 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20 61 62 6f   none of the abo
3370: 76 65 20 61 72 65 20 64 65 66 69 6e 65 64 2c 20  ve are defined, 
3380: 74 68 65 6e 20 73 65 74 20 53 51 4c 49 54 45 5f  then set SQLITE_
3390: 53 59 53 54 45 4d 5f 4d 41 4c 4c 4f 43 20 61 73  SYSTEM_MALLOC as
33a0: 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74 2e  .** the default.
33b0: 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
33c0: 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41  SQLITE_SYSTEM_MA
33d0: 4c 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51  LLOC)+defined(SQ
33e0: 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c  LITE_MEMDEBUG)+\
33f0: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
3400: 49 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29  ITE_MEMORY_SIZE)
3410: 2b 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  +defined(SQLITE_
3420: 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b  MMAP_HEAP_SIZE)+
3430: 5c 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51  \.    defined(SQ
3440: 4c 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59  LITE_POW2_MEMORY
3450: 5f 53 49 5a 45 29 3e 31 0a 23 20 65 72 72 6f 72  _SIZE)>1.# error
3460: 20 22 41 74 20 6d 6f 73 74 20 6f 6e 65 20 6f 66   "At most one of
3470: 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
3480: 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 63 6f 6e 66  ompile-time conf
3490: 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e  iguration option
34a0: 73 5c 0a 20 69 73 20 61 6c 6c 6f 77 73 3a 20 53  s\. is allows: S
34b0: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
34c0: 4c 4f 43 2c 20 53 51 4c 49 54 45 5f 4d 45 4d 44  LOC, SQLITE_MEMD
34d0: 45 42 55 47 2c 20 53 51 4c 49 54 45 5f 4d 45 4d  EBUG, SQLITE_MEM
34e0: 4f 52 59 5f 53 49 5a 45 2c 5c 0a 20 53 51 4c 49  ORY_SIZE,\. SQLI
34f0: 54 45 5f 4d 4d 41 50 5f 48 45 41 50 5f 53 49 5a  TE_MMAP_HEAP_SIZ
3500: 45 2c 20 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d  E, SQLITE_POW2_M
3510: 45 4d 4f 52 59 5f 53 49 5a 45 22 0a 23 65 6e 64  EMORY_SIZE".#end
3520: 69 66 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  if.#if defined(S
3530: 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
3540: 4c 4f 43 29 2b 64 65 66 69 6e 65 64 28 53 51 4c  LOC)+defined(SQL
3550: 49 54 45 5f 4d 45 4d 44 45 42 55 47 29 2b 5c 0a  ITE_MEMDEBUG)+\.
3560: 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c 49      defined(SQLI
3570: 54 45 5f 4d 45 4d 4f 52 59 5f 53 49 5a 45 29 2b  TE_MEMORY_SIZE)+
3580: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
3590: 4d 41 50 5f 48 45 41 50 5f 53 49 5a 45 29 2b 5c  MAP_HEAP_SIZE)+\
35a0: 0a 20 20 20 20 64 65 66 69 6e 65 64 28 53 51 4c  .    defined(SQL
35b0: 49 54 45 5f 50 4f 57 32 5f 4d 45 4d 4f 52 59 5f  ITE_POW2_MEMORY_
35c0: 53 49 5a 45 29 3d 3d 30 0a 23 20 64 65 66 69 6e  SIZE)==0.# defin
35d0: 65 20 53 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f  e SQLITE_SYSTEM_
35e0: 4d 41 4c 4c 4f 43 20 31 0a 23 65 6e 64 69 66 0a  MALLOC 1.#endif.
35f0: 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45  ./*.** If SQLITE
3600: 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
3610: 49 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74  IT is defined, t
3620: 68 65 6e 20 74 72 79 20 74 6f 20 6b 65 65 70 20  hen try to keep 
3630: 74 68 65 0a 2a 2a 20 73 69 7a 65 73 20 6f 66 20  the.** sizes of 
3640: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
3650: 6e 73 20 62 65 6c 6f 77 20 74 68 69 73 20 76 61  ns below this va
3660: 6c 75 65 20 77 68 65 72 65 20 70 6f 73 73 69 62  lue where possib
3670: 6c 65 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  le..*/.#if defin
3680: 65 64 28 53 51 4c 49 54 45 5f 50 4f 57 32 5f 4d  ed(SQLITE_POW2_M
3690: 45 4d 4f 52 59 5f 53 49 5a 45 29 20 26 26 20 21  EMORY_SIZE) && !
36a0: 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
36b0: 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d 49 54  ALLOC_SOFT_LIMIT
36c0: 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
36d0: 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49  E_MALLOC_SOFT_LI
36e0: 4d 49 54 20 31 30 32 34 0a 23 65 6e 64 69 66 0a  MIT 1024.#endif.
36f0: 0a 2f 2a 0a 2a 2a 20 57 65 20 6e 65 65 64 20 74  ./*.** We need t
3700: 6f 20 64 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f  o define _XOPEN_
3710: 53 4f 55 52 43 45 20 61 73 20 66 6f 6c 6c 6f 77  SOURCE as follow
3720: 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 65 6e  s in order to en
3730: 61 62 6c 65 0a 2a 2a 20 72 65 63 75 72 73 69 76  able.** recursiv
3740: 65 20 6d 75 74 65 78 65 73 20 6f 6e 20 6d 6f 73  e mutexes on mos
3750: 74 20 75 6e 69 78 20 73 79 73 74 65 6d 73 2e 20  t unix systems. 
3760: 20 42 75 74 20 4d 61 63 20 4f 53 20 58 20 69 73   But Mac OS X is
3770: 20 64 69 66 66 65 72 65 6e 74 2e 0a 2a 2a 20 54   different..** T
3780: 68 65 20 5f 58 4f 50 45 4e 5f 53 4f 55 52 43 45  he _XOPEN_SOURCE
3790: 20 64 65 66 69 6e 65 20 63 61 75 73 65 73 20 70   define causes p
37a0: 72 6f 62 6c 65 6d 73 20 66 6f 72 20 4d 61 63 20  roblems for Mac 
37b0: 4f 53 20 58 20 77 65 20 61 72 65 20 74 6f 6c 64  OS X we are told
37c0: 2c 0a 2a 2a 20 73 6f 20 69 74 20 69 73 20 6f 6d  ,.** so it is om
37d0: 69 74 74 65 64 20 74 68 65 72 65 2e 20 20 53 65  itted there.  Se
37e0: 65 20 74 69 63 6b 65 74 20 23 32 36 37 33 2e 0a  e ticket #2673..
37f0: 2a 2a 0a 2a 2a 20 4c 61 74 65 72 20 77 65 20 6c  **.** Later we l
3800: 65 61 72 6e 20 74 68 61 74 20 5f 58 4f 50 45 4e  earn that _XOPEN
3810: 5f 53 4f 55 52 43 45 20 69 73 20 70 6f 6f 72 6c  _SOURCE is poorl
3820: 79 20 6f 72 20 69 6e 63 6f 72 72 65 63 74 6c 79  y or incorrectly
3830: 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20  .** implemented 
3840: 6f 6e 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 2e  on some systems.
3850: 20 20 53 6f 20 77 65 20 61 76 6f 69 64 20 64 65    So we avoid de
3860: 66 69 6e 69 6e 67 20 69 74 20 61 74 20 61 6c 6c  fining it at all
3870: 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 6c 72  .** if it is alr
3880: 65 61 64 79 20 64 65 66 69 6e 65 64 20 6f 72 20  eady defined or 
3890: 69 66 20 69 74 20 69 73 20 75 6e 6e 65 65 64 65  if it is unneede
38a0: 64 20 62 65 63 61 75 73 65 20 77 65 20 61 72 65  d because we are
38b0: 0a 2a 2a 20 6e 6f 74 20 64 6f 69 6e 67 20 61 20  .** not doing a 
38c0: 74 68 72 65 61 64 73 61 66 65 20 62 75 69 6c 64  threadsafe build
38d0: 2e 20 20 54 69 63 6b 65 74 20 23 32 36 38 31 2e  .  Ticket #2681.
38e0: 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20  .**.** See also 
38f0: 74 69 63 6b 65 74 20 23 32 37 34 31 2e 0a 2a 2f  ticket #2741..*/
3900: 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 5f 58  .#if !defined(_X
3910: 4f 50 45 4e 5f 53 4f 55 52 43 45 29 20 26 26 20  OPEN_SOURCE) && 
3920: 21 64 65 66 69 6e 65 64 28 5f 5f 44 41 52 57 49  !defined(__DARWI
3930: 4e 5f 5f 29 20 26 26 20 21 64 65 66 69 6e 65 64  N__) && !defined
3940: 28 5f 5f 41 50 50 4c 45 5f 5f 29 20 26 26 20 53  (__APPLE__) && S
3950: 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
3960: 0a 23 20 20 64 65 66 69 6e 65 20 5f 58 4f 50 45  .#  define _XOPE
3970: 4e 5f 53 4f 55 52 43 45 20 35 30 30 20 20 2f 2a  N_SOURCE 500  /*
3980: 20 4e 65 65 64 65 64 20 74 6f 20 65 6e 61 62 6c   Needed to enabl
3990: 65 20 70 74 68 72 65 61 64 20 72 65 63 75 72 73  e pthread recurs
39a0: 69 76 65 20 6d 75 74 65 78 65 73 20 2a 2f 0a 23  ive mutexes */.#
39b0: 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
39c0: 20 54 43 4c 20 68 65 61 64 65 72 73 20 61 72 65   TCL headers are
39d0: 20 6f 6e 6c 79 20 6e 65 65 64 65 64 20 77 68 65   only needed whe
39e0: 6e 20 63 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20  n compiling the 
39f0: 54 43 4c 20 62 69 6e 64 69 6e 67 73 2e 0a 2a 2f  TCL bindings..*/
3a00: 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
3a10: 49 54 45 5f 54 43 4c 29 20 7c 7c 20 64 65 66 69  ITE_TCL) || defi
3a20: 6e 65 64 28 54 43 4c 53 48 29 0a 23 20 69 6e 63  ned(TCLSH).# inc
3a30: 6c 75 64 65 20 3c 74 63 6c 2e 68 3e 0a 23 65 6e  lude <tcl.h>.#en
3a40: 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6e 79 20  dif../*.** Many 
3a50: 70 65 6f 70 6c 65 20 61 72 65 20 66 61 69 6c 69  people are faili
3a60: 6e 67 20 74 6f 20 73 65 74 20 2d 44 4e 44 45 42  ng to set -DNDEB
3a70: 55 47 3d 31 20 77 68 65 6e 20 63 6f 6d 70 69 6c  UG=1 when compil
3a80: 69 6e 67 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 53  ing SQLite..** S
3a90: 65 74 74 69 6e 67 20 4e 44 45 42 55 47 20 6d 61  etting NDEBUG ma
3aa0: 6b 65 73 20 74 68 65 20 63 6f 64 65 20 73 6d 61  kes the code sma
3ab0: 6c 6c 65 72 20 61 6e 64 20 72 75 6e 20 66 61 73  ller and run fas
3ac0: 74 65 72 2e 20 20 53 6f 20 74 68 65 20 66 6f 6c  ter.  So the fol
3ad0: 6c 6f 77 69 6e 67 0a 2a 2a 20 6c 69 6e 65 73 20  lowing.** lines 
3ae0: 61 72 65 20 61 64 64 65 64 20 74 6f 20 61 75 74  are added to aut
3af0: 6f 6d 61 74 69 63 61 6c 6c 79 20 73 65 74 20 4e  omatically set N
3b00: 44 45 42 55 47 20 75 6e 6c 65 73 73 20 74 68 65  DEBUG unless the
3b10: 20 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 3d   -DSQLITE_DEBUG=
3b20: 31 0a 2a 2a 20 6f 70 74 69 6f 6e 20 69 73 20 73  1.** option is s
3b30: 65 74 2e 20 20 54 68 75 73 20 4e 44 45 42 55 47  et.  Thus NDEBUG
3b40: 20 62 65 63 6f 6d 65 73 20 61 6e 20 6f 70 74 2d   becomes an opt-
3b50: 69 6e 20 72 61 74 68 65 72 20 74 68 61 6e 20 61  in rather than a
3b60: 6e 20 6f 70 74 2d 6f 75 74 0a 2a 2a 20 66 65 61  n opt-out.** fea
3b70: 74 75 72 65 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  ture..*/.#if !de
3b80: 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26  fined(NDEBUG) &&
3b90: 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
3ba0: 5f 44 45 42 55 47 29 20 0a 23 20 64 65 66 69 6e  _DEBUG) .# defin
3bb0: 65 20 4e 44 45 42 55 47 20 31 0a 23 65 6e 64 69  e NDEBUG 1.#endi
3bc0: 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
3bd0: 2a 2a 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74  ** Include sqlit
3be0: 65 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64  e3.h in the midd
3bf0: 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e  le of sqliteInt.
3c00: 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
3c10: 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
3c20: 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71  ** Begin file sq
3c30: 6c 69 74 65 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  lite3.h ********
3c40: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c50: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3c60: 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65  */./*.** 2001 Se
3c70: 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a  ptember 15.**.**
3c80: 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
3c90: 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
3ca0: 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
3cb0: 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
3cc0: 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
3cd0: 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
3ce0: 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
3cf0: 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
3d00: 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
3d10: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
3d20: 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
3d30: 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
3d40: 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
3d50: 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
3d60: 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
3d70: 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
3d80: 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
3d90: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3da0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3db0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dc0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3dd0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
3de0: 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
3df0: 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
3e00: 61 63 65 20 74 68 61 74 20 74 68 65 20 53 51 4c  ace that the SQL
3e10: 69 74 65 20 6c 69 62 72 61 72 79 0a 2a 2a 20 70  ite library.** p
3e20: 72 65 73 65 6e 74 73 20 74 6f 20 63 6c 69 65 6e  resents to clien
3e30: 74 20 70 72 6f 67 72 61 6d 73 2e 20 20 49 66 20  t programs.  If 
3e40: 61 20 43 2d 66 75 6e 63 74 69 6f 6e 2c 20 73 74  a C-function, st
3e50: 72 75 63 74 75 72 65 2c 20 64 61 74 61 74 79 70  ructure, datatyp
3e60: 65 2c 0a 2a 2a 20 6f 72 20 63 6f 6e 73 74 61 6e  e,.** or constan
3e70: 74 20 64 65 66 69 6e 69 74 69 6f 6e 20 64 6f 65  t definition doe
3e80: 73 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20  s not appear in 
3e90: 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65 6e 20  this file, then 
3ea0: 69 74 20 69 73 0a 2a 2a 20 6e 6f 74 20 61 20 70  it is.** not a p
3eb0: 75 62 6c 69 73 68 65 64 20 41 50 49 20 6f 66 20  ublished API of 
3ec0: 53 51 4c 69 74 65 2c 20 69 73 20 73 75 62 6a 65  SQLite, is subje
3ed0: 63 74 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74  ct to change wit
3ee0: 68 6f 75 74 0a 2a 2a 20 6e 6f 74 69 63 65 2c 20  hout.** notice, 
3ef0: 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62  and should not b
3f00: 65 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20  e referenced by 
3f10: 70 72 6f 67 72 61 6d 73 20 74 68 61 74 20 75 73  programs that us
3f20: 65 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20  e SQLite..**.** 
3f30: 53 6f 6d 65 20 6f 66 20 74 68 65 20 64 65 66 69  Some of the defi
3f40: 6e 69 74 69 6f 6e 73 20 74 68 61 74 20 61 72 65  nitions that are
3f50: 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 61 72   in this file ar
3f60: 65 20 6d 61 72 6b 65 64 20 61 73 0a 2a 2a 20 22  e marked as.** "
3f70: 65 78 70 65 72 69 6d 65 6e 74 61 6c 22 2e 20 20  experimental".  
3f80: 45 78 70 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74  Experimental int
3f90: 65 72 66 61 63 65 73 20 61 72 65 20 6e 6f 72 6d  erfaces are norm
3fa0: 61 6c 6c 79 20 6e 65 77 0a 2a 2a 20 66 65 61 74  ally new.** feat
3fb0: 75 72 65 73 20 72 65 63 65 6e 74 6c 79 20 61 64  ures recently ad
3fc0: 64 65 64 20 74 6f 20 53 51 4c 69 74 65 2e 20 20  ded to SQLite.  
3fd0: 57 65 20 64 6f 20 6e 6f 74 20 61 6e 74 69 63 69  We do not antici
3fe0: 70 61 74 65 20 63 68 61 6e 67 65 73 0a 2a 2a 20  pate changes.** 
3ff0: 74 6f 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20  to experimental 
4000: 69 6e 74 65 72 66 61 63 65 73 20 62 75 74 20 72  interfaces but r
4010: 65 73 65 72 76 65 20 74 6f 20 6d 61 6b 65 20 6d  eserve to make m
4020: 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 69 66 0a  inor changes if.
4030: 2a 2a 20 65 78 70 65 72 69 65 6e 63 65 20 66 72  ** experience fr
4040: 6f 6d 20 75 73 65 20 22 69 6e 20 74 68 65 20 77  om use "in the w
4050: 69 6c 64 22 20 73 75 67 67 65 73 74 20 73 75 63  ild" suggest suc
4060: 68 20 63 68 61 6e 67 65 73 20 61 72 65 20 70 72  h changes are pr
4070: 75 64 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  udent..**.** The
4080: 20 6f 66 66 69 63 69 61 6c 20 43 2d 6c 61 6e 67   official C-lang
4090: 75 61 67 65 20 41 50 49 20 64 6f 63 75 6d 65 6e  uage API documen
40a0: 74 61 74 69 6f 6e 20 66 6f 72 20 53 51 4c 69 74  tation for SQLit
40b0: 65 20 69 73 20 64 65 72 69 76 65 64 0a 2a 2a 20  e is derived.** 
40c0: 66 72 6f 6d 20 63 6f 6d 6d 65 6e 74 73 20 69 6e  from comments in
40d0: 20 74 68 69 73 20 66 69 6c 65 2e 20 20 54 68 69   this file.  Thi
40e0: 73 20 66 69 6c 65 20 69 73 20 74 68 65 20 61 75  s file is the au
40f0: 74 68 6f 72 69 74 61 74 69 76 65 20 73 6f 75 72  thoritative sour
4100: 63 65 0a 2a 2a 20 6f 6e 20 68 6f 77 20 53 51 4c  ce.** on how SQL
4110: 69 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 61  ite interfaces a
4120: 72 65 20 73 75 70 70 6f 73 65 20 74 6f 20 6f 70  re suppose to op
4130: 65 72 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  erate..**.** The
4140: 20 6e 61 6d 65 20 6f 66 20 74 68 69 73 20 66 69   name of this fi
4150: 6c 65 20 75 6e 64 65 72 20 63 6f 6e 66 69 67 75  le under configu
4160: 72 61 74 69 6f 6e 20 6d 61 6e 61 67 65 6d 65 6e  ration managemen
4170: 74 20 69 73 20 22 73 71 6c 69 74 65 2e 68 2e 69  t is "sqlite.h.i
4180: 6e 22 2e 0a 2a 2a 20 54 68 65 20 6d 61 6b 65 66  n"..** The makef
4190: 69 6c 65 20 6d 61 6b 65 73 20 73 6f 6d 65 20 6d  ile makes some m
41a0: 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f 20  inor changes to 
41b0: 74 68 69 73 20 66 69 6c 65 20 28 73 75 63 68 20  this file (such 
41c0: 61 73 20 69 6e 73 65 72 74 69 6e 67 0a 2a 2a 20  as inserting.** 
41d0: 74 68 65 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62  the version numb
41e0: 65 72 29 20 61 6e 64 20 63 68 61 6e 67 65 73 20  er) and changes 
41f0: 69 74 73 20 6e 61 6d 65 20 74 6f 20 22 73 71 6c  its name to "sql
4200: 69 74 65 33 2e 68 22 20 61 73 0a 2a 2a 20 70 61  ite3.h" as.** pa
4210: 72 74 20 6f 66 20 74 68 65 20 62 75 69 6c 64 20  rt of the build 
4220: 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 40  process..**.** @
4230: 28 23 29 20 24 49 64 3a 20 73 71 6c 69 74 65 2e  (#) $Id: sqlite.
4240: 68 2e 69 6e 2c 76 20 31 2e 34 31 35 20 32 30 30  h.in,v 1.415 200
4250: 38 2f 31 31 2f 31 39 20 30 31 3a 32 30 3a 32 36  8/11/19 01:20:26
4260: 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 23 69   drh Exp $.*/.#i
4270: 66 6e 64 65 66 20 5f 53 51 4c 49 54 45 33 5f 48  fndef _SQLITE3_H
4280: 5f 0a 23 64 65 66 69 6e 65 20 5f 53 51 4c 49 54  _.#define _SQLIT
4290: 45 33 5f 48 5f 0a 23 69 6e 63 6c 75 64 65 20 3c  E3_H_.#include <
42a0: 73 74 64 61 72 67 2e 68 3e 20 20 20 20 20 2f 2a  stdarg.h>     /*
42b0: 20 4e 65 65 64 65 64 20 66 6f 72 20 74 68 65 20   Needed for the 
42c0: 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 76 61  definition of va
42d0: 5f 6c 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  _list */../*.** 
42e0: 4d 61 6b 65 20 73 75 72 65 20 77 65 20 63 61 6e  Make sure we can
42f0: 20 63 61 6c 6c 20 74 68 69 73 20 73 74 75 66 66   call this stuff
4300: 20 66 72 6f 6d 20 43 2b 2b 2e 0a 2a 2f 0a 23 69   from C++..*/.#i
4310: 66 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b  f 0.extern "C" {
4320: 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20  .#endif.../*.** 
4330: 41 64 64 20 74 68 65 20 61 62 69 6c 69 74 79 20  Add the ability 
4340: 74 6f 20 6f 76 65 72 72 69 64 65 20 27 65 78 74  to override 'ext
4350: 65 72 6e 27 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ern'.*/.#ifndef 
4360: 53 51 4c 49 54 45 5f 45 58 54 45 52 4e 0a 23 20  SQLITE_EXTERN.# 
4370: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58  define SQLITE_EX
4380: 54 45 52 4e 20 65 78 74 65 72 6e 0a 23 65 6e 64  TERN extern.#end
4390: 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20  if../*.** These 
43a0: 6e 6f 2d 6f 70 20 6d 61 63 72 6f 73 20 61 72 65  no-op macros are
43b0: 20 75 73 65 64 20 69 6e 20 66 72 6f 6e 74 20 6f   used in front o
43c0: 66 20 69 6e 74 65 72 66 61 63 65 73 20 74 6f 20  f interfaces to 
43d0: 6d 61 72 6b 20 74 68 6f 73 65 0a 2a 2a 20 69 6e  mark those.** in
43e0: 74 65 72 66 61 63 65 73 20 61 73 20 65 69 74 68  terfaces as eith
43f0: 65 72 20 64 65 70 72 65 63 61 74 65 64 20 6f 72  er deprecated or
4400: 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20 20   experimental.  
4410: 4e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73  New applications
4420: 0a 2a 2a 20 73 68 6f 75 6c 64 20 6e 6f 74 20 75  .** should not u
4430: 73 65 20 64 65 70 72 65 63 61 74 65 64 20 69 6e  se deprecated in
4440: 74 72 66 61 63 65 73 20 2d 20 74 68 65 79 20 61  trfaces - they a
4450: 72 65 20 73 75 70 70 6f 72 74 20 66 6f 72 20 62  re support for b
4460: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70  ackwards.** comp
4470: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 20  atibility only. 
4480: 20 41 70 70 6c 69 63 61 74 69 6f 6e 20 77 72 69   Application wri
4490: 74 65 72 73 20 73 68 6f 75 6c 64 20 62 65 20 61  ters should be a
44a0: 77 61 72 65 20 74 68 61 74 0a 2a 2a 20 65 78 70  ware that.** exp
44b0: 65 72 69 6d 65 6e 74 61 6c 20 69 6e 74 65 72 66  erimental interf
44c0: 61 63 65 73 20 61 72 65 20 73 75 62 6a 65 63 74  aces are subject
44d0: 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 70 6f   to change in po
44e0: 69 6e 74 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a  int releases..**
44f0: 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f 73  .** These macros
4500: 20 75 73 65 64 20 74 6f 20 72 65 73 6f 6c 76 65   used to resolve
4510: 20 74 6f 20 76 61 72 69 6f 75 73 20 6b 69 6e 64   to various kind
4520: 73 20 6f 66 20 63 6f 6d 70 69 6c 65 72 20 6d 61  s of compiler ma
4530: 67 69 63 20 74 68 61 74 0a 2a 2a 20 77 6f 75 6c  gic that.** woul
4540: 64 20 67 65 6e 65 72 61 74 65 20 77 61 72 6e 69  d generate warni
4550: 6e 67 20 6d 65 73 73 61 67 65 73 20 77 68 65 6e  ng messages when
4560: 20 74 68 65 79 20 77 65 72 65 20 75 73 65 64 2e   they were used.
4570: 20 20 42 75 74 20 74 68 61 74 0a 2a 2a 20 63 6f    But that.** co
4580: 6d 70 69 6c 65 72 20 6d 61 67 69 63 20 65 6e 64  mpiler magic end
4590: 65 64 20 75 70 20 67 65 6e 65 72 61 74 69 6e 67  ed up generating
45a0: 20 73 75 63 68 20 61 20 66 6c 75 72 72 79 20 6f   such a flurry o
45b0: 66 20 62 75 67 20 72 65 70 6f 72 74 73 0a 2a 2a  f bug reports.**
45c0: 20 74 68 61 74 20 77 65 20 68 61 76 65 20 74 61   that we have ta
45d0: 6b 65 6e 20 69 74 20 61 6c 6c 20 6f 75 74 20 61  ken it all out a
45e0: 6e 64 20 67 6f 6e 65 20 62 61 63 6b 20 74 6f 20  nd gone back to 
45f0: 75 73 69 6e 67 20 73 69 6d 70 6c 65 0a 2a 2a 20  using simple.** 
4600: 6e 6f 6f 70 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  noop macros..*/.
4610: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
4620: 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
4630: 65 20 53 51 4c 49 54 45 5f 45 58 50 45 52 49 4d  e SQLITE_EXPERIM
4640: 45 4e 54 41 4c 0a 0a 2f 2a 0a 2a 2a 20 45 6e 73  ENTAL../*.** Ens
4650: 75 72 65 20 74 68 65 73 65 20 73 79 6d 62 6f 6c  ure these symbol
4660: 73 20 77 65 72 65 20 6e 6f 74 20 64 65 66 69 6e  s were not defin
4670: 65 64 20 62 79 20 73 6f 6d 65 20 70 72 65 76 69  ed by some previ
4680: 6f 75 73 20 68 65 61 64 65 72 20 66 69 6c 65 2e  ous header file.
4690: 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
46a0: 45 5f 56 45 52 53 49 4f 4e 0a 23 20 75 6e 64 65  E_VERSION.# unde
46b0: 66 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e  f SQLITE_VERSION
46c0: 0a 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53  .#endif.#ifdef S
46d0: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
46e0: 4d 42 45 52 0a 23 20 75 6e 64 65 66 20 53 51 4c  MBER.# undef SQL
46f0: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
4700: 45 52 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  ER.#endif../*.**
4710: 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6d 70 69   CAPI3REF: Compi
4720: 6c 65 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20  le-Time Library 
4730: 56 65 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20  Version Numbers 
4740: 7b 48 31 30 30 31 30 7d 20 3c 53 36 30 31 30 30  {H10010} <S60100
4750: 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49  >.**.** The SQLI
4760: 54 45 5f 56 45 52 53 49 4f 4e 20 61 6e 64 20 53  TE_VERSION and S
4770: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
4780: 4d 42 45 52 20 23 64 65 66 69 6e 65 73 20 69 6e  MBER #defines in
4790: 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 2e  .** the sqlite3.
47a0: 68 20 66 69 6c 65 20 73 70 65 63 69 66 79 20 74  h file specify t
47b0: 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 53 51  he version of SQ
47c0: 4c 69 74 65 20 77 69 74 68 20 77 68 69 63 68 0a  Lite with which.
47d0: 2a 2a 20 74 68 61 74 20 68 65 61 64 65 72 20 66  ** that header f
47e0: 69 6c 65 20 69 73 20 61 73 73 6f 63 69 61 74 65  ile is associate
47f0: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 76 65  d..**.** The "ve
4800: 72 73 69 6f 6e 22 20 6f 66 20 53 51 4c 69 74 65  rsion" of SQLite
4810: 20 69 73 20 61 20 73 74 72 69 6e 67 20 6f 66 20   is a string of 
4820: 74 68 65 20 66 6f 72 6d 20 22 58 2e 59 2e 5a 22  the form "X.Y.Z"
4830: 2e 0a 2a 2a 20 54 68 65 20 70 68 72 61 73 65 20  ..** The phrase 
4840: 22 61 6c 70 68 61 22 20 6f 72 20 22 62 65 74 61  "alpha" or "beta
4850: 22 20 6d 69 67 68 74 20 62 65 20 61 70 70 65 6e  " might be appen
4860: 64 65 64 20 61 66 74 65 72 20 74 68 65 20 5a 2e  ded after the Z.
4870: 0a 2a 2a 20 54 68 65 20 58 20 76 61 6c 75 65 20  .** The X value 
4880: 69 73 20 6d 61 6a 6f 72 20 76 65 72 73 69 6f 6e  is major version
4890: 20 6e 75 6d 62 65 72 20 61 6c 77 61 79 73 20 33   number always 3
48a0: 20 69 6e 20 53 51 4c 69 74 65 33 2e 0a 2a 2a 20   in SQLite3..** 
48b0: 54 68 65 20 58 20 76 61 6c 75 65 20 6f 6e 6c 79  The X value only
48c0: 20 63 68 61 6e 67 65 73 20 77 68 65 6e 20 62 61   changes when ba
48d0: 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
48e0: 69 6c 69 74 79 20 69 73 0a 2a 2a 20 62 72 6f 6b  ility is.** brok
48f0: 65 6e 20 61 6e 64 20 77 65 20 69 6e 74 65 6e 64  en and we intend
4900: 20 74 6f 20 6e 65 76 65 72 20 62 72 65 61 6b 20   to never break 
4910: 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
4920: 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 54 68 65 20  ibility..** The 
4930: 59 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 6d  Y value is the m
4940: 69 6e 6f 72 20 76 65 72 73 69 6f 6e 20 6e 75 6d  inor version num
4950: 62 65 72 20 61 6e 64 20 6f 6e 6c 79 20 63 68 61  ber and only cha
4960: 6e 67 65 73 20 77 68 65 6e 0a 2a 2a 20 74 68 65  nges when.** the
4970: 72 65 20 61 72 65 20 6d 61 6a 6f 72 20 66 65 61  re are major fea
4980: 74 75 72 65 20 65 6e 68 61 6e 63 65 6d 65 6e 74  ture enhancement
4990: 73 20 74 68 61 74 20 61 72 65 20 66 6f 72 77 61  s that are forwa
49a0: 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 0a 2a  rds compatible.*
49b0: 2a 20 62 75 74 20 6e 6f 74 20 62 61 63 6b 77 61  * but not backwa
49c0: 72 64 73 20 63 6f 6d 70 61 74 69 62 6c 65 2e 0a  rds compatible..
49d0: 2a 2a 20 54 68 65 20 5a 20 76 61 6c 75 65 20 69  ** The Z value i
49e0: 73 20 74 68 65 20 72 65 6c 65 61 73 65 20 6e 75  s the release nu
49f0: 6d 62 65 72 20 61 6e 64 20 69 73 20 69 6e 63 72  mber and is incr
4a00: 65 6d 65 6e 74 65 64 20 77 69 74 68 0a 2a 2a 20  emented with.** 
4a10: 65 61 63 68 20 72 65 6c 65 61 73 65 20 62 75 74  each release but
4a20: 20 72 65 73 65 74 73 20 62 61 63 6b 20 74 6f 20   resets back to 
4a30: 30 20 77 68 65 6e 65 76 65 72 20 59 20 69 73 20  0 whenever Y is 
4a40: 69 6e 63 72 65 6d 65 6e 74 65 64 2e 0a 2a 2a 0a  incremented..**.
4a50: 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71  ** See also: [sq
4a60: 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e  lite3_libversion
4a70: 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  ()] and [sqlite3
4a80: 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62  _libversion_numb
4a90: 65 72 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  er()]..**.** INV
4aa0: 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
4ab0: 48 31 30 30 31 31 7d 20 54 68 65 20 53 51 4c 49  H10011} The SQLI
4ac0: 54 45 5f 56 45 52 53 49 4f 4e 20 23 64 65 66 69  TE_VERSION #defi
4ad0: 6e 65 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65  ne in the sqlite
4ae0: 33 2e 68 20 68 65 61 64 65 72 20 66 69 6c 65 20  3.h header file 
4af0: 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20  shall.**        
4b00: 20 20 65 76 61 6c 75 61 74 65 20 74 6f 20 61 20    evaluate to a 
4b10: 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74  string literal t
4b20: 68 61 74 20 69 73 20 74 68 65 20 53 51 4c 69 74  hat is the SQLit
4b30: 65 20 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20  e version.**    
4b40: 20 20 20 20 20 20 77 69 74 68 20 77 68 69 63 68        with which
4b50: 20 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65   the header file
4b60: 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 2e 0a   is associated..
4b70: 2a 2a 0a 2a 2a 20 7b 48 31 30 30 31 34 7d 20 54  **.** {H10014} T
4b80: 68 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49 4f  he SQLITE_VERSIO
4b90: 4e 5f 4e 55 4d 42 45 52 20 23 64 65 66 69 6e 65  N_NUMBER #define
4ba0: 20 73 68 61 6c 6c 20 72 65 73 6f 6c 76 65 20 74   shall resolve t
4bb0: 6f 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  o an integer.** 
4bc0: 20 20 20 20 20 20 20 20 20 77 69 74 68 20 74 68           with th
4bd0: 65 20 76 61 6c 75 65 20 28 58 2a 31 30 30 30 30  e value (X*10000
4be0: 30 30 20 2b 20 59 2a 31 30 30 30 20 2b 20 5a 29  00 + Y*1000 + Z)
4bf0: 20 77 68 65 72 65 20 58 2c 20 59 2c 20 61 6e 64   where X, Y, and
4c00: 20 5a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61   Z.**          a
4c10: 72 65 20 74 68 65 20 6d 61 6a 6f 72 20 76 65 72  re the major ver
4c20: 73 69 6f 6e 2c 20 6d 69 6e 6f 72 20 76 65 72 73  sion, minor vers
4c30: 69 6f 6e 2c 20 61 6e 64 20 72 65 6c 65 61 73 65  ion, and release
4c40: 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 23 64 65 66   number..*/.#def
4c50: 69 6e 65 20 53 51 4c 49 54 45 5f 56 45 52 53 49  ine SQLITE_VERSI
4c60: 4f 4e 20 20 20 20 20 20 20 20 20 22 33 2e 36 2e  ON         "3.6.
4c70: 36 2e 31 22 0a 23 64 65 66 69 6e 65 20 53 51 4c  6.1".#define SQL
4c80: 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42  ITE_VERSION_NUMB
4c90: 45 52 20 20 33 30 30 36 30 30 36 0a 0a 2f 2a 0a  ER  3006006../*.
4ca0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e  ** CAPI3REF: Run
4cb0: 2d 54 69 6d 65 20 4c 69 62 72 61 72 79 20 56 65  -Time Library Ve
4cc0: 72 73 69 6f 6e 20 4e 75 6d 62 65 72 73 20 7b 48  rsion Numbers {H
4cd0: 31 30 30 32 30 7d 20 3c 53 36 30 31 30 30 3e 0a  10020} <S60100>.
4ce0: 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c  ** KEYWORDS: sql
4cf0: 69 74 65 33 5f 76 65 72 73 69 6f 6e 0a 2a 2a 0a  ite3_version.**.
4d00: 2a 2a 20 54 68 65 73 65 20 66 65 61 74 75 72 65  ** These feature
4d10: 73 20 70 72 6f 76 69 64 65 20 74 68 65 20 73 61  s provide the sa
4d20: 6d 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  me information a
4d30: 73 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 56 45  s the [SQLITE_VE
4d40: 52 53 49 4f 4e 5d 0a 2a 2a 20 61 6e 64 20 5b 53  RSION].** and [S
4d50: 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e 55  QLITE_VERSION_NU
4d60: 4d 42 45 52 5d 20 23 64 65 66 69 6e 65 73 20 69  MBER] #defines i
4d70: 6e 20 74 68 65 20 68 65 61 64 65 72 2c 20 62 75  n the header, bu
4d80: 74 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64  t are associated
4d90: 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 6c 69 62  .** with the lib
4da0: 72 61 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20  rary instead of 
4db0: 74 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 2e  the header file.
4dc0: 20 20 43 61 75 74 69 6f 75 73 20 70 72 6f 67 72    Cautious progr
4dd0: 61 6d 6d 65 72 73 20 6d 69 67 68 74 0a 2a 2a 20  ammers might.** 
4de0: 69 6e 63 6c 75 64 65 20 61 20 63 68 65 63 6b 20  include a check 
4df0: 69 6e 20 74 68 65 69 72 20 61 70 70 6c 69 63 61  in their applica
4e00: 74 69 6f 6e 20 74 6f 20 76 65 72 69 66 79 20 74  tion to verify t
4e10: 68 61 74 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c  hat.** sqlite3_l
4e20: 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
4e30: 28 29 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  () always return
4e40: 73 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 5b  s the value.** [
4e50: 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f 4e  SQLITE_VERSION_N
4e60: 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68  UMBER]..**.** Th
4e70: 65 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  e sqlite3_libver
4e80: 73 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20  sion() function 
4e90: 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d 65  returns the same
4ea0: 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 73 20   information as 
4eb0: 69 73 0a 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c  is.** in the sql
4ec0: 69 74 65 33 5f 76 65 72 73 69 6f 6e 5b 5d 20 73  ite3_version[] s
4ed0: 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e 20  tring constant. 
4ee0: 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73   The function is
4ef0: 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20 66 6f 72   provided.** for
4f00: 20 75 73 65 20 69 6e 20 44 4c 4c 73 20 73 69 6e   use in DLLs sin
4f10: 63 65 20 44 4c 4c 20 75 73 65 72 73 20 75 73 75  ce DLL users usu
4f20: 61 6c 6c 79 20 64 6f 20 6e 6f 74 20 68 61 76 65  ally do not have
4f30: 20 64 69 72 65 63 74 20 61 63 63 65 73 73 20 74   direct access t
4f40: 6f 20 73 74 72 69 6e 67 0a 2a 2a 20 63 6f 6e 73  o string.** cons
4f50: 74 61 6e 74 73 20 77 69 74 68 69 6e 20 74 68 65  tants within the
4f60: 20 44 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41   DLL..**.** INVA
4f70: 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
4f80: 31 30 30 32 31 7d 20 54 68 65 20 5b 73 71 6c 69  10021} The [sqli
4f90: 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e  te3_libversion_n
4fa0: 75 6d 62 65 72 28 29 5d 20 69 6e 74 65 72 66 61  umber()] interfa
4fb0: 63 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a  ce shall return.
4fc0: 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 20 69  **          an i
4fd0: 6e 74 65 67 65 72 20 65 71 75 61 6c 20 74 6f 20  nteger equal to 
4fe0: 5b 53 51 4c 49 54 45 5f 56 45 52 53 49 4f 4e 5f  [SQLITE_VERSION_
4ff0: 4e 55 4d 42 45 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  NUMBER]..**.** {
5000: 48 31 30 30 32 32 7d 20 54 68 65 20 5b 73 71 6c  H10022} The [sql
5010: 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d 20 73 74  ite3_version] st
5020: 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 20 73 68  ring constant sh
5030: 61 6c 6c 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 20  all contain.**  
5040: 20 20 20 20 20 20 20 20 74 68 65 20 74 65 78 74          the text
5050: 20 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f   of the [SQLITE_
5060: 56 45 52 53 49 4f 4e 5d 20 73 74 72 69 6e 67 2e  VERSION] string.
5070: 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 30 32 33 7d 20  .**.** {H10023} 
5080: 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 69 62  The [sqlite3_lib
5090: 76 65 72 73 69 6f 6e 28 29 5d 20 66 75 6e 63 74  version()] funct
50a0: 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72 6e  ion shall return
50b0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 70  .**          a p
50c0: 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 5b 73  ointer to the [s
50d0: 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 5d 20  qlite3_version] 
50e0: 73 74 72 69 6e 67 20 63 6f 6e 73 74 61 6e 74 2e  string constant.
50f0: 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
5100: 6f 6e 73 74 20 63 68 61 72 20 73 71 6c 69 74 65  onst char sqlite
5110: 33 5f 76 65 72 73 69 6f 6e 5b 5d 3b 0a 53 51 4c  3_version[];.SQL
5120: 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
5130: 61 72 20 2a 73 71 6c 69 74 65 33 5f 6c 69 62 76  ar *sqlite3_libv
5140: 65 72 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 53 51  ersion(void);.SQ
5150: 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
5160: 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f  ite3_libversion_
5170: 6e 75 6d 62 65 72 28 76 6f 69 64 29 3b 0a 0a 2f  number(void);../
5180: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54  *.** CAPI3REF: T
5190: 65 73 74 20 54 6f 20 53 65 65 20 49 66 20 54 68  est To See If Th
51a0: 65 20 4c 69 62 72 61 72 79 20 49 73 20 54 68 72  e Library Is Thr
51b0: 65 61 64 73 61 66 65 20 7b 48 31 30 31 30 30 7d  eadsafe {H10100}
51c0: 20 3c 53 36 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S60100>.**.** 
51d0: 53 51 4c 69 74 65 20 63 61 6e 20 62 65 20 63 6f  SQLite can be co
51e0: 6d 70 69 6c 65 64 20 77 69 74 68 20 6f 72 20 77  mpiled with or w
51f0: 69 74 68 6f 75 74 20 6d 75 74 65 78 65 73 2e 20  ithout mutexes. 
5200: 20 57 68 65 6e 0a 2a 2a 20 74 68 65 20 5b 53 51   When.** the [SQ
5210: 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d  LITE_THREADSAFE]
5220: 20 43 20 70 72 65 70 72 6f 63 65 73 73 6f 72 20   C preprocessor 
5230: 6d 61 63 72 6f 20 31 20 6f 72 20 32 2c 20 6d 75  macro 1 or 2, mu
5240: 74 65 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61  texes.** are ena
5250: 62 6c 65 64 20 61 6e 64 20 53 51 4c 69 74 65 20  bled and SQLite 
5260: 69 73 20 74 68 72 65 61 64 73 61 66 65 2e 20 20  is threadsafe.  
5270: 57 68 65 6e 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  When the.** [SQL
5280: 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 5d 20  ITE_THREADSAFE] 
5290: 6d 61 63 72 6f 20 69 73 20 30 2c 20 0a 2a 2a 20  macro is 0, .** 
52a0: 74 68 65 20 6d 75 74 65 78 65 73 20 61 72 65 20  the mutexes are 
52b0: 6f 6d 69 74 74 65 64 2e 20 20 57 69 74 68 6f 75  omitted.  Withou
52c0: 74 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 69  t the mutexes, i
52d0: 74 20 69 73 20 6e 6f 74 20 73 61 66 65 0a 2a 2a  t is not safe.**
52e0: 20 74 6f 20 75 73 65 20 53 51 4c 69 74 65 20 63   to use SQLite c
52f0: 6f 6e 63 75 72 72 65 6e 74 6c 79 20 66 72 6f 6d  oncurrently from
5300: 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 74   more than one t
5310: 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 45 6e 61  hread..**.** Ena
5320: 62 6c 69 6e 67 20 6d 75 74 65 78 65 73 20 69 6e  bling mutexes in
5330: 63 75 72 73 20 61 20 6d 65 61 73 75 72 61 62 6c  curs a measurabl
5340: 65 20 70 65 72 66 6f 72 6d 61 6e 63 65 20 70 65  e performance pe
5350: 6e 61 6c 74 79 2e 0a 2a 2a 20 53 6f 20 69 66 20  nalty..** So if 
5360: 73 70 65 65 64 20 69 73 20 6f 66 20 75 74 6d 6f  speed is of utmo
5370: 73 74 20 69 6d 70 6f 72 74 61 6e 63 65 2c 20 69  st importance, i
5380: 74 20 6d 61 6b 65 73 20 73 65 6e 73 65 20 74 6f  t makes sense to
5390: 20 64 69 73 61 62 6c 65 0a 2a 2a 20 74 68 65 20   disable.** the 
53a0: 6d 75 74 65 78 65 73 2e 20 20 42 75 74 20 66 6f  mutexes.  But fo
53b0: 72 20 6d 61 78 69 6d 75 6d 20 73 61 66 65 74 79  r maximum safety
53c0: 2c 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64  , mutexes should
53d0: 20 62 65 20 65 6e 61 62 6c 65 64 2e 0a 2a 2a 20   be enabled..** 
53e0: 54 68 65 20 64 65 66 61 75 6c 74 20 62 65 68 61  The default beha
53f0: 76 69 6f 72 20 69 73 20 66 6f 72 20 6d 75 74 65  vior is for mute
5400: 78 65 73 20 74 6f 20 62 65 20 65 6e 61 62 6c 65  xes to be enable
5410: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  d..**.** This in
5420: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
5430: 73 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d  sed by a program
5440: 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
5450: 61 74 20 74 68 65 0a 2a 2a 20 76 65 72 73 69 6f  at the.** versio
5460: 6e 20 6f 66 20 53 51 4c 69 74 65 20 74 68 61 74  n of SQLite that
5470: 20 69 74 20 69 73 20 6c 69 6e 6b 69 6e 67 20 61   it is linking a
5480: 67 61 69 6e 73 74 20 77 61 73 20 63 6f 6d 70 69  gainst was compi
5490: 6c 65 64 20 77 69 74 68 0a 2a 2a 20 74 68 65 20  led with.** the 
54a0: 64 65 73 69 72 65 64 20 73 65 74 74 69 6e 67 20  desired setting 
54b0: 6f 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f 54  of the [SQLITE_T
54c0: 48 52 45 41 44 53 41 46 45 5d 20 6d 61 63 72 6f  HREADSAFE] macro
54d0: 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74  ..**.** This int
54e0: 65 72 66 61 63 65 20 6f 6e 6c 79 20 72 65 70 6f  erface only repo
54f0: 72 74 73 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69  rts on the compi
5500: 6c 65 2d 74 69 6d 65 20 6d 75 74 65 78 20 73 65  le-time mutex se
5510: 74 74 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20  tting.** of the 
5520: 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
5530: 46 45 5d 20 66 6c 61 67 2e 20 20 49 66 20 53 51  FE] flag.  If SQ
5540: 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c 65 64  Lite is compiled
5550: 20 77 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f   with.** SQLITE_
5560: 54 48 52 45 41 44 53 41 46 45 3d 31 20 74 68 65  THREADSAFE=1 the
5570: 6e 20 6d 75 74 65 78 65 73 20 61 72 65 20 65 6e  n mutexes are en
5580: 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
5590: 20 62 75 74 0a 2a 2a 20 63 61 6e 20 62 65 20 66   but.** can be f
55a0: 75 6c 6c 79 20 6f 72 20 70 61 72 74 69 61 6c 6c  ully or partiall
55b0: 79 20 64 69 73 61 62 6c 65 64 20 75 73 69 6e 67  y disabled using
55c0: 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69   a call to [sqli
55d0: 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 0a 2a 2a  te3_config()].**
55e0: 20 77 69 74 68 20 74 68 65 20 76 65 72 62 73 20   with the verbs 
55f0: 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53  [SQLITE_CONFIG_S
5600: 49 4e 47 4c 45 54 48 52 45 41 44 5d 2c 20 5b 53  INGLETHREAD], [S
5610: 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c  QLITE_CONFIG_MUL
5620: 54 49 54 48 52 45 41 44 5d 2c 0a 2a 2a 20 6f 72  TITHREAD],.** or
5630: 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f   [SQLITE_CONFIG_
5640: 4d 55 54 45 58 5d 2e 20 20 54 68 65 20 72 65 74  MUTEX].  The ret
5650: 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69  urn value of thi
5660: 73 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 77 73  s function shows
5670: 0a 2a 2a 20 6f 6e 6c 79 20 74 68 65 20 64 65 66  .** only the def
5680: 61 75 6c 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  ault compile-tim
5690: 65 20 73 65 74 74 69 6e 67 2c 20 6e 6f 74 20 61  e setting, not a
56a0: 6e 79 20 72 75 6e 2d 74 69 6d 65 20 63 68 61 6e  ny run-time chan
56b0: 67 65 73 0a 2a 2a 20 74 6f 20 74 68 61 74 20 73  ges.** to that s
56c0: 65 74 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 65  etting..**.** Se
56d0: 65 20 74 68 65 20 5b 74 68 72 65 61 64 69 6e 67  e the [threading
56e0: 20 6d 6f 64 65 5d 20 64 6f 63 75 6d 65 6e 74 61   mode] documenta
56f0: 74 69 6f 6e 20 66 6f 72 20 61 64 64 69 74 69 6f  tion for additio
5700: 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
5710: 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
5720: 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31 30 31  S:.**.** {H10101
5730: 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 74  } The [sqlite3_t
5740: 68 72 65 61 64 73 61 66 65 28 29 5d 20 66 75 6e  hreadsafe()] fun
5750: 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75  ction shall retu
5760: 72 6e 20 7a 65 72 6f 20 69 66 0a 2a 2a 20 20 20  rn zero if.**   
5770: 20 20 20 20 20 20 20 61 6e 64 20 6f 6e 6c 79 20         and only 
5780: 69 66 20 53 51 4c 69 74 65 20 77 61 73 20 63 6f  if SQLite was co
5790: 6d 70 69 6c 65 64 20 77 69 74 68 20 6d 75 74 65  mpiled with mute
57a0: 78 69 6e 67 20 63 6f 64 65 20 6f 6d 69 74 74 65  xing code omitte
57b0: 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 31 30 32  d..**.** {H10102
57c0: 7d 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75  } The value retu
57d0: 72 6e 65 64 20 62 79 20 74 68 65 20 5b 73 71 6c  rned by the [sql
57e0: 69 74 65 33 5f 74 68 72 65 61 64 73 61 66 65 28  ite3_threadsafe(
57f0: 29 5d 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20  )] function.**  
5800: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 72 65          shall re
5810: 6d 61 69 6e 20 74 68 65 20 73 61 6d 65 20 61 63  main the same ac
5820: 72 6f 73 73 20 63 61 6c 6c 73 20 74 6f 20 5b 73  ross calls to [s
5830: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
5840: 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
5850: 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65  int sqlite3_thre
5860: 61 64 73 61 66 65 28 76 6f 69 64 29 3b 0a 0a 2f  adsafe(void);../
5870: 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44  *.** CAPI3REF: D
5880: 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
5890: 6f 6e 20 48 61 6e 64 6c 65 20 7b 48 31 32 30 30  on Handle {H1200
58a0: 30 7d 20 3c 53 34 30 32 30 30 3e 0a 2a 2a 20 4b  0} <S40200>.** K
58b0: 45 59 57 4f 52 44 53 3a 20 7b 64 61 74 61 62 61  EYWORDS: {databa
58c0: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 7d 20 7b  se connection} {
58d0: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
58e0: 69 6f 6e 73 7d 0a 2a 2a 0a 2a 2a 20 45 61 63 68  ions}.**.** Each
58f0: 20 6f 70 65 6e 20 53 51 4c 69 74 65 20 64 61 74   open SQLite dat
5900: 61 62 61 73 65 20 69 73 20 72 65 70 72 65 73 65  abase is represe
5910: 6e 74 65 64 20 62 79 20 61 20 70 6f 69 6e 74 65  nted by a pointe
5920: 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e 63 65  r to an instance
5930: 20 6f 66 0a 2a 2a 20 74 68 65 20 6f 70 61 71 75   of.** the opaqu
5940: 65 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65  e structure name
5950: 64 20 22 73 71 6c 69 74 65 33 22 2e 20 20 49 74  d "sqlite3".  It
5960: 20 69 73 20 75 73 65 66 75 6c 20 74 6f 20 74 68   is useful to th
5970: 69 6e 6b 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ink of an sqlite
5980: 33 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 73 20  3.** pointer as 
5990: 61 6e 20 6f 62 6a 65 63 74 2e 20 20 54 68 65 20  an object.  The 
59a0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
59b0: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
59c0: 36 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73 71  6()], and.** [sq
59d0: 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d  lite3_open_v2()]
59e0: 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
59f0: 69 74 73 20 63 6f 6e 73 74 72 75 63 74 6f 72 73  its constructors
5a00: 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63  , and [sqlite3_c
5a10: 6c 6f 73 65 28 29 5d 0a 2a 2a 20 69 73 20 69 74  lose()].** is it
5a20: 73 20 64 65 73 74 72 75 63 74 6f 72 2e 20 20 54  s destructor.  T
5a30: 68 65 72 65 20 61 72 65 20 6d 61 6e 79 20 6f 74  here are many ot
5a40: 68 65 72 20 69 6e 74 65 72 66 61 63 65 73 20 28  her interfaces (
5a50: 73 75 63 68 20 61 73 0a 2a 2a 20 5b 73 71 6c 69  such as.** [sqli
5a60: 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
5a70: 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  ], [sqlite3_crea
5a80: 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 2c 20  te_function()], 
5a90: 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  and.** [sqlite3_
5aa0: 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20  busy_timeout()] 
5ab0: 74 6f 20 6e 61 6d 65 20 62 75 74 20 74 68 72 65  to name but thre
5ac0: 65 29 20 74 68 61 74 20 61 72 65 20 6d 65 74 68  e) that are meth
5ad0: 6f 64 73 20 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c  ods on an.** sql
5ae0: 69 74 65 33 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  ite3 object..*/.
5af0: 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
5b00: 71 6c 69 74 65 33 20 73 71 6c 69 74 65 33 3b 0a  qlite3 sqlite3;.
5b10: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
5b20: 20 36 34 2d 42 69 74 20 49 6e 74 65 67 65 72 20   64-Bit Integer 
5b30: 54 79 70 65 73 20 7b 48 31 30 32 30 30 7d 20 3c  Types {H10200} <
5b40: 53 31 30 31 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S10110>.** KEYWO
5b50: 52 44 53 3a 20 73 71 6c 69 74 65 5f 69 6e 74 36  RDS: sqlite_int6
5b60: 34 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 0a  4 sqlite_uint64.
5b70: 2a 2a 0a 2a 2a 20 42 65 63 61 75 73 65 20 74 68  **.** Because th
5b80: 65 72 65 20 69 73 20 6e 6f 20 63 72 6f 73 73 2d  ere is no cross-
5b90: 70 6c 61 74 66 6f 72 6d 20 77 61 79 20 74 6f 20  platform way to 
5ba0: 73 70 65 63 69 66 79 20 36 34 2d 62 69 74 20 69  specify 64-bit i
5bb0: 6e 74 65 67 65 72 20 74 79 70 65 73 0a 2a 2a 20  nteger types.** 
5bc0: 53 51 4c 69 74 65 20 69 6e 63 6c 75 64 65 73 20  SQLite includes 
5bd0: 74 79 70 65 64 65 66 73 20 66 6f 72 20 36 34 2d  typedefs for 64-
5be0: 62 69 74 20 73 69 67 6e 65 64 20 61 6e 64 20 75  bit signed and u
5bf0: 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73  nsigned integers
5c00: 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
5c10: 74 65 33 5f 69 6e 74 36 34 20 61 6e 64 20 73 71  te3_int64 and sq
5c20: 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 61 72 65  lite3_uint64 are
5c30: 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74   the preferred t
5c40: 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e 73 2e  ype definitions.
5c50: 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 5f 69  .** The sqlite_i
5c60: 6e 74 36 34 20 61 6e 64 20 73 71 6c 69 74 65 5f  nt64 and sqlite_
5c70: 75 69 6e 74 36 34 20 74 79 70 65 73 20 61 72 65  uint64 types are
5c80: 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20 62   supported for b
5c90: 61 63 6b 77 61 72 64 73 0a 2a 2a 20 63 6f 6d 70  ackwards.** comp
5ca0: 61 74 69 62 69 6c 69 74 79 20 6f 6e 6c 79 2e 0a  atibility only..
5cb0: 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
5cc0: 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 31 7d  :.**.** {H10201}
5cd0: 20 54 68 65 20 5b 73 71 6c 69 74 65 5f 69 6e 74   The [sqlite_int
5ce0: 36 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33  64] and [sqlite3
5cf0: 5f 69 6e 74 36 34 5d 20 74 79 70 65 20 73 68 61  _int64] type sha
5d00: 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20 20 20  ll specify.**   
5d10: 20 20 20 20 20 20 20 61 20 36 34 2d 62 69 74 20         a 64-bit 
5d20: 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 2e 0a  signed integer..
5d30: 2a 2a 0a 2a 2a 20 7b 48 31 30 32 30 32 7d 20 54  **.** {H10202} T
5d40: 68 65 20 5b 73 71 6c 69 74 65 5f 75 69 6e 74 36  he [sqlite_uint6
5d50: 34 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  4] and [sqlite3_
5d60: 75 69 6e 74 36 34 5d 20 74 79 70 65 20 73 68 61  uint64] type sha
5d70: 6c 6c 20 73 70 65 63 69 66 79 0a 2a 2a 20 20 20  ll specify.**   
5d80: 20 20 20 20 20 20 20 61 20 36 34 2d 62 69 74 20         a 64-bit 
5d90: 75 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  unsigned integer
5da0: 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
5db0: 54 45 5f 49 4e 54 36 34 5f 54 59 50 45 0a 20 20  TE_INT64_TYPE.  
5dc0: 74 79 70 65 64 65 66 20 53 51 4c 49 54 45 5f 49  typedef SQLITE_I
5dd0: 4e 54 36 34 5f 54 59 50 45 20 73 71 6c 69 74 65  NT64_TYPE sqlite
5de0: 5f 69 6e 74 36 34 3b 0a 20 20 74 79 70 65 64 65  _int64;.  typede
5df0: 66 20 75 6e 73 69 67 6e 65 64 20 53 51 4c 49 54  f unsigned SQLIT
5e00: 45 5f 49 4e 54 36 34 5f 54 59 50 45 20 73 71 6c  E_INT64_TYPE sql
5e10: 69 74 65 5f 75 69 6e 74 36 34 3b 0a 23 65 6c 69  ite_uint64;.#eli
5e20: 66 20 64 65 66 69 6e 65 64 28 5f 4d 53 43 5f 56  f defined(_MSC_V
5e30: 45 52 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  ER) || defined(_
5e40: 5f 42 4f 52 4c 41 4e 44 43 5f 5f 29 0a 20 20 74  _BORLANDC__).  t
5e50: 79 70 65 64 65 66 20 5f 5f 69 6e 74 36 34 20 73  ypedef __int64 s
5e60: 71 6c 69 74 65 5f 69 6e 74 36 34 3b 0a 20 20 74  qlite_int64;.  t
5e70: 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64 20  ypedef unsigned 
5e80: 5f 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 5f 75  __int64 sqlite_u
5e90: 69 6e 74 36 34 3b 0a 23 65 6c 73 65 0a 20 20 74  int64;.#else.  t
5ea0: 79 70 65 64 65 66 20 6c 6f 6e 67 20 6c 6f 6e 67  ypedef long long
5eb0: 20 69 6e 74 20 73 71 6c 69 74 65 5f 69 6e 74 36   int sqlite_int6
5ec0: 34 3b 0a 20 20 74 79 70 65 64 65 66 20 75 6e 73  4;.  typedef uns
5ed0: 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20  igned long long 
5ee0: 69 6e 74 20 73 71 6c 69 74 65 5f 75 69 6e 74 36  int sqlite_uint6
5ef0: 34 3b 0a 23 65 6e 64 69 66 0a 74 79 70 65 64 65  4;.#endif.typede
5f00: 66 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 73  f sqlite_int64 s
5f10: 71 6c 69 74 65 33 5f 69 6e 74 36 34 3b 0a 74 79  qlite3_int64;.ty
5f20: 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75 69 6e  pedef sqlite_uin
5f30: 74 36 34 20 73 71 6c 69 74 65 33 5f 75 69 6e 74  t64 sqlite3_uint
5f40: 36 34 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f  64;../*.** If co
5f50: 6d 70 69 6c 69 6e 67 20 66 6f 72 20 61 20 70 72  mpiling for a pr
5f60: 6f 63 65 73 73 6f 72 20 74 68 61 74 20 6c 61 63  ocessor that lac
5f70: 6b 73 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ks floating poin
5f80: 74 20 73 75 70 70 6f 72 74 2c 0a 2a 2a 20 73 75  t support,.** su
5f90: 62 73 74 69 74 75 74 65 20 69 6e 74 65 67 65 72  bstitute integer
5fa0: 20 66 6f 72 20 66 6c 6f 61 74 69 6e 67 2d 70 6f   for floating-po
5fb0: 69 6e 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  int..*/.#ifdef S
5fc0: 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
5fd0: 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 64 65 66 69  ING_POINT.# defi
5fe0: 6e 65 20 64 6f 75 62 6c 65 20 73 71 6c 69 74 65  ne double sqlite
5ff0: 33 5f 69 6e 74 36 34 0a 23 65 6e 64 69 66 0a 0a  3_int64.#endif..
6000: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
6010: 43 6c 6f 73 69 6e 67 20 41 20 44 61 74 61 62 61  Closing A Databa
6020: 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 7b 48  se Connection {H
6030: 31 32 30 31 30 7d 20 3c 53 33 30 31 30 30 3e 3c  12010} <S30100><
6040: 53 34 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40200>.**.** Th
6050: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
6060: 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  e destructor for
6070: 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d 20 6f   the [sqlite3] o
6080: 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 70 70  bject..**.** App
6090: 6c 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64  lications should
60a0: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69   [sqlite3_finali
60b0: 7a 65 20 7c 20 66 69 6e 61 6c 69 7a 65 5d 20 61  ze | finalize] a
60c0: 6c 6c 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  ll [prepared sta
60d0: 74 65 6d 65 6e 74 73 5d 0a 2a 2a 20 61 6e 64 20  tements].** and 
60e0: 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c  [sqlite3_blob_cl
60f0: 6f 73 65 20 7c 20 63 6c 6f 73 65 5d 20 61 6c 6c  ose | close] all
6100: 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 73 5d 20   [BLOB handles] 
6110: 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
6120: 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5d  ** the [sqlite3]
6130: 20 6f 62 6a 65 63 74 20 70 72 69 6f 72 20 74 6f   object prior to
6140: 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 63   attempting to c
6150: 6c 6f 73 65 20 74 68 65 20 6f 62 6a 65 63 74 2e  lose the object.
6160: 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
6170: 5f 6e 65 78 74 5f 73 74 6d 74 28 29 5d 20 69 6e  _next_stmt()] in
6180: 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
6190: 73 65 64 20 74 6f 20 6c 6f 63 61 74 65 20 61 6c  sed to locate al
61a0: 6c 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  l.** [prepared s
61b0: 74 61 74 65 6d 65 6e 74 73 5d 20 61 73 73 6f 63  tatements] assoc
61c0: 69 61 74 65 64 20 77 69 74 68 20 61 20 5b 64 61  iated with a [da
61d0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
61e0: 6e 5d 20 69 66 20 64 65 73 69 72 65 64 2e 0a 2a  n] if desired..*
61f0: 2a 20 54 79 70 69 63 61 6c 20 63 6f 64 65 20 6d  * Typical code m
6200: 69 67 68 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 74  ight look like t
6210: 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63  his:.**.** <bloc
6220: 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20  kquote><pre>.** 
6230: 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
6240: 74 6d 74 3b 0a 2a 2a 20 77 68 69 6c 65 28 20 28  tmt;.** while( (
6250: 70 53 74 6d 74 20 3d 20 73 71 6c 69 74 65 33 5f  pStmt = sqlite3_
6260: 6e 65 78 74 5f 73 74 6d 74 28 64 62 2c 20 30 29  next_stmt(db, 0)
6270: 29 21 3d 30 20 29 7b 0a 2a 2a 20 26 6e 62 73 70  )!=0 ){.** &nbsp
6280: 3b 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  ;   sqlite3_fina
6290: 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 2a 2a 20  lize(pStmt);.** 
62a0: 7d 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  }.** </pre></blo
62b0: 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 49  ckquote>.**.** I
62c0: 66 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  f [sqlite3_close
62d0: 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 77  ()] is invoked w
62e0: 68 69 6c 65 20 61 20 74 72 61 6e 73 61 63 74 69  hile a transacti
62f0: 6f 6e 20 69 73 20 6f 70 65 6e 2c 0a 2a 2a 20 74  on is open,.** t
6300: 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
6310: 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  s automatically 
6320: 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2a 0a  rolled back..**.
6330: 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
6340: 2a 0a 2a 2a 20 7b 48 31 32 30 31 31 7d 20 41 20  *.** {H12011} A 
6350: 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
6360: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73  to [sqlite3_clos
6370: 65 28 43 29 5d 20 73 68 61 6c 6c 20 64 65 73 74  e(C)] shall dest
6380: 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  roy the.**      
6390: 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f      [database co
63a0: 6e 6e 65 63 74 69 6f 6e 5d 20 6f 62 6a 65 63 74  nnection] object
63b0: 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31   C..**.** {H1201
63c0: 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  2} A successful 
63d0: 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
63e0: 5f 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c  _close(C)] shall
63f0: 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
6400: 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 30 31 33  K..**.** {H12013
6410: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
6420: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
6430: 63 6c 6f 73 65 28 43 29 5d 20 73 68 61 6c 6c 20  close(C)] shall 
6440: 72 65 6c 65 61 73 65 20 61 6c 6c 0a 2a 2a 20 20  release all.**  
6450: 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 61          memory a
6460: 6e 64 20 73 79 73 74 65 6d 20 72 65 73 6f 75 72  nd system resour
6470: 63 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ces associated w
6480: 69 74 68 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ith [database co
6490: 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 20 20 20  nnection].**    
64a0: 20 20 20 20 20 20 43 2e 0a 2a 2a 0a 2a 2a 20 7b        C..**.** {
64b0: 48 31 32 30 31 34 7d 20 41 20 63 61 6c 6c 20 74  H12014} A call t
64c0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65  o [sqlite3_close
64d0: 28 43 29 5d 20 6f 6e 20 61 20 5b 64 61 74 61 62  (C)] on a [datab
64e0: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
64f0: 43 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20  C that.**       
6500: 20 20 20 68 61 73 20 6f 6e 65 20 6f 72 20 6d 6f     has one or mo
6510: 72 65 20 6f 70 65 6e 20 5b 70 72 65 70 61 72 65  re open [prepare
6520: 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 68  d statements] sh
6530: 61 6c 6c 20 66 61 69 6c 20 77 69 74 68 0a 2a 2a  all fail with.**
6540: 20 20 20 20 20 20 20 20 20 20 61 6e 20 5b 53 51            an [SQ
6550: 4c 49 54 45 5f 42 55 53 59 5d 20 65 72 72 6f 72  LITE_BUSY] error
6560: 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31   code..**.** {H1
6570: 32 30 31 35 7d 20 41 20 63 61 6c 6c 20 74 6f 20  2015} A call to 
6580: 5b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 43  [sqlite3_close(C
6590: 29 5d 20 77 68 65 72 65 20 43 20 69 73 20 61 20  )] where C is a 
65a0: 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 73 68 61  NULL pointer sha
65b0: 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62  ll.**          b
65c0: 65 20 61 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d  e a harmless no-
65d0: 6f 70 20 72 65 74 75 72 6e 69 6e 67 20 53 51 4c  op returning SQL
65e0: 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ITE_OK..**.** {H
65f0: 31 32 30 31 39 7d 20 57 68 65 6e 20 5b 73 71 6c  12019} When [sql
6600: 69 74 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 69  ite3_close(C)] i
6610: 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 61 20 5b  s invoked on a [
6620: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
6630: 69 6f 6e 5d 20 43 0a 2a 2a 20 20 20 20 20 20 20  ion] C.**       
6640: 20 20 20 74 68 61 74 20 68 61 73 20 61 20 70 65     that has a pe
6650: 6e 64 69 6e 67 20 74 72 61 6e 73 61 63 74 69 6f  nding transactio
6660: 6e 2c 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  n, the transacti
6670: 6f 6e 20 73 68 61 6c 6c 20 62 65 0a 2a 2a 20 20  on shall be.**  
6680: 20 20 20 20 20 20 20 20 72 6f 6c 6c 65 64 20 62          rolled b
6690: 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d  ack..**.** ASSUM
66a0: 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41  PTIONS:.**.** {A
66b0: 31 32 30 31 36 7d 20 54 68 65 20 43 20 70 61 72  12016} The C par
66c0: 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74  ameter to [sqlit
66d0: 65 33 5f 63 6c 6f 73 65 28 43 29 5d 20 6d 75 73  e3_close(C)] mus
66e0: 74 20 62 65 20 65 69 74 68 65 72 20 61 20 4e 55  t be either a NU
66f0: 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  LL.**          p
6700: 6f 69 6e 74 65 72 20 6f 72 20 61 6e 20 5b 73 71  ointer or an [sq
6710: 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 20 70 6f  lite3] object po
6720: 69 6e 74 65 72 20 6f 62 74 61 69 6e 65 64 0a 2a  inter obtained.*
6730: 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20  *          from 
6740: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
6750: 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  , [sqlite3_open1
6760: 36 28 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20  6()], or.**     
6770: 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6f 70       [sqlite3_op
6780: 65 6e 5f 76 32 28 29 5d 2c 20 61 6e 64 20 6e 6f  en_v2()], and no
6790: 74 20 70 72 65 76 69 6f 75 73 6c 79 20 63 6c 6f  t previously clo
67a0: 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  sed..*/.SQLITE_A
67b0: 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
67c0: 6c 6f 73 65 28 73 71 6c 69 74 65 33 20 2a 29 3b  lose(sqlite3 *);
67d0: 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65  ../*.** The type
67e0: 20 66 6f 72 20 61 20 63 61 6c 6c 62 61 63 6b 20   for a callback 
67f0: 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
6800: 73 20 69 73 20 6c 65 67 61 63 79 20 61 6e 64 20  s is legacy and 
6810: 64 65 70 72 65 63 61 74 65 64 2e 20 20 49 74 20  deprecated.  It 
6820: 69 73 20 69 6e 63 6c 75 64 65 64 20 66 6f 72 20  is included for 
6830: 68 69 73 74 6f 72 69 63 61 6c 0a 2a 2a 20 63 6f  historical.** co
6840: 6d 70 61 74 69 62 69 6c 69 74 79 20 61 6e 64 20  mpatibility and 
6850: 69 73 20 6e 6f 74 20 64 6f 63 75 6d 65 6e 74 65  is not documente
6860: 64 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 69 6e  d..*/.typedef in
6870: 74 20 28 2a 73 71 6c 69 74 65 33 5f 63 61 6c 6c  t (*sqlite3_call
6880: 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  back)(void*,int,
6890: 63 68 61 72 2a 2a 2c 20 63 68 61 72 2a 2a 29 3b  char**, char**);
68a0: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
68b0: 3a 20 4f 6e 65 2d 53 74 65 70 20 51 75 65 72 79  : One-Step Query
68c0: 20 45 78 65 63 75 74 69 6f 6e 20 49 6e 74 65 72   Execution Inter
68d0: 66 61 63 65 20 7b 48 31 32 31 30 30 7d 20 3c 53  face {H12100} <S
68e0: 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  10000>.**.** The
68f0: 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
6900: 69 6e 74 65 72 66 61 63 65 20 69 73 20 61 20 63  interface is a c
6910: 6f 6e 76 65 6e 69 65 6e 74 20 77 61 79 20 6f 66  onvenient way of
6920: 20 72 75 6e 6e 69 6e 67 20 6f 6e 65 20 6f 72 20   running one or 
6930: 6d 6f 72 65 0a 2a 2a 20 53 51 4c 20 73 74 61 74  more.** SQL stat
6940: 65 6d 65 6e 74 73 20 77 69 74 68 6f 75 74 20 68  ements without h
6950: 61 76 69 6e 67 20 74 6f 20 77 72 69 74 65 20 61  aving to write a
6960: 20 6c 6f 74 20 6f 66 20 43 20 63 6f 64 65 2e 20   lot of C code. 
6970: 20 54 68 65 20 55 54 46 2d 38 20 65 6e 63 6f 64   The UTF-8 encod
6980: 65 64 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d  ed.** SQL statem
6990: 65 6e 74 73 20 61 72 65 20 70 61 73 73 65 64 20  ents are passed 
69a0: 69 6e 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  in as the second
69b0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
69c0: 6c 69 74 65 33 5f 65 78 65 63 28 29 2e 0a 2a 2a  lite3_exec()..**
69d0: 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 73 20   The statements 
69e0: 61 72 65 20 65 76 61 6c 75 61 74 65 64 20 6f 6e  are evaluated on
69f0: 65 20 62 79 20 6f 6e 65 20 75 6e 74 69 6c 20 65  e by one until e
6a00: 69 74 68 65 72 20 61 6e 20 65 72 72 6f 72 20 6f  ither an error o
6a10: 72 0a 2a 2a 20 61 6e 20 69 6e 74 65 72 72 75 70  r.** an interrup
6a20: 74 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  t is encountered
6a30: 2c 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 79 20  , or until they 
6a40: 61 72 65 20 61 6c 6c 20 64 6f 6e 65 2e 20 20 54  are all done.  T
6a50: 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
6a60: 0a 2a 2a 20 69 73 20 61 6e 20 6f 70 74 69 6f 6e  .** is an option
6a70: 61 6c 20 63 61 6c 6c 62 61 63 6b 20 74 68 61 74  al callback that
6a80: 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65   is invoked once
6a90: 20 66 6f 72 20 65 61 63 68 20 72 6f 77 20 6f 66   for each row of
6aa0: 20 61 6e 79 20 71 75 65 72 79 0a 2a 2a 20 72 65   any query.** re
6ab0: 73 75 6c 74 73 20 70 72 6f 64 75 63 65 64 20 62  sults produced b
6ac0: 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  y the SQL statem
6ad0: 65 6e 74 73 2e 20 20 54 68 65 20 35 74 68 20 70  ents.  The 5th p
6ae0: 61 72 61 6d 65 74 65 72 20 74 65 6c 6c 73 20 77  arameter tells w
6af0: 68 65 72 65 0a 2a 2a 20 74 6f 20 77 72 69 74 65  here.** to write
6b00: 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61   any error messa
6b10: 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65  ges..**.** The e
6b20: 72 72 6f 72 20 6d 65 73 73 61 67 65 20 70 61 73  rror message pas
6b30: 73 65 64 20 62 61 63 6b 20 74 68 72 6f 75 67 68  sed back through
6b40: 20 74 68 65 20 35 74 68 20 70 61 72 61 6d 65 74   the 5th paramet
6b50: 65 72 20 69 73 20 68 65 6c 64 0a 2a 2a 20 69 6e  er is held.** in
6b60: 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
6b70: 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d   from [sqlite3_m
6b80: 61 6c 6c 6f 63 28 29 5d 2e 20 20 54 6f 20 61 76  alloc()].  To av
6b90: 6f 69 64 20 61 20 6d 65 6d 6f 72 79 20 6c 65 61  oid a memory lea
6ba0: 6b 2c 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 69 6e  k,.** the callin
6bb0: 67 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68  g application sh
6bc0: 6f 75 6c 64 20 63 61 6c 6c 20 5b 73 71 6c 69 74  ould call [sqlit
6bd0: 65 33 5f 66 72 65 65 28 29 5d 20 6f 6e 20 61 6e  e3_free()] on an
6be0: 79 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61  y error.** messa
6bf0: 67 65 20 72 65 74 75 72 6e 65 64 20 74 68 72 6f  ge returned thro
6c00: 75 67 68 20 74 68 65 20 35 74 68 20 70 61 72 61  ugh the 5th para
6c10: 6d 65 74 65 72 20 77 68 65 6e 20 69 74 20 68 61  meter when it ha
6c20: 73 20 66 69 6e 69 73 68 65 64 20 75 73 69 6e 67  s finished using
6c30: 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 6d 65  .** the error me
6c40: 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssage..**.** If 
6c50: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
6c60: 74 20 69 6e 20 74 68 65 20 32 6e 64 20 70 61 72  t in the 2nd par
6c70: 61 6d 65 74 65 72 20 69 73 20 4e 55 4c 4c 20 6f  ameter is NULL o
6c80: 72 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e  r an empty strin
6c90: 67 0a 2a 2a 20 6f 72 20 61 20 73 74 72 69 6e 67  g.** or a string
6ca0: 20 63 6f 6e 74 61 69 6e 69 6e 67 20 6f 6e 6c 79   containing only
6cb0: 20 77 68 69 74 65 73 70 61 63 65 20 61 6e 64 20   whitespace and 
6cc0: 63 6f 6d 6d 65 6e 74 73 2c 20 74 68 65 6e 20 6e  comments, then n
6cd0: 6f 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  o SQL.** stateme
6ce0: 6e 74 73 20 61 72 65 20 65 76 61 6c 75 61 74 65  nts are evaluate
6cf0: 64 20 61 6e 64 20 74 68 65 20 64 61 74 61 62 61  d and the databa
6d00: 73 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  se is not change
6d10: 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  d..**.** The sql
6d20: 69 74 65 33 5f 65 78 65 63 28 29 20 69 6e 74 65  ite3_exec() inte
6d30: 72 66 61 63 65 20 69 73 20 69 6d 70 6c 65 6d 65  rface is impleme
6d40: 6e 74 65 64 20 69 6e 20 74 65 72 6d 73 20 6f 66  nted in terms of
6d50: 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
6d60: 70 61 72 65 5f 76 32 28 29 5d 2c 20 5b 73 71 6c  pare_v2()], [sql
6d70: 69 74 65 33 5f 73 74 65 70 28 29 5d 2c 20 61 6e  ite3_step()], an
6d80: 64 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  d [sqlite3_final
6d90: 69 7a 65 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73  ize()]..** The s
6da0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 6f  qlite3_exec() ro
6db0: 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 68 69  utine does nothi
6dc0: 6e 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ng to the databa
6dd0: 73 65 20 74 68 61 74 20 63 61 6e 6e 6f 74 20 62  se that cannot b
6de0: 65 20 64 6f 6e 65 0a 2a 2a 20 62 79 20 5b 73 71  e done.** by [sq
6df0: 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
6e00: 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 73 74  ()], [sqlite3_st
6e10: 65 70 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69  ep()], and [sqli
6e20: 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 2e  te3_finalize()].
6e30: 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
6e40: 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 30 31  S:.**.** {H12101
6e50: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 69  } A successful i
6e60: 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71  nvocation of [sq
6e70: 6c 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43  lite3_exec(D,S,C
6e80: 2c 41 2c 45 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,A,E)].**       
6e90: 20 20 20 73 68 61 6c 6c 20 73 65 71 75 65 6e 74     shall sequent
6ea0: 69 61 6c 6c 79 20 65 76 61 6c 75 61 74 65 20 61  ially evaluate a
6eb0: 6c 6c 20 6f 66 20 74 68 65 20 55 54 46 2d 38 20  ll of the UTF-8 
6ec0: 65 6e 63 6f 64 65 64 2c 0a 2a 2a 20 20 20 20 20  encoded,.**     
6ed0: 20 20 20 20 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73       semicolon-s
6ee0: 65 70 61 72 61 74 65 64 20 53 51 4c 20 73 74 61  eparated SQL sta
6ef0: 74 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 7a  tements in the z
6f00: 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a  ero-terminated.*
6f10: 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e  *          strin
6f20: 67 20 53 20 77 69 74 68 69 6e 20 74 68 65 20 63  g S within the c
6f30: 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 5b 64  ontext of the [d
6f40: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
6f50: 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  on] D..**.** {H1
6f60: 32 31 30 32 7d 20 49 66 20 74 68 65 20 53 20 70  2102} If the S p
6f70: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
6f80: 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c  ite3_exec(D,S,C,
6f90: 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20 74 68  A,E)] is NULL th
6fa0: 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  en.**          t
6fb0: 68 65 20 61 63 74 69 6f 6e 73 20 6f 66 20 74 68  he actions of th
6fc0: 65 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c  e interface shal
6fd0: 6c 20 62 65 20 74 68 65 20 73 61 6d 65 20 61 73  l be the same as
6fe0: 20 69 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   if the.**      
6ff0: 20 20 20 20 53 20 70 61 72 61 6d 65 74 65 72 20      S parameter 
7000: 77 65 72 65 20 61 6e 20 65 6d 70 74 79 20 73 74  were an empty st
7010: 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ring..**.** {H12
7020: 31 30 34 7d 20 54 68 65 20 72 65 74 75 72 6e 20  104} The return 
7030: 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65  value of [sqlite
7040: 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c 20  3_exec()] shall 
7050: 62 65 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69  be [SQLITE_OK] i
7060: 66 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20  f all.**        
7070: 20 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73    SQL statements
7080: 20 72 75 6e 20 73 75 63 63 65 73 73 66 75 6c 6c   run successfull
7090: 79 20 61 6e 64 20 74 6f 20 63 6f 6d 70 6c 65 74  y and to complet
70a0: 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31  ion..**.** {H121
70b0: 30 35 7d 20 54 68 65 20 72 65 74 75 72 6e 20 76  05} The return v
70c0: 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69 74 65 33  alue of [sqlite3
70d0: 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c 20 62  _exec()] shall b
70e0: 65 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  e an appropriate
70f0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 6f 6e  .**          non
7100: 2d 7a 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64  -zero [error cod
7110: 65 5d 20 69 66 20 61 6e 79 20 53 51 4c 20 73 74  e] if any SQL st
7120: 61 74 65 6d 65 6e 74 20 66 61 69 6c 73 2e 0a 2a  atement fails..*
7130: 2a 0a 2a 2a 20 7b 48 31 32 31 30 37 7d 20 49 66  *.** {H12107} If
7140: 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20   one or more of 
7150: 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
7160: 74 73 20 68 61 6e 64 65 64 20 74 6f 20 5b 73 71  ts handed to [sq
7170: 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a 2a  lite3_exec()].**
7180: 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
7190: 20 72 65 73 75 6c 74 73 20 61 6e 64 20 74 68 65   results and the
71a0: 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 69   3rd parameter i
71b0: 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e  s not NULL, then
71c0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
71d0: 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
71e0: 6f 6e 20 73 70 65 63 69 66 69 65 64 20 62 79 20  on specified by 
71f0: 74 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65  the 3rd paramete
7200: 72 20 73 68 61 6c 6c 20 62 65 0a 2a 2a 20 20 20  r shall be.**   
7210: 20 20 20 20 20 20 20 69 6e 76 6f 6b 65 64 20 6f         invoked o
7220: 6e 63 65 20 66 6f 72 20 65 61 63 68 20 72 6f 77  nce for each row
7230: 20 6f 66 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a   of result..**.*
7240: 2a 20 7b 48 31 32 31 31 30 7d 20 49 66 20 74 68  * {H12110} If th
7250: 65 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72  e callback retur
7260: 6e 73 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61  ns a non-zero va
7270: 6c 75 65 20 74 68 65 6e 20 5b 73 71 6c 69 74 65  lue then [sqlite
7280: 33 5f 65 78 65 63 28 29 5d 0a 2a 2a 20 20 20 20  3_exec()].**    
7290: 20 20 20 20 20 20 73 68 61 6c 6c 20 61 62 6f 72        shall abor
72a0: 74 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  t the SQL statem
72b0: 65 6e 74 20 69 74 20 69 73 20 63 75 72 72 65 6e  ent it is curren
72c0: 74 6c 79 20 65 76 61 6c 75 61 74 69 6e 67 2c 0a  tly evaluating,.
72d0: 2a 2a 20 20 20 20 20 20 20 20 20 20 73 6b 69 70  **          skip
72e0: 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 20   all subsequent 
72f0: 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2c 20  SQL statements, 
7300: 61 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49  and return [SQLI
7310: 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a  TE_ABORT]..**.**
7320: 20 7b 48 31 32 31 31 33 7d 20 54 68 65 20 5b 73   {H12113} The [s
7330: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 20 72  qlite3_exec()] r
7340: 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 70 61 73  outine shall pas
7350: 73 20 69 74 73 20 34 74 68 20 70 61 72 61 6d 65  s its 4th parame
7360: 74 65 72 20 74 68 72 6f 75 67 68 0a 2a 2a 20 20  ter through.**  
7370: 20 20 20 20 20 20 20 20 61 73 20 74 68 65 20 31          as the 1
7380: 73 74 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  st parameter of 
7390: 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a  the callback..**
73a0: 0a 2a 2a 20 7b 48 31 32 31 31 36 7d 20 54 68 65  .** {H12116} The
73b0: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
73c0: 5d 20 72 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20  ] routine shall 
73d0: 73 65 74 20 74 68 65 20 32 6e 64 20 70 61 72 61  set the 2nd para
73e0: 6d 65 74 65 72 20 6f 66 20 69 74 73 0a 2a 2a 20  meter of its.** 
73f0: 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62 61 63           callbac
7400: 6b 20 74 6f 20 62 65 20 74 68 65 20 6e 75 6d 62  k to be the numb
7410: 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
7420: 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
7430: 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
7440: 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48  result..**.** {H
7450: 31 32 31 31 39 7d 20 54 68 65 20 5b 73 71 6c 69  12119} The [sqli
7460: 74 65 33 5f 65 78 65 63 28 29 5d 20 72 6f 75 74  te3_exec()] rout
7470: 69 6e 65 20 73 68 61 6c 6c 20 73 65 74 20 74 68  ine shall set th
7480: 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20  e 3rd parameter 
7490: 6f 66 20 69 74 73 0a 2a 2a 20 20 20 20 20 20 20  of its.**       
74a0: 20 20 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62     callback to b
74b0: 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20 70 6f  e an array of po
74c0: 69 6e 74 65 72 73 20 74 6f 20 73 74 72 69 6e 67  inters to string
74d0: 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a  s holding the.**
74e0: 20 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 73            values
74f0: 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
7500: 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
7510: 72 65 73 75 6c 74 20 73 65 74 20 72 6f 77 20 61  result set row a
7520: 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62  s.**          ob
7530: 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
7540: 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
7550: 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31  ()]..**.** {H121
7560: 32 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  22} The [sqlite3
7570: 5f 65 78 65 63 28 29 5d 20 72 6f 75 74 69 6e 65  _exec()] routine
7580: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 34   shall set the 4
7590: 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  th parameter of 
75a0: 69 74 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  its.**          
75b0: 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 61  callback to be a
75c0: 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74  n array of point
75d0: 65 72 73 20 74 6f 20 73 74 72 69 6e 67 73 20 68  ers to strings h
75e0: 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a 20 20 20  olding the.**   
75f0: 20 20 20 20 20 20 20 6e 61 6d 65 73 20 6f 66 20         names of 
7600: 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 61  result columns a
7610: 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
7620: 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
7630: 6e 61 6d 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  name()]..**.** {
7640: 48 31 32 31 32 35 7d 20 49 66 20 74 68 65 20 33  H12125} If the 3
7650: 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  rd parameter to 
7660: 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d  [sqlite3_exec()]
7670: 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a   is NULL then.**
7680: 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
7690: 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c  e3_exec()] shall
76a0: 20 73 69 6c 65 6e 74 6c 79 20 64 69 73 63 61 72   silently discar
76b0: 64 20 71 75 65 72 79 20 72 65 73 75 6c 74 73 2e  d query results.
76c0: 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31 33 31 7d 20  .**.** {H12131} 
76d0: 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
76e0: 72 73 20 77 68 69 6c 65 20 70 61 72 73 69 6e 67  rs while parsing
76f0: 20 6f 72 20 65 76 61 6c 75 61 74 69 6e 67 20 61   or evaluating a
7700: 6e 79 20 6f 66 20 74 68 65 20 53 51 4c 0a 2a 2a  ny of the SQL.**
7710: 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 6d            statem
7720: 65 6e 74 73 20 69 6e 20 74 68 65 20 53 20 70 61  ents in the S pa
7730: 72 61 6d 65 74 65 72 20 6f 66 20 5b 73 71 6c 69  rameter of [sqli
7740: 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41  te3_exec(D,S,C,A
7750: 2c 45 29 5d 20 61 6e 64 20 69 66 0a 2a 2a 20 20  ,E)] and if.**  
7760: 20 20 20 20 20 20 20 20 74 68 65 20 45 20 70 61          the E pa
7770: 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 74 20 4e  rameter is not N
7780: 55 4c 4c 2c 20 74 68 65 6e 20 5b 73 71 6c 69 74  ULL, then [sqlit
7790: 65 33 5f 65 78 65 63 28 29 5d 20 73 68 61 6c 6c  e3_exec()] shall
77a0: 20 73 74 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20   store.**       
77b0: 20 20 20 69 6e 20 2a 45 20 61 6e 20 61 70 70 72     in *E an appr
77c0: 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
77d0: 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 69 6e  ssage written in
77e0: 74 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e  to memory obtain
77f0: 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  ed.**          f
7800: 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
7810: 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  loc()]..**.** {H
7820: 31 32 31 33 34 7d 20 54 68 65 20 5b 73 71 6c 69  12134} The [sqli
7830: 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41  te3_exec(D,S,C,A
7840: 2c 45 29 5d 20 72 6f 75 74 69 6e 65 20 73 68 61  ,E)] routine sha
7850: 6c 6c 20 73 65 74 20 74 68 65 20 76 61 6c 75 65  ll set the value
7860: 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
7870: 2a 45 20 74 6f 20 4e 55 4c 4c 20 69 66 20 45 20  *E to NULL if E 
7880: 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e 64 20  is not NULL and 
7890: 74 68 65 72 65 20 61 72 65 20 6e 6f 20 65 72 72  there are no err
78a0: 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 31  ors..**.** {H121
78b0: 33 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  37} The [sqlite3
78c0: 5f 65 78 65 63 28 44 2c 53 2c 43 2c 41 2c 45 29  _exec(D,S,C,A,E)
78d0: 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c  ] function shall
78e0: 20 73 65 74 20 74 68 65 20 5b 65 72 72 6f 72 20   set the [error 
78f0: 63 6f 64 65 5d 0a 2a 2a 20 20 20 20 20 20 20 20  code].**        
7900: 20 20 61 6e 64 20 6d 65 73 73 61 67 65 20 61 63    and message ac
7910: 63 65 73 73 69 62 6c 65 20 76 69 61 20 5b 73 71  cessible via [sq
7920: 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d  lite3_errcode()]
7930: 2c 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  , .**          [
7940: 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
7950: 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20  _errcode()],.** 
7960: 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
7970: 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 61 6e 64  3_errmsg()], and
7980: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
7990: 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  16()]..**.** {H1
79a0: 32 31 33 38 7d 20 49 66 20 74 68 65 20 53 20 70  2138} If the S p
79b0: 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
79c0: 69 74 65 33 5f 65 78 65 63 28 44 2c 53 2c 43 2c  ite3_exec(D,S,C,
79d0: 41 2c 45 29 5d 20 69 73 20 4e 55 4c 4c 20 6f 72  A,E)] is NULL or
79e0: 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   an.**          
79f0: 65 6d 70 74 79 20 73 74 72 69 6e 67 20 6f 72 20  empty string or 
7a00: 63 6f 6e 74 61 69 6e 73 20 6e 6f 74 68 69 6e 67  contains nothing
7a10: 20 6f 74 68 65 72 20 74 68 61 6e 20 77 68 69 74   other than whit
7a20: 65 73 70 61 63 65 2c 20 63 6f 6d 6d 65 6e 74 73  espace, comments
7a30: 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  ,.**          an
7a40: 64 2f 6f 72 20 73 65 6d 69 63 6f 6c 6f 6e 73 2c  d/or semicolons,
7a50: 20 74 68 65 6e 20 72 65 73 75 6c 74 73 20 6f 66   then results of
7a60: 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64   [sqlite3_errcod
7a70: 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  e()],.**        
7a80: 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e    [sqlite3_exten
7a90: 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a  ded_errcode()],.
7aa0: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
7ab0: 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20  ite3_errmsg()], 
7ac0: 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  and [sqlite3_err
7ad0: 6d 73 67 31 36 28 29 5d 0a 2a 2a 20 20 20 20 20  msg16()].**     
7ae0: 20 20 20 20 20 73 68 61 6c 6c 20 72 65 73 65 74       shall reset
7af0: 20 74 6f 20 69 6e 64 69 63 61 74 65 20 6e 6f 20   to indicate no 
7b00: 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 41 53  errors..**.** AS
7b10: 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a  SUMPTIONS:.**.**
7b20: 20 7b 41 31 32 31 34 31 7d 20 54 68 65 20 66 69   {A12141} The fi
7b30: 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rst parameter to
7b40: 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29   [sqlite3_exec()
7b50: 5d 20 6d 75 73 74 20 62 65 20 61 6e 20 76 61 6c  ] must be an val
7b60: 69 64 20 61 6e 64 20 6f 70 65 6e 0a 2a 2a 20 20  id and open.**  
7b70: 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73          [databas
7b80: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a  e connection]..*
7b90: 2a 0a 2a 2a 20 7b 41 31 32 31 34 32 7d 20 54 68  *.** {A12142} Th
7ba0: 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
7bb0: 63 74 69 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 62  ction must not b
7bc0: 65 20 63 6c 6f 73 65 64 20 77 68 69 6c 65 0a 2a  e closed while.*
7bd0: 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
7be0: 74 65 33 5f 65 78 65 63 28 29 5d 20 69 73 20 72  te3_exec()] is r
7bf0: 75 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 41  unning..**.** {A
7c00: 31 32 31 34 33 7d 20 54 68 65 20 63 61 6c 6c 69  12143} The calli
7c10: 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 68 6f 75  ng function shou
7c20: 6c 64 20 75 73 65 20 5b 73 71 6c 69 74 65 33 5f  ld use [sqlite3_
7c30: 66 72 65 65 28 29 5d 20 74 6f 20 66 72 65 65 0a  free()] to free.
7c40: 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
7c50: 6d 65 6d 6f 72 79 20 74 68 61 74 20 2a 65 72 72  memory that *err
7c60: 6d 73 67 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  msg is left poin
7c70: 74 69 6e 67 20 61 74 20 6f 6e 63 65 20 74 68 65  ting at once the
7c80: 20 65 72 72 6f 72 0a 2a 2a 20 20 20 20 20 20 20   error.**       
7c90: 20 20 20 6d 65 73 73 61 67 65 20 69 73 20 6e 6f     message is no
7ca0: 20 6c 6f 6e 67 65 72 20 6e 65 65 64 65 64 2e 0a   longer needed..
7cb0: 2a 2a 0a 2a 2a 20 7b 41 31 32 31 34 35 7d 20 54  **.** {A12145} T
7cc0: 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
7cd0: 20 74 65 78 74 20 69 6e 20 74 68 65 20 32 6e 64   text in the 2nd
7ce0: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
7cf0: 71 6c 69 74 65 33 5f 65 78 65 63 28 29 5d 0a 2a  qlite3_exec()].*
7d00: 2a 20 20 20 20 20 20 20 20 20 20 6d 75 73 74 20  *          must 
7d10: 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64  remain unchanged
7d20: 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f   while [sqlite3_
7d30: 65 78 65 63 28 29 5d 20 69 73 20 72 75 6e 6e 69  exec()] is runni
7d40: 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ng..*/.SQLITE_AP
7d50: 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78  I int sqlite3_ex
7d60: 65 63 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  ec(.  sqlite3*, 
7d70: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d80: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7d90: 20 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61   /* An open data
7da0: 62 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  base */.  const 
7db0: 63 68 61 72 20 2a 73 71 6c 2c 20 20 20 20 20 20  char *sql,      
7dc0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7dd0: 20 20 20 20 20 2f 2a 20 53 51 4c 20 74 6f 20 62       /* SQL to b
7de0: 65 20 65 76 61 6c 75 61 74 65 64 20 2a 2f 0a 20  e evaluated */. 
7df0: 20 69 6e 74 20 28 2a 63 61 6c 6c 62 61 63 6b 29   int (*callback)
7e00: 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 68 61 72 2a  (void*,int,char*
7e10: 2a 2c 63 68 61 72 2a 2a 29 2c 20 20 2f 2a 20 43  *,char**),  /* C
7e20: 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
7e30: 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2c 20 20 20   */.  void *,   
7e40: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e50: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7e60: 20 2f 2a 20 31 73 74 20 61 72 67 75 6d 65 6e 74   /* 1st argument
7e70: 20 74 6f 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a   to callback */.
7e80: 20 20 63 68 61 72 20 2a 2a 65 72 72 6d 73 67 20    char **errmsg 
7e90: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7ea0: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
7eb0: 45 72 72 6f 72 20 6d 73 67 20 77 72 69 74 74 65  Error msg writte
7ec0: 6e 20 68 65 72 65 20 2a 2f 0a 29 3b 0a 0a 2f 2a  n here */.);../*
7ed0: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65  .** CAPI3REF: Re
7ee0: 73 75 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32  sult Codes {H102
7ef0: 31 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20  10} <S10700>.** 
7f00: 4b 45 59 57 4f 52 44 53 3a 20 53 51 4c 49 54 45  KEYWORDS: SQLITE
7f10: 5f 4f 4b 20 7b 65 72 72 6f 72 20 63 6f 64 65 7d  _OK {error code}
7f20: 20 7b 65 72 72 6f 72 20 63 6f 64 65 73 7d 0a 2a   {error codes}.*
7f30: 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 72 65 73  * KEYWORDS: {res
7f40: 75 6c 74 20 63 6f 64 65 7d 20 7b 72 65 73 75 6c  ult code} {resul
7f50: 74 20 63 6f 64 65 73 7d 0a 2a 2a 0a 2a 2a 20 4d  t codes}.**.** M
7f60: 61 6e 79 20 53 51 4c 69 74 65 20 66 75 6e 63 74  any SQLite funct
7f70: 69 6f 6e 73 20 72 65 74 75 72 6e 20 61 6e 20 69  ions return an i
7f80: 6e 74 65 67 65 72 20 72 65 73 75 6c 74 20 63 6f  nteger result co
7f90: 64 65 20 66 72 6f 6d 20 74 68 65 20 73 65 74 20  de from the set 
7fa0: 73 68 6f 77 6e 0a 2a 2a 20 68 65 72 65 20 69 6e  shown.** here in
7fb0: 20 6f 72 64 65 72 20 74 6f 20 69 6e 64 69 63 61   order to indica
7fc0: 74 65 73 20 73 75 63 63 65 73 73 20 6f 72 20 66  tes success or f
7fd0: 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65  ailure..**.** Ne
7fe0: 77 20 65 72 72 6f 72 20 63 6f 64 65 73 20 6d 61  w error codes ma
7ff0: 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75  y be added in fu
8000: 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  ture versions of
8010: 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 53   SQLite..**.** S
8020: 65 65 20 61 6c 73 6f 3a 20 5b 53 51 4c 49 54 45  ee also: [SQLITE
8030: 5f 49 4f 45 52 52 5f 52 45 41 44 20 7c 20 65 78  _IOERR_READ | ex
8040: 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f  tended result co
8050: 64 65 73 5d 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  des].*/.#define 
8060: 53 51 4c 49 54 45 5f 4f 4b 20 20 20 20 20 20 20  SQLITE_OK       
8070: 20 20 20 20 30 20 20 20 2f 2a 20 53 75 63 63 65      0   /* Succe
8080: 73 73 66 75 6c 20 72 65 73 75 6c 74 20 2a 2f 0a  ssful result */.
8090: 2f 2a 20 62 65 67 69 6e 6e 69 6e 67 2d 6f 66 2d  /* beginning-of-
80a0: 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a 2f 0a 23  error-codes */.#
80b0: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 52  define SQLITE_ER
80c0: 52 4f 52 20 20 20 20 20 20 20 20 31 20 20 20 2f  ROR        1   /
80d0: 2a 20 53 51 4c 20 65 72 72 6f 72 20 6f 72 20 6d  * SQL error or m
80e0: 69 73 73 69 6e 67 20 64 61 74 61 62 61 73 65 20  issing database 
80f0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8100: 45 5f 49 4e 54 45 52 4e 41 4c 20 20 20 20 20 32  E_INTERNAL     2
8110: 20 20 20 2f 2a 20 49 6e 74 65 72 6e 61 6c 20 6c     /* Internal l
8120: 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 53 51  ogic error in SQ
8130: 4c 69 74 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  Lite */.#define 
8140: 53 51 4c 49 54 45 5f 50 45 52 4d 20 20 20 20 20  SQLITE_PERM     
8150: 20 20 20 20 33 20 20 20 2f 2a 20 41 63 63 65 73      3   /* Acces
8160: 73 20 70 65 72 6d 69 73 73 69 6f 6e 20 64 65 6e  s permission den
8170: 69 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ied */.#define S
8180: 51 4c 49 54 45 5f 41 42 4f 52 54 20 20 20 20 20  QLITE_ABORT     
8190: 20 20 20 34 20 20 20 2f 2a 20 43 61 6c 6c 62 61     4   /* Callba
81a0: 63 6b 20 72 6f 75 74 69 6e 65 20 72 65 71 75 65  ck routine reque
81b0: 73 74 65 64 20 61 6e 20 61 62 6f 72 74 20 2a 2f  sted an abort */
81c0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
81d0: 42 55 53 59 20 20 20 20 20 20 20 20 20 35 20 20  BUSY         5  
81e0: 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
81f0: 20 66 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20   file is locked 
8200: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8210: 45 5f 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 36  E_LOCKED       6
8220: 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e     /* A table in
8230: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
8240: 20 6c 6f 63 6b 65 64 20 2a 2f 0a 23 64 65 66 69   locked */.#defi
8250: 6e 65 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20  ne SQLITE_NOMEM 
8260: 20 20 20 20 20 20 20 37 20 20 20 2f 2a 20 41 20         7   /* A 
8270: 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20  malloc() failed 
8280: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8290: 45 5f 52 45 41 44 4f 4e 4c 59 20 20 20 20 20 38  E_READONLY     8
82a0: 20 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f     /* Attempt to
82b0: 20 77 72 69 74 65 20 61 20 72 65 61 64 6f 6e 6c   write a readonl
82c0: 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 23 64  y database */.#d
82d0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4e 54  efine SQLITE_INT
82e0: 45 52 52 55 50 54 20 20 20 20 39 20 20 20 2f 2a  ERRUPT    9   /*
82f0: 20 4f 70 65 72 61 74 69 6f 6e 20 74 65 72 6d 69   Operation termi
8300: 6e 61 74 65 64 20 62 79 20 73 71 6c 69 74 65 33  nated by sqlite3
8310: 5f 69 6e 74 65 72 72 75 70 74 28 29 2a 2f 0a 23  _interrupt()*/.#
8320: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
8330: 45 52 52 20 20 20 20 20 20 20 31 30 20 20 20 2f  ERR       10   /
8340: 2a 20 53 6f 6d 65 20 6b 69 6e 64 20 6f 66 20 64  * Some kind of d
8350: 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 20 6f 63  isk I/O error oc
8360: 63 75 72 72 65 64 20 2a 2f 0a 23 64 65 66 69 6e  curred */.#defin
8370: 65 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54  e SQLITE_CORRUPT
8380: 20 20 20 20 20 31 31 20 20 20 2f 2a 20 54 68 65       11   /* The
8390: 20 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69   database disk i
83a0: 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65  mage is malforme
83b0: 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  d */.#define SQL
83c0: 49 54 45 5f 4e 4f 54 46 4f 55 4e 44 20 20 20 20  ITE_NOTFOUND    
83d0: 31 32 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45 44  12   /* NOT USED
83e0: 2e 20 54 61 62 6c 65 20 6f 72 20 72 65 63 6f 72  . Table or recor
83f0: 64 20 6e 6f 74 20 66 6f 75 6e 64 20 2a 2f 0a 23  d not found */.#
8400: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55  define SQLITE_FU
8410: 4c 4c 20 20 20 20 20 20 20 20 31 33 20 20 20 2f  LL        13   /
8420: 2a 20 49 6e 73 65 72 74 69 6f 6e 20 66 61 69 6c  * Insertion fail
8430: 65 64 20 62 65 63 61 75 73 65 20 64 61 74 61 62  ed because datab
8440: 61 73 65 20 69 73 20 66 75 6c 6c 20 2a 2f 0a 23  ase is full */.#
8450: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 41  define SQLITE_CA
8460: 4e 54 4f 50 45 4e 20 20 20 20 31 34 20 20 20 2f  NTOPEN    14   /
8470: 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e  * Unable to open
8480: 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
8490: 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  le */.#define SQ
84a0: 4c 49 54 45 5f 50 52 4f 54 4f 43 4f 4c 20 20 20  LITE_PROTOCOL   
84b0: 20 31 35 20 20 20 2f 2a 20 4e 4f 54 20 55 53 45   15   /* NOT USE
84c0: 44 2e 20 44 61 74 61 62 61 73 65 20 6c 6f 63 6b  D. Database lock
84d0: 20 70 72 6f 74 6f 63 6f 6c 20 65 72 72 6f 72 20   protocol error 
84e0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
84f0: 45 5f 45 4d 50 54 59 20 20 20 20 20 20 20 31 36  E_EMPTY       16
8500: 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
8510: 73 20 65 6d 70 74 79 20 2a 2f 0a 23 64 65 66 69  s empty */.#defi
8520: 6e 65 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  ne SQLITE_SCHEMA
8530: 20 20 20 20 20 20 31 37 20 20 20 2f 2a 20 54 68        17   /* Th
8540: 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
8550: 61 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65  a changed */.#de
8560: 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 4f 4f 42  fine SQLITE_TOOB
8570: 49 47 20 20 20 20 20 20 31 38 20 20 20 2f 2a 20  IG      18   /* 
8580: 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20 65  String or BLOB e
8590: 78 63 65 65 64 73 20 73 69 7a 65 20 6c 69 6d 69  xceeds size limi
85a0: 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  t */.#define SQL
85b0: 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20 20  ITE_CONSTRAINT  
85c0: 31 39 20 20 20 2f 2a 20 41 62 6f 72 74 20 64 75  19   /* Abort du
85d0: 65 20 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 20  e to constraint 
85e0: 76 69 6f 6c 61 74 69 6f 6e 20 2a 2f 0a 23 64 65  violation */.#de
85f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 49 53 4d  fine SQLITE_MISM
8600: 41 54 43 48 20 20 20 20 32 30 20 20 20 2f 2a 20  ATCH    20   /* 
8610: 44 61 74 61 20 74 79 70 65 20 6d 69 73 6d 61 74  Data type mismat
8620: 63 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ch */.#define SQ
8630: 4c 49 54 45 5f 4d 49 53 55 53 45 20 20 20 20 20  LITE_MISUSE     
8640: 20 32 31 20 20 20 2f 2a 20 4c 69 62 72 61 72 79   21   /* Library
8650: 20 75 73 65 64 20 69 6e 63 6f 72 72 65 63 74 6c   used incorrectl
8660: 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  y */.#define SQL
8670: 49 54 45 5f 4e 4f 4c 46 53 20 20 20 20 20 20 20  ITE_NOLFS       
8680: 32 32 20 20 20 2f 2a 20 55 73 65 73 20 4f 53 20  22   /* Uses OS 
8690: 66 65 61 74 75 72 65 73 20 6e 6f 74 20 73 75 70  features not sup
86a0: 70 6f 72 74 65 64 20 6f 6e 20 68 6f 73 74 20 2a  ported on host *
86b0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
86c0: 5f 41 55 54 48 20 20 20 20 20 20 20 20 32 33 20  _AUTH        23 
86d0: 20 20 2f 2a 20 41 75 74 68 6f 72 69 7a 61 74 69    /* Authorizati
86e0: 6f 6e 20 64 65 6e 69 65 64 20 2a 2f 0a 23 64 65  on denied */.#de
86f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 4f 52 4d  fine SQLITE_FORM
8700: 41 54 20 20 20 20 20 20 32 34 20 20 20 2f 2a 20  AT      24   /* 
8710: 41 75 78 69 6c 69 61 72 79 20 64 61 74 61 62 61  Auxiliary databa
8720: 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72 20  se format error 
8730: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
8740: 45 5f 52 41 4e 47 45 20 20 20 20 20 20 20 32 35  E_RANGE       25
8750: 20 20 20 2f 2a 20 32 6e 64 20 70 61 72 61 6d 65     /* 2nd parame
8760: 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 62  ter to sqlite3_b
8770: 69 6e 64 20 6f 75 74 20 6f 66 20 72 61 6e 67 65  ind out of range
8780: 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
8790: 54 45 5f 4e 4f 54 41 44 42 20 20 20 20 20 20 32  TE_NOTADB      2
87a0: 36 20 20 20 2f 2a 20 46 69 6c 65 20 6f 70 65 6e  6   /* File open
87b0: 65 64 20 74 68 61 74 20 69 73 20 6e 6f 74 20 61  ed that is not a
87c0: 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
87d0: 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
87e0: 5f 52 4f 57 20 20 20 20 20 20 20 20 20 31 30 30  _ROW         100
87f0: 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f 73 74 65    /* sqlite3_ste
8800: 70 28 29 20 68 61 73 20 61 6e 6f 74 68 65 72 20  p() has another 
8810: 72 6f 77 20 72 65 61 64 79 20 2a 2f 0a 23 64 65  row ready */.#de
8820: 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45  fine SQLITE_DONE
8830: 20 20 20 20 20 20 20 20 31 30 31 20 20 2f 2a 20          101  /* 
8840: 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 20 68  sqlite3_step() h
8850: 61 73 20 66 69 6e 69 73 68 65 64 20 65 78 65 63  as finished exec
8860: 75 74 69 6e 67 20 2a 2f 0a 2f 2a 20 65 6e 64 2d  uting */./* end-
8870: 6f 66 2d 65 72 72 6f 72 2d 63 6f 64 65 73 20 2a  of-error-codes *
8880: 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
8890: 46 3a 20 45 78 74 65 6e 64 65 64 20 52 65 73 75  F: Extended Resu
88a0: 6c 74 20 43 6f 64 65 73 20 7b 48 31 30 32 32 30  lt Codes {H10220
88b0: 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 20 4b 45  } <S10700>.** KE
88c0: 59 57 4f 52 44 53 3a 20 7b 65 78 74 65 6e 64 65  YWORDS: {extende
88d0: 64 20 65 72 72 6f 72 20 63 6f 64 65 7d 20 7b 65  d error code} {e
88e0: 78 74 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f  xtended error co
88f0: 64 65 73 7d 0a 2a 2a 20 4b 45 59 57 4f 52 44 53  des}.** KEYWORDS
8900: 3a 20 7b 65 78 74 65 6e 64 65 64 20 72 65 73 75  : {extended resu
8910: 6c 74 20 63 6f 64 65 7d 20 7b 65 78 74 65 6e 64  lt code} {extend
8920: 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 7d  ed result codes}
8930: 0a 2a 2a 0a 2a 2a 20 49 6e 20 69 74 73 20 64 65  .**.** In its de
8940: 66 61 75 6c 74 20 63 6f 6e 66 69 67 75 72 61 74  fault configurat
8950: 69 6f 6e 2c 20 53 51 4c 69 74 65 20 41 50 49 20  ion, SQLite API 
8960: 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
8970: 6f 6e 65 20 6f 66 20 32 36 20 69 6e 74 65 67 65  one of 26 intege
8980: 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b 20  r.** [SQLITE_OK 
8990: 7c 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 2e  | result codes].
89a0: 20 20 48 6f 77 65 76 65 72 2c 20 65 78 70 65 72    However, exper
89b0: 69 65 6e 63 65 20 68 61 73 20 73 68 6f 77 6e 20  ience has shown 
89c0: 74 68 61 74 20 6d 61 6e 79 20 6f 66 0a 2a 2a 20  that many of.** 
89d0: 74 68 65 73 65 20 72 65 73 75 6c 74 20 63 6f 64  these result cod
89e0: 65 73 20 61 72 65 20 74 6f 6f 20 63 6f 61 72 73  es are too coars
89f0: 65 2d 67 72 61 69 6e 65 64 2e 20 20 54 68 65 79  e-grained.  They
8a00: 20 64 6f 20 6e 6f 74 20 70 72 6f 76 69 64 65 20   do not provide 
8a10: 61 73 0a 2a 2a 20 6d 75 63 68 20 69 6e 66 6f 72  as.** much infor
8a20: 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 70 72 6f  mation about pro
8a30: 62 6c 65 6d 73 20 61 73 20 70 72 6f 67 72 61 6d  blems as program
8a40: 6d 65 72 73 20 6d 69 67 68 74 20 6c 69 6b 65 2e  mers might like.
8a50: 20 20 49 6e 20 61 6e 20 65 66 66 6f 72 74 20 74    In an effort t
8a60: 6f 0a 2a 2a 20 61 64 64 72 65 73 73 20 74 68 69  o.** address thi
8a70: 73 2c 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e  s, newer version
8a80: 73 20 6f 66 20 53 51 4c 69 74 65 20 28 76 65 72  s of SQLite (ver
8a90: 73 69 6f 6e 20 33 2e 33 2e 38 20 61 6e 64 20 6c  sion 3.3.8 and l
8aa0: 61 74 65 72 29 20 69 6e 63 6c 75 64 65 0a 2a 2a  ater) include.**
8ab0: 20 73 75 70 70 6f 72 74 20 66 6f 72 20 61 64 64   support for add
8ac0: 69 74 69 6f 6e 61 6c 20 72 65 73 75 6c 74 20 63  itional result c
8ad0: 6f 64 65 73 20 74 68 61 74 20 70 72 6f 76 69 64  odes that provid
8ae0: 65 20 6d 6f 72 65 20 64 65 74 61 69 6c 65 64 20  e more detailed 
8af0: 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61  information.** a
8b00: 62 6f 75 74 20 65 72 72 6f 72 73 2e 20 54 68 65  bout errors. The
8b10: 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
8b20: 20 63 6f 64 65 73 20 61 72 65 20 65 6e 61 62 6c   codes are enabl
8b30: 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a  ed or disabled.*
8b40: 2a 20 6f 6e 20 61 20 70 65 72 20 64 61 74 61 62  * on a per datab
8b50: 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 62  ase connection b
8b60: 61 73 69 73 20 75 73 69 6e 67 20 74 68 65 0a 2a  asis using the.*
8b70: 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e  * [sqlite3_exten
8b80: 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73  ded_result_codes
8b90: 28 29 5d 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 53  ()] API..**.** S
8ba0: 6f 6d 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c  ome of the avail
8bb0: 61 62 6c 65 20 65 78 74 65 6e 64 65 64 20 72 65  able extended re
8bc0: 73 75 6c 74 20 63 6f 64 65 73 20 61 72 65 20 6c  sult codes are l
8bd0: 69 73 74 65 64 20 68 65 72 65 2e 0a 2a 2a 20 4f  isted here..** O
8be0: 6e 65 20 6d 61 79 20 65 78 70 65 63 74 20 74 68  ne may expect th
8bf0: 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 78 74 65  e number of exte
8c00: 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65  nded result code
8c10: 73 20 77 69 6c 6c 20 62 65 20 65 78 70 61 6e 64  s will be expand
8c20: 0a 2a 2a 20 6f 76 65 72 20 74 69 6d 65 2e 20 20  .** over time.  
8c30: 53 6f 66 74 77 61 72 65 20 74 68 61 74 20 75 73  Software that us
8c40: 65 73 20 65 78 74 65 6e 64 65 64 20 72 65 73 75  es extended resu
8c50: 6c 74 20 63 6f 64 65 73 20 73 68 6f 75 6c 64 20  lt codes should 
8c60: 65 78 70 65 63 74 0a 2a 2a 20 74 6f 20 73 65 65  expect.** to see
8c70: 20 6e 65 77 20 72 65 73 75 6c 74 20 63 6f 64 65   new result code
8c80: 73 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65  s in future rele
8c90: 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a  ases of SQLite..
8ca0: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
8cb0: 5f 4f 4b 20 72 65 73 75 6c 74 20 63 6f 64 65 20  _OK result code 
8cc0: 77 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 65 78  will never be ex
8cd0: 74 65 6e 64 65 64 2e 20 20 49 74 20 77 69 6c 6c  tended.  It will
8ce0: 20 61 6c 77 61 79 73 0a 2a 2a 20 62 65 20 65 78   always.** be ex
8cf0: 61 63 74 6c 79 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a  actly zero..**.*
8d00: 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
8d10: 0a 2a 2a 20 7b 48 31 30 32 32 33 7d 20 54 68 65  .** {H10223} The
8d20: 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20 66   symbolic name f
8d30: 6f 72 20 61 6e 20 65 78 74 65 6e 64 65 64 20 72  or an extended r
8d40: 65 73 75 6c 74 20 63 6f 64 65 20 73 68 61 6c 6c  esult code shall
8d50: 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 20 20 20   contains.**    
8d60: 20 20 20 20 20 20 61 20 72 65 6c 61 74 65 64 20        a related 
8d70: 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63  primary result c
8d80: 6f 64 65 20 61 73 20 61 20 70 72 65 66 69 78 2e  ode as a prefix.
8d90: 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 34 7d 20  .**.** {H10224} 
8da0: 50 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20 63  Primary result c
8db0: 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c 6c 20  ode names shall 
8dc0: 63 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65  contain a single
8dd0: 20 22 5f 22 20 63 68 61 72 61 63 74 65 72 2e 0a   "_" character..
8de0: 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32 35 7d 20 45  **.** {H10225} E
8df0: 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63  xtended result c
8e00: 6f 64 65 20 6e 61 6d 65 73 20 73 68 61 6c 6c 20  ode names shall 
8e10: 63 6f 6e 74 61 69 6e 20 74 77 6f 20 6f 72 20 6d  contain two or m
8e20: 6f 72 65 20 22 5f 22 20 63 68 61 72 61 63 74 65  ore "_" characte
8e30: 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 32 32  rs..**.** {H1022
8e40: 36 7d 20 54 68 65 20 6e 75 6d 65 72 69 63 20 76  6} The numeric v
8e50: 61 6c 75 65 20 6f 66 20 61 6e 20 65 78 74 65 6e  alue of an exten
8e60: 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 20  ded result code 
8e70: 73 68 61 6c 6c 20 63 6f 6e 74 61 69 6e 20 74 68  shall contain th
8e80: 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75  e.**          nu
8e90: 6d 65 72 69 63 20 76 61 6c 75 65 20 6f 66 20 69  meric value of i
8ea0: 74 73 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  ts corresponding
8eb0: 20 70 72 69 6d 61 72 79 20 72 65 73 75 6c 74 20   primary result 
8ec0: 63 6f 64 65 20 69 6e 0a 2a 2a 20 20 20 20 20 20  code in.**      
8ed0: 20 20 20 20 69 74 73 20 6c 65 61 73 74 20 73 69      its least si
8ee0: 67 6e 69 66 69 63 61 6e 74 20 38 20 62 69 74 73  gnificant 8 bits
8ef0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
8f00: 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 20 20  ITE_IOERR_READ  
8f10: 20 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c              (SQL
8f20: 49 54 45 5f 49 4f 45 52 52 20 7c 20 28 31 3c 3c  ITE_IOERR | (1<<
8f30: 38 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  8)).#define SQLI
8f40: 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52  TE_IOERR_SHORT_R
8f50: 45 41 44 20 20 20 20 20 20 20 20 28 53 51 4c 49  EAD        (SQLI
8f60: 54 45 5f 49 4f 45 52 52 20 7c 20 28 32 3c 3c 38  TE_IOERR | (2<<8
8f70: 29 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  )).#define SQLIT
8f80: 45 5f 49 4f 45 52 52 5f 57 52 49 54 45 20 20 20  E_IOERR_WRITE   
8f90: 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54            (SQLIT
8fa0: 45 5f 49 4f 45 52 52 20 7c 20 28 33 3c 3c 38 29  E_IOERR | (3<<8)
8fb0: 29 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ).#define SQLITE
8fc0: 5f 49 4f 45 52 52 5f 46 53 59 4e 43 20 20 20 20  _IOERR_FSYNC    
8fd0: 20 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45           (SQLITE
8fe0: 5f 49 4f 45 52 52 20 7c 20 28 34 3c 3c 38 29 29  _IOERR | (4<<8))
8ff0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9000: 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e 43 20  IOERR_DIR_FSYNC 
9010: 20 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f          (SQLITE_
9020: 49 4f 45 52 52 20 7c 20 28 35 3c 3c 38 29 29 0a  IOERR | (5<<8)).
9030: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
9040: 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 20 20 20  OERR_TRUNCATE   
9050: 20 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49         (SQLITE_I
9060: 4f 45 52 52 20 7c 20 28 36 3c 3c 38 29 29 0a 23  OERR | (6<<8)).#
9070: 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f  define SQLITE_IO
9080: 45 52 52 5f 46 53 54 41 54 20 20 20 20 20 20 20  ERR_FSTAT       
9090: 20 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f        (SQLITE_IO
90a0: 45 52 52 20 7c 20 28 37 3c 3c 38 29 29 0a 23 64  ERR | (7<<8)).#d
90b0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45  efine SQLITE_IOE
90c0: 52 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20  RR_UNLOCK       
90d0: 20 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45       (SQLITE_IOE
90e0: 52 52 20 7c 20 28 38 3c 3c 38 29 29 0a 23 64 65  RR | (8<<8)).#de
90f0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52  fine SQLITE_IOER
9100: 52 5f 52 44 4c 4f 43 4b 20 20 20 20 20 20 20 20  R_RDLOCK        
9110: 20 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52      (SQLITE_IOER
9120: 52 20 7c 20 28 39 3c 3c 38 29 29 0a 23 64 65 66  R | (9<<8)).#def
9130: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9140: 5f 44 45 4c 45 54 45 20 20 20 20 20 20 20 20 20  _DELETE         
9150: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
9160: 20 7c 20 28 31 30 3c 3c 38 29 29 0a 23 64 65 66   | (10<<8)).#def
9170: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9180: 5f 42 4c 4f 43 4b 45 44 20 20 20 20 20 20 20 20  _BLOCKED        
9190: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
91a0: 20 7c 20 28 31 31 3c 3c 38 29 29 0a 23 64 65 66   | (11<<8)).#def
91b0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
91c0: 5f 4e 4f 4d 45 4d 20 20 20 20 20 20 20 20 20 20  _NOMEM          
91d0: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
91e0: 20 7c 20 28 31 32 3c 3c 38 29 29 0a 23 64 65 66   | (12<<8)).#def
91f0: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9200: 5f 41 43 43 45 53 53 20 20 20 20 20 20 20 20 20  _ACCESS         
9210: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
9220: 20 7c 20 28 31 33 3c 3c 38 29 29 0a 23 64 65 66   | (13<<8)).#def
9230: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9240: 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f  _CHECKRESERVEDLO
9250: 43 4b 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52  CK (SQLITE_IOERR
9260: 20 7c 20 28 31 34 3c 3c 38 29 29 0a 23 64 65 66   | (14<<8)).#def
9270: 69 6e 65 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  ine SQLITE_IOERR
9280: 5f 4c 4f 43 4b 20 20 20 20 20 20 20 20 20 20 20  _LOCK           
9290: 20 20 20 28 53 51 4c 49 54 45 5f 49 4f 45 52 52     (SQLITE_IOERR
92a0: 20 7c 20 28 31 35 3c 3c 38 29 29 0a 0a 2f 2a 0a   | (15<<8))../*.
92b0: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6c 61  ** CAPI3REF: Fla
92c0: 67 73 20 46 6f 72 20 46 69 6c 65 20 4f 70 65 6e  gs For File Open
92d0: 20 4f 70 65 72 61 74 69 6f 6e 73 20 7b 48 31 30   Operations {H10
92e0: 32 33 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48  230} <H11120> <H
92f0: 31 32 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  12700>.**.** The
9300: 73 65 20 62 69 74 20 76 61 6c 75 65 73 20 61 72  se bit values ar
9310: 65 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75  e intended for u
9320: 73 65 20 69 6e 20 74 68 65 0a 2a 2a 20 33 72 64  se in the.** 3rd
9330: 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
9340: 65 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  e [sqlite3_open_
9350: 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  v2()] interface 
9360: 61 6e 64 0a 2a 2a 20 69 6e 20 74 68 65 20 34 74  and.** in the 4t
9370: 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
9380: 68 65 20 78 4f 70 65 6e 20 6d 65 74 68 6f 64 20  he xOpen method 
9390: 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74  of the.** [sqlit
93a0: 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 2e 0a  e3_vfs] object..
93b0: 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
93c0: 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 20  E_OPEN_READONLY 
93d0: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30          0x000000
93e0: 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  01.#define SQLIT
93f0: 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
9400: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30          0x000000
9410: 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  02.#define SQLIT
9420: 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20 20 20  E_OPEN_CREATE   
9430: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30          0x000000
9440: 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  04.#define SQLIT
9450: 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43  E_OPEN_DELETEONC
9460: 4c 4f 53 45 20 20 20 20 30 78 30 30 30 30 30 30  LOSE    0x000000
9470: 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  08.#define SQLIT
9480: 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
9490: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 30          0x000000
94a0: 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  10.#define SQLIT
94b0: 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20 20  E_OPEN_MAIN_DB  
94c0: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 31          0x000001
94d0: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
94e0: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 44 42 20 20  E_OPEN_TEMP_DB  
94f0: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 30 32          0x000002
9500: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
9510: 45 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54  E_OPEN_TRANSIENT
9520: 5f 44 42 20 20 20 20 20 30 78 30 30 30 30 30 34  _DB     0x000004
9530: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
9540: 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
9550: 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 30 38  NAL     0x000008
9560: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
9570: 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a 4f 55 52  E_OPEN_TEMP_JOUR
9580: 4e 41 4c 20 20 20 20 20 30 78 30 30 30 30 31 30  NAL     0x000010
9590: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
95a0: 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
95b0: 4c 20 20 20 20 20 20 20 30 78 30 30 30 30 32 30  L       0x000020
95c0: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
95d0: 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
95e0: 55 52 4e 41 4c 20 20 20 30 78 30 30 30 30 34 30  URNAL   0x000040
95f0: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
9600: 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 20 20  E_OPEN_NOMUTEX  
9610: 20 20 20 20 20 20 20 20 30 78 30 30 30 30 38 30          0x000080
9620: 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  00.#define SQLIT
9630: 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58  E_OPEN_FULLMUTEX
9640: 20 20 20 20 20 20 20 20 30 78 30 30 30 31 30 30          0x000100
9650: 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  00../*.** CAPI3R
9660: 45 46 3a 20 44 65 76 69 63 65 20 43 68 61 72 61  EF: Device Chara
9670: 63 74 65 72 69 73 74 69 63 73 20 7b 48 31 30 32  cteristics {H102
9680: 34 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a 2a 0a  40} <H11120>.**.
9690: 2a 2a 20 54 68 65 20 78 44 65 76 69 63 65 43 61  ** The xDeviceCa
96a0: 70 61 62 69 6c 69 74 69 65 73 20 6d 65 74 68 6f  pabilities metho
96b0: 64 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65  d of the [sqlite
96c0: 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a  3_io_methods].**
96d0: 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e 73 20   object returns 
96e0: 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69 63 68  an integer which
96f0: 20 69 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20   is a vector of 
9700: 74 68 65 20 74 68 65 73 65 0a 2a 2a 20 62 69 74  the these.** bit
9710: 20 76 61 6c 75 65 73 20 65 78 70 72 65 73 73 69   values expressi
9720: 6e 67 20 49 2f 4f 20 63 68 61 72 61 63 74 65 72  ng I/O character
9730: 69 73 74 69 63 73 20 6f 66 20 74 68 65 20 6d 61  istics of the ma
9740: 73 73 20 73 74 6f 72 61 67 65 0a 2a 2a 20 64 65  ss storage.** de
9750: 76 69 63 65 20 74 68 61 74 20 68 6f 6c 64 73 20  vice that holds 
9760: 74 68 65 20 66 69 6c 65 20 74 68 61 74 20 74 68  the file that th
9770: 65 20 5b 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65  e [sqlite3_io_me
9780: 74 68 6f 64 73 5d 0a 2a 2a 20 72 65 66 65 72 73  thods].** refers
9790: 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53   to..**.** The S
97a0: 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d  QLITE_IOCAP_ATOM
97b0: 49 43 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e  IC property mean
97c0: 73 20 74 68 61 74 20 61 6c 6c 20 77 72 69 74 65  s that all write
97d0: 73 20 6f 66 0a 2a 2a 20 61 6e 79 20 73 69 7a 65  s of.** any size
97e0: 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68   are atomic.  Th
97f0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 41  e SQLITE_IOCAP_A
9800: 54 4f 4d 49 43 6e 6e 6e 20 76 61 6c 75 65 73 0a  TOMICnnn values.
9810: 2a 2a 20 6d 65 61 6e 20 74 68 61 74 20 77 72 69  ** mean that wri
9820: 74 65 73 20 6f 66 20 62 6c 6f 63 6b 73 20 74 68  tes of blocks th
9830: 61 74 20 61 72 65 20 6e 6e 6e 20 62 79 74 65 73  at are nnn bytes
9840: 20 69 6e 20 73 69 7a 65 20 61 6e 64 0a 2a 2a 20   in size and.** 
9850: 61 72 65 20 61 6c 69 67 6e 65 64 20 74 6f 20 61  are aligned to a
9860: 6e 20 61 64 64 72 65 73 73 20 77 68 69 63 68 20  n address which 
9870: 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6d 75  is an integer mu
9880: 6c 74 69 70 6c 65 20 6f 66 0a 2a 2a 20 6e 6e 6e  ltiple of.** nnn
9890: 20 61 72 65 20 61 74 6f 6d 69 63 2e 20 20 54 68   are atomic.  Th
98a0: 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  e SQLITE_IOCAP_S
98b0: 41 46 45 5f 41 50 50 45 4e 44 20 76 61 6c 75 65  AFE_APPEND value
98c0: 20 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 77   means.** that w
98d0: 68 65 6e 20 64 61 74 61 20 69 73 20 61 70 70 65  hen data is appe
98e0: 6e 64 65 64 20 74 6f 20 61 20 66 69 6c 65 2c 20  nded to a file, 
98f0: 74 68 65 20 64 61 74 61 20 69 73 20 61 70 70 65  the data is appe
9900: 6e 64 65 64 0a 2a 2a 20 66 69 72 73 74 20 74 68  nded.** first th
9910: 65 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  en the size of t
9920: 68 65 20 66 69 6c 65 20 69 73 20 65 78 74 65 6e  he file is exten
9930: 64 65 64 2c 20 6e 65 76 65 72 20 74 68 65 20 6f  ded, never the o
9940: 74 68 65 72 0a 2a 2a 20 77 61 79 20 61 72 6f 75  ther.** way arou
9950: 6e 64 2e 20 20 54 68 65 20 53 51 4c 49 54 45 5f  nd.  The SQLITE_
9960: 49 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c  IOCAP_SEQUENTIAL
9970: 20 70 72 6f 70 65 72 74 79 20 6d 65 61 6e 73 20   property means 
9980: 74 68 61 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74  that.** informat
9990: 69 6f 6e 20 69 73 20 77 72 69 74 74 65 6e 20 74  ion is written t
99a0: 6f 20 64 69 73 6b 20 69 6e 20 74 68 65 20 73 61  o disk in the sa
99b0: 6d 65 20 6f 72 64 65 72 20 61 73 20 63 61 6c 6c  me order as call
99c0: 73 0a 2a 2a 20 74 6f 20 78 57 72 69 74 65 28 29  s.** to xWrite()
99d0: 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
99e0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
99f0: 20 20 20 20 20 20 20 20 20 20 30 78 30 30 30 30            0x0000
9a00: 30 30 30 31 0a 23 64 65 66 69 6e 65 20 53 51 4c  0001.#define SQL
9a10: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9a20: 35 31 32 20 20 20 20 20 20 20 30 78 30 30 30 30  512       0x0000
9a30: 30 30 30 32 0a 23 64 65 66 69 6e 65 20 53 51 4c  0002.#define SQL
9a40: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9a50: 31 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30  1K        0x0000
9a60: 30 30 30 34 0a 23 64 65 66 69 6e 65 20 53 51 4c  0004.#define SQL
9a70: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9a80: 32 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30  2K        0x0000
9a90: 30 30 30 38 0a 23 64 65 66 69 6e 65 20 53 51 4c  0008.#define SQL
9aa0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9ab0: 34 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30  4K        0x0000
9ac0: 30 30 31 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0010.#define SQL
9ad0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9ae0: 38 4b 20 20 20 20 20 20 20 20 30 78 30 30 30 30  8K        0x0000
9af0: 30 30 32 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0020.#define SQL
9b00: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9b10: 31 36 4b 20 20 20 20 20 20 20 30 78 30 30 30 30  16K       0x0000
9b20: 30 30 34 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0040.#define SQL
9b30: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9b40: 33 32 4b 20 20 20 20 20 20 20 30 78 30 30 30 30  32K       0x0000
9b50: 30 30 38 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0080.#define SQL
9b60: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
9b70: 36 34 4b 20 20 20 20 20 20 20 30 78 30 30 30 30  64K       0x0000
9b80: 30 31 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0100.#define SQL
9b90: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
9ba0: 50 50 45 4e 44 20 20 20 20 20 30 78 30 30 30 30  PPEND     0x0000
9bb0: 30 32 30 30 0a 23 64 65 66 69 6e 65 20 53 51 4c  0200.#define SQL
9bc0: 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45 4e  ITE_IOCAP_SEQUEN
9bd0: 54 49 41 4c 20 20 20 20 20 20 30 78 30 30 30 30  TIAL      0x0000
9be0: 30 34 30 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  0400../*.** CAPI
9bf0: 33 52 45 46 3a 20 46 69 6c 65 20 4c 6f 63 6b 69  3REF: File Locki
9c00: 6e 67 20 4c 65 76 65 6c 73 20 7b 48 31 30 32 35  ng Levels {H1025
9c10: 30 7d 20 3c 48 31 31 31 32 30 3e 20 3c 48 31 31  0} <H11120> <H11
9c20: 33 31 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  310>.**.** SQLit
9c30: 65 20 75 73 65 73 20 6f 6e 65 20 6f 66 20 74 68  e uses one of th
9c40: 65 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75  ese integer valu
9c50: 65 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64  es as the second
9c60: 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  .** argument to 
9c70: 63 61 6c 6c 73 20 69 74 20 6d 61 6b 65 73 20 74  calls it makes t
9c80: 6f 20 74 68 65 20 78 4c 6f 63 6b 28 29 20 61 6e  o the xLock() an
9c90: 64 20 78 55 6e 6c 6f 63 6b 28 29 20 6d 65 74 68  d xUnlock() meth
9ca0: 6f 64 73 0a 2a 2a 20 6f 66 20 61 6e 20 5b 73 71  ods.** of an [sq
9cb0: 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
9cc0: 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 23 64 65  ] object..*/.#de
9cd0: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
9ce0: 5f 4e 4f 4e 45 20 20 20 20 20 20 20 20 20 20 30  _NONE          0
9cf0: 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
9d00: 4c 4f 43 4b 5f 53 48 41 52 45 44 20 20 20 20 20  LOCK_SHARED     
9d10: 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51 4c     1.#define SQL
9d20: 49 54 45 5f 4c 4f 43 4b 5f 52 45 53 45 52 56 45  ITE_LOCK_RESERVE
9d30: 44 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65  D      2.#define
9d40: 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e   SQLITE_LOCK_PEN
9d50: 44 49 4e 47 20 20 20 20 20 20 20 33 0a 23 64 65  DING       3.#de
9d60: 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 4f 43 4b  fine SQLITE_LOCK
9d70: 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20 34  _EXCLUSIVE     4
9d80: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
9d90: 3a 20 53 79 6e 63 68 72 6f 6e 69 7a 61 74 69 6f  : Synchronizatio
9da0: 6e 20 54 79 70 65 20 46 6c 61 67 73 20 7b 48 31  n Type Flags {H1
9db0: 30 32 36 30 7d 20 3c 48 31 31 31 32 30 3e 0a 2a  0260} <H11120>.*
9dc0: 2a 0a 2a 2a 20 57 68 65 6e 20 53 51 4c 69 74 65  *.** When SQLite
9dd0: 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 78 53 79   invokes the xSy
9de0: 6e 63 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 61  nc() method of a
9df0: 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f  n.** [sqlite3_io
9e00: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
9e10: 20 69 74 20 75 73 65 73 20 61 20 63 6f 6d 62 69   it uses a combi
9e20: 6e 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65  nation of.** the
9e30: 73 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  se integer value
9e40: 73 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  s as the second 
9e50: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
9e60: 57 68 65 6e 20 74 68 65 20 53 51 4c 49 54 45 5f  When the SQLITE_
9e70: 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59 20 66 6c  SYNC_DATAONLY fl
9e80: 61 67 20 69 73 20 75 73 65 64 2c 20 69 74 20 6d  ag is used, it m
9e90: 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a  eans that the.**
9ea0: 20 73 79 6e 63 20 6f 70 65 72 61 74 69 6f 6e 20   sync operation 
9eb0: 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20 66 6c  only needs to fl
9ec0: 75 73 68 20 64 61 74 61 20 74 6f 20 6d 61 73 73  ush data to mass
9ed0: 20 73 74 6f 72 61 67 65 2e 20 20 49 6e 6f 64 65   storage.  Inode
9ee0: 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
9ef0: 6e 65 65 64 20 6e 6f 74 20 62 65 20 66 6c 75 73  need not be flus
9f00: 68 65 64 2e 20 54 68 65 20 53 51 4c 49 54 45 5f  hed. The SQLITE_
9f10: 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 66 6c 61 67  SYNC_NORMAL flag
9f20: 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f 20 75 73 65   means.** to use
9f30: 20 6e 6f 72 6d 61 6c 20 66 73 79 6e 63 28 29 20   normal fsync() 
9f40: 73 65 6d 61 6e 74 69 63 73 2e 20 54 68 65 20 53  semantics. The S
9f50: 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 20  QLITE_SYNC_FULL 
9f60: 66 6c 61 67 20 6d 65 61 6e 73 0a 2a 2a 20 74 6f  flag means.** to
9f70: 20 75 73 65 20 4d 61 63 20 4f 53 20 58 20 73 74   use Mac OS X st
9f80: 79 6c 65 20 66 75 6c 6c 73 79 6e 63 20 69 6e 73  yle fullsync ins
9f90: 74 65 61 64 20 6f 66 20 66 73 79 6e 63 28 29 2e  tead of fsync().
9fa0: 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
9fb0: 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 20 20  TE_SYNC_NORMAL  
9fc0: 20 20 20 20 20 20 30 78 30 30 30 30 32 0a 23 64        0x00002.#d
9fd0: 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 59 4e  efine SQLITE_SYN
9fe0: 43 5f 46 55 4c 4c 20 20 20 20 20 20 20 20 20 20  C_FULL          
9ff0: 30 78 30 30 30 30 33 0a 23 64 65 66 69 6e 65 20  0x00003.#define 
a000: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
a010: 4f 4e 4c 59 20 20 20 20 20 20 30 78 30 30 30 31  ONLY      0x0001
a020: 30 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  0../*.** CAPI3RE
a030: 46 3a 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20  F: OS Interface 
a040: 4f 70 65 6e 20 46 69 6c 65 20 48 61 6e 64 6c 65  Open File Handle
a050: 20 7b 48 31 31 31 31 30 7d 20 3c 53 32 30 31 31   {H11110} <S2011
a060: 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20 5b 73 71 6c  0>.**.** An [sql
a070: 69 74 65 33 5f 66 69 6c 65 5d 20 6f 62 6a 65 63  ite3_file] objec
a080: 74 20 72 65 70 72 65 73 65 6e 74 73 20 61 6e 20  t represents an 
a090: 6f 70 65 6e 20 66 69 6c 65 20 69 6e 20 74 68 65  open file in the
a0a0: 20 4f 53 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65   OS.** interface
a0b0: 20 6c 61 79 65 72 2e 20 20 49 6e 64 69 76 69 64   layer.  Individ
a0c0: 75 61 6c 20 4f 53 20 69 6e 74 65 72 66 61 63 65  ual OS interface
a0d0: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
a0e0: 20 77 69 6c 6c 0a 2a 2a 20 77 61 6e 74 20 74 6f   will.** want to
a0f0: 20 73 75 62 63 6c 61 73 73 20 74 68 69 73 20 6f   subclass this o
a100: 62 6a 65 63 74 20 62 79 20 61 70 70 65 6e 64 69  bject by appendi
a110: 6e 67 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 69  ng additional fi
a120: 65 6c 64 73 0a 2a 2a 20 66 6f 72 20 74 68 65 69  elds.** for thei
a130: 72 20 6f 77 6e 20 75 73 65 2e 20 20 54 68 65 20  r own use.  The 
a140: 70 4d 65 74 68 6f 64 73 20 65 6e 74 72 79 20 69  pMethods entry i
a150: 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
a160: 6e 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6f  n.** [sqlite3_io
a170: 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74  _methods] object
a180: 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 6d 65   that defines me
a190: 74 68 6f 64 73 20 66 6f 72 20 70 65 72 66 6f 72  thods for perfor
a1a0: 6d 69 6e 67 0a 2a 2a 20 49 2f 4f 20 6f 70 65 72  ming.** I/O oper
a1b0: 61 74 69 6f 6e 73 20 6f 6e 20 74 68 65 20 6f 70  ations on the op
a1c0: 65 6e 20 66 69 6c 65 2e 0a 2a 2f 0a 74 79 70 65  en file..*/.type
a1d0: 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
a1e0: 65 33 5f 66 69 6c 65 20 73 71 6c 69 74 65 33 5f  e3_file sqlite3_
a1f0: 66 69 6c 65 3b 0a 73 74 72 75 63 74 20 73 71 6c  file;.struct sql
a200: 69 74 65 33 5f 66 69 6c 65 20 7b 0a 20 20 63 6f  ite3_file {.  co
a210: 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69 74  nst struct sqlit
a220: 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70  e3_io_methods *p
a230: 4d 65 74 68 6f 64 73 3b 20 20 2f 2a 20 4d 65 74  Methods;  /* Met
a240: 68 6f 64 73 20 66 6f 72 20 61 6e 20 6f 70 65 6e  hods for an open
a250: 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a   file */.};../*.
a260: 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 53 20  ** CAPI3REF: OS 
a270: 49 6e 74 65 72 66 61 63 65 20 46 69 6c 65 20 56  Interface File V
a280: 69 72 74 75 61 6c 20 4d 65 74 68 6f 64 73 20 4f  irtual Methods O
a290: 62 6a 65 63 74 20 7b 48 31 31 31 32 30 7d 20 3c  bject {H11120} <
a2a0: 53 32 30 31 31 30 3e 0a 2a 2a 0a 2a 2a 20 45 76  S20110>.**.** Ev
a2b0: 65 72 79 20 66 69 6c 65 20 6f 70 65 6e 65 64 20  ery file opened 
a2c0: 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  by the [sqlite3_
a2d0: 76 66 73 5d 20 78 4f 70 65 6e 20 6d 65 74 68 6f  vfs] xOpen metho
a2e0: 64 20 70 6f 70 75 6c 61 74 65 73 20 61 6e 0a 2a  d populates an.*
a2f0: 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d  * [sqlite3_file]
a300: 20 6f 62 6a 65 63 74 20 28 6f 72 2c 20 6d 6f 72   object (or, mor
a310: 65 20 63 6f 6d 6d 6f 6e 6c 79 2c 20 61 20 73 75  e commonly, a su
a320: 62 63 6c 61 73 73 20 6f 66 20 74 68 65 0a 2a 2a  bclass of the.**
a330: 20 5b 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20   [sqlite3_file] 
a340: 6f 62 6a 65 63 74 29 20 77 69 74 68 20 61 20 70  object) with a p
a350: 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73  ointer to an ins
a360: 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
a370: 6a 65 63 74 2e 0a 2a 2a 20 54 68 69 73 20 6f 62  ject..** This ob
a380: 6a 65 63 74 20 64 65 66 69 6e 65 73 20 74 68 65  ject defines the
a390: 20 6d 65 74 68 6f 64 73 20 75 73 65 64 20 74 6f   methods used to
a3a0: 20 70 65 72 66 6f 72 6d 20 76 61 72 69 6f 75 73   perform various
a3b0: 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a 2a 20 61   operations.** a
a3c0: 67 61 69 6e 73 74 20 74 68 65 20 6f 70 65 6e 20  gainst the open 
a3d0: 66 69 6c 65 20 72 65 70 72 65 73 65 6e 74 65 64  file represented
a3e0: 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33   by the [sqlite3
a3f0: 5f 66 69 6c 65 5d 20 6f 62 6a 65 63 74 2e 0a 2a  _file] object..*
a400: 2a 0a 2a 2a 20 54 68 65 20 66 6c 61 67 73 20 61  *.** The flags a
a410: 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 79 6e 63  rgument to xSync
a420: 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66 20 5b   may be one of [
a430: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d  SQLITE_SYNC_NORM
a440: 41 4c 5d 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54  AL] or.** [SQLIT
a450: 45 5f 53 59 4e 43 5f 46 55 4c 4c 5d 2e 20 20 54  E_SYNC_FULL].  T
a460: 68 65 20 66 69 72 73 74 20 63 68 6f 69 63 65 20  he first choice 
a470: 69 73 20 74 68 65 20 6e 6f 72 6d 61 6c 20 66 73  is the normal fs
a480: 79 6e 63 28 29 2e 0a 2a 2a 20 54 68 65 20 73 65  ync()..** The se
a490: 63 6f 6e 64 20 63 68 6f 69 63 65 20 69 73 20 61  cond choice is a
a4a0: 20 4d 61 63 20 4f 53 20 58 20 73 74 79 6c 65 20   Mac OS X style 
a4b0: 66 75 6c 6c 73 79 6e 63 2e 20 20 54 68 65 20 5b  fullsync.  The [
a4c0: 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54 41  SQLITE_SYNC_DATA
a4d0: 4f 4e 4c 59 5d 0a 2a 2a 20 66 6c 61 67 20 6d 61  ONLY].** flag ma
a4e0: 79 20 62 65 20 4f 52 65 64 20 69 6e 20 74 6f 20  y be ORed in to 
a4f0: 69 6e 64 69 63 61 74 65 20 74 68 61 74 20 6f 6e  indicate that on
a500: 6c 79 20 74 68 65 20 64 61 74 61 20 6f 66 20 74  ly the data of t
a510: 68 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 6e  he file.** and n
a520: 6f 74 20 69 74 73 20 69 6e 6f 64 65 20 6e 65 65  ot its inode nee
a530: 64 73 20 74 6f 20 62 65 20 73 79 6e 63 65 64 2e  ds to be synced.
a540: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 74 65 67  .**.** The integ
a550: 65 72 20 76 61 6c 75 65 73 20 74 6f 20 78 4c 6f  er values to xLo
a560: 63 6b 28 29 20 61 6e 64 20 78 55 6e 6c 6f 63 6b  ck() and xUnlock
a570: 28 29 20 61 72 65 20 6f 6e 65 20 6f 66 0a 2a 2a  () are one of.**
a580: 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53   <ul>.** <li> [S
a590: 51 4c 49 54 45 5f 4c 4f 43 4b 5f 4e 4f 4e 45 5d  QLITE_LOCK_NONE]
a5a0: 2c 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ,.** <li> [SQLIT
a5b0: 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c 0a  E_LOCK_SHARED],.
a5c0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a5d0: 4c 4f 43 4b 5f 52 45 53 45 52 56 45 44 5d 2c 0a  LOCK_RESERVED],.
a5e0: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
a5f0: 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47 5d 2c 20 6f  LOCK_PENDING], o
a600: 72 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  r.** <li> [SQLIT
a610: 45 5f 4c 4f 43 4b 5f 45 58 43 4c 55 53 49 56 45  E_LOCK_EXCLUSIVE
a620: 5d 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 20 78  ]..** </ul>.** x
a630: 4c 6f 63 6b 28 29 20 69 6e 63 72 65 61 73 65 73  Lock() increases
a640: 20 74 68 65 20 6c 6f 63 6b 2e 20 78 55 6e 6c 6f   the lock. xUnlo
a650: 63 6b 28 29 20 64 65 63 72 65 61 73 65 73 20 74  ck() decreases t
a660: 68 65 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 65 20  he lock..** The 
a670: 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
a680: 63 6b 28 29 20 6d 65 74 68 6f 64 20 63 68 65 63  ck() method chec
a690: 6b 73 20 77 68 65 74 68 65 72 20 61 6e 79 20 64  ks whether any d
a6a0: 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
a6b0: 6f 6e 2c 0a 2a 2a 20 65 69 74 68 65 72 20 69 6e  on,.** either in
a6c0: 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 6f 72   this process or
a6d0: 20 69 6e 20 73 6f 6d 65 20 6f 74 68 65 72 20 70   in some other p
a6e0: 72 6f 63 65 73 73 2c 20 69 73 20 68 6f 6c 64 69  rocess, is holdi
a6f0: 6e 67 20 61 20 52 45 53 45 52 56 45 44 2c 0a 2a  ng a RESERVED,.*
a700: 2a 20 50 45 4e 44 49 4e 47 2c 20 6f 72 20 45 58  * PENDING, or EX
a710: 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20  CLUSIVE lock on 
a720: 74 68 65 20 66 69 6c 65 2e 20 20 49 74 20 72 65  the file.  It re
a730: 74 75 72 6e 73 20 74 72 75 65 0a 2a 2a 20 69 66  turns true.** if
a740: 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 65 78 69   such a lock exi
a750: 73 74 73 20 61 6e 64 20 66 61 6c 73 65 20 6f 74  sts and false ot
a760: 68 65 72 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  herwise..**.** T
a770: 68 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  he xFileControl(
a780: 29 20 6d 65 74 68 6f 64 20 69 73 20 61 20 67 65  ) method is a ge
a790: 6e 65 72 69 63 20 69 6e 74 65 72 66 61 63 65 20  neric interface 
a7a0: 74 68 61 74 20 61 6c 6c 6f 77 73 20 63 75 73 74  that allows cust
a7b0: 6f 6d 0a 2a 2a 20 56 46 53 20 69 6d 70 6c 65 6d  om.** VFS implem
a7c0: 65 6e 74 61 74 69 6f 6e 73 20 74 6f 20 64 69 72  entations to dir
a7d0: 65 63 74 6c 79 20 63 6f 6e 74 72 6f 6c 20 61 6e  ectly control an
a7e0: 20 6f 70 65 6e 20 66 69 6c 65 20 75 73 69 6e 67   open file using
a7f0: 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   the.** [sqlite3
a800: 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 29 5d  _file_control()]
a810: 20 69 6e 74 65 72 66 61 63 65 2e 20 20 54 68 65   interface.  The
a820: 20 73 65 63 6f 6e 64 20 22 6f 70 22 20 61 72 67   second "op" arg
a830: 75 6d 65 6e 74 20 69 73 20 61 6e 0a 2a 2a 20 69  ument is an.** i
a840: 6e 74 65 67 65 72 20 6f 70 63 6f 64 65 2e 20 20  nteger opcode.  
a850: 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  The third argume
a860: 6e 74 20 69 73 20 61 20 67 65 6e 65 72 69 63 20  nt is a generic 
a870: 70 6f 69 6e 74 65 72 20 69 6e 74 65 6e 64 65 64  pointer intended
a880: 20 74 6f 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20   to.** point to 
a890: 61 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  a structure that
a8a0: 20 6d 61 79 20 63 6f 6e 74 61 69 6e 20 61 72 67   may contain arg
a8b0: 75 6d 65 6e 74 73 20 6f 72 20 73 70 61 63 65 20  uments or space 
a8c0: 69 6e 20 77 68 69 63 68 20 74 6f 0a 2a 2a 20 77  in which to.** w
a8d0: 72 69 74 65 20 72 65 74 75 72 6e 20 76 61 6c 75  rite return valu
a8e0: 65 73 2e 20 20 50 6f 74 65 6e 74 69 61 6c 20 75  es.  Potential u
a8f0: 73 65 73 20 66 6f 72 20 78 46 69 6c 65 43 6f 6e  ses for xFileCon
a900: 74 72 6f 6c 28 29 20 6d 69 67 68 74 20 62 65 0a  trol() might be.
a910: 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  ** functions to 
a920: 65 6e 61 62 6c 65 20 62 6c 6f 63 6b 69 6e 67 20  enable blocking 
a930: 6c 6f 63 6b 73 20 77 69 74 68 20 74 69 6d 65 6f  locks with timeo
a940: 75 74 73 2c 20 74 6f 20 63 68 61 6e 67 65 20 74  uts, to change t
a950: 68 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 73 74  he.** locking st
a960: 72 61 74 65 67 79 20 28 66 6f 72 20 65 78 61 6d  rategy (for exam
a970: 70 6c 65 20 74 6f 20 75 73 65 20 64 6f 74 2d 66  ple to use dot-f
a980: 69 6c 65 20 6c 6f 63 6b 73 29 2c 20 74 6f 20 69  ile locks), to i
a990: 6e 71 75 69 72 65 0a 2a 2a 20 61 62 6f 75 74 20  nquire.** about 
a9a0: 74 68 65 20 73 74 61 74 75 73 20 6f 66 20 61 20  the status of a 
a9b0: 6c 6f 63 6b 2c 20 6f 72 20 74 6f 20 62 72 65 61  lock, or to brea
a9c0: 6b 20 73 74 61 6c 65 20 6c 6f 63 6b 73 2e 20 20  k stale locks.  
a9d0: 54 68 65 20 53 51 4c 69 74 65 0a 2a 2a 20 63 6f  The SQLite.** co
a9e0: 72 65 20 72 65 73 65 72 76 65 73 20 61 6c 6c 20  re reserves all 
a9f0: 6f 70 63 6f 64 65 73 20 6c 65 73 73 20 74 68 61  opcodes less tha
aa00: 6e 20 31 30 30 20 66 6f 72 20 69 74 73 20 6f 77  n 100 for its ow
aa10: 6e 20 75 73 65 2e 0a 2a 2a 20 41 20 5b 53 51 4c  n use..** A [SQL
aa20: 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53 54  ITE_FCNTL_LOCKST
aa30: 41 54 45 20 7c 20 6c 69 73 74 20 6f 66 20 6f 70  ATE | list of op
aa40: 63 6f 64 65 73 5d 20 6c 65 73 73 20 74 68 61 6e  codes] less than
aa50: 20 31 30 30 20 69 73 20 61 76 61 69 6c 61 62 6c   100 is availabl
aa60: 65 2e 0a 2a 2a 20 41 70 70 6c 69 63 61 74 69 6f  e..** Applicatio
aa70: 6e 73 20 74 68 61 74 20 64 65 66 69 6e 65 20 61  ns that define a
aa80: 20 63 75 73 74 6f 6d 20 78 46 69 6c 65 43 6f 6e   custom xFileCon
aa90: 74 72 6f 6c 20 6d 65 74 68 6f 64 20 73 68 6f 75  trol method shou
aaa0: 6c 64 20 75 73 65 20 6f 70 63 6f 64 65 73 0a 2a  ld use opcodes.*
aab0: 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 31  * greater than 1
aac0: 30 30 20 74 6f 20 61 76 6f 69 64 20 63 6f 6e 66  00 to avoid conf
aad0: 6c 69 63 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  licts..**.** The
aae0: 20 78 53 65 63 74 6f 72 53 69 7a 65 28 29 20 6d   xSectorSize() m
aaf0: 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 74 68  ethod returns th
ab00: 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 6f 66  e sector size of
ab10: 20 74 68 65 0a 2a 2a 20 64 65 76 69 63 65 20 74   the.** device t
ab20: 68 61 74 20 75 6e 64 65 72 6c 69 65 73 20 74 68  hat underlies th
ab30: 65 20 66 69 6c 65 2e 20 20 54 68 65 20 73 65 63  e file.  The sec
ab40: 74 6f 72 20 73 69 7a 65 20 69 73 20 74 68 65 0a  tor size is the.
ab50: 2a 2a 20 6d 69 6e 69 6d 75 6d 20 77 72 69 74 65  ** minimum write
ab60: 20 74 68 61 74 20 63 61 6e 20 62 65 20 70 65 72   that can be per
ab70: 66 6f 72 6d 65 64 20 77 69 74 68 6f 75 74 20 64  formed without d
ab80: 69 73 74 75 72 62 69 6e 67 0a 2a 2a 20 6f 74 68  isturbing.** oth
ab90: 65 72 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  er bytes in the 
aba0: 66 69 6c 65 2e 20 20 54 68 65 20 78 44 65 76 69  file.  The xDevi
abb0: 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69 63  ceCharacteristic
abc0: 73 28 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65  s().** method re
abd0: 74 75 72 6e 73 20 61 20 62 69 74 20 76 65 63 74  turns a bit vect
abe0: 6f 72 20 64 65 73 63 72 69 62 69 6e 67 20 62 65  or describing be
abf0: 68 61 76 69 6f 72 73 20 6f 66 20 74 68 65 0a 2a  haviors of the.*
ac00: 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 65 76  * underlying dev
ac10: 69 63 65 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a  ice:.**.** <ul>.
ac20: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
ac30: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 5d 0a 2a 2a  IOCAP_ATOMIC].**
ac40: 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49 4f   <li> [SQLITE_IO
ac50: 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 5d 0a 2a  CAP_ATOMIC512].*
ac60: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
ac70: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 4b 5d 0a 2a  OCAP_ATOMIC1K].*
ac80: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
ac90: 4f 43 41 50 5f 41 54 4f 4d 49 43 32 4b 5d 0a 2a  OCAP_ATOMIC2K].*
aca0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
acb0: 4f 43 41 50 5f 41 54 4f 4d 49 43 34 4b 5d 0a 2a  OCAP_ATOMIC4K].*
acc0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
acd0: 4f 43 41 50 5f 41 54 4f 4d 49 43 38 4b 5d 0a 2a  OCAP_ATOMIC8K].*
ace0: 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f 49  * <li> [SQLITE_I
acf0: 4f 43 41 50 5f 41 54 4f 4d 49 43 31 36 4b 5d 0a  OCAP_ATOMIC16K].
ad00: 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45 5f  ** <li> [SQLITE_
ad10: 49 4f 43 41 50 5f 41 54 4f 4d 49 43 33 32 4b 5d  IOCAP_ATOMIC32K]
ad20: 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54 45  .** <li> [SQLITE
ad30: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b  _IOCAP_ATOMIC64K
ad40: 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49 54  ].** <li> [SQLIT
ad50: 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50 50  E_IOCAP_SAFE_APP
ad60: 45 4e 44 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51  END].** <li> [SQ
ad70: 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45 51 55 45  LITE_IOCAP_SEQUE
ad80: 4e 54 49 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a  NTIAL].** </ul>.
ad90: 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45  **.** The SQLITE
ada0: 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 20 70 72  _IOCAP_ATOMIC pr
adb0: 6f 70 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61  operty means tha
adc0: 74 20 61 6c 6c 20 77 72 69 74 65 73 20 6f 66 0a  t all writes of.
add0: 2a 2a 20 61 6e 79 20 73 69 7a 65 20 61 72 65 20  ** any size are 
ade0: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c  atomic.  The SQL
adf0: 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
ae00: 6e 6e 6e 20 76 61 6c 75 65 73 0a 2a 2a 20 6d 65  nnn values.** me
ae10: 61 6e 20 74 68 61 74 20 77 72 69 74 65 73 20 6f  an that writes o
ae20: 66 20 62 6c 6f 63 6b 73 20 74 68 61 74 20 61 72  f blocks that ar
ae30: 65 20 6e 6e 6e 20 62 79 74 65 73 20 69 6e 20 73  e nnn bytes in s
ae40: 69 7a 65 20 61 6e 64 0a 2a 2a 20 61 72 65 20 61  ize and.** are a
ae50: 6c 69 67 6e 65 64 20 74 6f 20 61 6e 20 61 64 64  ligned to an add
ae60: 72 65 73 73 20 77 68 69 63 68 20 69 73 20 61 6e  ress which is an
ae70: 20 69 6e 74 65 67 65 72 20 6d 75 6c 74 69 70 6c   integer multipl
ae80: 65 20 6f 66 0a 2a 2a 20 6e 6e 6e 20 61 72 65 20  e of.** nnn are 
ae90: 61 74 6f 6d 69 63 2e 20 20 54 68 65 20 53 51 4c  atomic.  The SQL
aea0: 49 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41  ITE_IOCAP_SAFE_A
aeb0: 50 50 45 4e 44 20 76 61 6c 75 65 20 6d 65 61 6e  PPEND value mean
aec0: 73 0a 2a 2a 20 74 68 61 74 20 77 68 65 6e 20 64  s.** that when d
aed0: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 20  ata is appended 
aee0: 74 6f 20 61 20 66 69 6c 65 2c 20 74 68 65 20 64  to a file, the d
aef0: 61 74 61 20 69 73 20 61 70 70 65 6e 64 65 64 0a  ata is appended.
af00: 2a 2a 20 66 69 72 73 74 20 74 68 65 6e 20 74 68  ** first then th
af10: 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66 69  e size of the fi
af20: 6c 65 20 69 73 20 65 78 74 65 6e 64 65 64 2c 20  le is extended, 
af30: 6e 65 76 65 72 20 74 68 65 20 6f 74 68 65 72 0a  never the other.
af40: 2a 2a 20 77 61 79 20 61 72 6f 75 6e 64 2e 20 20  ** way around.  
af50: 54 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50  The SQLITE_IOCAP
af60: 5f 53 45 51 55 45 4e 54 49 41 4c 20 70 72 6f 70  _SEQUENTIAL prop
af70: 65 72 74 79 20 6d 65 61 6e 73 20 74 68 61 74 0a  erty means that.
af80: 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69  ** information i
af90: 73 20 77 72 69 74 74 65 6e 20 74 6f 20 64 69 73  s written to dis
afa0: 6b 20 69 6e 20 74 68 65 20 73 61 6d 65 20 6f 72  k in the same or
afb0: 64 65 72 20 61 73 20 63 61 6c 6c 73 0a 2a 2a 20  der as calls.** 
afc0: 74 6f 20 78 57 72 69 74 65 28 29 2e 0a 2a 2a 0a  to xWrite()..**.
afd0: 2a 2a 20 49 66 20 78 52 65 61 64 28 29 20 72 65  ** If xRead() re
afe0: 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49 4f 45  turns SQLITE_IOE
aff0: 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20 69 74  RR_SHORT_READ it
b000: 20 6d 75 73 74 20 61 6c 73 6f 20 66 69 6c 6c 0a   must also fill.
b010: 2a 2a 20 69 6e 20 74 68 65 20 75 6e 72 65 61 64  ** in the unread
b020: 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20 74 68 65   portions of the
b030: 20 62 75 66 66 65 72 20 77 69 74 68 20 7a 65 72   buffer with zer
b040: 6f 73 2e 20 20 41 20 56 46 53 20 74 68 61 74 0a  os.  A VFS that.
b050: 2a 2a 20 66 61 69 6c 73 20 74 6f 20 7a 65 72 6f  ** fails to zero
b060: 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65 61 64  -fill short read
b070: 73 20 6d 69 67 68 74 20 73 65 65 6d 20 74 6f 20  s might seem to 
b080: 77 6f 72 6b 2e 20 20 48 6f 77 65 76 65 72 2c 0a  work.  However,.
b090: 2a 2a 20 66 61 69 6c 75 72 65 20 74 6f 20 7a 65  ** failure to ze
b0a0: 72 6f 2d 66 69 6c 6c 20 73 68 6f 72 74 20 72 65  ro-fill short re
b0b0: 61 64 73 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  ads will eventua
b0c0: 6c 6c 79 20 6c 65 61 64 20 74 6f 0a 2a 2a 20 64  lly lead to.** d
b0d0: 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
b0e0: 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  on..*/.typedef s
b0f0: 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
b100: 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74 65 33  _methods sqlite3
b110: 5f 69 6f 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72  _io_methods;.str
b120: 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  uct sqlite3_io_m
b130: 65 74 68 6f 64 73 20 7b 0a 20 20 69 6e 74 20 69  ethods {.  int i
b140: 56 65 72 73 69 6f 6e 3b 0a 20 20 69 6e 74 20 28  Version;.  int (
b150: 2a 78 43 6c 6f 73 65 29 28 73 71 6c 69 74 65 33  *xClose)(sqlite3
b160: 5f 66 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28  _file*);.  int (
b170: 2a 78 52 65 61 64 29 28 73 71 6c 69 74 65 33 5f  *xRead)(sqlite3_
b180: 66 69 6c 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e  file*, void*, in
b190: 74 20 69 41 6d 74 2c 20 73 71 6c 69 74 65 33 5f  t iAmt, sqlite3_
b1a0: 69 6e 74 36 34 20 69 4f 66 73 74 29 3b 0a 20 20  int64 iOfst);.  
b1b0: 69 6e 74 20 28 2a 78 57 72 69 74 65 29 28 73 71  int (*xWrite)(sq
b1c0: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 63 6f 6e  lite3_file*, con
b1d0: 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20 69 41  st void*, int iA
b1e0: 6d 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  mt, sqlite3_int6
b1f0: 34 20 69 4f 66 73 74 29 3b 0a 20 20 69 6e 74 20  4 iOfst);.  int 
b200: 28 2a 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c  (*xTruncate)(sql
b210: 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c 69  ite3_file*, sqli
b220: 74 65 33 5f 69 6e 74 36 34 20 73 69 7a 65 29 3b  te3_int64 size);
b230: 0a 20 20 69 6e 74 20 28 2a 78 53 79 6e 63 29 28  .  int (*xSync)(
b240: 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69  sqlite3_file*, i
b250: 6e 74 20 66 6c 61 67 73 29 3b 0a 20 20 69 6e 74  nt flags);.  int
b260: 20 28 2a 78 46 69 6c 65 53 69 7a 65 29 28 73 71   (*xFileSize)(sq
b270: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 73 71 6c  lite3_file*, sql
b280: 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a  ite3_int64 *pSiz
b290: 65 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4c 6f 63  e);.  int (*xLoc
b2a0: 6b 29 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  k)(sqlite3_file*
b2b0: 2c 20 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a  , int);.  int (*
b2c0: 78 55 6e 6c 6f 63 6b 29 28 73 71 6c 69 74 65 33  xUnlock)(sqlite3
b2d0: 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b 0a 20 20  _file*, int);.  
b2e0: 69 6e 74 20 28 2a 78 43 68 65 63 6b 52 65 73 65  int (*xCheckRese
b2f0: 72 76 65 64 4c 6f 63 6b 29 28 73 71 6c 69 74 65  rvedLock)(sqlite
b300: 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 20 2a 70 52  3_file*, int *pR
b310: 65 73 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a  esOut);.  int (*
b320: 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 29 28 73 71  xFileControl)(sq
b330: 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74  lite3_file*, int
b340: 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72 67 29   op, void *pArg)
b350: 3b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74 6f  ;.  int (*xSecto
b360: 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 66  rSize)(sqlite3_f
b370: 69 6c 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78  ile*);.  int (*x
b380: 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
b390: 73 74 69 63 73 29 28 73 71 6c 69 74 65 33 5f 66  stics)(sqlite3_f
b3a0: 69 6c 65 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 69  ile*);.  /* Addi
b3b0: 74 69 6f 6e 61 6c 20 6d 65 74 68 6f 64 73 20 6d  tional methods m
b3c0: 61 79 20 62 65 20 61 64 64 65 64 20 69 6e 20 66  ay be added in f
b3d0: 75 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 2a  uture releases *
b3e0: 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  /.};../*.** CAPI
b3f0: 33 52 45 46 3a 20 53 74 61 6e 64 61 72 64 20 46  3REF: Standard F
b400: 69 6c 65 20 43 6f 6e 74 72 6f 6c 20 4f 70 63 6f  ile Control Opco
b410: 64 65 73 20 7b 48 31 31 33 31 30 7d 20 3c 53 33  des {H11310} <S3
b420: 30 38 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  0800>.**.** Thes
b430: 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61  e integer consta
b440: 6e 74 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20  nts are opcodes 
b450: 66 6f 72 20 74 68 65 20 78 46 69 6c 65 43 6f 6e  for the xFileCon
b460: 74 72 6f 6c 20 6d 65 74 68 6f 64 0a 2a 2a 20 6f  trol method.** o
b470: 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 69  f the [sqlite3_i
b480: 6f 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  o_methods] objec
b490: 74 20 61 6e 64 20 66 6f 72 20 74 68 65 20 5b 73  t and for the [s
b4a0: 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
b4b0: 72 6f 6c 28 29 5d 0a 2a 2a 20 69 6e 74 65 72 66  rol()].** interf
b4c0: 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b  ace..**.** The [
b4d0: 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43  SQLITE_FCNTL_LOC
b4e0: 4b 53 54 41 54 45 5d 20 6f 70 63 6f 64 65 20 69  KSTATE] opcode i
b4f0: 73 20 75 73 65 64 20 66 6f 72 20 64 65 62 75 67  s used for debug
b500: 67 69 6e 67 2e 20 20 54 68 69 73 0a 2a 2a 20 6f  ging.  This.** o
b510: 70 63 6f 64 65 20 63 61 75 73 65 73 20 74 68 65  pcode causes the
b520: 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65   xFileControl me
b530: 74 68 6f 64 20 74 6f 20 77 72 69 74 65 20 74 68  thod to write th
b540: 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 20  e current state 
b550: 6f 66 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 20 28  of.** the lock (
b560: 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 4c  one of [SQLITE_L
b570: 4f 43 4b 5f 4e 4f 4e 45 5d 2c 20 5b 53 51 4c 49  OCK_NONE], [SQLI
b580: 54 45 5f 4c 4f 43 4b 5f 53 48 41 52 45 44 5d 2c  TE_LOCK_SHARED],
b590: 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4c 4f 43 4b  .** [SQLITE_LOCK
b5a0: 5f 52 45 53 45 52 56 45 44 5d 2c 20 5b 53 51 4c  _RESERVED], [SQL
b5b0: 49 54 45 5f 4c 4f 43 4b 5f 50 45 4e 44 49 4e 47  ITE_LOCK_PENDING
b5c0: 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4c 4f  ], or [SQLITE_LO
b5d0: 43 4b 5f 45 58 43 4c 55 53 49 56 45 5d 29 0a 2a  CK_EXCLUSIVE]).*
b5e0: 2a 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65 67 65  * into an intege
b5f0: 72 20 74 68 61 74 20 74 68 65 20 70 41 72 67 20  r that the pArg 
b600: 61 72 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 20  argument points 
b610: 74 6f 2e 20 54 68 69 73 20 63 61 70 61 62 69 6c  to. This capabil
b620: 69 74 79 0a 2a 2a 20 69 73 20 75 73 65 64 20 64  ity.** is used d
b630: 75 72 69 6e 67 20 74 65 73 74 69 6e 67 20 61 6e  uring testing an
b640: 64 20 6f 6e 6c 79 20 6e 65 65 64 73 20 74 6f 20  d only needs to 
b650: 62 65 20 73 75 70 70 6f 72 74 65 64 20 77 68 65  be supported whe
b660: 6e 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 2a 2a  n SQLITE_TEST.**
b670: 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a   is defined..*/.
b680: 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
b690: 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 20 20  CNTL_LOCKSTATE  
b6a0: 20 20 20 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 43        1../*.** C
b6b0: 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78 20 48  API3REF: Mutex H
b6c0: 61 6e 64 6c 65 20 7b 48 31 37 31 31 30 7d 20 3c  andle {H17110} <
b6d0: 53 32 30 31 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20130>.**.** Th
b6e0: 65 20 6d 75 74 65 78 20 6d 6f 64 75 6c 65 20 77  e mutex module w
b6f0: 69 74 68 69 6e 20 53 51 4c 69 74 65 20 64 65 66  ithin SQLite def
b700: 69 6e 65 73 20 5b 73 71 6c 69 74 65 33 5f 6d 75  ines [sqlite3_mu
b710: 74 65 78 5d 20 74 6f 20 62 65 20 61 6e 0a 2a 2a  tex] to be an.**
b720: 20 61 62 73 74 72 61 63 74 20 74 79 70 65 20 66   abstract type f
b730: 6f 72 20 61 20 6d 75 74 65 78 20 6f 62 6a 65 63  or a mutex objec
b740: 74 2e 20 20 54 68 65 20 53 51 4c 69 74 65 20 63  t.  The SQLite c
b750: 6f 72 65 20 6e 65 76 65 72 20 6c 6f 6f 6b 73 0a  ore never looks.
b760: 2a 2a 20 61 74 20 74 68 65 20 69 6e 74 65 72 6e  ** at the intern
b770: 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  al representatio
b780: 6e 20 6f 66 20 61 6e 20 5b 73 71 6c 69 74 65 33  n of an [sqlite3
b790: 5f 6d 75 74 65 78 5d 2e 20 20 49 74 20 6f 6e 6c  _mutex].  It onl
b7a0: 79 0a 2a 2a 20 64 65 61 6c 73 20 77 69 74 68 20  y.** deals with 
b7b0: 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20  pointers to the 
b7c0: 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5d 20  [sqlite3_mutex] 
b7d0: 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 4d 75  object..**.** Mu
b7e0: 74 65 78 65 73 20 61 72 65 20 63 72 65 61 74 65  texes are create
b7f0: 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
b800: 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 5d 2e  _mutex_alloc()].
b810: 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
b820: 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ct sqlite3_mutex
b830: 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 3b 0a   sqlite3_mutex;.
b840: 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
b850: 20 4f 53 20 49 6e 74 65 72 66 61 63 65 20 4f 62   OS Interface Ob
b860: 6a 65 63 74 20 7b 48 31 31 31 34 30 7d 20 3c 53  ject {H11140} <S
b870: 32 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 6e 20  20100>.**.** An 
b880: 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
b890: 73 71 6c 69 74 65 33 5f 76 66 73 20 6f 62 6a 65  sqlite3_vfs obje
b8a0: 63 74 20 64 65 66 69 6e 65 73 20 74 68 65 20 69  ct defines the i
b8b0: 6e 74 65 72 66 61 63 65 20 62 65 74 77 65 65 6e  nterface between
b8c0: 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 63  .** the SQLite c
b8d0: 6f 72 65 20 61 6e 64 20 74 68 65 20 75 6e 64 65  ore and the unde
b8e0: 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
b8f0: 20 73 79 73 74 65 6d 2e 20 20 54 68 65 20 22 76   system.  The "v
b900: 66 73 22 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61  fs".** in the na
b910: 6d 65 20 6f 66 20 74 68 65 20 6f 62 6a 65 63 74  me of the object
b920: 20 73 74 61 6e 64 73 20 66 6f 72 20 22 76 69 72   stands for "vir
b930: 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65 6d  tual file system
b940: 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 76 61 6c  "..**.** The val
b950: 75 65 20 6f 66 20 74 68 65 20 69 56 65 72 73 69  ue of the iVersi
b960: 6f 6e 20 66 69 65 6c 64 20 69 73 20 69 6e 69 74  on field is init
b970: 69 61 6c 6c 79 20 31 20 62 75 74 20 6d 61 79 20  ially 1 but may 
b980: 62 65 20 6c 61 72 67 65 72 20 69 6e 0a 2a 2a 20  be larger in.** 
b990: 66 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20  future versions 
b9a0: 6f 66 20 53 51 4c 69 74 65 2e 20 20 41 64 64 69  of SQLite.  Addi
b9b0: 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 6d 61  tional fields ma
b9c0: 79 20 62 65 20 61 70 70 65 6e 64 65 64 20 74 6f  y be appended to
b9d0: 20 74 68 69 73 0a 2a 2a 20 6f 62 6a 65 63 74 20   this.** object 
b9e0: 77 68 65 6e 20 74 68 65 20 69 56 65 72 73 69 6f  when the iVersio
b9f0: 6e 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65  n value is incre
ba00: 61 73 65 64 2e 20 20 4e 6f 74 65 20 74 68 61 74  ased.  Note that
ba10: 20 74 68 65 20 73 74 72 75 63 74 75 72 65 0a 2a   the structure.*
ba20: 2a 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33  * of the sqlite3
ba30: 5f 76 66 73 20 6f 62 6a 65 63 74 20 63 68 61 6e  _vfs object chan
ba40: 67 65 73 20 69 6e 20 74 68 65 20 74 72 61 6e 73  ges in the trans
ba50: 61 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 0a 2a  action between.*
ba60: 2a 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e  * SQLite version
ba70: 20 33 2e 35 2e 39 20 61 6e 64 20 33 2e 36 2e 30   3.5.9 and 3.6.0
ba80: 20 61 6e 64 20 79 65 74 20 74 68 65 20 69 56 65   and yet the iVe
ba90: 72 73 69 6f 6e 20 66 69 65 6c 64 20 77 61 73 20  rsion field was 
baa0: 6e 6f 74 0a 2a 2a 20 6d 6f 64 69 66 69 65 64 2e  not.** modified.
bab0: 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 4f 73 46  .**.** The szOsF
bac0: 69 6c 65 20 66 69 65 6c 64 20 69 73 20 74 68 65  ile field is the
bad0: 20 73 69 7a 65 20 6f 66 20 74 68 65 20 73 75 62   size of the sub
bae0: 63 6c 61 73 73 65 64 20 5b 73 71 6c 69 74 65 33  classed [sqlite3
baf0: 5f 66 69 6c 65 5d 0a 2a 2a 20 73 74 72 75 63 74  _file].** struct
bb00: 75 72 65 20 75 73 65 64 20 62 79 20 74 68 69 73  ure used by this
bb10: 20 56 46 53 2e 20 20 6d 78 50 61 74 68 6e 61 6d   VFS.  mxPathnam
bb20: 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d  e is the maximum
bb30: 20 6c 65 6e 67 74 68 20 6f 66 0a 2a 2a 20 61 20   length of.** a 
bb40: 70 61 74 68 6e 61 6d 65 20 69 6e 20 74 68 69 73  pathname in this
bb50: 20 56 46 53 2e 0a 2a 2a 0a 2a 2a 20 52 65 67 69   VFS..**.** Regi
bb60: 73 74 65 72 65 64 20 73 71 6c 69 74 65 33 5f 76  stered sqlite3_v
bb70: 66 73 20 6f 62 6a 65 63 74 73 20 61 72 65 20 6b  fs objects are k
bb80: 65 70 74 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20  ept on a linked 
bb90: 6c 69 73 74 20 66 6f 72 6d 65 64 20 62 79 0a 2a  list formed by.*
bba0: 2a 20 74 68 65 20 70 4e 65 78 74 20 70 6f 69 6e  * the pNext poin
bbb0: 74 65 72 2e 20 20 54 68 65 20 5b 73 71 6c 69 74  ter.  The [sqlit
bbc0: 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
bbd0: 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73 71 6c 69 74  )].** and [sqlit
bbe0: 65 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65  e3_vfs_unregiste
bbf0: 72 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20  r()] interfaces 
bc00: 6d 61 6e 61 67 65 20 74 68 69 73 20 6c 69 73 74  manage this list
bc10: 0a 2a 2a 20 69 6e 20 61 20 74 68 72 65 61 64 2d  .** in a thread-
bc20: 73 61 66 65 20 77 61 79 2e 20 20 54 68 65 20 5b  safe way.  The [
bc30: 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e 64  sqlite3_vfs_find
bc40: 28 29 5d 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  ()] interface.**
bc50: 20 73 65 61 72 63 68 65 73 20 74 68 65 20 6c 69   searches the li
bc60: 73 74 2e 20 20 4e 65 69 74 68 65 72 20 74 68 65  st.  Neither the
bc70: 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63 6f 64   application cod
bc80: 65 20 6e 6f 72 20 74 68 65 20 56 46 53 0a 2a 2a  e nor the VFS.**
bc90: 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
bca0: 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65 20 70  should use the p
bcb0: 4e 65 78 74 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a  Next pointer..**
bcc0: 0a 2a 2a 20 54 68 65 20 70 4e 65 78 74 20 66 69  .** The pNext fi
bcd0: 65 6c 64 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  eld is the only 
bce0: 66 69 65 6c 64 20 69 6e 20 74 68 65 20 73 71 6c  field in the sql
bcf0: 69 74 65 33 5f 76 66 73 0a 2a 2a 20 73 74 72 75  ite3_vfs.** stru
bd00: 63 74 75 72 65 20 74 68 61 74 20 53 51 4c 69 74  cture that SQLit
bd10: 65 20 77 69 6c 6c 20 65 76 65 72 20 6d 6f 64 69  e will ever modi
bd20: 66 79 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c  fy.  SQLite will
bd30: 20 6f 6e 6c 79 20 61 63 63 65 73 73 0a 2a 2a 20   only access.** 
bd40: 6f 72 20 6d 6f 64 69 66 79 20 74 68 69 73 20 66  or modify this f
bd50: 69 65 6c 64 20 77 68 69 6c 65 20 68 6f 6c 64 69  ield while holdi
bd60: 6e 67 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  ng a particular 
bd70: 73 74 61 74 69 63 20 6d 75 74 65 78 2e 0a 2a 2a  static mutex..**
bd80: 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   The application
bd90: 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 6d 6f   should never mo
bda0: 64 69 66 79 20 61 6e 79 74 68 69 6e 67 20 77 69  dify anything wi
bdb0: 74 68 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33  thin the sqlite3
bdc0: 5f 76 66 73 0a 2a 2a 20 6f 62 6a 65 63 74 20 6f  _vfs.** object o
bdd0: 6e 63 65 20 74 68 65 20 6f 62 6a 65 63 74 20 68  nce the object h
bde0: 61 73 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  as been register
bdf0: 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 4e  ed..**.** The zN
be00: 61 6d 65 20 66 69 65 6c 64 20 68 6f 6c 64 73 20  ame field holds 
be10: 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
be20: 56 46 53 20 6d 6f 64 75 6c 65 2e 20 20 54 68 65  VFS module.  The
be30: 20 6e 61 6d 65 20 6d 75 73 74 0a 2a 2a 20 62 65   name must.** be
be40: 20 75 6e 69 71 75 65 20 61 63 72 6f 73 73 20 61   unique across a
be50: 6c 6c 20 56 46 53 20 6d 6f 64 75 6c 65 73 2e 0a  ll VFS modules..
be60: 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 31 7d 20 53  **.** {H11141} S
be70: 51 4c 69 74 65 20 77 69 6c 6c 20 67 75 61 72 61  QLite will guara
be80: 6e 74 65 65 20 74 68 61 74 20 74 68 65 20 7a 46  ntee that the zF
be90: 69 6c 65 6e 61 6d 65 20 70 61 72 61 6d 65 74 65  ilename paramete
bea0: 72 20 74 6f 20 78 4f 70 65 6e 0a 2a 2a 20 69 73  r to xOpen.** is
beb0: 20 65 69 74 68 65 72 20 61 20 4e 55 4c 4c 20 70   either a NULL p
bec0: 6f 69 6e 74 65 72 20 6f 72 20 73 74 72 69 6e 67  ointer or string
bed0: 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
bee0: 6d 20 78 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  m xFullPathname(
bef0: 29 2e 20 20 53 51 4c 69 74 65 20 66 75 72 74 68  ).  SQLite furth
bf00: 65 72 20 67 75 61 72 61 6e 74 65 65 73 20 74 68  er guarantees th
bf10: 61 74 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  at.** the string
bf20: 20 77 69 6c 6c 20 62 65 20 76 61 6c 69 64 20 61   will be valid a
bf30: 6e 64 20 75 6e 63 68 61 6e 67 65 64 20 75 6e 74  nd unchanged unt
bf40: 69 6c 20 78 43 6c 6f 73 65 28 29 20 69 73 0a 2a  il xClose() is.*
bf50: 2a 20 63 61 6c 6c 65 64 2e 20 7b 45 4e 44 7d 20  * called. {END} 
bf60: 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 65 20   Because of the 
bf70: 70 72 65 76 69 6f 75 73 20 73 65 6e 74 65 6e 73  previous sentens
bf80: 65 2c 0a 2a 2a 20 74 68 65 20 5b 73 71 6c 69 74  e,.** the [sqlit
bf90: 65 33 5f 66 69 6c 65 5d 20 63 61 6e 20 73 61 66  e3_file] can saf
bfa0: 65 6c 79 20 73 74 6f 72 65 20 61 20 70 6f 69 6e  ely store a poin
bfb0: 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 66 69  ter to the.** fi
bfc0: 6c 65 6e 61 6d 65 20 69 66 20 69 74 20 6e 65 65  lename if it nee
bfd0: 64 73 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74  ds to remember t
bfe0: 68 65 20 66 69 6c 65 6e 61 6d 65 20 66 6f 72 20  he filename for 
bff0: 73 6f 6d 65 20 72 65 61 73 6f 6e 2e 0a 2a 2a 20  some reason..** 
c000: 49 66 20 74 68 65 20 7a 46 69 6c 65 6e 61 6d 65  If the zFilename
c010: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 78 4f   parameter is xO
c020: 70 65 6e 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f  pen is a NULL po
c030: 69 6e 74 65 72 20 74 68 65 6e 20 78 4f 70 65 6e  inter then xOpen
c040: 0a 2a 2a 20 6d 75 73 74 20 69 6e 76 69 74 65 20  .** must invite 
c050: 69 74 73 20 6f 77 6e 20 74 65 6d 70 6f 72 61 72  its own temporar
c060: 79 20 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 66  y name for the f
c070: 69 6c 65 2e 20 20 57 68 65 6e 65 76 65 72 20 74  ile.  Whenever t
c080: 68 65 20 0a 2a 2a 20 78 46 69 6c 65 6e 61 6d 65  he .** xFilename
c090: 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 4e 55   parameter is NU
c0a0: 4c 4c 20 69 74 20 77 69 6c 6c 20 61 6c 73 6f 20  LL it will also 
c0b0: 62 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74  be the case that
c0c0: 20 74 68 65 0a 2a 2a 20 66 6c 61 67 73 20 70 61   the.** flags pa
c0d0: 72 61 6d 65 74 65 72 20 77 69 6c 6c 20 69 6e 63  rameter will inc
c0e0: 6c 75 64 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45  lude [SQLITE_OPE
c0f0: 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d  N_DELETEONCLOSE]
c100: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 32 7d  ..**.** {H11142}
c110: 20 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d   The flags argum
c120: 65 6e 74 20 74 6f 20 78 4f 70 65 6e 28 29 20 69  ent to xOpen() i
c130: 6e 63 6c 75 64 65 73 20 61 6c 6c 20 62 69 74 73  ncludes all bits
c140: 20 73 65 74 20 69 6e 0a 2a 2a 20 74 68 65 20 66   set in.** the f
c150: 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 20 74 6f  lags argument to
c160: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
c170: 32 28 29 5d 2e 20 20 4f 72 20 69 66 20 5b 73 71  2()].  Or if [sq
c180: 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 0a 2a 2a  lite3_open()].**
c190: 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65   or [sqlite3_ope
c1a0: 6e 31 36 28 29 5d 20 69 73 20 75 73 65 64 2c 20  n16()] is used, 
c1b0: 74 68 65 6e 20 66 6c 61 67 73 20 69 6e 63 6c 75  then flags inclu
c1c0: 64 65 73 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20  des at least.** 
c1d0: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41  [SQLITE_OPEN_REA
c1e0: 44 57 52 49 54 45 5d 20 7c 20 5b 53 51 4c 49 54  DWRITE] | [SQLIT
c1f0: 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 2e 20  E_OPEN_CREATE]. 
c200: 7b 45 4e 44 7d 0a 2a 2a 20 49 66 20 78 4f 70 65  {END}.** If xOpe
c210: 6e 28 29 20 6f 70 65 6e 73 20 61 20 66 69 6c 65  n() opens a file
c220: 20 72 65 61 64 2d 6f 6e 6c 79 20 74 68 65 6e 20   read-only then 
c230: 69 74 20 73 65 74 73 20 2a 70 4f 75 74 46 6c 61  it sets *pOutFla
c240: 67 73 20 74 6f 0a 2a 2a 20 69 6e 63 6c 75 64 65  gs to.** include
c250: 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   [SQLITE_OPEN_RE
c260: 41 44 4f 4e 4c 59 5d 2e 20 20 4f 74 68 65 72 20  ADONLY].  Other 
c270: 62 69 74 73 20 69 6e 20 2a 70 4f 75 74 46 6c 61  bits in *pOutFla
c280: 67 73 20 6d 61 79 20 62 65 20 73 65 74 2e 0a 2a  gs may be set..*
c290: 2a 0a 2a 2a 20 7b 48 31 31 31 34 33 7d 20 53 51  *.** {H11143} SQ
c2a0: 4c 69 74 65 20 77 69 6c 6c 20 61 6c 73 6f 20 61  Lite will also a
c2b0: 64 64 20 6f 6e 65 20 6f 66 20 74 68 65 20 66 6f  dd one of the fo
c2c0: 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20 74 6f  llowing flags to
c2d0: 20 74 68 65 20 78 4f 70 65 6e 28 29 0a 2a 2a 20   the xOpen().** 
c2e0: 63 61 6c 6c 2c 20 64 65 70 65 6e 64 69 6e 67 20  call, depending 
c2f0: 6f 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 62 65  on the object be
c300: 69 6e 67 20 6f 70 65 6e 65 64 3a 0a 2a 2a 0a 2a  ing opened:.**.*
c310: 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
c320: 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  [SQLITE_OPEN_MAI
c330: 4e 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b  N_DB].** <li>  [
c340: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
c350: 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 6c 69  _JOURNAL].** <li
c360: 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  >  [SQLITE_OPEN_
c370: 54 45 4d 50 5f 44 42 5d 0a 2a 2a 20 3c 6c 69 3e  TEMP_DB].** <li>
c380: 20 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54    [SQLITE_OPEN_T
c390: 45 4d 50 5f 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20  EMP_JOURNAL].** 
c3a0: 3c 6c 69 3e 20 20 5b 53 51 4c 49 54 45 5f 4f 50  <li>  [SQLITE_OP
c3b0: 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42 5d  EN_TRANSIENT_DB]
c3c0: 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c 49 54  .** <li>  [SQLIT
c3d0: 45 5f 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41  E_OPEN_SUBJOURNA
c3e0: 4c 5d 0a 2a 2a 20 3c 6c 69 3e 20 20 5b 53 51 4c  L].** <li>  [SQL
c3f0: 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f  ITE_OPEN_MASTER_
c400: 4a 4f 55 52 4e 41 4c 5d 0a 2a 2a 20 3c 2f 75 6c  JOURNAL].** </ul
c410: 3e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68  > {END}.**.** Th
c420: 65 20 66 69 6c 65 20 49 2f 4f 20 69 6d 70 6c 65  e file I/O imple
c430: 6d 65 6e 74 61 74 69 6f 6e 20 63 61 6e 20 75 73  mentation can us
c440: 65 20 74 68 65 20 6f 62 6a 65 63 74 20 74 79 70  e the object typ
c450: 65 20 66 6c 61 67 73 20 74 6f 0a 2a 2a 20 63 68  e flags to.** ch
c460: 61 6e 67 65 20 74 68 65 20 77 61 79 20 69 74 20  ange the way it 
c470: 64 65 61 6c 73 20 77 69 74 68 20 66 69 6c 65 73  deals with files
c480: 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  .  For example, 
c490: 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 0a 2a  an application.*
c4a0: 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20  * that does not 
c4b0: 63 61 72 65 20 61 62 6f 75 74 20 63 72 61 73 68  care about crash
c4c0: 20 72 65 63 6f 76 65 72 79 20 6f 72 20 72 6f 6c   recovery or rol
c4d0: 6c 62 61 63 6b 20 6d 69 67 68 74 20 6d 61 6b 65  lback might make
c4e0: 0a 2a 2a 20 74 68 65 20 6f 70 65 6e 20 6f 66 20  .** the open of 
c4f0: 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  a journal file a
c500: 20 6e 6f 2d 6f 70 2e 20 20 57 72 69 74 65 73 20   no-op.  Writes 
c510: 74 6f 20 74 68 69 73 20 6a 6f 75 72 6e 61 6c 20  to this journal 
c520: 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20 62 65  would.** also be
c530: 20 6e 6f 2d 6f 70 73 2c 20 61 6e 64 20 61 6e 79   no-ops, and any
c540: 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 61 64   attempt to read
c550: 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 6f 75   the journal wou
c560: 6c 64 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ld return.** SQL
c570: 49 54 45 5f 49 4f 45 52 52 2e 20 20 4f 72 20 74  ITE_IOERR.  Or t
c580: 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
c590: 6e 20 6d 69 67 68 74 20 72 65 63 6f 67 6e 69 7a  n might recogniz
c5a0: 65 20 74 68 61 74 20 61 20 64 61 74 61 62 61 73  e that a databas
c5b0: 65 0a 2a 2a 20 66 69 6c 65 20 77 69 6c 6c 20 62  e.** file will b
c5c0: 65 20 64 6f 69 6e 67 20 70 61 67 65 2d 61 6c 69  e doing page-ali
c5d0: 67 6e 65 64 20 73 65 63 74 6f 72 20 72 65 61 64  gned sector read
c5e0: 73 20 61 6e 64 20 77 72 69 74 65 73 20 69 6e 20  s and writes in 
c5f0: 61 20 72 61 6e 64 6f 6d 0a 2a 2a 20 6f 72 64 65  a random.** orde
c600: 72 20 61 6e 64 20 73 65 74 20 75 70 20 69 74 73  r and set up its
c610: 20 49 2f 4f 20 73 75 62 73 79 73 74 65 6d 20 61   I/O subsystem a
c620: 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a 2a  ccordingly..**.*
c630: 2a 20 53 51 4c 69 74 65 20 6d 69 67 68 74 20 61  * SQLite might a
c640: 6c 73 6f 20 61 64 64 20 6f 6e 65 20 6f 66 20 74  lso add one of t
c650: 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61  he following fla
c660: 67 73 20 74 6f 20 74 68 65 20 78 4f 70 65 6e 20  gs to the xOpen 
c670: 6d 65 74 68 6f 64 3a 0a 2a 2a 0a 2a 2a 20 3c 75  method:.**.** <u
c680: 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 5b 53 51 4c 49  l>.** <li> [SQLI
c690: 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e  TE_OPEN_DELETEON
c6a0: 43 4c 4f 53 45 5d 0a 2a 2a 20 3c 6c 69 3e 20 5b  CLOSE].** <li> [
c6b0: 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c  SQLITE_OPEN_EXCL
c6c0: 55 53 49 56 45 5d 0a 2a 2a 20 3c 2f 75 6c 3e 0a  USIVE].** </ul>.
c6d0: 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 35 7d 20 54  **.** {H11145} T
c6e0: 68 65 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  he [SQLITE_OPEN_
c6f0: 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 5d 20 66  DELETEONCLOSE] f
c700: 6c 61 67 20 6d 65 61 6e 73 20 74 68 65 20 66 69  lag means the fi
c710: 6c 65 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20  le should be.** 
c720: 64 65 6c 65 74 65 64 20 77 68 65 6e 20 69 74 20  deleted when it 
c730: 69 73 20 63 6c 6f 73 65 64 2e 20 20 7b 48 31 31  is closed.  {H11
c740: 31 34 36 7d 20 54 68 65 20 5b 53 51 4c 49 54 45  146} The [SQLITE
c750: 5f 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c  _OPEN_DELETEONCL
c760: 4f 53 45 5d 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  OSE].** will be 
c770: 73 65 74 20 66 6f 72 20 54 45 4d 50 20 20 64 61  set for TEMP  da
c780: 74 61 62 61 73 65 73 2c 20 6a 6f 75 72 6e 61 6c  tabases, journal
c790: 73 20 61 6e 64 20 66 6f 72 20 73 75 62 6a 6f 75  s and for subjou
c7a0: 72 6e 61 6c 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  rnals..**.** {H1
c7b0: 31 31 34 37 7d 20 54 68 65 20 5b 53 51 4c 49 54  1147} The [SQLIT
c7c0: 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45  E_OPEN_EXCLUSIVE
c7d0: 5d 20 66 6c 61 67 20 6d 65 61 6e 73 20 74 68 65  ] flag means the
c7e0: 20 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20   file should be 
c7f0: 6f 70 65 6e 65 64 0a 2a 2a 20 66 6f 72 20 65 78  opened.** for ex
c800: 63 6c 75 73 69 76 65 20 61 63 63 65 73 73 2e 20  clusive access. 
c810: 20 54 68 69 73 20 66 6c 61 67 20 69 73 20 73 65   This flag is se
c820: 74 20 66 6f 72 20 61 6c 6c 20 66 69 6c 65 73 20  t for all files 
c830: 65 78 63 65 70 74 0a 2a 2a 20 66 6f 72 20 74 68  except.** for th
c840: 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
c850: 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  file..**.** {H11
c860: 31 34 38 7d 20 41 74 20 6c 65 61 73 74 20 73 7a  148} At least sz
c870: 4f 73 46 69 6c 65 20 62 79 74 65 73 20 6f 66 20  OsFile bytes of 
c880: 6d 65 6d 6f 72 79 20 61 72 65 20 61 6c 6c 6f 63  memory are alloc
c890: 61 74 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a  ated by SQLite.*
c8a0: 2a 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 20 5b  * to hold the  [
c8b0: 73 71 6c 69 74 65 33 5f 66 69 6c 65 5d 20 73 74  sqlite3_file] st
c8c0: 72 75 63 74 75 72 65 20 70 61 73 73 65 64 20 61  ructure passed a
c8d0: 73 20 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61  s the third.** a
c8e0: 72 67 75 6d 65 6e 74 20 74 6f 20 78 4f 70 65 6e  rgument to xOpen
c8f0: 2e 20 7b 45 4e 44 7d 20 20 54 68 65 20 78 4f 70  . {END}  The xOp
c900: 65 6e 20 6d 65 74 68 6f 64 20 64 6f 65 73 20 6e  en method does n
c910: 6f 74 20 68 61 76 65 20 74 6f 0a 2a 2a 20 61 6c  ot have to.** al
c920: 6c 6f 63 61 74 65 20 74 68 65 20 73 74 72 75 63  locate the struc
c930: 74 75 72 65 3b 20 69 74 20 73 68 6f 75 6c 64 20  ture; it should 
c940: 6a 75 73 74 20 66 69 6c 6c 20 69 74 20 69 6e 2e  just fill it in.
c950: 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31 34 39 7d 20  .**.** {H11149} 
c960: 54 68 65 20 66 6c 61 67 73 20 61 72 67 75 6d 65  The flags argume
c970: 6e 74 20 74 6f 20 78 41 63 63 65 73 73 28 29 20  nt to xAccess() 
c980: 6d 61 79 20 62 65 20 5b 53 51 4c 49 54 45 5f 41  may be [SQLITE_A
c990: 43 43 45 53 53 5f 45 58 49 53 54 53 5d 0a 2a 2a  CCESS_EXISTS].**
c9a0: 20 74 6f 20 74 65 73 74 20 66 6f 72 20 74 68 65   to test for the
c9b0: 20 65 78 69 73 74 65 6e 63 65 20 6f 66 20 61 20   existence of a 
c9c0: 66 69 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54 45  file, or [SQLITE
c9d0: 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
c9e0: 45 5d 20 74 6f 0a 2a 2a 20 74 65 73 74 20 77 68  E] to.** test wh
c9f0: 65 74 68 65 72 20 61 20 66 69 6c 65 20 69 73 20  ether a file is 
ca00: 72 65 61 64 61 62 6c 65 20 61 6e 64 20 77 72 69  readable and wri
ca10: 74 61 62 6c 65 2c 20 6f 72 20 5b 53 51 4c 49 54  table, or [SQLIT
ca20: 45 5f 41 43 43 45 53 53 5f 52 45 41 44 5d 0a 2a  E_ACCESS_READ].*
ca30: 2a 20 74 6f 20 74 65 73 74 20 77 68 65 74 68 65  * to test whethe
ca40: 72 20 61 20 66 69 6c 65 20 69 73 20 61 74 20 6c  r a file is at l
ca50: 65 61 73 74 20 72 65 61 64 61 62 6c 65 2e 20 7b  east readable. {
ca60: 45 4e 44 7d 20 20 54 68 65 20 66 69 6c 65 20 63  END}  The file c
ca70: 61 6e 20 62 65 20 61 0a 2a 2a 20 64 69 72 65 63  an be a.** direc
ca80: 74 6f 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  tory..**.** {H11
ca90: 31 35 30 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c  150} SQLite will
caa0: 20 61 6c 77 61 79 73 20 61 6c 6c 6f 63 61 74 65   always allocate
cab0: 20 61 74 20 6c 65 61 73 74 20 6d 78 50 61 74 68   at least mxPath
cac0: 6e 61 6d 65 2b 31 20 62 79 74 65 73 20 66 6f 72  name+1 bytes for
cad0: 20 74 68 65 0a 2a 2a 20 6f 75 74 70 75 74 20 62   the.** output b
cae0: 75 66 66 65 72 20 78 46 75 6c 6c 50 61 74 68 6e  uffer xFullPathn
caf0: 61 6d 65 2e 20 7b 48 31 31 31 35 31 7d 20 54 68  ame. {H11151} Th
cb00: 65 20 65 78 61 63 74 20 73 69 7a 65 20 6f 66 20  e exact size of 
cb10: 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65  the output buffe
cb20: 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 70 61 73  r.** is also pas
cb30: 73 65 64 20 61 73 20 61 20 70 61 72 61 6d 65 74  sed as a paramet
cb40: 65 72 20 74 6f 20 62 6f 74 68 20 20 6d 65 74 68  er to both  meth
cb50: 6f 64 73 2e 20 7b 45 4e 44 7d 20 20 49 66 20 74  ods. {END}  If t
cb60: 68 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72  he output buffer
cb70: 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61 72 67 65  .** is not large
cb80: 20 65 6e 6f 75 67 68 2c 20 5b 53 51 4c 49 54 45   enough, [SQLITE
cb90: 5f 43 41 4e 54 4f 50 45 4e 5d 20 73 68 6f 75 6c  _CANTOPEN] shoul
cba0: 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20 53  d be returned. S
cbb0: 69 6e 63 65 20 74 68 69 73 20 69 73 0a 2a 2a 20  ince this is.** 
cbc0: 68 61 6e 64 6c 65 64 20 61 73 20 61 20 66 61 74  handled as a fat
cbd0: 61 6c 20 65 72 72 6f 72 20 62 79 20 53 51 4c 69  al error by SQLi
cbe0: 74 65 2c 20 76 66 73 20 69 6d 70 6c 65 6d 65 6e  te, vfs implemen
cbf0: 74 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20 65  tations should e
cc00: 6e 64 65 61 76 6f 72 0a 2a 2a 20 74 6f 20 70 72  ndeavor.** to pr
cc10: 65 76 65 6e 74 20 74 68 69 73 20 62 79 20 73 65  event this by se
cc20: 74 74 69 6e 67 20 6d 78 50 61 74 68 6e 61 6d 65  tting mxPathname
cc30: 20 74 6f 20 61 20 73 75 66 66 69 63 69 65 6e 74   to a sufficient
cc40: 6c 79 20 6c 61 72 67 65 20 76 61 6c 75 65 2e 0a  ly large value..
cc50: 2a 2a 0a 2a 2a 20 54 68 65 20 78 52 61 6e 64 6f  **.** The xRando
cc60: 6d 6e 65 73 73 28 29 2c 20 78 53 6c 65 65 70 28  mness(), xSleep(
cc70: 29 2c 20 61 6e 64 20 78 43 75 72 72 65 6e 74 54  ), and xCurrentT
cc80: 69 6d 65 28 29 20 69 6e 74 65 72 66 61 63 65 73  ime() interfaces
cc90: 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 73 74 72 69  .** are not stri
cca0: 63 74 6c 79 20 61 20 70 61 72 74 20 6f 66 20 74  ctly a part of t
ccb0: 68 65 20 66 69 6c 65 73 79 73 74 65 6d 2c 20 62  he filesystem, b
ccc0: 75 74 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 69  ut they are.** i
ccd0: 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20 56  ncluded in the V
cce0: 46 53 20 73 74 72 75 63 74 75 72 65 20 66 6f 72  FS structure for
ccf0: 20 63 6f 6d 70 6c 65 74 65 6e 65 73 73 2e 0a 2a   completeness..*
cd00: 2a 20 54 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73  * The xRandomnes
cd10: 73 28 29 20 66 75 6e 63 74 69 6f 6e 20 61 74 74  s() function att
cd20: 65 6d 70 74 73 20 74 6f 20 72 65 74 75 72 6e 20  empts to return 
cd30: 6e 42 79 74 65 73 20 62 79 74 65 73 0a 2a 2a 20  nBytes bytes.** 
cd40: 6f 66 20 67 6f 6f 64 2d 71 75 61 6c 69 74 79 20  of good-quality 
cd50: 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20  randomness into 
cd60: 7a 4f 75 74 2e 20 20 54 68 65 20 72 65 74 75 72  zOut.  The retur
cd70: 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68  n value is.** th
cd80: 65 20 61 63 74 75 61 6c 20 6e 75 6d 62 65 72 20  e actual number 
cd90: 6f 66 20 62 79 74 65 73 20 6f 66 20 72 61 6e 64  of bytes of rand
cda0: 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 2e  omness obtained.
cdb0: 0a 2a 2a 20 54 68 65 20 78 53 6c 65 65 70 28 29  .** The xSleep()
cdc0: 20 6d 65 74 68 6f 64 20 63 61 75 73 65 73 20 74   method causes t
cdd0: 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72 65 61  he calling threa
cde0: 64 20 74 6f 20 73 6c 65 65 70 20 66 6f 72 20 61  d to sleep for a
cdf0: 74 0a 2a 2a 20 6c 65 61 73 74 20 74 68 65 20 6e  t.** least the n
ce00: 75 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65  umber of microse
ce10: 63 6f 6e 64 73 20 67 69 76 65 6e 2e 20 20 54 68  conds given.  Th
ce20: 65 20 78 43 75 72 72 65 6e 74 54 69 6d 65 28 29  e xCurrentTime()
ce30: 0a 2a 2a 20 6d 65 74 68 6f 64 20 72 65 74 75 72  .** method retur
ce40: 6e 73 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20  ns a Julian Day 
ce50: 4e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 63  Number for the c
ce60: 75 72 72 65 6e 74 20 64 61 74 65 20 61 6e 64 20  urrent date and 
ce70: 74 69 6d 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  time..*/.typedef
ce80: 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
ce90: 76 66 73 20 73 71 6c 69 74 65 33 5f 76 66 73 3b  vfs sqlite3_vfs;
cea0: 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
ceb0: 76 66 73 20 7b 0a 20 20 69 6e 74 20 69 56 65 72  vfs {.  int iVer
cec0: 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20  sion;           
ced0: 20 2f 2a 20 53 74 72 75 63 74 75 72 65 20 76 65   /* Structure ve
cee0: 72 73 69 6f 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  rsion number */.
cef0: 20 20 69 6e 74 20 73 7a 4f 73 46 69 6c 65 3b 20    int szOsFile; 
cf00: 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69             /* Si
cf10: 7a 65 20 6f 66 20 73 75 62 63 6c 61 73 73 65 64  ze of subclassed
cf20: 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 2f   sqlite3_file */
cf30: 0a 20 20 69 6e 74 20 6d 78 50 61 74 68 6e 61 6d  .  int mxPathnam
cf40: 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  e;          /* M
cf50: 61 78 69 6d 75 6d 20 66 69 6c 65 20 70 61 74 68  aximum file path
cf60: 6e 61 6d 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  name length */. 
cf70: 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 4e   sqlite3_vfs *pN
cf80: 65 78 74 3b 20 20 20 20 20 20 2f 2a 20 4e 65 78  ext;      /* Nex
cf90: 74 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53  t registered VFS
cfa0: 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
cfb0: 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f   *zName;       /
cfc0: 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20 76  * Name of this v
cfd0: 69 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74  irtual file syst
cfe0: 65 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  em */.  void *pA
cff0: 70 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  ppData;         
d000: 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
d010: 70 70 6c 69 63 61 74 69 6f 6e 2d 73 70 65 63 69  pplication-speci
d020: 66 69 63 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  fic data */.  in
d030: 74 20 28 2a 78 4f 70 65 6e 29 28 73 71 6c 69 74  t (*xOpen)(sqlit
d040: 65 33 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63  e3_vfs*, const c
d050: 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 73 71 6c 69  har *zName, sqli
d060: 74 65 33 5f 66 69 6c 65 2a 2c 0a 20 20 20 20 20  te3_file*,.     
d070: 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c            int fl
d080: 61 67 73 2c 20 69 6e 74 20 2a 70 4f 75 74 46 6c  ags, int *pOutFl
d090: 61 67 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78 44  ags);.  int (*xD
d0a0: 65 6c 65 74 65 29 28 73 71 6c 69 74 65 33 5f 76  elete)(sqlite3_v
d0b0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
d0c0: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 73 79 6e 63  *zName, int sync
d0d0: 44 69 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 41  Dir);.  int (*xA
d0e0: 63 63 65 73 73 29 28 73 71 6c 69 74 65 33 5f 76  ccess)(sqlite3_v
d0f0: 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fs*, const char 
d100: 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67  *zName, int flag
d110: 73 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29  s, int *pResOut)
d120: 3b 0a 20 20 69 6e 74 20 28 2a 78 46 75 6c 6c 50  ;.  int (*xFullP
d130: 61 74 68 6e 61 6d 65 29 28 73 71 6c 69 74 65 33  athname)(sqlite3
d140: 5f 76 66 73 2a 2c 20 63 6f 6e 73 74 20 63 68 61  _vfs*, const cha
d150: 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e 74 20 6e 4f  r *zName, int nO
d160: 75 74 2c 20 63 68 61 72 20 2a 7a 4f 75 74 29 3b  ut, char *zOut);
d170: 0a 20 20 76 6f 69 64 20 2a 28 2a 78 44 6c 4f 70  .  void *(*xDlOp
d180: 65 6e 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a  en)(sqlite3_vfs*
d190: 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  , const char *zF
d1a0: 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 76 6f 69 64  ilename);.  void
d1b0: 20 28 2a 78 44 6c 45 72 72 6f 72 29 28 73 71 6c   (*xDlError)(sql
d1c0: 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 20 6e  ite3_vfs*, int n
d1d0: 42 79 74 65 2c 20 63 68 61 72 20 2a 7a 45 72 72  Byte, char *zErr
d1e0: 4d 73 67 29 3b 0a 20 20 76 6f 69 64 20 2a 28 2a  Msg);.  void *(*
d1f0: 78 44 6c 53 79 6d 29 28 73 71 6c 69 74 65 33 5f  xDlSym)(sqlite3_
d200: 76 66 73 2a 2c 76 6f 69 64 2a 2c 20 63 6f 6e 73  vfs*,void*, cons
d210: 74 20 63 68 61 72 20 2a 7a 53 79 6d 62 6f 6c 29  t char *zSymbol)
d220: 3b 0a 20 20 76 6f 69 64 20 28 2a 78 44 6c 43 6c  ;.  void (*xDlCl
d230: 6f 73 65 29 28 73 71 6c 69 74 65 33 5f 76 66 73  ose)(sqlite3_vfs
d240: 2a 2c 20 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74  *, void*);.  int
d250: 20 28 2a 78 52 61 6e 64 6f 6d 6e 65 73 73 29 28   (*xRandomness)(
d260: 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c 20 69 6e  sqlite3_vfs*, in
d270: 74 20 6e 42 79 74 65 2c 20 63 68 61 72 20 2a 7a  t nByte, char *z
d280: 4f 75 74 29 3b 0a 20 20 69 6e 74 20 28 2a 78 53  Out);.  int (*xS
d290: 6c 65 65 70 29 28 73 71 6c 69 74 65 33 5f 76 66  leep)(sqlite3_vf
d2a0: 73 2a 2c 20 69 6e 74 20 6d 69 63 72 6f 73 65 63  s*, int microsec
d2b0: 6f 6e 64 73 29 3b 0a 20 20 69 6e 74 20 28 2a 78  onds);.  int (*x
d2c0: 43 75 72 72 65 6e 74 54 69 6d 65 29 28 73 71 6c  CurrentTime)(sql
d2d0: 69 74 65 33 5f 76 66 73 2a 2c 20 64 6f 75 62 6c  ite3_vfs*, doubl
d2e0: 65 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 47 65  e*);.  int (*xGe
d2f0: 74 4c 61 73 74 45 72 72 6f 72 29 28 73 71 6c 69  tLastError)(sqli
d300: 74 65 33 5f 76 66 73 2a 2c 20 69 6e 74 2c 20 63  te3_vfs*, int, c
d310: 68 61 72 20 2a 29 3b 0a 20 20 2f 2a 20 4e 65 77  har *);.  /* New
d320: 20 66 69 65 6c 64 73 20 6d 61 79 20 62 65 20 61   fields may be a
d330: 70 70 65 6e 64 65 64 20 69 6e 20 66 69 67 75 72  ppended in figur
d340: 65 20 76 65 72 73 69 6f 6e 73 2e 20 20 54 68 65  e versions.  The
d350: 20 69 56 65 72 73 69 6f 6e 0a 20 20 2a 2a 20 76   iVersion.  ** v
d360: 61 6c 75 65 20 77 69 6c 6c 20 69 6e 63 72 65 6d  alue will increm
d370: 65 6e 74 20 77 68 65 6e 65 76 65 72 20 74 68 69  ent whenever thi
d380: 73 20 68 61 70 70 65 6e 73 2e 20 2a 2f 0a 7d 3b  s happens. */.};
d390: 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
d3a0: 3a 20 46 6c 61 67 73 20 66 6f 72 20 74 68 65 20  : Flags for the 
d3b0: 78 41 63 63 65 73 73 20 56 46 53 20 6d 65 74 68  xAccess VFS meth
d3c0: 6f 64 20 7b 48 31 31 31 39 30 7d 20 3c 48 31 31  od {H11190} <H11
d3d0: 31 34 30 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 31  140>.**.** {H111
d3e0: 39 31 7d 20 54 68 65 73 65 20 69 6e 74 65 67 65  91} These intege
d3f0: 72 20 63 6f 6e 73 74 61 6e 74 73 20 63 61 6e 20  r constants can 
d400: 62 65 20 75 73 65 64 20 61 73 20 74 68 65 20 74  be used as the t
d410: 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
d420: 6f 0a 2a 2a 20 74 68 65 20 78 41 63 63 65 73 73  o.** the xAccess
d430: 20 6d 65 74 68 6f 64 20 6f 66 20 61 6e 20 5b 73   method of an [s
d440: 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
d450: 63 74 2e 20 7b 45 4e 44 7d 20 20 54 68 65 79 20  ct. {END}  They 
d460: 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77 68 61  determine.** wha
d470: 74 20 6b 69 6e 64 20 6f 66 20 70 65 72 6d 69 73  t kind of permis
d480: 73 69 6f 6e 73 20 74 68 65 20 78 41 63 63 65 73  sions the xAcces
d490: 73 20 6d 65 74 68 6f 64 20 69 73 20 6c 6f 6f 6b  s method is look
d4a0: 69 6e 67 20 66 6f 72 2e 0a 2a 2a 20 7b 48 31 31  ing for..** {H11
d4b0: 31 39 32 7d 20 57 69 74 68 20 53 51 4c 49 54 45  192} With SQLITE
d4c0: 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 2c 20  _ACCESS_EXISTS, 
d4d0: 74 68 65 20 78 41 63 63 65 73 73 20 6d 65 74 68  the xAccess meth
d4e0: 6f 64 0a 2a 2a 20 73 69 6d 70 6c 79 20 63 68 65  od.** simply che
d4f0: 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20  cks whether the 
d500: 66 69 6c 65 20 65 78 69 73 74 73 2e 0a 2a 2a 20  file exists..** 
d510: 7b 48 31 31 31 39 33 7d 20 57 69 74 68 20 53 51  {H11193} With SQ
d520: 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44  LITE_ACCESS_READ
d530: 57 52 49 54 45 2c 20 74 68 65 20 78 41 63 63 65  WRITE, the xAcce
d540: 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63 68 65  ss method.** che
d550: 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 65 20  cks whether the 
d560: 66 69 6c 65 20 69 73 20 62 6f 74 68 20 72 65 61  file is both rea
d570: 64 61 62 6c 65 20 61 6e 64 20 77 72 69 74 61 62  dable and writab
d580: 6c 65 2e 0a 2a 2a 20 7b 48 31 31 31 39 34 7d 20  le..** {H11194} 
d590: 57 69 74 68 20 53 51 4c 49 54 45 5f 41 43 43 45  With SQLITE_ACCE
d5a0: 53 53 5f 52 45 41 44 2c 20 74 68 65 20 78 41 63  SS_READ, the xAc
d5b0: 63 65 73 73 20 6d 65 74 68 6f 64 0a 2a 2a 20 63  cess method.** c
d5c0: 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68  hecks whether th
d5d0: 65 20 66 69 6c 65 20 69 73 20 72 65 61 64 61 62  e file is readab
d5e0: 6c 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  le..*/.#define S
d5f0: 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
d600: 53 54 53 20 20 20 20 30 0a 23 64 65 66 69 6e 65  STS    0.#define
d610: 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
d620: 45 41 44 57 52 49 54 45 20 31 0a 23 64 65 66 69  EADWRITE 1.#defi
d630: 6e 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  ne SQLITE_ACCESS
d640: 5f 52 45 41 44 20 20 20 20 20 20 32 0a 0a 2f 2a  _READ      2../*
d650: 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 49 6e  .** CAPI3REF: In
d660: 69 74 69 61 6c 69 7a 65 20 54 68 65 20 53 51 4c  itialize The SQL
d670: 69 74 65 20 4c 69 62 72 61 72 79 20 7b 48 31 30  ite Library {H10
d680: 31 33 30 7d 20 3c 53 32 30 30 30 30 3e 3c 53 33  130} <S20000><S3
d690: 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0100>.**.** The 
d6a0: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d6b0: 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 69 6e 69  ze() routine ini
d6c0: 74 69 61 6c 69 7a 65 73 20 74 68 65 0a 2a 2a 20  tializes the.** 
d6d0: 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20  SQLite library. 
d6e0: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 73 68 75   The sqlite3_shu
d6f0: 74 64 6f 77 6e 28 29 20 72 6f 75 74 69 6e 65 0a  tdown() routine.
d700: 2a 2a 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61  ** deallocates a
d710: 6e 79 20 72 65 73 6f 75 72 63 65 73 20 74 68 61  ny resources tha
d720: 74 20 77 65 72 65 20 61 6c 6c 6f 63 61 74 65 64  t were allocated
d730: 20 62 79 20 73 71 6c 69 74 65 33 5f 69 6e 69 74   by sqlite3_init
d740: 69 61 6c 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  ialize()..**.** 
d750: 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  A call to sqlite
d760: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
d770: 73 20 61 6e 20 22 65 66 66 65 63 74 69 76 65 22  s an "effective"
d780: 20 63 61 6c 6c 20 69 66 20 69 74 20 69 73 0a 2a   call if it is.*
d790: 2a 20 74 68 65 20 66 69 72 73 74 20 74 69 6d 65  * the first time
d7a0: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
d7b0: 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64  ize() is invoked
d7c0: 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65   during the life
d7d0: 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 70  time of.** the p
d7e0: 72 6f 63 65 73 73 2c 20 6f 72 20 69 66 20 69 74  rocess, or if it
d7f0: 20 69 73 20 74 68 65 20 66 69 72 73 74 20 74 69   is the first ti
d800: 6d 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69  me sqlite3_initi
d810: 61 6c 69 7a 65 28 29 20 69 73 20 69 6e 76 6f 6b  alize() is invok
d820: 65 64 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20  ed.** following 
d830: 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
d840: 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20 4f  3_shutdown().  O
d850: 6e 6c 79 20 61 6e 20 65 66 66 65 63 74 69 76 65  nly an effective
d860: 20 63 61 6c 6c 0a 2a 2a 20 6f 66 20 73 71 6c 69   call.** of sqli
d870: 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29  te3_initialize()
d880: 20 64 6f 65 73 20 61 6e 79 20 69 6e 69 74 69 61   does any initia
d890: 6c 69 7a 61 74 69 6f 6e 2e 20 20 41 6c 6c 20 6f  lization.  All o
d8a0: 74 68 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 61 72  ther calls.** ar
d8b0: 65 20 68 61 72 6d 6c 65 73 73 20 6e 6f 2d 6f 70  e harmless no-op
d8c0: 73 2e 0a 2a 2a 0a 2a 2a 20 41 6d 6f 6e 67 20 6f  s..**.** Among o
d8d0: 74 68 65 72 20 74 68 69 6e 67 73 2c 20 73 71 6c  ther things, sql
d8e0: 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
d8f0: 29 20 73 68 61 6c 6c 20 69 6e 76 6f 6b 65 0a 2a  ) shall invoke.*
d900: 2a 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  * sqlite3_os_ini
d910: 74 28 29 2e 20 20 53 69 6d 69 6c 61 72 6c 79 2c  t().  Similarly,
d920: 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77   sqlite3_shutdow
d930: 6e 28 29 0a 2a 2a 20 73 68 61 6c 6c 20 69 6e 76  n().** shall inv
d940: 6f 6b 65 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65  oke sqlite3_os_e
d950: 6e 64 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  nd()..**.** The 
d960: 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
d970: 7a 65 28 29 20 72 6f 75 74 69 6e 65 20 72 65 74  ze() routine ret
d980: 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  urns [SQLITE_OK]
d990: 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 20   on success..** 
d9a0: 49 66 20 66 6f 72 20 73 6f 6d 65 20 72 65 61 73  If for some reas
d9b0: 6f 6e 2c 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  on, sqlite3_init
d9c0: 69 61 6c 69 7a 65 28 29 20 69 73 20 75 6e 61 62  ialize() is unab
d9d0: 6c 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65  le to initialize
d9e0: 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20  .** the library 
d9f0: 28 70 65 72 68 61 70 73 20 69 74 20 69 73 20 75  (perhaps it is u
da00: 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74  nable to allocat
da10: 65 20 61 20 6e 65 65 64 65 64 20 72 65 73 6f 75  e a needed resou
da20: 72 63 65 20 73 75 63 68 0a 2a 2a 20 61 73 20 61  rce such.** as a
da30: 20 6d 75 74 65 78 29 20 69 74 20 72 65 74 75 72   mutex) it retur
da40: 6e 73 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64  ns an [error cod
da50: 65 5d 20 6f 74 68 65 72 20 74 68 61 6e 20 5b 53  e] other than [S
da60: 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a  QLITE_OK]..**.**
da70: 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 69   The sqlite3_ini
da80: 74 69 61 6c 69 7a 65 28 29 20 72 6f 75 74 69 6e  tialize() routin
da90: 65 20 69 73 20 63 61 6c 6c 65 64 20 69 6e 74 65  e is called inte
daa0: 72 6e 61 6c 6c 79 20 62 79 20 6d 61 6e 79 20 6f  rnally by many o
dab0: 74 68 65 72 0a 2a 2a 20 53 51 4c 69 74 65 20 69  ther.** SQLite i
dac0: 6e 74 65 72 66 61 63 65 73 20 73 6f 20 74 68 61  nterfaces so tha
dad0: 74 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  t an application
dae0: 20 75 73 75 61 6c 6c 79 20 64 6f 65 73 20 6e 6f   usually does no
daf0: 74 20 6e 65 65 64 20 74 6f 0a 2a 2a 20 69 6e 76  t need to.** inv
db00: 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  oke sqlite3_init
db10: 69 61 6c 69 7a 65 28 29 20 64 69 72 65 63 74 6c  ialize() directl
db20: 79 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  y.  For example,
db30: 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
db40: 5d 0a 2a 2a 20 63 61 6c 6c 73 20 73 71 6c 69 74  ].** calls sqlit
db50: 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
db60: 73 6f 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  so the SQLite li
db70: 62 72 61 72 79 20 77 69 6c 6c 20 62 65 20 61 75  brary will be au
db80: 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 69  tomatically.** i
db90: 6e 69 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20  nitialized when 
dba0: 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d  [sqlite3_open()]
dbb0: 20 69 73 20 63 61 6c 6c 65 64 20 69 66 20 69 74   is called if it
dbc0: 20 68 61 73 20 6e 6f 74 20 62 65 20 69 6e 69 74   has not be init
dbd0: 69 61 6c 69 7a 65 64 0a 2a 2a 20 61 6c 72 65 61  ialized.** alrea
dbe0: 64 79 2e 20 20 48 6f 77 65 76 65 72 2c 20 69 66  dy.  However, if
dbf0: 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
dc00: 6c 65 64 20 77 69 74 68 20 74 68 65 20 5b 53 51  led with the [SQ
dc10: 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
dc20: 49 54 5d 0a 2a 2a 20 63 6f 6d 70 69 6c 65 2d 74  IT].** compile-t
dc30: 69 6d 65 20 6f 70 74 69 6f 6e 2c 20 74 68 65 6e  ime option, then
dc40: 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63 20 63   the automatic c
dc50: 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  alls to sqlite3_
dc60: 69 6e 69 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20  initialize().** 
dc70: 61 72 65 20 6f 6d 69 74 74 65 64 20 61 6e 64 20  are omitted and 
dc80: 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  the application 
dc90: 6d 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65  must call sqlite
dca0: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 64  3_initialize() d
dcb0: 69 72 65 63 74 6c 79 0a 2a 2a 20 70 72 69 6f 72  irectly.** prior
dcc0: 20 74 6f 20 75 73 69 6e 67 20 61 6e 79 20 6f 74   to using any ot
dcd0: 68 65 72 20 53 51 4c 69 74 65 20 69 6e 74 65 72  her SQLite inter
dce0: 66 61 63 65 2e 20 20 46 6f 72 20 6d 61 78 69 6d  face.  For maxim
dcf0: 75 6d 20 70 6f 72 74 61 62 69 6c 69 74 79 2c 0a  um portability,.
dd00: 2a 2a 20 69 74 20 69 73 20 72 65 63 6f 6d 6d 65  ** it is recomme
dd10: 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c 69 63  nded that applic
dd20: 61 74 69 6f 6e 73 20 61 6c 77 61 79 73 20 69 6e  ations always in
dd30: 76 6f 6b 65 20 73 71 6c 69 74 65 33 5f 69 6e 69  voke sqlite3_ini
dd40: 74 69 61 6c 69 7a 65 28 29 0a 2a 2a 20 64 69 72  tialize().** dir
dd50: 65 63 74 6c 79 20 70 72 69 6f 72 20 74 6f 20 75  ectly prior to u
dd60: 73 69 6e 67 20 61 6e 79 20 6f 74 68 65 72 20 53  sing any other S
dd70: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 2e  QLite interface.
dd80: 20 20 46 75 74 75 72 65 20 72 65 6c 65 61 73 65    Future release
dd90: 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65 20 6d  s.** of SQLite m
dda0: 61 79 20 72 65 71 75 69 72 65 20 74 68 69 73 2e  ay require this.
ddb0: 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
ddc0: 2c 20 74 68 65 20 62 65 68 61 76 69 6f 72 20 65  , the behavior e
ddd0: 78 68 69 62 69 74 65 64 0a 2a 2a 20 77 68 65 6e  xhibited.** when
dde0: 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
ddf0: 6c 65 64 20 77 69 74 68 20 5b 53 51 4c 49 54 45  led with [SQLITE
de00: 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 5d 20  _OMIT_AUTOINIT] 
de10: 6d 69 67 68 74 20 62 65 63 6f 6d 65 20 74 68 65  might become the
de20: 0a 2a 2a 20 64 65 66 61 75 6c 74 20 62 65 68 61  .** default beha
de30: 76 69 6f 72 20 69 6e 20 73 6f 6d 65 20 66 75 74  vior in some fut
de40: 75 72 65 20 72 65 6c 65 61 73 65 20 6f 66 20 53  ure release of S
de50: 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  QLite..**.** The
de60: 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74   sqlite3_os_init
de70: 28 29 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20  () routine does 
de80: 6f 70 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d  operating-system
de90: 20 73 70 65 63 69 66 69 63 0a 2a 2a 20 69 6e 69   specific.** ini
dea0: 74 69 61 6c 69 7a 61 74 69 6f 6e 20 6f 66 20 74  tialization of t
deb0: 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72  he SQLite librar
dec0: 79 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  y.  The sqlite3_
ded0: 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 72 6f 75 74  os_end().** rout
dee0: 69 6e 65 20 75 6e 64 6f 65 73 20 74 68 65 20 65  ine undoes the e
def0: 66 66 65 63 74 20 6f 66 20 73 71 6c 69 74 65 33  ffect of sqlite3
df00: 5f 6f 73 5f 69 6e 69 74 28 29 2e 20 20 54 79 70  _os_init().  Typ
df10: 69 63 61 6c 20 74 61 73 6b 73 0a 2a 2a 20 70 65  ical tasks.** pe
df20: 72 66 6f 72 6d 65 64 20 62 79 20 74 68 65 73 65  rformed by these
df30: 20 72 6f 75 74 69 6e 65 73 20 69 6e 63 6c 75 64   routines includ
df40: 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f 72 20  e allocation or 
df50: 64 65 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20  deallocation.** 
df60: 6f 66 20 73 74 61 74 69 63 20 72 65 73 6f 75 72  of static resour
df70: 63 65 73 2c 20 69 6e 69 74 69 61 6c 69 7a 61 74  ces, initializat
df80: 69 6f 6e 20 6f 66 20 67 6c 6f 62 61 6c 20 76 61  ion of global va
df90: 72 69 61 62 6c 65 73 2c 0a 2a 2a 20 73 65 74 74  riables,.** sett
dfa0: 69 6e 67 20 75 70 20 61 20 64 65 66 61 75 6c 74  ing up a default
dfb0: 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6d   [sqlite3_vfs] m
dfc0: 6f 64 75 6c 65 2c 20 6f 72 20 73 65 74 74 69 6e  odule, or settin
dfd0: 67 20 75 70 0a 2a 2a 20 61 20 64 65 66 61 75 6c  g up.** a defaul
dfe0: 74 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20  t configuration 
dff0: 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63  using [sqlite3_c
e000: 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  onfig()]..**.** 
e010: 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  The application 
e020: 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 69 6e 76  should never inv
e030: 6f 6b 65 20 65 69 74 68 65 72 20 73 71 6c 69 74  oke either sqlit
e040: 65 33 5f 6f 73 5f 69 6e 69 74 28 29 0a 2a 2a 20  e3_os_init().** 
e050: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e  or sqlite3_os_en
e060: 64 28 29 20 64 69 72 65 63 74 6c 79 2e 20 20 54  d() directly.  T
e070: 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 73  he application s
e080: 68 6f 75 6c 64 20 6f 6e 6c 79 20 69 6e 76 6f 6b  hould only invok
e090: 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69  e.** sqlite3_ini
e0a0: 74 69 61 6c 69 7a 65 28 29 20 61 6e 64 20 73 71  tialize() and sq
e0b0: 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
e0c0: 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6f  .  The sqlite3_o
e0d0: 73 5f 69 6e 69 74 28 29 0a 2a 2a 20 69 6e 74 65  s_init().** inte
e0e0: 72 66 61 63 65 20 69 73 20 63 61 6c 6c 65 64 20  rface is called 
e0f0: 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79  automatically by
e100: 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
e110: 69 7a 65 28 29 20 61 6e 64 0a 2a 2a 20 73 71 6c  ize() and.** sql
e120: 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 20 69 73  ite3_os_end() is
e130: 20 63 61 6c 6c 65 64 20 62 79 20 73 71 6c 69 74   called by sqlit
e140: 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 2e 20 20  e3_shutdown().  
e150: 41 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 69  Appropriate.** i
e160: 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66  mplementations f
e170: 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e  or sqlite3_os_in
e180: 69 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33  it() and sqlite3
e190: 5f 6f 73 5f 65 6e 64 28 29 0a 2a 2a 20 61 72 65  _os_end().** are
e1a0: 20 62 75 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69   built into SQLi
e1b0: 74 65 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f  te when it is co
e1c0: 6d 70 69 6c 65 64 20 66 6f 72 20 75 6e 69 78 2c  mpiled for unix,
e1d0: 20 77 69 6e 64 6f 77 73 2c 20 6f 72 20 6f 73 2f   windows, or os/
e1e0: 32 2e 0a 2a 2a 20 57 68 65 6e 20 62 75 69 6c 74  2..** When built
e1f0: 20 66 6f 72 20 6f 74 68 65 72 20 70 6c 61 74 66   for other platf
e200: 6f 72 6d 73 20 28 75 73 69 6e 67 20 74 68 65 20  orms (using the 
e210: 5b 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52  [SQLITE_OS_OTHER
e220: 3d 31 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  =1] compile-time
e230: 0a 2a 2a 20 6f 70 74 69 6f 6e 29 20 74 68 65 20  .** option) the 
e240: 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75 73 74  application must
e250: 20 73 75 70 70 6c 79 20 61 20 73 75 69 74 61 62   supply a suitab
e260: 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  le implementatio
e270: 6e 20 66 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33  n for.** sqlite3
e280: 5f 6f 73 5f 69 6e 69 74 28 29 20 61 6e 64 20 73  _os_init() and s
e290: 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 29 2e  qlite3_os_end().
e2a0: 20 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e    An application
e2b0: 2d 73 75 70 70 6c 69 65 64 0a 2a 2a 20 69 6d 70  -supplied.** imp
e2c0: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 73  lementation of s
e2d0: 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29  qlite3_os_init()
e2e0: 20 6f 72 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65   or sqlite3_os_e
e2f0: 6e 64 28 29 0a 2a 2a 20 6d 75 73 74 20 72 65 74  nd().** must ret
e300: 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  urn [SQLITE_OK] 
e310: 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 73  on success and s
e320: 6f 6d 65 20 6f 74 68 65 72 20 5b 65 72 72 6f 72  ome other [error
e330: 20 63 6f 64 65 5d 20 75 70 6f 6e 0a 2a 2a 20 66   code] upon.** f
e340: 61 69 6c 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54  ailure..*/.SQLIT
e350: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
e360: 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f 69  3_initialize(voi
e370: 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  d);.SQLITE_API i
e380: 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75 74 64  nt sqlite3_shutd
e390: 6f 77 6e 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  own(void);.SQLIT
e3a0: 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
e3b0: 33 5f 6f 73 5f 69 6e 69 74 28 76 6f 69 64 29 3b  3_os_init(void);
e3c0: 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
e3d0: 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76  sqlite3_os_end(v
e3e0: 6f 69 64 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  oid);../*.** CAP
e3f0: 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 69  I3REF: Configuri
e400: 6e 67 20 54 68 65 20 53 51 4c 69 74 65 20 4c 69  ng The SQLite Li
e410: 62 72 61 72 79 20 7b 48 31 34 31 30 30 7d 20 3c  brary {H14100} <
e420: 53 32 30 30 30 30 3e 3c 53 33 30 32 30 30 3e 0a  S20000><S30200>.
e430: 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
e440: 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
e450: 33 5f 63 6f 6e 66 69 67 28 29 20 69 6e 74 65 72  3_config() inter
e460: 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f 20  face is used to 
e470: 6d 61 6b 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66  make global conf
e480: 69 67 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61  iguration.** cha
e490: 6e 67 65 73 20 74 6f 20 53 51 4c 69 74 65 20 69  nges to SQLite i
e4a0: 6e 20 6f 72 64 65 72 20 74 6f 20 74 75 6e 65 20  n order to tune 
e4b0: 53 51 4c 69 74 65 20 74 6f 20 74 68 65 20 73 70  SQLite to the sp
e4c0: 65 63 69 66 69 63 20 6e 65 65 64 73 20 6f 66 0a  ecific needs of.
e4d0: 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ** the applicati
e4e0: 6f 6e 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74  on.  The default
e4f0: 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 69   configuration i
e500: 73 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f  s recommended fo
e510: 72 20 6d 6f 73 74 0a 2a 2a 20 61 70 70 6c 69 63  r most.** applic
e520: 61 74 69 6f 6e 73 20 61 6e 64 20 73 6f 20 74 68  ations and so th
e530: 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
e540: 75 61 6c 6c 79 20 6e 6f 74 20 6e 65 63 65 73 73  ually not necess
e550: 61 72 79 2e 20 20 49 74 20 69 73 0a 2a 2a 20 70  ary.  It is.** p
e560: 72 6f 76 69 64 65 64 20 74 6f 20 73 75 70 70 6f  rovided to suppo
e570: 72 74 20 72 61 72 65 20 61 70 70 6c 69 63 61 74  rt rare applicat
e580: 69 6f 6e 73 20 77 69 74 68 20 75 6e 75 73 75 61  ions with unusua
e590: 6c 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54  l needs..**.** T
e5a0: 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  he sqlite3_confi
e5b0: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  g() interface is
e5c0: 20 6e 6f 74 20 74 68 72 65 61 64 73 61 66 65 2e   not threadsafe.
e5d0: 20 20 54 68 65 20 61 70 70 6c 69 63 61 74 69 6f    The applicatio
e5e0: 6e 0a 2a 2a 20 6d 75 73 74 20 69 6e 73 75 72 65  n.** must insure
e5f0: 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 53   that no other S
e600: 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65 73  QLite interfaces
e610: 20 61 72 65 20 69 6e 76 6f 6b 65 64 20 62 79 20   are invoked by 
e620: 6f 74 68 65 72 0a 2a 2a 20 74 68 72 65 61 64 73  other.** threads
e630: 20 77 68 69 6c 65 20 73 71 6c 69 74 65 33 5f 63   while sqlite3_c
e640: 6f 6e 66 69 67 28 29 20 69 73 20 72 75 6e 6e 69  onfig() is runni
e650: 6e 67 2e 20 20 46 75 72 74 68 65 72 6d 6f 72 65  ng.  Furthermore
e660: 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67  , sqlite3_config
e670: 28 29 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62  ().** may only b
e680: 65 20 69 6e 76 6f 6b 65 64 20 70 72 69 6f 72 20  e invoked prior 
e690: 74 6f 20 6c 69 62 72 61 72 79 20 69 6e 69 74 69  to library initi
e6a0: 61 6c 69 7a 61 74 69 6f 6e 20 75 73 69 6e 67 0a  alization using.
e6b0: 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69 74  ** [sqlite3_init
e6c0: 69 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66 74  ialize()] or aft
e6d0: 65 72 20 73 68 75 74 64 6f 77 6e 20 62 79 20 5b  er shutdown by [
e6e0: 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
e6f0: 28 29 5d 2e 0a 2a 2a 20 4e 6f 74 65 2c 20 68 6f  ()]..** Note, ho
e700: 77 65 76 65 72 2c 20 74 68 61 74 20 73 71 6c 69  wever, that sqli
e710: 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 63 61 6e  te3_config() can
e720: 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20 70 61   be called as pa
e730: 72 74 20 6f 66 20 74 68 65 0a 2a 2a 20 69 6d 70  rt of the.** imp
e740: 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  lementation of a
e750: 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  n application-de
e760: 66 69 6e 65 64 20 5b 73 71 6c 69 74 65 33 5f 6f  fined [sqlite3_o
e770: 73 5f 69 6e 69 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a  s_init()]..**.**
e780: 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
e790: 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  ent to sqlite3_c
e7a0: 6f 6e 66 69 67 28 29 20 69 73 20 61 6e 20 69 6e  onfig() is an in
e7b0: 74 65 67 65 72 0a 2a 2a 20 5b 53 51 4c 49 54 45  teger.** [SQLITE
e7c0: 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48  _CONFIG_SINGLETH
e7d0: 52 45 41 44 20 7c 20 63 6f 6e 66 69 67 75 72 61  READ | configura
e7e0: 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 20 74 68 61  tion option] tha
e7f0: 74 20 64 65 74 65 72 6d 69 6e 65 73 0a 2a 2a 20  t determines.** 
e800: 77 68 61 74 20 70 72 6f 70 65 72 74 79 20 6f 66  what property of
e810: 20 53 51 4c 69 74 65 20 69 73 20 74 6f 20 62 65   SQLite is to be
e820: 20 63 6f 6e 66 69 67 75 72 65 64 2e 20 20 53 75   configured.  Su
e830: 62 73 65 71 75 65 6e 74 20 61 72 67 75 6d 65 6e  bsequent argumen
e840: 74 73 0a 2a 2a 20 76 61 72 79 20 64 65 70 65 6e  ts.** vary depen
e850: 64 69 6e 67 20 6f 6e 20 74 68 65 20 5b 53 51 4c  ding on the [SQL
e860: 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c  ITE_CONFIG_SINGL
e870: 45 54 48 52 45 41 44 20 7c 20 63 6f 6e 66 69 67  ETHREAD | config
e880: 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 5d 0a  uration option].
e890: 2a 2a 20 69 6e 20 74 68 65 20 66 69 72 73 74 20  ** in the first 
e8a0: 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
e8b0: 57 68 65 6e 20 61 20 63 6f 6e 66 69 67 75 72 61  When a configura
e8c0: 74 69 6f 6e 20 6f 70 74 69 6f 6e 20 69 73 20 73  tion option is s
e8d0: 65 74 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66  et, sqlite3_conf
e8e0: 69 67 28 29 20 72 65 74 75 72 6e 73 20 5b 53 51  ig() returns [SQ
e8f0: 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 20 49 66 20  LITE_OK]..** If 
e900: 74 68 65 20 6f 70 74 69 6f 6e 20 69 73 20 75 6e  the option is un
e910: 6b 6e 6f 77 6e 20 6f 72 20 53 51 4c 69 74 65 20  known or SQLite 
e920: 69 73 20 75 6e 61 62 6c 65 20 74 6f 20 73 65 74  is unable to set
e930: 20 74 68 65 20 6f 70 74 69 6f 6e 0a 2a 2a 20 74   the option.** t
e940: 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
e950: 20 72 65 74 75 72 6e 73 20 61 20 6e 6f 6e 2d 7a   returns a non-z
e960: 65 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d  ero [error code]
e970: 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
e980: 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 30  TS:.**.** {H1410
e990: 33 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  3} A successful 
e9a0: 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73  invocation of [s
e9b0: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d  qlite3_config()]
e9c0: 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a   shall return.**
e9d0: 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
e9e0: 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  E_OK]..**.** {H1
e9f0: 34 31 30 36 7d 20 54 68 65 20 5b 73 71 6c 69 74  4106} The [sqlit
ea00: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74  e3_config()] int
ea10: 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74  erface shall ret
ea20: 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55  urn [SQLITE_MISU
ea30: 53 45 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  SE].**          
ea40: 69 66 20 69 74 20 69 73 20 69 6e 76 6f 6b 65 64  if it is invoked
ea50: 20 69 6e 20 62 65 74 77 65 65 6e 20 63 61 6c 6c   in between call
ea60: 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e  s to [sqlite3_in
ea70: 69 74 69 61 6c 69 7a 65 28 29 5d 20 61 6e 64 0a  itialize()] and.
ea80: 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
ea90: 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29 5d  ite3_shutdown()]
eaa0: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 30 7d  ..**.** {H14120}
eab0: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   A successful ca
eac0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
ead0: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
eae0: 4f 4e 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45  ONFIG_SINGLETHRE
eaf0: 41 44 5d 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  AD]).**         
eb00: 20 73 68 61 6c 6c 20 73 65 74 20 74 68 65 20 64   shall set the d
eb10: 65 66 61 75 6c 74 20 5b 74 68 72 65 61 64 69 6e  efault [threadin
eb20: 67 20 6d 6f 64 65 5d 20 74 6f 20 53 69 6e 67 6c  g mode] to Singl
eb30: 65 2d 74 68 72 65 61 64 2e 0a 2a 2a 0a 2a 2a 20  e-thread..**.** 
eb40: 7b 48 31 34 31 32 33 7d 20 41 20 73 75 63 63 65  {H14123} A succe
eb50: 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
eb60: 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 5d 28 5b  qlite3_config]([
eb70: 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55  SQLITE_CONFIG_MU
eb80: 4c 54 49 54 48 52 45 41 44 5d 29 0a 2a 2a 20 20  LTITHREAD]).**  
eb90: 20 20 20 20 20 20 20 20 73 68 61 6c 6c 20 73 65          shall se
eba0: 74 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 74  t the default [t
ebb0: 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 74  hreading mode] t
ebc0: 6f 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 2e 0a  o Multi-thread..
ebd0: 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 36 7d 20 41  **.** {H14126} A
ebe0: 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
ebf0: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
ec00: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e  fig]([SQLITE_CON
ec10: 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 5d 29  FIG_SERIALIZED])
ec20: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61  .**          sha
ec30: 6c 6c 20 73 65 74 20 74 68 65 20 64 65 66 61 75  ll set the defau
ec40: 6c 74 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f  lt [threading mo
ec50: 64 65 5d 20 74 6f 20 53 65 72 69 61 6c 69 7a 65  de] to Serialize
ec60: 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 32 39  d..**.** {H14129
ec70: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
ec80: 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
ec90: 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f  config]([SQLITE_
eca0: 43 4f 4e 46 49 47 5f 4d 55 54 45 58 5d 2c 58 29  CONFIG_MUTEX],X)
ecb0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65  .**          whe
ecc0: 72 65 20 58 20 69 73 20 61 20 70 6f 69 6e 74 65  re X is a pointe
ecd0: 72 20 74 6f 20 61 6e 20 69 6e 69 74 69 61 6c 69  r to an initiali
ece0: 7a 65 64 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74  zed [sqlite3_mut
ecf0: 65 78 5f 6d 65 74 68 6f 64 73 5d 0a 2a 2a 20 20  ex_methods].**  
ed00: 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20 73          object s
ed10: 68 61 6c 6c 20 63 61 75 73 65 20 61 6c 6c 20 73  hall cause all s
ed20: 75 62 73 65 71 75 65 6e 74 20 6d 75 74 65 78 20  ubsequent mutex 
ed30: 6f 70 65 72 61 74 69 6f 6e 73 20 70 65 72 66 6f  operations perfo
ed40: 72 6d 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  rmed.**         
ed50: 20 62 79 20 53 51 4c 69 74 65 20 74 6f 20 75 73   by SQLite to us
ed60: 65 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74 68  e the mutex meth
ed70: 6f 64 73 20 74 68 61 74 20 77 65 72 65 20 70 72  ods that were pr
ed80: 65 73 65 6e 74 20 69 6e 20 58 0a 2a 2a 20 20 20  esent in X.**   
ed90: 20 20 20 20 20 20 20 64 75 72 69 6e 67 20 74 68         during th
eda0: 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
edb0: 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a 2a 2a  e3_config()]..**
edc0: 0a 2a 2a 20 7b 48 31 34 31 33 32 7d 20 41 20 73  .** {H14132} A s
edd0: 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
ede0: 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  o [sqlite3_confi
edf0: 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  g]([SQLITE_CONFI
ee00: 47 5f 47 45 54 4d 55 54 45 58 5d 2c 58 29 0a 2a  G_GETMUTEX],X).*
ee10: 2a 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65  *          where
ee20: 20 58 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20   X is a pointer 
ee30: 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65 33 5f 6d  to an [sqlite3_m
ee40: 75 74 65 78 5f 6d 65 74 68 6f 64 73 5d 20 6f 62  utex_methods] ob
ee50: 6a 65 63 74 20 0a 2a 2a 20 20 20 20 20 20 20 20  ject .**        
ee60: 20 20 73 68 61 6c 6c 20 6f 76 65 72 77 72 69 74    shall overwrit
ee70: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
ee80: 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
ee90: 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 0a  methods] object.
eea0: 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68  **          with
eeb0: 20 74 68 65 20 6d 75 74 65 78 20 6d 65 74 68 6f   the mutex metho
eec0: 64 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20  ds currently in 
eed0: 75 73 65 20 62 79 20 53 51 4c 69 74 65 2e 0a 2a  use by SQLite..*
eee0: 2a 0a 2a 2a 20 7b 48 31 34 31 33 35 7d 20 41 20  *.** {H14135} A 
eef0: 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
ef00: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  to [sqlite3_conf
ef10: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  ig]([SQLITE_CONF
ef20: 49 47 5f 4d 41 4c 4c 4f 43 5d 2c 4d 29 0a 2a 2a  IG_MALLOC],M).**
ef30: 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20            where 
ef40: 4d 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  M is a pointer t
ef50: 6f 20 61 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64  o an initialized
ef60: 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65   [sqlite3_mem_me
ef70: 74 68 6f 64 73 5d 0a 2a 2a 20 20 20 20 20 20 20  thods].**       
ef80: 20 20 20 6f 62 6a 65 63 74 20 73 68 61 6c 6c 20     object shall 
ef90: 63 61 75 73 65 20 61 6c 6c 20 73 75 62 73 65 71  cause all subseq
efa0: 75 65 6e 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  uent memory allo
efb0: 63 61 74 69 6f 6e 20 6f 70 65 72 61 74 69 6f 6e  cation operation
efc0: 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 65  s.**          pe
efd0: 72 66 6f 72 6d 65 64 20 62 79 20 53 51 4c 69 74  rformed by SQLit
efe0: 65 20 74 6f 20 75 73 65 20 74 68 65 20 6d 65 74  e to use the met
eff0: 68 6f 64 73 20 74 68 61 74 20 77 65 72 65 20 70  hods that were p
f000: 72 65 73 65 6e 74 20 69 6e 20 0a 2a 2a 20 20 20  resent in .**   
f010: 20 20 20 20 20 20 20 4d 20 64 75 72 69 6e 67 20         M during 
f020: 74 68 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  the call to [sql
f030: 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 2e 0a  ite3_config()]..
f040: 2a 2a 0a 2a 2a 20 7b 48 31 34 31 33 38 7d 20 41  **.** {H14138} A
f050: 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
f060: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
f070: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e  fig]([SQLITE_CON
f080: 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43 5d 2c 4d  FIG_GETMALLOC],M
f090: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68  ).**          wh
f0a0: 65 72 65 20 4d 20 69 73 20 61 20 70 6f 69 6e 74  ere M is a point
f0b0: 65 72 20 74 6f 20 61 6e 20 5b 73 71 6c 69 74 65  er to an [sqlite
f0c0: 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 6f  3_mem_methods] o
f0d0: 62 6a 65 63 74 20 73 68 61 6c 6c 0a 2a 2a 20 20  bject shall.**  
f0e0: 20 20 20 20 20 20 20 20 6f 76 65 72 77 72 69 74          overwrit
f0f0: 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
f100: 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65   [sqlite3_mem_me
f110: 74 68 6f 64 73 5d 20 6f 62 6a 65 63 74 20 77 69  thods] object wi
f120: 74 68 20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  th .**          
f130: 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
f140: 61 74 69 6f 6e 20 6d 65 74 68 6f 64 73 20 63 75  ation methods cu
f150: 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 62  rrently in use b
f160: 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 53 51  y.**          SQ
f170: 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34  Lite..**.** {H14
f180: 31 34 31 7d 20 41 20 73 75 63 63 65 73 73 66 75  141} A successfu
f190: 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
f1a0: 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
f1b0: 54 45 5f 43 4f 4e 46 49 47 5f 4d 45 4d 53 54 41  TE_CONFIG_MEMSTA
f1c0: 54 55 53 5d 2c 31 29 0a 2a 2a 20 20 20 20 20 20  TUS],1).**      
f1d0: 20 20 20 20 73 68 61 6c 6c 20 65 6e 61 62 6c 65      shall enable
f1e0: 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
f1f0: 63 61 74 69 6f 6e 20 73 74 61 74 75 73 20 63 6f  cation status co
f200: 6c 6c 65 63 74 69 6f 6e 20 6c 6f 67 69 63 2e 0a  llection logic..
f210: 2a 2a 0a 2a 2a 20 7b 48 31 34 31 34 34 7d 20 41  **.** {H14144} A
f220: 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c   successful call
f230: 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
f240: 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e  fig]([SQLITE_CON
f250: 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 5d 2c 30  FIG_MEMSTATUS],0
f260: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68  ).**          sh
f270: 61 6c 6c 20 64 69 73 61 62 6c 65 20 74 68 65 20  all disable the 
f280: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
f290: 6e 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74  n status collect
f2a0: 69 6f 6e 20 6c 6f 67 69 63 2e 0a 2a 2a 0a 2a 2a  ion logic..**.**
f2b0: 20 7b 48 31 34 31 34 37 7d 20 54 68 65 20 6d 65   {H14147} The me
f2c0: 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
f2d0: 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74 69 6f  status collectio
f2e0: 6e 20 6c 6f 67 69 63 20 73 68 61 6c 6c 20 62 65  n logic shall be
f2f0: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 6e 61  .**          ena
f300: 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e  bled by default.
f310: 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 30 7d 20  .**.** {H14150} 
f320: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  A successful cal
f330: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f  l to [sqlite3_co
f340: 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f  nfig]([SQLITE_CO
f350: 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 2c 53 2c  NFIG_SCRATCH],S,
f360: 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  Z,N).**         
f370: 20 77 68 65 72 65 20 5a 20 61 6e 64 20 4e 20 61   where Z and N a
f380: 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  re non-negative 
f390: 69 6e 74 65 67 65 72 73 20 61 6e 64 20 0a 2a 2a  integers and .**
f3a0: 20 20 20 20 20 20 20 20 20 20 53 20 69 73 20 61            S is a
f3b0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61   pointer to an a
f3c0: 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75  ligned memory bu
f3d0: 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 20 74 68  ffer not less th
f3e0: 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5a  an.**          Z
f3f0: 2a 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  *N bytes in size
f400: 20 73 68 61 6c 6c 20 63 61 75 73 65 20 53 20 74   shall cause S t
f410: 6f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65  o be used by the
f420: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 63  .**          [sc
f430: 72 61 74 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c  ratch memory all
f440: 6f 63 61 74 6f 72 5d 20 66 6f 72 20 61 73 20 6d  ocator] for as m
f450: 61 6e 79 20 61 73 20 4e 20 73 69 6d 75 6c 61 74  any as N simulat
f460: 61 6e 65 6f 75 73 0a 2a 2a 20 20 20 20 20 20 20  aneous.**       
f470: 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 65     allocations e
f480: 61 63 68 20 6f 66 20 73 69 7a 65 20 5a 2e 0a 2a  ach of size Z..*
f490: 2a 0a 2a 2a 20 7b 48 31 34 31 35 33 7d 20 41 20  *.** {H14153} A 
f4a0: 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
f4b0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66  to [sqlite3_conf
f4c0: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  ig]([SQLITE_CONF
f4d0: 49 47 5f 53 43 52 41 54 43 48 5d 2c 53 2c 5a 2c  IG_SCRATCH],S,Z,
f4e0: 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  N).**          w
f4f0: 68 65 72 65 20 53 20 69 73 20 61 20 4e 55 4c 4c  here S is a NULL
f500: 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 64   pointer shall d
f510: 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 20  isable the.**   
f520: 20 20 20 20 20 20 20 5b 73 63 72 61 74 63 68 20         [scratch 
f530: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
f540: 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 36  ]..**.** {H14156
f550: 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63  } A successful c
f560: 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  all to.**       
f570: 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66     [sqlite3_conf
f580: 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  ig]([SQLITE_CONF
f590: 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2c 53 2c  IG_PAGECACHE],S,
f5a0: 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20  Z,N).**         
f5b0: 20 77 68 65 72 65 20 5a 20 61 6e 64 20 4e 20 61   where Z and N a
f5c0: 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 20  re non-negative 
f5d0: 69 6e 74 65 67 65 72 73 20 61 6e 64 20 0a 2a 2a  integers and .**
f5e0: 20 20 20 20 20 20 20 20 20 20 53 20 69 73 20 61            S is a
f5f0: 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 61   pointer to an a
f600: 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62 75  ligned memory bu
f610: 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 20 74 68  ffer not less th
f620: 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5a  an.**          Z
f630: 2a 4e 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  *N bytes in size
f640: 20 73 68 61 6c 6c 20 63 61 75 73 65 20 53 20 74   shall cause S t
f650: 6f 20 62 65 20 75 73 65 64 20 62 79 20 74 68 65  o be used by the
f660: 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 61  .**          [pa
f670: 67 65 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 61  gecache memory a
f680: 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72 20 61 73  llocator] for as
f690: 20 6d 61 6e 79 20 61 73 20 4e 20 73 69 6d 75 6c   many as N simul
f6a0: 61 74 61 6e 65 6f 75 73 0a 2a 2a 20 20 20 20 20  ataneous.**     
f6b0: 20 20 20 20 20 61 6c 6c 6f 63 61 74 69 6f 6e 73       allocations
f6c0: 20 65 61 63 68 20 6f 66 20 73 69 7a 65 20 5a 2e   each of size Z.
f6d0: 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 35 39 7d 20  .**.** {H14159} 
f6e0: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  A successful cal
f6f0: 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  l to.**         
f700: 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67   [sqlite3_config
f710: 5d 28 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  ]([SQLITE_CONFIG
f720: 5f 50 41 47 45 43 41 43 48 45 5d 2c 53 2c 5a 2c  _PAGECACHE],S,Z,
f730: 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  N).**          w
f740: 68 65 72 65 20 53 20 69 73 20 61 20 4e 55 4c 4c  here S is a NULL
f750: 20 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 64   pointer shall d
f760: 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 20  isable the.**   
f770: 20 20 20 20 20 20 20 5b 70 61 67 65 63 61 63 68         [pagecach
f780: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
f790: 6f 72 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31  or]..**.** {H141
f7a0: 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  62} A successful
f7b0: 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
f7c0: 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54  3_config]([SQLIT
f7d0: 45 5f 43 4f 4e 46 49 47 5f 48 45 41 50 5d 2c 48  E_CONFIG_HEAP],H
f7e0: 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20  ,Z,N).**        
f7f0: 20 20 77 68 65 72 65 20 5a 20 61 6e 64 20 4e 20    where Z and N 
f800: 61 72 65 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65  are non-negative
f810: 20 69 6e 74 65 67 65 72 73 20 61 6e 64 20 0a 2a   integers and .*
f820: 2a 20 20 20 20 20 20 20 20 20 20 48 20 69 73 20  *          H is 
f830: 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
f840: 61 6c 69 67 6e 65 64 20 6d 65 6d 6f 72 79 20 62  aligned memory b
f850: 75 66 66 65 72 20 6e 6f 74 20 6c 65 73 73 20 74  uffer not less t
f860: 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  han.**          
f870: 5a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 20  Z bytes in size 
f880: 73 68 61 6c 6c 20 65 6e 61 62 6c 65 20 74 68 65  shall enable the
f890: 20 5b 6d 65 6d 73 79 73 35 5d 20 6d 65 6d 6f 72   [memsys5] memor
f8a0: 79 20 61 6c 6c 6f 63 61 74 6f 72 0a 2a 2a 20 20  y allocator.**  
f8b0: 20 20 20 20 20 20 20 20 61 6e 64 20 63 61 75 73          and caus
f8c0: 65 20 69 74 20 74 6f 20 75 73 65 20 62 75 66 66  e it to use buff
f8d0: 65 72 20 53 20 61 73 20 69 74 73 20 6d 65 6d 6f  er S as its memo
f8e0: 72 79 20 73 6f 75 72 63 65 20 61 6e 64 20 74 6f  ry source and to
f8f0: 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   use.**         
f900: 20 61 20 6d 69 6e 69 6d 75 6d 20 61 6c 6c 6f 63   a minimum alloc
f910: 61 74 69 6f 6e 20 73 69 7a 65 20 6f 66 20 4e 2e  ation size of N.
f920: 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 35 7d 20  .**.** {H14165} 
f930: 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  A successful cal
f940: 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f  l to [sqlite3_co
f950: 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43 4f  nfig]([SQLITE_CO
f960: 4e 46 49 47 5f 48 45 41 50 5d 2c 48 2c 5a 2c 4e  NFIG_HEAP],H,Z,N
f970: 29 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68  ).**          wh
f980: 65 72 65 20 48 20 69 73 20 61 20 4e 55 4c 4c 20  ere H is a NULL 
f990: 70 6f 69 6e 74 65 72 20 73 68 61 6c 6c 20 64 69  pointer shall di
f9a0: 73 61 62 6c 65 20 74 68 65 0a 2a 2a 20 20 20 20  sable the.**    
f9b0: 20 20 20 20 20 20 5b 6d 65 6d 73 79 73 35 5d 20        [memsys5] 
f9c0: 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72  memory allocator
f9d0: 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 31 36 38 7d  ..**.** {H14168}
f9e0: 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61   A successful ca
f9f0: 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
fa00: 6f 6e 66 69 67 5d 28 5b 53 51 4c 49 54 45 5f 43  onfig]([SQLITE_C
fa10: 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 5d  ONFIG_LOOKASIDE]
fa20: 2c 5a 2c 4e 29 0a 2a 2a 20 20 20 20 20 20 20 20  ,Z,N).**        
fa30: 20 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 68    shall cause th
fa40: 65 20 64 65 66 61 75 6c 74 20 5b 6c 6f 6f 6b 61  e default [looka
fa50: 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  side memory allo
fa60: 63 61 74 6f 72 5d 20 63 6f 6e 66 69 67 75 72 61  cator] configura
fa70: 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  tion.**         
fa80: 20 66 6f 72 20 6e 65 77 20 5b 64 61 74 61 62 61   for new [databa
fa90: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 5d 20  se connections] 
faa0: 74 6f 20 62 65 20 4e 20 73 6c 6f 74 73 20 6f 66  to be N slots of
fab0: 20 5a 20 62 79 74 65 73 20 65 61 63 68 2e 0a 2a   Z bytes each..*
fac0: 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
fad0: 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
fae0: 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6e   int sqlite3_con
faf0: 66 69 67 28 69 6e 74 2c 20 2e 2e 2e 29 3b 0a 0a  fig(int, ...);..
fb00: 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
fb10: 43 6f 6e 66 69 67 75 72 65 20 64 61 74 61 62 61  Configure databa
fb20: 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 20  se connections  
fb30: 7b 48 31 34 32 30 30 7d 20 3c 53 32 30 30 30 30  {H14200} <S20000
fb40: 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41  >.** EXPERIMENTA
fb50: 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  L.**.** The sqli
fb60: 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 20  te3_db_config() 
fb70: 69 6e 74 65 72 66 61 63 65 20 69 73 20 75 73 65  interface is use
fb80: 64 20 74 6f 20 6d 61 6b 65 20 63 6f 6e 66 69 67  d to make config
fb90: 75 72 61 74 69 6f 6e 0a 2a 2a 20 63 68 61 6e 67  uration.** chang
fba0: 65 73 20 74 6f 20 61 20 5b 64 61 74 61 62 61 73  es to a [databas
fbb0: 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20 20  e connection].  
fbc0: 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 69 73  The interface is
fbd0: 20 73 69 6d 69 6c 61 72 20 74 6f 0a 2a 2a 20 5b   similar to.** [
fbe0: 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
fbf0: 5d 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  ] except that th
fc00: 65 20 63 68 61 6e 67 65 73 20 61 70 70 6c 79 20  e changes apply 
fc10: 74 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 5b  to a single.** [
fc20: 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
fc30: 69 6f 6e 5d 20 28 73 70 65 63 69 66 69 65 64 20  ion] (specified 
fc40: 69 6e 20 74 68 65 20 66 69 72 73 74 20 61 72 67  in the first arg
fc50: 75 6d 65 6e 74 29 2e 20 20 54 68 65 0a 2a 2a 20  ument).  The.** 
fc60: 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69  sqlite3_db_confi
fc70: 67 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61  g() interface ca
fc80: 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 20 69  n only be used i
fc90: 6d 6d 65 64 69 61 74 65 6c 79 20 61 66 74 65 72  mmediately after
fca0: 0a 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65  .** the database
fcb0: 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
fcc0: 72 65 61 74 65 64 20 75 73 69 6e 67 20 5b 73 71  reated using [sq
fcd0: 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 0a 2a  lite3_open()],.*
fce0: 2a 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  * [sqlite3_open1
fcf0: 36 28 29 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65  6()], or [sqlite
fd00: 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 2e 20 20 0a  3_open_v2()].  .
fd10: 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
fd20: 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
fd30: 69 74 65 33 5f 64 62 5f 63 6f 6e 66 69 67 28 44  ite3_db_config(D
fd40: 2c 56 2c 2e 2e 2e 29 20 20 69 73 20 74 68 65 0a  ,V,...)  is the.
fd50: 2a 2a 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ** configuration
fd60: 20 76 65 72 62 20 2d 20 61 6e 20 69 6e 74 65 67   verb - an integ
fd70: 65 72 20 63 6f 64 65 20 74 68 61 74 20 69 6e 64  er code that ind
fd80: 69 63 61 74 65 73 20 77 68 61 74 0a 2a 2a 20 61  icates what.** a
fd90: 73 70 65 63 74 20 6f 66 20 74 68 65 20 5b 64 61  spect of the [da
fda0: 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
fdb0: 6e 5d 20 69 73 20 62 65 69 6e 67 20 63 6f 6e 66  n] is being conf
fdc0: 69 67 75 72 65 64 2e 0a 2a 2a 20 54 68 65 20 6f  igured..** The o
fdd0: 6e 6c 79 20 63 68 6f 69 63 65 20 66 6f 72 20 74  nly choice for t
fde0: 68 69 73 20 76 61 6c 75 65 20 69 73 20 5b 53 51  his value is [SQ
fdf0: 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f  LITE_DBCONFIG_LO
fe00: 4f 4b 41 53 49 44 45 5d 2e 0a 2a 2a 20 4e 65 77  OKASIDE]..** New
fe10: 20 76 65 72 62 73 20 61 72 65 20 6c 69 6b 65 6c   verbs are likel
fe20: 79 20 74 6f 20 62 65 20 61 64 64 65 64 20 69 6e  y to be added in
fe30: 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
fe40: 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 41   of SQLite..** A
fe50: 64 64 69 74 69 6f 6e 61 6c 20 61 72 67 75 6d 65  dditional argume
fe60: 6e 74 73 20 64 65 70 65 6e 64 20 6f 6e 20 74 68  nts depend on th
fe70: 65 20 76 65 72 62 2e 0a 2a 2a 0a 2a 2a 20 49 4e  e verb..**.** IN
fe80: 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
fe90: 7b 48 31 34 32 30 33 7d 20 41 20 63 61 6c 6c 20  {H14203} A call 
fea0: 74 6f 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63  to [sqlite3_db_c
feb0: 6f 6e 66 69 67 28 44 2c 56 2c 2e 2e 2e 29 5d 20  onfig(D,V,...)] 
fec0: 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b 53 51  shall return [SQ
fed0: 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20 20 20  LITE_OK].**     
fee0: 20 20 20 20 20 69 66 20 61 6e 64 20 6f 6e 6c 79       if and only
fef0: 20 69 66 20 74 68 65 20 63 61 6c 6c 20 69 73 20   if the call is 
ff00: 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a 0a 2a  successful..**.*
ff10: 2a 20 7b 48 31 34 32 30 36 7d 20 49 66 20 6f 6e  * {H14206} If on
ff20: 65 20 6f 72 20 6d 6f 72 65 20 73 6c 6f 74 73 20  e or more slots 
ff30: 6f 66 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64  of the [lookasid
ff40: 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
ff50: 6f 72 5d 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20  or] for.**      
ff60: 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63 6f      [database co
ff70: 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 61 72 65 20  nnection] D are 
ff80: 69 6e 20 75 73 65 2c 20 74 68 65 6e 20 61 20 63  in use, then a c
ff90: 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  all to.**       
ffa0: 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63     [sqlite3_db_c
ffb0: 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 54 45  onfig](D,[SQLITE
ffc0: 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53  _DBCONFIG_LOOKAS
ffd0: 49 44 45 5d 2c 2e 2e 2e 29 20 73 68 61 6c 6c 0a  IDE],...) shall.
ffe0: 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61 69 6c  **          fail
fff0: 20 77 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45   with an [SQLITE
10000 5f 42 55 53 59 5d 20 72 65 74 75 72 6e 20 63 6f  _BUSY] return co
10010 64 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 30  de..**.** {H1420
10020 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  9} A successful 
10030 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 20  call to .**     
10040 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64 62       [sqlite3_db
10050 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49  _config](D,[SQLI
10060 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  TE_DBCONFIG_LOOK
10070 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 68  ASIDE],B,Z,N) wh
10080 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ere.**          
10090 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64 61  D is an open [da
100a0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
100b0 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20 61  n] and Z and N a
100c0 72 65 20 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20  re positive.**  
100d0 20 20 20 20 20 20 20 20 69 6e 74 65 67 65 72 73          integers
100e0 20 61 6e 64 20 42 20 69 73 20 61 6e 20 61 6c 69   and B is an ali
100f0 67 6e 65 64 20 62 75 66 66 65 72 20 61 74 20 6c  gned buffer at l
10100 65 61 73 74 20 5a 2a 4e 20 62 79 74 65 73 20 69  east Z*N bytes i
10110 6e 20 73 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20  n size.**       
10120 20 20 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74     shall cause t
10130 68 65 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65  he [lookaside me
10140 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20  mory allocator] 
10150 66 6f 72 20 44 20 74 6f 20 75 73 65 20 62 75 66  for D to use buf
10160 66 65 72 20 42 20 0a 2a 2a 20 20 20 20 20 20 20  fer B .**       
10170 20 20 20 77 69 74 68 20 4e 20 73 6c 6f 74 73 20     with N slots 
10180 6f 66 20 5a 20 62 79 74 65 73 20 65 61 63 68 2e  of Z bytes each.
10190 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32 31 32 7d 20  .**.** {H14212} 
101a0 41 20 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c  A successful cal
101b0 6c 20 74 6f 20 0a 2a 2a 20 20 20 20 20 20 20 20  l to .**        
101c0 20 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f    [sqlite3_db_co
101d0 6e 66 69 67 5d 28 44 2c 5b 53 51 4c 49 54 45 5f  nfig](D,[SQLITE_
101e0 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49  DBCONFIG_LOOKASI
101f0 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77 68 65 72 65  DE],B,Z,N) where
10200 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 44 20 69  .**          D i
10210 73 20 61 6e 20 6f 70 65 6e 20 5b 64 61 74 61 62  s an open [datab
10220 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
10230 61 6e 64 20 5a 20 61 6e 64 20 4e 20 61 72 65 20  and Z and N are 
10240 70 6f 73 69 74 69 76 65 0a 2a 2a 20 20 20 20 20  positive.**     
10250 20 20 20 20 20 69 6e 74 65 67 65 72 73 20 61 6e       integers an
10260 64 20 42 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e  d B is NULL poin
10270 74 65 72 20 73 68 61 6c 6c 20 63 61 75 73 65 20  ter shall cause 
10280 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
10290 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72  [lookaside memor
102a0 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 66 6f 72  y allocator] for
102b0 20 44 20 74 6f 20 61 20 6f 62 74 61 69 6e 20 5a   D to a obtain Z
102c0 2a 4e 20 62 79 74 65 20 62 75 66 66 65 72 0a 2a  *N byte buffer.*
102d0 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d 20  *          from 
102e0 74 68 65 20 70 72 69 6d 61 72 79 20 6d 65 6d 6f  the primary memo
102f0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 61 6e 64  ry allocator and
10300 20 75 73 65 20 74 68 61 74 20 62 75 66 66 65 72   use that buffer
10310 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74  .**          wit
10320 68 20 4e 20 6c 6f 6f 6b 61 73 69 64 65 20 73 6c  h N lookaside sl
10330 6f 74 73 20 6f 66 20 5a 20 62 79 74 65 73 20 65  ots of Z bytes e
10340 61 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 34 32  ach..**.** {H142
10350 31 35 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  15} A successful
10360 20 63 61 6c 6c 20 74 6f 20 0a 2a 2a 20 20 20 20   call to .**    
10370 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 64        [sqlite3_d
10380 62 5f 63 6f 6e 66 69 67 5d 28 44 2c 5b 53 51 4c  b_config](D,[SQL
10390 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c 4f 4f  ITE_DBCONFIG_LOO
103a0 4b 41 53 49 44 45 5d 2c 42 2c 5a 2c 4e 29 20 77  KASIDE],B,Z,N) w
103b0 68 65 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  here.**         
103c0 20 44 20 69 73 20 61 6e 20 6f 70 65 6e 20 5b 64   D is an open [d
103d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
103e0 6f 6e 5d 20 61 6e 64 20 5a 20 61 6e 64 20 4e 20  on] and Z and N 
103f0 61 72 65 20 7a 65 72 6f 20 73 68 61 6c 6c 0a 2a  are zero shall.*
10400 2a 20 20 20 20 20 20 20 20 20 20 64 69 73 61 62  *          disab
10410 6c 65 20 74 68 65 20 5b 6c 6f 6f 6b 61 73 69 64  le the [lookasid
10420 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
10430 6f 72 5d 20 66 6f 72 20 44 2e 0a 2a 2a 0a 2a 2a  or] for D..**.**
10440 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
10450 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
10460 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  AL int sqlite3_d
10470 62 5f 63 6f 6e 66 69 67 28 73 71 6c 69 74 65 33  b_config(sqlite3
10480 2a 2c 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b  *, int op, ...);
10490 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
104a0 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74  : Memory Allocat
104b0 69 6f 6e 20 52 6f 75 74 69 6e 65 73 20 7b 48 31  ion Routines {H1
104c0 30 31 35 35 7d 20 3c 53 32 30 31 32 30 3e 0a 2a  0155} <S20120>.*
104d0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
104e0 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65  *.** An instance
104f0 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 20   of this object 
10500 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65  defines the inte
10510 72 66 61 63 65 20 62 65 74 77 65 65 6e 20 53 51  rface between SQ
10520 4c 69 74 65 0a 2a 2a 20 61 6e 64 20 6c 6f 77 2d  Lite.** and low-
10530 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c  level memory all
10540 6f 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73  ocation routines
10550 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 62 6a  ..**.** This obj
10560 65 63 74 20 69 73 20 75 73 65 64 20 69 6e 20 6f  ect is used in o
10570 6e 6c 79 20 6f 6e 65 20 70 6c 61 63 65 20 69 6e  nly one place in
10580 20 74 68 65 20 53 51 4c 69 74 65 20 69 6e 74 65   the SQLite inte
10590 72 66 61 63 65 2e 0a 2a 2a 20 41 20 70 6f 69 6e  rface..** A poin
105a0 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61 6e  ter to an instan
105b0 63 65 20 6f 66 20 74 68 69 73 20 6f 62 6a 65 63  ce of this objec
105c0 74 20 69 73 20 74 68 65 20 61 72 67 75 6d 65 6e  t is the argumen
105d0 74 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  t to.** [sqlite3
105e0 5f 63 6f 6e 66 69 67 28 29 5d 20 77 68 65 6e 20  _config()] when 
105f0 74 68 65 20 63 6f 6e 66 69 67 75 72 61 74 69 6f  the configuratio
10600 6e 20 6f 70 74 69 6f 6e 20 69 73 0a 2a 2a 20 5b  n option is.** [
10610 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41  SQLITE_CONFIG_MA
10620 4c 4c 4f 43 5d 2e 20 20 42 79 20 63 72 65 61 74  LLOC].  By creat
10630 69 6e 67 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  ing an instance 
10640 6f 66 20 74 68 69 73 20 6f 62 6a 65 63 74 0a 2a  of this object.*
10650 2a 20 61 6e 64 20 70 61 73 73 69 6e 67 20 69 74  * and passing it
10660 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e   to [sqlite3_con
10670 66 69 67 28 29 5d 20 64 75 72 69 6e 67 20 63 6f  fig()] during co
10680 6e 66 69 67 75 72 61 74 69 6f 6e 2c 20 61 6e 0a  nfiguration, an.
10690 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 63  ** application c
106a0 61 6e 20 73 70 65 63 69 66 79 20 61 6e 20 61 6c  an specify an al
106b0 74 65 72 6e 61 74 69 76 65 20 6d 65 6d 6f 72 79  ternative memory
106c0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73   allocation subs
106d0 79 73 74 65 6d 0a 2a 2a 20 66 6f 72 20 53 51 4c  ystem.** for SQL
106e0 69 74 65 20 74 6f 20 75 73 65 20 66 6f 72 20 61  ite to use for a
106f0 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d 69  ll of its dynami
10700 63 20 6d 65 6d 6f 72 79 20 6e 65 65 64 73 2e 0a  c memory needs..
10710 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
10720 53 51 4c 69 74 65 20 63 6f 6d 65 73 20 77 69 74  SQLite comes wit
10730 68 20 61 20 62 75 69 6c 74 2d 69 6e 20 6d 65 6d  h a built-in mem
10740 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20 74 68  ory allocator th
10750 61 74 20 69 73 0a 2a 2a 20 70 65 72 66 65 63 74  at is.** perfect
10760 6c 79 20 61 64 65 71 75 61 74 65 20 66 6f 72 20  ly adequate for 
10770 74 68 65 20 6f 76 65 72 77 68 65 6c 6d 69 6e 67  the overwhelming
10780 20 6d 61 6a 6f 72 69 74 79 20 6f 66 20 61 70 70   majority of app
10790 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 6e 64  lications.** and
107a0 20 74 68 61 74 20 74 68 69 73 20 6f 62 6a 65 63   that this objec
107b0 74 20 69 73 20 6f 6e 6c 79 20 75 73 65 66 75 6c  t is only useful
107c0 20 74 6f 20 61 20 74 69 6e 79 20 6d 69 6e 6f 72   to a tiny minor
107d0 69 74 79 20 6f 66 20 61 70 70 6c 69 63 61 74 69  ity of applicati
107e0 6f 6e 73 0a 2a 2a 20 77 69 74 68 20 73 70 65 63  ons.** with spec
107f0 69 61 6c 69 7a 65 64 20 6d 65 6d 6f 72 79 20 61  ialized memory a
10800 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72  llocation requir
10810 65 6d 65 6e 74 73 2e 20 20 54 68 69 73 20 6f 62  ements.  This ob
10820 6a 65 63 74 20 69 73 0a 2a 2a 20 61 6c 73 6f 20  ject is.** also 
10830 75 73 65 64 20 64 75 72 69 6e 67 20 74 65 73 74  used during test
10840 69 6e 67 20 6f 66 20 53 51 4c 69 74 65 20 69 6e  ing of SQLite in
10850 20 6f 72 64 65 72 20 74 6f 20 73 70 65 63 69 66   order to specif
10860 79 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  y an alternative
10870 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  .** memory alloc
10880 61 74 6f 72 20 74 68 61 74 20 73 69 6d 75 6c 61  ator that simula
10890 74 65 73 20 6d 65 6d 6f 72 79 20 6f 75 74 2d 6f  tes memory out-o
108a0 66 2d 6d 65 6d 6f 72 79 20 63 6f 6e 64 69 74 69  f-memory conditi
108b0 6f 6e 73 20 69 6e 0a 2a 2a 20 6f 72 64 65 72 20  ons in.** order 
108c0 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 53  to verify that S
108d0 51 4c 69 74 65 20 72 65 63 6f 76 65 72 73 20 67  QLite recovers g
108e0 72 61 63 65 66 75 6c 6c 79 20 66 72 6f 6d 20 73  racefully from s
108f0 75 63 68 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e  uch.** condition
10900 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 61  s..**.** The xMa
10910 6c 6c 6f 63 2c 20 78 46 72 65 65 2c 20 61 6e 64  lloc, xFree, and
10920 20 78 52 65 61 6c 6c 6f 63 20 6d 65 74 68 6f 64   xRealloc method
10930 73 20 6d 75 73 74 20 77 6f 72 6b 20 6c 69 6b 65  s must work like
10940 20 74 68 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29   the.** malloc()
10950 2c 20 66 72 65 65 28 29 2c 20 61 6e 64 20 72 65  , free(), and re
10960 61 6c 6c 6f 63 28 29 20 66 75 6e 63 74 69 6f 6e  alloc() function
10970 73 20 66 72 6f 6d 20 74 68 65 20 73 74 61 6e 64  s from the stand
10980 61 72 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a  ard library..**.
10990 2a 2a 20 78 53 69 7a 65 20 73 68 6f 75 6c 64 20  ** xSize should 
109a0 72 65 74 75 72 6e 20 74 68 65 20 61 6c 6c 6f 63  return the alloc
109b0 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 20 6d  ated size of a m
109c0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
109d0 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20 6f  .** previously o
109e0 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 78 4d 61  btained from xMa
109f0 6c 6c 6f 63 20 6f 72 20 78 52 65 61 6c 6c 6f 63  lloc or xRealloc
10a00 2e 20 20 54 68 65 20 61 6c 6c 6f 63 61 74 65 64  .  The allocated
10a10 20 73 69 7a 65 0a 2a 2a 20 69 73 20 61 6c 77 61   size.** is alwa
10a20 79 73 20 61 74 20 6c 65 61 73 74 20 61 73 20 62  ys at least as b
10a30 69 67 20 61 73 20 74 68 65 20 72 65 71 75 65 73  ig as the reques
10a40 74 65 64 20 73 69 7a 65 20 62 75 74 20 6d 61 79  ted size but may
10a50 20 62 65 20 6c 61 72 67 65 72 2e 0a 2a 2a 0a 2a   be larger..**.*
10a60 2a 20 54 68 65 20 78 52 6f 75 6e 64 75 70 20 6d  * The xRoundup m
10a70 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20 77 68  ethod returns wh
10a80 61 74 20 77 6f 75 6c 64 20 62 65 20 74 68 65 20  at would be the 
10a90 61 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f  allocated size o
10aa0 66 0a 2a 2a 20 61 20 6d 65 6d 6f 72 79 20 61 6c  f.** a memory al
10ab0 6c 6f 63 61 74 69 6f 6e 20 67 69 76 65 6e 20 61  location given a
10ac0 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 71 75   particular requ
10ad0 65 73 74 65 64 20 73 69 7a 65 2e 20 20 4d 6f 73  ested size.  Mos
10ae0 74 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f  t memory.** allo
10af0 63 61 74 6f 72 73 20 72 6f 75 6e 64 20 75 70 20  cators round up 
10b00 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
10b10 6e 73 20 61 74 20 6c 65 61 73 74 20 74 6f 20 74  ns at least to t
10b20 68 65 20 6e 65 78 74 20 6d 75 6c 74 69 70 6c 65  he next multiple
10b30 0a 2a 2a 20 6f 66 20 38 2e 20 20 53 6f 6d 65 20  .** of 8.  Some 
10b40 61 6c 6c 6f 63 61 74 6f 72 73 20 72 6f 75 6e 64  allocators round
10b50 20 75 70 20 74 6f 20 61 20 6c 61 72 67 65 72 20   up to a larger 
10b60 6d 75 6c 74 69 70 6c 65 20 6f 72 20 74 6f 20 61  multiple or to a
10b70 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2a 0a   power of 2..**.
10b80 2a 2a 20 54 68 65 20 78 49 6e 69 74 20 6d 65 74  ** The xInit met
10b90 68 6f 64 20 69 6e 69 74 69 61 6c 69 7a 65 73 20  hod initializes 
10ba0 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
10bb0 61 74 6f 72 2e 20 20 28 46 6f 72 20 65 78 61 6d  ator.  (For exam
10bc0 70 6c 65 2c 0a 2a 2a 20 69 74 20 6d 69 67 68 74  ple,.** it might
10bd0 20 61 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65   allocate any re
10be0 71 75 69 72 65 20 6d 75 74 65 78 65 73 20 6f 72  quire mutexes or
10bf0 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65   initialize inte
10c00 72 6e 61 6c 20 64 61 74 61 0a 2a 2a 20 73 74 72  rnal data.** str
10c10 75 63 74 75 72 65 73 2e 20 20 54 68 65 20 78 53  uctures.  The xS
10c20 68 75 74 64 6f 77 6e 20 6d 65 74 68 6f 64 20 69  hutdown method i
10c30 73 20 69 6e 76 6f 6b 65 64 20 28 69 6e 64 69 72  s invoked (indir
10c40 65 63 74 6c 79 29 20 62 79 0a 2a 2a 20 5b 73 71  ectly) by.** [sq
10c50 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e 28 29  lite3_shutdown()
10c60 5d 20 61 6e 64 20 73 68 6f 75 6c 64 20 64 65 61  ] and should dea
10c70 6c 6c 6f 63 61 74 65 20 61 6e 79 20 72 65 73 6f  llocate any reso
10c80 75 72 63 65 73 20 61 63 71 75 69 72 65 64 0a 2a  urces acquired.*
10c90 2a 20 62 79 20 78 49 6e 69 74 2e 20 20 54 68 65  * by xInit.  The
10ca0 20 70 41 70 70 44 61 74 61 20 70 6f 69 6e 74 65   pAppData pointe
10cb0 72 20 69 73 20 75 73 65 64 20 61 73 20 74 68 65  r is used as the
10cc0 20 6f 6e 6c 79 20 70 61 72 61 6d 65 74 65 72 20   only parameter 
10cd0 74 6f 0a 2a 2a 20 78 49 6e 69 74 20 61 6e 64 20  to.** xInit and 
10ce0 78 53 68 75 74 64 6f 77 6e 2e 0a 2a 2f 0a 74 79  xShutdown..*/.ty
10cf0 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
10d00 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
10d10 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
10d20 68 6f 64 73 3b 0a 73 74 72 75 63 74 20 73 71 6c  hods;.struct sql
10d30 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
10d40 20 7b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 4d 61   {.  void *(*xMa
10d50 6c 6c 6f 63 29 28 69 6e 74 29 3b 20 20 20 20 20  lloc)(int);     
10d60 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 61 6c      /* Memory al
10d70 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  location functio
10d80 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 46  n */.  void (*xF
10d90 72 65 65 29 28 76 6f 69 64 2a 29 3b 20 20 20 20  ree)(void*);    
10da0 20 20 20 20 20 20 2f 2a 20 46 72 65 65 20 61 20        /* Free a 
10db0 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  prior allocation
10dc0 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 28 2a 78 52   */.  void *(*xR
10dd0 65 61 6c 6c 6f 63 29 28 76 6f 69 64 2a 2c 69 6e  ealloc)(void*,in
10de0 74 29 3b 20 20 2f 2a 20 52 65 73 69 7a 65 20 61  t);  /* Resize a
10df0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 2a 2f 0a  n allocation */.
10e00 20 20 69 6e 74 20 28 2a 78 53 69 7a 65 29 28 76    int (*xSize)(v
10e10 6f 69 64 2a 29 3b 20 20 20 20 20 20 20 20 20 20  oid*);          
10e20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73   /* Return the s
10e30 69 7a 65 20 6f 66 20 61 6e 20 61 6c 6c 6f 63 61  ize of an alloca
10e40 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  tion */.  int (*
10e50 78 52 6f 75 6e 64 75 70 29 28 69 6e 74 29 3b 20  xRoundup)(int); 
10e60 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 75 6e           /* Roun
10e70 64 20 75 70 20 72 65 71 75 65 73 74 20 73 69 7a  d up request siz
10e80 65 20 74 6f 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e to allocation 
10e90 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  size */.  int (*
10ea0 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 20 20  xInit)(void*);  
10eb0 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 69 74           /* Init
10ec0 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
10ed0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 2a 2f 0a 20  y allocator */. 
10ee0 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77   void (*xShutdow
10ef0 6e 29 28 76 6f 69 64 2a 29 3b 20 20 20 20 20 20  n)(void*);      
10f00 2f 2a 20 44 65 69 6e 69 74 69 61 6c 69 7a 65 20  /* Deinitialize 
10f10 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
10f20 61 74 6f 72 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  ator */.  void *
10f30 70 41 70 70 44 61 74 61 3b 20 20 20 20 20 20 20  pAppData;       
10f40 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
10f50 6d 65 6e 74 20 74 6f 20 78 49 6e 69 74 28 29 20  ment to xInit() 
10f60 61 6e 64 20 78 53 68 75 74 64 6f 77 6e 28 29 20  and xShutdown() 
10f70 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.};../*.** CAP
10f80 49 33 52 45 46 3a 20 43 6f 6e 66 69 67 75 72 61  I3REF: Configura
10f90 74 69 6f 6e 20 4f 70 74 69 6f 6e 73 20 7b 48 31  tion Options {H1
10fa0 30 31 36 30 7d 20 3c 53 32 30 30 30 30 3e 0a 2a  0160} <S20000>.*
10fb0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
10fc0 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e 73 74  *.** These const
10fd0 61 6e 74 73 20 61 72 65 20 74 68 65 20 61 76 61  ants are the ava
10fe0 69 6c 61 62 6c 65 20 69 6e 74 65 67 65 72 20 63  ilable integer c
10ff0 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74  onfiguration opt
11000 69 6f 6e 73 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ions that.** can
11010 20 62 65 20 70 61 73 73 65 64 20 61 73 20 74 68   be passed as th
11020 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
11030 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33   to the [sqlite3
11040 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e 74 65 72  _config()] inter
11050 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  face..**.** New 
11060 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
11070 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 61 64 64  tions may be add
11080 65 64 20 69 6e 20 66 75 74 75 72 65 20 72 65 6c  ed in future rel
11090 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74 65 2e  eases of SQLite.
110a0 0a 2a 2a 20 45 78 69 73 74 69 6e 67 20 63 6f 6e  .** Existing con
110b0 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
110c0 6e 73 20 6d 69 67 68 74 20 62 65 20 64 69 73 63  ns might be disc
110d0 6f 6e 74 69 6e 75 65 64 2e 20 20 41 70 70 6c 69  ontinued.  Appli
110e0 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 68 6f 75 6c  cations.** shoul
110f0 64 20 63 68 65 63 6b 20 74 68 65 20 72 65 74 75  d check the retu
11100 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 5b 73 71  rn code from [sq
11110 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20  lite3_config()] 
11120 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61  to make sure tha
11130 74 0a 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f  t.** the call wo
11140 72 6b 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69  rked.  The [sqli
11150 74 65 33 5f 63 6f 6e 66 69 67 28 29 5d 20 69 6e  te3_config()] in
11160 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65 74  terface will ret
11170 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72  urn a.** non-zer
11180 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  o [error code] i
11190 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65 64  f a discontinued
111a0 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20   or unsupported 
111b0 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f 70  configuration op
111c0 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b  tion.** is invok
111d0 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a 2a  ed..**.** <dl>.*
111e0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
111f0 46 49 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44  FIG_SINGLETHREAD
11200 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65  </dt>.** <dd>The
11210 72 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65  re are no argume
11220 6e 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69  nts to this opti
11230 6f 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e  on.  This option
11240 20 64 69 73 61 62 6c 65 73 0a 2a 2a 20 61 6c 6c   disables.** all
11250 20 6d 75 74 65 78 69 6e 67 20 61 6e 64 20 70 75   mutexing and pu
11260 74 73 20 53 51 4c 69 74 65 20 69 6e 74 6f 20 61  ts SQLite into a
11270 20 6d 6f 64 65 20 77 68 65 72 65 20 69 74 20 63   mode where it c
11280 61 6e 20 6f 6e 6c 79 20 62 65 20 75 73 65 64 0a  an only be used.
11290 2a 2a 20 62 79 20 61 20 73 69 6e 67 6c 65 20 74  ** by a single t
112a0 68 72 65 61 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  hread.</dd>.**.*
112b0 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
112c0 46 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 3c  FIG_MULTITHREAD<
112d0 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 72  /dt>.** <dd>Ther
112e0 65 20 61 72 65 20 6e 6f 20 61 72 67 75 6d 65 6e  e are no argumen
112f0 74 73 20 74 6f 20 74 68 69 73 20 6f 70 74 69 6f  ts to this optio
11300 6e 2e 20 20 54 68 69 73 20 6f 70 74 69 6f 6e 20  n.  This option 
11310 64 69 73 61 62 6c 65 73 0a 2a 2a 20 6d 75 74 65  disables.** mute
11320 78 69 6e 67 20 6f 6e 20 5b 64 61 74 61 62 61 73  xing on [databas
11330 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e  e connection] an
11340 64 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  d [prepared stat
11350 65 6d 65 6e 74 5d 20 6f 62 6a 65 63 74 73 2e 0a  ement] objects..
11360 2a 2a 20 54 68 65 20 61 70 70 6c 69 63 61 74 69  ** The applicati
11370 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
11380 65 20 66 6f 72 20 73 65 72 69 61 6c 69 7a 69 6e  e for serializin
11390 67 20 61 63 63 65 73 73 20 74 6f 0a 2a 2a 20 5b  g access to.** [
113a0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
113b0 69 6f 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61  ions] and [prepa
113c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 2e  red statements].
113d0 20 20 42 75 74 20 6f 74 68 65 72 20 6d 75 74 65    But other mute
113e0 78 65 73 0a 2a 2a 20 61 72 65 20 65 6e 61 62 6c  xes.** are enabl
113f0 65 64 20 73 6f 20 74 68 61 74 20 53 51 4c 69 74  ed so that SQLit
11400 65 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74  e will be safe t
11410 6f 20 75 73 65 20 69 6e 20 61 20 6d 75 6c 74 69  o use in a multi
11420 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 65 6e 76  -threaded.** env
11430 69 72 6f 6e 6d 65 6e 74 20 61 73 20 6c 6f 6e 67  ironment as long
11440 20 61 73 20 6e 6f 20 74 77 6f 20 74 68 72 65 61   as no two threa
11450 64 73 20 61 74 74 65 6d 70 74 20 74 6f 20 75 73  ds attempt to us
11460 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 5b 64  e the same.** [d
11470 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
11480 6f 6e 5d 20 61 74 20 74 68 65 20 73 61 6d 65 20  on] at the same 
11490 74 69 6d 65 2e 20 20 53 65 65 20 74 68 65 20 5b  time.  See the [
114a0 74 68 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 0a  threading mode].
114b0 2a 2a 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e  ** documentation
114c0 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20   for additional 
114d0 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 3c 2f 64 64  information.</dd
114e0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
114f0 54 45 5f 43 4f 4e 46 49 47 5f 53 45 52 49 41 4c  TE_CONFIG_SERIAL
11500 49 5a 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  IZED</dt>.** <dd
11510 3e 54 68 65 72 65 20 61 72 65 20 6e 6f 20 61 72  >There are no ar
11520 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 69 73 20  guments to this 
11530 6f 70 74 69 6f 6e 2e 20 20 54 68 69 73 20 6f 70  option.  This op
11540 74 69 6f 6e 20 65 6e 61 62 6c 65 73 0a 2a 2a 20  tion enables.** 
11550 61 6c 6c 20 6d 75 74 65 78 65 73 20 69 6e 63 6c  all mutexes incl
11560 75 64 69 6e 67 20 74 68 65 20 72 65 63 75 72 73  uding the recurs
11570 69 76 65 0a 2a 2a 20 6d 75 74 65 78 65 73 20 6f  ive.** mutexes o
11580 6e 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  n [database conn
11590 65 63 74 69 6f 6e 5d 20 61 6e 64 20 5b 70 72 65  ection] and [pre
115a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
115b0 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 20 49 6e 20   objects..** In 
115c0 74 68 69 73 20 6d 6f 64 65 20 28 77 68 69 63 68  this mode (which
115d0 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74 20   is the default 
115e0 77 68 65 6e 20 53 51 4c 69 74 65 20 69 73 20 63  when SQLite is c
115f0 6f 6d 70 69 6c 65 64 20 77 69 74 68 0a 2a 2a 20  ompiled with.** 
11600 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41  [SQLITE_THREADSA
11610 46 45 3d 31 5d 29 20 74 68 65 20 53 51 4c 69 74  FE=1]) the SQLit
11620 65 20 6c 69 62 72 61 72 79 20 77 69 6c 6c 20 69  e library will i
11630 74 73 65 6c 66 20 73 65 72 69 61 6c 69 7a 65 20  tself serialize 
11640 61 63 63 65 73 73 0a 2a 2a 20 74 6f 20 5b 64 61  access.** to [da
11650 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
11660 6e 73 5d 20 61 6e 64 20 5b 70 72 65 70 61 72 65  ns] and [prepare
11670 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 20 73 6f  d statements] so
11680 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 61 70 70   that the.** app
11690 6c 69 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  lication is free
116a0 20 74 6f 20 75 73 65 20 74 68 65 20 73 61 6d 65   to use the same
116b0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
116c0 63 74 69 6f 6e 5d 20 6f 72 20 74 68 65 0a 2a 2a  ction] or the.**
116d0 20 73 61 6d 65 20 5b 70 72 65 70 61 72 65 64 20   same [prepared 
116e0 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 64 69  statement] in di
116f0 66 66 65 72 65 6e 74 20 74 68 72 65 61 64 73 20  fferent threads 
11700 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65  at the same time
11710 2e 0a 2a 2a 20 53 65 65 20 74 68 65 20 5b 74 68  ..** See the [th
11720 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 64 6f  reading mode] do
11730 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  cumentation for 
11740 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72  additional infor
11750 6d 61 74 69 6f 6e 2e 3c 2f 64 64 3e 0a 2a 2a 0a  mation.</dd>.**.
11760 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f  ** <dt>SQLITE_CO
11770 4e 46 49 47 5f 4d 41 4c 4c 4f 43 3c 2f 64 74 3e  NFIG_MALLOC</dt>
11780 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f 70 74  .** <dd>This opt
11790 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69 6e 67  ion takes a sing
117a0 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68 69 63  le argument whic
117b0 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74  h is a pointer t
117c0 6f 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e 63 65  o an.** instance
117d0 20 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33   of the [sqlite3
117e0 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 20 73 74  _mem_methods] st
117f0 72 75 63 74 75 72 65 2e 20 20 54 68 65 20 61 72  ructure.  The ar
11800 67 75 6d 65 6e 74 20 73 70 65 63 69 66 69 65 73  gument specifies
11810 0a 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20  .** alternative 
11820 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
11830 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f 75 74   allocation rout
11840 69 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20  ines to be used 
11850 69 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 74  in place of.** t
11860 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
11870 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20 62 75  tion routines bu
11880 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e  ilt into SQLite.
11890 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
118a0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
118b0 54 4d 41 4c 4c 4f 43 3c 2f 64 74 3e 0a 2a 2a 20  TMALLOC</dt>.** 
118c0 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
118d0 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
118e0 72 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73  rgument which is
118f0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
11900 0a 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20  .** instance of 
11910 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 65 6d  the [sqlite3_mem
11920 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74  _methods] struct
11930 75 72 65 2e 20 20 54 68 65 20 5b 73 71 6c 69 74  ure.  The [sqlit
11940 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 5d 0a  e3_mem_methods].
11950 2a 2a 20 73 74 72 75 63 74 75 72 65 20 69 73 20  ** structure is 
11960 66 69 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20  filled with the 
11970 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69 6e 65  currently define
11980 64 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  d memory allocat
11990 69 6f 6e 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ion routines..**
119a0 20 54 68 69 73 20 6f 70 74 69 6f 6e 20 63 61 6e   This option can
119b0 20 62 65 20 75 73 65 64 20 74 6f 20 6f 76 65 72   be used to over
119c0 6c 6f 61 64 20 74 68 65 20 64 65 66 61 75 6c 74  load the default
119d0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
119e0 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 77  on.** routines w
119f0 69 74 68 20 61 20 77 72 61 70 70 65 72 20 74 68  ith a wrapper th
11a00 61 74 20 73 69 6d 75 6c 61 74 69 6f 6e 73 20 6d  at simulations m
11a10 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
11a20 20 66 61 69 6c 75 72 65 20 6f 72 0a 2a 2a 20 74   failure or.** t
11a30 72 61 63 6b 73 20 6d 65 6d 6f 72 79 20 75 73 61  racks memory usa
11a40 67 65 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 2e  ge, for example.
11a50 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
11a60 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
11a70 4d 53 54 41 54 55 53 3c 2f 64 74 3e 0a 2a 2a 20  MSTATUS</dt>.** 
11a80 3c 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20  <dd>This option 
11a90 74 61 6b 65 73 20 73 69 6e 67 6c 65 20 61 72 67  takes single arg
11aa0 75 6d 65 6e 74 20 6f 66 20 74 79 70 65 20 69 6e  ument of type in
11ab0 74 2c 20 69 6e 74 65 72 70 72 65 74 65 64 20 61  t, interpreted a
11ac0 73 20 61 20 0a 2a 2a 20 62 6f 6f 6c 65 61 6e 2c  s a .** boolean,
11ad0 20 77 68 69 63 68 20 65 6e 61 62 6c 65 73 20 6f   which enables o
11ae0 72 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63  r disables the c
11af0 6f 6c 6c 65 63 74 69 6f 6e 20 6f 66 20 6d 65 6d  ollection of mem
11b00 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 0a  ory allocation .
11b10 2a 2a 20 73 74 61 74 69 73 74 69 63 73 2e 20 57  ** statistics. W
11b20 68 65 6e 20 64 69 73 61 62 6c 65 64 2c 20 74 68  hen disabled, th
11b30 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 53 51 4c 69  e following SQLi
11b40 74 65 20 69 6e 74 65 72 66 61 63 65 73 20 62 65  te interfaces be
11b50 63 6f 6d 65 20 0a 2a 2a 20 6e 6f 6e 2d 6f 70 65  come .** non-ope
11b60 72 61 74 69 6f 6e 61 6c 3a 0a 2a 2a 20 20 20 3c  rational:.**   <
11b70 75 6c 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73  ul>.**   <li> [s
11b80 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
11b90 65 64 28 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20  ed()].**   <li> 
11ba0 5b 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f  [sqlite3_memory_
11bb0 68 69 67 68 77 61 74 65 72 28 29 5d 0a 2a 2a 20  highwater()].** 
11bc0 20 20 3c 6c 69 3e 20 5b 73 71 6c 69 74 65 33 5f    <li> [sqlite3_
11bd0 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28  soft_heap_limit(
11be0 29 5d 0a 2a 2a 20 20 20 3c 6c 69 3e 20 5b 73 71  )].**   <li> [sq
11bf0 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d 0a  lite3_status()].
11c00 2a 2a 20 20 20 3c 2f 75 6c 3e 0a 2a 2a 20 3c 2f  **   </ul>.** </
11c10 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
11c20 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
11c30 54 43 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TCH</dt>.** <dd>
11c40 54 68 69 73 20 6f 70 74 69 6f 6e 20 73 70 65 63  This option spec
11c50 69 66 69 65 73 20 61 20 73 74 61 74 69 63 20 6d  ifies a static m
11c60 65 6d 6f 72 79 20 62 75 66 66 65 72 20 74 68 61  emory buffer tha
11c70 74 20 53 51 4c 69 74 65 20 63 61 6e 20 75 73 65  t SQLite can use
11c80 20 66 6f 72 0a 2a 2a 20 73 63 72 61 74 63 68 20   for.** scratch 
11c90 6d 65 6d 6f 72 79 2e 20 20 54 68 65 72 65 20 61  memory.  There a
11ca0 72 65 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e  re three argumen
11cb0 74 73 3a 20 20 41 20 70 6f 69 6e 74 65 72 20 74  ts:  A pointer t
11cc0 6f 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74 68  o the memory, th
11cd0 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 65 61 63  e.** size of eac
11ce0 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  h scratch buffer
11cf0 20 28 73 7a 29 2c 20 61 6e 64 20 74 68 65 20 6e   (sz), and the n
11d00 75 6d 62 65 72 20 6f 66 20 62 75 66 66 65 72 73  umber of buffers
11d10 20 28 4e 29 2e 20 20 54 68 65 20 73 7a 0a 2a 2a   (N).  The sz.**
11d20 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74 20 62   argument must b
11d30 65 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66 20  e a multiple of 
11d40 31 36 2e 20 54 68 65 20 73 7a 20 70 61 72 61 6d  16. The sz param
11d50 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65 20 61  eter should be a
11d60 20 66 65 77 20 62 79 74 65 73 0a 2a 2a 20 6c 61   few bytes.** la
11d70 72 67 65 72 20 74 68 61 6e 20 74 68 65 20 61 63  rger than the ac
11d80 74 75 61 6c 20 73 63 72 61 74 63 68 20 73 70 61  tual scratch spa
11d90 63 65 20 72 65 71 75 69 72 65 64 20 64 75 65 20  ce required due 
11da0 69 6e 74 65 72 6e 61 6c 20 6f 76 65 72 68 65 61  internal overhea
11db0 64 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 0a  d..** The first.
11dc0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 73 68 6f 75  ** argument shou
11dd0 6c 64 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 61  ld point to an a
11de0 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 61 74 20  llocation of at 
11df0 6c 65 61 73 74 20 73 7a 2a 4e 20 62 79 74 65 73  least sz*N bytes
11e00 20 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 53   of memory..** S
11e10 51 4c 69 74 65 20 77 69 6c 6c 20 75 73 65 20 6e  QLite will use n
11e20 6f 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20  o more than one 
11e30 73 63 72 61 74 63 68 20 62 75 66 66 65 72 20 61  scratch buffer a
11e40 74 20 6f 6e 63 65 20 70 65 72 20 74 68 72 65 61  t once per threa
11e50 64 2c 20 73 6f 0a 2a 2a 20 4e 20 73 68 6f 75 6c  d, so.** N shoul
11e60 64 20 62 65 20 73 65 74 20 74 6f 20 74 68 65 20  d be set to the 
11e70 65 78 70 65 63 74 65 64 20 6d 61 78 69 6d 75 6d  expected maximum
11e80 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 72 65 61   number of threa
11e90 64 73 2e 20 20 54 68 65 20 73 7a 0a 2a 2a 20 70  ds.  The sz.** p
11ea0 61 72 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20  arameter should 
11eb0 62 65 20 36 20 74 69 6d 65 73 20 74 68 65 20 73  be 6 times the s
11ec0 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72 67 65  ize of the large
11ed0 73 74 20 64 61 74 61 62 61 73 65 20 70 61 67 65  st database page
11ee0 20 73 69 7a 65 2e 0a 2a 2a 20 53 63 72 61 74 63   size..** Scratc
11ef0 68 20 62 75 66 66 65 72 73 20 61 72 65 20 75 73  h buffers are us
11f00 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 74 68  ed as part of th
11f10 65 20 62 74 72 65 65 20 62 61 6c 61 6e 63 65 20  e btree balance 
11f20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 49 66 0a 2a  operation.  If.*
11f30 2a 20 54 68 65 20 62 74 72 65 65 20 62 61 6c 61  * The btree bala
11f40 6e 63 65 72 20 6e 65 65 64 73 20 61 64 64 69 74  ncer needs addit
11f50 69 6f 6e 61 6c 20 6d 65 6d 6f 72 79 20 62 65 79  ional memory bey
11f60 6f 6e 64 20 77 68 61 74 20 69 73 20 70 72 6f 76  ond what is prov
11f70 69 64 65 64 20 62 79 0a 2a 2a 20 73 63 72 61 74  ided by.** scrat
11f80 63 68 20 62 75 66 66 65 72 73 20 6f 72 20 69 66  ch buffers or if
11f90 20 6e 6f 20 73 63 72 61 74 63 68 20 62 75 66 66   no scratch buff
11fa0 65 72 20 73 70 61 63 65 20 69 73 20 73 70 65 63  er space is spec
11fb0 69 66 69 65 64 2c 20 74 68 65 6e 20 53 51 4c 69  ified, then SQLi
11fc0 74 65 0a 2a 2a 20 67 6f 65 73 20 74 6f 20 5b 73  te.** goes to [s
11fd0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
11fe0 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 6d   to obtain the m
11ff0 65 6d 6f 72 79 20 69 74 20 6e 65 65 64 73 2e 3c  emory it needs.<
12000 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53  /dd>.**.** <dt>S
12010 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47  QLITE_CONFIG_PAG
12020 45 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c  ECACHE</dt>.** <
12030 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73  dd>This option s
12040 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69  pecifies a stati
12050 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  c memory buffer 
12060 74 68 61 74 20 53 51 4c 69 74 65 20 63 61 6e 20  that SQLite can 
12070 75 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 64  use for.** the d
12080 61 74 61 62 61 73 65 20 70 61 67 65 20 63 61 63  atabase page cac
12090 68 65 20 77 69 74 68 20 74 68 65 20 64 65 66 61  he with the defa
120a0 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20 69  ult page cache i
120b0 6d 70 6c 65 6d 65 6e 61 74 69 6f 6e 2e 20 20 0a  mplemenation.  .
120c0 2a 2a 20 54 68 69 73 20 63 6f 6e 66 69 67 75 72  ** This configur
120d0 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6e 6f 74  ation should not
120e0 20 62 65 20 75 73 65 64 20 69 66 20 61 6e 20 61   be used if an a
120f0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
12100 65 20 70 61 67 65 0a 2a 2a 20 63 61 63 68 65 20  e page.** cache 
12110 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69  implementation i
12120 73 20 6c 6f 61 64 65 64 20 75 73 69 6e 67 20 74  s loaded using t
12130 68 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  he SQLITE_CONFIG
12140 5f 50 43 41 43 48 45 20 6f 70 74 69 6f 6e 2e 0a  _PCACHE option..
12150 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72  ** There are thr
12160 65 65 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20  ee arguments to 
12170 74 68 69 73 20 6f 70 74 69 6f 6e 3a 20 41 20 70  this option: A p
12180 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 0a 2a 2a  ointer to the.**
12190 20 6d 65 6d 6f 72 79 2c 20 74 68 65 20 73 69 7a   memory, the siz
121a0 65 20 6f 66 20 65 61 63 68 20 70 61 67 65 20 62  e of each page b
121b0 75 66 66 65 72 20 28 73 7a 29 2c 20 61 6e 64 20  uffer (sz), and 
121c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  the number of pa
121d0 67 65 73 20 28 4e 29 2e 0a 2a 2a 20 54 68 65 20  ges (N)..** The 
121e0 73 7a 20 61 72 67 75 6d 65 6e 74 20 6d 75 73 74  sz argument must
121f0 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74   be a power of t
12200 77 6f 20 62 65 74 77 65 65 6e 20 35 31 32 20 61  wo between 512 a
12210 6e 64 20 33 32 37 36 38 2e 20 20 54 68 65 20 66  nd 32768.  The f
12220 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74  irst.** argument
12230 20 73 68 6f 75 6c 64 20 70 6f 69 6e 74 20 74 6f   should point to
12240 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 6f   an allocation o
12250 66 20 61 74 20 6c 65 61 73 74 20 73 7a 2a 4e 20  f at least sz*N 
12260 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 2e  bytes of memory.
12270 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
12280 75 73 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 70  use the memory p
12290 72 6f 76 69 64 65 64 20 62 79 20 74 68 65 20 66  rovided by the f
122a0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
122b0 20 73 61 74 69 73 66 79 20 69 74 73 0a 2a 2a 20   satisfy its.** 
122c0 6d 65 6d 6f 72 79 20 6e 65 65 64 73 20 66 6f 72  memory needs for
122d0 20 74 68 65 20 66 69 72 73 74 20 4e 20 70 61 67   the first N pag
122e0 65 73 20 74 68 61 74 20 69 74 20 61 64 64 73 20  es that it adds 
122f0 74 6f 20 63 61 63 68 65 2e 20 20 49 66 20 61 64  to cache.  If ad
12300 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 70 61 67 65  ditional.** page
12310 20 63 61 63 68 65 20 6d 65 6d 6f 72 79 20 69 73   cache memory is
12320 20 6e 65 65 64 65 64 20 62 65 79 6f 6e 64 20 77   needed beyond w
12330 68 61 74 20 69 73 20 70 72 6f 76 69 64 65 64 20  hat is provided 
12340 62 79 20 74 68 69 73 20 6f 70 74 69 6f 6e 2c 20  by this option, 
12350 74 68 65 6e 0a 2a 2a 20 53 51 4c 69 74 65 20 67  then.** SQLite g
12360 6f 65 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  oes to [sqlite3_
12370 6d 61 6c 6c 6f 63 28 29 5d 20 66 6f 72 20 74 68  malloc()] for th
12380 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 6f  e additional sto
12390 72 61 67 65 20 73 70 61 63 65 2e 0a 2a 2a 20 54  rage space..** T
123a0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
123b0 6e 20 6d 69 67 68 74 20 75 73 65 20 6f 6e 65 20  n might use one 
123c0 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 4e  or more of the N
123d0 20 62 75 66 66 65 72 73 20 74 6f 20 68 6f 6c 64   buffers to hold
123e0 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 63 63 6f   .** memory acco
123f0 75 6e 74 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69  unting informati
12400 6f 6e 2e 20 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  on. </dd>.**.** 
12410 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49  <dt>SQLITE_CONFI
12420 47 5f 48 45 41 50 3c 2f 64 74 3e 0a 2a 2a 20 3c  G_HEAP</dt>.** <
12430 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 73  dd>This option s
12440 70 65 63 69 66 69 65 73 20 61 20 73 74 61 74 69  pecifies a stati
12450 63 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72 20  c memory buffer 
12460 74 68 61 74 20 53 51 4c 69 74 65 20 77 69 6c 6c  that SQLite will
12470 20 75 73 65 0a 2a 2a 20 66 6f 72 20 61 6c 6c 20   use.** for all 
12480 6f 66 20 69 74 73 20 64 79 6e 61 6d 69 63 20 6d  of its dynamic m
12490 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
124a0 20 6e 65 65 64 73 20 62 65 79 6f 6e 64 20 74 68   needs beyond th
124b0 6f 73 65 20 70 72 6f 76 69 64 65 64 0a 2a 2a 20  ose provided.** 
124c0 66 6f 72 20 62 79 20 5b 53 51 4c 49 54 45 5f 43  for by [SQLITE_C
124d0 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61  ONFIG_SCRATCH] a
124e0 6e 64 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49  nd [SQLITE_CONFI
124f0 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 0a 2a 2a  G_PAGECACHE]..**
12500 20 54 68 65 72 65 20 61 72 65 20 74 68 72 65 65   There are three
12510 20 61 72 67 75 6d 65 6e 74 73 3a 20 41 20 70 6f   arguments: A po
12520 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 65 6d  inter to the mem
12530 6f 72 79 2c 20 74 68 65 20 6e 75 6d 62 65 72 20  ory, the number 
12540 6f 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74  of.** bytes in t
12550 68 65 20 6d 65 6d 6f 72 79 20 62 75 66 66 65 72  he memory buffer
12560 2c 20 61 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75  , and the minimu
12570 6d 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a  m allocation siz
12580 65 2e 20 20 49 66 0a 2a 2a 20 74 68 65 20 66 69  e.  If.** the fi
12590 72 73 74 20 70 6f 69 6e 74 65 72 20 28 74 68 65  rst pointer (the
125a0 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 72 29   memory pointer)
125b0 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 53   is NULL, then S
125c0 51 4c 69 74 65 20 72 65 76 65 72 74 73 0a 2a 2a  QLite reverts.**
125d0 20 74 6f 20 75 73 69 6e 67 20 69 74 73 20 64 65   to using its de
125e0 66 61 75 6c 74 20 6d 65 6d 6f 72 79 20 61 6c 6c  fault memory all
125f0 6f 63 61 74 6f 72 20 28 74 68 65 20 73 79 73 74  ocator (the syst
12600 65 6d 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c  em malloc() impl
12610 65 6d 65 6e 74 61 74 69 6f 6e 29 2c 0a 2a 2a 20  ementation),.** 
12620 75 6e 64 6f 69 6e 67 20 61 6e 79 20 70 72 69 6f  undoing any prio
12630 72 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  r invocation of 
12640 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
12650 41 4c 4c 4f 43 5d 2e 20 20 49 66 20 74 68 65 0a  ALLOC].  If the.
12660 2a 2a 20 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65  ** memory pointe
12670 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61 6e  r is not NULL an
12680 64 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54 45  d either [SQLITE
12690 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 5d  _ENABLE_MEMSYS3]
126a0 20 6f 72 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45   or.** [SQLITE_E
126b0 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 5d 20 61  NABLE_MEMSYS5] a
126c0 72 65 20 64 65 66 69 6e 65 64 2c 20 74 68 65 6e  re defined, then
126d0 20 74 68 65 20 61 6c 74 65 72 6e 61 74 69 76 65   the alternative
126e0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c 6f 63   memory.** alloc
126f0 61 74 6f 72 20 69 73 20 65 6e 67 61 67 65 64 20  ator is engaged 
12700 74 6f 20 68 61 6e 64 6c 65 20 61 6c 6c 20 6f 66  to handle all of
12710 20 53 51 4c 69 74 65 73 20 6d 65 6d 6f 72 79 20   SQLites memory 
12720 61 6c 6c 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73  allocation needs
12730 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
12740 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  >SQLITE_CONFIG_M
12750 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64  UTEX</dt>.** <dd
12760 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61 6b  >This option tak
12770 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75  es a single argu
12780 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61 20  ment which is a 
12790 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a 2a 2a  pointer to an.**
127a0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
127b0 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f   [sqlite3_mutex_
127c0 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63 74 75  methods] structu
127d0 72 65 2e 20 20 54 68 65 20 61 72 67 75 6d 65 6e  re.  The argumen
127e0 74 20 73 70 65 63 69 66 69 65 73 0a 2a 2a 20 61  t specifies.** a
127f0 6c 74 65 72 6e 61 74 69 76 65 20 6c 6f 77 2d 6c  lternative low-l
12800 65 76 65 6c 20 6d 75 74 65 78 20 72 6f 75 74 69  evel mutex routi
12810 6e 65 73 20 74 6f 20 62 65 20 75 73 65 64 20 69  nes to be used i
12820 6e 20 70 6c 61 63 65 0a 2a 2a 20 74 68 65 20 6d  n place.** the m
12830 75 74 65 78 20 72 6f 75 74 69 6e 65 73 20 62 75  utex routines bu
12840 69 6c 74 20 69 6e 74 6f 20 53 51 4c 69 74 65 2e  ilt into SQLite.
12850 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
12860 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
12870 54 4d 55 54 45 58 3c 2f 64 74 3e 0a 2a 2a 20 3c  TMUTEX</dt>.** <
12880 64 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74  dd>This option t
12890 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72  akes a single ar
128a0 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20  gument which is 
128b0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 0a  a pointer to an.
128c0 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
128d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65  he [sqlite3_mute
128e0 78 5f 6d 65 74 68 6f 64 73 5d 20 73 74 72 75 63  x_methods] struc
128f0 74 75 72 65 2e 20 20 54 68 65 0a 2a 2a 20 5b 73  ture.  The.** [s
12900 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
12910 68 6f 64 73 5d 0a 2a 2a 20 73 74 72 75 63 74 75  hods].** structu
12920 72 65 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74  re is filled wit
12930 68 20 74 68 65 20 63 75 72 72 65 6e 74 6c 79 20  h the currently 
12940 64 65 66 69 6e 65 64 20 6d 75 74 65 78 20 72 6f  defined mutex ro
12950 75 74 69 6e 65 73 2e 0a 2a 2a 20 54 68 69 73 20  utines..** This 
12960 6f 70 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73  option can be us
12970 65 64 20 74 6f 20 6f 76 65 72 6c 6f 61 64 20 74  ed to overload t
12980 68 65 20 64 65 66 61 75 6c 74 20 6d 75 74 65 78  he default mutex
12990 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72   allocation.** r
129a0 6f 75 74 69 6e 65 73 20 77 69 74 68 20 61 20 77  outines with a w
129b0 72 61 70 70 65 72 20 75 73 65 64 20 74 6f 20 74  rapper used to t
129c0 72 61 63 6b 20 6d 75 74 65 78 20 75 73 61 67 65  rack mutex usage
129d0 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65   for performance
129e0 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 20 6f 72  .** profiling or
129f0 20 74 65 73 74 69 6e 67 2c 20 66 6f 72 20 65 78   testing, for ex
12a00 61 6d 70 6c 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  ample.</dd>.**.*
12a10 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
12a20 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3c 2f 64  FIG_LOOKASIDE</d
12a30 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
12a40 70 74 69 6f 6e 20 74 61 6b 65 73 20 74 77 6f 20  ption takes two 
12a50 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64  arguments that d
12a60 65 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65 66  etermine the def
12a70 61 75 6c 74 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61  ault.** memory a
12a80 6c 6c 63 61 74 69 6f 6e 20 6c 6f 6f 6b 61 73 69  llcation lookasi
12a90 64 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2e  de optimization.
12aa0 20 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75    The first argu
12ab0 6d 65 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73  ment is the.** s
12ac0 69 7a 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b  ize of each look
12ad0 61 73 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f  aside buffer slo
12ae0 74 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  t and the second
12af0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
12b00 66 0a 2a 2a 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  f.** slots alloc
12b10 61 74 65 64 20 74 6f 20 65 61 63 68 20 64 61 74  ated to each dat
12b20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
12b30 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
12b40 3e 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  >SQLITE_CONFIG_P
12b50 43 41 43 48 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64  CACHE</dt>.** <d
12b60 64 3e 54 68 69 73 20 6f 70 74 69 6f 6e 20 74 61  d>This option ta
12b70 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61 72 67  kes a single arg
12b80 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20 61  ument which is a
12b90 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 61   pointer to.** a
12ba0 6e 20 5b 73 71 6c 69 74 65 33 5f 70 63 61 63 68  n [sqlite3_pcach
12bb0 65 5f 6d 65 74 68 6f 64 73 5d 20 6f 62 6a 65 63  e_methods] objec
12bc0 74 2e 20 20 54 68 69 73 20 6f 62 6a 65 63 74 20  t.  This object 
12bd0 73 70 65 63 69 66 69 65 73 20 74 68 65 20 69 6e  specifies the in
12be0 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 61 20  terface.** to a 
12bf0 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68  custom page cach
12c00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
12c10 2e 20 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20  .  SQLite makes 
12c20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
12c30 20 6f 62 6a 65 63 74 20 61 6e 64 20 75 73 65 73   object and uses
12c40 20 69 74 20 66 6f 72 20 70 61 67 65 20 63 61 63   it for page cac
12c50 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
12c60 74 69 6f 6e 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a  tions.</dd>.**.*
12c70 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 43 4f 4e  * <dt>SQLITE_CON
12c80 46 49 47 5f 47 45 54 50 43 41 43 48 45 3c 2f 64  FIG_GETPCACHE</d
12c90 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 6f  t>.** <dd>This o
12ca0 70 74 69 6f 6e 20 74 61 6b 65 73 20 61 20 73 69  ption takes a si
12cb0 6e 67 6c 65 20 61 72 67 75 6d 65 6e 74 20 77 68  ngle argument wh
12cc0 69 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ich is a pointer
12cd0 20 74 6f 20 61 6e 0a 2a 2a 20 5b 73 71 6c 69 74   to an.** [sqlit
12ce0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
12cf0 73 5d 20 6f 62 6a 65 63 74 2e 20 20 53 51 4c 69  s] object.  SQLi
12d00 74 65 20 63 6f 70 69 65 73 20 6f 66 20 74 68 65  te copies of the
12d10 20 63 75 72 72 65 6e 74 0a 2a 2a 20 70 61 67 65   current.** page
12d20 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
12d30 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 61 74 20  ation into that 
12d40 6f 62 6a 65 63 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a  object.</dd>.**.
12d50 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23 64 65 66  ** </dl>.*/.#def
12d60 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ine SQLITE_CONFI
12d70 47 5f 53 49 4e 47 4c 45 54 48 52 45 41 44 20 20  G_SINGLETHREAD  
12d80 31 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64 65  1  /* nil */.#de
12d90 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46  fine SQLITE_CONF
12da0 49 47 5f 4d 55 4c 54 49 54 48 52 45 41 44 20 20  IG_MULTITHREAD  
12db0 20 32 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23 64   2  /* nil */.#d
12dc0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12dd0 46 49 47 5f 53 45 52 49 41 4c 49 5a 45 44 20 20  FIG_SERIALIZED  
12de0 20 20 33 20 20 2f 2a 20 6e 69 6c 20 2a 2f 0a 23    3  /* nil */.#
12df0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f  define SQLITE_CO
12e00 4e 46 49 47 5f 4d 41 4c 4c 4f 43 20 20 20 20 20  NFIG_MALLOC     
12e10 20 20 20 34 20 20 2f 2a 20 73 71 6c 69 74 65 33     4  /* sqlite3
12e20 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f  _mem_methods* */
12e30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
12e40 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f 43  CONFIG_GETMALLOC
12e50 20 20 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74       5  /* sqlit
12e60 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 2a 20  e3_mem_methods* 
12e70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
12e80 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41 54 43 48  E_CONFIG_SCRATCH
12e90 20 20 20 20 20 20 20 36 20 20 2f 2a 20 76 6f 69         6  /* voi
12ea0 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74 20  d*, int sz, int 
12eb0 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  N */.#define SQL
12ec0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
12ed0 41 43 48 45 20 20 20 20 20 37 20 20 2f 2a 20 76  ACHE     7  /* v
12ee0 6f 69 64 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  oid*, int sz, in
12ef0 74 20 4e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  t N */.#define S
12f00 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41  QLITE_CONFIG_HEA
12f10 50 20 20 20 20 20 20 20 20 20 20 38 20 20 2f 2a  P          8  /*
12f20 20 76 6f 69 64 2a 2c 20 69 6e 74 20 6e 42 79 74   void*, int nByt
12f30 65 2c 20 69 6e 74 20 6d 69 6e 20 2a 2f 0a 23 64  e, int min */.#d
12f40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
12f50 46 49 47 5f 4d 45 4d 53 54 41 54 55 53 20 20 20  FIG_MEMSTATUS   
12f60 20 20 39 20 20 2f 2a 20 62 6f 6f 6c 65 61 6e 20    9  /* boolean 
12f70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
12f80 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45 58 20 20  E_CONFIG_MUTEX  
12f90 20 20 20 20 20 20 31 30 20 20 2f 2a 20 73 71 6c        10  /* sql
12fa0 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f  ite3_mutex_metho
12fb0 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ds* */.#define S
12fc0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
12fd0 4d 55 54 45 58 20 20 20 20 20 31 31 20 20 2f 2a  MUTEX     11  /*
12fe0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
12ff0 65 74 68 6f 64 73 2a 20 2a 2f 0a 2f 2a 20 70 72  ethods* */./* pr
13000 65 76 69 6f 75 73 6c 79 20 53 51 4c 49 54 45 5f  eviously SQLITE_
13010 43 4f 4e 46 49 47 5f 43 48 55 4e 4b 41 4c 4c 4f  CONFIG_CHUNKALLO
13020 43 20 31 32 20 77 68 69 63 68 20 69 73 20 6e 6f  C 12 which is no
13030 77 20 75 6e 75 73 65 64 2e 20 2a 2f 20 0a 23 64  w unused. */ .#d
13040 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4e  efine SQLITE_CON
13050 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 20 20 20  FIG_LOOKASIDE   
13060 20 31 33 20 20 2f 2a 20 69 6e 74 20 69 6e 74 20   13  /* int int 
13070 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
13080 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 20  E_CONFIG_PCACHE 
13090 20 20 20 20 20 20 31 34 20 20 2f 2a 20 73 71 6c        14  /* sql
130a0 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
130b0 6f 64 73 2a 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ods* */.#define 
130c0 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
130d0 54 50 43 41 43 48 45 20 20 20 20 31 35 20 20 2f  TPCACHE    15  /
130e0 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  * sqlite3_pcache
130f0 5f 6d 65 74 68 6f 64 73 2a 20 2a 2f 0a 0a 2f 2a  _methods* */../*
13100 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f  .** CAPI3REF: Co
13110 6e 66 69 67 75 72 61 74 69 6f 6e 20 4f 70 74 69  nfiguration Opti
13120 6f 6e 73 20 7b 48 31 30 31 37 30 7d 20 3c 53 32  ons {H10170} <S2
13130 30 30 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0000>.** EXPERIM
13140 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 73  ENTAL.**.** Thes
13150 65 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20  e constants are 
13160 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  the available in
13170 74 65 67 65 72 20 63 6f 6e 66 69 67 75 72 61 74  teger configurat
13180 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 74 68 61 74  ion options that
13190 0a 2a 2a 20 63 61 6e 20 62 65 20 70 61 73 73 65  .** can be passe
131a0 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
131b0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
131c0 5b 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e 66  [sqlite3_db_conf
131d0 69 67 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e  ig()] interface.
131e0 0a 2a 2a 0a 2a 2a 20 4e 65 77 20 63 6f 6e 66 69  .**.** New confi
131f0 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73  guration options
13200 20 6d 61 79 20 62 65 20 61 64 64 65 64 20 69 6e   may be added in
13210 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65 73   future releases
13220 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2a 20 45   of SQLite..** E
13230 78 69 73 74 69 6e 67 20 63 6f 6e 66 69 67 75 72  xisting configur
13240 61 74 69 6f 6e 20 6f 70 74 69 6f 6e 73 20 6d 69  ation options mi
13250 67 68 74 20 62 65 20 64 69 73 63 6f 6e 74 69 6e  ght be discontin
13260 75 65 64 2e 20 20 41 70 70 6c 69 63 61 74 69 6f  ued.  Applicatio
13270 6e 73 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 68 65  ns.** should che
13280 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  ck the return co
13290 64 65 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  de from [sqlite3
132a0 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 74 6f  _db_config()] to
132b0 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a   make sure that.
132c0 2a 2a 20 74 68 65 20 63 61 6c 6c 20 77 6f 72 6b  ** the call work
132d0 65 64 2e 20 20 54 68 65 20 5b 73 71 6c 69 74 65  ed.  The [sqlite
132e0 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69  3_db_config()] i
132f0 6e 74 65 72 66 61 63 65 20 77 69 6c 6c 20 72 65  nterface will re
13300 74 75 72 6e 20 61 0a 2a 2a 20 6e 6f 6e 2d 7a 65  turn a.** non-ze
13310 72 6f 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  ro [error code] 
13320 69 66 20 61 20 64 69 73 63 6f 6e 74 69 6e 75 65  if a discontinue
13330 64 20 6f 72 20 75 6e 73 75 70 70 6f 72 74 65 64  d or unsupported
13340 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 6f   configuration o
13350 70 74 69 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f  ption.** is invo
13360 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c 3e 0a  ked..**.** <dl>.
13370 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 44 42  ** <dt>SQLITE_DB
13380 43 4f 4e 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45  CONFIG_LOOKASIDE
13390 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
133a0 73 20 6f 70 74 69 6f 6e 20 74 61 6b 65 73 20 74  s option takes t
133b0 68 72 65 65 20 61 64 64 69 74 69 6f 6e 61 6c 20  hree additional 
133c0 61 72 67 75 6d 65 6e 74 73 20 74 68 61 74 20 64  arguments that d
133d0 65 74 65 72 6d 69 6e 65 20 74 68 65 20 0a 2a 2a  etermine the .**
133e0 20 5b 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d 6f   [lookaside memo
133f0 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f  ry allocator] co
13400 6e 66 69 67 75 72 61 74 69 6f 6e 20 66 6f 72 20  nfiguration for 
13410 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  the [database co
13420 6e 6e 65 63 74 69 6f 6e 5d 2e 0a 2a 2a 20 54 68  nnection]..** Th
13430 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
13440 20 28 74 68 65 20 74 68 69 72 64 20 70 61 72 61   (the third para
13450 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
13460 33 5f 64 62 5f 63 6f 6e 66 69 67 28 29 5d 20 69  3_db_config()] i
13470 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74  s a.** pointer t
13480 6f 20 61 20 6d 65 6d 6f 72 79 20 62 75 66 66 65  o a memory buffe
13490 72 20 74 6f 20 75 73 65 20 66 6f 72 20 6c 6f 6f  r to use for loo
134a0 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 20 20  kaside memory.  
134b0 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72 67  The first.** arg
134c0 75 6d 65 6e 74 20 6d 61 79 20 62 65 20 4e 55 4c  ument may be NUL
134d0 4c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20  L in which case 
134e0 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c 6f  SQLite will allo
134f0 63 61 74 65 20 74 68 65 20 6c 6f 6f 6b 61 73 69  cate the lookasi
13500 64 65 0a 2a 2a 20 62 75 66 66 65 72 20 69 74 73  de.** buffer its
13510 65 6c 66 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  elf using [sqlit
13520 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 54  e3_malloc()].  T
13530 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
13540 6e 74 20 69 73 20 74 68 65 0a 2a 2a 20 73 69 7a  nt is the.** siz
13550 65 20 6f 66 20 65 61 63 68 20 6c 6f 6f 6b 61 73  e of each lookas
13560 69 64 65 20 62 75 66 66 65 72 20 73 6c 6f 74 20  ide buffer slot 
13570 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 61 72  and the third ar
13580 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75  gument is the nu
13590 6d 62 65 72 20 6f 66 0a 2a 2a 20 73 6c 6f 74 73  mber of.** slots
135a0 2e 20 20 54 68 65 20 73 69 7a 65 20 6f 66 20 74  .  The size of t
135b0 68 65 20 62 75 66 66 65 72 20 69 6e 20 74 68 65  he buffer in the
135c0 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   first argument 
135d0 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72 20  must be greater 
135e0 74 68 61 6e 0a 2a 2a 20 6f 72 20 65 71 75 61 6c  than.** or equal
135f0 20 74 6f 20 74 68 65 20 70 72 6f 64 75 63 74 20   to the product 
13600 6f 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 6e  of the second an
13610 64 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e 74  d third argument
13620 73 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 2f  s.</dd>.**.** </
13630 64 6c 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  dl>.*/.#define S
13640 51 4c 49 54 45 5f 44 42 43 4f 4e 46 49 47 5f 4c  QLITE_DBCONFIG_L
13650 4f 4f 4b 41 53 49 44 45 20 20 20 20 31 30 30 31  OOKASIDE    1001
13660 20 20 2f 2a 20 76 6f 69 64 2a 20 69 6e 74 20 69    /* void* int i
13670 6e 74 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 43 41  nt */.../*.** CA
13680 50 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f  PI3REF: Enable O
13690 72 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e 64  r Disable Extend
136a0 65 64 20 52 65 73 75 6c 74 20 43 6f 64 65 73 20  ed Result Codes 
136b0 7b 48 31 32 32 30 30 7d 20 3c 53 31 30 37 30 30  {H12200} <S10700
136c0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
136d0 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73  te3_extended_res
136e0 75 6c 74 5f 63 6f 64 65 73 28 29 20 72 6f 75 74  ult_codes() rout
136f0 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20 64  ine enables or d
13700 69 73 61 62 6c 65 73 20 74 68 65 0a 2a 2a 20 5b  isables the.** [
13710 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
13720 63 6f 64 65 73 5d 20 66 65 61 74 75 72 65 20 6f  codes] feature o
13730 66 20 53 51 4c 69 74 65 2e 20 54 68 65 20 65 78  f SQLite. The ex
13740 74 65 6e 64 65 64 20 72 65 73 75 6c 74 0a 2a 2a  tended result.**
13750 20 63 6f 64 65 73 20 61 72 65 20 64 69 73 61 62   codes are disab
13760 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 20 66  led by default f
13770 6f 72 20 68 69 73 74 6f 72 69 63 61 6c 20 63 6f  or historical co
13780 6d 70 61 74 69 62 69 6c 69 74 79 20 63 6f 6e 73  mpatibility cons
13790 69 64 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  iderations..**.*
137a0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
137b0 0a 2a 2a 20 7b 48 31 32 32 30 31 7d 20 45 61 63  .** {H12201} Eac
137c0 68 20 6e 65 77 20 5b 64 61 74 61 62 61 73 65 20  h new [database 
137d0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 68 61 6c  connection] shal
137e0 6c 20 68 61 76 65 20 74 68 65 0a 2a 2a 20 20 20  l have the.**   
137f0 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64         [extended
13800 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66   result codes] f
13810 65 61 74 75 72 65 20 64 69 73 61 62 6c 65 64 20  eature disabled 
13820 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a  by default..**.*
13830 2a 20 7b 48 31 32 32 30 32 7d 20 54 68 65 20 5b  * {H12202} The [
13840 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64  sqlite3_extended
13850 5f 72 65 73 75 6c 74 5f 63 6f 64 65 73 28 44 2c  _result_codes(D,
13860 46 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68  F)] interface sh
13870 61 6c 6c 20 65 6e 61 62 6c 65 0a 2a 2a 20 20 20  all enable.**   
13880 20 20 20 20 20 20 20 5b 65 78 74 65 6e 64 65 64         [extended
13890 20 72 65 73 75 6c 74 20 63 6f 64 65 73 5d 20 66   result codes] f
138a0 6f 72 20 74 68 65 20 20 5b 64 61 74 61 62 61 73  or the  [databas
138b0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 0a  e connection] D.
138c0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 74  **          if t
138d0 68 65 20 46 20 70 61 72 61 6d 65 74 65 72 20 69  he F parameter i
138e0 73 20 74 72 75 65 2c 20 6f 72 20 64 69 73 61 62  s true, or disab
138f0 6c 65 20 74 68 65 6d 20 69 66 20 46 20 69 73 20  le them if F is 
13900 66 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  false..*/.SQLITE
13910 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
13920 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c 74  _extended_result
13930 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 2a 2c  _codes(sqlite3*,
13940 20 69 6e 74 20 6f 6e 6f 66 66 29 3b 0a 0a 2f 2a   int onoff);../*
13950 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4c 61  .** CAPI3REF: La
13960 73 74 20 49 6e 73 65 72 74 20 52 6f 77 69 64 20  st Insert Rowid 
13970 7b 48 31 32 32 32 30 7d 20 3c 53 31 30 37 30 30  {H12220} <S10700
13980 3e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 65 6e 74  >.**.** Each ent
13990 72 79 20 69 6e 20 61 6e 20 53 51 4c 69 74 65 20  ry in an SQLite 
139a0 74 61 62 6c 65 20 68 61 73 20 61 20 75 6e 69 71  table has a uniq
139b0 75 65 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64  ue 64-bit signed
139c0 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6b 65 79 20  .** integer key 
139d0 63 61 6c 6c 65 64 20 74 68 65 20 22 72 6f 77 69  called the "rowi
139e0 64 22 2e 20 54 68 65 20 72 6f 77 69 64 20 69 73  d". The rowid is
139f0 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
13a00 65 0a 2a 2a 20 61 73 20 61 6e 20 75 6e 64 65 63  e.** as an undec
13a10 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d  lared column nam
13a20 65 64 20 52 4f 57 49 44 2c 20 4f 49 44 2c 20 6f  ed ROWID, OID, o
13a30 72 20 5f 52 4f 57 49 44 5f 20 61 73 20 6c 6f 6e  r _ROWID_ as lon
13a40 67 20 61 73 20 74 68 6f 73 65 0a 2a 2a 20 6e 61  g as those.** na
13a50 6d 65 73 20 61 72 65 20 6e 6f 74 20 61 6c 73 6f  mes are not also
13a60 20 75 73 65 64 20 62 79 20 65 78 70 6c 69 63 69   used by explici
13a70 74 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c  tly declared col
13a80 75 6d 6e 73 2e 20 49 66 0a 2a 2a 20 74 68 65 20  umns. If.** the 
13a90 74 61 62 6c 65 20 68 61 73 20 61 20 63 6f 6c 75  table has a colu
13aa0 6d 6e 20 6f 66 20 74 79 70 65 20 49 4e 54 45 47  mn of type INTEG
13ab0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 74  ER PRIMARY KEY t
13ac0 68 65 6e 20 74 68 61 74 20 63 6f 6c 75 6d 6e 0a  hen that column.
13ad0 2a 2a 20 69 73 20 61 6e 6f 74 68 65 72 20 61 6c  ** is another al
13ae0 69 61 73 20 66 6f 72 20 74 68 65 20 72 6f 77 69  ias for the rowi
13af0 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  d..**.** This ro
13b00 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68  utine returns th
13b10 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 6d  e rowid of the m
13b20 6f 73 74 20 72 65 63 65 6e 74 0a 2a 2a 20 73 75  ost recent.** su
13b30 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52 54  ccessful [INSERT
13b40 5d 20 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62  ] into the datab
13b50 61 73 65 20 66 72 6f 6d 20 74 68 65 20 5b 64 61  ase from the [da
13b60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
13b70 6e 5d 0a 2a 2a 20 69 6e 20 74 68 65 20 66 69 72  n].** in the fir
13b80 73 74 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66  st argument.  If
13b90 20 6e 6f 20 73 75 63 63 65 73 73 66 75 6c 20 5b   no successful [
13ba0 49 4e 53 45 52 54 5d 73 0a 2a 2a 20 68 61 76 65  INSERT]s.** have
13bb0 20 65 76 65 72 20 6f 63 63 75 72 72 65 64 20 6f   ever occurred o
13bc0 6e 20 74 68 61 74 20 64 61 74 61 62 61 73 65 20  n that database 
13bd0 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 7a 65 72 6f  connection, zero
13be0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
13bf0 0a 2a 2a 20 49 66 20 61 6e 20 5b 49 4e 53 45 52  .** If an [INSER
13c00 54 5d 20 6f 63 63 75 72 73 20 77 69 74 68 69 6e  T] occurs within
13c10 20 61 20 74 72 69 67 67 65 72 2c 20 74 68 65 6e   a trigger, then
13c20 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   the rowid of th
13c30 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a 20 72 6f  e inserted.** ro
13c40 77 20 69 73 20 72 65 74 75 72 6e 65 64 20 62 79  w is returned by
13c50 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61 73   this routine as
13c60 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 72 69   long as the tri
13c70 67 67 65 72 20 69 73 20 72 75 6e 6e 69 6e 67 2e  gger is running.
13c80 0a 2a 2a 20 42 75 74 20 6f 6e 63 65 20 74 68 65  .** But once the
13c90 20 74 72 69 67 67 65 72 20 74 65 72 6d 69 6e 61   trigger termina
13ca0 74 65 73 2c 20 74 68 65 20 76 61 6c 75 65 20 72  tes, the value r
13cb0 65 74 75 72 6e 65 64 20 62 79 20 74 68 69 73 20  eturned by this 
13cc0 72 6f 75 74 69 6e 65 0a 2a 2a 20 72 65 76 65 72  routine.** rever
13cd0 74 73 20 74 6f 20 74 68 65 20 6c 61 73 74 20 76  ts to the last v
13ce0 61 6c 75 65 20 69 6e 73 65 72 74 65 64 20 62 65  alue inserted be
13cf0 66 6f 72 65 20 74 68 65 20 74 72 69 67 67 65 72  fore the trigger
13d00 20 66 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e   fired..**.** An
13d10 20 5b 49 4e 53 45 52 54 5d 20 74 68 61 74 20 66   [INSERT] that f
13d20 61 69 6c 73 20 64 75 65 20 74 6f 20 61 20 63 6f  ails due to a co
13d30 6e 73 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69  nstraint violati
13d40 6f 6e 20 69 73 20 6e 6f 74 20 61 0a 2a 2a 20 73  on is not a.** s
13d50 75 63 63 65 73 73 66 75 6c 20 5b 49 4e 53 45 52  uccessful [INSER
13d60 54 5d 20 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20  T] and does not 
13d70 63 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65  change the value
13d80 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 69   returned by thi
13d90 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 20 20 54  s.** routine.  T
13da0 68 75 73 20 49 4e 53 45 52 54 20 4f 52 20 46 41  hus INSERT OR FA
13db0 49 4c 2c 20 49 4e 53 45 52 54 20 4f 52 20 49 47  IL, INSERT OR IG
13dc0 4e 4f 52 45 2c 20 49 4e 53 45 52 54 20 4f 52 20  NORE, INSERT OR 
13dd0 52 4f 4c 4c 42 41 43 4b 2c 0a 2a 2a 20 61 6e 64  ROLLBACK,.** and
13de0 20 49 4e 53 45 52 54 20 4f 52 20 41 42 4f 52 54   INSERT OR ABORT
13df0 20 6d 61 6b 65 20 6e 6f 20 63 68 61 6e 67 65 73   make no changes
13e00 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 20 76   to the return v
13e10 61 6c 75 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20  alue of this.** 
13e20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65  routine when the
13e30 69 72 20 69 6e 73 65 72 74 69 6f 6e 20 66 61 69  ir insertion fai
13e40 6c 73 2e 20 20 57 68 65 6e 20 49 4e 53 45 52 54  ls.  When INSERT
13e50 20 4f 52 20 52 45 50 4c 41 43 45 0a 2a 2a 20 65   OR REPLACE.** e
13e60 6e 63 6f 75 6e 74 65 72 73 20 61 20 63 6f 6e 73  ncounters a cons
13e70 74 72 61 69 6e 74 20 76 69 6f 6c 61 74 69 6f 6e  traint violation
13e80 2c 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 66 61  , it does not fa
13e90 69 6c 2e 20 20 54 68 65 0a 2a 2a 20 49 4e 53 45  il.  The.** INSE
13ea0 52 54 20 63 6f 6e 74 69 6e 75 65 73 20 74 6f 20  RT continues to 
13eb0 63 6f 6d 70 6c 65 74 69 6f 6e 20 61 66 74 65 72  completion after
13ec0 20 64 65 6c 65 74 69 6e 67 20 72 6f 77 73 20 74   deleting rows t
13ed0 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 74 68  hat caused.** th
13ee0 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 70 72 6f  e constraint pro
13ef0 62 6c 65 6d 20 73 6f 20 49 4e 53 45 52 54 20 4f  blem so INSERT O
13f00 52 20 52 45 50 4c 41 43 45 20 77 69 6c 6c 20 61  R REPLACE will a
13f10 6c 77 61 79 73 20 63 68 61 6e 67 65 0a 2a 2a 20  lways change.** 
13f20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
13f30 20 6f 66 20 74 68 69 73 20 69 6e 74 65 72 66 61   of this interfa
13f40 63 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ce..**.** For th
13f50 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
13f60 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 6e 20 5b  is routine, an [
13f70 49 4e 53 45 52 54 5d 20 69 73 20 63 6f 6e 73 69  INSERT] is consi
13f80 64 65 72 65 64 20 74 6f 0a 2a 2a 20 62 65 20 73  dered to.** be s
13f90 75 63 63 65 73 73 66 75 6c 20 65 76 65 6e 20 69  uccessful even i
13fa0 66 20 69 74 20 69 73 20 73 75 62 73 65 71 75 65  f it is subseque
13fb0 6e 74 6c 79 20 72 6f 6c 6c 65 64 20 62 61 63 6b  ntly rolled back
13fc0 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
13fd0 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 32  TS:.**.** {H1222
13fe0 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  1} The [sqlite3_
13ff0 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
14000 64 28 29 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68  d()] function sh
14010 61 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 72  all return the r
14020 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  owid.**         
14030 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
14040 65 6e 74 20 73 75 63 63 65 73 73 66 75 6c 20 5b  ent successful [
14050 49 4e 53 45 52 54 5d 20 70 65 72 66 6f 72 6d 65  INSERT] performe
14060 64 20 6f 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a  d on the same.**
14070 20 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62            [datab
14080 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
14090 61 6e 64 20 77 69 74 68 69 6e 20 74 68 65 20 73  and within the s
140a0 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20 6c 65  ame or higher le
140b0 76 65 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  vel.**          
140c0 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c  trigger context,
140d0 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72   or zero if ther
140e0 65 20 68 61 76 65 20 62 65 65 6e 20 6e 6f 20 71  e have been no q
140f0 75 61 6c 69 66 79 69 6e 67 0a 2a 2a 20 20 20 20  ualifying.**    
14100 20 20 20 20 20 20 5b 49 4e 53 45 52 54 5d 20 73        [INSERT] s
14110 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a  tatements..**.**
14120 20 7b 48 31 32 32 32 33 7d 20 54 68 65 20 5b 73   {H12223} The [s
14130 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65  qlite3_last_inse
14140 72 74 5f 72 6f 77 69 64 28 29 5d 20 66 75 6e 63  rt_rowid()] func
14150 74 69 6f 6e 20 73 68 61 6c 6c 20 72 65 74 75 72  tion shall retur
14160 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
14170 20 20 73 61 6d 65 20 76 61 6c 75 65 20 77 68 65    same value whe
14180 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  n called from th
14190 65 20 73 61 6d 65 20 74 72 69 67 67 65 72 20 63  e same trigger c
141a0 6f 6e 74 65 78 74 0a 2a 2a 20 20 20 20 20 20 20  ontext.**       
141b0 20 20 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 62     immediately b
141c0 65 66 6f 72 65 20 61 6e 64 20 61 66 74 65 72 20  efore and after 
141d0 61 20 5b 52 4f 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a  a [ROLLBACK]..**
141e0 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a  .** ASSUMPTIONS:
141f0 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 33 32 7d 20  .**.** {A12232} 
14200 49 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68  If a separate th
14210 72 65 61 64 20 70 65 72 66 6f 72 6d 73 20 61 20  read performs a 
14220 6e 65 77 20 5b 49 4e 53 45 52 54 5d 20 6f 6e 20  new [INSERT] on 
14230 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20  the same.**     
14240 20 20 20 20 20 64 61 74 61 62 61 73 65 20 63 6f       database co
14250 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nnection while t
14260 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74  he [sqlite3_last
14270 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 5d  _insert_rowid()]
14280 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e  .**          fun
14290 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67  ction is running
142a0 20 61 6e 64 20 74 68 75 73 20 63 68 61 6e 67 65   and thus change
142b0 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72  s the last inser
142c0 74 20 72 6f 77 69 64 2c 0a 2a 2a 20 20 20 20 20  t rowid,.**     
142d0 20 20 20 20 20 74 68 65 6e 20 74 68 65 20 76 61       then the va
142e0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
142f0 5b 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e  [sqlite3_last_in
14300 73 65 72 74 5f 72 6f 77 69 64 28 29 5d 20 69 73  sert_rowid()] is
14310 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e 70  .**          unp
14320 72 65 64 69 63 74 61 62 6c 65 20 61 6e 64 20 6d  redictable and m
14330 69 67 68 74 20 6e 6f 74 20 65 71 75 61 6c 20 65  ight not equal e
14340 69 74 68 65 72 20 74 68 65 20 6f 6c 64 20 6f 72  ither the old or
14350 20 74 68 65 20 6e 65 77 0a 2a 2a 20 20 20 20 20   the new.**     
14360 20 20 20 20 20 6c 61 73 74 20 69 6e 73 65 72 74       last insert
14370 20 72 6f 77 69 64 2e 0a 2a 2f 0a 53 51 4c 49 54   rowid..*/.SQLIT
14380 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
14390 74 36 34 20 73 71 6c 69 74 65 33 5f 6c 61 73 74  t64 sqlite3_last
143a0 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 73 71  _insert_rowid(sq
143b0 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
143c0 43 41 50 49 33 52 45 46 3a 20 43 6f 75 6e 74 20  CAPI3REF: Count 
143d0 54 68 65 20 4e 75 6d 62 65 72 20 4f 66 20 52 6f  The Number Of Ro
143e0 77 73 20 4d 6f 64 69 66 69 65 64 20 7b 48 31 32  ws Modified {H12
143f0 32 34 30 7d 20 3c 53 31 30 36 30 30 3e 0a 2a 2a  240} <S10600>.**
14400 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
14410 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  n returns the nu
14420 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73 65  mber of database
14430 20 72 6f 77 73 20 74 68 61 74 20 77 65 72 65 20   rows that were 
14440 63 68 61 6e 67 65 64 0a 2a 2a 20 6f 72 20 69 6e  changed.** or in
14450 73 65 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65  serted or delete
14460 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72 65  d by the most re
14470 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64  cently completed
14480 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a   SQL statement.*
14490 2a 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61  * on the [databa
144a0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73  se connection] s
144b0 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
144c0 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 2e  first parameter.
144d0 0a 2a 2a 20 4f 6e 6c 79 20 63 68 61 6e 67 65 73  .** Only changes
144e0 20 74 68 61 74 20 61 72 65 20 64 69 72 65 63 74   that are direct
144f0 6c 79 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ly specified by 
14500 74 68 65 20 5b 49 4e 53 45 52 54 5d 2c 20 5b 55  the [INSERT], [U
14510 50 44 41 54 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 44  PDATE],.** or [D
14520 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e 74  ELETE] statement
14530 20 61 72 65 20 63 6f 75 6e 74 65 64 2e 20 20 41   are counted.  A
14540 75 78 69 6c 69 61 72 79 20 63 68 61 6e 67 65 73  uxiliary changes
14550 20 63 61 75 73 65 64 20 62 79 0a 2a 2a 20 74 72   caused by.** tr
14560 69 67 67 65 72 73 20 61 72 65 20 6e 6f 74 20 63  iggers are not c
14570 6f 75 6e 74 65 64 2e 20 55 73 65 20 74 68 65 20  ounted. Use the 
14580 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63  [sqlite3_total_c
14590 68 61 6e 67 65 73 28 29 5d 20 66 75 6e 63 74 69  hanges()] functi
145a0 6f 6e 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68  on.** to find th
145b0 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
145c0 66 20 63 68 61 6e 67 65 73 20 69 6e 63 6c 75 64  f changes includ
145d0 69 6e 67 20 63 68 61 6e 67 65 73 20 63 61 75 73  ing changes caus
145e0 65 64 20 62 79 20 74 72 69 67 67 65 72 73 2e 0a  ed by triggers..
145f0 2a 2a 0a 2a 2a 20 41 20 22 72 6f 77 20 63 68 61  **.** A "row cha
14600 6e 67 65 22 20 69 73 20 61 20 63 68 61 6e 67 65  nge" is a change
14610 20 74 6f 20 61 20 73 69 6e 67 6c 65 20 72 6f 77   to a single row
14620 20 6f 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62   of a single tab
14630 6c 65 0a 2a 2a 20 63 61 75 73 65 64 20 62 79 20  le.** caused by 
14640 61 6e 20 49 4e 53 45 52 54 2c 20 44 45 4c 45 54  an INSERT, DELET
14650 45 2c 20 6f 72 20 55 50 44 41 54 45 20 73 74 61  E, or UPDATE sta
14660 74 65 6d 65 6e 74 2e 20 20 52 6f 77 73 20 74 68  tement.  Rows th
14670 61 74 0a 2a 2a 20 61 72 65 20 63 68 61 6e 67 65  at.** are change
14680 64 20 61 73 20 73 69 64 65 20 65 66 66 65 63 74  d as side effect
14690 73 20 6f 66 20 52 45 50 4c 41 43 45 20 63 6f 6e  s of REPLACE con
146a0 73 74 72 61 69 6e 74 20 72 65 73 6f 6c 75 74 69  straint resoluti
146b0 6f 6e 2c 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 2c  on,.** rollback,
146c0 20 41 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e   ABORT processin
146d0 67 2c 20 44 52 4f 50 20 54 41 42 4c 45 2c 20 6f  g, DROP TABLE, o
146e0 72 20 62 79 20 61 6e 79 20 6f 74 68 65 72 0a 2a  r by any other.*
146f0 2a 20 6d 65 63 68 61 6e 69 73 6d 73 20 64 6f 20  * mechanisms do 
14700 6e 6f 74 20 63 6f 75 6e 74 20 61 73 20 64 69 72  not count as dir
14710 65 63 74 20 72 6f 77 20 63 68 61 6e 67 65 73 2e  ect row changes.
14720 0a 2a 2a 0a 2a 2a 20 41 20 22 74 72 69 67 67 65  .**.** A "trigge
14730 72 20 63 6f 6e 74 65 78 74 22 20 69 73 20 61 20  r context" is a 
14740 73 63 6f 70 65 20 6f 66 20 65 78 65 63 75 74 69  scope of executi
14750 6f 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61  on that begins a
14760 6e 64 0a 2a 2a 20 65 6e 64 73 20 77 69 74 68 20  nd.** ends with 
14770 74 68 65 20 73 63 72 69 70 74 20 6f 66 20 61 20  the script of a 
14780 74 72 69 67 67 65 72 2e 20 20 4d 6f 73 74 20 53  trigger.  Most S
14790 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  QL statements ar
147a0 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6f  e.** evaluated o
147b0 75 74 73 69 64 65 20 6f 66 20 61 6e 79 20 74 72  utside of any tr
147c0 69 67 67 65 72 2e 20 20 54 68 69 73 20 69 73 20  igger.  This is 
147d0 74 68 65 20 22 74 6f 70 20 6c 65 76 65 6c 22 0a  the "top level".
147e0 2a 2a 20 74 72 69 67 67 65 72 20 63 6f 6e 74 65  ** trigger conte
147f0 78 74 2e 20 20 49 66 20 61 20 74 72 69 67 67 65  xt.  If a trigge
14800 72 20 66 69 72 65 73 20 66 72 6f 6d 20 74 68 65  r fires from the
14810 20 74 6f 70 20 6c 65 76 65 6c 2c 20 61 0a 2a 2a   top level, a.**
14820 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e   new trigger con
14830 74 65 78 74 20 69 73 20 65 6e 74 65 72 65 64 20  text is entered 
14840 66 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e  for the duration
14850 20 6f 66 20 74 68 61 74 20 6f 6e 65 0a 2a 2a 20   of that one.** 
14860 74 72 69 67 67 65 72 2e 20 20 53 75 62 74 72 69  trigger.  Subtri
14870 67 67 65 72 73 20 63 72 65 61 74 65 20 73 75 62  ggers create sub
14880 63 6f 6e 74 65 78 74 73 20 66 6f 72 20 74 68 65  contexts for the
14890 69 72 20 64 75 72 61 74 69 6f 6e 2e 0a 2a 2a 0a  ir duration..**.
148a0 2a 2a 20 43 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  ** Calling [sqli
148b0 74 65 33 5f 65 78 65 63 28 29 5d 20 6f 72 20 5b  te3_exec()] or [
148c0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
148d0 72 65 63 75 72 73 69 76 65 6c 79 20 64 6f 65 73  recursively does
148e0 0a 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 61  .** not create a
148f0 20 6e 65 77 20 74 72 69 67 67 65 72 20 63 6f 6e   new trigger con
14900 74 65 78 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  text..**.** This
14910 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
14920 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
14930 64 69 72 65 63 74 20 72 6f 77 20 63 68 61 6e 67  direct row chang
14940 65 73 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 6f 73  es in the.** mos
14950 74 20 72 65 63 65 6e 74 20 49 4e 53 45 52 54 2c  t recent INSERT,
14960 20 55 50 44 41 54 45 2c 20 6f 72 20 44 45 4c 45   UPDATE, or DELE
14970 54 45 20 73 74 61 74 65 6d 65 6e 74 20 77 69 74  TE statement wit
14980 68 69 6e 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20  hin the same.** 
14990 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2e  trigger context.
149a0 0a 2a 2a 0a 2a 2a 20 54 68 75 73 2c 20 77 68 65  .**.** Thus, whe
149b0 6e 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  n called from th
149c0 65 20 74 6f 70 20 6c 65 76 65 6c 2c 20 74 68 69  e top level, thi
149d0 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  s function retur
149e0 6e 73 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72  ns the.** number
149f0 20 6f 66 20 63 68 61 6e 67 65 73 20 69 6e 20 74   of changes in t
14a00 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 49  he most recent I
14a10 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f  NSERT, UPDATE, o
14a20 72 20 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74  r DELETE.** that
14a30 20 61 6c 73 6f 20 6f 63 63 75 72 72 65 64 20 61   also occurred a
14a40 74 20 74 68 65 20 74 6f 70 20 6c 65 76 65 6c 2e  t the top level.
14a50 20 20 57 69 74 68 69 6e 20 74 68 65 20 62 6f 64    Within the bod
14a60 79 20 6f 66 20 61 20 74 72 69 67 67 65 72 2c 0a  y of a trigger,.
14a70 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  ** the sqlite3_c
14a80 68 61 6e 67 65 73 28 29 20 69 6e 74 65 72 66 61  hanges() interfa
14a90 63 65 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ce can be called
14aa0 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 75 6d   to find the num
14ab0 62 65 72 20 6f 66 0a 2a 2a 20 63 68 61 6e 67 65  ber of.** change
14ac0 73 20 69 6e 20 74 68 65 20 6d 6f 73 74 20 72 65  s in the most re
14ad0 63 65 6e 74 6c 79 20 63 6f 6d 70 6c 65 74 65 64  cently completed
14ae0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
14af0 20 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 73 74   or DELETE.** st
14b00 61 74 65 6d 65 6e 74 20 77 69 74 68 69 6e 20 74  atement within t
14b10 68 65 20 62 6f 64 79 20 6f 66 20 74 68 65 20 73  he body of the s
14b20 61 6d 65 20 74 72 69 67 67 65 72 2e 0a 2a 2a 20  ame trigger..** 
14b30 48 6f 77 65 76 65 72 2c 20 74 68 65 20 6e 75 6d  However, the num
14b40 62 65 72 20 72 65 74 75 72 6e 65 64 20 64 6f 65  ber returned doe
14b50 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 63 68  s not include ch
14b60 61 6e 67 65 73 0a 2a 2a 20 63 61 75 73 65 64 20  anges.** caused 
14b70 62 79 20 73 75 62 74 72 69 67 67 65 72 73 20 73  by subtriggers s
14b80 69 6e 63 65 20 74 68 6f 73 65 20 68 61 76 65 20  ince those have 
14b90 74 68 65 69 72 20 6f 77 6e 20 63 6f 6e 74 65 78  their own contex
14ba0 74 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  t..**.** SQLite 
14bb0 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 63  implements the c
14bc0 6f 6d 6d 61 6e 64 20 22 44 45 4c 45 54 45 20 46  ommand "DELETE F
14bd0 52 4f 4d 20 74 61 62 6c 65 22 20 77 69 74 68 6f  ROM table" witho
14be0 75 74 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  ut a WHERE claus
14bf0 65 0a 2a 2a 20 62 79 20 64 72 6f 70 70 69 6e 67  e.** by dropping
14c00 20 61 6e 64 20 72 65 63 72 65 61 74 69 6e 67 20   and recreating 
14c10 74 68 65 20 74 61 62 6c 65 2e 20 20 44 6f 69 6e  the table.  Doin
14c20 67 20 73 6f 20 69 73 20 6d 75 63 68 20 66 61 73  g so is much fas
14c30 74 65 72 20 74 68 61 6e 20 67 6f 69 6e 67 0a 2a  ter than going.*
14c40 2a 20 74 68 72 6f 75 67 68 20 61 6e 64 20 64 65  * through and de
14c50 6c 65 74 69 6e 67 20 69 6e 64 69 76 69 64 75 61  leting individua
14c60 6c 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d 20  l elements from 
14c70 74 68 65 20 74 61 62 6c 65 2e 20 20 42 65 63 61  the table.  Beca
14c80 75 73 65 20 6f 66 20 74 68 69 73 0a 2a 2a 20 6f  use of this.** o
14c90 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74 68 65  ptimization, the
14ca0 20 64 65 6c 65 74 69 6f 6e 73 20 69 6e 20 22 44   deletions in "D
14cb0 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
14cc0 22 20 61 72 65 20 6e 6f 74 20 72 6f 77 20 63 68  " are not row ch
14cd0 61 6e 67 65 73 20 61 6e 64 0a 2a 2a 20 77 69 6c  anges and.** wil
14ce0 6c 20 6e 6f 74 20 62 65 20 63 6f 75 6e 74 65 64  l not be counted
14cf0 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
14d00 63 68 61 6e 67 65 73 28 29 20 6f 72 20 5b 73 71  changes() or [sq
14d10 6c 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e  lite3_total_chan
14d20 67 65 73 28 29 5d 0a 2a 2a 20 66 75 6e 63 74 69  ges()].** functi
14d30 6f 6e 73 2c 20 72 65 67 61 72 64 6c 65 73 73 20  ons, regardless 
14d40 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
14d50 20 65 6c 65 6d 65 6e 74 73 20 74 68 61 74 20 77   elements that w
14d60 65 72 65 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 2a  ere originally.*
14d70 2a 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 20  * in the table. 
14d80 20 54 6f 20 67 65 74 20 61 6e 20 61 63 63 75 72   To get an accur
14d90 61 74 65 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  ate count of the
14da0 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
14db0 64 65 6c 65 74 65 64 2c 20 75 73 65 0a 2a 2a 20  deleted, use.** 
14dc0 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62  "DELETE FROM tab
14dd0 6c 65 20 57 48 45 52 45 20 31 22 20 69 6e 73 74  le WHERE 1" inst
14de0 65 61 64 2e 20 20 4f 72 20 72 65 63 6f 6d 70 69  ead.  Or recompi
14df0 6c 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  le using the.** 
14e00 5b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55  [SQLITE_OMIT_TRU
14e10 4e 43 41 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49  NCATE_OPTIMIZATI
14e20 4f 4e 5d 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65  ON] compile-time
14e30 20 6f 70 74 69 6f 6e 20 74 6f 20 64 69 73 61 62   option to disab
14e40 6c 65 20 74 68 65 0a 2a 2a 20 6f 70 74 69 6d 69  le the.** optimi
14e50 7a 61 74 69 6f 6e 20 6f 6e 20 61 6c 6c 20 71 75  zation on all qu
14e60 65 72 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  eries..**.** INV
14e70 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
14e80 48 31 32 32 34 31 7d 20 54 68 65 20 5b 73 71 6c  H12241} The [sql
14e90 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 5d 20  ite3_changes()] 
14ea0 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 72  function shall r
14eb0 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
14ec0 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   of.**          
14ed0 72 6f 77 20 63 68 61 6e 67 65 73 20 63 61 75 73  row changes caus
14ee0 65 64 20 62 79 20 74 68 65 20 6d 6f 73 74 20 72  ed by the most r
14ef0 65 63 65 6e 74 20 49 4e 53 45 52 54 2c 20 55 50  ecent INSERT, UP
14f00 44 41 54 45 2c 0a 2a 2a 20 20 20 20 20 20 20 20  DATE,.**        
14f10 20 20 6f 72 20 44 45 4c 45 54 45 20 73 74 61 74    or DELETE stat
14f20 65 6d 65 6e 74 20 6f 6e 20 74 68 65 20 73 61 6d  ement on the sam
14f30 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
14f40 63 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20 20  ction and.**    
14f50 20 20 20 20 20 20 77 69 74 68 69 6e 20 74 68 65        within the
14f60 20 73 61 6d 65 20 6f 72 20 68 69 67 68 65 72 20   same or higher 
14f70 74 72 69 67 67 65 72 20 63 6f 6e 74 65 78 74 2c  trigger context,
14f80 20 6f 72 20 7a 65 72 6f 20 69 66 20 74 68 65 72   or zero if ther
14f90 65 20 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20  e have.**       
14fa0 20 20 20 6e 6f 74 20 62 65 65 6e 20 61 6e 79 20     not been any 
14fb0 71 75 61 6c 69 66 79 69 6e 67 20 72 6f 77 20 63  qualifying row c
14fc0 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  hanges..**.** {H
14fd0 31 32 32 34 33 7d 20 53 74 61 74 65 6d 65 6e 74  12243} Statement
14fe0 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 44  s of the form "D
14ff0 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c 65  ELETE FROM table
15000 6e 61 6d 65 22 20 77 69 74 68 20 6e 6f 0a 2a 2a  name" with no.**
15010 20 20 20 20 20 20 20 20 20 20 57 48 45 52 45 20            WHERE 
15020 63 6c 61 75 73 65 20 73 68 61 6c 6c 20 63 61 75  clause shall cau
15030 73 65 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  se subsequent ca
15040 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  lls to.**       
15050 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e     [sqlite3_chan
15060 67 65 73 28 29 5d 20 74 6f 20 72 65 74 75 72 6e  ges()] to return
15070 20 7a 65 72 6f 2c 20 72 65 67 61 72 64 6c 65 73   zero, regardles
15080 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20  s of the.**     
15090 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 72       number of r
150a0 6f 77 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 69  ows originally i
150b0 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a  n the table..**.
150c0 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a  ** ASSUMPTIONS:.
150d0 2a 2a 0a 2a 2a 20 7b 41 31 32 32 35 32 7d 20 49  **.** {A12252} I
150e0 66 20 61 20 73 65 70 61 72 61 74 65 20 74 68 72  f a separate thr
150f0 65 61 64 20 6d 61 6b 65 73 20 63 68 61 6e 67 65  ead makes change
15100 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 64 61  s on the same da
15110 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
15120 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68  n.**          wh
15130 69 6c 65 20 5b 73 71 6c 69 74 65 33 5f 63 68 61  ile [sqlite3_cha
15140 6e 67 65 73 28 29 5d 20 69 73 20 72 75 6e 6e 69  nges()] is runni
15150 6e 67 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  ng then the valu
15160 65 20 72 65 74 75 72 6e 65 64 0a 2a 2a 20 20 20  e returned.**   
15170 20 20 20 20 20 20 20 69 73 20 75 6e 70 72 65 64         is unpred
15180 69 63 74 61 62 6c 65 20 61 6e 64 20 6e 6f 74 20  ictable and not 
15190 6d 65 61 6e 69 6e 67 66 75 6c 2e 0a 2a 2f 0a 53  meaningful..*/.S
151a0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
151b0 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 73 71  lite3_changes(sq
151c0 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
151d0 43 41 50 49 33 52 45 46 3a 20 54 6f 74 61 6c 20  CAPI3REF: Total 
151e0 4e 75 6d 62 65 72 20 4f 66 20 52 6f 77 73 20 4d  Number Of Rows M
151f0 6f 64 69 66 69 65 64 20 7b 48 31 32 32 36 30 7d  odified {H12260}
15200 20 3c 53 31 30 36 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10600>.**.** 
15210 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65  This function re
15220 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
15230 20 6f 66 20 72 6f 77 20 63 68 61 6e 67 65 73 20   of row changes 
15240 63 61 75 73 65 64 20 62 79 20 49 4e 53 45 52 54  caused by INSERT
15250 2c 0a 2a 2a 20 55 50 44 41 54 45 20 6f 72 20 44  ,.** UPDATE or D
15260 45 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 73  ELETE statements
15270 20 73 69 6e 63 65 20 74 68 65 20 5b 64 61 74 61   since the [data
15280 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
15290 20 77 61 73 20 6f 70 65 6e 65 64 2e 0a 2a 2a 20   was opened..** 
152a0 54 68 65 20 63 6f 75 6e 74 20 69 6e 63 6c 75 64  The count includ
152b0 65 73 20 61 6c 6c 20 63 68 61 6e 67 65 73 20 66  es all changes f
152c0 72 6f 6d 20 61 6c 6c 20 74 72 69 67 67 65 72 20  rom all trigger 
152d0 63 6f 6e 74 65 78 74 73 2e 20 20 48 6f 77 65 76  contexts.  Howev
152e0 65 72 2c 0a 2a 2a 20 74 68 65 20 63 6f 75 6e 74  er,.** the count
152f0 20 64 6f 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64   does not includ
15300 65 20 63 68 61 6e 67 65 73 20 75 73 65 64 20 74  e changes used t
15310 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 52 45 50 4c  o implement REPL
15320 41 43 45 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c  ACE constraints,
15330 0a 2a 2a 20 64 6f 20 72 6f 6c 6c 62 61 63 6b 73  .** do rollbacks
15340 20 6f 72 20 41 42 4f 52 54 20 70 72 6f 63 65 73   or ABORT proces
15350 73 69 6e 67 2c 20 6f 72 20 44 52 4f 50 20 74 61  sing, or DROP ta
15360 62 6c 65 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a  ble processing..
15370 2a 2a 20 54 68 65 20 63 68 61 6e 67 65 73 20 61  ** The changes a
15380 72 65 20 63 6f 75 6e 74 65 64 20 61 73 20 73 6f  re counted as so
15390 6f 6e 20 61 73 20 74 68 65 20 73 74 61 74 65 6d  on as the statem
153a0 65 6e 74 20 74 68 61 74 20 6d 61 6b 65 73 20 74  ent that makes t
153b0 68 65 6d 20 69 73 0a 2a 2a 20 63 6f 6d 70 6c 65  hem is.** comple
153c0 74 65 64 20 28 77 68 65 6e 20 74 68 65 20 73 74  ted (when the st
153d0 61 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69  atement handle i
153e0 73 20 70 61 73 73 65 64 20 74 6f 20 5b 73 71 6c  s passed to [sql
153f0 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72  ite3_reset()] or
15400 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e  .** [sqlite3_fin
15410 61 6c 69 7a 65 28 29 5d 29 2e 0a 2a 2a 0a 2a 2a  alize()])..**.**
15420 20 53 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e   SQLite implemen
15430 74 73 20 74 68 65 20 63 6f 6d 6d 61 6e 64 20 22  ts the command "
15440 44 45 4c 45 54 45 20 46 52 4f 4d 20 74 61 62 6c  DELETE FROM tabl
15450 65 22 20 77 69 74 68 6f 75 74 20 61 20 57 48 45  e" without a WHE
15460 52 45 20 63 6c 61 75 73 65 0a 2a 2a 20 62 79 20  RE clause.** by 
15470 64 72 6f 70 70 69 6e 67 20 61 6e 64 20 72 65 63  dropping and rec
15480 72 65 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c  reating the tabl
15490 65 2e 20 20 28 54 68 69 73 20 69 73 20 6d 75 63  e.  (This is muc
154a0 68 20 66 61 73 74 65 72 20 74 68 61 6e 20 67 6f  h faster than go
154b0 69 6e 67 0a 2a 2a 20 74 68 72 6f 75 67 68 20 61  ing.** through a
154c0 6e 64 20 64 65 6c 65 74 69 6e 67 20 69 6e 64 69  nd deleting indi
154d0 76 69 64 75 61 6c 20 65 6c 65 6d 65 6e 74 73 20  vidual elements 
154e0 66 72 6f 6d 20 74 68 65 20 74 61 62 6c 65 2e 29  from the table.)
154f0 20 20 42 65 63 61 75 73 65 20 6f 66 20 74 68 69    Because of thi
15500 73 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s.** optimizatio
15510 6e 2c 20 74 68 65 20 64 65 6c 65 74 69 6f 6e 73  n, the deletions
15520 20 69 6e 20 22 44 45 4c 45 54 45 20 46 52 4f 4d   in "DELETE FROM
15530 20 74 61 62 6c 65 22 20 61 72 65 20 6e 6f 74 20   table" are not 
15540 72 6f 77 20 63 68 61 6e 67 65 73 20 61 6e 64 0a  row changes and.
15550 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 63  ** will not be c
15560 6f 75 6e 74 65 64 20 62 79 20 74 68 65 20 73 71  ounted by the sq
15570 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28 29 20  lite3_changes() 
15580 6f 72 20 5b 73 71 6c 69 74 65 33 5f 74 6f 74 61  or [sqlite3_tota
15590 6c 5f 63 68 61 6e 67 65 73 28 29 5d 0a 2a 2a 20  l_changes()].** 
155a0 66 75 6e 63 74 69 6f 6e 73 2c 20 72 65 67 61 72  functions, regar
155b0 64 6c 65 73 73 20 6f 66 20 74 68 65 20 6e 75 6d  dless of the num
155c0 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20  ber of elements 
155d0 74 68 61 74 20 77 65 72 65 20 6f 72 69 67 69 6e  that were origin
155e0 61 6c 6c 79 0a 2a 2a 20 69 6e 20 74 68 65 20 74  ally.** in the t
155f0 61 62 6c 65 2e 20 20 54 6f 20 67 65 74 20 61 6e  able.  To get an
15600 20 61 63 63 75 72 61 74 65 20 63 6f 75 6e 74 20   accurate count 
15610 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  of the number of
15620 20 72 6f 77 73 20 64 65 6c 65 74 65 64 2c 20 75   rows deleted, u
15630 73 65 0a 2a 2a 20 22 44 45 4c 45 54 45 20 46 52  se.** "DELETE FR
15640 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20 31  OM table WHERE 1
15650 22 20 69 6e 73 74 65 61 64 2e 20 20 20 4f 72 20  " instead.   Or 
15660 72 65 63 6f 6d 70 69 6c 65 20 75 73 69 6e 67 20  recompile using 
15670 74 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f  the.** [SQLITE_O
15680 4d 49 54 5f 54 52 55 4e 43 41 54 45 5f 4f 50 54  MIT_TRUNCATE_OPT
15690 49 4d 49 5a 41 54 49 4f 4e 5d 20 63 6f 6d 70 69  IMIZATION] compi
156a0 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e 20 74  le-time option t
156b0 6f 20 64 69 73 61 62 6c 65 20 74 68 65 0a 2a 2a  o disable the.**
156c0 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6f 6e   optimization on
156d0 20 61 6c 6c 20 71 75 65 72 69 65 73 2e 0a 2a 2a   all queries..**
156e0 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 74 68 65  .** See also the
156f0 20 5b 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65   [sqlite3_change
15700 73 28 29 5d 20 69 6e 74 65 72 66 61 63 65 2e 0a  s()] interface..
15710 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
15720 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36 31 7d  :.**.** {H12261}
15730 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 74 6f   The [sqlite3_to
15740 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 20 72  tal_changes()] r
15750 65 74 75 72 6e 73 20 74 68 65 20 74 6f 74 61 6c  eturns the total
15760 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20   number.**      
15770 20 20 20 20 6f 66 20 72 6f 77 20 63 68 61 6e 67      of row chang
15780 65 73 20 63 61 75 73 65 64 20 62 79 20 49 4e 53  es caused by INS
15790 45 52 54 2c 20 55 50 44 41 54 45 2c 20 61 6e 64  ERT, UPDATE, and
157a0 2f 6f 72 20 44 45 4c 45 54 45 0a 2a 2a 20 20 20  /or DELETE.**   
157b0 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
157c0 73 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 5b 64  s on the same [d
157d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
157e0 6f 6e 5d 2c 20 69 6e 20 61 6e 79 0a 2a 2a 20 20  on], in any.**  
157f0 20 20 20 20 20 20 20 20 74 72 69 67 67 65 72 20          trigger 
15800 63 6f 6e 74 65 78 74 2c 20 73 69 6e 63 65 20 74  context, since t
15810 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
15820 65 63 74 69 6f 6e 20 77 61 73 20 63 72 65 61 74  ection was creat
15830 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 36  ed..**.** {H1226
15840 33 7d 20 53 74 61 74 65 6d 65 6e 74 73 20 6f 66  3} Statements of
15850 20 74 68 65 20 66 6f 72 6d 20 22 44 45 4c 45 54   the form "DELET
15860 45 20 46 52 4f 4d 20 74 61 62 6c 65 6e 61 6d 65  E FROM tablename
15870 22 20 77 69 74 68 20 6e 6f 0a 2a 2a 20 20 20 20  " with no.**    
15880 20 20 20 20 20 20 57 48 45 52 45 20 63 6c 61 75        WHERE clau
15890 73 65 20 73 68 61 6c 6c 20 6e 6f 74 20 63 68 61  se shall not cha
158a0 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 72 65  nge the value re
158b0 74 75 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20  turned.**       
158c0 20 20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74     by [sqlite3_t
158d0 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29 5d 2e  otal_changes()].
158e0 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f  .**.** ASSUMPTIO
158f0 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 32 36  NS:.**.** {A1226
15900 34 7d 20 49 66 20 61 20 73 65 70 61 72 61 74 65  4} If a separate
15910 20 74 68 72 65 61 64 20 6d 61 6b 65 73 20 63 68   thread makes ch
15920 61 6e 67 65 73 20 6f 6e 20 74 68 65 20 73 61 6d  anges on the sam
15930 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
15940 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ction.**        
15950 20 20 77 68 69 6c 65 20 5b 73 71 6c 69 74 65 33    while [sqlite3
15960 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 29  _total_changes()
15970 5d 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65  ] is running the
15980 6e 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 20  n the value.**  
15990 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 65 64          returned
159a0 20 69 73 20 75 6e 70 72 65 64 69 63 74 61 62 6c   is unpredictabl
159b0 65 20 61 6e 64 20 6e 6f 74 20 6d 65 61 6e 69 6e  e and not meanin
159c0 67 66 75 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  gful..*/.SQLITE_
159d0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
159e0 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28 73 71  total_changes(sq
159f0 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20  lite3*);../*.** 
15a00 43 41 50 49 33 52 45 46 3a 20 49 6e 74 65 72 72  CAPI3REF: Interr
15a10 75 70 74 20 41 20 4c 6f 6e 67 2d 52 75 6e 6e 69  upt A Long-Runni
15a20 6e 67 20 51 75 65 72 79 20 7b 48 31 32 32 37 30  ng Query {H12270
15a30 7d 20 3c 53 33 30 35 30 30 3e 0a 2a 2a 0a 2a 2a  } <S30500>.**.**
15a40 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
15a50 61 75 73 65 73 20 61 6e 79 20 70 65 6e 64 69 6e  auses any pendin
15a60 67 20 64 61 74 61 62 61 73 65 20 6f 70 65 72 61  g database opera
15a70 74 69 6f 6e 20 74 6f 20 61 62 6f 72 74 20 61 6e  tion to abort an
15a80 64 0a 2a 2a 20 72 65 74 75 72 6e 20 61 74 20 69  d.** return at i
15a90 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70 6f  ts earliest oppo
15aa0 72 74 75 6e 69 74 79 2e 20 54 68 69 73 20 72 6f  rtunity. This ro
15ab0 75 74 69 6e 65 20 69 73 20 74 79 70 69 63 61 6c  utine is typical
15ac0 6c 79 0a 2a 2a 20 63 61 6c 6c 65 64 20 69 6e 20  ly.** called in 
15ad0 72 65 73 70 6f 6e 73 65 20 74 6f 20 61 20 75 73  response to a us
15ae0 65 72 20 61 63 74 69 6f 6e 20 73 75 63 68 20 61  er action such a
15af0 73 20 70 72 65 73 73 69 6e 67 20 22 43 61 6e 63  s pressing "Canc
15b00 65 6c 22 0a 2a 2a 20 6f 72 20 43 74 72 6c 2d 43  el".** or Ctrl-C
15b10 20 77 68 65 72 65 20 74 68 65 20 75 73 65 72 20   where the user 
15b20 77 61 6e 74 73 20 61 20 6c 6f 6e 67 20 71 75 65  wants a long que
15b30 72 79 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  ry operation to 
15b40 68 61 6c 74 0a 2a 2a 20 69 6d 6d 65 64 69 61 74  halt.** immediat
15b50 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  ely..**.** It is
15b60 20 73 61 66 65 20 74 6f 20 63 61 6c 6c 20 74 68   safe to call th
15b70 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d 20  is routine from 
15b80 61 20 74 68 72 65 61 64 20 64 69 66 66 65 72 65  a thread differe
15b90 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 74  nt from the.** t
15ba0 68 72 65 61 64 20 74 68 61 74 20 69 73 20 63 75  hread that is cu
15bb0 72 72 65 6e 74 6c 79 20 72 75 6e 6e 69 6e 67 20  rrently running 
15bc0 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 70 65  the database ope
15bd0 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 74 0a  ration.  But it.
15be0 2a 2a 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74  ** is not safe t
15bf0 6f 20 63 61 6c 6c 20 74 68 69 73 20 72 6f 75 74  o call this rout
15c00 69 6e 65 20 77 69 74 68 20 61 20 5b 64 61 74 61  ine with a [data
15c10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
15c20 20 74 68 61 74 0a 2a 2a 20 69 73 20 63 6c 6f 73   that.** is clos
15c30 65 64 20 6f 72 20 6d 69 67 68 74 20 63 6c 6f 73  ed or might clos
15c40 65 20 62 65 66 6f 72 65 20 73 71 6c 69 74 65 33  e before sqlite3
15c50 5f 69 6e 74 65 72 72 75 70 74 28 29 20 72 65 74  _interrupt() ret
15c60 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  urns..**.** If a
15c70 6e 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20  n SQL operation 
15c80 69 73 20 76 65 72 79 20 6e 65 61 72 6c 79 20 66  is very nearly f
15c90 69 6e 69 73 68 65 64 20 61 74 20 74 68 65 20 74  inished at the t
15ca0 69 6d 65 20 77 68 65 6e 0a 2a 2a 20 73 71 6c 69  ime when.** sqli
15cb0 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
15cc0 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 6e 20  is called, then 
15cd0 69 74 20 6d 69 67 68 74 20 6e 6f 74 20 68 61 76  it might not hav
15ce0 65 20 61 6e 20 6f 70 70 6f 72 74 75 6e 69 74 79  e an opportunity
15cf0 0a 2a 2a 20 74 6f 20 62 65 20 69 6e 74 65 72 72  .** to be interr
15d00 75 70 74 65 64 20 61 6e 64 20 6d 69 67 68 74 20  upted and might 
15d10 63 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6d 70  continue to comp
15d20 6c 65 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e  letion..**.** An
15d30 20 53 51 4c 20 6f 70 65 72 61 74 69 6f 6e 20 74   SQL operation t
15d40 68 61 74 20 69 73 20 69 6e 74 65 72 72 75 70 74  hat is interrupt
15d50 65 64 20 77 69 6c 6c 20 72 65 74 75 72 6e 20 5b  ed will return [
15d60 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54  SQLITE_INTERRUPT
15d70 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 74  ]..** If the int
15d80 65 72 72 75 70 74 65 64 20 53 51 4c 20 6f 70 65  errupted SQL ope
15d90 72 61 74 69 6f 6e 20 69 73 20 61 6e 20 49 4e 53  ration is an INS
15da0 45 52 54 2c 20 55 50 44 41 54 45 2c 20 6f 72 20  ERT, UPDATE, or 
15db0 44 45 4c 45 54 45 0a 2a 2a 20 74 68 61 74 20 69  DELETE.** that i
15dc0 73 20 69 6e 73 69 64 65 20 61 6e 20 65 78 70 6c  s inside an expl
15dd0 69 63 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e  icit transaction
15de0 2c 20 74 68 65 6e 20 74 68 65 20 65 6e 74 69 72  , then the entir
15df0 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a  e transaction.**
15e00 20 77 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20   will be rolled 
15e10 62 61 63 6b 20 61 75 74 6f 6d 61 74 69 63 61 6c  back automatical
15e20 6c 79 2e 0a 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c  ly..**.** A call
15e30 20 74 6f 20 73 71 6c 69 74 65 33 5f 69 6e 74 65   to sqlite3_inte
15e40 72 72 75 70 74 28 29 20 68 61 73 20 6e 6f 20 65  rrupt() has no e
15e50 66 66 65 63 74 20 6f 6e 20 53 51 4c 20 73 74 61  ffect on SQL sta
15e60 74 65 6d 65 6e 74 73 0a 2a 2a 20 74 68 61 74 20  tements.** that 
15e70 61 72 65 20 73 74 61 72 74 65 64 20 61 66 74 65  are started afte
15e80 72 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72  r sqlite3_interr
15e90 75 70 74 28 29 20 72 65 74 75 72 6e 73 2e 0a 2a  upt() returns..*
15ea0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
15eb0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 37 31 7d 20  .**.** {H12271} 
15ec0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74  The [sqlite3_int
15ed0 65 72 72 75 70 74 28 29 5d 20 69 6e 74 65 72 66  errupt()] interf
15ee0 61 63 65 20 77 69 6c 6c 20 66 6f 72 63 65 20 61  ace will force a
15ef0 6c 6c 20 72 75 6e 6e 69 6e 67 0a 2a 2a 20 20 20  ll running.**   
15f00 20 20 20 20 20 20 20 53 51 4c 20 73 74 61 74 65         SQL state
15f10 6d 65 6e 74 73 20 61 73 73 6f 63 69 61 74 65 64  ments associated
15f20 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 64   with the same d
15f30 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
15f40 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  on.**          t
15f50 6f 20 68 61 6c 74 20 61 66 74 65 72 20 70 72 6f  o halt after pro
15f60 63 65 73 73 69 6e 67 20 61 74 20 6d 6f 73 74 20  cessing at most 
15f70 6f 6e 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 72  one additional r
15f80 6f 77 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a  ow of data..**.*
15f90 2a 20 7b 48 31 32 32 37 32 7d 20 41 6e 79 20 53  * {H12272} Any S
15fa0 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61  QL statement tha
15fb0 74 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64  t is interrupted
15fc0 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74   by [sqlite3_int
15fd0 65 72 72 75 70 74 28 29 5d 0a 2a 2a 20 20 20 20  errupt()].**    
15fe0 20 20 20 20 20 20 77 69 6c 6c 20 72 65 74 75 72        will retur
15ff0 6e 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 52 52  n [SQLITE_INTERR
16000 55 50 54 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55  UPT]..**.** ASSU
16010 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b  MPTIONS:.**.** {
16020 41 31 32 32 37 39 7d 20 49 66 20 74 68 65 20 64  A12279} If the d
16030 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
16040 6f 6e 20 63 6c 6f 73 65 73 20 77 68 69 6c 65 20  on closes while 
16050 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75  [sqlite3_interru
16060 70 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  pt()].**        
16070 20 20 69 73 20 72 75 6e 6e 69 6e 67 20 74 68 65    is running the
16080 6e 20 62 61 64 20 74 68 69 6e 67 73 20 77 69 6c  n bad things wil
16090 6c 20 6c 69 6b 65 6c 79 20 68 61 70 70 65 6e 2e  l likely happen.
160a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
160b0 6f 69 64 20 73 71 6c 69 74 65 33 5f 69 6e 74 65  oid sqlite3_inte
160c0 72 72 75 70 74 28 73 71 6c 69 74 65 33 2a 29 3b  rrupt(sqlite3*);
160d0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
160e0 3a 20 44 65 74 65 72 6d 69 6e 65 20 49 66 20 41  : Determine If A
160f0 6e 20 53 51 4c 20 53 74 61 74 65 6d 65 6e 74 20  n SQL Statement 
16100 49 73 20 43 6f 6d 70 6c 65 74 65 20 7b 48 31 30  Is Complete {H10
16110 35 31 30 7d 20 3c 53 37 30 32 30 30 3e 0a 2a 2a  510} <S70200>.**
16120 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
16130 65 73 20 61 72 65 20 75 73 65 66 75 6c 20 66 6f  es are useful fo
16140 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 20 69  r command-line i
16150 6e 70 75 74 20 74 6f 20 64 65 74 65 72 6d 69 6e  nput to determin
16160 65 20 69 66 20 74 68 65 0a 2a 2a 20 63 75 72 72  e if the.** curr
16170 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20 74 65  ently entered te
16180 78 74 20 73 65 65 6d 73 20 74 6f 20 66 6f 72 6d  xt seems to form
16190 20 63 6f 6d 70 6c 65 74 65 20 61 20 53 51 4c 20   complete a SQL 
161a0 73 74 61 74 65 6d 65 6e 74 20 6f 72 0a 2a 2a 20  statement or.** 
161b0 69 66 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  if additional in
161c0 70 75 74 20 69 73 20 6e 65 65 64 65 64 20 62 65  put is needed be
161d0 66 6f 72 65 20 73 65 6e 64 69 6e 67 20 74 68 65  fore sending the
161e0 20 74 65 78 74 20 69 6e 74 6f 0a 2a 2a 20 53 51   text into.** SQ
161f0 4c 69 74 65 20 66 6f 72 20 70 61 72 73 69 6e 67  Lite for parsing
16200 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  .  These routine
16210 73 20 72 65 74 75 72 6e 20 74 72 75 65 20 69 66  s return true if
16220 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e   the input strin
16230 67 0a 2a 2a 20 61 70 70 65 61 72 73 20 74 6f 20  g.** appears to 
16240 62 65 20 61 20 63 6f 6d 70 6c 65 74 65 20 53 51  be a complete SQ
16250 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 41 20  L statement.  A 
16260 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6a 75 64  statement is jud
16270 67 65 64 20 74 6f 20 62 65 0a 2a 2a 20 63 6f 6d  ged to be.** com
16280 70 6c 65 74 65 20 69 66 20 69 74 20 65 6e 64 73  plete if it ends
16290 20 77 69 74 68 20 61 20 73 65 6d 69 63 6f 6c 6f   with a semicolo
162a0 6e 20 74 6f 6b 65 6e 20 61 6e 64 20 69 73 20 6e  n token and is n
162b0 6f 74 20 61 20 66 72 61 67 6d 65 6e 74 20 6f 66  ot a fragment of
162c0 20 61 0a 2a 2a 20 43 52 45 41 54 45 20 54 52 49   a.** CREATE TRI
162d0 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e 20  GGER statement. 
162e0 20 53 65 6d 69 63 6f 6c 6f 6e 73 20 74 68 61 74   Semicolons that
162f0 20 61 72 65 20 65 6d 62 65 64 64 65 64 20 77 69   are embedded wi
16300 74 68 69 6e 0a 2a 2a 20 73 74 72 69 6e 67 20 6c  thin.** string l
16310 69 74 65 72 61 6c 73 20 6f 72 20 71 75 6f 74 65  iterals or quote
16320 64 20 69 64 65 6e 74 69 66 69 65 72 20 6e 61 6d  d identifier nam
16330 65 73 20 6f 72 20 63 6f 6d 6d 65 6e 74 73 20 61  es or comments a
16340 72 65 20 6e 6f 74 0a 2a 2a 20 69 6e 64 65 70 65  re not.** indepe
16350 6e 64 65 6e 74 20 74 6f 6b 65 6e 73 20 28 74 68  ndent tokens (th
16360 65 79 20 61 72 65 20 70 61 72 74 20 6f 66 20 74  ey are part of t
16370 68 65 20 74 6f 6b 65 6e 20 69 6e 20 77 68 69 63  he token in whic
16380 68 20 74 68 65 79 20 61 72 65 0a 2a 2a 20 65 6d  h they are.** em
16390 62 65 64 64 65 64 29 20 61 6e 64 20 74 68 75 73  bedded) and thus
163a0 20 64 6f 20 6e 6f 74 20 63 6f 75 6e 74 20 61 73   do not count as
163b0 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 65 72   a statement ter
163c0 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54  minator..**.** T
163d0 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 64 6f  hese routines do
163e0 20 6e 6f 74 20 70 61 72 73 65 20 74 68 65 20 53   not parse the S
163f0 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68  QL statements th
16400 75 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 64  us.** will not d
16410 65 74 65 63 74 20 73 79 6e 74 61 63 74 69 63 61  etect syntactica
16420 6c 6c 79 20 69 6e 63 6f 72 72 65 63 74 20 53 51  lly incorrect SQ
16430 4c 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  L..**.** INVARIA
16440 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35  NTS:.**.** {H105
16450 31 31 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  11} A successful
16460 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 5b   evaluation of [
16470 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65  sqlite3_complete
16480 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  ()] or.**       
16490 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70     [sqlite3_comp
164a0 6c 65 74 65 31 36 28 29 5d 20 66 75 6e 63 74 69  lete16()] functi
164b0 6f 6e 73 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20  ons shall.**    
164c0 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 20 6e        return a n
164d0 75 6d 65 72 69 63 20 31 20 69 66 20 61 6e 64 20  umeric 1 if and 
164e0 6f 6e 6c 79 20 69 66 20 74 68 65 20 6c 61 73 74  only if the last
164f0 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61 63 65 0a   non-whitespace.
16500 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65  **          toke
16510 6e 20 69 6e 20 74 68 65 69 72 20 69 6e 70 75 74  n in their input
16520 20 69 73 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 20   is a semicolon 
16530 74 68 61 74 20 69 73 20 6e 6f 74 20 69 6e 20 62  that is not in b
16540 65 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 20 20  etween.**       
16550 20 20 20 74 68 65 20 42 45 47 49 4e 20 61 6e 64     the BEGIN and
16560 20 45 4e 44 20 6f 66 20 61 20 43 52 45 41 54 45   END of a CREATE
16570 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
16580 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 30 35 31  nt..**.** {H1051
16590 32 7d 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  2} If a memory a
165a0 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20  llocation error 
165b0 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61 6e  occurs during an
165c0 20 69 6e 76 6f 63 61 74 69 6f 6e 0a 2a 2a 20 20   invocation.**  
165d0 20 20 20 20 20 20 20 20 6f 66 20 5b 73 71 6c 69          of [sqli
165e0 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28 29 5d 20  te3_complete()] 
165f0 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70  or [sqlite3_comp
16600 6c 65 74 65 31 36 28 29 5d 20 74 68 65 6e 20 74  lete16()] then t
16610 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  he.**          r
16620 6f 75 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74  outine shall ret
16630 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45  urn [SQLITE_NOME
16640 4d 5d 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50  M]..**.** ASSUMP
16650 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31  TIONS:.**.** {A1
16660 30 35 31 32 7d 20 54 68 65 20 69 6e 70 75 74 20  0512} The input 
16670 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 70  to [sqlite3_comp
16680 6c 65 74 65 28 29 5d 20 6d 75 73 74 20 62 65 20  lete()] must be 
16690 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  a zero-terminate
166a0 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 55 54  d.**          UT
166b0 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  F-8 string..**.*
166c0 2a 20 7b 41 31 30 35 31 33 7d 20 54 68 65 20 69  * {A10513} The i
166d0 6e 70 75 74 20 74 6f 20 5b 73 71 6c 69 74 65 33  nput to [sqlite3
166e0 5f 63 6f 6d 70 6c 65 74 65 31 36 28 29 5d 20 6d  _complete16()] m
166f0 75 73 74 20 62 65 20 61 20 7a 65 72 6f 2d 74 65  ust be a zero-te
16700 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20  rminated.**     
16710 20 20 20 20 20 55 54 46 2d 31 36 20 73 74 72 69       UTF-16 stri
16720 6e 67 20 69 6e 20 6e 61 74 69 76 65 20 62 79 74  ng in native byt
16730 65 20 6f 72 64 65 72 2e 0a 2a 2f 0a 53 51 4c 49  e order..*/.SQLI
16740 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
16750 65 33 5f 63 6f 6d 70 6c 65 74 65 28 63 6f 6e 73  e3_complete(cons
16760 74 20 63 68 61 72 20 2a 73 71 6c 29 3b 0a 53 51  t char *sql);.SQ
16770 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
16780 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 28  ite3_complete16(
16790 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 29  const void *sql)
167a0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
167b0 46 3a 20 52 65 67 69 73 74 65 72 20 41 20 43 61  F: Register A Ca
167c0 6c 6c 62 61 63 6b 20 54 6f 20 48 61 6e 64 6c 65  llback To Handle
167d0 20 53 51 4c 49 54 45 5f 42 55 53 59 20 45 72 72   SQLITE_BUSY Err
167e0 6f 72 73 20 7b 48 31 32 33 31 30 7d 20 3c 53 34  ors {H12310} <S4
167f0 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  0400>.**.** This
16800 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 61 20   routine sets a 
16810 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
16820 6e 20 74 68 61 74 20 6d 69 67 68 74 20 62 65 20  n that might be 
16830 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65 72  invoked whenever
16840 0a 2a 2a 20 61 6e 20 61 74 74 65 6d 70 74 20 69  .** an attempt i
16850 73 20 6d 61 64 65 20 74 6f 20 6f 70 65 6e 20 61  s made to open a
16860 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
16870 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68 72  that another thr
16880 65 61 64 0a 2a 2a 20 6f 72 20 70 72 6f 63 65 73  ead.** or proces
16890 73 20 68 61 73 20 6c 6f 63 6b 65 64 2e 0a 2a 2a  s has locked..**
168a0 0a 2a 2a 20 49 66 20 74 68 65 20 62 75 73 79 20  .** If the busy 
168b0 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e 55 4c 4c  callback is NULL
168c0 2c 20 74 68 65 6e 20 5b 53 51 4c 49 54 45 5f 42  , then [SQLITE_B
168d0 55 53 59 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  USY] or [SQLITE_
168e0 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a 2a  IOERR_BLOCKED].*
168f0 2a 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d  * is returned im
16900 6d 65 64 69 61 74 65 6c 79 20 75 70 6f 6e 20 65  mediately upon e
16910 6e 63 6f 75 6e 74 65 72 69 6e 67 20 74 68 65 20  ncountering the 
16920 6c 6f 63 6b 2e 20 49 66 20 74 68 65 20 62 75 73  lock. If the bus
16930 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  y callback.** is
16940 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
16950 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 77 69 6c  the callback wil
16960 6c 20 62 65 20 69 6e 76 6f 6b 65 64 20 77 69 74  l be invoked wit
16970 68 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e  h two arguments.
16980 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
16990 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
169a0 20 68 61 6e 64 6c 65 72 20 69 73 20 61 20 63 6f   handler is a co
169b0 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64 2a 20  py of the void* 
169c0 70 6f 69 6e 74 65 72 20 77 68 69 63 68 0a 2a 2a  pointer which.**
169d0 20 69 73 20 74 68 65 20 74 68 69 72 64 20 61 72   is the third ar
169e0 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65  gument to sqlite
169f0 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29  3_busy_handler()
16a00 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72  .  The second ar
16a10 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 74 68 65  gument to.** the
16a20 20 68 61 6e 64 6c 65 72 20 63 61 6c 6c 62 61 63   handler callbac
16a30 6b 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  k is the number 
16a40 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 74 68  of times that th
16a50 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 68  e busy handler h
16a60 61 73 0a 2a 2a 20 62 65 65 6e 20 69 6e 76 6f 6b  as.** been invok
16a70 65 64 20 66 6f 72 20 74 68 69 73 20 6c 6f 63 6b  ed for this lock
16a80 69 6e 67 20 65 76 65 6e 74 2e 20 20 49 66 20 74  ing event.  If t
16a90 68 65 0a 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62  he.** busy callb
16aa0 61 63 6b 20 72 65 74 75 72 6e 73 20 30 2c 20 74  ack returns 0, t
16ab0 68 65 6e 20 6e 6f 20 61 64 64 69 74 69 6f 6e 61  hen no additiona
16ac0 6c 20 61 74 74 65 6d 70 74 73 20 61 72 65 20 6d  l attempts are m
16ad0 61 64 65 20 74 6f 0a 2a 2a 20 61 63 63 65 73 73  ade to.** access
16ae0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61 6e   the database an
16af0 64 20 5b 53 51 4c 49 54 45 5f 42 55 53 59 5d 20  d [SQLITE_BUSY] 
16b00 6f 72 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52 52  or [SQLITE_IOERR
16b10 5f 42 4c 4f 43 4b 45 44 5d 20 69 73 20 72 65 74  _BLOCKED] is ret
16b20 75 72 6e 65 64 2e 0a 2a 2a 20 49 66 20 74 68 65  urned..** If the
16b30 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
16b40 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
16b50 20 61 6e 6f 74 68 65 72 20 61 74 74 65 6d 70 74   another attempt
16b60 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 6f  .** is made to o
16b70 70 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  pen the database
16b80 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e 64   for reading and
16b90 20 74 68 65 20 63 79 63 6c 65 20 72 65 70 65 61   the cycle repea
16ba0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 72  ts..**.** The pr
16bb0 65 73 65 6e 63 65 20 6f 66 20 61 20 62 75 73 79  esence of a busy
16bc0 20 68 61 6e 64 6c 65 72 20 64 6f 65 73 20 6e 6f   handler does no
16bd0 74 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74  t guarantee that
16be0 20 69 74 20 77 69 6c 6c 20 62 65 20 69 6e 76 6f   it will be invo
16bf0 6b 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68 65 72  ked.** when ther
16c00 65 20 69 73 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  e is lock conten
16c10 74 69 6f 6e 2e 20 49 66 20 53 51 4c 69 74 65 20  tion. If SQLite 
16c20 64 65 74 65 72 6d 69 6e 65 73 20 74 68 61 74 20  determines that 
16c30 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62 75 73  invoking the bus
16c40 79 0a 2a 2a 20 68 61 6e 64 6c 65 72 20 63 6f 75  y.** handler cou
16c50 6c 64 20 72 65 73 75 6c 74 20 69 6e 20 61 20 64  ld result in a d
16c60 65 61 64 6c 6f 63 6b 2c 20 69 74 20 77 69 6c 6c  eadlock, it will
16c70 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 72 65   go ahead and re
16c80 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42 55 53  turn [SQLITE_BUS
16c90 59 5d 0a 2a 2a 20 6f 72 20 5b 53 51 4c 49 54 45  Y].** or [SQLITE
16ca0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 20  _IOERR_BLOCKED] 
16cb0 69 6e 73 74 65 61 64 20 6f 66 20 69 6e 76 6f 6b  instead of invok
16cc0 69 6e 67 20 74 68 65 20 62 75 73 79 20 68 61 6e  ing the busy han
16cd0 64 6c 65 72 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65  dler..** Conside
16ce0 72 20 61 20 73 63 65 6e 61 72 69 6f 20 77 68 65  r a scenario whe
16cf0 72 65 20 6f 6e 65 20 70 72 6f 63 65 73 73 20 69  re one process i
16d00 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65 61 64  s holding a read
16d10 20 6c 6f 63 6b 20 74 68 61 74 0a 2a 2a 20 69 74   lock that.** it
16d20 20 69 73 20 74 72 79 69 6e 67 20 74 6f 20 70 72   is trying to pr
16d30 6f 6d 6f 74 65 20 74 6f 20 61 20 72 65 73 65 72  omote to a reser
16d40 76 65 64 20 6c 6f 63 6b 20 61 6e 64 0a 2a 2a 20  ved lock and.** 
16d50 61 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73 73  a second process
16d60 20 69 73 20 68 6f 6c 64 69 6e 67 20 61 20 72 65   is holding a re
16d70 73 65 72 76 65 64 20 6c 6f 63 6b 20 74 68 61 74  served lock that
16d80 20 69 74 20 69 73 20 74 72 79 69 6e 67 0a 2a 2a   it is trying.**
16d90 20 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 61   to promote to a
16da0 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b  n exclusive lock
16db0 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 72 6f  .  The first pro
16dc0 63 65 73 73 20 63 61 6e 6e 6f 74 20 70 72 6f 63  cess cannot proc
16dd0 65 65 64 0a 2a 2a 20 62 65 63 61 75 73 65 20 69  eed.** because i
16de0 74 20 69 73 20 62 6c 6f 63 6b 65 64 20 62 79 20  t is blocked by 
16df0 74 68 65 20 73 65 63 6f 6e 64 20 61 6e 64 20 74  the second and t
16e00 68 65 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73  he second proces
16e10 73 20 63 61 6e 6e 6f 74 0a 2a 2a 20 70 72 6f 63  s cannot.** proc
16e20 65 65 64 20 62 65 63 61 75 73 65 20 69 74 20 69  eed because it i
16e30 73 20 62 6c 6f 63 6b 65 64 20 62 79 20 74 68 65  s blocked by the
16e40 20 66 69 72 73 74 2e 20 20 49 66 20 62 6f 74 68   first.  If both
16e50 20 70 72 6f 63 65 73 73 65 73 0a 2a 2a 20 69 6e   processes.** in
16e60 76 6f 6b 65 20 74 68 65 20 62 75 73 79 20 68 61  voke the busy ha
16e70 6e 64 6c 65 72 73 2c 20 6e 65 69 74 68 65 72 20  ndlers, neither 
16e80 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 70 72  will make any pr
16e90 6f 67 72 65 73 73 2e 20 20 54 68 65 72 65 66 6f  ogress.  Therefo
16ea0 72 65 2c 0a 2a 2a 20 53 51 4c 69 74 65 20 72 65  re,.** SQLite re
16eb0 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 42 55  turns [SQLITE_BU
16ec0 53 59 5d 20 66 6f 72 20 74 68 65 20 66 69 72 73  SY] for the firs
16ed0 74 20 70 72 6f 63 65 73 73 2c 20 68 6f 70 69 6e  t process, hopin
16ee0 67 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20 77  g that this.** w
16ef0 69 6c 6c 20 69 6e 64 75 63 65 20 74 68 65 20 66  ill induce the f
16f00 69 72 73 74 20 70 72 6f 63 65 73 73 20 74 6f 20  irst process to 
16f10 72 65 6c 65 61 73 65 20 69 74 73 20 72 65 61 64  release its read
16f20 20 6c 6f 63 6b 20 61 6e 64 20 61 6c 6c 6f 77 0a   lock and allow.
16f30 2a 2a 20 74 68 65 20 73 65 63 6f 6e 64 20 70 72  ** the second pr
16f40 6f 63 65 73 73 20 74 6f 20 70 72 6f 63 65 65 64  ocess to proceed
16f50 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 65 66 61  ..**.** The defa
16f60 75 6c 74 20 62 75 73 79 20 63 61 6c 6c 62 61 63  ult busy callbac
16f70 6b 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  k is NULL..**.**
16f80 20 54 68 65 20 5b 53 51 4c 49 54 45 5f 42 55 53   The [SQLITE_BUS
16f90 59 5d 20 65 72 72 6f 72 20 69 73 20 63 6f 6e 76  Y] error is conv
16fa0 65 72 74 65 64 20 74 6f 20 5b 53 51 4c 49 54 45  erted to [SQLITE
16fb0 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d 0a  _IOERR_BLOCKED].
16fc0 2a 2a 20 77 68 65 6e 20 53 51 4c 69 74 65 20 69  ** when SQLite i
16fd0 73 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  s in the middle 
16fe0 6f 66 20 61 20 6c 61 72 67 65 20 74 72 61 6e 73  of a large trans
16ff0 61 63 74 69 6f 6e 20 77 68 65 72 65 20 61 6c 6c  action where all
17000 20 74 68 65 0a 2a 2a 20 63 68 61 6e 67 65 73 20   the.** changes 
17010 77 69 6c 6c 20 6e 6f 74 20 66 69 74 20 69 6e 74  will not fit int
17020 6f 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20  o the in-memory 
17030 63 61 63 68 65 2e 20 20 53 51 4c 69 74 65 20 77  cache.  SQLite w
17040 69 6c 6c 0a 2a 2a 20 61 6c 72 65 61 64 79 20 68  ill.** already h
17050 6f 6c 64 20 61 20 52 45 53 45 52 56 45 44 20 6c  old a RESERVED l
17060 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
17070 61 73 65 20 66 69 6c 65 2c 20 62 75 74 20 69 74  ase file, but it
17080 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20 70 72 6f   needs.** to pro
17090 6d 6f 74 65 20 74 68 69 73 20 6c 6f 63 6b 20 74  mote this lock t
170a0 6f 20 45 58 43 4c 55 53 49 56 45 20 73 6f 20 74  o EXCLUSIVE so t
170b0 68 61 74 20 69 74 20 63 61 6e 20 73 70 69 6c 6c  hat it can spill
170c0 20 63 61 63 68 65 0a 2a 2a 20 70 61 67 65 73 20   cache.** pages 
170d0 69 6e 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  into the databas
170e0 65 20 66 69 6c 65 20 77 69 74 68 6f 75 74 20 68  e file without h
170f0 61 72 6d 20 74 6f 20 63 6f 6e 63 75 72 72 65 6e  arm to concurren
17100 74 0a 2a 2a 20 72 65 61 64 65 72 73 2e 20 20 49  t.** readers.  I
17110 66 20 69 74 20 69 73 20 75 6e 61 62 6c 65 20 74  f it is unable t
17120 6f 20 70 72 6f 6d 6f 74 65 20 74 68 65 20 6c 6f  o promote the lo
17130 63 6b 2c 20 74 68 65 6e 20 74 68 65 20 69 6e 2d  ck, then the in-
17140 6d 65 6d 6f 72 79 0a 2a 2a 20 63 61 63 68 65 20  memory.** cache 
17150 77 69 6c 6c 20 62 65 20 6c 65 66 74 20 69 6e 20  will be left in 
17160 61 6e 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74 20  an inconsistent 
17170 73 74 61 74 65 20 61 6e 64 20 73 6f 20 74 68 65  state and so the
17180 20 65 72 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69   error.** code i
17190 73 20 70 72 6f 6d 6f 74 65 64 20 66 72 6f 6d 20  s promoted from 
171a0 74 68 65 20 72 65 6c 61 74 69 76 65 6c 79 20 62  the relatively b
171b0 65 6e 69 67 6e 20 5b 53 51 4c 49 54 45 5f 42 55  enign [SQLITE_BU
171c0 53 59 5d 20 74 6f 0a 2a 2a 20 74 68 65 20 6d 6f  SY] to.** the mo
171d0 72 65 20 73 65 76 65 72 65 20 5b 53 51 4c 49 54  re severe [SQLIT
171e0 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b 45 44 5d  E_IOERR_BLOCKED]
171f0 2e 20 20 54 68 69 73 20 65 72 72 6f 72 20 63 6f  .  This error co
17200 64 65 20 70 72 6f 6d 6f 74 69 6f 6e 0a 2a 2a 20  de promotion.** 
17210 66 6f 72 63 65 73 20 61 6e 20 61 75 74 6f 6d 61  forces an automa
17220 74 69 63 20 72 6f 6c 6c 62 61 63 6b 20 6f 66 20  tic rollback of 
17230 74 68 65 20 63 68 61 6e 67 65 73 2e 20 20 53 65  the changes.  Se
17240 65 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66  e the.** <a href
17250 3d 22 2f 63 76 73 74 72 61 63 2f 77 69 6b 69 3f  ="/cvstrac/wiki?
17260 70 3d 43 6f 72 72 75 70 74 69 6f 6e 46 6f 6c 6c  p=CorruptionFoll
17270 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72 22 3e  owingBusyError">
17280 0a 2a 2a 20 43 6f 72 72 75 70 74 69 6f 6e 46 6f  .** CorruptionFo
17290 6c 6c 6f 77 69 6e 67 42 75 73 79 45 72 72 6f 72  llowingBusyError
172a0 3c 2f 61 3e 20 77 69 6b 69 20 70 61 67 65 20 66  </a> wiki page f
172b0 6f 72 20 61 20 64 69 73 63 75 73 73 69 6f 6e 20  or a discussion 
172c0 6f 66 20 77 68 79 0a 2a 2a 20 74 68 69 73 20 69  of why.** this i
172d0 73 20 69 6d 70 6f 72 74 61 6e 74 2e 0a 2a 2a 0a  s important..**.
172e0 2a 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c  ** There can onl
172f0 79 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75  y be a single bu
17300 73 79 20 68 61 6e 64 6c 65 72 20 64 65 66 69 6e  sy handler defin
17310 65 64 20 66 6f 72 20 65 61 63 68 0a 2a 2a 20 5b  ed for each.** [
17320 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
17330 69 6f 6e 5d 2e 20 20 53 65 74 74 69 6e 67 20 61  ion].  Setting a
17340 20 6e 65 77 20 62 75 73 79 20 68 61 6e 64 6c 65   new busy handle
17350 72 20 63 6c 65 61 72 73 20 61 6e 79 0a 2a 2a 20  r clears any.** 
17360 70 72 65 76 69 6f 75 73 6c 79 20 73 65 74 20 68  previously set h
17370 61 6e 64 6c 65 72 2e 20 20 4e 6f 74 65 20 74 68  andler.  Note th
17380 61 74 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69  at calling [sqli
17390 74 65 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74  te3_busy_timeout
173a0 28 29 5d 0a 2a 2a 20 77 69 6c 6c 20 61 6c 73 6f  ()].** will also
173b0 20 73 65 74 20 6f 72 20 63 6c 65 61 72 20 74 68   set or clear th
173c0 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 2e 0a  e busy handler..
173d0 2a 2a 0a 2a 2a 20 54 68 65 20 62 75 73 79 20 63  **.** The busy c
173e0 61 6c 6c 62 61 63 6b 20 73 68 6f 75 6c 64 20 6e  allback should n
173f0 6f 74 20 74 61 6b 65 20 61 6e 79 20 61 63 74 69  ot take any acti
17400 6f 6e 73 20 77 68 69 63 68 20 6d 6f 64 69 66 79  ons which modify
17410 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
17420 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 68 61 74   connection that
17430 20 69 6e 76 6f 6b 65 64 20 74 68 65 20 62 75 73   invoked the bus
17440 79 20 68 61 6e 64 6c 65 72 2e 20 20 41 6e 79 20  y handler.  Any 
17450 73 75 63 68 20 61 63 74 69 6f 6e 73 0a 2a 2a 20  such actions.** 
17460 72 65 73 75 6c 74 20 69 6e 20 75 6e 64 65 66 69  result in undefi
17470 6e 65 64 20 62 65 68 61 76 69 6f 72 2e 0a 2a 2a  ned behavior..**
17480 20 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a   .** INVARIANTS:
17490 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 31 7d 20  .**.** {H12311} 
174a0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 75 73  The [sqlite3_bus
174b0 79 5f 68 61 6e 64 6c 65 72 28 44 2c 43 2c 41 29  y_handler(D,C,A)
174c0 5d 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c  ] function shall
174d0 20 72 65 70 6c 61 63 65 0a 2a 2a 20 20 20 20 20   replace.**     
174e0 20 20 20 20 20 62 75 73 79 20 63 61 6c 6c 62 61       busy callba
174f0 63 6b 20 69 6e 20 74 68 65 20 5b 64 61 74 61 62  ck in the [datab
17500 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
17510 44 20 77 69 74 68 20 61 20 6e 65 77 0a 2a 2a 20  D with a new.** 
17520 20 20 20 20 20 20 20 20 20 61 20 6e 65 77 20 62           a new b
17530 75 73 79 20 68 61 6e 64 6c 65 72 20 43 20 61 6e  usy handler C an
17540 64 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 61  d application da
17550 74 61 20 70 6f 69 6e 74 65 72 20 41 2e 0a 2a 2a  ta pointer A..**
17560 0a 2a 2a 20 7b 48 31 32 33 31 32 7d 20 4e 65 77  .** {H12312} New
17570 6c 79 20 63 72 65 61 74 65 64 20 5b 64 61 74 61  ly created [data
17580 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
17590 5d 20 73 68 61 6c 6c 20 68 61 76 65 20 61 20 62  ] shall have a b
175a0 75 73 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  usy.**          
175b0 68 61 6e 64 6c 65 72 20 6f 66 20 4e 55 4c 4c 2e  handler of NULL.
175c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 31 34 7d 20  .**.** {H12314} 
175d0 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d 6f 72 65  When two or more
175e0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
175f0 63 74 69 6f 6e 73 5d 20 73 68 61 72 65 20 61 0a  ctions] share a.
17600 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
17610 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72  ite3_enable_shar
17620 65 64 5f 63 61 63 68 65 20 7c 20 63 6f 6d 6d 6f  ed_cache | commo
17630 6e 20 63 61 63 68 65 5d 2c 0a 2a 2a 20 20 20 20  n cache],.**    
17640 20 20 20 20 20 20 74 68 65 20 62 75 73 79 20 68        the busy h
17650 61 6e 64 6c 65 72 20 66 6f 72 20 74 68 65 20 64  andler for the d
17660 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
17670 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 75 73 69  on currently usi
17680 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ng.**          t
17690 68 65 20 63 61 63 68 65 20 73 68 61 6c 6c 20 62  he cache shall b
176a0 65 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 20 74  e invoked when t
176b0 68 65 20 63 61 63 68 65 20 65 6e 63 6f 75 6e 74  he cache encount
176c0 65 72 73 20 61 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a  ers a lock..**.*
176d0 2a 20 7b 48 31 32 33 31 36 7d 20 49 66 20 61 20  * {H12316} If a 
176e0 62 75 73 79 20 68 61 6e 64 6c 65 72 20 63 61 6c  busy handler cal
176f0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 7a 65  lback returns ze
17700 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 53 51 4c  ro, then the SQL
17710 69 74 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a  ite interface.**
17720 20 20 20 20 20 20 20 20 20 20 74 68 61 74 20 70            that p
17730 72 6f 76 6f 6b 65 64 20 74 68 65 20 6c 6f 63 6b  rovoked the lock
17740 69 6e 67 20 65 76 65 6e 74 20 73 68 61 6c 6c 20  ing event shall 
17750 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 42  return [SQLITE_B
17760 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  USY]..**.** {H12
17770 33 31 38 7d 20 53 51 4c 69 74 65 20 73 68 61 6c  318} SQLite shal
17780 6c 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 62 75  l invokes the bu
17790 73 79 20 68 61 6e 64 6c 65 72 20 77 69 74 68 20  sy handler with 
177a0 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 20 77 68  two arguments wh
177b0 69 63 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ich.**          
177c0 61 72 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68  are a copy of th
177d0 65 20 70 6f 69 6e 74 65 72 20 73 75 70 70 6c 69  e pointer suppli
177e0 65 64 20 62 79 20 74 68 65 20 33 72 64 20 70 61  ed by the 3rd pa
177f0 72 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20  rameter to.**   
17800 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
17810 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 29 5d 20  busy_handler()] 
17820 61 6e 64 20 61 20 63 6f 75 6e 74 20 6f 66 20 74  and a count of t
17830 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 72 69  he number of pri
17840 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  or.**          i
17850 6e 76 6f 63 61 74 69 6f 6e 73 20 6f 66 20 74 68  nvocations of th
17860 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 66  e busy handler f
17870 6f 72 20 74 68 65 20 73 61 6d 65 20 6c 6f 63 6b  or the same lock
17880 69 6e 67 20 65 76 65 6e 74 2e 0a 2a 2a 0a 2a 2a  ing event..**.**
17890 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a   ASSUMPTIONS:.**
178a0 0a 2a 2a 20 7b 41 31 32 33 31 39 7d 20 41 20 62  .** {A12319} A b
178b0 75 73 79 20 68 61 6e 64 6c 65 72 20 6d 75 73 74  usy handler must
178c0 20 6e 6f 74 20 63 6c 6f 73 65 20 74 68 65 20 64   not close the d
178d0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
178e0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  on.**          o
178f0 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  r [prepared stat
17900 65 6d 65 6e 74 5d 20 74 68 61 74 20 69 6e 76 6f  ement] that invo
17910 6b 65 64 20 74 68 65 20 62 75 73 79 20 68 61 6e  ked the busy han
17920 64 6c 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dler..*/.SQLITE_
17930 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
17940 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 73 71 6c  busy_handler(sql
17950 69 74 65 33 2a 2c 20 69 6e 74 28 2a 29 28 76 6f  ite3*, int(*)(vo
17960 69 64 2a 2c 69 6e 74 29 2c 20 76 6f 69 64 2a 29  id*,int), void*)
17970 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
17980 46 3a 20 53 65 74 20 41 20 42 75 73 79 20 54 69  F: Set A Busy Ti
17990 6d 65 6f 75 74 20 7b 48 31 32 33 34 30 7d 20 3c  meout {H12340} <
179a0 53 34 30 34 31 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S40410>.**.** Th
179b0 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
179c0 61 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  a [sqlite3_busy_
179d0 68 61 6e 64 6c 65 72 20 7c 20 62 75 73 79 20 68  handler | busy h
179e0 61 6e 64 6c 65 72 5d 20 74 68 61 74 20 73 6c 65  andler] that sle
179f0 65 70 73 0a 2a 2a 20 66 6f 72 20 61 20 73 70 65  eps.** for a spe
17a00 63 69 66 69 65 64 20 61 6d 6f 75 6e 74 20 6f 66  cified amount of
17a10 20 74 69 6d 65 20 77 68 65 6e 20 61 20 74 61 62   time when a tab
17a20 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2e 20 20 54  le is locked.  T
17a30 68 65 20 68 61 6e 64 6c 65 72 0a 2a 2a 20 77 69  he handler.** wi
17a40 6c 6c 20 73 6c 65 65 70 20 6d 75 6c 74 69 70 6c  ll sleep multipl
17a50 65 20 74 69 6d 65 73 20 75 6e 74 69 6c 20 61 74  e times until at
17a60 20 6c 65 61 73 74 20 22 6d 73 22 20 6d 69 6c 6c   least "ms" mill
17a70 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65  iseconds of slee
17a80 70 69 6e 67 0a 2a 2a 20 68 61 76 65 20 61 63 63  ping.** have acc
17a90 75 6d 75 6c 61 74 65 64 2e 20 7b 48 31 32 33 34  umulated. {H1234
17aa0 33 7d 20 41 66 74 65 72 20 22 6d 73 22 20 6d 69  3} After "ms" mi
17ab0 6c 6c 69 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c  lliseconds of sl
17ac0 65 65 70 69 6e 67 2c 0a 2a 2a 20 74 68 65 20 68  eeping,.** the h
17ad0 61 6e 64 6c 65 72 20 72 65 74 75 72 6e 73 20 30  andler returns 0
17ae0 20 77 68 69 63 68 20 63 61 75 73 65 73 20 5b 73   which causes [s
17af0 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 74  qlite3_step()] t
17b00 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 5b 53 51 4c  o return.** [SQL
17b10 49 54 45 5f 42 55 53 59 5d 20 6f 72 20 5b 53 51  ITE_BUSY] or [SQ
17b20 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
17b30 45 44 5d 2e 0a 2a 2a 0a 2a 2a 20 43 61 6c 6c 69  ED]..**.** Calli
17b40 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ng this routine 
17b50 77 69 74 68 20 61 6e 20 61 72 67 75 6d 65 6e 74  with an argument
17b60 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
17b70 75 61 6c 20 74 6f 20 7a 65 72 6f 0a 2a 2a 20 74  ual to zero.** t
17b80 75 72 6e 73 20 6f 66 66 20 61 6c 6c 20 62 75 73  urns off all bus
17b90 79 20 68 61 6e 64 6c 65 72 73 2e 0a 2a 2a 0a 2a  y handlers..**.*
17ba0 2a 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79  * There can only
17bb0 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62 75 73   be a single bus
17bc0 79 20 68 61 6e 64 6c 65 72 20 66 6f 72 20 61 20  y handler for a 
17bd0 70 61 72 74 69 63 75 6c 61 72 0a 2a 2a 20 5b 64  particular.** [d
17be0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
17bf0 6f 6e 5d 20 61 6e 79 20 61 6e 79 20 67 69 76 65  on] any any give
17c00 6e 20 6d 6f 6d 65 6e 74 2e 20 20 49 66 20 61 6e  n moment.  If an
17c10 6f 74 68 65 72 20 62 75 73 79 20 68 61 6e 64 6c  other busy handl
17c20 65 72 0a 2a 2a 20 77 61 73 20 64 65 66 69 6e 65  er.** was define
17c30 64 20 20 28 75 73 69 6e 67 20 5b 73 71 6c 69 74  d  (using [sqlit
17c40 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
17c50 29 5d 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  )]) prior to cal
17c60 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ling.** this rou
17c70 74 69 6e 65 2c 20 74 68 61 74 20 6f 74 68 65 72  tine, that other
17c80 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 69 73   busy handler is
17c90 20 63 6c 65 61 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   cleared..**.** 
17ca0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
17cb0 2a 20 7b 48 31 32 33 34 31 7d 20 54 68 65 20 5b  * {H12341} The [
17cc0 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
17cd0 65 6f 75 74 28 29 5d 20 66 75 6e 63 74 69 6f 6e  eout()] function
17ce0 20 73 68 61 6c 6c 20 6f 76 65 72 72 69 64 65 20   shall override 
17cf0 61 6e 79 20 70 72 69 6f 72 0a 2a 2a 20 20 20 20  any prior.**    
17d00 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62        [sqlite3_b
17d10 75 73 79 5f 74 69 6d 65 6f 75 74 28 29 5d 20 6f  usy_timeout()] o
17d20 72 20 5b 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  r [sqlite3_busy_
17d30 68 61 6e 64 6c 65 72 28 29 5d 20 73 65 74 74 69  handler()] setti
17d40 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ng.**          o
17d50 6e 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61  n the same [data
17d60 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
17d70 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 34 33 7d  ..**.** {H12343}
17d80 20 49 66 20 74 68 65 20 32 6e 64 20 70 61 72 61   If the 2nd para
17d90 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65  meter to [sqlite
17da0 33 5f 62 75 73 79 5f 74 69 6d 65 6f 75 74 28 29  3_busy_timeout()
17db0 5d 20 69 73 20 6c 65 73 73 20 74 68 61 6e 0a 2a  ] is less than.*
17dc0 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 65 71  *          or eq
17dd0 75 61 6c 20 74 6f 20 7a 65 72 6f 2c 20 74 68 65  ual to zero, the
17de0 6e 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  n the busy handl
17df0 65 72 20 73 68 61 6c 6c 20 62 65 20 63 6c 65 61  er shall be clea
17e00 72 65 64 20 73 6f 20 74 68 61 74 0a 2a 2a 20 20  red so that.**  
17e10 20 20 20 20 20 20 20 20 61 6c 6c 20 73 75 62 73          all subs
17e20 65 71 75 65 6e 74 20 6c 6f 63 6b 69 6e 67 20 65  equent locking e
17e30 76 65 6e 74 73 20 69 6d 6d 65 64 69 61 74 65 6c  vents immediatel
17e40 79 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45  y return [SQLITE
17e50 5f 42 55 53 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  _BUSY]..**.** {H
17e60 31 32 33 34 34 7d 20 49 66 20 74 68 65 20 32 6e  12344} If the 2n
17e70 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  d parameter to [
17e80 73 71 6c 69 74 65 33 5f 62 75 73 79 5f 74 69 6d  sqlite3_busy_tim
17e90 65 6f 75 74 28 29 5d 20 69 73 20 61 20 70 6f 73  eout()] is a pos
17ea0 69 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20  itive.**        
17eb0 20 20 6e 75 6d 62 65 72 20 4e 2c 20 74 68 65 6e    number N, then
17ec0 20 61 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20   a busy handler 
17ed0 73 68 61 6c 6c 20 62 65 20 73 65 74 20 74 68 61  shall be set tha
17ee0 74 20 72 65 70 65 61 74 65 64 6c 79 20 63 61 6c  t repeatedly cal
17ef0 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ls.**          t
17f00 68 65 20 78 53 6c 65 65 70 28 29 20 6d 65 74 68  he xSleep() meth
17f10 6f 64 20 69 6e 20 74 68 65 20 5b 73 71 6c 69 74  od in the [sqlit
17f20 65 33 5f 76 66 73 20 7c 20 56 46 53 20 69 6e 74  e3_vfs | VFS int
17f30 65 72 66 61 63 65 5d 20 75 6e 74 69 6c 0a 2a 2a  erface] until.**
17f40 20 20 20 20 20 20 20 20 20 20 65 69 74 68 65 72            either
17f50 20 74 68 65 20 6c 6f 63 6b 20 63 6c 65 61 72 73   the lock clears
17f60 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20 63 75   or until the cu
17f70 6d 75 6c 61 74 69 76 65 20 73 6c 65 65 70 20 74  mulative sleep t
17f80 69 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ime.**          
17f90 72 65 70 6f 72 74 65 64 20 62 61 63 6b 20 62 79  reported back by
17fa0 20 78 53 6c 65 65 70 28 29 20 65 78 63 65 65 64   xSleep() exceed
17fb0 73 20 4e 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73  s N milliseconds
17fc0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
17fd0 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
17fe0 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33  _timeout(sqlite3
17ff0 2a 2c 20 69 6e 74 20 6d 73 29 3b 0a 0a 2f 2a 0a  *, int ms);../*.
18000 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43 6f 6e  ** CAPI3REF: Con
18010 76 65 6e 69 65 6e 63 65 20 52 6f 75 74 69 6e 65  venience Routine
18020 73 20 46 6f 72 20 52 75 6e 6e 69 6e 67 20 51 75  s For Running Qu
18030 65 72 69 65 73 20 7b 48 31 32 33 37 30 7d 20 3c  eries {H12370} <
18040 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 44 65  S10000>.**.** De
18050 66 69 6e 69 74 69 6f 6e 3a 20 41 20 3c 62 3e 72  finition: A <b>r
18060 65 73 75 6c 74 20 74 61 62 6c 65 3c 2f 62 3e 20  esult table</b> 
18070 69 73 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73  is memory data s
18080 74 72 75 63 74 75 72 65 20 63 72 65 61 74 65 64  tructure created
18090 20 62 79 20 74 68 65 0a 2a 2a 20 5b 73 71 6c 69   by the.** [sqli
180a0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d  te3_get_table()]
180b0 20 69 6e 74 65 72 66 61 63 65 2e 20 20 41 20 72   interface.  A r
180c0 65 73 75 6c 74 20 74 61 62 6c 65 20 72 65 63 6f  esult table reco
180d0 72 64 73 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 6c  rds the.** compl
180e0 65 74 65 20 71 75 65 72 79 20 72 65 73 75 6c 74  ete query result
180f0 73 20 66 72 6f 6d 20 6f 6e 65 20 6f 72 20 6d 6f  s from one or mo
18100 72 65 20 71 75 65 72 69 65 73 2e 0a 2a 2a 0a 2a  re queries..**.*
18110 2a 20 54 68 65 20 74 61 62 6c 65 20 63 6f 6e 63  * The table conc
18120 65 70 74 75 61 6c 6c 79 20 68 61 73 20 61 20 6e  eptually has a n
18130 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20 61 6e  umber of rows an
18140 64 20 63 6f 6c 75 6d 6e 73 2e 20 20 42 75 74 0a  d columns.  But.
18150 2a 2a 20 74 68 65 73 65 20 6e 75 6d 62 65 72 73  ** these numbers
18160 20 61 72 65 20 6e 6f 74 20 70 61 72 74 20 6f 66   are not part of
18170 20 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c   the result tabl
18180 65 20 69 74 73 65 6c 66 2e 20 20 54 68 65 73 65  e itself.  These
18190 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 72 65 20  .** numbers are 
181a0 6f 62 74 61 69 6e 65 64 20 73 65 70 61 72 61 74  obtained separat
181b0 65 6c 79 2e 20 20 4c 65 74 20 4e 20 62 65 20 74  ely.  Let N be t
181c0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
181d0 73 0a 2a 2a 20 61 6e 64 20 4d 20 62 65 20 74 68  s.** and M be th
181e0 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  e number of colu
181f0 6d 6e 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65 73  mns..**.** A res
18200 75 6c 74 20 74 61 62 6c 65 20 69 73 20 61 6e 20  ult table is an 
18210 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74 65 72  array of pointer
18220 73 20 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  s to zero-termin
18230 61 74 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e  ated UTF-8 strin
18240 67 73 2e 0a 2a 2a 20 54 68 65 72 65 20 61 72 65  gs..** There are
18250 20 28 4e 2b 31 29 2a 4d 20 65 6c 65 6d 65 6e 74   (N+1)*M element
18260 73 20 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20  s in the array. 
18270 20 54 68 65 20 66 69 72 73 74 20 4d 20 70 6f 69   The first M poi
18280 6e 74 65 72 73 20 70 6f 69 6e 74 0a 2a 2a 20 74  nters point.** t
18290 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65  o zero-terminate
182a0 64 20 73 74 72 69 6e 67 73 20 74 68 61 74 20 20  d strings that  
182b0 63 6f 6e 74 61 69 6e 20 74 68 65 20 6e 61 6d 65  contain the name
182c0 73 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 73  s of the columns
182d0 2e 0a 2a 2a 20 54 68 65 20 72 65 6d 61 69 6e 69  ..** The remaini
182e0 6e 67 20 65 6e 74 72 69 65 73 20 61 6c 6c 20 70  ng entries all p
182f0 6f 69 6e 74 20 74 6f 20 71 75 65 72 79 20 72 65  oint to query re
18300 73 75 6c 74 73 2e 20 20 4e 55 4c 4c 20 76 61 6c  sults.  NULL val
18310 75 65 73 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e  ues result.** in
18320 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 2e 20   NULL pointers. 
18330 20 41 6c 6c 20 6f 74 68 65 72 20 76 61 6c 75 65   All other value
18340 73 20 61 72 65 20 69 6e 20 74 68 65 69 72 20 55  s are in their U
18350 54 46 2d 38 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  TF-8 zero-termin
18360 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 72  ated.** string r
18370 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 61 73  epresentation as
18380 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
18390 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
183a0 74 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65  t()]..**.** A re
183b0 73 75 6c 74 20 74 61 62 6c 65 20 6d 69 67 68 74  sult table might
183c0 20 63 6f 6e 73 69 73 74 20 6f 66 20 6f 6e 65 20   consist of one 
183d0 6f 72 20 6d 6f 72 65 20 6d 65 6d 6f 72 79 20 61  or more memory a
183e0 6c 6c 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2a 20 49  llocations..** I
183f0 74 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f  t is not safe to
18400 20 70 61 73 73 20 61 20 72 65 73 75 6c 74 20 74   pass a result t
18410 61 62 6c 65 20 64 69 72 65 63 74 6c 79 20 74 6f  able directly to
18420 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
18430 5d 2e 0a 2a 2a 20 41 20 72 65 73 75 6c 74 20 74  ]..** A result t
18440 61 62 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 64  able should be d
18450 65 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67  eallocated using
18460 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74   [sqlite3_free_t
18470 61 62 6c 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 41  able()]..**.** A
18480 73 20 61 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20  s an example of 
18490 74 68 65 20 72 65 73 75 6c 74 20 74 61 62 6c 65  the result table
184a0 20 66 6f 72 6d 61 74 2c 20 73 75 70 70 6f 73 65   format, suppose
184b0 20 61 20 71 75 65 72 79 20 72 65 73 75 6c 74 0a   a query result.
184c0 2a 2a 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ** is as follows
184d0 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
184e0 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 20 20  ote><pre>.**    
184f0 20 20 20 20 4e 61 6d 65 20 20 20 20 20 20 20 20      Name        
18500 7c 20 41 67 65 0a 2a 2a 20 20 20 20 20 20 20 20  | Age.**        
18510 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
18520 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20  -------.**      
18530 20 20 41 6c 69 63 65 20 20 20 20 20 20 20 7c 20    Alice       | 
18540 34 33 0a 2a 2a 20 20 20 20 20 20 20 20 42 6f 62  43.**        Bob
18550 20 20 20 20 20 20 20 20 20 7c 20 32 38 0a 2a 2a           | 28.**
18560 20 20 20 20 20 20 20 20 43 69 6e 64 79 20 20 20          Cindy   
18570 20 20 20 20 7c 20 32 31 0a 2a 2a 20 3c 2f 70 72      | 21.** </pr
18580 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a  e></blockquote>.
18590 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 61 72 65 20  **.** There are 
185a0 74 77 6f 20 63 6f 6c 75 6d 6e 20 28 4d 3d 3d 32  two column (M==2
185b0 29 20 61 6e 64 20 74 68 72 65 65 20 72 6f 77 73  ) and three rows
185c0 20 28 4e 3d 3d 33 29 2e 20 20 54 68 75 73 20 74   (N==3).  Thus t
185d0 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 74 61 62  he.** result tab
185e0 6c 65 20 68 61 73 20 38 20 65 6e 74 72 69 65 73  le has 8 entries
185f0 2e 20 20 53 75 70 70 6f 73 65 20 74 68 65 20 72  .  Suppose the r
18600 65 73 75 6c 74 20 74 61 62 6c 65 20 69 73 20 73  esult table is s
18610 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 61 6e 20 61  tored.** in an a
18620 72 72 61 79 20 6e 61 6d 65 73 20 61 7a 52 65 73  rray names azRes
18630 75 6c 74 2e 20 20 54 68 65 6e 20 61 7a 52 65 73  ult.  Then azRes
18640 75 6c 74 20 68 6f 6c 64 73 20 74 68 69 73 20 63  ult holds this c
18650 6f 6e 74 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 3c 62  ontent:.**.** <b
18660 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70 72 65 3e 0a  lockquote><pre>.
18670 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73 75  **        azResu
18680 6c 74 26 23 39 31 3b 30 5d 20 3d 20 22 4e 61 6d  lt&#91;0] = "Nam
18690 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a  e";.**        az
186a0 52 65 73 75 6c 74 26 23 39 31 3b 31 5d 20 3d 20  Result&#91;1] = 
186b0 22 41 67 65 22 3b 0a 2a 2a 20 20 20 20 20 20 20  "Age";.**       
186c0 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 32 5d   azResult&#91;2]
186d0 20 3d 20 22 41 6c 69 63 65 22 3b 0a 2a 2a 20 20   = "Alice";.**  
186e0 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74 26 23        azResult&#
186f0 39 31 3b 33 5d 20 3d 20 22 34 33 22 3b 0a 2a 2a  91;3] = "43";.**
18700 20 20 20 20 20 20 20 20 61 7a 52 65 73 75 6c 74          azResult
18710 26 23 39 31 3b 34 5d 20 3d 20 22 42 6f 62 22 3b  &#91;4] = "Bob";
18720 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52 65 73  .**        azRes
18730 75 6c 74 26 23 39 31 3b 35 5d 20 3d 20 22 32 38  ult&#91;5] = "28
18740 22 3b 0a 2a 2a 20 20 20 20 20 20 20 20 61 7a 52  ";.**        azR
18750 65 73 75 6c 74 26 23 39 31 3b 36 5d 20 3d 20 22  esult&#91;6] = "
18760 43 69 6e 64 79 22 3b 0a 2a 2a 20 20 20 20 20 20  Cindy";.**      
18770 20 20 61 7a 52 65 73 75 6c 74 26 23 39 31 3b 37    azResult&#91;7
18780 5d 20 3d 20 22 32 31 22 3b 0a 2a 2a 20 3c 2f 70  ] = "21";.** </p
18790 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
187a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
187b0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 66  e3_get_table() f
187c0 75 6e 63 74 69 6f 6e 20 65 76 61 6c 75 61 74 65  unction evaluate
187d0 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a 2a  s one or more.**
187e0 20 73 65 6d 69 63 6f 6c 6f 6e 2d 73 65 70 61 72   semicolon-separ
187f0 61 74 65 64 20 53 51 4c 20 73 74 61 74 65 6d 65  ated SQL stateme
18800 6e 74 73 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d  nts in the zero-
18810 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38  terminated UTF-8
18820 0a 2a 2a 20 73 74 72 69 6e 67 20 6f 66 20 69 74  .** string of it
18830 73 20 32 6e 64 20 70 61 72 61 6d 65 74 65 72 2e  s 2nd parameter.
18840 20 20 49 74 20 72 65 74 75 72 6e 73 20 61 20 72    It returns a r
18850 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 74  esult table to t
18860 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 67 69  he.** pointer gi
18870 76 65 6e 20 69 6e 20 69 74 73 20 33 72 64 20 70  ven in its 3rd p
18880 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
18890 41 66 74 65 72 20 74 68 65 20 63 61 6c 6c 69 6e  After the callin
188a0 67 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 20 66  g function has f
188b0 69 6e 69 73 68 65 64 20 75 73 69 6e 67 20 74 68  inished using th
188c0 65 20 72 65 73 75 6c 74 2c 20 69 74 20 73 68 6f  e result, it sho
188d0 75 6c 64 0a 2a 2a 20 70 61 73 73 20 74 68 65 20  uld.** pass the 
188e0 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72  pointer to the r
188f0 65 73 75 6c 74 20 74 61 62 6c 65 20 74 6f 20 73  esult table to s
18900 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c  qlite3_free_tabl
18910 65 28 29 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a  e() in order to.
18920 2a 2a 20 72 65 6c 65 61 73 65 20 74 68 65 20 6d  ** release the m
18930 65 6d 6f 72 79 20 74 68 61 74 20 77 61 73 20 6d  emory that was m
18940 61 6c 6c 6f 63 65 64 2e 20 20 42 65 63 61 75 73  alloced.  Becaus
18950 65 20 6f 66 20 74 68 65 20 77 61 79 20 74 68 65  e of the way the
18960 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  .** [sqlite3_mal
18970 6c 6f 63 28 29 5d 20 68 61 70 70 65 6e 73 20 77  loc()] happens w
18980 69 74 68 69 6e 20 73 71 6c 69 74 65 33 5f 67 65  ithin sqlite3_ge
18990 74 5f 74 61 62 6c 65 28 29 2c 20 74 68 65 20 63  t_table(), the c
189a0 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
189b0 6f 6e 20 6d 75 73 74 20 6e 6f 74 20 74 72 79 20  on must not try 
189c0 74 6f 20 63 61 6c 6c 20 5b 73 71 6c 69 74 65 33  to call [sqlite3
189d0 5f 66 72 65 65 28 29 5d 20 64 69 72 65 63 74 6c  _free()] directl
189e0 79 2e 20 20 4f 6e 6c 79 0a 2a 2a 20 5b 73 71 6c  y.  Only.** [sql
189f0 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28  ite3_free_table(
18a00 29 5d 20 69 73 20 61 62 6c 65 20 74 6f 20 72 65  )] is able to re
18a10 6c 65 61 73 65 20 74 68 65 20 6d 65 6d 6f 72 79  lease the memory
18a20 20 70 72 6f 70 65 72 6c 79 20 61 6e 64 20 73 61   properly and sa
18a30 66 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fely..**.** The 
18a40 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c  sqlite3_get_tabl
18a50 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  e() interface is
18a60 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
18a70 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  a wrapper around
18a80 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 65 78 65  .** [sqlite3_exe
18a90 63 28 29 5d 2e 20 20 54 68 65 20 73 71 6c 69 74  c()].  The sqlit
18aa0 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 20 72  e3_get_table() r
18ab0 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20  outine does not 
18ac0 68 61 76 65 20 61 63 63 65 73 73 0a 2a 2a 20 74  have access.** t
18ad0 6f 20 61 6e 79 20 69 6e 74 65 72 6e 61 6c 20 64  o any internal d
18ae0 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 6f  ata structures o
18af0 66 20 53 51 4c 69 74 65 2e 20 20 49 74 20 75 73  f SQLite.  It us
18b00 65 73 20 6f 6e 6c 79 20 74 68 65 20 70 75 62 6c  es only the publ
18b10 69 63 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20  ic.** interface 
18b20 64 65 66 69 6e 65 64 20 68 65 72 65 2e 20 20 41  defined here.  A
18b30 73 20 61 20 63 6f 6e 73 65 71 75 65 6e 63 65 2c  s a consequence,
18b40 20 65 72 72 6f 72 73 20 74 68 61 74 20 6f 63 63   errors that occ
18b50 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 61  ur in the.** wra
18b60 70 70 65 72 20 6c 61 79 65 72 20 6f 75 74 73 69  pper layer outsi
18b70 64 65 20 6f 66 20 74 68 65 20 69 6e 74 65 72 6e  de of the intern
18b80 61 6c 20 5b 73 71 6c 69 74 65 33 5f 65 78 65 63  al [sqlite3_exec
18b90 28 29 5d 20 63 61 6c 6c 20 61 72 65 20 6e 6f 74  ()] call are not
18ba0 0a 2a 2a 20 72 65 66 6c 65 63 74 65 64 20 69 6e  .** reflected in
18bb0 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c   subsequent call
18bc0 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72  s to [sqlite3_er
18bd0 72 63 6f 64 65 28 29 5d 20 6f 72 20 5b 73 71 6c  rcode()] or [sql
18be0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a  ite3_errmsg()]..
18bf0 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
18c00 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 31 7d  :.**.** {H12371}
18c10 20 49 66 20 61 20 5b 73 71 6c 69 74 65 33 5f 67   If a [sqlite3_g
18c20 65 74 5f 74 61 62 6c 65 28 29 5d 20 66 61 69 6c  et_table()] fail
18c30 73 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  s a memory alloc
18c40 61 74 69 6f 6e 2c 20 74 68 65 6e 0a 2a 2a 20 20  ation, then.**  
18c50 20 20 20 20 20 20 20 20 69 74 20 73 68 61 6c 6c          it shall
18c60 20 66 72 65 65 20 74 68 65 20 72 65 73 75 6c 74   free the result
18c70 20 74 61 62 6c 65 20 75 6e 64 65 72 20 63 6f 6e   table under con
18c80 73 74 72 75 63 74 69 6f 6e 2c 20 61 62 6f 72 74  struction, abort
18c90 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
18ca0 20 71 75 65 72 79 20 69 6e 20 70 72 6f 63 65 73   query in proces
18cb0 73 2c 20 73 6b 69 70 20 61 6e 79 20 73 75 62 73  s, skip any subs
18cc0 65 71 75 65 6e 74 20 71 75 65 72 69 65 73 2c 20  equent queries, 
18cd0 73 65 74 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  set the.**      
18ce0 20 20 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 6f      *pazResult o
18cf0 75 74 70 75 74 20 70 6f 69 6e 74 65 72 20 74 6f  utput pointer to
18d00 20 4e 55 4c 4c 20 61 6e 64 20 72 65 74 75 72 6e   NULL and return
18d10 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e   [SQLITE_NOMEM].
18d20 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33 37 33 7d 20  .**.** {H12373} 
18d30 49 66 20 74 68 65 20 70 6e 43 6f 6c 75 6d 6e 20  If the pnColumn 
18d40 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
18d50 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28  lite3_get_table(
18d60 29 5d 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 0a 2a  )] is not NULL.*
18d70 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20  *          then 
18d80 61 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76  a successful inv
18d90 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69  ocation of [sqli
18da0 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d  te3_get_table()]
18db0 20 73 68 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20   shall.**       
18dc0 20 20 20 77 72 69 74 65 20 74 68 65 20 6e 75 6d     write the num
18dd0 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
18de0 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
18df0 20 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20    result set of 
18e00 74 68 65 20 71 75 65 72 79 20 69 6e 74 6f 20 2a  the query into *
18e10 70 6e 43 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  pnColumn..**.** 
18e20 7b 48 31 32 33 37 34 7d 20 49 66 20 74 68 65 20  {H12374} If the 
18e30 70 6e 52 6f 77 20 70 61 72 61 6d 65 74 65 72 20  pnRow parameter 
18e40 74 6f 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f  to [sqlite3_get_
18e50 74 61 62 6c 65 28 29 5d 20 69 73 20 6e 6f 74 20  table()] is not 
18e60 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20  NULL.**         
18e70 20 74 68 65 6e 20 61 20 73 75 63 63 65 73 73 66   then a successf
18e80 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  ul invocation of
18e90 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
18ea0 62 6c 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a 20  ble()] shall.** 
18eb0 20 20 20 20 20 20 20 20 20 77 72 69 74 65 73 20           writes 
18ec0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  the number of ro
18ed0 77 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20  ws in the.**    
18ee0 20 20 20 20 20 20 72 65 73 75 6c 74 20 73 65 74        result set
18ef0 20 6f 66 20 74 68 65 20 71 75 65 72 79 20 69 6e   of the query in
18f00 74 6f 20 2a 70 6e 52 6f 77 2e 0a 2a 2a 0a 2a 2a  to *pnRow..**.**
18f10 20 7b 48 31 32 33 37 36 7d 20 41 20 73 75 63 63   {H12376} A succ
18f20 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f  essful invocatio
18f30 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 67 65  n of [sqlite3_ge
18f40 74 5f 74 61 62 6c 65 28 29 5d 20 74 68 61 74 20  t_table()] that 
18f50 63 6f 6d 70 75 74 65 73 0a 2a 2a 20 20 20 20 20  computes.**     
18f60 20 20 20 20 20 4e 20 72 6f 77 73 20 6f 66 20 72       N rows of r
18f70 65 73 75 6c 74 20 77 69 74 68 20 43 20 63 6f 6c  esult with C col
18f80 75 6d 6e 73 20 70 65 72 20 72 6f 77 20 73 68 61  umns per row sha
18f90 6c 6c 20 6d 61 6b 65 20 2a 70 61 7a 52 65 73 75  ll make *pazResu
18fa0 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  lt.**          p
18fb0 6f 69 6e 74 20 74 6f 20 61 6e 20 61 72 72 61 79  oint to an array
18fc0 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
18fd0 28 4e 2b 31 29 2a 43 20 73 74 72 69 6e 67 73 20  (N+1)*C strings 
18fe0 77 68 65 72 65 20 74 68 65 20 66 69 72 73 74 0a  where the first.
18ff0 2a 2a 20 20 20 20 20 20 20 20 20 20 43 20 73 74  **          C st
19000 72 69 6e 67 73 20 61 72 65 20 63 6f 6c 75 6d 6e  rings are column
19010 20 6e 61 6d 65 73 20 61 73 20 6f 62 74 61 69 6e   names as obtain
19020 65 64 20 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20  ed from.**      
19030 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c      [sqlite3_col
19040 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20 61 6e 64 20  umn_name()] and 
19050 74 68 65 20 72 65 73 74 20 61 72 65 20 63 6f 6c  the rest are col
19060 75 6d 6e 20 72 65 73 75 6c 74 20 76 61 6c 75 65  umn result value
19070 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62  s.**          ob
19080 74 61 69 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c  tained from [sql
19090 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
190a0 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 33  ()]..**.** {H123
190b0 37 39 7d 20 54 68 65 20 76 61 6c 75 65 73 20 69  79} The values i
190c0 6e 20 74 68 65 20 70 61 7a 52 65 73 75 6c 74 20  n the pazResult 
190d0 61 72 72 61 79 20 72 65 74 75 72 6e 65 64 20 62  array returned b
190e0 79 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  y [sqlite3_get_t
190f0 61 62 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20  able()].**      
19100 20 20 20 20 73 68 61 6c 6c 20 72 65 6d 61 69 6e      shall remain
19110 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 63 6c 65   valid until cle
19120 61 72 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  ared by [sqlite3
19130 5f 66 72 65 65 5f 74 61 62 6c 65 28 29 5d 2e 0a  _free_table()]..
19140 2a 2a 0a 2a 2a 20 7b 48 31 32 33 38 32 7d 20 57  **.** {H12382} W
19150 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f 63 63  hen an error occ
19160 75 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75  urs during evalu
19170 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65  ation of [sqlite
19180 33 5f 67 65 74 5f 74 61 62 6c 65 28 29 5d 0a 2a  3_get_table()].*
19190 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66  *          the f
191a0 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c 20 73 65  unction shall se
191b0 74 20 2a 70 61 7a 52 65 73 75 6c 74 20 74 6f 20  t *pazResult to 
191c0 4e 55 4c 4c 2c 20 77 72 69 74 65 20 61 6e 20 65  NULL, write an e
191d0 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a 2a 20  rror message.** 
191e0 20 20 20 20 20 20 20 20 20 69 6e 74 6f 20 6d 65           into me
191f0 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
19200 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  om [sqlite3_mall
19210 6f 63 28 29 5d 2c 20 6d 61 6b 65 0a 2a 2a 20 20  oc()], make.**  
19220 20 20 20 20 20 20 20 20 2a 2a 70 7a 45 72 72 6d          **pzErrm
19230 73 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74  sg point to that
19240 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20   error message, 
19250 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a 20  and return a.** 
19260 20 20 20 20 20 20 20 20 20 61 70 70 72 6f 70 72           appropr
19270 69 61 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65  iate [error code
19280 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  ]..*/.SQLITE_API
19290 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74   int sqlite3_get
192a0 5f 74 61 62 6c 65 28 0a 20 20 73 71 6c 69 74 65  _table(.  sqlite
192b0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
192c0 2f 2a 20 41 6e 20 6f 70 65 6e 20 64 61 74 61 62  /* An open datab
192d0 61 73 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ase */.  const c
192e0 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 2f  har *zSql,     /
192f0 2a 20 53 51 4c 20 74 6f 20 62 65 20 65 76 61 6c  * SQL to be eval
19300 75 61 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  uated */.  char 
19310 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 2c 20 20 20  ***pazResult,   
19320 20 2f 2a 20 52 65 73 75 6c 74 73 20 6f 66 20 74   /* Results of t
19330 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 69 6e  he query */.  in
19340 74 20 2a 70 6e 52 6f 77 2c 20 20 20 20 20 20 20  t *pnRow,       
19350 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
19360 20 72 65 73 75 6c 74 20 72 6f 77 73 20 77 72 69   result rows wri
19370 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69  tten here */.  i
19380 6e 74 20 2a 70 6e 43 6f 6c 75 6d 6e 2c 20 20 20  nt *pnColumn,   
19390 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
193a0 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
193b0 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
193c0 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72 6d  .  char **pzErrm
193d0 73 67 20 20 20 20 20 20 20 2f 2a 20 45 72 72 6f  sg       /* Erro
193e0 72 20 6d 73 67 20 77 72 69 74 74 65 6e 20 68 65  r msg written he
193f0 72 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f  re */.);.SQLITE_
19400 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
19410 5f 66 72 65 65 5f 74 61 62 6c 65 28 63 68 61 72  _free_table(char
19420 20 2a 2a 72 65 73 75 6c 74 29 3b 0a 0a 2f 2a 0a   **result);../*.
19430 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 6f 72  ** CAPI3REF: For
19440 6d 61 74 74 65 64 20 53 74 72 69 6e 67 20 50 72  matted String Pr
19450 69 6e 74 69 6e 67 20 46 75 6e 63 74 69 6f 6e 73  inting Functions
19460 20 7b 48 31 37 34 30 30 7d 20 3c 53 37 30 30 30   {H17400} <S7000
19470 30 3e 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a  0><S20000>.**.**
19480 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
19490 61 72 65 20 77 6f 72 6b 61 6c 69 6b 65 73 20 6f  are workalikes o
194a0 66 20 74 68 65 20 22 70 72 69 6e 74 66 28 29 22  f the "printf()"
194b0 20 66 61 6d 69 6c 79 20 6f 66 20 66 75 6e 63 74   family of funct
194c0 69 6f 6e 73 0a 2a 2a 20 66 72 6f 6d 20 74 68 65  ions.** from the
194d0 20 73 74 61 6e 64 61 72 64 20 43 20 6c 69 62 72   standard C libr
194e0 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ary..**.** The s
194f0 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
19500 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 6d 70   and sqlite3_vmp
19510 72 69 6e 74 66 28 29 20 72 6f 75 74 69 6e 65 73  rintf() routines
19520 20 77 72 69 74 65 20 74 68 65 69 72 0a 2a 2a 20   write their.** 
19530 72 65 73 75 6c 74 73 20 69 6e 74 6f 20 6d 65 6d  results into mem
19540 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f  ory obtained fro
19550 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  m [sqlite3_mallo
19560 63 28 29 5d 2e 0a 2a 2a 20 54 68 65 20 73 74 72  c()]..** The str
19570 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62 79  ings returned by
19580 20 74 68 65 73 65 20 74 77 6f 20 72 6f 75 74 69   these two routi
19590 6e 65 73 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a  nes should be.**
195a0 20 72 65 6c 65 61 73 65 64 20 62 79 20 5b 73 71   released by [sq
195b0 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 2e 20 20  lite3_free()].  
195c0 42 6f 74 68 20 72 6f 75 74 69 6e 65 73 20 72 65  Both routines re
195d0 74 75 72 6e 20 61 0a 2a 2a 20 4e 55 4c 4c 20 70  turn a.** NULL p
195e0 6f 69 6e 74 65 72 20 69 66 20 5b 73 71 6c 69 74  ointer if [sqlit
195f0 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 69 73 20  e3_malloc()] is 
19600 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61  unable to alloca
19610 74 65 20 65 6e 6f 75 67 68 0a 2a 2a 20 6d 65 6d  te enough.** mem
19620 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ory to hold the 
19630 72 65 73 75 6c 74 69 6e 67 20 73 74 72 69 6e 67  resulting string
19640 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 73 71 6c 69 74  ..**.** In sqlit
19650 65 33 5f 73 6e 70 72 69 6e 74 66 28 29 20 72 6f  e3_snprintf() ro
19660 75 74 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72  utine is similar
19670 20 74 6f 20 22 73 6e 70 72 69 6e 74 66 28 29 22   to "snprintf()"
19680 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 73 74 61   from.** the sta
19690 6e 64 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e  ndard C library.
196a0 20 20 54 68 65 20 72 65 73 75 6c 74 20 69 73 20    The result is 
196b0 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
196c0 0a 2a 2a 20 62 75 66 66 65 72 20 73 75 70 70 6c  .** buffer suppl
196d0 69 65 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e  ied as the secon
196e0 64 20 70 61 72 61 6d 65 74 65 72 20 77 68 6f 73  d parameter whos
196f0 65 20 73 69 7a 65 20 69 73 20 67 69 76 65 6e 20  e size is given 
19700 62 79 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20  by.** the first 
19710 70 61 72 61 6d 65 74 65 72 2e 20 4e 6f 74 65 20  parameter. Note 
19720 74 68 61 74 20 74 68 65 20 6f 72 64 65 72 20 6f  that the order o
19730 66 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 74  f the.** first t
19740 77 6f 20 70 61 72 61 6d 65 74 65 72 73 20 69 73  wo parameters is
19750 20 72 65 76 65 72 73 65 64 20 66 72 6f 6d 20 73   reversed from s
19760 6e 70 72 69 6e 74 66 28 29 2e 20 20 54 68 69 73  nprintf().  This
19770 20 69 73 20 61 6e 0a 2a 2a 20 68 69 73 74 6f 72   is an.** histor
19780 69 63 61 6c 20 61 63 63 69 64 65 6e 74 20 74 68  ical accident th
19790 61 74 20 63 61 6e 6e 6f 74 20 62 65 20 66 69 78  at cannot be fix
197a0 65 64 20 77 69 74 68 6f 75 74 20 62 72 65 61 6b  ed without break
197b0 69 6e 67 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  ing.** backwards
197c0 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 20   compatibility. 
197d0 20 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20   Note also that 
197e0 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
197f0 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 20  ().** returns a 
19800 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 73 20 62  pointer to its b
19810 75 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66  uffer instead of
19820 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
19830 2a 20 63 68 61 72 61 63 74 65 72 73 20 61 63 74  * characters act
19840 75 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e  ually written in
19850 74 6f 20 74 68 65 20 62 75 66 66 65 72 2e 20 20  to the buffer.  
19860 57 65 20 61 64 6d 69 74 20 74 68 61 74 0a 2a 2a  We admit that.**
19870 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
19880 68 61 72 61 63 74 65 72 73 20 77 72 69 74 74 65  haracters writte
19890 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d 6f 72  n would be a mor
198a0 65 20 75 73 65 66 75 6c 20 72 65 74 75 72 6e 0a  e useful return.
198b0 2a 2a 20 76 61 6c 75 65 20 62 75 74 20 77 65 20  ** value but we 
198c0 63 61 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  cannot change th
198d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
198e0 20 6f 66 20 73 71 6c 69 74 65 33 5f 73 6e 70 72   of sqlite3_snpr
198f0 69 6e 74 66 28 29 0a 2a 2a 20 6e 6f 77 20 77 69  intf().** now wi
19900 74 68 6f 75 74 20 62 72 65 61 6b 69 6e 67 20 63  thout breaking c
19910 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e 0a 2a 2a  ompatibility..**
19920 0a 2a 2a 20 41 73 20 6c 6f 6e 67 20 61 73 20 74  .** As long as t
19930 68 65 20 62 75 66 66 65 72 20 73 69 7a 65 20 69  he buffer size i
19940 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a  s greater than z
19950 65 72 6f 2c 20 73 71 6c 69 74 65 33 5f 73 6e 70  ero, sqlite3_snp
19960 72 69 6e 74 66 28 29 0a 2a 2a 20 67 75 61 72 61  rintf().** guara
19970 6e 74 65 65 73 20 74 68 61 74 20 74 68 65 20 62  ntees that the b
19980 75 66 66 65 72 20 69 73 20 61 6c 77 61 79 73 20  uffer is always 
19990 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 2e  zero-terminated.
199a0 20 20 54 68 65 20 66 69 72 73 74 0a 2a 2a 20 70    The first.** p
199b0 61 72 61 6d 65 74 65 72 20 22 6e 22 20 69 73 20  arameter "n" is 
199c0 74 68 65 20 74 6f 74 61 6c 20 73 69 7a 65 20 6f  the total size o
199d0 66 20 74 68 65 20 62 75 66 66 65 72 2c 20 69 6e  f the buffer, in
199e0 63 6c 75 64 69 6e 67 20 73 70 61 63 65 20 66 6f  cluding space fo
199f0 72 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 20 74 65  r.** the zero te
19a00 72 6d 69 6e 61 74 6f 72 2e 20 20 53 6f 20 74 68  rminator.  So th
19a10 65 20 6c 6f 6e 67 65 73 74 20 73 74 72 69 6e 67  e longest string
19a20 20 74 68 61 74 20 63 61 6e 20 62 65 20 63 6f 6d   that can be com
19a30 70 6c 65 74 65 6c 79 0a 2a 2a 20 77 72 69 74 74  pletely.** writt
19a40 65 6e 20 77 69 6c 6c 20 62 65 20 6e 2d 31 20 63  en will be n-1 c
19a50 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a  haracters..**.**
19a60 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
19a70 61 6c 6c 20 69 6d 70 6c 65 6d 65 6e 74 20 73 6f  all implement so
19a80 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 66 6f  me additional fo
19a90 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 6f 70 74 69  rmatting.** opti
19aa0 6f 6e 73 20 74 68 61 74 20 61 72 65 20 75 73 65  ons that are use
19ab0 66 75 6c 20 66 6f 72 20 63 6f 6e 73 74 72 75 63  ful for construc
19ac0 74 69 6e 67 20 53 51 4c 20 73 74 61 74 65 6d 65  ting SQL stateme
19ad0 6e 74 73 2e 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74  nts..** All of t
19ae0 68 65 20 75 73 75 61 6c 20 70 72 69 6e 74 66 28  he usual printf(
19af0 29 20 66 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74  ) formatting opt
19b00 69 6f 6e 73 20 61 70 70 6c 79 2e 20 20 49 6e 20  ions apply.  In 
19b10 61 64 64 69 74 69 6f 6e 2c 20 74 68 65 72 65 0a  addition, there.
19b20 2a 2a 20 69 73 20 61 72 65 20 22 25 71 22 2c 20  ** is are "%q", 
19b30 22 25 51 22 2c 20 61 6e 64 20 22 25 7a 22 20 6f  "%Q", and "%z" o
19b40 70 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  ptions..**.** Th
19b50 65 20 25 71 20 6f 70 74 69 6f 6e 20 77 6f 72 6b  e %q option work
19b60 73 20 6c 69 6b 65 20 25 73 20 69 6e 20 74 68 61  s like %s in tha
19b70 74 20 69 74 20 73 75 62 73 74 69 74 75 74 65 73  t it substitutes
19b80 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74   a null-terminat
19b90 65 64 0a 2a 2a 20 73 74 72 69 6e 67 20 66 72 6f  ed.** string fro
19ba0 6d 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20 6c  m the argument l
19bb0 69 73 74 2e 20 20 42 75 74 20 25 71 20 61 6c 73  ist.  But %q als
19bc0 6f 20 64 6f 75 62 6c 65 73 20 65 76 65 72 79 20  o doubles every 
19bd0 27 5c 27 27 20 63 68 61 72 61 63 74 65 72 2e 0a  '\'' character..
19be0 2a 2a 20 25 71 20 69 73 20 64 65 73 69 67 6e 65  ** %q is designe
19bf0 64 20 66 6f 72 20 75 73 65 20 69 6e 73 69 64 65  d for use inside
19c00 20 61 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61   a string litera
19c10 6c 2e 20 20 42 79 20 64 6f 75 62 6c 69 6e 67 20  l.  By doubling 
19c20 65 61 63 68 20 27 5c 27 27 0a 2a 2a 20 63 68 61  each '\''.** cha
19c30 72 61 63 74 65 72 20 69 74 20 65 73 63 61 70 65  racter it escape
19c40 73 20 74 68 61 74 20 63 68 61 72 61 63 74 65 72  s that character
19c50 20 61 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74   and allows it t
19c60 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
19c70 74 6f 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  to.** the string
19c80 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d  ..**.** For exam
19c90 70 6c 65 2c 20 61 73 73 75 6d 65 20 74 68 65 20  ple, assume the 
19ca0 73 74 72 69 6e 67 20 76 61 72 69 61 62 6c 65 20  string variable 
19cb0 7a 54 65 78 74 20 63 6f 6e 74 61 69 6e 73 20 74  zText contains t
19cc0 65 78 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  ext as follows:.
19cd0 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
19ce0 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68 61 72  e><pre>.**  char
19cf0 20 2a 7a 54 65 78 74 20 3d 20 22 49 74 27 73 20   *zText = "It's 
19d00 61 20 68 61 70 70 79 20 64 61 79 21 22 3b 0a 2a  a happy day!";.*
19d10 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71  * </pre></blockq
19d20 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 4f 6e 65 20  uote>.**.** One 
19d30 63 61 6e 20 75 73 65 20 74 68 69 73 20 74 65 78  can use this tex
19d40 74 20 69 6e 20 61 6e 20 53 51 4c 20 73 74 61 74  t in an SQL stat
19d50 65 6d 65 6e 74 20 61 73 20 66 6f 6c 6c 6f 77 73  ement as follows
19d60 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75  :.**.** <blockqu
19d70 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20 63 68  ote><pre>.**  ch
19d80 61 72 20 2a 7a 53 51 4c 20 3d 20 73 71 6c 69 74  ar *zSQL = sqlit
19d90 65 33 5f 6d 70 72 69 6e 74 66 28 22 49 4e 53 45  e3_mprintf("INSE
19da0 52 54 20 49 4e 54 4f 20 74 61 62 6c 65 20 56 41  RT INTO table VA
19db0 4c 55 45 53 28 27 25 71 27 29 22 2c 20 7a 54 65  LUES('%q')", zTe
19dc0 78 74 29 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33  xt);.**  sqlite3
19dd0 5f 65 78 65 63 28 64 62 2c 20 7a 53 51 4c 2c 20  _exec(db, zSQL, 
19de0 30 2c 20 30 2c 20 30 29 3b 0a 2a 2a 20 20 73 71  0, 0, 0);.**  sq
19df0 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 51 4c 29  lite3_free(zSQL)
19e00 3b 0a 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f  ;.** </pre></blo
19e10 63 6b 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 42  ckquote>.**.** B
19e20 65 63 61 75 73 65 20 74 68 65 20 25 71 20 66 6f  ecause the %q fo
19e30 72 6d 61 74 20 73 74 72 69 6e 67 20 69 73 20 75  rmat string is u
19e40 73 65 64 2c 20 74 68 65 20 27 5c 27 27 20 63 68  sed, the '\'' ch
19e50 61 72 61 63 74 65 72 20 69 6e 20 7a 54 65 78 74  aracter in zText
19e60 0a 2a 2a 20 69 73 20 65 73 63 61 70 65 64 20 61  .** is escaped a
19e70 6e 64 20 74 68 65 20 53 51 4c 20 67 65 6e 65 72  nd the SQL gener
19e80 61 74 65 64 20 69 73 20 61 73 20 66 6f 6c 6c 6f  ated is as follo
19e90 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b  ws:.**.** <block
19ea0 71 75 6f 74 65 3e 3c 70 72 65 3e 0a 2a 2a 20 20  quote><pre>.**  
19eb0 49 4e 53 45 52 54 20 49 4e 54 4f 20 74 61 62 6c  INSERT INTO tabl
19ec0 65 31 20 56 41 4c 55 45 53 28 27 49 74 27 27 73  e1 VALUES('It''s
19ed0 20 61 20 68 61 70 70 79 20 64 61 79 21 27 29 0a   a happy day!').
19ee0 2a 2a 20 3c 2f 70 72 65 3e 3c 2f 62 6c 6f 63 6b  ** </pre></block
19ef0 71 75 6f 74 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  quote>.**.** Thi
19f00 73 20 69 73 20 63 6f 72 72 65 63 74 2e 20 20 48  s is correct.  H
19f10 61 64 20 77 65 20 75 73 65 64 20 25 73 20 69 6e  ad we used %s in
19f20 73 74 65 61 64 20 6f 66 20 25 71 2c 20 74 68 65  stead of %q, the
19f30 20 67 65 6e 65 72 61 74 65 64 20 53 51 4c 0a 2a   generated SQL.*
19f40 2a 20 77 6f 75 6c 64 20 68 61 76 65 20 6c 6f 6f  * would have loo
19f50 6b 65 64 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ked like this:.*
19f60 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65  *.** <blockquote
19f70 3e 3c 70 72 65 3e 0a 2a 2a 20 20 49 4e 53 45 52  ><pre>.**  INSER
19f80 54 20 49 4e 54 4f 20 74 61 62 6c 65 31 20 56 41  T INTO table1 VA
19f90 4c 55 45 53 28 27 49 74 27 73 20 61 20 68 61 70  LUES('It's a hap
19fa0 70 79 20 64 61 79 21 27 29 3b 0a 2a 2a 20 3c 2f  py day!');.** </
19fb0 70 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65  pre></blockquote
19fc0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 65 63  >.**.** This sec
19fd0 6f 6e 64 20 65 78 61 6d 70 6c 65 20 69 73 20 61  ond example is a
19fe0 6e 20 53 51 4c 20 73 79 6e 74 61 78 20 65 72 72  n SQL syntax err
19ff0 6f 72 2e 20 20 41 73 20 61 20 67 65 6e 65 72 61  or.  As a genera
1a000 6c 20 72 75 6c 65 20 79 6f 75 20 73 68 6f 75 6c  l rule you shoul
1a010 64 0a 2a 2a 20 61 6c 77 61 79 73 20 75 73 65 20  d.** always use 
1a020 25 71 20 69 6e 73 74 65 61 64 20 6f 66 20 25 73  %q instead of %s
1a030 20 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20   when inserting 
1a040 74 65 78 74 20 69 6e 74 6f 20 61 20 73 74 72 69  text into a stri
1a050 6e 67 20 6c 69 74 65 72 61 6c 2e 0a 2a 2a 0a 2a  ng literal..**.*
1a060 2a 20 54 68 65 20 25 51 20 6f 70 74 69 6f 6e 20  * The %Q option 
1a070 77 6f 72 6b 73 20 6c 69 6b 65 20 25 71 20 65 78  works like %q ex
1a080 63 65 70 74 20 69 74 20 61 6c 73 6f 20 61 64 64  cept it also add
1a090 73 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 20  s single quotes 
1a0a0 61 72 6f 75 6e 64 0a 2a 2a 20 74 68 65 20 6f 75  around.** the ou
1a0b0 74 73 69 64 65 20 6f 66 20 74 68 65 20 74 6f 74  tside of the tot
1a0c0 61 6c 20 73 74 72 69 6e 67 2e 20 20 41 64 64 69  al string.  Addi
1a0d0 74 69 6f 6e 61 6c 6c 79 2c 20 69 66 20 74 68 65  tionally, if the
1a0e0 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74 68   parameter in th
1a0f0 65 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 6c 69  e.** argument li
1a100 73 74 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  st is a NULL poi
1a110 6e 74 65 72 2c 20 25 51 20 73 75 62 73 74 69 74  nter, %Q substit
1a120 75 74 65 73 20 74 68 65 20 74 65 78 74 20 22 4e  utes the text "N
1a130 55 4c 4c 22 20 28 77 69 74 68 6f 75 74 0a 2a 2a  ULL" (without.**
1a140 20 73 69 6e 67 6c 65 20 71 75 6f 74 65 73 29 20   single quotes) 
1a150 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65 20  in place of the 
1a160 25 51 20 6f 70 74 69 6f 6e 2e 20 20 53 6f 2c 20  %Q option.  So, 
1a170 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 6f 6e 65  for example, one
1a180 20 63 6f 75 6c 64 20 73 61 79 3a 0a 2a 2a 0a 2a   could say:.**.*
1a190 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74 65 3e 3c 70  * <blockquote><p
1a1a0 72 65 3e 0a 2a 2a 20 20 63 68 61 72 20 2a 7a 53  re>.**  char *zS
1a1b0 51 4c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  QL = sqlite3_mpr
1a1c0 69 6e 74 66 28 22 49 4e 53 45 52 54 20 49 4e 54  intf("INSERT INT
1a1d0 4f 20 74 61 62 6c 65 20 56 41 4c 55 45 53 28 25  O table VALUES(%
1a1e0 51 29 22 2c 20 7a 54 65 78 74 29 3b 0a 2a 2a 20  Q)", zText);.** 
1a1f0 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62   sqlite3_exec(db
1a200 2c 20 7a 53 51 4c 2c 20 30 2c 20 30 2c 20 30 29  , zSQL, 0, 0, 0)
1a210 3b 0a 2a 2a 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.**  sqlite3_fr
1a220 65 65 28 7a 53 51 4c 29 3b 0a 2a 2a 20 3c 2f 70  ee(zSQL);.** </p
1a230 72 65 3e 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e  re></blockquote>
1a240 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20  .**.** The code 
1a250 61 62 6f 76 65 20 77 69 6c 6c 20 72 65 6e 64 65  above will rende
1a260 72 20 61 20 63 6f 72 72 65 63 74 20 53 51 4c 20  r a correct SQL 
1a270 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
1a280 20 7a 53 51 4c 0a 2a 2a 20 76 61 72 69 61 62 6c   zSQL.** variabl
1a290 65 20 65 76 65 6e 20 69 66 20 74 68 65 20 7a 54  e even if the zT
1a2a0 65 78 74 20 76 61 72 69 61 62 6c 65 20 69 73 20  ext variable is 
1a2b0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
1a2c0 2a 2a 0a 2a 2a 20 54 68 65 20 22 25 7a 22 20 66  **.** The "%z" f
1a2d0 6f 72 6d 61 74 74 69 6e 67 20 6f 70 74 69 6f 6e  ormatting option
1a2e0 20 77 6f 72 6b 73 20 65 78 61 63 74 6c 79 20 6c   works exactly l
1a2f0 69 6b 65 20 22 25 73 22 20 77 69 74 68 20 74 68  ike "%s" with th
1a300 65 0a 2a 2a 20 61 64 64 69 74 69 6f 6e 20 74 68  e.** addition th
1a310 61 74 20 61 66 74 65 72 20 74 68 65 20 73 74 72  at after the str
1a320 69 6e 67 20 68 61 73 20 62 65 65 6e 20 72 65 61  ing has been rea
1a330 64 20 61 6e 64 20 63 6f 70 69 65 64 20 69 6e 74  d and copied int
1a340 6f 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c  o.** the result,
1a350 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29   [sqlite3_free()
1a360 5d 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74  ] is called on t
1a370 68 65 20 69 6e 70 75 74 20 73 74 72 69 6e 67 2e  he input string.
1a380 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 49 4e 56   {END}.**.** INV
1a390 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
1a3a0 48 31 37 34 30 33 7d 20 20 54 68 65 20 5b 73 71  H17403}  The [sq
1a3b0 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29 5d  lite3_mprintf()]
1a3c0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 76 6d   and [sqlite3_vm
1a3d0 70 72 69 6e 74 66 28 29 5d 20 69 6e 74 65 72 66  printf()] interf
1a3e0 61 63 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  aces.**         
1a3f0 20 20 72 65 74 75 72 6e 20 65 69 74 68 65 72 20    return either 
1a400 70 6f 69 6e 74 65 72 73 20 74 6f 20 7a 65 72 6f  pointers to zero
1a410 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
1a420 38 20 73 74 72 69 6e 67 73 20 68 65 6c 64 20 69  8 strings held i
1a430 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6d  n.**           m
1a440 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
1a450 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  rom [sqlite3_mal
1a460 6c 6f 63 28 29 5d 20 6f 72 20 4e 55 4c 4c 20 70  loc()] or NULL p
1a470 6f 69 6e 74 65 72 73 20 69 66 0a 2a 2a 20 20 20  ointers if.**   
1a480 20 20 20 20 20 20 20 20 61 20 63 61 6c 6c 20 74          a call t
1a490 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  o [sqlite3_mallo
1a4a0 63 28 29 5d 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a  c()] fails..**.*
1a4b0 2a 20 7b 48 31 37 34 30 36 7d 20 20 54 68 65 20  * {H17406}  The 
1a4c0 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74  [sqlite3_snprint
1a4d0 66 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 77  f()] interface w
1a4e0 72 69 74 65 73 20 61 20 7a 65 72 6f 2d 74 65 72  rites a zero-ter
1a4f0 6d 69 6e 61 74 65 64 0a 2a 2a 20 20 20 20 20 20  minated.**      
1a500 20 20 20 20 20 55 54 46 2d 38 20 73 74 72 69 6e       UTF-8 strin
1a510 67 20 69 6e 74 6f 20 74 68 65 20 62 75 66 66 65  g into the buffe
1a520 72 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  r pointed to by 
1a530 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
1a540 65 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  eter.**         
1a550 20 20 70 72 6f 76 69 64 65 64 20 74 68 61 74 20    provided that 
1a560 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
1a570 74 65 72 20 69 73 20 67 72 65 61 74 65 72 20 74  ter is greater t
1a580 68 61 6e 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20  han zero..**.** 
1a590 7b 48 31 37 34 30 37 7d 20 20 54 68 65 20 5b 73  {H17407}  The [s
1a5a0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1a5b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f 65  )] interface doe
1a5c0 73 20 6e 6f 74 20 77 72 69 74 65 20 73 6c 6f 74  s not write slot
1a5d0 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  s of.**         
1a5e0 20 20 69 74 73 20 6f 75 74 70 75 74 20 62 75 66    its output buf
1a5f0 66 65 72 20 28 74 68 65 20 73 65 63 6f 6e 64 20  fer (the second 
1a600 70 61 72 61 6d 65 74 65 72 29 20 6f 75 74 73 69  parameter) outsi
1a610 64 65 20 74 68 65 20 72 61 6e 67 65 0a 2a 2a 20  de the range.** 
1a620 20 20 20 20 20 20 20 20 20 20 6f 66 20 30 20 74            of 0 t
1a630 68 72 6f 75 67 68 20 4e 2d 31 20 28 77 68 65 72  hrough N-1 (wher
1a640 65 20 4e 20 69 73 20 74 68 65 20 66 69 72 73 74  e N is the first
1a650 20 70 61 72 61 6d 65 74 65 72 29 0a 2a 2a 20 20   parameter).**  
1a660 20 20 20 20 20 20 20 20 20 72 65 67 61 72 64 6c           regardl
1a670 65 73 73 20 6f 66 20 74 68 65 20 6c 65 6e 67 74  ess of the lengt
1a680 68 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 0a  h of the string.
1a690 2a 2a 20 20 20 20 20 20 20 20 20 20 20 72 65 71  **           req
1a6a0 75 65 73 74 65 64 20 62 79 20 74 68 65 20 66 6f  uested by the fo
1a6b0 72 6d 61 74 20 73 70 65 63 69 66 69 63 61 74 69  rmat specificati
1a6c0 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  on..*/.SQLITE_AP
1a6d0 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
1a6e0 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  mprintf(const ch
1a6f0 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45  ar*,...);.SQLITE
1a700 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69 74  _API char *sqlit
1a710 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e 73  e3_vmprintf(cons
1a720 74 20 63 68 61 72 2a 2c 20 76 61 5f 6c 69 73 74  t char*, va_list
1a730 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68  );.SQLITE_API ch
1a740 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 6e 70 72  ar *sqlite3_snpr
1a750 69 6e 74 66 28 69 6e 74 2c 63 68 61 72 2a 2c 63  intf(int,char*,c
1a760 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
1a770 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
1a780 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61  F: Memory Alloca
1a790 74 69 6f 6e 20 53 75 62 73 79 73 74 65 6d 20 7b  tion Subsystem {
1a7a0 48 31 37 33 30 30 7d 20 3c 53 32 30 30 30 30 3e  H17300} <S20000>
1a7b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74  .**.** The SQLit
1a7c0 65 20 63 6f 72 65 20 20 75 73 65 73 20 74 68 65  e core  uses the
1a7d0 73 65 20 74 68 72 65 65 20 72 6f 75 74 69 6e 65  se three routine
1a7e0 73 20 66 6f 72 20 61 6c 6c 20 6f 66 20 69 74 73  s for all of its
1a7f0 20 6f 77 6e 0a 2a 2a 20 69 6e 74 65 72 6e 61 6c   own.** internal
1a800 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
1a810 6f 6e 20 6e 65 65 64 73 2e 20 22 43 6f 72 65 22  on needs. "Core"
1a820 20 69 6e 20 74 68 65 20 70 72 65 76 69 6f 75 73   in the previous
1a830 20 73 65 6e 74 65 6e 63 65 0a 2a 2a 20 64 6f 65   sentence.** doe
1a840 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 6f 70  s not include op
1a850 65 72 61 74 69 6e 67 2d 73 79 73 74 65 6d 20 73  erating-system s
1a860 70 65 63 69 66 69 63 20 56 46 53 20 69 6d 70 6c  pecific VFS impl
1a870 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 20 54 68 65  ementation.  The
1a880 0a 2a 2a 20 57 69 6e 64 6f 77 73 20 56 46 53 20  .** Windows VFS 
1a890 75 73 65 73 20 6e 61 74 69 76 65 20 6d 61 6c 6c  uses native mall
1a8a0 6f 63 28 29 20 61 6e 64 20 66 72 65 65 28 29 20  oc() and free() 
1a8b0 66 6f 72 20 73 6f 6d 65 20 6f 70 65 72 61 74 69  for some operati
1a8c0 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ons..**.** The s
1a8d0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
1a8e0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
1a8f0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 62  a pointer to a b
1a900 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 6d 65 6d 6f 72  lock.** of memor
1a910 79 20 61 74 20 6c 65 61 73 74 20 4e 20 62 79 74  y at least N byt
1a920 65 73 20 69 6e 20 6c 65 6e 67 74 68 2c 20 77 68  es in length, wh
1a930 65 72 65 20 4e 20 69 73 20 74 68 65 20 70 61 72  ere N is the par
1a940 61 6d 65 74 65 72 2e 0a 2a 2a 20 49 66 20 73 71  ameter..** If sq
1a950 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 69  lite3_malloc() i
1a960 73 20 75 6e 61 62 6c 65 20 74 6f 20 6f 62 74 61  s unable to obta
1a970 69 6e 20 73 75 66 66 69 63 69 65 6e 74 20 66 72  in sufficient fr
1a980 65 65 0a 2a 2a 20 6d 65 6d 6f 72 79 2c 20 69 74  ee.** memory, it
1a990 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20   returns a NULL 
1a9a0 70 6f 69 6e 74 65 72 2e 20 20 49 66 20 74 68 65  pointer.  If the
1a9b0 20 70 61 72 61 6d 65 74 65 72 20 4e 20 74 6f 0a   parameter N to.
1a9c0 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  ** sqlite3_mallo
1a9d0 63 28 29 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e  c() is zero or n
1a9e0 65 67 61 74 69 76 65 20 74 68 65 6e 20 73 71 6c  egative then sql
1a9f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 72 65  ite3_malloc() re
1aa00 74 75 72 6e 73 0a 2a 2a 20 61 20 4e 55 4c 4c 20  turns.** a NULL 
1aa10 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 43  pointer..**.** C
1aa20 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 66  alling sqlite3_f
1aa30 72 65 65 28 29 20 77 69 74 68 20 61 20 70 6f 69  ree() with a poi
1aa40 6e 74 65 72 20 70 72 65 76 69 6f 75 73 6c 79 20  nter previously 
1aa50 72 65 74 75 72 6e 65 64 0a 2a 2a 20 62 79 20 73  returned.** by s
1aa60 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20  qlite3_malloc() 
1aa70 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  or sqlite3_reall
1aa80 6f 63 28 29 20 72 65 6c 65 61 73 65 73 20 74 68  oc() releases th
1aa90 61 74 20 6d 65 6d 6f 72 79 20 73 6f 0a 2a 2a 20  at memory so.** 
1aaa0 74 68 61 74 20 69 74 20 6d 69 67 68 74 20 62 65  that it might be
1aab0 20 72 65 75 73 65 64 2e 20 20 54 68 65 20 73 71   reused.  The sq
1aac0 6c 69 74 65 33 5f 66 72 65 65 28 29 20 72 6f 75  lite3_free() rou
1aad0 74 69 6e 65 20 69 73 0a 2a 2a 20 61 20 6e 6f 2d  tine is.** a no-
1aae0 6f 70 20 69 66 20 69 73 20 63 61 6c 6c 65 64 20  op if is called 
1aaf0 77 69 74 68 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  with a NULL poin
1ab00 74 65 72 2e 20 20 50 61 73 73 69 6e 67 20 61 20  ter.  Passing a 
1ab10 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  NULL pointer.** 
1ab20 74 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  to sqlite3_free(
1ab30 29 20 69 73 20 68 61 72 6d 6c 65 73 73 2e 20 20  ) is harmless.  
1ab40 41 66 74 65 72 20 62 65 69 6e 67 20 66 72 65 65  After being free
1ab50 64 2c 20 6d 65 6d 6f 72 79 0a 2a 2a 20 73 68 6f  d, memory.** sho
1ab60 75 6c 64 20 6e 65 69 74 68 65 72 20 62 65 20 72  uld neither be r
1ab70 65 61 64 20 6e 6f 72 20 77 72 69 74 74 65 6e 2e  ead nor written.
1ab80 20 20 45 76 65 6e 20 72 65 61 64 69 6e 67 20 70    Even reading p
1ab90 72 65 76 69 6f 75 73 6c 79 20 66 72 65 65 64 0a  reviously freed.
1aba0 2a 2a 20 6d 65 6d 6f 72 79 20 6d 69 67 68 74 20  ** memory might 
1abb0 72 65 73 75 6c 74 20 69 6e 20 61 20 73 65 67 6d  result in a segm
1abc0 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74 20 6f  entation fault o
1abd0 72 20 6f 74 68 65 72 20 73 65 76 65 72 65 20 65  r other severe e
1abe0 72 72 6f 72 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20  rror..** Memory 
1abf0 63 6f 72 72 75 70 74 69 6f 6e 2c 20 61 20 73 65  corruption, a se
1ac00 67 6d 65 6e 74 61 74 69 6f 6e 20 66 61 75 6c 74  gmentation fault
1ac10 2c 20 6f 72 20 6f 74 68 65 72 20 73 65 76 65 72  , or other sever
1ac20 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 69 67 68 74  e error.** might
1ac30 20 72 65 73 75 6c 74 20 69 66 20 73 71 6c 69 74   result if sqlit
1ac40 65 33 5f 66 72 65 65 28 29 20 69 73 20 63 61 6c  e3_free() is cal
1ac50 6c 65 64 20 77 69 74 68 20 61 20 6e 6f 6e 2d 4e  led with a non-N
1ac60 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 61 74  ULL pointer that
1ac70 0a 2a 2a 20 77 61 73 20 6e 6f 74 20 6f 62 74 61  .** was not obta
1ac80 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1ac90 33 5f 6d 61 6c 6c 6f 63 28 29 20 6f 72 20 73 71  3_malloc() or sq
1aca0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 2e  lite3_realloc().
1acb0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
1acc0 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 69 6e 74  e3_realloc() int
1acd0 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
1ace0 74 6f 20 72 65 73 69 7a 65 20 61 0a 2a 2a 20 70  to resize a.** p
1acf0 72 69 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  rior memory allo
1ad00 63 61 74 69 6f 6e 20 74 6f 20 62 65 20 61 74 20  cation to be at 
1ad10 6c 65 61 73 74 20 4e 20 62 79 74 65 73 2c 20 77  least N bytes, w
1ad20 68 65 72 65 20 4e 20 69 73 20 74 68 65 0a 2a 2a  here N is the.**
1ad30 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65   second paramete
1ad40 72 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 61  r.  The memory a
1ad50 6c 6c 6f 63 61 74 69 6f 6e 20 74 6f 20 62 65 20  llocation to be 
1ad60 72 65 73 69 7a 65 64 20 69 73 20 74 68 65 20 66  resized is the f
1ad70 69 72 73 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65  irst.** paramete
1ad80 72 2e 20 20 49 66 20 74 68 65 20 66 69 72 73 74  r.  If the first
1ad90 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71   parameter to sq
1ada0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 0a  lite3_realloc().
1adb0 2a 2a 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  ** is a NULL poi
1adc0 6e 74 65 72 20 74 68 65 6e 20 69 74 73 20 62 65  nter then its be
1add0 68 61 76 69 6f 72 20 69 73 20 69 64 65 6e 74 69  havior is identi
1ade0 63 61 6c 20 74 6f 20 63 61 6c 6c 69 6e 67 0a 2a  cal to calling.*
1adf0 2a 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  * sqlite3_malloc
1ae00 28 4e 29 20 77 68 65 72 65 20 4e 20 69 73 20 74  (N) where N is t
1ae10 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
1ae20 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
1ae30 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 49 66 20  ealloc()..** If 
1ae40 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  the second param
1ae50 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
1ae60 72 65 61 6c 6c 6f 63 28 29 20 69 73 20 7a 65 72  realloc() is zer
1ae70 6f 20 6f 72 0a 2a 2a 20 6e 65 67 61 74 69 76 65  o or.** negative
1ae80 20 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69   then the behavi
1ae90 6f 72 20 69 73 20 65 78 61 63 74 6c 79 20 74 68  or is exactly th
1aea0 65 20 73 61 6d 65 20 61 73 20 63 61 6c 6c 69 6e  e same as callin
1aeb0 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 66 72 65  g.** sqlite3_fre
1aec0 65 28 50 29 20 77 68 65 72 65 20 50 20 69 73 20  e(P) where P is 
1aed0 74 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  the first parame
1aee0 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 72  ter to sqlite3_r
1aef0 65 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 20 73 71 6c  ealloc()..** sql
1af00 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 20 72  ite3_realloc() r
1af10 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
1af20 20 74 6f 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c   to a memory all
1af30 6f 63 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20 61 74  ocation.** of at
1af40 20 6c 65 61 73 74 20 4e 20 62 79 74 65 73 20 69   least N bytes i
1af50 6e 20 73 69 7a 65 20 6f 72 20 4e 55 4c 4c 20 69  n size or NULL i
1af60 66 20 73 75 66 66 69 63 69 65 6e 74 20 6d 65 6d  f sufficient mem
1af70 6f 72 79 20 69 73 20 75 6e 61 76 61 69 6c 61 62  ory is unavailab
1af80 6c 65 2e 0a 2a 2a 20 49 66 20 4d 20 69 73 20 74  le..** If M is t
1af90 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  he size of the p
1afa0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  rior allocation,
1afb0 20 74 68 65 6e 20 6d 69 6e 28 4e 2c 4d 29 20 62   then min(N,M) b
1afc0 79 74 65 73 0a 2a 2a 20 6f 66 20 74 68 65 20 70  ytes.** of the p
1afd0 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  rior allocation 
1afe0 61 72 65 20 63 6f 70 69 65 64 20 69 6e 74 6f 20  are copied into 
1aff0 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
1b000 20 62 75 66 66 65 72 20 72 65 74 75 72 6e 65 64   buffer returned
1b010 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 33 5f 72  .** by sqlite3_r
1b020 65 61 6c 6c 6f 63 28 29 20 61 6e 64 20 74 68 65  ealloc() and the
1b030 20 70 72 69 6f 72 20 61 6c 6c 6f 63 61 74 69 6f   prior allocatio
1b040 6e 20 69 73 20 66 72 65 65 64 2e 0a 2a 2a 20 49  n is freed..** I
1b050 66 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  f sqlite3_reallo
1b060 63 28 29 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c  c() returns NULL
1b070 2c 20 74 68 65 6e 20 74 68 65 20 70 72 69 6f 72  , then the prior
1b080 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 69   allocation.** i
1b090 73 20 6e 6f 74 20 66 72 65 65 64 2e 0a 2a 2a 0a  s not freed..**.
1b0a0 2a 2a 20 54 68 65 20 6d 65 6d 6f 72 79 20 72 65  ** The memory re
1b0b0 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65  turned by sqlite
1b0c0 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 73  3_malloc() and s
1b0d0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
1b0e0 0a 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 61 6c  .** is always al
1b0f0 69 67 6e 65 64 20 74 6f 20 61 74 20 6c 65 61 73  igned to at leas
1b100 74 20 61 6e 20 38 20 62 79 74 65 20 62 6f 75 6e  t an 8 byte boun
1b110 64 61 72 79 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  dary. {END}.**.*
1b120 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 69 6d  * The default im
1b130 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
1b140 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  the memory alloc
1b150 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20  ation subsystem 
1b160 75 73 65 73 0a 2a 2a 20 74 68 65 20 6d 61 6c 6c  uses.** the mall
1b170 6f 63 28 29 2c 20 72 65 61 6c 6c 6f 63 28 29 20  oc(), realloc() 
1b180 61 6e 64 20 66 72 65 65 28 29 20 70 72 6f 76 69  and free() provi
1b190 64 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64  ded by the stand
1b1a0 61 72 64 20 43 20 6c 69 62 72 61 72 79 2e 0a 2a  ard C library..*
1b1b0 2a 20 7b 48 31 37 33 38 32 7d 20 48 6f 77 65 76  * {H17382} Howev
1b1c0 65 72 2c 20 69 66 20 53 51 4c 69 74 65 20 69 73  er, if SQLite is
1b1d0 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74   compiled with t
1b1e0 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 45 4d  he.** SQLITE_MEM
1b1f0 4f 52 59 5f 53 49 5a 45 3d 3c 69 3e 4e 4e 4e 3c  ORY_SIZE=<i>NNN<
1b200 2f 69 3e 20 43 20 70 72 65 70 72 6f 63 65 73 73  /i> C preprocess
1b210 6f 72 20 6d 61 63 72 6f 20 28 77 68 65 72 65 20  or macro (where 
1b220 3c 69 3e 4e 4e 4e 3c 2f 69 3e 0a 2a 2a 20 69 73  <i>NNN</i>.** is
1b230 20 61 6e 20 69 6e 74 65 67 65 72 29 2c 20 74 68   an integer), th
1b240 65 6e 20 53 51 4c 69 74 65 20 63 72 65 61 74 65  en SQLite create
1b250 20 61 20 73 74 61 74 69 63 20 61 72 72 61 79 20   a static array 
1b260 6f 66 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20 3c  of at least.** <
1b270 69 3e 4e 4e 4e 3c 2f 69 3e 20 62 79 74 65 73 20  i>NNN</i> bytes 
1b280 69 6e 20 73 69 7a 65 20 61 6e 64 20 75 73 65 73  in size and uses
1b290 20 74 68 61 74 20 61 72 72 61 79 20 66 6f 72 20   that array for 
1b2a0 61 6c 6c 20 6f 66 20 69 74 73 20 64 79 6e 61 6d  all of its dynam
1b2b0 69 63 0a 2a 2a 20 6d 65 6d 6f 72 79 20 61 6c 6c  ic.** memory all
1b2c0 6f 63 61 74 69 6f 6e 20 6e 65 65 64 73 2e 20 7b  ocation needs. {
1b2d0 45 4e 44 7d 20 20 41 64 64 69 74 69 6f 6e 61 6c  END}  Additional
1b2e0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
1b2f0 72 20 6f 70 74 69 6f 6e 73 0a 2a 2a 20 6d 61 79  r options.** may
1b300 20 62 65 20 61 64 64 65 64 20 69 6e 20 66 75 74   be added in fut
1b310 75 72 65 20 72 65 6c 65 61 73 65 73 2e 0a 2a 2a  ure releases..**
1b320 0a 2a 2a 20 49 6e 20 53 51 4c 69 74 65 20 76 65  .** In SQLite ve
1b330 72 73 69 6f 6e 20 33 2e 35 2e 30 20 61 6e 64 20  rsion 3.5.0 and 
1b340 33 2e 35 2e 31 2c 20 69 74 20 77 61 73 20 70 6f  3.5.1, it was po
1b350 73 73 69 62 6c 65 20 74 6f 20 64 65 66 69 6e 65  ssible to define
1b360 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45 5f 4f  .** the SQLITE_O
1b370 4d 49 54 5f 4d 45 4d 4f 52 59 5f 41 4c 4c 4f 43  MIT_MEMORY_ALLOC
1b380 41 54 49 4f 4e 20 77 68 69 63 68 20 77 6f 75 6c  ATION which woul
1b390 64 20 63 61 75 73 65 20 74 68 65 20 62 75 69 6c  d cause the buil
1b3a0 74 2d 69 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  t-in.** implemen
1b3b0 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  tation of these 
1b3c0 72 6f 75 74 69 6e 65 73 20 74 6f 20 62 65 20 6f  routines to be o
1b3d0 6d 69 74 74 65 64 2e 20 20 54 68 61 74 20 63 61  mitted.  That ca
1b3e0 70 61 62 69 6c 69 74 79 0a 2a 2a 20 69 73 20 6e  pability.** is n
1b3f0 6f 20 6c 6f 6e 67 65 72 20 70 72 6f 76 69 64 65  o longer provide
1b400 64 2e 20 20 4f 6e 6c 79 20 62 75 69 6c 74 2d 69  d.  Only built-i
1b410 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  n memory allocat
1b420 6f 72 73 20 63 61 6e 20 62 65 20 75 73 65 64 2e  ors can be used.
1b430 0a 2a 2a 0a 2a 2a 20 54 68 65 20 57 69 6e 64 6f  .**.** The Windo
1b440 77 73 20 4f 53 20 69 6e 74 65 72 66 61 63 65 20  ws OS interface 
1b450 6c 61 79 65 72 20 63 61 6c 6c 73 0a 2a 2a 20 74  layer calls.** t
1b460 68 65 20 73 79 73 74 65 6d 20 6d 61 6c 6c 6f 63  he system malloc
1b470 28 29 20 61 6e 64 20 66 72 65 65 28 29 20 64 69  () and free() di
1b480 72 65 63 74 6c 79 20 77 68 65 6e 20 63 6f 6e 76  rectly when conv
1b490 65 72 74 69 6e 67 0a 2a 2a 20 66 69 6c 65 6e 61  erting.** filena
1b4a0 6d 65 73 20 62 65 74 77 65 65 6e 20 74 68 65 20  mes between the 
1b4b0 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 75  UTF-8 encoding u
1b4c0 73 65 64 20 62 79 20 53 51 4c 69 74 65 0a 2a 2a  sed by SQLite.**
1b4d0 20 61 6e 64 20 77 68 61 74 65 76 65 72 20 66 69   and whatever fi
1b4e0 6c 65 6e 61 6d 65 20 65 6e 63 6f 64 69 6e 67 20  lename encoding 
1b4f0 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20 70  is used by the p
1b500 61 72 74 69 63 75 6c 61 72 20 57 69 6e 64 6f 77  articular Window
1b510 73 0a 2a 2a 20 69 6e 73 74 61 6c 6c 61 74 69 6f  s.** installatio
1b520 6e 2e 20 20 4d 65 6d 6f 72 79 20 61 6c 6c 6f 63  n.  Memory alloc
1b530 61 74 69 6f 6e 20 65 72 72 6f 72 73 20 61 72 65  ation errors are
1b540 20 64 65 74 65 63 74 65 64 2c 20 62 75 74 0a 2a   detected, but.*
1b550 2a 20 74 68 65 79 20 61 72 65 20 72 65 70 6f 72  * they are repor
1b560 74 65 64 20 62 61 63 6b 20 61 73 20 5b 53 51 4c  ted back as [SQL
1b570 49 54 45 5f 43 41 4e 54 4f 50 45 4e 5d 20 6f 72  ITE_CANTOPEN] or
1b580 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4f 45 52  .** [SQLITE_IOER
1b590 52 5d 20 72 61 74 68 65 72 20 74 68 61 6e 20 5b  R] rather than [
1b5a0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 5d 2e 0a 2a  SQLITE_NOMEM]..*
1b5b0 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
1b5c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 33 7d 20  .**.** {H17303} 
1b5d0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61   The [sqlite3_ma
1b5e0 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61  lloc(N)] interfa
1b5f0 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68 65  ce returns eithe
1b600 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a  r a pointer to.*
1b610 2a 20 20 20 20 20 20 20 20 20 20 20 61 20 6e 65  *           a ne
1b620 77 6c 79 20 63 68 65 63 6b 65 64 2d 6f 75 74 20  wly checked-out 
1b630 62 6c 6f 63 6b 20 6f 66 20 61 74 20 6c 65 61 73  block of at leas
1b640 74 20 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d  t N bytes of mem
1b650 6f 72 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ory.**          
1b660 20 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20   that is 8-byte 
1b670 61 6c 69 67 6e 65 64 2c 20 6f 72 20 69 74 20 72  aligned, or it r
1b680 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66 20 69  eturns NULL if i
1b690 74 20 69 73 20 75 6e 61 62 6c 65 0a 2a 2a 20 20  t is unable.**  
1b6a0 20 20 20 20 20 20 20 20 20 74 6f 20 66 75 6c 66           to fulf
1b6b0 69 6c 6c 20 74 68 65 20 72 65 71 75 65 73 74 2e  ill the request.
1b6c0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 30 34 7d 20  .**.** {H17304} 
1b6d0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61   The [sqlite3_ma
1b6e0 6c 6c 6f 63 28 4e 29 5d 20 69 6e 74 65 72 66 61  lloc(N)] interfa
1b6f0 63 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55 4c  ce returns a NUL
1b700 4c 20 70 6f 69 6e 74 65 72 20 69 66 0a 2a 2a 20  L pointer if.** 
1b710 20 20 20 20 20 20 20 20 20 20 4e 20 69 73 20 6c            N is l
1b720 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61  ess than or equa
1b730 6c 20 74 6f 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a  l to zero..**.**
1b740 20 7b 48 31 37 33 30 35 7d 20 20 54 68 65 20 5b   {H17305}  The [
1b750 73 71 6c 69 74 65 33 5f 66 72 65 65 28 50 29 5d  sqlite3_free(P)]
1b760 20 69 6e 74 65 72 66 61 63 65 20 72 65 6c 65 61   interface relea
1b770 73 65 73 20 6d 65 6d 6f 72 79 20 70 72 65 76 69  ses memory previ
1b780 6f 75 73 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20  ously.**        
1b790 20 20 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d     returned from
1b7a0 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63   [sqlite3_malloc
1b7b0 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
1b7c0 72 65 61 6c 6c 6f 63 28 29 5d 2c 0a 2a 2a 20 20  realloc()],.**  
1b7d0 20 20 20 20 20 20 20 20 20 6d 61 6b 69 6e 67 20           making 
1b7e0 69 74 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  it available for
1b7f0 20 72 65 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48   reuse..**.** {H
1b800 31 37 33 30 36 7d 20 20 41 20 63 61 6c 6c 20 74  17306}  A call t
1b810 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65 28  o [sqlite3_free(
1b820 4e 55 4c 4c 29 5d 20 69 73 20 61 20 68 61 72 6d  NULL)] is a harm
1b830 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  less no-op..**.*
1b840 2a 20 7b 48 31 37 33 31 30 7d 20 20 41 20 63 61  * {H17310}  A ca
1b850 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72  ll to [sqlite3_r
1b860 65 61 6c 6c 6f 63 28 30 2c 4e 29 5d 20 69 73 20  ealloc(0,N)] is 
1b870 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20  equivalent to a 
1b880 63 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20  call.**         
1b890 20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61    to [sqlite3_ma
1b8a0 6c 6c 6f 63 28 4e 29 5d 2e 0a 2a 2a 0a 2a 2a 20  lloc(N)]..**.** 
1b8b0 7b 48 31 37 33 31 32 7d 20 20 41 20 63 61 6c 6c  {H17312}  A call
1b8c0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 61   to [sqlite3_rea
1b8d0 6c 6c 6f 63 28 50 2c 30 29 5d 20 69 73 20 65 71  lloc(P,0)] is eq
1b8e0 75 69 76 61 6c 65 6e 74 20 74 6f 20 61 20 63 61  uivalent to a ca
1b8f0 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ll.**           
1b900 74 6f 20 5b 73 71 6c 69 74 65 33 5f 66 72 65 65  to [sqlite3_free
1b910 28 50 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  (P)]..**.** {H17
1b920 33 31 35 7d 20 20 54 68 65 20 53 51 4c 69 74 65  315}  The SQLite
1b930 20 63 6f 72 65 20 75 73 65 73 20 5b 73 71 6c 69   core uses [sqli
1b940 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2c 20 5b  te3_malloc()], [
1b950 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
1b960 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
1b970 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 66 72   and [sqlite3_fr
1b980 65 65 28 29 5d 20 66 6f 72 20 61 6c 6c 20 6f 66  ee()] for all of
1b990 20 69 74 73 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   its memory allo
1b9a0 63 61 74 69 6f 6e 20 61 6e 64 0a 2a 2a 20 20 20  cation and.**   
1b9b0 20 20 20 20 20 20 20 20 64 65 61 6c 6c 6f 63 61          dealloca
1b9c0 74 69 6f 6e 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a  tion needs..**.*
1b9d0 2a 20 7b 48 31 37 33 31 38 7d 20 20 54 68 65 20  * {H17318}  The 
1b9e0 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
1b9f0 28 50 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (P,N)] interface
1ba00 20 72 65 74 75 72 6e 73 20 65 69 74 68 65 72 20   returns either 
1ba10 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 20 20 20  a pointer.**    
1ba20 20 20 20 20 20 20 20 74 6f 20 61 20 62 6c 6f 63         to a bloc
1ba30 6b 20 6f 66 20 63 68 65 63 6b 65 64 2d 6f 75 74  k of checked-out
1ba40 20 6d 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65   memory of at le
1ba50 61 73 74 20 4e 20 62 79 74 65 73 20 69 6e 20 73  ast N bytes in s
1ba60 69 7a 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ize.**          
1ba70 20 74 68 61 74 20 69 73 20 38 2d 62 79 74 65 20   that is 8-byte 
1ba80 61 6c 69 67 6e 65 64 2c 20 6f 72 20 61 20 4e 55  aligned, or a NU
1ba90 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
1baa0 2a 20 7b 48 31 37 33 32 31 7d 20 20 57 68 65 6e  * {H17321}  When
1bab0 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f   [sqlite3_reallo
1bac0 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73 20  c(P,N)] returns 
1bad0 61 20 6e 6f 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74  a non-NULL point
1bae0 65 72 2c 20 69 74 20 66 69 72 73 74 0a 2a 2a 20  er, it first.** 
1baf0 20 20 20 20 20 20 20 20 20 20 63 6f 70 69 65 73            copies
1bb00 20 74 68 65 20 66 69 72 73 74 20 4b 20 62 79 74   the first K byt
1bb10 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 66 72  es of content fr
1bb20 6f 6d 20 50 20 69 6e 74 6f 20 74 68 65 20 6e 65  om P into the ne
1bb30 77 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  wly.**          
1bb40 20 61 6c 6c 6f 63 61 74 65 64 20 62 6c 6f 63 6b   allocated block
1bb50 2c 20 77 68 65 72 65 20 4b 20 69 73 20 74 68 65  , where K is the
1bb60 20 6c 65 73 73 65 72 20 6f 66 20 4e 20 61 6e 64   lesser of N and
1bb70 20 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20   the size of.** 
1bb80 20 20 20 20 20 20 20 20 20 20 74 68 65 20 62 75            the bu
1bb90 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ffer P..**.** {H
1bba0 31 37 33 32 32 7d 20 20 57 68 65 6e 20 5b 73 71  17322}  When [sq
1bbb0 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 50 2c  lite3_realloc(P,
1bbc0 4e 29 5d 20 72 65 74 75 72 6e 73 20 61 20 6e 6f  N)] returns a no
1bbd0 6e 2d 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c 20  n-NULL pointer, 
1bbe0 69 74 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20  it first.**     
1bbf0 20 20 20 20 20 20 72 65 6c 65 61 73 65 73 20 74        releases t
1bc00 68 65 20 62 75 66 66 65 72 20 50 2e 0a 2a 2a 0a  he buffer P..**.
1bc10 2a 2a 20 7b 48 31 37 33 32 33 7d 20 20 57 68 65  ** {H17323}  Whe
1bc20 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  n [sqlite3_reall
1bc30 6f 63 28 50 2c 4e 29 5d 20 72 65 74 75 72 6e 73  oc(P,N)] returns
1bc40 20 4e 55 4c 4c 2c 20 74 68 65 20 62 75 66 66 65   NULL, the buffe
1bc50 72 20 50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  r P is.**       
1bc60 20 20 20 20 6e 6f 74 20 6d 6f 64 69 66 69 65 64      not modified
1bc70 20 6f 72 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a   or released..**
1bc80 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e 53 3a  .** ASSUMPTIONS:
1bc90 0a 2a 2a 0a 2a 2a 20 7b 41 31 37 33 35 30 7d 20  .**.** {A17350} 
1bca0 20 54 68 65 20 70 6f 69 6e 74 65 72 20 61 72 67   The pointer arg
1bcb0 75 6d 65 6e 74 73 20 74 6f 20 5b 73 71 6c 69 74  uments to [sqlit
1bcc0 65 33 5f 66 72 65 65 28 29 5d 20 61 6e 64 20 5b  e3_free()] and [
1bcd0 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
1bce0 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  )].**           
1bcf0 6d 75 73 74 20 62 65 20 65 69 74 68 65 72 20 4e  must be either N
1bd00 55 4c 4c 20 6f 72 20 65 6c 73 65 20 70 6f 69 6e  ULL or else poin
1bd10 74 65 72 73 20 6f 62 74 61 69 6e 65 64 20 66 72  ters obtained fr
1bd20 6f 6d 20 61 20 70 72 69 6f 72 0a 2a 2a 20 20 20  om a prior.**   
1bd30 20 20 20 20 20 20 20 20 69 6e 76 6f 63 61 74 69          invocati
1bd40 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 6d  on of [sqlite3_m
1bd50 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c  alloc()] or [sql
1bd60 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20  ite3_realloc()] 
1bd70 74 68 61 74 20 68 61 76 65 0a 2a 2a 20 20 20 20  that have.**    
1bd80 20 20 20 20 20 20 20 6e 6f 74 20 79 65 74 20 62         not yet b
1bd90 65 65 6e 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a  een released..**
1bda0 0a 2a 2a 20 7b 41 31 37 33 35 31 7d 20 20 54 68  .** {A17351}  Th
1bdb0 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d 75  e application mu
1bdc0 73 74 20 6e 6f 74 20 72 65 61 64 20 6f 72 20 77  st not read or w
1bdd0 72 69 74 65 20 61 6e 79 20 70 61 72 74 20 6f 66  rite any part of
1bde0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 20  .**           a 
1bdf0 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 20  block of memory 
1be00 61 66 74 65 72 20 69 74 20 68 61 73 20 62 65 65  after it has bee
1be10 6e 20 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67  n released using
1be20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73  .**           [s
1be30 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 20 6f  qlite3_free()] o
1be40 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c  r [sqlite3_reall
1be50 6f 63 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  oc()]..*/.SQLITE
1be60 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74  _API void *sqlit
1be70 65 33 5f 6d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a  e3_malloc(int);.
1be80 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1be90 2a 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  *sqlite3_realloc
1bea0 28 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51  (void*, int);.SQ
1beb0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
1bec0 6c 69 74 65 33 5f 66 72 65 65 28 76 6f 69 64 2a  lite3_free(void*
1bed0 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1bee0 45 46 3a 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63  EF: Memory Alloc
1bef0 61 74 6f 72 20 53 74 61 74 69 73 74 69 63 73 20  ator Statistics 
1bf00 7b 48 31 37 33 37 30 7d 20 3c 53 33 30 32 31 30  {H17370} <S30210
1bf10 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 70  >.**.** SQLite p
1bf20 72 6f 76 69 64 65 73 20 74 68 65 73 65 20 74 77  rovides these tw
1bf30 6f 20 69 6e 74 65 72 66 61 63 65 73 20 66 6f 72  o interfaces for
1bf40 20 72 65 70 6f 72 74 69 6e 67 20 6f 6e 20 74 68   reporting on th
1bf50 65 20 73 74 61 74 75 73 0a 2a 2a 20 6f 66 20 74  e status.** of t
1bf60 68 65 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  he [sqlite3_mall
1bf70 6f 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  oc()], [sqlite3_
1bf80 66 72 65 65 28 29 5d 2c 20 61 6e 64 20 5b 73 71  free()], and [sq
1bf90 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d  lite3_realloc()]
1bfa0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 2c 20 77 68  .** routines, wh
1bfb0 69 63 68 20 66 6f 72 6d 20 74 68 65 20 62 75 69  ich form the bui
1bfc0 6c 74 2d 69 6e 20 6d 65 6d 6f 72 79 20 61 6c 6c  lt-in memory all
1bfd0 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
1bfe0 6d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  m..**.** INVARIA
1bff0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33  NTS:.**.** {H173
1c000 37 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  71} The [sqlite3
1c010 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20  _memory_used()] 
1c020 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
1c030 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
1c040 74 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  tes.**          
1c050 6f 66 20 6d 65 6d 6f 72 79 20 63 75 72 72 65 6e  of memory curren
1c060 74 6c 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20  tly outstanding 
1c070 28 6d 61 6c 6c 6f 63 65 64 20 62 75 74 20 6e 6f  (malloced but no
1c080 74 20 66 72 65 65 64 29 2e 0a 2a 2a 0a 2a 2a 20  t freed)..**.** 
1c090 7b 48 31 37 33 37 33 7d 20 54 68 65 20 5b 73 71  {H17373} The [sq
1c0a0 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67  lite3_memory_hig
1c0b0 68 77 61 74 65 72 28 29 5d 20 72 6f 75 74 69 6e  hwater()] routin
1c0c0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6d 61  e returns the ma
1c0d0 78 69 6d 75 6d 0a 2a 2a 20 20 20 20 20 20 20 20  ximum.**        
1c0e0 20 20 76 61 6c 75 65 20 6f 66 20 5b 73 71 6c 69    value of [sqli
1c0f0 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28  te3_memory_used(
1c100 29 5d 20 73 69 6e 63 65 20 74 68 65 20 68 69 67  )] since the hig
1c110 68 2d 77 61 74 65 72 20 6d 61 72 6b 0a 2a 2a 20  h-water mark.** 
1c120 20 20 20 20 20 20 20 20 20 77 61 73 20 6c 61 73           was las
1c130 74 20 72 65 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 7b  t reset..**.** {
1c140 48 31 37 33 37 34 7d 20 54 68 65 20 76 61 6c 75  H17374} The valu
1c150 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b  es returned by [
1c160 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75  sqlite3_memory_u
1c170 73 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20 20  sed()] and.**   
1c180 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
1c190 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72  memory_highwater
1c1a0 28 29 5d 20 69 6e 63 6c 75 64 65 20 61 6e 79 20  ()] include any 
1c1b0 6f 76 65 72 68 65 61 64 0a 2a 2a 20 20 20 20 20  overhead.**     
1c1c0 20 20 20 20 20 61 64 64 65 64 20 62 79 20 53 51       added by SQ
1c1d0 4c 69 74 65 20 69 6e 20 69 74 73 20 69 6d 70 6c  Lite in its impl
1c1e0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 5b 73  ementation of [s
1c1f0 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d  qlite3_malloc()]
1c200 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75  ,.**          bu
1c210 74 20 6e 6f 74 20 6f 76 65 72 68 65 61 64 20 61  t not overhead a
1c220 64 64 65 64 20 62 79 20 74 68 65 20 61 6e 79 20  dded by the any 
1c230 75 6e 64 65 72 6c 79 69 6e 67 20 73 79 73 74 65  underlying syste
1c240 6d 20 6c 69 62 72 61 72 79 0a 2a 2a 20 20 20 20  m library.**    
1c250 20 20 20 20 20 20 72 6f 75 74 69 6e 65 73 20 74        routines t
1c260 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c  hat [sqlite3_mal
1c270 6c 6f 63 28 29 5d 20 6d 61 79 20 63 61 6c 6c 2e  loc()] may call.
1c280 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 33 37 35 7d 20  .**.** {H17375} 
1c290 54 68 65 20 6d 65 6d 6f 72 79 20 68 69 67 68 2d  The memory high-
1c2a0 77 61 74 65 72 20 6d 61 72 6b 20 69 73 20 72 65  water mark is re
1c2b0 73 65 74 20 74 6f 20 74 68 65 20 63 75 72 72 65  set to the curre
1c2c0 6e 74 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20 20  nt value of.**  
1c2d0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
1c2e0 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28 29 5d 20  _memory_used()] 
1c2f0 69 66 20 61 6e 64 20 6f 6e 6c 79 20 69 66 20 74  if and only if t
1c300 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 0a  he parameter to.
1c310 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
1c320 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
1c330 77 61 74 65 72 28 29 5d 20 69 73 20 74 72 75 65  water()] is true
1c340 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65 74  .  The value ret
1c350 75 72 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20  urned.**        
1c360 20 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 6d 65    by [sqlite3_me
1c370 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 28 31  mory_highwater(1
1c380 29 5d 20 69 73 20 74 68 65 20 68 69 67 68 2d 77  )] is the high-w
1c390 61 74 65 72 20 6d 61 72 6b 0a 2a 2a 20 20 20 20  ater mark.**    
1c3a0 20 20 20 20 20 20 70 72 69 6f 72 20 74 6f 20 74        prior to t
1c3b0 68 65 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c  he reset..*/.SQL
1c3c0 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
1c3d0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65  int64 sqlite3_me
1c3e0 6d 6f 72 79 5f 75 73 65 64 28 76 6f 69 64 29 3b  mory_used(void);
1c3f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1c400 74 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65  te3_int64 sqlite
1c410 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61 74  3_memory_highwat
1c420 65 72 28 69 6e 74 20 72 65 73 65 74 46 6c 61 67  er(int resetFlag
1c430 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
1c440 45 46 3a 20 50 73 65 75 64 6f 2d 52 61 6e 64 6f  EF: Pseudo-Rando
1c450 6d 20 4e 75 6d 62 65 72 20 47 65 6e 65 72 61 74  m Number Generat
1c460 6f 72 20 7b 48 31 37 33 39 30 7d 20 3c 53 32 30  or {H17390} <S20
1c470 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74  000>.**.** SQLit
1c480 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 68 69 67  e contains a hig
1c490 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75 64 6f  h-quality pseudo
1c4a0 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 20 67  -random number g
1c4b0 65 6e 65 72 61 74 6f 72 20 28 50 52 4e 47 29 20  enerator (PRNG) 
1c4c0 75 73 65 64 20 74 6f 0a 2a 2a 20 73 65 6c 65 63  used to.** selec
1c4d0 74 20 72 61 6e 64 6f 6d 20 52 4f 57 49 44 73 20  t random ROWIDs 
1c4e0 77 68 65 6e 20 69 6e 73 65 72 74 69 6e 67 20 6e  when inserting n
1c4f0 65 77 20 72 65 63 6f 72 64 73 20 69 6e 74 6f 20  ew records into 
1c500 61 20 74 61 62 6c 65 20 74 68 61 74 0a 2a 2a 20  a table that.** 
1c510 61 6c 72 65 61 64 79 20 75 73 65 73 20 74 68 65  already uses the
1c520 20 6c 61 72 67 65 73 74 20 70 6f 73 73 69 62 6c   largest possibl
1c530 65 20 52 4f 57 49 44 2e 20 20 54 68 65 20 50 52  e ROWID.  The PR
1c540 4e 47 20 69 73 20 61 6c 73 6f 20 75 73 65 64 20  NG is also used 
1c550 66 6f 72 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64  for.** the build
1c560 2d 69 6e 20 72 61 6e 64 6f 6d 28 29 20 61 6e 64  -in random() and
1c570 20 72 61 6e 64 6f 6d 62 6c 6f 62 28 29 20 53 51   randomblob() SQ
1c580 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54 68  L functions.  Th
1c590 69 73 20 69 6e 74 65 72 66 61 63 65 20 61 6c 6c  is interface all
1c5a0 6f 77 73 0a 2a 2a 20 61 70 70 6c 69 63 61 74 69  ows.** applicati
1c5b0 6f 6e 73 20 74 6f 20 61 63 63 65 73 73 20 74 68  ons to access th
1c5c0 65 20 73 61 6d 65 20 50 52 4e 47 20 66 6f 72 20  e same PRNG for 
1c5d0 6f 74 68 65 72 20 70 75 72 70 6f 73 65 73 2e 0a  other purposes..
1c5e0 2a 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20  **.** A call to 
1c5f0 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73 74 6f  this routine sto
1c600 72 65 73 20 4e 20 62 79 74 65 73 20 6f 66 20 72  res N bytes of r
1c610 61 6e 64 6f 6d 6e 65 73 73 20 69 6e 74 6f 20 62  andomness into b
1c620 75 66 66 65 72 20 50 2e 0a 2a 2a 0a 2a 2a 20 54  uffer P..**.** T
1c630 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74 68  he first time th
1c640 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69 6e  is routine is in
1c650 76 6f 6b 65 64 20 28 65 69 74 68 65 72 20 69 6e  voked (either in
1c660 74 65 72 6e 61 6c 6c 79 20 6f 72 20 62 79 0a 2a  ternally or by.*
1c670 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  * the applicatio
1c680 6e 29 20 74 68 65 20 50 52 4e 47 20 69 73 20 73  n) the PRNG is s
1c690 65 65 64 65 64 20 75 73 69 6e 67 20 72 61 6e 64  eeded using rand
1c6a0 6f 6d 6e 65 73 73 20 6f 62 74 61 69 6e 65 64 0a  omness obtained.
1c6b0 2a 2a 20 66 72 6f 6d 20 74 68 65 20 78 52 61 6e  ** from the xRan
1c6c0 64 6f 6d 6e 65 73 73 20 6d 65 74 68 6f 64 20 6f  domness method o
1c6d0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  f the default [s
1c6e0 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65  qlite3_vfs] obje
1c6f0 63 74 2e 0a 2a 2a 20 4f 6e 20 61 6c 6c 20 73 75  ct..** On all su
1c700 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74  bsequent invocat
1c710 69 6f 6e 73 2c 20 74 68 65 20 70 73 65 75 64 6f  ions, the pseudo
1c720 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 73 20 67  -randomness is g
1c730 65 6e 65 72 61 74 65 64 0a 2a 2a 20 69 6e 74 65  enerated.** inte
1c740 72 6e 61 6c 6c 79 20 61 6e 64 20 77 69 74 68 6f  rnally and witho
1c750 75 74 20 72 65 63 6f 75 72 73 65 20 74 6f 20 74  ut recourse to t
1c760 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d  he [sqlite3_vfs]
1c770 20 78 52 61 6e 64 6f 6d 6e 65 73 73 0a 2a 2a 20   xRandomness.** 
1c780 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e  method..**.** IN
1c790 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
1c7a0 7b 48 31 37 33 39 32 7d 20 54 68 65 20 5b 73 71  {H17392} The [sq
1c7b0 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73  lite3_randomness
1c7c0 28 4e 2c 50 29 5d 20 69 6e 74 65 72 66 61 63 65  (N,P)] interface
1c7d0 20 77 72 69 74 65 73 20 4e 20 62 79 74 65 73 20   writes N bytes 
1c7e0 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 68  of.**          h
1c7f0 69 67 68 2d 71 75 61 6c 69 74 79 20 70 73 65 75  igh-quality pseu
1c800 64 6f 2d 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  do-randomness in
1c810 74 6f 20 62 75 66 66 65 72 20 50 2e 0a 2a 2f 0a  to buffer P..*/.
1c820 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
1c830 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
1c840 73 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a  ss(int N, void *
1c850 50 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  P);../*.** CAPI3
1c860 52 45 46 3a 20 43 6f 6d 70 69 6c 65 2d 54 69 6d  REF: Compile-Tim
1c870 65 20 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  e Authorization 
1c880 43 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 35 30  Callbacks {H1250
1c890 30 7d 20 3c 53 37 30 31 30 30 3e 0a 2a 2a 0a 2a  0} <S70100>.**.*
1c8a0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
1c8b0 65 67 69 73 74 65 72 73 20 61 20 61 75 74 68 6f  egisters a autho
1c8c0 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 77  rizer callback w
1c8d0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
1c8e0 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20 63 6f  .** [database co
1c8f0 6e 6e 65 63 74 69 6f 6e 5d 2c 20 73 75 70 70 6c  nnection], suppl
1c900 69 65 64 20 69 6e 20 74 68 65 20 66 69 72 73 74  ied in the first
1c910 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 20 54 68   argument..** Th
1c920 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1c930 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1c940 20 61 73 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   as SQL statemen
1c950 74 73 20 61 72 65 20 62 65 69 6e 67 20 63 6f 6d  ts are being com
1c960 70 69 6c 65 64 0a 2a 2a 20 62 79 20 5b 73 71 6c  piled.** by [sql
1c970 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20  ite3_prepare()] 
1c980 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 20  or its variants 
1c990 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
1c9a0 5f 76 32 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69  _v2()],.** [sqli
1c9b0 74 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d  te3_prepare16()]
1c9c0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
1c9d0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 20 20  epare16_v2()].  
1c9e0 41 74 20 76 61 72 69 6f 75 73 0a 2a 2a 20 70 6f  At various.** po
1c9f0 69 6e 74 73 20 64 75 72 69 6e 67 20 74 68 65 20  ints during the 
1ca00 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 70 72 6f 63  compilation proc
1ca10 65 73 73 2c 20 61 73 20 6c 6f 67 69 63 20 69 73  ess, as logic is
1ca20 20 62 65 69 6e 67 20 63 72 65 61 74 65 64 0a 2a   being created.*
1ca30 2a 20 74 6f 20 70 65 72 66 6f 72 6d 20 76 61 72  * to perform var
1ca40 69 6f 75 73 20 61 63 74 69 6f 6e 73 2c 20 74 68  ious actions, th
1ca50 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1ca60 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1ca70 20 74 6f 0a 2a 2a 20 73 65 65 20 69 66 20 74 68   to.** see if th
1ca80 6f 73 65 20 61 63 74 69 6f 6e 73 20 61 72 65 20  ose actions are 
1ca90 61 6c 6c 6f 77 65 64 2e 20 20 54 68 65 20 61 75  allowed.  The au
1caa0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1cab0 6b 20 73 68 6f 75 6c 64 0a 2a 2a 20 72 65 74 75  k should.** retu
1cac0 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 74  rn [SQLITE_OK] t
1cad0 6f 20 61 6c 6c 6f 77 20 74 68 65 20 61 63 74 69  o allow the acti
1cae0 6f 6e 2c 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f  on, [SQLITE_IGNO
1caf0 52 45 5d 20 74 6f 20 64 69 73 61 6c 6c 6f 77 20  RE] to disallow 
1cb00 74 68 65 0a 2a 2a 20 73 70 65 63 69 66 69 63 20  the.** specific 
1cb10 61 63 74 69 6f 6e 20 62 75 74 20 61 6c 6c 6f 77  action but allow
1cb20 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65   the SQL stateme
1cb30 6e 74 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74  nt to continue t
1cb40 6f 20 62 65 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64  o be.** compiled
1cb50 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e  , or [SQLITE_DEN
1cb60 59 5d 20 74 6f 20 63 61 75 73 65 20 74 68 65 20  Y] to cause the 
1cb70 65 6e 74 69 72 65 20 53 51 4c 20 73 74 61 74 65  entire SQL state
1cb80 6d 65 6e 74 20 74 6f 20 62 65 0a 2a 2a 20 72 65  ment to be.** re
1cb90 6a 65 63 74 65 64 20 77 69 74 68 20 61 6e 20 65  jected with an e
1cba0 72 72 6f 72 2e 20 20 49 66 20 74 68 65 20 61 75  rror.  If the au
1cbb0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1cbc0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 61 6e 79  k returns.** any
1cbd0 20 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61   value other tha
1cbe0 6e 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  n [SQLITE_IGNORE
1cbf0 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2c 20  ], [SQLITE_OK], 
1cc00 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45 4e 59 5d  or [SQLITE_DENY]
1cc10 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 5b 73 71  .** then the [sq
1cc20 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
1cc30 28 29 5d 20 6f 72 20 65 71 75 69 76 61 6c 65 6e  ()] or equivalen
1cc40 74 20 63 61 6c 6c 20 74 68 61 74 20 74 72 69 67  t call that trig
1cc50 67 65 72 65 64 0a 2a 2a 20 74 68 65 20 61 75 74  gered.** the aut
1cc60 68 6f 72 69 7a 65 72 20 77 69 6c 6c 20 66 61 69  horizer will fai
1cc70 6c 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20  l with an error 
1cc80 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 57  message..**.** W
1cc90 68 65 6e 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  hen the callback
1cca0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
1ccb0 5f 4f 4b 5d 2c 20 74 68 61 74 20 6d 65 61 6e 73  _OK], that means
1ccc0 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 0a 2a   the operation.*
1ccd0 2a 20 72 65 71 75 65 73 74 65 64 20 69 73 20 6f  * requested is o
1cce0 6b 2e 20 20 57 68 65 6e 20 74 68 65 20 63 61 6c  k.  When the cal
1ccf0 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 5b 53  lback returns [S
1cd00 51 4c 49 54 45 5f 44 45 4e 59 5d 2c 20 74 68 65  QLITE_DENY], the
1cd10 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
1cd20 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 65 71  pare_v2()] or eq
1cd30 75 69 76 61 6c 65 6e 74 20 63 61 6c 6c 20 74 68  uivalent call th
1cd40 61 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65  at triggered the
1cd50 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 77  .** authorizer w
1cd60 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e  ill fail with an
1cd70 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 65   error message e
1cd80 78 70 6c 61 69 6e 69 6e 67 20 74 68 61 74 0a 2a  xplaining that.*
1cd90 2a 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69  * access is deni
1cda0 65 64 2e 20 20 49 66 20 74 68 65 20 61 75 74 68  ed.  If the auth
1cdb0 6f 72 69 7a 65 72 20 63 6f 64 65 20 69 73 20 5b  orizer code is [
1cdc0 53 51 4c 49 54 45 5f 52 45 41 44 5d 0a 2a 2a 20  SQLITE_READ].** 
1cdd0 61 6e 64 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  and the callback
1cde0 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
1cdf0 5f 49 47 4e 4f 52 45 5d 20 74 68 65 6e 20 74 68  _IGNORE] then th
1ce00 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  e.** [prepared s
1ce10 74 61 74 65 6d 65 6e 74 5d 20 73 74 61 74 65 6d  tatement] statem
1ce20 65 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74  ent is construct
1ce30 65 64 20 74 6f 20 73 75 62 73 74 69 74 75 74 65  ed to substitute
1ce40 0a 2a 2a 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  .** a NULL value
1ce50 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68 65   in place of the
1ce60 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68   table column th
1ce70 61 74 20 77 6f 75 6c 64 20 68 61 76 65 0a 2a 2a  at would have.**
1ce80 20 62 65 65 6e 20 72 65 61 64 20 69 66 20 5b 53   been read if [S
1ce90 51 4c 49 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65  QLITE_OK] had be
1cea0 65 6e 20 72 65 74 75 72 6e 65 64 2e 20 20 54 68  en returned.  Th
1ceb0 65 20 5b 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45  e [SQLITE_IGNORE
1cec0 5d 0a 2a 2a 20 72 65 74 75 72 6e 20 63 61 6e 20  ].** return can 
1ced0 62 65 20 75 73 65 64 20 74 6f 20 64 65 6e 79 20  be used to deny 
1cee0 61 6e 20 75 6e 74 72 75 73 74 65 64 20 75 73 65  an untrusted use
1cef0 72 20 61 63 63 65 73 73 20 74 6f 20 69 6e 64 69  r access to indi
1cf00 76 69 64 75 61 6c 0a 2a 2a 20 63 6f 6c 75 6d 6e  vidual.** column
1cf10 73 20 6f 66 20 61 20 74 61 62 6c 65 2e 0a 2a 2a  s of a table..**
1cf20 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61  .** The first pa
1cf30 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61  rameter to the a
1cf40 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1cf50 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20  ck is a copy of 
1cf60 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 70 61 72  the third.** par
1cf70 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 73 71  ameter to the sq
1cf80 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72  lite3_set_author
1cf90 69 7a 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  izer() interface
1cfa0 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  . The second par
1cfb0 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ameter.** to the
1cfc0 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20   callback is an 
1cfd0 69 6e 74 65 67 65 72 20 5b 53 51 4c 49 54 45 5f  integer [SQLITE_
1cfe0 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20 63 6f  COPY | action co
1cff0 64 65 5d 20 74 68 61 74 20 73 70 65 63 69 66 69  de] that specifi
1d000 65 73 0a 2a 2a 20 74 68 65 20 70 61 72 74 69 63  es.** the partic
1d010 75 6c 61 72 20 61 63 74 69 6f 6e 20 74 6f 20 62  ular action to b
1d020 65 20 61 75 74 68 6f 72 69 7a 65 64 2e 20 54 68  e authorized. Th
1d030 65 20 74 68 69 72 64 20 74 68 72 6f 75 67 68 20  e third through 
1d040 73 69 78 74 68 20 70 61 72 61 6d 65 74 65 72 73  sixth parameters
1d050 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c 62  .** to the callb
1d060 61 63 6b 20 61 72 65 20 7a 65 72 6f 2d 74 65 72  ack are zero-ter
1d070 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67 73 20  minated strings 
1d080 74 68 61 74 20 63 6f 6e 74 61 69 6e 20 61 64 64  that contain add
1d090 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64 65 74 61 69  itional.** detai
1d0a0 6c 73 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ls about the act
1d0b0 69 6f 6e 20 74 6f 20 62 65 20 61 75 74 68 6f 72  ion to be author
1d0c0 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61  ized..**.** An a
1d0d0 75 74 68 6f 72 69 7a 65 72 20 69 73 20 75 73 65  uthorizer is use
1d0e0 64 20 77 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f  d when [sqlite3_
1d0f0 70 72 65 70 61 72 65 20 7c 20 70 72 65 70 61 72  prepare | prepar
1d100 69 6e 67 5d 0a 2a 2a 20 53 51 4c 20 73 74 61 74  ing].** SQL stat
1d110 65 6d 65 6e 74 73 20 66 72 6f 6d 20 61 6e 20 75  ements from an u
1d120 6e 74 72 75 73 74 65 64 20 73 6f 75 72 63 65 2c  ntrusted source,
1d130 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
1d140 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1d150 74 73 0a 2a 2a 20 64 6f 20 6e 6f 74 20 74 72 79  ts.** do not try
1d160 20 74 6f 20 61 63 63 65 73 73 20 64 61 74 61 20   to access data 
1d170 74 68 65 79 20 61 72 65 20 6e 6f 74 20 61 6c 6c  they are not all
1d180 6f 77 65 64 20 74 6f 20 73 65 65 2c 20 6f 72 20  owed to see, or 
1d190 74 68 61 74 20 74 68 65 79 20 64 6f 20 6e 6f 74  that they do not
1d1a0 0a 2a 2a 20 74 72 79 20 74 6f 20 65 78 65 63 75  .** try to execu
1d1b0 74 65 20 6d 61 6c 69 63 69 6f 75 73 20 73 74 61  te malicious sta
1d1c0 74 65 6d 65 6e 74 73 20 74 68 61 74 20 64 61 6d  tements that dam
1d1d0 61 67 65 20 74 68 65 20 64 61 74 61 62 61 73 65  age the database
1d1e0 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61 6d 70 6c  .  For.** exampl
1d1f0 65 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f  e, an applicatio
1d200 6e 20 6d 61 79 20 61 6c 6c 6f 77 20 61 20 75 73  n may allow a us
1d210 65 72 20 74 6f 20 65 6e 74 65 72 20 61 72 62 69  er to enter arbi
1d220 74 72 61 72 79 0a 2a 2a 20 53 51 4c 20 71 75 65  trary.** SQL que
1d230 72 69 65 73 20 66 6f 72 20 65 76 61 6c 75 61 74  ries for evaluat
1d240 69 6f 6e 20 62 79 20 61 20 64 61 74 61 62 61 73  ion by a databas
1d250 65 2e 20 20 42 75 74 20 74 68 65 20 61 70 70 6c  e.  But the appl
1d260 69 63 61 74 69 6f 6e 20 64 6f 65 73 0a 2a 2a 20  ication does.** 
1d270 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 75 73 65  not want the use
1d280 72 20 74 6f 20 62 65 20 61 62 6c 65 20 74 6f 20  r to be able to 
1d290 6d 61 6b 65 20 61 72 62 69 74 72 61 72 79 20 63  make arbitrary c
1d2a0 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a 2a  hanges to the.**
1d2b0 20 64 61 74 61 62 61 73 65 2e 20 20 41 6e 20 61   database.  An a
1d2c0 75 74 68 6f 72 69 7a 65 72 20 63 6f 75 6c 64 20  uthorizer could 
1d2d0 74 68 65 6e 20 62 65 20 70 75 74 20 69 6e 20 70  then be put in p
1d2e0 6c 61 63 65 20 77 68 69 6c 65 20 74 68 65 0a 2a  lace while the.*
1d2f0 2a 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20 53  * user-entered S
1d300 51 4c 20 69 73 20 62 65 69 6e 67 20 5b 73 71 6c  QL is being [sql
1d310 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20 70  ite3_prepare | p
1d320 72 65 70 61 72 65 64 5d 20 74 68 61 74 0a 2a 2a  repared] that.**
1d330 20 64 69 73 61 6c 6c 6f 77 73 20 65 76 65 72 79   disallows every
1d340 74 68 69 6e 67 20 65 78 63 65 70 74 20 5b 53 45  thing except [SE
1d350 4c 45 43 54 5d 20 73 74 61 74 65 6d 65 6e 74 73  LECT] statements
1d360 2e 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 69 63 61 74  ..**.** Applicat
1d370 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20 74  ions that need t
1d380 6f 20 70 72 6f 63 65 73 73 20 53 51 4c 20 66 72  o process SQL fr
1d390 6f 6d 20 75 6e 74 72 75 73 74 65 64 20 73 6f 75  om untrusted sou
1d3a0 72 63 65 73 0a 2a 2a 20 6d 69 67 68 74 20 61 6c  rces.** might al
1d3b0 73 6f 20 63 6f 6e 73 69 64 65 72 20 6c 6f 77 65  so consider lowe
1d3c0 72 69 6e 67 20 72 65 73 6f 75 72 63 65 20 6c 69  ring resource li
1d3d0 6d 69 74 73 20 75 73 69 6e 67 20 5b 73 71 6c 69  mits using [sqli
1d3e0 74 65 33 5f 6c 69 6d 69 74 28 29 5d 0a 2a 2a 20  te3_limit()].** 
1d3f0 61 6e 64 20 6c 69 6d 69 74 69 6e 67 20 64 61 74  and limiting dat
1d400 61 62 61 73 65 20 73 69 7a 65 20 75 73 69 6e 67  abase size using
1d410 20 74 68 65 20 5b 6d 61 78 5f 70 61 67 65 5f 63   the [max_page_c
1d420 6f 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 0a 2a  ount] [PRAGMA].*
1d430 2a 20 69 6e 20 61 64 64 69 74 69 6f 6e 20 74 6f  * in addition to
1d440 20 75 73 69 6e 67 20 61 6e 20 61 75 74 68 6f 72   using an author
1d450 69 7a 65 72 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 6c 79  izer..**.** Only
1d460 20 61 20 73 69 6e 67 6c 65 20 61 75 74 68 6f 72   a single author
1d470 69 7a 65 72 20 63 61 6e 20 62 65 20 69 6e 20 70  izer can be in p
1d480 6c 61 63 65 20 6f 6e 20 61 20 64 61 74 61 62 61  lace on a databa
1d490 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 2a 2a  se connection.**
1d4a0 20 61 74 20 61 20 74 69 6d 65 2e 20 20 45 61 63   at a time.  Eac
1d4b0 68 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  h call to sqlite
1d4c0 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
1d4d0 20 6f 76 65 72 72 69 64 65 73 20 74 68 65 0a 2a   overrides the.*
1d4e0 2a 20 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 2e  * previous call.
1d4f0 20 20 44 69 73 61 62 6c 65 20 74 68 65 20 61 75    Disable the au
1d500 74 68 6f 72 69 7a 65 72 20 62 79 20 69 6e 73 74  thorizer by inst
1d510 61 6c 6c 69 6e 67 20 61 20 4e 55 4c 4c 20 63 61  alling a NULL ca
1d520 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 54 68 65 20 61  llback..** The a
1d530 75 74 68 6f 72 69 7a 65 72 20 69 73 20 64 69 73  uthorizer is dis
1d540 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74  abled by default
1d550 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  ..**.** The auth
1d560 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20  orizer callback 
1d570 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
1d580 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
1d590 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
1d5a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
1d5b0 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
1d5c0 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c  e authorizer cal
1d5d0 6c 62 61 63 6b 2e 0a 2a 2a 20 4e 6f 74 65 20 74  lback..** Note t
1d5e0 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hat [sqlite3_pre
1d5f0 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b  pare_v2()] and [
1d600 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20  sqlite3_step()] 
1d610 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65 69  both modify thei
1d620 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  r.** database co
1d630 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74 68  nnections for th
1d640 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d 6f  e meaning of "mo
1d650 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70 61  dify" in this pa
1d660 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20 57  ragraph..**.** W
1d670 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  hen [sqlite3_pre
1d680 70 61 72 65 5f 76 32 28 29 5d 20 69 73 20 75 73  pare_v2()] is us
1d690 65 64 20 74 6f 20 70 72 65 70 61 72 65 20 61 20  ed to prepare a 
1d6a0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 0a 2a  statement, the.*
1d6b0 2a 20 73 74 61 74 65 6d 65 6e 74 20 6d 69 67 68  * statement migh
1d6c0 74 20 62 65 20 72 65 70 72 65 70 61 72 65 64 20  t be reprepared 
1d6d0 64 75 72 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  during [sqlite3_
1d6e0 73 74 65 70 28 29 5d 20 64 75 65 20 74 6f 20 61  step()] due to a
1d6f0 20 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e   .** schema chan
1d700 67 65 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  ge.  Hence, the 
1d710 61 70 70 6c 69 63 61 74 69 6f 6e 20 73 68 6f 75  application shou
1d720 6c 64 20 65 6e 73 75 72 65 20 74 68 61 74 20 74  ld ensure that t
1d730 68 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61 75  he.** correct au
1d740 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1d750 6b 20 72 65 6d 61 69 6e 73 20 69 6e 20 70 6c 61  k remains in pla
1d760 63 65 20 64 75 72 69 6e 67 20 74 68 65 20 5b 73  ce during the [s
1d770 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 2e 0a  qlite3_step()]..
1d780 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
1d790 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20 63  the authorizer c
1d7a0 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b  allback is invok
1d7b0 65 64 20 6f 6e 6c 79 20 64 75 72 69 6e 67 0a 2a  ed only during.*
1d7c0 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
1d7d0 72 65 28 29 5d 20 6f 72 20 69 74 73 20 76 61 72  re()] or its var
1d7e0 69 61 6e 74 73 2e 20 20 41 75 74 68 6f 72 69 7a  iants.  Authoriz
1d7f0 61 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a 20  ation is not.** 
1d800 70 65 72 66 6f 72 6d 65 64 20 64 75 72 69 6e 67  performed during
1d810 20 73 74 61 74 65 6d 65 6e 74 20 65 76 61 6c 75   statement evalu
1d820 61 74 69 6f 6e 20 69 6e 20 5b 73 71 6c 69 74 65  ation in [sqlite
1d830 33 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a  3_step()]..**.**
1d840 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
1d850 2a 2a 20 7b 48 31 32 35 30 31 7d 20 54 68 65 20  ** {H12501} The 
1d860 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74  [sqlite3_set_aut
1d870 68 6f 72 69 7a 65 72 28 44 2c 2e 2e 2e 29 5d 20  horizer(D,...)] 
1d880 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73 74  interface regist
1d890 65 72 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ers a.**        
1d8a0 20 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c    authorizer cal
1d8b0 6c 62 61 63 6b 20 77 69 74 68 20 64 61 74 61 62  lback with datab
1d8c0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 44  ase connection D
1d8d0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 32 7d  ..**.** {H12502}
1d8e0 20 54 68 65 20 61 75 74 68 6f 72 69 7a 65 72 20   The authorizer 
1d8f0 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
1d900 6b 65 64 20 61 73 20 53 51 4c 20 73 74 61 74 65  ked as SQL state
1d910 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 20 20 20  ments are.**    
1d920 20 20 20 20 20 20 62 65 69 6e 67 20 70 61 72 73        being pars
1d930 65 65 64 20 61 6e 64 20 63 6f 6d 70 69 6c 65 64  eed and compiled
1d940 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 33 7d  ..**.** {H12503}
1d950 20 49 66 20 74 68 65 20 61 75 74 68 6f 72 69 7a   If the authoriz
1d960 65 72 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  er callback retu
1d970 72 6e 73 20 61 6e 79 20 76 61 6c 75 65 20 6f 74  rns any value ot
1d980 68 65 72 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20  her than.**     
1d990 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47 4e       [SQLITE_IGN
1d9a0 4f 52 45 5d 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b  ORE], [SQLITE_OK
1d9b0 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 45  ], or [SQLITE_DE
1d9c0 4e 59 5d 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20  NY], then.**    
1d9d0 20 20 20 20 20 20 74 68 65 20 61 70 70 6c 69 63        the applic
1d9e0 61 74 69 6f 6e 20 69 6e 74 65 72 66 61 63 65 20  ation interface 
1d9f0 63 61 6c 6c 20 74 68 61 74 20 63 61 75 73 65 64  call that caused
1da00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
1da10 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c   authorizer call
1da20 62 61 63 6b 20 74 6f 20 72 75 6e 20 73 68 61 6c  back to run shal
1da30 6c 20 66 61 69 6c 20 77 69 74 68 20 61 6e 0a 2a  l fail with an.*
1da40 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49  *          [SQLI
1da50 54 45 5f 45 52 52 4f 52 5d 20 65 72 72 6f 72 20  TE_ERROR] error 
1da60 63 6f 64 65 20 61 6e 64 20 61 6e 20 61 70 70 72  code and an appr
1da70 6f 70 72 69 61 74 65 20 65 72 72 6f 72 20 6d 65  opriate error me
1da80 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ssage..**.** {H1
1da90 32 35 30 34 7d 20 57 68 65 6e 20 74 68 65 20 61  2504} When the a
1daa0 75 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61  uthorizer callba
1dab0 63 6b 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49  ck returns [SQLI
1dac0 54 45 5f 4f 4b 5d 2c 20 74 68 65 20 6f 70 65 72  TE_OK], the oper
1dad0 61 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20  ation.**        
1dae0 20 20 64 65 73 63 72 69 62 65 64 20 69 73 20 70    described is p
1daf0 72 6f 63 65 73 73 65 64 20 6e 6f 72 6d 61 6c 6c  rocessed normall
1db00 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30 35  y..**.** {H12505
1db10 7d 20 57 68 65 6e 20 74 68 65 20 61 75 74 68 6f  } When the autho
1db20 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b 20 72  rizer callback r
1db30 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 44  eturns [SQLITE_D
1db40 45 4e 59 5d 2c 20 74 68 65 0a 2a 2a 20 20 20 20  ENY], the.**    
1db50 20 20 20 20 20 20 61 70 70 6c 69 63 61 74 69 6f        applicatio
1db60 6e 20 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c  n interface call
1db70 20 74 68 61 74 20 63 61 75 73 65 64 20 74 68 65   that caused the
1db80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74  .**          aut
1db90 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63 6b  horizer callback
1dba0 20 74 6f 20 72 75 6e 20 73 68 61 6c 6c 20 66 61   to run shall fa
1dbb0 69 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  il.**          w
1dbc0 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f 45  ith an [SQLITE_E
1dbd0 52 52 4f 52 5d 20 65 72 72 6f 72 20 63 6f 64 65  RROR] error code
1dbe0 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65   and an error me
1dbf0 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20 20 20  ssage.**        
1dc00 20 20 65 78 70 6c 61 69 6e 69 6e 67 20 74 68 61    explaining tha
1dc10 74 20 61 63 63 65 73 73 20 69 73 20 64 65 6e 69  t access is deni
1dc20 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 30  ed..**.** {H1250
1dc30 36 7d 20 49 66 20 74 68 65 20 61 75 74 68 6f 72  6} If the author
1dc40 69 7a 65 72 20 63 6f 64 65 20 28 74 68 65 20 32  izer code (the 2
1dc50 6e 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  nd parameter to 
1dc60 74 68 65 20 61 75 74 68 6f 72 69 7a 65 72 0a 2a  the authorizer.*
1dc70 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 62  *          callb
1dc80 61 63 6b 29 20 69 73 20 5b 53 51 4c 49 54 45 5f  ack) is [SQLITE_
1dc90 52 45 41 44 5d 20 61 6e 64 20 74 68 65 20 61 75  READ] and the au
1dca0 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1dcb0 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20  k returns.**    
1dcc0 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 49 47        [SQLITE_IG
1dcd0 4e 4f 52 45 5d 2c 20 74 68 65 6e 20 74 68 65 20  NORE], then the 
1dce0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
1dcf0 6e 74 20 69 73 20 63 6f 6e 73 74 72 75 63 74 65  nt is constructe
1dd00 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  d to.**         
1dd10 20 69 6e 73 65 72 74 20 61 20 4e 55 4c 4c 20 76   insert a NULL v
1dd20 61 6c 75 65 20 69 6e 20 70 6c 61 63 65 20 6f 66  alue in place of
1dd30 20 74 68 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d   the table colum
1dd40 6e 20 74 68 61 74 20 77 6f 75 6c 64 20 68 61 76  n that would hav
1dd50 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65  e.**          be
1dd60 65 6e 20 72 65 61 64 20 69 66 20 5b 53 51 4c 49  en read if [SQLI
1dd70 54 45 5f 4f 4b 5d 20 68 61 64 20 62 65 65 6e 20  TE_OK] had been 
1dd80 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
1dd90 7b 48 31 32 35 30 37 7d 20 49 66 20 74 68 65 20  {H12507} If the 
1dda0 61 75 74 68 6f 72 69 7a 65 72 20 63 6f 64 65 20  authorizer code 
1ddb0 28 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74  (the 2nd paramet
1ddc0 65 72 20 74 6f 20 74 68 65 20 61 75 74 68 6f 72  er to the author
1ddd0 69 7a 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  izer.**         
1dde0 20 63 61 6c 6c 62 61 63 6b 29 20 69 73 20 61 6e   callback) is an
1ddf0 79 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ything other tha
1de00 6e 20 5b 53 51 4c 49 54 45 5f 52 45 41 44 5d 2c  n [SQLITE_READ],
1de10 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20   then.**        
1de20 20 20 61 20 72 65 74 75 72 6e 20 6f 66 20 5b 53    a return of [S
1de30 51 4c 49 54 45 5f 49 47 4e 4f 52 45 5d 20 68 61  QLITE_IGNORE] ha
1de40 73 20 74 68 65 20 73 61 6d 65 20 65 66 66 65 63  s the same effec
1de50 74 20 61 73 20 5b 53 51 4c 49 54 45 5f 44 45 4e  t as [SQLITE_DEN
1de60 59 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 31  Y]..**.** {H1251
1de70 30 7d 20 54 68 65 20 66 69 72 73 74 20 70 61 72  0} The first par
1de80 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 61 75  ameter to the au
1de90 74 68 6f 72 69 7a 65 72 20 63 61 6c 6c 62 61 63  thorizer callbac
1dea0 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a 2a  k is a copy of.*
1deb0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74  *          the t
1dec0 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20 74  hird parameter t
1ded0 6f 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  o the [sqlite3_s
1dee0 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d  et_authorizer()]
1def0 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a   interface..**.*
1df00 2a 20 7b 48 31 32 35 31 31 7d 20 54 68 65 20 73  * {H12511} The s
1df10 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20  econd parameter 
1df20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
1df30 69 73 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a  is an integer.**
1df40 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
1df50 45 5f 43 4f 50 59 20 7c 20 61 63 74 69 6f 6e 20  E_COPY | action 
1df60 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65 63 69  code] that speci
1df70 66 69 65 73 20 74 68 65 20 70 61 72 74 69 63 75  fies the particu
1df80 6c 61 72 20 61 63 74 69 6f 6e 0a 2a 2a 20 20 20  lar action.**   
1df90 20 20 20 20 20 20 20 74 6f 20 62 65 20 61 75 74         to be aut
1dfa0 68 6f 72 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  horized..**.** {
1dfb0 48 31 32 35 31 32 7d 20 54 68 65 20 74 68 69 72  H12512} The thir
1dfc0 64 20 74 68 72 6f 75 67 68 20 73 69 78 74 68 20  d through sixth 
1dfd0 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
1dfe0 65 20 63 61 6c 6c 62 61 63 6b 20 61 72 65 0a 2a  e callback are.*
1dff0 2a 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 2d  *          zero-
1e000 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
1e010 67 73 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 0a  gs that contain.
1e020 2a 2a 20 20 20 20 20 20 20 20 20 20 61 64 64 69  **          addi
1e030 74 69 6f 6e 61 6c 20 64 65 74 61 69 6c 73 20 61  tional details a
1e040 62 6f 75 74 20 74 68 65 20 61 63 74 69 6f 6e 20  bout the action 
1e050 74 6f 20 62 65 20 61 75 74 68 6f 72 69 7a 65 64  to be authorized
1e060 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 30 7d  ..**.** {H12520}
1e070 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b 73   Each call to [s
1e080 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f  qlite3_set_autho
1e090 72 69 7a 65 72 28 29 5d 20 6f 76 65 72 72 69 64  rizer()] overrid
1e0a0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  es.**          a
1e0b0 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e  ny previously in
1e0c0 73 74 61 6c 6c 65 64 20 61 75 74 68 6f 72 69 7a  stalled authoriz
1e0d0 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32  er..**.** {H1252
1e0e0 31 7d 20 41 20 4e 55 4c 4c 20 61 75 74 68 6f 72  1} A NULL author
1e0f0 69 7a 65 72 20 6d 65 61 6e 73 20 74 68 61 74 20  izer means that 
1e100 6e 6f 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  no authorization
1e110 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c  .**          cal
1e120 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f 6b 65 64  lback is invoked
1e130 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 32 32 7d  ..**.** {H12522}
1e140 20 54 68 65 20 64 65 66 61 75 6c 74 20 61 75 74   The default aut
1e150 68 6f 72 69 7a 65 72 20 69 73 20 4e 55 4c 4c 2e  horizer is NULL.
1e160 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
1e170 6e 74 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  nt sqlite3_set_a
1e180 75 74 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c  uthorizer(.  sql
1e190 69 74 65 33 2a 2c 0a 20 20 69 6e 74 20 28 2a 78  ite3*,.  int (*x
1e1a0 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c  Auth)(void*,int,
1e1b0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
1e1c0 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
1e1d0 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  ar*,const char*)
1e1e0 2c 0a 20 20 76 6f 69 64 20 2a 70 55 73 65 72 44  ,.  void *pUserD
1e1f0 61 74 61 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  ata.);../*.** CA
1e200 50 49 33 52 45 46 3a 20 41 75 74 68 6f 72 69 7a  PI3REF: Authoriz
1e210 65 72 20 52 65 74 75 72 6e 20 43 6f 64 65 73 20  er Return Codes 
1e220 7b 48 31 32 35 39 30 7d 20 3c 48 31 32 35 30 30  {H12590} <H12500
1e230 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c  >.**.** The [sql
1e240 69 74 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69  ite3_set_authori
1e250 7a 65 72 20 7c 20 61 75 74 68 6f 72 69 7a 65 72  zer | authorizer
1e260 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69   callback functi
1e270 6f 6e 5d 20 6d 75 73 74 0a 2a 2a 20 72 65 74 75  on] must.** retu
1e280 72 6e 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54  rn either [SQLIT
1e290 45 5f 4f 4b 5d 20 6f 72 20 6f 6e 65 20 6f 66 20  E_OK] or one of 
1e2a0 74 68 65 73 65 20 74 77 6f 20 63 6f 6e 73 74 61  these two consta
1e2b0 6e 74 73 20 69 6e 20 6f 72 64 65 72 0a 2a 2a 20  nts in order.** 
1e2c0 74 6f 20 73 69 67 6e 61 6c 20 53 51 4c 69 74 65  to signal SQLite
1e2d0 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
1e2e0 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20 70 65  the action is pe
1e2f0 72 6d 69 74 74 65 64 2e 20 20 53 65 65 20 74 68  rmitted.  See th
1e300 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 65  e.** [sqlite3_se
1e310 74 5f 61 75 74 68 6f 72 69 7a 65 72 20 7c 20 61  t_authorizer | a
1e320 75 74 68 6f 72 69 7a 65 72 20 64 6f 63 75 6d 65  uthorizer docume
1e330 6e 74 61 74 69 6f 6e 5d 20 66 6f 72 20 61 64 64  ntation] for add
1e340 69 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72  itional.** infor
1e350 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69  mation..*/.#defi
1e360 6e 65 20 53 51 4c 49 54 45 5f 44 45 4e 59 20 20  ne SQLITE_DENY  
1e370 20 31 20 20 20 2f 2a 20 41 62 6f 72 74 20 74 68   1   /* Abort th
1e380 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20  e SQL statement 
1e390 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 2a 2f  with an error */
1e3a0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1e3b0 49 47 4e 4f 52 45 20 32 20 20 20 2f 2a 20 44 6f  IGNORE 2   /* Do
1e3c0 6e 27 74 20 61 6c 6c 6f 77 20 61 63 63 65 73 73  n't allow access
1e3d0 2c 20 62 75 74 20 64 6f 6e 27 74 20 67 65 6e 65  , but don't gene
1e3e0 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 2a 2f  rate an error */
1e3f0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
1e400 3a 20 41 75 74 68 6f 72 69 7a 65 72 20 41 63 74  : Authorizer Act
1e410 69 6f 6e 20 43 6f 64 65 73 20 7b 48 31 32 35 35  ion Codes {H1255
1e420 30 7d 20 3c 48 31 32 35 30 30 3e 0a 2a 2a 0a 2a  0} <H12500>.**.*
1e430 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  * The [sqlite3_s
1e440 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 28 29 5d  et_authorizer()]
1e450 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
1e460 74 65 72 73 20 61 20 63 61 6c 6c 62 61 63 6b 20  ters a callback 
1e470 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 74 68 61 74  function.** that
1e480 20 69 73 20 69 6e 76 6f 6b 65 64 20 74 6f 20 61   is invoked to a
1e490 75 74 68 6f 72 69 7a 65 20 63 65 72 74 61 69 6e  uthorize certain
1e4a0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61   SQL statement a
1e4b0 63 74 69 6f 6e 73 2e 20 20 54 68 65 0a 2a 2a 20  ctions.  The.** 
1e4c0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
1e4d0 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b   to the callback
1e4e0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
1e4f0 6f 64 65 20 74 68 61 74 20 73 70 65 63 69 66 69  ode that specifi
1e500 65 73 0a 2a 2a 20 77 68 61 74 20 61 63 74 69 6f  es.** what actio
1e510 6e 20 69 73 20 62 65 69 6e 67 20 61 75 74 68 6f  n is being autho
1e520 72 69 7a 65 64 2e 20 20 54 68 65 73 65 20 61 72  rized.  These ar
1e530 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 61 63  e the integer ac
1e540 74 69 6f 6e 20 63 6f 64 65 73 20 74 68 61 74 0a  tion codes that.
1e550 2a 2a 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  ** the authorize
1e560 72 20 63 61 6c 6c 62 61 63 6b 20 6d 61 79 20 62  r callback may b
1e570 65 20 70 61 73 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  e passed..**.** 
1e580 54 68 65 73 65 20 61 63 74 69 6f 6e 20 63 6f 64  These action cod
1e590 65 20 76 61 6c 75 65 73 20 73 69 67 6e 69 66 79  e values signify
1e5a0 20 77 68 61 74 20 6b 69 6e 64 20 6f 66 20 6f 70   what kind of op
1e5b0 65 72 61 74 69 6f 6e 20 69 73 20 74 6f 20 62 65  eration is to be
1e5c0 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 64 2e 20  .** authorized. 
1e5d0 20 54 68 65 20 33 72 64 20 61 6e 64 20 34 74 68   The 3rd and 4th
1e5e0 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74   parameters to t
1e5f0 68 65 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  he authorization
1e600 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  .** callback fun
1e610 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 70 61  ction will be pa
1e620 72 61 6d 65 74 65 72 73 20 6f 72 20 4e 55 4c 4c  rameters or NULL
1e630 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77 68   depending on wh
1e640 69 63 68 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20  ich of these.** 
1e650 63 6f 64 65 73 20 69 73 20 75 73 65 64 20 61 73  codes is used as
1e660 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   the second para
1e670 6d 65 74 65 72 2e 20 20 54 68 65 20 35 74 68 20  meter.  The 5th 
1e680 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
1e690 0a 2a 2a 20 61 75 74 68 6f 72 69 7a 65 72 20 63  .** authorizer c
1e6a0 61 6c 6c 62 61 63 6b 20 69 73 20 74 68 65 20 6e  allback is the n
1e6b0 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1e6c0 61 73 65 20 28 22 6d 61 69 6e 22 2c 20 22 74 65  ase ("main", "te
1e6d0 6d 70 22 2c 0a 2a 2a 20 65 74 63 2e 29 20 69 66  mp",.** etc.) if
1e6e0 20 61 70 70 6c 69 63 61 62 6c 65 2e 20 20 54 68   applicable.  Th
1e6f0 65 20 36 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 6th parameter 
1e700 74 6f 20 74 68 65 20 61 75 74 68 6f 72 69 7a 65  to the authorize
1e710 72 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  r callback.** is
1e720 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1e730 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72 69 67   inner-most trig
1e740 67 65 72 20 6f 72 20 76 69 65 77 20 74 68 61 74  ger or view that
1e750 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
1e760 66 6f 72 0a 2a 2a 20 74 68 65 20 61 63 63 65 73  for.** the acces
1e770 73 20 61 74 74 65 6d 70 74 20 6f 72 20 4e 55 4c  s attempt or NUL
1e780 4c 20 69 66 20 74 68 69 73 20 61 63 63 65 73 73  L if this access
1e790 20 61 74 74 65 6d 70 74 20 69 73 20 64 69 72 65   attempt is dire
1e7a0 63 74 6c 79 20 66 72 6f 6d 0a 2a 2a 20 74 6f 70  ctly from.** top
1e7b0 2d 6c 65 76 65 6c 20 53 51 4c 20 63 6f 64 65 2e  -level SQL code.
1e7c0 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54  .**.** INVARIANT
1e7d0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35 31  S:.**.** {H12551
1e7e0 7d 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72  } The second par
1e7f0 61 6d 65 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20  ameter to an.** 
1e800 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
1e810 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72  3_set_authorizer
1e820 20 7c 20 61 75 74 68 6f 72 69 7a 65 72 20 63 61   | authorizer ca
1e830 6c 6c 62 61 63 6b 5d 20 73 68 61 6c 6c 20 62 65  llback] shall be
1e840 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20   an integer.**  
1e850 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f          [SQLITE_
1e860 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69 7a 65  COPY | authorize
1e870 72 20 63 6f 64 65 5d 20 74 68 61 74 20 73 70 65  r code] that spe
1e880 63 69 66 69 65 73 20 77 68 61 74 20 61 63 74 69  cifies what acti
1e890 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  on.**          i
1e8a0 73 20 62 65 69 6e 67 20 61 75 74 68 6f 72 69 7a  s being authoriz
1e8b0 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 35 35  ed..**.** {H1255
1e8c0 32 7d 20 54 68 65 20 33 72 64 20 61 6e 64 20 34  2} The 3rd and 4
1e8d0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
1e8e0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
1e8f0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75   [sqlite3_set_au
1e900 74 68 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f  thorizer | autho
1e910 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
1e920 6b 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  k].**          s
1e930 68 61 6c 6c 20 62 65 20 70 61 72 61 6d 65 74 65  hall be paramete
1e940 72 73 20 6f 72 20 4e 55 4c 4c 20 64 65 70 65 6e  rs or NULL depen
1e950 64 69 6e 67 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  ding on which.**
1e960 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54            [SQLIT
1e970 45 5f 43 4f 50 59 20 7c 20 61 75 74 68 6f 72 69  E_COPY | authori
1e980 7a 65 72 20 63 6f 64 65 5d 20 69 73 20 75 73 65  zer code] is use
1e990 64 20 61 73 20 74 68 65 20 73 65 63 6f 6e 64 20  d as the second 
1e9a0 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a  parameter..**.**
1e9b0 20 7b 48 31 32 35 35 33 7d 20 54 68 65 20 35 74   {H12553} The 5t
1e9c0 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
1e9d0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
1e9e0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1e9f0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69  orizer | authori
1ea00 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68  zer callback] sh
1ea10 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a  all be the name.
1ea20 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74  **          of t
1ea30 68 65 20 64 61 74 61 62 61 73 65 20 28 65 78 61  he database (exa
1ea40 6d 70 6c 65 3a 20 22 6d 61 69 6e 22 2c 20 22 74  mple: "main", "t
1ea50 65 6d 70 22 2c 20 65 74 63 2e 29 20 69 66 20 61  emp", etc.) if a
1ea60 70 70 6c 69 63 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  pplicable..**.**
1ea70 20 7b 48 31 32 35 35 34 7d 20 54 68 65 20 36 74   {H12554} The 6t
1ea80 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74  h parameter to t
1ea90 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
1eaa0 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74 68  sqlite3_set_auth
1eab0 6f 72 69 7a 65 72 20 7c 20 61 75 74 68 6f 72 69  orizer | authori
1eac0 7a 65 72 20 63 61 6c 6c 62 61 63 6b 5d 20 73 68  zer callback] sh
1ead0 61 6c 6c 20 62 65 20 74 68 65 20 6e 61 6d 65 0a  all be the name.
1eae0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 66 20 74  **          of t
1eaf0 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 74 72  he inner-most tr
1eb00 69 67 67 65 72 20 6f 72 20 76 69 65 77 20 74 68  igger or view th
1eb10 61 74 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  at is responsibl
1eb20 65 20 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  e for.**        
1eb30 20 20 74 68 65 20 61 63 63 65 73 73 20 61 74 74    the access att
1eb40 65 6d 70 74 20 6f 72 20 4e 55 4c 4c 20 69 66 20  empt or NULL if 
1eb50 74 68 69 73 20 61 63 63 65 73 73 20 61 74 74 65  this access atte
1eb60 6d 70 74 20 69 73 20 64 69 72 65 63 74 6c 79 20  mpt is directly 
1eb70 66 72 6f 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20  from.**         
1eb80 20 74 6f 70 2d 6c 65 76 65 6c 20 53 51 4c 20 63   top-level SQL c
1eb90 6f 64 65 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ode..*/./*******
1eba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ebb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1ebc0 2a 2a 2a 2a 20 33 72 64 20 2a 2a 2a 2a 2a 2a 2a  **** 3rd *******
1ebd0 2a 2a 2a 2a 2a 20 34 74 68 20 2a 2a 2a 2a 2a 2a  ***** 4th ******
1ebe0 2a 2a 2a 2a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  *****/.#define S
1ebf0 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 49 4e 44  QLITE_CREATE_IND
1ec00 45 58 20 20 20 20 20 20 20 20 20 20 31 20 20 20  EX          1   
1ec10 2f 2a 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20  /* Index Name   
1ec20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20     Table Name   
1ec30 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1ec40 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c  LITE_CREATE_TABL
1ec50 45 20 20 20 20 20 20 20 20 20 20 32 20 20 20 2f  E          2   /
1ec60 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
1ec70 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1ec80 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1ec90 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f  ITE_CREATE_TEMP_
1eca0 49 4e 44 45 58 20 20 20 20 20 33 20 20 20 2f 2a  INDEX     3   /*
1ecb0 20 49 6e 64 65 78 20 4e 61 6d 65 20 20 20 20 20   Index Name     
1ecc0 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20   Table Name     
1ecd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1ece0 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54  TE_CREATE_TEMP_T
1ecf0 41 42 4c 45 20 20 20 20 20 34 20 20 20 2f 2a 20  ABLE     4   /* 
1ed00 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1ed10 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20  NULL            
1ed20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1ed30 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
1ed40 49 47 47 45 52 20 20 20 35 20 20 20 2f 2a 20 54  IGGER   5   /* T
1ed50 72 69 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54  rigger Name    T
1ed60 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a  able Name      *
1ed70 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1ed80 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 56 49 45  _CREATE_TEMP_VIE
1ed90 57 20 20 20 20 20 20 36 20 20 20 2f 2a 20 56 69  W      6   /* Vi
1eda0 65 77 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55  ew Name       NU
1edb0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1edc0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1edd0 43 52 45 41 54 45 5f 54 52 49 47 47 45 52 20 20  CREATE_TRIGGER  
1ede0 20 20 20 20 20 20 37 20 20 20 2f 2a 20 54 72 69        7   /* Tri
1edf0 67 67 65 72 20 4e 61 6d 65 20 20 20 20 54 61 62  gger Name    Tab
1ee00 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a  le Name      */.
1ee10 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
1ee20 52 45 41 54 45 5f 56 49 45 57 20 20 20 20 20 20  REATE_VIEW      
1ee30 20 20 20 20 20 38 20 20 20 2f 2a 20 56 69 65 77       8   /* View
1ee40 20 4e 61 6d 65 20 20 20 20 20 20 20 4e 55 4c 4c   Name       NULL
1ee50 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1ee60 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
1ee70 4c 45 54 45 20 20 20 20 20 20 20 20 20 20 20 20  LETE            
1ee80 20 20 20 20 39 20 20 20 2f 2a 20 54 61 62 6c 65      9   /* Table
1ee90 20 4e 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20   Name      NULL 
1eea0 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1eeb0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f  efine SQLITE_DRO
1eec0 50 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  P_INDEX         
1eed0 20 20 31 30 20 20 20 2f 2a 20 49 6e 64 65 78 20    10   /* Index 
1eee0 4e 61 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20  Name      Table 
1eef0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1ef00 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50  fine SQLITE_DROP
1ef10 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20 20 20  _TABLE          
1ef20 20 31 31 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e   11   /* Table N
1ef30 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1ef40 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1ef50 69 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f  ine SQLITE_DROP_
1ef60 54 45 4d 50 5f 49 4e 44 45 58 20 20 20 20 20 20  TEMP_INDEX      
1ef70 31 32 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e 61  12   /* Index Na
1ef80 6d 65 20 20 20 20 20 20 54 61 62 6c 65 20 4e 61  me      Table Na
1ef90 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69  me      */.#defi
1efa0 6e 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54  ne SQLITE_DROP_T
1efb0 45 4d 50 5f 54 41 42 4c 45 20 20 20 20 20 20 31  EMP_TABLE      1
1efc0 33 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  3   /* Table Nam
1efd0 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20  e      NULL     
1efe0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
1eff0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45  e SQLITE_DROP_TE
1f000 4d 50 5f 54 52 49 47 47 45 52 20 20 20 20 31 34  MP_TRIGGER    14
1f010 20 20 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61     /* Trigger Na
1f020 6d 65 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65  me    Table Name
1f030 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65        */.#define
1f040 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d   SQLITE_DROP_TEM
1f050 50 5f 56 49 45 57 20 20 20 20 20 20 20 31 35 20  P_VIEW       15 
1f060 20 20 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20    /* View Name  
1f070 20 20 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20       NULL       
1f080 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20       */.#define 
1f090 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49 47  SQLITE_DROP_TRIG
1f0a0 47 45 52 20 20 20 20 20 20 20 20 20 31 36 20 20  GER         16  
1f0b0 20 2f 2a 20 54 72 69 67 67 65 72 20 4e 61 6d 65   /* Trigger Name
1f0c0 20 20 20 20 54 61 62 6c 65 20 4e 61 6d 65 20 20      Table Name  
1f0d0 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53      */.#define S
1f0e0 51 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 20  QLITE_DROP_VIEW 
1f0f0 20 20 20 20 20 20 20 20 20 20 20 31 37 20 20 20             17   
1f100 2f 2a 20 56 69 65 77 20 4e 61 6d 65 20 20 20 20  /* View Name    
1f110 20 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20     NULL         
1f120 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51     */.#define SQ
1f130 4c 49 54 45 5f 49 4e 53 45 52 54 20 20 20 20 20  LITE_INSERT     
1f140 20 20 20 20 20 20 20 20 20 20 31 38 20 20 20 2f            18   /
1f150 2a 20 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20  * Table Name    
1f160 20 20 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 20    NULL          
1f170 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c    */.#define SQL
1f180 49 54 45 5f 50 52 41 47 4d 41 20 20 20 20 20 20  ITE_PRAGMA      
1f190 20 20 20 20 20 20 20 20 20 31 39 20 20 20 2f 2a           19   /*
1f1a0 20 50 72 61 67 6d 61 20 4e 61 6d 65 20 20 20 20   Pragma Name    
1f1b0 20 31 73 74 20 61 72 67 20 6f 72 20 4e 55 4c 4c   1st arg or NULL
1f1c0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
1f1d0 54 45 5f 52 45 41 44 20 20 20 20 20 20 20 20 20  TE_READ         
1f1e0 20 20 20 20 20 20 20 20 32 30 20 20 20 2f 2a 20          20   /* 
1f1f0 54 61 62 6c 65 20 4e 61 6d 65 20 20 20 20 20 20  Table Name      
1f200 43 6f 6c 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20  Column Name     
1f210 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
1f220 45 5f 53 45 4c 45 43 54 20 20 20 20 20 20 20 20  E_SELECT        
1f230 20 20 20 20 20 20 20 32 31 20 20 20 2f 2a 20 4e         21   /* N
1f240 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e  ULL            N
1f250 55 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a  ULL            *
1f260 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
1f270 5f 54 52 41 4e 53 41 43 54 49 4f 4e 20 20 20 20  _TRANSACTION    
1f280 20 20 20 20 20 20 32 32 20 20 20 2f 2a 20 4e 55        22   /* NU
1f290 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 4e 55  LL            NU
1f2a0 4c 4c 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f  LL            */
1f2b0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
1f2c0 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20 20  UPDATE          
1f2d0 20 20 20 20 20 32 33 20 20 20 2f 2a 20 54 61 62       23   /* Tab
1f2e0 6c 65 20 4e 61 6d 65 20 20 20 20 20 20 43 6f 6c  le Name      Col
1f2f0 75 6d 6e 20 4e 61 6d 65 20 20 20 20 20 2a 2f 0a  umn Name     */.
1f300 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41  #define SQLITE_A
1f310 54 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20  TTACH           
1f320 20 20 20 20 32 34 20 20 20 2f 2a 20 46 69 6c 65      24   /* File
1f330 6e 61 6d 65 20 20 20 20 20 20 20 20 4e 55 4c 4c  name        NULL
1f340 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23              */.#
1f350 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45  define SQLITE_DE
1f360 54 41 43 48 20 20 20 20 20 20 20 20 20 20 20 20  TACH            
1f370 20 20 20 32 35 20 20 20 2f 2a 20 44 61 74 61 62     25   /* Datab
1f380 61 73 65 20 4e 61 6d 65 20 20 20 4e 55 4c 4c 20  ase Name   NULL 
1f390 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64             */.#d
1f3a0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 41 4c 54  efine SQLITE_ALT
1f3b0 45 52 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20  ER_TABLE        
1f3c0 20 20 32 36 20 20 20 2f 2a 20 44 61 74 61 62 61    26   /* Databa
1f3d0 73 65 20 4e 61 6d 65 20 20 20 54 61 62 6c 65 20  se Name   Table 
1f3e0 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 23 64 65  Name      */.#de
1f3f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 52 45 49 4e  fine SQLITE_REIN
1f400 44 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20  DEX             
1f410 20 32 37 20 20 20 2f 2a 20 49 6e 64 65 78 20 4e   27   /* Index N
1f420 61 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20  ame      NULL   
1f430 20 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66           */.#def
1f440 69 6e 65 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59  ine SQLITE_ANALY
1f450 5a 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ZE              
1f460 32 38 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61  28   /* Table Na
1f470 6d 65 20 20 20 20 20 20 4e 55 4c 4c 20 20 20 20  me      NULL    
1f480 20 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69          */.#defi
1f490 6e 65 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45  ne SQLITE_CREATE
1f4a0 5f 56 54 41 42 4c 45 20 20 20 20 20 20 20 20 32  _VTABLE        2
1f4b0 39 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d  9   /* Table Nam
1f4c0 65 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61  e      Module Na
1f4d0 6d 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e  me     */.#defin
1f4e0 65 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56 54  e SQLITE_DROP_VT
1f4f0 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 33 30  ABLE          30
1f500 20 20 20 2f 2a 20 54 61 62 6c 65 20 4e 61 6d 65     /* Table Name
1f510 20 20 20 20 20 20 4d 6f 64 75 6c 65 20 4e 61 6d        Module Nam
1f520 65 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  e     */.#define
1f530 20 53 51 4c 49 54 45 5f 46 55 4e 43 54 49 4f 4e   SQLITE_FUNCTION
1f540 20 20 20 20 20 20 20 20 20 20 20 20 20 33 31 20               31 
1f550 20 20 2f 2a 20 4e 55 4c 4c 20 20 20 20 20 20 20    /* NULL       
1f560 20 20 20 20 20 46 75 6e 63 74 69 6f 6e 20 4e 61       Function Na
1f570 6d 65 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  me   */.#define 
1f580 53 51 4c 49 54 45 5f 43 4f 50 59 20 20 20 20 20  SQLITE_COPY     
1f590 20 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20               0  
1f5a0 20 2f 2a 20 4e 6f 20 6c 6f 6e 67 65 72 20 75 73   /* No longer us
1f5b0 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ed */../*.** CAP
1f5c0 49 33 52 45 46 3a 20 54 72 61 63 69 6e 67 20 41  I3REF: Tracing A
1f5d0 6e 64 20 50 72 6f 66 69 6c 69 6e 67 20 46 75 6e  nd Profiling Fun
1f5e0 63 74 69 6f 6e 73 20 7b 48 31 32 32 38 30 7d 20  ctions {H12280} 
1f5f0 3c 53 36 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45  <S60400>.** EXPE
1f600 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
1f610 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65  hese routines re
1f620 67 69 73 74 65 72 20 63 61 6c 6c 62 61 63 6b 20  gister callback 
1f630 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 63  functions that c
1f640 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72 0a 2a  an be used for.*
1f650 2a 20 74 72 61 63 69 6e 67 20 61 6e 64 20 70 72  * tracing and pr
1f660 6f 66 69 6c 69 6e 67 20 74 68 65 20 65 78 65 63  ofiling the exec
1f670 75 74 69 6f 6e 20 6f 66 20 53 51 4c 20 73 74 61  ution of SQL sta
1f680 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  tements..**.** T
1f690 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63  he callback func
1f6a0 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64 20  tion registered 
1f6b0 62 79 20 73 71 6c 69 74 65 33 5f 74 72 61 63 65  by sqlite3_trace
1f6c0 28 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 61 74  () is invoked at
1f6d0 0a 2a 2a 20 76 61 72 69 6f 75 73 20 74 69 6d 65  .** various time
1f6e0 73 20 77 68 65 6e 20 61 6e 20 53 51 4c 20 73 74  s when an SQL st
1f6f0 61 74 65 6d 65 6e 74 20 69 73 20 62 65 69 6e 67  atement is being
1f700 20 72 75 6e 20 62 79 20 5b 73 71 6c 69 74 65 33   run by [sqlite3
1f710 5f 73 74 65 70 28 29 5d 2e 0a 2a 2a 20 54 68 65  _step()]..** The
1f720 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75 72 6e   callback return
1f730 73 20 61 20 55 54 46 2d 38 20 72 65 6e 64 65 72  s a UTF-8 render
1f740 69 6e 67 20 6f 66 20 74 68 65 20 53 51 4c 20 73  ing of the SQL s
1f750 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a 2a 2a  tatement text.**
1f760 20 61 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e   as the statemen
1f770 74 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 65  t first begins e
1f780 78 65 63 75 74 69 6e 67 2e 20 20 41 64 64 69 74  xecuting.  Addit
1f790 69 6f 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20  ional callbacks 
1f7a0 6f 63 63 75 72 0a 2a 2a 20 61 73 20 65 61 63 68  occur.** as each
1f7b0 20 74 72 69 67 67 65 72 65 64 20 73 75 62 70 72   triggered subpr
1f7c0 6f 67 72 61 6d 20 69 73 20 65 6e 74 65 72 65 64  ogram is entered
1f7d0 2e 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 73  .  The callbacks
1f7e0 20 66 6f 72 20 74 72 69 67 67 65 72 73 0a 2a 2a   for triggers.**
1f7f0 20 63 6f 6e 74 61 69 6e 20 61 20 55 54 46 2d 38   contain a UTF-8
1f800 20 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 74 68 61   SQL comment tha
1f810 74 20 69 64 65 6e 74 69 66 69 65 73 20 74 68 65  t identifies the
1f820 20 74 72 69 67 67 65 72 2e 0a 2a 2a 0a 2a 2a 20   trigger..**.** 
1f830 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75 6e  The callback fun
1f840 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
1f850 20 62 79 20 73 71 6c 69 74 65 33 5f 70 72 6f 66   by sqlite3_prof
1f860 69 6c 65 28 29 20 69 73 20 69 6e 76 6f 6b 65 64  ile() is invoked
1f870 0a 2a 2a 20 61 73 20 65 61 63 68 20 53 51 4c 20  .** as each SQL 
1f880 73 74 61 74 65 6d 65 6e 74 20 66 69 6e 69 73 68  statement finish
1f890 65 73 2e 20 20 54 68 65 20 70 72 6f 66 69 6c 65  es.  The profile
1f8a0 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69   callback contai
1f8b0 6e 73 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69 6e  ns.** the origin
1f8c0 61 6c 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78  al statement tex
1f8d0 74 20 61 6e 64 20 61 6e 20 65 73 74 69 6d 61 74  t and an estimat
1f8e0 65 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20  e of wall-clock 
1f8f0 74 69 6d 65 0a 2a 2a 20 6f 66 20 68 6f 77 20 6c  time.** of how l
1f900 6f 6e 67 20 74 68 61 74 20 73 74 61 74 65 6d 65  ong that stateme
1f910 6e 74 20 74 6f 6f 6b 20 74 6f 20 72 75 6e 2e 0a  nt took to run..
1f920 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
1f930 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32 38 31 7d  :.**.** {H12281}
1f940 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66 75   The callback fu
1f950 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65  nction registere
1f960 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 74 72  d by [sqlite3_tr
1f970 61 63 65 28 29 5d 20 0a 2a 2a 20 20 20 20 20 20  ace()] .**      
1f980 20 20 20 20 73 68 61 6c 6c 20 62 65 20 69 6e 76      shall be inv
1f990 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  oked.**         
1f9a0 20 77 68 65 6e 65 76 65 72 20 61 6e 20 53 51 4c   whenever an SQL
1f9b0 20 73 74 61 74 65 6d 65 6e 74 20 66 69 72 73 74   statement first
1f9c0 20 62 65 67 69 6e 73 20 74 6f 20 65 78 65 63 75   begins to execu
1f9d0 74 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20  te and.**       
1f9e0 20 20 20 77 68 65 6e 65 76 65 72 20 61 20 74 72     whenever a tr
1f9f0 69 67 67 65 72 20 73 75 62 70 72 6f 67 72 61 6d  igger subprogram
1fa00 20 66 69 72 73 74 20 62 65 67 69 6e 73 20 74 6f   first begins to
1fa10 20 72 75 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32   run..**.** {H12
1fa20 32 38 32 7d 20 45 61 63 68 20 63 61 6c 6c 20 74  282} Each call t
1fa30 6f 20 5b 73 71 6c 69 74 65 33 5f 74 72 61 63 65  o [sqlite3_trace
1fa40 28 29 5d 20 73 68 61 6c 6c 20 6f 76 65 72 72 69  ()] shall overri
1fa50 64 65 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c  de the previousl
1fa60 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  y.**          re
1fa70 67 69 73 74 65 72 65 64 20 74 72 61 63 65 20 63  gistered trace c
1fa80 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 7b  allback..**.** {
1fa90 48 31 32 32 38 33 7d 20 41 20 4e 55 4c 4c 20 74  H12283} A NULL t
1faa0 72 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68  race callback sh
1fab0 61 6c 6c 20 64 69 73 61 62 6c 65 20 74 72 61 63  all disable trac
1fac0 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 32  ing..**.** {H122
1fad0 38 34 7d 20 54 68 65 20 66 69 72 73 74 20 61 72  84} The first ar
1fae0 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72  gument to the tr
1faf0 61 63 65 20 63 61 6c 6c 62 61 63 6b 20 73 68 61  ace callback sha
1fb00 6c 6c 20 62 65 20 61 20 63 6f 70 79 20 6f 66 0a  ll be a copy of.
1fb10 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
1fb20 70 6f 69 6e 74 65 72 20 77 68 69 63 68 20 77 61  pointer which wa
1fb30 73 20 74 68 65 20 33 72 64 20 61 72 67 75 6d 65  s the 3rd argume
1fb40 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74  nt to [sqlite3_t
1fb50 72 61 63 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  race()]..**.** {
1fb60 48 31 32 32 38 35 7d 20 54 68 65 20 73 65 63 6f  H12285} The seco
1fb70 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74  nd argument to t
1fb80 68 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61 63  he trace callbac
1fb90 6b 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  k is a.**       
1fba0 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74     zero-terminat
1fbb0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  ed UTF-8 string 
1fbc0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6f  containing the o
1fbd0 72 69 67 69 6e 61 6c 20 74 65 78 74 0a 2a 2a 20  riginal text.** 
1fbe0 20 20 20 20 20 20 20 20 20 6f 66 20 74 68 65 20           of the 
1fbf0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 73  SQL statement as
1fc00 20 69 74 20 77 61 73 20 70 61 73 73 65 64 20 69   it was passed i
1fc10 6e 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  nto [sqlite3_pre
1fc20 70 61 72 65 5f 76 32 28 29 5d 0a 2a 2a 20 20 20  pare_v2()].**   
1fc30 20 20 20 20 20 20 20 6f 72 20 74 68 65 20 65 71         or the eq
1fc40 75 69 76 61 6c 65 6e 74 2c 20 6f 72 20 61 6e 20  uivalent, or an 
1fc50 53 51 4c 20 63 6f 6d 6d 65 6e 74 20 69 6e 64 69  SQL comment indi
1fc60 63 61 74 69 6e 67 20 74 68 65 20 62 65 67 69 6e  cating the begin
1fc70 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ning.**         
1fc80 20 6f 66 20 61 20 74 72 69 67 67 65 72 20 73 75   of a trigger su
1fc90 62 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a 20  bprogram..**.** 
1fca0 7b 48 31 32 32 38 37 7d 20 54 68 65 20 63 61 6c  {H12287} The cal
1fcb0 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 72  lback function r
1fcc0 65 67 69 73 74 65 72 65 64 20 62 79 20 5b 73 71  egistered by [sq
1fcd0 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28 29 5d  lite3_profile()]
1fce0 20 69 73 20 69 6e 76 6f 6b 65 64 0a 2a 2a 20 20   is invoked.**  
1fcf0 20 20 20 20 20 20 20 20 61 73 20 65 61 63 68 20          as each 
1fd00 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 66 69  SQL statement fi
1fd10 6e 69 73 68 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nishes..**.** {H
1fd20 31 32 32 38 38 7d 20 54 68 65 20 66 69 72 73 74  12288} The first
1fd30 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
1fd40 65 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61  e profile callba
1fd50 63 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 0a  ck is a copy of.
1fd60 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
1fd70 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f  3rd parameter to
1fd80 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c   [sqlite3_profil
1fd90 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  e()]..**.** {H12
1fda0 32 38 39 7d 20 54 68 65 20 73 65 63 6f 6e 64 20  289} The second 
1fdb0 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65  parameter to the
1fdc0 20 70 72 6f 66 69 6c 65 20 63 61 6c 6c 62 61 63   profile callbac
1fdd0 6b 20 69 73 20 61 0a 2a 2a 20 20 20 20 20 20 20  k is a.**       
1fde0 20 20 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74     zero-terminat
1fdf0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20  ed UTF-8 string 
1fe00 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
1fe10 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20  e complete text 
1fe20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  of.**          t
1fe30 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
1fe40 20 61 73 20 69 74 20 77 61 73 20 70 72 6f 63 65   as it was proce
1fe50 73 73 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33  ssed by [sqlite3
1fe60 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d 0a 2a  _prepare_v2()].*
1fe70 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 74 68  *          or th
1fe80 65 20 65 71 75 69 76 61 6c 65 6e 74 2e 0a 2a 2a  e equivalent..**
1fe90 0a 2a 2a 20 7b 48 31 32 32 39 30 7d 20 54 68 65  .** {H12290} The
1fea0 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
1feb0 20 74 6f 20 74 68 65 20 70 72 6f 66 69 6c 65 20   to the profile 
1fec0 63 61 6c 6c 62 61 63 6b 20 69 73 20 61 6e 20 65  callback is an e
1fed0 73 74 69 6d 61 74 65 0a 2a 2a 20 20 20 20 20 20  stimate.**      
1fee0 20 20 20 20 6f 66 20 74 68 65 20 6e 75 6d 62 65      of the numbe
1fef0 72 20 6f 66 20 6e 61 6e 6f 73 65 63 6f 6e 64 73  r of nanoseconds
1ff00 20 6f 66 20 77 61 6c 6c 2d 63 6c 6f 63 6b 20 74   of wall-clock t
1ff10 69 6d 65 20 72 65 71 75 69 72 65 64 20 74 6f 0a  ime required to.
1ff20 2a 2a 20 20 20 20 20 20 20 20 20 20 72 75 6e 20  **          run 
1ff30 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e  the SQL statemen
1ff40 74 20 66 72 6f 6d 20 73 74 61 72 74 20 74 6f 20  t from start to 
1ff50 66 69 6e 69 73 68 2e 0a 2a 2f 0a 53 51 4c 49 54  finish..*/.SQLIT
1ff60 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45 58 50  E_API SQLITE_EXP
1ff70 45 52 49 4d 45 4e 54 41 4c 20 76 6f 69 64 20 2a  ERIMENTAL void *
1ff80 73 71 6c 69 74 65 33 5f 74 72 61 63 65 28 73 71  sqlite3_trace(sq
1ff90 6c 69 74 65 33 2a 2c 20 76 6f 69 64 28 2a 78 54  lite3*, void(*xT
1ffa0 72 61 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  race)(void*,cons
1ffb0 74 20 63 68 61 72 2a 29 2c 20 76 6f 69 64 2a 29  t char*), void*)
1ffc0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  ;.SQLITE_API SQL
1ffd0 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
1ffe0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 70   void *sqlite3_p
1fff0 72 6f 66 69 6c 65 28 73 71 6c 69 74 65 33 2a 2c  rofile(sqlite3*,
20000 0a 20 20 20 76 6f 69 64 28 2a 78 50 72 6f 66 69  .   void(*xProfi
20010 6c 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  le)(void*,const 
20020 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 5f 75 69  char*,sqlite3_ui
20030 6e 74 36 34 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  nt64), void*);..
20040 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
20050 51 75 65 72 79 20 50 72 6f 67 72 65 73 73 20 43  Query Progress C
20060 61 6c 6c 62 61 63 6b 73 20 7b 48 31 32 39 31 30  allbacks {H12910
20070 7d 20 3c 53 36 30 34 30 30 3e 0a 2a 2a 0a 2a 2a  } <S60400>.**.**
20080 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   This routine co
20090 6e 66 69 67 75 72 65 73 20 61 20 63 61 6c 6c 62  nfigures a callb
200a0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 2d 20 74  ack function - t
200b0 68 65 0a 2a 2a 20 70 72 6f 67 72 65 73 73 20 63  he.** progress c
200c0 61 6c 6c 62 61 63 6b 20 2d 20 74 68 61 74 20 69  allback - that i
200d0 73 20 69 6e 76 6f 6b 65 64 20 70 65 72 69 6f 64  s invoked period
200e0 69 63 61 6c 6c 79 20 64 75 72 69 6e 67 20 6c 6f  ically during lo
200f0 6e 67 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 63 61  ng.** running ca
20100 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  lls to [sqlite3_
20110 65 78 65 63 28 29 5d 2c 20 5b 73 71 6c 69 74 65  exec()], [sqlite
20120 33 5f 73 74 65 70 28 29 5d 20 61 6e 64 0a 2a 2a  3_step()] and.**
20130 20 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   [sqlite3_get_ta
20140 62 6c 65 28 29 5d 2e 20 20 41 6e 20 65 78 61 6d  ble()].  An exam
20150 70 6c 65 20 75 73 65 20 66 6f 72 20 74 68 69 73  ple use for this
20160 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73  .** interface is
20170 20 74 6f 20 6b 65 65 70 20 61 20 47 55 49 20 75   to keep a GUI u
20180 70 64 61 74 65 64 20 64 75 72 69 6e 67 20 61 20  pdated during a 
20190 6c 61 72 67 65 20 71 75 65 72 79 2e 0a 2a 2a 0a  large query..**.
201a0 2a 2a 20 49 66 20 74 68 65 20 70 72 6f 67 72 65  ** If the progre
201b0 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  ss callback retu
201c0 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68  rns non-zero, th
201d0 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 0a 2a  e operation is.*
201e0 2a 20 69 6e 74 65 72 72 75 70 74 65 64 2e 20 20  * interrupted.  
201f0 54 68 69 73 20 66 65 61 74 75 72 65 20 63 61 6e  This feature can
20200 20 62 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   be used to impl
20210 65 6d 65 6e 74 20 61 0a 2a 2a 20 22 43 61 6e 63  ement a.** "Canc
20220 65 6c 22 20 62 75 74 74 6f 6e 20 6f 6e 20 61 20  el" button on a 
20230 47 55 49 20 70 72 6f 67 72 65 73 73 20 64 69 61  GUI progress dia
20240 6c 6f 67 20 62 6f 78 2e 0a 2a 2a 0a 2a 2a 20 54  log box..**.** T
20250 68 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64  he progress hand
20260 6c 65 72 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20  ler must not do 
20270 61 6e 79 74 68 69 6e 67 20 74 68 61 74 20 77 69  anything that wi
20280 6c 6c 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65  ll modify.** the
20290 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
202a0 74 69 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65  tion that invoke
202b0 64 20 74 68 65 20 70 72 6f 67 72 65 73 73 20 68  d the progress h
202c0 61 6e 64 6c 65 72 2e 0a 2a 2a 20 4e 6f 74 65 20  andler..** Note 
202d0 74 68 61 74 20 5b 73 71 6c 69 74 65 33 5f 70 72  that [sqlite3_pr
202e0 65 70 61 72 65 5f 76 32 28 29 5d 20 61 6e 64 20  epare_v2()] and 
202f0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
20300 20 62 6f 74 68 20 6d 6f 64 69 66 79 20 74 68 65   both modify the
20310 69 72 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63  ir.** database c
20320 6f 6e 6e 65 63 74 69 6f 6e 73 20 66 6f 72 20 74  onnections for t
20330 68 65 20 6d 65 61 6e 69 6e 67 20 6f 66 20 22 6d  he meaning of "m
20340 6f 64 69 66 79 22 20 69 6e 20 74 68 69 73 20 70  odify" in this p
20350 61 72 61 67 72 61 70 68 2e 0a 2a 2a 0a 2a 2a 20  aragraph..**.** 
20360 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
20370 2a 20 7b 48 31 32 39 31 31 7d 20 54 68 65 20 63  * {H12911} The c
20380 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e  allback function
20390 20 72 65 67 69 73 74 65 72 65 64 20 62 79 20 73   registered by s
203a0 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f  qlite3_progress_
203b0 68 61 6e 64 6c 65 72 28 29 0a 2a 2a 20 20 20 20  handler().**    
203c0 20 20 20 20 20 20 69 73 20 69 6e 76 6f 6b 65 64        is invoked
203d0 20 70 65 72 69 6f 64 69 63 61 6c 6c 79 20 64 75   periodically du
203e0 72 69 6e 67 20 6c 6f 6e 67 20 72 75 6e 6e 69 6e  ring long runnin
203f0 67 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 20 20  g calls to.**   
20400 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
20410 73 74 65 70 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  step()]..**.** {
20420 48 31 32 39 31 32 7d 20 54 68 65 20 70 72 6f 67  H12912} The prog
20430 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73  ress callback is
20440 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f   invoked once fo
20450 72 20 65 76 65 72 79 20 4e 20 76 69 72 74 75 61  r every N virtua
20460 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6d 61  l.**          ma
20470 63 68 69 6e 65 20 6f 70 63 6f 64 65 73 2c 20 77  chine opcodes, w
20480 68 65 72 65 20 4e 20 69 73 20 74 68 65 20 73 65  here N is the se
20490 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  cond argument to
204a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
204b0 20 5b 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65   [sqlite3_progre
204c0 73 73 5f 68 61 6e 64 6c 65 72 28 29 5d 20 63 61  ss_handler()] ca
204d0 6c 6c 20 74 68 61 74 20 72 65 67 69 73 74 65 72  ll that register
204e0 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  ed.**          t
204f0 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 49 66  he callback.  If
20500 20 4e 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20   N is less than 
20510 31 2c 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72  1, sqlite3_progr
20520 65 73 73 5f 68 61 6e 64 6c 65 72 28 29 0a 2a 2a  ess_handler().**
20530 20 20 20 20 20 20 20 20 20 20 61 63 74 73 20 61            acts a
20540 73 20 69 66 20 61 20 4e 55 4c 4c 20 70 72 6f 67  s if a NULL prog
20550 72 65 73 73 20 68 61 6e 64 6c 65 72 20 68 61 64  ress handler had
20560 20 62 65 65 6e 20 73 70 65 63 69 66 69 65 64 2e   been specified.
20570 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 31 33 7d 20  .**.** {H12913} 
20580 54 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  The progress cal
20590 6c 62 61 63 6b 20 69 74 73 65 6c 66 20 69 73 20  lback itself is 
205a0 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68  identified by th
205b0 65 20 74 68 69 72 64 0a 2a 2a 20 20 20 20 20 20  e third.**      
205c0 20 20 20 20 61 72 67 75 6d 65 6e 74 20 74 6f 20      argument to 
205d0 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73 73  sqlite3_progress
205e0 5f 68 61 6e 64 6c 65 72 28 29 2e 0a 2a 2a 0a 2a  _handler()..**.*
205f0 2a 20 7b 48 31 32 39 31 34 7d 20 54 68 65 20 66  * {H12914} The f
20600 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 20 74  ourth argument t
20610 6f 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  o sqlite3_progre
20620 73 73 5f 68 61 6e 64 6c 65 72 28 29 20 69 73 20  ss_handler() is 
20630 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 6f  a.**          vo
20640 69 64 20 70 6f 69 6e 74 65 72 20 70 61 73 73 65  id pointer passe
20650 64 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73  d to the progres
20660 73 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20  s callback.**   
20670 20 20 20 20 20 20 20 66 75 6e 63 74 69 6f 6e 20         function 
20680 65 61 63 68 20 74 69 6d 65 20 69 74 20 69 73 20  each time it is 
20690 69 6e 76 6f 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  invoked..**.** {
206a0 48 31 32 39 31 35 7d 20 49 66 20 61 20 63 61 6c  H12915} If a cal
206b0 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
206c0 65 70 28 29 5d 20 72 65 73 75 6c 74 73 20 69 6e  ep()] results in
206d0 20 66 65 77 65 72 20 74 68 61 6e 20 4e 20 6f 70   fewer than N op
206e0 63 6f 64 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  codes.**        
206f0 20 20 62 65 69 6e 67 20 65 78 65 63 75 74 65 64    being executed
20700 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67 72  , then the progr
20710 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  ess callback is 
20720 6e 65 76 65 72 20 69 6e 76 6f 6b 65 64 2e 0a 2a  never invoked..*
20730 2a 0a 2a 2a 20 7b 48 31 32 39 31 36 7d 20 45 76  *.** {H12916} Ev
20740 65 72 79 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ery call to [sql
20750 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
20760 6e 64 6c 65 72 28 29 5d 0a 2a 2a 20 20 20 20 20  ndler()].**     
20770 20 20 20 20 20 6f 76 65 72 77 72 69 74 65 73 20       overwrites 
20780 61 6e 79 20 70 72 65 76 69 6f 75 73 6c 79 20 72  any previously r
20790 65 67 69 73 74 65 72 65 64 20 70 72 6f 67 72 65  egistered progre
207a0 73 73 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a  ss handler..**.*
207b0 2a 20 7b 48 31 32 39 31 37 7d 20 49 66 20 74 68  * {H12917} If th
207c0 65 20 70 72 6f 67 72 65 73 73 20 68 61 6e 64 6c  e progress handl
207d0 65 72 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 4e  er callback is N
207e0 55 4c 4c 20 74 68 65 6e 20 6e 6f 20 70 72 6f 67  ULL then no prog
207f0 72 65 73 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  ress.**         
20800 20 68 61 6e 64 6c 65 72 20 69 73 20 69 6e 76 6f   handler is invo
20810 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39  ked..**.** {H129
20820 31 38 7d 20 49 66 20 74 68 65 20 70 72 6f 67 72  18} If the progr
20830 65 73 73 20 63 61 6c 6c 62 61 63 6b 20 72 65 74  ess callback ret
20840 75 72 6e 73 20 61 20 72 65 73 75 6c 74 20 6f 74  urns a result ot
20850 68 65 72 20 74 68 61 6e 20 30 2c 20 74 68 65 6e  her than 0, then
20860 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
20870 20 62 65 68 61 76 69 6f 72 20 69 73 20 61 20 69   behavior is a i
20880 66 20 5b 73 71 6c 69 74 65 33 5f 69 6e 74 65 72  f [sqlite3_inter
20890 72 75 70 74 28 29 5d 20 68 61 64 20 62 65 65 6e  rupt()] had been
208a0 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 20 20   called..**     
208b0 20 20 20 20 20 3c 53 33 30 35 30 30 3e 0a 2a 2f       <S30500>.*/
208c0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
208d0 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65 73   sqlite3_progres
208e0 73 5f 68 61 6e 64 6c 65 72 28 73 71 6c 69 74 65  s_handler(sqlite
208f0 33 2a 2c 20 69 6e 74 2c 20 69 6e 74 28 2a 29 28  3*, int, int(*)(
20900 76 6f 69 64 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a  void*), void*);.
20910 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
20920 20 4f 70 65 6e 69 6e 67 20 41 20 4e 65 77 20 44   Opening A New D
20930 61 74 61 62 61 73 65 20 43 6f 6e 6e 65 63 74 69  atabase Connecti
20940 6f 6e 20 7b 48 31 32 37 30 30 7d 20 3c 53 34 30  on {H12700} <S40
20950 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  200>.**.** These
20960 20 72 6f 75 74 69 6e 65 73 20 6f 70 65 6e 20 61   routines open a
20970 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  n SQLite databas
20980 65 20 66 69 6c 65 20 77 68 6f 73 65 20 6e 61 6d  e file whose nam
20990 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 74 68  e is given by th
209a0 65 0a 2a 2a 20 66 69 6c 65 6e 61 6d 65 20 61 72  e.** filename ar
209b0 67 75 6d 65 6e 74 2e 20 54 68 65 20 66 69 6c 65  gument. The file
209c0 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20 69 73  name argument is
209d0 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
209e0 55 54 46 2d 38 20 66 6f 72 0a 2a 2a 20 73 71 6c  UTF-8 for.** sql
209f0 69 74 65 33 5f 6f 70 65 6e 28 29 20 61 6e 64 20  ite3_open() and 
20a00 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
20a10 29 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 20  ) and as UTF-16 
20a20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62 79  in the native by
20a30 74 65 0a 2a 2a 20 6f 72 64 65 72 20 66 6f 72 20  te.** order for 
20a40 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29  sqlite3_open16()
20a50 2e 20 41 20 5b 64 61 74 61 62 61 73 65 20 63 6f  . A [database co
20a60 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65  nnection] handle
20a70 20 69 73 20 75 73 75 61 6c 6c 79 0a 2a 2a 20 72   is usually.** r
20a80 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 70 44 62  eturned in *ppDb
20a90 2c 20 65 76 65 6e 20 69 66 20 61 6e 20 65 72 72  , even if an err
20aa0 6f 72 20 6f 63 63 75 72 73 2e 20 20 54 68 65 20  or occurs.  The 
20ab0 6f 6e 6c 79 20 65 78 63 65 70 74 69 6f 6e 20 69  only exception i
20ac0 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 53 51 4c  s that.** if SQL
20ad0 69 74 65 20 69 73 20 75 6e 61 62 6c 65 20 74 6f  ite is unable to
20ae0 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
20af0 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 5b 73 71   to hold the [sq
20b00 6c 69 74 65 33 5d 20 6f 62 6a 65 63 74 2c 0a 2a  lite3] object,.*
20b10 2a 20 61 20 4e 55 4c 4c 20 77 69 6c 6c 20 62 65  * a NULL will be
20b20 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70   written into *p
20b30 70 44 62 20 69 6e 73 74 65 61 64 20 6f 66 20 61  pDb instead of a
20b40 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
20b50 5b 73 71 6c 69 74 65 33 5d 0a 2a 2a 20 6f 62 6a  [sqlite3].** obj
20b60 65 63 74 2e 20 49 66 20 74 68 65 20 64 61 74 61  ect. If the data
20b70 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 28  base is opened (
20b80 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64 29 20  and/or created) 
20b90 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 74 68  successfully, th
20ba0 65 6e 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 4b  en.** [SQLITE_OK
20bb0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  ] is returned.  
20bc0 4f 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72  Otherwise an [er
20bd0 72 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74  ror code] is ret
20be0 75 72 6e 65 64 2e 20 20 54 68 65 0a 2a 2a 20 5b  urned.  The.** [
20bf0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
20c00 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72  ] or [sqlite3_er
20c10 72 6d 73 67 31 36 28 29 5d 20 72 6f 75 74 69 6e  rmsg16()] routin
20c20 65 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74  es can be used t
20c30 6f 20 6f 62 74 61 69 6e 0a 2a 2a 20 61 6e 20 45  o obtain.** an E
20c40 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65 20  nglish language 
20c50 64 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74  description of t
20c60 68 65 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20  he error..**.** 
20c70 54 68 65 20 64 65 66 61 75 6c 74 20 65 6e 63 6f  The default enco
20c80 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74  ding for the dat
20c90 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 55 54  abase will be UT
20ca0 46 2d 38 20 69 66 0a 2a 2a 20 73 71 6c 69 74 65  F-8 if.** sqlite
20cb0 33 5f 6f 70 65 6e 28 29 20 6f 72 20 73 71 6c 69  3_open() or sqli
20cc0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 20 69 73  te3_open_v2() is
20cd0 20 63 61 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 55   called and.** U
20ce0 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61 74  TF-16 in the nat
20cf0 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 69  ive byte order i
20d00 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36  f sqlite3_open16
20d10 28 29 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a  () is used..**.*
20d20 2a 20 57 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  * Whether or not
20d30 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
20d40 20 77 68 65 6e 20 69 74 20 69 73 20 6f 70 65 6e   when it is open
20d50 65 64 2c 20 72 65 73 6f 75 72 63 65 73 0a 2a 2a  ed, resources.**
20d60 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
20d70 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
20d80 6f 6e 6e 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c  onnection] handl
20d90 65 20 73 68 6f 75 6c 64 20 62 65 20 72 65 6c 65  e should be rele
20da0 61 73 65 64 20 62 79 0a 2a 2a 20 70 61 73 73 69  ased by.** passi
20db0 6e 67 20 69 74 20 74 6f 20 5b 73 71 6c 69 74 65  ng it to [sqlite
20dc0 33 5f 63 6c 6f 73 65 28 29 5d 20 77 68 65 6e 20  3_close()] when 
20dd0 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72 20  it is no longer 
20de0 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a 20  required..**.** 
20df0 54 68 65 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  The sqlite3_open
20e00 5f 76 32 28 29 20 69 6e 74 65 72 66 61 63 65 20  _v2() interface 
20e10 77 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69 74  works like sqlit
20e20 65 33 5f 6f 70 65 6e 28 29 0a 2a 2a 20 65 78 63  e3_open().** exc
20e30 65 70 74 20 74 68 61 74 20 69 74 20 61 63 63 65  ept that it acce
20e40 70 74 73 20 74 77 6f 20 61 64 64 69 74 69 6f 6e  pts two addition
20e50 61 6c 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f  al parameters fo
20e60 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6e  r additional con
20e70 74 72 6f 6c 0a 2a 2a 20 6f 76 65 72 20 74 68 65  trol.** over the
20e80 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 63 6f   new database co
20e90 6e 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 66  nnection.  The f
20ea0 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20 63  lags parameter c
20eb0 61 6e 20 74 61 6b 65 20 6f 6e 65 20 6f 66 0a 2a  an take one of.*
20ec0 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * the following 
20ed0 74 68 72 65 65 20 76 61 6c 75 65 73 2c 20 6f 70  three values, op
20ee0 74 69 6f 6e 61 6c 6c 79 20 63 6f 6d 62 69 6e 65  tionally combine
20ef0 64 20 77 69 74 68 20 74 68 65 20 0a 2a 2a 20 5b  d with the .** [
20f00 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55  SQLITE_OPEN_NOMU
20f10 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f  TEX] or [SQLITE_
20f20 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20  OPEN_FULLMUTEX] 
20f30 66 6c 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c  flags:.**.** <dl
20f40 3e 0a 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45  >.** <dt>[SQLITE
20f50 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 5d 3c  _OPEN_READONLY]<
20f60 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20  /dt>.** <dd>The 
20f70 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
20f80 65 64 20 69 6e 20 72 65 61 64 2d 6f 6e 6c 79 20  ed in read-only 
20f90 6d 6f 64 65 2e 20 20 49 66 20 74 68 65 20 64 61  mode.  If the da
20fa0 74 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a  tabase does not.
20fb0 2a 2a 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  ** already exist
20fc0 2c 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65  , an error is re
20fd0 74 75 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a  turned.</dd>.**.
20fe0 2a 2a 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f  ** <dt>[SQLITE_O
20ff0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 5d 3c 2f  PEN_READWRITE]</
21000 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 64  dt>.** <dd>The d
21010 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65  atabase is opene
21020 64 20 66 6f 72 20 72 65 61 64 69 6e 67 20 61 6e  d for reading an
21030 64 20 77 72 69 74 69 6e 67 20 69 66 20 70 6f 73  d writing if pos
21040 73 69 62 6c 65 2c 20 6f 72 20 72 65 61 64 69 6e  sible, or readin
21050 67 0a 2a 2a 20 6f 6e 6c 79 20 69 66 20 74 68 65  g.** only if the
21060 20 66 69 6c 65 20 69 73 20 77 72 69 74 65 20 70   file is write p
21070 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20  rotected by the 
21080 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
21090 2e 20 20 49 6e 20 65 69 74 68 65 72 0a 2a 2a 20  .  In either.** 
210a0 63 61 73 65 20 74 68 65 20 64 61 74 61 62 61 73  case the databas
210b0 65 20 6d 75 73 74 20 61 6c 72 65 61 64 79 20 65  e must already e
210c0 78 69 73 74 2c 20 6f 74 68 65 72 77 69 73 65 20  xist, otherwise 
210d0 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  an error is retu
210e0 72 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a  rned.</dd>.**.**
210f0 20 3c 64 74 3e 5b 53 51 4c 49 54 45 5f 4f 50 45   <dt>[SQLITE_OPE
21100 4e 5f 52 45 41 44 57 52 49 54 45 5d 20 7c 20 5b  N_READWRITE] | [
21110 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41  SQLITE_OPEN_CREA
21120 54 45 5d 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  TE]</dt>.** <dd>
21130 54 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20  The database is 
21140 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64 69  opened for readi
21150 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2c 20  ng and writing, 
21160 61 6e 64 20 69 73 20 63 72 65 61 74 65 73 20 69  and is creates i
21170 74 20 69 66 0a 2a 2a 20 69 74 20 64 6f 65 73 20  t if.** it does 
21180 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78 69 73  not already exis
21190 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20 62  t. This is the b
211a0 65 68 61 76 69 6f 72 20 74 68 61 74 20 69 73 20  ehavior that is 
211b0 61 6c 77 61 79 73 20 75 73 65 64 20 66 6f 72 0a  always used for.
211c0 2a 2a 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28  ** sqlite3_open(
211d0 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70  ) and sqlite3_op
211e0 65 6e 31 36 28 29 2e 3c 2f 64 64 3e 0a 2a 2a 20  en16().</dd>.** 
211f0 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  </dl>.**.** If t
21200 68 65 20 33 72 64 20 70 61 72 61 6d 65 74 65 72  he 3rd parameter
21210 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e   to sqlite3_open
21220 5f 76 32 28 29 20 69 73 20 6e 6f 74 20 6f 6e 65  _v2() is not one
21230 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f 6d 62 69   of the.** combi
21240 6e 61 74 69 6f 6e 73 20 73 68 6f 77 6e 20 61 62  nations shown ab
21250 6f 76 65 20 6f 72 20 6f 6e 65 20 6f 66 20 74 68  ove or one of th
21260 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 73  e combinations s
21270 68 6f 77 6e 20 61 62 6f 76 65 20 63 6f 6d 62 69  hown above combi
21280 6e 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65 20  ned.** with the 
21290 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d  [SQLITE_OPEN_NOM
212a0 55 54 45 58 5d 20 6f 72 20 5b 53 51 4c 49 54 45  UTEX] or [SQLITE
212b0 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d  _OPEN_FULLMUTEX]
212c0 20 66 6c 61 67 73 2c 0a 2a 2a 20 74 68 65 6e 20   flags,.** then 
212d0 74 68 65 20 62 65 68 61 76 69 6f 72 20 69 73 20  the behavior is 
212e0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
212f0 20 49 66 20 74 68 65 20 5b 53 51 4c 49 54 45 5f   If the [SQLITE_
21300 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58 5d 20 66 6c  OPEN_NOMUTEX] fl
21310 61 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20  ag is set, then 
21320 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  the database con
21330 6e 65 63 74 69 6f 6e 0a 2a 2a 20 6f 70 65 6e 73  nection.** opens
21340 20 69 6e 20 74 68 65 20 6d 75 6c 74 69 2d 74 68   in the multi-th
21350 72 65 61 64 20 5b 74 68 72 65 61 64 69 6e 67 20  read [threading 
21360 6d 6f 64 65 5d 20 61 73 20 6c 6f 6e 67 20 61 73  mode] as long as
21370 20 74 68 65 20 73 69 6e 67 6c 65 2d 74 68 72 65   the single-thre
21380 61 64 0a 2a 2a 20 6d 6f 64 65 20 68 61 73 20 6e  ad.** mode has n
21390 6f 74 20 62 65 65 6e 20 73 65 74 20 61 74 20 63  ot been set at c
213a0 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 6f 72 20 73  ompile-time or s
213b0 74 61 72 74 2d 74 69 6d 65 2e 20 20 49 66 20 74  tart-time.  If t
213c0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 4f 50  he.** [SQLITE_OP
213d0 45 4e 5f 46 55 4c 4c 4d 55 54 45 58 5d 20 66 6c  EN_FULLMUTEX] fl
213e0 61 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 74  ag is set then t
213f0 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
21400 65 63 74 69 6f 6e 20 6f 70 65 6e 73 0a 2a 2a 20  ection opens.** 
21410 69 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65  in the serialize
21420 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  d [threading mod
21430 65 5d 20 75 6e 6c 65 73 73 20 73 69 6e 67 6c 65  e] unless single
21440 2d 74 68 72 65 61 64 20 77 61 73 0a 2a 2a 20 70  -thread was.** p
21450 72 65 76 69 6f 75 73 6c 79 20 73 65 6c 65 63 74  reviously select
21460 65 64 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69  ed at compile-ti
21470 6d 65 20 6f 72 20 73 74 61 72 74 2d 74 69 6d 65  me or start-time
21480 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66  ..**.** If the f
21490 69 6c 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d  ilename is ":mem
214a0 6f 72 79 3a 22 2c 20 74 68 65 6e 20 61 20 70 72  ory:", then a pr
214b0 69 76 61 74 65 2c 20 74 65 6d 70 6f 72 61 72 79  ivate, temporary
214c0 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62   in-memory datab
214d0 61 73 65 0a 2a 2a 20 69 73 20 63 72 65 61 74 65  ase.** is create
214e0 64 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63  d for the connec
214f0 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 6e 2d 6d  tion.  This in-m
21500 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 77  emory database w
21510 69 6c 6c 20 76 61 6e 69 73 68 20 77 68 65 6e 0a  ill vanish when.
21520 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
21530 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c  connection is cl
21540 6f 73 65 64 2e 20 20 46 75 74 75 72 65 20 76 65  osed.  Future ve
21550 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74 65  rsions of SQLite
21560 20 6d 69 67 68 74 0a 2a 2a 20 6d 61 6b 65 20 75   might.** make u
21570 73 65 20 6f 66 20 61 64 64 69 74 69 6f 6e 61 6c  se of additional
21580 20 73 70 65 63 69 61 6c 20 66 69 6c 65 6e 61 6d   special filenam
21590 65 73 20 74 68 61 74 20 62 65 67 69 6e 20 77 69  es that begin wi
215a0 74 68 20 74 68 65 20 22 3a 22 20 63 68 61 72 61  th the ":" chara
215b0 63 74 65 72 2e 0a 2a 2a 20 49 74 20 69 73 20 72  cter..** It is r
215c0 65 63 6f 6d 6d 65 6e 64 65 64 20 74 68 61 74 20  ecommended that 
215d0 77 68 65 6e 20 61 20 64 61 74 61 62 61 73 65 20  when a database 
215e0 66 69 6c 65 6e 61 6d 65 20 61 63 74 75 61 6c 6c  filename actuall
215f0 79 20 64 6f 65 73 20 62 65 67 69 6e 20 77 69 74  y does begin wit
21600 68 0a 2a 2a 20 61 20 22 3a 22 20 63 68 61 72 61  h.** a ":" chara
21610 63 74 65 72 20 79 6f 75 20 73 68 6f 75 6c 64 20  cter you should 
21620 70 72 65 66 69 78 20 74 68 65 20 66 69 6c 65 6e  prefix the filen
21630 61 6d 65 20 77 69 74 68 20 61 20 70 61 74 68 6e  ame with a pathn
21640 61 6d 65 20 73 75 63 68 20 61 73 0a 2a 2a 20 22  ame such as.** "
21650 2e 2f 22 20 74 6f 20 61 76 6f 69 64 20 61 6d 62  ./" to avoid amb
21660 69 67 75 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 49 66  iguity..**.** If
21670 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 20 69 73   the filename is
21680 20 61 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67   an empty string
21690 2c 20 74 68 65 6e 20 61 20 70 72 69 76 61 74 65  , then a private
216a0 2c 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a 20 6f  , temporary.** o
216b0 6e 2d 64 69 73 6b 20 64 61 74 61 62 61 73 65 20  n-disk database 
216c0 77 69 6c 6c 20 62 65 20 63 72 65 61 74 65 64 2e  will be created.
216d0 20 20 54 68 69 73 20 70 72 69 76 61 74 65 20 64    This private d
216e0 61 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 0a  atabase will be.
216f0 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ** automatically
21700 20 64 65 6c 65 74 65 64 20 61 73 20 73 6f 6f 6e   deleted as soon
21710 20 61 73 20 74 68 65 20 64 61 74 61 62 61 73 65   as the database
21720 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
21730 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  losed..**.** The
21740 20 66 6f 75 72 74 68 20 70 61 72 61 6d 65 74 65   fourth paramete
21750 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 6f 70 65  r to sqlite3_ope
21760 6e 5f 76 32 28 29 20 69 73 20 74 68 65 20 6e 61  n_v2() is the na
21770 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 5b 73 71  me of the.** [sq
21780 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
21790 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 74  t that defines t
217a0 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79 73  he operating sys
217b0 74 65 6d 20 69 6e 74 65 72 66 61 63 65 20 74 68  tem interface th
217c0 61 74 0a 2a 2a 20 74 68 65 20 6e 65 77 20 64 61  at.** the new da
217d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
217e0 6e 20 73 68 6f 75 6c 64 20 75 73 65 2e 20 20 49  n should use.  I
217f0 66 20 74 68 65 20 66 6f 75 72 74 68 20 70 61 72  f the fourth par
21800 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 61 20 4e  ameter is.** a N
21810 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68 65 6e  ULL pointer then
21820 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73 71   the default [sq
21830 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
21840 74 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a  t is used..**.**
21850 20 3c 62 3e 4e 6f 74 65 20 74 6f 20 57 69 6e 64   <b>Note to Wind
21860 6f 77 73 20 75 73 65 72 73 3a 3c 2f 62 3e 20 20  ows users:</b>  
21870 54 68 65 20 65 6e 63 6f 64 69 6e 67 20 75 73 65  The encoding use
21880 64 20 66 6f 72 20 74 68 65 20 66 69 6c 65 6e 61  d for the filena
21890 6d 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6f  me argument.** o
218a0 66 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29  f sqlite3_open()
218b0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65   and sqlite3_ope
218c0 6e 5f 76 32 28 29 20 6d 75 73 74 20 62 65 20 55  n_v2() must be U
218d0 54 46 2d 38 2c 20 6e 6f 74 20 77 68 61 74 65 76  TF-8, not whatev
218e0 65 72 0a 2a 2a 20 63 6f 64 65 70 61 67 65 20 69  er.** codepage i
218f0 73 20 63 75 72 72 65 6e 74 6c 79 20 64 65 66 69  s currently defi
21900 6e 65 64 2e 20 20 46 69 6c 65 6e 61 6d 65 73 20  ned.  Filenames 
21910 63 6f 6e 74 61 69 6e 69 6e 67 20 69 6e 74 65 72  containing inter
21920 6e 61 74 69 6f 6e 61 6c 0a 2a 2a 20 63 68 61 72  national.** char
21930 61 63 74 65 72 73 20 6d 75 73 74 20 62 65 20 63  acters must be c
21940 6f 6e 76 65 72 74 65 64 20 74 6f 20 55 54 46 2d  onverted to UTF-
21950 38 20 70 72 69 6f 72 20 74 6f 20 70 61 73 73 69  8 prior to passi
21960 6e 67 20 74 68 65 6d 20 69 6e 74 6f 0a 2a 2a 20  ng them into.** 
21970 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 6f  sqlite3_open() o
21980 72 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76  r sqlite3_open_v
21990 32 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  2()..**.** INVAR
219a0 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
219b0 32 37 30 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  2701} The [sqlit
219c0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
219d0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 20  ite3_open16()], 
219e0 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
219f0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
21a00 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20 63  ()] interfaces c
21a10 72 65 61 74 65 20 61 20 6e 65 77 0a 2a 2a 20 20  reate a new.**  
21a20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73          [databas
21a30 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 73  e connection] as
21a40 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a 2a 2a  sociated with.**
21a50 20 20 20 20 20 20 20 20 20 20 74 68 65 20 64 61            the da
21a60 74 61 62 61 73 65 20 66 69 6c 65 20 67 69 76 65  tabase file give
21a70 6e 20 69 6e 20 74 68 65 69 72 20 66 69 72 73 74  n in their first
21a80 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a   parameter..**.*
21a90 2a 20 7b 48 31 32 37 30 32 7d 20 54 68 65 20 66  * {H12702} The f
21aa0 69 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74  ilename argument
21ab0 20 69 73 20 69 6e 74 65 72 70 72 65 74 65 64 20   is interpreted 
21ac0 61 73 20 55 54 46 2d 38 0a 2a 2a 20 20 20 20 20  as UTF-8.**     
21ad0 20 20 20 20 20 66 6f 72 20 5b 73 71 6c 69 74 65       for [sqlite
21ae0 33 5f 6f 70 65 6e 28 29 5d 20 61 6e 64 20 5b 73  3_open()] and [s
21af0 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29  qlite3_open_v2()
21b00 5d 20 61 6e 64 20 61 73 20 55 54 46 2d 31 36 0a  ] and as UTF-16.
21b10 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74  **          in t
21b20 68 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f  he native byte o
21b30 72 64 65 72 20 66 6f 72 20 5b 73 71 6c 69 74 65  rder for [sqlite
21b40 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 0a  3_open16()]..**.
21b50 2a 2a 20 7b 48 31 32 37 30 33 7d 20 41 20 73 75  ** {H12703} A su
21b60 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74  ccessful invocat
21b70 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ion of [sqlite3_
21b80 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c 69 74 65  open()], [sqlite
21b90 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20  3_open16()],.** 
21ba0 20 20 20 20 20 20 20 20 20 6f 72 20 5b 73 71 6c           or [sql
21bb0 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 5d 20  ite3_open_v2()] 
21bc0 77 72 69 74 65 73 20 61 20 70 6f 69 6e 74 65 72  writes a pointer
21bd0 20 74 6f 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20   to a new.**    
21be0 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20        [database 
21bf0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69 6e 74 6f  connection] into
21c00 20 2a 70 70 44 62 2e 0a 2a 2a 0a 2a 2a 20 7b 48   *ppDb..**.** {H
21c10 31 32 37 30 34 7d 20 54 68 65 20 5b 73 71 6c 69  12704} The [sqli
21c20 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71  te3_open()], [sq
21c30 6c 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c  lite3_open16()],
21c40 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
21c50 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76   [sqlite3_open_v
21c60 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20  2()] interfaces 
21c70 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f  return [SQLITE_O
21c80 4b 5d 20 75 70 6f 6e 20 73 75 63 63 65 73 73 2c  K] upon success,
21c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20  .**          or 
21ca0 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 5b  an appropriate [
21cb0 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20 66  error code] on f
21cc0 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ailure..**.** {H
21cd0 31 32 37 30 36 7d 20 54 68 65 20 64 65 66 61 75  12706} The defau
21ce0 6c 74 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  lt text encoding
21cf0 20 66 6f 72 20 61 20 6e 65 77 20 64 61 74 61 62   for a new datab
21d00 61 73 65 20 63 72 65 61 74 65 64 20 75 73 69 6e  ase created usin
21d10 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  g.**          [s
21d20 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 20 6f  qlite3_open()] o
21d30 72 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  r [sqlite3_open_
21d40 76 32 28 29 5d 20 77 69 6c 6c 20 62 65 20 55 54  v2()] will be UT
21d50 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37  F-8..**.** {H127
21d60 30 37 7d 20 54 68 65 20 64 65 66 61 75 6c 74 20  07} The default 
21d70 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 66 6f  text encoding fo
21d80 72 20 61 20 6e 65 77 20 64 61 74 61 62 61 73 65  r a new database
21d90 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 0a 2a   created using.*
21da0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
21db0 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 20 77 69  te3_open16()] wi
21dc0 6c 6c 20 62 65 20 55 54 46 2d 31 36 2e 0a 2a 2a  ll be UTF-16..**
21dd0 0a 2a 2a 20 7b 48 31 32 37 30 39 7d 20 54 68 65  .** {H12709} The
21de0 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 46   [sqlite3_open(F
21df0 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20 69  ,D)] interface i
21e00 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74 6f 0a  s equivalent to.
21e10 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
21e20 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44  ite3_open_v2(F,D
21e30 2c 47 2c 30 29 5d 20 77 68 65 72 65 20 74 68 65  ,G,0)] where the
21e40 20 47 20 70 61 72 61 6d 65 74 65 72 20 69 73 0a   G parameter is.
21e50 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
21e60 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
21e70 54 45 5d 7c 5b 53 51 4c 49 54 45 5f 4f 50 45 4e  TE]|[SQLITE_OPEN
21e80 5f 43 52 45 41 54 45 5d 2e 0a 2a 2a 0a 2a 2a 20  _CREATE]..**.** 
21e90 7b 48 31 32 37 31 31 7d 20 49 66 20 74 68 65 20  {H12711} If the 
21ea0 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  G parameter to [
21eb0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
21ec0 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e 74 61 69  F,D,G,V)] contai
21ed0 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ns the.**       
21ee0 20 20 20 62 69 74 20 76 61 6c 75 65 20 5b 53 51     bit value [SQ
21ef0 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e  LITE_OPEN_READON
21f00 4c 59 5d 20 74 68 65 6e 20 74 68 65 20 64 61 74  LY] then the dat
21f10 61 62 61 73 65 20 69 73 20 6f 70 65 6e 65 64 0a  abase is opened.
21f20 2a 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 20  **          for 
21f30 72 65 61 64 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2a  reading only..**
21f40 0a 2a 2a 20 7b 48 31 32 37 31 32 7d 20 49 66 20  .** {H12712} If 
21f50 74 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20  the G parameter 
21f60 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  to [sqlite3_open
21f70 5f 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f  _v2(F,D,G,V)] co
21f80 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20  ntains the.**   
21f90 20 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65         bit value
21fa0 20 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45   [SQLITE_OPEN_RE
21fb0 41 44 57 52 49 54 45 5d 20 74 68 65 6e 20 74 68  ADWRITE] then th
21fc0 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  e database is op
21fd0 65 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  ened.**         
21fe0 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77 72 69   reading and wri
21ff0 74 69 6e 67 20 69 66 20 70 6f 73 73 69 62 6c 65  ting if possible
22000 2c 20 6f 72 20 66 6f 72 20 72 65 61 64 69 6e 67  , or for reading
22010 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a 20   only if the.** 
22020 20 20 20 20 20 20 20 20 20 66 69 6c 65 20 69 73           file is
22030 20 77 72 69 74 65 20 70 72 6f 74 65 63 74 65 64   write protected
22040 20 62 79 20 74 68 65 20 6f 70 65 72 61 74 69 6e   by the operatin
22050 67 20 73 79 73 74 65 6d 2e 0a 2a 2a 0a 2a 2a 20  g system..**.** 
22060 7b 48 31 32 37 31 33 7d 20 49 66 20 74 68 65 20  {H12713} If the 
22070 47 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  G parameter to [
22080 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
22090 46 2c 44 2c 47 2c 56 29 5d 20 6f 6d 69 74 73 20  F,D,G,V)] omits 
220a0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
220b0 62 69 74 20 76 61 6c 75 65 20 5b 53 51 4c 49 54  bit value [SQLIT
220c0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 5d 20 61  E_OPEN_CREATE] a
220d0 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nd the database 
220e0 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20  does not.**     
220f0 20 20 20 20 20 70 72 65 76 69 6f 75 73 6c 79 20       previously 
22100 65 78 69 73 74 2c 20 61 6e 20 65 72 72 6f 72 20  exist, an error 
22110 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
22120 2a 2a 20 7b 48 31 32 37 31 34 7d 20 49 66 20 74  ** {H12714} If t
22130 68 65 20 47 20 70 61 72 61 6d 65 74 65 72 20 74  he G parameter t
22140 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f  o [sqlite3_open_
22150 76 32 28 46 2c 44 2c 47 2c 56 29 5d 20 63 6f 6e  v2(F,D,G,V)] con
22160 74 61 69 6e 73 20 74 68 65 0a 2a 2a 20 20 20 20  tains the.**    
22170 20 20 20 20 20 20 62 69 74 20 76 61 6c 75 65 20        bit value 
22180 5b 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45  [SQLITE_OPEN_CRE
22190 41 54 45 5d 20 61 6e 64 20 74 68 65 20 64 61 74  ATE] and the dat
221a0 61 62 61 73 65 20 64 6f 65 73 20 6e 6f 74 0a 2a  abase does not.*
221b0 2a 20 20 20 20 20 20 20 20 20 20 70 72 65 76 69  *          previ
221c0 6f 75 73 6c 79 20 65 78 69 73 74 2c 20 74 68 65  ously exist, the
221d0 6e 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  n an attempt is 
221e0 6d 61 64 65 20 74 6f 20 63 72 65 61 74 65 20 61  made to create a
221f0 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  nd.**          i
22200 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 64 61  nitialize the da
22210 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tabase..**.** {H
22220 31 32 37 31 37 7d 20 49 66 20 74 68 65 20 66 69  12717} If the fi
22230 6c 65 6e 61 6d 65 20 61 72 67 75 6d 65 6e 74 20  lename argument 
22240 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  to [sqlite3_open
22250 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70  ()], [sqlite3_op
22260 65 6e 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20  en16()],.**     
22270 20 20 20 20 20 6f 72 20 5b 73 71 6c 69 74 65 33       or [sqlite3
22280 5f 6f 70 65 6e 5f 76 32 28 29 5d 20 69 73 20 22  _open_v2()] is "
22290 3a 6d 65 6d 6f 72 79 3a 22 2c 20 74 68 65 6e 20  :memory:", then 
222a0 61 6e 20 70 72 69 76 61 74 65 2c 0a 2a 2a 20 20  an private,.**  
222b0 20 20 20 20 20 20 20 20 65 70 68 65 6d 65 72 61          ephemera
222c0 6c 2c 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  l, in-memory dat
222d0 61 62 61 73 65 20 69 73 20 63 72 65 61 74 65 64  abase is created
222e0 20 66 6f 72 20 74 68 65 20 63 6f 6e 6e 65 63 74   for the connect
222f0 69 6f 6e 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  ion..**         
22300 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c 49 54 45   <todo>Is SQLITE
22310 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 53 51 4c  _OPEN_CREATE|SQL
22320 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
22330 54 45 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 20  TE required.**  
22340 20 20 20 20 20 20 20 20 69 6e 20 73 71 6c 69 74          in sqlit
22350 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f 3c 2f 74  e3_open_v2()?</t
22360 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37  odo>.**.** {H127
22370 31 39 7d 20 49 66 20 74 68 65 20 66 69 6c 65 6e  19} If the filen
22380 61 6d 65 20 69 73 20 4e 55 4c 4c 20 6f 72 20 61  ame is NULL or a
22390 6e 20 65 6d 70 74 79 20 73 74 72 69 6e 67 2c 20  n empty string, 
223a0 74 68 65 6e 20 61 20 70 72 69 76 61 74 65 2c 0a  then a private,.
223b0 2a 2a 20 20 20 20 20 20 20 20 20 20 65 70 68 65  **          ephe
223c0 6d 65 72 61 6c 20 6f 6e 2d 64 69 73 6b 20 64 61  meral on-disk da
223d0 74 61 62 61 73 65 20 77 69 6c 6c 20 62 65 20 63  tabase will be c
223e0 72 65 61 74 65 64 2e 0a 2a 2a 20 20 20 20 20 20  reated..**      
223f0 20 20 20 20 3c 74 6f 64 6f 3e 49 73 20 53 51 4c      <todo>Is SQL
22400 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c  ITE_OPEN_CREATE|
22410 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
22420 57 52 49 54 45 20 72 65 71 75 69 72 65 64 0a 2a  WRITE required.*
22430 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 73 71  *          in sq
22440 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 3f  lite3_open_v2()?
22450 3c 2f 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48  </todo>.**.** {H
22460 31 32 37 32 31 7d 20 54 68 65 20 5b 64 61 74 61  12721} The [data
22470 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
22480 20 63 72 65 61 74 65 64 20 62 79 20 5b 73 71 6c   created by [sql
22490 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 46 2c 44  ite3_open_v2(F,D
224a0 2c 47 2c 56 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,G,V)].**       
224b0 20 20 20 77 69 6c 6c 20 75 73 65 20 74 68 65 20     will use the 
224c0 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62  [sqlite3_vfs] ob
224d0 6a 65 63 74 20 69 64 65 6e 74 69 66 69 65 64 20  ject identified 
224e0 62 79 20 74 68 65 20 56 20 70 61 72 61 6d 65 74  by the V paramet
224f0 65 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  er,.**          
22500 6f 72 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b  or the default [
22510 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
22520 65 63 74 20 69 66 20 56 20 69 73 20 61 20 4e 55  ect if V is a NU
22530 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a  LL pointer..**.*
22540 2a 20 7b 48 31 32 37 32 33 7d 20 54 77 6f 20 5b  * {H12723} Two [
22550 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
22560 69 6f 6e 73 5d 20 77 69 6c 6c 20 73 68 61 72 65  ions] will share
22570 20 61 20 63 6f 6d 6d 6f 6e 20 63 61 63 68 65 20   a common cache 
22580 69 66 20 62 6f 74 68 20 77 65 72 65 0a 2a 2a 20  if both were.** 
22590 20 20 20 20 20 20 20 20 20 6f 70 65 6e 65 64 20           opened 
225a0 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 56 46  with the same VF
225b0 53 20 77 68 69 6c 65 20 5b 73 68 61 72 65 64 20  S while [shared 
225c0 63 61 63 68 65 20 6d 6f 64 65 5d 20 77 61 73 20  cache mode] was 
225d0 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a 2a 20 20  enabled and.**  
225e0 20 20 20 20 20 20 20 20 69 66 20 62 6f 74 68 20          if both 
225f0 66 69 6c 65 6e 61 6d 65 73 20 63 6f 6d 70 61 72  filenames compar
22600 65 20 65 71 75 61 6c 20 75 73 69 6e 67 20 6d 65  e equal using me
22610 6d 63 6d 70 28 29 20 61 66 74 65 72 20 68 61 76  mcmp() after hav
22620 69 6e 67 20 62 65 65 6e 0a 2a 2a 20 20 20 20 20  ing been.**     
22630 20 20 20 20 20 70 72 6f 63 65 73 73 65 64 20 62       processed b
22640 79 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  y the [sqlite3_v
22650 66 73 20 7c 20 78 46 75 6c 6c 50 61 74 68 6e 61  fs | xFullPathna
22660 6d 65 5d 20 6d 65 74 68 6f 64 20 6f 66 20 74 68  me] method of th
22670 65 20 56 46 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45  e VFS..*/.SQLITE
22680 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22690 5f 6f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63  _open(.  const c
226a0 68 61 72 20 2a 66 69 6c 65 6e 61 6d 65 2c 20 20  har *filename,  
226b0 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66 69 6c   /* Database fil
226c0 65 6e 61 6d 65 20 28 55 54 46 2d 38 29 20 2a 2f  ename (UTF-8) */
226d0 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70 44  .  sqlite3 **ppD
226e0 62 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55  b          /* OU
226f0 54 3a 20 53 51 4c 69 74 65 20 64 62 20 68 61 6e  T: SQLite db han
22700 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45  dle */.);.SQLITE
22710 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
22720 5f 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74  _open16(.  const
22730 20 76 6f 69 64 20 2a 66 69 6c 65 6e 61 6d 65 2c   void *filename,
22740 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66     /* Database f
22750 69 6c 65 6e 61 6d 65 20 28 55 54 46 2d 31 36 29  ilename (UTF-16)
22760 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a   */.  sqlite3 **
22770 70 70 44 62 20 20 20 20 20 20 20 20 20 20 2f 2a  ppDb          /*
22780 20 4f 55 54 3a 20 53 51 4c 69 74 65 20 64 62 20   OUT: SQLite db 
22790 68 61 6e 64 6c 65 20 2a 2f 0a 29 3b 0a 53 51 4c  handle */.);.SQL
227a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
227b0 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a 20 20 63  te3_open_v2(.  c
227c0 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c 65 6e  onst char *filen
227d0 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74 61 62 61  ame,   /* Databa
227e0 73 65 20 66 69 6c 65 6e 61 6d 65 20 28 55 54 46  se filename (UTF
227f0 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  -8) */.  sqlite3
22800 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20 20 20   **ppDb,        
22810 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69 74 65 20   /* OUT: SQLite 
22820 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 69  db handle */.  i
22830 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
22840 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
22850 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
22860 2a 7a 56 66 73 20 20 20 20 20 20 20 20 2f 2a 20  *zVfs        /* 
22870 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d 6f 64 75  Name of VFS modu
22880 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a 29 3b 0a  le to use */.);.
22890 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
228a0 20 45 72 72 6f 72 20 43 6f 64 65 73 20 41 6e 64   Error Codes And
228b0 20 4d 65 73 73 61 67 65 73 20 7b 48 31 32 38 30   Messages {H1280
228c0 30 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 0a 2a  0} <S60200>.**.*
228d0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 65 72  * The sqlite3_er
228e0 72 63 6f 64 65 28 29 20 69 6e 74 65 72 66 61 63  rcode() interfac
228f0 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e 75  e returns the nu
22900 6d 65 72 69 63 20 5b 72 65 73 75 6c 74 20 63 6f  meric [result co
22910 64 65 5d 20 6f 72 0a 2a 2a 20 5b 65 78 74 65 6e  de] or.** [exten
22920 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d  ded result code]
22930 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20 72 65   for the most re
22940 63 65 6e 74 20 66 61 69 6c 65 64 20 73 71 6c 69  cent failed sqli
22950 74 65 33 5f 2a 20 41 50 49 20 63 61 6c 6c 0a 2a  te3_* API call.*
22960 2a 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  * associated wit
22970 68 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f  h a [database co
22980 6e 6e 65 63 74 69 6f 6e 5d 2e 20 49 66 20 61 20  nnection]. If a 
22990 70 72 69 6f 72 20 41 50 49 20 63 61 6c 6c 20 66  prior API call f
229a0 61 69 6c 65 64 0a 2a 2a 20 62 75 74 20 74 68 65  ailed.** but the
229b0 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 41 50 49   most recent API
229c0 20 63 61 6c 6c 20 73 75 63 63 65 65 64 65 64 2c   call succeeded,
229d0 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
229e0 65 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65  e from.** sqlite
229f0 33 5f 65 72 72 63 6f 64 65 28 29 20 69 73 20 75  3_errcode() is u
22a00 6e 64 65 66 69 6e 65 64 2e 20 20 54 68 65 20 73  ndefined.  The s
22a10 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f  qlite3_extended_
22a20 65 72 72 63 6f 64 65 28 29 0a 2a 2a 20 69 6e 74  errcode().** int
22a30 65 72 66 61 63 65 20 69 73 20 74 68 65 20 73 61  erface is the sa
22a40 6d 65 20 65 78 63 65 70 74 20 74 68 61 74 20 69  me except that i
22a50 74 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73  t always returns
22a60 20 74 68 65 20 0a 2a 2a 20 5b 65 78 74 65 6e 64   the .** [extend
22a70 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 5d 20  ed result code] 
22a80 65 76 65 6e 20 77 68 65 6e 20 65 78 74 65 6e 64  even when extend
22a90 65 64 20 72 65 73 75 6c 74 20 63 6f 64 65 73 20  ed result codes 
22aa0 61 72 65 0a 2a 2a 20 64 69 73 61 62 6c 65 64 2e  are.** disabled.
22ab0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
22ac0 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64 20  e3_errmsg() and 
22ad0 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
22ae0 28 29 20 72 65 74 75 72 6e 20 45 6e 67 6c 69 73  () return Englis
22af0 68 2d 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 74 65  h-language.** te
22b00 78 74 20 74 68 61 74 20 64 65 73 63 72 69 62 65  xt that describe
22b10 73 20 74 68 65 20 65 72 72 6f 72 2c 20 61 73 20  s the error, as 
22b20 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20  either UTF-8 or 
22b30 55 54 46 2d 31 36 20 72 65 73 70 65 63 74 69 76  UTF-16 respectiv
22b40 65 6c 79 2e 0a 2a 2a 20 4d 65 6d 6f 72 79 20 74  ely..** Memory t
22b50 6f 20 68 6f 6c 64 20 74 68 65 20 65 72 72 6f 72  o hold the error
22b60 20 6d 65 73 73 61 67 65 20 73 74 72 69 6e 67 20   message string 
22b70 69 73 20 6d 61 6e 61 67 65 64 20 69 6e 74 65 72  is managed inter
22b80 6e 61 6c 6c 79 2e 0a 2a 2a 20 54 68 65 20 61 70  nally..** The ap
22b90 70 6c 69 63 61 74 69 6f 6e 20 64 6f 65 73 20 6e  plication does n
22ba0 6f 74 20 6e 65 65 64 20 74 6f 20 77 6f 72 72 79  ot need to worry
22bb0 20 61 62 6f 75 74 20 66 72 65 65 69 6e 67 20 74   about freeing t
22bc0 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 48 6f  he result..** Ho
22bd0 77 65 76 65 72 2c 20 74 68 65 20 65 72 72 6f 72  wever, the error
22be0 20 73 74 72 69 6e 67 20 6d 69 67 68 74 20 62 65   string might be
22bf0 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72 20   overwritten or 
22c00 64 65 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a  deallocated by.*
22c10 2a 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  * subsequent cal
22c20 6c 73 20 74 6f 20 6f 74 68 65 72 20 53 51 4c 69  ls to other SQLi
22c30 74 65 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e  te interface fun
22c40 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 57 68  ctions..**.** Wh
22c50 65 6e 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65  en the serialize
22c60 64 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f 64  d [threading mod
22c70 65 5d 20 69 73 20 69 6e 20 75 73 65 2c 20 69 74  e] is in use, it
22c80 20 6d 69 67 68 74 20 62 65 20 74 68 65 0a 2a 2a   might be the.**
22c90 20 63 61 73 65 20 74 68 61 74 20 61 20 73 65 63   case that a sec
22ca0 6f 6e 64 20 65 72 72 6f 72 20 6f 63 63 75 72 73  ond error occurs
22cb0 20 6f 6e 20 61 20 73 65 70 61 72 61 74 65 20 74   on a separate t
22cc0 68 72 65 61 64 20 69 6e 20 62 65 74 77 65 65 6e  hread in between
22cd0 0a 2a 2a 20 74 68 65 20 74 69 6d 65 20 6f 66 20  .** the time of 
22ce0 74 68 65 20 66 69 72 73 74 20 65 72 72 6f 72 20  the first error 
22cf0 61 6e 64 20 74 68 65 20 63 61 6c 6c 20 74 6f 20  and the call to 
22d00 74 68 65 73 65 20 69 6e 74 65 72 66 61 63 65 73  these interfaces
22d10 2e 0a 2a 2a 20 57 68 65 6e 20 74 68 61 74 20 68  ..** When that h
22d20 61 70 70 65 6e 73 2c 20 74 68 65 20 73 65 63 6f  appens, the seco
22d30 6e 64 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65  nd error will be
22d40 20 72 65 70 6f 72 74 65 64 20 73 69 6e 63 65 20   reported since 
22d50 74 68 65 73 65 0a 2a 2a 20 69 6e 74 65 72 66 61  these.** interfa
22d60 63 65 73 20 61 6c 77 61 79 73 20 72 65 70 6f 72  ces always repor
22d70 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  t the most recen
22d80 74 20 72 65 73 75 6c 74 2e 20 20 54 6f 20 61 76  t result.  To av
22d90 6f 69 64 0a 2a 2a 20 74 68 69 73 2c 20 65 61 63  oid.** this, eac
22da0 68 20 74 68 72 65 61 64 20 63 61 6e 20 6f 62 74  h thread can obt
22db0 61 69 6e 20 65 78 63 6c 75 73 69 76 65 20 75 73  ain exclusive us
22dc0 65 20 6f 66 20 74 68 65 20 5b 64 61 74 61 62 61  e of the [databa
22dd0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
22de0 0a 2a 2a 20 62 79 20 69 6e 76 6f 6b 69 6e 67 20  .** by invoking 
22df0 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65  [sqlite3_mutex_e
22e00 6e 74 65 72 5d 28 5b 73 71 6c 69 74 65 33 5f 64  nter]([sqlite3_d
22e10 62 5f 6d 75 74 65 78 5d 28 44 29 29 20 62 65 66  b_mutex](D)) bef
22e20 6f 72 65 20 62 65 67 69 6e 6e 69 6e 67 0a 2a 2a  ore beginning.**
22e30 20 74 6f 20 75 73 65 20 44 20 61 6e 64 20 69 6e   to use D and in
22e40 76 6f 6b 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  voking [sqlite3_
22e50 6d 75 74 65 78 5f 6c 65 61 76 65 5d 28 5b 73 71  mutex_leave]([sq
22e60 6c 69 74 65 33 5f 64 62 5f 6d 75 74 65 78 5d 28  lite3_db_mutex](
22e70 44 29 29 20 61 66 74 65 72 0a 2a 2a 20 61 6c 6c  D)) after.** all
22e80 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 69 6e   calls to the in
22e90 74 65 72 66 61 63 65 73 20 6c 69 73 74 65 64 20  terfaces listed 
22ea0 68 65 72 65 20 61 72 65 20 63 6f 6d 70 6c 65 74  here are complet
22eb0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
22ec0 69 6e 74 65 72 66 61 63 65 20 66 61 69 6c 73 20  interface fails 
22ed0 77 69 74 68 20 53 51 4c 49 54 45 5f 4d 49 53 55  with SQLITE_MISU
22ee0 53 45 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 74  SE, that means t
22ef0 68 65 20 69 6e 74 65 72 66 61 63 65 0a 2a 2a 20  he interface.** 
22f00 77 61 73 20 69 6e 76 6f 6b 65 64 20 69 6e 63 6f  was invoked inco
22f10 72 72 65 63 74 6c 79 20 62 79 20 74 68 65 20 61  rrectly by the a
22f20 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 49 6e 20  pplication.  In 
22f30 74 68 61 74 20 63 61 73 65 2c 20 74 68 65 0a 2a  that case, the.*
22f40 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  * error code and
22f50 20 6d 65 73 73 61 67 65 20 6d 61 79 20 6f 72 20   message may or 
22f60 6d 61 79 20 6e 6f 74 20 62 65 20 73 65 74 2e 0a  may not be set..
22f70 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
22f80 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 31 7d  :.**.** {H12801}
22f90 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 72   The [sqlite3_er
22fa0 72 63 6f 64 65 28 44 29 5d 20 69 6e 74 65 72 66  rcode(D)] interf
22fb0 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20  ace returns the 
22fc0 6e 75 6d 65 72 69 63 0a 2a 2a 20 20 20 20 20 20  numeric.**      
22fd0 20 20 20 20 5b 72 65 73 75 6c 74 20 63 6f 64 65      [result code
22fe0 5d 20 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 72  ] or [extended r
22ff0 65 73 75 6c 74 20 63 6f 64 65 5d 20 66 6f 72 20  esult code] for 
23000 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c  the most recentl
23010 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 61  y.**          fa
23020 69 6c 65 64 20 69 6e 74 65 72 66 61 63 65 20 63  iled interface c
23030 61 6c 6c 20 61 73 73 6f 63 69 61 74 65 64 20 77  all associated w
23040 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61 73  ith the [databas
23050 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2e  e connection] D.
23060 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 38 30 32 7d 20  .**.** {H12802} 
23070 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 65 78 74  The [sqlite3_ext
23080 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 44 29  ended_errcode(D)
23090 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ] interface retu
230a0 72 6e 73 20 74 68 65 20 6e 75 6d 65 72 69 63 0a  rns the numeric.
230b0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 65 78 74  **          [ext
230c0 65 6e 64 65 64 20 72 65 73 75 6c 74 20 63 6f 64  ended result cod
230d0 65 5d 20 66 6f 72 20 74 68 65 20 6d 6f 73 74 20  e] for the most 
230e0 72 65 63 65 6e 74 6c 79 0a 2a 2a 20 20 20 20 20  recently.**     
230f0 20 20 20 20 20 66 61 69 6c 65 64 20 69 6e 74 65       failed inte
23100 72 66 61 63 65 20 63 61 6c 6c 20 61 73 73 6f 63  rface call assoc
23110 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b  iated with the [
23120 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
23130 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ion] D..**.** {H
23140 31 32 38 30 33 7d 20 54 68 65 20 5b 73 71 6c 69  12803} The [sqli
23150 74 65 33 5f 65 72 72 6d 73 67 28 44 29 5d 20 61  te3_errmsg(D)] a
23160 6e 64 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  nd [sqlite3_errm
23170 73 67 31 36 28 44 29 5d 0a 2a 2a 20 20 20 20 20  sg16(D)].**     
23180 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20       interfaces 
23190 72 65 74 75 72 6e 20 45 6e 67 6c 69 73 68 2d 6c  return English-l
231a0 61 6e 67 75 61 67 65 20 74 65 78 74 20 74 68 61  anguage text tha
231b0 74 20 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 20  t describes.**  
231c0 20 20 20 20 20 20 20 20 74 68 65 20 65 72 72 6f          the erro
231d0 72 20 69 6e 20 74 68 65 20 6d 6f 73 74 6c 79 20  r in the mostly 
231e0 72 65 63 65 6e 74 6c 79 20 66 61 69 6c 65 64 20  recently failed 
231f0 69 6e 74 65 72 66 61 63 65 20 63 61 6c 6c 2c 0a  interface call,.
23200 2a 2a 20 20 20 20 20 20 20 20 20 20 65 6e 63 6f  **          enco
23210 64 65 64 20 61 73 20 65 69 74 68 65 72 20 55 54  ded as either UT
23220 46 2d 38 20 6f 72 20 55 54 46 2d 31 36 20 72 65  F-8 or UTF-16 re
23230 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a  spectively..**.*
23240 2a 20 7b 48 31 32 38 30 37 7d 20 54 68 65 20 73  * {H12807} The s
23250 74 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20  trings returned 
23260 62 79 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  by [sqlite3_errm
23270 73 67 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  sg()] and [sqlit
23280 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d 0a 2a  e3_errmsg16()].*
23290 2a 20 20 20 20 20 20 20 20 20 20 61 72 65 20 76  *          are v
232a0 61 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 6e  alid until the n
232b0 65 78 74 20 53 51 4c 69 74 65 20 69 6e 74 65 72  ext SQLite inter
232c0 66 61 63 65 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a  face call..**.**
232d0 20 7b 48 31 32 38 30 38 7d 20 43 61 6c 6c 73 20   {H12808} Calls 
232e0 74 6f 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20  to API routines 
232f0 74 68 61 74 20 64 6f 20 6e 6f 74 20 72 65 74 75  that do not retu
23300 72 6e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  rn an error code
23310 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 28 65 78  .**          (ex
23320 61 6d 70 6c 65 3a 20 5b 73 71 6c 69 74 65 33 5f  ample: [sqlite3_
23330 64 61 74 61 5f 63 6f 75 6e 74 28 29 5d 29 20 64  data_count()]) d
23340 6f 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20  o not.**        
23350 20 20 63 68 61 6e 67 65 20 74 68 65 20 65 72 72    change the err
23360 6f 72 20 63 6f 64 65 20 6f 72 20 6d 65 73 73 61  or code or messa
23370 67 65 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  ge returned by.*
23380 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
23390 74 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 2c 20  te3_errcode()], 
233a0 5b 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65  [sqlite3_extende
233b0 64 5f 65 72 72 63 6f 64 65 28 29 5d 2c 0a 2a 2a  d_errcode()],.**
233c0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
233d0 65 33 5f 65 72 72 6d 73 67 28 29 5d 2c 20 6f 72  e3_errmsg()], or
233e0 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67   [sqlite3_errmsg
233f0 31 36 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  16()]..**.** {H1
23400 32 38 30 39 7d 20 49 6e 74 65 72 66 61 63 65 73  2809} Interfaces
23410 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20 61 73   that are not as
23420 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 61 20  sociated with a 
23430 73 70 65 63 69 66 69 63 0a 2a 2a 20 20 20 20 20  specific.**     
23440 20 20 20 20 20 5b 64 61 74 61 62 61 73 65 20 63       [database c
23450 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 65 78 61 6d  onnection] (exam
23460 70 6c 65 73 3a 0a 2a 2a 20 20 20 20 20 20 20 20  ples:.**        
23470 20 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e    [sqlite3_mprin
23480 74 66 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  tf()] or [sqlite
23490 33 5f 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  3_enable_shared_
234a0 63 61 63 68 65 28 29 5d 0a 2a 2a 20 20 20 20 20  cache()].**     
234b0 20 20 20 20 20 64 6f 20 6e 6f 74 20 63 68 61 6e       do not chan
234c0 67 65 20 74 68 65 20 76 61 6c 75 65 73 20 72 65  ge the values re
234d0 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 20 20 20  turned by.**    
234e0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65        [sqlite3_e
234f0 72 72 63 6f 64 65 28 29 5d 2c 20 5b 73 71 6c 69  rrcode()], [sqli
23500 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72  te3_extended_err
23510 63 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20  code()],.**     
23520 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72       [sqlite3_er
23530 72 6d 73 67 28 29 5d 2c 20 6f 72 20 5b 73 71 6c  rmsg()], or [sql
23540 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 29 5d  ite3_errmsg16()]
23550 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
23560 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63  int sqlite3_errc
23570 6f 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29  ode(sqlite3 *db)
23580 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
23590 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
235a0 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65  d_errcode(sqlite
235b0 33 20 2a 64 62 29 3b 0a 53 51 4c 49 54 45 5f 41  3 *db);.SQLITE_A
235c0 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
235d0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73 71  qlite3_errmsg(sq
235e0 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
235f0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
23600 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36  sqlite3_errmsg16
23610 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
23620 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c  ** CAPI3REF: SQL
23630 20 53 74 61 74 65 6d 65 6e 74 20 4f 62 6a 65 63   Statement Objec
23640 74 20 7b 48 31 33 30 30 30 7d 20 3c 48 31 33 30  t {H13000} <H130
23650 31 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  10>.** KEYWORDS:
23660 20 7b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   {prepared state
23670 6d 65 6e 74 7d 20 7b 70 72 65 70 61 72 65 64 20  ment} {prepared 
23680 73 74 61 74 65 6d 65 6e 74 73 7d 0a 2a 2a 0a 2a  statements}.**.*
23690 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
236a0 20 74 68 69 73 20 6f 62 6a 65 63 74 20 72 65 70   this object rep
236b0 72 65 73 65 6e 74 73 20 61 20 73 69 6e 67 6c 65  resents a single
236c0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a   SQL statement..
236d0 2a 2a 20 54 68 69 73 20 6f 62 6a 65 63 74 20 69  ** This object i
236e0 73 20 76 61 72 69 6f 75 73 6c 79 20 6b 6e 6f 77  s variously know
236f0 6e 20 61 73 20 61 20 22 70 72 65 70 61 72 65 64  n as a "prepared
23700 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20 61   statement" or a
23710 0a 2a 2a 20 22 63 6f 6d 70 69 6c 65 64 20 53 51  .** "compiled SQ
23720 4c 20 73 74 61 74 65 6d 65 6e 74 22 20 6f 72 20  L statement" or 
23730 73 69 6d 70 6c 79 20 61 73 20 61 20 22 73 74 61  simply as a "sta
23740 74 65 6d 65 6e 74 22 2e 0a 2a 2a 0a 2a 2a 20 54  tement"..**.** T
23750 68 65 20 6c 69 66 65 20 6f 66 20 61 20 73 74 61  he life of a sta
23760 74 65 6d 65 6e 74 20 6f 62 6a 65 63 74 20 67 6f  tement object go
23770 65 73 20 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b  es something lik
23780 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 3c 6f  e this:.**.** <o
23790 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 43 72 65 61 74  l>.** <li> Creat
237a0 65 20 74 68 65 20 6f 62 6a 65 63 74 20 75 73 69  e the object usi
237b0 6e 67 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  ng [sqlite3_prep
237c0 61 72 65 5f 76 32 28 29 5d 20 6f 72 20 61 20 72  are_v2()] or a r
237d0 65 6c 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 66  elated.**      f
237e0 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 3c 6c 69 3e  unction..** <li>
237f0 20 42 69 6e 64 20 76 61 6c 75 65 73 20 74 6f 20   Bind values to 
23800 5b 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73  [host parameters
23810 5d 20 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69  ] using the sqli
23820 74 65 33 5f 62 69 6e 64 5f 2a 28 29 0a 2a 2a 20  te3_bind_*().** 
23830 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 2e       interfaces.
23840 0a 2a 2a 20 3c 6c 69 3e 20 52 75 6e 20 74 68 65  .** <li> Run the
23850 20 53 51 4c 20 62 79 20 63 61 6c 6c 69 6e 67 20   SQL by calling 
23860 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
23870 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d   one or more tim
23880 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 52 65 73 65  es..** <li> Rese
23890 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
238a0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 72  using [sqlite3_r
238b0 65 73 65 74 28 29 5d 20 74 68 65 6e 20 67 6f 20  eset()] then go 
238c0 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 74 6f 20  back.**      to 
238d0 73 74 65 70 20 32 2e 20 20 44 6f 20 74 68 69 73  step 2.  Do this
238e0 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 74 69   zero or more ti
238f0 6d 65 73 2e 0a 2a 2a 20 3c 6c 69 3e 20 44 65 73  mes..** <li> Des
23900 74 72 6f 79 20 74 68 65 20 6f 62 6a 65 63 74 20  troy the object 
23910 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  using [sqlite3_f
23920 69 6e 61 6c 69 7a 65 28 29 5d 2e 0a 2a 2a 20 3c  inalize()]..** <
23930 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a 20 52 65 66 65 72  /ol>.**.** Refer
23940 20 74 6f 20 64 6f 63 75 6d 65 6e 74 61 74 69 6f   to documentatio
23950 6e 20 6f 6e 20 69 6e 64 69 76 69 64 75 61 6c 20  n on individual 
23960 6d 65 74 68 6f 64 73 20 61 62 6f 76 65 20 66 6f  methods above fo
23970 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
23980 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a  information..*/.
23990 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73  typedef struct s
239a0 71 6c 69 74 65 33 5f 73 74 6d 74 20 73 71 6c 69  qlite3_stmt sqli
239b0 74 65 33 5f 73 74 6d 74 3b 0a 0a 2f 2a 0a 2a 2a  te3_stmt;../*.**
239c0 20 43 41 50 49 33 52 45 46 3a 20 52 75 6e 2d 74   CAPI3REF: Run-t
239d0 69 6d 65 20 4c 69 6d 69 74 73 20 7b 48 31 32 37  ime Limits {H127
239e0 36 30 7d 20 3c 53 32 30 36 30 30 3e 0a 2a 2a 0a  60} <S20600>.**.
239f0 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66 61 63  ** This interfac
23a00 65 20 61 6c 6c 6f 77 73 20 74 68 65 20 73 69 7a  e allows the siz
23a10 65 20 6f 66 20 76 61 72 69 6f 75 73 20 63 6f 6e  e of various con
23a20 73 74 72 75 63 74 73 20 74 6f 20 62 65 20 6c 69  structs to be li
23a30 6d 69 74 65 64 0a 2a 2a 20 6f 6e 20 61 20 63 6f  mited.** on a co
23a40 6e 6e 65 63 74 69 6f 6e 20 62 79 20 63 6f 6e 6e  nnection by conn
23a50 65 63 74 69 6f 6e 20 62 61 73 69 73 2e 20 20 54  ection basis.  T
23a60 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
23a70 65 72 20 69 73 20 74 68 65 0a 2a 2a 20 5b 64 61  er is the.** [da
23a80 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
23a90 6e 5d 20 77 68 6f 73 65 20 6c 69 6d 69 74 20 69  n] whose limit i
23aa0 73 20 74 6f 20 62 65 20 73 65 74 20 6f 72 20 71  s to be set or q
23ab0 75 65 72 69 65 64 2e 20 20 54 68 65 0a 2a 2a 20  ueried.  The.** 
23ac0 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
23ad0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 20 5b   is one of the [
23ae0 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69 65 73  limit categories
23af0 5d 20 74 68 61 74 20 64 65 66 69 6e 65 20 61 0a  ] that define a.
23b00 2a 2a 20 63 6c 61 73 73 20 6f 66 20 63 6f 6e 73  ** class of cons
23b10 74 72 75 63 74 73 20 74 6f 20 62 65 20 73 69 7a  tructs to be siz
23b20 65 20 6c 69 6d 69 74 65 64 2e 20 20 54 68 65 20  e limited.  The 
23b30 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72 20  third parameter 
23b40 69 73 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c 69  is the.** new li
23b50 6d 69 74 20 66 6f 72 20 74 68 61 74 20 63 6f 6e  mit for that con
23b60 73 74 72 75 63 74 2e 20 20 54 68 65 20 66 75 6e  struct.  The fun
23b70 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
23b80 65 20 6f 6c 64 20 6c 69 6d 69 74 2e 0a 2a 2a 0a  e old limit..**.
23b90 2a 2a 20 49 66 20 74 68 65 20 6e 65 77 20 6c 69  ** If the new li
23ba0 6d 69 74 20 69 73 20 61 20 6e 65 67 61 74 69 76  mit is a negativ
23bb0 65 20 6e 75 6d 62 65 72 2c 20 74 68 65 20 6c 69  e number, the li
23bc0 6d 69 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64  mit is unchanged
23bd0 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6c 69 6d  ..** For the lim
23be0 69 74 20 63 61 74 65 67 6f 72 79 20 6f 66 20 53  it category of S
23bf0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 58 59 5a 20  QLITE_LIMIT_XYZ 
23c00 74 68 65 72 65 20 69 73 20 61 20 68 61 72 64 20  there is a hard 
23c10 75 70 70 65 72 0a 2a 2a 20 62 6f 75 6e 64 20 73  upper.** bound s
23c20 65 74 20 62 79 20 61 20 63 6f 6d 70 69 6c 65 2d  et by a compile-
23c30 74 69 6d 65 20 43 20 70 72 65 70 72 6f 63 65 73  time C preproces
23c40 73 6f 72 20 6d 61 63 72 6f 20 6e 61 6d 65 64 20  sor macro named 
23c50 53 51 4c 49 54 45 5f 4d 41 58 5f 58 59 5a 2e 0a  SQLITE_MAX_XYZ..
23c60 2a 2a 20 28 54 68 65 20 22 5f 4c 49 4d 49 54 5f  ** (The "_LIMIT_
23c70 22 20 69 6e 20 74 68 65 20 6e 61 6d 65 20 69 73  " in the name is
23c80 20 63 68 61 6e 67 65 64 20 74 6f 20 22 5f 4d 41   changed to "_MA
23c90 58 5f 22 2e 29 0a 2a 2a 20 41 74 74 65 6d 70 74  X_".).** Attempt
23ca0 73 20 74 6f 20 69 6e 63 72 65 61 73 65 20 61 20  s to increase a 
23cb0 6c 69 6d 69 74 20 61 62 6f 76 65 20 69 74 73 20  limit above its 
23cc0 68 61 72 64 20 75 70 70 65 72 20 62 6f 75 6e 64  hard upper bound
23cd0 20 61 72 65 0a 2a 2a 20 73 69 6c 65 6e 74 6c 79   are.** silently
23ce0 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20 74 68   truncated to th
23cf0 65 20 68 61 72 64 20 75 70 70 65 72 20 6c 69 6d  e hard upper lim
23d00 69 74 2e 0a 2a 2a 0a 2a 2a 20 52 75 6e 20 74 69  it..**.** Run ti
23d10 6d 65 20 6c 69 6d 69 74 73 20 61 72 65 20 69 6e  me limits are in
23d20 74 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69  tended for use i
23d30 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 74  n applications t
23d40 68 61 74 20 6d 61 6e 61 67 65 0a 2a 2a 20 62 6f  hat manage.** bo
23d50 74 68 20 74 68 65 69 72 20 6f 77 6e 20 69 6e 74  th their own int
23d60 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 20 61  ernal database a
23d70 6e 64 20 61 6c 73 6f 20 64 61 74 61 62 61 73 65  nd also database
23d80 73 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 72  s that are contr
23d90 6f 6c 6c 65 64 0a 2a 2a 20 62 79 20 75 6e 74 72  olled.** by untr
23da0 75 73 74 65 64 20 65 78 74 65 72 6e 61 6c 20 73  usted external s
23db0 6f 75 72 63 65 73 2e 20 20 41 6e 20 65 78 61 6d  ources.  An exam
23dc0 70 6c 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  ple application 
23dd0 6d 69 67 68 74 20 62 65 20 61 0a 2a 2a 20 77 65  might be a.** we
23de0 62 62 72 6f 77 73 65 72 20 74 68 61 74 20 68 61  bbrowser that ha
23df0 73 20 69 74 73 20 6f 77 6e 20 64 61 74 61 62 61  s its own databa
23e00 73 65 73 20 66 6f 72 20 73 74 6f 72 69 6e 67 20  ses for storing 
23e10 68 69 73 74 6f 72 79 20 61 6e 64 0a 2a 2a 20 73  history and.** s
23e20 65 70 61 72 61 74 65 20 64 61 74 61 62 61 73 65  eparate database
23e30 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20  s controlled by 
23e40 4a 61 76 61 53 63 72 69 70 74 20 61 70 70 6c 69  JavaScript appli
23e50 63 61 74 69 6f 6e 73 20 64 6f 77 6e 6c 6f 61 64  cations download
23e60 65 64 0a 2a 2a 20 6f 66 66 20 74 68 65 20 49 6e  ed.** off the In
23e70 74 65 72 6e 65 74 2e 20 20 54 68 65 20 69 6e 74  ternet.  The int
23e80 65 72 6e 61 6c 20 64 61 74 61 62 61 73 65 73 20  ernal databases 
23e90 63 61 6e 20 62 65 20 67 69 76 65 6e 20 74 68 65  can be given the
23ea0 0a 2a 2a 20 6c 61 72 67 65 2c 20 64 65 66 61 75  .** large, defau
23eb0 6c 74 20 6c 69 6d 69 74 73 2e 20 20 44 61 74 61  lt limits.  Data
23ec0 62 61 73 65 73 20 6d 61 6e 61 67 65 64 20 62 79  bases managed by
23ed0 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63 65   external source
23ee0 73 20 63 61 6e 0a 2a 2a 20 62 65 20 67 69 76 65  s can.** be give
23ef0 6e 20 6d 75 63 68 20 73 6d 61 6c 6c 65 72 20 6c  n much smaller l
23f00 69 6d 69 74 73 20 64 65 73 69 67 6e 65 64 20 74  imits designed t
23f10 6f 20 70 72 65 76 65 6e 74 20 61 20 64 65 6e 69  o prevent a deni
23f20 61 6c 20 6f 66 20 73 65 72 76 69 63 65 0a 2a 2a  al of service.**
23f30 20 61 74 74 61 63 6b 2e 20 20 44 65 76 65 6c 6f   attack.  Develo
23f40 70 65 72 73 20 6d 69 67 68 74 20 61 6c 73 6f 20  pers might also 
23f50 77 61 6e 74 20 74 6f 20 75 73 65 20 74 68 65 20  want to use the 
23f60 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74  [sqlite3_set_aut
23f70 68 6f 72 69 7a 65 72 28 29 5d 0a 2a 2a 20 69 6e  horizer()].** in
23f80 74 65 72 66 61 63 65 20 74 6f 20 66 75 72 74 68  terface to furth
23f90 65 72 20 63 6f 6e 74 72 6f 6c 20 75 6e 74 72 75  er control untru
23fa0 73 74 65 64 20 53 51 4c 2e 20 20 54 68 65 20 73  sted SQL.  The s
23fb0 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ize of the datab
23fc0 61 73 65 0a 2a 2a 20 63 72 65 61 74 65 64 20 62  ase.** created b
23fd0 79 20 61 6e 20 75 6e 74 72 75 73 74 65 64 20 73  y an untrusted s
23fe0 63 72 69 70 74 20 63 61 6e 20 62 65 20 63 6f 6e  cript can be con
23ff0 74 61 69 6e 65 64 20 75 73 69 6e 67 20 74 68 65  tained using the
24000 0a 2a 2a 20 5b 6d 61 78 5f 70 61 67 65 5f 63 6f  .** [max_page_co
24010 75 6e 74 5d 20 5b 50 52 41 47 4d 41 5d 2e 0a 2a  unt] [PRAGMA]..*
24020 2a 0a 2a 2a 20 4e 65 77 20 72 75 6e 2d 74 69 6d  *.** New run-tim
24030 65 20 6c 69 6d 69 74 20 63 61 74 65 67 6f 72 69  e limit categori
24040 65 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  es may be added 
24050 69 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73  in future releas
24060 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  es..**.** INVARI
24070 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  ANTS:.**.** {H12
24080 37 36 32 7d 20 41 20 73 75 63 63 65 73 73 66 75  762} A successfu
24090 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
240a0 65 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d  e3_limit(D,C,V)]
240b0 20 77 68 65 72 65 20 56 20 69 73 0a 2a 2a 20 20   where V is.**  
240c0 20 20 20 20 20 20 20 20 70 6f 73 69 74 69 76 65          positive
240d0 20 63 68 61 6e 67 65 73 20 74 68 65 20 6c 69 6d   changes the lim
240e0 69 74 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f  it on the size o
240f0 66 20 63 6f 6e 73 74 72 75 63 74 20 43 20 69 6e  f construct C in
24100 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
24110 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
24120 63 74 69 6f 6e 5d 20 44 20 74 6f 20 74 68 65 20  ction] D to the 
24130 6c 65 73 73 65 72 20 6f 66 20 56 20 61 6e 64 20  lesser of V and 
24140 74 68 65 20 68 61 72 64 20 75 70 70 65 72 0a 2a  the hard upper.*
24150 2a 20 20 20 20 20 20 20 20 20 20 62 6f 75 6e 64  *          bound
24160 20 6f 6e 20 74 68 65 20 73 69 7a 65 20 6f 66 20   on the size of 
24170 43 20 74 68 61 74 20 69 73 20 73 65 74 20 61 74  C that is set at
24180 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a 2a   compile-time..*
24190 2a 0a 2a 2a 20 7b 48 31 32 37 36 36 7d 20 41 20  *.** {H12766} A 
241a0 73 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20  successful call 
241b0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6c 69 6d 69  to [sqlite3_limi
241c0 74 28 44 2c 43 2c 56 29 5d 20 77 68 65 72 65 20  t(D,C,V)] where 
241d0 56 20 69 73 20 6e 65 67 61 74 69 76 65 0a 2a 2a  V is negative.**
241e0 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 73            leaves
241f0 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
24200 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
24210 65 63 74 69 6f 6e 5d 20 44 20 75 6e 63 68 61 6e  ection] D unchan
24220 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 37  ged..**.** {H127
24230 36 39 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c  69} A successful
24240 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
24250 33 5f 6c 69 6d 69 74 28 44 2c 43 2c 56 29 5d 20  3_limit(D,C,V)] 
24260 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 20  returns the.**  
24270 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66          value of
24280 20 74 68 65 20 6c 69 6d 69 74 20 6f 6e 20 74 68   the limit on th
24290 65 20 73 69 7a 65 20 6f 66 20 63 6f 6e 73 74 72  e size of constr
242a0 75 63 74 20 43 20 69 6e 20 74 68 65 0a 2a 2a 20  uct C in the.** 
242b0 20 20 20 20 20 20 20 20 20 5b 64 61 74 61 62 61           [databa
242c0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
242d0 20 61 73 20 69 74 20 77 61 73 20 70 72 69 6f 72   as it was prior
242e0 20 74 6f 20 74 68 65 20 63 61 6c 6c 2e 0a 2a 2f   to the call..*/
242f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
24300 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 73 71  sqlite3_limit(sq
24310 6c 69 74 65 33 2a 2c 20 69 6e 74 20 69 64 2c 20  lite3*, int id, 
24320 69 6e 74 20 6e 65 77 56 61 6c 29 3b 0a 0a 2f 2a  int newVal);../*
24330 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52 75  .** CAPI3REF: Ru
24340 6e 2d 54 69 6d 65 20 4c 69 6d 69 74 20 43 61 74  n-Time Limit Cat
24350 65 67 6f 72 69 65 73 20 7b 48 31 32 37 39 30 7d  egories {H12790}
24360 20 3c 48 31 32 37 36 30 3e 0a 2a 2a 20 4b 45 59   <H12760>.** KEY
24370 57 4f 52 44 53 3a 20 7b 6c 69 6d 69 74 20 63 61  WORDS: {limit ca
24380 74 65 67 6f 72 79 7d 20 7b 6c 69 6d 69 74 20 63  tegory} {limit c
24390 61 74 65 67 6f 72 69 65 73 7d 0a 2a 2a 0a 2a 2a  ategories}.**.**
243a0 20 54 68 65 73 65 20 63 6f 6e 73 74 61 6e 74 73   These constants
243b0 20 64 65 66 69 6e 65 20 76 61 72 69 6f 75 73 20   define various 
243c0 61 73 70 65 63 74 73 20 6f 66 20 61 20 5b 64 61  aspects of a [da
243d0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
243e0 6e 5d 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62  n].** that can b
243f0 65 20 6c 69 6d 69 74 65 64 20 69 6e 20 73 69 7a  e limited in siz
24400 65 20 62 79 20 63 61 6c 6c 73 20 74 6f 20 5b 73  e by calls to [s
24410 71 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 5d 2e  qlite3_limit()].
24420 0a 2a 2a 20 54 68 65 20 6d 65 61 6e 69 6e 67 73  .** The meanings
24430 20 6f 66 20 74 68 65 20 76 61 72 69 6f 75 73 20   of the various 
24440 6c 69 6d 69 74 73 20 61 72 65 20 61 73 20 66 6f  limits are as fo
24450 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 3c 64 6c  llows:.**.** <dl
24460 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  >.** <dt>SQLITE_
24470 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 3c 2f 64 74  LIMIT_LENGTH</dt
24480 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78  >.** <dd>The max
24490 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20 61 6e 79  imum size of any
244a0 20 73 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20   string or BLOB 
244b0 6f 72 20 74 61 62 6c 65 20 72 6f 77 2e 3c 64 64  or table row.<dd
244c0 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49  >.**.** <dt>SQLI
244d0 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e  TE_LIMIT_SQL_LEN
244e0 47 54 48 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  GTH</dt>.** <dd>
244f0 54 68 65 20 6d 61 78 69 6d 75 6d 20 6c 65 6e 67  The maximum leng
24500 74 68 20 6f 66 20 61 6e 20 53 51 4c 20 73 74 61  th of an SQL sta
24510 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a  tement.</dd>.**.
24520 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49  ** <dt>SQLITE_LI
24530 4d 49 54 5f 43 4f 4c 55 4d 4e 3c 2f 64 74 3e 0a  MIT_COLUMN</dt>.
24540 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
24550 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  um number of col
24560 75 6d 6e 73 20 69 6e 20 61 20 74 61 62 6c 65 20  umns in a table 
24570 64 65 66 69 6e 69 74 69 6f 6e 20 6f 72 20 69 6e  definition or in
24580 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74 20 73   the.** result s
24590 65 74 20 6f 66 20 61 20 53 45 4c 45 43 54 20 6f  et of a SELECT o
245a0 72 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75  r the maximum nu
245b0 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
245c0 69 6e 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 6f  in an index.** o
245d0 72 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59  r in an ORDER BY
245e0 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61   or GROUP BY cla
245f0 75 73 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  use.</dd>.**.** 
24600 3c 64 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54  <dt>SQLITE_LIMIT
24610 5f 45 58 50 52 5f 44 45 50 54 48 3c 2f 64 74 3e  _EXPR_DEPTH</dt>
24620 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69  .** <dd>The maxi
24630 6d 75 6d 20 64 65 70 74 68 20 6f 66 20 74 68 65  mum depth of the
24640 20 70 61 72 73 65 20 74 72 65 65 20 6f 6e 20 61   parse tree on a
24650 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 2e 3c 2f  ny expression.</
24660 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51  dd>.**.** <dt>SQ
24670 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f  LITE_LIMIT_COMPO
24680 55 4e 44 5f 53 45 4c 45 43 54 3c 2f 64 74 3e 0a  UND_SELECT</dt>.
24690 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d  ** <dd>The maxim
246a0 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  um number of ter
246b0 6d 73 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64  ms in a compound
246c0 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
246d0 74 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  t.</dd>.**.** <d
246e0 74 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  t>SQLITE_LIMIT_V
246f0 44 42 45 5f 4f 50 3c 2f 64 74 3e 0a 2a 2a 20 3c  DBE_OP</dt>.** <
24700 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d 20 6e  dd>The maximum n
24710 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75 63  umber of instruc
24720 74 69 6f 6e 73 20 69 6e 20 61 20 76 69 72 74 75  tions in a virtu
24730 61 6c 20 6d 61 63 68 69 6e 65 20 70 72 6f 67 72  al machine progr
24740 61 6d 0a 2a 2a 20 75 73 65 64 20 74 6f 20 69 6d  am.** used to im
24750 70 6c 65 6d 65 6e 74 20 61 6e 20 53 51 4c 20 73  plement an SQL s
24760 74 61 74 65 6d 65 6e 74 2e 3c 2f 64 64 3e 0a 2a  tatement.</dd>.*
24770 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  *.** <dt>SQLITE_
24780 4c 49 4d 49 54 5f 46 55 4e 43 54 49 4f 4e 5f 41  LIMIT_FUNCTION_A
24790 52 47 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  RG</dt>.** <dd>T
247a0 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
247b0 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 6f  r of arguments o
247c0 6e 20 61 20 66 75 6e 63 74 69 6f 6e 2e 3c 2f 64  n a function.</d
247d0 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e 53 51 4c  d>.**.** <dt>SQL
247e0 49 54 45 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48  ITE_LIMIT_ATTACH
247f0 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ED</dt>.** <dd>T
24800 68 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65  he maximum numbe
24810 72 20 6f 66 20 61 74 74 61 63 68 65 64 20 64 61  r of attached da
24820 74 61 62 61 73 65 73 2e 3c 2f 64 64 3e 0a 2a 2a  tabases.</dd>.**
24830 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 4c  .** <dt>SQLITE_L
24840 49 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52  IMIT_LIKE_PATTER
24850 4e 5f 4c 45 4e 47 54 48 3c 2f 64 74 3e 0a 2a 2a  N_LENGTH</dt>.**
24860 20 3c 64 64 3e 54 68 65 20 6d 61 78 69 6d 75 6d   <dd>The maximum
24870 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 70   length of the p
24880 61 74 74 65 72 6e 20 61 72 67 75 6d 65 6e 74 20  attern argument 
24890 74 6f 20 74 68 65 20 4c 49 4b 45 20 6f 72 0a 2a  to the LIKE or.*
248a0 2a 20 47 4c 4f 42 20 6f 70 65 72 61 74 6f 72 73  * GLOB operators
248b0 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74  .</dd>.**.** <dt
248c0 3e 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41  >SQLITE_LIMIT_VA
248d0 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52 3c 2f 64  RIABLE_NUMBER</d
248e0 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 65 20 6d 61  t>.** <dd>The ma
248f0 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
24900 76 61 72 69 61 62 6c 65 73 20 69 6e 20 61 6e 20  variables in an 
24910 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 68  SQL statement th
24920 61 74 20 63 61 6e 0a 2a 2a 20 62 65 20 62 6f 75  at can.** be bou
24930 6e 64 2e 3c 2f 64 64 3e 0a 2a 2a 20 3c 2f 64 6c  nd.</dd>.** </dl
24940 3e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  >.*/.#define SQL
24950 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
24960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24970 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51      0.#define SQ
24980 4c 49 54 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c  LITE_LIMIT_SQL_L
24990 45 4e 47 54 48 20 20 20 20 20 20 20 20 20 20 20  ENGTH           
249a0 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53       1.#define S
249b0 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55  QLITE_LIMIT_COLU
249c0 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  MN              
249d0 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20        2.#define 
249e0 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50  SQLITE_LIMIT_EXP
249f0 52 5f 44 45 50 54 48 20 20 20 20 20 20 20 20 20  R_DEPTH         
24a00 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e 65         3.#define
24a10 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f   SQLITE_LIMIT_CO
24a20 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 20 20 20  MPOUND_SELECT   
24a30 20 20 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e          4.#defin
24a40 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56  e SQLITE_LIMIT_V
24a50 44 42 45 5f 4f 50 20 20 20 20 20 20 20 20 20 20  DBE_OP          
24a60 20 20 20 20 20 20 20 20 20 35 0a 23 64 65 66 69           5.#defi
24a70 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  ne SQLITE_LIMIT_
24a80 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20 20 20 20  FUNCTION_ARG    
24a90 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66            6.#def
24aa0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49 54  ine SQLITE_LIMIT
24ab0 5f 41 54 54 41 43 48 45 44 20 20 20 20 20 20 20  _ATTACHED       
24ac0 20 20 20 20 20 20 20 20 20 20 20 37 0a 23 64 65             7.#de
24ad0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d 49  fine SQLITE_LIMI
24ae0 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c  T_LIKE_PATTERN_L
24af0 45 4e 47 54 48 20 20 20 20 20 20 20 38 0a 23 64  ENGTH       8.#d
24b00 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 4d  efine SQLITE_LIM
24b10 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
24b20 45 52 20 20 20 20 20 20 20 20 20 20 20 39 0a 0a  ER           9..
24b30 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
24b40 43 6f 6d 70 69 6c 69 6e 67 20 41 6e 20 53 51 4c  Compiling An SQL
24b50 20 53 74 61 74 65 6d 65 6e 74 20 7b 48 31 33 30   Statement {H130
24b60 31 30 7d 20 3c 53 31 30 30 30 30 3e 0a 2a 2a 20  10} <S10000>.** 
24b70 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20 73  KEYWORDS: {SQL s
24b80 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70 69 6c 65  tatement compile
24b90 72 7d 0a 2a 2a 0a 2a 2a 20 54 6f 20 65 78 65 63  r}.**.** To exec
24ba0 75 74 65 20 61 6e 20 53 51 4c 20 71 75 65 72 79  ute an SQL query
24bb0 2c 20 69 74 20 6d 75 73 74 20 66 69 72 73 74 20  , it must first 
24bc0 62 65 20 63 6f 6d 70 69 6c 65 64 20 69 6e 74 6f  be compiled into
24bd0 20 61 20 62 79 74 65 2d 63 6f 64 65 0a 2a 2a 20   a byte-code.** 
24be0 70 72 6f 67 72 61 6d 20 75 73 69 6e 67 20 6f 6e  program using on
24bf0 65 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69  e of these routi
24c00 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  nes..**.** The f
24c10 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2c 20 22  irst argument, "
24c20 64 62 22 2c 20 69 73 20 61 20 5b 64 61 74 61 62  db", is a [datab
24c30 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
24c40 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 61 0a  obtained from a.
24c50 2a 2a 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f  ** prior call to
24c60 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29   [sqlite3_open()
24c70 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e  ], [sqlite3_open
24c80 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  _v2()] or [sqlit
24c90 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a  e3_open16()]..**
24ca0 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61  .** The second a
24cb0 72 67 75 6d 65 6e 74 2c 20 22 7a 53 71 6c 22 2c  rgument, "zSql",
24cc0 20 69 73 20 74 68 65 20 73 74 61 74 65 6d 65 6e   is the statemen
24cd0 74 20 74 6f 20 62 65 20 63 6f 6d 70 69 6c 65 64  t to be compiled
24ce0 2c 20 65 6e 63 6f 64 65 64 0a 2a 2a 20 61 73 20  , encoded.** as 
24cf0 65 69 74 68 65 72 20 55 54 46 2d 38 20 6f 72 20  either UTF-8 or 
24d00 55 54 46 2d 31 36 2e 20 20 54 68 65 20 73 71 6c  UTF-16.  The sql
24d10 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 61  ite3_prepare() a
24d20 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  nd sqlite3_prepa
24d30 72 65 5f 76 32 28 29 0a 2a 2a 20 69 6e 74 65 72  re_v2().** inter
24d40 66 61 63 65 73 20 75 73 65 20 55 54 46 2d 38 2c  faces use UTF-8,
24d50 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 70 72 65   and sqlite3_pre
24d60 70 61 72 65 31 36 28 29 20 61 6e 64 20 73 71 6c  pare16() and sql
24d70 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76  ite3_prepare16_v
24d80 32 28 29 0a 2a 2a 20 75 73 65 20 55 54 46 2d 31  2().** use UTF-1
24d90 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  6..**.** If the 
24da0 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20 69  nByte argument i
24db0 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  s less than zero
24dc0 2c 20 74 68 65 6e 20 7a 53 71 6c 20 69 73 20 72  , then zSql is r
24dd0 65 61 64 20 75 70 20 74 6f 20 74 68 65 0a 2a 2a  ead up to the.**
24de0 20 66 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d   first zero term
24df0 69 6e 61 74 6f 72 2e 20 49 66 20 6e 42 79 74 65  inator. If nByte
24e00 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65   is non-negative
24e10 2c 20 74 68 65 6e 20 69 74 20 69 73 20 74 68 65  , then it is the
24e20 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 6e 75 6d 62   maximum.** numb
24e30 65 72 20 6f 66 20 20 62 79 74 65 73 20 72 65 61  er of  bytes rea
24e40 64 20 66 72 6f 6d 20 7a 53 71 6c 2e 20 20 57 68  d from zSql.  Wh
24e50 65 6e 20 6e 42 79 74 65 20 69 73 20 6e 6f 6e 2d  en nByte is non-
24e60 6e 65 67 61 74 69 76 65 2c 20 74 68 65 0a 2a 2a  negative, the.**
24e70 20 7a 53 71 6c 20 73 74 72 69 6e 67 20 65 6e 64   zSql string end
24e80 73 20 61 74 20 65 69 74 68 65 72 20 74 68 65 20  s at either the 
24e90 66 69 72 73 74 20 27 5c 30 30 30 27 20 6f 72 20  first '\000' or 
24ea0 27 5c 75 30 30 30 30 27 20 63 68 61 72 61 63 74  '\u0000' charact
24eb0 65 72 20 6f 72 0a 2a 2a 20 74 68 65 20 6e 42 79  er or.** the nBy
24ec0 74 65 2d 74 68 20 62 79 74 65 2c 20 77 68 69 63  te-th byte, whic
24ed0 68 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73  hever comes firs
24ee0 74 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65 72  t. If the caller
24ef0 20 6b 6e 6f 77 73 0a 2a 2a 20 74 68 61 74 20 74   knows.** that t
24f00 68 65 20 73 75 70 70 6c 69 65 64 20 73 74 72 69  he supplied stri
24f10 6e 67 20 69 73 20 6e 75 6c 2d 74 65 72 6d 69 6e  ng is nul-termin
24f20 61 74 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65  ated, then there
24f30 20 69 73 20 61 20 73 6d 61 6c 6c 0a 2a 2a 20 70   is a small.** p
24f40 65 72 66 6f 72 6d 61 6e 63 65 20 61 64 76 61 6e  erformance advan
24f50 74 61 67 65 20 74 6f 20 62 65 20 67 61 69 6e 65  tage to be gaine
24f60 64 20 62 79 20 70 61 73 73 69 6e 67 20 61 6e 20  d by passing an 
24f70 6e 42 79 74 65 20 70 61 72 61 6d 65 74 65 72 20  nByte parameter 
24f80 74 68 61 74 0a 2a 2a 20 69 73 20 65 71 75 61 6c  that.** is equal
24f90 20 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   to the number o
24fa0 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 69  f bytes in the i
24fb0 6e 70 75 74 20 73 74 72 69 6e 67 20 3c 69 3e 69  nput string <i>i
24fc0 6e 63 6c 75 64 69 6e 67 3c 2f 69 3e 0a 2a 2a 20  ncluding</i>.** 
24fd0 74 68 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74  the nul-terminat
24fe0 6f 72 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a 20  or bytes..**.** 
24ff0 2a 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20  *pzTail is made 
25000 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20  to point to the 
25010 66 69 72 73 74 20 62 79 74 65 20 70 61 73 74 20  first byte past 
25020 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
25030 2a 20 66 69 72 73 74 20 53 51 4c 20 73 74 61 74  * first SQL stat
25040 65 6d 65 6e 74 20 69 6e 20 7a 53 71 6c 2e 20 20  ement in zSql.  
25050 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 6f  These routines o
25060 6e 6c 79 20 63 6f 6d 70 69 6c 65 20 74 68 65 20  nly compile the 
25070 66 69 72 73 74 0a 2a 2a 20 73 74 61 74 65 6d 65  first.** stateme
25080 6e 74 20 69 6e 20 7a 53 71 6c 2c 20 73 6f 20 2a  nt in zSql, so *
25090 70 7a 54 61 69 6c 20 69 73 20 6c 65 66 74 20 70  pzTail is left p
250a0 6f 69 6e 74 69 6e 67 20 74 6f 20 77 68 61 74 20  ointing to what 
250b0 72 65 6d 61 69 6e 73 0a 2a 2a 20 75 6e 63 6f 6d  remains.** uncom
250c0 70 69 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 2a 70 70  piled..**.** *pp
250d0 53 74 6d 74 20 69 73 20 6c 65 66 74 20 70 6f 69  Stmt is left poi
250e0 6e 74 69 6e 67 20 74 6f 20 61 20 63 6f 6d 70 69  nting to a compi
250f0 6c 65 64 20 5b 70 72 65 70 61 72 65 64 20 73 74  led [prepared st
25100 61 74 65 6d 65 6e 74 5d 20 74 68 61 74 20 63 61  atement] that ca
25110 6e 20 62 65 0a 2a 2a 20 65 78 65 63 75 74 65 64  n be.** executed
25120 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f   using [sqlite3_
25130 73 74 65 70 28 29 5d 2e 20 20 49 66 20 74 68 65  step()].  If the
25140 72 65 20 69 73 20 61 6e 20 65 72 72 6f 72 2c 20  re is an error, 
25150 2a 70 70 53 74 6d 74 20 69 73 20 73 65 74 0a 2a  *ppStmt is set.*
25160 2a 20 74 6f 20 4e 55 4c 4c 2e 20 20 49 66 20 74  * to NULL.  If t
25170 68 65 20 69 6e 70 75 74 20 74 65 78 74 20 63 6f  he input text co
25180 6e 74 61 69 6e 73 20 6e 6f 20 53 51 4c 20 28 69  ntains no SQL (i
25190 66 20 74 68 65 20 69 6e 70 75 74 20 69 73 20 61  f the input is a
251a0 6e 20 65 6d 70 74 79 0a 2a 2a 20 73 74 72 69 6e  n empty.** strin
251b0 67 20 6f 72 20 61 20 63 6f 6d 6d 65 6e 74 29 20  g or a comment) 
251c0 74 68 65 6e 20 2a 70 70 53 74 6d 74 20 69 73 20  then *ppStmt is 
251d0 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 20  set to NULL..** 
251e0 7b 41 31 33 30 31 38 7d 20 54 68 65 20 63 61 6c  {A13018} The cal
251f0 6c 69 6e 67 20 70 72 6f 63 65 64 75 72 65 20 69  ling procedure i
25200 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f  s responsible fo
25210 72 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63  r deleting the c
25220 6f 6d 70 69 6c 65 64 0a 2a 2a 20 53 51 4c 20 73  ompiled.** SQL s
25230 74 61 74 65 6d 65 6e 74 20 75 73 69 6e 67 20 5b  tatement using [
25240 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
25250 28 29 5d 20 61 66 74 65 72 20 69 74 20 68 61 73  ()] after it has
25260 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 69   finished with i
25270 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63  t..**.** On succ
25280 65 73 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d  ess, [SQLITE_OK]
25290 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f 74   is returned, ot
252a0 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72 6f  herwise an [erro
252b0 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72  r code] is retur
252c0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ned..**.** The s
252d0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
252e0 32 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f  2() and sqlite3_
252f0 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 20 69  prepare16_v2() i
25300 6e 74 65 72 66 61 63 65 73 20 61 72 65 0a 2a 2a  nterfaces are.**
25310 20 72 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72   recommended for
25320 20 61 6c 6c 20 6e 65 77 20 70 72 6f 67 72 61 6d   all new program
25330 73 2e 20 54 68 65 20 74 77 6f 20 6f 6c 64 65 72  s. The two older
25340 20 69 6e 74 65 72 66 61 63 65 73 20 61 72 65 20   interfaces are 
25350 72 65 74 61 69 6e 65 64 0a 2a 2a 20 66 6f 72 20  retained.** for 
25360 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
25370 69 62 69 6c 69 74 79 2c 20 62 75 74 20 74 68 65  ibility, but the
25380 69 72 20 75 73 65 20 69 73 20 64 69 73 63 6f 75  ir use is discou
25390 72 61 67 65 64 2e 0a 2a 2a 20 49 6e 20 74 68 65  raged..** In the
253a0 20 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 73   "v2" interfaces
253b0 2c 20 74 68 65 20 70 72 65 70 61 72 65 64 20 73  , the prepared s
253c0 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 68 61 74  tatement.** that
253d0 20 69 73 20 72 65 74 75 72 6e 65 64 20 28 74 68   is returned (th
253e0 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5d  e [sqlite3_stmt]
253f0 20 6f 62 6a 65 63 74 29 20 63 6f 6e 74 61 69 6e   object) contain
25400 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a  s a copy of the.
25410 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  ** original SQL 
25420 74 65 78 74 2e 20 54 68 69 73 20 63 61 75 73 65  text. This cause
25430 73 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  s the [sqlite3_s
25440 74 65 70 28 29 5d 20 69 6e 74 65 72 66 61 63 65  tep()] interface
25450 20 74 6f 0a 2a 2a 20 62 65 68 61 76 65 20 61 20   to.** behave a 
25460 64 69 66 66 65 72 65 6e 74 6c 79 20 69 6e 20 74  differently in t
25470 77 6f 20 77 61 79 73 3a 0a 2a 2a 0a 2a 2a 20 3c  wo ways:.**.** <
25480 6f 6c 3e 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a 20 49  ol>.** <li>.** I
25490 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  f the database s
254a0 63 68 65 6d 61 20 63 68 61 6e 67 65 73 2c 20 69  chema changes, i
254b0 6e 73 74 65 61 64 20 6f 66 20 72 65 74 75 72 6e  nstead of return
254c0 69 6e 67 20 5b 53 51 4c 49 54 45 5f 53 43 48 45  ing [SQLITE_SCHE
254d0 4d 41 5d 20 61 73 20 69 74 0a 2a 2a 20 61 6c 77  MA] as it.** alw
254e0 61 79 73 20 75 73 65 64 20 74 6f 20 64 6f 2c 20  ays used to do, 
254f0 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 5d  [sqlite3_step()]
25500 20 77 69 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61   will automatica
25510 6c 6c 79 20 72 65 63 6f 6d 70 69 6c 65 20 74 68  lly recompile th
25520 65 20 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65  e SQL.** stateme
25530 6e 74 20 61 6e 64 20 74 72 79 20 74 6f 20 72 75  nt and try to ru
25540 6e 20 69 74 20 61 67 61 69 6e 2e 20 20 49 66 20  n it again.  If 
25550 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 20 63  the schema has c
25560 68 61 6e 67 65 64 20 69 6e 0a 2a 2a 20 61 20 77  hanged in.** a w
25570 61 79 20 74 68 61 74 20 6d 61 6b 65 73 20 74 68  ay that makes th
25580 65 20 73 74 61 74 65 6d 65 6e 74 20 6e 6f 20 6c  e statement no l
25590 6f 6e 67 65 72 20 76 61 6c 69 64 2c 20 5b 73 71  onger valid, [sq
255a0 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 77 69  lite3_step()] wi
255b0 6c 6c 20 73 74 69 6c 6c 0a 2a 2a 20 72 65 74 75  ll still.** retu
255c0 72 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d  rn [SQLITE_SCHEM
255d0 41 5d 2e 20 20 42 75 74 20 75 6e 6c 69 6b 65 20  A].  But unlike 
255e0 74 68 65 20 6c 65 67 61 63 79 20 62 65 68 61 76  the legacy behav
255f0 69 6f 72 2c 20 5b 53 51 4c 49 54 45 5f 53 43 48  ior, [SQLITE_SCH
25600 45 4d 41 5d 20 69 73 0a 2a 2a 20 6e 6f 77 20 61  EMA] is.** now a
25610 20 66 61 74 61 6c 20 65 72 72 6f 72 2e 20 20 43   fatal error.  C
25620 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
25630 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 61 67  prepare_v2()] ag
25640 61 69 6e 20 77 69 6c 6c 20 6e 6f 74 20 6d 61 6b  ain will not mak
25650 65 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20 67  e the.** error g
25660 6f 20 61 77 61 79 2e 20 20 4e 6f 74 65 3a 20 75  o away.  Note: u
25670 73 65 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d  se [sqlite3_errm
25680 73 67 28 29 5d 20 74 6f 20 66 69 6e 64 20 74 68  sg()] to find th
25690 65 20 74 65 78 74 0a 2a 2a 20 6f 66 20 74 68 65  e text.** of the
256a0 20 70 61 72 73 69 6e 67 20 65 72 72 6f 72 20 74   parsing error t
256b0 68 61 74 20 72 65 73 75 6c 74 73 20 69 6e 20 61  hat results in a
256c0 6e 20 5b 53 51 4c 49 54 45 5f 53 43 48 45 4d 41  n [SQLITE_SCHEMA
256d0 5d 20 72 65 74 75 72 6e 2e 0a 2a 2a 20 3c 2f 6c  ] return..** </l
256e0 69 3e 0a 2a 2a 0a 2a 2a 20 3c 6c 69 3e 0a 2a 2a  i>.**.** <li>.**
256f0 20 57 68 65 6e 20 61 6e 20 65 72 72 6f 72 20 6f   When an error o
25700 63 63 75 72 73 2c 20 5b 73 71 6c 69 74 65 33 5f  ccurs, [sqlite3_
25710 73 74 65 70 28 29 5d 20 77 69 6c 6c 20 72 65 74  step()] will ret
25720 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 64  urn one of the d
25730 65 74 61 69 6c 65 64 0a 2a 2a 20 5b 65 72 72 6f  etailed.** [erro
25740 72 20 63 6f 64 65 73 5d 20 6f 72 20 5b 65 78 74  r codes] or [ext
25750 65 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65  ended error code
25760 73 5d 2e 20 20 54 68 65 20 6c 65 67 61 63 79 20  s].  The legacy 
25770 62 65 68 61 76 69 6f 72 20 77 61 73 20 74 68 61  behavior was tha
25780 74 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 73 74  t.** [sqlite3_st
25790 65 70 28 29 5d 20 77 6f 75 6c 64 20 6f 6e 6c 79  ep()] would only
257a0 20 72 65 74 75 72 6e 20 61 20 67 65 6e 65 72 69   return a generi
257b0 63 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  c [SQLITE_ERROR]
257c0 20 72 65 73 75 6c 74 20 63 6f 64 65 0a 2a 2a 20   result code.** 
257d0 61 6e 64 20 79 6f 75 20 77 6f 75 6c 64 20 68 61  and you would ha
257e0 76 65 20 74 6f 20 6d 61 6b 65 20 61 20 73 65 63  ve to make a sec
257f0 6f 6e 64 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c  ond call to [sql
25800 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 69 6e  ite3_reset()] in
25810 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 66 69 6e   order.** to fin
25820 64 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  d the underlying
25830 20 63 61 75 73 65 20 6f 66 20 74 68 65 20 70 72   cause of the pr
25840 6f 62 6c 65 6d 2e 20 57 69 74 68 20 74 68 65 20  oblem. With the 
25850 22 76 32 22 20 70 72 65 70 61 72 65 0a 2a 2a 20  "v2" prepare.** 
25860 69 6e 74 65 72 66 61 63 65 73 2c 20 74 68 65 20  interfaces, the 
25870 75 6e 64 65 72 6c 79 69 6e 67 20 72 65 61 73 6f  underlying reaso
25880 6e 20 66 6f 72 20 74 68 65 20 65 72 72 6f 72 20  n for the error 
25890 69 73 20 72 65 74 75 72 6e 65 64 20 69 6d 6d 65  is returned imme
258a0 64 69 61 74 65 6c 79 2e 0a 2a 2a 20 3c 2f 6c 69  diately..** </li
258b0 3e 0a 2a 2a 20 3c 2f 6f 6c 3e 0a 2a 2a 0a 2a 2a  >.** </ol>.**.**
258c0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
258d0 2a 2a 20 7b 48 31 33 30 31 31 7d 20 54 68 65 20  ** {H13011} The 
258e0 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
258f0 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d 20 61  (db,zSql,...)] a
25900 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  nd.**          [
25910 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
25920 76 32 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d  v2(db,zSql,...)]
25930 20 69 6e 74 65 72 66 61 63 65 73 20 69 6e 74 65   interfaces inte
25940 72 70 72 65 74 20 74 68 65 0a 2a 2a 20 20 20 20  rpret the.**    
25950 20 20 20 20 20 20 74 65 78 74 20 69 6e 20 74 68        text in th
25960 65 69 72 20 7a 53 71 6c 20 70 61 72 61 6d 65 74  eir zSql paramet
25970 65 72 20 61 73 20 55 54 46 2d 38 2e 0a 2a 2a 0a  er as UTF-8..**.
25980 2a 2a 20 7b 48 31 33 30 31 32 7d 20 54 68 65 20  ** {H13012} The 
25990 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
259a0 31 36 28 64 62 2c 7a 53 71 6c 2c 2e 2e 2e 29 5d  16(db,zSql,...)]
259b0 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20   and.**         
259c0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
259d0 65 31 36 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 2e  e16_v2(db,zSql,.
259e0 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65 73 20  ..)] interfaces 
259f0 69 6e 74 65 72 70 72 65 74 20 74 68 65 0a 2a 2a  interpret the.**
25a00 20 20 20 20 20 20 20 20 20 20 74 65 78 74 20 69            text i
25a10 6e 20 74 68 65 69 72 20 7a 53 71 6c 20 70 61 72  n their zSql par
25a20 61 6d 65 74 65 72 20 61 73 20 55 54 46 2d 31 36  ameter as UTF-16
25a30 20 69 6e 20 74 68 65 20 6e 61 74 69 76 65 20 62   in the native b
25a40 79 74 65 20 6f 72 64 65 72 2e 0a 2a 2a 0a 2a 2a  yte order..**.**
25a50 20 7b 48 31 33 30 31 33 7d 20 49 66 20 74 68 65   {H13013} If the
25a60 20 6e 42 79 74 65 20 61 72 67 75 6d 65 6e 74 20   nByte argument 
25a70 74 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  to [sqlite3_prep
25a80 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c 6e  are_v2(db,zSql,n
25a90 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20  Byte,...)].**   
25aa0 20 20 20 20 20 20 20 61 6e 64 20 69 74 73 20 76         and its v
25ab0 61 72 69 61 6e 74 73 20 69 73 20 6c 65 73 73 20  ariants is less 
25ac0 74 68 61 6e 20 7a 65 72 6f 2c 20 74 68 65 20 53  than zero, the S
25ad0 51 4c 20 74 65 78 74 20 69 73 0a 2a 2a 20 20 20  QL text is.**   
25ae0 20 20 20 20 20 20 20 72 65 61 64 20 66 72 6f 6d         read from
25af0 20 7a 53 71 6c 20 69 73 20 72 65 61 64 20 75 70   zSql is read up
25b00 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65   to the first ze
25b10 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a  ro terminator..*
25b20 2a 0a 2a 2a 20 7b 48 31 33 30 31 34 7d 20 49 66  *.** {H13014} If
25b30 20 74 68 65 20 6e 42 79 74 65 20 61 72 67 75 6d   the nByte argum
25b40 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ent to [sqlite3_
25b50 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 7a 53  prepare_v2(db,zS
25b60 71 6c 2c 6e 42 79 74 65 2c 2e 2e 2e 29 5d 0a 2a  ql,nByte,...)].*
25b70 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 69  *          and i
25b80 74 73 20 76 61 72 69 61 6e 74 73 20 69 73 20 6e  ts variants is n
25b90 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65  on-negative, the
25ba0 6e 20 61 74 20 6d 6f 73 74 20 6e 42 79 74 65 73  n at most nBytes
25bb0 20 62 79 74 65 73 20 6f 66 0a 2a 2a 20 20 20 20   bytes of.**    
25bc0 20 20 20 20 20 20 53 51 4c 20 74 65 78 74 20 69        SQL text i
25bd0 73 20 72 65 61 64 20 66 72 6f 6d 20 7a 53 71 6c  s read from zSql
25be0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 30 31 35 7d  ..**.** {H13015}
25bf0 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f 70 72 65   In [sqlite3_pre
25c00 70 61 72 65 5f 76 32 28 64 62 2c 7a 53 71 6c 2c  pare_v2(db,zSql,
25c10 4e 2c 50 2c 70 7a 54 61 69 6c 29 5d 20 61 6e 64  N,P,pzTail)] and
25c20 20 69 74 73 20 76 61 72 69 61 6e 74 73 0a 2a 2a   its variants.**
25c30 20 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65            if the
25c40 20 7a 53 71 6c 20 69 6e 70 75 74 20 74 65 78 74   zSql input text
25c50 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74   contains more t
25c60 68 61 6e 20 6f 6e 65 20 53 51 4c 20 73 74 61 74  han one SQL stat
25c70 65 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20  ement.**        
25c80 20 20 61 6e 64 20 70 7a 54 61 69 6c 20 69 73 20    and pzTail is 
25c90 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 2a  not NULL, then *
25ca0 70 7a 54 61 69 6c 20 69 73 20 6d 61 64 65 20 74  pzTail is made t
25cb0 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a  o point to the.*
25cc0 2a 20 20 20 20 20 20 20 20 20 20 66 69 72 73 74  *          first
25cd0 20 62 79 74 65 20 70 61 73 74 20 74 68 65 20 65   byte past the e
25ce0 6e 64 20 6f 66 20 74 68 65 20 66 69 72 73 74 20  nd of the first 
25cf0 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 69 6e  SQL statement in
25d00 20 7a 53 71 6c 2e 0a 2a 2a 20 20 20 20 20 20 20   zSql..**       
25d10 20 20 20 3c 74 6f 64 6f 3e 57 68 61 74 20 64 6f     <todo>What do
25d20 65 73 20 2a 70 7a 54 61 69 6c 20 70 6f 69 6e 74  es *pzTail point
25d30 20 74 6f 20 69 66 20 74 68 65 72 65 20 69 73 20   to if there is 
25d40 6f 6e 65 20 73 74 61 74 65 6d 65 6e 74 3f 3c 2f  one statement?</
25d50 74 6f 64 6f 3e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  todo>.**.** {H13
25d60 30 31 36 7d 20 41 20 73 75 63 63 65 73 73 66 75  016} A successfu
25d70 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  l call to [sqlit
25d80 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
25d90 2c 7a 53 71 6c 2c 4e 2c 70 70 53 74 6d 74 2c 2e  ,zSql,N,ppStmt,.
25da0 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..)].**         
25db0 20 6f 72 20 6f 6e 65 20 6f 66 20 69 74 73 20 76   or one of its v
25dc0 61 72 69 61 6e 74 73 20 77 72 69 74 65 73 20 69  ariants writes i
25dd0 6e 74 6f 20 2a 70 70 53 74 6d 74 20 61 20 70 6f  nto *ppStmt a po
25de0 69 6e 74 65 72 20 74 6f 20 61 20 6e 65 77 0a 2a  inter to a new.*
25df0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
25e00 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
25e10 6f 72 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  or a pointer to 
25e20 4e 55 4c 4c 20 69 66 20 7a 53 71 6c 20 63 6f 6e  NULL if zSql con
25e30 74 61 69 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  tains.**        
25e40 20 20 6e 6f 74 68 69 6e 67 20 6f 74 68 65 72 20    nothing other 
25e50 74 68 61 6e 20 77 68 69 74 65 73 70 61 63 65 20  than whitespace 
25e60 6f 72 20 63 6f 6d 6d 65 6e 74 73 2e 0a 2a 2a 0a  or comments..**.
25e70 2a 2a 20 7b 48 31 33 30 31 39 7d 20 54 68 65 20  ** {H13019} The 
25e80 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65  [sqlite3_prepare
25e90 5f 76 32 28 29 5d 20 69 6e 74 65 72 66 61 63 65  _v2()] interface
25ea0 20 61 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74   and its variant
25eb0 73 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20  s return.**     
25ec0 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d       [SQLITE_OK]
25ed0 20 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61   or an appropria
25ee0 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  te [error code] 
25ef0 75 70 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2a  upon failure..**
25f00 0a 2a 2a 20 7b 48 31 33 30 32 31 7d 20 42 65 66  .** {H13021} Bef
25f10 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  ore [sqlite3_pre
25f20 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79  pare(db,zSql,nBy
25f30 74 65 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c  te,ppStmt,pzTail
25f40 29 5d 20 6f 72 20 69 74 73 0a 2a 2a 20 20 20 20  )] or its.**    
25f50 20 20 20 20 20 20 76 61 72 69 61 6e 74 73 20 72        variants r
25f60 65 74 75 72 6e 73 20 61 6e 20 65 72 72 6f 72 20  eturns an error 
25f70 28 61 6e 79 20 76 61 6c 75 65 20 6f 74 68 65 72  (any value other
25f80 20 74 68 61 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b   than [SQLITE_OK
25f90 5d 29 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ]),.**          
25fa0 74 68 65 79 20 66 69 72 73 74 20 73 65 74 20 2a  they first set *
25fb0 70 70 53 74 6d 74 20 74 6f 20 4e 55 4c 4c 2e 0a  ppStmt to NULL..
25fc0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
25fd0 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
25fe0 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  e(.  sqlite3 *db
25ff0 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
26000 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  Database handle 
26010 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
26020 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a 20  *zSql,       /* 
26030 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20 55  SQL statement, U
26040 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f 0a  TF-8 encoded */.
26050 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20 20    int nByte,    
26060 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
26070 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20 7a  imum length of z
26080 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f  Sql in bytes. */
26090 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
260a0 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f 55  **ppStmt,  /* OU
260b0 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61 6e  T: Statement han
260c0 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  dle */.  const c
260d0 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20  har **pzTail    
260e0 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65 72   /* OUT: Pointer
260f0 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74 69   to unused porti
26100 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29 3b  on of zSql */.);
26110 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
26120 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
26130 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  v2(.  sqlite3 *d
26140 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
26150 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
26160 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
26170 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
26180 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
26190 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 2a 2f  UTF-8 encoded */
261a0 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20 20  .  int nByte,   
261b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61             /* Ma
261c0 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66 20  ximum length of 
261d0 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a  zSql in bytes. *
261e0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
261f0 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20 4f   **ppStmt,  /* O
26200 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68 61  UT: Statement ha
26210 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
26220 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
26230 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74 65    /* OUT: Pointe
26240 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72 74  r to unused port
26250 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a 29  ion of zSql */.)
26260 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
26270 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
26280 31 36 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  16(.  sqlite3 *d
26290 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  b,            /*
262a0 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
262b0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
262c0 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 2f 2a   *zSql,       /*
262d0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2c 20   SQL statement, 
262e0 55 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 2a  UTF-16 encoded *
262f0 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 2c 20 20  /.  int nByte,  
26300 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
26310 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 6f 66  aximum length of
26320 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
26330 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
26340 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 2f 2a 20  t **ppStmt,  /* 
26350 4f 55 54 3a 20 53 74 61 74 65 6d 65 6e 74 20 68  OUT: Statement h
26360 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74  andle */.  const
26370 20 76 6f 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20   void **pzTail  
26380 20 20 20 2f 2a 20 4f 55 54 3a 20 50 6f 69 6e 74     /* OUT: Point
26390 65 72 20 74 6f 20 75 6e 75 73 65 64 20 70 6f 72  er to unused por
263a0 74 69 6f 6e 20 6f 66 20 7a 53 71 6c 20 2a 2f 0a  tion of zSql */.
263b0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
263c0 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
263d0 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  e16_v2(.  sqlite
263e0 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
263f0 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
26400 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ndle */.  const 
26410 76 6f 69 64 20 2a 7a 53 71 6c 2c 20 20 20 20 20  void *zSql,     
26420 20 20 2f 2a 20 53 51 4c 20 73 74 61 74 65 6d 65    /* SQL stateme
26430 6e 74 2c 20 55 54 46 2d 31 36 20 65 6e 63 6f 64  nt, UTF-16 encod
26440 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ed */.  int nByt
26450 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
26460 2f 2a 20 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74  /* Maximum lengt
26470 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
26480 65 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  es. */.  sqlite3
26490 5f 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20  _stmt **ppStmt, 
264a0 20 2f 2a 20 4f 55 54 3a 20 53 74 61 74 65 6d 65   /* OUT: Stateme
264b0 6e 74 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63  nt handle */.  c
264c0 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70 7a 54 61  onst void **pzTa
264d0 69 6c 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 50  il     /* OUT: P
264e0 6f 69 6e 74 65 72 20 74 6f 20 75 6e 75 73 65 64  ointer to unused
264f0 20 70 6f 72 74 69 6f 6e 20 6f 66 20 7a 53 71 6c   portion of zSql
26500 20 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41   */.);../*.** CA
26510 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 69  PI3REF: Retrievi
26520 6e 67 20 53 74 61 74 65 6d 65 6e 74 20 53 51 4c  ng Statement SQL
26530 20 7b 48 31 33 31 30 30 7d 20 3c 48 31 33 30 30   {H13100} <H1300
26540 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  0>.**.** This in
26550 74 65 72 66 61 63 65 20 63 61 6e 20 62 65 20 75  terface can be u
26560 73 65 64 20 74 6f 20 72 65 74 72 69 65 76 65 20  sed to retrieve 
26570 61 20 73 61 76 65 64 20 63 6f 70 79 20 6f 66 20  a saved copy of 
26580 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20  the original.** 
26590 53 51 4c 20 74 65 78 74 20 75 73 65 64 20 74 6f  SQL text used to
265a0 20 63 72 65 61 74 65 20 61 20 5b 70 72 65 70 61   create a [prepa
265b0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 69  red statement] i
265c0 66 20 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74  f that statement
265d0 20 77 61 73 0a 2a 2a 20 63 6f 6d 70 69 6c 65 64   was.** compiled
265e0 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b 73   using either [s
265f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76  qlite3_prepare_v
26600 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  2()] or [sqlite3
26610 5f 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d  _prepare16_v2()]
26620 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e  ..**.** INVARIAN
26630 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 31 30  TS:.**.** {H1310
26640 31 7d 20 49 66 20 74 68 65 20 5b 70 72 65 70 61  1} If the [prepa
26650 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 70  red statement] p
26660 61 73 73 65 64 20 61 73 20 74 68 65 20 61 72 67  assed as the arg
26670 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20 20 20  ument to.**     
26680 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 71       [sqlite3_sq
26690 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69 6c 65  l()] was compile
266a0 64 20 75 73 69 6e 67 20 65 69 74 68 65 72 20 5b  d using either [
266b0 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
266c0 76 32 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20  v2()] or.**     
266d0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72       [sqlite3_pr
266e0 65 70 61 72 65 31 36 5f 76 32 28 29 5d 2c 20 74  epare16_v2()], t
266f0 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f 73 71 6c  hen [sqlite3_sql
26700 28 29 5d 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20  ()] returns.**  
26710 20 20 20 20 20 20 20 20 61 20 70 6f 69 6e 74 65          a pointe
26720 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72 6d  r to a zero-term
26730 69 6e 61 74 65 64 20 73 74 72 69 6e 67 20 63 6f  inated string co
26740 6e 74 61 69 6e 69 6e 67 20 61 20 55 54 46 2d 38  ntaining a UTF-8
26750 20 72 65 6e 64 65 72 69 6e 67 0a 2a 2a 20 20 20   rendering.**   
26760 20 20 20 20 20 20 20 6f 66 20 74 68 65 20 6f 72         of the or
26770 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74 65  iginal SQL state
26780 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  ment..**.** {H13
26790 31 30 32 7d 20 49 66 20 74 68 65 20 5b 70 72 65  102} If the [pre
267a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
267b0 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 61   passed as the a
267c0 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 20 20  rgument to.**   
267d0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
267e0 73 71 6c 28 29 5d 20 77 61 73 20 63 6f 6d 70 69  sql()] was compi
267f0 6c 65 64 20 75 73 69 6e 67 20 65 69 74 68 65 72  led using either
26800 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
26810 65 28 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20  e()] or.**      
26820 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 70 72 65      [sqlite3_pre
26830 70 61 72 65 31 36 28 29 5d 2c 20 74 68 65 6e 20  pare16()], then 
26840 5b 73 71 6c 69 74 65 33 5f 73 71 6c 28 29 5d 20  [sqlite3_sql()] 
26850 72 65 74 75 72 6e 73 20 61 20 4e 55 4c 4c 20 70  returns a NULL p
26860 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ointer..**.** {H
26870 31 33 31 30 33 7d 20 54 68 65 20 73 74 72 69 6e  13103} The strin
26880 67 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73  g returned by [s
26890 71 6c 69 74 65 33 5f 73 71 6c 28 53 29 5d 20 69  qlite3_sql(S)] i
268a0 73 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 74 68  s valid until th
268b0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
268c0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
268d0 74 5d 20 53 20 69 73 20 64 65 6c 65 74 65 64 20  t] S is deleted 
268e0 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 66  using [sqlite3_f
268f0 69 6e 61 6c 69 7a 65 28 53 29 5d 2e 0a 2a 2f 0a  inalize(S)]..*/.
26900 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
26910 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 73   char *sqlite3_s
26920 71 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  ql(sqlite3_stmt 
26930 2a 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20  *pStmt);../*.** 
26940 43 41 50 49 33 52 45 46 3a 20 44 79 6e 61 6d 69  CAPI3REF: Dynami
26950 63 61 6c 6c 79 20 54 79 70 65 64 20 56 61 6c 75  cally Typed Valu
26960 65 20 4f 62 6a 65 63 74 20 7b 48 31 35 30 30 30  e Object {H15000
26970 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a 20 4b 45  } <S20200>.** KE
26980 59 57 4f 52 44 53 3a 20 7b 70 72 6f 74 65 63 74  YWORDS: {protect
26990 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ed sqlite3_value
269a0 7d 20 7b 75 6e 70 72 6f 74 65 63 74 65 64 20 73  } {unprotected s
269b0 71 6c 69 74 65 33 5f 76 61 6c 75 65 7d 0a 2a 2a  qlite3_value}.**
269c0 0a 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20  .** SQLite uses 
269d0 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  the sqlite3_valu
269e0 65 20 6f 62 6a 65 63 74 20 74 6f 20 72 65 70 72  e object to repr
269f0 65 73 65 6e 74 20 61 6c 6c 20 76 61 6c 75 65 73  esent all values
26a00 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 62 65 20  .** that can be 
26a10 73 74 6f 72 65 64 20 69 6e 20 61 20 64 61 74 61  stored in a data
26a20 62 61 73 65 20 74 61 62 6c 65 2e 20 53 51 4c 69  base table. SQLi
26a30 74 65 20 75 73 65 73 20 64 79 6e 61 6d 69 63 20  te uses dynamic 
26a40 74 79 70 69 6e 67 0a 2a 2a 20 66 6f 72 20 74 68  typing.** for th
26a50 65 20 76 61 6c 75 65 73 20 69 74 20 73 74 6f 72  e values it stor
26a60 65 73 2e 20 56 61 6c 75 65 73 20 73 74 6f 72 65  es. Values store
26a70 64 20 69 6e 20 73 71 6c 69 74 65 33 5f 76 61 6c  d in sqlite3_val
26a80 75 65 20 6f 62 6a 65 63 74 73 0a 2a 2a 20 63 61  ue objects.** ca
26a90 6e 20 62 65 20 69 6e 74 65 67 65 72 73 2c 20 66  n be integers, f
26aa0 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
26ab0 6c 75 65 73 2c 20 73 74 72 69 6e 67 73 2c 20 42  lues, strings, B
26ac0 4c 4f 42 73 2c 20 6f 72 20 4e 55 4c 4c 2e 0a 2a  LOBs, or NULL..*
26ad0 2a 0a 2a 2a 20 41 6e 20 73 71 6c 69 74 65 33 5f  *.** An sqlite3_
26ae0 76 61 6c 75 65 20 6f 62 6a 65 63 74 20 6d 61 79  value object may
26af0 20 62 65 20 65 69 74 68 65 72 20 22 70 72 6f 74   be either "prot
26b00 65 63 74 65 64 22 20 6f 72 20 22 75 6e 70 72 6f  ected" or "unpro
26b10 74 65 63 74 65 64 22 2e 0a 2a 2a 20 53 6f 6d 65  tected"..** Some
26b20 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75   interfaces requ
26b30 69 72 65 20 61 20 70 72 6f 74 65 63 74 65 64 20  ire a protected 
26b40 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 20 20  sqlite3_value.  
26b50 4f 74 68 65 72 20 69 6e 74 65 72 66 61 63 65 73  Other interfaces
26b60 0a 2a 2a 20 77 69 6c 6c 20 61 63 63 65 70 74 20  .** will accept 
26b70 65 69 74 68 65 72 20 61 20 70 72 6f 74 65 63 74  either a protect
26b80 65 64 20 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65  ed or an unprote
26b90 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
26ba0 75 65 2e 0a 2a 2a 20 45 76 65 72 79 20 69 6e 74  ue..** Every int
26bb0 65 72 66 61 63 65 20 74 68 61 74 20 61 63 63 65  erface that acce
26bc0 70 74 73 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  pts sqlite3_valu
26bd0 65 20 61 72 67 75 6d 65 6e 74 73 20 73 70 65 63  e arguments spec
26be0 69 66 69 65 73 0a 2a 2a 20 77 68 65 74 68 65 72  ifies.** whether
26bf0 20 6f 72 20 6e 6f 74 20 69 74 20 72 65 71 75 69   or not it requi
26c00 72 65 73 20 61 20 70 72 6f 74 65 63 74 65 64 20  res a protected 
26c10 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2e 0a 2a  sqlite3_value..*
26c20 2a 0a 2a 2a 20 54 68 65 20 74 65 72 6d 73 20 22  *.** The terms "
26c30 70 72 6f 74 65 63 74 65 64 22 20 61 6e 64 20 22  protected" and "
26c40 75 6e 70 72 6f 74 65 63 74 65 64 22 20 72 65 66  unprotected" ref
26c50 65 72 20 74 6f 20 77 68 65 74 68 65 72 20 6f 72  er to whether or
26c60 20 6e 6f 74 0a 2a 2a 20 61 20 6d 75 74 65 78 20   not.** a mutex 
26c70 69 73 20 68 65 6c 64 2e 20 20 41 20 69 6e 74 65  is held.  A inte
26c80 72 6e 61 6c 20 6d 75 74 65 78 20 69 73 20 68 65  rnal mutex is he
26c90 6c 64 20 66 6f 72 20 61 20 70 72 6f 74 65 63 74  ld for a protect
26ca0 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ed.** sqlite3_va
26cb0 6c 75 65 20 6f 62 6a 65 63 74 20 62 75 74 20 6e  lue object but n
26cc0 6f 20 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20  o mutex is held 
26cd0 66 6f 72 20 61 6e 20 75 6e 70 72 6f 74 65 63 74  for an unprotect
26ce0 65 64 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61  ed.** sqlite3_va
26cf0 6c 75 65 20 6f 62 6a 65 63 74 2e 20 20 49 66 20  lue object.  If 
26d00 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69 6c  SQLite is compil
26d10 65 64 20 74 6f 20 62 65 20 73 69 6e 67 6c 65 2d  ed to be single-
26d20 74 68 72 65 61 64 65 64 0a 2a 2a 20 28 77 69 74  threaded.** (wit
26d30 68 20 5b 53 51 4c 49 54 45 5f 54 48 52 45 41 44  h [SQLITE_THREAD
26d40 53 41 46 45 3d 30 5d 20 61 6e 64 20 77 69 74 68  SAFE=0] and with
26d50 20 5b 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64   [sqlite3_thread
26d60 73 61 66 65 28 29 5d 20 72 65 74 75 72 6e 69 6e  safe()] returnin
26d70 67 20 30 29 0a 2a 2a 20 6f 72 20 69 66 20 53 51  g 0).** or if SQ
26d80 4c 69 74 65 20 69 73 20 72 75 6e 20 69 6e 20 6f  Lite is run in o
26d90 6e 65 20 6f 66 20 72 65 64 75 63 65 64 20 6d 75  ne of reduced mu
26da0 74 65 78 20 6d 6f 64 65 73 20 0a 2a 2a 20 5b 53  tex modes .** [S
26db0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e  QLITE_CONFIG_SIN
26dc0 47 4c 45 54 48 52 45 41 44 5d 20 6f 72 20 5b 53  GLETHREAD] or [S
26dd0 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c  QLITE_CONFIG_MUL
26de0 54 49 54 48 52 45 41 44 5d 0a 2a 2a 20 74 68 65  TITHREAD].** the
26df0 6e 20 74 68 65 72 65 20 69 73 20 6e 6f 20 64 69  n there is no di
26e00 73 74 69 6e 63 74 69 6f 6e 20 62 65 74 77 65 65  stinction betwee
26e10 6e 20 70 72 6f 74 65 63 74 65 64 20 61 6e 64 20  n protected and 
26e20 75 6e 70 72 6f 74 65 63 74 65 64 0a 2a 2a 20 73  unprotected.** s
26e30 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
26e40 65 63 74 73 20 61 6e 64 20 74 68 65 79 20 63 61  ects and they ca
26e50 6e 20 62 65 20 75 73 65 64 20 69 6e 74 65 72 63  n be used interc
26e60 68 61 6e 67 65 61 62 6c 79 2e 20 20 48 6f 77 65  hangeably.  Howe
26e70 76 65 72 2c 0a 2a 2a 20 66 6f 72 20 6d 61 78 69  ver,.** for maxi
26e80 6d 75 6d 20 63 6f 64 65 20 70 6f 72 74 61 62 69  mum code portabi
26e90 6c 69 74 79 20 69 74 20 69 73 20 72 65 63 6f 6d  lity it is recom
26ea0 6d 65 6e 64 65 64 20 74 68 61 74 20 61 70 70 6c  mended that appl
26eb0 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 73 74 69 6c  ications.** stil
26ec0 6c 20 6d 61 6b 65 20 74 68 65 20 64 69 73 74 69  l make the disti
26ed0 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 62  nction between b
26ee0 65 74 77 65 65 6e 20 70 72 6f 74 65 63 74 65 64  etween protected
26ef0 20 61 6e 64 20 75 6e 70 72 6f 74 65 63 74 65 64   and unprotected
26f00 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  .** sqlite3_valu
26f10 65 20 6f 62 6a 65 63 74 73 20 65 76 65 6e 20 77  e objects even w
26f20 68 65 6e 20 6e 6f 74 20 73 74 72 69 63 74 6c 79  hen not strictly
26f30 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a 2a   required..**.**
26f40 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   The sqlite3_val
26f50 75 65 20 6f 62 6a 65 63 74 73 20 74 68 61 74 20  ue objects that 
26f60 61 72 65 20 70 61 73 73 65 64 20 61 73 20 70 61  are passed as pa
26f70 72 61 6d 65 74 65 72 73 20 69 6e 74 6f 20 74 68  rameters into th
26f80 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74  e.** implementat
26f90 69 6f 6e 20 6f 66 20 5b 61 70 70 6c 69 63 61 74  ion of [applicat
26fa0 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51 4c 20  ion-defined SQL 
26fb0 66 75 6e 63 74 69 6f 6e 73 5d 20 61 72 65 20 70  functions] are p
26fc0 72 6f 74 65 63 74 65 64 2e 0a 2a 2a 20 54 68 65  rotected..** The
26fd0 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
26fe0 62 6a 65 63 74 20 72 65 74 75 72 6e 65 64 20 62  bject returned b
26ff0 79 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f  y.** [sqlite3_co
27000 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69 73  lumn_value()] is
27010 20 75 6e 70 72 6f 74 65 63 74 65 64 2e 0a 2a 2a   unprotected..**
27020 20 55 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c   Unprotected sql
27030 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63  ite3_value objec
27040 74 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75  ts may only be u
27050 73 65 64 20 77 69 74 68 0a 2a 2a 20 5b 73 71 6c  sed with.** [sql
27060 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75  ite3_result_valu
27070 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  e()] and [sqlite
27080 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d 2e  3_bind_value()].
27090 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74 65 33  .** The [sqlite3
270a0 5f 76 61 6c 75 65 5f 62 6c 6f 62 20 7c 20 73 71  _value_blob | sq
270b0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
270c0 28 29 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a  ()] family of.**
270d0 20 69 6e 74 65 72 66 61 63 65 73 20 72 65 71 75   interfaces requ
270e0 69 72 65 20 70 72 6f 74 65 63 74 65 64 20 73 71  ire protected sq
270f0 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65  lite3_value obje
27100 63 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  cts..*/.typedef 
27110 73 74 72 75 63 74 20 4d 65 6d 20 73 71 6c 69 74  struct Mem sqlit
27120 65 33 5f 76 61 6c 75 65 3b 0a 0a 2f 2a 0a 2a 2a  e3_value;../*.**
27130 20 43 41 50 49 33 52 45 46 3a 20 53 51 4c 20 46   CAPI3REF: SQL F
27140 75 6e 63 74 69 6f 6e 20 43 6f 6e 74 65 78 74 20  unction Context 
27150 4f 62 6a 65 63 74 20 7b 48 31 36 30 30 31 7d 20  Object {H16001} 
27160 3c 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20200>.**.** T
27170 68 65 20 63 6f 6e 74 65 78 74 20 69 6e 20 77 68  he context in wh
27180 69 63 68 20 61 6e 20 53 51 4c 20 66 75 6e 63 74  ich an SQL funct
27190 69 6f 6e 20 65 78 65 63 75 74 65 73 20 69 73 20  ion executes is 
271a0 73 74 6f 72 65 64 20 69 6e 20 61 6e 0a 2a 2a 20  stored in an.** 
271b0 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
271c0 6f 62 6a 65 63 74 2e 20 20 41 20 70 6f 69 6e 74  object.  A point
271d0 65 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33  er to an sqlite3
271e0 5f 63 6f 6e 74 65 78 74 20 6f 62 6a 65 63 74 0a  _context object.
271f0 2a 2a 20 69 73 20 61 6c 77 61 79 73 20 66 69 72  ** is always fir
27200 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  st parameter to 
27210 5b 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66  [application-def
27220 69 6e 65 64 20 53 51 4c 20 66 75 6e 63 74 69 6f  ined SQL functio
27230 6e 73 5d 2e 0a 2a 2a 20 54 68 65 20 61 70 70 6c  ns]..** The appl
27240 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20  ication-defined 
27250 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
27260 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 6c 6c  lementation will
27270 20 70 61 73 73 20 74 68 69 73 0a 2a 2a 20 70 6f   pass this.** po
27280 69 6e 74 65 72 20 74 68 72 6f 75 67 68 20 69 6e  inter through in
27290 74 6f 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c  to calls to [sql
272a0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 20  ite3_result_int 
272b0 7c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  | sqlite3_result
272c0 28 29 5d 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  ()],.** [sqlite3
272d0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
272e0 78 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f  xt()], [sqlite3_
272f0 75 73 65 72 5f 64 61 74 61 28 29 5d 2c 0a 2a 2a  user_data()],.**
27300 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
27310 74 5f 64 62 5f 68 61 6e 64 6c 65 28 29 5d 2c 20  t_db_handle()], 
27320 5b 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78  [sqlite3_get_aux
27330 64 61 74 61 28 29 5d 2c 0a 2a 2a 20 61 6e 64 2f  data()],.** and/
27340 6f 72 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f  or [sqlite3_set_
27350 61 75 78 64 61 74 61 28 29 5d 2e 0a 2a 2f 0a 74  auxdata()]..*/.t
27360 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
27370 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 73 71  lite3_context sq
27380 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 3b 0a 0a  lite3_context;..
27390 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
273a0 42 69 6e 64 69 6e 67 20 56 61 6c 75 65 73 20 54  Binding Values T
273b0 6f 20 50 72 65 70 61 72 65 64 20 53 74 61 74 65  o Prepared State
273c0 6d 65 6e 74 73 20 7b 48 31 33 35 30 30 7d 20 3c  ments {H13500} <
273d0 53 37 30 33 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f  S70300>.** KEYWO
273e0 52 44 53 3a 20 7b 68 6f 73 74 20 70 61 72 61 6d  RDS: {host param
273f0 65 74 65 72 7d 20 7b 68 6f 73 74 20 70 61 72 61  eter} {host para
27400 6d 65 74 65 72 73 7d 20 7b 68 6f 73 74 20 70 61  meters} {host pa
27410 72 61 6d 65 74 65 72 20 6e 61 6d 65 7d 0a 2a 2a  rameter name}.**
27420 20 4b 45 59 57 4f 52 44 53 3a 20 7b 53 51 4c 20   KEYWORDS: {SQL 
27430 70 61 72 61 6d 65 74 65 72 7d 20 7b 53 51 4c 20  parameter} {SQL 
27440 70 61 72 61 6d 65 74 65 72 73 7d 20 7b 70 61 72  parameters} {par
27450 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67 7d 0a  ameter binding}.
27460 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 53 51 4c  **.** In the SQL
27470 20 73 74 72 69 6e 67 73 20 69 6e 70 75 74 20 74   strings input t
27480 6f 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  o [sqlite3_prepa
27490 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 69 74 73  re_v2()] and its
274a0 20 76 61 72 69 61 6e 74 73 2c 0a 2a 2a 20 6c 69   variants,.** li
274b0 74 65 72 61 6c 73 20 6d 61 79 20 62 65 20 72 65  terals may be re
274c0 70 6c 61 63 65 64 20 62 79 20 61 20 70 61 72 61  placed by a para
274d0 6d 65 74 65 72 20 69 6e 20 6f 6e 65 20 6f 66 20  meter in one of 
274e0 74 68 65 73 65 20 66 6f 72 6d 73 3a 0a 2a 2a 0a  these forms:.**.
274f0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
27500 20 3f 0a 2a 2a 20 3c 6c 69 3e 20 20 3f 4e 4e 4e   ?.** <li>  ?NNN
27510 0a 2a 2a 20 3c 6c 69 3e 20 20 3a 56 56 56 0a 2a  .** <li>  :VVV.*
27520 2a 20 3c 6c 69 3e 20 20 40 56 56 56 0a 2a 2a 20  * <li>  @VVV.** 
27530 3c 6c 69 3e 20 20 24 56 56 56 0a 2a 2a 20 3c 2f  <li>  $VVV.** </
27540 75 6c 3e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65  ul>.**.** In the
27550 20 70 61 72 61 6d 65 74 65 72 20 66 6f 72 6d 73   parameter forms
27560 20 73 68 6f 77 6e 20 61 62 6f 76 65 20 4e 4e 4e   shown above NNN
27570 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 6c   is an integer l
27580 69 74 65 72 61 6c 2c 0a 2a 2a 20 61 6e 64 20 56  iteral,.** and V
27590 56 56 20 69 73 20 61 6e 20 61 6c 70 68 61 2d 6e  VV is an alpha-n
275a0 75 6d 65 72 69 63 20 70 61 72 61 6d 65 74 65 72  umeric parameter
275b0 20 6e 61 6d 65 2e 20 54 68 65 20 76 61 6c 75 65   name. The value
275c0 73 20 6f 66 20 74 68 65 73 65 0a 2a 2a 20 70 61  s of these.** pa
275d0 72 61 6d 65 74 65 72 73 20 28 61 6c 73 6f 20 63  rameters (also c
275e0 61 6c 6c 65 64 20 22 68 6f 73 74 20 70 61 72 61  alled "host para
275f0 6d 65 74 65 72 20 6e 61 6d 65 73 22 20 6f 72 20  meter names" or 
27600 22 53 51 4c 20 70 61 72 61 6d 65 74 65 72 73 22  "SQL parameters"
27610 29 0a 2a 2a 20 63 61 6e 20 62 65 20 73 65 74 20  ).** can be set 
27620 75 73 69 6e 67 20 74 68 65 20 73 71 6c 69 74 65  using the sqlite
27630 33 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69  3_bind_*() routi
27640 6e 65 73 20 64 65 66 69 6e 65 64 20 68 65 72 65  nes defined here
27650 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
27660 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
27670 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 2a  e sqlite3_bind_*
27680 28 29 20 72 6f 75 74 69 6e 65 73 20 69 73 20 61  () routines is a
27690 6c 77 61 79 73 0a 2a 2a 20 61 20 70 6f 69 6e 74  lways.** a point
276a0 65 72 20 74 6f 20 74 68 65 20 5b 73 71 6c 69 74  er to the [sqlit
276b0 65 33 5f 73 74 6d 74 5d 20 6f 62 6a 65 63 74 20  e3_stmt] object 
276c0 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 0a 2a 2a  returned from.**
276d0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
276e0 65 5f 76 32 28 29 5d 20 6f 72 20 69 74 73 20 76  e_v2()] or its v
276f0 61 72 69 61 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 54  ariants..**.** T
27700 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
27710 6e 74 20 69 73 20 74 68 65 20 69 6e 64 65 78 20  nt is the index 
27720 6f 66 20 74 68 65 20 53 51 4c 20 70 61 72 61 6d  of the SQL param
27730 65 74 65 72 20 74 6f 20 62 65 20 73 65 74 2e 0a  eter to be set..
27740 2a 2a 20 54 68 65 20 6c 65 66 74 6d 6f 73 74 20  ** The leftmost 
27750 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 68 61  SQL parameter ha
27760 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20 31 2e  s an index of 1.
27770 20 20 57 68 65 6e 20 74 68 65 20 73 61 6d 65 20    When the same 
27780 6e 61 6d 65 64 0a 2a 2a 20 53 51 4c 20 70 61 72  named.** SQL par
27790 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 20 6d  ameter is used m
277a0 6f 72 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 73  ore than once, s
277b0 65 63 6f 6e 64 20 61 6e 64 20 73 75 62 73 65 71  econd and subseq
277c0 75 65 6e 74 0a 2a 2a 20 6f 63 63 75 72 72 65 6e  uent.** occurren
277d0 63 65 73 20 68 61 76 65 20 74 68 65 20 73 61 6d  ces have the sam
277e0 65 20 69 6e 64 65 78 20 61 73 20 74 68 65 20 66  e index as the f
277f0 69 72 73 74 20 6f 63 63 75 72 72 65 6e 63 65 2e  irst occurrence.
27800 0a 2a 2a 20 54 68 65 20 69 6e 64 65 78 20 66 6f  .** The index fo
27810 72 20 6e 61 6d 65 64 20 70 61 72 61 6d 65 74 65  r named paramete
27820 72 73 20 63 61 6e 20 62 65 20 6c 6f 6f 6b 65 64  rs can be looked
27830 20 75 70 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a   up using the.**
27840 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
27850 61 72 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 29  arameter_index()
27860 5d 20 41 50 49 20 69 66 20 64 65 73 69 72 65 64  ] API if desired
27870 2e 20 20 54 68 65 20 69 6e 64 65 78 0a 2a 2a 20  .  The index.** 
27880 66 6f 72 20 22 3f 4e 4e 4e 22 20 70 61 72 61 6d  for "?NNN" param
27890 65 74 65 72 73 20 69 73 20 74 68 65 20 76 61 6c  eters is the val
278a0 75 65 20 6f 66 20 4e 4e 4e 2e 0a 2a 2a 20 54 68  ue of NNN..** Th
278b0 65 20 4e 4e 4e 20 76 61 6c 75 65 20 6d 75 73 74  e NNN value must
278c0 20 62 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e   be between 1 an
278d0 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 6c  d the [sqlite3_l
278e0 69 6d 69 74 28 29 5d 0a 2a 2a 20 70 61 72 61 6d  imit()].** param
278f0 65 74 65 72 20 5b 53 51 4c 49 54 45 5f 4c 49 4d  eter [SQLITE_LIM
27900 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42  IT_VARIABLE_NUMB
27910 45 52 5d 20 28 64 65 66 61 75 6c 74 20 76 61 6c  ER] (default val
27920 75 65 3a 20 39 39 39 29 2e 0a 2a 2a 0a 2a 2a 20  ue: 999)..**.** 
27930 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  The third argume
27940 6e 74 20 69 73 20 74 68 65 20 76 61 6c 75 65 20  nt is the value 
27950 74 6f 20 62 69 6e 64 20 74 6f 20 74 68 65 20 70  to bind to the p
27960 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
27970 49 6e 20 74 68 6f 73 65 20 72 6f 75 74 69 6e 65  In those routine
27980 73 20 74 68 61 74 20 68 61 76 65 20 61 20 66 6f  s that have a fo
27990 75 72 74 68 20 61 72 67 75 6d 65 6e 74 2c 20 69  urth argument, i
279a0 74 73 20 76 61 6c 75 65 20 69 73 20 74 68 65 0a  ts value is the.
279b0 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74  ** number of byt
279c0 65 73 20 69 6e 20 74 68 65 20 70 61 72 61 6d 65  es in the parame
279d0 74 65 72 2e 20 20 54 6f 20 62 65 20 63 6c 65 61  ter.  To be clea
279e0 72 3a 20 74 68 65 20 76 61 6c 75 65 20 69 73 20  r: the value is 
279f0 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  the.** number of
27a00 20 3c 75 3e 62 79 74 65 73 3c 2f 75 3e 20 69 6e   <u>bytes</u> in
27a10 20 74 68 65 20 76 61 6c 75 65 2c 20 6e 6f 74 20   the value, not 
27a20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
27a30 61 72 61 63 74 65 72 73 2e 0a 2a 2a 20 49 66 20  aracters..** If 
27a40 74 68 65 20 66 6f 75 72 74 68 20 70 61 72 61 6d  the fourth param
27a50 65 74 65 72 20 69 73 20 6e 65 67 61 74 69 76 65  eter is negative
27a60 2c 20 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20  , the length of 
27a70 74 68 65 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a  the string is.**
27a80 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
27a90 79 74 65 73 20 75 70 20 74 6f 20 74 68 65 20 66  ytes up to the f
27aa0 69 72 73 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e  irst zero termin
27ab0 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ator..**.** The 
27ac0 66 69 66 74 68 20 61 72 67 75 6d 65 6e 74 20 74  fifth argument t
27ad0 6f 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62  o sqlite3_bind_b
27ae0 6c 6f 62 28 29 2c 20 73 71 6c 69 74 65 33 5f 62  lob(), sqlite3_b
27af0 69 6e 64 5f 74 65 78 74 28 29 2c 20 61 6e 64 0a  ind_text(), and.
27b00 2a 2a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ** sqlite3_bind_
27b10 74 65 78 74 31 36 28 29 20 69 73 20 61 20 64 65  text16() is a de
27b20 73 74 72 75 63 74 6f 72 20 75 73 65 64 20 74 6f  structor used to
27b30 20 64 69 73 70 6f 73 65 20 6f 66 20 74 68 65 20   dispose of the 
27b40 42 4c 4f 42 20 6f 72 0a 2a 2a 20 73 74 72 69 6e  BLOB or.** strin
27b50 67 20 61 66 74 65 72 20 53 51 4c 69 74 65 20 68  g after SQLite h
27b60 61 73 20 66 69 6e 69 73 68 65 64 20 77 69 74 68  as finished with
27b70 20 69 74 2e 20 49 66 20 74 68 65 20 66 69 66 74   it. If the fift
27b80 68 20 61 72 67 75 6d 65 6e 74 20 69 73 0a 2a 2a  h argument is.**
27b90 20 74 68 65 20 73 70 65 63 69 61 6c 20 76 61 6c   the special val
27ba0 75 65 20 5b 53 51 4c 49 54 45 5f 53 54 41 54 49  ue [SQLITE_STATI
27bb0 43 5d 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20  C], then SQLite 
27bc0 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
27bd0 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
27be0 69 73 20 69 6e 20 73 74 61 74 69 63 2c 20 75 6e  is in static, un
27bf0 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 61 6e  managed space an
27c00 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  d does not need 
27c10 74 6f 20 62 65 20 66 72 65 65 64 2e 0a 2a 2a 20  to be freed..** 
27c20 49 66 20 74 68 65 20 66 69 66 74 68 20 61 72 67  If the fifth arg
27c30 75 6d 65 6e 74 20 68 61 73 20 74 68 65 20 76 61  ument has the va
27c40 6c 75 65 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e  lue [SQLITE_TRAN
27c50 53 49 45 4e 54 5d 2c 20 74 68 65 6e 0a 2a 2a 20  SIENT], then.** 
27c60 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 69 74 73  SQLite makes its
27c70 20 6f 77 6e 20 70 72 69 76 61 74 65 20 63 6f 70   own private cop
27c80 79 20 6f 66 20 74 68 65 20 64 61 74 61 20 69 6d  y of the data im
27c90 6d 65 64 69 61 74 65 6c 79 2c 20 62 65 66 6f 72  mediately, befor
27ca0 65 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74 65 33  e.** the sqlite3
27cb0 5f 62 69 6e 64 5f 2a 28 29 20 72 6f 75 74 69 6e  _bind_*() routin
27cc0 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  e returns..**.**
27cd0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   The sqlite3_bin
27ce0 64 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 72 6f 75  d_zeroblob() rou
27cf0 74 69 6e 65 20 62 69 6e 64 73 20 61 20 42 4c 4f  tine binds a BLO
27d00 42 20 6f 66 20 6c 65 6e 67 74 68 20 4e 20 74 68  B of length N th
27d10 61 74 0a 2a 2a 20 69 73 20 66 69 6c 6c 65 64 20  at.** is filled 
27d20 77 69 74 68 20 7a 65 72 6f 65 73 2e 20 20 41 20  with zeroes.  A 
27d30 7a 65 72 6f 62 6c 6f 62 20 75 73 65 73 20 61 20  zeroblob uses a 
27d40 66 69 78 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20  fixed amount of 
27d50 6d 65 6d 6f 72 79 0a 2a 2a 20 28 6a 75 73 74 20  memory.** (just 
27d60 61 6e 20 69 6e 74 65 67 65 72 20 74 6f 20 68 6f  an integer to ho
27d70 6c 64 20 69 74 73 20 73 69 7a 65 29 20 77 68 69  ld its size) whi
27d80 6c 65 20 69 74 20 69 73 20 62 65 69 6e 67 20 70  le it is being p
27d90 72 6f 63 65 73 73 65 64 2e 0a 2a 2a 20 5a 65 72  rocessed..** Zer
27da0 6f 62 6c 6f 62 73 20 61 72 65 20 69 6e 74 65 6e  oblobs are inten
27db0 64 65 64 20 74 6f 20 73 65 72 76 65 20 61 73 20  ded to serve as 
27dc0 70 6c 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72  placeholders for
27dd0 20 42 4c 4f 42 73 20 77 68 6f 73 65 0a 2a 2a 20   BLOBs whose.** 
27de0 63 6f 6e 74 65 6e 74 20 69 73 20 6c 61 74 65 72  content is later
27df0 20 77 72 69 74 74 65 6e 20 75 73 69 6e 67 0a 2a   written using.*
27e00 2a 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  * [sqlite3_blob_
27e10 6f 70 65 6e 20 7c 20 69 6e 63 72 65 6d 65 6e 74  open | increment
27e20 61 6c 20 42 4c 4f 42 20 49 2f 4f 5d 20 72 6f 75  al BLOB I/O] rou
27e30 74 69 6e 65 73 2e 0a 2a 2a 20 41 20 6e 65 67 61  tines..** A nega
27e40 74 69 76 65 20 76 61 6c 75 65 20 66 6f 72 20 74  tive value for t
27e50 68 65 20 7a 65 72 6f 62 6c 6f 62 20 72 65 73 75  he zeroblob resu
27e60 6c 74 73 20 69 6e 20 61 20 7a 65 72 6f 2d 6c 65  lts in a zero-le
27e70 6e 67 74 68 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a  ngth BLOB..**.**
27e80 20 54 68 65 20 73 71 6c 69 74 65 33 5f 62 69 6e   The sqlite3_bin
27e90 64 5f 2a 28 29 20 72 6f 75 74 69 6e 65 73 20 6d  d_*() routines m
27ea0 75 73 74 20 62 65 20 63 61 6c 6c 65 64 20 61 66  ust be called af
27eb0 74 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ter.** [sqlite3_
27ec0 70 72 65 70 61 72 65 5f 76 32 28 29 5d 20 28 61  prepare_v2()] (a
27ed0 6e 64 20 69 74 73 20 76 61 72 69 61 6e 74 73 29  nd its variants)
27ee0 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   or [sqlite3_res
27ef0 65 74 28 29 5d 20 61 6e 64 0a 2a 2a 20 62 65 66  et()] and.** bef
27f00 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ore [sqlite3_ste
27f10 70 28 29 5d 2e 0a 2a 2a 20 42 69 6e 64 69 6e 67  p()]..** Binding
27f20 73 20 61 72 65 20 6e 6f 74 20 63 6c 65 61 72 65  s are not cleare
27f30 64 20 62 79 20 74 68 65 20 5b 73 71 6c 69 74 65  d by the [sqlite
27f40 33 5f 72 65 73 65 74 28 29 5d 20 72 6f 75 74 69  3_reset()] routi
27f50 6e 65 2e 0a 2a 2a 20 55 6e 62 6f 75 6e 64 20 70  ne..** Unbound p
27f60 61 72 61 6d 65 74 65 72 73 20 61 72 65 20 69 6e  arameters are in
27f70 74 65 72 70 72 65 74 65 64 20 61 73 20 4e 55 4c  terpreted as NUL
27f80 4c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72  L..**.** These r
27f90 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20 5b  outines return [
27fa0 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f 6e 20 73 75  SQLITE_OK] on su
27fb0 63 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f  ccess or an erro
27fc0 72 20 63 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79  r code if.** any
27fd0 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67  thing goes wrong
27fe0 2e 20 20 5b 53 51 4c 49 54 45 5f 52 41 4e 47 45  .  [SQLITE_RANGE
27ff0 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  ] is returned if
28000 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a 2a   the parameter.*
28010 2a 20 69 6e 64 65 78 20 69 73 20 6f 75 74 20 6f  * index is out o
28020 66 20 72 61 6e 67 65 2e 20 20 5b 53 51 4c 49 54  f range.  [SQLIT
28030 45 5f 4e 4f 4d 45 4d 5d 20 69 73 20 72 65 74 75  E_NOMEM] is retu
28040 72 6e 65 64 20 69 66 20 6d 61 6c 6c 6f 63 28 29  rned if malloc()
28050 20 66 61 69 6c 73 2e 0a 2a 2a 20 5b 53 51 4c 49   fails..** [SQLI
28060 54 45 5f 4d 49 53 55 53 45 5d 20 6d 69 67 68 74  TE_MISUSE] might
28070 20 62 65 20 72 65 74 75 72 6e 65 64 20 69 66 20   be returned if 
28080 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  these routines a
28090 72 65 20 63 61 6c 6c 65 64 20 6f 6e 20 61 0a 2a  re called on a.*
280a0 2a 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  * virtual machin
280b0 65 20 74 68 61 74 20 69 73 20 74 68 65 20 77 72  e that is the wr
280c0 6f 6e 67 20 73 74 61 74 65 20 6f 72 20 77 68 69  ong state or whi
280d0 63 68 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ch has already b
280e0 65 65 6e 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a  een finalized..*
280f0 2a 20 44 65 74 65 63 74 69 6f 6e 20 6f 66 20 6d  * Detection of m
28100 69 73 75 73 65 20 69 73 20 75 6e 72 65 6c 69 61  isuse is unrelia
28110 62 6c 65 2e 20 20 41 70 70 6c 69 63 61 74 69 6f  ble.  Applicatio
28120 6e 73 20 73 68 6f 75 6c 64 20 6e 6f 74 20 64 65  ns should not de
28130 70 65 6e 64 0a 2a 2a 20 6f 6e 20 53 51 4c 49 54  pend.** on SQLIT
28140 45 5f 4d 49 53 55 53 45 20 72 65 74 75 72 6e 73  E_MISUSE returns
28150 2e 20 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45  .  SQLITE_MISUSE
28160 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 6f 20   is intended to 
28170 69 6e 64 69 63 61 74 65 20 61 0a 2a 2a 20 61 20  indicate a.** a 
28180 6c 6f 67 69 63 20 65 72 72 6f 72 20 69 6e 20 74  logic error in t
28190 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 20  he application. 
281a0 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73   Future versions
281b0 20 6f 66 20 53 51 4c 69 74 65 20 6d 69 67 68 74   of SQLite might
281c0 0a 2a 2a 20 70 61 6e 69 63 20 72 61 74 68 65 72  .** panic rather
281d0 20 74 68 61 6e 20 72 65 74 75 72 6e 20 53 51 4c   than return SQL
281e0 49 54 45 5f 4d 49 53 55 53 45 2e 0a 2a 2a 0a 2a  ITE_MISUSE..**.*
281f0 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73 71 6c  * See also: [sql
28200 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
28210 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c 0a 2a 2a  ter_count()],.**
28220 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70   [sqlite3_bind_p
28230 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 28 29 5d  arameter_name()]
28240 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62  , and [sqlite3_b
28250 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e  ind_parameter_in
28260 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e  dex()]..**.** IN
28270 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20  VARIANTS:.**.** 
28280 7b 48 31 33 35 30 36 7d 20 54 68 65 20 5b 53 51  {H13506} The [SQ
28290 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 70  L statement comp
282a0 69 6c 65 72 5d 20 72 65 63 6f 67 6e 69 7a 65 73  iler] recognizes
282b0 20 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 20 66   tokens of the f
282c0 6f 72 6d 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  orms.**         
282d0 20 22 3f 22 2c 20 22 3f 4e 4e 4e 22 2c 20 22 24   "?", "?NNN", "$
282e0 56 56 56 22 2c 20 22 3a 56 56 56 22 2c 20 61 6e  VVV", ":VVV", an
282f0 64 20 22 40 56 56 56 22 20 61 73 20 53 51 4c 20  d "@VVV" as SQL 
28300 70 61 72 61 6d 65 74 65 72 73 2c 0a 2a 2a 20 20  parameters,.**  
28310 20 20 20 20 20 20 20 20 77 68 65 72 65 20 4e 4e          where NN
28320 4e 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63  N is any sequenc
28330 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  e of one or more
28340 20 64 69 67 69 74 73 0a 2a 2a 20 20 20 20 20 20   digits.**      
28350 20 20 20 20 61 6e 64 20 77 68 65 72 65 20 56 56      and where VV
28360 56 20 69 73 20 61 6e 79 20 73 65 71 75 65 6e 63  V is any sequenc
28370 65 20 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  e of one or more
28380 20 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a   alphanumeric.**
28390 20 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63            charac
283a0 74 65 72 73 20 6f 72 20 22 3a 3a 22 20 6f 70 74  ters or "::" opt
283b0 69 6f 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64  ionally followed
283c0 20 62 79 20 61 20 73 74 72 69 6e 67 20 63 6f 6e   by a string con
283d0 74 61 69 6e 69 6e 67 0a 2a 2a 20 20 20 20 20 20  taining.**      
283e0 20 20 20 20 6e 6f 20 73 70 61 63 65 73 20 61 6e      no spaces an
283f0 64 20 63 6f 6e 74 61 69 6e 65 64 20 77 69 74 68  d contained with
28400 69 6e 20 70 61 72 65 6e 74 68 65 73 65 73 2e 0a  in parentheses..
28410 2a 2a 0a 2a 2a 20 7b 48 31 33 35 30 39 7d 20 54  **.** {H13509} T
28420 68 65 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65  he initial value
28430 20 6f 66 20 61 6e 20 53 51 4c 20 70 61 72 61 6d   of an SQL param
28440 65 74 65 72 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a  eter is NULL..**
28450 0a 2a 2a 20 7b 48 31 33 35 31 32 7d 20 54 68 65  .** {H13512} The
28460 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 22 3f 22   index of an "?"
28470 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69   SQL parameter i
28480 73 20 6f 6e 65 20 6c 61 72 67 65 72 20 74 68 61  s one larger tha
28490 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  n the.**        
284a0 20 20 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20    largest index 
284b0 6f 66 20 53 51 4c 20 70 61 72 61 6d 65 74 65 72  of SQL parameter
284c0 20 74 6f 20 74 68 65 20 6c 65 66 74 2c 20 6f 72   to the left, or
284d0 20 31 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20   1 if.**        
284e0 20 20 74 68 65 20 22 3f 22 20 69 73 20 74 68 65    the "?" is the
284f0 20 6c 65 66 74 6d 6f 73 74 20 53 51 4c 20 70 61   leftmost SQL pa
28500 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b  rameter..**.** {
28510 48 31 33 35 31 35 7d 20 54 68 65 20 69 6e 64 65  H13515} The inde
28520 78 20 6f 66 20 61 6e 20 22 3f 4e 4e 4e 22 20 53  x of an "?NNN" S
28530 51 4c 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  QL parameter is 
28540 74 68 65 20 69 6e 74 65 67 65 72 20 4e 4e 4e 2e  the integer NNN.
28550 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 31 38 7d 20  .**.** {H13518} 
28560 54 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20  The index of an 
28570 22 3a 56 56 56 22 2c 20 22 24 56 56 56 22 2c 20  ":VVV", "$VVV", 
28580 6f 72 20 22 40 56 56 56 22 20 53 51 4c 20 70 61  or "@VVV" SQL pa
28590 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 20 20  rameter is.**   
285a0 20 20 20 20 20 20 20 74 68 65 20 73 61 6d 65 20         the same 
285b0 61 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  as the index of 
285c0 6c 65 66 74 6d 6f 73 74 20 6f 63 63 75 72 72 65  leftmost occurre
285d0 6e 63 65 73 20 6f 66 20 74 68 65 20 73 61 6d 65  nces of the same
285e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72  .**          par
285f0 61 6d 65 74 65 72 2c 20 6f 72 20 6f 6e 65 20 6d  ameter, or one m
28600 6f 72 65 20 74 68 61 6e 20 74 68 65 20 6c 61 72  ore than the lar
28610 67 65 73 74 20 69 6e 64 65 78 20 6f 76 65 72 20  gest index over 
28620 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  all.**          
28630 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 74 68  parameters to th
28640 65 20 6c 65 66 74 20 69 66 20 74 68 69 73 20 69  e left if this i
28650 73 20 74 68 65 20 66 69 72 73 74 20 6f 63 63 75  s the first occu
28660 72 72 65 6e 63 65 0a 2a 2a 20 20 20 20 20 20 20  rrence.**       
28670 20 20 20 6f 66 20 74 68 69 73 20 70 61 72 61 6d     of this param
28680 65 74 65 72 2c 20 6f 72 20 31 20 69 66 20 74 68  eter, or 1 if th
28690 69 73 20 69 73 20 74 68 65 20 6c 65 66 74 6d 6f  is is the leftmo
286a0 73 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  st parameter..**
286b0 0a 2a 2a 20 7b 48 31 33 35 32 31 7d 20 54 68 65  .** {H13521} The
286c0 20 5b 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20   [SQL statement 
286d0 63 6f 6d 70 69 6c 65 72 5d 20 66 61 69 6c 73 20  compiler] fails 
286e0 77 69 74 68 20 61 6e 20 5b 53 51 4c 49 54 45 5f  with an [SQLITE_
286f0 52 41 4e 47 45 5d 0a 2a 2a 20 20 20 20 20 20 20  RANGE].**       
28700 20 20 20 65 72 72 6f 72 20 69 66 20 74 68 65 20     error if the 
28710 69 6e 64 65 78 20 6f 66 20 61 6e 20 53 51 4c 20  index of an SQL 
28720 70 61 72 61 6d 65 74 65 72 20 69 73 20 6c 65 73  parameter is les
28730 73 20 74 68 61 6e 20 31 0a 2a 2a 20 20 20 20 20  s than 1.**     
28740 20 20 20 20 20 6f 72 20 67 72 65 61 74 65 72 20       or greater 
28750 74 68 61 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  than the compile
28760 2d 74 69 6d 65 20 53 51 4c 49 54 45 5f 4d 41 58  -time SQLITE_MAX
28770 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
28780 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72  .**          par
28790 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ameter..**.** {H
287a0 31 33 35 32 34 7d 20 43 61 6c 6c 73 20 74 6f 20  13524} Calls to 
287b0 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65  [sqlite3_bind_te
287c0 78 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e  xt | sqlite3_bin
287d0 64 28 53 2c 4e 2c 56 2c 2e 2e 2e 29 5d 0a 2a 2a  d(S,N,V,...)].**
287e0 20 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69            associ
287f0 61 74 65 20 74 68 65 20 76 61 6c 75 65 20 56 20  ate the value V 
28800 77 69 74 68 20 61 6c 6c 20 53 51 4c 20 70 61 72  with all SQL par
28810 61 6d 65 74 65 72 73 20 68 61 76 69 6e 67 20 61  ameters having a
28820 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e  n.**          in
28830 64 65 78 20 6f 66 20 4e 20 69 6e 20 74 68 65 20  dex of N in the 
28840 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
28850 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ent] S..**.** {H
28860 31 33 35 32 37 7d 20 43 61 6c 6c 73 20 74 6f 20  13527} Calls to 
28870 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65  [sqlite3_bind_te
28880 78 74 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e  xt | sqlite3_bin
28890 64 28 53 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20  d(S,N,...)].**  
288a0 20 20 20 20 20 20 20 20 6f 76 65 72 72 69 64 65          override
288b0 20 70 72 69 6f 72 20 63 61 6c 6c 73 20 77 69 74   prior calls wit
288c0 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  h the same value
288d0 73 20 6f 66 20 53 20 61 6e 64 20 4e 2e 0a 2a 2a  s of S and N..**
288e0 0a 2a 2a 20 7b 48 31 33 35 33 30 7d 20 42 69 6e  .** {H13530} Bin
288f0 64 69 6e 67 73 20 65 73 74 61 62 6c 69 73 68 65  dings establishe
28900 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 62 69  d by [sqlite3_bi
28910 6e 64 5f 74 65 78 74 20 7c 20 73 71 6c 69 74 65  nd_text | sqlite
28920 33 5f 62 69 6e 64 28 53 2c 2e 2e 2e 29 5d 0a 2a  3_bind(S,...)].*
28930 2a 20 20 20 20 20 20 20 20 20 20 70 65 72 73 69  *          persi
28940 73 74 20 61 63 72 6f 73 73 20 63 61 6c 6c 73 20  st across calls 
28950 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65  to [sqlite3_rese
28960 74 28 53 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  t(S)]..**.** {H1
28970 33 35 33 33 7d 20 49 6e 20 63 61 6c 6c 73 20 74  3533} In calls t
28980 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  o [sqlite3_bind_
28990 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d  blob(S,N,V,L,D)]
289a0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  ,.**          [s
289b0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
289c0 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72  (S,N,V,L,D)], or
289d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
289e0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
289f0 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51  6(S,N,V,L,D)] SQ
28a00 4c 69 74 65 20 62 69 6e 64 73 20 74 68 65 20 66  Lite binds the f
28a10 69 72 73 74 20 4c 0a 2a 2a 20 20 20 20 20 20 20  irst L.**       
28a20 20 20 20 62 79 74 65 73 20 6f 66 20 74 68 65 20     bytes of the 
28a30 42 4c 4f 42 20 6f 72 20 73 74 72 69 6e 67 20 70  BLOB or string p
28a40 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 56 2c 20  ointed to by V, 
28a50 77 68 65 6e 20 4c 0a 2a 2a 20 20 20 20 20 20 20  when L.**       
28a60 20 20 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74 69     is non-negati
28a70 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 33  ve..**.** {H1353
28a80 36 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b  6} In calls to [
28a90 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28aa0 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 6f 72  t(S,N,V,L,D)] or
28ab0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
28ac0 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31  lite3_bind_text1
28ad0 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 53 51  6(S,N,V,L,D)] SQ
28ae0 4c 69 74 65 20 62 69 6e 64 73 20 63 68 61 72 61  Lite binds chara
28af0 63 74 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20  cters.**        
28b00 20 20 66 72 6f 6d 20 56 20 74 68 72 6f 75 67 68    from V through
28b10 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f 20   the first zero 
28b20 63 68 61 72 61 63 74 65 72 20 77 68 65 6e 20 4c  character when L
28b30 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a   is negative..**
28b40 0a 2a 2a 20 7b 48 31 33 35 33 39 7d 20 49 6e 20  .** {H13539} In 
28b50 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
28b60 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 53 2c 4e 2c  3_bind_blob(S,N,
28b70 56 2c 4c 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20  V,L,D)],.**     
28b80 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69       [sqlite3_bi
28b90 6e 64 5f 74 65 78 74 28 53 2c 4e 2c 56 2c 4c 2c  nd_text(S,N,V,L,
28ba0 44 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20  D)], or.**      
28bb0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e      [sqlite3_bin
28bc0 64 5f 74 65 78 74 31 36 28 53 2c 4e 2c 56 2c 4c  d_text16(S,N,V,L
28bd0 2c 44 29 5d 20 77 68 65 6e 20 44 20 69 73 20 74  ,D)] when D is t
28be0 68 65 20 73 70 65 63 69 61 6c 0a 2a 2a 20 20 20  he special.**   
28bf0 20 20 20 20 20 20 20 63 6f 6e 73 74 61 6e 74 20         constant 
28c00 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 2c  [SQLITE_STATIC],
28c10 20 53 51 4c 69 74 65 20 61 73 73 75 6d 65 73 20   SQLite assumes 
28c20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 56  that the value V
28c30 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20  .**          is 
28c40 68 65 6c 64 20 69 6e 20 73 74 61 74 69 63 20 75  held in static u
28c50 6e 6d 61 6e 61 67 65 64 20 73 70 61 63 65 20 74  nmanaged space t
28c60 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 63 68 61  hat will not cha
28c70 6e 67 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nge.**          
28c80 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65 74  during the lifet
28c90 69 6d 65 20 6f 66 20 74 68 65 20 62 69 6e 64 69  ime of the bindi
28ca0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 35 34  ng..**.** {H1354
28cb0 32 7d 20 49 6e 20 63 61 6c 6c 73 20 74 6f 20 5b  2} In calls to [
28cc0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
28cd0 62 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 0a 2a  b(S,N,V,L,D)],.*
28ce0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
28cf0 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 28 53 2c  te3_bind_text(S,
28d00 4e 2c 56 2c 4c 2c 44 29 5d 2c 20 6f 72 0a 2a 2a  N,V,L,D)], or.**
28d10 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
28d20 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28 53  e3_bind_text16(S
28d30 2c 4e 2c 56 2c 4c 2c 44 29 5d 20 77 68 65 6e 20  ,N,V,L,D)] when 
28d40 44 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c  D is the special
28d50 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .**          con
28d60 73 74 61 6e 74 20 5b 53 51 4c 49 54 45 5f 54 52  stant [SQLITE_TR
28d70 41 4e 53 49 45 4e 54 5d 2c 20 74 68 65 20 72 6f  ANSIENT], the ro
28d80 75 74 69 6e 65 20 6d 61 6b 65 73 20 61 0a 2a 2a  utine makes a.**
28d90 20 20 20 20 20 20 20 20 20 20 70 72 69 76 61 74            privat
28da0 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61  e copy of the va
28db0 6c 75 65 20 56 20 62 65 66 6f 72 65 20 69 74 20  lue V before it 
28dc0 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a 20 7b  returns..**.** {
28dd0 48 31 33 35 34 35 7d 20 49 6e 20 63 61 6c 6c 73  H13545} In calls
28de0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e   to [sqlite3_bin
28df0 64 5f 62 6c 6f 62 28 53 2c 4e 2c 56 2c 4c 2c 44  d_blob(S,N,V,L,D
28e00 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
28e10 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65  [sqlite3_bind_te
28e20 78 74 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 2c 20  xt(S,N,V,L,D)], 
28e30 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  or.**          [
28e40 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
28e50 74 31 36 28 53 2c 4e 2c 56 2c 4c 2c 44 29 5d 20  t16(S,N,V,L,D)] 
28e60 77 68 65 6e 20 44 20 69 73 20 61 20 70 6f 69 6e  when D is a poin
28e70 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20  ter to.**       
28e80 20 20 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 53     a function, S
28e90 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 74 68  QLite invokes th
28ea0 61 74 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 64  at function to d
28eb0 65 73 74 72 6f 79 20 74 68 65 0a 2a 2a 20 20 20  estroy the.**   
28ec0 20 20 20 20 20 20 20 76 61 6c 75 65 20 56 20 61         value V a
28ed0 66 74 65 72 20 69 74 20 68 61 73 20 66 69 6e 69  fter it has fini
28ee0 73 68 65 64 20 75 73 69 6e 67 20 74 68 65 20 76  shed using the v
28ef0 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48  alue V..**.** {H
28f00 31 33 35 34 38 7d 20 49 6e 20 63 61 6c 6c 73 20  13548} In calls 
28f10 74 6f 20 5b 73 71 6c 69 74 65 33 5f 62 69 6e 64  to [sqlite3_bind
28f20 5f 7a 65 72 6f 62 6c 6f 62 28 53 2c 4e 2c 56 2c  _zeroblob(S,N,V,
28f30 4c 29 5d 20 74 68 65 20 76 61 6c 75 65 20 62 6f  L)] the value bo
28f40 75 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  und.**          
28f50 69 73 20 61 20 42 4c 4f 42 20 6f 66 20 4c 20 62  is a BLOB of L b
28f60 79 74 65 73 2c 20 6f 72 20 61 20 7a 65 72 6f 2d  ytes, or a zero-
28f70 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69 66 20 4c  length BLOB if L
28f80 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a   is negative..**
28f90 0a 2a 2a 20 7b 48 31 33 35 35 31 7d 20 49 6e 20  .** {H13551} In 
28fa0 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
28fb0 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 53 2c 4e  3_bind_value(S,N
28fc0 2c 56 29 5d 20 74 68 65 20 56 20 61 72 67 75 6d  ,V)] the V argum
28fd0 65 6e 74 20 6d 61 79 0a 2a 2a 20 20 20 20 20 20  ent may.**      
28fe0 20 20 20 20 62 65 20 65 69 74 68 65 72 20 61 20      be either a 
28ff0 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
29000 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
29010 20 6f 72 20 61 6e 0a 2a 2a 20 20 20 20 20 20 20   or an.**       
29020 20 20 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20     [unprotected 
29030 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
29040 62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  bject..*/.SQLITE
29050 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
29060 5f 62 69 6e 64 5f 62 6c 6f 62 28 73 71 6c 69 74  _bind_blob(sqlit
29070 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63  e3_stmt*, int, c
29080 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 20  onst void*, int 
29090 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  n, void(*)(void*
290a0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ));.SQLITE_API i
290b0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
290c0 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33 5f 73  double(sqlite3_s
290d0 74 6d 74 2a 2c 20 69 6e 74 2c 20 64 6f 75 62 6c  tmt*, int, doubl
290e0 65 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  e);.SQLITE_API i
290f0 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
29100 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  int(sqlite3_stmt
29110 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
29120 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
29130 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
29140 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
29150 6e 74 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  nt, sqlite3_int6
29160 34 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  4);.SQLITE_API i
29170 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
29180 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74 6d  null(sqlite3_stm
29190 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  t*, int);.SQLITE
291a0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
291b0 5f 62 69 6e 64 5f 74 65 78 74 28 73 71 6c 69 74  _bind_text(sqlit
291c0 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 2c 20 63  e3_stmt*, int, c
291d0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 20  onst char*, int 
291e0 6e 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  n, void(*)(void*
291f0 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ));.SQLITE_API i
29200 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  nt sqlite3_bind_
29210 74 65 78 74 31 36 28 73 71 6c 69 74 65 33 5f 73  text16(sqlite3_s
29220 74 6d 74 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74  tmt*, int, const
29230 20 76 6f 69 64 2a 2c 20 69 6e 74 2c 20 76 6f 69   void*, int, voi
29240 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51  d(*)(void*));.SQ
29250 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
29260 69 74 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28  ite3_bind_value(
29270 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
29280 6e 74 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  nt, const sqlite
29290 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54  3_value*);.SQLIT
292a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
292b0 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28  3_bind_zeroblob(
292c0 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
292d0 6e 74 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a  nt, int n);../*.
292e0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 75 6d  ** CAPI3REF: Num
292f0 62 65 72 20 4f 66 20 53 51 4c 20 50 61 72 61 6d  ber Of SQL Param
29300 65 74 65 72 73 20 7b 48 31 33 36 30 30 7d 20 3c  eters {H13600} <
29310 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S70300>.**.** Th
29320 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62  is routine can b
29330 65 20 75 73 65 64 20 74 6f 20 66 69 6e 64 20 74  e used to find t
29340 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 5b 53 51  he number of [SQ
29350 4c 20 70 61 72 61 6d 65 74 65 72 73 5d 0a 2a 2a  L parameters].**
29360 20 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20   in a [prepared 
29370 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20 53 51 4c  statement].  SQL
29380 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65 20   parameters are 
29390 74 6f 6b 65 6e 73 20 6f 66 20 74 68 65 0a 2a 2a  tokens of the.**
293a0 20 66 6f 72 6d 20 22 3f 22 2c 20 22 3f 4e 4e 4e   form "?", "?NNN
293b0 22 2c 20 22 3a 41 41 41 22 2c 20 22 24 41 41 41  ", ":AAA", "$AAA
293c0 22 2c 20 6f 72 20 22 40 41 41 41 22 20 74 68 61  ", or "@AAA" tha
293d0 74 20 73 65 72 76 65 20 61 73 0a 2a 2a 20 70 6c  t serve as.** pl
293e0 61 63 65 68 6f 6c 64 65 72 73 20 66 6f 72 20 76  aceholders for v
293f0 61 6c 75 65 73 20 74 68 61 74 20 61 72 65 20 5b  alues that are [
29400 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
29410 62 20 7c 20 62 6f 75 6e 64 5d 0a 2a 2a 20 74 6f  b | bound].** to
29420 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 73 20   the parameters 
29430 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2e  at a later time.
29440 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
29450 69 6e 65 20 61 63 74 75 61 6c 6c 79 20 72 65 74  ine actually ret
29460 75 72 6e 73 20 74 68 65 20 69 6e 64 65 78 20 6f  urns the index o
29470 66 20 74 68 65 20 6c 61 72 67 65 73 74 20 28 72  f the largest (r
29480 69 67 68 74 6d 6f 73 74 29 0a 2a 2a 20 70 61 72  ightmost).** par
29490 61 6d 65 74 65 72 2e 20 46 6f 72 20 61 6c 6c 20  ameter. For all 
294a0 66 6f 72 6d 73 20 65 78 63 65 70 74 20 3f 4e 4e  forms except ?NN
294b0 4e 2c 20 74 68 69 73 20 77 69 6c 6c 20 63 6f 72  N, this will cor
294c0 72 65 73 70 6f 6e 64 20 74 6f 20 74 68 65 0a 2a  respond to the.*
294d0 2a 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e 69 71  * number of uniq
294e0 75 65 20 70 61 72 61 6d 65 74 65 72 73 2e 20 20  ue parameters.  
294f0 49 66 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66  If parameters of
29500 20 74 68 65 20 3f 4e 4e 4e 20 61 72 65 20 75 73   the ?NNN are us
29510 65 64 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 61 79  ed,.** there may
29520 20 62 65 20 67 61 70 73 20 69 6e 20 74 68 65 20   be gaps in the 
29530 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  list..**.** See 
29540 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 62  also: [sqlite3_b
29550 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74 65 33  ind_blob|sqlite3
29560 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b 73 71  _bind()],.** [sq
29570 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
29580 65 74 65 72 5f 6e 61 6d 65 28 29 5d 2c 20 61 6e  eter_name()], an
29590 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  d.** [sqlite3_bi
295a0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
295b0 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  ex()]..**.** INV
295c0 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
295d0 48 31 33 36 30 31 7d 20 54 68 65 20 5b 73 71 6c  H13601} The [sql
295e0 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
295f0 74 65 72 5f 63 6f 75 6e 74 28 53 29 5d 20 69 6e  ter_count(S)] in
29600 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 0a  terface returns.
29610 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
29620 6c 61 72 67 65 73 74 20 69 6e 64 65 78 20 6f 66  largest index of
29630 20 61 6c 6c 20 53 51 4c 20 70 61 72 61 6d 65 74   all SQL paramet
29640 65 72 73 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20  ers in the.**   
29650 20 20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64         [prepared
29660 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2c 20 6f   statement] S, o
29670 72 20 30 20 69 66 20 53 20 63 6f 6e 74 61 69 6e  r 0 if S contain
29680 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65 74  s no SQL paramet
29690 65 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ers..*/.SQLITE_A
296a0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
296b0 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f  ind_parameter_co
296c0 75 6e 74 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  unt(sqlite3_stmt
296d0 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  *);../*.** CAPI3
296e0 52 45 46 3a 20 4e 61 6d 65 20 4f 66 20 41 20 48  REF: Name Of A H
296f0 6f 73 74 20 50 61 72 61 6d 65 74 65 72 20 7b 48  ost Parameter {H
29700 31 33 36 32 30 7d 20 3c 53 37 30 33 30 30 3e 0a  13620} <S70300>.
29710 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
29720 6e 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ne returns a poi
29730 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65  nter to the name
29740 20 6f 66 20 74 68 65 20 6e 2d 74 68 0a 2a 2a 20   of the n-th.** 
29750 5b 53 51 4c 20 70 61 72 61 6d 65 74 65 72 5d 20  [SQL parameter] 
29760 69 6e 20 61 20 5b 70 72 65 70 61 72 65 64 20 73  in a [prepared s
29770 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 53 51  tatement]..** SQ
29780 4c 20 70 61 72 61 6d 65 74 65 72 73 20 6f 66 20  L parameters of 
29790 74 68 65 20 66 6f 72 6d 20 22 3f 4e 4e 4e 22 20  the form "?NNN" 
297a0 6f 72 20 22 3a 41 41 41 22 20 6f 72 20 22 40 41  or ":AAA" or "@A
297b0 41 41 22 20 6f 72 20 22 24 41 41 41 22 0a 2a 2a  AA" or "$AAA".**
297c0 20 68 61 76 65 20 61 20 6e 61 6d 65 20 77 68 69   have a name whi
297d0 63 68 20 69 73 20 74 68 65 20 73 74 72 69 6e 67  ch is the string
297e0 20 22 3f 4e 4e 4e 22 20 6f 72 20 22 3a 41 41 41   "?NNN" or ":AAA
297f0 22 20 6f 72 20 22 40 41 41 41 22 20 6f 72 20 22  " or "@AAA" or "
29800 24 41 41 41 22 0a 2a 2a 20 72 65 73 70 65 63 74  $AAA".** respect
29810 69 76 65 6c 79 2e 0a 2a 2a 20 49 6e 20 6f 74 68  ively..** In oth
29820 65 72 20 77 6f 72 64 73 2c 20 74 68 65 20 69 6e  er words, the in
29830 69 74 69 61 6c 20 22 3a 22 20 6f 72 20 22 24 22  itial ":" or "$"
29840 20 6f 72 20 22 40 22 20 6f 72 20 22 3f 22 0a 2a   or "@" or "?".*
29850 2a 20 69 73 20 69 6e 63 6c 75 64 65 64 20 61 73  * is included as
29860 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e 61 6d   part of the nam
29870 65 2e 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 73  e..** Parameters
29880 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22   of the form "?"
29890 20 77 69 74 68 6f 75 74 20 61 20 66 6f 6c 6c 6f   without a follo
298a0 77 69 6e 67 20 69 6e 74 65 67 65 72 20 68 61 76  wing integer hav
298b0 65 20 6e 6f 20 6e 61 6d 65 0a 2a 2a 20 61 6e 64  e no name.** and
298c0 20 61 72 65 20 61 6c 73 6f 20 72 65 66 65 72 72   are also referr
298d0 65 64 20 74 6f 20 61 73 20 22 61 6e 6f 6e 79 6d  ed to as "anonym
298e0 6f 75 73 20 70 61 72 61 6d 65 74 65 72 73 22 2e  ous parameters".
298f0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
29900 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 20   host parameter 
29910 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f 66 20  has an index of 
29920 31 2c 20 6e 6f 74 20 30 2e 0a 2a 2a 0a 2a 2a 20  1, not 0..**.** 
29930 49 66 20 74 68 65 20 76 61 6c 75 65 20 6e 20 69  If the value n i
29940 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20 6f  s out of range o
29950 72 20 69 66 20 74 68 65 20 6e 2d 74 68 20 70 61  r if the n-th pa
29960 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 6e 61  rameter is.** na
29970 6d 65 6c 65 73 73 2c 20 74 68 65 6e 20 4e 55 4c  meless, then NUL
29980 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  L is returned.  
29990 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  The returned str
299a0 69 6e 67 20 69 73 0a 2a 2a 20 61 6c 77 61 79 73  ing is.** always
299b0 20 69 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 69   in UTF-8 encodi
299c0 6e 67 20 65 76 65 6e 20 69 66 20 74 68 65 20 6e  ng even if the n
299d0 61 6d 65 64 20 70 61 72 61 6d 65 74 65 72 20 77  amed parameter w
299e0 61 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79  as.** originally
299f0 20 73 70 65 63 69 66 69 65 64 20 61 73 20 55 54   specified as UT
29a00 46 2d 31 36 20 69 6e 20 5b 73 71 6c 69 74 65 33  F-16 in [sqlite3
29a10 5f 70 72 65 70 61 72 65 31 36 28 29 5d 20 6f 72  _prepare16()] or
29a20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65  .** [sqlite3_pre
29a30 70 61 72 65 31 36 5f 76 32 28 29 5d 2e 0a 2a 2a  pare16_v2()]..**
29a40 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
29a50 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
29a60 7c 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d  |sqlite3_bind()]
29a70 2c 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 62 69  ,.** [sqlite3_bi
29a80 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 63 6f 75  nd_parameter_cou
29a90 6e 74 28 29 5d 2c 20 61 6e 64 0a 2a 2a 20 5b 73  nt()], and.** [s
29aa0 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61  qlite3_bind_para
29ab0 6d 65 74 65 72 5f 69 6e 64 65 78 28 29 5d 2e 0a  meter_index()]..
29ac0 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
29ad0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 36 32 31 7d  :.**.** {H13621}
29ae0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69   The [sqlite3_bi
29af0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61 6d  nd_parameter_nam
29b00 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  e(S,N)] interfac
29b10 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20  e returns.**    
29b20 20 20 20 20 20 20 61 20 55 54 46 2d 38 20 72 65        a UTF-8 re
29b30 6e 64 65 72 69 6e 67 20 6f 66 20 74 68 65 20 6e  ndering of the n
29b40 61 6d 65 20 6f 66 20 74 68 65 20 53 51 4c 20 70  ame of the SQL p
29b50 61 72 61 6d 65 74 65 72 20 69 6e 0a 2a 2a 20 20  arameter in.**  
29b60 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 65          the [pre
29b70 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
29b80 20 53 20 68 61 76 69 6e 67 20 69 6e 64 65 78 20   S having index 
29b90 4e 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  N, or.**        
29ba0 20 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20    NULL if there 
29bb0 69 73 20 6e 6f 20 53 51 4c 20 70 61 72 61 6d 65  is no SQL parame
29bc0 74 65 72 20 77 69 74 68 20 69 6e 64 65 78 20 4e  ter with index N
29bd0 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a 20 20 20   or if the.**   
29be0 20 20 20 20 20 20 20 70 61 72 61 6d 65 74 65 72         parameter
29bf0 20 77 69 74 68 20 69 6e 64 65 78 20 4e 20 69 73   with index N is
29c00 20 61 6e 20 61 6e 6f 6e 79 6d 6f 75 73 20 70 61   an anonymous pa
29c10 72 61 6d 65 74 65 72 20 22 3f 22 2e 0a 2a 2f 0a  rameter "?"..*/.
29c20 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
29c30 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 62   char *sqlite3_b
29c40 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
29c50 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  me(sqlite3_stmt*
29c60 2c 20 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  , int);../*.** C
29c70 41 50 49 33 52 45 46 3a 20 49 6e 64 65 78 20 4f  API3REF: Index O
29c80 66 20 41 20 50 61 72 61 6d 65 74 65 72 20 57 69  f A Parameter Wi
29c90 74 68 20 41 20 47 69 76 65 6e 20 4e 61 6d 65 20  th A Given Name 
29ca0 7b 48 31 33 36 34 30 7d 20 3c 53 37 30 33 30 30  {H13640} <S70300
29cb0 3e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  >.**.** Return t
29cc0 68 65 20 69 6e 64 65 78 20 6f 66 20 61 6e 20 53  he index of an S
29cd0 51 4c 20 70 61 72 61 6d 65 74 65 72 20 67 69 76  QL parameter giv
29ce0 65 6e 20 69 74 73 20 6e 61 6d 65 2e 20 20 54 68  en its name.  Th
29cf0 65 0a 2a 2a 20 69 6e 64 65 78 20 76 61 6c 75 65  e.** index value
29d00 20 72 65 74 75 72 6e 65 64 20 69 73 20 73 75 69   returned is sui
29d10 74 61 62 6c 65 20 66 6f 72 20 75 73 65 20 61 73  table for use as
29d20 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70   the second.** p
29d30 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c  arameter to [sql
29d40 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73  ite3_bind_blob|s
29d50 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 5d 2e 20  qlite3_bind()]. 
29d60 20 41 20 7a 65 72 6f 0a 2a 2a 20 69 73 20 72 65   A zero.** is re
29d70 74 75 72 6e 65 64 20 69 66 20 6e 6f 20 6d 61 74  turned if no mat
29d80 63 68 69 6e 67 20 70 61 72 61 6d 65 74 65 72 20  ching parameter 
29d90 69 73 20 66 6f 75 6e 64 2e 20 20 54 68 65 20 70  is found.  The p
29da0 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6e 61 6d 65  arameter.** name
29db0 20 6d 75 73 74 20 62 65 20 67 69 76 65 6e 20 69   must be given i
29dc0 6e 20 55 54 46 2d 38 20 65 76 65 6e 20 69 66 20  n UTF-8 even if 
29dd0 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  the original sta
29de0 74 65 6d 65 6e 74 0a 2a 2a 20 77 61 73 20 70 72  tement.** was pr
29df0 65 70 61 72 65 64 20 66 72 6f 6d 20 55 54 46 2d  epared from UTF-
29e00 31 36 20 74 65 78 74 20 75 73 69 6e 67 20 5b 73  16 text using [s
29e10 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
29e20 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 53 65  _v2()]..**.** Se
29e30 65 20 61 6c 73 6f 3a 20 5b 73 71 6c 69 74 65 33  e also: [sqlite3
29e40 5f 62 69 6e 64 5f 62 6c 6f 62 7c 73 71 6c 69 74  _bind_blob|sqlit
29e50 65 33 5f 62 69 6e 64 28 29 5d 2c 0a 2a 2a 20 5b  e3_bind()],.** [
29e60 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
29e70 61 6d 65 74 65 72 5f 63 6f 75 6e 74 28 29 5d 2c  ameter_count()],
29e80 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74 65 33   and.** [sqlite3
29e90 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  _bind_parameter_
29ea0 69 6e 64 65 78 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20  index()]..**.** 
29eb0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
29ec0 2a 20 7b 48 31 33 36 34 31 7d 20 54 68 65 20 5b  * {H13641} The [
29ed0 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72  sqlite3_bind_par
29ee0 61 6d 65 74 65 72 5f 69 6e 64 65 78 28 53 2c 4e  ameter_index(S,N
29ef0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74  )] interface ret
29f00 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  urns.**         
29f10 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 53 51   the index of SQ
29f20 4c 20 70 61 72 61 6d 65 74 65 72 20 69 6e 20 74  L parameter in t
29f30 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
29f40 74 65 6d 65 6e 74 5d 0a 2a 2a 20 20 20 20 20 20  tement].**      
29f50 20 20 20 20 53 20 77 68 6f 73 65 20 6e 61 6d 65      S whose name
29f60 20 6d 61 74 63 68 65 73 20 74 68 65 20 55 54 46   matches the UTF
29f70 2d 38 20 73 74 72 69 6e 67 20 4e 2c 20 6f 72 20  -8 string N, or 
29f80 30 20 69 66 20 74 68 65 72 65 20 69 73 0a 2a 2a  0 if there is.**
29f90 20 20 20 20 20 20 20 20 20 20 6e 6f 20 6d 61 74            no mat
29fa0 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ch..*/.SQLITE_AP
29fb0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
29fc0 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64  nd_parameter_ind
29fd0 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  ex(sqlite3_stmt*
29fe0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
29ff0 61 6d 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  ame);../*.** CAP
2a000 49 33 52 45 46 3a 20 52 65 73 65 74 20 41 6c 6c  I3REF: Reset All
2a010 20 42 69 6e 64 69 6e 67 73 20 4f 6e 20 41 20 50   Bindings On A P
2a020 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e  repared Statemen
2a030 74 20 7b 48 31 33 36 36 30 7d 20 3c 53 37 30 33  t {H13660} <S703
2a040 30 30 3e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 74 72 61  00>.**.** Contra
2a050 72 79 20 74 6f 20 74 68 65 20 69 6e 74 75 69 74  ry to the intuit
2a060 69 6f 6e 20 6f 66 20 6d 61 6e 79 2c 20 5b 73 71  ion of many, [sq
2a070 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 64  lite3_reset()] d
2a080 6f 65 73 20 6e 6f 74 20 72 65 73 65 74 0a 2a 2a  oes not reset.**
2a090 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 69   the [sqlite3_bi
2a0a0 6e 64 5f 62 6c 6f 62 20 7c 20 62 69 6e 64 69 6e  nd_blob | bindin
2a0b0 67 73 5d 20 6f 6e 20 61 20 5b 70 72 65 70 61 72  gs] on a [prepar
2a0c0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a  ed statement]..*
2a0d0 2a 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69  * Use this routi
2a0e0 6e 65 20 74 6f 20 72 65 73 65 74 20 61 6c 6c 20  ne to reset all 
2a0f0 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72 73 20  host parameters 
2a100 74 6f 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49  to NULL..**.** I
2a110 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a  NVARIANTS:.**.**
2a120 20 7b 48 31 33 36 36 31 7d 20 54 68 65 20 5b 73   {H13661} The [s
2a130 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69 6e  qlite3_clear_bin
2a140 64 69 6e 67 73 28 53 29 5d 20 69 6e 74 65 72 66  dings(S)] interf
2a150 61 63 65 20 72 65 73 65 74 73 20 61 6c 6c 20 53  ace resets all S
2a160 51 4c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70  QL.**          p
2a170 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69 6e 67  arameter binding
2a180 73 20 69 6e 20 74 68 65 20 5b 70 72 65 70 61 72  s in the [prepar
2a190 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20  ed statement] S 
2a1a0 62 61 63 6b 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f  back to NULL..*/
2a1b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2a1c0 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
2a1d0 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
2a1e0 74 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  tmt*);../*.** CA
2a1f0 50 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 4f  PI3REF: Number O
2a200 66 20 43 6f 6c 75 6d 6e 73 20 49 6e 20 41 20 52  f Columns In A R
2a210 65 73 75 6c 74 20 53 65 74 20 7b 48 31 33 37 31  esult Set {H1371
2a220 30 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a  0} <S10700>.**.*
2a230 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
2a240 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
2a250 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2a260 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68 65   returned by the
2a270 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73 74  .** [prepared st
2a280 61 74 65 6d 65 6e 74 5d 2e 20 54 68 69 73 20 72  atement]. This r
2a290 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30  outine returns 0
2a2a0 20 69 66 20 70 53 74 6d 74 20 69 73 20 61 6e 20   if pStmt is an 
2a2b0 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
2a2c0 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 72   that does not r
2a2d0 65 74 75 72 6e 20 64 61 74 61 20 28 66 6f 72 20  eturn data (for 
2a2e0 65 78 61 6d 70 6c 65 20 61 6e 20 5b 55 50 44 41  example an [UPDA
2a2f0 54 45 5d 29 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  TE])..**.** INVA
2a300 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
2a310 31 33 37 31 31 7d 20 54 68 65 20 5b 73 71 6c 69  13711} The [sqli
2a320 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
2a330 28 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72  (S)] interface r
2a340 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65  eturns the numbe
2a350 72 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  r of.**         
2a360 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
2a370 72 65 73 75 6c 74 20 73 65 74 20 67 65 6e 65 72  result set gener
2a380 61 74 65 64 20 62 79 20 74 68 65 20 5b 70 72 65  ated by the [pre
2a390 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2a3a0 20 53 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   S,.**          
2a3b0 6f 72 20 30 20 69 66 20 53 20 64 6f 65 73 20 6e  or 0 if S does n
2a3c0 6f 74 20 67 65 6e 65 72 61 74 65 20 61 20 72 65  ot generate a re
2a3d0 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 53 51 4c  sult set..*/.SQL
2a3e0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2a3f0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74  te3_column_count
2a400 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
2a410 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  Stmt);../*.** CA
2a420 50 49 33 52 45 46 3a 20 43 6f 6c 75 6d 6e 20 4e  PI3REF: Column N
2a430 61 6d 65 73 20 49 6e 20 41 20 52 65 73 75 6c 74  ames In A Result
2a440 20 53 65 74 20 7b 48 31 33 37 32 30 7d 20 3c 53   Set {H13720} <S
2a450 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  10700>.**.** The
2a460 73 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  se routines retu
2a470 72 6e 20 74 68 65 20 6e 61 6d 65 20 61 73 73 69  rn the name assi
2a480 67 6e 65 64 20 74 6f 20 61 20 70 61 72 74 69 63  gned to a partic
2a490 75 6c 61 72 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69  ular column.** i
2a4a0 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
2a4b0 20 6f 66 20 61 20 5b 53 45 4c 45 43 54 5d 20 73   of a [SELECT] s
2a4c0 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20 73  tatement.  The s
2a4d0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61  qlite3_column_na
2a4e0 6d 65 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  me().** interfac
2a4f0 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e  e returns a poin
2a500 74 65 72 20 74 6f 20 61 20 7a 65 72 6f 2d 74 65  ter to a zero-te
2a510 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73  rminated UTF-8 s
2a520 74 72 69 6e 67 0a 2a 2a 20 61 6e 64 20 73 71 6c  tring.** and sql
2a530 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2a540 31 36 28 29 20 72 65 74 75 72 6e 73 20 61 20 70  16() returns a p
2a550 6f 69 6e 74 65 72 20 74 6f 20 61 20 7a 65 72 6f  ointer to a zero
2a560 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 55  -terminated.** U
2a570 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54  TF-16 string.  T
2a580 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65 74  he first paramet
2a590 65 72 20 69 73 20 74 68 65 20 5b 70 72 65 70 61  er is the [prepa
2a5a0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a  red statement].*
2a5b0 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  * that implement
2a5c0 73 20 74 68 65 20 5b 53 45 4c 45 43 54 5d 20 73  s the [SELECT] s
2a5d0 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 20 73 65  tatement. The se
2a5e0 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  cond parameter i
2a5f0 73 20 74 68 65 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  s the.** column 
2a600 6e 75 6d 62 65 72 2e 20 20 54 68 65 20 6c 65 66  number.  The lef
2a610 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69 73 20  tmost column is 
2a620 6e 75 6d 62 65 72 20 30 2e 0a 2a 2a 0a 2a 2a 20  number 0..**.** 
2a630 54 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  The returned str
2a640 69 6e 67 20 70 6f 69 6e 74 65 72 20 69 73 20 76  ing pointer is v
2a650 61 6c 69 64 20 75 6e 74 69 6c 20 65 69 74 68 65  alid until eithe
2a660 72 20 74 68 65 20 5b 70 72 65 70 61 72 65 64 20  r the [prepared 
2a670 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20 69 73  statement].** is
2a680 20 64 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73   destroyed by [s
2a690 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
2a6a0 29 5d 20 6f 72 20 75 6e 74 69 6c 20 74 68 65 20  )] or until the 
2a6b0 6e 65 78 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  next call to.** 
2a6c0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e  sqlite3_column_n
2a6d0 61 6d 65 28 29 20 6f 72 20 73 71 6c 69 74 65 33  ame() or sqlite3
2a6e0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 29  _column_name16()
2a6f0 20 6f 6e 20 74 68 65 20 73 61 6d 65 20 63 6f 6c   on the same col
2a700 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 71  umn..**.** If sq
2a710 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 66  lite3_malloc() f
2a720 61 69 6c 73 20 64 75 72 69 6e 67 20 74 68 65 20  ails during the 
2a730 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20 65 69  processing of ei
2a740 74 68 65 72 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  ther routine.** 
2a750 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 64 75 72  (for example dur
2a760 69 6e 67 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e  ing a conversion
2a770 20 66 72 6f 6d 20 55 54 46 2d 38 20 74 6f 20 55   from UTF-8 to U
2a780 54 46 2d 31 36 29 20 74 68 65 6e 20 61 0a 2a 2a  TF-16) then a.**
2a790 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73   NULL pointer is
2a7a0 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
2a7b0 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 72   The name of a r
2a7c0 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20  esult column is 
2a7d0 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
2a7e0 20 22 41 53 22 20 63 6c 61 75 73 65 20 66 6f 72   "AS" clause for
2a7f0 0a 2a 2a 20 74 68 61 74 20 63 6f 6c 75 6d 6e 2c  .** that column,
2a800 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
2a810 41 53 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74  AS clause.  If t
2a820 68 65 72 65 20 69 73 20 6e 6f 20 41 53 20 63 6c  here is no AS cl
2a830 61 75 73 65 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ause.** then the
2a840 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   name of the col
2a850 75 6d 6e 20 69 73 20 75 6e 73 70 65 63 69 66 69  umn is unspecifi
2a860 65 64 20 61 6e 64 20 6d 61 79 20 63 68 61 6e 67  ed and may chang
2a870 65 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65 20 72 65  e from.** one re
2a880 6c 65 61 73 65 20 6f 66 20 53 51 4c 69 74 65 20  lease of SQLite 
2a890 74 6f 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2a 0a  to the next..**.
2a8a0 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
2a8b0 2a 0a 2a 2a 20 7b 48 31 33 37 32 31 7d 20 41 20  *.** {H13721} A 
2a8c0 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63  successful invoc
2a8d0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71  ation of the [sq
2a8e0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
2a8f0 65 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20 20  e(S,N)].**      
2a900 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72 65      interface re
2a910 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20 6f  turns the name o
2a920 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
2a930 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a 20   (where 0 is.** 
2a940 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65 66           the lef
2a950 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66 6f  tmost column) fo
2a960 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  r the result set
2a970 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   of the.**      
2a980 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74      [prepared st
2a990 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61 20  atement] S as a 
2a9a0 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
2a9b0 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a  UTF-8 string..**
2a9c0 0a 2a 2a 20 7b 48 31 33 37 32 33 7d 20 41 20 73  .** {H13723} A s
2a9d0 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61  uccessful invoca
2a9e0 74 69 6f 6e 20 6f 66 20 74 68 65 20 5b 73 71 6c  tion of the [sql
2a9f0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2aa00 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20 20 20 20  16(S,N)].**     
2aa10 20 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 72       interface r
2aa20 65 74 75 72 6e 73 20 74 68 65 20 6e 61 6d 65 20  eturns the name 
2aa30 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  of the Nth colum
2aa40 6e 20 28 77 68 65 72 65 20 30 20 69 73 0a 2a 2a  n (where 0 is.**
2aa50 20 20 20 20 20 20 20 20 20 20 74 68 65 20 6c 65            the le
2aa60 66 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 29 20 66  ftmost column) f
2aa70 6f 72 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  or the result se
2aa80 74 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20  t of the.**     
2aa90 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73       [prepared s
2aaa0 74 61 74 65 6d 65 6e 74 5d 20 53 20 61 73 20 61  tatement] S as a
2aab0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
2aac0 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 0a 2a   UTF-16 string.*
2aad0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 74 68  *          in th
2aae0 65 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  e native byte or
2aaf0 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  der..**.** {H137
2ab00 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  24} The [sqlite3
2ab10 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 29 5d 20  _column_name()] 
2ab20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c  and [sqlite3_col
2ab30 75 6d 6e 5f 6e 61 6d 65 31 36 28 29 5d 0a 2a 2a  umn_name16()].**
2ab40 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66            interf
2ab50 61 63 65 73 20 72 65 74 75 72 6e 20 61 20 4e 55  aces return a NU
2ab60 4c 4c 20 70 6f 69 6e 74 65 72 20 69 66 20 74 68  LL pointer if th
2ab70 65 79 20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f  ey are unable to
2ab80 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6c 6c  .**          all
2ab90 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20  ocate memory to 
2aba0 68 6f 6c 64 20 74 68 65 69 72 20 6e 6f 72 6d 61  hold their norma
2abb0 6c 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 73  l return strings
2abc0 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 32 35 7d  ..**.** {H13725}
2abd0 20 49 66 20 74 68 65 20 4e 20 70 61 72 61 6d 65   If the N parame
2abe0 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
2abf0 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 53 2c 4e 29  column_name(S,N)
2ac00 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ] or.**         
2ac10 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   [sqlite3_column
2ac20 5f 6e 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 73  _name16(S,N)] is
2ac30 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74   out of range, t
2ac40 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  hen the.**      
2ac50 20 20 20 20 69 6e 74 65 72 66 61 63 65 73 20 72      interfaces r
2ac60 65 74 75 72 6e 20 61 20 4e 55 4c 4c 20 70 6f 69  eturn a NULL poi
2ac70 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  nter..**.** {H13
2ac80 37 32 36 7d 20 54 68 65 20 73 74 72 69 6e 67 73  726} The strings
2ac90 20 72 65 74 75 72 6e 65 64 20 62 79 20 5b 73 71   returned by [sq
2aca0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d  lite3_column_nam
2acb0 65 28 53 2c 4e 29 5d 20 61 6e 64 0a 2a 2a 20 20  e(S,N)] and.**  
2acc0 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33          [sqlite3
2acd0 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 53  _column_name16(S
2ace0 2c 4e 29 5d 20 61 72 65 20 76 61 6c 69 64 20 75  ,N)] are valid u
2acf0 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 0a 2a 2a  ntil the next.**
2ad00 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 20 74            call t
2ad10 6f 20 65 69 74 68 65 72 20 72 6f 75 74 69 6e 65  o either routine
2ad20 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 53   with the same S
2ad30 20 61 6e 64 20 4e 20 70 61 72 61 6d 65 74 65 72   and N parameter
2ad40 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72  s.**          or
2ad50 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f   until [sqlite3_
2ad60 66 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20  finalize(S)] is 
2ad70 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  called..**.** {H
2ad80 31 33 37 32 37 7d 20 57 68 65 6e 20 61 20 72 65  13727} When a re
2ad90 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 61  sult column of a
2ada0 20 5b 53 45 4c 45 43 54 5d 20 73 74 61 74 65 6d   [SELECT] statem
2adb0 65 6e 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ent contains.** 
2adc0 20 20 20 20 20 20 20 20 20 61 6e 20 41 53 20 63           an AS c
2add0 6c 61 75 73 65 2c 20 74 68 65 20 6e 61 6d 65 20  lause, the name 
2ade0 6f 66 20 74 68 61 74 20 63 6f 6c 75 6d 6e 20 69  of that column i
2adf0 73 20 74 68 65 20 69 64 65 6e 74 69 66 69 65 72  s the identifier
2ae00 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f 20  .**          to 
2ae10 74 68 65 20 72 69 67 68 74 20 6f 66 20 74 68 65  the right of the
2ae20 20 41 53 20 6b 65 79 77 6f 72 64 2e 0a 2a 2f 0a   AS keyword..*/.
2ae30 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74  SQLITE_API const
2ae40 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 63   char *sqlite3_c
2ae50 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 73 71 6c 69 74  olumn_name(sqlit
2ae60 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 4e 29  e3_stmt*, int N)
2ae70 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  ;.SQLITE_API con
2ae80 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
2ae90 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31 36 28 73  _column_name16(s
2aea0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e  qlite3_stmt*, in
2aeb0 74 20 4e 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  t N);../*.** CAP
2aec0 49 33 52 45 46 3a 20 53 6f 75 72 63 65 20 4f 66  I3REF: Source Of
2aed0 20 44 61 74 61 20 49 6e 20 41 20 51 75 65 72 79   Data In A Query
2aee0 20 52 65 73 75 6c 74 20 7b 48 31 33 37 34 30 7d   Result {H13740}
2aef0 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
2af00 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 70  These routines p
2af10 72 6f 76 69 64 65 20 61 20 6d 65 61 6e 73 20 74  rovide a means t
2af20 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 61 74  o determine what
2af30 20 63 6f 6c 75 6d 6e 20 6f 66 20 77 68 61 74 0a   column of what.
2af40 2a 2a 20 74 61 62 6c 65 20 69 6e 20 77 68 69 63  ** table in whic
2af50 68 20 64 61 74 61 62 61 73 65 20 61 20 72 65 73  h database a res
2af60 75 6c 74 20 6f 66 20 61 20 5b 53 45 4c 45 43 54  ult of a [SELECT
2af70 5d 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6d 65  ] statement come
2af80 73 20 66 72 6f 6d 2e 0a 2a 2a 20 54 68 65 20 6e  s from..** The n
2af90 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
2afa0 61 73 65 20 6f 72 20 74 61 62 6c 65 20 6f 72 20  ase or table or 
2afb0 63 6f 6c 75 6d 6e 20 63 61 6e 20 62 65 20 72 65  column can be re
2afc0 74 75 72 6e 65 64 20 61 73 0a 2a 2a 20 65 69 74  turned as.** eit
2afd0 68 65 72 20 61 20 55 54 46 2d 38 20 6f 72 20 55  her a UTF-8 or U
2afe0 54 46 2d 31 36 20 73 74 72 69 6e 67 2e 20 20 54  TF-16 string.  T
2aff0 68 65 20 5f 64 61 74 61 62 61 73 65 5f 20 72 6f  he _database_ ro
2b000 75 74 69 6e 65 73 20 72 65 74 75 72 6e 0a 2a 2a  utines return.**
2b010 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61   the database na
2b020 6d 65 2c 20 74 68 65 20 5f 74 61 62 6c 65 5f 20  me, the _table_ 
2b030 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
2b040 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 2c 20  the table name, 
2b050 61 6e 64 0a 2a 2a 20 74 68 65 20 6f 72 69 67 69  and.** the origi
2b060 6e 5f 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75  n_ routines retu
2b070 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  rn the column na
2b080 6d 65 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72  me..** The retur
2b090 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 76 61  ned string is va
2b0a0 6c 69 64 20 75 6e 74 69 6c 20 74 68 65 20 5b 70  lid until the [p
2b0b0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2b0c0 74 5d 20 69 73 20 64 65 73 74 72 6f 79 65 64 0a  t] is destroyed.
2b0d0 2a 2a 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65  ** using [sqlite
2b0e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72  3_finalize()] or
2b0f0 20 75 6e 74 69 6c 20 74 68 65 20 73 61 6d 65 20   until the same 
2b100 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 72  information is r
2b110 65 71 75 65 73 74 65 64 0a 2a 2a 20 61 67 61 69  equested.** agai
2b120 6e 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e 74  n in a different
2b130 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a 2a   encoding..**.**
2b140 20 54 68 65 20 6e 61 6d 65 73 20 72 65 74 75 72   The names retur
2b150 6e 65 64 20 61 72 65 20 74 68 65 20 6f 72 69 67  ned are the orig
2b160 69 6e 61 6c 20 75 6e 2d 61 6c 69 61 73 65 64 20  inal un-aliased 
2b170 6e 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20  names of the.** 
2b180 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c 65 2c  database, table,
2b190 20 61 6e 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a   and column..**.
2b1a0 2a 2a 20 54 68 65 20 66 69 72 73 74 20 61 72 67  ** The first arg
2b1b0 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 6f 6c  ument to the fol
2b1c0 6c 6f 77 69 6e 67 20 63 61 6c 6c 73 20 69 73 20  lowing calls is 
2b1d0 61 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  a [prepared stat
2b1e0 65 6d 65 6e 74 5d 2e 0a 2a 2a 20 54 68 65 73 65  ement]..** These
2b1f0 20 66 75 6e 63 74 69 6f 6e 73 20 72 65 74 75 72   functions retur
2b200 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  n information ab
2b210 6f 75 74 20 74 68 65 20 4e 74 68 20 63 6f 6c 75  out the Nth colu
2b220 6d 6e 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a  mn returned by.*
2b230 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2c  * the statement,
2b240 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
2b250 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20  second function 
2b260 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
2b270 49 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  If the Nth colum
2b280 6e 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  n returned by th
2b290 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  e statement is a
2b2a0 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 0a  n expression or.
2b2b0 2a 2a 20 73 75 62 71 75 65 72 79 20 61 6e 64 20  ** subquery and 
2b2c0 69 73 20 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20  is not a column 
2b2d0 76 61 6c 75 65 2c 20 74 68 65 6e 20 61 6c 6c 20  value, then all 
2b2e0 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f  of these functio
2b2f0 6e 73 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c  ns return.** NUL
2b300 4c 2e 20 20 54 68 65 73 65 20 72 6f 75 74 69 6e  L.  These routin
2b310 65 20 6d 69 67 68 74 20 61 6c 73 6f 20 72 65 74  e might also ret
2b320 75 72 6e 20 4e 55 4c 4c 20 69 66 20 61 20 6d 65  urn NULL if a me
2b330 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
2b340 65 72 72 6f 72 0a 2a 2a 20 6f 63 63 75 72 73 2e  error.** occurs.
2b350 20 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65    Otherwise, the
2b360 79 20 72 65 74 75 72 6e 20 74 68 65 20 6e 61 6d  y return the nam
2b370 65 20 6f 66 20 74 68 65 20 61 74 74 61 63 68 65  e of the attache
2b380 64 20 64 61 74 61 62 61 73 65 2c 20 74 61 62 6c  d database, tabl
2b390 65 0a 2a 2a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20  e.** and column 
2b3a0 74 68 61 74 20 71 75 65 72 79 20 72 65 73 75 6c  that query resul
2b3b0 74 20 63 6f 6c 75 6d 6e 20 77 61 73 20 65 78 74  t column was ext
2b3c0 72 61 63 74 65 64 20 66 72 6f 6d 2e 0a 2a 2a 0a  racted from..**.
2b3d0 2a 2a 20 41 73 20 77 69 74 68 20 61 6c 6c 20 6f  ** As with all o
2b3e0 74 68 65 72 20 53 51 4c 69 74 65 20 41 50 49 73  ther SQLite APIs
2b3f0 2c 20 74 68 6f 73 65 20 70 6f 73 74 66 69 78 65  , those postfixe
2b400 64 20 77 69 74 68 20 22 31 36 22 20 72 65 74 75  d with "16" retu
2b410 72 6e 0a 2a 2a 20 55 54 46 2d 31 36 20 65 6e 63  rn.** UTF-16 enc
2b420 6f 64 65 64 20 73 74 72 69 6e 67 73 2c 20 74 68  oded strings, th
2b430 65 20 6f 74 68 65 72 20 66 75 6e 63 74 69 6f 6e  e other function
2b440 73 20 72 65 74 75 72 6e 20 55 54 46 2d 38 2e 20  s return UTF-8. 
2b450 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 73  {END}.**.** Thes
2b460 65 20 41 50 49 73 20 61 72 65 20 6f 6e 6c 79 20  e APIs are only 
2b470 61 76 61 69 6c 61 62 6c 65 20 69 66 20 74 68 65  available if the
2b480 20 6c 69 62 72 61 72 79 20 77 61 73 20 63 6f 6d   library was com
2b490 70 69 6c 65 64 20 77 69 74 68 20 74 68 65 0a 2a  piled with the.*
2b4a0 2a 20 5b 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  * [SQLITE_ENABLE
2b4b0 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
2b4c0 5d 20 43 2d 70 72 65 70 72 6f 63 65 73 73 6f 72  ] C-preprocessor
2b4d0 20 73 79 6d 62 6f 6c 20 64 65 66 69 6e 65 64 2e   symbol defined.
2b4e0 0a 2a 2a 0a 2a 2a 20 7b 41 31 33 37 35 31 7d 0a  .**.** {A13751}.
2b4f0 2a 2a 20 49 66 20 74 77 6f 20 6f 72 20 6d 6f 72  ** If two or mor
2b500 65 20 74 68 72 65 61 64 73 20 63 61 6c 6c 20 6f  e threads call o
2b510 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 74 68  ne or more of th
2b520 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 67 61  ese routines aga
2b530 69 6e 73 74 20 74 68 65 20 73 61 6d 65 0a 2a 2a  inst the same.**
2b540 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2b550 65 6e 74 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 61  ent and column a
2b560 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 20  t the same time 
2b570 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73  then the results
2b580 20 61 72 65 0a 2a 2a 20 75 6e 64 65 66 69 6e 65   are.** undefine
2b590 64 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  d..**.** INVARIA
2b5a0 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37  NTS:.**.** {H137
2b5b0 34 31 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  41} The [sqlite3
2b5c0 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  _column_database
2b5d0 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65  _name(S,N)] inte
2b5e0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69  rface returns ei
2b5f0 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ther.**         
2b600 20 74 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d   the UTF-8 zero-
2b610 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20  terminated name 
2b620 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
2b630 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 0a 2a  from which the.*
2b640 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 72  *          Nth r
2b650 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  esult column of 
2b660 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
2b670 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65 78  atement] S is ex
2b680 74 72 61 63 74 65 64 2c 0a 2a 2a 20 20 20 20 20  tracted,.**     
2b690 20 20 20 20 20 6f 72 20 4e 55 4c 4c 20 69 66 20       or NULL if 
2b6a0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
2b6b0 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c  f S is a general
2b6c0 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20   expression.**  
2b6d0 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e          or if un
2b6e0 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
2b6f0 20 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65   memory to store
2b700 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a   the name..**.**
2b710 20 7b 48 31 33 37 34 32 7d 20 54 68 65 20 5b 73   {H13742} The [s
2b720 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61  qlite3_column_da
2b730 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28 53 2c  tabase_name16(S,
2b740 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  N)] interface re
2b750 74 75 72 6e 73 20 65 69 74 68 65 72 0a 2a 2a 20  turns either.** 
2b760 20 20 20 20 20 20 20 20 20 74 68 65 20 55 54 46           the UTF
2b770 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65 20  -16 native byte 
2b780 6f 72 64 65 72 20 7a 65 72 6f 2d 74 65 72 6d 69  order zero-termi
2b790 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66 20 74 68  nated name of th
2b7a0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20 20  e database.**   
2b7b0 20 20 20 20 20 20 20 66 72 6f 6d 20 77 68 69 63         from whic
2b7c0 68 20 74 68 65 20 4e 74 68 20 72 65 73 75 6c 74  h the Nth result
2b7d0 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b   column of the [
2b7e0 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2b7f0 6e 74 5d 20 53 20 69 73 0a 2a 2a 20 20 20 20 20  nt] S is.**     
2b800 20 20 20 20 20 65 78 74 72 61 63 74 65 64 2c 20       extracted, 
2b810 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e  or NULL if the N
2b820 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69  th column of S i
2b830 73 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72  s a general expr
2b840 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ession.**       
2b850 20 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20     or if unable 
2b860 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  to allocate memo
2b870 72 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ry to store the 
2b880 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  name..**.** {H13
2b890 37 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  743} The [sqlite
2b8a0 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
2b8b0 61 6d 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  ame(S,N)] interf
2b8c0 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68  ace returns eith
2b8d0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  er.**          t
2b8e0 68 65 20 55 54 46 2d 38 20 7a 65 72 6f 2d 74 65  he UTF-8 zero-te
2b8f0 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65 20 6f 66  rminated name of
2b900 20 74 68 65 20 74 61 62 6c 65 20 66 72 6f 6d 20   the table from 
2b910 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 20 20 20  which the.**    
2b920 20 20 20 20 20 20 4e 74 68 20 72 65 73 75 6c 74        Nth result
2b930 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b   column of the [
2b940 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2b950 6e 74 5d 20 53 20 69 73 20 65 78 74 72 61 63 74  nt] S is extract
2b960 65 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ed,.**          
2b970 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e  or NULL if the N
2b980 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69  th column of S i
2b990 73 20 61 20 67 65 6e 65 72 61 6c 20 65 78 70 72  s a general expr
2b9a0 65 73 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20  ession.**       
2b9b0 20 20 20 6f 72 20 69 66 20 75 6e 61 62 6c 65 20     or if unable 
2b9c0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f  to allocate memo
2b9d0 72 79 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  ry to store the 
2b9e0 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  name..**.** {H13
2b9f0 37 34 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  744} The [sqlite
2ba00 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
2ba10 61 6d 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65  ame16(S,N)] inte
2ba20 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 65 69  rface returns ei
2ba30 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ther.**         
2ba40 20 74 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69   the UTF-16 nati
2ba50 76 65 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65  ve byte order ze
2ba60 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61  ro-terminated na
2ba70 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a  me of the table.
2ba80 2a 2a 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d  **          from
2ba90 20 77 68 69 63 68 20 74 68 65 20 4e 74 68 20 72   which the Nth r
2baa0 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  esult column of 
2bab0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
2bac0 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 0a 2a 2a  atement] S is.**
2bad0 20 20 20 20 20 20 20 20 20 20 65 78 74 72 61 63            extrac
2bae0 74 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20  ted, or NULL if 
2baf0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
2bb00 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61 6c  f S is a general
2bb10 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 20   expression.**  
2bb20 20 20 20 20 20 20 20 20 6f 72 20 69 66 20 75 6e          or if un
2bb30 61 62 6c 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65  able to allocate
2bb40 20 6d 65 6d 6f 72 79 20 74 6f 20 73 74 6f 72 65   memory to store
2bb50 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a   the name..**.**
2bb60 20 7b 48 31 33 37 34 35 7d 20 54 68 65 20 5b 73   {H13745} The [s
2bb70 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72  qlite3_column_or
2bb80 69 67 69 6e 5f 6e 61 6d 65 28 53 2c 4e 29 5d 20  igin_name(S,N)] 
2bb90 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
2bba0 73 20 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20  s either.**     
2bbb0 20 20 20 20 20 74 68 65 20 55 54 46 2d 38 20 7a       the UTF-8 z
2bbc0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e  ero-terminated n
2bbd0 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
2bbe0 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69   column from whi
2bbf0 63 68 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ch the.**       
2bc00 20 20 20 4e 74 68 20 72 65 73 75 6c 74 20 63 6f     Nth result co
2bc10 6c 75 6d 6e 20 6f 66 20 74 68 65 20 5b 70 72 65  lumn of the [pre
2bc20 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d  pared statement]
2bc30 20 53 20 69 73 20 65 78 74 72 61 63 74 65 64 2c   S is extracted,
2bc40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20  .**          or 
2bc50 4e 55 4c 4c 20 69 66 20 74 68 65 20 4e 74 68 20  NULL if the Nth 
2bc60 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69 73 20 61  column of S is a
2bc70 20 67 65 6e 65 72 61 6c 20 65 78 70 72 65 73 73   general express
2bc80 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ion.**          
2bc90 6f 72 20 69 66 20 75 6e 61 62 6c 65 20 74 6f 20  or if unable to 
2bca0 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  allocate memory 
2bcb0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 6e 61 6d  to store the nam
2bcc0 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 34 36  e..**.** {H13746
2bcd0 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
2bce0 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d  olumn_origin_nam
2bcf0 65 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66  e16(S,N)] interf
2bd00 61 63 65 20 72 65 74 75 72 6e 73 20 65 69 74 68  ace returns eith
2bd10 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  er.**          t
2bd20 68 65 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65  he UTF-16 native
2bd30 20 62 79 74 65 20 6f 72 64 65 72 20 7a 65 72 6f   byte order zero
2bd40 2d 74 65 72 6d 69 6e 61 74 65 64 20 6e 61 6d 65  -terminated name
2bd50 20 6f 66 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a   of the table.**
2bd60 20 20 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e            column
2bd70 20 66 72 6f 6d 20 77 68 69 63 68 20 74 68 65 20   from which the 
2bd80 4e 74 68 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d  Nth result colum
2bd90 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20  n of the.**     
2bda0 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73       [prepared s
2bdb0 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 65  tatement] S is e
2bdc0 78 74 72 61 63 74 65 64 2c 20 6f 72 20 4e 55 4c  xtracted, or NUL
2bdd0 4c 20 69 66 20 74 68 65 20 4e 74 68 20 63 6f 6c  L if the Nth col
2bde0 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  umn.**          
2bdf0 6f 66 20 53 20 69 73 20 61 20 67 65 6e 65 72 61  of S is a genera
2be00 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  l expression or 
2be10 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 61 6c 6c  if unable to all
2be20 6f 63 61 74 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ocate memory.** 
2be30 20 20 20 20 20 20 20 20 20 74 6f 20 73 74 6f 72           to stor
2be40 65 20 74 68 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a  e the name..**.*
2be50 2a 20 7b 48 31 33 37 34 38 7d 20 54 68 65 20 72  * {H13748} The r
2be60 65 74 75 72 6e 20 76 61 6c 75 65 73 20 66 72 6f  eturn values fro
2be70 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  m.**          [s
2be80 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61  qlite3_column_da
2be90 74 61 62 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f  tabase_name | co
2bea0 6c 75 6d 6e 20 6d 65 74 61 64 61 74 61 20 69 6e  lumn metadata in
2beb0 74 65 72 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20  terfaces].**    
2bec0 20 20 20 20 20 20 61 72 65 20 76 61 6c 69 64 20        are valid 
2bed0 66 6f 72 20 74 68 65 20 6c 69 66 65 74 69 6d 65  for the lifetime
2bee0 20 6f 66 20 74 68 65 20 5b 70 72 65 70 61 72 65   of the [prepare
2bef0 64 20 73 74 61 74 65 6d 65 6e 74 5d 0a 2a 2a 20  d statement].** 
2bf00 20 20 20 20 20 20 20 20 20 6f 72 20 75 6e 74 69           or unti
2bf10 6c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69  l the encoding i
2bf20 73 20 63 68 61 6e 67 65 64 20 62 79 20 61 6e 6f  s changed by ano
2bf30 74 68 65 72 20 6d 65 74 61 64 61 74 61 0a 2a 2a  ther metadata.**
2bf40 20 20 20 20 20 20 20 20 20 20 69 6e 74 65 72 66            interf
2bf50 61 63 65 20 63 61 6c 6c 20 66 6f 72 20 74 68 65  ace call for the
2bf60 20 73 61 6d 65 20 70 72 65 70 61 72 65 64 20 73   same prepared s
2bf70 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 63 6f 6c  tatement and col
2bf80 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 41 53 53 55 4d  umn..**.** ASSUM
2bf90 50 54 49 4f 4e 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41  PTIONS:.**.** {A
2bfa0 31 33 37 35 31 7d 20 49 66 20 74 77 6f 20 6f 72  13751} If two or
2bfb0 20 6d 6f 72 65 20 74 68 72 65 61 64 73 20 63 61   more threads ca
2bfc0 6c 6c 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a  ll one or more.*
2bfd0 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
2bfe0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
2bff0 61 73 65 5f 6e 61 6d 65 20 7c 20 63 6f 6c 75 6d  ase_name | colum
2c000 6e 20 6d 65 74 61 64 61 74 61 20 69 6e 74 65 72  n metadata inter
2c010 66 61 63 65 73 5d 0a 2a 2a 20 20 20 20 20 20 20  faces].**       
2c020 20 20 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20     for the same 
2c030 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
2c040 65 6e 74 5d 20 61 6e 64 20 72 65 73 75 6c 74 20  ent] and result 
2c050 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20 20 20 20 20  column.**       
2c060 20 20 20 61 74 20 74 68 65 20 73 61 6d 65 20 74     at the same t
2c070 69 6d 65 20 74 68 65 6e 20 74 68 65 20 72 65 73  ime then the res
2c080 75 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e  ults are undefin
2c090 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
2c0a0 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  I const char *sq
2c0b0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74  lite3_column_dat
2c0c0 61 62 61 73 65 5f 6e 61 6d 65 28 73 71 6c 69 74  abase_name(sqlit
2c0d0 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53  e3_stmt*,int);.S
2c0e0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
2c0f0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
2c100 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
2c110 6d 65 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d  me16(sqlite3_stm
2c120 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
2c130 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  API const char *
2c140 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
2c150 61 62 6c 65 5f 6e 61 6d 65 28 73 71 6c 69 74 65  able_name(sqlite
2c160 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51  3_stmt*,int);.SQ
2c170 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
2c180 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
2c190 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36  umn_table_name16
2c1a0 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69  (sqlite3_stmt*,i
2c1b0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  nt);.SQLITE_API 
2c1c0 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
2c1d0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69  te3_column_origi
2c1e0 6e 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  n_name(sqlite3_s
2c1f0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  tmt*,int);.SQLIT
2c200 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
2c210 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
2c220 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31 36 28 73  _origin_name16(s
2c230 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
2c240 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
2c250 45 46 3a 20 44 65 63 6c 61 72 65 64 20 44 61 74  EF: Declared Dat
2c260 61 74 79 70 65 20 4f 66 20 41 20 51 75 65 72 79  atype Of A Query
2c270 20 52 65 73 75 6c 74 20 7b 48 31 33 37 36 30 7d   Result {H13760}
2c280 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a 20   <S10700>.**.** 
2c290 54 68 65 20 66 69 72 73 74 20 70 61 72 61 6d 65  The first parame
2c2a0 74 65 72 20 69 73 20 61 20 5b 70 72 65 70 61 72  ter is a [prepar
2c2b0 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 0a 2a  ed statement]..*
2c2c0 2a 20 49 66 20 74 68 69 73 20 73 74 61 74 65 6d  * If this statem
2c2d0 65 6e 74 20 69 73 20 61 20 5b 53 45 4c 45 43 54  ent is a [SELECT
2c2e0 5d 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  ] statement and 
2c2f0 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 6f  the Nth column o
2c300 66 20 74 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65  f the.** returne
2c310 64 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  d result set of 
2c320 74 68 61 74 20 5b 53 45 4c 45 43 54 5d 20 69 73  that [SELECT] is
2c330 20 61 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20   a table column 
2c340 28 6e 6f 74 20 61 6e 0a 2a 2a 20 65 78 70 72 65  (not an.** expre
2c350 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65 72  ssion or subquer
2c360 79 29 20 74 68 65 6e 20 74 68 65 20 64 65 63 6c  y) then the decl
2c370 61 72 65 64 20 74 79 70 65 20 6f 66 20 74 68 65  ared type of the
2c380 20 74 61 62 6c 65 0a 2a 2a 20 63 6f 6c 75 6d 6e   table.** column
2c390 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 49   is returned.  I
2c3a0 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  f the Nth column
2c3b0 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
2c3c0 65 74 20 69 73 20 61 6e 0a 2a 2a 20 65 78 70 72  et is an.** expr
2c3d0 65 73 73 69 6f 6e 20 6f 72 20 73 75 62 71 75 65  ession or subque
2c3e0 72 79 2c 20 74 68 65 6e 20 61 20 4e 55 4c 4c 20  ry, then a NULL 
2c3f0 70 6f 69 6e 74 65 72 20 69 73 20 72 65 74 75 72  pointer is retur
2c400 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75  ned..** The retu
2c410 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20 61  rned string is a
2c420 6c 77 61 79 73 20 55 54 46 2d 38 20 65 6e 63 6f  lways UTF-8 enco
2c430 64 65 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a 2a  ded. {END}.**.**
2c440 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 67 69   For example, gi
2c450 76 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  ven the database
2c460 20 73 63 68 65 6d 61 3a 0a 2a 2a 0a 2a 2a 20 43   schema:.**.** C
2c470 52 45 41 54 45 20 54 41 42 4c 45 20 74 31 28 63  REATE TABLE t1(c
2c480 31 20 56 41 52 49 41 4e 54 29 3b 0a 2a 2a 0a 2a  1 VARIANT);.**.*
2c490 2a 20 61 6e 64 20 74 68 65 20 66 6f 6c 6c 6f 77  * and the follow
2c4a0 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  ing statement to
2c4b0 20 62 65 20 63 6f 6d 70 69 6c 65 64 3a 0a 2a 2a   be compiled:.**
2c4c0 0a 2a 2a 20 53 45 4c 45 43 54 20 63 31 20 2b 20  .** SELECT c1 + 
2c4d0 31 2c 20 63 31 20 46 52 4f 4d 20 74 31 3b 0a 2a  1, c1 FROM t1;.*
2c4e0 2a 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e  *.** this routin
2c4f0 65 20 77 6f 75 6c 64 20 72 65 74 75 72 6e 20 74  e would return t
2c500 68 65 20 73 74 72 69 6e 67 20 22 56 41 52 49 41  he string "VARIA
2c510 4e 54 22 20 66 6f 72 20 74 68 65 20 73 65 63 6f  NT" for the seco
2c520 6e 64 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f 6c  nd result.** col
2c530 75 6d 6e 20 28 69 3d 3d 31 29 2c 20 61 6e 64 20  umn (i==1), and 
2c540 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 66  a NULL pointer f
2c550 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65 73  or the first res
2c560 75 6c 74 20 63 6f 6c 75 6d 6e 20 28 69 3d 3d 30  ult column (i==0
2c570 29 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  )..**.** SQLite 
2c580 75 73 65 73 20 64 79 6e 61 6d 69 63 20 72 75 6e  uses dynamic run
2c590 2d 74 69 6d 65 20 74 79 70 69 6e 67 2e 20 20 53  -time typing.  S
2c5a0 6f 20 6a 75 73 74 20 62 65 63 61 75 73 65 20 61  o just because a
2c5b0 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 64 65   column.** is de
2c5c0 63 6c 61 72 65 64 20 74 6f 20 63 6f 6e 74 61 69  clared to contai
2c5d0 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 74  n a particular t
2c5e0 79 70 65 20 64 6f 65 73 20 6e 6f 74 20 6d 65 61  ype does not mea
2c5f0 6e 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 64 61  n that the.** da
2c600 74 61 20 73 74 6f 72 65 64 20 69 6e 20 74 68 61  ta stored in tha
2c610 74 20 63 6f 6c 75 6d 6e 20 69 73 20 6f 66 20 74  t column is of t
2c620 68 65 20 64 65 63 6c 61 72 65 64 20 74 79 70 65  he declared type
2c630 2e 20 20 53 51 4c 69 74 65 20 69 73 0a 2a 2a 20  .  SQLite is.** 
2c640 73 74 72 6f 6e 67 6c 79 20 74 79 70 65 64 2c 20  strongly typed, 
2c650 62 75 74 20 74 68 65 20 74 79 70 69 6e 67 20 69  but the typing i
2c660 73 20 64 79 6e 61 6d 69 63 20 6e 6f 74 20 73 74  s dynamic not st
2c670 61 74 69 63 2e 20 20 54 79 70 65 0a 2a 2a 20 69  atic.  Type.** i
2c680 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
2c690 68 20 69 6e 64 69 76 69 64 75 61 6c 20 76 61 6c  h individual val
2c6a0 75 65 73 2c 20 6e 6f 74 20 77 69 74 68 20 74 68  ues, not with th
2c6b0 65 20 63 6f 6e 74 61 69 6e 65 72 73 0a 2a 2a 20  e containers.** 
2c6c0 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 6f  used to hold tho
2c6d0 73 65 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  se values..**.**
2c6e0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
2c6f0 2a 2a 20 7b 48 31 33 37 36 31 7d 20 20 41 20 73  ** {H13761}  A s
2c700 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
2c710 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  o [sqlite3_colum
2c720 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29 5d  n_decltype(S,N)]
2c730 20 72 65 74 75 72 6e 73 20 61 0a 2a 2a 20 20 20   returns a.**   
2c740 20 20 20 20 20 20 20 20 7a 65 72 6f 2d 74 65 72          zero-ter
2c750 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73 74  minated UTF-8 st
2c760 72 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ring containing 
2c770 74 68 65 20 64 65 63 6c 61 72 65 64 20 64 61 74  the declared dat
2c780 61 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20 20  atype.**        
2c790 20 20 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20     of the table 
2c7a0 63 6f 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65  column that appe
2c7b0 61 72 73 20 61 73 20 74 68 65 20 4e 74 68 20 63  ars as the Nth c
2c7c0 6f 6c 75 6d 6e 20 28 6e 75 6d 62 65 72 65 64 0a  olumn (numbered.
2c7d0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 66 72 6f  **           fro
2c7e0 6d 20 30 29 20 6f 66 20 74 68 65 20 72 65 73 75  m 0) of the resu
2c7f0 6c 74 20 73 65 74 20 74 6f 20 74 68 65 20 5b 70  lt set to the [p
2c800 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
2c810 74 5d 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  t] S..**.** {H13
2c820 37 36 32 7d 20 20 41 20 73 75 63 63 65 73 73 66  762}  A successf
2c830 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  ul call to [sqli
2c840 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74  te3_column_declt
2c850 79 70 65 31 36 28 53 2c 4e 29 5d 0a 2a 2a 20 20  ype16(S,N)].**  
2c860 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73           returns
2c870 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
2c880 65 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65  ed UTF-16 native
2c890 20 62 79 74 65 20 6f 72 64 65 72 20 73 74 72 69   byte order stri
2c8a0 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ng.**           
2c8b0 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 64  containing the d
2c8c0 65 63 6c 61 72 65 64 20 64 61 74 61 74 79 70 65  eclared datatype
2c8d0 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 63 6f   of the table co
2c8e0 6c 75 6d 6e 20 74 68 61 74 20 61 70 70 65 61 72  lumn that appear
2c8f0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61  s.**           a
2c900 73 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d 6e  s the Nth column
2c910 20 28 6e 75 6d 62 65 72 65 64 20 66 72 6f 6d 20   (numbered from 
2c920 30 29 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74  0) of the result
2c930 20 73 65 74 20 74 6f 20 74 68 65 0a 2a 2a 20 20   set to the.**  
2c940 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61 72           [prepar
2c950 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 2e  ed statement] S.
2c960 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 37 36 33 7d 20  .**.** {H13763} 
2c970 20 49 66 20 4e 20 69 73 20 6c 65 73 73 20 74 68   If N is less th
2c980 61 6e 20 30 20 6f 72 20 4e 20 69 73 20 67 72 65  an 0 or N is gre
2c990 61 74 65 72 20 74 68 61 6e 20 6f 72 20 65 71 75  ater than or equ
2c9a0 61 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  al to.**        
2c9b0 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66     the number of
2c9c0 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
2c9d0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
2c9e0 65 6e 74 5d 20 53 2c 0a 2a 2a 20 20 20 20 20 20  ent] S,.**      
2c9f0 20 20 20 20 20 6f 72 20 69 66 20 74 68 65 20 4e       or if the N
2ca00 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 53 20 69  th column of S i
2ca10 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
2ca20 6f 72 20 73 75 62 71 75 65 72 79 20 72 61 74 68  or subquery rath
2ca30 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  er.**           
2ca40 74 68 61 6e 20 61 20 74 61 62 6c 65 20 63 6f 6c  than a table col
2ca50 75 6d 6e 2c 20 6f 72 20 69 66 20 61 20 6d 65 6d  umn, or if a mem
2ca60 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  ory allocation f
2ca70 61 69 6c 75 72 65 0a 2a 2a 20 20 20 20 20 20 20  ailure.**       
2ca80 20 20 20 20 6f 63 63 75 72 73 20 64 75 72 69 6e      occurs durin
2ca90 67 20 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65  g encoding conve
2caa0 72 73 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20  rsions, then.** 
2cab0 20 20 20 20 20 20 20 20 20 20 63 61 6c 6c 73 20            calls 
2cac0 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  to [sqlite3_colu
2cad0 6d 6e 5f 64 65 63 6c 74 79 70 65 28 53 2c 4e 29  mn_decltype(S,N)
2cae0 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ] or.**         
2caf0 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d    [sqlite3_colum
2cb00 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 53 2c 4e  n_decltype16(S,N
2cb10 29 5d 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a  )] return NULL..
2cb20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  */.SQLITE_API co
2cb30 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
2cb40 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
2cb50 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  e(sqlite3_stmt*,
2cb60 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  int);.SQLITE_API
2cb70 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
2cb80 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c  ite3_column_decl
2cb90 74 79 70 65 31 36 28 73 71 6c 69 74 65 33 5f 73  type16(sqlite3_s
2cba0 74 6d 74 2a 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  tmt*,int);../*.*
2cbb0 2a 20 43 41 50 49 33 52 45 46 3a 20 45 76 61 6c  * CAPI3REF: Eval
2cbc0 75 61 74 65 20 41 6e 20 53 51 4c 20 53 74 61 74  uate An SQL Stat
2cbd0 65 6d 65 6e 74 20 7b 48 31 33 32 30 30 7d 20 3c  ement {H13200} <
2cbe0 53 31 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 66  S10000>.**.** Af
2cbf0 74 65 72 20 61 20 5b 70 72 65 70 61 72 65 64 20  ter a [prepared 
2cc00 73 74 61 74 65 6d 65 6e 74 5d 20 68 61 73 20 62  statement] has b
2cc10 65 65 6e 20 70 72 65 70 61 72 65 64 20 75 73 69  een prepared usi
2cc20 6e 67 20 65 69 74 68 65 72 0a 2a 2a 20 5b 73 71  ng either.** [sq
2cc30 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32  lite3_prepare_v2
2cc40 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  ()] or [sqlite3_
2cc50 70 72 65 70 61 72 65 31 36 5f 76 32 28 29 5d 20  prepare16_v2()] 
2cc60 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 65  or one of the le
2cc70 67 61 63 79 0a 2a 2a 20 69 6e 74 65 72 66 61 63  gacy.** interfac
2cc80 65 73 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  es [sqlite3_prep
2cc90 61 72 65 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  are()] or [sqlit
2cca0 65 33 5f 70 72 65 70 61 72 65 31 36 28 29 5d 2c  e3_prepare16()],
2ccb0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 0a 2a   this function.*
2ccc0 2a 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  * must be called
2ccd0 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 74 69 6d   one or more tim
2cce0 65 73 20 74 6f 20 65 76 61 6c 75 61 74 65 20 74  es to evaluate t
2ccf0 68 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  he statement..**
2cd00 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20  .** The details 
2cd10 6f 66 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  of the behavior 
2cd20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  of the sqlite3_s
2cd30 74 65 70 28 29 20 69 6e 74 65 72 66 61 63 65 20  tep() interface 
2cd40 64 65 70 65 6e 64 0a 2a 2a 20 6f 6e 20 77 68 65  depend.** on whe
2cd50 74 68 65 72 20 74 68 65 20 73 74 61 74 65 6d 65  ther the stateme
2cd60 6e 74 20 77 61 73 20 70 72 65 70 61 72 65 64 20  nt was prepared 
2cd70 75 73 69 6e 67 20 74 68 65 20 6e 65 77 65 72 20  using the newer 
2cd80 22 76 32 22 20 69 6e 74 65 72 66 61 63 65 0a 2a  "v2" interface.*
2cd90 2a 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61  * [sqlite3_prepa
2cda0 72 65 5f 76 32 28 29 5d 20 61 6e 64 20 5b 73 71  re_v2()] and [sq
2cdb0 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f  lite3_prepare16_
2cdc0 76 32 28 29 5d 20 6f 72 20 74 68 65 20 6f 6c 64  v2()] or the old
2cdd0 65 72 20 6c 65 67 61 63 79 0a 2a 2a 20 69 6e 74  er legacy.** int
2cde0 65 72 66 61 63 65 20 5b 73 71 6c 69 74 65 33 5f  erface [sqlite3_
2cdf0 70 72 65 70 61 72 65 28 29 5d 20 61 6e 64 20 5b  prepare()] and [
2ce00 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2ce10 36 28 29 5d 2e 20 20 54 68 65 20 75 73 65 20 6f  6()].  The use o
2ce20 66 20 74 68 65 0a 2a 2a 20 6e 65 77 20 22 76 32  f the.** new "v2
2ce30 22 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 72  " interface is r
2ce40 65 63 6f 6d 6d 65 6e 64 65 64 20 66 6f 72 20 6e  ecommended for n
2ce50 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20  ew applications 
2ce60 62 75 74 20 74 68 65 20 6c 65 67 61 63 79 0a 2a  but the legacy.*
2ce70 2a 20 69 6e 74 65 72 66 61 63 65 20 77 69 6c 6c  * interface will
2ce80 20 63 6f 6e 74 69 6e 75 65 20 74 6f 20 62 65 20   continue to be 
2ce90 73 75 70 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a  supported..**.**
2cea0 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79 20 69   In the legacy i
2ceb0 6e 74 65 72 66 61 63 65 2c 20 74 68 65 20 72 65  nterface, the re
2cec0 74 75 72 6e 20 76 61 6c 75 65 20 77 69 6c 6c 20  turn value will 
2ced0 62 65 20 65 69 74 68 65 72 20 5b 53 51 4c 49 54  be either [SQLIT
2cee0 45 5f 42 55 53 59 5d 2c 0a 2a 2a 20 5b 53 51 4c  E_BUSY],.** [SQL
2cef0 49 54 45 5f 44 4f 4e 45 5d 2c 20 5b 53 51 4c 49  ITE_DONE], [SQLI
2cf00 54 45 5f 52 4f 57 5d 2c 20 5b 53 51 4c 49 54 45  TE_ROW], [SQLITE
2cf10 5f 45 52 52 4f 52 5d 2c 20 6f 72 20 5b 53 51 4c  _ERROR], or [SQL
2cf20 49 54 45 5f 4d 49 53 55 53 45 5d 2e 0a 2a 2a 20  ITE_MISUSE]..** 
2cf30 57 69 74 68 20 74 68 65 20 22 76 32 22 20 69 6e  With the "v2" in
2cf40 74 65 72 66 61 63 65 2c 20 61 6e 79 20 6f 66 20  terface, any of 
2cf50 74 68 65 20 6f 74 68 65 72 20 5b 72 65 73 75 6c  the other [resul
2cf60 74 20 63 6f 64 65 73 5d 20 6f 72 0a 2a 2a 20 5b  t codes] or.** [
2cf70 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74 20  extended result 
2cf80 63 6f 64 65 73 5d 20 6d 69 67 68 74 20 62 65 20  codes] might be 
2cf90 72 65 74 75 72 6e 65 64 20 61 73 20 77 65 6c 6c  returned as well
2cfa0 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f  ..**.** [SQLITE_
2cfb0 42 55 53 59 5d 20 6d 65 61 6e 73 20 74 68 61 74  BUSY] means that
2cfc0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e   the database en
2cfd0 67 69 6e 65 20 77 61 73 20 75 6e 61 62 6c 65 20  gine was unable 
2cfe0 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 0a 2a  to acquire the.*
2cff0 2a 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 73  * database locks
2d000 20 69 74 20 6e 65 65 64 73 20 74 6f 20 64 6f 20   it needs to do 
2d010 69 74 73 20 6a 6f 62 2e 20 20 49 66 20 74 68 65  its job.  If the
2d020 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 20   statement is a 
2d030 5b 43 4f 4d 4d 49 54 5d 0a 2a 2a 20 6f 72 20 6f  [COMMIT].** or o
2d040 63 63 75 72 73 20 6f 75 74 73 69 64 65 20 6f 66  ccurs outside of
2d050 20 61 6e 20 65 78 70 6c 69 63 69 74 20 74 72 61   an explicit tra
2d060 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 79  nsaction, then y
2d070 6f 75 20 63 61 6e 20 72 65 74 72 79 20 74 68 65  ou can retry the
2d080 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  .** statement.  
2d090 49 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  If the statement
2d0a0 20 69 73 20 6e 6f 74 20 61 20 5b 43 4f 4d 4d 49   is not a [COMMI
2d0b0 54 5d 20 61 6e 64 20 6f 63 63 75 72 73 20 77 69  T] and occurs wi
2d0c0 74 68 69 6e 20 61 0a 2a 2a 20 65 78 70 6c 69 63  thin a.** explic
2d0d0 69 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  it transaction t
2d0e0 68 65 6e 20 79 6f 75 20 73 68 6f 75 6c 64 20 72  hen you should r
2d0f0 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e  ollback the tran
2d100 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 0a 2a  saction before.*
2d110 2a 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 0a 2a 2a  * continuing..**
2d120 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45  .** [SQLITE_DONE
2d130 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
2d140 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20 66   statement has f
2d150 69 6e 69 73 68 65 64 20 65 78 65 63 75 74 69 6e  inished executin
2d160 67 0a 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 6c  g.** successfull
2d170 79 2e 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70  y.  sqlite3_step
2d180 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65  () should not be
2d190 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 6f 6e   called again on
2d1a0 20 74 68 69 73 20 76 69 72 74 75 61 6c 0a 2a 2a   this virtual.**
2d1b0 20 6d 61 63 68 69 6e 65 20 77 69 74 68 6f 75 74   machine without
2d1c0 20 66 69 72 73 74 20 63 61 6c 6c 69 6e 67 20 5b   first calling [
2d1d0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d  sqlite3_reset()]
2d1e0 20 74 6f 20 72 65 73 65 74 20 74 68 65 20 76 69   to reset the vi
2d1f0 72 74 75 61 6c 0a 2a 2a 20 6d 61 63 68 69 6e 65  rtual.** machine
2d200 20 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69   back to its ini
2d210 74 69 61 6c 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a  tial state..**.*
2d220 2a 20 49 66 20 74 68 65 20 53 51 4c 20 73 74 61  * If the SQL sta
2d230 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 65 78 65  tement being exe
2d240 63 75 74 65 64 20 72 65 74 75 72 6e 73 20 61 6e  cuted returns an
2d250 79 20 64 61 74 61 2c 20 74 68 65 6e 20 5b 53 51  y data, then [SQ
2d260 4c 49 54 45 5f 52 4f 57 5d 0a 2a 2a 20 69 73 20  LITE_ROW].** is 
2d270 72 65 74 75 72 6e 65 64 20 65 61 63 68 20 74 69  returned each ti
2d280 6d 65 20 61 20 6e 65 77 20 72 6f 77 20 6f 66 20  me a new row of 
2d290 64 61 74 61 20 69 73 20 72 65 61 64 79 20 66 6f  data is ready fo
2d2a0 72 20 70 72 6f 63 65 73 73 69 6e 67 20 62 79 20  r processing by 
2d2b0 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72 2e 20 54  the.** caller. T
2d2c0 68 65 20 76 61 6c 75 65 73 20 6d 61 79 20 62 65  he values may be
2d2d0 20 61 63 63 65 73 73 65 64 20 75 73 69 6e 67 20   accessed using 
2d2e0 74 68 65 20 5b 63 6f 6c 75 6d 6e 20 61 63 63 65  the [column acce
2d2f0 73 73 20 66 75 6e 63 74 69 6f 6e 73 5d 2e 0a 2a  ss functions]..*
2d300 2a 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  * sqlite3_step()
2d310 20 69 73 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   is called again
2d320 20 74 6f 20 72 65 74 72 69 65 76 65 20 74 68 65   to retrieve the
2d330 20 6e 65 78 74 20 72 6f 77 20 6f 66 20 64 61 74   next row of dat
2d340 61 2e 0a 2a 2a 0a 2a 2a 20 5b 53 51 4c 49 54 45  a..**.** [SQLITE
2d350 5f 45 52 52 4f 52 5d 20 6d 65 61 6e 73 20 74 68  _ERROR] means th
2d360 61 74 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72  at a run-time er
2d370 72 6f 72 20 28 73 75 63 68 20 61 73 20 61 20 63  ror (such as a c
2d380 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 76 69 6f  onstraint.** vio
2d390 6c 61 74 69 6f 6e 29 20 68 61 73 20 6f 63 63 75  lation) has occu
2d3a0 72 72 65 64 2e 20 20 73 71 6c 69 74 65 33 5f 73  rred.  sqlite3_s
2d3b0 74 65 70 28 29 20 73 68 6f 75 6c 64 20 6e 6f 74  tep() should not
2d3c0 20 62 65 20 63 61 6c 6c 65 64 20 61 67 61 69 6e   be called again
2d3d0 20 6f 6e 0a 2a 2a 20 74 68 65 20 56 4d 2e 20 4d   on.** the VM. M
2d3e0 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ore information 
2d3f0 6d 61 79 20 62 65 20 66 6f 75 6e 64 20 62 79 20  may be found by 
2d400 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33  calling [sqlite3
2d410 5f 65 72 72 6d 73 67 28 29 5d 2e 0a 2a 2a 20 57  _errmsg()]..** W
2d420 69 74 68 20 74 68 65 20 6c 65 67 61 63 79 20 69  ith the legacy i
2d430 6e 74 65 72 66 61 63 65 2c 20 61 20 6d 6f 72 65  nterface, a more
2d440 20 73 70 65 63 69 66 69 63 20 65 72 72 6f 72 20   specific error 
2d450 63 6f 64 65 20 28 66 6f 72 20 65 78 61 6d 70 6c  code (for exampl
2d460 65 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 49 4e  e,.** [SQLITE_IN
2d470 54 45 52 52 55 50 54 5d 2c 20 5b 53 51 4c 49 54  TERRUPT], [SQLIT
2d480 45 5f 53 43 48 45 4d 41 5d 2c 20 5b 53 51 4c 49  E_SCHEMA], [SQLI
2d490 54 45 5f 43 4f 52 52 55 50 54 5d 2c 20 61 6e 64  TE_CORRUPT], and
2d4a0 20 73 6f 20 66 6f 72 74 68 29 0a 2a 2a 20 63 61   so forth).** ca
2d4b0 6e 20 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79  n be obtained by
2d4c0 20 63 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65   calling [sqlite
2d4d0 33 5f 72 65 73 65 74 28 29 5d 20 6f 6e 20 74 68  3_reset()] on th
2d4e0 65 0a 2a 2a 20 5b 70 72 65 70 61 72 65 64 20 73  e.** [prepared s
2d4f0 74 61 74 65 6d 65 6e 74 5d 2e 20 20 49 6e 20 74  tatement].  In t
2d500 68 65 20 22 76 32 22 20 69 6e 74 65 72 66 61 63  he "v2" interfac
2d510 65 2c 0a 2a 2a 20 74 68 65 20 6d 6f 72 65 20 73  e,.** the more s
2d520 70 65 63 69 66 69 63 20 65 72 72 6f 72 20 63 6f  pecific error co
2d530 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 64  de is returned d
2d540 69 72 65 63 74 6c 79 20 62 79 20 73 71 6c 69 74  irectly by sqlit
2d550 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2a 0a 2a 2a  e3_step()..**.**
2d560 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d   [SQLITE_MISUSE]
2d570 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
2d580 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61 73  this routine was
2d590 20 63 61 6c 6c 65 64 20 69 6e 61 70 70 72 6f 70   called inapprop
2d5a0 72 69 61 74 65 6c 79 2e 0a 2a 2a 20 50 65 72 68  riately..** Perh
2d5b0 61 70 73 20 69 74 20 77 61 73 20 63 61 6c 6c 65  aps it was calle
2d5c0 64 20 6f 6e 20 61 20 5b 70 72 65 70 61 72 65 64  d on a [prepared
2d5d0 20 73 74 61 74 65 6d 65 6e 74 5d 20 74 68 61 74   statement] that
2d5e0 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20   has.** already 
2d5f0 62 65 65 6e 20 5b 73 71 6c 69 74 65 33 5f 66 69  been [sqlite3_fi
2d600 6e 61 6c 69 7a 65 20 7c 20 66 69 6e 61 6c 69 7a  nalize | finaliz
2d610 65 64 5d 20 6f 72 20 6f 6e 20 6f 6e 65 20 74 68  ed] or on one th
2d620 61 74 20 68 61 64 0a 2a 2a 20 70 72 65 76 69 6f  at had.** previo
2d630 75 73 6c 79 20 72 65 74 75 72 6e 65 64 20 5b 53  usly returned [S
2d640 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 6f 72 20  QLITE_ERROR] or 
2d650 5b 53 51 4c 49 54 45 5f 44 4f 4e 45 5d 2e 20 20  [SQLITE_DONE].  
2d660 4f 72 20 69 74 20 63 6f 75 6c 64 0a 2a 2a 20 62  Or it could.** b
2d670 65 20 74 68 65 20 63 61 73 65 20 74 68 61 74 20  e the case that 
2d680 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
2d690 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
2d6a0 62 65 69 6e 67 20 75 73 65 64 20 62 79 20 74 77  being used by tw
2d6b0 6f 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 68 72  o or.** more thr
2d6c0 65 61 64 73 20 61 74 20 74 68 65 20 73 61 6d 65  eads at the same
2d6d0 20 6d 6f 6d 65 6e 74 20 69 6e 20 74 69 6d 65 2e   moment in time.
2d6e0 0a 2a 2a 0a 2a 2a 20 3c 62 3e 47 6f 6f 66 79 20  .**.** <b>Goofy 
2d6f0 49 6e 74 65 72 66 61 63 65 20 41 6c 65 72 74 3a  Interface Alert:
2d700 3c 2f 62 3e 20 49 6e 20 74 68 65 20 6c 65 67 61  </b> In the lega
2d710 63 79 20 69 6e 74 65 72 66 61 63 65 2c 20 74 68  cy interface, th
2d720 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
2d730 0a 2a 2a 20 41 50 49 20 61 6c 77 61 79 73 20 72  .** API always r
2d740 65 74 75 72 6e 73 20 61 20 67 65 6e 65 72 69 63  eturns a generic
2d750 20 65 72 72 6f 72 20 63 6f 64 65 2c 20 5b 53 51   error code, [SQ
2d760 4c 49 54 45 5f 45 52 52 4f 52 5d 2c 20 66 6f 6c  LITE_ERROR], fol
2d770 6c 6f 77 69 6e 67 20 61 6e 79 0a 2a 2a 20 65 72  lowing any.** er
2d780 72 6f 72 20 6f 74 68 65 72 20 74 68 61 6e 20 5b  ror other than [
2d790 53 51 4c 49 54 45 5f 42 55 53 59 5d 20 61 6e 64  SQLITE_BUSY] and
2d7a0 20 5b 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 5d   [SQLITE_MISUSE]
2d7b0 2e 20 20 59 6f 75 20 6d 75 73 74 20 63 61 6c 6c  .  You must call
2d7c0 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 72 65 73  .** [sqlite3_res
2d7d0 65 74 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65  et()] or [sqlite
2d7e0 33 5f 66 69 6e 61 6c 69 7a 65 28 29 5d 20 69 6e  3_finalize()] in
2d7f0 20 6f 72 64 65 72 20 74 6f 20 66 69 6e 64 20 6f   order to find o
2d800 6e 65 20 6f 66 20 74 68 65 0a 2a 2a 20 73 70 65  ne of the.** spe
2d810 63 69 66 69 63 20 5b 65 72 72 6f 72 20 63 6f 64  cific [error cod
2d820 65 73 5d 20 74 68 61 74 20 62 65 74 74 65 72 20  es] that better 
2d830 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65 72  describes the er
2d840 72 6f 72 2e 0a 2a 2a 20 57 65 20 61 64 6d 69 74  ror..** We admit
2d850 20 74 68 61 74 20 74 68 69 73 20 69 73 20 61 20   that this is a 
2d860 67 6f 6f 66 79 20 64 65 73 69 67 6e 2e 20 20 54  goofy design.  T
2d870 68 65 20 70 72 6f 62 6c 65 6d 20 68 61 73 20 62  he problem has b
2d880 65 65 6e 20 66 69 78 65 64 0a 2a 2a 20 77 69 74  een fixed.** wit
2d890 68 20 74 68 65 20 22 76 32 22 20 69 6e 74 65 72  h the "v2" inter
2d8a0 66 61 63 65 2e 20 20 49 66 20 79 6f 75 20 70 72  face.  If you pr
2d8b0 65 70 61 72 65 20 61 6c 6c 20 6f 66 20 79 6f 75  epare all of you
2d8c0 72 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73  r SQL statements
2d8d0 0a 2a 2a 20 75 73 69 6e 67 20 65 69 74 68 65 72  .** using either
2d8e0 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72   [sqlite3_prepar
2d8f0 65 5f 76 32 28 29 5d 20 6f 72 20 5b 73 71 6c 69  e_v2()] or [sqli
2d900 74 65 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32  te3_prepare16_v2
2d910 28 29 5d 20 69 6e 73 74 65 61 64 0a 2a 2a 20 6f  ()] instead.** o
2d920 66 20 74 68 65 20 6c 65 67 61 63 79 20 5b 73 71  f the legacy [sq
2d930 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d  lite3_prepare()]
2d940 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 70 72   and [sqlite3_pr
2d950 65 70 61 72 65 31 36 28 29 5d 20 69 6e 74 65 72  epare16()] inter
2d960 66 61 63 65 73 2c 0a 2a 2a 20 74 68 65 6e 20 74  faces,.** then t
2d970 68 65 20 6d 6f 72 65 20 73 70 65 63 69 66 69 63  he more specific
2d980 20 5b 65 72 72 6f 72 20 63 6f 64 65 73 5d 20 61   [error codes] a
2d990 72 65 20 72 65 74 75 72 6e 65 64 20 64 69 72 65  re returned dire
2d9a0 63 74 6c 79 0a 2a 2a 20 62 79 20 73 71 6c 69 74  ctly.** by sqlit
2d9b0 65 33 5f 73 74 65 70 28 29 2e 20 20 54 68 65 20  e3_step().  The 
2d9c0 75 73 65 20 6f 66 20 74 68 65 20 22 76 32 22 20  use of the "v2" 
2d9d0 69 6e 74 65 72 66 61 63 65 20 69 73 20 72 65 63  interface is rec
2d9e0 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a 2a 2a 20  ommended..**.** 
2d9f0 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a  INVARIANTS:.**.*
2da00 2a 20 7b 48 31 33 32 30 32 7d 20 20 49 66 20 74  * {H13202}  If t
2da10 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61  he [prepared sta
2da20 74 65 6d 65 6e 74 5d 20 53 20 69 73 20 72 65 61  tement] S is rea
2da30 64 79 20 74 6f 20 62 65 20 72 75 6e 2c 20 74 68  dy to be run, th
2da40 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  en.**           
2da50 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29  [sqlite3_step(S)
2da60 5d 20 61 64 76 61 6e 63 65 73 20 74 68 61 74 20  ] advances that 
2da70 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
2da80 6e 74 20 75 6e 74 69 6c 0a 2a 2a 20 20 20 20 20  nt until.**     
2da90 20 20 20 20 20 20 63 6f 6d 70 6c 65 74 69 6f 6e        completion
2daa0 20 6f 72 20 75 6e 74 69 6c 20 69 74 20 69 73 20   or until it is 
2dab0 72 65 61 64 79 20 74 6f 20 72 65 74 75 72 6e 20  ready to return 
2dac0 61 6e 6f 74 68 65 72 20 72 6f 77 20 6f 66 20 74  another row of t
2dad0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
2dae0 72 65 73 75 6c 74 20 73 65 74 2c 20 6f 72 20 75  result set, or u
2daf0 6e 74 69 6c 20 61 6e 20 5b 73 71 6c 69 74 65 33  ntil an [sqlite3
2db00 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e 74  _interrupt | int
2db10 65 72 72 75 70 74 5d 0a 2a 2a 20 20 20 20 20 20  errupt].**      
2db20 20 20 20 20 20 6f 72 20 61 20 72 75 6e 2d 74 69       or a run-ti
2db30 6d 65 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2e  me error occurs.
2db40 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 30 34 7d 20  .**.** {H15304} 
2db50 20 57 68 65 6e 20 61 20 63 61 6c 6c 20 74 6f 20   When a call to 
2db60 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 53 29  [sqlite3_step(S)
2db70 5d 20 63 61 75 73 65 73 20 74 68 65 20 5b 70 72  ] causes the [pr
2db80 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
2db90 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 53  ].**           S
2dba0 20 74 6f 20 72 75 6e 20 74 6f 20 63 6f 6d 70 6c   to run to compl
2dbb0 65 74 69 6f 6e 2c 20 74 68 65 20 66 75 6e 63 74  etion, the funct
2dbc0 69 6f 6e 20 72 65 74 75 72 6e 73 20 5b 53 51 4c  ion returns [SQL
2dbd0 49 54 45 5f 44 4f 4e 45 5d 2e 0a 2a 2a 0a 2a 2a  ITE_DONE]..**.**
2dbe0 20 7b 48 31 35 33 30 36 7d 20 20 57 68 65 6e 20   {H15306}  When 
2dbf0 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
2dc00 65 33 5f 73 74 65 70 28 53 29 5d 20 73 74 6f 70  e3_step(S)] stop
2dc10 73 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20  s because it is 
2dc20 72 65 61 64 79 20 74 6f 0a 2a 2a 20 20 20 20 20  ready to.**     
2dc30 20 20 20 20 20 20 72 65 74 75 72 6e 20 61 6e 6f        return ano
2dc40 74 68 65 72 20 72 6f 77 20 6f 66 20 74 68 65 20  ther row of the 
2dc50 72 65 73 75 6c 74 20 73 65 74 2c 20 69 74 20 72  result set, it r
2dc60 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52  eturns [SQLITE_R
2dc70 4f 57 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33  OW]..**.** {H153
2dc80 30 38 7d 20 20 49 66 20 61 20 63 61 6c 6c 20 74  08}  If a call t
2dc90 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
2dca0 53 29 5d 20 65 6e 63 6f 75 6e 74 65 72 73 20 61  S)] encounters a
2dcb0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  n.**           [
2dcc0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
2dcd0 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 20 6f  t | interrupt] o
2dce0 72 20 61 20 72 75 6e 2d 74 69 6d 65 20 65 72 72  r a run-time err
2dcf0 6f 72 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  or,.**          
2dd00 20 69 74 20 72 65 74 75 72 6e 73 20 61 6e 20 61   it returns an a
2dd10 70 70 72 6f 70 72 69 61 74 65 20 65 72 72 6f 72  ppropriate error
2dd20 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 6e 6f   code that is no
2dd30 74 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20 20 20  t one of.**     
2dd40 20 20 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b        [SQLITE_OK
2dd50 5d 2c 20 5b 53 51 4c 49 54 45 5f 52 4f 57 5d 2c  ], [SQLITE_ROW],
2dd60 20 6f 72 20 5b 53 51 4c 49 54 45 5f 44 4f 4e 45   or [SQLITE_DONE
2dd70 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 33 31 30  ]..**.** {H15310
2dd80 7d 20 20 49 66 20 61 6e 20 5b 73 71 6c 69 74 65  }  If an [sqlite
2dd90 33 5f 69 6e 74 65 72 72 75 70 74 20 7c 20 69 6e  3_interrupt | in
2dda0 74 65 72 72 75 70 74 5d 20 6f 72 20 61 20 72 75  terrupt] or a ru
2ddb0 6e 2d 74 69 6d 65 20 65 72 72 6f 72 0a 2a 2a 20  n-time error.** 
2ddc0 20 20 20 20 20 20 20 20 20 20 6f 63 63 75 72 73            occurs
2ddd0 20 64 75 72 69 6e 67 20 61 20 63 61 6c 6c 20 74   during a call t
2dde0 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65 70 28  o [sqlite3_step(
2ddf0 53 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  S)].**          
2de00 20 66 6f 72 20 61 20 5b 70 72 65 70 61 72 65 64   for a [prepared
2de10 20 73 74 61 74 65 6d 65 6e 74 5d 20 53 20 63 72   statement] S cr
2de20 65 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20  eated using.**  
2de30 20 20 20 20 20 20 20 20 20 6c 65 67 61 63 79 20           legacy 
2de40 69 6e 74 65 72 66 61 63 65 73 20 5b 73 71 6c 69  interfaces [sqli
2de50 74 65 33 5f 70 72 65 70 61 72 65 28 29 5d 20 6f  te3_prepare()] o
2de60 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b  r.**           [
2de70 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31  sqlite3_prepare1
2de80 36 28 29 5d 2c 20 74 68 65 6e 20 74 68 65 20 66  6()], then the f
2de90 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
2dea0 65 69 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20  either.**       
2deb0 20 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f      [SQLITE_ERRO
2dec0 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 55 53 59  R], [SQLITE_BUSY
2ded0 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f 4d 49  ], or [SQLITE_MI
2dee0 53 55 53 45 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  SUSE]..*/.SQLITE
2def0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2df00 5f 73 74 65 70 28 73 71 6c 69 74 65 33 5f 73 74  _step(sqlite3_st
2df10 6d 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  mt*);../*.** CAP
2df20 49 33 52 45 46 3a 20 4e 75 6d 62 65 72 20 6f 66  I3REF: Number of
2df30 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 61 20 72 65   columns in a re
2df40 73 75 6c 74 20 73 65 74 20 7b 48 31 33 37 37 30  sult set {H13770
2df50 7d 20 3c 53 31 30 37 30 30 3e 0a 2a 2a 0a 2a 2a  } <S10700>.**.**
2df60 20 52 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d   Returns the num
2df70 62 65 72 20 6f 66 20 76 61 6c 75 65 73 20 69 6e  ber of values in
2df80 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
2df90 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20 73   of the result s
2dfa0 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  et..**.** INVARI
2dfb0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  ANTS:.**.** {H13
2dfc0 37 37 31 7d 20 20 41 66 74 65 72 20 61 20 63 61  771}  After a ca
2dfd0 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73  ll to [sqlite3_s
2dfe0 74 65 70 28 53 29 5d 20 74 68 61 74 20 72 65 74  tep(S)] that ret
2dff0 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f 52 4f 57  urns [SQLITE_ROW
2e000 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ],.**           
2e010 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 61 74  the [sqlite3_dat
2e020 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72 6f 75 74  a_count(S)] rout
2e030 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ine will return 
2e040 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 0a 2a  the same value.*
2e050 2a 20 20 20 20 20 20 20 20 20 20 20 61 73 20 74  *           as t
2e060 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75  he [sqlite3_colu
2e070 6d 6e 5f 63 6f 75 6e 74 28 53 29 5d 20 66 75 6e  mn_count(S)] fun
2e080 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ction..**.** {H1
2e090 33 37 37 32 7d 20 20 41 66 74 65 72 20 5b 73 71  3772}  After [sq
2e0a0 6c 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68  lite3_step(S)] h
2e0b0 61 73 20 72 65 74 75 72 6e 65 64 20 61 6e 79 20  as returned any 
2e0c0 76 61 6c 75 65 20 6f 74 68 65 72 20 74 68 61 6e  value other than
2e0d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 53  .**           [S
2e0e0 51 4c 49 54 45 5f 52 4f 57 5d 20 6f 72 20 62 65  QLITE_ROW] or be
2e0f0 66 6f 72 65 20 5b 73 71 6c 69 74 65 33 5f 73 74  fore [sqlite3_st
2e100 65 70 28 53 29 5d 20 68 61 73 20 62 65 65 6e 20  ep(S)] has been 
2e110 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 0a 2a 2a  called on the.**
2e120 20 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70             [prep
2e130 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
2e140 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 74 69  for the first ti
2e150 6d 65 20 73 69 6e 63 65 20 69 74 20 77 61 73 0a  me since it was.
2e160 2a 2a 20 20 20 20 20 20 20 20 20 20 20 5b 73 71  **           [sq
2e170 6c 69 74 65 33 5f 70 72 65 70 61 72 65 20 7c 20  lite3_prepare | 
2e180 70 72 65 70 61 72 65 64 5d 20 6f 72 20 5b 73 71  prepared] or [sq
2e190 6c 69 74 65 33 5f 72 65 73 65 74 20 7c 20 72 65  lite3_reset | re
2e1a0 73 65 74 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  set],.**        
2e1b0 20 20 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f     the [sqlite3_
2e1c0 64 61 74 61 5f 63 6f 75 6e 74 28 53 29 5d 20 72  data_count(S)] r
2e1d0 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 7a  outine returns z
2e1e0 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ero..*/.SQLITE_A
2e1f0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64  PI int sqlite3_d
2e200 61 74 61 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ata_count(sqlite
2e210 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a  3_stmt *pStmt);.
2e220 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
2e230 20 46 75 6e 64 61 6d 65 6e 74 61 6c 20 44 61 74   Fundamental Dat
2e240 61 74 79 70 65 73 20 7b 48 31 30 32 36 35 7d 20  atypes {H10265} 
2e250 3c 53 31 30 31 31 30 3e 3c 53 31 30 31 32 30 3e  <S10110><S10120>
2e260 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 53 51  .** KEYWORDS: SQ
2e270 4c 49 54 45 5f 54 45 58 54 0a 2a 2a 0a 2a 2a 20  LITE_TEXT.**.** 
2e280 7b 48 31 30 32 36 36 7d 20 45 76 65 72 79 20 76  {H10266} Every v
2e290 61 6c 75 65 20 69 6e 20 53 51 4c 69 74 65 20 68  alue in SQLite h
2e2a0 61 73 20 6f 6e 65 20 6f 66 20 66 69 76 65 20 66  as one of five f
2e2b0 75 6e 64 61 6d 65 6e 74 61 6c 20 64 61 74 61 74  undamental datat
2e2c0 79 70 65 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ypes:.**.** <ul>
2e2d0 0a 2a 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20  .** <li> 64-bit 
2e2e0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a  signed integer.*
2e2f0 2a 20 3c 6c 69 3e 20 36 34 2d 62 69 74 20 49 45  * <li> 64-bit IE
2e300 45 45 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  EE floating poin
2e310 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 3c 6c 69 3e  t number.** <li>
2e320 20 73 74 72 69 6e 67 0a 2a 2a 20 3c 6c 69 3e 20   string.** <li> 
2e330 42 4c 4f 42 0a 2a 2a 20 3c 6c 69 3e 20 4e 55 4c  BLOB.** <li> NUL
2e340 4c 0a 2a 2a 20 3c 2f 75 6c 3e 20 7b 45 4e 44 7d  L.** </ul> {END}
2e350 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  .**.** These con
2e360 73 74 61 6e 74 73 20 61 72 65 20 63 6f 64 65 73  stants are codes
2e370 20 66 6f 72 20 65 61 63 68 20 6f 66 20 74 68 6f   for each of tho
2e380 73 65 20 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20  se types..**.** 
2e390 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 53 51  Note that the SQ
2e3a0 4c 49 54 45 5f 54 45 58 54 20 63 6f 6e 73 74 61  LITE_TEXT consta
2e3b0 6e 74 20 77 61 73 20 61 6c 73 6f 20 75 73 65 64  nt was also used
2e3c0 20 69 6e 20 53 51 4c 69 74 65 20 76 65 72 73 69   in SQLite versi
2e3d0 6f 6e 20 32 0a 2a 2a 20 66 6f 72 20 61 20 63 6f  on 2.** for a co
2e3e0 6d 70 6c 65 74 65 6c 79 20 64 69 66 66 65 72 65  mpletely differe
2e3f0 6e 74 20 6d 65 61 6e 69 6e 67 2e 20 20 53 6f 66  nt meaning.  Sof
2e400 74 77 61 72 65 20 74 68 61 74 20 6c 69 6e 6b 73  tware that links
2e410 20 61 67 61 69 6e 73 74 20 62 6f 74 68 0a 2a 2a   against both.**
2e420 20 53 51 4c 69 74 65 20 76 65 72 73 69 6f 6e 20   SQLite version 
2e430 32 20 61 6e 64 20 53 51 4c 69 74 65 20 76 65 72  2 and SQLite ver
2e440 73 69 6f 6e 20 33 20 73 68 6f 75 6c 64 20 75 73  sion 3 should us
2e450 65 20 53 51 4c 49 54 45 33 5f 54 45 58 54 2c 20  e SQLITE3_TEXT, 
2e460 6e 6f 74 0a 2a 2a 20 53 51 4c 49 54 45 5f 54 45  not.** SQLITE_TE
2e470 58 54 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  XT..*/.#define S
2e480 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 20 31  QLITE_INTEGER  1
2e490 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
2e4a0 46 4c 4f 41 54 20 20 20 20 32 0a 23 64 65 66 69  FLOAT    2.#defi
2e4b0 6e 65 20 53 51 4c 49 54 45 5f 42 4c 4f 42 20 20  ne SQLITE_BLOB  
2e4c0 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53 51 4c     4.#define SQL
2e4d0 49 54 45 5f 4e 55 4c 4c 20 20 20 20 20 35 0a 23  ITE_NULL     5.#
2e4e0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 58  ifdef SQLITE_TEX
2e4f0 54 0a 23 20 75 6e 64 65 66 20 53 51 4c 49 54 45  T.# undef SQLITE
2e500 5f 54 45 58 54 0a 23 65 6c 73 65 0a 23 20 64 65  _TEXT.#else.# de
2e510 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 58 54  fine SQLITE_TEXT
2e520 20 20 20 20 20 33 0a 23 65 6e 64 69 66 0a 23 64       3.#endif.#d
2e530 65 66 69 6e 65 20 53 51 4c 49 54 45 33 5f 54 45  efine SQLITE3_TE
2e540 58 54 20 20 20 20 20 33 0a 0a 2f 2a 0a 2a 2a 20  XT     3../*.** 
2e550 43 41 50 49 33 52 45 46 3a 20 52 65 73 75 6c 74  CAPI3REF: Result
2e560 20 56 61 6c 75 65 73 20 46 72 6f 6d 20 41 20 51   Values From A Q
2e570 75 65 72 79 20 7b 48 31 33 38 30 30 7d 20 3c 53  uery {H13800} <S
2e580 31 30 37 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52  10700>.** KEYWOR
2e590 44 53 3a 20 7b 63 6f 6c 75 6d 6e 20 61 63 63 65  DS: {column acce
2e5a0 73 73 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a  ss functions}.**
2e5b0 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
2e5c0 65 73 20 66 6f 72 6d 20 74 68 65 20 22 72 65 73  es form the "res
2e5d0 75 6c 74 20 73 65 74 20 71 75 65 72 79 22 20 69  ult set query" i
2e5e0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
2e5f0 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 72  These routines r
2e600 65 74 75 72 6e 20 69 6e 66 6f 72 6d 61 74 69 6f  eturn informatio
2e610 6e 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65  n about a single
2e620 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 63   column of the c
2e630 75 72 72 65 6e 74 0a 2a 2a 20 72 65 73 75 6c 74  urrent.** result
2e640 20 72 6f 77 20 6f 66 20 61 20 71 75 65 72 79 2e   row of a query.
2e650 20 20 49 6e 20 65 76 65 72 79 20 63 61 73 65 20    In every case 
2e660 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
2e670 6e 74 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a  nt is a pointer.
2e680 2a 2a 20 74 6f 20 74 68 65 20 5b 70 72 65 70 61  ** to the [prepa
2e690 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 74  red statement] t
2e6a0 68 61 74 20 69 73 20 62 65 69 6e 67 20 65 76 61  hat is being eva
2e6b0 6c 75 61 74 65 64 20 28 74 68 65 20 5b 73 71 6c  luated (the [sql
2e6c0 69 74 65 33 5f 73 74 6d 74 2a 5d 0a 2a 2a 20 74  ite3_stmt*].** t
2e6d0 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65 64  hat was returned
2e6e0 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 70   from [sqlite3_p
2e6f0 72 65 70 61 72 65 5f 76 32 28 29 5d 20 6f 72 20  repare_v2()] or 
2e700 6f 6e 65 20 6f 66 20 69 74 73 20 76 61 72 69 61  one of its varia
2e710 6e 74 73 29 0a 2a 2a 20 61 6e 64 20 74 68 65 20  nts).** and the 
2e720 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
2e730 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  is the index of 
2e740 74 68 65 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77  the column for w
2e750 68 69 63 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  hich information
2e760 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 72 65  .** should be re
2e770 74 75 72 6e 65 64 2e 20 20 54 68 65 20 6c 65 66  turned.  The lef
2e780 74 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20  tmost column of 
2e790 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 68  the result set h
2e7a0 61 73 20 74 68 65 20 69 6e 64 65 78 20 30 2e 0a  as the index 0..
2e7b0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c  **.** If the SQL
2e7c0 20 73 74 61 74 65 6d 65 6e 74 20 64 6f 65 73 20   statement does 
2e7d0 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 70 6f  not currently po
2e7e0 69 6e 74 20 74 6f 20 61 20 76 61 6c 69 64 20 72  int to a valid r
2e7f0 6f 77 2c 20 6f 72 20 69 66 20 74 68 65 0a 2a 2a  ow, or if the.**
2e800 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 73   column index is
2e810 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2c 20 74   out of range, t
2e820 68 65 20 72 65 73 75 6c 74 20 69 73 20 75 6e 64  he result is und
2e830 65 66 69 6e 65 64 2e 0a 2a 2a 20 54 68 65 73 65  efined..** These
2e840 20 72 6f 75 74 69 6e 65 73 20 6d 61 79 20 6f 6e   routines may on
2e850 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65  ly be called whe
2e860 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e  n the most recen
2e870 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 5b 73 71  t call to.** [sq
2e880 6c 69 74 65 33 5f 73 74 65 70 28 29 5d 20 68 61  lite3_step()] ha
2e890 73 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  s returned [SQLI
2e8a0 54 45 5f 52 4f 57 5d 20 61 6e 64 20 6e 65 69 74  TE_ROW] and neit
2e8b0 68 65 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  her.** [sqlite3_
2e8c0 72 65 73 65 74 28 29 5d 20 6e 6f 72 20 5b 73 71  reset()] nor [sq
2e8d0 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29  lite3_finalize()
2e8e0 5d 20 68 61 76 65 20 62 65 65 6e 20 63 61 6c 6c  ] have been call
2e8f0 65 64 20 73 75 62 73 65 71 75 65 6e 74 6c 79 2e  ed subsequently.
2e900 0a 2a 2a 20 49 66 20 61 6e 79 20 6f 66 20 74 68  .** If any of th
2e910 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
2e920 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20 5b 73   called after [s
2e930 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20  qlite3_reset()] 
2e940 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66  or.** [sqlite3_f
2e950 69 6e 61 6c 69 7a 65 28 29 5d 20 6f 72 20 61 66  inalize()] or af
2e960 74 65 72 20 5b 73 71 6c 69 74 65 33 5f 73 74 65  ter [sqlite3_ste
2e970 70 28 29 5d 20 68 61 73 20 72 65 74 75 72 6e 65  p()] has returne
2e980 64 0a 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f  d.** something o
2e990 74 68 65 72 20 74 68 61 6e 20 5b 53 51 4c 49 54  ther than [SQLIT
2e9a0 45 5f 52 4f 57 5d 2c 20 74 68 65 20 72 65 73 75  E_ROW], the resu
2e9b0 6c 74 73 20 61 72 65 20 75 6e 64 65 66 69 6e 65  lts are undefine
2e9c0 64 2e 0a 2a 2a 20 49 66 20 5b 73 71 6c 69 74 65  d..** If [sqlite
2e9d0 33 5f 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71  3_step()] or [sq
2e9e0 6c 69 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f  lite3_reset()] o
2e9f0 72 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c  r [sqlite3_final
2ea00 69 7a 65 28 29 5d 0a 2a 2a 20 61 72 65 20 63 61  ize()].** are ca
2ea10 6c 6c 65 64 20 66 72 6f 6d 20 61 20 64 69 66 66  lled from a diff
2ea20 65 72 65 6e 74 20 74 68 72 65 61 64 20 77 68 69  erent thread whi
2ea30 6c 65 20 61 6e 79 20 6f 66 20 74 68 65 73 65 20  le any of these 
2ea40 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20  routines.** are 
2ea50 70 65 6e 64 69 6e 67 2c 20 74 68 65 6e 20 74 68  pending, then th
2ea60 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 75 6e  e results are un
2ea70 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  defined..**.** T
2ea80 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  he sqlite3_colum
2ea90 6e 5f 74 79 70 65 28 29 20 72 6f 75 74 69 6e 65  n_type() routine
2eaa0 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
2eab0 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20  [SQLITE_INTEGER 
2eac0 7c 20 64 61 74 61 74 79 70 65 20 63 6f 64 65 5d  | datatype code]
2ead0 20 66 6f 72 20 74 68 65 20 69 6e 69 74 69 61 6c   for the initial
2eae0 20 64 61 74 61 20 74 79 70 65 0a 2a 2a 20 6f 66   data type.** of
2eaf0 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
2eb00 6d 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e 65  mn.  The returne
2eb10 64 20 76 61 6c 75 65 20 69 73 20 6f 6e 65 20 6f  d value is one o
2eb20 66 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47 45  f [SQLITE_INTEGE
2eb30 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 46  R],.** [SQLITE_F
2eb40 4c 4f 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54  LOAT], [SQLITE_T
2eb50 45 58 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 42 4c  EXT], [SQLITE_BL
2eb60 4f 42 5d 2c 20 6f 72 20 5b 53 51 4c 49 54 45 5f  OB], or [SQLITE_
2eb70 4e 55 4c 4c 5d 2e 20 20 54 68 65 20 76 61 6c 75  NULL].  The valu
2eb80 65 0a 2a 2a 20 72 65 74 75 72 6e 65 64 20 62 79  e.** returned by
2eb90 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2eba0 74 79 70 65 28 29 20 69 73 20 6f 6e 6c 79 20 6d  type() is only m
2ebb0 65 61 6e 69 6e 67 66 75 6c 20 69 66 20 6e 6f 20  eaningful if no 
2ebc0 74 79 70 65 0a 2a 2a 20 63 6f 6e 76 65 72 73 69  type.** conversi
2ebd0 6f 6e 73 20 68 61 76 65 20 6f 63 63 75 72 72 65  ons have occurre
2ebe0 64 20 61 73 20 64 65 73 63 72 69 62 65 64 20 62  d as described b
2ebf0 65 6c 6f 77 2e 20 20 41 66 74 65 72 20 61 20 74  elow.  After a t
2ec00 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2c 0a  ype conversion,.
2ec10 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 72 65 74  ** the value ret
2ec20 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
2ec30 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 29 20 69  _column_type() i
2ec40 73 20 75 6e 64 65 66 69 6e 65 64 2e 20 20 46 75  s undefined.  Fu
2ec50 74 75 72 65 0a 2a 2a 20 76 65 72 73 69 6f 6e 73  ture.** versions
2ec60 20 6f 66 20 53 51 4c 69 74 65 20 6d 61 79 20 63   of SQLite may c
2ec70 68 61 6e 67 65 20 74 68 65 20 62 65 68 61 76 69  hange the behavi
2ec80 6f 72 20 6f 66 20 73 71 6c 69 74 65 33 5f 63 6f  or of sqlite3_co
2ec90 6c 75 6d 6e 5f 74 79 70 65 28 29 0a 2a 2a 20 66  lumn_type().** f
2eca0 6f 6c 6c 6f 77 69 6e 67 20 61 20 74 79 70 65 20  ollowing a type 
2ecb0 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a  conversion..**.*
2ecc0 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
2ecd0 69 73 20 61 20 42 4c 4f 42 20 6f 72 20 55 54 46  is a BLOB or UTF
2ece0 2d 38 20 73 74 72 69 6e 67 20 74 68 65 6e 20 74  -8 string then t
2ecf0 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  he sqlite3_colum
2ed00 6e 5f 62 79 74 65 73 28 29 0a 2a 2a 20 72 6f 75  n_bytes().** rou
2ed10 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74 68 65  tine returns the
2ed20 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
2ed30 20 69 6e 20 74 68 61 74 20 42 4c 4f 42 20 6f 72   in that BLOB or
2ed40 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 49 66 20 74   string..** If t
2ed50 68 65 20 72 65 73 75 6c 74 20 69 73 20 61 20 55  he result is a U
2ed60 54 46 2d 31 36 20 73 74 72 69 6e 67 2c 20 74 68  TF-16 string, th
2ed70 65 6e 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  en sqlite3_colum
2ed80 6e 5f 62 79 74 65 73 28 29 20 63 6f 6e 76 65 72  n_bytes() conver
2ed90 74 73 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  ts.** the string
2eda0 20 74 6f 20 55 54 46 2d 38 20 61 6e 64 20 74 68   to UTF-8 and th
2edb0 65 6e 20 72 65 74 75 72 6e 73 20 74 68 65 20 6e  en returns the n
2edc0 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 2e 0a  umber of bytes..
2edd0 2a 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74  ** If the result
2ede0 20 69 73 20 61 20 6e 75 6d 65 72 69 63 20 76 61   is a numeric va
2edf0 6c 75 65 20 74 68 65 6e 20 73 71 6c 69 74 65 33  lue then sqlite3
2ee00 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 20  _column_bytes() 
2ee10 75 73 65 73 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  uses.** [sqlite3
2ee20 5f 73 6e 70 72 69 6e 74 66 28 29 5d 20 74 6f 20  _snprintf()] to 
2ee30 63 6f 6e 76 65 72 74 20 74 68 61 74 20 76 61 6c  convert that val
2ee40 75 65 20 74 6f 20 61 20 55 54 46 2d 38 20 73 74  ue to a UTF-8 st
2ee50 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  ring and returns
2ee60 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
2ee70 66 20 62 79 74 65 73 20 69 6e 20 74 68 61 74 20  f bytes in that 
2ee80 73 74 72 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 76  string..** The v
2ee90 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 64 6f  alue returned do
2eea0 65 73 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74  es not include t
2eeb0 68 65 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74  he zero terminat
2eec0 6f 72 20 61 74 20 74 68 65 20 65 6e 64 0a 2a 2a  or at the end.**
2eed0 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 2e 20   of the string. 
2eee0 20 46 6f 72 20 63 6c 61 72 69 74 79 3a 20 74 68   For clarity: th
2eef0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
2ef00 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
2ef10 66 0a 2a 2a 20 62 79 74 65 73 20 69 6e 20 74 68  f.** bytes in th
2ef20 65 20 73 74 72 69 6e 67 2c 20 6e 6f 74 20 74 68  e string, not th
2ef30 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  e number of char
2ef40 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 53 74  acters..**.** St
2ef50 72 69 6e 67 73 20 72 65 74 75 72 6e 65 64 20 62  rings returned b
2ef60 79 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  y sqlite3_column
2ef70 5f 74 65 78 74 28 29 20 61 6e 64 20 73 71 6c 69  _text() and sqli
2ef80 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
2ef90 36 28 29 2c 0a 2a 2a 20 65 76 65 6e 20 65 6d 70  6(),.** even emp
2efa0 74 79 20 73 74 72 69 6e 67 73 2c 20 61 72 65 20  ty strings, are 
2efb0 61 6c 77 61 79 73 20 7a 65 72 6f 20 74 65 72 6d  always zero term
2efc0 69 6e 61 74 65 64 2e 20 20 54 68 65 20 72 65 74  inated.  The ret
2efd0 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 66 72 6f  urn.** value fro
2efe0 6d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  m sqlite3_column
2eff0 5f 62 6c 6f 62 28 29 20 66 6f 72 20 61 20 7a 65  _blob() for a ze
2f000 72 6f 2d 6c 65 6e 67 74 68 20 42 4c 4f 42 20 69  ro-length BLOB i
2f010 73 20 61 6e 20 61 72 62 69 74 72 61 72 79 0a 2a  s an arbitrary.*
2f020 2a 20 70 6f 69 6e 74 65 72 2c 20 70 6f 73 73 69  * pointer, possi
2f030 62 6c 79 20 65 76 65 6e 20 61 20 4e 55 4c 4c 20  bly even a NULL 
2f040 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  pointer..**.** T
2f050 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  he sqlite3_colum
2f060 6e 5f 62 79 74 65 73 31 36 28 29 20 72 6f 75 74  n_bytes16() rout
2f070 69 6e 65 20 69 73 20 73 69 6d 69 6c 61 72 20 74  ine is similar t
2f080 6f 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  o sqlite3_column
2f090 5f 62 79 74 65 73 28 29 0a 2a 2a 20 62 75 74 20  _bytes().** but 
2f0a0 6c 65 61 76 65 73 20 74 68 65 20 72 65 73 75 6c  leaves the resul
2f0b0 74 20 69 6e 20 55 54 46 2d 31 36 20 69 6e 20 6e  t in UTF-16 in n
2f0c0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
2f0d0 20 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d   instead of UTF-
2f0e0 38 2e 0a 2a 2a 20 54 68 65 20 7a 65 72 6f 20 74  8..** The zero t
2f0f0 65 72 6d 69 6e 61 74 6f 72 20 69 73 20 6e 6f 74  erminator is not
2f100 20 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 69   included in thi
2f110 73 20 63 6f 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54  s count..**.** T
2f120 68 65 20 6f 62 6a 65 63 74 20 72 65 74 75 72 6e  he object return
2f130 65 64 20 62 79 20 5b 73 71 6c 69 74 65 33 5f 63  ed by [sqlite3_c
2f140 6f 6c 75 6d 6e 5f 76 61 6c 75 65 28 29 5d 20 69  olumn_value()] i
2f150 73 20 61 6e 0a 2a 2a 20 5b 75 6e 70 72 6f 74 65  s an.** [unprote
2f160 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
2f170 75 65 5d 20 6f 62 6a 65 63 74 2e 20 20 41 6e 20  ue] object.  An 
2f180 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
2f190 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74  te3_value object
2f1a0 0a 2a 2a 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20  .** may only be 
2f1b0 75 73 65 64 20 77 69 74 68 20 5b 73 71 6c 69 74  used with [sqlit
2f1c0 65 33 5f 62 69 6e 64 5f 76 61 6c 75 65 28 29 5d  e3_bind_value()]
2f1d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 72 65   and [sqlite3_re
2f1e0 73 75 6c 74 5f 76 61 6c 75 65 28 29 5d 2e 0a 2a  sult_value()]..*
2f1f0 2a 20 49 66 20 74 68 65 20 5b 75 6e 70 72 6f 74  * If the [unprot
2f200 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
2f210 6c 75 65 5d 20 6f 62 6a 65 63 74 20 72 65 74 75  lue] object retu
2f220 72 6e 65 64 20 62 79 0a 2a 2a 20 5b 73 71 6c 69  rned by.** [sqli
2f230 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c 75 65  te3_column_value
2f240 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e 20 61  ()] is used in a
2f250 6e 79 20 6f 74 68 65 72 20 77 61 79 2c 20 69 6e  ny other way, in
2f260 63 6c 75 64 69 6e 67 20 63 61 6c 6c 73 0a 2a 2a  cluding calls.**
2f270 20 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b   to routines lik
2f280 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  e [sqlite3_value
2f290 5f 69 6e 74 28 29 5d 2c 20 5b 73 71 6c 69 74 65  _int()], [sqlite
2f2a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 29 5d 2c  3_value_text()],
2f2b0 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  .** or [sqlite3_
2f2c0 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c 20  value_bytes()], 
2f2d0 74 68 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f  then the behavio
2f2e0 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a  r is undefined..
2f2f0 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2f300 69 6e 65 73 20 61 74 74 65 6d 70 74 20 74 6f 20  ines attempt to 
2f310 63 6f 6e 76 65 72 74 20 74 68 65 20 76 61 6c 75  convert the valu
2f320 65 20 77 68 65 72 65 20 61 70 70 72 6f 70 72 69  e where appropri
2f330 61 74 65 2e 20 20 46 6f 72 0a 2a 2a 20 65 78 61  ate.  For.** exa
2f340 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 69 6e 74  mple, if the int
2f350 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
2f360 74 69 6f 6e 20 69 73 20 46 4c 4f 41 54 20 61 6e  tion is FLOAT an
2f370 64 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 0a  d a text result.
2f380 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ** is requested,
2f390 20 5b 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e   [sqlite3_snprin
2f3a0 74 66 28 29 5d 20 69 73 20 75 73 65 64 20 69 6e  tf()] is used in
2f3b0 74 65 72 6e 61 6c 6c 79 20 74 6f 20 70 65 72 66  ternally to perf
2f3c0 6f 72 6d 20 74 68 65 0a 2a 2a 20 63 6f 6e 76 65  orm the.** conve
2f3d0 72 73 69 6f 6e 20 61 75 74 6f 6d 61 74 69 63 61  rsion automatica
2f3e0 6c 6c 79 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  lly.  The follow
2f3f0 69 6e 67 20 74 61 62 6c 65 20 64 65 74 61 69 6c  ing table detail
2f400 73 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e  s the conversion
2f410 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 61 70  s.** that are ap
2f420 70 6c 69 65 64 3a 0a 2a 2a 0a 2a 2a 20 3c 62 6c  plied:.**.** <bl
2f430 6f 63 6b 71 75 6f 74 65 3e 0a 2a 2a 20 3c 74 61  ockquote>.** <ta
2f440 62 6c 65 20 62 6f 72 64 65 72 3d 22 31 22 3e 0a  ble border="1">.
2f450 2a 2a 20 3c 74 72 3e 3c 74 68 3e 20 49 6e 74 65  ** <tr><th> Inte
2f460 72 6e 61 6c 3c 62 72 3e 54 79 70 65 20 3c 74 68  rnal<br>Type <th
2f470 3e 20 52 65 71 75 65 73 74 65 64 3c 62 72 3e 54  > Requested<br>T
2f480 79 70 65 20 3c 74 68 3e 20 20 43 6f 6e 76 65 72  ype <th>  Conver
2f490 73 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c  sion.**.** <tr><
2f4a0 74 64 3e 20 20 4e 55 4c 4c 20 20 20 20 3c 74 64  td>  NULL    <td
2f4b0 3e 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e  > INTEGER   <td>
2f4c0 20 52 65 73 75 6c 74 20 69 73 20 30 0a 2a 2a 20   Result is 0.** 
2f4d0 3c 74 72 3e 3c 74 64 3e 20 20 4e 55 4c 4c 20 20  <tr><td>  NULL  
2f4e0 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20    <td>  FLOAT   
2f4f0 20 3c 74 64 3e 20 52 65 73 75 6c 74 20 69 73 20   <td> Result is 
2f500 30 2e 30 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  0.0.** <tr><td> 
2f510 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20   NULL    <td>   
2f520 54 45 58 54 20 20 20 20 3c 74 64 3e 20 52 65 73  TEXT    <td> Res
2f530 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e  ult is NULL poin
2f540 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  ter.** <tr><td> 
2f550 20 4e 55 4c 4c 20 20 20 20 3c 74 64 3e 20 20 20   NULL    <td>   
2f560 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 52 65 73  BLOB    <td> Res
2f570 75 6c 74 20 69 73 20 4e 55 4c 4c 20 70 6f 69 6e  ult is NULL poin
2f580 74 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  ter.** <tr><td> 
2f590 49 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 46  INTEGER  <td>  F
2f5a0 4c 4f 41 54 20 20 20 20 3c 74 64 3e 20 43 6f 6e  LOAT    <td> Con
2f5b0 76 65 72 74 20 66 72 6f 6d 20 69 6e 74 65 67 65  vert from intege
2f5c0 72 20 74 6f 20 66 6c 6f 61 74 0a 2a 2a 20 3c 74  r to float.** <t
2f5d0 72 3e 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20  r><td> INTEGER  
2f5e0 3c 74 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c  <td>   TEXT    <
2f5f0 74 64 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72  td> ASCII render
2f600 69 6e 67 20 6f 66 20 74 68 65 20 69 6e 74 65 67  ing of the integ
2f610 65 72 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 49  er.** <tr><td> I
2f620 4e 54 45 47 45 52 20 20 3c 74 64 3e 20 20 20 42  NTEGER  <td>   B
2f630 4c 4f 42 20 20 20 20 3c 74 64 3e 20 53 61 6d 65  LOB    <td> Same
2f640 20 61 73 20 49 4e 54 45 47 45 52 2d 3e 54 45 58   as INTEGER->TEX
2f650 54 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46  T.** <tr><td>  F
2f660 4c 4f 41 54 20 20 20 3c 74 64 3e 20 49 4e 54 45  LOAT   <td> INTE
2f670 47 45 52 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65  GER   <td> Conve
2f680 72 74 20 66 72 6f 6d 20 66 6c 6f 61 74 20 74 6f  rt from float to
2f690 20 69 6e 74 65 67 65 72 0a 2a 2a 20 3c 74 72 3e   integer.** <tr>
2f6a0 3c 74 64 3e 20 20 46 4c 4f 41 54 20 20 20 3c 74  <td>  FLOAT   <t
2f6b0 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64  d>   TEXT    <td
2f6c0 3e 20 41 53 43 49 49 20 72 65 6e 64 65 72 69 6e  > ASCII renderin
2f6d0 67 20 6f 66 20 74 68 65 20 66 6c 6f 61 74 0a 2a  g of the float.*
2f6e0 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 46 4c 4f 41  * <tr><td>  FLOA
2f6f0 54 20 20 20 3c 74 64 3e 20 20 20 42 4c 4f 42 20  T   <td>   BLOB 
2f700 20 20 20 3c 74 64 3e 20 53 61 6d 65 20 61 73 20     <td> Same as 
2f710 46 4c 4f 41 54 2d 3e 54 45 58 54 0a 2a 2a 20 3c  FLOAT->TEXT.** <
2f720 74 72 3e 3c 74 64 3e 20 20 54 45 58 54 20 20 20  tr><td>  TEXT   
2f730 20 3c 74 64 3e 20 49 4e 54 45 47 45 52 20 20 20   <td> INTEGER   
2f740 3c 74 64 3e 20 55 73 65 20 61 74 6f 69 28 29 0a  <td> Use atoi().
2f750 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 54 45 58  ** <tr><td>  TEX
2f760 54 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  T    <td>  FLOAT
2f770 20 20 20 20 3c 74 64 3e 20 55 73 65 20 61 74 6f      <td> Use ato
2f780 66 28 29 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20  f().** <tr><td> 
2f790 20 54 45 58 54 20 20 20 20 3c 74 64 3e 20 20 20   TEXT    <td>   
2f7a0 42 4c 4f 42 20 20 20 20 3c 74 64 3e 20 4e 6f 20  BLOB    <td> No 
2f7b0 63 68 61 6e 67 65 0a 2a 2a 20 3c 74 72 3e 3c 74  change.** <tr><t
2f7c0 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74 64 3e  d>  BLOB    <td>
2f7d0 20 49 4e 54 45 47 45 52 20 20 20 3c 74 64 3e 20   INTEGER   <td> 
2f7e0 43 6f 6e 76 65 72 74 20 74 6f 20 54 45 58 54 20  Convert to TEXT 
2f7f0 74 68 65 6e 20 75 73 65 20 61 74 6f 69 28 29 0a  then use atoi().
2f800 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 20 42 4c 4f  ** <tr><td>  BLO
2f810 42 20 20 20 20 3c 74 64 3e 20 20 46 4c 4f 41 54  B    <td>  FLOAT
2f820 20 20 20 20 3c 74 64 3e 20 43 6f 6e 76 65 72 74      <td> Convert
2f830 20 74 6f 20 54 45 58 54 20 74 68 65 6e 20 75 73   to TEXT then us
2f840 65 20 61 74 6f 66 28 29 0a 2a 2a 20 3c 74 72 3e  e atof().** <tr>
2f850 3c 74 64 3e 20 20 42 4c 4f 42 20 20 20 20 3c 74  <td>  BLOB    <t
2f860 64 3e 20 20 20 54 45 58 54 20 20 20 20 3c 74 64  d>   TEXT    <td
2f870 3e 20 41 64 64 20 61 20 7a 65 72 6f 20 74 65 72  > Add a zero ter
2f880 6d 69 6e 61 74 6f 72 20 69 66 20 6e 65 65 64 65  minator if neede
2f890 64 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a  d.** </table>.**
2f8a0 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74 65 3e 0a 2a   </blockquote>.*
2f8b0 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20 61  *.** The table a
2f8c0 62 6f 76 65 20 6d 61 6b 65 73 20 72 65 66 65 72  bove makes refer
2f8d0 65 6e 63 65 20 74 6f 20 73 74 61 6e 64 61 72 64  ence to standard
2f8e0 20 43 20 6c 69 62 72 61 72 79 20 66 75 6e 63 74   C library funct
2f8f0 69 6f 6e 73 20 61 74 6f 69 28 29 0a 2a 2a 20 61  ions atoi().** a
2f900 6e 64 20 61 74 6f 66 28 29 2e 20 20 53 51 4c 69  nd atof().  SQLi
2f910 74 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c  te does not real
2f920 6c 79 20 75 73 65 20 74 68 65 73 65 20 66 75 6e  ly use these fun
2f930 63 74 69 6f 6e 73 2e 20 20 49 74 20 68 61 73 20  ctions.  It has 
2f940 69 74 73 0a 2a 2a 20 6f 77 6e 20 65 71 75 69 76  its.** own equiv
2f950 61 6c 65 6e 74 20 69 6e 74 65 72 6e 61 6c 20 72  alent internal r
2f960 6f 75 74 69 6e 65 73 2e 20 20 54 68 65 20 61 74  outines.  The at
2f970 6f 69 28 29 20 61 6e 64 20 61 74 6f 66 28 29 20  oi() and atof() 
2f980 6e 61 6d 65 73 20 61 72 65 0a 2a 2a 20 75 73 65  names are.** use
2f990 64 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20 66  d in the table f
2f9a0 6f 72 20 62 72 65 76 69 74 79 20 61 6e 64 20 62  or brevity and b
2f9b0 65 63 61 75 73 65 20 74 68 65 79 20 61 72 65 20  ecause they are 
2f9c0 66 61 6d 69 6c 69 61 72 20 74 6f 20 6d 6f 73 74  familiar to most
2f9d0 0a 2a 2a 20 43 20 70 72 6f 67 72 61 6d 6d 65 72  .** C programmer
2f9e0 73 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  s..**.** Note th
2f9f0 61 74 20 77 68 65 6e 20 74 79 70 65 20 63 6f 6e  at when type con
2fa00 76 65 72 73 69 6f 6e 73 20 6f 63 63 75 72 2c 20  versions occur, 
2fa10 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65  pointers returne
2fa20 64 20 62 79 20 70 72 69 6f 72 0a 2a 2a 20 63 61  d by prior.** ca
2fa30 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  lls to sqlite3_c
2fa40 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 2c 20 73 71  olumn_blob(), sq
2fa50 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2fa60 74 28 29 2c 20 61 6e 64 2f 6f 72 0a 2a 2a 20 73  t(), and/or.** s
2fa70 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
2fa80 78 74 31 36 28 29 20 6d 61 79 20 62 65 20 69 6e  xt16() may be in
2fa90 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2a 20 54 79  validated..** Ty
2faa0 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61  pe conversions a
2fab0 6e 64 20 70 6f 69 6e 74 65 72 20 69 6e 76 61 6c  nd pointer inval
2fac0 69 64 61 74 69 6f 6e 73 20 6d 69 67 68 74 20 6f  idations might o
2fad0 63 63 75 72 0a 2a 2a 20 69 6e 20 74 68 65 20 66  ccur.** in the f
2fae0 6f 6c 6c 6f 77 69 6e 67 20 63 61 73 65 73 3a 0a  ollowing cases:.
2faf0 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  **.** <ul>.** <l
2fb00 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63  i> The initial c
2fb10 6f 6e 74 65 6e 74 20 69 73 20 61 20 42 4c 4f 42  ontent is a BLOB
2fb20 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 63 6f 6c   and sqlite3_col
2fb30 75 6d 6e 5f 74 65 78 74 28 29 20 6f 72 0a 2a 2a  umn_text() or.**
2fb40 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f        sqlite3_co
2fb50 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 69 73  lumn_text16() is
2fb60 20 63 61 6c 6c 65 64 2e 20 20 41 20 7a 65 72 6f   called.  A zero
2fb70 2d 74 65 72 6d 69 6e 61 74 6f 72 20 6d 69 67 68  -terminator migh
2fb80 74 0a 2a 2a 20 20 20 20 20 20 6e 65 65 64 20 74  t.**      need t
2fb90 6f 20 62 65 20 61 64 64 65 64 20 74 6f 20 74 68  o be added to th
2fba0 65 20 73 74 72 69 6e 67 2e 3c 2f 6c 69 3e 0a 2a  e string.</li>.*
2fbb0 2a 20 3c 6c 69 3e 20 54 68 65 20 69 6e 69 74 69  * <li> The initi
2fbc0 61 6c 20 63 6f 6e 74 65 6e 74 20 69 73 20 55 54  al content is UT
2fbd0 46 2d 38 20 74 65 78 74 20 61 6e 64 20 73 71 6c  F-8 text and sql
2fbe0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
2fbf0 73 31 36 28 29 20 6f 72 0a 2a 2a 20 20 20 20 20  s16() or.**     
2fc00 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
2fc10 74 65 78 74 31 36 28 29 20 69 73 20 63 61 6c 6c  text16() is call
2fc20 65 64 2e 20 20 54 68 65 20 63 6f 6e 74 65 6e 74  ed.  The content
2fc30 20 6d 75 73 74 20 62 65 20 63 6f 6e 76 65 72 74   must be convert
2fc40 65 64 0a 2a 2a 20 20 20 20 20 20 74 6f 20 55 54  ed.**      to UT
2fc50 46 2d 31 36 2e 3c 2f 6c 69 3e 0a 2a 2a 20 3c 6c  F-16.</li>.** <l
2fc60 69 3e 20 54 68 65 20 69 6e 69 74 69 61 6c 20 63  i> The initial c
2fc70 6f 6e 74 65 6e 74 20 69 73 20 55 54 46 2d 31 36  ontent is UTF-16
2fc80 20 74 65 78 74 20 61 6e 64 20 73 71 6c 69 74 65   text and sqlite
2fc90 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
2fca0 20 6f 72 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69   or.**      sqli
2fcb0 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28  te3_column_text(
2fcc0 29 20 69 73 20 63 61 6c 6c 65 64 2e 20 20 54 68  ) is called.  Th
2fcd0 65 20 63 6f 6e 74 65 6e 74 20 6d 75 73 74 20 62  e content must b
2fce0 65 20 63 6f 6e 76 65 72 74 65 64 0a 2a 2a 20 20  e converted.**  
2fcf0 20 20 20 20 74 6f 20 55 54 46 2d 38 2e 3c 2f 6c      to UTF-8.</l
2fd00 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a  i>.** </ul>.**.*
2fd10 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 73 20 62 65  * Conversions be
2fd20 74 77 65 65 6e 20 55 54 46 2d 31 36 62 65 20 61  tween UTF-16be a
2fd30 6e 64 20 55 54 46 2d 31 36 6c 65 20 61 72 65 20  nd UTF-16le are 
2fd40 61 6c 77 61 79 73 20 64 6f 6e 65 20 69 6e 20 70  always done in p
2fd50 6c 61 63 65 20 61 6e 64 20 64 6f 0a 2a 2a 20 6e  lace and do.** n
2fd60 6f 74 20 69 6e 76 61 6c 69 64 61 74 65 20 61 20  ot invalidate a 
2fd70 70 72 69 6f 72 20 70 6f 69 6e 74 65 72 2c 20 74  prior pointer, t
2fd80 68 6f 75 67 68 20 6f 66 20 63 6f 75 72 73 65 20  hough of course 
2fd90 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74  the content of t
2fda0 68 65 20 62 75 66 66 65 72 0a 2a 2a 20 74 68 61  he buffer.** tha
2fdb0 74 20 74 68 65 20 70 72 69 6f 72 20 70 6f 69 6e  t the prior poin
2fdc0 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 77 69  ter points to wi
2fdd0 6c 6c 20 68 61 76 65 20 62 65 65 6e 20 6d 6f 64  ll have been mod
2fde0 69 66 69 65 64 2e 20 20 4f 74 68 65 72 20 6b 69  ified.  Other ki
2fdf0 6e 64 73 0a 2a 2a 20 6f 66 20 63 6f 6e 76 65 72  nds.** of conver
2fe00 73 69 6f 6e 20 61 72 65 20 64 6f 6e 65 20 69 6e  sion are done in
2fe10 20 70 6c 61 63 65 20 77 68 65 6e 20 69 74 20 69   place when it i
2fe20 73 20 70 6f 73 73 69 62 6c 65 2c 20 62 75 74 20  s possible, but 
2fe30 73 6f 6d 65 74 69 6d 65 73 20 74 68 65 79 0a 2a  sometimes they.*
2fe40 2a 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62  * are not possib
2fe50 6c 65 20 61 6e 64 20 69 6e 20 74 68 6f 73 65 20  le and in those 
2fe60 63 61 73 65 73 20 70 72 69 6f 72 20 70 6f 69 6e  cases prior poin
2fe70 74 65 72 73 20 61 72 65 20 69 6e 76 61 6c 69 64  ters are invalid
2fe80 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ated..**.** The 
2fe90 73 61 66 65 73 74 20 61 6e 64 20 65 61 73 69 65  safest and easie
2fea0 73 74 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 70  st to remember p
2feb0 6f 6c 69 63 79 20 69 73 20 74 6f 20 69 6e 76 6f  olicy is to invo
2fec0 6b 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  ke these routine
2fed0 73 0a 2a 2a 20 69 6e 20 6f 6e 65 20 6f 66 20 74  s.** in one of t
2fee0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 61 79  he following way
2fef0 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a  s:.**.** <ul>.**
2ff00 20 20 3c 6c 69 3e 73 71 6c 69 74 65 33 5f 63 6f    <li>sqlite3_co
2ff10 6c 75 6d 6e 5f 74 65 78 74 28 29 20 66 6f 6c 6c  lumn_text() foll
2ff20 6f 77 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f  owed by sqlite3_
2ff30 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 3c 2f  column_bytes()</
2ff40 6c 69 3e 0a 2a 2a 20 20 3c 6c 69 3e 73 71 6c 69  li>.**  <li>sqli
2ff50 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
2ff60 29 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20 73 71  ) followed by sq
2ff70 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
2ff80 65 73 28 29 3c 2f 6c 69 3e 0a 2a 2a 20 20 3c 6c  es()</li>.**  <l
2ff90 69 3e 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  i>sqlite3_column
2ffa0 5f 74 65 78 74 31 36 28 29 20 66 6f 6c 6c 6f 77  _text16() follow
2ffb0 65 64 20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f  ed by sqlite3_co
2ffc0 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 3c 2f  lumn_bytes16()</
2ffd0 6c 69 3e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a  li>.** </ul>.**.
2ffe0 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  ** In other word
2fff0 73 2c 20 79 6f 75 20 73 68 6f 75 6c 64 20 63 61  s, you should ca
30000 6c 6c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ll sqlite3_colum
30010 6e 5f 74 65 78 74 28 29 2c 0a 2a 2a 20 73 71 6c  n_text(),.** sql
30020 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
30030 28 29 2c 20 6f 72 20 73 71 6c 69 74 65 33 5f 63  (), or sqlite3_c
30040 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 20 66  olumn_text16() f
30050 69 72 73 74 20 74 6f 20 66 6f 72 63 65 20 74 68  irst to force th
30060 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 6e 74 6f  e result.** into
30070 20 74 68 65 20 64 65 73 69 72 65 64 20 66 6f 72   the desired for
30080 6d 61 74 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65  mat, then invoke
30090 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
300a0 62 79 74 65 73 28 29 20 6f 72 0a 2a 2a 20 73 71  bytes() or.** sq
300b0 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
300c0 65 73 31 36 28 29 20 74 6f 20 66 69 6e 64 20 74  es16() to find t
300d0 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 72  he size of the r
300e0 65 73 75 6c 74 2e 20 20 44 6f 20 6e 6f 74 20 6d  esult.  Do not m
300f0 69 78 20 63 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73  ix calls.** to s
30100 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
30110 78 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 5f  xt() or sqlite3_
30120 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 29 20 77 69  column_blob() wi
30130 74 68 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20 73  th calls to.** s
30140 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
30150 74 65 73 31 36 28 29 2c 20 61 6e 64 20 64 6f 20  tes16(), and do 
30160 6e 6f 74 20 6d 69 78 20 63 61 6c 6c 73 20 74 6f  not mix calls to
30170 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
30180 74 65 78 74 31 36 28 29 0a 2a 2a 20 77 69 74 68  text16().** with
30190 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65   calls to sqlite
301a0 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29  3_column_bytes()
301b0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e  ..**.** The poin
301c0 74 65 72 73 20 72 65 74 75 72 6e 65 64 20 61 72  ters returned ar
301d0 65 20 76 61 6c 69 64 20 75 6e 74 69 6c 20 61 20  e valid until a 
301e0 74 79 70 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20  type conversion 
301f0 6f 63 63 75 72 73 20 61 73 0a 2a 2a 20 64 65 73  occurs as.** des
30200 63 72 69 62 65 64 20 61 62 6f 76 65 2c 20 6f 72  cribed above, or
30210 20 75 6e 74 69 6c 20 5b 73 71 6c 69 74 65 33 5f   until [sqlite3_
30220 73 74 65 70 28 29 5d 20 6f 72 20 5b 73 71 6c 69  step()] or [sqli
30230 74 65 33 5f 72 65 73 65 74 28 29 5d 20 6f 72 0a  te3_reset()] or.
30240 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 66 69 6e 61  ** [sqlite3_fina
30250 6c 69 7a 65 28 29 5d 20 69 73 20 63 61 6c 6c 65  lize()] is calle
30260 64 2e 20 20 54 68 65 20 6d 65 6d 6f 72 79 20 73  d.  The memory s
30270 70 61 63 65 20 75 73 65 64 20 74 6f 20 68 6f 6c  pace used to hol
30280 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 61 6e 64  d strings.** and
30290 20 42 4c 4f 42 73 20 69 73 20 66 72 65 65 64 20   BLOBs is freed 
302a0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20  automatically.  
302b0 44 6f 20 3c 62 3e 6e 6f 74 3c 2f 62 3e 20 70 61  Do <b>not</b> pa
302c0 73 73 20 74 68 65 20 70 6f 69 6e 74 65 72 73 20  ss the pointers 
302d0 72 65 74 75 72 6e 65 64 0a 2a 2a 20 5b 73 71 6c  returned.** [sql
302e0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
302f0 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 6f  ()], [sqlite3_co
30300 6c 75 6d 6e 5f 74 65 78 74 28 29 5d 2c 20 65 74  lumn_text()], et
30310 63 2e 20 69 6e 74 6f 0a 2a 2a 20 5b 73 71 6c 69  c. into.** [sqli
30320 74 65 33 5f 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a  te3_free()]..**.
30330 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20 61  ** If a memory a
30340 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20  llocation error 
30350 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68  occurs during th
30360 65 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20  e evaluation of 
30370 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20  any.** of these 
30380 72 6f 75 74 69 6e 65 73 2c 20 61 20 64 65 66 61  routines, a defa
30390 75 6c 74 20 76 61 6c 75 65 20 69 73 20 72 65 74  ult value is ret
303a0 75 72 6e 65 64 2e 20 20 54 68 65 20 64 65 66 61  urned.  The defa
303b0 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  ult value.** is 
303c0 65 69 74 68 65 72 20 74 68 65 20 69 6e 74 65 67  either the integ
303d0 65 72 20 30 2c 20 74 68 65 20 66 6c 6f 61 74 69  er 0, the floati
303e0 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72 20  ng point number 
303f0 30 2e 30 2c 20 6f 72 20 61 20 4e 55 4c 4c 0a 2a  0.0, or a NULL.*
30400 2a 20 70 6f 69 6e 74 65 72 2e 20 20 53 75 62 73  * pointer.  Subs
30410 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f 20  equent calls to 
30420 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64 65  [sqlite3_errcode
30430 28 29 5d 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a  ()] will return.
30440 2a 2a 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  ** [SQLITE_NOMEM
30450 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  ]..**.** INVARIA
30460 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38  NTS:.**.** {H138
30470 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  03} The [sqlite3
30480 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c 4e  _column_blob(S,N
30490 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e  )] interface con
304a0 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20  verts the.**    
304b0 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d 6e        Nth column
304c0 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20   in the current 
304d0 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75 6c  row of the resul
304e0 74 20 73 65 74 20 66 6f 72 0a 2a 2a 20 20 20 20  t set for.**    
304f0 20 20 20 20 20 20 74 68 65 20 5b 70 72 65 70 61        the [prepa
30500 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
30510 20 69 6e 74 6f 20 61 20 42 4c 4f 42 20 61 6e 64   into a BLOB and
30520 20 74 68 65 6e 20 72 65 74 75 72 6e 73 20 61 0a   then returns a.
30530 2a 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e  **          poin
30540 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6e 76 65  ter to the conve
30550 72 74 65 64 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  rted value..**.*
30560 2a 20 7b 48 31 33 38 30 36 7d 20 54 68 65 20 5b  * {H13806} The [
30570 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
30580 79 74 65 73 28 53 2c 4e 29 5d 20 69 6e 74 65 72  ytes(S,N)] inter
30590 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
305a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d  .**          num
305b0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
305c0 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74 72 69  the BLOB or stri
305d0 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20 6f 66  ng (exclusive of
305e0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
305f0 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72   zero terminator
30600 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67 29 20   on the string) 
30610 74 68 61 74 20 77 61 73 20 72 65 74 75 72 6e 65  that was returne
30620 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20 20 20  d by the.**     
30630 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65 6e 74       most recent
30640 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
30650 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 53 2c  3_column_blob(S,
30660 4e 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  N)] or.**       
30670 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75     [sqlite3_colu
30680 6d 6e 5f 74 65 78 74 28 53 2c 4e 29 5d 2e 0a 2a  mn_text(S,N)]..*
30690 2a 0a 2a 2a 20 7b 48 31 33 38 30 39 7d 20 54 68  *.** {H13809} Th
306a0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e [sqlite3_colum
306b0 6e 5f 62 79 74 65 73 31 36 28 53 2c 4e 29 5d 20  n_bytes16(S,N)] 
306c0 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
306d0 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  s the.**        
306e0 20 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65    number of byte
306f0 73 20 69 6e 20 74 68 65 20 73 74 72 69 6e 67 20  s in the string 
30700 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68  (exclusive of th
30710 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a 65  e.**          ze
30720 72 6f 20 74 65 72 6d 69 6e 61 74 6f 72 20 6f 6e  ro terminator on
30730 20 74 68 65 20 73 74 72 69 6e 67 29 20 74 68 61   the string) tha
30740 74 20 77 61 73 20 72 65 74 75 72 6e 65 64 20 62  t was returned b
30750 79 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  y the.**        
30760 20 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61    most recent ca
30770 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 63  ll to [sqlite3_c
30780 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 53 2c 4e  olumn_text16(S,N
30790 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33 38 31  )]..**.** {H1381
307a0 32 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  2} The [sqlite3_
307b0 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 28 53 2c  column_double(S,
307c0 4e 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f  N)] interface co
307d0 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20  nverts the.**   
307e0 20 20 20 20 20 20 20 4e 74 68 20 63 6f 6c 75 6d         Nth colum
307f0 6e 20 69 6e 20 74 68 65 20 63 75 72 72 65 6e 74  n in the current
30800 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65 73 75   row of the resu
30810 6c 74 20 73 65 74 20 66 6f 72 20 74 68 65 0a 2a  lt set for the.*
30820 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
30830 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
30840 53 20 69 6e 74 6f 20 61 20 66 6c 6f 61 74 69 6e  S into a floatin
30850 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20 61 6e  g point value an
30860 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  d.**          re
30870 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66 20  turns a copy of 
30880 74 68 61 74 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  that value..**.*
30890 2a 20 7b 48 31 33 38 31 35 7d 20 54 68 65 20 5b  * {H13815} The [
308a0 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
308b0 6e 74 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61  nt(S,N)] interfa
308c0 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a  ce converts the.
308d0 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74 68 20  **          Nth 
308e0 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75  column in the cu
308f0 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  rrent row of the
30900 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f 72 20   result set for 
30910 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
30920 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
30930 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20 36 34  ent] S into a 64
30940 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65  -bit signed inte
30950 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20  ger and.**      
30960 20 20 20 20 72 65 74 75 72 6e 73 20 74 68 65 20      returns the 
30970 6c 6f 77 65 72 20 33 32 20 62 69 74 73 20 6f 66  lower 32 bits of
30980 20 74 68 61 74 20 69 6e 74 65 67 65 72 2e 0a 2a   that integer..*
30990 2a 0a 2a 2a 20 7b 48 31 33 38 31 38 7d 20 54 68  *.** {H13818} Th
309a0 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  e [sqlite3_colum
309b0 6e 5f 69 6e 74 36 34 28 53 2c 4e 29 5d 20 69 6e  n_int64(S,N)] in
309c0 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72 74 73  terface converts
309d0 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
309e0 20 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74   Nth column in t
309f0 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f  he current row o
30a00 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
30a10 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20   for the.**     
30a20 20 20 20 20 20 5b 70 72 65 70 61 72 65 64 20 73       [prepared s
30a30 74 61 74 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f  tatement] S into
30a40 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64   a 64-bit signed
30a50 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20   integer and.** 
30a60 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 73           returns
30a70 20 61 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20   a copy of that 
30a80 69 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b  integer..**.** {
30a90 48 31 33 38 32 31 7d 20 54 68 65 20 5b 73 71 6c  H13821} The [sql
30aa0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
30ab0 28 53 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (S,N)] interface
30ac0 20 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a   converts the.**
30ad0 20 20 20 20 20 20 20 20 20 20 4e 74 68 20 63 6f            Nth co
30ae0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 63 75 72 72  lumn in the curr
30af0 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72  ent row of the r
30b00 65 73 75 6c 74 20 73 65 74 20 66 6f 72 0a 2a 2a  esult set for.**
30b10 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 70            the [p
30b20 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
30b30 74 5d 20 53 20 69 6e 74 6f 20 61 20 7a 65 72 6f  t] S into a zero
30b40 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
30b50 38 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73 74  8.**          st
30b60 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e 73  ring and returns
30b70 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
30b80 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a  at string..**.**
30b90 20 7b 48 31 33 38 32 34 7d 20 54 68 65 20 5b 73   {H13824} The [s
30ba0 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
30bb0 78 74 31 36 28 53 2c 4e 29 5d 20 69 6e 74 65 72  xt16(S,N)] inter
30bc0 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68  face converts th
30bd0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 4e 74  e.**          Nt
30be0 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20  h column in the 
30bf0 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
30c00 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66 6f  he result set fo
30c10 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20  r the.**        
30c20 20 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74    [prepared stat
30c30 65 6d 65 6e 74 5d 20 53 20 69 6e 74 6f 20 61 20  ement] S into a 
30c40 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20  zero-terminated 
30c50 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20 20  2-byte.**       
30c60 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d 31     aligned UTF-1
30c70 36 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72  6 native byte or
30c80 64 65 72 20 73 74 72 69 6e 67 20 61 6e 64 20 72  der string and r
30c90 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20 20 20  eturns.**       
30ca0 20 20 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20     a pointer to 
30cb0 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  that string..**.
30cc0 2a 2a 20 7b 48 31 33 38 32 37 7d 20 54 68 65 20  ** {H13827} The 
30cd0 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  [sqlite3_column_
30ce0 74 79 70 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72  type(S,N)] inter
30cf0 66 61 63 65 20 72 65 74 75 72 6e 73 0a 2a 2a 20  face returns.** 
30d00 20 20 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20           one of 
30d10 5b 53 51 4c 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b  [SQLITE_NULL], [
30d20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 5d 2c  SQLITE_INTEGER],
30d30 20 5b 53 51 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c   [SQLITE_FLOAT],
30d40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51  .**          [SQ
30d50 4c 49 54 45 5f 54 45 58 54 5d 2c 20 6f 72 20 5b  LITE_TEXT], or [
30d60 53 51 4c 49 54 45 5f 42 4c 4f 42 5d 20 61 73 20  SQLITE_BLOB] as 
30d70 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72 0a  appropriate for.
30d80 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
30d90 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Nth column in th
30da0 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
30db0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
30dc0 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  for.**          
30dd0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
30de0 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2a 0a 2a  atement] S..**.*
30df0 2a 20 7b 48 31 33 38 33 30 7d 20 54 68 65 20 5b  * {H13830} The [
30e00 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
30e10 61 6c 75 65 28 53 2c 4e 29 5d 20 69 6e 74 65 72  alue(S,N)] inter
30e20 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 0a 2a  face returns a.*
30e30 2a 20 20 20 20 20 20 20 20 20 20 70 6f 69 6e 74  *          point
30e40 65 72 20 74 6f 20 61 6e 20 5b 75 6e 70 72 6f 74  er to an [unprot
30e50 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61  ected sqlite3_va
30e60 6c 75 65 5d 20 6f 62 6a 65 63 74 20 66 6f 72 20  lue] object for 
30e70 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
30e80 4e 74 68 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68  Nth column in th
30e90 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66  e current row of
30ea0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
30eb0 66 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  for.**          
30ec0 74 68 65 20 5b 70 72 65 70 61 72 65 64 20 73 74  the [prepared st
30ed0 61 74 65 6d 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53  atement] S..*/.S
30ee0 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
30ef0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
30f00 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c 69 74 65  lumn_blob(sqlite
30f10 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
30f20 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
30f30 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
30f40 6e 5f 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f  n_bytes(sqlite3_
30f50 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
30f60 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  ;.SQLITE_API int
30f70 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
30f80 62 79 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f  bytes16(sqlite3_
30f90 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29  stmt*, int iCol)
30fa0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 64 6f 75  ;.SQLITE_API dou
30fb0 62 6c 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ble sqlite3_colu
30fc0 6d 6e 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65  mn_double(sqlite
30fd0 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f  3_stmt*, int iCo
30fe0 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  l);.SQLITE_API i
30ff0 6e 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  nt sqlite3_colum
31000 6e 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 73 74  n_int(sqlite3_st
31010 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a  mt*, int iCol);.
31020 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74  SQLITE_API sqlit
31030 65 33 5f 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e3_int64 sqlite3
31040 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 71  _column_int64(sq
31050 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74  lite3_stmt*, int
31060 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41   iCol);.SQLITE_A
31070 50 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  PI const unsigne
31080 64 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  d char *sqlite3_
31090 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 73 71 6c 69  column_text(sqli
310a0 74 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69  te3_stmt*, int i
310b0 43 6f 6c 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  Col);.SQLITE_API
310c0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
310d0 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
310e0 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  16(sqlite3_stmt*
310f0 2c 20 69 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c  , int iCol);.SQL
31100 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
31110 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28  te3_column_type(
31120 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 20 69  sqlite3_stmt*, i
31130 6e 74 20 69 43 6f 6c 29 3b 0a 53 51 4c 49 54 45  nt iCol);.SQLITE
31140 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c  _API sqlite3_val
31150 75 65 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ue *sqlite3_colu
31160 6d 6e 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33  mn_value(sqlite3
31170 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 69 43 6f 6c  _stmt*, int iCol
31180 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
31190 45 46 3a 20 44 65 73 74 72 6f 79 20 41 20 50 72  EF: Destroy A Pr
311a0 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74  epared Statement
311b0 20 4f 62 6a 65 63 74 20 7b 48 31 33 33 30 30 7d   Object {H13300}
311c0 20 3c 53 37 30 33 30 30 3e 3c 53 33 30 31 30 30   <S70300><S30100
311d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
311e0 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 20 66  te3_finalize() f
311f0 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
31200 64 20 74 6f 20 64 65 6c 65 74 65 20 61 20 5b 70  d to delete a [p
31210 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
31220 74 5d 2e 0a 2a 2a 20 49 66 20 74 68 65 20 73 74  t]..** If the st
31230 61 74 65 6d 65 6e 74 20 77 61 73 20 65 78 65 63  atement was exec
31240 75 74 65 64 20 73 75 63 63 65 73 73 66 75 6c 6c  uted successfull
31250 79 20 6f 72 20 6e 6f 74 20 65 78 65 63 75 74 65  y or not execute
31260 64 20 61 74 20 61 6c 6c 2c 20 74 68 65 6e 0a 2a  d at all, then.*
31270 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72  * SQLITE_OK is r
31280 65 74 75 72 6e 65 64 2e 20 49 66 20 65 78 65 63  eturned. If exec
31290 75 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 61  ution of the sta
312a0 74 65 6d 65 6e 74 20 66 61 69 6c 65 64 20 74 68  tement failed th
312b0 65 6e 20 61 6e 0a 2a 2a 20 5b 65 72 72 6f 72 20  en an.** [error 
312c0 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e 64  code] or [extend
312d0 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69  ed error code] i
312e0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a  s returned..**.*
312f0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63  * This routine c
31300 61 6e 20 62 65 20 63 61 6c 6c 65 64 20 61 74 20  an be called at 
31310 61 6e 79 20 70 6f 69 6e 74 20 64 75 72 69 6e 67  any point during
31320 20 74 68 65 20 65 78 65 63 75 74 69 6f 6e 20 6f   the execution o
31330 66 20 74 68 65 0a 2a 2a 20 5b 70 72 65 70 61 72  f the.** [prepar
31340 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 2e 20 20  ed statement].  
31350 49 66 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d  If the virtual m
31360 61 63 68 69 6e 65 20 68 61 73 20 6e 6f 74 0a 2a  achine has not.*
31370 2a 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63  * completed exec
31380 75 74 69 6f 6e 20 77 68 65 6e 20 74 68 69 73 20  ution when this 
31390 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
313a0 64 2c 20 74 68 61 74 20 69 73 20 6c 69 6b 65 0a  d, that is like.
313b0 2a 2a 20 65 6e 63 6f 75 6e 74 65 72 69 6e 67 20  ** encountering 
313c0 61 6e 20 65 72 72 6f 72 20 6f 72 20 61 6e 20 5b  an error or an [
313d0 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
313e0 74 20 7c 20 69 6e 74 65 72 72 75 70 74 5d 2e 0a  t | interrupt]..
313f0 2a 2a 20 49 6e 63 6f 6d 70 6c 65 74 65 20 75 70  ** Incomplete up
31400 64 61 74 65 73 20 6d 61 79 20 62 65 20 72 6f 6c  dates may be rol
31410 6c 65 64 20 62 61 63 6b 20 61 6e 64 20 74 72 61  led back and tra
31420 6e 73 61 63 74 69 6f 6e 73 20 63 61 6e 63 65 6c  nsactions cancel
31430 65 64 2c 0a 2a 2a 20 64 65 70 65 6e 64 69 6e 67  ed,.** depending
31440 20 6f 6e 20 74 68 65 20 63 69 72 63 75 6d 73 74   on the circumst
31450 61 6e 63 65 73 2c 20 61 6e 64 20 74 68 65 0a 2a  ances, and the.*
31460 2a 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 72  * [error code] r
31470 65 74 75 72 6e 65 64 20 77 69 6c 6c 20 62 65 20  eturned will be 
31480 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a  [SQLITE_ABORT]..
31490 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
314a0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 32 7d  :.**.** {H11302}
314b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 66 69   The [sqlite3_fi
314c0 6e 61 6c 69 7a 65 28 53 29 5d 20 69 6e 74 65 72  nalize(S)] inter
314d0 66 61 63 65 20 64 65 73 74 72 6f 79 73 20 74 68  face destroys th
314e0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
314f0 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
31500 74 5d 20 53 20 61 6e 64 20 72 65 6c 65 61 73 65  t] S and release
31510 73 20 61 6c 6c 0a 2a 2a 20 20 20 20 20 20 20 20  s all.**        
31520 20 20 6d 65 6d 6f 72 79 20 61 6e 64 20 66 69 6c    memory and fil
31530 65 20 72 65 73 6f 75 72 63 65 73 20 68 65 6c 64  e resources held
31540 20 62 79 20 74 68 61 74 20 6f 62 6a 65 63 74 2e   by that object.
31550 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 33 30 34 7d 20  .**.** {H11304} 
31560 49 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  If the most rece
31570 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nt call to [sqli
31580 74 65 33 5f 73 74 65 70 28 53 29 5d 20 66 6f 72  te3_step(S)] for
31590 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
315a0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
315b0 6d 65 6e 74 5d 20 53 20 72 65 74 75 72 6e 65 64  ment] S returned
315c0 20 61 6e 20 65 72 72 6f 72 2c 0a 2a 2a 20 20 20   an error,.**   
315d0 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71 6c         then [sql
315e0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 53 29  ite3_finalize(S)
315f0 5d 20 72 65 74 75 72 6e 73 20 74 68 61 74 20 73  ] returns that s
31600 61 6d 65 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51  ame error..*/.SQ
31610 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
31620 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 73 71  ite3_finalize(sq
31630 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
31640 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  t);../*.** CAPI3
31650 52 45 46 3a 20 52 65 73 65 74 20 41 20 50 72 65  REF: Reset A Pre
31660 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20  pared Statement 
31670 4f 62 6a 65 63 74 20 7b 48 31 33 33 33 30 7d 20  Object {H13330} 
31680 3c 53 37 30 33 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S70300>.**.** T
31690 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  he sqlite3_reset
316a0 28 29 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63  () function is c
316b0 61 6c 6c 65 64 20 74 6f 20 72 65 73 65 74 20 61  alled to reset a
316c0 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65   [prepared state
316d0 6d 65 6e 74 5d 0a 2a 2a 20 6f 62 6a 65 63 74 20  ment].** object 
316e0 62 61 63 6b 20 74 6f 20 69 74 73 20 69 6e 69 74  back to its init
316f0 69 61 6c 20 73 74 61 74 65 2c 20 72 65 61 64 79  ial state, ready
31700 20 74 6f 20 62 65 20 72 65 2d 65 78 65 63 75 74   to be re-execut
31710 65 64 2e 0a 2a 2a 20 41 6e 79 20 53 51 4c 20 73  ed..** Any SQL s
31720 74 61 74 65 6d 65 6e 74 20 76 61 72 69 61 62 6c  tatement variabl
31730 65 73 20 74 68 61 74 20 68 61 64 20 76 61 6c 75  es that had valu
31740 65 73 20 62 6f 75 6e 64 20 74 6f 20 74 68 65 6d  es bound to them
31750 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 5b 73   using.** the [s
31760 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
31770 20 7c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   | sqlite3_bind_
31780 2a 28 29 20 41 50 49 5d 20 72 65 74 61 69 6e 20  *() API] retain 
31790 74 68 65 69 72 20 76 61 6c 75 65 73 2e 0a 2a 2a  their values..**
317a0 20 55 73 65 20 5b 73 71 6c 69 74 65 33 5f 63 6c   Use [sqlite3_cl
317b0 65 61 72 5f 62 69 6e 64 69 6e 67 73 28 29 5d 20  ear_bindings()] 
317c0 74 6f 20 72 65 73 65 74 20 74 68 65 20 62 69 6e  to reset the bin
317d0 64 69 6e 67 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  dings..**.** {H1
317e0 31 33 33 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  1332} The [sqlit
317f0 65 33 5f 72 65 73 65 74 28 53 29 5d 20 69 6e 74  e3_reset(S)] int
31800 65 72 66 61 63 65 20 72 65 73 65 74 73 20 74 68  erface resets th
31810 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
31820 65 6d 65 6e 74 5d 20 53 0a 2a 2a 20 20 20 20 20  ement] S.**     
31830 20 20 20 20 20 62 61 63 6b 20 74 6f 20 74 68 65       back to the
31840 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 69 74   beginning of it
31850 73 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 0a 2a 2a  s program..**.**
31860 20 7b 48 31 31 33 33 34 7d 20 49 66 20 74 68 65   {H11334} If the
31870 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c   most recent cal
31880 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74  l to [sqlite3_st
31890 65 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a  ep(S)] for the.*
318a0 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70  *          [prep
318b0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20  ared statement] 
318c0 53 20 72 65 74 75 72 6e 65 64 20 5b 53 51 4c 49  S returned [SQLI
318d0 54 45 5f 52 4f 57 5d 20 6f 72 20 5b 53 51 4c 49  TE_ROW] or [SQLI
318e0 54 45 5f 44 4f 4e 45 5d 2c 0a 2a 2a 20 20 20 20  TE_DONE],.**    
318f0 20 20 20 20 20 20 6f 72 20 69 66 20 5b 73 71 6c        or if [sql
31900 69 74 65 33 5f 73 74 65 70 28 53 29 5d 20 68 61  ite3_step(S)] ha
31910 73 20 6e 65 76 65 72 20 62 65 66 6f 72 65 20 62  s never before b
31920 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 53 2c  een called on S,
31930 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
31940 6e 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74  n [sqlite3_reset
31950 28 53 29 5d 20 72 65 74 75 72 6e 73 20 5b 53 51  (S)] returns [SQ
31960 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a 2a 0a 2a 2a 20  LITE_OK]..**.** 
31970 7b 48 31 31 33 33 36 7d 20 49 66 20 74 68 65 20  {H11336} If the 
31980 6d 6f 73 74 20 72 65 63 65 6e 74 20 63 61 6c 6c  most recent call
31990 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 65   to [sqlite3_ste
319a0 70 28 53 29 5d 20 66 6f 72 20 74 68 65 0a 2a 2a  p(S)] for the.**
319b0 20 20 20 20 20 20 20 20 20 20 5b 70 72 65 70 61            [prepa
319c0 72 65 64 20 73 74 61 74 65 6d 65 6e 74 5d 20 53  red statement] S
319d0 20 69 6e 64 69 63 61 74 65 64 20 61 6e 20 65 72   indicated an er
319e0 72 6f 72 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20  ror, then.**    
319f0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72        [sqlite3_r
31a00 65 73 65 74 28 53 29 5d 20 72 65 74 75 72 6e 73  eset(S)] returns
31a10 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20   an appropriate 
31a20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a  [error code]..**
31a30 0a 2a 2a 20 7b 48 31 31 33 33 38 7d 20 54 68 65  .** {H11338} The
31a40 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
31a50 53 29 5d 20 69 6e 74 65 72 66 61 63 65 20 64 6f  S)] interface do
31a60 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  es not change th
31a70 65 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20 20 20  e values.**     
31a80 20 20 20 20 20 6f 66 20 61 6e 79 20 5b 73 71 6c       of any [sql
31a90 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 7c 62  ite3_bind_blob|b
31aa0 69 6e 64 69 6e 67 73 5d 20 6f 6e 20 74 68 65 20  indings] on the 
31ab0 5b 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d  [prepared statem
31ac0 65 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54  ent] S..*/.SQLIT
31ad0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
31ae0 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f  3_reset(sqlite3_
31af0 73 74 6d 74 20 2a 70 53 74 6d 74 29 3b 0a 0a 2f  stmt *pStmt);../
31b00 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 43  *.** CAPI3REF: C
31b10 72 65 61 74 65 20 4f 72 20 52 65 64 65 66 69 6e  reate Or Redefin
31b20 65 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e 73 20  e SQL Functions 
31b30 7b 48 31 36 31 30 30 7d 20 3c 53 32 30 32 30 30  {H16100} <S20200
31b40 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
31b50 66 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f  function creatio
31b60 6e 20 72 6f 75 74 69 6e 65 73 7d 0a 2a 2a 20 4b  n routines}.** K
31b70 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63  EYWORDS: {applic
31b80 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51  ation-defined SQ
31b90 4c 20 66 75 6e 63 74 69 6f 6e 7d 0a 2a 2a 20 4b  L function}.** K
31ba0 45 59 57 4f 52 44 53 3a 20 7b 61 70 70 6c 69 63  EYWORDS: {applic
31bb0 61 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 53 51  ation-defined SQ
31bc0 4c 20 66 75 6e 63 74 69 6f 6e 73 7d 0a 2a 2a 0a  L functions}.**.
31bd0 2a 2a 20 54 68 65 73 65 20 74 77 6f 20 66 75 6e  ** These two fun
31be0 63 74 69 6f 6e 73 20 28 63 6f 6c 6c 65 63 74 69  ctions (collecti
31bf0 76 65 6c 79 20 6b 6e 6f 77 6e 20 61 73 20 22 66  vely known as "f
31c00 75 6e 63 74 69 6f 6e 20 63 72 65 61 74 69 6f 6e  unction creation
31c10 20 72 6f 75 74 69 6e 65 73 22 29 0a 2a 2a 20 61   routines").** a
31c20 72 65 20 75 73 65 64 20 74 6f 20 61 64 64 20 53  re used to add S
31c30 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 20  QL functions or 
31c40 61 67 67 72 65 67 61 74 65 73 20 6f 72 20 74 6f  aggregates or to
31c50 20 72 65 64 65 66 69 6e 65 20 74 68 65 20 62 65   redefine the be
31c60 68 61 76 69 6f 72 0a 2a 2a 20 6f 66 20 65 78 69  havior.** of exi
31c70 73 74 69 6e 67 20 53 51 4c 20 66 75 6e 63 74 69  sting SQL functi
31c80 6f 6e 73 20 6f 72 20 61 67 67 72 65 67 61 74 65  ons or aggregate
31c90 73 2e 20 20 54 68 65 20 6f 6e 6c 79 20 64 69 66  s.  The only dif
31ca0 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
31cb0 74 68 65 0a 2a 2a 20 74 77 6f 20 69 73 20 74 68  the.** two is th
31cc0 61 74 20 74 68 65 20 73 65 63 6f 6e 64 20 70 61  at the second pa
31cd0 72 61 6d 65 74 65 72 2c 20 74 68 65 20 6e 61 6d  rameter, the nam
31ce0 65 20 6f 66 20 74 68 65 20 28 73 63 61 6c 61 72  e of the (scalar
31cf0 29 20 66 75 6e 63 74 69 6f 6e 20 6f 72 0a 2a 2a  ) function or.**
31d00 20 61 67 67 72 65 67 61 74 65 2c 20 69 73 20 65   aggregate, is e
31d10 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20  ncoded in UTF-8 
31d20 66 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61  for sqlite3_crea
31d30 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 20 61 6e  te_function() an
31d40 64 20 55 54 46 2d 31 36 0a 2a 2a 20 66 6f 72 20  d UTF-16.** for 
31d50 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
31d60 75 6e 63 74 69 6f 6e 31 36 28 29 2e 0a 2a 2a 0a  unction16()..**.
31d70 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
31d80 61 6d 65 74 65 72 20 69 73 20 74 68 65 20 5b 64  ameter is the [d
31d90 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
31da0 6f 6e 5d 20 74 6f 20 77 68 69 63 68 20 74 68 65  on] to which the
31db0 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e   SQL.** function
31dc0 20 69 73 20 74 6f 20 62 65 20 61 64 64 65 64 2e   is to be added.
31dd0 20 20 49 66 20 61 20 73 69 6e 67 6c 65 20 70 72    If a single pr
31de0 6f 67 72 61 6d 20 75 73 65 73 20 6d 6f 72 65 20  ogram uses more 
31df0 74 68 61 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  than one databas
31e00 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  e.** connection 
31e10 69 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68 65 6e  internally, then
31e20 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 6d   SQL functions m
31e30 75 73 74 20 62 65 20 61 64 64 65 64 20 69 6e 64  ust be added ind
31e40 69 76 69 64 75 61 6c 6c 79 20 74 6f 0a 2a 2a 20  ividually to.** 
31e50 65 61 63 68 20 64 61 74 61 62 61 73 65 20 63 6f  each database co
31e60 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  nnection..**.** 
31e70 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d  The second param
31e80 65 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65  eter is the name
31e90 20 6f 66 20 74 68 65 20 53 51 4c 20 66 75 6e 63   of the SQL func
31ea0 74 69 6f 6e 20 74 6f 20 62 65 20 63 72 65 61 74  tion to be creat
31eb0 65 64 20 6f 72 0a 2a 2a 20 72 65 64 65 66 69 6e  ed or.** redefin
31ec0 65 64 2e 20 20 54 68 65 20 6c 65 6e 67 74 68 20  ed.  The length 
31ed0 6f 66 20 74 68 65 20 6e 61 6d 65 20 69 73 20 6c  of the name is l
31ee0 69 6d 69 74 65 64 20 74 6f 20 32 35 35 20 62 79  imited to 255 by
31ef0 74 65 73 2c 20 65 78 63 6c 75 73 69 76 65 20 6f  tes, exclusive o
31f00 66 0a 2a 2a 20 74 68 65 20 7a 65 72 6f 2d 74 65  f.** the zero-te
31f10 72 6d 69 6e 61 74 6f 72 2e 20 20 4e 6f 74 65 20  rminator.  Note 
31f20 74 68 61 74 20 74 68 65 20 6e 61 6d 65 20 6c 65  that the name le
31f30 6e 67 74 68 20 6c 69 6d 69 74 20 69 73 20 69 6e  ngth limit is in
31f40 20 62 79 74 65 73 2c 20 6e 6f 74 0a 2a 2a 20 63   bytes, not.** c
31f50 68 61 72 61 63 74 65 72 73 2e 20 20 41 6e 79 20  haracters.  Any 
31f60 61 74 74 65 6d 70 74 20 74 6f 20 63 72 65 61 74  attempt to creat
31f70 65 20 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  e a function wit
31f80 68 20 61 20 6c 6f 6e 67 65 72 20 6e 61 6d 65 0a  h a longer name.
31f90 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  ** will result i
31fa0 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d  n [SQLITE_ERROR]
31fb0 20 62 65 69 6e 67 20 72 65 74 75 72 6e 65 64 2e   being returned.
31fc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
31fd0 20 70 61 72 61 6d 65 74 65 72 20 28 6e 41 72 67   parameter (nArg
31fe0 29 0a 2a 2a 20 69 73 20 74 68 65 20 6e 75 6d 62  ).** is the numb
31ff0 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
32000 74 68 61 74 20 74 68 65 20 53 51 4c 20 66 75 6e  that the SQL fun
32010 63 74 69 6f 6e 20 6f 72 0a 2a 2a 20 61 67 67 72  ction or.** aggr
32020 65 67 61 74 65 20 74 61 6b 65 73 2e 20 49 66 20  egate takes. If 
32030 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69  this parameter i
32040 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e  s negative, then
32050 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
32060 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74  n or.** aggregat
32070 65 20 6d 61 79 20 74 61 6b 65 20 61 6e 79 20 6e  e may take any n
32080 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e  umber of argumen
32090 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ts..**.** The fo
320a0 75 72 74 68 20 70 61 72 61 6d 65 74 65 72 2c 20  urth parameter, 
320b0 65 54 65 78 74 52 65 70 2c 20 73 70 65 63 69 66  eTextRep, specif
320c0 69 65 73 20 77 68 61 74 0a 2a 2a 20 5b 53 51 4c  ies what.** [SQL
320d0 49 54 45 5f 55 54 46 38 20 7c 20 74 65 78 74 20  ITE_UTF8 | text 
320e0 65 6e 63 6f 64 69 6e 67 5d 20 74 68 69 73 20 53  encoding] this S
320f0 51 4c 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66  QL function pref
32100 65 72 73 20 66 6f 72 0a 2a 2a 20 69 74 73 20 70  ers for.** its p
32110 61 72 61 6d 65 74 65 72 73 2e 20 20 41 6e 79 20  arameters.  Any 
32120 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  SQL function imp
32130 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 73 68 6f 75  lementation shou
32140 6c 64 20 62 65 20 61 62 6c 65 20 74 6f 20 77 6f  ld be able to wo
32150 72 6b 0a 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20  rk.** work with 
32160 55 54 46 2d 38 2c 20 55 54 46 2d 31 36 6c 65 2c  UTF-8, UTF-16le,
32170 20 6f 72 20 55 54 46 2d 31 36 62 65 2e 20 20 42   or UTF-16be.  B
32180 75 74 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e  ut some implemen
32190 74 61 74 69 6f 6e 73 20 6d 61 79 20 62 65 0a 2a  tations may be.*
321a0 2a 20 6d 6f 72 65 20 65 66 66 69 63 69 65 6e 74  * more efficient
321b0 20 77 69 74 68 20 6f 6e 65 20 65 6e 63 6f 64 69   with one encodi
321c0 6e 67 20 74 68 61 6e 20 61 6e 6f 74 68 65 72 2e  ng than another.
321d0 20 20 49 74 20 69 73 20 61 6c 6c 6f 77 65 64 20    It is allowed 
321e0 74 6f 0a 2a 2a 20 69 6e 76 6f 6b 65 20 73 71 6c  to.** invoke sql
321f0 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
32200 74 69 6f 6e 28 29 20 6f 72 20 73 71 6c 69 74 65  tion() or sqlite
32210 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
32220 6e 31 36 28 29 20 6d 75 6c 74 69 70 6c 65 0a 2a  n16() multiple.*
32230 2a 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65  * times with the
32240 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20 62   same function b
32250 75 74 20 77 69 74 68 20 64 69 66 66 65 72 65 6e  ut with differen
32260 74 20 76 61 6c 75 65 73 20 6f 66 20 65 54 65 78  t values of eTex
32270 74 52 65 70 2e 0a 2a 2a 20 57 68 65 6e 20 6d 75  tRep..** When mu
32280 6c 74 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  ltiple implement
32290 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61  ations of the sa
322a0 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 65 20  me function are 
322b0 61 76 61 69 6c 61 62 6c 65 2c 20 53 51 4c 69 74  available, SQLit
322c0 65 0a 2a 2a 20 77 69 6c 6c 20 70 69 63 6b 20 74  e.** will pick t
322d0 68 65 20 6f 6e 65 20 74 68 61 74 20 69 6e 76 6f  he one that invo
322e0 6c 76 65 73 20 74 68 65 20 6c 65 61 73 74 20 61  lves the least a
322f0 6d 6f 75 6e 74 20 6f 66 20 64 61 74 61 20 63 6f  mount of data co
32300 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20  nversion..** If 
32310 74 68 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20  there is only a 
32320 73 69 6e 67 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  single implement
32330 61 74 69 6f 6e 20 77 68 69 63 68 20 64 6f 65 73  ation which does
32340 20 6e 6f 74 20 63 61 72 65 20 77 68 61 74 20 74   not care what t
32350 65 78 74 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67 20  ext.** encoding 
32360 69 73 20 75 73 65 64 2c 20 74 68 65 6e 20 74 68  is used, then th
32370 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  e fourth argumen
32380 74 20 73 68 6f 75 6c 64 20 62 65 20 5b 53 51 4c  t should be [SQL
32390 49 54 45 5f 41 4e 59 5d 2e 0a 2a 2a 0a 2a 2a 20  ITE_ANY]..**.** 
323a0 54 68 65 20 66 69 66 74 68 20 70 61 72 61 6d 65  The fifth parame
323b0 74 65 72 20 69 73 20 61 6e 20 61 72 62 69 74 72  ter is an arbitr
323c0 61 72 79 20 70 6f 69 6e 74 65 72 2e 20 20 54 68  ary pointer.  Th
323d0 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
323e0 20 6f 66 20 74 68 65 0a 2a 2a 20 66 75 6e 63 74   of the.** funct
323f0 69 6f 6e 20 63 61 6e 20 67 61 69 6e 20 61 63 63  ion can gain acc
32400 65 73 73 20 74 6f 20 74 68 69 73 20 70 6f 69 6e  ess to this poin
32410 74 65 72 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ter using [sqlit
32420 65 33 5f 75 73 65 72 5f 64 61 74 61 28 29 5d 2e  e3_user_data()].
32430 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 76 65 6e  .**.** The seven
32440 74 68 2c 20 65 69 67 68 74 68 20 61 6e 64 20 6e  th, eighth and n
32450 69 6e 74 68 20 70 61 72 61 6d 65 74 65 72 73 2c  inth parameters,
32460 20 78 46 75 6e 63 2c 20 78 53 74 65 70 20 61 6e   xFunc, xStep an
32470 64 20 78 46 69 6e 61 6c 2c 20 61 72 65 0a 2a 2a  d xFinal, are.**
32480 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 43 2d 6c   pointers to C-l
32490 61 6e 67 75 61 67 65 20 66 75 6e 63 74 69 6f 6e  anguage function
324a0 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  s that implement
324b0 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f   the SQL functio
324c0 6e 20 6f 72 0a 2a 2a 20 61 67 67 72 65 67 61 74  n or.** aggregat
324d0 65 2e 20 41 20 73 63 61 6c 61 72 20 53 51 4c 20  e. A scalar SQL 
324e0 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65  function require
324f0 73 20 61 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74  s an implementat
32500 69 6f 6e 20 6f 66 20 74 68 65 20 78 46 75 6e 63  ion of the xFunc
32510 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 6c  .** callback onl
32520 79 2c 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73  y, NULL pointers
32530 20 73 68 6f 75 6c 64 20 62 65 20 70 61 73 73 65   should be passe
32540 64 20 61 73 20 74 68 65 20 78 53 74 65 70 20 61  d as the xStep a
32550 6e 64 20 78 46 69 6e 61 6c 0a 2a 2a 20 70 61 72  nd xFinal.** par
32560 61 6d 65 74 65 72 73 2e 20 41 6e 20 61 67 67 72  ameters. An aggr
32570 65 67 61 74 65 20 53 51 4c 20 66 75 6e 63 74 69  egate SQL functi
32580 6f 6e 20 72 65 71 75 69 72 65 73 20 61 6e 20 69  on requires an i
32590 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
325a0 20 78 53 74 65 70 0a 2a 2a 20 61 6e 64 20 78 46   xStep.** and xF
325b0 69 6e 61 6c 20 61 6e 64 20 4e 55 4c 4c 20 73 68  inal and NULL sh
325c0 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 66  ould be passed f
325d0 6f 72 20 78 46 75 6e 63 2e 20 54 6f 20 64 65 6c  or xFunc. To del
325e0 65 74 65 20 61 6e 20 65 78 69 73 74 69 6e 67 0a  ete an existing.
325f0 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20  ** SQL function 
32600 6f 72 20 61 67 67 72 65 67 61 74 65 2c 20 70 61  or aggregate, pa
32610 73 73 20 4e 55 4c 4c 20 66 6f 72 20 61 6c 6c 20  ss NULL for all 
32620 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e 20 63  three function c
32630 61 6c 6c 62 61 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  allbacks..**.** 
32640 49 74 20 69 73 20 70 65 72 6d 69 74 74 65 64 20  It is permitted 
32650 74 6f 20 72 65 67 69 73 74 65 72 20 6d 75 6c 74  to register mult
32660 69 70 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  iple implementat
32670 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
32680 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73 20 77 69  .** functions wi
32690 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
326a0 20 62 75 74 20 77 69 74 68 20 65 69 74 68 65 72   but with either
326b0 20 64 69 66 66 65 72 69 6e 67 20 6e 75 6d 62 65   differing numbe
326c0 72 73 20 6f 66 0a 2a 2a 20 61 72 67 75 6d 65 6e  rs of.** argumen
326d0 74 73 20 6f 72 20 64 69 66 66 65 72 69 6e 67 20  ts or differing 
326e0 70 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  preferred text e
326f0 6e 63 6f 64 69 6e 67 73 2e 20 20 53 51 4c 69 74  ncodings.  SQLit
32700 65 20 77 69 6c 6c 20 75 73 65 0a 2a 2a 20 74 68  e will use.** th
32710 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
32720 20 6d 6f 73 74 20 63 6c 6f 73 65 6c 79 20 6d 61   most closely ma
32730 74 63 68 65 73 20 74 68 65 20 77 61 79 20 69 6e  tches the way in
32740 20 77 68 69 63 68 20 74 68 65 0a 2a 2a 20 53 51   which the.** SQ
32750 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  L function is us
32760 65 64 2e 20 20 41 20 66 75 6e 63 74 69 6f 6e 20  ed.  A function 
32770 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77  implementation w
32780 69 74 68 20 61 20 6e 6f 6e 2d 6e 65 67 61 74 69  ith a non-negati
32790 76 65 0a 2a 2a 20 6e 41 72 67 20 70 61 72 61 6d  ve.** nArg param
327a0 65 74 65 72 20 69 73 20 61 20 62 65 74 74 65 72  eter is a better
327b0 20 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75   match than a fu
327c0 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74  nction implement
327d0 61 74 69 6f 6e 20 77 69 74 68 0a 2a 2a 20 61 20  ation with.** a 
327e0 6e 65 67 61 74 69 76 65 20 6e 41 72 67 2e 20 20  negative nArg.  
327f0 41 20 66 75 6e 63 74 69 6f 6e 20 77 68 65 72 65  A function where
32800 20 74 68 65 20 70 72 65 66 65 72 72 65 64 20 74   the preferred t
32810 65 78 74 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20  ext encoding.** 
32820 6d 61 74 63 68 65 73 20 74 68 65 20 64 61 74 61  matches the data
32830 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 69 73  base encoding is
32840 20 61 20 62 65 74 74 65 72 0a 2a 2a 20 6d 61 74   a better.** mat
32850 63 68 20 74 68 61 6e 20 61 20 66 75 6e 63 74 69  ch than a functi
32860 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e 63  on where the enc
32870 6f 64 69 6e 67 20 69 73 20 64 69 66 66 65 72 65  oding is differe
32880 6e 74 2e 20 20 0a 2a 2a 20 41 20 66 75 6e 63 74  nt.  .** A funct
32890 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20 65 6e  ion where the en
328a0 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65 6e 63  coding differenc
328b0 65 20 69 73 20 62 65 74 77 65 65 6e 20 55 54 46  e is between UTF
328c0 31 36 6c 65 20 61 6e 64 20 55 54 46 31 36 62 65  16le and UTF16be
328d0 0a 2a 2a 20 69 73 20 61 20 63 6c 6f 73 65 72 20  .** is a closer 
328e0 6d 61 74 63 68 20 74 68 61 6e 20 61 20 66 75 6e  match than a fun
328f0 63 74 69 6f 6e 20 77 68 65 72 65 20 74 68 65 20  ction where the 
32900 65 6e 63 6f 64 69 6e 67 20 64 69 66 66 65 72 65  encoding differe
32910 6e 63 65 20 69 73 0a 2a 2a 20 62 65 74 77 65 65  nce is.** betwee
32920 6e 20 55 54 46 38 20 61 6e 64 20 55 54 46 31 36  n UTF8 and UTF16
32930 2e 0a 2a 2a 0a 2a 2a 20 42 75 69 6c 74 2d 69 6e  ..**.** Built-in
32940 20 66 75 6e 63 74 69 6f 6e 73 20 6d 61 79 20 62   functions may b
32950 65 20 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20  e overloaded by 
32960 6e 65 77 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d  new application-
32970 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e  defined function
32980 73 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  s..** The first 
32990 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
329a0 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 77 69 74  ned function wit
329b0 68 20 61 20 67 69 76 65 6e 20 6e 61 6d 65 20 6f  h a given name o
329c0 76 65 72 72 69 64 65 73 20 61 6c 6c 0a 2a 2a 20  verrides all.** 
329d0 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f  built-in functio
329e0 6e 73 20 69 6e 20 74 68 65 20 73 61 6d 65 20 5b  ns in the same [
329f0 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
32a00 69 6f 6e 5d 20 77 69 74 68 20 74 68 65 20 73 61  ion] with the sa
32a10 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 20 53 75 62 73  me name..** Subs
32a20 65 71 75 65 6e 74 20 61 70 70 6c 69 63 61 74 69  equent applicati
32a30 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
32a40 69 6f 6e 73 20 6f 66 20 74 68 65 20 73 61 6d 65  ions of the same
32a50 20 6e 61 6d 65 20 6f 6e 6c 79 20 6f 76 65 72 72   name only overr
32a60 69 64 65 20 0a 2a 2a 20 70 72 69 6f 72 20 61 70  ide .** prior ap
32a70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
32a80 64 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  d functions that
32a90 20 61 72 65 20 61 6e 20 65 78 61 63 74 20 6d 61   are an exact ma
32aa0 74 63 68 20 66 6f 72 20 74 68 65 0a 2a 2a 20 6e  tch for the.** n
32ab0 75 6d 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74  umber of paramet
32ac0 65 72 73 20 61 6e 64 20 70 72 65 66 65 72 72 65  ers and preferre
32ad0 64 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 0a 2a  d encoding..**.*
32ae0 2a 20 41 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  * An application
32af0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
32b00 6e 20 69 73 20 70 65 72 6d 69 74 74 65 64 20 74  n is permitted t
32b10 6f 20 63 61 6c 6c 20 6f 74 68 65 72 0a 2a 2a 20  o call other.** 
32b20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
32b30 73 2e 20 20 48 6f 77 65 76 65 72 2c 20 73 75 63  s.  However, suc
32b40 68 20 63 61 6c 6c 73 20 6d 75 73 74 20 6e 6f 74  h calls must not
32b50 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 64 61  .** close the da
32b60 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
32b70 6e 20 6e 6f 72 20 66 69 6e 61 6c 69 7a 65 20 6f  n nor finalize o
32b80 72 20 72 65 73 65 74 20 74 68 65 20 70 72 65 70  r reset the prep
32b90 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  ared.** statemen
32ba0 74 20 69 6e 20 77 68 69 63 68 20 74 68 65 20 66  t in which the f
32bb0 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
32bc0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ng..**.** INVARI
32bd0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ANTS:.**.** {H16
32be0 31 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  103} The [sqlite
32bf0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
32c00 6e 31 36 28 44 2c 58 2c 2e 2e 2e 29 5d 20 69 6e  n16(D,X,...)] in
32c10 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65  terface shall be
32c20 68 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  have.**         
32c30 20 61 73 20 5b 73 71 6c 69 74 65 33 5f 63 72 65   as [sqlite3_cre
32c40 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58  ate_function(D,X
32c50 2c 2e 2e 2e 29 5d 20 69 6e 20 65 76 65 72 79 20  ,...)] in every 
32c60 77 61 79 20 65 78 63 65 70 74 20 74 68 61 74 20  way except that 
32c70 69 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  it.**          i
32c80 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 58 20  nterprets the X 
32c90 61 72 67 75 6d 65 6e 74 20 61 73 20 7a 65 72 6f  argument as zero
32ca0 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46 2d  -terminated UTF-
32cb0 31 36 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e  16.**          n
32cc0 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72  ative byte order
32cd0 20 69 6e 73 74 65 61 64 20 6f 66 20 61 73 20 7a   instead of as z
32ce0 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
32cf0 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  TF-8..**.** {H16
32d00 31 30 36 7d 20 41 20 73 75 63 63 65 73 73 66 75  106} A successfu
32d10 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  l invocation of 
32d20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
32d30 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
32d40 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45  function(D,X,N,E
32d50 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61 63 65  ,...)] interface
32d60 20 73 68 61 6c 6c 20 72 65 67 69 73 74 65 72 0a   shall register.
32d70 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 72  **          or r
32d80 65 70 6c 61 63 65 73 20 63 61 6c 6c 62 61 63 6b  eplaces callback
32d90 20 66 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68   functions in th
32da0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
32db0 65 63 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20  ection] D.**    
32dc0 20 20 20 20 20 20 75 73 65 64 20 74 6f 20 69 6d        used to im
32dd0 70 6c 65 6d 65 6e 74 20 74 68 65 20 53 51 4c 20  plement the SQL 
32de0 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58  function named X
32df0 20 77 69 74 68 20 4e 20 70 61 72 61 6d 65 74 65   with N paramete
32e00 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  rs.**          a
32e10 6e 64 20 68 61 76 69 6e 67 20 61 20 70 72 65 66  nd having a pref
32e20 65 72 72 65 64 20 74 65 78 74 20 65 6e 63 6f 64  erred text encod
32e30 69 6e 67 20 6f 66 20 45 2e 0a 2a 2a 0a 2a 2a 20  ing of E..**.** 
32e40 7b 48 31 36 31 30 39 7d 20 41 20 73 75 63 63 65  {H16109} A succe
32e50 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b 73  ssful call to [s
32e60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
32e70 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50  nction(D,X,N,E,P
32e80 2c 46 2c 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20  ,F,S,L)].**     
32e90 20 20 20 20 20 73 68 61 6c 6c 20 72 65 70 6c 61       shall repla
32ea0 63 65 20 74 68 65 20 50 2c 20 46 2c 20 53 2c 20  ce the P, F, S, 
32eb0 61 6e 64 20 4c 20 76 61 6c 75 65 73 20 66 72 6f  and L values fro
32ec0 6d 20 61 6e 79 20 70 72 69 6f 72 20 63 61 6c 6c  m any prior call
32ed0 73 20 77 69 74 68 0a 2a 2a 20 20 20 20 20 20 20  s with.**       
32ee0 20 20 20 74 68 65 20 73 61 6d 65 20 44 2c 20 58     the same D, X
32ef0 2c 20 4e 2c 20 61 6e 64 20 45 20 76 61 6c 75 65  , N, and E value
32f00 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 31 32  s..**.** {H16112
32f10 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63  } The [sqlite3_c
32f20 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44  reate_function(D
32f30 2c 58 2c 2e 2e 2e 29 5d 20 69 6e 74 65 72 66 61  ,X,...)] interfa
32f40 63 65 20 73 68 61 6c 6c 20 66 61 69 6c 0a 2a 2a  ce shall fail.**
32f50 20 20 20 20 20 20 20 20 20 20 69 66 20 74 68 65            if the
32f60 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 6e 61   SQL function na
32f70 6d 65 20 58 20 69 73 0a 2a 2a 20 20 20 20 20 20  me X is.**      
32f80 20 20 20 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20      longer than 
32f90 32 35 35 20 62 79 74 65 73 20 65 78 63 6c 75 73  255 bytes exclus
32fa0 69 76 65 20 6f 66 20 74 68 65 20 7a 65 72 6f 20  ive of the zero 
32fb0 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a  terminator..**.*
32fc0 2a 20 7b 48 31 36 31 31 38 7d 20 54 68 65 20 5b  * {H16118} The [
32fd0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66  sqlite3_create_f
32fe0 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c  unction(D,X,N,E,
32ff0 50 2c 46 2c 53 2c 4c 29 5d 20 69 6e 74 65 72 66  P,F,S,L)] interf
33000 61 63 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ace.**          
33010 73 68 61 6c 6c 20 66 61 69 6c 20 75 6e 6c 65 73  shall fail unles
33020 73 20 65 69 74 68 65 72 20 46 20 69 73 20 4e 55  s either F is NU
33030 4c 4c 20 61 6e 64 20 53 20 61 6e 64 20 4c 20 61  LL and S and L a
33040 72 65 20 6e 6f 6e 2d 4e 55 4c 4c 20 6f 72 0a 2a  re non-NULL or.*
33050 2a 2a 20 20 20 20 20 20 20 20 20 46 20 69 73 20  **         F is 
33060 6e 6f 6e 2d 4e 55 4c 4c 20 61 6e 64 20 53 20 61  non-NULL and S a
33070 6e 64 20 4c 20 61 72 65 20 4e 55 4c 4c 2e 0a 2a  nd L are NULL..*
33080 2a 0a 2a 2a 20 7b 48 31 36 31 32 31 7d 20 54 68  *.** {H16121} Th
33090 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  e [sqlite3_creat
330a0 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 2e 2e 2e  e_function(D,...
330b0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 73 68 61  )] interface sha
330c0 6c 6c 20 66 61 69 6c 73 20 77 69 74 68 20 61 6e  ll fails with an
330d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 65 72 72  .**          err
330e0 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49  or code of [SQLI
330f0 54 45 5f 42 55 53 59 5d 20 69 66 20 74 68 65 72  TE_BUSY] if ther
33100 65 20 65 78 69 73 74 20 5b 70 72 65 70 61 72 65  e exist [prepare
33110 64 20 73 74 61 74 65 6d 65 6e 74 73 5d 0a 2a 2a  d statements].**
33120 20 20 20 20 20 20 20 20 20 20 61 73 73 6f 63 69            associ
33130 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b 64  ated with the [d
33140 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
33150 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  on] D..**.** {H1
33160 36 31 32 34 7d 20 54 68 65 20 5b 73 71 6c 69 74  6124} The [sqlit
33170 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
33180 6f 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 20 69  on(D,X,N,...)] i
33190 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 66  nterface shall f
331a0 61 69 6c 20 77 69 74 68 0a 2a 2a 20 20 20 20 20  ail with.**     
331b0 20 20 20 20 20 61 6e 20 65 72 72 6f 72 20 63 6f       an error co
331c0 64 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 45 52  de of [SQLITE_ER
331d0 52 4f 52 5d 20 69 66 20 70 61 72 61 6d 65 74 65  ROR] if paramete
331e0 72 20 4e 20 69 73 20 6c 65 73 73 0a 2a 2a 20 20  r N is less.**  
331f0 20 20 20 20 20 20 20 20 74 68 61 6e 20 2d 31 20          than -1 
33200 6f 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  or greater than 
33210 31 32 37 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31  127..**.** {H161
33220 32 37 7d 20 57 68 65 6e 20 4e 20 69 73 20 6e 6f  27} When N is no
33230 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20  n-negative, the 
33240 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
33250 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 2e  function(D,X,N,.
33260 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..)].**         
33270 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
33280 20 72 65 67 69 73 74 65 72 20 63 61 6c 6c 62 61   register callba
33290 63 6b 73 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65  cks to be invoke
332a0 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20  d for the.**    
332b0 20 20 20 20 20 20 53 51 4c 20 66 75 6e 63 74 69        SQL functi
332c0 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e  on.**          n
332d0 61 6d 65 64 20 58 20 77 68 65 6e 20 74 68 65 20  amed X when the 
332e0 6e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65  number of argume
332f0 6e 74 73 20 74 6f 20 74 68 65 20 53 51 4c 20 66  nts to the SQL f
33300 75 6e 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 20 20  unction is.**   
33310 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 4e         exactly N
33320 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 30 7d  ..**.** {H16130}
33330 20 57 68 65 6e 20 4e 20 69 73 20 2d 31 2c 20 74   When N is -1, t
33340 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  he [sqlite3_crea
33350 74 65 5f 66 75 6e 63 74 69 6f 6e 28 44 2c 58 2c  te_function(D,X,
33360 4e 2c 2e 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20  N,...)].**      
33370 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68      interface sh
33380 61 6c 6c 20 72 65 67 69 73 74 65 72 20 63 61 6c  all register cal
33390 6c 62 61 63 6b 73 20 74 6f 20 62 65 20 69 6e 76  lbacks to be inv
333a0 6f 6b 65 64 20 66 6f 72 20 74 68 65 20 53 51 4c  oked for the SQL
333b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e  .**          fun
333c0 63 74 69 6f 6e 20 6e 61 6d 65 64 20 58 20 77 69  ction named X wi
333d0 74 68 20 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66  th any number of
333e0 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a   arguments..**.*
333f0 2a 20 7b 48 31 36 31 33 33 7d 20 57 68 65 6e 20  * {H16133} When 
33400 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  calls to [sqlite
33410 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
33420 6e 28 44 2c 58 2c 4e 2c 2e 2e 2e 29 5d 0a 2a 2a  n(D,X,N,...)].**
33430 20 20 20 20 20 20 20 20 20 20 73 70 65 63 69 66            specif
33440 79 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70 6c 65  y multiple imple
33450 6d 65 6e 74 61 74 69 6f 6e 73 20 6f 66 20 74 68  mentations of th
33460 65 20 73 61 6d 65 20 66 75 6e 63 74 69 6f 6e 20  e same function 
33470 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e  X.**          an
33480 64 20 77 68 65 6e 20 6f 6e 65 20 69 6d 70 6c 65  d when one imple
33490 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 4e 3e  mentation has N>
334a0 3d 30 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72  =0 and the other
334b0 20 68 61 73 20 4e 3d 28 2d 31 29 0a 2a 2a 20 20   has N=(-1).**  
334c0 20 20 20 20 20 20 20 20 74 68 65 20 69 6d 70 6c          the impl
334d0 65 6d 65 6e 74 61 74 69 6f 6e 20 77 69 74 68 20  ementation with 
334e0 61 20 6e 6f 6e 2d 7a 65 72 6f 20 4e 20 73 68 61  a non-zero N sha
334f0 6c 6c 20 62 65 20 70 72 65 66 65 72 72 65 64 2e  ll be preferred.
33500 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 36 7d 20  .**.** {H16136} 
33510 57 68 65 6e 20 63 61 6c 6c 73 20 74 6f 20 5b 73  When calls to [s
33520 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
33530 6e 63 74 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 2e  nction(D,X,N,E,.
33540 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..)].**         
33550 20 73 70 65 63 69 66 79 20 6d 75 6c 74 69 70 6c   specify multipl
33560 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
33570 73 20 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75  s of the same fu
33580 6e 63 74 69 6f 6e 20 58 20 77 69 74 68 0a 2a 2a  nction X with.**
33590 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73 61            the sa
335a0 6d 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  me number of arg
335b0 75 6d 65 6e 74 73 20 4e 20 62 75 74 20 77 69 74  uments N but wit
335c0 68 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 20  h different.**  
335d0 20 20 20 20 20 20 20 20 65 6e 63 6f 64 69 6e 67          encoding
335e0 73 20 45 2c 20 74 68 65 6e 20 74 68 65 20 69 6d  s E, then the im
335f0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 77 68 65  plementation whe
33600 72 65 20 45 20 6d 61 74 63 68 65 73 20 74 68 65  re E matches the
33610 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 61 74  .**          dat
33620 61 62 61 73 65 20 65 6e 63 6f 64 69 6e 67 20 73  abase encoding s
33630 68 61 6c 6c 20 70 72 65 66 65 72 72 65 64 2e 0a  hall preferred..
33640 2a 2a 0a 2a 2a 20 7b 48 31 36 31 33 39 7d 20 46  **.** {H16139} F
33650 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  or an aggregate 
33660 53 51 4c 20 66 75 6e 63 74 69 6f 6e 20 63 72 65  SQL function cre
33670 61 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20 20 20  ated using.**   
33680 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
33690 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
336a0 44 2c 58 2c 4e 2c 45 2c 50 2c 30 2c 53 2c 4c 29  D,X,N,E,P,0,S,L)
336b0 5d 20 74 68 65 20 66 69 6e 61 6c 69 7a 65 72 0a  ] the finalizer.
336c0 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63  **          func
336d0 74 69 6f 6e 20 4c 20 73 68 61 6c 6c 20 61 6c 77  tion L shall alw
336e0 61 79 73 20 62 65 20 69 6e 76 6f 6b 65 64 20 65  ays be invoked e
336f0 78 61 63 74 6c 79 20 6f 6e 63 65 20 69 66 20 74  xactly once if t
33700 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  he.**          s
33710 74 65 70 20 66 75 6e 63 74 69 6f 6e 20 53 20 69  tep function S i
33720 73 20 63 61 6c 6c 65 64 20 6f 6e 65 20 6f 72 20  s called one or 
33730 6d 6f 72 65 20 74 69 6d 65 73 2e 0a 2a 2a 0a 2a  more times..**.*
33740 2a 20 7b 48 31 36 31 34 32 7d 20 57 68 65 6e 20  * {H16142} When 
33750 53 51 4c 69 74 65 20 69 6e 76 6f 6b 65 73 20 65  SQLite invokes e
33760 69 74 68 65 72 20 74 68 65 20 78 46 75 6e 63 20  ither the xFunc 
33770 6f 72 20 78 53 74 65 70 20 66 75 6e 63 74 69 6f  or xStep functio
33780 6e 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  n of.**         
33790 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d   an application-
337a0 64 65 66 69 6e 65 64 20 53 51 4c 20 66 75 6e 63  defined SQL func
337b0 74 69 6f 6e 20 6f 72 20 61 67 67 72 65 67 61 74  tion or aggregat
337c0 65 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20  e created.**    
337d0 20 20 20 20 20 20 62 79 20 5b 73 71 6c 69 74 65        by [sqlite
337e0 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f  3_create_functio
337f0 6e 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  n()] or [sqlite3
33800 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
33810 31 36 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  16()],.**       
33820 20 20 20 74 68 65 6e 20 74 68 65 20 61 72 72 61     then the arra
33830 79 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f 76 61  y of [sqlite3_va
33840 6c 75 65 5d 20 6f 62 6a 65 63 74 73 20 70 61 73  lue] objects pas
33850 73 65 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20  sed as the.**   
33860 20 20 20 20 20 20 20 74 68 69 72 64 20 70 61 72         third par
33870 61 6d 65 74 65 72 20 73 68 61 6c 6c 20 62 65 20  ameter shall be 
33880 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74  [protected sqlit
33890 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74  e3_value] object
338a0 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  s..*/.SQLITE_API
338b0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
338c0 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  ate_function(.  
338d0 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
338e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 75 6e 63  onst char *zFunc
338f0 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20  tionName,.  int 
33900 6e 41 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78  nArg,.  int eTex
33910 74 52 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 41  tRep,.  void *pA
33920 70 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  pp,.  void (*xFu
33930 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
33940 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
33950 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76 6f 69  _value**),.  voi
33960 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
33970 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
33980 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
33990 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61  ,.  void (*xFina
339a0 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  l)(sqlite3_conte
339b0 78 74 2a 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41  xt*).);.SQLITE_A
339c0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
339d0 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36  reate_function16
339e0 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
339f0 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
33a00 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 2c 0a 20 20  FunctionName,.  
33a10 69 6e 74 20 6e 41 72 67 2c 0a 20 20 69 6e 74 20  int nArg,.  int 
33a20 65 54 65 78 74 52 65 70 2c 0a 20 20 76 6f 69 64  eTextRep,.  void
33a30 20 2a 70 41 70 70 2c 0a 20 20 76 6f 69 64 20 28   *pApp,.  void (
33a40 2a 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f  *xFunc)(sqlite3_
33a50 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
33a60 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20  ite3_value**),. 
33a70 20 76 6f 69 64 20 28 2a 78 53 74 65 70 29 28 73   void (*xStep)(s
33a80 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
33a90 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
33aa0 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
33ab0 46 69 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63  Final)(sqlite3_c
33ac0 6f 6e 74 65 78 74 2a 29 0a 29 3b 0a 0a 2f 2a 0a  ontext*).);../*.
33ad0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 78  ** CAPI3REF: Tex
33ae0 74 20 45 6e 63 6f 64 69 6e 67 73 20 7b 48 31 30  t Encodings {H10
33af0 32 36 37 7d 20 3c 53 35 30 32 30 30 3e 20 3c 48  267} <S50200> <H
33b00 31 36 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  16100>.**.** The
33b10 73 65 20 63 6f 6e 73 74 61 6e 74 20 64 65 66 69  se constant defi
33b20 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65 73  ne integer codes
33b30 20 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 20   that represent 
33b40 74 68 65 20 76 61 72 69 6f 75 73 0a 2a 2a 20 74  the various.** t
33b50 65 78 74 20 65 6e 63 6f 64 69 6e 67 73 20 73 75  ext encodings su
33b60 70 70 6f 72 74 65 64 20 62 79 20 53 51 4c 69 74  pported by SQLit
33b70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
33b80 4c 49 54 45 5f 55 54 46 38 20 20 20 20 20 20 20  LITE_UTF8       
33b90 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 53 51      1.#define SQ
33ba0 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
33bb0 20 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51      2.#define SQ
33bc0 4c 49 54 45 5f 55 54 46 31 36 42 45 20 20 20 20  LITE_UTF16BE    
33bd0 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 53 51      3.#define SQ
33be0 4c 49 54 45 5f 55 54 46 31 36 20 20 20 20 20 20  LITE_UTF16      
33bf0 20 20 20 20 34 20 20 20 20 2f 2a 20 55 73 65 20      4    /* Use 
33c00 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64 65  native byte orde
33c10 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  r */.#define SQL
33c20 49 54 45 5f 41 4e 59 20 20 20 20 20 20 20 20 20  ITE_ANY         
33c30 20 20 20 35 20 20 20 20 2f 2a 20 73 71 6c 69 74     5    /* sqlit
33c40 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
33c50 6f 6e 20 6f 6e 6c 79 20 2a 2f 0a 23 64 65 66 69  on only */.#defi
33c60 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36 5f  ne SQLITE_UTF16_
33c70 41 4c 49 47 4e 45 44 20 20 38 20 20 20 20 2f 2a  ALIGNED  8    /*
33c80 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
33c90 63 6f 6c 6c 61 74 69 6f 6e 20 6f 6e 6c 79 20 2a  collation only *
33ca0 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  /../*.** CAPI3RE
33cb0 46 3a 20 44 65 70 72 65 63 61 74 65 64 20 46 75  F: Deprecated Fu
33cc0 6e 63 74 69 6f 6e 73 0a 2a 2a 20 44 45 50 52 45  nctions.** DEPRE
33cd0 43 41 54 45 44 0a 2a 2a 0a 2a 2a 20 54 68 65 73  CATED.**.** Thes
33ce0 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20  e functions are 
33cf0 5b 64 65 70 72 65 63 61 74 65 64 5d 2e 20 20 49  [deprecated].  I
33d00 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 69 6e 74  n order to maint
33d10 61 69 6e 0a 2a 2a 20 62 61 63 6b 77 61 72 64 73  ain.** backwards
33d20 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77   compatibility w
33d30 69 74 68 20 6f 6c 64 65 72 20 63 6f 64 65 2c 20  ith older code, 
33d40 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73 20  these functions 
33d50 63 6f 6e 74 69 6e 75 65 20 0a 2a 2a 20 74 6f 20  continue .** to 
33d60 62 65 20 73 75 70 70 6f 72 74 65 64 2e 20 20 48  be supported.  H
33d70 6f 77 65 76 65 72 2c 20 6e 65 77 20 61 70 70 6c  owever, new appl
33d80 69 63 61 74 69 6f 6e 73 20 73 68 6f 75 6c 64 20  ications should 
33d90 61 76 6f 69 64 0a 2a 2a 20 74 68 65 20 75 73 65  avoid.** the use
33da0 20 6f 66 20 74 68 65 73 65 20 66 75 6e 63 74 69   of these functi
33db0 6f 6e 73 2e 20 20 54 6f 20 68 65 6c 70 20 65 6e  ons.  To help en
33dc0 63 6f 75 72 61 67 65 20 70 65 6f 70 6c 65 20 74  courage people t
33dd0 6f 20 61 76 6f 69 64 0a 2a 2a 20 75 73 69 6e 67  o avoid.** using
33de0 20 74 68 65 73 65 20 66 75 6e 63 74 69 6f 6e 73   these functions
33df0 2c 20 77 65 20 61 72 65 20 6e 6f 74 20 67 6f 69  , we are not goi
33e00 6e 67 20 74 6f 20 74 65 6c 6c 20 79 6f 75 20 77  ng to tell you w
33e10 68 61 74 20 74 68 65 79 20 64 6f 2e 0a 2a 2f 0a  hat they do..*/.
33e20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
33e30 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 53  MIT_DEPRECATED.S
33e40 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45  QLITE_API SQLITE
33e50 5f 44 45 50 52 45 43 41 54 45 44 20 69 6e 74 20  _DEPRECATED int 
33e60 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
33e70 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  e_count(sqlite3_
33e80 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
33e90 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45 50  E_API SQLITE_DEP
33ea0 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c 69  RECATED int sqli
33eb0 74 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69  te3_expired(sqli
33ec0 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 53 51 4c 49  te3_stmt*);.SQLI
33ed0 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 44 45  TE_API SQLITE_DE
33ee0 50 52 45 43 41 54 45 44 20 69 6e 74 20 73 71 6c  PRECATED int sql
33ef0 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
33f00 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f 73  ndings(sqlite3_s
33f10 74 6d 74 2a 2c 20 73 71 6c 69 74 65 33 5f 73 74  tmt*, sqlite3_st
33f20 6d 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  mt*);.SQLITE_API
33f30 20 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54   SQLITE_DEPRECAT
33f40 45 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67  ED int sqlite3_g
33f50 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f  lobal_recover(vo
33f60 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
33f70 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
33f80 44 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 74  D void sqlite3_t
33f90 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f  hread_cleanup(vo
33fa0 69 64 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  id);.SQLITE_API 
33fb0 53 51 4c 49 54 45 5f 44 45 50 52 45 43 41 54 45  SQLITE_DEPRECATE
33fc0 44 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  D int sqlite3_me
33fd0 6d 6f 72 79 5f 61 6c 61 72 6d 28 76 6f 69 64 28  mory_alarm(void(
33fe0 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33  *)(void*,sqlite3
33ff0 5f 69 6e 74 36 34 2c 69 6e 74 29 2c 76 6f 69 64  _int64,int),void
34000 2a 2c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29  *,sqlite3_int64)
34010 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
34020 43 41 50 49 33 52 45 46 3a 20 4f 62 74 61 69 6e  CAPI3REF: Obtain
34030 69 6e 67 20 53 51 4c 20 46 75 6e 63 74 69 6f 6e  ing SQL Function
34040 20 50 61 72 61 6d 65 74 65 72 20 56 61 6c 75 65   Parameter Value
34050 73 20 7b 48 31 35 31 30 30 7d 20 3c 53 32 30 32  s {H15100} <S202
34060 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 2d  00>.**.** The C-
34070 6c 61 6e 67 75 61 67 65 20 69 6d 70 6c 65 6d 65  language impleme
34080 6e 74 61 74 69 6f 6e 20 6f 66 20 53 51 4c 20 66  ntation of SQL f
34090 75 6e 63 74 69 6f 6e 73 20 61 6e 64 20 61 67 67  unctions and agg
340a0 72 65 67 61 74 65 73 20 75 73 65 73 0a 2a 2a 20  regates uses.** 
340b0 74 68 69 73 20 73 65 74 20 6f 66 20 69 6e 74 65  this set of inte
340c0 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73 20 74  rface routines t
340d0 6f 20 61 63 63 65 73 73 20 74 68 65 20 70 61 72  o access the par
340e0 61 6d 65 74 65 72 20 76 61 6c 75 65 73 20 6f 6e  ameter values on
340f0 0a 2a 2a 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  .** the function
34100 20 6f 72 20 61 67 67 72 65 67 61 74 65 2e 0a 2a   or aggregate..*
34110 2a 0a 2a 2a 20 54 68 65 20 78 46 75 6e 63 20 28  *.** The xFunc (
34120 66 6f 72 20 73 63 61 6c 61 72 20 66 75 6e 63 74  for scalar funct
34130 69 6f 6e 73 29 20 6f 72 20 78 53 74 65 70 20 28  ions) or xStep (
34140 66 6f 72 20 61 67 67 72 65 67 61 74 65 73 29 20  for aggregates) 
34150 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 74 6f  parameters.** to
34160 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
34170 5f 66 75 6e 63 74 69 6f 6e 28 29 5d 20 61 6e 64  _function()] and
34180 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
34190 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d 0a 2a  _function16()].*
341a0 2a 20 64 65 66 69 6e 65 20 63 61 6c 6c 62 61 63  * define callbac
341b0 6b 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ks that implemen
341c0 74 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69  t the SQL functi
341d0 6f 6e 73 20 61 6e 64 20 61 67 67 72 65 67 61 74  ons and aggregat
341e0 65 73 2e 0a 2a 2a 20 54 68 65 20 34 74 68 20 70  es..** The 4th p
341f0 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 73  arameter to thes
34200 65 20 63 61 6c 6c 62 61 63 6b 73 20 69 73 20 61  e callbacks is a
34210 6e 20 61 72 72 61 79 20 6f 66 20 70 6f 69 6e 74  n array of point
34220 65 72 73 20 74 6f 0a 2a 2a 20 5b 70 72 6f 74 65  ers to.** [prote
34230 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c  cted sqlite3_val
34240 75 65 5d 20 6f 62 6a 65 63 74 73 2e 20 20 54 68  ue] objects.  Th
34250 65 72 65 20 69 73 20 6f 6e 65 20 5b 73 71 6c 69  ere is one [sqli
34260 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
34270 74 20 66 6f 72 0a 2a 2a 20 65 61 63 68 20 70 61  t for.** each pa
34280 72 61 6d 65 74 65 72 20 74 6f 20 74 68 65 20 53  rameter to the S
34290 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
342a0 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65  ese routines are
342b0 20 75 73 65 64 20 74 6f 0a 2a 2a 20 65 78 74 72   used to.** extr
342c0 61 63 74 20 76 61 6c 75 65 73 20 66 72 6f 6d 20  act values from 
342d0 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  the [sqlite3_val
342e0 75 65 5d 20 6f 62 6a 65 63 74 73 2e 0a 2a 2a 0a  ue] objects..**.
342f0 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65  ** These routine
34300 73 20 77 6f 72 6b 20 6f 6e 6c 79 20 77 69 74 68  s work only with
34310 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
34320 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
34330 74 73 2e 0a 2a 2a 20 41 6e 79 20 61 74 74 65 6d  ts..** Any attem
34340 70 74 20 74 6f 20 75 73 65 20 74 68 65 73 65 20  pt to use these 
34350 72 6f 75 74 69 6e 65 73 20 6f 6e 20 61 6e 20 5b  routines on an [
34360 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69  unprotected sqli
34370 74 65 33 5f 76 61 6c 75 65 5d 0a 2a 2a 20 6f 62  te3_value].** ob
34380 6a 65 63 74 20 72 65 73 75 6c 74 73 20 69 6e 20  ject results in 
34390 75 6e 64 65 66 69 6e 65 64 20 62 65 68 61 76 69  undefined behavi
343a0 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  or..**.** These 
343b0 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20 6a 75  routines work ju
343c0 73 74 20 6c 69 6b 65 20 74 68 65 20 63 6f 72 72  st like the corr
343d0 65 73 70 6f 6e 64 69 6e 67 20 5b 63 6f 6c 75 6d  esponding [colum
343e0 6e 20 61 63 63 65 73 73 20 66 75 6e 63 74 69 6f  n access functio
343f0 6e 73 5d 0a 2a 2a 20 65 78 63 65 70 74 20 74 68  ns].** except th
34400 61 74 20 20 74 68 65 73 65 20 72 6f 75 74 69 6e  at  these routin
34410 65 73 20 74 61 6b 65 20 61 20 73 69 6e 67 6c 65  es take a single
34420 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71 6c 69   [protected sqli
34430 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63  te3_value] objec
34440 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 69 6e 73  t.** pointer ins
34450 74 65 61 64 20 6f 66 20 61 20 5b 73 71 6c 69 74  tead of a [sqlit
34460 65 33 5f 73 74 6d 74 2a 5d 20 70 6f 69 6e 74 65  e3_stmt*] pointe
34470 72 20 61 6e 64 20 61 6e 20 69 6e 74 65 67 65 72  r and an integer
34480 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 2e 0a   column number..
34490 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
344a0 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28 29  3_value_text16()
344b0 20 69 6e 74 65 72 66 61 63 65 20 65 78 74 72 61   interface extra
344c0 63 74 73 20 61 20 55 54 46 2d 31 36 20 73 74 72  cts a UTF-16 str
344d0 69 6e 67 0a 2a 2a 20 69 6e 20 74 68 65 20 6e 61  ing.** in the na
344e0 74 69 76 65 20 62 79 74 65 2d 6f 72 64 65 72 20  tive byte-order 
344f0 6f 66 20 74 68 65 20 68 6f 73 74 20 6d 61 63 68  of the host mach
34500 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c  ine.  The.** sql
34510 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
34520 36 62 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65  6be() and sqlite
34530 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
34540 28 29 20 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a  () interfaces.**
34550 20 65 78 74 72 61 63 74 20 55 54 46 2d 31 36 20   extract UTF-16 
34560 73 74 72 69 6e 67 73 20 61 73 20 62 69 67 2d 65  strings as big-e
34570 6e 64 69 61 6e 20 61 6e 64 20 6c 69 74 74 6c 65  ndian and little
34580 2d 65 6e 64 69 61 6e 20 72 65 73 70 65 63 74 69  -endian respecti
34590 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  vely..**.** The 
345a0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75  sqlite3_value_nu
345b0 6d 65 72 69 63 5f 74 79 70 65 28 29 20 69 6e 74  meric_type() int
345c0 65 72 66 61 63 65 20 61 74 74 65 6d 70 74 73 20  erface attempts 
345d0 74 6f 20 61 70 70 6c 79 0a 2a 2a 20 6e 75 6d 65  to apply.** nume
345e0 72 69 63 20 61 66 66 69 6e 69 74 79 20 74 6f 20  ric affinity to 
345f0 74 68 65 20 76 61 6c 75 65 2e 20 20 54 68 69 73  the value.  This
34600 20 6d 65 61 6e 73 20 74 68 61 74 20 61 6e 20 61   means that an a
34610 74 74 65 6d 70 74 20 69 73 0a 2a 2a 20 6d 61 64  ttempt is.** mad
34620 65 20 74 6f 20 63 6f 6e 76 65 72 74 20 74 68 65  e to convert the
34630 20 76 61 6c 75 65 20 74 6f 20 61 6e 20 69 6e 74   value to an int
34640 65 67 65 72 20 6f 72 20 66 6c 6f 61 74 69 6e 67  eger or floating
34650 20 70 6f 69 6e 74 2e 20 20 49 66 0a 2a 2a 20 73   point.  If.** s
34660 75 63 68 20 61 20 63 6f 6e 76 65 72 73 69 6f 6e  uch a conversion
34670 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69 74   is possible wit
34680 68 6f 75 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66  hout loss of inf
34690 6f 72 6d 61 74 69 6f 6e 20 28 69 6e 20 6f 74 68  ormation (in oth
346a0 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20 69 66 20  er.** words, if 
346b0 74 68 65 20 76 61 6c 75 65 20 69 73 20 61 20 73  the value is a s
346c0 74 72 69 6e 67 20 74 68 61 74 20 6c 6f 6f 6b 73  tring that looks
346d0 20 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 29 0a   like a number).
346e0 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 76  ** then the conv
346f0 65 72 73 69 6f 6e 20 69 73 20 70 65 72 66 6f 72  ersion is perfor
34700 6d 65 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20  med.  Otherwise 
34710 6e 6f 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 63  no conversion oc
34720 63 75 72 73 2e 0a 2a 2a 20 54 68 65 20 5b 53 51  curs..** The [SQ
34730 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 20 64  LITE_INTEGER | d
34740 61 74 61 74 79 70 65 5d 20 61 66 74 65 72 20 63  atatype] after c
34750 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 72 65 74  onversion is ret
34760 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 50 6c 65  urned..**.** Ple
34770 61 73 65 20 70 61 79 20 70 61 72 74 69 63 75 6c  ase pay particul
34780 61 72 20 61 74 74 65 6e 74 69 6f 6e 20 74 6f 20  ar attention to 
34790 74 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68  the fact that th
347a0 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72 6e  e pointer return
347b0 65 64 0a 2a 2a 20 66 72 6f 6d 20 5b 73 71 6c 69  ed.** from [sqli
347c0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 29  te3_value_blob()
347d0 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  ], [sqlite3_valu
347e0 65 5f 74 65 78 74 28 29 5d 2c 20 6f 72 0a 2a 2a  e_text()], or.**
347f0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
34800 74 65 78 74 31 36 28 29 5d 20 63 61 6e 20 62 65  text16()] can be
34810 20 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20   invalidated by 
34820 61 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c  a subsequent cal
34830 6c 20 74 6f 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  l to.** [sqlite3
34840 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 29 5d 2c  _value_bytes()],
34850 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
34860 62 79 74 65 73 31 36 28 29 5d 2c 20 5b 73 71 6c  bytes16()], [sql
34870 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
34880 29 5d 2c 0a 2a 2a 20 6f 72 20 5b 73 71 6c 69 74  )],.** or [sqlit
34890 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 28  e3_value_text16(
348a0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  )]..**.** These 
348b0 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62 65  routines must be
348c0 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65   called from the
348d0 20 73 61 6d 65 20 74 68 72 65 61 64 20 61 73 0a   same thread as.
348e0 2a 2a 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  ** the SQL funct
348f0 69 6f 6e 20 74 68 61 74 20 73 75 70 70 6c 69 65  ion that supplie
34900 64 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  d the [sqlite3_v
34910 61 6c 75 65 2a 5d 20 70 61 72 61 6d 65 74 65 72  alue*] parameter
34920 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41  s..**.** INVARIA
34930 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31  NTS:.**.** {H151
34940 30 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  03} The [sqlite3
34950 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56 29 5d 20  _value_blob(V)] 
34960 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72  interface conver
34970 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ts the.**       
34980 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71     [protected sq
34990 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
349a0 65 63 74 20 56 20 69 6e 74 6f 20 61 20 42 4c 4f  ect V into a BLO
349b0 42 20 61 6e 64 20 74 68 65 6e 0a 2a 2a 20 20 20  B and then.**   
349c0 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61         returns a
349d0 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
349e0 63 6f 6e 76 65 72 74 65 64 20 76 61 6c 75 65 2e  converted value.
349f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 30 36 7d 20  .**.** {H15106} 
34a00 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c  The [sqlite3_val
34a10 75 65 5f 62 79 74 65 73 28 56 29 5d 20 69 6e 74  ue_bytes(V)] int
34a20 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74  erface returns t
34a30 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6e  he.**          n
34a40 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
34a50 6e 20 74 68 65 20 42 4c 4f 42 20 6f 72 20 73 74  n the BLOB or st
34a60 72 69 6e 67 20 28 65 78 63 6c 75 73 69 76 65 20  ring (exclusive 
34a70 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  of the.**       
34a80 20 20 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74     zero terminat
34a90 6f 72 20 6f 6e 20 74 68 65 20 73 74 72 69 6e 67  or on the string
34aa0 29 20 74 68 61 74 20 77 61 73 20 72 65 74 75 72  ) that was retur
34ab0 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 20 20  ned by the.**   
34ac0 20 20 20 20 20 20 20 6d 6f 73 74 20 72 65 63 65         most rece
34ad0 6e 74 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69  nt call to [sqli
34ae0 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 56  te3_value_blob(V
34af0 29 5d 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20  )] or.**        
34b00 20 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65    [sqlite3_value
34b10 5f 74 65 78 74 28 56 29 5d 2e 0a 2a 2a 0a 2a 2a  _text(V)]..**.**
34b20 20 7b 48 31 35 31 30 39 7d 20 54 68 65 20 5b 73   {H15109} The [s
34b30 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
34b40 65 73 31 36 28 56 29 5d 20 69 6e 74 65 72 66 61  es16(V)] interfa
34b50 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a  ce returns the.*
34b60 2a 20 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65  *          numbe
34b70 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
34b80 65 20 73 74 72 69 6e 67 20 28 65 78 63 6c 75 73  e string (exclus
34b90 69 76 65 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20  ive of the.**   
34ba0 20 20 20 20 20 20 20 7a 65 72 6f 20 74 65 72 6d         zero term
34bb0 69 6e 61 74 6f 72 20 6f 6e 20 74 68 65 20 73 74  inator on the st
34bc0 72 69 6e 67 29 20 74 68 61 74 20 77 61 73 20 72  ring) that was r
34bd0 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 0a 2a  eturned by the.*
34be0 2a 20 20 20 20 20 20 20 20 20 20 6d 6f 73 74 20  *          most 
34bf0 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f 20 5b  recent call to [
34c00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
34c10 78 74 31 36 28 56 29 5d 2c 0a 2a 2a 20 20 20 20  xt16(V)],.**    
34c20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76        [sqlite3_v
34c30 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28 56 29  alue_text16be(V)
34c40 5d 2c 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 76  ], or [sqlite3_v
34c50 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 56 29  alue_text16le(V)
34c60 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 32  ]..**.** {H15112
34c70 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 76  } The [sqlite3_v
34c80 61 6c 75 65 5f 64 6f 75 62 6c 65 28 56 29 5d 20  alue_double(V)] 
34c90 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76 65 72  interface conver
34ca0 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  ts the.**       
34cb0 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71     [protected sq
34cc0 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
34cd0 65 63 74 20 56 20 69 6e 74 6f 20 61 20 66 6c 6f  ect V into a flo
34ce0 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75  ating point valu
34cf0 65 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  e and.**        
34d00 20 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79    returns a copy
34d10 20 6f 66 20 74 68 61 74 20 76 61 6c 75 65 2e 0a   of that value..
34d20 2a 2a 0a 2a 2a 20 7b 48 31 35 31 31 35 7d 20 54  **.** {H15115} T
34d30 68 65 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75  he [sqlite3_valu
34d40 65 5f 69 6e 74 28 56 29 5d 20 69 6e 74 65 72 66  e_int(V)] interf
34d50 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68 65  ace converts the
34d60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72  .**          [pr
34d70 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f  otected sqlite3_
34d80 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20  value] object V 
34d90 69 6e 74 6f 20 61 20 36 34 2d 62 69 74 20 73 69  into a 64-bit si
34da0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 61 6e 64  gned integer and
34db0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
34dc0 75 72 6e 73 20 74 68 65 20 6c 6f 77 65 72 20 33  urns the lower 3
34dd0 32 20 62 69 74 73 20 6f 66 20 74 68 61 74 20 69  2 bits of that i
34de0 6e 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  nteger..**.** {H
34df0 31 35 31 31 38 7d 20 54 68 65 20 5b 73 71 6c 69  15118} The [sqli
34e00 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28  te3_value_int64(
34e10 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f  V)] interface co
34e20 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20  nverts the.**   
34e30 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65         [protecte
34e40 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
34e50 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61   object V into a
34e60 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
34e70 6e 74 65 67 65 72 20 61 6e 64 0a 2a 2a 20 20 20  nteger and.**   
34e80 20 20 20 20 20 20 20 72 65 74 75 72 6e 73 20 61         returns a
34e90 20 63 6f 70 79 20 6f 66 20 74 68 61 74 20 69 6e   copy of that in
34ea0 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  teger..**.** {H1
34eb0 35 31 32 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  5121} The [sqlit
34ec0 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 56 29  e3_value_text(V)
34ed0 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f 6e 76  ] interface conv
34ee0 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  erts the.**     
34ef0 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20       [protected 
34f00 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
34f10 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61 20 7a  bject V into a z
34f20 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64 20 55  ero-terminated U
34f30 54 46 2d 38 0a 2a 2a 20 20 20 20 20 20 20 20 20  TF-8.**         
34f40 20 73 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75   string and retu
34f50 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
34f60 20 74 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a   that string..**
34f70 0a 2a 2a 20 7b 48 31 35 31 32 34 7d 20 54 68 65  .** {H15124} The
34f80 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f   [sqlite3_value_
34f90 74 65 78 74 31 36 28 56 29 5d 20 69 6e 74 65 72  text16(V)] inter
34fa0 66 61 63 65 20 63 6f 6e 76 65 72 74 73 20 74 68  face converts th
34fb0 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70  e.**          [p
34fc0 72 6f 74 65 63 74 65 64 20 73 71 6c 69 74 65 33  rotected sqlite3
34fd0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56  _value] object V
34fe0 20 69 6e 74 6f 20 61 20 7a 65 72 6f 2d 74 65 72   into a zero-ter
34ff0 6d 69 6e 61 74 65 64 20 32 2d 62 79 74 65 0a 2a  minated 2-byte.*
35000 2a 20 20 20 20 20 20 20 20 20 20 61 6c 69 67 6e  *          align
35010 65 64 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65  ed UTF-16 native
35020 20 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20   byte order.**  
35030 20 20 20 20 20 20 20 20 73 74 72 69 6e 67 20 61          string a
35040 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
35050 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73 74 72  nter to that str
35060 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35 31  ing..**.** {H151
35070 32 37 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  27} The [sqlite3
35080 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 28  _value_text16be(
35090 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 6f  V)] interface co
350a0 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20 20 20  nverts the.**   
350b0 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63 74 65         [protecte
350c0 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
350d0 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f 20 61   object V into a
350e0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
350f0 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20 20 20   2-byte.**      
35100 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54 46 2d      aligned UTF-
35110 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a 2a  16 big-endian.**
35120 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e 67            string
35130 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70   and returns a p
35140 6f 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 73  ointer to that s
35150 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  tring..**.** {H1
35160 35 31 33 30 7d 20 54 68 65 20 5b 73 71 6c 69 74  5130} The [sqlit
35170 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
35180 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65 20  e(V)] interface 
35190 63 6f 6e 76 65 72 74 73 20 74 68 65 0a 2a 2a 20  converts the.** 
351a0 20 20 20 20 20 20 20 20 20 5b 70 72 6f 74 65 63           [protec
351b0 74 65 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ted sqlite3_valu
351c0 65 5d 20 6f 62 6a 65 63 74 20 56 20 69 6e 74 6f  e] object V into
351d0 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74   a zero-terminat
351e0 65 64 20 32 2d 62 79 74 65 0a 2a 2a 20 20 20 20  ed 2-byte.**    
351f0 20 20 20 20 20 20 61 6c 69 67 6e 65 64 20 55 54        aligned UT
35200 46 2d 31 36 20 6c 69 74 74 6c 65 2d 65 6e 64 69  F-16 little-endi
35210 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  an.**          s
35220 74 72 69 6e 67 20 61 6e 64 20 72 65 74 75 72 6e  tring and return
35230 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  s a pointer to t
35240 68 61 74 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  hat string..**.*
35250 2a 20 7b 48 31 35 31 33 33 7d 20 54 68 65 20 5b  * {H15133} The [
35260 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
35270 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61 63 65  pe(V)] interface
35280 20 72 65 74 75 72 6e 73 0a 2a 2a 20 20 20 20 20   returns.**     
35290 20 20 20 20 20 6f 6e 65 20 6f 66 20 5b 53 51 4c       one of [SQL
352a0 49 54 45 5f 4e 55 4c 4c 5d 2c 20 5b 53 51 4c 49  ITE_NULL], [SQLI
352b0 54 45 5f 49 4e 54 45 47 45 52 5d 2c 20 5b 53 51  TE_INTEGER], [SQ
352c0 4c 49 54 45 5f 46 4c 4f 41 54 5d 2c 0a 2a 2a 20  LITE_FLOAT],.** 
352d0 20 20 20 20 20 20 20 20 20 5b 53 51 4c 49 54 45           [SQLITE
352e0 5f 54 45 58 54 5d 2c 20 6f 72 20 5b 53 51 4c 49  _TEXT], or [SQLI
352f0 54 45 5f 42 4c 4f 42 5d 20 61 73 20 61 70 70 72  TE_BLOB] as appr
35300 6f 70 72 69 61 74 65 20 66 6f 72 0a 2a 2a 20 20  opriate for.**  
35310 20 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c          the [sql
35320 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a 65  ite3_value] obje
35330 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 35  ct V..**.** {H15
35340 31 33 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  136} The [sqlite
35350 33 5f 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63 5f  3_value_numeric_
35360 74 79 70 65 28 56 29 5d 20 69 6e 74 65 72 66 61  type(V)] interfa
35370 63 65 20 63 6f 6e 76 65 72 74 73 0a 2a 2a 20 20  ce converts.**  
35380 20 20 20 20 20 20 20 20 74 68 65 20 5b 70 72 6f          the [pro
35390 74 65 63 74 65 64 20 73 71 6c 69 74 65 33 5f 76  tected sqlite3_v
353a0 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 56 20 69  alue] object V i
353b0 6e 74 6f 20 65 69 74 68 65 72 20 61 6e 20 69 6e  nto either an in
353c0 74 65 67 65 72 20 6f 72 0a 2a 2a 20 20 20 20 20  teger or.**     
353d0 20 20 20 20 20 61 20 66 6c 6f 61 74 69 6e 67 20       a floating 
353e0 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 66 20 69  point value if i
353f0 74 20 63 61 6e 20 64 6f 20 73 6f 20 77 69 74 68  t can do so with
35400 6f 75 74 20 6c 6f 73 73 20 6f 66 0a 2a 2a 20 20  out loss of.**  
35410 20 20 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74          informat
35420 69 6f 6e 2c 20 61 6e 64 20 72 65 74 75 72 6e 73  ion, and returns
35430 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f   one of [SQLITE_
35440 4e 55 4c 4c 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  NULL],.**       
35450 20 20 20 5b 53 51 4c 49 54 45 5f 49 4e 54 45 47     [SQLITE_INTEG
35460 45 52 5d 2c 20 5b 53 51 4c 49 54 45 5f 46 4c 4f  ER], [SQLITE_FLO
35470 41 54 5d 2c 20 5b 53 51 4c 49 54 45 5f 54 45 58  AT], [SQLITE_TEX
35480 54 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20  T], or.**       
35490 20 20 20 5b 53 51 4c 49 54 45 5f 42 4c 4f 42 5d     [SQLITE_BLOB]
354a0 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   as appropriate 
354b0 66 6f 72 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  for the.**      
354c0 20 20 20 20 5b 70 72 6f 74 65 63 74 65 64 20 73      [protected s
354d0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62  qlite3_value] ob
354e0 6a 65 63 74 20 56 20 61 66 74 65 72 20 74 68 65  ject V after the
354f0 20 63 6f 6e 76 65 72 73 69 6f 6e 20 61 74 74 65   conversion atte
35500 6d 70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  mpt..*/.SQLITE_A
35510 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  PI const void *s
35520 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
35530 62 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  b(sqlite3_value*
35540 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
35550 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t sqlite3_value_
35560 62 79 74 65 73 28 73 71 6c 69 74 65 33 5f 76 61  bytes(sqlite3_va
35570 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
35580 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
35590 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71 6c 69  lue_bytes16(sqli
355a0 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c  te3_value*);.SQL
355b0 49 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73  ITE_API double s
355c0 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75  qlite3_value_dou
355d0 62 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ble(sqlite3_valu
355e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
355f0 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  int sqlite3_valu
35600 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76 61  e_int(sqlite3_va
35610 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
35620 49 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20  I sqlite3_int64 
35630 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
35640 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  t64(sqlite3_valu
35650 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
35660 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
35670 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76 61 6c  har *sqlite3_val
35680 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f  ue_text(sqlite3_
35690 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  value*);.SQLITE_
356a0 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  API const void *
356b0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
356c0 78 74 31 36 28 73 71 6c 69 74 65 33 5f 76 61 6c  xt16(sqlite3_val
356d0 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ue*);.SQLITE_API
356e0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
356f0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
35700 36 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  6le(sqlite3_valu
35710 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  e*);.SQLITE_API 
35720 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69  const void *sqli
35730 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36  te3_value_text16
35740 62 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  be(sqlite3_value
35750 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
35760 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
35770 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 76 61  _type(sqlite3_va
35780 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  lue*);.SQLITE_AP
35790 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
357a0 6c 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65  lue_numeric_type
357b0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29  (sqlite3_value*)
357c0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
357d0 46 3a 20 4f 62 74 61 69 6e 20 41 67 67 72 65 67  F: Obtain Aggreg
357e0 61 74 65 20 46 75 6e 63 74 69 6f 6e 20 43 6f 6e  ate Function Con
357f0 74 65 78 74 20 7b 48 31 36 32 31 30 7d 20 3c 53  text {H16210} <S
35800 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65  20200>.**.** The
35810 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
35820 6f 66 20 61 67 67 72 65 67 61 74 65 20 53 51 4c  of aggregate SQL
35830 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 20 74   functions use t
35840 68 69 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 61  his routine to a
35850 6c 6c 6f 63 61 74 65 0a 2a 2a 20 61 20 73 74 72  llocate.** a str
35860 75 63 74 75 72 65 20 66 6f 72 20 73 74 6f 72 69  ucture for stori
35870 6e 67 20 74 68 65 69 72 20 73 74 61 74 65 2e 0a  ng their state..
35880 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
35890 74 69 6d 65 20 74 68 65 20 73 71 6c 69 74 65 33  time the sqlite3
358a0 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65  _aggregate_conte
358b0 78 74 28 29 20 72 6f 75 74 69 6e 65 20 69 73 20  xt() routine is 
358c0 63 61 6c 6c 65 64 20 66 6f 72 20 61 0a 2a 2a 20  called for a.** 
358d0 70 61 72 74 69 63 75 6c 61 72 20 61 67 67 72 65  particular aggre
358e0 67 61 74 65 2c 20 53 51 4c 69 74 65 20 61 6c 6c  gate, SQLite all
358f0 6f 63 61 74 65 73 20 6e 42 79 74 65 73 20 6f 66  ocates nBytes of
35900 20 6d 65 6d 6f 72 79 2c 20 7a 65 72 6f 65 73 20   memory, zeroes 
35910 6f 75 74 20 74 68 61 74 0a 2a 2a 20 6d 65 6d 6f  out that.** memo
35920 72 79 2c 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ry, and returns 
35930 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
35940 20 4f 6e 20 73 65 63 6f 6e 64 20 61 6e 64 20 73   On second and s
35950 75 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20  ubsequent calls 
35960 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 61 67  to.** sqlite3_ag
35970 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
35980 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20 61  ) for the same a
35990 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
359a0 6e 20 69 6e 64 65 78 2c 0a 2a 2a 20 74 68 65 20  n index,.** the 
359b0 73 61 6d 65 20 62 75 66 66 65 72 20 69 73 20 72  same buffer is r
359c0 65 74 75 72 6e 65 64 2e 20 54 68 65 20 69 6d 70  eturned. The imp
359d0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
359e0 68 65 20 61 67 67 72 65 67 61 74 65 20 63 61 6e  he aggregate can
359f0 20 75 73 65 0a 2a 2a 20 74 68 65 20 72 65 74 75   use.** the retu
35a00 72 6e 65 64 20 62 75 66 66 65 72 20 74 6f 20 61  rned buffer to a
35a10 63 63 75 6d 75 6c 61 74 65 20 64 61 74 61 2e 0a  ccumulate data..
35a20 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 61 75 74  **.** SQLite aut
35a30 6f 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 73  omatically frees
35a40 20 74 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 62   the allocated b
35a50 75 66 66 65 72 20 77 68 65 6e 20 74 68 65 20 61  uffer when the a
35a60 67 67 72 65 67 61 74 65 0a 2a 2a 20 71 75 65 72  ggregate.** quer
35a70 79 20 63 6f 6e 63 6c 75 64 65 73 2e 0a 2a 2a 0a  y concludes..**.
35a80 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70 61 72  ** The first par
35a90 61 6d 65 74 65 72 20 73 68 6f 75 6c 64 20 62 65  ameter should be
35aa0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
35ab0 2a 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  * [sqlite3_conte
35ac0 78 74 20 7c 20 53 51 4c 20 66 75 6e 63 74 69 6f  xt | SQL functio
35ad0 6e 20 63 6f 6e 74 65 78 74 5d 20 74 68 61 74 20  n context] that 
35ae0 69 73 20 74 68 65 20 66 69 72 73 74 20 70 61 72  is the first par
35af0 61 6d 65 74 65 72 0a 2a 2a 20 74 6f 20 74 68 65  ameter.** to the
35b00 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e   callback routin
35b10 65 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  e that implement
35b20 73 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20  s the aggregate 
35b30 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  function..**.** 
35b40 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 75 73  This routine mus
35b50 74 20 62 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d  t be called from
35b60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
35b70 20 69 6e 20 77 68 69 63 68 0a 2a 2a 20 74 68 65   in which.** the
35b80 20 61 67 67 72 65 67 61 74 65 20 53 51 4c 20 66   aggregate SQL f
35b90 75 6e 63 74 69 6f 6e 20 69 73 20 72 75 6e 6e 69  unction is runni
35ba0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  ng..**.** INVARI
35bb0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  ANTS:.**.** {H16
35bc0 32 31 31 7d 20 54 68 65 20 66 69 72 73 74 20 69  211} The first i
35bd0 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71  nvocation of [sq
35be0 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
35bf0 63 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 66 6f  context(C,N)] fo
35c00 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20  r.**          a 
35c10 70 61 72 74 69 63 75 6c 61 72 20 69 6e 73 74 61  particular insta
35c20 6e 63 65 20 6f 66 20 61 6e 20 61 67 67 72 65 67  nce of an aggreg
35c30 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f  ate function (fo
35c40 72 20 61 20 70 61 72 74 69 63 75 6c 61 72 0a 2a  r a particular.*
35c50 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65  *          conte
35c60 78 74 20 43 29 20 63 61 75 73 65 73 20 53 51 4c  xt C) causes SQL
35c70 69 74 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20  ite to allocate 
35c80 4e 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  N bytes of memor
35c90 79 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 7a  y,.**          z
35ca0 65 72 6f 20 74 68 61 74 20 6d 65 6d 6f 72 79 2c  ero that memory,
35cb0 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 70 6f   and return a po
35cc0 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61 6c 6c  inter to the all
35cd0 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a  ocated memory..*
35ce0 2a 0a 2a 2a 20 7b 48 31 36 32 31 33 7d 20 49 66  *.** {H16213} If
35cf0 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
35d00 74 69 6f 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  tion error occur
35d10 73 20 64 75 72 69 6e 67 0a 2a 2a 20 20 20 20 20  s during.**     
35d20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67       [sqlite3_ag
35d30 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28  gregate_context(
35d40 43 2c 4e 29 5d 20 74 68 65 6e 20 74 68 65 20 66  C,N)] then the f
35d50 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
35d60 30 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 31 35  0..**.** {H16215
35d70 7d 20 53 65 63 6f 6e 64 20 61 6e 64 20 73 75 62  } Second and sub
35d80 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74 69  sequent invocati
35d90 6f 6e 73 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20  ons of.**       
35da0 20 20 20 5b 73 71 6c 69 74 65 33 5f 61 67 67 72     [sqlite3_aggr
35db0 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 43 2c  egate_context(C,
35dc0 4e 29 5d 20 66 6f 72 20 74 68 65 20 73 61 6d 65  N)] for the same
35dd0 20 63 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72   context pointer
35de0 20 43 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69   C.**          i
35df0 67 6e 6f 72 65 20 74 68 65 20 4e 20 70 61 72 61  gnore the N para
35e00 6d 65 74 65 72 20 61 6e 64 20 72 65 74 75 72 6e  meter and return
35e10 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
35e20 65 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20  e same.**       
35e30 20 20 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f     block of memo
35e40 72 79 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  ry returned by t
35e50 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74  he first invocat
35e60 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32  ion..**.** {H162
35e70 31 37 7d 20 54 68 65 20 6d 65 6d 6f 72 79 20 61  17} The memory a
35e80 6c 6c 6f 63 61 74 65 64 20 62 79 20 5b 73 71 6c  llocated by [sql
35e90 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
35ea0 6f 6e 74 65 78 74 28 43 2c 4e 29 5d 20 69 73 0a  ontext(C,N)] is.
35eb0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 75 74 6f  **          auto
35ec0 6d 61 74 69 63 61 6c 6c 79 20 66 72 65 65 64 20  matically freed 
35ed0 6f 6e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  on the next call
35ee0 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   to [sqlite3_res
35ef0 65 74 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  et()].**        
35f00 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 66 69    or [sqlite3_fi
35f10 6e 61 6c 69 7a 65 28 29 5d 20 66 6f 72 20 74 68  nalize()] for th
35f20 65 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  e [prepared stat
35f30 65 6d 65 6e 74 5d 20 63 6f 6e 74 61 69 6e 69 6e  ement] containin
35f40 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  g.**          th
35f50 65 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  e aggregate func
35f60 74 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20  tion associated 
35f70 77 69 74 68 20 63 6f 6e 74 65 78 74 20 43 2e 0a  with context C..
35f80 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
35f90 69 64 20 2a 73 71 6c 69 74 65 33 5f 61 67 67 72  id *sqlite3_aggr
35fa0 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 73 71  egate_context(sq
35fb0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
35fc0 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 0a 2f 2a  int nBytes);../*
35fd0 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 55 73  .** CAPI3REF: Us
35fe0 65 72 20 44 61 74 61 20 46 6f 72 20 46 75 6e 63  er Data For Func
35ff0 74 69 6f 6e 73 20 7b 48 31 36 32 34 30 7d 20 3c  tions {H16240} <
36000 53 32 30 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20200>.**.** Th
36010 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  e sqlite3_user_d
36020 61 74 61 28 29 20 69 6e 74 65 72 66 61 63 65 20  ata() interface 
36030 72 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f  returns a copy o
36040 66 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72  f.** the pointer
36050 20 74 68 61 74 20 77 61 73 20 74 68 65 20 70 55   that was the pU
36060 73 65 72 44 61 74 61 20 70 61 72 61 6d 65 74 65  serData paramete
36070 72 20 28 74 68 65 20 35 74 68 20 70 61 72 61 6d  r (the 5th param
36080 65 74 65 72 29 0a 2a 2a 20 6f 66 20 74 68 65 20  eter).** of the 
36090 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
360a0 66 75 6e 63 74 69 6f 6e 28 29 5d 0a 2a 2a 20 61  function()].** a
360b0 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61  nd [sqlite3_crea
360c0 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 29 5d  te_function16()]
360d0 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20 6f   routines that o
360e0 72 69 67 69 6e 61 6c 6c 79 0a 2a 2a 20 72 65 67  riginally.** reg
360f0 69 73 74 65 72 65 64 20 74 68 65 20 61 70 70 6c  istered the appl
36100 69 63 61 74 69 6f 6e 20 64 65 66 69 6e 65 64 20  ication defined 
36110 66 75 6e 63 74 69 6f 6e 2e 20 7b 45 4e 44 7d 0a  function. {END}.
36120 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  **.** This routi
36130 6e 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  ne must be calle
36140 64 20 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 20  d from the same 
36150 74 68 72 65 61 64 20 69 6e 20 77 68 69 63 68 0a  thread in which.
36160 2a 2a 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ** the applicati
36170 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on-defined funct
36180 69 6f 6e 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a  ion is running..
36190 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
361a0 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32 34 33 7d  :.**.** {H16243}
361b0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 73   The [sqlite3_us
361c0 65 72 5f 64 61 74 61 28 43 29 5d 20 69 6e 74 65  er_data(C)] inte
361d0 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 61 20  rface returns a 
361e0 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20  copy of the.**  
361f0 20 20 20 20 20 20 20 20 50 20 70 6f 69 6e 74 65          P pointe
36200 72 20 66 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69  r from the [sqli
36210 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
36220 69 6f 6e 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c  ion(D,X,N,E,P,F,
36230 53 2c 4c 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20  S,L)].**        
36240 20 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 72    or [sqlite3_cr
36250 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
36260 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29  D,X,N,E,P,F,S,L)
36270 5d 20 63 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20  ] call that.**  
36280 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72          register
36290 65 64 20 74 68 65 20 53 51 4c 20 66 75 6e 63 74  ed the SQL funct
362a0 69 6f 6e 20 61 73 73 6f 63 69 61 74 65 64 20 77  ion associated w
362b0 69 74 68 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e  ith [sqlite3_con
362c0 74 65 78 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49  text] C..*/.SQLI
362d0 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
362e0 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61 28 73  ite3_user_data(s
362f0 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
36300 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
36310 46 3a 20 44 61 74 61 62 61 73 65 20 43 6f 6e 6e  F: Database Conn
36320 65 63 74 69 6f 6e 20 46 6f 72 20 46 75 6e 63 74  ection For Funct
36330 69 6f 6e 73 20 7b 48 31 36 32 35 30 7d 20 3c 53  ions {H16250} <S
36340 36 30 36 30 30 3e 3c 53 32 30 32 30 30 3e 0a 2a  60600><S20200>.*
36350 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
36360 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
36370 6c 65 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  le() interface r
36380 65 74 75 72 6e 73 20 61 20 63 6f 70 79 20 6f 66  eturns a copy of
36390 0a 2a 2a 20 74 68 65 20 70 6f 69 6e 74 65 72 20  .** the pointer 
363a0 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61 73 65  to the [database
363b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 28 74 68   connection] (th
363c0 65 20 31 73 74 20 70 61 72 61 6d 65 74 65 72 29  e 1st parameter)
363d0 0a 2a 2a 20 6f 66 20 74 68 65 20 5b 73 71 6c 69  .** of the [sqli
363e0 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
363f0 69 6f 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 5b 73  ion()].** and [s
36400 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
36410 6e 63 74 69 6f 6e 31 36 28 29 5d 20 72 6f 75 74  nction16()] rout
36420 69 6e 65 73 20 74 68 61 74 20 6f 72 69 67 69 6e  ines that origin
36430 61 6c 6c 79 0a 2a 2a 20 72 65 67 69 73 74 65 72  ally.** register
36440 65 64 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  ed the applicati
36450 6f 6e 20 64 65 66 69 6e 65 64 20 66 75 6e 63 74  on defined funct
36460 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52  ion..**.** INVAR
36470 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31  IANTS:.**.** {H1
36480 36 32 35 33 7d 20 54 68 65 20 5b 73 71 6c 69 74  6253} The [sqlit
36490 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
364a0 6e 64 6c 65 28 43 29 5d 20 69 6e 74 65 72 66 61  ndle(C)] interfa
364b0 63 65 20 72 65 74 75 72 6e 73 20 61 20 63 6f 70  ce returns a cop
364c0 79 20 6f 66 20 74 68 65 0a 2a 2a 20 20 20 20 20  y of the.**     
364d0 20 20 20 20 20 44 20 70 6f 69 6e 74 65 72 20 66       D pointer f
364e0 72 6f 6d 20 74 68 65 20 5b 73 71 6c 69 74 65 33  rom the [sqlite3
364f0 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
36500 28 44 2c 58 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c  (D,X,N,E,P,F,S,L
36510 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  )].**          o
36520 72 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  r [sqlite3_creat
36530 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28 44 2c 58  e_function16(D,X
36540 2c 4e 2c 45 2c 50 2c 46 2c 53 2c 4c 29 5d 20 63  ,N,E,P,F,S,L)] c
36550 61 6c 6c 20 74 68 61 74 0a 2a 2a 20 20 20 20 20  all that.**     
36560 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64 20       registered 
36570 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
36580 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
36590 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78   [sqlite3_contex
365a0 74 5d 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  t] C..*/.SQLITE_
365b0 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  API sqlite3 *sql
365c0 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f  ite3_context_db_
365d0 68 61 6e 64 6c 65 28 73 71 6c 69 74 65 33 5f 63  handle(sqlite3_c
365e0 6f 6e 74 65 78 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a  ontext*);../*.**
365f0 20 43 41 50 49 33 52 45 46 3a 20 46 75 6e 63 74   CAPI3REF: Funct
36600 69 6f 6e 20 41 75 78 69 6c 69 61 72 79 20 44 61  ion Auxiliary Da
36610 74 61 20 7b 48 31 36 32 37 30 7d 20 3c 53 32 30  ta {H16270} <S20
36620 32 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  200>.**.** The f
36630 6f 6c 6c 6f 77 69 6e 67 20 74 77 6f 20 66 75 6e  ollowing two fun
36640 63 74 69 6f 6e 73 20 6d 61 79 20 62 65 20 75 73  ctions may be us
36650 65 64 20 62 79 20 73 63 61 6c 61 72 20 53 51 4c  ed by scalar SQL
36660 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 0a 2a 2a   functions to.**
36670 20 61 73 73 6f 63 69 61 74 65 20 6d 65 74 61 64   associate metad
36680 61 74 61 20 77 69 74 68 20 61 72 67 75 6d 65 6e  ata with argumen
36690 74 20 76 61 6c 75 65 73 2e 20 49 66 20 74 68 65  t values. If the
366a0 20 73 61 6d 65 20 76 61 6c 75 65 20 69 73 20 70   same value is p
366b0 61 73 73 65 64 20 74 6f 0a 2a 2a 20 6d 75 6c 74  assed to.** mult
366c0 69 70 6c 65 20 69 6e 76 6f 63 61 74 69 6f 6e 73  iple invocations
366d0 20 6f 66 20 74 68 65 20 73 61 6d 65 20 53 51 4c   of the same SQL
366e0 20 66 75 6e 63 74 69 6f 6e 20 64 75 72 69 6e 67   function during
366f0 20 71 75 65 72 79 20 65 78 65 63 75 74 69 6f 6e   query execution
36700 2c 20 75 6e 64 65 72 0a 2a 2a 20 73 6f 6d 65 20  , under.** some 
36710 63 69 72 63 75 6d 73 74 61 6e 63 65 73 20 74 68  circumstances th
36720 65 20 61 73 73 6f 63 69 61 74 65 64 20 6d 65 74  e associated met
36730 61 64 61 74 61 20 6d 61 79 20 62 65 20 70 72 65  adata may be pre
36740 73 65 72 76 65 64 2e 20 54 68 69 73 20 6d 61 79  served. This may
36750 0a 2a 2a 20 62 65 20 75 73 65 64 2c 20 66 6f 72  .** be used, for
36760 20 65 78 61 6d 70 6c 65 2c 20 74 6f 20 61 64 64   example, to add
36770 20 61 20 72 65 67 75 6c 61 72 2d 65 78 70 72 65   a regular-expre
36780 73 73 69 6f 6e 20 6d 61 74 63 68 69 6e 67 20 73  ssion matching s
36790 63 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f  calar.** functio
367a0 6e 2e 20 54 68 65 20 63 6f 6d 70 69 6c 65 64 20  n. The compiled 
367b0 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 20 72  version of the r
367c0 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
367d0 6e 20 69 73 20 73 74 6f 72 65 64 20 61 73 0a 2a  n is stored as.*
367e0 2a 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63  * metadata assoc
367f0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 53  iated with the S
36800 51 4c 20 76 61 6c 75 65 20 70 61 73 73 65 64 20  QL value passed 
36810 61 73 20 74 68 65 20 72 65 67 75 6c 61 72 20 65  as the regular e
36820 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 70 61 74  xpression.** pat
36830 74 65 72 6e 2e 20 20 54 68 65 20 63 6f 6d 70 69  tern.  The compi
36840 6c 65 64 20 72 65 67 75 6c 61 72 20 65 78 70 72  led regular expr
36850 65 73 73 69 6f 6e 20 63 61 6e 20 62 65 20 72 65  ession can be re
36860 75 73 65 64 20 6f 6e 20 6d 75 6c 74 69 70 6c 65  used on multiple
36870 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20  .** invocations 
36880 6f 66 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63  of the same func
36890 74 69 6f 6e 20 73 6f 20 74 68 61 74 20 74 68 65  tion so that the
368a0 20 6f 72 69 67 69 6e 61 6c 20 70 61 74 74 65 72   original patter
368b0 6e 20 73 74 72 69 6e 67 0a 2a 2a 20 64 6f 65 73  n string.** does
368c0 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20   not need to be 
368d0 72 65 63 6f 6d 70 69 6c 65 64 20 6f 6e 20 65 61  recompiled on ea
368e0 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e 2e 0a 2a  ch invocation..*
368f0 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
36900 5f 67 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _get_auxdata() i
36910 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
36920 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
36930 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 61 73  e metadata.** as
36940 73 6f 63 69 61 74 65 64 20 62 79 20 74 68 65 20  sociated by the 
36950 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
36960 61 74 61 28 29 20 66 75 6e 63 74 69 6f 6e 20 77  ata() function w
36970 69 74 68 20 74 68 65 20 4e 74 68 20 61 72 67 75  ith the Nth argu
36980 6d 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 20 74 6f  ment.** value to
36990 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
369a0 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
369b0 6e 2e 20 49 66 20 6e 6f 20 6d 65 74 61 64 61 74  n. If no metadat
369c0 61 20 68 61 73 20 62 65 65 6e 20 65 76 65 72 0a  a has been ever.
369d0 2a 2a 20 62 65 65 6e 20 73 65 74 20 66 6f 72 20  ** been set for 
369e0 74 68 65 20 4e 74 68 20 61 72 67 75 6d 65 6e 74  the Nth argument
369f0 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   of the function
36a00 2c 20 6f 72 20 69 66 20 74 68 65 20 63 6f 72 72  , or if the corr
36a10 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 66 75 6e  esponding.** fun
36a20 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
36a30 68 61 73 20 63 68 61 6e 67 65 64 20 73 69 6e 63  has changed sinc
36a40 65 20 74 68 65 20 6d 65 74 61 2d 64 61 74 61 20  e the meta-data 
36a50 77 61 73 20 73 65 74 2c 0a 2a 2a 20 74 68 65 6e  was set,.** then
36a60 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 78   sqlite3_get_aux
36a70 64 61 74 61 28 29 20 72 65 74 75 72 6e 73 20 61  data() returns a
36a80 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a   NULL pointer..*
36a90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
36aa0 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 69  _set_auxdata() i
36ab0 6e 74 65 72 66 61 63 65 20 73 61 76 65 73 20 74  nterface saves t
36ac0 68 65 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 70  he metadata.** p
36ad0 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 69 74 73  ointed to by its
36ae0 20 33 72 64 20 70 61 72 61 6d 65 74 65 72 20 61   3rd parameter a
36af0 73 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 66  s the metadata f
36b00 6f 72 20 74 68 65 20 4e 2d 74 68 0a 2a 2a 20 61  or the N-th.** a
36b10 72 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 61  rgument of the a
36b20 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
36b30 65 64 20 66 75 6e 63 74 69 6f 6e 2e 20 20 53 75  ed function.  Su
36b40 62 73 65 71 75 65 6e 74 0a 2a 2a 20 63 61 6c 6c  bsequent.** call
36b50 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 67 65 74  s to sqlite3_get
36b60 5f 61 75 78 64 61 74 61 28 29 20 6d 69 67 68 74  _auxdata() might
36b70 20 72 65 74 75 72 6e 20 74 68 69 73 20 64 61 74   return this dat
36b80 61 2c 20 69 66 20 69 74 20 68 61 73 0a 2a 2a 20  a, if it has.** 
36b90 6e 6f 74 20 62 65 65 6e 20 64 65 73 74 72 6f 79  not been destroy
36ba0 65 64 2e 0a 2a 2a 20 49 66 20 69 74 20 69 73 20  ed..** If it is 
36bb0 6e 6f 74 20 4e 55 4c 4c 2c 20 53 51 4c 69 74 65  not NULL, SQLite
36bc0 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65   will invoke the
36bd0 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20 66   destructor.** f
36be0 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 62 79  unction given by
36bf0 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
36c00 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f 73 65  er to sqlite3_se
36c10 74 5f 61 75 78 64 61 74 61 28 29 20 6f 6e 0a 2a  t_auxdata() on.*
36c20 2a 20 74 68 65 20 6d 65 74 61 64 61 74 61 20 77  * the metadata w
36c30 68 65 6e 20 74 68 65 20 63 6f 72 72 65 73 70 6f  hen the correspo
36c40 6e 64 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70  nding function p
36c50 61 72 61 6d 65 74 65 72 20 63 68 61 6e 67 65 73  arameter changes
36c60 0a 2a 2a 20 6f 72 20 77 68 65 6e 20 74 68 65 20  .** or when the 
36c70 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 63 6f  SQL statement co
36c80 6d 70 6c 65 74 65 73 2c 20 77 68 69 63 68 65 76  mpletes, whichev
36c90 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74 2e 0a  er comes first..
36ca0 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 69 73 20  **.** SQLite is 
36cb0 66 72 65 65 20 74 6f 20 63 61 6c 6c 20 74 68 65  free to call the
36cc0 20 64 65 73 74 72 75 63 74 6f 72 20 61 6e 64 20   destructor and 
36cd0 64 72 6f 70 20 6d 65 74 61 64 61 74 61 20 6f 6e  drop metadata on
36ce0 20 61 6e 79 0a 2a 2a 20 70 61 72 61 6d 65 74 65   any.** paramete
36cf0 72 20 6f 66 20 61 6e 79 20 66 75 6e 63 74 69 6f  r of any functio
36d00 6e 20 61 74 20 61 6e 79 20 74 69 6d 65 2e 20 20  n at any time.  
36d10 54 68 65 20 6f 6e 6c 79 20 67 75 61 72 61 6e 74  The only guarant
36d20 65 65 20 69 73 20 74 68 61 74 0a 2a 2a 20 74 68  ee is that.** th
36d30 65 20 64 65 73 74 72 75 63 74 6f 72 20 77 69 6c  e destructor wil
36d40 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 65 66 6f  l be called befo
36d50 72 65 20 74 68 65 20 6d 65 74 61 64 61 74 61 20  re the metadata 
36d60 69 73 20 64 72 6f 70 70 65 64 2e 0a 2a 2a 0a 2a  is dropped..**.*
36d70 2a 20 49 6e 20 70 72 61 63 74 69 63 65 2c 20 6d  * In practice, m
36d80 65 74 61 64 61 74 61 20 69 73 20 70 72 65 73 65  etadata is prese
36d90 72 76 65 64 20 62 65 74 77 65 65 6e 20 66 75 6e  rved between fun
36da0 63 74 69 6f 6e 20 63 61 6c 6c 73 20 66 6f 72 0a  ction calls for.
36db0 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74  ** expressions t
36dc0 68 61 74 20 61 72 65 20 63 6f 6e 73 74 61 6e 74  hat are constant
36dd0 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69 6d 65   at compile time
36de0 2e 20 54 68 69 73 20 69 6e 63 6c 75 64 65 73 20  . This includes 
36df0 6c 69 74 65 72 61 6c 0a 2a 2a 20 76 61 6c 75 65  literal.** value
36e00 73 20 61 6e 64 20 53 51 4c 20 76 61 72 69 61 62  s and SQL variab
36e10 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  les..**.** These
36e20 20 72 6f 75 74 69 6e 65 73 20 6d 75 73 74 20 62   routines must b
36e30 65 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68  e called from th
36e40 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e  e same thread in
36e50 20 77 68 69 63 68 0a 2a 2a 20 74 68 65 20 53 51   which.** the SQ
36e60 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 75  L function is ru
36e70 6e 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  nning..**.** INV
36e80 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
36e90 48 31 36 32 37 32 7d 20 54 68 65 20 5b 73 71 6c  H16272} The [sql
36ea0 69 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61  ite3_get_auxdata
36eb0 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  (C,N)] interface
36ec0 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
36ed0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  er.**          t
36ee0 6f 20 6d 65 74 61 64 61 74 61 20 61 73 73 6f 63  o metadata assoc
36ef0 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 4e  iated with the N
36f00 74 68 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  th parameter of 
36f10 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
36f20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 6f  .**          who
36f30 73 65 20 63 6f 6e 74 65 78 74 20 69 73 20 43 2c  se context is C,
36f40 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72   or NULL if ther
36f50 65 20 69 73 20 6e 6f 20 6d 65 74 61 64 61 74 61  e is no metadata
36f60 20 61 73 73 6f 63 69 61 74 65 64 0a 2a 2a 20 20   associated.**  
36f70 20 20 20 20 20 20 20 20 77 69 74 68 20 74 68 61          with tha
36f80 74 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a  t parameter..**.
36f90 2a 2a 20 7b 48 31 36 32 37 34 7d 20 54 68 65 20  ** {H16274} The 
36fa0 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78  [sqlite3_set_aux
36fb0 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20 69  data(C,N,P,D)] i
36fc0 6e 74 65 72 66 61 63 65 20 61 73 73 69 67 6e 73  nterface assigns
36fd0 20 61 20 6d 65 74 61 64 61 74 61 0a 2a 2a 20 20   a metadata.**  
36fe0 20 20 20 20 20 20 20 20 70 6f 69 6e 74 65 72 20          pointer 
36ff0 50 20 74 6f 20 74 68 65 20 4e 74 68 20 70 61 72  P to the Nth par
37000 61 6d 65 74 65 72 20 6f 66 20 74 68 65 20 53 51  ameter of the SQ
37010 4c 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  L function with 
37020 63 6f 6e 74 65 78 74 20 43 2e 0a 2a 2a 0a 2a 2a  context C..**.**
37030 20 7b 48 31 36 32 37 36 7d 20 53 51 4c 69 74 65   {H16276} SQLite
37040 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65   will invoke the
37050 20 64 65 73 74 72 75 63 74 6f 72 20 44 20 77 69   destructor D wi
37060 74 68 20 61 20 73 69 6e 67 6c 65 20 61 72 67 75  th a single argu
37070 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  ment.**         
37080 20 77 68 69 63 68 20 69 73 20 74 68 65 20 6d 65   which is the me
37090 74 61 64 61 74 61 20 70 6f 69 6e 74 65 72 20 50  tadata pointer P
370a0 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 63 61 6c   following a cal
370b0 6c 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  l to.**         
370c0 20 5b 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75   [sqlite3_set_au
370d0 78 64 61 74 61 28 43 2c 4e 2c 50 2c 44 29 5d 20  xdata(C,N,P,D)] 
370e0 77 68 65 6e 20 53 51 4c 69 74 65 20 63 65 61 73  when SQLite ceas
370f0 65 73 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 20 20  es to hold.**   
37100 20 20 20 20 20 20 20 74 68 65 20 6d 65 74 61 64         the metad
37110 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 32  ata..**.** {H162
37120 37 37 7d 20 53 51 4c 69 74 65 20 63 65 61 73 65  77} SQLite cease
37130 73 20 74 6f 20 68 6f 6c 64 20 6d 65 74 61 64 61  s to hold metada
37140 74 61 20 66 6f 72 20 61 6e 20 53 51 4c 20 66 75  ta for an SQL fu
37150 6e 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72  nction parameter
37160 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 68 65  .**          whe
37170 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  n the value of t
37180 68 61 74 20 70 61 72 61 6d 65 74 65 72 20 63 68  hat parameter ch
37190 61 6e 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  anges..**.** {H1
371a0 36 32 37 38 7d 20 57 68 65 6e 20 5b 73 71 6c 69  6278} When [sqli
371b0 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28  te3_set_auxdata(
371c0 43 2c 4e 2c 50 2c 44 29 5d 20 69 73 20 69 6e 76  C,N,P,D)] is inv
371d0 6f 6b 65 64 2c 20 74 68 65 20 64 65 73 74 72 75  oked, the destru
371e0 63 74 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ctor.**         
371f0 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20 61   is called for a
37200 6e 79 20 70 72 69 6f 72 20 6d 65 74 61 64 61 74  ny prior metadat
37210 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
37220 68 20 74 68 65 20 73 61 6d 65 20 66 75 6e 63 74  h the same funct
37230 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ion.**          
37240 63 6f 6e 74 65 78 74 20 43 20 61 6e 64 20 70 61  context C and pa
37250 72 61 6d 65 74 65 72 20 4e 2e 0a 2a 2a 0a 2a 2a  rameter N..**.**
37260 20 7b 48 31 36 32 37 39 7d 20 53 51 4c 69 74 65   {H16279} SQLite
37270 20 77 69 6c 6c 20 63 61 6c 6c 20 64 65 73 74 72   will call destr
37280 75 63 74 6f 72 73 20 66 6f 72 20 61 6e 79 20 6d  uctors for any m
37290 65 74 61 64 61 74 61 20 69 74 20 69 73 20 68 6f  etadata it is ho
372a0 6c 64 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20  lding.**        
372b0 20 20 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61    in a particula
372c0 72 20 5b 70 72 65 70 61 72 65 64 20 73 74 61 74  r [prepared stat
372d0 65 6d 65 6e 74 5d 20 53 20 77 68 65 6e 20 65 69  ement] S when ei
372e0 74 68 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ther.**         
372f0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28   [sqlite3_reset(
37300 53 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f  S)] or [sqlite3_
37310 66 69 6e 61 6c 69 7a 65 28 53 29 5d 20 69 73 20  finalize(S)] is 
37320 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  called..*/.SQLIT
37330 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
37340 74 65 33 5f 67 65 74 5f 61 75 78 64 61 74 61 28  te3_get_auxdata(
37350 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
37360 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45  , int N);.SQLITE
37370 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
37380 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 73 71  3_set_auxdata(sq
37390 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
373a0 69 6e 74 20 4e 2c 20 76 6f 69 64 2a 2c 20 76 6f  int N, void*, vo
373b0 69 64 20 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a  id (*)(void*));.
373c0 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
373d0 3a 20 43 6f 6e 73 74 61 6e 74 73 20 44 65 66 69  : Constants Defi
373e0 6e 69 6e 67 20 53 70 65 63 69 61 6c 20 44 65 73  ning Special Des
373f0 74 72 75 63 74 6f 72 20 42 65 68 61 76 69 6f 72  tructor Behavior
37400 20 7b 48 31 30 32 38 30 7d 20 3c 53 33 30 31 30   {H10280} <S3010
37410 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 61  0>.**.** These a
37420 72 65 20 73 70 65 63 69 61 6c 20 76 61 6c 75 65  re special value
37430 73 20 66 6f 72 20 74 68 65 20 64 65 73 74 72 75  s for the destru
37440 63 74 6f 72 20 74 68 61 74 20 69 73 20 70 61 73  ctor that is pas
37450 73 65 64 20 69 6e 20 61 73 20 74 68 65 0a 2a 2a  sed in as the.**
37460 20 66 69 6e 61 6c 20 61 72 67 75 6d 65 6e 74 20   final argument 
37470 74 6f 20 72 6f 75 74 69 6e 65 73 20 6c 69 6b 65  to routines like
37480 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
37490 5f 62 6c 6f 62 28 29 5d 2e 20 20 49 66 20 74 68  _blob()].  If th
374a0 65 20 64 65 73 74 72 75 63 74 6f 72 0a 2a 2a 20  e destructor.** 
374b0 61 72 67 75 6d 65 6e 74 20 69 73 20 53 51 4c 49  argument is SQLI
374c0 54 45 5f 53 54 41 54 49 43 2c 20 69 74 20 6d 65  TE_STATIC, it me
374d0 61 6e 73 20 74 68 61 74 20 74 68 65 20 63 6f 6e  ans that the con
374e0 74 65 6e 74 20 70 6f 69 6e 74 65 72 20 69 73 20  tent pointer is 
374f0 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61 6e 64 20  constant.** and 
37500 77 69 6c 6c 20 6e 65 76 65 72 20 63 68 61 6e 67  will never chang
37510 65 2e 20 20 49 74 20 64 6f 65 73 20 6e 6f 74 20  e.  It does not 
37520 6e 65 65 64 20 74 6f 20 62 65 20 64 65 73 74 72  need to be destr
37530 6f 79 65 64 2e 20 20 54 68 65 0a 2a 2a 20 53 51  oyed.  The.** SQ
37540 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 20 76  LITE_TRANSIENT v
37550 61 6c 75 65 20 6d 65 61 6e 73 20 74 68 61 74 20  alue means that 
37560 74 68 65 20 63 6f 6e 74 65 6e 74 20 77 69 6c 6c  the content will
37570 20 6c 69 6b 65 6c 79 20 63 68 61 6e 67 65 20 69   likely change i
37580 6e 0a 2a 2a 20 74 68 65 20 6e 65 61 72 20 66 75  n.** the near fu
37590 74 75 72 65 20 61 6e 64 20 74 68 61 74 20 53 51  ture and that SQ
375a0 4c 69 74 65 20 73 68 6f 75 6c 64 20 6d 61 6b 65  Lite should make
375b0 20 69 74 73 20 6f 77 6e 20 70 72 69 76 61 74 65   its own private
375c0 20 63 6f 70 79 20 6f 66 0a 2a 2a 20 74 68 65 20   copy of.** the 
375d0 63 6f 6e 74 65 6e 74 20 62 65 66 6f 72 65 20 72  content before r
375e0 65 74 75 72 6e 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20  eturning..**.** 
375f0 54 68 65 20 74 79 70 65 64 65 66 20 69 73 20 6e  The typedef is n
37600 65 63 65 73 73 61 72 79 20 74 6f 20 77 6f 72 6b  ecessary to work
37610 20 61 72 6f 75 6e 64 20 70 72 6f 62 6c 65 6d 73   around problems
37620 20 69 6e 20 63 65 72 74 61 69 6e 0a 2a 2a 20 43   in certain.** C
37630 2b 2b 20 63 6f 6d 70 69 6c 65 72 73 2e 20 20 53  ++ compilers.  S
37640 65 65 20 74 69 63 6b 65 74 20 23 32 31 39 31 2e  ee ticket #2191.
37650 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76 6f 69 64  .*/.typedef void
37660 20 28 2a 73 71 6c 69 74 65 33 5f 64 65 73 74 72   (*sqlite3_destr
37670 75 63 74 6f 72 5f 74 79 70 65 29 28 76 6f 69 64  uctor_type)(void
37680 2a 29 3b 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  *);.#define SQLI
37690 54 45 5f 53 54 41 54 49 43 20 20 20 20 20 20 28  TE_STATIC      (
376a0 28 73 71 6c 69 74 65 33 5f 64 65 73 74 72 75 63  (sqlite3_destruc
376b0 74 6f 72 5f 74 79 70 65 29 30 29 0a 23 64 65 66  tor_type)0).#def
376c0 69 6e 65 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ine SQLITE_TRANS
376d0 49 45 4e 54 20 20 20 28 28 73 71 6c 69 74 65 33  IENT   ((sqlite3
376e0 5f 64 65 73 74 72 75 63 74 6f 72 5f 74 79 70 65  _destructor_type
376f0 29 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  )-1)../*.** CAPI
37700 33 52 45 46 3a 20 53 65 74 74 69 6e 67 20 54 68  3REF: Setting Th
37710 65 20 52 65 73 75 6c 74 20 4f 66 20 41 6e 20 53  e Result Of An S
37720 51 4c 20 46 75 6e 63 74 69 6f 6e 20 7b 48 31 36  QL Function {H16
37730 34 30 30 7d 20 3c 53 32 30 32 30 30 3e 0a 2a 2a  400} <S20200>.**
37740 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
37750 65 73 20 61 72 65 20 75 73 65 64 20 62 79 20 74  es are used by t
37760 68 65 20 78 46 75 6e 63 20 6f 72 20 78 46 69 6e  he xFunc or xFin
37770 61 6c 20 63 61 6c 6c 62 61 63 6b 73 20 74 68 61  al callbacks tha
37780 74 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 53  t.** implement S
37790 51 4c 20 66 75 6e 63 74 69 6f 6e 73 20 61 6e 64  QL functions and
377a0 20 61 67 67 72 65 67 61 74 65 73 2e 20 20 53 65   aggregates.  Se
377b0 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f 63 72  e.** [sqlite3_cr
377c0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 29 5d  eate_function()]
377d0 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 63 72   and [sqlite3_cr
377e0 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 28  eate_function16(
377f0 29 5d 0a 2a 2a 20 66 6f 72 20 61 64 64 69 74 69  )].** for additi
37800 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
37810 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 66 75  ..**.** These fu
37820 6e 63 74 69 6f 6e 73 20 77 6f 72 6b 20 76 65 72  nctions work ver
37830 79 20 6d 75 63 68 20 6c 69 6b 65 20 74 68 65 20  y much like the 
37840 5b 70 61 72 61 6d 65 74 65 72 20 62 69 6e 64 69  [parameter bindi
37850 6e 67 5d 20 66 61 6d 69 6c 79 20 6f 66 0a 2a 2a  ng] family of.**
37860 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   functions used 
37870 74 6f 20 62 69 6e 64 20 76 61 6c 75 65 73 20 74  to bind values t
37880 6f 20 68 6f 73 74 20 70 61 72 61 6d 65 74 65 72  o host parameter
37890 73 20 69 6e 20 70 72 65 70 61 72 65 64 20 73 74  s in prepared st
378a0 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 20 52 65 66  atements..** Ref
378b0 65 72 20 74 6f 20 74 68 65 20 5b 53 51 4c 20 70  er to the [SQL p
378c0 61 72 61 6d 65 74 65 72 5d 20 64 6f 63 75 6d 65  arameter] docume
378d0 6e 74 61 74 69 6f 6e 20 66 6f 72 20 61 64 64 69  ntation for addi
378e0 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
378f0 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71  on..**.** The sq
37900 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
37910 62 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  b() interface se
37920 74 73 20 74 68 65 20 72 65 73 75 6c 74 20 66 72  ts the result fr
37930 6f 6d 0a 2a 2a 20 61 6e 20 61 70 70 6c 69 63 61  om.** an applica
37940 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
37950 63 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20  ction to be the 
37960 42 4c 4f 42 20 77 68 6f 73 65 20 63 6f 6e 74 65  BLOB whose conte
37970 6e 74 20 69 73 20 70 6f 69 6e 74 65 64 0a 2a 2a  nt is pointed.**
37980 20 74 6f 20 62 79 20 74 68 65 20 73 65 63 6f 6e   to by the secon
37990 64 20 70 61 72 61 6d 65 74 65 72 20 61 6e 64 20  d parameter and 
379a0 77 68 69 63 68 20 69 73 20 4e 20 62 79 74 65 73  which is N bytes
379b0 20 6c 6f 6e 67 20 77 68 65 72 65 20 4e 20 69 73   long where N is
379c0 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20 70 61   the.** third pa
379d0 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54  rameter..**.** T
379e0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
379f0 74 5f 7a 65 72 6f 62 6c 6f 62 28 29 20 69 6e 74  t_zeroblob() int
37a00 65 72 66 61 63 65 73 20 73 65 74 20 74 68 65 20  erfaces set the 
37a10 72 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65  result of.** the
37a20 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66   application-def
37a30 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f  ined function to
37a40 20 62 65 20 61 20 42 4c 4f 42 20 63 6f 6e 74 61   be a BLOB conta
37a50 69 6e 69 6e 67 20 61 6c 6c 20 7a 65 72 6f 0a 2a  ining all zero.*
37a60 2a 20 62 79 74 65 73 20 61 6e 64 20 4e 20 62 79  * bytes and N by
37a70 74 65 73 20 69 6e 20 73 69 7a 65 2c 20 77 68 65  tes in size, whe
37a80 72 65 20 4e 20 69 73 20 74 68 65 20 76 61 6c 75  re N is the valu
37a90 65 20 6f 66 20 74 68 65 20 32 6e 64 20 70 61 72  e of the 2nd par
37aa0 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68  ameter..**.** Th
37ab0 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
37ac0 5f 64 6f 75 62 6c 65 28 29 20 69 6e 74 65 72 66  _double() interf
37ad0 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65 73  ace sets the res
37ae0 75 6c 74 20 66 72 6f 6d 0a 2a 2a 20 61 6e 20 61  ult from.** an a
37af0 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e  pplication-defin
37b00 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62  ed function to b
37b10 65 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69  e a floating poi
37b20 6e 74 20 76 61 6c 75 65 20 73 70 65 63 69 66 69  nt value specifi
37b30 65 64 0a 2a 2a 20 62 79 20 69 74 73 20 32 6e 64  ed.** by its 2nd
37b40 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a   argument..**.**
37b50 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
37b60 75 6c 74 5f 65 72 72 6f 72 28 29 20 61 6e 64 20  ult_error() and 
37b70 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37b80 72 72 6f 72 31 36 28 29 20 66 75 6e 63 74 69 6f  rror16() functio
37b90 6e 73 0a 2a 2a 20 63 61 75 73 65 20 74 68 65 20  ns.** cause the 
37ba0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 53 51 4c 20  implemented SQL 
37bb0 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 72 6f  function to thro
37bc0 77 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a  w an exception..
37bd0 2a 2a 20 53 51 4c 69 74 65 20 75 73 65 73 20 74  ** SQLite uses t
37be0 68 65 20 73 74 72 69 6e 67 20 70 6f 69 6e 74 65  he string pointe
37bf0 64 20 74 6f 20 62 79 20 74 68 65 0a 2a 2a 20 32  d to by the.** 2
37c00 6e 64 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  nd parameter of 
37c10 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
37c20 72 72 6f 72 28 29 20 6f 72 20 73 71 6c 69 74 65  rror() or sqlite
37c30 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
37c40 28 29 0a 2a 2a 20 61 73 20 74 68 65 20 74 65 78  ().** as the tex
37c50 74 20 6f 66 20 61 6e 20 65 72 72 6f 72 20 6d 65  t of an error me
37c60 73 73 61 67 65 2e 20 20 53 51 4c 69 74 65 20 69  ssage.  SQLite i
37c70 6e 74 65 72 70 72 65 74 73 20 74 68 65 20 65 72  nterprets the er
37c80 72 6f 72 0a 2a 2a 20 6d 65 73 73 61 67 65 20 73  ror.** message s
37c90 74 72 69 6e 67 20 66 72 6f 6d 20 73 71 6c 69 74  tring from sqlit
37ca0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
37cb0 29 20 61 73 20 55 54 46 2d 38 2e 20 53 51 4c 69  ) as UTF-8. SQLi
37cc0 74 65 0a 2a 2a 20 69 6e 74 65 72 70 72 65 74 73  te.** interprets
37cd0 20 74 68 65 20 73 74 72 69 6e 67 20 66 72 6f 6d   the string from
37ce0 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
37cf0 65 72 72 6f 72 31 36 28 29 20 61 73 20 55 54 46  error16() as UTF
37d00 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 0a 2a 2a  -16 in native.**
37d10 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20 49 66   byte order.  If
37d20 20 74 68 65 20 74 68 69 72 64 20 70 61 72 61 6d   the third param
37d30 65 74 65 72 20 74 6f 20 73 71 6c 69 74 65 33 5f  eter to sqlite3_
37d40 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 29 0a 2a  result_error().*
37d50 2a 20 6f 72 20 73 71 6c 69 74 65 33 5f 72 65 73  * or sqlite3_res
37d60 75 6c 74 5f 65 72 72 6f 72 31 36 28 29 20 69 73  ult_error16() is
37d70 20 6e 65 67 61 74 69 76 65 20 74 68 65 6e 20 53   negative then S
37d80 51 4c 69 74 65 20 74 61 6b 65 73 20 61 73 20 74  QLite takes as t
37d90 68 65 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73  he error.** mess
37da0 61 67 65 20 61 6c 6c 20 74 65 78 74 20 75 70 20  age all text up 
37db0 74 68 72 6f 75 67 68 20 74 68 65 20 66 69 72 73  through the firs
37dc0 74 20 7a 65 72 6f 20 63 68 61 72 61 63 74 65 72  t zero character
37dd0 2e 0a 2a 2a 20 49 66 20 74 68 65 20 74 68 69 72  ..** If the thir
37de0 64 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 73  d parameter to s
37df0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37e00 72 6f 72 28 29 20 6f 72 0a 2a 2a 20 73 71 6c 69  ror() or.** sqli
37e10 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
37e20 31 36 28 29 20 69 73 20 6e 6f 6e 2d 6e 65 67 61  16() is non-nega
37e30 74 69 76 65 20 74 68 65 6e 20 53 51 4c 69 74 65  tive then SQLite
37e40 20 74 61 6b 65 73 20 74 68 61 74 20 6d 61 6e 79   takes that many
37e50 0a 2a 2a 20 62 79 74 65 73 20 28 6e 6f 74 20 63  .** bytes (not c
37e60 68 61 72 61 63 74 65 72 73 29 20 66 72 6f 6d 20  haracters) from 
37e70 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
37e80 72 20 61 73 20 74 68 65 20 65 72 72 6f 72 20 6d  r as the error m
37e90 65 73 73 61 67 65 2e 0a 2a 2a 20 54 68 65 20 73  essage..** The s
37ea0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
37eb0 72 6f 72 28 29 20 61 6e 64 20 73 71 6c 69 74 65  ror() and sqlite
37ec0 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
37ed0 28 29 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 6d  ().** routines m
37ee0 61 6b 65 20 61 20 70 72 69 76 61 74 65 20 63 6f  ake a private co
37ef0 70 79 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  py of the error 
37f00 6d 65 73 73 61 67 65 20 74 65 78 74 20 62 65 66  message text bef
37f10 6f 72 65 0a 2a 2a 20 74 68 65 79 20 72 65 74 75  ore.** they retu
37f20 72 6e 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20  rn.  Hence, the 
37f30 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e  calling function
37f40 20 63 61 6e 20 64 65 61 6c 6c 6f 63 61 74 65 20   can deallocate 
37f50 6f 72 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 65  or.** modify the
37f60 20 74 65 78 74 20 61 66 74 65 72 20 74 68 65 79   text after they
37f70 20 72 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   return without 
37f80 68 61 72 6d 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  harm..** The sql
37f90 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
37fa0 72 5f 63 6f 64 65 28 29 20 66 75 6e 63 74 69 6f  r_code() functio
37fb0 6e 20 63 68 61 6e 67 65 73 20 74 68 65 20 65 72  n changes the er
37fc0 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 72 65 74 75  ror code.** retu
37fd0 72 6e 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  rned by SQLite a
37fe0 73 20 61 20 72 65 73 75 6c 74 20 6f 66 20 61 6e  s a result of an
37ff0 20 65 72 72 6f 72 20 69 6e 20 61 20 66 75 6e 63   error in a func
38000 74 69 6f 6e 2e 20 20 42 79 20 64 65 66 61 75 6c  tion.  By defaul
38010 74 2c 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 20  t,.** the error 
38020 63 6f 64 65 20 69 73 20 53 51 4c 49 54 45 5f 45  code is SQLITE_E
38030 52 52 4f 52 2e 20 20 41 20 73 75 62 73 65 71 75  RROR.  A subsequ
38040 65 6e 74 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  ent call to sqli
38050 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
38060 28 29 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65 33  ().** or sqlite3
38070 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 28  _result_error16(
38080 29 20 72 65 73 65 74 73 20 74 68 65 20 65 72 72  ) resets the err
38090 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49 54  or code to SQLIT
380a0 45 5f 45 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 54  E_ERROR..**.** T
380b0 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  he sqlite3_resul
380c0 74 5f 74 6f 6f 62 69 67 28 29 20 69 6e 74 65 72  t_toobig() inter
380d0 66 61 63 65 20 63 61 75 73 65 73 20 53 51 4c 69  face causes SQLi
380e0 74 65 20 74 6f 20 74 68 72 6f 77 20 61 6e 20 65  te to throw an e
380f0 72 72 6f 72 0a 2a 2a 20 69 6e 64 69 63 61 74 69  rror.** indicati
38100 6e 67 20 74 68 61 74 20 61 20 73 74 72 69 6e 67  ng that a string
38110 20 6f 72 20 42 4c 4f 42 20 69 73 20 74 6f 20 6c   or BLOB is to l
38120 6f 6e 67 20 74 6f 20 72 65 70 72 65 73 65 6e 74  ong to represent
38130 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
38140 74 65 33 5f 72 65 73 75 6c 74 5f 6e 6f 6d 65 6d  te3_result_nomem
38150 28 29 20 69 6e 74 65 72 66 61 63 65 20 63 61 75  () interface cau
38160 73 65 73 20 53 51 4c 69 74 65 20 74 6f 20 74 68  ses SQLite to th
38170 72 6f 77 20 61 6e 20 65 72 72 6f 72 0a 2a 2a 20  row an error.** 
38180 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74 20  indicating that 
38190 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  a memory allocat
381a0 69 6f 6e 20 66 61 69 6c 65 64 2e 0a 2a 2a 0a 2a  ion failed..**.*
381b0 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65  * The sqlite3_re
381c0 73 75 6c 74 5f 69 6e 74 28 29 20 69 6e 74 65 72  sult_int() inter
381d0 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72 65  face sets the re
381e0 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 6f 66  turn value.** of
381f0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
38200 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
38210 6e 20 74 6f 20 62 65 20 74 68 65 20 33 32 2d 62  n to be the 32-b
38220 69 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65  it signed intege
38230 72 0a 2a 2a 20 76 61 6c 75 65 20 67 69 76 65 6e  r.** value given
38240 20 69 6e 20 74 68 65 20 32 6e 64 20 61 72 67 75   in the 2nd argu
38250 6d 65 6e 74 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  ment..** The sql
38260 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
38270 34 28 29 20 69 6e 74 65 72 66 61 63 65 20 73 65  4() interface se
38280 74 73 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  ts the return va
38290 6c 75 65 0a 2a 2a 20 6f 66 20 74 68 65 20 61 70  lue.** of the ap
382a0 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69 6e 65  plication-define
382b0 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 65  d function to be
382c0 20 74 68 65 20 36 34 2d 62 69 74 20 73 69 67 6e   the 64-bit sign
382d0 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 76 61  ed integer.** va
382e0 6c 75 65 20 67 69 76 65 6e 20 69 6e 20 74 68 65  lue given in the
382f0 20 32 6e 64 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   2nd argument..*
38300 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
38310 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 29 20 69  _result_null() i
38320 6e 74 65 72 66 61 63 65 20 73 65 74 73 20 74 68  nterface sets th
38330 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a  e return value.*
38340 2a 20 6f 66 20 74 68 65 20 61 70 70 6c 69 63 61  * of the applica
38350 74 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e  tion-defined fun
38360 63 74 69 6f 6e 20 74 6f 20 62 65 20 4e 55 4c 4c  ction to be NULL
38370 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  ..**.** The sqli
38380 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
38390 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  ), sqlite3_resul
383a0 74 5f 74 65 78 74 31 36 28 29 2c 0a 2a 2a 20 73  t_text16(),.** s
383b0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
383c0 78 74 31 36 6c 65 28 29 2c 20 61 6e 64 20 73 71  xt16le(), and sq
383d0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
383e0 74 31 36 62 65 28 29 20 69 6e 74 65 72 66 61 63  t16be() interfac
383f0 65 73 0a 2a 2a 20 73 65 74 20 74 68 65 20 72 65  es.** set the re
38400 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
38410 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65  e application-de
38420 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74  fined function t
38430 6f 20 62 65 0a 2a 2a 20 61 20 74 65 78 74 20 73  o be.** a text s
38440 74 72 69 6e 67 20 77 68 69 63 68 20 69 73 20 72  tring which is r
38450 65 70 72 65 73 65 6e 74 65 64 20 61 73 20 55 54  epresented as UT
38460 46 2d 38 2c 20 55 54 46 2d 31 36 20 6e 61 74 69  F-8, UTF-16 nati
38470 76 65 20 62 79 74 65 20 6f 72 64 65 72 2c 0a 2a  ve byte order,.*
38480 2a 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65 20  * UTF-16 little 
38490 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54 46 2d 31  endian, or UTF-1
384a0 36 20 62 69 67 20 65 6e 64 69 61 6e 2c 20 72 65  6 big endian, re
384b0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 53  spectively..** S
384c0 51 4c 69 74 65 20 74 61 6b 65 73 20 74 68 65 20  QLite takes the 
384d0 74 65 78 74 20 72 65 73 75 6c 74 20 66 72 6f 6d  text result from
384e0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
384f0 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 32 6e 64   from.** the 2nd
38500 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20 74 68   parameter of th
38510 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
38520 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65  _text* interface
38530 73 2e 0a 2a 2a 20 49 66 20 74 68 65 20 33 72 64  s..** If the 3rd
38540 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
38550 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
38560 5f 74 65 78 74 2a 20 69 6e 74 65 72 66 61 63 65  _text* interface
38570 73 0a 2a 2a 20 69 73 20 6e 65 67 61 74 69 76 65  s.** is negative
38580 2c 20 74 68 65 6e 20 53 51 4c 69 74 65 20 74 61  , then SQLite ta
38590 6b 65 73 20 72 65 73 75 6c 74 20 74 65 78 74 20  kes result text 
385a0 66 72 6f 6d 20 74 68 65 20 32 6e 64 20 70 61 72  from the 2nd par
385b0 61 6d 65 74 65 72 0a 2a 2a 20 74 68 72 6f 75 67  ameter.** throug
385c0 68 20 74 68 65 20 66 69 72 73 74 20 7a 65 72 6f  h the first zero
385d0 20 63 68 61 72 61 63 74 65 72 2e 0a 2a 2a 20 49   character..** I
385e0 66 20 74 68 65 20 33 72 64 20 70 61 72 61 6d 65  f the 3rd parame
385f0 74 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ter to the sqlit
38600 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20  e3_result_text* 
38610 69 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 69 73  interfaces.** is
38620 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2c 20 74   non-negative, t
38630 68 65 6e 20 61 73 20 6d 61 6e 79 20 62 79 74 65  hen as many byte
38640 73 20 28 6e 6f 74 20 63 68 61 72 61 63 74 65 72  s (not character
38650 73 29 20 6f 66 20 74 68 65 20 74 65 78 74 0a 2a  s) of the text.*
38660 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  * pointed to by 
38670 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74 65  the 2nd paramete
38680 72 20 61 72 65 20 74 61 6b 65 6e 20 61 73 20 74  r are taken as t
38690 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
386a0 65 66 69 6e 65 64 0a 2a 2a 20 66 75 6e 63 74 69  efined.** functi
386b0 6f 6e 20 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66  on result..** If
386c0 20 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74   the 4th paramet
386d0 65 72 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65  er to the sqlite
386e0 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2a 20 69  3_result_text* i
386f0 6e 74 65 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20  nterfaces.** or 
38700 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62  sqlite3_result_b
38710 6c 6f 62 20 69 73 20 61 20 6e 6f 6e 2d 4e 55 4c  lob is a non-NUL
38720 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 20  L pointer, then 
38730 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74 68 61  SQLite calls tha
38740 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61 73  t.** function as
38750 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
38760 6f 6e 20 74 68 65 20 74 65 78 74 20 6f 72 20 42  on the text or B
38770 4c 4f 42 20 72 65 73 75 6c 74 20 77 68 65 6e 20  LOB result when 
38780 69 74 20 68 61 73 0a 2a 2a 20 66 69 6e 69 73 68  it has.** finish
38790 65 64 20 75 73 69 6e 67 20 74 68 61 74 20 72 65  ed using that re
387a0 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  sult..** If the 
387b0 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
387c0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   the sqlite3_res
387d0 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65 72 66  ult_text* interf
387e0 61 63 65 73 20 6f 72 0a 2a 2a 20 73 71 6c 69 74  aces or.** sqlit
387f0 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 20 69  e3_result_blob i
38800 73 20 74 68 65 20 73 70 65 63 69 61 6c 20 63 6f  s the special co
38810 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f 53 54  nstant SQLITE_ST
38820 41 54 49 43 2c 20 74 68 65 6e 20 53 51 4c 69 74  ATIC, then SQLit
38830 65 0a 2a 2a 20 61 73 73 75 6d 65 73 20 74 68 61  e.** assumes tha
38840 74 20 74 68 65 20 74 65 78 74 20 6f 72 20 42 4c  t the text or BL
38850 4f 42 20 72 65 73 75 6c 74 20 69 73 20 69 6e 20  OB result is in 
38860 63 6f 6e 73 74 61 6e 74 20 73 70 61 63 65 20 61  constant space a
38870 6e 64 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 63  nd does not.** c
38880 6f 70 79 20 74 68 65 20 69 74 20 6f 72 20 63 61  opy the it or ca
38890 6c 6c 20 61 20 64 65 73 74 72 75 63 74 6f 72 20  ll a destructor 
388a0 77 68 65 6e 20 69 74 20 68 61 73 20 66 69 6e 69  when it has fini
388b0 73 68 65 64 20 75 73 69 6e 67 20 74 68 61 74 20  shed using that 
388c0 72 65 73 75 6c 74 2e 0a 2a 2a 20 49 66 20 74 68  result..** If th
388d0 65 20 34 74 68 20 70 61 72 61 6d 65 74 65 72 20  e 4th parameter 
388e0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  to the sqlite3_r
388f0 65 73 75 6c 74 5f 74 65 78 74 2a 20 69 6e 74 65  esult_text* inte
38900 72 66 61 63 65 73 0a 2a 2a 20 6f 72 20 73 71 6c  rfaces.** or sql
38910 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62  ite3_result_blob
38920 20 69 73 20 74 68 65 20 73 70 65 63 69 61 6c 20   is the special 
38930 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49 54 45 5f  constant SQLITE_
38940 54 52 41 4e 53 49 45 4e 54 0a 2a 2a 20 74 68 65  TRANSIENT.** the
38950 6e 20 53 51 4c 69 74 65 20 6d 61 6b 65 73 20 61  n SQLite makes a
38960 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65 73   copy of the res
38970 75 6c 74 20 69 6e 74 6f 20 73 70 61 63 65 20 6f  ult into space o
38980 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20  btained from.** 
38990 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 61  from [sqlite3_ma
389a0 6c 6c 6f 63 28 29 5d 20 62 65 66 6f 72 65 20 69  lloc()] before i
389b0 74 20 72 65 74 75 72 6e 73 2e 0a 2a 2a 0a 2a 2a  t returns..**.**
389c0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 72 65 73   The sqlite3_res
389d0 75 6c 74 5f 76 61 6c 75 65 28 29 20 69 6e 74 65  ult_value() inte
389e0 72 66 61 63 65 20 73 65 74 73 20 74 68 65 20 72  rface sets the r
389f0 65 73 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20  esult of.** the 
38a00 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64 65 66 69  application-defi
38a10 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  ned function to 
38a20 62 65 20 61 20 63 6f 70 79 20 74 68 65 0a 2a 2a  be a copy the.**
38a30 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20 73 71   [unprotected sq
38a40 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
38a50 65 63 74 20 73 70 65 63 69 66 69 65 64 20 62 79  ect specified by
38a60 20 74 68 65 20 32 6e 64 20 70 61 72 61 6d 65 74   the 2nd paramet
38a70 65 72 2e 20 20 54 68 65 0a 2a 2a 20 73 71 6c 69  er.  The.** sqli
38a80 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
38a90 28 29 20 69 6e 74 65 72 66 61 63 65 20 6d 61 6b  () interface mak
38aa0 65 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  es a copy of the
38ab0 20 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d   [sqlite3_value]
38ac0 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20  .** so that the 
38ad0 5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20  [sqlite3_value] 
38ae0 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68 65  specified in the
38af0 20 70 61 72 61 6d 65 74 65 72 20 6d 61 79 20 63   parameter may c
38b00 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 62 65 20 64  hange or.** be d
38b10 65 61 6c 6c 6f 63 61 74 65 64 20 61 66 74 65 72  eallocated after
38b20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
38b30 76 61 6c 75 65 28 29 20 72 65 74 75 72 6e 73 20  value() returns 
38b40 77 69 74 68 6f 75 74 20 68 61 72 6d 2e 0a 2a 2a  without harm..**
38b50 20 41 20 5b 70 72 6f 74 65 63 74 65 64 20 73 71   A [protected sq
38b60 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f 62 6a  lite3_value] obj
38b70 65 63 74 20 6d 61 79 20 61 6c 77 61 79 73 20 62  ect may always b
38b80 65 20 75 73 65 64 20 77 68 65 72 65 20 61 6e 0a  e used where an.
38b90 2a 2a 20 5b 75 6e 70 72 6f 74 65 63 74 65 64 20  ** [unprotected 
38ba0 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d 20 6f  sqlite3_value] o
38bb0 62 6a 65 63 74 20 69 73 20 72 65 71 75 69 72 65  bject is require
38bc0 64 2c 20 73 6f 20 65 69 74 68 65 72 0a 2a 2a 20  d, so either.** 
38bd0 6b 69 6e 64 20 6f 66 20 5b 73 71 6c 69 74 65 33  kind of [sqlite3
38be0 5f 76 61 6c 75 65 5d 20 6f 62 6a 65 63 74 20 63  _value] object c
38bf0 61 6e 20 62 65 20 75 73 65 64 20 77 69 74 68 20  an be used with 
38c00 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 2e 0a  this interface..
38c10 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 73 65 20 72  **.** If these r
38c20 6f 75 74 69 6e 65 73 20 61 72 65 20 63 61 6c 6c  outines are call
38c30 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74  ed from within t
38c40 68 65 20 64 69 66 66 65 72 65 6e 74 20 74 68 72  he different thr
38c50 65 61 64 0a 2a 2a 20 74 68 61 6e 20 74 68 65 20  ead.** than the 
38c60 6f 6e 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  one containing t
38c70 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2d 64  he application-d
38c80 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f 6e 20  efined function 
38c90 74 68 61 74 20 72 65 63 65 69 76 65 64 0a 2a 2a  that received.**
38ca0 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   the [sqlite3_co
38cb0 6e 74 65 78 74 5d 20 70 6f 69 6e 74 65 72 2c 20  ntext] pointer, 
38cc0 74 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20  the results are 
38cd0 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a  undefined..**.**
38ce0 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
38cf0 2a 2a 20 7b 48 31 36 34 30 33 7d 20 54 68 65 20  ** {H16403} The 
38d00 64 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20 76  default return v
38d10 61 6c 75 65 20 66 72 6f 6d 20 61 6e 79 20 53 51  alue from any SQ
38d20 4c 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e 55  L function is NU
38d30 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 30  LL..**.** {H1640
38d40 36 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  6} The [sqlite3_
38d50 72 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c  result_blob(C,V,
38d60 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20  N,D)] interface 
38d70 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20  changes the.**  
38d80 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76          return v
38d90 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e  alue of function
38da0 20 43 20 74 6f 20 62 65 20 61 20 42 4c 4f 42 20   C to be a BLOB 
38db0 74 68 61 74 20 69 73 20 4e 20 62 79 74 65 73 0a  that is N bytes.
38dc0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 6c  **          in l
38dd0 65 6e 67 74 68 20 61 6e 64 20 77 69 74 68 20 63  ength and with c
38de0 6f 6e 74 65 6e 74 20 70 6f 69 6e 74 65 64 20 74  ontent pointed t
38df0 6f 20 62 79 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48  o by V..**.** {H
38e00 31 36 34 30 39 7d 20 54 68 65 20 5b 73 71 6c 69  16409} The [sqli
38e10 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
38e20 65 28 43 2c 56 29 5d 20 69 6e 74 65 72 66 61 63  e(C,V)] interfac
38e30 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a  e changes the.**
38e40 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
38e50 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69   value of functi
38e60 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20 66  on C to be the f
38e70 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
38e80 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  lue V..**.** {H1
38e90 36 34 31 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  6412} The [sqlit
38ea0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
38eb0 43 2c 56 2c 4e 29 5d 20 69 6e 74 65 72 66 61 63  C,V,N)] interfac
38ec0 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65  e changes the re
38ed0 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  turn.**         
38ee0 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69   value of functi
38ef0 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78  on C to be an ex
38f00 63 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72  ception with err
38f10 6f 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20  or code.**      
38f20 20 20 20 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f      [SQLITE_ERRO
38f30 52 5d 20 61 6e 64 20 61 20 55 54 46 2d 38 20 65  R] and a UTF-8 e
38f40 72 72 6f 72 20 6d 65 73 73 61 67 65 20 63 6f 70  rror message cop
38f50 69 65 64 20 66 72 6f 6d 20 56 20 75 70 20 74 6f  ied from V up to
38f60 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20   the.**         
38f70 20 66 69 72 73 74 20 7a 65 72 6f 20 62 79 74 65   first zero byte
38f80 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74 65   or until N byte
38f90 73 20 61 72 65 20 72 65 61 64 20 69 66 20 4e 20  s are read if N 
38fa0 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a  is positive..**.
38fb0 2a 2a 20 7b 48 31 36 34 31 35 7d 20 54 68 65 20  ** {H16415} The 
38fc0 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
38fd0 65 72 72 6f 72 31 36 28 43 2c 56 2c 4e 29 5d 20  error16(C,V,N)] 
38fe0 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67 65  interface change
38ff0 73 20 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20  s the return.** 
39000 20 20 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f           value o
39010 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20  f function C to 
39020 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 20  be an exception 
39030 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64 65 0a  with error code.
39040 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51 4c  **          [SQL
39050 49 54 45 5f 45 52 52 4f 52 5d 20 61 6e 64 20 61  ITE_ERROR] and a
39060 20 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62   UTF-16 native b
39070 79 74 65 20 6f 72 64 65 72 20 65 72 72 6f 72 20  yte order error 
39080 6d 65 73 73 61 67 65 0a 2a 2a 20 20 20 20 20 20  message.**      
39090 20 20 20 20 63 6f 70 69 65 64 20 66 72 6f 6d 20      copied from 
390a0 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  V up to the firs
390b0 74 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 6f  t zero terminato
390c0 72 20 6f 72 20 75 6e 74 69 6c 20 4e 20 62 79 74  r or until N byt
390d0 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61  es.**          a
390e0 72 65 20 72 65 61 64 20 69 66 20 4e 20 69 73 20  re read if N is 
390f0 70 6f 73 69 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20  positive..**.** 
39100 7b 48 31 36 34 31 38 7d 20 54 68 65 20 5b 73 71  {H16418} The [sq
39110 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
39120 6f 72 5f 74 6f 6f 62 69 67 28 43 29 5d 20 69 6e  or_toobig(C)] in
39130 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20  terface changes 
39140 74 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20  the return.**   
39150 20 20 20 20 20 20 20 76 61 6c 75 65 20 6f 66 20         value of 
39160 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 43 20 74  the function C t
39170 6f 20 62 65 20 61 6e 20 65 78 63 65 70 74 69 6f  o be an exceptio
39180 6e 20 77 69 74 68 20 65 72 72 6f 72 20 63 6f 64  n with error cod
39190 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53  e.**          [S
391a0 51 4c 49 54 45 5f 54 4f 4f 42 49 47 5d 20 61 6e  QLITE_TOOBIG] an
391b0 64 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74 65  d an appropriate
391c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2e 0a   error message..
391d0 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 31 7d 20 54  **.** {H16421} T
391e0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  he [sqlite3_resu
391f0 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 43  lt_error_nomem(C
39200 29 5d 20 69 6e 74 65 72 66 61 63 65 20 63 68 61  )] interface cha
39210 6e 67 65 73 20 74 68 65 20 72 65 74 75 72 6e 0a  nges the return.
39220 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61 6c 75  **          valu
39230 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  e of the functio
39240 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 65 78 63  n C to be an exc
39250 65 70 74 69 6f 6e 20 77 69 74 68 20 65 72 72 6f  eption with erro
39260 72 20 63 6f 64 65 0a 2a 2a 20 20 20 20 20 20 20  r code.**       
39270 20 20 20 5b 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d     [SQLITE_NOMEM
39280 5d 20 61 6e 64 20 61 6e 20 61 70 70 72 6f 70 72  ] and an appropr
39290 69 61 74 65 20 65 72 72 6f 72 20 6d 65 73 73 61  iate error messa
392a0 67 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32  ge..**.** {H1642
392b0 34 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  4} The [sqlite3_
392c0 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64  result_error_cod
392d0 65 28 43 2c 45 29 5d 20 69 6e 74 65 72 66 61 63  e(C,E)] interfac
392e0 65 20 63 68 61 6e 67 65 73 20 74 68 65 20 72 65  e changes the re
392f0 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20  turn.**         
39300 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 75   value of the fu
39310 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 61  nction C to be a
39320 6e 20 65 78 63 65 70 74 69 6f 6e 20 77 69 74 68  n exception with
39330 20 65 72 72 6f 72 20 63 6f 64 65 20 45 2e 0a 2a   error code E..*
39340 2a 20 20 20 20 20 20 20 20 20 20 54 68 65 20 65  *          The e
39350 72 72 6f 72 20 6d 65 73 73 61 67 65 20 74 65 78  rror message tex
39360 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a  t is unchanged..
39370 2a 2a 0a 2a 2a 20 7b 48 31 36 34 32 37 7d 20 54  **.** {H16427} T
39380 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  he [sqlite3_resu
39390 6c 74 5f 69 6e 74 28 43 2c 56 29 5d 20 69 6e 74  lt_int(C,V)] int
393a0 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74  erface changes t
393b0 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72  he.**          r
393c0 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66  eturn value of f
393d0 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20  unction C to be 
393e0 74 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65 67  the 32-bit integ
393f0 65 72 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a  er value V..**.*
39400 2a 20 7b 48 31 36 34 33 30 7d 20 54 68 65 20 5b  * {H16430} The [
39410 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
39420 6e 74 36 34 28 43 2c 56 29 5d 20 69 6e 74 65 72  nt64(C,V)] inter
39430 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68 65  face changes the
39440 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 74  .**          ret
39450 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e  urn value of fun
39460 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68  ction C to be th
39470 65 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  e 64-bit integer
39480 20 76 61 6c 75 65 20 56 2e 0a 2a 2a 0a 2a 2a 20   value V..**.** 
39490 7b 48 31 36 34 33 33 7d 20 54 68 65 20 5b 73 71  {H16433} The [sq
394a0 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c  lite3_result_nul
394b0 6c 28 43 29 5d 20 69 6e 74 65 72 66 61 63 65 20  l(C)] interface 
394c0 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20  changes the.**  
394d0 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76          return v
394e0 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e  alue of function
394f0 20 43 20 74 6f 20 62 65 20 4e 55 4c 4c 2e 0a 2a   C to be NULL..*
39500 2a 0a 2a 2a 20 7b 48 31 36 34 33 36 7d 20 54 68  *.** {H16436} Th
39510 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
39520 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d  t_text(C,V,N,D)]
39530 20 69 6e 74 65 72 66 61 63 65 20 63 68 61 6e 67   interface chang
39540 65 73 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  es the.**       
39550 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20     return value 
39560 6f 66 20 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f  of function C to
39570 20 62 65 20 74 68 65 20 55 54 46 2d 38 20 73 74   be the UTF-8 st
39580 72 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ring.**         
39590 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69 72   V up to the fir
395a0 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73 20  st zero if N is 
395b0 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20 20  negative.**     
395c0 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72 73       or the firs
395d0 74 20 4e 20 62 79 74 65 73 20 6f 66 20 56 20 69  t N bytes of V i
395e0 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61 74  f N is non-negat
395f0 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34  ive..**.** {H164
39600 33 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33  39} The [sqlite3
39610 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28 43  _result_text16(C
39620 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61  ,V,N,D)] interfa
39630 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a  ce changes the.*
39640 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  *          retur
39650 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74  n value of funct
39660 69 6f 6e 20 43 20 74 6f 20 62 65 20 74 68 65 20  ion C to be the 
39670 55 54 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79  UTF-16 native by
39680 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20 20 20  te order.**     
39690 20 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70       string V up
396a0 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65   to the first ze
396b0 72 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74  ro if N is negat
396c0 69 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ive.**          
396d0 6f 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62  or the first N b
396e0 79 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69  ytes of V if N i
396f0 73 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a  s non-negative..
39700 2a 2a 0a 2a 2a 20 7b 48 31 36 34 34 32 7d 20 54  **.** {H16442} T
39710 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  he [sqlite3_resu
39720 6c 74 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c  lt_text16be(C,V,
39730 4e 2c 44 29 5d 20 69 6e 74 65 72 66 61 63 65 20  N,D)] interface 
39740 63 68 61 6e 67 65 73 20 74 68 65 0a 2a 2a 20 20  changes the.**  
39750 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 76          return v
39760 61 6c 75 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e  alue of function
39770 20 43 20 74 6f 20 62 65 20 74 68 65 20 55 54 46   C to be the UTF
39780 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 0a 2a  -16 big-endian.*
39790 2a 20 20 20 20 20 20 20 20 20 20 73 74 72 69 6e  *          strin
397a0 67 20 56 20 75 70 20 74 6f 20 74 68 65 20 66 69  g V up to the fi
397b0 72 73 74 20 7a 65 72 6f 20 69 66 20 4e 20 69 73  rst zero if N is
397c0 20 6e 65 67 61 74 69 76 65 0a 2a 2a 20 20 20 20   negative.**    
397d0 20 20 20 20 20 20 6f 72 20 74 68 65 20 66 69 72        or the fir
397e0 73 74 20 4e 20 62 79 74 65 73 20 6f 72 20 56 20  st N bytes or V 
397f0 69 66 20 4e 20 69 73 20 6e 6f 6e 2d 6e 65 67 61  if N is non-nega
39800 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  tive..**.** {H16
39810 34 34 35 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  445} The [sqlite
39820 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
39830 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 6e 74 65  e(C,V,N,D)] inte
39840 72 66 61 63 65 20 63 68 61 6e 67 65 73 20 74 68  rface changes th
39850 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  e.**          re
39860 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 66 75  turn value of fu
39870 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65 20 74  nction C to be t
39880 68 65 20 55 54 46 2d 31 36 20 6c 69 74 74 6c 65  he UTF-16 little
39890 2d 65 6e 64 69 61 6e 0a 2a 2a 20 20 20 20 20 20  -endian.**      
398a0 20 20 20 20 73 74 72 69 6e 67 20 56 20 75 70 20      string V up 
398b0 74 6f 20 74 68 65 20 66 69 72 73 74 20 7a 65 72  to the first zer
398c0 6f 20 69 66 20 4e 20 69 73 20 6e 65 67 61 74 69  o if N is negati
398d0 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  ve.**          o
398e0 72 20 74 68 65 20 66 69 72 73 74 20 4e 20 62 79  r the first N by
398f0 74 65 73 20 6f 66 20 56 20 69 66 20 4e 20 69 73  tes of V if N is
39900 20 6e 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 0a 2a   non-negative..*
39910 2a 0a 2a 2a 20 7b 48 31 36 34 34 38 7d 20 54 68  *.** {H16448} Th
39920 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  e [sqlite3_resul
39930 74 5f 76 61 6c 75 65 28 43 2c 56 29 5d 20 69 6e  t_value(C,V)] in
39940 74 65 72 66 61 63 65 20 63 68 61 6e 67 65 73 20  terface changes 
39950 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
39960 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
39970 66 75 6e 63 74 69 6f 6e 20 43 20 74 6f 20 62 65  function C to be
39980 20 74 68 65 20 5b 75 6e 70 72 6f 74 65 63 74 65   the [unprotecte
39990 64 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5d  d sqlite3_value]
399a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 62 6a  .**          obj
399b0 65 63 74 20 56 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ect V..**.** {H1
399c0 36 34 35 31 7d 20 54 68 65 20 5b 73 71 6c 69 74  6451} The [sqlit
399d0 65 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c  e3_result_zerobl
399e0 6f 62 28 43 2c 4e 29 5d 20 69 6e 74 65 72 66 61  ob(C,N)] interfa
399f0 63 65 20 63 68 61 6e 67 65 73 20 74 68 65 0a 2a  ce changes the.*
39a00 2a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  *          retur
39a10 6e 20 76 61 6c 75 65 20 6f 66 20 66 75 6e 63 74  n value of funct
39a20 69 6f 6e 20 43 20 74 6f 20 62 65 20 61 6e 20 4e  ion C to be an N
39a30 2d 62 79 74 65 20 42 4c 4f 42 20 6f 66 20 61 6c  -byte BLOB of al
39a40 6c 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a 20 7b  l zeros..**.** {
39a50 48 31 36 34 35 34 7d 20 54 68 65 20 5b 73 71 6c  H16454} The [sql
39a60 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
39a70 72 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  r()] and [sqlite
39a80 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
39a90 28 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ()].**          
39aa0 69 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 20  interfaces make 
39ab0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 69 72 20  a copy of their 
39ac0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74  error message st
39ad0 72 69 6e 67 73 20 62 65 66 6f 72 65 0a 2a 2a 20  rings before.** 
39ae0 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 69           returni
39af0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 34 35  ng..**.** {H1645
39b00 37 7d 20 49 66 20 74 68 65 20 44 20 64 65 73 74  7} If the D dest
39b10 72 75 63 74 6f 72 20 70 61 72 61 6d 65 74 65 72  ructor parameter
39b20 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 72 65 73   to [sqlite3_res
39b30 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c 4e 2c 44  ult_blob(C,V,N,D
39b40 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  )],.**          
39b50 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
39b60 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29 5d 2c 20  text(C,V,N,D)], 
39b70 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f  [sqlite3_result_
39b80 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c 44 29 5d  text16(C,V,N,D)]
39b90 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  ,.**          [s
39ba0 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
39bb0 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c 44 29 5d  xt16be(C,V,N,D)]
39bc0 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  , or.**         
39bd0 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
39be0 5f 74 65 78 74 31 36 6c 65 28 43 2c 56 2c 4e 2c  _text16le(C,V,N,
39bf0 44 29 5d 20 69 73 20 74 68 65 20 63 6f 6e 73 74  D)] is the const
39c00 61 6e 74 20 5b 53 51 4c 49 54 45 5f 53 54 41 54  ant [SQLITE_STAT
39c10 49 43 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  IC].**          
39c20 74 68 65 6e 20 6e 6f 20 64 65 73 74 72 75 63 74  then no destruct
39c30 6f 72 20 69 73 20 65 76 65 72 20 63 61 6c 6c 65  or is ever calle
39c40 64 20 6f 6e 20 74 68 65 20 70 6f 69 6e 74 65 72  d on the pointer
39c50 20 56 20 61 6e 64 20 53 51 4c 69 74 65 0a 2a 2a   V and SQLite.**
39c60 20 20 20 20 20 20 20 20 20 20 61 73 73 75 6d 65            assume
39c70 73 20 74 68 61 74 20 56 20 69 73 20 69 6d 6d 75  s that V is immu
39c80 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  table..**.** {H1
39c90 36 34 36 30 7d 20 49 66 20 74 68 65 20 44 20 64  6460} If the D d
39ca0 65 73 74 72 75 63 74 6f 72 20 70 61 72 61 6d 65  estructor parame
39cb0 74 65 72 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ter to [sqlite3_
39cc0 72 65 73 75 6c 74 5f 62 6c 6f 62 28 43 2c 56 2c  result_blob(C,V,
39cd0 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20  N,D)],.**       
39ce0 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75     [sqlite3_resu
39cf0 6c 74 5f 74 65 78 74 28 43 2c 56 2c 4e 2c 44 29  lt_text(C,V,N,D)
39d00 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75  ], [sqlite3_resu
39d10 6c 74 5f 74 65 78 74 31 36 28 43 2c 56 2c 4e 2c  lt_text16(C,V,N,
39d20 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20  D)],.**         
39d30 20 5b 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74   [sqlite3_result
39d40 5f 74 65 78 74 31 36 62 65 28 43 2c 56 2c 4e 2c  _text16be(C,V,N,
39d50 44 29 5d 2c 20 6f 72 0a 2a 2a 20 20 20 20 20 20  D)], or.**      
39d60 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 72 65 73      [sqlite3_res
39d70 75 6c 74 5f 74 65 78 74 31 36 6c 65 28 43 2c 56  ult_text16le(C,V
39d80 2c 4e 2c 44 29 5d 20 69 73 20 74 68 65 20 63 6f  ,N,D)] is the co
39d90 6e 73 74 61 6e 74 0a 2a 2a 20 20 20 20 20 20 20  nstant.**       
39da0 20 20 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e 53     [SQLITE_TRANS
39db0 49 45 4e 54 5d 20 74 68 65 6e 20 74 68 65 20 69  IENT] then the i
39dc0 6e 74 65 72 66 61 63 65 73 20 6d 61 6b 65 73 20  nterfaces makes 
39dd0 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a  a copy of the.**
39de0 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 65 6e            conten
39df0 74 20 6f 66 20 56 20 61 6e 64 20 72 65 74 61 69  t of V and retai
39e00 6e 73 20 74 68 65 20 63 6f 70 79 2e 0a 2a 2a 0a  ns the copy..**.
39e10 2a 2a 20 7b 48 31 36 34 36 33 7d 20 49 66 20 74  ** {H16463} If t
39e20 68 65 20 44 20 64 65 73 74 72 75 63 74 6f 72 20  he D destructor 
39e30 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71  parameter to [sq
39e40 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
39e50 62 28 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20  b(C,V,N,D)],.** 
39e60 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
39e70 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 43 2c  3_result_text(C,
39e80 56 2c 4e 2c 44 29 5d 2c 20 5b 73 71 6c 69 74 65  V,N,D)], [sqlite
39e90 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28  3_result_text16(
39ea0 43 2c 56 2c 4e 2c 44 29 5d 2c 0a 2a 2a 20 20 20  C,V,N,D)],.**   
39eb0 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
39ec0 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28  result_text16be(
39ed0 43 2c 56 2c 4e 2c 44 29 5d 2c 20 6f 72 0a 2a 2a  C,V,N,D)], or.**
39ee0 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
39ef0 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36  e3_result_text16
39f00 6c 65 28 43 2c 56 2c 4e 2c 44 29 5d 20 69 73 20  le(C,V,N,D)] is 
39f10 73 6f 6d 65 20 76 61 6c 75 65 20 6f 74 68 65 72  some value other
39f20 20 74 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20   than.**        
39f30 20 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20    the constants 
39f40 5b 53 51 4c 49 54 45 5f 53 54 41 54 49 43 5d 20  [SQLITE_STATIC] 
39f50 61 6e 64 20 5b 53 51 4c 49 54 45 5f 54 52 41 4e  and [SQLITE_TRAN
39f60 53 49 45 4e 54 5d 20 74 68 65 6e 0a 2a 2a 20 20  SIENT] then.**  
39f70 20 20 20 20 20 20 20 20 53 51 4c 69 74 65 20 77          SQLite w
39f80 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68 65 20 64  ill invoke the d
39f90 65 73 74 72 75 63 74 6f 72 20 44 20 77 69 74 68  estructor D with
39fa0 20 56 20 61 73 20 69 74 73 20 6f 6e 6c 79 20 61   V as its only a
39fb0 72 67 75 6d 65 6e 74 0a 2a 2a 20 20 20 20 20 20  rgument.**      
39fc0 20 20 20 20 77 68 65 6e 20 69 74 20 68 61 73 20      when it has 
39fd0 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 74 68  finished with th
39fe0 65 20 56 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51  e V value..*/.SQ
39ff0 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
3a000 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f  lite3_result_blo
3a010 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  b(sqlite3_contex
3a020 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
3a030 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29 28 76 6f   int, void(*)(vo
3a040 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50  id*));.SQLITE_AP
3a050 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
3a060 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c  esult_double(sql
3a070 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 64  ite3_context*, d
3a080 6f 75 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 41  ouble);.SQLITE_A
3a090 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
3a0a0 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 73 71 6c  result_error(sql
3a0b0 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63  ite3_context*, c
3a0c0 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 29  onst char*, int)
3a0d0 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
3a0e0 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
3a0f0 5f 65 72 72 6f 72 31 36 28 73 71 6c 69 74 65 33  _error16(sqlite3
3a100 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73 74  _context*, const
3a110 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a 53 51   void*, int);.SQ
3a120 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71  LITE_API void sq
3a130 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
3a140 6f 72 5f 74 6f 6f 62 69 67 28 73 71 6c 69 74 65  or_toobig(sqlite
3a150 33 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c  3_context*);.SQL
3a160 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3a170 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
3a180 72 5f 6e 6f 6d 65 6d 28 73 71 6c 69 74 65 33 5f  r_nomem(sqlite3_
3a190 63 6f 6e 74 65 78 74 2a 29 3b 0a 53 51 4c 49 54  context*);.SQLIT
3a1a0 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
3a1b0 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f  e3_result_error_
3a1c0 63 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e  code(sqlite3_con
3a1d0 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  text*, int);.SQL
3a1e0 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
3a1f0 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28  ite3_result_int(
3a200 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
3a210 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 41  , int);.SQLITE_A
3a220 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
3a230 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 73 71 6c  result_int64(sql
3a240 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 73  ite3_context*, s
3a250 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 3b 0a 53  qlite3_int64);.S
3a260 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
3a270 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75  qlite3_result_nu
3a280 6c 6c 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ll(sqlite3_conte
3a290 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  xt*);.SQLITE_API
3a2a0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
3a2b0 73 75 6c 74 5f 74 65 78 74 28 73 71 6c 69 74 65  sult_text(sqlite
3a2c0 33 5f 63 6f 6e 74 65 78 74 2a 2c 20 63 6f 6e 73  3_context*, cons
3a2d0 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 76 6f  t char*, int, vo
3a2e0 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53  id(*)(void*));.S
3a2f0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
3a300 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
3a310 78 74 31 36 28 73 71 6c 69 74 65 33 5f 63 6f 6e  xt16(sqlite3_con
3a320 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69  text*, const voi
3a330 64 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28 2a 29  d*, int, void(*)
3a340 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54 45  (void*));.SQLITE
3a350 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
3a360 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c  3_result_text16l
3a370 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
3a380 74 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  t*, const void*,
3a390 20 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69   int,void(*)(voi
3a3a0 64 2a 29 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  d*));.SQLITE_API
3a3b0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
3a3c0 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28 73 71  sult_text16be(sq
3a3d0 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 20  lite3_context*, 
3a3e0 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74  const void*, int
3a3f0 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29  ,void(*)(void*))
3a400 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  ;.SQLITE_API voi
3a410 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
3a420 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 63  _value(sqlite3_c
3a430 6f 6e 74 65 78 74 2a 2c 20 73 71 6c 69 74 65 33  ontext*, sqlite3
3a440 5f 76 61 6c 75 65 2a 29 3b 0a 53 51 4c 49 54 45  _value*);.SQLITE
3a450 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
3a460 33 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  3_result_zeroblo
3a470 62 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  b(sqlite3_contex
3a480 74 2a 2c 20 69 6e 74 20 6e 29 3b 0a 0a 2f 2a 0a  t*, int n);../*.
3a490 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 44 65 66  ** CAPI3REF: Def
3a4a0 69 6e 65 20 4e 65 77 20 43 6f 6c 6c 61 74 69 6e  ine New Collatin
3a4b0 67 20 53 65 71 75 65 6e 63 65 73 20 7b 48 31 36  g Sequences {H16
3a4c0 36 30 30 7d 20 3c 53 32 30 33 30 30 3e 0a 2a 2a  600} <S20300>.**
3a4d0 0a 2a 2a 20 54 68 65 73 65 20 66 75 6e 63 74 69  .** These functi
3a4e0 6f 6e 73 20 61 72 65 20 75 73 65 64 20 74 6f 20  ons are used to 
3a4f0 61 64 64 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f  add new collatio
3a500 6e 20 73 65 71 75 65 6e 63 65 73 20 74 6f 20 74  n sequences to t
3a510 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73 65 20  he.** [database 
3a520 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 73 70 65 63  connection] spec
3a530 69 66 69 65 64 20 61 73 20 74 68 65 20 66 69 72  ified as the fir
3a540 73 74 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  st argument..**.
3a550 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  ** The name of t
3a560 68 65 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e  he new collation
3a570 20 73 65 71 75 65 6e 63 65 20 69 73 20 73 70 65   sequence is spe
3a580 63 69 66 69 65 64 20 61 73 20 61 20 55 54 46 2d  cified as a UTF-
3a590 38 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20  8 string.** for 
3a5a0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
3a5b0 6f 6c 6c 61 74 69 6f 6e 28 29 20 61 6e 64 20 73  ollation() and s
3a5c0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f  qlite3_create_co
3a5d0 6c 6c 61 74 69 6f 6e 5f 76 32 28 29 0a 2a 2a 20  llation_v2().** 
3a5e0 61 6e 64 20 61 20 55 54 46 2d 31 36 20 73 74 72  and a UTF-16 str
3a5f0 69 6e 67 20 66 6f 72 20 73 71 6c 69 74 65 33 5f  ing for sqlite3_
3a600 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3a610 31 36 28 29 2e 20 49 6e 20 61 6c 6c 20 63 61 73  16(). In all cas
3a620 65 73 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 69  es.** the name i
3a630 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
3a640 73 65 63 6f 6e 64 20 66 75 6e 63 74 69 6f 6e 20  second function 
3a650 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
3a660 54 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65  The third argume
3a670 6e 74 20 6d 61 79 20 62 65 20 6f 6e 65 20 6f 66  nt may be one of
3a680 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 73 20 5b   the constants [
3a690 53 51 4c 49 54 45 5f 55 54 46 38 5d 2c 0a 2a 2a  SQLITE_UTF8],.**
3a6a0 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45   [SQLITE_UTF16LE
3a6b0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 54 46  ] or [SQLITE_UTF
3a6c0 31 36 42 45 5d 2c 20 69 6e 64 69 63 61 74 69 6e  16BE], indicatin
3a6d0 67 20 74 68 61 74 20 74 68 65 20 75 73 65 72 2d  g that the user-
3a6e0 73 75 70 70 6c 69 65 64 0a 2a 2a 20 72 6f 75 74  supplied.** rout
3a6f0 69 6e 65 20 65 78 70 65 63 74 73 20 74 6f 20 62  ine expects to b
3a700 65 20 70 61 73 73 65 64 20 70 6f 69 6e 74 65 72  e passed pointer
3a710 73 20 74 6f 20 73 74 72 69 6e 67 73 20 65 6e 63  s to strings enc
3a720 6f 64 65 64 20 75 73 69 6e 67 20 55 54 46 2d 38  oded using UTF-8
3a730 2c 0a 2a 2a 20 55 54 46 2d 31 36 20 6c 69 74 74  ,.** UTF-16 litt
3a740 6c 65 2d 65 6e 64 69 61 6e 2c 20 6f 72 20 55 54  le-endian, or UT
3a750 46 2d 31 36 20 62 69 67 2d 65 6e 64 69 61 6e 2c  F-16 big-endian,
3a760 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 54   respectively. T
3a770 68 65 0a 2a 2a 20 74 68 69 72 64 20 61 72 67 75  he.** third argu
3a780 6d 65 6e 74 20 6d 69 67 68 74 20 61 6c 73 6f 20  ment might also 
3a790 62 65 20 5b 53 51 4c 49 54 45 5f 55 54 46 31 36  be [SQLITE_UTF16
3a7a0 5f 41 4c 49 47 4e 45 44 5d 20 74 6f 20 69 6e 64  _ALIGNED] to ind
3a7b0 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 74 68  icate that.** th
3a7c0 65 20 72 6f 75 74 69 6e 65 20 65 78 70 65 63 74  e routine expect
3a7d0 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 31 36  s pointers to 16
3a7e0 2d 62 69 74 20 77 6f 72 64 20 61 6c 69 67 6e 65  -bit word aligne
3a7f0 64 20 73 74 72 69 6e 67 73 0a 2a 2a 20 6f 66 20  d strings.** of 
3a800 55 54 46 2d 31 36 20 69 6e 20 74 68 65 20 6e 61  UTF-16 in the na
3a810 74 69 76 65 20 62 79 74 65 20 6f 72 64 65 72 20  tive byte order 
3a820 6f 66 20 74 68 65 20 68 6f 73 74 20 63 6f 6d 70  of the host comp
3a830 75 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 6f  uter..**.** A po
3a840 69 6e 74 65 72 20 74 6f 20 74 68 65 20 75 73 65  inter to the use
3a850 72 20 73 75 70 70 6c 69 65 64 20 72 6f 75 74 69  r supplied routi
3a860 6e 65 20 6d 75 73 74 20 62 65 20 70 61 73 73 65  ne must be passe
3a870 64 20 61 73 20 74 68 65 20 66 69 66 74 68 0a 2a  d as the fifth.*
3a880 2a 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20  * argument.  If 
3a890 69 74 20 69 73 20 4e 55 4c 4c 2c 20 74 68 69 73  it is NULL, this
3a8a0 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
3a8b0 64 65 6c 65 74 69 6e 67 20 74 68 65 20 63 6f 6c  deleting the col
3a8c0 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e  lation.** sequen
3a8d0 63 65 20 28 73 6f 20 74 68 61 74 20 53 51 4c 69  ce (so that SQLi
3a8e0 74 65 20 63 61 6e 6e 6f 74 20 63 61 6c 6c 20 69  te cannot call i
3a8f0 74 20 61 6e 79 6d 6f 72 65 29 2e 0a 2a 2a 20 45  t anymore)..** E
3a900 61 63 68 20 74 69 6d 65 20 74 68 65 20 61 70 70  ach time the app
3a910 6c 69 63 61 74 69 6f 6e 20 73 75 70 70 6c 69 65  lication supplie
3a920 64 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  d function is in
3a930 76 6f 6b 65 64 2c 20 69 74 20 69 73 20 70 61 73  voked, it is pas
3a940 73 65 64 0a 2a 2a 20 61 73 20 69 74 73 20 66 69  sed.** as its fi
3a950 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 61 20  rst parameter a 
3a960 63 6f 70 79 20 6f 66 20 74 68 65 20 76 6f 69 64  copy of the void
3a970 2a 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  * passed as the 
3a980 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 0a  fourth argument.
3a990 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 72  ** to sqlite3_cr
3a9a0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29  eate_collation()
3a9b0 20 6f 72 20 73 71 6c 69 74 65 33 5f 63 72 65 61   or sqlite3_crea
3a9c0 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 29  te_collation16()
3a9d0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 6d 61  ..**.** The rema
3a9e0 69 6e 69 6e 67 20 61 72 67 75 6d 65 6e 74 73 20  ining arguments 
3a9f0 74 6f 20 74 68 65 20 61 70 70 6c 69 63 61 74 69  to the applicati
3aa00 6f 6e 2d 73 75 70 70 6c 69 65 64 20 72 6f 75 74  on-supplied rout
3aa10 69 6e 65 20 61 72 65 20 74 77 6f 20 73 74 72 69  ine are two stri
3aa20 6e 67 73 2c 0a 2a 2a 20 65 61 63 68 20 72 65 70  ngs,.** each rep
3aa30 72 65 73 65 6e 74 65 64 20 62 79 20 61 20 28 6c  resented by a (l
3aa40 65 6e 67 74 68 2c 20 64 61 74 61 29 20 70 61 69  ength, data) pai
3aa50 72 20 61 6e 64 20 65 6e 63 6f 64 65 64 20 69 6e  r and encoded in
3aa60 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a   the encoding.**
3aa70 20 74 68 61 74 20 77 61 73 20 70 61 73 73 65 64   that was passed
3aa80 20 61 73 20 74 68 65 20 74 68 69 72 64 20 61 72   as the third ar
3aa90 67 75 6d 65 6e 74 20 77 68 65 6e 20 74 68 65 20  gument when the 
3aaa0 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
3aab0 63 65 20 77 61 73 0a 2a 2a 20 72 65 67 69 73 74  ce was.** regist
3aac0 65 72 65 64 2e 20 7b 45 4e 44 7d 20 20 54 68 65  ered. {END}  The
3aad0 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 64 65 66   application def
3aae0 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 72  ined collation r
3aaf0 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 0a 2a 2a  outine should.**
3ab00 20 72 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65   return negative
3ab10 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69  , zero or positi
3ab20 76 65 20 69 66 20 74 68 65 20 66 69 72 73 74 20  ve if the first 
3ab30 73 74 72 69 6e 67 20 69 73 20 6c 65 73 73 20 74  string is less t
3ab40 68 61 6e 2c 0a 2a 2a 20 65 71 75 61 6c 20 74 6f  han,.** equal to
3ab50 2c 20 6f 72 20 67 72 65 61 74 65 72 20 74 68 61  , or greater tha
3ab60 6e 20 74 68 65 20 73 65 63 6f 6e 64 20 73 74 72  n the second str
3ab70 69 6e 67 2e 20 69 2e 65 2e 20 28 53 54 52 49 4e  ing. i.e. (STRIN
3ab80 47 31 20 2d 20 53 54 52 49 4e 47 32 29 2e 0a 2a  G1 - STRING2)..*
3ab90 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
3aba0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
3abb0 6e 5f 76 32 28 29 20 77 6f 72 6b 73 20 6c 69 6b  n_v2() works lik
3abc0 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
3abd0 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 0a 2a 2a 20  _collation().** 
3abe0 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 74  except that it t
3abf0 61 6b 65 73 20 61 6e 20 65 78 74 72 61 20 61 72  akes an extra ar
3ac00 67 75 6d 65 6e 74 20 77 68 69 63 68 20 69 73 20  gument which is 
3ac10 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 6f 72  a destructor for
3ac20 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  .** the collatio
3ac30 6e 2e 20 20 54 68 65 20 64 65 73 74 72 75 63 74  n.  The destruct
3ac40 6f 72 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  or is called whe
3ac50 6e 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  n the collation 
3ac60 69 73 0a 2a 2a 20 64 65 73 74 72 6f 79 65 64 20  is.** destroyed 
3ac70 61 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 20  and is passed a 
3ac80 63 6f 70 79 20 6f 66 20 74 68 65 20 66 6f 75 72  copy of the four
3ac90 74 68 20 70 61 72 61 6d 65 74 65 72 20 76 6f 69  th parameter voi
3aca0 64 2a 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 6f 66  d* pointer.** of
3acb0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 72 65   the sqlite3_cre
3acc0 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32  ate_collation_v2
3acd0 28 29 2e 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e  ()..** Collation
3ace0 73 20 61 72 65 20 64 65 73 74 72 6f 79 65 64 20  s are destroyed 
3acf0 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6f 76  when they are ov
3ad00 65 72 72 69 64 64 65 6e 20 62 79 20 6c 61 74 65  erridden by late
3ad10 72 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 0a 2a  r calls to the.*
3ad20 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 63 72 65 61  * collation crea
3ad30 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 73 20 6f  tion functions o
3ad40 72 20 77 68 65 6e 20 74 68 65 20 5b 64 61 74 61  r when the [data
3ad50 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
3ad60 20 69 73 20 63 6c 6f 73 65 64 0a 2a 2a 20 75 73   is closed.** us
3ad70 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f 63 6c 6f  ing [sqlite3_clo
3ad80 73 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  se()]..**.** INV
3ad90 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
3ada0 48 31 36 36 30 33 7d 20 41 20 73 75 63 63 65 73  H16603} A succes
3adb0 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 74 68 65  sful call to the
3adc0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
3add0 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
3ade0 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c  lation_v2(B,X,E,
3adf0 50 2c 46 2c 44 29 5d 20 69 6e 74 65 72 66 61 63  P,F,D)] interfac
3ae00 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65  e.**          re
3ae10 67 69 73 74 65 72 73 20 66 75 6e 63 74 69 6f 6e  gisters function
3ae20 20 46 20 61 73 20 74 68 65 20 63 6f 6d 70 61 72   F as the compar
3ae30 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73  ison function us
3ae40 65 64 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20  ed to.**        
3ae50 20 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f 6c 6c    implement coll
3ae60 61 74 69 6f 6e 20 58 20 6f 6e 20 74 68 65 20 5b  ation X on the [
3ae70 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3ae80 69 6f 6e 5d 20 42 20 66 6f 72 0a 2a 2a 20 20 20  ion] B for.**   
3ae90 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65 73         databases
3aea0 20 68 61 76 69 6e 67 20 65 6e 63 6f 64 69 6e 67   having encoding
3aeb0 20 45 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 30   E..**.** {H1660
3aec0 34 7d 20 53 51 4c 69 74 65 20 75 6e 64 65 72 73  4} SQLite unders
3aed0 74 61 6e 64 73 20 74 68 65 20 58 20 70 61 72 61  tands the X para
3aee0 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 20 20 20 20  meter to.**     
3aef0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 63 72       [sqlite3_cr
3af00 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76  eate_collation_v
3af10 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 20  2(B,X,E,P,F,D)] 
3af20 61 73 20 61 20 7a 65 72 6f 2d 74 65 72 6d 69 6e  as a zero-termin
3af30 61 74 65 64 0a 2a 2a 20 20 20 20 20 20 20 20 20  ated.**         
3af40 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 6e   UTF-8 string in
3af50 20 77 68 69 63 68 20 63 61 73 65 20 69 73 20 69   which case is i
3af60 67 6e 6f 72 65 64 20 66 6f 72 20 41 53 43 49 49  gnored for ASCII
3af70 20 63 68 61 72 61 63 74 65 72 73 20 61 6e 64 0a   characters and.
3af80 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73 20 73  **          is s
3af90 69 67 6e 69 66 69 63 61 6e 74 20 66 6f 72 20 6e  ignificant for n
3afa0 6f 6e 2d 41 53 43 49 49 20 63 68 61 72 61 63 74  on-ASCII charact
3afb0 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36  ers..**.** {H166
3afc0 30 36 7d 20 53 75 63 63 65 73 73 69 76 65 20 63  06} Successive c
3afd0 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
3afe0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
3aff0 6e 5f 76 32 28 42 2c 58 2c 45 2c 50 2c 46 2c 44  n_v2(B,X,E,P,F,D
3b000 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77  )].**          w
3b010 69 74 68 20 74 68 65 20 73 61 6d 65 20 76 61 6c  ith the same val
3b020 75 65 73 20 66 6f 72 20 42 2c 20 58 2c 20 61 6e  ues for B, X, an
3b030 64 20 45 2c 20 6f 76 65 72 72 69 64 65 20 70 72  d E, override pr
3b040 69 6f 72 20 76 61 6c 75 65 73 0a 2a 2a 20 20 20  ior values.**   
3b050 20 20 20 20 20 20 20 6f 66 20 50 2c 20 46 2c 20         of P, F, 
3b060 61 6e 64 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  and D..**.** {H1
3b070 36 36 30 39 7d 20 49 66 20 74 68 65 20 64 65 73  6609} If the des
3b080 74 72 75 63 74 6f 72 20 44 20 69 6e 20 5b 73 71  tructor D in [sq
3b090 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
3b0a0 6c 61 74 69 6f 6e 5f 76 32 28 42 2c 58 2c 45 2c  lation_v2(B,X,E,
3b0b0 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20 20 20 20 20  P,F,D)].**      
3b0c0 20 20 20 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20      is not NULL 
3b0d0 74 68 65 6e 20 69 74 20 69 73 20 63 61 6c 6c 65  then it is calle
3b0e0 64 20 77 69 74 68 20 61 72 67 75 6d 65 6e 74 20  d with argument 
3b0f0 50 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20  P when the.**   
3b100 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e 67         collating
3b110 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f   function is dro
3b120 70 70 65 64 20 62 79 20 53 51 4c 69 74 65 2e 0a  pped by SQLite..
3b130 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 32 7d 20 41  **.** {H16612} A
3b140 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74   collating funct
3b150 69 6f 6e 20 69 73 20 64 72 6f 70 70 65 64 20 77  ion is dropped w
3b160 68 65 6e 20 69 74 20 69 73 20 6f 76 65 72 6c 6f  hen it is overlo
3b170 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36  aded..**.** {H16
3b180 36 31 35 7d 20 41 20 63 6f 6c 6c 61 74 69 6e 67  615} A collating
3b190 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 72 6f   function is dro
3b1a0 70 70 65 64 20 77 68 65 6e 20 74 68 65 20 64 61  pped when the da
3b1b0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
3b1c0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69 73  n.**          is
3b1d0 20 63 6c 6f 73 65 64 20 75 73 69 6e 67 20 5b 73   closed using [s
3b1e0 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 5d 2e  qlite3_close()].
3b1f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 31 38 7d 20  .**.** {H16618} 
3b200 54 68 65 20 70 6f 69 6e 74 65 72 20 50 20 69 6e  The pointer P in
3b210 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65   [sqlite3_create
3b220 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42 2c  _collation_v2(B,
3b230 58 2c 45 2c 50 2c 46 2c 44 29 5d 0a 2a 2a 20 20  X,E,P,F,D)].**  
3b240 20 20 20 20 20 20 20 20 69 73 20 70 61 73 73 65          is passe
3b250 64 20 74 68 72 6f 75 67 68 20 61 73 20 74 68 65  d through as the
3b260 20 66 69 72 73 74 20 70 61 72 61 6d 65 74 65 72   first parameter
3b270 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69 73   to the comparis
3b280 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  on.**          f
3b290 75 6e 63 74 69 6f 6e 20 46 20 66 6f 72 20 61 6c  unction F for al
3b2a0 6c 20 73 75 62 73 65 71 75 65 6e 74 20 69 6e 76  l subsequent inv
3b2b0 6f 63 61 74 69 6f 6e 73 20 6f 66 20 46 2e 0a 2a  ocations of F..*
3b2c0 2a 0a 2a 2a 20 7b 48 31 36 36 32 31 7d 20 41 20  *.** {H16621} A 
3b2d0 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33  call to [sqlite3
3b2e0 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
3b2f0 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 69 73  n(B,X,E,P,F)] is
3b300 20 65 78 61 63 74 6c 79 0a 2a 2a 20 20 20 20 20   exactly.**     
3b310 20 20 20 20 20 74 68 65 20 73 61 6d 65 20 61 73       the same as
3b320 20 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69   a call to [sqli
3b330 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3b340 74 69 6f 6e 5f 76 32 28 29 5d 20 77 69 74 68 0a  tion_v2()] with.
3b350 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20  **          the 
3b360 73 61 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20  same parameters 
3b370 61 6e 64 20 61 20 4e 55 4c 4c 20 64 65 73 74 72  and a NULL destr
3b380 75 63 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  uctor..**.** {H1
3b390 36 36 32 34 7d 20 46 6f 6c 6c 6f 77 69 6e 67 20  6624} Following 
3b3a0 61 20 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74  a [sqlite3_creat
3b3b0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 42  e_collation_v2(B
3b3c0 2c 58 2c 45 2c 50 2c 46 2c 44 29 5d 2c 0a 2a 2a  ,X,E,P,F,D)],.**
3b3d0 20 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65            SQLite
3b3e0 20 75 73 65 73 20 74 68 65 20 63 6f 6d 70 61 72   uses the compar
3b3f0 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 20 46 20  ison function F 
3b400 66 6f 72 20 61 6c 6c 20 74 65 78 74 20 63 6f 6d  for all text com
3b410 70 61 72 69 73 6f 6e 0a 2a 2a 20 20 20 20 20 20  parison.**      
3b420 20 20 20 20 6f 70 65 72 61 74 69 6f 6e 73 20 6f      operations o
3b430 6e 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  n the [database 
3b440 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 42 20 6f 6e  connection] B on
3b450 20 74 65 78 74 20 76 61 6c 75 65 73 20 74 68 61   text values tha
3b460 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73  t.**          us
3b470 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20  e the collating 
3b480 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20 58  sequence named X
3b490 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 36 36 32 37 7d  ..**.** {H16627}
3b4a0 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 72   The [sqlite3_cr
3b4b0 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
3b4c0 28 42 2c 58 2c 45 2c 50 2c 46 29 5d 20 77 6f 72  (B,X,E,P,F)] wor
3b4d0 6b 73 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20  ks the same.**  
3b4e0 20 20 20 20 20 20 20 20 61 73 20 5b 73 71 6c 69          as [sqli
3b4f0 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3b500 74 69 6f 6e 28 42 2c 58 2c 45 2c 50 2c 46 29 5d  tion(B,X,E,P,F)]
3b510 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65   except that the
3b520 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6c  .**          col
3b530 6c 61 74 69 6f 6e 20 6e 61 6d 65 20 58 20 69 73  lation name X is
3b540 20 75 6e 64 65 72 73 74 6f 6f 64 20 61 73 20 55   understood as U
3b550 54 46 2d 31 36 20 69 6e 20 6e 61 74 69 76 65 20  TF-16 in native 
3b560 62 79 74 65 20 6f 72 64 65 72 0a 2a 2a 20 20 20  byte order.**   
3b570 20 20 20 20 20 20 20 69 6e 73 74 65 61 64 20 6f         instead o
3b580 66 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 7b  f UTF-8..**.** {
3b590 48 31 36 36 33 30 7d 20 57 68 65 6e 20 6d 75 6c  H16630} When mul
3b5a0 74 69 70 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e  tiple comparison
3b5b0 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 61   functions are a
3b5c0 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 74 68 65  vailable for the
3b5d0 20 73 61 6d 65 0a 2a 2a 20 20 20 20 20 20 20 20   same.**        
3b5e0 20 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75    collating sequ
3b5f0 65 6e 63 65 2c 20 53 51 4c 69 74 65 20 63 68 6f  ence, SQLite cho
3b600 6f 73 65 73 20 74 68 65 20 6f 6e 65 20 77 68 6f  oses the one who
3b610 73 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  se text encoding
3b620 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 72 65 71  .**          req
3b630 75 69 72 65 73 20 74 68 65 20 6c 65 61 73 74 20  uires the least 
3b640 61 6d 6f 75 6e 74 20 6f 66 20 63 6f 6e 76 65 72  amount of conver
3b650 73 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 64 65  sion from the de
3b660 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20 20 20 20  fault.**        
3b670 20 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20    text encoding 
3b680 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
3b690 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
3b6a0 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
3b6b0 65 5f 63 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20 73  e_collation(.  s
3b6c0 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e 73  qlite3*, .  cons
3b6d0 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a  t char *zName, .
3b6e0 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20    int eTextRep, 
3b6f0 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28  .  void*,.  int(
3b700 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
3b710 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3b720 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3b730 29 0a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  ).);.SQLITE_API 
3b740 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61  int sqlite3_crea
3b750 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28  te_collation_v2(
3b760 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20  .  sqlite3*, .  
3b770 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
3b780 65 2c 20 0a 20 20 69 6e 74 20 65 54 65 78 74 52  e, .  int eTextR
3b790 65 70 2c 20 0a 20 20 76 6f 69 64 2a 2c 0a 20 20  ep, .  void*,.  
3b7a0 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29 28 76  int(*xCompare)(v
3b7b0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
3b7c0 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  oid*,int,const v
3b7d0 6f 69 64 2a 29 2c 0a 20 20 76 6f 69 64 28 2a 78  oid*),.  void(*x
3b7e0 44 65 73 74 72 6f 79 29 28 76 6f 69 64 2a 29 0a  Destroy)(void*).
3b7f0 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  );.SQLITE_API in
3b800 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  t sqlite3_create
3b810 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28 0a 20 20  _collation16(.  
3b820 73 71 6c 69 74 65 33 2a 2c 20 0a 20 20 63 6f 6e  sqlite3*, .  con
3b830 73 74 20 76 6f 69 64 20 2a 7a 4e 61 6d 65 2c 0a  st void *zName,.
3b840 20 20 69 6e 74 20 65 54 65 78 74 52 65 70 2c 20    int eTextRep, 
3b850 0a 20 20 76 6f 69 64 2a 2c 0a 20 20 69 6e 74 28  .  void*,.  int(
3b860 2a 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a  *xCompare)(void*
3b870 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3b880 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ,int,const void*
3b890 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ).);../*.** CAPI
3b8a0 33 52 45 46 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20  3REF: Collation 
3b8b0 4e 65 65 64 65 64 20 43 61 6c 6c 62 61 63 6b 73  Needed Callbacks
3b8c0 20 7b 48 31 36 37 30 30 7d 20 3c 53 32 30 33 30   {H16700} <S2030
3b8d0 30 3e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 76 6f 69  0>.**.** To avoi
3b8e0 64 20 68 61 76 69 6e 67 20 74 6f 20 72 65 67 69  d having to regi
3b8f0 73 74 65 72 20 61 6c 6c 20 63 6f 6c 6c 61 74 69  ster all collati
3b900 6f 6e 20 73 65 71 75 65 6e 63 65 73 20 62 65 66  on sequences bef
3b910 6f 72 65 20 61 20 64 61 74 61 62 61 73 65 0a 2a  ore a database.*
3b920 2a 20 63 61 6e 20 62 65 20 75 73 65 64 2c 20 61  * can be used, a
3b930 20 73 69 6e 67 6c 65 20 63 61 6c 6c 62 61 63 6b   single callback
3b940 20 66 75 6e 63 74 69 6f 6e 20 6d 61 79 20 62 65   function may be
3b950 20 72 65 67 69 73 74 65 72 65 64 20 77 69 74 68   registered with
3b960 20 74 68 65 0a 2a 2a 20 5b 64 61 74 61 62 61 73   the.** [databas
3b970 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 6f  e connection] to
3b980 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 65   be called whene
3b990 76 65 72 20 61 6e 20 75 6e 64 65 66 69 6e 65 64  ver an undefined
3b9a0 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20 73 65   collation.** se
3b9b0 71 75 65 6e 63 65 20 69 73 20 72 65 71 75 69 72  quence is requir
3b9c0 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
3b9d0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 72 65 67   function is reg
3b9e0 69 73 74 65 72 65 64 20 75 73 69 6e 67 20 74 68  istered using th
3b9f0 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  e sqlite3_collat
3ba00 69 6f 6e 5f 6e 65 65 64 65 64 28 29 20 41 50 49  ion_needed() API
3ba10 2c 0a 2a 2a 20 74 68 65 6e 20 69 74 20 69 73 20  ,.** then it is 
3ba20 70 61 73 73 65 64 20 74 68 65 20 6e 61 6d 65 73  passed the names
3ba30 20 6f 66 20 75 6e 64 65 66 69 6e 65 64 20 63 6f   of undefined co
3ba40 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
3ba50 73 20 61 73 20 73 74 72 69 6e 67 73 0a 2a 2a 20  s as strings.** 
3ba60 65 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38  encoded in UTF-8
3ba70 2e 20 7b 48 31 36 37 30 33 7d 20 49 66 20 73 71  . {H16703} If sq
3ba80 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f  lite3_collation_
3ba90 6e 65 65 64 65 64 31 36 28 29 20 69 73 20 75 73  needed16() is us
3baa0 65 64 2c 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 73  ed,.** the names
3bab0 20 61 72 65 20 70 61 73 73 65 64 20 61 73 20 55   are passed as U
3bac0 54 46 2d 31 36 20 69 6e 20 6d 61 63 68 69 6e 65  TF-16 in machine
3bad0 20 6e 61 74 69 76 65 20 62 79 74 65 20 6f 72 64   native byte ord
3bae0 65 72 2e 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f  er..** A call to
3baf0 20 65 69 74 68 65 72 20 66 75 6e 63 74 69 6f 6e   either function
3bb00 20 72 65 70 6c 61 63 65 73 20 61 6e 79 20 65 78   replaces any ex
3bb10 69 73 74 69 6e 67 20 63 61 6c 6c 62 61 63 6b 2e  isting callback.
3bb20 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20  .**.** When the 
3bb30 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
3bb40 6b 65 64 2c 20 74 68 65 20 66 69 72 73 74 20 61  ked, the first a
3bb50 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64 20 69  rgument passed i
3bb60 73 20 61 20 63 6f 70 79 0a 2a 2a 20 6f 66 20 74  s a copy.** of t
3bb70 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
3bb80 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 63 6f  nt to sqlite3_co
3bb90 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 28 29  llation_needed()
3bba0 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 63   or.** sqlite3_c
3bbb0 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31  ollation_needed1
3bbc0 36 28 29 2e 20 20 54 68 65 20 73 65 63 6f 6e 64  6().  The second
3bbd0 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65   argument is the
3bbe0 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63 6f 6e   database.** con
3bbf0 6e 65 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 68  nection.  The th
3bc00 69 72 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20  ird argument is 
3bc10 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45 5f 55  one of [SQLITE_U
3bc20 54 46 38 5d 2c 20 5b 53 51 4c 49 54 45 5f 55 54  TF8], [SQLITE_UT
3bc30 46 31 36 42 45 5d 2c 0a 2a 2a 20 6f 72 20 5b 53  F16BE],.** or [S
3bc40 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 5d 2c 20  QLITE_UTF16LE], 
3bc50 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65 20 6d  indicating the m
3bc60 6f 73 74 20 64 65 73 69 72 61 62 6c 65 20 66 6f  ost desirable fo
3bc70 72 6d 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74  rm of the collat
3bc80 69 6f 6e 0a 2a 2a 20 73 65 71 75 65 6e 63 65 20  ion.** sequence 
3bc90 66 75 6e 63 74 69 6f 6e 20 72 65 71 75 69 72 65  function require
3bca0 64 2e 20 20 54 68 65 20 66 6f 75 72 74 68 20 70  d.  The fourth p
3bcb0 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
3bcc0 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20 72  name of the.** r
3bcd0 65 71 75 69 72 65 64 20 63 6f 6c 6c 61 74 69 6f  equired collatio
3bce0 6e 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a 0a 2a  n sequence..**.*
3bcf0 2a 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  * The callback f
3bd00 75 6e 63 74 69 6f 6e 20 73 68 6f 75 6c 64 20 72  unction should r
3bd10 65 67 69 73 74 65 72 20 74 68 65 20 64 65 73 69  egister the desi
3bd20 72 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 75 73  red collation us
3bd30 69 6e 67 0a 2a 2a 20 5b 73 71 6c 69 74 65 33 5f  ing.** [sqlite3_
3bd40 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e  create_collation
3bd50 28 29 5d 2c 20 5b 73 71 6c 69 74 65 33 5f 63 72  ()], [sqlite3_cr
3bd60 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36  eate_collation16
3bd70 28 29 5d 2c 20 6f 72 0a 2a 2a 20 5b 73 71 6c 69  ()], or.** [sqli
3bd80 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
3bd90 74 69 6f 6e 5f 76 32 28 29 5d 2e 0a 2a 2a 0a 2a  tion_v2()]..**.*
3bda0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
3bdb0 0a 2a 2a 20 7b 48 31 36 37 30 32 7d 20 41 20 73  .** {H16702} A s
3bdc0 75 63 63 65 73 73 66 75 6c 20 63 61 6c 6c 20 74  uccessful call t
3bdd0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61  o [sqlite3_colla
3bde0 74 69 6f 6e 5f 6e 65 65 64 65 64 28 44 2c 50 2c  tion_needed(D,P,
3bdf0 46 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  F)].**          
3be00 6f 72 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6c 6c  or [sqlite3_coll
3be10 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 28 44  ation_needed16(D
3be20 2c 50 2c 46 29 5d 20 63 61 75 73 65 73 0a 2a 2a  ,P,F)] causes.**
3be30 20 20 20 20 20 20 20 20 20 20 74 68 65 20 5b 64            the [d
3be40 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3be50 6f 6e 5d 20 44 20 74 6f 20 69 6e 76 6f 6b 65 20  on] D to invoke 
3be60 63 61 6c 6c 62 61 63 6b 20 46 20 77 69 74 68 20  callback F with 
3be70 66 69 72 73 74 0a 2a 2a 20 20 20 20 20 20 20 20  first.**        
3be80 20 20 70 61 72 61 6d 65 74 65 72 20 50 20 77 68    parameter P wh
3be90 65 6e 65 76 65 72 20 69 74 20 6e 65 65 64 73 20  enever it needs 
3bea0 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e  a comparison fun
3beb0 63 74 69 6f 6e 20 66 6f 72 20 61 0a 2a 2a 20 20  ction for a.**  
3bec0 20 20 20 20 20 20 20 20 63 6f 6c 6c 61 74 69 6e          collatin
3bed0 67 20 73 65 71 75 65 6e 63 65 20 74 68 61 74 20  g sequence that 
3bee0 69 74 20 64 6f 65 73 20 6e 6f 74 20 6b 6e 6f 77  it does not know
3bef0 20 61 62 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48   about..**.** {H
3bf00 31 36 37 30 34 7d 20 45 61 63 68 20 73 75 63 63  16704} Each succ
3bf10 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 5b  essful call to [
3bf20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
3bf30 6e 5f 6e 65 65 64 65 64 28 29 5d 20 6f 72 0a 2a  n_needed()] or.*
3bf40 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
3bf50 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
3bf60 65 64 65 64 31 36 28 29 5d 20 6f 76 65 72 72 69  eded16()] overri
3bf70 64 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  des the callback
3bf80 20 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20   registered.**  
3bf90 20 20 20 20 20 20 20 20 6f 6e 20 74 68 65 20 73          on the s
3bfa0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ame [database co
3bfb0 6e 6e 65 63 74 69 6f 6e 5d 20 62 79 20 70 72 69  nnection] by pri
3bfc0 6f 72 20 63 61 6c 6c 73 20 74 6f 20 65 69 74 68  or calls to eith
3bfd0 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  er.**          i
3bfe0 6e 74 65 72 66 61 63 65 2e 0a 2a 2a 0a 2a 2a 20  nterface..**.** 
3bff0 7b 48 31 36 37 30 36 7d 20 54 68 65 20 6e 61 6d  {H16706} The nam
3c000 65 20 6f 66 20 74 68 65 20 72 65 71 75 65 73 74  e of the request
3c010 65 64 20 63 6f 6c 6c 61 74 69 6e 67 20 66 75 6e  ed collating fun
3c020 63 74 69 6f 6e 20 70 61 73 73 65 64 20 69 6e 20  ction passed in 
3c030 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
3c040 34 74 68 20 70 61 72 61 6d 65 74 65 72 20 74 6f  4th parameter to
3c050 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73   the callback is
3c060 20 69 6e 20 55 54 46 2d 38 20 69 66 20 74 68 65   in UTF-8 if the
3c070 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20   callback.**    
3c080 20 20 20 20 20 20 77 61 73 20 72 65 67 69 73 74        was regist
3c090 65 72 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69  ered using [sqli
3c0a0 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65  te3_collation_ne
3c0b0 65 64 65 64 28 29 5d 20 61 6e 64 0a 2a 2a 20 20  eded()] and.**  
3c0c0 20 20 20 20 20 20 20 20 69 73 20 69 6e 20 55 54          is in UT
3c0d0 46 2d 31 36 20 6e 61 74 69 76 65 20 62 79 74 65  F-16 native byte
3c0e0 20 6f 72 64 65 72 20 69 66 20 74 68 65 20 63 61   order if the ca
3c0f0 6c 6c 62 61 63 6b 20 77 61 73 0a 2a 2a 20 20 20  llback was.**   
3c100 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65         registere
3c110 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74 65 33  d using [sqlite3
3c120 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64 65  _collation_neede
3c130 64 31 36 28 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  d16()]..*/.SQLIT
3c140 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3c150 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65 64  3_collation_need
3c160 65 64 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20  ed(.  sqlite3*, 
3c170 0a 20 20 76 6f 69 64 2a 2c 20 0a 20 20 76 6f 69  .  void*, .  voi
3c180 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d(*)(void*,sqlit
3c190 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
3c1a0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 0a 29 3b  ,const char*).);
3c1b0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
3c1c0 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
3c1d0 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71  n_needed16(.  sq
3c1e0 6c 69 74 65 33 2a 2c 20 0a 20 20 76 6f 69 64 2a  lite3*, .  void*
3c1f0 2c 0a 20 20 76 6f 69 64 28 2a 29 28 76 6f 69 64  ,.  void(*)(void
3c200 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
3c210 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
3c220 69 64 2a 29 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53  id*).);../*.** S
3c230 70 65 63 69 66 79 20 74 68 65 20 6b 65 79 20 66  pecify the key f
3c240 6f 72 20 61 6e 20 65 6e 63 72 79 70 74 65 64 20  or an encrypted 
3c250 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20  database.  This 
3c260 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 62  routine should b
3c270 65 0a 2a 2a 20 63 61 6c 6c 65 64 20 72 69 67 68  e.** called righ
3c280 74 20 61 66 74 65 72 20 73 71 6c 69 74 65 33 5f  t after sqlite3_
3c290 6f 70 65 6e 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68  open()..**.** Th
3c2a0 65 20 63 6f 64 65 20 74 6f 20 69 6d 70 6c 65 6d  e code to implem
3c2b0 65 6e 74 20 74 68 69 73 20 41 50 49 20 69 73 20  ent this API is 
3c2c0 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 69 6e  not available in
3c2d0 20 74 68 65 20 70 75 62 6c 69 63 20 72 65 6c 65   the public rele
3c2e0 61 73 65 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65  ase.** of SQLite
3c2f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
3c300 69 6e 74 20 73 71 6c 69 74 65 33 5f 6b 65 79 28  int sqlite3_key(
3c310 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
3c320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3c330 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f    /* Database to
3c340 20 62 65 20 72 65 6b 65 79 65 64 20 2a 2f 0a 20   be rekeyed */. 
3c350 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
3c360 79 2c 20 69 6e 74 20 6e 4b 65 79 20 20 20 20 20  y, int nKey     
3c370 2f 2a 20 54 68 65 20 6b 65 79 20 2a 2f 0a 29 3b  /* The key */.);
3c380 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
3c390 68 65 20 6b 65 79 20 6f 6e 20 61 6e 20 6f 70 65  he key on an ope
3c3a0 6e 20 64 61 74 61 62 61 73 65 2e 20 20 49 66 20  n database.  If 
3c3b0 74 68 65 20 63 75 72 72 65 6e 74 20 64 61 74 61  the current data
3c3c0 62 61 73 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 65  base is not.** e
3c3d0 6e 63 72 79 70 74 65 64 2c 20 74 68 69 73 20 72  ncrypted, this r
3c3e0 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 6e 63 72  outine will encr
3c3f0 79 70 74 20 69 74 2e 20 20 49 66 20 70 4e 65 77  ypt it.  If pNew
3c400 3d 3d 30 20 6f 72 20 6e 4e 65 77 3d 3d 30 2c 20  ==0 or nNew==0, 
3c410 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
3c420 69 73 20 64 65 63 72 79 70 74 65 64 2e 0a 2a 2a  is decrypted..**
3c430 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 74 6f 20  .** The code to 
3c440 69 6d 70 6c 65 6d 65 6e 74 20 74 68 69 73 20 41  implement this A
3c450 50 49 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61  PI is not availa
3c460 62 6c 65 20 69 6e 20 74 68 65 20 70 75 62 6c 69  ble in the publi
3c470 63 20 72 65 6c 65 61 73 65 0a 2a 2a 20 6f 66 20  c release.** of 
3c480 53 51 4c 69 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  SQLite..*/.SQLIT
3c490 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3c4a0 33 5f 72 65 6b 65 79 28 0a 20 20 73 71 6c 69 74  3_rekey(.  sqlit
3c4b0 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
3c4c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
3c4d0 61 62 61 73 65 20 74 6f 20 62 65 20 72 65 6b 65  abase to be reke
3c4e0 79 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  yed */.  const v
3c4f0 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e  oid *pKey, int n
3c500 4b 65 79 20 20 20 20 20 2f 2a 20 54 68 65 20 6e  Key     /* The n
3c510 65 77 20 6b 65 79 20 2a 2f 0a 29 3b 0a 0a 2f 2a  ew key */.);../*
3c520 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 53 75  .** CAPI3REF: Su
3c530 73 70 65 6e 64 20 45 78 65 63 75 74 69 6f 6e 20  spend Execution 
3c540 46 6f 72 20 41 20 53 68 6f 72 74 20 54 69 6d 65  For A Short Time
3c550 20 7b 48 31 30 35 33 30 7d 20 3c 53 34 30 34 31   {H10530} <S4041
3c560 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  0>.**.** The sql
3c570 69 74 65 33 5f 73 6c 65 65 70 28 29 20 66 75 6e  ite3_sleep() fun
3c580 63 74 69 6f 6e 20 63 61 75 73 65 73 20 74 68 65  ction causes the
3c590 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20   current thread 
3c5a0 74 6f 20 73 75 73 70 65 6e 64 20 65 78 65 63 75  to suspend execu
3c5b0 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 61 74 20 6c  tion.** for at l
3c5c0 65 61 73 74 20 61 20 6e 75 6d 62 65 72 20 6f 66  east a number of
3c5d0 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 73 70   milliseconds sp
3c5e0 65 63 69 66 69 65 64 20 69 6e 20 69 74 73 20 70  ecified in its p
3c5f0 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 0a 2a 2a 20  arameter..**.** 
3c600 49 66 20 74 68 65 20 6f 70 65 72 61 74 69 6e 67  If the operating
3c610 20 73 79 73 74 65 6d 20 64 6f 65 73 20 6e 6f 74   system does not
3c620 20 73 75 70 70 6f 72 74 20 73 6c 65 65 70 20 72   support sleep r
3c630 65 71 75 65 73 74 73 20 77 69 74 68 0a 2a 2a 20  equests with.** 
3c640 6d 69 6c 6c 69 73 65 63 6f 6e 64 20 74 69 6d 65  millisecond time
3c650 20 72 65 73 6f 6c 75 74 69 6f 6e 2c 20 74 68 65   resolution, the
3c660 6e 20 74 68 65 20 74 69 6d 65 20 77 69 6c 6c 20  n the time will 
3c670 62 65 20 72 6f 75 6e 64 65 64 20 75 70 20 74 6f  be rounded up to
3c680 0a 2a 2a 20 74 68 65 20 6e 65 61 72 65 73 74 20  .** the nearest 
3c690 73 65 63 6f 6e 64 2e 20 54 68 65 20 6e 75 6d 62  second. The numb
3c6a0 65 72 20 6f 66 20 6d 69 6c 6c 69 73 65 63 6f 6e  er of millisecon
3c6b0 64 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75  ds of sleep actu
3c6c0 61 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65  ally.** requeste
3c6d0 64 20 66 72 6f 6d 20 74 68 65 20 6f 70 65 72 61  d from the opera
3c6e0 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20 72  ting system is r
3c6f0 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 53  eturned..**.** S
3c700 51 4c 69 74 65 20 69 6d 70 6c 65 6d 65 6e 74 73  QLite implements
3c710 20 74 68 69 73 20 69 6e 74 65 72 66 61 63 65 20   this interface 
3c720 62 79 20 63 61 6c 6c 69 6e 67 20 74 68 65 20 78  by calling the x
3c730 53 6c 65 65 70 28 29 0a 2a 2a 20 6d 65 74 68 6f  Sleep().** metho
3c740 64 20 6f 66 20 74 68 65 20 64 65 66 61 75 6c 74  d of the default
3c750 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
3c760 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  bject..**.** INV
3c770 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
3c780 48 31 30 35 33 33 7d 20 54 68 65 20 5b 73 71 6c  H10533} The [sql
3c790 69 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69  ite3_sleep(M)] i
3c7a0 6e 74 65 72 66 61 63 65 20 69 6e 76 6f 6b 65 73  nterface invokes
3c7b0 20 74 68 65 20 78 53 6c 65 65 70 0a 2a 2a 20 20   the xSleep.**  
3c7c0 20 20 20 20 20 20 20 20 6d 65 74 68 6f 64 20 6f          method o
3c7d0 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b 73  f the default [s
3c7e0 71 6c 69 74 65 33 5f 76 66 73 7c 56 46 53 5d 20  qlite3_vfs|VFS] 
3c7f0 69 6e 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 20  in order to.**  
3c800 20 20 20 20 20 20 20 20 73 75 73 70 65 6e 64 20          suspend 
3c810 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 65  execution of the
3c820 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64 20   current thread 
3c830 66 6f 72 20 61 74 20 6c 65 61 73 74 0a 2a 2a 20  for at least.** 
3c840 20 20 20 20 20 20 20 20 20 4d 20 6d 69 6c 6c 69           M milli
3c850 73 65 63 6f 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 7b  seconds..**.** {
3c860 48 31 30 35 33 36 7d 20 54 68 65 20 5b 73 71 6c  H10536} The [sql
3c870 69 74 65 33 5f 73 6c 65 65 70 28 4d 29 5d 20 69  ite3_sleep(M)] i
3c880 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3c890 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
3c8a0 2a 20 20 20 20 20 20 20 20 20 20 6d 69 6c 6c 69  *          milli
3c8b0 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65 70  seconds of sleep
3c8c0 20 61 63 74 75 61 6c 6c 79 20 72 65 71 75 65 73   actually reques
3c8d0 74 65 64 20 6f 66 20 74 68 65 20 6f 70 65 72 61  ted of the opera
3c8e0 74 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20  ting.**         
3c8f0 20 73 79 73 74 65 6d 2c 20 77 68 69 63 68 20 6d   system, which m
3c900 69 67 68 74 20 62 65 20 6c 61 72 67 65 72 20 74  ight be larger t
3c910 68 61 6e 20 74 68 65 20 70 61 72 61 6d 65 74 65  han the paramete
3c920 72 20 4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  r M..*/.SQLITE_A
3c930 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  PI int sqlite3_s
3c940 6c 65 65 70 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a  leep(int);../*.*
3c950 2a 20 43 41 50 49 33 52 45 46 3a 20 4e 61 6d 65  * CAPI3REF: Name
3c960 20 4f 66 20 54 68 65 20 46 6f 6c 64 65 72 20 48   Of The Folder H
3c970 6f 6c 64 69 6e 67 20 54 65 6d 70 6f 72 61 72 79  olding Temporary
3c980 20 46 69 6c 65 73 20 7b 48 31 30 33 31 30 7d 20   Files {H10310} 
3c990 3c 53 32 30 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 49  <S20000>.**.** I
3c9a0 66 20 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61  f this global va
3c9b0 72 69 61 62 6c 65 20 69 73 20 6d 61 64 65 20 74  riable is made t
3c9c0 6f 20 70 6f 69 6e 74 20 74 6f 20 61 20 73 74 72  o point to a str
3c9d0 69 6e 67 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  ing which is.** 
3c9e0 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 66 6f  the name of a fo
3c9f0 6c 64 65 72 20 28 61 2e 6b 2e 61 2e 20 64 69 72  lder (a.k.a. dir
3ca00 65 63 74 6f 72 79 29 2c 20 74 68 65 6e 20 61 6c  ectory), then al
3ca10 6c 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  l temporary file
3ca20 73 0a 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20  s.** created by 
3ca30 53 51 4c 69 74 65 20 77 69 6c 6c 20 62 65 20 70  SQLite will be p
3ca40 6c 61 63 65 64 20 69 6e 20 74 68 61 74 20 64 69  laced in that di
3ca50 72 65 63 74 6f 72 79 2e 20 20 49 66 20 74 68 69  rectory.  If thi
3ca60 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 69 73  s variable.** is
3ca70 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
3ca80 20 74 68 65 6e 20 53 51 4c 69 74 65 20 70 65 72   then SQLite per
3ca90 66 6f 72 6d 73 20 61 20 73 65 61 72 63 68 20 66  forms a search f
3caa0 6f 72 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  or an appropriat
3cab0 65 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66  e.** temporary f
3cac0 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a  ile directory..*
3cad0 2a 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 73  *.** It is not s
3cae0 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  afe to modify th
3caf0 69 73 20 76 61 72 69 61 62 6c 65 20 6f 6e 63 65  is variable once
3cb00 20 61 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e   a [database con
3cb10 6e 65 63 74 69 6f 6e 5d 0a 2a 2a 20 68 61 73 20  nection].** has 
3cb20 62 65 65 6e 20 6f 70 65 6e 65 64 2e 20 20 49 74  been opened.  It
3cb30 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74 68 61   is intended tha
3cb40 74 20 74 68 69 73 20 76 61 72 69 61 62 6c 65 20  t this variable 
3cb50 62 65 20 73 65 74 20 6f 6e 63 65 0a 2a 2a 20 61  be set once.** a
3cb60 73 20 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73  s part of proces
3cb70 73 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  s initialization
3cb80 20 61 6e 64 20 62 65 66 6f 72 65 20 61 6e 79 20   and before any 
3cb90 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61 63 65  SQLite interface
3cba0 0a 2a 2a 20 72 6f 75 74 69 6e 65 73 20 68 61 76  .** routines hav
3cbb0 65 20 62 65 65 6e 20 63 61 6c 6c 20 61 6e 64 20  e been call and 
3cbc0 72 65 6d 61 69 6e 20 75 6e 63 68 61 6e 67 65 64  remain unchanged
3cbd0 20 74 68 65 72 65 61 66 74 65 72 2e 0a 2a 2f 0a   thereafter..*/.
3cbe0 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20  SQLITE_API char 
3cbf0 2a 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69  *sqlite3_temp_di
3cc00 72 65 63 74 6f 72 79 3b 0a 0a 2f 2a 0a 2a 2a 20  rectory;../*.** 
3cc10 43 41 50 49 33 52 45 46 3a 20 54 65 73 74 20 46  CAPI3REF: Test F
3cc20 6f 72 20 41 75 74 6f 2d 43 6f 6d 6d 69 74 20 4d  or Auto-Commit M
3cc30 6f 64 65 20 7b 48 31 32 39 33 30 7d 20 3c 53 36  ode {H12930} <S6
3cc40 30 32 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44  0200>.** KEYWORD
3cc50 53 3a 20 7b 61 75 74 6f 63 6f 6d 6d 69 74 20 6d  S: {autocommit m
3cc60 6f 64 65 7d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  ode}.**.** The s
3cc70 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f 63  qlite3_get_autoc
3cc80 6f 6d 6d 69 74 28 29 20 69 6e 74 65 72 66 61 63  ommit() interfac
3cc90 65 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65  e returns non-ze
3cca0 72 6f 20 6f 72 0a 2a 2a 20 7a 65 72 6f 20 69 66  ro or.** zero if
3ccb0 20 74 68 65 20 67 69 76 65 6e 20 64 61 74 61 62   the given datab
3ccc0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
3ccd0 73 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61  s or is not in a
3cce0 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 2c 0a  utocommit mode,.
3ccf0 2a 2a 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  ** respectively.
3cd00 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64    Autocommit mod
3cd10 65 20 69 73 20 6f 6e 20 62 79 20 64 65 66 61 75  e is on by defau
3cd20 6c 74 2e 0a 2a 2a 20 41 75 74 6f 63 6f 6d 6d 69  lt..** Autocommi
3cd30 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61 62 6c  t mode is disabl
3cd40 65 64 20 62 79 20 61 20 5b 42 45 47 49 4e 5d 20  ed by a [BEGIN] 
3cd50 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 41 75  statement..** Au
3cd60 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 69 73  tocommit mode is
3cd70 20 72 65 2d 65 6e 61 62 6c 65 64 20 62 79 20 61   re-enabled by a
3cd80 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52 4f   [COMMIT] or [RO
3cd90 4c 4c 42 41 43 4b 5d 2e 0a 2a 2a 0a 2a 2a 20 49  LLBACK]..**.** I
3cda0 66 20 63 65 72 74 61 69 6e 20 6b 69 6e 64 73 20  f certain kinds 
3cdb0 6f 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 20  of errors occur 
3cdc0 6f 6e 20 61 20 73 74 61 74 65 6d 65 6e 74 20 77  on a statement w
3cdd0 69 74 68 69 6e 20 61 20 6d 75 6c 74 69 2d 73 74  ithin a multi-st
3cde0 61 74 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73  atement.** trans
3cdf0 61 63 74 69 6f 6e 20 28 65 72 72 6f 72 73 20 69  action (errors i
3ce00 6e 63 6c 75 64 69 6e 67 20 5b 53 51 4c 49 54 45  ncluding [SQLITE
3ce10 5f 46 55 4c 4c 5d 2c 20 5b 53 51 4c 49 54 45 5f  _FULL], [SQLITE_
3ce20 49 4f 45 52 52 5d 2c 0a 2a 2a 20 5b 53 51 4c 49  IOERR],.** [SQLI
3ce30 54 45 5f 4e 4f 4d 45 4d 5d 2c 20 5b 53 51 4c 49  TE_NOMEM], [SQLI
3ce40 54 45 5f 42 55 53 59 5d 2c 20 61 6e 64 20 5b 53  TE_BUSY], and [S
3ce50 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50 54 5d  QLITE_INTERRUPT]
3ce60 29 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 74 72  ) then the.** tr
3ce70 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68 74 20  ansaction might 
3ce80 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 61  be rolled back a
3ce90 75 74 6f 6d 61 74 69 63 61 6c 6c 79 2e 20 20 54  utomatically.  T
3cea0 68 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 0a 2a  he only way to.*
3ceb0 2a 20 66 69 6e 64 20 6f 75 74 20 77 68 65 74 68  * find out wheth
3cec0 65 72 20 53 51 4c 69 74 65 20 61 75 74 6f 6d 61  er SQLite automa
3ced0 74 69 63 61 6c 6c 79 20 72 6f 6c 6c 65 64 20 62  tically rolled b
3cee0 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74  ack the transact
3cef0 69 6f 6e 20 61 66 74 65 72 0a 2a 2a 20 61 6e 20  ion after.** an 
3cf00 65 72 72 6f 72 20 69 73 20 74 6f 20 75 73 65 20  error is to use 
3cf10 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  this function..*
3cf20 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
3cf30 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 33 31 7d 20  .**.** {H12931} 
3cf40 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 67 65 74  The [sqlite3_get
3cf50 5f 61 75 74 6f 63 6f 6d 6d 69 74 28 44 29 5d 20  _autocommit(D)] 
3cf60 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e  interface return
3cf70 73 20 6e 6f 6e 2d 7a 65 72 6f 20 6f 72 0a 2a 2a  s non-zero or.**
3cf80 20 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 69            zero i
3cf90 66 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20  f the [database 
3cfa0 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 20 69 73  connection] D is
3cfb0 20 6f 72 20 69 73 20 6e 6f 74 20 69 6e 20 61 75   or is not in au
3cfc0 74 6f 63 6f 6d 6d 69 74 0a 2a 2a 20 20 20 20 20  tocommit.**     
3cfd0 20 20 20 20 20 6d 6f 64 65 2c 20 72 65 73 70 65       mode, respe
3cfe0 63 74 69 76 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 7b  ctively..**.** {
3cff0 48 31 32 39 33 32 7d 20 41 75 74 6f 63 6f 6d 6d  H12932} Autocomm
3d000 69 74 20 6d 6f 64 65 20 69 73 20 6f 6e 20 62 79  it mode is on by
3d010 20 64 65 66 61 75 6c 74 2e 0a 2a 2a 0a 2a 2a 20   default..**.** 
3d020 7b 48 31 32 39 33 33 7d 20 41 75 74 6f 63 6f 6d  {H12933} Autocom
3d030 6d 69 74 20 6d 6f 64 65 20 69 73 20 64 69 73 61  mit mode is disa
3d040 62 6c 65 64 20 62 79 20 61 20 73 75 63 63 65 73  bled by a succes
3d050 73 66 75 6c 20 5b 42 45 47 49 4e 5d 20 73 74 61  sful [BEGIN] sta
3d060 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48  tement..**.** {H
3d070 31 32 39 33 34 7d 20 41 75 74 6f 63 6f 6d 6d 69  12934} Autocommi
3d080 74 20 6d 6f 64 65 20 69 73 20 65 6e 61 62 6c 65  t mode is enable
3d090 64 20 62 79 20 61 20 73 75 63 63 65 73 73 66 75  d by a successfu
3d0a0 6c 20 5b 43 4f 4d 4d 49 54 5d 20 6f 72 20 5b 52  l [COMMIT] or [R
3d0b0 4f 4c 4c 42 41 43 4b 5d 0a 2a 2a 20 20 20 20 20  OLLBACK].**     
3d0c0 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2e 0a       statement..
3d0d0 2a 2a 0a 2a 2a 20 41 53 53 55 4d 50 54 49 4f 4e  **.** ASSUMPTION
3d0e0 53 3a 0a 2a 2a 0a 2a 2a 20 7b 41 31 32 39 33 36  S:.**.** {A12936
3d0f0 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68 72  } If another thr
3d100 65 61 64 20 63 68 61 6e 67 65 73 20 74 68 65 20  ead changes the 
3d110 61 75 74 6f 63 6f 6d 6d 69 74 20 73 74 61 74 75  autocommit statu
3d120 73 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  s of the databas
3d130 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f  e.**          co
3d140 6e 6e 65 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nnection while t
3d150 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 72  his routine is r
3d160 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 20 74 68 65  unning, then the
3d170 20 72 65 74 75 72 6e 20 76 61 6c 75 65 0a 2a 2a   return value.**
3d180 20 20 20 20 20 20 20 20 20 20 69 73 20 75 6e 64            is und
3d190 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  efined..*/.SQLIT
3d1a0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
3d1b0 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69 74  3_get_autocommit
3d1c0 28 73 71 6c 69 74 65 33 2a 29 3b 0a 0a 2f 2a 0a  (sqlite3*);../*.
3d1d0 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 46 69 6e  ** CAPI3REF: Fin
3d1e0 64 20 54 68 65 20 44 61 74 61 62 61 73 65 20 48  d The Database H
3d1f0 61 6e 64 6c 65 20 4f 66 20 41 20 50 72 65 70 61  andle Of A Prepa
3d200 72 65 64 20 53 74 61 74 65 6d 65 6e 74 20 7b 48  red Statement {H
3d210 31 33 31 32 30 7d 20 3c 53 36 30 36 30 30 3e 0a  13120} <S60600>.
3d220 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
3d230 33 5f 64 62 5f 68 61 6e 64 6c 65 20 69 6e 74 65  3_db_handle inte
3d240 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68  rface returns th
3d250 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3d260 65 63 74 69 6f 6e 5d 20 68 61 6e 64 6c 65 0a 2a  ection] handle.*
3d270 2a 20 74 6f 20 77 68 69 63 68 20 61 20 5b 70 72  * to which a [pr
3d280 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3d290 5d 20 62 65 6c 6f 6e 67 73 2e 20 20 54 68 65 20  ] belongs.  The 
3d2a0 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20  database handle 
3d2b0 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
3d2c0 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
3d2d0 20 69 73 20 74 68 65 20 73 61 6d 65 20 64 61 74   is the same dat
3d2e0 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61  abase handle tha
3d2f0 74 20 77 61 73 20 74 68 65 20 66 69 72 73 74 20  t was the first 
3d300 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74  argument.** to t
3d310 68 65 20 5b 73 71 6c 69 74 65 33 5f 70 72 65 70  he [sqlite3_prep
3d320 61 72 65 5f 76 32 28 29 5d 20 63 61 6c 6c 20 28  are_v2()] call (
3d330 6f 72 20 69 74 73 20 76 61 72 69 61 6e 74 73 29  or its variants)
3d340 20 74 68 61 74 20 77 61 73 20 75 73 65 64 20 74   that was used t
3d350 6f 0a 2a 2a 20 63 72 65 61 74 65 20 74 68 65 20  o.** create the 
3d360 73 74 61 74 65 6d 65 6e 74 20 69 6e 20 74 68 65  statement in the
3d370 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a   first place..**
3d380 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3d390 2a 2a 0a 2a 2a 20 7b 48 31 33 31 32 33 7d 20 54  **.** {H13123} T
3d3a0 68 65 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f 68  he [sqlite3_db_h
3d3b0 61 6e 64 6c 65 28 53 29 5d 20 69 6e 74 65 72 66  andle(S)] interf
3d3c0 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
3d3d0 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  inter.**        
3d3e0 20 20 74 6f 20 74 68 65 20 5b 64 61 74 61 62 61    to the [databa
3d3f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61  se connection] a
3d400 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
3d410 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
3d420 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3d430 6e 74 5d 20 53 2e 0a 2a 2f 0a 53 51 4c 49 54 45  nt] S..*/.SQLITE
3d440 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73 71  _API sqlite3 *sq
3d450 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65 28  lite3_db_handle(
3d460 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a  sqlite3_stmt*);.
3d470 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
3d480 20 46 69 6e 64 20 74 68 65 20 6e 65 78 74 20 70   Find the next p
3d490 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
3d4a0 74 20 7b 48 31 33 31 34 30 7d 20 3c 53 36 30 36  t {H13140} <S606
3d4b0 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  00>.**.** This i
3d4c0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3d4d0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
3d4e0 65 20 6e 65 78 74 20 5b 70 72 65 70 61 72 65 64  e next [prepared
3d4f0 20 73 74 61 74 65 6d 65 6e 74 5d 20 61 66 74 65   statement] afte
3d500 72 0a 2a 2a 20 70 53 74 6d 74 20 61 73 73 6f 63  r.** pStmt assoc
3d510 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 5b  iated with the [
3d520 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
3d530 69 6f 6e 5d 20 70 44 62 2e 20 20 49 66 20 70 53  ion] pDb.  If pS
3d540 74 6d 74 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 74  tmt is NULL.** t
3d550 68 65 6e 20 74 68 69 73 20 69 6e 74 65 72 66 61  hen this interfa
3d560 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  ce returns a poi
3d570 6e 74 65 72 20 74 6f 20 74 68 65 20 66 69 72 73  nter to the firs
3d580 74 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  t prepared state
3d590 6d 65 6e 74 0a 2a 2a 20 61 73 73 6f 63 69 61 74  ment.** associat
3d5a0 65 64 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ed with the data
3d5b0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3d5c0 70 44 62 2e 20 20 49 66 20 6e 6f 20 70 72 65 70  pDb.  If no prep
3d5d0 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a  ared statement.*
3d5e0 2a 20 73 61 74 69 73 66 69 65 73 20 74 68 65 20  * satisfies the 
3d5f0 63 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 74 68  conditions of th
3d600 69 73 20 72 6f 75 74 69 6e 65 2c 20 69 74 20 72  is routine, it r
3d610 65 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a  eturns NULL..**.
3d620 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
3d630 2a 0a 2a 2a 20 7b 48 31 33 31 34 33 7d 20 49 66  *.** {H13143} If
3d640 20 44 20 69 73 20 61 20 5b 64 61 74 61 62 61 73   D is a [databas
3d650 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 74 68  e connection] th
3d660 61 74 20 68 6f 6c 64 73 20 6f 6e 65 20 6f 72 20  at holds one or 
3d670 6d 6f 72 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  more.**         
3d680 20 75 6e 66 69 6e 61 6c 69 7a 65 64 20 5b 70 72   unfinalized [pr
3d690 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
3d6a0 73 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55  s] and S is a NU
3d6b0 4c 4c 20 70 6f 69 6e 74 65 72 2c 0a 2a 2a 20 20  LL pointer,.**  
3d6c0 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73 71          then [sq
3d6d0 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28  lite3_next_stmt(
3d6e0 44 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73  D, S)] routine s
3d6f0 68 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 70 6f  hall return a po
3d700 69 6e 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20  inter.**        
3d710 20 20 74 6f 20 6f 6e 65 20 6f 66 20 74 68 65 20    to one of the 
3d720 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
3d730 6e 74 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  nts associated w
3d740 69 74 68 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  ith D..**.** {H1
3d750 33 31 34 36 7d 20 49 66 20 44 20 69 73 20 61 20  3146} If D is a 
3d760 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3d770 74 69 6f 6e 5d 20 74 68 61 74 20 68 6f 6c 64 73  tion] that holds
3d780 20 6e 6f 20 75 6e 66 69 6e 61 6c 69 7a 65 64 0a   no unfinalized.
3d790 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 70 72 65  **          [pre
3d7a0 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73  pared statements
3d7b0 5d 20 61 6e 64 20 53 20 69 73 20 61 20 4e 55 4c  ] and S is a NUL
3d7c0 4c 20 70 6f 69 6e 74 65 72 2c 20 74 68 65 6e 0a  L pointer, then.
3d7d0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
3d7e0 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44  ite3_next_stmt(D
3d7f0 2c 20 53 29 5d 20 72 6f 75 74 69 6e 65 20 73 68  , S)] routine sh
3d800 61 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c  all return a NUL
3d810 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
3d820 20 7b 48 31 33 31 34 39 7d 20 49 66 20 53 20 69   {H13149} If S i
3d830 73 20 61 20 5b 70 72 65 70 61 72 65 64 20 73 74  s a [prepared st
3d840 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68 65 20  atement] in the 
3d850 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3d860 74 69 6f 6e 5d 20 44 0a 2a 2a 20 20 20 20 20 20  tion] D.**      
3d870 20 20 20 20 61 6e 64 20 53 20 69 73 20 6e 6f 74      and S is not
3d880 20 74 68 65 20 6c 61 73 74 20 70 72 65 70 61 72   the last prepar
3d890 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  ed statement in 
3d8a0 44 2c 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20  D, then.**      
3d8b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6e 65 78      [sqlite3_nex
3d8c0 74 5f 73 74 6d 74 28 44 2c 20 53 29 5d 20 72 6f  t_stmt(D, S)] ro
3d8d0 75 74 69 6e 65 20 73 68 61 6c 6c 20 72 65 74 75  utine shall retu
3d8e0 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a 20  rn a pointer.** 
3d8f0 20 20 20 20 20 20 20 20 20 74 6f 20 74 68 65 20           to the 
3d900 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73 74  next prepared st
3d910 61 74 65 6d 65 6e 74 20 69 6e 20 44 20 61 66 74  atement in D aft
3d920 65 72 20 53 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 33  er S..**.** {H13
3d930 31 35 32 7d 20 49 66 20 53 20 69 73 20 74 68 65  152} If S is the
3d940 20 6c 61 73 74 20 5b 70 72 65 70 61 72 65 64 20   last [prepared 
3d950 73 74 61 74 65 6d 65 6e 74 5d 20 69 6e 20 74 68  statement] in th
3d960 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 64  e.**          [d
3d970 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3d980 6f 6e 5d 20 44 20 74 68 65 6e 20 74 68 65 20 5b  on] D then the [
3d990 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d  sqlite3_next_stm
3d9a0 74 28 44 2c 20 53 29 5d 0a 2a 2a 20 20 20 20 20  t(D, S)].**     
3d9b0 20 20 20 20 20 72 6f 75 74 69 6e 65 20 73 68 61       routine sha
3d9c0 6c 6c 20 72 65 74 75 72 6e 20 61 20 4e 55 4c 4c  ll return a NULL
3d9d0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
3d9e0 41 53 53 55 4d 50 54 49 4f 4e 53 3a 0a 2a 2a 0a  ASSUMPTIONS:.**.
3d9f0 2a 2a 20 7b 41 31 33 31 35 34 7d 20 54 68 65 20  ** {A13154} The 
3da00 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3da10 74 69 6f 6e 5d 20 70 6f 69 6e 74 65 72 20 44 20  tion] pointer D 
3da20 69 6e 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20  in a call to.** 
3da30 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65           [sqlite
3da40 33 5f 6e 65 78 74 5f 73 74 6d 74 28 44 2c 53 29  3_next_stmt(D,S)
3da50 5d 20 6d 75 73 74 20 72 65 66 65 72 20 74 6f 20  ] must refer to 
3da60 61 6e 20 6f 70 65 6e 20 64 61 74 61 62 61 73 65  an open database
3da70 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6e  .**          con
3da80 6e 65 63 74 69 6f 6e 20 61 6e 64 20 69 6e 20 70  nection and in p
3da90 61 72 74 69 63 75 6c 61 72 20 6d 75 73 74 20 6e  articular must n
3daa0 6f 74 20 62 65 20 61 20 4e 55 4c 4c 20 70 6f 69  ot be a NULL poi
3dab0 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  nter..*/.SQLITE_
3dac0 41 50 49 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  API sqlite3_stmt
3dad0 20 2a 73 71 6c 69 74 65 33 5f 6e 65 78 74 5f 73   *sqlite3_next_s
3dae0 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 70 44 62  tmt(sqlite3 *pDb
3daf0 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  , sqlite3_stmt *
3db00 70 53 74 6d 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  pStmt);../*.** C
3db10 41 50 49 33 52 45 46 3a 20 43 6f 6d 6d 69 74 20  API3REF: Commit 
3db20 41 6e 64 20 52 6f 6c 6c 62 61 63 6b 20 4e 6f 74  And Rollback Not
3db30 69 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61  ification Callba
3db40 63 6b 73 20 7b 48 31 32 39 35 30 7d 20 3c 53 36  cks {H12950} <S6
3db50 30 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0400>.**.** The 
3db60 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68  sqlite3_commit_h
3db70 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20  ook() interface 
3db80 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c  registers a call
3db90 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  back.** function
3dba0 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77   to be invoked w
3dbb0 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61  henever a transa
3dbc0 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74  ction is committ
3dbd0 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62  ed..** Any callb
3dbe0 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65  ack set by a pre
3dbf0 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71  vious call to sq
3dc00 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
3dc10 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  k().** for the s
3dc20 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
3dc30 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72  nection is overr
3dc40 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 73 71  idden..** The sq
3dc50 6c 69 74 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68  lite3_rollback_h
3dc60 6f 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20  ook() interface 
3dc70 72 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c  registers a call
3dc80 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  back.** function
3dc90 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64 20 77   to be invoked w
3dca0 68 65 6e 65 76 65 72 20 61 20 74 72 61 6e 73 61  henever a transa
3dcb0 63 74 69 6f 6e 20 69 73 20 63 6f 6d 6d 69 74 74  ction is committ
3dcc0 65 64 2e 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62  ed..** Any callb
3dcd0 61 63 6b 20 73 65 74 20 62 79 20 61 20 70 72 65  ack set by a pre
3dce0 76 69 6f 75 73 20 63 61 6c 6c 20 74 6f 20 73 71  vious call to sq
3dcf0 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f  lite3_commit_hoo
3dd00 6b 28 29 0a 2a 2a 20 66 6f 72 20 74 68 65 20 73  k().** for the s
3dd10 61 6d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e  ame database con
3dd20 6e 65 63 74 69 6f 6e 20 69 73 20 6f 76 65 72 72  nection is overr
3dd30 69 64 64 65 6e 2e 0a 2a 2a 20 54 68 65 20 70 41  idden..** The pA
3dd40 72 67 20 61 72 67 75 6d 65 6e 74 20 69 73 20 70  rg argument is p
3dd50 61 73 73 65 64 20 74 68 72 6f 75 67 68 20 74 6f  assed through to
3dd60 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 0a 2a   the callback..*
3dd70 2a 20 49 66 20 74 68 65 20 63 61 6c 6c 62 61 63  * If the callbac
3dd80 6b 20 6f 6e 20 61 20 63 6f 6d 6d 69 74 20 68 6f  k on a commit ho
3dd90 6f 6b 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75  ok function retu
3dda0 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a 2a  rns non-zero,.**
3ddb0 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74   then the commit
3ddc0 20 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e   is converted in
3ddd0 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a  to a rollback..*
3dde0 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20  *.** If another 
3ddf0 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65  function was pre
3de00 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72  viously register
3de10 65 64 2c 20 69 74 73 0a 2a 2a 20 70 41 72 67 20  ed, its.** pArg 
3de20 76 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65  value is returne
3de30 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55  d.  Otherwise NU
3de40 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
3de50 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 62 61  **.** The callba
3de60 63 6b 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ck implementatio
3de70 6e 20 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e  n must not do an
3de80 79 74 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c  ything that will
3de90 20 6d 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64   modify.** the d
3dea0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3deb0 6f 6e 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20  on that invoked 
3dec0 74 68 65 20 63 61 6c 6c 62 61 63 6b 2e 20 20 41  the callback.  A
3ded0 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74 6f  ny actions.** to
3dee0 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74 61   modify the data
3def0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
3df00 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65 64  must be deferred
3df10 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68 65   until after the
3df20 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20 6f  .** completion o
3df30 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 73  f the [sqlite3_s
3df40 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61 74  tep()] call that
3df50 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20 63   triggered the c
3df60 6f 6d 6d 69 74 0a 2a 2a 20 6f 72 20 72 6f 6c 6c  ommit.** or roll
3df70 62 61 63 6b 20 68 6f 6f 6b 20 69 6e 20 74 68 65  back hook in the
3df80 20 66 69 72 73 74 20 70 6c 61 63 65 2e 0a 2a 2a   first place..**
3df90 20 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69   Note that [sqli
3dfa0 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29  te3_prepare_v2()
3dfb0 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73  ] and [sqlite3_s
3dfc0 74 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69  tep()] both modi
3dfd0 66 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61  fy their.** data
3dfe0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
3dff0 20 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67   for the meaning
3e000 20 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20   of "modify" in 
3e010 74 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a  this paragraph..
3e020 2a 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 69 6e  **.** Registerin
3e030 67 20 61 20 4e 55 4c 4c 20 66 75 6e 63 74 69 6f  g a NULL functio
3e040 6e 20 64 69 73 61 62 6c 65 73 20 74 68 65 20 63  n disables the c
3e050 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 46  allback..**.** F
3e060 6f 72 20 74 68 65 20 70 75 72 70 6f 73 65 73 20  or the purposes 
3e070 6f 66 20 74 68 69 73 20 41 50 49 2c 20 61 20 74  of this API, a t
3e080 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 61  ransaction is sa
3e090 69 64 20 74 6f 20 68 61 76 65 20 62 65 65 6e 0a  id to have been.
3e0a0 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63 6b 20 69  ** rolled back i
3e0b0 66 20 61 6e 20 65 78 70 6c 69 63 69 74 20 22 52  f an explicit "R
3e0c0 4f 4c 4c 42 41 43 4b 22 20 73 74 61 74 65 6d 65  OLLBACK" stateme
3e0d0 6e 74 20 69 73 20 65 78 65 63 75 74 65 64 2c 20  nt is executed, 
3e0e0 6f 72 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f  or.** an error o
3e0f0 72 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61 75  r constraint cau
3e100 73 65 73 20 61 6e 20 69 6d 70 6c 69 63 69 74 20  ses an implicit 
3e110 72 6f 6c 6c 62 61 63 6b 20 74 6f 20 6f 63 63 75  rollback to occu
3e120 72 2e 0a 2a 2a 20 54 68 65 20 72 6f 6c 6c 62 61  r..** The rollba
3e130 63 6b 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 6e  ck callback is n
3e140 6f 74 20 69 6e 76 6f 6b 65 64 20 69 66 20 61 20  ot invoked if a 
3e150 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a  transaction is.*
3e160 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
3e170 72 6f 6c 6c 65 64 20 62 61 63 6b 20 62 65 63 61  rolled back beca
3e180 75 73 65 20 74 68 65 20 64 61 74 61 62 61 73 65  use the database
3e190 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20 63   connection is c
3e1a0 6c 6f 73 65 64 2e 0a 2a 2a 20 54 68 65 20 72 6f  losed..** The ro
3e1b0 6c 6c 62 61 63 6b 20 63 61 6c 6c 62 61 63 6b 20  llback callback 
3e1c0 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20 69  is not invoked i
3e1d0 66 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  f a transaction 
3e1e0 69 73 0a 2a 2a 20 72 6f 6c 6c 65 64 20 62 61 63  is.** rolled bac
3e1f0 6b 20 62 65 63 61 75 73 65 20 61 20 63 6f 6d 6d  k because a comm
3e200 69 74 20 63 61 6c 6c 62 61 63 6b 20 72 65 74 75  it callback retu
3e210 72 6e 65 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 0a 2a  rned non-zero..*
3e220 2a 20 3c 74 6f 64 6f 3e 20 43 68 65 63 6b 20 6f  * <todo> Check o
3e230 6e 20 74 68 69 73 20 3c 2f 74 6f 64 6f 3e 0a 2a  n this </todo>.*
3e240 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a  *.** INVARIANTS:
3e250 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 31 7d 20  .**.** {H12951} 
3e260 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d  The [sqlite3_com
3e270 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d  mit_hook(D,F,P)]
3e280 20 69 6e 74 65 72 66 61 63 65 20 72 65 67 69 73   interface regis
3e290 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  ters the.**     
3e2a0 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20 66 75       callback fu
3e2b0 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69  nction F to be i
3e2c0 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72 67 75  nvoked with argu
3e2d0 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65 72 0a  ment P whenever.
3e2e0 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20 74 72  **          a tr
3e2f0 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74  ansaction commit
3e300 73 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62 61  s on the [databa
3e310 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44  se connection] D
3e320 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 32 7d  ..**.** {H12952}
3e330 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 63 6f   The [sqlite3_co
3e340 6d 6d 69 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29  mmit_hook(D,F,P)
3e350 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  ] interface retu
3e360 72 6e 73 20 74 68 65 20 50 20 61 72 67 75 6d 65  rns the P argume
3e370 6e 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 66  nt.**          f
3e380 72 6f 6d 20 74 68 65 20 70 72 65 76 69 6f 75 73  rom the previous
3e390 20 63 61 6c 6c 20 77 69 74 68 20 74 68 65 20 73   call with the s
3e3a0 61 6d 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f  ame [database co
3e3b0 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 0a 2a 2a 20  nnection] D,.** 
3e3c0 20 20 20 20 20 20 20 20 20 6f 72 20 4e 55 4c 4c           or NULL
3e3d0 20 6f 6e 20 74 68 65 20 66 69 72 73 74 20 63 61   on the first ca
3e3e0 6c 6c 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ll for a particu
3e3f0 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e  lar database con
3e400 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a 0a 2a 2a  nection D..**.**
3e410 20 7b 48 31 32 39 35 33 7d 20 45 61 63 68 20 63   {H12953} Each c
3e420 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
3e430 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 29 5d 20 6f  commit_hook()] o
3e440 76 65 72 77 72 69 74 65 73 20 74 68 65 20 63 61  verwrites the ca
3e450 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20  llback.**       
3e460 20 20 20 72 65 67 69 73 74 65 72 65 64 20 62 79     registered by
3e470 20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2a   prior calls..**
3e480 0a 2a 2a 20 7b 48 31 32 39 35 34 7d 20 49 66 20  .** {H12954} If 
3e490 74 68 65 20 46 20 61 72 67 75 6d 65 6e 74 20 74  the F argument t
3e4a0 6f 20 5b 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69  o [sqlite3_commi
3e4b0 74 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 69  t_hook(D,F,P)] i
3e4c0 73 20 4e 55 4c 4c 0a 2a 2a 20 20 20 20 20 20 20  s NULL.**       
3e4d0 20 20 20 74 68 65 6e 20 74 68 65 20 63 6f 6d 6d     then the comm
3e4e0 69 74 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b  it hook callback
3e4f0 20 69 73 20 63 61 6e 63 65 6c 65 64 20 61 6e 64   is canceled and
3e500 20 6e 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20   no callback.** 
3e510 20 20 20 20 20 20 20 20 20 69 73 20 69 6e 76 6f           is invo
3e520 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e 73  ked when a trans
3e530 61 63 74 69 6f 6e 20 63 6f 6d 6d 69 74 73 2e 0a  action commits..
3e540 2a 2a 0a 2a 2a 20 7b 48 31 32 39 35 35 7d 20 49  **.** {H12955} I
3e550 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63 61 6c  f the commit cal
3e560 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
3e570 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20  n-zero then the 
3e580 63 6f 6d 6d 69 74 20 69 73 0a 2a 2a 20 20 20 20  commit is.**    
3e590 20 20 20 20 20 20 63 6f 6e 76 65 72 74 65 64 20        converted 
3e5a0 69 6e 74 6f 20 61 20 72 6f 6c 6c 62 61 63 6b 2e  into a rollback.
3e5b0 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 31 7d 20  .**.** {H12961} 
3e5c0 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 6f 6c  The [sqlite3_rol
3e5d0 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c 46 2c 50  lback_hook(D,F,P
3e5e0 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65 67  )] interface reg
3e5f0 69 73 74 65 72 73 20 74 68 65 0a 2a 2a 20 20 20  isters the.**   
3e600 20 20 20 20 20 20 20 63 61 6c 6c 62 61 63 6b 20         callback 
3e610 66 75 6e 63 74 69 6f 6e 20 46 20 74 6f 20 62 65  function F to be
3e620 20 69 6e 76 6f 6b 65 64 20 77 69 74 68 20 61 72   invoked with ar
3e630 67 75 6d 65 6e 74 20 50 20 77 68 65 6e 65 76 65  gument P wheneve
3e640 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 20  r.**          a 
3e650 74 72 61 6e 73 61 63 74 69 6f 6e 20 72 6f 6c 6c  transaction roll
3e660 73 20 62 61 63 6b 20 6f 6e 20 74 68 65 20 5b 64  s back on the [d
3e670 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3e680 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  on] D..**.** {H1
3e690 32 39 36 32 7d 20 54 68 65 20 5b 73 71 6c 69 74  2962} The [sqlit
3e6a0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
3e6b0 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61  (D,F,P)] interfa
3e6c0 63 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 50  ce returns the P
3e6d0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 72 67  .**          arg
3e6e0 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 70  ument from the p
3e6f0 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 77 69 74  revious call wit
3e700 68 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 20 20  h the same.**   
3e710 20 20 20 20 20 20 20 5b 64 61 74 61 62 61 73 65         [database
3e720 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 44 2c 20   connection] D, 
3e730 6f 72 20 4e 55 4c 4c 20 6f 6e 20 74 68 65 20 66  or NULL on the f
3e740 69 72 73 74 20 63 61 6c 6c 0a 2a 2a 20 20 20 20  irst call.**    
3e750 20 20 20 20 20 20 66 6f 72 20 61 20 70 61 72 74        for a part
3e760 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65 20  icular database 
3e770 63 6f 6e 6e 65 63 74 69 6f 6e 20 44 2e 0a 2a 2a  connection D..**
3e780 0a 2a 2a 20 7b 48 31 32 39 36 33 7d 20 45 61 63  .** {H12963} Eac
3e790 68 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  h call to [sqlit
3e7a0 65 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b  e3_rollback_hook
3e7b0 28 29 5d 20 6f 76 65 72 77 72 69 74 65 73 20 74  ()] overwrites t
3e7c0 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20  he callback.**  
3e7d0 20 20 20 20 20 20 20 20 72 65 67 69 73 74 65 72          register
3e7e0 65 64 20 62 79 20 70 72 69 6f 72 20 63 61 6c 6c  ed by prior call
3e7f0 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 36 34  s..**.** {H12964
3e800 7d 20 49 66 20 74 68 65 20 46 20 61 72 67 75 6d  } If the F argum
3e810 65 6e 74 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  ent to [sqlite3_
3e820 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 44 2c  rollback_hook(D,
3e830 46 2c 50 29 5d 20 69 73 20 4e 55 4c 4c 0a 2a 2a  F,P)] is NULL.**
3e840 20 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 74            then t
3e850 68 65 20 72 6f 6c 6c 62 61 63 6b 20 68 6f 6f 6b  he rollback hook
3e860 20 63 61 6c 6c 62 61 63 6b 20 69 73 20 63 61 6e   callback is can
3e870 63 65 6c 65 64 20 61 6e 64 20 6e 6f 20 63 61 6c  celed and no cal
3e880 6c 62 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20  lback.**        
3e890 20 20 69 73 20 69 6e 76 6f 6b 65 64 20 77 68 65    is invoked whe
3e8a0 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  n a transaction 
3e8b0 72 6f 6c 6c 73 20 62 61 63 6b 2e 0a 2a 2f 0a 53  rolls back..*/.S
3e8c0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
3e8d0 73 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68  sqlite3_commit_h
3e8e0 6f 6f 6b 28 73 71 6c 69 74 65 33 2a 2c 20 69 6e  ook(sqlite3*, in
3e8f0 74 28 2a 29 28 76 6f 69 64 2a 29 2c 20 76 6f 69  t(*)(void*), voi
3e900 64 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20  d*);.SQLITE_API 
3e910 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f  void *sqlite3_ro
3e920 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 73 71 6c 69  llback_hook(sqli
3e930 74 65 33 2a 2c 20 76 6f 69 64 28 2a 29 28 76 6f  te3*, void(*)(vo
3e940 69 64 20 2a 29 2c 20 76 6f 69 64 2a 29 3b 0a 0a  id *), void*);..
3e950 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
3e960 44 61 74 61 20 43 68 61 6e 67 65 20 4e 6f 74 69  Data Change Noti
3e970 66 69 63 61 74 69 6f 6e 20 43 61 6c 6c 62 61 63  fication Callbac
3e980 6b 73 20 7b 48 31 32 39 37 30 7d 20 3c 53 36 30  ks {H12970} <S60
3e990 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  400>.**.** The s
3e9a0 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
3e9b0 6f 6b 28 29 20 69 6e 74 65 72 66 61 63 65 20 72  ok() interface r
3e9c0 65 67 69 73 74 65 72 73 20 61 20 63 61 6c 6c 62  egisters a callb
3e9d0 61 63 6b 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20  ack function.** 
3e9e0 77 69 74 68 20 74 68 65 20 5b 64 61 74 61 62 61  with the [databa
3e9f0 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 69  se connection] i
3ea00 64 65 6e 74 69 66 69 65 64 20 62 79 20 74 68 65  dentified by the
3ea10 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 0a   first argument.
3ea20 2a 2a 20 74 6f 20 62 65 20 69 6e 76 6f 6b 65 64  ** to be invoked
3ea30 20 77 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20   whenever a row 
3ea40 69 73 20 75 70 64 61 74 65 64 2c 20 69 6e 73 65  is updated, inse
3ea50 72 74 65 64 20 6f 72 20 64 65 6c 65 74 65 64 2e  rted or deleted.
3ea60 0a 2a 2a 20 41 6e 79 20 63 61 6c 6c 62 61 63 6b  .** Any callback
3ea70 20 73 65 74 20 62 79 20 61 20 70 72 65 76 69 6f   set by a previo
3ea80 75 73 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20  us call to this 
3ea90 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20  function.** for 
3eaa0 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
3eab0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  e connection is 
3eac0 6f 76 65 72 72 69 64 64 65 6e 2e 0a 2a 2a 0a 2a  overridden..**.*
3ead0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67  * The second arg
3eae0 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e 74  ument is a point
3eaf0 65 72 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  er to the functi
3eb00 6f 6e 20 74 6f 20 69 6e 76 6f 6b 65 20 77 68 65  on to invoke whe
3eb10 6e 20 61 0a 2a 2a 20 72 6f 77 20 69 73 20 75 70  n a.** row is up
3eb20 64 61 74 65 64 2c 20 69 6e 73 65 72 74 65 64 20  dated, inserted 
3eb30 6f 72 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 20 54  or deleted..** T
3eb40 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
3eb50 74 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63  t to the callbac
3eb60 6b 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  k is a copy of t
3eb70 68 65 20 74 68 69 72 64 20 61 72 67 75 6d 65 6e  he third argumen
3eb80 74 0a 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f  t.** to sqlite3_
3eb90 75 70 64 61 74 65 5f 68 6f 6f 6b 28 29 2e 0a 2a  update_hook()..*
3eba0 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c  * The second cal
3ebb0 6c 62 61 63 6b 20 61 72 67 75 6d 65 6e 74 20 69  lback argument i
3ebc0 73 20 6f 6e 65 20 6f 66 20 5b 53 51 4c 49 54 45  s one of [SQLITE
3ebd0 5f 49 4e 53 45 52 54 5d 2c 20 5b 53 51 4c 49 54  _INSERT], [SQLIT
3ebe0 45 5f 44 45 4c 45 54 45 5d 2c 0a 2a 2a 20 6f 72  E_DELETE],.** or
3ebf0 20 5b 53 51 4c 49 54 45 5f 55 50 44 41 54 45 5d   [SQLITE_UPDATE]
3ec00 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  , depending on t
3ec10 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  he operation tha
3ec20 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c  t caused the cal
3ec30 6c 62 61 63 6b 0a 2a 2a 20 74 6f 20 62 65 20 69  lback.** to be i
3ec40 6e 76 6f 6b 65 64 2e 0a 2a 2a 20 54 68 65 20 74  nvoked..** The t
3ec50 68 69 72 64 20 61 6e 64 20 66 6f 75 72 74 68 20  hird and fourth 
3ec60 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65  arguments to the
3ec70 20 63 61 6c 6c 62 61 63 6b 20 63 6f 6e 74 61 69   callback contai
3ec80 6e 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  n pointers to th
3ec90 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 61 6e  e.** database an
3eca0 64 20 74 61 62 6c 65 20 6e 61 6d 65 20 63 6f 6e  d table name con
3ecb0 74 61 69 6e 69 6e 67 20 74 68 65 20 61 66 66 65  taining the affe
3ecc0 63 74 65 64 20 72 6f 77 2e 0a 2a 2a 20 54 68 65  cted row..** The
3ecd0 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63 6b 20   final callback 
3ece0 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
3ecf0 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 72 6f   rowid of the ro
3ed00 77 2e 20 49 6e 20 74 68 65 20 63 61 73 65 20 6f  w. In the case o
3ed10 66 0a 2a 2a 20 61 6e 20 75 70 64 61 74 65 2c 20  f.** an update, 
3ed20 74 68 69 73 20 69 73 20 74 68 65 20 72 6f 77 69  this is the rowi
3ed30 64 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61  d after the upda
3ed40 74 65 20 74 61 6b 65 73 20 70 6c 61 63 65 2e 0a  te takes place..
3ed50 2a 2a 0a 2a 2a 20 54 68 65 20 75 70 64 61 74 65  **.** The update
3ed60 20 68 6f 6f 6b 20 69 73 20 6e 6f 74 20 69 6e 76   hook is not inv
3ed70 6f 6b 65 64 20 77 68 65 6e 20 69 6e 74 65 72 6e  oked when intern
3ed80 61 6c 20 73 79 73 74 65 6d 20 74 61 62 6c 65 73  al system tables
3ed90 20 61 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 65 64   are.** modified
3eda0 20 28 69 2e 65 2e 20 73 71 6c 69 74 65 5f 6d 61   (i.e. sqlite_ma
3edb0 73 74 65 72 20 61 6e 64 20 73 71 6c 69 74 65 5f  ster and sqlite_
3edc0 73 65 71 75 65 6e 63 65 29 2e 0a 2a 2a 0a 2a 2a  sequence)..**.**
3edd0 20 54 68 65 20 75 70 64 61 74 65 20 68 6f 6f 6b   The update hook
3ede0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
3edf0 6d 75 73 74 20 6e 6f 74 20 64 6f 20 61 6e 79 74  must not do anyt
3ee00 68 69 6e 67 20 74 68 61 74 20 77 69 6c 6c 20 6d  hing that will m
3ee10 6f 64 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  odify.** the dat
3ee20 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3ee30 20 74 68 61 74 20 69 6e 76 6f 6b 65 64 20 74 68   that invoked th
3ee40 65 20 75 70 64 61 74 65 20 68 6f 6f 6b 2e 20 20  e update hook.  
3ee50 41 6e 79 20 61 63 74 69 6f 6e 73 0a 2a 2a 20 74  Any actions.** t
3ee60 6f 20 6d 6f 64 69 66 79 20 74 68 65 20 64 61 74  o modify the dat
3ee70 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
3ee80 20 6d 75 73 74 20 62 65 20 64 65 66 65 72 72 65   must be deferre
3ee90 64 20 75 6e 74 69 6c 20 61 66 74 65 72 20 74 68  d until after th
3eea0 65 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 20  e.** completion 
3eeb0 6f 66 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f  of the [sqlite3_
3eec0 73 74 65 70 28 29 5d 20 63 61 6c 6c 20 74 68 61  step()] call tha
3eed0 74 20 74 72 69 67 67 65 72 65 64 20 74 68 65 20  t triggered the 
3eee0 75 70 64 61 74 65 20 68 6f 6f 6b 2e 0a 2a 2a 20  update hook..** 
3eef0 4e 6f 74 65 20 74 68 61 74 20 5b 73 71 6c 69 74  Note that [sqlit
3ef00 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 29 5d  e3_prepare_v2()]
3ef10 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73 74   and [sqlite3_st
3ef20 65 70 28 29 5d 20 62 6f 74 68 20 6d 6f 64 69 66  ep()] both modif
3ef30 79 20 74 68 65 69 72 0a 2a 2a 20 64 61 74 61 62  y their.** datab
3ef40 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ase connections 
3ef50 66 6f 72 20 74 68 65 20 6d 65 61 6e 69 6e 67 20  for the meaning 
3ef60 6f 66 20 22 6d 6f 64 69 66 79 22 20 69 6e 20 74  of "modify" in t
3ef70 68 69 73 20 70 61 72 61 67 72 61 70 68 2e 0a 2a  his paragraph..*
3ef80 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68 65 72 20  *.** If another 
3ef90 66 75 6e 63 74 69 6f 6e 20 77 61 73 20 70 72 65  function was pre
3efa0 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65 72  viously register
3efb0 65 64 2c 20 69 74 73 20 70 41 72 67 20 76 61 6c  ed, its pArg val
3efc0 75 65 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  ue.** is returne
3efd0 64 2e 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55  d.  Otherwise NU
3efe0 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  LL is returned..
3eff0 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53  **.** INVARIANTS
3f000 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 31 7d  :.**.** {H12971}
3f010 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 75 70   The [sqlite3_up
3f020 64 61 74 65 5f 68 6f 6f 6b 28 44 2c 46 2c 50 29  date_hook(D,F,P)
3f030 5d 20 69 6e 74 65 72 66 61 63 65 20 63 61 75 73  ] interface caus
3f040 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a  es the callback.
3f050 2a 2a 20 20 20 20 20 20 20 20 20 20 66 75 6e 63  **          func
3f060 74 69 6f 6e 20 46 20 74 6f 20 62 65 20 69 6e 76  tion F to be inv
3f070 6f 6b 65 64 20 77 69 74 68 20 66 69 72 73 74 20  oked with first 
3f080 70 61 72 61 6d 65 74 65 72 20 50 20 77 68 65 6e  parameter P when
3f090 65 76 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  ever.**         
3f0a0 20 61 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20   a table row is 
3f0b0 6d 6f 64 69 66 69 65 64 2c 20 69 6e 73 65 72 74  modified, insert
3f0c0 65 64 2c 20 6f 72 20 64 65 6c 65 74 65 64 20 6f  ed, or deleted o
3f0d0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  n.**          th
3f0e0 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  e [database conn
3f0f0 65 63 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a  ection] D..**.**
3f100 20 7b 48 31 32 39 37 33 7d 20 54 68 65 20 5b 73   {H12973} The [s
3f110 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f  qlite3_update_ho
3f120 6f 6b 28 44 2c 46 2c 50 29 5d 20 69 6e 74 65 72  ok(D,F,P)] inter
3f130 66 61 63 65 20 72 65 74 75 72 6e 73 20 74 68 65  face returns the
3f140 20 76 61 6c 75 65 0a 2a 2a 20 20 20 20 20 20 20   value.**       
3f150 20 20 20 6f 66 20 50 20 66 6f 72 20 74 68 65 20     of P for the 
3f160 70 72 65 76 69 6f 75 73 20 63 61 6c 6c 20 6f 6e  previous call on
3f170 20 74 68 65 20 73 61 6d 65 20 5b 64 61 74 61 62   the same [datab
3f180 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
3f190 44 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 6f  D,.**          o
3f1a0 72 20 4e 55 4c 4c 20 66 6f 72 20 74 68 65 20 66  r NULL for the f
3f1b0 69 72 73 74 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a  irst call..**.**
3f1c0 20 7b 48 31 32 39 37 35 7d 20 49 66 20 74 68 65   {H12975} If the
3f1d0 20 75 70 64 61 74 65 20 68 6f 6f 6b 20 63 61 6c   update hook cal
3f1e0 6c 62 61 63 6b 20 46 20 69 6e 20 5b 73 71 6c 69  lback F in [sqli
3f1f0 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
3f200 44 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20  D,F,P)].**      
3f210 20 20 20 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e      is NULL then
3f220 20 74 68 65 20 6e 6f 20 75 70 64 61 74 65 20 63   the no update c
3f230 61 6c 6c 62 61 63 6b 73 20 61 72 65 20 6d 61 64  allbacks are mad
3f240 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39 37 37  e..**.** {H12977
3f250 7d 20 45 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b  } Each call to [
3f260 73 71 6c 69 74 65 33 5f 75 70 64 61 74 65 5f 68  sqlite3_update_h
3f270 6f 6f 6b 28 44 2c 46 2c 50 29 5d 20 6f 76 65 72  ook(D,F,P)] over
3f280 72 69 64 65 73 20 70 72 69 6f 72 20 63 61 6c 6c  rides prior call
3f290 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 6f  s.**          to
3f2a0 20 74 68 65 20 73 61 6d 65 20 69 6e 74 65 72 66   the same interf
3f2b0 61 63 65 20 6f 6e 20 74 68 65 20 73 61 6d 65 20  ace on the same 
3f2c0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
3f2d0 74 69 6f 6e 5d 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b  tion] D..**.** {
3f2e0 48 31 32 39 37 39 7d 20 54 68 65 20 75 70 64 61  H12979} The upda
3f2f0 74 65 20 68 6f 6f 6b 20 63 61 6c 6c 62 61 63 6b  te hook callback
3f300 20 69 73 20 6e 6f 74 20 69 6e 76 6f 6b 65 64 20   is not invoked 
3f310 77 68 65 6e 20 69 6e 74 65 72 6e 61 6c 20 73 79  when internal sy
3f320 73 74 65 6d 0a 2a 2a 20 20 20 20 20 20 20 20 20  stem.**         
3f330 20 74 61 62 6c 65 73 20 73 75 63 68 20 61 73 20   tables such as 
3f340 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 61 6e  sqlite_master an
3f350 64 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  d sqlite_sequenc
3f360 65 20 61 72 65 20 6d 6f 64 69 66 69 65 64 2e 0a  e are modified..
3f370 2a 2a 0a 2a 2a 20 7b 48 31 32 39 38 31 7d 20 54  **.** {H12981} T
3f380 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61 6d 65  he second parame
3f390 74 65 72 20 74 6f 20 74 68 65 20 75 70 64 61 74  ter to the updat
3f3a0 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 20 20  e callback.**   
3f3b0 20 20 20 20 20 20 20 69 73 20 6f 6e 65 20 6f 66         is one of
3f3c0 20 5b 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 5d   [SQLITE_INSERT]
3f3d0 2c 20 5b 53 51 4c 49 54 45 5f 44 45 4c 45 54 45  , [SQLITE_DELETE
3f3e0 5d 20 6f 72 20 5b 53 51 4c 49 54 45 5f 55 50 44  ] or [SQLITE_UPD
3f3f0 41 54 45 5d 2c 0a 2a 2a 20 20 20 20 20 20 20 20  ATE],.**        
3f400 20 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74    depending on t
3f410 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  he operation tha
3f420 74 20 63 61 75 73 65 64 20 74 68 65 20 63 61 6c  t caused the cal
3f430 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76 6f  lback to be invo
3f440 6b 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 39  ked..**.** {H129
3f450 38 33 7d 20 54 68 65 20 74 68 69 72 64 20 61 6e  83} The third an
3f460 64 20 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e  d fourth argumen
3f470 74 73 20 74 6f 20 74 68 65 20 63 61 6c 6c 62 61  ts to the callba
3f480 63 6b 20 63 6f 6e 74 61 69 6e 20 70 6f 69 6e 74  ck contain point
3f490 65 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ers.**          
3f4a0 74 6f 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74  to zero-terminat
3f4b0 65 64 20 55 54 46 2d 38 20 73 74 72 69 6e 67 73  ed UTF-8 strings
3f4c0 20 77 68 69 63 68 20 61 72 65 20 74 68 65 20 6e   which are the n
3f4d0 61 6d 65 73 20 6f 66 20 74 68 65 0a 2a 2a 20 20  ames of the.**  
3f4e0 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
3f4f0 20 61 6e 64 20 74 61 62 6c 65 20 74 68 61 74 20   and table that 
3f500 69 73 20 62 65 69 6e 67 20 75 70 64 61 74 65 64  is being updated
3f510 2e 0a 0a 2a 2a 20 7b 48 31 32 39 38 35 7d 20 54  ...** {H12985} T
3f520 68 65 20 66 69 6e 61 6c 20 63 61 6c 6c 62 61 63  he final callbac
3f530 6b 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 74  k parameter is t
3f540 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
3f550 72 6f 77 20 61 66 74 65 72 0a 2a 2a 20 20 20 20  row after.**    
3f560 20 20 20 20 20 20 74 68 65 20 63 68 61 6e 67 65        the change
3f570 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49   occurs..*/.SQLI
3f580 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c  TE_API void *sql
3f590 69 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b  ite3_update_hook
3f5a0 28 0a 20 20 73 71 6c 69 74 65 33 2a 2c 20 0a 20  (.  sqlite3*, . 
3f5b0 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 20 2a 2c   void(*)(void *,
3f5c0 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 20  int ,char const 
3f5d0 2a 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73  *,char const *,s
3f5e0 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 2c 0a 20  qlite3_int64),. 
3f5f0 20 76 6f 69 64 2a 0a 29 3b 0a 0a 2f 2a 0a 2a 2a   void*.);../*.**
3f600 20 43 41 50 49 33 52 45 46 3a 20 45 6e 61 62 6c   CAPI3REF: Enabl
3f610 65 20 4f 72 20 44 69 73 61 62 6c 65 20 53 68 61  e Or Disable Sha
3f620 72 65 64 20 50 61 67 65 72 20 43 61 63 68 65 20  red Pager Cache 
3f630 7b 48 31 30 33 33 30 7d 20 3c 53 33 30 39 30 30  {H10330} <S30900
3f640 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b  >.** KEYWORDS: {
3f650 73 68 61 72 65 64 20 63 61 63 68 65 7d 20 7b 73  shared cache} {s
3f660 68 61 72 65 64 20 63 61 63 68 65 20 6d 6f 64 65  hared cache mode
3f670 7d 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  }.**.** This rou
3f680 74 69 6e 65 20 65 6e 61 62 6c 65 73 20 6f 72 20  tine enables or 
3f690 64 69 73 61 62 6c 65 73 20 74 68 65 20 73 68 61  disables the sha
3f6a0 72 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61  ring of the data
3f6b0 62 61 73 65 20 63 61 63 68 65 0a 2a 2a 20 61 6e  base cache.** an
3f6c0 64 20 73 63 68 65 6d 61 20 64 61 74 61 20 73 74  d schema data st
3f6d0 72 75 63 74 75 72 65 73 20 62 65 74 77 65 65 6e  ructures between
3f6e0 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65   [database conne
3f6f0 63 74 69 6f 6e 20 7c 20 63 6f 6e 6e 65 63 74 69  ction | connecti
3f700 6f 6e 73 5d 0a 2a 2a 20 74 6f 20 74 68 65 20 73  ons].** to the s
3f710 61 6d 65 20 64 61 74 61 62 61 73 65 2e 20 53 68  ame database. Sh
3f720 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65 64  aring is enabled
3f730 20 69 66 20 74 68 65 20 61 72 67 75 6d 65 6e 74   if the argument
3f740 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64 20   is true.** and 
3f750 64 69 73 61 62 6c 65 64 20 69 66 20 74 68 65 20  disabled if the 
3f760 61 72 67 75 6d 65 6e 74 20 69 73 20 66 61 6c 73  argument is fals
3f770 65 2e 0a 2a 2a 0a 2a 2a 20 43 61 63 68 65 20 73  e..**.** Cache s
3f780 68 61 72 69 6e 67 20 69 73 20 65 6e 61 62 6c 65  haring is enable
3f790 64 20 61 6e 64 20 64 69 73 61 62 6c 65 64 20 66  d and disabled f
3f7a0 6f 72 20 61 6e 20 65 6e 74 69 72 65 20 70 72 6f  or an entire pro
3f7b0 63 65 73 73 2e 20 7b 45 4e 44 7d 0a 2a 2a 20 54  cess. {END}.** T
3f7c0 68 69 73 20 69 73 20 61 20 63 68 61 6e 67 65 20  his is a change 
3f7d0 61 73 20 6f 66 20 53 51 4c 69 74 65 20 76 65 72  as of SQLite ver
3f7e0 73 69 6f 6e 20 33 2e 35 2e 30 2e 20 49 6e 20 70  sion 3.5.0. In p
3f7f0 72 69 6f 72 20 76 65 72 73 69 6f 6e 73 20 6f 66  rior versions of
3f800 20 53 51 4c 69 74 65 2c 0a 2a 2a 20 73 68 61 72   SQLite,.** shar
3f810 69 6e 67 20 77 61 73 20 65 6e 61 62 6c 65 64 20  ing was enabled 
3f820 6f 72 20 64 69 73 61 62 6c 65 64 20 66 6f 72 20  or disabled for 
3f830 65 61 63 68 20 74 68 72 65 61 64 20 73 65 70 61  each thread sepa
3f840 72 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  rately..**.** Th
3f850 65 20 63 61 63 68 65 20 73 68 61 72 69 6e 67 20  e cache sharing 
3f860 6d 6f 64 65 20 73 65 74 20 62 79 20 74 68 69 73  mode set by this
3f870 20 69 6e 74 65 72 66 61 63 65 20 65 66 66 65 63   interface effec
3f880 74 73 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  ts all subsequen
3f890 74 0a 2a 2a 20 63 61 6c 6c 73 20 74 6f 20 5b 73  t.** calls to [s
3f8a0 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 5d 2c 20  qlite3_open()], 
3f8b0 5b 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32  [sqlite3_open_v2
3f8c0 28 29 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65  ()], and [sqlite
3f8d0 33 5f 6f 70 65 6e 31 36 28 29 5d 2e 0a 2a 2a 20  3_open16()]..** 
3f8e0 45 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  Existing databas
3f8f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 63 6f  e connections co
3f900 6e 74 69 6e 75 65 20 75 73 65 20 74 68 65 20 73  ntinue use the s
3f910 68 61 72 69 6e 67 20 6d 6f 64 65 0a 2a 2a 20 74  haring mode.** t
3f920 68 61 74 20 77 61 73 20 69 6e 20 65 66 66 65 63  hat was in effec
3f930 74 20 61 74 20 74 68 65 20 74 69 6d 65 20 74 68  t at the time th
3f940 65 79 20 77 65 72 65 20 6f 70 65 6e 65 64 2e 0a  ey were opened..
3f950 2a 2a 0a 2a 2a 20 56 69 72 74 75 61 6c 20 74 61  **.** Virtual ta
3f960 62 6c 65 73 20 63 61 6e 6e 6f 74 20 62 65 20 75  bles cannot be u
3f970 73 65 64 20 77 69 74 68 20 61 20 73 68 61 72 65  sed with a share
3f980 64 20 63 61 63 68 65 2e 20 20 57 68 65 6e 20 73  d cache.  When s
3f990 68 61 72 65 64 0a 2a 2a 20 63 61 63 68 65 20 69  hared.** cache i
3f9a0 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b  s enabled, the [
3f9b0 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
3f9c0 6f 64 75 6c 65 28 29 5d 20 41 50 49 20 75 73 65  odule()] API use
3f9d0 64 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a 2a  d to register.**
3f9e0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20   virtual tables 
3f9f0 77 69 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75  will always retu
3fa00 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a  rn an error..**.
3fa10 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
3fa20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45 5f  returns [SQLITE_
3fa30 4f 4b 5d 20 69 66 20 73 68 61 72 65 64 20 63 61  OK] if shared ca
3fa40 63 68 65 20 77 61 73 20 65 6e 61 62 6c 65 64 20  che was enabled 
3fa50 6f 72 20 64 69 73 61 62 6c 65 64 0a 2a 2a 20 73  or disabled.** s
3fa60 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 20 41 6e  uccessfully.  An
3fa70 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73   [error code] is
3fa80 20 72 65 74 75 72 6e 65 64 20 6f 74 68 65 72 77   returned otherw
3fa90 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 68 61 72 65  ise..**.** Share
3faa0 64 20 63 61 63 68 65 20 69 73 20 64 69 73 61 62  d cache is disab
3fab0 6c 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20  led by default. 
3fac0 42 75 74 20 74 68 69 73 20 6d 69 67 68 74 20 63  But this might c
3fad0 68 61 6e 67 65 20 69 6e 0a 2a 2a 20 66 75 74 75  hange in.** futu
3fae0 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20 53  re releases of S
3faf0 51 4c 69 74 65 2e 20 20 41 70 70 6c 69 63 61 74  QLite.  Applicat
3fb00 69 6f 6e 73 20 74 68 61 74 20 63 61 72 65 20 61  ions that care a
3fb10 62 6f 75 74 20 73 68 61 72 65 64 0a 2a 2a 20 63  bout shared.** c
3fb20 61 63 68 65 20 73 65 74 74 69 6e 67 20 73 68 6f  ache setting sho
3fb30 75 6c 64 20 73 65 74 20 69 74 20 65 78 70 6c 69  uld set it expli
3fb40 63 69 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56  citly..**.** INV
3fb50 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b  ARIANTS:.**.** {
3fb60 48 31 30 33 33 31 7d 20 41 20 73 75 63 63 65 73  H10331} A succes
3fb70 73 66 75 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20  sful invocation 
3fb80 6f 66 20 5b 73 71 6c 69 74 65 33 5f 65 6e 61 62  of [sqlite3_enab
3fb90 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65 28  le_shared_cache(
3fba0 42 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  B)].**          
3fbb0 77 69 6c 6c 20 65 6e 61 62 6c 65 20 6f 72 20 64  will enable or d
3fbc0 69 73 61 62 6c 65 20 73 68 61 72 65 64 20 63 61  isable shared ca
3fbd0 63 68 65 20 6d 6f 64 65 20 66 6f 72 20 61 6e 79  che mode for any
3fbe0 20 73 75 62 73 65 71 75 65 6e 74 6c 79 0a 2a 2a   subsequently.**
3fbf0 20 20 20 20 20 20 20 20 20 20 63 72 65 61 74 65            create
3fc00 64 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  d [database conn
3fc10 65 63 74 69 6f 6e 5d 20 69 6e 20 74 68 65 20 73  ection] in the s
3fc20 61 6d 65 20 70 72 6f 63 65 73 73 2e 0a 2a 2a 0a  ame process..**.
3fc30 2a 2a 20 7b 48 31 30 33 33 36 7d 20 57 68 65 6e  ** {H10336} When
3fc40 20 73 68 61 72 65 64 20 63 61 63 68 65 20 69 73   shared cache is
3fc50 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 20 5b 73   enabled, the [s
3fc60 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
3fc70 64 75 6c 65 28 29 5d 0a 2a 2a 20 20 20 20 20 20  dule()].**      
3fc80 20 20 20 20 69 6e 74 65 72 66 61 63 65 20 77 69      interface wi
3fc90 6c 6c 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e  ll always return
3fca0 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a   an error..**.**
3fcb0 20 7b 48 31 30 33 33 37 7d 20 54 68 65 20 5b 73   {H10337} The [s
3fcc0 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 73 68  qlite3_enable_sh
3fcd0 61 72 65 64 5f 63 61 63 68 65 28 42 29 5d 20 69  ared_cache(B)] i
3fce0 6e 74 65 72 66 61 63 65 20 72 65 74 75 72 6e 73  nterface returns
3fcf0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 53 51  .**          [SQ
3fd00 4c 49 54 45 5f 4f 4b 5d 20 69 66 20 73 68 61 72  LITE_OK] if shar
3fd10 65 64 20 63 61 63 68 65 20 77 61 73 20 65 6e 61  ed cache was ena
3fd20 62 6c 65 64 20 6f 72 20 64 69 73 61 62 6c 65 64  bled or disabled
3fd30 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a   successfully..*
3fd40 2a 0a 2a 2a 20 7b 48 31 30 33 33 39 7d 20 53 68  *.** {H10339} Sh
3fd50 61 72 65 64 20 63 61 63 68 65 20 69 73 20 64 69  ared cache is di
3fd60 73 61 62 6c 65 64 20 62 79 20 64 65 66 61 75 6c  sabled by defaul
3fd70 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
3fd80 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61   int sqlite3_ena
3fd90 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
3fda0 28 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  (int);../*.** CA
3fdb0 50 49 33 52 45 46 3a 20 41 74 74 65 6d 70 74 20  PI3REF: Attempt 
3fdc0 54 6f 20 46 72 65 65 20 48 65 61 70 20 4d 65 6d  To Free Heap Mem
3fdd0 6f 72 79 20 7b 48 31 37 33 34 30 7d 20 3c 53 33  ory {H17340} <S3
3fde0 30 32 32 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  0220>.**.** The 
3fdf0 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
3fe00 6d 65 6d 6f 72 79 28 29 20 69 6e 74 65 72 66 61  memory() interfa
3fe10 63 65 20 61 74 74 65 6d 70 74 73 20 74 6f 20 66  ce attempts to f
3fe20 72 65 65 20 4e 20 62 79 74 65 73 0a 2a 2a 20 6f  ree N bytes.** o
3fe30 66 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79  f heap memory by
3fe40 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f   deallocating no
3fe50 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65 6d 6f  n-essential memo
3fe60 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 2a  ry allocations.*
3fe70 2a 20 68 65 6c 64 20 62 79 20 74 68 65 20 64 61  * held by the da
3fe80 74 61 62 61 73 65 20 6c 69 62 72 61 72 79 2e 20  tabase library. 
3fe90 7b 45 4e 44 7d 20 20 4d 65 6d 6f 72 79 20 75 73  {END}  Memory us
3fea0 65 64 20 74 6f 20 63 61 63 68 65 20 64 61 74 61  ed to cache data
3feb0 62 61 73 65 0a 2a 2a 20 70 61 67 65 73 20 74 6f  base.** pages to
3fec0 20 69 6d 70 72 6f 76 65 20 70 65 72 66 6f 72 6d   improve perform
3fed0 61 6e 63 65 20 69 73 20 61 6e 20 65 78 61 6d 70  ance is an examp
3fee0 6c 65 20 6f 66 20 6e 6f 6e 2d 65 73 73 65 6e 74  le of non-essent
3fef0 69 61 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 73  ial memory..** s
3ff00 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d  qlite3_release_m
3ff10 65 6d 6f 72 79 28 29 20 72 65 74 75 72 6e 73 20  emory() returns 
3ff20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
3ff30 74 65 73 20 61 63 74 75 61 6c 6c 79 20 66 72 65  tes actually fre
3ff40 65 64 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 69 67  ed,.** which mig
3ff50 68 74 20 62 65 20 6d 6f 72 65 20 6f 72 20 6c 65  ht be more or le
3ff60 73 73 20 74 68 61 6e 20 74 68 65 20 61 6d 6f 75  ss than the amou
3ff70 6e 74 20 72 65 71 75 65 73 74 65 64 2e 0a 2a 2a  nt requested..**
3ff80 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
3ff90 2a 2a 0a 2a 2a 20 7b 48 31 37 33 34 31 7d 20 54  **.** {H17341} T
3ffa0 68 65 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65  he [sqlite3_rele
3ffb0 61 73 65 5f 6d 65 6d 6f 72 79 28 4e 29 5d 20 69  ase_memory(N)] i
3ffc0 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70 74  nterface attempt
3ffd0 73 20 74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20  s to.**         
3ffe0 20 66 72 65 65 20 4e 20 62 79 74 65 73 20 6f 66   free N bytes of
3fff0 20 68 65 61 70 20 6d 65 6d 6f 72 79 20 62 79 20   heap memory by 
40000 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20 6e 6f 6e  deallocating non
40010 2d 65 73 73 65 6e 74 69 61 6c 0a 2a 2a 20 20 20  -essential.**   
40020 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 20 61 6c         memory al
40030 6c 6f 63 61 74 69 6f 6e 73 20 68 65 6c 64 20 62  locations held b
40040 79 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6c  y the database l
40050 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ibrary..**.** {H
40060 31 36 33 34 32 7d 20 54 68 65 20 5b 73 71 6c 69  16342} The [sqli
40070 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
40080 72 79 28 4e 29 5d 20 72 65 74 75 72 6e 73 20 74  ry(N)] returns t
40090 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20  he number.**    
400a0 20 20 20 20 20 20 6f 66 20 62 79 74 65 73 20 61        of bytes a
400b0 63 74 75 61 6c 6c 79 20 66 72 65 65 64 2c 20 77  ctually freed, w
400c0 68 69 63 68 20 6d 69 67 68 74 20 62 65 20 6d 6f  hich might be mo
400d0 72 65 20 6f 72 20 6c 65 73 73 0a 2a 2a 20 20 20  re or less.**   
400e0 20 20 20 20 20 20 20 74 68 61 6e 20 74 68 65 20         than the 
400f0 61 6d 6f 75 6e 74 20 72 65 71 75 65 73 74 65 64  amount requested
40100 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
40110 69 6e 74 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  int sqlite3_rele
40120 61 73 65 5f 6d 65 6d 6f 72 79 28 69 6e 74 29 3b  ase_memory(int);
40130 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
40140 3a 20 49 6d 70 6f 73 65 20 41 20 4c 69 6d 69 74  : Impose A Limit
40150 20 4f 6e 20 48 65 61 70 20 53 69 7a 65 20 7b 48   On Heap Size {H
40160 31 37 33 35 30 7d 20 3c 53 33 30 32 32 30 3e 0a  17350} <S30220>.
40170 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  **.** The sqlite
40180 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69  3_soft_heap_limi
40190 74 28 29 20 69 6e 74 65 72 66 61 63 65 20 70 6c  t() interface pl
401a0 61 63 65 73 20 61 20 22 73 6f 66 74 22 20 6c 69  aces a "soft" li
401b0 6d 69 74 0a 2a 2a 20 6f 6e 20 74 68 65 20 61 6d  mit.** on the am
401c0 6f 75 6e 74 20 6f 66 20 68 65 61 70 20 6d 65 6d  ount of heap mem
401d0 6f 72 79 20 74 68 61 74 20 6d 61 79 20 62 65 20  ory that may be 
401e0 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 53 51 4c  allocated by SQL
401f0 69 74 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  ite..** If an in
40200 74 65 72 6e 61 6c 20 61 6c 6c 6f 63 61 74 69 6f  ternal allocatio
40210 6e 20 69 73 20 72 65 71 75 65 73 74 65 64 20 74  n is requested t
40220 68 61 74 20 77 6f 75 6c 64 20 65 78 63 65 65 64  hat would exceed
40230 20 74 68 65 0a 2a 2a 20 73 6f 66 74 20 68 65 61   the.** soft hea
40240 70 20 6c 69 6d 69 74 2c 20 5b 73 71 6c 69 74 65  p limit, [sqlite
40250 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
40260 28 29 5d 20 69 73 20 69 6e 76 6f 6b 65 64 20 6f  ()] is invoked o
40270 6e 65 20 6f 72 0a 2a 2a 20 6d 6f 72 65 20 74 69  ne or.** more ti
40280 6d 65 73 20 74 6f 20 66 72 65 65 20 75 70 20 73  mes to free up s
40290 6f 6d 65 20 73 70 61 63 65 20 62 65 66 6f 72 65  ome space before
402a0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
402b0 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a  is performed..**
402c0 0a 2a 2a 20 54 68 65 20 6c 69 6d 69 74 20 69 73  .** The limit is
402d0 20 63 61 6c 6c 65 64 20 22 73 6f 66 74 22 2c 20   called "soft", 
402e0 62 65 63 61 75 73 65 20 69 66 20 5b 73 71 6c 69  because if [sqli
402f0 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f  te3_release_memo
40300 72 79 28 29 5d 0a 2a 2a 20 63 61 6e 6e 6f 74 20  ry()].** cannot 
40310 66 72 65 65 20 73 75 66 66 69 63 69 65 6e 74 20  free sufficient 
40320 6d 65 6d 6f 72 79 20 74 6f 20 70 72 65 76 65 6e  memory to preven
40330 74 20 74 68 65 20 6c 69 6d 69 74 20 66 72 6f 6d  t the limit from
40340 20 62 65 69 6e 67 20 65 78 63 65 65 64 65 64 2c   being exceeded,
40350 0a 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  .** the memory i
40360 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 79 77  s allocated anyw
40370 61 79 20 61 6e 64 20 74 68 65 20 63 75 72 72 65  ay and the curre
40380 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 70 72 6f  nt operation pro
40390 63 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e  ceeds..**.** A n
403a0 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20  egative or zero 
403b0 76 61 6c 75 65 20 66 6f 72 20 4e 20 6d 65 61 6e  value for N mean
403c0 73 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  s that there is 
403d0 6e 6f 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d  no soft heap lim
403e0 69 74 20 61 6e 64 0a 2a 2a 20 5b 73 71 6c 69 74  it and.** [sqlit
403f0 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
40400 79 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62  y()] will only b
40410 65 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 6d 65  e called when me
40420 6d 6f 72 79 20 69 73 20 65 78 68 61 75 73 74 65  mory is exhauste
40430 64 2e 0a 2a 2a 20 54 68 65 20 64 65 66 61 75 6c  d..** The defaul
40440 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20  t value for the 
40450 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 20  soft heap limit 
40460 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 53  is zero..**.** S
40470 51 4c 69 74 65 20 6d 61 6b 65 73 20 61 20 62 65  QLite makes a be
40480 73 74 20 65 66 66 6f 72 74 20 74 6f 20 68 6f 6e  st effort to hon
40490 6f 72 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  or the soft heap
404a0 20 6c 69 6d 69 74 2e 0a 2a 2a 20 42 75 74 20 69   limit..** But i
404b0 66 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  f the soft heap 
404c0 6c 69 6d 69 74 20 63 61 6e 6e 6f 74 20 62 65 20  limit cannot be 
404d0 68 6f 6e 6f 72 65 64 2c 20 65 78 65 63 75 74 69  honored, executi
404e0 6f 6e 20 77 69 6c 6c 0a 2a 2a 20 63 6f 6e 74 69  on will.** conti
404f0 6e 75 65 20 77 69 74 68 6f 75 74 20 65 72 72 6f  nue without erro
40500 72 20 6f 72 20 6e 6f 74 69 66 69 63 61 74 69 6f  r or notificatio
40510 6e 2e 20 20 54 68 69 73 20 69 73 20 77 68 79 20  n.  This is why 
40520 74 68 65 20 6c 69 6d 69 74 20 69 73 0a 2a 2a 20  the limit is.** 
40530 63 61 6c 6c 65 64 20 61 20 22 73 6f 66 74 22 20  called a "soft" 
40540 6c 69 6d 69 74 2e 20 20 49 74 20 69 73 20 61 64  limit.  It is ad
40550 76 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a  visory only..**.
40560 2a 2a 20 50 72 69 6f 72 20 74 6f 20 53 51 4c 69  ** Prior to SQLi
40570 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30  te version 3.5.0
40580 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6f  , this routine o
40590 6e 6c 79 20 63 6f 6e 73 74 72 61 69 6e 65 64 20  nly constrained 
405a0 74 68 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  the memory.** al
405b0 6c 6f 63 61 74 65 64 20 62 79 20 61 20 73 69 6e  located by a sin
405c0 67 6c 65 20 74 68 72 65 61 64 20 2d 20 74 68 65  gle thread - the
405d0 20 73 61 6d 65 20 74 68 72 65 61 64 20 69 6e 20   same thread in 
405e0 77 68 69 63 68 20 74 68 69 73 20 72 6f 75 74 69  which this routi
405f0 6e 65 0a 2a 2a 20 72 75 6e 73 2e 20 20 42 65 67  ne.** runs.  Beg
40600 69 6e 6e 69 6e 67 20 77 69 74 68 20 53 51 4c 69  inning with SQLi
40610 74 65 20 76 65 72 73 69 6f 6e 20 33 2e 35 2e 30  te version 3.5.0
40620 2c 20 74 68 65 20 73 6f 66 74 20 68 65 61 70 20  , the soft heap 
40630 6c 69 6d 69 74 20 69 73 0a 2a 2a 20 61 70 70 6c  limit is.** appl
40640 69 65 64 20 74 6f 20 61 6c 6c 20 74 68 72 65 61  ied to all threa
40650 64 73 2e 20 54 68 65 20 76 61 6c 75 65 20 73 70  ds. The value sp
40660 65 63 69 66 69 65 64 20 66 6f 72 20 74 68 65 20  ecified for the 
40670 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69 74 0a  soft heap limit.
40680 2a 2a 20 69 73 20 61 6e 20 75 70 70 65 72 20 62  ** is an upper b
40690 6f 75 6e 64 20 6f 6e 20 74 68 65 20 74 6f 74 61  ound on the tota
406a0 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  l memory allocat
406b0 69 6f 6e 20 66 6f 72 20 61 6c 6c 20 74 68 72 65  ion for all thre
406c0 61 64 73 2e 20 49 6e 0a 2a 2a 20 76 65 72 73 69  ads. In.** versi
406d0 6f 6e 20 33 2e 35 2e 30 20 74 68 65 72 65 20 69  on 3.5.0 there i
406e0 73 20 6e 6f 20 6d 65 63 68 61 6e 69 73 6d 20 66  s no mechanism f
406f0 6f 72 20 6c 69 6d 69 74 69 6e 67 20 74 68 65 20  or limiting the 
40700 68 65 61 70 20 75 73 61 67 65 20 66 6f 72 0a 2a  heap usage for.*
40710 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 68 72  * individual thr
40720 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41  eads..**.** INVA
40730 52 49 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48  RIANTS:.**.** {H
40740 31 36 33 35 31 7d 20 54 68 65 20 5b 73 71 6c 69  16351} The [sqli
40750 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69  te3_soft_heap_li
40760 6d 69 74 28 4e 29 5d 20 69 6e 74 65 72 66 61 63  mit(N)] interfac
40770 65 20 70 6c 61 63 65 73 20 61 20 73 6f 66 74 20  e places a soft 
40780 6c 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20 20  limit.**        
40790 20 20 6f 66 20 4e 20 62 79 74 65 73 20 6f 6e 20    of N bytes on 
407a0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 68 65  the amount of he
407b0 61 70 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d  ap memory that m
407c0 61 79 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a  ay be allocated.
407d0 2a 2a 20 20 20 20 20 20 20 20 20 20 75 73 69 6e  **          usin
407e0 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  g [sqlite3_mallo
407f0 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74 65 33  c()] or [sqlite3
40800 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 61 74 20 61  _realloc()] at a
40810 6e 79 20 70 6f 69 6e 74 0a 2a 2a 20 20 20 20 20  ny point.**     
40820 20 20 20 20 20 69 6e 20 74 69 6d 65 2e 0a 2a 2a       in time..**
40830 0a 2a 2a 20 7b 48 31 36 33 35 32 7d 20 49 66 20  .** {H16352} If 
40840 61 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  a call to [sqlit
40850 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20  e3_malloc()] or 
40860 5b 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63  [sqlite3_realloc
40870 28 29 5d 20 77 6f 75 6c 64 0a 2a 2a 20 20 20 20  ()] would.**    
40880 20 20 20 20 20 20 63 61 75 73 65 20 74 68 65 20        cause the 
40890 74 6f 74 61 6c 20 61 6d 6f 75 6e 74 20 6f 66 20  total amount of 
408a0 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
408b0 20 74 6f 20 65 78 63 65 65 64 20 74 68 65 0a 2a   to exceed the.*
408c0 2a 20 20 20 20 20 20 20 20 20 20 73 6f 66 74 20  *          soft 
408d0 68 65 61 70 20 6c 69 6d 69 74 2c 20 74 68 65 6e  heap limit, then
408e0 20 5b 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73   [sqlite3_releas
408f0 65 5f 6d 65 6d 6f 72 79 28 29 5d 20 69 73 20 69  e_memory()] is i
40900 6e 76 6f 6b 65 64 0a 2a 2a 20 20 20 20 20 20 20  nvoked.**       
40910 20 20 20 69 6e 20 61 6e 20 61 74 74 65 6d 70 74     in an attempt
40920 20 74 6f 20 72 65 64 75 63 65 20 74 68 65 20 6d   to reduce the m
40930 65 6d 6f 72 79 20 75 73 61 67 65 20 70 72 69 6f  emory usage prio
40940 72 20 74 6f 20 70 72 6f 63 65 65 64 69 6e 67 0a  r to proceeding.
40950 2a 2a 20 20 20 20 20 20 20 20 20 20 77 69 74 68  **          with
40960 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
40970 63 61 74 69 6f 6e 20 61 74 74 65 6d 70 74 2e 0a  cation attempt..
40980 2a 2a 0a 2a 2a 20 7b 48 31 36 33 35 33 7d 20 43  **.** {H16353} C
40990 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
409a0 5f 6d 61 6c 6c 6f 63 28 29 5d 20 6f 72 20 5b 73  _malloc()] or [s
409b0 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 29  qlite3_realloc()
409c0 5d 20 74 68 61 74 20 74 72 69 67 67 65 72 0a 2a  ] that trigger.*
409d0 2a 20 20 20 20 20 20 20 20 20 20 61 74 74 65 6d  *          attem
409e0 70 74 73 20 74 6f 20 72 65 64 75 63 65 20 6d 65  pts to reduce me
409f0 6d 6f 72 79 20 75 73 61 67 65 20 74 68 72 6f 75  mory usage throu
40a00 67 68 20 74 68 65 20 73 6f 66 74 20 68 65 61 70  gh the soft heap
40a10 20 6c 69 6d 69 74 0a 2a 2a 20 20 20 20 20 20 20   limit.**       
40a20 20 20 20 6d 65 63 68 61 6e 69 73 6d 20 63 6f 6e     mechanism con
40a30 74 69 6e 75 65 20 65 76 65 6e 20 69 66 20 74 68  tinue even if th
40a40 65 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 64  e attempt to red
40a50 75 63 65 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20  uce memory.**   
40a60 20 20 20 20 20 20 20 75 73 61 67 65 20 69 73 20         usage is 
40a70 75 6e 73 75 63 63 65 73 73 66 75 6c 2e 0a 2a 2a  unsuccessful..**
40a80 0a 2a 2a 20 7b 48 31 36 33 35 34 7d 20 41 20 6e  .** {H16354} A n
40a90 65 67 61 74 69 76 65 20 6f 72 20 7a 65 72 6f 20  egative or zero 
40aa0 76 61 6c 75 65 20 66 6f 72 20 4e 20 69 6e 20 61  value for N in a
40ab0 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 20 20 20 20   call to.**     
40ac0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 73 6f       [sqlite3_so
40ad0 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28 4e 29  ft_heap_limit(N)
40ae0 5d 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  ] means that the
40af0 72 65 20 69 73 20 6e 6f 20 73 6f 66 74 0a 2a 2a  re is no soft.**
40b00 20 20 20 20 20 20 20 20 20 20 68 65 61 70 20 6c            heap l
40b10 69 6d 69 74 20 61 6e 64 20 5b 73 71 6c 69 74 65  imit and [sqlite
40b20 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
40b30 28 29 5d 20 77 69 6c 6c 20 6f 6e 6c 79 20 62 65  ()] will only be
40b40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 61 6c  .**          cal
40b50 6c 65 64 20 77 68 65 6e 20 6d 65 6d 6f 72 79 20  led when memory 
40b60 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79 20 65 78  is completely ex
40b70 68 61 75 73 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b  hausted..**.** {
40b80 48 31 36 33 35 35 7d 20 54 68 65 20 64 65 66 61  H16355} The defa
40b90 75 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68  ult value for th
40ba0 65 20 73 6f 66 74 20 68 65 61 70 20 6c 69 6d 69  e soft heap limi
40bb0 74 20 69 73 20 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a  t is zero..**.**
40bc0 20 7b 48 31 36 33 35 38 7d 20 45 61 63 68 20 63   {H16358} Each c
40bd0 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f  all to [sqlite3_
40be0 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74 28  soft_heap_limit(
40bf0 4e 29 5d 20 6f 76 65 72 72 69 64 65 73 20 74 68  N)] overrides th
40c00 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 76 61  e.**          va
40c10 6c 75 65 73 20 73 65 74 20 62 79 20 61 6c 6c 20  lues set by all 
40c20 70 72 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a  prior calls..*/.
40c30 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
40c40 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65 61  sqlite3_soft_hea
40c50 70 5f 6c 69 6d 69 74 28 69 6e 74 29 3b 0a 0a 2f  p_limit(int);../
40c60 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 45  *.** CAPI3REF: E
40c70 78 74 72 61 63 74 20 4d 65 74 61 64 61 74 61 20  xtract Metadata 
40c80 41 62 6f 75 74 20 41 20 43 6f 6c 75 6d 6e 20 4f  About A Column O
40c90 66 20 41 20 54 61 62 6c 65 20 7b 48 31 32 38 35  f A Table {H1285
40ca0 30 7d 20 3c 53 36 30 33 30 30 3e 0a 2a 2a 0a 2a  0} <S60300>.**.*
40cb0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72  * This routine r
40cc0 65 74 75 72 6e 73 20 6d 65 74 61 64 61 74 61 20  eturns metadata 
40cd0 61 62 6f 75 74 20 61 20 73 70 65 63 69 66 69 63  about a specific
40ce0 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 73 70 65   column of a spe
40cf0 63 69 66 69 63 0a 2a 2a 20 64 61 74 61 62 61 73  cific.** databas
40d00 65 20 74 61 62 6c 65 20 61 63 63 65 73 73 69 62  e table accessib
40d10 6c 65 20 75 73 69 6e 67 20 74 68 65 20 5b 64 61  le using the [da
40d20 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
40d30 6e 5d 20 68 61 6e 64 6c 65 0a 2a 2a 20 70 61 73  n] handle.** pas
40d40 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
40d50 20 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65   function argume
40d60 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  nt..**.** The co
40d70 6c 75 6d 6e 20 69 73 20 69 64 65 6e 74 69 66 69  lumn is identifi
40d80 65 64 20 62 79 20 74 68 65 20 73 65 63 6f 6e 64  ed by the second
40d90 2c 20 74 68 69 72 64 20 61 6e 64 20 66 6f 75 72  , third and four
40da0 74 68 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f  th parameters to
40db0 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
40dc0 6e 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61  n. The second pa
40dd0 72 61 6d 65 74 65 72 20 69 73 20 65 69 74 68 65  rameter is eithe
40de0 72 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68  r the name of th
40df0 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 28 69  e database.** (i
40e00 2e 65 2e 20 22 6d 61 69 6e 22 2c 20 22 74 65 6d  .e. "main", "tem
40e10 70 22 20 6f 72 20 61 6e 20 61 74 74 61 63 68 65  p" or an attache
40e20 64 20 64 61 74 61 62 61 73 65 29 20 63 6f 6e 74  d database) cont
40e30 61 69 6e 69 6e 67 20 74 68 65 20 73 70 65 63 69  aining the speci
40e40 66 69 65 64 0a 2a 2a 20 74 61 62 6c 65 20 6f 72  fied.** table or
40e50 20 4e 55 4c 4c 2e 20 49 66 20 69 74 20 69 73 20   NULL. If it is 
40e60 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 6c 6c 20 61  NULL, then all a
40e70 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
40e80 73 20 61 72 65 20 73 65 61 72 63 68 65 64 0a 2a  s are searched.*
40e90 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  * for the table 
40ea0 75 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 61  using the same a
40eb0 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 20 62 79  lgorithm used by
40ec0 20 74 68 65 20 64 61 74 61 62 61 73 65 20 65 6e   the database en
40ed0 67 69 6e 65 20 74 6f 0a 2a 2a 20 72 65 73 6f 6c  gine to.** resol
40ee0 76 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 74  ve unqualified t
40ef0 61 62 6c 65 20 72 65 66 65 72 65 6e 63 65 73 2e  able references.
40f00 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  .**.** The third
40f10 20 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61   and fourth para
40f20 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
40f30 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20  unction are the 
40f40 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75 6d 6e  table and column
40f50 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  .** name of the 
40f60 64 65 73 69 72 65 64 20 63 6f 6c 75 6d 6e 2c 20  desired column, 
40f70 72 65 73 70 65 63 74 69 76 65 6c 79 2e 20 4e 65  respectively. Ne
40f80 69 74 68 65 72 20 6f 66 20 74 68 65 73 65 20 70  ither of these p
40f90 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 6d 61 79  arameters.** may
40fa0 20 62 65 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20   be NULL..**.** 
40fb0 4d 65 74 61 64 61 74 61 20 69 73 20 72 65 74 75  Metadata is retu
40fc0 72 6e 65 64 20 62 79 20 77 72 69 74 69 6e 67 20  rned by writing 
40fd0 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 6c 6f  to the memory lo
40fe0 63 61 74 69 6f 6e 73 20 70 61 73 73 65 64 20 61  cations passed a
40ff0 73 20 74 68 65 20 35 74 68 0a 2a 2a 20 61 6e 64  s the 5th.** and
41000 20 73 75 62 73 65 71 75 65 6e 74 20 70 61 72 61   subsequent para
41010 6d 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 66  meters to this f
41020 75 6e 63 74 69 6f 6e 2e 20 41 6e 79 20 6f 66 20  unction. Any of 
41030 74 68 65 73 65 20 61 72 67 75 6d 65 6e 74 73 20  these arguments 
41040 6d 61 79 20 62 65 0a 2a 2a 20 4e 55 4c 4c 2c 20  may be.** NULL, 
41050 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 74 68  in which case th
41060 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
41070 65 6c 65 6d 65 6e 74 20 6f 66 20 6d 65 74 61 64  element of metad
41080 61 74 61 20 69 73 20 6f 6d 69 74 74 65 64 2e 0a  ata is omitted..
41090 2a 2a 0a 2a 2a 20 3c 62 6c 6f 63 6b 71 75 6f 74  **.** <blockquot
410a0 65 3e 0a 2a 2a 20 3c 74 61 62 6c 65 20 62 6f 72  e>.** <table bor
410b0 64 65 72 3d 22 31 22 3e 0a 2a 2a 20 3c 74 72 3e  der="1">.** <tr>
410c0 3c 74 68 3e 20 50 61 72 61 6d 65 74 65 72 20 3c  <th> Parameter <
410d0 74 68 3e 20 4f 75 74 70 75 74 3c 62 72 3e 54 79  th> Output<br>Ty
410e0 70 65 20 3c 74 68 3e 20 20 44 65 73 63 72 69 70  pe <th>  Descrip
410f0 74 69 6f 6e 0a 2a 2a 0a 2a 2a 20 3c 74 72 3e 3c  tion.**.** <tr><
41100 74 64 3e 20 35 74 68 20 3c 74 64 3e 20 63 6f 6e  td> 5th <td> con
41110 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 44 61  st char* <td> Da
41120 74 61 20 74 79 70 65 0a 2a 2a 20 3c 74 72 3e 3c  ta type.** <tr><
41130 74 64 3e 20 36 74 68 20 3c 74 64 3e 20 63 6f 6e  td> 6th <td> con
41140 73 74 20 63 68 61 72 2a 20 3c 74 64 3e 20 4e 61  st char* <td> Na
41150 6d 65 20 6f 66 20 64 65 66 61 75 6c 74 20 63 6f  me of default co
41160 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
41170 0a 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 37 74 68  .** <tr><td> 7th
41180 20 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20   <td> int       
41190 20 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63    <td> True if c
411a0 6f 6c 75 6d 6e 20 68 61 73 20 61 20 4e 4f 54 20  olumn has a NOT 
411b0 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74 0a  NULL constraint.
411c0 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 38 74 68 20  ** <tr><td> 8th 
411d0 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
411e0 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
411f0 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66 20  lumn is part of 
41200 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59 0a  the PRIMARY KEY.
41210 2a 2a 20 3c 74 72 3e 3c 74 64 3e 20 39 74 68 20  ** <tr><td> 9th 
41220 3c 74 64 3e 20 69 6e 74 20 20 20 20 20 20 20 20  <td> int        
41230 20 3c 74 64 3e 20 54 72 75 65 20 69 66 20 63 6f   <td> True if co
41240 6c 75 6d 6e 20 69 73 20 41 55 54 4f 49 4e 43 52  lumn is AUTOINCR
41250 45 4d 45 4e 54 0a 2a 2a 20 3c 2f 74 61 62 6c 65  EMENT.** </table
41260 3e 0a 2a 2a 20 3c 2f 62 6c 6f 63 6b 71 75 6f 74  >.** </blockquot
41270 65 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 65 6d  e>.**.** The mem
41280 6f 72 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ory pointed to b
41290 79 20 74 68 65 20 63 68 61 72 61 63 74 65 72 20  y the character 
412a0 70 6f 69 6e 74 65 72 73 20 72 65 74 75 72 6e 65  pointers returne
412b0 64 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64 65 63  d for the.** dec
412c0 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 61 6e  laration type an
412d0 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
412e0 65 6e 63 65 20 69 73 20 76 61 6c 69 64 20 6f 6e  ence is valid on
412f0 6c 79 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78  ly until the nex
41300 74 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 61 6e 79  t.** call to any
41310 20 53 51 4c 69 74 65 20 41 50 49 20 66 75 6e 63   SQLite API func
41320 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  tion..**.** If t
41330 68 65 20 73 70 65 63 69 66 69 65 64 20 74 61 62  he specified tab
41340 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61  le is actually a
41350 20 76 69 65 77 2c 20 61 6e 20 5b 65 72 72 6f 72   view, an [error
41360 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75 72 6e   code] is return
41370 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ed..**.** If the
41380 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 75 6d   specified colum
41390 6e 20 69 73 20 22 72 6f 77 69 64 22 2c 20 22 6f  n is "rowid", "o
413a0 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f 22  id" or "_rowid_"
413b0 20 61 6e 64 20 61 6e 0a 2a 2a 20 49 4e 54 45 47   and an.** INTEG
413c0 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63  ER PRIMARY KEY c
413d0 6f 6c 75 6d 6e 20 68 61 73 20 62 65 65 6e 20 65  olumn has been e
413e0 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
413f0 65 64 2c 20 74 68 65 6e 20 74 68 65 20 6f 75 74  ed, then the out
41400 70 75 74 0a 2a 2a 20 70 61 72 61 6d 65 74 65 72  put.** parameter
41410 73 20 61 72 65 20 73 65 74 20 66 6f 72 20 74 68  s are set for th
41420 65 20 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63  e explicitly dec
41430 6c 61 72 65 64 20 63 6f 6c 75 6d 6e 2e 20 49 66  lared column. If
41440 20 74 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a 20   there is no.** 
41450 65 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61  explicitly decla
41460 72 65 64 20 49 4e 54 45 47 45 52 20 50 52 49 4d  red INTEGER PRIM
41470 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 2c 20  ARY KEY column, 
41480 74 68 65 6e 20 74 68 65 20 6f 75 74 70 75 74 0a  then the output.
41490 2a 2a 20 70 61 72 61 6d 65 74 65 72 73 20 61 72  ** parameters ar
414a0 65 20 73 65 74 20 61 73 20 66 6f 6c 6c 6f 77 73  e set as follows
414b0 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65 3e 0a 2a 2a  :.**.** <pre>.**
414c0 20 20 20 20 20 64 61 74 61 20 74 79 70 65 3a 20       data type: 
414d0 22 49 4e 54 45 47 45 52 22 0a 2a 2a 20 20 20 20  "INTEGER".**    
414e0 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
414f0 6e 63 65 3a 20 22 42 49 4e 41 52 59 22 0a 2a 2a  nce: "BINARY".**
41500 20 20 20 20 20 6e 6f 74 20 6e 75 6c 6c 3a 20 30       not null: 0
41510 0a 2a 2a 20 20 20 20 20 70 72 69 6d 61 72 79 20  .**     primary 
41520 6b 65 79 3a 20 31 0a 2a 2a 20 20 20 20 20 61 75  key: 1.**     au
41530 74 6f 20 69 6e 63 72 65 6d 65 6e 74 3a 20 30 0a  to increment: 0.
41540 2a 2a 20 3c 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20  ** </pre>.**.** 
41550 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
41560 79 20 6c 6f 61 64 20 6f 6e 65 20 6f 72 20 6d 6f  y load one or mo
41570 72 65 20 73 63 68 65 6d 61 73 20 66 72 6f 6d 20  re schemas from 
41580 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 20  database files. 
41590 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72 20 6f  If an.** error o
415a0 63 63 75 72 73 20 64 75 72 69 6e 67 20 74 68 69  ccurs during thi
415b0 73 20 70 72 6f 63 65 73 73 2c 20 6f 72 20 69 66  s process, or if
415c0 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74   the requested t
415d0 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 0a 2a  able or column.*
415e0 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e  * cannot be foun
415f0 64 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64  d, an [error cod
41600 65 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  e] is returned a
41610 6e 64 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  nd an error mess
41620 61 67 65 20 6c 65 66 74 0a 2a 2a 20 69 6e 20 74  age left.** in t
41630 68 65 20 5b 64 61 74 61 62 61 73 65 20 63 6f 6e  he [database con
41640 6e 65 63 74 69 6f 6e 5d 20 28 74 6f 20 62 65 20  nection] (to be 
41650 72 65 74 72 69 65 76 65 64 20 75 73 69 6e 67 20  retrieved using 
41660 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29  sqlite3_errmsg()
41670 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50  )..**.** This AP
41680 49 20 69 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61  I is only availa
41690 62 6c 65 20 69 66 20 74 68 65 20 6c 69 62 72 61  ble if the libra
416a0 72 79 20 77 61 73 20 63 6f 6d 70 69 6c 65 64 20  ry was compiled 
416b0 77 69 74 68 20 74 68 65 0a 2a 2a 20 5b 53 51 4c  with the.** [SQL
416c0 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
416d0 4e 5f 4d 45 54 41 44 41 54 41 5d 20 43 2d 70 72  N_METADATA] C-pr
416e0 65 70 72 6f 63 65 73 73 6f 72 20 73 79 6d 62 6f  eprocessor symbo
416f0 6c 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51  l defined..*/.SQ
41700 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
41710 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75 6d  ite3_table_colum
41720 6e 5f 6d 65 74 61 64 61 74 61 28 0a 20 20 73 71  n_metadata(.  sq
41730 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
41740 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e            /* Con
41750 6e 65 63 74 69 6f 6e 20 68 61 6e 64 6c 65 20 2a  nection handle *
41760 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
41770 7a 44 62 4e 61 6d 65 2c 20 20 20 20 20 20 20 20  zDbName,        
41780 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 61 6d 65  /* Database name
41790 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 6f   or NULL */.  co
417a0 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 6c 65  nst char *zTable
417b0 4e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 54 61 62  Name,     /* Tab
417c0 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e  le name */.  con
417d0 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d 6e  st char *zColumn
417e0 4e 61 6d 65 2c 20 20 20 20 2f 2a 20 43 6f 6c 75  Name,    /* Colu
417f0 6d 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61  mn name */.  cha
41800 72 20 63 6f 6e 73 74 20 2a 2a 70 7a 44 61 74 61  r const **pzData
41810 54 79 70 65 2c 20 20 20 20 2f 2a 20 4f 55 54 50  Type,    /* OUTP
41820 55 54 3a 20 44 65 63 6c 61 72 65 64 20 64 61 74  UT: Declared dat
41830 61 20 74 79 70 65 20 2a 2f 0a 20 20 63 68 61 72  a type */.  char
41840 20 63 6f 6e 73 74 20 2a 2a 70 7a 43 6f 6c 6c 53   const **pzCollS
41850 65 71 2c 20 20 20 20 20 2f 2a 20 4f 55 54 50 55  eq,     /* OUTPU
41860 54 3a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  T: Collation seq
41870 75 65 6e 63 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  uence name */.  
41880 69 6e 74 20 2a 70 4e 6f 74 4e 75 6c 6c 2c 20 20  int *pNotNull,  
41890 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
418a0 55 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 4e  UTPUT: True if N
418b0 4f 54 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69  OT NULL constrai
418c0 6e 74 20 65 78 69 73 74 73 20 2a 2f 0a 20 20 69  nt exists */.  i
418d0 6e 74 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 2c  nt *pPrimaryKey,
418e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55             /* OU
418f0 54 50 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f  TPUT: True if co
41900 6c 75 6d 6e 20 70 61 72 74 20 6f 66 20 50 4b 20  lumn part of PK 
41910 2a 2f 0a 20 20 69 6e 74 20 2a 70 41 75 74 6f 69  */.  int *pAutoi
41920 6e 63 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nc              
41930 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72 75 65   /* OUTPUT: True
41940 20 69 66 20 63 6f 6c 75 6d 6e 20 69 73 20 61 75   if column is au
41950 74 6f 2d 69 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a  to-increment */.
41960 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
41970 45 46 3a 20 4c 6f 61 64 20 41 6e 20 45 78 74 65  EF: Load An Exte
41980 6e 73 69 6f 6e 20 7b 48 31 32 36 30 30 7d 20 3c  nsion {H12600} <
41990 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68  S20500>.**.** Th
419a0 69 73 20 69 6e 74 65 72 66 61 63 65 20 6c 6f 61  is interface loa
419b0 64 73 20 61 6e 20 53 51 4c 69 74 65 20 65 78 74  ds an SQLite ext
419c0 65 6e 73 69 6f 6e 20 6c 69 62 72 61 72 79 20 66  ension library f
419d0 72 6f 6d 20 74 68 65 20 6e 61 6d 65 64 20 66 69  rom the named fi
419e0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30  le..**.** {H1260
419f0 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c  1} The sqlite3_l
41a00 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20  oad_extension() 
41a10 69 6e 74 65 72 66 61 63 65 20 61 74 74 65 6d 70  interface attemp
41a20 74 73 20 74 6f 20 6c 6f 61 64 20 61 6e 0a 2a 2a  ts to load an.**
41a30 20 20 20 20 20 20 20 20 20 20 53 51 4c 69 74 65            SQLite
41a40 20 65 78 74 65 6e 73 69 6f 6e 20 6c 69 62 72 61   extension libra
41a50 72 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20  ry contained in 
41a60 74 68 65 20 66 69 6c 65 20 7a 46 69 6c 65 2e 0a  the file zFile..
41a70 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30 32 7d 20 54  **.** {H12602} T
41a80 68 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 69  he entry point i
41a90 73 20 7a 50 72 6f 63 2e 0a 2a 2a 0a 2a 2a 20 7b  s zProc..**.** {
41aa0 48 31 32 36 30 33 7d 20 7a 50 72 6f 63 20 6d 61  H12603} zProc ma
41ab0 79 20 62 65 20 30 2c 20 69 6e 20 77 68 69 63 68  y be 0, in which
41ac0 20 63 61 73 65 20 74 68 65 20 6e 61 6d 65 20 6f   case the name o
41ad0 66 20 74 68 65 20 65 6e 74 72 79 20 70 6f 69 6e  f the entry poin
41ae0 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 64 65  t.**          de
41af0 66 61 75 6c 74 73 20 74 6f 20 22 73 71 6c 69 74  faults to "sqlit
41b00 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
41b10 74 22 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 30  t"..**.** {H1260
41b20 34 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6c  4} The sqlite3_l
41b30 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20  oad_extension() 
41b40 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20  interface shall 
41b50 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20 20  return.**       
41b60 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 6f     [SQLITE_OK] o
41b70 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20 5b 53  n success and [S
41b80 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 66 20  QLITE_ERROR] if 
41b90 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
41ba0 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32  rong..**.** {H12
41bb0 36 30 35 7d 20 49 66 20 61 6e 20 65 72 72 6f 72  605} If an error
41bc0 20 6f 63 63 75 72 73 20 61 6e 64 20 70 7a 45 72   occurs and pzEr
41bd0 72 4d 73 67 20 69 73 20 6e 6f 74 20 30 2c 20 74  rMsg is not 0, t
41be0 68 65 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20  hen the.**      
41bf0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61      [sqlite3_loa
41c00 64 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 69  d_extension()] i
41c10 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 61  nterface shall a
41c20 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 20 20 20  ttempt to.**    
41c30 20 20 20 20 20 20 66 69 6c 6c 20 2a 70 7a 45 72        fill *pzEr
41c40 72 4d 73 67 20 77 69 74 68 20 65 72 72 6f 72 20  rMsg with error 
41c50 6d 65 73 73 61 67 65 20 74 65 78 74 20 73 74 6f  message text sto
41c60 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 0a 2a 2a  red in memory.**
41c70 20 20 20 20 20 20 20 20 20 20 6f 62 74 61 69 6e            obtain
41c80 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
41c90 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 7b 45 4e 44  _malloc()]. {END
41ca0 7d 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  }  The calling f
41cb0 75 6e 63 74 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  unction.**      
41cc0 20 20 20 20 73 68 6f 75 6c 64 20 66 72 65 65 20      should free 
41cd0 74 68 69 73 20 6d 65 6d 6f 72 79 20 62 79 20 63  this memory by c
41ce0 61 6c 6c 69 6e 67 20 5b 73 71 6c 69 74 65 33 5f  alling [sqlite3_
41cf0 66 72 65 65 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b  free()]..**.** {
41d00 48 31 32 36 30 36 7d 20 45 78 74 65 6e 73 69 6f  H12606} Extensio
41d10 6e 20 6c 6f 61 64 69 6e 67 20 6d 75 73 74 20 62  n loading must b
41d20 65 20 65 6e 61 62 6c 65 64 20 75 73 69 6e 67 0a  e enabled using.
41d30 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
41d40 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64  ite3_enable_load
41d50 5f 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 70 72  _extension()] pr
41d60 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e 67 20 74  ior to calling t
41d70 68 69 73 20 41 50 49 2c 0a 2a 2a 20 20 20 20 20  his API,.**     
41d80 20 20 20 20 20 6f 74 68 65 72 77 69 73 65 20 61       otherwise a
41d90 6e 20 65 72 72 6f 72 20 77 69 6c 6c 20 62 65 20  n error will be 
41da0 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c  returned..*/.SQL
41db0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
41dc0 74 65 33 5f 6c 6f 61 64 5f 65 78 74 65 6e 73 69  te3_load_extensi
41dd0 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  on(.  sqlite3 *d
41de0 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  b,          /* L
41df0 6f 61 64 20 74 68 65 20 65 78 74 65 6e 73 69 6f  oad the extensio
41e00 6e 20 69 6e 74 6f 20 74 68 69 73 20 64 61 74 61  n into this data
41e10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
41e20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
41e30 2a 7a 46 69 6c 65 2c 20 20 20 20 2f 2a 20 4e 61  *zFile,    /* Na
41e40 6d 65 20 6f 66 20 74 68 65 20 73 68 61 72 65 64  me of the shared
41e50 20 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e   library contain
41e60 69 6e 67 20 65 78 74 65 6e 73 69 6f 6e 20 2a 2f  ing extension */
41e70 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
41e80 50 72 6f 63 2c 20 20 20 20 2f 2a 20 45 6e 74 72  Proc,    /* Entr
41e90 79 20 70 6f 69 6e 74 2e 20 20 44 65 72 69 76 65  y point.  Derive
41ea0 64 20 66 72 6f 6d 20 7a 46 69 6c 65 20 69 66 20  d from zFile if 
41eb0 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  0 */.  char **pz
41ec0 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a 20  ErrMsg       /* 
41ed0 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61 67  Put error messag
41ee0 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30 20  e here if not 0 
41ef0 2a 2f 0a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.);../*.** CAP
41f00 49 33 52 45 46 3a 20 45 6e 61 62 6c 65 20 4f 72  I3REF: Enable Or
41f10 20 44 69 73 61 62 6c 65 20 45 78 74 65 6e 73 69   Disable Extensi
41f20 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36  on Loading {H126
41f30 32 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a  20} <S20500>.**.
41f40 2a 2a 20 53 6f 20 61 73 20 6e 6f 74 20 74 6f 20  ** So as not to 
41f50 6f 70 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f  open security ho
41f60 6c 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70  les in older app
41f70 6c 69 63 61 74 69 6f 6e 73 20 74 68 61 74 20 61  lications that a
41f80 72 65 0a 2a 2a 20 75 6e 70 72 65 70 61 72 65 64  re.** unprepared
41f90 20 74 6f 20 64 65 61 6c 20 77 69 74 68 20 65 78   to deal with ex
41fa0 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 2c  tension loading,
41fb0 20 61 6e 64 20 61 73 20 61 20 6d 65 61 6e 73 20   and as a means 
41fc0 6f 66 20 64 69 73 61 62 6c 69 6e 67 0a 2a 2a 20  of disabling.** 
41fd0 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e  extension loadin
41fe0 67 20 77 68 69 6c 65 20 65 76 61 6c 75 61 74 69  g while evaluati
41ff0 6e 67 20 75 73 65 72 2d 65 6e 74 65 72 65 64 20  ng user-entered 
42000 53 51 4c 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  SQL, the followi
42010 6e 67 20 41 50 49 0a 2a 2a 20 69 73 20 70 72 6f  ng API.** is pro
42020 76 69 64 65 64 20 74 6f 20 74 75 72 6e 20 74 68  vided to turn th
42030 65 20 5b 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f  e [sqlite3_load_
42040 65 78 74 65 6e 73 69 6f 6e 28 29 5d 20 6d 65 63  extension()] mec
42050 68 61 6e 69 73 6d 20 6f 6e 20 61 6e 64 20 6f 66  hanism on and of
42060 66 2e 0a 2a 2a 0a 2a 2a 20 45 78 74 65 6e 73 69  f..**.** Extensi
42070 6f 6e 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66  on loading is of
42080 66 20 62 79 20 64 65 66 61 75 6c 74 2e 20 53 65  f by default. Se
42090 65 20 74 69 63 6b 65 74 20 23 31 38 36 33 2e 0a  e ticket #1863..
420a0 2a 2a 0a 2a 2a 20 7b 48 31 32 36 32 31 7d 20 43  **.** {H12621} C
420b0 61 6c 6c 20 74 68 65 20 73 71 6c 69 74 65 33 5f  all the sqlite3_
420c0 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74 65  enable_load_exte
420d0 6e 73 69 6f 6e 28 29 20 72 6f 75 74 69 6e 65 20  nsion() routine 
420e0 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 31 0a 2a 2a  with onoff==1.**
420f0 20 20 20 20 20 20 20 20 20 20 74 6f 20 74 75 72            to tur
42100 6e 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  n extension load
42110 69 6e 67 20 6f 6e 20 61 6e 64 20 63 61 6c 6c 20  ing on and call 
42120 69 74 20 77 69 74 68 20 6f 6e 6f 66 66 3d 3d 30  it with onoff==0
42130 20 74 6f 20 74 75 72 6e 0a 2a 2a 20 20 20 20 20   to turn.**     
42140 20 20 20 20 20 69 74 20 62 61 63 6b 20 6f 66 66       it back off
42150 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 7b 48   again..**.** {H
42160 31 32 36 32 32 7d 20 45 78 74 65 6e 73 69 6f 6e  12622} Extension
42170 20 6c 6f 61 64 69 6e 67 20 69 73 20 6f 66 66 20   loading is off 
42180 62 79 20 64 65 66 61 75 6c 74 2e 0a 2a 2f 0a 53  by default..*/.S
42190 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
421a0 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61  lite3_enable_loa
421b0 64 5f 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69  d_extension(sqli
421c0 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f  te3 *db, int ono
421d0 66 66 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  ff);../*.** CAPI
421e0 33 52 45 46 3a 20 41 75 74 6f 6d 61 74 69 63 61  3REF: Automatica
421f0 6c 6c 79 20 4c 6f 61 64 20 41 6e 20 45 78 74 65  lly Load An Exte
42200 6e 73 69 6f 6e 73 20 7b 48 31 32 36 34 30 7d 20  nsions {H12640} 
42210 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a 2a 2a 20 54  <S20500>.**.** T
42220 68 69 73 20 41 50 49 20 63 61 6e 20 62 65 20 69  his API can be i
42230 6e 76 6f 6b 65 64 20 61 74 20 70 72 6f 67 72 61  nvoked at progra
42240 6d 20 73 74 61 72 74 75 70 20 69 6e 20 6f 72 64  m startup in ord
42250 65 72 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a  er to register.*
42260 2a 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 73 74  * one or more st
42270 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 65 64 20  atically linked 
42280 65 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20  extensions that 
42290 77 69 6c 6c 20 62 65 20 61 76 61 69 6c 61 62 6c  will be availabl
422a0 65 0a 2a 2a 20 74 6f 20 61 6c 6c 20 6e 65 77 20  e.** to all new 
422b0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
422c0 74 69 6f 6e 73 5d 2e 20 7b 45 4e 44 7d 0a 2a 2a  tions]. {END}.**
422d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
422e0 20 73 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65   stores a pointe
422f0 72 20 74 6f 20 74 68 65 20 65 78 74 65 6e 73 69  r to the extensi
42300 6f 6e 20 69 6e 20 61 6e 20 61 72 72 61 79 20 74  on in an array t
42310 68 61 74 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e  hat is.** obtain
42320 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65 33  ed from [sqlite3
42330 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 20 20 49 66 20  _malloc()].  If 
42340 79 6f 75 20 72 75 6e 20 61 20 6d 65 6d 6f 72 79  you run a memory
42350 20 6c 65 61 6b 20 63 68 65 63 6b 65 72 0a 2a 2a   leak checker.**
42360 20 6f 6e 20 79 6f 75 72 20 70 72 6f 67 72 61 6d   on your program
42370 20 61 6e 64 20 69 74 20 72 65 70 6f 72 74 73 20   and it reports 
42380 61 20 6c 65 61 6b 20 62 65 63 61 75 73 65 20 6f  a leak because o
42390 66 20 74 68 69 73 20 61 72 72 61 79 2c 20 69 6e  f this array, in
423a0 76 6f 6b 65 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  voke.** [sqlite3
423b0 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74 65  _reset_auto_exte
423c0 6e 73 69 6f 6e 28 29 5d 20 70 72 69 6f 72 20 74  nsion()] prior t
423d0 6f 20 73 68 75 74 64 6f 77 6e 20 74 6f 20 66 72  o shutdown to fr
423e0 65 65 20 74 68 65 20 6d 65 6d 6f 72 79 2e 0a 2a  ee the memory..*
423f0 2a 0a 2a 2a 20 7b 48 31 32 36 34 31 7d 20 54 68  *.** {H12641} Th
42400 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  is function regi
42410 73 74 65 72 73 20 61 6e 20 65 78 74 65 6e 73 69  sters an extensi
42420 6f 6e 20 65 6e 74 72 79 20 70 6f 69 6e 74 20 74  on entry point t
42430 68 61 74 20 69 73 0a 2a 2a 20 20 20 20 20 20 20  hat is.**       
42440 20 20 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79     automatically
42450 20 69 6e 76 6f 6b 65 64 20 77 68 65 6e 65 76 65   invoked wheneve
42460 72 20 61 20 6e 65 77 20 5b 64 61 74 61 62 61 73  r a new [databas
42470 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 0a 2a 2a  e connection].**
42480 20 20 20 20 20 20 20 20 20 20 69 73 20 6f 70 65            is ope
42490 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ned using [sqlit
424a0 65 33 5f 6f 70 65 6e 28 29 5d 2c 20 5b 73 71 6c  e3_open()], [sql
424b0 69 74 65 33 5f 6f 70 65 6e 31 36 28 29 5d 2c 0a  ite3_open16()],.
424c0 2a 2a 20 20 20 20 20 20 20 20 20 20 6f 72 20 5b  **          or [
424d0 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28  sqlite3_open_v2(
424e0 29 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 32 36 34  )]..**.** {H1264
424f0 32 7d 20 44 75 70 6c 69 63 61 74 65 20 65 78 74  2} Duplicate ext
42500 65 6e 73 69 6f 6e 73 20 61 72 65 20 64 65 74 65  ensions are dete
42510 63 74 65 64 20 73 6f 20 63 61 6c 6c 69 6e 67 20  cted so calling 
42520 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
42530 20 20 20 20 20 20 20 20 20 6d 75 6c 74 69 70 6c           multipl
42540 65 20 74 69 6d 65 73 20 77 69 74 68 20 74 68 65  e times with the
42550 20 73 61 6d 65 20 65 78 74 65 6e 73 69 6f 6e 20   same extension 
42560 69 73 20 68 61 72 6d 6c 65 73 73 2e 0a 2a 2a 0a  is harmless..**.
42570 2a 2a 20 7b 48 31 32 36 34 33 7d 20 54 68 69 73  ** {H12643} This
42580 20 72 6f 75 74 69 6e 65 20 73 74 6f 72 65 73 20   routine stores 
42590 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
425a0 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 20 61 6e   extension in an
425b0 20 61 72 72 61 79 0a 2a 2a 20 20 20 20 20 20 20   array.**       
425c0 20 20 20 74 68 61 74 20 69 73 20 6f 62 74 61 69     that is obtai
425d0 6e 65 64 20 66 72 6f 6d 20 5b 73 71 6c 69 74 65  ned from [sqlite
425e0 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e 0a 2a 2a 0a  3_malloc()]..**.
425f0 2a 2a 20 7b 48 31 32 36 34 34 7d 20 41 75 74 6f  ** {H12644} Auto
42600 6d 61 74 69 63 20 65 78 74 65 6e 73 69 6f 6e 73  matic extensions
42610 20 61 70 70 6c 79 20 61 63 72 6f 73 73 20 61 6c   apply across al
42620 6c 20 74 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51  l threads..*/.SQ
42630 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
42640 69 74 65 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73  ite3_auto_extens
42650 69 6f 6e 28 76 6f 69 64 20 2a 78 45 6e 74 72 79  ion(void *xEntry
42660 50 6f 69 6e 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43  Point);../*.** C
42670 41 50 49 33 52 45 46 3a 20 52 65 73 65 74 20 41  API3REF: Reset A
42680 75 74 6f 6d 61 74 69 63 20 45 78 74 65 6e 73 69  utomatic Extensi
42690 6f 6e 20 4c 6f 61 64 69 6e 67 20 7b 48 31 32 36  on Loading {H126
426a0 36 30 7d 20 3c 53 32 30 35 30 30 3e 0a 2a 2a 0a  60} <S20500>.**.
426b0 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
426c0 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20 70 72   disables all pr
426d0 65 76 69 6f 75 73 6c 79 20 72 65 67 69 73 74 65  eviously registe
426e0 72 65 64 20 61 75 74 6f 6d 61 74 69 63 0a 2a 2a  red automatic.**
426f0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 20 7b 45 4e   extensions. {EN
42700 44 7d 20 20 49 74 20 75 6e 64 6f 65 73 20 74 68  D}  It undoes th
42710 65 20 65 66 66 65 63 74 20 6f 66 20 61 6c 6c 20  e effect of all 
42720 70 72 69 6f 72 0a 2a 2a 20 5b 73 71 6c 69 74 65  prior.** [sqlite
42730 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  3_auto_extension
42740 28 29 5d 20 63 61 6c 6c 73 2e 0a 2a 2a 0a 2a 2a  ()] calls..**.**
42750 20 7b 48 31 32 36 36 31 7d 20 54 68 69 73 20 66   {H12661} This f
42760 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c 65 73  unction disables
42770 20 61 6c 6c 20 70 72 65 76 69 6f 75 73 6c 79 20   all previously 
42780 72 65 67 69 73 74 65 72 65 64 0a 2a 2a 20 20 20  registered.**   
42790 20 20 20 20 20 20 20 61 75 74 6f 6d 61 74 69 63         automatic
427a0 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2a 0a   extensions..**.
427b0 2a 2a 20 7b 48 31 32 36 36 32 7d 20 54 68 69 73  ** {H12662} This
427c0 20 66 75 6e 63 74 69 6f 6e 20 64 69 73 61 62 6c   function disabl
427d0 65 73 20 61 75 74 6f 6d 61 74 69 63 20 65 78 74  es automatic ext
427e0 65 6e 73 69 6f 6e 73 20 69 6e 20 61 6c 6c 20 74  ensions in all t
427f0 68 72 65 61 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54  hreads..*/.SQLIT
42800 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
42810 65 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78  e3_reset_auto_ex
42820 74 65 6e 73 69 6f 6e 28 76 6f 69 64 29 3b 0a 0a  tension(void);..
42830 2f 2a 0a 2a 2a 2a 2a 2a 2a 20 45 58 50 45 52 49  /*.****** EXPERI
42840 4d 45 4e 54 41 4c 20 2d 20 73 75 62 6a 65 63 74  MENTAL - subject
42850 20 74 6f 20 63 68 61 6e 67 65 20 77 69 74 68 6f   to change witho
42860 75 74 20 6e 6f 74 69 63 65 20 2a 2a 2a 2a 2a 2a  ut notice ******
42870 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
42880 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20  he interface to 
42890 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  the virtual-tabl
428a0 65 20 6d 65 63 68 61 6e 69 73 6d 20 69 73 20 63  e mechanism is c
428b0 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69 64 65  urrently conside
428c0 72 65 64 0a 2a 2a 20 74 6f 20 62 65 20 65 78 70  red.** to be exp
428d0 65 72 69 6d 65 6e 74 61 6c 2e 20 20 54 68 65 20  erimental.  The 
428e0 69 6e 74 65 72 66 61 63 65 20 6d 69 67 68 74 20  interface might 
428f0 63 68 61 6e 67 65 20 69 6e 20 69 6e 63 6f 6d 70  change in incomp
42900 61 74 69 62 6c 65 20 77 61 79 73 2e 0a 2a 2a 20  atible ways..** 
42910 49 66 20 74 68 69 73 20 69 73 20 61 20 70 72 6f  If this is a pro
42920 62 6c 65 6d 20 66 6f 72 20 79 6f 75 2c 20 64 6f  blem for you, do
42930 20 6e 6f 74 20 75 73 65 20 74 68 65 20 69 6e 74   not use the int
42940 65 72 66 61 63 65 20 61 74 20 74 68 69 73 20 74  erface at this t
42950 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  ime..**.** When 
42960 74 68 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c  the virtual-tabl
42970 65 20 6d 65 63 68 61 6e 69 73 6d 20 73 74 61 62  e mechanism stab
42980 69 6c 69 7a 65 73 2c 20 77 65 20 77 69 6c 6c 20  ilizes, we will 
42990 64 65 63 6c 61 72 65 20 74 68 65 0a 2a 2a 20 69  declare the.** i
429a0 6e 74 65 72 66 61 63 65 20 66 69 78 65 64 2c 20  nterface fixed, 
429b0 73 75 70 70 6f 72 74 20 69 74 20 69 6e 64 65 66  support it indef
429c0 69 6e 69 74 65 6c 79 2c 20 61 6e 64 20 72 65 6d  initely, and rem
429d0 6f 76 65 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74  ove this comment
429e0 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 74 72 75  ..*/../*.** Stru
429f0 63 74 75 72 65 73 20 75 73 65 64 20 62 79 20 74  ctures used by t
42a00 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
42a10 20 69 6e 74 65 72 66 61 63 65 0a 2a 2f 0a 74 79   interface.*/.ty
42a20 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
42a30 69 74 65 33 5f 76 74 61 62 20 73 71 6c 69 74 65  ite3_vtab sqlite
42a40 33 5f 76 74 61 62 3b 0a 74 79 70 65 64 65 66 20  3_vtab;.typedef 
42a50 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
42a60 6e 64 65 78 5f 69 6e 66 6f 20 73 71 6c 69 74 65  ndex_info sqlite
42a70 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 3b 0a 74 79  3_index_info;.ty
42a80 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c  pedef struct sql
42a90 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
42aa0 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
42ab0 72 73 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74  rsor;.typedef st
42ac0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  ruct sqlite3_mod
42ad0 75 6c 65 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75  ule sqlite3_modu
42ae0 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33  le;../*.** CAPI3
42af0 52 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62  REF: Virtual Tab
42b00 6c 65 20 4f 62 6a 65 63 74 20 7b 48 31 38 30 30  le Object {H1800
42b10 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 4b  0} <S20400>.** K
42b20 45 59 57 4f 52 44 53 3a 20 73 71 6c 69 74 65 33  EYWORDS: sqlite3
42b30 5f 6d 6f 64 75 6c 65 0a 2a 2a 20 45 58 50 45 52  _module.** EXPER
42b40 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 41 20  IMENTAL.**.** A 
42b50 6d 6f 64 75 6c 65 20 69 73 20 61 20 63 6c 61 73  module is a clas
42b60 73 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61 62  s of virtual tab
42b70 6c 65 73 2e 20 20 45 61 63 68 20 6d 6f 64 75 6c  les.  Each modul
42b80 65 20 69 73 20 64 65 66 69 6e 65 64 0a 2a 2a 20  e is defined.** 
42b90 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
42ba0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
42bb0 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69 73  structure.  This
42bc0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 73 69   structure consi
42bd0 73 74 73 0a 2a 2a 20 6d 6f 73 74 6c 79 20 6f 66  sts.** mostly of
42be0 20 6d 65 74 68 6f 64 73 20 66 6f 72 20 74 68 65   methods for the
42bf0 20 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54   module..**.** T
42c00 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
42c10 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e   experimental an
42c20 64 20 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20  d is subject to 
42c30 63 68 61 6e 67 65 20 6f 72 0a 2a 2a 20 72 65 6d  change or.** rem
42c40 6f 76 61 6c 20 69 6e 20 66 75 74 75 72 65 20 72  oval in future r
42c50 65 6c 65 61 73 65 73 20 6f 66 20 53 51 4c 69 74  eleases of SQLit
42c60 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c  e..*/.struct sql
42c70 69 74 65 33 5f 6d 6f 64 75 6c 65 20 7b 0a 20 20  ite3_module {.  
42c80 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a 20 20  int iVersion;.  
42c90 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28 73  int (*xCreate)(s
42ca0 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 20 2a 70  qlite3*, void *p
42cb0 41 75 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Aux,.           
42cc0 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f      int argc, co
42cd0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a  nst char *const*
42ce0 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20 20 20  argv,.          
42cf0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
42d00 62 20 2a 2a 70 70 56 54 61 62 2c 20 63 68 61 72  b **ppVTab, char
42d10 2a 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6f  **);.  int (*xCo
42d20 6e 6e 65 63 74 29 28 73 71 6c 69 74 65 33 2a 2c  nnect)(sqlite3*,
42d30 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 20   void *pAux,.   
42d40 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
42d50 61 72 67 63 2c 20 63 6f 6e 73 74 20 63 68 61 72  argc, const char
42d60 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20   *const*argv,.  
42d70 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
42d80 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54  ite3_vtab **ppVT
42d90 61 62 2c 20 63 68 61 72 2a 2a 29 3b 0a 20 20 69  ab, char**);.  i
42da0 6e 74 20 28 2a 78 42 65 73 74 49 6e 64 65 78 29  nt (*xBestIndex)
42db0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
42dc0 56 54 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e  VTab, sqlite3_in
42dd0 64 65 78 5f 69 6e 66 6f 2a 29 3b 0a 20 20 69 6e  dex_info*);.  in
42de0 74 20 28 2a 78 44 69 73 63 6f 6e 6e 65 63 74 29  t (*xDisconnect)
42df0 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
42e00 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
42e10 44 65 73 74 72 6f 79 29 28 73 71 6c 69 74 65 33  Destroy)(sqlite3
42e20 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a 20  _vtab *pVTab);. 
42e30 20 69 6e 74 20 28 2a 78 4f 70 65 6e 29 28 73 71   int (*xOpen)(sq
42e40 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
42e50 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  b, sqlite3_vtab_
42e60 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72 73 6f  cursor **ppCurso
42e70 72 29 3b 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f  r);.  int (*xClo
42e80 73 65 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62  se)(sqlite3_vtab
42e90 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74  _cursor*);.  int
42ea0 20 28 2a 78 46 69 6c 74 65 72 29 28 73 71 6c 69   (*xFilter)(sqli
42eb0 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 2a  te3_vtab_cursor*
42ec0 2c 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f  , int idxNum, co
42ed0 6e 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72  nst char *idxStr
42ee0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
42ef0 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69    int argc, sqli
42f00 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
42f10 29 3b 0a 20 20 69 6e 74 20 28 2a 78 4e 65 78 74  );.  int (*xNext
42f20 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  )(sqlite3_vtab_c
42f30 75 72 73 6f 72 2a 29 3b 0a 20 20 69 6e 74 20 28  ursor*);.  int (
42f40 2a 78 45 6f 66 29 28 73 71 6c 69 74 65 33 5f 76  *xEof)(sqlite3_v
42f50 74 61 62 5f 63 75 72 73 6f 72 2a 29 3b 0a 20 20  tab_cursor*);.  
42f60 69 6e 74 20 28 2a 78 43 6f 6c 75 6d 6e 29 28 73  int (*xColumn)(s
42f70 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
42f80 6f 72 2a 2c 20 73 71 6c 69 74 65 33 5f 63 6f 6e  or*, sqlite3_con
42f90 74 65 78 74 2a 2c 20 69 6e 74 29 3b 0a 20 20 69  text*, int);.  i
42fa0 6e 74 20 28 2a 78 52 6f 77 69 64 29 28 73 71 6c  nt (*xRowid)(sql
42fb0 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72  ite3_vtab_cursor
42fc0 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
42fd0 20 2a 70 52 6f 77 69 64 29 3b 0a 20 20 69 6e 74   *pRowid);.  int
42fe0 20 28 2a 78 55 70 64 61 74 65 29 28 73 71 6c 69   (*xUpdate)(sqli
42ff0 74 65 33 5f 76 74 61 62 20 2a 2c 20 69 6e 74 2c  te3_vtab *, int,
43000 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
43010 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  *, sqlite3_int64
43020 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 78 42 65   *);.  int (*xBe
43030 67 69 6e 29 28 73 71 6c 69 74 65 33 5f 76 74 61  gin)(sqlite3_vta
43040 62 20 2a 70 56 54 61 62 29 3b 0a 20 20 69 6e 74  b *pVTab);.  int
43050 20 28 2a 78 53 79 6e 63 29 28 73 71 6c 69 74 65   (*xSync)(sqlite
43060 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
43070 20 20 69 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 29    int (*xCommit)
43080 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
43090 56 54 61 62 29 3b 0a 20 20 69 6e 74 20 28 2a 78  VTab);.  int (*x
430a0 52 6f 6c 6c 62 61 63 6b 29 28 73 71 6c 69 74 65  Rollback)(sqlite
430b0 33 5f 76 74 61 62 20 2a 70 56 54 61 62 29 3b 0a  3_vtab *pVTab);.
430c0 20 20 69 6e 74 20 28 2a 78 46 69 6e 64 46 75 6e    int (*xFindFun
430d0 63 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 5f 76  ction)(sqlite3_v
430e0 74 61 62 20 2a 70 56 74 61 62 2c 20 69 6e 74 20  tab *pVtab, int 
430f0 6e 41 72 67 2c 20 63 6f 6e 73 74 20 63 68 61 72  nArg, const char
43100 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20   *zName,.       
43110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
43120 76 6f 69 64 20 28 2a 2a 70 78 46 75 6e 63 29 28  void (**pxFunc)(
43130 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
43140 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c  ,int,sqlite3_val
43150 75 65 2a 2a 29 2c 0a 20 20 20 20 20 20 20 20 20  ue**),.         
43160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 6f                vo
43170 69 64 20 2a 2a 70 70 41 72 67 29 3b 0a 20 20 69  id **ppArg);.  i
43180 6e 74 20 28 2a 78 52 65 6e 61 6d 65 29 28 73 71  nt (*xRename)(sq
43190 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
431a0 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
431b0 4e 65 77 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  New);.};../*.** 
431c0 43 41 50 49 33 52 45 46 3a 20 56 69 72 74 75 61  CAPI3REF: Virtua
431d0 6c 20 54 61 62 6c 65 20 49 6e 64 65 78 69 6e 67  l Table Indexing
431e0 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 7b 48 31   Information {H1
431f0 38 31 30 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8100} <S20400>.*
43200 2a 20 4b 45 59 57 4f 52 44 53 3a 20 73 71 6c 69  * KEYWORDS: sqli
43210 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a  te3_index_info.*
43220 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
43230 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
43240 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
43250 63 74 75 72 65 20 61 6e 64 20 69 74 73 20 73 75  cture and its su
43260 62 73 74 72 75 63 74 75 72 65 73 20 69 73 20 75  bstructures is u
43270 73 65 64 20 74 6f 0a 2a 2a 20 70 61 73 73 20 69  sed to.** pass i
43280 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20  nformation into 
43290 61 6e 64 20 72 65 63 65 69 76 65 20 74 68 65 20  and receive the 
432a0 72 65 70 6c 79 20 66 72 6f 6d 20 74 68 65 20 78  reply from the x
432b0 42 65 73 74 49 6e 64 65 78 0a 2a 2a 20 6d 65 74  BestIndex.** met
432c0 68 6f 64 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  hod of an sqlite
432d0 33 5f 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 66  3_module.  The f
432e0 69 65 6c 64 73 20 75 6e 64 65 72 20 2a 2a 49 6e  ields under **In
432f0 70 75 74 73 2a 2a 20 61 72 65 20 74 68 65 0a 2a  puts** are the.*
43300 2a 20 69 6e 70 75 74 73 20 74 6f 20 78 42 65 73  * inputs to xBes
43310 74 49 6e 64 65 78 20 61 6e 64 20 61 72 65 20 72  tIndex and are r
43320 65 61 64 2d 6f 6e 6c 79 2e 20 20 78 42 65 73 74  ead-only.  xBest
43330 49 6e 64 65 78 20 69 6e 73 65 72 74 73 20 69 74  Index inserts it
43340 73 0a 2a 2a 20 72 65 73 75 6c 74 73 20 69 6e 74  s.** results int
43350 6f 20 74 68 65 20 2a 2a 4f 75 74 70 75 74 73 2a  o the **Outputs*
43360 2a 20 66 69 65 6c 64 73 2e 0a 2a 2a 0a 2a 2a 20  * fields..**.** 
43370 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  The aConstraint[
43380 5d 20 61 72 72 61 79 20 72 65 63 6f 72 64 73 20  ] array records 
43390 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
433a0 73 74 72 61 69 6e 74 73 20 6f 66 20 74 68 65 20  straints of the 
433b0 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20 3c 70 72 65  form:.**.** <pre
433c0 3e 63 6f 6c 75 6d 6e 20 4f 50 20 65 78 70 72 3c  >column OP expr<
433d0 2f 70 72 65 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72  /pre>.**.** wher
433e0 65 20 4f 50 20 69 73 20 3d 2c 20 26 6c 74 3b 2c  e OP is =, &lt;,
433f0 20 26 6c 74 3b 3d 2c 20 26 67 74 3b 2c 20 6f 72   &lt;=, &gt;, or
43400 20 26 67 74 3b 3d 2e 20 20 54 68 65 20 70 61 72   &gt;=.  The par
43410 74 69 63 75 6c 61 72 20 6f 70 65 72 61 74 6f 72  ticular operator
43420 20 69 73 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e   is.** stored in
43430 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e 6f   aConstraint[].o
43440 70 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f 66  p.  The index of
43450 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 73   the column is s
43460 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 61 43 6f 6e  tored in.** aCon
43470 73 74 72 61 69 6e 74 5b 5d 2e 69 43 6f 6c 75 6d  straint[].iColum
43480 6e 2e 20 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b  n.  aConstraint[
43490 5d 2e 75 73 61 62 6c 65 20 69 73 20 54 52 55 45  ].usable is TRUE
434a0 20 69 66 20 74 68 65 0a 2a 2a 20 65 78 70 72 20   if the.** expr 
434b0 6f 6e 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  on the right-han
434c0 64 20 73 69 64 65 20 63 61 6e 20 62 65 20 65 76  d side can be ev
434d0 61 6c 75 61 74 65 64 20 28 61 6e 64 20 74 68 75  aluated (and thu
434e0 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74  s the constraint
434f0 0a 2a 2a 20 69 73 20 75 73 61 62 6c 65 29 20 61  .** is usable) a
43500 6e 64 20 66 61 6c 73 65 20 69 66 20 69 74 20 63  nd false if it c
43510 61 6e 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  annot..**.** The
43520 20 6f 70 74 69 6d 69 7a 65 72 20 61 75 74 6f 6d   optimizer autom
43530 61 74 69 63 61 6c 6c 79 20 69 6e 76 65 72 74 73  atically inverts
43540 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f   terms of the fo
43550 72 6d 20 22 65 78 70 72 20 4f 50 20 63 6f 6c 75  rm "expr OP colu
43560 6d 6e 22 0a 2a 2a 20 61 6e 64 20 6d 61 6b 65 73  mn".** and makes
43570 20 6f 74 68 65 72 20 73 69 6d 70 6c 69 66 69 63   other simplific
43580 61 74 69 6f 6e 73 20 74 6f 20 74 68 65 20 57 48  ations to the WH
43590 45 52 45 20 63 6c 61 75 73 65 20 69 6e 20 61 6e  ERE clause in an
435a0 20 61 74 74 65 6d 70 74 20 74 6f 0a 2a 2a 20 67   attempt to.** g
435b0 65 74 20 61 73 20 6d 61 6e 79 20 57 48 45 52 45  et as many WHERE
435c0 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 69 6e   clause terms in
435d0 74 6f 20 74 68 65 20 66 6f 72 6d 20 73 68 6f 77  to the form show
435e0 6e 20 61 62 6f 76 65 20 61 73 20 70 6f 73 73 69  n above as possi
435f0 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 61 43 6f 6e  ble..** The aCon
43600 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79 20  straint[] array 
43610 6f 6e 6c 79 20 72 65 70 6f 72 74 73 20 57 48 45  only reports WHE
43620 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20  RE clause terms 
43630 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74 0a 2a  in the correct.*
43640 2a 20 66 6f 72 6d 20 74 68 61 74 20 72 65 66 65  * form that refe
43650 72 20 74 6f 20 74 68 65 20 70 61 72 74 69 63 75  r to the particu
43660 6c 61 72 20 76 69 72 74 75 61 6c 20 74 61 62 6c  lar virtual tabl
43670 65 20 62 65 69 6e 67 20 71 75 65 72 69 65 64 2e  e being queried.
43680 0a 2a 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74 69  .**.** Informati
43690 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 4f 52 44  on about the ORD
436a0 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73 20  ER BY clause is 
436b0 73 74 6f 72 65 64 20 69 6e 20 61 4f 72 64 65 72  stored in aOrder
436c0 42 79 5b 5d 2e 0a 2a 2a 20 45 61 63 68 20 74 65  By[]..** Each te
436d0 72 6d 20 6f 66 20 61 4f 72 64 65 72 42 79 20 72  rm of aOrderBy r
436e0 65 63 6f 72 64 73 20 61 20 63 6f 6c 75 6d 6e 20  ecords a column 
436f0 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 20  of the ORDER BY 
43700 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  clause..**.** Th
43710 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
43720 68 6f 64 20 6d 75 73 74 20 66 69 6c 6c 20 61 43  hod must fill aC
43730 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d  onstraintUsage[]
43740 20 77 69 74 68 20 69 6e 66 6f 72 6d 61 74 69 6f   with informatio
43750 6e 0a 2a 2a 20 61 62 6f 75 74 20 77 68 61 74 20  n.** about what 
43760 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 70 61  parameters to pa
43770 73 73 20 74 6f 20 78 46 69 6c 74 65 72 2e 20 20  ss to xFilter.  
43780 49 66 20 61 72 67 76 49 6e 64 65 78 3e 30 20 74  If argvIndex>0 t
43790 68 65 6e 0a 2a 2a 20 74 68 65 20 72 69 67 68 74  hen.** the right
437a0 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
437b0 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
437c0 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 20 69 73  aConstraint[] is
437d0 20 65 76 61 6c 75 61 74 65 64 0a 2a 2a 20 61 6e   evaluated.** an
437e0 64 20 62 65 63 6f 6d 65 73 20 74 68 65 20 61 72  d becomes the ar
437f0 67 76 49 6e 64 65 78 2d 74 68 20 65 6e 74 72 79  gvIndex-th entry
43800 20 69 6e 20 61 72 67 76 2e 20 20 49 66 20 61 43   in argv.  If aC
43810 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 5d  onstraintUsage[]
43820 2e 6f 6d 69 74 0a 2a 2a 20 69 73 20 74 72 75 65  .omit.** is true
43830 2c 20 74 68 65 6e 20 74 68 65 20 63 6f 6e 73 74  , then the const
43840 72 61 69 6e 74 20 69 73 20 61 73 73 75 6d 65 64  raint is assumed
43850 20 74 6f 20 62 65 20 66 75 6c 6c 79 20 68 61 6e   to be fully han
43860 64 6c 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 76  dled by the.** v
43870 69 72 74 75 61 6c 20 74 61 62 6c 65 20 61 6e 64  irtual table and
43880 20 69 73 20 6e 6f 74 20 63 68 65 63 6b 65 64 20   is not checked 
43890 61 67 61 69 6e 20 62 79 20 53 51 4c 69 74 65 2e  again by SQLite.
438a0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 64 78 4e 75  .**.** The idxNu
438b0 6d 20 61 6e 64 20 69 64 78 50 74 72 20 76 61 6c  m and idxPtr val
438c0 75 65 73 20 61 72 65 20 72 65 63 6f 72 64 65 64  ues are recorded
438d0 20 61 6e 64 20 70 61 73 73 65 64 20 69 6e 74 6f   and passed into
438e0 20 78 46 69 6c 74 65 72 2e 0a 2a 2a 20 73 71 6c   xFilter..** sql
438f0 69 74 65 33 5f 66 72 65 65 28 29 20 69 73 20 75  ite3_free() is u
43900 73 65 64 20 74 6f 20 66 72 65 65 20 69 64 78 50  sed to free idxP
43910 74 72 20 69 66 20 6e 65 65 64 54 6f 46 72 65 65  tr if needToFree
43920 49 64 78 50 74 72 20 69 73 20 74 72 75 65 2e 0a  IdxPtr is true..
43930 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 64 65 72 42  **.** The orderB
43940 79 43 6f 6e 73 75 6d 65 64 20 6d 65 61 6e 73 20  yConsumed means 
43950 74 68 61 74 20 6f 75 74 70 75 74 20 66 72 6f 6d  that output from
43960 20 78 46 69 6c 74 65 72 20 77 69 6c 6c 20 6f 63   xFilter will oc
43970 63 75 72 20 69 6e 0a 2a 2a 20 74 68 65 20 63 6f  cur in.** the co
43980 72 72 65 63 74 20 6f 72 64 65 72 20 74 6f 20 73  rrect order to s
43990 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52  atisfy the ORDER
439a0 20 42 59 20 63 6c 61 75 73 65 20 73 6f 20 74 68   BY clause so th
439b0 61 74 20 6e 6f 20 73 65 70 61 72 61 74 65 0a 2a  at no separate.*
439c0 2a 20 73 6f 72 74 69 6e 67 20 73 74 65 70 20 69  * sorting step i
439d0 73 20 72 65 71 75 69 72 65 64 2e 0a 2a 2a 0a 2a  s required..**.*
439e0 2a 20 54 68 65 20 65 73 74 69 6d 61 74 65 64 43  * The estimatedC
439f0 6f 73 74 20 76 61 6c 75 65 20 69 73 20 61 6e 20  ost value is an 
43a00 65 73 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20  estimate of the 
43a10 63 6f 73 74 20 6f 66 20 64 6f 69 6e 67 20 74 68  cost of doing th
43a20 65 0a 2a 2a 20 70 61 72 74 69 63 75 6c 61 72 20  e.** particular 
43a30 6c 6f 6f 6b 75 70 2e 20 20 41 20 66 75 6c 6c 20  lookup.  A full 
43a40 73 63 61 6e 20 6f 66 20 61 20 74 61 62 6c 65 20  scan of a table 
43a50 77 69 74 68 20 4e 20 65 6e 74 72 69 65 73 20 73  with N entries s
43a60 68 6f 75 6c 64 20 68 61 76 65 0a 2a 2a 20 61 20  hould have.** a 
43a70 63 6f 73 74 20 6f 66 20 4e 2e 20 20 41 20 62 69  cost of N.  A bi
43a80 6e 61 72 79 20 73 65 61 72 63 68 20 6f 66 20 61  nary search of a
43a90 20 74 61 62 6c 65 20 6f 66 20 4e 20 65 6e 74 72   table of N entr
43aa0 69 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20  ies should have 
43ab0 61 0a 2a 2a 20 63 6f 73 74 20 6f 66 20 61 70 70  a.** cost of app
43ac0 72 6f 78 69 6d 61 74 65 6c 79 20 6c 6f 67 28 4e  roximately log(N
43ad0 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  )..**.** This in
43ae0 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72  terface is exper
43af0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73  imental and is s
43b00 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
43b10 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69   or.** removal i
43b20 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
43b30 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  s of SQLite..*/.
43b40 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
43b50 6e 64 65 78 5f 69 6e 66 6f 20 7b 0a 20 20 2f 2a  ndex_info {.  /*
43b60 20 49 6e 70 75 74 73 20 2a 2f 0a 20 20 69 6e 74   Inputs */.  int
43b70 20 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20   nConstraint;   
43b80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
43b90 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20  r of entries in 
43ba0 61 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20  aConstraint */. 
43bb0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
43bc0 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74  index_constraint
43bd0 20 7b 0a 20 20 20 20 20 69 6e 74 20 69 43 6f 6c   {.     int iCol
43be0 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  umn;            
43bf0 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6f 6e 20 6c    /* Column on l
43c00 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66  eft-hand side of
43c10 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20   constraint */. 
43c20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61      unsigned cha
43c30 72 20 6f 70 3b 20 20 20 20 20 20 20 20 20 2f 2a  r op;         /*
43c40 20 43 6f 6e 73 74 72 61 69 6e 74 20 6f 70 65 72   Constraint oper
43c50 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 75 6e 73  ator */.     uns
43c60 69 67 6e 65 64 20 63 68 61 72 20 75 73 61 62 6c  igned char usabl
43c70 65 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  e;     /* True i
43c80 66 20 74 68 69 73 20 63 6f 6e 73 74 72 61 69 6e  f this constrain
43c90 74 20 69 73 20 75 73 61 62 6c 65 20 2a 2f 0a 20  t is usable */. 
43ca0 20 20 20 20 69 6e 74 20 69 54 65 72 6d 4f 66 66      int iTermOff
43cb0 73 65 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  set;          /*
43cc0 20 55 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c 79   Used internally
43cd0 20 2d 20 78 42 65 73 74 49 6e 64 65 78 20 73 68   - xBestIndex sh
43ce0 6f 75 6c 64 20 69 67 6e 6f 72 65 20 2a 2f 0a 20  ould ignore */. 
43cf0 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b   } *aConstraint;
43d00 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
43d10 61 62 6c 65 20 6f 66 20 57 48 45 52 45 20 63 6c  able of WHERE cl
43d20 61 75 73 65 20 63 6f 6e 73 74 72 61 69 6e 74 73  ause constraints
43d30 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72   */.  int nOrder
43d40 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  By;             
43d50 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
43d60 72 6d 73 20 69 6e 20 74 68 65 20 4f 52 44 45 52  rms in the ORDER
43d70 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
43d80 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69  struct sqlite3_i
43d90 6e 64 65 78 5f 6f 72 64 65 72 62 79 20 7b 0a 20  ndex_orderby {. 
43da0 20 20 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b      int iColumn;
43db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
43dc0 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a   Column number *
43dd0 2f 0a 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20  /.     unsigned 
43de0 63 68 61 72 20 64 65 73 63 3b 20 20 20 20 20 20  char desc;      
43df0 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 44 45 53   /* True for DES
43e00 43 2e 20 20 46 61 6c 73 65 20 66 6f 72 20 41 53  C.  False for AS
43e10 43 2e 20 2a 2f 0a 20 20 7d 20 2a 61 4f 72 64 65  C. */.  } *aOrde
43e20 72 42 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  rBy;            
43e30 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
43e40 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 2f  BY clause */.  /
43e50 2a 20 4f 75 74 70 75 74 73 20 2a 2f 0a 20 20 73  * Outputs */.  s
43e60 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
43e70 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 5f 75  dex_constraint_u
43e80 73 61 67 65 20 7b 0a 20 20 20 20 69 6e 74 20 61  sage {.    int a
43e90 72 67 76 49 6e 64 65 78 3b 20 20 20 20 20 20 20  rgvIndex;       
43ea0 20 20 20 20 2f 2a 20 69 66 20 3e 30 2c 20 63 6f      /* if >0, co
43eb0 6e 73 74 72 61 69 6e 74 20 69 73 20 70 61 72 74  nstraint is part
43ec0 20 6f 66 20 61 72 67 76 20 74 6f 20 78 46 69 6c   of argv to xFil
43ed0 74 65 72 20 2a 2f 0a 20 20 20 20 75 6e 73 69 67  ter */.    unsig
43ee0 6e 65 64 20 63 68 61 72 20 6f 6d 69 74 3b 20 20  ned char omit;  
43ef0 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f      /* Do not co
43f00 64 65 20 61 20 74 65 73 74 20 66 6f 72 20 74 68  de a test for th
43f10 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f  is constraint */
43f20 0a 20 20 7d 20 2a 61 43 6f 6e 73 74 72 61 69 6e  .  } *aConstrain
43f30 74 55 73 61 67 65 3b 0a 20 20 69 6e 74 20 69 64  tUsage;.  int id
43f40 78 4e 75 6d 3b 20 20 20 20 20 20 20 20 20 20 20  xNum;           
43f50 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 75       /* Number u
43f60 73 65 64 20 74 6f 20 69 64 65 6e 74 69 66 79 20  sed to identify 
43f70 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20 63  the index */.  c
43f80 68 61 72 20 2a 69 64 78 53 74 72 3b 20 20 20 20  har *idxStr;    
43f90 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72            /* Str
43fa0 69 6e 67 2c 20 70 6f 73 73 69 62 6c 79 20 6f 62  ing, possibly ob
43fb0 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
43fc0 74 65 33 5f 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20  te3_malloc */.  
43fd0 69 6e 74 20 6e 65 65 64 54 6f 46 72 65 65 49 64  int needToFreeId
43fe0 78 53 74 72 3b 20 20 20 20 20 20 2f 2a 20 46 72  xStr;      /* Fr
43ff0 65 65 20 69 64 78 53 74 72 20 75 73 69 6e 67 20  ee idxStr using 
44000 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 20 69  sqlite3_free() i
44010 66 20 74 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20  f true */.  int 
44020 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 3b  orderByConsumed;
44030 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
44040 66 20 6f 75 74 70 75 74 20 69 73 20 61 6c 72 65  f output is alre
44050 61 64 79 20 6f 72 64 65 72 65 64 20 2a 2f 0a 20  ady ordered */. 
44060 20 64 6f 75 62 6c 65 20 65 73 74 69 6d 61 74 65   double estimate
44070 64 43 6f 73 74 3b 20 20 20 20 20 20 2f 2a 20 45  dCost;      /* E
44080 73 74 69 6d 61 74 65 64 20 63 6f 73 74 20 6f 66  stimated cost of
44090 20 75 73 69 6e 67 20 74 68 69 73 20 69 6e 64 65   using this inde
440a0 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  x */.};.#define 
440b0 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
440c0 53 54 52 41 49 4e 54 5f 45 51 20 20 20 20 32 0a  STRAINT_EQ    2.
440d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
440e0 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
440f0 47 54 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20  GT    4.#define 
44100 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
44110 53 54 52 41 49 4e 54 5f 4c 45 20 20 20 20 38 0a  STRAINT_LE    8.
44120 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49  #define SQLITE_I
44130 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
44140 4c 54 20 20 20 20 31 36 0a 23 64 65 66 69 6e 65  LT    16.#define
44150 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
44160 4e 53 54 52 41 49 4e 54 5f 47 45 20 20 20 20 33  NSTRAINT_GE    3
44170 32 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  2.#define SQLITE
44180 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
44190 54 5f 4d 41 54 43 48 20 36 34 0a 0a 2f 2a 0a 2a  T_MATCH 64../*.*
441a0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 67 69  * CAPI3REF: Regi
441b0 73 74 65 72 20 41 20 56 69 72 74 75 61 6c 20 54  ster A Virtual T
441c0 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e 74 61 74  able Implementat
441d0 69 6f 6e 20 7b 48 31 38 32 30 30 7d 20 3c 53 32  ion {H18200} <S2
441e0 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d  0400>.** EXPERIM
441f0 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ENTAL.**.** This
44200 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
44210 20 74 6f 20 72 65 67 69 73 74 65 72 20 61 20 6e   to register a n
44220 65 77 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 77  ew module name w
44230 69 74 68 20 61 0a 2a 2a 20 5b 64 61 74 61 62 61  ith a.** [databa
44240 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 2e 20  se connection]. 
44250 20 4d 6f 64 75 6c 65 20 6e 61 6d 65 73 20 6d 75   Module names mu
44260 73 74 20 62 65 20 72 65 67 69 73 74 65 72 65 64  st be registered
44270 20 62 65 66 6f 72 65 0a 2a 2a 20 63 72 65 61 74   before.** creat
44280 69 6e 67 20 6e 65 77 20 76 69 72 74 75 61 6c 20  ing new virtual 
44290 74 61 62 6c 65 73 20 6f 6e 20 74 68 65 20 6d 6f  tables on the mo
442a0 64 75 6c 65 2c 20 6f 72 20 62 65 66 6f 72 65 20  dule, or before 
442b0 75 73 69 6e 67 0a 2a 2a 20 70 72 65 65 78 69 73  using.** preexis
442c0 74 69 6e 67 20 76 69 72 74 75 61 6c 20 74 61 62  ting virtual tab
442d0 6c 65 73 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c  les of the modul
442e0 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  e..**.** This in
442f0 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72  terface is exper
44300 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73  imental and is s
44310 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
44320 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69   or.** removal i
44330 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
44340 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  s of SQLite..*/.
44350 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
44360 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
44370 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  nt sqlite3_creat
44380 65 5f 6d 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69  e_module(.  sqli
44390 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
443a0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74 65         /* SQLite
443b0 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 72   connection to r
443c0 65 67 69 73 74 65 72 20 6d 6f 64 75 6c 65 20 77  egister module w
443d0 69 74 68 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ith */.  const c
443e0 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20  har *zName,     
443f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
44400 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63  he module */.  c
44410 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
44420 75 6c 65 20 2a 2c 20 20 20 20 2f 2a 20 4d 65 74  ule *,    /* Met
44430 68 6f 64 73 20 66 6f 72 20 74 68 65 20 6d 6f 64  hods for the mod
44440 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 20  ule */.  void * 
44450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44460 20 20 20 20 2f 2a 20 43 6c 69 65 6e 74 20 64 61      /* Client da
44470 74 61 20 66 6f 72 20 78 43 72 65 61 74 65 2f 78  ta for xCreate/x
44480 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 3b 0a 0a 2f  Connect */.);../
44490 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 52  *.** CAPI3REF: R
444a0 65 67 69 73 74 65 72 20 41 20 56 69 72 74 75 61  egister A Virtua
444b0 6c 20 54 61 62 6c 65 20 49 6d 70 6c 65 6d 65 6e  l Table Implemen
444c0 74 61 74 69 6f 6e 20 7b 48 31 38 32 31 30 7d 20  tation {H18210} 
444d0 3c 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45  <S20400>.** EXPE
444e0 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
444f0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69  his routine is i
44500 64 65 6e 74 69 63 61 6c 20 74 6f 20 74 68 65 20  dentical to the 
44510 5b 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f  [sqlite3_create_
44520 6d 6f 64 75 6c 65 28 29 5d 20 6d 65 74 68 6f 64  module()] method
44530 20 61 62 6f 76 65 2c 0a 2a 2a 20 65 78 63 65 70   above,.** excep
44540 74 20 74 68 61 74 20 69 74 20 61 6c 6c 6f 77 73  t that it allows
44550 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 75   a destructor fu
44560 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 73 70 65  nction to be spe
44570 63 69 66 69 65 64 2e 20 49 74 20 69 73 0a 2a 2a  cified. It is.**
44580 20 65 76 65 6e 20 6d 6f 72 65 20 65 78 70 65 72   even more exper
44590 69 6d 65 6e 74 61 6c 20 74 68 61 6e 20 74 68 65  imental than the
445a0 20 72 65 73 74 20 6f 66 20 74 68 65 20 76 69 72   rest of the vir
445b0 74 75 61 6c 20 74 61 62 6c 65 73 20 41 50 49 2e  tual tables API.
445c0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
445d0 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
445e0 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  AL int sqlite3_c
445f0 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
44600 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
44610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
44620 20 53 51 4c 69 74 65 20 63 6f 6e 6e 65 63 74 69   SQLite connecti
44630 6f 6e 20 74 6f 20 72 65 67 69 73 74 65 72 20 6d  on to register m
44640 6f 64 75 6c 65 20 77 69 74 68 20 2a 2f 0a 20 20  odule with */.  
44650 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
44660 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61  e,         /* Na
44670 6d 65 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65  me of the module
44680 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69   */.  const sqli
44690 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 2c 20 20 20  te3_module *,   
446a0 20 2f 2a 20 4d 65 74 68 6f 64 73 20 66 6f 72 20   /* Methods for 
446b0 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  the module */.  
446c0 76 6f 69 64 20 2a 2c 20 20 20 20 20 20 20 20 20  void *,         
446d0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6c             /* Cl
446e0 69 65 6e 74 20 64 61 74 61 20 66 6f 72 20 78 43  ient data for xC
446f0 72 65 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a  reate/xConnect *
44700 2f 0a 20 20 76 6f 69 64 28 2a 78 44 65 73 74 72  /.  void(*xDestr
44710 6f 79 29 28 76 6f 69 64 2a 29 20 20 20 20 20 2f  oy)(void*)     /
44720 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74 72 75 63  * Module destruc
44730 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  tor function */.
44740 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  );../*.** CAPI3R
44750 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c  EF: Virtual Tabl
44760 65 20 49 6e 73 74 61 6e 63 65 20 4f 62 6a 65 63  e Instance Objec
44770 74 20 7b 48 31 38 30 31 30 7d 20 3c 53 32 30 34  t {H18010} <S204
44780 30 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a  00>.** KEYWORDS:
44790 20 73 71 6c 69 74 65 33 5f 76 74 61 62 0a 2a 2a   sqlite3_vtab.**
447a0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
447b0 0a 2a 2a 20 45 76 65 72 79 20 6d 6f 64 75 6c 65  .** Every module
447c0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
447d0 75 73 65 73 20 61 20 73 75 62 63 6c 61 73 73 20  uses a subclass 
447e0 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
447f0 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 74 6f   structure.** to
44800 20 64 65 73 63 72 69 62 65 20 61 20 70 61 72 74   describe a part
44810 69 63 75 6c 61 72 20 69 6e 73 74 61 6e 63 65 20  icular instance 
44820 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 2e 20 20  of the module.  
44830 45 61 63 68 20 73 75 62 63 6c 61 73 73 20 77 69  Each subclass wi
44840 6c 6c 0a 2a 2a 20 62 65 20 74 61 69 6c 6f 72 65  ll.** be tailore
44850 64 20 74 6f 20 74 68 65 20 73 70 65 63 69 66 69  d to the specifi
44860 63 20 6e 65 65 64 73 20 6f 66 20 74 68 65 20 6d  c needs of the m
44870 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
44880 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 70 75 72  tion..** The pur
44890 70 6f 73 65 20 6f 66 20 74 68 69 73 20 73 75 70  pose of this sup
448a0 65 72 63 6c 61 73 73 20 69 73 20 74 6f 20 64 65  erclass is to de
448b0 66 69 6e 65 20 63 65 72 74 61 69 6e 20 66 69 65  fine certain fie
448c0 6c 64 73 20 74 68 61 74 20 61 72 65 0a 2a 2a 20  lds that are.** 
448d0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 6d 6f  common to all mo
448e0 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  dule implementat
448f0 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 56 69 72 74  ions..**.** Virt
44900 75 61 6c 20 74 61 62 6c 65 73 20 6d 65 74 68 6f  ual tables metho
44910 64 73 20 63 61 6e 20 73 65 74 20 61 6e 20 65 72  ds can set an er
44920 72 6f 72 20 6d 65 73 73 61 67 65 20 62 79 20 61  ror message by a
44930 73 73 69 67 6e 69 6e 67 20 61 0a 2a 2a 20 73 74  ssigning a.** st
44940 72 69 6e 67 20 6f 62 74 61 69 6e 65 64 20 66 72  ring obtained fr
44950 6f 6d 20 5b 73 71 6c 69 74 65 33 5f 6d 70 72 69  om [sqlite3_mpri
44960 6e 74 66 28 29 5d 20 74 6f 20 7a 45 72 72 4d 73  ntf()] to zErrMs
44970 67 2e 20 20 54 68 65 20 6d 65 74 68 6f 64 20 73  g.  The method s
44980 68 6f 75 6c 64 0a 2a 2a 20 74 61 6b 65 20 63 61  hould.** take ca
44990 72 65 20 74 68 61 74 20 61 6e 79 20 70 72 69 6f  re that any prio
449a0 72 20 73 74 72 69 6e 67 20 69 73 20 66 72 65 65  r string is free
449b0 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20 5b  d by a call to [
449c0 73 71 6c 69 74 65 33 5f 66 72 65 65 28 29 5d 0a  sqlite3_free()].
449d0 2a 2a 20 70 72 69 6f 72 20 74 6f 20 61 73 73 69  ** prior to assi
449e0 67 6e 69 6e 67 20 61 20 6e 65 77 20 73 74 72 69  gning a new stri
449f0 6e 67 20 74 6f 20 7a 45 72 72 4d 73 67 2e 20 20  ng to zErrMsg.  
44a00 41 66 74 65 72 20 74 68 65 20 65 72 72 6f 72 20  After the error 
44a10 6d 65 73 73 61 67 65 0a 2a 2a 20 69 73 20 64 65  message.** is de
44a20 6c 69 76 65 72 65 64 20 75 70 20 74 6f 20 74 68  livered up to th
44a30 65 20 63 6c 69 65 6e 74 20 61 70 70 6c 69 63 61  e client applica
44a40 74 69 6f 6e 2c 20 74 68 65 20 73 74 72 69 6e 67  tion, the string
44a50 20 77 69 6c 6c 20 62 65 20 61 75 74 6f 6d 61 74   will be automat
44a60 69 63 61 6c 6c 79 0a 2a 2a 20 66 72 65 65 64 20  ically.** freed 
44a70 62 79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  by sqlite3_free(
44a80 29 20 61 6e 64 20 74 68 65 20 7a 45 72 72 4d 73  ) and the zErrMs
44a90 67 20 66 69 65 6c 64 20 77 69 6c 6c 20 62 65 20  g field will be 
44aa0 7a 65 72 6f 65 64 2e 20 20 4e 6f 74 65 0a 2a 2a  zeroed.  Note.**
44ab0 20 74 68 61 74 20 73 71 6c 69 74 65 33 5f 6d 70   that sqlite3_mp
44ac0 72 69 6e 74 66 28 29 20 61 6e 64 20 73 71 6c 69  rintf() and sqli
44ad0 74 65 33 5f 66 72 65 65 28 29 20 61 72 65 20 75  te3_free() are u
44ae0 73 65 64 20 6f 6e 20 74 68 65 20 7a 45 72 72 4d  sed on the zErrM
44af0 73 67 20 66 69 65 6c 64 0a 2a 2a 20 73 69 6e 63  sg field.** sinc
44b00 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
44b10 20 61 72 65 20 63 6f 6d 6d 6f 6e 6c 79 20 69 6d   are commonly im
44b20 70 6c 65 6d 65 6e 74 65 64 20 69 6e 20 6c 6f 61  plemented in loa
44b30 64 61 62 6c 65 20 65 78 74 65 6e 73 69 6f 6e 73  dable extensions
44b40 20 77 68 69 63 68 0a 2a 2a 20 64 6f 20 6e 6f 74   which.** do not
44b50 20 68 61 76 65 20 61 63 63 65 73 73 20 74 6f 20   have access to 
44b60 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 29  sqlite3MPrintf()
44b70 20 6f 72 20 73 71 6c 69 74 65 33 46 72 65 65 28   or sqlite3Free(
44b80 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  )..**.** This in
44b90 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72  terface is exper
44ba0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73  imental and is s
44bb0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
44bc0 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69   or.** removal i
44bd0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
44be0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  s of SQLite..*/.
44bf0 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 76  struct sqlite3_v
44c00 74 61 62 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71  tab {.  const sq
44c10 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d  lite3_module *pM
44c20 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68 65 20 6d  odule;  /* The m
44c30 6f 64 75 6c 65 20 66 6f 72 20 74 68 69 73 20 76  odule for this v
44c40 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a  irtual table */.
44c50 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
44c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
44c70 20 20 2f 2a 20 55 73 65 64 20 69 6e 74 65 72 6e    /* Used intern
44c80 61 6c 6c 79 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ally */.  char *
44c90 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20  zErrMsg;        
44ca0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 72 72            /* Err
44cb0 6f 72 20 6d 65 73 73 61 67 65 20 66 72 6f 6d 20  or message from 
44cc0 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
44cd0 29 20 2a 2f 0a 20 20 2f 2a 20 56 69 72 74 75 61  ) */.  /* Virtua
44ce0 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
44cf0 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70  tations will typ
44d00 69 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74  ically add addit
44d10 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a  ional fields */.
44d20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  };../*.** CAPI3R
44d30 45 46 3a 20 56 69 72 74 75 61 6c 20 54 61 62 6c  EF: Virtual Tabl
44d40 65 20 43 75 72 73 6f 72 20 4f 62 6a 65 63 74 20  e Cursor Object 
44d50 20 7b 48 31 38 30 32 30 7d 20 3c 53 32 30 34 30   {H18020} <S2040
44d60 30 3e 0a 2a 2a 20 4b 45 59 57 4f 52 44 53 3a 20  0>.** KEYWORDS: 
44d70 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
44d80 73 6f 72 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e  sor.** EXPERIMEN
44d90 54 41 4c 0a 2a 2a 0a 2a 2a 20 45 76 65 72 79 20  TAL.**.** Every 
44da0 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  module implement
44db0 61 74 69 6f 6e 20 75 73 65 73 20 61 20 73 75 62  ation uses a sub
44dc0 63 6c 61 73 73 20 6f 66 20 74 68 65 20 66 6f 6c  class of the fol
44dd0 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
44de0 0a 2a 2a 20 74 6f 20 64 65 73 63 72 69 62 65 20  .** to describe 
44df0 63 75 72 73 6f 72 73 20 74 68 61 74 20 70 6f 69  cursors that poi
44e00 6e 74 20 69 6e 74 6f 20 74 68 65 20 76 69 72 74  nt into the virt
44e10 75 61 6c 20 74 61 62 6c 65 20 61 6e 64 20 61 72  ual table and ar
44e20 65 20 75 73 65 64 0a 2a 2a 20 74 6f 20 6c 6f 6f  e used.** to loo
44e30 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 76 69  p through the vi
44e40 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 43 75  rtual table.  Cu
44e50 72 73 6f 72 73 20 61 72 65 20 63 72 65 61 74 65  rsors are create
44e60 64 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 78  d using the.** x
44e70 4f 70 65 6e 20 6d 65 74 68 6f 64 20 6f 66 20 74  Open method of t
44e80 68 65 20 6d 6f 64 75 6c 65 2e 20 20 45 61 63 68  he module.  Each
44e90 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e   module implemen
44ea0 74 61 74 69 6f 6e 20 77 69 6c 6c 20 64 65 66 69  tation will defi
44eb0 6e 65 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e  ne.** the conten
44ec0 74 20 6f 66 20 61 20 63 75 72 73 6f 72 20 73 74  t of a cursor st
44ed0 72 75 63 74 75 72 65 20 74 6f 20 73 75 69 74 20  ructure to suit 
44ee0 69 74 73 20 6f 77 6e 20 6e 65 65 64 73 2e 0a 2a  its own needs..*
44ef0 2a 0a 2a 2a 20 54 68 69 73 20 73 75 70 65 72 63  *.** This superc
44f00 6c 61 73 73 20 65 78 69 73 74 73 20 69 6e 20 6f  lass exists in o
44f10 72 64 65 72 20 74 6f 20 64 65 66 69 6e 65 20 66  rder to define f
44f20 69 65 6c 64 73 20 6f 66 20 74 68 65 20 63 75 72  ields of the cur
44f30 73 6f 72 20 74 68 61 74 0a 2a 2a 20 61 72 65 20  sor that.** are 
44f40 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 20 69 6d  common to all im
44f50 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 2a  plementations..*
44f60 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65 72 66  *.** This interf
44f70 61 63 65 20 69 73 20 65 78 70 65 72 69 6d 65 6e  ace is experimen
44f80 74 61 6c 20 61 6e 64 20 69 73 20 73 75 62 6a 65  tal and is subje
44f90 63 74 20 74 6f 20 63 68 61 6e 67 65 20 6f 72 0a  ct to change or.
44fa0 2a 2a 20 72 65 6d 6f 76 61 6c 20 69 6e 20 66 75  ** removal in fu
44fb0 74 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66  ture releases of
44fc0 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a 73 74 72 75   SQLite..*/.stru
44fd0 63 74 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  ct sqlite3_vtab_
44fe0 63 75 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74  cursor {.  sqlit
44ff0 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 20  e3_vtab *pVtab; 
45000 20 20 20 20 20 2f 2a 20 56 69 72 74 75 61 6c 20       /* Virtual 
45010 74 61 62 6c 65 20 6f 66 20 74 68 69 73 20 63 75  table of this cu
45020 72 73 6f 72 20 2a 2f 0a 20 20 2f 2a 20 56 69 72  rsor */.  /* Vir
45030 74 75 61 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65  tual table imple
45040 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c 20  mentations will 
45050 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61 64  typically add ad
45060 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73 20  ditional fields 
45070 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50  */.};../*.** CAP
45080 49 33 52 45 46 3a 20 44 65 63 6c 61 72 65 20 54  I3REF: Declare T
45090 68 65 20 53 63 68 65 6d 61 20 4f 66 20 41 20 56  he Schema Of A V
450a0 69 72 74 75 61 6c 20 54 61 62 6c 65 20 7b 48 31  irtual Table {H1
450b0 38 32 38 30 7d 20 3c 53 32 30 34 30 30 3e 0a 2a  8280} <S20400>.*
450c0 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a  * EXPERIMENTAL.*
450d0 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61 74 65  *.** The xCreate
450e0 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 20 6d 65   and xConnect me
450f0 74 68 6f 64 73 20 6f 66 20 61 20 6d 6f 64 75 6c  thods of a modul
45100 65 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77  e use the follow
45110 69 6e 67 20 41 50 49 0a 2a 2a 20 74 6f 20 64 65  ing API.** to de
45120 63 6c 61 72 65 20 74 68 65 20 66 6f 72 6d 61 74  clare the format
45130 20 28 74 68 65 20 6e 61 6d 65 73 20 61 6e 64 20   (the names and 
45140 64 61 74 61 74 79 70 65 73 20 6f 66 20 74 68 65  datatypes of the
45150 20 63 6f 6c 75 6d 6e 73 29 20 6f 66 0a 2a 2a 20   columns) of.** 
45160 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
45170 65 73 20 74 68 65 79 20 69 6d 70 6c 65 6d 65 6e  es they implemen
45180 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e  t..**.** This in
45190 74 65 72 66 61 63 65 20 69 73 20 65 78 70 65 72  terface is exper
451a0 69 6d 65 6e 74 61 6c 20 61 6e 64 20 69 73 20 73  imental and is s
451b0 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65  ubject to change
451c0 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c 20 69   or.** removal i
451d0 6e 20 66 75 74 75 72 65 20 72 65 6c 65 61 73 65  n future release
451e0 73 20 6f 66 20 53 51 4c 69 74 65 2e 0a 2a 2f 0a  s of SQLite..*/.
451f0 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c 49 54  SQLITE_API SQLIT
45200 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 69  E_EXPERIMENTAL i
45210 6e 74 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61  nt sqlite3_decla
45220 72 65 5f 76 74 61 62 28 73 71 6c 69 74 65 33 2a  re_vtab(sqlite3*
45230 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43  , const char *zC
45240 72 65 61 74 65 54 61 62 6c 65 29 3b 0a 0a 2f 2a  reateTable);../*
45250 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 4f 76  .** CAPI3REF: Ov
45260 65 72 6c 6f 61 64 20 41 20 46 75 6e 63 74 69 6f  erload A Functio
45270 6e 20 46 6f 72 20 41 20 56 69 72 74 75 61 6c 20  n For A Virtual 
45280 54 61 62 6c 65 20 7b 48 31 38 33 30 30 7d 20 3c  Table {H18300} <
45290 53 32 30 34 30 30 3e 0a 2a 2a 20 45 58 50 45 52  S20400>.** EXPER
452a0 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 56 69  IMENTAL.**.** Vi
452b0 72 74 75 61 6c 20 74 61 62 6c 65 73 20 63 61 6e  rtual tables can
452c0 20 70 72 6f 76 69 64 65 20 61 6c 74 65 72 6e 61   provide alterna
452d0 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74  tive implementat
452e0 69 6f 6e 73 20 6f 66 20 66 75 6e 63 74 69 6f 6e  ions of function
452f0 73 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 78  s.** using the x
45300 46 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74  FindFunction met
45310 68 6f 64 2e 20 20 42 75 74 20 67 6c 6f 62 61 6c  hod.  But global
45320 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 6f   versions of tho
45330 73 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20  se functions.** 
45340 6d 75 73 74 20 65 78 69 73 74 20 69 6e 20 6f 72  must exist in or
45350 64 65 72 20 74 6f 20 62 65 20 6f 76 65 72 6c 6f  der to be overlo
45360 61 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  aded..**.** This
45370 20 41 50 49 20 6d 61 6b 65 73 20 73 75 72 65 20   API makes sure 
45380 61 20 67 6c 6f 62 61 6c 20 76 65 72 73 69 6f 6e  a global version
45390 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 77   of a function w
453a0 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61 72  ith a particular
453b0 0a 2a 2a 20 6e 61 6d 65 20 61 6e 64 20 6e 75 6d  .** name and num
453c0 62 65 72 20 6f 66 20 70 61 72 61 6d 65 74 65 72  ber of parameter
453d0 73 20 65 78 69 73 74 73 2e 20 20 49 66 20 6e 6f  s exists.  If no
453e0 20 73 75 63 68 20 66 75 6e 63 74 69 6f 6e 20 65   such function e
453f0 78 69 73 74 73 0a 2a 2a 20 62 65 66 6f 72 65 20  xists.** before 
45400 74 68 69 73 20 41 50 49 20 69 73 20 63 61 6c 6c  this API is call
45410 65 64 2c 20 61 20 6e 65 77 20 66 75 6e 63 74 69  ed, a new functi
45420 6f 6e 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  on is created.  
45430 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  The implementati
45440 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 6e 65 77  on.** of the new
45450 20 66 75 6e 63 74 69 6f 6e 20 61 6c 77 61 79 73   function always
45460 20 63 61 75 73 65 73 20 61 6e 20 65 78 63 65 70   causes an excep
45470 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 72 6f 77  tion to be throw
45480 6e 2e 20 20 53 6f 0a 2a 2a 20 74 68 65 20 6e 65  n.  So.** the ne
45490 77 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f  w function is no
454a0 74 20 67 6f 6f 64 20 66 6f 72 20 61 6e 79 74 68  t good for anyth
454b0 69 6e 67 20 62 79 20 69 74 73 65 6c 66 2e 20 20  ing by itself.  
454c0 49 74 73 20 6f 6e 6c 79 0a 2a 2a 20 70 75 72 70  Its only.** purp
454d0 6f 73 65 20 69 73 20 74 6f 20 62 65 20 61 20 70  ose is to be a p
454e0 6c 61 63 65 68 6f 6c 64 65 72 20 66 75 6e 63 74  laceholder funct
454f0 69 6f 6e 20 74 68 61 74 20 63 61 6e 20 62 65 20  ion that can be 
45500 6f 76 65 72 6c 6f 61 64 65 64 0a 2a 2a 20 62 79  overloaded.** by
45510 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
45520 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49 20  .**.** This API 
45530 73 68 6f 75 6c 64 20 62 65 20 63 6f 6e 73 69 64  should be consid
45540 65 72 65 64 20 70 61 72 74 20 6f 66 20 74 68 65  ered part of the
45550 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 69   virtual table i
45560 6e 74 65 72 66 61 63 65 2c 0a 2a 2a 20 77 68 69  nterface,.** whi
45570 63 68 20 69 73 20 65 78 70 65 72 69 6d 65 6e 74  ch is experiment
45580 61 6c 20 61 6e 64 20 73 75 62 6a 65 63 74 20 74  al and subject t
45590 6f 20 63 68 61 6e 67 65 2e 0a 2a 2f 0a 53 51 4c  o change..*/.SQL
455a0 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
455b0 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
455c0 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
455d0 5f 66 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  _function(sqlite
455e0 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  3*, const char *
455f0 7a 46 75 6e 63 4e 61 6d 65 2c 20 69 6e 74 20 6e  zFuncName, int n
45600 41 72 67 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  Arg);../*.** The
45610 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   interface to th
45620 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
45630 6d 65 63 68 61 6e 69 73 6d 20 64 65 66 69 6e 65  mechanism define
45640 64 20 61 62 6f 76 65 20 28 62 61 63 6b 20 75 70  d above (back up
45650 0a 2a 2a 20 74 6f 20 61 20 63 6f 6d 6d 65 6e 74  .** to a comment
45660 20 72 65 6d 61 72 6b 61 62 6c 79 20 73 69 6d 69   remarkably simi
45670 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65 29  lar to this one)
45680 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 63 6f   is currently co
45690 6e 73 69 64 65 72 65 64 0a 2a 2a 20 74 6f 20 62  nsidered.** to b
456a0 65 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 2e 20  e experimental. 
456b0 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20 6d   The interface m
456c0 69 67 68 74 20 63 68 61 6e 67 65 20 69 6e 20 69  ight change in i
456d0 6e 63 6f 6d 70 61 74 69 62 6c 65 20 77 61 79 73  ncompatible ways
456e0 2e 0a 2a 2a 20 49 66 20 74 68 69 73 20 69 73 20  ..** If this is 
456f0 61 20 70 72 6f 62 6c 65 6d 20 66 6f 72 20 79 6f  a problem for yo
45700 75 2c 20 64 6f 20 6e 6f 74 20 75 73 65 20 74 68  u, do not use th
45710 65 20 69 6e 74 65 72 66 61 63 65 20 61 74 20 74  e interface at t
45720 68 69 73 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20  his time..**.** 
45730 57 68 65 6e 20 74 68 65 20 76 69 72 74 75 61 6c  When the virtual
45740 2d 74 61 62 6c 65 20 6d 65 63 68 61 6e 69 73 6d  -table mechanism
45750 20 73 74 61 62 69 6c 69 7a 65 73 2c 20 77 65 20   stabilizes, we 
45760 77 69 6c 6c 20 64 65 63 6c 61 72 65 20 74 68 65  will declare the
45770 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 20 66 69  .** interface fi
45780 78 65 64 2c 20 73 75 70 70 6f 72 74 20 69 74 20  xed, support it 
45790 69 6e 64 65 66 69 6e 69 74 65 6c 79 2c 20 61 6e  indefinitely, an
457a0 64 20 72 65 6d 6f 76 65 20 74 68 69 73 20 63 6f  d remove this co
457b0 6d 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  mment..**.******
457c0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20   EXPERIMENTAL - 
457d0 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67  subject to chang
457e0 65 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65  e without notice
457f0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a   **************.
45800 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52  */../*.** CAPI3R
45810 45 46 3a 20 41 20 48 61 6e 64 6c 65 20 54 6f 20  EF: A Handle To 
45820 41 6e 20 4f 70 65 6e 20 42 4c 4f 42 20 7b 48 31  An Open BLOB {H1
45830 37 38 30 30 7d 20 3c 53 33 30 32 33 30 3e 0a 2a  7800} <S30230>.*
45840 2a 20 4b 45 59 57 4f 52 44 53 3a 20 7b 42 4c 4f  * KEYWORDS: {BLO
45850 42 20 68 61 6e 64 6c 65 7d 20 7b 42 4c 4f 42 20  B handle} {BLOB 
45860 68 61 6e 64 6c 65 73 7d 0a 2a 2a 0a 2a 2a 20 41  handles}.**.** A
45870 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
45880 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  is object repres
45890 65 6e 74 73 20 61 6e 20 6f 70 65 6e 20 42 4c 4f  ents an open BLO
458a0 42 20 6f 6e 20 77 68 69 63 68 0a 2a 2a 20 5b 73  B on which.** [s
458b0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
458c0 20 7c 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 42   | incremental B
458d0 4c 4f 42 20 49 2f 4f 5d 20 63 61 6e 20 62 65 20  LOB I/O] can be 
458e0 70 65 72 66 6f 72 6d 65 64 2e 0a 2a 2a 20 4f 62  performed..** Ob
458f0 6a 65 63 74 73 20 6f 66 20 74 68 69 73 20 74 79  jects of this ty
45900 70 65 20 61 72 65 20 63 72 65 61 74 65 64 20 62  pe are created b
45910 79 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  y [sqlite3_blob_
45920 6f 70 65 6e 28 29 5d 0a 2a 2a 20 61 6e 64 20 64  open()].** and d
45930 65 73 74 72 6f 79 65 64 20 62 79 20 5b 73 71 6c  estroyed by [sql
45940 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28  ite3_blob_close(
45950 29 5d 2e 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69  )]..** The [sqli
45960 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d  te3_blob_read()]
45970 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c   and [sqlite3_bl
45980 6f 62 5f 77 72 69 74 65 28 29 5d 20 69 6e 74 65  ob_write()] inte
45990 72 66 61 63 65 73 0a 2a 2a 20 63 61 6e 20 62 65  rfaces.** can be
459a0 20 75 73 65 64 20 74 6f 20 72 65 61 64 20 6f 72   used to read or
459b0 20 77 72 69 74 65 20 73 6d 61 6c 6c 20 73 75 62   write small sub
459c0 73 65 63 74 69 6f 6e 73 20 6f 66 20 74 68 65 20  sections of the 
459d0 42 4c 4f 42 2e 0a 2a 2a 20 54 68 65 20 5b 73 71  BLOB..** The [sq
459e0 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73  lite3_blob_bytes
459f0 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20 72 65  ()] interface re
45a00 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 6f  turns the size o
45a10 66 20 74 68 65 20 42 4c 4f 42 20 69 6e 20 62 79  f the BLOB in by
45a20 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  tes..*/.typedef 
45a30 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 62  struct sqlite3_b
45a40 6c 6f 62 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  lob sqlite3_blob
45a50 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
45a60 46 3a 20 4f 70 65 6e 20 41 20 42 4c 4f 42 20 46  F: Open A BLOB F
45a70 6f 72 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20 49  or Incremental I
45a80 2f 4f 20 7b 48 31 37 38 31 30 7d 20 3c 53 33 30  /O {H17810} <S30
45a90 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  230>.**.** This 
45aa0 69 6e 74 65 72 66 61 63 65 73 20 6f 70 65 6e 73  interfaces opens
45ab0 20 61 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 20   a [BLOB handle 
45ac0 7c 20 68 61 6e 64 6c 65 5d 20 74 6f 20 74 68 65  | handle] to the
45ad0 20 42 4c 4f 42 20 6c 6f 63 61 74 65 64 0a 2a 2a   BLOB located.**
45ae0 20 69 6e 20 72 6f 77 20 69 52 6f 77 2c 20 63 6f   in row iRow, co
45af0 6c 75 6d 6e 20 7a 43 6f 6c 75 6d 6e 2c 20 74 61  lumn zColumn, ta
45b00 62 6c 65 20 7a 54 61 62 6c 65 20 69 6e 20 64 61  ble zTable in da
45b10 74 61 62 61 73 65 20 7a 44 62 3b 0a 2a 2a 20 69  tabase zDb;.** i
45b20 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 74  n other words, t
45b30 68 65 20 73 61 6d 65 20 42 4c 4f 42 20 74 68 61  he same BLOB tha
45b40 74 20 77 6f 75 6c 64 20 62 65 20 73 65 6c 65 63  t would be selec
45b50 74 65 64 20 62 79 3a 0a 2a 2a 0a 2a 2a 20 3c 70  ted by:.**.** <p
45b60 72 65 3e 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  re>.**     SELEC
45b70 54 20 7a 43 6f 6c 75 6d 6e 20 46 52 4f 4d 20 7a  T zColumn FROM z
45b80 44 62 2e 7a 54 61 62 6c 65 20 57 48 45 52 45 20  Db.zTable WHERE 
45b90 72 6f 77 69 64 20 3d 20 69 52 6f 77 3b 0a 2a 2a  rowid = iRow;.**
45ba0 20 3c 2f 70 72 65 3e 20 7b 45 4e 44 7d 0a 2a 2a   </pre> {END}.**
45bb0 0a 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67 73  .** If the flags
45bc0 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f   parameter is no
45bd0 6e 2d 7a 65 72 6f 2c 20 74 68 65 20 74 68 65 20  n-zero, the the 
45be0 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66  BLOB is opened f
45bf0 6f 72 20 72 65 61 64 0a 2a 2a 20 61 6e 64 20 77  or read.** and w
45c00 72 69 74 65 20 61 63 63 65 73 73 2e 20 49 66 20  rite access. If 
45c10 69 74 20 69 73 20 7a 65 72 6f 2c 20 74 68 65 20  it is zero, the 
45c20 42 4c 4f 42 20 69 73 20 6f 70 65 6e 65 64 20 66  BLOB is opened f
45c30 6f 72 20 72 65 61 64 20 61 63 63 65 73 73 2e 0a  or read access..
45c40 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20  **.** Note that 
45c50 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
45c60 65 20 69 73 20 6e 6f 74 20 74 68 65 20 66 69 6c  e is not the fil
45c70 65 6e 61 6d 65 20 74 68 61 74 20 63 6f 6e 74 61  ename that conta
45c80 69 6e 73 0a 2a 2a 20 74 68 65 20 64 61 74 61 62  ins.** the datab
45c90 61 73 65 20 62 75 74 20 72 61 74 68 65 72 20 74  ase but rather t
45ca0 68 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65  he symbolic name
45cb0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
45cc0 20 74 68 61 74 0a 2a 2a 20 69 73 20 61 73 73 69   that.** is assi
45cd0 67 6e 65 64 20 77 68 65 6e 20 74 68 65 20 64 61  gned when the da
45ce0 74 61 62 61 73 65 20 69 73 20 63 6f 6e 6e 65 63  tabase is connec
45cf0 74 65 64 20 75 73 69 6e 67 20 5b 41 54 54 41 43  ted using [ATTAC
45d00 48 5d 2e 0a 2a 2a 20 46 6f 72 20 74 68 65 20 6d  H]..** For the m
45d10 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
45d20 65 2c 20 74 68 65 20 64 61 74 61 62 61 73 65 20  e, the database 
45d30 6e 61 6d 65 20 69 73 20 22 6d 61 69 6e 22 2e 0a  name is "main"..
45d40 2a 2a 20 46 6f 72 20 54 45 4d 50 20 74 61 62 6c  ** For TEMP tabl
45d50 65 73 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  es, the database
45d60 20 6e 61 6d 65 20 69 73 20 22 74 65 6d 70 22 2e   name is "temp".
45d70 0a 2a 2a 0a 2a 2a 20 4f 6e 20 73 75 63 63 65 73  .**.** On succes
45d80 73 2c 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20 69  s, [SQLITE_OK] i
45d90 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74  s returned and t
45da0 68 65 20 6e 65 77 20 5b 42 4c 4f 42 20 68 61 6e  he new [BLOB han
45db0 64 6c 65 5d 20 69 73 20 77 72 69 74 74 65 6e 0a  dle] is written.
45dc0 2a 2a 20 74 6f 20 2a 70 70 42 6c 6f 62 2e 20 4f  ** to *ppBlob. O
45dd0 74 68 65 72 77 69 73 65 20 61 6e 20 5b 65 72 72  therwise an [err
45de0 6f 72 20 63 6f 64 65 5d 20 69 73 20 72 65 74 75  or code] is retu
45df0 72 6e 65 64 20 61 6e 64 20 61 6e 79 20 76 61 6c  rned and any val
45e00 75 65 20 77 72 69 74 74 65 6e 0a 2a 2a 20 74 6f  ue written.** to
45e10 20 2a 70 70 42 6c 6f 62 20 73 68 6f 75 6c 64 20   *ppBlob should 
45e20 6e 6f 74 20 62 65 20 75 73 65 64 20 62 79 20 74  not be used by t
45e30 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2a 20 54 68  he caller..** Th
45e40 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 65 74 73  is function sets
45e50 20 74 68 65 20 5b 64 61 74 61 62 61 73 65 20 63   the [database c
45e60 6f 6e 6e 65 63 74 69 6f 6e 5d 20 65 72 72 6f 72  onnection] error
45e70 20 63 6f 64 65 20 61 6e 64 20 6d 65 73 73 61 67   code and messag
45e80 65 0a 2a 2a 20 61 63 63 65 73 73 69 62 6c 65 20  e.** accessible 
45e90 76 69 61 20 5b 73 71 6c 69 74 65 33 5f 65 72 72  via [sqlite3_err
45ea0 63 6f 64 65 28 29 5d 20 61 6e 64 20 5b 73 71 6c  code()] and [sql
45eb0 69 74 65 33 5f 65 72 72 6d 73 67 28 29 5d 2e 0a  ite3_errmsg()]..
45ec0 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72 6f 77  **.** If the row
45ed0 20 74 68 61 74 20 61 20 42 4c 4f 42 20 68 61 6e   that a BLOB han
45ee0 64 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20 69 73  dle points to is
45ef0 20 6d 6f 64 69 66 69 65 64 20 62 79 20 61 6e 0a   modified by an.
45f00 2a 2a 20 5b 55 50 44 41 54 45 5d 2c 20 5b 44 45  ** [UPDATE], [DE
45f10 4c 45 54 45 5d 2c 20 6f 72 20 62 79 20 5b 4f 4e  LETE], or by [ON
45f20 20 43 4f 4e 46 4c 49 43 54 5d 20 73 69 64 65 2d   CONFLICT] side-
45f30 65 66 66 65 63 74 73 0a 2a 2a 20 74 68 65 6e 20  effects.** then 
45f40 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65 20  the BLOB handle 
45f50 69 73 20 6d 61 72 6b 65 64 20 61 73 20 22 65 78  is marked as "ex
45f60 70 69 72 65 64 22 2e 0a 2a 2a 20 54 68 69 73 20  pired"..** This 
45f70 69 73 20 74 72 75 65 20 69 66 20 61 6e 79 20 63  is true if any c
45f80 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 6f 77  olumn of the row
45f90 20 69 73 20 63 68 61 6e 67 65 64 2c 20 65 76 65   is changed, eve
45fa0 6e 20 61 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 74  n a column.** ot
45fb0 68 65 72 20 74 68 61 6e 20 74 68 65 20 6f 6e 65  her than the one
45fc0 20 74 68 65 20 42 4c 4f 42 20 68 61 6e 64 6c 65   the BLOB handle
45fd0 20 69 73 20 6f 70 65 6e 20 6f 6e 2e 0a 2a 2a 20   is open on..** 
45fe0 43 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65  Calls to [sqlite
45ff0 33 5f 62 6c 6f 62 5f 72 65 61 64 28 29 5d 20 61  3_blob_read()] a
46000 6e 64 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  nd [sqlite3_blob
46010 5f 77 72 69 74 65 28 29 5d 20 66 6f 72 0a 2a 2a  _write()] for.**
46020 20 61 20 65 78 70 69 72 65 64 20 42 4c 4f 42 20   a expired BLOB 
46030 68 61 6e 64 6c 65 20 66 61 69 6c 20 77 69 74 68  handle fail with
46040 20 61 6e 20 72 65 74 75 72 6e 20 63 6f 64 65 20   an return code 
46050 6f 66 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54  of [SQLITE_ABORT
46060 5d 2e 0a 2a 2a 20 43 68 61 6e 67 65 73 20 77 72  ]..** Changes wr
46070 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 42 4c 4f  itten into a BLO
46080 42 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 42  B prior to the B
46090 4c 4f 42 20 65 78 70 69 72 69 6e 67 20 61 72 65  LOB expiring are
460a0 20 6e 6f 74 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b   not.** rollback
460b0 20 62 79 20 74 68 65 20 65 78 70 69 72 61 74 69   by the expirati
460c0 6f 6e 20 6f 66 20 74 68 65 20 42 4c 4f 42 2e 20  on of the BLOB. 
460d0 20 53 75 63 68 20 63 68 61 6e 67 65 73 20 77 69   Such changes wi
460e0 6c 6c 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a 2a  ll eventually.**
460f0 20 63 6f 6d 6d 69 74 20 69 66 20 74 68 65 20 74   commit if the t
46100 72 61 6e 73 61 63 74 69 6f 6e 20 63 6f 6e 74 69  ransaction conti
46110 6e 75 65 73 20 74 6f 20 63 6f 6d 70 6c 65 74 69  nues to completi
46120 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  on..**.** INVARI
46130 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ANTS:.**.** {H17
46140 38 31 33 7d 20 41 20 73 75 63 63 65 73 73 66 75  813} A successfu
46150 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20  l invocation of 
46160 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f  the [sqlite3_blo
46170 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c 52  b_open(D,B,T,C,R
46180 2c 46 2c 50 29 5d 0a 2a 2a 20 20 20 20 20 20 20  ,F,P)].**       
46190 20 20 20 69 6e 74 65 72 66 61 63 65 20 73 68 61     interface sha
461a0 6c 6c 20 6f 70 65 6e 20 61 6e 20 5b 73 71 6c 69  ll open an [sqli
461b0 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74  te3_blob] object
461c0 20 50 20 6f 6e 20 74 68 65 20 42 4c 4f 42 0a 2a   P on the BLOB.*
461d0 2a 20 20 20 20 20 20 20 20 20 20 69 6e 20 63 6f  *          in co
461e0 6c 75 6d 6e 20 43 20 6f 66 20 74 68 65 20 74 61  lumn C of the ta
461f0 62 6c 65 20 54 20 69 6e 20 74 68 65 20 64 61 74  ble T in the dat
46200 61 62 61 73 65 20 42 20 6f 6e 0a 2a 2a 20 20 20  abase B on.**   
46210 20 20 20 20 20 20 20 74 68 65 20 5b 64 61 74 61         the [data
46220 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
46230 20 44 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31   D..**.** {H1781
46240 34 7d 20 41 20 73 75 63 63 65 73 73 66 75 6c 20  4} A successful 
46250 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73  invocation of [s
46260 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e  qlite3_blob_open
46270 28 44 2c 2e 2e 2e 29 5d 20 73 68 61 6c 6c 20 73  (D,...)] shall s
46280 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  tart.**         
46290 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69   a new transacti
462a0 6f 6e 20 6f 6e 20 74 68 65 20 5b 64 61 74 61 62  on on the [datab
462b0 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20  ase connection] 
462c0 44 20 69 66 20 74 68 61 74 0a 2a 2a 20 20 20 20  D if that.**    
462d0 20 20 20 20 20 20 63 6f 6e 6e 65 63 74 69 6f 6e        connection
462e0 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
462f0 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
46300 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31 36 7d  ..**.** {H17816}
46310 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c   The [sqlite3_bl
46320 6f 62 5f 6f 70 65 6e 28 44 2c 42 2c 54 2c 43 2c  ob_open(D,B,T,C,
46330 52 2c 46 2c 50 29 5d 20 69 6e 74 65 72 66 61 63  R,F,P)] interfac
46340 65 20 73 68 61 6c 6c 20 6f 70 65 6e 0a 2a 2a 20  e shall open.** 
46350 20 20 20 20 20 20 20 20 20 74 68 65 20 42 4c 4f           the BLO
46360 42 20 66 6f 72 20 72 65 61 64 20 61 6e 64 20 77  B for read and w
46370 72 69 74 65 20 61 63 63 65 73 73 20 69 66 20 61  rite access if a
46380 6e 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 20 46  nd only if the F
46390 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 70 61 72  .**          par
463a0 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
463b0 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 31  ro..**.** {H1781
463c0 39 7d 20 54 68 65 20 5b 73 71 6c 69 74 65 33 5f  9} The [sqlite3_
463d0 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 20 69 6e 74  blob_open()] int
463e0 65 72 66 61 63 65 20 73 68 61 6c 6c 20 72 65 74  erface shall ret
463f0 75 72 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20  urn [SQLITE_OK] 
46400 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 73  on.**          s
46410 75 63 63 65 73 73 20 61 6e 64 20 61 6e 20 61 70  uccess and an ap
46420 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72  propriate [error
46430 20 63 6f 64 65 5d 20 6f 6e 20 66 61 69 6c 75 72   code] on failur
46440 65 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 32 31  e..**.** {H17821
46450 7d 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  } If an error oc
46460 63 75 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c  curs during eval
46470 75 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  uation of [sqlit
46480 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 44 2c 2e  e3_blob_open(D,.
46490 2e 2e 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..)].**         
464a0 20 74 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74   then subsequent
464b0 20 63 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74   calls to [sqlit
464c0 65 33 5f 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a  e3_errcode(D)],.
464d0 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c  **          [sql
464e0 69 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72  ite3_extended_er
464f0 72 63 6f 64 65 28 29 5d 2c 20 0a 2a 2a 20 20 20  rcode()], .**   
46500 20 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f         [sqlite3_
46510 65 72 72 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20  errmsg(D)], and 
46520 5b 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31  [sqlite3_errmsg1
46530 36 28 44 29 5d 20 73 68 61 6c 6c 20 72 65 74 75  6(D)] shall retu
46540 72 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  rn.**          i
46550 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f  nformation appro
46560 70 72 69 61 74 65 20 66 6f 72 20 74 68 61 74 20  priate for that 
46570 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  error..**.** {H1
46580 37 38 32 34 7d 20 49 66 20 61 6e 79 20 63 6f 6c  7824} If any col
46590 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f 77 20 74  umn in the row t
465a0 68 61 74 20 61 20 5b 73 71 6c 69 74 65 33 5f 62  hat a [sqlite3_b
465b0 6c 6f 62 5d 20 68 61 73 20 6f 70 65 6e 20 69 73  lob] has open is
465c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 68 61  .**          cha
465d0 6e 67 65 64 20 62 79 20 61 20 73 65 70 61 72 61  nged by a separa
465e0 74 65 20 5b 55 50 44 41 54 45 5d 20 6f 72 20 5b  te [UPDATE] or [
465f0 44 45 4c 45 54 45 5d 20 73 74 61 74 65 6d 65 6e  DELETE] statemen
46600 74 20 6f 72 20 62 79 0a 2a 2a 20 20 20 20 20 20  t or by.**      
46610 20 20 20 20 61 6e 20 5b 4f 4e 20 43 4f 4e 46 4c      an [ON CONFL
46620 49 43 54 5d 20 73 69 64 65 20 65 66 66 65 63 74  ICT] side effect
46630 2c 20 74 68 65 6e 20 74 68 65 20 5b 73 71 6c 69  , then the [sqli
46640 74 65 33 5f 62 6c 6f 62 5d 20 73 68 61 6c 6c 0a  te3_blob] shall.
46650 2a 2a 20 20 20 20 20 20 20 20 20 20 62 65 20 6d  **          be m
46660 61 72 6b 65 64 20 61 73 20 69 6e 76 61 6c 69 64  arked as invalid
46670 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
46680 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  int sqlite3_blob
46690 5f 6f 70 65 6e 28 0a 20 20 73 71 6c 69 74 65 33  _open(.  sqlite3
466a0 2a 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  *,.  const char 
466b0 2a 7a 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68  *zDb,.  const ch
466c0 61 72 20 2a 7a 54 61 62 6c 65 2c 0a 20 20 63 6f  ar *zTable,.  co
466d0 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 75 6d  nst char *zColum
466e0 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  n,.  sqlite3_int
466f0 36 34 20 69 52 6f 77 2c 0a 20 20 69 6e 74 20 66  64 iRow,.  int f
46700 6c 61 67 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f  lags,.  sqlite3_
46710 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62 0a 29 3b  blob **ppBlob.);
46720 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46  ../*.** CAPI3REF
46730 3a 20 43 6c 6f 73 65 20 41 20 42 4c 4f 42 20 48  : Close A BLOB H
46740 61 6e 64 6c 65 20 7b 48 31 37 38 33 30 7d 20 3c  andle {H17830} <
46750 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 43 6c  S30230>.**.** Cl
46760 6f 73 65 73 20 61 6e 20 6f 70 65 6e 20 5b 42 4c  oses an open [BL
46770 4f 42 20 68 61 6e 64 6c 65 5d 2e 0a 2a 2a 0a 2a  OB handle]..**.*
46780 2a 20 43 6c 6f 73 69 6e 67 20 61 20 42 4c 4f 42  * Closing a BLOB
46790 20 73 68 61 6c 6c 20 63 61 75 73 65 20 74 68 65   shall cause the
467a0 20 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63   current transac
467b0 74 69 6f 6e 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a  tion to commit.*
467c0 2a 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  * if there are n
467d0 6f 20 6f 74 68 65 72 20 42 4c 4f 42 73 2c 20 6e  o other BLOBs, n
467e0 6f 20 70 65 6e 64 69 6e 67 20 70 72 65 70 61 72  o pending prepar
467f0 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2c 20 61  ed statements, a
46800 6e 64 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61  nd the.** databa
46810 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73  se connection is
46820 20 69 6e 20 5b 61 75 74 6f 63 6f 6d 6d 69 74 20   in [autocommit 
46830 6d 6f 64 65 5d 2e 0a 2a 2a 20 49 66 20 61 6e 79  mode]..** If any
46840 20 77 72 69 74 65 73 20 77 65 72 65 20 6d 61 64   writes were mad
46850 65 20 74 6f 20 74 68 65 20 42 4c 4f 42 2c 20 74  e to the BLOB, t
46860 68 65 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c  hey might be hel
46870 64 20 69 6e 20 63 61 63 68 65 0a 2a 2a 20 75 6e  d in cache.** un
46880 74 69 6c 20 74 68 65 20 63 6c 6f 73 65 20 6f 70  til the close op
46890 65 72 61 74 69 6f 6e 20 69 66 20 74 68 65 79 20  eration if they 
468a0 77 69 6c 6c 20 66 69 74 2e 20 7b 45 4e 44 7d 0a  will fit. {END}.
468b0 2a 2a 0a 2a 2a 20 43 6c 6f 73 69 6e 67 20 74 68  **.** Closing th
468c0 65 20 42 4c 4f 42 20 6f 66 74 65 6e 20 66 6f 72  e BLOB often for
468d0 63 65 73 20 74 68 65 20 63 68 61 6e 67 65 73 0a  ces the changes.
468e0 2a 2a 20 6f 75 74 20 74 6f 20 64 69 73 6b 20 61  ** out to disk a
468f0 6e 64 20 73 6f 20 69 66 20 61 6e 79 20 49 2f 4f  nd so if any I/O
46900 20 65 72 72 6f 72 73 20 6f 63 63 75 72 2c 20 74   errors occur, t
46910 68 65 79 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20  hey will likely 
46920 6f 63 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20  occur.** at the 
46930 74 69 6d 65 20 77 68 65 6e 20 74 68 65 20 42 4c  time when the BL
46940 4f 42 20 69 73 20 63 6c 6f 73 65 64 2e 20 20 7b  OB is closed.  {
46950 48 31 37 38 33 33 7d 20 41 6e 79 20 65 72 72 6f  H17833} Any erro
46960 72 73 20 74 68 61 74 20 6f 63 63 75 72 20 64 75  rs that occur du
46970 72 69 6e 67 0a 2a 2a 20 63 6c 6f 73 69 6e 67 20  ring.** closing 
46980 61 72 65 20 72 65 70 6f 72 74 65 64 20 61 73 20  are reported as 
46990 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75 72  a non-zero retur
469a0 6e 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54  n value..**.** T
469b0 68 65 20 42 4c 4f 42 20 69 73 20 63 6c 6f 73 65  he BLOB is close
469c0 64 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c  d unconditionall
469d0 79 2e 20 20 45 76 65 6e 20 69 66 20 74 68 69 73  y.  Even if this
469e0 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
469f0 0a 2a 2a 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  .** an error cod
46a00 65 2c 20 74 68 65 20 42 4c 4f 42 20 69 73 20 73  e, the BLOB is s
46a10 74 69 6c 6c 20 63 6c 6f 73 65 64 2e 0a 2a 2a 0a  till closed..**.
46a20 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a  ** INVARIANTS:.*
46a30 2a 0a 2a 2a 20 7b 48 31 37 38 33 33 7d 20 54 68  *.** {H17833} Th
46a40 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  e [sqlite3_blob_
46a50 63 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66  close(P)] interf
46a60 61 63 65 20 63 6c 6f 73 65 73 20 61 6e 20 5b 73  ace closes an [s
46a70 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 0a 2a 2a 20  qlite3_blob].** 
46a80 20 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74 20           object 
46a90 50 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65  P previously ope
46aa0 6e 65 64 20 75 73 69 6e 67 20 5b 73 71 6c 69 74  ned using [sqlit
46ab0 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 29 5d 2e  e3_blob_open()].
46ac0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 33 36 7d 20  .**.** {H17836} 
46ad0 43 6c 6f 73 69 6e 67 20 61 6e 20 5b 73 71 6c 69  Closing an [sqli
46ae0 74 65 33 5f 62 6c 6f 62 5d 20 6f 62 6a 65 63 74  te3_blob] object
46af0 20 75 73 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20   using.**       
46b00 20 20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62     [sqlite3_blob
46b10 5f 63 6c 6f 73 65 28 29 5d 20 73 68 61 6c 6c 20  _close()] shall 
46b20 63 61 75 73 65 20 74 68 65 20 63 75 72 72 65 6e  cause the curren
46b30 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74 6f  t transaction to
46b40 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 63 6f 6d  .**          com
46b50 6d 69 74 20 69 66 20 74 68 65 72 65 20 61 72 65  mit if there are
46b60 20 6e 6f 20 6f 74 68 65 72 20 6f 70 65 6e 20 5b   no other open [
46b70 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f 62  sqlite3_blob] ob
46b80 6a 65 63 74 73 0a 2a 2a 20 20 20 20 20 20 20 20  jects.**        
46b90 20 20 6f 72 20 5b 70 72 65 70 61 72 65 64 20 73    or [prepared s
46ba0 74 61 74 65 6d 65 6e 74 73 5d 20 6f 6e 20 74 68  tatements] on th
46bb0 65 20 73 61 6d 65 20 5b 64 61 74 61 62 61 73 65  e same [database
46bc0 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d 20 61 6e 64   connection] and
46bd0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
46be0 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
46bf0 74 69 6f 6e 20 69 73 20 69 6e 20 5b 61 75 74 6f  tion is in [auto
46c00 63 6f 6d 6d 69 74 20 6d 6f 64 65 5d 2e 0a 2a 2a  commit mode]..**
46c10 0a 2a 2a 20 7b 48 31 37 38 33 39 7d 20 54 68 65  .** {H17839} The
46c20 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63   [sqlite3_blob_c
46c30 6c 6f 73 65 28 50 29 5d 20 69 6e 74 65 72 66 61  lose(P)] interfa
46c40 63 65 73 20 73 68 61 6c 6c 20 63 6c 6f 73 65 20  ces shall close 
46c50 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
46c60 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5d 20 6f  [sqlite3_blob] o
46c70 62 6a 65 63 74 20 50 20 75 6e 63 6f 6e 64 69 74  bject P uncondit
46c80 69 6f 6e 61 6c 6c 79 2c 20 65 76 65 6e 20 69 66  ionally, even if
46c90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71  .**          [sq
46ca0 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65  lite3_blob_close
46cb0 28 50 29 5d 20 72 65 74 75 72 6e 73 20 73 6f 6d  (P)] returns som
46cc0 65 74 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61  ething other tha
46cd0 6e 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 2e 0a 2a  n [SQLITE_OK]..*
46ce0 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
46cf0 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c   sqlite3_blob_cl
46d00 6f 73 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ose(sqlite3_blob
46d10 20 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49   *);../*.** CAPI
46d20 33 52 45 46 3a 20 52 65 74 75 72 6e 20 54 68 65  3REF: Return The
46d30 20 53 69 7a 65 20 4f 66 20 41 6e 20 4f 70 65 6e   Size Of An Open
46d40 20 42 4c 4f 42 20 7b 48 31 37 38 34 30 7d 20 3c   BLOB {H17840} <
46d50 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20 52 65  S30230>.**.** Re
46d60 74 75 72 6e 73 20 74 68 65 20 73 69 7a 65 20 69  turns the size i
46d70 6e 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 42  n bytes of the B
46d80 4c 4f 42 20 61 63 63 65 73 73 69 62 6c 65 20 76  LOB accessible v
46d90 69 61 20 74 68 65 20 6f 70 65 6e 0a 2a 2a 20 5b  ia the open.** [
46da0 5d 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 69 6e  ]BLOB handle] in
46db0 20 69 74 73 20 6f 6e 6c 79 20 61 72 67 75 6d 65   its only argume
46dc0 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 4e 56 41 52 49  nt..**.** INVARI
46dd0 41 4e 54 53 3a 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  ANTS:.**.** {H17
46de0 38 34 33 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  843} The [sqlite
46df0 33 5f 62 6c 6f 62 5f 62 79 74 65 73 28 50 29 5d  3_blob_bytes(P)]
46e00 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75 72   interface retur
46e10 6e 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a 20 20  ns the size.**  
46e20 20 20 20 20 20 20 20 20 69 6e 20 62 79 74 65 73          in bytes
46e30 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 74 68 61   of the BLOB tha
46e40 74 20 74 68 65 20 5b 73 71 6c 69 74 65 33 5f 62  t the [sqlite3_b
46e50 6c 6f 62 5d 20 6f 62 6a 65 63 74 20 50 0a 2a 2a  lob] object P.**
46e60 20 20 20 20 20 20 20 20 20 20 72 65 66 65 72 73            refers
46e70 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   to..*/.SQLITE_A
46e80 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
46e90 6c 6f 62 5f 62 79 74 65 73 28 73 71 6c 69 74 65  lob_bytes(sqlite
46ea0 33 5f 62 6c 6f 62 20 2a 29 3b 0a 0a 2f 2a 0a 2a  3_blob *);../*.*
46eb0 2a 20 43 41 50 49 33 52 45 46 3a 20 52 65 61 64  * CAPI3REF: Read
46ec0 20 44 61 74 61 20 46 72 6f 6d 20 41 20 42 4c 4f   Data From A BLO
46ed0 42 20 49 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20  B Incrementally 
46ee0 7b 48 31 37 38 35 30 7d 20 3c 53 33 30 32 33 30  {H17850} <S30230
46ef0 3e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  >.**.** This fun
46f00 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f  ction is used to
46f10 20 72 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   read data from 
46f20 61 6e 20 6f 70 65 6e 20 5b 42 4c 4f 42 20 68 61  an open [BLOB ha
46f30 6e 64 6c 65 5d 20 69 6e 74 6f 20 61 0a 2a 2a 20  ndle] into a.** 
46f40 63 61 6c 6c 65 72 2d 73 75 70 70 6c 69 65 64 20  caller-supplied 
46f50 62 75 66 66 65 72 2e 20 4e 20 62 79 74 65 73 20  buffer. N bytes 
46f60 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f 70 69  of data are copi
46f70 65 64 20 69 6e 74 6f 20 62 75 66 66 65 72 20 5a  ed into buffer Z
46f80 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 6f 70 65  .** from the ope
46f90 6e 20 42 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67  n BLOB, starting
46fa0 20 61 74 20 6f 66 66 73 65 74 20 69 4f 66 66 73   at offset iOffs
46fb0 65 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6f 66 66  et..**.** If off
46fc0 73 65 74 20 69 4f 66 66 73 65 74 20 69 73 20 6c  set iOffset is l
46fd0 65 73 73 20 74 68 61 6e 20 4e 20 62 79 74 65 73  ess than N bytes
46fe0 20 66 72 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66   from the end of
46ff0 20 74 68 65 20 42 4c 4f 42 2c 0a 2a 2a 20 5b 53   the BLOB,.** [S
47000 51 4c 49 54 45 5f 45 52 52 4f 52 5d 20 69 73 20  QLITE_ERROR] is 
47010 72 65 74 75 72 6e 65 64 20 61 6e 64 20 6e 6f 20  returned and no 
47020 64 61 74 61 20 69 73 20 72 65 61 64 2e 20 20 49  data is read.  I
47030 66 20 4e 20 6f 72 20 69 4f 66 66 73 65 74 20 69  f N or iOffset i
47040 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a  s.** less than z
47050 65 72 6f 2c 20 5b 53 51 4c 49 54 45 5f 45 52 52  ero, [SQLITE_ERR
47060 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20  OR] is returned 
47070 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 72  and no data is r
47080 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 61 74  ead..**.** An at
47090 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 66 72  tempt to read fr
470a0 6f 6d 20 61 6e 20 65 78 70 69 72 65 64 20 5b 42  om an expired [B
470b0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69 6c  LOB handle] fail
470c0 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72 72  s with an.** err
470d0 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c 49  or code of [SQLI
470e0 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a 0a 2a 2a  TE_ABORT]..**.**
470f0 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53 51 4c   On success, SQL
47100 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
47110 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65  ed..** Otherwise
47120 2c 20 61 6e 20 5b 65 72 72 6f 72 20 63 6f 64 65  , an [error code
47130 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65 6e 64 65  ] or an [extende
47140 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 20 69 73  d error code] is
47150 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
47160 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a 0a   INVARIANTS:.**.
47170 2a 2a 20 7b 48 31 37 38 35 33 7d 20 41 20 73 75  ** {H17853} A su
47180 63 63 65 73 73 66 75 6c 20 69 6e 76 6f 63 61 74  ccessful invocat
47190 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33 5f  ion of [sqlite3_
471a0 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c  blob_read(P,Z,N,
471b0 58 29 5d 20 0a 2a 2a 20 20 20 20 20 20 20 20 20  X)] .**         
471c0 20 73 68 61 6c 6c 20 72 65 61 64 73 20 4e 20 62   shall reads N b
471d0 79 74 65 73 20 6f 66 20 64 61 74 61 20 6f 75 74  ytes of data out
471e0 20 6f 66 20 74 68 65 20 42 4c 4f 42 20 72 65 66   of the BLOB ref
471f0 65 72 65 6e 63 65 64 20 62 79 0a 2a 2a 20 20 20  erenced by.**   
47200 20 20 20 20 20 20 20 5b 42 4c 4f 42 20 68 61 6e         [BLOB han
47210 64 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67  dle] P beginning
47220 20 61 74 20 6f 66 66 73 65 74 20 58 20 61 6e 64   at offset X and
47230 20 73 74 6f 72 65 20 74 68 6f 73 65 20 62 79 74   store those byt
47240 65 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 69  es.**          i
47250 6e 74 6f 20 62 75 66 66 65 72 20 5a 2e 0a 2a 2a  nto buffer Z..**
47260 0a 2a 2a 20 7b 48 31 37 38 35 36 7d 20 49 6e 20  .** {H17856} In 
47270 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65  [sqlite3_blob_re
47280 61 64 28 50 2c 5a 2c 4e 2c 58 29 5d 20 69 66 20  ad(P,Z,N,X)] if 
47290 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
472a0 42 4c 4f 42 0a 2a 2a 20 20 20 20 20 20 20 20 20  BLOB.**         
472b0 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 4e 2b   is less than N+
472c0 58 20 62 79 74 65 73 2c 20 74 68 65 6e 20 74 68  X bytes, then th
472d0 65 20 66 75 6e 63 74 69 6f 6e 20 73 68 61 6c 6c  e function shall
472e0 20 6c 65 61 76 65 20 74 68 65 0a 2a 2a 20 20 20   leave the.**   
472f0 20 20 20 20 20 20 20 5a 20 62 75 66 66 65 72 20         Z buffer 
47300 75 6e 63 68 61 6e 67 65 64 20 61 6e 64 20 72 65  unchanged and re
47310 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f 45 52 52  turn [SQLITE_ERR
47320 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38  OR]..**.** {H178
47330 35 39 7d 20 49 6e 20 5b 73 71 6c 69 74 65 33 5f  59} In [sqlite3_
47340 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c  blob_read(P,Z,N,
47350 58 29 5d 20 69 66 20 58 20 6f 72 20 4e 20 69 73  X)] if X or N is
47360 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f 0a   less than zero.
47370 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 6e  **          then
47380 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 73 68   the function sh
47390 61 6c 6c 20 6c 65 61 76 65 20 74 68 65 20 5a 20  all leave the Z 
473a0 62 75 66 66 65 72 20 75 6e 63 68 61 6e 67 65 64  buffer unchanged
473b0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 6e 64  .**          and
473c0 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54 45 5f   return [SQLITE_
473d0 45 52 52 4f 52 5d 2e 0a 2a 2a 0a 2a 2a 20 7b 48  ERROR]..**.** {H
473e0 31 37 38 36 32 7d 20 54 68 65 20 5b 73 71 6c 69  17862} The [sqli
473f0 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c  te3_blob_read(P,
47400 5a 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63  Z,N,X)] interfac
47410 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 5b  e shall return [
47420 53 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 20 20  SQLITE_OK].**   
47430 20 20 20 20 20 20 20 69 66 20 4e 20 62 79 74 65         if N byte
47440 73 20 61 72 65 20 73 75 63 63 65 73 73 66 75 6c  s are successful
47450 6c 79 20 72 65 61 64 20 69 6e 74 6f 20 62 75 66  ly read into buf
47460 66 65 72 20 5a 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  fer Z..**.** {H1
47470 37 38 36 33 7d 20 49 66 20 74 68 65 20 5b 42 4c  7863} If the [BL
47480 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 69 73 20  OB handle] P is 
47490 65 78 70 69 72 65 64 20 61 6e 64 20 58 20 61 6e  expired and X an
474a0 64 20 4e 20 61 72 65 20 77 69 74 68 69 6e 20 62  d N are within b
474b0 6f 75 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20  ounds.**        
474c0 20 20 74 68 65 6e 20 5b 73 71 6c 69 74 65 33 5f    then [sqlite3_
474d0 62 6c 6f 62 5f 72 65 61 64 28 50 2c 5a 2c 4e 2c  blob_read(P,Z,N,
474e0 58 29 5d 20 73 68 61 6c 6c 20 6c 65 61 76 65 20  X)] shall leave 
474f0 74 68 65 20 5a 20 62 75 66 66 65 72 0a 2a 2a 20  the Z buffer.** 
47500 20 20 20 20 20 20 20 20 20 75 6e 63 68 61 6e 67           unchang
47510 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53  ed and return [S
47520 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e 0a 2a 2a  QLITE_ABORT]..**
47530 0a 2a 2a 20 7b 48 31 37 38 36 35 7d 20 49 66 20  .** {H17865} If 
47540 74 68 65 20 72 65 71 75 65 73 74 65 64 20 72 65  the requested re
47550 61 64 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ad could not be 
47560 63 6f 6d 70 6c 65 74 65 64 2c 0a 2a 2a 20 20 20  completed,.**   
47570 20 20 20 20 20 20 20 74 68 65 20 5b 73 71 6c 69         the [sqli
47580 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c  te3_blob_read(P,
47590 5a 2c 4e 2c 58 29 5d 20 69 6e 74 65 72 66 61 63  Z,N,X)] interfac
475a0 65 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 20 61  e shall return a
475b0 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 61 70  n.**          ap
475c0 70 72 6f 70 72 69 61 74 65 20 5b 65 72 72 6f 72  propriate [error
475d0 20 63 6f 64 65 5d 20 6f 72 20 5b 65 78 74 65 6e   code] or [exten
475e0 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d 2e  ded error code].
475f0 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 36 38 7d 20  .**.** {H17868} 
47600 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
47610 72 73 20 64 75 72 69 6e 67 20 65 76 61 6c 75 61  rs during evalua
47620 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74 65 33  tion of [sqlite3
47630 5f 62 6c 6f 62 5f 72 65 61 64 28 50 2c 2e 2e 2e  _blob_read(P,...
47640 29 5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  )].**          t
47650 68 65 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63  hen subsequent c
47660 61 6c 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33  alls to [sqlite3
47670 5f 65 72 72 63 6f 64 65 28 44 29 5d 2c 0a 2a 2a  _errcode(D)],.**
47680 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69 74            [sqlit
47690 65 33 5f 65 78 74 65 6e 64 65 64 5f 65 72 72 63  e3_extended_errc
476a0 6f 64 65 28 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  ode()],.**      
476b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 72 72      [sqlite3_err
476c0 6d 73 67 28 44 29 5d 2c 20 61 6e 64 20 5b 73 71  msg(D)], and [sq
476d0 6c 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 44  lite3_errmsg16(D
476e0 29 5d 20 73 68 61 6c 6c 20 72 65 74 75 72 6e 0a  )] shall return.
476f0 2a 2a 20 20 20 20 20 20 20 20 20 20 69 6e 66 6f  **          info
47700 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f 70 72 69  rmation appropri
47710 61 74 65 20 66 6f 72 20 74 68 61 74 20 65 72 72  ate for that err
47720 6f 72 2c 20 77 68 65 72 65 20 44 20 69 73 20 74  or, where D is t
47730 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b  he.**          [
47740 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
47750 69 6f 6e 5d 20 74 68 61 74 20 77 61 73 20 75 73  ion] that was us
47760 65 64 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 5b  ed to open the [
47770 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 2e 0a  BLOB handle] P..
47780 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
47790 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72  t sqlite3_blob_r
477a0 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ead(sqlite3_blob
477b0 20 2a 2c 20 76 6f 69 64 20 2a 5a 2c 20 69 6e 74   *, void *Z, int
477c0 20 4e 2c 20 69 6e 74 20 69 4f 66 66 73 65 74 29   N, int iOffset)
477d0 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45  ;../*.** CAPI3RE
477e0 46 3a 20 57 72 69 74 65 20 44 61 74 61 20 49 6e  F: Write Data In
477f0 74 6f 20 41 20 42 4c 4f 42 20 49 6e 63 72 65 6d  to A BLOB Increm
47800 65 6e 74 61 6c 6c 79 20 7b 48 31 37 38 37 30 7d  entally {H17870}
47810 20 3c 53 33 30 32 33 30 3e 0a 2a 2a 0a 2a 2a 20   <S30230>.**.** 
47820 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
47830 20 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64   used to write d
47840 61 74 61 20 69 6e 74 6f 20 61 6e 20 6f 70 65 6e  ata into an open
47850 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66   [BLOB handle] f
47860 72 6f 6d 20 61 0a 2a 2a 20 63 61 6c 6c 65 72 2d  rom a.** caller-
47870 73 75 70 70 6c 69 65 64 20 62 75 66 66 65 72 2e  supplied buffer.
47880 20 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61   N bytes of data
47890 20 61 72 65 20 63 6f 70 69 65 64 20 66 72 6f 6d   are copied from
478a0 20 74 68 65 20 62 75 66 66 65 72 20 5a 0a 2a 2a   the buffer Z.**
478b0 20 69 6e 74 6f 20 74 68 65 20 6f 70 65 6e 20 42   into the open B
478c0 4c 4f 42 2c 20 73 74 61 72 74 69 6e 67 20 61 74  LOB, starting at
478d0 20 6f 66 66 73 65 74 20 69 4f 66 66 73 65 74 2e   offset iOffset.
478e0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 5b 42  .**.** If the [B
478f0 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 70 61 73 73  LOB handle] pass
47900 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ed as the first 
47910 61 72 67 75 6d 65 6e 74 20 77 61 73 20 6e 6f 74  argument was not
47920 20 6f 70 65 6e 65 64 20 66 6f 72 0a 2a 2a 20 77   opened for.** w
47930 72 69 74 69 6e 67 20 28 74 68 65 20 66 6c 61 67  riting (the flag
47940 73 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b  s parameter to [
47950 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65  sqlite3_blob_ope
47960 6e 28 29 5d 20 77 61 73 20 7a 65 72 6f 29 2c 0a  n()] was zero),.
47970 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** this function
47980 20 72 65 74 75 72 6e 73 20 5b 53 51 4c 49 54 45   returns [SQLITE
47990 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a 2a  _READONLY]..**.*
479a0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
479b0 6d 61 79 20 6f 6e 6c 79 20 6d 6f 64 69 66 79 20  may only modify 
479c0 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
479d0 74 68 65 20 42 4c 4f 42 3b 20 69 74 20 69 73 0a  the BLOB; it is.
479e0 2a 2a 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20  ** not possible 
479f0 74 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  to increase the 
47a00 73 69 7a 65 20 6f 66 20 61 20 42 4c 4f 42 20 75  size of a BLOB u
47a10 73 69 6e 67 20 74 68 69 73 20 41 50 49 2e 0a 2a  sing this API..*
47a20 2a 20 49 66 20 6f 66 66 73 65 74 20 69 4f 66 66  * If offset iOff
47a30 73 65 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  set is less than
47a40 20 4e 20 62 79 74 65 73 20 66 72 6f 6d 20 74 68   N bytes from th
47a50 65 20 65 6e 64 20 6f 66 20 74 68 65 20 42 4c 4f  e end of the BLO
47a60 42 2c 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 45 52  B,.** [SQLITE_ER
47a70 52 4f 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64  ROR] is returned
47a80 20 61 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20   and no data is 
47a90 77 72 69 74 74 65 6e 2e 20 20 49 66 20 4e 20 69  written.  If N i
47aa0 73 0a 2a 2a 20 6c 65 73 73 20 74 68 61 6e 20 7a  s.** less than z
47ab0 65 72 6f 20 5b 53 51 4c 49 54 45 5f 45 52 52 4f  ero [SQLITE_ERRO
47ac0 52 5d 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  R] is returned a
47ad0 6e 64 20 6e 6f 20 64 61 74 61 20 69 73 20 77 72  nd no data is wr
47ae0 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20  itten..**.** An 
47af0 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65  attempt to write
47b00 20 74 6f 20 61 6e 20 65 78 70 69 72 65 64 20 5b   to an expired [
47b10 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 66 61 69  BLOB handle] fai
47b20 6c 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 65 72  ls with an.** er
47b30 72 6f 72 20 63 6f 64 65 20 6f 66 20 5b 53 51 4c  ror code of [SQL
47b40 49 54 45 5f 41 42 4f 52 54 5d 2e 20 20 57 72 69  ITE_ABORT].  Wri
47b50 74 65 73 20 74 6f 20 74 68 65 20 42 4c 4f 42 20  tes to the BLOB 
47b60 74 68 61 74 20 6f 63 63 75 72 72 65 64 0a 2a 2a  that occurred.**
47b70 20 62 65 66 6f 72 65 20 74 68 65 20 5b 42 4c 4f   before the [BLO
47b80 42 20 68 61 6e 64 6c 65 5d 20 65 78 70 69 72 65  B handle] expire
47b90 64 20 61 72 65 20 6e 6f 74 20 72 6f 6c 6c 65 64  d are not rolled
47ba0 20 62 61 63 6b 20 62 79 20 74 68 65 0a 2a 2a 20   back by the.** 
47bb0 65 78 70 69 72 61 74 69 6f 6e 20 6f 66 20 74 68  expiration of th
47bc0 65 20 68 61 6e 64 6c 65 2c 20 74 68 6f 75 67 68  e handle, though
47bd0 20 6f 66 20 63 6f 75 72 73 65 20 74 68 6f 73 65   of course those
47be0 20 63 68 61 6e 67 65 73 20 6d 69 67 68 74 0a 2a   changes might.*
47bf0 2a 20 68 61 76 65 20 62 65 65 6e 20 6f 76 65 72  * have been over
47c00 77 72 69 74 74 65 6e 20 62 79 20 74 68 65 20 73  written by the s
47c10 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20 65 78  tatement that ex
47c20 70 69 72 65 64 20 74 68 65 20 42 4c 4f 42 20 68  pired the BLOB h
47c30 61 6e 64 6c 65 0a 2a 2a 20 6f 72 20 62 79 20 6f  andle.** or by o
47c40 74 68 65 72 20 69 6e 64 65 70 65 6e 64 65 6e 74  ther independent
47c50 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
47c60 2a 2a 20 4f 6e 20 73 75 63 63 65 73 73 2c 20 53  ** On success, S
47c70 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
47c80 72 6e 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77 69  rned..** Otherwi
47c90 73 65 2c 20 61 6e 20 20 5b 65 72 72 6f 72 20 63  se, an  [error c
47ca0 6f 64 65 5d 20 6f 72 20 61 6e 20 5b 65 78 74 65  ode] or an [exte
47cb0 6e 64 65 64 20 65 72 72 6f 72 20 63 6f 64 65 5d  nded error code]
47cc0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
47cd0 0a 2a 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a  .** INVARIANTS:.
47ce0 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 33 7d 20 41  **.** {H17873} A
47cf0 20 73 75 63 63 65 73 73 66 75 6c 20 69 6e 76 6f   successful invo
47d00 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69 74  cation of [sqlit
47d10 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c  e3_blob_write(P,
47d20 5a 2c 4e 2c 58 29 5d 0a 2a 2a 20 20 20 20 20 20  Z,N,X)].**      
47d30 20 20 20 20 73 68 61 6c 6c 20 77 72 69 74 65 20      shall write 
47d40 4e 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  N bytes of data 
47d50 66 72 6f 6d 20 62 75 66 66 65 72 20 5a 20 69 6e  from buffer Z in
47d60 74 6f 20 74 68 65 20 42 4c 4f 42 20 0a 2a 2a 20  to the BLOB .** 
47d70 20 20 20 20 20 20 20 20 20 72 65 66 65 72 65 6e           referen
47d80 63 65 64 20 62 79 20 5b 42 4c 4f 42 20 68 61 6e  ced by [BLOB han
47d90 64 6c 65 5d 20 50 20 62 65 67 69 6e 6e 69 6e 67  dle] P beginning
47da0 20 61 74 20 6f 66 66 73 65 74 20 58 20 69 6e 74   at offset X int
47db0 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68  o.**          th
47dc0 65 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48  e BLOB..**.** {H
47dd0 31 37 38 37 34 7d 20 49 6e 20 74 68 65 20 61 62  17874} In the ab
47de0 73 65 6e 63 65 20 6f 66 20 6f 74 68 65 72 20 6f  sence of other o
47df0 76 65 72 72 69 64 64 69 6e 67 20 63 68 61 6e 67  verridding chang
47e00 65 73 2c 20 74 68 65 20 63 68 61 6e 67 65 73 0a  es, the changes.
47e10 2a 2a 20 20 20 20 20 20 20 20 20 20 77 72 69 74  **          writ
47e20 74 65 6e 20 74 6f 20 61 20 42 4c 4f 42 20 62 79  ten to a BLOB by
47e30 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
47e40 72 69 74 65 28 29 5d 20 73 68 61 6c 6c 0a 2a 2a  rite()] shall.**
47e50 20 20 20 20 20 20 20 20 20 20 72 65 6d 61 69 6e            remain
47e60 20 69 6e 20 65 66 66 65 63 74 20 61 66 74 65 72   in effect after
47e70 20 74 68 65 20 61 73 73 6f 63 69 61 74 65 64 20   the associated 
47e80 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 65 78  [BLOB handle] ex
47e90 70 69 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31  pires..**.** {H1
47ea0 37 38 37 35 7d 20 49 66 20 74 68 65 20 5b 42 4c  7875} If the [BL
47eb0 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 77 61 73  OB handle] P was
47ec0 20 6f 70 65 6e 65 64 20 66 6f 72 20 72 65 61 64   opened for read
47ed0 69 6e 67 20 6f 6e 6c 79 20 74 68 65 6e 0a 2a 2a  ing only then.**
47ee0 20 20 20 20 20 20 20 20 20 20 61 6e 20 69 6e 76            an inv
47ef0 6f 63 61 74 69 6f 6e 20 6f 66 20 5b 73 71 6c 69  ocation of [sqli
47f00 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50  te3_blob_write(P
47f10 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 20 6c  ,Z,N,X)] shall l
47f20 65 61 76 65 0a 2a 2a 20 20 20 20 20 20 20 20 20  eave.**         
47f30 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 64 20   the referenced 
47f40 42 4c 4f 42 20 75 6e 63 68 61 6e 67 65 64 20 61  BLOB unchanged a
47f50 6e 64 20 72 65 74 75 72 6e 20 5b 53 51 4c 49 54  nd return [SQLIT
47f60 45 5f 52 45 41 44 4f 4e 4c 59 5d 2e 0a 2a 2a 0a  E_READONLY]..**.
47f70 2a 2a 20 7b 48 31 37 38 37 36 7d 20 49 66 20 74  ** {H17876} If t
47f80 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 42  he size of the B
47f90 4c 4f 42 20 72 65 66 65 72 65 6e 63 65 64 20 62  LOB referenced b
47fa0 79 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20  y [BLOB handle] 
47fb0 50 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  P is.**         
47fc0 20 6c 65 73 73 20 74 68 61 6e 20 4e 2b 58 20 62   less than N+X b
47fd0 79 74 65 73 20 74 68 65 6e 20 5b 73 71 6c 69 74  ytes then [sqlit
47fe0 65 33 5f 62 6c 6f 62 5f 77 72 69 74 65 28 50 2c  e3_blob_write(P,
47ff0 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c 0a 2a 2a  Z,N,X)] shall.**
48000 20 20 20 20 20 20 20 20 20 20 6c 65 61 76 65 20            leave 
48010 74 68 65 20 42 4c 4f 42 20 75 6e 63 68 61 6e 67  the BLOB unchang
48020 65 64 20 61 6e 64 20 72 65 74 75 72 6e 20 5b 53  ed and return [S
48030 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a 2a 2a  QLITE_ERROR]..**
48040 0a 2a 2a 20 7b 48 31 37 38 37 37 7d 20 49 66 20  .** {H17877} If 
48050 74 68 65 20 5b 42 4c 4f 42 20 68 61 6e 64 6c 65  the [BLOB handle
48060 5d 20 50 20 69 73 20 65 78 70 69 72 65 64 20 61  ] P is expired a
48070 6e 64 20 58 20 61 6e 64 20 4e 20 61 72 65 20 77  nd X and N are w
48080 69 74 68 69 6e 20 62 6f 75 6e 64 73 0a 2a 2a 20  ithin bounds.** 
48090 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 5b 73           then [s
480a0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64  qlite3_blob_read
480b0 28 50 2c 5a 2c 4e 2c 58 29 5d 20 73 68 61 6c 6c  (P,Z,N,X)] shall
480c0 20 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42 0a   leave the BLOB.
480d0 2a 2a 20 20 20 20 20 20 20 20 20 20 75 6e 63 68  **          unch
480e0 61 6e 67 65 64 20 61 6e 64 20 72 65 74 75 72 6e  anged and return
480f0 20 5b 53 51 4c 49 54 45 5f 41 42 4f 52 54 5d 2e   [SQLITE_ABORT].
48100 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38 37 39 7d 20  .**.** {H17879} 
48110 49 66 20 58 20 6f 72 20 4e 20 61 72 65 20 6c 65  If X or N are le
48120 73 73 20 74 68 61 6e 20 7a 65 72 6f 20 74 68 65  ss than zero the
48130 6e 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  n [sqlite3_blob_
48140 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d 0a  write(P,Z,N,X)].
48150 2a 2a 20 20 20 20 20 20 20 20 20 20 73 68 61 6c  **          shal
48160 6c 20 6c 65 61 76 65 20 74 68 65 20 42 4c 4f 42  l leave the BLOB
48170 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 5b   referenced by [
48180 42 4c 4f 42 20 68 61 6e 64 6c 65 5d 20 50 20 75  BLOB handle] P u
48190 6e 63 68 61 6e 67 65 64 0a 2a 2a 20 20 20 20 20  nchanged.**     
481a0 20 20 20 20 20 61 6e 64 20 72 65 74 75 72 6e 20       and return 
481b0 5b 53 51 4c 49 54 45 5f 45 52 52 4f 52 5d 2e 0a  [SQLITE_ERROR]..
481c0 2a 2a 0a 2a 2a 20 7b 48 31 37 38 38 32 7d 20 54  **.** {H17882} T
481d0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
481e0 5f 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d  _write(P,Z,N,X)]
481f0 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
48200 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20 20 20   return.**      
48210 20 20 20 20 5b 53 51 4c 49 54 45 5f 4f 4b 5d 20      [SQLITE_OK] 
48220 69 66 20 4e 20 62 79 74 65 73 20 77 68 65 72 65  if N bytes where
48230 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 77 72   successfully wr
48240 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 42  itten into the B
48250 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 38  LOB..**.** {H178
48260 38 35 7d 20 49 66 20 74 68 65 20 72 65 71 75 65  85} If the reque
48270 73 74 65 64 20 77 72 69 74 65 20 63 6f 75 6c 64  sted write could
48280 20 6e 6f 74 20 62 65 20 63 6f 6d 70 6c 65 74 65   not be complete
48290 64 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74  d,.**          t
482a0 68 65 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62  he [sqlite3_blob
482b0 5f 77 72 69 74 65 28 50 2c 5a 2c 4e 2c 58 29 5d  _write(P,Z,N,X)]
482c0 20 69 6e 74 65 72 66 61 63 65 20 73 68 61 6c 6c   interface shall
482d0 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 20 20   return an.**   
482e0 20 20 20 20 20 20 20 61 70 70 72 6f 70 72 69 61         appropria
482f0 74 65 20 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20  te [error code] 
48300 6f 72 20 5b 65 78 74 65 6e 64 65 64 20 65 72 72  or [extended err
48310 6f 72 20 63 6f 64 65 5d 2e 0a 2a 2a 0a 2a 2a 20  or code]..**.** 
48320 7b 48 31 37 38 38 38 7d 20 49 66 20 61 6e 20 65  {H17888} If an e
48330 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
48340 6e 67 20 65 76 61 6c 75 61 74 69 6f 6e 20 6f 66  ng evaluation of
48350 20 5b 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77   [sqlite3_blob_w
48360 72 69 74 65 28 44 2c 2e 2e 2e 29 5d 0a 2a 2a 20  rite(D,...)].** 
48370 20 20 20 20 20 20 20 20 20 74 68 65 6e 20 73 75           then su
48380 62 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74  bsequent calls t
48390 6f 20 5b 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  o [sqlite3_errco
483a0 64 65 28 44 29 5d 2c 0a 2a 2a 20 20 20 20 20 20  de(D)],.**      
483b0 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 65 78 74      [sqlite3_ext
483c0 65 6e 64 65 64 5f 65 72 72 63 6f 64 65 28 29 5d  ended_errcode()]
483d0 2c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 5b 73  ,.**          [s
483e0 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 44 29  qlite3_errmsg(D)
483f0 5d 2c 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f  ], and [sqlite3_
48400 65 72 72 6d 73 67 31 36 28 44 29 5d 20 73 68 61  errmsg16(D)] sha
48410 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 20 20 20  ll return.**    
48420 20 20 20 20 20 20 69 6e 66 6f 72 6d 61 74 69 6f        informatio
48430 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  n appropriate fo
48440 72 20 74 68 61 74 20 65 72 72 6f 72 2e 0a 2a 2f  r that error..*/
48450 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
48460 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69  sqlite3_blob_wri
48470 74 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20  te(sqlite3_blob 
48480 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  *, const void *z
48490 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66  , int n, int iOf
484a0 66 73 65 74 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41  fset);../*.** CA
484b0 50 49 33 52 45 46 3a 20 56 69 72 74 75 61 6c 20  PI3REF: Virtual 
484c0 46 69 6c 65 20 53 79 73 74 65 6d 20 4f 62 6a 65  File System Obje
484d0 63 74 73 20 7b 48 31 31 32 30 30 7d 20 3c 53 32  cts {H11200} <S2
484e0 30 31 30 30 3e 0a 2a 2a 0a 2a 2a 20 41 20 76 69  0100>.**.** A vi
484f0 72 74 75 61 6c 20 66 69 6c 65 73 79 73 74 65 6d  rtual filesystem
48500 20 28 56 46 53 29 20 69 73 20 61 6e 20 5b 73 71   (VFS) is an [sq
48510 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63  lite3_vfs] objec
48520 74 0a 2a 2a 20 74 68 61 74 20 53 51 4c 69 74 65  t.** that SQLite
48530 20 75 73 65 73 20 74 6f 20 69 6e 74 65 72 61 63   uses to interac
48540 74 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 75 6e  t.** with the un
48550 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69  derlying operati
48560 6e 67 20 73 79 73 74 65 6d 2e 20 20 4d 6f 73 74  ng system.  Most
48570 20 53 51 4c 69 74 65 20 62 75 69 6c 64 73 20 63   SQLite builds c
48580 6f 6d 65 20 77 69 74 68 20 61 0a 2a 2a 20 73 69  ome with a.** si
48590 6e 67 6c 65 20 64 65 66 61 75 6c 74 20 56 46 53  ngle default VFS
485a0 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
485b0 69 61 74 65 20 66 6f 72 20 74 68 65 20 68 6f 73  iate for the hos
485c0 74 20 63 6f 6d 70 75 74 65 72 2e 0a 2a 2a 20 4e  t computer..** N
485d0 65 77 20 56 46 53 65 73 20 63 61 6e 20 62 65 20  ew VFSes can be 
485e0 72 65 67 69 73 74 65 72 65 64 20 61 6e 64 20 65  registered and e
485f0 78 69 73 74 69 6e 67 20 56 46 53 65 73 20 63 61  xisting VFSes ca
48600 6e 20 62 65 20 75 6e 72 65 67 69 73 74 65 72 65  n be unregistere
48610 64 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  d..** The follow
48620 69 6e 67 20 69 6e 74 65 72 66 61 63 65 73 20 61  ing interfaces a
48630 72 65 20 70 72 6f 76 69 64 65 64 2e 0a 2a 2a 0a  re provided..**.
48640 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 76  ** The sqlite3_v
48650 66 73 5f 66 69 6e 64 28 29 20 69 6e 74 65 72 66  fs_find() interf
48660 61 63 65 20 72 65 74 75 72 6e 73 20 61 20 70 6f  ace returns a po
48670 69 6e 74 65 72 20 74 6f 20 61 20 56 46 53 20 67  inter to a VFS g
48680 69 76 65 6e 20 69 74 73 20 6e 61 6d 65 2e 0a 2a  iven its name..*
48690 2a 20 4e 61 6d 65 73 20 61 72 65 20 63 61 73 65  * Names are case
486a0 20 73 65 6e 73 69 74 69 76 65 2e 0a 2a 2a 20 4e   sensitive..** N
486b0 61 6d 65 73 20 61 72 65 20 7a 65 72 6f 2d 74 65  ames are zero-te
486c0 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 73  rminated UTF-8 s
486d0 74 72 69 6e 67 73 2e 0a 2a 2a 20 49 66 20 74 68  trings..** If th
486e0 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 2c  ere is no match,
486f0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20   a NULL pointer 
48700 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20  is returned..** 
48710 49 66 20 7a 56 66 73 4e 61 6d 65 20 69 73 20 4e  If zVfsName is N
48720 55 4c 4c 20 74 68 65 6e 20 74 68 65 20 64 65 66  ULL then the def
48730 61 75 6c 74 20 56 46 53 20 69 73 20 72 65 74 75  ault VFS is retu
48740 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65 77 20  rned..**.** New 
48750 56 46 53 65 73 20 61 72 65 20 72 65 67 69 73 74  VFSes are regist
48760 65 72 65 64 20 77 69 74 68 20 73 71 6c 69 74 65  ered with sqlite
48770 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 29  3_vfs_register()
48780 2e 0a 2a 2a 20 45 61 63 68 20 6e 65 77 20 56 46  ..** Each new VF
48790 53 20 62 65 63 6f 6d 65 73 20 74 68 65 20 64 65  S becomes the de
487a0 66 61 75 6c 74 20 56 46 53 20 69 66 20 74 68 65  fault VFS if the
487b0 20 6d 61 6b 65 44 66 6c 74 20 66 6c 61 67 20 69   makeDflt flag i
487c0 73 20 73 65 74 2e 0a 2a 2a 20 54 68 65 20 73 61  s set..** The sa
487d0 6d 65 20 56 46 53 20 63 61 6e 20 62 65 20 72 65  me VFS can be re
487e0 67 69 73 74 65 72 65 64 20 6d 75 6c 74 69 70 6c  gistered multipl
487f0 65 20 74 69 6d 65 73 20 77 69 74 68 6f 75 74 20  e times without 
48800 69 6e 6a 75 72 79 2e 0a 2a 2a 20 54 6f 20 6d 61  injury..** To ma
48810 6b 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 56  ke an existing V
48820 46 53 20 69 6e 74 6f 20 74 68 65 20 64 65 66 61  FS into the defa
48830 75 6c 74 20 56 46 53 2c 20 72 65 67 69 73 74 65  ult VFS, registe
48840 72 20 69 74 20 61 67 61 69 6e 0a 2a 2a 20 77 69  r it again.** wi
48850 74 68 20 74 68 65 20 6d 61 6b 65 44 66 6c 74 20  th the makeDflt 
48860 66 6c 61 67 20 73 65 74 2e 20 20 49 66 20 74 77  flag set.  If tw
48870 6f 20 64 69 66 66 65 72 65 6e 74 20 56 46 53 65  o different VFSe
48880 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 73 61  s with the.** sa
48890 6d 65 20 6e 61 6d 65 20 61 72 65 20 72 65 67 69  me name are regi
488a0 73 74 65 72 65 64 2c 20 74 68 65 20 62 65 68 61  stered, the beha
488b0 76 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65  vior is undefine
488c0 64 2e 20 20 49 66 20 61 0a 2a 2a 20 56 46 53 20  d.  If a.** VFS 
488d0 69 73 20 72 65 67 69 73 74 65 72 65 64 20 77 69  is registered wi
488e0 74 68 20 61 20 6e 61 6d 65 20 74 68 61 74 20 69  th a name that i
488f0 73 20 4e 55 4c 4c 20 6f 72 20 61 6e 20 65 6d 70  s NULL or an emp
48900 74 79 20 73 74 72 69 6e 67 2c 0a 2a 2a 20 74 68  ty string,.** th
48910 65 6e 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  en the behavior 
48920 69 73 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a  is undefined..**
48930 0a 2a 2a 20 55 6e 72 65 67 69 73 74 65 72 20 61  .** Unregister a
48940 20 56 46 53 20 77 69 74 68 20 74 68 65 20 73 71   VFS with the sq
48950 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67 69  lite3_vfs_unregi
48960 73 74 65 72 28 29 20 69 6e 74 65 72 66 61 63 65  ster() interface
48970 2e 0a 2a 2a 20 49 66 20 74 68 65 20 64 65 66 61  ..** If the defa
48980 75 6c 74 20 56 46 53 20 69 73 20 75 6e 72 65 67  ult VFS is unreg
48990 69 73 74 65 72 65 64 2c 20 61 6e 6f 74 68 65 72  istered, another
489a0 20 56 46 53 20 69 73 20 63 68 6f 73 65 6e 20 61   VFS is chosen a
489b0 73 0a 2a 2a 20 74 68 65 20 64 65 66 61 75 6c 74  s.** the default
489c0 2e 20 20 54 68 65 20 63 68 6f 69 63 65 20 66 6f  .  The choice fo
489d0 72 20 74 68 65 20 6e 65 77 20 56 46 53 20 69 73  r the new VFS is
489e0 20 61 72 62 69 74 72 61 72 79 2e 0a 2a 2a 0a 2a   arbitrary..**.*
489f0 2a 20 49 4e 56 41 52 49 41 4e 54 53 3a 0a 2a 2a  * INVARIANTS:.**
48a00 0a 2a 2a 20 7b 48 31 31 32 30 33 7d 20 54 68 65  .** {H11203} The
48a10 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69   [sqlite3_vfs_fi
48a20 6e 64 28 4e 29 5d 20 69 6e 74 65 72 66 61 63 65  nd(N)] interface
48a30 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74   returns a point
48a40 65 72 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  er to the.**    
48a50 20 20 20 20 20 20 72 65 67 69 73 74 65 72 65 64        registered
48a60 20 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f   [sqlite3_vfs] o
48a70 62 6a 65 63 74 20 77 68 6f 73 65 20 6e 61 6d 65  bject whose name
48a80 20 65 78 61 63 74 6c 79 20 6d 61 74 63 68 65 73   exactly matches
48a90 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 74 68 65  .**          the
48aa0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
48ab0 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 4e 2c   UTF-8 string N,
48ac0 20 6f 72 20 69 74 20 72 65 74 75 72 6e 73 20 4e   or it returns N
48ad0 55 4c 4c 20 69 66 0a 2a 2a 20 20 20 20 20 20 20  ULL if.**       
48ae0 20 20 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d     there is no m
48af0 61 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  atch..**.** {H11
48b00 32 30 36 7d 20 49 66 20 74 68 65 20 4e 20 70 61  206} If the N pa
48b10 72 61 6d 65 74 65 72 20 74 6f 20 5b 73 71 6c 69  rameter to [sqli
48b20 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 4e 29 5d  te3_vfs_find(N)]
48b30 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a   is NULL then.**
48b40 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66 75            the fu
48b50 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61  nction returns a
48b60 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
48b70 64 65 66 61 75 6c 74 20 5b 73 71 6c 69 74 65 33  default [sqlite3
48b80 5f 76 66 73 5d 0a 2a 2a 20 20 20 20 20 20 20 20  _vfs].**        
48b90 20 20 6f 62 6a 65 63 74 20 69 66 20 74 68 65 72    object if ther
48ba0 65 20 69 73 20 6f 6e 65 2c 20 6f 72 20 4e 55 4c  e is one, or NUL
48bb0 4c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  L if there is no
48bc0 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20 20 20 20   default.**     
48bd0 20 20 20 20 20 5b 73 71 6c 69 74 65 33 5f 76 66       [sqlite3_vf
48be0 73 5d 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a 2a  s] object..**.**
48bf0 20 7b 48 31 31 32 30 39 7d 20 54 68 65 20 5b 73   {H11209} The [s
48c00 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73  qlite3_vfs_regis
48c10 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66  ter(P,F)] interf
48c20 61 63 65 20 72 65 67 69 73 74 65 72 73 20 74 68  ace registers th
48c30 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65  e.**          we
48c40 6c 6c 2d 66 6f 72 6d 65 64 20 5b 73 71 6c 69 74  ll-formed [sqlit
48c50 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20 50  e3_vfs] object P
48c60 20 75 73 69 6e 67 20 74 68 65 20 6e 61 6d 65 20   using the name 
48c70 67 69 76 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20  given.**        
48c80 20 20 62 79 20 74 68 65 20 7a 4e 61 6d 65 20 66    by the zName f
48c90 69 65 6c 64 20 6f 66 20 74 68 65 20 6f 62 6a 65  ield of the obje
48ca0 63 74 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31 32 31  ct..**.** {H1121
48cb0 32 7d 20 55 73 69 6e 67 20 74 68 65 20 5b 73 71  2} Using the [sq
48cc0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
48cd0 65 72 28 50 2c 46 29 5d 20 69 6e 74 65 72 66 61  er(P,F)] interfa
48ce0 63 65 20 74 6f 20 72 65 67 69 73 74 65 72 0a 2a  ce to register.*
48cf0 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73  *          the s
48d00 61 6d 65 20 5b 73 71 6c 69 74 65 33 5f 76 66 73  ame [sqlite3_vfs
48d10 5d 20 6f 62 6a 65 63 74 20 6d 75 6c 74 69 70 6c  ] object multipl
48d20 65 20 74 69 6d 65 73 20 69 73 20 61 20 68 61 72  e times is a har
48d30 6d 6c 65 73 73 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a  mless no-op..**.
48d40 2a 2a 20 7b 48 31 31 32 31 35 7d 20 54 68 65 20  ** {H11215} The 
48d50 5b 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67  [sqlite3_vfs_reg
48d60 69 73 74 65 72 28 50 2c 46 29 5d 20 69 6e 74 65  ister(P,F)] inte
48d70 72 66 61 63 65 20 6d 61 6b 65 73 20 74 68 65 20  rface makes the 
48d80 5b 73 71 6c 69 74 65 33 5f 76 66 73 5d 0a 2a 2a  [sqlite3_vfs].**
48d90 20 20 20 20 20 20 20 20 20 20 6f 62 6a 65 63 74            object
48da0 20 50 20 74 68 65 20 64 65 66 61 75 6c 74 20 5b   P the default [
48db0 73 71 6c 69 74 65 33 5f 76 66 73 5d 20 6f 62 6a  sqlite3_vfs] obj
48dc0 65 63 74 20 69 66 20 46 20 69 73 20 6e 6f 6e 2d  ect if F is non-
48dd0 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 31  zero..**.** {H11
48de0 32 31 38 7d 20 54 68 65 20 5b 73 71 6c 69 74 65  218} The [sqlite
48df0 33 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  3_vfs_unregister
48e00 28 50 29 5d 20 69 6e 74 65 72 66 61 63 65 20 75  (P)] interface u
48e10 6e 72 65 67 69 73 74 65 72 73 20 74 68 65 0a 2a  nregisters the.*
48e20 2a 20 20 20 20 20 20 20 20 20 20 5b 73 71 6c 69  *          [sqli
48e30 74 65 33 5f 76 66 73 5d 20 6f 62 6a 65 63 74 20  te3_vfs] object 
48e40 50 20 73 6f 20 74 68 61 74 20 69 74 20 69 73 20  P so that it is 
48e50 6e 6f 20 6c 6f 6e 67 65 72 20 72 65 74 75 72 6e  no longer return
48e60 65 64 20 62 79 0a 2a 2a 20 20 20 20 20 20 20 20  ed by.**        
48e70 20 20 73 75 62 73 65 71 75 65 6e 74 20 63 61 6c    subsequent cal
48e80 6c 73 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 76  ls to [sqlite3_v
48e90 66 73 5f 66 69 6e 64 28 29 5d 2e 0a 2a 2f 0a 53  fs_find()]..*/.S
48ea0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
48eb0 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 5f 76  3_vfs *sqlite3_v
48ec0 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74 20 63 68  fs_find(const ch
48ed0 61 72 20 2a 7a 56 66 73 4e 61 6d 65 29 3b 0a 53  ar *zVfsName);.S
48ee0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
48ef0 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69 73 74  lite3_vfs_regist
48f00 65 72 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 2c  er(sqlite3_vfs*,
48f10 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29 3b 0a   int makeDflt);.
48f20 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
48f30 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72 65 67  qlite3_vfs_unreg
48f40 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f 76 66  ister(sqlite3_vf
48f50 73 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  s*);../*.** CAPI
48f60 33 52 45 46 3a 20 4d 75 74 65 78 65 73 20 7b 48  3REF: Mutexes {H
48f70 31 37 30 30 30 7d 20 3c 53 32 30 30 30 30 3e 0a  17000} <S20000>.
48f80 2a 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65  **.** The SQLite
48f90 20 63 6f 72 65 20 75 73 65 73 20 74 68 65 73 65   core uses these
48fa0 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 74 68   routines for th
48fb0 72 65 61 64 0a 2a 2a 20 73 79 6e 63 68 72 6f 6e  read.** synchron
48fc0 69 7a 61 74 69 6f 6e 2e 20 54 68 6f 75 67 68 20  ization. Though 
48fd0 74 68 65 79 20 61 72 65 20 69 6e 74 65 6e 64 65  they are intende
48fe0 64 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a  d for internal.*
48ff0 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65 2c  * use by SQLite,
49000 20 63 6f 64 65 20 74 68 61 74 20 6c 69 6e 6b 73   code that links
49010 20 61 67 61 69 6e 73 74 20 53 51 4c 69 74 65 20   against SQLite 
49020 69 73 0a 2a 2a 20 70 65 72 6d 69 74 74 65 64 20  is.** permitted 
49030 74 6f 20 75 73 65 20 61 6e 79 20 6f 66 20 74 68  to use any of th
49040 65 73 65 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a  ese routines..**
49050 0a 2a 2a 20 54 68 65 20 53 51 4c 69 74 65 20 73  .** The SQLite s
49060 6f 75 72 63 65 20 63 6f 64 65 20 63 6f 6e 74 61  ource code conta
49070 69 6e 73 20 6d 75 6c 74 69 70 6c 65 20 69 6d 70  ins multiple imp
49080 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
49090 6f 66 20 74 68 65 73 65 20 6d 75 74 65 78 20 72  of these mutex r
490a0 6f 75 74 69 6e 65 73 2e 20 20 41 6e 20 61 70 70  outines.  An app
490b0 72 6f 70 72 69 61 74 65 20 69 6d 70 6c 65 6d 65  ropriate impleme
490c0 6e 74 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 73 65  ntation.** is se
490d0 6c 65 63 74 65 64 20 61 75 74 6f 6d 61 74 69 63  lected automatic
490e0 61 6c 6c 79 20 61 74 20 63 6f 6d 70 69 6c 65 2d  ally at compile-
490f0 74 69 6d 65 2e 20 20 54 68 65 20 66 6f 6c 6c 6f  time.  The follo
49100 77 69 6e 67 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e  wing.** implemen
49110 74 61 74 69 6f 6e 73 20 61 72 65 20 61 76 61 69  tations are avai
49120 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 53 51 4c  lable in the SQL
49130 69 74 65 20 63 6f 72 65 3a 0a 2a 2a 0a 2a 2a 20  ite core:.**.** 
49140 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 20 53  <ul>.** <li>   S
49150 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53 32 0a  QLITE_MUTEX_OS2.
49160 2a 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45  ** <li>   SQLITE
49170 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 0a 2a  _MUTEX_PTHREAD.*
49180 2a 20 3c 6c 69 3e 20 20 20 53 51 4c 49 54 45 5f  * <li>   SQLITE_
49190 4d 55 54 45 58 5f 57 33 32 0a 2a 2a 20 3c 6c 69  MUTEX_W32.** <li
491a0 3e 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  >   SQLITE_MUTEX
491b0 5f 4e 4f 4f 50 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a  _NOOP.** </ul>.*
491c0 2a 0a 2a 2a 20 54 68 65 20 53 51 4c 49 54 45 5f  *.** The SQLITE_
491d0 4d 55 54 45 58 5f 4e 4f 4f 50 20 69 6d 70 6c 65  MUTEX_NOOP imple
491e0 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 20 73  mentation is a s
491f0 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73 0a 2a  et of routines.*
49200 2a 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 72  * that does no r
49210 65 61 6c 20 6c 6f 63 6b 69 6e 67 20 61 6e 64 20  eal locking and 
49220 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20 66  is appropriate f
49230 6f 72 20 75 73 65 20 69 6e 0a 2a 2a 20 61 20 73  or use in.** a s
49240 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61  ingle-threaded a
49250 70 70 6c 69 63 61 74 69 6f 6e 2e 20 20 54 68 65  pplication.  The
49260 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 53   SQLITE_MUTEX_OS
49270 32 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54  2,.** SQLITE_MUT
49280 45 58 5f 50 54 48 52 45 41 44 2c 20 61 6e 64 20  EX_PTHREAD, and 
49290 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32  SQLITE_MUTEX_W32
492a0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
492b0 0a 2a 2a 20 61 72 65 20 61 70 70 72 6f 70 72 69  .** are appropri
492c0 61 74 65 20 66 6f 72 20 75 73 65 20 6f 6e 20 4f  ate for use on O
492d0 53 2f 32 2c 20 55 6e 69 78 2c 20 61 6e 64 20 57  S/2, Unix, and W
492e0 69 6e 64 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  indows..**.** If
492f0 20 53 51 4c 69 74 65 20 69 73 20 63 6f 6d 70 69   SQLite is compi
49300 6c 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c  led with the SQL
49310 49 54 45 5f 4d 55 54 45 58 5f 41 50 50 44 45 46  ITE_MUTEX_APPDEF
49320 20 70 72 65 70 72 6f 63 65 73 73 6f 72 0a 2a 2a   preprocessor.**
49330 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20 28   macro defined (
49340 77 69 74 68 20 22 2d 44 53 51 4c 49 54 45 5f 4d  with "-DSQLITE_M
49350 55 54 45 58 5f 41 50 50 44 45 46 3d 31 22 29 2c  UTEX_APPDEF=1"),
49360 20 74 68 65 6e 20 6e 6f 20 6d 75 74 65 78 0a 2a   then no mutex.*
49370 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * implementation
49380 20 69 73 20 69 6e 63 6c 75 64 65 64 20 77 69 74   is included wit
49390 68 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 49  h the library. I
493a0 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65 0a  n this case the.
493b0 2a 2a 20 61 70 70 6c 69 63 61 74 69 6f 6e 20 6d  ** application m
493c0 75 73 74 20 73 75 70 70 6c 79 20 61 20 63 75 73  ust supply a cus
493d0 74 6f 6d 20 6d 75 74 65 78 20 69 6d 70 6c 65 6d  tom mutex implem
493e0 65 6e 74 61 74 69 6f 6e 20 75 73 69 6e 67 20 74  entation using t
493f0 68 65 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f  he.** [SQLITE_CO
49400 4e 46 49 47 5f 4d 55 54 45 58 5d 20 6f 70 74 69  NFIG_MUTEX] opti
49410 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  on of the sqlite
49420 33 5f 63 6f 6e 66 69 67 28 29 20 66 75 6e 63 74  3_config() funct
49430 69 6f 6e 0a 2a 2a 20 62 65 66 6f 72 65 20 63 61  ion.** before ca
49440 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f 69 6e  lling sqlite3_in
49450 69 74 69 61 6c 69 7a 65 28 29 20 6f 72 20 61 6e  itialize() or an
49460 79 20 6f 74 68 65 72 20 70 75 62 6c 69 63 20 73  y other public s
49470 71 6c 69 74 65 33 5f 0a 2a 2a 20 66 75 6e 63 74  qlite3_.** funct
49480 69 6f 6e 20 74 68 61 74 20 63 61 6c 6c 73 20 73  ion that calls s
49490 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
494a0 65 28 29 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  e()..**.** {H170
494b0 31 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33 5f  11} The sqlite3_
494c0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
494d0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
494e0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
494f0 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
49500 6e 74 65 72 20 74 6f 20 69 74 2e 20 7b 48 31 37  nter to it. {H17
49510 30 31 32 7d 20 49 66 20 69 74 20 72 65 74 75 72  012} If it retur
49520 6e 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20  ns NULL.** that 
49530 6d 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74  means that a mut
49540 65 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ex could not be 
49550 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 48 31 37 30  allocated. {H170
49560 31 33 7d 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  13} SQLite.** wi
49570 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74  ll unwind its st
49580 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ack and return a
49590 6e 20 65 72 72 6f 72 2e 20 7b 48 31 37 30 31 34  n error. {H17014
495a0 7d 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a  } The argument.*
495b0 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74  * to sqlite3_mut
495c0 65 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e  ex_alloc() is on
495d0 65 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67  e of these integ
495e0 65 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a  er constants:.**
495f0 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e  .** <ul>.** <li>
49600 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46    SQLITE_MUTEX_F
49610 41 53 54 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  AST.** <li>  SQL
49620 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
49630 49 56 45 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  IVE.** <li>  SQL
49640 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
49650 5f 4d 41 53 54 45 52 0a 2a 2a 20 3c 6c 69 3e 20  _MASTER.** <li> 
49660 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54   SQLITE_MUTEX_ST
49670 41 54 49 43 5f 4d 45 4d 0a 2a 2a 20 3c 6c 69 3e  ATIC_MEM.** <li>
49680 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
49690 54 41 54 49 43 5f 4d 45 4d 32 0a 2a 2a 20 3c 6c  TATIC_MEM2.** <l
496a0 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  i>  SQLITE_MUTEX
496b0 5f 53 54 41 54 49 43 5f 50 52 4e 47 0a 2a 2a 20  _STATIC_PRNG.** 
496c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
496d0 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 0a 2a 2a  EX_STATIC_LRU.**
496e0 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
496f0 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a  TEX_STATIC_LRU2.
49700 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 7b  ** </ul>.**.** {
49710 48 31 37 30 31 35 7d 20 54 68 65 20 66 69 72 73  H17015} The firs
49720 74 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20  t two constants 
49730 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75  cause sqlite3_mu
49740 74 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63  tex_alloc() to c
49750 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d  reate.** a new m
49760 75 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d  utex.  The new m
49770 75 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76  utex is recursiv
49780 65 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55  e when SQLITE_MU
49790 54 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a  TEX_RECURSIVE.**
497a0 20 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74   is used but not
497b0 20 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20   necessarily so 
497c0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
497d0 58 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 20  X_FAST is used. 
497e0 7b 45 4e 44 7d 0a 2a 2a 20 54 68 65 20 6d 75 74  {END}.** The mut
497f0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
49800 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
49810 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
49820 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
49830 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
49840 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
49850 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
49860 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
49870 77 61 6e 74 20 74 6f 2e 20 20 7b 48 31 37 30 31  want to.  {H1701
49880 36 7d 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  6} But SQLite wi
49890 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
498a0 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
498b0 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
498c0 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
498d0 65 64 73 20 6f 6e 65 2e 20 20 7b 45 4e 44 7d 20  eds one.  {END} 
498e0 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e 2d  If a faster non-
498f0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 0a  recursive mutex.
49900 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
49910 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 6f  n is available o
49920 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74 66  n the host platf
49930 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20 73  orm, the mutex s
49940 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67 68  ubsystem.** migh
49950 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61 20  t return such a 
49960 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e 73  mutex in respons
49970 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54 45  e to SQLITE_MUTE
49980 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 7b 48  X_FAST..**.** {H
49990 31 37 30 31 37 7d 20 54 68 65 20 6f 74 68 65 72  17017} The other
499a0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74   allowed paramet
499b0 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ers to sqlite3_m
499c0 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63  utex_alloc() eac
499d0 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f  h return.** a po
499e0 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69  inter to a stati
499f0 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75  c preexisting mu
49a00 74 65 78 2e 20 7b 45 4e 44 7d 20 20 46 6f 75 72  tex. {END}  Four
49a10 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73 20   static mutexes 
49a20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62 79 20 74  are.** used by t
49a30 68 65 20 63 75 72 72 65 6e 74 20 76 65 72 73 69  he current versi
49a40 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e 20 20 46  on of SQLite.  F
49a50 75 74 75 72 65 20 76 65 72 73 69 6f 6e 73 20 6f  uture versions o
49a60 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d 61 79 20  f SQLite.** may 
49a70 61 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20 73  add additional s
49a80 74 61 74 69 63 20 6d 75 74 65 78 65 73 2e 20 20  tatic mutexes.  
49a90 53 74 61 74 69 63 20 6d 75 74 65 78 65 73 20 61  Static mutexes a
49aa0 72 65 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a  re for internal.
49ab0 2a 2a 20 75 73 65 20 62 79 20 53 51 4c 69 74 65  ** use by SQLite
49ac0 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69 63 61 74   only.  Applicat
49ad0 69 6f 6e 73 20 74 68 61 74 20 75 73 65 20 53 51  ions that use SQ
49ae0 4c 69 74 65 20 6d 75 74 65 78 65 73 20 73 68 6f  Lite mutexes sho
49af0 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e 6c 79 20  uld.** use only 
49b00 74 68 65 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  the dynamic mute
49b10 78 65 73 20 72 65 74 75 72 6e 65 64 20 62 79 20  xes returned by 
49b20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
49b30 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d  T or.** SQLITE_M
49b40 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 2e 0a  UTEX_RECURSIVE..
49b50 2a 2a 0a 2a 2a 20 7b 48 31 37 30 31 38 7d 20 4e  **.** {H17018} N
49b60 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e 65 20  ote that if one 
49b70 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  of the dynamic m
49b80 75 74 65 78 20 70 61 72 61 6d 65 74 65 72 73 20  utex parameters 
49b90 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41  (SQLITE_MUTEX_FA
49ba0 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f  ST.** or SQLITE_
49bb0 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
49bc0 20 69 73 20 75 73 65 64 20 74 68 65 6e 20 73 71   is used then sq
49bd0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
49be0 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61  c().** returns a
49bf0 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74 65 78   different mutex
49c00 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 2e 20   on every call. 
49c10 20 7b 48 31 37 30 33 34 7d 20 42 75 74 20 66 6f   {H17034} But fo
49c20 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a 20  r the static.** 
49c30 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68 65  mutex types, the
49c40 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20 72   same mutex is r
49c50 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72 79  eturned on every
49c60 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a 2a   call that has.*
49c70 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65 20  * the same type 
49c80 6e 75 6d 62 65 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48  number..**.** {H
49c90 31 37 30 31 39 7d 20 54 68 65 20 73 71 6c 69 74  17019} The sqlit
49ca0 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 29 20  e3_mutex_free() 
49cb0 72 6f 75 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61  routine dealloca
49cc0 74 65 73 20 61 20 70 72 65 76 69 6f 75 73 6c 79  tes a previously
49cd0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 64 79  .** allocated dy
49ce0 6e 61 6d 69 63 20 6d 75 74 65 78 2e 20 7b 48 31  namic mutex. {H1
49cf0 37 30 32 30 7d 20 53 51 4c 69 74 65 20 69 73 20  7020} SQLite is 
49d00 63 61 72 65 66 75 6c 20 74 6f 20 64 65 61 6c 6c  careful to deall
49d10 6f 63 61 74 65 20 65 76 65 72 79 0a 2a 2a 20 64  ocate every.** d
49d20 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 74 68 61  ynamic mutex tha
49d30 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e 20  t it allocates. 
49d40 7b 41 31 37 30 32 31 7d 20 54 68 65 20 64 79 6e  {A17021} The dyn
49d50 61 6d 69 63 20 6d 75 74 65 78 65 73 20 6d 75 73  amic mutexes mus
49d60 74 20 6e 6f 74 20 62 65 20 69 6e 0a 2a 2a 20 75  t not be in.** u
49d70 73 65 20 77 68 65 6e 20 74 68 65 79 20 61 72 65  se when they are
49d80 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 20 7b 41   deallocated. {A
49d90 31 37 30 32 32 7d 20 41 74 74 65 6d 70 74 69 6e  17022} Attemptin
49da0 67 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  g to deallocate 
49db0 61 20 73 74 61 74 69 63 0a 2a 2a 20 6d 75 74 65  a static.** mute
49dc0 78 20 72 65 73 75 6c 74 73 20 69 6e 20 75 6e 64  x results in und
49dd0 65 66 69 6e 65 64 20 62 65 68 61 76 69 6f 72 2e  efined behavior.
49de0 20 7b 48 31 37 30 32 33 7d 20 53 51 4c 69 74 65   {H17023} SQLite
49df0 20 6e 65 76 65 72 20 64 65 61 6c 6c 6f 63 61 74   never deallocat
49e00 65 73 0a 2a 2a 20 61 20 73 74 61 74 69 63 20 6d  es.** a static m
49e10 75 74 65 78 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a 2a  utex. {END}.**.*
49e20 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
49e30 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
49e40 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
49e50 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
49e60 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
49e70 20 61 20 6d 75 74 65 78 2e 20 7b 48 31 37 30 32   a mutex. {H1702
49e80 34 7d 20 49 66 20 61 6e 6f 74 68 65 72 20 74 68  4} If another th
49e90 72 65 61 64 20 69 73 20 61 6c 72 65 61 64 79 20  read is already 
49ea0 77 69 74 68 69 6e 20 74 68 65 20 6d 75 74 65 78  within the mutex
49eb0 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 75 74  ,.** sqlite3_mut
49ec0 65 78 5f 65 6e 74 65 72 28 29 20 77 69 6c 6c 20  ex_enter() will 
49ed0 62 6c 6f 63 6b 20 61 6e 64 20 73 71 6c 69 74 65  block and sqlite
49ee0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 77 69  3_mutex_try() wi
49ef0 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c  ll return.** SQL
49f00 49 54 45 5f 42 55 53 59 2e 20 7b 48 31 37 30 32  ITE_BUSY. {H1702
49f10 35 7d 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f  5}  The sqlite3_
49f20 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65  mutex_try() inte
49f30 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 5b 53  rface returns [S
49f40 51 4c 49 54 45 5f 4f 4b 5d 0a 2a 2a 20 75 70 6f  QLITE_OK].** upo
49f50 6e 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74  n successful ent
49f60 72 79 2e 20 20 7b 48 31 37 30 32 36 7d 20 4d 75  ry.  {H17026} Mu
49f70 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
49f80 69 6e 67 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55  ing.** SQLITE_MU
49f90 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63 61  TEX_RECURSIVE ca
49fa0 6e 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  n be entered mul
49fb0 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
49fc0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 0a  he same thread..
49fd0 2a 2a 20 7b 48 31 37 30 32 37 7d 20 49 6e 20 73  ** {H17027} In s
49fe0 75 63 68 20 63 61 73 65 73 20 74 68 65 2c 0a 2a  uch cases the,.*
49ff0 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20  * mutex must be 
4a000 65 78 69 74 65 64 20 61 6e 20 65 71 75 61 6c 20  exited an equal 
4a010 6e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20  number of times 
4a020 62 65 66 6f 72 65 20 61 6e 6f 74 68 65 72 20 74  before another t
4a030 68 72 65 61 64 0a 2a 2a 20 63 61 6e 20 65 6e 74  hread.** can ent
4a040 65 72 2e 20 20 7b 41 31 37 30 32 38 7d 20 49 66  er.  {A17028} If
4a050 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
4a060 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
4a070 61 6e 79 20 6f 74 68 65 72 0a 2a 2a 20 6b 69 6e  any other.** kin
4a080 64 20 6f 66 20 6d 75 74 65 78 20 6d 6f 72 65 20  d of mutex more 
4a090 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
4a0a0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
4a0b0 69 6e 65 64 2e 0a 2a 2a 20 7b 48 31 37 30 32 39  ined..** {H17029
4a0c0 7d 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 65  } SQLite will ne
4a0d0 76 65 72 20 65 78 68 69 62 69 74 0a 2a 2a 20 73  ver exhibit.** s
4a0e0 75 63 68 20 62 65 68 61 76 69 6f 72 20 69 6e 20  uch behavior in 
4a0f0 69 74 73 20 6f 77 6e 20 75 73 65 20 6f 66 20 6d  its own use of m
4a100 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 6f  utexes..**.** So
4a110 6d 65 20 73 79 73 74 65 6d 73 20 28 66 6f 72 20  me systems (for 
4a120 65 78 61 6d 70 6c 65 2c 20 57 69 6e 64 6f 77 73  example, Windows
4a130 20 39 35 29 20 64 6f 20 6e 6f 74 20 73 75 70 70   95) do not supp
4a140 6f 72 74 20 74 68 65 20 6f 70 65 72 61 74 69 6f  ort the operatio
4a150 6e 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 65 64  n.** implemented
4a160 20 62 79 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   by sqlite3_mute
4a170 78 5f 74 72 79 28 29 2e 20 20 4f 6e 20 74 68 6f  x_try().  On tho
4a180 73 65 20 73 79 73 74 65 6d 73 2c 20 73 71 6c 69  se systems, sqli
4a190 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 0a  te3_mutex_try().
4a1a0 2a 2a 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 72  ** will always r
4a1b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53  eturn SQLITE_BUS
4a1c0 59 2e 20 20 7b 48 31 37 30 33 30 7d 20 54 68 65  Y.  {H17030} The
4a1d0 20 53 51 4c 69 74 65 20 63 6f 72 65 20 6f 6e 6c   SQLite core onl
4a1e0 79 20 65 76 65 72 20 75 73 65 73 0a 2a 2a 20 73  y ever uses.** s
4a1f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
4a200 28 29 20 61 73 20 61 6e 20 6f 70 74 69 6d 69 7a  () as an optimiz
4a210 61 74 69 6f 6e 20 73 6f 20 74 68 69 73 20 69 73  ation so this is
4a220 20 61 63 63 65 70 74 61 62 6c 65 20 62 65 68 61   acceptable beha
4a230 76 69 6f 72 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37  vior..**.** {H17
4a240 30 33 31 7d 20 54 68 65 20 73 71 6c 69 74 65 33  031} The sqlite3
4a250 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72  _mutex_leave() r
4a260 6f 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d  outine exits a m
4a270 75 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a  utex that was.**
4a280 20 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65   previously ente
4a290 72 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20  red by the same 
4a2a0 74 68 72 65 61 64 2e 20 20 7b 41 31 37 30 33 32  thread.  {A17032
4a2b0 7d 20 54 68 65 20 62 65 68 61 76 69 6f 72 0a 2a  } The behavior.*
4a2c0 2a 20 69 73 20 75 6e 64 65 66 69 6e 65 64 20 69  * is undefined i
4a2d0 66 20 74 68 65 20 6d 75 74 65 78 20 69 73 20 6e  f the mutex is n
4a2e0 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
4a2f0 65 72 65 64 20 62 79 20 74 68 65 0a 2a 2a 20 63  ered by the.** c
4a300 61 6c 6c 69 6e 67 20 74 68 72 65 61 64 20 6f 72  alling thread or
4a310 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
4a320 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 7b 48  y allocated.  {H
4a330 31 37 30 33 33 7d 20 53 51 4c 69 74 65 20 77 69  17033} SQLite wi
4a340 6c 6c 0a 2a 2a 20 6e 65 76 65 72 20 64 6f 20 65  ll.** never do e
4a350 69 74 68 65 72 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ither. {END}.**.
4a360 2a 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65  ** If the argume
4a370 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75  nt to sqlite3_mu
4a380 74 65 78 5f 65 6e 74 65 72 28 29 2c 20 73 71 6c  tex_enter(), sql
4a390 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29  ite3_mutex_try()
4a3a0 2c 20 6f 72 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  , or.** sqlite3_
4a3b0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 69 73  mutex_leave() is
4a3c0 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2c   a NULL pointer,
4a3d0 20 74 68 65 6e 20 61 6c 6c 20 74 68 72 65 65 20   then all three 
4a3e0 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 62 65 68 61  routines.** beha
4a3f0 76 65 20 61 73 20 6e 6f 2d 6f 70 73 2e 0a 2a 2a  ve as no-ops..**
4a400 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 73  .** See also: [s
4a410 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
4a420 64 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65  d()] and [sqlite
4a430 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
4a440 29 5d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  )]..*/.SQLITE_AP
4a450 49 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  I sqlite3_mutex 
4a460 2a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61  *sqlite3_mutex_a
4a470 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51 4c 49 54  lloc(int);.SQLIT
4a480 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74  E_API void sqlit
4a490 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 28 73 71  e3_mutex_free(sq
4a4a0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
4a4b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
4a4c0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
4a4d0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
4a4e0 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  *);.SQLITE_API i
4a4f0 6e 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  nt sqlite3_mutex
4a500 5f 74 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  _try(sqlite3_mut
4a510 65 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 41 50 49  ex*);.SQLITE_API
4a520 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
4a530 74 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65  tex_leave(sqlite
4a540 33 5f 6d 75 74 65 78 2a 29 3b 0a 0a 2f 2a 0a 2a  3_mutex*);../*.*
4a550 2a 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65  * CAPI3REF: Mute
4a560 78 20 4d 65 74 68 6f 64 73 20 4f 62 6a 65 63 74  x Methods Object
4a570 20 7b 48 31 37 31 32 30 7d 20 3c 53 32 30 31 33   {H17120} <S2013
4a580 30 3e 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54  0>.** EXPERIMENT
4a590 41 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  AL.**.** An inst
4a5a0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
4a5b0 75 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74  ucture defines t
4a5c0 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 72 6f 75  he low-level rou
4a5d0 74 69 6e 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f  tines.** used to
4a5e0 20 61 6c 6c 6f 63 61 74 65 20 61 6e 64 20 75 73   allocate and us
4a5f0 65 20 6d 75 74 65 78 65 73 2e 0a 2a 2a 0a 2a 2a  e mutexes..**.**
4a600 20 55 73 75 61 6c 6c 79 2c 20 74 68 65 20 64 65   Usually, the de
4a610 66 61 75 6c 74 20 6d 75 74 65 78 20 69 6d 70 6c  fault mutex impl
4a620 65 6d 65 6e 74 61 74 69 6f 6e 73 20 70 72 6f 76  ementations prov
4a630 69 64 65 64 20 62 79 20 53 51 4c 69 74 65 20 61  ided by SQLite a
4a640 72 65 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74  re.** sufficient
4a650 2c 20 68 6f 77 65 76 65 72 20 74 68 65 20 75 73  , however the us
4a660 65 72 20 68 61 73 20 74 68 65 20 6f 70 74 69 6f  er has the optio
4a670 6e 20 6f 66 20 73 75 62 73 74 69 74 75 74 69 6e  n of substitutin
4a680 67 20 61 20 63 75 73 74 6f 6d 0a 2a 2a 20 69 6d  g a custom.** im
4a690 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72  plementation for
4a6a0 20 73 70 65 63 69 61 6c 69 7a 65 64 20 64 65 70   specialized dep
4a6b0 6c 6f 79 6d 65 6e 74 73 20 6f 72 20 73 79 73 74  loyments or syst
4a6c0 65 6d 73 20 66 6f 72 20 77 68 69 63 68 20 53 51  ems for which SQ
4a6d0 4c 69 74 65 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74  Lite.** does not
4a6e0 20 70 72 6f 76 69 64 65 20 61 20 73 75 69 74 61   provide a suita
4a6f0 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ble implementati
4a700 6f 6e 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  on. In this case
4a710 2c 20 74 68 65 20 75 73 65 72 0a 2a 2a 20 63 72  , the user.** cr
4a720 65 61 74 65 73 20 61 6e 64 20 70 6f 70 75 6c 61  eates and popula
4a730 74 65 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20  tes an instance 
4a740 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
4a750 65 20 74 6f 20 70 61 73 73 0a 2a 2a 20 74 6f 20  e to pass.** to 
4a760 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
4a770 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
4a780 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d  [SQLITE_CONFIG_M
4a790 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a 2a  UTEX] option..**
4a7a0 20 41 64 64 69 74 69 6f 6e 61 6c 6c 79 2c 20 61   Additionally, a
4a7b0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
4a7c0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
4a7d0 20 62 65 20 75 73 65 64 20 61 73 20 61 6e 0a 2a   be used as an.*
4a7e0 2a 20 6f 75 74 70 75 74 20 76 61 72 69 61 62 6c  * output variabl
4a7f0 65 20 77 68 65 6e 20 71 75 65 72 79 69 6e 67 20  e when querying 
4a800 74 68 65 20 73 79 73 74 65 6d 20 66 6f 72 20 74  the system for t
4a810 68 65 20 63 75 72 72 65 6e 74 20 6d 75 74 65 78  he current mutex
4a820 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
4a830 6f 6e 2c 20 75 73 69 6e 67 20 74 68 65 20 5b 53  on, using the [S
4a840 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54  QLITE_CONFIG_GET
4a850 4d 55 54 45 58 5d 20 6f 70 74 69 6f 6e 2e 0a 2a  MUTEX] option..*
4a860 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74 65 78 49  *.** The xMutexI
4a870 6e 69 74 20 6d 65 74 68 6f 64 20 64 65 66 69 6e  nit method defin
4a880 65 64 20 62 79 20 74 68 69 73 20 73 74 72 75 63  ed by this struc
4a890 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65 64 20  ture is invoked 
4a8a0 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20 73 79  as.** part of sy
4a8b0 73 74 65 6d 20 69 6e 69 74 69 61 6c 69 7a 61 74  stem initializat
4a8c0 69 6f 6e 20 62 79 20 74 68 65 20 73 71 6c 69 74  ion by the sqlit
4a8d0 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20  e3_initialize() 
4a8e0 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 7b 48 31  function..** {H1
4a8f0 37 30 30 31 7d 20 54 68 65 20 78 4d 75 74 65 78  7001} The xMutex
4a900 49 6e 69 74 20 72 6f 75 74 69 6e 65 20 73 68 61  Init routine sha
4a910 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 62 79 20  ll be called by 
4a920 53 51 4c 69 74 65 20 6f 6e 63 65 20 66 6f 72 20  SQLite once for 
4a930 65 61 63 68 0a 2a 2a 20 65 66 66 65 63 74 69 76  each.** effectiv
4a940 65 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74  e call to [sqlit
4a950 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 5d  e3_initialize()]
4a960 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 4d 75 74  ..**.** The xMut
4a970 65 78 45 6e 64 20 6d 65 74 68 6f 64 20 64 65 66  exEnd method def
4a980 69 6e 65 64 20 62 79 20 74 68 69 73 20 73 74 72  ined by this str
4a990 75 63 74 75 72 65 20 69 73 20 69 6e 76 6f 6b 65  ucture is invoke
4a9a0 64 20 61 73 0a 2a 2a 20 70 61 72 74 20 6f 66 20  d as.** part of 
4a9b0 73 79 73 74 65 6d 20 73 68 75 74 64 6f 77 6e 20  system shutdown 
4a9c0 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73  by the sqlite3_s
4a9d0 68 75 74 64 6f 77 6e 28 29 20 66 75 6e 63 74 69  hutdown() functi
4a9e0 6f 6e 2e 20 54 68 65 0a 2a 2a 20 69 6d 70 6c 65  on. The.** imple
4a9f0 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 69  mentation of thi
4aa00 73 20 6d 65 74 68 6f 64 20 69 73 20 65 78 70 65  s method is expe
4aa10 63 74 65 64 20 74 6f 20 72 65 6c 65 61 73 65 20  cted to release 
4aa20 61 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 0a  all outstanding.
4aa30 2a 2a 20 72 65 73 6f 75 72 63 65 73 20 6f 62 74  ** resources obt
4aa40 61 69 6e 65 64 20 62 79 20 74 68 65 20 6d 75 74  ained by the mut
4aa50 65 78 20 6d 65 74 68 6f 64 73 20 69 6d 70 6c 65  ex methods imple
4aa60 6d 65 6e 74 61 74 69 6f 6e 2c 20 65 73 70 65 63  mentation, espec
4aa70 69 61 6c 6c 79 0a 2a 2a 20 74 68 6f 73 65 20 6f  ially.** those o
4aa80 62 74 61 69 6e 65 64 20 62 79 20 74 68 65 20 78  btained by the x
4aa90 4d 75 74 65 78 49 6e 69 74 20 6d 65 74 68 6f 64  MutexInit method
4aaa0 2e 20 7b 48 31 37 30 30 33 7d 20 54 68 65 20 78  . {H17003} The x
4aab0 4d 75 74 65 78 45 6e 64 28 29 0a 2a 2a 20 69 6e  MutexEnd().** in
4aac0 74 65 72 66 61 63 65 20 73 68 61 6c 6c 20 62 65  terface shall be
4aad0 20 69 6e 76 6f 6b 65 64 20 6f 6e 63 65 20 66 6f   invoked once fo
4aae0 72 20 65 61 63 68 20 63 61 6c 6c 20 74 6f 20 5b  r each call to [
4aaf0 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
4ab00 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ()]..**.** The r
4ab10 65 6d 61 69 6e 69 6e 67 20 73 65 76 65 6e 20 6d  emaining seven m
4ab20 65 74 68 6f 64 73 20 64 65 66 69 6e 65 64 20 62  ethods defined b
4ab30 79 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  y this structure
4ab40 20 28 78 4d 75 74 65 78 41 6c 6c 6f 63 2c 0a 2a   (xMutexAlloc,.*
4ab50 2a 20 78 4d 75 74 65 78 46 72 65 65 2c 20 78 4d  * xMutexFree, xM
4ab60 75 74 65 78 45 6e 74 65 72 2c 20 78 4d 75 74 65  utexEnter, xMute
4ab70 78 54 72 79 2c 20 78 4d 75 74 65 78 4c 65 61 76  xTry, xMutexLeav
4ab80 65 2c 20 78 4d 75 74 65 78 48 65 6c 64 20 61 6e  e, xMutexHeld an
4ab90 64 0a 2a 2a 20 78 4d 75 74 65 78 4e 6f 74 68 65  d.** xMutexNothe
4aba0 6c 64 29 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  ld) implement th
4abb0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 74 65  e following inte
4abc0 72 66 61 63 65 73 20 28 72 65 73 70 65 63 74 69  rfaces (respecti
4abd0 76 65 6c 79 29 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  vely):.**.** <ul
4abe0 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73 71  >.**   <li>  [sq
4abf0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
4ac00 63 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20 20  c()] </li>.**   
4ac10 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f 6d  <li>  [sqlite3_m
4ac20 75 74 65 78 5f 66 72 65 65 28 29 5d 20 3c 2f 6c  utex_free()] </l
4ac30 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
4ac40 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
4ac50 65 72 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  er()] </li>.**  
4ac60 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
4ac70 6d 75 74 65 78 5f 74 72 79 28 29 5d 20 3c 2f 6c  mutex_try()] </l
4ac80 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b 73  i>.**   <li>  [s
4ac90 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
4aca0 76 65 28 29 5d 20 3c 2f 6c 69 3e 0a 2a 2a 20 20  ve()] </li>.**  
4acb0 20 3c 6c 69 3e 20 20 5b 73 71 6c 69 74 65 33 5f   <li>  [sqlite3_
4acc0 6d 75 74 65 78 5f 68 65 6c 64 28 29 5d 20 3c 2f  mutex_held()] </
4acd0 6c 69 3e 0a 2a 2a 20 20 20 3c 6c 69 3e 20 20 5b  li>.**   <li>  [
4ace0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f  sqlite3_mutex_no
4acf0 74 68 65 6c 64 28 29 5d 20 3c 2f 6c 69 3e 0a 2a  theld()] </li>.*
4ad00 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
4ad10 65 20 6f 6e 6c 79 20 64 69 66 66 65 72 65 6e 63  e only differenc
4ad20 65 20 69 73 20 74 68 61 74 20 74 68 65 20 70 75  e is that the pu
4ad30 62 6c 69 63 20 73 71 6c 69 74 65 33 5f 58 58 58  blic sqlite3_XXX
4ad40 20 66 75 6e 63 74 69 6f 6e 73 20 65 6e 75 6d 65   functions enume
4ad50 72 61 74 65 64 0a 2a 2a 20 61 62 6f 76 65 20 73  rated.** above s
4ad60 69 6c 65 6e 74 6c 79 20 69 67 6e 6f 72 65 20 61  ilently ignore a
4ad70 6e 79 20 69 6e 76 6f 63 61 74 69 6f 6e 73 20 74  ny invocations t
4ad80 68 61 74 20 70 61 73 73 20 61 20 4e 55 4c 4c 20  hat pass a NULL 
4ad90 70 6f 69 6e 74 65 72 20 69 6e 73 74 65 61 64 0a  pointer instead.
4ada0 2a 2a 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ** of a valid mu
4adb0 74 65 78 20 68 61 6e 64 6c 65 2e 20 54 68 65 20  tex handle. The 
4adc0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20  implementations 
4add0 6f 66 20 74 68 65 20 6d 65 74 68 6f 64 73 20 64  of the methods d
4ade0 65 66 69 6e 65 64 0a 2a 2a 20 62 79 20 74 68 69  efined.** by thi
4adf0 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20  s structure are 
4ae00 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20  not required to 
4ae10 68 61 6e 64 6c 65 20 74 68 69 73 20 63 61 73 65  handle this case
4ae20 2c 20 74 68 65 20 72 65 73 75 6c 74 73 0a 2a 2a  , the results.**
4ae30 20 6f 66 20 70 61 73 73 69 6e 67 20 61 20 4e 55   of passing a NU
4ae40 4c 4c 20 70 6f 69 6e 74 65 72 20 69 6e 73 74 65  LL pointer inste
4ae50 61 64 20 6f 66 20 61 20 76 61 6c 69 64 20 6d 75  ad of a valid mu
4ae60 74 65 78 20 68 61 6e 64 6c 65 20 61 72 65 20 75  tex handle are u
4ae70 6e 64 65 66 69 6e 65 64 0a 2a 2a 20 28 69 2e 65  ndefined.** (i.e
4ae80 2e 20 69 74 20 69 73 20 61 63 63 65 70 74 61 62  . it is acceptab
4ae90 6c 65 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e  le to provide an
4aea0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
4aeb0 74 68 61 74 20 73 65 67 66 61 75 6c 74 73 20 69  that segfaults i
4aec0 66 0a 2a 2a 20 69 74 20 69 73 20 70 61 73 73 65  f.** it is passe
4aed0 64 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  d a NULL pointer
4aee0 29 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  )..*/.typedef st
4aef0 72 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74  ruct sqlite3_mut
4af00 65 78 5f 6d 65 74 68 6f 64 73 20 73 71 6c 69 74  ex_methods sqlit
4af10 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
4af20 3b 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  ;.struct sqlite3
4af30 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 7b  _mutex_methods {
4af40 0a 20 20 69 6e 74 20 28 2a 78 4d 75 74 65 78 49  .  int (*xMutexI
4af50 6e 69 74 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e  nit)(void);.  in
4af60 74 20 28 2a 78 4d 75 74 65 78 45 6e 64 29 28 76  t (*xMutexEnd)(v
4af70 6f 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  oid);.  sqlite3_
4af80 6d 75 74 65 78 20 2a 28 2a 78 4d 75 74 65 78 41  mutex *(*xMutexA
4af90 6c 6c 6f 63 29 28 69 6e 74 29 3b 0a 20 20 76 6f  lloc)(int);.  vo
4afa0 69 64 20 28 2a 78 4d 75 74 65 78 46 72 65 65 29  id (*xMutexFree)
4afb0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
4afc0 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 4d 75 74  );.  void (*xMut
4afd0 65 78 45 6e 74 65 72 29 28 73 71 6c 69 74 65 33  exEnter)(sqlite3
4afe0 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74  _mutex *);.  int
4aff0 20 28 2a 78 4d 75 74 65 78 54 72 79 29 28 73 71   (*xMutexTry)(sq
4b000 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a  lite3_mutex *);.
4b010 20 20 76 6f 69 64 20 28 2a 78 4d 75 74 65 78 4c    void (*xMutexL
4b020 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  eave)(sqlite3_mu
4b030 74 65 78 20 2a 29 3b 0a 20 20 69 6e 74 20 28 2a  tex *);.  int (*
4b040 78 4d 75 74 65 78 48 65 6c 64 29 28 73 71 6c 69  xMutexHeld)(sqli
4b050 74 65 33 5f 6d 75 74 65 78 20 2a 29 3b 0a 20 20  te3_mutex *);.  
4b060 69 6e 74 20 28 2a 78 4d 75 74 65 78 4e 6f 74 68  int (*xMutexNoth
4b070 65 6c 64 29 28 73 71 6c 69 74 65 33 5f 6d 75 74  eld)(sqlite3_mut
4b080 65 78 20 2a 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ex *);.};../*.**
4b090 20 43 41 50 49 33 52 45 46 3a 20 4d 75 74 65 78   CAPI3REF: Mutex
4b0a0 20 56 65 72 69 66 69 63 61 74 69 6f 6e 20 52 6f   Verification Ro
4b0b0 75 74 69 6e 65 73 20 7b 48 31 37 30 38 30 7d 20  utines {H17080} 
4b0c0 3c 53 32 30 31 33 30 3e 20 3c 53 33 30 38 30 30  <S20130> <S30800
4b0d0 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69  >.**.** The sqli
4b0e0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 29  te3_mutex_held()
4b0f0 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74   and sqlite3_mut
4b100 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f 75  ex_notheld() rou
4b110 74 69 6e 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74  tines.** are int
4b120 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 69 6e  ended for use in
4b130 73 69 64 65 20 61 73 73 65 72 74 28 29 20 73 74  side assert() st
4b140 61 74 65 6d 65 6e 74 73 2e 20 7b 48 31 37 30 38  atements. {H1708
4b150 31 7d 20 54 68 65 20 53 51 4c 69 74 65 20 63 6f  1} The SQLite co
4b160 72 65 0a 2a 2a 20 6e 65 76 65 72 20 75 73 65 73  re.** never uses
4b170 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   these routines 
4b180 65 78 63 65 70 74 20 69 6e 73 69 64 65 20 61 6e  except inside an
4b190 20 61 73 73 65 72 74 28 29 20 61 6e 64 20 61 70   assert() and ap
4b1a0 70 6c 69 63 61 74 69 6f 6e 73 0a 2a 2a 20 61 72  plications.** ar
4b1b0 65 20 61 64 76 69 73 65 64 20 74 6f 20 66 6f 6c  e advised to fol
4b1c0 6c 6f 77 20 74 68 65 20 6c 65 61 64 20 6f 66 20  low the lead of 
4b1d0 74 68 65 20 63 6f 72 65 2e 20 20 7b 48 31 37 30  the core.  {H170
4b1e0 38 32 7d 20 54 68 65 20 63 6f 72 65 20 6f 6e 6c  82} The core onl
4b1f0 79 0a 2a 2a 20 70 72 6f 76 69 64 65 73 20 69 6d  y.** provides im
4b200 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 66 6f  plementations fo
4b210 72 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65 73  r these routines
4b220 20 77 68 65 6e 20 69 74 20 69 73 20 63 6f 6d 70   when it is comp
4b230 69 6c 65 64 0a 2a 2a 20 77 69 74 68 20 74 68 65  iled.** with the
4b240 20 53 51 4c 49 54 45 5f 44 45 42 55 47 20 66 6c   SQLITE_DEBUG fl
4b250 61 67 2e 20 20 7b 41 31 37 30 38 37 7d 20 45 78  ag.  {A17087} Ex
4b260 74 65 72 6e 61 6c 20 6d 75 74 65 78 20 69 6d 70  ternal mutex imp
4b270 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 0a 2a 2a 20  lementations.** 
4b280 61 72 65 20 6f 6e 6c 79 20 72 65 71 75 69 72 65  are only require
4b290 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74 68 65  d to provide the
4b2a0 73 65 20 72 6f 75 74 69 6e 65 73 20 69 66 20 53  se routines if S
4b2b0 51 4c 49 54 45 5f 44 45 42 55 47 20 69 73 0a 2a  QLITE_DEBUG is.*
4b2c0 2a 20 64 65 66 69 6e 65 64 20 61 6e 64 20 69 66  * defined and if
4b2d0 20 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64   NDEBUG is not d
4b2e0 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 7b 48  efined..**.** {H
4b2f0 31 37 30 38 33 7d 20 54 68 65 73 65 20 72 6f 75  17083} These rou
4b300 74 69 6e 65 73 20 73 68 6f 75 6c 64 20 72 65 74  tines should ret
4b310 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
4b320 6d 75 74 65 78 20 69 6e 20 74 68 65 69 72 20 61  mutex in their a
4b330 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 68 65  rgument.** is he
4b340 6c 64 20 6f 72 20 6e 6f 74 20 68 65 6c 64 2c 20  ld or not held, 
4b350 72 65 73 70 65 63 74 69 76 65 6c 79 2c 20 62 79  respectively, by
4b360 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 74 68 72   the calling thr
4b370 65 61 64 2e 0a 2a 2a 0a 2a 2a 20 7b 58 31 37 30  ead..**.** {X170
4b380 38 34 7d 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e  84} The implemen
4b390 74 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20 72 65  tation is not re
4b3a0 71 75 69 72 65 64 20 74 6f 20 70 72 6f 76 69 64  quired to provid
4b3b0 65 64 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74  ed versions of t
4b3c0 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  hese.** routines
4b3d0 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79 20 77   that actually w
4b3e0 6f 72 6b 2e 20 49 66 20 74 68 65 20 69 6d 70 6c  ork. If the impl
4b3f0 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20  ementation does 
4b400 6e 6f 74 20 70 72 6f 76 69 64 65 20 77 6f 72 6b  not provide work
4b410 69 6e 67 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20  ing.** versions 
4b420 6f 66 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  of these routine
4b430 73 2c 20 69 74 20 73 68 6f 75 6c 64 20 61 74 20  s, it should at 
4b440 6c 65 61 73 74 20 70 72 6f 76 69 64 65 20 73 74  least provide st
4b450 75 62 73 20 74 68 61 74 20 61 6c 77 61 79 73 0a  ubs that always.
4b460 2a 2a 20 72 65 74 75 72 6e 20 74 72 75 65 20 73  ** return true s
4b470 6f 20 74 68 61 74 20 6f 6e 65 20 64 6f 65 73 20  o that one does 
4b480 6e 6f 74 20 67 65 74 20 73 70 75 72 69 6f 75 73  not get spurious
4b490 20 61 73 73 65 72 74 69 6f 6e 20 66 61 69 6c 75   assertion failu
4b4a0 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 7b 48 31 37 30  res..**.** {H170
4b4b0 38 35 7d 20 49 66 20 74 68 65 20 61 72 67 75 6d  85} If the argum
4b4c0 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ent to sqlite3_m
4b4d0 75 74 65 78 5f 68 65 6c 64 28 29 20 69 73 20 61  utex_held() is a
4b4e0 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 74 68   NULL pointer th
4b4f0 65 6e 0a 2a 2a 20 74 68 65 20 72 6f 75 74 69 6e  en.** the routin
4b500 65 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20  e should return 
4b510 31 2e 20 20 7b 45 4e 44 7d 20 54 68 69 73 20 73  1.  {END} This s
4b520 65 65 6d 73 20 63 6f 75 6e 74 65 72 2d 69 6e 74  eems counter-int
4b530 75 69 74 69 76 65 20 73 69 6e 63 65 0a 2a 2a 20  uitive since.** 
4b540 63 6c 65 61 72 6c 79 20 74 68 65 20 6d 75 74 65  clearly the mute
4b550 78 20 63 61 6e 6e 6f 74 20 62 65 20 68 65 6c 64  x cannot be held
4b560 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20   if it does not 
4b570 65 78 69 73 74 2e 20 20 42 75 74 20 74 68 65 0a  exist.  But the.
4b580 2a 2a 20 74 68 65 20 72 65 61 73 6f 6e 20 74 68  ** the reason th
4b590 65 20 6d 75 74 65 78 20 64 6f 65 73 20 6e 6f 74  e mutex does not
4b5a0 20 65 78 69 73 74 20 69 73 20 62 65 63 61 75 73   exist is becaus
4b5b0 65 20 74 68 65 20 62 75 69 6c 64 20 69 73 20 6e  e the build is n
4b5c0 6f 74 0a 2a 2a 20 75 73 69 6e 67 20 6d 75 74 65  ot.** using mute
4b5d0 78 65 73 2e 20 20 41 6e 64 20 77 65 20 64 6f 20  xes.  And we do 
4b5e0 6e 6f 74 20 77 61 6e 74 20 74 68 65 20 61 73 73  not want the ass
4b5f0 65 72 74 28 29 20 63 6f 6e 74 61 69 6e 69 6e 67  ert() containing
4b600 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20   the.** call to 
4b610 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
4b620 6c 64 28 29 20 74 6f 20 66 61 69 6c 2c 20 73 6f  ld() to fail, so
4b630 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 72 65 74 75   a non-zero retu
4b640 72 6e 20 69 73 0a 2a 2a 20 74 68 65 20 61 70 70  rn is.** the app
4b650 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67 20 74  ropriate thing t
4b660 6f 20 64 6f 2e 20 20 7b 48 31 37 30 38 36 7d 20  o do.  {H17086} 
4b670 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
4b680 78 5f 6e 6f 74 68 65 6c 64 28 29 0a 2a 2a 20 69  x_notheld().** i
4b690 6e 74 65 72 66 61 63 65 20 73 68 6f 75 6c 64 20  nterface should 
4b6a0 61 6c 73 6f 20 72 65 74 75 72 6e 20 31 20 77 68  also return 1 wh
4b6b0 65 6e 20 67 69 76 65 6e 20 61 20 4e 55 4c 4c 20  en given a NULL 
4b6c0 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49  pointer..*/.SQLI
4b6d0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
4b6e0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71  e3_mutex_held(sq
4b6f0 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a 53  lite3_mutex*);.S
4b700 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
4b710 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
4b720 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  eld(sqlite3_mute
4b730 78 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  x*);../*.** CAPI
4b740 33 52 45 46 3a 20 4d 75 74 65 78 20 54 79 70 65  3REF: Mutex Type
4b750 73 20 7b 48 31 37 30 30 31 7d 20 3c 48 31 37 30  s {H17001} <H170
4b760 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73  00>.**.** The [s
4b770 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
4b780 6f 63 28 29 5d 20 69 6e 74 65 72 66 61 63 65 20  oc()] interface 
4b790 74 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 20 61  takes a single a
4b7a0 72 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 69 63 68  rgument.** which
4b7b0 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65 73 65   is one of these
4b7c0 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e   integer constan
4b7d0 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 65  ts..**.** The se
4b7e0 74 20 6f 66 20 73 74 61 74 69 63 20 6d 75 74 65  t of static mute
4b7f0 78 65 73 20 6d 61 79 20 63 68 61 6e 67 65 20 66  xes may change f
4b800 72 6f 6d 20 6f 6e 65 20 53 51 4c 69 74 65 20 72  rom one SQLite r
4b810 65 6c 65 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  elease to the.**
4b820 20 6e 65 78 74 2e 20 20 41 70 70 6c 69 63 61 74   next.  Applicat
4b830 69 6f 6e 73 20 74 68 61 74 20 6f 76 65 72 72 69  ions that overri
4b840 64 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  de the built-in 
4b850 6d 75 74 65 78 20 6c 6f 67 69 63 20 6d 75 73 74  mutex logic must
4b860 20 62 65 0a 2a 2a 20 70 72 65 70 61 72 65 64 20   be.** prepared 
4b870 74 6f 20 61 63 63 6f 6d 6d 6f 64 61 74 65 20 61  to accommodate a
4b880 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
4b890 20 6d 75 74 65 78 65 73 2e 0a 2a 2f 0a 23 64 65   mutexes..*/.#de
4b8a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
4b8b0 58 5f 46 41 53 54 20 20 20 20 20 20 20 20 20 20  X_FAST          
4b8c0 20 20 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c     0.#define SQL
4b8d0 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
4b8e0 49 56 45 20 20 20 20 20 20 20 20 31 0a 23 64 65  IVE        1.#de
4b8f0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
4b900 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20  X_STATIC_MASTER 
4b910 20 20 20 32 0a 23 64 65 66 69 6e 65 20 53 51 4c     2.#define SQL
4b920 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
4b930 5f 4d 45 4d 20 20 20 20 20 20 20 33 20 20 2f 2a  _MEM       3  /*
4b940 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
4b950 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ) */.#define SQL
4b960 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
4b970 5f 4d 45 4d 32 20 20 20 20 20 20 34 20 20 2f 2a  _MEM2      4  /*
4b980 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65   sqlite3_release
4b990 5f 6d 65 6d 6f 72 79 28 29 20 2a 2f 0a 23 64 65  _memory() */.#de
4b9a0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
4b9b0 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20 20  X_STATIC_PRNG   
4b9c0 20 20 20 35 20 20 2f 2a 20 73 71 6c 69 74 65 33     5  /* sqlite3
4b9d0 5f 72 61 6e 64 6f 6d 28 29 20 2a 2f 0a 23 64 65  _random() */.#de
4b9e0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45  fine SQLITE_MUTE
4b9f0 58 5f 53 54 41 54 49 43 5f 4c 52 55 20 20 20 20  X_STATIC_LRU    
4ba00 20 20 20 36 20 20 2f 2a 20 6c 72 75 20 70 61 67     6  /* lru pag
4ba10 65 20 6c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e  e list */.#defin
4ba20 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  e SQLITE_MUTEX_S
4ba30 54 41 54 49 43 5f 4c 52 55 32 20 20 20 20 20 20  TATIC_LRU2      
4ba40 37 20 20 2f 2a 20 6c 72 75 20 70 61 67 65 20 6c  7  /* lru page l
4ba50 69 73 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 41  ist */../*.** CA
4ba60 50 49 33 52 45 46 3a 20 52 65 74 72 69 65 76 65  PI3REF: Retrieve
4ba70 20 74 68 65 20 6d 75 74 65 78 20 66 6f 72 20 61   the mutex for a
4ba80 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
4ba90 74 69 6f 6e 20 7b 48 31 37 30 30 32 7d 20 3c 48  tion {H17002} <H
4baa0 31 37 30 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 69  17000>.**.** Thi
4bab0 73 20 69 6e 74 65 72 66 61 63 65 20 72 65 74 75  s interface retu
4bac0 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 68  rns a pointer th
4bad0 65 20 5b 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  e [sqlite3_mutex
4bae0 5d 20 6f 62 6a 65 63 74 20 74 68 61 74 20 0a 2a  ] object that .*
4baf0 2a 20 73 65 72 69 61 6c 69 7a 65 73 20 61 63 63  * serializes acc
4bb00 65 73 73 20 74 6f 20 74 68 65 20 5b 64 61 74 61  ess to the [data
4bb10 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 5d  base connection]
4bb20 20 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72   given in the ar
4bb30 67 75 6d 65 6e 74 0a 2a 2a 20 77 68 65 6e 20 74  gument.** when t
4bb40 68 65 20 5b 74 68 72 65 61 64 69 6e 67 20 6d 6f  he [threading mo
4bb50 64 65 5d 20 69 73 20 53 65 72 69 61 6c 69 7a 65  de] is Serialize
4bb60 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 5b 74 68  d..** If the [th
4bb70 72 65 61 64 69 6e 67 20 6d 6f 64 65 5d 20 69 73  reading mode] is
4bb80 20 53 69 6e 67 6c 65 2d 74 68 72 65 61 64 20 6f   Single-thread o
4bb90 72 20 4d 75 6c 74 69 2d 74 68 72 65 61 64 20 74  r Multi-thread t
4bba0 68 65 6e 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74  hen this.** rout
4bbb0 69 6e 65 20 72 65 74 75 72 6e 73 20 61 20 4e 55  ine returns a NU
4bbc0 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  LL pointer..*/.S
4bbd0 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65  QLITE_API sqlite
4bbe0 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33  3_mutex *sqlite3
4bbf0 5f 64 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65  _db_mutex(sqlite
4bc00 33 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 43 41 50 49  3*);../*.** CAPI
4bc10 33 52 45 46 3a 20 4c 6f 77 2d 4c 65 76 65 6c 20  3REF: Low-Level 
4bc20 43 6f 6e 74 72 6f 6c 20 4f 66 20 44 61 74 61 62  Control Of Datab
4bc30 61 73 65 20 46 69 6c 65 73 20 7b 48 31 31 33 30  ase Files {H1130
4bc40 30 7d 20 3c 53 33 30 38 30 30 3e 0a 2a 2a 0a 2a  0} <S30800>.**.*
4bc50 2a 20 7b 48 31 31 33 30 31 7d 20 54 68 65 20 5b  * {H11301} The [
4bc60 73 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e  sqlite3_file_con
4bc70 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63  trol()] interfac
4bc80 65 20 6d 61 6b 65 73 20 61 20 64 69 72 65 63 74  e makes a direct
4bc90 20 63 61 6c 6c 20 74 6f 20 74 68 65 0a 2a 2a 20   call to the.** 
4bca0 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74  xFileControl met
4bcb0 68 6f 64 20 66 6f 72 20 74 68 65 20 5b 73 71 6c  hod for the [sql
4bcc0 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 5d  ite3_io_methods]
4bcd0 20 6f 62 6a 65 63 74 20 61 73 73 6f 63 69 61 74   object associat
4bce0 65 64 0a 2a 2a 20 77 69 74 68 20 61 20 70 61 72  ed.** with a par
4bcf0 74 69 63 75 6c 61 72 20 64 61 74 61 62 61 73 65  ticular database
4bd00 20 69 64 65 6e 74 69 66 69 65 64 20 62 79 20 74   identified by t
4bd10 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65  he second argume
4bd20 6e 74 2e 20 7b 48 31 31 33 30 32 7d 20 54 68 65  nt. {H11302} The
4bd30 0a 2a 2a 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  .** name of the 
4bd40 64 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20  database is the 
4bd50 6e 61 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f  name assigned to
4bd60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 79   the database by
4bd70 20 74 68 65 0a 2a 2a 20 3c 61 20 68 72 65 66 3d   the.** <a href=
4bd80 22 6c 61 6e 67 5f 61 74 74 61 63 68 2e 68 74 6d  "lang_attach.htm
4bd90 6c 22 3e 41 54 54 41 43 48 3c 2f 61 3e 20 53 51  l">ATTACH</a> SQ
4bda0 4c 20 63 6f 6d 6d 61 6e 64 20 74 68 61 74 20 6f  L command that o
4bdb0 70 65 6e 65 64 20 74 68 65 0a 2a 2a 20 64 61 74  pened the.** dat
4bdc0 61 62 61 73 65 2e 20 7b 48 31 31 33 30 33 7d 20  abase. {H11303} 
4bdd0 54 6f 20 63 6f 6e 74 72 6f 6c 20 74 68 65 20 6d  To control the m
4bde0 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c  ain database fil
4bdf0 65 2c 20 75 73 65 20 74 68 65 20 6e 61 6d 65 20  e, use the name 
4be00 22 6d 61 69 6e 22 0a 2a 2a 20 6f 72 20 61 20 4e  "main".** or a N
4be10 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 20 7b 48 31  ULL pointer. {H1
4be20 31 33 30 34 7d 20 54 68 65 20 74 68 69 72 64 20  1304} The third 
4be30 61 6e 64 20 66 6f 75 72 74 68 20 70 61 72 61 6d  and fourth param
4be40 65 74 65 72 73 20 74 6f 20 74 68 69 73 20 72 6f  eters to this ro
4be50 75 74 69 6e 65 0a 2a 2a 20 61 72 65 20 70 61 73  utine.** are pas
4be60 73 65 64 20 64 69 72 65 63 74 6c 79 20 74 68 72  sed directly thr
4be70 6f 75 67 68 20 74 6f 20 74 68 65 20 73 65 63 6f  ough to the seco
4be80 6e 64 20 61 6e 64 20 74 68 69 72 64 20 70 61 72  nd and third par
4be90 61 6d 65 74 65 72 73 20 6f 66 0a 2a 2a 20 74 68  ameters of.** th
4bea0 65 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  e xFileControl m
4beb0 65 74 68 6f 64 2e 20 20 7b 48 31 31 33 30 35 7d  ethod.  {H11305}
4bec0 20 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   The return valu
4bed0 65 20 6f 66 20 74 68 65 20 78 46 69 6c 65 43 6f  e of the xFileCo
4bee0 6e 74 72 6f 6c 0a 2a 2a 20 6d 65 74 68 6f 64 20  ntrol.** method 
4bef0 62 65 63 6f 6d 65 73 20 74 68 65 20 72 65 74 75  becomes the retu
4bf00 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68 69 73  rn value of this
4bf10 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
4bf20 7b 48 31 31 33 30 36 7d 20 49 66 20 74 68 65 20  {H11306} If the 
4bf30 73 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  second parameter
4bf40 20 28 7a 44 62 4e 61 6d 65 29 20 64 6f 65 73 20   (zDbName) does 
4bf50 6e 6f 74 20 6d 61 74 63 68 20 74 68 65 20 6e 61  not match the na
4bf60 6d 65 20 6f 66 20 61 6e 79 0a 2a 2a 20 6f 70 65  me of any.** ope
4bf70 6e 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  n database file,
4bf80 20 74 68 65 6e 20 53 51 4c 49 54 45 5f 45 52 52   then SQLITE_ERR
4bf90 4f 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OR is returned. 
4bfa0 7b 48 31 31 33 30 37 7d 20 54 68 69 73 20 65 72  {H11307} This er
4bfb0 72 6f 72 0a 2a 2a 20 63 6f 64 65 20 69 73 20 6e  ror.** code is n
4bfc0 6f 74 20 72 65 6d 65 6d 62 65 72 65 64 20 61 6e  ot remembered an
4bfd0 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 72 65  d will not be re
4bfe0 63 61 6c 6c 65 64 20 62 79 20 5b 73 71 6c 69 74  called by [sqlit
4bff0 65 33 5f 65 72 72 63 6f 64 65 28 29 5d 0a 2a 2a  e3_errcode()].**
4c000 20 6f 72 20 5b 73 71 6c 69 74 65 33 5f 65 72 72   or [sqlite3_err
4c010 6d 73 67 28 29 5d 2e 20 7b 41 31 31 33 30 38 7d  msg()]. {A11308}
4c020 20 54 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20   The underlying 
4c030 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d 65 74  xFileControl met
4c040 68 6f 64 20 6d 69 67 68 74 0a 2a 2a 20 61 6c 73  hod might.** als
4c050 6f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  o return SQLITE_
4c060 45 52 52 4f 52 2e 20 20 7b 41 31 31 33 30 39 7d  ERROR.  {A11309}
4c070 20 54 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79   There is no way
4c080 20 74 6f 20 64 69 73 74 69 6e 67 75 69 73 68 20   to distinguish 
4c090 62 65 74 77 65 65 6e 0a 2a 2a 20 61 6e 20 69 6e  between.** an in
4c0a0 63 6f 72 72 65 63 74 20 7a 44 62 4e 61 6d 65 20  correct zDbName 
4c0b0 61 6e 64 20 61 6e 20 53 51 4c 49 54 45 5f 45 52  and an SQLITE_ER
4c0c0 52 4f 52 20 72 65 74 75 72 6e 20 66 72 6f 6d 20  ROR return from 
4c0d0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 0a 2a  the underlying.*
4c0e0 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 6d  * xFileControl m
4c0f0 65 74 68 6f 64 2e 20 7b 45 4e 44 7d 0a 2a 2a 0a  ethod. {END}.**.
4c100 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b 53 51  ** See also: [SQ
4c110 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
4c120 54 41 54 45 5d 0a 2a 2f 0a 53 51 4c 49 54 45 5f  TATE].*/.SQLITE_
4c130 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
4c140 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c  file_control(sql
4c150 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
4c160 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69 6e 74 20  r *zDbName, int 
4c170 6f 70 2c 20 76 6f 69 64 2a 29 3b 0a 0a 2f 2a 0a  op, void*);../*.
4c180 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 54 65 73  ** CAPI3REF: Tes
4c190 74 69 6e 67 20 49 6e 74 65 72 66 61 63 65 20 7b  ting Interface {
4c1a0 48 31 31 34 30 30 7d 20 3c 53 33 30 38 30 30 3e  H11400} <S30800>
4c1b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  .**.** The sqlit
4c1c0 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28  e3_test_control(
4c1d0 29 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 75  ) interface is u
4c1e0 73 65 64 20 74 6f 20 72 65 61 64 20 6f 75 74 20  sed to read out 
4c1f0 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 73 74 61 74  internal.** stat
4c200 65 20 6f 66 20 53 51 4c 69 74 65 20 61 6e 64 20  e of SQLite and 
4c210 74 6f 20 69 6e 6a 65 63 74 20 66 61 75 6c 74 73  to inject faults
4c220 20 69 6e 74 6f 20 53 51 4c 69 74 65 20 66 6f 72   into SQLite for
4c230 20 74 65 73 74 69 6e 67 0a 2a 2a 20 70 75 72 70   testing.** purp
4c240 6f 73 65 73 2e 20 20 54 68 65 20 66 69 72 73 74  oses.  The first
4c250 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 61 6e   parameter is an
4c260 20 6f 70 65 72 61 74 69 6f 6e 20 63 6f 64 65 20   operation code 
4c270 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 0a  that determines.
4c280 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 2c 20 6d  ** the number, m
4c290 65 61 6e 69 6e 67 2c 20 61 6e 64 20 6f 70 65 72  eaning, and oper
4c2a0 61 74 69 6f 6e 20 6f 66 20 61 6c 6c 20 73 75 62  ation of all sub
4c2b0 73 65 71 75 65 6e 74 20 70 61 72 61 6d 65 74 65  sequent paramete
4c2c0 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  rs..**.** This i
4c2d0 6e 74 65 72 66 61 63 65 20 69 73 20 6e 6f 74 20  nterface is not 
4c2e0 66 6f 72 20 75 73 65 20 62 79 20 61 70 70 6c 69  for use by appli
4c2f0 63 61 74 69 6f 6e 73 2e 20 20 49 74 20 65 78 69  cations.  It exi
4c300 73 74 73 20 73 6f 6c 65 6c 79 0a 2a 2a 20 66 6f  sts solely.** fo
4c310 72 20 76 65 72 69 66 79 69 6e 67 20 74 68 65 20  r verifying the 
4c320 63 6f 72 72 65 63 74 20 6f 70 65 72 61 74 69 6f  correct operatio
4c330 6e 20 6f 66 20 74 68 65 20 53 51 4c 69 74 65 20  n of the SQLite 
4c340 6c 69 62 72 61 72 79 2e 20 20 44 65 70 65 6e 64  library.  Depend
4c350 69 6e 67 0a 2a 2a 20 6f 6e 20 68 6f 77 20 74 68  ing.** on how th
4c360 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
4c370 20 69 73 20 63 6f 6d 70 69 6c 65 64 2c 20 74 68   is compiled, th
4c380 69 73 20 69 6e 74 65 72 66 61 63 65 20 6d 69 67  is interface mig
4c390 68 74 20 6e 6f 74 20 65 78 69 73 74 2e 0a 2a 2a  ht not exist..**
4c3a0 0a 2a 2a 20 54 68 65 20 64 65 74 61 69 6c 73 20  .** The details 
4c3b0 6f 66 20 74 68 65 20 6f 70 65 72 61 74 69 6f 6e  of the operation
4c3c0 20 63 6f 64 65 73 2c 20 74 68 65 69 72 20 6d 65   codes, their me
4c3d0 61 6e 69 6e 67 73 2c 20 74 68 65 20 70 61 72 61  anings, the para
4c3e0 6d 65 74 65 72 73 0a 2a 2a 20 74 68 65 79 20 74  meters.** they t
4c3f0 61 6b 65 2c 20 61 6e 64 20 77 68 61 74 20 74 68  ake, and what th
4c400 65 79 20 64 6f 20 61 72 65 20 61 6c 6c 20 73 75  ey do are all su
4c410 62 6a 65 63 74 20 74 6f 20 63 68 61 6e 67 65 20  bject to change 
4c420 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e 0a  without notice..
4c430 2a 2a 20 55 6e 6c 69 6b 65 20 6d 6f 73 74 20 6f  ** Unlike most o
4c440 66 20 74 68 65 20 53 51 4c 69 74 65 20 41 50 49  f the SQLite API
4c450 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
4c460 69 73 20 6e 6f 74 20 67 75 61 72 61 6e 74 65 65  is not guarantee
4c470 64 20 74 6f 0a 2a 2a 20 6f 70 65 72 61 74 65 20  d to.** operate 
4c480 63 6f 6e 73 69 73 74 65 6e 74 6c 79 20 66 72 6f  consistently fro
4c490 6d 20 6f 6e 65 20 72 65 6c 65 61 73 65 20 74 6f  m one release to
4c4a0 20 74 68 65 20 6e 65 78 74 2e 0a 2a 2f 0a 53 51   the next..*/.SQ
4c4b0 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
4c4c0 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f  ite3_test_contro
4c4d0 6c 28 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 3b 0a  l(int op, ...);.
4c4e0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4c4f0 20 54 65 73 74 69 6e 67 20 49 6e 74 65 72 66 61   Testing Interfa
4c500 63 65 20 4f 70 65 72 61 74 69 6f 6e 20 43 6f 64  ce Operation Cod
4c510 65 73 20 7b 48 31 31 34 31 30 7d 20 3c 48 31 31  es {H11410} <H11
4c520 34 30 30 3e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65  400>.**.** These
4c530 20 63 6f 6e 73 74 61 6e 74 73 20 61 72 65 20 74   constants are t
4c540 68 65 20 76 61 6c 69 64 20 6f 70 65 72 61 74 69  he valid operati
4c550 6f 6e 20 63 6f 64 65 20 70 61 72 61 6d 65 74 65  on code paramete
4c560 72 73 20 75 73 65 64 0a 2a 2a 20 61 73 20 74 68  rs used.** as th
4c570 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
4c580 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 74 65 73   to [sqlite3_tes
4c590 74 5f 63 6f 6e 74 72 6f 6c 28 29 5d 2e 0a 2a 2a  t_control()]..**
4c5a0 0a 2a 2a 20 54 68 65 73 65 20 70 61 72 61 6d 65  .** These parame
4c5b0 74 65 72 73 20 61 6e 64 20 74 68 65 69 72 20 6d  ters and their m
4c5c0 65 61 6e 69 6e 67 73 20 61 72 65 20 73 75 62 6a  eanings are subj
4c5d0 65 63 74 20 74 6f 20 63 68 61 6e 67 65 0a 2a 2a  ect to change.**
4c5e0 20 77 69 74 68 6f 75 74 20 6e 6f 74 69 63 65 2e   without notice.
4c5f0 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 61    These values a
4c600 72 65 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70  re for testing p
4c610 75 72 70 6f 73 65 73 20 6f 6e 6c 79 2e 0a 2a 2a  urposes only..**
4c620 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20 73 68   Applications sh
4c630 6f 75 6c 64 20 6e 6f 74 20 75 73 65 20 61 6e 79  ould not use any
4c640 20 6f 66 20 74 68 65 73 65 20 70 61 72 61 6d 65   of these parame
4c650 74 65 72 73 20 6f 72 20 74 68 65 0a 2a 2a 20 5b  ters or the.** [
4c660 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
4c670 74 72 6f 6c 28 29 5d 20 69 6e 74 65 72 66 61 63  trol()] interfac
4c680 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
4c690 4c 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52  LITE_TESTCTRL_PR
4c6a0 4e 47 5f 53 41 56 45 20 20 20 20 20 20 20 20 20  NG_SAVE         
4c6b0 20 20 20 20 20 20 20 35 0a 23 64 65 66 69 6e 65         5.#define
4c6c0 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
4c6d0 5f 50 52 4e 47 5f 52 45 53 54 4f 52 45 20 20 20  _PRNG_RESTORE   
4c6e0 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65 66            6.#def
4c6f0 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 53 54 43  ine SQLITE_TESTC
4c700 54 52 4c 5f 50 52 4e 47 5f 52 45 53 45 54 20 20  TRL_PRNG_RESET  
4c710 20 20 20 20 20 20 20 20 20 20 20 20 20 37 0a 23               7.#
4c720 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
4c730 53 54 43 54 52 4c 5f 42 49 54 56 45 43 5f 54 45  STCTRL_BITVEC_TE
4c740 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ST              
4c750 38 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  8.#define SQLITE
4c760 5f 54 45 53 54 43 54 52 4c 5f 46 41 55 4c 54 5f  _TESTCTRL_FAULT_
4c770 49 4e 53 54 41 4c 4c 20 20 20 20 20 20 20 20 20  INSTALL         
4c780 20 20 20 39 0a 23 64 65 66 69 6e 65 20 53 51 4c     9.#define SQL
4c790 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e  ITE_TESTCTRL_BEN
4c7a0 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53  IGN_MALLOC_HOOKS
4c7b0 20 20 20 20 20 31 30 0a 0a 2f 2a 0a 2a 2a 20 43       10../*.** C
4c7c0 41 50 49 33 52 45 46 3a 20 53 51 4c 69 74 65 20  API3REF: SQLite 
4c7d0 52 75 6e 74 69 6d 65 20 53 74 61 74 75 73 20 7b  Runtime Status {
4c7e0 48 31 37 32 30 30 7d 20 3c 53 36 30 32 30 30 3e  H17200} <S60200>
4c7f0 0a 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c  .** EXPERIMENTAL
4c800 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
4c810 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f  rface is used to
4c820 20 72 65 74 72 69 65 76 65 20 72 75 6e 74 69 6d   retrieve runtim
4c830 65 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61  e status informa
4c840 74 69 6f 6e 0a 2a 2a 20 61 62 6f 75 74 20 74 68  tion.** about th
4c850 65 20 70 72 65 66 6f 72 6d 61 6e 63 65 20 6f 66  e preformance of
4c860 20 53 51 4c 69 74 65 2c 20 61 6e 64 20 6f 70 74   SQLite, and opt
4c870 69 6f 6e 61 6c 6c 79 20 74 6f 20 72 65 73 65 74  ionally to reset
4c880 20 76 61 72 69 6f 75 73 0a 2a 2a 20 68 69 67 68   various.** high
4c890 77 61 74 65 72 20 6d 61 72 6b 73 2e 20 20 54 68  water marks.  Th
4c8a0 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
4c8b0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
4c8c0 6f 64 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 73  ode for.** the s
4c8d0 70 65 63 69 66 69 63 20 70 61 72 61 6d 65 74 65  pecific paramete
4c8e0 72 20 74 6f 20 6d 65 61 73 75 72 65 2e 20 20 52  r to measure.  R
4c8f0 65 63 6f 67 6e 69 7a 65 64 20 69 6e 74 65 67 65  ecognized intege
4c900 72 20 63 6f 64 65 73 0a 2a 2a 20 61 72 65 20 6f  r codes.** are o
4c910 66 20 74 68 65 20 66 6f 72 6d 20 5b 53 51 4c 49  f the form [SQLI
4c920 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
4c930 5f 55 53 45 44 20 7c 20 53 51 4c 49 54 45 5f 53  _USED | SQLITE_S
4c940 54 41 54 55 53 5f 2e 2e 2e 5d 2e 0a 2a 2a 20 54  TATUS_...]..** T
4c950 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65  he current value
4c960 20 6f 66 20 74 68 65 20 70 61 72 61 6d 65 74 65   of the paramete
4c970 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e  r is returned in
4c980 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a  to *pCurrent..**
4c990 20 54 68 65 20 68 69 67 68 65 73 74 20 72 65 63   The highest rec
4c9a0 6f 72 64 65 64 20 76 61 6c 75 65 20 69 73 20 72  orded value is r
4c9b0 65 74 75 72 6e 65 64 20 69 6e 20 2a 70 48 69 67  eturned in *pHig
4c9c0 68 77 61 74 65 72 2e 20 20 49 66 20 74 68 65 0a  hwater.  If the.
4c9d0 2a 2a 20 72 65 73 65 74 46 6c 61 67 20 69 73 20  ** resetFlag is 
4c9e0 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20 68  true, then the h
4c9f0 69 67 68 65 73 74 20 72 65 63 6f 72 64 20 76 61  ighest record va
4ca00 6c 75 65 20 69 73 20 72 65 73 65 74 20 61 66 74  lue is reset aft
4ca10 65 72 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65  er.** *pHighwate
4ca20 72 20 69 73 20 77 72 69 74 74 65 6e 2e 20 53 6f  r is written. So
4ca30 6d 65 20 70 61 72 61 6d 65 74 65 72 73 20 64 6f  me parameters do
4ca40 20 6e 6f 74 20 72 65 63 6f 72 64 20 74 68 65 20   not record the 
4ca50 68 69 67 68 65 73 74 0a 2a 2a 20 76 61 6c 75 65  highest.** value
4ca60 2e 20 20 46 6f 72 20 74 68 6f 73 65 20 70 61 72  .  For those par
4ca70 61 6d 65 74 65 72 73 0a 2a 2a 20 6e 6f 74 68 69  ameters.** nothi
4ca80 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ng is written in
4ca90 74 6f 20 2a 70 48 69 67 68 77 61 74 65 72 20 61  to *pHighwater a
4caa0 6e 64 20 74 68 65 20 72 65 73 65 74 46 6c 61 67  nd the resetFlag
4cab0 20 69 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20   is ignored..** 
4cac0 4f 74 68 65 72 20 70 61 72 61 6d 65 74 65 72 73  Other parameters
4cad0 20 72 65 63 6f 72 64 20 6f 6e 6c 79 20 74 68 65   record only the
4cae0 20 68 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20   highwater mark 
4caf0 61 6e 64 20 6e 6f 74 20 74 68 65 20 63 75 72 72  and not the curr
4cb00 65 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20 46  ent.** value.  F
4cb10 6f 72 20 74 68 65 73 65 20 6c 61 74 74 65 72 20  or these latter 
4cb20 70 61 72 61 6d 65 74 65 72 73 20 6e 6f 74 68 69  parameters nothi
4cb30 6e 67 20 69 73 20 77 72 69 74 74 65 6e 20 69 6e  ng is written in
4cb40 74 6f 20 2a 70 43 75 72 72 65 6e 74 2e 0a 2a 2a  to *pCurrent..**
4cb50 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
4cb60 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
4cb70 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e  OK on success an
4cb80 64 20 61 20 6e 6f 6e 2d 7a 65 72 6f 0a 2a 2a 20  d a non-zero.** 
4cb90 5b 65 72 72 6f 72 20 63 6f 64 65 5d 20 6f 6e 20  [error code] on 
4cba0 66 61 69 6c 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  failure..**.** T
4cbb0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74  his routine is t
4cbc0 68 72 65 61 64 73 61 66 65 20 62 75 74 20 69 73  hreadsafe but is
4cbd0 20 6e 6f 74 20 61 74 6f 6d 69 63 2e 20 20 54 68   not atomic.  Th
4cbe0 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 0a 2a  is routine can.*
4cbf0 2a 20 63 61 6c 6c 65 64 20 77 68 69 6c 65 20 6f  * called while o
4cc00 74 68 65 72 20 74 68 72 65 61 64 73 20 61 72 65  ther threads are
4cc10 20 72 75 6e 6e 69 6e 67 20 74 68 65 20 73 61 6d   running the sam
4cc20 65 20 6f 72 20 64 69 66 66 65 72 65 6e 74 20 53  e or different S
4cc30 51 4c 69 74 65 0a 2a 2a 20 69 6e 74 65 72 66 61  QLite.** interfa
4cc40 63 65 73 2e 20 20 48 6f 77 65 76 65 72 20 74 68  ces.  However th
4cc50 65 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65  e values returne
4cc60 64 20 69 6e 20 2a 70 43 75 72 72 65 6e 74 20 61  d in *pCurrent a
4cc70 6e 64 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65  nd.** *pHighwate
4cc80 72 20 72 65 66 6c 65 63 74 20 74 68 65 20 73 74  r reflect the st
4cc90 61 74 75 73 20 6f 66 20 53 51 4c 69 74 65 20 61  atus of SQLite a
4cca0 74 20 64 69 66 66 65 72 65 6e 74 20 70 6f 69 6e  t different poin
4ccb0 74 73 20 69 6e 20 74 69 6d 65 0a 2a 2a 20 61 6e  ts in time.** an
4ccc0 64 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  d it is possible
4ccd0 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 74 68   that another th
4cce0 72 65 61 64 20 6d 69 67 68 74 20 63 68 61 6e 67  read might chang
4ccf0 65 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 0a  e the parameter.
4cd00 2a 2a 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68  ** in between th
4cd10 65 20 74 69 6d 65 73 20 77 68 65 6e 20 2a 70 43  e times when *pC
4cd20 75 72 72 65 6e 74 20 61 6e 64 20 2a 70 48 69 67  urrent and *pHig
4cd30 68 77 61 74 65 72 20 61 72 65 20 77 72 69 74 74  hwater are writt
4cd40 65 6e 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c  en..**.** See al
4cd50 73 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 64 62 5f  so: [sqlite3_db_
4cd60 73 74 61 74 75 73 28 29 5d 0a 2a 2f 0a 53 51 4c  status()].*/.SQL
4cd70 49 54 45 5f 41 50 49 20 53 51 4c 49 54 45 5f 45  ITE_API SQLITE_E
4cd80 58 50 45 52 49 4d 45 4e 54 41 4c 20 69 6e 74 20  XPERIMENTAL int 
4cd90 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 69  sqlite3_status(i
4cda0 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72  nt op, int *pCur
4cdb0 72 65 6e 74 2c 20 69 6e 74 20 2a 70 48 69 67 68  rent, int *pHigh
4cdc0 77 61 74 65 72 2c 20 69 6e 74 20 72 65 73 65 74  water, int reset
4cdd0 46 6c 61 67 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43  Flag);.../*.** C
4cde0 41 50 49 33 52 45 46 3a 20 53 74 61 74 75 73 20  API3REF: Status 
4cdf0 50 61 72 61 6d 65 74 65 72 73 20 7b 48 31 37 32  Parameters {H172
4ce00 35 30 7d 20 3c 48 31 37 32 30 30 3e 0a 2a 2a 20  50} <H17200>.** 
4ce10 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
4ce20 2a 2a 20 54 68 65 73 65 20 69 6e 74 65 67 65 72  ** These integer
4ce30 20 63 6f 6e 73 74 61 6e 74 73 20 64 65 73 69 67   constants desig
4ce40 6e 61 74 65 20 76 61 72 69 6f 75 73 20 72 75 6e  nate various run
4ce50 2d 74 69 6d 65 20 73 74 61 74 75 73 20 70 61 72  -time status par
4ce60 61 6d 65 74 65 72 73 0a 2a 2a 20 74 68 61 74 20  ameters.** that 
4ce70 63 61 6e 20 62 65 20 72 65 74 75 72 6e 65 64 20  can be returned 
4ce80 62 79 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  by [sqlite3_stat
4ce90 75 73 28 29 5d 2e 0a 2a 2a 0a 2a 2a 20 3c 64 6c  us()]..**.** <dl
4cea0 3e 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f  >.** <dt>SQLITE_
4ceb0 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
4cec0 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ED</dt>.** <dd>T
4ced0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 69 73  his parameter is
4cee0 20 74 68 65 20 63 75 72 72 65 6e 74 20 61 6d 6f   the current amo
4cef0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 20 63 68  unt of memory ch
4cf00 65 63 6b 65 64 20 6f 75 74 0a 2a 2a 20 75 73 69  ecked out.** usi
4cf10 6e 67 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  ng [sqlite3_mall
4cf20 6f 63 28 29 5d 2c 20 65 69 74 68 65 72 20 64 69  oc()], either di
4cf30 72 65 63 74 6c 79 20 6f 72 20 69 6e 64 69 72 65  rectly or indire
4cf40 63 74 6c 79 2e 20 20 54 68 65 0a 2a 2a 20 66 69  ctly.  The.** fi
4cf50 67 75 72 65 20 69 6e 63 6c 75 64 65 73 20 63 61  gure includes ca
4cf60 6c 6c 73 20 6d 61 64 65 20 74 6f 20 5b 73 71 6c  lls made to [sql
4cf70 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 20 62  ite3_malloc()] b
4cf80 79 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f  y the applicatio
4cf90 6e 0a 2a 2a 20 61 6e 64 20 69 6e 74 65 72 6e 61  n.** and interna
4cfa0 6c 20 6d 65 6d 6f 72 79 20 75 73 61 67 65 20 62  l memory usage b
4cfb0 79 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  y the SQLite lib
4cfc0 72 61 72 79 2e 20 20 53 63 72 61 74 63 68 20 6d  rary.  Scratch m
4cfd0 65 6d 6f 72 79 0a 2a 2a 20 63 6f 6e 74 72 6f 6c  emory.** control
4cfe0 6c 65 64 20 62 79 20 5b 53 51 4c 49 54 45 5f 43  led by [SQLITE_C
4cff0 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 5d 20 61  ONFIG_SCRATCH] a
4d000 6e 64 20 61 75 78 69 6c 69 61 72 79 20 70 61 67  nd auxiliary pag
4d010 65 2d 63 61 63 68 65 0a 2a 2a 20 6d 65 6d 6f 72  e-cache.** memor
4d020 79 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79 20  y controlled by 
4d030 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50  [SQLITE_CONFIG_P
4d040 41 47 45 43 41 43 48 45 5d 20 69 73 20 6e 6f 74  AGECACHE] is not
4d050 20 69 6e 63 6c 75 64 65 64 20 69 6e 0a 2a 2a 20   included in.** 
4d060 74 68 69 73 20 70 61 72 61 6d 65 74 65 72 2e 20  this parameter. 
4d070 20 54 68 65 20 61 6d 6f 75 6e 74 20 72 65 74 75   The amount retu
4d080 72 6e 65 64 20 69 73 20 74 68 65 20 73 75 6d 20  rned is the sum 
4d090 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
4d0a0 6e 0a 2a 2a 20 73 69 7a 65 73 20 61 73 20 72 65  n.** sizes as re
4d0b0 70 6f 72 74 65 64 20 62 79 20 74 68 65 20 78 53  ported by the xS
4d0c0 69 7a 65 20 6d 65 74 68 6f 64 20 69 6e 20 5b 73  ize method in [s
4d0d0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
4d0e0 64 73 5d 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ds].</dd>.**.** 
4d0f0 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
4d100 53 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 3c 2f 64  S_MALLOC_SIZE</d
4d110 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70  t>.** <dd>This p
4d120 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64 73  arameter records
4d130 20 74 68 65 20 6c 61 72 67 65 73 74 20 6d 65 6d   the largest mem
4d140 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72  ory allocation r
4d150 65 71 75 65 73 74 0a 2a 2a 20 68 61 6e 64 65 64  equest.** handed
4d160 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 6d 61 6c   to [sqlite3_mal
4d170 6c 6f 63 28 29 5d 20 6f 72 20 5b 73 71 6c 69 74  loc()] or [sqlit
4d180 65 33 5f 72 65 61 6c 6c 6f 63 28 29 5d 20 28 6f  e3_realloc()] (o
4d190 72 20 74 68 65 69 72 0a 2a 2a 20 69 6e 74 65 72  r their.** inter
4d1a0 6e 61 6c 20 65 71 75 69 76 61 6c 65 6e 74 73 29  nal equivalents)
4d1b0 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61 6c 75  .  Only the valu
4d1c0 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20 74 68  e returned in th
4d1d0 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74 65 72  e.** *pHighwater
4d1e0 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 5b 73   parameter to [s
4d1f0 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29 5d  qlite3_status()]
4d200 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73 74 2e   is of interest.
4d210 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20    .** The value 
4d220 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
4d230 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72 61 6d   *pCurrent param
4d240 65 74 65 72 20 69 73 20 75 6e 64 65 66 69 6e 65  eter is undefine
4d250 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  d.</dd>.**.** <d
4d260 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t>SQLITE_STATUS_
4d270 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 3c 2f  PAGECACHE_USED</
4d280 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
4d290 70 61 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e  parameter return
4d2a0 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
4d2b0 70 61 67 65 73 20 75 73 65 64 20 6f 75 74 20 6f  pages used out o
4d2c0 66 20 74 68 65 0a 2a 2a 20 5b 70 61 67 65 63 61  f the.** [pageca
4d2d0 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  che memory alloc
4d2e0 61 74 6f 72 5d 20 74 68 61 74 20 77 61 73 20 63  ator] that was c
4d2f0 6f 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 20  onfigured using 
4d300 0a 2a 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46  .** [SQLITE_CONF
4d310 49 47 5f 50 41 47 45 43 41 43 48 45 5d 2e 20 20  IG_PAGECACHE].  
4d320 54 68 65 0a 2a 2a 20 76 61 6c 75 65 20 72 65 74  The.** value ret
4d330 75 72 6e 65 64 20 69 73 20 69 6e 20 70 61 67 65  urned is in page
4d340 73 2c 20 6e 6f 74 20 69 6e 20 62 79 74 65 73 2e  s, not in bytes.
4d350 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
4d360 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 50 41  SQLITE_STATUS_PA
4d370 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c 4f 57  GECACHE_OVERFLOW
4d380 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
4d390 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
4d3a0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
4d3b0 66 20 62 79 74 65 73 20 6f 66 20 70 61 67 65 20  f bytes of page 
4d3c0 63 61 63 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74  cache.** allocat
4d3d0 69 6f 6e 20 77 68 69 63 68 20 63 6f 75 6c 64 20  ion which could 
4d3e0 6e 6f 74 20 62 65 20 73 74 61 74 69 73 66 69 65  not be statisfie
4d3f0 64 20 62 79 20 74 68 65 20 5b 53 51 4c 49 54 45  d by the [SQLITE
4d400 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
4d410 45 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61 6e 64  E].** buffer and
4d420 20 77 68 65 72 65 20 66 6f 72 63 65 64 20 74 6f   where forced to
4d430 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b 73 71   overflow to [sq
4d440 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 5d 2e  lite3_malloc()].
4d450 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 65    The.** returne
4d460 64 20 76 61 6c 75 65 20 69 6e 63 6c 75 64 65 73  d value includes
4d470 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 74 68 61   allocations tha
4d480 74 20 6f 76 65 72 66 6c 6f 77 65 64 20 62 65 63  t overflowed bec
4d490 61 75 73 65 20 74 68 65 79 0a 2a 2a 20 77 68 65  ause they.** whe
4d4a0 72 65 20 74 6f 6f 20 6c 61 72 67 65 20 28 74 68  re too large (th
4d4b0 65 79 20 77 65 72 65 20 6c 61 72 67 65 72 20 74  ey were larger t
4d4c0 68 61 6e 20 74 68 65 20 22 73 7a 22 20 70 61 72  han the "sz" par
4d4d0 61 6d 65 74 65 72 20 74 6f 0a 2a 2a 20 5b 53 51  ameter to.** [SQ
4d4e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45  LITE_CONFIG_PAGE
4d4f0 43 41 43 48 45 5d 29 20 61 6e 64 20 61 6c 6c 6f  CACHE]) and allo
4d500 63 61 74 69 6f 6e 73 20 74 68 61 74 20 6f 76 65  cations that ove
4d510 72 66 6c 6f 77 65 64 20 62 65 63 61 75 73 65 0a  rflowed because.
4d520 2a 2a 20 6e 6f 20 73 70 61 63 65 20 77 61 73 20  ** no space was 
4d530 6c 65 66 74 20 69 6e 20 74 68 65 20 70 61 67 65  left in the page
4d540 20 63 61 63 68 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a   cache.</dd>.**.
4d550 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
4d560 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 53  ATUS_PAGECACHE_S
4d570 49 5a 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e  IZE</dt>.** <dd>
4d580 54 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72  This parameter r
4d590 65 63 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65  ecords the large
4d5a0 73 74 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  st memory alloca
4d5b0 74 69 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20  tion request.** 
4d5c0 68 61 6e 64 65 64 20 74 6f 20 5b 70 61 67 65 63  handed to [pagec
4d5d0 61 63 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ache memory allo
4d5e0 63 61 74 6f 72 5d 2e 20 20 4f 6e 6c 79 20 74 68  cator].  Only th
4d5f0 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64  e value returned
4d600 20 69 6e 20 74 68 65 0a 2a 2a 20 2a 70 48 69 67   in the.** *pHig
4d610 68 77 61 74 65 72 20 70 61 72 61 6d 65 74 65 72  hwater parameter
4d620 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 73 74 61   to [sqlite3_sta
4d630 74 75 73 28 29 5d 20 69 73 20 6f 66 20 69 6e 74  tus()] is of int
4d640 65 72 65 73 74 2e 20 20 0a 2a 2a 20 54 68 65 20  erest.  .** The 
4d650 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 69 6e  value written in
4d660 74 6f 20 74 68 65 20 2a 70 43 75 72 72 65 6e 74  to the *pCurrent
4d670 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 75 6e   parameter is un
4d680 64 65 66 69 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a  defined.</dd>.**
4d690 0a 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53  .** <dt>SQLITE_S
4d6a0 54 41 54 55 53 5f 53 43 52 41 54 43 48 5f 55 53  TATUS_SCRATCH_US
4d6b0 45 44 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54  ED</dt>.** <dd>T
4d6c0 68 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65  his parameter re
4d6d0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
4d6e0 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20   of allocations 
4d6f0 75 73 65 64 20 6f 75 74 20 6f 66 20 74 68 65 0a  used out of the.
4d700 2a 2a 20 5b 73 63 72 61 74 63 68 20 6d 65 6d 6f  ** [scratch memo
4d710 72 79 20 61 6c 6c 6f 63 61 74 6f 72 5d 20 63 6f  ry allocator] co
4d720 6e 66 69 67 75 72 65 64 20 75 73 69 6e 67 0a 2a  nfigured using.*
4d730 2a 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  * [SQLITE_CONFIG
4d740 5f 53 43 52 41 54 43 48 5d 2e 20 20 54 68 65 20  _SCRATCH].  The 
4d750 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
4d760 73 20 69 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 73  s in allocations
4d770 2c 20 6e 6f 74 0a 2a 2a 20 69 6e 20 62 79 74 65  , not.** in byte
4d780 73 2e 20 20 53 69 6e 63 65 20 61 20 73 69 6e 67  s.  Since a sing
4d790 6c 65 20 74 68 72 65 61 64 20 6d 61 79 20 6f 6e  le thread may on
4d7a0 6c 79 20 68 61 76 65 20 6f 6e 65 20 73 63 72 61  ly have one scra
4d7b0 74 63 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a  tch allocation.*
4d7c0 2a 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74  * outstanding at
4d7d0 20 74 69 6d 65 2c 20 74 68 69 73 20 70 61 72 61   time, this para
4d7e0 6d 65 74 65 72 20 61 6c 73 6f 20 72 65 70 6f 72  meter also repor
4d7f0 74 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ts the number of
4d800 20 74 68 72 65 61 64 73 0a 2a 2a 20 75 73 69 6e   threads.** usin
4d810 67 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79  g scratch memory
4d820 20 61 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d   at the same tim
4d830 65 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64  e.</dd>.**.** <d
4d840 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t>SQLITE_STATUS_
4d850 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57  SCRATCH_OVERFLOW
4d860 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69  </dt>.** <dd>Thi
4d870 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 74 75  s parameter retu
4d880 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
4d890 66 20 62 79 74 65 73 20 6f 66 20 73 63 72 61 74  f bytes of scrat
4d8a0 63 68 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c 6c  ch memory.** all
4d8b0 6f 63 61 74 69 6f 6e 20 77 68 69 63 68 20 63 6f  ocation which co
4d8c0 75 6c 64 20 6e 6f 74 20 62 65 20 73 74 61 74 69  uld not be stati
4d8d0 73 66 69 65 64 20 62 79 20 74 68 65 20 5b 53 51  sfied by the [SQ
4d8e0 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 53 43 52 41  LITE_CONFIG_SCRA
4d8f0 54 43 48 5d 0a 2a 2a 20 62 75 66 66 65 72 20 61  TCH].** buffer a
4d900 6e 64 20 77 68 65 72 65 20 66 6f 72 63 65 64 20  nd where forced 
4d910 74 6f 20 6f 76 65 72 66 6c 6f 77 20 74 6f 20 5b  to overflow to [
4d920 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
4d930 5d 2e 20 20 54 68 65 20 76 61 6c 75 65 73 0a 2a  ].  The values.*
4d940 2a 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75  * returned inclu
4d950 64 65 20 6f 76 65 72 66 6c 6f 77 73 20 62 65 63  de overflows bec
4d960 61 75 73 65 20 74 68 65 20 72 65 71 75 65 73 74  ause the request
4d970 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61  ed allocation wa
4d980 73 20 74 6f 6f 0a 2a 2a 20 6c 61 72 67 65 72 20  s too.** larger 
4d990 28 74 68 61 74 20 69 73 2c 20 62 65 63 61 75 73  (that is, becaus
4d9a0 65 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  e the requested 
4d9b0 61 6c 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 6c  allocation was l
4d9c0 61 72 67 65 72 20 74 68 61 6e 20 74 68 65 0a 2a  arger than the.*
4d9d0 2a 20 22 73 7a 22 20 70 61 72 61 6d 65 74 65 72  * "sz" parameter
4d9e0 20 74 6f 20 5b 53 51 4c 49 54 45 5f 43 4f 4e 46   to [SQLITE_CONF
4d9f0 49 47 5f 53 43 52 41 54 43 48 5d 29 20 61 6e 64  IG_SCRATCH]) and
4da00 20 62 65 63 61 75 73 65 20 6e 6f 20 73 63 72 61   because no scra
4da10 74 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 73 6c  tch buffer.** sl
4da20 6f 74 73 20 77 65 72 65 20 61 76 61 69 6c 61 62  ots were availab
4da30 6c 65 2e 0a 2a 2a 20 3c 2f 64 64 3e 0a 2a 2a 0a  le..** </dd>.**.
4da40 2a 2a 20 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54  ** <dt>SQLITE_ST
4da50 41 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a  ATUS_SCRATCH_SIZ
4da60 45 3c 2f 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68  E</dt>.** <dd>Th
4da70 69 73 20 70 61 72 61 6d 65 74 65 72 20 72 65 63  is parameter rec
4da80 6f 72 64 73 20 74 68 65 20 6c 61 72 67 65 73 74  ords the largest
4da90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
4daa0 6f 6e 20 72 65 71 75 65 73 74 0a 2a 2a 20 68 61  on request.** ha
4dab0 6e 64 65 64 20 74 6f 20 5b 73 63 72 61 74 63 68  nded to [scratch
4dac0 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
4dad0 72 5d 2e 20 20 4f 6e 6c 79 20 74 68 65 20 76 61  r].  Only the va
4dae0 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e 20  lue returned in 
4daf0 74 68 65 0a 2a 2a 20 2a 70 48 69 67 68 77 61 74  the.** *pHighwat
4db00 65 72 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  er parameter to 
4db10 5b 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28  [sqlite3_status(
4db20 29 5d 20 69 73 20 6f 66 20 69 6e 74 65 72 65 73  )] is of interes
4db30 74 2e 20 20 0a 2a 2a 20 54 68 65 20 76 61 6c 75  t.  .** The valu
4db40 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  e written into t
4db50 68 65 20 2a 70 43 75 72 72 65 6e 74 20 70 61 72  he *pCurrent par
4db60 61 6d 65 74 65 72 20 69 73 20 75 6e 64 65 66 69  ameter is undefi
4db70 6e 65 64 2e 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20  ned.</dd>.**.** 
4db80 3c 64 74 3e 53 51 4c 49 54 45 5f 53 54 41 54 55  <dt>SQLITE_STATU
4db90 53 5f 50 41 52 53 45 52 5f 53 54 41 43 4b 3c 2f  S_PARSER_STACK</
4dba0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
4dbb0 70 61 72 61 6d 65 74 65 72 20 72 65 63 6f 72 64  parameter record
4dbc0 73 20 74 68 65 20 64 65 65 70 65 73 74 20 70 61  s the deepest pa
4dbd0 72 73 65 72 20 73 74 61 63 6b 2e 20 20 49 74 20  rser stack.  It 
4dbe0 69 73 20 6f 6e 6c 79 0a 2a 2a 20 6d 65 61 6e 69  is only.** meani
4dbf0 6e 67 66 75 6c 20 69 66 20 53 51 4c 69 74 65 20  ngful if SQLite 
4dc00 69 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  is compiled with
4dc10 20 5b 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43   [YYTRACKMAXSTAC
4dc20 4b 44 45 50 54 48 5d 2e 3c 2f 64 64 3e 0a 2a 2a  KDEPTH].</dd>.**
4dc30 20 3c 2f 64 6c 3e 0a 2a 2a 0a 2a 2a 20 4e 65 77   </dl>.**.** New
4dc40 20 73 74 61 74 75 73 20 70 61 72 61 6d 65 74 65   status paramete
4dc50 72 73 20 6d 61 79 20 62 65 20 61 64 64 65 64 20  rs may be added 
4dc60 66 72 6f 6d 20 74 69 6d 65 20 74 6f 20 74 69 6d  from time to tim
4dc70 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  e..*/.#define SQ
4dc80 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f  LITE_STATUS_MEMO
4dc90 52 59 5f 55 53 45 44 20 20 20 20 20 20 20 20 20  RY_USED         
4dca0 20 30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54   0.#define SQLIT
4dcb0 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
4dcc0 48 45 5f 55 53 45 44 20 20 20 20 20 20 20 31 0a  HE_USED       1.
4dcd0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53  #define SQLITE_S
4dce0 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
4dcf0 4f 56 45 52 46 4c 4f 57 20 20 20 32 0a 23 64 65  OVERFLOW   2.#de
4dd00 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41 54  fine SQLITE_STAT
4dd10 55 53 5f 53 43 52 41 54 43 48 5f 55 53 45 44 20  US_SCRATCH_USED 
4dd20 20 20 20 20 20 20 20 20 33 0a 23 64 65 66 69 6e          3.#defin
4dd30 65 20 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e SQLITE_STATUS_
4dd40 53 43 52 41 54 43 48 5f 4f 56 45 52 46 4c 4f 57  SCRATCH_OVERFLOW
4dd50 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20 53       4.#define S
4dd60 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 41 4c  QLITE_STATUS_MAL
4dd70 4c 4f 43 5f 53 49 5a 45 20 20 20 20 20 20 20 20  LOC_SIZE        
4dd80 20 20 35 0a 23 64 65 66 69 6e 65 20 53 51 4c 49    5.#define SQLI
4dd90 54 45 5f 53 54 41 54 55 53 5f 50 41 52 53 45 52  TE_STATUS_PARSER
4dda0 5f 53 54 41 43 4b 20 20 20 20 20 20 20 20 20 36  _STACK         6
4ddb0 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
4ddc0 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
4ddd0 5f 53 49 5a 45 20 20 20 20 20 20 20 37 0a 23 64  _SIZE       7.#d
4dde0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 41  efine SQLITE_STA
4ddf0 54 55 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45  TUS_SCRATCH_SIZE
4de00 20 20 20 20 20 20 20 20 20 38 0a 0a 2f 2a 0a 2a           8../*.*
4de10 2a 20 43 41 50 49 33 52 45 46 3a 20 44 61 74 61  * CAPI3REF: Data
4de20 62 61 73 65 20 43 6f 6e 6e 65 63 74 69 6f 6e 20  base Connection 
4de30 53 74 61 74 75 73 20 7b 48 31 37 35 30 30 7d 20  Status {H17500} 
4de40 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58 50 45  <S60200>.** EXPE
4de50 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54  RIMENTAL.**.** T
4de60 68 69 73 20 69 6e 74 65 72 66 61 63 65 20 69 73  his interface is
4de70 20 75 73 65 64 20 74 6f 20 72 65 74 72 69 65 76   used to retriev
4de80 65 20 72 75 6e 74 69 6d 65 20 73 74 61 74 75 73  e runtime status
4de90 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 0a 2a 2a   information .**
4dea0 20 61 62 6f 75 74 20 61 20 73 69 6e 67 6c 65 20   about a single 
4deb0 5b 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63  [database connec
4dec0 74 69 6f 6e 5d 2e 20 20 54 68 65 20 66 69 72 73  tion].  The firs
4ded0 74 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  t argument is th
4dee0 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
4def0 6e 6e 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 20  nnection object 
4df00 74 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74  to be interrogat
4df10 65 64 2e 20 20 54 68 65 20 73 65 63 6f 6e 64 20  ed.  The second 
4df20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 69 73 20 74  argument.** is t
4df30 68 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20  he parameter to 
4df40 69 6e 74 65 72 72 6f 67 61 74 65 2e 20 20 43 75  interrogate.  Cu
4df50 72 72 65 6e 74 6c 79 2c 20 74 68 65 20 6f 6e 6c  rrently, the onl
4df60 79 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75 65 0a  y allowed value.
4df70 2a 2a 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e  ** for the secon
4df80 64 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 5b  d parameter is [
4df90 53 51 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f  SQLITE_DBSTATUS_
4dfa0 4c 4f 4f 4b 41 53 49 44 45 5f 55 53 45 44 5d 2e  LOOKASIDE_USED].
4dfb0 0a 2a 2a 20 41 64 64 69 74 69 6f 6e 61 6c 20 6f  .** Additional o
4dfc0 70 74 69 6f 6e 73 20 77 69 6c 6c 20 6c 69 6b 65  ptions will like
4dfd0 6c 79 20 61 70 70 65 61 72 20 69 6e 20 66 75 74  ly appear in fut
4dfe0 75 72 65 20 72 65 6c 65 61 73 65 73 20 6f 66 20  ure releases of 
4dff0 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  SQLite..**.** Th
4e000 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
4e010 6f 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64  of the requested
4e020 20 70 61 72 61 6d 65 74 65 72 20 69 73 20 77 72   parameter is wr
4e030 69 74 74 65 6e 20 69 6e 74 6f 20 2a 70 43 75 72  itten into *pCur
4e040 0a 2a 2a 20 61 6e 64 20 74 68 65 20 68 69 67 68  .** and the high
4e050 65 73 74 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75  est instantaneou
4e060 73 20 76 61 6c 75 65 20 69 73 20 77 72 69 74 74  s value is writt
4e070 65 6e 20 69 6e 74 6f 20 2a 70 48 69 77 74 72 2e  en into *pHiwtr.
4e080 20 20 49 66 0a 2a 2a 20 74 68 65 20 72 65 73 65    If.** the rese
4e090 74 46 6c 67 20 69 73 20 74 72 75 65 2c 20 74 68  tFlg is true, th
4e0a0 65 6e 20 74 68 65 20 68 69 67 68 65 73 74 20 69  en the highest i
4e0b0 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 76 61 6c  nstantaneous val
4e0c0 75 65 20 69 73 0a 2a 2a 20 72 65 73 65 74 20 62  ue is.** reset b
4e0d0 61 63 6b 20 64 6f 77 6e 20 74 6f 20 74 68 65 20  ack down to the 
4e0e0 63 75 72 72 65 6e 74 20 76 61 6c 75 65 2e 0a 2a  current value..*
4e0f0 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 5b  *.** See also: [
4e100 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 28 29  sqlite3_status()
4e110 5d 20 61 6e 64 20 5b 73 71 6c 69 74 65 33 5f 73  ] and [sqlite3_s
4e120 74 6d 74 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a  tmt_status()]..*
4e130 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53 51 4c  /.SQLITE_API SQL
4e140 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54 41 4c  ITE_EXPERIMENTAL
4e150 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 64 62 5f   int sqlite3_db_
4e160 73 74 61 74 75 73 28 73 71 6c 69 74 65 33 2a 2c  status(sqlite3*,
4e170 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20 2a 70 43   int op, int *pC
4e180 75 72 2c 20 69 6e 74 20 2a 70 48 69 77 74 72 2c  ur, int *pHiwtr,
4e190 20 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a   int resetFlg);.
4e1a0 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4e1b0 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65   Status Paramete
4e1c0 72 73 20 66 6f 72 20 64 61 74 61 62 61 73 65 20  rs for database 
4e1d0 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 7b 48 31 37  connections {H17
4e1e0 35 32 30 7d 20 3c 48 31 37 35 30 30 3e 0a 2a 2a  520} <H17500>.**
4e1f0 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a   EXPERIMENTAL.**
4e200 0a 2a 2a 20 53 74 61 74 75 73 20 76 65 72 62 73  .** Status verbs
4e210 20 66 6f 72 20 5b 73 71 6c 69 74 65 33 5f 64 62   for [sqlite3_db
4e220 5f 73 74 61 74 75 73 28 29 5d 2e 0a 2a 2a 0a 2a  _status()]..**.*
4e230 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53 51  * <dl>.** <dt>SQ
4e240 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f  LITE_DBSTATUS_LO
4e250 4f 4b 41 53 49 44 45 5f 55 53 45 44 3c 2f 64 74  OKASIDE_USED</dt
4e260 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20 70 61  >.** <dd>This pa
4e270 72 61 6d 65 74 65 72 20 72 65 74 75 72 6e 73 20  rameter returns 
4e280 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6c 6f  the number of lo
4e290 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 73  okaside memory s
4e2a0 6c 6f 74 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a  lots currently.*
4e2b0 2a 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 3c 2f  * checked out.</
4e2c0 64 64 3e 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a  dd>.** </dl>.*/.
4e2d0 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 44  #define SQLITE_D
4e2e0 42 53 54 41 54 55 53 5f 4c 4f 4f 4b 41 53 49 44  BSTATUS_LOOKASID
4e2f0 45 5f 55 53 45 44 20 20 20 20 20 30 0a 0a 0a 2f  E_USED     0.../
4e300 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20 50  *.** CAPI3REF: P
4e310 72 65 70 61 72 65 64 20 53 74 61 74 65 6d 65 6e  repared Statemen
4e320 74 20 53 74 61 74 75 73 20 7b 48 31 37 35 35 30  t Status {H17550
4e330 7d 20 3c 53 36 30 32 30 30 3e 0a 2a 2a 20 45 58  } <S60200>.** EX
4e340 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a 2a 2a  PERIMENTAL.**.**
4e350 20 45 61 63 68 20 70 72 65 70 61 72 65 64 20 73   Each prepared s
4e360 74 61 74 65 6d 65 6e 74 20 6d 61 69 6e 74 61 69  tatement maintai
4e370 6e 73 20 76 61 72 69 6f 75 73 0a 2a 2a 20 5b 53  ns various.** [S
4e380 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4e390 5f 53 4f 52 54 20 7c 20 63 6f 75 6e 74 65 72 73  _SORT | counters
4e3a0 5d 20 74 68 61 74 20 6d 65 61 73 75 72 65 20 74  ] that measure t
4e3b0 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
4e3c0 74 69 6d 65 73 20 69 74 20 68 61 73 20 70 65 72  times it has per
4e3d0 66 6f 72 6d 65 64 20 73 70 65 63 69 66 69 63 20  formed specific 
4e3e0 6f 70 65 72 61 74 69 6f 6e 73 2e 20 20 54 68 65  operations.  The
4e3f0 73 65 20 63 6f 75 6e 74 65 72 73 20 63 61 6e 0a  se counters can.
4e400 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 6d 6f  ** be used to mo
4e410 6e 69 74 6f 72 20 74 68 65 20 70 65 72 66 6f 72  nitor the perfor
4e420 6d 61 6e 63 65 20 63 68 61 72 61 63 74 65 72 69  mance characteri
4e430 73 74 69 63 73 20 6f 66 20 74 68 65 20 70 72 65  stics of the pre
4e440 70 61 72 65 64 0a 2a 2a 20 73 74 61 74 65 6d 65  pared.** stateme
4e450 6e 74 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  nts.  For exampl
4e460 65 2c 20 69 66 20 74 68 65 20 6e 75 6d 62 65 72  e, if the number
4e470 20 6f 66 20 74 61 62 6c 65 20 73 74 65 70 73 20   of table steps 
4e480 67 72 65 61 74 6c 79 20 65 78 63 65 65 64 73 0a  greatly exceeds.
4e490 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** the number of
4e4a0 20 74 61 62 6c 65 20 73 65 61 72 63 68 65 73 20   table searches 
4e4b0 6f 72 20 72 65 73 75 6c 74 20 72 6f 77 73 2c 20  or result rows, 
4e4c0 74 68 61 74 20 77 6f 75 6c 64 20 74 65 6e 64 20  that would tend 
4e4d0 74 6f 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74  to indicate.** t
4e4e0 68 61 74 20 74 68 65 20 70 72 65 70 61 72 65 64  hat the prepared
4e4f0 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 75 73   statement is us
4e500 69 6e 67 20 61 20 66 75 6c 6c 20 74 61 62 6c 65  ing a full table
4e510 20 73 63 61 6e 20 72 61 74 68 65 72 20 74 68 61   scan rather tha
4e520 6e 0a 2a 2a 20 61 6e 20 69 6e 64 65 78 2e 20 20  n.** an index.  
4e530 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 74 65  .**.** This inte
4e540 72 66 61 63 65 20 69 73 20 75 73 65 64 20 74 6f  rface is used to
4e550 20 72 65 74 72 69 65 76 65 20 61 6e 64 20 72 65   retrieve and re
4e560 73 65 74 20 63 6f 75 6e 74 65 72 20 76 61 6c 75  set counter valu
4e570 65 73 20 66 72 6f 6d 0a 2a 2a 20 61 20 5b 70 72  es from.** a [pr
4e580 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
4e590 5d 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72  ].  The first ar
4e5a0 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 70 72  gument is the pr
4e5b0 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
4e5c0 0a 2a 2a 20 6f 62 6a 65 63 74 20 74 6f 20 62 65  .** object to be
4e5d0 20 69 6e 74 65 72 72 6f 67 61 74 65 64 2e 20 20   interrogated.  
4e5e0 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  The second argum
4e5f0 65 6e 74 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 74  ent.** is an int
4e600 65 67 65 72 20 63 6f 64 65 20 66 6f 72 20 61 20  eger code for a 
4e610 73 70 65 63 69 66 69 63 20 5b 53 51 4c 49 54 45  specific [SQLITE
4e620 5f 53 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54  _STMTSTATUS_SORT
4e630 20 7c 20 63 6f 75 6e 74 65 72 5d 0a 2a 2a 20 74   | counter].** t
4e640 6f 20 62 65 20 69 6e 74 65 72 72 6f 67 61 74 65  o be interrogate
4e650 64 2e 20 0a 2a 2a 20 54 68 65 20 63 75 72 72 65  d. .** The curre
4e660 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  nt value of the 
4e670 72 65 71 75 65 73 74 65 64 20 63 6f 75 6e 74 65  requested counte
4e680 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
4e690 2a 20 49 66 20 74 68 65 20 72 65 73 65 74 46 6c  * If the resetFl
4e6a0 67 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  g is true, then 
4e6b0 74 68 65 20 63 6f 75 6e 74 65 72 20 69 73 20 72  the counter is r
4e6c0 65 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 66 74  eset to zero aft
4e6d0 65 72 20 74 68 69 73 0a 2a 2a 20 69 6e 74 65 72  er this.** inter
4e6e0 66 61 63 65 20 63 61 6c 6c 20 72 65 74 75 72 6e  face call return
4e6f0 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  s..**.** See als
4e700 6f 3a 20 5b 73 71 6c 69 74 65 33 5f 73 74 61 74  o: [sqlite3_stat
4e710 75 73 28 29 5d 20 61 6e 64 20 5b 73 71 6c 69 74  us()] and [sqlit
4e720 65 33 5f 64 62 5f 73 74 61 74 75 73 28 29 5d 2e  e3_db_status()].
4e730 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 53  .*/.SQLITE_API S
4e740 51 4c 49 54 45 5f 45 58 50 45 52 49 4d 45 4e 54  QLITE_EXPERIMENT
4e750 41 4c 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73  AL int sqlite3_s
4e760 74 6d 74 5f 73 74 61 74 75 73 28 73 71 6c 69 74  tmt_status(sqlit
4e770 65 33 5f 73 74 6d 74 2a 2c 20 69 6e 74 20 6f 70  e3_stmt*, int op
4e780 2c 69 6e 74 20 72 65 73 65 74 46 6c 67 29 3b 0a  ,int resetFlg);.
4e790 0a 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a  ./*.** CAPI3REF:
4e7a0 20 53 74 61 74 75 73 20 50 61 72 61 6d 65 74 65   Status Paramete
4e7b0 72 73 20 66 6f 72 20 70 72 65 70 61 72 65 64 20  rs for prepared 
4e7c0 73 74 61 74 65 6d 65 6e 74 73 20 7b 48 31 37 35  statements {H175
4e7d0 37 30 7d 20 3c 48 31 37 35 35 30 3e 0a 2a 2a 20  70} <H17550>.** 
4e7e0 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a 2a 2a 0a  EXPERIMENTAL.**.
4e7f0 2a 2a 20 54 68 65 73 65 20 70 72 65 70 72 6f 63  ** These preproc
4e800 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 64 65 66  essor macros def
4e810 69 6e 65 20 69 6e 74 65 67 65 72 20 63 6f 64 65  ine integer code
4e820 73 20 74 68 61 74 20 6e 61 6d 65 20 63 6f 75 6e  s that name coun
4e830 74 65 72 0a 2a 2a 20 76 61 6c 75 65 73 20 61 73  ter.** values as
4e840 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
4e850 65 20 5b 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  e [sqlite3_stmt_
4e860 73 74 61 74 75 73 28 29 5d 20 69 6e 74 65 72 66  status()] interf
4e870 61 63 65 2e 0a 2a 2a 20 54 68 65 20 6d 65 61 6e  ace..** The mean
4e880 69 6e 67 73 20 6f 66 20 74 68 65 20 76 61 72 69  ings of the vari
4e890 6f 75 73 20 63 6f 75 6e 74 65 72 73 20 61 72 65  ous counters are
4e8a0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a   as follows:.**.
4e8b0 2a 2a 20 3c 64 6c 3e 0a 2a 2a 20 3c 64 74 3e 53  ** <dl>.** <dt>S
4e8c0 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55 53  QLITE_STMTSTATUS
4e8d0 5f 46 55 4c 4c 53 43 41 4e 5f 53 54 45 50 3c 2f  _FULLSCAN_STEP</
4e8e0 64 74 3e 0a 2a 2a 20 3c 64 64 3e 54 68 69 73 20  dt>.** <dd>This 
4e8f0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
4e900 20 74 69 6d 65 73 20 74 68 61 74 20 53 51 4c 69   times that SQLi
4e910 74 65 20 68 61 73 20 73 74 65 70 70 65 64 20 66  te has stepped f
4e920 6f 72 77 61 72 64 20 69 6e 0a 2a 2a 20 61 20 74  orward in.** a t
4e930 61 62 6c 65 20 61 73 20 70 61 72 74 20 6f 66 20  able as part of 
4e940 61 20 66 75 6c 6c 20 74 61 62 6c 65 20 73 63 61  a full table sca
4e950 6e 2e 20 20 4c 61 72 67 65 20 6e 75 6d 62 65 72  n.  Large number
4e960 73 20 66 6f 72 20 74 68 69 73 20 63 6f 75 6e 74  s for this count
4e970 65 72 0a 2a 2a 20 6d 61 79 20 69 6e 64 69 63 61  er.** may indica
4e980 74 65 20 6f 70 70 6f 72 74 75 6e 69 74 69 65 73  te opportunities
4e990 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63 65   for performance
4e9a0 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 74 68 72   improvement thr
4e9b0 6f 75 67 68 20 0a 2a 2a 20 63 61 72 65 66 75 6c  ough .** careful
4e9c0 20 75 73 65 20 6f 66 20 69 6e 64 69 63 65 73 2e   use of indices.
4e9d0 3c 2f 64 64 3e 0a 2a 2a 0a 2a 2a 20 3c 64 74 3e  </dd>.**.** <dt>
4e9e0 53 51 4c 49 54 45 5f 53 54 4d 54 53 54 41 54 55  SQLITE_STMTSTATU
4e9f0 53 5f 53 4f 52 54 3c 2f 64 74 3e 0a 2a 2a 20 3c  S_SORT</dt>.** <
4ea00 64 64 3e 54 68 69 73 20 69 73 20 74 68 65 20 6e  dd>This is the n
4ea10 75 6d 62 65 72 20 6f 66 20 73 6f 72 74 20 6f 70  umber of sort op
4ea20 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 68 61  erations that ha
4ea30 76 65 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 20  ve occurred..** 
4ea40 41 20 6e 6f 6e 2d 7a 65 72 6f 20 76 61 6c 75 65  A non-zero value
4ea50 20 69 6e 20 74 68 69 73 20 63 6f 75 6e 74 65 72   in this counter
4ea60 20 6d 61 79 20 69 6e 64 69 63 61 74 65 20 61 6e   may indicate an
4ea70 20 6f 70 70 6f 72 74 75 6e 69 74 79 20 74 6f 0a   opportunity to.
4ea80 2a 2a 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 70  ** improvement p
4ea90 65 72 66 6f 72 6d 61 6e 63 65 20 74 68 72 6f 75  erformance throu
4eaa0 67 68 20 63 61 72 65 66 75 6c 20 75 73 65 20 6f  gh careful use o
4eab0 66 20 69 6e 64 69 63 65 73 2e 3c 2f 64 64 3e 0a  f indices.</dd>.
4eac0 2a 2a 0a 2a 2a 20 3c 2f 64 6c 3e 0a 2a 2f 0a 23  **.** </dl>.*/.#
4ead0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54  define SQLITE_ST
4eae0 4d 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41  MTSTATUS_FULLSCA
4eaf0 4e 5f 53 54 45 50 20 20 20 20 20 31 0a 23 64 65  N_STEP     1.#de
4eb00 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 54 4d 54  fine SQLITE_STMT
4eb10 53 54 41 54 55 53 5f 53 4f 52 54 20 20 20 20 20  STATUS_SORT     
4eb20 20 20 20 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a           2../*.*
4eb30 2a 20 43 41 50 49 33 52 45 46 3a 20 43 75 73 74  * CAPI3REF: Cust
4eb40 6f 6d 20 50 61 67 65 20 43 61 63 68 65 20 4f 62  om Page Cache Ob
4eb50 6a 65 63 74 0a 2a 2a 20 45 58 50 45 52 49 4d 45  ject.** EXPERIME
4eb60 4e 54 41 4c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73  NTAL.**.** The s
4eb70 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 74 79  qlite3_pcache ty
4eb80 70 65 20 69 73 20 6f 70 61 71 75 65 2e 20 20 49  pe is opaque.  I
4eb90 74 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64  t is implemented
4eba0 20 62 79 0a 2a 2a 20 74 68 65 20 70 6c 75 67 67   by.** the plugg
4ebb0 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 20 20 54 68  able module.  Th
4ebc0 65 20 53 51 4c 69 74 65 20 63 6f 72 65 20 68 61  e SQLite core ha
4ebd0 73 20 6e 6f 20 6b 6e 6f 77 6c 65 64 67 65 20 6f  s no knowledge o
4ebe0 66 0a 2a 2a 20 69 74 73 20 73 69 7a 65 20 6f 72  f.** its size or
4ebf0 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75 63 74   internal struct
4ec00 75 72 65 20 61 6e 64 20 6e 65 76 65 72 20 64 65  ure and never de
4ec10 61 6c 73 20 77 69 74 68 20 74 68 65 0a 2a 2a 20  als with the.** 
4ec20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 6f  sqlite3_pcache o
4ec30 62 6a 65 63 74 20 65 78 63 65 70 74 20 62 79 20  bject except by 
4ec40 68 6f 6c 64 69 6e 67 20 61 6e 64 20 70 61 73 73  holding and pass
4ec50 69 6e 67 20 70 6f 69 6e 74 65 72 73 0a 2a 2a 20  ing pointers.** 
4ec60 74 6f 20 74 68 65 20 6f 62 6a 65 63 74 2e 0a 2a  to the object..*
4ec70 2a 0a 2a 2a 20 53 65 65 20 5b 73 71 6c 69 74 65  *.** See [sqlite
4ec80 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64 73  3_pcache_methods
4ec90 5d 20 66 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c  ] for additional
4eca0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f   information..*/
4ecb0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
4ecc0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 73  sqlite3_pcache s
4ecd0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 3b 0a 0a  qlite3_pcache;..
4ece0 2f 2a 0a 2a 2a 20 43 41 50 49 33 52 45 46 3a 20  /*.** CAPI3REF: 
4ecf0 41 70 70 6c 69 63 61 74 69 6f 6e 20 44 65 66 69  Application Defi
4ed00 6e 65 64 20 50 61 67 65 20 43 61 63 68 65 2e 0a  ned Page Cache..
4ed10 2a 2a 20 45 58 50 45 52 49 4d 45 4e 54 41 4c 0a  ** EXPERIMENTAL.
4ed20 2a 2a 0a 2a 2a 20 54 68 65 20 5b 73 71 6c 69 74  **.** The [sqlit
4ed30 65 33 5f 63 6f 6e 66 69 67 5d 28 5b 53 51 4c 49  e3_config]([SQLI
4ed40 54 45 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45  TE_CONFIG_PCACHE
4ed50 5d 2c 20 2e 2e 2e 29 20 69 6e 74 65 72 66 61 63  ], ...) interfac
4ed60 65 20 63 61 6e 0a 2a 2a 20 72 65 67 69 73 74 65  e can.** registe
4ed70 72 20 61 6e 20 61 6c 74 65 72 6e 61 74 69 76 65  r an alternative
4ed80 20 70 61 67 65 20 63 61 63 68 65 20 69 6d 70 6c   page cache impl
4ed90 65 6d 65 6e 74 61 74 69 6f 6e 20 62 79 20 70 61  ementation by pa
4eda0 73 73 69 6e 67 20 69 6e 20 61 6e 20 0a 2a 2a 20  ssing in an .** 
4edb0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
4edc0 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d  sqlite3_pcache_m
4edd0 65 74 68 6f 64 73 20 73 74 72 75 63 74 75 72 65  ethods structure
4ede0 2e 20 54 68 65 20 6d 61 6a 6f 72 69 74 79 20 6f  . The majority o
4edf0 66 20 74 68 65 20 0a 2a 2a 20 68 65 61 70 20 6d  f the .** heap m
4ee00 65 6d 6f 72 79 20 75 73 65 64 20 62 79 20 73 71  emory used by sq
4ee10 6c 69 74 65 20 69 73 20 75 73 65 64 20 62 79 20  lite is used by 
4ee20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 20 74  the page cache t
4ee30 6f 20 63 61 63 68 65 20 64 61 74 61 20 72 65 61  o cache data rea
4ee40 64 20 0a 2a 2a 20 66 72 6f 6d 2c 20 6f 72 20 72  d .** from, or r
4ee50 65 61 64 79 20 74 6f 20 62 65 20 77 72 69 74 74  eady to be writt
4ee60 65 6e 20 74 6f 2c 20 74 68 65 20 64 61 74 61 62  en to, the datab
4ee70 61 73 65 20 66 69 6c 65 2e 20 42 79 20 69 6d 70  ase file. By imp
4ee80 6c 65 6d 65 6e 74 69 6e 67 20 61 20 0a 2a 2a 20  lementing a .** 
4ee90 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63 68  custom page cach
4eea0 65 20 75 73 69 6e 67 20 74 68 69 73 20 41 50 49  e using this API
4eeb0 2c 20 61 6e 20 61 70 70 6c 69 63 61 74 69 6f 6e  , an application
4eec0 20 63 61 6e 20 63 6f 6e 74 72 6f 6c 20 6d 6f 72   can control mor
4eed0 65 20 0a 2a 2a 20 70 72 65 63 69 73 65 6c 79 20  e .** precisely 
4eee0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  the amount of me
4eef0 6d 6f 72 79 20 63 6f 6e 73 75 6d 65 64 20 62 79  mory consumed by
4ef00 20 73 71 6c 69 74 65 2c 20 74 68 65 20 77 61 79   sqlite, the way
4ef10 20 69 6e 20 77 68 69 63 68 20 0a 2a 2a 20 73 61   in which .** sa
4ef20 69 64 20 6d 65 6d 6f 72 79 20 69 73 20 61 6c 6c  id memory is all
4ef30 6f 63 61 74 65 64 20 61 6e 64 20 72 65 6c 65 61  ocated and relea
4ef40 73 65 64 2c 20 61 6e 64 20 74 68 65 20 70 6f 6c  sed, and the pol
4ef50 69 63 69 65 73 20 75 73 65 64 20 74 6f 20 0a 2a  icies used to .*
4ef60 2a 20 64 65 74 65 72 6d 69 6e 65 20 65 78 61 63  * determine exac
4ef70 74 6c 79 20 77 68 69 63 68 20 70 61 72 74 73 20  tly which parts 
4ef80 6f 66 20 61 20 64 61 74 61 62 61 73 65 20 66 69  of a database fi
4ef90 6c 65 20 61 72 65 20 63 61 63 68 65 64 20 61 6e  le are cached an
4efa0 64 20 66 6f 72 20 0a 2a 2a 20 68 6f 77 20 6c 6f  d for .** how lo
4efb0 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f  ng..**.** The co
4efc0 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 73 74  ntents of the st
4efd0 72 75 63 74 75 72 65 20 61 72 65 20 63 6f 70 69  ructure are copi
4efe0 65 64 20 74 6f 20 61 6e 20 69 6e 74 65 72 6e 61  ed to an interna
4eff0 6c 20 62 75 66 66 65 72 20 62 79 20 73 71 6c 69  l buffer by sqli
4f000 74 65 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  te.** within the
4f010 20 63 61 6c 6c 20 74 6f 20 5b 73 71 6c 69 74 65   call to [sqlite
4f020 33 5f 63 6f 6e 66 69 67 5d 2e 0a 2a 2a 0a 2a 2a  3_config]..**.**
4f030 20 54 68 65 20 78 49 6e 69 74 28 29 20 6d 65 74   The xInit() met
4f040 68 6f 64 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  hod is called on
4f050 63 65 20 66 6f 72 20 65 61 63 68 20 63 61 6c 6c  ce for each call
4f060 20 74 6f 20 5b 73 71 6c 69 74 65 33 5f 69 6e 69   to [sqlite3_ini
4f070 74 69 61 6c 69 7a 65 28 29 5d 0a 2a 2a 20 28 75  tialize()].** (u
4f080 73 75 61 6c 6c 79 20 6f 6e 6c 79 20 6f 6e 63 65  sually only once
4f090 20 64 75 72 69 6e 67 20 74 68 65 20 6c 69 66 65   during the life
4f0a0 74 69 6d 65 20 6f 66 20 74 68 65 20 70 72 6f 63  time of the proc
4f0b0 65 73 73 29 2e 20 49 74 20 69 73 20 70 61 73 73  ess). It is pass
4f0c0 65 64 0a 2a 2a 20 61 20 63 6f 70 79 20 6f 66 20  ed.** a copy of 
4f0d0 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63  the sqlite3_pcac
4f0e0 68 65 5f 6d 65 74 68 6f 64 73 2e 70 41 72 67 20  he_methods.pArg 
4f0f0 76 61 6c 75 65 2e 20 49 74 20 63 61 6e 20 62 65  value. It can be
4f100 20 75 73 65 64 20 74 6f 20 73 65 74 0a 2a 2a 20   used to set.** 
4f110 75 70 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74  up global struct
4f120 75 72 65 73 20 61 6e 64 20 6d 75 74 65 78 65 73  ures and mutexes
4f130 20 72 65 71 75 69 72 65 64 20 62 79 20 74 68 65   required by the
4f140 20 63 75 73 74 6f 6d 20 70 61 67 65 20 63 61 63   custom page cac
4f150 68 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  he .** implement
4f160 61 74 69 6f 6e 2e 20 54 68 65 20 78 53 68 75 74  ation. The xShut
4f170 64 6f 77 6e 28 29 20 6d 65 74 68 6f 64 20 69 73  down() method is
4f180 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
4f190 68 69 6e 20 0a 2a 2a 20 5b 73 71 6c 69 74 65 33  hin .** [sqlite3
4f1a0 5f 73 68 75 74 64 6f 77 6e 28 29 5d 2c 20 69 66  _shutdown()], if
4f1b0 20 74 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e   the application
4f1c0 20 69 6e 76 6f 6b 65 73 20 74 68 69 73 20 41 50   invokes this AP
4f1d0 49 2e 20 49 74 20 63 61 6e 20 62 65 20 75 73 65  I. It can be use
4f1e0 64 0a 2a 2a 20 74 6f 20 63 6c 65 61 6e 20 75 70  d.** to clean up
4f1f0 20 61 6e 79 20 6f 75 74 73 74 61 6e 64 69 6e 67   any outstanding
4f200 20 72 65 73 6f 75 72 63 65 73 20 62 65 66 6f 72   resources befor
4f210 65 20 70 72 6f 63 65 73 73 20 73 68 75 74 64 6f  e process shutdo
4f220 77 6e 2c 20 69 66 20 72 65 71 75 69 72 65 64 2e  wn, if required.
4f230 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78 43 72 65 61  .**.** The xCrea
4f240 74 65 28 29 20 6d 65 74 68 6f 64 20 69 73 20 75  te() method is u
4f250 73 65 64 20 74 6f 20 63 6f 6e 73 74 72 75 63 74  sed to construct
4f260 20 61 20 6e 65 77 20 63 61 63 68 65 20 69 6e 73   a new cache ins
4f270 74 61 6e 63 65 2e 20 54 68 65 0a 2a 2a 20 66 69  tance. The.** fi
4f280 72 73 74 20 70 61 72 61 6d 65 74 65 72 2c 20 73  rst parameter, s
4f290 7a 50 61 67 65 2c 20 69 73 20 74 68 65 20 73 69  zPage, is the si
4f2a0 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 74  ze in bytes of t
4f2b0 68 65 20 70 61 67 65 73 20 74 68 61 74 20 6d 75  he pages that mu
4f2c0 73 74 0a 2a 2a 20 62 65 20 61 6c 6c 6f 63 61 74  st.** be allocat
4f2d0 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e  ed by the cache.
4f2e0 20 73 7a 50 61 67 65 20 77 69 6c 6c 20 6e 6f 74   szPage will not
4f2f0 20 62 65 20 61 20 70 6f 77 65 72 20 6f 66 20 74   be a power of t
4f300 77 6f 2e 20 54 68 65 0a 2a 2a 20 73 65 63 6f 6e  wo. The.** secon
4f310 64 20 61 72 67 75 6d 65 6e 74 2c 20 62 50 75 72  d argument, bPur
4f320 67 65 61 62 6c 65 2c 20 69 73 20 74 72 75 65 20  geable, is true 
4f330 69 66 20 74 68 65 20 63 61 63 68 65 20 62 65 69  if the cache bei
4f340 6e 67 20 63 72 65 61 74 65 64 20 77 69 6c 6c 0a  ng created will.
4f350 2a 2a 20 62 65 20 75 73 65 64 20 74 6f 20 63 61  ** be used to ca
4f360 63 68 65 20 64 61 74 61 62 61 73 65 20 70 61 67  che database pag
4f370 65 73 20 72 65 61 64 20 66 72 6f 6d 20 61 20 66  es read from a f
4f380 69 6c 65 20 73 74 6f 72 65 64 20 6f 6e 20 64 69  ile stored on di
4f390 73 6b 2c 20 6f 72 0a 2a 2a 20 66 61 6c 73 65 20  sk, or.** false 
4f3a0 69 66 20 69 74 20 69 73 20 75 73 65 64 20 66 6f  if it is used fo
4f3b0 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64  r an in-memory d
4f3c0 61 74 61 62 61 73 65 2e 20 54 68 65 20 63 61 63  atabase. The cac
4f3d0 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  he implementatio
4f3e0 6e 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 68 61  n.** does not ha
4f3f0 76 65 20 74 6f 20 64 6f 20 61 6e 79 74 68 69 6e  ve to do anythin
4f400 67 20 73 70 65 63 69 61 6c 20 62 61 73 65 64 20  g special based 
4f410 6f 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  on the value of 
4f420 62 50 75 72 67 65 61 62 6c 65 2c 0a 2a 2a 20 69  bPurgeable,.** i
4f430 74 20 69 73 20 70 75 72 65 6c 79 20 61 64 76 69  t is purely advi
4f440 73 6f 72 79 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  sory. .**.** The
4f450 20 78 43 61 63 68 65 73 69 7a 65 28 29 20 6d 65   xCachesize() me
4f460 74 68 6f 64 20 6d 61 79 20 62 65 20 63 61 6c 6c  thod may be call
4f470 65 64 20 61 74 20 61 6e 79 20 74 69 6d 65 20 62  ed at any time b
4f480 79 20 53 51 4c 69 74 65 20 74 6f 20 73 65 74 20  y SQLite to set 
4f490 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74 65 64  the.** suggested
4f4a0 20 6d 61 78 69 6d 75 6d 20 63 61 63 68 65 2d 73   maximum cache-s
4f4b0 69 7a 65 20 28 6e 75 6d 62 65 72 20 6f 66 20 70  ize (number of p
4f4c0 61 67 65 73 20 73 74 6f 72 65 64 20 62 79 29 20  ages stored by) 
4f4d0 74 68 65 20 63 61 63 68 65 0a 2a 2a 20 69 6e 73  the cache.** ins
4f4e0 74 61 6e 63 65 20 70 61 73 73 65 64 20 61 73 20  tance passed as 
4f4f0 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
4f500 6e 74 2e 20 54 68 69 73 20 69 73 20 74 68 65 20  nt. This is the 
4f510 76 61 6c 75 65 20 63 6f 6e 66 69 67 75 72 65 64  value configured
4f520 20 75 73 69 6e 67 0a 2a 2a 20 74 68 65 20 53 51   using.** the SQ
4f530 4c 69 74 65 20 22 5b 50 52 41 47 4d 41 20 63 61  Lite "[PRAGMA ca
4f540 63 68 65 5f 73 69 7a 65 5d 22 20 63 6f 6d 6d 61  che_size]" comma
4f550 6e 64 2e 20 41 73 20 77 69 74 68 20 74 68 65 20  nd. As with the 
4f560 62 50 75 72 67 65 61 62 6c 65 20 70 61 72 61 6d  bPurgeable param
4f570 65 74 65 72 2c 0a 2a 2a 20 74 68 65 20 69 6d 70  eter,.** the imp
4f580 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 6e  lementation is n
4f590 6f 74 20 72 65 71 75 69 72 65 64 20 74 6f 20 64  ot required to d
4f5a0 6f 20 61 6e 79 74 68 69 6e 67 20 73 70 65 63 69  o anything speci
4f5b0 61 6c 20 77 69 74 68 20 74 68 69 73 0a 2a 2a 20  al with this.** 
4f5c0 76 61 6c 75 65 2c 20 69 74 20 69 73 20 61 64 76  value, it is adv
4f5d0 69 73 6f 72 79 20 6f 6e 6c 79 2e 0a 2a 2a 0a 2a  isory only..**.*
4f5e0 2a 20 54 68 65 20 78 50 61 67 65 63 6f 75 6e 74  * The xPagecount
4f5f0 28 29 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64  () method should
4f600 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   return the numb
4f610 65 72 20 6f 66 20 70 61 67 65 73 20 63 75 72 72  er of pages curr
4f620 65 6e 74 6c 79 0a 2a 2a 20 73 74 6f 72 65 64 20  ently.** stored 
4f630 69 6e 20 74 68 65 20 63 61 63 68 65 20 73 75 70  in the cache sup
4f640 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75  plied as an argu
4f650 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65  ment..** .** The
4f660 20 78 46 65 74 63 68 28 29 20 6d 65 74 68 6f 64   xFetch() method
4f670 20 69 73 20 75 73 65 64 20 74 6f 20 66 65 74 63   is used to fetc
4f680 68 20 61 20 70 61 67 65 20 61 6e 64 20 72 65 74  h a page and ret
4f690 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
4f6a0 20 69 74 2e 20 0a 2a 2a 20 41 20 27 70 61 67 65   it. .** A 'page
4f6b0 27 2c 20 69 6e 20 74 68 69 73 20 63 6f 6e 74 65  ', in this conte
4f6c0 78 74 2c 20 69 73 20 61 20 62 75 66 66 65 72 20  xt, is a buffer 
4f6d0 6f 66 20 73 7a 50 61 67 65 20 62 79 74 65 73 20  of szPage bytes 
4f6e0 61 6c 69 67 6e 65 64 20 61 74 20 61 6e 0a 2a 2a  aligned at an.**
4f6f0 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72 79   8-byte boundary
4f700 2e 20 54 68 65 20 70 61 67 65 20 74 6f 20 62 65  . The page to be
4f710 20 66 65 74 63 68 65 64 20 69 73 20 64 65 74 65   fetched is dete
4f720 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6b 65  rmined by the ke
4f730 79 2e 20 54 68 65 0a 2a 2a 20 6d 69 6d 69 6d 75  y. The.** mimimu
4f740 6d 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20 31  m key value is 1
4f750 2e 20 41 66 74 65 72 20 69 74 20 68 61 73 20 62  . After it has b
4f760 65 65 6e 20 72 65 74 72 69 65 76 65 64 20 75 73  een retrieved us
4f770 69 6e 67 20 78 46 65 74 63 68 2c 20 74 68 65 20  ing xFetch, the 
4f780 70 61 67 65 20 0a 2a 2a 20 69 73 20 63 6f 6e 73  page .** is cons
4f790 69 64 65 72 65 64 20 74 6f 20 62 65 20 70 69 6e  idered to be pin
4f7a0 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ned..**.** If th
4f7b0 65 20 72 65 71 75 65 73 74 65 64 20 70 61 67 65  e requested page
4f7c0 20 69 73 20 61 6c 72 65 61 64 79 20 69 6e 20 74   is already in t
4f7d0 68 65 20 70 61 67 65 20 63 61 63 68 65 2c 20 74  he page cache, t
4f7e0 68 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  hen a pointer to
4f7f0 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 64 20 62  .** the cached b
4f800 75 66 66 65 72 20 73 68 6f 75 6c 64 20 62 65 20  uffer should be 
4f810 72 65 74 75 72 6e 65 64 20 77 69 74 68 20 69 74  returned with it
4f820 73 20 63 6f 6e 74 65 6e 74 73 20 69 6e 74 61 63  s contents intac
4f830 74 2e 20 49 66 20 74 68 65 0a 2a 2a 20 70 61 67  t. If the.** pag
4f840 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79  e is not already
4f850 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c 20 74   in the cache, t
4f860 68 65 6e 20 74 68 65 20 65 78 70 65 63 74 65 64  hen the expected
4f870 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 74 68   behaviour of th
4f880 65 0a 2a 2a 20 63 61 63 68 65 20 69 73 20 64 65  e.** cache is de
4f890 74 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20  termined by the 
4f8a0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 72 65  value of the cre
4f8b0 61 74 65 46 6c 61 67 20 70 61 72 61 6d 65 74 65  ateFlag paramete
4f8c0 72 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20 78  r passed.** to x
4f8d0 46 65 74 63 68 2c 20 61 63 63 6f 72 64 69 6e 67  Fetch, according
4f8e0 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   to the followin
4f8f0 67 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 3c  g table:.**.** <
4f900 74 61 62 6c 65 20 62 6f 72 64 65 72 3d 31 20 77  table border=1 w
4f910 69 64 74 68 3d 38 35 25 20 61 6c 69 67 6e 3d 63  idth=85% align=c
4f920 65 6e 74 65 72 3e 0a 2a 2a 20 20 20 3c 74 72 3e  enter>.**   <tr>
4f930 3c 74 68 3e 63 72 65 61 74 65 46 6c 61 67 3c 74  <th>createFlag<t
4f940 68 3e 45 78 70 65 63 74 65 64 20 42 65 68 61 76  h>Expected Behav
4f950 69 6f 75 72 0a 2a 2a 20 20 20 3c 74 72 3e 3c 74  iour.**   <tr><t
4f960 64 3e 30 3c 74 64 3e 4e 55 4c 4c 20 73 68 6f 75  d>0<td>NULL shou
4f970 6c 64 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  ld be returned. 
4f980 4e 6f 20 6e 65 77 20 63 61 63 68 65 20 65 6e 74  No new cache ent
4f990 72 79 20 69 73 20 63 72 65 61 74 65 64 2e 0a 2a  ry is created..*
4f9a0 2a 20 20 20 3c 74 72 3e 3c 74 64 3e 31 3c 74 64  *   <tr><td>1<td
4f9b0 3e 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69  >If createFlag i
4f9c0 73 20 73 65 74 20 74 6f 20 31 2c 20 74 68 69 73  s set to 1, this
4f9d0 20 69 6e 64 69 63 61 74 65 73 20 74 68 61 74 20   indicates that 
4f9e0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
4f9f0 20 20 20 53 51 4c 69 74 65 20 69 73 20 68 6f 6c     SQLite is hol
4fa00 64 69 6e 67 20 70 69 6e 6e 65 64 20 70 61 67 65  ding pinned page
4fa10 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 75 6e  s that can be un
4fa20 70 69 6e 6e 65 64 0a 2a 2a 20 20 20 20 20 20 20  pinned.**       
4fa30 20 20 20 20 20 20 20 20 20 62 79 20 77 72 69 74           by writ
4fa40 69 6e 67 20 74 68 65 69 72 20 63 6f 6e 74 65 6e  ing their conten
4fa50 74 73 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  ts to the databa
4fa60 73 65 20 66 69 6c 65 20 28 61 0a 2a 2a 20 20 20  se file (a.**   
4fa70 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65 6c               rel
4fa80 61 74 69 76 65 6c 79 20 65 78 70 65 6e 73 69 76  atively expensiv
4fa90 65 20 6f 70 65 72 61 74 69 6f 6e 29 2e 20 49 6e  e operation). In
4faa0 20 74 68 69 73 20 73 69 74 75 61 74 69 6f 6e 20   this situation 
4fab0 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  the.**          
4fac0 20 20 20 20 20 20 63 61 63 68 65 20 69 6d 70 6c        cache impl
4fad0 65 6d 65 6e 74 61 74 69 6f 6e 20 68 61 73 20 74  ementation has t
4fae0 77 6f 20 63 68 6f 69 63 65 73 3a 20 69 74 20 63  wo choices: it c
4faf0 61 6e 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2c 0a  an return NULL,.
4fb00 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
4fb10 20 20 69 6e 20 77 68 69 63 68 20 63 61 73 65 20    in which case 
4fb20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 74 74 65  SQLite will atte
4fb30 6d 70 74 20 74 6f 20 75 6e 70 69 6e 20 6f 6e 65  mpt to unpin one
4fb40 20 6f 72 20 6d 6f 72 65 20 0a 2a 2a 20 20 20 20   or more .**    
4fb50 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65              page
4fb60 73 20 62 65 66 6f 72 65 20 72 65 2d 72 65 71 75  s before re-requ
4fb70 65 73 74 69 6e 67 20 74 68 65 20 73 61 6d 65 20  esting the same 
4fb80 70 61 67 65 2c 20 6f 72 20 69 74 20 63 61 6e 0a  page, or it can.
4fb90 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
4fba0 20 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77    allocate a new
4fbb0 20 70 61 67 65 20 61 6e 64 20 72 65 74 75 72 6e   page and return
4fbc0 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74   a pointer to it
4fbd0 2e 20 49 66 20 61 20 6e 65 77 0a 2a 2a 20 20 20  . If a new.**   
4fbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 70 61 67               pag
4fbf0 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 2c 20  e is allocated, 
4fc00 74 68 65 6e 20 69 74 20 6d 75 73 74 20 62 65 20  then it must be 
4fc10 63 6f 6d 70 6c 65 74 65 6c 79 20 7a 65 72 6f 65  completely zeroe
4fc20 64 20 62 65 66 6f 72 65 20 0a 2a 2a 20 20 20 20  d before .**    
4fc30 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20 69              it i
4fc40 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 20  s returned..**  
4fc50 20 3c 74 72 3e 3c 74 64 3e 32 3c 74 64 3e 49 66   <tr><td>2<td>If
4fc60 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20 73   createFlag is s
4fc70 65 74 20 74 6f 20 32 2c 20 74 68 65 6e 20 53 51  et to 2, then SQ
4fc80 4c 69 74 65 20 69 73 20 6e 6f 74 20 68 6f 6c 64  Lite is not hold
4fc90 69 6e 67 20 61 6e 79 0a 2a 2a 20 20 20 20 20 20  ing any.**      
4fca0 20 20 20 20 20 20 20 20 20 20 70 69 6e 6e 65 64            pinned
4fcb0 20 70 61 67 65 73 20 61 73 73 6f 63 69 61 74 65   pages associate
4fcc0 64 20 77 69 74 68 20 74 68 65 20 73 70 65 63 69  d with the speci
4fcd0 66 69 63 20 63 61 63 68 65 20 70 61 73 73 65 64  fic cache passed
4fce0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
4fcf0 20 20 20 61 73 20 74 68 65 20 66 69 72 73 74 20     as the first 
4fd00 61 72 67 75 6d 65 6e 74 20 74 6f 20 78 46 65 74  argument to xFet
4fd10 63 68 28 29 20 74 68 61 74 20 63 61 6e 20 62 65  ch() that can be
4fd20 20 75 6e 70 69 6e 6e 65 64 2e 20 54 68 65 0a 2a   unpinned. The.*
4fd30 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
4fd40 20 63 61 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74   cache implement
4fd50 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 61 74 74  ation should att
4fd60 65 6d 70 74 20 74 6f 20 61 6c 6c 6f 63 61 74 65  empt to allocate
4fd70 20 61 20 6e 65 77 0a 2a 2a 20 20 20 20 20 20 20   a new.**       
4fd80 20 20 20 20 20 20 20 20 20 63 61 63 68 65 20 65           cache e
4fd90 6e 74 72 79 20 61 6e 64 20 72 65 74 75 72 6e 20  ntry and return 
4fda0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e  a pointer to it.
4fdb0 20 41 67 61 69 6e 2c 20 74 68 65 20 6e 65 77 0a   Again, the new.
4fdc0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
4fdd0 20 20 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65    page should be
4fde0 20 7a 65 72 6f 65 64 20 62 65 66 6f 72 65 20 69   zeroed before i
4fdf0 74 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 49  t is returned. I
4fe00 66 20 74 68 65 20 78 46 65 74 63 68 28 29 0a 2a  f the xFetch().*
4fe10 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
4fe20 20 6d 65 74 68 6f 64 20 72 65 74 75 72 6e 73 20   method returns 
4fe30 4e 55 4c 4c 20 77 68 65 6e 20 63 72 65 61 74 65  NULL when create
4fe40 46 6c 61 67 3d 3d 32 2c 20 53 51 4c 69 74 65 20  Flag==2, SQLite 
4fe50 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20  assumes that.** 
4fe60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
4fe70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
4fe80 6f 6e 20 66 61 69 6c 65 64 20 61 6e 64 20 72 65  on failed and re
4fe90 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 4e 4f 4d  turns SQLITE_NOM
4fea0 45 4d 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20  EM to the.**    
4feb0 20 20 20 20 20 20 20 20 20 20 20 20 75 73 65 72              user
4fec0 2e 0a 2a 2a 20 3c 2f 74 61 62 6c 65 3e 0a 2a 2a  ..** </table>.**
4fed0 0a 2a 2a 20 78 55 6e 70 69 6e 28 29 20 69 73 20  .** xUnpin() is 
4fee0 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74 65  called by SQLite
4fef0 20 77 69 74 68 20 61 20 70 6f 69 6e 74 65 72 20   with a pointer 
4ff00 74 6f 20 61 20 63 75 72 72 65 6e 74 6c 79 20 70  to a currently p
4ff10 69 6e 6e 65 64 20 70 61 67 65 0a 2a 2a 20 61 73  inned page.** as
4ff20 20 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75   its second argu
4ff30 6d 65 6e 74 2e 20 49 66 20 74 68 65 20 74 68 69  ment. If the thi
4ff40 72 64 20 70 61 72 61 6d 65 74 65 72 2c 20 64 69  rd parameter, di
4ff50 73 63 61 72 64 2c 20 69 73 20 6e 6f 6e 2d 7a 65  scard, is non-ze
4ff60 72 6f 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  ro,.** then the 
4ff70 70 61 67 65 20 73 68 6f 75 6c 64 20 62 65 20 65  page should be e
4ff80 76 69 63 74 65 64 20 66 72 6f 6d 20 74 68 65 20  victed from the 
4ff90 63 61 63 68 65 2e 20 49 6e 20 74 68 69 73 20 63  cache. In this c
4ffa0 61 73 65 20 53 51 4c 69 74 65 20 0a 2a 2a 20 61  ase SQLite .** a
4ffb0 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20  ssumes that the 
4ffc0 6e 65 78 74 20 74 69 6d 65 20 74 68 65 20 70 61  next time the pa
4ffd0 67 65 20 69 73 20 72 65 74 72 69 65 76 65 64 20  ge is retrieved 
4ffe0 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 20 75  from the cache u
4fff0 73 69 6e 67 0a 2a 2a 20 74 68 65 20 78 46 65 74  sing.** the xFet
50000 63 68 28 29 20 6d 65 74 68 6f 64 2c 20 69 74 20  ch() method, it 
50010 77 69 6c 6c 20 62 65 20 7a 65 72 6f 65 64 2e 20  will be zeroed. 
50020 49 66 20 74 68 65 20 64 69 73 63 61 72 64 20 70  If the discard p
50030 61 72 61 6d 65 74 65 72 20 69 73 0a 2a 2a 20 7a  arameter is.** z
50040 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 61  ero, then the pa
50050 67 65 20 69 73 20 63 6f 6e 73 69 64 65 72 65 64  ge is considered
50060 20 74 6f 20 62 65 20 75 6e 70 69 6e 6e 65 64 2e   to be unpinned.
50070 20 54 68 65 20 63 61 63 68 65 20 69 6d 70 6c 65   The cache imple
50080 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6d 61 79  mentation.** may
50090 20 63 68 6f 6f 73 65 20 74 6f 20 72 65 63 6c 61   choose to recla
500a0 69 6d 20 28 66 72 65 65 20 6f 72 20 72 65 63 79  im (free or recy
500b0 63 6c 65 29 20 75 6e 70 69 6e 6e 65 64 20 70 61  cle) unpinned pa
500c0 67 65 73 20 61 74 20 61 6e 79 20 74 69 6d 65 2e  ges at any time.
500d0 0a 2a 2a 20 53 51 4c 69 74 65 20 61 73 73 75 6d  .** SQLite assum
500e0 65 73 20 74 68 61 74 20 6e 65 78 74 20 74 69 6d  es that next tim
500f0 65 20 74 68 65 20 70 61 67 65 20 69 73 20 72 65  e the page is re
50100 74 72 69 65 76 65 64 20 66 72 6f 6d 20 74 68 65  trieved from the
50110 20 63 61 63 68 65 0a 2a 2a 20 69 74 20 77 69 6c   cache.** it wil
50120 6c 20 65 69 74 68 65 72 20 62 65 20 7a 65 72 6f  l either be zero
50130 65 64 2c 20 6f 72 20 63 6f 6e 74 61 69 6e 20 74  ed, or contain t
50140 68 65 20 73 61 6d 65 20 64 61 74 61 20 74 68 61  he same data tha
50150 74 20 69 74 20 64 69 64 20 77 68 65 6e 20 69 74  t it did when it
50160 0a 2a 2a 20 77 61 73 20 75 6e 70 69 6e 6e 65 64  .** was unpinned
50170 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 63 68  ..**.** The cach
50180 65 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65  e is not require
50190 64 20 74 6f 20 70 65 72 66 6f 72 6d 20 61 6e 79  d to perform any
501a0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
501b0 69 6e 67 2e 20 41 20 73 69 6e 67 6c 65 20 0a 2a  ing. A single .*
501c0 2a 20 63 61 6c 6c 20 74 6f 20 78 55 6e 70 69 6e  * call to xUnpin
501d0 28 29 20 75 6e 70 69 6e 73 20 74 68 65 20 70 61  () unpins the pa
501e0 67 65 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  ge regardless of
501f0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
50200 72 69 6f 72 20 63 61 6c 6c 73 20 0a 2a 2a 20 74  rior calls .** t
50210 6f 20 78 46 65 74 63 68 28 29 2e 0a 2a 2a 0a 2a  o xFetch()..**.*
50220 2a 20 54 68 65 20 78 52 65 6b 65 79 28 29 20 6d  * The xRekey() m
50230 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74 6f  ethod is used to
50240 20 63 68 61 6e 67 65 20 74 68 65 20 6b 65 79 20   change the key 
50250 76 61 6c 75 65 20 61 73 73 6f 63 69 61 74 65 64  value associated
50260 20 77 69 74 68 20 74 68 65 0a 2a 2a 20 70 61 67   with the.** pag
50270 65 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  e passed as the 
50280 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
50290 66 72 6f 6d 20 6f 6c 64 4b 65 79 20 74 6f 20 6e  from oldKey to n
502a0 65 77 4b 65 79 2e 20 49 66 20 74 68 65 20 63 61  ewKey. If the ca
502b0 63 68 65 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c  che.** previousl
502c0 79 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 65 6e  y contains an en
502d0 74 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  try associated w
502e0 69 74 68 20 6e 65 77 4b 65 79 2c 20 69 74 20 73  ith newKey, it s
502f0 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 64 69 73 63  hould be.** disc
50300 61 72 64 65 64 2e 20 41 6e 79 20 70 72 69 6f 72  arded. Any prior
50310 20 63 61 63 68 65 20 65 6e 74 72 79 20 61 73 73   cache entry ass
50320 6f 63 69 61 74 65 64 20 77 69 74 68 20 6e 65 77  ociated with new
50330 4b 65 79 20 69 73 20 67 75 61 72 61 6e 74 65 65  Key is guarantee
50340 64 20 6e 6f 74 0a 2a 2a 20 74 6f 20 62 65 20 70  d not.** to be p
50350 69 6e 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  inned..**.** Whe
50360 6e 20 53 51 4c 69 74 65 20 63 61 6c 6c 73 20 74  n SQLite calls t
50370 68 65 20 78 54 72 75 6e 63 61 74 65 28 29 20 6d  he xTruncate() m
50380 65 74 68 6f 64 2c 20 74 68 65 20 63 61 63 68 65  ethod, the cache
50390 20 6d 75 73 74 20 64 69 73 63 61 72 64 20 61 6c   must discard al
503a0 6c 0a 2a 2a 20 65 78 69 73 74 69 6e 67 20 63 61  l.** existing ca
503b0 63 68 65 20 65 6e 74 72 69 65 73 20 77 69 74 68  che entries with
503c0 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 28 6b   page numbers (k
503d0 65 79 73 29 20 67 72 65 61 74 65 72 20 74 68 61  eys) greater tha
503e0 6e 20 6f 72 20 65 71 75 61 6c 0a 2a 2a 20 74 6f  n or equal.** to
503f0 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
50400 65 20 69 4c 69 6d 69 74 20 70 61 72 61 6d 65 74  e iLimit paramet
50410 65 72 20 70 61 73 73 65 64 20 74 6f 20 78 54 72  er passed to xTr
50420 75 6e 63 61 74 65 28 29 2e 20 49 66 20 61 6e 79  uncate(). If any
50430 0a 2a 2a 20 6f 66 20 74 68 65 73 65 20 70 61 67  .** of these pag
50440 65 73 20 61 72 65 20 70 69 6e 6e 65 64 2c 20 74  es are pinned, t
50450 68 65 79 20 61 72 65 20 69 6d 70 6c 69 63 69 74  hey are implicit
50460 6c 79 20 75 6e 70 69 6e 6e 65 64 2c 20 6d 65 61  ly unpinned, mea
50470 6e 69 6e 67 20 74 68 61 74 0a 2a 2a 20 74 68 65  ning that.** the
50480 79 20 63 61 6e 20 62 65 20 73 61 66 65 6c 79 20  y can be safely 
50490 64 69 73 63 61 72 64 65 64 2e 0a 2a 2a 0a 2a 2a  discarded..**.**
504a0 20 54 68 65 20 78 44 65 73 74 72 6f 79 28 29 20   The xDestroy() 
504b0 6d 65 74 68 6f 64 20 69 73 20 75 73 65 64 20 74  method is used t
504c0 6f 20 64 65 6c 65 74 65 20 61 20 63 61 63 68 65  o delete a cache
504d0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 78 43   allocated by xC
504e0 72 65 61 74 65 28 29 2e 0a 2a 2a 20 41 6c 6c 20  reate()..** All 
504f0 72 65 73 6f 75 72 63 65 73 20 61 73 73 6f 63 69  resources associ
50500 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 70  ated with the sp
50510 65 63 69 66 69 65 64 20 63 61 63 68 65 20 73 68  ecified cache sh
50520 6f 75 6c 64 20 62 65 20 66 72 65 65 64 2e 20 41  ould be freed. A
50530 66 74 65 72 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20  fter.** calling 
50540 74 68 65 20 78 44 65 73 74 72 6f 79 28 29 20 6d  the xDestroy() m
50550 65 74 68 6f 64 2c 20 53 51 4c 69 74 65 20 63 6f  ethod, SQLite co
50560 6e 73 69 64 65 72 73 20 74 68 65 20 5b 73 71 6c  nsiders the [sql
50570 69 74 65 33 5f 70 63 61 63 68 65 2a 5d 0a 2a 2a  ite3_pcache*].**
50580 20 68 61 6e 64 6c 65 20 69 6e 76 61 6c 69 64 2c   handle invalid,
50590 20 61 6e 64 20 77 69 6c 6c 20 6e 6f 74 20 75 73   and will not us
505a0 65 20 69 74 20 77 69 74 68 20 61 6e 79 20 6f 74  e it with any ot
505b0 68 65 72 20 73 71 6c 69 74 65 33 5f 70 63 61 63  her sqlite3_pcac
505c0 68 65 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 66 75  he_methods.** fu
505d0 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 74 79 70 65  nctions..*/.type
505e0 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74  def struct sqlit
505f0 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68 6f 64  e3_pcache_method
50600 73 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  s sqlite3_pcache
50610 5f 6d 65 74 68 6f 64 73 3b 0a 73 74 72 75 63 74  _methods;.struct
50620 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 5f   sqlite3_pcache_
50630 6d 65 74 68 6f 64 73 20 7b 0a 20 20 76 6f 69 64  methods {.  void
50640 20 2a 70 41 72 67 3b 0a 20 20 69 6e 74 20 28 2a   *pArg;.  int (*
50650 78 49 6e 69 74 29 28 76 6f 69 64 2a 29 3b 0a 20  xInit)(void*);. 
50660 20 76 6f 69 64 20 28 2a 78 53 68 75 74 64 6f 77   void (*xShutdow
50670 6e 29 28 76 6f 69 64 2a 29 3b 0a 20 20 73 71 6c  n)(void*);.  sql
50680 69 74 65 33 5f 70 63 61 63 68 65 20 2a 28 2a 78  ite3_pcache *(*x
50690 43 72 65 61 74 65 29 28 69 6e 74 20 73 7a 50 61  Create)(int szPa
506a0 67 65 2c 20 69 6e 74 20 62 50 75 72 67 65 61 62  ge, int bPurgeab
506b0 6c 65 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 43  le);.  void (*xC
506c0 61 63 68 65 73 69 7a 65 29 28 73 71 6c 69 74 65  achesize)(sqlite
506d0 33 5f 70 63 61 63 68 65 2a 2c 20 69 6e 74 20 6e  3_pcache*, int n
506e0 43 61 63 68 65 73 69 7a 65 29 3b 0a 20 20 69 6e  Cachesize);.  in
506f0 74 20 28 2a 78 50 61 67 65 63 6f 75 6e 74 29 28  t (*xPagecount)(
50700 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29  sqlite3_pcache*)
50710 3b 0a 20 20 76 6f 69 64 20 2a 28 2a 78 46 65 74  ;.  void *(*xFet
50720 63 68 29 28 73 71 6c 69 74 65 33 5f 70 63 61 63  ch)(sqlite3_pcac
50730 68 65 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6b 65  he*, unsigned ke
50740 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61  y, int createFla
50750 67 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 6e  g);.  void (*xUn
50760 70 69 6e 29 28 73 71 6c 69 74 65 33 5f 70 63 61  pin)(sqlite3_pca
50770 63 68 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74  che*, void*, int
50780 20 64 69 73 63 61 72 64 29 3b 0a 20 20 76 6f 69   discard);.  voi
50790 64 20 28 2a 78 52 65 6b 65 79 29 28 73 71 6c 69  d (*xRekey)(sqli
507a0 74 65 33 5f 70 63 61 63 68 65 2a 2c 20 76 6f 69  te3_pcache*, voi
507b0 64 2a 2c 20 75 6e 73 69 67 6e 65 64 20 6f 6c 64  d*, unsigned old
507c0 4b 65 79 2c 20 75 6e 73 69 67 6e 65 64 20 6e 65  Key, unsigned ne
507d0 77 4b 65 79 29 3b 0a 20 20 76 6f 69 64 20 28 2a  wKey);.  void (*
507e0 78 54 72 75 6e 63 61 74 65 29 28 73 71 6c 69 74  xTruncate)(sqlit
507f0 65 33 5f 70 63 61 63 68 65 2a 2c 20 75 6e 73 69  e3_pcache*, unsi
50800 67 6e 65 64 20 69 4c 69 6d 69 74 29 3b 0a 20 20  gned iLimit);.  
50810 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79 29  void (*xDestroy)
50820 28 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a  (sqlite3_pcache*
50830 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 55 6e 64  );.};../*.** Und
50840 6f 20 74 68 65 20 68 61 63 6b 20 74 68 61 74 20  o the hack that 
50850 63 6f 6e 76 65 72 74 73 20 66 6c 6f 61 74 69 6e  converts floatin
50860 67 20 70 6f 69 6e 74 20 74 79 70 65 73 20 74 6f  g point types to
50870 20 69 6e 74 65 67 65 72 20 66 6f 72 0a 2a 2a 20   integer for.** 
50880 62 75 69 6c 64 73 20 6f 6e 20 70 72 6f 63 65 73  builds on proces
50890 73 6f 72 73 20 77 69 74 68 6f 75 74 20 66 6c 6f  sors without flo
508a0 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75 70 70  ating point supp
508b0 6f 72 74 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ort..*/.#ifdef S
508c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54  QLITE_OMIT_FLOAT
508d0 49 4e 47 5f 50 4f 49 4e 54 0a 23 20 75 6e 64 65  ING_POINT.# unde
508e0 66 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66 0a  f double.#endif.
508f0 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20 45 6e 64  .#if 0.}  /* End
50900 20 6f 66 20 74 68 65 20 27 65 78 74 65 72 6e 20   of the 'extern 
50910 22 43 22 27 20 62 6c 6f 63 6b 20 2a 2f 0a 23 65  "C"' block */.#e
50920 6e 64 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a  ndif.#endif../**
50930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
50940 20 6f 66 20 73 71 6c 69 74 65 33 2e 68 20 2a 2a   of sqlite3.h **
50950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
50980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
50990 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
509a0 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
509b0 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
509c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
509d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
509e0 6c 75 64 65 20 68 61 73 68 2e 68 20 69 6e 20 74  lude hash.h in t
509f0 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c  he middle of sql
50a00 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
50a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
50a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
50a30 69 6e 20 66 69 6c 65 20 68 61 73 68 2e 68 20 2a  in file hash.h *
50a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50a50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
50a70 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
50a80 72 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 22.**.** The a
50a90 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
50aa0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
50ab0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
50ac0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
50ad0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
50ae0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
50af0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
50b00 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
50b10 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
50b20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
50b30 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
50b40 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
50b50 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
50b60 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
50b70 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
50b80 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
50b90 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
50ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50bb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
50be0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74  ***.** This is t
50bf0 68 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66  he header file f
50c00 6f 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68  or the generic h
50c10 61 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d  ash-table implem
50c20 65 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20  enation.** used 
50c30 69 6e 20 53 51 4c 69 74 65 2e 0a 2a 2a 0a 2a 2a  in SQLite..**.**
50c40 20 24 49 64 3a 20 68 61 73 68 2e 68 2c 76 20 31   $Id: hash.h,v 1
50c50 2e 31 32 20 32 30 30 38 2f 31 30 2f 31 30 20 31  .12 2008/10/10 1
50c60 37 3a 34 31 3a 32 39 20 64 72 68 20 45 78 70 20  7:41:29 drh Exp 
50c70 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51  $.*/.#ifndef _SQ
50c80 4c 49 54 45 5f 48 41 53 48 5f 48 5f 0a 23 64 65  LITE_HASH_H_.#de
50c90 66 69 6e 65 20 5f 53 51 4c 49 54 45 5f 48 41 53  fine _SQLITE_HAS
50ca0 48 5f 48 5f 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  H_H_../* Forward
50cb0 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 6f 66   declarations of
50cc0 20 73 74 72 75 63 74 75 72 65 73 2e 20 2a 2f 0a   structures. */.
50cd0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 48  typedef struct H
50ce0 61 73 68 20 48 61 73 68 3b 0a 74 79 70 65 64 65  ash Hash;.typede
50cf0 66 20 73 74 72 75 63 74 20 48 61 73 68 45 6c 65  f struct HashEle
50d00 6d 20 48 61 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20  m HashElem;../* 
50d10 41 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68 20  A complete hash 
50d20 74 61 62 6c 65 20 69 73 20 61 6e 20 69 6e 73 74  table is an inst
50d30 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
50d40 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
50d50 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 6e 61 6c  .** The internal
50d60 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
50d70 75 72 65 20 61 72 65 20 69 6e 74 65 6e 64 65 64  ure are intended
50d80 20 74 6f 20 62 65 20 6f 70 61 71 75 65 20 2d 2d   to be opaque --
50d90 20 63 6c 69 65 6e 74 0a 2a 2a 20 63 6f 64 65 20   client.** code 
50da0 73 68 6f 75 6c 64 20 6e 6f 74 20 61 74 74 65 6d  should not attem
50db0 70 74 20 74 6f 20 61 63 63 65 73 73 20 6f 72 20  pt to access or 
50dc0 6d 6f 64 69 66 79 20 74 68 65 20 66 69 65 6c 64  modify the field
50dd0 73 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  s of this struct
50de0 75 72 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2e  ure.** directly.
50df0 20 20 43 68 61 6e 67 65 20 74 68 69 73 20 73 74    Change this st
50e00 72 75 63 74 75 72 65 20 6f 6e 6c 79 20 62 79 20  ructure only by 
50e10 75 73 69 6e 67 20 74 68 65 20 72 6f 75 74 69 6e  using the routin
50e20 65 73 20 62 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77  es below..** How
50e30 65 76 65 72 2c 20 6d 61 6e 79 20 6f 66 20 74 68  ever, many of th
50e40 65 20 22 70 72 6f 63 65 64 75 72 65 73 22 20 61  e "procedures" a
50e50 6e 64 20 22 66 75 6e 63 74 69 6f 6e 73 22 20 66  nd "functions" f
50e60 6f 72 20 6d 6f 64 69 66 79 69 6e 67 20 61 6e 64  or modifying and
50e70 0a 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 74 68  .** accessing th
50e80 69 73 20 73 74 72 75 63 74 75 72 65 20 61 72 65  is structure are
50e90 20 72 65 61 6c 6c 79 20 6d 61 63 72 6f 73 2c 20   really macros, 
50ea0 73 6f 20 77 65 20 63 61 6e 27 74 20 72 65 61 6c  so we can't real
50eb0 6c 79 20 6d 61 6b 65 0a 2a 2a 20 74 68 69 73 20  ly make.** this 
50ec0 73 74 72 75 63 74 75 72 65 20 6f 70 61 71 75 65  structure opaque
50ed0 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 48 61 73 68  ..*/.struct Hash
50ee0 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e   {.  unsigned in
50ef0 74 20 63 6f 70 79 4b 65 79 3a 20 31 3b 20 20 2f  t copyKey: 1;  /
50f00 2a 20 54 72 75 65 20 69 66 20 63 6f 70 79 20 6f  * True if copy o
50f10 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e 20 69 6e  f key made on in
50f20 73 65 72 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  sert */.  unsign
50f30 65 64 20 69 6e 74 20 68 74 73 69 7a 65 20 3a 20  ed int htsize : 
50f40 33 31 3b 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66  31; /* Number of
50f50 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20   buckets in the 
50f60 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
50f70 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 6f 75  unsigned int cou
50f80 6e 74 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  nt;       /* Num
50f90 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
50fa0 6e 20 74 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a  n this table */.
50fb0 20 20 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73    HashElem *firs
50fc0 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
50fd0 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
50fe0 20 6f 66 20 74 68 65 20 61 72 72 61 79 20 2a 2f   of the array */
50ff0 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20 7b 20  .  struct _ht { 
51000 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
51010 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 2a  the hash table *
51020 2f 0a 20 20 20 20 69 6e 74 20 63 6f 75 6e 74 3b  /.    int count;
51030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51040 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
51050 74 72 69 65 73 20 77 69 74 68 20 74 68 69 73 20  tries with this 
51060 68 61 73 68 20 2a 2f 0a 20 20 20 20 48 61 73 68  hash */.    Hash
51070 45 6c 65 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20  Elem *chain;    
51080 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
51090 72 20 74 6f 20 66 69 72 73 74 20 65 6e 74 72 79  r to first entry
510a0 20 77 69 74 68 20 74 68 69 73 20 68 61 73 68 20   with this hash 
510b0 2a 2f 0a 20 20 7d 20 2a 68 74 3b 0a 7d 3b 0a 0a  */.  } *ht;.};..
510c0 2f 2a 20 45 61 63 68 20 65 6c 65 6d 65 6e 74 20  /* Each element 
510d0 69 6e 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  in the hash tabl
510e0 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
510f0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
51100 67 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  g .** structure.
51110 20 20 41 6c 6c 20 65 6c 65 6d 65 6e 74 73 20 61    All elements a
51120 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 73  re stored on a s
51130 69 6e 67 6c 65 20 64 6f 75 62 6c 79 2d 6c 69 6e  ingle doubly-lin
51140 6b 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20  ked list..**.** 
51150 41 67 61 69 6e 2c 20 74 68 69 73 20 73 74 72 75  Again, this stru
51160 63 74 75 72 65 20 69 73 20 69 6e 74 65 6e 64 65  cture is intende
51170 64 20 74 6f 20 62 65 20 6f 70 61 71 75 65 2c 20  d to be opaque, 
51180 62 75 74 20 69 74 20 63 61 6e 27 74 20 72 65 61  but it can't rea
51190 6c 6c 79 0a 2a 2a 20 62 65 20 6f 70 61 71 75 65  lly.** be opaque
511a0 20 62 65 63 61 75 73 65 20 69 74 20 69 73 20 75   because it is u
511b0 73 65 64 20 62 79 20 6d 61 63 72 6f 73 2e 0a 2a  sed by macros..*
511c0 2f 0a 73 74 72 75 63 74 20 48 61 73 68 45 6c 65  /.struct HashEle
511d0 6d 20 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a  m {.  HashElem *
511e0 6e 65 78 74 2c 20 2a 70 72 65 76 3b 20 20 20 2f  next, *prev;   /
511f0 2a 20 4e 65 78 74 20 61 6e 64 20 70 72 65 76 69  * Next and previ
51200 6f 75 73 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20  ous elements in 
51210 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76  the table */.  v
51220 6f 69 64 20 2a 64 61 74 61 3b 20 20 20 20 20 20  oid *data;      
51230 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20          /* Data 
51240 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
51250 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a  this element */.
51260 20 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e    void *pKey; in
51270 74 20 6e 4b 65 79 3b 20 20 20 20 2f 2a 20 4b 65  t nKey;    /* Ke
51280 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  y associated wit
51290 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a  h this element *
512a0 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 63 63 65  /.};../*.** Acce
512b0 73 73 20 72 6f 75 74 69 6e 65 73 2e 20 20 54 6f  ss routines.  To
512c0 20 64 65 6c 65 74 65 2c 20 69 6e 73 65 72 74 20   delete, insert 
512d0 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a  a NULL pointer..
512e0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
512f0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61  E void sqlite3Ha
51300 73 68 49 6e 69 74 28 48 61 73 68 2a 2c 20 69 6e  shInit(Hash*, in
51310 74 20 63 6f 70 79 4b 65 79 29 3b 0a 53 51 4c 49  t copyKey);.SQLI
51320 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
51330 2a 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65  *sqlite3HashInse
51340 72 74 28 48 61 73 68 2a 2c 20 63 6f 6e 73 74 20  rt(Hash*, const 
51350 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20  void *pKey, int 
51360 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44 61 74  nKey, void *pDat
51370 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  a);.SQLITE_PRIVA
51380 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
51390 48 61 73 68 46 69 6e 64 28 63 6f 6e 73 74 20 48  HashFind(const H
513a0 61 73 68 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64  ash*, const void
513b0 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79   *pKey, int nKey
513c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
513d0 45 20 48 61 73 68 45 6c 65 6d 20 2a 73 71 6c 69  E HashElem *sqli
513e0 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65 6d 28  te3HashFindElem(
513f0 63 6f 6e 73 74 20 48 61 73 68 2a 2c 20 63 6f 6e  const Hash*, con
51400 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69  st void *pKey, i
51410 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c 49 54 45  nt nKey);.SQLITE
51420 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
51430 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28 48  lite3HashClear(H
51440 61 73 68 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61  ash*);../*.** Ma
51450 63 72 6f 73 20 66 6f 72 20 6c 6f 6f 70 69 6e 67  cros for looping
51460 20 6f 76 65 72 20 61 6c 6c 20 65 6c 65 6d 65 6e   over all elemen
51470 74 73 20 6f 66 20 61 20 68 61 73 68 20 74 61 62  ts of a hash tab
51480 6c 65 2e 20 20 54 68 65 20 69 64 69 6f 6d 20 69  le.  The idiom i
51490 73 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73 3a 0a  s.** like this:.
514a0 2a 2a 0a 2a 2a 20 20 20 48 61 73 68 20 68 3b 0a  **.**   Hash h;.
514b0 2a 2a 20 20 20 48 61 73 68 45 6c 65 6d 20 2a 70  **   HashElem *p
514c0 3b 0a 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20  ;.**   ....**   
514d0 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73 68  for(p=sqliteHash
514e0 46 69 72 73 74 28 26 68 29 3b 20 70 3b 20 70 3d  First(&h); p; p=
514f0 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70  sqliteHashNext(p
51500 29 29 7b 0a 2a 2a 20 20 20 20 20 53 6f 6d 65 53  )){.**     SomeS
51510 74 72 75 63 74 75 72 65 20 2a 70 44 61 74 61 20  tructure *pData 
51520 3d 20 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  = sqliteHashData
51530 28 70 29 3b 0a 2a 2a 20 20 20 20 20 2f 2f 20 64  (p);.**     // d
51540 6f 20 73 6f 6d 65 74 68 69 6e 67 20 77 69 74 68  o something with
51550 20 70 44 61 74 61 0a 2a 2a 20 20 20 7d 0a 2a 2f   pData.**   }.*/
51560 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 48  .#define sqliteH
51570 61 73 68 46 69 72 73 74 28 48 29 20 20 28 28 48  ashFirst(H)  ((H
51580 29 2d 3e 66 69 72 73 74 29 0a 23 64 65 66 69 6e  )->first).#defin
51590 65 20 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74  e sqliteHashNext
515a0 28 45 29 20 20 20 28 28 45 29 2d 3e 6e 65 78 74  (E)   ((E)->next
515b0 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
515c0 48 61 73 68 44 61 74 61 28 45 29 20 20 20 28 28  HashData(E)   ((
515d0 45 29 2d 3e 64 61 74 61 29 0a 23 64 65 66 69 6e  E)->data).#defin
515e0 65 20 73 71 6c 69 74 65 48 61 73 68 4b 65 79 28  e sqliteHashKey(
515f0 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79  E)    ((E)->pKey
51600 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  ).#define sqlite
51610 48 61 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28  HashKeysize(E) (
51620 28 45 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a  (E)->nKey)../*.*
51630 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
51640 69 65 73 20 69 6e 20 61 20 68 61 73 68 20 74 61  ies in a hash ta
51650 62 6c 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 73  ble.*/.#define s
51660 71 6c 69 74 65 48 61 73 68 43 6f 75 6e 74 28 48  qliteHashCount(H
51670 29 20 20 28 28 48 29 2d 3e 63 6f 75 6e 74 29 0a  )  ((H)->count).
51680 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c 49  .#endif /* _SQLI
51690 54 45 5f 48 41 53 48 5f 48 5f 20 2a 2f 0a 0a 2f  TE_HASH_H_ */../
516a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
516b0 6e 64 20 6f 66 20 68 61 73 68 2e 68 20 2a 2a 2a  nd of hash.h ***
516c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
516d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
516e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
516f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
51700 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
51710 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73  we left off in s
51720 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a  qliteInt.h *****
51730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
51740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
51750 6e 63 6c 75 64 65 20 70 61 72 73 65 2e 68 20 69  nclude parse.h i
51760 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20  n the middle of 
51770 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
51780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
51790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
517a0 65 67 69 6e 20 66 69 6c 65 20 70 61 72 73 65 2e  egin file parse.
517b0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
517c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
517d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23  *************/.#
517e0 64 65 66 69 6e 65 20 54 4b 5f 53 45 4d 49 20 20  define TK_SEMI  
517f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51800 20 20 20 20 20 20 20 20 20 20 31 0a 23 64 65 66            1.#def
51810 69 6e 65 20 54 4b 5f 45 58 50 4c 41 49 4e 20 20  ine TK_EXPLAIN  
51820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51830 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e 65         2.#define
51840 20 54 4b 5f 51 55 45 52 59 20 20 20 20 20 20 20   TK_QUERY       
51850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51860 20 20 20 20 33 0a 23 64 65 66 69 6e 65 20 54 4b      3.#define TK
51870 5f 50 4c 41 4e 20 20 20 20 20 20 20 20 20 20 20  _PLAN           
51880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51890 20 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 45   4.#define TK_BE
518a0 47 49 4e 20 20 20 20 20 20 20 20 20 20 20 20 20  GIN             
518b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a                5.
518c0 23 64 65 66 69 6e 65 20 54 4b 5f 54 52 41 4e 53  #define TK_TRANS
518d0 41 43 54 49 4f 4e 20 20 20 20 20 20 20 20 20 20  ACTION          
518e0 20 20 20 20 20 20 20 20 20 20 20 36 0a 23 64 65             6.#de
518f0 66 69 6e 65 20 54 4b 5f 44 45 46 45 52 52 45 44  fine TK_DEFERRED
51900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51910 20 20 20 20 20 20 20 20 37 0a 23 64 65 66 69 6e          7.#defin
51920 65 20 54 4b 5f 49 4d 4d 45 44 49 41 54 45 20 20  e TK_IMMEDIATE  
51930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51940 20 20 20 20 20 38 0a 23 64 65 66 69 6e 65 20 54       8.#define T
51950 4b 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20 20  K_EXCLUSIVE     
51960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51970 20 20 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43    9.#define TK_C
51980 4f 4d 4d 49 54 20 20 20 20 20 20 20 20 20 20 20  OMMIT           
51990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
519a0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45 4e 44 20  .#define TK_END 
519b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
519c0 20 20 20 20 20 20 20 20 20 20 20 31 31 0a 23 64             11.#d
519d0 65 66 69 6e 65 20 54 4b 5f 52 4f 4c 4c 42 41 43  efine TK_ROLLBAC
519e0 4b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  K               
519f0 20 20 20 20 20 20 20 20 31 32 0a 23 64 65 66 69          12.#defi
51a00 6e 65 20 54 4b 5f 43 52 45 41 54 45 20 20 20 20  ne TK_CREATE    
51a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a20 20 20 20 20 20 31 33 0a 23 64 65 66 69 6e 65 20       13.#define 
51a30 54 4b 5f 54 41 42 4c 45 20 20 20 20 20 20 20 20  TK_TABLE        
51a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51a50 20 20 31 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    14.#define TK_
51a60 49 46 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IF              
51a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
51a80 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4e 4f 54  5.#define TK_NOT
51a90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51aa0 20 20 20 20 20 20 20 20 20 20 20 20 31 36 0a 23              16.#
51ab0 64 65 66 69 6e 65 20 54 4b 5f 45 58 49 53 54 53  define TK_EXISTS
51ac0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51ad0 20 20 20 20 20 20 20 20 20 31 37 0a 23 64 65 66           17.#def
51ae0 69 6e 65 20 54 4b 5f 54 45 4d 50 20 20 20 20 20  ine TK_TEMP     
51af0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b00 20 20 20 20 20 20 31 38 0a 23 64 65 66 69 6e 65        18.#define
51b10 20 54 4b 5f 4c 50 20 20 20 20 20 20 20 20 20 20   TK_LP          
51b20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b30 20 20 20 31 39 0a 23 64 65 66 69 6e 65 20 54 4b     19.#define TK
51b40 5f 52 50 20 20 20 20 20 20 20 20 20 20 20 20 20  _RP             
51b50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b60 32 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53  20.#define TK_AS
51b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51b80 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31 0a               21.
51b90 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4d 4d 41  #define TK_COMMA
51ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51bb0 20 20 20 20 20 20 20 20 20 20 32 32 0a 23 64 65            22.#de
51bc0 66 69 6e 65 20 54 4b 5f 49 44 20 20 20 20 20 20  fine TK_ID      
51bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51be0 20 20 20 20 20 20 20 32 33 0a 23 64 65 66 69 6e         23.#defin
51bf0 65 20 54 4b 5f 41 42 4f 52 54 20 20 20 20 20 20  e TK_ABORT      
51c00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51c10 20 20 20 20 32 34 0a 23 64 65 66 69 6e 65 20 54      24.#define T
51c20 4b 5f 41 46 54 45 52 20 20 20 20 20 20 20 20 20  K_AFTER         
51c30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51c40 20 32 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41   25.#define TK_A
51c50 4e 41 4c 59 5a 45 20 20 20 20 20 20 20 20 20 20  NALYZE          
51c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 36                26
51c70 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 53 43 20  .#define TK_ASC 
51c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51c90 20 20 20 20 20 20 20 20 20 20 20 32 37 0a 23 64             27.#d
51ca0 65 66 69 6e 65 20 54 4b 5f 41 54 54 41 43 48 20  efine TK_ATTACH 
51cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51cc0 20 20 20 20 20 20 20 20 32 38 0a 23 64 65 66 69          28.#defi
51cd0 6e 65 20 54 4b 5f 42 45 46 4f 52 45 20 20 20 20  ne TK_BEFORE    
51ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51cf0 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65 20       29.#define 
51d00 54 4b 5f 43 41 53 43 41 44 45 20 20 20 20 20 20  TK_CASCADE      
51d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51d20 20 20 33 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f    30.#define TK_
51d30 43 41 53 54 20 20 20 20 20 20 20 20 20 20 20 20  CAST            
51d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33                 3
51d50 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43 4f 4e  1.#define TK_CON
51d60 46 4c 49 43 54 20 20 20 20 20 20 20 20 20 20 20  FLICT           
51d70 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23              32.#
51d80 64 65 66 69 6e 65 20 54 4b 5f 44 41 54 41 42 41  define TK_DATABA
51d90 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
51da0 20 20 20 20 20 20 20 20 20 33 33 0a 23 64 65 66           33.#def
51db0 69 6e 65 20 54 4b 5f 44 45 53 43 20 20 20 20 20  ine TK_DESC     
51dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51dd0 20 20 20 20 20 20 33 34 0a 23 64 65 66 69 6e 65        34.#define
51de0 20 54 4b 5f 44 45 54 41 43 48 20 20 20 20 20 20   TK_DETACH      
51df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51e00 20 20 20 33 35 0a 23 64 65 66 69 6e 65 20 54 4b     35.#define TK
51e10 5f 45 41 43 48 20 20 20 20 20 20 20 20 20 20 20  _EACH           
51e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51e30 33 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 41  36.#define TK_FA
51e40 49 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  IL              
51e50 20 20 20 20 20 20 20 20 20 20 20 20 20 33 37 0a               37.
51e60 23 64 65 66 69 6e 65 20 54 4b 5f 46 4f 52 20 20  #define TK_FOR  
51e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51e80 20 20 20 20 20 20 20 20 20 20 33 38 0a 23 64 65            38.#de
51e90 66 69 6e 65 20 54 4b 5f 49 47 4e 4f 52 45 20 20  fine TK_IGNORE  
51ea0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51eb0 20 20 20 20 20 20 20 33 39 0a 23 64 65 66 69 6e         39.#defin
51ec0 65 20 54 4b 5f 49 4e 49 54 49 41 4c 4c 59 20 20  e TK_INITIALLY  
51ed0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51ee0 20 20 20 20 34 30 0a 23 64 65 66 69 6e 65 20 54      40.#define T
51ef0 4b 5f 49 4e 53 54 45 41 44 20 20 20 20 20 20 20  K_INSTEAD       
51f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51f10 20 34 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4c   41.#define TK_L
51f20 49 4b 45 5f 4b 57 20 20 20 20 20 20 20 20 20 20  IKE_KW          
51f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 32                42
51f40 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4d 41 54 43  .#define TK_MATC
51f50 48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  H               
51f60 20 20 20 20 20 20 20 20 20 20 20 34 33 0a 23 64             43.#d
51f70 65 66 69 6e 65 20 54 4b 5f 4b 45 59 20 20 20 20  efine TK_KEY    
51f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51f90 20 20 20 20 20 20 20 20 34 34 0a 23 64 65 66 69          44.#defi
51fa0 6e 65 20 54 4b 5f 4f 46 20 20 20 20 20 20 20 20  ne TK_OF        
51fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51fc0 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65 20       45.#define 
51fd0 54 4b 5f 4f 46 46 53 45 54 20 20 20 20 20 20 20  TK_OFFSET       
51fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
51ff0 20 20 34 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f    46.#define TK_
52000 50 52 41 47 4d 41 20 20 20 20 20 20 20 20 20 20  PRAGMA          
52010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34                 4
52020 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 41 49  7.#define TK_RAI
52030 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20  SE              
52040 20 20 20 20 20 20 20 20 20 20 20 20 34 38 0a 23              48.#
52050 64 65 66 69 6e 65 20 54 4b 5f 52 45 50 4c 41 43  define TK_REPLAC
52060 45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  E               
52070 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66           49.#def
52080 69 6e 65 20 54 4b 5f 52 45 53 54 52 49 43 54 20  ine TK_RESTRICT 
52090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
520a0 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e 65        50.#define
520b0 20 54 4b 5f 52 4f 57 20 20 20 20 20 20 20 20 20   TK_ROW         
520c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
520d0 20 20 20 35 31 0a 23 64 65 66 69 6e 65 20 54 4b     51.#define TK
520e0 5f 54 52 49 47 47 45 52 20 20 20 20 20 20 20 20  _TRIGGER        
520f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52100 35 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 56 41  52.#define TK_VA
52110 43 55 55 4d 20 20 20 20 20 20 20 20 20 20 20 20  CUUM            
52120 20 20 20 20 20 20 20 20 20 20 20 20 20 35 33 0a               53.
52130 23 64 65 66 69 6e 65 20 54 4b 5f 56 49 45 57 20  #define TK_VIEW 
52140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52150 20 20 20 20 20 20 20 20 20 20 35 34 0a 23 64 65            54.#de
52160 66 69 6e 65 20 54 4b 5f 56 49 52 54 55 41 4c 20  fine TK_VIRTUAL 
52170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52180 20 20 20 20 20 20 20 35 35 0a 23 64 65 66 69 6e         55.#defin
52190 65 20 54 4b 5f 52 45 49 4e 44 45 58 20 20 20 20  e TK_REINDEX    
521a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
521b0 20 20 20 20 35 36 0a 23 64 65 66 69 6e 65 20 54      56.#define T
521c0 4b 5f 52 45 4e 41 4d 45 20 20 20 20 20 20 20 20  K_RENAME        
521d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
521e0 20 35 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   57.#define TK_C
521f0 54 49 4d 45 5f 4b 57 20 20 20 20 20 20 20 20 20  TIME_KW         
52200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 38                58
52210 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 4e 59 20  .#define TK_ANY 
52220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52230 20 20 20 20 20 20 20 20 20 20 20 35 39 0a 23 64             59.#d
52240 65 66 69 6e 65 20 54 4b 5f 4f 52 20 20 20 20 20  efine TK_OR     
52250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52260 20 20 20 20 20 20 20 20 36 30 0a 23 64 65 66 69          60.#defi
52270 6e 65 20 54 4b 5f 41 4e 44 20 20 20 20 20 20 20  ne TK_AND       
52280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52290 20 20 20 20 20 36 31 0a 23 64 65 66 69 6e 65 20       61.#define 
522a0 54 4b 5f 49 53 20 20 20 20 20 20 20 20 20 20 20  TK_IS           
522b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
522c0 20 20 36 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f    62.#define TK_
522d0 42 45 54 57 45 45 4e 20 20 20 20 20 20 20 20 20  BETWEEN         
522e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
522f0 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 20  3.#define TK_IN 
52300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52310 20 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23              64.#
52320 64 65 66 69 6e 65 20 54 4b 5f 49 53 4e 55 4c 4c  define TK_ISNULL
52330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52340 20 20 20 20 20 20 20 20 20 36 35 0a 23 64 65 66           65.#def
52350 69 6e 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20  ine TK_NOTNULL  
52360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52370 20 20 20 20 20 20 36 36 0a 23 64 65 66 69 6e 65        66.#define
52380 20 54 4b 5f 4e 45 20 20 20 20 20 20 20 20 20 20   TK_NE          
52390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
523a0 20 20 20 36 37 0a 23 64 65 66 69 6e 65 20 54 4b     67.#define TK
523b0 5f 45 51 20 20 20 20 20 20 20 20 20 20 20 20 20  _EQ             
523c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
523d0 36 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 47 54  68.#define TK_GT
523e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
523f0 20 20 20 20 20 20 20 20 20 20 20 20 20 36 39 0a               69.
52400 23 64 65 66 69 6e 65 20 54 4b 5f 4c 45 20 20 20  #define TK_LE   
52410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52420 20 20 20 20 20 20 20 20 20 20 37 30 0a 23 64 65            70.#de
52430 66 69 6e 65 20 54 4b 5f 4c 54 20 20 20 20 20 20  fine TK_LT      
52440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52450 20 20 20 20 20 20 20 37 31 0a 23 64 65 66 69 6e         71.#defin
52460 65 20 54 4b 5f 47 45 20 20 20 20 20 20 20 20 20  e TK_GE         
52470 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52480 20 20 20 20 37 32 0a 23 64 65 66 69 6e 65 20 54      72.#define T
52490 4b 5f 45 53 43 41 50 45 20 20 20 20 20 20 20 20  K_ESCAPE        
524a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
524b0 20 37 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42   73.#define TK_B
524c0 49 54 41 4e 44 20 20 20 20 20 20 20 20 20 20 20  ITAND           
524d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37 34                74
524e0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 42 49 54 4f  .#define TK_BITO
524f0 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
52500 20 20 20 20 20 20 20 20 20 20 20 37 35 0a 23 64             75.#d
52510 65 66 69 6e 65 20 54 4b 5f 4c 53 48 49 46 54 20  efine TK_LSHIFT 
52520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52530 20 20 20 20 20 20 20 20 37 36 0a 23 64 65 66 69          76.#defi
52540 6e 65 20 54 4b 5f 52 53 48 49 46 54 20 20 20 20  ne TK_RSHIFT    
52550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52560 20 20 20 20 20 37 37 0a 23 64 65 66 69 6e 65 20       77.#define 
52570 54 4b 5f 50 4c 55 53 20 20 20 20 20 20 20 20 20  TK_PLUS         
52580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52590 20 20 37 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f    78.#define TK_
525a0 4d 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20  MINUS           
525b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
525c0 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 54 41  9.#define TK_STA
525d0 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
525e0 20 20 20 20 20 20 20 20 20 20 20 20 38 30 0a 23              80.#
525f0 64 65 66 69 6e 65 20 54 4b 5f 53 4c 41 53 48 20  define TK_SLASH 
52600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52610 20 20 20 20 20 20 20 20 20 38 31 0a 23 64 65 66           81.#def
52620 69 6e 65 20 54 4b 5f 52 45 4d 20 20 20 20 20 20  ine TK_REM      
52630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52640 20 20 20 20 20 20 38 32 0a 23 64 65 66 69 6e 65        82.#define
52650 20 54 4b 5f 43 4f 4e 43 41 54 20 20 20 20 20 20   TK_CONCAT      
52660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52670 20 20 20 38 33 0a 23 64 65 66 69 6e 65 20 54 4b     83.#define TK
52680 5f 43 4f 4c 4c 41 54 45 20 20 20 20 20 20 20 20  _COLLATE        
52690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
526a0 38 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55 4d  84.#define TK_UM
526b0 49 4e 55 53 20 20 20 20 20 20 20 20 20 20 20 20  INUS            
526c0 20 20 20 20 20 20 20 20 20 20 20 20 20 38 35 0a               85.
526d0 23 64 65 66 69 6e 65 20 54 4b 5f 55 50 4c 55 53  #define TK_UPLUS
526e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
526f0 20 20 20 20 20 20 20 20 20 20 38 36 0a 23 64 65            86.#de
52700 66 69 6e 65 20 54 4b 5f 42 49 54 4e 4f 54 20 20  fine TK_BITNOT  
52710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52720 20 20 20 20 20 20 20 38 37 0a 23 64 65 66 69 6e         87.#defin
52730 65 20 54 4b 5f 53 54 52 49 4e 47 20 20 20 20 20  e TK_STRING     
52740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52750 20 20 20 20 38 38 0a 23 64 65 66 69 6e 65 20 54      88.#define T
52760 4b 5f 4a 4f 49 4e 5f 4b 57 20 20 20 20 20 20 20  K_JOIN_KW       
52770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52780 20 38 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43   89.#define TK_C
52790 4f 4e 53 54 52 41 49 4e 54 20 20 20 20 20 20 20  ONSTRAINT       
527a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39 30                90
527b0 0a 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 41  .#define TK_DEFA
527c0 55 4c 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ULT             
527d0 20 20 20 20 20 20 20 20 20 20 20 39 31 0a 23 64             91.#d
527e0 65 66 69 6e 65 20 54 4b 5f 4e 55 4c 4c 20 20 20  efine TK_NULL   
527f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52800 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66 69          92.#defi
52810 6e 65 20 54 4b 5f 50 52 49 4d 41 52 59 20 20 20  ne TK_PRIMARY   
52820 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52830 20 20 20 20 20 39 33 0a 23 64 65 66 69 6e 65 20       93.#define 
52840 54 4b 5f 55 4e 49 51 55 45 20 20 20 20 20 20 20  TK_UNIQUE       
52850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52860 20 20 39 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f    94.#define TK_
52870 43 48 45 43 4b 20 20 20 20 20 20 20 20 20 20 20  CHECK           
52880 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
52890 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 52 45 46  5.#define TK_REF
528a0 45 52 45 4e 43 45 53 20 20 20 20 20 20 20 20 20  ERENCES         
528b0 20 20 20 20 20 20 20 20 20 20 20 20 39 36 0a 23              96.#
528c0 64 65 66 69 6e 65 20 54 4b 5f 41 55 54 4f 49 4e  define TK_AUTOIN
528d0 43 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  CR              
528e0 20 20 20 20 20 20 20 20 20 39 37 0a 23 64 65 66           97.#def
528f0 69 6e 65 20 54 4b 5f 4f 4e 20 20 20 20 20 20 20  ine TK_ON       
52900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52910 20 20 20 20 20 20 39 38 0a 23 64 65 66 69 6e 65        98.#define
52920 20 54 4b 5f 44 45 4c 45 54 45 20 20 20 20 20 20   TK_DELETE      
52930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52940 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 54 4b     99.#define TK
52950 5f 55 50 44 41 54 45 20 20 20 20 20 20 20 20 20  _UPDATE         
52960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52970 31 30 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  100.#define TK_I
52980 4e 53 45 52 54 20 20 20 20 20 20 20 20 20 20 20  NSERT           
52990 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
529a0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 54  1.#define TK_SET
529b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
529c0 20 20 20 20 20 20 20 20 20 20 20 20 31 30 32 0a              102.
529d0 23 64 65 66 69 6e 65 20 54 4b 5f 44 45 46 45 52  #define TK_DEFER
529e0 52 41 42 4c 45 20 20 20 20 20 20 20 20 20 20 20  RABLE           
529f0 20 20 20 20 20 20 20 20 20 20 31 30 33 0a 23 64            103.#d
52a00 65 66 69 6e 65 20 54 4b 5f 46 4f 52 45 49 47 4e  efine TK_FOREIGN
52a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a20 20 20 20 20 20 20 20 20 31 30 34 0a 23 64 65 66          104.#def
52a30 69 6e 65 20 54 4b 5f 44 52 4f 50 20 20 20 20 20  ine TK_DROP     
52a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a50 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e        105.#defin
52a60 65 20 54 4b 5f 55 4e 49 4f 4e 20 20 20 20 20 20  e TK_UNION      
52a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52a80 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e 65 20      106.#define 
52a90 54 4b 5f 41 4c 4c 20 20 20 20 20 20 20 20 20 20  TK_ALL          
52aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ab0 20 20 31 30 37 0a 23 64 65 66 69 6e 65 20 54 4b    107.#define TK
52ac0 5f 45 58 43 45 50 54 20 20 20 20 20 20 20 20 20  _EXCEPT         
52ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ae0 31 30 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  108.#define TK_I
52af0 4e 54 45 52 53 45 43 54 20 20 20 20 20 20 20 20  NTERSECT        
52b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 30                10
52b10 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 53 45 4c  9.#define TK_SEL
52b20 45 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20  ECT             
52b30 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 0a              110.
52b40 23 64 65 66 69 6e 65 20 54 4b 5f 44 49 53 54 49  #define TK_DISTI
52b50 4e 43 54 20 20 20 20 20 20 20 20 20 20 20 20 20  NCT             
52b60 20 20 20 20 20 20 20 20 20 20 31 31 31 0a 23 64            111.#d
52b70 65 66 69 6e 65 20 54 4b 5f 44 4f 54 20 20 20 20  efine TK_DOT    
52b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52b90 20 20 20 20 20 20 20 20 31 31 32 0a 23 64 65 66          112.#def
52ba0 69 6e 65 20 54 4b 5f 46 52 4f 4d 20 20 20 20 20  ine TK_FROM     
52bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52bc0 20 20 20 20 20 20 31 31 33 0a 23 64 65 66 69 6e        113.#defin
52bd0 65 20 54 4b 5f 4a 4f 49 4e 20 20 20 20 20 20 20  e TK_JOIN       
52be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52bf0 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20      114.#define 
52c00 54 4b 5f 49 4e 44 45 58 45 44 20 20 20 20 20 20  TK_INDEXED      
52c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52c20 20 20 31 31 35 0a 23 64 65 66 69 6e 65 20 54 4b    115.#define TK
52c30 5f 42 59 20 20 20 20 20 20 20 20 20 20 20 20 20  _BY             
52c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52c50 31 31 36 0a 23 64 65 66 69 6e 65 20 54 4b 5f 55  116.#define TK_U
52c60 53 49 4e 47 20 20 20 20 20 20 20 20 20 20 20 20  SING            
52c70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
52c80 37 0a 23 64 65 66 69 6e 65 20 54 4b 5f 4f 52 44  7.#define TK_ORD
52c90 45 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ER              
52ca0 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 0a              118.
52cb0 23 64 65 66 69 6e 65 20 54 4b 5f 47 52 4f 55 50  #define TK_GROUP
52cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52cd0 20 20 20 20 20 20 20 20 20 20 31 31 39 0a 23 64            119.#d
52ce0 65 66 69 6e 65 20 54 4b 5f 48 41 56 49 4e 47 20  efine TK_HAVING 
52cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d00 20 20 20 20 20 20 20 20 31 32 30 0a 23 64 65 66          120.#def
52d10 69 6e 65 20 54 4b 5f 4c 49 4d 49 54 20 20 20 20  ine TK_LIMIT    
52d20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d30 20 20 20 20 20 20 31 32 31 0a 23 64 65 66 69 6e        121.#defin
52d40 65 20 54 4b 5f 57 48 45 52 45 20 20 20 20 20 20  e TK_WHERE      
52d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d60 20 20 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20      122.#define 
52d70 54 4b 5f 49 4e 54 4f 20 20 20 20 20 20 20 20 20  TK_INTO         
52d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52d90 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20 54 4b    123.#define TK
52da0 5f 56 41 4c 55 45 53 20 20 20 20 20 20 20 20 20  _VALUES         
52db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52dc0 31 32 34 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49  124.#define TK_I
52dd0 4e 54 45 47 45 52 20 20 20 20 20 20 20 20 20 20  NTEGER          
52de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
52df0 35 0a 23 64 65 66 69 6e 65 20 54 4b 5f 46 4c 4f  5.#define TK_FLO
52e00 41 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AT              
52e10 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36 0a              126.
52e20 23 64 65 66 69 6e 65 20 54 4b 5f 42 4c 4f 42 20  #define TK_BLOB 
52e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52e40 20 20 20 20 20 20 20 20 20 20 31 32 37 0a 23 64            127.#d
52e50 65 66 69 6e 65 20 54 4b 5f 52 45 47 49 53 54 45  efine TK_REGISTE
52e60 52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  R               
52e70 20 20 20 20 20 20 20 20 31 32 38 0a 23 64 65 66          128.#def
52e80 69 6e 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 20  ine TK_VARIABLE 
52e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ea0 20 20 20 20 20 20 31 32 39 0a 23 64 65 66 69 6e        129.#defin
52eb0 65 20 54 4b 5f 43 41 53 45 20 20 20 20 20 20 20  e TK_CASE       
52ec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52ed0 20 20 20 20 31 33 30 0a 23 64 65 66 69 6e 65 20      130.#define 
52ee0 54 4b 5f 57 48 45 4e 20 20 20 20 20 20 20 20 20  TK_WHEN         
52ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52f00 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 54 4b    131.#define TK
52f10 5f 54 48 45 4e 20 20 20 20 20 20 20 20 20 20 20  _THEN           
52f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52f30 31 33 32 0a 23 64 65 66 69 6e 65 20 54 4b 5f 45  132.#define TK_E
52f40 4c 53 45 20 20 20 20 20 20 20 20 20 20 20 20 20  LSE             
52f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
52f60 33 0a 23 64 65 66 69 6e 65 20 54 4b 5f 49 4e 44  3.#define TK_IND
52f70 45 58 20 20 20 20 20 20 20 20 20 20 20 20 20 20  EX              
52f80 20 20 20 20 20 20 20 20 20 20 20 20 31 33 34 0a              134.
52f90 23 64 65 66 69 6e 65 20 54 4b 5f 41 4c 54 45 52  #define TK_ALTER
52fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52fb0 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64            135.#d
52fc0 65 66 69 6e 65 20 54 4b 5f 54 4f 20 20 20 20 20  efine TK_TO     
52fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
52fe0 20 20 20 20 20 20 20 20 31 33 36 0a 23 64 65 66          136.#def
52ff0 69 6e 65 20 54 4b 5f 41 44 44 20 20 20 20 20 20  ine TK_ADD      
53000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53010 20 20 20 20 20 20 31 33 37 0a 23 64 65 66 69 6e        137.#defin
53020 65 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 20 20 20  e TK_COLUMNKW   
53030 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53040 20 20 20 20 31 33 38 0a 23 64 65 66 69 6e 65 20      138.#define 
53050 54 4b 5f 54 4f 5f 54 45 58 54 20 20 20 20 20 20  TK_TO_TEXT      
53060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53070 20 20 31 33 39 0a 23 64 65 66 69 6e 65 20 54 4b    139.#define TK
53080 5f 54 4f 5f 42 4c 4f 42 20 20 20 20 20 20 20 20  _TO_BLOB        
53090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
530a0 31 34 30 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54  140.#define TK_T
530b0 4f 5f 4e 55 4d 45 52 49 43 20 20 20 20 20 20 20  O_NUMERIC       
530c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
530d0 31 0a 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f  1.#define TK_TO_
530e0 49 4e 54 20 20 20 20 20 20 20 20 20 20 20 20 20  INT             
530f0 20 20 20 20 20 20 20 20 20 20 20 20 31 34 32 0a              142.
53100 23 64 65 66 69 6e 65 20 54 4b 5f 54 4f 5f 52 45  #define TK_TO_RE
53110 41 4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  AL              
53120 20 20 20 20 20 20 20 20 20 20 31 34 33 0a 23 64            143.#d
53130 65 66 69 6e 65 20 54 4b 5f 45 4e 44 5f 4f 46 5f  efine TK_END_OF_
53140 46 49 4c 45 20 20 20 20 20 20 20 20 20 20 20 20  FILE            
53150 20 20 20 20 20 20 20 20 31 34 34 0a 23 64 65 66          144.#def
53160 69 6e 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 20 20  ine TK_ILLEGAL  
53170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53180 20 20 20 20 20 20 31 34 35 0a 23 64 65 66 69 6e        145.#defin
53190 65 20 54 4b 5f 53 50 41 43 45 20 20 20 20 20 20  e TK_SPACE      
531a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
531b0 20 20 20 20 31 34 36 0a 23 64 65 66 69 6e 65 20      146.#define 
531c0 54 4b 5f 55 4e 43 4c 4f 53 45 44 5f 53 54 52 49  TK_UNCLOSED_STRI
531d0 4e 47 20 20 20 20 20 20 20 20 20 20 20 20 20 20  NG              
531e0 20 20 31 34 37 0a 23 64 65 66 69 6e 65 20 54 4b    147.#define TK
531f0 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
53200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
53210 31 34 38 0a 23 64 65 66 69 6e 65 20 54 4b 5f 43  148.#define TK_C
53220 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
53230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 34                14
53240 39 0a 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47  9.#define TK_AGG
53250 5f 46 55 4e 43 54 49 4f 4e 20 20 20 20 20 20 20  _FUNCTION       
53260 20 20 20 20 20 20 20 20 20 20 20 20 31 35 30 0a              150.
53270 23 64 65 66 69 6e 65 20 54 4b 5f 41 47 47 5f 43  #define TK_AGG_C
53280 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20  OLUMN           
53290 20 20 20 20 20 20 20 20 20 20 31 35 31 0a 23 64            151.#d
532a0 65 66 69 6e 65 20 54 4b 5f 43 4f 4e 53 54 5f 46  efine TK_CONST_F
532b0 55 4e 43 20 20 20 20 20 20 20 20 20 20 20 20 20  UNC             
532c0 20 20 20 20 20 20 20 20 31 35 32 0a 0a 2f 2a 2a          152../**
532d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
532e0 20 6f 66 20 70 61 72 73 65 2e 68 20 2a 2a 2a 2a   of parse.h ****
532f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53300 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
53310 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
53320 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e  ************ Con
53330 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65  tinuing where we
53340 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c   left off in sql
53350 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  iteInt.h *******
53360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 6e  ***********/.#in
53370 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a  clude <stdio.h>.
53380 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62  #include <stdlib
53390 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74  .h>.#include <st
533a0 72 69 6e 67 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  ring.h>.#include
533b0 20 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63   <assert.h>.#inc
533c0 6c 75 64 65 20 3c 73 74 64 64 65 66 2e 68 3e 0a  lude <stddef.h>.
533d0 0a 2f 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c  ./*.** If compil
533e0 69 6e 67 20 66 6f 72 20 61 20 70 72 6f 63 65 73  ing for a proces
533f0 73 6f 72 20 74 68 61 74 20 6c 61 63 6b 73 20 66  sor that lacks f
53400 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 73 75  loating point su
53410 70 70 6f 72 74 2c 0a 2a 2a 20 73 75 62 73 74 69  pport,.** substi
53420 74 75 74 65 20 69 6e 74 65 67 65 72 20 66 6f 72  tute integer for
53430 20 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 0a   floating-point.
53440 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
53450 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
53460 4f 49 4e 54 0a 23 20 64 65 66 69 6e 65 20 64 6f  OINT.# define do
53470 75 62 6c 65 20 73 71 6c 69 74 65 5f 69 6e 74 36  uble sqlite_int6
53480 34 0a 23 20 64 65 66 69 6e 65 20 4c 4f 4e 47 44  4.# define LONGD
53490 4f 55 42 4c 45 5f 54 59 50 45 20 73 71 6c 69 74  OUBLE_TYPE sqlit
534a0 65 5f 69 6e 74 36 34 0a 23 20 69 66 6e 64 65 66  e_int64.# ifndef
534b0 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a   SQLITE_BIG_DBL.
534c0 23 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  #   define SQLIT
534d0 45 5f 42 49 47 5f 44 42 4c 20 28 30 78 37 66 66  E_BIG_DBL (0x7ff
534e0 66 66 66 66 66 66 66 66 66 66 66 66 66 29 0a 23  fffffffffffff).#
534f0 20 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20   endif.# define 
53500 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45  SQLITE_OMIT_DATE
53510 54 49 4d 45 5f 46 55 4e 43 53 20 31 0a 23 20 64  TIME_FUNCS 1.# d
53520 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 4d 49  efine SQLITE_OMI
53530 54 5f 54 52 41 43 45 20 31 0a 23 20 75 6e 64 65  T_TRACE 1.# unde
53540 66 20 53 51 4c 49 54 45 5f 4d 49 58 45 44 5f 45  f SQLITE_MIXED_E
53550 4e 44 49 41 4e 5f 36 34 42 49 54 5f 46 4c 4f 41  NDIAN_64BIT_FLOA
53560 54 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  T.#endif.#ifndef
53570 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 0a   SQLITE_BIG_DBL.
53580 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
53590 42 49 47 5f 44 42 4c 20 28 31 65 39 39 29 0a 23  BIG_DBL (1e99).#
535a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4f 4d 49  endif../*.** OMI
535b0 54 5f 54 45 4d 50 44 42 20 69 73 20 73 65 74 20  T_TEMPDB is set 
535c0 74 6f 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f  to 1 if SQLITE_O
535d0 4d 49 54 5f 54 45 4d 50 44 42 20 69 73 20 64 65  MIT_TEMPDB is de
535e0 66 69 6e 65 64 2c 20 6f 72 20 30 0a 2a 2a 20 61  fined, or 0.** a
535f0 66 74 65 72 77 61 72 64 2e 20 48 61 76 69 6e 67  fterward. Having
53600 20 74 68 69 73 20 6d 61 63 72 6f 20 61 6c 6c 6f   this macro allo
53610 77 73 20 75 73 20 74 6f 20 63 61 75 73 65 20 74  ws us to cause t
53620 68 65 20 43 20 63 6f 6d 70 69 6c 65 72 20 0a 2a  he C compiler .*
53630 2a 20 74 6f 20 6f 6d 69 74 20 63 6f 64 65 20 75  * to omit code u
53640 73 65 64 20 62 79 20 54 45 4d 50 20 74 61 62 6c  sed by TEMP tabl
53650 65 73 20 77 69 74 68 6f 75 74 20 6d 65 73 73 79  es without messy
53660 20 23 69 66 6e 64 65 66 20 73 74 61 74 65 6d 65   #ifndef stateme
53670 6e 74 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  nts..*/.#ifdef S
53680 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
53690 42 0a 23 64 65 66 69 6e 65 20 4f 4d 49 54 5f 54  B.#define OMIT_T
536a0 45 4d 50 44 42 20 31 0a 23 65 6c 73 65 0a 23 64  EMPDB 1.#else.#d
536b0 65 66 69 6e 65 20 4f 4d 49 54 5f 54 45 4d 50 44  efine OMIT_TEMPD
536c0 42 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  B 0.#endif../*.*
536d0 2a 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  * If the followi
536e0 6e 67 20 6d 61 63 72 6f 20 69 73 20 73 65 74 20  ng macro is set 
536f0 74 6f 20 31 2c 20 74 68 65 6e 20 4e 55 4c 4c 20  to 1, then NULL 
53700 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69  values are consi
53710 64 65 72 65 64 0a 2a 2a 20 64 69 73 74 69 6e 63  dered.** distinc
53720 74 20 77 68 65 6e 20 64 65 74 65 72 6d 69 6e 69  t when determini
53730 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ng whether or no
53740 74 20 74 77 6f 20 65 6e 74 72 69 65 73 20 61 72  t two entries ar
53750 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 69 6e  e the same.** in
53760 20 61 20 55 4e 49 51 55 45 20 69 6e 64 65 78 2e   a UNIQUE index.
53770 20 20 54 68 69 73 20 69 73 20 74 68 65 20 77 61    This is the wa
53780 79 20 50 6f 73 74 67 72 65 53 51 4c 2c 20 4f 72  y PostgreSQL, Or
53790 61 63 6c 65 2c 20 44 42 32 2c 20 4d 79 53 51 4c  acle, DB2, MySQL
537a0 2c 0a 2a 2a 20 4f 43 45 4c 4f 54 2c 20 61 6e 64  ,.** OCELOT, and
537b0 20 46 69 72 65 62 69 72 64 20 61 6c 6c 20 77 6f   Firebird all wo
537c0 72 6b 2e 20 20 54 68 65 20 53 51 4c 39 32 20 73  rk.  The SQL92 s
537d0 70 65 63 20 65 78 70 6c 69 63 69 74 6c 79 20 73  pec explicitly s
537e0 61 79 73 20 74 68 69 73 0a 2a 2a 20 69 73 20 74  ays this.** is t
537f0 68 65 20 77 61 79 20 74 68 69 6e 67 73 20 61 72  he way things ar
53800 65 20 73 75 70 70 6f 73 65 20 74 6f 20 77 6f 72  e suppose to wor
53810 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  k..**.** If the 
53820 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 20  following macro 
53830 69 73 20 73 65 74 20 74 6f 20 30 2c 20 74 68 65  is set to 0, the
53840 20 4e 55 4c 4c 73 20 61 72 65 20 69 6e 64 69 73   NULLs are indis
53850 74 69 6e 63 74 20 66 6f 72 0a 2a 2a 20 61 20 55  tinct for.** a U
53860 4e 49 51 55 45 20 69 6e 64 65 78 2e 20 20 49 6e  NIQUE index.  In
53870 20 74 68 69 73 20 6d 6f 64 65 2c 20 79 6f 75 20   this mode, you 
53880 63 61 6e 20 6f 6e 6c 79 20 68 61 76 65 20 61 20  can only have a 
53890 73 69 6e 67 6c 65 20 4e 55 4c 4c 20 65 6e 74 72  single NULL entr
538a0 79 0a 2a 2a 20 66 6f 72 20 61 20 63 6f 6c 75 6d  y.** for a colum
538b0 6e 20 64 65 63 6c 61 72 65 64 20 55 4e 49 51 55  n declared UNIQU
538c0 45 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  E.  This is the 
538d0 77 61 79 20 49 6e 66 6f 72 6d 69 78 20 61 6e 64  way Informix and
538e0 20 53 51 4c 20 53 65 72 76 65 72 0a 2a 2a 20 77   SQL Server.** w
538f0 6f 72 6b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ork..*/.#define 
53900 4e 55 4c 4c 5f 44 49 53 54 49 4e 43 54 5f 46 4f  NULL_DISTINCT_FO
53910 52 5f 55 4e 49 51 55 45 20 31 0a 0a 2f 2a 0a 2a  R_UNIQUE 1../*.*
53920 2a 20 54 68 65 20 22 66 69 6c 65 20 66 6f 72 6d  * The "file form
53930 61 74 22 20 6e 75 6d 62 65 72 20 69 73 20 61 6e  at" number is an
53940 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73   integer that is
53950 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 68 65   incremented whe
53960 6e 65 76 65 72 0a 2a 2a 20 74 68 65 20 56 44 42  never.** the VDB
53970 45 2d 6c 65 76 65 6c 20 66 69 6c 65 20 66 6f 72  E-level file for
53980 6d 61 74 20 63 68 61 6e 67 65 73 2e 20 20 54 68  mat changes.  Th
53990 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63 72  e following macr
539a0 6f 73 20 64 65 66 69 6e 65 20 74 68 65 0a 2a 2a  os define the.**
539b0 20 74 68 65 20 64 65 66 61 75 6c 74 20 66 69 6c   the default fil
539c0 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 6e 65 77  e format for new
539d0 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20 74   databases and t
539e0 68 65 20 6d 61 78 69 6d 75 6d 20 66 69 6c 65 20  he maximum file 
539f0 66 6f 72 6d 61 74 0a 2a 2a 20 74 68 61 74 20 74  format.** that t
53a00 68 65 20 6c 69 62 72 61 72 79 20 63 61 6e 20 72  he library can r
53a10 65 61 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ead..*/.#define 
53a20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 49 4c 45 5f  SQLITE_MAX_FILE_
53a30 46 4f 52 4d 41 54 20 34 0a 23 69 66 6e 64 65 66  FORMAT 4.#ifndef
53a40 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
53a50 46 49 4c 45 5f 46 4f 52 4d 41 54 0a 23 20 64 65  FILE_FORMAT.# de
53a60 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
53a70 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54 20  ULT_FILE_FORMAT 
53a80 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  1.#endif../*.** 
53a90 50 72 6f 76 69 64 65 20 61 20 64 65 66 61 75 6c  Provide a defaul
53aa0 74 20 76 61 6c 75 65 20 66 6f 72 20 53 51 4c 49  t value for SQLI
53ab0 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 69 6e  TE_TEMP_STORE in
53ac0 20 63 61 73 65 20 69 74 20 69 73 20 6e 6f 74 20   case it is not 
53ad0 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 6f 6e 20  specified.** on 
53ae0 74 68 65 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65  the command-line
53af0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
53b00 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 0a 23 20  TE_TEMP_STORE.# 
53b10 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45  define SQLITE_TE
53b20 4d 50 5f 53 54 4f 52 45 20 31 0a 23 65 6e 64 69  MP_STORE 1.#endi
53b30 66 0a 0a 2f 2a 0a 2a 2a 20 47 43 43 20 64 6f 65  f../*.** GCC doe
53b40 73 20 6e 6f 74 20 64 65 66 69 6e 65 20 74 68 65  s not define the
53b50 20 6f 66 66 73 65 74 6f 66 28 29 20 6d 61 63 72   offsetof() macr
53b60 6f 20 73 6f 20 77 65 27 6c 6c 20 68 61 76 65 20  o so we'll have 
53b70 74 6f 20 64 6f 20 69 74 0a 2a 2a 20 6f 75 72 73  to do it.** ours
53b80 65 6c 76 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  elves..*/.#ifnde
53b90 66 20 6f 66 66 73 65 74 6f 66 0a 23 64 65 66 69  f offsetof.#defi
53ba0 6e 65 20 6f 66 66 73 65 74 6f 66 28 53 54 52 55  ne offsetof(STRU
53bb0 43 54 55 52 45 2c 46 49 45 4c 44 29 20 28 28 69  CTURE,FIELD) ((i
53bc0 6e 74 29 28 28 63 68 61 72 2a 29 26 28 28 53 54  nt)((char*)&((ST
53bd0 52 55 43 54 55 52 45 2a 29 30 29 2d 3e 46 49 45  RUCTURE*)0)->FIE
53be0 4c 44 29 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  LD)).#endif../*.
53bf0 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
53c00 69 66 20 74 68 69 73 20 6d 61 63 68 69 6e 65 20  if this machine 
53c10 75 73 65 73 20 45 42 43 44 49 43 2e 20 20 28 59  uses EBCDIC.  (Y
53c20 65 73 2c 20 62 65 6c 69 65 76 65 20 69 74 20 6f  es, believe it o
53c30 72 0a 2a 2a 20 6e 6f 74 2c 20 74 68 65 72 65 20  r.** not, there 
53c40 61 72 65 20 73 74 69 6c 6c 20 6d 61 63 68 69 6e  are still machin
53c50 65 73 20 6f 75 74 20 74 68 65 72 65 20 74 68 61  es out there tha
53c60 74 20 75 73 65 20 45 42 43 44 49 43 2e 29 0a 2a  t use EBCDIC.).*
53c70 2f 0a 23 69 66 20 27 41 27 20 3d 3d 20 27 5c 33  /.#if 'A' == '\3
53c80 30 31 27 0a 23 20 64 65 66 69 6e 65 20 53 51 4c  01'.# define SQL
53c90 49 54 45 5f 45 42 43 44 49 43 20 31 0a 23 65 6c  ITE_EBCDIC 1.#el
53ca0 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
53cb0 54 45 5f 41 53 43 49 49 20 31 0a 23 65 6e 64 69  TE_ASCII 1.#endi
53cc0 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 67 65 72  f../*.** Integer
53cd0 73 20 6f 66 20 6b 6e 6f 77 6e 20 73 69 7a 65 73  s of known sizes
53ce0 2e 20 20 54 68 65 73 65 20 74 79 70 65 64 65 66  .  These typedef
53cf0 73 20 6d 69 67 68 74 20 63 68 61 6e 67 65 20 66  s might change f
53d00 6f 72 20 61 72 63 68 69 74 65 63 74 75 72 65 73  or architectures
53d10 0a 2a 2a 20 77 68 65 72 65 20 74 68 65 20 73 69  .** where the si
53d20 7a 65 73 20 76 65 72 79 2e 20 20 50 72 65 70 72  zes very.  Prepr
53d30 6f 63 65 73 73 6f 72 20 6d 61 63 72 6f 73 20 61  ocessor macros a
53d40 72 65 20 61 76 61 69 6c 61 62 6c 65 20 73 6f 20  re available so 
53d50 74 68 61 74 20 74 68 65 0a 2a 2a 20 74 79 70 65  that the.** type
53d60 73 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 6e 69  s can be conveni
53d70 65 6e 74 6c 79 20 72 65 64 65 66 69 6e 65 64 20  ently redefined 
53d80 61 74 20 63 6f 6d 70 69 6c 65 2d 74 79 70 65 2e  at compile-type.
53d90 20 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a    Like this:.**.
53da0 2a 2a 20 20 20 20 20 20 20 20 20 63 63 20 27 2d  **         cc '-
53db0 44 55 49 4e 54 50 54 52 5f 54 59 50 45 3d 6c 6f  DUINTPTR_TYPE=lo
53dc0 6e 67 20 6c 6f 6e 67 20 69 6e 74 27 20 2e 2e 2e  ng long int' ...
53dd0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 55 49 4e 54  .*/.#ifndef UINT
53de0 33 32 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20  32_TYPE.# ifdef 
53df0 48 41 56 45 5f 55 49 4e 54 33 32 5f 54 0a 23 20  HAVE_UINT32_T.# 
53e00 20 64 65 66 69 6e 65 20 55 49 4e 54 33 32 5f 54   define UINT32_T
53e10 59 50 45 20 75 69 6e 74 33 32 5f 74 0a 23 20 65  YPE uint32_t.# e
53e20 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49  lse.#  define UI
53e30 4e 54 33 32 5f 54 59 50 45 20 75 6e 73 69 67 6e  NT32_TYPE unsign
53e40 65 64 20 69 6e 74 0a 23 20 65 6e 64 69 66 0a 23  ed int.# endif.#
53e50 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 55 49  endif.#ifndef UI
53e60 4e 54 31 36 5f 54 59 50 45 0a 23 20 69 66 64 65  NT16_TYPE.# ifde
53e70 66 20 48 41 56 45 5f 55 49 4e 54 31 36 5f 54 0a  f HAVE_UINT16_T.
53e80 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 31 36  #  define UINT16
53e90 5f 54 59 50 45 20 75 69 6e 74 31 36 5f 74 0a 23  _TYPE uint16_t.#
53ea0 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20   else.#  define 
53eb0 55 49 4e 54 31 36 5f 54 59 50 45 20 75 6e 73 69  UINT16_TYPE unsi
53ec0 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a 23  gned short int.#
53ed0 20 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69   endif.#endif.#i
53ee0 66 6e 64 65 66 20 49 4e 54 31 36 5f 54 59 50 45  fndef INT16_TYPE
53ef0 0a 23 20 69 66 64 65 66 20 48 41 56 45 5f 49 4e  .# ifdef HAVE_IN
53f00 54 31 36 5f 54 0a 23 20 20 64 65 66 69 6e 65 20  T16_T.#  define 
53f10 49 4e 54 31 36 5f 54 59 50 45 20 69 6e 74 31 36  INT16_TYPE int16
53f20 5f 74 0a 23 20 65 6c 73 65 0a 23 20 20 64 65 66  _t.# else.#  def
53f30 69 6e 65 20 49 4e 54 31 36 5f 54 59 50 45 20 73  ine INT16_TYPE s
53f40 68 6f 72 74 20 69 6e 74 0a 23 20 65 6e 64 69 66  hort int.# endif
53f50 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
53f60 55 49 4e 54 38 5f 54 59 50 45 0a 23 20 69 66 64  UINT8_TYPE.# ifd
53f70 65 66 20 48 41 56 45 5f 55 49 4e 54 38 5f 54 0a  ef HAVE_UINT8_T.
53f80 23 20 20 64 65 66 69 6e 65 20 55 49 4e 54 38 5f  #  define UINT8_
53f90 54 59 50 45 20 75 69 6e 74 38 5f 74 0a 23 20 65  TYPE uint8_t.# e
53fa0 6c 73 65 0a 23 20 20 64 65 66 69 6e 65 20 55 49  lse.#  define UI
53fb0 4e 54 38 5f 54 59 50 45 20 75 6e 73 69 67 6e 65  NT8_TYPE unsigne
53fc0 64 20 63 68 61 72 0a 23 20 65 6e 64 69 66 0a 23  d char.# endif.#
53fd0 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 49 4e  endif.#ifndef IN
53fe0 54 38 5f 54 59 50 45 0a 23 20 69 66 64 65 66 20  T8_TYPE.# ifdef 
53ff0 48 41 56 45 5f 49 4e 54 38 5f 54 0a 23 20 20 64  HAVE_INT8_T.#  d
54000 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50 45 20  efine INT8_TYPE 
54010 69 6e 74 38 5f 74 0a 23 20 65 6c 73 65 0a 23 20  int8_t.# else.# 
54020 20 64 65 66 69 6e 65 20 49 4e 54 38 5f 54 59 50   define INT8_TYP
54030 45 20 73 69 67 6e 65 64 20 63 68 61 72 0a 23 20  E signed char.# 
54040 65 6e 64 69 66 0a 23 65 6e 64 69 66 0a 23 69 66  endif.#endif.#if
54050 6e 64 65 66 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f  ndef LONGDOUBLE_
54060 54 59 50 45 0a 23 20 64 65 66 69 6e 65 20 4c 4f  TYPE.# define LO
54070 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 6c 6f  NGDOUBLE_TYPE lo
54080 6e 67 20 64 6f 75 62 6c 65 0a 23 65 6e 64 69 66  ng double.#endif
54090 0a 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f  .typedef sqlite_
540a0 69 6e 74 36 34 20 69 36 34 3b 20 20 20 20 20 20  int64 i64;      
540b0 20 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 73 69      /* 8-byte si
540c0 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
540d0 74 79 70 65 64 65 66 20 73 71 6c 69 74 65 5f 75  typedef sqlite_u
540e0 69 6e 74 36 34 20 75 36 34 3b 20 20 20 20 20 20  int64 u64;      
540f0 20 20 20 2f 2a 20 38 2d 62 79 74 65 20 75 6e 73     /* 8-byte uns
54100 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
54110 0a 74 79 70 65 64 65 66 20 55 49 4e 54 33 32 5f  .typedef UINT32_
54120 54 59 50 45 20 75 33 32 3b 20 20 20 20 20 20 20  TYPE u32;       
54130 20 20 20 20 2f 2a 20 34 2d 62 79 74 65 20 75 6e      /* 4-byte un
54140 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
54150 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 31 36  /.typedef UINT16
54160 5f 54 59 50 45 20 75 31 36 3b 20 20 20 20 20 20  _TYPE u16;      
54170 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20 75       /* 2-byte u
54180 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20  nsigned integer 
54190 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 31 36  */.typedef INT16
541a0 5f 54 59 50 45 20 69 31 36 3b 20 20 20 20 20 20  _TYPE i16;      
541b0 20 20 20 20 20 20 2f 2a 20 32 2d 62 79 74 65 20        /* 2-byte 
541c0 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
541d0 2f 0a 74 79 70 65 64 65 66 20 55 49 4e 54 38 5f  /.typedef UINT8_
541e0 54 59 50 45 20 75 38 3b 20 20 20 20 20 20 20 20  TYPE u8;        
541f0 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20 75       /* 1-byte u
54200 6e 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20  nsigned integer 
54210 2a 2f 0a 74 79 70 65 64 65 66 20 49 4e 54 38 5f  */.typedef INT8_
54220 54 59 50 45 20 69 38 3b 20 20 20 20 20 20 20 20  TYPE i8;        
54230 20 20 20 20 20 20 2f 2a 20 31 2d 62 79 74 65 20        /* 1-byte 
54240 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
54250 2f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20  /../*.** Macros 
54260 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77 68 65  to determine whe
54270 74 68 65 72 20 74 68 65 20 6d 61 63 68 69 6e 65  ther the machine
54280 20 69 73 20 62 69 67 20 6f 72 20 6c 69 74 74 6c   is big or littl
54290 65 20 65 6e 64 69 61 6e 2c 0a 2a 2a 20 65 76 61  e endian,.** eva
542a0 6c 75 61 74 65 64 20 61 74 20 72 75 6e 74 69 6d  luated at runtim
542b0 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
542c0 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f 4e  ITE_AMALGAMATION
542d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
542e0 63 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65  const int sqlite
542f0 33 6f 6e 65 3b 0a 23 65 6c 73 65 0a 53 51 4c 49  3one;.#else.SQLI
54300 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
54310 20 69 6e 74 20 73 71 6c 69 74 65 33 6f 6e 65 3b   int sqlite3one;
54320 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66 69  .#endif.#if defi
54330 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66  ned(i386) || def
54340 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c  ined(__i386__) |
54350 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38  | defined(_M_IX8
54360 36 29 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  6)\.            
54370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
54380 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 5f 78 38   || defined(__x8
54390 36 5f 36 34 29 20 7c 7c 20 64 65 66 69 6e 65 64  6_64) || defined
543a0 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 0a 23 20 64  (__x86_64__).# d
543b0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47  efine SQLITE_BIG
543c0 45 4e 44 49 41 4e 20 20 20 20 30 0a 23 20 64 65  ENDIAN    0.# de
543d0 66 69 6e 65 20 53 51 4c 49 54 45 5f 4c 49 54 54  fine SQLITE_LITT
543e0 4c 45 45 4e 44 49 41 4e 20 31 0a 23 20 64 65 66  LEENDIAN 1.# def
543f0 69 6e 65 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ine SQLITE_UTF16
54400 4e 41 54 49 56 45 20 20 53 51 4c 49 54 45 5f 55  NATIVE  SQLITE_U
54410 54 46 31 36 4c 45 0a 23 65 6c 73 65 0a 23 20 64  TF16LE.#else.# d
54420 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 42 49 47  efine SQLITE_BIG
54430 45 4e 44 49 41 4e 20 20 20 20 28 2a 28 63 68 61  ENDIAN    (*(cha
54440 72 20 2a 29 28 26 73 71 6c 69 74 65 33 6f 6e 65  r *)(&sqlite3one
54450 29 3d 3d 30 29 0a 23 20 64 65 66 69 6e 65 20 53  )==0).# define S
54460 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e 44 49  QLITE_LITTLEENDI
54470 41 4e 20 28 2a 28 63 68 61 72 20 2a 29 28 26 73  AN (*(char *)(&s
54480 71 6c 69 74 65 33 6f 6e 65 29 3d 3d 31 29 0a 23  qlite3one)==1).#
54490 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 55   define SQLITE_U
544a0 54 46 31 36 4e 41 54 49 56 45 20 28 53 51 4c 49  TF16NATIVE (SQLI
544b0 54 45 5f 42 49 47 45 4e 44 49 41 4e 3f 53 51 4c  TE_BIGENDIAN?SQL
544c0 49 54 45 5f 55 54 46 31 36 42 45 3a 53 51 4c 49  ITE_UTF16BE:SQLI
544d0 54 45 5f 55 54 46 31 36 4c 45 29 0a 23 65 6e 64  TE_UTF16LE).#end
544e0 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 61  if../*.** Consta
544f0 6e 74 73 20 66 6f 72 20 74 68 65 20 6c 61 72 67  nts for the larg
54500 65 73 74 20 61 6e 64 20 73 6d 61 6c 6c 65 73 74  est and smallest
54510 20 70 6f 73 73 69 62 6c 65 20 36 34 2d 62 69 74   possible 64-bit
54520 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 73   signed integers
54530 2e 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f  ..** These macro
54540 73 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 74  s are designed t
54550 6f 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c 79  o work correctly
54560 20 6f 6e 20 62 6f 74 68 20 33 32 2d 62 69 74 20   on both 32-bit 
54570 61 6e 64 20 36 34 2d 62 69 74 0a 2a 2a 20 63 6f  and 64-bit.** co
54580 6d 70 69 6c 65 72 73 2e 0a 2a 2f 0a 23 64 65 66  mpilers..*/.#def
54590 69 6e 65 20 4c 41 52 47 45 53 54 5f 49 4e 54 36  ine LARGEST_INT6
545a0 34 20 20 28 30 78 66 66 66 66 66 66 66 66 7c 28  4  (0xffffffff|(
545b0 28 28 69 36 34 29 30 78 37 66 66 66 66 66 66 66  ((i64)0x7fffffff
545c0 29 3c 3c 33 32 29 29 0a 23 64 65 66 69 6e 65 20  )<<32)).#define 
545d0 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36 34 20 28  SMALLEST_INT64 (
545e0 28 28 69 36 34 29 2d 31 29 20 2d 20 4c 41 52 47  ((i64)-1) - LARG
545f0 45 53 54 5f 49 4e 54 36 34 29 0a 0a 2f 2a 0a 2a  EST_INT64)../*.*
54600 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
54610 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
54620 74 72 75 63 74 75 72 65 20 69 73 20 75 73 65 64  tructure is used
54630 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 62 75   to store the bu
54640 73 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61  sy-handler.** ca
54650 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 67 69 76  llback for a giv
54660 65 6e 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65  en sqlite handle
54670 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  . .**.** The sql
54680 69 74 65 2e 62 75 73 79 48 61 6e 64 6c 65 72 20  ite.busyHandler 
54690 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 73 71  member of the sq
546a0 6c 69 74 65 20 73 74 72 75 63 74 20 63 6f 6e 74  lite struct cont
546b0 61 69 6e 73 20 74 68 65 20 62 75 73 79 0a 2a 2a  ains the busy.**
546c0 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20 74 68   callback for th
546d0 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
546e0 65 2e 20 45 61 63 68 20 70 61 67 65 72 20 6f 70  e. Each pager op
546f0 65 6e 65 64 20 76 69 61 20 74 68 65 20 73 71 6c  ened via the sql
54700 69 74 65 0a 2a 2a 20 68 61 6e 64 6c 65 20 69 73  ite.** handle is
54710 20 70 61 73 73 65 64 20 61 20 70 6f 69 6e 74 65   passed a pointe
54720 72 20 74 6f 20 73 71 6c 69 74 65 2e 62 75 73 79  r to sqlite.busy
54730 48 61 6e 64 6c 65 72 2e 20 54 68 65 20 62 75 73  Handler. The bus
54740 79 2d 68 61 6e 64 6c 65 72 0a 2a 2a 20 63 61 6c  y-handler.** cal
54750 6c 62 61 63 6b 20 69 73 20 63 75 72 72 65 6e 74  lback is current
54760 6c 79 20 69 6e 76 6f 6b 65 64 20 6f 6e 6c 79 20  ly invoked only 
54770 66 72 6f 6d 20 77 69 74 68 69 6e 20 70 61 67 65  from within page
54780 72 2e 63 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  r.c..*/.typedef 
54790 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c  struct BusyHandl
547a0 65 72 20 42 75 73 79 48 61 6e 64 6c 65 72 3b 0a  er BusyHandler;.
547b0 73 74 72 75 63 74 20 42 75 73 79 48 61 6e 64 6c  struct BusyHandl
547c0 65 72 20 7b 0a 20 20 69 6e 74 20 28 2a 78 46 75  er {.  int (*xFu
547d0 6e 63 29 28 76 6f 69 64 20 2a 2c 69 6e 74 29 3b  nc)(void *,int);
547e0 20 20 2f 2a 20 54 68 65 20 62 75 73 79 20 63 61    /* The busy ca
547f0 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 76 6f 69 64  llback */.  void
54800 20 2a 70 41 72 67 3b 20 20 20 20 20 20 20 20 20   *pArg;         
54810 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20         /* First 
54820 61 72 67 20 74 6f 20 62 75 73 79 20 63 61 6c 6c  arg to busy call
54830 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  back */.  int nB
54840 75 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  usy;            
54850 20 20 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e       /* Incremen
54860 74 65 64 20 77 69 74 68 20 65 61 63 68 20 62 75  ted with each bu
54870 73 79 20 63 61 6c 6c 20 2a 2f 0a 7d 3b 0a 0a 2f  sy call */.};../
54880 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65  *.** Name of the
54890 20 6d 61 73 74 65 72 20 64 61 74 61 62 61 73 65   master database
548a0 20 74 61 62 6c 65 2e 20 20 54 68 65 20 6d 61 73   table.  The mas
548b0 74 65 72 20 64 61 74 61 62 61 73 65 20 74 61 62  ter database tab
548c0 6c 65 0a 2a 2a 20 69 73 20 61 20 73 70 65 63 69  le.** is a speci
548d0 61 6c 20 74 61 62 6c 65 20 74 68 61 74 20 68 6f  al table that ho
548e0 6c 64 73 20 74 68 65 20 6e 61 6d 65 73 20 61 6e  lds the names an
548f0 64 20 61 74 74 72 69 62 75 74 65 73 20 6f 66 20  d attributes of 
54900 61 6c 6c 0a 2a 2a 20 75 73 65 72 20 74 61 62 6c  all.** user tabl
54910 65 73 20 61 6e 64 20 69 6e 64 69 63 65 73 2e 0a  es and indices..
54920 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45  */.#define MASTE
54930 52 5f 4e 41 4d 45 20 20 20 20 20 20 20 22 73 71  R_NAME       "sq
54940 6c 69 74 65 5f 6d 61 73 74 65 72 22 0a 23 64 65  lite_master".#de
54950 66 69 6e 65 20 54 45 4d 50 5f 4d 41 53 54 45 52  fine TEMP_MASTER
54960 5f 4e 41 4d 45 20 20 22 73 71 6c 69 74 65 5f 74  _NAME  "sqlite_t
54970 65 6d 70 5f 6d 61 73 74 65 72 22 0a 0a 2f 2a 0a  emp_master"../*.
54980 2a 2a 20 54 68 65 20 72 6f 6f 74 2d 70 61 67 65  ** The root-page
54990 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 64   of the master d
549a0 61 74 61 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a  atabase table..*
549b0 2f 0a 23 64 65 66 69 6e 65 20 4d 41 53 54 45 52  /.#define MASTER
549c0 5f 52 4f 4f 54 20 20 20 20 20 20 20 31 0a 0a 2f  _ROOT       1../
549d0 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  *.** The name of
549e0 20 74 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c   the schema tabl
549f0 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 43  e..*/.#define SC
54a00 48 45 4d 41 5f 54 41 42 4c 45 28 78 29 20 20 28  HEMA_TABLE(x)  (
54a10 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 29 26 26  (!OMIT_TEMPDB)&&
54a20 28 78 3d 3d 31 29 3f 54 45 4d 50 5f 4d 41 53 54  (x==1)?TEMP_MAST
54a30 45 52 5f 4e 41 4d 45 3a 4d 41 53 54 45 52 5f 4e  ER_NAME:MASTER_N
54a40 41 4d 45 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 6f  AME)../*.** A co
54a50 6e 76 65 6e 69 65 6e 63 65 20 6d 61 63 72 6f 20  nvenience macro 
54a60 74 68 61 74 20 72 65 74 75 72 6e 73 20 74 68 65  that returns the
54a70 20 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   number of eleme
54a80 6e 74 73 20 69 6e 0a 2a 2a 20 61 6e 20 61 72 72  nts in.** an arr
54a90 61 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 41  ay..*/.#define A
54aa0 72 72 61 79 53 69 7a 65 28 58 29 20 20 20 20 28  rraySize(X)    (
54ab0 28 69 6e 74 29 28 73 69 7a 65 6f 66 28 58 29 2f  (int)(sizeof(X)/
54ac0 73 69 7a 65 6f 66 28 58 5b 30 5d 29 29 29 0a 0a  sizeof(X[0])))..
54ad0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
54ae0 69 6e 67 20 76 61 6c 75 65 20 61 73 20 61 20 64  ing value as a d
54af0 65 73 74 72 75 63 74 6f 72 20 6d 65 61 6e 73 20  estructor means 
54b00 74 6f 20 75 73 65 20 73 71 6c 69 74 65 33 44 62  to use sqlite3Db
54b10 46 72 65 65 28 29 2e 0a 2a 2a 20 54 68 69 73 20  Free()..** This 
54b20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 65  is an internal e
54b30 78 74 65 6e 73 69 6f 6e 20 74 6f 20 53 51 4c 49  xtension to SQLI
54b40 54 45 5f 53 54 41 54 49 43 20 61 6e 64 20 53 51  TE_STATIC and SQ
54b50 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 2e 0a  LITE_TRANSIENT..
54b60 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
54b70 45 5f 44 59 4e 41 4d 49 43 20 20 20 28 28 73 71  E_DYNAMIC   ((sq
54b80 6c 69 74 65 33 5f 64 65 73 74 72 75 63 74 6f 72  lite3_destructor
54b90 5f 74 79 70 65 29 73 71 6c 69 74 65 33 44 62 46  _type)sqlite3DbF
54ba0 72 65 65 29 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ree)../*.** When
54bb0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
54bc0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 69 74 20   is defined, it 
54bd0 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74  means that the t
54be0 61 72 67 65 74 20 70 6c 61 74 66 6f 72 6d 20 64  arget platform d
54bf0 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70 70 6f  oes.** not suppo
54c00 72 74 20 57 72 69 74 61 62 6c 65 20 53 74 61 74  rt Writable Stat
54c10 69 63 20 44 61 74 61 20 28 57 53 44 29 20 73 75  ic Data (WSD) su
54c20 63 68 20 61 73 20 67 6c 6f 62 61 6c 20 61 6e 64  ch as global and
54c30 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65   static variable
54c40 73 2e 0a 2a 2a 20 41 6c 6c 20 76 61 72 69 61 62  s..** All variab
54c50 6c 65 73 20 6d 75 73 74 20 65 69 74 68 65 72 20  les must either 
54c60 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 20  be on the stack 
54c70 6f 72 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61  or dynamically a
54c80 6c 6c 6f 63 61 74 65 64 20 66 72 6f 6d 0a 2a 2a  llocated from.**
54c90 20 74 68 65 20 68 65 61 70 2e 20 20 57 68 65 6e   the heap.  When
54ca0 20 57 53 44 20 69 73 20 75 6e 73 75 70 70 6f 72   WSD is unsuppor
54cb0 74 65 64 2c 20 74 68 65 20 76 61 72 69 61 62 6c  ted, the variabl
54cc0 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 73  e declarations s
54cd0 63 61 74 74 65 72 65 64 0a 2a 2a 20 74 68 72 6f  cattered.** thro
54ce0 75 67 68 6f 75 74 20 74 68 65 20 53 51 4c 69 74  ughout the SQLit
54cf0 65 20 63 6f 64 65 20 6d 75 73 74 20 62 65 63 6f  e code must beco
54d00 6d 65 20 63 6f 6e 73 74 61 6e 74 73 20 69 6e 73  me constants ins
54d10 74 65 61 64 2e 20 20 54 68 65 20 53 51 4c 49 54  tead.  The SQLIT
54d20 45 5f 57 53 44 0a 2a 2a 20 6d 61 63 72 6f 20 69  E_WSD.** macro i
54d30 73 20 75 73 65 64 20 66 6f 72 20 74 68 69 73 20  s used for this 
54d40 70 75 72 70 6f 73 65 2e 20 20 41 6e 64 20 69 6e  purpose.  And in
54d50 73 74 65 61 64 20 6f 66 20 72 65 66 65 72 65 6e  stead of referen
54d60 63 69 6e 67 20 74 68 65 20 76 61 72 69 61 62 6c  cing the variabl
54d70 65 0a 2a 2a 20 64 69 72 65 63 74 6c 79 2c 20 77  e.** directly, w
54d80 65 20 75 73 65 20 69 74 73 20 63 6f 6e 73 74 61  e use its consta
54d90 6e 74 20 61 73 20 61 20 6b 65 79 20 74 6f 20 6c  nt as a key to l
54da0 6f 6f 6b 75 70 20 74 68 65 20 72 75 6e 2d 74 69  ookup the run-ti
54db0 6d 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20  me allocated.** 
54dc0 62 75 66 66 65 72 20 74 68 61 74 20 68 6f 6c 64  buffer that hold
54dd0 73 20 72 65 61 6c 20 76 61 72 69 61 62 6c 65 2e  s real variable.
54de0 20 20 54 68 65 20 63 6f 6e 73 74 61 6e 74 20 69    The constant i
54df0 73 20 61 6c 73 6f 20 74 68 65 20 69 6e 69 74 69  s also the initi
54e00 61 6c 69 7a 65 72 0a 2a 2a 20 66 6f 72 20 74 68  alizer.** for th
54e10 65 20 72 75 6e 2d 74 69 6d 65 20 61 6c 6c 6f 63  e run-time alloc
54e20 61 74 65 64 20 62 75 66 66 65 72 2e 0a 2a 2a 0a  ated buffer..**.
54e30 2a 2a 20 49 6e 20 74 68 65 20 75 73 75 61 6c 20  ** In the usual 
54e40 63 61 73 65 20 77 68 65 72 65 20 57 53 44 20 69  case where WSD i
54e50 73 20 73 75 70 70 6f 72 74 65 64 2c 20 74 68 65  s supported, the
54e60 20 53 51 4c 49 54 45 5f 57 53 44 20 61 6e 64 20   SQLITE_WSD and 
54e70 47 4c 4f 42 41 4c 0a 2a 2a 20 6d 61 63 72 6f 73  GLOBAL.** macros
54e80 20 62 65 63 6f 6d 65 20 6e 6f 2d 6f 70 73 20 61   become no-ops a
54e90 6e 64 20 68 61 76 65 20 7a 65 72 6f 20 70 65 72  nd have zero per
54ea0 66 6f 72 6d 61 6e 63 65 20 69 6d 70 61 63 74 2e  formance impact.
54eb0 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
54ec0 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 23 64 65  E_OMIT_WSD.  #de
54ed0 66 69 6e 65 20 53 51 4c 49 54 45 5f 57 53 44 20  fine SQLITE_WSD 
54ee0 63 6f 6e 73 74 0a 20 20 23 64 65 66 69 6e 65 20  const.  #define 
54ef0 47 4c 4f 42 41 4c 28 74 2c 76 29 20 28 2a 28 74  GLOBAL(t,v) (*(t
54f00 2a 29 73 71 6c 69 74 65 33 5f 77 73 64 5f 66 69  *)sqlite3_wsd_fi
54f10 6e 64 28 28 76 6f 69 64 2a 29 26 28 76 29 2c 20  nd((void*)&(v), 
54f20 73 69 7a 65 6f 66 28 76 29 29 29 0a 20 20 23 64  sizeof(v))).  #d
54f30 65 66 69 6e 65 20 73 71 6c 69 74 65 33 47 6c 6f  efine sqlite3Glo
54f40 62 61 6c 43 6f 6e 66 69 67 20 47 4c 4f 42 41 4c  balConfig GLOBAL
54f50 28 73 74 72 75 63 74 20 53 71 6c 69 74 65 33 43  (struct Sqlite3C
54f60 6f 6e 66 69 67 2c 20 73 71 6c 69 74 65 33 43 6f  onfig, sqlite3Co
54f70 6e 66 69 67 29 0a 53 51 4c 49 54 45 5f 41 50 49  nfig).SQLITE_API
54f80 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 77     int sqlite3_w
54f90 73 64 5f 69 6e 69 74 28 69 6e 74 20 4e 2c 20 69  sd_init(int N, i
54fa0 6e 74 20 4a 29 3b 0a 53 51 4c 49 54 45 5f 41 50  nt J);.SQLITE_AP
54fb0 49 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  I   void *sqlite
54fc0 33 5f 77 73 64 5f 66 69 6e 64 28 76 6f 69 64 20  3_wsd_find(void 
54fd0 2a 4b 2c 20 69 6e 74 20 4c 29 3b 0a 23 65 6c 73  *K, int L);.#els
54fe0 65 0a 20 20 23 64 65 66 69 6e 65 20 53 51 4c 49  e.  #define SQLI
54ff0 54 45 5f 57 53 44 20 0a 20 20 23 64 65 66 69 6e  TE_WSD .  #defin
55000 65 20 47 4c 4f 42 41 4c 28 74 2c 76 29 20 76 0a  e GLOBAL(t,v) v.
55010 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
55020 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 20 73 71  3GlobalConfig sq
55030 6c 69 74 65 33 43 6f 6e 66 69 67 0a 23 65 6e 64  lite3Config.#end
55040 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  if../*.** The fo
55050 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 61  llowing macros a
55060 72 65 20 75 73 65 64 20 74 6f 20 73 75 70 70 72  re used to suppr
55070 65 73 73 20 63 6f 6d 70 69 6c 65 72 20 77 61 72  ess compiler war
55080 6e 69 6e 67 73 20 61 6e 64 20 74 6f 0a 2a 2a 20  nings and to.** 
55090 6d 61 6b 65 20 69 74 20 63 6c 65 61 72 20 74 6f  make it clear to
550a0 20 68 75 6d 61 6e 20 72 65 61 64 65 72 73 20 77   human readers w
550b0 68 65 6e 20 61 20 66 75 6e 63 74 69 6f 6e 20 70  hen a function p
550c0 61 72 61 6d 65 74 65 72 20 69 73 20 64 65 6c 69  arameter is deli
550d0 62 65 72 61 74 65 6c 79 20 0a 2a 2a 20 6c 65 66  berately .** lef
550e0 74 20 75 6e 75 73 65 64 20 77 69 74 68 69 6e 20  t unused within 
550f0 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 66 75  the body of a fu
55100 6e 63 74 69 6f 6e 2e 20 54 68 69 73 20 75 73 75  nction. This usu
55110 61 6c 6c 79 20 68 61 70 70 65 6e 73 20 77 68 65  ally happens whe
55120 6e 0a 2a 2a 20 61 20 66 75 6e 63 74 69 6f 6e 20  n.** a function 
55130 69 73 20 63 61 6c 6c 65 64 20 76 69 61 20 61 20  is called via a 
55140 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
55150 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 74 68  . For example th
55160 65 20 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61  e .** implementa
55170 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 61  tion of an SQL a
55180 67 67 72 65 67 61 74 65 20 73 74 65 70 20 63 61  ggregate step ca
55190 6c 6c 62 61 63 6b 20 6d 61 79 20 6e 6f 74 20 75  llback may not u
551a0 73 65 20 74 68 65 0a 2a 2a 20 70 61 72 61 6d 65  se the.** parame
551b0 74 65 72 20 69 6e 64 69 63 61 74 69 6e 67 20 74  ter indicating t
551c0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  he number of arg
551d0 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20 74 6f  uments passed to
551e0 20 74 68 65 20 61 67 67 72 65 67 61 74 65 2c 0a   the aggregate,.
551f0 2a 2a 20 69 66 20 69 74 20 6b 6e 6f 77 73 20 74  ** if it knows t
55200 68 61 74 20 74 68 69 73 20 69 73 20 65 6e 66 6f  hat this is enfo
55210 72 63 65 64 20 65 6c 73 65 77 68 65 72 65 2e 0a  rced elsewhere..
55220 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 66 75 6e  **.** When a fun
55230 63 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 20  ction parameter 
55240 69 73 20 6e 6f 74 20 75 73 65 64 20 61 74 20 61  is not used at a
55250 6c 6c 20 77 69 74 68 69 6e 20 74 68 65 20 62 6f  ll within the bo
55260 64 79 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e  dy of a function
55270 2c 0a 2a 2a 20 69 74 20 69 73 20 67 65 6e 65 72  ,.** it is gener
55280 61 6c 6c 79 20 6e 61 6d 65 64 20 22 4e 6f 74 55  ally named "NotU
55290 73 65 64 22 20 6f 72 20 22 4e 6f 74 55 73 65 64  sed" or "NotUsed
552a0 32 22 20 74 6f 20 6d 61 6b 65 20 74 68 69 6e 67  2" to make thing
552b0 73 20 65 76 65 6e 20 63 6c 65 61 72 65 72 2e 0a  s even clearer..
552c0 2a 2a 20 48 6f 77 65 76 65 72 2c 20 74 68 65 73  ** However, thes
552d0 65 20 6d 61 63 72 6f 73 20 6d 61 79 20 61 6c 73  e macros may als
552e0 6f 20 62 65 20 75 73 65 64 20 74 6f 20 73 75 70  o be used to sup
552f0 70 72 65 73 73 20 77 61 72 6e 69 6e 67 73 20 72  press warnings r
55300 65 6c 61 74 65 64 20 74 6f 0a 2a 2a 20 70 61 72  elated to.** par
55310 61 6d 65 74 65 72 73 20 74 68 61 74 20 6d 61 79  ameters that may
55320 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 75   or may not be u
55330 73 65 64 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  sed depending on
55340 20 63 6f 6d 70 69 6c 61 74 69 6f 6e 20 6f 70 74   compilation opt
55350 69 6f 6e 73 2e 0a 2a 2a 20 46 6f 72 20 65 78 61  ions..** For exa
55360 6d 70 6c 65 20 74 68 6f 73 65 20 70 61 72 61 6d  mple those param
55370 65 74 65 72 73 20 6f 6e 6c 79 20 75 73 65 64 20  eters only used 
55380 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61 74  in assert() stat
55390 65 6d 65 6e 74 73 2e 20 49 6e 20 74 68 65 73 65  ements. In these
553a0 0a 2a 2a 20 63 61 73 65 73 20 74 68 65 20 70 61  .** cases the pa
553b0 72 61 6d 65 74 65 72 73 20 61 72 65 20 6e 61 6d  rameters are nam
553c0 65 64 20 61 73 20 70 65 72 20 74 68 65 20 75 73  ed as per the us
553d0 75 61 6c 20 63 6f 6e 76 65 6e 74 69 6f 6e 73 2e  ual conventions.
553e0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 55 53  .*/.#define UNUS
553f0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 78 29 20  ED_PARAMETER(x) 
55400 28 76 6f 69 64 29 28 78 29 0a 23 64 65 66 69 6e  (void)(x).#defin
55410 65 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54  e UNUSED_PARAMET
55420 45 52 32 28 78 2c 79 29 20 55 4e 55 53 45 44 5f  ER2(x,y) UNUSED_
55430 50 41 52 41 4d 45 54 45 52 28 78 29 2c 55 4e 55  PARAMETER(x),UNU
55440 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 79 29  SED_PARAMETER(y)
55450 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 77 61 72 64 20  ../*.** Forward 
55460 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 73 74  references to st
55470 72 75 63 74 75 72 65 73 0a 2a 2f 0a 74 79 70 65  ructures.*/.type
55480 64 65 66 20 73 74 72 75 63 74 20 41 67 67 49 6e  def struct AggIn
55490 66 6f 20 41 67 67 49 6e 66 6f 3b 0a 74 79 70 65  fo AggInfo;.type
554a0 64 65 66 20 73 74 72 75 63 74 20 41 75 74 68 43  def struct AuthC
554b0 6f 6e 74 65 78 74 20 41 75 74 68 43 6f 6e 74 65  ontext AuthConte
554c0 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  xt;.typedef stru
554d0 63 74 20 42 69 74 76 65 63 20 42 69 74 76 65 63  ct Bitvec Bitvec
554e0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
554f0 20 43 6f 6c 6c 53 65 71 20 43 6f 6c 6c 53 65 71   CollSeq CollSeq
55500 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
55510 20 43 6f 6c 75 6d 6e 20 43 6f 6c 75 6d 6e 3b 0a   Column Column;.
55520 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 44  typedef struct D
55530 62 20 44 62 3b 0a 74 79 70 65 64 65 66 20 73 74  b Db;.typedef st
55540 72 75 63 74 20 53 63 68 65 6d 61 20 53 63 68 65  ruct Schema Sche
55550 6d 61 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  ma;.typedef stru
55560 63 74 20 45 78 70 72 20 45 78 70 72 3b 0a 74 79  ct Expr Expr;.ty
55570 70 65 64 65 66 20 73 74 72 75 63 74 20 45 78 70  pedef struct Exp
55580 72 4c 69 73 74 20 45 78 70 72 4c 69 73 74 3b 0a  rList ExprList;.
55590 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
555a0 4b 65 79 20 46 4b 65 79 3b 0a 74 79 70 65 64 65  Key FKey;.typede
555b0 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66  f struct FuncDef
555c0 20 46 75 6e 63 44 65 66 3b 0a 74 79 70 65 64 65   FuncDef;.typede
555d0 66 20 73 74 72 75 63 74 20 46 75 6e 63 44 65 66  f struct FuncDef
555e0 48 61 73 68 20 46 75 6e 63 44 65 66 48 61 73 68  Hash FuncDefHash
555f0 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
55600 20 49 64 4c 69 73 74 20 49 64 4c 69 73 74 3b 0a   IdList IdList;.
55610 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49  typedef struct I
55620 6e 64 65 78 20 49 6e 64 65 78 3b 0a 74 79 70 65  ndex Index;.type
55630 64 65 66 20 73 74 72 75 63 74 20 4b 65 79 43 6c  def struct KeyCl
55640 61 73 73 20 4b 65 79 43 6c 61 73 73 3b 0a 74 79  ass KeyClass;.ty
55650 70 65 64 65 66 20 73 74 72 75 63 74 20 4b 65 79  pedef struct Key
55660 49 6e 66 6f 20 4b 65 79 49 6e 66 6f 3b 0a 74 79  Info KeyInfo;.ty
55670 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 6f 6f  pedef struct Loo
55680 6b 61 73 69 64 65 20 4c 6f 6f 6b 61 73 69 64 65  kaside Lookaside
55690 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
556a0 20 4c 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 20 4c   LookasideSlot L
556b0 6f 6f 6b 61 73 69 64 65 53 6c 6f 74 3b 0a 74 79  ookasideSlot;.ty
556c0 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 6f 64  pedef struct Mod
556d0 75 6c 65 20 4d 6f 64 75 6c 65 3b 0a 74 79 70 65  ule Module;.type
556e0 64 65 66 20 73 74 72 75 63 74 20 4e 61 6d 65 43  def struct NameC
556f0 6f 6e 74 65 78 74 20 4e 61 6d 65 43 6f 6e 74 65  ontext NameConte
55700 78 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  xt;.typedef stru
55710 63 74 20 50 61 72 73 65 20 50 61 72 73 65 3b 0a  ct Parse Parse;.
55720 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 53  typedef struct S
55730 65 6c 65 63 74 20 53 65 6c 65 63 74 3b 0a 74 79  elect Select;.ty
55740 70 65 64 65 66 20 73 74 72 75 63 74 20 53 72 63  pedef struct Src
55750 4c 69 73 74 20 53 72 63 4c 69 73 74 3b 0a 74 79  List SrcList;.ty
55760 70 65 64 65 66 20 73 74 72 75 63 74 20 53 74 72  pedef struct Str
55770 41 63 63 75 6d 20 53 74 72 41 63 63 75 6d 3b 0a  Accum StrAccum;.
55780 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
55790 61 62 6c 65 20 54 61 62 6c 65 3b 0a 74 79 70 65  able Table;.type
557a0 64 65 66 20 73 74 72 75 63 74 20 54 61 62 6c 65  def struct Table
557b0 4c 6f 63 6b 20 54 61 62 6c 65 4c 6f 63 6b 3b 0a  Lock TableLock;.
557c0 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 54  typedef struct T
557d0 6f 6b 65 6e 20 54 6f 6b 65 6e 3b 0a 74 79 70 65  oken Token;.type
557e0 64 65 66 20 73 74 72 75 63 74 20 54 72 69 67 67  def struct Trigg
557f0 65 72 53 74 61 63 6b 20 54 72 69 67 67 65 72 53  erStack TriggerS
55800 74 61 63 6b 3b 0a 74 79 70 65 64 65 66 20 73 74  tack;.typedef st
55810 72 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70  ruct TriggerStep
55820 20 54 72 69 67 67 65 72 53 74 65 70 3b 0a 74 79   TriggerStep;.ty
55830 70 65 64 65 66 20 73 74 72 75 63 74 20 54 72 69  pedef struct Tri
55840 67 67 65 72 20 54 72 69 67 67 65 72 3b 0a 74 79  gger Trigger;.ty
55850 70 65 64 65 66 20 73 74 72 75 63 74 20 55 6e 70  pedef struct Unp
55860 61 63 6b 65 64 52 65 63 6f 72 64 20 55 6e 70 61  ackedRecord Unpa
55870 63 6b 65 64 52 65 63 6f 72 64 3b 0a 74 79 70 65  ckedRecord;.type
55880 64 65 66 20 73 74 72 75 63 74 20 57 61 6c 6b 65  def struct Walke
55890 72 20 57 61 6c 6b 65 72 3b 0a 74 79 70 65 64 65  r Walker;.typede
558a0 66 20 73 74 72 75 63 74 20 57 68 65 72 65 49 6e  f struct WhereIn
558b0 66 6f 20 57 68 65 72 65 49 6e 66 6f 3b 0a 74 79  fo WhereInfo;.ty
558c0 70 65 64 65 66 20 73 74 72 75 63 74 20 57 68 65  pedef struct Whe
558d0 72 65 4c 65 76 65 6c 20 57 68 65 72 65 4c 65 76  reLevel WhereLev
558e0 65 6c 3b 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 65 72  el;../*.** Defer
558f0 20 73 6f 75 72 63 69 6e 67 20 76 64 62 65 2e 68   sourcing vdbe.h
55900 20 61 6e 64 20 62 74 72 65 65 2e 68 20 75 6e 74   and btree.h unt
55910 69 6c 20 61 66 74 65 72 20 74 68 65 20 22 75 38  il after the "u8
55920 22 20 61 6e 64 20 0a 2a 2a 20 22 42 75 73 79 48  " and .** "BusyH
55930 61 6e 64 6c 65 72 22 20 74 79 70 65 64 65 66 73  andler" typedefs
55940 2e 20 76 64 62 65 2e 68 20 61 6c 73 6f 20 72 65  . vdbe.h also re
55950 71 75 69 72 65 73 20 61 20 66 65 77 20 6f 66 20  quires a few of 
55960 74 68 65 20 6f 70 61 71 75 65 0a 2a 2a 20 70 6f  the opaque.** po
55970 69 6e 74 65 72 20 74 79 70 65 73 20 28 69 2e 65  inter types (i.e
55980 2e 20 46 75 6e 63 44 65 66 29 20 64 65 66 69 6e  . FuncDef) defin
55990 65 64 20 61 62 6f 76 65 2e 0a 2a 2f 0a 2f 2a 2a  ed above..*/./**
559a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63  ************ Inc
559b0 6c 75 64 65 20 62 74 72 65 65 2e 68 20 69 6e 20  lude btree.h in 
559c0 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 73 71  the middle of sq
559d0 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a  liteInt.h ******
559e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
559f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
55a00 69 6e 20 66 69 6c 65 20 62 74 72 65 65 2e 68 20  in file btree.h 
55a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
55a40 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65  ** 2001 Septembe
55a50 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  r 15.**.** The a
55a60 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
55a70 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
55a80 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
55a90 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
55aa0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
55ab0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
55ac0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
55ad0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
55ae0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
55af0 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
55b00 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
55b10 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
55b20 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
55b30 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
55b40 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
55b50 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
55b60 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
55b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55b90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55ba0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
55bb0 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  ***.** This head
55bc0 65 72 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20  er file defines 
55bd0 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74 68  the interface th
55be0 61 74 20 74 68 65 20 73 71 6c 69 74 65 20 42 2d  at the sqlite B-
55bf0 54 72 65 65 20 66 69 6c 65 0a 2a 2a 20 73 75 62  Tree file.** sub
55c00 73 79 73 74 65 6d 2e 20 20 53 65 65 20 63 6f 6d  system.  See com
55c10 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 73 6f 75  ments in the sou
55c20 72 63 65 20 63 6f 64 65 20 66 6f 72 20 61 20 64  rce code for a d
55c30 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70 74  etailed descript
55c40 69 6f 6e 0a 2a 2a 20 6f 66 20 77 68 61 74 20 65  ion.** of what e
55c50 61 63 68 20 69 6e 74 65 72 66 61 63 65 20 72 6f  ach interface ro
55c60 75 74 69 6e 65 20 64 6f 65 73 2e 0a 2a 2a 0a 2a  utine does..**.*
55c70 2a 20 40 28 23 29 20 24 49 64 3a 20 62 74 72 65  * @(#) $Id: btre
55c80 65 2e 68 2c 76 20 31 2e 31 30 35 20 32 30 30 38  e.h,v 1.105 2008
55c90 2f 31 30 2f 32 37 20 31 33 3a 35 39 3a 33 34 20  /10/27 13:59:34 
55ca0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
55cb0 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 42 54  $.*/.#ifndef _BT
55cc0 52 45 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f  REE_H_.#define _
55cd0 42 54 52 45 45 5f 48 5f 0a 0a 2f 2a 20 54 4f 44  BTREE_H_../* TOD
55ce0 4f 3a 20 54 68 69 73 20 64 65 66 69 6e 69 74 69  O: This definiti
55cf0 6f 6e 20 69 73 20 6a 75 73 74 20 69 6e 63 6c 75  on is just inclu
55d00 64 65 64 20 73 6f 20 6f 74 68 65 72 20 6d 6f 64  ded so other mod
55d10 75 6c 65 73 20 63 6f 6d 70 69 6c 65 2e 20 49 74  ules compile. It
55d20 0a 2a 2a 20 6e 65 65 64 73 20 74 6f 20 62 65 20  .** needs to be 
55d30 72 65 76 69 73 69 74 65 64 2e 0a 2a 2f 0a 23 64  revisited..*/.#d
55d40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 42  efine SQLITE_N_B
55d50 54 52 45 45 5f 4d 45 54 41 20 31 30 0a 0a 2f 2a  TREE_META 10../*
55d60 0a 2a 2a 20 49 66 20 64 65 66 69 6e 65 64 20 61  .** If defined a
55d70 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 61 75 74 6f  s non-zero, auto
55d80 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62 6c  -vacuum is enabl
55d90 65 64 20 62 79 20 64 65 66 61 75 6c 74 2e 20 4f  ed by default. O
55da0 74 68 65 72 77 69 73 65 0a 2a 2a 20 69 74 20 6d  therwise.** it m
55db0 75 73 74 20 62 65 20 74 75 72 6e 65 64 20 6f 6e  ust be turned on
55dc0 20 66 6f 72 20 65 61 63 68 20 64 61 74 61 62 61   for each databa
55dd0 73 65 20 75 73 69 6e 67 20 22 50 52 41 47 4d 41  se using "PRAGMA
55de0 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 31   auto_vacuum = 1
55df0 22 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  "..*/.#ifndef SQ
55e00 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
55e10 4f 56 41 43 55 55 4d 0a 20 20 23 64 65 66 69 6e  OVACUUM.  #defin
55e20 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  e SQLITE_DEFAULT
55e30 5f 41 55 54 4f 56 41 43 55 55 4d 20 30 0a 23 65  _AUTOVACUUM 0.#e
55e40 6e 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 42 54  ndif..#define BT
55e50 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 4e  REE_AUTOVACUUM_N
55e60 4f 4e 45 20 30 20 20 20 20 20 20 20 20 2f 2a 20  ONE 0        /* 
55e70 44 6f 20 6e 6f 74 20 64 6f 20 61 75 74 6f 2d 76  Do not do auto-v
55e80 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e 65  acuum */.#define
55e90 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55   BTREE_AUTOVACUU
55ea0 4d 5f 46 55 4c 4c 20 31 20 20 20 20 20 20 20 20  M_FULL 1        
55eb0 2f 2a 20 44 6f 20 66 75 6c 6c 20 61 75 74 6f 2d  /* Do full auto-
55ec0 76 61 63 75 75 6d 20 2a 2f 0a 23 64 65 66 69 6e  vacuum */.#defin
55ed0 65 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  e BTREE_AUTOVACU
55ee0 55 4d 5f 49 4e 43 52 20 32 20 20 20 20 20 20 20  UM_INCR 2       
55ef0 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 61 6c 20   /* Incremental 
55f00 76 61 63 75 75 6d 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  vacuum */../*.**
55f10 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
55f20 74 69 6f 6e 73 20 6f 66 20 73 74 72 75 63 74 75  tions of structu
55f30 72 65 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  re.*/.typedef st
55f40 72 75 63 74 20 42 74 72 65 65 20 42 74 72 65 65  ruct Btree Btree
55f50 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74  ;.typedef struct
55f60 20 42 74 43 75 72 73 6f 72 20 42 74 43 75 72 73   BtCursor BtCurs
55f70 6f 72 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  or;.typedef stru
55f80 63 74 20 42 74 53 68 61 72 65 64 20 42 74 53 68  ct BtShared BtSh
55f90 61 72 65 64 3b 0a 74 79 70 65 64 65 66 20 73 74  ared;.typedef st
55fa0 72 75 63 74 20 42 74 72 65 65 4d 75 74 65 78 41  ruct BtreeMutexA
55fb0 72 72 61 79 20 42 74 72 65 65 4d 75 74 65 78 41  rray BtreeMutexA
55fc0 72 72 61 79 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rray;../*.** Thi
55fd0 73 20 73 74 72 75 63 74 75 72 65 20 72 65 63 6f  s structure reco
55fe0 72 64 73 20 61 6c 6c 20 6f 66 20 74 68 65 20 42  rds all of the B
55ff0 74 72 65 65 73 20 74 68 61 74 20 6e 65 65 64 20  trees that need 
56000 74 6f 20 68 6f 6c 64 0a 2a 2a 20 61 20 6d 75 74  to hold.** a mut
56010 65 78 20 62 65 66 6f 72 65 20 77 65 20 65 6e 74  ex before we ent
56020 65 72 20 73 71 6c 69 74 65 33 56 64 62 65 45 78  er sqlite3VdbeEx
56030 65 63 28 29 2e 20 20 54 68 65 20 42 74 72 65 65  ec().  The Btree
56040 73 20 61 72 65 0a 2a 2a 20 61 72 65 20 70 6c 61  s are.** are pla
56050 63 65 64 20 69 6e 20 61 42 74 72 65 65 5b 5d 20  ced in aBtree[] 
56060 69 6e 20 6f 72 64 65 72 20 6f 66 20 61 42 74 72  in order of aBtr
56070 65 65 5b 5d 2d 3e 70 42 74 2e 20 20 54 68 61 74  ee[]->pBt.  That
56080 20 77 61 79 2c 0a 2a 2a 20 77 65 20 63 61 6e 20   way,.** we can 
56090 61 6c 77 61 79 73 20 6c 6f 63 6b 20 61 6e 64 20  always lock and 
560a0 75 6e 6c 6f 63 6b 20 74 68 65 6d 20 61 6c 6c 20  unlock them all 
560b0 71 75 69 63 6b 6c 79 2e 0a 2a 2f 0a 73 74 72 75  quickly..*/.stru
560c0 63 74 20 42 74 72 65 65 4d 75 74 65 78 41 72 72  ct BtreeMutexArr
560d0 61 79 20 7b 0a 20 20 69 6e 74 20 6e 4d 75 74 65  ay {.  int nMute
560e0 78 3b 0a 20 20 42 74 72 65 65 20 2a 61 42 74 72  x;.  Btree *aBtr
560f0 65 65 5b 53 51 4c 49 54 45 5f 4d 41 58 5f 41 54  ee[SQLITE_MAX_AT
56100 54 41 43 48 45 44 2b 31 5d 3b 0a 7d 3b 0a 0a 0a  TACHED+1];.};...
56110 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
56120 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4f  nt sqlite3BtreeO
56130 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63 68 61  pen(.  const cha
56140 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 20  r *zFilename,   
56150 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61 74 61 62  /* Name of datab
56160 61 73 65 20 66 69 6c 65 20 74 6f 20 6f 70 65 6e  ase file to open
56170 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64   */.  sqlite3 *d
56180 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  b,             /
56190 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61 74  * Associated dat
561a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
561b0 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a 2c 20   */.  Btree **, 
561c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
561d0 2a 20 52 65 74 75 72 6e 20 6f 70 65 6e 20 42 74  * Return open Bt
561e0 72 65 65 2a 20 68 65 72 65 20 2a 2f 0a 20 20 69  ree* here */.  i
561f0 6e 74 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20  nt flags,       
56200 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73          /* Flags
56210 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
56220 67 73 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  gs             /
56230 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20 74  * Flags passed t
56240 68 72 6f 75 67 68 20 74 6f 20 56 46 53 20 6f 70  hrough to VFS op
56250 65 6e 20 2a 2f 0a 29 3b 0a 0a 2f 2a 20 54 68 65  en */.);../* The
56260 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72   flags parameter
56270 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
56280 4f 70 65 6e 20 63 61 6e 20 62 65 20 74 68 65 20  Open can be the 
56290 62 69 74 77 69 73 65 20 6f 72 20 6f 66 20 74 68  bitwise or of th
562a0 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76  e.** following v
562b0 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 4e 4f 54  alues..**.** NOT
562c0 45 3a 20 20 54 68 65 73 65 20 76 61 6c 75 65 73  E:  These values
562d0 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68 65 20   must match the 
562e0 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 50 41  corresponding PA
562f0 47 45 52 5f 20 76 61 6c 75 65 73 20 69 6e 0a 2a  GER_ values in.*
56300 2a 20 70 61 67 65 72 2e 68 2e 0a 2a 2f 0a 23 64  * pager.h..*/.#d
56310 65 66 69 6e 65 20 42 54 52 45 45 5f 4f 4d 49 54  efine BTREE_OMIT
56320 5f 4a 4f 55 52 4e 41 4c 20 20 31 20 20 2f 2a 20  _JOURNAL  1  /* 
56330 44 6f 20 6e 6f 74 20 75 73 65 20 6a 6f 75 72 6e  Do not use journ
56340 61 6c 2e 20 20 4e 6f 20 61 72 67 75 6d 65 6e 74  al.  No argument
56350 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45   */.#define BTRE
56360 45 5f 4e 4f 5f 52 45 41 44 4c 4f 43 4b 20 20 20  E_NO_READLOCK   
56370 32 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c  2  /* Omit readl
56380 6f 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79  ocks on readonly
56390 20 66 69 6c 65 73 20 2a 2f 0a 23 64 65 66 69 6e   files */.#defin
563a0 65 20 42 54 52 45 45 5f 4d 45 4d 4f 52 59 20 20  e BTREE_MEMORY  
563b0 20 20 20 20 20 20 34 20 20 2f 2a 20 49 6e 2d 6d        4  /* In-m
563c0 65 6d 6f 72 79 20 44 42 2e 20 20 4e 6f 20 61 72  emory DB.  No ar
563d0 67 75 6d 65 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  gument */.#defin
563e0 65 20 42 54 52 45 45 5f 52 45 41 44 4f 4e 4c 59  e BTREE_READONLY
563f0 20 20 20 20 20 20 38 20 20 2f 2a 20 4f 70 65 6e        8  /* Open
56400 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   the database in
56410 20 72 65 61 64 2d 6f 6e 6c 79 20 6d 6f 64 65 20   read-only mode 
56420 2a 2f 0a 23 64 65 66 69 6e 65 20 42 54 52 45 45  */.#define BTREE
56430 5f 52 45 41 44 57 52 49 54 45 20 20 20 20 31 36  _READWRITE    16
56440 20 20 2f 2a 20 4f 70 65 6e 20 66 6f 72 20 62 6f    /* Open for bo
56450 74 68 20 72 65 61 64 69 6e 67 20 61 6e 64 20 77  th reading and w
56460 72 69 74 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e  riting */.#defin
56470 65 20 42 54 52 45 45 5f 43 52 45 41 54 45 20 20  e BTREE_CREATE  
56480 20 20 20 20 20 33 32 20 20 2f 2a 20 43 72 65 61       32  /* Crea
56490 74 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  te the database 
564a0 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 65  if it does not e
564b0 78 69 73 74 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f  xist */..SQLITE_
564c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
564d0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28 42 74  te3BtreeClose(Bt
564e0 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
564f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
56500 33 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69  3BtreeSetCacheSi
56510 7a 65 28 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a  ze(Btree*,int);.
56520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
56530 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
56540 65 74 53 61 66 65 74 79 4c 65 76 65 6c 28 42 74  etSafetyLevel(Bt
56550 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ree*,int,int);.S
56560 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
56570 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 79  t sqlite3BtreeSy
56580 6e 63 44 69 73 61 62 6c 65 64 28 42 74 72 65 65  ncDisabled(Btree
56590 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
565a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
565b0 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 42  reeSetPageSize(B
565c0 74 72 65 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a  tree*,int,int);.
565d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
565e0 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
565f0 65 74 50 61 67 65 53 69 7a 65 28 42 74 72 65 65  etPageSize(Btree
56600 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
56610 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
56620 72 65 65 4d 61 78 50 61 67 65 43 6f 75 6e 74 28  reeMaxPageCount(
56630 42 74 72 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c  Btree*,int);.SQL
56640 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
56650 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
56660 65 73 65 72 76 65 28 42 74 72 65 65 2a 29 3b 0a  eserve(Btree*);.
56670 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
56680 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  nt sqlite3BtreeS
56690 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
566a0 65 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ee *, int);.SQLI
566b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
566c0 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41 75  qlite3BtreeGetAu
566d0 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65 20 2a  toVacuum(Btree *
566e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
566f0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
56700 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72  eeBeginTrans(Btr
56710 65 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45  ee*,int);.SQLITE
56720 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
56730 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50  ite3BtreeCommitP
56740 68 61 73 65 4f 6e 65 28 42 74 72 65 65 2a 2c 20  haseOne(Btree*, 
56750 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
56760 74 65 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ter);.SQLITE_PRI
56770 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
56780 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
56790 54 77 6f 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  Two(Btree*);.SQL
567a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
567b0 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d 6d  sqlite3BtreeComm
567c0 69 74 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49  it(Btree*);.SQLI
567d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
567e0 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
567f0 61 63 6b 28 42 74 72 65 65 2a 29 3b 0a 53 51 4c  ack(Btree*);.SQL
56800 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
56810 73 71 6c 69 74 65 33 42 74 72 65 65 42 65 67 69  sqlite3BtreeBegi
56820 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53  nStmt(Btree*);.S
56830 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
56840 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  t sqlite3BtreeCo
56850 6d 6d 69 74 53 74 6d 74 28 42 74 72 65 65 2a 29  mmitStmt(Btree*)
56860 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
56870 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
56880 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74  eRollbackStmt(Bt
56890 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ree*);.SQLITE_PR
568a0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
568b0 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62 6c  3BtreeCreateTabl
568c0 65 28 42 74 72 65 65 2a 2c 20 69 6e 74 2a 2c 20  e(Btree*, int*, 
568d0 69 6e 74 20 66 6c 61 67 73 29 3b 0a 53 51 4c 49  int flags);.SQLI
568e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
568f0 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
56900 72 61 6e 73 28 42 74 72 65 65 2a 29 3b 0a 53 51  rans(Btree*);.SQ
56910 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
56920 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
56930 6e 53 74 6d 74 28 42 74 72 65 65 2a 29 3b 0a 53  nStmt(Btree*);.S
56940 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
56950 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  t sqlite3BtreeIs
56960 49 6e 52 65 61 64 54 72 61 6e 73 28 42 74 72 65  InReadTrans(Btre
56970 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
56980 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
56990 33 42 74 72 65 65 53 63 68 65 6d 61 28 42 74 72  3BtreeSchema(Btr
569a0 65 65 20 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 28  ee *, int, void(
569b0 2a 29 28 76 6f 69 64 20 2a 29 29 3b 0a 53 51 4c  *)(void *));.SQL
569c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
569d0 73 71 6c 69 74 65 33 42 74 72 65 65 53 63 68 65  sqlite3BtreeSche
569e0 6d 61 4c 6f 63 6b 65 64 28 42 74 72 65 65 20 2a  maLocked(Btree *
569f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
56a00 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
56a10 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42 74 72 65  eeLockTable(Btre
56a20 65 20 2a 2c 20 69 6e 74 2c 20 75 38 29 3b 0a 0a  e *, int, u8);..
56a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
56a40 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
56a50 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
56a60 6d 65 28 42 74 72 65 65 20 2a 29 3b 0a 53 51 4c  me(Btree *);.SQL
56a70 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
56a80 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 42  t char *sqlite3B
56a90 74 72 65 65 47 65 74 44 69 72 6e 61 6d 65 28 42  treeGetDirname(B
56aa0 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  tree *);.SQLITE_
56ab0 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68  PRIVATE const ch
56ac0 61 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  ar *sqlite3Btree
56ad0 47 65 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42  GetJournalname(B
56ae0 74 72 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  tree *);.SQLITE_
56af0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
56b00 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
56b10 28 42 74 72 65 65 20 2a 2c 20 42 74 72 65 65 20  (Btree *, Btree 
56b20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
56b30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
56b40 74 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42  treeIncrVacuum(B
56b50 74 72 65 65 20 2a 29 3b 0a 0a 2f 2a 20 54 68 65  tree *);../* The
56b60 20 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72   flags parameter
56b70 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
56b80 43 72 65 61 74 65 54 61 62 6c 65 20 63 61 6e 20  CreateTable can 
56b90 62 65 20 74 68 65 20 62 69 74 77 69 73 65 20 4f  be the bitwise O
56ba0 52 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c 6c  R.** of the foll
56bb0 6f 77 69 6e 67 20 66 6c 61 67 73 3a 0a 2a 2f 0a  owing flags:.*/.
56bc0 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 49 4e  #define BTREE_IN
56bd0 54 4b 45 59 20 20 20 20 20 31 20 20 20 20 2f 2a  TKEY     1    /*
56be0 20 54 61 62 6c 65 20 68 61 73 20 6f 6e 6c 79 20   Table has only 
56bf0 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69 6e  64-bit signed in
56c00 74 65 67 65 72 20 6b 65 79 73 20 2a 2f 0a 23 64  teger keys */.#d
56c10 65 66 69 6e 65 20 42 54 52 45 45 5f 5a 45 52 4f  efine BTREE_ZERO
56c20 44 41 54 41 20 20 20 32 20 20 20 20 2f 2a 20 54  DATA   2    /* T
56c30 61 62 6c 65 20 68 61 73 20 6b 65 79 73 20 6f 6e  able has keys on
56c40 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a 2f 0a  ly - no data */.
56c50 23 64 65 66 69 6e 65 20 42 54 52 45 45 5f 4c 45  #define BTREE_LE
56c60 41 46 44 41 54 41 20 20 20 34 20 20 20 20 2f 2a  AFDATA   4    /*
56c70 20 44 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20   Data stored in 
56c80 6c 65 61 76 65 73 20 6f 6e 6c 79 2e 20 20 49 6d  leaves only.  Im
56c90 70 6c 69 65 73 20 49 4e 54 4b 45 59 20 2a 2f 0a  plies INTKEY */.
56ca0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
56cb0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
56cc0 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65 65 2a  DropTable(Btree*
56cd0 2c 20 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51  , int, int*);.SQ
56ce0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
56cf0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
56d00 61 72 54 61 62 6c 65 28 42 74 72 65 65 2a 2c 20  arTable(Btree*, 
56d10 69 6e 74 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49  int, int*);.SQLI
56d20 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
56d30 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65  qlite3BtreeGetMe
56d40 74 61 28 42 74 72 65 65 2a 2c 20 69 6e 74 20 69  ta(Btree*, int i
56d50 64 78 2c 20 75 33 32 20 2a 70 56 61 6c 75 65 29  dx, u32 *pValue)
56d60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
56d70 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
56d80 65 55 70 64 61 74 65 4d 65 74 61 28 42 74 72 65  eUpdateMeta(Btre
56d90 65 2a 2c 20 69 6e 74 20 69 64 78 2c 20 75 33 32  e*, int idx, u32
56da0 20 76 61 6c 75 65 29 3b 0a 53 51 4c 49 54 45 5f   value);.SQLITE_
56db0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
56dc0 69 74 65 33 42 74 72 65 65 54 72 69 70 41 6c 6c  ite3BtreeTripAll
56dd0 43 75 72 73 6f 72 73 28 42 74 72 65 65 2a 2c 20  Cursors(Btree*, 
56de0 69 6e 74 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  int);..SQLITE_PR
56df0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
56e00 33 42 74 72 65 65 43 75 72 73 6f 72 28 0a 20 20  3BtreeCursor(.  
56e10 42 74 72 65 65 2a 2c 20 20 20 20 20 20 20 20 20  Btree*,         
56e20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56e30 20 20 20 20 20 2f 2a 20 42 54 72 65 65 20 63 6f       /* BTree co
56e40 6e 74 61 69 6e 69 6e 67 20 74 61 62 6c 65 20 74  ntaining table t
56e50 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20  o open */.  int 
56e60 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
56e70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56e80 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72 6f 6f   /* Index of roo
56e90 74 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  t page */.  int 
56ea0 77 72 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20  wrFlag,         
56eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
56ec0 20 2f 2a 20 31 20 66 6f 72 20 77 72 69 74 69 6e   /* 1 for writin
56ed0 67 2e 20 20 30 20 66 6f 72 20 72 65 61 64 2d 6f  g.  0 for read-o
56ee0 6e 6c 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20  nly */.  struct 
56ef0 4b 65 79 49 6e 66 6f 2a 2c 20 20 20 20 20 20 20  KeyInfo*,       
56f00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
56f10 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
56f20 74 6f 20 63 6f 6d 70 61 72 65 20 66 75 6e 63 74  to compare funct
56f30 69 6f 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  ion */.  BtCurso
56f40 72 20 2a 70 43 75 72 73 6f 72 20 20 20 20 20 20  r *pCursor      
56f50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
56f60 20 53 70 61 63 65 20 74 6f 20 77 72 69 74 65 20   Space to write 
56f70 63 75 72 73 6f 72 20 73 74 72 75 63 74 75 72 65  cursor structure
56f80 20 2a 2f 0a 29 3b 0a 53 51 4c 49 54 45 5f 50 52   */.);.SQLITE_PR
56f90 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
56fa0 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
56fb0 28 76 6f 69 64 29 3b 0a 0a 53 51 4c 49 54 45 5f  (void);..SQLITE_
56fc0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
56fd0 74 65 33 42 74 72 65 65 43 6c 6f 73 65 43 75 72  te3BtreeCloseCur
56fe0 73 6f 72 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  sor(BtCursor*);.
56ff0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
57000 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
57010 6f 76 65 74 6f 28 0a 20 20 42 74 43 75 72 73 6f  oveto(.  BtCurso
57020 72 2a 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  r*,.  const void
57030 20 2a 70 4b 65 79 2c 0a 20 20 69 36 34 20 6e 4b   *pKey,.  i64 nK
57040 65 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a  ey,.  int bias,.
57050 20 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53    int *pRes.);.S
57060 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
57070 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  t sqlite3BtreeMo
57080 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20 20  vetoUnpacked(.  
57090 42 74 43 75 72 73 6f 72 2a 2c 0a 20 20 55 6e 70  BtCursor*,.  Unp
570a0 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55 6e  ackedRecord *pUn
570b0 4b 65 79 2c 0a 20 20 69 36 34 20 69 6e 74 4b 65  Key,.  i64 intKe
570c0 79 2c 0a 20 20 69 6e 74 20 62 69 61 73 2c 0a 20  y,.  int bias,. 
570d0 20 69 6e 74 20 2a 70 52 65 73 0a 29 3b 0a 53 51   int *pRes.);.SQ
570e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
570f0 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72   sqlite3BtreeCur
57100 73 6f 72 48 61 73 4d 6f 76 65 64 28 42 74 43 75  sorHasMoved(BtCu
57110 72 73 6f 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51  rsor*, int*);.SQ
57120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
57130 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
57140 65 74 65 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a  ete(BtCursor*);.
57150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
57160 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  nt sqlite3BtreeI
57170 6e 73 65 72 74 28 42 74 43 75 72 73 6f 72 2a 2c  nsert(BtCursor*,
57180 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
57190 79 2c 20 69 36 34 20 6e 4b 65 79 2c 0a 20 20 20  y, i64 nKey,.   
571a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
571b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
571c0 6f 6e 73 74 20 76 6f 69 64 20 2a 70 44 61 74 61  onst void *pData
571d0 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20  , int nData,.   
571e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
571f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
57200 6e 74 20 6e 5a 65 72 6f 2c 20 69 6e 74 20 62 69  nt nZero, int bi
57210 61 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  as);.SQLITE_PRIV
57220 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
57230 74 72 65 65 46 69 72 73 74 28 42 74 43 75 72 73  treeFirst(BtCurs
57240 6f 72 2a 2c 20 69 6e 74 20 2a 70 52 65 73 29 3b  or*, int *pRes);
57250 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
57260 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
57270 4c 61 73 74 28 42 74 43 75 72 73 6f 72 2a 2c 20  Last(BtCursor*, 
57280 69 6e 74 20 2a 70 52 65 73 29 3b 0a 53 51 4c 49  int *pRes);.SQLI
57290 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
572a0 71 6c 69 74 65 33 42 74 72 65 65 4e 65 78 74 28  qlite3BtreeNext(
572b0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a  BtCursor*, int *
572c0 70 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52  pRes);.SQLITE_PR
572d0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
572e0 33 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73  3BtreeEof(BtCurs
572f0 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  or*);.SQLITE_PRI
57300 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57310 42 74 72 65 65 46 6c 61 67 73 28 42 74 43 75 72  BtreeFlags(BtCur
57320 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
57330 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
57340 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42  3BtreePrevious(B
57350 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
57360 52 65 73 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Res);.SQLITE_PRI
57370 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
57380 42 74 72 65 65 4b 65 79 53 69 7a 65 28 42 74 43  BtreeKeySize(BtC
57390 75 72 73 6f 72 2a 2c 20 69 36 34 20 2a 70 53 69  ursor*, i64 *pSi
573a0 7a 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ze);.SQLITE_PRIV
573b0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42  ATE int sqlite3B
573c0 74 72 65 65 4b 65 79 28 42 74 43 75 72 73 6f 72  treeKey(BtCursor
573d0 2a 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75  *, u32 offset, u
573e0 33 32 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a  32 amt, void*);.
573f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 73  SQLITE_PRIVATE s
57400 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65 33 42  qlite3 *sqlite3B
57410 74 72 65 65 43 75 72 73 6f 72 44 62 28 63 6f 6e  treeCursorDb(con
57420 73 74 20 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53  st BtCursor*);.S
57430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
57440 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
57450 33 42 74 72 65 65 4b 65 79 46 65 74 63 68 28 42  3BtreeKeyFetch(B
57460 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 20 2a 70  tCursor*, int *p
57470 41 6d 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Amt);.SQLITE_PRI
57480 56 41 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20  VATE const void 
57490 2a 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74  *sqlite3BtreeDat
574a0 61 46 65 74 63 68 28 42 74 43 75 72 73 6f 72 2a  aFetch(BtCursor*
574b0 2c 20 69 6e 74 20 2a 70 41 6d 74 29 3b 0a 53 51  , int *pAmt);.SQ
574c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
574d0 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
574e0 61 53 69 7a 65 28 42 74 43 75 72 73 6f 72 2a 2c  aSize(BtCursor*,
574f0 20 75 33 32 20 2a 70 53 69 7a 65 29 3b 0a 53 51   u32 *pSize);.SQ
57500 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
57510 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 61 74   sqlite3BtreeDat
57520 61 28 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32  a(BtCursor*, u32
57530 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d 74   offset, u32 amt
57540 2c 20 76 6f 69 64 2a 29 3b 0a 0a 53 51 4c 49 54  , void*);..SQLIT
57550 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
57560 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 74 65  sqlite3BtreeInte
57570 67 72 69 74 79 43 68 65 63 6b 28 42 74 72 65 65  grityCheck(Btree
57580 2a 2c 20 69 6e 74 20 2a 61 52 6f 6f 74 2c 20 69  *, int *aRoot, i
57590 6e 74 20 6e 52 6f 6f 74 2c 20 69 6e 74 2c 20 69  nt nRoot, int, i
575a0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
575b0 56 41 54 45 20 73 74 72 75 63 74 20 50 61 67 65  VATE struct Page
575c0 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 50  r *sqlite3BtreeP
575d0 61 67 65 72 28 42 74 72 65 65 2a 29 3b 0a 0a 53  ager(Btree*);..S
575e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
575f0 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75  t sqlite3BtreePu
57600 74 44 61 74 61 28 42 74 43 75 72 73 6f 72 2a 2c  tData(BtCursor*,
57610 20 75 33 32 20 6f 66 66 73 65 74 2c 20 75 33 32   u32 offset, u32
57620 20 61 6d 74 2c 20 76 6f 69 64 2a 29 3b 0a 53 51   amt, void*);.SQ
57630 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
57640 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 61  d sqlite3BtreeCa
57650 63 68 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75  cheOverflow(BtCu
57660 72 73 6f 72 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  rsor *);.SQLITE_
57670 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
57680 69 74 65 33 42 74 72 65 65 43 6c 65 61 72 43 75  ite3BtreeClearCu
57690 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 29  rsor(BtCursor *)
576a0 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
576b0 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49  _TEST.SQLITE_PRI
576c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
576d0 42 74 72 65 65 43 75 72 73 6f 72 49 6e 66 6f 28  BtreeCursorInfo(
576e0 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74 2a 2c  BtCursor*, int*,
576f0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
57700 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
57710 65 33 42 74 72 65 65 43 75 72 73 6f 72 4c 69 73  e3BtreeCursorLis
57720 74 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64 69  t(Btree*);.#endi
57730 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61  f../*.** If we a
57740 72 65 20 6e 6f 74 20 75 73 69 6e 67 20 73 68 61  re not using sha
57750 72 65 64 20 63 61 63 68 65 2c 20 74 68 65 6e 20  red cache, then 
57760 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65 64  there is no need
57770 20 74 6f 0a 2a 2a 20 75 73 65 20 6d 75 74 65 78   to.** use mutex
57780 65 73 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  es to access the
57790 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74   BtShared struct
577a0 75 72 65 73 2e 20 20 53 6f 20 6d 61 6b 65 20 74  ures.  So make t
577b0 68 65 0a 2a 2a 20 45 6e 74 65 72 20 61 6e 64 20  he.** Enter and 
577c0 4c 65 61 76 65 20 70 72 6f 63 65 64 75 72 65 73  Leave procedures
577d0 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23 69 66 20   no-ops..*/.#if 
577e0 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
577f0 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
57800 45 29 20 26 26 20 53 51 4c 49 54 45 5f 54 48 52  E) && SQLITE_THR
57810 45 41 44 53 41 46 45 0a 53 51 4c 49 54 45 5f 50  EADSAFE.SQLITE_P
57820 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
57830 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
57840 42 74 72 65 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  Btree*);.SQLITE_
57850 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
57860 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
57870 28 42 74 72 65 65 2a 29 3b 0a 23 69 66 6e 64 65  (Btree*);.#ifnde
57880 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 54 68  f NDEBUG.  /* Th
57890 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
578a0 65 64 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ed inside assert
578b0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 20 6f 6e  () statements on
578c0 6c 79 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ly. */.SQLITE_PR
578d0 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
578e0 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
578f0 65 78 28 42 74 72 65 65 2a 29 3b 0a 23 65 6e 64  ex(Btree*);.#end
57900 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
57910 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
57920 42 74 72 65 65 45 6e 74 65 72 43 75 72 73 6f 72  BtreeEnterCursor
57930 28 42 74 43 75 72 73 6f 72 2a 29 3b 0a 53 51 4c  (BtCursor*);.SQL
57940 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
57950 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
57960 65 61 76 65 43 75 72 73 6f 72 28 42 74 43 75 72  eaveCursor(BtCur
57970 73 6f 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  sor*);.SQLITE_PR
57980 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
57990 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c  ite3BtreeEnterAl
579a0 6c 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  l(sqlite3*);.SQL
579b0 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
579c0 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  id sqlite3BtreeL
579d0 65 61 76 65 41 6c 6c 28 73 71 6c 69 74 65 33 2a  eaveAll(sqlite3*
579e0 29 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  );.#ifndef NDEBU
579f0 47 0a 20 20 2f 2a 20 54 68 69 73 20 72 6f 75 74  G.  /* This rout
57a00 69 6e 65 20 69 73 20 75 73 65 64 20 69 6e 73 69  ine is used insi
57a10 64 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74  de assert() stat
57a20 65 6d 65 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a  ements only. */.
57a30 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20  SQLITE_PRIVATE  
57a40 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
57a50 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73  eHoldsAllMutexes
57a60 28 73 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6e 64  (sqlite3*);.#end
57a70 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
57a80 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
57a90 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 45  BtreeMutexArrayE
57aa0 6e 74 65 72 28 42 74 72 65 65 4d 75 74 65 78 41  nter(BtreeMutexA
57ab0 72 72 61 79 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  rray*);.SQLITE_P
57ac0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
57ad0 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
57ae0 72 72 61 79 4c 65 61 76 65 28 42 74 72 65 65 4d  rrayLeave(BtreeM
57af0 75 74 65 78 41 72 72 61 79 2a 29 3b 0a 53 51 4c  utexArray*);.SQL
57b00 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f  ITE_PRIVATE   vo
57b10 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
57b20 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28  utexArrayInsert(
57b30 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2a  BtreeMutexArray*
57b40 2c 20 42 74 72 65 65 2a 29 3b 0a 23 65 6c 73 65  , Btree*);.#else
57b50 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
57b60 33 42 74 72 65 65 45 6e 74 65 72 28 58 29 0a 23  3BtreeEnter(X).#
57b70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
57b80 74 72 65 65 4c 65 61 76 65 28 58 29 0a 23 69 66  treeLeave(X).#if
57b90 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a  ndef NDEBUG.  /*
57ba0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
57bb0 20 75 73 65 64 20 69 6e 73 69 64 65 20 61 73 73   used inside ass
57bc0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73  ert() statements
57bd0 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 64 65 66 69   only. */.# defi
57be0 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  ne sqlite3BtreeH
57bf0 6f 6c 64 73 4d 75 74 65 78 28 58 29 20 31 0a 23  oldsMutex(X) 1.#
57c00 65 6e 64 69 66 0a 23 20 64 65 66 69 6e 65 20 73  endif.# define s
57c10 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
57c20 43 75 72 73 6f 72 28 58 29 0a 23 20 64 65 66 69  Cursor(X).# defi
57c30 6e 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  ne sqlite3BtreeL
57c40 65 61 76 65 43 75 72 73 6f 72 28 58 29 0a 23 20  eaveCursor(X).# 
57c50 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
57c60 72 65 65 45 6e 74 65 72 41 6c 6c 28 58 29 0a 23  reeEnterAll(X).#
57c70 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42   define sqlite3B
57c80 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 58 29 0a  treeLeaveAll(X).
57c90 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
57ca0 20 2f 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65   /* This routine
57cb0 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20   is used inside 
57cc0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
57cd0 6e 74 73 20 6f 6e 6c 79 2e 20 2a 2f 0a 23 20 64  nts only. */.# d
57ce0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74 72  efine sqlite3Btr
57cf0 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65  eeHoldsAllMutexe
57d00 73 28 58 29 20 31 0a 23 65 6e 64 69 66 0a 23 20  s(X) 1.#endif.# 
57d10 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
57d20 72 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74  reeMutexArrayEnt
57d30 65 72 28 58 29 0a 23 20 64 65 66 69 6e 65 20 73  er(X).# define s
57d40 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78  qlite3BtreeMutex
57d50 41 72 72 61 79 4c 65 61 76 65 28 58 29 0a 23 20  ArrayLeave(X).# 
57d60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 42 74  define sqlite3Bt
57d70 72 65 65 4d 75 74 65 78 41 72 72 61 79 49 6e 73  reeMutexArrayIns
57d80 65 72 74 28 58 2c 59 29 0a 23 65 6e 64 69 66 0a  ert(X,Y).#endif.
57d90 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 42 54 52  ..#endif /* _BTR
57da0 45 45 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  EE_H_ */../*****
57db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
57dc0 20 62 74 72 65 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a   btree.h *******
57dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57de0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57df0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
57e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e  ********* Contin
57e10 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65  uing where we le
57e20 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65  ft off in sqlite
57e30 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
57e40 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
57e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64  ********* Includ
57e60 65 20 76 64 62 65 2e 68 20 69 6e 20 74 68 65 20  e vdbe.h in the 
57e70 6d 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65  middle of sqlite
57e80 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  Int.h **********
57e90 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
57ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
57eb0 66 69 6c 65 20 76 64 62 65 2e 68 20 2a 2a 2a 2a  file vdbe.h ****
57ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
57ee0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
57ef0 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
57f00 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
57f10 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
57f20 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
57f30 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
57f40 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
57f50 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
57f60 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
57f70 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
57f80 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
57f90 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
57fa0 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
57fb0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
57fc0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
57fd0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
57fe0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
57ff0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
58000 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
58010 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
58020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
58060 0a 2a 2a 20 48 65 61 64 65 72 20 66 69 6c 65 20  .** Header file 
58070 66 6f 72 20 74 68 65 20 56 69 72 74 75 61 6c 20  for the Virtual 
58080 44 61 74 61 42 61 73 65 20 45 6e 67 69 6e 65 20  DataBase Engine 
58090 28 56 44 42 45 29 0a 2a 2a 0a 2a 2a 20 54 68 69  (VDBE).**.** Thi
580a0 73 20 68 65 61 64 65 72 20 64 65 66 69 6e 65 73  s header defines
580b0 20 74 68 65 20 69 6e 74 65 72 66 61 63 65 20 74   the interface t
580c0 6f 20 74 68 65 20 76 69 72 74 75 61 6c 20 64 61  o the virtual da
580d0 74 61 62 61 73 65 20 65 6e 67 69 6e 65 0a 2a 2a  tabase engine.**
580e0 20 6f 72 20 56 44 42 45 2e 20 20 54 68 65 20 56   or VDBE.  The V
580f0 44 42 45 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61  DBE implements a
58100 6e 20 61 62 73 74 72 61 63 74 20 6d 61 63 68 69  n abstract machi
58110 6e 65 20 74 68 61 74 20 72 75 6e 73 20 61 0a 2a  ne that runs a.*
58120 2a 20 73 69 6d 70 6c 65 20 70 72 6f 67 72 61 6d  * simple program
58130 20 74 6f 20 61 63 63 65 73 73 20 61 6e 64 20 6d   to access and m
58140 6f 64 69 66 79 20 74 68 65 20 75 6e 64 65 72 6c  odify the underl
58150 79 69 6e 67 20 64 61 74 61 62 61 73 65 2e 0a 2a  ying database..*
58160 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 2e 68  *.** $Id: vdbe.h
58170 2c 76 20 31 2e 31 33 39 20 32 30 30 38 2f 31 30  ,v 1.139 2008/10
58180 2f 33 31 20 31 30 3a 35 33 3a 32 33 20 64 61 6e  /31 10:53:23 dan
58190 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
581a0 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
581b0 45 5f 56 44 42 45 5f 48 5f 0a 23 64 65 66 69 6e  E_VDBE_H_.#defin
581c0 65 20 5f 53 51 4c 49 54 45 5f 56 44 42 45 5f 48  e _SQLITE_VDBE_H
581d0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c  _../*.** A singl
581e0 65 20 56 44 42 45 20 69 73 20 61 6e 20 6f 70 61  e VDBE is an opa
581f0 71 75 65 20 73 74 72 75 63 74 75 72 65 20 6e 61  que structure na
58200 6d 65 64 20 22 56 64 62 65 22 2e 20 20 4f 6e 6c  med "Vdbe".  Onl
58210 79 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20 69 6e  y routines.** in
58220 20 74 68 65 20 73 6f 75 72 63 65 20 66 69 6c 65   the source file
58230 20 73 71 6c 69 74 65 56 64 62 65 2e 63 20 61 72   sqliteVdbe.c ar
58240 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 73 65 65  e allowed to see
58250 20 74 68 65 20 69 6e 73 69 64 65 73 0a 2a 2a 20   the insides.** 
58260 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
58270 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
58280 72 75 63 74 20 56 64 62 65 20 56 64 62 65 3b 0a  ruct Vdbe Vdbe;.
58290 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 73  ./*.** The names
582a0 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
582b0 67 20 74 79 70 65 73 20 64 65 63 6c 61 72 65 64  g types declared
582c0 20 69 6e 20 76 64 62 65 49 6e 74 2e 68 20 61 72   in vdbeInt.h ar
582d0 65 20 72 65 71 75 69 72 65 64 0a 2a 2a 20 66 6f  e required.** fo
582e0 72 20 74 68 65 20 56 64 62 65 4f 70 20 64 65 66  r the VdbeOp def
582f0 69 6e 69 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65  inition..*/.type
58300 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65 46  def struct VdbeF
58310 75 6e 63 20 56 64 62 65 46 75 6e 63 3b 0a 74 79  unc VdbeFunc;.ty
58320 70 65 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d  pedef struct Mem
58330 20 4d 65 6d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73   Mem;../*.** A s
58340 69 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f  ingle instructio
58350 6e 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c  n of the virtual
58360 20 6d 61 63 68 69 6e 65 20 68 61 73 20 61 6e 20   machine has an 
58370 6f 70 63 6f 64 65 0a 2a 2a 20 61 6e 64 20 61 73  opcode.** and as
58380 20 6d 61 6e 79 20 61 73 20 74 68 72 65 65 20 6f   many as three o
58390 70 65 72 61 6e 64 73 2e 20 20 54 68 65 20 69 6e  perands.  The in
583a0 73 74 72 75 63 74 69 6f 6e 20 69 73 20 72 65 63  struction is rec
583b0 6f 72 64 65 64 0a 2a 2a 20 61 73 20 61 6e 20 69  orded.** as an i
583c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
583d0 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
583e0 72 65 3a 0a 2a 2f 0a 73 74 72 75 63 74 20 56 64  re:.*/.struct Vd
583f0 62 65 4f 70 20 7b 0a 20 20 75 38 20 6f 70 63 6f  beOp {.  u8 opco
58400 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
58410 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74  What operation t
58420 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73  o perform */.  s
58430 69 67 6e 65 64 20 63 68 61 72 20 70 34 74 79 70  igned char p4typ
58440 65 3b 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65  e; /* One of the
58450 20 50 34 5f 78 78 78 20 63 6f 6e 73 74 61 6e 74   P4_xxx constant
58460 73 20 66 6f 72 20 70 34 20 2a 2f 0a 20 20 75 38  s for p4 */.  u8
58470 20 6f 70 66 6c 61 67 73 3b 20 20 20 20 20 20 20   opflags;       
58480 20 20 2f 2a 20 4e 6f 74 20 63 75 72 72 65 6e 74    /* Not current
58490 6c 79 20 75 73 65 64 20 2a 2f 0a 20 20 75 38 20  ly used */.  u8 
584a0 70 35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p5;             
584b0 20 2f 2a 20 46 69 66 74 68 20 70 61 72 61 6d 65   /* Fifth parame
584c0 74 65 72 20 69 73 20 61 6e 20 75 6e 73 69 67 6e  ter is an unsign
584d0 65 64 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a  ed character */.
584e0 20 20 69 6e 74 20 70 31 3b 20 20 20 20 20 20 20    int p1;       
584f0 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6f        /* First o
58500 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e 74 20  perand */.  int 
58510 70 32 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  p2;             
58520 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72 61 6d 65  /* Second parame
58530 74 65 72 20 28 6f 66 74 65 6e 20 74 68 65 20 6a  ter (often the j
58540 75 6d 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 29  ump destination)
58550 20 2a 2f 0a 20 20 69 6e 74 20 70 33 3b 20 20 20   */.  int p3;   
58560 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
58570 20 74 68 69 72 64 20 70 61 72 61 6d 65 74 65 72   third parameter
58580 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 20 20 20   */.  union {   
58590 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 6f 72            /* for
585a0 74 68 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a  th parameter */.
585b0 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20      int i;      
585c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
585d0 74 65 67 65 72 20 76 61 6c 75 65 20 69 66 20 70  teger value if p
585e0 34 74 79 70 65 3d 3d 50 34 5f 49 4e 54 33 32 20  4type==P4_INT32 
585f0 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 3b 20  */.    void *p; 
58600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
58610 20 47 65 6e 65 72 69 63 20 70 6f 69 6e 74 65 72   Generic pointer
58620 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b   */.    char *z;
58630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
58640 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 64 61 74  * Pointer to dat
58650 61 20 66 6f 72 20 73 74 72 69 6e 67 20 28 63 68  a for string (ch
58660 61 72 20 61 72 72 61 79 29 20 74 79 70 65 73 20  ar array) types 
58670 2a 2f 0a 20 20 20 20 69 36 34 20 2a 70 49 36 34  */.    i64 *pI64
58680 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
58690 20 55 73 65 64 20 77 68 65 6e 20 70 34 74 79 70   Used when p4typ
586a0 65 20 69 73 20 50 34 5f 49 4e 54 36 34 20 2a 2f  e is P4_INT64 */
586b0 0a 20 20 20 20 64 6f 75 62 6c 65 20 2a 70 52 65  .    double *pRe
586c0 61 6c 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 55  al;         /* U
586d0 73 65 64 20 77 68 65 6e 20 70 34 74 79 70 65 20  sed when p4type 
586e0 69 73 20 50 34 5f 52 45 41 4c 20 2a 2f 0a 20 20  is P4_REAL */.  
586f0 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63    FuncDef *pFunc
58700 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65 64  ;        /* Used
58710 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
58720 50 34 5f 46 55 4e 43 44 45 46 20 2a 2f 0a 20 20  P4_FUNCDEF */.  
58730 20 20 56 64 62 65 46 75 6e 63 20 2a 70 56 64 62    VdbeFunc *pVdb
58740 65 46 75 6e 63 3b 20 20 20 2f 2a 20 55 73 65 64  eFunc;   /* Used
58750 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73 20   when p4type is 
58760 50 34 5f 56 44 42 45 46 55 4e 43 20 2a 2f 0a 20  P4_VDBEFUNC */. 
58770 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
58780 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65  l;        /* Use
58790 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
587a0 20 50 34 5f 43 4f 4c 4c 53 45 51 20 2a 2f 0a 20   P4_COLLSEQ */. 
587b0 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20 20 20     Mem *pMem;   
587c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73 65            /* Use
587d0 64 20 77 68 65 6e 20 70 34 74 79 70 65 20 69 73  d when p4type is
587e0 20 50 34 5f 4d 45 4d 20 2a 2f 0a 20 20 20 20 73   P4_MEM */.    s
587f0 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
58800 61 62 3b 20 20 20 2f 2a 20 55 73 65 64 20 77 68  ab;   /* Used wh
58810 65 6e 20 70 34 74 79 70 65 20 69 73 20 50 34 5f  en p4type is P4_
58820 56 54 41 42 20 2a 2f 0a 20 20 20 20 4b 65 79 49  VTAB */.    KeyI
58830 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20 20  nfo *pKeyInfo;  
58840 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
58850 70 34 74 79 70 65 20 69 73 20 50 34 5f 4b 45 59  p4type is P4_KEY
58860 49 4e 46 4f 20 2a 2f 0a 20 20 20 20 69 6e 74 20  INFO */.    int 
58870 2a 61 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  *ai;            
58880 20 20 20 2f 2a 20 55 73 65 64 20 77 68 65 6e 20     /* Used when 
58890 70 34 74 79 70 65 20 69 73 20 50 34 5f 49 4e 54  p4type is P4_INT
588a0 41 52 52 41 59 20 2a 2f 0a 20 20 7d 20 70 34 3b  ARRAY */.  } p4;
588b0 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
588c0 45 42 55 47 0a 20 20 63 68 61 72 20 2a 7a 43 6f  EBUG.  char *zCo
588d0 6d 6d 65 6e 74 3b 20 20 20 20 20 20 20 20 20 20  mment;          
588e0 2f 2a 20 43 6f 6d 6d 65 6e 74 20 74 6f 20 69 6d  /* Comment to im
588f0 70 72 6f 76 65 20 72 65 61 64 61 62 69 6c 69 74  prove readabilit
58900 79 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  y */.#endif.#ifd
58910 65 66 20 56 44 42 45 5f 50 52 4f 46 49 4c 45 0a  ef VDBE_PROFILE.
58920 20 20 69 6e 74 20 63 6e 74 3b 20 20 20 20 20 20    int cnt;      
58930 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
58940 6d 62 65 72 20 6f 66 20 74 69 6d 65 73 20 74 68  mber of times th
58950 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 77  is instruction w
58960 61 73 20 65 78 65 63 75 74 65 64 20 2a 2f 0a 20  as executed */. 
58970 20 75 36 34 20 63 79 63 6c 65 73 3b 20 20 20 20   u64 cycles;    
58980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74            /* Tot
58990 61 6c 20 74 69 6d 65 20 73 70 65 6e 74 20 65 78  al time spent ex
589a0 65 63 75 74 69 6e 67 20 74 68 69 73 20 69 6e 73  ecuting this ins
589b0 74 72 75 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e 64  truction */.#end
589c0 69 66 0a 7d 3b 0a 74 79 70 65 64 65 66 20 73 74  if.};.typedef st
589d0 72 75 63 74 20 56 64 62 65 4f 70 20 56 64 62 65  ruct VdbeOp Vdbe
589e0 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 6d 61  Op;../*.** A sma
589f0 6c 6c 65 72 20 76 65 72 73 69 6f 6e 20 6f 66 20  ller version of 
58a00 56 64 62 65 4f 70 20 75 73 65 64 20 66 6f 72 20  VdbeOp used for 
58a10 74 68 65 20 56 64 62 65 41 64 64 4f 70 4c 69 73  the VdbeAddOpLis
58a20 74 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63  t() function bec
58a30 61 75 73 65 0a 2a 2a 20 69 74 20 74 61 6b 65 73  ause.** it takes
58a40 20 75 70 20 6c 65 73 73 20 73 70 61 63 65 2e 0a   up less space..
58a50 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 4f 70  */.struct VdbeOp
58a60 4c 69 73 74 20 7b 0a 20 20 75 38 20 6f 70 63 6f  List {.  u8 opco
58a70 64 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  de;          /* 
58a80 57 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20 74  What operation t
58a90 6f 20 70 65 72 66 6f 72 6d 20 2a 2f 0a 20 20 73  o perform */.  s
58aa0 69 67 6e 65 64 20 63 68 61 72 20 70 31 3b 20 20  igned char p1;  
58ab0 20 20 20 2f 2a 20 46 69 72 73 74 20 6f 70 65 72     /* First oper
58ac0 61 6e 64 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20  and */.  signed 
58ad0 63 68 61 72 20 70 32 3b 20 20 20 20 20 2f 2a 20  char p2;     /* 
58ae0 53 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72  Second parameter
58af0 20 28 6f 66 74 65 6e 20 74 68 65 20 6a 75 6d 70   (often the jump
58b00 20 64 65 73 74 69 6e 61 74 69 6f 6e 29 20 2a 2f   destination) */
58b10 0a 20 20 73 69 67 6e 65 64 20 63 68 61 72 20 70  .  signed char p
58b20 33 3b 20 20 20 20 20 2f 2a 20 54 68 69 72 64 20  3;     /* Third 
58b30 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 7d 3b 0a  parameter */.};.
58b40 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 56  typedef struct V
58b50 64 62 65 4f 70 4c 69 73 74 20 56 64 62 65 4f 70  dbeOpList VdbeOp
58b60 4c 69 73 74 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c  List;../*.** All
58b70 6f 77 65 64 20 76 61 6c 75 65 73 20 6f 66 20 56  owed values of V
58b80 64 62 65 4f 70 2e 70 33 74 79 70 65 0a 2a 2f 0a  dbeOp.p3type.*/.
58b90 23 64 65 66 69 6e 65 20 50 34 5f 4e 4f 54 55 53  #define P4_NOTUS
58ba0 45 44 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65  ED    0   /* The
58bb0 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 69 73   P4 parameter is
58bc0 20 6e 6f 74 20 75 73 65 64 20 2a 2f 0a 23 64 65   not used */.#de
58bd0 66 69 6e 65 20 50 34 5f 44 59 4e 41 4d 49 43 20  fine P4_DYNAMIC 
58be0 20 28 2d 31 29 20 20 2f 2a 20 50 6f 69 6e 74 65   (-1)  /* Pointe
58bf0 72 20 74 6f 20 61 20 73 74 72 69 6e 67 20 6f 62  r to a string ob
58c00 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69  tained from sqli
58c10 74 65 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23 64  teMalloc() */.#d
58c20 65 66 69 6e 65 20 50 34 5f 53 54 41 54 49 43 20  efine P4_STATIC 
58c30 20 20 28 2d 32 29 20 20 2f 2a 20 50 6f 69 6e 74    (-2)  /* Point
58c40 65 72 20 74 6f 20 61 20 73 74 61 74 69 63 20 73  er to a static s
58c50 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  tring */.#define
58c60 20 50 34 5f 43 4f 4c 4c 53 45 51 20 20 28 2d 34   P4_COLLSEQ  (-4
58c70 29 20 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f  )  /* P4 is a po
58c80 69 6e 74 65 72 20 74 6f 20 61 20 43 6f 6c 6c 53  inter to a CollS
58c90 65 71 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  eq structure */.
58ca0 23 64 65 66 69 6e 65 20 50 34 5f 46 55 4e 43 44  #define P4_FUNCD
58cb0 45 46 20 20 28 2d 35 29 20 20 2f 2a 20 50 34 20  EF  (-5)  /* P4 
58cc0 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
58cd0 61 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74  a FuncDef struct
58ce0 75 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ure */.#define P
58cf0 34 5f 4b 45 59 49 4e 46 4f 20 20 28 2d 36 29 20  4_KEYINFO  (-6) 
58d00 20 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e   /* P4 is a poin
58d10 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f  ter to a KeyInfo
58d20 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64   structure */.#d
58d30 65 66 69 6e 65 20 50 34 5f 56 44 42 45 46 55 4e  efine P4_VDBEFUN
58d40 43 20 28 2d 37 29 20 20 2f 2a 20 50 34 20 69 73  C (-7)  /* P4 is
58d50 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
58d60 56 64 62 65 46 75 6e 63 20 73 74 72 75 63 74 75  VdbeFunc structu
58d70 72 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34  re */.#define P4
58d80 5f 4d 45 4d 20 20 20 20 20 20 28 2d 38 29 20 20  _MEM      (-8)  
58d90 2f 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  /* P4 is a point
58da0 65 72 20 74 6f 20 61 20 4d 65 6d 2a 20 20 20 20  er to a Mem*    
58db0 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 23 64 65  structure */.#de
58dc0 66 69 6e 65 20 50 34 5f 54 52 41 4e 53 49 45 4e  fine P4_TRANSIEN
58dd0 54 20 28 2d 39 29 20 2f 2a 20 50 34 20 69 73 20  T (-9) /* P4 is 
58de0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 74  a pointer to a t
58df0 72 61 6e 73 69 65 6e 74 20 73 74 72 69 6e 67 20  ransient string 
58e00 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 56 54  */.#define P4_VT
58e10 41 42 20 20 20 20 20 28 2d 31 30 29 20 2f 2a 20  AB     (-10) /* 
58e20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  P4 is a pointer 
58e30 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 76 74  to an sqlite3_vt
58e40 61 62 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a  ab structure */.
58e50 23 64 65 66 69 6e 65 20 50 34 5f 4d 50 52 49 4e  #define P4_MPRIN
58e60 54 46 20 20 28 2d 31 31 29 20 2f 2a 20 50 34 20  TF  (-11) /* P4 
58e70 69 73 20 61 20 73 74 72 69 6e 67 20 6f 62 74 61  is a string obta
58e80 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
58e90 33 5f 6d 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23  3_mprintf() */.#
58ea0 64 65 66 69 6e 65 20 50 34 5f 52 45 41 4c 20 20  define P4_REAL  
58eb0 20 20 20 28 2d 31 32 29 20 2f 2a 20 50 34 20 69     (-12) /* P4 i
58ec0 73 20 61 20 36 34 2d 62 69 74 20 66 6c 6f 61 74  s a 64-bit float
58ed0 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65 20  ing point value 
58ee0 2a 2f 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e  */.#define P4_IN
58ef0 54 36 34 20 20 20 20 28 2d 31 33 29 20 2f 2a 20  T64    (-13) /* 
58f00 50 34 20 69 73 20 61 20 36 34 2d 62 69 74 20 73  P4 is a 64-bit s
58f10 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f  igned integer */
58f20 0a 23 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 33  .#define P4_INT3
58f30 32 20 20 20 20 28 2d 31 34 29 20 2f 2a 20 50 34  2    (-14) /* P4
58f40 20 69 73 20 61 20 33 32 2d 62 69 74 20 73 69 67   is a 32-bit sig
58f50 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 23  ned integer */.#
58f60 64 65 66 69 6e 65 20 50 34 5f 49 4e 54 41 52 52  define P4_INTARR
58f70 41 59 20 28 2d 31 35 29 20 2f 2a 20 50 34 20 69  AY (-15) /* P4 i
58f80 73 20 61 20 76 65 63 74 6f 72 20 6f 66 20 33 32  s a vector of 32
58f90 2d 62 69 74 20 69 6e 74 65 67 65 72 73 20 2a 2f  -bit integers */
58fa0 0a 0a 2f 2a 20 57 68 65 6e 20 61 64 64 69 6e 67  ../* When adding
58fb0 20 61 20 50 34 20 61 72 67 75 6d 65 6e 74 20 75   a P4 argument u
58fc0 73 69 6e 67 20 50 34 5f 4b 45 59 49 4e 46 4f 2c  sing P4_KEYINFO,
58fd0 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 4b   a copy of the K
58fe0 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65  eyInfo structure
58ff0 0a 2a 2a 20 69 73 20 6d 61 64 65 2e 20 20 54 68  .** is made.  Th
59000 61 74 20 63 6f 70 79 20 69 73 20 66 72 65 65 64  at copy is freed
59010 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20 69   when the Vdbe i
59020 73 20 66 69 6e 61 6c 69 7a 65 64 2e 20 20 42 75  s finalized.  Bu
59030 74 20 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75  t if the.** argu
59040 6d 65 6e 74 20 69 73 20 50 34 5f 4b 45 59 49 4e  ment is P4_KEYIN
59050 46 4f 5f 48 41 4e 44 4f 46 46 2c 20 74 68 65 20  FO_HANDOFF, the 
59060 70 61 73 73 65 64 20 69 6e 20 70 6f 69 6e 74 65  passed in pointe
59070 72 20 69 73 20 75 73 65 64 2e 20 20 49 74 20 73  r is used.  It s
59080 74 69 6c 6c 0a 2a 2a 20 67 65 74 73 20 66 72 65  till.** gets fre
59090 65 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65  ed when the Vdbe
590a0 20 69 73 20 66 69 6e 61 6c 69 7a 65 64 20 73 6f   is finalized so
590b0 20 69 74 20 73 74 69 6c 6c 20 73 68 6f 75 6c 64   it still should
590c0 20 62 65 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20   be obtained.** 
590d0 66 72 6f 6d 20 61 20 73 69 6e 67 6c 65 20 73 71  from a single sq
590e0 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20 20 42  liteMalloc().  B
590f0 75 74 20 6e 6f 20 63 6f 70 79 20 69 73 20 6d 61  ut no copy is ma
59100 64 65 20 61 6e 64 20 74 68 65 20 63 61 6c 6c 69  de and the calli
59110 6e 67 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 73  ng.** function s
59120 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 74 72 79 20  hould *not* try 
59130 74 6f 20 66 72 65 65 20 74 68 65 20 4b 65 79 49  to free the KeyI
59140 6e 66 6f 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  nfo..*/.#define 
59150 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f  P4_KEYINFO_HANDO
59160 46 46 20 28 2d 31 36 29 0a 23 64 65 66 69 6e 65  FF (-16).#define
59170 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54   P4_KEYINFO_STAT
59180 49 43 20 20 28 2d 31 37 29 0a 0a 2f 2a 0a 2a 2a  IC  (-17)../*.**
59190 20 54 68 65 20 56 64 62 65 2e 61 43 6f 6c 4e 61   The Vdbe.aColNa
591a0 6d 65 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  me array contain
591b0 73 20 35 6e 20 4d 65 6d 20 73 74 72 75 63 74 75  s 5n Mem structu
591c0 72 65 73 2c 20 77 68 65 72 65 20 6e 20 69 73 20  res, where n is 
591d0 74 68 65 20 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f  the .** number o
591e0 66 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 64 61 74  f columns of dat
591f0 61 20 72 65 74 75 72 6e 65 64 20 62 79 20 74 68  a returned by th
59200 65 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  e statement..*/.
59210 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f  #define COLNAME_
59220 4e 41 4d 45 20 20 20 20 20 30 0a 23 64 65 66 69  NAME     0.#defi
59230 6e 65 20 43 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54  ne COLNAME_DECLT
59240 59 50 45 20 31 0a 23 64 65 66 69 6e 65 20 43 4f  YPE 1.#define CO
59250 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 20 32  LNAME_DATABASE 2
59260 0a 23 64 65 66 69 6e 65 20 43 4f 4c 4e 41 4d 45  .#define COLNAME
59270 5f 54 41 42 4c 45 20 20 20 20 33 0a 23 64 65 66  _TABLE    3.#def
59280 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55  ine COLNAME_COLU
59290 4d 4e 20 20 20 34 0a 23 69 66 64 65 66 20 53 51  MN   4.#ifdef SQ
592a0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55  LITE_ENABLE_COLU
592b0 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 23 20 64 65  MN_METADATA.# de
592c0 66 69 6e 65 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20  fine COLNAME_N  
592d0 20 20 20 20 20 20 35 20 20 20 20 20 20 2f 2a 20        5      /* 
592e0 4e 75 6d 62 65 72 20 6f 66 20 43 4f 4c 4e 41 4d  Number of COLNAM
592f0 45 5f 78 78 78 20 73 79 6d 62 6f 6c 73 20 2a 2f  E_xxx symbols */
59300 0a 23 65 6c 73 65 0a 23 20 69 66 64 65 66 20 53  .#else.# ifdef S
59310 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
59320 59 50 45 0a 23 20 20 20 64 65 66 69 6e 65 20 43  YPE.#   define C
59330 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20 31 20  OLNAME_N      1 
59340 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 6f 6e       /* Store on
59350 6c 79 20 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 23  ly the name */.#
59360 20 65 6c 73 65 0a 23 20 20 20 64 65 66 69 6e 65   else.#   define
59370 20 43 4f 4c 4e 41 4d 45 5f 4e 20 20 20 20 20 20   COLNAME_N      
59380 32 20 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20  2      /* Store 
59390 74 68 65 20 6e 61 6d 65 20 61 6e 64 20 64 65 63  the name and dec
593a0 6c 74 79 70 65 20 2a 2f 0a 23 20 65 6e 64 69 66  ltype */.# endif
593b0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
593c0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
593d0 72 6f 20 63 6f 6e 76 65 72 74 73 20 61 20 72 65  ro converts a re
593e0 6c 61 74 69 76 65 20 61 64 64 72 65 73 73 20 69  lative address i
593f0 6e 20 74 68 65 20 70 32 20 66 69 65 6c 64 0a 2a  n the p2 field.*
59400 2a 20 6f 66 20 61 20 56 64 62 65 4f 70 20 73 74  * of a VdbeOp st
59410 72 75 63 74 75 72 65 20 69 6e 74 6f 20 61 20 6e  ructure into a n
59420 65 67 61 74 69 76 65 20 6e 75 6d 62 65 72 20 73  egative number s
59430 6f 20 74 68 61 74 20 0a 2a 2a 20 73 71 6c 69 74  o that .** sqlit
59440 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74 28  e3VdbeAddOpList(
59450 29 20 6b 6e 6f 77 73 20 74 68 61 74 20 74 68 65  ) knows that the
59460 20 61 64 64 72 65 73 73 20 69 73 20 72 65 6c 61   address is rela
59470 74 69 76 65 2e 20 20 43 61 6c 6c 69 6e 67 0a 2a  tive.  Calling.*
59480 2a 20 74 68 65 20 6d 61 63 72 6f 20 61 67 61 69  * the macro agai
59490 6e 20 72 65 73 74 6f 72 65 73 20 74 68 65 20 61  n restores the a
594a0 64 64 72 65 73 73 2e 0a 2a 2f 0a 23 64 65 66 69  ddress..*/.#defi
594b0 6e 65 20 41 44 44 52 28 58 29 20 20 28 2d 31 2d  ne ADDR(X)  (-1-
594c0 28 58 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  (X))../*.** The 
594d0 6d 61 6b 65 66 69 6c 65 20 73 63 61 6e 73 20 74  makefile scans t
594e0 68 65 20 76 64 62 65 2e 63 20 73 6f 75 72 63 65  he vdbe.c source
594f0 20 66 69 6c 65 20 61 6e 64 20 63 72 65 61 74 65   file and create
59500 73 20 74 68 65 20 22 6f 70 63 6f 64 65 73 2e 68  s the "opcodes.h
59510 22 0a 2a 2a 20 68 65 61 64 65 72 20 66 69 6c 65  ".** header file
59520 20 74 68 61 74 20 64 65 66 69 6e 65 73 20 61 20   that defines a 
59530 6e 75 6d 62 65 72 20 66 6f 72 20 65 61 63 68 20  number for each 
59540 6f 70 63 6f 64 65 20 75 73 65 64 20 62 79 20 74  opcode used by t
59550 68 65 20 56 44 42 45 2e 0a 2a 2f 0a 2f 2a 2a 2a  he VDBE..*/./***
59560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
59570 75 64 65 20 6f 70 63 6f 64 65 73 2e 68 20 69 6e  ude opcodes.h in
59580 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 76   the middle of v
59590 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dbe.h **********
595a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
595b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
595c0 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e 68  n file opcodes.h
595d0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
595e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
595f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 41  **********/./* A
59600 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
59610 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74 20  erated.  Do not 
59620 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20 74  edit */./* See t
59630 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77 6b  he mkopcodeh.awk
59640 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74 61   script for deta
59650 69 6c 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ils */.#define O
59660 50 5f 56 4e 65 78 74 20 20 20 20 20 20 20 20 20  P_VNext         
59670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59680 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
59690 20 4f 50 5f 41 66 66 69 6e 69 74 79 20 20 20 20   OP_Affinity    
596a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
596b0 20 20 20 20 20 20 20 20 20 32 0a 23 64 65 66 69           2.#defi
596c0 6e 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 20 20 20  ne OP_Column    
596d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
596e0 20 20 20 20 20 20 20 20 20 20 20 33 0a 23 64 65             3.#de
596f0 66 69 6e 65 20 4f 50 5f 53 65 74 43 6f 6f 6b 69  fine OP_SetCooki
59700 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
59710 20 20 20 20 20 20 20 20 20 20 20 20 20 34 0a 23               4.#
59720 64 65 66 69 6e 65 20 4f 50 5f 52 65 61 6c 20 20  define OP_Real  
59730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59740 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32 36               126
59750 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b     /* same as TK
59760 5f 46 4c 4f 41 54 20 20 20 20 2a 2f 0a 23 64 65  _FLOAT    */.#de
59770 66 69 6e 65 20 4f 50 5f 53 65 71 75 65 6e 63 65  fine OP_Sequence
59780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59790 20 20 20 20 20 20 20 20 20 20 20 20 20 35 0a 23               5.#
597a0 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47 74  define OP_MoveGt
597b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
597c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 36                 6
597d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 47 65 20 20  .#define OP_Ge  
597e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
597f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59800 37 32 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  72   /* same as 
59810 54 4b 5f 47 45 20 20 20 20 20 20 20 2a 2f 0a 23  TK_GE       */.#
59820 64 65 66 69 6e 65 20 4f 50 5f 52 6f 77 4b 65 79  define OP_RowKey
59830 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
59850 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 43 6f 70  .#define OP_SCop
59860 79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y               
59870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59880 20 38 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45 71   8.#define OP_Eq
59890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
598a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
598b0 20 20 36 38 20 20 20 2f 2a 20 73 61 6d 65 20 61    68   /* same a
598c0 73 20 54 4b 5f 45 51 20 20 20 20 20 20 20 2a 2f  s TK_EQ       */
598d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70 65 6e  .#define OP_Open
598e0 57 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20  Write           
598f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59900 20 39 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f   9.#define OP_No
59910 74 4e 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  tNull           
59920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59930 20 20 36 36 20 20 20 2f 2a 20 73 61 6d 65 20 61    66   /* same a
59940 73 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 20 2a 2f  s TK_NOTNULL  */
59950 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 20 20  .#define OP_If  
59960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59980 31 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54 6f  10.#define OP_To
59990 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Int             
599a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
599b0 20 31 34 32 20 20 20 2f 2a 20 73 61 6d 65 20 61   142   /* same a
599c0 73 20 54 4b 5f 54 4f 5f 49 4e 54 20 20 20 2a 2f  s TK_TO_INT   */
599d0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74 72 69  .#define OP_Stri
599e0 6e 67 38 20 20 20 20 20 20 20 20 20 20 20 20 20  ng8             
599f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a00 38 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20  88   /* same as 
59a10 54 4b 5f 53 54 52 49 4e 47 20 20 20 2a 2f 0a 23  TK_STRING   */.#
59a20 64 65 66 69 6e 65 20 4f 50 5f 56 52 6f 77 69 64  define OP_VRowid
59a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
59a50 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6c 6c  .#define OP_Coll
59a60 53 65 71 20 20 20 20 20 20 20 20 20 20 20 20 20  Seq             
59a70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59a80 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4f 70  12.#define OP_Op
59a90 65 6e 52 65 61 64 20 20 20 20 20 20 20 20 20 20  enRead          
59aa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ab0 20 20 31 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    13.#define OP_
59ac0 45 78 70 69 72 65 20 20 20 20 20 20 20 20 20 20  Expire          
59ad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ae0 20 20 20 20 31 34 0a 23 64 65 66 69 6e 65 20 4f      14.#define O
59af0 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 20 20 20 20  P_AutoCommit    
59b00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b10 20 20 20 20 20 20 31 35 0a 23 64 65 66 69 6e 65        15.#define
59b20 20 4f 50 5f 47 74 20 20 20 20 20 20 20 20 20 20   OP_Gt          
59b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b40 20 20 20 20 20 20 20 20 36 39 20 20 20 2f 2a 20          69   /* 
59b50 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 20 20 20  same as TK_GT   
59b60 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
59b70 50 5f 50 61 67 65 63 6f 75 6e 74 20 20 20 20 20  P_Pagecount     
59b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59b90 20 20 20 20 20 20 31 37 0a 23 64 65 66 69 6e 65        17.#define
59ba0 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b 20   OP_IntegrityCk 
59bb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59bc0 20 20 20 20 20 20 20 20 31 38 0a 23 64 65 66 69          18.#defi
59bd0 6e 65 20 4f 50 5f 53 6f 72 74 20 20 20 20 20 20  ne OP_Sort      
59be0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59bf0 20 20 20 20 20 20 20 20 20 20 31 39 0a 23 64 65            19.#de
59c00 66 69 6e 65 20 4f 50 5f 43 6f 70 79 20 20 20 20  fine OP_Copy    
59c10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c20 20 20 20 20 20 20 20 20 20 20 20 20 32 30 0a 23              20.#
59c30 64 65 66 69 6e 65 20 4f 50 5f 54 72 61 63 65 20  define OP_Trace 
59c40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 31                21
59c60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 46 75 6e 63  .#define OP_Func
59c70 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
59c80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59c90 32 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66  22.#define OP_If
59ca0 4e 65 67 20 20 20 20 20 20 20 20 20 20 20 20 20  Neg             
59cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59cc0 20 20 32 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    23.#define OP_
59cd0 41 6e 64 20 20 20 20 20 20 20 20 20 20 20 20 20  And             
59ce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59cf0 20 20 20 20 36 31 20 20 20 2f 2a 20 73 61 6d 65      61   /* same
59d00 20 61 73 20 54 4b 5f 41 4e 44 20 20 20 20 20 20   as TK_AND      
59d10 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 75  */.#define OP_Su
59d20 62 74 72 61 63 74 20 20 20 20 20 20 20 20 20 20  btract          
59d30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d40 20 20 37 39 20 20 20 2f 2a 20 73 61 6d 65 20 61    79   /* same a
59d50 73 20 54 4b 5f 4d 49 4e 55 53 20 20 20 20 2a 2f  s TK_MINUS    */
59d60 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 6f 70  .#define OP_Noop
59d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59d90 32 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 52 65  24.#define OP_Re
59da0 74 75 72 6e 20 20 20 20 20 20 20 20 20 20 20 20  turn            
59db0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59dc0 20 20 32 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    25.#define OP_
59dd0 52 65 6d 61 69 6e 64 65 72 20 20 20 20 20 20 20  Remainder       
59de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59df0 20 20 20 20 38 32 20 20 20 2f 2a 20 73 61 6d 65      82   /* same
59e00 20 61 73 20 54 4b 5f 52 45 4d 20 20 20 20 20 20   as TK_REM      
59e10 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 65  */.#define OP_Ne
59e20 77 52 6f 77 69 64 20 20 20 20 20 20 20 20 20 20  wRowid          
59e30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59e40 20 20 32 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f    26.#define OP_
59e50 4d 75 6c 74 69 70 6c 79 20 20 20 20 20 20 20 20  Multiply        
59e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59e70 20 20 20 20 38 30 20 20 20 2f 2a 20 73 61 6d 65      80   /* same
59e80 20 61 73 20 54 4b 5f 53 54 41 52 20 20 20 20 20   as TK_STAR     
59e90 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 61  */.#define OP_Va
59ea0 72 69 61 62 6c 65 20 20 20 20 20 20 20 20 20 20  riable          
59eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ec0 20 20 32 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f    27.#define OP_
59ed0 53 74 72 69 6e 67 20 20 20 20 20 20 20 20 20 20  String          
59ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59ef0 20 20 20 20 32 38 0a 23 64 65 66 69 6e 65 20 4f      28.#define O
59f00 50 5f 52 65 61 6c 41 66 66 69 6e 69 74 79 20 20  P_RealAffinity  
59f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f20 20 20 20 20 20 20 32 39 0a 23 64 65 66 69 6e 65        29.#define
59f30 20 4f 50 5f 56 52 65 6e 61 6d 65 20 20 20 20 20   OP_VRename     
59f40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59f50 20 20 20 20 20 20 20 20 33 30 0a 23 64 65 66 69          30.#defi
59f60 6e 65 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  ne OP_ParseSchem
59f70 61 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  a               
59f80 20 20 20 20 20 20 20 20 20 20 33 31 0a 23 64 65            31.#de
59f90 66 69 6e 65 20 4f 50 5f 56 4f 70 65 6e 20 20 20  fine OP_VOpen   
59fa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59fb0 20 20 20 20 20 20 20 20 20 20 20 20 33 32 0a 23              32.#
59fc0 64 65 66 69 6e 65 20 4f 50 5f 43 6c 6f 73 65 20  define OP_Close 
59fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
59fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 33                33
59ff0 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 72 65 61  .#define OP_Crea
5a000 74 65 49 6e 64 65 78 20 20 20 20 20 20 20 20 20  teIndex         
5a010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a020 33 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 73  34.#define OP_Is
5a030 55 6e 69 71 75 65 20 20 20 20 20 20 20 20 20 20  Unique          
5a040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a050 20 20 33 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f    35.#define OP_
5a060 4e 6f 74 46 6f 75 6e 64 20 20 20 20 20 20 20 20  NotFound        
5a070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a080 20 20 20 20 33 36 0a 23 64 65 66 69 6e 65 20 4f      36.#define O
5a090 50 5f 49 6e 74 36 34 20 20 20 20 20 20 20 20 20  P_Int64         
5a0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0b0 20 20 20 20 20 20 33 37 0a 23 64 65 66 69 6e 65        37.#define
5a0c0 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 20 20 20   OP_MustBeInt   
5a0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a0e0 20 20 20 20 20 20 20 20 33 38 0a 23 64 65 66 69          38.#defi
5a0f0 6e 65 20 4f 50 5f 48 61 6c 74 20 20 20 20 20 20  ne OP_Halt      
5a100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a110 20 20 20 20 20 20 20 20 20 20 33 39 0a 23 64 65            39.#de
5a120 66 69 6e 65 20 4f 50 5f 52 6f 77 69 64 20 20 20  fine OP_Rowid   
5a130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a140 20 20 20 20 20 20 20 20 20 20 20 20 34 30 0a 23              40.#
5a150 64 65 66 69 6e 65 20 4f 50 5f 49 64 78 4c 54 20  define OP_IdxLT 
5a160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 34 31                41
5a180 0a 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 49  .#define OP_AddI
5a190 6d 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20  mm              
5a1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a1b0 34 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 74  42.#define OP_St
5a1c0 61 74 65 6d 65 6e 74 20 20 20 20 20 20 20 20 20  atement         
5a1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a1e0 20 20 34 33 0a 23 64 65 66 69 6e 65 20 4f 50 5f    43.#define OP_
5a1f0 52 6f 77 44 61 74 61 20 20 20 20 20 20 20 20 20  RowData         
5a200 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a210 20 20 20 20 34 34 0a 23 64 65 66 69 6e 65 20 4f      44.#define O
5a220 50 5f 4d 65 6d 4d 61 78 20 20 20 20 20 20 20 20  P_MemMax        
5a230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a240 20 20 20 20 20 20 34 35 0a 23 64 65 66 69 6e 65        45.#define
5a250 20 4f 50 5f 4f 72 20 20 20 20 20 20 20 20 20 20   OP_Or          
5a260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a270 20 20 20 20 20 20 20 20 36 30 20 20 20 2f 2a 20          60   /* 
5a280 73 61 6d 65 20 61 73 20 54 4b 5f 4f 52 20 20 20  same as TK_OR   
5a290 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f      */.#define O
5a2a0 50 5f 4e 6f 74 45 78 69 73 74 73 20 20 20 20 20  P_NotExists     
5a2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2c0 20 20 20 20 20 20 34 36 0a 23 64 65 66 69 6e 65        46.#define
5a2d0 20 4f 50 5f 47 6f 73 75 62 20 20 20 20 20 20 20   OP_Gosub       
5a2e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a2f0 20 20 20 20 20 20 20 20 34 37 0a 23 64 65 66 69          47.#defi
5a300 6e 65 20 4f 50 5f 44 69 76 69 64 65 20 20 20 20  ne OP_Divide    
5a310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a320 20 20 20 20 20 20 20 20 20 20 38 31 20 20 20 2f            81   /
5a330 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 53 4c 41  * same as TK_SLA
5a340 53 48 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65  SH    */.#define
5a350 20 4f 50 5f 49 6e 74 65 67 65 72 20 20 20 20 20   OP_Integer     
5a360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a370 20 20 20 20 20 20 20 20 34 38 0a 23 64 65 66 69          48.#defi
5a380 6e 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20  ne OP_ToNumeric 
5a390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3a0 20 20 20 20 20 20 20 20 20 31 34 31 20 20 20 2f           141   /
5a3b0 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f  * same as TK_TO_
5a3c0 4e 55 4d 45 52 49 43 2a 2f 0a 23 64 65 66 69 6e  NUMERIC*/.#defin
5a3d0 65 20 4f 50 5f 50 72 65 76 20 20 20 20 20 20 20  e OP_Prev       
5a3e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a3f0 20 20 20 20 20 20 20 20 20 34 39 0a 23 64 65 66           49.#def
5a400 69 6e 65 20 4f 50 5f 43 6f 6e 63 61 74 20 20 20  ine OP_Concat   
5a410 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a420 20 20 20 20 20 20 20 20 20 20 20 38 33 20 20 20             83   
5a430 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 43 4f  /* same as TK_CO
5a440 4e 43 41 54 20 20 20 2a 2f 0a 23 64 65 66 69 6e  NCAT   */.#defin
5a450 65 20 4f 50 5f 42 69 74 41 6e 64 20 20 20 20 20  e OP_BitAnd     
5a460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a470 20 20 20 20 20 20 20 20 20 37 34 20 20 20 2f 2a           74   /*
5a480 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 41   same as TK_BITA
5a490 4e 44 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ND   */.#define 
5a4a0 4f 50 5f 56 43 6f 6c 75 6d 6e 20 20 20 20 20 20  OP_VColumn      
5a4b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4c0 20 20 20 20 20 20 20 35 30 0a 23 64 65 66 69 6e         50.#defin
5a4d0 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  e OP_CreateTable
5a4e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a4f0 20 20 20 20 20 20 20 20 20 35 31 0a 23 64 65 66           51.#def
5a500 69 6e 65 20 4f 50 5f 4c 61 73 74 20 20 20 20 20  ine OP_Last     
5a510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a520 20 20 20 20 20 20 20 20 20 20 20 35 32 0a 23 64             52.#d
5a530 65 66 69 6e 65 20 4f 50 5f 49 73 4e 75 6c 6c 20  efine OP_IsNull 
5a540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a550 20 20 20 20 20 20 20 20 20 20 20 20 20 36 35 20               65 
5a560 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
5a570 49 53 4e 55 4c 4c 20 20 20 2a 2f 0a 23 64 65 66  ISNULL   */.#def
5a580 69 6e 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75  ine OP_IncrVacuu
5a590 6d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m               
5a5a0 20 20 20 20 20 20 20 20 20 20 20 35 33 0a 23 64             53.#d
5a5b0 65 66 69 6e 65 20 4f 50 5f 49 64 78 52 6f 77 69  efine OP_IdxRowi
5a5c0 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d               
5a5d0 20 20 20 20 20 20 20 20 20 20 20 20 20 35 34 0a               54.
5a5e0 23 64 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74  #define OP_Shift
5a5f0 52 69 67 68 74 20 20 20 20 20 20 20 20 20 20 20  Right           
5a600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
5a610 37 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  7   /* same as T
5a620 4b 5f 52 53 48 49 46 54 20 20 20 2a 2f 0a 23 64  K_RSHIFT   */.#d
5a630 65 66 69 6e 65 20 4f 50 5f 52 65 73 65 74 43 6f  efine OP_ResetCo
5a640 75 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  unt             
5a650 20 20 20 20 20 20 20 20 20 20 20 20 20 35 35 0a               55.
5a660 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f 57  #define OP_FifoW
5a670 72 69 74 65 20 20 20 20 20 20 20 20 20 20 20 20  rite            
5a680 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
5a690 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6f 6e  6.#define OP_Con
5a6a0 74 65 78 74 50 75 73 68 20 20 20 20 20 20 20 20  textPush        
5a6b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6c0 20 35 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 59   57.#define OP_Y
5a6d0 69 65 6c 64 20 20 20 20 20 20 20 20 20 20 20 20  ield            
5a6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a6f0 20 20 20 35 38 0a 23 64 65 66 69 6e 65 20 4f 50     58.#define OP
5a700 5f 44 72 6f 70 54 72 69 67 67 65 72 20 20 20 20  _DropTrigger    
5a710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a720 20 20 20 20 20 35 39 0a 23 64 65 66 69 6e 65 20       59.#define 
5a730 4f 50 5f 44 72 6f 70 49 6e 64 65 78 20 20 20 20  OP_DropIndex    
5a740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a750 20 20 20 20 20 20 20 36 32 0a 23 64 65 66 69 6e         62.#defin
5a760 65 20 4f 50 5f 49 64 78 47 45 20 20 20 20 20 20  e OP_IdxGE      
5a770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a780 20 20 20 20 20 20 20 20 20 36 33 0a 23 64 65 66           63.#def
5a790 69 6e 65 20 4f 50 5f 49 64 78 44 65 6c 65 74 65  ine OP_IdxDelete
5a7a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a7b0 20 20 20 20 20 20 20 20 20 20 20 36 34 0a 23 64             64.#d
5a7c0 65 66 69 6e 65 20 4f 50 5f 56 61 63 75 75 6d 20  efine OP_Vacuum 
5a7d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a7e0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 33 0a               73.
5a7f0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 4c  #define OP_MoveL
5a800 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
5a810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 38                 8
5a820 34 0a 23 64 65 66 69 6e 65 20 4f 50 5f 49 66 4e  4.#define OP_IfN
5a830 6f 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ot              
5a840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a850 20 38 35 0a 23 64 65 66 69 6e 65 20 4f 50 5f 44   85.#define OP_D
5a860 72 6f 70 54 61 62 6c 65 20 20 20 20 20 20 20 20  ropTable        
5a870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a880 20 20 20 38 36 0a 23 64 65 66 69 6e 65 20 4f 50     86.#define OP
5a890 5f 4d 61 6b 65 52 65 63 6f 72 64 20 20 20 20 20  _MakeRecord     
5a8a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a8b0 20 20 20 20 20 38 39 0a 23 64 65 66 69 6e 65 20       89.#define 
5a8c0 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 20 20 20  OP_ToBlob       
5a8d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a8e0 20 20 20 20 20 20 31 34 30 20 20 20 2f 2a 20 73        140   /* s
5a8f0 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 42 4c 4f  ame as TK_TO_BLO
5a900 42 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  B  */.#define OP
5a910 5f 52 65 73 75 6c 74 52 6f 77 20 20 20 20 20 20  _ResultRow      
5a920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a930 20 20 20 20 20 39 30 0a 23 64 65 66 69 6e 65 20       90.#define 
5a940 4f 50 5f 44 65 6c 65 74 65 20 20 20 20 20 20 20  OP_Delete       
5a950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a960 20 20 20 20 20 20 20 39 31 0a 23 64 65 66 69 6e         91.#defin
5a970 65 20 4f 50 5f 41 67 67 46 69 6e 61 6c 20 20 20  e OP_AggFinal   
5a980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a990 20 20 20 20 20 20 20 20 20 39 32 0a 23 64 65 66           92.#def
5a9a0 69 6e 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 20  ine OP_Compare  
5a9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5a9c0 20 20 20 20 20 20 20 20 20 20 20 39 33 0a 23 64             93.#d
5a9d0 65 66 69 6e 65 20 4f 50 5f 53 68 69 66 74 4c 65  efine OP_ShiftLe
5a9e0 66 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ft              
5a9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 37 36 20               76 
5aa00 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
5aa10 4c 53 48 49 46 54 20 20 20 2a 2f 0a 23 64 65 66  LSHIFT   */.#def
5aa20 69 6e 65 20 4f 50 5f 47 6f 74 6f 20 20 20 20 20  ine OP_Goto     
5aa30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aa40 20 20 20 20 20 20 20 20 20 20 20 39 34 0a 23 64             94.#d
5aa50 65 66 69 6e 65 20 4f 50 5f 54 61 62 6c 65 4c 6f  efine OP_TableLo
5aa60 63 6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ck              
5aa70 20 20 20 20 20 20 20 20 20 20 20 20 20 39 35 0a               95.
5aa80 23 64 65 66 69 6e 65 20 4f 50 5f 46 69 66 6f 52  #define OP_FifoR
5aa90 65 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20  ead             
5aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 39                 9
5aab0 36 0a 23 64 65 66 69 6e 65 20 4f 50 5f 43 6c 65  6.#define OP_Cle
5aac0 61 72 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ar              
5aad0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aae0 20 39 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4d   97.#define OP_M
5aaf0 6f 76 65 4c 74 20 20 20 20 20 20 20 20 20 20 20  oveLt           
5ab00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab10 20 20 20 39 38 0a 23 64 65 66 69 6e 65 20 4f 50     98.#define OP
5ab20 5f 4c 65 20 20 20 20 20 20 20 20 20 20 20 20 20  _Le             
5ab30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab40 20 20 20 20 20 37 30 20 20 20 2f 2a 20 73 61 6d       70   /* sam
5ab50 65 20 61 73 20 54 4b 5f 4c 45 20 20 20 20 20 20  e as TK_LE      
5ab60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56   */.#define OP_V
5ab70 65 72 69 66 79 43 6f 6f 6b 69 65 20 20 20 20 20  erifyCookie     
5ab80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ab90 20 20 20 39 39 0a 23 64 65 66 69 6e 65 20 4f 50     99.#define OP
5aba0 5f 41 67 67 53 74 65 70 20 20 20 20 20 20 20 20  _AggStep        
5abb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5abc0 20 20 20 20 31 30 30 0a 23 64 65 66 69 6e 65 20      100.#define 
5abd0 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 20 20 20  OP_ToText       
5abe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5abf0 20 20 20 20 20 20 31 33 39 20 20 20 2f 2a 20 73        139   /* s
5ac00 61 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58  ame as TK_TO_TEX
5ac10 54 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  T  */.#define OP
5ac20 5f 4e 6f 74 20 20 20 20 20 20 20 20 20 20 20 20  _Not            
5ac30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac40 20 20 20 20 20 31 36 20 20 20 2f 2a 20 73 61 6d       16   /* sam
5ac50 65 20 61 73 20 54 4b 5f 4e 4f 54 20 20 20 20 20  e as TK_NOT     
5ac60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54   */.#define OP_T
5ac70 6f 52 65 61 6c 20 20 20 20 20 20 20 20 20 20 20  oReal           
5ac80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ac90 20 20 31 34 33 20 20 20 2f 2a 20 73 61 6d 65 20    143   /* same 
5aca0 61 73 20 54 4b 5f 54 4f 5f 52 45 41 4c 20 20 2a  as TK_TO_REAL  *
5acb0 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 53 65 74  /.#define OP_Set
5acc0 4e 75 6d 43 6f 6c 75 6d 6e 73 20 20 20 20 20 20  NumColumns      
5acd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ace0 31 30 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 54  101.#define OP_T
5acf0 72 61 6e 73 61 63 74 69 6f 6e 20 20 20 20 20 20  ransaction      
5ad00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad10 20 20 31 30 32 0a 23 64 65 66 69 6e 65 20 4f 50    102.#define OP
5ad20 5f 56 46 69 6c 74 65 72 20 20 20 20 20 20 20 20  _VFilter        
5ad30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad40 20 20 20 20 31 30 33 0a 23 64 65 66 69 6e 65 20      103.#define 
5ad50 4f 50 5f 4e 65 20 20 20 20 20 20 20 20 20 20 20  OP_Ne           
5ad60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ad70 20 20 20 20 20 20 20 36 37 20 20 20 2f 2a 20 73         67   /* s
5ad80 61 6d 65 20 61 73 20 54 4b 5f 4e 45 20 20 20 20  ame as TK_NE    
5ad90 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
5ada0 5f 56 44 65 73 74 72 6f 79 20 20 20 20 20 20 20  _VDestroy       
5adb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5adc0 20 20 20 20 31 30 34 0a 23 64 65 66 69 6e 65 20      104.#define 
5add0 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70 20 20 20  OP_ContextPop   
5ade0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5adf0 20 20 20 20 20 20 31 30 35 0a 23 64 65 66 69 6e        105.#defin
5ae00 65 20 4f 50 5f 42 69 74 4f 72 20 20 20 20 20 20  e OP_BitOr      
5ae10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae20 20 20 20 20 20 20 20 20 20 37 35 20 20 20 2f 2a           75   /*
5ae30 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4f   same as TK_BITO
5ae40 52 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20  R    */.#define 
5ae50 4f 50 5f 4e 65 78 74 20 20 20 20 20 20 20 20 20  OP_Next         
5ae60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ae70 20 20 20 20 20 20 31 30 36 0a 23 64 65 66 69 6e        106.#defin
5ae80 65 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 20 20  e OP_IdxInsert  
5ae90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aea0 20 20 20 20 20 20 20 20 31 30 37 0a 23 64 65 66          107.#def
5aeb0 69 6e 65 20 4f 50 5f 4c 74 20 20 20 20 20 20 20  ine OP_Lt       
5aec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aed0 20 20 20 20 20 20 20 20 20 20 20 37 31 20 20 20             71   
5aee0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 54  /* same as TK_LT
5aef0 20 20 20 20 20 20 20 2a 2f 0a 23 64 65 66 69 6e         */.#defin
5af00 65 20 4f 50 5f 49 6e 73 65 72 74 20 20 20 20 20  e OP_Insert     
5af10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af20 20 20 20 20 20 20 20 20 31 30 38 0a 23 64 65 66          108.#def
5af30 69 6e 65 20 4f 50 5f 44 65 73 74 72 6f 79 20 20  ine OP_Destroy  
5af40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5af50 20 20 20 20 20 20 20 20 20 20 31 30 39 0a 23 64            109.#d
5af60 65 66 69 6e 65 20 4f 50 5f 52 65 61 64 43 6f 6f  efine OP_ReadCoo
5af70 6b 69 65 20 20 20 20 20 20 20 20 20 20 20 20 20  kie             
5af80 20 20 20 20 20 20 20 20 20 20 20 20 31 31 30 0a              110.
5af90 23 64 65 66 69 6e 65 20 4f 50 5f 46 6f 72 63 65  #define OP_Force
5afa0 49 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20  Int             
5afb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 31                11
5afc0 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4c 6f 61  1.#define OP_Loa
5afd0 64 41 6e 61 6c 79 73 69 73 20 20 20 20 20 20 20  dAnalysis       
5afe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5aff0 31 31 32 0a 23 64 65 66 69 6e 65 20 4f 50 5f 45  112.#define OP_E
5b000 78 70 6c 61 69 6e 20 20 20 20 20 20 20 20 20 20  xplain          
5b010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b020 20 20 31 31 33 0a 23 64 65 66 69 6e 65 20 4f 50    113.#define OP
5b030 5f 4f 70 65 6e 50 73 65 75 64 6f 20 20 20 20 20  _OpenPseudo     
5b040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b050 20 20 20 20 31 31 34 0a 23 64 65 66 69 6e 65 20      114.#define 
5b060 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
5b070 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b080 20 20 20 20 20 20 31 31 35 0a 23 64 65 66 69 6e        115.#defin
5b090 65 20 4f 50 5f 4e 75 6c 6c 20 20 20 20 20 20 20  e OP_Null       
5b0a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0b0 20 20 20 20 20 20 20 20 31 31 36 0a 23 64 65 66          116.#def
5b0c0 69 6e 65 20 4f 50 5f 4d 6f 76 65 20 20 20 20 20  ine OP_Move     
5b0d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b0e0 20 20 20 20 20 20 20 20 20 20 31 31 37 0a 23 64            117.#d
5b0f0 65 66 69 6e 65 20 4f 50 5f 42 6c 6f 62 20 20 20  efine OP_Blob   
5b100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b110 20 20 20 20 20 20 20 20 20 20 20 20 31 31 38 0a              118.
5b120 23 64 65 66 69 6e 65 20 4f 50 5f 41 64 64 20 20  #define OP_Add  
5b130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 37                 7
5b150 38 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54  8   /* same as T
5b160 4b 5f 50 4c 55 53 20 20 20 20 20 2a 2f 0a 23 64  K_PLUS     */.#d
5b170 65 66 69 6e 65 20 4f 50 5f 52 65 77 69 6e 64 20  efine OP_Rewind 
5b180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b190 20 20 20 20 20 20 20 20 20 20 20 20 31 31 39 0a              119.
5b1a0 23 64 65 66 69 6e 65 20 4f 50 5f 4d 6f 76 65 47  #define OP_MoveG
5b1b0 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
5b1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 32                12
5b1d0 30 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56 42 65  0.#define OP_VBe
5b1e0 67 69 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  gin             
5b1f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b200 31 32 31 0a 23 64 65 66 69 6e 65 20 4f 50 5f 56  121.#define OP_V
5b210 55 70 64 61 74 65 20 20 20 20 20 20 20 20 20 20  Update          
5b220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b230 20 20 31 32 32 0a 23 64 65 66 69 6e 65 20 4f 50    122.#define OP
5b240 5f 49 66 5a 65 72 6f 20 20 20 20 20 20 20 20 20  _IfZero         
5b250 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b260 20 20 20 20 31 32 33 0a 23 64 65 66 69 6e 65 20      123.#define 
5b270 4f 50 5f 42 69 74 4e 6f 74 20 20 20 20 20 20 20  OP_BitNot       
5b280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b290 20 20 20 20 20 20 20 38 37 20 20 20 2f 2a 20 73         87   /* s
5b2a0 61 6d 65 20 61 73 20 54 4b 5f 42 49 54 4e 4f 54  ame as TK_BITNOT
5b2b0 20 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50     */.#define OP
5b2c0 5f 56 43 72 65 61 74 65 20 20 20 20 20 20 20 20  _VCreate        
5b2d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b2e0 20 20 20 20 31 32 34 0a 23 64 65 66 69 6e 65 20      124.#define 
5b2f0 4f 50 5f 46 6f 75 6e 64 20 20 20 20 20 20 20 20  OP_Found        
5b300 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b310 20 20 20 20 20 20 31 32 35 0a 23 64 65 66 69 6e        125.#defin
5b320 65 20 4f 50 5f 49 66 50 6f 73 20 20 20 20 20 20  e OP_IfPos      
5b330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b340 20 20 20 20 20 20 20 20 31 32 37 0a 23 64 65 66          127.#def
5b350 69 6e 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77 20 20  ine OP_NullRow  
5b360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b370 20 20 20 20 20 20 20 20 20 20 31 32 38 0a 23 64            128.#d
5b380 65 66 69 6e 65 20 4f 50 5f 4a 75 6d 70 20 20 20  efine OP_Jump   
5b390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b3a0 20 20 20 20 20 20 20 20 20 20 20 20 31 32 39 0a              129.
5b3b0 23 64 65 66 69 6e 65 20 4f 50 5f 50 65 72 6d 75  #define OP_Permu
5b3c0 74 61 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20  tation          
5b3d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
5b3e0 30 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  0../* The follow
5b3f0 69 6e 67 20 6f 70 63 6f 64 65 20 76 61 6c 75 65  ing opcode value
5b400 73 20 61 72 65 20 6e 65 76 65 72 20 75 73 65 64  s are never used
5b410 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e   */.#define OP_N
5b420 6f 74 55 73 65 64 5f 31 33 31 20 20 20 20 20 20  otUsed_131      
5b430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b440 20 20 31 33 31 0a 23 64 65 66 69 6e 65 20 4f 50    131.#define OP
5b450 5f 4e 6f 74 55 73 65 64 5f 31 33 32 20 20 20 20  _NotUsed_132    
5b460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b470 20 20 20 20 31 33 32 0a 23 64 65 66 69 6e 65 20      132.#define 
5b480 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 33 20 20  OP_NotUsed_133  
5b490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4a0 20 20 20 20 20 20 31 33 33 0a 23 64 65 66 69 6e        133.#defin
5b4b0 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31 33 34  e OP_NotUsed_134
5b4c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b4d0 20 20 20 20 20 20 20 20 31 33 34 0a 23 64 65 66          134.#def
5b4e0 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64 5f 31  ine OP_NotUsed_1
5b4f0 33 35 20 20 20 20 20 20 20 20 20 20 20 20 20 20  35              
5b500 20 20 20 20 20 20 20 20 20 20 31 33 35 0a 23 64            135.#d
5b510 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73 65 64  efine OP_NotUsed
5b520 5f 31 33 36 20 20 20 20 20 20 20 20 20 20 20 20  _136            
5b530 20 20 20 20 20 20 20 20 20 20 20 20 31 33 36 0a              136.
5b540 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74 55 73  #define OP_NotUs
5b550 65 64 5f 31 33 37 20 20 20 20 20 20 20 20 20 20  ed_137          
5b560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 33                13
5b570 37 0a 23 64 65 66 69 6e 65 20 4f 50 5f 4e 6f 74  7.#define OP_Not
5b580 55 73 65 64 5f 31 33 38 20 20 20 20 20 20 20 20  Used_138        
5b590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5b5a0 31 33 38 0a 0a 0a 2f 2a 20 50 72 6f 70 65 72 74  138.../* Propert
5b5b0 69 65 73 20 73 75 63 68 20 61 73 20 22 6f 75 74  ies such as "out
5b5c0 32 22 20 6f 72 20 22 6a 75 6d 70 22 20 74 68 61  2" or "jump" tha
5b5d0 74 20 61 72 65 20 73 70 65 63 69 66 69 65 64 20  t are specified 
5b5e0 69 6e 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 66  in.** comments f
5b5f0 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61  ollowing the "ca
5b600 73 65 22 20 66 6f 72 20 65 61 63 68 20 6f 70 63  se" for each opc
5b610 6f 64 65 20 69 6e 20 74 68 65 20 76 64 62 65 2e  ode in the vdbe.
5b620 63 0a 2a 2a 20 61 72 65 20 65 6e 63 6f 64 65 64  c.** are encoded
5b630 20 69 6e 74 6f 20 62 69 74 76 65 63 74 6f 72 73   into bitvectors
5b640 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2f 0a   as follows:.*/.
5b650 23 64 65 66 69 6e 65 20 4f 50 46 4c 47 5f 4a 55  #define OPFLG_JU
5b660 4d 50 20 20 20 20 20 20 20 20 20 20 20 20 30 78  MP            0x
5b670 30 30 30 31 20 20 2f 2a 20 6a 75 6d 70 3a 20 20  0001  /* jump:  
5b680 50 32 20 68 6f 6c 64 73 20 6a 6d 70 20 74 61 72  P2 holds jmp tar
5b690 67 65 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  get */.#define O
5b6a0 50 46 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c  PFLG_OUT2_PREREL
5b6b0 45 41 53 45 20 30 78 30 30 30 32 20 20 2f 2a 20  EASE 0x0002  /* 
5b6c0 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 3a  out2-prerelease:
5b6d0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c   */.#define OPFL
5b6e0 47 5f 49 4e 31 20 20 20 20 20 20 20 20 20 20 20  G_IN1           
5b6f0 20 20 30 78 30 30 30 34 20 20 2f 2a 20 69 6e 31    0x0004  /* in1
5b700 3a 20 20 20 50 31 20 69 73 20 61 6e 20 69 6e 70  :   P1 is an inp
5b710 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50  ut */.#define OP
5b720 46 4c 47 5f 49 4e 32 20 20 20 20 20 20 20 20 20  FLG_IN2         
5b730 20 20 20 20 30 78 30 30 30 38 20 20 2f 2a 20 69      0x0008  /* i
5b740 6e 32 3a 20 20 20 50 32 20 69 73 20 61 6e 20 69  n2:   P2 is an i
5b750 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nput */.#define 
5b760 4f 50 46 4c 47 5f 49 4e 33 20 20 20 20 20 20 20  OPFLG_IN3       
5b770 20 20 20 20 20 20 30 78 30 30 31 30 20 20 2f 2a        0x0010  /*
5b780 20 69 6e 33 3a 20 20 20 50 33 20 69 73 20 61 6e   in3:   P3 is an
5b790 20 69 6e 70 75 74 20 2a 2f 0a 23 64 65 66 69 6e   input */.#defin
5b7a0 65 20 4f 50 46 4c 47 5f 4f 55 54 33 20 20 20 20  e OPFLG_OUT3    
5b7b0 20 20 20 20 20 20 20 20 30 78 30 30 32 30 20 20          0x0020  
5b7c0 2f 2a 20 6f 75 74 33 3a 20 20 50 33 20 69 73 20  /* out3:  P3 is 
5b7d0 61 6e 20 6f 75 74 70 75 74 20 2a 2f 0a 23 64 65  an output */.#de
5b7e0 66 69 6e 65 20 4f 50 46 4c 47 5f 49 4e 49 54 49  fine OPFLG_INITI
5b7f0 41 4c 49 5a 45 52 20 7b 5c 0a 2f 2a 20 20 20 30  ALIZER {\./*   0
5b800 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20   */ 0x00, 0x01, 
5b810 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 31 30  0x00, 0x00, 0x10
5b820 2c 20 30 78 30 32 2c 20 30 78 31 31 2c 20 30 78  , 0x02, 0x11, 0x
5b830 30 30 2c 5c 0a 2f 2a 20 20 20 38 20 2a 2f 20 30  00,\./*   8 */ 0
5b840 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c  x00, 0x00, 0x05,
5b850 20 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30   0x02, 0x00, 0x0
5b860 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 5c 0a  0, 0x00, 0x00,\.
5b870 2f 2a 20 20 31 36 20 2a 2f 20 30 78 30 34 2c 20  /*  16 */ 0x04, 
5b880 30 78 30 32 2c 20 30 78 30 30 2c 20 30 78 30 31  0x02, 0x00, 0x01
5b890 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
5b8a0 30 30 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 20 32  00, 0x05,\./*  2
5b8b0 34 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 34 2c  4 */ 0x00, 0x04,
5b8c0 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
5b8d0 32 2c 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30  2, 0x04, 0x00, 0
5b8e0 78 30 30 2c 5c 0a 2f 2a 20 20 33 32 20 2a 2f 20  x00,\./*  32 */ 
5b8f0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 32  0x00, 0x00, 0x02
5b900 2c 20 30 78 31 31 2c 20 30 78 31 31 2c 20 30 78  , 0x11, 0x11, 0x
5b910 30 32 2c 20 30 78 30 35 2c 20 30 78 30 30 2c 5c  02, 0x05, 0x00,\
5b920 0a 2f 2a 20 20 34 30 20 2a 2f 20 30 78 30 32 2c  ./*  40 */ 0x02,
5b930 20 30 78 31 31 2c 20 30 78 30 34 2c 20 30 78 30   0x11, 0x04, 0x0
5b940 30 2c 20 30 78 30 30 2c 20 30 78 30 63 2c 20 30  0, 0x00, 0x0c, 0
5b950 78 31 31 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 20  x11, 0x01,\./*  
5b960 34 38 20 2a 2f 20 30 78 30 32 2c 20 30 78 30 31  48 */ 0x02, 0x01
5b970 2c 20 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78  , 0x00, 0x02, 0x
5b980 30 31 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  01, 0x01, 0x02, 
5b990 30 78 30 30 2c 5c 0a 2f 2a 20 20 35 36 20 2a 2f  0x00,\./*  56 */
5b9a0 20 30 78 30 34 2c 20 30 78 30 30 2c 20 30 78 30   0x04, 0x00, 0x0
5b9b0 30 2c 20 30 78 30 30 2c 20 30 78 32 63 2c 20 30  0, 0x00, 0x2c, 0
5b9c0 78 32 63 2c 20 30 78 30 30 2c 20 30 78 31 31 2c  x2c, 0x00, 0x11,
5b9d0 5c 0a 2f 2a 20 20 36 34 20 2a 2f 20 30 78 30 30  \./*  64 */ 0x00
5b9e0 2c 20 30 78 30 35 2c 20 30 78 30 35 2c 20 30 78  , 0x05, 0x05, 0x
5b9f0 31 35 2c 20 30 78 31 35 2c 20 30 78 31 35 2c 20  15, 0x15, 0x15, 
5ba00 30 78 31 35 2c 20 30 78 31 35 2c 5c 0a 2f 2a 20  0x15, 0x15,\./* 
5ba10 20 37 32 20 2a 2f 20 30 78 31 35 2c 20 30 78 30   72 */ 0x15, 0x0
5ba20 30 2c 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30  0, 0x2c, 0x2c, 0
5ba30 78 32 63 2c 20 30 78 32 63 2c 20 30 78 32 63 2c  x2c, 0x2c, 0x2c,
5ba40 20 30 78 32 63 2c 5c 0a 2f 2a 20 20 38 30 20 2a   0x2c,\./*  80 *
5ba50 2f 20 30 78 32 63 2c 20 30 78 32 63 2c 20 30 78  / 0x2c, 0x2c, 0x
5ba60 32 63 2c 20 30 78 32 63 2c 20 30 78 31 31 2c 20  2c, 0x2c, 0x11, 
5ba70 30 78 30 35 2c 20 30 78 30 30 2c 20 30 78 30 34  0x05, 0x00, 0x04
5ba80 2c 5c 0a 2f 2a 20 20 38 38 20 2a 2f 20 30 78 30  ,\./*  88 */ 0x0
5ba90 32 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  2, 0x00, 0x00, 0
5baa0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
5bab0 20 30 78 30 31 2c 20 30 78 30 30 2c 5c 0a 2f 2a   0x01, 0x00,\./*
5bac0 20 20 39 36 20 2a 2f 20 30 78 30 31 2c 20 30 78    96 */ 0x01, 0x
5bad0 30 30 2c 20 30 78 31 31 2c 20 30 78 30 30 2c 20  00, 0x11, 0x00, 
5bae0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
5baf0 2c 20 30 78 30 31 2c 5c 0a 2f 2a 20 31 30 34 20  , 0x01,\./* 104 
5bb00 2a 2f 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30  */ 0x00, 0x00, 0
5bb10 78 30 31 2c 20 30 78 30 38 2c 20 30 78 30 30 2c  x01, 0x08, 0x00,
5bb20 20 30 78 30 32 2c 20 30 78 30 32 2c 20 30 78 30   0x02, 0x02, 0x0
5bb30 35 2c 5c 0a 2f 2a 20 31 31 32 20 2a 2f 20 30 78  5,\./* 112 */ 0x
5bb40 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20  00, 0x00, 0x00, 
5bb50 30 78 30 30 2c 20 30 78 30 32 2c 20 30 78 30 30  0x00, 0x02, 0x00
5bb60 2c 20 30 78 30 32 2c 20 30 78 30 31 2c 5c 0a 2f  , 0x02, 0x01,\./
5bb70 2a 20 31 32 30 20 2a 2f 20 30 78 31 31 2c 20 30  * 120 */ 0x11, 0
5bb80 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 35 2c  x00, 0x00, 0x05,
5bb90 20 30 78 30 30 2c 20 30 78 31 31 2c 20 30 78 30   0x00, 0x11, 0x0
5bba0 32 2c 20 30 78 30 35 2c 5c 0a 2f 2a 20 31 32 38  2, 0x05,\./* 128
5bbb0 20 2a 2f 20 30 78 30 30 2c 20 30 78 30 31 2c 20   */ 0x00, 0x01, 
5bbc0 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30  0x00, 0x00, 0x00
5bbd0 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 20 30 78  , 0x00, 0x00, 0x
5bbe0 30 30 2c 5c 0a 2f 2a 20 31 33 36 20 2a 2f 20 30  00,\./* 136 */ 0
5bbf0 78 30 30 2c 20 30 78 30 30 2c 20 30 78 30 30 2c  x00, 0x00, 0x00,
5bc00 20 30 78 30 34 2c 20 30 78 30 34 2c 20 30 78 30   0x04, 0x04, 0x0
5bc10 34 2c 20 30 78 30 34 2c 20 30 78 30 34 2c 5c 0a  4, 0x04, 0x04,\.
5bc20 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
5bc30 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64 65  ** End of opcode
5bc40 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.h ************
5bc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bc60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bc70 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
5bc80 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
5bc90 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
5bca0 69 6e 20 76 64 62 65 2e 68 20 2a 2a 2a 2a 2a 2a  in vdbe.h ******
5bcb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5bcc0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 74 6f 74  */../*.** Protot
5bcd0 79 70 65 73 20 66 6f 72 20 74 68 65 20 56 44 42  ypes for the VDB
5bce0 45 20 69 6e 74 65 72 66 61 63 65 2e 20 20 53 65  E interface.  Se
5bcf0 65 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68  e comments on th
5bd00 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  e implementation
5bd10 0a 2a 2a 20 66 6f 72 20 61 20 64 65 73 63 72 69  .** for a descri
5bd20 70 74 69 6f 6e 20 6f 66 20 77 68 61 74 20 65 61  ption of what ea
5bd30 63 68 20 6f 66 20 74 68 65 73 65 20 72 6f 75 74  ch of these rout
5bd40 69 6e 65 73 20 64 6f 65 73 2e 0a 2a 2f 0a 53 51  ines does..*/.SQ
5bd50 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64 62  LITE_PRIVATE Vdb
5bd60 65 20 2a 73 71 6c 69 74 65 33 56 64 62 65 43 72  e *sqlite3VdbeCr
5bd70 65 61 74 65 28 73 71 6c 69 74 65 33 2a 29 3b 0a  eate(sqlite3*);.
5bd80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5bd90 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  nt sqlite3VdbeAd
5bda0 64 4f 70 30 28 56 64 62 65 2a 2c 69 6e 74 29 3b  dOp0(Vdbe*,int);
5bdb0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5bdc0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
5bdd0 64 64 4f 70 31 28 56 64 62 65 2a 2c 69 6e 74 2c  ddOp1(Vdbe*,int,
5bde0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5bdf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5be00 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 2a  VdbeAddOp2(Vdbe*
5be10 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53  ,int,int,int);.S
5be20 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5be30 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
5be40 4f 70 33 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e  Op3(Vdbe*,int,in
5be50 74 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  t,int,int);.SQLI
5be60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5be70 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
5be80 28 56 64 62 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69  (Vdbe*,int,int,i
5be90 6e 74 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61  nt,int,const cha
5bea0 72 20 2a 7a 50 34 2c 69 6e 74 29 3b 0a 53 51 4c  r *zP4,int);.SQL
5beb0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5bec0 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
5bed0 4c 69 73 74 28 56 64 62 65 2a 2c 20 69 6e 74 20  List(Vdbe*, int 
5bee0 6e 4f 70 2c 20 56 64 62 65 4f 70 4c 69 73 74 20  nOp, VdbeOpList 
5bef0 63 6f 6e 73 74 20 2a 61 4f 70 29 3b 0a 53 51 4c  const *aOp);.SQL
5bf00 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5bf10 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
5bf20 67 65 50 31 28 56 64 62 65 2a 2c 20 69 6e 74 20  geP1(Vdbe*, int 
5bf30 61 64 64 72 2c 20 69 6e 74 20 50 31 29 3b 0a 53  addr, int P1);.S
5bf40 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5bf50 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
5bf60 61 6e 67 65 50 32 28 56 64 62 65 2a 2c 20 69 6e  angeP2(Vdbe*, in
5bf70 74 20 61 64 64 72 2c 20 69 6e 74 20 50 32 29 3b  t addr, int P2);
5bf80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5bf90 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
5bfa0 43 68 61 6e 67 65 50 33 28 56 64 62 65 2a 2c 20  ChangeP3(Vdbe*, 
5bfb0 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 50 33  int addr, int P3
5bfc0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5bfd0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
5bfe0 62 65 43 68 61 6e 67 65 50 35 28 56 64 62 65 2a  beChangeP5(Vdbe*
5bff0 2c 20 75 38 20 50 35 29 3b 0a 53 51 4c 49 54 45  , u8 P5);.SQLITE
5c000 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5c010 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
5c020 65 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64  e(Vdbe*, int add
5c030 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
5c040 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
5c050 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
5c060 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72 2c  Vdbe*, int addr,
5c070 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54 45 5f   int N);.SQLITE_
5c080 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5c090 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34  ite3VdbeChangeP4
5c0a0 28 56 64 62 65 2a 2c 20 69 6e 74 20 61 64 64 72  (Vdbe*, int addr
5c0b0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50  , const char *zP
5c0c0 34 2c 20 69 6e 74 20 4e 29 3b 0a 53 51 4c 49 54  4, int N);.SQLIT
5c0d0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
5c0e0 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
5c0f0 72 65 65 28 56 64 62 65 2a 2c 20 69 6e 74 29 3b  ree(Vdbe*, int);
5c100 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5c110 56 64 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56  VdbeOp *sqlite3V
5c120 64 62 65 47 65 74 4f 70 28 56 64 62 65 2a 2c 20  dbeGetOp(Vdbe*, 
5c130 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
5c140 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5c150 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 56 64  VdbeMakeLabel(Vd
5c160 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
5c170 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5c180 33 56 64 62 65 44 65 6c 65 74 65 28 56 64 62 65  3VdbeDelete(Vdbe
5c190 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5c1a0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
5c1b0 64 62 65 4d 61 6b 65 52 65 61 64 79 28 56 64 62  dbeMakeReady(Vdb
5c1c0 65 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69  e*,int,int,int,i
5c1d0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
5c1e0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
5c1f0 64 62 65 46 69 6e 61 6c 69 7a 65 28 56 64 62 65  dbeFinalize(Vdbe
5c200 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5c210 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
5c220 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
5c230 56 64 62 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  Vdbe*, int);.SQL
5c240 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5c250 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
5c260 6e 74 41 64 64 72 28 56 64 62 65 2a 29 3b 0a 23  ntAddr(Vdbe*);.#
5c270 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
5c280 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  UG.SQLITE_PRIVAT
5c290 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
5c2a0 56 64 62 65 54 72 61 63 65 28 56 64 62 65 2a 2c  VdbeTrace(Vdbe*,
5c2b0 46 49 4c 45 2a 29 3b 0a 23 65 6e 64 69 66 0a 53  FILE*);.#endif.S
5c2c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5c2d0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  id sqlite3VdbeRe
5c2e0 73 65 74 53 74 65 70 52 65 73 75 6c 74 28 56 64  setStepResult(Vd
5c2f0 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  be*);.SQLITE_PRI
5c300 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5c310 56 64 62 65 52 65 73 65 74 28 56 64 62 65 2a 29  VdbeReset(Vdbe*)
5c320 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5c330 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
5c340 65 53 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65  eSetNumCols(Vdbe
5c350 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  *,int);.SQLITE_P
5c360 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5c370 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
5c380 28 56 64 62 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  (Vdbe*, int, int
5c390 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20  , const char *, 
5c3a0 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
5c3b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5c3c0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
5c3d0 43 6f 75 6e 74 43 68 61 6e 67 65 73 28 56 64 62  CountChanges(Vdb
5c3e0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
5c3f0 41 54 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c  ATE sqlite3 *sql
5c400 69 74 65 33 56 64 62 65 44 62 28 56 64 62 65 2a  ite3VdbeDb(Vdbe*
5c410 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5c420 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
5c430 62 65 53 65 74 53 71 6c 28 56 64 62 65 2a 2c 20  beSetSql(Vdbe*, 
5c440 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69  const char *z, i
5c450 6e 74 20 6e 29 3b 0a 53 51 4c 49 54 45 5f 50 52  nt n);.SQLITE_PR
5c460 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
5c470 65 33 56 64 62 65 53 77 61 70 28 56 64 62 65 2a  e3VdbeSwap(Vdbe*
5c480 2c 56 64 62 65 2a 29 3b 0a 0a 23 69 66 64 65 66  ,Vdbe*);..#ifdef
5c490 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
5c4a0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
5c4b0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5c4c0 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 52  int sqlite3VdbeR
5c4d0 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e 74  eleaseMemory(int
5c4e0 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45  );.#endif.SQLITE
5c4f0 5f 50 52 49 56 41 54 45 20 55 6e 70 61 63 6b 65  _PRIVATE Unpacke
5c500 64 52 65 63 6f 72 64 20 2a 73 71 6c 69 74 65 33  dRecord *sqlite3
5c510 56 64 62 65 52 65 63 6f 72 64 55 6e 70 61 63 6b  VdbeRecordUnpack
5c520 28 4b 65 79 49 6e 66 6f 2a 2c 69 6e 74 2c 63 6f  (KeyInfo*,int,co
5c530 6e 73 74 20 76 6f 69 64 2a 2c 0a 20 20 20 20 20  nst void*,.     
5c540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5c560 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72     UnpackedRecor
5c570 64 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  d*,int);.SQLITE_
5c580 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5c590 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e  ite3VdbeDeleteUn
5c5a0 70 61 63 6b 65 64 52 65 63 6f 72 64 28 55 6e 70  packedRecord(Unp
5c5b0 61 63 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 53  ackedRecord*);.S
5c5c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5c5d0 74 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  t sqlite3VdbeRec
5c5e0 6f 72 64 43 6f 6d 70 61 72 65 28 69 6e 74 2c 63  ordCompare(int,c
5c5f0 6f 6e 73 74 20 76 6f 69 64 2a 2c 55 6e 70 61 63  onst void*,Unpac
5c600 6b 65 64 52 65 63 6f 72 64 2a 29 3b 0a 0a 0a 23  kedRecord*);...#
5c610 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 53 51  ifndef NDEBUG.SQ
5c620 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
5c630 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43  oid sqlite3VdbeC
5c640 6f 6d 6d 65 6e 74 28 56 64 62 65 2a 2c 20 63 6f  omment(Vdbe*, co
5c650 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b  nst char*, ...);
5c660 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43 6f  .# define VdbeCo
5c670 6d 6d 65 6e 74 28 58 29 20 20 73 71 6c 69 74 65  mment(X)  sqlite
5c680 33 56 64 62 65 43 6f 6d 6d 65 6e 74 20 58 0a 53  3VdbeComment X.S
5c690 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20  QLITE_PRIVATE   
5c6a0 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
5c6b0 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65  NoopComment(Vdbe
5c6c0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20  *, const char*, 
5c6d0 2e 2e 2e 29 3b 0a 23 20 64 65 66 69 6e 65 20 56  ...);.# define V
5c6e0 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 58  dbeNoopComment(X
5c6f0 29 20 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f  )  sqlite3VdbeNo
5c700 6f 70 43 6f 6d 6d 65 6e 74 20 58 0a 23 65 6c 73  opComment X.#els
5c710 65 0a 23 20 64 65 66 69 6e 65 20 56 64 62 65 43  e.# define VdbeC
5c720 6f 6d 6d 65 6e 74 28 58 29 0a 23 20 64 65 66 69  omment(X).# defi
5c730 6e 65 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65  ne VdbeNoopComme
5c740 6e 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 23 65  nt(X).#endif..#e
5c750 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ndif../*********
5c760 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
5c770 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
5c780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c7a0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5c7b0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
5c7c0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
5c7d0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
5c7e0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
5c7f0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5c800 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 61  ***** Include pa
5c810 67 65 72 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ger.h in the mid
5c820 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74  dle of sqliteInt
5c830 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
5c840 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5c850 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
5c860 20 70 61 67 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a   pager.h *******
5c870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c890 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
5c8a0 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
5c8b0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
5c8c0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
5c8d0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
5c8e0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
5c8f0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
5c900 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
5c910 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
5c920 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
5c930 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
5c940 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
5c950 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
5c960 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
5c970 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
5c980 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
5c990 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
5c9a0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
5c9b0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
5c9c0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
5c9d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c9e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5c9f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5ca00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
5ca10 54 68 69 73 20 68 65 61 64 65 72 20 66 69 6c 65  This header file
5ca20 20 64 65 66 69 6e 65 73 20 74 68 65 20 69 6e 74   defines the int
5ca30 65 72 66 61 63 65 20 74 68 61 74 20 74 68 65 20  erface that the 
5ca40 73 71 6c 69 74 65 20 70 61 67 65 20 63 61 63 68  sqlite page cach
5ca50 65 0a 2a 2a 20 73 75 62 73 79 73 74 65 6d 2e 20  e.** subsystem. 
5ca60 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
5ca70 73 75 62 73 79 73 74 65 6d 20 72 65 61 64 73 20  subsystem reads 
5ca80 61 6e 64 20 77 72 69 74 65 73 20 61 20 66 69 6c  and writes a fil
5ca90 65 20 61 20 70 61 67 65 0a 2a 2a 20 61 74 20 61  e a page.** at a
5caa0 20 74 69 6d 65 20 61 6e 64 20 70 72 6f 76 69 64   time and provid
5cab0 65 73 20 61 20 6a 6f 75 72 6e 61 6c 20 66 6f 72  es a journal for
5cac0 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
5cad0 20 40 28 23 29 20 24 49 64 3a 20 70 61 67 65 72   @(#) $Id: pager
5cae0 2e 68 2c 76 20 31 2e 38 37 20 32 30 30 38 2f 31  .h,v 1.87 2008/1
5caf0 31 2f 31 39 20 31 30 3a 32 32 3a 33 33 20 64 61  1/19 10:22:33 da
5cb00 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
5cb10 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 5f 50 41 47  */..#ifndef _PAG
5cb20 45 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 50  ER_H_.#define _P
5cb30 41 47 45 52 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 49  AGER_H_../*.** I
5cb40 66 20 64 65 66 69 6e 65 64 20 61 73 20 6e 6f 6e  f defined as non
5cb50 2d 7a 65 72 6f 2c 20 61 75 74 6f 2d 76 61 63 75  -zero, auto-vacu
5cb60 75 6d 20 69 73 20 65 6e 61 62 6c 65 64 20 62 79  um is enabled by
5cb70 20 64 65 66 61 75 6c 74 2e 20 4f 74 68 65 72 77   default. Otherw
5cb80 69 73 65 0a 2a 2a 20 69 74 20 6d 75 73 74 20 62  ise.** it must b
5cb90 65 20 74 75 72 6e 65 64 20 6f 6e 20 66 6f 72 20  e turned on for 
5cba0 65 61 63 68 20 64 61 74 61 62 61 73 65 20 75 73  each database us
5cbb0 69 6e 67 20 22 50 52 41 47 4d 41 20 61 75 74 6f  ing "PRAGMA auto
5cbc0 5f 76 61 63 75 75 6d 20 3d 20 31 22 2e 0a 2a 2f  _vacuum = 1"..*/
5cbd0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
5cbe0 44 45 46 41 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f  DEFAULT_JOURNAL_
5cbf0 53 49 5a 45 5f 4c 49 4d 49 54 0a 20 20 23 64 65  SIZE_LIMIT.  #de
5cc00 66 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41  fine SQLITE_DEFA
5cc10 55 4c 54 5f 4a 4f 55 52 4e 41 4c 5f 53 49 5a 45  ULT_JOURNAL_SIZE
5cc20 5f 4c 49 4d 49 54 20 2d 31 0a 23 65 6e 64 69 66  _LIMIT -1.#endif
5cc30 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 79 70 65  ../*.** The type
5cc40 20 75 73 65 64 20 74 6f 20 72 65 70 72 65 73 65   used to represe
5cc50 6e 74 20 61 20 70 61 67 65 20 6e 75 6d 62 65 72  nt a page number
5cc60 2e 20 20 54 68 65 20 66 69 72 73 74 20 70 61 67  .  The first pag
5cc70 65 20 69 6e 20 61 20 66 69 6c 65 0a 2a 2a 20 69  e in a file.** i
5cc80 73 20 63 61 6c 6c 65 64 20 70 61 67 65 20 31 2e  s called page 1.
5cc90 20 20 30 20 69 73 20 75 73 65 64 20 74 6f 20 72    0 is used to r
5cca0 65 70 72 65 73 65 6e 74 20 22 6e 6f 74 20 61 20  epresent "not a 
5ccb0 70 61 67 65 22 2e 0a 2a 2f 0a 74 79 70 65 64 65  page"..*/.typede
5ccc0 66 20 75 33 32 20 50 67 6e 6f 3b 0a 0a 2f 2a 0a  f u32 Pgno;../*.
5ccd0 2a 2a 20 45 61 63 68 20 6f 70 65 6e 20 66 69 6c  ** Each open fil
5cce0 65 20 69 73 20 6d 61 6e 61 67 65 64 20 62 79 20  e is managed by 
5ccf0 61 20 73 65 70 61 72 61 74 65 20 69 6e 73 74 61  a separate insta
5cd00 6e 63 65 20 6f 66 20 74 68 65 20 22 50 61 67 65  nce of the "Page
5cd10 72 22 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  r" structure..*/
5cd20 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
5cd30 50 61 67 65 72 20 50 61 67 65 72 3b 0a 0a 2f 2a  Pager Pager;../*
5cd40 0a 2a 2a 20 48 61 6e 64 6c 65 20 74 79 70 65 20  .** Handle type 
5cd50 66 6f 72 20 70 61 67 65 73 2e 0a 2a 2f 0a 74 79  for pages..*/.ty
5cd60 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67 48  pedef struct PgH
5cd70 64 72 20 44 62 50 61 67 65 3b 0a 0a 2f 2a 0a 2a  dr DbPage;../*.*
5cd80 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75 65 73  * Allowed values
5cd90 20 66 6f 72 20 74 68 65 20 66 6c 61 67 73 20 70   for the flags p
5cda0 61 72 61 6d 65 74 65 72 20 74 6f 20 73 71 6c 69  arameter to sqli
5cdb0 74 65 33 50 61 67 65 72 4f 70 65 6e 28 29 2e 0a  te3PagerOpen()..
5cdc0 2a 2a 0a 2a 2a 20 4e 4f 54 45 3a 20 54 68 69 73  **.** NOTE: This
5cdd0 20 76 61 6c 75 65 73 20 6d 75 73 74 20 6d 61 74   values must mat
5cde0 63 68 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  ch the correspon
5cdf0 64 69 6e 67 20 42 54 52 45 45 5f 20 76 61 6c 75  ding BTREE_ valu
5ce00 65 73 20 69 6e 20 62 74 72 65 65 2e 68 2e 0a 2a  es in btree.h..*
5ce10 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
5ce20 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 20 20 30 78  OMIT_JOURNAL  0x
5ce30 30 30 30 31 20 20 20 20 2f 2a 20 44 6f 20 6e 6f  0001    /* Do no
5ce40 74 20 75 73 65 20 61 20 72 6f 6c 6c 62 61 63 6b  t use a rollback
5ce50 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23 64 65 66   journal */.#def
5ce60 69 6e 65 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41  ine PAGER_NO_REA
5ce70 44 4c 4f 43 4b 20 20 20 30 78 30 30 30 32 20 20  DLOCK   0x0002  
5ce80 20 20 2f 2a 20 4f 6d 69 74 20 72 65 61 64 6c 6f    /* Omit readlo
5ce90 63 6b 73 20 6f 6e 20 72 65 61 64 6f 6e 6c 79 20  cks on readonly 
5cea0 66 69 6c 65 73 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  files */../*.** 
5ceb0 56 61 6c 69 64 20 76 61 6c 75 65 73 20 66 6f 72  Valid values for
5cec0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
5ced0 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74 65 33 50  ment to sqlite3P
5cee0 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28  agerLockingMode(
5cef0 29 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  )..*/.#define PA
5cf00 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
5cf10 51 55 45 52 59 20 20 20 20 20 20 2d 31 0a 23 64  QUERY      -1.#d
5cf20 65 66 69 6e 65 20 50 41 47 45 52 5f 4c 4f 43 4b  efine PAGER_LOCK
5cf30 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 20 20  INGMODE_NORMAL  
5cf40 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 50 41      0.#define PA
5cf50 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
5cf60 45 58 43 4c 55 53 49 56 45 20 20 20 31 0a 0a 2f  EXCLUSIVE   1../
5cf70 2a 0a 2a 2a 20 56 61 6c 69 64 20 76 61 6c 75 65  *.** Valid value
5cf80 73 20 66 6f 72 20 74 68 65 20 73 65 63 6f 6e 64  s for the second
5cf90 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c   argument to sql
5cfa0 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c  ite3PagerJournal
5cfb0 4d 6f 64 65 28 29 2e 0a 2a 2f 0a 23 64 65 66 69  Mode()..*/.#defi
5cfc0 6e 65 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  ne PAGER_JOURNAL
5cfd0 4d 4f 44 45 5f 51 55 45 52 59 20 20 20 20 20 20  MODE_QUERY      
5cfe0 2d 31 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  -1.#define PAGER
5cff0 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c  _JOURNALMODE_DEL
5d000 45 54 45 20 20 20 20 20 20 30 20 20 20 2f 2a 20  ETE      0   /* 
5d010 43 6f 6d 6d 69 74 20 62 79 20 64 65 6c 65 74 69  Commit by deleti
5d020 6e 67 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  ng journal file 
5d030 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
5d040 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 50 45 52  _JOURNALMODE_PER
5d050 53 49 53 54 20 20 20 20 20 31 20 20 20 2f 2a 20  SIST     1   /* 
5d060 43 6f 6d 6d 69 74 20 62 79 20 7a 65 72 6f 69 6e  Commit by zeroin
5d070 67 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72  g journal header
5d080 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
5d090 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46  R_JOURNALMODE_OF
5d0a0 46 20 20 20 20 20 20 20 20 20 32 20 20 20 2f 2a  F         2   /*
5d0b0 20 4a 6f 75 72 6e 61 6c 20 6f 6d 69 74 74 65 64   Journal omitted
5d0c0 2e 20 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41  .  */.#define PA
5d0d0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
5d0e0 54 52 55 4e 43 41 54 45 20 20 20 20 33 20 20 20  TRUNCATE    3   
5d0f0 2f 2a 20 43 6f 6d 6d 69 74 20 62 79 20 74 72 75  /* Commit by tru
5d100 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 20  ncating journal 
5d110 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52  */.#define PAGER
5d120 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d  _JOURNALMODE_MEM
5d130 4f 52 59 20 20 20 20 20 20 34 20 20 20 2f 2a 20  ORY      4   /* 
5d140 49 6e 2d 6d 65 6d 6f 72 79 20 6a 6f 75 72 6e 61  In-memory journa
5d150 6c 20 66 69 6c 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  l file */../*.**
5d160 20 53 65 65 20 73 6f 75 72 63 65 20 63 6f 64 65   See source code
5d170 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 61 20   comments for a 
5d180 64 65 74 61 69 6c 65 64 20 64 65 73 63 72 69 70  detailed descrip
5d190 74 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 6c 6c  tion of the foll
5d1a0 6f 77 69 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65  owing.** routine
5d1b0 73 3a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s:.*/.SQLITE_PRI
5d1c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5d1d0 50 61 67 65 72 4f 70 65 6e 28 73 71 6c 69 74 65  PagerOpen(sqlite
5d1e0 33 5f 76 66 73 20 2a 2c 20 50 61 67 65 72 20 2a  3_vfs *, Pager *
5d1f0 2a 70 70 50 61 67 65 72 2c 20 63 6f 6e 73 74 20  *ppPager, const 
5d200 63 68 61 72 2a 2c 20 69 6e 74 2c 69 6e 74 2c 69  char*, int,int,i
5d210 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
5d220 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5d230 50 61 67 65 72 53 65 74 42 75 73 79 68 61 6e 64  PagerSetBusyhand
5d240 6c 65 72 28 50 61 67 65 72 2a 2c 20 69 6e 74 28  ler(Pager*, int(
5d250 2a 29 28 76 6f 69 64 20 2a 29 2c 20 76 6f 69 64  *)(void *), void
5d260 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
5d270 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5d280 50 61 67 65 72 53 65 74 52 65 69 6e 69 74 65 72  PagerSetReiniter
5d290 28 50 61 67 65 72 2a 2c 20 76 6f 69 64 28 2a 29  (Pager*, void(*)
5d2a0 28 44 62 50 61 67 65 2a 29 29 3b 0a 53 51 4c 49  (DbPage*));.SQLI
5d2b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d2c0 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 50 61  qlite3PagerSetPa
5d2d0 67 65 73 69 7a 65 28 50 61 67 65 72 2a 2c 20 75  gesize(Pager*, u
5d2e0 31 36 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  16*);.SQLITE_PRI
5d2f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5d300 50 61 67 65 72 4d 61 78 50 61 67 65 43 6f 75 6e  PagerMaxPageCoun
5d310 74 28 50 61 67 65 72 2a 2c 20 69 6e 74 29 3b 0a  t(Pager*, int);.
5d320 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d330 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  nt sqlite3PagerR
5d340 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61  eadFileheader(Pa
5d350 67 65 72 2a 2c 20 69 6e 74 2c 20 75 6e 73 69 67  ger*, int, unsig
5d360 6e 65 64 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  ned char*);.SQLI
5d370 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5d380 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74 43  sqlite3PagerSetC
5d390 61 63 68 65 73 69 7a 65 28 50 61 67 65 72 2a 2c  achesize(Pager*,
5d3a0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
5d3b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5d3c0 33 50 61 67 65 72 43 6c 6f 73 65 28 50 61 67 65  3PagerClose(Page
5d3d0 72 20 2a 70 50 61 67 65 72 29 3b 0a 53 51 4c 49  r *pPager);.SQLI
5d3e0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d3f0 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69  qlite3PagerAcqui
5d400 72 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  re(Pager *pPager
5d410 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 44 62 50  , Pgno pgno, DbP
5d420 61 67 65 20 2a 2a 70 70 50 61 67 65 2c 20 69 6e  age **ppPage, in
5d430 74 20 63 6c 72 46 6c 61 67 29 3b 0a 23 64 65 66  t clrFlag);.#def
5d440 69 6e 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  ine sqlite3Pager
5d450 47 65 74 28 41 2c 42 2c 43 29 20 73 71 6c 69 74  Get(A,B,C) sqlit
5d460 65 33 50 61 67 65 72 41 63 71 75 69 72 65 28 41  e3PagerAcquire(A
5d470 2c 42 2c 43 2c 30 29 0a 53 51 4c 49 54 45 5f 50  ,B,C,0).SQLITE_P
5d480 52 49 56 41 54 45 20 44 62 50 61 67 65 20 2a 73  RIVATE DbPage *s
5d490 71 6c 69 74 65 33 50 61 67 65 72 4c 6f 6f 6b 75  qlite3PagerLooku
5d4a0 70 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  p(Pager *pPager,
5d4b0 20 50 67 6e 6f 20 70 67 6e 6f 29 3b 0a 53 51 4c   Pgno pgno);.SQL
5d4c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5d4d0 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
5d4e0 52 65 66 63 6f 75 6e 74 28 44 62 50 61 67 65 2a  Refcount(DbPage*
5d4f0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5d500 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
5d510 65 72 52 65 66 28 44 62 50 61 67 65 2a 29 3b 0a  erRef(DbPage*);.
5d520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
5d530 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 55  nt sqlite3PagerU
5d540 6e 72 65 66 28 44 62 50 61 67 65 2a 29 3b 0a 53  nref(DbPage*);.S
5d550 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5d560 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  t sqlite3PagerWr
5d570 69 74 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51  ite(DbPage*);.SQ
5d580 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5d590 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
5d5a0 65 63 6f 75 6e 74 28 50 61 67 65 72 2a 2c 20 69  ecount(Pager*, i
5d5b0 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
5d5c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
5d5d0 50 61 67 65 72 54 72 75 6e 63 61 74 65 28 50 61  PagerTruncate(Pa
5d5e0 67 65 72 2a 2c 50 67 6e 6f 29 3b 0a 53 51 4c 49  ger*,Pgno);.SQLI
5d5f0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d600 71 6c 69 74 65 33 50 61 67 65 72 42 65 67 69 6e  qlite3PagerBegin
5d610 28 44 62 50 61 67 65 2a 2c 20 69 6e 74 20 65 78  (DbPage*, int ex
5d620 46 6c 61 67 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Flag);.SQLITE_PR
5d630 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5d640 33 50 61 67 65 72 43 6f 6d 6d 69 74 50 68 61 73  3PagerCommitPhas
5d650 65 4f 6e 65 28 50 61 67 65 72 2a 2c 63 6f 6e 73  eOne(Pager*,cons
5d660 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 2c  t char *zMaster,
5d670 20 50 67 6e 6f 2c 20 69 6e 74 29 3b 0a 53 51 4c   Pgno, int);.SQL
5d680 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5d690 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
5d6a0 69 74 50 68 61 73 65 54 77 6f 28 50 61 67 65 72  itPhaseTwo(Pager
5d6b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5d6c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  TE int sqlite3Pa
5d6d0 67 65 72 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65  gerRollback(Page
5d6e0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
5d6f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
5d700 61 67 65 72 49 73 72 65 61 64 6f 6e 6c 79 28 50  agerIsreadonly(P
5d710 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
5d720 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5d730 65 33 50 61 67 65 72 53 74 6d 74 42 65 67 69 6e  e3PagerStmtBegin
5d740 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45  (Pager*);.SQLITE
5d750 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
5d760 69 74 65 33 50 61 67 65 72 53 74 6d 74 43 6f 6d  ite3PagerStmtCom
5d770 6d 69 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  mit(Pager*);.SQL
5d780 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
5d790 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74  sqlite3PagerStmt
5d7a0 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 2a 29  Rollback(Pager*)
5d7b0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5d7c0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
5d7d0 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 44  erDontRollback(D
5d7e0 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49 54 45 5f  bPage*);.SQLITE_
5d7f0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5d800 74 65 33 50 61 67 65 72 44 6f 6e 74 57 72 69 74  te3PagerDontWrit
5d810 65 28 44 62 50 61 67 65 2a 29 3b 0a 53 51 4c 49  e(DbPage*);.SQLI
5d820 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d830 71 6c 69 74 65 33 50 61 67 65 72 52 65 66 63 6f  qlite3PagerRefco
5d840 75 6e 74 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c  unt(Pager*);.SQL
5d850 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5d860 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
5d870 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65  SafetyLevel(Page
5d880 72 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c  r*,int,int);.SQL
5d890 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
5d8a0 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 50  t char *sqlite3P
5d8b0 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 50 61 67  agerFilename(Pag
5d8c0 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  er*);.SQLITE_PRI
5d8d0 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74  VATE const sqlit
5d8e0 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50  e3_vfs *sqlite3P
5d8f0 61 67 65 72 56 66 73 28 50 61 67 65 72 2a 29 3b  agerVfs(Pager*);
5d900 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5d910 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 73 71  sqlite3_file *sq
5d920 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 28 50  lite3PagerFile(P
5d930 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  ager*);.SQLITE_P
5d940 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
5d950 72 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 44  r *sqlite3PagerD
5d960 69 72 6e 61 6d 65 28 50 61 67 65 72 2a 29 3b 0a  irname(Pager*);.
5d970 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
5d980 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
5d990 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 6e 61  e3PagerJournalna
5d9a0 6d 65 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49  me(Pager*);.SQLI
5d9b0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5d9c0 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79 6e  qlite3PagerNosyn
5d9d0 63 28 50 61 67 65 72 2a 29 3b 0a 53 51 4c 49 54  c(Pager*);.SQLIT
5d9e0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
5d9f0 6c 69 74 65 33 50 61 67 65 72 4d 6f 76 65 70 61  lite3PagerMovepa
5da00 67 65 28 50 61 67 65 72 2a 2c 44 62 50 61 67 65  ge(Pager*,DbPage
5da10 2a 2c 50 67 6e 6f 2c 69 6e 74 29 3b 0a 53 51 4c  *,Pgno,int);.SQL
5da20 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
5da30 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65   *sqlite3PagerGe
5da40 74 44 61 74 61 28 44 62 50 61 67 65 20 2a 29 3b  tData(DbPage *);
5da50 20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45   .SQLITE_PRIVATE
5da60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
5da70 67 65 72 47 65 74 45 78 74 72 61 28 44 62 50 61  gerGetExtra(DbPa
5da80 67 65 20 2a 29 3b 20 0a 53 51 4c 49 54 45 5f 50  ge *); .SQLITE_P
5da90 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5daa0 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e 67 4d 6f  e3PagerLockingMo
5dab0 64 65 28 50 61 67 65 72 20 2a 2c 20 69 6e 74 29  de(Pager *, int)
5dac0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
5dad0 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
5dae0 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 50 61 67  rJournalMode(Pag
5daf0 65 72 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  er *, int);.SQLI
5db00 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20 73  TE_PRIVATE i64 s
5db10 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72 6e  qlite3PagerJourn
5db20 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50 61 67 65  alSizeLimit(Page
5db30 72 20 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  r *, i64);.SQLIT
5db40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
5db50 73 71 6c 69 74 65 33 50 61 67 65 72 54 65 6d 70  sqlite3PagerTemp
5db60 53 70 61 63 65 28 50 61 67 65 72 2a 29 3b 0a 53  Space(Pager*);.S
5db70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
5db80 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  t sqlite3PagerSy
5db90 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
5dba0 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
5dbb0 45 5f 48 41 53 5f 43 4f 44 45 43 0a 53 51 4c 49  E_HAS_CODEC.SQLI
5dbc0 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
5dbd0 64 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  d sqlite3PagerSe
5dbe0 74 43 6f 64 65 63 28 50 61 67 65 72 2a 2c 76 6f  tCodec(Pager*,vo
5dbf0 69 64 2a 28 2a 29 28 76 6f 69 64 2a 2c 76 6f 69  id*(*)(void*,voi
5dc00 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c 76 6f 69  d*,Pgno,int),voi
5dc10 64 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  d*);.#endif..#if
5dc20 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
5dc30 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
5dc40 49 54 45 5f 54 45 53 54 29 0a 53 51 4c 49 54 45  ITE_TEST).SQLITE
5dc50 5f 50 52 49 56 41 54 45 20 20 20 50 67 6e 6f 20  _PRIVATE   Pgno 
5dc60 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
5dc70 6e 75 6d 62 65 72 28 44 62 50 61 67 65 2a 29 3b  number(DbPage*);
5dc80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5dc90 20 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67    int sqlite3Pag
5dca0 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 44 62  erIswriteable(Db
5dcb0 50 61 67 65 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a  Page*);.#endif..
5dcc0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
5dcd0 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ST.SQLITE_PRIVAT
5dce0 45 20 20 20 69 6e 74 20 2a 73 71 6c 69 74 65 33  E   int *sqlite3
5dcf0 50 61 67 65 72 53 74 61 74 73 28 50 61 67 65 72  PagerStats(Pager
5dd00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
5dd10 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
5dd20 33 50 61 67 65 72 52 65 66 64 75 6d 70 28 50 61  3PagerRefdump(Pa
5dd30 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ger*);.SQLITE_PR
5dd40 49 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69  IVATE   int sqli
5dd50 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
5dd60 50 61 67 65 72 2a 29 3b 0a 23 65 6e 64 69 66 0a  Pager*);.#endif.
5dd70 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
5dd80 45 53 54 0a 76 6f 69 64 20 64 69 73 61 62 6c 65  EST.void disable
5dd90 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72  _simulated_io_er
5dda0 72 6f 72 73 28 76 6f 69 64 29 3b 0a 76 6f 69 64  rors(void);.void
5ddb0 20 65 6e 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65   enable_simulate
5ddc0 64 5f 69 6f 5f 65 72 72 6f 72 73 28 76 6f 69 64  d_io_errors(void
5ddd0 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  );.#else.# defin
5dde0 65 20 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61  e disable_simula
5ddf0 74 65 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 0a  ted_io_errors().
5de00 23 20 64 65 66 69 6e 65 20 65 6e 61 62 6c 65 5f  # define enable_
5de10 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
5de20 6f 72 73 28 29 0a 23 65 6e 64 69 66 0a 0a 23 65  ors().#endif..#e
5de30 6e 64 69 66 20 2f 2a 20 5f 50 41 47 45 52 5f 48  ndif /* _PAGER_H
5de40 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
5de50 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 61 67  ***** End of pag
5de60 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
5de70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5de80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5de90 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5dea0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
5deb0 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
5dec0 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
5ded0 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
5dee0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5def0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 70 63  ***** Include pc
5df00 61 63 68 65 2e 68 20 69 6e 20 74 68 65 20 6d 69  ache.h in the mi
5df10 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49 6e  ddle of sqliteIn
5df20 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
5df30 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5df40 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
5df50 20 70 63 61 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a   pcache.h ******
5df60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5df70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5df80 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38  ****/./*.** 2008
5df90 20 41 75 67 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a   August 05.**.**
5dfa0 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
5dfb0 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
5dfc0 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
5dfd0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
5dfe0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
5dff0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
5e000 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
5e010 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
5e020 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
5e030 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
5e040 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
5e050 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
5e060 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
5e070 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
5e080 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
5e090 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
5e0a0 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
5e0b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e0c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e0e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e0f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
5e100 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 64 65  s header file de
5e110 66 69 6e 65 73 20 74 68 65 20 69 6e 74 65 72 66  fines the interf
5e120 61 63 65 20 74 68 61 74 20 74 68 65 20 73 71 6c  ace that the sql
5e130 69 74 65 20 70 61 67 65 20 63 61 63 68 65 0a 2a  ite page cache.*
5e140 2a 20 73 75 62 73 79 73 74 65 6d 2e 20 0a 2a 2a  * subsystem. .**
5e150 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70 63  .** @(#) $Id: pc
5e160 61 63 68 65 2e 68 2c 76 20 31 2e 31 36 20 32 30  ache.h,v 1.16 20
5e170 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 34  08/11/19 16:52:4
5e180 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  4 danielk1977 Ex
5e190 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
5e1a0 5f 50 43 41 43 48 45 5f 48 5f 0a 0a 74 79 70 65  _PCACHE_H_..type
5e1b0 64 65 66 20 73 74 72 75 63 74 20 50 67 48 64 72  def struct PgHdr
5e1c0 20 50 67 48 64 72 3b 0a 74 79 70 65 64 65 66 20   PgHdr;.typedef 
5e1d0 73 74 72 75 63 74 20 50 43 61 63 68 65 20 50 43  struct PCache PC
5e1e0 61 63 68 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 76 65  ache;../*.** Eve
5e1f0 72 79 20 70 61 67 65 20 69 6e 20 74 68 65 20 63  ry page in the c
5e200 61 63 68 65 20 69 73 20 63 6f 6e 74 72 6f 6c 6c  ache is controll
5e210 65 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ed by an instanc
5e220 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
5e230 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e  ng.** structure.
5e240 0a 2a 2f 0a 73 74 72 75 63 74 20 50 67 48 64 72  .*/.struct PgHdr
5e250 20 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61   {.  void *pData
5e260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
5e270 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f      /* Content o
5e280 66 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20  f this page */. 
5e290 20 76 6f 69 64 20 2a 70 45 78 74 72 61 3b 20 20   void *pExtra;  
5e2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e2b0 2f 2a 20 45 78 74 72 61 20 63 6f 6e 74 65 6e 74  /* Extra content
5e2c0 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a 70 44 69   */.  PgHdr *pDi
5e2d0 72 74 79 3b 20 20 20 20 20 20 20 20 20 20 20 20  rty;            
5e2e0 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 69 65 6e       /* Transien
5e2f0 74 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  t list of dirty 
5e300 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  pages */.  Pgno 
5e310 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
5e320 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67            /* Pag
5e330 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 69  e number for thi
5e340 73 20 70 61 67 65 20 2a 2f 0a 20 20 50 61 67 65  s page */.  Page
5e350 72 20 2a 70 50 61 67 65 72 3b 20 20 20 20 20 20  r *pPager;      
5e360 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
5e370 65 20 70 61 67 65 72 20 74 68 69 73 20 70 61 67  e pager this pag
5e380 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f 0a  e is part of */.
5e390 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
5e3a0 45 43 4b 5f 50 41 47 45 53 0a 20 20 75 33 32 20  ECK_PAGES.  u32 
5e3b0 70 61 67 65 48 61 73 68 3b 20 20 20 20 20 20 20  pageHash;       
5e3c0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61             /* Ha
5e3d0 73 68 20 6f 66 20 70 61 67 65 20 63 6f 6e 74 65  sh of page conte
5e3e0 6e 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 75  nt */.#endif.  u
5e3f0 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  16 flags;       
5e400 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5e410 20 50 47 48 44 52 20 66 6c 61 67 73 20 64 65 66   PGHDR flags def
5e420 69 6e 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 0a 20  ined below */.. 
5e430 20 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   /**************
5e440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e450 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e460 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5e470 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2a 20 45 6c  ********.  ** El
5e480 65 6d 65 6e 74 73 20 61 62 6f 76 65 20 61 72 65  ements above are
5e490 20 70 75 62 6c 69 63 2e 20 20 41 6c 6c 20 74 68   public.  All th
5e4a0 61 74 20 66 6f 6c 6c 6f 77 73 20 69 73 20 70 72  at follows is pr
5e4b0 69 76 61 74 65 20 74 6f 20 70 63 61 63 68 65 2e  ivate to pcache.
5e4c0 63 0a 20 20 2a 2a 20 61 6e 64 20 73 68 6f 75 6c  c.  ** and shoul
5e4d0 64 20 6e 6f 74 20 62 65 20 61 63 63 65 73 73 65  d not be accesse
5e4e0 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64 75 6c  d by other modul
5e4f0 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 31 36 20 6e  es..  */.  i16 n
5e500 52 65 66 3b 20 20 20 20 20 20 20 20 20 20 20 20  Ref;            
5e510 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
5e520 62 65 72 20 6f 66 20 75 73 65 72 73 20 6f 66 20  ber of users of 
5e530 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 50  this page */.  P
5e540 43 61 63 68 65 20 2a 70 43 61 63 68 65 3b 20 20  Cache *pCache;  
5e550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5e560 20 43 61 63 68 65 20 74 68 61 74 20 6f 77 6e 73   Cache that owns
5e570 20 74 68 69 73 20 70 61 67 65 20 2a 2f 0a 0a 20   this page */.. 
5e580 20 50 67 48 64 72 20 2a 70 44 69 72 74 79 4e 65   PgHdr *pDirtyNe
5e590 78 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  xt;             
5e5a0 2f 2a 20 4e 65 78 74 20 65 6c 65 6d 65 6e 74 20  /* Next element 
5e5b0 69 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79  in list of dirty
5e5c0 20 70 61 67 65 73 20 2a 2f 0a 20 20 50 67 48 64   pages */.  PgHd
5e5d0 72 20 2a 70 44 69 72 74 79 50 72 65 76 3b 20 20  r *pDirtyPrev;  
5e5e0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72             /* Pr
5e5f0 65 76 69 6f 75 73 20 65 6c 65 6d 65 6e 74 20 69  evious element i
5e600 6e 20 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20  n list of dirty 
5e610 70 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 20  pages */.};../* 
5e620 42 69 74 20 76 61 6c 75 65 73 20 66 6f 72 20 50  Bit values for P
5e630 67 48 64 72 2e 66 6c 61 67 73 20 2a 2f 0a 23 64  gHdr.flags */.#d
5e640 65 66 69 6e 65 20 50 47 48 44 52 5f 44 49 52 54  efine PGHDR_DIRT
5e650 59 20 20 20 20 20 20 20 20 20 20 20 20 20 30 78  Y             0x
5e660 30 30 32 20 20 2f 2a 20 50 61 67 65 20 68 61 73  002  /* Page has
5e670 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 64 65 66   changed */.#def
5e680 69 6e 65 20 50 47 48 44 52 5f 4e 45 45 44 5f 53  ine PGHDR_NEED_S
5e690 59 4e 43 20 20 20 20 20 20 20 20 20 30 78 30 30  YNC         0x00
5e6a0 34 20 20 2f 2a 20 46 73 79 6e 63 20 74 68 65 20  4  /* Fsync the 
5e6b0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
5e6c0 20 62 65 66 6f 72 65 0a 20 20 20 20 20 20 20 20   before.        
5e6d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5e6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a                 *
5e6f0 2a 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 70  * writing this p
5e700 61 67 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  age to the datab
5e710 61 73 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50  ase */.#define P
5e720 47 48 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 20  GHDR_NEED_READ  
5e730 20 20 20 20 20 20 20 30 78 30 30 38 20 20 2f 2a         0x008  /*
5e740 20 43 6f 6e 74 65 6e 74 20 69 73 20 75 6e 72 65   Content is unre
5e750 61 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 47  ad */.#define PG
5e760 48 44 52 5f 52 45 55 53 45 5f 55 4e 4c 49 4b 45  HDR_REUSE_UNLIKE
5e770 4c 59 20 20 20 20 30 78 30 31 30 20 20 2f 2a 20  LY    0x010  /* 
5e780 41 20 68 69 6e 74 20 74 68 61 74 20 72 65 75 73  A hint that reus
5e790 65 20 69 73 20 75 6e 6c 69 6b 65 6c 79 20 2a 2f  e is unlikely */
5e7a0 0a 23 64 65 66 69 6e 65 20 50 47 48 44 52 5f 44  .#define PGHDR_D
5e7b0 4f 4e 54 5f 57 52 49 54 45 20 20 20 20 20 20 20  ONT_WRITE       
5e7c0 20 30 78 30 32 30 20 20 2f 2a 20 44 6f 20 6e 6f   0x020  /* Do no
5e7d0 74 20 77 72 69 74 65 20 63 6f 6e 74 65 6e 74 20  t write content 
5e7e0 74 6f 20 64 69 73 6b 20 2a 2f 0a 0a 2f 2a 20 49  to disk */../* I
5e7f0 6e 69 74 69 61 6c 69 7a 65 20 61 6e 64 20 73 68  nitialize and sh
5e800 75 74 64 6f 77 6e 20 74 68 65 20 70 61 67 65 20  utdown the page 
5e810 63 61 63 68 65 20 73 75 62 73 79 73 74 65 6d 20  cache subsystem 
5e820 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
5e830 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
5e840 63 68 65 49 6e 69 74 69 61 6c 69 7a 65 28 76 6f  cheInitialize(vo
5e850 69 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  id);.SQLITE_PRIV
5e860 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5e870 50 63 61 63 68 65 53 68 75 74 64 6f 77 6e 28 76  PcacheShutdown(v
5e880 6f 69 64 29 3b 0a 0a 2f 2a 20 50 61 67 65 20 63  oid);../* Page c
5e890 61 63 68 65 20 62 75 66 66 65 72 20 6d 61 6e 61  ache buffer mana
5e8a0 67 65 6d 65 6e 74 3a 0a 2a 2a 20 54 68 65 73 65  gement:.** These
5e8b0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d   routines implem
5e8c0 65 6e 74 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49  ent SQLITE_CONFI
5e8d0 47 5f 50 41 47 45 43 41 43 48 45 2e 0a 2a 2f 0a  G_PAGECACHE..*/.
5e8e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5e8f0 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61 63 68  oid sqlite3PCach
5e900 65 42 75 66 66 65 72 53 65 74 75 70 28 76 6f 69  eBufferSetup(voi
5e910 64 20 2a 2c 20 69 6e 74 20 73 7a 2c 20 69 6e 74  d *, int sz, int
5e920 20 6e 29 3b 0a 0a 2f 2a 20 43 72 65 61 74 65 20   n);../* Create 
5e930 61 20 6e 65 77 20 70 61 67 65 72 20 63 61 63 68  a new pager cach
5e940 65 2e 0a 2a 2a 20 55 6e 64 65 72 20 6d 65 6d 6f  e..** Under memo
5e950 72 79 20 73 74 72 65 73 73 2c 20 69 6e 76 6f 6b  ry stress, invok
5e960 65 20 78 53 74 72 65 73 73 20 74 6f 20 74 72 79  e xStress to try
5e970 20 74 6f 20 6d 61 6b 65 20 70 61 67 65 73 20 63   to make pages c
5e980 6c 65 61 6e 2e 0a 2a 2a 20 4f 6e 6c 79 20 63 6c  lean..** Only cl
5e990 65 61 6e 20 61 6e 64 20 75 6e 70 69 6e 6e 65 64  ean and unpinned
5e9a0 20 70 61 67 65 73 20 63 61 6e 20 62 65 20 72 65   pages can be re
5e9b0 63 6c 61 69 6d 65 64 2e 0a 2a 2f 0a 53 51 4c 49  claimed..*/.SQLI
5e9c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
5e9d0 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65  sqlite3PcacheOpe
5e9e0 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 2c  n(.  int szPage,
5e9f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
5ea00 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
5ea10 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69  very page */.  i
5ea20 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20  nt szExtra,     
5ea30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
5ea40 20 45 78 74 72 61 20 73 70 61 63 65 20 61 73 73   Extra space ass
5ea50 6f 63 69 61 74 65 64 20 77 69 74 68 20 65 61 63  ociated with eac
5ea60 68 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20  h page */.  int 
5ea70 62 50 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20  bPurgeable,     
5ea80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
5ea90 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20  ue if pages are 
5eaa0 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65  on backing store
5eab0 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72   */.  int (*xStr
5eac0 65 73 73 29 28 76 6f 69 64 2a 2c 20 50 67 48 64  ess)(void*, PgHd
5ead0 72 2a 29 2c 20 2f 2a 20 43 61 6c 6c 20 74 6f 20  r*), /* Call to 
5eae0 74 72 79 20 74 6f 20 6d 61 6b 65 20 70 61 67 65  try to make page
5eaf0 73 20 63 6c 65 61 6e 20 2a 2f 0a 20 20 76 6f 69  s clean */.  voi
5eb00 64 20 2a 70 53 74 72 65 73 73 2c 20 20 20 20 20  d *pStress,     
5eb10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
5eb20 72 67 75 6d 65 6e 74 20 74 6f 20 78 53 74 72 65  rgument to xStre
5eb30 73 73 20 2a 2f 0a 20 20 50 43 61 63 68 65 20 2a  ss */.  PCache *
5eb40 70 54 6f 49 6e 69 74 20 20 20 20 20 20 20 20 20  pToInit         
5eb50 20 20 20 20 20 20 20 2f 2a 20 50 72 65 61 6c 6c         /* Preall
5eb60 6f 63 61 74 65 64 20 73 70 61 63 65 20 66 6f 72  ocated space for
5eb70 20 74 68 65 20 50 43 61 63 68 65 20 2a 2f 0a 29   the PCache */.)
5eb80 3b 0a 0a 2f 2a 20 4d 6f 64 69 66 79 20 74 68 65  ;../* Modify the
5eb90 20 70 61 67 65 2d 73 69 7a 65 20 61 66 74 65 72   page-size after
5eba0 20 74 68 65 20 63 61 63 68 65 20 68 61 73 20 62   the cache has b
5ebb0 65 65 6e 20 63 72 65 61 74 65 64 2e 20 2a 2f 0a  een created. */.
5ebc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5ebd0 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
5ebe0 65 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61  eSetPageSize(PCa
5ebf0 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 0a 2f 2a  che *, int);../*
5ec00 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
5ec10 20 69 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50   in bytes of a P
5ec20 43 61 63 68 65 20 6f 62 6a 65 63 74 2e 20 20 55  Cache object.  U
5ec30 73 65 64 20 74 6f 20 70 72 65 61 6c 6c 6f 63 61  sed to prealloca
5ec40 74 65 0a 2a 2a 20 73 74 6f 72 61 67 65 20 73 70  te.** storage sp
5ec50 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ace..*/.SQLITE_P
5ec60 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
5ec70 65 33 50 63 61 63 68 65 53 69 7a 65 28 76 6f 69  e3PcacheSize(voi
5ec80 64 29 3b 0a 0a 2f 2a 20 4f 6e 65 20 72 65 6c 65  d);../* One rele
5ec90 61 73 65 20 70 65 72 20 73 75 63 63 65 73 73 66  ase per successf
5eca0 75 6c 20 66 65 74 63 68 2e 20 20 50 61 67 65 20  ul fetch.  Page 
5ecb0 69 73 20 70 69 6e 6e 65 64 20 75 6e 74 69 6c 20  is pinned until 
5ecc0 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 20 52 65 66  released..** Ref
5ecd0 65 72 65 6e 63 65 20 63 6f 75 6e 74 65 64 2e 20  erence counted. 
5ece0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
5ecf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
5ed00 61 63 68 65 46 65 74 63 68 28 50 43 61 63 68 65  acheFetch(PCache
5ed10 2a 2c 20 50 67 6e 6f 2c 20 69 6e 74 20 63 72 65  *, Pgno, int cre
5ed20 61 74 65 46 6c 61 67 2c 20 50 67 48 64 72 2a 2a  ateFlag, PgHdr**
5ed30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
5ed40 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63  E void sqlite3Pc
5ed50 61 63 68 65 52 65 6c 65 61 73 65 28 50 67 48 64  acheRelease(PgHd
5ed60 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
5ed70 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5ed80 33 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64  3PcacheDrop(PgHd
5ed90 72 2a 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  r*);         /* 
5eda0 52 65 6d 6f 76 65 20 70 61 67 65 20 66 72 6f 6d  Remove page from
5edb0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
5edc0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5edd0 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
5ede0 69 72 74 79 28 50 67 48 64 72 2a 29 3b 20 20 20  irty(PgHdr*);   
5edf0 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 70 61   /* Make sure pa
5ee00 67 65 20 69 73 20 6d 61 72 6b 65 64 20 64 69 72  ge is marked dir
5ee10 74 79 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  ty */.SQLITE_PRI
5ee20 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
5ee30 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e  3PcacheMakeClean
5ee40 28 50 67 48 64 72 2a 29 3b 20 20 20 20 2f 2a 20  (PgHdr*);    /* 
5ee50 4d 61 72 6b 20 61 20 73 69 6e 67 6c 65 20 70 61  Mark a single pa
5ee60 67 65 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 53  ge as clean */.S
5ee70 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
5ee80 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
5ee90 43 6c 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 2a  CleanAll(PCache*
5eea0 29 3b 20 20 20 20 2f 2a 20 4d 61 72 6b 20 61 6c  );    /* Mark al
5eeb0 6c 20 64 69 72 74 79 20 6c 69 73 74 20 70 61 67  l dirty list pag
5eec0 65 73 20 61 73 20 63 6c 65 61 6e 20 2a 2f 0a 0a  es as clean */..
5eed0 2f 2a 20 43 68 61 6e 67 65 20 61 20 70 61 67 65  /* Change a page
5eee0 20 6e 75 6d 62 65 72 2e 20 20 55 73 65 64 20 62   number.  Used b
5eef0 79 20 69 6e 63 72 2d 76 61 63 75 75 6d 2e 20 2a  y incr-vacuum. *
5ef00 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
5ef10 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61   void sqlite3Pca
5ef20 63 68 65 4d 6f 76 65 28 50 67 48 64 72 2a 2c 20  cheMove(PgHdr*, 
5ef30 50 67 6e 6f 29 3b 0a 0a 2f 2a 20 52 65 6d 6f 76  Pgno);../* Remov
5ef40 65 20 61 6c 6c 20 70 61 67 65 73 20 77 69 74 68  e all pages with
5ef50 20 70 67 6e 6f 3e 78 2e 20 20 52 65 73 65 74 20   pgno>x.  Reset 
5ef60 74 68 65 20 63 61 63 68 65 20 69 66 20 78 3d 3d  the cache if x==
5ef70 30 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  0 */.SQLITE_PRIV
5ef80 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
5ef90 50 63 61 63 68 65 54 72 75 6e 63 61 74 65 28 50  PcacheTruncate(P
5efa0 43 61 63 68 65 2a 2c 20 50 67 6e 6f 20 78 29 3b  Cache*, Pgno x);
5efb0 0a 0a 2f 2a 20 47 65 74 20 61 20 6c 69 73 74 20  ../* Get a list 
5efc0 6f 66 20 61 6c 6c 20 64 69 72 74 79 20 70 61 67  of all dirty pag
5efd0 65 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 2c  es in the cache,
5efe0 20 73 6f 72 74 65 64 20 62 79 20 70 61 67 65 20   sorted by page 
5eff0 6e 75 6d 62 65 72 20 2a 2f 0a 53 51 4c 49 54 45  number */.SQLITE
5f000 5f 50 52 49 56 41 54 45 20 50 67 48 64 72 20 2a  _PRIVATE PgHdr *
5f010 73 71 6c 69 74 65 33 50 63 61 63 68 65 44 69 72  sqlite3PcacheDir
5f020 74 79 4c 69 73 74 28 50 43 61 63 68 65 2a 29 3b  tyList(PCache*);
5f030 0a 0a 2f 2a 20 52 65 73 65 74 20 61 6e 64 20 63  ../* Reset and c
5f040 6c 6f 73 65 20 74 68 65 20 63 61 63 68 65 20 6f  lose the cache o
5f050 62 6a 65 63 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  bject */.SQLITE_
5f060 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5f070 69 74 65 33 50 63 61 63 68 65 43 6c 6f 73 65 28  ite3PcacheClose(
5f080 50 43 61 63 68 65 2a 29 3b 0a 0a 2f 2a 20 43 6c  PCache*);../* Cl
5f090 65 61 72 20 66 6c 61 67 73 20 66 72 6f 6d 20 70  ear flags from p
5f0a0 61 67 65 73 20 6f 66 20 74 68 65 20 70 61 67 65  ages of the page
5f0b0 20 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45   cache */.SQLITE
5f0c0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
5f0d0 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 72  lite3PcacheClear
5f0e0 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63 68 65  SyncFlags(PCache
5f0f0 20 2a 29 3b 0a 0a 2f 2a 20 44 69 73 63 61 72 64   *);../* Discard
5f100 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
5f110 20 74 68 65 20 63 61 63 68 65 20 2a 2f 0a 53 51   the cache */.SQ
5f120 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
5f130 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
5f140 65 61 72 28 50 43 61 63 68 65 2a 29 3b 0a 0a 2f  ear(PCache*);../
5f150 2a 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74  * Return the tot
5f160 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 6f 75 74  al number of out
5f170 73 74 61 6e 64 69 6e 67 20 70 61 67 65 20 72 65  standing page re
5f180 66 65 72 65 6e 63 65 73 20 2a 2f 0a 53 51 4c 49  ferences */.SQLI
5f190 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
5f1a0 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
5f1b0 6f 75 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a  ount(PCache*);..
5f1c0 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  /* Increment the
5f1d0 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
5f1e0 20 6f 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   of an existing 
5f1f0 70 61 67 65 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  page */.SQLITE_P
5f200 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
5f210 74 65 33 50 63 61 63 68 65 52 65 66 28 50 67 48  te3PcacheRef(PgH
5f220 64 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52  dr*);..SQLITE_PR
5f230 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
5f240 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63 6f  3PcachePageRefco
5f250 75 6e 74 28 50 67 48 64 72 2a 29 3b 0a 0a 2f 2a  unt(PgHdr*);../*
5f260 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61   Return the tota
5f270 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
5f280 73 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  s stored in the 
5f290 63 61 63 68 65 20 2a 2f 0a 53 51 4c 49 54 45 5f  cache */.SQLITE_
5f2a0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
5f2b0 74 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75  te3PcachePagecou
5f2c0 6e 74 28 50 43 61 63 68 65 2a 29 3b 0a 0a 23 69  nt(PCache*);..#i
5f2d0 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48 45 43  fdef SQLITE_CHEC
5f2e0 4b 5f 50 41 47 45 53 0a 2f 2a 20 49 74 65 72 61  K_PAGES./* Itera
5f2f0 74 65 20 74 68 72 6f 75 67 68 20 61 6c 6c 20 64  te through all d
5f300 69 72 74 79 20 70 61 67 65 73 20 63 75 72 72 65  irty pages curre
5f310 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e 20 74  ntly stored in t
5f320 68 65 20 63 61 63 68 65 2e 20 54 68 69 73 0a 2a  he cache. This.*
5f330 2a 20 69 6e 74 65 72 66 61 63 65 20 69 73 20 6f  * interface is o
5f340 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66  nly available if
5f350 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
5f360 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 77  GES is defined w
5f370 68 65 6e 20 74 68 65 20 0a 2a 2a 20 6c 69 62 72  hen the .** libr
5f380 61 72 79 20 69 73 20 62 75 69 6c 74 2e 0a 2a 2f  ary is built..*/
5f390 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f3a0 76 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63  void sqlite3Pcac
5f3b0 68 65 49 74 65 72 61 74 65 44 69 72 74 79 28 50  heIterateDirty(P
5f3c0 43 61 63 68 65 20 2a 70 43 61 63 68 65 2c 20 76  Cache *pCache, v
5f3d0 6f 69 64 20 28 2a 78 49 74 65 72 29 28 50 67 48  oid (*xIter)(PgH
5f3e0 64 72 20 2a 29 29 3b 0a 23 65 6e 64 69 66 0a 0a  dr *));.#endif..
5f3f0 2f 2a 20 53 65 74 20 61 6e 64 20 67 65 74 20 74  /* Set and get t
5f400 68 65 20 73 75 67 67 65 73 74 65 64 20 63 61 63  he suggested cac
5f410 68 65 2d 73 69 7a 65 20 66 6f 72 20 74 68 65 20  he-size for the 
5f420 73 70 65 63 69 66 69 65 64 20 70 61 67 65 72 2d  specified pager-
5f430 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  cache..**.** If 
5f440 6e 6f 20 67 6c 6f 62 61 6c 20 6d 61 78 69 6d 75  no global maximu
5f450 6d 20 69 73 20 63 6f 6e 66 69 67 75 72 65 64 2c  m is configured,
5f460 20 74 68 65 6e 20 74 68 65 20 73 79 73 74 65 6d   then the system
5f470 20 61 74 74 65 6d 70 74 73 20 74 6f 20 6c 69 6d   attempts to lim
5f480 69 74 0a 2a 2a 20 74 68 65 20 74 6f 74 61 6c 20  it.** the total 
5f490 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
5f4a0 63 61 63 68 65 64 20 62 79 20 70 75 72 67 65 61  cached by purgea
5f4b0 62 6c 65 20 70 61 67 65 72 2d 63 61 63 68 65 73  ble pager-caches
5f4c0 20 74 6f 20 74 68 65 20 73 75 6d 0a 2a 2a 20 6f   to the sum.** o
5f4d0 66 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20  f the suggested 
5f4e0 63 61 63 68 65 2d 73 69 7a 65 73 2e 0a 2a 2f 0a  cache-sizes..*/.
5f4f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
5f500 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
5f510 65 53 65 74 43 61 63 68 65 73 69 7a 65 28 50 43  eSetCachesize(PC
5f520 61 63 68 65 20 2a 2c 20 69 6e 74 29 3b 0a 23 69  ache *, int);.#i
5f530 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
5f540 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
5f550 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68  int sqlite3Pcach
5f560 65 47 65 74 43 61 63 68 65 73 69 7a 65 28 50 43  eGetCachesize(PC
5f570 61 63 68 65 20 2a 29 3b 0a 23 65 6e 64 69 66 0a  ache *);.#endif.
5f580 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
5f590 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e  NABLE_MEMORY_MAN
5f5a0 41 47 45 4d 45 4e 54 0a 2f 2a 20 54 72 79 20 74  AGEMENT./* Try t
5f5b0 6f 20 72 65 74 75 72 6e 20 6d 65 6d 6f 72 79 20  o return memory 
5f5c0 75 73 65 64 20 62 79 20 74 68 65 20 70 63 61 63  used by the pcac
5f5d0 68 65 20 6d 6f 64 75 6c 65 20 74 6f 20 74 68 65  he module to the
5f5e0 20 6d 61 69 6e 20 6d 65 6d 6f 72 79 20 68 65 61   main memory hea
5f5f0 70 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  p */.SQLITE_PRIV
5f600 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
5f610 63 61 63 68 65 52 65 6c 65 61 73 65 4d 65 6d 6f  cacheReleaseMemo
5f620 72 79 28 69 6e 74 29 3b 0a 23 65 6e 64 69 66 0a  ry(int);.#endif.
5f630 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
5f640 45 53 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  EST.SQLITE_PRIVA
5f650 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
5f660 63 61 63 68 65 53 74 61 74 73 28 69 6e 74 2a 2c  cacheStats(int*,
5f670 69 6e 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b  int*,int*,int*);
5f680 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f  .#endif..SQLITE_
5f690 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
5f6a0 69 74 65 33 50 43 61 63 68 65 53 65 74 44 65 66  ite3PCacheSetDef
5f6b0 61 75 6c 74 28 76 6f 69 64 29 3b 0a 0a 23 65 6e  ault(void);..#en
5f6c0 64 69 66 20 2f 2a 20 5f 50 43 41 43 48 45 5f 48  dif /* _PCACHE_H
5f6d0 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  _ */../*********
5f6e0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 63 61  ***** End of pca
5f6f0 63 68 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  che.h **********
5f700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f720 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
5f730 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
5f740 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
5f750 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ff in sqliteInt.
5f760 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
5f770 2a 2a 2a 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  ****/../********
5f780 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f  ****** Include o
5f790 73 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  s.h in the middl
5f7a0 65 20 6f 66 20 73 71 6c 69 74 65 49 6e 74 2e 68  e of sqliteInt.h
5f7b0 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
5f7c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
5f7d0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
5f7e0 65 20 6f 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  e os.h *********
5f7f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f800 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f810 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
5f820 31 20 53 65 70 74 65 6d 62 65 72 20 31 36 0a 2a  1 September 16.*
5f830 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
5f840 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
5f850 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
5f860 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
5f870 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
5f880 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
5f890 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
5f8a0 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
5f8b0 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
5f8c0 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
5f8d0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
5f8e0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
5f8f0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
5f900 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
5f910 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
5f920 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
5f930 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
5f940 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
5f950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f970 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f980 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
5f990 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  **.**.** This he
5f9a0 61 64 65 72 20 66 69 6c 65 20 28 74 6f 67 65 74  ader file (toget
5f9b0 68 65 72 20 77 69 74 68 20 69 73 20 63 6f 6d 70  her with is comp
5f9c0 61 6e 69 6f 6e 20 43 20 73 6f 75 72 63 65 2d 63  anion C source-c
5f9d0 6f 64 65 20 66 69 6c 65 0a 2a 2a 20 22 6f 73 2e  ode file.** "os.
5f9e0 63 22 29 20 61 74 74 65 6d 70 74 20 74 6f 20 61  c") attempt to a
5f9f0 62 73 74 72 61 63 74 20 74 68 65 20 75 6e 64 65  bstract the unde
5fa00 72 6c 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67  rlying operating
5fa10 20 73 79 73 74 65 6d 20 73 6f 20 74 68 61 74 0a   system so that.
5fa20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69  ** the SQLite li
5fa30 62 72 61 72 79 20 77 69 6c 6c 20 77 6f 72 6b 20  brary will work 
5fa40 6f 6e 20 62 6f 74 68 20 50 4f 53 49 58 20 61 6e  on both POSIX an
5fa50 64 20 77 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  d windows system
5fa60 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65  s..**.** This he
5fa70 61 64 65 72 20 66 69 6c 65 20 69 73 20 23 69 6e  ader file is #in
5fa80 63 6c 75 64 65 2d 65 64 20 62 79 20 73 71 6c 69  clude-ed by sqli
5fa90 74 65 49 6e 74 2e 68 20 61 6e 64 20 74 68 75 73  teInt.h and thus
5faa0 20 65 6e 64 73 20 75 70 0a 2a 2a 20 62 65 69 6e   ends up.** bein
5fab0 67 20 69 6e 63 6c 75 64 65 64 20 62 79 20 65 76  g included by ev
5fac0 65 72 79 20 73 6f 75 72 63 65 20 66 69 6c 65 2e  ery source file.
5fad0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 68  .**.** $Id: os.h
5fae0 2c 76 20 31 2e 31 30 35 20 32 30 30 38 2f 30 36  ,v 1.105 2008/06
5faf0 2f 32 36 20 31 30 3a 34 31 3a 31 39 20 64 61 6e  /26 10:41:19 dan
5fb00 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
5fb10 2f 0a 23 69 66 6e 64 65 66 20 5f 53 51 4c 49 54  /.#ifndef _SQLIT
5fb20 45 5f 4f 53 5f 48 5f 0a 23 64 65 66 69 6e 65 20  E_OS_H_.#define 
5fb30 5f 53 51 4c 49 54 45 5f 4f 53 5f 48 5f 0a 0a 2f  _SQLITE_OS_H_../
5fb40 2a 0a 2a 2a 20 46 69 67 75 72 65 20 6f 75 74 20  *.** Figure out 
5fb50 69 66 20 77 65 20 61 72 65 20 64 65 61 6c 69 6e  if we are dealin
5fb60 67 20 77 69 74 68 20 55 6e 69 78 2c 20 57 69 6e  g with Unix, Win
5fb70 64 6f 77 73 2c 20 6f 72 20 73 6f 6d 65 20 6f 74  dows, or some ot
5fb80 68 65 72 0a 2a 2a 20 6f 70 65 72 61 74 69 6e 67  her.** operating
5fb90 20 73 79 73 74 65 6d 2e 20 20 41 66 74 65 72 20   system.  After 
5fba0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  the following bl
5fbb0 6f 63 6b 20 6f 66 20 70 72 65 70 72 6f 63 65 73  ock of preproces
5fbc0 73 20 6d 61 63 72 6f 73 2c 0a 2a 2a 20 61 6c 6c  s macros,.** all
5fbd0 20 6f 66 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e   of SQLITE_OS_UN
5fbe0 49 58 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  IX, SQLITE_OS_WI
5fbf0 4e 2c 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32  N, SQLITE_OS_OS2
5fc00 2c 20 61 6e 64 20 53 51 4c 49 54 45 5f 4f 53 5f  , and SQLITE_OS_
5fc10 4f 54 48 45 52 20 0a 2a 2a 20 77 69 6c 6c 20 64  OTHER .** will d
5fc20 65 66 69 6e 65 64 20 74 6f 20 65 69 74 68 65 72  efined to either
5fc30 20 31 20 6f 72 20 30 2e 20 20 4f 6e 65 20 6f 66   1 or 0.  One of
5fc40 20 74 68 65 20 66 6f 75 72 20 77 69 6c 6c 20 62   the four will b
5fc50 65 20 31 2e 20 20 54 68 65 20 6f 74 68 65 72 20  e 1.  The other 
5fc60 0a 2a 2a 20 74 68 72 65 65 20 77 69 6c 6c 20 62  .** three will b
5fc70 65 20 30 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69  e 0..*/.#if defi
5fc80 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54  ned(SQLITE_OS_OT
5fc90 48 45 52 29 0a 23 20 69 66 20 53 51 4c 49 54 45  HER).# if SQLITE
5fca0 5f 4f 53 5f 4f 54 48 45 52 3d 3d 31 0a 23 20 20  _OS_OTHER==1.#  
5fcb0 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53   undef SQLITE_OS
5fcc0 5f 55 4e 49 58 0a 23 20 20 20 64 65 66 69 6e 65  _UNIX.#   define
5fcd0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
5fce0 30 0a 23 20 20 20 75 6e 64 65 66 20 53 51 4c 49  0.#   undef SQLI
5fcf0 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 20 20 64 65  TE_OS_WIN.#   de
5fd00 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57  fine SQLITE_OS_W
5fd10 49 4e 20 30 0a 23 20 20 20 75 6e 64 65 66 20 53  IN 0.#   undef S
5fd20 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a 23 20 20  QLITE_OS_OS2.#  
5fd30 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
5fd40 53 5f 4f 53 32 20 30 0a 23 20 65 6c 73 65 0a 23  S_OS2 0.# else.#
5fd50 20 20 20 75 6e 64 65 66 20 53 51 4c 49 54 45 5f     undef SQLITE_
5fd60 4f 53 5f 4f 54 48 45 52 0a 23 20 65 6e 64 69 66  OS_OTHER.# endif
5fd70 0a 23 65 6e 64 69 66 0a 23 69 66 20 21 64 65 66  .#endif.#if !def
5fd80 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 53 5f 55  ined(SQLITE_OS_U
5fd90 4e 49 58 29 20 26 26 20 21 64 65 66 69 6e 65 64  NIX) && !defined
5fda0 28 53 51 4c 49 54 45 5f 4f 53 5f 4f 54 48 45 52  (SQLITE_OS_OTHER
5fdb0 29 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  ).# define SQLIT
5fdc0 45 5f 4f 53 5f 4f 54 48 45 52 20 30 0a 23 20 69  E_OS_OTHER 0.# i
5fdd0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f  fndef SQLITE_OS_
5fde0 57 49 4e 0a 23 20 20 20 69 66 20 64 65 66 69 6e  WIN.#   if defin
5fdf0 65 64 28 5f 57 49 4e 33 32 29 20 7c 7c 20 64 65  ed(_WIN32) || de
5fe00 66 69 6e 65 64 28 57 49 4e 33 32 29 20 7c 7c 20  fined(WIN32) || 
5fe10 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49 4e  defined(__CYGWIN
5fe20 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
5fe30 5f 4d 49 4e 47 57 33 32 5f 5f 29 20 7c 7c 20 64  _MINGW32__) || d
5fe40 65 66 69 6e 65 64 28 5f 5f 42 4f 52 4c 41 4e 44  efined(__BORLAND
5fe50 43 5f 5f 29 0a 23 20 20 20 20 20 64 65 66 69 6e  C__).#     defin
5fe60 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 20  e SQLITE_OS_WIN 
5fe70 31 0a 23 20 20 20 20 20 64 65 66 69 6e 65 20 53  1.#     define S
5fe80 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 30 0a  QLITE_OS_UNIX 0.
5fe90 23 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c  #     define SQL
5fea0 49 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 20  ITE_OS_OS2 0.#  
5feb0 20 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 5f   elif defined(__
5fec0 45 4d 58 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  EMX__) || define
5fed0 64 28 5f 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e  d(_OS2) || defin
5fee0 65 64 28 4f 53 32 29 20 7c 7c 20 64 65 66 69 6e  ed(OS2) || defin
5fef0 65 64 28 5f 4f 53 32 5f 29 20 7c 7c 20 64 65 66  ed(_OS2_) || def
5ff00 69 6e 65 64 28 5f 5f 4f 53 32 5f 5f 29 0a 23 20  ined(__OS2__).# 
5ff10 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
5ff20 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20 20  E_OS_WIN 0.#    
5ff30 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f   define SQLITE_O
5ff40 53 5f 55 4e 49 58 20 30 0a 23 20 20 20 20 20 64  S_UNIX 0.#     d
5ff50 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f  efine SQLITE_OS_
5ff60 4f 53 32 20 31 0a 23 20 20 20 65 6c 73 65 0a 23  OS2 1.#   else.#
5ff70 20 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49       define SQLI
5ff80 54 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 20 20  TE_OS_WIN 0.#   
5ff90 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
5ffa0 4f 53 5f 55 4e 49 58 20 31 0a 23 20 20 20 20 20  OS_UNIX 1.#     
5ffb0 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53  define SQLITE_OS
5ffc0 5f 4f 53 32 20 30 0a 23 20 20 65 6e 64 69 66 0a  _OS2 0.#  endif.
5ffd0 23 20 65 6c 73 65 0a 23 20 20 64 65 66 69 6e 65  # else.#  define
5ffe0 20 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20   SQLITE_OS_UNIX 
5fff0 30 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49  0.#  define SQLI
60000 54 45 5f 4f 53 5f 4f 53 32 20 30 0a 23 20 65 6e  TE_OS_OS2 0.# en
60010 64 69 66 0a 23 65 6c 73 65 0a 23 20 69 66 6e 64  dif.#else.# ifnd
60020 65 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ef SQLITE_OS_WIN
60030 0a 23 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54  .#  define SQLIT
60040 45 5f 4f 53 5f 57 49 4e 20 30 0a 23 20 65 6e 64  E_OS_WIN 0.# end
60050 69 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  if.#endif../*.**
60060 20 44 65 74 65 72 6d 69 6e 65 20 69 66 20 77 65   Determine if we
60070 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77 69 74   are dealing wit
60080 68 20 57 69 6e 64 6f 77 73 43 45 20 2d 20 77 68  h WindowsCE - wh
60090 69 63 68 20 68 61 73 20 61 20 6d 75 63 68 0a 2a  ich has a much.*
600a0 2a 20 72 65 64 75 63 65 64 20 41 50 49 2e 0a 2a  * reduced API..*
600b0 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 5f 57  /.#if defined(_W
600c0 49 4e 33 32 5f 57 43 45 29 0a 23 20 64 65 66 69  IN32_WCE).# defi
600d0 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  ne SQLITE_OS_WIN
600e0 43 45 20 31 0a 23 65 6c 73 65 0a 23 20 64 65 66  CE 1.#else.# def
600f0 69 6e 65 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  ine SQLITE_OS_WI
60100 4e 43 45 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  NCE 0.#endif.../
60110 2a 0a 2a 2a 20 44 65 66 69 6e 65 20 74 68 65 20  *.** Define the 
60120 6d 61 78 69 6d 75 6d 20 73 69 7a 65 20 6f 66 20  maximum size of 
60130 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
60140 6e 61 6d 65 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  name.*/.#if SQLI
60150 54 45 5f 4f 53 5f 57 49 4e 0a 23 20 69 6e 63 6c  TE_OS_WIN.# incl
60160 75 64 65 20 3c 77 69 6e 64 6f 77 73 2e 68 3e 0a  ude <windows.h>.
60170 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
60180 54 45 4d 50 4e 41 4d 45 5f 53 49 5a 45 20 28 4d  TEMPNAME_SIZE (M
60190 41 58 5f 50 41 54 48 2b 35 30 29 0a 23 65 6c 69  AX_PATH+50).#eli
601a0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 4f 53 32 0a  f SQLITE_OS_OS2.
601b0 23 20 69 66 20 28 5f 5f 47 4e 55 43 5f 5f 20 3e  # if (__GNUC__ >
601c0 20 33 20 7c 7c 20 5f 5f 47 4e 55 43 5f 5f 20 3d   3 || __GNUC__ =
601d0 3d 20 33 20 26 26 20 5f 5f 47 4e 55 43 5f 4d 49  = 3 && __GNUC_MI
601e0 4e 4f 52 5f 5f 20 3e 3d 20 33 29 20 26 26 20 64  NOR__ >= 3) && d
601f0 65 66 69 6e 65 64 28 4f 53 32 5f 48 49 47 48 5f  efined(OS2_HIGH_
60200 4d 45 4d 4f 52 59 29 0a 23 20 20 69 6e 63 6c 75  MEMORY).#  inclu
60210 64 65 20 3c 6f 73 32 73 61 66 65 2e 68 3e 20 2f  de <os2safe.h> /
60220 2a 20 68 61 73 20 74 6f 20 62 65 20 69 6e 63 6c  * has to be incl
60230 75 64 65 64 20 62 65 66 6f 72 65 20 6f 73 32 2e  uded before os2.
60240 68 20 66 6f 72 20 6c 69 6e 6b 69 6e 67 20 74 6f  h for linking to
60250 20 77 6f 72 6b 20 2a 2f 0a 23 20 65 6e 64 69 66   work */.# endif
60260 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f 44  .# define INCL_D
60270 4f 53 44 41 54 45 54 49 4d 45 0a 23 20 64 65 66  OSDATETIME.# def
60280 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 46 49 4c 45  ine INCL_DOSFILE
60290 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 43  MGR.# define INC
602a0 4c 5f 44 4f 53 45 52 52 4f 52 53 0a 23 20 64 65  L_DOSERRORS.# de
602b0 66 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 49 53  fine INCL_DOSMIS
602c0 43 0a 23 20 64 65 66 69 6e 65 20 49 4e 43 4c 5f  C.# define INCL_
602d0 44 4f 53 50 52 4f 43 45 53 53 0a 23 20 64 65 66  DOSPROCESS.# def
602e0 69 6e 65 20 49 4e 43 4c 5f 44 4f 53 4d 4f 44 55  ine INCL_DOSMODU
602f0 4c 45 4d 47 52 0a 23 20 64 65 66 69 6e 65 20 49  LEMGR.# define I
60300 4e 43 4c 5f 44 4f 53 53 45 4d 41 50 48 4f 52 45  NCL_DOSSEMAPHORE
60310 53 0a 23 20 69 6e 63 6c 75 64 65 20 3c 6f 73 32  S.# include <os2
60320 2e 68 3e 0a 23 20 69 6e 63 6c 75 64 65 20 3c 75  .h>.# include <u
60330 63 6f 6e 76 2e 68 3e 0a 23 20 64 65 66 69 6e 65  conv.h>.# define
60340 20 53 51 4c 49 54 45 5f 54 45 4d 50 4e 41 4d 45   SQLITE_TEMPNAME
60350 5f 53 49 5a 45 20 28 43 43 48 4d 41 58 50 41 54  _SIZE (CCHMAXPAT
60360 48 43 4f 4d 50 29 0a 23 65 6c 73 65 0a 23 20 64  HCOMP).#else.# d
60370 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 54 45 4d  efine SQLITE_TEM
60380 50 4e 41 4d 45 5f 53 49 5a 45 20 32 30 30 0a 23  PNAME_SIZE 200.#
60390 65 6e 64 69 66 0a 0a 2f 2a 20 49 66 20 74 68 65  endif../* If the
603a0 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 20 6d 61   SET_FULLSYNC ma
603b0 63 72 6f 20 69 73 20 6e 6f 74 20 64 65 66 69 6e  cro is not defin
603c0 65 64 20 61 62 6f 76 65 2c 20 74 68 65 6e 20 6d  ed above, then m
603d0 61 6b 65 20 69 74 0a 2a 2a 20 61 20 6e 6f 2d 6f  ake it.** a no-o
603e0 70 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 45 54  p.*/.#ifndef SET
603f0 5f 46 55 4c 4c 53 59 4e 43 0a 23 20 64 65 66 69  _FULLSYNC.# defi
60400 6e 65 20 53 45 54 5f 46 55 4c 4c 53 59 4e 43 28  ne SET_FULLSYNC(
60410 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  x,y).#endif../*.
60420 2a 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 73  ** The default s
60430 69 7a 65 20 6f 66 20 61 20 64 69 73 6b 20 73 65  ize of a disk se
60440 63 74 6f 72 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ctor.*/.#ifndef 
60450 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53  SQLITE_DEFAULT_S
60460 45 43 54 4f 52 5f 53 49 5a 45 0a 23 20 64 65 66  ECTOR_SIZE.# def
60470 69 6e 65 20 53 51 4c 49 54 45 5f 44 45 46 41 55  ine SQLITE_DEFAU
60480 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 20 35  LT_SECTOR_SIZE 5
60490 31 32 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  12.#endif../*.**
604a0 20 54 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 73   Temporary files
604b0 20 61 72 65 20 6e 61 6d 65 64 20 73 74 61 72 74   are named start
604c0 69 6e 67 20 77 69 74 68 20 74 68 69 73 20 70 72  ing with this pr
604d0 65 66 69 78 20 66 6f 6c 6c 6f 77 65 64 20 62 79  efix followed by
604e0 20 31 36 20 72 61 6e 64 6f 6d 0a 2a 2a 20 61 6c   16 random.** al
604f0 70 68 61 6e 75 6d 65 72 69 63 20 63 68 61 72 61  phanumeric chara
60500 63 74 65 72 73 2c 20 61 6e 64 20 6e 6f 20 66 69  cters, and no fi
60510 6c 65 20 65 78 74 65 6e 73 69 6f 6e 2e 20 54 68  le extension. Th
60520 65 79 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e  ey are stored in
60530 20 74 68 65 0a 2a 2a 20 4f 53 27 73 20 73 74 61   the.** OS's sta
60540 6e 64 61 72 64 20 74 65 6d 70 6f 72 61 72 79 20  ndard temporary 
60550 66 69 6c 65 20 64 69 72 65 63 74 6f 72 79 2c 20  file directory, 
60560 61 6e 64 20 61 72 65 20 64 65 6c 65 74 65 64 20  and are deleted 
60570 70 72 69 6f 72 20 74 6f 20 65 78 69 74 2e 0a 2a  prior to exit..*
60580 2a 20 49 66 20 73 71 6c 69 74 65 20 69 73 20 62  * If sqlite is b
60590 65 69 6e 67 20 65 6d 62 65 64 64 65 64 20 69 6e  eing embedded in
605a0 20 61 6e 6f 74 68 65 72 20 70 72 6f 67 72 61 6d   another program
605b0 2c 20 79 6f 75 20 6d 61 79 20 77 69 73 68 20 74  , you may wish t
605c0 6f 20 63 68 61 6e 67 65 20 74 68 65 0a 2a 2a 20  o change the.** 
605d0 70 72 65 66 69 78 20 74 6f 20 72 65 66 6c 65 63  prefix to reflec
605e0 74 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 27 73  t your program's
605f0 20 6e 61 6d 65 2c 20 73 6f 20 74 68 61 74 20 69   name, so that i
60600 66 20 79 6f 75 72 20 70 72 6f 67 72 61 6d 20 65  f your program e
60610 78 69 74 73 0a 2a 2a 20 70 72 65 6d 61 74 75 72  xits.** prematur
60620 65 6c 79 2c 20 6f 6c 64 20 74 65 6d 70 6f 72 61  ely, old tempora
60630 72 79 20 66 69 6c 65 73 20 63 61 6e 20 62 65 20  ry files can be 
60640 65 61 73 69 6c 79 20 69 64 65 6e 74 69 66 69 65  easily identifie
60650 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20 64  d. This can be d
60660 6f 6e 65 0a 2a 2a 20 75 73 69 6e 67 20 2d 44 53  one.** using -DS
60670 51 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f  QLITE_TEMP_FILE_
60680 50 52 45 46 49 58 3d 6d 79 70 72 65 66 69 78 5f  PREFIX=myprefix_
60690 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65 72   on the compiler
606a0 20 63 6f 6d 6d 61 6e 64 20 6c 69 6e 65 2e 0a 2a   command line..*
606b0 2a 0a 2a 2a 20 32 30 30 36 2d 31 30 2d 33 31 3a  *.** 2006-10-31:
606c0 20 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 72    The default pr
606d0 65 66 69 78 20 75 73 65 64 20 74 6f 20 62 65 20  efix used to be 
606e0 22 73 71 6c 69 74 65 5f 22 2e 20 20 42 75 74 20  "sqlite_".  But 
606f0 74 68 65 6e 0a 2a 2a 20 4d 63 61 66 65 65 20 73  then.** Mcafee s
60700 74 61 72 74 65 64 20 75 73 69 6e 67 20 53 51 4c  tarted using SQL
60710 69 74 65 20 69 6e 20 74 68 65 69 72 20 61 6e 74  ite in their ant
60720 69 2d 76 69 72 75 73 20 70 72 6f 64 75 63 74 20  i-virus product 
60730 61 6e 64 20 69 74 0a 2a 2a 20 73 74 61 72 74 65  and it.** starte
60740 64 20 70 75 74 74 69 6e 67 20 66 69 6c 65 73 20  d putting files 
60750 77 69 74 68 20 74 68 65 20 22 73 71 6c 69 74 65  with the "sqlite
60760 22 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 63 3a  " name in the c:
60770 2f 74 65 6d 70 20 66 6f 6c 64 65 72 2e 0a 2a 2a  /temp folder..**
60780 20 54 68 69 73 20 61 6e 6e 6f 79 65 64 20 6d 61   This annoyed ma
60790 6e 79 20 77 69 6e 64 6f 77 73 20 75 73 65 72 73  ny windows users
607a0 2e 20 20 54 68 6f 73 65 20 75 73 65 72 73 20 77  .  Those users w
607b0 6f 75 6c 64 20 74 68 65 6e 20 64 6f 20 61 20 0a  ould then do a .
607c0 2a 2a 20 47 6f 6f 67 6c 65 20 73 65 61 72 63 68  ** Google search
607d0 20 66 6f 72 20 22 73 71 6c 69 74 65 22 2c 20 66   for "sqlite", f
607e0 69 6e 64 20 74 68 65 20 74 65 6c 65 70 68 6f 6e  ind the telephon
607f0 65 20 6e 75 6d 62 65 72 73 20 6f 66 20 74 68 65  e numbers of the
60800 0a 2a 2a 20 64 65 76 65 6c 6f 70 65 72 73 20 61  .** developers a
60810 6e 64 20 63 61 6c 6c 20 74 6f 20 77 61 6b 65 20  nd call to wake 
60820 74 68 65 6d 20 75 70 20 61 74 20 6e 69 67 68 74  them up at night
60830 20 61 6e 64 20 63 6f 6d 70 6c 61 69 6e 2e 0a 2a   and complain..*
60840 2a 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f  * For this reaso
60850 6e 2c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6e  n, the default n
60860 61 6d 65 20 70 72 65 66 69 78 20 69 73 20 63 68  ame prefix is ch
60870 61 6e 67 65 64 20 74 6f 20 62 65 20 22 73 71 6c  anged to be "sql
60880 69 74 65 22 20 0a 2a 2a 20 73 70 65 6c 6c 65 64  ite" .** spelled
60890 20 62 61 63 6b 77 61 72 64 73 2e 20 20 53 6f 20   backwards.  So 
608a0 74 68 65 20 74 65 6d 70 20 66 69 6c 65 73 20 61  the temp files a
608b0 72 65 20 73 74 69 6c 6c 20 69 64 65 6e 74 69 66  re still identif
608c0 69 65 64 2c 20 62 75 74 0a 2a 2a 20 61 6e 79 62  ied, but.** anyb
608d0 6f 64 79 20 73 6d 61 72 74 20 65 6e 6f 75 67 68  ody smart enough
608e0 20 74 6f 20 66 69 67 75 72 65 20 6f 75 74 20 74   to figure out t
608f0 68 65 20 63 6f 64 65 20 69 73 20 61 6c 73 6f 20  he code is also 
60900 6c 69 6b 65 6c 79 20 73 6d 61 72 74 0a 2a 2a 20  likely smart.** 
60910 65 6e 6f 75 67 68 20 74 6f 20 6b 6e 6f 77 20 74  enough to know t
60920 68 61 74 20 63 61 6c 6c 69 6e 67 20 74 68 65 20  hat calling the 
60930 64 65 76 65 6c 6f 70 65 72 20 77 69 6c 6c 20 6e  developer will n
60940 6f 74 20 68 65 6c 70 20 67 65 74 20 72 69 64 0a  ot help get rid.
60950 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a  ** of the file..
60960 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
60970 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46  E_TEMP_FILE_PREF
60980 49 58 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  IX.# define SQLI
60990 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45  TE_TEMP_FILE_PRE
609a0 46 49 58 20 22 65 74 69 6c 71 73 5f 22 0a 23 65  FIX "etilqs_".#e
609b0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
609c0 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75 65 73  following values
609d0 20 6d 61 79 20 62 65 20 70 61 73 73 65 64 20 61   may be passed a
609e0 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
609f0 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20 73 71 6c 69  ument to.** sqli
60a00 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 54 68 65  te3OsLock(). The
60a10 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73 20 65   various locks e
60a20 78 68 69 62 69 74 20 74 68 65 20 66 6f 6c 6c 6f  xhibit the follo
60a30 77 69 6e 67 20 73 65 6d 61 6e 74 69 63 73 3a 0a  wing semantics:.
60a40 2a 2a 0a 2a 2a 20 53 48 41 52 45 44 3a 20 20 20  **.** SHARED:   
60a50 20 41 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 70   Any number of p
60a60 72 6f 63 65 73 73 65 73 20 6d 61 79 20 68 6f 6c  rocesses may hol
60a70 64 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20  d a SHARED lock 
60a80 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e 0a  simultaneously..
60a90 2a 2a 20 52 45 53 45 52 56 45 44 3a 20 20 41 20  ** RESERVED:  A 
60aa0 73 69 6e 67 6c 65 20 70 72 6f 63 65 73 73 20 6d  single process m
60ab0 61 79 20 68 6f 6c 64 20 61 20 52 45 53 45 52 56  ay hold a RESERV
60ac0 45 44 20 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c  ED lock on a fil
60ad0 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  e at.**         
60ae0 20 20 20 61 6e 79 20 74 69 6d 65 2e 20 4f 74 68     any time. Oth
60af0 65 72 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79  er processes may
60b00 20 68 6f 6c 64 20 61 6e 64 20 6f 62 74 61 69 6e   hold and obtain
60b10 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b   new SHARED lock
60b20 73 2e 0a 2a 2a 20 50 45 4e 44 49 4e 47 3a 20 20  s..** PENDING:  
60b30 20 41 20 73 69 6e 67 6c 65 20 70 72 6f 63 65 73   A single proces
60b40 73 20 6d 61 79 20 68 6f 6c 64 20 61 20 50 45 4e  s may hold a PEN
60b50 44 49 4e 47 20 6c 6f 63 6b 20 6f 6e 20 61 20 66  DING lock on a f
60b60 69 6c 65 20 61 74 0a 2a 2a 20 20 20 20 20 20 20  ile at.**       
60b70 20 20 20 20 20 61 6e 79 20 6f 6e 65 20 74 69 6d       any one tim
60b80 65 2e 20 45 78 69 73 74 69 6e 67 20 53 48 41 52  e. Existing SHAR
60b90 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 70 65 72  ED locks may per
60ba0 73 69 73 74 2c 20 62 75 74 20 6e 6f 20 6e 65 77  sist, but no new
60bb0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 53  .**            S
60bc0 48 41 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20  HARED locks may 
60bd0 62 65 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6f  be obtained by o
60be0 74 68 65 72 20 70 72 6f 63 65 73 73 65 73 2e 0a  ther processes..
60bf0 2a 2a 20 45 58 43 4c 55 53 49 56 45 3a 20 41 6e  ** EXCLUSIVE: An
60c00 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
60c10 70 72 65 63 6c 75 64 65 73 20 61 6c 6c 20 6f 74  precludes all ot
60c20 68 65 72 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a  her locks..**.**
60c30 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 6d 61   PENDING_LOCK ma
60c40 79 20 6e 6f 74 20 62 65 20 70 61 73 73 65 64 20  y not be passed 
60c50 64 69 72 65 63 74 6c 79 20 74 6f 20 73 71 6c 69  directly to sqli
60c60 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 20 49 6e 73  te3OsLock(). Ins
60c70 74 65 61 64 2c 20 61 0a 2a 2a 20 70 72 6f 63 65  tead, a.** proce
60c80 73 73 20 74 68 61 74 20 72 65 71 75 65 73 74 73  ss that requests
60c90 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f   an EXCLUSIVE lo
60ca0 63 6b 20 6d 61 79 20 61 63 74 75 61 6c 6c 79 20  ck may actually 
60cb0 6f 62 74 61 69 6e 20 61 20 50 45 4e 44 49 4e 47  obtain a PENDING
60cc0 0a 2a 2a 20 6c 6f 63 6b 2e 20 54 68 69 73 20 63  .** lock. This c
60cd0 61 6e 20 62 65 20 75 70 67 72 61 64 65 64 20 74  an be upgraded t
60ce0 6f 20 61 6e 20 45 58 43 4c 55 53 49 56 45 20 6c  o an EXCLUSIVE l
60cf0 6f 63 6b 20 62 79 20 61 20 73 75 62 73 65 71 75  ock by a subsequ
60d00 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 73  ent call to.** s
60d10 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 29 2e 0a  qlite3OsLock()..
60d20 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 4f 5f 4c 4f  */.#define NO_LO
60d30 43 4b 20 20 20 20 20 20 20 20 20 30 0a 23 64 65  CK         0.#de
60d40 66 69 6e 65 20 53 48 41 52 45 44 5f 4c 4f 43 4b  fine SHARED_LOCK
60d50 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 52       1.#define R
60d60 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 20 20 32  ESERVED_LOCK   2
60d70 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47  .#define PENDING
60d80 5f 4c 4f 43 4b 20 20 20 20 33 0a 23 64 65 66 69  _LOCK    3.#defi
60d90 6e 65 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  ne EXCLUSIVE_LOC
60da0 4b 20 20 34 0a 0a 2f 2a 0a 2a 2a 20 46 69 6c 65  K  4../*.** File
60db0 20 4c 6f 63 6b 69 6e 67 20 4e 6f 74 65 73 3a 20   Locking Notes: 
60dc0 20 28 4d 6f 73 74 6c 79 20 61 62 6f 75 74 20 77   (Mostly about w
60dd0 69 6e 64 6f 77 73 20 62 75 74 20 61 6c 73 6f 20  indows but also 
60de0 73 6f 6d 65 20 69 6e 66 6f 20 66 6f 72 20 55 6e  some info for Un
60df0 69 78 29 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6e  ix).**.** We can
60e00 6e 6f 74 20 75 73 65 20 4c 6f 63 6b 46 69 6c 65  not use LockFile
60e10 45 78 28 29 20 6f 72 20 55 6e 6c 6f 63 6b 46 69  Ex() or UnlockFi
60e20 6c 65 45 78 28 29 20 6f 6e 20 57 69 6e 39 35 2f  leEx() on Win95/
60e30 39 38 2f 4d 45 20 62 65 63 61 75 73 65 0a 2a 2a  98/ME because.**
60e40 20 74 68 6f 73 65 20 66 75 6e 63 74 69 6f 6e 73   those functions
60e50 20 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62   are not availab
60e60 6c 65 2e 20 20 53 6f 20 77 65 20 75 73 65 20 6f  le.  So we use o
60e70 6e 6c 79 20 4c 6f 63 6b 46 69 6c 65 28 29 20 61  nly LockFile() a
60e80 6e 64 0a 2a 2a 20 55 6e 6c 6f 63 6b 46 69 6c 65  nd.** UnlockFile
60e90 28 29 2e 0a 2a 2a 0a 2a 2a 20 4c 6f 63 6b 46 69  ()..**.** LockFi
60ea0 6c 65 28 29 20 70 72 65 76 65 6e 74 73 20 6e 6f  le() prevents no
60eb0 74 20 6a 75 73 74 20 77 72 69 74 69 6e 67 20 62  t just writing b
60ec0 75 74 20 61 6c 73 6f 20 72 65 61 64 69 6e 67 20  ut also reading 
60ed0 62 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  by other process
60ee0 65 73 2e 0a 2a 2a 20 41 20 53 48 41 52 45 44 5f  es..** A SHARED_
60ef0 4c 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64  LOCK is obtained
60f00 20 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 73 69   by locking a si
60f10 6e 67 6c 65 20 72 61 6e 64 6f 6d 6c 79 2d 63 68  ngle randomly-ch
60f20 6f 73 65 6e 20 0a 2a 2a 20 62 79 74 65 20 6f 75  osen .** byte ou
60f30 74 20 6f 66 20 61 20 73 70 65 63 69 66 69 63 20  t of a specific 
60f40 72 61 6e 67 65 20 6f 66 20 62 79 74 65 73 2e 20  range of bytes. 
60f50 54 68 65 20 6c 6f 63 6b 20 62 79 74 65 20 69 73  The lock byte is
60f60 20 6f 62 74 61 69 6e 65 64 20 61 74 20 0a 2a 2a   obtained at .**
60f70 20 72 61 6e 64 6f 6d 20 73 6f 20 74 77 6f 20 73   random so two s
60f80 65 70 61 72 61 74 65 20 72 65 61 64 65 72 73 20  eparate readers 
60f90 63 61 6e 20 70 72 6f 62 61 62 6c 79 20 61 63 63  can probably acc
60fa0 65 73 73 20 74 68 65 20 66 69 6c 65 20 61 74 20  ess the file at 
60fb0 74 68 65 20 0a 2a 2a 20 73 61 6d 65 20 74 69 6d  the .** same tim
60fc0 65 2c 20 75 6e 6c 65 73 73 20 74 68 65 79 20 61  e, unless they a
60fd0 72 65 20 75 6e 6c 75 63 6b 79 20 61 6e 64 20 63  re unlucky and c
60fe0 68 6f 6f 73 65 20 74 68 65 20 73 61 6d 65 20 6c  hoose the same l
60ff0 6f 63 6b 20 62 79 74 65 2e 0a 2a 2a 20 41 6e 20  ock byte..** An 
61000 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 69  EXCLUSIVE_LOCK i
61010 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c 6f  s obtained by lo
61020 63 6b 69 6e 67 20 61 6c 6c 20 62 79 74 65 73 20  cking all bytes 
61030 69 6e 20 74 68 65 20 72 61 6e 67 65 2e 0a 2a 2a  in the range..**
61040 20 54 68 65 72 65 20 63 61 6e 20 6f 6e 6c 79 20   There can only 
61050 62 65 20 6f 6e 65 20 77 72 69 74 65 72 2e 20 20  be one writer.  
61060 41 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20  A RESERVED_LOCK 
61070 69 73 20 6f 62 74 61 69 6e 65 64 20 62 79 20 6c  is obtained by l
61080 6f 63 6b 69 6e 67 0a 2a 2a 20 61 20 73 69 6e 67  ocking.** a sing
61090 6c 65 20 62 79 74 65 20 6f 66 20 74 68 65 20 66  le byte of the f
610a0 69 6c 65 20 74 68 61 74 20 69 73 20 64 65 73 69  ile that is desi
610b0 67 6e 61 74 65 64 20 61 73 20 74 68 65 20 72 65  gnated as the re
610c0 73 65 72 76 65 64 20 6c 6f 63 6b 20 62 79 74 65  served lock byte
610d0 2e 0a 2a 2a 20 41 20 50 45 4e 44 49 4e 47 5f 4c  ..** A PENDING_L
610e0 4f 43 4b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  OCK is obtained 
610f0 62 79 20 6c 6f 63 6b 69 6e 67 20 61 20 64 65 73  by locking a des
61100 69 67 6e 61 74 65 64 20 62 79 74 65 20 64 69 66  ignated byte dif
61110 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a 2a 20 74  ferent from.** t
61120 68 65 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  he RESERVED_LOCK
61130 20 62 79 74 65 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 20   byte..**.** On 
61140 57 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 79 73 74  WinNT/2K/XP syst
61150 65 6d 73 2c 20 4c 6f 63 6b 46 69 6c 65 45 78 28  ems, LockFileEx(
61160 29 20 61 6e 64 20 55 6e 6c 6f 63 6b 46 69 6c 65  ) and UnlockFile
61170 45 78 28 29 20 61 72 65 20 61 76 61 69 6c 61 62  Ex() are availab
61180 6c 65 2c 0a 2a 2a 20 77 68 69 63 68 20 6d 65 61  le,.** which mea
61190 6e 73 20 77 65 20 63 61 6e 20 75 73 65 20 72 65  ns we can use re
611a0 61 64 65 72 2f 77 72 69 74 65 72 20 6c 6f 63 6b  ader/writer lock
611b0 73 2e 20 20 57 68 65 6e 20 72 65 61 64 65 72 2f  s.  When reader/
611c0 77 72 69 74 65 72 20 6c 6f 63 6b 73 0a 2a 2a 20  writer locks.** 
611d0 61 72 65 20 75 73 65 64 2c 20 74 68 65 20 6c 6f  are used, the lo
611e0 63 6b 20 69 73 20 70 6c 61 63 65 64 20 6f 6e 20  ck is placed on 
611f0 74 68 65 20 73 61 6d 65 20 72 61 6e 67 65 20 6f  the same range o
61200 66 20 62 79 74 65 73 20 74 68 61 74 20 69 73 20  f bytes that is 
61210 75 73 65 64 0a 2a 2a 20 66 6f 72 20 70 72 6f 62  used.** for prob
61220 61 62 69 6c 69 73 74 69 63 20 6c 6f 63 6b 69 6e  abilistic lockin
61230 67 20 69 6e 20 57 69 6e 39 35 2f 39 38 2f 4d 45  g in Win95/98/ME
61240 2e 20 20 48 65 6e 63 65 2c 20 74 68 65 20 6c 6f  .  Hence, the lo
61250 63 6b 69 6e 67 20 73 63 68 65 6d 65 0a 2a 2a 20  cking scheme.** 
61260 77 69 6c 6c 20 73 75 70 70 6f 72 74 20 74 77 6f  will support two
61270 20 6f 72 20 6d 6f 72 65 20 57 69 6e 39 35 20 72   or more Win95 r
61280 65 61 64 65 72 73 20 6f 72 20 74 77 6f 20 6f 72  eaders or two or
61290 20 6d 6f 72 65 20 57 69 6e 4e 54 20 72 65 61 64   more WinNT read
612a0 65 72 73 2e 0a 2a 2a 20 42 75 74 20 61 20 73 69  ers..** But a si
612b0 6e 67 6c 65 20 57 69 6e 39 35 20 72 65 61 64 65  ngle Win95 reade
612c0 72 20 77 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20  r will lock out 
612d0 61 6c 6c 20 57 69 6e 4e 54 20 72 65 61 64 65 72  all WinNT reader
612e0 73 20 61 6e 64 20 61 20 73 69 6e 67 6c 65 0a 2a  s and a single.*
612f0 2a 20 57 69 6e 4e 54 20 72 65 61 64 65 72 20 77  * WinNT reader w
61300 69 6c 6c 20 6c 6f 63 6b 20 6f 75 74 20 61 6c 6c  ill lock out all
61310 20 6f 74 68 65 72 20 57 69 6e 39 35 20 72 65 61   other Win95 rea
61320 64 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ders..**.** The 
61330 66 6f 6c 6c 6f 77 69 6e 67 20 23 64 65 66 69 6e  following #defin
61340 65 73 20 73 70 65 63 69 66 79 20 74 68 65 20 72  es specify the r
61350 61 6e 67 65 20 6f 66 20 62 79 74 65 73 20 75 73  ange of bytes us
61360 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 0a  ed for locking..
61370 2a 2a 20 53 48 41 52 45 44 5f 53 49 5a 45 20 69  ** SHARED_SIZE i
61380 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  s the number of 
61390 62 79 74 65 73 20 61 76 61 69 6c 61 62 6c 65 20  bytes available 
613a0 69 6e 20 74 68 65 20 70 6f 6f 6c 20 66 72 6f 6d  in the pool from
613b0 20 77 68 69 63 68 0a 2a 2a 20 61 20 72 61 6e 64   which.** a rand
613c0 6f 6d 20 62 79 74 65 20 69 73 20 73 65 6c 65 63  om byte is selec
613d0 74 65 64 20 66 6f 72 20 61 20 73 68 61 72 65 64  ted for a shared
613e0 20 6c 6f 63 6b 2e 20 20 54 68 65 20 70 6f 6f 6c   lock.  The pool
613f0 20 6f 66 20 62 79 74 65 73 20 66 6f 72 0a 2a 2a   of bytes for.**
61400 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 62 65   shared locks be
61410 67 69 6e 73 20 61 74 20 53 48 41 52 45 44 5f 46  gins at SHARED_F
61420 49 52 53 54 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 65  IRST. .**.** The
61430 73 65 20 23 64 65 66 69 6e 65 73 20 61 72 65 20  se #defines are 
61440 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 73 71 6c  available in sql
61450 69 74 65 5f 61 75 78 2e 68 20 73 6f 20 74 68 61  ite_aux.h so tha
61460 74 20 61 64 61 70 74 6f 72 73 20 66 6f 72 0a 2a  t adaptors for.*
61470 2a 20 63 6f 6e 6e 65 63 74 69 6e 67 20 53 51 4c  * connecting SQL
61480 69 74 65 20 74 6f 20 6f 74 68 65 72 20 6f 70 65  ite to other ope
61490 72 61 74 69 6e 67 20 73 79 73 74 65 6d 73 20 63  rating systems c
614a0 61 6e 20 75 73 65 20 74 68 65 20 73 61 6d 65 20  an use the same 
614b0 62 79 74 65 0a 2a 2a 20 72 61 6e 67 65 73 20 66  byte.** ranges f
614c0 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20 20 49 6e 20  or locking.  In 
614d0 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 65 20  particular, the 
614e0 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72  same locking str
614f0 61 74 65 67 79 20 61 6e 64 0a 2a 2a 20 62 79 74  ategy and.** byt
61500 65 20 72 61 6e 67 65 73 20 61 72 65 20 75 73 65  e ranges are use
61510 64 20 66 6f 72 20 55 6e 69 78 2e 20 20 54 68 69  d for Unix.  Thi
61520 73 20 6c 65 61 76 65 73 20 6f 70 65 6e 20 74 68  s leaves open th
61530 65 20 70 6f 73 73 69 62 6c 69 74 79 20 6f 66 20  e possiblity of 
61540 68 61 76 69 6e 67 0a 2a 2a 20 63 6c 69 65 6e 74  having.** client
61550 73 20 6f 6e 20 77 69 6e 39 35 2c 20 77 69 6e 4e  s on win95, winN
61560 54 2c 20 61 6e 64 20 75 6e 69 78 20 61 6c 6c 20  T, and unix all 
61570 74 61 6c 6b 69 6e 67 20 74 6f 20 74 68 65 20 73  talking to the s
61580 61 6d 65 20 73 68 61 72 65 64 20 66 69 6c 65 0a  ame shared file.
61590 2a 2a 20 61 6e 64 20 61 6c 6c 20 6c 6f 63 6b 69  ** and all locki
615a0 6e 67 20 63 6f 72 72 65 63 74 6c 79 2e 20 20 54  ng correctly.  T
615b0 6f 20 64 6f 20 73 6f 20 77 6f 75 6c 64 20 72 65  o do so would re
615c0 71 75 69 72 65 20 74 68 61 74 20 73 61 6d 62 61  quire that samba
615d0 20 28 6f 72 20 77 68 61 74 65 76 65 72 0a 2a 2a   (or whatever.**
615e0 20 74 6f 6f 6c 20 69 73 20 62 65 69 6e 67 20 75   tool is being u
615f0 73 65 64 20 66 6f 72 20 66 69 6c 65 20 73 68 61  sed for file sha
61600 72 69 6e 67 29 20 69 6d 70 6c 65 6d 65 6e 74 73  ring) implements
61610 20 6c 6f 63 6b 73 20 63 6f 72 72 65 63 74 6c 79   locks correctly
61620 20 62 65 74 77 65 65 6e 0a 2a 2a 20 77 69 6e 64   between.** wind
61630 6f 77 73 20 61 6e 64 20 75 6e 69 78 2e 20 20 49  ows and unix.  I
61640 27 6d 20 67 75 65 73 73 69 6e 67 20 74 68 61 74  'm guessing that
61650 20 69 73 6e 27 74 20 6c 69 6b 65 6c 79 20 74 6f   isn't likely to
61660 20 68 61 70 70 65 6e 2c 20 62 75 74 20 62 79 0a   happen, but by.
61670 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 61 6d  ** using the sam
61680 65 20 6c 6f 63 6b 69 6e 67 20 72 61 6e 67 65 20  e locking range 
61690 77 65 20 61 72 65 20 61 74 20 6c 65 61 73 74 20  we are at least 
616a0 6f 70 65 6e 20 74 6f 20 74 68 65 20 70 6f 73 73  open to the poss
616b0 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 4c  ibility..**.** L
616c0 6f 63 6b 69 6e 67 20 69 6e 20 77 69 6e 64 6f 77  ocking in window
616d0 73 20 69 73 20 6d 61 6e 64 69 74 6f 72 79 2e 20  s is manditory. 
616e0 20 46 6f 72 20 74 68 69 73 20 72 65 61 73 6f 6e   For this reason
616f0 2c 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f 72  , we cannot stor
61700 65 0a 2a 2a 20 61 63 74 75 61 6c 20 64 61 74 61  e.** actual data
61710 20 69 6e 20 74 68 65 20 62 79 74 65 73 20 75 73   in the bytes us
61720 65 64 20 66 6f 72 20 6c 6f 63 6b 69 6e 67 2e 20  ed for locking. 
61730 20 54 68 65 20 70 61 67 65 72 20 6e 65 76 65 72   The pager never
61740 20 61 6c 6c 6f 63 61 74 65 73 0a 2a 2a 20 74 68   allocates.** th
61750 65 20 70 61 67 65 73 20 69 6e 76 6f 6c 76 65 64  e pages involved
61760 20 69 6e 20 6c 6f 63 6b 69 6e 67 20 74 68 65 72   in locking ther
61770 65 66 6f 72 65 2e 20 20 53 48 41 52 45 44 5f 53  efore.  SHARED_S
61780 49 5a 45 20 69 73 20 73 65 6c 65 63 74 65 64 20  IZE is selected 
61790 73 6f 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20 6c  so.** that all l
617a0 6f 63 6b 73 20 77 69 6c 6c 20 66 69 74 20 6f 6e  ocks will fit on
617b0 20 61 20 73 69 6e 67 6c 65 20 70 61 67 65 20 65   a single page e
617c0 76 65 6e 20 61 74 20 74 68 65 20 6d 69 6e 69 6d  ven at the minim
617d0 75 6d 20 70 61 67 65 20 73 69 7a 65 2e 0a 2a 2a  um page size..**
617e0 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 64 65   PENDING_BYTE de
617f0 66 69 6e 65 73 20 74 68 65 20 62 65 67 69 6e 6e  fines the beginn
61800 69 6e 67 20 6f 66 20 74 68 65 20 6c 6f 63 6b 73  ing of the locks
61810 2e 20 20 42 79 20 64 65 66 61 75 6c 74 20 50 45  .  By default PE
61820 4e 44 49 4e 47 5f 42 59 54 45 0a 2a 2a 20 69 73  NDING_BYTE.** is
61830 20 73 65 74 20 68 69 67 68 20 73 6f 20 74 68 61   set high so tha
61840 74 20 77 65 20 64 6f 6e 27 74 20 68 61 76 65 20  t we don't have 
61850 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 6e 20 75  to allocate an u
61860 6e 75 73 65 64 20 70 61 67 65 20 65 78 63 65 70  nused page excep
61870 74 0a 2a 2a 20 66 6f 72 20 76 65 72 79 20 6c 61  t.** for very la
61880 72 67 65 20 64 61 74 61 62 61 73 65 73 2e 20 20  rge databases.  
61890 42 75 74 20 6f 6e 65 20 73 68 6f 75 6c 64 20 74  But one should t
618a0 65 73 74 20 74 68 65 20 70 61 67 65 20 73 6b 69  est the page ski
618b0 70 70 69 6e 67 20 6c 6f 67 69 63 20 0a 2a 2a 20  pping logic .** 
618c0 62 79 20 73 65 74 74 69 6e 67 20 50 45 4e 44 49  by setting PENDI
618d0 4e 47 5f 42 59 54 45 20 6c 6f 77 20 61 6e 64 20  NG_BYTE low and 
618e0 72 75 6e 6e 69 6e 67 20 74 68 65 20 65 6e 74 69  running the enti
618f0 72 65 20 72 65 67 72 65 73 73 69 6f 6e 20 73 75  re regression su
61900 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 68 61 6e 67  ite..**.** Chang
61910 69 6e 67 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ing the value of
61920 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 72 65   PENDING_BYTE re
61930 73 75 6c 74 73 20 69 6e 20 61 20 73 75 62 74 6c  sults in a subtl
61940 79 20 69 6e 63 6f 6d 70 61 74 69 62 6c 65 0a 2a  y incompatible.*
61950 2a 20 66 69 6c 65 20 66 6f 72 6d 61 74 2e 20 20  * file format.  
61960 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20 68 6f 77  Depending on how
61970 20 69 74 20 69 73 20 63 68 61 6e 67 65 64 2c 20   it is changed, 
61980 79 6f 75 20 6d 69 67 68 74 20 6e 6f 74 20 6e 6f  you might not no
61990 74 69 63 65 0a 2a 2a 20 74 68 65 20 69 6e 63 6f  tice.** the inco
619a0 6d 70 61 74 69 62 69 6c 69 74 79 20 72 69 67 68  mpatibility righ
619b0 74 20 61 77 61 79 2c 20 65 76 65 6e 20 72 75 6e  t away, even run
619c0 6e 69 6e 67 20 61 20 66 75 6c 6c 20 72 65 67 72  ning a full regr
619d0 65 73 73 69 6f 6e 20 74 65 73 74 2e 0a 2a 2a 20  ession test..** 
619e0 54 68 65 20 64 65 66 61 75 6c 74 20 6c 6f 63 61  The default loca
619f0 74 69 6f 6e 20 6f 66 20 50 45 4e 44 49 4e 47 5f  tion of PENDING_
61a00 42 59 54 45 20 69 73 20 74 68 65 20 66 69 72 73  BYTE is the firs
61a10 74 20 62 79 74 65 20 70 61 73 74 20 74 68 65 0a  t byte past the.
61a20 2a 2a 20 31 47 42 20 62 6f 75 6e 64 61 72 79 2e  ** 1GB boundary.
61a30 0a 2a 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  .**.*/.#ifndef S
61a40 51 4c 49 54 45 5f 54 45 53 54 0a 23 64 65 66 69  QLITE_TEST.#defi
61a50 6e 65 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20  ne PENDING_BYTE 
61a60 20 20 20 20 20 30 78 34 30 30 30 30 30 30 30 20       0x40000000 
61a70 20 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 70   /* First byte p
61a80 61 73 74 20 74 68 65 20 31 47 42 20 62 6f 75 6e  ast the 1GB boun
61a90 64 61 72 79 20 2a 2f 0a 23 65 6c 73 65 0a 53 51  dary */.#else.SQ
61aa0 4c 49 54 45 5f 41 50 49 20 65 78 74 65 72 6e 20  LITE_API extern 
61ab0 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 73 71 6c  unsigned int sql
61ac0 69 74 65 33 5f 70 65 6e 64 69 6e 67 5f 62 79 74  ite3_pending_byt
61ad0 65 3b 0a 23 64 65 66 69 6e 65 20 50 45 4e 44 49  e;.#define PENDI
61ae0 4e 47 5f 42 59 54 45 20 73 71 6c 69 74 65 33 5f  NG_BYTE sqlite3_
61af0 70 65 6e 64 69 6e 67 5f 62 79 74 65 0a 23 65 6e  pending_byte.#en
61b00 64 69 66 0a 0a 23 64 65 66 69 6e 65 20 52 45 53  dif..#define RES
61b10 45 52 56 45 44 5f 42 59 54 45 20 20 20 20 20 28  ERVED_BYTE     (
61b20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 29 0a  PENDING_BYTE+1).
61b30 23 64 65 66 69 6e 65 20 53 48 41 52 45 44 5f 46  #define SHARED_F
61b40 49 52 53 54 20 20 20 20 20 20 28 50 45 4e 44 49  IRST      (PENDI
61b50 4e 47 5f 42 59 54 45 2b 32 29 0a 23 64 65 66 69  NG_BYTE+2).#defi
61b60 6e 65 20 53 48 41 52 45 44 5f 53 49 5a 45 20 20  ne SHARED_SIZE  
61b70 20 20 20 20 20 35 31 30 0a 0a 2f 2a 20 0a 2a 2a       510../* .**
61b80 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61   Functions for a
61b90 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33  ccessing sqlite3
61ba0 5f 66 69 6c 65 20 6d 65 74 68 6f 64 73 20 0a 2a  _file methods .*
61bb0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
61bc0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c   int sqlite3OsCl
61bd0 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
61be0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
61bf0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
61c00 52 65 61 64 28 73 71 6c 69 74 65 33 5f 66 69 6c  Read(sqlite3_fil
61c10 65 2a 2c 20 76 6f 69 64 2a 2c 20 69 6e 74 20 61  e*, void*, int a
61c20 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29 3b  mt, i64 offset);
61c30 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61c40 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 57 72 69  int sqlite3OsWri
61c50 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a  te(sqlite3_file*
61c60 2c 20 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 20 69  , const void*, i
61c70 6e 74 20 61 6d 74 2c 20 69 36 34 20 6f 66 66 73  nt amt, i64 offs
61c80 65 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  et);.SQLITE_PRIV
61c90 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
61ca0 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  sTruncate(sqlite
61cb0 33 5f 66 69 6c 65 2a 2c 20 69 36 34 20 73 69 7a  3_file*, i64 siz
61cc0 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  e);.SQLITE_PRIVA
61cd0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
61ce0 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66 69 6c  Sync(sqlite3_fil
61cf0 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  e*, int);.SQLITE
61d00 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
61d10 69 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 73  ite3OsFileSize(s
61d20 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 36  qlite3_file*, i6
61d30 34 20 2a 70 53 69 7a 65 29 3b 0a 53 51 4c 49 54  4 *pSize);.SQLIT
61d40 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
61d50 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73 71 6c 69  lite3OsLock(sqli
61d60 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e 74 29 3b  te3_file*, int);
61d70 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61d80 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  int sqlite3OsUnl
61d90 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
61da0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
61db0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
61dc0 74 65 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76  te3OsCheckReserv
61dd0 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
61de0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52  ile *id, int *pR
61df0 65 73 4f 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50  esOut);.SQLITE_P
61e00 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
61e10 65 33 4f 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28  e3OsFileControl(
61e20 73 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 69 6e  sqlite3_file*,in
61e30 74 2c 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  t,void*);.SQLITE
61e40 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
61e50 69 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65  ite3OsSectorSize
61e60 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
61e70 64 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  d);.SQLITE_PRIVA
61e80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
61e90 44 65 76 69 63 65 43 68 61 72 61 63 74 65 72 69  DeviceCharacteri
61ea0 73 74 69 63 73 28 73 71 6c 69 74 65 33 5f 66 69  stics(sqlite3_fi
61eb0 6c 65 20 2a 69 64 29 3b 0a 0a 2f 2a 20 0a 2a 2a  le *id);../* .**
61ec0 20 46 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 61   Functions for a
61ed0 63 63 65 73 73 69 6e 67 20 73 71 6c 69 74 65 33  ccessing sqlite3
61ee0 5f 76 66 73 20 6d 65 74 68 6f 64 73 20 0a 2a 2f  _vfs methods .*/
61ef0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61f00 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65  int sqlite3OsOpe
61f10 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c  n(sqlite3_vfs *,
61f20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 73   const char *, s
61f30 71 6c 69 74 65 33 5f 66 69 6c 65 2a 2c 20 69 6e  qlite3_file*, in
61f40 74 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54  t, int *);.SQLIT
61f50 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
61f60 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71  lite3OsDelete(sq
61f70 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
61f80 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
61f90 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
61fa0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63 63  int sqlite3OsAcc
61fb0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
61fc0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
61fd0 20 69 6e 74 2c 20 69 6e 74 20 2a 70 52 65 73 4f   int, int *pResO
61fe0 75 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ut);.SQLITE_PRIV
61ff0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
62000 73 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28 73 71  sFullPathname(sq
62010 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e  lite3_vfs *, con
62020 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 2c 20  st char *, int, 
62030 63 68 61 72 20 2a 29 3b 0a 23 69 66 6e 64 65 66  char *);.#ifndef
62040 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
62050 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49  D_EXTENSION.SQLI
62060 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
62070 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65 6e  *sqlite3OsDlOpen
62080 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20  (sqlite3_vfs *, 
62090 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
620a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
620b0 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45 72  id sqlite3OsDlEr
620c0 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ror(sqlite3_vfs 
620d0 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20 2a 29 3b  *, int, char *);
620e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
620f0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4f 73 44  void *sqlite3OsD
62100 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73  lSym(sqlite3_vfs
62110 20 2a 2c 20 76 6f 69 64 20 2a 2c 20 63 6f 6e 73   *, void *, cons
62120 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49 54  t char *);.SQLIT
62130 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
62140 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
62150 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 2c 20 76  sqlite3_vfs *, v
62160 6f 69 64 20 2a 29 3b 0a 23 65 6e 64 69 66 20 2f  oid *);.#endif /
62170 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  * SQLITE_OMIT_LO
62180 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a  AD_EXTENSION */.
62190 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
621a0 6e 74 20 73 71 6c 69 74 65 33 4f 73 52 61 6e 64  nt sqlite3OsRand
621b0 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
621c0 66 73 20 2a 2c 20 69 6e 74 2c 20 63 68 61 72 20  fs *, int, char 
621d0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
621e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
621f0 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f 76 66  Sleep(sqlite3_vf
62200 73 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  s *, int);.SQLIT
62210 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
62220 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
62230 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  me(sqlite3_vfs *
62240 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 0a 2f 2a 0a  , double*);../*.
62250 2a 2a 20 43 6f 6e 76 65 6e 69 65 6e 63 65 20 66  ** Convenience f
62260 75 6e 63 74 69 6f 6e 73 20 66 6f 72 20 6f 70 65  unctions for ope
62270 6e 69 6e 67 20 61 6e 64 20 63 6c 6f 73 69 6e 67  ning and closing
62280 20 66 69 6c 65 73 20 75 73 69 6e 67 20 0a 2a 2a   files using .**
62290 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
622a0 29 20 74 6f 20 6f 62 74 61 69 6e 20 73 70 61 63  ) to obtain spac
622b0 65 20 66 6f 72 20 74 68 65 20 66 69 6c 65 2d 68  e for the file-h
622c0 61 6e 64 6c 65 20 73 74 72 75 63 74 75 72 65 2e  andle structure.
622d0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
622e0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
622f0 4f 70 65 6e 4d 61 6c 6c 6f 63 28 73 71 6c 69 74  OpenMalloc(sqlit
62300 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73 74 20  e3_vfs *, const 
62310 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65 33 5f  char *, sqlite3_
62320 66 69 6c 65 20 2a 2a 2c 20 69 6e 74 2c 69 6e 74  file **, int,int
62330 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
62340 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73  TE int sqlite3Os
62350 43 6c 6f 73 65 46 72 65 65 28 73 71 6c 69 74 65  CloseFree(sqlite
62360 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 65 6e 64  3_file *);..#end
62370 69 66 20 2f 2a 20 5f 53 51 4c 49 54 45 5f 4f 53  if /* _SQLITE_OS
62380 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  _H_ */../*******
62390 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f  ******* End of o
623a0 73 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  s.h ************
623b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
623c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
623d0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
623e0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
623f0 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
62400 20 6f 66 66 20 69 6e 20 73 71 6c 69 74 65 49 6e   off in sqliteIn
62410 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t.h ************
62420 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
62430 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20  ******* Include 
62440 6d 75 74 65 78 2e 68 20 69 6e 20 74 68 65 20 6d  mutex.h in the m
62450 69 64 64 6c 65 20 6f 66 20 73 71 6c 69 74 65 49  iddle of sqliteI
62460 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  nt.h ***********
62470 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
62480 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
62490 6c 65 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a  le mutex.h *****
624a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
624b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
624c0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
624d0 30 37 20 41 75 67 75 73 74 20 32 38 0a 2a 2a 0a  07 August 28.**.
624e0 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
624f0 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
62500 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
62510 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
62520 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
62530 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
62540 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
62550 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
62560 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
62570 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
62580 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
62590 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
625a0 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
625b0 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
625c0 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
625d0 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
625e0 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
625f0 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
62600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
62640 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
62650 61 69 6e 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20  ains the common 
62660 68 65 61 64 65 72 20 66 6f 72 20 61 6c 6c 20 6d  header for all m
62670 75 74 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74  utex implementat
62680 69 6f 6e 73 2e 0a 2a 2a 20 54 68 65 20 73 71 6c  ions..** The sql
62690 69 74 65 49 6e 74 2e 68 20 68 65 61 64 65 72 20  iteInt.h header 
626a0 23 69 6e 63 6c 75 64 65 73 20 74 68 69 73 20 66  #includes this f
626b0 69 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 69  ile so that it i
626c0 73 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a 20 74  s available.** t
626d0 6f 20 61 6c 6c 20 73 6f 75 72 63 65 20 66 69 6c  o all source fil
626e0 65 73 2e 20 20 57 65 20 62 72 65 61 6b 20 69 74  es.  We break it
626f0 20 6f 75 74 20 69 6e 20 61 6e 20 65 66 66 6f 72   out in an effor
62700 74 20 74 6f 20 6b 65 65 70 20 74 68 65 20 63 6f  t to keep the co
62710 64 65 0a 2a 2a 20 62 65 74 74 65 72 20 6f 72 67  de.** better org
62720 61 6e 69 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 4e 4f  anized..**.** NO
62730 54 45 3a 20 20 73 6f 75 72 63 65 20 66 69 6c 65  TE:  source file
62740 73 20 73 68 6f 75 6c 64 20 2a 6e 6f 74 2a 20 23  s should *not* #
62750 69 6e 63 6c 75 64 65 20 74 68 69 73 20 68 65 61  include this hea
62760 64 65 72 20 66 69 6c 65 20 64 69 72 65 63 74 6c  der file directl
62770 79 2e 0a 2a 2a 20 53 6f 75 72 63 65 20 66 69 6c  y..** Source fil
62780 65 73 20 73 68 6f 75 6c 64 20 23 69 6e 63 6c 75  es should #inclu
62790 64 65 20 74 68 65 20 73 71 6c 69 74 65 49 6e 74  de the sqliteInt
627a0 2e 68 20 66 69 6c 65 20 61 6e 64 20 6c 65 74 20  .h file and let 
627b0 74 68 61 74 20 66 69 6c 65 0a 2a 2a 20 69 6e 63  that file.** inc
627c0 6c 75 64 65 20 74 68 69 73 20 6f 6e 65 20 69 6e  lude this one in
627d0 64 69 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a 20  directly..**.** 
627e0 24 49 64 3a 20 6d 75 74 65 78 2e 68 2c 76 20 31  $Id: mutex.h,v 1
627f0 2e 39 20 32 30 30 38 2f 31 30 2f 30 37 20 31 35  .9 2008/10/07 15
62800 3a 32 35 3a 34 38 20 64 72 68 20 45 78 70 20 24  :25:48 drh Exp $
62810 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 46 69 67 75  .*/.../*.** Figu
62820 72 65 20 6f 75 74 20 77 68 61 74 20 76 65 72 73  re out what vers
62830 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 64 65 20  ion of the code 
62840 74 6f 20 75 73 65 2e 20 20 54 68 65 20 63 68 6f  to use.  The cho
62850 69 63 65 73 20 61 72 65 0a 2a 2a 0a 2a 2a 20 20  ices are.**.**  
62860 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4f 4d   SQLITE_MUTEX_OM
62870 49 54 20 20 20 20 20 20 20 20 20 4e 6f 20 6d 75  IT         No mu
62880 74 65 78 20 6c 6f 67 69 63 2e 20 20 4e 6f 74 20  tex logic.  Not 
62890 65 76 65 6e 20 73 74 75 62 73 2e 20 20 54 68 65  even stubs.  The
628a0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
628b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
628c0 6d 75 74 65 78 65 73 20 69 6d 70 6c 65 6d 65 6e  mutexes implemen
628d0 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 6f  tion cannot be o
628e0 76 65 72 72 69 64 64 65 6e 0a 2a 2a 20 20 20 20  verridden.**    
628f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62900 20 20 20 20 20 20 20 20 20 61 74 20 73 74 61 72           at star
62910 74 2d 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  t-time..**.**   
62920 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f  SQLITE_MUTEX_NOO
62930 50 20 20 20 20 20 20 20 20 20 46 6f 72 20 73 69  P         For si
62940 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20 61 70  ngle-threaded ap
62950 70 6c 69 63 61 74 69 6f 6e 73 2e 20 20 4e 6f 0a  plications.  No.
62960 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
62970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
62980 75 74 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 20  utual exclusion 
62990 69 73 20 70 72 6f 76 69 64 65 64 2e 20 20 42 75  is provided.  Bu
629a0 74 20 74 68 69 73 0a 2a 2a 20 20 20 20 20 20 20  t this.**       
629b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
629c0 20 20 20 20 20 20 69 6d 70 6c 65 6d 65 6e 74 61        implementa
629d0 74 69 6f 6e 20 63 61 6e 20 62 65 20 6f 76 65 72  tion can be over
629e0 72 69 64 64 65 6e 20 61 74 0a 2a 2a 20 20 20 20  ridden at.**    
629f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
62a00 20 20 20 20 20 20 20 20 20 73 74 61 72 74 2d 74           start-t
62a10 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c  ime..**.**   SQL
62a20 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
62a30 44 53 20 20 20 20 20 46 6f 72 20 6d 75 6c 74 69  DS     For multi
62a40 2d 74 68 72 65 61 64 65 64 20 61 70 70 6c 69 63  -threaded applic
62a50 61 74 69 6f 6e 73 20 6f 6e 20 55 6e 69 78 2e 0a  ations on Unix..
62a60 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54 45 5f 4d  **.**   SQLITE_M
62a70 55 54 45 58 5f 57 33 32 20 20 20 20 20 20 20 20  UTEX_W32        
62a80 20 20 46 6f 72 20 6d 75 6c 74 69 2d 74 68 72 65    For multi-thre
62a90 61 64 65 64 20 61 70 70 6c 69 63 61 74 69 6f 6e  aded application
62aa0 73 20 6f 6e 20 57 69 6e 33 32 2e 0a 2a 2a 0a 2a  s on Win32..**.*
62ab0 2a 20 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  *   SQLITE_MUTEX
62ac0 5f 4f 53 32 20 20 20 20 20 20 20 20 20 20 46 6f  _OS2          Fo
62ad0 72 20 6d 75 6c 74 69 2d 74 68 72 65 61 64 65 64  r multi-threaded
62ae0 20 61 70 70 6c 69 63 61 74 69 6f 6e 73 20 6f 6e   applications on
62af0 20 4f 53 2f 32 2e 0a 2a 2f 0a 23 69 66 20 21 53   OS/2..*/.#if !S
62b00 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46 45  QLITE_THREADSAFE
62b10 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45  .# define SQLITE
62b20 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 23 65 6e 64  _MUTEX_OMIT.#end
62b30 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  if.#if SQLITE_TH
62b40 52 45 41 44 53 41 46 45 20 26 26 20 21 64 65 66  READSAFE && !def
62b50 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d 55 54 45  ined(SQLITE_MUTE
62b60 58 5f 4e 4f 4f 50 29 0a 23 20 20 69 66 20 53 51  X_NOOP).#  if SQ
62b70 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 0a 23 20 20  LITE_OS_UNIX.#  
62b80 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
62b90 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a 23  MUTEX_PTHREADS.#
62ba0 20 20 65 6c 69 66 20 53 51 4c 49 54 45 5f 4f 53    elif SQLITE_OS
62bb0 5f 57 49 4e 0a 23 20 20 20 20 64 65 66 69 6e 65  _WIN.#    define
62bc0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33   SQLITE_MUTEX_W3
62bd0 32 0a 23 20 20 65 6c 69 66 20 53 51 4c 49 54 45  2.#  elif SQLITE
62be0 5f 4f 53 5f 4f 53 32 0a 23 20 20 20 20 64 65 66  _OS_OS2.#    def
62bf0 69 6e 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ine SQLITE_MUTEX
62c00 5f 4f 53 32 0a 23 20 20 65 6c 73 65 0a 23 20 20  _OS2.#  else.#  
62c10 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
62c20 4d 55 54 45 58 5f 4e 4f 4f 50 0a 23 20 20 65 6e  MUTEX_NOOP.#  en
62c30 64 69 66 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  dif.#endif..#ifd
62c40 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
62c50 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  OMIT./*.** If th
62c60 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 6d  is is a no-op im
62c70 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 69 6d  plementation, im
62c80 70 6c 65 6d 65 6e 74 20 65 76 65 72 79 74 68 69  plement everythi
62c90 6e 67 20 61 73 20 6d 61 63 72 6f 73 2e 0a 2a 2f  ng as macros..*/
62ca0 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
62cb0 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 58 29 20  _mutex_alloc(X) 
62cc0 20 20 20 28 28 73 71 6c 69 74 65 33 5f 6d 75 74     ((sqlite3_mut
62cd0 65 78 2a 29 38 29 0a 23 64 65 66 69 6e 65 20 73  ex*)8).#define s
62ce0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65  qlite3_mutex_fre
62cf0 65 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c  e(X).#define sql
62d00 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
62d10 28 58 29 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  (X).#define sqli
62d20 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 58 29  te3_mutex_try(X)
62d30 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 0a        SQLITE_OK.
62d40 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
62d50 6d 75 74 65 78 5f 6c 65 61 76 65 28 58 29 0a 23  mutex_leave(X).#
62d60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
62d70 75 74 65 78 5f 68 65 6c 64 28 58 29 20 20 20 20  utex_held(X)    
62d80 20 31 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   1.#define sqlit
62d90 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64  e3_mutex_notheld
62da0 28 58 29 20 20 31 0a 23 64 65 66 69 6e 65 20 73  (X)  1.#define s
62db0 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
62dc0 28 58 29 20 20 20 20 20 20 28 28 73 71 6c 69 74  (X)      ((sqlit
62dd0 65 33 5f 6d 75 74 65 78 2a 29 38 29 0a 23 64 65  e3_mutex*)8).#de
62de0 66 69 6e 65 20 73 71 6c 69 74 65 33 4d 75 74 65  fine sqlite3Mute
62df0 78 49 6e 69 74 28 29 20 20 20 20 20 20 20 20 53  xInit()        S
62e00 51 4c 49 54 45 5f 4f 4b 0a 23 64 65 66 69 6e 65  QLITE_OK.#define
62e10 20 73 71 6c 69 74 65 33 4d 75 74 65 78 45 6e 64   sqlite3MutexEnd
62e20 28 29 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66  ().#endif /* def
62e30 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
62e40 5f 4d 55 54 45 58 29 20 2a 2f 0a 0a 2f 2a 2a 2a  _MUTEX) */../***
62e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
62e60 6f 66 20 6d 75 74 65 78 2e 68 20 2a 2a 2a 2a 2a  of mutex.h *****
62e70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
62e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
62ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74  *********** Cont
62eb0 69 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20  inuing where we 
62ec0 6c 65 66 74 20 6f 66 66 20 69 6e 20 73 71 6c 69  left off in sqli
62ed0 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  teInt.h ********
62ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 0a 2f 2a  **********/.../*
62ef0 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62 61 73  .** Each databas
62f00 65 20 66 69 6c 65 20 74 6f 20 62 65 20 61 63 63  e file to be acc
62f10 65 73 73 65 64 20 62 79 20 74 68 65 20 73 79 73  essed by the sys
62f20 74 65 6d 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  tem is an instan
62f30 63 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66 6f 6c  ce.** of the fol
62f40 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
62f50 2e 20 20 54 68 65 72 65 20 61 72 65 20 6e 6f 72  .  There are nor
62f60 6d 61 6c 6c 79 20 74 77 6f 20 6f 66 20 74 68 65  mally two of the
62f70 73 65 20 73 74 72 75 63 74 75 72 65 73 0a 2a 2a  se structures.**
62f80 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 2e 61   in the sqlite.a
62f90 44 62 5b 5d 20 61 72 72 61 79 2e 20 20 61 44 62  Db[] array.  aDb
62fa0 5b 30 5d 20 69 73 20 74 68 65 20 6d 61 69 6e 20  [0] is the main 
62fb0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
62fc0 64 0a 2a 2a 20 61 44 62 5b 31 5d 20 69 73 20 74  d.** aDb[1] is t
62fd0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
62fe0 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20 74 65   used to hold te
62ff0 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20  mporary tables. 
63000 20 41 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20 64   Additional.** d
63010 61 74 61 62 61 73 65 73 20 6d 61 79 20 62 65 20  atabases may be 
63020 61 74 74 61 63 68 65 64 2e 0a 2a 2f 0a 73 74 72  attached..*/.str
63030 75 63 74 20 44 62 20 7b 0a 20 20 63 68 61 72 20  uct Db {.  char 
63040 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
63050 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69 73 20  /* Name of this 
63060 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 42 74  database */.  Bt
63070 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20 20 20  ree *pBt;       
63080 20 20 20 2f 2a 20 54 68 65 20 42 2a 54 72 65 65     /* The B*Tree
63090 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74   structure for t
630a0 68 69 73 20 64 61 74 61 62 61 73 65 20 66 69 6c  his database fil
630b0 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e  e */.  u8 inTran
630c0 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 30  s;          /* 0
630d0 3a 20 6e 6f 74 20 77 72 69 74 61 62 6c 65 2e 20  : not writable. 
630e0 20 31 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e 2e   1: Transaction.
630f0 20 20 32 3a 20 43 68 65 63 6b 70 6f 69 6e 74 20    2: Checkpoint 
63100 2a 2f 0a 20 20 75 38 20 73 61 66 65 74 79 5f 6c  */.  u8 safety_l
63110 65 76 65 6c 3b 20 20 20 20 20 2f 2a 20 48 6f 77  evel;     /* How
63120 20 61 67 67 72 65 73 73 69 76 65 20 61 74 20 73   aggressive at s
63130 79 6e 63 68 69 6e 67 20 64 61 74 61 20 74 6f 20  ynching data to 
63140 64 69 73 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  disk */.  void *
63150 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20 20  pAux;           
63160 20 20 20 20 2f 2a 20 41 75 78 69 6c 69 61 72 79      /* Auxiliary
63170 20 64 61 74 61 2e 20 20 55 73 75 61 6c 6c 79 20   data.  Usually 
63180 4e 55 4c 4c 20 2a 2f 0a 20 20 76 6f 69 64 20 28  NULL */.  void (
63190 2a 78 46 72 65 65 41 75 78 29 28 76 6f 69 64 2a  *xFreeAux)(void*
631a0 29 3b 20 20 2f 2a 20 52 6f 75 74 69 6e 65 20 74  );  /* Routine t
631b0 6f 20 66 72 65 65 20 70 41 75 78 20 2a 2f 0a 20  o free pAux */. 
631c0 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
631d0 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72  ;     /* Pointer
631e0 20 74 6f 20 64 61 74 61 62 61 73 65 20 73 63 68   to database sch
631f0 65 6d 61 20 28 70 6f 73 73 69 62 6c 79 20 73 68  ema (possibly sh
63200 61 72 65 64 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ared) */.};../*.
63210 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
63220 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
63230 73 74 72 75 63 74 75 72 65 20 73 74 6f 72 65 73  structure stores
63240 20 61 20 64 61 74 61 62 61 73 65 20 73 63 68 65   a database sche
63250 6d 61 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ma..**.** If the
63260 72 65 20 61 72 65 20 6e 6f 20 76 69 72 74 75 61  re are no virtua
63270 6c 20 74 61 62 6c 65 73 20 63 6f 6e 66 69 67 75  l tables configu
63280 72 65 64 20 69 6e 20 74 68 69 73 20 73 63 68 65  red in this sche
63290 6d 61 2c 20 74 68 65 0a 2a 2a 20 53 63 68 65 6d  ma, the.** Schem
632a0 61 2e 64 62 20 76 61 72 69 61 62 6c 65 20 69 73  a.db variable is
632b0 20 73 65 74 20 74 6f 20 4e 55 4c 4c 2e 20 41 66   set to NULL. Af
632c0 74 65 72 20 74 68 65 20 66 69 72 73 74 20 76 69  ter the first vi
632d0 72 74 75 61 6c 20 74 61 62 6c 65 0a 2a 2a 20 68  rtual table.** h
632e0 61 73 20 62 65 65 6e 20 61 64 64 65 64 2c 20 69  as been added, i
632f0 74 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e  t is set to poin
63300 74 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  t to the databas
63310 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 0a 2a 2a  e connection .**
63320 20 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20   used to create 
63330 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  the connection. 
63340 4f 6e 63 65 20 61 20 76 69 72 74 75 61 6c 20 74  Once a virtual t
63350 61 62 6c 65 20 68 61 73 20 62 65 65 6e 0a 2a 2a  able has been.**
63360 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 53 63   added to the Sc
63370 68 65 6d 61 20 73 74 72 75 63 74 75 72 65 20 61  hema structure a
63380 6e 64 20 74 68 65 20 53 63 68 65 6d 61 2e 64 62  nd the Schema.db
63390 20 76 61 72 69 61 62 6c 65 20 70 6f 70 75 6c 61   variable popula
633a0 74 65 64 2c 20 0a 2a 2a 20 6f 6e 6c 79 20 74 68  ted, .** only th
633b0 61 74 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  at database conn
633c0 65 63 74 69 6f 6e 20 6d 61 79 20 75 73 65 20 74  ection may use t
633d0 68 65 20 53 63 68 65 6d 61 20 74 6f 20 70 72 65  he Schema to pre
633e0 70 61 72 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65  pare .** stateme
633f0 6e 74 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  nts..*/.struct S
63400 63 68 65 6d 61 20 7b 0a 20 20 69 6e 74 20 73 63  chema {.  int sc
63410 68 65 6d 61 5f 63 6f 6f 6b 69 65 3b 20 20 20 2f  hema_cookie;   /
63420 2a 20 44 61 74 61 62 61 73 65 20 73 63 68 65 6d  * Database schem
63430 61 20 76 65 72 73 69 6f 6e 20 6e 75 6d 62 65 72  a version number
63440 20 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a   for this file *
63450 2f 0a 20 20 48 61 73 68 20 74 62 6c 48 61 73 68  /.  Hash tblHash
63460 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20  ;        /* All 
63470 74 61 62 6c 65 73 20 69 6e 64 65 78 65 64 20 62  tables indexed b
63480 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73 68  y name */.  Hash
63490 20 69 64 78 48 61 73 68 3b 20 20 20 20 20 20 20   idxHash;       
634a0 20 2f 2a 20 41 6c 6c 20 28 6e 61 6d 65 64 29 20   /* All (named) 
634b0 69 6e 64 69 63 65 73 20 69 6e 64 65 78 65 64 20  indices indexed 
634c0 62 79 20 6e 61 6d 65 20 2a 2f 0a 20 20 48 61 73  by name */.  Has
634d0 68 20 74 72 69 67 48 61 73 68 3b 20 20 20 20 20  h trigHash;     
634e0 20 20 2f 2a 20 41 6c 6c 20 74 72 69 67 67 65 72    /* All trigger
634f0 73 20 69 6e 64 65 78 65 64 20 62 79 20 6e 61 6d  s indexed by nam
63500 65 20 2a 2f 0a 20 20 48 61 73 68 20 61 46 4b 65  e */.  Hash aFKe
63510 79 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46  y;          /* F
63520 6f 72 65 69 67 6e 20 6b 65 79 73 20 69 6e 64 65  oreign keys inde
63530 78 65 64 20 62 79 20 74 6f 2d 74 61 62 6c 65 20  xed by to-table 
63540 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 53 65 71  */.  Table *pSeq
63550 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 54 68 65  Tab;      /* The
63560 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65   sqlite_sequence
63570 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 41   table used by A
63580 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 2a 2f 0a  UTOINCREMENT */.
63590 20 20 75 38 20 66 69 6c 65 5f 66 6f 72 6d 61 74    u8 file_format
635a0 3b 20 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  ;      /* Schema
635b0 20 66 6f 72 6d 61 74 20 76 65 72 73 69 6f 6e 20   format version 
635c0 66 6f 72 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  for this file */
635d0 0a 20 20 75 38 20 65 6e 63 3b 20 20 20 20 20 20  .  u8 enc;      
635e0 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78 74 20          /* Text 
635f0 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20 62 79  encoding used by
63600 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
63610 2f 0a 20 20 75 31 36 20 66 6c 61 67 73 3b 20 20  /.  u16 flags;  
63620 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67           /* Flag
63630 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
63640 68 20 74 68 69 73 20 73 63 68 65 6d 61 20 2a 2f  h this schema */
63650 0a 20 20 69 6e 74 20 63 61 63 68 65 5f 73 69 7a  .  int cache_siz
63660 65 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  e;      /* Numbe
63670 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 75 73  r of pages to us
63680 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 2a  e in the cache *
63690 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
636a0 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
636b0 4c 45 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  LE.  sqlite3 *db
636c0 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 22 4f 77  ;         /* "Ow
636d0 6e 65 72 22 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  ner" connection.
636e0 20 53 65 65 20 63 6f 6d 6d 65 6e 74 20 61 62 6f   See comment abo
636f0 76 65 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  ve */.#endif.};.
63700 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63  ./*.** These mac
63710 72 6f 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  ros can be used 
63720 74 6f 20 74 65 73 74 2c 20 73 65 74 2c 20 6f 72  to test, set, or
63730 20 63 6c 65 61 72 20 62 69 74 73 20 69 6e 20 74   clear bits in t
63740 68 65 20 0a 2a 2a 20 44 62 2e 66 6c 61 67 73 20  he .** Db.flags 
63750 66 69 65 6c 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  field..*/.#defin
63760 65 20 44 62 48 61 73 50 72 6f 70 65 72 74 79 28  e DbHasProperty(
63770 44 2c 49 2c 50 29 20 20 20 20 20 28 28 28 44 29  D,I,P)     (((D)
63780 2d 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61  ->aDb[I].pSchema
63790 2d 3e 66 6c 61 67 73 26 28 50 29 29 3d 3d 28 50  ->flags&(P))==(P
637a0 29 29 0a 23 64 65 66 69 6e 65 20 44 62 48 61 73  )).#define DbHas
637b0 41 6e 79 50 72 6f 70 65 72 74 79 28 44 2c 49 2c  AnyProperty(D,I,
637c0 50 29 20 20 28 28 28 44 29 2d 3e 61 44 62 5b 49  P)  (((D)->aDb[I
637d0 5d 2e 70 53 63 68 65 6d 61 2d 3e 66 6c 61 67 73  ].pSchema->flags
637e0 26 28 50 29 29 21 3d 30 29 0a 23 64 65 66 69 6e  &(P))!=0).#defin
637f0 65 20 44 62 53 65 74 50 72 6f 70 65 72 74 79 28  e DbSetProperty(
63800 44 2c 49 2c 50 29 20 20 20 20 20 28 44 29 2d 3e  D,I,P)     (D)->
63810 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d 3e  aDb[I].pSchema->
63820 66 6c 61 67 73 7c 3d 28 50 29 0a 23 64 65 66 69  flags|=(P).#defi
63830 6e 65 20 44 62 43 6c 65 61 72 50 72 6f 70 65 72  ne DbClearProper
63840 74 79 28 44 2c 49 2c 50 29 20 20 20 28 44 29 2d  ty(D,I,P)   (D)-
63850 3e 61 44 62 5b 49 5d 2e 70 53 63 68 65 6d 61 2d  >aDb[I].pSchema-
63860 3e 66 6c 61 67 73 26 3d 7e 28 50 29 0a 0a 2f 2a  >flags&=~(P)../*
63870 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
63880 65 73 20 66 6f 72 20 74 68 65 20 44 42 2e 66 6c  es for the DB.fl
63890 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2a 0a 2a 2a  ags field..**.**
638a0 20 54 68 65 20 44 42 5f 53 63 68 65 6d 61 4c 6f   The DB_SchemaLo
638b0 61 64 65 64 20 66 6c 61 67 20 69 73 20 73 65 74  aded flag is set
638c0 20 61 66 74 65 72 20 74 68 65 20 64 61 74 61 62   after the datab
638d0 61 73 65 20 73 63 68 65 6d 61 20 68 61 73 20 62  ase schema has b
638e0 65 65 6e 0a 2a 2a 20 72 65 61 64 20 69 6e 74 6f  een.** read into
638f0 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74   internal hash t
63900 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 44 42 5f  ables..**.** DB_
63910 55 6e 72 65 73 65 74 56 69 65 77 73 20 6d 65 61  UnresetViews mea
63920 6e 73 20 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d  ns that one or m
63930 6f 72 65 20 76 69 65 77 73 20 68 61 76 65 20 63  ore views have c
63940 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 68 61 74  olumn names that
63950 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 66 69  .** have been fi
63960 6c 6c 65 64 20 6f 75 74 2e 20 20 49 66 20 74 68  lled out.  If th
63970 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73  e schema changes
63980 2c 20 74 68 65 73 65 20 63 6f 6c 75 6d 6e 20 6e  , these column n
63990 61 6d 65 73 20 6d 69 67 68 74 0a 2a 2a 20 63 68  ames might.** ch
639a0 61 6e 67 65 73 20 61 6e 64 20 73 6f 20 74 68 65  anges and so the
639b0 20 76 69 65 77 20 77 69 6c 6c 20 6e 65 65 64 20   view will need 
639c0 74 6f 20 62 65 20 72 65 73 65 74 2e 0a 2a 2f 0a  to be reset..*/.
639d0 23 64 65 66 69 6e 65 20 44 42 5f 53 63 68 65 6d  #define DB_Schem
639e0 61 4c 6f 61 64 65 64 20 20 20 20 30 78 30 30 30  aLoaded    0x000
639f0 31 20 20 2f 2a 20 54 68 65 20 73 63 68 65 6d 61  1  /* The schema
63a00 20 68 61 73 20 62 65 65 6e 20 6c 6f 61 64 65 64   has been loaded
63a10 20 2a 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 55   */.#define DB_U
63a20 6e 72 65 73 65 74 56 69 65 77 73 20 20 20 20 30  nresetViews    0
63a30 78 30 30 30 32 20 20 2f 2a 20 53 6f 6d 65 20 76  x0002  /* Some v
63a40 69 65 77 73 20 68 61 76 65 20 64 65 66 69 6e 65  iews have define
63a50 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a  d column names *
63a60 2f 0a 23 64 65 66 69 6e 65 20 44 42 5f 45 6d 70  /.#define DB_Emp
63a70 74 79 20 20 20 20 20 20 20 20 20 20 20 30 78 30  ty           0x0
63a80 30 30 34 20 20 2f 2a 20 54 68 65 20 66 69 6c 65  004  /* The file
63a90 20 69 73 20 65 6d 70 74 79 20 28 6c 65 6e 67 74   is empty (lengt
63aa0 68 20 30 20 62 79 74 65 73 29 20 2a 2f 0a 0a 2f  h 0 bytes) */../
63ab0 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
63ac0 6f 66 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e  of different kin
63ad0 64 73 20 6f 66 20 74 68 69 6e 67 73 20 74 68 61  ds of things tha
63ae0 74 20 63 61 6e 20 62 65 20 6c 69 6d 69 74 65 64  t can be limited
63af0 0a 2a 2a 20 75 73 69 6e 67 20 74 68 65 20 73 71  .** using the sq
63b00 6c 69 74 65 33 5f 6c 69 6d 69 74 28 29 20 69 6e  lite3_limit() in
63b10 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 23 64 65 66  terface..*/.#def
63b20 69 6e 65 20 53 51 4c 49 54 45 5f 4e 5f 4c 49 4d  ine SQLITE_N_LIM
63b30 49 54 20 28 53 51 4c 49 54 45 5f 4c 49 4d 49 54  IT (SQLITE_LIMIT
63b40 5f 56 41 52 49 41 42 4c 45 5f 4e 55 4d 42 45 52  _VARIABLE_NUMBER
63b50 2b 31 29 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 61  +1)../*.** Looka
63b60 73 69 64 65 20 6d 61 6c 6c 6f 63 20 69 73 20 61  side malloc is a
63b70 20 73 65 74 20 6f 66 20 66 69 78 65 64 2d 73 69   set of fixed-si
63b80 7a 65 20 62 75 66 66 65 72 73 20 74 68 61 74 20  ze buffers that 
63b90 63 61 6e 20 62 65 20 75 73 65 64 0a 2a 2a 20 74  can be used.** t
63ba0 6f 20 73 61 74 69 73 69 66 79 20 73 6d 61 6c 6c  o satisify small
63bb0 20 74 72 61 6e 73 69 65 6e 74 20 6d 65 6d 6f 72   transient memor
63bc0 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 65 71  y allocation req
63bd0 75 65 73 74 73 20 66 6f 72 20 6f 62 6a 65 63 74  uests for object
63be0 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64 20  s.** associated 
63bf0 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c 61  with a particula
63c00 72 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  r database conne
63c10 63 74 69 6f 6e 2e 20 20 54 68 65 20 75 73 65 20  ction.  The use 
63c20 6f 66 0a 2a 2a 20 6c 6f 6f 6b 61 73 69 64 65 20  of.** lookaside 
63c30 6d 61 6c 6c 6f 63 20 70 72 6f 76 69 64 65 73 20  malloc provides 
63c40 61 20 73 69 67 6e 69 66 69 63 61 6e 74 20 70 65  a significant pe
63c50 72 66 6f 72 6d 61 6e 63 65 20 65 6e 68 61 6e 63  rformance enhanc
63c60 65 6d 65 6e 74 0a 2a 2a 20 28 61 70 70 72 6f 78  ement.** (approx
63c70 20 31 30 25 29 20 62 79 20 61 76 6f 69 64 69 6e   10%) by avoidin
63c80 67 20 6e 75 6d 65 72 6f 75 73 20 6d 61 6c 6c 6f  g numerous mallo
63c90 63 2f 66 72 65 65 20 72 65 71 75 65 73 74 73 20  c/free requests 
63ca0 77 68 69 6c 65 20 70 61 72 73 69 6e 67 0a 2a 2a  while parsing.**
63cb0 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 2e   SQL statements.
63cc0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 4c 6f 6f 6b 61  .**.** The Looka
63cd0 73 69 64 65 20 73 74 72 75 63 74 75 72 65 20 68  side structure h
63ce0 6f 6c 64 73 20 63 6f 6e 66 69 67 75 72 61 74 69  olds configurati
63cf0 6f 6e 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  on information a
63d00 62 6f 75 74 20 74 68 65 0a 2a 2a 20 6c 6f 6f 6b  bout the.** look
63d10 61 73 69 64 65 20 6d 61 6c 6c 6f 63 20 73 75 62  aside malloc sub
63d20 73 79 73 74 65 6d 2e 20 20 45 61 63 68 20 61 76  system.  Each av
63d30 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72 79 20 61  ailable memory a
63d40 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 0a 2a 2a 20  llocation in.** 
63d50 74 68 65 20 6c 6f 6f 6b 61 73 69 64 65 20 73 75  the lookaside su
63d60 62 73 79 73 74 65 6d 20 69 73 20 73 74 6f 72 65  bsystem is store
63d70 64 20 6f 6e 20 61 20 6c 69 6e 6b 65 64 20 6c 69  d on a linked li
63d80 73 74 20 6f 66 20 4c 6f 6f 6b 61 73 69 64 65 53  st of LookasideS
63d90 6c 6f 74 0a 2a 2a 20 6f 62 6a 65 63 74 73 2e 0a  lot.** objects..
63da0 2a 2f 0a 73 74 72 75 63 74 20 4c 6f 6f 6b 61 73  */.struct Lookas
63db0 69 64 65 20 7b 0a 20 20 75 31 36 20 73 7a 3b 20  ide {.  u16 sz; 
63dc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
63dd0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20  /* Size of each 
63de0 62 75 66 66 65 72 20 69 6e 20 62 79 74 65 73 20  buffer in bytes 
63df0 2a 2f 0a 20 20 75 38 20 62 45 6e 61 62 6c 65 64  */.  u8 bEnabled
63e00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
63e10 54 72 75 65 20 69 66 20 75 73 65 20 6c 6f 6f 6b  True if use look
63e20 61 73 69 64 65 2e 20 20 46 61 6c 73 65 20 74 6f  aside.  False to
63e30 20 69 67 6e 6f 72 65 20 69 74 20 2a 2f 0a 20 20   ignore it */.  
63e40 75 38 20 62 4d 61 6c 6c 6f 63 65 64 3b 20 20 20  u8 bMalloced;   
63e50 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
63e60 69 66 20 70 53 74 61 72 74 20 6f 62 74 61 69 6e  if pStart obtain
63e70 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f  ed from sqlite3_
63e80 6d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 20 20 69 6e  malloc() */.  in
63e90 74 20 6e 4f 75 74 3b 20 20 20 20 20 20 20 20 20  t nOut;         
63ea0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
63eb0 6f 66 20 62 75 66 66 65 72 73 20 63 75 72 72 65  of buffers curre
63ec0 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74  ntly checked out
63ed0 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 4f 75 74 3b   */.  int mxOut;
63ee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
63ef0 20 48 69 67 68 77 61 74 65 72 20 6d 61 72 6b 20   Highwater mark 
63f00 66 6f 72 20 6e 4f 75 74 20 2a 2f 0a 20 20 4c 6f  for nOut */.  Lo
63f10 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 46 72  okasideSlot *pFr
63f20 65 65 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ee;   /* List of
63f30 20 61 76 61 69 6c 61 62 6c 65 20 62 75 66 66 65   available buffe
63f40 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53  rs */.  void *pS
63f50 74 61 72 74 3b 20 20 20 20 20 20 20 20 20 20 20  tart;           
63f60 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66  /* First byte of
63f70 20 61 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72   available memor
63f80 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 76 6f 69  y space */.  voi
63f90 64 20 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20  d *pEnd;        
63fa0 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 62 79       /* First by
63fb0 74 65 20 70 61 73 74 20 65 6e 64 20 6f 66 20 61  te past end of a
63fc0 76 61 69 6c 61 62 6c 65 20 73 70 61 63 65 20 2a  vailable space *
63fd0 2f 0a 7d 3b 0a 73 74 72 75 63 74 20 4c 6f 6f 6b  /.};.struct Look
63fe0 61 73 69 64 65 53 6c 6f 74 20 7b 0a 20 20 4c 6f  asideSlot {.  Lo
63ff0 6f 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 4e 65  okasideSlot *pNe
64000 78 74 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20 62  xt;    /* Next b
64010 75 66 66 65 72 20 69 6e 20 74 68 65 20 6c 69 73  uffer in the lis
64020 74 20 6f 66 20 66 72 65 65 20 62 75 66 66 65 72  t of free buffer
64030 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  s */.};../*.** A
64040 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20   hash table for 
64050 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74  function definit
64060 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 48 61 73 68  ions..**.** Hash
64070 20 65 61 63 68 20 46 75 6e 63 44 65 66 20 73 74   each FuncDef st
64080 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6f 6e 65  ructure into one
64090 20 6f 66 20 74 68 65 20 46 75 6e 63 44 65 66 48   of the FuncDefH
640a0 61 73 68 2e 61 5b 5d 20 73 6c 6f 74 73 2e 0a 2a  ash.a[] slots..*
640b0 2a 20 43 6f 6c 6c 69 73 69 6f 6e 73 20 61 72 65  * Collisions are
640c0 20 6f 6e 20 74 68 65 20 46 75 6e 63 44 65 66 2e   on the FuncDef.
640d0 70 48 61 73 68 20 63 68 61 69 6e 2e 0a 2a 2f 0a  pHash chain..*/.
640e0 73 74 72 75 63 74 20 46 75 6e 63 44 65 66 48 61  struct FuncDefHa
640f0 73 68 20 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a  sh {.  FuncDef *
64100 61 5b 32 33 5d 3b 20 20 20 20 20 20 20 2f 2a 20  a[23];       /* 
64110 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20 66  Hash table for f
64120 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 7d 3b 0a 0a  unctions */.};..
64130 2f 2a 0a 2a 2a 20 45 61 63 68 20 64 61 74 61 62  /*.** Each datab
64140 61 73 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  ase is an instan
64150 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
64160 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
64170 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  *.** The sqlite.
64180 6c 61 73 74 52 6f 77 69 64 20 72 65 63 6f 72 64  lastRowid record
64190 73 20 74 68 65 20 6c 61 73 74 20 69 6e 73 65 72  s the last inser
641a0 74 20 72 6f 77 69 64 20 67 65 6e 65 72 61 74 65  t rowid generate
641b0 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 65 72  d by an.** inser
641c0 74 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e  t statement.  In
641d0 73 65 72 74 73 20 6f 6e 20 76 69 65 77 73 20 64  serts on views d
641e0 6f 20 6e 6f 74 20 61 66 66 65 63 74 20 69 74 73  o not affect its
641f0 20 76 61 6c 75 65 2e 20 20 45 61 63 68 0a 2a 2a   value.  Each.**
64200 20 74 72 69 67 67 65 72 20 68 61 73 20 69 74 73   trigger has its
64210 20 6f 77 6e 20 63 6f 6e 74 65 78 74 2c 20 73 6f   own context, so
64220 20 74 68 61 74 20 6c 61 73 74 52 6f 77 69 64 20   that lastRowid 
64230 63 61 6e 20 62 65 20 75 70 64 61 74 65 64 20 69  can be updated i
64240 6e 73 69 64 65 0a 2a 2a 20 74 72 69 67 67 65 72  nside.** trigger
64250 73 20 61 73 20 75 73 75 61 6c 2e 20 20 54 68 65  s as usual.  The
64260 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
64270 77 69 6c 6c 20 62 65 20 72 65 73 74 6f 72 65 64  will be restored
64280 20 6f 6e 63 65 20 74 68 65 20 74 72 69 67 67 65   once the trigge
64290 72 0a 2a 2a 20 65 78 69 74 73 2e 20 20 55 70 6f  r.** exits.  Upo
642a0 6e 20 65 6e 74 65 72 69 6e 67 20 61 20 62 65 66  n entering a bef
642b0 6f 72 65 20 6f 72 20 69 6e 73 74 65 61 64 20 6f  ore or instead o
642c0 66 20 74 72 69 67 67 65 72 2c 20 6c 61 73 74 52  f trigger, lastR
642d0 6f 77 69 64 20 69 73 20 6e 6f 0a 2a 2a 20 6c 6f  owid is no.** lo
642e0 6e 67 65 72 20 28 73 69 6e 63 65 20 61 66 74 65  nger (since afte
642f0 72 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e 31 32  r version 2.8.12
64300 29 20 72 65 73 65 74 20 74 6f 20 2d 31 2e 0a 2a  ) reset to -1..*
64310 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 2e  *.** The sqlite.
64320 6e 43 68 61 6e 67 65 20 64 6f 65 73 20 6e 6f 74  nChange does not
64330 20 63 6f 75 6e 74 20 63 68 61 6e 67 65 73 20 77   count changes w
64340 69 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 61  ithin triggers a
64350 6e 64 20 6b 65 65 70 73 20 6e 6f 0a 2a 2a 20 63  nd keeps no.** c
64360 6f 6e 74 65 78 74 2e 20 20 49 74 20 69 73 20 72  ontext.  It is r
64370 65 73 65 74 20 61 74 20 73 74 61 72 74 20 6f 66  eset at start of
64380 20 73 71 6c 69 74 65 33 5f 65 78 65 63 2e 0a 2a   sqlite3_exec..*
64390 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 6c 73 43  * The sqlite.lsC
643a0 68 61 6e 67 65 20 72 65 70 72 65 73 65 6e 74 73  hange represents
643b0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   the number of c
643c0 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74  hanges made by t
643d0 68 65 20 6c 61 73 74 0a 2a 2a 20 69 6e 73 65 72  he last.** inser
643e0 74 2c 20 75 70 64 61 74 65 2c 20 6f 72 20 64 65  t, update, or de
643f0 6c 65 74 65 20 73 74 61 74 65 6d 65 6e 74 2e 20  lete statement. 
64400 20 49 74 20 72 65 6d 61 69 6e 73 20 63 6f 6e 73   It remains cons
64410 74 61 6e 74 20 74 68 72 6f 75 67 68 6f 75 74 20  tant throughout 
64420 74 68 65 0a 2a 2a 20 6c 65 6e 67 74 68 20 6f 66  the.** length of
64430 20 61 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64   a statement and
64440 20 69 73 20 74 68 65 6e 20 75 70 64 61 74 65 64   is then updated
64450 20 62 79 20 4f 50 5f 53 65 74 43 6f 75 6e 74 73   by OP_SetCounts
64460 2e 20 20 49 74 20 6b 65 65 70 73 20 61 0a 2a 2a  .  It keeps a.**
64470 20 63 6f 6e 74 65 78 74 20 73 74 61 63 6b 20 6a   context stack j
64480 75 73 74 20 6c 69 6b 65 20 6c 61 73 74 52 6f 77  ust like lastRow
64490 69 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63  id so that the c
644a0 6f 75 6e 74 20 6f 66 20 63 68 61 6e 67 65 73 0a  ount of changes.
644b0 2a 2a 20 77 69 74 68 69 6e 20 61 20 74 72 69 67  ** within a trig
644c0 67 65 72 20 69 73 20 6e 6f 74 20 73 65 65 6e 20  ger is not seen 
644d0 6f 75 74 73 69 64 65 20 74 68 65 20 74 72 69 67  outside the trig
644e0 67 65 72 2e 20 20 43 68 61 6e 67 65 73 20 74 6f  ger.  Changes to
644f0 20 76 69 65 77 73 20 64 6f 20 6e 6f 74 0a 2a 2a   views do not.**
64500 20 61 66 66 65 63 74 20 74 68 65 20 76 61 6c 75   affect the valu
64510 65 20 6f 66 20 6c 73 43 68 61 6e 67 65 2e 0a 2a  e of lsChange..*
64520 2a 20 54 68 65 20 73 71 6c 69 74 65 2e 63 73 43  * The sqlite.csC
64530 68 61 6e 67 65 20 6b 65 65 70 73 20 74 72 61 63  hange keeps trac
64540 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  k of the number 
64550 6f 66 20 63 75 72 72 65 6e 74 20 63 68 61 6e 67  of current chang
64560 65 73 20 28 73 69 6e 63 65 0a 2a 2a 20 74 68 65  es (since.** the
64570 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74 29   last statement)
64580 20 61 6e 64 20 69 73 20 75 73 65 64 20 74 6f 20   and is used to 
64590 75 70 64 61 74 65 20 73 71 6c 69 74 65 5f 6c 73  update sqlite_ls
645a0 43 68 61 6e 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  Change..**.** Th
645b0 65 20 6d 65 6d 62 65 72 20 76 61 72 69 61 62 6c  e member variabl
645c0 65 73 20 73 71 6c 69 74 65 2e 65 72 72 43 6f 64  es sqlite.errCod
645d0 65 2c 20 73 71 6c 69 74 65 2e 7a 45 72 72 4d 73  e, sqlite.zErrMs
645e0 67 20 61 6e 64 20 73 71 6c 69 74 65 2e 7a 45 72  g and sqlite.zEr
645f0 72 4d 73 67 31 36 0a 2a 2a 20 73 74 6f 72 65 20  rMsg16.** store 
64600 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20  the most recent 
64610 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64 2c 20  error code and, 
64620 69 66 20 61 70 70 6c 69 63 61 62 6c 65 2c 20 73  if applicable, s
64630 74 72 69 6e 67 2e 20 54 68 65 0a 2a 2a 20 69 6e  tring. The.** in
64640 74 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20  ternal function 
64650 73 71 6c 69 74 65 33 45 72 72 6f 72 28 29 20 69  sqlite3Error() i
64660 73 20 75 73 65 64 20 74 6f 20 73 65 74 20 74 68  s used to set th
64670 65 73 65 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a  ese variables.**
64680 20 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 0a 2a   consistently..*
64690 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  /.struct sqlite3
646a0 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73   {.  sqlite3_vfs
646b0 20 2a 70 56 66 73 3b 20 20 20 20 20 20 20 20 20   *pVfs;         
646c0 20 20 20 2f 2a 20 4f 53 20 49 6e 74 65 72 66 61     /* OS Interfa
646d0 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 62 3b  ce */.  int nDb;
646e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
646f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
64700 6f 66 20 62 61 63 6b 65 6e 64 73 20 63 75 72 72  of backends curr
64710 65 6e 74 6c 79 20 69 6e 20 75 73 65 20 2a 2f 0a  ently in use */.
64720 20 20 44 62 20 2a 61 44 62 3b 20 20 20 20 20 20    Db *aDb;      
64730 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64740 2f 2a 20 41 6c 6c 20 62 61 63 6b 65 6e 64 73 20  /* All backends 
64750 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20  */.  int flags; 
64760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64770 20 20 20 2f 2a 20 4d 69 73 63 65 6c 6c 61 6e 6f     /* Miscellano
64780 75 73 20 66 6c 61 67 73 2e 20 53 65 65 20 62 65  us flags. See be
64790 6c 6f 77 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 65  low */.  int ope
647a0 6e 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20  nFlags;         
647b0 20 20 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20         /* Flags 
647c0 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
647d0 33 5f 76 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f  3_vfs.xOpen() */
647e0 0a 20 20 69 6e 74 20 65 72 72 43 6f 64 65 3b 20  .  int errCode; 
647f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64800 20 2f 2a 20 4d 6f 73 74 20 72 65 63 65 6e 74 20   /* Most recent 
64810 65 72 72 6f 72 20 63 6f 64 65 20 28 53 51 4c 49  error code (SQLI
64820 54 45 5f 2a 29 20 2a 2f 0a 20 20 69 6e 74 20 65  TE_*) */.  int e
64830 72 72 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20  rrMask;         
64840 20 20 20 20 20 20 20 20 20 2f 2a 20 26 20 72 65           /* & re
64850 73 75 6c 74 20 63 6f 64 65 73 20 77 69 74 68 20  sult codes with 
64860 74 68 69 73 20 62 65 66 6f 72 65 20 72 65 74 75  this before retu
64870 72 6e 69 6e 67 20 2a 2f 0a 20 20 75 38 20 61 75  rning */.  u8 au
64880 74 6f 43 6f 6d 6d 69 74 3b 20 20 20 20 20 20 20  toCommit;       
64890 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
648a0 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61 67  auto-commit flag
648b0 2e 20 2a 2f 0a 20 20 75 38 20 74 65 6d 70 5f 73  . */.  u8 temp_s
648c0 74 6f 72 65 3b 20 20 20 20 20 20 20 20 20 20 20  tore;           
648d0 20 20 20 20 20 2f 2a 20 31 3a 20 66 69 6c 65 20       /* 1: file 
648e0 32 3a 20 6d 65 6d 6f 72 79 20 30 3a 20 64 65 66  2: memory 0: def
648f0 61 75 6c 74 20 2a 2f 0a 20 20 75 38 20 6d 61 6c  ault */.  u8 mal
64900 6c 6f 63 46 61 69 6c 65 64 3b 20 20 20 20 20 20  locFailed;      
64910 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
64920 69 66 20 77 65 20 68 61 76 65 20 73 65 65 6e 20  if we have seen 
64930 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  a malloc failure
64940 20 2a 2f 0a 20 20 75 38 20 64 66 6c 74 4c 6f 63   */.  u8 dfltLoc
64950 6b 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20  kMode;          
64960 20 20 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c      /* Default l
64970 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20 66 6f 72 20  ocking-mode for 
64980 61 74 74 61 63 68 65 64 20 64 62 73 20 2a 2f 0a  attached dbs */.
64990 20 20 75 38 20 64 66 6c 74 4a 6f 75 72 6e 61 6c    u8 dfltJournal
649a0 4d 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Mode;           
649b0 2f 2a 20 44 65 66 61 75 6c 74 20 6a 6f 75 72 6e  /* Default journ
649c0 61 6c 20 6d 6f 64 65 20 66 6f 72 20 61 74 74 61  al mode for atta
649d0 63 68 65 64 20 64 62 73 20 2a 2f 0a 20 20 73 69  ched dbs */.  si
649e0 67 6e 65 64 20 63 68 61 72 20 6e 65 78 74 41 75  gned char nextAu
649f0 74 6f 76 61 63 3b 20 20 20 20 20 20 2f 2a 20 41  tovac;      /* A
64a00 75 74 6f 76 61 63 20 73 65 74 74 69 6e 67 20 61  utovac setting a
64a10 66 74 65 72 20 56 41 43 55 55 4d 20 69 66 20 3e  fter VACUUM if >
64a20 3d 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74  =0 */.  int next
64a30 50 61 67 65 73 69 7a 65 3b 20 20 20 20 20 20 20  Pagesize;       
64a40 20 20 20 20 20 20 2f 2a 20 50 61 67 65 73 69 7a        /* Pagesiz
64a50 65 20 61 66 74 65 72 20 56 41 43 55 55 4d 20 69  e after VACUUM i
64a60 66 20 3e 30 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  f >0 */.  int nT
64a70 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20  able;           
64a80 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
64a90 72 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20 74  r of tables in t
64aa0 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  he database */. 
64ab0 20 43 6f 6c 6c 53 65 71 20 2a 70 44 66 6c 74 43   CollSeq *pDfltC
64ac0 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f  oll;           /
64ad0 2a 20 54 68 65 20 64 65 66 61 75 6c 74 20 63 6f  * The default co
64ae0 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
64af0 20 28 42 49 4e 41 52 59 29 20 2a 2f 0a 20 20 69   (BINARY) */.  i
64b00 36 34 20 6c 61 73 74 52 6f 77 69 64 3b 20 20 20  64 lastRowid;   
64b10 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
64b20 52 4f 57 49 44 20 6f 66 20 6d 6f 73 74 20 72 65  ROWID of most re
64b30 63 65 6e 74 20 69 6e 73 65 72 74 20 28 73 65 65  cent insert (see
64b40 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 69 36 34   above) */.  i64
64b50 20 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 3b 20   priorNewRowid; 
64b60 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
64b70 73 74 20 72 61 6e 64 6f 6d 6c 79 20 67 65 6e 65  st randomly gene
64b80 72 61 74 65 64 20 52 4f 57 49 44 20 2a 2f 0a 20  rated ROWID */. 
64b90 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20 20   u32 magic;     
64ba0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
64bb0 2a 20 4d 61 67 69 63 20 6e 75 6d 62 65 72 20 66  * Magic number f
64bc0 6f 72 20 64 65 74 65 63 74 20 6c 69 62 72 61 72  or detect librar
64bd0 79 20 6d 69 73 75 73 65 20 2a 2f 0a 20 20 69 6e  y misuse */.  in
64be0 74 20 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20  t nChange;      
64bf0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56              /* V
64c00 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79  alue returned by
64c10 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
64c20 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f 74  () */.  int nTot
64c30 61 6c 43 68 61 6e 67 65 3b 20 20 20 20 20 20 20  alChange;       
64c40 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72        /* Value r
64c50 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74  eturned by sqlit
64c60 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
64c70 28 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  () */.  sqlite3_
64c80 6d 75 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20  mutex *mutex;   
64c90 20 20 20 20 20 20 2f 2a 20 43 6f 6e 6e 65 63 74        /* Connect
64ca0 69 6f 6e 20 6d 75 74 65 78 20 2a 2f 0a 20 20 69  ion mutex */.  i
64cb0 6e 74 20 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  nt aLimit[SQLITE
64cc0 5f 4e 5f 4c 49 4d 49 54 5d 3b 20 20 20 2f 2a 20  _N_LIMIT];   /* 
64cd0 4c 69 6d 69 74 73 20 2a 2f 0a 20 20 73 74 72 75  Limits */.  stru
64ce0 63 74 20 73 71 6c 69 74 65 33 49 6e 69 74 49 6e  ct sqlite3InitIn
64cf0 66 6f 20 7b 20 20 20 20 20 20 2f 2a 20 49 6e 66  fo {      /* Inf
64d00 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 64 75  ormation used du
64d10 72 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a 61 74  ring initializat
64d20 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ion */.    int i
64d30 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
64d40 20 20 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 62         /* When b
64d50 61 63 6b 20 69 73 20 62 65 69 6e 67 20 69 6e 69  ack is being ini
64d60 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 20 20  tialized */.    
64d70 69 6e 74 20 6e 65 77 54 6e 75 6d 3b 20 20 20 20  int newTnum;    
64d80 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
64d90 6f 6f 74 70 61 67 65 20 6f 66 20 74 61 62 6c 65  ootpage of table
64da0 20 62 65 69 6e 67 20 69 6e 69 74 69 61 6c 69 7a   being initializ
64db0 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 62 75 73  ed */.    u8 bus
64dc0 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
64dd0 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 69 66        /* TRUE if
64de0 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 69 74 69   currently initi
64df0 61 6c 69 7a 69 6e 67 20 2a 2f 0a 20 20 7d 20 69  alizing */.  } i
64e00 6e 69 74 3b 0a 20 20 69 6e 74 20 6e 45 78 74 65  nit;.  int nExte
64e10 6e 73 69 6f 6e 3b 20 20 20 20 20 20 20 20 20 20  nsion;          
64e20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
64e30 66 20 6c 6f 61 64 65 64 20 65 78 74 65 6e 73 69  f loaded extensi
64e40 6f 6e 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 2a  ons */.  void **
64e50 61 45 78 74 65 6e 73 69 6f 6e 3b 20 20 20 20 20  aExtension;     
64e60 20 20 20 20 20 20 20 2f 2a 20 41 72 72 61 79 20         /* Array 
64e70 6f 66 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  of shared librar
64e80 61 79 20 68 61 6e 64 6c 65 73 20 2a 2f 0a 20 20  ay handles */.  
64e90 73 74 72 75 63 74 20 56 64 62 65 20 2a 70 56 64  struct Vdbe *pVd
64ea0 62 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  be;           /*
64eb0 20 4c 69 73 74 20 6f 66 20 61 63 74 69 76 65 20   List of active 
64ec0 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 73  virtual machines
64ed0 20 2a 2f 0a 20 20 69 6e 74 20 61 63 74 69 76 65   */.  int active
64ee0 56 64 62 65 43 6e 74 3b 20 20 20 20 20 20 20 20  VdbeCnt;        
64ef0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
64f00 20 76 64 62 65 73 20 63 75 72 72 65 6e 74 6c 79   vdbes currently
64f10 20 65 78 65 63 75 74 69 6e 67 20 2a 2f 0a 20 20   executing */.  
64f20 69 6e 74 20 77 72 69 74 65 56 64 62 65 43 6e 74  int writeVdbeCnt
64f30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
64f40 20 4e 75 6d 62 65 72 20 6f 66 20 61 63 74 69 76   Number of activ
64f50 65 20 56 44 42 45 73 20 74 68 61 74 20 61 72 65  e VDBEs that are
64f60 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 76 6f   writing */.  vo
64f70 69 64 20 28 2a 78 54 72 61 63 65 29 28 76 6f 69  id (*xTrace)(voi
64f80 64 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  d*,const char*);
64f90 20 20 20 20 20 20 20 20 2f 2a 20 54 72 61 63 65          /* Trace
64fa0 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 76   function */.  v
64fb0 6f 69 64 20 2a 70 54 72 61 63 65 41 72 67 3b 20  oid *pTraceArg; 
64fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
64fd0 20 20 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75           /* Argu
64fe0 6d 65 6e 74 20 74 6f 20 74 68 65 20 74 72 61 63  ment to the trac
64ff0 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  e function */.  
65000 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c 65 29  void (*xProfile)
65010 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63 68 61  (void*,const cha
65020 72 2a 2c 75 36 34 29 3b 20 20 2f 2a 20 50 72 6f  r*,u64);  /* Pro
65030 66 69 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  filing function 
65040 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50 72 6f 66  */.  void *pProf
65050 69 6c 65 41 72 67 3b 20 20 20 20 20 20 20 20 20  ileArg;         
65060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
65070 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 70 72  * Argument to pr
65080 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 2a  ofile function *
65090 2f 0a 20 20 76 6f 69 64 20 2a 70 43 6f 6d 6d 69  /.  void *pCommi
650a0 74 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  tArg;           
650b0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
650c0 74 20 74 6f 20 78 43 6f 6d 6d 69 74 43 61 6c 6c  t to xCommitCall
650d0 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 69  back() */   .  i
650e0 6e 74 20 28 2a 78 43 6f 6d 6d 69 74 43 61 6c 6c  nt (*xCommitCall
650f0 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b 20 20 20  back)(void*);   
65100 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65   /* Invoked at e
65110 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a  very commit. */.
65120 20 20 76 6f 69 64 20 2a 70 52 6f 6c 6c 62 61 63    void *pRollbac
65130 6b 41 72 67 3b 20 20 20 20 20 20 20 20 20 20 20  kArg;           
65140 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
65150 74 6f 20 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  to xRollbackCall
65160 62 61 63 6b 28 29 20 2a 2f 20 20 20 0a 20 20 76  back() */   .  v
65170 6f 69 64 20 28 2a 78 52 6f 6c 6c 62 61 63 6b 43  oid (*xRollbackC
65180 61 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 29 3b  allback)(void*);
65190 20 2f 2a 20 49 6e 76 6f 6b 65 64 20 61 74 20 65   /* Invoked at e
651a0 76 65 72 79 20 63 6f 6d 6d 69 74 2e 20 2a 2f 0a  very commit. */.
651b0 20 20 76 6f 69 64 20 2a 70 55 70 64 61 74 65 41    void *pUpdateA
651c0 72 67 3b 0a 20 20 76 6f 69 64 20 28 2a 78 55 70  rg;.  void (*xUp
651d0 64 61 74 65 43 61 6c 6c 62 61 63 6b 29 28 76 6f  dateCallback)(vo
651e0 69 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 63  id*,int, const c
651f0 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
65200 2c 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 3b 0a  ,sqlite_int64);.
65210 20 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65    void(*xCollNee
65220 64 65 64 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  ded)(void*,sqlit
65230 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
65240 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
65250 20 76 6f 69 64 28 2a 78 43 6f 6c 6c 4e 65 65 64   void(*xCollNeed
65260 65 64 31 36 29 28 76 6f 69 64 2a 2c 73 71 6c 69  ed16)(void*,sqli
65270 74 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65  te3*,int eTextRe
65280 70 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 3b 0a  p,const void*);.
65290 20 20 76 6f 69 64 20 2a 70 43 6f 6c 6c 4e 65 65    void *pCollNee
652a0 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74 65  dedArg;.  sqlite
652b0 33 5f 76 61 6c 75 65 20 2a 70 45 72 72 3b 20 20  3_value *pErr;  
652c0 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20          /* Most 
652d0 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73  recent error mes
652e0 73 61 67 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  sage */.  char *
652f0 7a 45 72 72 4d 73 67 3b 20 20 20 20 20 20 20 20  zErrMsg;        
65300 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20          /* Most 
65310 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73  recent error mes
65320 73 61 67 65 20 28 55 54 46 2d 38 20 65 6e 63 6f  sage (UTF-8 enco
65330 64 65 64 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ded) */.  char *
65340 7a 45 72 72 4d 73 67 31 36 3b 20 20 20 20 20 20  zErrMsg16;      
65350 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 73 74 20          /* Most 
65360 72 65 63 65 6e 74 20 65 72 72 6f 72 20 6d 65 73  recent error mes
65370 73 61 67 65 20 28 55 54 46 2d 31 36 20 65 6e 63  sage (UTF-16 enc
65380 6f 64 65 64 29 20 2a 2f 0a 20 20 75 6e 69 6f 6e  oded) */.  union
65390 20 7b 0a 20 20 20 20 76 6f 6c 61 74 69 6c 65 20   {.    volatile 
653a0 69 6e 74 20 69 73 49 6e 74 65 72 72 75 70 74 65  int isInterrupte
653b0 64 3b 20 2f 2a 20 54 72 75 65 20 69 66 20 73 71  d; /* True if sq
653c0 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20  lite3_interrupt 
653d0 68 61 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  has been called 
653e0 2a 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 6e 6f  */.    double no
653f0 74 55 73 65 64 31 3b 20 20 20 20 20 20 20 20 20  tUsed1;         
65400 20 20 20 2f 2a 20 53 70 61 63 65 72 20 2a 2f 0a     /* Spacer */.
65410 20 20 7d 20 75 31 3b 0a 20 20 4c 6f 6f 6b 61 73    } u1;.  Lookas
65420 69 64 65 20 6c 6f 6f 6b 61 73 69 64 65 3b 20 20  ide lookaside;  
65430 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 6b 61          /* Looka
65440 73 69 64 65 20 6d 61 6c 6c 6f 63 20 63 6f 6e 66  side malloc conf
65450 69 67 75 72 61 74 69 6f 6e 20 2a 2f 0a 23 69 66  iguration */.#if
65460 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
65470 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20  _AUTHORIZATION. 
65480 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76 6f   int (*xAuth)(vo
65490 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68  id*,int,const ch
654a0 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ar*,const char*,
654b0 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
654c0 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20  t char*);.      
654d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
654e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 63 63            /* Acc
654f0 65 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f  ess authorizatio
65500 6e 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20  n function */.  
65510 76 6f 69 64 20 2a 70 41 75 74 68 41 72 67 3b 20  void *pAuthArg; 
65520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
65530 20 31 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f   1st argument to
65540 20 74 68 65 20 61 63 63 65 73 73 20 61 75 74 68   the access auth
65550 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 23 65 6e   function */.#en
65560 64 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  dif.#ifndef SQLI
65570 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52 45 53 53  TE_OMIT_PROGRESS
65580 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69 6e 74 20  _CALLBACK.  int 
65590 28 2a 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69  (*xProgress)(voi
655a0 64 20 2a 29 3b 20 20 20 20 20 2f 2a 20 54 68 65  d *);     /* The
655b0 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62 61   progress callba
655c0 63 6b 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 50  ck */.  void *pP
655d0 72 6f 67 72 65 73 73 41 72 67 3b 20 20 20 20 20  rogressArg;     
655e0 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e        /* Argumen
655f0 74 20 74 6f 20 74 68 65 20 70 72 6f 67 72 65 73  t to the progres
65600 73 20 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20  s callback */.  
65610 69 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73  int nProgressOps
65620 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
65630 20 4e 75 6d 62 65 72 20 6f 66 20 6f 70 63 6f 64   Number of opcod
65640 65 73 20 66 6f 72 20 70 72 6f 67 72 65 73 73 20  es for progress 
65650 63 61 6c 6c 62 61 63 6b 20 2a 2f 0a 23 65 6e 64  callback */.#end
65660 69 66 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  if.#ifndef SQLIT
65670 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
65680 42 4c 45 0a 20 20 48 61 73 68 20 61 4d 6f 64 75  BLE.  Hash aModu
65690 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  le;             
656a0 20 20 20 20 2f 2a 20 70 6f 70 75 6c 61 74 65 64      /* populated
656b0 20 62 79 20 73 71 6c 69 74 65 33 5f 63 72 65 61   by sqlite3_crea
656c0 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20  te_module() */. 
656d0 20 54 61 62 6c 65 20 2a 70 56 54 61 62 3b 20 20   Table *pVTab;  
656e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
656f0 2a 20 76 74 61 62 20 77 69 74 68 20 61 63 74 69  * vtab with acti
65700 76 65 20 43 6f 6e 6e 65 63 74 2f 43 72 65 61 74  ve Connect/Creat
65710 65 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 73 71  e method */.  sq
65720 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 61 56 54  lite3_vtab **aVT
65730 72 61 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 56  rans;       /* V
65740 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 77 69  irtual tables wi
65750 74 68 20 6f 70 65 6e 20 74 72 61 6e 73 61 63 74  th open transact
65760 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 56  ions */.  int nV
65770 54 72 61 6e 73 3b 20 20 20 20 20 20 20 20 20 20  Trans;          
65780 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 6f 63          /* Alloc
65790 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61 56 54  ated size of aVT
657a0 72 61 6e 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  rans */.#endif. 
657b0 20 46 75 6e 63 44 65 66 48 61 73 68 20 61 46 75   FuncDefHash aFu
657c0 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  nc;            /
657d0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20  * Hash table of 
657e0 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 75 6e 63 74  connection funct
657f0 69 6f 6e 73 20 2a 2f 0a 20 20 48 61 73 68 20 61  ions */.  Hash a
65800 43 6f 6c 6c 53 65 71 3b 20 20 20 20 20 20 20 20  CollSeq;        
65810 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c 20 63          /* All c
65820 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
65830 65 73 20 2a 2f 0a 20 20 42 75 73 79 48 61 6e 64  es */.  BusyHand
65840 6c 65 72 20 62 75 73 79 48 61 6e 64 6c 65 72 3b  ler busyHandler;
65850 20 20 20 20 20 20 2f 2a 20 42 75 73 79 20 63 61        /* Busy ca
65860 6c 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20  llback */.  int 
65870 62 75 73 79 54 69 6d 65 6f 75 74 3b 20 20 20 20  busyTimeout;    
65880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 75 73            /* Bus
65890 79 20 68 61 6e 64 6c 65 72 20 74 69 6d 65 6f 75  y handler timeou
658a0 74 2c 20 69 6e 20 6d 73 65 63 20 2a 2f 0a 20 20  t, in msec */.  
658b0 44 62 20 61 44 62 53 74 61 74 69 63 5b 32 5d 3b  Db aDbStatic[2];
658c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
658d0 20 53 74 61 74 69 63 20 73 70 61 63 65 20 66 6f   Static space fo
658e0 72 20 74 68 65 20 32 20 64 65 66 61 75 6c 74 20  r the 2 default 
658f0 62 61 63 6b 65 6e 64 73 20 2a 2f 0a 23 69 66 64  backends */.#ifd
65900 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 20 20  ef SQLITE_SSE.  
65910 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 46  sqlite3_stmt *pF
65920 65 74 63 68 3b 20 20 20 20 20 20 20 20 20 2f 2a  etch;         /*
65930 20 55 73 65 64 20 62 79 20 53 53 45 20 74 6f 20   Used by SSE to 
65940 66 65 74 63 68 20 73 74 6f 72 65 64 20 73 74 61  fetch stored sta
65950 74 65 6d 65 6e 74 73 20 2a 2f 0a 23 65 6e 64 69  tements */.#endi
65960 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 6d 61  f.};../*.** A ma
65970 63 72 6f 20 74 6f 20 64 69 73 63 6f 76 65 72 20  cro to discover 
65980 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  the encoding of 
65990 61 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a 23  a database..*/.#
659a0 64 65 66 69 6e 65 20 45 4e 43 28 64 62 29 20 28  define ENC(db) (
659b0 28 64 62 29 2d 3e 61 44 62 5b 30 5d 2e 70 53 63  (db)->aDb[0].pSc
659c0 68 65 6d 61 2d 3e 65 6e 63 29 0a 0a 2f 2a 0a 2a  hema->enc)../*.*
659d0 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
659e0 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69 74 65  s for the sqlite
659f0 2e 66 6c 61 67 73 20 61 6e 64 20 6f 72 20 44 62  .flags and or Db
65a00 2e 66 6c 61 67 73 20 66 69 65 6c 64 73 2e 0a 2a  .flags fields..*
65a10 2a 0a 2a 2a 20 4f 6e 20 73 71 6c 69 74 65 2e 66  *.** On sqlite.f
65a20 6c 61 67 73 2c 20 74 68 65 20 53 51 4c 49 54 45  lags, the SQLITE
65a30 5f 49 6e 54 72 61 6e 73 20 76 61 6c 75 65 20 6d  _InTrans value m
65a40 65 61 6e 73 20 74 68 61 74 20 77 65 20 68 61 76  eans that we hav
65a50 65 0a 2a 2a 20 65 78 65 63 75 74 65 64 20 61 20  e.** executed a 
65a60 42 45 47 49 4e 2e 20 20 4f 6e 20 44 62 2e 66 6c  BEGIN.  On Db.fl
65a70 61 67 73 2c 20 53 51 4c 49 54 45 5f 49 6e 54 72  ags, SQLITE_InTr
65a80 61 6e 73 20 6d 65 61 6e 73 20 61 20 73 74 61 74  ans means a stat
65a90 65 6d 65 6e 74 0a 2a 2a 20 74 72 61 6e 73 61 63  ement.** transac
65aa0 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 6f  tion is active o
65ab0 6e 20 74 68 61 74 20 70 61 72 74 69 63 75 6c 61  n that particula
65ac0 72 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  r database file.
65ad0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49  .*/.#define SQLI
65ae0 54 45 5f 56 64 62 65 54 72 61 63 65 20 20 20 20  TE_VdbeTrace    
65af0 20 20 30 78 30 30 30 30 30 30 30 31 20 20 2f 2a    0x00000001  /*
65b00 20 54 72 75 65 20 74 6f 20 74 72 61 63 65 20 56   True to trace V
65b10 44 42 45 20 65 78 65 63 75 74 69 6f 6e 20 2a 2f  DBE execution */
65b20 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
65b30 49 6e 54 72 61 6e 73 20 20 20 20 20 20 20 20 30  InTrans        0
65b40 78 30 30 30 30 30 30 30 38 20 20 2f 2a 20 54 72  x00000008  /* Tr
65b50 75 65 20 69 66 20 69 6e 20 61 20 74 72 61 6e 73  ue if in a trans
65b60 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  action */.#defin
65b70 65 20 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43  e SQLITE_InternC
65b80 68 61 6e 67 65 73 20 20 30 78 30 30 30 30 30 30  hanges  0x000000
65b90 31 30 20 20 2f 2a 20 55 6e 63 6f 6d 6d 69 74 74  10  /* Uncommitt
65ba0 65 64 20 48 61 73 68 20 74 61 62 6c 65 20 63 68  ed Hash table ch
65bb0 61 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65  anges */.#define
65bc0 20 53 51 4c 49 54 45 5f 46 75 6c 6c 43 6f 6c 4e   SQLITE_FullColN
65bd0 61 6d 65 73 20 20 20 30 78 30 30 30 30 30 30 32  ames   0x0000002
65be0 30 20 20 2f 2a 20 53 68 6f 77 20 66 75 6c 6c 20  0  /* Show full 
65bf0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f 6e 20  column names on 
65c00 53 45 4c 45 43 54 20 2a 2f 0a 23 64 65 66 69 6e  SELECT */.#defin
65c10 65 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  e SQLITE_ShortCo
65c20 6c 4e 61 6d 65 73 20 20 30 78 30 30 30 30 30 30  lNames  0x000000
65c30 34 30 20 20 2f 2a 20 53 68 6f 77 20 73 68 6f 72  40  /* Show shor
65c40 74 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73 20  t columns names 
65c50 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
65c60 45 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20  E_CountRows     
65c70 20 30 78 30 30 30 30 30 30 38 30 20 20 2f 2a 20   0x00000080  /* 
65c80 43 6f 75 6e 74 20 72 6f 77 73 20 63 68 61 6e 67  Count rows chang
65c90 65 64 20 62 79 20 49 4e 53 45 52 54 2c 20 2a 2f  ed by INSERT, */
65ca0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
65cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65cc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20             /*   
65cd0 44 45 4c 45 54 45 2c 20 6f 72 20 55 50 44 41 54  DELETE, or UPDAT
65ce0 45 20 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a  E and return */.
65cf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65d10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 20 20 74            /*   t
65d20 68 65 20 63 6f 75 6e 74 20 75 73 69 6e 67 20 61  he count using a
65d30 20 63 61 6c 6c 62 61 63 6b 2e 20 2a 2f 0a 23 64   callback. */.#d
65d40 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 75 6c  efine SQLITE_Nul
65d50 6c 43 61 6c 6c 62 61 63 6b 20 20 20 30 78 30 30  lCallback   0x00
65d60 30 30 30 31 30 30 20 20 2f 2a 20 49 6e 76 6f 6b  000100  /* Invok
65d70 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f  e the callback o
65d80 6e 63 65 20 69 66 20 74 68 65 20 2a 2f 0a 20 20  nce if the */.  
65d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65da0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65db0 20 20 20 20 20 20 20 20 2f 2a 20 20 20 72 65 73          /*   res
65dc0 75 6c 74 20 73 65 74 20 69 73 20 65 6d 70 74 79  ult set is empty
65dd0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49   */.#define SQLI
65de0 54 45 5f 53 71 6c 54 72 61 63 65 20 20 20 20 20  TE_SqlTrace     
65df0 20 20 30 78 30 30 30 30 30 32 30 30 20 20 2f 2a    0x00000200  /*
65e00 20 44 65 62 75 67 20 70 72 69 6e 74 20 53 51 4c   Debug print SQL
65e10 20 61 73 20 69 74 20 65 78 65 63 75 74 65 73 20   as it executes 
65e20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
65e30 45 5f 56 64 62 65 4c 69 73 74 69 6e 67 20 20 20  E_VdbeListing   
65e40 20 30 78 30 30 30 30 30 34 30 30 20 20 2f 2a 20   0x00000400  /* 
65e50 44 65 62 75 67 20 6c 69 73 74 69 6e 67 73 20 6f  Debug listings o
65e60 66 20 56 44 42 45 20 70 72 6f 67 72 61 6d 73 20  f VDBE programs 
65e70 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54  */.#define SQLIT
65e80 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 20 20  E_WriteSchema   
65e90 20 30 78 30 30 30 30 30 38 30 30 20 20 2f 2a 20   0x00000800  /* 
65ea0 4f 4b 20 74 6f 20 75 70 64 61 74 65 20 53 51 4c  OK to update SQL
65eb0 49 54 45 5f 4d 41 53 54 45 52 20 2a 2f 0a 23 64  ITE_MASTER */.#d
65ec0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 4e 6f 52  efine SQLITE_NoR
65ed0 65 61 64 6c 6f 63 6b 20 20 20 20 20 30 78 30 30  eadlock     0x00
65ee0 30 30 31 30 30 30 20 20 2f 2a 20 52 65 61 64 6c  001000  /* Readl
65ef0 6f 63 6b 73 20 61 72 65 20 6f 6d 69 74 74 65 64  ocks are omitted
65f00 20 77 68 65 6e 20 0a 20 20 20 20 20 20 20 20 20   when .         
65f10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
65f30 20 2a 2a 20 61 63 63 65 73 73 69 6e 67 20 72 65   ** accessing re
65f40 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61 73 65  ad-only database
65f50 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  s */.#define SQL
65f60 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b 73  ITE_IgnoreChecks
65f70 20 20 20 30 78 30 30 30 30 32 30 30 30 20 20 2f     0x00002000  /
65f80 2a 20 44 6f 20 6e 6f 74 20 65 6e 66 6f 72 63 65  * Do not enforce
65f90 20 63 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e   check constrain
65fa0 74 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  ts */.#define SQ
65fb0 4c 49 54 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69  LITE_ReadUncommi
65fc0 74 74 65 64 20 30 78 30 30 30 30 34 30 30 30 20  tted 0x00004000 
65fd0 2f 2a 20 46 6f 72 20 73 68 61 72 65 64 2d 63 61  /* For shared-ca
65fe0 63 68 65 20 6d 6f 64 65 20 2a 2f 0a 23 64 65 66  che mode */.#def
65ff0 69 6e 65 20 53 51 4c 49 54 45 5f 4c 65 67 61 63  ine SQLITE_Legac
66000 79 46 69 6c 65 46 6d 74 20 20 30 78 30 30 30 30  yFileFmt  0x0000
66010 38 30 30 30 20 20 2f 2a 20 43 72 65 61 74 65 20  8000  /* Create 
66020 6e 65 77 20 64 61 74 61 62 61 73 65 73 20 69 6e  new databases in
66030 20 66 6f 72 6d 61 74 20 31 20 2a 2f 0a 23 64 65   format 1 */.#de
66040 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 75 6c 6c  fine SQLITE_Full
66050 46 53 79 6e 63 20 20 20 20 20 20 30 78 30 30 30  FSync      0x000
66060 31 30 30 30 30 20 20 2f 2a 20 55 73 65 20 66 75  10000  /* Use fu
66070 6c 6c 20 66 73 79 6e 63 20 6f 6e 20 74 68 65 20  ll fsync on the 
66080 62 61 63 6b 65 6e 64 20 2a 2f 0a 23 64 65 66 69  backend */.#defi
66090 6e 65 20 53 51 4c 49 54 45 5f 4c 6f 61 64 45 78  ne SQLITE_LoadEx
660a0 74 65 6e 73 69 6f 6e 20 20 30 78 30 30 30 32 30  tension  0x00020
660b0 30 30 30 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6c  000  /* Enable l
660c0 6f 61 64 5f 65 78 74 65 6e 73 69 6f 6e 20 2a 2f  oad_extension */
660d0 0a 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  ..#define SQLITE
660e0 5f 52 65 63 6f 76 65 72 79 4d 6f 64 65 20 20 20  _RecoveryMode   
660f0 30 78 30 30 30 34 30 30 30 30 20 20 2f 2a 20 49  0x00040000  /* I
66100 67 6e 6f 72 65 20 73 63 68 65 6d 61 20 65 72 72  gnore schema err
66110 6f 72 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ors */.#define S
66120 51 4c 49 54 45 5f 53 68 61 72 65 64 43 61 63 68  QLITE_SharedCach
66130 65 20 20 20 20 30 78 30 30 30 38 30 30 30 30 20  e    0x00080000 
66140 20 2f 2a 20 43 61 63 68 65 20 73 68 61 72 69 6e   /* Cache sharin
66150 67 20 69 73 20 65 6e 61 62 6c 65 64 20 2a 2f 0a  g is enabled */.
66160 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 56  #define SQLITE_V
66170 74 61 62 20 20 20 20 20 20 20 20 20 20 20 30 78  tab           0x
66180 30 30 31 30 30 30 30 30 20 20 2f 2a 20 54 68 65  00100000  /* The
66190 72 65 20 65 78 69 73 74 73 20 61 20 76 69 72 74  re exists a virt
661a0 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 0a 2f 2a  ual table */../*
661b0 0a 2a 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c  .** Possible val
661c0 75 65 73 20 66 6f 72 20 74 68 65 20 73 71 6c 69  ues for the sqli
661d0 74 65 2e 6d 61 67 69 63 20 66 69 65 6c 64 2e 0a  te.magic field..
661e0 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 73 20 61  ** The numbers a
661f0 72 65 20 6f 62 74 61 69 6e 65 64 20 61 74 20 72  re obtained at r
66200 61 6e 64 6f 6d 20 61 6e 64 20 68 61 76 65 20 6e  andom and have n
66210 6f 20 73 70 65 63 69 61 6c 20 6d 65 61 6e 69 6e  o special meanin
66220 67 2c 20 6f 74 68 65 72 0a 2a 2a 20 74 68 61 6e  g, other.** than
66230 20 62 65 69 6e 67 20 64 69 73 74 69 6e 63 74 20   being distinct 
66240 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65 72  from one another
66250 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
66260 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 20  ITE_MAGIC_OPEN  
66270 20 20 20 30 78 61 30 32 39 61 36 39 37 20 20 2f     0xa029a697  /
66280 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 6f 70  * Database is op
66290 65 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51  en */.#define SQ
662a0 4c 49 54 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45  LITE_MAGIC_CLOSE
662b0 44 20 20 20 30 78 39 66 33 63 32 64 33 33 20 20  D   0x9f3c2d33  
662c0 2f 2a 20 44 61 74 61 62 61 73 65 20 69 73 20 63  /* Database is c
662d0 6c 6f 73 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65  losed */.#define
662e0 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49   SQLITE_MAGIC_SI
662f0 43 4b 20 20 20 20 20 30 78 34 62 37 37 31 32 39  CK     0x4b77129
66300 30 20 20 2f 2a 20 45 72 72 6f 72 20 61 6e 64 20  0  /* Error and 
66310 61 77 61 69 74 69 6e 67 20 63 6c 6f 73 65 20 2a  awaiting close *
66320 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
66330 5f 4d 41 47 49 43 5f 42 55 53 59 20 20 20 20 20  _MAGIC_BUSY     
66340 30 78 66 30 33 62 37 39 30 36 20 20 2f 2a 20 44  0xf03b7906  /* D
66350 61 74 61 62 61 73 65 20 63 75 72 72 65 6e 74 6c  atabase currentl
66360 79 20 69 6e 20 75 73 65 20 2a 2f 0a 23 64 65 66  y in use */.#def
66370 69 6e 65 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  ine SQLITE_MAGIC
66380 5f 45 52 52 4f 52 20 20 20 20 30 78 62 35 33 35  _ERROR    0xb535
66390 37 39 33 30 20 20 2f 2a 20 41 6e 20 53 51 4c 49  7930  /* An SQLI
663a0 54 45 5f 4d 49 53 55 53 45 20 65 72 72 6f 72 20  TE_MISUSE error 
663b0 6f 63 63 75 72 72 65 64 20 2a 2f 0a 0a 2f 2a 0a  occurred */../*.
663c0 2a 2a 20 45 61 63 68 20 53 51 4c 20 66 75 6e 63  ** Each SQL func
663d0 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64 20  tion is defined 
663e0 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f  by an instance o
663f0 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a  f the following.
66400 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 41  ** structure.  A
66410 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
66420 20 73 74 72 75 63 74 75 72 65 20 69 73 20 73 74   structure is st
66430 6f 72 65 64 20 69 6e 20 74 68 65 20 73 71 6c 69  ored in the sqli
66440 74 65 2e 61 46 75 6e 63 0a 2a 2a 20 68 61 73 68  te.aFunc.** hash
66450 20 74 61 62 6c 65 2e 20 20 57 68 65 6e 20 6d 75   table.  When mu
66460 6c 74 69 70 6c 65 20 66 75 6e 63 74 69 6f 6e 73  ltiple functions
66470 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20 6e   have the same n
66480 61 6d 65 2c 20 74 68 65 20 68 61 73 68 20 74 61  ame, the hash ta
66490 62 6c 65 0a 2a 2a 20 70 6f 69 6e 74 73 20 74 6f  ble.** points to
664a0 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f   a linked list o
664b0 66 20 74 68 65 73 65 20 73 74 72 75 63 74 75 72  f these structur
664c0 65 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 46 75  es..*/.struct Fu
664d0 6e 63 44 65 66 20 7b 0a 20 20 69 31 36 20 6e 41  ncDef {.  i16 nA
664e0 72 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  rg;            /
664f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * Number of argu
66500 6d 65 6e 74 73 2e 20 20 2d 31 20 6d 65 61 6e 73  ments.  -1 means
66510 20 75 6e 6c 69 6d 69 74 65 64 20 2a 2f 0a 20 20   unlimited */.  
66520 75 38 20 69 50 72 65 66 45 6e 63 3b 20 20 20 20  u8 iPrefEnc;    
66530 20 20 20 20 20 2f 2a 20 50 72 65 66 65 72 72 65       /* Preferre
66540 64 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  d text encoding 
66550 28 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 31 36  (SQLITE_UTF8, 16
66560 4c 45 2c 20 31 36 42 45 29 20 2a 2f 0a 20 20 75  LE, 16BE) */.  u
66570 38 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20  8 flags;        
66580 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d 62      /* Some comb
66590 69 6e 61 74 69 6f 6e 20 6f 66 20 53 51 4c 49 54  ination of SQLIT
665a0 45 5f 46 55 4e 43 5f 2a 20 2a 2f 0a 20 20 76 6f  E_FUNC_* */.  vo
665b0 69 64 20 2a 70 55 73 65 72 44 61 74 61 3b 20 20  id *pUserData;  
665c0 20 20 20 2f 2a 20 55 73 65 72 20 64 61 74 61 20     /* User data 
665d0 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20 46  parameter */.  F
665e0 75 6e 63 44 65 66 20 2a 70 4e 65 78 74 3b 20 20  uncDef *pNext;  
665f0 20 20 20 20 2f 2a 20 4e 65 78 74 20 66 75 6e 63      /* Next func
66600 74 69 6f 6e 20 77 69 74 68 20 73 61 6d 65 20 6e  tion with same n
66610 61 6d 65 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ame */.  void (*
66620 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
66630 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
66640 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a  te3_value**); /*
66650 20 52 65 67 75 6c 61 72 20 66 75 6e 63 74 69 6f   Regular functio
66660 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 53  n */.  void (*xS
66670 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
66680 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
66690 33 5f 76 61 6c 75 65 2a 2a 29 3b 20 2f 2a 20 41  3_value**); /* A
666a0 67 67 72 65 67 61 74 65 20 73 74 65 70 20 2a 2f  ggregate step */
666b0 0a 20 20 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c  .  void (*xFinal
666c0 69 7a 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  ize)(sqlite3_con
666d0 74 65 78 74 2a 29 3b 20 20 20 20 20 20 20 20 20  text*);         
666e0 20 20 20 20 20 20 20 2f 2a 20 41 67 67 72 65 67         /* Aggreg
666f0 61 74 65 20 66 69 6e 69 61 6c 69 7a 65 72 20 2a  ate finializer *
66700 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b  /.  char *zName;
66710 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 20           /* SQL 
66720 6e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6e 63  name of the func
66730 74 69 6f 6e 2e 20 2a 2f 0a 20 20 46 75 6e 63 44  tion. */.  FuncD
66740 65 66 20 2a 70 48 61 73 68 3b 20 20 20 20 20 20  ef *pHash;      
66750 2f 2a 20 4e 65 78 74 20 77 69 74 68 20 61 20 64  /* Next with a d
66760 69 66 66 65 72 65 6e 74 20 6e 61 6d 65 20 62 75  ifferent name bu
66770 74 20 74 68 65 20 73 61 6d 65 20 68 61 73 68 20  t the same hash 
66780 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 6f 73  */.};../*.** Pos
66790 73 69 62 6c 65 20 76 61 6c 75 65 73 20 66 6f 72  sible values for
667a0 20 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 0a 2a   FuncDef.flags.*
667b0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
667c0 5f 46 55 4e 43 5f 4c 49 4b 45 20 20 20 20 20 30  _FUNC_LIKE     0
667d0 78 30 31 20 2f 2a 20 43 61 6e 64 69 64 61 74 65  x01 /* Candidate
667e0 20 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70   for the LIKE op
667f0 74 69 6d 69 7a 61 74 69 6f 6e 20 2a 2f 0a 23 64  timization */.#d
66800 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
66810 43 5f 43 41 53 45 20 20 20 20 20 30 78 30 32 20  C_CASE     0x02 
66820 2f 2a 20 43 61 73 65 2d 73 65 6e 73 69 74 69 76  /* Case-sensitiv
66830 65 20 4c 49 4b 45 2d 74 79 70 65 20 66 75 6e 63  e LIKE-type func
66840 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  tion */.#define 
66850 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45  SQLITE_FUNC_EPHE
66860 4d 20 20 20 20 30 78 30 34 20 2f 2a 20 45 70 68  M    0x04 /* Eph
66870 65 72 6d 65 72 61 6c 2e 20 20 44 65 6c 65 74 65  ermeral.  Delete
66880 20 77 69 74 68 20 56 44 42 45 20 2a 2f 0a 23 64   with VDBE */.#d
66890 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 55 4e  efine SQLITE_FUN
668a0 43 5f 4e 45 45 44 43 4f 4c 4c 20 30 78 30 38 20  C_NEEDCOLL 0x08 
668b0 2f 2a 20 73 71 6c 69 74 65 33 47 65 74 46 75 6e  /* sqlite3GetFun
668c0 63 43 6f 6c 6c 53 65 71 28 29 20 6d 69 67 68 74  cCollSeq() might
668d0 20 62 65 20 63 61 6c 6c 65 64 20 2a 2f 0a 0a 2f   be called */../
668e0 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
668f0 6e 67 20 74 68 72 65 65 20 6d 61 63 72 6f 73 2c  ng three macros,
66900 20 46 55 4e 43 54 49 4f 4e 28 29 2c 20 4c 49 4b   FUNCTION(), LIK
66910 45 46 55 4e 43 28 29 20 61 6e 64 20 41 47 47 52  EFUNC() and AGGR
66920 45 47 41 54 45 28 29 20 61 72 65 0a 2a 2a 20 75  EGATE() are.** u
66930 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74 68  sed to create th
66940 65 20 69 6e 69 74 69 61 6c 69 7a 65 72 73 20 66  e initializers f
66950 6f 72 20 74 68 65 20 46 75 6e 63 44 65 66 20 73  or the FuncDef s
66960 74 72 75 63 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a  tructures..**.**
66970 20 20 20 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d     FUNCTION(zNam
66980 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
66990 4e 43 2c 20 78 46 75 6e 63 29 0a 2a 2a 20 20 20  NC, xFunc).**   
669a0 20 20 55 73 65 64 20 74 6f 20 63 72 65 61 74 65    Used to create
669b0 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69   a scalar functi
669c0 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66  on definition of
669d0 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a 4e 61 6d   a function zNam
669e0 65 20 0a 2a 2a 20 20 20 20 20 69 6d 70 6c 65 6d  e .**     implem
669f0 65 6e 74 65 64 20 62 79 20 43 20 66 75 6e 63 74  ented by C funct
66a00 69 6f 6e 20 78 46 75 6e 63 20 74 68 61 74 20 61  ion xFunc that a
66a10 63 63 65 70 74 73 20 6e 41 72 67 20 61 72 67 75  ccepts nArg argu
66a20 6d 65 6e 74 73 2e 20 54 68 65 0a 2a 2a 20 20 20  ments. The.**   
66a30 20 20 76 61 6c 75 65 20 70 61 73 73 65 64 20 61    value passed a
66a40 73 20 69 41 72 67 20 69 73 20 63 61 73 74 20 74  s iArg is cast t
66a50 6f 20 61 20 28 76 6f 69 64 2a 29 20 61 6e 64 20  o a (void*) and 
66a60 6d 61 64 65 20 61 76 61 69 6c 61 62 6c 65 0a 2a  made available.*
66a70 2a 20 20 20 20 20 61 73 20 74 68 65 20 75 73 65  *     as the use
66a80 72 2d 64 61 74 61 20 28 73 71 6c 69 74 65 33 5f  r-data (sqlite3_
66a90 75 73 65 72 5f 64 61 74 61 28 29 29 20 66 6f 72  user_data()) for
66aa0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 49   the function. I
66ab0 66 20 0a 2a 2a 20 20 20 20 20 61 72 67 75 6d 65  f .**     argume
66ac0 6e 74 20 62 4e 43 20 69 73 20 74 72 75 65 2c 20  nt bNC is true, 
66ad0 74 68 65 6e 20 74 68 65 20 46 75 6e 63 44 65 66  then the FuncDef
66ae0 2e 6e 65 65 64 43 6f 6c 6c 61 74 65 20 66 6c 61  .needCollate fla
66af0 67 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a 20  g is set..**.** 
66b00 20 20 41 47 47 52 45 47 41 54 45 28 7a 4e 61 6d    AGGREGATE(zNam
66b10 65 2c 20 6e 41 72 67 2c 20 69 41 72 67 2c 20 62  e, nArg, iArg, b
66b20 4e 43 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61  NC, xStep, xFina
66b30 6c 29 0a 2a 2a 20 20 20 20 20 55 73 65 64 20 74  l).**     Used t
66b40 6f 20 63 72 65 61 74 65 20 61 6e 20 61 67 67 72  o create an aggr
66b50 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 64  egate function d
66b60 65 66 69 6e 69 74 69 6f 6e 20 69 6d 70 6c 65 6d  efinition implem
66b70 65 6e 74 65 64 20 62 79 0a 2a 2a 20 20 20 20 20  ented by.**     
66b80 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
66b90 78 53 74 65 70 20 61 6e 64 20 78 46 69 6e 61 6c  xStep and xFinal
66ba0 2e 20 54 68 65 20 66 69 72 73 74 20 66 6f 75 72  . The first four
66bb0 20 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 20   parameters.**  
66bc0 20 20 20 61 72 65 20 69 6e 74 65 72 70 72 65 74     are interpret
66bd0 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 77  ed in the same w
66be0 61 79 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ay as the first 
66bf0 34 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 0a  4 parameters to.
66c00 2a 2a 20 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  **     FUNCTION(
66c10 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 4c 49 4b 45 46  )..**.**   LIKEF
66c20 55 4e 43 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c  UNC(zName, nArg,
66c30 20 70 41 72 67 2c 20 66 6c 61 67 73 29 0a 2a 2a   pArg, flags).**
66c40 20 20 20 20 20 55 73 65 64 20 74 6f 20 63 72 65       Used to cre
66c50 61 74 65 20 61 20 73 63 61 6c 61 72 20 66 75 6e  ate a scalar fun
66c60 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e  ction definition
66c70 20 6f 66 20 61 20 66 75 6e 63 74 69 6f 6e 20 7a   of a function z
66c80 4e 61 6d 65 20 0a 2a 2a 20 20 20 20 20 74 68 61  Name .**     tha
66c90 74 20 61 63 63 65 70 74 73 20 6e 41 72 67 20 61  t accepts nArg a
66ca0 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 69 73 20  rguments and is 
66cb0 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79 20 61  implemented by a
66cc0 20 63 61 6c 6c 20 74 6f 20 43 20 0a 2a 2a 20 20   call to C .**  
66cd0 20 20 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65     function like
66ce0 46 75 6e 63 2e 20 41 72 67 75 6d 65 6e 74 20 70  Func. Argument p
66cf0 41 72 67 20 69 73 20 63 61 73 74 20 74 6f 20 61  Arg is cast to a
66d00 20 28 76 6f 69 64 20 2a 29 20 61 6e 64 20 6d 61   (void *) and ma
66d10 64 65 0a 2a 2a 20 20 20 20 20 61 76 61 69 6c 61  de.**     availa
66d20 62 6c 65 20 61 73 20 74 68 65 20 66 75 6e 63 74  ble as the funct
66d30 69 6f 6e 20 75 73 65 72 2d 64 61 74 61 20 28 73  ion user-data (s
66d40 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
66d50 28 29 29 2e 20 54 68 65 0a 2a 2a 20 20 20 20 20  ()). The.**     
66d60 46 75 6e 63 44 65 66 2e 66 6c 61 67 73 20 76 61  FuncDef.flags va
66d70 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f  riable is set to
66d80 20 74 68 65 20 76 61 6c 75 65 20 70 61 73 73 65   the value passe
66d90 64 20 61 73 20 74 68 65 20 66 6c 61 67 73 0a 2a  d as the flags.*
66da0 2a 20 20 20 20 20 70 61 72 61 6d 65 74 65 72 2e  *     parameter.
66db0 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 55 4e 43  .*/.#define FUNC
66dc0 54 49 4f 4e 28 7a 4e 61 6d 65 2c 20 6e 41 72 67  TION(zName, nArg
66dd0 2c 20 69 41 72 67 2c 20 62 4e 43 2c 20 78 46 75  , iArg, bNC, xFu
66de0 6e 63 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20 53  nc) \.  {nArg, S
66df0 51 4c 49 54 45 5f 55 54 46 38 2c 20 62 4e 43 2a  QLITE_UTF8, bNC*
66e00 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54 4f  8, SQLITE_INT_TO
66e10 5f 50 54 52 28 69 41 72 67 29 2c 20 30 2c 20 78  _PTR(iArg), 0, x
66e20 46 75 6e 63 2c 20 30 2c 20 30 2c 20 23 7a 4e 61  Func, 0, 0, #zNa
66e30 6d 65 2c 20 30 7d 0a 23 64 65 66 69 6e 65 20 53  me, 0}.#define S
66e40 54 52 5f 46 55 4e 43 54 49 4f 4e 28 7a 4e 61 6d  TR_FUNCTION(zNam
66e50 65 2c 20 6e 41 72 67 2c 20 70 41 72 67 2c 20 62  e, nArg, pArg, b
66e60 4e 43 2c 20 78 46 75 6e 63 29 20 5c 0a 20 20 7b  NC, xFunc) \.  {
66e70 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
66e80 38 2c 20 62 4e 43 2a 38 2c 20 70 41 72 67 2c 20  8, bNC*8, pArg, 
66e90 30 2c 20 78 46 75 6e 63 2c 20 30 2c 20 30 2c 20  0, xFunc, 0, 0, 
66ea0 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23 64 65 66 69  #zName, 0}.#defi
66eb0 6e 65 20 4c 49 4b 45 46 55 4e 43 28 7a 4e 61 6d  ne LIKEFUNC(zNam
66ec0 65 2c 20 6e 41 72 67 2c 20 61 72 67 2c 20 66 6c  e, nArg, arg, fl
66ed0 61 67 73 29 20 5c 0a 20 20 7b 6e 41 72 67 2c 20  ags) \.  {nArg, 
66ee0 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 66 6c 61  SQLITE_UTF8, fla
66ef0 67 73 2c 20 28 76 6f 69 64 20 2a 29 61 72 67 2c  gs, (void *)arg,
66f00 20 30 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c   0, likeFunc, 0,
66f10 20 30 2c 20 23 7a 4e 61 6d 65 2c 20 30 7d 0a 23   0, #zName, 0}.#
66f20 64 65 66 69 6e 65 20 41 47 47 52 45 47 41 54 45  define AGGREGATE
66f30 28 7a 4e 61 6d 65 2c 20 6e 41 72 67 2c 20 61 72  (zName, nArg, ar
66f40 67 2c 20 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  g, nc, xStep, xF
66f50 69 6e 61 6c 29 20 5c 0a 20 20 7b 6e 41 72 67 2c  inal) \.  {nArg,
66f60 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 6e 63   SQLITE_UTF8, nc
66f70 2a 38 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  *8, SQLITE_INT_T
66f80 4f 5f 50 54 52 28 61 72 67 29 2c 20 30 2c 20 30  O_PTR(arg), 0, 0
66f90 2c 20 78 53 74 65 70 2c 78 46 69 6e 61 6c 2c 23  , xStep,xFinal,#
66fa0 7a 4e 61 6d 65 2c 30 7d 0a 0a 0a 2f 2a 0a 2a 2a  zName,0}.../*.**
66fb0 20 45 61 63 68 20 53 51 4c 69 74 65 20 6d 6f 64   Each SQLite mod
66fc0 75 6c 65 20 28 76 69 72 74 75 61 6c 20 74 61 62  ule (virtual tab
66fd0 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 29 20 69  le definition) i
66fe0 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e 0a  s defined by an.
66ff0 2a 2a 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  ** instance of t
67000 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
67010 75 63 74 75 72 65 2c 20 73 74 6f 72 65 64 20 69  ucture, stored i
67020 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 4d  n the sqlite3.aM
67030 6f 64 75 6c 65 0a 2a 2a 20 68 61 73 68 20 74 61  odule.** hash ta
67040 62 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d  ble..*/.struct M
67050 6f 64 75 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20  odule {.  const 
67060 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a  sqlite3_module *
67070 70 4d 6f 64 75 6c 65 3b 20 20 20 20 20 20 20 2f  pModule;       /
67080 2a 20 43 61 6c 6c 62 61 63 6b 20 70 6f 69 6e 74  * Callback point
67090 65 72 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ers */.  const c
670a0 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
670b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
670c0 20 4e 61 6d 65 20 70 61 73 73 65 64 20 74 6f 20   Name passed to 
670d0 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 28 29 20  create_module() 
670e0 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 3b  */.  void *pAux;
670f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
67100 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 41 75            /* pAu
67110 78 20 70 61 73 73 65 64 20 74 6f 20 63 72 65 61  x passed to crea
67120 74 65 5f 6d 6f 64 75 6c 65 28 29 20 2a 2f 0a 20  te_module() */. 
67130 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f 79   void (*xDestroy
67140 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20  )(void *);      
67150 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65 20        /* Module 
67160 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e 63 74  destructor funct
67170 69 6f 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ion */.};../*.**
67180 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
67190 75 74 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 6f  ut each column o
671a0 66 20 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 69  f an SQL table i
671b0 73 20 68 65 6c 64 20 69 6e 20 61 6e 20 69 6e 73  s held in an ins
671c0 74 61 6e 63 65 0a 2a 2a 20 6f 66 20 74 68 69 73  tance.** of this
671d0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
671e0 74 72 75 63 74 20 43 6f 6c 75 6d 6e 20 7b 0a 20  truct Column {. 
671f0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
67200 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 69    /* Name of thi
67210 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 45 78  s column */.  Ex
67220 70 72 20 2a 70 44 66 6c 74 3b 20 20 20 20 20 2f  pr *pDflt;     /
67230 2a 20 44 65 66 61 75 6c 74 20 76 61 6c 75 65 20  * Default value 
67240 6f 66 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a  of this column *
67250 2f 0a 20 20 63 68 61 72 20 2a 7a 54 79 70 65 3b  /.  char *zType;
67260 20 20 20 20 20 2f 2a 20 44 61 74 61 20 74 79 70       /* Data typ
67270 65 20 66 6f 72 20 74 68 69 73 20 63 6f 6c 75 6d  e for this colum
67280 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f  n */.  char *zCo
67290 6c 6c 3b 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  ll;     /* Colla
672a0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 20 20  ting sequence.  
672b0 49 66 20 4e 55 4c 4c 2c 20 75 73 65 20 74 68 65  If NULL, use the
672c0 20 64 65 66 61 75 6c 74 20 2a 2f 0a 20 20 75 38   default */.  u8
672d0 20 6e 6f 74 4e 75 6c 6c 3b 20 20 20 20 20 20 2f   notNull;      /
672e0 2a 20 54 72 75 65 20 69 66 20 74 68 65 72 65 20  * True if there 
672f0 69 73 20 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f  is a NOT NULL co
67300 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20 75 38  nstraint */.  u8
67310 20 69 73 50 72 69 6d 4b 65 79 3b 20 20 20 20 2f   isPrimKey;    /
67320 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 63  * True if this c
67330 6f 6c 75 6d 6e 20 69 73 20 70 61 72 74 20 6f 66  olumn is part of
67340 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
67350 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
67360 69 74 79 3b 20 20 20 2f 2a 20 4f 6e 65 20 6f 66  ity;   /* One of
67370 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   the SQLITE_AFF_
67380 2e 2e 2e 20 76 61 6c 75 65 73 20 2a 2f 0a 23 69  ... values */.#i
67390 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
673a0 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20  T_VIRTUALTABLE. 
673b0 20 75 38 20 69 73 48 69 64 64 65 6e 3b 20 20 20   u8 isHidden;   
673c0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69    /* True if thi
673d0 73 20 63 6f 6c 75 6d 6e 20 69 73 20 27 68 69 64  s column is 'hid
673e0 64 65 6e 27 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d  den' */.#endif.}
673f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 22 43 6f 6c 6c  ;../*.** A "Coll
67400 61 74 69 6e 67 20 53 65 71 75 65 6e 63 65 22 20  ating Sequence" 
67410 69 73 20 64 65 66 69 6e 65 64 20 62 79 20 61 6e  is defined by an
67420 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
67430 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74   following.** st
67440 72 75 63 74 75 72 65 2e 20 43 6f 6e 63 65 70 74  ructure. Concept
67450 75 61 6c 6c 79 2c 20 61 20 63 6f 6c 6c 61 74 69  ually, a collati
67460 6e 67 20 73 65 71 75 65 6e 63 65 20 63 6f 6e 73  ng sequence cons
67470 69 73 74 73 20 6f 66 20 61 20 6e 61 6d 65 20 61  ists of a name a
67480 6e 64 0a 2a 2a 20 61 20 63 6f 6d 70 61 72 69 73  nd.** a comparis
67490 6f 6e 20 72 6f 75 74 69 6e 65 20 74 68 61 74 20  on routine that 
674a0 64 65 66 69 6e 65 73 20 74 68 65 20 6f 72 64 65  defines the orde
674b0 72 20 6f 66 20 74 68 61 74 20 73 65 71 75 65 6e  r of that sequen
674c0 63 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20  ce..**.** There 
674d0 6d 61 79 20 74 77 6f 20 73 65 70 65 72 61 74 65  may two seperate
674e0 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
674f0 20 6f 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f   of the collatio
67500 6e 20 66 75 6e 63 74 69 6f 6e 2c 20 6f 6e 65 0a  n function, one.
67510 2a 2a 20 74 68 61 74 20 70 72 6f 63 65 73 73 65  ** that processe
67520 73 20 74 65 78 74 20 69 6e 20 55 54 46 2d 38 20  s text in UTF-8 
67530 65 6e 63 6f 64 69 6e 67 20 28 43 6f 6c 6c 53 65  encoding (CollSe
67540 71 2e 78 43 6d 70 29 20 61 6e 64 20 61 6e 6f 74  q.xCmp) and anot
67550 68 65 72 20 74 68 61 74 0a 2a 2a 20 70 72 6f 63  her that.** proc
67560 65 73 73 65 73 20 74 65 78 74 20 65 6e 63 6f 64  esses text encod
67570 65 64 20 69 6e 20 55 54 46 2d 31 36 20 28 43 6f  ed in UTF-16 (Co
67580 6c 6c 53 65 71 2e 78 43 6d 70 31 36 29 2c 20 75  llSeq.xCmp16), u
67590 73 69 6e 67 20 74 68 65 20 6d 61 63 68 69 6e 65  sing the machine
675a0 0a 2a 2a 20 6e 61 74 69 76 65 20 62 79 74 65 20  .** native byte 
675b0 6f 72 64 65 72 2e 20 57 68 65 6e 20 61 20 63 6f  order. When a co
675c0 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
675d0 20 69 73 20 69 6e 76 6f 6b 65 64 2c 20 53 51 4c   is invoked, SQL
675e0 69 74 65 20 73 65 6c 65 63 74 73 0a 2a 2a 20 74  ite selects.** t
675f0 68 65 20 76 65 72 73 69 6f 6e 20 74 68 61 74 20  he version that 
67600 77 69 6c 6c 20 72 65 71 75 69 72 65 20 74 68 65  will require the
67610 20 6c 65 61 73 74 20 65 78 70 65 6e 73 69 76 65   least expensive
67620 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a 20 74 72 61   encoding.** tra
67630 6e 73 6c 61 74 69 6f 6e 73 2c 20 69 66 20 61 6e  nslations, if an
67640 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 6f 6c  y..**.** The Col
67650 6c 53 65 71 2e 70 55 73 65 72 20 6d 65 6d 62 65  lSeq.pUser membe
67660 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 61 6e  r variable is an
67670 20 65 78 74 72 61 20 70 61 72 61 6d 65 74 65 72   extra parameter
67680 20 74 68 61 74 20 70 61 73 73 65 64 20 69 6e 0a   that passed in.
67690 2a 2a 20 61 73 20 74 68 65 20 66 69 72 73 74 20  ** as the first 
676a0 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20  argument to the 
676b0 55 54 46 2d 38 20 63 6f 6d 70 61 72 69 73 6f 6e  UTF-8 comparison
676c0 20 66 75 6e 63 74 69 6f 6e 2c 20 78 43 6d 70 2e   function, xCmp.
676d0 0a 2a 2a 20 43 6f 6c 6c 53 65 71 2e 70 55 73 65  .** CollSeq.pUse
676e0 72 31 36 20 69 73 20 74 68 65 20 65 71 75 69 76  r16 is the equiv
676f0 61 6c 65 6e 74 20 66 6f 72 20 74 68 65 20 55 54  alent for the UT
67700 46 2d 31 36 20 63 6f 6d 70 61 72 69 73 6f 6e 20  F-16 comparison 
67710 66 75 6e 63 74 69 6f 6e 2c 0a 2a 2a 20 78 43 6d  function,.** xCm
67720 70 31 36 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f  p16..**.** If bo
67730 74 68 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70 20  th CollSeq.xCmp 
67740 61 6e 64 20 43 6f 6c 6c 53 65 71 2e 78 43 6d 70  and CollSeq.xCmp
67750 31 36 20 61 72 65 20 4e 55 4c 4c 2c 20 69 74 20  16 are NULL, it 
67760 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a  means that the.*
67770 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * collating sequ
67780 65 6e 63 65 20 69 73 20 75 6e 64 65 66 69 6e 65  ence is undefine
67790 64 2e 20 20 49 6e 64 69 63 65 73 20 62 75 69 6c  d.  Indices buil
677a0 74 20 6f 6e 20 61 6e 20 75 6e 64 65 66 69 6e 65  t on an undefine
677b0 64 0a 2a 2a 20 63 6f 6c 6c 61 74 69 6e 67 20 73  d.** collating s
677c0 65 71 75 65 6e 63 65 20 6d 61 79 20 6e 6f 74 20  equence may not 
677d0 62 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74  be read or writt
677e0 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 43 6f  en..*/.struct Co
677f0 6c 6c 53 65 71 20 7b 0a 20 20 63 68 61 72 20 2a  llSeq {.  char *
67800 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20  zName;          
67810 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63  /* Name of the c
67820 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
67830 65 2c 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64  e, UTF-8 encoded
67840 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
67850 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
67860 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 68 61 6e  ext encoding han
67870 64 6c 65 64 20 62 79 20 78 43 6d 70 28 29 20 2a  dled by xCmp() *
67880 2f 0a 20 20 75 38 20 74 79 70 65 3b 20 20 20 20  /.  u8 type;    
67890 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65            /* One
678a0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43   of the SQLITE_C
678b0 4f 4c 4c 5f 2e 2e 2e 20 76 61 6c 75 65 73 20 62  OLL_... values b
678c0 65 6c 6f 77 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  elow */.  void *
678d0 70 55 73 65 72 3b 20 20 20 20 20 20 20 20 20 20  pUser;          
678e0 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e  /* First argumen
678f0 74 20 74 6f 20 78 43 6d 70 28 29 20 2a 2f 0a 20  t to xCmp() */. 
67900 20 69 6e 74 20 28 2a 78 43 6d 70 29 28 76 6f 69   int (*xCmp)(voi
67910 64 2a 2c 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f  d*,int, const vo
67920 69 64 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20  id*, int, const 
67930 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69 64 20 28  void*);.  void (
67940 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 3b 20 20  *xDel)(void*);  
67950 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
67960 72 20 70 55 73 65 72 20 2a 2f 0a 7d 3b 0a 0a 2f  r pUser */.};../
67970 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c  *.** Allowed val
67980 75 65 73 20 6f 66 20 43 6f 6c 6c 53 65 71 2e 74  ues of CollSeq.t
67990 79 70 65 3a 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  ype:.*/.#define 
679a0 53 51 4c 49 54 45 5f 43 4f 4c 4c 5f 42 49 4e 41  SQLITE_COLL_BINA
679b0 52 59 20 20 31 20 20 2f 2a 20 54 68 65 20 64 65  RY  1  /* The de
679c0 66 61 75 6c 74 20 6d 65 6d 63 6d 70 28 29 20 63  fault memcmp() c
679d0 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
679e0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  e */.#define SQL
679f0 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53 45 20  ITE_COLL_NOCASE 
67a00 20 32 20 20 2f 2a 20 54 68 65 20 62 75 69 6c 74   2  /* The built
67a10 2d 69 6e 20 4e 4f 43 41 53 45 20 63 6f 6c 6c 61  -in NOCASE colla
67a20 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
67a30 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
67a40 43 4f 4c 4c 5f 52 45 56 45 52 53 45 20 33 20 20  COLL_REVERSE 3  
67a50 2f 2a 20 54 68 65 20 62 75 69 6c 74 2d 69 6e 20  /* The built-in 
67a60 52 45 56 45 52 53 45 20 63 6f 6c 6c 61 74 69 6e  REVERSE collatin
67a70 67 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 23 64  g sequence */.#d
67a80 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 4c  efine SQLITE_COL
67a90 4c 5f 55 53 45 52 20 20 20 20 30 20 20 2f 2a 20  L_USER    0  /* 
67aa0 41 6e 79 20 6f 74 68 65 72 20 75 73 65 72 2d 64  Any other user-d
67ab0 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74 69 6e 67  efined collating
67ac0 20 73 65 71 75 65 6e 63 65 20 2a 2f 0a 0a 2f 2a   sequence */../*
67ad0 0a 2a 2a 20 41 20 73 6f 72 74 20 6f 72 64 65 72  .** A sort order
67ae0 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 41   can be either A
67af0 53 43 20 6f 72 20 44 45 53 43 2e 0a 2a 2f 0a 23  SC or DESC..*/.#
67b00 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f  define SQLITE_SO
67b10 5f 41 53 43 20 20 20 20 20 20 20 30 20 20 2f 2a  _ASC       0  /*
67b20 20 53 6f 72 74 20 69 6e 20 61 73 63 65 6e 64 69   Sort in ascendi
67b30 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66  ng order */.#def
67b40 69 6e 65 20 53 51 4c 49 54 45 5f 53 4f 5f 44 45  ine SQLITE_SO_DE
67b50 53 43 20 20 20 20 20 20 31 20 20 2f 2a 20 53 6f  SC      1  /* So
67b60 72 74 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20  rt in ascending 
67b70 6f 72 64 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  order */../*.** 
67b80 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20  Column affinity 
67b90 74 79 70 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  types..**.** The
67ba0 73 65 20 75 73 65 64 20 74 6f 20 68 61 76 65 20  se used to have 
67bb0 6d 6e 65 6d 6f 6e 69 63 20 6e 61 6d 65 20 6c 69  mnemonic name li
67bc0 6b 65 20 27 69 27 20 66 6f 72 20 53 51 4c 49 54  ke 'i' for SQLIT
67bd0 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 20 61 6e  E_AFF_INTEGER an
67be0 64 0a 2a 2a 20 27 74 27 20 66 6f 72 20 53 51 4c  d.** 't' for SQL
67bf0 49 54 45 5f 41 46 46 5f 54 45 58 54 2e 20 20 42  ITE_AFF_TEXT.  B
67c00 75 74 20 77 65 20 63 61 6e 20 73 61 76 65 20 61  ut we can save a
67c10 20 6c 69 74 74 6c 65 20 73 70 61 63 65 20 61 6e   little space an
67c20 64 20 69 6d 70 72 6f 76 65 0a 2a 2a 20 74 68 65  d improve.** the
67c30 20 73 70 65 65 64 20 61 20 6c 69 74 74 6c 65 20   speed a little 
67c40 62 79 20 6e 75 6d 62 65 72 69 6e 67 20 74 68 65  by numbering the
67c50 20 76 61 6c 75 65 73 20 63 6f 6e 73 65 63 75 74   values consecut
67c60 69 76 65 6c 79 2e 20 20 0a 2a 2a 0a 2a 2a 20 42  ively.  .**.** B
67c70 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 73  ut rather than s
67c80 74 61 72 74 20 77 69 74 68 20 30 20 6f 72 20 31  tart with 0 or 1
67c90 2c 20 77 65 20 62 65 67 69 6e 20 77 69 74 68 20  , we begin with 
67ca0 27 61 27 2e 20 20 54 68 61 74 20 77 61 79 2c 0a  'a'.  That way,.
67cb0 2a 2a 20 77 68 65 6e 20 6d 75 6c 74 69 70 6c 65  ** when multiple
67cc0 20 61 66 66 69 6e 69 74 79 20 74 79 70 65 73 20   affinity types 
67cd0 61 72 65 20 63 6f 6e 63 61 74 65 6e 61 74 65 64  are concatenated
67ce0 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67 20 61   into a string a
67cf0 6e 64 0a 2a 2a 20 75 73 65 64 20 61 73 20 74 68  nd.** used as th
67d00 65 20 50 34 20 6f 70 65 72 61 6e 64 2c 20 74 68  e P4 operand, th
67d10 65 79 20 77 69 6c 6c 20 62 65 20 6d 6f 72 65 20  ey will be more 
67d20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  readable..**.** 
67d30 4e 6f 74 65 20 61 6c 73 6f 20 74 68 61 74 20 74  Note also that t
67d40 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70 65 73  he numeric types
67d50 20 61 72 65 20 67 72 6f 75 70 65 64 20 74 6f 67   are grouped tog
67d60 65 74 68 65 72 20 73 6f 20 74 68 61 74 20 74 65  ether so that te
67d70 73 74 69 6e 67 0a 2a 2a 20 66 6f 72 20 61 20 6e  sting.** for a n
67d80 75 6d 65 72 69 63 20 74 79 70 65 20 69 73 20 61  umeric type is a
67d90 20 73 69 6e 67 6c 65 20 63 6f 6d 70 61 72 69 73   single comparis
67da0 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53  on..*/.#define S
67db0 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20 20  QLITE_AFF_TEXT  
67dc0 20 20 20 27 61 27 0a 23 64 65 66 69 6e 65 20 53     'a'.#define S
67dd0 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 20  QLITE_AFF_NONE  
67de0 20 20 20 27 62 27 0a 23 64 65 66 69 6e 65 20 53     'b'.#define S
67df0 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
67e00 43 20 20 27 63 27 0a 23 64 65 66 69 6e 65 20 53  C  'c'.#define S
67e10 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
67e20 52 20 20 27 64 27 0a 23 64 65 66 69 6e 65 20 53  R  'd'.#define S
67e30 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c 20 20  QLITE_AFF_REAL  
67e40 20 20 20 27 65 27 0a 0a 23 64 65 66 69 6e 65 20     'e'..#define 
67e50 73 71 6c 69 74 65 33 49 73 4e 75 6d 65 72 69 63  sqlite3IsNumeric
67e60 41 66 66 69 6e 69 74 79 28 58 29 20 20 28 28 58  Affinity(X)  ((X
67e70 29 3e 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  )>=SQLITE_AFF_NU
67e80 4d 45 52 49 43 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  MERIC)../*.** Th
67e90 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41 53  e SQLITE_AFF_MAS
67ea0 4b 20 76 61 6c 75 65 73 20 6d 61 73 6b 73 20 6f  K values masks o
67eb0 66 66 20 74 68 65 20 73 69 67 6e 69 66 69 63 61  ff the significa
67ec0 6e 74 20 62 69 74 73 20 6f 66 20 61 6e 0a 2a 2a  nt bits of an.**
67ed0 20 61 66 66 69 6e 69 74 79 20 76 61 6c 75 65 2e   affinity value.
67ee0 20 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c   .*/.#define SQL
67ef0 49 54 45 5f 41 46 46 5f 4d 41 53 4b 20 20 20 20  ITE_AFF_MASK    
67f00 20 30 78 36 37 0a 0a 2f 2a 0a 2a 2a 20 41 64 64   0x67../*.** Add
67f10 69 74 69 6f 6e 61 6c 20 62 69 74 20 76 61 6c 75  itional bit valu
67f20 65 73 20 74 68 61 74 20 63 61 6e 20 62 65 20 4f  es that can be O
67f30 52 65 64 20 77 69 74 68 20 61 6e 20 61 66 66 69  Red with an affi
67f40 6e 69 74 79 20 77 69 74 68 6f 75 74 0a 2a 2a 20  nity without.** 
67f50 63 68 61 6e 67 69 6e 67 20 74 68 65 20 61 66 66  changing the aff
67f60 69 6e 69 74 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e  inity..*/.#defin
67f70 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  e SQLITE_JUMPIFN
67f80 55 4c 4c 20 20 20 30 78 30 38 20 20 2f 2a 20 6a  ULL   0x08  /* j
67f90 75 6d 70 73 20 69 66 20 65 69 74 68 65 72 20 6f  umps if either o
67fa0 70 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a  perand is NULL *
67fb0 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45  /.#define SQLITE
67fc0 5f 53 54 4f 52 45 50 32 20 20 20 20 20 20 30 78  _STOREP2      0x
67fd0 31 30 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73  10  /* Store res
67fe0 75 6c 74 20 69 6e 20 72 65 67 5b 50 32 5d 20 72  ult in reg[P2] r
67ff0 61 74 68 65 72 20 74 68 61 6e 20 6a 75 6d 70 20  ather than jump 
68000 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53  */../*.** Each S
68010 51 4c 20 74 61 62 6c 65 20 69 73 20 72 65 70 72  QL table is repr
68020 65 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72  esented in memor
68030 79 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  y by an instance
68040 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 6c 6c 6f   of the.** follo
68050 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a  wing structure..
68060 2a 2a 0a 2a 2a 20 54 61 62 6c 65 2e 7a 4e 61 6d  **.** Table.zNam
68070 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
68080 20 74 68 65 20 74 61 62 6c 65 2e 20 20 54 68 65   the table.  The
68090 20 63 61 73 65 20 6f 66 20 74 68 65 20 6f 72 69   case of the ori
680a0 67 69 6e 61 6c 0a 2a 2a 20 43 52 45 41 54 45 20  ginal.** CREATE 
680b0 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
680c0 69 73 20 73 74 6f 72 65 64 2c 20 62 75 74 20 63  is stored, but c
680d0 61 73 65 20 69 73 20 6e 6f 74 20 73 69 67 6e 69  ase is not signi
680e0 66 69 63 61 6e 74 20 66 6f 72 0a 2a 2a 20 63 6f  ficant for.** co
680f0 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a 0a 2a 2a  mparisons..**.**
68100 20 54 61 62 6c 65 2e 6e 43 6f 6c 20 69 73 20 74   Table.nCol is t
68110 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c  he number of col
68120 75 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62  umns in this tab
68130 6c 65 2e 20 20 54 61 62 6c 65 2e 61 43 6f 6c 20  le.  Table.aCol 
68140 69 73 20 61 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  is a.** pointer 
68150 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20 43  to an array of C
68160 6f 6c 75 6d 6e 20 73 74 72 75 63 74 75 72 65 73  olumn structures
68170 2c 20 6f 6e 65 20 66 6f 72 20 65 61 63 68 20 63  , one for each c
68180 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  olumn..**.** If 
68190 74 68 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e  the table has an
681a0 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
681b0 20 4b 45 59 2c 20 74 68 65 6e 20 54 61 62 6c 65   KEY, then Table
681c0 2e 69 50 4b 65 79 20 69 73 20 74 68 65 20 69 6e  .iPKey is the in
681d0 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  dex of.** the co
681e0 6c 75 6d 6e 20 74 68 61 74 20 69 73 20 74 68 61  lumn that is tha
681f0 74 20 6b 65 79 2e 20 20 20 4f 74 68 65 72 77 69  t key.   Otherwi
68200 73 65 20 54 61 62 6c 65 2e 69 50 4b 65 79 20 69  se Table.iPKey i
68210 73 20 6e 65 67 61 74 69 76 65 2e 20 20 4e 6f 74  s negative.  Not
68220 65 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 64 61  e.** that the da
68230 74 61 74 79 70 65 20 6f 66 20 74 68 65 20 50 52  tatype of the PR
68240 49 4d 41 52 59 20 4b 45 59 20 6d 75 73 74 20 62  IMARY KEY must b
68250 65 20 49 4e 54 45 47 45 52 20 66 6f 72 20 74 68  e INTEGER for th
68260 69 73 20 66 69 65 6c 64 20 74 6f 0a 2a 2a 20 62  is field to.** b
68270 65 20 73 65 74 2e 20 20 41 6e 20 49 4e 54 45 47  e set.  An INTEG
68280 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 69  ER PRIMARY KEY i
68290 73 20 75 73 65 64 20 61 73 20 74 68 65 20 72 6f  s used as the ro
682a0 77 69 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  wid for each row
682b0 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65   of.** the table
682c0 2e 20 20 49 66 20 61 20 74 61 62 6c 65 20 68 61  .  If a table ha
682d0 73 20 6e 6f 20 49 4e 54 45 47 45 52 20 50 52 49  s no INTEGER PRI
682e0 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e 20 61  MARY KEY, then a
682f0 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 0a 2a 2a   random rowid.**
68300 20 69 73 20 67 65 6e 65 72 61 74 65 64 20 66 6f   is generated fo
68310 72 20 65 61 63 68 20 72 6f 77 20 6f 66 20 74 68  r each row of th
68320 65 20 74 61 62 6c 65 2e 20 20 54 46 5f 48 61 73  e table.  TF_Has
68330 50 72 69 6d 61 72 79 4b 65 79 20 69 73 20 73 65  PrimaryKey is se
68340 74 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  t if.** the tabl
68350 65 20 68 61 73 20 61 6e 79 20 50 52 49 4d 41 52  e has any PRIMAR
68360 59 20 4b 45 59 2c 20 49 4e 54 45 47 45 52 20 6f  Y KEY, INTEGER o
68370 72 20 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 0a  r otherwise..**.
68380 2a 2a 20 54 61 62 6c 65 2e 74 6e 75 6d 20 69 73  ** Table.tnum is
68390 20 74 68 65 20 70 61 67 65 20 6e 75 6d 62 65 72   the page number
683a0 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 20 42 54   for the root BT
683b0 72 65 65 20 70 61 67 65 20 6f 66 20 74 68 65 20  ree page of the 
683c0 74 61 62 6c 65 20 69 6e 20 74 68 65 0a 2a 2a 20  table in the.** 
683d0 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20  database file.  
683e0 49 66 20 54 61 62 6c 65 2e 69 44 62 20 69 73 20  If Table.iDb is 
683f0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
68400 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20   database table 
68410 62 61 63 6b 65 6e 64 0a 2a 2a 20 69 6e 20 73 71  backend.** in sq
68420 6c 69 74 65 2e 61 44 62 5b 5d 2e 20 20 30 20 69  lite.aDb[].  0 i
68430 73 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20 64  s for the main d
68440 61 74 61 62 61 73 65 20 61 6e 64 20 31 20 69 73  atabase and 1 is
68450 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 74 68   for the file th
68460 61 74 0a 2a 2a 20 68 6f 6c 64 73 20 74 65 6d 70  at.** holds temp
68470 6f 72 61 72 79 20 74 61 62 6c 65 73 20 61 6e 64  orary tables and
68480 20 69 6e 64 69 63 65 73 2e 20 20 49 66 20 54 46   indices.  If TF
68490 5f 45 70 68 65 6d 65 72 61 6c 20 69 73 20 73 65  _Ephemeral is se
684a0 74 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 74 61  t.** then the ta
684b0 62 6c 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ble is stored in
684c0 20 61 20 66 69 6c 65 20 74 68 61 74 20 69 73 20   a file that is 
684d0 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 64 65  automatically de
684e0 6c 65 74 65 64 0a 2a 2a 20 77 68 65 6e 20 74 68  leted.** when th
684f0 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 74 6f  e VDBE cursor to
68500 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 63 6c   the table is cl
68510 6f 73 65 64 2e 20 20 49 6e 20 74 68 69 73 20 63  osed.  In this c
68520 61 73 65 20 54 61 62 6c 65 2e 74 6e 75 6d 20 0a  ase Table.tnum .
68530 2a 2a 20 72 65 66 65 72 73 20 56 44 42 45 20 63  ** refers VDBE c
68540 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 68 61  ursor number tha
68550 74 20 68 6f 6c 64 73 20 74 68 65 20 74 61 62 6c  t holds the tabl
68560 65 20 6f 70 65 6e 2c 20 6e 6f 74 20 74 6f 20 74  e open, not to t
68570 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67 65 20  he root.** page 
68580 6e 75 6d 62 65 72 2e 20 20 54 72 61 6e 73 69 65  number.  Transie
68590 6e 74 20 74 61 62 6c 65 73 20 61 72 65 20 75 73  nt tables are us
685a0 65 64 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72  ed to hold the r
685b0 65 73 75 6c 74 73 20 6f 66 20 61 0a 2a 2a 20 73  esults of a.** s
685c0 75 62 2d 71 75 65 72 79 20 74 68 61 74 20 61 70  ub-query that ap
685d0 70 65 61 72 73 20 69 6e 73 74 65 61 64 20 6f 66  pears instead of
685e0 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 6e 61   a real table na
685f0 6d 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  me in the FROM c
68600 6c 61 75 73 65 20 0a 2a 2a 20 6f 66 20 61 20 53  lause .** of a S
68610 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
68620 0a 2a 2f 0a 73 74 72 75 63 74 20 54 61 62 6c 65  .*/.struct Table
68630 20 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62   {.  sqlite3 *db
68640 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 73 73  ;         /* Ass
68650 6f 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65  ociated database
68660 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 20 4d 69   connection.  Mi
68670 67 68 74 20 62 65 20 4e 55 4c 4c 2e 20 2a 2f 0a  ght be NULL. */.
68680 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20    char *zName;  
68690 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
686a0 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76  f the table or v
686b0 69 65 77 20 2a 2f 0a 20 20 69 6e 74 20 69 50 4b  iew */.  int iPK
686c0 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ey;           /*
686d0 20 49 66 20 6e 6f 74 20 6e 65 67 61 74 69 76 65   If not negative
686e0 2c 20 75 73 65 20 61 43 6f 6c 5b 69 50 4b 65 79  , use aCol[iPKey
686f0 5d 20 61 73 20 74 68 65 20 70 72 69 6d 61 72 79  ] as the primary
68700 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e 43   key */.  int nC
68710 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  ol;            /
68720 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75  * Number of colu
68730 6d 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c  mns in this tabl
68740 65 20 2a 2f 0a 20 20 43 6f 6c 75 6d 6e 20 2a 61  e */.  Column *a
68750 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 49  Col;        /* I
68760 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
68770 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   each column */.
68780 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
68790 20 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f         /* List o
687a0 66 20 53 51 4c 20 69 6e 64 65 78 65 73 20 6f 6e  f SQL indexes on
687b0 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a 2f 0a   this table. */.
687c0 20 20 69 6e 74 20 74 6e 75 6d 3b 20 20 20 20 20    int tnum;     
687d0 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 42         /* Root B
687e0 54 72 65 65 20 6e 6f 64 65 20 66 6f 72 20 74 68  Tree node for th
687f0 69 73 20 74 61 62 6c 65 20 28 73 65 65 20 6e 6f  is table (see no
68800 74 65 20 61 62 6f 76 65 29 20 2a 2f 0a 20 20 53  te above) */.  S
68810 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20  elect *pSelect; 
68820 20 20 20 20 2f 2a 20 4e 55 4c 4c 20 66 6f 72 20      /* NULL for 
68830 74 61 62 6c 65 73 2e 20 20 50 6f 69 6e 74 73 20  tables.  Points 
68840 74 6f 20 64 65 66 69 6e 69 74 69 6f 6e 20 69 66  to definition if
68850 20 61 20 76 69 65 77 2e 20 2a 2f 0a 20 20 75 31   a view. */.  u1
68860 36 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  6 nRef;         
68870 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
68880 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 69 73  pointers to this
68890 20 54 61 62 6c 65 20 2a 2f 0a 20 20 75 38 20 74   Table */.  u8 t
688a0 61 62 46 6c 61 67 73 3b 20 20 20 20 20 20 20 20  abFlags;        
688b0 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 54 46 5f 2a   /* Mask of TF_*
688c0 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38 20   values */.  u8 
688d0 6b 65 79 43 6f 6e 66 3b 20 20 20 20 20 20 20 20  keyConf;        
688e0 20 20 2f 2a 20 57 68 61 74 20 74 6f 20 64 6f 20    /* What to do 
688f0 69 6e 20 63 61 73 65 20 6f 66 20 75 6e 69 71 75  in case of uniqu
68900 65 6e 65 73 73 20 63 6f 6e 66 6c 69 63 74 20 6f  eness conflict o
68910 6e 20 69 50 4b 65 79 20 2a 2f 0a 20 20 54 72 69  n iPKey */.  Tri
68920 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
68930 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 53 51 4c    /* List of SQL
68940 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74 68 69   triggers on thi
68950 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 46 4b 65  s table */.  FKe
68960 79 20 2a 70 46 4b 65 79 3b 20 20 20 20 20 20 20  y *pFKey;       
68970 20 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74    /* Linked list
68980 20 6f 66 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20   of all foreign 
68990 6b 65 79 73 20 69 6e 20 74 68 69 73 20 74 61 62  keys in this tab
689a0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43  le */.  char *zC
689b0 6f 6c 41 66 66 3b 20 20 20 20 20 20 20 2f 2a 20  olAff;       /* 
689c0 53 74 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20  String defining 
689d0 74 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20  the affinity of 
689e0 65 61 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 23  each column */.#
689f0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
68a00 49 54 5f 43 48 45 43 4b 0a 20 20 45 78 70 72 20  IT_CHECK.  Expr 
68a10 2a 70 43 68 65 63 6b 3b 20 20 20 20 20 20 20 20  *pCheck;        
68a20 2f 2a 20 54 68 65 20 41 4e 44 20 6f 66 20 61 6c  /* The AND of al
68a30 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
68a40 6e 74 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69  nts */.#endif.#i
68a50 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
68a60 54 5f 41 4c 54 45 52 54 41 42 4c 45 0a 20 20 69  T_ALTERTABLE.  i
68a70 6e 74 20 61 64 64 43 6f 6c 4f 66 66 73 65 74 3b  nt addColOffset;
68a80 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
68a90 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
68aa0 6d 74 20 74 6f 20 61 64 64 20 61 20 6e 65 77 20  mt to add a new 
68ab0 63 6f 6c 75 6d 6e 20 2a 2f 0a 23 65 6e 64 69 66  column */.#endif
68ac0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
68ad0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
68ae0 45 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64  E.  Module *pMod
68af0 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e  ;        /* Poin
68b00 74 65 72 20 74 6f 20 74 68 65 20 69 6d 70 6c 65  ter to the imple
68b10 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
68b20 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 73 71 6c   module */.  sql
68b30 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62  ite3_vtab *pVtab
68b40 3b 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20  ; /* Pointer to 
68b50 74 68 65 20 6d 6f 64 75 6c 65 20 69 6e 73 74 61  the module insta
68b60 6e 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 6f  nce */.  int nMo
68b70 64 75 6c 65 41 72 67 3b 20 20 20 20 20 20 2f 2a  duleArg;      /*
68b80 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75 6d   Number of argum
68b90 65 6e 74 73 20 74 6f 20 74 68 65 20 6d 6f 64 75  ents to the modu
68ba0 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  le */.  char **a
68bb0 7a 4d 6f 64 75 6c 65 41 72 67 3b 20 20 2f 2a 20  zModuleArg;  /* 
68bc0 54 65 78 74 20 6f 66 20 61 6c 6c 20 6d 6f 64 75  Text of all modu
68bd0 6c 65 20 61 72 67 73 2e 20 5b 30 5d 20 69 73 20  le args. [0] is 
68be0 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 2a 2f 0a 23  module name */.#
68bf0 65 6e 64 69 66 0a 20 20 53 63 68 65 6d 61 20 2a  endif.  Schema *
68c00 70 53 63 68 65 6d 61 3b 20 20 20 20 20 2f 2a 20  pSchema;     /* 
68c10 53 63 68 65 6d 61 20 74 68 61 74 20 63 6f 6e 74  Schema that cont
68c20 61 69 6e 73 20 74 68 69 73 20 74 61 62 6c 65 20  ains this table 
68c30 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 78  */.  Table *pNex
68c40 74 5a 6f 6d 62 69 65 3b 20 20 2f 2a 20 4e 65 78  tZombie;  /* Nex
68c50 74 20 6f 6e 20 74 68 65 20 50 61 72 73 65 2e 70  t on the Parse.p
68c60 5a 6f 6d 62 69 65 54 61 62 20 6c 69 73 74 20 2a  ZombieTab list *
68c70 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  /.};../*.** Allo
68c80 77 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 54  wed values for T
68c90 61 62 65 2e 74 61 62 46 6c 61 67 73 2e 0a 2a 2f  abe.tabFlags..*/
68ca0 0a 23 64 65 66 69 6e 65 20 54 46 5f 52 65 61 64  .#define TF_Read
68cb0 6f 6e 6c 79 20 20 20 20 20 20 20 20 30 78 30 31  only        0x01
68cc0 20 20 20 20 2f 2a 20 52 65 61 64 2d 6f 6e 6c 79      /* Read-only
68cd0 20 73 79 73 74 65 6d 20 74 61 62 6c 65 20 2a 2f   system table */
68ce0 0a 23 64 65 66 69 6e 65 20 54 46 5f 45 70 68 65  .#define TF_Ephe
68cf0 6d 65 72 61 6c 20 20 20 20 20 20 20 30 78 30 32  meral       0x02
68d00 20 20 20 20 2f 2a 20 41 6e 20 65 6d 70 68 65 72      /* An empher
68d10 6d 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65  mal table */.#de
68d20 66 69 6e 65 20 54 46 5f 48 61 73 50 72 69 6d 61  fine TF_HasPrima
68d30 72 79 4b 65 79 20 20 20 30 78 30 34 20 20 20 20  ryKey   0x04    
68d40 2f 2a 20 54 61 62 6c 65 20 68 61 73 20 61 20 70  /* Table has a p
68d50 72 69 6d 61 72 79 20 6b 65 79 20 2a 2f 0a 23 64  rimary key */.#d
68d60 65 66 69 6e 65 20 54 46 5f 41 75 74 6f 69 6e 63  efine TF_Autoinc
68d70 72 65 6d 65 6e 74 20 20 20 30 78 30 38 20 20 20  rement   0x08   
68d80 20 2f 2a 20 49 6e 74 65 67 65 72 20 70 72 69 6d   /* Integer prim
68d90 61 72 79 20 6b 65 79 20 69 73 20 61 75 74 6f 69  ary key is autoi
68da0 6e 63 72 65 6d 65 6e 74 20 2a 2f 0a 23 64 65 66  ncrement */.#def
68db0 69 6e 65 20 54 46 5f 56 69 72 74 75 61 6c 20 20  ine TF_Virtual  
68dc0 20 20 20 20 20 20 20 30 78 31 30 20 20 20 20 2f         0x10    /
68dd0 2a 20 49 73 20 61 20 76 69 72 74 75 61 6c 20 74  * Is a virtual t
68de0 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20  able */.#define 
68df0 54 46 5f 4e 65 65 64 4d 65 74 61 64 61 74 61 20  TF_NeedMetadata 
68e00 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20 61 43     0x20    /* aC
68e10 6f 6c 5b 5d 2e 7a 54 79 70 65 20 61 6e 64 20 61  ol[].zType and a
68e20 43 6f 6c 5b 5d 2e 70 43 6f 6c 6c 20 6d 69 73 73  Col[].pColl miss
68e30 69 6e 67 20 2a 2f 0a 0a 0a 0a 2f 2a 0a 2a 2a 20  ing */..../*.** 
68e40 54 65 73 74 20 74 6f 20 73 65 65 20 77 68 65 74  Test to see whet
68e50 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 74 61 62  her or not a tab
68e60 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c 20  le is a virtual 
68e70 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a  table.  This is.
68e80 2a 2a 20 64 6f 6e 65 20 61 73 20 61 20 6d 61 63  ** done as a mac
68e90 72 6f 20 73 6f 20 74 68 61 74 20 69 74 20 77 69  ro so that it wi
68ea0 6c 6c 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20  ll be optimized 
68eb0 6f 75 74 20 77 68 65 6e 20 76 69 72 74 75 61 6c  out when virtual
68ec0 0a 2a 2a 20 74 61 62 6c 65 20 73 75 70 70 6f 72  .** table suppor
68ed0 74 20 69 73 20 6f 6d 69 74 74 65 64 20 66 72 6f  t is omitted fro
68ee0 6d 20 74 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a  m the build..*/.
68ef0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
68f00 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
68f10 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72  .#  define IsVir
68f20 74 75 61 6c 28 58 29 20 20 20 20 20 20 28 28 28  tual(X)      (((
68f30 58 29 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  X)->tabFlags & T
68f40 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 29 0a 23  F_Virtual)!=0).#
68f50 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65    define IsHidde
68f60 6e 43 6f 6c 75 6d 6e 28 58 29 20 28 28 58 29 2d  nColumn(X) ((X)-
68f70 3e 69 73 48 69 64 64 65 6e 29 0a 23 65 6c 73 65  >isHidden).#else
68f80 0a 23 20 20 64 65 66 69 6e 65 20 49 73 56 69 72  .#  define IsVir
68f90 74 75 61 6c 28 58 29 20 20 20 20 20 20 30 0a 23  tual(X)      0.#
68fa0 20 20 64 65 66 69 6e 65 20 49 73 48 69 64 64 65    define IsHidde
68fb0 6e 43 6f 6c 75 6d 6e 28 58 29 20 30 0a 23 65 6e  nColumn(X) 0.#en
68fc0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  dif../*.** Each 
68fd0 66 6f 72 65 69 67 6e 20 6b 65 79 20 63 6f 6e 73  foreign key cons
68fe0 74 72 61 69 6e 74 20 69 73 20 61 6e 20 69 6e 73  traint is an ins
68ff0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
69000 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
69010 2e 0a 2a 2a 0a 2a 2a 20 41 20 66 6f 72 65 69 67  ..**.** A foreig
69020 6e 20 6b 65 79 20 69 73 20 61 73 73 6f 63 69 61  n key is associa
69030 74 65 64 20 77 69 74 68 20 74 77 6f 20 74 61 62  ted with two tab
69040 6c 65 73 2e 20 20 54 68 65 20 22 66 72 6f 6d 22  les.  The "from"
69050 20 74 61 62 6c 65 20 69 73 0a 2a 2a 20 74 68 65   table is.** the
69060 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74   table that cont
69070 61 69 6e 73 20 74 68 65 20 52 45 46 45 52 45 4e  ains the REFEREN
69080 43 45 53 20 63 6c 61 75 73 65 20 74 68 61 74 20  CES clause that 
69090 63 72 65 61 74 65 73 20 74 68 65 20 66 6f 72 65  creates the fore
690a0 69 67 6e 0a 2a 2a 20 6b 65 79 2e 20 20 54 68 65  ign.** key.  The
690b0 20 22 74 6f 22 20 74 61 62 6c 65 20 69 73 20 74   "to" table is t
690c0 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 69 73  he table that is
690d0 20 6e 61 6d 65 64 20 69 6e 20 74 68 65 20 52 45   named in the RE
690e0 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73 65 2e  FERENCES clause.
690f0 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 74 68 69  .** Consider thi
69100 73 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a  s example:.**.**
69110 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42 4c       CREATE TABL
69120 45 20 65 78 31 28 0a 2a 2a 20 20 20 20 20 20 20  E ex1(.**       
69130 61 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  a INTEGER PRIMAR
69140 59 20 4b 45 59 2c 0a 2a 2a 20 20 20 20 20 20 20  Y KEY,.**       
69150 62 20 49 4e 54 45 47 45 52 20 43 4f 4e 53 54 52  b INTEGER CONSTR
69160 41 49 4e 54 20 66 6b 31 20 52 45 46 45 52 45 4e  AINT fk1 REFEREN
69170 43 45 53 20 65 78 32 28 78 29 0a 2a 2a 20 20 20  CES ex2(x).**   
69180 20 20 29 3b 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 66    );.**.** For f
69190 6f 72 65 69 67 6e 20 6b 65 79 20 22 66 6b 31 22  oreign key "fk1"
691a0 2c 20 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65  , the from-table
691b0 20 69 73 20 22 65 78 31 22 20 61 6e 64 20 74 68   is "ex1" and th
691c0 65 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 22 65  e to-table is "e
691d0 78 32 22 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20  x2"..**.** Each 
691e0 52 45 46 45 52 45 4e 43 45 53 20 63 6c 61 75 73  REFERENCES claus
691f0 65 20 67 65 6e 65 72 61 74 65 73 20 61 6e 20 69  e generates an i
69200 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
69210 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
69220 72 65 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 61  re.** which is a
69230 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 66  ttached to the f
69240 72 6f 6d 2d 74 61 62 6c 65 2e 20 20 54 68 65 20  rom-table.  The 
69250 74 6f 2d 74 61 62 6c 65 20 6e 65 65 64 20 6e 6f  to-table need no
69260 74 20 65 78 69 73 74 20 77 68 65 6e 0a 2a 2a 20  t exist when.** 
69270 74 68 65 20 66 72 6f 6d 2d 74 61 62 6c 65 20 69  the from-table i
69280 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20  s created.  The 
69290 65 78 69 73 74 61 6e 63 65 20 6f 66 20 74 68 65  existance of the
692a0 20 74 6f 2d 74 61 62 6c 65 20 69 73 20 6e 6f 74   to-table is not
692b0 20 63 68 65 63 6b 65 64 0a 2a 2a 20 75 6e 74 69   checked.** unti
692c0 6c 20 61 6e 20 61 74 74 65 6d 70 74 20 69 73 20  l an attempt is 
692d0 6d 61 64 65 20 74 6f 20 69 6e 73 65 72 74 20 64  made to insert d
692e0 61 74 61 20 69 6e 74 6f 20 74 68 65 20 66 72 6f  ata into the fro
692f0 6d 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  m-table..**.** T
69300 68 65 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20  he sqlite.aFKey 
69310 68 61 73 68 20 74 61 62 6c 65 20 73 74 6f 72 65  hash table store
69320 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74 68  s pointers to th
69330 69 73 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20  is structure.** 
69340 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f  given the name o
69350 66 20 61 20 74 6f 2d 74 61 62 6c 65 2e 20 20 46  f a to-table.  F
69360 6f 72 20 65 61 63 68 20 74 6f 2d 74 61 62 6c 65  or each to-table
69370 2c 20 61 6c 6c 20 66 6f 72 65 69 67 6e 20 6b 65  , all foreign ke
69380 79 73 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  ys.** associated
69390 20 77 69 74 68 20 74 68 61 74 20 74 61 62 6c 65   with that table
693a0 20 61 72 65 20 6f 6e 20 61 20 6c 69 6e 6b 65 64   are on a linked
693b0 20 6c 69 73 74 20 75 73 69 6e 67 20 74 68 65 20   list using the 
693c0 46 4b 65 79 2e 70 4e 65 78 74 54 6f 0a 2a 2a 20  FKey.pNextTo.** 
693d0 66 69 65 6c 64 2e 0a 2a 2f 0a 73 74 72 75 63 74  field..*/.struct
693e0 20 46 4b 65 79 20 7b 0a 20 20 54 61 62 6c 65 20   FKey {.  Table 
693f0 2a 70 46 72 6f 6d 3b 20 20 20 20 20 2f 2a 20 54  *pFrom;     /* T
69400 68 65 20 74 61 62 6c 65 20 74 68 61 74 20 63 6f  he table that co
69410 6e 73 74 61 69 6e 73 20 74 68 65 20 52 45 46 45  nstains the REFE
69420 52 45 4e 43 45 53 20 63 6c 61 75 73 65 20 2a 2f  RENCES clause */
69430 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78 74 46 72  .  FKey *pNextFr
69440 6f 6d 3b 20 20 2f 2a 20 4e 65 78 74 20 66 6f 72  om;  /* Next for
69450 65 69 67 6e 20 6b 65 79 20 69 6e 20 70 46 72 6f  eign key in pFro
69460 6d 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f  m */.  char *zTo
69470 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ;        /* Name
69480 20 6f 66 20 74 61 62 6c 65 20 74 68 61 74 20 74   of table that t
69490 68 65 20 6b 65 79 20 70 6f 69 6e 74 73 20 74 6f  he key points to
694a0 20 2a 2f 0a 20 20 46 4b 65 79 20 2a 70 4e 65 78   */.  FKey *pNex
694b0 74 54 6f 3b 20 20 20 20 2f 2a 20 4e 65 78 74 20  tTo;    /* Next 
694c0 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 68 61 74  foreign key that
694d0 20 70 6f 69 6e 74 73 20 74 6f 20 7a 54 6f 20 2a   points to zTo *
694e0 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 20 20 20  /.  int nCol;   
694f0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
69500 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  of columns in th
69510 69 73 20 6b 65 79 20 2a 2f 0a 20 20 73 74 72 75  is key */.  stru
69520 63 74 20 73 43 6f 6c 4d 61 70 20 7b 20 20 2f 2a  ct sColMap {  /*
69530 20 4d 61 70 70 69 6e 67 20 6f 66 20 63 6f 6c 75   Mapping of colu
69540 6d 6e 73 20 69 6e 20 70 46 72 6f 6d 20 74 6f 20  mns in pFrom to 
69550 63 6f 6c 75 6d 6e 73 20 69 6e 20 7a 54 6f 20 2a  columns in zTo *
69560 2f 0a 20 20 20 20 69 6e 74 20 69 46 72 6f 6d 3b  /.    int iFrom;
69570 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
69580 78 20 6f 66 20 63 6f 6c 75 6d 6e 20 69 6e 20 70  x of column in p
69590 46 72 6f 6d 20 2a 2f 0a 20 20 20 20 63 68 61 72  From */.    char
695a0 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20 2f   *zCol;        /
695b0 2a 20 4e 61 6d 65 20 6f 66 20 63 6f 6c 75 6d 6e  * Name of column
695c0 20 69 6e 20 7a 54 6f 2e 20 20 49 66 20 30 20 75   in zTo.  If 0 u
695d0 73 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 2a  se PRIMARY KEY *
695e0 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 20 20 20 20  /.  } *aCol;    
695f0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74        /* One ent
69600 72 79 20 66 6f 72 20 65 61 63 68 20 6f 66 20 6e  ry for each of n
69610 43 6f 6c 20 63 6f 6c 75 6d 6e 20 73 20 2a 2f 0a  Col column s */.
69620 20 20 75 38 20 69 73 44 65 66 65 72 72 65 64 3b    u8 isDeferred;
69630 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
69640 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63 6b 69  onstraint checki
69650 6e 67 20 69 73 20 64 65 66 65 72 72 65 64 20 74  ng is deferred t
69660 69 6c 6c 20 43 4f 4d 4d 49 54 20 2a 2f 0a 20 20  ill COMMIT */.  
69670 75 38 20 75 70 64 61 74 65 43 6f 6e 66 3b 20 20  u8 updateConf;  
69680 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f    /* How to reso
69690 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68  lve conflicts th
696a0 61 74 20 6f 63 63 75 72 20 6f 6e 20 55 50 44 41  at occur on UPDA
696b0 54 45 20 2a 2f 0a 20 20 75 38 20 64 65 6c 65 74  TE */.  u8 delet
696c0 65 43 6f 6e 66 3b 20 20 20 20 2f 2a 20 48 6f 77  eConf;    /* How
696d0 20 74 6f 20 72 65 73 6f 6c 76 65 20 63 6f 6e 66   to resolve conf
696e0 6c 69 63 74 73 20 74 68 61 74 20 6f 63 63 75 72  licts that occur
696f0 20 6f 6e 20 44 45 4c 45 54 45 20 2a 2f 0a 20 20   on DELETE */.  
69700 75 38 20 69 6e 73 65 72 74 43 6f 6e 66 3b 20 20  u8 insertConf;  
69710 20 20 2f 2a 20 48 6f 77 20 74 6f 20 72 65 73 6f    /* How to reso
69720 6c 76 65 20 63 6f 6e 66 6c 69 63 74 73 20 74 68  lve conflicts th
69730 61 74 20 6f 63 63 75 72 20 6f 6e 20 49 4e 53 45  at occur on INSE
69740 52 54 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  RT */.};../*.** 
69750 53 51 4c 69 74 65 20 73 75 70 70 6f 72 74 73 20  SQLite supports 
69760 6d 61 6e 79 20 64 69 66 66 65 72 65 6e 74 20 77  many different w
69770 61 79 73 20 74 6f 20 72 65 73 6f 6c 76 65 20 61  ays to resolve a
69780 20 63 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65   constraint.** e
69790 72 72 6f 72 2e 20 20 52 4f 4c 4c 42 41 43 4b 20  rror.  ROLLBACK 
697a0 70 72 6f 63 65 73 73 69 6e 67 20 6d 65 61 6e 73  processing means
697b0 20 74 68 61 74 20 61 20 63 6f 6e 73 74 72 61 69   that a constrai
697c0 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 0a 2a 2a 20  nt violation.** 
697d0 63 61 75 73 65 73 20 74 68 65 20 6f 70 65 72 61  causes the opera
697e0 74 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 20  tion in process 
697f0 74 6f 20 66 61 69 6c 20 61 6e 64 20 66 6f 72 20  to fail and for 
69800 74 68 65 20 63 75 72 72 65 6e 74 20 74 72 61 6e  the current tran
69810 73 61 63 74 69 6f 6e 0a 2a 2a 20 74 6f 20 62 65  saction.** to be
69820 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41   rolled back.  A
69830 42 4f 52 54 20 70 72 6f 63 65 73 73 69 6e 67 20  BORT processing 
69840 6d 65 61 6e 73 20 74 68 65 20 6f 70 65 72 61 74  means the operat
69850 69 6f 6e 20 69 6e 20 70 72 6f 63 65 73 73 0a 2a  ion in process.*
69860 2a 20 66 61 69 6c 73 20 61 6e 64 20 61 6e 79 20  * fails and any 
69870 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 66 72  prior changes fr
69880 6f 6d 20 74 68 61 74 20 6f 6e 65 20 6f 70 65 72  om that one oper
69890 61 74 69 6f 6e 20 61 72 65 20 62 61 63 6b 65 64  ation are backed
698a0 20 6f 75 74 2c 0a 2a 2a 20 62 75 74 20 74 68 65   out,.** but the
698b0 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
698c0 6e 6f 74 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e  not rolled back.
698d0 20 20 46 41 49 4c 20 70 72 6f 63 65 73 73 69 6e    FAIL processin
698e0 67 20 6d 65 61 6e 73 20 74 68 61 74 0a 2a 2a 20  g means that.** 
698f0 74 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 69 6e  the operation in
69900 20 70 72 6f 67 72 65 73 73 20 73 74 6f 70 73 20   progress stops 
69910 61 6e 64 20 72 65 74 75 72 6e 73 20 61 6e 20 65  and returns an e
69920 72 72 6f 72 20 63 6f 64 65 2e 20 20 42 75 74 20  rror code.  But 
69930 70 72 69 6f 72 0a 2a 2a 20 63 68 61 6e 67 65 73  prior.** changes
69940 20 64 75 65 20 74 6f 20 74 68 65 20 73 61 6d 65   due to the same
69950 20 6f 70 65 72 61 74 69 6f 6e 20 61 72 65 20 6e   operation are n
69960 6f 74 20 62 61 63 6b 65 64 20 6f 75 74 20 61 6e  ot backed out an
69970 64 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a  d no rollback.**
69980 20 6f 63 63 75 72 73 2e 20 20 49 47 4e 4f 52 45   occurs.  IGNORE
69990 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
699a0 70 61 72 74 69 63 75 6c 61 72 20 72 6f 77 20 74  particular row t
699b0 68 61 74 20 63 61 75 73 65 64 20 74 68 65 20 63  hat caused the c
699c0 6f 6e 73 74 72 61 69 6e 74 0a 2a 2a 20 65 72 72  onstraint.** err
699d0 6f 72 20 69 73 20 6e 6f 74 20 69 6e 73 65 72 74  or is not insert
699e0 65 64 20 6f 72 20 75 70 64 61 74 65 64 2e 20 20  ed or updated.  
699f0 50 72 6f 63 65 73 73 69 6e 67 20 63 6f 6e 74 69  Processing conti
69a00 6e 75 65 73 20 61 6e 64 20 6e 6f 20 65 72 72 6f  nues and no erro
69a10 72 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64  r.** is returned
69a20 2e 20 20 52 45 50 4c 41 43 45 20 6d 65 61 6e 73  .  REPLACE means
69a30 20 74 68 61 74 20 70 72 65 65 78 69 73 74 69 6e   that preexistin
69a40 67 20 64 61 74 61 62 61 73 65 20 72 6f 77 73 20  g database rows 
69a50 74 68 61 74 20 63 61 75 73 65 64 0a 2a 2a 20 61  that caused.** a
69a60 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69   UNIQUE constrai
69a70 6e 74 20 76 69 6f 6c 61 74 69 6f 6e 20 61 72 65  nt violation are
69a80 20 72 65 6d 6f 76 65 64 20 73 6f 20 74 68 61 74   removed so that
69a90 20 74 68 65 20 6e 65 77 20 69 6e 73 65 72 74 20   the new insert 
69aa0 6f 72 0a 2a 2a 20 75 70 64 61 74 65 20 63 61 6e  or.** update can
69ab0 20 70 72 6f 63 65 65 64 2e 20 20 50 72 6f 63 65   proceed.  Proce
69ac0 73 73 69 6e 67 20 63 6f 6e 74 69 6e 75 65 73 20  ssing continues 
69ad0 61 6e 64 20 6e 6f 20 65 72 72 6f 72 20 69 73 20  and no error is 
69ae0 72 65 70 6f 72 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  reported..**.** 
69af0 52 45 53 54 52 49 43 54 2c 20 53 45 54 4e 55 4c  RESTRICT, SETNUL
69b00 4c 2c 20 61 6e 64 20 43 41 53 43 41 44 45 20 61  L, and CASCADE a
69b10 63 74 69 6f 6e 73 20 61 70 70 6c 79 20 6f 6e 6c  ctions apply onl
69b20 79 20 74 6f 20 66 6f 72 65 69 67 6e 20 6b 65 79  y to foreign key
69b30 73 2e 0a 2a 2a 20 52 45 53 54 52 49 43 54 20 69  s..** RESTRICT i
69b40 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 41 42  s the same as AB
69b50 4f 52 54 20 66 6f 72 20 49 4d 4d 45 44 49 41 54  ORT for IMMEDIAT
69b60 45 20 66 6f 72 65 69 67 6e 20 6b 65 79 73 20 61  E foreign keys a
69b70 6e 64 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 61  nd the.** same a
69b80 73 20 52 4f 4c 4c 42 41 43 4b 20 66 6f 72 20 44  s ROLLBACK for D
69b90 45 46 45 52 52 45 44 20 6b 65 79 73 2e 20 20 53  EFERRED keys.  S
69ba0 45 54 4e 55 4c 4c 20 6d 65 61 6e 73 20 74 68 61  ETNULL means tha
69bb0 74 20 74 68 65 20 66 6f 72 65 69 67 6e 0a 2a 2a  t the foreign.**
69bc0 20 6b 65 79 20 69 73 20 73 65 74 20 74 6f 20 4e   key is set to N
69bd0 55 4c 4c 2e 20 20 43 41 53 43 41 44 45 20 6d 65  ULL.  CASCADE me
69be0 61 6e 73 20 74 68 61 74 20 61 20 44 45 4c 45 54  ans that a DELET
69bf0 45 20 6f 72 20 55 50 44 41 54 45 20 6f 66 20 74  E or UPDATE of t
69c00 68 65 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 64  he.** referenced
69c10 20 74 61 62 6c 65 20 72 6f 77 20 69 73 20 70 72   table row is pr
69c20 6f 70 61 67 61 74 65 64 20 69 6e 74 6f 20 74 68  opagated into th
69c30 65 20 72 6f 77 20 74 68 61 74 20 68 6f 6c 64 73  e row that holds
69c40 20 74 68 65 0a 2a 2a 20 66 6f 72 65 69 67 6e 20   the.** foreign 
69c50 6b 65 79 2e 0a 2a 2a 20 0a 2a 2a 20 54 68 65 20  key..** .** The 
69c60 66 6f 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c  following symbol
69c70 69 63 20 76 61 6c 75 65 73 20 61 72 65 20 75 73  ic values are us
69c80 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68 69  ed to record whi
69c90 63 68 20 74 79 70 65 0a 2a 2a 20 6f 66 20 61 63  ch type.** of ac
69ca0 74 69 6f 6e 20 74 6f 20 74 61 6b 65 2e 0a 2a 2f  tion to take..*/
69cb0 0a 23 64 65 66 69 6e 65 20 4f 45 5f 4e 6f 6e 65  .#define OE_None
69cc0 20 20 20 20 20 30 20 20 20 2f 2a 20 54 68 65 72       0   /* Ther
69cd0 65 20 69 73 20 6e 6f 20 63 6f 6e 73 74 72 61 69  e is no constrai
69ce0 6e 74 20 74 6f 20 63 68 65 63 6b 20 2a 2f 0a 23  nt to check */.#
69cf0 64 65 66 69 6e 65 20 4f 45 5f 52 6f 6c 6c 62 61  define OE_Rollba
69d00 63 6b 20 31 20 20 20 2f 2a 20 46 61 69 6c 20 74  ck 1   /* Fail t
69d10 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e 64  he operation and
69d20 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 74 72   rollback the tr
69d30 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65  ansaction */.#de
69d40 66 69 6e 65 20 4f 45 5f 41 62 6f 72 74 20 20 20  fine OE_Abort   
69d50 20 32 20 20 20 2f 2a 20 42 61 63 6b 20 6f 75 74   2   /* Back out
69d60 20 63 68 61 6e 67 65 73 20 62 75 74 20 64 6f 20   changes but do 
69d70 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61 6e  no rollback tran
69d80 73 61 63 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69  saction */.#defi
69d90 6e 65 20 4f 45 5f 46 61 69 6c 20 20 20 20 20 33  ne OE_Fail     3
69da0 20 20 20 2f 2a 20 53 74 6f 70 20 74 68 65 20 6f     /* Stop the o
69db0 70 65 72 61 74 69 6f 6e 20 62 75 74 20 6c 65 61  peration but lea
69dc0 76 65 20 61 6c 6c 20 70 72 69 6f 72 20 63 68 61  ve all prior cha
69dd0 6e 67 65 73 20 2a 2f 0a 23 64 65 66 69 6e 65 20  nges */.#define 
69de0 4f 45 5f 49 67 6e 6f 72 65 20 20 20 34 20 20 20  OE_Ignore   4   
69df0 2f 2a 20 49 67 6e 6f 72 65 20 74 68 65 20 65 72  /* Ignore the er
69e00 72 6f 72 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74  ror. Do not do t
69e10 68 65 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44  he INSERT or UPD
69e20 41 54 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f  ATE */.#define O
69e30 45 5f 52 65 70 6c 61 63 65 20 20 35 20 20 20 2f  E_Replace  5   /
69e40 2a 20 44 65 6c 65 74 65 20 65 78 69 73 74 69 6e  * Delete existin
69e50 67 20 72 65 63 6f 72 64 2c 20 74 68 65 6e 20 64  g record, then d
69e60 6f 20 49 4e 53 45 52 54 20 6f 72 20 55 50 44 41  o INSERT or UPDA
69e70 54 45 20 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4f  TE */..#define O
69e80 45 5f 52 65 73 74 72 69 63 74 20 36 20 20 20 2f  E_Restrict 6   /
69e90 2a 20 4f 45 5f 41 62 6f 72 74 20 66 6f 72 20 49  * OE_Abort for I
69ea0 4d 4d 45 44 49 41 54 45 2c 20 4f 45 5f 52 6f 6c  MMEDIATE, OE_Rol
69eb0 6c 62 61 63 6b 20 66 6f 72 20 44 45 46 45 52 52  lback for DEFERR
69ec0 45 44 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  ED */.#define OE
69ed0 5f 53 65 74 4e 75 6c 6c 20 20 37 20 20 20 2f 2a  _SetNull  7   /*
69ee0 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e   Set the foreign
69ef0 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 4e 55   key value to NU
69f00 4c 4c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 45  LL */.#define OE
69f10 5f 53 65 74 44 66 6c 74 20 20 38 20 20 20 2f 2a  _SetDflt  8   /*
69f20 20 53 65 74 20 74 68 65 20 66 6f 72 65 69 67 6e   Set the foreign
69f30 20 6b 65 79 20 76 61 6c 75 65 20 74 6f 20 69 74   key value to it
69f40 73 20 64 65 66 61 75 6c 74 20 2a 2f 0a 23 64 65  s default */.#de
69f50 66 69 6e 65 20 4f 45 5f 43 61 73 63 61 64 65 20  fine OE_Cascade 
69f60 20 39 20 20 20 2f 2a 20 43 61 73 63 61 64 65 20   9   /* Cascade 
69f70 74 68 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 0a  the changes */..
69f80 23 64 65 66 69 6e 65 20 4f 45 5f 44 65 66 61 75  #define OE_Defau
69f90 6c 74 20 20 39 39 20 20 2f 2a 20 44 6f 20 77 68  lt  99  /* Do wh
69fa0 61 74 65 76 65 72 20 74 68 65 20 64 65 66 61 75  atever the defau
69fb0 6c 74 20 61 63 74 69 6f 6e 20 69 73 20 2a 2f 0a  lt action is */.
69fc0 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
69fd0 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  nce of the follo
69fe0 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69  wing structure i
69ff0 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
6a000 66 69 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e  first.** argumen
6a010 74 20 74 6f 20 73 71 6c 69 74 65 33 56 64 62 65  t to sqlite3Vdbe
6a020 4b 65 79 43 6f 6d 70 61 72 65 20 61 6e 64 20 69  KeyCompare and i
6a030 73 20 75 73 65 64 20 74 6f 20 63 6f 6e 74 72 6f  s used to contro
6a040 6c 20 74 68 65 20 0a 2a 2a 20 63 6f 6d 70 61 72  l the .** compar
6a050 69 73 6f 6e 20 6f 66 20 74 68 65 20 74 77 6f 20  ison of the two 
6a060 69 6e 64 65 78 20 6b 65 79 73 2e 0a 2a 2f 0a 73  index keys..*/.s
6a070 74 72 75 63 74 20 4b 65 79 49 6e 66 6f 20 7b 0a  truct KeyInfo {.
6a080 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20    sqlite3 *db;  
6a090 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
6a0a0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
6a0b0 20 2a 2f 0a 20 20 75 38 20 65 6e 63 3b 20 20 20   */.  u8 enc;   
6a0c0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
6a0d0 74 20 65 6e 63 6f 64 69 6e 67 20 2d 20 6f 6e 65  t encoding - one
6a0e0 20 6f 66 20 74 68 65 20 54 45 58 54 5f 55 74 66   of the TEXT_Utf
6a0f0 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 31  * values */.  u1
6a100 36 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  6 nField;       
6a110 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65    /* Number of e
6a120 6e 74 72 69 65 73 20 69 6e 20 61 43 6f 6c 6c 5b  ntries in aColl[
6a130 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 53 6f 72 74  ] */.  u8 *aSort
6a140 4f 72 64 65 72 3b 20 20 20 20 20 2f 2a 20 49 66  Order;     /* If
6a150 20 64 65 66 69 6e 65 64 20 61 6e 20 61 53 6f 72   defined an aSor
6a160 74 4f 72 64 65 72 5b 69 5d 20 69 73 20 74 72 75  tOrder[i] is tru
6a170 65 2c 20 73 6f 72 74 20 44 45 53 43 20 2a 2f 0a  e, sort DESC */.
6a180 20 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c    CollSeq *aColl
6a190 5b 31 5d 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  [1];  /* Collati
6a1a0 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20  ng sequence for 
6a1b0 65 61 63 68 20 74 65 72 6d 20 6f 66 20 74 68 65  each term of the
6a1c0 20 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   key */.};../*.*
6a1d0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6a1e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
6a1f0 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 69  tructure holds i
6a200 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
6a210 20 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 69 6e 64   a.** single ind
6a220 65 78 20 72 65 63 6f 72 64 20 74 68 61 74 20 68  ex record that h
6a230 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
6a240 70 61 72 73 65 64 20 6f 75 74 20 69 6e 74 6f 20  parsed out into 
6a250 69 6e 64 69 76 69 64 75 61 6c 0a 2a 2a 20 76 61  individual.** va
6a260 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 72 65  lues..**.** A re
6a270 63 6f 72 64 20 69 73 20 61 6e 20 6f 62 6a 65 63  cord is an objec
6a280 74 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20  t that contains 
6a290 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 66 69 65 6c  one or more fiel
6a2a0 64 73 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 20 52  ds of data..** R
6a2b0 65 63 6f 72 64 73 20 61 72 65 20 75 73 65 64 20  ecords are used 
6a2c0 74 6f 20 73 74 6f 72 65 20 74 68 65 20 63 6f 6e  to store the con
6a2d0 74 65 6e 74 20 6f 66 20 61 20 74 61 62 6c 65 20  tent of a table 
6a2e0 72 6f 77 20 61 6e 64 20 74 6f 20 73 74 6f 72 65  row and to store
6a2f0 0a 2a 2a 20 74 68 65 20 6b 65 79 20 6f 66 20 61  .** the key of a
6a300 6e 20 69 6e 64 65 78 2e 20 20 41 20 62 6c 6f 62  n index.  A blob
6a310 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20 72   encoding of a r
6a320 65 63 6f 72 64 20 69 73 20 63 72 65 61 74 65 64  ecord is created
6a330 20 62 79 0a 2a 2a 20 74 68 65 20 4f 50 5f 4d 61   by.** the OP_Ma
6a340 6b 65 52 65 63 6f 72 64 20 6f 70 63 6f 64 65 20  keRecord opcode 
6a350 6f 66 20 74 68 65 20 56 44 42 45 20 61 6e 64 20  of the VDBE and 
6a360 69 73 20 64 69 73 61 73 73 65 6d 62 6c 69 65 64  is disassemblied
6a370 20 62 79 20 74 68 65 0a 2a 2a 20 4f 50 5f 43 6f   by the.** OP_Co
6a380 6c 75 6d 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a 0a  lumn opcode..**.
6a390 2a 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72  ** This structur
6a3a0 65 20 68 6f 6c 64 73 20 61 20 72 65 63 6f 72 64  e holds a record
6a3b0 20 74 68 61 74 20 68 61 73 20 61 6c 72 65 61 64   that has alread
6a3c0 79 20 62 65 65 6e 20 64 69 73 61 73 73 65 6d 62  y been disassemb
6a3d0 6c 65 64 0a 2a 2a 20 69 6e 74 6f 20 69 74 73 20  led.** into its 
6a3e0 63 6f 6e 73 74 69 74 75 74 65 6e 74 20 66 69 65  constitutent fie
6a3f0 6c 64 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 55  lds..*/.struct U
6a400 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 7b 0a  npackedRecord {.
6a410 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
6a420 6e 66 6f 3b 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  nfo;  /* Collati
6a430 6f 6e 20 61 6e 64 20 73 6f 72 74 2d 6f 72 64 65  on and sort-orde
6a440 72 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f  r information */
6a450 0a 20 20 75 31 36 20 6e 46 69 65 6c 64 3b 20 20  .  u16 nField;  
6a460 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6a470 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
6a480 70 4d 65 6d 5b 5d 20 2a 2f 0a 20 20 75 31 36 20  pMem[] */.  u16 
6a490 66 6c 61 67 73 3b 20 20 20 20 20 20 20 20 20 20  flags;          
6a4a0 2f 2a 20 42 6f 6f 6c 65 61 6e 20 73 65 74 74 69  /* Boolean setti
6a4b0 6e 67 73 2e 20 20 55 4e 50 41 43 4b 45 44 5f 2e  ngs.  UNPACKED_.
6a4c0 2e 2e 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 4d 65  .. below */.  Me
6a4d0 6d 20 2a 61 4d 65 6d 3b 20 20 20 20 20 20 20 20  m *aMem;        
6a4e0 20 20 2f 2a 20 56 61 6c 75 65 73 20 2a 2f 0a 7d    /* Values */.}
6a4f0 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64  ;../*.** Allowed
6a500 20 76 61 6c 75 65 73 20 6f 66 20 55 6e 70 61 63   values of Unpac
6a510 6b 65 64 52 65 63 6f 72 64 2e 66 6c 61 67 73 0a  kedRecord.flags.
6a520 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50 41 43  */.#define UNPAC
6a530 4b 45 44 5f 4e 45 45 44 5f 46 52 45 45 20 20 20  KED_NEED_FREE   
6a540 20 20 30 78 30 30 30 31 20 20 2f 2a 20 4d 65 6d    0x0001  /* Mem
6a550 6f 72 79 20 69 73 20 66 72 6f 6d 20 73 71 6c 69  ory is from sqli
6a560 74 65 33 4d 61 6c 6c 6f 63 28 29 20 2a 2f 0a 23  te3Malloc() */.#
6a570 64 65 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f  define UNPACKED_
6a580 4e 45 45 44 5f 44 45 53 54 52 4f 59 20 20 30 78  NEED_DESTROY  0x
6a590 30 30 30 32 20 20 2f 2a 20 61 70 4d 65 6d 5b 5d  0002  /* apMem[]
6a5a0 73 20 73 68 6f 75 6c 64 20 61 6c 6c 20 62 65 20  s should all be 
6a5b0 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 23 64 65  destroyed */.#de
6a5c0 66 69 6e 65 20 55 4e 50 41 43 4b 45 44 5f 49 47  fine UNPACKED_IG
6a5d0 4e 4f 52 45 5f 52 4f 57 49 44 20 20 30 78 30 30  NORE_ROWID  0x00
6a5e0 30 34 20 20 2f 2a 20 49 67 6e 6f 72 65 20 74 72  04  /* Ignore tr
6a5f0 61 69 6c 69 6e 67 20 72 6f 77 69 64 20 6f 6e 20  ailing rowid on 
6a600 6b 65 79 31 20 2a 2f 0a 23 64 65 66 69 6e 65 20  key1 */.#define 
6a610 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59  UNPACKED_INCRKEY
6a620 20 20 20 20 20 20 20 30 78 30 30 30 38 20 20 2f         0x0008  /
6a630 2a 20 4d 61 6b 65 20 74 68 69 73 20 6b 65 79 20  * Make this key 
6a640 61 6e 20 65 70 73 69 6c 6f 6e 20 6c 61 72 67 65  an epsilon large
6a650 72 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 4e 50  r */.#define UNP
6a660 41 43 4b 45 44 5f 50 52 45 46 49 58 5f 4d 41 54  ACKED_PREFIX_MAT
6a670 43 48 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  CH  0x0010  /* A
6a680 20 70 72 65 66 69 78 20 6d 61 74 63 68 20 69 73   prefix match is
6a690 20 63 6f 6e 73 69 64 65 72 65 64 20 4f 4b 20 2a   considered OK *
6a6a0 2f 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20 53 51  /../*.** Each SQ
6a6b0 4c 20 69 6e 64 65 78 20 69 73 20 72 65 70 72 65  L index is repre
6a6c0 73 65 6e 74 65 64 20 69 6e 20 6d 65 6d 6f 72 79  sented in memory
6a6d0 20 62 79 20 61 6e 0a 2a 2a 20 69 6e 73 74 61 6e   by an.** instan
6a6e0 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
6a6f0 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
6a700 2a 0a 2a 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73  *.** The columns
6a710 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 68   of the table th
6a720 61 74 20 61 72 65 20 74 6f 20 62 65 20 69 6e 64  at are to be ind
6a730 65 78 65 64 20 61 72 65 20 64 65 73 63 72 69 62  exed are describ
6a740 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 61 69 43  ed.** by the aiC
6a750 6f 6c 75 6d 6e 5b 5d 20 66 69 65 6c 64 20 6f 66  olumn[] field of
6a760 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 2e   this structure.
6a770 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73    For example, s
6a780 75 70 70 6f 73 65 0a 2a 2a 20 77 65 20 68 61 76  uppose.** we hav
6a790 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  e the following 
6a7a0 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65 78 3a  table and index:
6a7b0 0a 2a 2a 0a 2a 2a 20 20 20 20 20 43 52 45 41 54  .**.**     CREAT
6a7c0 45 20 54 41 42 4c 45 20 45 78 31 28 63 31 20 69  E TABLE Ex1(c1 i
6a7d0 6e 74 2c 20 63 32 20 69 6e 74 2c 20 63 33 20 74  nt, c2 int, c3 t
6a7e0 65 78 74 29 3b 0a 2a 2a 20 20 20 20 20 43 52 45  ext);.**     CRE
6a7f0 41 54 45 20 49 4e 44 45 58 20 45 78 32 20 4f 4e  ATE INDEX Ex2 ON
6a800 20 45 78 31 28 63 33 2c 63 31 29 3b 0a 2a 2a 0a   Ex1(c3,c1);.**.
6a810 2a 2a 20 49 6e 20 74 68 65 20 54 61 62 6c 65 20  ** In the Table 
6a820 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72 69  structure descri
6a830 62 69 6e 67 20 45 78 31 2c 20 6e 43 6f 6c 3d 3d  bing Ex1, nCol==
6a840 33 20 62 65 63 61 75 73 65 20 74 68 65 72 65 20  3 because there 
6a850 61 72 65 0a 2a 2a 20 74 68 72 65 65 20 63 6f 6c  are.** three col
6a860 75 6d 6e 73 20 69 6e 20 74 68 65 20 74 61 62 6c  umns in the tabl
6a870 65 2e 20 20 49 6e 20 74 68 65 20 49 6e 64 65 78  e.  In the Index
6a880 20 73 74 72 75 63 74 75 72 65 20 64 65 73 63 72   structure descr
6a890 69 62 69 6e 67 0a 2a 2a 20 45 78 32 2c 20 6e 43  ibing.** Ex2, nC
6a8a0 6f 6c 75 6d 6e 3d 3d 32 20 73 69 6e 63 65 20 32  olumn==2 since 2
6a8b0 20 6f 66 20 74 68 65 20 33 20 63 6f 6c 75 6d 6e   of the 3 column
6a8c0 73 20 6f 66 20 45 78 31 20 61 72 65 20 69 6e 64  s of Ex1 are ind
6a8d0 65 78 65 64 2e 0a 2a 2a 20 54 68 65 20 76 61 6c  exed..** The val
6a8e0 75 65 20 6f 66 20 61 69 43 6f 6c 75 6d 6e 20 69  ue of aiColumn i
6a8f0 73 20 7b 32 2c 20 30 7d 2e 20 20 61 69 43 6f 6c  s {2, 0}.  aiCol
6a900 75 6d 6e 5b 30 5d 3d 3d 32 20 62 65 63 61 75 73  umn[0]==2 becaus
6a910 65 20 74 68 65 20 0a 2a 2a 20 66 69 72 73 74 20  e the .** first 
6a920 63 6f 6c 75 6d 6e 20 74 6f 20 62 65 20 69 6e 64  column to be ind
6a930 65 78 65 64 20 28 63 33 29 20 68 61 73 20 61 6e  exed (c3) has an
6a940 20 69 6e 64 65 78 20 6f 66 20 32 20 69 6e 20 45   index of 2 in E
6a950 78 31 2e 61 43 6f 6c 5b 5d 2e 0a 2a 2a 20 54 68  x1.aCol[]..** Th
6a960 65 20 73 65 63 6f 6e 64 20 63 6f 6c 75 6d 6e 20  e second column 
6a970 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 28 63  to be indexed (c
6a980 31 29 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20  1) has an index 
6a990 6f 66 20 30 20 69 6e 0a 2a 2a 20 45 78 31 2e 61  of 0 in.** Ex1.a
6a9a0 43 6f 6c 5b 5d 2c 20 68 65 6e 63 65 20 45 78 32  Col[], hence Ex2
6a9b0 2e 61 69 43 6f 6c 75 6d 6e 5b 31 5d 3d 3d 30 2e  .aiColumn[1]==0.
6a9c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 49 6e 64 65 78  .**.** The Index
6a9d0 2e 6f 6e 45 72 72 6f 72 20 66 69 65 6c 64 20 64  .onError field d
6a9e0 65 74 65 72 6d 69 6e 65 73 20 77 68 65 74 68 65  etermines whethe
6a9f0 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 69 6e 64  r or not the ind
6aa00 65 78 65 64 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20  exed columns.** 
6aa10 6d 75 73 74 20 62 65 20 75 6e 69 71 75 65 20 61  must be unique a
6aa20 6e 64 20 77 68 61 74 20 74 6f 20 64 6f 20 69 66  nd what to do if
6aa30 20 74 68 65 79 20 61 72 65 20 6e 6f 74 2e 20 20   they are not.  
6aa40 57 68 65 6e 20 49 6e 64 65 78 2e 6f 6e 45 72 72  When Index.onErr
6aa50 6f 72 3d 4f 45 5f 4e 6f 6e 65 2c 0a 2a 2a 20 69  or=OE_None,.** i
6aa60 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20  t means this is 
6aa70 6e 6f 74 20 61 20 75 6e 69 71 75 65 20 69 6e 64  not a unique ind
6aa80 65 78 2e 20 20 4f 74 68 65 72 77 69 73 65 20 69  ex.  Otherwise i
6aa90 74 20 69 73 20 61 20 75 6e 69 71 75 65 20 69 6e  t is a unique in
6aaa0 64 65 78 0a 2a 2a 20 61 6e 64 20 74 68 65 20 76  dex.** and the v
6aab0 61 6c 75 65 20 6f 66 20 49 6e 64 65 78 2e 6f 6e  alue of Index.on
6aac0 45 72 72 6f 72 20 69 6e 64 69 63 61 74 65 20 74  Error indicate t
6aad0 68 65 20 77 68 69 63 68 20 63 6f 6e 66 6c 69 63  he which conflic
6aae0 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 2a 2a  t resolution .**
6aaf0 20 61 6c 67 6f 72 69 74 68 6d 20 74 6f 20 65 6d   algorithm to em
6ab00 70 6c 6f 79 20 77 68 65 6e 65 76 65 72 20 61 6e  ploy whenever an
6ab10 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
6ab20 20 74 6f 20 69 6e 73 65 72 74 20 61 20 6e 6f 6e   to insert a non
6ab30 2d 75 6e 69 71 75 65 0a 2a 2a 20 65 6c 65 6d 65  -unique.** eleme
6ab40 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 49 6e  nt..*/.struct In
6ab50 64 65 78 20 7b 0a 20 20 63 68 61 72 20 2a 7a 4e  dex {.  char *zN
6ab60 61 6d 65 3b 20 20 20 20 20 2f 2a 20 4e 61 6d 65  ame;     /* Name
6ab70 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 2a   of this index *
6ab80 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b  /.  int nColumn;
6ab90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6aba0 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
6abb0 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20 74   table used by t
6abc0 68 69 73 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  his index */.  i
6abd0 6e 74 20 2a 61 69 43 6f 6c 75 6d 6e 3b 20 20 20  nt *aiColumn;   
6abe0 2f 2a 20 57 68 69 63 68 20 63 6f 6c 75 6d 6e 73  /* Which columns
6abf0 20 61 72 65 20 75 73 65 64 20 62 79 20 74 68 69   are used by thi
6ac00 73 20 69 6e 64 65 78 2e 20 20 31 73 74 20 69 73  s index.  1st is
6ac10 20 30 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64   0 */.  unsigned
6ac20 20 2a 61 69 52 6f 77 45 73 74 3b 20 2f 2a 20 52   *aiRowEst; /* R
6ac30 65 73 75 6c 74 20 6f 66 20 41 4e 41 4c 59 5a 45  esult of ANALYZE
6ac40 3a 20 45 73 74 2e 20 72 6f 77 73 20 73 65 6c 65  : Est. rows sele
6ac50 63 74 65 64 20 62 79 20 65 61 63 68 20 63 6f 6c  cted by each col
6ac60 75 6d 6e 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a  umn */.  Table *
6ac70 70 54 61 62 6c 65 3b 20 20 20 2f 2a 20 54 68 65  pTable;   /* The
6ac80 20 53 51 4c 20 74 61 62 6c 65 20 62 65 69 6e 67   SQL table being
6ac90 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e   indexed */.  in
6aca0 74 20 74 6e 75 6d 3b 20 20 20 20 20 20 20 20 2f  t tnum;        /
6acb0 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e 69 6e  * Page containin
6acc0 67 20 72 6f 6f 74 20 6f 66 20 74 68 69 73 20 69  g root of this i
6acd0 6e 64 65 78 20 69 6e 20 64 61 74 61 62 61 73 65  ndex in database
6ace0 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38 20 6f 6e   file */.  u8 on
6acf0 45 72 72 6f 72 3b 20 20 20 20 20 20 2f 2a 20 4f  Error;      /* O
6ad00 45 5f 41 62 6f 72 74 2c 20 4f 45 5f 49 67 6e 6f  E_Abort, OE_Igno
6ad10 72 65 2c 20 4f 45 5f 52 65 70 6c 61 63 65 2c 20  re, OE_Replace, 
6ad20 6f 72 20 4f 45 5f 4e 6f 6e 65 20 2a 2f 0a 20 20  or OE_None */.  
6ad30 75 38 20 61 75 74 6f 49 6e 64 65 78 3b 20 20 20  u8 autoIndex;   
6ad40 20 2f 2a 20 54 72 75 65 20 69 66 20 69 73 20 61   /* True if is a
6ad50 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 63 72 65  utomatically cre
6ad60 61 74 65 64 20 28 65 78 3a 20 62 79 20 55 4e 49  ated (ex: by UNI
6ad70 51 55 45 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a  QUE) */.  char *
6ad80 7a 43 6f 6c 41 66 66 3b 20 20 20 2f 2a 20 53 74  zColAff;   /* St
6ad90 72 69 6e 67 20 64 65 66 69 6e 69 6e 67 20 74 68  ring defining th
6ada0 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 65 61  e affinity of ea
6adb0 63 68 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 49  ch column */.  I
6adc0 6e 64 65 78 20 2a 70 4e 65 78 74 3b 20 20 20 20  ndex *pNext;    
6add0 2f 2a 20 54 68 65 20 6e 65 78 74 20 69 6e 64 65  /* The next inde
6ade0 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  x associated wit
6adf0 68 20 74 68 65 20 73 61 6d 65 20 74 61 62 6c 65  h the same table
6ae00 20 2a 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53   */.  Schema *pS
6ae10 63 68 65 6d 61 3b 20 2f 2a 20 53 63 68 65 6d 61  chema; /* Schema
6ae20 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 69 73   containing this
6ae30 20 69 6e 64 65 78 20 2a 2f 0a 20 20 75 38 20 2a   index */.  u8 *
6ae40 61 53 6f 72 74 4f 72 64 65 72 3b 20 20 2f 2a 20  aSortOrder;  /* 
6ae50 41 72 72 61 79 20 6f 66 20 73 69 7a 65 20 49 6e  Array of size In
6ae60 64 65 78 2e 6e 43 6f 6c 75 6d 6e 2e 20 54 72 75  dex.nColumn. Tru
6ae70 65 3d 3d 44 45 53 43 2c 20 46 61 6c 73 65 3d 3d  e==DESC, False==
6ae80 41 53 43 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ASC */.  char **
6ae90 61 7a 43 6f 6c 6c 3b 20 20 20 2f 2a 20 41 72 72  azColl;   /* Arr
6aea0 61 79 20 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20  ay of collation 
6aeb0 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 73 20 66  sequence names f
6aec0 6f 72 20 69 6e 64 65 78 20 2a 2f 0a 7d 3b 0a 0a  or index */.};..
6aed0 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 6f 6b 65 6e  /*.** Each token
6aee0 20 63 6f 6d 69 6e 67 20 6f 75 74 20 6f 66 20 74   coming out of t
6aef0 68 65 20 6c 65 78 65 72 20 69 73 20 61 6e 20 69  he lexer is an i
6af00 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a 20 74 68  nstance of.** th
6af10 69 73 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  is structure.  T
6af20 6f 6b 65 6e 73 20 61 72 65 20 61 6c 73 6f 20 75  okens are also u
6af30 73 65 64 20 61 73 20 70 61 72 74 20 6f 66 20 61  sed as part of a
6af40 6e 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a  n expression..**
6af50 0a 2a 2a 20 4e 6f 74 65 20 69 66 20 54 6f 6b 65  .** Note if Toke
6af60 6e 2e 7a 3d 3d 30 20 74 68 65 6e 20 54 6f 6b 65  n.z==0 then Toke
6af70 6e 2e 64 79 6e 20 61 6e 64 20 54 6f 6b 65 6e 2e  n.dyn and Token.
6af80 6e 20 61 72 65 20 75 6e 64 65 66 69 6e 65 64 20  n are undefined 
6af90 61 6e 64 0a 2a 2a 20 6d 61 79 20 63 6f 6e 74 61  and.** may conta
6afa0 69 6e 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 73  in random values
6afb0 2e 20 20 44 6f 20 6e 6f 74 20 6d 61 6b 65 20 61  .  Do not make a
6afc0 6e 79 20 61 73 73 75 70 74 69 6f 6e 73 20 61 62  ny assuptions ab
6afd0 6f 75 74 20 54 6f 6b 65 6e 2e 64 79 6e 0a 2a 2a  out Token.dyn.**
6afe0 20 61 6e 64 20 54 6f 6b 65 6e 2e 6e 20 77 68 65   and Token.n whe
6aff0 6e 20 54 6f 6b 65 6e 2e 7a 3d 3d 30 2e 0a 2a 2f  n Token.z==0..*/
6b000 0a 73 74 72 75 63 74 20 54 6f 6b 65 6e 20 7b 0a  .struct Token {.
6b010 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64    const unsigned
6b020 20 63 68 61 72 20 2a 7a 3b 20 2f 2a 20 54 65 78   char *z; /* Tex
6b030 74 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 2e 20  t of the token. 
6b040 20 4e 6f 74 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e   Not NULL-termin
6b050 61 74 65 64 21 20 2a 2f 0a 20 20 75 6e 73 69 67  ated! */.  unsig
6b060 6e 65 64 20 64 79 6e 20 20 3a 20 31 3b 20 20 20  ned dyn  : 1;   
6b070 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 6d     /* True for m
6b080 61 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2c 20  alloced memory, 
6b090 66 61 6c 73 65 20 66 6f 72 20 73 74 61 74 69 63  false for static
6b0a0 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6e   */.  unsigned n
6b0b0 20 20 20 20 3a 20 33 31 3b 20 20 20 20 20 2f 2a      : 31;     /*
6b0c0 20 4e 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61   Number of chara
6b0d0 63 74 65 72 73 20 69 6e 20 74 68 69 73 20 74 6f  cters in this to
6b0e0 6b 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ken */.};../*.**
6b0f0 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   An instance of 
6b100 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 63  this structure c
6b110 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61 74  ontains informat
6b120 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
6b130 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
6b140 6f 72 20 61 20 53 45 4c 45 43 54 20 74 68 61 74  or a SELECT that
6b150 20 63 6f 6e 74 61 69 6e 73 20 61 67 67 72 65 67   contains aggreg
6b160 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ate functions..*
6b170 2a 0a 2a 2a 20 49 66 20 45 78 70 72 2e 6f 70 3d  *.** If Expr.op=
6b180 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 6f  =TK_AGG_COLUMN o
6b190 72 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  r TK_AGG_FUNCTIO
6b1a0 4e 20 74 68 65 6e 20 45 78 70 72 2e 70 41 67 67  N then Expr.pAgg
6b1b0 49 6e 66 6f 20 69 73 20 61 0a 2a 2a 20 70 6f 69  Info is a.** poi
6b1c0 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 74 72  nter to this str
6b1d0 75 63 74 75 72 65 2e 20 20 54 68 65 20 45 78 70  ucture.  The Exp
6b1e0 72 2e 69 43 6f 6c 75 6d 6e 20 66 69 65 6c 64 20  r.iColumn field 
6b1f0 69 73 20 74 68 65 20 69 6e 64 65 78 20 69 6e 0a  is the index in.
6b200 2a 2a 20 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b  ** AggInfo.aCol[
6b210 5d 20 6f 72 20 41 67 67 49 6e 66 6f 2e 61 46 75  ] or AggInfo.aFu
6b220 6e 63 5b 5d 20 6f 66 20 69 6e 66 6f 72 6d 61 74  nc[] of informat
6b230 69 6f 6e 20 6e 65 65 64 65 64 20 74 6f 20 67 65  ion needed to ge
6b240 6e 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66  nerate.** code f
6b250 6f 72 20 74 68 61 74 20 6e 6f 64 65 2e 0a 2a 2a  or that node..**
6b260 0a 2a 2a 20 41 67 67 49 6e 66 6f 2e 70 47 72 6f  .** AggInfo.pGro
6b270 75 70 42 79 20 61 6e 64 20 41 67 67 49 6e 66 6f  upBy and AggInfo
6b280 2e 61 46 75 6e 63 2e 70 45 78 70 72 20 70 6f 69  .aFunc.pExpr poi
6b290 6e 74 20 74 6f 20 66 69 65 6c 64 73 20 77 69 74  nt to fields wit
6b2a0 68 69 6e 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69  hin the.** origi
6b2b0 6e 61 6c 20 53 65 6c 65 63 74 20 73 74 72 75 63  nal Select struc
6b2c0 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
6b2d0 62 65 73 20 74 68 65 20 53 45 4c 45 43 54 20 73  bes the SELECT s
6b2e0 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 73 65  tatement.  These
6b2f0 0a 2a 2a 20 66 69 65 6c 64 73 20 64 6f 20 6e 6f  .** fields do no
6b300 74 20 6e 65 65 64 20 74 6f 20 62 65 20 66 72 65  t need to be fre
6b310 65 64 20 77 68 65 6e 20 64 65 61 6c 6c 6f 63 61  ed when dealloca
6b320 74 69 6e 67 20 74 68 65 20 41 67 67 49 6e 66 6f  ting the AggInfo
6b330 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
6b340 74 72 75 63 74 20 41 67 67 49 6e 66 6f 20 7b 0a  truct AggInfo {.
6b350 20 20 75 38 20 64 69 72 65 63 74 4d 6f 64 65 3b    u8 directMode;
6b360 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 72            /* Dir
6b370 65 63 74 20 72 65 6e 64 65 72 69 6e 67 20 6d 6f  ect rendering mo
6b380 64 65 20 6d 65 61 6e 73 20 74 61 6b 65 20 64 61  de means take da
6b390 74 61 20 64 69 72 65 63 74 6c 79 0a 20 20 20 20  ta directly.    
6b3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b3b0 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20 73 6f        ** from so
6b3c0 75 72 63 65 20 74 61 62 6c 65 73 20 72 61 74 68  urce tables rath
6b3d0 65 72 20 74 68 61 6e 20 66 72 6f 6d 20 61 63 63  er than from acc
6b3e0 75 6d 75 6c 61 74 6f 72 73 20 2a 2f 0a 20 20 75  umulators */.  u
6b3f0 38 20 75 73 65 53 6f 72 74 69 6e 67 49 64 78 3b  8 useSortingIdx;
6b400 20 20 20 20 20 20 20 2f 2a 20 49 6e 20 64 69 72         /* In dir
6b410 65 63 74 20 6d 6f 64 65 2c 20 72 65 66 65 72 65  ect mode, refere
6b420 6e 63 65 20 74 68 65 20 73 6f 72 74 69 6e 67 20  nce the sorting 
6b430 69 6e 64 65 78 20 72 61 74 68 65 72 0a 20 20 20  index rather.   
6b440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6b450 20 20 20 20 20 20 20 2a 2a 20 74 68 61 6e 20 74         ** than t
6b460 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20  he source table 
6b470 2a 2f 0a 20 20 69 6e 74 20 73 6f 72 74 69 6e 67  */.  int sorting
6b480 49 64 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  Idx;         /* 
6b490 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 6f 66  Cursor number of
6b4a0 20 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64   the sorting ind
6b4b0 65 78 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  ex */.  ExprList
6b4c0 20 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20   *pGroupBy;     
6b4d0 2f 2a 20 54 68 65 20 67 72 6f 75 70 20 62 79 20  /* The group by 
6b4e0 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20  clause */.  int 
6b4f0 6e 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 3b 20  nSortingColumn; 
6b500 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6b510 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
6b520 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f  sorting index */
6b530 0a 20 20 73 74 72 75 63 74 20 41 67 67 49 6e 66  .  struct AggInf
6b540 6f 5f 63 6f 6c 20 7b 20 20 20 20 2f 2a 20 46 6f  o_col {    /* Fo
6b550 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e 20 75 73  r each column us
6b560 65 64 20 69 6e 20 73 6f 75 72 63 65 20 74 61 62  ed in source tab
6b570 6c 65 73 20 2a 2f 0a 20 20 20 20 54 61 62 6c 65  les */.    Table
6b580 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20   *pTab;         
6b590 20 20 20 20 2f 2a 20 53 6f 75 72 63 65 20 74 61      /* Source ta
6b5a0 62 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69  ble */.    int i
6b5b0 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  Table;          
6b5c0 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
6b5d0 6d 62 65 72 20 6f 66 20 74 68 65 20 73 6f 75 72  mber of the sour
6b5e0 63 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20  ce table */.    
6b5f0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
6b600 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75           /* Colu
6b610 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68 69 6e  mn number within
6b620 20 74 68 65 20 73 6f 75 72 63 65 20 74 61 62 6c   the source tabl
6b630 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 53 6f  e */.    int iSo
6b640 72 74 65 72 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  rterColumn;     
6b650 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62    /* Column numb
6b660 65 72 20 69 6e 20 74 68 65 20 73 6f 72 74 69 6e  er in the sortin
6b670 67 20 69 6e 64 65 78 20 2a 2f 0a 20 20 20 20 69  g index */.    i
6b680 6e 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20  nt iMem;        
6b690 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72          /* Memor
6b6a0 79 20 6c 6f 63 61 74 69 6f 6e 20 74 68 61 74 20  y location that 
6b6b0 61 63 74 73 20 61 73 20 61 63 63 75 6d 75 6c 61  acts as accumula
6b6c0 74 6f 72 20 2a 2f 0a 20 20 20 20 45 78 70 72 20  tor */.    Expr 
6b6d0 2a 70 45 78 70 72 3b 20 20 20 20 20 20 20 20 20  *pExpr;         
6b6e0 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 69 67 69      /* The origi
6b6f0 6e 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 20 2a  nal expression *
6b700 2f 0a 20 20 7d 20 2a 61 43 6f 6c 3b 0a 20 20 69  /.  } *aCol;.  i
6b710 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nt nColumn;     
6b720 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6b730 20 6f 66 20 75 73 65 64 20 65 6e 74 72 69 65 73   of used entries
6b740 20 69 6e 20 61 43 6f 6c 5b 5d 20 2a 2f 0a 20 20   in aCol[] */.  
6b750 69 6e 74 20 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63  int nColumnAlloc
6b760 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
6b770 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f 63  r of slots alloc
6b780 61 74 65 64 20 66 6f 72 20 61 43 6f 6c 5b 5d 20  ated for aCol[] 
6b790 2a 2f 0a 20 20 69 6e 74 20 6e 41 63 63 75 6d 75  */.  int nAccumu
6b7a0 6c 61 74 6f 72 3b 20 20 20 20 20 20 20 2f 2a 20  lator;       /* 
6b7b0 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  Number of column
6b7c0 73 20 74 68 61 74 20 73 68 6f 77 20 74 68 72 6f  s that show thro
6b7d0 75 67 68 20 74 6f 20 74 68 65 20 6f 75 74 70 75  ugh to the outpu
6b7e0 74 2e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t..             
6b7f0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
6b800 41 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  Additional colum
6b810 6e 73 20 61 72 65 20 75 73 65 64 20 6f 6e 6c 79  ns are used only
6b820 20 61 73 20 70 61 72 61 6d 65 74 65 72 73 20 74   as parameters t
6b830 6f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o.              
6b840 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 61              ** a
6b850 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
6b860 6e 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41  ns */.  struct A
6b870 67 67 49 6e 66 6f 5f 66 75 6e 63 20 7b 20 20 20  ggInfo_func {   
6b880 2f 2a 20 46 6f 72 20 65 61 63 68 20 61 67 67 72  /* For each aggr
6b890 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 2a  egate function *
6b8a0 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 45 78 70  /.    Expr *pExp
6b8b0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  r;             /
6b8c0 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 65 6e 63  * Expression enc
6b8d0 6f 64 69 6e 67 20 74 68 65 20 66 75 6e 63 74 69  oding the functi
6b8e0 6f 6e 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65  on */.    FuncDe
6b8f0 66 20 2a 70 46 75 6e 63 3b 20 20 20 20 20 20 20  f *pFunc;       
6b900 20 20 20 2f 2a 20 54 68 65 20 61 67 67 72 65 67     /* The aggreg
6b910 61 74 65 20 66 75 6e 63 74 69 6f 6e 20 69 6d 70  ate function imp
6b920 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20  lementation */. 
6b930 20 20 20 69 6e 74 20 69 4d 65 6d 3b 20 20 20 20     int iMem;    
6b940 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d              /* M
6b950 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 74  emory location t
6b960 68 61 74 20 61 63 74 73 20 61 73 20 61 63 63 75  hat acts as accu
6b970 6d 75 6c 61 74 6f 72 20 2a 2f 0a 20 20 20 20 69  mulator */.    i
6b980 6e 74 20 69 44 69 73 74 69 6e 63 74 3b 20 20 20  nt iDistinct;   
6b990 20 20 20 20 20 20 20 20 2f 2a 20 45 70 68 65 72          /* Epher
6b9a0 6d 65 72 61 6c 20 74 61 62 6c 65 20 75 73 65 64  meral table used
6b9b0 20 74 6f 20 65 6e 66 6f 72 63 65 20 44 49 53 54   to enforce DIST
6b9c0 49 4e 43 54 20 2a 2f 0a 20 20 7d 20 2a 61 46 75  INCT */.  } *aFu
6b9d0 6e 63 3b 0a 20 20 69 6e 74 20 6e 46 75 6e 63 3b  nc;.  int nFunc;
6b9e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6b9f0 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69   Number of entri
6ba00 65 73 20 69 6e 20 61 46 75 6e 63 5b 5d 20 2a 2f  es in aFunc[] */
6ba10 0a 20 20 69 6e 74 20 6e 46 75 6e 63 41 6c 6c 6f  .  int nFuncAllo
6ba20 63 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  c;         /* Nu
6ba30 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c  mber of slots al
6ba40 6c 6f 63 61 74 65 64 20 66 6f 72 20 61 46 75 6e  located for aFun
6ba50 63 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  c[] */.};../*.**
6ba60 20 45 61 63 68 20 6e 6f 64 65 20 6f 66 20 61 6e   Each node of an
6ba70 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74   expression in t
6ba80 68 65 20 70 61 72 73 65 20 74 72 65 65 20 69 73  he parse tree is
6ba90 20 61 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20   an instance.** 
6baa0 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
6bab0 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 6f 70  e..**.** Expr.op
6bac0 20 69 73 20 74 68 65 20 6f 70 63 6f 64 65 2e 20   is the opcode. 
6bad0 20 54 68 65 20 69 6e 74 65 67 65 72 20 70 61 72   The integer par
6bae0 73 65 72 20 74 6f 6b 65 6e 20 63 6f 64 65 73 20  ser token codes 
6baf0 61 72 65 20 72 65 75 73 65 64 0a 2a 2a 20 61 73  are reused.** as
6bb00 20 6f 70 63 6f 64 65 73 20 68 65 72 65 2e 20 20   opcodes here.  
6bb10 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  For example, the
6bb20 20 70 61 72 73 65 72 20 64 65 66 69 6e 65 73 20   parser defines 
6bb30 54 4b 5f 47 45 20 74 6f 20 62 65 20 61 6e 20 69  TK_GE to be an i
6bb40 6e 74 65 67 65 72 0a 2a 2a 20 63 6f 64 65 20 72  nteger.** code r
6bb50 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68 65 20  epresenting the 
6bb60 22 3e 3d 22 20 6f 70 65 72 61 74 6f 72 2e 20 20  ">=" operator.  
6bb70 54 68 69 73 20 73 61 6d 65 20 69 6e 74 65 67 65  This same intege
6bb80 72 20 63 6f 64 65 20 69 73 20 72 65 75 73 65 64  r code is reused
6bb90 0a 2a 2a 20 74 6f 20 72 65 70 72 65 73 65 6e 74  .** to represent
6bba0 20 74 68 65 20 67 72 65 61 74 65 72 2d 74 68 61   the greater-tha
6bbb0 6e 2d 6f 72 2d 65 71 75 61 6c 2d 74 6f 20 6f 70  n-or-equal-to op
6bbc0 65 72 61 74 6f 72 20 69 6e 20 74 68 65 20 65 78  erator in the ex
6bbd0 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74 72 65 65  pression.** tree
6bbe0 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 72 2e 70 52 69  ..**.** Expr.pRi
6bbf0 67 68 74 20 61 6e 64 20 45 78 70 72 2e 70 4c 65  ght and Expr.pLe
6bc00 66 74 20 61 72 65 20 73 75 62 65 78 70 72 65 73  ft are subexpres
6bc10 73 69 6f 6e 73 2e 20 20 45 78 70 72 2e 70 4c 69  sions.  Expr.pLi
6bc20 73 74 20 69 73 20 61 20 6c 69 73 74 0a 2a 2a 20  st is a list.** 
6bc30 6f 66 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  of argument if t
6bc40 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73  he expression is
6bc50 20 61 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a   a function..**.
6bc60 2a 2a 20 45 78 70 72 2e 74 6f 6b 65 6e 20 69 73  ** Expr.token is
6bc70 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f   the operator to
6bc80 6b 65 6e 20 66 6f 72 20 74 68 69 73 20 6e 6f 64  ken for this nod
6bc90 65 2e 20 20 46 6f 72 20 73 6f 6d 65 20 65 78 70  e.  For some exp
6bca0 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  ressions.** that
6bcb0 20 68 61 76 65 20 73 75 62 65 78 70 72 65 73 73   have subexpress
6bcc0 69 6f 6e 73 2c 20 45 78 70 72 2e 74 6f 6b 65 6e  ions, Expr.token
6bcd0 20 63 61 6e 20 62 65 20 74 68 65 20 63 6f 6d 70   can be the comp
6bce0 6c 65 74 65 20 74 65 78 74 20 74 68 61 74 20 67  lete text that g
6bcf0 61 76 65 0a 2a 2a 20 72 69 73 65 20 74 6f 20 74  ave.** rise to t
6bd00 68 65 20 45 78 70 72 2e 20 20 49 6e 20 74 68 65  he Expr.  In the
6bd10 20 6c 61 74 74 65 72 20 63 61 73 65 2c 20 74 68   latter case, th
6bd20 65 20 74 6f 6b 65 6e 20 69 73 20 6d 61 72 6b 65  e token is marke
6bd30 64 20 61 73 20 62 65 69 6e 67 0a 2a 2a 20 61 20  d as being.** a 
6bd40 63 6f 6d 70 6f 75 6e 64 20 74 6f 6b 65 6e 2e 0a  compound token..
6bd50 2a 2a 0a 2a 2a 20 41 6e 20 65 78 70 72 65 73 73  **.** An express
6bd60 69 6f 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ion of the form 
6bd70 49 44 20 6f 72 20 49 44 2e 49 44 20 72 65 66 65  ID or ID.ID refe
6bd80 72 73 20 74 6f 20 61 20 63 6f 6c 75 6d 6e 20 69  rs to a column i
6bd90 6e 20 61 20 74 61 62 6c 65 2e 0a 2a 2a 20 46 6f  n a table..** Fo
6bda0 72 20 73 75 63 68 20 65 78 70 72 65 73 73 69 6f  r such expressio
6bdb0 6e 73 2c 20 45 78 70 72 2e 6f 70 20 69 73 20 73  ns, Expr.op is s
6bdc0 65 74 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20  et to TK_COLUMN 
6bdd0 61 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20  and Expr.iTable 
6bde0 69 73 0a 2a 2a 20 74 68 65 20 69 6e 74 65 67 65  is.** the intege
6bdf0 72 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  r cursor number 
6be00 6f 66 20 61 20 56 44 42 45 20 63 75 72 73 6f 72  of a VDBE cursor
6be10 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 61   pointing to tha
6be20 74 20 74 61 62 6c 65 20 61 6e 64 0a 2a 2a 20 45  t table and.** E
6be30 78 70 72 2e 69 43 6f 6c 75 6d 6e 20 69 73 20 74  xpr.iColumn is t
6be40 68 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72  he column number
6be50 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
6be60 63 20 63 6f 6c 75 6d 6e 2e 20 20 49 66 20 74 68  c column.  If th
6be70 65 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20  e.** expression 
6be80 69 73 20 75 73 65 64 20 61 73 20 61 20 72 65 73  is used as a res
6be90 75 6c 74 20 69 6e 20 61 6e 20 61 67 67 72 65 67  ult in an aggreg
6bea0 61 74 65 20 53 45 4c 45 43 54 2c 20 74 68 65 6e  ate SELECT, then
6beb0 20 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 69 73   the.** value is
6bec0 20 61 6c 73 6f 20 73 74 6f 72 65 64 20 69 6e 20   also stored in 
6bed0 74 68 65 20 45 78 70 72 2e 69 41 67 67 20 63 6f  the Expr.iAgg co
6bee0 6c 75 6d 6e 20 69 6e 20 74 68 65 20 61 67 67 72  lumn in the aggr
6bef0 65 67 61 74 65 20 73 6f 20 74 68 61 74 0a 2a 2a  egate so that.**
6bf00 20 69 74 20 63 61 6e 20 62 65 20 61 63 63 65 73   it can be acces
6bf10 73 65 64 20 61 66 74 65 72 20 61 6c 6c 20 61 67  sed after all ag
6bf20 67 72 65 67 61 74 65 73 20 61 72 65 20 63 6f 6d  gregates are com
6bf30 70 75 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  puted..**.** If 
6bf40 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  the expression i
6bf50 73 20 61 20 66 75 6e 63 74 69 6f 6e 2c 20 74 68  s a function, th
6bf60 65 20 45 78 70 72 2e 69 54 61 62 6c 65 20 69 73  e Expr.iTable is
6bf70 20 61 6e 20 69 6e 74 65 67 65 72 20 63 6f 64 65   an integer code
6bf80 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 69 6e 67  .** representing
6bf90 20 77 68 69 63 68 20 66 75 6e 63 74 69 6f 6e 2e   which function.
6bfa0 20 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73    If the express
6bfb0 69 6f 6e 20 69 73 20 61 6e 20 75 6e 62 6f 75 6e  ion is an unboun
6bfc0 64 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61  d variable.** ma
6bfd0 72 6b 65 72 20 28 61 20 71 75 65 73 74 69 6f 6e  rker (a question
6bfe0 20 6d 61 72 6b 20 63 68 61 72 61 63 74 65 72 20   mark character 
6bff0 27 3f 27 20 69 6e 20 74 68 65 20 6f 72 69 67 69  '?' in the origi
6c000 6e 61 6c 20 53 51 4c 29 20 74 68 65 6e 20 74 68  nal SQL) then th
6c010 65 0a 2a 2a 20 45 78 70 72 2e 69 54 61 62 6c 65  e.** Expr.iTable
6c020 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 64 65 78   holds the index
6c030 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
6c040 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   variable..**.**
6c050 20 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69   If the expressi
6c060 6f 6e 20 69 73 20 61 20 73 75 62 71 75 65 72 79  on is a subquery
6c070 20 74 68 65 6e 20 45 78 70 72 2e 69 43 6f 6c 75   then Expr.iColu
6c080 6d 6e 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74 65  mn holds an inte
6c090 67 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72 20  ger.** register 
6c0a0 6e 75 6d 62 65 72 20 63 6f 6e 74 61 69 6e 69 6e  number containin
6c0b0 67 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  g the result of 
6c0c0 74 68 65 20 73 75 62 71 75 65 72 79 2e 20 20 49  the subquery.  I
6c0d0 66 20 74 68 65 0a 2a 2a 20 73 75 62 71 75 65 72  f the.** subquer
6c0e0 79 20 67 69 76 65 73 20 61 20 63 6f 6e 73 74 61  y gives a consta
6c0f0 6e 74 20 72 65 73 75 6c 74 2c 20 74 68 65 6e 20  nt result, then 
6c100 69 54 61 62 6c 65 20 69 73 20 2d 31 2e 20 20 49  iTable is -1.  I
6c110 66 20 74 68 65 20 73 75 62 71 75 65 72 79 0a 2a  f the subquery.*
6c120 2a 20 67 69 76 65 73 20 61 20 64 69 66 66 65 72  * gives a differ
6c130 65 6e 74 20 61 6e 73 77 65 72 20 61 74 20 64 69  ent answer at di
6c140 66 66 65 72 65 6e 74 20 74 69 6d 65 73 20 64 75  fferent times du
6c150 72 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 70  ring statement p
6c160 72 6f 63 65 73 73 69 6e 67 0a 2a 2a 20 74 68 65  rocessing.** the
6c170 6e 20 69 54 61 62 6c 65 20 69 73 20 74 68 65 20  n iTable is the 
6c180 61 64 64 72 65 73 73 20 6f 66 20 61 20 73 75 62  address of a sub
6c190 72 6f 75 74 69 6e 65 20 74 68 61 74 20 63 6f 6d  routine that com
6c1a0 70 75 74 65 73 20 74 68 65 20 73 75 62 71 75 65  putes the subque
6c1b0 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 45 78  ry..**.** The Ex
6c1c0 70 72 2e 70 53 65 6c 65 63 74 20 66 69 65 6c 64  pr.pSelect field
6c1d0 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 53 45 4c   points to a SEL
6c1e0 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
6c1f0 54 68 65 20 53 45 4c 45 43 54 20 6d 69 67 68 74  The SELECT might
6c200 0a 2a 2a 20 62 65 20 74 68 65 20 72 69 67 68 74  .** be the right
6c210 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 49   operand of an I
6c220 4e 20 6f 70 65 72 61 74 6f 72 2e 20 20 4f 72 2c  N operator.  Or,
6c230 20 69 66 20 61 20 73 63 61 6c 61 72 20 53 45 4c   if a scalar SEL
6c240 45 43 54 20 61 70 70 65 61 72 73 0a 2a 2a 20 69  ECT appears.** i
6c250 6e 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  n an expression 
6c260 74 68 65 20 6f 70 63 6f 64 65 20 69 73 20 54 4b  the opcode is TK
6c270 5f 53 45 4c 45 43 54 20 61 6e 64 20 45 78 70 72  _SELECT and Expr
6c280 2e 70 53 65 6c 65 63 74 20 69 73 20 74 68 65 20  .pSelect is the 
6c290 6f 6e 6c 79 0a 2a 2a 20 6f 70 65 72 61 6e 64 2e  only.** operand.
6c2a0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 45 78  .**.** If the Ex
6c2b0 70 72 20 69 73 20 6f 66 20 74 79 70 65 20 4f 50  pr is of type OP
6c2c0 5f 43 6f 6c 75 6d 6e 2c 20 61 6e 64 20 74 68 65  _Column, and the
6c2d0 20 74 61 62 6c 65 20 69 74 20 69 73 20 73 65 6c   table it is sel
6c2e0 65 63 74 69 6e 67 20 66 72 6f 6d 0a 2a 2a 20 69  ecting from.** i
6c2f0 73 20 61 20 64 69 73 6b 20 74 61 62 6c 65 20 6f  s a disk table o
6c300 72 20 74 68 65 20 22 6f 6c 64 2e 2a 22 20 70 73  r the "old.*" ps
6c310 65 75 64 6f 2d 74 61 62 6c 65 2c 20 74 68 65 6e  eudo-table, then
6c320 20 70 54 61 62 20 70 6f 69 6e 74 73 20 74 6f 20   pTab points to 
6c330 74 68 65 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e  the.** correspon
6c340 64 69 6e 67 20 74 61 62 6c 65 20 64 65 66 69 6e  ding table defin
6c350 69 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ition..*/.struct
6c360 20 45 78 70 72 20 7b 0a 20 20 75 38 20 6f 70 3b   Expr {.  u8 op;
6c370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c380 20 2f 2a 20 4f 70 65 72 61 74 69 6f 6e 20 70 65   /* Operation pe
6c390 72 66 6f 72 6d 65 64 20 62 79 20 74 68 69 73 20  rformed by this 
6c3a0 6e 6f 64 65 20 2a 2f 0a 20 20 63 68 61 72 20 61  node */.  char a
6c3b0 66 66 69 6e 69 74 79 3b 20 20 20 20 20 20 20 20  ffinity;        
6c3c0 20 2f 2a 20 54 68 65 20 61 66 66 69 6e 69 74 79   /* The affinity
6c3d0 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f   of the column o
6c3e0 72 20 30 20 69 66 20 6e 6f 74 20 61 20 63 6f 6c  r 0 if not a col
6c3f0 75 6d 6e 20 2a 2f 0a 20 20 75 31 36 20 66 6c 61  umn */.  u16 fla
6c400 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
6c410 2f 2a 20 56 61 72 69 6f 75 73 20 66 6c 61 67 73  /* Various flags
6c420 2e 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a  .  See below */.
6c430 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
6c440 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ;        /* The 
6c450 63 6f 6c 6c 61 74 69 6f 6e 20 74 79 70 65 20 6f  collation type o
6c460 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6f 72 20  f the column or 
6c470 30 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65  0 */.  Expr *pLe
6c480 66 74 2c 20 2a 70 52 69 67 68 74 3b 20 20 2f 2a  ft, *pRight;  /*
6c490 20 4c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20   Left and right 
6c4a0 73 75 62 6e 6f 64 65 73 20 2a 2f 0a 20 20 45 78  subnodes */.  Ex
6c4b0 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 20 20  prList *pList;  
6c4c0 20 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f       /* A list o
6c4d0 66 20 65 78 70 72 65 73 73 69 6f 6e 73 20 75 73  f expressions us
6c4e0 65 64 20 61 73 20 66 75 6e 63 74 69 6f 6e 20 61  ed as function a
6c4f0 72 67 75 6d 65 6e 74 73 0a 20 20 20 20 20 20 20  rguments.       
6c500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c510 20 20 2a 2a 20 6f 72 20 69 6e 20 22 3c 65 78 70    ** or in "<exp
6c520 72 3e 20 49 4e 20 28 3c 65 78 70 72 2d 6c 69 73  r> IN (<expr-lis
6c530 74 29 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 74  t)" */.  Token t
6c540 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  oken;           
6c550 2f 2a 20 41 6e 20 6f 70 65 72 61 6e 64 20 74 6f  /* An operand to
6c560 6b 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 73  ken */.  Token s
6c570 70 61 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  pan;            
6c580 2f 2a 20 43 6f 6d 70 6c 65 74 65 20 74 65 78 74  /* Complete text
6c590 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73 69   of the expressi
6c5a0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62  on */.  int iTab
6c5b0 6c 65 2c 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 2f  le, iColumn;   /
6c5c0 2a 20 57 68 65 6e 20 6f 70 3d 3d 54 4b 5f 43 4f  * When op==TK_CO
6c5d0 4c 55 4d 4e 2c 20 74 68 65 6e 20 74 68 69 73 20  LUMN, then this 
6c5e0 65 78 70 72 20 6e 6f 64 65 20 6d 65 61 6e 73 20  expr node means 
6c5f0 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 20 20  the.            
6c600 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
6c610 69 43 6f 6c 75 6d 6e 2d 74 68 20 66 69 65 6c 64  iColumn-th field
6c620 20 6f 66 20 74 68 65 20 69 54 61 62 6c 65 2d 74   of the iTable-t
6c630 68 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 41 67  h table. */.  Ag
6c640 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f 3b  gInfo *pAggInfo;
6c650 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62 79 20       /* Used by 
6c660 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61 6e  TK_AGG_COLUMN an
6c670 64 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f  d TK_AGG_FUNCTIO
6c680 4e 20 2a 2f 0a 20 20 69 6e 74 20 69 41 67 67 3b  N */.  int iAgg;
6c690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
6c6a0 20 57 68 69 63 68 20 65 6e 74 72 79 20 69 6e 20   Which entry in 
6c6b0 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d  pAggInfo->aCol[]
6c6c0 20 6f 72 20 2d 3e 61 46 75 6e 63 5b 5d 20 2a 2f   or ->aFunc[] */
6c6d0 0a 20 20 69 6e 74 20 69 52 69 67 68 74 4a 6f 69  .  int iRightJoi
6c6e0 6e 54 61 62 6c 65 3b 20 20 20 2f 2a 20 49 66 20  nTable;   /* If 
6c6f0 45 50 5f 46 72 6f 6d 4a 6f 69 6e 2c 20 74 68 65  EP_FromJoin, the
6c700 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20   right table of 
6c710 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a 20 20 53 65  the join */.  Se
6c720 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b 20 20  lect *pSelect;  
6c730 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65       /* When the
6c740 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
6c750 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 20 41 6c   sub-select.  Al
6c760 73 6f 20 74 68 65 0a 20 20 20 20 20 20 20 20 20  so the.         
6c770 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6c780 2a 2a 20 72 69 67 68 74 20 73 69 64 65 20 6f 66  ** right side of
6c790 20 22 3c 65 78 70 72 3e 20 49 4e 20 28 3c 73 65   "<expr> IN (<se
6c7a0 6c 65 63 74 3e 29 22 20 2a 2f 0a 20 20 54 61 62  lect>)" */.  Tab
6c7b0 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20  le *pTab;       
6c7c0 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 66 6f 72      /* Table for
6c7d0 20 54 4b 5f 43 4f 4c 55 4d 4e 20 65 78 70 72 65   TK_COLUMN expre
6c7e0 73 73 69 6f 6e 73 2e 20 2a 2f 0a 23 69 66 20 53  ssions. */.#if S
6c7f0 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
6c800 45 50 54 48 3e 30 0a 20 20 69 6e 74 20 6e 48 65  EPTH>0.  int nHe
6c810 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
6c820 2f 2a 20 48 65 69 67 68 74 20 6f 66 20 74 68 65  /* Height of the
6c830 20 74 72 65 65 20 68 65 61 64 65 64 20 62 79 20   tree headed by 
6c840 74 68 69 73 20 6e 6f 64 65 20 2a 2f 0a 23 65 6e  this node */.#en
6c850 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68  dif.};../*.** Th
6c860 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
6c870 74 68 65 20 6d 65 61 6e 69 6e 67 73 20 6f 66 20  the meanings of 
6c880 62 69 74 73 20 69 6e 20 74 68 65 20 45 78 70 72  bits in the Expr
6c890 2e 66 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f  .flags field..*/
6c8a0 0a 23 64 65 66 69 6e 65 20 45 50 5f 46 72 6f 6d  .#define EP_From
6c8b0 4a 6f 69 6e 20 20 20 30 78 30 30 30 31 20 20 2f  Join   0x0001  /
6c8c0 2a 20 4f 72 69 67 69 6e 61 74 65 64 20 69 6e 20  * Originated in 
6c8d0 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61 75  ON or USING clau
6c8e0 73 65 20 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a  se of a join */.
6c8f0 23 64 65 66 69 6e 65 20 45 50 5f 41 67 67 20 20  #define EP_Agg  
6c900 20 20 20 20 20 20 30 78 30 30 30 32 20 20 2f 2a        0x0002  /*
6c910 20 43 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72   Contains one or
6c920 20 6d 6f 72 65 20 61 67 67 72 65 67 61 74 65 20   more aggregate 
6c930 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64 65  functions */.#de
6c940 66 69 6e 65 20 45 50 5f 52 65 73 6f 6c 76 65 64  fine EP_Resolved
6c950 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 49 44     0x0004  /* ID
6c960 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f  s have been reso
6c970 6c 76 65 64 20 74 6f 20 43 4f 4c 55 4d 4e 73 20  lved to COLUMNs 
6c980 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 45 72  */.#define EP_Er
6c990 72 6f 72 20 20 20 20 20 20 30 78 30 30 30 38 20  ror      0x0008 
6c9a0 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 63   /* Expression c
6c9b0 6f 6e 74 61 69 6e 73 20 6f 6e 65 20 6f 72 20 6d  ontains one or m
6c9c0 6f 72 65 20 65 72 72 6f 72 73 20 2a 2f 0a 23 64  ore errors */.#d
6c9d0 65 66 69 6e 65 20 45 50 5f 44 69 73 74 69 6e 63  efine EP_Distinc
6c9e0 74 20 20 20 30 78 30 30 31 30 20 20 2f 2a 20 41  t   0x0010  /* A
6c9f0 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
6ca00 6e 20 77 69 74 68 20 44 49 53 54 49 4e 43 54 20  n with DISTINCT 
6ca10 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65 66 69  keyword */.#defi
6ca20 6e 65 20 45 50 5f 56 61 72 53 65 6c 65 63 74 20  ne EP_VarSelect 
6ca30 20 30 78 30 30 32 30 20 20 2f 2a 20 70 53 65 6c   0x0020  /* pSel
6ca40 65 63 74 20 69 73 20 63 6f 72 72 65 6c 61 74 65  ect is correlate
6ca50 64 2c 20 6e 6f 74 20 63 6f 6e 73 74 61 6e 74 20  d, not constant 
6ca60 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f 44 65  */.#define EP_De
6ca70 71 75 6f 74 65 64 20 20 20 30 78 30 30 34 30 20  quoted   0x0040 
6ca80 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20   /* True if the 
6ca90 73 74 72 69 6e 67 20 68 61 73 20 62 65 65 6e 20  string has been 
6caa0 64 65 71 75 6f 74 65 64 20 2a 2f 0a 23 64 65 66  dequoted */.#def
6cab0 69 6e 65 20 45 50 5f 49 6e 66 69 78 46 75 6e 63  ine EP_InfixFunc
6cac0 20 20 30 78 30 30 38 30 20 20 2f 2a 20 54 72 75    0x0080  /* Tru
6cad0 65 20 66 6f 72 20 61 6e 20 69 6e 66 69 78 20 66  e for an infix f
6cae0 75 6e 63 74 69 6f 6e 3a 20 4c 49 4b 45 2c 20 47  unction: LIKE, G
6caf0 4c 4f 42 2c 20 65 74 63 20 2a 2f 0a 23 64 65 66  LOB, etc */.#def
6cb00 69 6e 65 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  ine EP_ExpCollat
6cb10 65 20 30 78 30 31 30 30 20 20 2f 2a 20 43 6f 6c  e 0x0100  /* Col
6cb20 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
6cb30 73 70 65 63 69 66 69 65 64 20 65 78 70 6c 69 63  specified explic
6cb40 69 74 6c 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20  itly */.#define 
6cb50 45 50 5f 41 6e 79 41 66 66 20 20 20 20 20 30 78  EP_AnyAff     0x
6cb60 30 32 30 30 20 20 2f 2a 20 43 61 6e 20 74 61 6b  0200  /* Can tak
6cb70 65 20 61 20 63 61 63 68 65 64 20 63 6f 6c 75 6d  e a cached colum
6cb80 6e 20 6f 66 20 61 6e 79 20 61 66 66 69 6e 69 74  n of any affinit
6cb90 79 20 2a 2f 0a 23 64 65 66 69 6e 65 20 45 50 5f  y */.#define EP_
6cba0 46 69 78 65 64 44 65 73 74 20 20 30 78 30 34 30  FixedDest  0x040
6cbb0 30 20 20 2f 2a 20 52 65 73 75 6c 74 20 6e 65 65  0  /* Result nee
6cbc0 64 65 64 20 69 6e 20 61 20 73 70 65 63 69 66 69  ded in a specifi
6cbd0 63 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 23 64  c register */.#d
6cbe0 65 66 69 6e 65 20 45 50 5f 49 6e 74 56 61 6c 75  efine EP_IntValu
6cbf0 65 20 20 20 30 78 30 38 30 30 20 20 2f 2a 20 49  e   0x0800  /* I
6cc00 6e 74 65 67 65 72 20 76 61 6c 75 65 20 63 6f 6e  nteger value con
6cc10 74 61 69 6e 65 64 20 69 6e 20 69 54 61 62 6c 65  tained in iTable
6cc20 20 2a 2f 0a 2f 2a 0a 2a 2a 20 54 68 65 73 65 20   */./*.** These 
6cc30 6d 61 63 72 6f 73 20 63 61 6e 20 62 65 20 75 73  macros can be us
6cc40 65 64 20 74 6f 20 74 65 73 74 2c 20 73 65 74 2c  ed to test, set,
6cc50 20 6f 72 20 63 6c 65 61 72 20 62 69 74 73 20 69   or clear bits i
6cc60 6e 20 74 68 65 20 0a 2a 2a 20 45 78 70 72 2e 66  n the .** Expr.f
6cc70 6c 61 67 73 20 66 69 65 6c 64 2e 0a 2a 2f 0a 23  lags field..*/.#
6cc80 64 65 66 69 6e 65 20 45 78 70 72 48 61 73 50 72  define ExprHasPr
6cc90 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
6cca0 28 28 28 45 29 2d 3e 66 6c 61 67 73 26 28 50 29  (((E)->flags&(P)
6ccb0 29 3d 3d 28 50 29 29 0a 23 64 65 66 69 6e 65 20  )==(P)).#define 
6ccc0 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70 65 72  ExprHasAnyProper
6ccd0 74 79 28 45 2c 50 29 20 20 28 28 28 45 29 2d 3e  ty(E,P)  (((E)->
6cce0 66 6c 61 67 73 26 28 50 29 29 21 3d 30 29 0a 23  flags&(P))!=0).#
6ccf0 64 65 66 69 6e 65 20 45 78 70 72 53 65 74 50 72  define ExprSetPr
6cd00 6f 70 65 72 74 79 28 45 2c 50 29 20 20 20 20 20  operty(E,P)     
6cd10 28 45 29 2d 3e 66 6c 61 67 73 7c 3d 28 50 29 0a  (E)->flags|=(P).
6cd20 23 64 65 66 69 6e 65 20 45 78 70 72 43 6c 65 61  #define ExprClea
6cd30 72 50 72 6f 70 65 72 74 79 28 45 2c 50 29 20 20  rProperty(E,P)  
6cd40 20 28 45 29 2d 3e 66 6c 61 67 73 26 3d 7e 28 50   (E)->flags&=~(P
6cd50 29 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73 74 20  )../*.** A list 
6cd60 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  of expressions. 
6cd70 20 45 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   Each expression
6cd80 20 6d 61 79 20 6f 70 74 69 6f 6e 61 6c 6c 79 20   may optionally 
6cd90 68 61 76 65 20 61 0a 2a 2a 20 6e 61 6d 65 2e 20  have a.** name. 
6cda0 20 41 6e 20 65 78 70 72 2f 6e 61 6d 65 20 63 6f   An expr/name co
6cdb0 6d 62 69 6e 61 74 69 6f 6e 20 63 61 6e 20 62 65  mbination can be
6cdc0 20 75 73 65 64 20 69 6e 20 73 65 76 65 72 61 6c   used in several
6cdd0 20 77 61 79 73 2c 20 73 75 63 68 0a 2a 2a 20 61   ways, such.** a
6cde0 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20 22 65  s the list of "e
6cdf0 78 70 72 20 41 53 20 49 44 22 20 66 69 65 6c 64  xpr AS ID" field
6ce00 73 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 20 22 53  s following a "S
6ce10 45 4c 45 43 54 22 20 6f 72 20 69 6e 20 74 68 65  ELECT" or in the
6ce20 0a 2a 2a 20 6c 69 73 74 20 6f 66 20 22 49 44 20  .** list of "ID 
6ce30 3d 20 65 78 70 72 22 20 69 74 65 6d 73 20 69 6e  = expr" items in
6ce40 20 61 6e 20 55 50 44 41 54 45 2e 20 20 41 20 6c   an UPDATE.  A l
6ce50 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
6ce60 6e 73 20 63 61 6e 0a 2a 2a 20 61 6c 73 6f 20 62  ns can.** also b
6ce70 65 20 75 73 65 64 20 61 73 20 74 68 65 20 61 72  e used as the ar
6ce80 67 75 6d 65 6e 74 20 74 6f 20 61 20 66 75 6e 63  gument to a func
6ce90 74 69 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 63  tion, in which c
6cea0 61 73 65 20 74 68 65 20 61 2e 7a 4e 61 6d 65 0a  ase the a.zName.
6ceb0 2a 2a 20 66 69 65 6c 64 20 69 73 20 6e 6f 74 20  ** field is not 
6cec0 75 73 65 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  used..*/.struct 
6ced0 45 78 70 72 4c 69 73 74 20 7b 0a 20 20 69 6e 74  ExprList {.  int
6cee0 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20 20 20   nExpr;         
6cef0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6cf00 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 6e 20   expressions on 
6cf10 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
6cf20 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20  t nAlloc;       
6cf30 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
6cf40 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63 61  f entries alloca
6cf50 74 65 64 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 69  ted below */.  i
6cf60 6e 74 20 69 45 43 75 72 73 6f 72 3b 20 20 20 20  nt iECursor;    
6cf70 20 20 20 20 20 20 2f 2a 20 56 44 42 45 20 43 75        /* VDBE Cu
6cf80 72 73 6f 72 20 61 73 73 6f 63 69 61 74 65 64 20  rsor associated 
6cf90 77 69 74 68 20 74 68 69 73 20 45 78 70 72 4c 69  with this ExprLi
6cfa0 73 74 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 45  st */.  struct E
6cfb0 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20  xprList_item {. 
6cfc0 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20     Expr *pExpr; 
6cfd0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
6cfe0 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
6cff0 69 6f 6e 73 20 2a 2f 0a 20 20 20 20 63 68 61 72  ions */.    char
6d000 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20   *zName;        
6d010 20 20 20 2f 2a 20 54 6f 6b 65 6e 20 61 73 73 6f     /* Token asso
6d020 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
6d030 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20   expression */. 
6d040 20 20 20 75 38 20 73 6f 72 74 4f 72 64 65 72 3b     u8 sortOrder;
6d050 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 20 66            /* 1 f
6d060 6f 72 20 44 45 53 43 20 6f 72 20 30 20 66 6f 72  or DESC or 0 for
6d070 20 41 53 43 20 2a 2f 0a 20 20 20 20 75 38 20 64   ASC */.    u8 d
6d080 6f 6e 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  one;            
6d090 20 20 20 2f 2a 20 41 20 66 6c 61 67 20 74 6f 20     /* A flag to 
6d0a0 69 6e 64 69 63 61 74 65 20 77 68 65 6e 20 70 72  indicate when pr
6d0b0 6f 63 65 73 73 69 6e 67 20 69 73 20 66 69 6e 69  ocessing is fini
6d0c0 73 68 65 64 20 2a 2f 0a 20 20 20 20 75 31 36 20  shed */.    u16 
6d0d0 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20  iCol;           
6d0e0 20 20 20 2f 2a 20 46 6f 72 20 4f 52 44 45 52 20     /* For ORDER 
6d0f0 42 59 2c 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  BY, column numbe
6d100 72 20 69 6e 20 72 65 73 75 6c 74 20 73 65 74 20  r in result set 
6d110 2a 2f 0a 20 20 20 20 75 31 36 20 69 41 6c 69 61  */.    u16 iAlia
6d120 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  s;            /*
6d130 20 49 6e 64 65 78 20 69 6e 74 6f 20 50 61 72 73   Index into Pars
6d140 65 2e 61 41 6c 69 61 73 5b 5d 20 66 6f 72 20 7a  e.aAlias[] for z
6d150 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b 20  Name */.  } *a; 
6d160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
6d170 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20 66 6f   /* One entry fo
6d180 72 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f  r each expressio
6d190 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  n */.};../*.** A
6d1a0 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
6d1b0 69 73 20 73 74 72 75 63 74 75 72 65 20 63 61 6e  is structure can
6d1c0 20 68 6f 6c 64 20 61 20 73 69 6d 70 6c 65 20 6c   hold a simple l
6d1d0 69 73 74 20 6f 66 20 69 64 65 6e 74 69 66 69 65  ist of identifie
6d1e0 72 73 2c 0a 2a 2a 20 73 75 63 68 20 61 73 20 74  rs,.** such as t
6d1f0 68 65 20 6c 69 73 74 20 22 61 2c 62 2c 63 22 20  he list "a,b,c" 
6d200 69 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  in the following
6d210 20 73 74 61 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a   statements:.**.
6d220 2a 2a 20 20 20 20 20 20 49 4e 53 45 52 54 20 49  **      INSERT I
6d230 4e 54 4f 20 74 28 61 2c 62 2c 63 29 20 56 41 4c  NTO t(a,b,c) VAL
6d240 55 45 53 20 2e 2e 2e 3b 0a 2a 2a 20 20 20 20 20  UES ...;.**     
6d250 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 69 64   CREATE INDEX id
6d260 78 20 4f 4e 20 74 28 61 2c 62 2c 63 29 3b 0a 2a  x ON t(a,b,c);.*
6d270 2a 20 20 20 20 20 20 43 52 45 41 54 45 20 54 52  *      CREATE TR
6d280 49 47 47 45 52 20 74 72 69 67 20 42 45 46 4f 52  IGGER trig BEFOR
6d290 45 20 55 50 44 41 54 45 20 4f 4e 20 74 28 61 2c  E UPDATE ON t(a,
6d2a0 62 2c 63 29 20 2e 2e 2e 3b 0a 2a 2a 0a 2a 2a 20  b,c) ...;.**.** 
6d2b0 54 68 65 20 49 64 4c 69 73 74 2e 61 2e 69 64 78  The IdList.a.idx
6d2c0 20 66 69 65 6c 64 20 69 73 20 75 73 65 64 20 77   field is used w
6d2d0 68 65 6e 20 74 68 65 20 49 64 4c 69 73 74 20 72  hen the IdList r
6d2e0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 6c 69  epresents the li
6d2f0 73 74 20 6f 66 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  st of.** column 
6d300 6e 61 6d 65 73 20 61 66 74 65 72 20 61 20 74 61  names after a ta
6d310 62 6c 65 20 6e 61 6d 65 20 69 6e 20 61 6e 20 49  ble name in an I
6d320 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e  NSERT statement.
6d330 20 20 49 6e 20 74 68 65 20 73 74 61 74 65 6d 65    In the stateme
6d340 6e 74 0a 2a 2a 0a 2a 2a 20 20 20 20 20 49 4e 53  nt.**.**     INS
6d350 45 52 54 20 49 4e 54 4f 20 74 28 61 2c 62 2c 63  ERT INTO t(a,b,c
6d360 29 20 2e 2e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 22  ) ....**.** If "
6d370 61 22 20 69 73 20 74 68 65 20 6b 2d 74 68 20 63  a" is the k-th c
6d380 6f 6c 75 6d 6e 20 6f 66 20 74 61 62 6c 65 20 22  olumn of table "
6d390 74 22 2c 20 74 68 65 6e 20 49 64 4c 69 73 74 2e  t", then IdList.
6d3a0 61 5b 30 5d 2e 69 64 78 3d 3d 6b 2e 0a 2a 2f 0a  a[0].idx==k..*/.
6d3b0 73 74 72 75 63 74 20 49 64 4c 69 73 74 20 7b 0a  struct IdList {.
6d3c0 20 20 73 74 72 75 63 74 20 49 64 4c 69 73 74 5f    struct IdList_
6d3d0 69 74 65 6d 20 7b 0a 20 20 20 20 63 68 61 72 20  item {.    char 
6d3e0 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20  *zName;      /* 
6d3f0 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 64 65 6e  Name of the iden
6d400 74 69 66 69 65 72 20 2a 2f 0a 20 20 20 20 69 6e  tifier */.    in
6d410 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
6d420 2f 2a 20 49 6e 64 65 78 20 69 6e 20 73 6f 6d 65  /* Index in some
6d430 20 54 61 62 6c 65 2e 61 43 6f 6c 5b 5d 20 6f 66   Table.aCol[] of
6d440 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20   a column named 
6d450 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7d 20 2a 61 3b  zName */.  } *a;
6d460 0a 20 20 69 6e 74 20 6e 49 64 3b 20 20 20 20 20  .  int nId;     
6d470 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
6d480 20 69 64 65 6e 74 69 66 69 65 72 73 20 6f 6e 20   identifiers on 
6d490 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20 20 69 6e  the list */.  in
6d4a0 74 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 2f  t nAlloc;      /
6d4b0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  * Number of entr
6d4c0 69 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f  ies allocated fo
6d4d0 72 20 61 5b 5d 20 62 65 6c 6f 77 20 2a 2f 0a 7d  r a[] below */.}
6d4e0 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 62 69 74  ;../*.** The bit
6d4f0 6d 61 73 6b 20 64 61 74 61 74 79 70 65 20 64 65  mask datatype de
6d500 66 69 6e 65 64 20 62 65 6c 6f 77 20 69 73 20 75  fined below is u
6d510 73 65 64 20 66 6f 72 20 76 61 72 69 6f 75 73 20  sed for various 
6d520 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73 2e 0a 2a  optimizations..*
6d530 2a 0a 2a 2a 20 43 68 61 6e 67 69 6e 67 20 74 68  *.** Changing th
6d540 69 73 20 66 72 6f 6d 20 61 20 36 34 2d 62 69 74  is from a 64-bit
6d550 20 74 6f 20 61 20 33 32 2d 62 69 74 20 74 79 70   to a 32-bit typ
6d560 65 20 6c 69 6d 69 74 73 20 74 68 65 20 6e 75 6d  e limits the num
6d570 62 65 72 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73  ber of.** tables
6d580 20 69 6e 20 61 20 6a 6f 69 6e 20 74 6f 20 33 32   in a join to 32
6d590 20 69 6e 73 74 65 61 64 20 6f 66 20 36 34 2e 20   instead of 64. 
6d5a0 20 42 75 74 20 69 74 20 61 6c 73 6f 20 72 65 64   But it also red
6d5b0 75 63 65 73 20 74 68 65 20 73 69 7a 65 0a 2a 2a  uces the size.**
6d5c0 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 20   of the library 
6d5d0 62 79 20 37 33 38 20 62 79 74 65 73 20 6f 6e 20  by 738 bytes on 
6d5e0 69 78 38 36 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  ix86..*/.typedef
6d5f0 20 75 36 34 20 42 69 74 6d 61 73 6b 3b 0a 0a 2f   u64 Bitmask;../
6d600 2a 0a 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20  *.** The number 
6d610 6f 66 20 62 69 74 73 20 69 6e 20 61 20 42 69 74  of bits in a Bit
6d620 6d 61 73 6b 2e 20 20 22 42 4d 53 22 20 6d 65 61  mask.  "BMS" mea
6d630 6e 73 20 22 42 69 74 4d 61 73 6b 20 53 69 7a 65  ns "BitMask Size
6d640 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 42 4d  "..*/.#define BM
6d650 53 20 20 28 28 69 6e 74 29 28 73 69 7a 65 6f 66  S  ((int)(sizeof
6d660 28 42 69 74 6d 61 73 6b 29 2a 38 29 29 0a 0a 2f  (Bitmask)*8))../
6d670 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
6d680 6e 67 20 73 74 72 75 63 74 75 72 65 20 64 65 73  ng structure des
6d690 63 72 69 62 65 73 20 74 68 65 20 46 52 4f 4d 20  cribes the FROM 
6d6a0 63 6c 61 75 73 65 20 6f 66 20 61 20 53 45 4c 45  clause of a SELE
6d6b0 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a  CT statement..**
6d6c0 20 45 61 63 68 20 74 61 62 6c 65 20 6f 72 20 73   Each table or s
6d6d0 75 62 71 75 65 72 79 20 69 6e 20 74 68 65 20 46  ubquery in the F
6d6e0 52 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 61 20  ROM clause is a 
6d6f0 73 65 70 61 72 61 74 65 20 65 6c 65 6d 65 6e 74  separate element
6d700 20 6f 66 0a 2a 2a 20 74 68 65 20 53 72 63 4c 69   of.** the SrcLi
6d710 73 74 2e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  st.a[] array..**
6d720 0a 2a 2a 20 57 69 74 68 20 74 68 65 20 61 64 64  .** With the add
6d730 69 74 69 6f 6e 20 6f 66 20 6d 75 6c 74 69 70 6c  ition of multipl
6d740 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
6d750 72 74 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  rt, the followin
6d760 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 63  g structure.** c
6d770 61 6e 20 61 6c 73 6f 20 62 65 20 75 73 65 64 20  an also be used 
6d780 74 6f 20 64 65 73 63 72 69 62 65 20 61 20 70 61  to describe a pa
6d790 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 20 73  rticular table s
6d7a0 75 63 68 20 61 73 20 74 68 65 20 74 61 62 6c 65  uch as the table
6d7b0 20 74 68 61 74 0a 2a 2a 20 69 73 20 6d 6f 64 69   that.** is modi
6d7c0 66 69 65 64 20 62 79 20 61 6e 20 49 4e 53 45 52  fied by an INSER
6d7d0 54 2c 20 44 45 4c 45 54 45 2c 20 6f 72 20 55 50  T, DELETE, or UP
6d7e0 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e 20  DATE statement. 
6d7f0 20 49 6e 20 73 74 61 6e 64 61 72 64 20 53 51 4c   In standard SQL
6d800 2c 0a 2a 2a 20 73 75 63 68 20 61 20 74 61 62 6c  ,.** such a tabl
6d810 65 20 6d 75 73 74 20 62 65 20 61 20 73 69 6d 70  e must be a simp
6d820 6c 65 20 6e 61 6d 65 3a 20 49 44 2e 20 20 42 75  le name: ID.  Bu
6d830 74 20 69 6e 20 53 51 4c 69 74 65 2c 20 74 68 65  t in SQLite, the
6d840 20 74 61 62 6c 65 20 63 61 6e 0a 2a 2a 20 6e 6f   table can.** no
6d850 77 20 62 65 20 69 64 65 6e 74 69 66 69 65 64 20  w be identified 
6d860 62 79 20 61 20 64 61 74 61 62 61 73 65 20 6e 61  by a database na
6d870 6d 65 2c 20 61 20 64 6f 74 2c 20 74 68 65 6e 20  me, a dot, then 
6d880 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 3a 20  the table name: 
6d890 49 44 2e 49 44 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ID.ID..**.** The
6d8a0 20 6a 6f 69 6e 74 79 70 65 20 73 74 61 72 74 73   jointype starts
6d8b0 20 6f 75 74 20 73 68 6f 77 69 6e 67 20 74 68 65   out showing the
6d8c0 20 6a 6f 69 6e 20 74 79 70 65 20 62 65 74 77 65   join type betwe
6d8d0 65 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  en the current t
6d8e0 61 62 6c 65 0a 2a 2a 20 61 6e 64 20 74 68 65 20  able.** and the 
6d8f0 6e 65 78 74 20 74 61 62 6c 65 20 6f 6e 20 74 68  next table on th
6d900 65 20 6c 69 73 74 2e 20 20 54 68 65 20 70 61 72  e list.  The par
6d910 73 65 72 20 62 75 69 6c 64 73 20 74 68 65 20 6c  ser builds the l
6d920 69 73 74 20 74 68 69 73 20 77 61 79 2e 0a 2a 2a  ist this way..**
6d930 20 42 75 74 20 73 71 6c 69 74 65 33 53 72 63 4c   But sqlite3SrcL
6d940 69 73 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65  istShiftJoinType
6d950 28 29 20 6c 61 74 65 72 20 73 68 69 66 74 73 20  () later shifts 
6d960 74 68 65 20 6a 6f 69 6e 74 79 70 65 73 20 73 6f  the jointypes so
6d970 20 74 68 61 74 20 65 61 63 68 0a 2a 2a 20 6a 6f   that each.** jo
6d980 69 6e 74 79 70 65 20 65 78 70 72 65 73 73 65 73  intype expresses
6d990 20 74 68 65 20 6a 6f 69 6e 20 62 65 74 77 65 65   the join betwee
6d9a0 6e 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  n the table and 
6d9b0 74 68 65 20 70 72 65 76 69 6f 75 73 20 74 61 62  the previous tab
6d9c0 6c 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53 72  le..*/.struct Sr
6d9d0 63 4c 69 73 74 20 7b 0a 20 20 69 31 36 20 6e 53  cList {.  i16 nS
6d9e0 72 63 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  rc;        /* Nu
6d9f0 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20 6f  mber of tables o
6da00 72 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20  r subqueries in 
6da10 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
6da20 2a 2f 0a 20 20 69 31 36 20 6e 41 6c 6c 6f 63 3b  */.  i16 nAlloc;
6da30 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
6da40 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
6da50 61 74 65 64 20 69 6e 20 61 5b 5d 20 62 65 6c 6f  ated in a[] belo
6da60 77 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  w */.  struct Sr
6da70 63 4c 69 73 74 5f 69 74 65 6d 20 7b 0a 20 20 20  cList_item {.   
6da80 20 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65   char *zDatabase
6da90 3b 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61  ;  /* Name of da
6daa0 74 61 62 61 73 65 20 68 6f 6c 64 69 6e 67 20 74  tabase holding t
6dab0 68 69 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  his table */.   
6dac0 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20   char *zName;   
6dad0 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
6dae0 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 63  e table */.    c
6daf0 68 61 72 20 2a 7a 41 6c 69 61 73 3b 20 20 20 20  har *zAlias;    
6db00 20 2f 2a 20 54 68 65 20 22 42 22 20 70 61 72 74   /* The "B" part
6db10 20 6f 66 20 61 20 22 41 20 41 53 20 42 22 20 70   of a "A AS B" p
6db20 68 72 61 73 65 2e 20 20 7a 4e 61 6d 65 20 69 73  hrase.  zName is
6db30 20 74 68 65 20 22 41 22 20 2a 2f 0a 20 20 20 20   the "A" */.    
6db40 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
6db50 20 20 2f 2a 20 41 6e 20 53 51 4c 20 74 61 62 6c    /* An SQL tabl
6db60 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
6db70 74 6f 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 20 20  to zName */.    
6db80 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 3b  Select *pSelect;
6db90 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74    /* A SELECT st
6dba0 61 74 65 6d 65 6e 74 20 75 73 65 64 20 69 6e 20  atement used in 
6dbb0 70 6c 61 63 65 20 6f 66 20 61 20 74 61 62 6c 65  place of a table
6dbc0 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20 75 38 20   name */.    u8 
6dbd0 69 73 50 6f 70 75 6c 61 74 65 64 3b 20 20 20 2f  isPopulated;   /
6dbe0 2a 20 54 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * Temporary tabl
6dbf0 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  e associated wit
6dc00 68 20 53 45 4c 45 43 54 20 69 73 20 70 6f 70 75  h SELECT is popu
6dc10 6c 61 74 65 64 20 2a 2f 0a 20 20 20 20 75 38 20  lated */.    u8 
6dc20 6a 6f 69 6e 74 79 70 65 3b 20 20 20 20 20 20 2f  jointype;      /
6dc30 2a 20 54 79 70 65 20 6f 66 20 6a 6f 69 6e 20 62  * Type of join b
6dc40 65 74 77 65 65 6e 20 74 68 69 73 20 61 62 6c 65  etween this able
6dc50 20 61 6e 64 20 74 68 65 20 70 72 65 76 69 6f 75   and the previou
6dc60 73 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 75  s */.    int iCu
6dc70 72 73 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68  rsor;      /* Th
6dc80 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
6dc90 6d 62 65 72 20 75 73 65 64 20 74 6f 20 61 63 63  mber used to acc
6dca0 65 73 73 20 74 68 69 73 20 74 61 62 6c 65 20 2a  ess this table *
6dcb0 2f 0a 20 20 20 20 45 78 70 72 20 2a 70 4f 6e 3b  /.    Expr *pOn;
6dcc0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4f          /* The O
6dcd0 4e 20 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f  N clause of a jo
6dce0 69 6e 20 2a 2f 0a 20 20 20 20 49 64 4c 69 73 74  in */.    IdList
6dcf0 20 2a 70 55 73 69 6e 67 3b 20 20 20 2f 2a 20 54   *pUsing;   /* T
6dd00 68 65 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  he USING clause 
6dd10 6f 66 20 61 20 6a 6f 69 6e 20 2a 2f 0a 20 20 20  of a join */.   
6dd20 20 42 69 74 6d 61 73 6b 20 63 6f 6c 55 73 65 64   Bitmask colUsed
6dd30 3b 20 20 2f 2a 20 42 69 74 20 4e 20 28 31 3c 3c  ;  /* Bit N (1<<
6dd40 4e 29 20 73 65 74 20 69 66 20 63 6f 6c 75 6d 6e  N) set if column
6dd50 20 4e 20 6f 72 20 70 54 61 62 20 69 73 20 75 73   N or pTab is us
6dd60 65 64 20 2a 2f 0a 20 20 20 20 75 38 20 6e 6f 74  ed */.    u8 not
6dd70 49 6e 64 65 78 65 64 3b 20 20 20 20 2f 2a 20 54  Indexed;    /* T
6dd80 72 75 65 20 69 66 20 74 68 65 72 65 20 69 73 20  rue if there is 
6dd90 61 20 4e 4f 54 20 49 4e 44 45 58 45 44 20 63 6c  a NOT INDEXED cl
6dda0 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 68 61 72  ause */.    char
6ddb0 20 2a 7a 49 6e 64 65 78 3b 20 20 20 20 20 2f 2a   *zIndex;     /*
6ddc0 20 49 64 65 6e 74 69 66 69 65 72 20 66 72 6f 6d   Identifier from
6ddd0 20 22 49 4e 44 45 58 45 44 20 42 59 20 3c 7a 49   "INDEXED BY <zI
6dde0 6e 64 65 78 3e 22 20 63 6c 61 75 73 65 20 2a 2f  ndex>" clause */
6ddf0 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64  .    Index *pInd
6de00 65 78 3b 20 20 20 20 2f 2a 20 49 6e 64 65 78 20  ex;    /* Index 
6de10 73 74 72 75 63 74 75 72 65 20 63 6f 72 72 65 73  structure corres
6de20 70 6f 6e 64 69 6e 67 20 74 6f 20 7a 49 6e 64 65  ponding to zInde
6de30 78 2c 20 69 66 20 61 6e 79 20 2a 2f 0a 20 20 7d  x, if any */.  }
6de40 20 61 5b 31 5d 3b 20 20 20 20 20 20 20 20 20 20   a[1];          
6de50 20 20 20 2f 2a 20 4f 6e 65 20 65 6e 74 72 79 20     /* One entry 
6de60 66 6f 72 20 65 61 63 68 20 69 64 65 6e 74 69 66  for each identif
6de70 69 65 72 20 6f 6e 20 74 68 65 20 6c 69 73 74 20  ier on the list 
6de80 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 50 65 72  */.};../*.** Per
6de90 6d 69 74 74 65 64 20 76 61 6c 75 65 73 20 6f 66  mitted values of
6dea0 20 74 68 65 20 53 72 63 4c 69 73 74 2e 61 2e 6a   the SrcList.a.j
6deb0 6f 69 6e 74 79 70 65 20 66 69 65 6c 64 0a 2a 2f  ointype field.*/
6dec0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 49 4e 4e 45  .#define JT_INNE
6ded0 52 20 20 20 20 20 30 78 30 30 30 31 20 20 20 20  R     0x0001    
6dee0 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 69  /* Any kind of i
6def0 6e 6e 65 72 20 6f 72 20 63 72 6f 73 73 20 6a 6f  nner or cross jo
6df00 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 54  in */.#define JT
6df10 5f 43 52 4f 53 53 20 20 20 20 20 30 78 30 30 30  _CROSS     0x000
6df20 32 20 20 20 20 2f 2a 20 45 78 70 6c 69 63 69 74  2    /* Explicit
6df30 20 75 73 65 20 6f 66 20 74 68 65 20 43 52 4f 53   use of the CROS
6df40 53 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 23 64 65  S keyword */.#de
6df50 66 69 6e 65 20 4a 54 5f 4e 41 54 55 52 41 4c 20  fine JT_NATURAL 
6df60 20 20 30 78 30 30 30 34 20 20 20 20 2f 2a 20 54    0x0004    /* T
6df70 72 75 65 20 66 6f 72 20 61 20 22 6e 61 74 75 72  rue for a "natur
6df80 61 6c 22 20 6a 6f 69 6e 20 2a 2f 0a 23 64 65 66  al" join */.#def
6df90 69 6e 65 20 4a 54 5f 4c 45 46 54 20 20 20 20 20  ine JT_LEFT     
6dfa0 20 30 78 30 30 30 38 20 20 20 20 2f 2a 20 4c 65   0x0008    /* Le
6dfb0 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 20 2a 2f  ft outer join */
6dfc0 0a 23 64 65 66 69 6e 65 20 4a 54 5f 52 49 47 48  .#define JT_RIGH
6dfd0 54 20 20 20 20 20 30 78 30 30 31 30 20 20 20 20  T     0x0010    
6dfe0 2f 2a 20 52 69 67 68 74 20 6f 75 74 65 72 20 6a  /* Right outer j
6dff0 6f 69 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4a  oin */.#define J
6e000 54 5f 4f 55 54 45 52 20 20 20 20 20 30 78 30 30  T_OUTER     0x00
6e010 32 30 20 20 20 20 2f 2a 20 54 68 65 20 22 4f 55  20    /* The "OU
6e020 54 45 52 22 20 6b 65 79 77 6f 72 64 20 69 73 20  TER" keyword is 
6e030 70 72 65 73 65 6e 74 20 2a 2f 0a 23 64 65 66 69  present */.#defi
6e040 6e 65 20 4a 54 5f 45 52 52 4f 52 20 20 20 20 20  ne JT_ERROR     
6e050 30 78 30 30 34 30 20 20 20 20 2f 2a 20 75 6e 6b  0x0040    /* unk
6e060 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72  nown or unsuppor
6e070 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 20 2a 2f  ted join type */
6e080 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65 61 63 68  ../*.** For each
6e090 20 6e 65 73 74 65 64 20 6c 6f 6f 70 20 69 6e 20   nested loop in 
6e0a0 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69  a WHERE clause i
6e0b0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 74  mplementation, t
6e0c0 68 65 20 57 68 65 72 65 49 6e 66 6f 0a 2a 2a 20  he WhereInfo.** 
6e0d0 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
6e0e0 6e 73 20 61 20 73 69 6e 67 6c 65 20 69 6e 73 74  ns a single inst
6e0f0 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72  ance of this str
6e100 75 63 74 75 72 65 2e 20 20 54 68 69 73 20 73 74  ucture.  This st
6e110 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 69 6e  ructure.** is in
6e120 74 65 6e 64 65 64 20 74 6f 20 62 65 20 70 72 69  tended to be pri
6e130 76 61 74 65 20 74 68 65 20 74 68 65 20 77 68 65  vate the the whe
6e140 72 65 2e 63 20 6d 6f 64 75 6c 65 20 61 6e 64 20  re.c module and 
6e150 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
6e160 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69 66   access or modif
6e170 69 65 64 20 62 79 20 6f 74 68 65 72 20 6d 6f 64  ied by other mod
6e180 75 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ules..**.** The 
6e190 70 49 64 78 49 6e 66 6f 20 61 6e 64 20 70 42 65  pIdxInfo and pBe
6e1a0 73 74 49 64 78 20 66 69 65 6c 64 73 20 61 72 65  stIdx fields are
6e1b0 20 75 73 65 64 20 74 6f 20 68 65 6c 70 20 70 69   used to help pi
6e1c0 63 6b 20 74 68 65 20 62 65 73 74 0a 2a 2a 20 69  ck the best.** i
6e1d0 6e 64 65 78 20 6f 6e 20 61 20 76 69 72 74 75 61  ndex on a virtua
6e1e0 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20 70 49  l table.  The pI
6e1f0 64 78 49 6e 66 6f 20 70 6f 69 6e 74 65 72 20 63  dxInfo pointer c
6e200 6f 6e 74 61 69 6e 73 20 69 6e 64 65 78 69 6e 67  ontains indexing
6e210 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  .** information 
6e220 66 6f 72 20 74 68 65 20 69 2d 74 68 20 74 61 62  for the i-th tab
6e230 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  le in the FROM c
6e240 6c 61 75 73 65 20 62 65 66 6f 72 65 20 72 65 6f  lause before reo
6e250 72 64 65 72 69 6e 67 2e 0a 2a 2a 20 41 6c 6c 20  rdering..** All 
6e260 74 68 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69  the pIdxInfo poi
6e270 6e 74 65 72 73 20 61 72 65 20 66 72 65 65 64 20  nters are freed 
6e280 62 79 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65  by whereInfoFree
6e290 28 29 20 69 6e 20 77 68 65 72 65 2e 63 2e 0a 2a  () in where.c..*
6e2a0 2a 20 54 68 65 20 70 42 65 73 74 49 64 78 20 70  * The pBestIdx p
6e2b0 6f 69 6e 74 65 72 20 69 73 20 61 20 63 6f 70 79  ointer is a copy
6e2c0 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f 72   of pIdxInfo for
6e2d0 20 74 68 65 20 69 2d 74 68 20 74 61 62 6c 65 20   the i-th table 
6e2e0 61 66 74 65 72 0a 2a 2a 20 46 52 4f 4d 20 63 6c  after.** FROM cl
6e2f0 61 75 73 65 20 6f 72 64 65 72 69 6e 67 2e 20 20  ause ordering.  
6e300 54 68 69 73 20 69 73 20 61 20 6c 69 74 74 6c 65  This is a little
6e310 20 63 6f 6e 66 75 73 69 6e 67 20 73 6f 20 49 20   confusing so I 
6e320 77 69 6c 6c 20 72 65 70 65 61 74 0a 2a 2a 20 69  will repeat.** i
6e330 74 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 77  t in different w
6e340 6f 72 64 73 2e 20 20 57 68 65 72 65 49 6e 66 6f  ords.  WhereInfo
6e350 2e 61 5b 69 5d 2e 70 49 64 78 49 6e 66 6f 20 69  .a[i].pIdxInfo i
6e360 73 20 69 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74  s index informat
6e370 69 6f 6e 20 0a 2a 2a 20 66 6f 72 20 57 68 65 72  ion .** for Wher
6e380 65 49 6e 66 6f 2e 70 54 61 62 4c 69 73 74 2e 61  eInfo.pTabList.a
6e390 5b 69 5d 2e 20 20 57 68 65 72 65 49 6e 66 6f 2e  [i].  WhereInfo.
6e3a0 61 5b 69 5d 2e 70 42 65 73 74 49 6e 66 6f 20 69  a[i].pBestInfo i
6e3b0 73 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 69  s the.** index i
6e3c0 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
6e3d0 68 65 20 69 2d 74 68 20 6c 6f 6f 70 20 6f 66 20  he i-th loop of 
6e3e0 74 68 65 20 6a 6f 69 6e 2e 20 20 70 42 65 73 74  the join.  pBest
6e3f0 49 6e 66 6f 20 69 73 20 61 6c 77 61 79 73 0a 2a  Info is always.*
6e400 2a 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72  * either NULL or
6e410 20 61 20 63 6f 70 79 20 6f 66 20 73 6f 6d 65 20   a copy of some 
6e420 70 49 64 78 49 6e 66 6f 2e 20 20 53 6f 20 66 6f  pIdxInfo.  So fo
6e430 72 20 63 6c 65 61 6e 75 70 20 69 74 20 69 73 20  r cleanup it is 
6e440 0a 2a 2a 20 73 75 66 66 69 63 69 65 6e 74 20 74  .** sufficient t
6e450 6f 20 66 72 65 65 20 61 6c 6c 20 6f 66 20 74 68  o free all of th
6e460 65 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e 74  e pIdxInfo point
6e470 65 72 73 2e 0a 2a 2a 20 0a 2a 2f 0a 73 74 72 75  ers..** .*/.stru
6e480 63 74 20 57 68 65 72 65 4c 65 76 65 6c 20 7b 0a  ct WhereLevel {.
6e490 20 20 69 6e 74 20 69 46 72 6f 6d 3b 20 20 20 20    int iFrom;    
6e4a0 20 20 20 20 20 20 20 20 2f 2a 20 57 68 69 63 68          /* Which
6e4b0 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 46 52   entry in the FR
6e4c0 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  OM clause */.  i
6e4d0 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
6e4e0 20 20 20 20 20 2f 2a 20 46 6c 61 67 73 20 61 73       /* Flags as
6e4f0 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68  sociated with th
6e500 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 69 6e  is level */.  in
6e510 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
6e520 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6d 65 6d      /* First mem
6e530 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 62 79  ory cell used by
6e540 20 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20   this level */. 
6e550 20 69 6e 74 20 69 4c 65 66 74 4a 6f 69 6e 3b 20   int iLeftJoin; 
6e560 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79         /* Memory
6e570 20 63 65 6c 6c 20 75 73 65 64 20 74 6f 20 69 6d   cell used to im
6e580 70 6c 65 6d 65 6e 74 20 4c 45 46 54 20 4f 55 54  plement LEFT OUT
6e590 45 52 20 4a 4f 49 4e 20 2a 2f 0a 20 20 49 6e 64  ER JOIN */.  Ind
6e5a0 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20 20  ex *pIdx;       
6e5b0 20 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 64     /* Index used
6e5c0 2e 20 20 4e 55 4c 4c 20 69 66 20 6e 6f 20 69 6e  .  NULL if no in
6e5d0 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  dex */.  int iTa
6e5e0 62 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 2f  bCur;          /
6e5f0 2a 20 54 68 65 20 56 44 42 45 20 63 75 72 73 6f  * The VDBE curso
6e600 72 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  r used to access
6e610 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
6e620 69 6e 74 20 69 49 64 78 43 75 72 3b 20 20 20 20  int iIdxCur;    
6e630 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56 44 42        /* The VDB
6e640 45 20 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f  E cursor used to
6e650 20 61 63 65 73 73 73 20 70 49 64 78 20 2a 2f 0a   acesss pIdx */.
6e660 20 20 69 6e 74 20 62 72 6b 3b 20 20 20 20 20 20    int brk;      
6e670 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20          /* Jump 
6e680 68 65 72 65 20 74 6f 20 62 72 65 61 6b 20 6f 75  here to break ou
6e690 74 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f  t of the loop */
6e6a0 0a 20 20 69 6e 74 20 6e 78 74 3b 20 20 20 20 20  .  int nxt;     
6e6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
6e6c0 20 68 65 72 65 20 74 6f 20 73 74 61 72 74 20 74   here to start t
6e6d0 68 65 20 6e 65 78 74 20 49 4e 20 63 6f 6d 62 69  he next IN combi
6e6e0 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  nation */.  int 
6e6f0 63 6f 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  cont;           
6e700 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
6e710 6f 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20  o continue with 
6e720 74 68 65 20 6e 65 78 74 20 6c 6f 6f 70 20 63 79  the next loop cy
6e730 63 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70  cle */.  int top
6e740 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
6e750 2a 20 46 69 72 73 74 20 69 6e 73 74 72 75 63 74  * First instruct
6e760 69 6f 6e 20 6f 66 20 69 6e 74 65 72 69 6f 72 20  ion of interior 
6e770 6f 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  of the loop */. 
6e780 20 69 6e 74 20 6f 70 2c 20 70 31 2c 20 70 32 2c   int op, p1, p2,
6e790 20 70 35 3b 20 20 20 2f 2a 20 4f 70 63 6f 64 65   p5;   /* Opcode
6e7a0 20 75 73 65 64 20 74 6f 20 74 65 72 6d 69 6e 61   used to termina
6e7b0 74 65 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  te the loop */. 
6e7c0 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20   int nEq;       
6e7d0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
6e7e0 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e   of == or IN con
6e7f0 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68 69 73  straints on this
6e800 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e   loop */.  int n
6e810 49 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  In;             
6e820 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 49 4e   /* Number of IN
6e830 20 6f 70 65 72 61 74 6f 72 73 20 63 6f 6e 73 74   operators const
6e840 72 61 69 6e 69 6e 67 20 74 68 69 73 20 6c 6f 6f  raining this loo
6e850 70 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 49 6e  p */.  struct In
6e860 4c 6f 6f 70 20 7b 0a 20 20 20 20 69 6e 74 20 69  Loop {.    int i
6e870 43 75 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  Cur;            
6e880 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 63 75    /* The VDBE cu
6e890 72 73 6f 72 20 75 73 65 64 20 62 79 20 74 68 69  rsor used by thi
6e8a0 73 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 2a 2f  s IN operator */
6e8b0 0a 20 20 20 20 69 6e 74 20 74 6f 70 41 64 64 72  .    int topAddr
6e8c0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
6e8d0 6f 70 20 6f 66 20 74 68 65 20 49 4e 20 6c 6f 6f  op of the IN loo
6e8e0 70 20 2a 2f 0a 20 20 7d 20 2a 61 49 6e 4c 6f 6f  p */.  } *aInLoo
6e8f0 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  p;           /* 
6e900 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
6e910 74 20 65 61 63 68 20 6e 65 73 74 65 64 20 49 4e  t each nested IN
6e920 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20 20 73   operator */.  s
6e930 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
6e940 6f 20 2a 70 42 65 73 74 49 64 78 3b 20 20 2f 2a  o *pBestIdx;  /*
6e950 20 49 6e 64 65 78 20 69 6e 66 6f 72 6d 61 74 69   Index informati
6e960 6f 6e 20 66 6f 72 20 74 68 69 73 20 6c 65 76 65  on for this leve
6e970 6c 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20 66  l */..  /* The f
6e980 6f 6c 6c 6f 77 69 6e 67 20 66 69 65 6c 64 20 69  ollowing field i
6e990 73 20 72 65 61 6c 6c 79 20 6e 6f 74 20 70 61 72  s really not par
6e9a0 74 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  t of the current
6e9b0 20 6c 65 76 65 6c 2e 20 20 42 75 74 0a 20 20 2a   level.  But.  *
6e9c0 2a 20 77 65 20 6e 65 65 64 20 61 20 70 6c 61 63  * we need a plac
6e9d0 65 20 74 6f 20 63 61 63 68 65 20 69 6e 64 65 78  e to cache index
6e9e0 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
6e9f0 20 65 61 63 68 20 74 61 62 6c 65 20 69 6e 20 74   each table in t
6ea00 68 65 0a 20 20 2a 2a 20 46 52 4f 4d 20 63 6c 61  he.  ** FROM cla
6ea10 75 73 65 20 61 6e 64 20 74 68 65 20 57 68 65 72  use and the Wher
6ea20 65 4c 65 76 65 6c 20 73 74 72 75 63 74 75 72 65  eLevel structure
6ea30 20 69 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 74   is a convenient
6ea40 20 70 6c 61 63 65 2e 0a 20 20 2a 2f 0a 20 20 73   place..  */.  s
6ea50 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
6ea60 6f 20 2a 70 49 64 78 49 6e 66 6f 3b 20 20 2f 2a  o *pIdxInfo;  /*
6ea70 20 49 6e 64 65 78 20 69 6e 66 6f 20 66 6f 72 20   Index info for 
6ea80 6e 2d 74 68 20 73 6f 75 72 63 65 20 74 61 62 6c  n-th source tabl
6ea90 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46  e */.};../*.** F
6eaa0 6c 61 67 73 20 61 70 70 72 6f 70 72 69 61 74 65  lags appropriate
6eab0 20 66 6f 72 20 74 68 65 20 77 66 6c 61 67 73 20   for the wflags 
6eac0 70 61 72 61 6d 65 74 65 72 20 6f 66 20 73 71 6c  parameter of sql
6ead0 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
6eae0 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45  ..*/.#define WHE
6eaf0 52 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41  RE_ORDERBY_NORMA
6eb00 4c 20 20 20 20 20 30 20 20 20 2f 2a 20 4e 6f 2d  L     0   /* No-
6eb10 6f 70 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  op */.#define WH
6eb20 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 20  ERE_ORDERBY_MIN 
6eb30 20 20 20 20 20 20 20 31 20 20 20 2f 2a 20 4f 52         1   /* OR
6eb40 44 45 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e  DER BY processin
6eb50 67 20 66 6f 72 20 6d 69 6e 28 29 20 66 75 6e 63  g for min() func
6eb60 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52   */.#define WHER
6eb70 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 20 20 20  E_ORDERBY_MAX   
6eb80 20 20 20 20 20 32 20 20 20 2f 2a 20 4f 52 44 45       2   /* ORDE
6eb90 52 20 42 59 20 70 72 6f 63 65 73 73 69 6e 67 20  R BY processing 
6eba0 66 6f 72 20 6d 61 78 28 29 20 66 75 6e 63 20 2a  for max() func *
6ebb0 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f  /.#define WHERE_
6ebc0 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 20  ONEPASS_DESIRED 
6ebd0 20 20 20 34 20 20 20 2f 2a 20 57 61 6e 74 20 74     4   /* Want t
6ebe0 6f 20 64 6f 20 6f 6e 65 2d 70 61 73 73 20 55 50  o do one-pass UP
6ebf0 44 41 54 45 2f 44 45 4c 45 54 45 20 2a 2f 0a 0a  DATE/DELETE */..
6ec00 2f 2a 0a 2a 2a 20 54 68 65 20 57 48 45 52 45 20  /*.** The WHERE 
6ec10 63 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e  clause processin
6ec20 67 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 77  g routine has tw
6ec30 6f 20 68 61 6c 76 65 73 2e 20 20 54 68 65 0a 2a  o halves.  The.*
6ec40 2a 20 66 69 72 73 74 20 70 61 72 74 20 64 6f 65  * first part doe
6ec50 73 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 74  s the start of t
6ec60 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 61 6e  he WHERE loop an
6ec70 64 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20  d the second.** 
6ec80 68 61 6c 66 20 64 6f 65 73 20 74 68 65 20 74 61  half does the ta
6ec90 69 6c 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  il of the WHERE 
6eca0 6c 6f 6f 70 2e 20 20 41 6e 20 69 6e 73 74 61 6e  loop.  An instan
6ecb0 63 65 20 6f 66 0a 2a 2a 20 74 68 69 73 20 73 74  ce of.** this st
6ecc0 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
6ecd0 6e 65 64 20 62 79 20 74 68 65 20 66 69 72 73 74  ned by the first
6ece0 20 68 61 6c 66 20 61 6e 64 20 70 61 73 73 65 64   half and passed
6ecf0 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73 65 63  .** into the sec
6ed00 6f 6e 64 20 68 61 6c 66 20 74 6f 20 67 69 76 65  ond half to give
6ed10 20 73 6f 6d 65 20 63 6f 6e 74 69 6e 75 69 74 79   some continuity
6ed20 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 57 68 65 72  ..*/.struct Wher
6ed30 65 49 6e 66 6f 20 7b 0a 20 20 50 61 72 73 65 20  eInfo {.  Parse 
6ed40 2a 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 2f  *pParse;       /
6ed50 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
6ed60 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
6ed70 6e 74 65 78 74 20 2a 2f 0a 20 20 75 38 20 6f 6b  ntext */.  u8 ok
6ed80 4f 6e 65 50 61 73 73 3b 20 20 20 20 20 20 20 20  OnePass;        
6ed90 2f 2a 20 4f 6b 20 74 6f 20 75 73 65 20 6f 6e 65  /* Ok to use one
6eda0 2d 70 61 73 73 20 61 6c 67 6f 72 69 74 68 6d 20  -pass algorithm 
6edb0 66 6f 72 20 55 50 44 41 54 45 20 6f 72 20 44 45  for UPDATE or DE
6edc0 4c 45 54 45 20 2a 2f 0a 20 20 53 72 63 4c 69 73  LETE */.  SrcLis
6edd0 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20 2f  t *pTabList;   /
6ede0 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65 73  * List of tables
6edf0 20 69 6e 20 74 68 65 20 6a 6f 69 6e 20 2a 2f 0a   in the join */.
6ee00 20 20 69 6e 74 20 69 54 6f 70 3b 20 20 20 20 20    int iTop;     
6ee10 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 65         /* The ve
6ee20 72 79 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  ry beginning of 
6ee30 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70 20 2a  the WHERE loop *
6ee40 2f 0a 20 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75  /.  int iContinu
6ee50 65 3b 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  e;       /* Jump
6ee60 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75   here to continu
6ee70 65 20 77 69 74 68 20 6e 65 78 74 20 72 65 63 6f  e with next reco
6ee80 72 64 20 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65  rd */.  int iBre
6ee90 61 6b 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ak;          /* 
6eea0 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65  Jump here to bre
6eeb0 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 6c 6f  ak out of the lo
6eec0 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 6e 4c 65 76  op */.  int nLev
6eed0 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  el;          /* 
6eee0 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65 64  Number of nested
6eef0 20 6c 6f 6f 70 20 2a 2f 0a 20 20 73 71 6c 69 74   loop */.  sqlit
6ef00 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a  e3_index_info **
6ef10 61 70 49 6e 66 6f 3b 20 20 2f 2a 20 41 72 72 61  apInfo;  /* Arra
6ef20 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f  y of pointers to
6ef30 20 69 6e 64 65 78 20 69 6e 66 6f 20 73 74 72 75   index info stru
6ef40 63 74 75 72 65 73 20 2a 2f 0a 20 20 57 68 65 72  ctures */.  Wher
6ef50 65 4c 65 76 65 6c 20 61 5b 31 5d 3b 20 20 20 20  eLevel a[1];    
6ef60 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
6ef70 61 62 6f 75 74 20 65 61 63 68 20 6e 65 73 74 20  about each nest 
6ef80 6c 6f 6f 70 20 69 6e 20 74 68 65 20 57 48 45 52  loop in the WHER
6ef90 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41  E */.};../*.** A
6efa0 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 64 65 66   NameContext def
6efb0 69 6e 65 73 20 61 20 63 6f 6e 74 65 78 74 20 69  ines a context i
6efc0 6e 20 77 68 69 63 68 20 74 6f 20 72 65 73 6f 6c  n which to resol
6efd0 76 65 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c  ve table and col
6efe0 75 6d 6e 0a 2a 2a 20 6e 61 6d 65 73 2e 20 20 54  umn.** names.  T
6eff0 68 65 20 63 6f 6e 74 65 78 74 20 63 6f 6e 73 69  he context consi
6f000 73 74 73 20 6f 66 20 61 20 6c 69 73 74 20 6f 66  sts of a list of
6f010 20 74 61 62 6c 65 73 20 28 74 68 65 20 70 53 72   tables (the pSr
6f020 63 4c 69 73 74 29 20 66 69 65 6c 64 20 61 6e 64  cList) field and
6f030 0a 2a 2a 20 61 20 6c 69 73 74 20 6f 66 20 6e 61  .** a list of na
6f040 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 28  med expression (
6f050 70 45 4c 69 73 74 29 2e 20 20 54 68 65 20 6e 61  pEList).  The na
6f060 6d 65 64 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  med expression l
6f070 69 73 74 20 6d 61 79 0a 2a 2a 20 62 65 20 4e 55  ist may.** be NU
6f080 4c 4c 2e 20 20 54 68 65 20 70 53 72 63 20 63 6f  LL.  The pSrc co
6f090 72 72 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65  rresponds to the
6f0a0 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20   FROM clause of 
6f0b0 61 20 53 45 4c 45 43 54 20 6f 72 0a 2a 2a 20 74  a SELECT or.** t
6f0c0 6f 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  o the table bein
6f0d0 67 20 6f 70 65 72 61 74 65 64 20 6f 6e 20 62 79  g operated on by
6f0e0 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
6f0f0 20 6f 72 20 44 45 4c 45 54 45 2e 20 20 54 68 65   or DELETE.  The
6f100 0a 2a 2a 20 70 45 4c 69 73 74 20 63 6f 72 72 65  .** pEList corre
6f110 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 72 65  sponds to the re
6f120 73 75 6c 74 20 73 65 74 20 6f 66 20 61 20 53 45  sult set of a SE
6f130 4c 45 43 54 20 61 6e 64 20 69 73 20 4e 55 4c 4c  LECT and is NULL
6f140 20 66 6f 72 0a 2a 2a 20 6f 74 68 65 72 20 73 74   for.** other st
6f150 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20  atements..**.** 
6f160 4e 61 6d 65 43 6f 6e 74 65 78 74 73 20 63 61 6e  NameContexts can
6f170 20 62 65 20 6e 65 73 74 65 64 2e 20 20 57 68 65   be nested.  Whe
6f180 6e 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65  n resolving name
6f190 73 2c 20 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73  s, the inner-mos
6f1a0 74 20 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73  t .** context is
6f1b0 20 73 65 61 72 63 68 65 64 20 66 69 72 73 74 2e   searched first.
6f1c0 20 20 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73    If no match is
6f1d0 20 66 6f 75 6e 64 2c 20 74 68 65 20 6e 65 78 74   found, the next
6f1e0 20 6f 75 74 65 72 0a 2a 2a 20 63 6f 6e 74 65 78   outer.** contex
6f1f0 74 20 69 73 20 63 68 65 63 6b 65 64 2e 20 20 49  t is checked.  I
6f200 66 20 74 68 65 72 65 20 69 73 20 73 74 69 6c 6c  f there is still
6f210 20 6e 6f 20 6d 61 74 63 68 2c 20 74 68 65 20 6e   no match, the n
6f220 65 78 74 20 63 6f 6e 74 65 78 74 0a 2a 2a 20 69  ext context.** i
6f230 73 20 63 68 65 63 6b 65 64 2e 20 20 54 68 69 73  s checked.  This
6f240 20 70 72 6f 63 65 73 73 20 63 6f 6e 74 69 6e 75   process continu
6f250 65 73 20 75 6e 74 69 6c 20 65 69 74 68 65 72 20  es until either 
6f260 61 20 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64  a match is found
6f270 0a 2a 2a 20 6f 72 20 61 6c 6c 20 63 6f 6e 74 65  .** or all conte
6f280 78 74 73 20 61 72 65 20 63 68 65 63 6b 2e 20 20  xts are check.  
6f290 57 68 65 6e 20 61 20 6d 61 74 63 68 20 69 73 20  When a match is 
6f2a0 66 6f 75 6e 64 2c 20 74 68 65 20 6e 52 65 66 20  found, the nRef 
6f2b0 6d 65 6d 62 65 72 20 6f 66 0a 2a 2a 20 74 68 65  member of.** the
6f2c0 20 63 6f 6e 74 65 78 74 20 63 6f 6e 74 61 69 6e   context contain
6f2d0 69 6e 67 20 74 68 65 20 6d 61 74 63 68 20 69 73  ing the match is
6f2e0 20 69 6e 63 72 65 6d 65 6e 74 65 64 2e 20 0a 2a   incremented. .*
6f2f0 2a 0a 2a 2a 20 45 61 63 68 20 73 75 62 71 75 65  *.** Each subque
6f300 72 79 20 67 65 74 73 20 61 20 6e 65 77 20 4e 61  ry gets a new Na
6f310 6d 65 43 6f 6e 74 65 78 74 2e 20 20 54 68 65 20  meContext.  The 
6f320 70 4e 65 78 74 20 66 69 65 6c 64 20 70 6f 69 6e  pNext field poin
6f330 74 73 20 74 6f 20 74 68 65 0a 2a 2a 20 4e 61 6d  ts to the.** Nam
6f340 65 43 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20  eContext in the 
6f350 70 61 72 65 6e 74 20 71 75 65 72 79 2e 20 20 54  parent query.  T
6f360 68 75 73 20 74 68 65 20 70 72 6f 63 65 73 73 20  hus the process 
6f370 6f 66 20 73 63 61 6e 6e 69 6e 67 20 74 68 65 0a  of scanning the.
6f380 2a 2a 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 6c  ** NameContext l
6f390 69 73 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20  ist corresponds 
6f3a0 74 6f 20 73 65 61 72 63 68 69 6e 67 20 74 68 72  to searching thr
6f3b0 6f 75 67 68 20 73 75 63 63 65 73 73 69 76 65 6c  ough successivel
6f3c0 79 20 6f 75 74 65 72 0a 2a 2a 20 73 75 62 71 75  y outer.** subqu
6f3d0 65 72 69 65 73 20 6c 6f 6f 6b 69 6e 67 20 66 6f  eries looking fo
6f3e0 72 20 61 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74  r a match..*/.st
6f3f0 72 75 63 74 20 4e 61 6d 65 43 6f 6e 74 65 78 74  ruct NameContext
6f400 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72   {.  Parse *pPar
6f410 73 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  se;       /* The
6f420 20 70 61 72 73 65 72 20 2a 2f 0a 20 20 53 72 63   parser */.  Src
6f430 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 3b 20  List *pSrcList; 
6f440 20 20 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65    /* One or more
6f450 20 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20   tables used to 
6f460 72 65 73 6f 6c 76 65 20 6e 61 6d 65 73 20 2a 2f  resolve names */
6f470 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c  .  ExprList *pEL
6f480 69 73 74 3b 20 20 20 20 2f 2a 20 4f 70 74 69 6f  ist;    /* Optio
6f490 6e 61 6c 20 6c 69 73 74 20 6f 66 20 6e 61 6d 65  nal list of name
6f4a0 64 20 65 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f  d expressions */
6f4b0 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20  .  int nRef;    
6f4c0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
6f4d0 72 20 6f 66 20 6e 61 6d 65 73 20 72 65 73 6f 6c  r of names resol
6f4e0 76 65 64 20 62 79 20 74 68 69 73 20 63 6f 6e 74  ved by this cont
6f4f0 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ext */.  int nEr
6f500 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
6f510 20 4e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   Number of error
6f520 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 20 77 68  s encountered wh
6f530 69 6c 65 20 72 65 73 6f 6c 76 69 6e 67 20 6e 61  ile resolving na
6f540 6d 65 73 20 2a 2f 0a 20 20 75 38 20 61 6c 6c 6f  mes */.  u8 allo
6f550 77 41 67 67 3b 20 20 20 20 20 20 20 20 20 2f 2a  wAgg;         /*
6f560 20 41 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   Aggregate funct
6f570 69 6f 6e 73 20 61 6c 6c 6f 77 65 64 20 68 65 72  ions allowed her
6f580 65 20 2a 2f 0a 20 20 75 38 20 68 61 73 41 67 67  e */.  u8 hasAgg
6f590 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ;           /* T
6f5a0 72 75 65 20 69 66 20 61 67 67 72 65 67 61 74 65  rue if aggregate
6f5b0 73 20 61 72 65 20 73 65 65 6e 20 2a 2f 0a 20 20  s are seen */.  
6f5c0 75 38 20 69 73 43 68 65 63 6b 3b 20 20 20 20 20  u8 isCheck;     
6f5d0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
6f5e0 72 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20  resolving names 
6f5f0 69 6e 20 61 20 43 48 45 43 4b 20 63 6f 6e 73 74  in a CHECK const
6f600 72 61 69 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e  raint */.  int n
6f610 44 65 70 74 68 3b 20 20 20 20 20 20 20 20 20 20  Depth;          
6f620 2f 2a 20 44 65 70 74 68 20 6f 66 20 73 75 62 71  /* Depth of subq
6f630 75 65 72 79 20 72 65 63 75 72 73 69 6f 6e 2e 20  uery recursion. 
6f640 31 20 66 6f 72 20 6e 6f 20 72 65 63 75 72 73 69  1 for no recursi
6f650 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e 66 6f 20  on */.  AggInfo 
6f660 2a 70 41 67 67 49 6e 66 6f 3b 20 20 20 2f 2a 20  *pAggInfo;   /* 
6f670 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
6f680 74 20 61 67 67 72 65 67 61 74 65 73 20 61 74 20  t aggregates at 
6f690 74 68 69 73 20 6c 65 76 65 6c 20 2a 2f 0a 20 20  this level */.  
6f6a0 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 65  NameContext *pNe
6f6b0 78 74 3b 20 20 2f 2a 20 4e 65 78 74 20 6f 75 74  xt;  /* Next out
6f6c0 65 72 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 2e  er name context.
6f6d0 20 20 4e 55 4c 4c 20 66 6f 72 20 6f 75 74 65 72    NULL for outer
6f6e0 6d 6f 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  most */.};../*.*
6f6f0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
6f700 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
6f710 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e  tructure contain
6f720 73 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74 69 6f  s all informatio
6f730 6e 0a 2a 2a 20 6e 65 65 64 65 64 20 74 6f 20 67  n.** needed to g
6f740 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72  enerate code for
6f750 20 61 20 73 69 6e 67 6c 65 20 53 45 4c 45 43 54   a single SELECT
6f760 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
6f770 2a 20 6e 4c 69 6d 69 74 20 69 73 20 73 65 74 20  * nLimit is set 
6f780 74 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69  to -1 if there i
6f790 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c 61 75 73  s no LIMIT claus
6f7a0 65 2e 20 20 6e 4f 66 66 73 65 74 20 69 73 20 73  e.  nOffset is s
6f7b0 65 74 20 74 6f 20 30 2e 0a 2a 2a 20 49 66 20 74  et to 0..** If t
6f7c0 68 65 72 65 20 69 73 20 61 20 4c 49 4d 49 54 20  here is a LIMIT 
6f7d0 63 6c 61 75 73 65 2c 20 74 68 65 20 70 61 72 73  clause, the pars
6f7e0 65 72 20 73 65 74 73 20 6e 4c 69 6d 69 74 20 74  er sets nLimit t
6f7f0 6f 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  o the value of t
6f800 68 65 0a 2a 2a 20 6c 69 6d 69 74 20 61 6e 64 20  he.** limit and 
6f810 6e 4f 66 66 73 65 74 20 74 6f 20 74 68 65 20 76  nOffset to the v
6f820 61 6c 75 65 20 6f 66 20 74 68 65 20 6f 66 66 73  alue of the offs
6f830 65 74 20 28 6f 72 20 30 20 69 66 20 74 68 65 72  et (or 0 if ther
6f840 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 6f 66 66 73  e is not.** offs
6f850 65 74 29 2e 20 20 42 75 74 20 6c 61 74 65 72 20  et).  But later 
6f860 6f 6e 2c 20 6e 4c 69 6d 69 74 20 61 6e 64 20 6e  on, nLimit and n
6f870 4f 66 66 73 65 74 20 62 65 63 6f 6d 65 20 74 68  Offset become th
6f880 65 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  e memory locatio
6f890 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 56 44 42  ns.** in the VDB
6f8a0 45 20 74 68 61 74 20 72 65 63 6f 72 64 20 74 68  E that record th
6f8b0 65 20 6c 69 6d 69 74 20 61 6e 64 20 6f 66 66 73  e limit and offs
6f8c0 65 74 20 63 6f 75 6e 74 65 72 73 2e 0a 2a 2a 0a  et counters..**.
6f8d0 2a 2a 20 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  ** addrOpenEphm[
6f8e0 5d 20 65 6e 74 72 69 65 73 20 63 6f 6e 74 61 69  ] entries contai
6f8f0 6e 20 74 68 65 20 61 64 64 72 65 73 73 20 6f 66  n the address of
6f900 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
6f910 6c 20 6f 70 63 6f 64 65 73 2e 0a 2a 2a 20 54 68  l opcodes..** Th
6f920 65 73 65 20 61 64 64 72 65 73 73 65 73 20 6d 75  ese addresses mu
6f930 73 74 20 62 65 20 73 74 6f 72 65 64 20 73 6f 20  st be stored so 
6f940 74 68 61 74 20 77 65 20 63 61 6e 20 67 6f 20 62  that we can go b
6f950 61 63 6b 20 61 6e 64 20 66 69 6c 6c 20 69 6e 0a  ack and fill in.
6f960 2a 2a 20 74 68 65 20 50 34 5f 4b 45 59 49 4e 46  ** the P4_KEYINF
6f970 4f 20 61 6e 64 20 50 32 20 70 61 72 61 6d 65 74  O and P2 paramet
6f980 65 72 73 20 6c 61 74 65 72 2e 20 20 4e 65 69 74  ers later.  Neit
6f990 68 65 72 20 74 68 65 20 4b 65 79 49 6e 66 6f 20  her the KeyInfo 
6f9a0 6e 6f 72 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65  nor.** the numbe
6f9b0 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
6f9c0 50 32 20 63 61 6e 20 62 65 20 63 6f 6d 70 75 74  P2 can be comput
6f9d0 65 64 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ed at the same t
6f9e0 69 6d 65 0a 2a 2a 20 61 73 20 74 68 65 20 4f 50  ime.** as the OP
6f9f0 5f 4f 70 65 6e 45 70 68 6d 20 69 6e 73 74 72 75  _OpenEphm instru
6fa00 63 74 69 6f 6e 20 69 73 20 63 6f 64 65 64 20 62  ction is coded b
6fa10 65 63 61 75 73 65 20 6e 6f 74 0a 2a 2a 20 65 6e  ecause not.** en
6fa20 6f 75 67 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ough information
6fa30 20 61 62 6f 75 74 20 74 68 65 20 63 6f 6d 70 6f   about the compo
6fa40 75 6e 64 20 71 75 65 72 79 20 69 73 20 6b 6e 6f  und query is kno
6fa50 77 6e 20 61 74 20 74 68 61 74 20 70 6f 69 6e 74  wn at that point
6fa60 2e 0a 2a 2a 20 54 68 65 20 4b 65 79 49 6e 66 6f  ..** The KeyInfo
6fa70 20 66 6f 72 20 61 64 64 72 4f 70 65 6e 54 72 61   for addrOpenTra
6fa80 6e 5b 30 5d 20 61 6e 64 20 5b 31 5d 20 63 6f 6e  n[0] and [1] con
6fa90 74 61 69 6e 73 20 63 6f 6c 6c 61 74 69 6e 67 20  tains collating 
6faa0 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 66 6f 72  sequences.** for
6fab0 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e   the result set.
6fac0 20 20 54 68 65 20 4b 65 79 49 6e 66 6f 20 66 6f    The KeyInfo fo
6fad0 72 20 61 64 64 72 4f 70 65 6e 54 72 61 6e 5b 32  r addrOpenTran[2
6fae0 5d 20 63 6f 6e 74 61 69 6e 73 20 63 6f 6c 6c 61  ] contains colla
6faf0 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ting.** sequence
6fb00 73 20 66 6f 72 20 74 68 65 20 4f 52 44 45 52 20  s for the ORDER 
6fb10 42 59 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74  BY clause..*/.st
6fb20 72 75 63 74 20 53 65 6c 65 63 74 20 7b 0a 20 20  ruct Select {.  
6fb30 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
6fb40 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69  ;      /* The fi
6fb50 65 6c 64 73 20 6f 66 20 74 68 65 20 72 65 73 75  elds of the resu
6fb60 6c 74 20 2a 2f 0a 20 20 75 38 20 6f 70 3b 20 20  lt */.  u8 op;  
6fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
6fb80 2a 20 4f 6e 65 20 6f 66 3a 20 54 4b 5f 55 4e 49  * One of: TK_UNI
6fb90 4f 4e 20 54 4b 5f 41 4c 4c 20 54 4b 5f 49 4e 54  ON TK_ALL TK_INT
6fba0 45 52 53 45 43 54 20 54 4b 5f 45 58 43 45 50 54  ERSECT TK_EXCEPT
6fbb0 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66 69 6e   */.  char affin
6fbc0 69 74 79 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ity;         /* 
6fbd0 4d 61 6b 65 52 65 63 6f 72 64 20 77 69 74 68 20  MakeRecord with 
6fbe0 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 66 6f  this affinity fo
6fbf0 72 20 53 52 54 5f 53 65 74 20 2a 2f 0a 20 20 75  r SRT_Set */.  u
6fc00 31 36 20 73 65 6c 46 6c 61 67 73 3b 20 20 20 20  16 selFlags;    
6fc10 20 20 20 20 20 20 2f 2a 20 56 61 72 69 6f 75 73        /* Various
6fc20 20 53 46 5f 2a 20 76 61 6c 75 65 73 20 2a 2f 0a   SF_* values */.
6fc30 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b    SrcList *pSrc;
6fc40 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
6fc50 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
6fc60 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b 20 20   Expr *pWhere;  
6fc70 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 57          /* The W
6fc80 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20  HERE clause */. 
6fc90 20 45 78 70 72 4c 69 73 74 20 2a 70 47 72 6f 75   ExprList *pGrou
6fca0 70 42 79 3b 20 20 20 20 2f 2a 20 54 68 65 20 47  pBy;    /* The G
6fcb0 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 20 2a  ROUP BY clause *
6fcc0 2f 0a 20 20 45 78 70 72 20 2a 70 48 61 76 69 6e  /.  Expr *pHavin
6fcd0 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  g;         /* Th
6fce0 65 20 48 41 56 49 4e 47 20 63 6c 61 75 73 65 20  e HAVING clause 
6fcf0 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
6fd00 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20 54  OrderBy;    /* T
6fd10 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75  he ORDER BY clau
6fd20 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  se */.  Select *
6fd30 70 50 72 69 6f 72 3b 20 20 20 20 20 20 20 20 2f  pPrior;        /
6fd40 2a 20 50 72 69 6f 72 20 73 65 6c 65 63 74 20 69  * Prior select i
6fd50 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c  n a compound sel
6fd60 65 63 74 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  ect statement */
6fd70 0a 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 78 74  .  Select *pNext
6fd80 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  ;         /* Nex
6fd90 74 20 73 65 6c 65 63 74 20 74 6f 20 74 68 65 20  t select to the 
6fda0 6c 65 66 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75  left in a compou
6fdb0 6e 64 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  nd */.  Select *
6fdc0 70 52 69 67 68 74 6d 6f 73 74 3b 20 20 20 20 2f  pRightmost;    /
6fdd0 2a 20 52 69 67 68 74 2d 6d 6f 73 74 20 73 65 6c  * Right-most sel
6fde0 65 63 74 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e  ect in a compoun
6fdf0 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  d select stateme
6fe00 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  nt */.  Expr *pL
6fe10 69 6d 69 74 3b 20 20 20 20 20 20 20 20 20 20 2f  imit;          /
6fe20 2a 20 4c 49 4d 49 54 20 65 78 70 72 65 73 73 69  * LIMIT expressi
6fe30 6f 6e 2e 20 4e 55 4c 4c 20 6d 65 61 6e 73 20 6e  on. NULL means n
6fe40 6f 74 20 75 73 65 64 2e 20 2a 2f 0a 20 20 45 78  ot used. */.  Ex
6fe50 70 72 20 2a 70 4f 66 66 73 65 74 3b 20 20 20 20  pr *pOffset;    
6fe60 20 20 20 20 20 2f 2a 20 4f 46 46 53 45 54 20 65       /* OFFSET e
6fe70 78 70 72 65 73 73 69 6f 6e 2e 20 4e 55 4c 4c 20  xpression. NULL 
6fe80 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64 2e 20  means not used. 
6fe90 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c  */.  int iLimit,
6fea0 20 69 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 4d   iOffset;   /* M
6feb0 65 6d 6f 72 79 20 72 65 67 69 73 74 65 72 73 20  emory registers 
6fec0 68 6f 6c 64 69 6e 67 20 4c 49 4d 49 54 20 26 20  holding LIMIT & 
6fed0 4f 46 46 53 45 54 20 63 6f 75 6e 74 65 72 73 20  OFFSET counters 
6fee0 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f 70 65  */.  int addrOpe
6fef0 6e 45 70 68 6d 5b 33 5d 3b 20 20 20 2f 2a 20 4f  nEphm[3];   /* O
6ff00 50 5f 4f 70 65 6e 45 70 68 65 6d 20 6f 70 63 6f  P_OpenEphem opco
6ff10 64 65 73 20 72 65 6c 61 74 65 64 20 74 6f 20 74  des related to t
6ff20 68 69 73 20 73 65 6c 65 63 74 20 2a 2f 0a 7d 3b  his select */.};
6ff30 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20  ../*.** Allowed 
6ff40 76 61 6c 75 65 73 20 66 6f 72 20 53 65 6c 65 63  values for Selec
6ff50 74 2e 73 65 6c 46 6c 61 67 73 2e 20 20 54 68 65  t.selFlags.  The
6ff60 20 22 53 46 22 20 70 72 65 66 69 78 20 73 74 61   "SF" prefix sta
6ff70 6e 64 73 20 66 6f 72 0a 2a 2a 20 22 53 65 6c 65  nds for.** "Sele
6ff80 63 74 20 46 6c 61 67 22 2e 0a 2a 2f 0a 23 64 65  ct Flag"..*/.#de
6ff90 66 69 6e 65 20 53 46 5f 44 69 73 74 69 6e 63 74  fine SF_Distinct
6ffa0 20 20 20 20 20 20 20 20 30 78 30 30 30 31 20 20          0x0001  
6ffb0 2f 2a 20 4f 75 74 70 75 74 20 73 68 6f 75 6c 64  /* Output should
6ffc0 20 62 65 20 44 49 53 54 49 4e 43 54 20 2a 2f 0a   be DISTINCT */.
6ffd0 23 64 65 66 69 6e 65 20 53 46 5f 52 65 73 6f 6c  #define SF_Resol
6ffe0 76 65 64 20 20 20 20 20 20 20 20 30 78 30 30 30  ved        0x000
6fff0 32 20 20 2f 2a 20 49 64 65 6e 74 69 66 69 65 72  2  /* Identifier
70000 73 20 68 61 76 65 20 62 65 65 6e 20 72 65 73 6f  s have been reso
70010 6c 76 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  lved */.#define 
70020 53 46 5f 41 67 67 72 65 67 61 74 65 20 20 20 20  SF_Aggregate    
70030 20 20 20 30 78 30 30 30 34 20 20 2f 2a 20 43 6f     0x0004  /* Co
70040 6e 74 61 69 6e 73 20 61 67 67 72 65 67 61 74 65  ntains aggregate
70050 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 23 64   functions */.#d
70060 65 66 69 6e 65 20 53 46 5f 55 73 65 73 45 70 68  efine SF_UsesEph
70070 65 6d 65 72 61 6c 20 20 20 30 78 30 30 30 38 20  emeral   0x0008 
70080 20 2f 2a 20 55 73 65 73 20 74 68 65 20 4f 70 65   /* Uses the Ope
70090 6e 45 70 68 65 6d 65 72 61 6c 20 6f 70 63 6f 64  nEphemeral opcod
700a0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f  e */.#define SF_
700b0 45 78 70 61 6e 64 65 64 20 20 20 20 20 20 20 20  Expanded        
700c0 30 78 30 30 31 30 20 20 2f 2a 20 73 71 6c 69 74  0x0010  /* sqlit
700d0 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29  e3SelectExpand()
700e0 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68 69 73 20   called on this 
700f0 2a 2f 0a 23 64 65 66 69 6e 65 20 53 46 5f 48 61  */.#define SF_Ha
70100 73 54 79 70 65 49 6e 66 6f 20 20 20 20 20 30 78  sTypeInfo     0x
70110 30 30 32 30 20 20 2f 2a 20 46 52 4f 4d 20 73 75  0020  /* FROM su
70120 62 71 75 65 72 69 65 73 20 68 61 76 65 20 54 61  bqueries have Ta
70130 62 6c 65 20 6d 65 74 61 64 61 74 61 20 2a 2f 0a  ble metadata */.
70140 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75  ../*.** The resu
70150 6c 74 73 20 6f 66 20 61 20 73 65 6c 65 63 74 20  lts of a select 
70160 63 61 6e 20 62 65 20 64 69 73 74 72 69 62 75 74  can be distribut
70170 65 64 20 69 6e 20 73 65 76 65 72 61 6c 20 77 61  ed in several wa
70180 79 73 2e 20 20 54 68 65 0a 2a 2a 20 22 53 52 54  ys.  The.** "SRT
70190 22 20 70 72 65 66 69 78 20 6d 65 61 6e 73 20 22  " prefix means "
701a0 53 45 4c 45 43 54 20 52 65 73 75 6c 74 20 54 79  SELECT Result Ty
701b0 70 65 22 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  pe"..*/.#define 
701c0 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20 20 20 20  SRT_Union       
701d0 20 31 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73   1  /* Store res
701e0 75 6c 74 20 61 73 20 6b 65 79 73 20 69 6e 20 61  ult as keys in a
701f0 6e 20 69 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69  n index */.#defi
70200 6e 65 20 53 52 54 5f 45 78 63 65 70 74 20 20 20  ne SRT_Except   
70210 20 20 20 20 32 20 20 2f 2a 20 52 65 6d 6f 76 65      2  /* Remove
70220 20 72 65 73 75 6c 74 20 66 72 6f 6d 20 61 20 55   result from a U
70230 4e 49 4f 4e 20 69 6e 64 65 78 20 2a 2f 0a 23 64  NION index */.#d
70240 65 66 69 6e 65 20 53 52 54 5f 45 78 69 73 74 73  efine SRT_Exists
70250 20 20 20 20 20 20 20 33 20 20 2f 2a 20 53 74 6f         3  /* Sto
70260 72 65 20 31 20 69 66 20 74 68 65 20 72 65 73 75  re 1 if the resu
70270 6c 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20  lt is not empty 
70280 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f 44  */.#define SRT_D
70290 69 73 63 61 72 64 20 20 20 20 20 20 34 20 20 2f  iscard      4  /
702a0 2a 20 44 6f 20 6e 6f 74 20 73 61 76 65 20 74 68  * Do not save th
702b0 65 20 72 65 73 75 6c 74 73 20 61 6e 79 77 68 65  e results anywhe
702c0 72 65 20 2a 2f 0a 0a 2f 2a 20 54 68 65 20 4f 52  re */../* The OR
702d0 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 69 73  DER BY clause is
702e0 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 61 6c 6c   ignored for all
702f0 20 6f 66 20 74 68 65 20 61 62 6f 76 65 20 2a 2f   of the above */
70300 0a 23 64 65 66 69 6e 65 20 49 67 6e 6f 72 61 62  .#define Ignorab
70310 6c 65 4f 72 64 65 72 62 79 28 58 29 20 28 28 58  leOrderby(X) ((X
70320 2d 3e 65 44 65 73 74 29 3c 3d 53 52 54 5f 44 69  ->eDest)<=SRT_Di
70330 73 63 61 72 64 29 0a 0a 23 64 65 66 69 6e 65 20  scard)..#define 
70340 53 52 54 5f 4f 75 74 70 75 74 20 20 20 20 20 20  SRT_Output      
70350 20 35 20 20 2f 2a 20 4f 75 74 70 75 74 20 65 61   5  /* Output ea
70360 63 68 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74  ch row of result
70370 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
70380 4d 65 6d 20 20 20 20 20 20 20 20 20 20 36 20 20  Mem          6  
70390 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 20  /* Store result 
703a0 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c 6c  in a memory cell
703b0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 52 54 5f   */.#define SRT_
703c0 53 65 74 20 20 20 20 20 20 20 20 20 20 37 20 20  Set          7  
703d0 2f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73  /* Store results
703e0 20 61 73 20 6b 65 79 73 20 69 6e 20 61 6e 20 69   as keys in an i
703f0 6e 64 65 78 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ndex */.#define 
70400 53 52 54 5f 54 61 62 6c 65 20 20 20 20 20 20 20  SRT_Table       
70410 20 38 20 20 2f 2a 20 53 74 6f 72 65 20 72 65 73   8  /* Store res
70420 75 6c 74 20 61 73 20 64 61 74 61 20 77 69 74 68  ult as data with
70430 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 72 6f   an automatic ro
70440 77 69 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  wid */.#define S
70450 52 54 5f 45 70 68 65 6d 54 61 62 20 20 20 20 20  RT_EphemTab     
70460 39 20 20 2f 2a 20 43 72 65 61 74 65 20 74 72 61  9  /* Create tra
70470 6e 73 69 65 6e 74 20 74 61 62 20 61 6e 64 20 73  nsient tab and s
70480 74 6f 72 65 20 6c 69 6b 65 20 53 52 54 5f 54 61  tore like SRT_Ta
70490 62 6c 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53  ble */.#define S
704a0 52 54 5f 43 6f 72 6f 75 74 69 6e 65 20 20 20 31  RT_Coroutine   1
704b0 30 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  0  /* Generate a
704c0 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20 72   single row of r
704d0 65 73 75 6c 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  esult */../*.** 
704e0 41 20 73 74 72 75 63 74 75 72 65 20 75 73 65 64  A structure used
704f0 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65 20 74 68   to customize th
70500 65 20 62 65 68 61 76 69 6f 75 72 20 6f 66 20 73  e behaviour of s
70510 71 6c 69 74 65 33 53 65 6c 65 63 74 28 29 2e 20  qlite3Select(). 
70520 53 65 65 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20  See.** comments 
70530 61 62 6f 76 65 20 73 71 6c 69 74 65 33 53 65 6c  above sqlite3Sel
70540 65 63 74 28 29 20 66 6f 72 20 64 65 74 61 69 6c  ect() for detail
70550 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  s..*/.typedef st
70560 72 75 63 74 20 53 65 6c 65 63 74 44 65 73 74 20  ruct SelectDest 
70570 53 65 6c 65 63 74 44 65 73 74 3b 0a 73 74 72 75  SelectDest;.stru
70580 63 74 20 53 65 6c 65 63 74 44 65 73 74 20 7b 0a  ct SelectDest {.
70590 20 20 75 38 20 65 44 65 73 74 3b 20 20 20 20 20    u8 eDest;     
705a0 20 20 20 20 2f 2a 20 48 6f 77 20 74 6f 20 64 69      /* How to di
705b0 73 70 6f 73 65 20 6f 66 20 74 68 65 20 72 65 73  spose of the res
705c0 75 6c 74 73 20 2a 2f 0a 20 20 75 38 20 61 66 66  ults */.  u8 aff
705d0 69 6e 69 74 79 3b 20 20 20 20 20 20 2f 2a 20 41  inity;      /* A
705e0 66 66 69 6e 69 74 79 20 75 73 65 64 20 77 68 65  ffinity used whe
705f0 6e 20 65 44 65 73 74 3d 3d 53 52 54 5f 53 65 74  n eDest==SRT_Set
70600 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61 72 6d 3b   */.  int iParm;
70610 20 20 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72          /* A par
70620 61 6d 65 74 65 72 20 75 73 65 64 20 62 79 20 74  ameter used by t
70630 68 65 20 65 44 65 73 74 20 64 69 73 70 6f 73 61  he eDest disposa
70640 6c 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20 69 6e  l method */.  in
70650 74 20 69 4d 65 6d 3b 20 20 20 20 20 20 20 20 20  t iMem;         
70660 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74 65 72  /* Base register
70670 20 77 68 65 72 65 20 72 65 73 75 6c 74 73 20 61   where results a
70680 72 65 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20 20  re written */.  
70690 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20  int nMem;       
706a0 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 72    /* Number of r
706b0 65 67 69 73 74 65 72 73 20 61 6c 6c 6f 63 61 74  egisters allocat
706c0 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ed */.};../*.** 
706d0 41 6e 20 53 51 4c 20 70 61 72 73 65 72 20 63 6f  An SQL parser co
706e0 6e 74 65 78 74 2e 20 20 41 20 63 6f 70 79 20 6f  ntext.  A copy o
706f0 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  f this structure
70700 20 69 73 20 70 61 73 73 65 64 20 74 68 72 6f 75   is passed throu
70710 67 68 0a 2a 2a 20 74 68 65 20 70 61 72 73 65 72  gh.** the parser
70720 20 61 6e 64 20 64 6f 77 6e 20 69 6e 74 6f 20 61   and down into a
70730 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 61 63  ll the parser ac
70740 74 69 6f 6e 20 72 6f 75 74 69 6e 65 20 69 6e 20  tion routine in 
70750 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 63 61 72 72  order to.** carr
70760 79 20 61 72 6f 75 6e 64 20 69 6e 66 6f 72 6d 61  y around informa
70770 74 69 6f 6e 20 74 68 61 74 20 69 73 20 67 6c 6f  tion that is glo
70780 62 61 6c 20 74 6f 20 74 68 65 20 65 6e 74 69 72  bal to the entir
70790 65 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54  e parse..**.** T
707a0 68 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  he structure is 
707b0 64 69 76 69 64 65 64 20 69 6e 74 6f 20 74 77 6f  divided into two
707c0 20 70 61 72 74 73 2e 20 20 57 68 65 6e 20 74 68   parts.  When th
707d0 65 20 70 61 72 73 65 72 20 61 6e 64 20 63 6f 64  e parser and cod
707e0 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 20 63 61  e.** generate ca
707f0 6c 6c 20 74 68 65 6d 73 65 6c 76 65 73 20 72 65  ll themselves re
70800 63 75 72 73 69 76 65 6c 79 2c 20 74 68 65 20 66  cursively, the f
70810 69 72 73 74 20 70 61 72 74 20 6f 66 20 74 68 65  irst part of the
70820 20 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 73   structure.** is
70830 20 63 6f 6e 73 74 61 6e 74 20 62 75 74 20 74 68   constant but th
70840 65 20 73 65 63 6f 6e 64 20 70 61 72 74 20 69 73  e second part is
70850 20 72 65 73 65 74 20 61 74 20 74 68 65 20 62 65   reset at the be
70860 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65 6e 64 20  ginning and end 
70870 6f 66 0a 2a 2a 20 65 61 63 68 20 72 65 63 75 72  of.** each recur
70880 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  sion..**.** The 
70890 6e 54 61 62 6c 65 4c 6f 63 6b 20 61 6e 64 20 61  nTableLock and a
708a0 54 61 62 6c 65 4c 6f 63 6b 20 76 61 72 69 61 62  TableLock variab
708b0 6c 65 73 20 61 72 65 20 6f 6e 6c 79 20 75 73 65  les are only use
708c0 64 20 69 66 20 74 68 65 20 73 68 61 72 65 64 2d  d if the shared-
708d0 63 61 63 68 65 20 0a 2a 2a 20 66 65 61 74 75 72  cache .** featur
708e0 65 20 69 73 20 65 6e 61 62 6c 65 64 20 28 69 66  e is enabled (if
708f0 20 73 71 6c 69 74 65 33 54 73 64 28 29 2d 3e 75   sqlite3Tsd()->u
70900 73 65 53 68 61 72 65 64 44 61 74 61 20 69 73 20  seSharedData is 
70910 74 72 75 65 29 2e 20 54 68 65 79 20 61 72 65 0a  true). They are.
70920 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
70930 20 74 68 65 20 73 65 74 20 6f 66 20 74 61 62 6c   the set of tabl
70940 65 2d 6c 6f 63 6b 73 20 72 65 71 75 69 72 65 64  e-locks required
70950 20 62 79 20 74 68 65 20 73 74 61 74 65 6d 65 6e   by the statemen
70960 74 20 62 65 69 6e 67 0a 2a 2a 20 63 6f 6d 70 69  t being.** compi
70970 6c 65 64 2e 20 46 75 6e 63 74 69 6f 6e 20 73 71  led. Function sq
70980 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29  lite3TableLock()
70990 20 69 73 20 75 73 65 64 20 74 6f 20 61 64 64 20   is used to add 
709a0 65 6e 74 72 69 65 73 20 74 6f 20 74 68 65 0a 2a  entries to the.*
709b0 2a 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 72 75 63  * list..*/.struc
709c0 74 20 50 61 72 73 65 20 7b 0a 20 20 73 71 6c 69  t Parse {.  sqli
709d0 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
709e0 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 61 74   /* The main dat
709f0 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65 20  abase structure 
70a00 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20 20 20  */.  int rc;    
70a10 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
70a20 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 65 78  urn code from ex
70a30 65 63 75 74 69 6f 6e 20 2a 2f 0a 20 20 63 68 61  ecution */.  cha
70a40 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20 20  r *zErrMsg;     
70a50 20 20 2f 2a 20 41 6e 20 65 72 72 6f 72 20 6d 65    /* An error me
70a60 73 73 61 67 65 20 2a 2f 0a 20 20 56 64 62 65 20  ssage */.  Vdbe 
70a70 2a 70 56 64 62 65 3b 20 20 20 20 20 20 20 20 20  *pVdbe;         
70a80 2f 2a 20 41 6e 20 65 6e 67 69 6e 65 20 66 6f 72  /* An engine for
70a90 20 65 78 65 63 75 74 69 6e 67 20 64 61 74 61 62   executing datab
70aa0 61 73 65 20 62 79 74 65 63 6f 64 65 20 2a 2f 0a  ase bytecode */.
70ab0 20 20 75 38 20 63 6f 6c 4e 61 6d 65 73 53 65 74    u8 colNamesSet
70ac0 3b 20 20 20 20 20 20 2f 2a 20 54 52 55 45 20 61  ;      /* TRUE a
70ad0 66 74 65 72 20 4f 50 5f 43 6f 6c 75 6d 6e 4e 61  fter OP_ColumnNa
70ae0 6d 65 20 68 61 73 20 62 65 65 6e 20 69 73 73 75  me has been issu
70af0 65 64 20 74 6f 20 70 56 64 62 65 20 2a 2f 0a 20  ed to pVdbe */. 
70b00 20 75 38 20 6e 61 6d 65 43 6c 61 73 68 3b 20 20   u8 nameClash;  
70b10 20 20 20 20 20 20 2f 2a 20 41 20 70 65 72 6d 61        /* A perma
70b20 6e 65 6e 74 20 74 61 62 6c 65 20 6e 61 6d 65 20  nent table name 
70b30 63 6c 61 73 68 65 73 20 77 69 74 68 20 74 65 6d  clashes with tem
70b40 70 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f 0a  p table name */.
70b50 20 20 75 38 20 63 68 65 63 6b 53 63 68 65 6d 61    u8 checkSchema
70b60 3b 20 20 20 20 20 20 2f 2a 20 43 61 75 73 65 73  ;      /* Causes
70b70 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 63   schema cookie c
70b80 68 65 63 6b 20 61 66 74 65 72 20 61 6e 20 65 72  heck after an er
70b90 72 6f 72 20 2a 2f 0a 20 20 75 38 20 6e 65 73 74  ror */.  u8 nest
70ba0 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ed;           /*
70bb0 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73 74 65   Number of neste
70bc0 64 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 70  d calls to the p
70bd0 61 72 73 65 72 2f 63 6f 64 65 20 67 65 6e 65 72  arser/code gener
70be0 61 74 6f 72 20 2a 2f 0a 20 20 75 38 20 70 61 72  ator */.  u8 par
70bf0 73 65 45 72 72 6f 72 3b 20 20 20 20 20 20 20 2f  seError;       /
70c00 2a 20 54 72 75 65 20 61 66 74 65 72 20 61 20 70  * True after a p
70c10 61 72 73 69 6e 67 20 65 72 72 6f 72 2e 20 20 54  arsing error.  T
70c20 69 63 6b 65 74 20 23 31 37 39 34 20 2a 2f 0a 20  icket #1794 */. 
70c30 20 75 38 20 6e 54 65 6d 70 52 65 67 3b 20 20 20   u8 nTempReg;   
70c40 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
70c50 6f 66 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67  of temporary reg
70c60 69 73 74 65 72 73 20 69 6e 20 61 54 65 6d 70 52  isters in aTempR
70c70 65 67 5b 5d 20 2a 2f 0a 20 20 75 38 20 6e 54 65  eg[] */.  u8 nTe
70c80 6d 70 49 6e 55 73 65 3b 20 20 20 20 20 20 20 2f  mpInUse;       /
70c90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 54 65 6d  * Number of aTem
70ca0 70 52 65 67 5b 5d 20 63 75 72 72 65 6e 74 6c 79  pReg[] currently
70cb0 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f 0a   checked out */.
70cc0 20 20 69 6e 74 20 61 54 65 6d 70 52 65 67 5b 38    int aTempReg[8
70cd0 5d 3b 20 20 20 20 20 2f 2a 20 48 6f 6c 64 69 6e  ];     /* Holdin
70ce0 67 20 61 72 65 61 20 66 6f 72 20 74 65 6d 70 6f  g area for tempo
70cf0 72 61 72 79 20 72 65 67 69 73 74 65 72 73 20 2a  rary registers *
70d00 2f 0a 20 20 69 6e 74 20 6e 52 61 6e 67 65 52 65  /.  int nRangeRe
70d10 67 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65  g;       /* Size
70d20 20 6f 66 20 74 68 65 20 74 65 6d 70 6f 72 61 72   of the temporar
70d30 79 20 72 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  y register block
70d40 20 2a 2f 0a 20 20 69 6e 74 20 69 52 61 6e 67 65   */.  int iRange
70d50 52 65 67 3b 20 20 20 20 20 20 20 2f 2a 20 46 69  Reg;       /* Fi
70d60 72 73 74 20 72 65 67 69 73 74 65 72 20 69 6e 20  rst register in 
70d70 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74  temporary regist
70d80 65 72 20 62 6c 6f 63 6b 20 2a 2f 0a 20 20 69 6e  er block */.  in
70d90 74 20 6e 45 72 72 3b 20 20 20 20 20 20 20 20 20  t nErr;         
70da0 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
70db0 65 72 72 6f 72 73 20 73 65 65 6e 20 2a 2f 0a 20  errors seen */. 
70dc0 20 69 6e 74 20 6e 54 61 62 3b 20 20 20 20 20 20   int nTab;      
70dd0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
70de0 6f 66 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c  of previously al
70df0 6c 6f 63 61 74 65 64 20 56 44 42 45 20 63 75 72  located VDBE cur
70e00 73 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d  sors */.  int nM
70e10 65 6d 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  em;            /
70e20 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65 6d 6f  * Number of memo
70e30 72 79 20 63 65 6c 6c 73 20 75 73 65 64 20 73 6f  ry cells used so
70e40 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 53   far */.  int nS
70e50 65 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  et;            /
70e60 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  * Number of sets
70e70 20 75 73 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a   used so far */.
70e80 20 20 69 6e 74 20 63 6b 42 61 73 65 3b 20 20 20    int ckBase;   
70e90 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
70ea0 65 67 69 73 74 65 72 20 6f 66 20 64 61 74 61 20  egister of data 
70eb0 64 75 72 69 6e 67 20 63 68 65 63 6b 20 63 6f 6e  during check con
70ec0 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 69 6e  straints */.  in
70ed0 74 20 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  t disableColCach
70ee0 65 3b 20 2f 2a 20 54 72 75 65 20 74 6f 20 64 69  e; /* True to di
70ef0 73 61 62 6c 65 20 61 64 64 69 6e 67 20 74 6f 20  sable adding to 
70f00 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20 2a 2f 0a  column cache */.
70f10 20 20 69 6e 74 20 6e 43 6f 6c 43 61 63 68 65 3b    int nColCache;
70f20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
70f30 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74   of entries in t
70f40 68 65 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65 20  he column cache 
70f50 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 43 61 63  */.  int iColCac
70f60 68 65 3b 20 20 20 20 20 20 20 2f 2a 20 4e 65 78  he;       /* Nex
70f70 74 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 63  t entry of the c
70f80 61 63 68 65 20 74 6f 20 72 65 70 6c 61 63 65 20  ache to replace 
70f90 2a 2f 0a 20 20 73 74 72 75 63 74 20 79 43 6f 6c  */.  struct yCol
70fa0 43 61 63 68 65 20 7b 0a 20 20 20 20 69 6e 74 20  Cache {.    int 
70fb0 69 54 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20  iTable;         
70fc0 20 20 2f 2a 20 54 61 62 6c 65 20 63 75 72 73 6f    /* Table curso
70fd0 72 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20  r number */.    
70fe0 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20 20  int iColumn;    
70ff0 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 63        /* Table c
71000 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 2a 2f 0a  olumn number */.
71010 20 20 20 20 63 68 61 72 20 61 66 66 43 68 61 6e      char affChan
71020 67 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 72 75  ge;       /* Tru
71030 65 20 69 66 20 74 68 69 73 20 72 65 67 69 73 74  e if this regist
71040 65 72 20 68 61 73 20 68 61 64 20 61 6e 20 61 66  er has had an af
71050 66 69 6e 69 74 79 20 63 68 61 6e 67 65 20 2a 2f  finity change */
71060 0a 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20  .    int iReg;  
71070 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
71080 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 76  gister holding v
71090 61 6c 75 65 20 6f 66 20 74 68 69 73 20 63 6f 6c  alue of this col
710a0 75 6d 6e 20 2a 2f 0a 20 20 7d 20 61 43 6f 6c 43  umn */.  } aColC
710b0 61 63 68 65 5b 31 30 5d 3b 20 20 20 20 20 2f 2a  ache[10];     /*
710c0 20 4f 6e 65 20 66 6f 72 20 65 61 63 68 20 76 61   One for each va
710d0 6c 69 64 20 63 6f 6c 75 6d 6e 20 63 61 63 68 65  lid column cache
710e0 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 33 32 20   entry */.  u32 
710f0 77 72 69 74 65 4d 61 73 6b 3b 20 20 20 20 20 20  writeMask;      
71100 20 2f 2a 20 53 74 61 72 74 20 61 20 77 72 69 74   /* Start a writ
71110 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 6e  e transaction on
71120 20 74 68 65 73 65 20 64 61 74 61 62 61 73 65 73   these databases
71130 20 2a 2f 0a 20 20 75 33 32 20 63 6f 6f 6b 69 65   */.  u32 cookie
71140 4d 61 73 6b 3b 20 20 20 20 20 20 2f 2a 20 42 69  Mask;      /* Bi
71150 74 6d 61 73 6b 20 6f 66 20 73 63 68 65 6d 61 20  tmask of schema 
71160 76 65 72 69 66 69 65 64 20 64 61 74 61 62 61 73  verified databas
71170 65 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b  es */.  int cook
71180 69 65 47 6f 74 6f 3b 20 20 20 20 20 20 2f 2a 20  ieGoto;      /* 
71190 41 64 64 72 65 73 73 20 6f 66 20 4f 50 5f 47 6f  Address of OP_Go
711a0 74 6f 20 74 6f 20 63 6f 6f 6b 69 65 20 76 65 72  to to cookie ver
711b0 69 66 69 65 72 20 73 75 62 72 6f 75 74 69 6e 65  ifier subroutine
711c0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65   */.  int cookie
711d0 56 61 6c 75 65 5b 53 51 4c 49 54 45 5f 4d 41 58  Value[SQLITE_MAX
711e0 5f 41 54 54 41 43 48 45 44 2b 32 5d 3b 20 20 2f  _ATTACHED+2];  /
711f0 2a 20 56 61 6c 75 65 73 20 6f 66 20 63 6f 6f 6b  * Values of cook
71200 69 65 73 20 74 6f 20 76 65 72 69 66 79 20 2a 2f  ies to verify */
71210 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
71220 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48  OMIT_SHARED_CACH
71230 45 0a 20 20 69 6e 74 20 6e 54 61 62 6c 65 4c 6f  E.  int nTableLo
71240 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ck;        /* Nu
71250 6d 62 65 72 20 6f 66 20 6c 6f 63 6b 73 20 69 6e  mber of locks in
71260 20 61 54 61 62 6c 65 4c 6f 63 6b 20 2a 2f 0a 20   aTableLock */. 
71270 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 61 54 61 62   TableLock *aTab
71280 6c 65 4c 6f 63 6b 3b 20 2f 2a 20 52 65 71 75 69  leLock; /* Requi
71290 72 65 64 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20  red table locks 
712a0 66 6f 72 20 73 68 61 72 65 64 2d 63 61 63 68 65  for shared-cache
712b0 20 6d 6f 64 65 20 2a 2f 0a 23 65 6e 64 69 66 0a   mode */.#endif.
712c0 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20    int regRowid; 
712d0 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
712e0 65 72 20 68 6f 6c 64 69 6e 67 20 72 6f 77 69 64  er holding rowid
712f0 20 6f 66 20 43 52 45 41 54 45 20 54 41 42 4c 45   of CREATE TABLE
71300 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20   entry */.  int 
71310 72 65 67 52 6f 6f 74 3b 20 20 20 20 20 20 20 20  regRoot;        
71320 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
71330 64 69 6e 67 20 72 6f 6f 74 20 70 61 67 65 20 6e  ding root page n
71340 75 6d 62 65 72 20 66 6f 72 20 6e 65 77 20 6f 62  umber for new ob
71350 6a 65 63 74 73 20 2a 2f 0a 0a 20 20 2f 2a 20 41  jects */..  /* A
71360 62 6f 76 65 20 69 73 20 63 6f 6e 73 74 61 6e 74  bove is constant
71370 20 62 65 74 77 65 65 6e 20 72 65 63 75 72 73 69   between recursi
71380 6f 6e 73 2e 20 20 42 65 6c 6f 77 20 69 73 20 72  ons.  Below is r
71390 65 73 65 74 20 62 65 66 6f 72 65 20 61 6e 64 20  eset before and 
713a0 61 66 74 65 72 0a 20 20 2a 2a 20 65 61 63 68 20  after.  ** each 
713b0 72 65 63 75 72 73 69 6f 6e 20 2a 2f 0a 0a 20 20  recursion */..  
713c0 69 6e 74 20 6e 56 61 72 3b 20 20 20 20 20 20 20  int nVar;       
713d0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
713e0 66 20 27 3f 27 20 76 61 72 69 61 62 6c 65 73 20  f '?' variables 
713f0 73 65 65 6e 20 69 6e 20 74 68 65 20 53 51 4c 20  seen in the SQL 
71400 73 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20  so far */.  int 
71410 6e 56 61 72 45 78 70 72 3b 20 20 20 20 20 20 20  nVarExpr;       
71420 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 73   /* Number of us
71430 65 64 20 73 6c 6f 74 73 20 69 6e 20 61 70 56 61  ed slots in apVa
71440 72 45 78 70 72 5b 5d 20 2a 2f 0a 20 20 69 6e 74  rExpr[] */.  int
71450 20 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3b 20   nVarExprAlloc; 
71460 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61    /* Number of a
71470 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20 69  llocated slots i
71480 6e 20 61 70 56 61 72 45 78 70 72 5b 5d 20 2a 2f  n apVarExpr[] */
71490 0a 20 20 45 78 70 72 20 2a 2a 61 70 56 61 72 45  .  Expr **apVarE
714a0 78 70 72 3b 20 20 20 20 2f 2a 20 50 6f 69 6e 74  xpr;    /* Point
714b0 65 72 73 20 74 6f 20 3a 61 61 61 20 61 6e 64 20  ers to :aaa and 
714c0 24 61 61 61 61 20 77 69 6c 64 63 61 72 64 20 65  $aaaa wildcard e
714d0 78 70 72 65 73 73 69 6f 6e 73 20 2a 2f 0a 20 20  xpressions */.  
714e0 69 6e 74 20 6e 41 6c 69 61 73 3b 20 20 20 20 20  int nAlias;     
714f0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
71500 66 20 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74  f aliased result
71510 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a   set columns */.
71520 20 20 69 6e 74 20 2a 61 41 6c 69 61 73 3b 20 20    int *aAlias;  
71530 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
71540 65 72 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  er used to hold 
71550 61 6c 69 61 73 65 64 20 72 65 73 75 6c 74 20 2a  aliased result *
71560 2f 0a 20 20 75 38 20 65 78 70 6c 61 69 6e 3b 20  /.  u8 explain; 
71570 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
71580 20 69 66 20 74 68 65 20 45 58 50 4c 41 49 4e 20   if the EXPLAIN 
71590 66 6c 61 67 20 69 73 20 66 6f 75 6e 64 20 6f 6e  flag is found on
715a0 20 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20   the query */.  
715b0 54 6f 6b 65 6e 20 73 45 72 72 54 6f 6b 65 6e 3b  Token sErrToken;
715c0 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65       /* The toke
715d0 6e 20 61 74 20 77 68 69 63 68 20 74 68 65 20 65  n at which the e
715e0 72 72 6f 72 20 6f 63 63 75 72 72 65 64 20 2a 2f  rror occurred */
715f0 0a 20 20 54 6f 6b 65 6e 20 73 4e 61 6d 65 54 6f  .  Token sNameTo
71600 6b 65 6e 3b 20 20 20 20 2f 2a 20 54 6f 6b 65 6e  ken;    /* Token
71610 20 77 69 74 68 20 75 6e 71 75 61 6c 69 66 69 65   with unqualifie
71620 64 20 73 63 68 65 6d 61 20 6f 62 6a 65 63 74 20  d schema object 
71630 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  name */.  Token 
71640 73 4c 61 73 74 54 6f 6b 65 6e 3b 20 20 20 20 2f  sLastToken;    /
71650 2a 20 54 68 65 20 6c 61 73 74 20 74 6f 6b 65 6e  * The last token
71660 20 70 61 72 73 65 64 20 2a 2f 0a 20 20 63 6f 6e   parsed */.  con
71670 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20 20  st char *zSql;  
71680 20 20 2f 2a 20 41 6c 6c 20 53 51 4c 20 74 65 78    /* All SQL tex
71690 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  t */.  const cha
716a0 72 20 2a 7a 54 61 69 6c 3b 20 20 20 2f 2a 20 41  r *zTail;   /* A
716b0 6c 6c 20 53 51 4c 20 74 65 78 74 20 70 61 73 74  ll SQL text past
716c0 20 74 68 65 20 6c 61 73 74 20 73 65 6d 69 63 6f   the last semico
716d0 6c 6f 6e 20 70 61 72 73 65 64 20 2a 2f 0a 20 20  lon parsed */.  
716e0 54 61 62 6c 65 20 2a 70 4e 65 77 54 61 62 6c 65  Table *pNewTable
716f0 3b 20 20 20 20 2f 2a 20 41 20 74 61 62 6c 65 20  ;    /* A table 
71700 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63 74 65  being constructe
71710 64 20 62 79 20 43 52 45 41 54 45 20 54 41 42 4c  d by CREATE TABL
71720 45 20 2a 2f 0a 20 20 54 72 69 67 67 65 72 20 2a  E */.  Trigger *
71730 70 4e 65 77 54 72 69 67 67 65 72 3b 20 20 20 20  pNewTrigger;    
71740 20 2f 2a 20 54 72 69 67 67 65 72 20 75 6e 64 65   /* Trigger unde
71750 72 20 63 6f 6e 73 74 72 75 63 74 20 62 79 20 61  r construct by a
71760 20 43 52 45 41 54 45 20 54 52 49 47 47 45 52 20   CREATE TRIGGER 
71770 2a 2f 0a 20 20 54 72 69 67 67 65 72 53 74 61 63  */.  TriggerStac
71780 6b 20 2a 74 72 69 67 53 74 61 63 6b 3b 20 20 2f  k *trigStack;  /
71790 2a 20 54 72 69 67 67 65 72 20 61 63 74 69 6f 6e  * Trigger action
717a0 73 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f  s being coded */
717b0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
717c0 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 2f 2a 20  AuthContext; /* 
717d0 54 68 65 20 36 74 68 20 70 61 72 61 6d 65 74 65  The 6th paramete
717e0 72 20 74 6f 20 64 62 2d 3e 78 41 75 74 68 20 63  r to db->xAuth c
717f0 61 6c 6c 62 61 63 6b 73 20 2a 2f 0a 23 69 66 6e  allbacks */.#ifn
71800 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
71810 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 54  VIRTUALTABLE.  T
71820 6f 6b 65 6e 20 73 41 72 67 3b 20 20 20 20 20 20  oken sArg;      
71830 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d            /* Com
71840 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20 61 20  plete text of a 
71850 6d 6f 64 75 6c 65 20 61 72 67 75 6d 65 6e 74 20  module argument 
71860 2a 2f 0a 20 20 75 38 20 64 65 63 6c 61 72 65 56  */.  u8 declareV
71870 74 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  tab;            
71880 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 73 69 64  /* True if insid
71890 65 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  e sqlite3_declar
718a0 65 5f 76 74 61 62 28 29 20 2a 2f 0a 20 20 69 6e  e_vtab() */.  in
718b0 74 20 6e 56 74 61 62 4c 6f 63 6b 3b 20 20 20 20  t nVtabLock;    
718c0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
718d0 65 72 20 6f 66 20 76 69 72 74 75 61 6c 20 74 61  er of virtual ta
718e0 62 6c 65 73 20 74 6f 20 6c 6f 63 6b 20 2a 2f 0a  bles to lock */.
718f0 20 20 54 61 62 6c 65 20 2a 2a 61 70 56 74 61 62    Table **apVtab
71900 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
71910 50 6f 69 6e 74 65 72 20 74 6f 20 76 69 72 74 75  Pointer to virtu
71920 61 6c 20 74 61 62 6c 65 73 20 6e 65 65 64 69 6e  al tables needin
71930 67 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 23 65 6e  g locking */.#en
71940 64 69 66 0a 20 20 69 6e 74 20 6e 48 65 69 67 68  dif.  int nHeigh
71950 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
71960 20 45 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65   Expression tree
71970 20 68 65 69 67 68 74 20 6f 66 20 63 75 72 72 65   height of curre
71980 6e 74 20 73 75 62 2d 73 65 6c 65 63 74 20 2a 2f  nt sub-select */
71990 0a 20 20 54 61 62 6c 65 20 2a 70 5a 6f 6d 62 69  .  Table *pZombi
719a0 65 54 61 62 3b 20 20 20 20 20 20 2f 2a 20 4c 69  eTab;      /* Li
719b0 73 74 20 6f 66 20 54 61 62 6c 65 20 6f 62 6a 65  st of Table obje
719c0 63 74 73 20 74 6f 20 64 65 6c 65 74 65 20 61 66  cts to delete af
719d0 74 65 72 20 63 6f 64 65 20 67 65 6e 20 2a 2f 0a  ter code gen */.
719e0 7d 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  };..#ifdef SQLIT
719f0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
71a00 42 4c 45 0a 20 20 23 64 65 66 69 6e 65 20 49 4e  BLE.  #define IN
71a10 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 30 0a  _DECLARE_VTAB 0.
71a20 23 65 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20  #else.  #define 
71a30 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20  IN_DECLARE_VTAB 
71a40 28 70 50 61 72 73 65 2d 3e 64 65 63 6c 61 72 65  (pParse->declare
71a50 56 74 61 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  Vtab).#endif../*
71a60 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20  .** An instance 
71a70 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
71a80 20 73 74 72 75 63 74 75 72 65 20 63 61 6e 20 62   structure can b
71a90 65 20 64 65 63 6c 61 72 65 64 20 6f 6e 20 61 20  e declared on a 
71aa0 73 74 61 63 6b 20 61 6e 64 20 75 73 65 64 0a 2a  stack and used.*
71ab0 2a 20 74 6f 20 73 61 76 65 20 74 68 65 20 50 61  * to save the Pa
71ac0 72 73 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74  rse.zAuthContext
71ad0 20 76 61 6c 75 65 20 73 6f 20 74 68 61 74 20 69   value so that i
71ae0 74 20 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65  t can be restore
71af0 64 20 6c 61 74 65 72 2e 0a 2a 2f 0a 73 74 72 75  d later..*/.stru
71b00 63 74 20 41 75 74 68 43 6f 6e 74 65 78 74 20 7b  ct AuthContext {
71b10 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
71b20 41 75 74 68 43 6f 6e 74 65 78 74 3b 20 20 20 2f  AuthContext;   /
71b30 2a 20 50 75 74 20 73 61 76 65 64 20 50 61 72 73  * Put saved Pars
71b40 65 2e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 68  e.zAuthContext h
71b50 65 72 65 20 2a 2f 0a 20 20 50 61 72 73 65 20 2a  ere */.  Parse *
71b60 70 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20  pParse;         
71b70 20 20 20 20 20 2f 2a 20 54 68 65 20 50 61 72 73       /* The Pars
71b80 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 7d  e structure */.}
71b90 3b 0a 0a 2f 2a 0a 2a 2a 20 42 69 74 66 69 65 6c  ;../*.** Bitfiel
71ba0 64 20 66 6c 61 67 73 20 66 6f 72 20 50 32 20 76  d flags for P2 v
71bb0 61 6c 75 65 20 69 6e 20 4f 50 5f 49 6e 73 65 72  alue in OP_Inser
71bc0 74 20 61 6e 64 20 4f 50 5f 44 65 6c 65 74 65 0a  t and OP_Delete.
71bd0 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41  */.#define OPFLA
71be0 47 5f 4e 43 48 41 4e 47 45 20 20 20 31 20 20 20  G_NCHANGE   1   
71bf0 20 2f 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74   /* Set to updat
71c00 65 20 64 62 2d 3e 6e 43 68 61 6e 67 65 20 2a 2f  e db->nChange */
71c10 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
71c20 4c 41 53 54 52 4f 57 49 44 20 32 20 20 20 20 2f  LASTROWID 2    /
71c30 2a 20 53 65 74 20 74 6f 20 75 70 64 61 74 65 20  * Set to update 
71c40 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 2a 2f  db->lastRowid */
71c50 0a 23 64 65 66 69 6e 65 20 4f 50 46 4c 41 47 5f  .#define OPFLAG_
71c60 49 53 55 50 44 41 54 45 20 20 34 20 20 20 20 2f  ISUPDATE  4    /
71c70 2a 20 54 68 69 73 20 4f 50 5f 49 6e 73 65 72 74  * This OP_Insert
71c80 20 69 73 20 61 6e 20 73 71 6c 20 55 50 44 41 54   is an sql UPDAT
71c90 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4f 50 46  E */.#define OPF
71ca0 4c 41 47 5f 41 50 50 45 4e 44 20 20 20 20 38 20  LAG_APPEND    8 
71cb0 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 6c 69     /* This is li
71cc0 6b 65 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70  kely to be an ap
71cd0 70 65 6e 64 20 2a 2f 0a 0a 2f 2a 0a 20 2a 20 45  pend */../*. * E
71ce0 61 63 68 20 74 72 69 67 67 65 72 20 70 72 65 73  ach trigger pres
71cf0 65 6e 74 20 69 6e 20 74 68 65 20 64 61 74 61 62  ent in the datab
71d00 61 73 65 20 73 63 68 65 6d 61 20 69 73 20 73 74  ase schema is st
71d10 6f 72 65 64 20 61 73 20 61 6e 20 69 6e 73 74 61  ored as an insta
71d20 6e 63 65 20 6f 66 0a 20 2a 20 73 74 72 75 63 74  nce of. * struct
71d30 20 54 72 69 67 67 65 72 2e 20 0a 20 2a 0a 20 2a   Trigger. . *. *
71d40 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 69 6e 73   Pointers to ins
71d50 74 61 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74  tances of struct
71d60 20 54 72 69 67 67 65 72 20 61 72 65 20 73 74 6f   Trigger are sto
71d70 72 65 64 20 69 6e 20 74 77 6f 20 77 61 79 73 2e  red in two ways.
71d80 0a 20 2a 20 31 2e 20 49 6e 20 74 68 65 20 22 74  . * 1. In the "t
71d90 72 69 67 48 61 73 68 22 20 68 61 73 68 20 74 61  rigHash" hash ta
71da0 62 6c 65 20 28 70 61 72 74 20 6f 66 20 74 68 65  ble (part of the
71db0 20 73 71 6c 69 74 65 33 2a 20 74 68 61 74 20 72   sqlite3* that r
71dc0 65 70 72 65 73 65 6e 74 73 20 74 68 65 20 0a 20  epresents the . 
71dd0 2a 20 20 20 20 64 61 74 61 62 61 73 65 29 2e 20  *    database). 
71de0 54 68 69 73 20 61 6c 6c 6f 77 73 20 54 72 69 67  This allows Trig
71df0 67 65 72 20 73 74 72 75 63 74 75 72 65 73 20 74  ger structures t
71e00 6f 20 62 65 20 72 65 74 72 69 65 76 65 64 20 62  o be retrieved b
71e10 79 20 6e 61 6d 65 2e 0a 20 2a 20 32 2e 20 41 6c  y name.. * 2. Al
71e20 6c 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63  l triggers assoc
71e30 69 61 74 65 64 20 77 69 74 68 20 61 20 73 69 6e  iated with a sin
71e40 67 6c 65 20 74 61 62 6c 65 20 66 6f 72 6d 20 61  gle table form a
71e50 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2c 20 75 73   linked list, us
71e60 69 6e 67 20 74 68 65 0a 20 2a 20 20 20 20 70 4e  ing the. *    pN
71e70 65 78 74 20 6d 65 6d 62 65 72 20 6f 66 20 73 74  ext member of st
71e80 72 75 63 74 20 54 72 69 67 67 65 72 2e 20 41 20  ruct Trigger. A 
71e90 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 66  pointer to the f
71ea0 69 72 73 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20  irst element of 
71eb0 74 68 65 0a 20 2a 20 20 20 20 6c 69 6e 6b 65 64  the. *    linked
71ec0 20 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20   list is stored 
71ed0 61 73 20 74 68 65 20 22 70 54 72 69 67 67 65 72  as the "pTrigger
71ee0 22 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20  " member of the 
71ef0 61 73 73 6f 63 69 61 74 65 64 0a 20 2a 20 20 20  associated. *   
71f00 20 73 74 72 75 63 74 20 54 61 62 6c 65 2e 0a 20   struct Table.. 
71f10 2a 0a 20 2a 20 54 68 65 20 22 73 74 65 70 5f 6c  *. * The "step_l
71f20 69 73 74 22 20 6d 65 6d 62 65 72 20 70 6f 69 6e  ist" member poin
71f30 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ts to the first 
71f40 65 6c 65 6d 65 6e 74 20 6f 66 20 61 20 6c 69 6e  element of a lin
71f50 6b 65 64 20 6c 69 73 74 0a 20 2a 20 63 6f 6e 74  ked list. * cont
71f60 61 69 6e 69 6e 67 20 74 68 65 20 53 51 4c 20 73  aining the SQL s
71f70 74 61 74 65 6d 65 6e 74 73 20 73 70 65 63 69 66  tatements specif
71f80 69 65 64 20 61 73 20 74 68 65 20 74 72 69 67 67  ied as the trigg
71f90 65 72 20 70 72 6f 67 72 61 6d 2e 0a 20 2a 2f 0a  er program.. */.
71fa0 73 74 72 75 63 74 20 54 72 69 67 67 65 72 20 7b  struct Trigger {
71fb0 0a 20 20 63 68 61 72 20 2a 6e 61 6d 65 3b 20 20  .  char *name;  
71fc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
71fd0 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 72  e name of the tr
71fe0 69 67 67 65 72 20 20 20 20 20 20 20 20 20 20 20  igger           
71ff0 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a               */.
72000 20 20 63 68 61 72 20 2a 74 61 62 6c 65 3b 20 20    char *table;  
72010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
72020 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 74   table or view t
72030 6f 20 77 68 69 63 68 20 74 68 65 20 74 72 69 67  o which the trig
72040 67 65 72 20 61 70 70 6c 69 65 73 20 2a 2f 0a 20  ger applies */. 
72050 20 75 38 20 6f 70 3b 20 20 20 20 20 20 20 20 20   u8 op;         
72060 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
72070 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b  of TK_DELETE, TK
72080 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
72090 52 54 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  RT         */.  
720a0 75 38 20 74 72 5f 74 6d 3b 20 20 20 20 20 20 20  u8 tr_tm;       
720b0 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f          /* One o
720c0 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52 45  f TRIGGER_BEFORE
720d0 2c 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 20  , TRIGGER_AFTER 
720e0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 6e  */.  Expr *pWhen
720f0 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
72100 54 68 65 20 57 48 45 4e 20 63 6c 61 75 73 65 20  The WHEN clause 
72110 6f 66 20 74 68 65 20 65 78 70 72 65 73 69 6f 6e  of the expresion
72120 20 28 6d 61 79 20 62 65 20 4e 55 4c 4c 29 20 2a   (may be NULL) *
72130 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43 6f 6c  /.  IdList *pCol
72140 75 6d 6e 73 3b 20 20 20 20 20 20 20 2f 2a 20 49  umns;       /* I
72150 66 20 74 68 69 73 20 69 73 20 61 6e 20 55 50 44  f this is an UPD
72160 41 54 45 20 4f 46 20 3c 63 6f 6c 75 6d 6e 2d 6c  ATE OF <column-l
72170 69 73 74 3e 20 74 72 69 67 67 65 72 2c 0a 20 20  ist> trigger,.  
72180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72190 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 3c             the <
721a0 63 6f 6c 75 6d 6e 2d 6c 69 73 74 3e 20 69 73 20  column-list> is 
721b0 73 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20  stored here */. 
721c0 20 54 6f 6b 65 6e 20 6e 61 6d 65 54 6f 6b 65 6e   Token nameToken
721d0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65  ;        /* Toke
721e0 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 7a 4e 61  n containing zNa
721f0 6d 65 2e 20 55 73 65 20 64 75 72 69 6e 67 20 70  me. Use during p
72200 61 72 73 69 6e 67 20 6f 6e 6c 79 20 2a 2f 0a 20  arsing only */. 
72210 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
72220 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 63 68 65  ;        /* Sche
72230 6d 61 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68  ma containing th
72240 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53  e trigger */.  S
72250 63 68 65 6d 61 20 2a 70 54 61 62 53 63 68 65 6d  chema *pTabSchem
72260 61 3b 20 20 20 20 20 2f 2a 20 53 63 68 65 6d 61  a;     /* Schema
72270 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
72280 74 61 62 6c 65 20 2a 2f 0a 20 20 54 72 69 67 67  table */.  Trigg
72290 65 72 53 74 65 70 20 2a 73 74 65 70 5f 6c 69 73  erStep *step_lis
722a0 74 3b 20 2f 2a 20 4c 69 6e 6b 20 6c 69 73 74 20  t; /* Link list 
722b0 6f 66 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  of trigger progr
722c0 61 6d 20 73 74 65 70 73 20 20 20 20 20 20 20 20  am steps        
722d0 20 20 20 20 20 2a 2f 0a 20 20 54 72 69 67 67 65       */.  Trigge
722e0 72 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  r *pNext;       
722f0 20 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65    /* Next trigge
72300 72 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  r associated wit
72310 68 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 7d  h the table */.}
72320 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 74 72 69 67 67  ;../*.** A trigg
72330 65 72 20 69 73 20 65 69 74 68 65 72 20 61 20 42  er is either a B
72340 45 46 4f 52 45 20 6f 72 20 61 6e 20 41 46 54 45  EFORE or an AFTE
72350 52 20 74 72 69 67 67 65 72 2e 20 20 54 68 65 20  R trigger.  The 
72360 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74 61  following consta
72370 6e 74 73 0a 2a 2a 20 64 65 74 65 72 6d 69 6e 65  nts.** determine
72380 20 77 68 69 63 68 2e 20 0a 2a 2a 0a 2a 2a 20 49   which. .**.** I
72390 66 20 74 68 65 72 65 20 61 72 65 20 6d 75 6c 74  f there are mult
723a0 69 70 6c 65 20 74 72 69 67 67 65 72 73 2c 20 79  iple triggers, y
723b0 6f 75 20 6d 69 67 68 74 20 6f 66 20 73 6f 6d 65  ou might of some
723c0 20 42 45 46 4f 52 45 20 61 6e 64 20 73 6f 6d 65   BEFORE and some
723d0 20 41 46 54 45 52 2e 0a 2a 2a 20 49 6e 20 74 68   AFTER..** In th
723e0 61 74 20 63 61 73 65 73 2c 20 74 68 65 20 63 6f  at cases, the co
723f0 6e 73 74 61 6e 74 73 20 62 65 6c 6f 77 20 63 61  nstants below ca
72400 6e 20 62 65 20 4f 52 65 64 20 74 6f 67 65 74 68  n be ORed togeth
72410 65 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54  er..*/.#define T
72420 52 49 47 47 45 52 5f 42 45 46 4f 52 45 20 20 31  RIGGER_BEFORE  1
72430 0a 23 64 65 66 69 6e 65 20 54 52 49 47 47 45 52  .#define TRIGGER
72440 5f 41 46 54 45 52 20 20 20 32 0a 0a 2f 2a 0a 20  _AFTER   2../*. 
72450 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
72460 20 73 74 72 75 63 74 20 54 72 69 67 67 65 72 53   struct TriggerS
72470 74 65 70 20 69 73 20 75 73 65 64 20 74 6f 20 73  tep is used to s
72480 74 6f 72 65 20 61 20 73 69 6e 67 6c 65 20 53 51  tore a single SQ
72490 4c 20 73 74 61 74 65 6d 65 6e 74 0a 20 2a 20 74  L statement. * t
724a0 68 61 74 20 69 73 20 61 20 70 61 72 74 20 6f 66  hat is a part of
724b0 20 61 20 74 72 69 67 67 65 72 2d 70 72 6f 67 72   a trigger-progr
724c0 61 6d 2e 20 0a 20 2a 0a 20 2a 20 49 6e 73 74 61  am. . *. * Insta
724d0 6e 63 65 73 20 6f 66 20 73 74 72 75 63 74 20 54  nces of struct T
724e0 72 69 67 67 65 72 53 74 65 70 20 61 72 65 20 73  riggerStep are s
724f0 74 6f 72 65 64 20 69 6e 20 61 20 73 69 6e 67 6c  tored in a singl
72500 79 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 28 6c  y linked list (l
72510 69 6e 6b 65 64 0a 20 2a 20 75 73 69 6e 67 20 74  inked. * using t
72520 68 65 20 22 70 4e 65 78 74 22 20 6d 65 6d 62 65  he "pNext" membe
72530 72 29 20 72 65 66 65 72 65 6e 63 65 64 20 62 79  r) referenced by
72540 20 74 68 65 20 22 73 74 65 70 5f 6c 69 73 74 22   the "step_list"
72550 20 6d 65 6d 62 65 72 20 6f 66 20 74 68 65 20 0a   member of the .
72560 20 2a 20 61 73 73 6f 63 69 61 74 65 64 20 73 74   * associated st
72570 72 75 63 74 20 54 72 69 67 67 65 72 20 69 6e 73  ruct Trigger ins
72580 74 61 6e 63 65 2e 20 54 68 65 20 66 69 72 73 74  tance. The first
72590 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
725a0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 0a 20  linked list is. 
725b0 2a 20 74 68 65 20 66 69 72 73 74 20 73 74 65 70  * the first step
725c0 20 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2d   of the trigger-
725d0 70 72 6f 67 72 61 6d 2e 0a 20 2a 20 0a 20 2a 20  program.. * . * 
725e0 54 68 65 20 22 6f 70 22 20 6d 65 6d 62 65 72 20  The "op" member 
725f0 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65  indicates whethe
72600 72 20 74 68 69 73 20 69 73 20 61 20 22 44 45 4c  r this is a "DEL
72610 45 54 45 22 2c 20 22 49 4e 53 45 52 54 22 2c 20  ETE", "INSERT", 
72620 22 55 50 44 41 54 45 22 20 6f 72 0a 20 2a 20 22  "UPDATE" or. * "
72630 53 45 4c 45 43 54 22 20 73 74 61 74 65 6d 65 6e  SELECT" statemen
72640 74 2e 20 54 68 65 20 6d 65 61 6e 69 6e 67 73 20  t. The meanings 
72650 6f 66 20 74 68 65 20 6f 74 68 65 72 20 6d 65 6d  of the other mem
72660 62 65 72 73 20 69 73 20 64 65 74 65 72 6d 69 6e  bers is determin
72670 65 64 20 62 79 20 74 68 65 20 0a 20 2a 20 76 61  ed by the . * va
72680 6c 75 65 20 6f 66 20 22 6f 70 22 20 61 73 20 66  lue of "op" as f
72690 6f 6c 6c 6f 77 73 3a 0a 20 2a 0a 20 2a 20 28 6f  ollows:. *. * (o
726a0 70 20 3d 3d 20 54 4b 5f 49 4e 53 45 52 54 29 0a  p == TK_INSERT).
726b0 20 2a 20 6f 72 63 6f 6e 66 20 20 20 20 2d 3e 20   * orconf    -> 
726c0 73 74 6f 72 65 73 20 74 68 65 20 4f 4e 20 43 4f  stores the ON CO
726d0 4e 46 4c 49 43 54 20 61 6c 67 6f 72 69 74 68 6d  NFLICT algorithm
726e0 0a 20 2a 20 70 53 65 6c 65 63 74 20 20 20 2d 3e  . * pSelect   ->
726f0 20 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 49   If this is an I
72700 4e 53 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 53  NSERT INTO ... S
72710 45 4c 45 43 54 20 2e 2e 2e 20 73 74 61 74 65 6d  ELECT ... statem
72720 65 6e 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20  ent, then. *    
72730 20 20 20 20 20 20 20 20 20 20 74 68 69 73 20 73            this s
72740 74 6f 72 65 73 20 61 20 70 6f 69 6e 74 65 72 20  tores a pointer 
72750 74 6f 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  to the SELECT st
72760 61 74 65 6d 65 6e 74 2e 20 4f 74 68 65 72 77 69  atement. Otherwi
72770 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 74 61 72 67  se NULL.. * targ
72780 65 74 20 20 20 20 2d 3e 20 41 20 74 6f 6b 65 6e  et    -> A token
72790 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 6e 61 6d   holding the nam
727a0 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74  e of the table t
727b0 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 2e 0a 20  o insert into.. 
727c0 2a 20 70 45 78 70 72 4c 69 73 74 20 2d 3e 20 49  * pExprList -> I
727d0 66 20 74 68 69 73 20 69 73 20 61 6e 20 49 4e 53  f this is an INS
727e0 45 52 54 20 49 4e 54 4f 20 2e 2e 2e 20 56 41 4c  ERT INTO ... VAL
727f0 55 45 53 20 2e 2e 2e 20 73 74 61 74 65 6d 65 6e  UES ... statemen
72800 74 2c 20 74 68 65 6e 0a 20 2a 20 20 20 20 20 20  t, then. *      
72810 20 20 20 20 20 20 20 20 74 68 69 73 20 73 74 6f          this sto
72820 72 65 73 20 76 61 6c 75 65 73 20 74 6f 20 62 65  res values to be
72830 20 69 6e 73 65 72 74 65 64 2e 20 4f 74 68 65 72   inserted. Other
72840 77 69 73 65 20 4e 55 4c 4c 2e 0a 20 2a 20 70 49  wise NULL.. * pI
72850 64 4c 69 73 74 20 20 20 2d 3e 20 49 66 20 74 68  dList   -> If th
72860 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
72870 49 4e 54 4f 20 2e 2e 2e 20 28 3c 63 6f 6c 75 6d  INTO ... (<colum
72880 6e 2d 6e 61 6d 65 73 3e 29 20 56 41 4c 55 45 53  n-names>) VALUES
72890 20 2e 2e 2e 20 0a 20 2a 20 20 20 20 20 20 20 20   ... . *        
728a0 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74 2c        statement,
728b0 20 74 68 65 6e 20 74 68 69 73 20 73 74 6f 72 65   then this store
728c0 73 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 6e 61 6d  s the column-nam
728d0 65 73 20 74 6f 20 62 65 0a 20 2a 20 20 20 20 20  es to be. *     
728e0 20 20 20 20 20 20 20 20 20 69 6e 73 65 72 74 65           inserte
728f0 64 20 69 6e 74 6f 2e 0a 20 2a 0a 20 2a 20 28 6f  d into.. *. * (o
72900 70 20 3d 3d 20 54 4b 5f 44 45 4c 45 54 45 29 0a  p == TK_DELETE).
72910 20 2a 20 74 61 72 67 65 74 20 20 20 20 2d 3e 20   * target    -> 
72920 41 20 74 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20  A token holding 
72930 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
72940 74 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 20  table to delete 
72950 66 72 6f 6d 2e 0a 20 2a 20 70 57 68 65 72 65 20  from.. * pWhere 
72960 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20     -> The WHERE 
72970 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 44 45  clause of the DE
72980 4c 45 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  LETE statement i
72990 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69  f one is specifi
729a0 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20  ed.. *          
729b0 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55      Otherwise NU
729c0 4c 4c 2e 0a 20 2a 20 0a 20 2a 20 28 6f 70 20 3d  LL.. * . * (op =
729d0 3d 20 54 4b 5f 55 50 44 41 54 45 29 0a 20 2a 20  = TK_UPDATE). * 
729e0 74 61 72 67 65 74 20 20 20 20 2d 3e 20 41 20 74  target    -> A t
729f0 6f 6b 65 6e 20 68 6f 6c 64 69 6e 67 20 74 68 65  oken holding the
72a00 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62   name of the tab
72a10 6c 65 20 74 6f 20 75 70 64 61 74 65 20 72 6f 77  le to update row
72a20 73 20 6f 66 2e 0a 20 2a 20 70 57 68 65 72 65 20  s of.. * pWhere 
72a30 20 20 20 2d 3e 20 54 68 65 20 57 48 45 52 45 20     -> The WHERE 
72a40 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 55 50  clause of the UP
72a50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 20 69  DATE statement i
72a60 66 20 6f 6e 65 20 69 73 20 73 70 65 63 69 66 69  f one is specifi
72a70 65 64 2e 0a 20 2a 20 20 20 20 20 20 20 20 20 20  ed.. *          
72a80 20 20 20 20 4f 74 68 65 72 77 69 73 65 20 4e 55      Otherwise NU
72a90 4c 4c 2e 0a 20 2a 20 70 45 78 70 72 4c 69 73 74  LL.. * pExprList
72aa0 20 2d 3e 20 41 20 6c 69 73 74 20 6f 66 20 74 68   -> A list of th
72ab0 65 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 75 70 64  e columns to upd
72ac0 61 74 65 20 61 6e 64 20 74 68 65 20 65 78 70 72  ate and the expr
72ad0 65 73 73 69 6f 6e 73 20 74 6f 20 75 70 64 61 74  essions to updat
72ae0 65 0a 20 2a 20 20 20 20 20 20 20 20 20 20 20 20  e. *            
72af0 20 20 74 68 65 6d 20 74 6f 2e 20 53 65 65 20 73    them to. See s
72b00 71 6c 69 74 65 33 55 70 64 61 74 65 28 29 20 64  qlite3Update() d
72b10 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  ocumentation of 
72b20 22 70 43 68 61 6e 67 65 73 22 0a 20 2a 20 20 20  "pChanges". *   
72b30 20 20 20 20 20 20 20 20 20 20 20 61 72 67 75 6d             argum
72b40 65 6e 74 2e 0a 20 2a 20 0a 20 2a 2f 0a 73 74 72  ent.. * . */.str
72b50 75 63 74 20 54 72 69 67 67 65 72 53 74 65 70 20  uct TriggerStep 
72b60 7b 0a 20 20 69 6e 74 20 6f 70 3b 20 20 20 20 20  {.  int op;     
72b70 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20           /* One 
72b80 6f 66 20 54 4b 5f 44 45 4c 45 54 45 2c 20 54 4b  of TK_DELETE, TK
72b90 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e 53 45  _UPDATE, TK_INSE
72ba0 52 54 2c 20 54 4b 5f 53 45 4c 45 43 54 20 2a 2f  RT, TK_SELECT */
72bb0 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 20 20  .  int orconf;  
72bc0 20 20 20 20 20 20 20 20 2f 2a 20 4f 45 5f 52 6f          /* OE_Ro
72bd0 6c 6c 62 61 63 6b 20 65 74 63 2e 20 2a 2f 0a 20  llback etc. */. 
72be0 20 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b   Trigger *pTrig;
72bf0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 69        /* The tri
72c00 67 67 65 72 20 74 68 61 74 20 74 68 69 73 20 73  gger that this s
72c10 74 65 70 20 69 73 20 61 20 70 61 72 74 20 6f 66  tep is a part of
72c20 20 2a 2f 0a 0a 20 20 53 65 6c 65 63 74 20 2a 70   */..  Select *p
72c30 53 65 6c 65 63 74 3b 20 20 20 20 20 2f 2a 20 56  Select;     /* V
72c40 61 6c 69 64 20 66 6f 72 20 53 45 4c 45 43 54 20  alid for SELECT 
72c50 61 6e 64 20 73 6f 6d 65 74 69 6d 65 73 20 0a 20  and sometimes . 
72c60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
72c70 20 20 20 20 20 20 20 20 20 49 4e 53 45 52 54 20           INSERT 
72c80 73 74 65 70 73 20 28 77 68 65 6e 20 70 45 78 70  steps (when pExp
72c90 72 4c 69 73 74 20 3d 3d 20 30 29 20 2a 2f 0a 20  rList == 0) */. 
72ca0 20 54 6f 6b 65 6e 20 74 61 72 67 65 74 3b 20 20   Token target;  
72cb0 20 20 20 20 20 20 2f 2a 20 56 61 6c 69 64 20 66        /* Valid f
72cc0 6f 72 20 44 45 4c 45 54 45 2c 20 55 50 44 41 54  or DELETE, UPDAT
72cd0 45 2c 20 49 4e 53 45 52 54 20 73 74 65 70 73 20  E, INSERT steps 
72ce0 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
72cf0 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c  e;        /* Val
72d00 69 64 20 66 6f 72 20 44 45 4c 45 54 45 2c 20 55  id for DELETE, U
72d10 50 44 41 54 45 20 73 74 65 70 73 20 2a 2f 0a 20  PDATE steps */. 
72d20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 78 70 72   ExprList *pExpr
72d30 4c 69 73 74 3b 20 2f 2a 20 56 61 6c 69 64 20 66  List; /* Valid f
72d40 6f 72 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  or UPDATE statem
72d50 65 6e 74 73 20 61 6e 64 20 73 6f 6d 65 74 69 6d  ents and sometim
72d60 65 73 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  es .            
72d70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 49                 I
72d80 4e 53 45 52 54 20 73 74 65 70 73 20 28 77 68 65  NSERT steps (whe
72d90 6e 20 70 53 65 6c 65 63 74 20 3d 3d 20 30 29 20  n pSelect == 0) 
72da0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 49 64 4c          */.  IdL
72db0 69 73 74 20 2a 70 49 64 4c 69 73 74 3b 20 20 20  ist *pIdList;   
72dc0 20 20 2f 2a 20 56 61 6c 69 64 20 66 6f 72 20 49    /* Valid for I
72dd0 4e 53 45 52 54 20 73 74 61 74 65 6d 65 6e 74 73  NSERT statements
72de0 20 6f 6e 6c 79 20 2a 2f 0a 20 20 54 72 69 67 67   only */.  Trigg
72df0 65 72 53 74 65 70 20 2a 70 4e 65 78 74 3b 20 20  erStep *pNext;  
72e00 2f 2a 20 4e 65 78 74 20 69 6e 20 74 68 65 20 6c  /* Next in the l
72e10 69 6e 6b 2d 6c 69 73 74 20 2a 2f 0a 20 20 54 72  ink-list */.  Tr
72e20 69 67 67 65 72 53 74 65 70 20 2a 70 4c 61 73 74  iggerStep *pLast
72e30 3b 20 20 2f 2a 20 4c 61 73 74 20 65 6c 65 6d 65  ;  /* Last eleme
72e40 6e 74 20 69 6e 20 6c 69 6e 6b 2d 6c 69 73 74 2e  nt in link-list.
72e50 20 56 61 6c 69 64 20 66 6f 72 20 31 73 74 20 65   Valid for 1st e
72e60 6c 65 6d 20 6f 6e 6c 79 20 2a 2f 0a 7d 3b 0a 0a  lem only */.};..
72e70 2f 2a 0a 20 2a 20 41 6e 20 69 6e 73 74 61 6e 63  /*. * An instanc
72e80 65 20 6f 66 20 73 74 72 75 63 74 20 54 72 69 67  e of struct Trig
72e90 67 65 72 53 74 61 63 6b 20 73 74 6f 72 65 73 20  gerStack stores 
72ea0 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 72 65 71 75  information requ
72eb0 69 72 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65  ired during code
72ec0 0a 20 2a 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f  . * generation o
72ed0 66 20 61 20 73 69 6e 67 6c 65 20 74 72 69 67 67  f a single trigg
72ee0 65 72 20 70 72 6f 67 72 61 6d 2e 20 57 68 69 6c  er program. Whil
72ef0 65 20 74 68 65 20 74 72 69 67 67 65 72 20 70 72  e the trigger pr
72f00 6f 67 72 61 6d 20 69 73 20 62 65 69 6e 67 0a 20  ogram is being. 
72f10 2a 20 63 6f 64 65 64 2c 20 69 74 73 20 61 73 73  * coded, its ass
72f20 6f 63 69 61 74 65 64 20 54 72 69 67 67 65 72 53  ociated TriggerS
72f30 74 61 63 6b 20 69 6e 73 74 61 6e 63 65 20 69 73  tack instance is
72f40 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74   pointed to by t
72f50 68 65 0a 20 2a 20 22 70 54 72 69 67 67 65 72 53  he. * "pTriggerS
72f60 74 61 63 6b 22 20 6d 65 6d 62 65 72 20 6f 66 20  tack" member of 
72f70 74 68 65 20 50 61 72 73 65 20 73 74 72 75 63 74  the Parse struct
72f80 75 72 65 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 70  ure.. *. * The p
72f90 54 61 62 20 6d 65 6d 62 65 72 20 70 6f 69 6e 74  Tab member point
72fa0 73 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 74  s to the table t
72fb0 68 61 74 20 74 72 69 67 67 65 72 73 20 61 72 65  hat triggers are
72fc0 20 62 65 69 6e 67 20 63 6f 64 65 64 20 6f 6e 2e   being coded on.
72fd0 20 54 68 65 20 0a 20 2a 20 6e 65 77 49 64 78 20   The . * newIdx 
72fe0 6d 65 6d 62 65 72 20 63 6f 6e 74 61 69 6e 73 20  member contains 
72ff0 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
73000 20 76 64 62 65 20 63 75 72 73 6f 72 20 74 68 61   vdbe cursor tha
73010 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68 65 20  t points at the 
73020 74 65 6d 70 0a 20 2a 20 74 61 62 6c 65 20 74 68  temp. * table th
73030 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 6e 65  at stores the ne
73040 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20  w.* references. 
73050 49 66 20 6e 65 77 2e 2a 20 72 65 66 65 72 65 6e  If new.* referen
73060 63 65 73 20 61 72 65 20 6e 6f 74 20 76 61 6c 69  ces are not vali
73070 64 0a 20 2a 20 66 6f 72 20 74 68 65 20 74 72 69  d. * for the tri
73080 67 67 65 72 20 62 65 69 6e 67 20 63 6f 64 65 64  gger being coded
73090 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 20 61 6e   (for example an
730a0 20 4f 4e 20 44 45 4c 45 54 45 20 74 72 69 67 67   ON DELETE trigg
730b0 65 72 29 2c 20 74 68 65 6e 20 6e 65 77 49 64 78  er), then newIdx
730c0 0a 20 2a 20 69 73 20 73 65 74 20 74 6f 20 2d 31  . * is set to -1
730d0 2e 20 54 68 65 20 6f 6c 64 49 64 78 20 6d 65 6d  . The oldIdx mem
730e0 62 65 72 20 69 73 20 61 6e 61 6c 6f 67 6f 75 73  ber is analogous
730f0 20 74 6f 20 6e 65 77 49 64 78 2c 20 66 6f 72 20   to newIdx, for 
73100 6f 6c 64 2e 2a 20 72 65 66 65 72 65 6e 63 65 73  old.* references
73110 2e 0a 20 2a 0a 20 2a 20 54 68 65 20 4f 4e 20 43  .. *. * The ON C
73120 4f 4e 46 4c 49 43 54 20 70 6f 6c 69 63 79 20 74  ONFLICT policy t
73130 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 74 68  o be used for th
73140 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  e trigger progra
73150 6d 20 73 74 65 70 73 20 69 73 20 73 74 6f 72 65  m steps is store
73160 64 20 0a 20 2a 20 61 73 20 74 68 65 20 6f 72 63  d . * as the orc
73170 6f 6e 66 20 6d 65 6d 62 65 72 2e 20 49 66 20 74  onf member. If t
73180 68 69 73 20 69 73 20 4f 45 5f 44 65 66 61 75 6c  his is OE_Defaul
73190 74 2c 20 74 68 65 6e 20 74 68 65 20 4f 4e 20 43  t, then the ON C
731a0 4f 4e 46 4c 49 43 54 20 63 6c 61 75 73 65 20 0a  ONFLICT clause .
731b0 20 2a 20 73 70 65 63 69 66 69 65 64 20 66 6f 72   * specified for
731c0 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72 69 67   individual trig
731d0 67 65 72 73 20 73 74 65 70 73 20 69 73 20 75 73  gers steps is us
731e0 65 64 2e 0a 20 2a 0a 20 2a 20 73 74 72 75 63 74  ed.. *. * struct
731f0 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 68 61   TriggerStack ha
73200 73 20 61 20 22 70 4e 65 78 74 22 20 6d 65 6d 62  s a "pNext" memb
73210 65 72 2c 20 74 6f 20 61 6c 6c 6f 77 20 6c 69 6e  er, to allow lin
73220 6b 65 64 20 6c 69 73 74 73 20 74 6f 20 62 65 0a  ked lists to be.
73230 20 2a 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 20   * constructed. 
73240 57 68 65 6e 20 63 6f 64 69 6e 67 20 6e 65 73 74  When coding nest
73250 65 64 20 74 72 69 67 67 65 72 73 20 28 74 72 69  ed triggers (tri
73260 67 67 65 72 73 20 66 69 72 65 64 20 62 79 20 6f  ggers fired by o
73270 74 68 65 72 20 74 72 69 67 67 65 72 73 29 0a 20  ther triggers). 
73280 2a 20 65 61 63 68 20 6e 65 73 74 65 64 20 74 72  * each nested tr
73290 69 67 67 65 72 20 73 74 6f 72 65 73 20 69 74 73  igger stores its
732a0 20 70 61 72 65 6e 74 20 74 72 69 67 67 65 72 27   parent trigger'
732b0 73 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 61  s TriggerStack a
732c0 73 20 74 68 65 20 22 70 4e 65 78 74 22 20 0a 20  s the "pNext" . 
732d0 2a 20 70 6f 69 6e 74 65 72 2e 20 4f 6e 63 65 20  * pointer. Once 
732e0 74 68 65 20 6e 65 73 74 65 64 20 74 72 69 67 67  the nested trigg
732f0 65 72 20 68 61 73 20 62 65 65 6e 20 63 6f 64 65  er has been code
73300 64 2c 20 74 68 65 20 70 4e 65 78 74 20 76 61 6c  d, the pNext val
73310 75 65 20 69 73 20 72 65 73 74 6f 72 65 64 0a 20  ue is restored. 
73320 2a 20 74 6f 20 74 68 65 20 70 54 72 69 67 67 65  * to the pTrigge
73330 72 53 74 61 63 6b 20 6d 65 6d 62 65 72 20 6f 66  rStack member of
73340 20 74 68 65 20 50 61 72 73 65 20 73 74 75 63 74   the Parse stuct
73350 75 72 65 20 61 6e 64 20 63 6f 64 69 6e 67 20 6f  ure and coding o
73360 66 20 74 68 65 20 70 61 72 65 6e 74 0a 20 2a 20  f the parent. * 
73370 74 72 69 67 67 65 72 20 63 6f 6e 74 69 6e 75 65  trigger continue
73380 73 2e 0a 20 2a 0a 20 2a 20 42 65 66 6f 72 65 20  s.. *. * Before 
73390 61 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72  a nested trigger
733a0 20 69 73 20 63 6f 64 65 64 2c 20 74 68 65 20 6c   is coded, the l
733b0 69 6e 6b 65 64 20 6c 69 73 74 20 70 6f 69 6e 74  inked list point
733c0 65 64 20 74 6f 20 62 79 20 74 68 65 20 0a 20 2a  ed to by the . *
733d0 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 20 69   pTriggerStack i
733e0 73 20 73 63 61 6e 6e 65 64 20 74 6f 20 65 6e 73  s scanned to ens
733f0 75 72 65 20 74 68 61 74 20 74 68 65 20 74 72 69  ure that the tri
73400 67 67 65 72 20 69 73 20 6e 6f 74 20 61 62 6f 75  gger is not abou
73410 74 20 74 6f 20 62 65 20 63 6f 64 65 64 0a 20 2a  t to be coded. *
73420 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20 49 66   recursively. If
73430 20 74 68 69 73 20 63 6f 6e 64 69 74 69 6f 6e 20   this condition 
73440 69 73 20 64 65 74 65 63 74 65 64 2c 20 74 68 65  is detected, the
73450 20 6e 65 73 74 65 64 20 74 72 69 67 67 65 72 20   nested trigger 
73460 69 73 20 6e 6f 74 20 63 6f 64 65 64 2e 0a 20 2a  is not coded.. *
73470 2f 0a 73 74 72 75 63 74 20 54 72 69 67 67 65 72  /.struct Trigger
73480 53 74 61 63 6b 20 7b 0a 20 20 54 61 62 6c 65 20  Stack {.  Table 
73490 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20 20 2f  *pTab;         /
734a0 2a 20 54 61 62 6c 65 20 74 68 61 74 20 74 72 69  * Table that tri
734b0 67 67 65 72 73 20 61 72 65 20 63 75 72 72 65 6e  ggers are curren
734c0 74 6c 79 20 62 65 69 6e 67 20 63 6f 64 65 64 20  tly being coded 
734d0 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49  on */.  int newI
734e0 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx;          /* 
734f0 49 6e 64 65 78 20 6f 66 20 76 64 62 65 20 63 75  Index of vdbe cu
73500 72 73 6f 72 20 74 6f 20 22 6e 65 77 22 20 74 65  rsor to "new" te
73510 6d 70 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  mp table */.  in
73520 74 20 6f 6c 64 49 64 78 3b 20 20 20 20 20 20 20  t oldIdx;       
73530 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 76     /* Index of v
73540 64 62 65 20 63 75 72 73 6f 72 20 74 6f 20 22 6f  dbe cursor to "o
73550 6c 64 22 20 74 65 6d 70 20 74 61 62 6c 65 20 2a  ld" temp table *
73560 2f 0a 20 20 75 33 32 20 6e 65 77 43 6f 6c 4d 61  /.  u32 newColMa
73570 73 6b 3b 0a 20 20 75 33 32 20 6f 6c 64 43 6f 6c  sk;.  u32 oldCol
73580 4d 61 73 6b 3b 0a 20 20 69 6e 74 20 6f 72 63 6f  Mask;.  int orco
73590 6e 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nf;          /* 
735a0 43 75 72 72 65 6e 74 20 6f 72 63 6f 6e 66 20 70  Current orconf p
735b0 6f 6c 69 63 79 20 2a 2f 0a 20 20 69 6e 74 20 69  olicy */.  int i
735c0 67 6e 6f 72 65 4a 75 6d 70 3b 20 20 20 20 20 20  gnoreJump;      
735d0 2f 2a 20 77 68 65 72 65 20 74 6f 20 6a 75 6d 70  /* where to jump
735e0 20 74 6f 20 66 6f 72 20 61 20 52 41 49 53 45 28   to for a RAISE(
735f0 49 47 4e 4f 52 45 29 20 2a 2f 0a 20 20 54 72 69  IGNORE) */.  Tri
73600 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 20  gger *pTrigger; 
73610 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
73620 20 63 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67   currently being
73630 20 63 6f 64 65 64 20 2a 2f 0a 20 20 54 72 69 67   coded */.  Trig
73640 67 65 72 53 74 61 63 6b 20 2a 70 4e 65 78 74 3b  gerStack *pNext;
73650 20 2f 2a 20 4e 65 78 74 20 74 72 69 67 67 65 72   /* Next trigger
73660 20 64 6f 77 6e 20 6f 6e 20 74 68 65 20 74 72 69   down on the tri
73670 67 67 65 72 20 73 74 61 63 6b 20 2a 2f 0a 7d 3b  gger stack */.};
73680 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
73690 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
736a0 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f 72 6d 61  contains informa
736b0 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74 68 65  tion used by the
736c0 20 73 71 6c 69 74 65 46 69 78 2e 2e 2e 0a 2a 2a   sqliteFix....**
736d0 20 72 6f 75 74 69 6e 65 73 20 61 73 20 74 68 65   routines as the
736e0 79 20 77 61 6c 6b 20 74 68 65 20 70 61 72 73 65  y walk the parse
736f0 20 74 72 65 65 20 74 6f 20 6d 61 6b 65 20 64 61   tree to make da
73700 74 61 62 61 73 65 20 72 65 66 65 72 65 6e 63 65  tabase reference
73710 73 0a 2a 2a 20 65 78 70 6c 69 63 69 74 2e 20 20  s.** explicit.  
73720 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
73730 63 74 20 44 62 46 69 78 65 72 20 44 62 46 69 78  ct DbFixer DbFix
73740 65 72 3b 0a 73 74 72 75 63 74 20 44 62 46 69 78  er;.struct DbFix
73750 65 72 20 7b 0a 20 20 50 61 72 73 65 20 2a 70 50  er {.  Parse *pP
73760 61 72 73 65 3b 20 20 20 20 20 20 2f 2a 20 54 68  arse;      /* Th
73770 65 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  e parsing contex
73780 74 2e 20 20 45 72 72 6f 72 20 6d 65 73 73 61 67  t.  Error messag
73790 65 73 20 77 72 69 74 74 65 6e 20 68 65 72 65 20  es written here 
737a0 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
737b0 2a 7a 44 62 3b 20 20 20 20 2f 2a 20 4d 61 6b 65  *zDb;    /* Make
737c0 20 73 75 72 65 20 61 6c 6c 20 6f 62 6a 65 63 74   sure all object
737d0 73 20 61 72 65 20 63 6f 6e 74 61 69 6e 65 64 20  s are contained 
737e0 69 6e 20 74 68 69 73 20 64 61 74 61 62 61 73 65  in this database
737f0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
73800 20 2a 7a 54 79 70 65 3b 20 20 2f 2a 20 54 79 70   *zType;  /* Typ
73810 65 20 6f 66 20 74 68 65 20 63 6f 6e 74 61 69 6e  e of the contain
73820 65 72 20 2d 20 75 73 65 64 20 66 6f 72 20 65 72  er - used for er
73830 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
73840 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70    const Token *p
73850 4e 61 6d 65 3b 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name; /* Name of
73860 20 74 68 65 20 63 6f 6e 74 61 69 6e 65 72 20 2d   the container -
73870 20 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72 20   used for error 
73880 6d 65 73 73 61 67 65 73 20 2a 2f 0a 7d 3b 0a 0a  messages */.};..
73890 2f 2a 0a 2a 2a 20 41 6e 20 6f 62 6a 65 63 74 65  /*.** An objecte
738a0 64 20 75 73 65 64 20 74 6f 20 61 63 63 75 6d 75  d used to accumu
738b0 6c 61 74 65 20 74 68 65 20 74 65 78 74 20 6f 66  late the text of
738c0 20 61 20 73 74 72 69 6e 67 20 77 68 65 72 65 20   a string where 
738d0 77 65 0a 2a 2a 20 64 6f 20 6e 6f 74 20 6e 65 63  we.** do not nec
738e0 65 73 73 61 72 69 6c 79 20 6b 6e 6f 77 20 68 6f  essarily know ho
738f0 77 20 62 69 67 20 74 68 65 20 73 74 72 69 6e 67  w big the string
73900 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68 65 20   will be in the 
73910 65 6e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 53  end..*/.struct S
73920 74 72 41 63 63 75 6d 20 7b 0a 20 20 73 71 6c 69  trAccum {.  sqli
73930 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20  te3 *db;        
73940 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 64 61 74   /* Optional dat
73950 61 62 61 73 65 20 66 6f 72 20 6c 6f 6f 6b 61 73  abase for lookas
73960 69 64 65 2e 20 20 43 61 6e 20 62 65 20 4e 55 4c  ide.  Can be NUL
73970 4c 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 42 61  L */.  char *zBa
73980 73 65 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41  se;         /* A
73990 20 62 61 73 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   base allocation
739a0 2e 20 20 4e 6f 74 20 66 72 6f 6d 20 6d 61 6c 6c  .  Not from mall
739b0 6f 63 2e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  oc. */.  char *z
739c0 54 65 78 74 3b 20 20 20 20 20 20 20 20 20 2f 2a  Text;         /*
739d0 20 54 68 65 20 73 74 72 69 6e 67 20 63 6f 6c 6c   The string coll
739e0 65 63 74 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a  ected so far */.
739f0 20 20 69 6e 74 20 20 6e 43 68 61 72 3b 20 20 20    int  nChar;   
73a00 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
73a10 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 73   of the string s
73a20 6f 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 20  o far */.  int  
73a30 6e 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20 20  nAlloc;         
73a40 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 73 70 61  /* Amount of spa
73a50 63 65 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20  ce allocated in 
73a60 7a 54 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 20  zText */.  int  
73a70 6d 78 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 20  mxAlloc;        
73a80 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77  /* Maximum allow
73a90 65 64 20 73 74 72 69 6e 67 20 6c 65 6e 67 74 68  ed string length
73aa0 20 2a 2f 0a 20 20 75 38 20 20 20 6d 61 6c 6c 6f   */.  u8   mallo
73ab0 63 46 61 69 6c 65 64 3b 20 20 20 2f 2a 20 42 65  cFailed;   /* Be
73ac0 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 61 6e  comes true if an
73ad0 79 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  y memory allocat
73ae0 69 6f 6e 20 66 61 69 6c 73 20 2a 2f 0a 20 20 75  ion fails */.  u
73af0 38 20 20 20 75 73 65 4d 61 6c 6c 6f 63 3b 20 20  8   useMalloc;  
73b00 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 7a      /* True if z
73b10 54 65 78 74 20 69 73 20 65 6e 6c 61 72 67 61 62  Text is enlargab
73b20 6c 65 20 75 73 69 6e 67 20 72 65 61 6c 6c 6f 63  le using realloc
73b30 20 2a 2f 0a 20 20 75 38 20 20 20 74 6f 6f 42 69   */.  u8   tooBi
73b40 67 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 42 65  g;         /* Be
73b50 63 6f 6d 65 73 20 74 72 75 65 20 69 66 20 73 74  comes true if st
73b60 72 69 6e 67 20 73 69 7a 65 20 65 78 63 65 65 64  ring size exceed
73b70 73 20 6c 69 6d 69 74 73 20 2a 2f 0a 7d 3b 0a 0a  s limits */.};..
73b80 2f 2a 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20  /*.** A pointer 
73b90 74 6f 20 74 68 69 73 20 73 74 72 75 63 74 75 72  to this structur
73ba0 65 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d  e is used to com
73bb0 6d 75 6e 69 63 61 74 65 20 69 6e 66 6f 72 6d 61  municate informa
73bc0 74 69 6f 6e 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c  tion.** from sql
73bd0 69 74 65 33 49 6e 69 74 20 61 6e 64 20 4f 50 5f  ite3Init and OP_
73be0 50 61 72 73 65 53 63 68 65 6d 61 20 69 6e 74 6f  ParseSchema into
73bf0 20 74 68 65 20 73 71 6c 69 74 65 33 49 6e 69 74   the sqlite3Init
73c00 43 61 6c 6c 62 61 63 6b 2e 0a 2a 2f 0a 74 79 70  Callback..*/.typ
73c10 65 64 65 66 20 73 74 72 75 63 74 20 7b 0a 20 20  edef struct {.  
73c20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
73c30 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
73c40 61 73 65 20 62 65 69 6e 67 20 69 6e 69 74 69 61  ase being initia
73c50 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  lized */.  int i
73c60 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Db;            /
73c70 2a 20 30 20 66 6f 72 20 6d 61 69 6e 20 64 61 74  * 0 for main dat
73c80 61 62 61 73 65 2e 20 20 31 20 66 6f 72 20 54 45  abase.  1 for TE
73c90 4d 50 2c 20 32 2e 2e 20 66 6f 72 20 41 54 54 41  MP, 2.. for ATTA
73ca0 43 48 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  CHed */.  char *
73cb0 2a 70 7a 45 72 72 4d 73 67 3b 20 20 20 20 2f 2a  *pzErrMsg;    /*
73cc0 20 45 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73   Error message s
73cd0 74 6f 72 65 64 20 68 65 72 65 20 2a 2f 0a 20 20  tored here */.  
73ce0 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20  int rc;         
73cf0 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 63 6f      /* Result co
73d00 64 65 20 73 74 6f 72 65 64 20 68 65 72 65 20 2a  de stored here *
73d10 2f 0a 7d 20 49 6e 69 74 44 61 74 61 3b 0a 0a 2f  /.} InitData;../
73d20 2a 0a 2a 2a 20 53 74 72 75 63 74 75 72 65 20 63  *.** Structure c
73d30 6f 6e 74 61 69 6e 69 6e 67 20 67 6c 6f 62 61 6c  ontaining global
73d40 20 63 6f 6e 66 69 67 75 72 61 74 69 6f 6e 20 64   configuration d
73d50 61 74 61 20 66 6f 72 20 74 68 65 20 53 51 4c 69  ata for the SQLi
73d60 74 65 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a  te library..**.*
73d70 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
73d80 20 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 73 20 73   also contains s
73d90 6f 6d 65 20 73 74 61 74 65 20 69 6e 66 6f 72 6d  ome state inform
73da0 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  ation..*/.struct
73db0 20 53 71 6c 69 74 65 33 43 6f 6e 66 69 67 20 7b   Sqlite3Config {
73dc0 0a 20 20 69 6e 74 20 62 4d 65 6d 73 74 61 74 3b  .  int bMemstat;
73dd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73de0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20       /* True to 
73df0 65 6e 61 62 6c 65 20 6d 65 6d 6f 72 79 20 73 74  enable memory st
73e00 61 74 75 73 20 2a 2f 0a 20 20 69 6e 74 20 62 43  atus */.  int bC
73e10 6f 72 65 4d 75 74 65 78 3b 20 20 20 20 20 20 20  oreMutex;       
73e20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
73e30 72 75 65 20 74 6f 20 65 6e 61 62 6c 65 20 63 6f  rue to enable co
73e40 72 65 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20  re mutexing */. 
73e50 20 69 6e 74 20 62 46 75 6c 6c 4d 75 74 65 78 3b   int bFullMutex;
73e60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73e70 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65 6e     /* True to en
73e80 61 62 6c 65 20 66 75 6c 6c 20 6d 75 74 65 78 69  able full mutexi
73e90 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 53 74  ng */.  int mxSt
73ea0 72 6c 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20  rlen;           
73eb0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
73ec0 69 6d 75 6d 20 73 74 72 69 6e 67 20 6c 65 6e 67  imum string leng
73ed0 74 68 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 4c 6f  th */.  int szLo
73ee0 6f 6b 61 73 69 64 65 3b 20 20 20 20 20 20 20 20  okaside;        
73ef0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65 66            /* Def
73f00 61 75 6c 74 20 6c 6f 6f 6b 61 73 69 64 65 20 62  ault lookaside b
73f10 75 66 66 65 72 20 73 69 7a 65 20 2a 2f 0a 20 20  uffer size */.  
73f20 69 6e 74 20 6e 4c 6f 6f 6b 61 73 69 64 65 3b 20  int nLookaside; 
73f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
73f40 20 20 2f 2a 20 44 65 66 61 75 6c 74 20 6c 6f 6f    /* Default loo
73f50 6b 61 73 69 64 65 20 62 75 66 66 65 72 20 63 6f  kaside buffer co
73f60 75 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  unt */.  sqlite3
73f70 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6d 3b 20  _mem_methods m; 
73f80 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
73f90 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
73fa0 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74 65 72 66  llocation interf
73fb0 61 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ace */.  sqlite3
73fc0 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 6d  _mutex_methods m
73fd0 75 74 65 78 3b 20 20 20 20 20 20 2f 2a 20 4c 6f  utex;      /* Lo
73fe0 77 2d 6c 65 76 65 6c 20 6d 75 74 65 78 20 69 6e  w-level mutex in
73ff0 74 65 72 66 61 63 65 20 2a 2f 0a 20 20 73 71 6c  terface */.  sql
74000 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74 68  ite3_pcache_meth
74010 6f 64 73 20 70 63 61 63 68 65 3b 20 20 20 20 2f  ods pcache;    /
74020 2a 20 4c 6f 77 2d 6c 65 76 65 6c 20 70 61 67 65  * Low-level page
74030 2d 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65  -cache interface
74040 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 48 65 61   */.  void *pHea
74050 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
74060 20 20 20 20 20 20 20 20 2f 2a 20 48 65 61 70 20          /* Heap 
74070 73 74 6f 72 61 67 65 20 73 70 61 63 65 20 2a 2f  storage space */
74080 0a 20 20 69 6e 74 20 6e 48 65 61 70 3b 20 20 20  .  int nHeap;   
74090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
740a0 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
740b0 70 48 65 61 70 5b 5d 20 2a 2f 0a 20 20 69 6e 74  pHeap[] */.  int
740c0 20 6d 6e 52 65 71 2c 20 6d 78 52 65 71 3b 20 20   mnReq, mxReq;  
740d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
740e0 2a 20 4d 69 6e 20 61 6e 64 20 6d 61 78 20 68 65  * Min and max he
740f0 61 70 20 72 65 71 75 65 73 74 73 20 73 69 7a 65  ap requests size
74100 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 63  s */.  void *pSc
74110 72 61 74 63 68 3b 20 20 20 20 20 20 20 20 20 20  ratch;          
74120 20 20 20 20 20 20 20 20 20 2f 2a 20 53 63 72 61           /* Scra
74130 74 63 68 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20  tch memory */.  
74140 69 6e 74 20 73 7a 53 63 72 61 74 63 68 3b 20 20  int szScratch;  
74150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74160 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63    /* Size of eac
74170 68 20 73 63 72 61 74 63 68 20 62 75 66 66 65 72  h scratch buffer
74180 20 2a 2f 0a 20 20 69 6e 74 20 6e 53 63 72 61 74   */.  int nScrat
74190 63 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ch;             
741a0 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
741b0 72 20 6f 66 20 73 63 72 61 74 63 68 20 62 75 66  r of scratch buf
741c0 66 65 72 73 20 2a 2f 0a 20 20 76 6f 69 64 20 2a  fers */.  void *
741d0 70 50 61 67 65 3b 20 20 20 20 20 20 20 20 20 20  pPage;          
741e0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
741f0 61 67 65 20 63 61 63 68 65 20 6d 65 6d 6f 72 79  age cache memory
74200 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 50 61 67 65   */.  int szPage
74210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
74220 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20          /* Size 
74230 6f 66 20 65 61 63 68 20 70 61 67 65 20 69 6e 20  of each page in 
74240 70 50 61 67 65 5b 5d 20 2a 2f 0a 20 20 69 6e 74  pPage[] */.  int
74250 20 6e 50 61 67 65 3b 20 20 20 20 20 20 20 20 20   nPage;         
74260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
74270 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
74280 73 20 69 6e 20 70 50 61 67 65 5b 5d 20 2a 2f 0a  s in pPage[] */.
74290 20 20 69 6e 74 20 69 73 49 6e 69 74 3b 20 20 20    int isInit;   
742a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
742b0 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74 65      /* True afte
742c0 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  r initialization
742d0 20 68 61 73 20 66 69 6e 69 73 68 65 64 20 2a 2f   has finished */
742e0 0a 20 20 69 6e 74 20 69 6e 50 72 6f 67 72 65 73  .  int inProgres
742f0 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s;              
74300 20 20 20 20 20 2f 2a 20 54 72 75 65 20 77 68 69       /* True whi
74310 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  le initializatio
74320 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 20 2a 2f  n in progress */
74330 0a 20 20 69 6e 74 20 69 73 4d 61 6c 6c 6f 63 49  .  int isMallocI
74340 6e 69 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  nit;            
74350 20 20 20 20 20 2f 2a 20 54 72 75 65 20 61 66 74       /* True aft
74360 65 72 20 6d 61 6c 6c 6f 63 20 69 73 20 69 6e 69  er malloc is ini
74370 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 73 71  tialized */.  sq
74380 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 49 6e  lite3_mutex *pIn
74390 69 74 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20  itMutex;        
743a0 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 62 79  /* Mutex used by
743b0 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
743c0 69 7a 65 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e  ize() */.  int n
743d0 52 65 66 49 6e 69 74 4d 75 74 65 78 3b 20 20 20  RefInitMutex;   
743e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
743f0 4e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20  Number of users 
74400 6f 66 20 70 49 6e 69 74 4d 75 74 65 78 20 2a 2f  of pInitMutex */
74410 0a 20 20 69 6e 74 20 6d 78 50 61 72 73 65 72 53  .  int mxParserS
74420 74 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20  tack;           
74430 20 20 20 20 20 2f 2a 20 6d 61 78 69 6d 75 6d 20       /* maximum 
74440 64 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72  depth of the par
74450 73 65 72 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69  ser stack */.  i
74460 6e 74 20 73 68 61 72 65 64 43 61 63 68 65 45 6e  nt sharedCacheEn
74470 61 62 6c 65 64 3b 20 20 20 20 20 20 20 20 20 20  abled;          
74480 20 2f 2a 20 74 72 75 65 20 69 66 20 73 68 61 72   /* true if shar
74490 65 64 2d 63 61 63 68 65 20 6d 6f 64 65 20 65 6e  ed-cache mode en
744a0 61 62 6c 65 64 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  abled */.};../*.
744b0 2a 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69 6e 74  ** Context point
744c0 65 72 20 70 61 73 73 65 64 20 64 6f 77 6e 20 74  er passed down t
744d0 68 72 6f 75 67 68 20 74 68 65 20 74 72 65 65 2d  hrough the tree-
744e0 77 61 6c 6b 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  walk..*/.struct 
744f0 57 61 6c 6b 65 72 20 7b 0a 20 20 69 6e 74 20 28  Walker {.  int (
74500 2a 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 29 28  *xExprCallback)(
74510 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 2a 29 3b  Walker*, Expr*);
74520 20 20 20 20 20 2f 2a 20 43 61 6c 6c 62 61 63 6b       /* Callback
74530 20 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73   for expressions
74540 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 65 6c   */.  int (*xSel
74550 65 63 74 43 61 6c 6c 62 61 63 6b 29 28 57 61 6c  ectCallback)(Wal
74560 6b 65 72 2a 2c 53 65 6c 65 63 74 2a 29 3b 20 20  ker*,Select*);  
74570 2f 2a 20 43 61 6c 6c 62 61 63 6b 20 66 6f 72 20  /* Callback for 
74580 53 45 4c 45 43 54 73 20 2a 2f 0a 20 20 50 61 72  SELECTs */.  Par
74590 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20 20 20  se *pParse;     
745a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
745b0 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72         /* Parser
745c0 20 63 6f 6e 74 65 78 74 2e 20 20 2a 2f 0a 20 20   context.  */.  
745d0 75 6e 69 6f 6e 20 7b 20 20 20 20 20 20 20 20 20  union {         
745e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
745f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 74            /* Ext
74600 72 61 20 64 61 74 61 20 66 6f 72 20 63 61 6c 6c  ra data for call
74610 62 61 63 6b 20 2a 2f 0a 20 20 20 20 4e 61 6d 65  back */.    Name
74620 43 6f 6e 74 65 78 74 20 2a 70 4e 43 3b 20 20 20  Context *pNC;   
74630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74640 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 69 6e 67         /* Naming
74650 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 20 20   context */.    
74660 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
74670 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
74690 74 65 67 65 72 20 76 61 6c 75 65 20 2a 2f 0a 20  teger value */. 
746a0 20 7d 20 75 3b 0a 7d 3b 0a 0a 2f 2a 20 46 6f 72   } u;.};../* For
746b0 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f 6e  ward declaration
746c0 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s */.SQLITE_PRIV
746d0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
746e0 61 6c 6b 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c  alkExpr(Walker*,
746f0 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
74700 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74710 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28  te3WalkExprList(
74720 57 61 6c 6b 65 72 2a 2c 20 45 78 70 72 4c 69 73  Walker*, ExprLis
74730 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
74740 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 57  ATE int sqlite3W
74750 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65 72  alkSelect(Walker
74760 2a 2c 20 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  *, Select*);.SQL
74770 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
74780 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
74790 74 45 78 70 72 28 57 61 6c 6b 65 72 2a 2c 20 53  tExpr(Walker*, S
747a0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
747b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
747c0 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 46 72 6f  te3WalkSelectFro
747d0 6d 28 57 61 6c 6b 65 72 2a 2c 20 53 65 6c 65 63  m(Walker*, Selec
747e0 74 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  t*);../*.** Retu
747f0 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 74 68 65  rn code from the
74800 20 70 61 72 73 65 2d 74 72 65 65 20 77 61 6c 6b   parse-tree walk
74810 69 6e 67 20 70 72 69 6d 69 74 69 76 65 73 20 61  ing primitives a
74820 6e 64 20 74 68 65 69 72 0a 2a 2a 20 63 61 6c 6c  nd their.** call
74830 62 61 63 6b 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  backs..*/.#defin
74840 65 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 20 20  e WRC_Continue  
74850 20 20 30 0a 23 64 65 66 69 6e 65 20 57 52 43 5f    0.#define WRC_
74860 50 72 75 6e 65 20 20 20 20 20 20 20 31 0a 23 64  Prune       1.#d
74870 65 66 69 6e 65 20 57 52 43 5f 41 62 6f 72 74 20  efine WRC_Abort 
74880 20 20 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a 20 41        2../*.** A
74890 73 73 75 6d 69 6e 67 20 7a 49 6e 20 70 6f 69 6e  ssuming zIn poin
748a0 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ts to the first 
748b0 62 79 74 65 20 6f 66 20 61 20 55 54 46 2d 38 20  byte of a UTF-8 
748c0 63 68 61 72 61 63 74 65 72 2c 0a 2a 2a 20 61 64  character,.** ad
748d0 76 61 6e 63 65 20 7a 49 6e 20 74 6f 20 70 6f 69  vance zIn to poi
748e0 6e 74 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  nt to the first 
748f0 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65 78 74  byte of the next
74900 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
74910 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 51 4c  ..*/.#define SQL
74920 49 54 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 49  ITE_SKIP_UTF8(zI
74930 6e 29 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  n) {            
74940 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
74950 69 66 28 20 28 2a 28 7a 49 6e 2b 2b 29 29 3e 3d  if( (*(zIn++))>=
74960 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20  0xc0 ){         
74970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
74980 20 20 20 20 20 5c 0a 20 20 20 20 77 68 69 6c 65       \.    while
74990 28 20 28 2a 7a 49 6e 20 26 20 30 78 63 30 29 3d  ( (*zIn & 0xc0)=
749a0 3d 30 78 38 30 20 29 7b 20 7a 49 6e 2b 2b 3b 20  =0x80 ){ zIn++; 
749b0 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a  }             \.
749c0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
749d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
749e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
749f0 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a         \.}../*.*
74a00 2a 20 54 68 65 20 53 51 4c 49 54 45 5f 43 4f 52  * The SQLITE_COR
74a10 52 55 50 54 5f 42 4b 50 54 20 6d 61 63 72 6f 20  RUPT_BKPT macro 
74a20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20 61 20  can be either a 
74a30 63 6f 6e 73 74 61 6e 74 20 28 66 6f 72 20 70 72  constant (for pr
74a40 6f 64 75 63 74 69 6f 6e 0a 2a 2a 20 62 75 69 6c  oduction.** buil
74a50 64 73 29 20 6f 72 20 61 20 66 75 6e 63 74 69 6f  ds) or a functio
74a60 6e 20 63 61 6c 6c 20 28 66 6f 72 20 64 65 62 75  n call (for debu
74a70 67 67 69 6e 67 29 2e 20 20 49 66 20 69 74 20 69  gging).  If it i
74a80 73 20 61 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c  s a function cal
74a90 6c 2c 0a 2a 2a 20 69 74 20 61 6c 6c 6f 77 73 20  l,.** it allows 
74aa0 74 68 65 20 6f 70 65 72 61 74 6f 72 20 74 6f 20  the operator to 
74ab0 73 65 74 20 61 20 62 72 65 61 6b 70 6f 69 6e 74  set a breakpoint
74ac0 20 61 74 20 74 68 65 20 73 70 6f 74 20 77 68 65   at the spot whe
74ad0 72 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  re database.** c
74ae0 6f 72 72 75 70 74 69 6f 6e 20 69 73 20 66 69 72  orruption is fir
74af0 73 74 20 64 65 74 65 63 74 65 64 2e 0a 2a 2f 0a  st detected..*/.
74b00 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
74b10 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
74b20 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
74b30 43 6f 72 72 75 70 74 28 76 6f 69 64 29 3b 0a 23  Corrupt(void);.#
74b40 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43   define SQLITE_C
74b50 4f 52 52 55 50 54 5f 42 4b 50 54 20 73 71 6c 69  ORRUPT_BKPT sqli
74b60 74 65 33 43 6f 72 72 75 70 74 28 29 0a 23 65 6c  te3Corrupt().#el
74b70 73 65 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49  se.# define SQLI
74b80 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 20  TE_CORRUPT_BKPT 
74b90 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 0a 23  SQLITE_CORRUPT.#
74ba0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74  endif../*.** Int
74bb0 65 72 6e 61 6c 20 66 75 6e 63 74 69 6f 6e 20 70  ernal function p
74bc0 72 6f 74 6f 74 79 70 65 73 0a 2a 2f 0a 53 51 4c  rototypes.*/.SQL
74bd0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
74be0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63  sqlite3StrICmp(c
74bf0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e  onst char *, con
74c00 73 74 20 63 68 61 72 20 2a 29 3b 0a 53 51 4c 49  st char *);.SQLI
74c10 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74c20 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 63  qlite3StrNICmp(c
74c30 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 63 6f 6e  onst char *, con
74c40 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b  st char *, int);
74c50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74c60 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75 6d  int sqlite3IsNum
74c70 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 2a 2c  ber(const char*,
74c80 20 69 6e 74 2a 2c 20 75 38 29 3b 0a 53 51 4c 49   int*, u8);.SQLI
74c90 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74ca0 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 73 71 6c  qlite3Strlen(sql
74cb0 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20 63 68 61  ite3*, const cha
74cc0 72 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49  r*);..SQLITE_PRI
74cd0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
74ce0 4d 61 6c 6c 6f 63 49 6e 69 74 28 76 6f 69 64 29  MallocInit(void)
74cf0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74d00 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 61 6c   void sqlite3Mal
74d10 6c 6f 63 45 6e 64 28 76 6f 69 64 29 3b 0a 53 51  locEnd(void);.SQ
74d20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
74d30 64 20 2a 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  d *sqlite3Malloc
74d40 28 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (int);.SQLITE_PR
74d50 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
74d60 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 69 6e  te3MallocZero(in
74d70 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
74d80 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
74d90 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c  DbMallocZero(sql
74da0 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ite3*, int);.SQL
74db0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
74dc0 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f   *sqlite3DbMallo
74dd0 63 52 61 77 28 73 71 6c 69 74 65 33 2a 2c 20 69  cRaw(sqlite3*, i
74de0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
74df0 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74 65  ATE char *sqlite
74e00 33 44 62 53 74 72 44 75 70 28 73 71 6c 69 74 65  3DbStrDup(sqlite
74e10 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  3*,const char*);
74e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
74e30 63 68 61 72 20 2a 73 71 6c 69 74 65 33 44 62 53  char *sqlite3DbS
74e40 74 72 4e 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  trNDup(sqlite3*,
74e50 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 69 6e 74  const char*, int
74e60 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
74e70 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 52  E void *sqlite3R
74e80 65 61 6c 6c 6f 63 28 76 6f 69 64 2a 2c 20 69 6e  ealloc(void*, in
74e90 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
74ea0 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  TE void *sqlite3
74eb0 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
74ec0 73 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20  sqlite3 *, void 
74ed0 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
74ee0 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
74ef0 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 73  lite3DbRealloc(s
74f00 71 6c 69 74 65 33 20 2a 2c 20 76 6f 69 64 20 2a  qlite3 *, void *
74f10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
74f20 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
74f30 74 65 33 44 62 46 72 65 65 28 73 71 6c 69 74 65  te3DbFree(sqlite
74f40 33 2a 2c 20 76 6f 69 64 2a 29 3b 0a 53 51 4c 49  3*, void*);.SQLI
74f50 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
74f60 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65  qlite3MallocSize
74f70 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45 5f  (void*);.SQLITE_
74f80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
74f90 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
74fa0 73 71 6c 69 74 65 33 2a 2c 20 76 6f 69 64 2a 29  sqlite3*, void*)
74fb0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74fc0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 53 63   void *sqlite3Sc
74fd0 72 61 74 63 68 4d 61 6c 6c 6f 63 28 69 6e 74 29  ratchMalloc(int)
74fe0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
74ff0 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 72   void sqlite3Scr
75000 61 74 63 68 46 72 65 65 28 76 6f 69 64 2a 29 3b  atchFree(void*);
75010 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75020 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67  void *sqlite3Pag
75030 65 4d 61 6c 6c 6f 63 28 69 6e 74 29 3b 0a 53 51  eMalloc(int);.SQ
75040 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75050 64 20 73 71 6c 69 74 65 33 50 61 67 65 46 72 65  d sqlite3PageFre
75060 65 28 76 6f 69 64 2a 29 3b 0a 53 51 4c 49 54 45  e(void*);.SQLITE
75070 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75080 6c 69 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75  lite3MemSetDefau
75090 6c 74 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54 45  lt(void);.SQLITE
750a0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
750b0 6c 69 74 65 33 42 65 6e 69 67 6e 4d 61 6c 6c 6f  lite3BenignMallo
750c0 63 48 6f 6f 6b 73 28 76 6f 69 64 20 28 2a 29 28  cHooks(void (*)(
750d0 76 6f 69 64 29 2c 20 76 6f 69 64 20 28 2a 29 28  void), void (*)(
750e0 76 6f 69 64 29 29 3b 0a 53 51 4c 49 54 45 5f 50  void));.SQLITE_P
750f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
75100 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 76 6f  e3MemoryAlarm(vo
75110 69 64 20 28 2a 29 28 76 6f 69 64 2a 2c 20 73 71  id (*)(void*, sq
75120 6c 69 74 65 33 5f 69 6e 74 36 34 2c 20 69 6e 74  lite3_int64, int
75130 29 2c 20 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65  ), void*, sqlite
75140 33 5f 69 6e 74 36 34 29 3b 0a 0a 23 69 66 64 65  3_int64);..#ifde
75150 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
75160 4d 45 4d 53 59 53 33 0a 53 51 4c 49 54 45 5f 50  MEMSYS3.SQLITE_P
75170 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c  RIVATE const sql
75180 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73  ite3_mem_methods
75190 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d   *sqlite3MemGetM
751a0 65 6d 73 79 73 33 28 76 6f 69 64 29 3b 0a 23 65  emsys3(void);.#e
751b0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
751c0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
751d0 35 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  5.SQLITE_PRIVATE
751e0 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d   const sqlite3_m
751f0 65 6d 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69  em_methods *sqli
75200 74 65 33 4d 65 6d 47 65 74 4d 65 6d 73 79 73 35  te3MemGetMemsys5
75210 28 76 6f 69 64 29 3b 0a 23 65 6e 64 69 66 0a 0a  (void);.#endif..
75220 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
75230 4d 55 54 45 58 5f 4f 4d 49 54 0a 53 51 4c 49 54  MUTEX_OMIT.SQLIT
75240 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
75250 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
75260 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
75270 74 4d 75 74 65 78 28 76 6f 69 64 29 3b 0a 53 51  tMutex(void);.SQ
75280 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
75290 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71  qlite3_mutex *sq
752a0 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
752b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
752c0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
752d0 65 33 4d 75 74 65 78 49 6e 69 74 28 76 6f 69 64  e3MutexInit(void
752e0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
752f0 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 4d  E   int sqlite3M
75300 75 74 65 78 45 6e 64 28 76 6f 69 64 29 3b 0a 23  utexEnd(void);.#
75310 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f 50 52  endif..SQLITE_PR
75320 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
75330 33 53 74 61 74 75 73 56 61 6c 75 65 28 69 6e 74  3StatusValue(int
75340 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75350 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
75360 61 74 75 73 41 64 64 28 69 6e 74 2c 20 69 6e 74  atusAdd(int, int
75370 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
75380 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74  E void sqlite3St
75390 61 74 75 73 53 65 74 28 69 6e 74 2c 20 69 6e 74  atusSet(int, int
753a0 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  );..SQLITE_PRIVA
753b0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73  TE int sqlite3Is
753c0 4e 61 4e 28 64 6f 75 62 6c 65 29 3b 0a 0a 53 51  NaN(double);..SQ
753d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
753e0 64 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74  d sqlite3VXPrint
753f0 66 28 53 74 72 41 63 63 75 6d 2a 2c 20 69 6e 74  f(StrAccum*, int
75400 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 76  , const char*, v
75410 61 5f 6c 69 73 74 29 3b 0a 53 51 4c 49 54 45 5f  a_list);.SQLITE_
75420 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
75430 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c  lite3MPrintf(sql
75440 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72  ite3*,const char
75450 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49 54 45 5f  *, ...);.SQLITE_
75460 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
75470 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 73 71  lite3VMPrintf(sq
75480 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61  lite3*,const cha
75490 72 2a 2c 20 76 61 5f 6c 69 73 74 29 3b 0a 53 51  r*, va_list);.SQ
754a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
754b0 72 20 2a 73 71 6c 69 74 65 33 4d 41 70 70 65 6e  r *sqlite3MAppen
754c0 64 66 28 73 71 6c 69 74 65 33 2a 2c 63 68 61 72  df(sqlite3*,char
754d0 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 2e 2e  *,const char*,..
754e0 2e 29 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .);.#if defined(
754f0 53 51 4c 49 54 45 5f 54 45 53 54 29 20 7c 7c 20  SQLITE_TEST) || 
75500 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
75510 45 42 55 47 29 0a 53 51 4c 49 54 45 5f 50 52 49  EBUG).SQLITE_PRI
75520 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
75530 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 63  te3DebugPrintf(c
75540 6f 6e 73 74 20 63 68 61 72 2a 2c 20 2e 2e 2e 29  onst char*, ...)
75550 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 64 65 66  ;.#endif.#if def
75560 69 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54  ined(SQLITE_TEST
75570 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ).SQLITE_PRIVATE
75580 20 20 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33     void *sqlite3
75590 54 65 73 74 54 65 78 74 54 6f 50 74 72 28 63 6f  TestTextToPtr(co
755a0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 23 65 6e 64  nst char*);.#end
755b0 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  if.SQLITE_PRIVAT
755c0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
755d0 74 53 74 72 69 6e 67 28 63 68 61 72 20 2a 2a 2c  tString(char **,
755e0 20 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74   sqlite3*, const
755f0 20 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51   char*, ...);.SQ
75600 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75610 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73  d sqlite3ErrorMs
75620 67 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20  g(Parse*, const 
75630 63 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c  char*, ...);.SQL
75640 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
75650 20 73 71 6c 69 74 65 33 45 72 72 6f 72 43 6c 65   sqlite3ErrorCle
75660 61 72 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  ar(Parse*);.SQLI
75670 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
75680 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63  sqlite3Dequote(c
75690 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
756a0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
756b0 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 73 71  e3DequoteExpr(sq
756c0 6c 69 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a  lite3*, Expr*);.
756d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
756e0 6e 74 20 73 71 6c 69 74 65 33 4b 65 79 77 6f 72  nt sqlite3Keywor
756f0 64 43 6f 64 65 28 63 6f 6e 73 74 20 75 6e 73 69  dCode(const unsi
75700 67 6e 65 64 20 63 68 61 72 2a 2c 20 69 6e 74 29  gned char*, int)
75710 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
75720 20 69 6e 74 20 73 71 6c 69 74 65 33 52 75 6e 50   int sqlite3RunP
75730 61 72 73 65 72 28 50 61 72 73 65 2a 2c 20 63 6f  arser(Parse*, co
75740 6e 73 74 20 63 68 61 72 2a 2c 20 63 68 61 72 20  nst char*, char 
75750 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  **);.SQLITE_PRIV
75760 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
75770 46 69 6e 69 73 68 43 6f 64 69 6e 67 28 50 61 72  FinishCoding(Par
75780 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
75790 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
757a0 47 65 74 54 65 6d 70 52 65 67 28 50 61 72 73 65  GetTempReg(Parse
757b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
757c0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
757d0 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 50 61  eleaseTempReg(Pa
757e0 72 73 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  rse*,int);.SQLIT
757f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
75800 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
75810 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b 0a 53  e(Parse*,int);.S
75820 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
75830 69 64 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73  id sqlite3Releas
75840 65 54 65 6d 70 52 61 6e 67 65 28 50 61 72 73 65  eTempRange(Parse
75850 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49  *,int,int);.SQLI
75860 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
75870 2a 73 71 6c 69 74 65 33 45 78 70 72 28 73 71 6c  *sqlite3Expr(sql
75880 69 74 65 33 2a 2c 20 69 6e 74 2c 20 45 78 70 72  ite3*, int, Expr
75890 2a 2c 20 45 78 70 72 2a 2c 20 63 6f 6e 73 74 20  *, Expr*, const 
758a0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
758b0 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71  PRIVATE Expr *sq
758c0 6c 69 74 65 33 50 45 78 70 72 28 50 61 72 73 65  lite3PExpr(Parse
758d0 2a 2c 20 69 6e 74 2c 20 45 78 70 72 2a 2c 20 45  *, int, Expr*, E
758e0 78 70 72 2a 2c 20 63 6f 6e 73 74 20 54 6f 6b 65  xpr*, const Toke
758f0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
75900 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65  ATE Expr *sqlite
75910 33 52 65 67 69 73 74 65 72 45 78 70 72 28 50 61  3RegisterExpr(Pa
75920 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a 53 51  rse*,Token*);.SQ
75930 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70  LITE_PRIVATE Exp
75940 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 41 6e  r *sqlite3ExprAn
75950 64 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a  d(sqlite3*,Expr*
75960 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  , Expr*);.SQLITE
75970 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75980 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 45 78  lite3ExprSpan(Ex
75990 70 72 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  pr*,Token*,Token
759a0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
759b0 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33  TE Expr *sqlite3
759c0 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 50 61 72  ExprFunction(Par
759d0 73 65 2a 2c 45 78 70 72 4c 69 73 74 2a 2c 20 54  se*,ExprList*, T
759e0 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
759f0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
75a00 74 65 33 45 78 70 72 41 73 73 69 67 6e 56 61 72  te3ExprAssignVar
75a10 4e 75 6d 62 65 72 28 50 61 72 73 65 2a 2c 20 45  Number(Parse*, E
75a20 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
75a30 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75a40 65 33 45 78 70 72 43 6c 65 61 72 28 73 71 6c 69  e3ExprClear(sqli
75a50 74 65 33 2a 2c 20 45 78 70 72 2a 29 3b 0a 53 51  te3*, Expr*);.SQ
75a60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75a70 64 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c  d sqlite3ExprDel
75a80 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 45 78  ete(sqlite3*, Ex
75a90 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
75aa0 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a 73  VATE ExprList *s
75ab0 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
75ac0 70 65 6e 64 28 50 61 72 73 65 2a 2c 45 78 70 72  pend(Parse*,Expr
75ad0 4c 69 73 74 2a 2c 45 78 70 72 2a 2c 54 6f 6b 65  List*,Expr*,Toke
75ae0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
75af0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
75b00 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 73  ExprListDelete(s
75b10 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69 73  qlite3*, ExprLis
75b20 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
75b30 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
75b40 6e 69 74 28 73 71 6c 69 74 65 33 2a 2c 20 63 68  nit(sqlite3*, ch
75b50 61 72 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ar**);.SQLITE_PR
75b60 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
75b70 33 49 6e 69 74 43 61 6c 6c 62 61 63 6b 28 76 6f  3InitCallback(vo
75b80 69 64 2a 2c 20 69 6e 74 2c 20 63 68 61 72 2a 2a  id*, int, char**
75b90 2c 20 63 68 61 72 2a 2a 29 3b 0a 53 51 4c 49 54  , char**);.SQLIT
75ba0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
75bb0 71 6c 69 74 65 33 50 72 61 67 6d 61 28 50 61 72  qlite3Pragma(Par
75bc0 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
75bd0 2a 2c 54 6f 6b 65 6e 2a 2c 69 6e 74 29 3b 0a 53  *,Token*,int);.S
75be0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
75bf0 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74 49  id sqlite3ResetI
75c00 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73 71  nternalSchema(sq
75c10 6c 69 74 65 33 2a 2c 20 69 6e 74 29 3b 0a 53 51  lite3*, int);.SQ
75c20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
75c30 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  d sqlite3BeginPa
75c40 72 73 65 28 50 61 72 73 65 2a 2c 69 6e 74 29 3b  rse(Parse*,int);
75c50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75c60 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d  void sqlite3Comm
75c70 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
75c80 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c  s(sqlite3*);.SQL
75c90 49 54 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c  ITE_PRIVATE Tabl
75ca0 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75 6c 74  e *sqlite3Result
75cb0 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61 72 73  SetOfSelect(Pars
75cc0 65 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c  e*,Select*);.SQL
75cd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
75ce0 20 73 71 6c 69 74 65 33 4f 70 65 6e 4d 61 73 74   sqlite3OpenMast
75cf0 65 72 54 61 62 6c 65 28 50 61 72 73 65 20 2a 2c  erTable(Parse *,
75d00 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
75d10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75d20 65 33 53 74 61 72 74 54 61 62 6c 65 28 50 61 72  e3StartTable(Par
75d30 73 65 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e  se*,Token*,Token
75d40 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74 2c 69 6e  *,int,int,int,in
75d50 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
75d60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
75d70 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 2a 2c  ddColumn(Parse*,
75d80 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
75d90 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
75da0 69 74 65 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 50  ite3AddNotNull(P
75db0 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  arse*, int);.SQL
75dc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
75dd0 20 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61   sqlite3AddPrima
75de0 72 79 4b 65 79 28 50 61 72 73 65 2a 2c 20 45 78  ryKey(Parse*, Ex
75df0 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69 6e  prList*, int, in
75e00 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
75e10 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
75e20 69 74 65 33 41 64 64 43 68 65 63 6b 43 6f 6e 73  ite3AddCheckCons
75e30 74 72 61 69 6e 74 28 50 61 72 73 65 2a 2c 20 45  traint(Parse*, E
75e40 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
75e50 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
75e60 65 33 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 28  e3AddColumnType(
75e70 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a 29 3b 0a  Parse*,Token*);.
75e80 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
75e90 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44 65  oid sqlite3AddDe
75ea0 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73 65  faultValue(Parse
75eb0 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45  *,Expr*);.SQLITE
75ec0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
75ed0 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74 65 54  lite3AddCollateT
75ee0 79 70 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65  ype(Parse*, Toke
75ef0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
75f00 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
75f10 45 6e 64 54 61 62 6c 65 28 50 61 72 73 65 2a 2c  EndTable(Parse*,
75f20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 53 65  Token*,Token*,Se
75f30 6c 65 63 74 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f  lect*);..SQLITE_
75f40 50 52 49 56 41 54 45 20 42 69 74 76 65 63 20 2a  PRIVATE Bitvec *
75f50 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65  sqlite3BitvecCre
75f60 61 74 65 28 75 33 32 29 3b 0a 53 51 4c 49 54 45  ate(u32);.SQLITE
75f70 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
75f80 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 42  ite3BitvecTest(B
75f90 69 74 76 65 63 2a 2c 20 75 33 32 29 3b 0a 53 51  itvec*, u32);.SQ
75fa0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
75fb0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53 65   sqlite3BitvecSe
75fc0 74 28 42 69 74 76 65 63 2a 2c 20 75 33 32 29 3b  t(Bitvec*, u32);
75fd0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
75fe0 76 6f 69 64 20 73 71 6c 69 74 65 33 42 69 74 76  void sqlite3Bitv
75ff0 65 63 43 6c 65 61 72 28 42 69 74 76 65 63 2a 2c  ecClear(Bitvec*,
76000 20 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52   u32);.SQLITE_PR
76010 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
76020 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28  e3BitvecDestroy(
76030 42 69 74 76 65 63 2a 29 3b 0a 53 51 4c 49 54 45  Bitvec*);.SQLITE
76040 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
76050 69 74 65 33 42 69 74 76 65 63 42 75 69 6c 74 69  ite3BitvecBuilti
76060 6e 54 65 73 74 28 69 6e 74 2c 69 6e 74 2a 29 3b  nTest(int,int*);
76070 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
76080 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65   void sqlite3Cre
76090 61 74 65 56 69 65 77 28 50 61 72 73 65 2a 2c 54  ateView(Parse*,T
760a0 6f 6b 65 6e 2a 2c 54 6f 6b 65 6e 2a 2c 54 6f 6b  oken*,Token*,Tok
760b0 65 6e 2a 2c 53 65 6c 65 63 74 2a 2c 69 6e 74 2c  en*,Select*,int,
760c0 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
760d0 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
760e0 56 49 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65  VIEW) || !define
760f0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
76100 52 54 55 41 4c 54 41 42 4c 45 29 0a 53 51 4c 49  RTUALTABLE).SQLI
76110 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
76120 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74 43   sqlite3ViewGetC
76130 6f 6c 75 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65  olumnNames(Parse
76140 2a 2c 54 61 62 6c 65 2a 29 3b 0a 23 65 6c 73 65  *,Table*);.#else
76150 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
76160 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61  3ViewGetColumnNa
76170 6d 65 73 28 41 2c 42 29 20 30 0a 23 65 6e 64 69  mes(A,B) 0.#endi
76180 66 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  f..SQLITE_PRIVAT
76190 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72  E void sqlite3Dr
761a0 6f 70 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20  opTable(Parse*, 
761b0 53 72 63 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 69  SrcList*, int, i
761c0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
761d0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
761e0 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62 6c  DeleteTable(Tabl
761f0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
76200 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
76210 49 6e 73 65 72 74 28 50 61 72 73 65 2a 2c 20 53  Insert(Parse*, S
76220 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 4c 69 73  rcList*, ExprLis
76230 74 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 49 64 4c  t*, Select*, IdL
76240 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  ist*, int);.SQLI
76250 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
76260 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c  *sqlite3ArrayAll
76270 6f 63 61 74 65 28 73 71 6c 69 74 65 33 2a 2c 76  ocate(sqlite3*,v
76280 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 69 6e 74  oid*,int,int,int
76290 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 53 51  *,int*,int*);.SQ
762a0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c  LITE_PRIVATE IdL
762b0 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69  ist *sqlite3IdLi
762c0 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74 65 33  stAppend(sqlite3
762d0 2a 2c 20 49 64 4c 69 73 74 2a 2c 20 54 6f 6b 65  *, IdList*, Toke
762e0 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  n*);.SQLITE_PRIV
762f0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
76300 64 4c 69 73 74 49 6e 64 65 78 28 49 64 4c 69 73  dListIndex(IdLis
76310 74 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  t*,const char*);
76320 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76330 53 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33  SrcList *sqlite3
76340 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 73  SrcListEnlarge(s
76350 71 6c 69 74 65 33 2a 2c 20 53 72 63 4c 69 73 74  qlite3*, SrcList
76360 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51  *, int, int);.SQ
76370 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63  LITE_PRIVATE Src
76380 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63  List *sqlite3Src
76390 4c 69 73 74 41 70 70 65 6e 64 28 73 71 6c 69 74  ListAppend(sqlit
763a0 65 33 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54  e3*, SrcList*, T
763b0 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  oken*, Token*);.
763c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
763d0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
763e0 72 63 4c 69 73 74 41 70 70 65 6e 64 46 72 6f 6d  rcListAppendFrom
763f0 54 65 72 6d 28 50 61 72 73 65 2a 2c 20 53 72 63  Term(Parse*, Src
76400 4c 69 73 74 2a 2c 20 54 6f 6b 65 6e 2a 2c 20 54  List*, Token*, T
76410 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20  oken*,.         
76420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
76430 20 20 20 20 20 20 20 20 20 20 20 20 20 54 6f 6b               Tok
76440 65 6e 2a 2c 20 53 65 6c 65 63 74 2a 2c 20 45 78  en*, Select*, Ex
76450 70 72 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53  pr*, IdList*);.S
76460 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
76470 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
76480 74 49 6e 64 65 78 65 64 42 79 28 50 61 72 73 65  tIndexedBy(Parse
76490 20 2a 2c 20 53 72 63 4c 69 73 74 20 2a 2c 20 54   *, SrcList *, T
764a0 6f 6b 65 6e 20 2a 29 3b 0a 53 51 4c 49 54 45 5f  oken *);.SQLITE_
764b0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
764c0 74 65 33 49 6e 64 65 78 65 64 42 79 4c 6f 6f 6b  te3IndexedByLook
764d0 75 70 28 50 61 72 73 65 20 2a 2c 20 73 74 72 75  up(Parse *, stru
764e0 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
764f0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
76500 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
76510 72 63 4c 69 73 74 53 68 69 66 74 4a 6f 69 6e 54  rcListShiftJoinT
76520 79 70 65 28 53 72 63 4c 69 73 74 2a 29 3b 0a 53  ype(SrcList*);.S
76530 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
76540 69 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  id sqlite3SrcLis
76550 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28 50  tAssignCursors(P
76560 61 72 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 29  arse*, SrcList*)
76570 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76580 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 64 4c   void sqlite3IdL
76590 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69 74 65  istDelete(sqlite
765a0 33 2a 2c 20 49 64 4c 69 73 74 2a 29 3b 0a 53 51  3*, IdList*);.SQ
765b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
765c0 64 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  d sqlite3SrcList
765d0 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c  Delete(sqlite3*,
765e0 20 53 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49   SrcList*);.SQLI
765f0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
76600 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
76610 65 78 28 50 61 72 73 65 2a 2c 54 6f 6b 65 6e 2a  ex(Parse*,Token*
76620 2c 54 6f 6b 65 6e 2a 2c 53 72 63 4c 69 73 74 2a  ,Token*,SrcList*
76630 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 54  ,ExprList*,int,T
76640 6f 6b 65 6e 2a 2c 0a 20 20 20 20 20 20 20 20 20  oken*,.         
76650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
76660 6f 6b 65 6e 2a 2c 20 69 6e 74 2c 20 69 6e 74 29  oken*, int, int)
76670 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76680 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 72 6f   void sqlite3Dro
76690 70 49 6e 64 65 78 28 50 61 72 73 65 2a 2c 20 53  pIndex(Parse*, S
766a0 72 63 4c 69 73 74 2a 2c 20 69 6e 74 29 3b 0a 53  rcList*, int);.S
766b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
766c0 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  t sqlite3Select(
766d0 50 61 72 73 65 2a 2c 20 53 65 6c 65 63 74 2a 2c  Parse*, Select*,
766e0 20 53 65 6c 65 63 74 44 65 73 74 2a 29 3b 0a 53   SelectDest*);.S
766f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65  QLITE_PRIVATE Se
76700 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c  lect *sqlite3Sel
76710 65 63 74 4e 65 77 28 50 61 72 73 65 2a 2c 45 78  ectNew(Parse*,Ex
76720 70 72 4c 69 73 74 2a 2c 53 72 63 4c 69 73 74 2a  prList*,SrcList*
76730 2c 45 78 70 72 2a 2c 45 78 70 72 4c 69 73 74 2a  ,Expr*,ExprList*
76740 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
76750 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72 2a             Expr*
76760 2c 45 78 70 72 4c 69 73 74 2a 2c 69 6e 74 2c 45  ,ExprList*,int,E
76770 78 70 72 2a 2c 45 78 70 72 2a 29 3b 0a 53 51 4c  xpr*,Expr*);.SQL
76780 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76790 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
767a0 6c 65 74 65 28 73 71 6c 69 74 65 33 2a 2c 20 53  lete(sqlite3*, S
767b0 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  elect*);.SQLITE_
767c0 50 52 49 56 41 54 45 20 54 61 62 6c 65 20 2a 73  PRIVATE Table *s
767d0 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f 6f  qlite3SrcListLoo
767e0 6b 75 70 28 50 61 72 73 65 2a 2c 20 53 72 63 4c  kup(Parse*, SrcL
767f0 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ist*);.SQLITE_PR
76800 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
76810 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61 72 73  3IsReadOnly(Pars
76820 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29  e*, Table*, int)
76830 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76840 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4f 70 65   void sqlite3Ope
76850 6e 54 61 62 6c 65 28 50 61 72 73 65 2a 2c 20 69  nTable(Parse*, i
76860 6e 74 20 69 43 75 72 2c 20 69 6e 74 20 69 44 62  nt iCur, int iDb
76870 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 29 3b 0a  , Table*, int);.
76880 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
76890 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54 45  TE_ENABLE_UPDATE
768a0 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20 26  _DELETE_LIMIT) &
768b0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
768c0 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29  E_OMIT_SUBQUERY)
768d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
768e0 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69 6d  Expr *sqlite3Lim
768f0 69 74 57 68 65 72 65 28 50 61 72 73 65 20 2a 2c  itWhere(Parse *,
76900 20 53 72 63 4c 69 73 74 20 2a 2c 20 45 78 70 72   SrcList *, Expr
76910 20 2a 2c 20 45 78 70 72 4c 69 73 74 20 2a 2c 20   *, ExprList *, 
76920 45 78 70 72 20 2a 2c 20 45 78 70 72 20 2a 2c 20  Expr *, Expr *, 
76930 63 68 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a  char *);.#endif.
76940 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
76950 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74  oid sqlite3Delet
76960 65 46 72 6f 6d 28 50 61 72 73 65 2a 2c 20 53 72  eFrom(Parse*, Sr
76970 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 29 3b 0a  cList*, Expr*);.
76980 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
76990 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74  oid sqlite3Updat
769a0 65 28 50 61 72 73 65 2a 2c 20 53 72 63 4c 69 73  e(Parse*, SrcLis
769b0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 45  t*, ExprList*, E
769c0 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  xpr*, int);.SQLI
769d0 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65  TE_PRIVATE Where
769e0 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65  Info *sqlite3Whe
769f0 72 65 42 65 67 69 6e 28 50 61 72 73 65 2a 2c 20  reBegin(Parse*, 
76a00 53 72 63 4c 69 73 74 2a 2c 20 45 78 70 72 2a 2c  SrcList*, Expr*,
76a10 20 45 78 70 72 4c 69 73 74 2a 2a 2c 20 75 38 29   ExprList**, u8)
76a20 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76a30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 57 68 65   void sqlite3Whe
76a40 72 65 45 6e 64 28 57 68 65 72 65 49 6e 66 6f 2a  reEnd(WhereInfo*
76a50 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
76a60 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70  E int sqlite3Exp
76a70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d 6e 28 50  rCodeGetColumn(P
76a80 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69  arse*, Table*, i
76a90 6e 74 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  nt, int, int, in
76aa0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76ab0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
76ac0 78 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73  xprCodeMove(Pars
76ad0 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
76ae0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76af0 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
76b00 78 70 72 43 6f 64 65 43 6f 70 79 28 50 61 72 73  xprCodeCopy(Pars
76b10 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  e*, int, int, in
76b20 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76b30 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
76b40 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43 61  xprClearColumnCa
76b50 63 68 65 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  che(Parse*, int)
76b60 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76b70 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
76b80 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
76b90 61 6e 67 65 28 50 61 72 73 65 2a 2c 20 69 6e 74  ange(Parse*, int
76ba0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
76bb0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
76bc0 65 33 45 78 70 72 57 72 69 74 61 62 6c 65 52 65  e3ExprWritableRe
76bd0 67 69 73 74 65 72 28 50 61 72 73 65 2a 2c 69 6e  gister(Parse*,in
76be0 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  t,int);.SQLITE_P
76bf0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
76c00 74 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28  te3ExprHardCopy(
76c10 50 61 72 73 65 2a 2c 69 6e 74 2c 69 6e 74 29 3b  Parse*,int,int);
76c20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76c30 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
76c40 6f 64 65 28 50 61 72 73 65 2a 2c 20 45 78 70 72  ode(Parse*, Expr
76c50 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
76c60 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
76c70 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
76c80 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
76c90 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  nt*);.SQLITE_PRI
76ca0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
76cb0 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 50  ExprCodeTarget(P
76cc0 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
76cd0 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76ce0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
76cf0 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28 50  prCodeAndCache(P
76d00 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69 6e  arse*, Expr*, in
76d10 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
76d20 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45  TE void sqlite3E
76d30 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74 73  xprCodeConstants
76d40 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 29 3b  (Parse*, Expr*);
76d50 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
76d60 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72 43  int sqlite3ExprC
76d70 6f 64 65 45 78 70 72 4c 69 73 74 28 50 61 72 73  odeExprList(Pars
76d80 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 69  e*, ExprList*, i
76d90 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
76da0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
76db0 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
76dc0 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20 69  Parse*, Expr*, i
76dd0 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
76de0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
76df0 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65  lite3ExprIfFalse
76e00 28 50 61 72 73 65 2a 2c 20 45 78 70 72 2a 2c 20  (Parse*, Expr*, 
76e10 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
76e20 45 5f 50 52 49 56 41 54 45 20 54 61 62 6c 65 20  E_PRIVATE Table 
76e30 2a 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c  *sqlite3FindTabl
76e40 65 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  e(sqlite3*,const
76e50 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68   char*, const ch
76e60 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ar*);.SQLITE_PRI
76e70 56 41 54 45 20 54 61 62 6c 65 20 2a 73 71 6c 69  VATE Table *sqli
76e80 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 50  te3LocateTable(P
76e90 61 72 73 65 2a 2c 69 6e 74 20 69 73 56 69 65 77  arse*,int isView
76ea0 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 20 63 6f  ,const char*, co
76eb0 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
76ec0 54 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78  TE_PRIVATE Index
76ed0 20 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64   *sqlite3FindInd
76ee0 65 78 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73  ex(sqlite3*,cons
76ef0 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
76f00 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
76f10 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
76f20 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
76f30 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 2a 2c  eTable(sqlite3*,
76f40 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29  int,const char*)
76f50 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
76f60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 55 6e 6c   void sqlite3Unl
76f70 69 6e 6b 41 6e 64 44 65 6c 65 74 65 49 6e 64 65  inkAndDeleteInde
76f80 78 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c 63  x(sqlite3*,int,c
76f90 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
76fa0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
76fb0 20 73 71 6c 69 74 65 33 56 61 63 75 75 6d 28 50   sqlite3Vacuum(P
76fc0 61 72 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  arse*);.SQLITE_P
76fd0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
76fe0 65 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72  e3RunVacuum(char
76ff0 2a 2a 2c 20 73 71 6c 69 74 65 33 2a 29 3b 0a 53  **, sqlite3*);.S
77000 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
77010 61 72 20 2a 73 71 6c 69 74 65 33 4e 61 6d 65 46  ar *sqlite3NameF
77020 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69 74 65 33  romToken(sqlite3
77030 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
77040 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
77050 71 6c 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72  qlite3ExprCompar
77060 65 28 45 78 70 72 2a 2c 20 45 78 70 72 2a 29 3b  e(Expr*, Expr*);
77070 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77080 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
77090 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65  AnalyzeAggregate
770a0 73 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20  s(NameContext*, 
770b0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
770c0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
770d0 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67  te3ExprAnalyzeAg
770e0 67 4c 69 73 74 28 4e 61 6d 65 43 6f 6e 74 65 78  gList(NameContex
770f0 74 2a 2c 45 78 70 72 4c 69 73 74 2a 29 3b 0a 53  t*,ExprList*);.S
77100 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
77110 62 65 20 2a 73 71 6c 69 74 65 33 47 65 74 56 64  be *sqlite3GetVd
77120 62 65 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  be(Parse*);.SQLI
77130 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20  TE_PRIVATE Expr 
77140 2a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 64  *sqlite3CreateId
77150 45 78 70 72 28 50 61 72 73 65 20 2a 2c 20 63 6f  Expr(Parse *, co
77160 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c 49  nst char*);.SQLI
77170 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
77180 73 71 6c 69 74 65 33 50 72 6e 67 53 61 76 65 53  sqlite3PrngSaveS
77190 74 61 74 65 28 76 6f 69 64 29 3b 0a 53 51 4c 49  tate(void);.SQLI
771a0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
771b0 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f  sqlite3PrngResto
771c0 72 65 53 74 61 74 65 28 76 6f 69 64 29 3b 0a 53  reState(void);.S
771d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
771e0 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65  id sqlite3PrngRe
771f0 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 3b 0a  setState(void);.
77200 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
77210 6f 69 64 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62  oid sqlite3Rollb
77220 61 63 6b 41 6c 6c 28 73 71 6c 69 74 65 33 2a 29  ackAll(sqlite3*)
77230 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
77240 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 64   void sqlite3Cod
77250 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50 61  eVerifySchema(Pa
77260 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  rse*, int);.SQLI
77270 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
77280 73 71 6c 69 74 65 33 42 65 67 69 6e 54 72 61 6e  sqlite3BeginTran
77290 73 61 63 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20  saction(Parse*, 
772a0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
772b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
772c0 33 43 6f 6d 6d 69 74 54 72 61 6e 73 61 63 74 69  3CommitTransacti
772d0 6f 6e 28 50 61 72 73 65 2a 29 3b 0a 53 51 4c 49  on(Parse*);.SQLI
772e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
772f0 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54  sqlite3RollbackT
77300 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65  ransaction(Parse
77310 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
77320 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78  TE int sqlite3Ex
77330 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78 70  prIsConstant(Exp
77340 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
77350 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
77360 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74  xprIsConstantNot
77370 4a 6f 69 6e 28 45 78 70 72 2a 29 3b 0a 53 51 4c  Join(Expr*);.SQL
77380 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
77390 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
773a0 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
773b0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
773c0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
773d0 65 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28  e3ExprIsInteger(
773e0 45 78 70 72 2a 2c 20 69 6e 74 2a 29 3b 0a 53 51  Expr*, int*);.SQ
773f0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
77400 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
77410 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51  const char*);.SQ
77420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
77430 64 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74  d sqlite3Generat
77440 65 52 6f 77 44 65 6c 65 74 65 28 50 61 72 73 65  eRowDelete(Parse
77450 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20  *, Table*, int, 
77460 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
77470 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
77480 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 52 6f  qlite3GenerateRo
77490 77 49 6e 64 65 78 44 65 6c 65 74 65 28 50 61 72  wIndexDelete(Par
774a0 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 69 6e 74  se*, Table*, int
774b0 2c 20 69 6e 74 2a 29 3b 0a 53 51 4c 49 54 45 5f  , int*);.SQLITE_
774c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
774d0 74 65 33 47 65 6e 65 72 61 74 65 49 6e 64 65 78  te3GenerateIndex
774e0 4b 65 79 28 50 61 72 73 65 2a 2c 20 49 6e 64 65  Key(Parse*, Inde
774f0 78 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e  x*, int, int, in
77500 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
77510 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47  TE void sqlite3G
77520 65 6e 65 72 61 74 65 43 6f 6e 73 74 72 61 69 6e  enerateConstrain
77530 74 43 68 65 63 6b 73 28 50 61 72 73 65 2a 2c 54  tChecks(Parse*,T
77540 61 62 6c 65 2a 2c 69 6e 74 2c 69 6e 74 2c 0a 20  able*,int,int,. 
77550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77560 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77570 20 20 20 20 69 6e 74 2a 2c 69 6e 74 2c 69 6e 74      int*,int,int
77580 2c 69 6e 74 2c 69 6e 74 29 3b 0a 53 51 4c 49 54  ,int,int);.SQLIT
77590 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
775a0 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74 65 49 6e  qlite3CompleteIn
775b0 73 65 72 74 69 6f 6e 28 50 61 72 73 65 2a 2c 20  sertion(Parse*, 
775c0 54 61 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Table*, int, int
775d0 2c 20 69 6e 74 2a 2c 20 69 6e 74 2c 20 69 6e 74  , int*, int, int
775e0 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
775f0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
77600 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e  e3OpenTableAndIn
77610 64 69 63 65 73 28 50 61 72 73 65 2a 2c 20 54 61  dices(Parse*, Ta
77620 62 6c 65 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b  ble*, int, int);
77630 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
77640 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65 67 69  void sqlite3Begi
77650 6e 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28  nWriteOperation(
77660 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 69 6e 74  Parse*, int, int
77670 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
77680 45 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 45  E Expr *sqlite3E
77690 78 70 72 44 75 70 28 73 71 6c 69 74 65 33 2a 2c  xprDup(sqlite3*,
776a0 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
776b0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
776c0 74 65 33 54 6f 6b 65 6e 43 6f 70 79 28 73 71 6c  te3TokenCopy(sql
776d0 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20 54 6f  ite3*,Token*, To
776e0 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ken*);.SQLITE_PR
776f0 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20 2a  IVATE ExprList *
77700 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
77710 75 70 28 73 71 6c 69 74 65 33 2a 2c 45 78 70 72  up(sqlite3*,Expr
77720 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  List*);.SQLITE_P
77730 52 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a  RIVATE SrcList *
77740 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75  sqlite3SrcListDu
77750 70 28 73 71 6c 69 74 65 33 2a 2c 53 72 63 4c 69  p(sqlite3*,SrcLi
77760 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
77770 56 41 54 45 20 49 64 4c 69 73 74 20 2a 73 71 6c  VATE IdList *sql
77780 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 73 71  ite3IdListDup(sq
77790 6c 69 74 65 33 2a 2c 49 64 4c 69 73 74 2a 29 3b  lite3*,IdList*);
777a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
777b0 53 65 6c 65 63 74 20 2a 73 71 6c 69 74 65 33 53  Select *sqlite3S
777c0 65 6c 65 63 74 44 75 70 28 73 71 6c 69 74 65 33  electDup(sqlite3
777d0 2a 2c 53 65 6c 65 63 74 2a 29 3b 0a 53 51 4c 49  *,Select*);.SQLI
777e0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
777f0 73 71 6c 69 74 65 33 46 75 6e 63 44 65 66 49 6e  sqlite3FuncDefIn
77800 73 65 72 74 28 46 75 6e 63 44 65 66 48 61 73 68  sert(FuncDefHash
77810 2a 2c 20 46 75 6e 63 44 65 66 2a 29 3b 0a 53 51  *, FuncDef*);.SQ
77820 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
77830 63 44 65 66 20 2a 73 71 6c 69 74 65 33 46 69 6e  cDef *sqlite3Fin
77840 64 46 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65  dFunction(sqlite
77850 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69  3*,const char*,i
77860 6e 74 2c 69 6e 74 2c 75 38 2c 69 6e 74 29 3b 0a  nt,int,u8,int);.
77870 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
77880 6f 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73  oid sqlite3Regis
77890 74 65 72 42 75 69 6c 74 69 6e 46 75 6e 63 74 69  terBuiltinFuncti
778a0 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b 0a 53  ons(sqlite3*);.S
778b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
778c0 69 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  id sqlite3Regist
778d0 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
778e0 6f 6e 73 28 76 6f 69 64 29 3b 0a 53 51 4c 49 54  ons(void);.SQLIT
778f0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
77900 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47 6c  qlite3RegisterGl
77910 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f  obalFunctions(vo
77920 69 64 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  id);.#ifdef SQLI
77930 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f  TE_DEBUG.SQLITE_
77940 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
77950 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71  lite3SafetyOn(sq
77960 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f  lite3*);.SQLITE_
77970 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73 71  PRIVATE   int sq
77980 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 73  lite3SafetyOff(s
77990 71 6c 69 74 65 33 2a 29 3b 0a 23 65 6c 73 65 0a  qlite3*);.#else.
779a0 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
779b0 53 61 66 65 74 79 4f 6e 28 41 29 20 30 0a 23 20  SafetyOn(A) 0.# 
779c0 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 53 61  define sqlite3Sa
779d0 66 65 74 79 4f 66 66 28 41 29 20 30 0a 23 65 6e  fetyOff(A) 0.#en
779e0 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  dif.SQLITE_PRIVA
779f0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61  TE int sqlite3Sa
77a00 66 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69  fetyCheckOk(sqli
77a10 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
77a20 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
77a30 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
77a40 4f 72 4f 6b 28 73 71 6c 69 74 65 33 2a 29 3b 0a  OrOk(sqlite3*);.
77a50 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
77a60 6f 69 64 20 73 71 6c 69 74 65 33 43 68 61 6e 67  oid sqlite3Chang
77a70 65 43 6f 6f 6b 69 65 28 50 61 72 73 65 2a 2c 20  eCookie(Parse*, 
77a80 69 6e 74 29 3b 0a 0a 23 69 66 20 21 64 65 66 69  int);..#if !defi
77a90 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
77aa0 56 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65  VIEW) && !define
77ab0 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  d(SQLITE_OMIT_TR
77ac0 49 47 47 45 52 29 0a 53 51 4c 49 54 45 5f 50 52  IGGER).SQLITE_PR
77ad0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
77ae0 65 33 4d 61 74 65 72 69 61 6c 69 7a 65 56 69 65  e3MaterializeVie
77af0 77 28 50 61 72 73 65 2a 2c 20 54 61 62 6c 65 2a  w(Parse*, Table*
77b00 2c 20 45 78 70 72 2a 2c 20 69 6e 74 29 3b 0a 23  , Expr*, int);.#
77b10 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
77b20 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
77b30 45 52 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ER.SQLITE_PRIVAT
77b40 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
77b50 42 65 67 69 6e 54 72 69 67 67 65 72 28 50 61 72  BeginTrigger(Par
77b60 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 54 6f 6b 65  se*, Token*,Toke
77b70 6e 2a 2c 69 6e 74 2c 69 6e 74 2c 49 64 4c 69 73  n*,int,int,IdLis
77b80 74 2a 2c 53 72 63 4c 69 73 74 2a 2c 0a 20 20 20  t*,SrcList*,.   
77b90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77ba0 20 20 20 20 20 20 20 20 45 78 70 72 2a 2c 69 6e          Expr*,in
77bb0 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t, int);.SQLITE_
77bc0 50 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73  PRIVATE   void s
77bd0 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72 69 67  qlite3FinishTrig
77be0 67 65 72 28 50 61 72 73 65 2a 2c 20 54 72 69 67  ger(Parse*, Trig
77bf0 67 65 72 53 74 65 70 2a 2c 20 54 6f 6b 65 6e 2a  gerStep*, Token*
77c00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
77c10 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33  E   void sqlite3
77c20 44 72 6f 70 54 72 69 67 67 65 72 28 50 61 72 73  DropTrigger(Pars
77c30 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 69 6e  e*, SrcList*, in
77c40 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
77c50 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
77c60 33 44 72 6f 70 54 72 69 67 67 65 72 50 74 72 28  3DropTriggerPtr(
77c70 50 61 72 73 65 2a 2c 20 54 72 69 67 67 65 72 2a  Parse*, Trigger*
77c80 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
77c90 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33 54  E   int sqlite3T
77ca0 72 69 67 67 65 72 73 45 78 69 73 74 28 54 61 62  riggersExist(Tab
77cb0 6c 65 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69  le*, int, ExprLi
77cc0 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
77cd0 56 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74  VATE   int sqlit
77ce0 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72  e3CodeRowTrigger
77cf0 28 50 61 72 73 65 2a 2c 20 69 6e 74 2c 20 45 78  (Parse*, int, Ex
77d00 70 72 4c 69 73 74 2a 2c 20 69 6e 74 2c 20 54 61  prList*, int, Ta
77d10 62 6c 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c  ble *, int, int,
77d20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
77d30 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
77d40 2c 20 69 6e 74 2c 20 75 33 32 2a 2c 20 75 33 32  , int, u32*, u32
77d50 2a 29 3b 0a 20 20 76 6f 69 64 20 73 71 6c 69 74  *);.  void sqlit
77d60 65 56 69 65 77 54 72 69 67 67 65 72 73 28 50 61  eViewTriggers(Pa
77d70 72 73 65 2a 2c 20 54 61 62 6c 65 2a 2c 20 45 78  rse*, Table*, Ex
77d80 70 72 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69  pr*, int, ExprLi
77d90 73 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  st*);.SQLITE_PRI
77da0 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
77db0 74 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72  te3DeleteTrigger
77dc0 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 20 54  Step(sqlite3*, T
77dd0 72 69 67 67 65 72 53 74 65 70 2a 29 3b 0a 53 51  riggerStep*);.SQ
77de0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 54  LITE_PRIVATE   T
77df0 72 69 67 67 65 72 53 74 65 70 20 2a 73 71 6c 69  riggerStep *sqli
77e00 74 65 33 54 72 69 67 67 65 72 53 65 6c 65 63 74  te3TriggerSelect
77e10 53 74 65 70 28 73 71 6c 69 74 65 33 2a 2c 53 65  Step(sqlite3*,Se
77e20 6c 65 63 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  lect*);.SQLITE_P
77e30 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
77e40 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
77e50 67 67 65 72 49 6e 73 65 72 74 53 74 65 70 28 73  ggerInsertStep(s
77e60 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20  qlite3*,Token*, 
77e70 49 64 4c 69 73 74 2a 2c 0a 20 20 20 20 20 20 20  IdList*,.       
77e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77e90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
77ea0 20 45 78 70 72 4c 69 73 74 2a 2c 53 65 6c 65 63   ExprList*,Selec
77eb0 74 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  t*,int);.SQLITE_
77ec0 50 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65  PRIVATE   Trigge
77ed0 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
77ee0 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70 28  iggerUpdateStep(
77ef0 73 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c  sqlite3*,Token*,
77f00 45 78 70 72 4c 69 73 74 2a 2c 20 45 78 70 72 2a  ExprList*, Expr*
77f10 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
77f20 52 49 56 41 54 45 20 20 20 54 72 69 67 67 65 72  RIVATE   Trigger
77f30 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72 69  Step *sqlite3Tri
77f40 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28 73  ggerDeleteStep(s
77f50 71 6c 69 74 65 33 2a 2c 54 6f 6b 65 6e 2a 2c 20  qlite3*,Token*, 
77f60 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Expr*);.SQLITE_P
77f70 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
77f80 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67 67  lite3DeleteTrigg
77f90 65 72 28 73 71 6c 69 74 65 33 2a 2c 20 54 72 69  er(sqlite3*, Tri
77fa0 67 67 65 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  gger*);.SQLITE_P
77fb0 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
77fc0 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65  lite3UnlinkAndDe
77fd0 6c 65 74 65 54 72 69 67 67 65 72 28 73 71 6c 69  leteTrigger(sqli
77fe0 74 65 33 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  te3*,int,const c
77ff0 68 61 72 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64  har*);.#else.# d
78000 65 66 69 6e 65 20 73 71 6c 69 74 65 33 54 72 69  efine sqlite3Tri
78010 67 67 65 72 73 45 78 69 73 74 28 42 2c 43 2c 44  ggersExist(B,C,D
78020 2c 45 2c 46 29 20 30 0a 23 20 64 65 66 69 6e 65  ,E,F) 0.# define
78030 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
78040 69 67 67 65 72 28 41 2c 42 29 0a 23 20 64 65 66  igger(A,B).# def
78050 69 6e 65 20 73 71 6c 69 74 65 33 44 72 6f 70 54  ine sqlite3DropT
78060 72 69 67 67 65 72 50 74 72 28 41 2c 42 29 0a 23  riggerPtr(A,B).#
78070 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 55   define sqlite3U
78080 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65 54 72  nlinkAndDeleteTr
78090 69 67 67 65 72 28 41 2c 42 2c 43 29 0a 23 20 64  igger(A,B,C).# d
780a0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 43 6f 64  efine sqlite3Cod
780b0 65 52 6f 77 54 72 69 67 67 65 72 28 41 2c 42 2c  eRowTrigger(A,B,
780c0 43 2c 44 2c 45 2c 46 2c 47 2c 48 2c 49 2c 4a 2c  C,D,E,F,G,H,I,J,
780d0 4b 29 20 30 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  K) 0.#endif..SQL
780e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
780f0 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79 70 65 28  sqlite3JoinType(
78100 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c 20  Parse*, Token*, 
78110 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 29 3b  Token*, Token*);
78120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78130 76 6f 69 64 20 73 71 6c 69 74 65 33 43 72 65 61  void sqlite3Crea
78140 74 65 46 6f 72 65 69 67 6e 4b 65 79 28 50 61 72  teForeignKey(Par
78150 73 65 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20  se*, ExprList*, 
78160 54 6f 6b 65 6e 2a 2c 20 45 78 70 72 4c 69 73 74  Token*, ExprList
78170 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  *, int);.SQLITE_
78180 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
78190 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e  ite3DeferForeign
781a0 4b 65 79 28 50 61 72 73 65 2a 2c 20 69 6e 74 29  Key(Parse*, int)
781b0 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
781c0 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
781d0 49 4f 4e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ION.SQLITE_PRIVA
781e0 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
781f0 33 41 75 74 68 52 65 61 64 28 50 61 72 73 65 2a  3AuthRead(Parse*
78200 2c 45 78 70 72 2a 2c 53 63 68 65 6d 61 2a 2c 53  ,Expr*,Schema*,S
78210 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
78220 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
78230 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
78240 50 61 72 73 65 2a 2c 69 6e 74 2c 20 63 6f 6e 73  Parse*,int, cons
78250 74 20 63 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63  t char*, const c
78260 68 61 72 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  har*, const char
78270 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
78280 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65  TE   void sqlite
78290 33 41 75 74 68 43 6f 6e 74 65 78 74 50 75 73 68  3AuthContextPush
782a0 28 50 61 72 73 65 2a 2c 20 41 75 74 68 43 6f 6e  (Parse*, AuthCon
782b0 74 65 78 74 2a 2c 20 63 6f 6e 73 74 20 63 68 61  text*, const cha
782c0 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
782d0 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
782e0 65 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70  e3AuthContextPop
782f0 28 41 75 74 68 43 6f 6e 74 65 78 74 2a 29 3b 0a  (AuthContext*);.
78300 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 73  #else.# define s
78310 71 6c 69 74 65 33 41 75 74 68 52 65 61 64 28 61  qlite3AuthRead(a
78320 2c 62 2c 63 2c 64 29 0a 23 20 64 65 66 69 6e 65  ,b,c,d).# define
78330 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
78340 6b 28 61 2c 62 2c 63 2c 64 2c 65 29 20 20 20 20  k(a,b,c,d,e)    
78350 53 51 4c 49 54 45 5f 4f 4b 0a 23 20 64 65 66 69  SQLITE_OK.# defi
78360 6e 65 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f  ne sqlite3AuthCo
78370 6e 74 65 78 74 50 75 73 68 28 61 2c 62 2c 63 29  ntextPush(a,b,c)
78380 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
78390 33 41 75 74 68 43 6f 6e 74 65 78 74 50 6f 70 28  3AuthContextPop(
783a0 61 29 20 20 28 28 76 6f 69 64 29 28 61 29 29 0a  a)  ((void)(a)).
783b0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
783c0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
783d0 65 33 41 74 74 61 63 68 28 50 61 72 73 65 2a 2c  e3Attach(Parse*,
783e0 20 45 78 70 72 2a 2c 20 45 78 70 72 2a 2c 20 45   Expr*, Expr*, E
783f0 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
78400 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
78410 65 33 44 65 74 61 63 68 28 50 61 72 73 65 2a 2c  e3Detach(Parse*,
78420 20 45 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f   Expr*);.SQLITE_
78430 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
78440 74 65 33 42 74 72 65 65 46 61 63 74 6f 72 79 28  te3BtreeFactory(
78450 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 20 2a 64  const sqlite3 *d
78460 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
78470 46 69 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20  Filename,.      
78480 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
78490 20 69 6e 74 20 6f 6d 69 74 4a 6f 75 72 6e 61 6c   int omitJournal
784a0 2c 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 69 6e  , int nCache, in
784b0 74 20 66 6c 61 67 73 2c 20 42 74 72 65 65 20 2a  t flags, Btree *
784c0 2a 70 70 42 74 72 65 65 29 3b 0a 53 51 4c 49 54  *ppBtree);.SQLIT
784d0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
784e0 6c 69 74 65 33 46 69 78 49 6e 69 74 28 44 62 46  lite3FixInit(DbF
784f0 69 78 65 72 2a 2c 20 50 61 72 73 65 2a 2c 20 69  ixer*, Parse*, i
78500 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 2c  nt, const char*,
78510 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a   const Token*);.
78520 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
78530 6e 74 20 73 71 6c 69 74 65 33 46 69 78 53 72 63  nt sqlite3FixSrc
78540 4c 69 73 74 28 44 62 46 69 78 65 72 2a 2c 20 53  List(DbFixer*, S
78550 72 63 4c 69 73 74 2a 29 3b 0a 53 51 4c 49 54 45  rcList*);.SQLITE
78560 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
78570 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 44 62  ite3FixSelect(Db
78580 46 69 78 65 72 2a 2c 20 53 65 6c 65 63 74 2a 29  Fixer*, Select*)
78590 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
785a0 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 45   int sqlite3FixE
785b0 78 70 72 28 44 62 46 69 78 65 72 2a 2c 20 45 78  xpr(DbFixer*, Ex
785c0 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  pr*);.SQLITE_PRI
785d0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
785e0 46 69 78 45 78 70 72 4c 69 73 74 28 44 62 46 69  FixExprList(DbFi
785f0 78 65 72 2a 2c 20 45 78 70 72 4c 69 73 74 2a 29  xer*, ExprList*)
78600 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
78610 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54   int sqlite3FixT
78620 72 69 67 67 65 72 53 74 65 70 28 44 62 46 69 78  riggerStep(DbFix
78630 65 72 2a 2c 20 54 72 69 67 67 65 72 53 74 65 70  er*, TriggerStep
78640 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
78650 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
78660 6f 46 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  oF(const char *z
78670 2c 20 64 6f 75 62 6c 65 2a 29 3b 0a 53 51 4c 49  , double*);.SQLI
78680 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
78690 71 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 63  qlite3GetInt32(c
786a0 6f 6e 73 74 20 63 68 61 72 20 2a 2c 20 69 6e 74  onst char *, int
786b0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
786c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
786d0 74 73 49 6e 36 34 42 69 74 73 28 63 6f 6e 73 74  tsIn64Bits(const
786e0 20 63 68 61 72 20 2a 2c 20 69 6e 74 29 3b 0a 53   char *, int);.S
786f0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
78700 74 20 73 71 6c 69 74 65 33 55 74 66 31 36 42 79  t sqlite3Utf16By
78710 74 65 4c 65 6e 28 63 6f 6e 73 74 20 76 6f 69 64  teLen(const void
78720 20 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 43 68   *pData, int nCh
78730 61 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  ar);.SQLITE_PRIV
78740 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55  ATE int sqlite3U
78750 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73 74  tf8CharLen(const
78760 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
78770 74 20 6e 42 79 74 65 29 3b 0a 53 51 4c 49 54 45  t nByte);.SQLITE
78780 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
78790 69 74 65 33 55 74 66 38 52 65 61 64 28 63 6f 6e  ite3Utf8Read(con
787a0 73 74 20 75 38 2a 2c 20 63 6f 6e 73 74 20 75 38  st u8*, const u8
787b0 2a 2c 20 63 6f 6e 73 74 20 75 38 2a 2a 29 3b 0a  *, const u8**);.
787c0 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65 73 20  ./*.** Routines 
787d0 74 6f 20 72 65 61 64 20 61 6e 64 20 77 72 69 74  to read and writ
787e0 65 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74  e variable-lengt
787f0 68 20 69 6e 74 65 67 65 72 73 2e 20 20 54 68 65  h integers.  The
78800 73 65 20 75 73 65 64 20 74 6f 0a 2a 2a 20 62 65  se used to.** be
78810 20 64 65 66 69 6e 65 64 20 6c 6f 63 61 6c 6c 79   defined locally
78820 2c 20 62 75 74 20 6e 6f 77 20 77 65 20 75 73 65  , but now we use
78830 20 74 68 65 20 76 61 72 69 6e 74 20 72 6f 75 74   the varint rout
78840 69 6e 65 73 20 69 6e 20 74 68 65 20 75 74 69 6c  ines in the util
78850 2e 63 0a 2a 2a 20 66 69 6c 65 2e 20 20 43 6f 64  .c.** file.  Cod
78860 65 20 73 68 6f 75 6c 64 20 75 73 65 20 74 68 65  e should use the
78870 20 4d 41 43 52 4f 20 66 6f 72 6d 73 20 62 65 6c   MACRO forms bel
78880 6f 77 2c 20 61 73 20 74 68 65 20 56 61 72 69 6e  ow, as the Varin
78890 74 33 32 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20  t32 versions.** 
788a0 61 72 65 20 63 6f 64 65 64 20 74 6f 20 61 73 73  are coded to ass
788b0 75 6d 65 20 74 68 65 20 73 69 6e 67 6c 65 20 62  ume the single b
788c0 79 74 65 20 63 61 73 65 20 69 73 20 61 6c 72 65  yte case is alre
788d0 61 64 79 20 68 61 6e 64 6c 65 64 20 28 77 68 69  ady handled (whi
788e0 63 68 20 0a 2a 2a 20 74 68 65 20 4d 41 43 52 4f  ch .** the MACRO
788f0 20 66 6f 72 6d 20 64 6f 65 73 29 2e 0a 2a 2f 0a   form does)..*/.
78900 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
78910 6e 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72  nt sqlite3PutVar
78920 69 6e 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61  int(unsigned cha
78930 72 2a 2c 20 75 36 34 29 3b 0a 53 51 4c 49 54 45  r*, u64);.SQLITE
78940 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
78950 69 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28  ite3PutVarint32(
78960 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 2c 20  unsigned char*, 
78970 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  u32);.SQLITE_PRI
78980 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
78990 47 65 74 56 61 72 69 6e 74 28 63 6f 6e 73 74 20  GetVarint(const 
789a0 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 2c  unsigned char *,
789b0 20 75 36 34 20 2a 29 3b 0a 53 51 4c 49 54 45 5f   u64 *);.SQLITE_
789c0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
789d0 74 65 33 47 65 74 56 61 72 69 6e 74 33 32 28 63  te3GetVarint32(c
789e0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
789f0 61 72 20 2a 2c 20 75 33 32 20 2a 29 3b 0a 53 51  ar *, u32 *);.SQ
78a00 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
78a10 20 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65   sqlite3VarintLe
78a20 6e 28 75 36 34 20 76 29 3b 0a 0a 2f 2a 0a 2a 2a  n(u64 v);../*.**
78a30 20 54 68 65 20 68 65 61 64 65 72 20 6f 66 20 61   The header of a
78a40 20 72 65 63 6f 72 64 20 63 6f 6e 73 69 73 74 73   record consists
78a50 20 6f 66 20 61 20 73 65 71 75 65 6e 63 65 20 76   of a sequence v
78a60 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69  ariable-length i
78a70 6e 74 65 67 65 72 73 2e 0a 2a 2a 20 54 68 65 73  ntegers..** Thes
78a80 65 20 69 6e 74 65 67 65 72 73 20 61 72 65 20 61  e integers are a
78a90 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 73 6d 61  lmost always sma
78aa0 6c 6c 20 61 6e 64 20 61 72 65 20 65 6e 63 6f 64  ll and are encod
78ab0 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65 20 62  ed as a single b
78ac0 79 74 65 2e 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  yte..** The foll
78ad0 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 74 61 6b  owing macros tak
78ae0 65 20 61 64 76 61 6e 74 61 67 65 20 74 68 69 73  e advantage this
78af0 20 66 61 63 74 20 74 6f 20 70 72 6f 76 69 64 65   fact to provide
78b00 20 61 20 66 61 73 74 20 65 6e 63 6f 64 65 0a 2a   a fast encode.*
78b10 2a 20 61 6e 64 20 64 65 63 6f 64 65 20 6f 66 20  * and decode of 
78b20 74 68 65 20 69 6e 74 65 67 65 72 73 20 69 6e 20  the integers in 
78b30 61 20 72 65 63 6f 72 64 20 68 65 61 64 65 72 2e  a record header.
78b40 20 20 49 74 20 69 73 20 66 61 73 74 65 72 20 66    It is faster f
78b50 6f 72 20 74 68 65 20 63 6f 6d 6d 6f 6e 0a 2a 2a  or the common.**
78b60 20 63 61 73 65 20 77 68 65 72 65 20 74 68 65 20   case where the 
78b70 69 6e 74 65 67 65 72 20 69 73 20 61 20 73 69 6e  integer is a sin
78b80 67 6c 65 20 62 79 74 65 2e 20 20 49 74 20 69 73  gle byte.  It is
78b90 20 61 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72   a little slower
78ba0 20 77 68 65 6e 20 74 68 65 0a 2a 2a 20 69 6e 74   when the.** int
78bb0 65 67 65 72 20 69 73 20 74 77 6f 20 6f 72 20 6d  eger is two or m
78bc0 6f 72 65 20 62 79 74 65 73 2e 20 20 42 75 74 20  ore bytes.  But 
78bd0 6f 76 65 72 61 6c 6c 20 69 74 20 69 73 20 66 61  overall it is fa
78be0 73 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ster..**.** The 
78bf0 66 6f 6c 6c 6f 77 69 6e 67 20 65 78 70 72 65 73  following expres
78c00 73 69 6f 6e 73 20 61 72 65 20 65 71 75 69 76 61  sions are equiva
78c10 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lent:.**.**     
78c20 78 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 61  x = sqlite3GetVa
78c30 72 69 6e 74 33 32 28 20 41 2c 20 26 42 20 29 3b  rint32( A, &B );
78c40 0a 2a 2a 20 20 20 20 20 78 20 3d 20 73 71 6c 69  .**     x = sqli
78c50 74 65 33 50 75 74 56 61 72 69 6e 74 33 32 28 20  te3PutVarint32( 
78c60 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2a 20 20 20  A, B );.**.**   
78c70 20 20 78 20 3d 20 67 65 74 56 61 72 69 6e 74 33    x = getVarint3
78c80 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 20 20 20  2( A, B );.**   
78c90 20 20 78 20 3d 20 70 75 74 56 61 72 69 6e 74 33    x = putVarint3
78ca0 32 28 20 41 2c 20 42 20 29 3b 0a 2a 2a 0a 2a 2f  2( A, B );.**.*/
78cb0 0a 23 64 65 66 69 6e 65 20 67 65 74 56 61 72 69  .#define getVari
78cc0 6e 74 33 32 28 41 2c 42 29 20 20 28 28 2a 28 41  nt32(A,B)  ((*(A
78cd0 29 3c 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  )<(unsigned char
78ce0 29 30 78 38 30 29 20 3f 20 28 28 42 29 20 3d 20  )0x80) ? ((B) = 
78cf0 28 75 33 32 29 2a 28 41 29 29 2c 31 20 3a 20 73  (u32)*(A)),1 : s
78d00 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e 74 33  qlite3GetVarint3
78d10 32 28 28 41 29 2c 20 28 75 33 32 20 2a 29 26 28  2((A), (u32 *)&(
78d20 42 29 29 29 0a 23 64 65 66 69 6e 65 20 70 75 74  B))).#define put
78d30 56 61 72 69 6e 74 33 32 28 41 2c 42 29 20 20 28  Varint32(A,B)  (
78d40 28 28 75 33 32 29 28 42 29 3c 28 75 33 32 29 30  ((u32)(B)<(u32)0
78d50 78 38 30 29 20 3f 20 28 2a 28 41 29 20 3d 20 28  x80) ? (*(A) = (
78d60 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 28 42  unsigned char)(B
78d70 29 29 2c 31 20 3a 20 73 71 6c 69 74 65 33 50 75  )),1 : sqlite3Pu
78d80 74 56 61 72 69 6e 74 33 32 28 28 41 29 2c 20 28  tVarint32((A), (
78d90 42 29 29 29 0a 23 64 65 66 69 6e 65 20 67 65 74  B))).#define get
78da0 56 61 72 69 6e 74 20 20 20 20 73 71 6c 69 74 65  Varint    sqlite
78db0 33 47 65 74 56 61 72 69 6e 74 0a 23 64 65 66 69  3GetVarint.#defi
78dc0 6e 65 20 70 75 74 56 61 72 69 6e 74 20 20 20 20  ne putVarint    
78dd0 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74  sqlite3PutVarint
78de0 0a 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  ...SQLITE_PRIVAT
78df0 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
78e00 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56  dexAffinityStr(V
78e10 64 62 65 20 2a 2c 20 49 6e 64 65 78 20 2a 29 3b  dbe *, Index *);
78e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
78e30 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61 62 6c  void sqlite3Tabl
78e40 65 41 66 66 69 6e 69 74 79 53 74 72 28 56 64 62  eAffinityStr(Vdb
78e50 65 20 2a 2c 20 54 61 62 6c 65 20 2a 29 3b 0a 53  e *, Table *);.S
78e60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68  QLITE_PRIVATE ch
78e70 61 72 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72  ar sqlite3Compar
78e80 65 41 66 66 69 6e 69 74 79 28 45 78 70 72 20 2a  eAffinity(Expr *
78e90 70 45 78 70 72 2c 20 63 68 61 72 20 61 66 66 32  pExpr, char aff2
78ea0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
78eb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 64  E int sqlite3Ind
78ec0 65 78 41 66 66 69 6e 69 74 79 4f 6b 28 45 78 70  exAffinityOk(Exp
78ed0 72 20 2a 70 45 78 70 72 2c 20 63 68 61 72 20 69  r *pExpr, char i
78ee0 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 53 51  dx_affinity);.SQ
78ef0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
78f00 72 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66  r sqlite3ExprAff
78f10 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78 70  inity(Expr *pExp
78f20 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  r);.SQLITE_PRIVA
78f30 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74  TE int sqlite3At
78f40 6f 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 2a  oi64(const char*
78f50 2c 20 69 36 34 2a 29 3b 0a 53 51 4c 49 54 45 5f  , i64*);.SQLITE_
78f60 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
78f70 69 74 65 33 45 72 72 6f 72 28 73 71 6c 69 74 65  ite3Error(sqlite
78f80 33 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63  3*, int, const c
78f90 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 53 51 4c 49 54  har*,...);.SQLIT
78fa0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a  E_PRIVATE void *
78fb0 73 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62  sqlite3HexToBlob
78fc0 28 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74  (sqlite3*, const
78fd0 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 6e 29   char *z, int n)
78fe0 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
78ff0 20 69 6e 74 20 73 71 6c 69 74 65 33 54 77 6f 50   int sqlite3TwoP
79000 61 72 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c  artName(Parse *,
79010 20 54 6f 6b 65 6e 20 2a 2c 20 54 6f 6b 65 6e 20   Token *, Token 
79020 2a 2c 20 54 6f 6b 65 6e 20 2a 2a 29 3b 0a 53 51  *, Token **);.SQ
79030 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e  LITE_PRIVATE con
79040 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
79050 45 72 72 53 74 72 28 69 6e 74 29 3b 0a 53 51 4c  ErrStr(int);.SQL
79060 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
79070 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
79080 61 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 29  a(Parse *pParse)
79090 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
790a0 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65   CollSeq *sqlite
790b0 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 73 71 6c  3FindCollSeq(sql
790c0 69 74 65 33 2a 2c 75 38 20 65 6e 63 2c 20 63 6f  ite3*,u8 enc, co
790d0 6e 73 74 20 63 68 61 72 20 2a 2c 69 6e 74 2c 69  nst char *,int,i
790e0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
790f0 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c  ATE CollSeq *sql
79100 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c 6c 53 65  ite3LocateCollSe
79110 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
79120 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
79130 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29 3b 0a  me, int nName);.
79140 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43  SQLITE_PRIVATE C
79150 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 45  ollSeq *sqlite3E
79160 78 70 72 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  xprCollSeq(Parse
79170 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
79180 70 45 78 70 72 29 3b 0a 53 51 4c 49 54 45 5f 50  pExpr);.SQLITE_P
79190 52 49 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c  RIVATE Expr *sql
791a0 69 74 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28  ite3ExprSetColl(
791b0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
791c0 78 70 72 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b  xpr *, Token *);
791d0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
791e0 69 6e 74 20 73 71 6c 69 74 65 33 43 68 65 63 6b  int sqlite3Check
791f0 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20 2a 2c  CollSeq(Parse *,
79200 20 43 6f 6c 6c 53 65 71 20 2a 29 3b 0a 53 51 4c   CollSeq *);.SQL
79210 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
79220 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65  sqlite3CheckObje
79230 63 74 4e 61 6d 65 28 50 61 72 73 65 20 2a 2c 20  ctName(Parse *, 
79240 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 53  const char *);.S
79250 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
79260 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
79270 74 43 68 61 6e 67 65 73 28 73 71 6c 69 74 65 33  tChanges(sqlite3
79280 20 2a 2c 20 69 6e 74 29 3b 0a 0a 53 51 4c 49 54   *, int);..SQLIT
79290 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
792a0 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 56 61 6c  void *sqlite3Val
792b0 75 65 54 65 78 74 28 73 71 6c 69 74 65 33 5f 76  ueText(sqlite3_v
792c0 61 6c 75 65 2a 2c 20 75 38 29 3b 0a 53 51 4c 49  alue*, u8);.SQLI
792d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
792e0 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74 65 73  qlite3ValueBytes
792f0 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2c  (sqlite3_value*,
79300 20 75 38 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   u8);.SQLITE_PRI
79310 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
79320 33 56 61 6c 75 65 53 65 74 53 74 72 28 73 71 6c  3ValueSetStr(sql
79330 69 74 65 33 5f 76 61 6c 75 65 2a 2c 20 69 6e 74  ite3_value*, int
79340 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2c 75  , const void *,u
79350 38 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  8, .            
79360 20 20 20 20 20 20 20 20 20 20 20 20 76 6f 69 64              void
79370 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c  (*)(void*));.SQL
79380 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
79390 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65   sqlite3ValueFre
793a0 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  e(sqlite3_value*
793b0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
793c0 45 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  E sqlite3_value 
793d0 2a 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77  *sqlite3ValueNew
793e0 28 73 71 6c 69 74 65 33 20 2a 29 3b 0a 53 51 4c  (sqlite3 *);.SQL
793f0 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61 72  ITE_PRIVATE char
79400 20 2a 73 71 6c 69 74 65 33 55 74 66 31 36 74 6f   *sqlite3Utf16to
79410 38 28 73 71 6c 69 74 65 33 20 2a 2c 20 63 6f 6e  8(sqlite3 *, con
79420 73 74 20 76 6f 69 64 2a 2c 20 69 6e 74 29 3b 0a  st void*, int);.
79430 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
79440 6e 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  nt sqlite3ValueF
79450 72 6f 6d 45 78 70 72 28 73 71 6c 69 74 65 33 20  romExpr(sqlite3 
79460 2a 2c 20 45 78 70 72 20 2a 2c 20 75 38 2c 20 75  *, Expr *, u8, u
79470 38 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  8, sqlite3_value
79480 20 2a 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49   **);.SQLITE_PRI
79490 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
794a0 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66 69 6e  3ValueApplyAffin
794b0 69 74 79 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  ity(sqlite3_valu
794c0 65 20 2a 2c 20 75 38 2c 20 75 38 29 3b 0a 23 69  e *, u8, u8);.#i
794d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 41 4d 41  fndef SQLITE_AMA
794e0 4c 47 41 4d 41 54 49 4f 4e 0a 53 51 4c 49 54 45  LGAMATION.SQLITE
794f0 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 75  _PRIVATE const u
79500 6e 73 69 67 6e 65 64 20 63 68 61 72 20 73 71 6c  nsigned char sql
79510 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
79520 5b 5d 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  [];.SQLITE_PRIVA
79530 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  TE SQLITE_WSD st
79540 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66  ruct Sqlite3Conf
79550 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67  ig sqlite3Config
79560 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
79570 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75 6e 63   SQLITE_WSD Func
79580 44 65 66 48 61 73 68 20 73 71 6c 69 74 65 33 47  DefHash sqlite3G
79590 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 3b 0a  lobalFunctions;.
795a0 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52  #endif.SQLITE_PR
795b0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
795c0 65 33 52 6f 6f 74 50 61 67 65 4d 6f 76 65 64 28  e3RootPageMoved(
795d0 44 62 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  Db*, int, int);.
795e0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
795f0 6f 69 64 20 73 71 6c 69 74 65 33 52 65 69 6e 64  oid sqlite3Reind
79600 65 78 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e  ex(Parse*, Token
79610 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49  *, Token*);.SQLI
79620 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
79630 73 71 6c 69 74 65 33 41 6c 74 65 72 46 75 6e 63  sqlite3AlterFunc
79640 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 29 3b  tions(sqlite3*);
79650 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
79660 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
79670 72 52 65 6e 61 6d 65 54 61 62 6c 65 28 50 61 72  rRenameTable(Par
79680 73 65 2a 2c 20 53 72 63 4c 69 73 74 2a 2c 20 54  se*, SrcList*, T
79690 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  oken*);.SQLITE_P
796a0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
796b0 65 33 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74  e3GetToken(const
796c0 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
796d0 2c 20 69 6e 74 20 2a 29 3b 0a 53 51 4c 49 54 45  , int *);.SQLITE
796e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
796f0 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65  lite3NestedParse
79700 28 50 61 72 73 65 2a 2c 20 63 6f 6e 73 74 20 63  (Parse*, const c
79710 68 61 72 2a 2c 20 2e 2e 2e 29 3b 0a 53 51 4c 49  har*, ...);.SQLI
79720 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
79730 73 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65  sqlite3ExpirePre
79740 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28  paredStatements(
79750 73 71 6c 69 74 65 33 2a 29 3b 0a 53 51 4c 49 54  sqlite3*);.SQLIT
79760 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
79770 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
79780 65 63 74 28 50 61 72 73 65 20 2a 2c 20 45 78 70  ect(Parse *, Exp
79790 72 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 29 3b 0a  r *, int, int);.
797a0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
797b0 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
797c0 74 50 72 65 70 28 50 61 72 73 65 2a 2c 20 53 65  tPrep(Parse*, Se
797d0 6c 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65  lect*, NameConte
797e0 78 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  xt*);.SQLITE_PRI
797f0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
79800 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
79810 28 4e 61 6d 65 43 6f 6e 74 65 78 74 2a 2c 20 45  (NameContext*, E
79820 78 70 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  xpr*);.SQLITE_PR
79830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
79840 65 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e  e3ResolveSelectN
79850 61 6d 65 73 28 50 61 72 73 65 2a 2c 20 53 65 6c  ames(Parse*, Sel
79860 65 63 74 2a 2c 20 4e 61 6d 65 43 6f 6e 74 65 78  ect*, NameContex
79870 74 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  t*);.SQLITE_PRIV
79880 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
79890 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
798a0 42 79 28 50 61 72 73 65 2a 2c 20 53 65 6c 65 63  By(Parse*, Selec
798b0 74 2a 2c 20 45 78 70 72 4c 69 73 74 2a 2c 20 63  t*, ExprList*, c
798c0 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 53 51 4c  onst char*);.SQL
798d0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
798e0 20 73 71 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65   sqlite3ColumnDe
798f0 66 61 75 6c 74 28 56 64 62 65 20 2a 2c 20 54 61  fault(Vdbe *, Ta
79900 62 6c 65 20 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  ble *, int);.SQL
79910 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
79920 20 73 71 6c 69 74 65 33 41 6c 74 65 72 46 69 6e   sqlite3AlterFin
79930 69 73 68 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72  ishAddColumn(Par
79940 73 65 20 2a 2c 20 54 6f 6b 65 6e 20 2a 29 3b 0a  se *, Token *);.
79950 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
79960 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65 72  oid sqlite3Alter
79970 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d 6e 28 50  BeginAddColumn(P
79980 61 72 73 65 20 2a 2c 20 53 72 63 4c 69 73 74 20  arse *, SrcList 
79990 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
799a0 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69  TE CollSeq *sqli
799b0 74 65 33 47 65 74 43 6f 6c 6c 53 65 71 28 73 71  te3GetCollSeq(sq
799c0 6c 69 74 65 33 2a 2c 20 43 6f 6c 6c 53 65 71 20  lite3*, CollSeq 
799d0 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2c  *, const char *,
799e0 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52   int);.SQLITE_PR
799f0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
79a00 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 63  e3AffinityType(c
79a10 6f 6e 73 74 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51  onst Token*);.SQ
79a20 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
79a30 64 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65  d sqlite3Analyze
79a40 28 50 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 2c  (Parse*, Token*,
79a50 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
79a60 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
79a70 69 74 65 33 49 6e 76 6f 6b 65 42 75 73 79 48 61  ite3InvokeBusyHa
79a80 6e 64 6c 65 72 28 42 75 73 79 48 61 6e 64 6c 65  ndler(BusyHandle
79a90 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  r*);.SQLITE_PRIV
79aa0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
79ab0 69 6e 64 44 62 28 73 71 6c 69 74 65 33 2a 2c 20  indDb(sqlite3*, 
79ac0 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f  Token*);.SQLITE_
79ad0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
79ae0 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28  te3AnalysisLoad(
79af0 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 69 44 42  sqlite3*,int iDB
79b00 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
79b10 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
79b20 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65  faultRowEst(Inde
79b30 78 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  x*);.SQLITE_PRIV
79b40 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
79b50 52 65 67 69 73 74 65 72 4c 69 6b 65 46 75 6e 63  RegisterLikeFunc
79b60 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 2a 2c 20  tions(sqlite3*, 
79b70 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
79b80 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
79b90 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 28 73  IsLikeFunction(s
79ba0 71 6c 69 74 65 33 2a 2c 45 78 70 72 2a 2c 69 6e  qlite3*,Expr*,in
79bb0 74 2a 2c 63 68 61 72 2a 29 3b 0a 53 51 4c 49 54  t*,char*);.SQLIT
79bc0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
79bd0 71 6c 69 74 65 33 4d 69 6e 69 6d 75 6d 46 69 6c  qlite3MinimumFil
79be0 65 46 6f 72 6d 61 74 28 50 61 72 73 65 2a 2c 20  eFormat(Parse*, 
79bf0 69 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54  int, int);.SQLIT
79c00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
79c10 71 6c 69 74 65 33 53 63 68 65 6d 61 46 72 65 65  qlite3SchemaFree
79c20 28 76 6f 69 64 20 2a 29 3b 0a 53 51 4c 49 54 45  (void *);.SQLITE
79c30 5f 50 52 49 56 41 54 45 20 53 63 68 65 6d 61 20  _PRIVATE Schema 
79c40 2a 73 71 6c 69 74 65 33 53 63 68 65 6d 61 47 65  *sqlite3SchemaGe
79c50 74 28 73 71 6c 69 74 65 33 20 2a 2c 20 42 74 72  t(sqlite3 *, Btr
79c60 65 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ee *);.SQLITE_PR
79c70 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
79c80 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 73  3SchemaToIndex(s
79c90 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 63 68 65  qlite3 *db, Sche
79ca0 6d 61 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ma *);.SQLITE_PR
79cb0 49 56 41 54 45 20 4b 65 79 49 6e 66 6f 20 2a 73  IVATE KeyInfo *s
79cc0 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e  qlite3IndexKeyin
79cd0 66 6f 28 50 61 72 73 65 20 2a 2c 20 49 6e 64 65  fo(Parse *, Inde
79ce0 78 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  x *);.SQLITE_PRI
79cf0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
79d00 43 72 65 61 74 65 46 75 6e 63 28 73 71 6c 69 74  CreateFunc(sqlit
79d10 65 33 20 2a 2c 20 63 6f 6e 73 74 20 63 68 61 72  e3 *, const char
79d20 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 76 6f   *, int, int, vo
79d30 69 64 20 2a 2c 20 0a 20 20 76 6f 69 64 20 28 2a  id *, .  void (*
79d40 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
79d50 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76  t*,int,sqlite3_v
79d60 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f 69 64  alue **),.  void
79d70 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e   (*)(sqlite3_con
79d80 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
79d90 33 5f 76 61 6c 75 65 20 2a 2a 29 2c 20 76 6f 69  3_value **), voi
79da0 64 20 28 2a 29 28 73 71 6c 69 74 65 33 5f 63 6f  d (*)(sqlite3_co
79db0 6e 74 65 78 74 2a 29 29 3b 0a 53 51 4c 49 54 45  ntext*));.SQLITE
79dc0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
79dd0 69 74 65 33 41 70 69 45 78 69 74 28 73 71 6c 69  ite3ApiExit(sqli
79de0 74 65 33 20 2a 64 62 2c 20 69 6e 74 29 3b 0a 53  te3 *db, int);.S
79df0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
79e00 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 65 6d  t sqlite3OpenTem
79e10 70 44 61 74 61 62 61 73 65 28 50 61 72 73 65 20  pDatabase(Parse 
79e20 2a 29 3b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  *);..SQLITE_PRIV
79e30 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
79e40 53 74 72 41 63 63 75 6d 49 6e 69 74 28 53 74 72  StrAccumInit(Str
79e50 41 63 63 75 6d 2a 2c 20 63 68 61 72 2a 2c 20 69  Accum*, char*, i
79e60 6e 74 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45  nt, int);.SQLITE
79e70 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
79e80 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
79e90 65 6e 64 28 53 74 72 41 63 63 75 6d 2a 2c 63 6f  end(StrAccum*,co
79ea0 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 29 3b 0a  nst char*,int);.
79eb0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
79ec0 68 61 72 20 2a 73 71 6c 69 74 65 33 53 74 72 41  har *sqlite3StrA
79ed0 63 63 75 6d 46 69 6e 69 73 68 28 53 74 72 41 63  ccumFinish(StrAc
79ee0 63 75 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  cum*);.SQLITE_PR
79ef0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
79f00 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
79f10 53 74 72 41 63 63 75 6d 2a 29 3b 0a 53 51 4c 49  StrAccum*);.SQLI
79f20 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
79f30 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73  sqlite3SelectDes
79f40 74 49 6e 69 74 28 53 65 6c 65 63 74 44 65 73 74  tInit(SelectDest
79f50 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 0a 2f 2a 0a  *,int,int);../*.
79f60 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63 65  ** The interface
79f70 20 74 6f 20 74 68 65 20 4c 45 4d 4f 4e 2d 67 65   to the LEMON-ge
79f80 6e 65 72 61 74 65 64 20 70 61 72 73 65 72 0a 2a  nerated parser.*
79f90 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
79fa0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50 61   void *sqlite3Pa
79fb0 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 2a 28  rserAlloc(void*(
79fc0 2a 29 28 73 69 7a 65 5f 74 29 29 3b 0a 53 51 4c  *)(size_t));.SQL
79fd0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
79fe0 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72   sqlite3ParserFr
79ff0 65 65 28 76 6f 69 64 2a 2c 20 76 6f 69 64 28 2a  ee(void*, void(*
7a000 29 28 76 6f 69 64 2a 29 29 3b 0a 53 51 4c 49 54  )(void*));.SQLIT
7a010 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
7a020 71 6c 69 74 65 33 50 61 72 73 65 72 28 76 6f 69  qlite3Parser(voi
7a030 64 2a 2c 20 69 6e 74 2c 20 54 6f 6b 65 6e 2c 20  d*, int, Token, 
7a040 50 61 72 73 65 2a 29 3b 0a 23 69 66 64 65 66 20  Parse*);.#ifdef 
7a050 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44  YYTRACKMAXSTACKD
7a060 45 50 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56  EPTH.SQLITE_PRIV
7a070 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
7a080 33 50 61 72 73 65 72 53 74 61 63 6b 50 65 61 6b  3ParserStackPeak
7a090 28 76 6f 69 64 2a 29 3b 0a 23 65 6e 64 69 66 0a  (void*);.#endif.
7a0a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7a0b0 69 6e 74 20 73 71 6c 69 74 65 33 41 75 74 6f 4c  int sqlite3AutoL
7a0c0 6f 61 64 45 78 74 65 6e 73 69 6f 6e 73 28 73 71  oadExtensions(sq
7a0d0 6c 69 74 65 33 2a 29 3b 0a 23 69 66 6e 64 65 66  lite3*);.#ifndef
7a0e0 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41   SQLITE_OMIT_LOA
7a0f0 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c 49  D_EXTENSION.SQLI
7a100 54 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69  TE_PRIVATE   voi
7a110 64 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78  d sqlite3CloseEx
7a120 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65 33  tensions(sqlite3
7a130 2a 29 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  *);.#else.# defi
7a140 6e 65 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45  ne sqlite3CloseE
7a150 78 74 65 6e 73 69 6f 6e 73 28 58 29 0a 23 65 6e  xtensions(X).#en
7a160 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
7a170 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
7a180 43 41 43 48 45 0a 53 51 4c 49 54 45 5f 50 52 49  CACHE.SQLITE_PRI
7a190 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69  VATE   void sqli
7a1a0 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 50 61 72  te3TableLock(Par
7a1b0 73 65 20 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20  se *, int, int, 
7a1c0 75 38 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  u8, const char *
7a1d0 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66 69  );.#else.  #defi
7a1e0 6e 65 20 73 71 6c 69 74 65 33 54 61 62 6c 65 4c  ne sqlite3TableL
7a1f0 6f 63 6b 28 76 2c 77 2c 78 2c 79 2c 7a 29 0a 23  ock(v,w,x,y,z).#
7a200 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
7a210 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
7a220 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74 20 73  _PRIVATE   int s
7a230 71 6c 69 74 65 33 55 74 66 38 54 6f 38 28 75 6e  qlite3Utf8To8(un
7a240 73 69 67 6e 65 64 20 63 68 61 72 2a 29 3b 0a 23  signed char*);.#
7a250 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
7a260 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
7a270 4c 54 41 42 4c 45 0a 23 20 20 64 65 66 69 6e 65  LTABLE.#  define
7a280 20 73 71 6c 69 74 65 33 56 74 61 62 43 6c 65 61   sqlite3VtabClea
7a290 72 28 58 29 0a 23 20 20 64 65 66 69 6e 65 20 73  r(X).#  define s
7a2a0 71 6c 69 74 65 33 56 74 61 62 53 79 6e 63 28 58  qlite3VtabSync(X
7a2b0 2c 59 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 20  ,Y) SQLITE_OK.# 
7a2c0 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
7a2d0 74 61 62 52 6f 6c 6c 62 61 63 6b 28 58 29 0a 23  tabRollback(X).#
7a2e0 20 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33    define sqlite3
7a2f0 56 74 61 62 43 6f 6d 6d 69 74 28 58 29 0a 23 20  VtabCommit(X).# 
7a300 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 56   define sqlite3V
7a310 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20 30 0a  tabInSync(db) 0.
7a320 23 65 6c 73 65 0a 53 51 4c 49 54 45 5f 50 52 49  #else.SQLITE_PRI
7a330 56 41 54 45 20 20 20 20 76 6f 69 64 20 73 71 6c  VATE    void sql
7a340 69 74 65 33 56 74 61 62 43 6c 65 61 72 28 54 61  ite3VtabClear(Ta
7a350 62 6c 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  ble*);.SQLITE_PR
7a360 49 56 41 54 45 20 20 20 20 69 6e 74 20 73 71 6c  IVATE    int sql
7a370 69 74 65 33 56 74 61 62 53 79 6e 63 28 73 71 6c  ite3VtabSync(sql
7a380 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a  ite3 *db, char *
7a390 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
7a3a0 54 45 20 20 20 20 69 6e 74 20 73 71 6c 69 74 65  TE    int sqlite
7a3b0 33 56 74 61 62 52 6f 6c 6c 62 61 63 6b 28 73 71  3VtabRollback(sq
7a3c0 6c 69 74 65 33 20 2a 64 62 29 3b 0a 53 51 4c 49  lite3 *db);.SQLI
7a3d0 54 45 5f 50 52 49 56 41 54 45 20 20 20 20 69 6e  TE_PRIVATE    in
7a3e0 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f 6d  t sqlite3VtabCom
7a3f0 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62 29  mit(sqlite3 *db)
7a400 3b 0a 23 20 20 64 65 66 69 6e 65 20 73 71 6c 69  ;.#  define sqli
7a410 74 65 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62  te3VtabInSync(db
7a420 29 20 28 28 64 62 29 2d 3e 6e 56 54 72 61 6e 73  ) ((db)->nVTrans
7a430 3e 30 20 26 26 20 28 64 62 29 2d 3e 61 56 54 72  >0 && (db)->aVTr
7a440 61 6e 73 3d 3d 30 29 0a 23 65 6e 64 69 66 0a 53  ans==0).#endif.S
7a450 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7a460 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 4d 61  id sqlite3VtabMa
7a470 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72 73 65  keWritable(Parse
7a480 2a 2c 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54  *,Table*);.SQLIT
7a490 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
7a4a0 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 73  qlite3VtabLock(s
7a4b0 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53  qlite3_vtab*);.S
7a4c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7a4d0 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e  id sqlite3VtabUn
7a4e0 6c 6f 63 6b 28 73 71 6c 69 74 65 33 2a 2c 20 73  lock(sqlite3*, s
7a4f0 71 6c 69 74 65 33 5f 76 74 61 62 2a 29 3b 0a 53  qlite3_vtab*);.S
7a500 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
7a510 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 42 65  id sqlite3VtabBe
7a520 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 2a 2c  ginParse(Parse*,
7a530 20 54 6f 6b 65 6e 2a 2c 20 54 6f 6b 65 6e 2a 2c   Token*, Token*,
7a540 20 54 6f 6b 65 6e 2a 29 3b 0a 53 51 4c 49 54 45   Token*);.SQLITE
7a550 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
7a560 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73 68 50  lite3VtabFinishP
7a570 61 72 73 65 28 50 61 72 73 65 2a 2c 20 54 6f 6b  arse(Parse*, Tok
7a580 65 6e 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  en*);.SQLITE_PRI
7a590 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7a5a0 33 56 74 61 62 41 72 67 49 6e 69 74 28 50 61 72  3VtabArgInit(Par
7a5b0 73 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  se*);.SQLITE_PRI
7a5c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
7a5d0 33 56 74 61 62 41 72 67 45 78 74 65 6e 64 28 50  3VtabArgExtend(P
7a5e0 61 72 73 65 2a 2c 20 54 6f 6b 65 6e 2a 29 3b 0a  arse*, Token*);.
7a5f0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7a600 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
7a610 6c 6c 43 72 65 61 74 65 28 73 71 6c 69 74 65 33  llCreate(sqlite3
7a620 2a 2c 20 69 6e 74 2c 20 63 6f 6e 73 74 20 63 68  *, int, const ch
7a630 61 72 20 2a 2c 20 63 68 61 72 20 2a 2a 29 3b 0a  ar *, char **);.
7a640 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
7a650 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 61  nt sqlite3VtabCa
7a660 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72 73 65 2a  llConnect(Parse*
7a670 2c 20 54 61 62 6c 65 2a 29 3b 0a 53 51 4c 49 54  , Table*);.SQLIT
7a680 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7a690 6c 69 74 65 33 56 74 61 62 43 61 6c 6c 44 65 73  lite3VtabCallDes
7a6a0 74 72 6f 79 28 73 71 6c 69 74 65 33 2a 2c 20 69  troy(sqlite3*, i
7a6b0 6e 74 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  nt, const char *
7a6c0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7a6d0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
7a6e0 62 42 65 67 69 6e 28 73 71 6c 69 74 65 33 20 2a  bBegin(sqlite3 *
7a6f0 2c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  , sqlite3_vtab *
7a700 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7a710 45 20 46 75 6e 63 44 65 66 20 2a 73 71 6c 69 74  E FuncDef *sqlit
7a720 65 33 56 74 61 62 4f 76 65 72 6c 6f 61 64 46 75  e3VtabOverloadFu
7a730 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a  nction(sqlite3 *
7a740 2c 46 75 6e 63 44 65 66 2a 2c 20 69 6e 74 20 6e  ,FuncDef*, int n
7a750 41 72 67 2c 20 45 78 70 72 2a 29 3b 0a 53 51 4c  Arg, Expr*);.SQL
7a760 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7a770 20 73 71 6c 69 74 65 33 49 6e 76 61 6c 69 64 46   sqlite3InvalidF
7a780 75 6e 63 74 69 6f 6e 28 73 71 6c 69 74 65 33 5f  unction(sqlite3_
7a790 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c  context*,int,sql
7a7a0 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 53  ite3_value**);.S
7a7b0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
7a7c0 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73 66 65  t sqlite3Transfe
7a7d0 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65  rBindings(sqlite
7a7e0 33 5f 73 74 6d 74 20 2a 2c 20 73 71 6c 69 74 65  3_stmt *, sqlite
7a7f0 33 5f 73 74 6d 74 20 2a 29 3b 0a 53 51 4c 49 54  3_stmt *);.SQLIT
7a800 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
7a810 6c 69 74 65 33 52 65 70 72 65 70 61 72 65 28 56  lite3Reprepare(V
7a820 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
7a830 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
7a840 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b 4c  e3ExprListCheckL
7a850 65 6e 67 74 68 28 50 61 72 73 65 2a 2c 20 45 78  ength(Parse*, Ex
7a860 70 72 4c 69 73 74 2a 2c 20 63 6f 6e 73 74 20 63  prList*, const c
7a870 68 61 72 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  har*);.SQLITE_PR
7a880 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
7a890 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70  qlite3BinaryComp
7a8a0 61 72 65 43 6f 6c 6c 53 65 71 28 50 61 72 73 65  areCollSeq(Parse
7a8b0 20 2a 2c 20 45 78 70 72 20 2a 2c 20 45 78 70 72   *, Expr *, Expr
7a8c0 20 2a 29 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 41 76 61   *);.../*.** Ava
7a8d0 69 6c 61 62 6c 65 20 66 61 75 6c 74 20 69 6e 6a  ilable fault inj
7a8e0 65 63 74 6f 72 73 2e 20 20 53 68 6f 75 6c 64 20  ectors.  Should 
7a8f0 62 65 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69  be numbered begi
7a900 6e 6e 69 6e 67 20 77 69 74 68 20 30 2e 0a 2a 2f  nning with 0..*/
7a910 0a 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  .#define SQLITE_
7a920 46 41 55 4c 54 49 4e 4a 45 43 54 4f 52 5f 4d 41  FAULTINJECTOR_MA
7a930 4c 4c 4f 43 20 20 20 20 20 30 0a 23 64 65 66 69  LLOC     0.#defi
7a940 6e 65 20 53 51 4c 49 54 45 5f 46 41 55 4c 54 49  ne SQLITE_FAULTI
7a950 4e 4a 45 43 54 4f 52 5f 43 4f 55 4e 54 20 20 20  NJECTOR_COUNT   
7a960 20 20 20 31 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20     1../*.** The 
7a970 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68 65  interface to the
7a980 20 63 6f 64 65 20 69 6e 20 66 61 75 6c 74 2e 63   code in fault.c
7a990 20 75 73 65 64 20 66 6f 72 20 69 64 65 6e 74 69   used for identi
7a9a0 66 79 69 6e 67 20 22 62 65 6e 69 67 6e 22 0a 2a  fying "benign".*
7a9b0 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  * malloc failure
7a9c0 73 2e 20 54 68 69 73 20 69 73 20 6f 6e 6c 79 20  s. This is only 
7a9d0 70 72 65 73 65 6e 74 20 69 66 20 53 51 4c 49 54  present if SQLIT
7a9e0 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54  E_OMIT_BUILTIN_T
7a9f0 45 53 54 0a 2a 2a 20 69 73 20 6e 6f 74 20 64 65  EST.** is not de
7aa00 66 69 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65  fined..*/.#ifnde
7aa10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
7aa20 49 4c 54 49 4e 5f 54 45 53 54 0a 53 51 4c 49 54  ILTIN_TEST.SQLIT
7aa30 45 5f 50 52 49 56 41 54 45 20 20 20 76 6f 69 64  E_PRIVATE   void
7aa40 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
7aa50 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f 69 64 29 3b  ignMalloc(void);
7aa60 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7aa70 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e    void sqlite3En
7aa80 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 76 6f  dBenignMalloc(vo
7aa90 69 64 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65  id);.#else.  #de
7aaa0 66 69 6e 65 20 73 71 6c 69 74 65 33 42 65 67 69  fine sqlite3Begi
7aab0 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 0a  nBenignMalloc().
7aac0 20 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65    #define sqlite
7aad0 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  3EndBenignMalloc
7aae0 28 29 0a 23 65 6e 64 69 66 0a 0a 23 64 65 66 69  ().#endif..#defi
7aaf0 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49  ne IN_INDEX_ROWI
7ab00 44 20 20 20 20 20 20 20 20 20 20 20 31 0a 23 64  D           1.#d
7ab10 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45 58 5f 45  efine IN_INDEX_E
7ab20 50 48 20 20 20 20 20 20 20 20 20 20 20 20 20 32  PH             2
7ab30 0a 23 64 65 66 69 6e 65 20 49 4e 5f 49 4e 44 45  .#define IN_INDE
7ab40 58 5f 49 4e 44 45 58 20 20 20 20 20 20 20 20 20  X_INDEX         
7ab50 20 20 33 0a 53 51 4c 49 54 45 5f 50 52 49 56 41    3.SQLITE_PRIVA
7ab60 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69  TE int sqlite3Fi
7ab70 6e 64 49 6e 49 6e 64 65 78 28 50 61 72 73 65 20  ndInIndex(Parse 
7ab80 2a 2c 20 45 78 70 72 20 2a 2c 20 69 6e 74 2a 29  *, Expr *, int*)
7ab90 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ;..#ifdef SQLITE
7aba0 5f 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57  _ENABLE_ATOMIC_W
7abb0 52 49 54 45 0a 53 51 4c 49 54 45 5f 50 52 49 56  RITE.SQLITE_PRIV
7abc0 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
7abd0 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 73 71 6c  3JournalOpen(sql
7abe0 69 74 65 33 5f 76 66 73 20 2a 2c 20 63 6f 6e 73  ite3_vfs *, cons
7abf0 74 20 63 68 61 72 20 2a 2c 20 73 71 6c 69 74 65  t char *, sqlite
7ac00 33 5f 66 69 6c 65 20 2a 2c 20 69 6e 74 2c 20 69  3_file *, int, i
7ac10 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
7ac20 41 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65  ATE   int sqlite
7ac30 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73 71 6c  3JournalSize(sql
7ac40 69 74 65 33 5f 76 66 73 20 2a 29 3b 0a 53 51 4c  ite3_vfs *);.SQL
7ac50 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e  ITE_PRIVATE   in
7ac60 74 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c  t sqlite3Journal
7ac70 43 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66  Create(sqlite3_f
7ac80 69 6c 65 20 2a 29 3b 0a 23 65 6c 73 65 0a 20 20  ile *);.#else.  
7ac90 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 4a  #define sqlite3J
7aca0 6f 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29  ournalSize(pVfs)
7acb0 20 28 28 70 56 66 73 29 2d 3e 73 7a 4f 73 46 69   ((pVfs)->szOsFi
7acc0 6c 65 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49  le).#endif..SQLI
7acd0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7ace0 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
7acf0 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 66 69  lOpen(sqlite3_fi
7ad00 6c 65 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  le *);.SQLITE_PR
7ad10 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
7ad20 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
7ad30 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
7ad40 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4d  E int sqlite3IsM
7ad50 65 6d 4a 6f 75 72 6e 61 6c 28 73 71 6c 69 74 65  emJournal(sqlite
7ad60 33 5f 66 69 6c 65 20 2a 29 3b 0a 0a 23 69 66 20  3_file *);..#if 
7ad70 53 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f  SQLITE_MAX_EXPR_
7ad80 44 45 50 54 48 3e 30 0a 53 51 4c 49 54 45 5f 50  DEPTH>0.SQLITE_P
7ad90 52 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71  RIVATE   void sq
7ada0 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
7adb0 68 74 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ht(Parse *pParse
7adc0 2c 20 45 78 70 72 20 2a 70 29 3b 0a 53 51 4c 49  , Expr *p);.SQLI
7add0 54 45 5f 50 52 49 56 41 54 45 20 20 20 69 6e 74  TE_PRIVATE   int
7ade0 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
7adf0 70 72 48 65 69 67 68 74 28 53 65 6c 65 63 74 20  prHeight(Select 
7ae00 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
7ae10 54 45 20 20 20 69 6e 74 20 73 71 6c 69 74 65 33  TE   int sqlite3
7ae20 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
7ae30 50 61 72 73 65 2a 2c 20 69 6e 74 29 3b 0a 23 65  Parse*, int);.#e
7ae40 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
7ae50 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
7ae60 68 74 28 78 2c 79 29 0a 20 20 23 64 65 66 69 6e  ht(x,y).  #defin
7ae70 65 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45  e sqlite3SelectE
7ae80 78 70 72 48 65 69 67 68 74 28 78 29 20 30 0a 20  xprHeight(x) 0. 
7ae90 20 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33   #define sqlite3
7aea0 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74 28  ExprCheckHeight(
7aeb0 78 2c 79 29 0a 23 65 6e 64 69 66 0a 0a 53 51 4c  x,y).#endif..SQL
7aec0 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
7aed0 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
7aee0 63 6f 6e 73 74 20 75 38 2a 29 3b 0a 53 51 4c 49  const u8*);.SQLI
7aef0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
7af00 73 71 6c 69 74 65 33 50 75 74 34 62 79 74 65 28  sqlite3Put4byte(
7af10 75 38 2a 2c 20 75 33 32 29 3b 0a 0a 23 69 66 64  u8*, u32);..#ifd
7af20 65 66 20 53 51 4c 49 54 45 5f 53 53 45 0a 23 69  ef SQLITE_SSE.#i
7af30 6e 63 6c 75 64 65 20 22 73 73 65 49 6e 74 2e 68  nclude "sseInt.h
7af40 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64 65 66  ".#endif..#ifdef
7af50 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a 53 51   SQLITE_DEBUG.SQ
7af60 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 76  LITE_PRIVATE   v
7af70 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73 65  oid sqlite3Parse
7af80 72 54 72 61 63 65 28 46 49 4c 45 2a 2c 20 63 68  rTrace(FILE*, ch
7af90 61 72 20 2a 29 3b 0a 23 65 6e 64 69 66 0a 0a 2f  ar *);.#endif../
7afa0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 53 51 4c 49  *.** If the SQLI
7afb0 54 45 5f 45 4e 41 42 4c 45 20 49 4f 54 52 41 43  TE_ENABLE IOTRAC
7afc0 45 20 65 78 69 73 74 73 20 74 68 65 6e 20 74 68  E exists then th
7afd0 65 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  e global variabl
7afe0 65 0a 2a 2a 20 73 71 6c 69 74 65 33 49 6f 54 72  e.** sqlite3IoTr
7aff0 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ace is a pointer
7b000 20 74 6f 20 61 20 70 72 69 6e 74 66 2d 6c 69 6b   to a printf-lik
7b010 65 20 72 6f 75 74 69 6e 65 20 75 73 65 64 20 74  e routine used t
7b020 6f 0a 2a 2a 20 70 72 69 6e 74 20 49 2f 4f 20 74  o.** print I/O t
7b030 72 61 63 69 6e 67 20 6d 65 73 73 61 67 65 73 2e  racing messages.
7b040 20 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49   .*/.#ifdef SQLI
7b050 54 45 5f 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43  TE_ENABLE_IOTRAC
7b060 45 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52 41  E.# define IOTRA
7b070 43 45 28 41 29 20 20 69 66 28 20 73 71 6c 69 74  CE(A)  if( sqlit
7b080 65 33 49 6f 54 72 61 63 65 20 29 7b 20 73 71 6c  e3IoTrace ){ sql
7b090 69 74 65 33 49 6f 54 72 61 63 65 20 41 3b 20 7d  ite3IoTrace A; }
7b0a0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
7b0b0 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64    void sqlite3Vd
7b0c0 62 65 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62  beIOTraceSql(Vdb
7b0d0 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  e*);.SQLITE_PRIV
7b0e0 41 54 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74  ATE void (*sqlit
7b0f0 65 33 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74  e3IoTrace)(const
7b100 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 23 65 6c   char*,...);.#el
7b110 73 65 0a 23 20 64 65 66 69 6e 65 20 49 4f 54 52  se.# define IOTR
7b120 41 43 45 28 41 29 0a 23 20 64 65 66 69 6e 65 20  ACE(A).# define 
7b130 73 71 6c 69 74 65 33 56 64 62 65 49 4f 54 72 61  sqlite3VdbeIOTra
7b140 63 65 53 71 6c 28 58 29 0a 23 65 6e 64 69 66 0a  ceSql(X).#endif.
7b150 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
7b160 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
7b170 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a 2a 2a 2a  sqliteInt.h ****
7b180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b1a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
7b1b0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
7b1c0 69 6c 65 20 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a  ile global.c ***
7b1d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b1e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b1f0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
7b200 30 30 38 20 4a 75 6e 65 20 31 33 0a 2a 2a 0a 2a  008 June 13.**.*
7b210 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
7b220 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
7b230 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
7b240 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
7b250 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
7b260 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
7b270 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
7b280 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
7b290 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
7b2a0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
7b2b0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
7b2c0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
7b2d0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
7b2e0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
7b2f0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
7b300 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
7b310 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
7b320 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
7b330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7b360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
7b370 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
7b380 69 6e 73 20 64 65 66 69 6e 69 74 69 6f 6e 73 20  ins definitions 
7b390 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  of global variab
7b3a0 6c 65 73 20 61 6e 64 20 63 6f 6e 74 61 6e 74 73  les and contants
7b3b0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 67 6c 6f  ..**.** $Id: glo
7b3c0 62 61 6c 2e 63 2c 76 20 31 2e 38 20 32 30 30 38  bal.c,v 1.8 2008
7b3d0 2f 30 39 2f 30 34 20 31 37 3a 31 37 3a 33 39 20  /09/04 17:17:39 
7b3e0 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20  danielk1977 Exp 
7b3f0 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6e 20 61 72 72  $.*/.../* An arr
7b400 61 79 20 74 6f 20 6d 61 70 20 61 6c 6c 20 75 70  ay to map all up
7b410 70 65 72 2d 63 61 73 65 20 63 68 61 72 61 63 74  per-case charact
7b420 65 72 73 20 69 6e 74 6f 20 74 68 65 69 72 20 63  ers into their c
7b430 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20  orresponding.** 
7b440 6c 6f 77 65 72 2d 63 61 73 65 20 63 68 61 72 61  lower-case chara
7b450 63 74 65 72 2e 20 0a 2a 2a 0a 2a 2a 20 53 51 4c  cter. .**.** SQL
7b460 69 74 65 20 6f 6e 6c 79 20 63 6f 6e 73 69 64 65  ite only conside
7b470 72 73 20 55 53 2d 41 53 43 49 49 20 28 6f 72 20  rs US-ASCII (or 
7b480 45 42 43 44 49 43 29 20 63 68 61 72 61 63 74 65  EBCDIC) characte
7b490 72 73 2e 20 20 57 65 20 64 6f 20 6e 6f 74 0a 2a  rs.  We do not.*
7b4a0 2a 20 68 61 6e 64 6c 65 20 63 61 73 65 20 63 6f  * handle case co
7b4b0 6e 76 65 72 73 69 6f 6e 73 20 66 6f 72 20 74 68  nversions for th
7b4c0 65 20 55 54 46 20 63 68 61 72 61 63 74 65 72 20  e UTF character 
7b4d0 73 65 74 20 73 69 6e 63 65 20 74 68 65 20 74 61  set since the ta
7b4e0 62 6c 65 73 0a 2a 2a 20 69 6e 76 6f 6c 76 65 64  bles.** involved
7b4f0 20 61 72 65 20 6e 65 61 72 6c 79 20 61 73 20 62   are nearly as b
7b500 69 67 20 6f 72 20 62 69 67 67 65 72 20 74 68 61  ig or bigger tha
7b510 6e 20 53 51 4c 69 74 65 20 69 74 73 65 6c 66 2e  n SQLite itself.
7b520 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
7b530 54 45 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  TE const unsigne
7b540 64 20 63 68 61 72 20 73 71 6c 69 74 65 33 55 70  d char sqlite3Up
7b550 70 65 72 54 6f 4c 6f 77 65 72 5b 5d 20 3d 20 7b  perToLower[] = {
7b560 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41  .#ifdef SQLITE_A
7b570 53 43 49 49 0a 20 20 20 20 20 20 30 2c 20 20 31  SCII.      0,  1
7b580 2c 20 20 32 2c 20 20 33 2c 20 20 34 2c 20 20 35  ,  2,  3,  4,  5
7b590 2c 20 20 36 2c 20 20 37 2c 20 20 38 2c 20 20 39  ,  6,  7,  8,  9
7b5a0 2c 20 31 30 2c 20 31 31 2c 20 31 32 2c 20 31 33  , 10, 11, 12, 13
7b5b0 2c 20 31 34 2c 20 31 35 2c 20 31 36 2c 20 31 37  , 14, 15, 16, 17
7b5c0 2c 0a 20 20 20 20 20 31 38 2c 20 31 39 2c 20 32  ,.     18, 19, 2
7b5d0 30 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32  0, 21, 22, 23, 2
7b5e0 34 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32  4, 25, 26, 27, 2
7b5f0 38 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 33  8, 29, 30, 31, 3
7b600 32 2c 20 33 33 2c 20 33 34 2c 20 33 35 2c 0a 20  2, 33, 34, 35,. 
7b610 20 20 20 20 33 36 2c 20 33 37 2c 20 33 38 2c 20      36, 37, 38, 
7b620 33 39 2c 20 34 30 2c 20 34 31 2c 20 34 32 2c 20  39, 40, 41, 42, 
7b630 34 33 2c 20 34 34 2c 20 34 35 2c 20 34 36 2c 20  43, 44, 45, 46, 
7b640 34 37 2c 20 34 38 2c 20 34 39 2c 20 35 30 2c 20  47, 48, 49, 50, 
7b650 35 31 2c 20 35 32 2c 20 35 33 2c 0a 20 20 20 20  51, 52, 53,.    
7b660 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37 2c   54, 55, 56, 57,
7b670 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31 2c   58, 59, 60, 61,
7b680 20 36 32 2c 20 36 33 2c 20 36 34 2c 20 39 37 2c   62, 63, 64, 97,
7b690 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30 31 2c   98, 99,100,101,
7b6a0 31 30 32 2c 31 30 33 2c 0a 20 20 20 20 31 30 34  102,103,.    104
7b6b0 2c 31 30 35 2c 31 30 36 2c 31 30 37 2c 31 30 38  ,105,106,107,108
7b6c0 2c 31 30 39 2c 31 31 30 2c 31 31 31 2c 31 31 32  ,109,110,111,112
7b6d0 2c 31 31 33 2c 31 31 34 2c 31 31 35 2c 31 31 36  ,113,114,115,116
7b6e0 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32 30  ,117,118,119,120
7b6f0 2c 31 32 31 2c 0a 20 20 20 20 31 32 32 2c 20 39  ,121,.    122, 9
7b700 31 2c 20 39 32 2c 20 39 33 2c 20 39 34 2c 20 39  1, 92, 93, 94, 9
7b710 35 2c 20 39 36 2c 20 39 37 2c 20 39 38 2c 20 39  5, 96, 97, 98, 9
7b720 39 2c 31 30 30 2c 31 30 31 2c 31 30 32 2c 31 30  9,100,101,102,10
7b730 33 2c 31 30 34 2c 31 30 35 2c 31 30 36 2c 31 30  3,104,105,106,10
7b740 37 2c 0a 20 20 20 20 31 30 38 2c 31 30 39 2c 31  7,.    108,109,1
7b750 31 30 2c 31 31 31 2c 31 31 32 2c 31 31 33 2c 31  10,111,112,113,1
7b760 31 34 2c 31 31 35 2c 31 31 36 2c 31 31 37 2c 31  14,115,116,117,1
7b770 31 38 2c 31 31 39 2c 31 32 30 2c 31 32 31 2c 31  18,119,120,121,1
7b780 32 32 2c 31 32 33 2c 31 32 34 2c 31 32 35 2c 0a  22,123,124,125,.
7b790 20 20 20 20 31 32 36 2c 31 32 37 2c 31 32 38 2c      126,127,128,
7b7a0 31 32 39 2c 31 33 30 2c 31 33 31 2c 31 33 32 2c  129,130,131,132,
7b7b0 31 33 33 2c 31 33 34 2c 31 33 35 2c 31 33 36 2c  133,134,135,136,
7b7c0 31 33 37 2c 31 33 38 2c 31 33 39 2c 31 34 30 2c  137,138,139,140,
7b7d0 31 34 31 2c 31 34 32 2c 31 34 33 2c 0a 20 20 20  141,142,143,.   
7b7e0 20 31 34 34 2c 31 34 35 2c 31 34 36 2c 31 34 37   144,145,146,147
7b7f0 2c 31 34 38 2c 31 34 39 2c 31 35 30 2c 31 35 31  ,148,149,150,151
7b800 2c 31 35 32 2c 31 35 33 2c 31 35 34 2c 31 35 35  ,152,153,154,155
7b810 2c 31 35 36 2c 31 35 37 2c 31 35 38 2c 31 35 39  ,156,157,158,159
7b820 2c 31 36 30 2c 31 36 31 2c 0a 20 20 20 20 31 36  ,160,161,.    16
7b830 32 2c 31 36 33 2c 31 36 34 2c 31 36 35 2c 31 36  2,163,164,165,16
7b840 36 2c 31 36 37 2c 31 36 38 2c 31 36 39 2c 31 37  6,167,168,169,17
7b850 30 2c 31 37 31 2c 31 37 32 2c 31 37 33 2c 31 37  0,171,172,173,17
7b860 34 2c 31 37 35 2c 31 37 36 2c 31 37 37 2c 31 37  4,175,176,177,17
7b870 38 2c 31 37 39 2c 0a 20 20 20 20 31 38 30 2c 31  8,179,.    180,1
7b880 38 31 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31  81,182,183,184,1
7b890 38 35 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31  85,186,187,188,1
7b8a0 38 39 2c 31 39 30 2c 31 39 31 2c 31 39 32 2c 31  89,190,191,192,1
7b8b0 39 33 2c 31 39 34 2c 31 39 35 2c 31 39 36 2c 31  93,194,195,196,1
7b8c0 39 37 2c 0a 20 20 20 20 31 39 38 2c 31 39 39 2c  97,.    198,199,
7b8d0 32 30 30 2c 32 30 31 2c 32 30 32 2c 32 30 33 2c  200,201,202,203,
7b8e0 32 30 34 2c 32 30 35 2c 32 30 36 2c 32 30 37 2c  204,205,206,207,
7b8f0 32 30 38 2c 32 30 39 2c 32 31 30 2c 32 31 31 2c  208,209,210,211,
7b900 32 31 32 2c 32 31 33 2c 32 31 34 2c 32 31 35 2c  212,213,214,215,
7b910 0a 20 20 20 20 32 31 36 2c 32 31 37 2c 32 31 38  .    216,217,218
7b920 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32  ,219,220,221,222
7b930 2c 32 32 33 2c 32 32 34 2c 32 32 35 2c 32 32 36  ,223,224,225,226
7b940 2c 32 32 37 2c 32 32 38 2c 32 32 39 2c 32 33 30  ,227,228,229,230
7b950 2c 32 33 31 2c 32 33 32 2c 32 33 33 2c 0a 20 20  ,231,232,233,.  
7b960 20 20 32 33 34 2c 32 33 35 2c 32 33 36 2c 32 33    234,235,236,23
7b970 37 2c 32 33 38 2c 32 33 39 2c 32 34 30 2c 32 34  7,238,239,240,24
7b980 31 2c 32 34 32 2c 32 34 33 2c 32 34 34 2c 32 34  1,242,243,244,24
7b990 35 2c 32 34 36 2c 32 34 37 2c 32 34 38 2c 32 34  5,246,247,248,24
7b9a0 39 2c 32 35 30 2c 32 35 31 2c 0a 20 20 20 20 32  9,250,251,.    2
7b9b0 35 32 2c 32 35 33 2c 32 35 34 2c 32 35 35 0a 23  52,253,254,255.#
7b9c0 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
7b9d0 49 54 45 5f 45 42 43 44 49 43 0a 20 20 20 20 20  ITE_EBCDIC.     
7b9e0 20 30 2c 20 20 31 2c 20 20 32 2c 20 20 33 2c 20   0,  1,  2,  3, 
7b9f0 20 34 2c 20 20 35 2c 20 20 36 2c 20 20 37 2c 20   4,  5,  6,  7, 
7ba00 20 38 2c 20 20 39 2c 20 31 30 2c 20 31 31 2c 20   8,  9, 10, 11, 
7ba10 31 32 2c 20 31 33 2c 20 31 34 2c 20 31 35 2c 20  12, 13, 14, 15, 
7ba20 2f 2a 20 30 78 20 2a 2f 0a 20 20 20 20 20 31 36  /* 0x */.     16
7ba30 2c 20 31 37 2c 20 31 38 2c 20 31 39 2c 20 32 30  , 17, 18, 19, 20
7ba40 2c 20 32 31 2c 20 32 32 2c 20 32 33 2c 20 32 34  , 21, 22, 23, 24
7ba50 2c 20 32 35 2c 20 32 36 2c 20 32 37 2c 20 32 38  , 25, 26, 27, 28
7ba60 2c 20 32 39 2c 20 33 30 2c 20 33 31 2c 20 2f 2a  , 29, 30, 31, /*
7ba70 20 31 78 20 2a 2f 0a 20 20 20 20 20 33 32 2c 20   1x */.     32, 
7ba80 33 33 2c 20 33 34 2c 20 33 35 2c 20 33 36 2c 20  33, 34, 35, 36, 
7ba90 33 37 2c 20 33 38 2c 20 33 39 2c 20 34 30 2c 20  37, 38, 39, 40, 
7baa0 34 31 2c 20 34 32 2c 20 34 33 2c 20 34 34 2c 20  41, 42, 43, 44, 
7bab0 34 35 2c 20 34 36 2c 20 34 37 2c 20 2f 2a 20 32  45, 46, 47, /* 2
7bac0 78 20 2a 2f 0a 20 20 20 20 20 34 38 2c 20 34 39  x */.     48, 49
7bad0 2c 20 35 30 2c 20 35 31 2c 20 35 32 2c 20 35 33  , 50, 51, 52, 53
7bae0 2c 20 35 34 2c 20 35 35 2c 20 35 36 2c 20 35 37  , 54, 55, 56, 57
7baf0 2c 20 35 38 2c 20 35 39 2c 20 36 30 2c 20 36 31  , 58, 59, 60, 61
7bb00 2c 20 36 32 2c 20 36 33 2c 20 2f 2a 20 33 78 20  , 62, 63, /* 3x 
7bb10 2a 2f 0a 20 20 20 20 20 36 34 2c 20 36 35 2c 20  */.     64, 65, 
7bb20 36 36 2c 20 36 37 2c 20 36 38 2c 20 36 39 2c 20  66, 67, 68, 69, 
7bb30 37 30 2c 20 37 31 2c 20 37 32 2c 20 37 33 2c 20  70, 71, 72, 73, 
7bb40 37 34 2c 20 37 35 2c 20 37 36 2c 20 37 37 2c 20  74, 75, 76, 77, 
7bb50 37 38 2c 20 37 39 2c 20 2f 2a 20 34 78 20 2a 2f  78, 79, /* 4x */
7bb60 0a 20 20 20 20 20 38 30 2c 20 38 31 2c 20 38 32  .     80, 81, 82
7bb70 2c 20 38 33 2c 20 38 34 2c 20 38 35 2c 20 38 36  , 83, 84, 85, 86
7bb80 2c 20 38 37 2c 20 38 38 2c 20 38 39 2c 20 39 30  , 87, 88, 89, 90
7bb90 2c 20 39 31 2c 20 39 32 2c 20 39 33 2c 20 39 34  , 91, 92, 93, 94
7bba0 2c 20 39 35 2c 20 2f 2a 20 35 78 20 2a 2f 0a 20  , 95, /* 5x */. 
7bbb0 20 20 20 20 39 36 2c 20 39 37 2c 20 36 36 2c 20      96, 97, 66, 
7bbc0 36 37 2c 20 36 38 2c 20 36 39 2c 20 37 30 2c 20  67, 68, 69, 70, 
7bbd0 37 31 2c 20 37 32 2c 20 37 33 2c 31 30 36 2c 31  71, 72, 73,106,1
7bbe0 30 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31  07,108,109,110,1
7bbf0 31 31 2c 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  11, /* 6x */.   
7bc00 20 31 31 32 2c 20 38 31 2c 20 38 32 2c 20 38 33   112, 81, 82, 83
7bc10 2c 20 38 34 2c 20 38 35 2c 20 38 36 2c 20 38 37  , 84, 85, 86, 87
7bc20 2c 20 38 38 2c 20 38 39 2c 31 32 32 2c 31 32 33  , 88, 89,122,123
7bc30 2c 31 32 34 2c 31 32 35 2c 31 32 36 2c 31 32 37  ,124,125,126,127
7bc40 2c 20 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 20 31  , /* 7x */.    1
7bc50 32 38 2c 31 32 39 2c 31 33 30 2c 31 33 31 2c 31  28,129,130,131,1
7bc60 33 32 2c 31 33 33 2c 31 33 34 2c 31 33 35 2c 31  32,133,134,135,1
7bc70 33 36 2c 31 33 37 2c 31 33 38 2c 31 33 39 2c 31  36,137,138,139,1
7bc80 34 30 2c 31 34 31 2c 31 34 32 2c 31 34 33 2c 20  40,141,142,143, 
7bc90 2f 2a 20 38 78 20 2a 2f 0a 20 20 20 20 31 34 34  /* 8x */.    144
7bca0 2c 31 34 35 2c 31 34 36 2c 31 34 37 2c 31 34 38  ,145,146,147,148
7bcb0 2c 31 34 39 2c 31 35 30 2c 31 35 31 2c 31 35 32  ,149,150,151,152
7bcc0 2c 31 35 33 2c 31 35 34 2c 31 35 35 2c 31 35 36  ,153,154,155,156
7bcd0 2c 31 35 37 2c 31 35 36 2c 31 35 39 2c 20 2f 2a  ,157,156,159, /*
7bce0 20 39 78 20 2a 2f 0a 20 20 20 20 31 36 30 2c 31   9x */.    160,1
7bcf0 36 31 2c 31 36 32 2c 31 36 33 2c 31 36 34 2c 31  61,162,163,164,1
7bd00 36 35 2c 31 36 36 2c 31 36 37 2c 31 36 38 2c 31  65,166,167,168,1
7bd10 36 39 2c 31 37 30 2c 31 37 31 2c 31 34 30 2c 31  69,170,171,140,1
7bd20 34 31 2c 31 34 32 2c 31 37 35 2c 20 2f 2a 20 41  41,142,175, /* A
7bd30 78 20 2a 2f 0a 20 20 20 20 31 37 36 2c 31 37 37  x */.    176,177
7bd40 2c 31 37 38 2c 31 37 39 2c 31 38 30 2c 31 38 31  ,178,179,180,181
7bd50 2c 31 38 32 2c 31 38 33 2c 31 38 34 2c 31 38 35  ,182,183,184,185
7bd60 2c 31 38 36 2c 31 38 37 2c 31 38 38 2c 31 38 39  ,186,187,188,189
7bd70 2c 31 39 30 2c 31 39 31 2c 20 2f 2a 20 42 78 20  ,190,191, /* Bx 
7bd80 2a 2f 0a 20 20 20 20 31 39 32 2c 31 32 39 2c 31  */.    192,129,1
7bd90 33 30 2c 31 33 31 2c 31 33 32 2c 31 33 33 2c 31  30,131,132,133,1
7bda0 33 34 2c 31 33 35 2c 31 33 36 2c 31 33 37 2c 32  34,135,136,137,2
7bdb0 30 32 2c 32 30 33 2c 32 30 34 2c 32 30 35 2c 32  02,203,204,205,2
7bdc0 30 36 2c 32 30 37 2c 20 2f 2a 20 43 78 20 2a 2f  06,207, /* Cx */
7bdd0 0a 20 20 20 20 32 30 38 2c 31 34 35 2c 31 34 36  .    208,145,146
7bde0 2c 31 34 37 2c 31 34 38 2c 31 34 39 2c 31 35 30  ,147,148,149,150
7bdf0 2c 31 35 31 2c 31 35 32 2c 31 35 33 2c 32 31 38  ,151,152,153,218
7be00 2c 32 31 39 2c 32 32 30 2c 32 32 31 2c 32 32 32  ,219,220,221,222
7be10 2c 32 32 33 2c 20 2f 2a 20 44 78 20 2a 2f 0a 20  ,223, /* Dx */. 
7be20 20 20 20 32 32 34 2c 32 32 35 2c 31 36 32 2c 31     224,225,162,1
7be30 36 33 2c 31 36 34 2c 31 36 35 2c 31 36 36 2c 31  63,164,165,166,1
7be40 36 37 2c 31 36 38 2c 31 36 39 2c 32 33 32 2c 32  67,168,169,232,2
7be50 30 33 2c 32 30 34 2c 32 30 35 2c 32 30 36 2c 32  03,204,205,206,2
7be60 30 37 2c 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  07, /* Ex */.   
7be70 20 32 33 39 2c 32 34 30 2c 32 34 31 2c 32 34 32   239,240,241,242
7be80 2c 32 34 33 2c 32 34 34 2c 32 34 35 2c 32 34 36  ,243,244,245,246
7be90 2c 32 34 37 2c 32 34 38 2c 32 34 39 2c 32 31 39  ,247,248,249,219
7bea0 2c 32 32 30 2c 32 32 31 2c 32 32 32 2c 32 35 35  ,220,221,222,255
7beb0 2c 20 2f 2a 20 46 78 20 2a 2f 0a 23 65 6e 64 69  , /* Fx */.#endi
7bec0 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  f.};../*.** The 
7bed0 66 6f 6c 6c 6f 77 69 6e 67 20 73 69 6e 67 6c 65  following single
7bee0 74 6f 6e 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ton contains the
7bef0 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67 75 72   global configur
7bf00 61 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 74 68 65  ation for.** the
7bf10 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79 2e   SQLite library.
7bf20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
7bf30 54 45 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  TE SQLITE_WSD st
7bf40 72 75 63 74 20 53 71 6c 69 74 65 33 43 6f 6e 66  ruct Sqlite3Conf
7bf50 69 67 20 73 71 6c 69 74 65 33 43 6f 6e 66 69 67  ig sqlite3Config
7bf60 20 3d 20 7b 0a 20 20 20 53 51 4c 49 54 45 5f 44   = {.   SQLITE_D
7bf70 45 46 41 55 4c 54 5f 4d 45 4d 53 54 41 54 55 53  EFAULT_MEMSTATUS
7bf80 2c 20 20 2f 2a 20 62 4d 65 6d 73 74 61 74 20 2a  ,  /* bMemstat *
7bf90 2f 0a 20 20 20 31 2c 20 20 20 20 20 20 20 20 20  /.   1,         
7bfa0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
7bfb0 2f 2a 20 62 43 6f 72 65 4d 75 74 65 78 20 2a 2f  /* bCoreMutex */
7bfc0 0a 20 20 20 53 51 4c 49 54 45 5f 54 48 52 45 41  .   SQLITE_THREA
7bfd0 44 53 41 46 45 3d 3d 31 2c 20 20 20 20 20 20 2f  DSAFE==1,      /
7bfe0 2a 20 62 46 75 6c 6c 4d 75 74 65 78 20 2a 2f 0a  * bFullMutex */.
7bff0 20 20 20 30 78 37 66 66 66 66 66 66 65 2c 20 20     0x7ffffffe,  
7c000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
7c010 20 6d 78 53 74 72 6c 65 6e 20 2a 2f 0a 20 20 20   mxStrlen */.   
7c020 31 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  100,            
7c030 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a             /* sz
7c040 4c 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20  Lookaside */.   
7c050 35 30 30 2c 20 20 20 20 20 20 20 20 20 20 20 20  500,            
7c060 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6e 4c             /* nL
7c070 6f 6f 6b 61 73 69 64 65 20 2a 2f 0a 20 20 20 2f  ookaside */.   /
7c080 2a 20 4f 74 68 65 72 20 66 69 65 6c 64 73 20 61  * Other fields a
7c090 6c 6c 20 64 65 66 61 75 6c 74 20 74 6f 20 7a 65  ll default to ze
7c0a0 72 6f 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a  ro */.};.../*.**
7c0b0 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72 20   Hash table for 
7c0c0 67 6c 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 73  global functions
7c0d0 20 2d 20 66 75 6e 63 74 69 6f 6e 73 20 63 6f 6d   - functions com
7c0e0 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a 20 64 61  mon to all.** da
7c0f0 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
7c100 6e 73 2e 20 20 41 66 74 65 72 20 69 6e 69 74 69  ns.  After initi
7c110 61 6c 69 7a 61 74 69 6f 6e 2c 20 74 68 69 73 20  alization, this 
7c120 74 61 62 6c 65 20 69 73 0a 2a 2a 20 72 65 61 64  table is.** read
7c130 2d 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  -only..*/.SQLITE
7c140 5f 50 52 49 56 41 54 45 20 53 51 4c 49 54 45 5f  _PRIVATE SQLITE_
7c150 57 53 44 20 46 75 6e 63 44 65 66 48 61 73 68 20  WSD FuncDefHash 
7c160 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e  sqlite3GlobalFun
7c170 63 74 69 6f 6e 73 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a  ctions;../******
7c180 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
7c190 67 6c 6f 62 61 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a  global.c *******
7c1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c1b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c1c0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
7c1d0 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
7c1e0 69 6c 65 20 73 74 61 74 75 73 2e 63 20 2a 2a 2a  ile status.c ***
7c1f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c210 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
7c220 30 30 38 20 4a 75 6e 65 20 31 38 0a 2a 2a 0a 2a  008 June 18.**.*
7c230 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
7c240 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
7c250 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
7c260 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
7c270 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
7c280 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
7c290 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
7c2a0 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
7c2b0 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
7c2c0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
7c2d0 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
7c2e0 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
7c2f0 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
7c300 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
7c310 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
7c320 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
7c330 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
7c340 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
7c350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7c380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
7c390 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 69 6d 70   This module imp
7c3a0 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 71 6c 69  lements the sqli
7c3b0 74 65 33 5f 73 74 61 74 75 73 28 29 20 69 6e 74  te3_status() int
7c3c0 65 72 66 61 63 65 20 61 6e 64 20 72 65 6c 61 74  erface and relat
7c3d0 65 64 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 61 6c  ed.** functional
7c3e0 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ity..**.** $Id: 
7c3f0 73 74 61 74 75 73 2e 63 2c 76 20 31 2e 39 20 32  status.c,v 1.9 2
7c400 30 30 38 2f 30 39 2f 30 32 20 30 30 3a 35 32 3a  008/09/02 00:52:
7c410 35 32 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a  52 drh Exp $.*/.
7c420 0a 2f 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 73  ./*.** Variables
7c430 20 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 63   in which to rec
7c440 6f 72 64 20 73 74 61 74 75 73 20 69 6e 66 6f 72  ord status infor
7c450 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64  mation..*/.typed
7c460 65 66 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  ef struct sqlite
7c470 33 53 74 61 74 54 79 70 65 20 73 71 6c 69 74 65  3StatType sqlite
7c480 33 53 74 61 74 54 79 70 65 3b 0a 73 74 61 74 69  3StatType;.stati
7c490 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
7c4a0 75 63 74 20 73 71 6c 69 74 65 33 53 74 61 74 54  uct sqlite3StatT
7c4b0 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 6f 77 56  ype {.  int nowV
7c4c0 61 6c 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20  alue[9];        
7c4d0 20 2f 2a 20 43 75 72 72 65 6e 74 20 76 61 6c 75   /* Current valu
7c4e0 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 56 61 6c  e */.  int mxVal
7c4f0 75 65 5b 39 5d 3b 20 20 20 20 20 20 20 20 20 20  ue[9];          
7c500 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75 65  /* Maximum value
7c510 20 2a 2f 0a 7d 20 73 71 6c 69 74 65 33 53 74 61   */.} sqlite3Sta
7c520 74 20 3d 20 7b 20 7b 30 2c 7d 2c 20 7b 30 2c 7d  t = { {0,}, {0,}
7c530 20 7d 3b 0a 0a 0a 2f 2a 20 54 68 65 20 22 77 73   };.../* The "ws
7c540 64 53 74 61 74 22 20 6d 61 63 72 6f 20 77 69 6c  dStat" macro wil
7c550 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  l resolve to the
7c560 20 73 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74   status informat
7c570 69 6f 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65 63  ion.** state vec
7c580 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c  tor.  If writabl
7c590 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
7c5a0 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20   unsupported on 
7c5b0 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77  the target,.** w
7c5c0 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65  e have to locate
7c5d0 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74 6f   the state vecto
7c5e0 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20  r at run-time.  
7c5f0 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d  In the more comm
7c600 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65  on.** case where
7c610 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69 63   writable static
7c620 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72 74   data is support
7c630 65 64 2c 20 77 73 64 53 74 61 74 20 63 61 6e 20  ed, wsdStat can 
7c640 72 65 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a  refer directly.*
7c650 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65  * to the "sqlite
7c660 33 53 74 61 74 22 20 73 74 61 74 65 20 76 65 63  3Stat" state vec
7c670 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62 6f  tor declared abo
7c680 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  ve..*/.#ifdef SQ
7c690 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23 20  LITE_OMIT_WSD.# 
7c6a0 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 49 6e  define wsdStatIn
7c6b0 69 74 20 20 73 71 6c 69 74 65 33 53 74 61 74 54  it  sqlite3StatT
7c6c0 79 70 65 20 2a 78 20 3d 20 26 47 4c 4f 42 41 4c  ype *x = &GLOBAL
7c6d0 28 73 71 6c 69 74 65 33 53 74 61 74 54 79 70 65  (sqlite3StatType
7c6e0 2c 73 71 6c 69 74 65 33 53 74 61 74 29 0a 23 20  ,sqlite3Stat).# 
7c6f0 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20 78  define wsdStat x
7c700 5b 30 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  [0].#else.# defi
7c710 6e 65 20 77 73 64 53 74 61 74 49 6e 69 74 0a 23  ne wsdStatInit.#
7c720 20 64 65 66 69 6e 65 20 77 73 64 53 74 61 74 20   define wsdStat 
7c730 73 71 6c 69 74 65 33 53 74 61 74 0a 23 65 6e 64  sqlite3Stat.#end
7c740 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  if../*.** Return
7c750 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 61 6c   the current val
7c760 75 65 20 6f 66 20 61 20 73 74 61 74 75 73 20 70  ue of a status p
7c770 61 72 61 6d 65 74 65 72 2e 0a 2a 2f 0a 53 51 4c  arameter..*/.SQL
7c780 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
7c790 73 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c  sqlite3StatusVal
7c7a0 75 65 28 69 6e 74 20 6f 70 29 7b 0a 20 20 77 73  ue(int op){.  ws
7c7b0 64 53 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73  dStatInit;.  ass
7c7c0 65 72 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70  ert( op>=0 && op
7c7d0 3c 41 72 72 61 79 53 69 7a 65 28 77 73 64 53 74  <ArraySize(wsdSt
7c7e0 61 74 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a  at.nowValue) );.
7c7f0 20 20 72 65 74 75 72 6e 20 77 73 64 53 74 61 74    return wsdStat
7c800 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 7d  .nowValue[op];.}
7c810 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4e 20 74 6f  ../*.** Add N to
7c820 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20   the value of a 
7c830 73 74 61 74 75 73 20 72 65 63 6f 72 64 2e 20 20  status record.  
7c840 49 74 20 69 73 20 61 73 73 75 6d 65 64 20 74 68  It is assumed th
7c850 61 74 20 74 68 65 0a 2a 2a 20 63 61 6c 6c 65 72  at the.** caller
7c860 20 68 6f 6c 64 73 20 61 70 70 72 6f 70 72 69 61   holds appropria
7c870 74 65 20 6c 6f 63 6b 73 2e 0a 2a 2f 0a 53 51 4c  te locks..*/.SQL
7c880 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
7c890 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
7c8a0 64 28 69 6e 74 20 6f 70 2c 20 69 6e 74 20 4e 29  d(int op, int N)
7c8b0 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69 74 3b  {.  wsdStatInit;
7c8c0 0a 20 20 61 73 73 65 72 74 28 20 6f 70 3e 3d 30  .  assert( op>=0
7c8d0 20 26 26 20 6f 70 3c 41 72 72 61 79 53 69 7a 65   && op<ArraySize
7c8e0 28 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75  (wsdStat.nowValu
7c8f0 65 29 20 29 3b 0a 20 20 77 73 64 53 74 61 74 2e  e) );.  wsdStat.
7c900 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 20 2b 3d 20  nowValue[op] += 
7c910 4e 3b 0a 20 20 69 66 28 20 77 73 64 53 74 61 74  N;.  if( wsdStat
7c920 2e 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3e 77 73  .nowValue[op]>ws
7c930 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b 6f 70  dStat.mxValue[op
7c940 5d 20 29 7b 0a 20 20 20 20 77 73 64 53 74 61 74  ] ){.    wsdStat
7c950 2e 6d 78 56 61 6c 75 65 5b 6f 70 5d 20 3d 20 77  .mxValue[op] = w
7c960 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 5b  sdStat.nowValue[
7c970 6f 70 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  op];.  }.}../*.*
7c980 2a 20 53 65 74 20 74 68 65 20 76 61 6c 75 65 20  * Set the value 
7c990 6f 66 20 61 20 73 74 61 74 75 73 20 74 6f 20 58  of a status to X
7c9a0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
7c9b0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
7c9c0 53 74 61 74 75 73 53 65 74 28 69 6e 74 20 6f 70  StatusSet(int op
7c9d0 2c 20 69 6e 74 20 58 29 7b 0a 20 20 77 73 64 53  , int X){.  wsdS
7c9e0 74 61 74 49 6e 69 74 3b 0a 20 20 61 73 73 65 72  tatInit;.  asser
7c9f0 74 28 20 6f 70 3e 3d 30 20 26 26 20 6f 70 3c 41  t( op>=0 && op<A
7ca00 72 72 61 79 53 69 7a 65 28 77 73 64 53 74 61 74  rraySize(wsdStat
7ca10 2e 6e 6f 77 56 61 6c 75 65 29 20 29 3b 0a 20 20  .nowValue) );.  
7ca20 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
7ca30 5b 6f 70 5d 20 3d 20 58 3b 0a 20 20 69 66 28 20  [op] = X;.  if( 
7ca40 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
7ca50 5b 6f 70 5d 3e 77 73 64 53 74 61 74 2e 6d 78 56  [op]>wsdStat.mxV
7ca60 61 6c 75 65 5b 6f 70 5d 20 29 7b 0a 20 20 20 20  alue[op] ){.    
7ca70 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75 65 5b  wsdStat.mxValue[
7ca80 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e 6e 6f  op] = wsdStat.no
7ca90 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 7d 0a  wValue[op];.  }.
7caa0 7d 0a 0a 2f 2a 0a 2a 2a 20 51 75 65 72 79 20 73  }../*.** Query s
7cab0 74 61 74 75 73 20 69 6e 66 6f 72 6d 61 74 69 6f  tatus informatio
7cac0 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d  n..**.** This im
7cad0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73  plementation ass
7cae0 75 6d 65 73 20 74 68 61 74 20 72 65 61 64 69 6e  umes that readin
7caf0 67 20 6f 72 20 77 72 69 74 69 6e 67 20 61 6e 20  g or writing an 
7cb00 61 6c 69 67 6e 65 64 0a 2a 2a 20 33 32 2d 62 69  aligned.** 32-bi
7cb10 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 6e 20  t integer is an 
7cb20 61 74 6f 6d 69 63 20 6f 70 65 72 61 74 69 6f 6e  atomic operation
7cb30 2e 20 20 49 66 20 74 68 61 74 20 61 73 73 75 6d  .  If that assum
7cb40 70 74 69 6f 6e 20 69 73 20 6e 6f 74 20 74 72 75  ption is not tru
7cb50 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20  e,.** then this 
7cb60 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f 74 20 74  routine is not t
7cb70 68 72 65 61 64 73 61 66 65 2e 0a 2a 2f 0a 53 51  hreadsafe..*/.SQ
7cb80 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
7cb90 69 74 65 33 5f 73 74 61 74 75 73 28 69 6e 74 20  ite3_status(int 
7cba0 6f 70 2c 20 69 6e 74 20 2a 70 43 75 72 72 65 6e  op, int *pCurren
7cbb0 74 2c 20 69 6e 74 20 2a 70 48 69 67 68 77 61 74  t, int *pHighwat
7cbc0 65 72 2c 20 69 6e 74 20 72 65 73 65 74 46 6c 61  er, int resetFla
7cbd0 67 29 7b 0a 20 20 77 73 64 53 74 61 74 49 6e 69  g){.  wsdStatIni
7cbe0 74 3b 0a 20 20 69 66 28 20 6f 70 3c 30 20 7c 7c  t;.  if( op<0 ||
7cbf0 20 6f 70 3e 3d 41 72 72 61 79 53 69 7a 65 28 77   op>=ArraySize(w
7cc00 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65 29  sdStat.nowValue)
7cc10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
7cc20 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
7cc30 7d 0a 20 20 2a 70 43 75 72 72 65 6e 74 20 3d 20  }.  *pCurrent = 
7cc40 77 73 64 53 74 61 74 2e 6e 6f 77 56 61 6c 75 65  wsdStat.nowValue
7cc50 5b 6f 70 5d 3b 0a 20 20 2a 70 48 69 67 68 77 61  [op];.  *pHighwa
7cc60 74 65 72 20 3d 20 77 73 64 53 74 61 74 2e 6d 78  ter = wsdStat.mx
7cc70 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20 69 66 28  Value[op];.  if(
7cc80 20 72 65 73 65 74 46 6c 61 67 20 29 7b 0a 20 20   resetFlag ){.  
7cc90 20 20 77 73 64 53 74 61 74 2e 6d 78 56 61 6c 75    wsdStat.mxValu
7cca0 65 5b 6f 70 5d 20 3d 20 77 73 64 53 74 61 74 2e  e[op] = wsdStat.
7ccb0 6e 6f 77 56 61 6c 75 65 5b 6f 70 5d 3b 0a 20 20  nowValue[op];.  
7ccc0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
7ccd0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  E_OK;.}../*.** Q
7cce0 75 65 72 79 20 73 74 61 74 75 73 20 69 6e 66 6f  uery status info
7ccf0 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 73 69  rmation for a si
7cd00 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 63 6f  ngle database co
7cd10 6e 6e 65 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49  nnection.*/.SQLI
7cd20 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
7cd30 65 33 5f 64 62 5f 73 74 61 74 75 73 28 0a 20 20  e3_db_status(.  
7cd40 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
7cd50 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
7cd60 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
7cd70 20 77 68 6f 73 65 20 73 74 61 74 75 73 20 69 73   whose status is
7cd80 20 64 65 73 69 72 65 64 20 2a 2f 0a 20 20 69 6e   desired */.  in
7cd90 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20  t op,           
7cda0 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 76 65      /* Status ve
7cdb0 72 62 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 43 75  rb */.  int *pCu
7cdc0 72 72 65 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a  rrent,        /*
7cdd0 20 57 72 69 74 65 20 63 75 72 72 65 6e 74 20 76   Write current v
7cde0 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 20 20 69  alue here */.  i
7cdf0 6e 74 20 2a 70 48 69 67 68 77 61 74 65 72 2c 20  nt *pHighwater, 
7ce00 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 68 69       /* Write hi
7ce10 67 68 2d 77 61 74 65 72 20 6d 61 72 6b 20 68 65  gh-water mark he
7ce20 72 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 65  re */.  int rese
7ce30 74 46 6c 61 67 20 20 20 20 20 20 20 20 20 2f 2a  tFlag         /*
7ce40 20 52 65 73 65 74 20 68 69 67 68 2d 77 61 74 65   Reset high-wate
7ce50 72 20 6d 61 72 6b 20 69 66 20 74 72 75 65 20 2a  r mark if true *
7ce60 2f 0a 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f  /.){.  switch( o
7ce70 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  p ){.    case SQ
7ce80 4c 49 54 45 5f 44 42 53 54 41 54 55 53 5f 4c 4f  LITE_DBSTATUS_LO
7ce90 4f 4b 41 53 49 44 45 5f 55 53 45 44 3a 20 7b 0a  OKASIDE_USED: {.
7cea0 20 20 20 20 20 20 2a 70 43 75 72 72 65 6e 74 20        *pCurrent 
7ceb0 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
7cec0 6e 4f 75 74 3b 0a 20 20 20 20 20 20 2a 70 48 69  nOut;.      *pHi
7ced0 67 68 77 61 74 65 72 20 3d 20 64 62 2d 3e 6c 6f  ghwater = db->lo
7cee0 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 3b 0a 20  okaside.mxOut;. 
7cef0 20 20 20 20 20 69 66 28 20 72 65 73 65 74 46 6c       if( resetFl
7cf00 61 67 20 29 7b 0a 20 20 20 20 20 20 20 20 64 62  ag ){.        db
7cf10 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75  ->lookaside.mxOu
7cf20 74 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64  t = db->lookasid
7cf30 65 2e 6e 4f 75 74 3b 0a 20 20 20 20 20 20 7d 0a  e.nOut;.      }.
7cf40 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
7cf50 20 7d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20   }.    default: 
7cf60 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
7cf70 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
7cf80 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
7cf90 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
7cfa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
7cfb0 64 20 6f 66 20 73 74 61 74 75 73 2e 63 20 2a 2a  d of status.c **
7cfc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cfd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7cfe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
7cff0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
7d000 67 69 6e 20 66 69 6c 65 20 64 61 74 65 2e 63 20  gin file date.c 
7d010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
7d040 0a 2a 2a 20 32 30 30 33 20 4f 63 74 6f 62 65 72  .** 2003 October
7d050 20 33 31 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   31.**.** The au
7d060 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
7d070 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
7d080 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
7d090 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
7d0a0 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
7d0b0 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
7d0c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
7d0d0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
7d0e0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
7d0f0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
7d100 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
7d110 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
7d120 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
7d130 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
7d140 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
7d150 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
7d160 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
7d170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d190 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
7d1b0 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
7d1c0 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66  contains the C f
7d1d0 75 6e 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d  unctions that im
7d1e0 70 6c 65 6d 65 6e 74 20 64 61 74 65 20 61 6e 64  plement date and
7d1f0 20 74 69 6d 65 0a 2a 2a 20 66 75 6e 63 74 69 6f   time.** functio
7d200 6e 73 20 66 6f 72 20 53 51 4c 69 74 65 2e 20 20  ns for SQLite.  
7d210 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73 20  .**.** There is 
7d220 6f 6e 6c 79 20 6f 6e 65 20 65 78 70 6f 72 74 65  only one exporte
7d230 64 20 73 79 6d 62 6f 6c 20 69 6e 20 74 68 69 73  d symbol in this
7d240 20 66 69 6c 65 20 2d 20 74 68 65 20 66 75 6e 63   file - the func
7d250 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 52  tion.** sqlite3R
7d260 65 67 69 73 74 65 72 44 61 74 65 54 69 6d 65 46  egisterDateTimeF
7d270 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75 6e 64  unctions() found
7d280 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d 20 6f   at the bottom o
7d290 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 41  f the file..** A
7d2a0 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20 68 61  ll other code ha
7d2b0 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a 2a 2a  s file scope..**
7d2c0 0a 2a 2a 20 24 49 64 3a 20 64 61 74 65 2e 63 2c  .** $Id: date.c,
7d2d0 76 20 31 2e 39 34 20 32 30 30 38 2f 31 31 2f 31  v 1.94 2008/11/1
7d2e0 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65  9 09:05:27 danie
7d2f0 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a 0a  lk1977 Exp $.**.
7d300 2a 2a 20 53 51 4c 69 74 65 20 70 72 6f 63 65 73  ** SQLite proces
7d310 73 65 73 20 61 6c 6c 20 74 69 6d 65 73 20 61 6e  ses all times an
7d320 64 20 64 61 74 65 73 20 61 73 20 4a 75 6c 69 61  d dates as Julia
7d330 6e 20 44 61 79 20 6e 75 6d 62 65 72 73 2e 20 20  n Day numbers.  
7d340 54 68 65 0a 2a 2a 20 64 61 74 65 73 20 61 6e 64  The.** dates and
7d350 20 74 69 6d 65 73 20 61 72 65 20 73 74 6f 72 65   times are store
7d360 64 20 61 73 20 74 68 65 20 6e 75 6d 62 65 72 20  d as the number 
7d370 6f 66 20 64 61 79 73 20 73 69 6e 63 65 20 6e 6f  of days since no
7d380 6f 6e 0a 2a 2a 20 69 6e 20 47 72 65 65 6e 77 69  on.** in Greenwi
7d390 63 68 20 6f 6e 20 4e 6f 76 65 6d 62 65 72 20 32  ch on November 2
7d3a0 34 2c 20 34 37 31 34 20 42 2e 43 2e 20 61 63 63  4, 4714 B.C. acc
7d3b0 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 47 72  ording to the Gr
7d3c0 65 67 6f 72 69 61 6e 0a 2a 2a 20 63 61 6c 65 6e  egorian.** calen
7d3d0 64 61 72 20 73 79 73 74 65 6d 2e 20 0a 2a 2a 0a  dar system. .**.
7d3e0 2a 2a 20 31 39 37 30 2d 30 31 2d 30 31 20 30 30  ** 1970-01-01 00
7d3f0 3a 30 30 3a 30 30 20 69 73 20 4a 44 20 32 34 34  :00:00 is JD 244
7d400 30 35 38 37 2e 35 0a 2a 2a 20 32 30 30 30 2d 30  0587.5.** 2000-0
7d410 31 2d 30 31 20 30 30 3a 30 30 3a 30 30 20 69 73  1-01 00:00:00 is
7d420 20 4a 44 20 32 34 35 31 35 34 34 2e 35 0a 2a 2a   JD 2451544.5.**
7d430 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c 65 6d 65  .** This impleme
7d440 6e 74 69 6f 6e 20 72 65 71 75 69 72 65 73 20 79  ntion requires y
7d450 65 61 72 73 20 74 6f 20 62 65 20 65 78 70 72 65  ears to be expre
7d460 73 73 65 64 20 61 73 20 61 20 34 2d 64 69 67 69  ssed as a 4-digi
7d470 74 20 6e 75 6d 62 65 72 0a 2a 2a 20 77 68 69 63  t number.** whic
7d480 68 20 6d 65 61 6e 73 20 74 68 61 74 20 6f 6e 6c  h means that onl
7d490 79 20 64 61 74 65 73 20 62 65 74 77 65 65 6e 20  y dates between 
7d4a0 30 30 30 30 2d 30 31 2d 30 31 20 61 6e 64 20 39  0000-01-01 and 9
7d4b0 39 39 39 2d 31 32 2d 33 31 20 63 61 6e 0a 2a 2a  999-12-31 can.**
7d4c0 20 62 65 20 72 65 70 72 65 73 65 6e 74 65 64 2c   be represented,
7d4d0 20 65 76 65 6e 20 74 68 6f 75 67 68 20 6a 75 6c   even though jul
7d4e0 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72 73 20  ian day numbers 
7d4f0 61 6c 6c 6f 77 20 61 20 6d 75 63 68 20 77 69 64  allow a much wid
7d500 65 72 0a 2a 2a 20 72 61 6e 67 65 20 6f 66 20 64  er.** range of d
7d510 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ates..**.** The 
7d520 47 72 65 67 6f 72 69 61 6e 20 63 61 6c 65 6e 64  Gregorian calend
7d530 61 72 20 73 79 73 74 65 6d 20 69 73 20 75 73 65  ar system is use
7d540 64 20 66 6f 72 20 61 6c 6c 20 64 61 74 65 73 20  d for all dates 
7d550 61 6e 64 20 74 69 6d 65 73 2c 0a 2a 2a 20 65 76  and times,.** ev
7d560 65 6e 20 74 68 6f 73 65 20 74 68 61 74 20 70 72  en those that pr
7d570 65 64 61 74 65 20 74 68 65 20 47 72 65 67 6f 72  edate the Gregor
7d580 69 61 6e 20 63 61 6c 65 6e 64 61 72 2e 20 20 48  ian calendar.  H
7d590 69 73 74 6f 72 69 61 6e 73 20 75 73 75 61 6c 6c  istorians usuall
7d5a0 79 0a 2a 2a 20 75 73 65 20 74 68 65 20 4a 75 6c  y.** use the Jul
7d5b0 69 61 6e 20 63 61 6c 65 6e 64 61 72 20 66 6f 72  ian calendar for
7d5c0 20 64 61 74 65 73 20 70 72 69 6f 72 20 74 6f 20   dates prior to 
7d5d0 31 35 38 32 2d 31 30 2d 31 35 20 61 6e 64 20 66  1582-10-15 and f
7d5e0 6f 72 20 73 6f 6d 65 0a 2a 2a 20 64 61 74 65 73  or some.** dates
7d5f0 20 61 66 74 65 72 77 61 72 64 73 2c 20 64 65 70   afterwards, dep
7d600 65 6e 64 69 6e 67 20 6f 6e 20 6c 6f 63 61 6c 65  ending on locale
7d610 2e 20 20 42 65 77 61 72 65 20 6f 66 20 74 68 69  .  Beware of thi
7d620 73 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 2a 2a  s difference..**
7d630 0a 2a 2a 20 54 68 65 20 63 6f 6e 76 65 72 73 69  .** The conversi
7d640 6f 6e 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  on algorithms ar
7d650 65 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 61  e implemented ba
7d660 73 65 64 20 6f 6e 20 64 65 73 63 72 69 70 74 69  sed on descripti
7d670 6f 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 66 6f  ons.** in the fo
7d680 6c 6c 6f 77 69 6e 67 20 74 65 78 74 3a 0a 2a 2a  llowing text:.**
7d690 0a 2a 2a 20 20 20 20 20 20 4a 65 61 6e 20 4d 65  .**      Jean Me
7d6a0 65 75 73 0a 2a 2a 20 20 20 20 20 20 41 73 74 72  eus.**      Astr
7d6b0 6f 6e 6f 6d 69 63 61 6c 20 41 6c 67 6f 72 69 74  onomical Algorit
7d6c0 68 6d 73 2c 20 32 6e 64 20 45 64 69 74 69 6f 6e  hms, 2nd Edition
7d6d0 2c 20 31 39 39 38 0a 2a 2a 20 20 20 20 20 20 49  , 1998.**      I
7d6e0 53 42 4d 20 30 2d 39 34 33 33 39 36 2d 36 31 2d  SBM 0-943396-61-
7d6f0 31 0a 2a 2a 20 20 20 20 20 20 57 69 6c 6c 6d 61  1.**      Willma
7d700 6e 6e 2d 42 65 6c 6c 2c 20 49 6e 63 0a 2a 2a 20  nn-Bell, Inc.** 
7d710 20 20 20 20 20 52 69 63 68 6d 6f 6e 64 2c 20 56       Richmond, V
7d720 69 72 67 69 6e 69 61 20 28 55 53 41 29 0a 2a 2f  irginia (USA).*/
7d730 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65  .#include <ctype
7d740 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 74 69  .h>.#include <ti
7d750 6d 65 2e 68 3e 0a 0a 23 69 66 6e 64 65 66 20 53  me.h>..#ifndef S
7d760 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54  QLITE_OMIT_DATET
7d770 49 4d 45 5f 46 55 4e 43 53 0a 0a 2f 2a 0a 2a 2a  IME_FUNCS../*.**
7d780 20 4f 6e 20 72 65 63 65 6e 74 20 57 69 6e 64 6f   On recent Windo
7d790 77 73 20 70 6c 61 74 66 6f 72 6d 73 2c 20 74 68  ws platforms, th
7d7a0 65 20 6c 6f 63 61 6c 74 69 6d 65 5f 73 28 29 20  e localtime_s() 
7d7b0 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 76 61 69  function is avai
7d7c0 6c 61 62 6c 65 0a 2a 2a 20 61 73 20 70 61 72 74  lable.** as part
7d7d0 20 6f 66 20 74 68 65 20 22 53 65 63 75 72 65 20   of the "Secure 
7d7e0 43 52 54 22 2e 20 49 74 20 69 73 20 65 73 73 65  CRT". It is esse
7d7f0 6e 74 69 61 6c 6c 79 20 65 71 75 69 76 61 6c 65  ntially equivale
7d800 6e 74 20 74 6f 20 0a 2a 2a 20 6c 6f 63 61 6c 74  nt to .** localt
7d810 69 6d 65 5f 72 28 29 20 61 76 61 69 6c 61 62 6c  ime_r() availabl
7d820 65 20 75 6e 64 65 72 20 6d 6f 73 74 20 50 4f 53  e under most POS
7d830 49 58 20 70 6c 61 74 66 6f 72 6d 73 2c 20 65 78  IX platforms, ex
7d840 63 65 70 74 20 74 68 61 74 20 74 68 65 20 0a 2a  cept that the .*
7d850 2a 20 6f 72 64 65 72 20 6f 66 20 74 68 65 20 70  * order of the p
7d860 61 72 61 6d 65 74 65 72 73 20 69 73 20 72 65 76  arameters is rev
7d870 65 72 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  ersed..**.** See
7d880 20 68 74 74 70 3a 2f 2f 6d 73 64 6e 2e 6d 69 63   http://msdn.mic
7d890 72 6f 73 6f 66 74 2e 63 6f 6d 2f 65 6e 2d 75 73  rosoft.com/en-us
7d8a0 2f 6c 69 62 72 61 72 79 2f 61 34 34 32 78 33 79  /library/a442x3y
7d8b0 65 28 56 53 2e 38 30 29 2e 61 73 70 78 2e 0a 2a  e(VS.80).aspx..*
7d8c0 2a 0a 2a 2a 20 49 66 20 74 68 65 20 75 73 65 72  *.** If the user
7d8d0 20 68 61 73 20 6e 6f 74 20 69 6e 64 69 63 61 74   has not indicat
7d8e0 65 64 20 74 6f 20 75 73 65 20 6c 6f 63 61 6c 74  ed to use localt
7d8f0 69 6d 65 5f 72 28 29 20 6f 72 20 6c 6f 63 61 6c  ime_r() or local
7d900 74 69 6d 65 5f 73 28 29 0a 2a 2a 20 61 6c 72 65  time_s().** alre
7d910 61 64 79 2c 20 63 68 65 63 6b 20 66 6f 72 20 61  ady, check for a
7d920 6e 20 4d 53 56 43 20 62 75 69 6c 64 20 65 6e 76  n MSVC build env
7d930 69 72 6f 6e 6d 65 6e 74 20 74 68 61 74 20 70 72  ironment that pr
7d940 6f 76 69 64 65 73 20 0a 2a 2a 20 6c 6f 63 61 6c  ovides .** local
7d950 74 69 6d 65 5f 73 28 29 2e 0a 2a 2f 0a 23 69 66  time_s()..*/.#if
7d960 20 21 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c   !defined(HAVE_L
7d970 4f 43 41 4c 54 49 4d 45 5f 52 29 20 26 26 20 21  OCALTIME_R) && !
7d980 64 65 66 69 6e 65 64 28 48 41 56 45 5f 4c 4f 43  defined(HAVE_LOC
7d990 41 4c 54 49 4d 45 5f 53 29 20 26 26 20 5c 0a 20  ALTIME_S) && \. 
7d9a0 20 20 20 20 64 65 66 69 6e 65 64 28 5f 4d 53 43      defined(_MSC
7d9b0 5f 56 45 52 29 20 26 26 20 64 65 66 69 6e 65 64  _VER) && defined
7d9c0 28 5f 43 52 54 5f 49 4e 53 45 43 55 52 45 5f 44  (_CRT_INSECURE_D
7d9d0 45 50 52 45 43 41 54 45 29 0a 23 64 65 66 69 6e  EPRECATE).#defin
7d9e0 65 20 48 41 56 45 5f 4c 4f 43 41 4c 54 49 4d 45  e HAVE_LOCALTIME
7d9f0 5f 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  _S 1.#endif../*.
7da00 2a 2a 20 41 20 73 74 72 75 63 74 75 72 65 20 66  ** A structure f
7da10 6f 72 20 68 6f 6c 64 69 6e 67 20 61 20 73 69 6e  or holding a sin
7da20 67 6c 65 20 64 61 74 65 20 61 6e 64 20 74 69 6d  gle date and tim
7da30 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  e..*/.typedef st
7da40 72 75 63 74 20 44 61 74 65 54 69 6d 65 20 44 61  ruct DateTime Da
7da50 74 65 54 69 6d 65 3b 0a 73 74 72 75 63 74 20 44  teTime;.struct D
7da60 61 74 65 54 69 6d 65 20 7b 0a 20 20 73 71 6c 69  ateTime {.  sqli
7da70 74 65 33 5f 69 6e 74 36 34 20 69 4a 44 3b 20 2f  te3_int64 iJD; /
7da80 2a 20 54 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  * The julian day
7da90 20 6e 75 6d 62 65 72 20 74 69 6d 65 73 20 38 36   number times 86
7daa0 34 30 30 30 30 30 20 2a 2f 0a 20 20 69 6e 74 20  400000 */.  int 
7dab0 59 2c 20 4d 2c 20 44 3b 20 20 20 20 20 20 20 2f  Y, M, D;       /
7dac0 2a 20 59 65 61 72 2c 20 6d 6f 6e 74 68 2c 20 61  * Year, month, a
7dad0 6e 64 20 64 61 79 20 2a 2f 0a 20 20 69 6e 74 20  nd day */.  int 
7dae0 68 2c 20 6d 3b 20 20 20 20 20 20 20 20 20 20 2f  h, m;          /
7daf0 2a 20 48 6f 75 72 20 61 6e 64 20 6d 69 6e 75 74  * Hour and minut
7db00 65 73 20 2a 2f 0a 20 20 69 6e 74 20 74 7a 3b 20  es */.  int tz; 
7db10 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 69             /* Ti
7db20 6d 65 7a 6f 6e 65 20 6f 66 66 73 65 74 20 69 6e  mezone offset in
7db30 20 6d 69 6e 75 74 65 73 20 2a 2f 0a 20 20 64 6f   minutes */.  do
7db40 75 62 6c 65 20 73 3b 20 20 20 20 20 20 20 20 20  uble s;         
7db50 20 2f 2a 20 53 65 63 6f 6e 64 73 20 2a 2f 0a 20   /* Seconds */. 
7db60 20 63 68 61 72 20 76 61 6c 69 64 59 4d 44 3b 20   char validYMD; 
7db70 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 59      /* True if Y
7db80 2c 4d 2c 44 20 61 72 65 20 76 61 6c 69 64 20 2a  ,M,D are valid *
7db90 2f 0a 20 20 63 68 61 72 20 76 61 6c 69 64 48 4d  /.  char validHM
7dba0 53 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  S;     /* True i
7dbb0 66 20 68 2c 6d 2c 73 20 61 72 65 20 76 61 6c 69  f h,m,s are vali
7dbc0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69  d */.  char vali
7dbd0 64 4a 44 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  dJD;      /* Tru
7dbe0 65 20 69 66 20 69 4a 44 20 69 73 20 76 61 6c 69  e if iJD is vali
7dbf0 64 20 2a 2f 0a 20 20 63 68 61 72 20 76 61 6c 69  d */.  char vali
7dc00 64 54 5a 3b 20 20 20 20 20 20 2f 2a 20 54 72 75  dTZ;      /* Tru
7dc10 65 20 69 66 20 74 7a 20 69 73 20 76 61 6c 69 64  e if tz is valid
7dc20 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a 20 43   */.};.../*.** C
7dc30 6f 6e 76 65 72 74 20 7a 44 61 74 65 20 69 6e 74  onvert zDate int
7dc40 6f 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 69 6e  o one or more in
7dc50 74 65 67 65 72 73 2e 20 20 41 64 64 69 74 69 6f  tegers.  Additio
7dc60 6e 61 6c 20 61 72 67 75 6d 65 6e 74 73 0a 2a 2a  nal arguments.**
7dc70 20 63 6f 6d 65 20 69 6e 20 67 72 6f 75 70 73 20   come in groups 
7dc80 6f 66 20 35 20 61 73 20 66 6f 6c 6c 6f 77 73 3a  of 5 as follows:
7dc90 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 4e 20 20  .**.**       N  
7dca0 20 20 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 64       number of d
7dcb0 69 67 69 74 73 20 69 6e 20 74 68 65 20 69 6e 74  igits in the int
7dcc0 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 6d 69  eger.**       mi
7dcd0 6e 20 20 20 20 20 6d 69 6e 69 6d 75 6d 20 61 6c  n     minimum al
7dce0 6c 6f 77 65 64 20 76 61 6c 75 65 20 6f 66 20 74  lowed value of t
7dcf0 68 65 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20  he integer.**   
7dd00 20 20 20 20 6d 61 78 20 20 20 20 20 6d 61 78 69      max     maxi
7dd10 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 76 61 6c 75  mum allowed valu
7dd20 65 20 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72  e of the integer
7dd30 0a 2a 2a 20 20 20 20 20 20 20 6e 65 78 74 43 20  .**       nextC 
7dd40 20 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65    first characte
7dd50 72 20 61 66 74 65 72 20 74 68 65 20 69 6e 74 65  r after the inte
7dd60 67 65 72 0a 2a 2a 20 20 20 20 20 20 20 70 56 61  ger.**       pVa
7dd70 6c 20 20 20 20 77 68 65 72 65 20 74 6f 20 77 72  l    where to wr
7dd80 69 74 65 20 74 68 65 20 69 6e 74 65 67 65 72 73  ite the integers
7dd90 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 43 6f   value..**.** Co
7dda0 6e 76 65 72 73 69 6f 6e 73 20 63 6f 6e 74 69 6e  nversions contin
7ddb0 75 65 20 75 6e 74 69 6c 20 6f 6e 65 20 77 69 74  ue until one wit
7ddc0 68 20 6e 65 78 74 43 3d 3d 30 20 69 73 20 65 6e  h nextC==0 is en
7ddd0 63 6f 75 6e 74 65 72 65 64 2e 0a 2a 2a 20 54 68  countered..** Th
7dde0 65 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72  e function retur
7ddf0 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ns the number of
7de00 20 73 75 63 63 65 73 73 66 75 6c 20 63 6f 6e 76   successful conv
7de10 65 72 73 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74  ersions..*/.stat
7de20 69 63 20 69 6e 74 20 67 65 74 44 69 67 69 74 73  ic int getDigits
7de30 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61  (const char *zDa
7de40 74 65 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c  te, ...){.  va_l
7de50 69 73 74 20 61 70 3b 0a 20 20 69 6e 74 20 76 61  ist ap;.  int va
7de60 6c 3b 0a 20 20 69 6e 74 20 4e 3b 0a 20 20 69 6e  l;.  int N;.  in
7de70 74 20 6d 69 6e 3b 0a 20 20 69 6e 74 20 6d 61 78  t min;.  int max
7de80 3b 0a 20 20 69 6e 74 20 6e 65 78 74 43 3b 0a 20  ;.  int nextC;. 
7de90 20 69 6e 74 20 2a 70 56 61 6c 3b 0a 20 20 69 6e   int *pVal;.  in
7dea0 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20 76 61 5f  t cnt = 0;.  va_
7deb0 73 74 61 72 74 28 61 70 2c 20 7a 44 61 74 65 29  start(ap, zDate)
7dec0 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 4e 20 3d 20  ;.  do{.    N = 
7ded0 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b  va_arg(ap, int);
7dee0 0a 20 20 20 20 6d 69 6e 20 3d 20 76 61 5f 61 72  .    min = va_ar
7def0 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
7df00 6d 61 78 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  max = va_arg(ap,
7df10 20 69 6e 74 29 3b 0a 20 20 20 20 6e 65 78 74 43   int);.    nextC
7df20 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
7df30 74 29 3b 0a 20 20 20 20 70 56 61 6c 20 3d 20 76  t);.    pVal = v
7df40 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 2a 29 3b  a_arg(ap, int*);
7df50 0a 20 20 20 20 76 61 6c 20 3d 20 30 3b 0a 20 20  .    val = 0;.  
7df60 20 20 77 68 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a    while( N-- ){.
7df70 20 20 20 20 20 20 69 66 28 20 21 69 73 64 69 67        if( !isdig
7df80 69 74 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20  it(*(u8*)zDate) 
7df90 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
7dfa0 65 6e 64 5f 67 65 74 44 69 67 69 74 73 3b 0a 20  end_getDigits;. 
7dfb0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 76 61 6c       }.      val
7dfc0 20 3d 20 76 61 6c 2a 31 30 20 2b 20 2a 7a 44 61   = val*10 + *zDa
7dfd0 74 65 20 2d 20 27 30 27 3b 0a 20 20 20 20 20 20  te - '0';.      
7dfe0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20  zDate++;.    }. 
7dff0 20 20 20 69 66 28 20 76 61 6c 3c 6d 69 6e 20 7c     if( val<min |
7e000 7c 20 76 61 6c 3e 6d 61 78 20 7c 7c 20 28 6e 65  | val>max || (ne
7e010 78 74 43 21 3d 30 20 26 26 20 6e 65 78 74 43 21  xtC!=0 && nextC!
7e020 3d 2a 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20  =*zDate) ){.    
7e030 20 20 67 6f 74 6f 20 65 6e 64 5f 67 65 74 44 69    goto end_getDi
7e040 67 69 74 73 3b 0a 20 20 20 20 7d 0a 20 20 20 20  gits;.    }.    
7e050 2a 70 56 61 6c 20 3d 20 76 61 6c 3b 0a 20 20 20  *pVal = val;.   
7e060 20 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 63 6e   zDate++;.    cn
7e070 74 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 6e  t++;.  }while( n
7e080 65 78 74 43 20 29 3b 0a 65 6e 64 5f 67 65 74 44  extC );.end_getD
7e090 69 67 69 74 73 3a 0a 20 20 76 61 5f 65 6e 64 28  igits:.  va_end(
7e0a0 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6e  ap);.  return cn
7e0b0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64  t;.}../*.** Read
7e0c0 20 74 65 78 74 20 66 72 6f 6d 20 7a 5b 5d 20 61   text from z[] a
7e0d0 6e 64 20 63 6f 6e 76 65 72 74 20 69 6e 74 6f 20  nd convert into 
7e0e0 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74  a floating point
7e0f0 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e   number.  Return
7e100 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  .** the number o
7e110 66 20 64 69 67 69 74 73 20 63 6f 6e 76 65 72 74  f digits convert
7e120 65 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 67  ed..*/.#define g
7e130 65 74 56 61 6c 75 65 20 73 71 6c 69 74 65 33 41  etValue sqlite3A
7e140 74 6f 46 0a 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65  toF../*.** Parse
7e150 20 61 20 74 69 6d 65 7a 6f 6e 65 20 65 78 74 65   a timezone exte
7e160 6e 73 69 6f 6e 20 6f 6e 20 74 68 65 20 65 6e 64  nsion on the end
7e170 20 6f 66 20 61 20 64 61 74 65 2d 74 69 6d 65 2e   of a date-time.
7e180 0a 2a 2a 20 54 68 65 20 65 78 74 65 6e 73 69 6f  .** The extensio
7e190 6e 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  n is of the form
7e1a0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 28  :.**.**        (
7e1b0 2b 2f 2d 29 48 48 3a 4d 4d 0a 2a 2a 0a 2a 2a 20  +/-)HH:MM.**.** 
7e1c0 4f 72 20 74 68 65 20 22 7a 75 6c 75 22 20 6e 6f  Or the "zulu" no
7e1d0 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  tation:.**.**   
7e1e0 20 20 20 20 20 5a 0a 2a 2a 0a 2a 2a 20 49 66 20       Z.**.** If 
7e1f0 74 68 65 20 70 61 72 73 65 20 69 73 20 73 75 63  the parse is suc
7e200 63 65 73 73 66 75 6c 2c 20 77 72 69 74 65 20 74  cessful, write t
7e210 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 6e  he number of min
7e220 75 74 65 73 0a 2a 2a 20 6f 66 20 63 68 61 6e 67  utes.** of chang
7e230 65 20 69 6e 20 70 2d 3e 74 7a 20 61 6e 64 20 72  e in p->tz and r
7e240 65 74 75 72 6e 20 30 2e 20 20 49 66 20 61 20 70  eturn 0.  If a p
7e250 61 72 73 65 72 20 65 72 72 6f 72 20 6f 63 63 75  arser error occu
7e260 72 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 6e 6f  rs,.** return no
7e270 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 41 20  n-zero..**.** A 
7e280 6d 69 73 73 69 6e 67 20 73 70 65 63 69 66 69 65  missing specifie
7e290 72 20 69 73 20 6e 6f 74 20 63 6f 6e 73 69 64 65  r is not conside
7e2a0 72 65 64 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f  red an error..*/
7e2b0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 72 73  .static int pars
7e2c0 65 54 69 6d 65 7a 6f 6e 65 28 63 6f 6e 73 74 20  eTimezone(const 
7e2d0 63 68 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74  char *zDate, Dat
7e2e0 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74  eTime *p){.  int
7e2f0 20 73 67 6e 20 3d 20 30 3b 0a 20 20 69 6e 74 20   sgn = 0;.  int 
7e300 6e 48 72 2c 20 6e 4d 6e 3b 0a 20 20 69 6e 74 20  nHr, nMn;.  int 
7e310 63 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70  c;.  while( issp
7e320 61 63 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29  ace(*(u8*)zDate)
7e330 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a 20   ){ zDate++; }. 
7e340 20 70 2d 3e 74 7a 20 3d 20 30 3b 0a 20 20 63 20   p->tz = 0;.  c 
7e350 3d 20 2a 7a 44 61 74 65 3b 0a 20 20 69 66 28 20  = *zDate;.  if( 
7e360 63 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 73 67  c=='-' ){.    sg
7e370 6e 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 20  n = -1;.  }else 
7e380 69 66 28 20 63 3d 3d 27 2b 27 20 29 7b 0a 20 20  if( c=='+' ){.  
7e390 20 20 73 67 6e 20 3d 20 2b 31 3b 0a 20 20 7d 65    sgn = +1;.  }e
7e3a0 6c 73 65 20 69 66 28 20 63 3d 3d 27 5a 27 20 7c  lse if( c=='Z' |
7e3b0 7c 20 63 3d 3d 27 7a 27 20 29 7b 0a 20 20 20 20  | c=='z' ){.    
7e3c0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 67 6f 74  zDate++;.    got
7e3d0 6f 20 7a 75 6c 75 5f 74 69 6d 65 3b 0a 20 20 7d  o zulu_time;.  }
7e3e0 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e  else{.    return
7e3f0 20 63 21 3d 30 3b 0a 20 20 7d 0a 20 20 7a 44 61   c!=0;.  }.  zDa
7e400 74 65 2b 2b 3b 0a 20 20 69 66 28 20 67 65 74 44  te++;.  if( getD
7e410 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32 2c 20  igits(zDate, 2, 
7e420 30 2c 20 31 34 2c 20 27 3a 27 2c 20 26 6e 48 72  0, 14, ':', &nHr
7e430 2c 20 32 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26  , 2, 0, 59, 0, &
7e440 6e 4d 6e 29 21 3d 32 20 29 7b 0a 20 20 20 20 72  nMn)!=2 ){.    r
7e450 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 7a  eturn 1;.  }.  z
7e460 44 61 74 65 20 2b 3d 20 35 3b 0a 20 20 70 2d 3e  Date += 5;.  p->
7e470 74 7a 20 3d 20 73 67 6e 2a 28 6e 4d 6e 20 2b 20  tz = sgn*(nMn + 
7e480 6e 48 72 2a 36 30 29 3b 0a 7a 75 6c 75 5f 74 69  nHr*60);.zulu_ti
7e490 6d 65 3a 0a 20 20 77 68 69 6c 65 28 20 69 73 73  me:.  while( iss
7e4a0 70 61 63 65 28 2a 28 75 38 2a 29 7a 44 61 74 65  pace(*(u8*)zDate
7e4b0 29 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d 0a  ) ){ zDate++; }.
7e4c0 20 20 72 65 74 75 72 6e 20 2a 7a 44 61 74 65 21    return *zDate!
7e4d0 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  =0;.}../*.** Par
7e4e0 73 65 20 74 69 6d 65 73 20 6f 66 20 74 68 65 20  se times of the 
7e4f0 66 6f 72 6d 20 48 48 3a 4d 4d 20 6f 72 20 48 48  form HH:MM or HH
7e500 3a 4d 4d 3a 53 53 20 6f 72 20 48 48 3a 4d 4d 3a  :MM:SS or HH:MM:
7e510 53 53 2e 46 46 46 46 2e 0a 2a 2a 20 54 68 65 20  SS.FFFF..** The 
7e520 48 48 2c 20 4d 4d 2c 20 61 6e 64 20 53 53 20 6d  HH, MM, and SS m
7e530 75 73 74 20 65 61 63 68 20 62 65 20 65 78 61 63  ust each be exac
7e540 74 6c 79 20 32 20 64 69 67 69 74 73 2e 20 20 54  tly 2 digits.  T
7e550 68 65 0a 2a 2a 20 66 72 61 63 74 69 6f 6e 61 6c  he.** fractional
7e560 20 73 65 63 6f 6e 64 73 20 46 46 46 46 20 63 61   seconds FFFF ca
7e570 6e 20 62 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  n be one or more
7e580 20 64 69 67 69 74 73 2e 0a 2a 2a 0a 2a 2a 20 52   digits..**.** R
7e590 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65  eturn 1 if there
7e5a0 20 69 73 20 61 20 70 61 72 73 69 6e 67 20 65 72   is a parsing er
7e5b0 72 6f 72 20 61 6e 64 20 30 20 6f 6e 20 73 75 63  ror and 0 on suc
7e5c0 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cess..*/.static 
7e5d0 69 6e 74 20 70 61 72 73 65 48 68 4d 6d 53 73 28  int parseHhMmSs(
7e5e0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 61 74  const char *zDat
7e5f0 65 2c 20 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  e, DateTime *p){
7e600 0a 20 20 69 6e 74 20 68 2c 20 6d 2c 20 73 3b 0a  .  int h, m, s;.
7e610 20 20 64 6f 75 62 6c 65 20 6d 73 20 3d 20 30 2e    double ms = 0.
7e620 30 3b 0a 20 20 69 66 28 20 67 65 74 44 69 67 69  0;.  if( getDigi
7e630 74 73 28 7a 44 61 74 65 2c 20 32 2c 20 30 2c 20  ts(zDate, 2, 0, 
7e640 32 34 2c 20 27 3a 27 2c 20 26 68 2c 20 32 2c 20  24, ':', &h, 2, 
7e650 30 2c 20 35 39 2c 20 30 2c 20 26 6d 29 21 3d 32  0, 59, 0, &m)!=2
7e660 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31   ){.    return 1
7e670 3b 0a 20 20 7d 0a 20 20 7a 44 61 74 65 20 2b 3d  ;.  }.  zDate +=
7e680 20 35 3b 0a 20 20 69 66 28 20 2a 7a 44 61 74 65   5;.  if( *zDate
7e690 3d 3d 27 3a 27 20 29 7b 0a 20 20 20 20 7a 44 61  ==':' ){.    zDa
7e6a0 74 65 2b 2b 3b 0a 20 20 20 20 69 66 28 20 67 65  te++;.    if( ge
7e6b0 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 20 32  tDigits(zDate, 2
7e6c0 2c 20 30 2c 20 35 39 2c 20 30 2c 20 26 73 29 21  , 0, 59, 0, &s)!
7e6d0 3d 31 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =1 ){.      retu
7e6e0 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
7e6f0 7a 44 61 74 65 20 2b 3d 20 32 3b 0a 20 20 20 20  zDate += 2;.    
7e700 69 66 28 20 2a 7a 44 61 74 65 3d 3d 27 2e 27 20  if( *zDate=='.' 
7e710 26 26 20 69 73 64 69 67 69 74 28 28 75 38 29 7a  && isdigit((u8)z
7e720 44 61 74 65 5b 31 5d 29 20 29 7b 0a 20 20 20 20  Date[1]) ){.    
7e730 20 20 64 6f 75 62 6c 65 20 72 53 63 61 6c 65 20    double rScale 
7e740 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 7a 44 61  = 1.0;.      zDa
7e750 74 65 2b 2b 3b 0a 20 20 20 20 20 20 77 68 69 6c  te++;.      whil
7e760 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a  e( isdigit(*(u8*
7e770 29 7a 44 61 74 65 29 20 29 7b 0a 20 20 20 20 20  )zDate) ){.     
7e780 20 20 20 6d 73 20 3d 20 6d 73 2a 31 30 2e 30 20     ms = ms*10.0 
7e790 2b 20 2a 7a 44 61 74 65 20 2d 20 27 30 27 3b 0a  + *zDate - '0';.
7e7a0 20 20 20 20 20 20 20 20 72 53 63 61 6c 65 20 2a          rScale *
7e7b0 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20  = 10.0;.        
7e7c0 7a 44 61 74 65 2b 2b 3b 0a 20 20 20 20 20 20 7d  zDate++;.      }
7e7d0 0a 20 20 20 20 20 20 6d 73 20 2f 3d 20 72 53 63  .      ms /= rSc
7e7e0 61 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ale;.    }.  }el
7e7f0 73 65 7b 0a 20 20 20 20 73 20 3d 20 30 3b 0a 20  se{.    s = 0;. 
7e800 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20   }.  p->validJD 
7e810 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c 69 64 48  = 0;.  p->validH
7e820 4d 53 20 3d 20 31 3b 0a 20 20 70 2d 3e 68 20 3d  MS = 1;.  p->h =
7e830 20 68 3b 0a 20 20 70 2d 3e 6d 20 3d 20 6d 3b 0a   h;.  p->m = m;.
7e840 20 20 70 2d 3e 73 20 3d 20 73 20 2b 20 6d 73 3b    p->s = s + ms;
7e850 0a 20 20 69 66 28 20 70 61 72 73 65 54 69 6d 65  .  if( parseTime
7e860 7a 6f 6e 65 28 7a 44 61 74 65 2c 20 70 29 20 29  zone(zDate, p) )
7e870 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 70 2d 3e   return 1;.  p->
7e880 76 61 6c 69 64 54 5a 20 3d 20 70 2d 3e 74 7a 21  validTZ = p->tz!
7e890 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a  =0;.  return 0;.
7e8a0 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
7e8b0 20 66 72 6f 6d 20 59 59 59 59 2d 4d 4d 2d 44 44   from YYYY-MM-DD
7e8c0 20 48 48 3a 4d 4d 3a 53 53 20 74 6f 20 6a 75 6c   HH:MM:SS to jul
7e8d0 69 61 6e 20 64 61 79 2e 20 20 57 65 20 61 6c 77  ian day.  We alw
7e8e0 61 79 73 20 61 73 73 75 6d 65 0a 2a 2a 20 74 68  ays assume.** th
7e8f0 61 74 20 74 68 65 20 59 59 59 59 2d 4d 4d 2d 44  at the YYYY-MM-D
7e900 44 20 69 73 20 61 63 63 6f 72 64 69 6e 67 20 74  D is according t
7e910 6f 20 74 68 65 20 47 72 65 67 6f 72 69 61 6e 20  o the Gregorian 
7e920 63 61 6c 65 6e 64 61 72 2e 0a 2a 2a 0a 2a 2a 20  calendar..**.** 
7e930 52 65 66 65 72 65 6e 63 65 3a 20 20 4d 65 65 75  Reference:  Meeu
7e940 73 20 70 61 67 65 20 36 31 0a 2a 2f 0a 73 74 61  s page 61.*/.sta
7e950 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74 65  tic void compute
7e960 4a 44 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b  JD(DateTime *p){
7e970 0a 20 20 69 6e 74 20 59 2c 20 4d 2c 20 44 2c 20  .  int Y, M, D, 
7e980 41 2c 20 42 2c 20 58 31 2c 20 58 32 3b 0a 0a 20  A, B, X1, X2;.. 
7e990 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 4a 44 20   if( p->validJD 
7e9a0 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
7e9b0 70 2d 3e 76 61 6c 69 64 59 4d 44 20 29 7b 0a 20  p->validYMD ){. 
7e9c0 20 20 20 59 20 3d 20 70 2d 3e 59 3b 0a 20 20 20     Y = p->Y;.   
7e9d0 20 4d 20 3d 20 70 2d 3e 4d 3b 0a 20 20 20 20 44   M = p->M;.    D
7e9e0 20 3d 20 70 2d 3e 44 3b 0a 20 20 7d 65 6c 73 65   = p->D;.  }else
7e9f0 7b 0a 20 20 20 20 59 20 3d 20 32 30 30 30 3b 20  {.    Y = 2000; 
7ea00 20 2f 2a 20 49 66 20 6e 6f 20 59 4d 44 20 73 70   /* If no YMD sp
7ea10 65 63 69 66 69 65 64 2c 20 61 73 73 75 6d 65 20  ecified, assume 
7ea20 32 30 30 30 2d 4a 61 6e 2d 30 31 20 2a 2f 0a 20  2000-Jan-01 */. 
7ea30 20 20 20 4d 20 3d 20 31 3b 0a 20 20 20 20 44 20     M = 1;.    D 
7ea40 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4d  = 1;.  }.  if( M
7ea50 3c 3d 32 20 29 7b 0a 20 20 20 20 59 2d 2d 3b 0a  <=2 ){.    Y--;.
7ea60 20 20 20 20 4d 20 2b 3d 20 31 32 3b 0a 20 20 7d      M += 12;.  }
7ea70 0a 20 20 41 20 3d 20 59 2f 31 30 30 3b 0a 20 20  .  A = Y/100;.  
7ea80 42 20 3d 20 32 20 2d 20 41 20 2b 20 28 41 2f 34  B = 2 - A + (A/4
7ea90 29 3b 0a 20 20 58 31 20 3d 20 33 36 35 2e 32 35  );.  X1 = 365.25
7eaa0 2a 28 59 2b 34 37 31 36 29 3b 0a 20 20 58 32 20  *(Y+4716);.  X2 
7eab0 3d 20 33 30 2e 36 30 30 31 2a 28 4d 2b 31 29 3b  = 30.6001*(M+1);
7eac0 0a 20 20 70 2d 3e 69 4a 44 20 3d 20 28 58 31 20  .  p->iJD = (X1 
7ead0 2b 20 58 32 20 2b 20 44 20 2b 20 42 20 2d 20 31  + X2 + D + B - 1
7eae0 35 32 34 2e 35 29 2a 38 36 34 30 30 30 30 30 3b  524.5)*86400000;
7eaf0 0a 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20  .  p->validJD = 
7eb00 31 3b 0a 20 20 69 66 28 20 70 2d 3e 76 61 6c 69  1;.  if( p->vali
7eb10 64 48 4d 53 20 29 7b 0a 20 20 20 20 70 2d 3e 69  dHMS ){.    p->i
7eb20 4a 44 20 2b 3d 20 70 2d 3e 68 2a 33 36 30 30 30  JD += p->h*36000
7eb30 30 30 20 2b 20 70 2d 3e 6d 2a 36 30 30 30 30 20  00 + p->m*60000 
7eb40 2b 20 70 2d 3e 73 2a 31 30 30 30 3b 0a 20 20 20  + p->s*1000;.   
7eb50 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 54 5a 20   if( p->validTZ 
7eb60 29 7b 0a 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  ){.      p->iJD 
7eb70 2d 3d 20 70 2d 3e 74 7a 2a 36 30 30 30 30 3b 0a  -= p->tz*60000;.
7eb80 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 59 4d        p->validYM
7eb90 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e  D = 0;.      p->
7eba0 76 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20  validHMS = 0;.  
7ebb0 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d      p->validTZ =
7ebc0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a   0;.    }.  }.}.
7ebd0 0a 2f 2a 0a 2a 2a 20 50 61 72 73 65 20 64 61 74  ./*.** Parse dat
7ebe0 65 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a  es of the form.*
7ebf0 2a 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d  *.**     YYYY-MM
7ec00 2d 44 44 20 48 48 3a 4d 4d 3a 53 53 2e 46 46 46  -DD HH:MM:SS.FFF
7ec10 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d 4d 4d 2d  .**     YYYY-MM-
7ec20 44 44 20 48 48 3a 4d 4d 3a 53 53 0a 2a 2a 20 20  DD HH:MM:SS.**  
7ec30 20 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48     YYYY-MM-DD HH
7ec40 3a 4d 4d 0a 2a 2a 20 20 20 20 20 59 59 59 59 2d  :MM.**     YYYY-
7ec50 4d 4d 2d 44 44 0a 2a 2a 0a 2a 2a 20 57 72 69 74  MM-DD.**.** Writ
7ec60 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 74  e the result int
7ec70 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65 20 73  o the DateTime s
7ec80 74 72 75 63 74 75 72 65 20 61 6e 64 20 72 65 74  tructure and ret
7ec90 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75 63 63  urn 0.** on succ
7eca0 65 73 73 20 61 6e 64 20 31 20 69 66 20 74 68 65  ess and 1 if the
7ecb0 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 69 73   input string is
7ecc0 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d   not a well-form
7ecd0 65 64 0a 2a 2a 20 64 61 74 65 2e 0a 2a 2f 0a 73  ed.** date..*/.s
7ece0 74 61 74 69 63 20 69 6e 74 20 70 61 72 73 65 59  tatic int parseY
7ecf0 79 79 79 4d 6d 44 64 28 63 6f 6e 73 74 20 63 68  yyyMmDd(const ch
7ed00 61 72 20 2a 7a 44 61 74 65 2c 20 44 61 74 65 54  ar *zDate, DateT
7ed10 69 6d 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 59  ime *p){.  int Y
7ed20 2c 20 4d 2c 20 44 2c 20 6e 65 67 3b 0a 0a 20 20  , M, D, neg;..  
7ed30 69 66 28 20 7a 44 61 74 65 5b 30 5d 3d 3d 27 2d  if( zDate[0]=='-
7ed40 27 20 29 7b 0a 20 20 20 20 7a 44 61 74 65 2b 2b  ' ){.    zDate++
7ed50 3b 0a 20 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20  ;.    neg = 1;. 
7ed60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65 67 20   }else{.    neg 
7ed70 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67  = 0;.  }.  if( g
7ed80 65 74 44 69 67 69 74 73 28 7a 44 61 74 65 2c 34  etDigits(zDate,4
7ed90 2c 30 2c 39 39 39 39 2c 27 2d 27 2c 26 59 2c 32  ,0,9999,'-',&Y,2
7eda0 2c 31 2c 31 32 2c 27 2d 27 2c 26 4d 2c 32 2c 31  ,1,12,'-',&M,2,1
7edb0 2c 33 31 2c 30 2c 26 44 29 21 3d 33 20 29 7b 0a  ,31,0,&D)!=3 ){.
7edc0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
7edd0 7d 0a 20 20 7a 44 61 74 65 20 2b 3d 20 31 30 3b  }.  zDate += 10;
7ede0 0a 20 20 77 68 69 6c 65 28 20 69 73 73 70 61 63  .  while( isspac
7edf0 65 28 2a 28 75 38 2a 29 7a 44 61 74 65 29 20 7c  e(*(u8*)zDate) |
7ee00 7c 20 27 54 27 3d 3d 2a 28 75 38 2a 29 7a 44 61  | 'T'==*(u8*)zDa
7ee10 74 65 20 29 7b 20 7a 44 61 74 65 2b 2b 3b 20 7d  te ){ zDate++; }
7ee20 0a 20 20 69 66 28 20 70 61 72 73 65 48 68 4d 6d  .  if( parseHhMm
7ee30 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d 30 20  Ss(zDate, p)==0 
7ee40 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 67 6f 74  ){.    /* We got
7ee50 20 74 68 65 20 74 69 6d 65 20 2a 2f 0a 20 20 7d   the time */.  }
7ee60 65 6c 73 65 20 69 66 28 20 2a 7a 44 61 74 65 3d  else if( *zDate=
7ee70 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 76 61 6c  =0 ){.    p->val
7ee80 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 7d 65 6c  idHMS = 0;.  }el
7ee90 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  se{.    return 1
7eea0 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69 64  ;.  }.  p->valid
7eeb0 4a 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76 61 6c  JD = 0;.  p->val
7eec0 69 64 59 4d 44 20 3d 20 31 3b 0a 20 20 70 2d 3e  idYMD = 1;.  p->
7eed0 59 20 3d 20 6e 65 67 20 3f 20 2d 59 20 3a 20 59  Y = neg ? -Y : Y
7eee0 3b 0a 20 20 70 2d 3e 4d 20 3d 20 4d 3b 0a 20 20  ;.  p->M = M;.  
7eef0 70 2d 3e 44 20 3d 20 44 3b 0a 20 20 69 66 28 20  p->D = D;.  if( 
7ef00 70 2d 3e 76 61 6c 69 64 54 5a 20 29 7b 0a 20 20  p->validTZ ){.  
7ef10 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a    computeJD(p);.
7ef20 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
7ef30 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
7ef40 20 74 69 6d 65 20 74 6f 20 74 68 65 20 63 75 72   time to the cur
7ef50 72 65 6e 74 20 74 69 6d 65 20 72 65 70 6f 72 74  rent time report
7ef60 65 64 20 62 79 20 74 68 65 20 56 46 53 0a 2a 2f  ed by the VFS.*/
7ef70 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
7ef80 44 61 74 65 54 69 6d 65 54 6f 43 75 72 72 65 6e  DateTimeToCurren
7ef90 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
7efa0 74 20 2a 63 6f 6e 74 65 78 74 2c 20 44 61 74 65  t *context, Date
7efb0 54 69 6d 65 20 2a 70 29 7b 0a 20 20 64 6f 75 62  Time *p){.  doub
7efc0 6c 65 20 72 3b 0a 20 20 73 71 6c 69 74 65 33 20  le r;.  sqlite3 
7efd0 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  *db = sqlite3_co
7efe0 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28  ntext_db_handle(
7eff0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69  context);.  sqli
7f000 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65  te3OsCurrentTime
7f010 28 64 62 2d 3e 70 56 66 73 2c 20 26 72 29 3b 0a  (db->pVfs, &r);.
7f020 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73 71 6c 69    p->iJD = (sqli
7f030 74 65 33 5f 69 6e 74 36 34 29 28 72 2a 38 36 34  te3_int64)(r*864
7f040 30 30 30 30 30 2e 30 20 2b 20 30 2e 35 29 3b 0a  00000.0 + 0.5);.
7f050 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20 31    p->validJD = 1
7f060 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d  ;.}../*.** Attem
7f070 70 74 20 74 6f 20 70 61 72 73 65 20 74 68 65 20  pt to parse the 
7f080 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 6e 74  given string int
7f090 6f 20 61 20 4a 75 6c 69 61 6e 20 44 61 79 20 4e  o a Julian Day N
7f0a0 75 6d 62 65 72 2e 20 20 52 65 74 75 72 6e 0a 2a  umber.  Return.*
7f0b0 2a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  * the number of 
7f0c0 65 72 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  errors..**.** Th
7f0d0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20  e following are 
7f0e0 61 63 63 65 70 74 61 62 6c 65 20 66 6f 72 6d 73  acceptable forms
7f0f0 20 66 6f 72 20 74 68 65 20 69 6e 70 75 74 20 73   for the input s
7f100 74 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  tring:.**.**    
7f110 20 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48 48 3a    YYYY-MM-DD HH:
7f120 4d 4d 3a 53 53 2e 46 46 46 20 20 2b 2f 2d 48 48  MM:SS.FFF  +/-HH
7f130 3a 4d 4d 0a 2a 2a 20 20 20 20 20 20 44 44 44 44  :MM.**      DDDD
7f140 2e 44 44 20 0a 2a 2a 20 20 20 20 20 20 6e 6f 77  .DD .**      now
7f150 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 66 69  .**.** In the fi
7f160 72 73 74 20 66 6f 72 6d 2c 20 74 68 65 20 2b 2f  rst form, the +/
7f170 2d 48 48 3a 4d 4d 20 69 73 20 61 6c 77 61 79 73  -HH:MM is always
7f180 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20   optional.  The 
7f190 66 72 61 63 74 69 6f 6e 61 6c 0a 2a 2a 20 73 65  fractional.** se
7f1a0 63 6f 6e 64 73 20 65 78 74 65 6e 73 69 6f 6e 20  conds extension 
7f1b0 28 74 68 65 20 22 2e 46 46 46 22 29 20 69 73 20  (the ".FFF") is 
7f1c0 6f 70 74 69 6f 6e 61 6c 2e 20 20 54 68 65 20 73  optional.  The s
7f1d0 65 63 6f 6e 64 73 20 70 6f 72 74 69 6f 6e 0a 2a  econds portion.*
7f1e0 2a 20 28 22 3a 53 53 2e 46 46 46 22 29 20 69 73  * (":SS.FFF") is
7f1f0 20 6f 70 74 69 6f 6e 2e 20 20 54 68 65 20 79 65   option.  The ye
7f200 61 72 20 61 6e 64 20 64 61 74 65 20 63 61 6e 20  ar and date can 
7f210 62 65 20 6f 6d 69 74 74 65 64 20 61 73 20 6c 6f  be omitted as lo
7f220 6e 67 0a 2a 2a 20 61 73 20 74 68 65 72 65 20 69  ng.** as there i
7f230 73 20 61 20 74 69 6d 65 20 73 74 72 69 6e 67 2e  s a time string.
7f240 20 20 54 68 65 20 74 69 6d 65 20 73 74 72 69 6e    The time strin
7f250 67 20 63 61 6e 20 62 65 20 6f 6d 69 74 74 65 64  g can be omitted
7f260 20 61 73 20 6c 6f 6e 67 0a 2a 2a 20 61 73 20 74   as long.** as t
7f270 68 65 72 65 20 69 73 20 61 20 79 65 61 72 20 61  here is a year a
7f280 6e 64 20 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  nd date..*/.stat
7f290 69 63 20 69 6e 74 20 70 61 72 73 65 44 61 74 65  ic int parseDate
7f2a0 4f 72 54 69 6d 65 28 0a 20 20 73 71 6c 69 74 65  OrTime(.  sqlite
7f2b0 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
7f2c0 78 74 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61  xt, .  const cha
7f2d0 72 20 2a 7a 44 61 74 65 2c 20 0a 20 20 44 61 74  r *zDate, .  Dat
7f2e0 65 54 69 6d 65 20 2a 70 0a 29 7b 0a 20 20 69 66  eTime *p.){.  if
7f2f0 28 20 70 61 72 73 65 59 79 79 79 4d 6d 44 64 28  ( parseYyyyMmDd(
7f300 7a 44 61 74 65 2c 70 29 3d 3d 30 20 29 7b 0a 20  zDate,p)==0 ){. 
7f310 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
7f320 65 6c 73 65 20 69 66 28 20 70 61 72 73 65 48 68  else if( parseHh
7f330 4d 6d 53 73 28 7a 44 61 74 65 2c 20 70 29 3d 3d  MmSs(zDate, p)==
7f340 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  0 ){.    return 
7f350 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  0;.  }else if( s
7f360 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 44  qlite3StrICmp(zD
7f370 61 74 65 2c 22 6e 6f 77 22 29 3d 3d 30 29 7b 0a  ate,"now")==0){.
7f380 20 20 20 20 73 65 74 44 61 74 65 54 69 6d 65 54      setDateTimeT
7f390 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74 65 78 74  oCurrent(context
7f3a0 2c 20 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  , p);.    return
7f3b0 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   0;.  }else if( 
7f3c0 73 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28  sqlite3IsNumber(
7f3d0 7a 44 61 74 65 2c 20 30 2c 20 53 51 4c 49 54 45  zDate, 0, SQLITE
7f3e0 5f 55 54 46 38 29 20 29 7b 0a 20 20 20 20 64 6f  _UTF8) ){.    do
7f3f0 75 62 6c 65 20 72 3b 0a 20 20 20 20 67 65 74 56  uble r;.    getV
7f400 61 6c 75 65 28 7a 44 61 74 65 2c 20 26 72 29 3b  alue(zDate, &r);
7f410 0a 20 20 20 20 70 2d 3e 69 4a 44 20 3d 20 28 73  .    p->iJD = (s
7f420 71 6c 69 74 65 33 5f 69 6e 74 36 34 29 28 72 2a  qlite3_int64)(r*
7f430 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30 2e 35  86400000.0 + 0.5
7f440 29 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a  );.    p->validJ
7f450 44 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72  D = 1;.    retur
7f460 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 0;.  }.  retur
7f470 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n 1;.}../*.** Co
7f480 6d 70 75 74 65 20 74 68 65 20 59 65 61 72 2c 20  mpute the Year, 
7f490 4d 6f 6e 74 68 2c 20 61 6e 64 20 44 61 79 20 66  Month, and Day f
7f4a0 72 6f 6d 20 74 68 65 20 6a 75 6c 69 61 6e 20 64  rom the julian d
7f4b0 61 79 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ay number..*/.st
7f4c0 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
7f4d0 65 59 4d 44 28 44 61 74 65 54 69 6d 65 20 2a 70  eYMD(DateTime *p
7f4e0 29 7b 0a 20 20 69 6e 74 20 5a 2c 20 41 2c 20 42  ){.  int Z, A, B
7f4f0 2c 20 43 2c 20 44 2c 20 45 2c 20 58 31 3b 0a 20  , C, D, E, X1;. 
7f500 20 69 66 28 20 70 2d 3e 76 61 6c 69 64 59 4d 44   if( p->validYMD
7f510 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
7f520 20 21 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b 0a   !p->validJD ){.
7f530 20 20 20 20 70 2d 3e 59 20 3d 20 32 30 30 30 3b      p->Y = 2000;
7f540 0a 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20  .    p->M = 1;. 
7f550 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20 7d     p->D = 1;.  }
7f560 65 6c 73 65 7b 0a 20 20 20 20 5a 20 3d 20 28 70  else{.    Z = (p
7f570 2d 3e 69 4a 44 20 2b 20 34 33 32 30 30 30 30 30  ->iJD + 43200000
7f580 29 2f 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20  )/86400000;.    
7f590 41 20 3d 20 28 5a 20 2d 20 31 38 36 37 32 31 36  A = (Z - 1867216
7f5a0 2e 32 35 29 2f 33 36 35 32 34 2e 32 35 3b 0a 20  .25)/36524.25;. 
7f5b0 20 20 20 41 20 3d 20 5a 20 2b 20 31 20 2b 20 41     A = Z + 1 + A
7f5c0 20 2d 20 28 41 2f 34 29 3b 0a 20 20 20 20 42 20   - (A/4);.    B 
7f5d0 3d 20 41 20 2b 20 31 35 32 34 3b 0a 20 20 20 20  = A + 1524;.    
7f5e0 43 20 3d 20 28 42 20 2d 20 31 32 32 2e 31 29 2f  C = (B - 122.1)/
7f5f0 33 36 35 2e 32 35 3b 0a 20 20 20 20 44 20 3d 20  365.25;.    D = 
7f600 33 36 35 2e 32 35 2a 43 3b 0a 20 20 20 20 45 20  365.25*C;.    E 
7f610 3d 20 28 42 2d 44 29 2f 33 30 2e 36 30 30 31 3b  = (B-D)/30.6001;
7f620 0a 20 20 20 20 58 31 20 3d 20 33 30 2e 36 30 30  .    X1 = 30.600
7f630 31 2a 45 3b 0a 20 20 20 20 70 2d 3e 44 20 3d 20  1*E;.    p->D = 
7f640 42 20 2d 20 44 20 2d 20 58 31 3b 0a 20 20 20 20  B - D - X1;.    
7f650 70 2d 3e 4d 20 3d 20 45 3c 31 34 20 3f 20 45 2d  p->M = E<14 ? E-
7f660 31 20 3a 20 45 2d 31 33 3b 0a 20 20 20 20 70 2d  1 : E-13;.    p-
7f670 3e 59 20 3d 20 70 2d 3e 4d 3e 32 20 3f 20 43 20  >Y = p->M>2 ? C 
7f680 2d 20 34 37 31 36 20 3a 20 43 20 2d 20 34 37 31  - 4716 : C - 471
7f690 35 3b 0a 20 20 7d 0a 20 20 70 2d 3e 76 61 6c 69  5;.  }.  p->vali
7f6a0 64 59 4d 44 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  dYMD = 1;.}../*.
7f6b0 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 48  ** Compute the H
7f6c0 6f 75 72 2c 20 4d 69 6e 75 74 65 2c 20 61 6e 64  our, Minute, and
7f6d0 20 53 65 63 6f 6e 64 73 20 66 72 6f 6d 20 74 68   Seconds from th
7f6e0 65 20 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d  e julian day num
7f6f0 62 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ber..*/.static v
7f700 6f 69 64 20 63 6f 6d 70 75 74 65 48 4d 53 28 44  oid computeHMS(D
7f710 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
7f720 6e 74 20 73 3b 0a 20 20 69 66 28 20 70 2d 3e 76  nt s;.  if( p->v
7f730 61 6c 69 64 48 4d 53 20 29 20 72 65 74 75 72 6e  alidHMS ) return
7f740 3b 0a 20 20 63 6f 6d 70 75 74 65 4a 44 28 70 29  ;.  computeJD(p)
7f750 3b 0a 20 20 73 20 3d 20 28 70 2d 3e 69 4a 44 20  ;.  s = (p->iJD 
7f760 2b 20 34 33 32 30 30 30 30 30 29 20 25 20 38 36  + 43200000) % 86
7f770 34 30 30 30 30 30 3b 0a 20 20 70 2d 3e 73 20 3d  400000;.  p->s =
7f780 20 73 2f 31 30 30 30 2e 30 3b 0a 20 20 73 20 3d   s/1000.0;.  s =
7f790 20 70 2d 3e 73 3b 0a 20 20 70 2d 3e 73 20 2d 3d   p->s;.  p->s -=
7f7a0 20 73 3b 0a 20 20 70 2d 3e 68 20 3d 20 73 2f 33   s;.  p->h = s/3
7f7b0 36 30 30 3b 0a 20 20 73 20 2d 3d 20 70 2d 3e 68  600;.  s -= p->h
7f7c0 2a 33 36 30 30 3b 0a 20 20 70 2d 3e 6d 20 3d 20  *3600;.  p->m = 
7f7d0 73 2f 36 30 3b 0a 20 20 70 2d 3e 73 20 2b 3d 20  s/60;.  p->s += 
7f7e0 73 20 2d 20 70 2d 3e 6d 2a 36 30 3b 0a 20 20 70  s - p->m*60;.  p
7f7f0 2d 3e 76 61 6c 69 64 48 4d 53 20 3d 20 31 3b 0a  ->validHMS = 1;.
7f800 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65  }../*.** Compute
7f810 20 62 6f 74 68 20 59 4d 44 20 61 6e 64 20 48 4d   both YMD and HM
7f820 53 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  S.*/.static void
7f830 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28   computeYMD_HMS(
7f840 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20  DateTime *p){.  
7f850 63 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20  computeYMD(p);. 
7f860 20 63 6f 6d 70 75 74 65 48 4d 53 28 70 29 3b 0a   computeHMS(p);.
7f870 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74  }../*.** Clear t
7f880 68 65 20 59 4d 44 20 61 6e 64 20 48 4d 53 20 61  he YMD and HMS a
7f890 6e 64 20 74 68 65 20 54 5a 0a 2a 2f 0a 73 74 61  nd the TZ.*/.sta
7f8a0 74 69 63 20 76 6f 69 64 20 63 6c 65 61 72 59 4d  tic void clearYM
7f8b0 44 5f 48 4d 53 5f 54 5a 28 44 61 74 65 54 69 6d  D_HMS_TZ(DateTim
7f8c0 65 20 2a 70 29 7b 0a 20 20 70 2d 3e 76 61 6c 69  e *p){.  p->vali
7f8d0 64 59 4d 44 20 3d 20 30 3b 0a 20 20 70 2d 3e 76  dYMD = 0;.  p->v
7f8e0 61 6c 69 64 48 4d 53 20 3d 20 30 3b 0a 20 20 70  alidHMS = 0;.  p
7f8f0 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 7d  ->validTZ = 0;.}
7f900 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
7f910 5f 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a  _OMIT_LOCALTIME.
7f920 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
7f930 65 20 64 69 66 66 65 72 65 6e 63 65 20 28 69 6e  e difference (in
7f940 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 29 0a 2a   milliseconds).*
7f950 2a 20 62 65 74 77 65 65 6e 20 6c 6f 63 61 6c 74  * between localt
7f960 69 6d 65 20 61 6e 64 20 55 54 43 20 28 61 2e 6b  ime and UTC (a.k
7f970 2e 61 2e 20 47 4d 54 29 0a 2a 2a 20 66 6f 72 20  .a. GMT).** for 
7f980 74 68 65 20 74 69 6d 65 20 76 61 6c 75 65 20 70  the time value p
7f990 20 77 68 65 72 65 20 70 20 69 73 20 69 6e 20 55   where p is in U
7f9a0 54 43 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  TC..*/.static in
7f9b0 74 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65  t localtimeOffse
7f9c0 74 28 44 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a  t(DateTime *p){.
7f9d0 20 20 44 61 74 65 54 69 6d 65 20 78 2c 20 79 3b    DateTime x, y;
7f9e0 0a 20 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 78  .  time_t t;.  x
7f9f0 20 3d 20 2a 70 3b 0a 20 20 63 6f 6d 70 75 74 65   = *p;.  compute
7fa00 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 69  YMD_HMS(&x);.  i
7fa10 66 28 20 78 2e 59 3c 31 39 37 31 20 7c 7c 20 78  f( x.Y<1971 || x
7fa20 2e 59 3e 3d 32 30 33 38 20 29 7b 0a 20 20 20 20  .Y>=2038 ){.    
7fa30 78 2e 59 20 3d 20 32 30 30 30 3b 0a 20 20 20 20  x.Y = 2000;.    
7fa40 78 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 78 2e 44  x.M = 1;.    x.D
7fa50 20 3d 20 31 3b 0a 20 20 20 20 78 2e 68 20 3d 20   = 1;.    x.h = 
7fa60 30 3b 0a 20 20 20 20 78 2e 6d 20 3d 20 30 3b 0a  0;.    x.m = 0;.
7fa70 20 20 20 20 78 2e 73 20 3d 20 30 2e 30 3b 0a 20      x.s = 0.0;. 
7fa80 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 69 6e   } else {.    in
7fa90 74 20 73 20 3d 20 78 2e 73 20 2b 20 30 2e 35 3b  t s = x.s + 0.5;
7faa0 0a 20 20 20 20 78 2e 73 20 3d 20 73 3b 0a 20 20  .    x.s = s;.  
7fab0 7d 0a 20 20 78 2e 74 7a 20 3d 20 30 3b 0a 20 20  }.  x.tz = 0;.  
7fac0 78 2e 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20  x.validJD = 0;. 
7fad0 20 63 6f 6d 70 75 74 65 4a 44 28 26 78 29 3b 0a   computeJD(&x);.
7fae0 20 20 74 20 3d 20 78 2e 69 4a 44 2f 31 30 30 30    t = x.iJD/1000
7faf0 20 2d 20 32 34 34 30 35 38 37 2e 35 2a 38 36 34   - 2440587.5*864
7fb00 30 30 2e 30 3b 0a 23 69 66 64 65 66 20 48 41 56  00.0;.#ifdef HAV
7fb10 45 5f 4c 4f 43 41 4c 54 49 4d 45 5f 52 0a 20 20  E_LOCALTIME_R.  
7fb20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20  {.    struct tm 
7fb30 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61  sLocal;.    loca
7fb40 6c 74 69 6d 65 5f 72 28 26 74 2c 20 26 73 4c 6f  ltime_r(&t, &sLo
7fb50 63 61 6c 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20  cal);.    y.Y = 
7fb60 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b  sLocal.tm_year +
7fb70 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d   1900;.    y.M =
7fb80 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b   sLocal.tm_mon +
7fb90 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c   1;.    y.D = sL
7fba0 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20  ocal.tm_mday;.  
7fbb0 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.h = sLocal.t
7fbc0 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20  m_hour;.    y.m 
7fbd0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b  = sLocal.tm_min;
7fbe0 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61  .    y.s = sLoca
7fbf0 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65  l.tm_sec;.  }.#e
7fc00 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56 45  lif defined(HAVE
7fc10 5f 4c 4f 43 41 4c 54 49 4d 45 5f 53 29 0a 20 20  _LOCALTIME_S).  
7fc20 7b 0a 20 20 20 20 73 74 72 75 63 74 20 74 6d 20  {.    struct tm 
7fc30 73 4c 6f 63 61 6c 3b 0a 20 20 20 20 6c 6f 63 61  sLocal;.    loca
7fc40 6c 74 69 6d 65 5f 73 28 26 73 4c 6f 63 61 6c 2c  ltime_s(&sLocal,
7fc50 20 26 74 29 3b 0a 20 20 20 20 79 2e 59 20 3d 20   &t);.    y.Y = 
7fc60 73 4c 6f 63 61 6c 2e 74 6d 5f 79 65 61 72 20 2b  sLocal.tm_year +
7fc70 20 31 39 30 30 3b 0a 20 20 20 20 79 2e 4d 20 3d   1900;.    y.M =
7fc80 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 6f 6e 20 2b   sLocal.tm_mon +
7fc90 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d 20 73 4c   1;.    y.D = sL
7fca0 6f 63 61 6c 2e 74 6d 5f 6d 64 61 79 3b 0a 20 20  ocal.tm_mday;.  
7fcb0 20 20 79 2e 68 20 3d 20 73 4c 6f 63 61 6c 2e 74    y.h = sLocal.t
7fcc0 6d 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20  m_hour;.    y.m 
7fcd0 3d 20 73 4c 6f 63 61 6c 2e 74 6d 5f 6d 69 6e 3b  = sLocal.tm_min;
7fce0 0a 20 20 20 20 79 2e 73 20 3d 20 73 4c 6f 63 61  .    y.s = sLoca
7fcf0 6c 2e 74 6d 5f 73 65 63 3b 0a 20 20 7d 0a 23 65  l.tm_sec;.  }.#e
7fd00 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 74 72 75  lse.  {.    stru
7fd10 63 74 20 74 6d 20 2a 70 54 6d 3b 0a 20 20 20 20  ct tm *pTm;.    
7fd20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
7fd30 74 65 72 28 73 71 6c 69 74 65 33 4d 75 74 65 78  ter(sqlite3Mutex
7fd40 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54  Alloc(SQLITE_MUT
7fd50 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
7fd60 29 29 3b 0a 20 20 20 20 70 54 6d 20 3d 20 6c 6f  ));.    pTm = lo
7fd70 63 61 6c 74 69 6d 65 28 26 74 29 3b 0a 20 20 20  caltime(&t);.   
7fd80 20 79 2e 59 20 3d 20 70 54 6d 2d 3e 74 6d 5f 79   y.Y = pTm->tm_y
7fd90 65 61 72 20 2b 20 31 39 30 30 3b 0a 20 20 20 20  ear + 1900;.    
7fda0 79 2e 4d 20 3d 20 70 54 6d 2d 3e 74 6d 5f 6d 6f  y.M = pTm->tm_mo
7fdb0 6e 20 2b 20 31 3b 0a 20 20 20 20 79 2e 44 20 3d  n + 1;.    y.D =
7fdc0 20 70 54 6d 2d 3e 74 6d 5f 6d 64 61 79 3b 0a 20   pTm->tm_mday;. 
7fdd0 20 20 20 79 2e 68 20 3d 20 70 54 6d 2d 3e 74 6d     y.h = pTm->tm
7fde0 5f 68 6f 75 72 3b 0a 20 20 20 20 79 2e 6d 20 3d  _hour;.    y.m =
7fdf0 20 70 54 6d 2d 3e 74 6d 5f 6d 69 6e 3b 0a 20 20   pTm->tm_min;.  
7fe00 20 20 79 2e 73 20 3d 20 70 54 6d 2d 3e 74 6d 5f    y.s = pTm->tm_
7fe10 73 65 63 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sec;.    sqlite3
7fe20 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
7fe30 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
7fe40 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
7fe50 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 7d  IC_MASTER));.  }
7fe60 0a 23 65 6e 64 69 66 0a 20 20 79 2e 76 61 6c 69  .#endif.  y.vali
7fe70 64 59 4d 44 20 3d 20 31 3b 0a 20 20 79 2e 76 61  dYMD = 1;.  y.va
7fe80 6c 69 64 48 4d 53 20 3d 20 31 3b 0a 20 20 79 2e  lidHMS = 1;.  y.
7fe90 76 61 6c 69 64 4a 44 20 3d 20 30 3b 0a 20 20 79  validJD = 0;.  y
7fea0 2e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20 20  .validTZ = 0;.  
7feb0 63 6f 6d 70 75 74 65 4a 44 28 26 79 29 3b 0a 20  computeJD(&y);. 
7fec0 20 72 65 74 75 72 6e 20 79 2e 69 4a 44 20 2d 20   return y.iJD - 
7fed0 78 2e 69 4a 44 3b 0a 7d 0a 23 65 6e 64 69 66 20  x.iJD;.}.#endif 
7fee0 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  /* SQLITE_OMIT_L
7fef0 4f 43 41 4c 54 49 4d 45 20 2a 2f 0a 0a 2f 2a 0a  OCALTIME */../*.
7ff00 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 6d 6f 64  ** Process a mod
7ff10 69 66 69 65 72 20 74 6f 20 61 20 64 61 74 65 2d  ifier to a date-
7ff20 74 69 6d 65 20 73 74 61 6d 70 2e 20 20 54 68 65  time stamp.  The
7ff30 20 6d 6f 64 69 66 69 65 72 73 20 61 72 65 0a 2a   modifiers are.*
7ff40 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  * as follows:.**
7ff50 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 64 61 79 73  .**     NNN days
7ff60 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 68 6f 75 72  .**     NNN hour
7ff70 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 20 6d 69 6e  s.**     NNN min
7ff80 75 74 65 73 0a 2a 2a 20 20 20 20 20 4e 4e 4e 2e  utes.**     NNN.
7ff90 4e 4e 4e 4e 20 73 65 63 6f 6e 64 73 0a 2a 2a 20  NNNN seconds.** 
7ffa0 20 20 20 20 4e 4e 4e 20 6d 6f 6e 74 68 73 0a 2a      NNN months.*
7ffb0 2a 20 20 20 20 20 4e 4e 4e 20 79 65 61 72 73 0a  *     NNN years.
7ffc0 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
7ffd0 6d 6f 6e 74 68 0a 2a 2a 20 20 20 20 20 73 74 61  month.**     sta
7ffe0 72 74 20 6f 66 20 79 65 61 72 0a 2a 2a 20 20 20  rt of year.**   
7fff0 20 20 73 74 61 72 74 20 6f 66 20 77 65 65 6b 0a    start of week.
80000 2a 2a 20 20 20 20 20 73 74 61 72 74 20 6f 66 20  **     start of 
80010 64 61 79 0a 2a 2a 20 20 20 20 20 77 65 65 6b 64  day.**     weekd
80020 61 79 20 4e 0a 2a 2a 20 20 20 20 20 75 6e 69 78  ay N.**     unix
80030 65 70 6f 63 68 0a 2a 2a 20 20 20 20 20 6c 6f 63  epoch.**     loc
80040 61 6c 74 69 6d 65 0a 2a 2a 20 20 20 20 20 75 74  altime.**     ut
80050 63 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 30  c.**.** Return 0
80060 20 6f 6e 20 73 75 63 63 65 73 73 20 61 6e 64 20   on success and 
80070 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  1 if there is an
80080 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72 2e  y kind of error.
80090 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
800a0 61 72 73 65 4d 6f 64 69 66 69 65 72 28 63 6f 6e  arseModifier(con
800b0 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 2c 20 44  st char *zMod, D
800c0 61 74 65 54 69 6d 65 20 2a 70 29 7b 0a 20 20 69  ateTime *p){.  i
800d0 6e 74 20 72 63 20 3d 20 31 3b 0a 20 20 69 6e 74  nt rc = 1;.  int
800e0 20 6e 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a   n;.  double r;.
800f0 20 20 63 68 61 72 20 2a 7a 2c 20 7a 42 75 66 5b    char *z, zBuf[
80100 33 30 5d 3b 0a 20 20 7a 20 3d 20 7a 42 75 66 3b  30];.  z = zBuf;
80110 0a 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 41 72  .  for(n=0; n<Ar
80120 72 61 79 53 69 7a 65 28 7a 42 75 66 29 2d 31 20  raySize(zBuf)-1 
80130 26 26 20 7a 4d 6f 64 5b 6e 5d 3b 20 6e 2b 2b 29  && zMod[n]; n++)
80140 7b 0a 20 20 20 20 7a 5b 6e 5d 20 3d 20 74 6f 6c  {.    z[n] = tol
80150 6f 77 65 72 28 7a 4d 6f 64 5b 6e 5d 29 3b 0a 20  ower(zMod[n]);. 
80160 20 7d 0a 20 20 7a 5b 6e 5d 20 3d 20 30 3b 0a 20   }.  z[n] = 0;. 
80170 20 73 77 69 74 63 68 28 20 7a 5b 30 5d 20 29 7b   switch( z[0] ){
80180 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
80190 4f 4d 49 54 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20  OMIT_LOCALTIME. 
801a0 20 20 20 63 61 73 65 20 27 6c 27 3a 20 7b 0a 20     case 'l': {. 
801b0 20 20 20 20 20 2f 2a 20 20 20 20 6c 6f 63 61 6c       /*    local
801c0 74 69 6d 65 0a 20 20 20 20 20 20 2a 2a 0a 20 20  time.      **.  
801d0 20 20 20 20 2a 2a 20 41 73 73 75 6d 69 6e 67 20      ** Assuming 
801e0 74 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65  the current time
801f0 20 76 61 6c 75 65 20 69 73 20 55 54 43 20 28 61   value is UTC (a
80200 2e 6b 2e 61 2e 20 47 4d 54 29 2c 20 73 68 69 66  .k.a. GMT), shif
80210 74 20 69 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  t it to.      **
80220 20 73 68 6f 77 20 6c 6f 63 61 6c 20 74 69 6d 65   show local time
80230 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
80240 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20 22   if( strcmp(z, "
80250 6c 6f 63 61 6c 74 69 6d 65 22 29 3d 3d 30 20 29  localtime")==0 )
80260 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74  {.        comput
80270 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20  eJD(p);.        
80280 70 2d 3e 69 4a 44 20 2b 3d 20 6c 6f 63 61 6c 74  p->iJD += localt
80290 69 6d 65 4f 66 66 73 65 74 28 70 29 3b 0a 20 20  imeOffset(p);.  
802a0 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48        clearYMD_H
802b0 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20  MS_TZ(p);.      
802c0 20 20 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20    rc = 0;.      
802d0 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
802e0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
802f0 63 61 73 65 20 27 75 27 3a 20 7b 0a 20 20 20 20  case 'u': {.    
80300 20 20 2f 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    /*.      **   
80310 20 75 6e 69 78 65 70 6f 63 68 0a 20 20 20 20 20   unixepoch.     
80320 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 72 65   **.      ** Tre
80330 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20 76  at the current v
80340 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4a 44 20 61  alue of p->iJD a
80350 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a  s the number of.
80360 20 20 20 20 20 20 2a 2a 20 73 65 63 6f 6e 64 73        ** seconds
80370 20 73 69 6e 63 65 20 31 39 37 30 2e 20 20 43 6f   since 1970.  Co
80380 6e 76 65 72 74 20 74 6f 20 61 20 72 65 61 6c 20  nvert to a real 
80390 6a 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65  julian day numbe
803a0 72 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  r..      */.    
803b0 20 20 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 20    if( strcmp(z, 
803c0 22 75 6e 69 78 65 70 6f 63 68 22 29 3d 3d 30 20  "unixepoch")==0 
803d0 26 26 20 70 2d 3e 76 61 6c 69 64 4a 44 20 29 7b  && p->validJD ){
803e0 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 4a 44 20  .        p->iJD 
803f0 3d 20 70 2d 3e 69 4a 44 2f 38 36 34 30 30 2e 30  = p->iJD/86400.0
80400 20 2b 20 32 34 34 30 35 38 37 2e 35 2a 38 36 34   + 2440587.5*864
80410 30 30 30 30 30 2e 30 3b 0a 20 20 20 20 20 20 20  00000.0;.       
80420 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f 54 5a   clearYMD_HMS_TZ
80430 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
80440 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  = 0;.      }.#if
80450 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
80460 5f 4c 4f 43 41 4c 54 49 4d 45 0a 20 20 20 20 20  _LOCALTIME.     
80470 20 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70   else if( strcmp
80480 28 7a 2c 20 22 75 74 63 22 29 3d 3d 30 20 29 7b  (z, "utc")==0 ){
80490 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 31 3b  .        int c1;
804a0 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65  .        compute
804b0 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 20 20 63  JD(p);.        c
804c0 31 20 3d 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66  1 = localtimeOff
804d0 73 65 74 28 70 29 3b 0a 20 20 20 20 20 20 20 20  set(p);.        
804e0 70 2d 3e 69 4a 44 20 2d 3d 20 63 31 3b 0a 20 20  p->iJD -= c1;.  
804f0 20 20 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48        clearYMD_H
80500 4d 53 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20  MS_TZ(p);.      
80510 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 63 31 20 2d    p->iJD += c1 -
80520 20 6c 6f 63 61 6c 74 69 6d 65 4f 66 66 73 65 74   localtimeOffset
80530 28 70 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  (p);.        rc 
80540 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e  = 0;.      }.#en
80550 64 69 66 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  dif.      break;
80560 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
80570 27 77 27 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 0a  'w': {.      /*.
80580 20 20 20 20 20 20 2a 2a 20 20 20 20 77 65 65 6b        **    week
80590 64 61 79 20 4e 0a 20 20 20 20 20 20 2a 2a 0a 20  day N.      **. 
805a0 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68 65       ** Move the
805b0 20 64 61 74 65 20 74 6f 20 74 68 65 20 73 61 6d   date to the sam
805c0 65 20 74 69 6d 65 20 6f 6e 20 74 68 65 20 6e 65  e time on the ne
805d0 78 74 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66  xt occurrence of
805e0 0a 20 20 20 20 20 20 2a 2a 20 77 65 65 6b 64 61  .      ** weekda
805f0 79 20 4e 20 77 68 65 72 65 20 30 3d 3d 53 75 6e  y N where 0==Sun
80600 64 61 79 2c 20 31 3d 3d 4d 6f 6e 64 61 79 2c 20  day, 1==Monday, 
80610 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 20 20 49  and so forth.  I
80620 66 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 64  f the.      ** d
80630 61 74 65 20 69 73 20 61 6c 72 65 61 64 79 20 6f  ate is already o
80640 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  n the appropriat
80650 65 20 77 65 65 6b 64 61 79 2c 20 74 68 69 73 20  e weekday, this 
80660 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 20 20  is a no-op..    
80670 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73    */.      if( s
80680 74 72 6e 63 6d 70 28 7a 2c 20 22 77 65 65 6b 64  trncmp(z, "weekd
80690 61 79 20 22 2c 20 38 29 3d 3d 30 20 26 26 20 67  ay ", 8)==0 && g
806a0 65 74 56 61 6c 75 65 28 26 7a 5b 38 5d 2c 26 72  etValue(&z[8],&r
806b0 29 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20 20  )>0.            
806c0 20 20 20 20 20 26 26 20 28 6e 3d 72 29 3d 3d 72       && (n=r)==r
806d0 20 26 26 20 6e 3e 3d 30 20 26 26 20 72 3c 37 20   && n>=0 && r<7 
806e0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
806f0 65 33 5f 69 6e 74 36 34 20 5a 3b 0a 20 20 20 20  e3_int64 Z;.    
80700 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f 48      computeYMD_H
80710 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70  MS(p);.        p
80720 2d 3e 76 61 6c 69 64 54 5a 20 3d 20 30 3b 0a 20  ->validTZ = 0;. 
80730 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64 4a         p->validJ
80740 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 63  D = 0;.        c
80750 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20  omputeJD(p);.   
80760 20 20 20 20 20 5a 20 3d 20 28 28 70 2d 3e 69 4a       Z = ((p->iJ
80770 44 20 2b 20 31 32 39 36 30 30 30 30 30 29 2f 38  D + 129600000)/8
80780 36 34 30 30 30 30 30 29 20 25 20 37 3b 0a 20 20  6400000) % 7;.  
80790 20 20 20 20 20 20 69 66 28 20 5a 3e 6e 20 29 20        if( Z>n ) 
807a0 5a 20 2d 3d 20 37 3b 0a 20 20 20 20 20 20 20 20  Z -= 7;.        
807b0 70 2d 3e 69 4a 44 20 2b 3d 20 28 6e 20 2d 20 5a  p->iJD += (n - Z
807c0 29 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20  )*86400000;.    
807d0 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53      clearYMD_HMS
807e0 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20  _TZ(p);.        
807f0 72 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  rc = 0;.      }.
80800 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
80810 20 7d 0a 20 20 20 20 63 61 73 65 20 27 73 27 3a   }.    case 's':
80820 20 7b 0a 20 20 20 20 20 20 2f 2a 0a 20 20 20 20   {.      /*.    
80830 20 20 2a 2a 20 20 20 20 73 74 61 72 74 20 6f 66    **    start of
80840 20 54 54 54 54 54 0a 20 20 20 20 20 20 2a 2a 0a   TTTTT.      **.
80850 20 20 20 20 20 20 2a 2a 20 4d 6f 76 65 20 74 68        ** Move th
80860 65 20 64 61 74 65 20 62 61 63 6b 77 61 72 64 73  e date backwards
80870 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   to the beginnin
80880 67 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  g of the current
80890 20 64 61 79 2c 0a 20 20 20 20 20 20 2a 2a 20 6f   day,.      ** o
808a0 72 20 6d 6f 6e 74 68 20 6f 72 20 79 65 61 72 2e  r month or year.
808b0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
808c0 69 66 28 20 73 74 72 6e 63 6d 70 28 7a 2c 20 22  if( strncmp(z, "
808d0 73 74 61 72 74 20 6f 66 20 22 2c 20 39 29 21 3d  start of ", 9)!=
808e0 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  0 ) break;.     
808f0 20 7a 20 2b 3d 20 39 3b 0a 20 20 20 20 20 20 63   z += 9;.      c
80900 6f 6d 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20  omputeYMD(p);.  
80910 20 20 20 20 70 2d 3e 76 61 6c 69 64 48 4d 53 20      p->validHMS 
80920 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 68 20  = 1;.      p->h 
80930 3d 20 70 2d 3e 6d 20 3d 20 30 3b 0a 20 20 20 20  = p->m = 0;.    
80940 20 20 70 2d 3e 73 20 3d 20 30 2e 30 3b 0a 20 20    p->s = 0.0;.  
80950 20 20 20 20 70 2d 3e 76 61 6c 69 64 54 5a 20 3d      p->validTZ =
80960 20 30 3b 0a 20 20 20 20 20 20 70 2d 3e 76 61 6c   0;.      p->val
80970 69 64 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20  idJD = 0;.      
80980 69 66 28 20 73 74 72 63 6d 70 28 7a 2c 22 6d 6f  if( strcmp(z,"mo
80990 6e 74 68 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  nth")==0 ){.    
809a0 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a 20 20      p->D = 1;.  
809b0 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20        rc = 0;.  
809c0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74      }else if( st
809d0 72 63 6d 70 28 7a 2c 22 79 65 61 72 22 29 3d 3d  rcmp(z,"year")==
809e0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6d  0 ){.        com
809f0 70 75 74 65 59 4d 44 28 70 29 3b 0a 20 20 20 20  puteYMD(p);.    
80a00 20 20 20 20 70 2d 3e 4d 20 3d 20 31 3b 0a 20 20      p->M = 1;.  
80a10 20 20 20 20 20 20 70 2d 3e 44 20 3d 20 31 3b 0a        p->D = 1;.
80a20 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b 0a          rc = 0;.
80a30 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
80a40 73 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d  strcmp(z,"day")=
80a50 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  =0 ){.        rc
80a60 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 0;.      }.  
80a70 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
80a80 0a 20 20 20 20 63 61 73 65 20 27 2b 27 3a 0a 20  .    case '+':. 
80a90 20 20 20 63 61 73 65 20 27 2d 27 3a 0a 20 20 20     case '-':.   
80aa0 20 63 61 73 65 20 27 30 27 3a 0a 20 20 20 20 63   case '0':.    c
80ab0 61 73 65 20 27 31 27 3a 0a 20 20 20 20 63 61 73  ase '1':.    cas
80ac0 65 20 27 32 27 3a 0a 20 20 20 20 63 61 73 65 20  e '2':.    case 
80ad0 27 33 27 3a 0a 20 20 20 20 63 61 73 65 20 27 34  '3':.    case '4
80ae0 27 3a 0a 20 20 20 20 63 61 73 65 20 27 35 27 3a  ':.    case '5':
80af0 0a 20 20 20 20 63 61 73 65 20 27 36 27 3a 0a 20  .    case '6':. 
80b00 20 20 20 63 61 73 65 20 27 37 27 3a 0a 20 20 20     case '7':.   
80b10 20 63 61 73 65 20 27 38 27 3a 0a 20 20 20 20 63   case '8':.    c
80b20 61 73 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 20  ase '9': {.     
80b30 20 6e 20 3d 20 67 65 74 56 61 6c 75 65 28 7a 2c   n = getValue(z,
80b40 20 26 72 29 3b 0a 20 20 20 20 20 20 61 73 73 65   &r);.      asse
80b50 72 74 28 20 6e 3e 3d 31 20 29 3b 0a 20 20 20 20  rt( n>=1 );.    
80b60 20 20 69 66 28 20 7a 5b 6e 5d 3d 3d 27 3a 27 20    if( z[n]==':' 
80b70 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20  ){.        /* A 
80b80 6d 6f 64 69 66 69 65 72 20 6f 66 20 74 68 65 20  modifier of the 
80b90 66 6f 72 6d 20 28 2b 7c 2d 29 48 48 3a 4d 4d 3a  form (+|-)HH:MM:
80ba0 53 53 2e 46 46 46 20 61 64 64 73 20 28 6f 72 20  SS.FFF adds (or 
80bb0 73 75 62 74 72 61 63 74 73 29 20 74 68 65 0a 20  subtracts) the. 
80bc0 20 20 20 20 20 20 20 2a 2a 20 73 70 65 63 69 66         ** specif
80bd0 69 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 68 6f  ied number of ho
80be0 75 72 73 2c 20 6d 69 6e 75 74 65 73 2c 20 73 65  urs, minutes, se
80bf0 63 6f 6e 64 73 2c 20 61 6e 64 20 66 72 61 63 74  conds, and fract
80c00 69 6f 6e 61 6c 20 73 65 63 6f 6e 64 73 0a 20 20  ional seconds.  
80c10 20 20 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20        ** to the 
80c20 74 69 6d 65 2e 20 20 54 68 65 20 22 2e 46 46 46  time.  The ".FFF
80c30 22 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64  " may be omitted
80c40 2e 20 20 54 68 65 20 22 3a 53 53 2e 46 46 46 22  .  The ":SS.FFF"
80c50 20 6d 61 79 20 62 65 0a 20 20 20 20 20 20 20 20   may be.        
80c60 2a 2a 20 6f 6d 69 74 74 65 64 2e 0a 20 20 20 20  ** omitted..    
80c70 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 63      */.        c
80c80 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20  onst char *z2 = 
80c90 7a 3b 0a 20 20 20 20 20 20 20 20 44 61 74 65 54  z;.        DateT
80ca0 69 6d 65 20 74 78 3b 0a 20 20 20 20 20 20 20 20  ime tx;.        
80cb0 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 64 61  sqlite3_int64 da
80cc0 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  y;.        if( !
80cd0 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 32  isdigit(*(u8*)z2
80ce0 29 20 29 20 7a 32 2b 2b 3b 0a 20 20 20 20 20 20  ) ) z2++;.      
80cf0 20 20 6d 65 6d 73 65 74 28 26 74 78 2c 20 30 2c    memset(&tx, 0,
80d00 20 73 69 7a 65 6f 66 28 74 78 29 29 3b 0a 20 20   sizeof(tx));.  
80d10 20 20 20 20 20 20 69 66 28 20 70 61 72 73 65 48        if( parseH
80d20 68 4d 6d 53 73 28 7a 32 2c 20 26 74 78 29 20 29  hMmSs(z2, &tx) )
80d30 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
80d40 63 6f 6d 70 75 74 65 4a 44 28 26 74 78 29 3b 0a  computeJD(&tx);.
80d50 20 20 20 20 20 20 20 20 74 78 2e 69 4a 44 20 2d          tx.iJD -
80d60 3d 20 34 33 32 30 30 30 30 30 3b 0a 20 20 20 20  = 43200000;.    
80d70 20 20 20 20 64 61 79 20 3d 20 74 78 2e 69 4a 44      day = tx.iJD
80d80 2f 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20  /86400000;.     
80d90 20 20 20 74 78 2e 69 4a 44 20 2d 3d 20 64 61 79     tx.iJD -= day
80da0 2a 38 36 34 30 30 30 30 30 3b 0a 20 20 20 20 20  *86400000;.     
80db0 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 2d 27     if( z[0]=='-'
80dc0 20 29 20 74 78 2e 69 4a 44 20 3d 20 2d 74 78 2e   ) tx.iJD = -tx.
80dd0 69 4a 44 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d  iJD;.        com
80de0 70 75 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20  puteJD(p);.     
80df0 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53 5f     clearYMD_HMS_
80e00 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 20 20 70  TZ(p);.        p
80e10 2d 3e 69 4a 44 20 2b 3d 20 74 78 2e 69 4a 44 3b  ->iJD += tx.iJD;
80e20 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 30 3b  .        rc = 0;
80e30 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
80e40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a 20        }.      z 
80e50 2b 3d 20 6e 3b 0a 20 20 20 20 20 20 77 68 69 6c  += n;.      whil
80e60 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38 2a  e( isspace(*(u8*
80e70 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20 20  )z) ) z++;.     
80e80 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a   n = strlen(z);.
80e90 20 20 20 20 20 20 69 66 28 20 6e 3e 31 30 20 7c        if( n>10 |
80ea0 7c 20 6e 3c 33 20 29 20 62 72 65 61 6b 3b 0a 20  | n<3 ) break;. 
80eb0 20 20 20 20 20 69 66 28 20 7a 5b 6e 2d 31 5d 3d       if( z[n-1]=
80ec0 3d 27 73 27 20 29 7b 20 7a 5b 6e 2d 31 5d 20 3d  ='s' ){ z[n-1] =
80ed0 20 30 3b 20 6e 2d 2d 3b 20 7d 0a 20 20 20 20 20   0; n--; }.     
80ee0 20 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20   computeJD(p);. 
80ef0 20 20 20 20 20 72 63 20 3d 20 30 3b 0a 20 20 20       rc = 0;.   
80f00 20 20 20 69 66 28 20 6e 3d 3d 33 20 26 26 20 73     if( n==3 && s
80f10 74 72 63 6d 70 28 7a 2c 22 64 61 79 22 29 3d 3d  trcmp(z,"day")==
80f20 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e  0 ){.        p->
80f30 69 4a 44 20 2b 3d 20 72 2a 38 36 34 30 30 30 30  iJD += r*8640000
80f40 30 2e 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20  0.0 + 0.5;.     
80f50 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 34 20   }else if( n==4 
80f60 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 68 6f 75  && strcmp(z,"hou
80f70 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  r")==0 ){.      
80f80 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 72 2a 28 38    p->iJD += r*(8
80f90 36 34 30 30 30 30 30 2e 30 2f 32 34 2e 30 29 20  6400000.0/24.0) 
80fa0 2b 20 30 2e 35 3b 0a 20 20 20 20 20 20 7d 65 6c  + 0.5;.      }el
80fb0 73 65 20 69 66 28 20 6e 3d 3d 36 20 26 26 20 73  se if( n==6 && s
80fc0 74 72 63 6d 70 28 7a 2c 22 6d 69 6e 75 74 65 22  trcmp(z,"minute"
80fd0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
80fe0 70 2d 3e 69 4a 44 20 2b 3d 20 72 2a 28 38 36 34  p->iJD += r*(864
80ff0 30 30 30 30 30 2e 30 2f 28 32 34 2e 30 2a 36 30  00000.0/(24.0*60
81000 2e 30 29 29 20 2b 20 30 2e 35 3b 0a 20 20 20 20  .0)) + 0.5;.    
81010 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d 36    }else if( n==6
81020 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22 73 65   && strcmp(z,"se
81030 63 6f 6e 64 22 29 3d 3d 30 20 29 7b 0a 20 20 20  cond")==0 ){.   
81040 20 20 20 20 20 70 2d 3e 69 4a 44 20 2b 3d 20 72       p->iJD += r
81050 2a 28 38 36 34 30 30 30 30 30 2e 30 2f 28 32 34  *(86400000.0/(24
81060 2e 30 2a 36 30 2e 30 2a 36 30 2e 30 29 29 20 2b  .0*60.0*60.0)) +
81070 20 30 2e 35 3b 0a 20 20 20 20 20 20 7d 65 6c 73   0.5;.      }els
81080 65 20 69 66 28 20 6e 3d 3d 35 20 26 26 20 73 74  e if( n==5 && st
81090 72 63 6d 70 28 7a 2c 22 6d 6f 6e 74 68 22 29 3d  rcmp(z,"month")=
810a0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  =0 ){.        in
810b0 74 20 78 2c 20 79 3b 0a 20 20 20 20 20 20 20 20  t x, y;.        
810c0 63 6f 6d 70 75 74 65 59 4d 44 5f 48 4d 53 28 70  computeYMD_HMS(p
810d0 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 4d 20  );.        p->M 
810e0 2b 3d 20 72 3b 0a 20 20 20 20 20 20 20 20 78 20  += r;.        x 
810f0 3d 20 70 2d 3e 4d 3e 30 20 3f 20 28 70 2d 3e 4d  = p->M>0 ? (p->M
81100 2d 31 29 2f 31 32 20 3a 20 28 70 2d 3e 4d 2d 31  -1)/12 : (p->M-1
81110 32 29 2f 31 32 3b 0a 20 20 20 20 20 20 20 20 70  2)/12;.        p
81120 2d 3e 59 20 2b 3d 20 78 3b 0a 20 20 20 20 20 20  ->Y += x;.      
81130 20 20 70 2d 3e 4d 20 2d 3d 20 78 2a 31 32 3b 0a    p->M -= x*12;.
81140 20 20 20 20 20 20 20 20 70 2d 3e 76 61 6c 69 64          p->valid
81150 4a 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  JD = 0;.        
81160 63 6f 6d 70 75 74 65 4a 44 28 70 29 3b 0a 20 20  computeJD(p);.  
81170 20 20 20 20 20 20 79 20 3d 20 72 3b 0a 20 20 20        y = r;.   
81180 20 20 20 20 20 69 66 28 20 79 21 3d 72 20 29 7b       if( y!=r ){
81190 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 4a  .          p->iJ
811a0 44 20 2b 3d 20 28 72 20 2d 20 79 29 2a 33 30 2e  D += (r - y)*30.
811b0 30 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20 30  0*86400000.0 + 0
811c0 2e 35 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  .5;.        }.  
811d0 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d      }else if( n=
811e0 3d 34 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 22  =4 && strcmp(z,"
811f0 79 65 61 72 22 29 3d 3d 30 20 29 7b 0a 20 20 20  year")==0 ){.   
81200 20 20 20 20 20 63 6f 6d 70 75 74 65 59 4d 44 5f       computeYMD_
81210 48 4d 53 28 70 29 3b 0a 20 20 20 20 20 20 20 20  HMS(p);.        
81220 70 2d 3e 59 20 2b 3d 20 72 3b 0a 20 20 20 20 20  p->Y += r;.     
81230 20 20 20 70 2d 3e 76 61 6c 69 64 4a 44 20 3d 20     p->validJD = 
81240 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6d 70 75  0;.        compu
81250 74 65 4a 44 28 70 29 3b 0a 20 20 20 20 20 20 7d  teJD(p);.      }
81260 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 72 63  else{.        rc
81270 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
81280 20 20 20 20 63 6c 65 61 72 59 4d 44 5f 48 4d 53      clearYMD_HMS
81290 5f 54 5a 28 70 29 3b 0a 20 20 20 20 20 20 62 72  _TZ(p);.      br
812a0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  eak;.    }.    d
812b0 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
812c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d  break;.    }.  }
812d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
812e0 0a 2f 2a 0a 2a 2a 20 50 72 6f 63 65 73 73 20 74  ./*.** Process t
812f0 69 6d 65 20 66 75 6e 63 74 69 6f 6e 20 61 72 67  ime function arg
81300 75 6d 65 6e 74 73 2e 20 20 61 72 67 76 5b 30 5d  uments.  argv[0]
81310 20 69 73 20 61 20 64 61 74 65 2d 74 69 6d 65 20   is a date-time 
81320 73 74 61 6d 70 2e 0a 2a 2a 20 61 72 67 76 5b 31  stamp..** argv[1
81330 5d 20 61 6e 64 20 66 6f 6c 6c 6f 77 69 6e 67 20  ] and following 
81340 61 72 65 20 6d 6f 64 69 66 69 65 72 73 2e 20 20  are modifiers.  
81350 50 61 72 73 65 20 74 68 65 6d 20 61 6c 6c 20 61  Parse them all a
81360 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65 20  nd write.** the 
81370 72 65 73 75 6c 74 69 6e 67 20 74 69 6d 65 20 69  resulting time i
81380 6e 74 6f 20 74 68 65 20 44 61 74 65 54 69 6d 65  nto the DateTime
81390 20 73 74 72 75 63 74 75 72 65 20 70 2e 20 20 52   structure p.  R
813a0 65 74 75 72 6e 20 30 0a 2a 2a 20 6f 6e 20 73 75  eturn 0.** on su
813b0 63 63 65 73 73 20 61 6e 64 20 31 20 69 66 20 74  ccess and 1 if t
813c0 68 65 72 65 20 61 72 65 20 61 6e 79 20 65 72 72  here are any err
813d0 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ors..**.** If th
813e0 65 72 65 20 61 72 65 20 7a 65 72 6f 20 70 61 72  ere are zero par
813f0 61 6d 65 74 65 72 73 20 28 69 66 20 65 76 65 6e  ameters (if even
81400 20 61 72 67 76 5b 30 5d 20 69 73 20 75 6e 64 65   argv[0] is unde
81410 66 69 6e 65 64 29 0a 2a 2a 20 74 68 65 6e 20 61  fined).** then a
81420 73 73 75 6d 65 20 61 20 64 65 66 61 75 6c 74 20  ssume a default 
81430 76 61 6c 75 65 20 6f 66 20 22 6e 6f 77 22 20 66  value of "now" f
81440 6f 72 20 61 72 67 76 5b 30 5d 2e 0a 2a 2f 0a 73  or argv[0]..*/.s
81450 74 61 74 69 63 20 69 6e 74 20 69 73 44 61 74 65  tatic int isDate
81460 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
81470 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
81480 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71   int argc, .  sq
81490 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
814a0 67 76 2c 20 0a 20 20 44 61 74 65 54 69 6d 65 20  gv, .  DateTime 
814b0 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  *p.){.  int i;. 
814c0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
814d0 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20 65  char *z;.  int e
814e0 54 79 70 65 3b 0a 20 20 6d 65 6d 73 65 74 28 70  Type;.  memset(p
814f0 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29 29  , 0, sizeof(*p))
81500 3b 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 30 20  ;.  if( argc==0 
81510 29 7b 0a 20 20 20 20 73 65 74 44 61 74 65 54 69  ){.    setDateTi
81520 6d 65 54 6f 43 75 72 72 65 6e 74 28 63 6f 6e 74  meToCurrent(cont
81530 65 78 74 2c 20 70 29 3b 0a 20 20 7d 65 6c 73 65  ext, p);.  }else
81540 20 69 66 28 20 28 65 54 79 70 65 20 3d 20 73 71   if( (eType = sq
81550 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
81560 28 61 72 67 76 5b 30 5d 29 29 3d 3d 53 51 4c 49  (argv[0]))==SQLI
81570 54 45 5f 46 4c 4f 41 54 0a 20 20 20 20 20 20 20  TE_FLOAT.       
81580 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
81590 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 49 4e 54  Type==SQLITE_INT
815a0 45 47 45 52 20 29 7b 0a 20 20 20 20 70 2d 3e 69  EGER ){.    p->i
815b0 4a 44 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  JD = sqlite3_val
815c0 75 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30  ue_double(argv[0
815d0 5d 29 2a 38 36 34 30 30 30 30 30 2e 30 20 2b 20  ])*86400000.0 + 
815e0 30 2e 35 3b 0a 20 20 20 20 70 2d 3e 76 61 6c 69  0.5;.    p->vali
815f0 64 4a 44 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  dJD = 1;.  }else
81600 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65  {.    z = sqlite
81610 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
81620 76 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 21  v[0]);.    if( !
81630 7a 20 7c 7c 20 70 61 72 73 65 44 61 74 65 4f 72  z || parseDateOr
81640 54 69 6d 65 28 63 6f 6e 74 65 78 74 2c 20 28 63  Time(context, (c
81650 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20 20  har*)z, p) ){.  
81660 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
81670 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
81680 31 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  1; i<argc; i++){
81690 0a 20 20 20 20 69 66 28 20 28 7a 20 3d 20 73 71  .    if( (z = sq
816a0 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
816b0 28 61 72 67 76 5b 69 5d 29 29 3d 3d 30 20 7c 7c  (argv[i]))==0 ||
816c0 20 70 61 72 73 65 4d 6f 64 69 66 69 65 72 28 28   parseModifier((
816d0 63 68 61 72 2a 29 7a 2c 20 70 29 20 29 7b 0a 20  char*)z, p) ){. 
816e0 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
816f0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
81700 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54  n 0;.}.../*.** T
81710 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
81720 74 69 6e 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20  tines implement 
81730 74 68 65 20 76 61 72 69 6f 75 73 20 64 61 74 65  the various date
81740 20 61 6e 64 20 74 69 6d 65 20 66 75 6e 63 74 69   and time functi
81750 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51 4c 69 74 65  ons.** of SQLite
81760 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 20 20 20 6a  ..*/../*.**    j
81770 75 6c 69 61 6e 64 61 79 28 20 54 49 4d 45 53 54  ulianday( TIMEST
81780 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20  RING, MOD, MOD, 
81790 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ...).**.** Retur
817a0 6e 20 74 68 65 20 6a 75 6c 69 61 6e 20 64 61 79  n the julian day
817b0 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 64   number of the d
817c0 61 74 65 20 73 70 65 63 69 66 69 65 64 20 69 6e  ate specified in
817d0 20 74 68 65 20 61 72 67 75 6d 65 6e 74 73 0a 2a   the arguments.*
817e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6a 75  /.static void ju
817f0 6c 69 61 6e 64 61 79 46 75 6e 63 28 0a 20 20 73  liandayFunc(.  s
81800 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
81810 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
81820 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
81830 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
81840 20 44 61 74 65 54 69 6d 65 20 78 3b 0a 20 20 69   DateTime x;.  i
81850 66 28 20 69 73 44 61 74 65 28 63 6f 6e 74 65 78  f( isDate(contex
81860 74 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 26  t, argc, argv, &
81870 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63 6f 6d  x)==0 ){.    com
81880 70 75 74 65 4a 44 28 26 78 29 3b 0a 20 20 20 20  puteJD(&x);.    
81890 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 64  sqlite3_result_d
818a0 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20 78  ouble(context, x
818b0 2e 69 4a 44 2f 38 36 34 30 30 30 30 30 2e 30 29  .iJD/86400000.0)
818c0 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
818d0 20 20 64 61 74 65 74 69 6d 65 28 20 54 49 4d 45    datetime( TIME
818e0 53 54 52 49 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44  STRING, MOD, MOD
818f0 2c 20 2e 2e 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74  , ...).**.** Ret
81900 75 72 6e 20 59 59 59 59 2d 4d 4d 2d 44 44 20 48  urn YYYY-MM-DD H
81910 48 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69  H:MM:SS.*/.stati
81920 63 20 76 6f 69 64 20 64 61 74 65 74 69 6d 65 46  c void datetimeF
81930 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
81940 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
81950 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
81960 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
81970 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d  rgv.){.  DateTim
81980 65 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74  e x;.  if( isDat
81990 65 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c  e(context, argc,
819a0 20 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b   argv, &x)==0 ){
819b0 0a 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31  .    char zBuf[1
819c0 30 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65  00];.    compute
819d0 59 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 20  YMD_HMS(&x);.   
819e0 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
819f0 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 20  f(sizeof(zBuf), 
81a00 7a 42 75 66 2c 20 22 25 30 34 64 2d 25 30 32 64  zBuf, "%04d-%02d
81a10 2d 25 30 32 64 20 25 30 32 64 3a 25 30 32 64 3a  -%02d %02d:%02d:
81a20 25 30 32 64 22 2c 0a 20 20 20 20 20 20 20 20 20  %02d",.         
81a30 20 20 20 20 20 20 20 20 20 20 20 20 78 2e 59 2c              x.Y,
81a40 20 78 2e 4d 2c 20 78 2e 44 2c 20 78 2e 68 2c 20   x.M, x.D, x.h, 
81a50 78 2e 6d 2c 20 28 69 6e 74 29 28 78 2e 73 29 29  x.m, (int)(x.s))
81a60 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
81a70 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
81a80 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51 4c  t, zBuf, -1, SQL
81a90 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
81aa0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20 20    }.}../*.**    
81ab0 74 69 6d 65 28 20 54 49 4d 45 53 54 52 49 4e 47  time( TIMESTRING
81ac0 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29  , MOD, MOD, ...)
81ad0 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 48 48  .**.** Return HH
81ae0 3a 4d 4d 3a 53 53 0a 2a 2f 0a 73 74 61 74 69 63  :MM:SS.*/.static
81af0 20 76 6f 69 64 20 74 69 6d 65 46 75 6e 63 28 0a   void timeFunc(.
81b00 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78    sqlite3_contex
81b10 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e  t *context,.  in
81b20 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65  t argc,.  sqlite
81b30 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29  3_value **argv.)
81b40 7b 0a 20 20 44 61 74 65 54 69 6d 65 20 78 3b 0a  {.  DateTime x;.
81b50 20 20 69 66 28 20 69 73 44 61 74 65 28 63 6f 6e    if( isDate(con
81b60 74 65 78 74 2c 20 61 72 67 63 2c 20 61 72 67 76  text, argc, argv
81b70 2c 20 26 78 29 3d 3d 30 20 29 7b 0a 20 20 20 20  , &x)==0 ){.    
81b80 63 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a  char zBuf[100];.
81b90 20 20 20 20 63 6f 6d 70 75 74 65 48 4d 53 28 26      computeHMS(&
81ba0 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  x);.    sqlite3_
81bb0 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
81bc0 7a 42 75 66 29 2c 20 7a 42 75 66 2c 20 22 25 30  zBuf), zBuf, "%0
81bd0 32 64 3a 25 30 32 64 3a 25 30 32 64 22 2c 20 78  2d:%02d:%02d", x
81be0 2e 68 2c 20 78 2e 6d 2c 20 28 69 6e 74 29 78 2e  .h, x.m, (int)x.
81bf0 73 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  s);.    sqlite3_
81c00 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
81c10 65 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53  ext, zBuf, -1, S
81c20 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29  QLITE_TRANSIENT)
81c30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20  ;.  }.}../*.**  
81c40 20 20 64 61 74 65 28 20 54 49 4d 45 53 54 52 49    date( TIMESTRI
81c50 4e 47 2c 20 4d 4f 44 2c 20 4d 4f 44 2c 20 2e 2e  NG, MOD, MOD, ..
81c60 2e 29 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  .).**.** Return 
81c70 59 59 59 59 2d 4d 4d 2d 44 44 0a 2a 2f 0a 73 74  YYYY-MM-DD.*/.st
81c80 61 74 69 63 20 76 6f 69 64 20 64 61 74 65 46 75  atic void dateFu
81c90 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
81ca0 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
81cb0 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
81cc0 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
81cd0 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d 65  gv.){.  DateTime
81ce0 20 78 3b 0a 20 20 69 66 28 20 69 73 44 61 74 65   x;.  if( isDate
81cf0 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 63 2c 20  (context, argc, 
81d00 61 72 67 76 2c 20 26 78 29 3d 3d 30 20 29 7b 0a  argv, &x)==0 ){.
81d10 20 20 20 20 63 68 61 72 20 7a 42 75 66 5b 31 30      char zBuf[10
81d20 30 5d 3b 0a 20 20 20 20 63 6f 6d 70 75 74 65 59  0];.    computeY
81d30 4d 44 28 26 78 29 3b 0a 20 20 20 20 73 71 6c 69  MD(&x);.    sqli
81d40 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
81d50 65 6f 66 28 7a 42 75 66 29 2c 20 7a 42 75 66 2c  eof(zBuf), zBuf,
81d60 20 22 25 30 34 64 2d 25 30 32 64 2d 25 30 32 64   "%04d-%02d-%02d
81d70 22 2c 20 78 2e 59 2c 20 78 2e 4d 2c 20 78 2e 44  ", x.Y, x.M, x.D
81d80 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
81d90 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65  esult_text(conte
81da0 78 74 2c 20 7a 42 75 66 2c 20 2d 31 2c 20 53 51  xt, zBuf, -1, SQ
81db0 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
81dc0 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 20 20  .  }.}../*.**   
81dd0 20 73 74 72 66 74 69 6d 65 28 20 46 4f 52 4d 41   strftime( FORMA
81de0 54 2c 20 54 49 4d 45 53 54 52 49 4e 47 2c 20 4d  T, TIMESTRING, M
81df0 4f 44 2c 20 4d 4f 44 2c 20 2e 2e 2e 29 0a 2a 2a  OD, MOD, ...).**
81e00 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 73 74 72  .** Return a str
81e10 69 6e 67 20 64 65 73 63 72 69 62 65 64 20 62 79  ing described by
81e20 20 46 4f 52 4d 41 54 2e 20 20 43 6f 6e 76 65 72   FORMAT.  Conver
81e30 73 69 6f 6e 73 20 61 73 20 66 6f 6c 6c 6f 77 73  sions as follows
81e40 3a 0a 2a 2a 0a 2a 2a 20 20 20 25 64 20 20 64 61  :.**.**   %d  da
81e50 79 20 6f 66 20 6d 6f 6e 74 68 0a 2a 2a 20 20 20  y of month.**   
81e60 25 66 20 20 2a 2a 20 66 72 61 63 74 69 6f 6e 61  %f  ** fractiona
81e70 6c 20 73 65 63 6f 6e 64 73 20 20 53 53 2e 53 53  l seconds  SS.SS
81e80 53 0a 2a 2a 20 20 20 25 48 20 20 68 6f 75 72 20  S.**   %H  hour 
81e90 30 30 2d 32 34 0a 2a 2a 20 20 20 25 6a 20 20 64  00-24.**   %j  d
81ea0 61 79 20 6f 66 20 79 65 61 72 20 30 30 30 2d 33  ay of year 000-3
81eb0 36 36 0a 2a 2a 20 20 20 25 4a 20 20 2a 2a 20 4a  66.**   %J  ** J
81ec0 75 6c 69 61 6e 20 64 61 79 20 6e 75 6d 62 65 72  ulian day number
81ed0 0a 2a 2a 20 20 20 25 6d 20 20 6d 6f 6e 74 68 20  .**   %m  month 
81ee0 30 31 2d 31 32 0a 2a 2a 20 20 20 25 4d 20 20 6d  01-12.**   %M  m
81ef0 69 6e 75 74 65 20 30 30 2d 35 39 0a 2a 2a 20 20  inute 00-59.**  
81f00 20 25 73 20 20 73 65 63 6f 6e 64 73 20 73 69 6e   %s  seconds sin
81f10 63 65 20 31 39 37 30 2d 30 31 2d 30 31 0a 2a 2a  ce 1970-01-01.**
81f20 20 20 20 25 53 20 20 73 65 63 6f 6e 64 73 20 30     %S  seconds 0
81f30 30 2d 35 39 0a 2a 2a 20 20 20 25 77 20 20 64 61  0-59.**   %w  da
81f40 79 20 6f 66 20 77 65 65 6b 20 30 2d 36 20 20 73  y of week 0-6  s
81f50 75 6e 64 61 79 3d 3d 30 0a 2a 2a 20 20 20 25 57  unday==0.**   %W
81f60 20 20 77 65 65 6b 20 6f 66 20 79 65 61 72 20 30    week of year 0
81f70 30 2d 35 33 0a 2a 2a 20 20 20 25 59 20 20 79 65  0-53.**   %Y  ye
81f80 61 72 20 30 30 30 30 2d 39 39 39 39 0a 2a 2a 20  ar 0000-9999.** 
81f90 20 20 25 25 20 20 25 0a 2a 2f 0a 73 74 61 74 69    %%  %.*/.stati
81fa0 63 20 76 6f 69 64 20 73 74 72 66 74 69 6d 65 46  c void strftimeF
81fb0 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
81fc0 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
81fd0 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
81fe0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
81ff0 72 67 76 0a 29 7b 0a 20 20 44 61 74 65 54 69 6d  rgv.){.  DateTim
82000 65 20 78 3b 0a 20 20 75 36 34 20 6e 3b 0a 20 20  e x;.  u64 n;.  
82010 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 63 68 61 72  int i, j;.  char
82020 20 2a 7a 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   *z;.  sqlite3 *
82030 64 62 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  db;.  const char
82040 20 2a 7a 46 6d 74 20 3d 20 28 63 6f 6e 73 74 20   *zFmt = (const 
82050 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
82060 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
82070 29 3b 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 31  );.  char zBuf[1
82080 30 30 5d 3b 0a 20 20 69 66 28 20 7a 46 6d 74 3d  00];.  if( zFmt=
82090 3d 30 20 7c 7c 20 69 73 44 61 74 65 28 63 6f 6e  =0 || isDate(con
820a0 74 65 78 74 2c 20 61 72 67 63 2d 31 2c 20 61 72  text, argc-1, ar
820b0 67 76 2b 31 2c 20 26 78 29 20 29 20 72 65 74 75  gv+1, &x) ) retu
820c0 72 6e 3b 0a 20 20 64 62 20 3d 20 73 71 6c 69 74  rn;.  db = sqlit
820d0 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
820e0 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20  ndle(context);. 
820f0 20 66 6f 72 28 69 3d 30 2c 20 6e 3d 31 3b 20 7a   for(i=0, n=1; z
82100 46 6d 74 5b 69 5d 3b 20 69 2b 2b 2c 20 6e 2b 2b  Fmt[i]; i++, n++
82110 29 7b 0a 20 20 20 20 69 66 28 20 7a 46 6d 74 5b  ){.    if( zFmt[
82120 69 5d 3d 3d 27 25 27 20 29 7b 0a 20 20 20 20 20  i]=='%' ){.     
82130 20 73 77 69 74 63 68 28 20 7a 46 6d 74 5b 69 2b   switch( zFmt[i+
82140 31 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61  1] ){.        ca
82150 73 65 20 27 64 27 3a 0a 20 20 20 20 20 20 20 20  se 'd':.        
82160 63 61 73 65 20 27 48 27 3a 0a 20 20 20 20 20 20  case 'H':.      
82170 20 20 63 61 73 65 20 27 6d 27 3a 0a 20 20 20 20    case 'm':.    
82180 20 20 20 20 63 61 73 65 20 27 4d 27 3a 0a 20 20      case 'M':.  
82190 20 20 20 20 20 20 63 61 73 65 20 27 53 27 3a 0a        case 'S':.
821a0 20 20 20 20 20 20 20 20 63 61 73 65 20 27 57 27          case 'W'
821b0 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 2b 2b 3b  :.          n++;
821c0 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 61  .          /* fa
821d0 6c 6c 20 74 68 72 75 20 2a 2f 0a 20 20 20 20 20  ll thru */.     
821e0 20 20 20 63 61 73 65 20 27 77 27 3a 0a 20 20 20     case 'w':.   
821f0 20 20 20 20 20 63 61 73 65 20 27 25 27 3a 0a 20       case '%':. 
82200 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
82210 20 20 20 20 20 20 20 20 63 61 73 65 20 27 66 27          case 'f'
82220 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  :.          n +=
82230 20 38 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   8;.          br
82240 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73  eak;.        cas
82250 65 20 27 6a 27 3a 0a 20 20 20 20 20 20 20 20 20  e 'j':.         
82260 20 6e 20 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20   n += 3;.       
82270 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
82280 20 20 63 61 73 65 20 27 59 27 3a 0a 20 20 20 20    case 'Y':.    
82290 20 20 20 20 20 20 6e 20 2b 3d 20 38 3b 0a 20 20        n += 8;.  
822a0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
822b0 20 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a         case 's':
822c0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 4a  .        case 'J
822d0 27 3a 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b  ':.          n +
822e0 3d 20 35 30 3b 0a 20 20 20 20 20 20 20 20 20 20  = 50;.          
822f0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64  break;.        d
82300 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 20 20  efault:.        
82310 20 20 72 65 74 75 72 6e 3b 20 20 2f 2a 20 45 52    return;  /* ER
82320 52 4f 52 2e 20 20 72 65 74 75 72 6e 20 61 20 4e  ROR.  return a N
82330 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20  ULL */.      }. 
82340 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d 0a       i++;.    }.
82350 20 20 7d 0a 20 20 69 66 28 20 6e 3c 73 69 7a 65    }.  if( n<size
82360 6f 66 28 7a 42 75 66 29 20 29 7b 0a 20 20 20 20  of(zBuf) ){.    
82370 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 7d 65 6c 73  z = zBuf;.  }els
82380 65 20 69 66 28 20 6e 3e 28 75 36 34 29 64 62 2d  e if( n>(u64)db-
82390 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c  >aLimit[SQLITE_L
823a0 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a  IMIT_LENGTH] ){.
823b0 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
823c0 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 28  lt_error_toobig(
823d0 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 72 65  context);.    re
823e0 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  turn;.  }else{. 
823f0 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 44 62     z = sqlite3Db
82400 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 29  MallocRaw(db, n)
82410 3b 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29  ;.    if( z==0 )
82420 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
82430 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
82440 65 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20  em(context);.   
82450 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
82460 0a 20 20 7d 0a 20 20 63 6f 6d 70 75 74 65 4a 44  .  }.  computeJD
82470 28 26 78 29 3b 0a 20 20 63 6f 6d 70 75 74 65 59  (&x);.  computeY
82480 4d 44 5f 48 4d 53 28 26 78 29 3b 0a 20 20 66 6f  MD_HMS(&x);.  fo
82490 72 28 69 3d 6a 3d 30 3b 20 7a 46 6d 74 5b 69 5d  r(i=j=0; zFmt[i]
824a0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
824b0 7a 46 6d 74 5b 69 5d 21 3d 27 25 27 20 29 7b 0a  zFmt[i]!='%' ){.
824c0 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 7a        z[j++] = z
824d0 46 6d 74 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73  Fmt[i];.    }els
824e0 65 7b 0a 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20  e{.      i++;.  
824f0 20 20 20 20 73 77 69 74 63 68 28 20 7a 46 6d 74      switch( zFmt
82500 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 63  [i] ){.        c
82510 61 73 65 20 27 64 27 3a 20 20 73 71 6c 69 74 65  ase 'd':  sqlite
82520 33 5f 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a  3_snprintf(3, &z
82530 5b 6a 5d 2c 22 25 30 32 64 22 2c 78 2e 44 29 3b  [j],"%02d",x.D);
82540 20 6a 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20   j+=2; break;.  
82550 20 20 20 20 20 20 63 61 73 65 20 27 66 27 3a 20        case 'f': 
82560 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 75 62  {.          doub
82570 6c 65 20 73 20 3d 20 78 2e 73 3b 0a 20 20 20 20  le s = x.s;.    
82580 20 20 20 20 20 20 69 66 28 20 73 3e 35 39 2e 39        if( s>59.9
82590 39 39 20 29 20 73 20 3d 20 35 39 2e 39 39 39 3b  99 ) s = 59.999;
825a0 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
825b0 65 33 5f 73 6e 70 72 69 6e 74 66 28 37 2c 20 26  e3_snprintf(7, &
825c0 7a 5b 6a 5d 2c 22 25 30 36 2e 33 66 22 2c 20 73  z[j],"%06.3f", s
825d0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b  );.          j +
825e0 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b  = strlen(&z[j]);
825f0 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
82600 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
82610 20 20 20 20 63 61 73 65 20 27 48 27 3a 20 20 73      case 'H':  s
82620 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
82630 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c  3, &z[j],"%02d",
82640 78 2e 68 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61  x.h); j+=2; brea
82650 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
82660 27 57 27 3a 20 2f 2a 20 46 61 6c 6c 20 74 68 72  'W': /* Fall thr
82670 75 20 2a 2f 0a 20 20 20 20 20 20 20 20 63 61 73  u */.        cas
82680 65 20 27 6a 27 3a 20 7b 0a 20 20 20 20 20 20 20  e 'j': {.       
82690 20 20 20 69 6e 74 20 6e 44 61 79 3b 20 20 20 20     int nDay;    
826a0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
826b0 65 72 20 6f 66 20 64 61 79 73 20 73 69 6e 63 65  er of days since
826c0 20 31 73 74 20 64 61 79 20 6f 66 20 79 65 61 72   1st day of year
826d0 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 44 61   */.          Da
826e0 74 65 54 69 6d 65 20 79 20 3d 20 78 3b 0a 20 20  teTime y = x;.  
826f0 20 20 20 20 20 20 20 20 79 2e 76 61 6c 69 64 4a          y.validJ
82700 44 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  D = 0;.         
82710 20 79 2e 4d 20 3d 20 31 3b 0a 20 20 20 20 20 20   y.M = 1;.      
82720 20 20 20 20 79 2e 44 20 3d 20 31 3b 0a 20 20 20      y.D = 1;.   
82730 20 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4a 44         computeJD
82740 28 26 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (&y);.          
82750 6e 44 61 79 20 3d 20 28 78 2e 69 4a 44 20 2d 20  nDay = (x.iJD - 
82760 79 2e 69 4a 44 29 2f 38 36 34 30 30 30 30 30 2e  y.iJD)/86400000.
82770 30 20 2b 20 30 2e 35 3b 0a 20 20 20 20 20 20 20  0 + 0.5;.       
82780 20 20 20 69 66 28 20 7a 46 6d 74 5b 69 5d 3d 3d     if( zFmt[i]==
82790 27 57 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  'W' ){.         
827a0 20 20 20 69 6e 74 20 77 64 3b 20 20 20 2f 2a 20     int wd;   /* 
827b0 30 3d 4d 6f 6e 64 61 79 2c 20 31 3d 54 75 65 73  0=Monday, 1=Tues
827c0 64 61 79 2c 20 2e 2e 2e 20 36 3d 53 75 6e 64 61  day, ... 6=Sunda
827d0 79 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  y */.           
827e0 20 77 64 20 3d 20 28 28 78 2e 69 4a 44 2b 34 33   wd = ((x.iJD+43
827f0 32 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30  200000)/86400000
82800 29 20 25 20 37 3b 0a 20 20 20 20 20 20 20 20 20  ) % 7;.         
82810 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
82820 6e 74 66 28 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30  ntf(3, &z[j],"%0
82830 32 64 22 2c 28 6e 44 61 79 2b 37 2d 77 64 29 2f  2d",(nDay+7-wd)/
82840 37 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  7);.            
82850 6a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20 20 20  j += 2;.        
82860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
82870 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70       sqlite3_snp
82880 72 69 6e 74 66 28 34 2c 20 26 7a 5b 6a 5d 2c 22  rintf(4, &z[j],"
82890 25 30 33 64 22 2c 6e 44 61 79 2b 31 29 3b 0a 20  %03d",nDay+1);. 
828a0 20 20 20 20 20 20 20 20 20 20 20 6a 20 2b 3d 20             j += 
828b0 33 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  3;.          }. 
828c0 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
828d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
828e0 20 20 63 61 73 65 20 27 4a 27 3a 20 7b 0a 20 20    case 'J': {.  
828f0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
82900 73 6e 70 72 69 6e 74 66 28 32 30 2c 20 26 7a 5b  snprintf(20, &z[
82910 6a 5d 2c 22 25 2e 31 36 67 22 2c 78 2e 69 4a 44  j],"%.16g",x.iJD
82920 2f 38 36 34 30 30 30 30 30 2e 30 29 3b 0a 20 20  /86400000.0);.  
82930 20 20 20 20 20 20 20 20 6a 2b 3d 73 74 72 6c 65          j+=strle
82940 6e 28 26 7a 5b 6a 5d 29 3b 0a 20 20 20 20 20 20  n(&z[j]);.      
82950 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
82960 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73     }.        cas
82970 65 20 27 6d 27 3a 20 20 73 71 6c 69 74 65 33 5f  e 'm':  sqlite3_
82980 73 6e 70 72 69 6e 74 66 28 33 2c 20 26 7a 5b 6a  snprintf(3, &z[j
82990 5d 2c 22 25 30 32 64 22 2c 78 2e 4d 29 3b 20 6a  ],"%02d",x.M); j
829a0 2b 3d 32 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  +=2; break;.    
829b0 20 20 20 20 63 61 73 65 20 27 4d 27 3a 20 20 73      case 'M':  s
829c0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
829d0 33 2c 20 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c  3, &z[j],"%02d",
829e0 78 2e 6d 29 3b 20 6a 2b 3d 32 3b 20 62 72 65 61  x.m); j+=2; brea
829f0 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
82a00 27 73 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20  's': {.         
82a10 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
82a20 66 28 33 30 2c 26 7a 5b 6a 5d 2c 22 25 64 22 2c  f(30,&z[j],"%d",
82a30 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
82a40 20 20 20 20 20 20 20 20 20 20 20 20 28 69 6e 74              (int
82a50 29 28 78 2e 69 4a 44 2f 31 30 30 30 2e 30 20 2d  )(x.iJD/1000.0 -
82a60 20 32 31 30 38 36 36 37 36 30 30 30 30 2e 30 29   210866760000.0)
82a70 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a 20 2b  );.          j +
82a80 3d 20 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b  = strlen(&z[j]);
82a90 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b  .          break
82aa0 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
82ab0 20 20 20 20 63 61 73 65 20 27 53 27 3a 20 20 73      case 'S':  s
82ac0 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
82ad0 33 2c 26 7a 5b 6a 5d 2c 22 25 30 32 64 22 2c 28  3,&z[j],"%02d",(
82ae0 69 6e 74 29 78 2e 73 29 3b 20 6a 2b 3d 32 3b 20  int)x.s); j+=2; 
82af0 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
82b00 61 73 65 20 27 77 27 3a 20 20 7a 5b 6a 2b 2b 5d  ase 'w':  z[j++]
82b10 20 3d 20 28 28 28 78 2e 69 4a 44 2b 31 32 39 36   = (((x.iJD+1296
82b20 30 30 30 30 30 29 2f 38 36 34 30 30 30 30 30 29  00000)/86400000)
82b30 20 25 20 37 29 20 2b 20 27 30 27 3b 20 62 72 65   % 7) + '0'; bre
82b40 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
82b50 20 27 59 27 3a 20 20 73 71 6c 69 74 65 33 5f 73   'Y':  sqlite3_s
82b60 6e 70 72 69 6e 74 66 28 35 2c 26 7a 5b 6a 5d 2c  nprintf(5,&z[j],
82b70 22 25 30 34 64 22 2c 78 2e 59 29 3b 20 6a 2b 3d  "%04d",x.Y); j+=
82b80 73 74 72 6c 65 6e 28 26 7a 5b 6a 5d 29 3b 62 72  strlen(&z[j]);br
82b90 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 64 65 66  eak;.        def
82ba0 61 75 6c 74 3a 20 20 20 7a 5b 6a 2b 2b 5d 20 3d  ault:   z[j++] =
82bb0 20 27 25 27 3b 20 62 72 65 61 6b 3b 0a 20 20 20   '%'; break;.   
82bc0 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
82bd0 20 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c   z[j] = 0;.  sql
82be0 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74  ite3_result_text
82bf0 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c  (context, z, -1,
82c00 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
82c10 20 20 20 20 20 20 20 7a 3d 3d 7a 42 75 66 20 3f         z==zBuf ?
82c20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
82c30 54 20 3a 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d  T : SQLITE_DYNAM
82c40 49 43 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 63 75  IC);.}../*.** cu
82c50 72 72 65 6e 74 5f 74 69 6d 65 28 29 0a 2a 2a 0a  rrent_time().**.
82c60 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
82c70 20 72 65 74 75 72 6e 73 20 74 68 65 20 73 61 6d   returns the sam
82c80 65 20 76 61 6c 75 65 20 61 73 20 74 69 6d 65 28  e value as time(
82c90 27 6e 6f 77 27 29 2e 0a 2a 2f 0a 73 74 61 74 69  'now')..*/.stati
82ca0 63 20 76 6f 69 64 20 63 74 69 6d 65 46 75 6e 63  c void ctimeFunc
82cb0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
82cc0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
82cd0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
82ce0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
82cf0 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
82d00 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
82d10 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
82d20 29 3b 0a 20 20 74 69 6d 65 46 75 6e 63 28 63 6f  );.  timeFunc(co
82d30 6e 74 65 78 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a  ntext, 0, 0);.}.
82d40 0a 2f 2a 0a 2a 2a 20 63 75 72 72 65 6e 74 5f 64  ./*.** current_d
82d50 61 74 65 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73  ate().**.** This
82d60 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
82d70 73 20 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65  s the same value
82d80 20 61 73 20 64 61 74 65 28 27 6e 6f 77 27 29 2e   as date('now').
82d90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
82da0 63 64 61 74 65 46 75 6e 63 28 0a 20 20 73 71 6c  cdateFunc(.  sql
82db0 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
82dc0 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74  ntext,.  int Not
82dd0 55 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f  Used,.  sqlite3_
82de0 76 61 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32  value **NotUsed2
82df0 0a 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  .){.  UNUSED_PAR
82e00 41 4d 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c  AMETER2(NotUsed,
82e10 20 4e 6f 74 55 73 65 64 32 29 3b 0a 20 20 64 61   NotUsed2);.  da
82e20 74 65 46 75 6e 63 28 63 6f 6e 74 65 78 74 2c 20  teFunc(context, 
82e30 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0, 0);.}../*.** 
82e40 63 75 72 72 65 6e 74 5f 74 69 6d 65 73 74 61 6d  current_timestam
82e50 70 28 29 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  p().**.** This f
82e60 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20  unction returns 
82e70 74 68 65 20 73 61 6d 65 20 76 61 6c 75 65 20 61  the same value a
82e80 73 20 64 61 74 65 74 69 6d 65 28 27 6e 6f 77 27  s datetime('now'
82e90 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
82ea0 64 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e 63  d ctimestampFunc
82eb0 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
82ec0 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20  ext *context,.  
82ed0 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73  int NotUsed,.  s
82ee0 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e  qlite3_value **N
82ef0 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 55 4e 55  otUsed2.){.  UNU
82f00 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
82f10 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
82f20 29 3b 0a 20 20 64 61 74 65 74 69 6d 65 46 75 6e  );.  datetimeFun
82f30 63 28 63 6f 6e 74 65 78 74 2c 20 30 2c 20 30 29  c(context, 0, 0)
82f40 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64  ;.}.#endif /* !d
82f50 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
82f60 49 54 5f 44 41 54 45 54 49 4d 45 5f 46 55 4e 43  IT_DATETIME_FUNC
82f70 53 29 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  S) */..#ifdef SQ
82f80 4c 49 54 45 5f 4f 4d 49 54 5f 44 41 54 45 54 49  LITE_OMIT_DATETI
82f90 4d 45 5f 46 55 4e 43 53 0a 2f 2a 0a 2a 2a 20 49  ME_FUNCS./*.** I
82fa0 66 20 74 68 65 20 6c 69 62 72 61 72 79 20 69 73  f the library is
82fb0 20 63 6f 6d 70 69 6c 65 64 20 74 6f 20 6f 6d 69   compiled to omi
82fc0 74 20 74 68 65 20 66 75 6c 6c 2d 73 63 61 6c 65  t the full-scale
82fd0 20 64 61 74 65 20 61 6e 64 20 74 69 6d 65 0a 2a   date and time.*
82fe0 2a 20 68 61 6e 64 6c 69 6e 67 20 28 74 6f 20 67  * handling (to g
82ff0 65 74 20 61 20 73 6d 61 6c 6c 65 72 20 62 69 6e  et a smaller bin
83000 61 72 79 29 2c 20 74 68 65 20 66 6f 6c 6c 6f 77  ary), the follow
83010 69 6e 67 20 6d 69 6e 69 6d 61 6c 20 76 65 72 73  ing minimal vers
83020 69 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 66 75  ion.** of the fu
83030 6e 63 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 5f  nctions current_
83040 74 69 6d 65 28 29 2c 20 63 75 72 72 65 6e 74 5f  time(), current_
83050 64 61 74 65 28 29 20 61 6e 64 20 63 75 72 72 65  date() and curre
83060 6e 74 5f 74 69 6d 65 73 74 61 6d 70 28 29 0a 2a  nt_timestamp().*
83070 2a 20 61 72 65 20 69 6e 63 6c 75 64 65 64 20 69  * are included i
83080 6e 73 74 65 61 64 2e 20 54 68 69 73 20 69 73 20  nstead. This is 
83090 74 6f 20 73 75 70 70 6f 72 74 20 63 6f 6c 75 6d  to support colum
830a0 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e 73 20 74  n declarations t
830b0 68 61 74 0a 2a 2a 20 69 6e 63 6c 75 64 65 20 22  hat.** include "
830c0 44 45 46 41 55 4c 54 20 43 55 52 52 45 4e 54 5f  DEFAULT CURRENT_
830d0 54 49 4d 45 22 20 65 74 63 2e 0a 2a 2a 0a 2a 2a  TIME" etc..**.**
830e0 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75   This function u
830f0 73 65 73 20 74 68 65 20 43 2d 6c 69 62 72 61 72  ses the C-librar
83100 79 20 66 75 6e 63 74 69 6f 6e 73 20 74 69 6d 65  y functions time
83110 28 29 2c 20 67 6d 74 69 6d 65 28 29 0a 2a 2a 20  (), gmtime().** 
83120 61 6e 64 20 73 74 72 66 74 69 6d 65 28 29 2e 20  and strftime(). 
83130 54 68 65 20 66 6f 72 6d 61 74 20 73 74 72 69 6e  The format strin
83140 67 20 74 6f 20 70 61 73 73 20 74 6f 20 73 74 72  g to pass to str
83150 66 74 69 6d 65 28 29 20 69 73 20 73 75 70 70 6c  ftime() is suppl
83160 69 65 64 0a 2a 2a 20 61 73 20 74 68 65 20 75 73  ied.** as the us
83170 65 72 2d 64 61 74 61 20 66 6f 72 20 74 68 65 20  er-data for the 
83180 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  function..*/.sta
83190 74 69 63 20 76 6f 69 64 20 63 75 72 72 65 6e 74  tic void current
831a0 54 69 6d 65 46 75 6e 63 28 0a 20 20 73 71 6c 69  TimeFunc(.  sqli
831b0 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
831c0 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
831d0 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
831e0 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 74 69  e **argv.){.  ti
831f0 6d 65 5f 74 20 74 3b 0a 20 20 63 68 61 72 20 2a  me_t t;.  char *
83200 7a 46 6f 72 6d 61 74 20 3d 20 28 63 68 61 72 20  zFormat = (char 
83210 2a 29 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64  *)sqlite3_user_d
83220 61 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ata(context);.  
83230 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 64  sqlite3 *db;.  d
83240 6f 75 62 6c 65 20 72 54 3b 0a 20 20 63 68 61 72  ouble rT;.  char
83250 20 7a 42 75 66 5b 32 30 5d 3b 0a 0a 20 20 64 62   zBuf[20];..  db
83260 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
83270 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
83280 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  text);.  sqlite3
83290 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 64 62  OsCurrentTime(db
832a0 2d 3e 70 56 66 73 2c 20 26 72 54 29 3b 0a 20 20  ->pVfs, &rT);.  
832b0 74 20 3d 20 38 36 34 30 30 2e 30 2a 28 72 54 20  t = 86400.0*(rT 
832c0 2d 20 32 34 34 30 35 38 37 2e 35 29 20 2b 20 30  - 2440587.5) + 0
832d0 2e 35 3b 0a 23 69 66 64 65 66 20 48 41 56 45 5f  .5;.#ifdef HAVE_
832e0 47 4d 54 49 4d 45 5f 52 0a 20 20 7b 0a 20 20 20  GMTIME_R.  {.   
832f0 20 73 74 72 75 63 74 20 74 6d 20 73 4e 6f 77 3b   struct tm sNow;
83300 0a 20 20 20 20 67 6d 74 69 6d 65 5f 72 28 26 74  .    gmtime_r(&t
83310 2c 20 26 73 4e 6f 77 29 3b 0a 20 20 20 20 73 74  , &sNow);.    st
83320 72 66 74 69 6d 65 28 7a 42 75 66 2c 20 32 30 2c  rftime(zBuf, 20,
83330 20 7a 46 6f 72 6d 61 74 2c 20 26 73 4e 6f 77 29   zFormat, &sNow)
83340 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 7b 0a  ;.  }.#else.  {.
83350 20 20 20 20 73 74 72 75 63 74 20 74 6d 20 2a 70      struct tm *p
83360 54 6d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  Tm;.    sqlite3_
83370 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
83380 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
83390 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
833a0 43 5f 4d 41 53 54 45 52 29 29 3b 0a 20 20 20 20  C_MASTER));.    
833b0 70 54 6d 20 3d 20 67 6d 74 69 6d 65 28 26 74 29  pTm = gmtime(&t)
833c0 3b 0a 20 20 20 20 73 74 72 66 74 69 6d 65 28 7a  ;.    strftime(z
833d0 42 75 66 2c 20 32 30 2c 20 7a 46 6f 72 6d 61 74  Buf, 20, zFormat
833e0 2c 20 70 54 6d 29 3b 0a 20 20 20 20 73 71 6c 69  , pTm);.    sqli
833f0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
83400 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
83410 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
83420 54 41 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a  TATIC_MASTER));.
83430 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71    }.#endif..  sq
83440 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
83450 74 28 63 6f 6e 74 65 78 74 2c 20 7a 42 75 66 2c  t(context, zBuf,
83460 20 2d 31 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e   -1, SQLITE_TRAN
83470 53 49 45 4e 54 29 3b 0a 7d 0a 23 65 6e 64 69 66  SIENT);.}.#endif
83480 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
83490 63 74 69 6f 6e 20 72 65 67 69 73 74 65 72 65 64  ction registered
834a0 20 61 6c 6c 20 6f 66 20 74 68 65 20 61 62 6f 76   all of the abov
834b0 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20 61 73  e C functions as
834c0 20 53 51 4c 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e   SQL.** function
834d0 73 2e 20 20 54 68 69 73 20 73 68 6f 75 6c 64 20  s.  This should 
834e0 62 65 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75 74  be the only rout
834f0 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ine in this file
83500 20 77 69 74 68 0a 2a 2a 20 65 78 74 65 72 6e 61   with.** externa
83510 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2f 0a 53 51  l linkage..*/.SQ
83520 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
83530 64 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65  d sqlite3Registe
83540 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69 6f  rDateTimeFunctio
83550 6e 73 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ns(void){.  stat
83560 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 46 75  ic SQLITE_WSD Fu
83570 6e 63 44 65 66 20 61 44 61 74 65 54 69 6d 65 46  ncDef aDateTimeF
83580 75 6e 63 73 5b 5d 20 3d 20 7b 0a 23 69 66 6e 64  uncs[] = {.#ifnd
83590 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
835a0 41 54 45 54 49 4d 45 5f 46 55 4e 43 53 0a 20 20  ATETIME_FUNCS.  
835b0 20 20 46 55 4e 43 54 49 4f 4e 28 6a 75 6c 69 61    FUNCTION(julia
835c0 6e 64 61 79 2c 20 20 20 20 20 20 20 20 2d 31 2c  nday,        -1,
835d0 20 30 2c 20 30 2c 20 6a 75 6c 69 61 6e 64 61 79   0, 0, julianday
835e0 46 75 6e 63 20 29 2c 0a 20 20 20 20 46 55 4e 43  Func ),.    FUNC
835f0 54 49 4f 4e 28 64 61 74 65 2c 20 20 20 20 20 20  TION(date,      
83600 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c         -1, 0, 0,
83610 20 64 61 74 65 46 75 6e 63 20 20 20 20 20 20 29   dateFunc      )
83620 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 74  ,.    FUNCTION(t
83630 69 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ime,            
83640 20 2d 31 2c 20 30 2c 20 30 2c 20 74 69 6d 65 46   -1, 0, 0, timeF
83650 75 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20  unc      ),.    
83660 46 55 4e 43 54 49 4f 4e 28 64 61 74 65 74 69 6d  FUNCTION(datetim
83670 65 2c 20 20 20 20 20 20 20 20 20 2d 31 2c 20 30  e,         -1, 0
83680 2c 20 30 2c 20 64 61 74 65 74 69 6d 65 46 75 6e  , 0, datetimeFun
83690 63 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49  c  ),.    FUNCTI
836a0 4f 4e 28 73 74 72 66 74 69 6d 65 2c 20 20 20 20  ON(strftime,    
836b0 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 73       -1, 0, 0, s
836c0 74 72 66 74 69 6d 65 46 75 6e 63 20 20 29 2c 0a  trftimeFunc  ),.
836d0 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 75 72      FUNCTION(cur
836e0 72 65 6e 74 5f 74 69 6d 65 2c 20 20 20 20 20 20  rent_time,      
836f0 30 2c 20 30 2c 20 30 2c 20 63 74 69 6d 65 46 75  0, 0, 0, ctimeFu
83700 6e 63 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55  nc     ),.    FU
83710 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74  NCTION(current_t
83720 69 6d 65 73 74 61 6d 70 2c 20 30 2c 20 30 2c 20  imestamp, 0, 0, 
83730 30 2c 20 63 74 69 6d 65 73 74 61 6d 70 46 75 6e  0, ctimestampFun
83740 63 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e  c),.    FUNCTION
83750 28 63 75 72 72 65 6e 74 5f 64 61 74 65 2c 20 20  (current_date,  
83760 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 63 64 61      0, 0, 0, cda
83770 74 65 46 75 6e 63 20 20 20 20 20 29 2c 0a 23 65  teFunc     ),.#e
83780 6c 73 65 0a 20 20 20 20 53 54 52 5f 46 55 4e 43  lse.    STR_FUNC
83790 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69 6d  TION(current_tim
837a0 65 2c 20 20 20 20 20 20 30 2c 20 22 25 48 3a 25  e,      0, "%H:%
837b0 4d 3a 25 53 22 2c 20 20 20 20 20 20 20 20 20 20  M:%S",          
837c0 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46 75  0, currentTimeFu
837d0 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55 4e  nc),.    STR_FUN
837e0 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 74 69  CTION(current_ti
837f0 6d 65 73 74 61 6d 70 2c 20 30 2c 20 22 25 59 2d  mestamp, 0, "%Y-
83800 25 6d 2d 25 64 22 2c 20 20 20 20 20 20 20 20 20  %m-%d",         
83810 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65 46   0, currentTimeF
83820 75 6e 63 29 2c 0a 20 20 20 20 53 54 52 5f 46 55  unc),.    STR_FU
83830 4e 43 54 49 4f 4e 28 63 75 72 72 65 6e 74 5f 64  NCTION(current_d
83840 61 74 65 2c 20 20 20 20 20 20 30 2c 20 22 25 59  ate,      0, "%Y
83850 2d 25 6d 2d 25 64 20 25 48 3a 25 4d 3a 25 53 22  -%m-%d %H:%M:%S"
83860 2c 20 30 2c 20 63 75 72 72 65 6e 74 54 69 6d 65  , 0, currentTime
83870 46 75 6e 63 29 2c 0a 23 65 6e 64 69 66 0a 20 20  Func),.#endif.  
83880 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 46 75  };.  int i;.  Fu
83890 6e 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68  ncDefHash *pHash
838a0 20 3d 20 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44   = &GLOBAL(FuncD
838b0 65 66 48 61 73 68 2c 20 73 71 6c 69 74 65 33 47  efHash, sqlite3G
838c0 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b  lobalFunctions);
838d0 0a 20 20 46 75 6e 63 44 65 66 20 2a 61 46 75 6e  .  FuncDef *aFun
838e0 63 20 3d 20 28 46 75 6e 63 44 65 66 2a 29 26 47  c = (FuncDef*)&G
838f0 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 2c 20 61  LOBAL(FuncDef, a
83900 44 61 74 65 54 69 6d 65 46 75 6e 63 73 29 3b 0a  DateTimeFuncs);.
83910 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72  .  for(i=0; i<Ar
83920 72 61 79 53 69 7a 65 28 61 44 61 74 65 54 69 6d  raySize(aDateTim
83930 65 46 75 6e 63 73 29 3b 20 69 2b 2b 29 7b 0a 20  eFuncs); i++){. 
83940 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63 44 65     sqlite3FuncDe
83950 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 26  fInsert(pHash, &
83960 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d 0a 7d  aFunc[i]);.  }.}
83970 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
83980 2a 20 45 6e 64 20 6f 66 20 64 61 74 65 2e 63 20  * End of date.c 
83990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
839a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
839b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
839c0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
839d0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 2e  * Begin file os.
839e0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
839f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83a10 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20 4e 6f 76  /./*.** 2005 Nov
83a20 65 6d 62 65 72 20 32 39 0a 2a 2a 0a 2a 2a 20 54  ember 29.**.** T
83a30 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
83a40 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
83a50 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
83a60 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
83a70 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
83a80 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
83a90 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
83aa0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
83ab0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
83ac0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
83ad0 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
83ae0 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
83af0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
83b00 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
83b10 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
83b20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
83b30 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
83b40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
83b80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
83b90 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
83ba0 74 61 69 6e 73 20 4f 53 20 69 6e 74 65 72 66 61  tains OS interfa
83bb0 63 65 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ce code that is 
83bc0 63 6f 6d 6d 6f 6e 20 74 6f 20 61 6c 6c 0a 2a 2a  common to all.**
83bd0 20 61 72 63 68 69 74 65 63 74 75 72 65 73 2e 0a   architectures..
83be0 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 2e 63 2c  **.** $Id: os.c,
83bf0 76 20 31 2e 31 32 34 20 32 30 30 38 2f 31 30 2f  v 1.124 2008/10/
83c00 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68 20  07 15:25:48 drh 
83c10 45 78 70 20 24 0a 2a 2f 0a 23 64 65 66 69 6e 65  Exp $.*/.#define
83c20 20 5f 53 51 4c 49 54 45 5f 4f 53 5f 43 5f 20 31   _SQLITE_OS_C_ 1
83c30 0a 23 75 6e 64 65 66 20 5f 53 51 4c 49 54 45 5f  .#undef _SQLITE_
83c40 4f 53 5f 43 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  OS_C_../*.** The
83c50 20 64 65 66 61 75 6c 74 20 53 51 4c 69 74 65 20   default SQLite 
83c60 73 71 6c 69 74 65 33 5f 76 66 73 20 69 6d 70 6c  sqlite3_vfs impl
83c70 65 6d 65 6e 74 61 74 69 6f 6e 73 20 64 6f 20 6e  ementations do n
83c80 6f 74 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d  ot allocate.** m
83c90 65 6d 6f 72 79 20 28 61 63 74 75 61 6c 6c 79 2c  emory (actually,
83ca0 20 6f 73 5f 75 6e 69 78 2e 63 20 61 6c 6c 6f 63   os_unix.c alloc
83cb0 61 74 65 73 20 61 20 73 6d 61 6c 6c 20 61 6d 6f  ates a small amo
83cc0 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a  unt of memory.**
83cd0 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 4f 73 4f   from within OsO
83ce0 70 65 6e 28 29 29 2c 20 62 75 74 20 73 6f 6d 65  pen()), but some
83cf0 20 74 68 69 72 64 2d 70 61 72 74 79 20 69 6d 70   third-party imp
83d00 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 6d 61 79  lementations may
83d10 2e 0a 2a 2a 20 53 6f 20 77 65 20 74 65 73 74 20  ..** So we test 
83d20 74 68 65 20 65 66 66 65 63 74 73 20 6f 66 20 61  the effects of a
83d30 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 69 6e   malloc() failin
83d40 67 20 61 6e 64 20 74 68 65 20 73 71 6c 69 74 65  g and the sqlite
83d50 33 4f 73 58 58 58 28 29 0a 2a 2a 20 66 75 6e 63  3OsXXX().** func
83d60 74 69 6f 6e 20 72 65 74 75 72 6e 69 6e 67 20 53  tion returning S
83d70 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45  QLITE_IOERR_NOME
83d80 4d 20 75 73 69 6e 67 20 74 68 65 20 44 4f 5f 4f  M using the DO_O
83d90 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 6d 61  S_MALLOC_TEST ma
83da0 63 72 6f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  cro..**.** The f
83db0 6f 6c 6c 6f 77 69 6e 67 20 66 75 6e 63 74 69 6f  ollowing functio
83dc0 6e 73 20 61 72 65 20 69 6e 73 74 72 75 6d 65 6e  ns are instrumen
83dd0 74 65 64 20 66 6f 72 20 6d 61 6c 6c 6f 63 28 29  ted for malloc()
83de0 20 66 61 69 6c 75 72 65 20 0a 2a 2a 20 74 65 73   failure .** tes
83df0 74 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ting:.**.**     
83e00 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 29 0a  sqlite3OsOpen().
83e10 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  **     sqlite3Os
83e20 52 65 61 64 28 29 0a 2a 2a 20 20 20 20 20 73 71  Read().**     sq
83e30 6c 69 74 65 33 4f 73 57 72 69 74 65 28 29 0a 2a  lite3OsWrite().*
83e40 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53  *     sqlite3OsS
83e50 79 6e 63 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c  ync().**     sql
83e60 69 74 65 33 4f 73 4c 6f 63 6b 28 29 0a 2a 2a 0a  ite3OsLock().**.
83e70 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  */.#if defined(S
83e80 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20 28  QLITE_TEST) && (
83e90 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 3d 3d 30  SQLITE_OS_WIN==0
83ea0 29 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f 4f  ).  #define DO_O
83eb0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 20 69 66  S_MALLOC_TEST if
83ec0 20 28 31 29 20 7b 20 20 20 20 20 20 20 20 20 20   (1) {          
83ed0 20 20 5c 0a 20 20 20 20 76 6f 69 64 20 2a 70 54    \.    void *pT
83ee0 73 74 41 6c 6c 6f 63 20 3d 20 73 71 6c 69 74 65  stAlloc = sqlite
83ef0 33 4d 61 6c 6c 6f 63 28 31 30 29 3b 20 20 20 20  3Malloc(10);    
83f00 20 20 20 5c 0a 20 20 20 20 69 66 20 28 21 70 54     \.    if (!pT
83f10 73 74 41 6c 6c 6f 63 29 20 72 65 74 75 72 6e 20  stAlloc) return 
83f20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e 4f 4d  SQLITE_IOERR_NOM
83f30 45 4d 3b 20 20 5c 0a 20 20 20 20 73 71 6c 69 74  EM;  \.    sqlit
83f40 65 33 5f 66 72 65 65 28 70 54 73 74 41 6c 6c 6f  e3_free(pTstAllo
83f50 63 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  c);             
83f60 20 20 20 20 20 20 20 5c 0a 20 20 7d 0a 23 65 6c         \.  }.#el
83f70 73 65 0a 20 20 23 64 65 66 69 6e 65 20 44 4f 5f  se.  #define DO_
83f80 4f 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 0a 23  OS_MALLOC_TEST.#
83f90 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
83fa0 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
83fb0 6e 65 73 20 61 72 65 20 63 6f 6e 76 65 6e 69 65  nes are convenie
83fc0 6e 63 65 20 77 72 61 70 70 65 72 73 20 61 72 6f  nce wrappers aro
83fd0 75 6e 64 20 6d 65 74 68 6f 64 73 0a 2a 2a 20 6f  und methods.** o
83fe0 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 66 69  f the sqlite3_fi
83ff0 6c 65 20 6f 62 6a 65 63 74 2e 20 20 54 68 69 73  le object.  This
84000 20 69 73 20 6d 6f 73 74 6c 79 20 6a 75 73 74 20   is mostly just 
84010 73 79 6e 74 61 63 74 69 63 20 73 75 67 61 72 2e  syntactic sugar.
84020 20 41 6c 6c 0a 2a 2a 20 6f 66 20 74 68 69 73 20   All.** of this 
84030 77 6f 75 6c 64 20 62 65 20 63 6f 6d 70 6c 65 74  would be complet
84040 65 6c 79 20 61 75 74 6f 6d 61 74 69 63 20 69 66  ely automatic if
84050 20 53 51 4c 69 74 65 20 77 65 72 65 20 63 6f 64   SQLite were cod
84060 65 64 20 75 73 69 6e 67 0a 2a 2a 20 43 2b 2b 20  ed using.** C++ 
84070 69 6e 73 74 65 61 64 20 6f 66 20 70 6c 61 69 6e  instead of plain
84080 20 6f 6c 64 20 43 2e 0a 2a 2f 0a 53 51 4c 49 54   old C..*/.SQLIT
84090 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
840a0 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 73 71 6c  lite3OsClose(sql
840b0 69 74 65 33 5f 66 69 6c 65 20 2a 70 49 64 29 7b  ite3_file *pId){
840c0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
840d0 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 49 64  TE_OK;.  if( pId
840e0 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20  ->pMethods ){.  
840f0 20 20 72 63 20 3d 20 70 49 64 2d 3e 70 4d 65 74    rc = pId->pMet
84100 68 6f 64 73 2d 3e 78 43 6c 6f 73 65 28 70 49 64  hods->xClose(pId
84110 29 3b 0a 20 20 20 20 70 49 64 2d 3e 70 4d 65 74  );.    pId->pMet
84120 68 6f 64 73 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  hods = 0;.  }.  
84130 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c  return rc;.}.SQL
84140 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
84150 73 71 6c 69 74 65 33 4f 73 52 65 61 64 28 73 71  sqlite3OsRead(sq
84160 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
84170 76 6f 69 64 20 2a 70 42 75 66 2c 20 69 6e 74 20  void *pBuf, int 
84180 61 6d 74 2c 20 69 36 34 20 6f 66 66 73 65 74 29  amt, i64 offset)
84190 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  {.  DO_OS_MALLOC
841a0 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20  _TEST;.  return 
841b0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 52  id->pMethods->xR
841c0 65 61 64 28 69 64 2c 20 70 42 75 66 2c 20 61 6d  ead(id, pBuf, am
841d0 74 2c 20 6f 66 66 73 65 74 29 3b 0a 7d 0a 53 51  t, offset);.}.SQ
841e0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
841f0 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28   sqlite3OsWrite(
84200 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
84210 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42  , const void *pB
84220 75 66 2c 20 69 6e 74 20 61 6d 74 2c 20 69 36 34  uf, int amt, i64
84230 20 6f 66 66 73 65 74 29 7b 0a 20 20 44 4f 5f 4f   offset){.  DO_O
84240 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
84250 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
84260 68 6f 64 73 2d 3e 78 57 72 69 74 65 28 69 64 2c  hods->xWrite(id,
84270 20 70 42 75 66 2c 20 61 6d 74 2c 20 6f 66 66 73   pBuf, amt, offs
84280 65 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  et);.}.SQLITE_PR
84290 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
842a0 33 4f 73 54 72 75 6e 63 61 74 65 28 73 71 6c 69  3OsTruncate(sqli
842b0 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36  te3_file *id, i6
842c0 34 20 73 69 7a 65 29 7b 0a 20 20 72 65 74 75 72  4 size){.  retur
842d0 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e  n id->pMethods->
842e0 78 54 72 75 6e 63 61 74 65 28 69 64 2c 20 73 69  xTruncate(id, si
842f0 7a 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  ze);.}.SQLITE_PR
84300 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
84310 33 4f 73 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  3OsSync(sqlite3_
84320 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c  file *id, int fl
84330 61 67 73 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  ags){.  DO_OS_MA
84340 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
84350 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
84360 2d 3e 78 53 79 6e 63 28 69 64 2c 20 66 6c 61 67  ->xSync(id, flag
84370 73 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  s);.}.SQLITE_PRI
84380 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
84390 4f 73 46 69 6c 65 53 69 7a 65 28 73 71 6c 69 74  OsFileSize(sqlit
843a0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
843b0 20 2a 70 53 69 7a 65 29 7b 0a 20 20 44 4f 5f 4f   *pSize){.  DO_O
843c0 53 5f 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20  S_MALLOC_TEST;. 
843d0 20 72 65 74 75 72 6e 20 69 64 2d 3e 70 4d 65 74   return id->pMet
843e0 68 6f 64 73 2d 3e 78 46 69 6c 65 53 69 7a 65 28  hods->xFileSize(
843f0 69 64 2c 20 70 53 69 7a 65 29 3b 0a 7d 0a 53 51  id, pSize);.}.SQ
84400 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
84410 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 73   sqlite3OsLock(s
84420 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
84430 20 69 6e 74 20 6c 6f 63 6b 54 79 70 65 29 7b 0a   int lockType){.
84440 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43 5f 54    DO_OS_MALLOC_T
84450 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20 69 64  EST;.  return id
84460 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 4c 6f 63  ->pMethods->xLoc
84470 6b 28 69 64 2c 20 6c 6f 63 6b 54 79 70 65 29 3b  k(id, lockType);
84480 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
84490 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 55  E int sqlite3OsU
844a0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
844b0 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
844c0 54 79 70 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  Type){.  return 
844d0 69 64 2d 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 55  id->pMethods->xU
844e0 6e 6c 6f 63 6b 28 69 64 2c 20 6c 6f 63 6b 54 79  nlock(id, lockTy
844f0 70 65 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  pe);.}.SQLITE_PR
84500 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
84510 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64  3OsCheckReserved
84520 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
84530 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
84540 4f 75 74 29 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41  Out){.  DO_OS_MA
84550 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72 65 74  LLOC_TEST;.  ret
84560 75 72 6e 20 69 64 2d 3e 70 4d 65 74 68 6f 64 73  urn id->pMethods
84570 2d 3e 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  ->xCheckReserved
84580 4c 6f 63 6b 28 69 64 2c 20 70 52 65 73 4f 75 74  Lock(id, pResOut
84590 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  );.}.SQLITE_PRIV
845a0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
845b0 73 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71 6c  sFileControl(sql
845c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
845d0 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
845e0 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d  g){.  return id-
845f0 3e 70 4d 65 74 68 6f 64 73 2d 3e 78 46 69 6c 65  >pMethods->xFile
84600 43 6f 6e 74 72 6f 6c 28 69 64 2c 20 6f 70 2c 20  Control(id, op, 
84610 70 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pArg);.}.SQLITE_
84620 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
84630 74 65 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28  te3OsSectorSize(
84640 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
84650 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53 65 63 74  ){.  int (*xSect
84660 6f 72 53 69 7a 65 29 28 73 71 6c 69 74 65 33 5f  orSize)(sqlite3_
84670 66 69 6c 65 2a 29 20 3d 20 69 64 2d 3e 70 4d 65  file*) = id->pMe
84680 74 68 6f 64 73 2d 3e 78 53 65 63 74 6f 72 53 69  thods->xSectorSi
84690 7a 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 78 53  ze;.  return (xS
846a0 65 63 74 6f 72 53 69 7a 65 20 3f 20 78 53 65 63  ectorSize ? xSec
846b0 74 6f 72 53 69 7a 65 28 69 64 29 20 3a 20 53 51  torSize(id) : SQ
846c0 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43  LITE_DEFAULT_SEC
846d0 54 4f 52 5f 53 49 5a 45 29 3b 0a 7d 0a 53 51 4c  TOR_SIZE);.}.SQL
846e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
846f0 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43  sqlite3OsDeviceC
84700 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28 73  haracteristics(s
84710 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29  qlite3_file *id)
84720 7b 0a 20 20 72 65 74 75 72 6e 20 69 64 2d 3e 70  {.  return id->p
84730 4d 65 74 68 6f 64 73 2d 3e 78 44 65 76 69 63 65  Methods->xDevice
84740 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
84750 69 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  id);.}../*.** Th
84760 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66 20  e next group of 
84770 72 6f 75 74 69 6e 65 73 20 61 72 65 20 63 6f 6e  routines are con
84780 76 65 6e 69 65 6e 63 65 20 77 72 61 70 70 65 72  venience wrapper
84790 73 20 61 72 6f 75 6e 64 20 74 68 65 0a 2a 2a 20  s around the.** 
847a0 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a  VFS methods..*/.
847b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
847c0 6e 74 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e  nt sqlite3OsOpen
847d0 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  (.  sqlite3_vfs 
847e0 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e 73 74 20  *pVfs, .  const 
847f0 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20  char *zPath, .  
84800 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46  sqlite3_file *pF
84810 69 6c 65 2c 20 0a 20 20 69 6e 74 20 66 6c 61 67  ile, .  int flag
84820 73 2c 20 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67  s, .  int *pFlag
84830 73 4f 75 74 0a 29 7b 0a 20 20 44 4f 5f 4f 53 5f  sOut.){.  DO_OS_
84840 4d 41 4c 4c 4f 43 5f 54 45 53 54 3b 0a 20 20 72  MALLOC_TEST;.  r
84850 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 4f 70 65  eturn pVfs->xOpe
84860 6e 28 70 56 66 73 2c 20 7a 50 61 74 68 2c 20 70  n(pVfs, zPath, p
84870 46 69 6c 65 2c 20 66 6c 61 67 73 2c 20 70 46 6c  File, flags, pFl
84880 61 67 73 4f 75 74 29 3b 0a 7d 0a 53 51 4c 49 54  agsOut);.}.SQLIT
84890 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
848a0 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28 73 71  lite3OsDelete(sq
848b0 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
848c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61   const char *zPa
848d0 74 68 2c 20 69 6e 74 20 64 69 72 53 79 6e 63 29  th, int dirSync)
848e0 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
848f0 3e 78 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a  >xDelete(pVfs, z
84900 50 61 74 68 2c 20 64 69 72 53 79 6e 63 29 3b 0a  Path, dirSync);.
84910 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
84920 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 41 63   int sqlite3OsAc
84930 63 65 73 73 28 0a 20 20 73 71 6c 69 74 65 33 5f  cess(.  sqlite3_
84940 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f  vfs *pVfs, .  co
84950 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c  nst char *zPath,
84960 20 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 0a   .  int flags, .
84970 20 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 0a 29    int *pResOut.)
84980 7b 0a 20 20 44 4f 5f 4f 53 5f 4d 41 4c 4c 4f 43  {.  DO_OS_MALLOC
84990 5f 54 45 53 54 3b 0a 20 20 72 65 74 75 72 6e 20  _TEST;.  return 
849a0 70 56 66 73 2d 3e 78 41 63 63 65 73 73 28 70 56  pVfs->xAccess(pV
849b0 66 73 2c 20 7a 50 61 74 68 2c 20 66 6c 61 67 73  fs, zPath, flags
849c0 2c 20 70 52 65 73 4f 75 74 29 3b 0a 7d 0a 53 51  , pResOut);.}.SQ
849d0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
849e0 20 73 71 6c 69 74 65 33 4f 73 46 75 6c 6c 50 61   sqlite3OsFullPa
849f0 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69 74 65  thname(.  sqlite
84a00 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 0a 20 20  3_vfs *pVfs, .  
84a10 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50 61 74  const char *zPat
84a20 68 2c 20 0a 20 20 69 6e 74 20 6e 50 61 74 68 4f  h, .  int nPathO
84a30 75 74 2c 20 0a 20 20 63 68 61 72 20 2a 7a 50 61  ut, .  char *zPa
84a40 74 68 4f 75 74 0a 29 7b 0a 20 20 72 65 74 75 72  thOut.){.  retur
84a50 6e 20 70 56 66 73 2d 3e 78 46 75 6c 6c 50 61 74  n pVfs->xFullPat
84a60 68 6e 61 6d 65 28 70 56 66 73 2c 20 7a 50 61 74  hname(pVfs, zPat
84a70 68 2c 20 6e 50 61 74 68 4f 75 74 2c 20 7a 50 61  h, nPathOut, zPa
84a80 74 68 4f 75 74 29 3b 0a 7d 0a 23 69 66 6e 64 65  thOut);.}.#ifnde
84a90 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
84aa0 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 53 51 4c  AD_EXTENSION.SQL
84ab0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
84ac0 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 4f 70 65   *sqlite3OsDlOpe
84ad0 6e 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  n(sqlite3_vfs *p
84ae0 56 66 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Vfs, const char 
84af0 2a 7a 50 61 74 68 29 7b 0a 20 20 72 65 74 75 72  *zPath){.  retur
84b00 6e 20 70 56 66 73 2d 3e 78 44 6c 4f 70 65 6e 28  n pVfs->xDlOpen(
84b10 70 56 66 73 2c 20 7a 50 61 74 68 29 3b 0a 7d 0a  pVfs, zPath);.}.
84b20 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
84b30 6f 69 64 20 73 71 6c 69 74 65 33 4f 73 44 6c 45  oid sqlite3OsDlE
84b40 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
84b50 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74   *pVfs, int nByt
84b60 65 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74  e, char *zBufOut
84b70 29 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 45 72  ){.  pVfs->xDlEr
84b80 72 6f 72 28 70 56 66 73 2c 20 6e 42 79 74 65 2c  ror(pVfs, nByte,
84b90 20 7a 42 75 66 4f 75 74 29 3b 0a 7d 0a 53 51 4c   zBufOut);.}.SQL
84ba0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
84bb0 20 2a 73 71 6c 69 74 65 33 4f 73 44 6c 53 79 6d   *sqlite3OsDlSym
84bc0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
84bd0 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c  fs, void *pHandl
84be0 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
84bf0 53 79 6d 62 6f 6c 29 7b 0a 20 20 72 65 74 75 72  Symbol){.  retur
84c00 6e 20 70 56 66 73 2d 3e 78 44 6c 53 79 6d 28 70  n pVfs->xDlSym(p
84c10 56 66 73 2c 20 70 48 61 6e 64 6c 65 2c 20 7a 53  Vfs, pHandle, zS
84c20 79 6d 62 6f 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45  ymbol);.}.SQLITE
84c30 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
84c40 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 73  lite3OsDlClose(s
84c50 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
84c60 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65 29  , void *pHandle)
84c70 7b 0a 20 20 70 56 66 73 2d 3e 78 44 6c 43 6c 6f  {.  pVfs->xDlClo
84c80 73 65 28 70 56 66 73 2c 20 70 48 61 6e 64 6c 65  se(pVfs, pHandle
84c90 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
84ca0 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f  QLITE_OMIT_LOAD_
84cb0 45 58 54 45 4e 53 49 4f 4e 20 2a 2f 0a 53 51 4c  EXTENSION */.SQL
84cc0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
84cd0 73 71 6c 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e  sqlite3OsRandomn
84ce0 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ess(sqlite3_vfs 
84cf0 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 79 74 65  *pVfs, int nByte
84d00 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29  , char *zBufOut)
84d10 7b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 2d  {.  return pVfs-
84d20 3e 78 52 61 6e 64 6f 6d 6e 65 73 73 28 70 56 66  >xRandomness(pVf
84d30 73 2c 20 6e 42 79 74 65 2c 20 7a 42 75 66 4f 75  s, nByte, zBufOu
84d40 74 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  t);.}.SQLITE_PRI
84d50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
84d60 4f 73 53 6c 65 65 70 28 73 71 6c 69 74 65 33 5f  OsSleep(sqlite3_
84d70 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e  vfs *pVfs, int n
84d80 4d 69 63 72 6f 29 7b 0a 20 20 72 65 74 75 72 6e  Micro){.  return
84d90 20 70 56 66 73 2d 3e 78 53 6c 65 65 70 28 70 56   pVfs->xSleep(pV
84da0 66 73 2c 20 6e 4d 69 63 72 6f 29 3b 0a 7d 0a 53  fs, nMicro);.}.S
84db0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
84dc0 74 20 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65  t sqlite3OsCurre
84dd0 6e 74 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76  ntTime(sqlite3_v
84de0 66 73 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65  fs *pVfs, double
84df0 20 2a 70 54 69 6d 65 4f 75 74 29 7b 0a 20 20 72   *pTimeOut){.  r
84e00 65 74 75 72 6e 20 70 56 66 73 2d 3e 78 43 75 72  eturn pVfs->xCur
84e10 72 65 6e 74 54 69 6d 65 28 70 56 66 73 2c 20 70  rentTime(pVfs, p
84e20 54 69 6d 65 4f 75 74 29 3b 0a 7d 0a 0a 53 51 4c  TimeOut);.}..SQL
84e30 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
84e40 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c  sqlite3OsOpenMal
84e50 6c 6f 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  loc(.  sqlite3_v
84e60 66 73 20 2a 70 56 66 73 2c 20 0a 20 20 63 6f 6e  fs *pVfs, .  con
84e70 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20  st char *zFile, 
84e80 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  .  sqlite3_file 
84e90 2a 2a 70 70 46 69 6c 65 2c 20 0a 20 20 69 6e 74  **ppFile, .  int
84ea0 20 66 6c 61 67 73 2c 0a 20 20 69 6e 74 20 2a 70   flags,.  int *p
84eb0 4f 75 74 46 6c 61 67 73 0a 29 7b 0a 20 20 69 6e  OutFlags.){.  in
84ec0 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  t rc = SQLITE_NO
84ed0 4d 45 4d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  MEM;.  sqlite3_f
84ee0 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20 70 46  ile *pFile;.  pF
84ef0 69 6c 65 20 3d 20 28 73 71 6c 69 74 65 33 5f 66  ile = (sqlite3_f
84f00 69 6c 65 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  ile *)sqlite3Mal
84f10 6c 6f 63 28 70 56 66 73 2d 3e 73 7a 4f 73 46 69  loc(pVfs->szOsFi
84f20 6c 65 29 3b 0a 20 20 69 66 28 20 70 46 69 6c 65  le);.  if( pFile
84f30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   ){.    rc = sql
84f40 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c  ite3OsOpen(pVfs,
84f50 20 7a 46 69 6c 65 2c 20 70 46 69 6c 65 2c 20 66   zFile, pFile, f
84f60 6c 61 67 73 2c 20 70 4f 75 74 46 6c 61 67 73 29  lags, pOutFlags)
84f70 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
84f80 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
84f90 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 46   sqlite3_free(pF
84fa0 69 6c 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ile);.    }else{
84fb0 0a 20 20 20 20 20 20 2a 70 70 46 69 6c 65 20 3d  .      *ppFile =
84fc0 20 70 46 69 6c 65 3b 0a 20 20 20 20 7d 0a 20 20   pFile;.    }.  
84fd0 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
84fe0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
84ff0 69 6e 74 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f  int sqlite3OsClo
85000 73 65 46 72 65 65 28 73 71 6c 69 74 65 33 5f 66  seFree(sqlite3_f
85010 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
85020 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
85030 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  K;.  assert( pFi
85040 6c 65 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  le );.  rc = sql
85050 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 46 69 6c  ite3OsClose(pFil
85060 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  e);.  sqlite3_fr
85070 65 65 28 70 46 69 6c 65 29 3b 0a 20 20 72 65 74  ee(pFile);.  ret
85080 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
85090 20 54 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c   The list of all
850a0 20 72 65 67 69 73 74 65 72 65 64 20 56 46 53 20   registered VFS 
850b0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e  implementations.
850c0 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69 74  .*/.static sqlit
850d0 65 33 5f 76 66 73 20 2a 20 53 51 4c 49 54 45 5f  e3_vfs * SQLITE_
850e0 57 53 44 20 76 66 73 4c 69 73 74 20 3d 20 30 3b  WSD vfsList = 0;
850f0 0a 23 64 65 66 69 6e 65 20 76 66 73 4c 69 73 74  .#define vfsList
85100 20 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 5f   GLOBAL(sqlite3_
85110 76 66 73 20 2a 2c 20 76 66 73 4c 69 73 74 29 0a  vfs *, vfsList).
85120 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 20  ./*.** Locate a 
85130 56 46 53 20 62 79 20 6e 61 6d 65 2e 20 20 49 66  VFS by name.  If
85140 20 6e 6f 20 6e 61 6d 65 20 69 73 20 67 69 76 65   no name is give
85150 6e 2c 20 73 69 6d 70 6c 79 20 72 65 74 75 72 6e  n, simply return
85160 20 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 56 46   the.** first VF
85170 53 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e 0a 2a  S on the list..*
85180 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c  /.SQLITE_API sql
85190 69 74 65 33 5f 76 66 73 20 2a 73 71 6c 69 74 65  ite3_vfs *sqlite
851a0 33 5f 76 66 73 5f 66 69 6e 64 28 63 6f 6e 73 74  3_vfs_find(const
851b0 20 63 68 61 72 20 2a 7a 56 66 73 29 7b 0a 20 20   char *zVfs){.  
851c0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
851d0 73 20 3d 20 30 3b 0a 23 69 66 20 53 51 4c 49 54  s = 0;.#if SQLIT
851e0 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
851f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
85200 74 65 78 3b 0a 23 65 6e 64 69 66 0a 23 69 66 6e  tex;.#endif.#ifn
85210 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
85220 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74 20 72  AUTOINIT.  int r
85230 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  c = sqlite3_init
85240 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20  ialize();.  if( 
85250 72 63 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23  rc ) return 0;.#
85260 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
85270 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 6d 75  _THREADSAFE.  mu
85280 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
85290 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
852a0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
852b0 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71  ER);.#endif.  sq
852c0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
852d0 72 28 6d 75 74 65 78 29 3b 0a 20 20 66 6f 72 28  r(mutex);.  for(
852e0 70 56 66 73 20 3d 20 76 66 73 4c 69 73 74 3b 20  pVfs = vfsList; 
852f0 70 56 66 73 3b 20 70 56 66 73 3d 70 56 66 73 2d  pVfs; pVfs=pVfs-
85300 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
85310 20 7a 56 66 73 3d 3d 30 20 29 20 62 72 65 61 6b   zVfs==0 ) break
85320 3b 0a 20 20 20 20 69 66 28 20 73 74 72 63 6d 70  ;.    if( strcmp
85330 28 7a 56 66 73 2c 20 70 56 66 73 2d 3e 7a 4e 61  (zVfs, pVfs->zNa
85340 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b 3b 0a  me)==0 ) break;.
85350 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
85360 74 65 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29  tex_leave(mutex)
85370 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 66 73 3b  ;.  return pVfs;
85380 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69 6e 6b  .}../*.** Unlink
85390 20 61 20 56 46 53 20 66 72 6f 6d 20 74 68 65 20   a VFS from the 
853a0 6c 69 6e 6b 65 64 20 6c 69 73 74 0a 2a 2f 0a 73  linked list.*/.s
853b0 74 61 74 69 63 20 76 6f 69 64 20 76 66 73 55 6e  tatic void vfsUn
853c0 6c 69 6e 6b 28 73 71 6c 69 74 65 33 5f 76 66 73  link(sqlite3_vfs
853d0 20 2a 70 56 66 73 29 7b 0a 20 20 61 73 73 65 72   *pVfs){.  asser
853e0 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
853f0 5f 68 65 6c 64 28 73 71 6c 69 74 65 33 4d 75 74  _held(sqlite3Mut
85400 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
85410 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
85420 45 52 29 29 20 29 3b 0a 20 20 69 66 28 20 70 56  ER)) );.  if( pV
85430 66 73 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20  fs==0 ){.    /* 
85440 4e 6f 2d 6f 70 20 2a 2f 0a 20 20 7d 65 6c 73 65  No-op */.  }else
85450 20 69 66 28 20 76 66 73 4c 69 73 74 3d 3d 70 56   if( vfsList==pV
85460 66 73 20 29 7b 0a 20 20 20 20 76 66 73 4c 69 73  fs ){.    vfsLis
85470 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b  t = pVfs->pNext;
85480 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 76 66 73  .  }else if( vfs
85490 4c 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  List ){.    sqli
854a0 74 65 33 5f 76 66 73 20 2a 70 20 3d 20 76 66 73  te3_vfs *p = vfs
854b0 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  List;.    while(
854c0 20 70 2d 3e 70 4e 65 78 74 20 26 26 20 70 2d 3e   p->pNext && p->
854d0 70 4e 65 78 74 21 3d 70 56 66 73 20 29 7b 0a 20  pNext!=pVfs ){. 
854e0 20 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78       p = p->pNex
854f0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
85500 20 70 2d 3e 70 4e 65 78 74 3d 3d 70 56 66 73 20   p->pNext==pVfs 
85510 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4e 65 78  ){.      p->pNex
85520 74 20 3d 20 70 56 66 73 2d 3e 70 4e 65 78 74 3b  t = pVfs->pNext;
85530 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
85540 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 56  .** Register a V
85550 46 53 20 77 69 74 68 20 74 68 65 20 73 79 73 74  FS with the syst
85560 65 6d 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c  em.  It is harml
85570 65 73 73 20 74 6f 20 72 65 67 69 73 74 65 72 20  ess to register 
85580 74 68 65 20 73 61 6d 65 0a 2a 2a 20 56 46 53 20  the same.** VFS 
85590 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 2e 20  multiple times. 
855a0 20 54 68 65 20 6e 65 77 20 56 46 53 20 62 65 63   The new VFS bec
855b0 6f 6d 65 73 20 74 68 65 20 64 65 66 61 75 6c 74  omes the default
855c0 20 69 66 20 6d 61 6b 65 44 66 6c 74 20 69 73 0a   if makeDflt is.
855d0 2a 2a 20 74 72 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ** true..*/.SQLI
855e0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
855f0 65 33 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28  e3_vfs_register(
85600 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
85610 73 2c 20 69 6e 74 20 6d 61 6b 65 44 66 6c 74 29  s, int makeDflt)
85620 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
85630 78 20 2a 6d 75 74 65 78 20 3d 20 30 3b 0a 23 69  x *mutex = 0;.#i
85640 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
85650 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 6e 74  T_AUTOINIT.  int
85660 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e   rc = sqlite3_in
85670 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66  itialize();.  if
85680 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
85690 3b 0a 23 65 6e 64 69 66 0a 20 20 6d 75 74 65 78  ;.#endif.  mutex
856a0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
856b0 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
856c0 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29  X_STATIC_MASTER)
856d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
856e0 78 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a  x_enter(mutex);.
856f0 20 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73    vfsUnlink(pVfs
85700 29 3b 0a 20 20 69 66 28 20 6d 61 6b 65 44 66 6c  );.  if( makeDfl
85710 74 20 7c 7c 20 76 66 73 4c 69 73 74 3d 3d 30 20  t || vfsList==0 
85720 29 7b 0a 20 20 20 20 70 56 66 73 2d 3e 70 4e 65  ){.    pVfs->pNe
85730 78 74 20 3d 20 76 66 73 4c 69 73 74 3b 0a 20 20  xt = vfsList;.  
85740 20 20 76 66 73 4c 69 73 74 20 3d 20 70 56 66 73    vfsList = pVfs
85750 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
85760 56 66 73 2d 3e 70 4e 65 78 74 20 3d 20 76 66 73  Vfs->pNext = vfs
85770 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  List->pNext;.   
85780 20 76 66 73 4c 69 73 74 2d 3e 70 4e 65 78 74 20   vfsList->pNext 
85790 3d 20 70 56 66 73 3b 0a 20 20 7d 0a 20 20 61 73  = pVfs;.  }.  as
857a0 73 65 72 74 28 76 66 73 4c 69 73 74 29 3b 0a 20  sert(vfsList);. 
857b0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
857c0 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
857d0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
857e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 72 65 67 69  .}../*.** Unregi
857f0 73 74 65 72 20 61 20 56 46 53 20 73 6f 20 74 68  ster a VFS so th
85800 61 74 20 69 74 20 69 73 20 6e 6f 20 6c 6f 6e 67  at it is no long
85810 65 72 20 61 63 63 65 73 73 69 62 6c 65 2e 0a 2a  er accessible..*
85820 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
85830 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75 6e 72   sqlite3_vfs_unr
85840 65 67 69 73 74 65 72 28 73 71 6c 69 74 65 33 5f  egister(sqlite3_
85850 76 66 73 20 2a 70 56 66 73 29 7b 0a 23 69 66 20  vfs *pVfs){.#if 
85860 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
85870 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  E.  sqlite3_mute
85880 78 20 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74  x *mutex = sqlit
85890 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
858a0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
858b0 5f 4d 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66  _MASTER);.#endif
858c0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
858d0 5f 65 6e 74 65 72 28 6d 75 74 65 78 29 3b 0a 20  _enter(mutex);. 
858e0 20 76 66 73 55 6e 6c 69 6e 6b 28 70 56 66 73 29   vfsUnlink(pVfs)
858f0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
85900 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
85910 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
85920 4f 4b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OK;.}../********
85930 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
85940 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
85950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85960 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85970 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
85980 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
85990 65 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a 2a 2a  e fault.c ******
859a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
859b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
859c0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
859d0 38 20 4a 61 6e 20 32 32 0a 2a 2a 0a 2a 2a 20 54  8 Jan 22.**.** T
859e0 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
859f0 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
85a00 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
85a10 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
85a20 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
85a30 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
85a40 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
85a50 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
85a60 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
85a70 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
85a80 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
85a90 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
85aa0 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
85ab0 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
85ac0 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
85ad0 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
85ae0 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
85af0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
85b30 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49  *******.**.** $I
85b40 64 3a 20 66 61 75 6c 74 2e 63 2c 76 20 31 2e 31  d: fault.c,v 1.1
85b50 31 20 32 30 30 38 2f 30 39 2f 30 32 20 30 30 3a  1 2008/09/02 00:
85b60 35 32 3a 35 32 20 64 72 68 20 45 78 70 20 24 0a  52:52 drh Exp $.
85b70 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  */../*.** This f
85b80 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64  ile contains cod
85b90 65 20 74 6f 20 73 75 70 70 6f 72 74 20 74 68 65  e to support the
85ba0 20 63 6f 6e 63 65 70 74 20 6f 66 20 22 62 65 6e   concept of "ben
85bb0 69 67 6e 22 20 0a 2a 2a 20 6d 61 6c 6c 6f 63 20  ign" .** malloc 
85bc0 66 61 69 6c 75 72 65 73 20 28 77 68 65 6e 20 74  failures (when t
85bd0 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 6f 72 20  he xMalloc() or 
85be0 78 52 65 61 6c 6c 6f 63 28 29 20 6d 65 74 68 6f  xRealloc() metho
85bf0 64 20 6f 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69  d of the.** sqli
85c00 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
85c10 73 74 72 75 63 74 75 72 65 20 66 61 69 6c 73 20  structure fails 
85c20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 62 6c  to allocate a bl
85c30 6f 63 6b 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a  ock of memory.**
85c40 20 61 6e 64 20 72 65 74 75 72 6e 73 20 30 29 2e   and returns 0).
85c50 20 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6d 61 6c   .**.** Most mal
85c60 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 61 72 65  loc failures are
85c70 20 6e 6f 6e 2d 62 65 6e 69 67 6e 2e 20 41 66 74   non-benign. Aft
85c80 65 72 20 74 68 65 79 20 6f 63 63 75 72 2c 20 53  er they occur, S
85c90 51 4c 69 74 65 0a 2a 2a 20 61 62 61 6e 64 6f 6e  QLite.** abandon
85ca0 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 6f 70  s the current op
85cb0 65 72 61 74 69 6f 6e 20 61 6e 64 20 72 65 74 75  eration and retu
85cc0 72 6e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rns an error cod
85cd0 65 20 28 75 73 75 61 6c 6c 79 0a 2a 2a 20 53 51  e (usually.** SQ
85ce0 4c 49 54 45 5f 4e 4f 4d 45 4d 29 20 74 6f 20 74  LITE_NOMEM) to t
85cf0 68 65 20 75 73 65 72 2e 20 48 6f 77 65 76 65 72  he user. However
85d00 2c 20 73 6f 6d 65 74 69 6d 65 73 20 61 20 66 61  , sometimes a fa
85d10 75 6c 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  ult is not neces
85d20 73 61 72 69 6c 79 0a 2a 2a 20 66 61 74 61 6c 2e  sarily.** fatal.
85d30 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66   For example, if
85d40 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20   a malloc fails 
85d50 77 68 69 6c 65 20 72 65 73 69 7a 69 6e 67 20 61  while resizing a
85d60 20 68 61 73 68 20 74 61 62 6c 65 2c 20 74 68 69   hash table, thi
85d70 73 20 0a 2a 2a 20 69 73 20 63 6f 6d 70 6c 65 74  s .** is complet
85d80 65 6c 79 20 72 65 63 6f 76 65 72 61 62 6c 65 20  ely recoverable 
85d90 73 69 6d 70 6c 79 20 62 79 20 6e 6f 74 20 63 61  simply by not ca
85da0 72 72 79 69 6e 67 20 6f 75 74 20 74 68 65 20 72  rrying out the r
85db0 65 73 69 7a 65 2e 20 54 68 65 20 0a 2a 2a 20 68  esize. The .** h
85dc0 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c 20 63  ash table will c
85dd0 6f 6e 74 69 6e 75 65 20 74 6f 20 66 75 6e 63 74  ontinue to funct
85de0 69 6f 6e 20 6e 6f 72 6d 61 6c 6c 79 2e 20 20 53  ion normally.  S
85df0 6f 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  o a malloc failu
85e00 72 65 20 0a 2a 2a 20 64 75 72 69 6e 67 20 61 20  re .** during a 
85e10 68 61 73 68 20 74 61 62 6c 65 20 72 65 73 69 7a  hash table resiz
85e20 65 20 69 73 20 61 20 62 65 6e 69 67 6e 20 66 61  e is a benign fa
85e30 75 6c 74 2e 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65  ult..*/...#ifnde
85e40 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
85e50 49 4c 54 49 4e 5f 54 45 53 54 0a 0a 2f 2a 0a 2a  ILTIN_TEST../*.*
85e60 2a 20 47 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  * Global variabl
85e70 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  es..*/.typedef s
85e80 74 72 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c  truct BenignMall
85e90 6f 63 48 6f 6f 6b 73 20 42 65 6e 69 67 6e 4d 61  ocHooks BenignMa
85ea0 6c 6c 6f 63 48 6f 6f 6b 73 3b 0a 73 74 61 74 69  llocHooks;.stati
85eb0 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
85ec0 75 63 74 20 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  uct BenignMalloc
85ed0 48 6f 6f 6b 73 20 7b 0a 20 20 76 6f 69 64 20 28  Hooks {.  void (
85ee0 2a 78 42 65 6e 69 67 6e 42 65 67 69 6e 29 28 76  *xBenignBegin)(v
85ef0 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 28 2a 78  oid);.  void (*x
85f00 42 65 6e 69 67 6e 45 6e 64 29 28 76 6f 69 64 29  BenignEnd)(void)
85f10 3b 0a 7d 20 73 71 6c 69 74 65 33 48 6f 6f 6b 73  ;.} sqlite3Hooks
85f20 20 3d 20 7b 20 30 2c 20 30 20 7d 3b 0a 0a 2f 2a   = { 0, 0 };../*
85f30 20 54 68 65 20 22 77 73 64 48 6f 6f 6b 73 22 20   The "wsdHooks" 
85f40 6d 61 63 72 6f 20 77 69 6c 6c 20 72 65 73 6f 6c  macro will resol
85f50 76 65 20 74 6f 20 74 68 65 20 61 70 70 72 6f 70  ve to the approp
85f60 72 69 61 74 65 20 42 65 6e 69 67 6e 4d 61 6c 6c  riate BenignMall
85f70 6f 63 48 6f 6f 6b 73 0a 2a 2a 20 73 74 72 75 63  ocHooks.** struc
85f80 74 75 72 65 2e 20 20 49 66 20 77 72 69 74 61 62  ture.  If writab
85f90 6c 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69  le static data i
85fa0 73 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e  s unsupported on
85fb0 20 74 68 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20   the target,.** 
85fc0 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61 74  we have to locat
85fd0 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63 74  e the state vect
85fe0 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20  or at run-time. 
85ff0 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d   In the more com
86000 6d 6f 6e 0a 2a 2a 20 63 61 73 65 20 77 68 65 72  mon.** case wher
86010 65 20 77 72 69 74 61 62 6c 65 20 73 74 61 74 69  e writable stati
86020 63 20 64 61 74 61 20 69 73 20 73 75 70 70 6f 72  c data is suppor
86030 74 65 64 2c 20 77 73 64 48 6f 6f 6b 73 20 63 61  ted, wsdHooks ca
86040 6e 20 72 65 66 65 72 20 64 69 72 65 63 74 6c 79  n refer directly
86050 0a 2a 2a 20 74 6f 20 74 68 65 20 22 73 71 6c 69  .** to the "sqli
86060 74 65 33 48 6f 6f 6b 73 22 20 73 74 61 74 65 20  te3Hooks" state 
86070 76 65 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20  vector declared 
86080 61 62 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66  above..*/.#ifdef
86090 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44   SQLITE_OMIT_WSD
860a0 0a 23 20 64 65 66 69 6e 65 20 77 73 64 48 6f 6f  .# define wsdHoo
860b0 6b 73 49 6e 69 74 20 5c 0a 20 20 42 65 6e 69 67  ksInit \.  Benig
860c0 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 20 2a 78 20  nMallocHooks *x 
860d0 3d 20 26 47 4c 4f 42 41 4c 28 42 65 6e 69 67 6e  = &GLOBAL(Benign
860e0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 2c 73 71 6c 69  MallocHooks,sqli
860f0 74 65 33 48 6f 6f 6b 73 29 0a 23 20 64 65 66 69  te3Hooks).# defi
86100 6e 65 20 77 73 64 48 6f 6f 6b 73 20 78 5b 30 5d  ne wsdHooks x[0]
86110 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20  .#else.# define 
86120 77 73 64 48 6f 6f 6b 73 49 6e 69 74 0a 23 20 64  wsdHooksInit.# d
86130 65 66 69 6e 65 20 77 73 64 48 6f 6f 6b 73 20 73  efine wsdHooks s
86140 71 6c 69 74 65 33 48 6f 6f 6b 73 0a 23 65 6e 64  qlite3Hooks.#end
86150 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  if.../*.** Regis
86160 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20 63 61 6c  ter hooks to cal
86170 6c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 65  l when sqlite3Be
86180 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
86190 29 20 61 6e 64 0a 2a 2a 20 73 71 6c 69 74 65 33  ) and.** sqlite3
861a0 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  EndBenignMalloc(
861b0 29 20 61 72 65 20 63 61 6c 6c 65 64 2c 20 72 65  ) are called, re
861c0 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 53  spectively..*/.S
861d0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
861e0 69 64 20 73 71 6c 69 74 65 33 42 65 6e 69 67 6e  id sqlite3Benign
861f0 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 0a 20 20 76  MallocHooks(.  v
86200 6f 69 64 20 28 2a 78 42 65 6e 69 67 6e 42 65 67  oid (*xBenignBeg
86210 69 6e 29 28 76 6f 69 64 29 2c 0a 20 20 76 6f 69  in)(void),.  voi
86220 64 20 28 2a 78 42 65 6e 69 67 6e 45 6e 64 29 28  d (*xBenignEnd)(
86230 76 6f 69 64 29 0a 29 7b 0a 20 20 77 73 64 48 6f  void).){.  wsdHo
86240 6f 6b 73 49 6e 69 74 3b 0a 20 20 77 73 64 48 6f  oksInit;.  wsdHo
86250 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69 6e  oks.xBenignBegin
86260 20 3d 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b   = xBenignBegin;
86270 0a 20 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  .  wsdHooks.xBen
86280 69 67 6e 45 6e 64 20 3d 20 78 42 65 6e 69 67 6e  ignEnd = xBenign
86290 45 6e 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  End;.}../*.** Th
862a0 69 73 20 28 73 71 6c 69 74 65 33 45 6e 64 42 65  is (sqlite3EndBe
862b0 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 29 20 69 73  nignMalloc()) is
862c0 20 63 61 6c 6c 65 64 20 62 79 20 53 51 4c 69 74   called by SQLit
862d0 65 20 63 6f 64 65 20 74 6f 20 69 6e 64 69 63 61  e code to indica
862e0 74 65 20 74 68 61 74 0a 2a 2a 20 73 75 62 73 65  te that.** subse
862f0 71 75 65 6e 74 20 6d 61 6c 6c 6f 63 20 66 61 69  quent malloc fai
86300 6c 75 72 65 73 20 61 72 65 20 62 65 6e 69 67 6e  lures are benign
86310 2e 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69  . A call to sqli
86320 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
86330 6f 63 28 29 0a 2a 2a 20 69 6e 64 69 63 61 74 65  oc().** indicate
86340 73 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e  s that subsequen
86350 74 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  t malloc failure
86360 73 20 61 72 65 20 6e 6f 6e 2d 62 65 6e 69 67 6e  s are non-benign
86370 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
86380 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
86390 42 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f  BeginBenignMallo
863a0 63 28 76 6f 69 64 29 7b 0a 20 20 77 73 64 48 6f  c(void){.  wsdHo
863b0 6f 6b 73 49 6e 69 74 3b 0a 20 20 69 66 28 20 77  oksInit;.  if( w
863c0 73 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42  sdHooks.xBenignB
863d0 65 67 69 6e 20 29 7b 0a 20 20 20 20 77 73 64 48  egin ){.    wsdH
863e0 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 42 65 67 69  ooks.xBenignBegi
863f0 6e 28 29 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54  n();.  }.}.SQLIT
86400 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
86410 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
86420 61 6c 6c 6f 63 28 76 6f 69 64 29 7b 0a 20 20 77  alloc(void){.  w
86430 73 64 48 6f 6f 6b 73 49 6e 69 74 3b 0a 20 20 69  sdHooksInit;.  i
86440 66 28 20 77 73 64 48 6f 6f 6b 73 2e 78 42 65 6e  f( wsdHooks.xBen
86450 69 67 6e 45 6e 64 20 29 7b 0a 20 20 20 20 77 73  ignEnd ){.    ws
86460 64 48 6f 6f 6b 73 2e 78 42 65 6e 69 67 6e 45 6e  dHooks.xBenignEn
86470 64 28 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64  d();.  }.}..#end
86480 69 66 20 20 20 2f 2a 20 23 69 66 6e 64 65 66 20  if   /* #ifndef 
86490 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
864a0 54 49 4e 5f 54 45 53 54 20 2a 2f 0a 0a 2f 2a 2a  TIN_TEST */../**
864b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
864c0 20 6f 66 20 66 61 75 6c 74 2e 63 20 2a 2a 2a 2a   of fault.c ****
864d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
864e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
864f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
86500 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
86510 69 6e 20 66 69 6c 65 20 6d 65 6d 30 2e 63 20 2a  in file mem0.c *
86520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86530 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
86550 2a 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20  ** 2008 October 
86560 32 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  28.**.** The aut
86570 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
86580 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
86590 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
865a0 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
865b0 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
865c0 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
865d0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
865e0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
865f0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
86600 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
86610 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
86620 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
86630 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
86640 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
86650 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
86660 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
86670 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
86680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
866a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
866b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
866c0 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  *.**.** This fil
866d0 65 20 63 6f 6e 74 61 69 6e 73 20 61 20 6e 6f 2d  e contains a no-
866e0 6f 70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  op memory alloca
866f0 74 69 6f 6e 20 64 72 69 76 65 72 73 20 66 6f 72  tion drivers for
86700 20 75 73 65 20 77 68 65 6e 0a 2a 2a 20 53 51 4c   use when.** SQL
86710 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43 20  ITE_ZERO_MALLOC 
86720 69 73 20 64 65 66 69 6e 65 64 2e 20 20 54 68 65  is defined.  The
86730 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72 69 76   allocation driv
86740 65 72 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 0a  ers implemented.
86750 2a 2a 20 68 65 72 65 20 61 6c 77 61 79 73 20 66  ** here always f
86760 61 69 6c 2e 20 20 53 51 4c 69 74 65 20 77 69 6c  ail.  SQLite wil
86770 6c 20 6e 6f 74 20 6f 70 65 72 61 74 65 20 77 69  l not operate wi
86780 74 68 20 74 68 65 73 65 20 64 72 69 76 65 72 73  th these drivers
86790 2e 20 20 54 68 65 73 65 0a 2a 2a 20 61 72 65 20  .  These.** are 
867a0 6d 65 72 65 6c 79 20 70 6c 61 63 65 68 6f 6c 64  merely placehold
867b0 65 72 73 2e 20 20 52 65 61 6c 20 64 72 69 76 65  ers.  Real drive
867c0 72 73 20 6d 75 73 74 20 62 65 20 73 75 62 73 74  rs must be subst
867d0 69 74 75 74 65 64 20 75 73 69 6e 67 0a 2a 2a 20  ituted using.** 
867e0 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
867f0 20 62 65 66 6f 72 65 20 53 51 4c 69 74 65 20 77   before SQLite w
86800 69 6c 6c 20 6f 70 65 72 61 74 65 2e 0a 2a 2a 0a  ill operate..**.
86810 2a 2a 20 24 49 64 3a 20 6d 65 6d 30 2e 63 2c 76  ** $Id: mem0.c,v
86820 20 31 2e 31 20 32 30 30 38 2f 31 30 2f 32 38 20   1.1 2008/10/28 
86830 31 38 3a 35 38 3a 32 30 20 64 72 68 20 45 78 70  18:58:20 drh Exp
86840 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   $.*/../*.** Thi
86850 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
86860 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f   memory allocato
86870 72 20 69 73 20 74 68 65 20 64 65 66 61 75 6c 74  r is the default
86880 2e 20 20 49 74 20 69 73 0a 2a 2a 20 75 73 65 64  .  It is.** used
86890 20 77 68 65 6e 20 6e 6f 20 6f 74 68 65 72 20 6d   when no other m
868a0 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 6f 72 20  emory allocator 
868b0 69 73 20 73 70 65 63 69 66 69 65 64 20 75 73 69  is specified usi
868c0 6e 67 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 0a  ng compile-time.
868d0 2a 2a 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a 23 69  ** macros..*/.#i
868e0 66 64 65 66 20 53 51 4c 49 54 45 5f 5a 45 52 4f  fdef SQLITE_ZERO
868f0 5f 4d 41 4c 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4e  _MALLOC../*.** N
86900 6f 2d 6f 70 20 76 65 72 73 69 6f 6e 73 20 6f 66  o-op versions of
86910 20 61 6c 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   all memory allo
86920 63 61 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 0a  cation routines.
86930 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
86940 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63  sqlite3MemMalloc
86950 28 69 6e 74 20 6e 42 79 74 65 29 7b 20 72 65 74  (int nByte){ ret
86960 75 72 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20  urn 0; }.static 
86970 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 46  void sqlite3MemF
86980 72 65 65 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  ree(void *pPrior
86990 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
869a0 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
869b0 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64  3MemRealloc(void
869c0 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
869d0 79 74 65 29 7b 20 72 65 74 75 72 6e 20 30 3b 20  yte){ return 0; 
869e0 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  }.static int sql
869f0 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f 69 64  ite3MemSize(void
86a00 20 2a 70 50 72 69 6f 72 29 7b 20 72 65 74 75 72   *pPrior){ retur
86a10 6e 20 30 3b 20 7d 0a 73 74 61 74 69 63 20 69 6e  n 0; }.static in
86a20 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e  t sqlite3MemRoun
86a30 64 75 70 28 69 6e 74 20 6e 29 7b 20 72 65 74 75  dup(int n){ retu
86a40 72 6e 20 6e 3b 20 7d 0a 73 74 61 74 69 63 20 69  rn n; }.static i
86a50 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69  nt sqlite3MemIni
86a60 74 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  t(void *NotUsed)
86a70 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  { return SQLITE_
86a80 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69  OK; }.static voi
86a90 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75 74  d sqlite3MemShut
86aa0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
86ab0 65 64 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 0a  ed){ return; }..
86ac0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
86ad0 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72  ne is the only r
86ae0 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
86af0 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61  ile with externa
86b00 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a  l linkage..**.**
86b10 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
86b20 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
86b30 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
86b40 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
86b50 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
86b60 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
86b70 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
86b80 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
86b90 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
86ba0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
86bb0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
86bc0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
86bd0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
86be0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
86bf0 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
86c00 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c    sqlite3MemMall
86c10 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
86c20 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71  MemFree,.     sq
86c30 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c  lite3MemRealloc,
86c40 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
86c50 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74  Size,.     sqlit
86c60 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20  e3MemRoundup,.  
86c70 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
86c80 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  t,.     sqlite3M
86c90 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  emShutdown,.    
86ca0 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65   0.  };.  sqlite
86cb0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
86cc0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26  CONFIG_MALLOC, &
86cd0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b  defaultMethods);
86ce0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
86cf0 4c 49 54 45 5f 5a 45 52 4f 5f 4d 41 4c 4c 4f 43  LITE_ZERO_MALLOC
86d00 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
86d10 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65 6d 30  **** End of mem0
86d20 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
86d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d50 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
86d60 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
86d70 6d 65 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  mem1.c *********
86d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86da0 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20  ***/./*.** 2007 
86db0 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a 20  August 14.**.** 
86dc0 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
86dd0 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
86de0 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
86df0 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
86e00 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
86e10 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
86e20 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
86e30 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
86e40 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
86e50 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
86e60 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
86e70 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
86e80 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
86e90 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
86ea0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
86eb0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
86ec0 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
86ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
86f10 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
86f20 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
86f30 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f  s low-level memo
86f40 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 64 72  ry allocation dr
86f50 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e 0a 2a  ivers for when.*
86f60 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 75 73  * SQLite will us
86f70 65 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43  e the standard C
86f80 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f 63 2f  -library malloc/
86f90 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69 6e 74  realloc/free int
86fa0 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f 62 74  erface.** to obt
86fb0 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79 20 69  ain the memory i
86fc0 74 20 6e 65 65 64 73 2e 0a 2a 2a 0a 2a 2a 20 54  t needs..**.** T
86fd0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
86fe0 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
86ff0 73 20 6f 66 20 74 68 65 20 6c 6f 77 2d 6c 65 76  s of the low-lev
87000 65 6c 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  el memory alloca
87010 74 69 6f 6e 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  tion.** routines
87020 20 73 70 65 63 69 66 69 65 64 20 69 6e 20 74 68   specified in th
87030 65 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65  e sqlite3_mem_me
87040 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a  thods object..**
87050 0a 2a 2a 20 24 49 64 3a 20 6d 65 6d 31 2e 63 2c  .** $Id: mem1.c,
87060 76 20 31 2e 32 38 20 32 30 30 38 2f 31 31 2f 31  v 1.28 2008/11/1
87070 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65  9 09:05:27 danie
87080 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
87090 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73  ./*.** This vers
870a0 69 6f 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72  ion of the memor
870b0 79 20 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 74  y allocator is t
870c0 68 65 20 64 65 66 61 75 6c 74 2e 20 20 49 74 20  he default.  It 
870d0 69 73 0a 2a 2a 20 75 73 65 64 20 77 68 65 6e 20  is.** used when 
870e0 6e 6f 20 6f 74 68 65 72 20 6d 65 6d 6f 72 79 20  no other memory 
870f0 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 73 70 65  allocator is spe
87100 63 69 66 69 65 64 20 75 73 69 6e 67 20 63 6f 6d  cified using com
87110 70 69 6c 65 2d 74 69 6d 65 0a 2a 2a 20 6d 61 63  pile-time.** mac
87120 72 6f 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ros..*/.#ifdef S
87130 51 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c  QLITE_SYSTEM_MAL
87140 4c 4f 43 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20  LOC../*.** Like 
87150 6d 61 6c 6c 6f 63 28 29 2c 20 62 75 74 20 72 65  malloc(), but re
87160 6d 65 6d 62 65 72 20 74 68 65 20 73 69 7a 65 20  member the size 
87170 6f 66 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  of the allocatio
87180 6e 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20  n.** so that we 
87190 63 61 6e 20 66 69 6e 64 20 69 74 20 6c 61 74 65  can find it late
871a0 72 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 4d  r using sqlite3M
871b0 65 6d 53 69 7a 65 28 29 2e 0a 2a 2a 0a 2a 2a 20  emSize()..**.** 
871c0 46 6f 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76  For this low-lev
871d0 65 6c 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61  el routine, we a
871e0 72 65 20 67 75 61 72 61 6e 74 65 65 64 20 74 68  re guaranteed th
871f0 61 74 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75  at nByte>0 becau
87200 73 65 0a 2a 2a 20 63 61 73 65 73 20 6f 66 20 6e  se.** cases of n
87210 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 62 65 20  Byte<=0 will be 
87220 69 6e 74 65 72 63 65 70 74 65 64 20 61 6e 64 20  intercepted and 
87230 64 65 61 6c 74 20 77 69 74 68 20 62 79 20 68 69  dealt with by hi
87240 67 68 65 72 20 6c 65 76 65 6c 0a 2a 2a 20 72 6f  gher level.** ro
87250 75 74 69 6e 65 73 2e 0a 2a 2f 0a 73 74 61 74 69  utines..*/.stati
87260 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  c void *sqlite3M
87270 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  emMalloc(int nBy
87280 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69  te){.  sqlite3_i
87290 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65 72  nt64 *p;.  asser
872a0 74 28 20 6e 42 79 74 65 3e 30 20 29 3b 0a 20 20  t( nByte>0 );.  
872b0 6e 42 79 74 65 20 3d 20 28 6e 42 79 74 65 2b 37  nByte = (nByte+7
872c0 29 26 7e 37 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c  )&~7;.  p = mall
872d0 6f 63 28 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20  oc( nByte+8 );. 
872e0 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 5b   if( p ){.    p[
872f0 30 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  0] = nByte;.    
87300 70 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  p++;.  }.  retur
87310 6e 20 28 76 6f 69 64 20 2a 29 70 3b 0a 7d 0a 0a  n (void *)p;.}..
87320 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 66 72 65 65 28  /*.** Like free(
87330 29 20 62 75 74 20 77 6f 72 6b 73 20 66 6f 72 20  ) but works for 
87340 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 6f 62 74 61  allocations obta
87350 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
87360 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 0a 2a 2a 20  3MemMalloc().** 
87370 6f 72 20 73 71 6c 69 74 65 33 4d 65 6d 52 65 61  or sqlite3MemRea
87380 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a 2a 20 46 6f  lloc()..**.** Fo
87390 72 20 74 68 69 73 20 6c 6f 77 2d 6c 65 76 65 6c  r this low-level
873a0 20 72 6f 75 74 69 6e 65 2c 20 77 65 20 61 6c 72   routine, we alr
873b0 65 61 64 79 20 6b 6e 6f 77 20 74 68 61 74 20 70  eady know that p
873c0 50 72 69 6f 72 21 3d 30 20 73 69 6e 63 65 0a 2a  Prior!=0 since.*
873d0 2a 20 63 61 73 65 73 20 77 68 65 72 65 20 70 50  * cases where pP
873e0 72 69 6f 72 3d 3d 30 20 77 69 6c 6c 20 68 61 76  rior==0 will hav
873f0 65 20 62 65 65 6e 20 69 6e 74 65 63 65 70 74 65  e been intecepte
87400 64 20 61 6e 64 20 64 65 61 6c 74 20 77 69 74 68  d and dealt with
87410 0a 2a 2a 20 62 79 20 68 69 67 68 65 72 2d 6c 65  .** by higher-le
87420 76 65 6c 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f  vel routines..*/
87430 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c  .static void sql
87440 69 74 65 33 4d 65 6d 46 72 65 65 28 76 6f 69 64  ite3MemFree(void
87450 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73 71 6c   *pPrior){.  sql
87460 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 20 3d 20  ite3_int64 *p = 
87470 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 2a 29  (sqlite3_int64*)
87480 70 50 72 69 6f 72 3b 0a 20 20 61 73 73 65 72 74  pPrior;.  assert
87490 28 20 70 50 72 69 6f 72 21 3d 30 20 29 3b 0a 20  ( pPrior!=0 );. 
874a0 20 70 2d 2d 3b 0a 20 20 66 72 65 65 28 70 29 3b   p--;.  free(p);
874b0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6b 65 20 72  .}../*.** Like r
874c0 65 61 6c 6c 6f 63 28 29 2e 20 20 52 65 73 69 7a  ealloc().  Resiz
874d0 65 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  e an allocation 
874e0 70 72 65 76 69 6f 75 73 6c 79 20 6f 62 74 61 69  previously obtai
874f0 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 71 6c 69  ned from.** sqli
87500 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 29 2e 0a  te3MemMalloc()..
87510 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 69 73 20 6c  **.** For this l
87520 6f 77 2d 6c 65 76 65 6c 20 69 6e 74 65 72 66 61  ow-level interfa
87530 63 65 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  ce, we know that
87540 20 70 50 72 69 6f 72 21 3d 30 2e 20 20 43 61 73   pPrior!=0.  Cas
87550 65 73 20 77 68 65 72 65 0a 2a 2a 20 70 50 72 69  es where.** pPri
87560 6f 72 3d 3d 30 20 77 68 69 6c 65 20 68 61 76 65  or==0 while have
87570 20 62 65 65 6e 20 69 6e 74 65 72 63 65 70 74 65   been intercepte
87580 64 20 62 79 20 68 69 67 68 65 72 2d 6c 65 76 65  d by higher-leve
87590 6c 20 72 6f 75 74 69 6e 65 20 61 6e 64 0a 2a 2a  l routine and.**
875a0 20 72 65 64 69 72 65 63 74 65 64 20 74 6f 20 78   redirected to x
875b0 4d 61 6c 6c 6f 63 2e 20 20 53 69 6d 69 6c 61 72  Malloc.  Similar
875c0 6c 79 2c 20 77 65 20 6b 6e 6f 77 20 74 68 61 74  ly, we know that
875d0 20 6e 42 79 74 65 3e 30 20 62 65 63 61 75 73 65   nByte>0 because
875e0 73 0a 2a 2a 20 63 61 73 65 73 20 77 68 65 72 65  s.** cases where
875f0 20 6e 42 79 74 65 3c 3d 30 20 77 69 6c 6c 20 68   nByte<=0 will h
87600 61 76 65 20 62 65 65 6e 20 69 6e 74 65 72 63 65  ave been interce
87610 70 74 65 64 20 62 79 20 68 69 67 68 65 72 2d 6c  pted by higher-l
87620 65 76 65 6c 0a 2a 2a 20 72 6f 75 74 69 6e 65 73  evel.** routines
87630 20 61 6e 64 20 72 65 64 69 72 65 63 74 65 64 20   and redirected 
87640 74 6f 20 78 46 72 65 65 2e 0a 2a 2f 0a 73 74 61  to xFree..*/.sta
87650 74 69 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  tic void *sqlite
87660 33 4d 65 6d 52 65 61 6c 6c 6f 63 28 76 6f 69 64  3MemRealloc(void
87670 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74 20 6e 42   *pPrior, int nB
87680 79 74 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  yte){.  sqlite3_
87690 69 6e 74 36 34 20 2a 70 20 3d 20 28 73 71 6c 69  int64 *p = (sqli
876a0 74 65 33 5f 69 6e 74 36 34 2a 29 70 50 72 69 6f  te3_int64*)pPrio
876b0 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  r;.  assert( pPr
876c0 69 6f 72 21 3d 30 20 26 26 20 6e 42 79 74 65 3e  ior!=0 && nByte>
876d0 30 20 29 3b 0a 20 20 6e 42 79 74 65 20 3d 20 28  0 );.  nByte = (
876e0 6e 42 79 74 65 2b 37 29 26 7e 37 3b 0a 20 20 70  nByte+7)&~7;.  p
876f0 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
87700 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d  4*)pPrior;.  p--
87710 3b 0a 20 20 70 20 3d 20 72 65 61 6c 6c 6f 63 28  ;.  p = realloc(
87720 70 2c 20 6e 42 79 74 65 2b 38 20 29 3b 0a 20 20  p, nByte+8 );.  
87730 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 5b 30  if( p ){.    p[0
87740 5d 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20 70  ] = nByte;.    p
87750 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ++;.  }.  return
87760 20 28 76 6f 69 64 2a 29 70 3b 0a 7d 0a 0a 2f 2a   (void*)p;.}../*
87770 0a 2a 2a 20 52 65 70 6f 72 74 20 74 68 65 20 61  .** Report the a
87780 6c 6c 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66  llocated size of
87790 20 61 20 70 72 69 6f 72 20 72 65 74 75 72 6e 20   a prior return 
877a0 66 72 6f 6d 20 78 4d 61 6c 6c 6f 63 28 29 0a 2a  from xMalloc().*
877b0 2a 20 6f 72 20 78 52 65 61 6c 6c 6f 63 28 29 2e  * or xRealloc().
877c0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
877d0 71 6c 69 74 65 33 4d 65 6d 53 69 7a 65 28 76 6f  qlite3MemSize(vo
877e0 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20 73  id *pPrior){.  s
877f0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a 70 3b  qlite3_int64 *p;
87800 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30  .  if( pPrior==0
87810 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
87820 20 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36   = (sqlite3_int6
87830 34 2a 29 70 50 72 69 6f 72 3b 0a 20 20 70 2d 2d  4*)pPrior;.  p--
87840 3b 0a 20 20 72 65 74 75 72 6e 20 70 5b 30 5d 3b  ;.  return p[0];
87850 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20  .}../*.** Round 
87860 75 70 20 61 20 72 65 71 75 65 73 74 20 73 69 7a  up a request siz
87870 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 76 61  e to the next va
87880 6c 69 64 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  lid allocation s
87890 69 7a 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ize..*/.static i
878a0 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75  nt sqlite3MemRou
878b0 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72  ndup(int n){.  r
878c0 65 74 75 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37  eturn (n+7) & ~7
878d0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
878e0 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
878f0 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
87900 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69 74 28   sqlite3MemInit(
87910 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a  void *NotUsed){.
87920 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
87930 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72  ER(NotUsed);.  r
87940 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
87950 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
87960 69 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75  ialize this modu
87970 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
87980 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 53 68 75  id sqlite3MemShu
87990 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55  tdown(void *NotU
879a0 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  sed){.  UNUSED_P
879b0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
879c0 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a  );.  return;.}..
879d0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
879e0 6e 65 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72  ne is the only r
879f0 6f 75 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66  outine in this f
87a00 69 6c 65 20 77 69 74 68 20 65 78 74 65 72 6e 61  ile with externa
87a10 6c 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a 2a 2a  l linkage..**.**
87a20 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20 6c 6f   Populate the lo
87a30 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20 61  w-level memory a
87a40 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
87a50 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e 0a 2a  on pointers in.*
87a60 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
87a70 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70 6f 69  onfig.m with poi
87a80 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72 6f 75  nters to the rou
87a90 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69  tines in this fi
87aa0 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
87ab0 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
87ac0 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
87ad0 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20  void){.  static 
87ae0 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 65  const sqlite3_me
87af0 6d 5f 6d 65 74 68 6f 64 73 20 64 65 66 61 75 6c  m_methods defaul
87b00 74 4d 65 74 68 6f 64 73 20 3d 20 7b 0a 20 20 20  tMethods = {.   
87b10 20 20 73 71 6c 69 74 65 33 4d 65 6d 4d 61 6c 6c    sqlite3MemMall
87b20 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  oc,.     sqlite3
87b30 4d 65 6d 46 72 65 65 2c 0a 20 20 20 20 20 73 71  MemFree,.     sq
87b40 6c 69 74 65 33 4d 65 6d 52 65 61 6c 6c 6f 63 2c  lite3MemRealloc,
87b50 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d  .     sqlite3Mem
87b60 53 69 7a 65 2c 0a 20 20 20 20 20 73 71 6c 69 74  Size,.     sqlit
87b70 65 33 4d 65 6d 52 6f 75 6e 64 75 70 2c 0a 20 20  e3MemRoundup,.  
87b80 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 49 6e 69     sqlite3MemIni
87b90 74 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d  t,.     sqlite3M
87ba0 65 6d 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20 20  emShutdown,.    
87bb0 20 30 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74 65   0.  };.  sqlite
87bc0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
87bd0 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43 2c 20 26  CONFIG_MALLOC, &
87be0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29 3b  defaultMethods);
87bf0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
87c00 4c 49 54 45 5f 53 59 53 54 45 4d 5f 4d 41 4c 4c  LITE_SYSTEM_MALL
87c10 4f 43 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  OC */../********
87c20 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
87c30 6d 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m1.c ***********
87c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87c60 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
87c70 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
87c80 65 20 6d 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a  e mem2.c *******
87c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87ca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87cb0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
87cc0 37 20 41 75 67 75 73 74 20 31 35 0a 2a 2a 0a 2a  7 August 15.**.*
87cd0 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
87ce0 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
87cf0 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
87d00 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
87d10 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
87d20 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
87d30 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
87d40 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
87d50 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
87d60 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
87d70 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
87d80 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
87d90 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
87da0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
87db0 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
87dc0 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
87dd0 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
87de0 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
87df0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
87e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
87e30 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
87e40 69 6e 73 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  ins low-level me
87e50 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
87e60 64 72 69 76 65 72 73 20 66 6f 72 20 77 68 65 6e  drivers for when
87e70 0a 2a 2a 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  .** SQLite will 
87e80 75 73 65 20 74 68 65 20 73 74 61 6e 64 61 72 64  use the standard
87e90 20 43 2d 6c 69 62 72 61 72 79 20 6d 61 6c 6c 6f   C-library mallo
87ea0 63 2f 72 65 61 6c 6c 6f 63 2f 66 72 65 65 20 69  c/realloc/free i
87eb0 6e 74 65 72 66 61 63 65 0a 2a 2a 20 74 6f 20 6f  nterface.** to o
87ec0 62 74 61 69 6e 20 74 68 65 20 6d 65 6d 6f 72 79  btain the memory
87ed0 20 69 74 20 6e 65 65 64 73 20 77 68 69 6c 65 20   it needs while 
87ee0 61 64 64 69 6e 67 20 6c 6f 74 73 20 6f 66 20 61  adding lots of a
87ef0 64 64 69 74 69 6f 6e 61 6c 20 64 65 62 75 67 67  dditional debugg
87f00 69 6e 67 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69  ing.** informati
87f10 6f 6e 20 74 6f 20 65 61 63 68 20 61 6c 6c 6f 63  on to each alloc
87f20 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74  ation in order t
87f30 6f 20 68 65 6c 70 20 64 65 74 65 63 74 20 61 6e  o help detect an
87f40 64 20 66 69 78 20 6d 65 6d 6f 72 79 0a 2a 2a 20  d fix memory.** 
87f50 6c 65 61 6b 73 20 61 6e 64 20 6d 65 6d 6f 72 79  leaks and memory
87f60 20 75 73 61 67 65 20 65 72 72 6f 72 73 2e 0a 2a   usage errors..*
87f70 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
87f80 6f 6e 74 61 69 6e 73 20 69 6d 70 6c 65 6d 65 6e  ontains implemen
87f90 74 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 6c  tations of the l
87fa0 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79 20  ow-level memory 
87fb0 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2a 20 72 6f  allocation.** ro
87fc0 75 74 69 6e 65 73 20 73 70 65 63 69 66 69 65 64  utines specified
87fd0 20 69 6e 20 74 68 65 20 73 71 6c 69 74 65 33 5f   in the sqlite3_
87fe0 6d 65 6d 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  mem_methods obje
87ff0 63 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d  ct..**.** $Id: m
88000 65 6d 32 2e 63 2c 76 20 31 2e 34 30 20 32 30 30  em2.c,v 1.40 200
88010 38 2f 31 30 2f 32 38 20 31 38 3a 35 38 3a 32 30  8/10/28 18:58:20
88020 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f   drh Exp $.*/../
88030 2a 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f  *.** This versio
88040 6e 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20  n of the memory 
88050 61 6c 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65  allocator is use
88060 64 20 6f 6e 6c 79 20 69 66 20 74 68 65 0a 2a 2a  d only if the.**
88070 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45 42 55 47   SQLITE_MEMDEBUG
88080 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
88090 64 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  d.*/.#ifdef SQLI
880a0 54 45 5f 4d 45 4d 44 45 42 55 47 0a 0a 2f 2a 0a  TE_MEMDEBUG../*.
880b0 2a 2a 20 54 68 65 20 62 61 63 6b 74 72 61 63 65  ** The backtrace
880c0 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 20 69   functionality i
880d0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
880e0 20 77 69 74 68 20 47 4c 49 42 43 0a 2a 2f 0a 23   with GLIBC.*/.#
880f0 69 66 64 65 66 20 5f 5f 47 4c 49 42 43 5f 5f 0a  ifdef __GLIBC__.
88100 20 20 65 78 74 65 72 6e 20 69 6e 74 20 62 61 63    extern int bac
88110 6b 74 72 61 63 65 28 76 6f 69 64 2a 2a 2c 69 6e  ktrace(void**,in
88120 74 29 3b 0a 20 20 65 78 74 65 72 6e 20 76 6f 69  t);.  extern voi
88130 64 20 62 61 63 6b 74 72 61 63 65 5f 73 79 6d 62  d backtrace_symb
88140 6f 6c 73 5f 66 64 28 76 6f 69 64 2a 63 6f 6e 73  ols_fd(void*cons
88150 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b 0a 23 65 6c  t*,int,int);.#el
88160 73 65 0a 23 20 64 65 66 69 6e 65 20 62 61 63 6b  se.# define back
88170 74 72 61 63 65 28 41 2c 42 29 20 31 0a 23 20 64  trace(A,B) 1.# d
88180 65 66 69 6e 65 20 62 61 63 6b 74 72 61 63 65 5f  efine backtrace_
88190 73 79 6d 62 6f 6c 73 5f 66 64 28 41 2c 42 2c 43  symbols_fd(A,B,C
881a0 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
881b0 45 61 63 68 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  Each memory allo
881c0 63 61 74 69 6f 6e 20 6c 6f 6f 6b 73 20 6c 69 6b  cation looks lik
881d0 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 2d  e this:.**.**  -
881e0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
881f0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
88200 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
88210 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
88220 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 7c 20 54 69  -------.**  | Ti
88230 74 6c 65 20 7c 20 20 62 61 63 6b 74 72 61 63 65  tle |  backtrace
88240 20 70 6f 69 6e 74 65 72 73 20 7c 20 20 4d 65 6d   pointers |  Mem
88250 42 6c 6f 63 6b 48 64 72 20 7c 20 20 61 6c 6c 6f  BlockHdr |  allo
88260 63 61 74 69 6f 6e 20 7c 20 20 45 6e 64 47 75 61  cation |  EndGua
88270 72 64 20 7c 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  rd |.**  -------
88280 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
88290 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
882a0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
882b0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
882c0 2d 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 70 70 6c  -.**.** The appl
882d0 69 63 61 74 69 6f 6e 20 63 6f 64 65 20 73 65 65  ication code see
882e0 73 20 6f 6e 6c 79 20 61 20 70 6f 69 6e 74 65 72  s only a pointer
882f0 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74 69   to the allocati
88300 6f 6e 2e 20 20 57 65 20 68 61 76 65 0a 2a 2a 20  on.  We have.** 
88310 74 6f 20 62 61 63 6b 20 75 70 20 66 72 6f 6d 20  to back up from 
88320 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70  the allocation p
88330 6f 69 6e 74 65 72 20 74 6f 20 66 69 6e 64 20 74  ointer to find t
88340 68 65 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 20  he MemBlockHdr. 
88350 20 54 68 65 0a 2a 2a 20 4d 65 6d 42 6c 6f 63 6b   The.** MemBlock
88360 48 64 72 20 74 65 6c 6c 73 20 75 73 20 74 68 65  Hdr tells us the
88370 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c   size of the all
88380 6f 63 61 74 69 6f 6e 20 61 6e 64 20 74 68 65 20  ocation and the 
88390 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 62 61 63  number of.** bac
883a0 6b 74 72 61 63 65 20 70 6f 69 6e 74 65 72 73 2e  ktrace pointers.
883b0 20 20 54 68 65 72 65 20 69 73 20 61 6c 73 6f 20    There is also 
883c0 61 20 67 75 61 72 64 20 77 6f 72 64 20 61 74 20  a guard word at 
883d0 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a  the end of the.*
883e0 2a 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2e 0a 2a  * MemBlockHdr..*
883f0 2f 0a 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63  /.struct MemBloc
88400 6b 48 64 72 20 7b 0a 20 20 69 36 34 20 69 53 69  kHdr {.  i64 iSi
88410 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ze;             
88420 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
88430 53 69 7a 65 20 6f 66 20 74 68 69 73 20 61 6c 6c  Size of this all
88440 6f 63 61 74 69 6f 6e 20 2a 2f 0a 20 20 73 74 72  ocation */.  str
88450 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 20  uct MemBlockHdr 
88460 2a 70 4e 65 78 74 2c 20 2a 70 50 72 65 76 3b 20  *pNext, *pPrev; 
88470 20 2f 2a 20 4c 69 6e 6b 65 64 20 6c 69 73 74 20   /* Linked list 
88480 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20 6d  of all unfreed m
88490 65 6d 6f 72 79 20 2a 2f 0a 20 20 63 68 61 72 20  emory */.  char 
884a0 6e 42 61 63 6b 74 72 61 63 65 3b 20 20 20 20 20  nBacktrace;     
884b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
884c0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 61 63 6b  * Number of back
884d0 74 72 61 63 65 73 20 6f 6e 20 74 68 69 73 20 61  traces on this a
884e0 6c 6c 6f 63 20 2a 2f 0a 20 20 63 68 61 72 20 6e  lloc */.  char n
884f0 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 20  BacktraceSlots; 
88500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
88510 20 41 76 61 69 6c 61 62 6c 65 20 62 61 63 6b 74   Available backt
88520 72 61 63 65 20 73 6c 6f 74 73 20 2a 2f 0a 20 20  race slots */.  
88530 73 68 6f 72 74 20 6e 54 69 74 6c 65 3b 20 20 20  short nTitle;   
88540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
88550 20 20 20 20 2f 2a 20 42 79 74 65 73 20 6f 66 20      /* Bytes of 
88560 74 69 74 6c 65 3b 20 69 6e 63 6c 75 64 65 73 20  title; includes 
88570 27 5c 30 27 20 2a 2f 0a 20 20 69 6e 74 20 69 46  '\0' */.  int iF
88580 6f 72 65 47 75 61 72 64 3b 20 20 20 20 20 20 20  oreGuard;       
88590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
885a0 20 47 75 61 72 64 20 77 6f 72 64 20 66 6f 72 20   Guard word for 
885b0 73 61 6e 69 74 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  sanity */.};../*
885c0 0a 2a 2a 20 47 75 61 72 64 20 77 6f 72 64 73 0a  .** Guard words.
885d0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f 52 45 47  */.#define FOREG
885e0 55 41 52 44 20 30 78 38 30 46 35 45 31 35 33 0a  UARD 0x80F5E153.
885f0 23 64 65 66 69 6e 65 20 52 45 41 52 47 55 41 52  #define REARGUAR
88600 44 20 30 78 45 34 36 37 36 42 35 33 0a 0a 2f 2a  D 0xE4676B53../*
88610 0a 2a 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 61  .** Number of ma
88620 6c 6c 6f 63 20 73 69 7a 65 20 69 6e 63 72 65 6d  lloc size increm
88630 65 6e 74 73 20 74 6f 20 74 72 61 63 6b 2e 0a 2a  ents to track..*
88640 2f 0a 23 64 65 66 69 6e 65 20 4e 43 53 49 5a 45  /.#define NCSIZE
88650 20 20 31 30 30 30 0a 0a 2f 2a 0a 2a 2a 20 41 6c    1000../*.** Al
88660 6c 20 6f 66 20 74 68 65 20 73 74 61 74 69 63 20  l of the static 
88670 76 61 72 69 61 62 6c 65 73 20 75 73 65 64 20 62  variables used b
88680 79 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 61 72  y this module ar
88690 65 20 63 6f 6c 6c 65 63 74 65 64 0a 2a 2a 20 69  e collected.** i
886a0 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 74 72  nto a single str
886b0 75 63 74 75 72 65 20 6e 61 6d 65 64 20 22 6d 65  ucture named "me
886c0 6d 22 2e 20 20 54 68 69 73 20 69 73 20 74 6f 20  m".  This is to 
886d0 6b 65 65 70 20 74 68 65 0a 2a 2a 20 73 74 61 74  keep the.** stat
886e0 69 63 20 76 61 72 69 61 62 6c 65 73 20 6f 72 67  ic variables org
886f0 61 6e 69 7a 65 64 20 61 6e 64 20 74 6f 20 72 65  anized and to re
88700 64 75 63 65 20 6e 61 6d 65 73 70 61 63 65 20 70  duce namespace p
88710 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e  ollution.** when
88720 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73 20   this module is 
88730 63 6f 6d 62 69 6e 65 64 20 77 69 74 68 20 6f 74  combined with ot
88740 68 65 72 20 69 6e 20 74 68 65 20 61 6d 61 6c 67  her in the amalg
88750 61 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  amation..*/.stat
88760 69 63 20 73 74 72 75 63 74 20 7b 0a 20 20 0a 20  ic struct {.  . 
88770 20 2f 2a 0a 20 20 2a 2a 20 4d 75 74 65 78 20 74   /*.  ** Mutex t
88780 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
88790 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   to the memory a
887a0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
887b0 74 65 6d 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  tem..  */.  sqli
887c0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
887d0 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 48 65 61  ;..  /*.  ** Hea
887e0 64 20 61 6e 64 20 74 61 69 6c 20 6f 66 20 61 20  d and tail of a 
887f0 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 61  linked list of a
88800 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61  ll outstanding a
88810 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a 2f 0a  llocations.  */.
88820 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
88830 6b 48 64 72 20 2a 70 46 69 72 73 74 3b 0a 20 20  kHdr *pFirst;.  
88840 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
88850 64 72 20 2a 70 4c 61 73 74 3b 0a 20 20 0a 20 20  dr *pLast;.  .  
88860 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6e 75 6d 62  /*.  ** The numb
88870 65 72 20 6f 66 20 6c 65 76 65 6c 73 20 6f 66 20  er of levels of 
88880 62 61 63 6b 74 72 61 63 65 20 74 6f 20 73 61 76  backtrace to sav
88890 65 20 69 6e 20 6e 65 77 20 61 6c 6c 6f 63 61 74  e in new allocat
888a0 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74  ions..  */.  int
888b0 20 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 76   nBacktrace;.  v
888c0 6f 69 64 20 28 2a 78 42 61 63 6b 74 72 61 63 65  oid (*xBacktrace
888d0 29 28 69 6e 74 2c 20 69 6e 74 2c 20 76 6f 69 64  )(int, int, void
888e0 20 2a 2a 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a   **);..  /*.  **
888f0 20 54 69 74 6c 65 20 74 65 78 74 20 74 6f 20 69   Title text to i
88900 6e 73 65 72 74 20 69 6e 20 66 72 6f 6e 74 20 6f  nsert in front o
88910 66 20 65 61 63 68 20 62 6c 6f 63 6b 0a 20 20 2a  f each block.  *
88920 2f 0a 20 20 69 6e 74 20 6e 54 69 74 6c 65 3b 20  /.  int nTitle; 
88930 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20         /* Bytes 
88940 6f 66 20 7a 54 69 74 6c 65 20 74 6f 20 73 61 76  of zTitle to sav
88950 65 2e 20 20 49 6e 63 6c 75 64 65 73 20 27 5c 30  e.  Includes '\0
88960 27 20 61 6e 64 20 70 61 64 64 69 6e 67 20 2a 2f  ' and padding */
88970 0a 20 20 63 68 61 72 20 7a 54 69 74 6c 65 5b 31  .  char zTitle[1
88980 30 30 5d 3b 20 20 2f 2a 20 54 68 65 20 74 69 74  00];  /* The tit
88990 6c 65 20 74 65 78 74 20 2a 2f 0a 0a 20 20 2f 2a  le text */..  /*
889a0 20 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61   .  ** sqlite3Ma
889b0 6c 6c 6f 63 44 69 73 61 6c 6c 6f 77 28 29 20 69  llocDisallow() i
889c0 6e 63 72 65 6d 65 6e 74 73 20 74 68 65 20 66 6f  ncrements the fo
889d0 6c 6c 6f 77 69 6e 67 20 63 6f 75 6e 74 65 72 2e  llowing counter.
889e0 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 4d 61 6c  .  ** sqlite3Mal
889f0 6c 6f 63 41 6c 6c 6f 77 28 29 20 64 65 63 72 65  locAllow() decre
88a00 6d 65 6e 74 73 20 69 74 2e 0a 20 20 2a 2f 0a 20  ments it..  */. 
88a10 20 69 6e 74 20 64 69 73 61 6c 6c 6f 77 3b 20 2f   int disallow; /
88a20 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 6d  * Do not allow m
88a30 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
88a40 20 2a 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 47   */..  /*.  ** G
88a50 61 74 68 65 72 20 73 74 61 74 69 73 74 69 63 73  ather statistics
88a60 20 6f 6e 20 74 68 65 20 73 69 7a 65 73 20 6f 66   on the sizes of
88a70 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
88a80 6f 6e 73 2e 0a 20 20 2a 2a 20 6e 41 6c 6c 6f 63  ons..  ** nAlloc
88a90 5b 69 5d 20 69 73 20 74 68 65 20 6e 75 6d 62 65  [i] is the numbe
88aa0 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  r of allocation 
88ab0 61 74 74 65 6d 70 74 73 20 6f 66 20 69 2a 38 0a  attempts of i*8.
88ac0 20 20 2a 2a 20 62 79 74 65 73 2e 20 20 69 3d 3d    ** bytes.  i==
88ad0 4e 43 53 49 5a 45 20 69 73 20 74 68 65 20 6e 75  NCSIZE is the nu
88ae0 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69  mber of allocati
88af0 6f 6e 20 61 74 74 65 6d 70 74 73 20 66 6f 72 0a  on attempts for.
88b00 20 20 2a 2a 20 73 69 7a 65 73 20 6d 6f 72 65 20    ** sizes more 
88b10 74 68 61 6e 20 4e 43 53 49 5a 45 2a 38 20 62 79  than NCSIZE*8 by
88b20 74 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  tes..  */.  int 
88b30 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 5d 3b 20  nAlloc[NCSIZE]; 
88b40 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
88b50 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69  mber of allocati
88b60 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 75  ons */.  int nCu
88b70 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20  rrent[NCSIZE];  
88b80 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75 6d    /* Current num
88b90 62 65 72 20 6f 66 20 61 6c 6c 6f 63 61 74 69 6f  ber of allocatio
88ba0 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 43 75  ns */.  int mxCu
88bb0 72 72 65 6e 74 5b 4e 43 53 49 5a 45 5d 3b 20 20  rrent[NCSIZE];  
88bc0 20 2f 2a 20 48 69 67 68 77 61 74 65 72 20 6d 61   /* Highwater ma
88bd0 72 6b 20 66 6f 72 20 6e 43 75 72 72 65 6e 74 20  rk for nCurrent 
88be0 2a 2f 0a 0a 7d 20 6d 65 6d 3b 0a 0a 0a 2f 2a 0a  */..} mem;.../*.
88bf0 2a 2a 20 41 64 6a 75 73 74 20 6d 65 6d 6f 72 79  ** Adjust memory
88c00 20 75 73 61 67 65 20 73 74 61 74 69 73 74 69 63   usage statistic
88c10 73 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  s.*/.static void
88c20 20 61 64 6a 75 73 74 53 74 61 74 73 28 69 6e 74   adjustStats(int
88c30 20 69 53 69 7a 65 2c 20 69 6e 74 20 69 6e 63 72   iSize, int incr
88c40 65 6d 65 6e 74 29 7b 0a 20 20 69 6e 74 20 69 20  ement){.  int i 
88c50 3d 20 28 28 69 53 69 7a 65 2b 37 29 26 7e 37 29  = ((iSize+7)&~7)
88c60 2f 38 3b 0a 20 20 69 66 28 20 69 3e 4e 43 53 49  /8;.  if( i>NCSI
88c70 5a 45 2d 31 20 29 7b 0a 20 20 20 20 69 20 3d 20  ZE-1 ){.    i = 
88c80 4e 43 53 49 5a 45 20 2d 20 31 3b 0a 20 20 7d 0a  NCSIZE - 1;.  }.
88c90 20 20 69 66 28 20 69 6e 63 72 65 6d 65 6e 74 3e    if( increment>
88ca0 30 20 29 7b 0a 20 20 20 20 6d 65 6d 2e 6e 41 6c  0 ){.    mem.nAl
88cb0 6c 6f 63 5b 69 5d 2b 2b 3b 0a 20 20 20 20 6d 65  loc[i]++;.    me
88cc0 6d 2e 6e 43 75 72 72 65 6e 74 5b 69 5d 2b 2b 3b  m.nCurrent[i]++;
88cd0 0a 20 20 20 20 69 66 28 20 6d 65 6d 2e 6e 43 75  .    if( mem.nCu
88ce0 72 72 65 6e 74 5b 69 5d 3e 6d 65 6d 2e 6d 78 43  rrent[i]>mem.mxC
88cf0 75 72 72 65 6e 74 5b 69 5d 20 29 7b 0a 20 20 20  urrent[i] ){.   
88d00 20 20 20 6d 65 6d 2e 6d 78 43 75 72 72 65 6e 74     mem.mxCurrent
88d10 5b 69 5d 20 3d 20 6d 65 6d 2e 6e 43 75 72 72 65  [i] = mem.nCurre
88d20 6e 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  nt[i];.    }.  }
88d30 65 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 2e 6e 43  else{.    mem.nC
88d40 75 72 72 65 6e 74 5b 69 5d 2d 2d 3b 0a 20 20 20  urrent[i]--;.   
88d50 20 61 73 73 65 72 74 28 20 6d 65 6d 2e 6e 43 75   assert( mem.nCu
88d60 72 72 65 6e 74 5b 69 5d 3e 3d 30 20 29 3b 0a 20  rrent[i]>=0 );. 
88d70 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65   }.}../*.** Give
88d80 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c  n an allocation,
88d90 20 66 69 6e 64 20 74 68 65 20 4d 65 6d 42 6c 6f   find the MemBlo
88da0 63 6b 48 64 72 20 66 6f 72 20 74 68 61 74 20 61  ckHdr for that a
88db0 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  llocation..**.**
88dc0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
88dd0 65 63 6b 73 20 74 68 65 20 67 75 61 72 64 73 20  ecks the guards 
88de0 61 74 20 65 69 74 68 65 72 20 65 6e 64 20 6f 66  at either end of
88df0 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20   the allocation 
88e00 61 6e 64 0a 2a 2a 20 69 66 20 74 68 65 79 20 61  and.** if they a
88e10 72 65 20 69 6e 63 6f 72 72 65 63 74 20 69 74 20  re incorrect it 
88e20 61 73 73 65 72 74 73 2e 0a 2a 2f 0a 73 74 61 74  asserts..*/.stat
88e30 69 63 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  ic struct MemBlo
88e40 63 6b 48 64 72 20 2a 73 71 6c 69 74 65 33 4d 65  ckHdr *sqlite3Me
88e50 6d 73 79 73 47 65 74 48 65 61 64 65 72 28 76 6f  msysGetHeader(vo
88e60 69 64 20 2a 70 41 6c 6c 6f 63 61 74 69 6f 6e 29  id *pAllocation)
88e70 7b 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c  {.  struct MemBl
88e80 6f 63 6b 48 64 72 20 2a 70 3b 0a 20 20 69 6e 74  ockHdr *p;.  int
88e90 20 2a 70 49 6e 74 3b 0a 20 20 75 38 20 2a 70 55   *pInt;.  u8 *pU
88ea0 38 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65 72 76  8;.  int nReserv
88eb0 65 3b 0a 0a 20 20 70 20 3d 20 28 73 74 72 75 63  e;..  p = (struc
88ec0 74 20 4d 65 6d 42 6c 6f 63 6b 48 64 72 2a 29 70  t MemBlockHdr*)p
88ed0 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 70 2d  Allocation;.  p-
88ee0 2d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  -;.  assert( p->
88ef0 69 46 6f 72 65 47 75 61 72 64 3d 3d 46 4f 52 45  iForeGuard==FORE
88f00 47 55 41 52 44 20 29 3b 0a 20 20 6e 52 65 73 65  GUARD );.  nRese
88f10 72 76 65 20 3d 20 28 70 2d 3e 69 53 69 7a 65 2b  rve = (p->iSize+
88f20 37 29 26 7e 37 3b 0a 20 20 70 49 6e 74 20 3d 20  7)&~7;.  pInt = 
88f30 28 69 6e 74 2a 29 70 41 6c 6c 6f 63 61 74 69 6f  (int*)pAllocatio
88f40 6e 3b 0a 20 20 70 55 38 20 3d 20 28 75 38 2a 29  n;.  pU8 = (u8*)
88f50 70 41 6c 6c 6f 63 61 74 69 6f 6e 3b 0a 20 20 61  pAllocation;.  a
88f60 73 73 65 72 74 28 20 70 49 6e 74 5b 6e 52 65 73  ssert( pInt[nRes
88f70 65 72 76 65 2f 73 69 7a 65 6f 66 28 69 6e 74 29  erve/sizeof(int)
88f80 5d 3d 3d 52 45 41 52 47 55 41 52 44 20 29 3b 0a  ]==REARGUARD );.
88f90 20 20 61 73 73 65 72 74 28 20 28 6e 52 65 73 65    assert( (nRese
88fa0 72 76 65 2d 30 29 3c 3d 70 2d 3e 69 53 69 7a 65  rve-0)<=p->iSize
88fb0 20 7c 7c 20 70 55 38 5b 6e 52 65 73 65 72 76 65   || pU8[nReserve
88fc0 2d 31 5d 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61  -1]==0x65 );.  a
88fd0 73 73 65 72 74 28 20 28 6e 52 65 73 65 72 76 65  ssert( (nReserve
88fe0 2d 31 29 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c  -1)<=p->iSize ||
88ff0 20 70 55 38 5b 6e 52 65 73 65 72 76 65 2d 32 5d   pU8[nReserve-2]
89000 3d 3d 30 78 36 35 20 29 3b 0a 20 20 61 73 73 65  ==0x65 );.  asse
89010 72 74 28 20 28 6e 52 65 73 65 72 76 65 2d 32 29  rt( (nReserve-2)
89020 3c 3d 70 2d 3e 69 53 69 7a 65 20 7c 7c 20 70 55  <=p->iSize || pU
89030 38 5b 6e 52 65 73 65 72 76 65 2d 33 5d 3d 3d 30  8[nReserve-3]==0
89040 78 36 35 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  x65 );.  return 
89050 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  p;.}../*.** Retu
89060 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
89070 20 62 79 74 65 73 20 63 75 72 72 65 6e 74 6c 79   bytes currently
89080 20 61 6c 6c 6f 63 61 74 65 64 20 61 74 20 61 64   allocated at ad
89090 64 72 65 73 73 20 70 2e 0a 2a 2f 0a 73 74 61 74  dress p..*/.stat
890a0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
890b0 6d 53 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a  mSize(void *p){.
890c0 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63    struct MemBloc
890d0 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 69 66  kHdr *pHdr;.  if
890e0 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
890f0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 48 64 72  rn 0;.  }.  pHdr
89100 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79 73   = sqlite3Memsys
89110 47 65 74 48 65 61 64 65 72 28 70 29 3b 0a 20 20  GetHeader(p);.  
89120 72 65 74 75 72 6e 20 70 48 64 72 2d 3e 69 53 69  return pHdr->iSi
89130 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  ze;.}../*.** Ini
89140 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f  tialize the memo
89150 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75  ry allocation su
89160 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74  bsystem..*/.stat
89170 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 65  ic int sqlite3Me
89180 6d 49 6e 69 74 28 76 6f 69 64 20 2a 4e 6f 74 55  mInit(void *NotU
89190 73 65 64 29 7b 0a 20 20 69 66 28 20 21 73 71 6c  sed){.  if( !sql
891a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
891b0 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20  .bMemstat ){.   
891c0 20 2f 2a 20 49 66 20 6d 65 6d 6f 72 79 20 73 74   /* If memory st
891d0 61 74 75 73 20 69 73 20 65 6e 61 62 6c 65 64 2c  atus is enabled,
891e0 20 74 68 65 6e 20 74 68 65 20 6d 61 6c 6c 6f 63   then the malloc
891f0 2e 63 20 77 72 61 70 70 65 72 20 77 69 6c 6c 20  .c wrapper will 
89200 61 6c 72 65 61 64 79 0a 20 20 20 20 2a 2a 20 68  already.    ** h
89210 6f 6c 64 20 74 68 65 20 53 54 41 54 49 43 5f 4d  old the STATIC_M
89220 45 4d 20 6d 75 74 65 78 20 77 68 65 6e 20 74 68  EM mutex when th
89230 65 20 72 6f 75 74 69 6e 65 73 20 68 65 72 65 20  e routines here 
89240 61 72 65 20 69 6e 76 6f 6b 65 64 2e 20 2a 2f 0a  are invoked. */.
89250 20 20 20 20 6d 65 6d 2e 6d 75 74 65 78 20 3d 20      mem.mutex = 
89260 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
89270 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
89280 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a  TATIC_MEM);.  }.
89290 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
892a0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69  OK;.}../*.** Dei
892b0 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 65  nitialize the me
892c0 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
892d0 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73 74  subsystem..*/.st
892e0 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65  atic void sqlite
892f0 33 4d 65 6d 53 68 75 74 64 6f 77 6e 28 76 6f 69  3MemShutdown(voi
89300 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 6d  d *NotUsed){.  m
89310 65 6d 2e 6d 75 74 65 78 20 3d 20 30 3b 0a 7d 0a  em.mutex = 0;.}.
89320 0a 2f 2a 0a 2a 2a 20 52 6f 75 6e 64 20 75 70 20  ./*.** Round up 
89330 61 20 72 65 71 75 65 73 74 20 73 69 7a 65 20 74  a request size t
89340 6f 20 74 68 65 20 6e 65 78 74 20 76 61 6c 69 64  o the next valid
89350 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65   allocation size
89360 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
89370 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64 75  sqlite3MemRoundu
89380 70 28 69 6e 74 20 6e 29 7b 0a 20 20 72 65 74 75  p(int n){.  retu
89390 72 6e 20 28 6e 2b 37 29 20 26 20 7e 37 3b 0a 7d  rn (n+7) & ~7;.}
893a0 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
893b0 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20   nByte bytes of 
893c0 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61 74 69  memory..*/.stati
893d0 63 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 4d  c void *sqlite3M
893e0 65 6d 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  emMalloc(int nBy
893f0 74 65 29 7b 0a 20 20 73 74 72 75 63 74 20 4d 65  te){.  struct Me
89400 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72 3b  mBlockHdr *pHdr;
89410 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a 20  .  void **pBt;. 
89420 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e 74 20   char *z;.  int 
89430 2a 70 49 6e 74 3b 0a 20 20 76 6f 69 64 20 2a 70  *pInt;.  void *p
89440 20 3d 20 30 3b 0a 20 20 69 6e 74 20 74 6f 74 61   = 0;.  int tota
89450 6c 53 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 52 65  lSize;.  int nRe
89460 73 65 72 76 65 3b 0a 20 20 73 71 6c 69 74 65 33  serve;.  sqlite3
89470 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
89480 2e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65 72  .mutex);.  asser
89490 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f 77 3d  t( mem.disallow=
894a0 3d 30 20 29 3b 0a 20 20 6e 52 65 73 65 72 76 65  =0 );.  nReserve
894b0 20 3d 20 28 6e 42 79 74 65 2b 37 29 26 7e 37 3b   = (nByte+7)&~7;
894c0 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20 6e  .  totalSize = n
894d0 52 65 73 65 72 76 65 20 2b 20 73 69 7a 65 6f 66  Reserve + sizeof
894e0 28 2a 70 48 64 72 29 20 2b 20 73 69 7a 65 6f 66  (*pHdr) + sizeof
894f0 28 69 6e 74 29 20 2b 0a 20 20 20 20 20 20 20 20  (int) +.        
89500 20 20 20 20 20 20 20 6d 65 6d 2e 6e 42 61 63 6b         mem.nBack
89510 74 72 61 63 65 2a 73 69 7a 65 6f 66 28 76 6f 69  trace*sizeof(voi
89520 64 2a 29 20 2b 20 6d 65 6d 2e 6e 54 69 74 6c 65  d*) + mem.nTitle
89530 3b 0a 20 20 70 20 3d 20 6d 61 6c 6c 6f 63 28 74  ;.  p = malloc(t
89540 6f 74 61 6c 53 69 7a 65 29 3b 0a 20 20 69 66 28  otalSize);.  if(
89550 20 70 20 29 7b 0a 20 20 20 20 7a 20 3d 20 70 3b   p ){.    z = p;
89560 0a 20 20 20 20 70 42 74 20 3d 20 28 76 6f 69 64  .    pBt = (void
89570 2a 2a 29 26 7a 5b 6d 65 6d 2e 6e 54 69 74 6c 65  **)&z[mem.nTitle
89580 5d 3b 0a 20 20 20 20 70 48 64 72 20 3d 20 28 73  ];.    pHdr = (s
89590 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48 64  truct MemBlockHd
895a0 72 2a 29 26 70 42 74 5b 6d 65 6d 2e 6e 42 61 63  r*)&pBt[mem.nBac
895b0 6b 74 72 61 63 65 5d 3b 0a 20 20 20 20 70 48 64  ktrace];.    pHd
895c0 72 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20  r->pNext = 0;.  
895d0 20 20 70 48 64 72 2d 3e 70 50 72 65 76 20 3d 20    pHdr->pPrev = 
895e0 6d 65 6d 2e 70 4c 61 73 74 3b 0a 20 20 20 20 69  mem.pLast;.    i
895f0 66 28 20 6d 65 6d 2e 70 4c 61 73 74 20 29 7b 0a  f( mem.pLast ){.
89600 20 20 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 2d        mem.pLast-
89610 3e 70 4e 65 78 74 20 3d 20 70 48 64 72 3b 0a 20  >pNext = pHdr;. 
89620 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
89630 6d 65 6d 2e 70 46 69 72 73 74 20 3d 20 70 48 64  mem.pFirst = pHd
89640 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d  r;.    }.    mem
89650 2e 70 4c 61 73 74 20 3d 20 70 48 64 72 3b 0a 20  .pLast = pHdr;. 
89660 20 20 20 70 48 64 72 2d 3e 69 46 6f 72 65 47 75     pHdr->iForeGu
89670 61 72 64 20 3d 20 46 4f 52 45 47 55 41 52 44 3b  ard = FOREGUARD;
89680 0a 20 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b  .    pHdr->nBack
89690 74 72 61 63 65 53 6c 6f 74 73 20 3d 20 6d 65 6d  traceSlots = mem
896a0 2e 6e 42 61 63 6b 74 72 61 63 65 3b 0a 20 20 20  .nBacktrace;.   
896b0 20 70 48 64 72 2d 3e 6e 54 69 74 6c 65 20 3d 20   pHdr->nTitle = 
896c0 6d 65 6d 2e 6e 54 69 74 6c 65 3b 0a 20 20 20 20  mem.nTitle;.    
896d0 69 66 28 20 6d 65 6d 2e 6e 42 61 63 6b 74 72 61  if( mem.nBacktra
896e0 63 65 20 29 7b 0a 20 20 20 20 20 20 76 6f 69 64  ce ){.      void
896f0 20 2a 61 41 64 64 72 5b 34 30 5d 3b 0a 20 20 20   *aAddr[40];.   
89700 20 20 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72     pHdr->nBacktr
89710 61 63 65 20 3d 20 62 61 63 6b 74 72 61 63 65 28  ace = backtrace(
89720 61 41 64 64 72 2c 20 6d 65 6d 2e 6e 42 61 63 6b  aAddr, mem.nBack
89730 74 72 61 63 65 2b 31 29 2d 31 3b 0a 20 20 20 20  trace+1)-1;.    
89740 20 20 6d 65 6d 63 70 79 28 70 42 74 2c 20 26 61    memcpy(pBt, &a
89750 41 64 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e  Addr[1], pHdr->n
89760 42 61 63 6b 74 72 61 63 65 2a 73 69 7a 65 6f 66  Backtrace*sizeof
89770 28 76 6f 69 64 2a 29 29 3b 0a 20 20 20 20 20 20  (void*));.      
89780 69 66 28 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61  if( mem.xBacktra
89790 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ce ){.        me
897a0 6d 2e 78 42 61 63 6b 74 72 61 63 65 28 6e 42 79  m.xBacktrace(nBy
897b0 74 65 2c 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74  te, pHdr->nBackt
897c0 72 61 63 65 2d 31 2c 20 26 61 41 64 64 72 5b 31  race-1, &aAddr[1
897d0 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ]);.      }.    
897e0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 48 64  }else{.      pHd
897f0 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 3d 20  r->nBacktrace = 
89800 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  0;.    }.    if(
89810 20 6d 65 6d 2e 6e 54 69 74 6c 65 20 29 7b 0a 20   mem.nTitle ){. 
89820 20 20 20 20 20 6d 65 6d 63 70 79 28 7a 2c 20 6d       memcpy(z, m
89830 65 6d 2e 7a 54 69 74 6c 65 2c 20 6d 65 6d 2e 6e  em.zTitle, mem.n
89840 54 69 74 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  Title);.    }.  
89850 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 3d 20    pHdr->iSize = 
89860 6e 42 79 74 65 3b 0a 20 20 20 20 61 64 6a 75 73  nByte;.    adjus
89870 74 53 74 61 74 73 28 6e 42 79 74 65 2c 20 2b 31  tStats(nByte, +1
89880 29 3b 0a 20 20 20 20 70 49 6e 74 20 3d 20 28 69  );.    pInt = (i
89890 6e 74 2a 29 26 70 48 64 72 5b 31 5d 3b 0a 20 20  nt*)&pHdr[1];.  
898a0 20 20 70 49 6e 74 5b 6e 52 65 73 65 72 76 65 2f    pInt[nReserve/
898b0 73 69 7a 65 6f 66 28 69 6e 74 29 5d 20 3d 20 52  sizeof(int)] = R
898c0 45 41 52 47 55 41 52 44 3b 0a 20 20 20 20 6d 65  EARGUARD;.    me
898d0 6d 73 65 74 28 70 49 6e 74 2c 20 30 78 36 35 2c  mset(pInt, 0x65,
898e0 20 6e 52 65 73 65 72 76 65 29 3b 0a 20 20 20 20   nReserve);.    
898f0 70 20 3d 20 28 76 6f 69 64 2a 29 70 49 6e 74 3b  p = (void*)pInt;
89900 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d  .  }.  sqlite3_m
89910 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d  utex_leave(mem.m
89920 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
89930 70 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  p; .}../*.** Fre
89940 65 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74 61  e memory..*/.sta
89950 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
89960 4d 65 6d 46 72 65 65 28 76 6f 69 64 20 2a 70 50  MemFree(void *pP
89970 72 69 6f 72 29 7b 0a 20 20 73 74 72 75 63 74 20  rior){.  struct 
89980 4d 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64  MemBlockHdr *pHd
89990 72 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b  r;.  void **pBt;
899a0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 61 73  .  char *z;.  as
899b0 73 65 72 74 28 20 73 71 6c 69 74 65 33 47 6c 6f  sert( sqlite3Glo
899c0 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74  balConfig.bMemst
899d0 61 74 20 7c 7c 20 6d 65 6d 2e 6d 75 74 65 78 21  at || mem.mutex!
899e0 3d 30 20 29 3b 0a 20 20 70 48 64 72 20 3d 20 73  =0 );.  pHdr = s
899f0 71 6c 69 74 65 33 4d 65 6d 73 79 73 47 65 74 48  qlite3MemsysGetH
89a00 65 61 64 65 72 28 70 50 72 69 6f 72 29 3b 0a 20  eader(pPrior);. 
89a10 20 70 42 74 20 3d 20 28 76 6f 69 64 2a 2a 29 70   pBt = (void**)p
89a20 48 64 72 3b 0a 20 20 70 42 74 20 2d 3d 20 70 48  Hdr;.  pBt -= pH
89a30 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 53 6c  dr->nBacktraceSl
89a40 6f 74 73 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  ots;.  sqlite3_m
89a50 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 2e 6d  utex_enter(mem.m
89a60 75 74 65 78 29 3b 0a 20 20 69 66 28 20 70 48 64  utex);.  if( pHd
89a70 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  r->pPrev ){.    
89a80 61 73 73 65 72 74 28 20 70 48 64 72 2d 3e 70 50  assert( pHdr->pP
89a90 72 65 76 2d 3e 70 4e 65 78 74 3d 3d 70 48 64 72  rev->pNext==pHdr
89aa0 20 29 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 50   );.    pHdr->pP
89ab0 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 48 64  rev->pNext = pHd
89ac0 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c 73  r->pNext;.  }els
89ad0 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d  e{.    assert( m
89ae0 65 6d 2e 70 46 69 72 73 74 3d 3d 70 48 64 72 20  em.pFirst==pHdr 
89af0 29 3b 0a 20 20 20 20 6d 65 6d 2e 70 46 69 72 73  );.    mem.pFirs
89b00 74 20 3d 20 70 48 64 72 2d 3e 70 4e 65 78 74 3b  t = pHdr->pNext;
89b10 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 64 72 2d  .  }.  if( pHdr-
89b20 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73  >pNext ){.    as
89b30 73 65 72 74 28 20 70 48 64 72 2d 3e 70 4e 65 78  sert( pHdr->pNex
89b40 74 2d 3e 70 50 72 65 76 3d 3d 70 48 64 72 20 29  t->pPrev==pHdr )
89b50 3b 0a 20 20 20 20 70 48 64 72 2d 3e 70 4e 65 78  ;.    pHdr->pNex
89b60 74 2d 3e 70 50 72 65 76 20 3d 20 70 48 64 72 2d  t->pPrev = pHdr-
89b70 3e 70 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65 7b  >pPrev;.  }else{
89b80 0a 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d  .    assert( mem
89b90 2e 70 4c 61 73 74 3d 3d 70 48 64 72 20 29 3b 0a  .pLast==pHdr );.
89ba0 20 20 20 20 6d 65 6d 2e 70 4c 61 73 74 20 3d 20      mem.pLast = 
89bb0 70 48 64 72 2d 3e 70 50 72 65 76 3b 0a 20 20 7d  pHdr->pPrev;.  }
89bc0 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 42  .  z = (char*)pB
89bd0 74 3b 0a 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e  t;.  z -= pHdr->
89be0 6e 54 69 74 6c 65 3b 0a 20 20 61 64 6a 75 73 74  nTitle;.  adjust
89bf0 53 74 61 74 73 28 70 48 64 72 2d 3e 69 53 69 7a  Stats(pHdr->iSiz
89c00 65 2c 20 2d 31 29 3b 0a 20 20 6d 65 6d 73 65 74  e, -1);.  memset
89c10 28 7a 2c 20 30 78 32 62 2c 20 73 69 7a 65 6f 66  (z, 0x2b, sizeof
89c20 28 76 6f 69 64 2a 29 2a 70 48 64 72 2d 3e 6e 42  (void*)*pHdr->nB
89c30 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 20 2b 20  acktraceSlots + 
89c40 73 69 7a 65 6f 66 28 2a 70 48 64 72 29 20 2b 0a  sizeof(*pHdr) +.
89c50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
89c60 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 20 2b 20    pHdr->iSize + 
89c70 73 69 7a 65 6f 66 28 69 6e 74 29 20 2b 20 70 48  sizeof(int) + pH
89c80 64 72 2d 3e 6e 54 69 74 6c 65 29 3b 0a 20 20 66  dr->nTitle);.  f
89c90 72 65 65 28 7a 29 3b 0a 20 20 73 71 6c 69 74 65  ree(z);.  sqlite
89ca0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
89cb0 6d 2e 6d 75 74 65 78 29 3b 20 20 0a 7d 0a 0a 2f  m.mutex);  .}../
89cc0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
89cd0 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73 74  size of an exist
89ce0 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63  ing memory alloc
89cf0 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72  ation..**.** For
89d00 20 74 68 69 73 20 64 65 62 75 67 67 69 6e 67 20   this debugging 
89d10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20  implementation, 
89d20 77 65 20 2a 61 6c 77 61 79 73 2a 20 6d 61 6b 65  we *always* make
89d30 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a   a copy of the.*
89d40 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e 74  * allocation int
89d50 6f 20 61 20 6e 65 77 20 70 6c 61 63 65 20 69 6e  o a new place in
89d60 20 6d 65 6d 6f 72 79 2e 20 20 49 6e 20 74 68 69   memory.  In thi
89d70 73 20 77 61 79 2c 20 69 66 20 74 68 65 20 0a 2a  s way, if the .*
89d80 2a 20 68 69 67 68 65 72 20 6c 65 76 65 6c 20 63  * higher level c
89d90 6f 64 65 20 69 73 20 75 73 69 6e 67 20 70 6f 69  ode is using poi
89da0 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 6c 64 20  nter to the old 
89db0 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20 69  allocation, it i
89dc0 73 20 0a 2a 2a 20 6d 75 63 68 20 6d 6f 72 65 20  s .** much more 
89dd0 6c 69 6b 65 6c 79 20 74 6f 20 62 72 65 61 6b 20  likely to break 
89de0 61 6e 64 20 77 65 20 61 72 65 20 6d 75 63 68 20  and we are much 
89df0 6d 6f 72 65 20 6c 69 6b 69 6e 67 20 74 6f 20 66  more liking to f
89e00 69 6e 64 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72  ind.** the error
89e10 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
89e20 20 2a 73 71 6c 69 74 65 33 4d 65 6d 52 65 61 6c   *sqlite3MemReal
89e30 6c 6f 63 28 76 6f 69 64 20 2a 70 50 72 69 6f 72  loc(void *pPrior
89e40 2c 20 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20  , int nByte){.  
89e50 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f 63 6b 48  struct MemBlockH
89e60 64 72 20 2a 70 4f 6c 64 48 64 72 3b 0a 20 20 76  dr *pOldHdr;.  v
89e70 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 61 73 73  oid *pNew;.  ass
89e80 65 72 74 28 20 6d 65 6d 2e 64 69 73 61 6c 6c 6f  ert( mem.disallo
89e90 77 3d 3d 30 20 29 3b 0a 20 20 70 4f 6c 64 48 64  w==0 );.  pOldHd
89ea0 72 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 73 79  r = sqlite3Memsy
89eb0 73 47 65 74 48 65 61 64 65 72 28 70 50 72 69 6f  sGetHeader(pPrio
89ec0 72 29 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c  r);.  pNew = sql
89ed0 69 74 65 33 4d 65 6d 4d 61 6c 6c 6f 63 28 6e 42  ite3MemMalloc(nB
89ee0 79 74 65 29 3b 0a 20 20 69 66 28 20 70 4e 65 77  yte);.  if( pNew
89ef0 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   ){.    memcpy(p
89f00 4e 65 77 2c 20 70 50 72 69 6f 72 2c 20 6e 42 79  New, pPrior, nBy
89f10 74 65 3c 70 4f 6c 64 48 64 72 2d 3e 69 53 69 7a  te<pOldHdr->iSiz
89f20 65 20 3f 20 6e 42 79 74 65 20 3a 20 70 4f 6c 64  e ? nByte : pOld
89f30 48 64 72 2d 3e 69 53 69 7a 65 29 3b 0a 20 20 20  Hdr->iSize);.   
89f40 20 69 66 28 20 6e 42 79 74 65 3e 70 4f 6c 64 48   if( nByte>pOldH
89f50 64 72 2d 3e 69 53 69 7a 65 20 29 7b 0a 20 20 20  dr->iSize ){.   
89f60 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63 68 61     memset(&((cha
89f70 72 2a 29 70 4e 65 77 29 5b 70 4f 6c 64 48 64 72  r*)pNew)[pOldHdr
89f80 2d 3e 69 53 69 7a 65 5d 2c 20 30 78 32 62 2c 20  ->iSize], 0x2b, 
89f90 6e 42 79 74 65 20 2d 20 70 4f 6c 64 48 64 72 2d  nByte - pOldHdr-
89fa0 3e 69 53 69 7a 65 29 3b 0a 20 20 20 20 7d 0a 20  >iSize);.    }. 
89fb0 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 46 72 65     sqlite3MemFre
89fc0 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20  e(pPrior);.  }. 
89fd0 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a   return pNew;.}.
89fe0 0a 2f 2a 0a 2a 2a 20 50 6f 70 75 6c 61 74 65 20  ./*.** Populate 
89ff0 74 68 65 20 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65  the low-level me
8a000 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  mory allocation 
8a010 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74 65 72  function pointer
8a020 73 20 69 6e 0a 2a 2a 20 73 71 6c 69 74 65 33 47  s in.** sqlite3G
8a030 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20 77 69  lobalConfig.m wi
8a040 74 68 20 70 6f 69 6e 74 65 72 73 20 74 6f 20 74  th pointers to t
8a050 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
8a060 68 69 73 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51 4c  his file..*/.SQL
8a070 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
8a080 20 73 71 6c 69 74 65 33 4d 65 6d 53 65 74 44 65   sqlite3MemSetDe
8a090 66 61 75 6c 74 28 76 6f 69 64 29 7b 0a 20 20 73  fault(void){.  s
8a0a0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
8a0b0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
8a0c0 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 20 3d  defaultMethods =
8a0d0 20 7b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 4d   {.     sqlite3M
8a0e0 65 6d 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73  emMalloc,.     s
8a0f0 71 6c 69 74 65 33 4d 65 6d 46 72 65 65 2c 0a 20  qlite3MemFree,. 
8a100 20 20 20 20 73 71 6c 69 74 65 33 4d 65 6d 52 65      sqlite3MemRe
8a110 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 73 71 6c 69  alloc,.     sqli
8a120 74 65 33 4d 65 6d 53 69 7a 65 2c 0a 20 20 20 20  te3MemSize,.    
8a130 20 73 71 6c 69 74 65 33 4d 65 6d 52 6f 75 6e 64   sqlite3MemRound
8a140 75 70 2c 0a 20 20 20 20 20 73 71 6c 69 74 65 33  up,.     sqlite3
8a150 4d 65 6d 49 6e 69 74 2c 0a 20 20 20 20 20 73 71  MemInit,.     sq
8a160 6c 69 74 65 33 4d 65 6d 53 68 75 74 64 6f 77 6e  lite3MemShutdown
8a170 2c 0a 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20  ,.     0.  };.  
8a180 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 53  sqlite3_config(S
8a190 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c  QLITE_CONFIG_MAL
8a1a0 4c 4f 43 2c 20 26 64 65 66 61 75 6c 74 4d 65 74  LOC, &defaultMet
8a1b0 68 6f 64 73 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  hods);.}../*.** 
8a1c0 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  Set the number o
8a1d0 66 20 62 61 63 6b 74 72 61 63 65 20 6c 65 76 65  f backtrace leve
8a1e0 6c 73 20 6b 65 70 74 20 66 6f 72 20 65 61 63 68  ls kept for each
8a1f0 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 20   allocation..** 
8a200 41 20 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 20  A value of zero 
8a210 74 75 72 6e 73 20 6f 66 66 20 62 61 63 6b 74 72  turns off backtr
8a220 61 63 69 6e 67 2e 20 20 54 68 65 20 6e 75 6d 62  acing.  The numb
8a230 65 72 20 69 73 20 61 6c 77 61 79 73 20 72 6f 75  er is always rou
8a240 6e 64 65 64 0a 2a 2a 20 75 70 20 74 6f 20 61 20  nded.** up to a 
8a250 6d 75 6c 74 69 70 6c 65 20 6f 66 20 32 2e 0a 2a  multiple of 2..*
8a260 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
8a270 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
8a280 64 65 62 75 67 42 61 63 6b 74 72 61 63 65 28 69  debugBacktrace(i
8a290 6e 74 20 64 65 70 74 68 29 7b 0a 20 20 69 66 28  nt depth){.  if(
8a2a0 20 64 65 70 74 68 3c 30 20 29 7b 20 64 65 70 74   depth<0 ){ dept
8a2b0 68 20 3d 20 30 3b 20 7d 0a 20 20 69 66 28 20 64  h = 0; }.  if( d
8a2c0 65 70 74 68 3e 32 30 20 29 7b 20 64 65 70 74 68  epth>20 ){ depth
8a2d0 20 3d 20 32 30 3b 20 7d 0a 20 20 64 65 70 74 68   = 20; }.  depth
8a2e0 20 3d 20 28 64 65 70 74 68 2b 31 29 26 30 78 66   = (depth+1)&0xf
8a2f0 65 3b 0a 20 20 6d 65 6d 2e 6e 42 61 63 6b 74 72  e;.  mem.nBacktr
8a300 61 63 65 20 3d 20 64 65 70 74 68 3b 0a 7d 0a 0a  ace = depth;.}..
8a310 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
8a320 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d 64 65  oid sqlite3Memde
8a330 62 75 67 42 61 63 6b 74 72 61 63 65 43 61 6c 6c  bugBacktraceCall
8a340 62 61 63 6b 28 76 6f 69 64 20 28 2a 78 42 61 63  back(void (*xBac
8a350 6b 74 72 61 63 65 29 28 69 6e 74 2c 20 69 6e 74  ktrace)(int, int
8a360 2c 20 76 6f 69 64 20 2a 2a 29 29 7b 0a 20 20 6d  , void **)){.  m
8a370 65 6d 2e 78 42 61 63 6b 74 72 61 63 65 20 3d 20  em.xBacktrace = 
8a380 78 42 61 63 6b 74 72 61 63 65 3b 0a 7d 0a 0a 2f  xBacktrace;.}../
8a390 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 74 69 74  *.** Set the tit
8a3a0 6c 65 20 73 74 72 69 6e 67 20 66 6f 72 20 73 75  le string for su
8a3b0 62 73 65 71 75 65 6e 74 20 61 6c 6c 6f 63 61 74  bsequent allocat
8a3c0 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ions..*/.SQLITE_
8a3d0 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
8a3e0 69 74 65 33 4d 65 6d 64 65 62 75 67 53 65 74 74  ite3MemdebugSett
8a3f0 69 74 6c 65 28 63 6f 6e 73 74 20 63 68 61 72 20  itle(const char 
8a400 2a 7a 54 69 74 6c 65 29 7b 0a 20 20 69 6e 74 20  *zTitle){.  int 
8a410 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 54 69 74 6c  n = strlen(zTitl
8a420 65 29 20 2b 20 31 3b 0a 20 20 73 71 6c 69 74 65  e) + 1;.  sqlite
8a430 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65  3_mutex_enter(me
8a440 6d 2e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  m.mutex);.  if( 
8a450 6e 3e 3d 73 69 7a 65 6f 66 28 6d 65 6d 2e 7a 54  n>=sizeof(mem.zT
8a460 69 74 6c 65 29 20 29 20 6e 20 3d 20 73 69 7a 65  itle) ) n = size
8a470 6f 66 28 6d 65 6d 2e 7a 54 69 74 6c 65 29 2d 31  of(mem.zTitle)-1
8a480 3b 0a 20 20 6d 65 6d 63 70 79 28 6d 65 6d 2e 7a  ;.  memcpy(mem.z
8a490 54 69 74 6c 65 2c 20 7a 54 69 74 6c 65 2c 20 6e  Title, zTitle, n
8a4a0 29 3b 0a 20 20 6d 65 6d 2e 7a 54 69 74 6c 65 5b  );.  mem.zTitle[
8a4b0 6e 5d 20 3d 20 30 3b 0a 20 20 6d 65 6d 2e 6e 54  n] = 0;.  mem.nT
8a4c0 69 74 6c 65 20 3d 20 28 6e 2b 37 29 26 7e 37 3b  itle = (n+7)&~7;
8a4d0 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
8a4e0 5f 6c 65 61 76 65 28 6d 65 6d 2e 6d 75 74 65 78  _leave(mem.mutex
8a4f0 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49  );.}..SQLITE_PRI
8a500 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
8a510 33 4d 65 6d 64 65 62 75 67 53 79 6e 63 28 29 7b  3MemdebugSync(){
8a520 0a 20 20 73 74 72 75 63 74 20 4d 65 6d 42 6c 6f  .  struct MemBlo
8a530 63 6b 48 64 72 20 2a 70 48 64 72 3b 0a 20 20 66  ckHdr *pHdr;.  f
8a540 6f 72 28 70 48 64 72 3d 6d 65 6d 2e 70 46 69 72  or(pHdr=mem.pFir
8a550 73 74 3b 20 70 48 64 72 3b 20 70 48 64 72 3d 70  st; pHdr; pHdr=p
8a560 48 64 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  Hdr->pNext){.   
8a570 20 76 6f 69 64 20 2a 2a 70 42 74 20 3d 20 28 76   void **pBt = (v
8a580 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20 20  oid**)pHdr;.    
8a590 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e 6e 42 61  pBt -= pHdr->nBa
8a5a0 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b 0a 20 20  cktraceSlots;.  
8a5b0 20 20 6d 65 6d 2e 78 42 61 63 6b 74 72 61 63 65    mem.xBacktrace
8a5c0 28 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 70 48  (pHdr->iSize, pH
8a5d0 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63 65 2d 31  dr->nBacktrace-1
8a5e0 2c 20 26 70 42 74 5b 31 5d 29 3b 0a 20 20 7d 0a  , &pBt[1]);.  }.
8a5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  }../*.** Open th
8a600 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
8a610 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67   and write a log
8a620 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
8a630 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63  memory .** alloc
8a640 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74  ations into that
8a650 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   log..*/.SQLITE_
8a660 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
8a670 69 74 65 33 4d 65 6d 64 65 62 75 67 44 75 6d 70  ite3MemdebugDump
8a680 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69  (const char *zFi
8a690 6c 65 6e 61 6d 65 29 7b 0a 20 20 46 49 4c 45 20  lename){.  FILE 
8a6a0 2a 6f 75 74 3b 0a 20 20 73 74 72 75 63 74 20 4d  *out;.  struct M
8a6b0 65 6d 42 6c 6f 63 6b 48 64 72 20 2a 70 48 64 72  emBlockHdr *pHdr
8a6c0 3b 0a 20 20 76 6f 69 64 20 2a 2a 70 42 74 3b 0a  ;.  void **pBt;.
8a6d0 20 20 69 6e 74 20 69 3b 0a 20 20 6f 75 74 20 3d    int i;.  out =
8a6e0 20 66 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65   fopen(zFilename
8a6f0 2c 20 22 77 22 29 3b 0a 20 20 69 66 28 20 6f 75  , "w");.  if( ou
8a700 74 3d 3d 30 20 29 7b 0a 20 20 20 20 66 70 72 69  t==0 ){.    fpri
8a710 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a 2a 20  ntf(stderr, "** 
8a720 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70 75 74  Unable to output
8a730 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20 6f 75   memory debug ou
8a740 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a 2a 5c  tput log: %s **\
8a750 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
8a760 20 20 20 20 20 20 20 20 7a 46 69 6c 65 6e 61 6d          zFilenam
8a770 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  e);.    return;.
8a780 20 20 7d 0a 20 20 66 6f 72 28 70 48 64 72 3d 6d    }.  for(pHdr=m
8a790 65 6d 2e 70 46 69 72 73 74 3b 20 70 48 64 72 3b  em.pFirst; pHdr;
8a7a0 20 70 48 64 72 3d 70 48 64 72 2d 3e 70 4e 65 78   pHdr=pHdr->pNex
8a7b0 74 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 20  t){.    char *z 
8a7c0 3d 20 28 63 68 61 72 2a 29 70 48 64 72 3b 0a 20  = (char*)pHdr;. 
8a7d0 20 20 20 7a 20 2d 3d 20 70 48 64 72 2d 3e 6e 42     z -= pHdr->nB
8a7e0 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 2a 73 69  acktraceSlots*si
8a7f0 7a 65 6f 66 28 76 6f 69 64 2a 29 20 2b 20 70 48  zeof(void*) + pH
8a800 64 72 2d 3e 6e 54 69 74 6c 65 3b 0a 20 20 20 20  dr->nTitle;.    
8a810 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 2a 2a  fprintf(out, "**
8a820 2a 2a 20 25 6c 6c 64 20 62 79 74 65 73 20 61 74  ** %lld bytes at
8a830 20 25 70 20 66 72 6f 6d 20 25 73 20 2a 2a 2a 2a   %p from %s ****
8a840 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20 20 20  \n", .          
8a850 20 20 70 48 64 72 2d 3e 69 53 69 7a 65 2c 20 26    pHdr->iSize, &
8a860 70 48 64 72 5b 31 5d 2c 20 70 48 64 72 2d 3e 6e  pHdr[1], pHdr->n
8a870 54 69 74 6c 65 20 3f 20 7a 20 3a 20 22 3f 3f 3f  Title ? z : "???
8a880 22 29 3b 0a 20 20 20 20 69 66 28 20 70 48 64 72  ");.    if( pHdr
8a890 2d 3e 6e 42 61 63 6b 74 72 61 63 65 20 29 7b 0a  ->nBacktrace ){.
8a8a0 20 20 20 20 20 20 66 66 6c 75 73 68 28 6f 75 74        fflush(out
8a8b0 29 3b 0a 20 20 20 20 20 20 70 42 74 20 3d 20 28  );.      pBt = (
8a8c0 76 6f 69 64 2a 2a 29 70 48 64 72 3b 0a 20 20 20  void**)pHdr;.   
8a8d0 20 20 20 70 42 74 20 2d 3d 20 70 48 64 72 2d 3e     pBt -= pHdr->
8a8e0 6e 42 61 63 6b 74 72 61 63 65 53 6c 6f 74 73 3b  nBacktraceSlots;
8a8f0 0a 20 20 20 20 20 20 62 61 63 6b 74 72 61 63 65  .      backtrace
8a900 5f 73 79 6d 62 6f 6c 73 5f 66 64 28 70 42 74 2c  _symbols_fd(pBt,
8a910 20 70 48 64 72 2d 3e 6e 42 61 63 6b 74 72 61 63   pHdr->nBacktrac
8a920 65 2c 20 66 69 6c 65 6e 6f 28 6f 75 74 29 29 3b  e, fileno(out));
8a930 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 6f  .      fprintf(o
8a940 75 74 2c 20 22 5c 6e 22 29 3b 0a 20 20 20 20 7d  ut, "\n");.    }
8a950 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28 6f  .  }.  fprintf(o
8a960 75 74 2c 20 22 43 4f 55 4e 54 53 3a 5c 6e 22 29  ut, "COUNTS:\n")
8a970 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e  ;.  for(i=0; i<N
8a980 43 53 49 5a 45 2d 31 3b 20 69 2b 2b 29 7b 0a 20  CSIZE-1; i++){. 
8a990 20 20 20 69 66 28 20 6d 65 6d 2e 6e 41 6c 6c 6f     if( mem.nAllo
8a9a0 63 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 66 70  c[i] ){.      fp
8a9b0 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 20 20 25  rintf(out, "   %
8a9c0 35 64 3a 20 25 31 30 64 20 25 31 30 64 20 25 31  5d: %10d %10d %1
8a9d0 30 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 20 20  0d\n", .        
8a9e0 20 20 20 20 69 2a 38 2c 20 6d 65 6d 2e 6e 41 6c      i*8, mem.nAl
8a9f0 6c 6f 63 5b 69 5d 2c 20 6d 65 6d 2e 6e 43 75 72  loc[i], mem.nCur
8aa00 72 65 6e 74 5b 69 5d 2c 20 6d 65 6d 2e 6d 78 43  rent[i], mem.mxC
8aa10 75 72 72 65 6e 74 5b 69 5d 29 3b 0a 20 20 20 20  urrent[i]);.    
8aa20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 2e  }.  }.  if( mem.
8aa30 6e 41 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d  nAlloc[NCSIZE-1]
8aa40 20 29 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28   ){.    fprintf(
8aa50 6f 75 74 2c 20 22 20 20 20 25 35 64 3a 20 25 31  out, "   %5d: %1
8aa60 30 64 20 25 31 30 64 20 25 31 30 64 5c 6e 22 2c  0d %10d %10d\n",
8aa70 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 43  .             NC
8aa80 53 49 5a 45 2a 38 2d 38 2c 20 6d 65 6d 2e 6e 41  SIZE*8-8, mem.nA
8aa90 6c 6c 6f 63 5b 4e 43 53 49 5a 45 2d 31 5d 2c 0a  lloc[NCSIZE-1],.
8aaa0 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d               mem
8aab0 2e 6e 43 75 72 72 65 6e 74 5b 4e 43 53 49 5a 45  .nCurrent[NCSIZE
8aac0 2d 31 5d 2c 20 6d 65 6d 2e 6d 78 43 75 72 72 65  -1], mem.mxCurre
8aad0 6e 74 5b 4e 43 53 49 5a 45 2d 31 5d 29 3b 0a 20  nt[NCSIZE-1]);. 
8aae0 20 7d 0a 20 20 66 63 6c 6f 73 65 28 6f 75 74 29   }.  fclose(out)
8aaf0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
8ab00 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  n the number of 
8ab10 74 69 6d 65 73 20 73 71 6c 69 74 65 33 4d 65 6d  times sqlite3Mem
8ab20 4d 61 6c 6c 6f 63 28 29 20 68 61 73 20 62 65 65  Malloc() has bee
8ab30 6e 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53 51 4c  n called..*/.SQL
8ab40 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
8ab50 73 71 6c 69 74 65 33 4d 65 6d 64 65 62 75 67 4d  sqlite3MemdebugM
8ab60 61 6c 6c 6f 63 43 6f 75 6e 74 28 29 7b 0a 20 20  allocCount(){.  
8ab70 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 54 6f  int i;.  int nTo
8ab80 74 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  tal = 0;.  for(i
8ab90 3d 30 3b 20 69 3c 4e 43 53 49 5a 45 3b 20 69 2b  =0; i<NCSIZE; i+
8aba0 2b 29 7b 0a 20 20 20 20 6e 54 6f 74 61 6c 20 2b  +){.    nTotal +
8abb0 3d 20 6d 65 6d 2e 6e 41 6c 6c 6f 63 5b 69 5d 3b  = mem.nAlloc[i];
8abc0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 54  .  }.  return nT
8abd0 6f 74 61 6c 3b 0a 7d 0a 0a 0a 23 65 6e 64 69 66  otal;.}...#endif
8abe0 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 45 4d 44 45   /* SQLITE_MEMDE
8abf0 42 55 47 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  BUG */../*******
8ac00 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d  ******* End of m
8ac10 65 6d 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em2.c **********
8ac20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ac30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ac40 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
8ac50 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
8ac60 6c 65 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a  le mem3.c ******
8ac70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ac80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ac90 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
8aca0 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a 2a 2a  07 October 14.**
8acb0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
8acc0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
8acd0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
8ace0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
8acf0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
8ad00 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
8ad10 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
8ad20 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
8ad30 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
8ad40 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
8ad50 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
8ad60 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
8ad70 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
8ad80 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
8ad90 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
8ada0 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
8adb0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
8adc0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
8add0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ade0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8adf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8ae00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
8ae10 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
8ae20 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74 69 6f  ns the C functio
8ae30 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e  ns that implemen
8ae40 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61 6c  t a memory.** al
8ae50 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
8ae60 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20 53 51  em for use by SQ
8ae70 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69  Lite. .**.** Thi
8ae80 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68 65  s version of the
8ae90 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
8aea0 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f 6d 69  on subsystem omi
8aeb0 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20 6f 66  ts all.** use of
8aec0 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65 20 53   malloc(). The S
8aed0 51 4c 69 74 65 20 75 73 65 72 20 73 75 70 70 6c  QLite user suppl
8aee0 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  ies a block of m
8aef0 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20  emory.** before 
8af00 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 5f  calling sqlite3_
8af10 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66 72 6f  initialize() fro
8af20 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61 74 69  m which allocati
8af30 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20  ons.** are made 
8af40 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62 79 20  and returned by 
8af50 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20 61 6e  the xMalloc() an
8af60 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a 2a 2a  d xRealloc() .**
8af70 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
8af80 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33 5f 69  . Once sqlite3_i
8af90 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61 73 20  nitialize() has 
8afa0 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a 2a 20  been called,.** 
8afb0 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 6d 65  the amount of me
8afc0 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65 20 74  mory available t
8afd0 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69 78 65  o SQLite is fixe
8afe0 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a 2a 20  d and cannot.** 
8aff0 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a  be changed..**.*
8b000 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f  * This version o
8b010 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c  f the memory all
8b020 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74 65  ocation subsyste
8b030 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a 2a 2a  m is included.**
8b040 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20 6f 6e   in the build on
8b050 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45 4e 41  ly if SQLITE_ENA
8b060 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73 20 64  BLE_MEMSYS3 is d
8b070 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 24 49  efined..**.** $I
8b080 64 3a 20 6d 65 6d 33 2e 63 2c 76 20 31 2e 32 35  d: mem3.c,v 1.25
8b090 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35   2008/11/19 16:5
8b0a0 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37  2:44 danielk1977
8b0b0 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
8b0c0 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66   This version of
8b0d0 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
8b0e0 63 61 74 6f 72 20 69 73 20 6f 6e 6c 79 20 62 75  cator is only bu
8b0f0 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 62  ilt into the lib
8b100 72 61 72 79 0a 2a 2a 20 53 51 4c 49 54 45 5f 45  rary.** SQLITE_E
8b110 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33 20 69 73  NABLE_MEMSYS3 is
8b120 20 64 65 66 69 6e 65 64 2e 20 44 65 66 69 6e 69   defined. Defini
8b130 6e 67 20 74 68 69 73 20 73 79 6d 62 6f 6c 20 64  ng this symbol d
8b140 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d 65 61 6e 20  oes not.** mean 
8b150 74 68 61 74 20 74 68 65 20 6c 69 62 72 61 72 79  that the library
8b160 20 77 69 6c 6c 20 75 73 65 20 61 20 6d 65 6d 6f   will use a memo
8b170 72 79 2d 70 6f 6f 6c 20 62 79 20 64 65 66 61 75  ry-pool by defau
8b180 6c 74 2c 20 6a 75 73 74 20 74 68 61 74 0a 2a 2a  lt, just that.**
8b190 20 69 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65   it is available
8b1a0 2e 20 54 68 65 20 6d 65 6d 70 6f 6f 6c 20 61 6c  . The mempool al
8b1b0 6c 6f 63 61 74 6f 72 20 69 73 20 61 63 74 69 76  locator is activ
8b1c0 61 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 0a  ated by calling.
8b1d0 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  ** sqlite3_confi
8b1e0 67 28 29 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  g()..*/.#ifdef S
8b1f0 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
8b200 53 59 53 33 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69  SYS3../*.** Maxi
8b210 6d 75 6d 20 73 69 7a 65 20 28 69 6e 20 4d 65 6d  mum size (in Mem
8b220 33 42 6c 6f 63 6b 73 29 20 6f 66 20 61 20 22 73  3Blocks) of a "s
8b230 6d 61 6c 6c 22 20 63 68 75 6e 6b 2e 0a 2a 2f 0a  mall" chunk..*/.
8b240 23 64 65 66 69 6e 65 20 4d 58 5f 53 4d 41 4c 4c  #define MX_SMALL
8b250 20 31 30 0a 0a 0a 2f 2a 0a 2a 2a 20 4e 75 6d 62   10.../*.** Numb
8b260 65 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 68  er of freelist h
8b270 61 73 68 20 73 6c 6f 74 73 0a 2a 2f 0a 23 64 65  ash slots.*/.#de
8b280 66 69 6e 65 20 4e 5f 48 41 53 48 20 20 36 31 0a  fine N_HASH  61.
8b290 0a 2f 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72 79 20  ./*.** A memory 
8b2a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 61 6c 73 6f  allocation (also
8b2b0 20 63 61 6c 6c 65 64 20 61 20 22 63 68 75 6e 6b   called a "chunk
8b2c0 22 29 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  ") consists of t
8b2d0 77 6f 20 6f 72 20 0a 2a 2a 20 6d 6f 72 65 20 62  wo or .** more b
8b2e0 6c 6f 63 6b 73 20 77 68 65 72 65 20 65 61 63 68  locks where each
8b2f0 20 62 6c 6f 63 6b 20 69 73 20 38 20 62 79 74 65   block is 8 byte
8b300 73 2e 20 20 54 68 65 20 66 69 72 73 74 20 38 20  s.  The first 8 
8b310 62 79 74 65 73 20 61 72 65 20 0a 2a 2a 20 61 20  bytes are .** a 
8b320 68 65 61 64 65 72 20 74 68 61 74 20 69 73 20 6e  header that is n
8b330 6f 74 20 72 65 74 75 72 6e 65 64 20 74 6f 20 74  ot returned to t
8b340 68 65 20 75 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 41  he user..**.** A
8b350 20 63 68 75 6e 6b 20 69 73 20 74 77 6f 20 6f 72   chunk is two or
8b360 20 6d 6f 72 65 20 62 6c 6f 63 6b 73 20 74 68 61   more blocks tha
8b370 74 20 69 73 20 65 69 74 68 65 72 20 63 68 65 63  t is either chec
8b380 6b 65 64 20 6f 75 74 20 6f 72 0a 2a 2a 20 66 72  ked out or.** fr
8b390 65 65 2e 20 20 54 68 65 20 66 69 72 73 74 20 62  ee.  The first b
8b3a0 6c 6f 63 6b 20 68 61 73 20 66 6f 72 6d 61 74 20  lock has format 
8b3b0 75 2e 68 64 72 2e 20 20 75 2e 68 64 72 2e 73 69  u.hdr.  u.hdr.si
8b3c0 7a 65 34 78 20 69 73 20 34 20 74 69 6d 65 73 20  ze4x is 4 times 
8b3d0 74 68 65 0a 2a 2a 20 73 69 7a 65 20 6f 66 20 74  the.** size of t
8b3e0 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 6e  he allocation in
8b3f0 20 62 6c 6f 63 6b 73 20 69 66 20 74 68 65 20 61   blocks if the a
8b400 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65  llocation is fre
8b410 65 2e 0a 2a 2a 20 54 68 65 20 75 2e 68 64 72 2e  e..** The u.hdr.
8b420 73 69 7a 65 34 78 26 31 20 62 69 74 20 69 73 20  size4x&1 bit is 
8b430 74 72 75 65 20 69 66 20 74 68 65 20 63 68 75 6e  true if the chun
8b440 6b 20 69 73 20 63 68 65 63 6b 65 64 20 6f 75 74  k is checked out
8b450 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 69 66   and.** false if
8b460 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 6f 6e   the chunk is on
8b470 20 74 68 65 20 66 72 65 65 6c 69 73 74 2e 20 20   the freelist.  
8b480 54 68 65 20 75 2e 68 64 72 2e 73 69 7a 65 34 78  The u.hdr.size4x
8b490 26 32 20 62 69 74 0a 2a 2a 20 69 73 20 74 72 75  &2 bit.** is tru
8b4a0 65 20 69 66 20 74 68 65 20 70 72 65 76 69 6f 75  e if the previou
8b4b0 73 20 63 68 75 6e 6b 20 69 73 20 63 68 65 63 6b  s chunk is check
8b4c0 65 64 20 6f 75 74 20 61 6e 64 20 66 61 6c 73 65  ed out and false
8b4d0 20 69 66 20 74 68 65 0a 2a 2a 20 70 72 65 76 69   if the.** previ
8b4e0 6f 75 73 20 63 68 75 6e 6b 20 69 73 20 66 72 65  ous chunk is fre
8b4f0 65 2e 20 20 54 68 65 20 75 2e 68 64 72 2e 70 72  e.  The u.hdr.pr
8b500 65 76 53 69 7a 65 20 66 69 65 6c 64 20 69 73 20  evSize field is 
8b510 74 68 65 20 73 69 7a 65 20 6f 66 0a 2a 2a 20 74  the size of.** t
8b520 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e  he previous chun
8b530 6b 20 69 6e 20 62 6c 6f 63 6b 73 20 69 66 20 74  k in blocks if t
8b540 68 65 20 70 72 65 76 69 6f 75 73 20 63 68 75 6e  he previous chun
8b550 6b 20 69 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 66  k is on the.** f
8b560 72 65 65 6c 69 73 74 2e 20 49 66 20 74 68 65 20  reelist. If the 
8b570 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
8b580 73 20 63 68 65 63 6b 65 64 20 6f 75 74 2c 20 74  s checked out, t
8b590 68 65 6e 0a 2a 2a 20 75 2e 68 64 72 2e 70 72 65  hen.** u.hdr.pre
8b5a0 76 53 69 7a 65 20 63 61 6e 20 62 65 20 70 61 72  vSize can be par
8b5b0 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
8b5c0 72 20 74 68 61 74 20 63 68 75 6e 6b 20 61 6e 64  r that chunk and
8b5d0 20 73 68 6f 75 6c 64 0a 2a 2a 20 6e 6f 74 20 62   should.** not b
8b5e0 65 20 72 65 61 64 20 6f 72 20 77 72 69 74 74 65  e read or writte
8b5f0 6e 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 6f 66 74 65  n..**.** We ofte
8b600 6e 20 69 64 65 6e 74 69 66 79 20 61 20 63 68 75  n identify a chu
8b610 6e 6b 20 62 79 20 69 74 73 20 69 6e 64 65 78 20  nk by its index 
8b620 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 2e  in mem3.aPool[].
8b630 20 20 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 69    When.** this i
8b640 73 20 64 6f 6e 65 2c 20 74 68 65 20 63 68 75 6e  s done, the chun
8b650 6b 20 69 6e 64 65 78 20 72 65 66 65 72 73 20 74  k index refers t
8b660 6f 20 74 68 65 20 73 65 63 6f 6e 64 20 62 6c 6f  o the second blo
8b670 63 6b 20 6f 66 0a 2a 2a 20 74 68 65 20 63 68 75  ck of.** the chu
8b680 6e 6b 2e 20 20 49 6e 20 74 68 69 73 20 77 61 79  nk.  In this way
8b690 2c 20 74 68 65 20 66 69 72 73 74 20 63 68 75 6e  , the first chun
8b6a0 6b 20 68 61 73 20 61 6e 20 69 6e 64 65 78 20 6f  k has an index o
8b6b0 66 20 31 2e 0a 2a 2a 20 41 20 63 68 75 6e 6b 20  f 1..** A chunk 
8b6c0 69 6e 64 65 78 20 6f 66 20 30 20 6d 65 61 6e 73  index of 0 means
8b6d0 20 22 6e 6f 20 73 75 63 68 20 63 68 75 6e 6b 22   "no such chunk"
8b6e0 20 61 6e 64 20 69 73 20 74 68 65 20 65 71 75 69   and is the equi
8b6f0 76 61 6c 65 6e 74 0a 2a 2a 20 6f 66 20 61 20 4e  valent.** of a N
8b700 55 4c 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a  ULL pointer..**.
8b710 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 62 6c  ** The second bl
8b720 6f 63 6b 20 6f 66 20 66 72 65 65 20 63 68 75 6e  ock of free chun
8b730 6b 73 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72  ks is of the for
8b740 6d 20 75 2e 6c 69 73 74 2e 20 20 54 68 65 0a 2a  m u.list.  The.*
8b750 2a 20 74 77 6f 20 66 69 65 6c 64 73 20 66 6f 72  * two fields for
8b760 6d 20 61 20 64 6f 75 62 6c 65 2d 6c 69 6e 6b 65  m a double-linke
8b770 64 20 6c 69 73 74 20 6f 66 20 63 68 75 6e 6b 73  d list of chunks
8b780 20 6f 66 20 72 65 6c 61 74 65 64 20 73 69 7a 65   of related size
8b790 73 2e 0a 2a 2a 20 50 6f 69 6e 74 65 72 73 20 74  s..** Pointers t
8b7a0 6f 20 74 68 65 20 68 65 61 64 20 6f 66 20 74 68  o the head of th
8b7b0 65 20 6c 69 73 74 20 61 72 65 20 73 74 6f 72 65  e list are store
8b7c0 64 20 69 6e 20 6d 65 6d 33 2e 61 69 53 6d 61 6c  d in mem3.aiSmal
8b7d0 6c 5b 5d 20 0a 2a 2a 20 66 6f 72 20 73 6d 61 6c  l[] .** for smal
8b7e0 6c 65 72 20 63 68 75 6e 6b 73 20 61 6e 64 20 6d  ler chunks and m
8b7f0 65 6d 33 2e 61 69 48 61 73 68 5b 5d 20 66 6f 72  em3.aiHash[] for
8b800 20 6c 61 72 67 65 72 20 63 68 75 6e 6b 73 2e 0a   larger chunks..
8b810 2a 2a 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64  **.** The second
8b820 20 62 6c 6f 63 6b 20 6f 66 20 61 20 63 68 75 6e   block of a chun
8b830 6b 20 69 73 20 75 73 65 72 20 64 61 74 61 20 69  k is user data i
8b840 66 20 74 68 65 20 63 68 75 6e 6b 20 69 73 20 63  f the chunk is c
8b850 68 65 63 6b 65 64 20 0a 2a 2a 20 6f 75 74 2e 20  hecked .** out. 
8b860 20 49 66 20 61 20 63 68 75 6e 6b 20 69 73 20 63   If a chunk is c
8b870 68 65 63 6b 65 64 20 6f 75 74 2c 20 74 68 65 20  hecked out, the 
8b880 75 73 65 72 20 64 61 74 61 20 6d 61 79 20 65 78  user data may ex
8b890 74 65 6e 64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65  tend into.** the
8b8a0 20 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 20   u.hdr.prevSize 
8b8b0 76 61 6c 75 65 20 6f 66 20 74 68 65 20 66 6f 6c  value of the fol
8b8c0 6c 6f 77 69 6e 67 20 63 68 75 6e 6b 2e 0a 2a 2f  lowing chunk..*/
8b8d0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
8b8e0 4d 65 6d 33 42 6c 6f 63 6b 20 4d 65 6d 33 42 6c  Mem3Block Mem3Bl
8b8f0 6f 63 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 33  ock;.struct Mem3
8b900 42 6c 6f 63 6b 20 7b 0a 20 20 75 6e 69 6f 6e 20  Block {.  union 
8b910 7b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20  {.    struct {. 
8b920 20 20 20 20 20 75 33 32 20 70 72 65 76 53 69 7a       u32 prevSiz
8b930 65 3b 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20  e;   /* Size of 
8b940 70 72 65 76 69 6f 75 73 20 63 68 75 6e 6b 20 69  previous chunk i
8b950 6e 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d  n Mem3Block elem
8b960 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20 20 75 33  ents */.      u3
8b970 32 20 73 69 7a 65 34 78 3b 20 20 20 20 20 2f 2a  2 size4x;     /*
8b980 20 34 78 20 74 68 65 20 73 69 7a 65 20 6f 66 20   4x the size of 
8b990 63 75 72 72 65 6e 74 20 63 68 75 6e 6b 20 69 6e  current chunk in
8b9a0 20 4d 65 6d 33 42 6c 6f 63 6b 20 65 6c 65 6d 65   Mem3Block eleme
8b9b0 6e 74 73 20 2a 2f 0a 20 20 20 20 7d 20 68 64 72  nts */.    } hdr
8b9c0 3b 0a 20 20 20 20 73 74 72 75 63 74 20 7b 0a 20  ;.    struct {. 
8b9d0 20 20 20 20 20 75 33 32 20 6e 65 78 74 3b 20 20       u32 next;  
8b9e0 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
8b9f0 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 5d 20 6f 66   mem3.aPool[] of
8ba00 20 6e 65 78 74 20 66 72 65 65 20 63 68 75 6e 6b   next free chunk
8ba10 20 2a 2f 0a 20 20 20 20 20 20 75 33 32 20 70 72   */.      u32 pr
8ba20 65 76 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  ev;       /* Ind
8ba30 65 78 20 69 6e 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ex in mem3.aPool
8ba40 5b 5d 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66  [] of previous f
8ba50 72 65 65 20 63 68 75 6e 6b 20 2a 2f 0a 20 20 20  ree chunk */.   
8ba60 20 7d 20 6c 69 73 74 3b 0a 20 20 7d 20 75 3b 0a   } list;.  } u;.
8ba70 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  };../*.** All of
8ba80 20 74 68 65 20 73 74 61 74 69 63 20 76 61 72 69   the static vari
8ba90 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 74 68  ables used by th
8baa0 69 73 20 6d 6f 64 75 6c 65 20 61 72 65 20 63 6f  is module are co
8bab0 6c 6c 65 63 74 65 64 0a 2a 2a 20 69 6e 74 6f 20  llected.** into 
8bac0 61 20 73 69 6e 67 6c 65 20 73 74 72 75 63 74 75  a single structu
8bad0 72 65 20 6e 61 6d 65 64 20 22 6d 65 6d 33 22 2e  re named "mem3".
8bae0 20 20 54 68 69 73 20 69 73 20 74 6f 20 6b 65 65    This is to kee
8baf0 70 20 74 68 65 0a 2a 2a 20 73 74 61 74 69 63 20  p the.** static 
8bb00 76 61 72 69 61 62 6c 65 73 20 6f 72 67 61 6e 69  variables organi
8bb10 7a 65 64 20 61 6e 64 20 74 6f 20 72 65 64 75 63  zed and to reduc
8bb20 65 20 6e 61 6d 65 73 70 61 63 65 20 70 6f 6c 6c  e namespace poll
8bb30 75 74 69 6f 6e 0a 2a 2a 20 77 68 65 6e 20 74 68  ution.** when th
8bb40 69 73 20 6d 6f 64 75 6c 65 20 69 73 20 63 6f 6d  is module is com
8bb50 62 69 6e 65 64 20 77 69 74 68 20 6f 74 68 65 72  bined with other
8bb60 20 69 6e 20 74 68 65 20 61 6d 61 6c 67 61 6d 61   in the amalgama
8bb70 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
8bb80 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63  SQLITE_WSD struc
8bb90 74 20 4d 65 6d 33 47 6c 6f 62 61 6c 20 7b 0a 20  t Mem3Global {. 
8bba0 20 2f 2a 0a 20 20 2a 2a 20 4d 65 6d 6f 72 79 20   /*.  ** Memory 
8bbb0 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 61 6c  available for al
8bbc0 6c 6f 63 61 74 69 6f 6e 2e 20 6e 50 6f 6f 6c 20  location. nPool 
8bbd0 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
8bbe0 68 65 20 61 72 72 61 79 0a 20 20 2a 2a 20 28 69  he array.  ** (i
8bbf0 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29 20 70 6f  n Mem3Blocks) po
8bc00 69 6e 74 65 64 20 74 6f 20 62 79 20 61 50 6f 6f  inted to by aPoo
8bc10 6c 20 6c 65 73 73 20 32 2e 0a 20 20 2a 2f 0a 20  l less 2..  */. 
8bc20 20 75 33 32 20 6e 50 6f 6f 6c 3b 0a 20 20 4d 65   u32 nPool;.  Me
8bc30 6d 33 42 6c 6f 63 6b 20 2a 61 50 6f 6f 6c 3b 0a  m3Block *aPool;.
8bc40 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 72 75 65 20  .  /*.  ** True 
8bc50 69 66 20 77 65 20 61 72 65 20 65 76 61 6c 75 61  if we are evalua
8bc60 74 69 6e 67 20 61 6e 20 6f 75 74 2d 6f 66 2d 6d  ting an out-of-m
8bc70 65 6d 6f 72 79 20 63 61 6c 6c 62 61 63 6b 2e 0a  emory callback..
8bc80 20 20 2a 2f 0a 20 20 69 6e 74 20 61 6c 61 72 6d    */.  int alarm
8bc90 42 75 73 79 3b 0a 20 20 0a 20 20 2f 2a 0a 20 20  Busy;.  .  /*.  
8bca0 2a 2a 20 4d 75 74 65 78 20 74 6f 20 63 6f 6e 74  ** Mutex to cont
8bcb0 72 6f 6c 20 61 63 63 65 73 73 20 74 6f 20 74 68  rol access to th
8bcc0 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
8bcd0 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 20  ion subsystem.. 
8bce0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
8bcf0 74 65 78 20 2a 6d 75 74 65 78 3b 0a 20 20 0a 20  tex *mutex;.  . 
8bd00 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 69 6e   /*.  ** The min
8bd10 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20 66  imum amount of f
8bd20 72 65 65 20 73 70 61 63 65 20 74 68 61 74 20 77  ree space that w
8bd30 65 20 68 61 76 65 20 73 65 65 6e 2e 0a 20 20 2a  e have seen..  *
8bd40 2f 0a 20 20 75 33 32 20 6d 6e 4d 61 73 74 65 72  /.  u32 mnMaster
8bd50 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 69 4d 61  ;..  /*.  ** iMa
8bd60 73 74 65 72 20 69 73 20 74 68 65 20 69 6e 64 65  ster is the inde
8bd70 78 20 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20  x of the master 
8bd80 63 68 75 6e 6b 2e 20 20 4d 6f 73 74 20 6e 65 77  chunk.  Most new
8bd90 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 0a 20 20 2a   allocations.  *
8bda0 2a 20 6f 63 63 75 72 20 6f 66 66 20 6f 66 20 74  * occur off of t
8bdb0 68 69 73 20 63 68 75 6e 6b 2e 20 20 73 7a 4d 61  his chunk.  szMa
8bdc0 73 74 65 72 20 69 73 20 74 68 65 20 73 69 7a 65  ster is the size
8bdd0 20 28 69 6e 20 4d 65 6d 33 42 6c 6f 63 6b 73 29   (in Mem3Blocks)
8bde0 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 63 75 72  .  ** of the cur
8bdf0 72 65 6e 74 20 6d 61 73 74 65 72 2e 20 20 69 4d  rent master.  iM
8be00 61 73 74 65 72 20 69 73 20 30 20 69 66 20 74 68  aster is 0 if th
8be10 65 72 65 20 69 73 20 6e 6f 74 20 6d 61 73 74 65  ere is not maste
8be20 72 20 63 68 75 6e 6b 2e 0a 20 20 2a 2a 20 54 68  r chunk..  ** Th
8be30 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20 69  e master chunk i
8be40 73 20 6e 6f 74 20 69 6e 20 65 69 74 68 65 72 20  s not in either 
8be50 74 68 65 20 61 69 48 61 73 68 5b 5d 20 6f 72 20  the aiHash[] or 
8be60 61 69 53 6d 61 6c 6c 5b 5d 2e 0a 20 20 2a 2f 0a  aiSmall[]..  */.
8be70 20 20 75 33 32 20 69 4d 61 73 74 65 72 3b 0a 20    u32 iMaster;. 
8be80 20 75 33 32 20 73 7a 4d 61 73 74 65 72 3b 0a 0a   u32 szMaster;..
8be90 20 20 2f 2a 0a 20 20 2a 2a 20 41 72 72 61 79 20    /*.  ** Array 
8bea0 6f 66 20 6c 69 73 74 73 20 6f 66 20 66 72 65 65  of lists of free
8beb0 20 62 6c 6f 63 6b 73 20 61 63 63 6f 72 64 69 6e   blocks accordin
8bec0 67 20 74 6f 20 74 68 65 20 62 6c 6f 63 6b 20 73  g to the block s
8bed0 69 7a 65 20 0a 20 20 2a 2a 20 66 6f 72 20 73 6d  ize .  ** for sm
8bee0 61 6c 6c 65 72 20 63 68 75 6e 6b 73 2c 20 6f 72  aller chunks, or
8bef0 20 61 20 68 61 73 68 20 6f 6e 20 74 68 65 20 62   a hash on the b
8bf00 6c 6f 63 6b 20 73 69 7a 65 20 66 6f 72 20 6c 61  lock size for la
8bf10 72 67 65 72 0a 20 20 2a 2a 20 63 68 75 6e 6b 73  rger.  ** chunks
8bf20 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 61 69 53  ..  */.  u32 aiS
8bf30 6d 61 6c 6c 5b 4d 58 5f 53 4d 41 4c 4c 2d 31 5d  mall[MX_SMALL-1]
8bf40 3b 20 20 20 2f 2a 20 46 6f 72 20 73 69 7a 65 73  ;   /* For sizes
8bf50 20 32 20 74 68 72 6f 75 67 68 20 4d 58 5f 53 4d   2 through MX_SM
8bf60 41 4c 4c 2c 20 69 6e 63 6c 75 73 69 76 65 20 2a  ALL, inclusive *
8bf70 2f 0a 20 20 75 33 32 20 61 69 48 61 73 68 5b 4e  /.  u32 aiHash[N
8bf80 5f 48 41 53 48 5d 3b 20 20 20 20 20 20 20 20 2f  _HASH];        /
8bf90 2a 20 46 6f 72 20 73 69 7a 65 73 20 4d 58 5f 53  * For sizes MX_S
8bfa0 4d 41 4c 4c 2b 31 20 61 6e 64 20 6c 61 72 67 65  MALL+1 and large
8bfb0 72 20 2a 2f 0a 7d 20 6d 65 6d 33 20 3d 20 7b 20  r */.} mem3 = { 
8bfc0 39 37 35 33 35 35 37 35 20 7d 3b 0a 0a 23 64 65  97535575 };..#de
8bfd0 66 69 6e 65 20 6d 65 6d 33 20 47 4c 4f 42 41 4c  fine mem3 GLOBAL
8bfe0 28 73 74 72 75 63 74 20 4d 65 6d 33 47 6c 6f 62  (struct Mem3Glob
8bff0 61 6c 2c 20 6d 65 6d 33 29 0a 0a 2f 2a 0a 2a 2a  al, mem3)../*.**
8c000 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63 68 75 6e   Unlink the chun
8c010 6b 20 61 74 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  k at mem3.aPool[
8c020 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20 69 74 20  i] from list it 
8c030 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a 2a 20  is currently.** 
8c040 6f 6e 2e 20 20 2a 70 52 6f 6f 74 20 69 73 20 74  on.  *pRoot is t
8c050 68 65 20 6c 69 73 74 20 74 68 61 74 20 69 20 69  he list that i i
8c060 73 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e 0a 2a  s a member of..*
8c070 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d 65  /.static void me
8c080 6d 73 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c  msys3UnlinkFromL
8c090 69 73 74 28 75 33 32 20 69 2c 20 75 33 32 20 2a  ist(u32 i, u32 *
8c0a0 70 52 6f 6f 74 29 7b 0a 20 20 75 33 32 20 6e 65  pRoot){.  u32 ne
8c0b0 78 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  xt = mem3.aPool[
8c0c0 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74 3b 0a  i].u.list.next;.
8c0d0 20 20 75 33 32 20 70 72 65 76 20 3d 20 6d 65 6d    u32 prev = mem
8c0e0 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73  3.aPool[i].u.lis
8c0f0 74 2e 70 72 65 76 3b 0a 20 20 61 73 73 65 72 74  t.prev;.  assert
8c100 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
8c110 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
8c120 20 29 3b 0a 20 20 69 66 28 20 70 72 65 76 3d 3d   );.  if( prev==
8c130 30 20 29 7b 0a 20 20 20 20 2a 70 52 6f 6f 74 20  0 ){.    *pRoot 
8c140 3d 20 6e 65 78 74 3b 0a 20 20 7d 65 6c 73 65 7b  = next;.  }else{
8c150 0a 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  .    mem3.aPool[
8c160 70 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78  prev].u.list.nex
8c170 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d 0a 20 20  t = next;.  }.  
8c180 69 66 28 20 6e 65 78 74 20 29 7b 0a 20 20 20 20  if( next ){.    
8c190 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 78 74 5d  mem3.aPool[next]
8c1a0 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20 3d 20 70  .u.list.prev = p
8c1b0 72 65 76 3b 0a 20 20 7d 0a 20 20 6d 65 6d 33 2e  rev;.  }.  mem3.
8c1c0 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e  aPool[i].u.list.
8c1d0 6e 65 78 74 20 3d 20 30 3b 0a 20 20 6d 65 6d 33  next = 0;.  mem3
8c1e0 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74  .aPool[i].u.list
8c1f0 2e 70 72 65 76 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  .prev = 0;.}../*
8c200 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63  .** Unlink the c
8c210 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20 69 20  hunk at index i 
8c220 66 72 6f 6d 20 0a 2a 2a 20 77 68 61 74 65 76 65  from .** whateve
8c230 72 20 6c 69 73 74 20 69 73 20 63 75 72 72 65 6e  r list is curren
8c240 74 6c 79 20 61 20 6d 65 6d 62 65 72 20 6f 66 2e  tly a member of.
8c250 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
8c260 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 75 33  memsys3Unlink(u3
8c270 32 20 69 29 7b 0a 20 20 75 33 32 20 73 69 7a 65  2 i){.  u32 size
8c280 2c 20 68 61 73 68 3b 0a 20 20 61 73 73 65 72 74  , hash;.  assert
8c290 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
8c2a0 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29  held(mem3.mutex)
8c2b0 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 6d   );.  assert( (m
8c2c0 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
8c2d0 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 31 29  .hdr.size4x & 1)
8c2e0 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
8c2f0 20 69 3e 3d 31 20 29 3b 0a 20 20 73 69 7a 65 20   i>=1 );.  size 
8c300 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31  = mem3.aPool[i-1
8c310 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34  ].u.hdr.size4x/4
8c320 3b 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  ;.  assert( size
8c330 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73  ==mem3.aPool[i+s
8c340 69 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ize-1].u.hdr.pre
8c350 76 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65 72  vSize );.  asser
8c360 74 28 20 73 69 7a 65 3e 3d 32 20 29 3b 0a 20 20  t( size>=2 );.  
8c370 69 66 28 20 73 69 7a 65 20 3c 3d 20 4d 58 5f 53  if( size <= MX_S
8c380 4d 41 4c 4c 20 29 7b 0a 20 20 20 20 6d 65 6d 73  MALL ){.    mems
8c390 79 73 33 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73  ys3UnlinkFromLis
8c3a0 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53 6d 61  t(i, &mem3.aiSma
8c3b0 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20 20 7d  ll[size-2]);.  }
8c3c0 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68 20 3d  else{.    hash =
8c3d0 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48 3b 0a   size % N_HASH;.
8c3e0 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e      memsys3Unlin
8c3f0 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65  kFromList(i, &me
8c400 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29  m3.aiHash[hash])
8c410 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  ;.  }.}../*.** L
8c420 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61 74  ink the chunk at
8c430 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 20 73   mem3.aPool[i] s
8c440 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68 65  o that is on the
8c450 20 6c 69 73 74 20 72 6f 6f 74 65 64 0a 2a 2a 20   list rooted.** 
8c460 61 74 20 2a 70 52 6f 6f 74 2e 0a 2a 2f 0a 73 74  at *pRoot..*/.st
8c470 61 74 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73  atic void memsys
8c480 33 4c 69 6e 6b 49 6e 74 6f 4c 69 73 74 28 75 33  3LinkIntoList(u3
8c490 32 20 69 2c 20 75 33 32 20 2a 70 52 6f 6f 74 29  2 i, u32 *pRoot)
8c4a0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
8c4b0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
8c4c0 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em3.mutex) );.  
8c4d0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75 2e  mem3.aPool[i].u.
8c4e0 6c 69 73 74 2e 6e 65 78 74 20 3d 20 2a 70 52 6f  list.next = *pRo
8c4f0 6f 74 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ot;.  mem3.aPool
8c500 5b 69 5d 2e 75 2e 6c 69 73 74 2e 70 72 65 76 20  [i].u.list.prev 
8c510 3d 20 30 3b 0a 20 20 69 66 28 20 2a 70 52 6f 6f  = 0;.  if( *pRoo
8c520 74 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 61 50  t ){.    mem3.aP
8c530 6f 6f 6c 5b 2a 70 52 6f 6f 74 5d 2e 75 2e 6c 69  ool[*pRoot].u.li
8c540 73 74 2e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d  st.prev = i;.  }
8c550 0a 20 20 2a 70 52 6f 6f 74 20 3d 20 69 3b 0a 7d  .  *pRoot = i;.}
8c560 0a 0a 2f 2a 0a 2a 2a 20 4c 69 6e 6b 20 74 68 65  ../*.** Link the
8c570 20 63 68 75 6e 6b 20 61 74 20 69 6e 64 65 78 20   chunk at index 
8c580 69 20 69 6e 74 6f 20 65 69 74 68 65 72 20 74 68  i into either th
8c590 65 20 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a  e appropriate.**
8c5a0 20 73 6d 61 6c 6c 20 63 68 75 6e 6b 20 6c 69 73   small chunk lis
8c5b0 74 2c 20 6f 72 20 69 6e 74 6f 20 74 68 65 20 6c  t, or into the l
8c5c0 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20  arge chunk hash 
8c5d0 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  table..*/.static
8c5e0 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 69 6e   void memsys3Lin
8c5f0 6b 28 75 33 32 20 69 29 7b 0a 20 20 75 33 32 20  k(u32 i){.  u32 
8c600 73 69 7a 65 2c 20 68 61 73 68 3b 0a 20 20 61 73  size, hash;.  as
8c610 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
8c620 74 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75  tex_held(mem3.mu
8c630 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
8c640 28 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65  ( i>=1 );.  asse
8c650 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  rt( (mem3.aPool[
8c660 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
8c670 78 20 26 20 31 29 3d 3d 30 20 29 3b 0a 20 20 73  x & 1)==0 );.  s
8c680 69 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c  ize = mem3.aPool
8c690 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65  [i-1].u.hdr.size
8c6a0 34 78 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20  4x/4;.  assert( 
8c6b0 73 69 7a 65 3d 3d 6d 65 6d 33 2e 61 50 6f 6f 6c  size==mem3.aPool
8c6c0 5b 69 2b 73 69 7a 65 2d 31 5d 2e 75 2e 68 64 72  [i+size-1].u.hdr
8c6d0 2e 70 72 65 76 53 69 7a 65 20 29 3b 0a 20 20 61  .prevSize );.  a
8c6e0 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 32 20 29  ssert( size>=2 )
8c6f0 3b 0a 20 20 69 66 28 20 73 69 7a 65 20 3c 3d 20  ;.  if( size <= 
8c700 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20 20  MX_SMALL ){.    
8c710 6d 65 6d 73 79 73 33 4c 69 6e 6b 49 6e 74 6f 4c  memsys3LinkIntoL
8c720 69 73 74 28 69 2c 20 26 6d 65 6d 33 2e 61 69 53  ist(i, &mem3.aiS
8c730 6d 61 6c 6c 5b 73 69 7a 65 2d 32 5d 29 3b 0a 20  mall[size-2]);. 
8c740 20 7d 65 6c 73 65 7b 0a 20 20 20 20 68 61 73 68   }else{.    hash
8c750 20 3d 20 73 69 7a 65 20 25 20 4e 5f 48 41 53 48   = size % N_HASH
8c760 3b 0a 20 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e  ;.    memsys3Lin
8c770 6b 49 6e 74 6f 4c 69 73 74 28 69 2c 20 26 6d 65  kIntoList(i, &me
8c780 6d 33 2e 61 69 48 61 73 68 5b 68 61 73 68 5d 29  m3.aiHash[hash])
8c790 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ;.  }.}../*.** I
8c7a0 66 20 74 68 65 20 53 54 41 54 49 43 5f 4d 45 4d  f the STATIC_MEM
8c7b0 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 61 6c   mutex is not al
8c7c0 72 65 61 64 79 20 68 65 6c 64 2c 20 6f 62 74 61  ready held, obta
8c7d0 69 6e 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 6d  in it now. The m
8c7e0 75 74 65 78 0a 2a 2a 20 77 69 6c 6c 20 61 6c 72  utex.** will alr
8c7f0 65 61 64 79 20 62 65 20 68 65 6c 64 20 28 6f 62  eady be held (ob
8c800 74 61 69 6e 65 64 20 62 79 20 63 6f 64 65 20 69  tained by code i
8c810 6e 20 6d 61 6c 6c 6f 63 2e 63 29 20 69 66 0a 2a  n malloc.c) if.*
8c820 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  * sqlite3GlobalC
8c830 6f 6e 66 69 67 2e 62 4d 65 6d 53 74 61 74 20 69  onfig.bMemStat i
8c840 73 20 74 72 75 65 2e 0a 2a 2f 0a 73 74 61 74 69  s true..*/.stati
8c850 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 45 6e  c void memsys3En
8c860 74 65 72 28 76 6f 69 64 29 7b 0a 20 20 69 66 28  ter(void){.  if(
8c870 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
8c880 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 3d 3d 30  nfig.bMemstat==0
8c890 20 26 26 20 6d 65 6d 33 2e 6d 75 74 65 78 3d 3d   && mem3.mutex==
8c8a0 30 20 29 7b 0a 20 20 20 20 6d 65 6d 33 2e 6d 75  0 ){.    mem3.mu
8c8b0 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  tex = sqlite3Mut
8c8c0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
8c8d0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29  UTEX_STATIC_MEM)
8c8e0 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
8c8f0 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 33  mutex_enter(mem3
8c900 2e 6d 75 74 65 78 29 3b 0a 7d 0a 73 74 61 74 69  .mutex);.}.stati
8c910 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4c 65  c void memsys3Le
8c920 61 76 65 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c  ave(void){.  sql
8c930 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
8c940 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 7d 0a  (mem3.mutex);.}.
8c950 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20 77 68  ./*.** Called wh
8c960 65 6e 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65  en we are unable
8c970 20 74 6f 20 73 61 74 69 73 66 79 20 61 6e 20 61   to satisfy an a
8c980 6c 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 6e 42 79  llocation of nBy
8c990 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tes..*/.static v
8c9a0 6f 69 64 20 6d 65 6d 73 79 73 33 4f 75 74 4f 66  oid memsys3OutOf
8c9b0 4d 65 6d 6f 72 79 28 69 6e 74 20 6e 42 79 74 65  Memory(int nByte
8c9c0 29 7b 0a 20 20 69 66 28 20 21 6d 65 6d 33 2e 61  ){.  if( !mem3.a
8c9d0 6c 61 72 6d 42 75 73 79 20 29 7b 0a 20 20 20 20  larmBusy ){.    
8c9e0 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73 79 20 3d  mem3.alarmBusy =
8c9f0 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   1;.    assert( 
8ca00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
8ca10 6c 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29  ld(mem3.mutex) )
8ca20 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
8ca30 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e 6d  tex_leave(mem3.m
8ca40 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74  utex);.    sqlit
8ca50 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
8ca60 79 28 6e 42 79 74 65 29 3b 0a 20 20 20 20 73 71  y(nByte);.    sq
8ca70 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
8ca80 72 28 6d 65 6d 33 2e 6d 75 74 65 78 29 3b 0a 20  r(mem3.mutex);. 
8ca90 20 20 20 6d 65 6d 33 2e 61 6c 61 72 6d 42 75 73     mem3.alarmBus
8caa0 79 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f  y = 0;.  }.}.../
8cab0 2a 0a 2a 2a 20 43 68 75 6e 6b 20 69 20 69 73 20  *.** Chunk i is 
8cac0 61 20 66 72 65 65 20 63 68 75 6e 6b 20 74 68 61  a free chunk tha
8cad0 74 20 68 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e  t has been unlin
8cae0 6b 65 64 2e 20 20 41 64 6a 75 73 74 20 69 74 73  ked.  Adjust its
8caf0 20 0a 2a 2a 20 73 69 7a 65 20 70 61 72 61 6d 65   .** size parame
8cb00 74 65 72 73 20 66 6f 72 20 63 68 65 63 6b 2d 6f  ters for check-o
8cb10 75 74 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20  ut and return a 
8cb20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 0a  pointer to the .
8cb30 2a 2a 20 75 73 65 72 20 70 6f 72 74 69 6f 6e 20  ** user portion 
8cb40 6f 66 20 74 68 65 20 63 68 75 6e 6b 2e 0a 2a 2f  of the chunk..*/
8cb50 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d 65  .static void *me
8cb60 6d 73 79 73 33 43 68 65 63 6b 6f 75 74 28 75 33  msys3Checkout(u3
8cb70 32 20 69 2c 20 75 33 32 20 6e 42 6c 6f 63 6b 29  2 i, u32 nBlock)
8cb80 7b 0a 20 20 75 33 32 20 78 3b 0a 20 20 61 73 73  {.  u32 x;.  ass
8cb90 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
8cba0 65 78 5f 68 65 6c 64 28 6d 65 6d 33 2e 6d 75 74  ex_held(mem3.mut
8cbb0 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
8cbc0 20 69 3e 3d 31 20 29 3b 0a 20 20 61 73 73 65 72   i>=1 );.  asser
8cbd0 74 28 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d  t( mem3.aPool[i-
8cbe0 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f  1].u.hdr.size4x/
8cbf0 34 3d 3d 6e 42 6c 6f 63 6b 20 29 3b 0a 20 20 61  4==nBlock );.  a
8cc00 73 73 65 72 74 28 20 6d 65 6d 33 2e 61 50 6f 6f  ssert( mem3.aPoo
8cc10 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e  l[i+nBlock-1].u.
8cc20 68 64 72 2e 70 72 65 76 53 69 7a 65 3d 3d 6e 42  hdr.prevSize==nB
8cc30 6c 6f 63 6b 20 29 3b 0a 20 20 78 20 3d 20 6d 65  lock );.  x = me
8cc40 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8cc50 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 6d 65  hdr.size4x;.  me
8cc60 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e  m3.aPool[i-1].u.
8cc70 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6e 42 6c  hdr.size4x = nBl
8cc80 6f 63 6b 2a 34 20 7c 20 31 20 7c 20 28 78 26 32  ock*4 | 1 | (x&2
8cc90 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  );.  mem3.aPool[
8cca0 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e 68 64  i+nBlock-1].u.hd
8ccb0 72 2e 70 72 65 76 53 69 7a 65 20 3d 20 6e 42 6c  r.prevSize = nBl
8ccc0 6f 63 6b 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  ock;.  mem3.aPoo
8ccd0 6c 5b 69 2b 6e 42 6c 6f 63 6b 2d 31 5d 2e 75 2e  l[i+nBlock-1].u.
8cce0 68 64 72 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b  hdr.size4x |= 2;
8ccf0 0a 20 20 72 65 74 75 72 6e 20 26 6d 65 6d 33 2e  .  return &mem3.
8cd00 61 50 6f 6f 6c 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a  aPool[i];.}../*.
8cd10 2a 2a 20 43 61 72 76 65 20 61 20 70 69 65 63 65  ** Carve a piece
8cd20 20 6f 66 66 20 6f 66 20 74 68 65 20 65 6e 64 20   off of the end 
8cd30 6f 66 20 74 68 65 20 6d 65 6d 33 2e 69 4d 61 73  of the mem3.iMas
8cd40 74 65 72 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a  ter free chunk..
8cd50 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  ** Return a poin
8cd60 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 20 61  ter to the new a
8cd70 6c 6c 6f 63 61 74 69 6f 6e 2e 20 20 4f 72 2c 20  llocation.  Or, 
8cd80 69 66 20 74 68 65 20 6d 61 73 74 65 72 20 63 68  if the master ch
8cd90 75 6e 6b 0a 2a 2a 20 69 73 20 6e 6f 74 20 6c 61  unk.** is not la
8cda0 72 67 65 20 65 6e 6f 75 67 68 2c 20 72 65 74 75  rge enough, retu
8cdb0 72 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rn 0..*/.static 
8cdc0 76 6f 69 64 20 2a 6d 65 6d 73 79 73 33 46 72 6f  void *memsys3Fro
8cdd0 6d 4d 61 73 74 65 72 28 75 33 32 20 6e 42 6c 6f  mMaster(u32 nBlo
8cde0 63 6b 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73  ck){.  assert( s
8cdf0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8ce00 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
8ce10 0a 20 20 61 73 73 65 72 74 28 20 6d 65 6d 33 2e  .  assert( mem3.
8ce20 73 7a 4d 61 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b  szMaster>=nBlock
8ce30 20 29 3b 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b   );.  if( nBlock
8ce40 3e 3d 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d  >=mem3.szMaster-
8ce50 31 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 65 20  1 ){.    /* Use 
8ce60 74 68 65 20 65 6e 74 69 72 65 20 6d 61 73 74 65  the entire maste
8ce70 72 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70  r */.    void *p
8ce80 20 3d 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f   = memsys3Checko
8ce90 75 74 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2c  ut(mem3.iMaster,
8cea0 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b   mem3.szMaster);
8ceb0 0a 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74 65  .    mem3.iMaste
8cec0 72 20 3d 20 30 3b 0a 20 20 20 20 6d 65 6d 33 2e  r = 0;.    mem3.
8ced0 73 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  szMaster = 0;.  
8cee0 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72 20    mem3.mnMaster 
8cef0 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 0;.    return 
8cf00 70 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  p;.  }else{.    
8cf10 2f 2a 20 53 70 6c 69 74 20 74 68 65 20 6d 61 73  /* Split the mas
8cf20 74 65 72 20 62 6c 6f 63 6b 2e 20 20 52 65 74 75  ter block.  Retu
8cf30 72 6e 20 74 68 65 20 74 61 69 6c 2e 20 2a 2f 0a  rn the tail. */.
8cf40 20 20 20 20 75 33 32 20 6e 65 77 69 2c 20 78 3b      u32 newi, x;
8cf50 0a 20 20 20 20 6e 65 77 69 20 3d 20 6d 65 6d 33  .    newi = mem3
8cf60 2e 69 4d 61 73 74 65 72 20 2b 20 6d 65 6d 33 2e  .iMaster + mem3.
8cf70 73 7a 4d 61 73 74 65 72 20 2d 20 6e 42 6c 6f 63  szMaster - nBloc
8cf80 6b 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e  k;.    assert( n
8cf90 65 77 69 20 3e 20 6d 65 6d 33 2e 69 4d 61 73 74  ewi > mem3.iMast
8cfa0 65 72 2b 31 20 29 3b 0a 20 20 20 20 6d 65 6d 33  er+1 );.    mem3
8cfb0 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73  .aPool[mem3.iMas
8cfc0 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  ter+mem3.szMaste
8cfd0 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53  r-1].u.hdr.prevS
8cfe0 69 7a 65 20 3d 20 6e 42 6c 6f 63 6b 3b 0a 20 20  ize = nBlock;.  
8cff0 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d    mem3.aPool[mem
8d000 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d 33 2e 73  3.iMaster+mem3.s
8d010 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72  zMaster-1].u.hdr
8d020 2e 73 69 7a 65 34 78 20 7c 3d 20 32 3b 0a 20 20  .size4x |= 2;.  
8d030 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65 77    mem3.aPool[new
8d040 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
8d050 78 20 3d 20 6e 42 6c 6f 63 6b 2a 34 20 2b 20 31  x = nBlock*4 + 1
8d060 3b 0a 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73  ;.    mem3.szMas
8d070 74 65 72 20 2d 3d 20 6e 42 6c 6f 63 6b 3b 0a 20  ter -= nBlock;. 
8d080 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6e 65     mem3.aPool[ne
8d090 77 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  wi-1].u.hdr.prev
8d0a0 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61  Size = mem3.szMa
8d0b0 73 74 65 72 3b 0a 20 20 20 20 78 20 3d 20 6d 65  ster;.    x = me
8d0c0 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8d0d0 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73  aster-1].u.hdr.s
8d0e0 69 7a 65 34 78 20 26 20 32 3b 0a 20 20 20 20 6d  ize4x & 2;.    m
8d0f0 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69  em3.aPool[mem3.i
8d100 4d 61 73 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e  Master-1].u.hdr.
8d110 73 69 7a 65 34 78 20 3d 20 6d 65 6d 33 2e 73 7a  size4x = mem3.sz
8d120 4d 61 73 74 65 72 2a 34 20 7c 20 78 3b 0a 20 20  Master*4 | x;.  
8d130 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73    if( mem3.szMas
8d140 74 65 72 20 3c 20 6d 65 6d 33 2e 6d 6e 4d 61 73  ter < mem3.mnMas
8d150 74 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ter ){.      mem
8d160 33 2e 6d 6e 4d 61 73 74 65 72 20 3d 20 6d 65 6d  3.mnMaster = mem
8d170 33 2e 73 7a 4d 61 73 74 65 72 3b 0a 20 20 20 20  3.szMaster;.    
8d180 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 76 6f  }.    return (vo
8d190 69 64 2a 29 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  id*)&mem3.aPool[
8d1a0 6e 65 77 69 5d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  newi];.  }.}../*
8d1b0 0a 2a 2a 20 2a 70 52 6f 6f 74 20 69 73 20 74 68  .** *pRoot is th
8d1c0 65 20 68 65 61 64 20 6f 66 20 61 20 6c 69 73 74  e head of a list
8d1d0 20 6f 66 20 66 72 65 65 20 63 68 75 6e 6b 73 20   of free chunks 
8d1e0 6f 66 20 74 68 65 20 73 61 6d 65 20 73 69 7a 65  of the same size
8d1f0 0a 2a 2a 20 6f 72 20 73 61 6d 65 20 73 69 7a 65  .** or same size
8d200 20 68 61 73 68 2e 20 20 49 6e 20 6f 74 68 65 72   hash.  In other
8d210 20 77 6f 72 64 73 2c 20 2a 70 52 6f 6f 74 20 69   words, *pRoot i
8d220 73 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 65 69  s an entry in ei
8d230 74 68 65 72 0a 2a 2a 20 6d 65 6d 33 2e 61 69 53  ther.** mem3.aiS
8d240 6d 61 6c 6c 5b 5d 20 6f 72 20 6d 65 6d 33 2e 61  mall[] or mem3.a
8d250 69 48 61 73 68 5b 5d 2e 20 20 0a 2a 2a 0a 2a 2a  iHash[].  .**.**
8d260 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 65 78   This routine ex
8d270 61 6d 69 6e 65 73 20 61 6c 6c 20 65 6e 74 72 69  amines all entri
8d280 65 73 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20  es on the given 
8d290 6c 69 73 74 20 61 6e 64 20 74 72 69 65 73 0a 2a  list and tries.*
8d2a0 2a 20 74 6f 20 63 6f 61 6c 65 73 63 65 20 65 61  * to coalesce ea
8d2b0 63 68 20 65 6e 74 72 69 65 73 20 77 69 74 68 20  ch entries with 
8d2c0 61 64 6a 61 63 65 6e 74 20 66 72 65 65 20 63 68  adjacent free ch
8d2d0 75 6e 6b 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66  unks.  .**.** If
8d2e0 20 69 74 20 73 65 65 73 20 61 20 63 68 75 6e 6b   it sees a chunk
8d2f0 20 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20   that is larger 
8d300 74 68 61 6e 20 6d 65 6d 33 2e 69 4d 61 73 74 65  than mem3.iMaste
8d310 72 2c 20 69 74 20 72 65 70 6c 61 63 65 73 20 0a  r, it replaces .
8d320 2a 2a 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d  ** the current m
8d330 65 6d 33 2e 69 4d 61 73 74 65 72 20 77 69 74 68  em3.iMaster with
8d340 20 74 68 65 20 6e 65 77 20 6c 61 72 67 65 72 20   the new larger 
8d350 63 68 75 6e 6b 2e 20 20 49 6e 20 6f 72 64 65 72  chunk.  In order
8d360 20 66 6f 72 0a 2a 2a 20 74 68 69 73 20 6d 65 6d   for.** this mem
8d370 33 2e 69 4d 61 73 74 65 72 20 72 65 70 6c 61 63  3.iMaster replac
8d380 65 6d 65 6e 74 20 74 6f 20 77 6f 72 6b 2c 20 74  ement to work, t
8d390 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 20  he master chunk 
8d3a0 6d 75 73 74 20 62 65 0a 2a 2a 20 6c 69 6e 6b 65  must be.** linke
8d3b0 64 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20  d into the hash 
8d3c0 74 61 62 6c 65 73 2e 20 20 54 68 61 74 20 69 73  tables.  That is
8d3d0 20 6e 6f 74 20 74 68 65 20 6e 6f 72 6d 61 6c 20   not the normal 
8d3e0 73 74 61 74 65 20 6f 66 0a 2a 2a 20 61 66 66 61  state of.** affa
8d3f0 69 72 73 2c 20 6f 66 20 63 6f 75 72 73 65 2e 20  irs, of course. 
8d400 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75   The calling rou
8d410 74 69 6e 65 20 6d 75 73 74 20 6c 69 6e 6b 20 74  tine must link t
8d420 68 65 20 6d 61 73 74 65 72 0a 2a 2a 20 63 68 75  he master.** chu
8d430 6e 6b 20 62 65 66 6f 72 65 20 69 6e 76 6f 6b 69  nk before invoki
8d440 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2c  ng this routine,
8d450 20 74 68 65 6e 20 6d 75 73 74 20 75 6e 6c 69 6e   then must unlin
8d460 6b 20 74 68 65 20 28 70 6f 73 73 69 62 6c 79 0a  k the (possibly.
8d470 2a 2a 20 63 68 61 6e 67 65 64 29 20 6d 61 73 74  ** changed) mast
8d480 65 72 20 63 68 75 6e 6b 20 6f 6e 63 65 20 74 68  er chunk once th
8d490 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 66  is routine has f
8d4a0 69 6e 69 73 68 65 64 2e 0a 2a 2f 0a 73 74 61 74  inished..*/.stat
8d4b0 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 4d  ic void memsys3M
8d4c0 65 72 67 65 28 75 33 32 20 2a 70 52 6f 6f 74 29  erge(u32 *pRoot)
8d4d0 7b 0a 20 20 75 33 32 20 69 4e 65 78 74 2c 20 70  {.  u32 iNext, p
8d4e0 72 65 76 2c 20 73 69 7a 65 2c 20 69 2c 20 78 3b  rev, size, i, x;
8d4f0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
8d500 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d  te3_mutex_held(m
8d510 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20  em3.mutex) );.  
8d520 66 6f 72 28 69 3d 2a 70 52 6f 6f 74 3b 20 69 3e  for(i=*pRoot; i>
8d530 30 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20 20  0; i=iNext){.   
8d540 20 69 4e 65 78 74 20 3d 20 6d 65 6d 33 2e 61 50   iNext = mem3.aP
8d550 6f 6f 6c 5b 69 5d 2e 75 2e 6c 69 73 74 2e 6e 65  ool[i].u.list.ne
8d560 78 74 3b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d  xt;.    size = m
8d570 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75  em3.aPool[i-1].u
8d580 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20 20  .hdr.size4x;.   
8d590 20 61 73 73 65 72 74 28 20 28 73 69 7a 65 26 31   assert( (size&1
8d5a0 29 3d 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  )==0 );.    if( 
8d5b0 28 73 69 7a 65 26 32 29 3d 3d 30 20 29 7b 0a 20  (size&2)==0 ){. 
8d5c0 20 20 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69       memsys3Unli
8d5d0 6e 6b 46 72 6f 6d 4c 69 73 74 28 69 2c 20 70 52  nkFromList(i, pR
8d5e0 6f 6f 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65  oot);.      asse
8d5f0 72 74 28 20 69 20 3e 20 6d 65 6d 33 2e 61 50 6f  rt( i > mem3.aPo
8d600 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 70 72  ol[i-1].u.hdr.pr
8d610 65 76 53 69 7a 65 20 29 3b 0a 20 20 20 20 20 20  evSize );.      
8d620 70 72 65 76 20 3d 20 69 20 2d 20 6d 65 6d 33 2e  prev = i - mem3.
8d630 61 50 6f 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72  aPool[i-1].u.hdr
8d640 2e 70 72 65 76 53 69 7a 65 3b 0a 20 20 20 20 20  .prevSize;.     
8d650 20 69 66 28 20 70 72 65 76 3d 3d 69 4e 65 78 74   if( prev==iNext
8d660 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4e 65 78   ){.        iNex
8d670 74 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 70  t = mem3.aPool[p
8d680 72 65 76 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  rev].u.list.next
8d690 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
8d6a0 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 70 72  memsys3Unlink(pr
8d6b0 65 76 29 3b 0a 20 20 20 20 20 20 73 69 7a 65 20  ev);.      size 
8d6c0 3d 20 69 20 2b 20 73 69 7a 65 2f 34 20 2d 20 70  = i + size/4 - p
8d6d0 72 65 76 3b 0a 20 20 20 20 20 20 78 20 3d 20 6d  rev;.      x = m
8d6e0 65 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2d 31  em3.aPool[prev-1
8d6f0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26  ].u.hdr.size4x &
8d700 20 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61   2;.      mem3.a
8d710 50 6f 6f 6c 5b 70 72 65 76 2d 31 5d 2e 75 2e 68  Pool[prev-1].u.h
8d720 64 72 2e 73 69 7a 65 34 78 20 3d 20 73 69 7a 65  dr.size4x = size
8d730 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65  *4 | x;.      me
8d740 6d 33 2e 61 50 6f 6f 6c 5b 70 72 65 76 2b 73 69  m3.aPool[prev+si
8d750 7a 65 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76  ze-1].u.hdr.prev
8d760 53 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20  Size = size;.   
8d770 20 20 20 6d 65 6d 73 79 73 33 4c 69 6e 6b 28 70     memsys3Link(p
8d780 72 65 76 29 3b 0a 20 20 20 20 20 20 69 20 3d 20  rev);.      i = 
8d790 70 72 65 76 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  prev;.    }else{
8d7a0 0a 20 20 20 20 20 20 73 69 7a 65 20 2f 3d 20 34  .      size /= 4
8d7b0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
8d7c0 73 69 7a 65 3e 6d 65 6d 33 2e 73 7a 4d 61 73 74  size>mem3.szMast
8d7d0 65 72 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 33  er ){.      mem3
8d7e0 2e 69 4d 61 73 74 65 72 20 3d 20 69 3b 0a 20 20  .iMaster = i;.  
8d7f0 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65      mem3.szMaste
8d800 72 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a  r = size;.    }.
8d810 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
8d820 75 72 6e 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d  urn a block of m
8d830 65 6d 6f 72 79 20 6f 66 20 61 74 20 6c 65 61 73  emory of at leas
8d840 74 20 6e 42 79 74 65 73 20 69 6e 20 73 69 7a 65  t nBytes in size
8d850 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c  ..** Return NULL
8d860 20 69 66 20 75 6e 61 62 6c 65 2e 0a 2a 2a 0a 2a   if unable..**.*
8d870 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
8d880 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
8d890 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78   necessary mutex
8d8a0 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a  es, if any, are.
8d8b0 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20  ** already held 
8d8c0 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48  by the caller. H
8d8d0 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a  ence "Unsafe"..*
8d8e0 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 6d  /.static void *m
8d8f0 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61  emsys3MallocUnsa
8d900 66 65 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20  fe(int nByte){. 
8d910 20 75 33 32 20 69 3b 0a 20 20 75 33 32 20 6e 42   u32 i;.  u32 nB
8d920 6c 6f 63 6b 3b 0a 20 20 75 33 32 20 74 6f 46 72  lock;.  u32 toFr
8d930 65 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ee;..  assert( s
8d940 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
8d950 64 28 6d 65 6d 33 2e 6d 75 74 65 78 29 20 29 3b  d(mem3.mutex) );
8d960 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
8d970 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38 20  f(Mem3Block)==8 
8d980 29 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 3c 3d  );.  if( nByte<=
8d990 31 32 20 29 7b 0a 20 20 20 20 6e 42 6c 6f 63 6b  12 ){.    nBlock
8d9a0 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 2;.  }else{. 
8d9b0 20 20 20 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79     nBlock = (nBy
8d9c0 74 65 20 2b 20 31 31 29 2f 38 3b 0a 20 20 7d 0a  te + 11)/8;.  }.
8d9d0 20 20 61 73 73 65 72 74 28 20 6e 42 6c 6f 63 6b    assert( nBlock
8d9e0 3e 3d 32 20 29 3b 0a 0a 20 20 2f 2a 20 53 54 45  >=2 );..  /* STE
8d9f0 50 20 31 3a 0a 20 20 2a 2a 20 4c 6f 6f 6b 20 66  P 1:.  ** Look f
8da00 6f 72 20 61 6e 20 65 6e 74 72 79 20 6f 66 20 74  or an entry of t
8da10 68 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20  he correct size 
8da20 69 6e 20 65 69 74 68 65 72 20 74 68 65 20 73 6d  in either the sm
8da30 61 6c 6c 0a 20 20 2a 2a 20 63 68 75 6e 6b 20 74  all.  ** chunk t
8da40 61 62 6c 65 20 6f 72 20 69 6e 20 74 68 65 20 6c  able or in the l
8da50 61 72 67 65 20 63 68 75 6e 6b 20 68 61 73 68 20  arge chunk hash 
8da60 74 61 62 6c 65 2e 20 20 54 68 69 73 20 69 73 0a  table.  This is.
8da70 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
8da80 6d 6f 73 74 20 6f 66 20 74 68 65 20 74 69 6d 65  most of the time
8da90 20 28 61 62 6f 75 74 20 39 20 74 69 6d 65 73 20   (about 9 times 
8daa0 6f 75 74 20 6f 66 20 31 30 29 2e 0a 20 20 2a 2f  out of 10)..  */
8dab0 0a 20 20 69 66 28 20 6e 42 6c 6f 63 6b 20 3c 3d  .  if( nBlock <=
8dac0 20 4d 58 5f 53 4d 41 4c 4c 20 29 7b 0a 20 20 20   MX_SMALL ){.   
8dad0 20 69 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61 6c   i = mem3.aiSmal
8dae0 6c 5b 6e 42 6c 6f 63 6b 2d 32 5d 3b 0a 20 20 20  l[nBlock-2];.   
8daf0 20 69 66 28 20 69 3e 30 20 29 7b 0a 20 20 20 20   if( i>0 ){.    
8db00 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 46    memsys3UnlinkF
8db10 72 6f 6d 4c 69 73 74 28 69 2c 20 26 6d 65 6d 33  romList(i, &mem3
8db20 2e 61 69 53 6d 61 6c 6c 5b 6e 42 6c 6f 63 6b 2d  .aiSmall[nBlock-
8db30 32 5d 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  2]);.      retur
8db40 6e 20 6d 65 6d 73 79 73 33 43 68 65 63 6b 6f 75  n memsys3Checkou
8db50 74 28 69 2c 20 6e 42 6c 6f 63 6b 29 3b 0a 20 20  t(i, nBlock);.  
8db60 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
8db70 20 69 6e 74 20 68 61 73 68 20 3d 20 6e 42 6c 6f   int hash = nBlo
8db80 63 6b 20 25 20 4e 5f 48 41 53 48 3b 0a 20 20 20  ck % N_HASH;.   
8db90 20 66 6f 72 28 69 3d 6d 65 6d 33 2e 61 69 48 61   for(i=mem3.aiHa
8dba0 73 68 5b 68 61 73 68 5d 3b 20 69 3e 30 3b 20 69  sh[hash]; i>0; i
8dbb0 3d 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2e 75  =mem3.aPool[i].u
8dbc0 2e 6c 69 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20  .list.next){.   
8dbd0 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 50 6f 6f     if( mem3.aPoo
8dbe0 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[i-1].u.hdr.siz
8dbf0 65 34 78 2f 34 3d 3d 6e 42 6c 6f 63 6b 20 29 7b  e4x/4==nBlock ){
8dc00 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 79 73 33  .        memsys3
8dc10 55 6e 6c 69 6e 6b 46 72 6f 6d 4c 69 73 74 28 69  UnlinkFromList(i
8dc20 2c 20 26 6d 65 6d 33 2e 61 69 48 61 73 68 5b 68  , &mem3.aiHash[h
8dc30 61 73 68 5d 29 3b 0a 20 20 20 20 20 20 20 20 72  ash]);.        r
8dc40 65 74 75 72 6e 20 6d 65 6d 73 79 73 33 43 68 65  eturn memsys3Che
8dc50 63 6b 6f 75 74 28 69 2c 20 6e 42 6c 6f 63 6b 29  ckout(i, nBlock)
8dc60 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
8dc70 20 20 7d 0a 0a 20 20 2f 2a 20 53 54 45 50 20 32    }..  /* STEP 2
8dc80 3a 0a 20 20 2a 2a 20 54 72 79 20 74 6f 20 73 61  :.  ** Try to sa
8dc90 74 69 73 66 79 20 74 68 65 20 61 6c 6c 6f 63 61  tisfy the alloca
8dca0 74 69 6f 6e 20 62 79 20 63 61 72 76 69 6e 67 20  tion by carving 
8dcb0 61 20 70 69 65 63 65 20 6f 66 66 20 6f 66 20 74  a piece off of t
8dcc0 68 65 20 65 6e 64 0a 20 20 2a 2a 20 6f 66 20 74  he end.  ** of t
8dcd0 68 65 20 6d 61 73 74 65 72 20 63 68 75 6e 6b 2e  he master chunk.
8dce0 20 20 54 68 69 73 20 73 74 65 70 20 75 73 75 61    This step usua
8dcf0 6c 6c 79 20 77 6f 72 6b 73 20 69 66 20 73 74 65  lly works if ste
8dd00 70 20 31 20 66 61 69 6c 73 2e 0a 20 20 2a 2f 0a  p 1 fails..  */.
8dd10 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61 73    if( mem3.szMas
8dd20 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a 20  ter>=nBlock ){. 
8dd30 20 20 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73     return memsys
8dd40 33 46 72 6f 6d 4d 61 73 74 65 72 28 6e 42 6c 6f  3FromMaster(nBlo
8dd50 63 6b 29 3b 0a 20 20 7d 0a 0a 0a 20 20 2f 2a 20  ck);.  }...  /* 
8dd60 53 54 45 50 20 33 3a 20 20 0a 20 20 2a 2a 20 4c  STEP 3:  .  ** L
8dd70 6f 6f 70 20 74 68 72 6f 75 67 68 20 74 68 65 20  oop through the 
8dd80 65 6e 74 69 72 65 20 6d 65 6d 6f 72 79 20 70 6f  entire memory po
8dd90 6f 6c 2e 20 20 43 6f 61 6c 65 73 63 65 20 61 64  ol.  Coalesce ad
8dda0 6a 61 63 65 6e 74 20 66 72 65 65 0a 20 20 2a 2a  jacent free.  **
8ddb0 20 63 68 75 6e 6b 73 2e 20 20 52 65 63 6f 6d 70   chunks.  Recomp
8ddc0 75 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 63  ute the master c
8ddd0 68 75 6e 6b 20 61 73 20 74 68 65 20 6c 61 72 67  hunk as the larg
8dde0 65 73 74 20 66 72 65 65 20 63 68 75 6e 6b 2e 0a  est free chunk..
8ddf0 20 20 2a 2a 20 54 68 65 6e 20 74 72 79 20 61 67    ** Then try ag
8de00 61 69 6e 20 74 6f 20 73 61 74 69 73 66 79 20 74  ain to satisfy t
8de10 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 62 79  he allocation by
8de20 20 63 61 72 76 69 6e 67 20 61 20 70 69 65 63 65   carving a piece
8de30 20 6f 66 66 0a 20 20 2a 2a 20 6f 66 20 74 68 65   off.  ** of the
8de40 20 65 6e 64 20 6f 66 20 74 68 65 20 6d 61 73 74   end of the mast
8de50 65 72 20 63 68 75 6e 6b 2e 20 20 54 68 69 73 20  er chunk.  This 
8de60 73 74 65 70 20 68 61 70 70 65 6e 73 20 76 65 72  step happens ver
8de70 79 0a 20 20 2a 2a 20 72 61 72 65 6c 79 20 28 77  y.  ** rarely (w
8de80 65 20 68 6f 70 65 21 29 0a 20 20 2a 2f 0a 20 20  e hope!).  */.  
8de90 66 6f 72 28 74 6f 46 72 65 65 3d 6e 42 6c 6f 63  for(toFree=nBloc
8dea0 6b 2a 31 36 3b 20 74 6f 46 72 65 65 3c 28 6d 65  k*16; toFree<(me
8deb0 6d 33 2e 6e 50 6f 6f 6c 2a 31 36 29 3b 20 74 6f  m3.nPool*16); to
8dec0 46 72 65 65 20 2a 3d 20 32 29 7b 0a 20 20 20 20  Free *= 2){.    
8ded0 6d 65 6d 73 79 73 33 4f 75 74 4f 66 4d 65 6d 6f  memsys3OutOfMemo
8dee0 72 79 28 74 6f 46 72 65 65 29 3b 0a 20 20 20 20  ry(toFree);.    
8def0 69 66 28 20 6d 65 6d 33 2e 69 4d 61 73 74 65 72  if( mem3.iMaster
8df00 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79 73   ){.      memsys
8df10 33 4c 69 6e 6b 28 6d 65 6d 33 2e 69 4d 61 73 74  3Link(mem3.iMast
8df20 65 72 29 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e  er);.      mem3.
8df30 69 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20  iMaster = 0;.   
8df40 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72     mem3.szMaster
8df50 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20   = 0;.    }.    
8df60 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48 41 53  for(i=0; i<N_HAS
8df70 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d  H; i++){.      m
8df80 65 6d 73 79 73 33 4d 65 72 67 65 28 26 6d 65 6d  emsys3Merge(&mem
8df90 33 2e 61 69 48 61 73 68 5b 69 5d 29 3b 0a 20 20  3.aiHash[i]);.  
8dfa0 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b    }.    for(i=0;
8dfb0 20 69 3c 4d 58 5f 53 4d 41 4c 4c 2d 31 3b 20 69   i<MX_SMALL-1; i
8dfc0 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 65 6d 73 79  ++){.      memsy
8dfd0 73 33 4d 65 72 67 65 28 26 6d 65 6d 33 2e 61 69  s3Merge(&mem3.ai
8dfe0 53 6d 61 6c 6c 5b 69 5d 29 3b 0a 20 20 20 20 7d  Small[i]);.    }
8dff0 0a 20 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a  .    if( mem3.sz
8e000 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20  Master ){.      
8e010 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65  memsys3Unlink(me
8e020 6d 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20  m3.iMaster);.   
8e030 20 20 20 69 66 28 20 6d 65 6d 33 2e 73 7a 4d 61     if( mem3.szMa
8e040 73 74 65 72 3e 3d 6e 42 6c 6f 63 6b 20 29 7b 0a  ster>=nBlock ){.
8e050 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 6d          return m
8e060 65 6d 73 79 73 33 46 72 6f 6d 4d 61 73 74 65 72  emsys3FromMaster
8e070 28 6e 42 6c 6f 63 6b 29 3b 0a 20 20 20 20 20 20  (nBlock);.      
8e080 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  }.    }.  }..  /
8e090 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68 65  * If none of the
8e0a0 20 61 62 6f 76 65 20 77 6f 72 6b 65 64 2c 20 74   above worked, t
8e0b0 68 65 6e 20 77 65 20 66 61 69 6c 2e 20 2a 2f 0a  hen we fail. */.
8e0c0 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
8e0d0 2a 0a 2a 2a 20 46 72 65 65 20 61 6e 20 6f 75 74  *.** Free an out
8e0e0 73 74 61 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20  standing memory 
8e0f0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a  allocation..**.*
8e100 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
8e110 61 73 73 75 6d 65 73 20 74 68 61 74 20 74 68 65  assumes that the
8e120 20 6e 65 63 65 73 73 61 72 79 20 6d 75 74 65 78   necessary mutex
8e130 65 73 2c 20 69 66 20 61 6e 79 2c 20 61 72 65 0a  es, if any, are.
8e140 2a 2a 20 61 6c 72 65 61 64 79 20 68 65 6c 64 20  ** already held 
8e150 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 20 48  by the caller. H
8e160 65 6e 63 65 20 22 55 6e 73 61 66 65 22 2e 0a 2a  ence "Unsafe"..*
8e170 2f 0a 76 6f 69 64 20 6d 65 6d 73 79 73 33 46 72  /.void memsys3Fr
8e180 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a 70  eeUnsafe(void *p
8e190 4f 6c 64 29 7b 0a 20 20 4d 65 6d 33 42 6c 6f 63  Old){.  Mem3Bloc
8e1a0 6b 20 2a 70 20 3d 20 28 4d 65 6d 33 42 6c 6f 63  k *p = (Mem3Bloc
8e1b0 6b 2a 29 70 4f 6c 64 3b 0a 20 20 69 6e 74 20 69  k*)pOld;.  int i
8e1c0 3b 0a 20 20 75 33 32 20 73 69 7a 65 2c 20 78 3b  ;.  u32 size, x;
8e1d0 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
8e1e0 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65  e3_mutex_held(me
8e1f0 6d 33 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  m3.mutex) );.  a
8e200 73 73 65 72 74 28 20 70 3e 6d 65 6d 33 2e 61 50  ssert( p>mem3.aP
8e210 6f 6f 6c 20 26 26 20 70 3c 26 6d 65 6d 33 2e 61  ool && p<&mem3.a
8e220 50 6f 6f 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d  Pool[mem3.nPool]
8e230 20 29 3b 0a 20 20 69 20 3d 20 70 20 2d 20 6d 65   );.  i = p - me
8e240 6d 33 2e 61 50 6f 6f 6c 3b 0a 20 20 61 73 73 65  m3.aPool;.  asse
8e250 72 74 28 20 28 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  rt( (mem3.aPool[
8e260 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34  i-1].u.hdr.size4
8e270 78 26 31 29 3d 3d 31 20 29 3b 0a 20 20 73 69 7a  x&1)==1 );.  siz
8e280 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69  e = mem3.aPool[i
8e290 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8e2a0 2f 34 3b 0a 20 20 61 73 73 65 72 74 28 20 69 2b  /4;.  assert( i+
8e2b0 73 69 7a 65 3c 3d 6d 65 6d 33 2e 6e 50 6f 6f 6c  size<=mem3.nPool
8e2c0 2b 31 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f  +1 );.  mem3.aPo
8e2d0 6f 6c 5b 69 2d 31 5d 2e 75 2e 68 64 72 2e 73 69  ol[i-1].u.hdr.si
8e2e0 7a 65 34 78 20 26 3d 20 7e 31 3b 0a 20 20 6d 65  ze4x &= ~1;.  me
8e2f0 6d 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d  m3.aPool[i+size-
8e300 31 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a  1].u.hdr.prevSiz
8e310 65 20 3d 20 73 69 7a 65 3b 0a 20 20 6d 65 6d 33  e = size;.  mem3
8e320 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2d 31 5d  .aPool[i+size-1]
8e330 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 3d  .u.hdr.size4x &=
8e340 20 7e 32 3b 0a 20 20 6d 65 6d 73 79 73 33 4c 69   ~2;.  memsys3Li
8e350 6e 6b 28 69 29 3b 0a 0a 20 20 2f 2a 20 54 72 79  nk(i);..  /* Try
8e360 20 74 6f 20 65 78 70 61 6e 64 20 74 68 65 20 6d   to expand the m
8e370 61 73 74 65 72 20 75 73 69 6e 67 20 74 68 65 20  aster using the 
8e380 6e 65 77 6c 79 20 66 72 65 65 64 20 63 68 75 6e  newly freed chun
8e390 6b 20 2a 2f 0a 20 20 69 66 28 20 6d 65 6d 33 2e  k */.  if( mem3.
8e3a0 69 4d 61 73 74 65 72 20 29 7b 0a 20 20 20 20 77  iMaster ){.    w
8e3b0 68 69 6c 65 28 20 28 6d 65 6d 33 2e 61 50 6f 6f  hile( (mem3.aPoo
8e3c0 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31  l[mem3.iMaster-1
8e3d0 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 26 32  ].u.hdr.size4x&2
8e3e0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 69  )==0 ){.      si
8e3f0 7a 65 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  ze = mem3.aPool[
8e400 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
8e410 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65 3b 0a  u.hdr.prevSize;.
8e420 20 20 20 20 20 20 6d 65 6d 33 2e 69 4d 61 73 74        mem3.iMast
8e430 65 72 20 2d 3d 20 73 69 7a 65 3b 0a 20 20 20 20  er -= size;.    
8e440 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 20    mem3.szMaster 
8e450 2b 3d 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d  += size;.      m
8e460 65 6d 73 79 73 33 55 6e 6c 69 6e 6b 28 6d 65 6d  emsys3Unlink(mem
8e470 33 2e 69 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  3.iMaster);.    
8e480 20 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c    x = mem3.aPool
8e490 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d  [mem3.iMaster-1]
8e4a0 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20  .u.hdr.size4x & 
8e4b0 32 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  2;.      mem3.aP
8e4c0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
8e4d0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8e4e0 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
8e4f0 2a 34 20 7c 20 78 3b 0a 20 20 20 20 20 20 6d 65  *4 | x;.      me
8e500 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8e510 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73  aster+mem3.szMas
8e520 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 70 72 65  ter-1].u.hdr.pre
8e530 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 73 7a 4d  vSize = mem3.szM
8e540 61 73 74 65 72 3b 0a 20 20 20 20 7d 0a 20 20 20  aster;.    }.   
8e550 20 78 20 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b   x = mem3.aPool[
8e560 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e  mem3.iMaster-1].
8e570 75 2e 68 64 72 2e 73 69 7a 65 34 78 20 26 20 32  u.hdr.size4x & 2
8e580 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 6d 65  ;.    while( (me
8e590 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d  m3.aPool[mem3.iM
8e5a0 61 73 74 65 72 2b 6d 65 6d 33 2e 73 7a 4d 61 73  aster+mem3.szMas
8e5b0 74 65 72 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  ter-1].u.hdr.siz
8e5c0 65 34 78 26 31 29 3d 3d 30 20 29 7b 0a 20 20 20  e4x&1)==0 ){.   
8e5d0 20 20 20 6d 65 6d 73 79 73 33 55 6e 6c 69 6e 6b     memsys3Unlink
8e5e0 28 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65  (mem3.iMaster+me
8e5f0 6d 33 2e 73 7a 4d 61 73 74 65 72 29 3b 0a 20 20  m3.szMaster);.  
8e600 20 20 20 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65      mem3.szMaste
8e610 72 20 2b 3d 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b  r += mem3.aPool[
8e620 6d 65 6d 33 2e 69 4d 61 73 74 65 72 2b 6d 65 6d  mem3.iMaster+mem
8e630 33 2e 73 7a 4d 61 73 74 65 72 2d 31 5d 2e 75 2e  3.szMaster-1].u.
8e640 68 64 72 2e 73 69 7a 65 34 78 2f 34 3b 0a 20 20  hdr.size4x/4;.  
8e650 20 20 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d      mem3.aPool[m
8e660 65 6d 33 2e 69 4d 61 73 74 65 72 2d 31 5d 2e 75  em3.iMaster-1].u
8e670 2e 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 6d 65  .hdr.size4x = me
8e680 6d 33 2e 73 7a 4d 61 73 74 65 72 2a 34 20 7c 20  m3.szMaster*4 | 
8e690 78 3b 0a 20 20 20 20 20 20 6d 65 6d 33 2e 61 50  x;.      mem3.aP
8e6a0 6f 6f 6c 5b 6d 65 6d 33 2e 69 4d 61 73 74 65 72  ool[mem3.iMaster
8e6b0 2b 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72 2d 31  +mem3.szMaster-1
8e6c0 5d 2e 75 2e 68 64 72 2e 70 72 65 76 53 69 7a 65  ].u.hdr.prevSize
8e6d0 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65 72   = mem3.szMaster
8e6e0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f  ;.    }.  }.}../
8e6f0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
8e700 73 69 7a 65 20 6f 66 20 61 6e 20 6f 75 74 73 74  size of an outst
8e710 61 6e 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f  anding allocatio
8e720 6e 2c 20 69 6e 20 62 79 74 65 73 2e 20 20 54 68  n, in bytes.  Th
8e730 65 0a 2a 2a 20 73 69 7a 65 20 72 65 74 75 72 6e  e.** size return
8e740 65 64 20 6f 6d 69 74 73 20 74 68 65 20 38 2d 62  ed omits the 8-b
8e750 79 74 65 20 68 65 61 64 65 72 20 6f 76 65 72 68  yte header overh
8e760 65 61 64 2e 20 20 54 68 69 73 20 6f 6e 6c 79 0a  ead.  This only.
8e770 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20 63 68 75  ** works for chu
8e780 6e 6b 73 20 74 68 61 74 20 61 72 65 20 63 75 72  nks that are cur
8e790 72 65 6e 74 6c 79 20 63 68 65 63 6b 65 64 20 6f  rently checked o
8e7a0 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ut..*/.static in
8e7b0 74 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 76 6f  t memsys3Size(vo
8e7c0 69 64 20 2a 70 29 7b 0a 20 20 4d 65 6d 33 42 6c  id *p){.  Mem3Bl
8e7d0 6f 63 6b 20 2a 70 42 6c 6f 63 6b 3b 0a 20 20 69  ock *pBlock;.  i
8e7e0 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( p==0 ) return
8e7f0 20 30 3b 0a 20 20 70 42 6c 6f 63 6b 20 3d 20 28   0;.  pBlock = (
8e800 4d 65 6d 33 42 6c 6f 63 6b 2a 29 70 3b 0a 20 20  Mem3Block*)p;.  
8e810 61 73 73 65 72 74 28 20 28 70 42 6c 6f 63 6b 5b  assert( (pBlock[
8e820 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8e830 26 31 29 21 3d 30 20 29 3b 0a 20 20 72 65 74 75  &1)!=0 );.  retu
8e840 72 6e 20 28 70 42 6c 6f 63 6b 5b 2d 31 5d 2e 75  rn (pBlock[-1].u
8e850 2e 68 64 72 2e 73 69 7a 65 34 78 26 7e 33 29 2a  .hdr.size4x&~3)*
8e860 32 20 2d 20 34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2 - 4;.}../*.** 
8e870 52 6f 75 6e 64 20 75 70 20 61 20 72 65 71 75 65  Round up a reque
8e880 73 74 20 73 69 7a 65 20 74 6f 20 74 68 65 20 6e  st size to the n
8e890 65 78 74 20 76 61 6c 69 64 20 61 6c 6c 6f 63 61  ext valid alloca
8e8a0 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74  tion size..*/.st
8e8b0 61 74 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 33  atic int memsys3
8e8c0 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e 29 7b 0a  Roundup(int n){.
8e8d0 20 20 69 66 28 20 6e 3c 3d 31 32 20 29 7b 0a 20    if( n<=12 ){. 
8e8e0 20 20 20 72 65 74 75 72 6e 20 31 32 3b 0a 20 20     return 12;.  
8e8f0 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
8e900 6e 20 28 28 6e 2b 31 31 29 26 7e 37 29 20 2d 20  n ((n+11)&~7) - 
8e910 34 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  4;.  }.}../*.** 
8e920 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65 73 20  Allocate nBytes 
8e930 6f 66 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a 73 74  of memory..*/.st
8e940 61 74 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79  atic void *memsy
8e950 73 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 42 79  s3Malloc(int nBy
8e960 74 65 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  tes){.  sqlite3_
8e970 69 6e 74 36 34 20 2a 70 3b 0a 20 20 61 73 73 65  int64 *p;.  asse
8e980 72 74 28 20 6e 42 79 74 65 73 3e 30 20 29 3b 20  rt( nBytes>0 ); 
8e990 20 20 20 20 20 20 20 20 20 2f 2a 20 6d 61 6c 6c           /* mall
8e9a0 6f 63 2e 63 20 66 69 6c 74 65 72 73 20 6f 75 74  oc.c filters out
8e9b0 20 30 20 62 79 74 65 20 72 65 71 75 65 73 74 73   0 byte requests
8e9c0 20 2a 2f 0a 20 20 6d 65 6d 73 79 73 33 45 6e 74   */.  memsys3Ent
8e9d0 65 72 28 29 3b 0a 20 20 70 20 3d 20 6d 65 6d 73  er();.  p = mems
8e9e0 79 73 33 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28  ys3MallocUnsafe(
8e9f0 6e 42 79 74 65 73 29 3b 0a 20 20 6d 65 6d 73 79  nBytes);.  memsy
8ea00 73 33 4c 65 61 76 65 28 29 3b 0a 20 20 72 65 74  s3Leave();.  ret
8ea10 75 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d  urn (void*)p; .}
8ea20 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d  ../*.** Free mem
8ea30 6f 72 79 2e 0a 2a 2f 0a 76 6f 69 64 20 6d 65 6d  ory..*/.void mem
8ea40 73 79 73 33 46 72 65 65 28 76 6f 69 64 20 2a 70  sys3Free(void *p
8ea50 50 72 69 6f 72 29 7b 0a 20 20 61 73 73 65 72 74  Prior){.  assert
8ea60 28 20 70 50 72 69 6f 72 20 29 3b 0a 20 20 6d 65  ( pPrior );.  me
8ea70 6d 73 79 73 33 45 6e 74 65 72 28 29 3b 0a 20 20  msys3Enter();.  
8ea80 6d 65 6d 73 79 73 33 46 72 65 65 55 6e 73 61 66  memsys3FreeUnsaf
8ea90 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d  e(pPrior);.  mem
8eaa0 73 79 73 33 4c 65 61 76 65 28 29 3b 0a 7d 0a 0a  sys3Leave();.}..
8eab0 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
8eac0 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78 69 73   size of an exis
8ead0 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  ting memory allo
8eae0 63 61 74 69 6f 6e 0a 2a 2f 0a 76 6f 69 64 20 2a  cation.*/.void *
8eaf0 6d 65 6d 73 79 73 33 52 65 61 6c 6c 6f 63 28 76  memsys3Realloc(v
8eb00 6f 69 64 20 2a 70 50 72 69 6f 72 2c 20 69 6e 74  oid *pPrior, int
8eb10 20 6e 42 79 74 65 73 29 7b 0a 20 20 69 6e 74 20   nBytes){.  int 
8eb20 6e 4f 6c 64 3b 0a 20 20 76 6f 69 64 20 2a 70 3b  nOld;.  void *p;
8eb30 0a 20 20 69 66 28 20 70 50 72 69 6f 72 3d 3d 30  .  if( pPrior==0
8eb40 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
8eb50 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42  qlite3_malloc(nB
8eb60 79 74 65 73 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ytes);.  }.  if(
8eb70 20 6e 42 79 74 65 73 3c 3d 30 20 29 7b 0a 20 20   nBytes<=0 ){.  
8eb80 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
8eb90 50 72 69 6f 72 29 3b 0a 20 20 20 20 72 65 74 75  Prior);.    retu
8eba0 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 4f 6c 64  rn 0;.  }.  nOld
8ebb0 20 3d 20 6d 65 6d 73 79 73 33 53 69 7a 65 28 70   = memsys3Size(p
8ebc0 50 72 69 6f 72 29 3b 0a 20 20 69 66 28 20 6e 42  Prior);.  if( nB
8ebd0 79 74 65 73 3c 3d 6e 4f 6c 64 20 26 26 20 6e 42  ytes<=nOld && nB
8ebe0 79 74 65 73 3e 3d 6e 4f 6c 64 2d 31 32 38 20 29  ytes>=nOld-128 )
8ebf0 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 72  {.    return pPr
8ec00 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 79  ior;.  }.  memsy
8ec10 73 33 45 6e 74 65 72 28 29 3b 0a 20 20 70 20 3d  s3Enter();.  p =
8ec20 20 6d 65 6d 73 79 73 33 4d 61 6c 6c 6f 63 55 6e   memsys3MallocUn
8ec30 73 61 66 65 28 6e 42 79 74 65 73 29 3b 0a 20 20  safe(nBytes);.  
8ec40 69 66 28 20 70 20 29 7b 0a 20 20 20 20 69 66 28  if( p ){.    if(
8ec50 20 6e 4f 6c 64 3c 6e 42 79 74 65 73 20 29 7b 0a   nOld<nBytes ){.
8ec60 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2c 20        memcpy(p, 
8ec70 70 50 72 69 6f 72 2c 20 6e 4f 6c 64 29 3b 0a 20  pPrior, nOld);. 
8ec80 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
8ec90 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72  memcpy(p, pPrior
8eca0 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 7d  , nBytes);.    }
8ecb0 0a 20 20 20 20 6d 65 6d 73 79 73 33 46 72 65 65  .    memsys3Free
8ecc0 55 6e 73 61 66 65 28 70 50 72 69 6f 72 29 3b 0a  Unsafe(pPrior);.
8ecd0 20 20 7d 0a 20 20 6d 65 6d 73 79 73 33 4c 65 61    }.  memsys3Lea
8ece0 76 65 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ve();.  return p
8ecf0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
8ed00 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
8ed10 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
8ed20 20 6d 65 6d 73 79 73 33 49 6e 69 74 28 76 6f 69   memsys3Init(voi
8ed30 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  d *NotUsed){.  U
8ed40 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
8ed50 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69 66 28 20  NotUsed);.  if( 
8ed60 21 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  !sqlite3GlobalCo
8ed70 6e 66 69 67 2e 70 48 65 61 70 20 29 7b 0a 20 20  nfig.pHeap ){.  
8ed80 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
8ed90 45 52 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  ERROR;.  }..  /*
8eda0 20 53 74 6f 72 65 20 61 20 70 6f 69 6e 74 65 72   Store a pointer
8edb0 20 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 62   to the memory b
8edc0 6c 6f 63 6b 20 69 6e 20 67 6c 6f 62 61 6c 20 73  lock in global s
8edd0 74 72 75 63 74 75 72 65 20 6d 65 6d 33 2e 20 2a  tructure mem3. *
8ede0 2f 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65  /.  assert( size
8edf0 6f 66 28 4d 65 6d 33 42 6c 6f 63 6b 29 3d 3d 38  of(Mem3Block)==8
8ee00 20 29 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f 6c   );.  mem3.aPool
8ee10 20 3d 20 28 4d 65 6d 33 42 6c 6f 63 6b 20 2a 29   = (Mem3Block *)
8ee20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
8ee30 66 69 67 2e 70 48 65 61 70 3b 0a 20 20 6d 65 6d  fig.pHeap;.  mem
8ee40 33 2e 6e 50 6f 6f 6c 20 3d 20 28 73 71 6c 69 74  3.nPool = (sqlit
8ee50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
8ee60 48 65 61 70 20 2f 20 73 69 7a 65 6f 66 28 4d 65  Heap / sizeof(Me
8ee70 6d 33 42 6c 6f 63 6b 29 29 20 2d 20 32 3b 0a 0a  m3Block)) - 2;..
8ee80 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
8ee90 74 68 65 20 6d 61 73 74 65 72 20 62 6c 6f 63 6b  the master block
8eea0 2e 20 2a 2f 0a 20 20 6d 65 6d 33 2e 73 7a 4d 61  . */.  mem3.szMa
8eeb0 73 74 65 72 20 3d 20 6d 65 6d 33 2e 6e 50 6f 6f  ster = mem3.nPoo
8eec0 6c 3b 0a 20 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74  l;.  mem3.mnMast
8eed0 65 72 20 3d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74  er = mem3.szMast
8eee0 65 72 3b 0a 20 20 6d 65 6d 33 2e 69 4d 61 73 74  er;.  mem3.iMast
8eef0 65 72 20 3d 20 31 3b 0a 20 20 6d 65 6d 33 2e 61  er = 1;.  mem3.a
8ef00 50 6f 6f 6c 5b 30 5d 2e 75 2e 68 64 72 2e 73 69  Pool[0].u.hdr.si
8ef10 7a 65 34 78 20 3d 20 28 6d 65 6d 33 2e 73 7a 4d  ze4x = (mem3.szM
8ef20 61 73 74 65 72 3c 3c 32 29 20 2b 20 32 3b 0a 20  aster<<2) + 2;. 
8ef30 20 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6d 65 6d 33   mem3.aPool[mem3
8ef40 2e 6e 50 6f 6f 6c 5d 2e 75 2e 68 64 72 2e 70 72  .nPool].u.hdr.pr
8ef50 65 76 53 69 7a 65 20 3d 20 6d 65 6d 33 2e 6e 50  evSize = mem3.nP
8ef60 6f 6f 6c 3b 0a 20 20 6d 65 6d 33 2e 61 50 6f 6f  ool;.  mem3.aPoo
8ef70 6c 5b 6d 65 6d 33 2e 6e 50 6f 6f 6c 5d 2e 75 2e  l[mem3.nPool].u.
8ef80 68 64 72 2e 73 69 7a 65 34 78 20 3d 20 31 3b 0a  hdr.size4x = 1;.
8ef90 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
8efa0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
8efb0 69 6e 69 74 69 61 6c 69 7a 65 20 74 68 69 73 20  initialize this 
8efc0 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  module..*/.stati
8efd0 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 33 53 68  c void memsys3Sh
8efe0 75 74 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74  utdown(void *Not
8eff0 55 73 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f  Used){.  UNUSED_
8f000 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
8f010 64 29 3b 0a 20 20 72 65 74 75 72 6e 3b 0a 7d 0a  d);.  return;.}.
8f020 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 74 68  .../*.** Open th
8f030 65 20 66 69 6c 65 20 69 6e 64 69 63 61 74 65 64  e file indicated
8f040 20 61 6e 64 20 77 72 69 74 65 20 61 20 6c 6f 67   and write a log
8f050 20 6f 66 20 61 6c 6c 20 75 6e 66 72 65 65 64 20   of all unfreed 
8f060 6d 65 6d 6f 72 79 20 0a 2a 2a 20 61 6c 6c 6f 63  memory .** alloc
8f070 61 74 69 6f 6e 73 20 69 6e 74 6f 20 74 68 61 74  ations into that
8f080 20 6c 6f 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f   log..*/.SQLITE_
8f090 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
8f0a0 69 74 65 33 4d 65 6d 73 79 73 33 44 75 6d 70 28  ite3Memsys3Dump(
8f0b0 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
8f0c0 65 6e 61 6d 65 29 7b 0a 23 69 66 64 65 66 20 53  ename){.#ifdef S
8f0d0 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 46 49  QLITE_DEBUG.  FI
8f0e0 4c 45 20 2a 6f 75 74 3b 0a 20 20 75 33 32 20 69  LE *out;.  u32 i
8f0f0 2c 20 6a 3b 0a 20 20 75 33 32 20 73 69 7a 65 3b  , j;.  u32 size;
8f100 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
8f110 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65  ==0 || zFilename
8f120 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75  [0]==0 ){.    ou
8f130 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65  t = stdout;.  }e
8f140 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66  lse{.    out = f
8f150 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  open(zFilename, 
8f160 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75  "w");.    if( ou
8f170 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70  t==0 ){.      fp
8f180 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a  rintf(stderr, "*
8f190 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70  * Unable to outp
8f1a0 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20  ut memory debug 
8f1b0 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a  output log: %s *
8f1c0 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  *\n",.          
8f1d0 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c              zFil
8f1e0 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65  ename);.      re
8f1f0 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
8f200 20 20 6d 65 6d 73 79 73 33 45 6e 74 65 72 28 29    memsys3Enter()
8f210 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c  ;.  fprintf(out,
8f220 20 22 43 48 55 4e 4b 53 3a 5c 6e 22 29 3b 0a 20   "CHUNKS:\n");. 
8f230 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 6d 65 6d   for(i=1; i<=mem
8f240 33 2e 6e 50 6f 6f 6c 3b 20 69 2b 3d 73 69 7a 65  3.nPool; i+=size
8f250 2f 34 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20  /4){.    size = 
8f260 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 2d 31 5d 2e  mem3.aPool[i-1].
8f270 75 2e 68 64 72 2e 73 69 7a 65 34 78 3b 0a 20 20  u.hdr.size4x;.  
8f280 20 20 69 66 28 20 73 69 7a 65 2f 34 3c 3d 31 20    if( size/4<=1 
8f290 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  ){.      fprintf
8f2a0 28 6f 75 74 2c 20 22 25 70 20 73 69 7a 65 20 65  (out, "%p size e
8f2b0 72 72 6f 72 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61  rror\n", &mem3.a
8f2c0 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20 20 20 20  Pool[i]);.      
8f2d0 61 73 73 65 72 74 28 20 30 20 29 3b 0a 20 20 20  assert( 0 );.   
8f2e0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
8f2f0 20 20 20 20 69 66 28 20 28 73 69 7a 65 26 31 29      if( (size&1)
8f300 3d 3d 30 20 26 26 20 6d 65 6d 33 2e 61 50 6f 6f  ==0 && mem3.aPoo
8f310 6c 5b 69 2b 73 69 7a 65 2f 34 2d 31 5d 2e 75 2e  l[i+size/4-1].u.
8f320 68 64 72 2e 70 72 65 76 53 69 7a 65 21 3d 73 69  hdr.prevSize!=si
8f330 7a 65 2f 34 20 29 7b 0a 20 20 20 20 20 20 66 70  ze/4 ){.      fp
8f340 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 74  rintf(out, "%p t
8f350 61 69 6c 20 73 69 7a 65 20 64 6f 65 73 20 6e 6f  ail size does no
8f360 74 20 6d 61 74 63 68 5c 6e 22 2c 20 26 6d 65 6d  t match\n", &mem
8f370 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a 20 20 20  3.aPool[i]);.   
8f380 20 20 20 61 73 73 65 72 74 28 20 30 20 29 3b 0a     assert( 0 );.
8f390 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
8f3a0 20 7d 0a 20 20 20 20 69 66 28 20 28 28 6d 65 6d   }.    if( ((mem
8f3b0 33 2e 61 50 6f 6f 6c 5b 69 2b 73 69 7a 65 2f 34  3.aPool[i+size/4
8f3c0 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a 65 34 78  -1].u.hdr.size4x
8f3d0 26 32 29 3e 3e 31 29 21 3d 28 73 69 7a 65 26 31  &2)>>1)!=(size&1
8f3e0 29 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e  ) ){.      fprin
8f3f0 74 66 28 6f 75 74 2c 20 22 25 70 20 74 61 69 6c  tf(out, "%p tail
8f400 20 63 68 65 63 6b 6f 75 74 20 62 69 74 20 69 73   checkout bit is
8f410 20 69 6e 63 6f 72 72 65 63 74 5c 6e 22 2c 20 26   incorrect\n", &
8f420 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 29 3b 0a  mem3.aPool[i]);.
8f430 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30 20        assert( 0 
8f440 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
8f450 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 69      }.    if( si
8f460 7a 65 26 31 20 29 7b 0a 20 20 20 20 20 20 66 70  ze&1 ){.      fp
8f470 72 69 6e 74 66 28 6f 75 74 2c 20 22 25 70 20 25  rintf(out, "%p %
8f480 36 64 20 62 79 74 65 73 20 63 68 65 63 6b 65 64  6d bytes checked
8f490 20 6f 75 74 5c 6e 22 2c 20 26 6d 65 6d 33 2e 61   out\n", &mem3.a
8f4a0 50 6f 6f 6c 5b 69 5d 2c 20 28 73 69 7a 65 2f 34  Pool[i], (size/4
8f4b0 29 2a 38 2d 38 29 3b 0a 20 20 20 20 7d 65 6c 73  )*8-8);.    }els
8f4c0 65 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  e{.      fprintf
8f4d0 28 6f 75 74 2c 20 22 25 70 20 25 36 64 20 62 79  (out, "%p %6d by
8f4e0 74 65 73 20 66 72 65 65 25 73 5c 6e 22 2c 20 26  tes free%s\n", &
8f4f0 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 69 5d 2c 20 28  mem3.aPool[i], (
8f500 73 69 7a 65 2f 34 29 2a 38 2d 38 2c 0a 20 20 20  size/4)*8-8,.   
8f510 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
8f520 3d 3d 6d 65 6d 33 2e 69 4d 61 73 74 65 72 20 3f  ==mem3.iMaster ?
8f530 20 22 20 2a 2a 6d 61 73 74 65 72 2a 2a 22 20 3a   " **master**" :
8f540 20 22 22 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   "");.    }.  }.
8f550 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4d 58 5f    for(i=0; i<MX_
8f560 53 4d 41 4c 4c 2d 31 3b 20 69 2b 2b 29 7b 0a 20  SMALL-1; i++){. 
8f570 20 20 20 69 66 28 20 6d 65 6d 33 2e 61 69 53 6d     if( mem3.aiSm
8f580 61 6c 6c 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  all[i]==0 ) cont
8f590 69 6e 75 65 3b 0a 20 20 20 20 66 70 72 69 6e 74  inue;.    fprint
8f5a0 66 28 6f 75 74 2c 20 22 73 6d 61 6c 6c 28 25 32  f(out, "small(%2
8f5b0 64 29 3a 22 2c 20 69 29 3b 0a 20 20 20 20 66 6f  d):", i);.    fo
8f5c0 72 28 6a 20 3d 20 6d 65 6d 33 2e 61 69 53 6d 61  r(j = mem3.aiSma
8f5d0 6c 6c 5b 69 5d 3b 20 6a 3e 30 3b 20 6a 3d 6d 65  ll[i]; j>0; j=me
8f5e0 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2e 75 2e 6c 69  m3.aPool[j].u.li
8f5f0 73 74 2e 6e 65 78 74 29 7b 0a 20 20 20 20 20 20  st.next){.      
8f600 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 25  fprintf(out, " %
8f610 70 28 25 64 29 22 2c 20 26 6d 65 6d 33 2e 61 50  p(%d)", &mem3.aP
8f620 6f 6f 6c 5b 6a 5d 2c 0a 20 20 20 20 20 20 20 20  ool[j],.        
8f630 20 20 20 20 20 20 28 6d 65 6d 33 2e 61 50 6f 6f        (mem3.aPoo
8f640 6c 5b 6a 2d 31 5d 2e 75 2e 68 64 72 2e 73 69 7a  l[j-1].u.hdr.siz
8f650 65 34 78 2f 34 29 2a 38 2d 38 29 3b 0a 20 20 20  e4x/4)*8-8);.   
8f660 20 7d 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f   }.    fprintf(o
8f670 75 74 2c 20 22 5c 6e 22 29 3b 20 0a 20 20 7d 0a  ut, "\n"); .  }.
8f680 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 5f 48    for(i=0; i<N_H
8f690 41 53 48 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  ASH; i++){.    i
8f6a0 66 28 20 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69  f( mem3.aiHash[i
8f6b0 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
8f6c0 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74  .    fprintf(out
8f6d0 2c 20 22 68 61 73 68 28 25 32 64 29 3a 22 2c 20  , "hash(%2d):", 
8f6e0 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 20 3d 20  i);.    for(j = 
8f6f0 6d 65 6d 33 2e 61 69 48 61 73 68 5b 69 5d 3b 20  mem3.aiHash[i]; 
8f700 6a 3e 30 3b 20 6a 3d 6d 65 6d 33 2e 61 50 6f 6f  j>0; j=mem3.aPoo
8f710 6c 5b 6a 5d 2e 75 2e 6c 69 73 74 2e 6e 65 78 74  l[j].u.list.next
8f720 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66  ){.      fprintf
8f730 28 6f 75 74 2c 20 22 20 25 70 28 25 64 29 22 2c  (out, " %p(%d)",
8f740 20 26 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 5d 2c   &mem3.aPool[j],
8f750 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28  .              (
8f760 6d 65 6d 33 2e 61 50 6f 6f 6c 5b 6a 2d 31 5d 2e  mem3.aPool[j-1].
8f770 75 2e 68 64 72 2e 73 69 7a 65 34 78 2f 34 29 2a  u.hdr.size4x/4)*
8f780 38 2d 38 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  8-8);.    }.    
8f790 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e  fprintf(out, "\n
8f7a0 22 29 3b 20 0a 20 20 7d 0a 20 20 66 70 72 69 6e  "); .  }.  fprin
8f7b0 74 66 28 6f 75 74 2c 20 22 6d 61 73 74 65 72 3d  tf(out, "master=
8f7c0 25 64 5c 6e 22 2c 20 6d 65 6d 33 2e 69 4d 61 73  %d\n", mem3.iMas
8f7d0 74 65 72 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  ter);.  fprintf(
8f7e0 6f 75 74 2c 20 22 6e 6f 77 55 73 65 64 3d 25 64  out, "nowUsed=%d
8f7f0 5c 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a  \n", mem3.nPool*
8f800 38 20 2d 20 6d 65 6d 33 2e 73 7a 4d 61 73 74 65  8 - mem3.szMaste
8f810 72 2a 38 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  r*8);.  fprintf(
8f820 6f 75 74 2c 20 22 6d 78 55 73 65 64 3d 25 64 5c  out, "mxUsed=%d\
8f830 6e 22 2c 20 6d 65 6d 33 2e 6e 50 6f 6f 6c 2a 38  n", mem3.nPool*8
8f840 20 2d 20 6d 65 6d 33 2e 6d 6e 4d 61 73 74 65 72   - mem3.mnMaster
8f850 2a 38 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  *8);.  sqlite3_m
8f860 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 33 2e  utex_leave(mem3.
8f870 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20 6f 75  mutex);.  if( ou
8f880 74 3d 3d 73 74 64 6f 75 74 20 29 7b 0a 20 20 20  t==stdout ){.   
8f890 20 66 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b   fflush(stdout);
8f8a0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 63  .  }else{.    fc
8f8b0 6c 6f 73 65 28 6f 75 74 29 3b 0a 20 20 7d 0a 23  lose(out);.  }.#
8f8c0 65 6c 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41  else.  UNUSED_PA
8f8d0 52 41 4d 45 54 45 52 28 7a 46 69 6c 65 6e 61 6d  RAMETER(zFilenam
8f8e0 65 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a  e);.#endif.}../*
8f8f0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
8f900 20 69 73 20 74 68 65 20 6f 6e 6c 79 20 72 6f 75   is the only rou
8f910 74 69 6e 65 20 69 6e 20 74 68 69 73 20 66 69 6c  tine in this fil
8f920 65 20 77 69 74 68 20 65 78 74 65 72 6e 61 6c 20  e with external 
8f930 0a 2a 2a 20 6c 69 6e 6b 61 67 65 2e 0a 2a 2a 0a  .** linkage..**.
8f940 2a 2a 20 50 6f 70 75 6c 61 74 65 20 74 68 65 20  ** Populate the 
8f950 6c 6f 77 2d 6c 65 76 65 6c 20 6d 65 6d 6f 72 79  low-level memory
8f960 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63   allocation func
8f970 74 69 6f 6e 20 70 6f 69 6e 74 65 72 73 20 69 6e  tion pointers in
8f980 0a 2a 2a 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  .** sqlite3Globa
8f990 6c 43 6f 6e 66 69 67 2e 6d 20 77 69 74 68 20 70  lConfig.m with p
8f9a0 6f 69 6e 74 65 72 73 20 74 6f 20 74 68 65 20 72  ointers to the r
8f9b0 6f 75 74 69 6e 65 73 20 69 6e 20 74 68 69 73 20  outines in this 
8f9c0 66 69 6c 65 2e 20 54 68 65 0a 2a 2a 20 61 72 67  file. The.** arg
8f9d0 75 6d 65 6e 74 73 20 73 70 65 63 69 66 79 20 74  uments specify t
8f9e0 68 65 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f  he block of memo
8f9f0 72 79 20 74 6f 20 6d 61 6e 61 67 65 2e 0a 2a 2a  ry to manage..**
8fa00 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
8fa10 20 69 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20   is only called 
8fa20 62 79 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69  by sqlite3_confi
8fa30 67 28 29 2c 20 61 6e 64 20 74 68 65 72 65 66 6f  g(), and therefo
8fa40 72 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 72 65 71  re.** is not req
8fa50 75 69 72 65 64 20 74 6f 20 62 65 20 74 68 72 65  uired to be thre
8fa60 61 64 73 61 66 65 20 28 69 74 20 69 73 20 6e 6f  adsafe (it is no
8fa70 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  t)..*/.SQLITE_PR
8fa80 49 56 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69  IVATE const sqli
8fa90 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
8faa0 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74 4d 65  *sqlite3MemGetMe
8fab0 6d 73 79 73 33 28 76 6f 69 64 29 7b 0a 20 20 73  msys3(void){.  s
8fac0 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c 69  tatic const sqli
8fad0 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f 64 73 20  te3_mem_methods 
8fae0 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64 73 20 3d  mempoolMethods =
8faf0 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79 73 33 4d   {.     memsys3M
8fb00 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d 65 6d 73  alloc,.     mems
8fb10 79 73 33 46 72 65 65 2c 0a 20 20 20 20 20 6d 65  ys3Free,.     me
8fb20 6d 73 79 73 33 52 65 61 6c 6c 6f 63 2c 0a 20 20  msys3Realloc,.  
8fb30 20 20 20 6d 65 6d 73 79 73 33 53 69 7a 65 2c 0a     memsys3Size,.
8fb40 20 20 20 20 20 6d 65 6d 73 79 73 33 52 6f 75 6e       memsys3Roun
8fb50 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73  dup,.     memsys
8fb60 33 49 6e 69 74 2c 0a 20 20 20 20 20 6d 65 6d 73  3Init,.     mems
8fb70 79 73 33 53 68 75 74 64 6f 77 6e 2c 0a 20 20 20  ys3Shutdown,.   
8fb80 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72    0.  };.  retur
8fb90 6e 20 26 6d 65 6d 70 6f 6f 6c 4d 65 74 68 6f 64  n &mempoolMethod
8fba0 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  s;.}..#endif /* 
8fbb0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45  SQLITE_ENABLE_ME
8fbc0 4d 53 59 53 33 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  MSYS3 */../*****
8fbd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
8fbe0 20 6d 65 6d 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   mem3.c ********
8fbf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fc00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fc10 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
8fc20 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
8fc30 66 69 6c 65 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a  file mem5.c ****
8fc40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fc50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fc60 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
8fc70 32 30 30 37 20 4f 63 74 6f 62 65 72 20 31 34 0a  2007 October 14.
8fc80 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
8fc90 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
8fca0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
8fcb0 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
8fcc0 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
8fcd0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
8fce0 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
8fcf0 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
8fd00 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
8fd10 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
8fd20 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
8fd30 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
8fd40 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
8fd50 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
8fd60 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
8fd70 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
8fd80 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
8fd90 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
8fda0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fdb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fdc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
8fdd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
8fde0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
8fdf0 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63 74  ains the C funct
8fe00 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65 6d  ions that implem
8fe10 65 6e 74 20 61 20 6d 65 6d 6f 72 79 0a 2a 2a 20  ent a memory.** 
8fe20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79  allocation subsy
8fe30 73 74 65 6d 20 66 6f 72 20 75 73 65 20 62 79 20  stem for use by 
8fe40 53 51 4c 69 74 65 2e 20 0a 2a 2a 0a 2a 2a 20 54  SQLite. .**.** T
8fe50 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  his version of t
8fe60 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
8fe70 74 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 20 6f  tion subsystem o
8fe80 6d 69 74 73 20 61 6c 6c 0a 2a 2a 20 75 73 65 20  mits all.** use 
8fe90 6f 66 20 6d 61 6c 6c 6f 63 28 29 2e 20 54 68 65  of malloc(). The
8fea0 20 53 51 4c 69 74 65 20 75 73 65 72 20 73 75 70   SQLite user sup
8feb0 70 6c 69 65 73 20 61 20 62 6c 6f 63 6b 20 6f 66  plies a block of
8fec0 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72   memory.** befor
8fed0 65 20 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65  e calling sqlite
8fee0 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 66  3_initialize() f
8fef0 72 6f 6d 20 77 68 69 63 68 20 61 6c 6c 6f 63 61  rom which alloca
8ff00 74 69 6f 6e 73 0a 2a 2a 20 61 72 65 20 6d 61 64  tions.** are mad
8ff10 65 20 61 6e 64 20 72 65 74 75 72 6e 65 64 20 62  e and returned b
8ff20 79 20 74 68 65 20 78 4d 61 6c 6c 6f 63 28 29 20  y the xMalloc() 
8ff30 61 6e 64 20 78 52 65 61 6c 6c 6f 63 28 29 20 0a  and xRealloc() .
8ff40 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
8ff50 6e 73 2e 20 4f 6e 63 65 20 73 71 6c 69 74 65 33  ns. Once sqlite3
8ff60 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 68 61  _initialize() ha
8ff70 73 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c 0a 2a  s been called,.*
8ff80 2a 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  * the amount of 
8ff90 6d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c 65  memory available
8ffa0 20 74 6f 20 53 51 4c 69 74 65 20 69 73 20 66 69   to SQLite is fi
8ffb0 78 65 64 20 61 6e 64 20 63 61 6e 6e 6f 74 0a 2a  xed and cannot.*
8ffc0 2a 20 62 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2a  * be changed..**
8ffd0 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
8ffe0 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
8fff0 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73  llocation subsys
90000 74 65 6d 20 69 73 20 69 6e 63 6c 75 64 65 64 0a  tem is included.
90010 2a 2a 20 69 6e 20 74 68 65 20 62 75 69 6c 64 20  ** in the build 
90020 6f 6e 6c 79 20 69 66 20 53 51 4c 49 54 45 5f 45  only if SQLITE_E
90030 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69 73  NABLE_MEMSYS5 is
90040 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20   defined..**.** 
90050 24 49 64 3a 20 6d 65 6d 35 2e 63 2c 76 20 31 2e  $Id: mem5.c,v 1.
90060 31 39 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36  19 2008/11/19 16
90070 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39  :52:44 danielk19
90080 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a  77 Exp $.*/../*.
90090 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e 20  ** This version 
900a0 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  of the memory al
900b0 6c 6f 63 61 74 6f 72 20 69 73 20 75 73 65 64 20  locator is used 
900c0 6f 6e 6c 79 20 77 68 65 6e 20 0a 2a 2a 20 53 51  only when .** SQ
900d0 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53  LITE_ENABLE_MEMS
900e0 59 53 35 20 69 73 20 64 65 66 69 6e 65 64 2e 0a  YS5 is defined..
900f0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
90100 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 0a  _ENABLE_MEMSYS5.
90110 0a 2f 2a 0a 2a 2a 20 41 20 6d 69 6e 69 6d 75 6d  ./*.** A minimum
90120 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61   allocation is a
90130 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
90140 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75  e following stru
90150 63 74 75 72 65 2e 0a 2a 2a 20 4c 61 72 67 65 72  cture..** Larger
90160 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20 61 72 65   allocations are
90170 20 61 6e 20 61 72 72 61 79 20 6f 66 20 74 68 65   an array of the
90180 73 65 20 73 74 72 75 63 74 75 72 65 73 20 77 68  se structures wh
90190 65 72 65 20 74 68 65 0a 2a 2a 20 73 69 7a 65 20  ere the.** size 
901a0 6f 66 20 74 68 65 20 61 72 72 61 79 20 69 73 20  of the array is 
901b0 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 0a 2a 2f  a power of 2..*/
901c0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
901d0 4d 65 6d 35 4c 69 6e 6b 20 4d 65 6d 35 4c 69 6e  Mem5Link Mem5Lin
901e0 6b 3b 0a 73 74 72 75 63 74 20 4d 65 6d 35 4c 69  k;.struct Mem5Li
901f0 6e 6b 20 7b 0a 20 20 69 6e 74 20 6e 65 78 74 3b  nk {.  int next;
90200 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
90210 6f 66 20 6e 65 78 74 20 66 72 65 65 20 63 68 75  of next free chu
90220 6e 6b 20 2a 2f 0a 20 20 69 6e 74 20 70 72 65 76  nk */.  int prev
90230 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  ;       /* Index
90240 20 6f 66 20 70 72 65 76 69 6f 75 73 20 66 72 65   of previous fre
90250 65 20 63 68 75 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f  e chunk */.};../
90260 2a 0a 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 69 7a  *.** Maximum siz
90270 65 20 6f 66 20 61 6e 79 20 61 6c 6c 6f 63 61 74  e of any allocat
90280 69 6f 6e 20 69 73 20 28 28 31 3c 3c 4c 4f 47 4d  ion is ((1<<LOGM
90290 41 58 29 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 29 2e  AX)*mem5.nAtom).
902a0 20 53 69 6e 63 65 0a 2a 2a 20 6d 65 6d 35 2e 6e   Since.** mem5.n
902b0 41 74 6f 6d 20 69 73 20 61 6c 77 61 79 73 20 61  Atom is always a
902c0 74 20 6c 65 61 73 74 20 38 2c 20 74 68 69 73 20  t least 8, this 
902d0 69 73 20 6e 6f 74 20 72 65 61 6c 6c 79 20 61 20  is not really a 
902e0 70 72 61 63 74 69 63 61 6c 0a 2a 2a 20 6c 69 6d  practical.** lim
902f0 69 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64 65 66  itation..*/.#def
90300 69 6e 65 20 4c 4f 47 4d 41 58 20 33 30 0a 0a 2f  ine LOGMAX 30../
90310 2a 0a 2a 2a 20 4d 61 73 6b 73 20 75 73 65 64 20  *.** Masks used 
90320 66 6f 72 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 5d  for mem5.aCtrl[]
90330 20 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 23 64   elements..*/.#d
90340 65 66 69 6e 65 20 43 54 52 4c 5f 4c 4f 47 53 49  efine CTRL_LOGSI
90350 5a 45 20 20 30 78 31 66 20 20 20 20 2f 2a 20 4c  ZE  0x1f    /* L
90360 6f 67 32 20 53 69 7a 65 20 6f 66 20 74 68 69 73  og2 Size of this
90370 20 62 6c 6f 63 6b 20 72 65 6c 61 74 69 76 65 20   block relative 
90380 74 6f 20 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 23  to POW2_MIN */.#
90390 64 65 66 69 6e 65 20 43 54 52 4c 5f 46 52 45 45  define CTRL_FREE
903a0 20 20 20 20 20 30 78 32 30 20 20 20 20 2f 2a 20       0x20    /* 
903b0 54 72 75 65 20 69 66 20 6e 6f 74 20 63 68 65 63  True if not chec
903c0 6b 65 64 20 6f 75 74 20 2a 2f 0a 0a 2f 2a 0a 2a  ked out */../*.*
903d0 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 73 74 61  * All of the sta
903e0 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 75 73  tic variables us
903f0 65 64 20 62 79 20 74 68 69 73 20 6d 6f 64 75 6c  ed by this modul
90400 65 20 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 0a  e are collected.
90410 2a 2a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65  ** into a single
90420 20 73 74 72 75 63 74 75 72 65 20 6e 61 6d 65 64   structure named
90430 20 22 6d 65 6d 35 22 2e 20 20 54 68 69 73 20 69   "mem5".  This i
90440 73 20 74 6f 20 6b 65 65 70 20 74 68 65 0a 2a 2a  s to keep the.**
90450 20 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65   static variable
90460 73 20 6f 72 67 61 6e 69 7a 65 64 20 61 6e 64 20  s organized and 
90470 74 6f 20 72 65 64 75 63 65 20 6e 61 6d 65 73 70  to reduce namesp
90480 61 63 65 20 70 6f 6c 6c 75 74 69 6f 6e 0a 2a 2a  ace pollution.**
90490 20 77 68 65 6e 20 74 68 69 73 20 6d 6f 64 75 6c   when this modul
904a0 65 20 69 73 20 63 6f 6d 62 69 6e 65 64 20 77 69  e is combined wi
904b0 74 68 20 6f 74 68 65 72 20 69 6e 20 74 68 65 20  th other in the 
904c0 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 2e 0a 2a 2f  amalgamation..*/
904d0 0a 73 74 61 74 69 63 20 53 51 4c 49 54 45 5f 57  .static SQLITE_W
904e0 53 44 20 73 74 72 75 63 74 20 4d 65 6d 35 47 6c  SD struct Mem5Gl
904f0 6f 62 61 6c 20 7b 0a 20 20 2f 2a 0a 20 20 2a 2a  obal {.  /*.  **
90500 20 4d 65 6d 6f 72 79 20 61 76 61 69 6c 61 62 6c   Memory availabl
90510 65 20 66 6f 72 20 61 6c 6c 6f 63 61 74 69 6f 6e  e for allocation
90520 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 74 6f  .  */.  int nAto
90530 6d 3b 20 20 20 20 20 20 20 2f 2a 20 53 6d 61 6c  m;       /* Smal
90540 6c 65 73 74 20 70 6f 73 73 69 62 6c 65 20 61 6c  lest possible al
90550 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 62 79 74 65  location in byte
90560 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 6c 6f 63  s */.  int nBloc
90570 6b 3b 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  k;      /* Numbe
90580 72 20 6f 66 20 6e 41 74 6f 6d 20 73 69 7a 65 64  r of nAtom sized
90590 20 62 6c 6f 63 6b 73 20 69 6e 20 7a 50 6f 6f 6c   blocks in zPool
905a0 20 2a 2f 0a 20 20 75 38 20 2a 7a 50 6f 6f 6c 3b   */.  u8 *zPool;
905b0 0a 20 20 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d 75  .  .  /*.  ** Mu
905c0 74 65 78 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 61  tex to control a
905d0 63 63 65 73 73 20 74 6f 20 74 68 65 20 6d 65 6d  ccess to the mem
905e0 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73  ory allocation s
905f0 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a 2f 0a 20  ubsystem..  */. 
90600 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
90610 6d 75 74 65 78 3b 0a 0a 20 20 2f 2a 0a 20 20 2a  mutex;..  /*.  *
90620 2a 20 50 65 72 66 6f 72 6d 61 6e 63 65 20 73 74  * Performance st
90630 61 74 69 73 74 69 63 73 0a 20 20 2a 2f 0a 20 20  atistics.  */.  
90640 75 36 34 20 6e 41 6c 6c 6f 63 3b 20 20 20 20 20  u64 nAlloc;     
90650 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d      /* Total num
90660 62 65 72 20 6f 66 20 63 61 6c 6c 73 20 74 6f 20  ber of calls to 
90670 6d 61 6c 6c 6f 63 20 2a 2f 0a 20 20 75 36 34 20  malloc */.  u64 
90680 74 6f 74 61 6c 41 6c 6c 6f 63 3b 20 20 20 20 20  totalAlloc;     
90690 2f 2a 20 54 6f 74 61 6c 20 6f 66 20 61 6c 6c 20  /* Total of all 
906a0 6d 61 6c 6c 6f 63 20 63 61 6c 6c 73 20 2d 20 69  malloc calls - i
906b0 6e 63 6c 75 64 65 73 20 69 6e 74 65 72 6e 61 6c  ncludes internal
906c0 20 66 72 61 67 20 2a 2f 0a 20 20 75 36 34 20 74   frag */.  u64 t
906d0 6f 74 61 6c 45 78 63 65 73 73 3b 20 20 20 20 2f  otalExcess;    /
906e0 2a 20 54 6f 74 61 6c 20 69 6e 74 65 72 6e 61 6c  * Total internal
906f0 20 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a   fragmentation *
90700 2f 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 4f  /.  u32 currentO
90710 75 74 3b 20 20 20 20 20 2f 2a 20 43 75 72 72 65  ut;     /* Curre
90720 6e 74 20 63 68 65 63 6b 6f 75 74 2c 20 69 6e 63  nt checkout, inc
90730 6c 75 64 69 6e 67 20 69 6e 74 65 72 6e 61 6c 20  luding internal 
90740 66 72 61 67 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f  fragmentation */
90750 0a 20 20 75 33 32 20 63 75 72 72 65 6e 74 43 6f  .  u32 currentCo
90760 75 6e 74 3b 20 20 20 2f 2a 20 43 75 72 72 65 6e  unt;   /* Curren
90770 74 20 6e 75 6d 62 65 72 20 6f 66 20 64 69 73 74  t number of dist
90780 69 6e 63 74 20 63 68 65 63 6b 6f 75 74 73 20 2a  inct checkouts *
90790 2f 0a 20 20 75 33 32 20 6d 61 78 4f 75 74 3b 20  /.  u32 maxOut; 
907a0 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d          /* Maxim
907b0 75 6d 20 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73  um instantaneous
907c0 20 63 75 72 72 65 6e 74 4f 75 74 20 2a 2f 0a 20   currentOut */. 
907d0 20 75 33 32 20 6d 61 78 43 6f 75 6e 74 3b 20 20   u32 maxCount;  
907e0 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20       /* Maximum 
907f0 69 6e 73 74 61 6e 74 61 6e 65 6f 75 73 20 63 75  instantaneous cu
90800 72 72 65 6e 74 43 6f 75 6e 74 20 2a 2f 0a 20 20  rrentCount */.  
90810 75 33 32 20 6d 61 78 52 65 71 75 65 73 74 3b 20  u32 maxRequest; 
90820 20 20 20 20 2f 2a 20 4c 61 72 67 65 73 74 20 61      /* Largest a
90830 6c 6c 6f 63 61 74 69 6f 6e 20 28 65 78 63 6c 75  llocation (exclu
90840 73 69 76 65 20 6f 66 20 69 6e 74 65 72 6e 61 6c  sive of internal
90850 20 66 72 61 67 29 20 2a 2f 0a 20 20 0a 20 20 2f   frag) */.  .  /
90860 2a 0a 20 20 2a 2a 20 4c 69 73 74 73 20 6f 66 20  *.  ** Lists of 
90870 66 72 65 65 20 62 6c 6f 63 6b 73 20 6f 66 20 76  free blocks of v
90880 61 72 69 6f 75 73 20 73 69 7a 65 73 2e 0a 20 20  arious sizes..  
90890 2a 2f 0a 20 20 69 6e 74 20 61 69 46 72 65 65 6c  */.  int aiFreel
908a0 69 73 74 5b 4c 4f 47 4d 41 58 2b 31 5d 3b 0a 0a  ist[LOGMAX+1];..
908b0 20 20 2f 2a 0a 20 20 2a 2a 20 53 70 61 63 65 20    /*.  ** Space 
908c0 66 6f 72 20 74 72 61 63 6b 69 6e 67 20 77 68 69  for tracking whi
908d0 63 68 20 62 6c 6f 63 6b 73 20 61 72 65 20 63 68  ch blocks are ch
908e0 65 63 6b 65 64 20 6f 75 74 20 61 6e 64 20 74 68  ecked out and th
908f0 65 20 73 69 7a 65 0a 20 20 2a 2a 20 6f 66 20 65  e size.  ** of e
90900 61 63 68 20 62 6c 6f 63 6b 2e 20 20 4f 6e 65 20  ach block.  One 
90910 62 79 74 65 20 70 65 72 20 62 6c 6f 63 6b 2e 0a  byte per block..
90920 20 20 2a 2f 0a 20 20 75 38 20 2a 61 43 74 72 6c    */.  u8 *aCtrl
90930 3b 0a 0a 7d 20 6d 65 6d 35 20 3d 20 7b 20 31 39  ;..} mem5 = { 19
90940 38 30 34 31 36 37 20 7d 3b 0a 0a 23 64 65 66 69  804167 };..#defi
90950 6e 65 20 6d 65 6d 35 20 47 4c 4f 42 41 4c 28 73  ne mem5 GLOBAL(s
90960 74 72 75 63 74 20 4d 65 6d 35 47 6c 6f 62 61 6c  truct Mem5Global
90970 2c 20 6d 65 6d 35 29 0a 0a 23 64 65 66 69 6e 65  , mem5)..#define
90980 20 4d 45 4d 35 4c 49 4e 4b 28 69 64 78 29 20 28   MEM5LINK(idx) (
90990 28 4d 65 6d 35 4c 69 6e 6b 20 2a 29 28 26 6d 65  (Mem5Link *)(&me
909a0 6d 35 2e 7a 50 6f 6f 6c 5b 28 69 64 78 29 2a 6d  m5.zPool[(idx)*m
909b0 65 6d 35 2e 6e 41 74 6f 6d 5d 29 29 0a 0a 2f 2a  em5.nAtom]))../*
909c0 0a 2a 2a 20 55 6e 6c 69 6e 6b 20 74 68 65 20 63  .** Unlink the c
909d0 68 75 6e 6b 20 61 74 20 6d 65 6d 35 2e 61 50 6f  hunk at mem5.aPo
909e0 6f 6c 5b 69 5d 20 66 72 6f 6d 20 6c 69 73 74 20  ol[i] from list 
909f0 69 74 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a  it is currently.
90a00 2a 2a 20 6f 6e 2e 20 20 49 74 20 73 68 6f 75 6c  ** on.  It shoul
90a10 64 20 62 65 20 66 6f 75 6e 64 20 6f 6e 20 6d 65  d be found on me
90a20 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c  m5.aiFreelist[iL
90a30 6f 67 73 69 7a 65 5d 2e 0a 2a 2f 0a 73 74 61 74  ogsize]..*/.stat
90a40 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 55  ic void memsys5U
90a50 6e 6c 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74  nlink(int i, int
90a60 20 69 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e   iLogsize){.  in
90a70 74 20 6e 65 78 74 2c 20 70 72 65 76 3b 0a 20 20  t next, prev;.  
90a80 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
90a90 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b  i<mem5.nBlock );
90aa0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73  .  assert( iLogs
90ab0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69  ize>=0 && iLogsi
90ac0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20  ze<=LOGMAX );.  
90ad0 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61 43  assert( (mem5.aC
90ae0 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c 4f  trl[i] & CTRL_LO
90af0 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a 65  GSIZE)==iLogsize
90b00 20 29 3b 0a 0a 20 20 6e 65 78 74 20 3d 20 4d 45   );..  next = ME
90b10 4d 35 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 3b  M5LINK(i)->next;
90b20 0a 20 20 70 72 65 76 20 3d 20 4d 45 4d 35 4c 49  .  prev = MEM5LI
90b30 4e 4b 28 69 29 2d 3e 70 72 65 76 3b 0a 20 20 69  NK(i)->prev;.  i
90b40 66 28 20 70 72 65 76 3c 30 20 29 7b 0a 20 20 20  f( prev<0 ){.   
90b50 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
90b60 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 6e 65 78  [iLogsize] = nex
90b70 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
90b80 4d 45 4d 35 4c 49 4e 4b 28 70 72 65 76 29 2d 3e  MEM5LINK(prev)->
90b90 6e 65 78 74 20 3d 20 6e 65 78 74 3b 0a 20 20 7d  next = next;.  }
90ba0 0a 20 20 69 66 28 20 6e 65 78 74 3e 3d 30 20 29  .  if( next>=0 )
90bb0 7b 0a 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 6e  {.    MEM5LINK(n
90bc0 65 78 74 29 2d 3e 70 72 65 76 20 3d 20 70 72 65  ext)->prev = pre
90bd0 76 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  v;.  }.}../*.** 
90be0 4c 69 6e 6b 20 74 68 65 20 63 68 75 6e 6b 20 61  Link the chunk a
90bf0 74 20 6d 65 6d 35 2e 61 50 6f 6f 6c 5b 69 5d 20  t mem5.aPool[i] 
90c00 73 6f 20 74 68 61 74 20 69 73 20 6f 6e 20 74 68  so that is on th
90c10 65 20 69 4c 6f 67 73 69 7a 65 0a 2a 2a 20 66 72  e iLogsize.** fr
90c20 65 65 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74  ee list..*/.stat
90c30 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 4c  ic void memsys5L
90c40 69 6e 6b 28 69 6e 74 20 69 2c 20 69 6e 74 20 69  ink(int i, int i
90c50 4c 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20  Logsize){.  int 
90c60 78 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  x;.  assert( sql
90c70 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
90c80 6d 65 6d 35 2e 6d 75 74 65 78 29 20 29 3b 0a 20  mem5.mutex) );. 
90c90 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
90ca0 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29   i<mem5.nBlock )
90cb0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67  ;.  assert( iLog
90cc0 73 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73  size>=0 && iLogs
90cd0 69 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20  ize<=LOGMAX );. 
90ce0 20 61 73 73 65 72 74 28 20 28 6d 65 6d 35 2e 61   assert( (mem5.a
90cf0 43 74 72 6c 5b 69 5d 20 26 20 43 54 52 4c 5f 4c  Ctrl[i] & CTRL_L
90d00 4f 47 53 49 5a 45 29 3d 3d 69 4c 6f 67 73 69 7a  OGSIZE)==iLogsiz
90d10 65 20 29 3b 0a 0a 20 20 78 20 3d 20 4d 45 4d 35  e );..  x = MEM5
90d20 4c 49 4e 4b 28 69 29 2d 3e 6e 65 78 74 20 3d 20  LINK(i)->next = 
90d30 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
90d40 69 4c 6f 67 73 69 7a 65 5d 3b 0a 20 20 4d 45 4d  iLogsize];.  MEM
90d50 35 4c 49 4e 4b 28 69 29 2d 3e 70 72 65 76 20 3d  5LINK(i)->prev =
90d60 20 2d 31 3b 0a 20 20 69 66 28 20 78 3e 3d 30 20   -1;.  if( x>=0 
90d70 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 78  ){.    assert( x
90d80 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a  <mem5.nBlock );.
90d90 20 20 20 20 4d 45 4d 35 4c 49 4e 4b 28 78 29 2d      MEM5LINK(x)-
90da0 3e 70 72 65 76 20 3d 20 69 3b 0a 20 20 7d 0a 20  >prev = i;.  }. 
90db0 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74   mem5.aiFreelist
90dc0 5b 69 4c 6f 67 73 69 7a 65 5d 20 3d 20 69 3b 0a  [iLogsize] = i;.
90dd0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20  }../*.** If the 
90de0 53 54 41 54 49 43 5f 4d 45 4d 20 6d 75 74 65 78  STATIC_MEM mutex
90df0 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
90e00 68 65 6c 64 2c 20 6f 62 74 61 69 6e 20 69 74 20  held, obtain it 
90e10 6e 6f 77 2e 20 54 68 65 20 6d 75 74 65 78 0a 2a  now. The mutex.*
90e20 2a 20 77 69 6c 6c 20 61 6c 72 65 61 64 79 20 62  * will already b
90e30 65 20 68 65 6c 64 20 28 6f 62 74 61 69 6e 65 64  e held (obtained
90e40 20 62 79 20 63 6f 64 65 20 69 6e 20 6d 61 6c 6c   by code in mall
90e50 6f 63 2e 63 29 20 69 66 0a 2a 2a 20 73 71 6c 69  oc.c) if.** sqli
90e60 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
90e70 62 4d 65 6d 53 74 61 74 20 69 73 20 74 72 75 65  bMemStat is true
90e80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
90e90 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 76 6f   memsys5Enter(vo
90ea0 69 64 29 7b 0a 20 20 69 66 28 20 73 71 6c 69 74  id){.  if( sqlit
90eb0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62  e3GlobalConfig.b
90ec0 4d 65 6d 73 74 61 74 3d 3d 30 20 26 26 20 6d 65  Memstat==0 && me
90ed0 6d 35 2e 6d 75 74 65 78 3d 3d 30 20 29 7b 0a 20  m5.mutex==0 ){. 
90ee0 20 20 20 6d 65 6d 35 2e 6d 75 74 65 78 20 3d 20     mem5.mutex = 
90ef0 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
90f00 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
90f10 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20 20 7d 0a  TATIC_MEM);.  }.
90f20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
90f30 65 6e 74 65 72 28 6d 65 6d 35 2e 6d 75 74 65 78  enter(mem5.mutex
90f40 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
90f50 20 6d 65 6d 73 79 73 35 4c 65 61 76 65 28 76 6f   memsys5Leave(vo
90f60 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id){.  sqlite3_m
90f70 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 35 2e  utex_leave(mem5.
90f80 6d 75 74 65 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  mutex);.}../*.**
90f90 20 52 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65   Return the size
90fa0 20 6f 66 20 61 6e 20 6f 75 74 73 74 61 6e 64 69   of an outstandi
90fb0 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  ng allocation, i
90fc0 6e 20 62 79 74 65 73 2e 20 20 54 68 65 0a 2a 2a  n bytes.  The.**
90fd0 20 73 69 7a 65 20 72 65 74 75 72 6e 65 64 20 6f   size returned o
90fe0 6d 69 74 73 20 74 68 65 20 38 2d 62 79 74 65 20  mits the 8-byte 
90ff0 68 65 61 64 65 72 20 6f 76 65 72 68 65 61 64 2e  header overhead.
91000 20 20 54 68 69 73 20 6f 6e 6c 79 0a 2a 2a 20 77    This only.** w
91010 6f 72 6b 73 20 66 6f 72 20 63 68 75 6e 6b 73 20  orks for chunks 
91020 74 68 61 74 20 61 72 65 20 63 75 72 72 65 6e 74  that are current
91030 6c 79 20 63 68 65 63 6b 65 64 20 6f 75 74 2e 0a  ly checked out..
91040 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65  */.static int me
91050 6d 73 79 73 35 53 69 7a 65 28 76 6f 69 64 20 2a  msys5Size(void *
91060 70 29 7b 0a 20 20 69 6e 74 20 69 53 69 7a 65 20  p){.  int iSize 
91070 3d 20 30 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  = 0;.  if( p ){.
91080 20 20 20 20 69 6e 74 20 69 20 3d 20 28 28 75 38      int i = ((u8
91090 20 2a 29 70 2d 6d 65 6d 35 2e 7a 50 6f 6f 6c 29   *)p-mem5.zPool)
910a0 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 0a 20 20 20  /mem5.nAtom;.   
910b0 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
910c0 20 69 3c 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29   i<mem5.nBlock )
910d0 3b 0a 20 20 20 20 69 53 69 7a 65 20 3d 20 6d 65  ;.    iSize = me
910e0 6d 35 2e 6e 41 74 6f 6d 20 2a 20 28 31 20 3c 3c  m5.nAtom * (1 <<
910f0 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 5d 26   (mem5.aCtrl[i]&
91100 43 54 52 4c 5f 4c 4f 47 53 49 5a 45 29 29 3b 0a  CTRL_LOGSIZE));.
91110 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 53 69    }.  return iSi
91120 7a 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e  ze;.}../*.** Fin
91130 64 20 74 68 65 20 66 69 72 73 74 20 65 6e 74 72  d the first entr
91140 79 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  y on the freelis
91150 74 20 69 4c 6f 67 73 69 7a 65 2e 20 20 55 6e 6c  t iLogsize.  Unl
91160 69 6e 6b 20 74 68 61 74 0a 2a 2a 20 65 6e 74 72  ink that.** entr
91170 79 20 61 6e 64 20 72 65 74 75 72 6e 20 69 74 73  y and return its
91180 20 69 6e 64 65 78 2e 20 0a 2a 2f 0a 73 74 61 74   index. .*/.stat
91190 69 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 55 6e  ic int memsys5Un
911a0 6c 69 6e 6b 46 69 72 73 74 28 69 6e 74 20 69 4c  linkFirst(int iL
911b0 6f 67 73 69 7a 65 29 7b 0a 20 20 69 6e 74 20 69  ogsize){.  int i
911c0 3b 0a 20 20 69 6e 74 20 69 46 69 72 73 74 3b 0a  ;.  int iFirst;.
911d0 0a 20 20 61 73 73 65 72 74 28 20 69 4c 6f 67 73  .  assert( iLogs
911e0 69 7a 65 3e 3d 30 20 26 26 20 69 4c 6f 67 73 69  ize>=0 && iLogsi
911f0 7a 65 3c 3d 4c 4f 47 4d 41 58 20 29 3b 0a 20 20  ze<=LOGMAX );.  
91200 69 20 3d 20 69 46 69 72 73 74 20 3d 20 6d 65 6d  i = iFirst = mem
91210 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f  5.aiFreelist[iLo
91220 67 73 69 7a 65 5d 3b 0a 20 20 61 73 73 65 72 74  gsize];.  assert
91230 28 20 69 46 69 72 73 74 3e 3d 30 20 29 3b 0a 20  ( iFirst>=0 );. 
91240 20 77 68 69 6c 65 28 20 69 3e 30 20 29 7b 0a 20   while( i>0 ){. 
91250 20 20 20 69 66 28 20 69 3c 69 46 69 72 73 74 20     if( i<iFirst 
91260 29 20 69 46 69 72 73 74 20 3d 20 69 3b 0a 20 20  ) iFirst = i;.  
91270 20 20 69 20 3d 20 4d 45 4d 35 4c 49 4e 4b 28 69    i = MEM5LINK(i
91280 29 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 6d  )->next;.  }.  m
91290 65 6d 73 79 73 35 55 6e 6c 69 6e 6b 28 69 46 69  emsys5Unlink(iFi
912a0 72 73 74 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a  rst, iLogsize);.
912b0 20 20 72 65 74 75 72 6e 20 69 46 69 72 73 74 3b    return iFirst;
912c0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
912d0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d 65 6d 6f   a block of memo
912e0 72 79 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e  ry of at least n
912f0 42 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a  Bytes in size..*
91300 2a 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66  * Return NULL if
91310 20 75 6e 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74   unable..*/.stat
91320 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35  ic void *memsys5
91330 4d 61 6c 6c 6f 63 55 6e 73 61 66 65 28 69 6e 74  MallocUnsafe(int
91340 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 69   nByte){.  int i
91350 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49  ;           /* I
91360 6e 64 65 78 20 6f 66 20 61 20 6d 65 6d 35 2e 61  ndex of a mem5.a
91370 50 6f 6f 6c 5b 5d 20 73 6c 6f 74 20 2a 2f 0a 20  Pool[] slot */. 
91380 20 69 6e 74 20 69 42 69 6e 3b 20 20 20 20 20 20   int iBin;      
91390 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f 20    /* Index into 
913a0 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b  mem5.aiFreelist[
913b0 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 46 75 6c 6c  ] */.  int iFull
913c0 53 7a 3b 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  Sz;     /* Size 
913d0 6f 66 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 72 6f  of allocation ro
913e0 75 6e 64 65 64 20 75 70 20 74 6f 20 70 6f 77 65  unded up to powe
913f0 72 20 6f 66 20 32 20 2a 2f 0a 20 20 69 6e 74 20  r of 2 */.  int 
91400 69 4c 6f 67 73 69 7a 65 3b 20 20 20 20 2f 2a 20  iLogsize;    /* 
91410 4c 6f 67 32 20 6f 66 20 69 46 75 6c 6c 53 7a 2f  Log2 of iFullSz/
91420 50 4f 57 32 5f 4d 49 4e 20 2a 2f 0a 0a 20 20 2f  POW2_MIN */..  /
91430 2a 20 4b 65 65 70 20 74 72 61 63 6b 20 6f 66 20  * Keep track of 
91440 74 68 65 20 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f  the maximum allo
91450 63 61 74 69 6f 6e 20 72 65 71 75 65 73 74 2e 20  cation request. 
91460 20 45 76 65 6e 20 75 6e 66 75 6c 66 69 6c 6c 65   Even unfulfille
91470 64 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 73 20  d.  ** requests 
91480 61 72 65 20 63 6f 75 6e 74 65 64 20 2a 2f 0a 20  are counted */. 
91490 20 69 66 28 20 28 75 33 32 29 6e 42 79 74 65 3e   if( (u32)nByte>
914a0 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20  mem5.maxRequest 
914b0 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6d 61 78 52  ){.    mem5.maxR
914c0 65 71 75 65 73 74 20 3d 20 6e 42 79 74 65 3b 0a  equest = nByte;.
914d0 20 20 7d 0a 0a 20 20 2f 2a 20 52 6f 75 6e 64 20    }..  /* Round 
914e0 6e 42 79 74 65 20 75 70 20 74 6f 20 74 68 65 20  nByte up to the 
914f0 6e 65 78 74 20 76 61 6c 69 64 20 70 6f 77 65 72  next valid power
91500 20 6f 66 20 74 77 6f 20 2a 2f 0a 20 20 66 6f 72   of two */.  for
91510 28 69 46 75 6c 6c 53 7a 3d 6d 65 6d 35 2e 6e 41  (iFullSz=mem5.nA
91520 74 6f 6d 2c 20 69 4c 6f 67 73 69 7a 65 3d 30 3b  tom, iLogsize=0;
91530 20 69 46 75 6c 6c 53 7a 3c 6e 42 79 74 65 3b 20   iFullSz<nByte; 
91540 69 46 75 6c 6c 53 7a 20 2a 3d 20 32 2c 20 69 4c  iFullSz *= 2, iL
91550 6f 67 73 69 7a 65 2b 2b 29 7b 7d 0a 0a 20 20 2f  ogsize++){}..  /
91560 2a 20 4d 61 6b 65 20 73 75 72 65 20 6d 65 6d 35  * Make sure mem5
91570 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 4c 6f 67  .aiFreelist[iLog
91580 73 69 7a 65 5d 20 63 6f 6e 74 61 69 6e 73 20 61  size] contains a
91590 74 20 6c 65 61 73 74 20 6f 6e 65 20 66 72 65 65  t least one free
915a0 0a 20 20 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66  .  ** block.  If
915b0 20 6e 6f 74 2c 20 74 68 65 6e 20 73 70 6c 69 74   not, then split
915c0 20 61 20 62 6c 6f 63 6b 20 6f 66 20 74 68 65 20   a block of the 
915d0 6e 65 78 74 20 6c 61 72 67 65 72 20 70 6f 77 65  next larger powe
915e0 72 20 6f 66 0a 20 20 2a 2a 20 74 77 6f 20 69 6e  r of.  ** two in
915f0 20 6f 72 64 65 72 20 74 6f 20 63 72 65 61 74 65   order to create
91600 20 61 20 6e 65 77 20 66 72 65 65 20 62 6c 6f 63   a new free bloc
91610 6b 20 6f 66 20 73 69 7a 65 20 69 4c 6f 67 73 69  k of size iLogsi
91620 7a 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  ze..  */.  for(i
91630 42 69 6e 3d 69 4c 6f 67 73 69 7a 65 3b 20 6d 65  Bin=iLogsize; me
91640 6d 35 2e 61 69 46 72 65 65 6c 69 73 74 5b 69 42  m5.aiFreelist[iB
91650 69 6e 5d 3c 30 20 26 26 20 69 42 69 6e 3c 3d 4c  in]<0 && iBin<=L
91660 4f 47 4d 41 58 3b 20 69 42 69 6e 2b 2b 29 7b 7d  OGMAX; iBin++){}
91670 0a 20 20 69 66 28 20 69 42 69 6e 3e 4c 4f 47 4d  .  if( iBin>LOGM
91680 41 58 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  AX ) return 0;. 
91690 20 69 20 3d 20 6d 65 6d 73 79 73 35 55 6e 6c 69   i = memsys5Unli
916a0 6e 6b 46 69 72 73 74 28 69 42 69 6e 29 3b 0a 20  nkFirst(iBin);. 
916b0 20 77 68 69 6c 65 28 20 69 42 69 6e 3e 69 4c 6f   while( iBin>iLo
916c0 67 73 69 7a 65 20 29 7b 0a 20 20 20 20 69 6e 74  gsize ){.    int
916d0 20 6e 65 77 53 69 7a 65 3b 0a 0a 20 20 20 20 69   newSize;..    i
916e0 42 69 6e 2d 2d 3b 0a 20 20 20 20 6e 65 77 53 69  Bin--;.    newSi
916f0 7a 65 20 3d 20 31 20 3c 3c 20 69 42 69 6e 3b 0a  ze = 1 << iBin;.
91700 20 20 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69      mem5.aCtrl[i
91710 2b 6e 65 77 53 69 7a 65 5d 20 3d 20 43 54 52 4c  +newSize] = CTRL
91720 5f 46 52 45 45 20 7c 20 69 42 69 6e 3b 0a 20 20  _FREE | iBin;.  
91730 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 2b    memsys5Link(i+
91740 6e 65 77 53 69 7a 65 2c 20 69 42 69 6e 29 3b 0a  newSize, iBin);.
91750 20 20 7d 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c    }.  mem5.aCtrl
91760 5b 69 5d 20 3d 20 69 4c 6f 67 73 69 7a 65 3b 0a  [i] = iLogsize;.
91770 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 61 6c 6c  .  /* Update all
91780 6f 63 61 74 6f 72 20 70 65 72 66 6f 72 6d 61 6e  ocator performan
91790 63 65 20 73 74 61 74 69 73 74 69 63 73 2e 20 2a  ce statistics. *
917a0 2f 0a 20 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 2b  /.  mem5.nAlloc+
917b0 2b 3b 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 41  +;.  mem5.totalA
917c0 6c 6c 6f 63 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b  lloc += iFullSz;
917d0 0a 20 20 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63  .  mem5.totalExc
917e0 65 73 73 20 2b 3d 20 69 46 75 6c 6c 53 7a 20 2d  ess += iFullSz -
917f0 20 6e 42 79 74 65 3b 0a 20 20 6d 65 6d 35 2e 63   nByte;.  mem5.c
91800 75 72 72 65 6e 74 43 6f 75 6e 74 2b 2b 3b 0a 20  urrentCount++;. 
91810 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74   mem5.currentOut
91820 20 2b 3d 20 69 46 75 6c 6c 53 7a 3b 0a 20 20 69   += iFullSz;.  i
91830 66 28 20 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74  f( mem5.maxCount
91840 3c 6d 65 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75  <mem5.currentCou
91850 6e 74 20 29 20 6d 65 6d 35 2e 6d 61 78 43 6f 75  nt ) mem5.maxCou
91860 6e 74 20 3d 20 6d 65 6d 35 2e 63 75 72 72 65 6e  nt = mem5.curren
91870 74 43 6f 75 6e 74 3b 0a 20 20 69 66 28 20 6d 65  tCount;.  if( me
91880 6d 35 2e 6d 61 78 4f 75 74 3c 6d 65 6d 35 2e 63  m5.maxOut<mem5.c
91890 75 72 72 65 6e 74 4f 75 74 20 29 20 6d 65 6d 35  urrentOut ) mem5
918a0 2e 6d 61 78 4f 75 74 20 3d 20 6d 65 6d 35 2e 63  .maxOut = mem5.c
918b0 75 72 72 65 6e 74 4f 75 74 3b 0a 0a 20 20 2f 2a  urrentOut;..  /*
918c0 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
918d0 72 20 74 6f 20 74 68 65 20 61 6c 6c 6f 63 61 74  r to the allocat
918e0 65 64 20 6d 65 6d 6f 72 79 2e 20 2a 2f 0a 20 20  ed memory. */.  
918f0 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29 26 6d  return (void*)&m
91900 65 6d 35 2e 7a 50 6f 6f 6c 5b 69 2a 6d 65 6d 35  em5.zPool[i*mem5
91910 2e 6e 41 74 6f 6d 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a  .nAtom];.}../*.*
91920 2a 20 46 72 65 65 20 61 6e 20 6f 75 74 73 74 61  * Free an outsta
91930 6e 64 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c 6c  nding memory all
91940 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  ocation..*/.stat
91950 69 63 20 76 6f 69 64 20 6d 65 6d 73 79 73 35 46  ic void memsys5F
91960 72 65 65 55 6e 73 61 66 65 28 76 6f 69 64 20 2a  reeUnsafe(void *
91970 70 4f 6c 64 29 7b 0a 20 20 75 33 32 20 73 69 7a  pOld){.  u32 siz
91980 65 2c 20 69 4c 6f 67 73 69 7a 65 3b 0a 20 20 69  e, iLogsize;.  i
91990 6e 74 20 69 42 6c 6f 63 6b 3b 20 20 20 20 20 20  nt iBlock;      
919a0 20 20 20 20 20 20 20 0a 0a 20 20 2f 2a 20 53 65         ..  /* Se
919b0 74 20 69 42 6c 6f 63 6b 20 74 6f 20 74 68 65 20  t iBlock to the 
919c0 69 6e 64 65 78 20 6f 66 20 74 68 65 20 62 6c 6f  index of the blo
919d0 63 6b 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ck pointed to by
919e0 20 70 4f 6c 64 20 69 6e 20 0a 20 20 2a 2a 20 74   pOld in .  ** t
919f0 68 65 20 61 72 72 61 79 20 6f 66 20 6d 65 6d 35  he array of mem5
91a00 2e 6e 41 74 6f 6d 20 62 79 74 65 20 62 6c 6f 63  .nAtom byte bloc
91a10 6b 73 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79  ks pointed to by
91a20 20 6d 65 6d 35 2e 7a 50 6f 6f 6c 2e 0a 20 20 2a   mem5.zPool..  *
91a30 2f 0a 20 20 69 42 6c 6f 63 6b 20 3d 20 28 28 75  /.  iBlock = ((u
91a40 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35 2e 7a 50  8 *)pOld-mem5.zP
91a50 6f 6f 6c 29 2f 6d 65 6d 35 2e 6e 41 74 6f 6d 3b  ool)/mem5.nAtom;
91a60 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61  ..  /* Check tha
91a70 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 70 4f  t the pointer pO
91a80 6c 64 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 76  ld points to a v
91a90 61 6c 69 64 2c 20 6e 6f 6e 2d 66 72 65 65 20 62  alid, non-free b
91aa0 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 61 73 73 65 72  lock. */.  asser
91ab0 74 28 20 69 42 6c 6f 63 6b 3e 3d 30 20 26 26 20  t( iBlock>=0 && 
91ac0 69 42 6c 6f 63 6b 3c 6d 65 6d 35 2e 6e 42 6c 6f  iBlock<mem5.nBlo
91ad0 63 6b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ck );.  assert( 
91ae0 28 28 75 38 20 2a 29 70 4f 6c 64 2d 6d 65 6d 35  ((u8 *)pOld-mem5
91af0 2e 7a 50 6f 6f 6c 29 25 6d 65 6d 35 2e 6e 41 74  .zPool)%mem5.nAt
91b00 6f 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  om==0 );.  asser
91b10 74 28 20 28 6d 65 6d 35 2e 61 43 74 72 6c 5b 69  t( (mem5.aCtrl[i
91b20 42 6c 6f 63 6b 5d 20 26 20 43 54 52 4c 5f 46 52  Block] & CTRL_FR
91b30 45 45 29 3d 3d 30 20 29 3b 0a 0a 20 20 69 4c 6f  EE)==0 );..  iLo
91b40 67 73 69 7a 65 20 3d 20 6d 65 6d 35 2e 61 43 74  gsize = mem5.aCt
91b50 72 6c 5b 69 42 6c 6f 63 6b 5d 20 26 20 43 54 52  rl[iBlock] & CTR
91b60 4c 5f 4c 4f 47 53 49 5a 45 3b 0a 20 20 73 69 7a  L_LOGSIZE;.  siz
91b70 65 20 3d 20 31 3c 3c 69 4c 6f 67 73 69 7a 65 3b  e = 1<<iLogsize;
91b80 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
91b90 6b 2b 73 69 7a 65 2d 31 3c 28 75 33 32 29 6d 65  k+size-1<(u32)me
91ba0 6d 35 2e 6e 42 6c 6f 63 6b 20 29 3b 0a 0a 20 20  m5.nBlock );..  
91bb0 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63  mem5.aCtrl[iBloc
91bc0 6b 5d 20 7c 3d 20 43 54 52 4c 5f 46 52 45 45 3b  k] |= CTRL_FREE;
91bd0 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c 5b 69 42  .  mem5.aCtrl[iB
91be0 6c 6f 63 6b 2b 73 69 7a 65 2d 31 5d 20 7c 3d 20  lock+size-1] |= 
91bf0 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20 61 73 73  CTRL_FREE;.  ass
91c00 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e  ert( mem5.curren
91c10 74 43 6f 75 6e 74 3e 30 20 29 3b 0a 20 20 61 73  tCount>0 );.  as
91c20 73 65 72 74 28 20 6d 65 6d 35 2e 63 75 72 72 65  sert( mem5.curre
91c30 6e 74 4f 75 74 3e 3d 28 73 69 7a 65 2a 6d 65 6d  ntOut>=(size*mem
91c40 35 2e 6e 41 74 6f 6d 29 20 29 3b 0a 20 20 6d 65  5.nAtom) );.  me
91c50 6d 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 2d  m5.currentCount-
91c60 2d 3b 0a 20 20 6d 65 6d 35 2e 63 75 72 72 65 6e  -;.  mem5.curren
91c70 74 4f 75 74 20 2d 3d 20 73 69 7a 65 2a 6d 65 6d  tOut -= size*mem
91c80 35 2e 6e 41 74 6f 6d 3b 0a 20 20 61 73 73 65 72  5.nAtom;.  asser
91c90 74 28 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f  t( mem5.currentO
91ca0 75 74 3e 30 20 7c 7c 20 6d 65 6d 35 2e 63 75 72  ut>0 || mem5.cur
91cb0 72 65 6e 74 43 6f 75 6e 74 3d 3d 30 20 29 3b 0a  rentCount==0 );.
91cc0 20 20 61 73 73 65 72 74 28 20 6d 65 6d 35 2e 63    assert( mem5.c
91cd0 75 72 72 65 6e 74 43 6f 75 6e 74 3e 30 20 7c 7c  urrentCount>0 ||
91ce0 20 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74   mem5.currentOut
91cf0 3d 3d 30 20 29 3b 0a 0a 20 20 6d 65 6d 35 2e 61  ==0 );..  mem5.a
91d00 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d 20 3d 20 43  Ctrl[iBlock] = C
91d10 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c 6f 67 73  TRL_FREE | iLogs
91d20 69 7a 65 3b 0a 20 20 77 68 69 6c 65 28 20 69 4c  ize;.  while( iL
91d30 6f 67 73 69 7a 65 3c 4c 4f 47 4d 41 58 20 29 7b  ogsize<LOGMAX ){
91d40 0a 20 20 20 20 69 6e 74 20 69 42 75 64 64 79 3b  .    int iBuddy;
91d50 0a 20 20 20 20 69 66 28 20 28 69 42 6c 6f 63 6b  .    if( (iBlock
91d60 3e 3e 69 4c 6f 67 73 69 7a 65 29 20 26 20 31 20  >>iLogsize) & 1 
91d70 29 7b 0a 20 20 20 20 20 20 69 42 75 64 64 79 20  ){.      iBuddy 
91d80 3d 20 69 42 6c 6f 63 6b 20 2d 20 73 69 7a 65 3b  = iBlock - size;
91d90 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
91da0 20 20 69 42 75 64 64 79 20 3d 20 69 42 6c 6f 63    iBuddy = iBloc
91db0 6b 20 2b 20 73 69 7a 65 3b 0a 20 20 20 20 7d 0a  k + size;.    }.
91dc0 20 20 20 20 61 73 73 65 72 74 28 20 69 42 75 64      assert( iBud
91dd0 64 79 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  dy>=0 );.    if(
91de0 20 28 69 42 75 64 64 79 2b 28 31 3c 3c 69 4c 6f   (iBuddy+(1<<iLo
91df0 67 73 69 7a 65 29 29 3e 6d 65 6d 35 2e 6e 42 6c  gsize))>mem5.nBl
91e00 6f 63 6b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  ock ) break;.   
91e10 20 69 66 28 20 6d 65 6d 35 2e 61 43 74 72 6c 5b   if( mem5.aCtrl[
91e20 69 42 75 64 64 79 5d 21 3d 28 43 54 52 4c 5f 46  iBuddy]!=(CTRL_F
91e30 52 45 45 20 7c 20 69 4c 6f 67 73 69 7a 65 29 20  REE | iLogsize) 
91e40 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 6d 65 6d  ) break;.    mem
91e50 73 79 73 35 55 6e 6c 69 6e 6b 28 69 42 75 64 64  sys5Unlink(iBudd
91e60 79 2c 20 69 4c 6f 67 73 69 7a 65 29 3b 0a 20 20  y, iLogsize);.  
91e70 20 20 69 4c 6f 67 73 69 7a 65 2b 2b 3b 0a 20 20    iLogsize++;.  
91e80 20 20 69 66 28 20 69 42 75 64 64 79 3c 69 42 6c    if( iBuddy<iBl
91e90 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ock ){.      mem
91ea0 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79 5d 20  5.aCtrl[iBuddy] 
91eb0 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69 4c  = CTRL_FREE | iL
91ec0 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d 65  ogsize;.      me
91ed0 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d  m5.aCtrl[iBlock]
91ee0 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 42 6c 6f   = 0;.      iBlo
91ef0 63 6b 20 3d 20 69 42 75 64 64 79 3b 0a 20 20 20  ck = iBuddy;.   
91f00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65   }else{.      me
91f10 6d 35 2e 61 43 74 72 6c 5b 69 42 6c 6f 63 6b 5d  m5.aCtrl[iBlock]
91f20 20 3d 20 43 54 52 4c 5f 46 52 45 45 20 7c 20 69   = CTRL_FREE | i
91f30 4c 6f 67 73 69 7a 65 3b 0a 20 20 20 20 20 20 6d  Logsize;.      m
91f40 65 6d 35 2e 61 43 74 72 6c 5b 69 42 75 64 64 79  em5.aCtrl[iBuddy
91f50 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20  ] = 0;.    }.   
91f60 20 73 69 7a 65 20 2a 3d 20 32 3b 0a 20 20 7d 0a   size *= 2;.  }.
91f70 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28 69 42    memsys5Link(iB
91f80 6c 6f 63 6b 2c 20 69 4c 6f 67 73 69 7a 65 29 3b  lock, iLogsize);
91f90 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61  .}../*.** Alloca
91fa0 74 65 20 6e 42 79 74 65 73 20 6f 66 20 6d 65 6d  te nBytes of mem
91fb0 6f 72 79 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ory.*/.static vo
91fc0 69 64 20 2a 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f  id *memsys5Mallo
91fd0 63 28 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20  c(int nBytes){. 
91fe0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 2a   sqlite3_int64 *
91ff0 70 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 42 79  p = 0;.  if( nBy
92000 74 65 73 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d  tes>0 ){.    mem
92010 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20 20  sys5Enter();.   
92020 20 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c   p = memsys5Mall
92030 6f 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29  ocUnsafe(nBytes)
92040 3b 0a 20 20 20 20 6d 65 6d 73 79 73 35 4c 65 61  ;.    memsys5Lea
92050 76 65 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ve();.  }.  retu
92060 72 6e 20 28 76 6f 69 64 2a 29 70 3b 20 0a 7d 0a  rn (void*)p; .}.
92070 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 6d 65 6d 6f  ./*.** Free memo
92080 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
92090 69 64 20 6d 65 6d 73 79 73 35 46 72 65 65 28 76  id memsys5Free(v
920a0 6f 69 64 20 2a 70 50 72 69 6f 72 29 7b 0a 20 20  oid *pPrior){.  
920b0 69 66 28 20 70 50 72 69 6f 72 3d 3d 30 20 29 7b  if( pPrior==0 ){
920c0 0a 61 73 73 65 72 74 28 30 29 3b 0a 20 20 20 20  .assert(0);.    
920d0 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6d 65  return;.  }.  me
920e0 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20  msys5Enter();.  
920f0 6d 65 6d 73 79 73 35 46 72 65 65 55 6e 73 61 66  memsys5FreeUnsaf
92100 65 28 70 50 72 69 6f 72 29 3b 0a 20 20 6d 65 6d  e(pPrior);.  mem
92110 73 79 73 35 4c 65 61 76 65 28 29 3b 20 20 0a 7d  sys5Leave();  .}
92120 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74  ../*.** Change t
92130 68 65 20 73 69 7a 65 20 6f 66 20 61 6e 20 65 78  he size of an ex
92140 69 73 74 69 6e 67 20 6d 65 6d 6f 72 79 20 61 6c  isting memory al
92150 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74  location.*/.stat
92160 69 63 20 76 6f 69 64 20 2a 6d 65 6d 73 79 73 35  ic void *memsys5
92170 52 65 61 6c 6c 6f 63 28 76 6f 69 64 20 2a 70 50  Realloc(void *pP
92180 72 69 6f 72 2c 20 69 6e 74 20 6e 42 79 74 65 73  rior, int nBytes
92190 29 7b 0a 20 20 69 6e 74 20 6e 4f 6c 64 3b 0a 20  ){.  int nOld;. 
921a0 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69 66 28 20   void *p;.  if( 
921b0 70 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20  pPrior==0 ){.   
921c0 20 72 65 74 75 72 6e 20 6d 65 6d 73 79 73 35 4d   return memsys5M
921d0 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a 20  alloc(nBytes);. 
921e0 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73 3c   }.  if( nBytes<
921f0 3d 30 20 29 7b 0a 20 20 20 20 6d 65 6d 73 79 73  =0 ){.    memsys
92200 35 46 72 65 65 28 70 50 72 69 6f 72 29 3b 0a 20  5Free(pPrior);. 
92210 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
92220 0a 20 20 6e 4f 6c 64 20 3d 20 6d 65 6d 73 79 73  .  nOld = memsys
92230 35 53 69 7a 65 28 70 50 72 69 6f 72 29 3b 0a 20  5Size(pPrior);. 
92240 20 69 66 28 20 6e 42 79 74 65 73 3c 3d 6e 4f 6c   if( nBytes<=nOl
92250 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  d ){.    return 
92260 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 6d 65  pPrior;.  }.  me
92270 6d 73 79 73 35 45 6e 74 65 72 28 29 3b 0a 20 20  msys5Enter();.  
92280 70 20 3d 20 6d 65 6d 73 79 73 35 4d 61 6c 6c 6f  p = memsys5Mallo
92290 63 55 6e 73 61 66 65 28 6e 42 79 74 65 73 29 3b  cUnsafe(nBytes);
922a0 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20  .  if( p ){.    
922b0 6d 65 6d 63 70 79 28 70 2c 20 70 50 72 69 6f 72  memcpy(p, pPrior
922c0 2c 20 6e 4f 6c 64 29 3b 0a 20 20 20 20 6d 65 6d  , nOld);.    mem
922d0 73 79 73 35 46 72 65 65 55 6e 73 61 66 65 28 70  sys5FreeUnsafe(p
922e0 50 72 69 6f 72 29 3b 0a 20 20 7d 0a 20 20 6d 65  Prior);.  }.  me
922f0 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20 20  msys5Leave();.  
92300 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
92310 2a 2a 20 52 6f 75 6e 64 20 75 70 20 61 20 72 65  ** Round up a re
92320 71 75 65 73 74 20 73 69 7a 65 20 74 6f 20 74 68  quest size to th
92330 65 20 6e 65 78 74 20 76 61 6c 69 64 20 61 6c 6c  e next valid all
92340 6f 63 61 74 69 6f 6e 20 73 69 7a 65 2e 0a 2a 2f  ocation size..*/
92350 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 73  .static int mems
92360 79 73 35 52 6f 75 6e 64 75 70 28 69 6e 74 20 6e  ys5Roundup(int n
92370 29 7b 0a 20 20 69 6e 74 20 69 46 75 6c 6c 53 7a  ){.  int iFullSz
92380 3b 0a 20 20 66 6f 72 28 69 46 75 6c 6c 53 7a 3d  ;.  for(iFullSz=
92390 6d 65 6d 35 2e 6e 41 74 6f 6d 3b 20 69 46 75 6c  mem5.nAtom; iFul
923a0 6c 53 7a 3c 6e 3b 20 69 46 75 6c 6c 53 7a 20 2a  lSz<n; iFullSz *
923b0 3d 20 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  = 2);.  return i
923c0 46 75 6c 6c 53 7a 3b 0a 7d 0a 0a 73 74 61 74 69  FullSz;.}..stati
923d0 63 20 69 6e 74 20 6d 65 6d 73 79 73 35 4c 6f 67  c int memsys5Log
923e0 28 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20  (int iValue){.  
923f0 69 6e 74 20 69 4c 6f 67 3b 0a 20 20 66 6f 72 28  int iLog;.  for(
92400 69 4c 6f 67 3d 30 3b 20 28 31 3c 3c 69 4c 6f 67  iLog=0; (1<<iLog
92410 29 3c 69 56 61 6c 75 65 3b 20 69 4c 6f 67 2b 2b  )<iValue; iLog++
92420 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 4c 6f 67  );.  return iLog
92430 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  ;.}../*.** Initi
92440 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
92450 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
92460 20 6d 65 6d 73 79 73 35 49 6e 69 74 28 76 6f 69   memsys5Init(voi
92470 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 69  d *NotUsed){.  i
92480 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6e 42 79  nt ii;.  int nBy
92490 74 65 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  te = sqlite3Glob
924a0 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 3b 0a  alConfig.nHeap;.
924b0 20 20 75 38 20 2a 7a 42 79 74 65 20 3d 20 28 75    u8 *zByte = (u
924c0 38 20 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61  8 *)sqlite3Globa
924d0 6c 43 6f 6e 66 69 67 2e 70 48 65 61 70 3b 0a 20  lConfig.pHeap;. 
924e0 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 20 20 20   int nMinLog;   
924f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
92500 20 4c 6f 67 20 6f 66 20 6d 69 6e 69 6d 75 6d 20   Log of minimum 
92510 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 69 7a 65 20  allocation size 
92520 69 6e 20 62 79 74 65 73 2a 2f 0a 20 20 69 6e 74  in bytes*/.  int
92530 20 69 4f 66 66 73 65 74 3b 0a 0a 20 20 55 4e 55   iOffset;..  UNU
92540 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f  SED_PARAMETER(No
92550 74 55 73 65 64 29 3b 0a 0a 20 20 69 66 28 20 21  tUsed);..  if( !
92560 7a 42 79 74 65 20 29 7b 0a 20 20 20 20 72 65 74  zByte ){.    ret
92570 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
92580 3b 0a 20 20 7d 0a 0a 20 20 6e 4d 69 6e 4c 6f 67  ;.  }..  nMinLog
92590 20 3d 20 6d 65 6d 73 79 73 35 4c 6f 67 28 73 71   = memsys5Log(sq
925a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
925b0 67 2e 6d 6e 52 65 71 29 3b 0a 20 20 6d 65 6d 35  g.mnReq);.  mem5
925c0 2e 6e 41 74 6f 6d 20 3d 20 28 31 3c 3c 6e 4d 69  .nAtom = (1<<nMi
925d0 6e 4c 6f 67 29 3b 0a 20 20 77 68 69 6c 65 28 20  nLog);.  while( 
925e0 28 69 6e 74 29 73 69 7a 65 6f 66 28 4d 65 6d 35  (int)sizeof(Mem5
925f0 4c 69 6e 6b 29 3e 6d 65 6d 35 2e 6e 41 74 6f 6d  Link)>mem5.nAtom
92600 20 29 7b 0a 20 20 20 20 6d 65 6d 35 2e 6e 41 74   ){.    mem5.nAt
92610 6f 6d 20 3d 20 6d 65 6d 35 2e 6e 41 74 6f 6d 20  om = mem5.nAtom 
92620 3c 3c 20 31 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d  << 1;.  }..  mem
92630 35 2e 6e 42 6c 6f 63 6b 20 3d 20 28 6e 42 79 74  5.nBlock = (nByt
92640 65 20 2f 20 28 6d 65 6d 35 2e 6e 41 74 6f 6d 2b  e / (mem5.nAtom+
92650 73 69 7a 65 6f 66 28 75 38 29 29 29 3b 0a 20 20  sizeof(u8)));.  
92660 6d 65 6d 35 2e 7a 50 6f 6f 6c 20 3d 20 7a 42 79  mem5.zPool = zBy
92670 74 65 3b 0a 20 20 6d 65 6d 35 2e 61 43 74 72 6c  te;.  mem5.aCtrl
92680 20 3d 20 28 75 38 20 2a 29 26 6d 65 6d 35 2e 7a   = (u8 *)&mem5.z
92690 50 6f 6f 6c 5b 6d 65 6d 35 2e 6e 42 6c 6f 63 6b  Pool[mem5.nBlock
926a0 2a 6d 65 6d 35 2e 6e 41 74 6f 6d 5d 3b 0a 0a 20  *mem5.nAtom];.. 
926b0 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 3d 4c   for(ii=0; ii<=L
926c0 4f 47 4d 41 58 3b 20 69 69 2b 2b 29 7b 0a 20 20  OGMAX; ii++){.  
926d0 20 20 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73    mem5.aiFreelis
926e0 74 5b 69 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a  t[ii] = -1;.  }.
926f0 0a 20 20 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  .  iOffset = 0;.
92700 20 20 66 6f 72 28 69 69 3d 4c 4f 47 4d 41 58 3b    for(ii=LOGMAX;
92710 20 69 69 3e 3d 30 3b 20 69 69 2d 2d 29 7b 0a 20   ii>=0; ii--){. 
92720 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 20 3d 20     int nAlloc = 
92730 28 31 3c 3c 69 69 29 3b 0a 20 20 20 20 69 66 28  (1<<ii);.    if(
92740 20 28 69 4f 66 66 73 65 74 2b 6e 41 6c 6c 6f 63   (iOffset+nAlloc
92750 29 3c 3d 6d 65 6d 35 2e 6e 42 6c 6f 63 6b 20 29  )<=mem5.nBlock )
92760 7b 0a 20 20 20 20 20 20 6d 65 6d 35 2e 61 43 74  {.      mem5.aCt
92770 72 6c 5b 69 4f 66 66 73 65 74 5d 20 3d 20 69 69  rl[iOffset] = ii
92780 20 7c 20 43 54 52 4c 5f 46 52 45 45 3b 0a 20 20   | CTRL_FREE;.  
92790 20 20 20 20 6d 65 6d 73 79 73 35 4c 69 6e 6b 28      memsys5Link(
927a0 69 4f 66 66 73 65 74 2c 20 69 69 29 3b 0a 20 20  iOffset, ii);.  
927b0 20 20 20 20 69 4f 66 66 73 65 74 20 2b 3d 20 6e      iOffset += n
927c0 41 6c 6c 6f 63 3b 0a 20 20 20 20 7d 0a 20 20 20  Alloc;.    }.   
927d0 20 61 73 73 65 72 74 28 28 69 4f 66 66 73 65 74   assert((iOffset
927e0 2b 6e 41 6c 6c 6f 63 29 3e 6d 65 6d 35 2e 6e 42  +nAlloc)>mem5.nB
927f0 6c 6f 63 6b 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  lock);.  }..  re
92800 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
92810 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74 69  }../*.** Deiniti
92820 61 6c 69 7a 65 20 74 68 69 73 20 6d 6f 64 75 6c  alize this modul
92830 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
92840 64 20 6d 65 6d 73 79 73 35 53 68 75 74 64 6f 77  d memsys5Shutdow
92850 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29  n(void *NotUsed)
92860 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
92870 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20  ETER(NotUsed);. 
92880 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a   return;.}../*.*
92890 2a 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20  * Open the file 
928a0 69 6e 64 69 63 61 74 65 64 20 61 6e 64 20 77 72  indicated and wr
928b0 69 74 65 20 61 20 6c 6f 67 20 6f 66 20 61 6c 6c  ite a log of all
928c0 20 75 6e 66 72 65 65 64 20 6d 65 6d 6f 72 79 20   unfreed memory 
928d0 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6f 6e 73 20  .** allocations 
928e0 69 6e 74 6f 20 74 68 61 74 20 6c 6f 67 2e 0a 2a  into that log..*
928f0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
92900 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d 65 6d   void sqlite3Mem
92910 73 79 73 35 44 75 6d 70 28 63 6f 6e 73 74 20 63  sys5Dump(const c
92920 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
92930 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
92940 45 42 55 47 0a 20 20 46 49 4c 45 20 2a 6f 75 74  EBUG.  FILE *out
92950 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 6e 3b  ;.  int i, j, n;
92960 0a 20 20 69 6e 74 20 6e 4d 69 6e 4c 6f 67 3b 0a  .  int nMinLog;.
92970 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
92980 3d 3d 30 20 7c 7c 20 7a 46 69 6c 65 6e 61 6d 65  ==0 || zFilename
92990 5b 30 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 6f 75  [0]==0 ){.    ou
929a0 74 20 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7d 65  t = stdout;.  }e
929b0 6c 73 65 7b 0a 20 20 20 20 6f 75 74 20 3d 20 66  lse{.    out = f
929c0 6f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d 65 2c 20  open(zFilename, 
929d0 22 77 22 29 3b 0a 20 20 20 20 69 66 28 20 6f 75  "w");.    if( ou
929e0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 70  t==0 ){.      fp
929f0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 2a  rintf(stderr, "*
92a00 2a 20 55 6e 61 62 6c 65 20 74 6f 20 6f 75 74 70  * Unable to outp
92a10 75 74 20 6d 65 6d 6f 72 79 20 64 65 62 75 67 20  ut memory debug 
92a20 6f 75 74 70 75 74 20 6c 6f 67 3a 20 25 73 20 2a  output log: %s *
92a30 2a 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  *\n",.          
92a40 20 20 20 20 20 20 20 20 20 20 20 20 7a 46 69 6c              zFil
92a50 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72 65  ename);.      re
92a60 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  turn;.    }.  }.
92a70 20 20 6d 65 6d 73 79 73 35 45 6e 74 65 72 28 29    memsys5Enter()
92a80 3b 0a 20 20 6e 4d 69 6e 4c 6f 67 20 3d 20 6d 65  ;.  nMinLog = me
92a90 6d 73 79 73 35 4c 6f 67 28 6d 65 6d 35 2e 6e 41  msys5Log(mem5.nA
92aa0 74 6f 6d 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  tom);.  for(i=0;
92ab0 20 69 3c 3d 4c 4f 47 4d 41 58 20 26 26 20 69 2b   i<=LOGMAX && i+
92ac0 6e 4d 69 6e 4c 6f 67 3c 33 32 3b 20 69 2b 2b 29  nMinLog<32; i++)
92ad0 7b 0a 20 20 20 20 66 6f 72 28 6e 3d 30 2c 20 6a  {.    for(n=0, j
92ae0 3d 6d 65 6d 35 2e 61 69 46 72 65 65 6c 69 73 74  =mem5.aiFreelist
92af0 5b 69 5d 3b 20 6a 3e 3d 30 3b 20 6a 20 3d 20 4d  [i]; j>=0; j = M
92b00 45 4d 35 4c 49 4e 4b 28 6a 29 2d 3e 6e 65 78 74  EM5LINK(j)->next
92b10 2c 20 6e 2b 2b 29 7b 7d 0a 20 20 20 20 66 70 72  , n++){}.    fpr
92b20 69 6e 74 66 28 6f 75 74 2c 20 22 66 72 65 65 6c  intf(out, "freel
92b30 69 73 74 20 69 74 65 6d 73 20 6f 66 20 73 69 7a  ist items of siz
92b40 65 20 25 64 3a 20 25 64 5c 6e 22 2c 20 6d 65 6d  e %d: %d\n", mem
92b50 35 2e 6e 41 74 6f 6d 20 3c 3c 20 69 2c 20 6e 29  5.nAtom << i, n)
92b60 3b 0a 20 20 7d 0a 20 20 66 70 72 69 6e 74 66 28  ;.  }.  fprintf(
92b70 6f 75 74 2c 20 22 6d 65 6d 35 2e 6e 41 6c 6c 6f  out, "mem5.nAllo
92b80 63 20 20 20 20 20 20 20 3d 20 25 6c 6c 75 5c 6e  c       = %llu\n
92b90 22 2c 20 6d 65 6d 35 2e 6e 41 6c 6c 6f 63 29 3b  ", mem5.nAlloc);
92ba0 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20  .  fprintf(out, 
92bb0 22 6d 65 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63  "mem5.totalAlloc
92bc0 20 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65     = %llu\n", me
92bd0 6d 35 2e 74 6f 74 61 6c 41 6c 6c 6f 63 29 3b 0a  m5.totalAlloc);.
92be0 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
92bf0 6d 65 6d 35 2e 74 6f 74 61 6c 45 78 63 65 73 73  mem5.totalExcess
92c00 20 20 3d 20 25 6c 6c 75 5c 6e 22 2c 20 6d 65 6d    = %llu\n", mem
92c10 35 2e 74 6f 74 61 6c 45 78 63 65 73 73 29 3b 0a  5.totalExcess);.
92c20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
92c30 6d 65 6d 35 2e 63 75 72 72 65 6e 74 4f 75 74 20  mem5.currentOut 
92c40 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e    = %u\n", mem5.
92c50 63 75 72 72 65 6e 74 4f 75 74 29 3b 0a 20 20 66  currentOut);.  f
92c60 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d  printf(out, "mem
92c70 35 2e 63 75 72 72 65 6e 74 43 6f 75 6e 74 20 3d  5.currentCount =
92c80 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 63 75 72   %u\n", mem5.cur
92c90 72 65 6e 74 43 6f 75 6e 74 29 3b 0a 20 20 66 70  rentCount);.  fp
92ca0 72 69 6e 74 66 28 6f 75 74 2c 20 22 6d 65 6d 35  rintf(out, "mem5
92cb0 2e 6d 61 78 4f 75 74 20 20 20 20 20 20 20 3d 20  .maxOut       = 
92cc0 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e 6d 61 78 4f  %u\n", mem5.maxO
92cd0 75 74 29 3b 0a 20 20 66 70 72 69 6e 74 66 28 6f  ut);.  fprintf(o
92ce0 75 74 2c 20 22 6d 65 6d 35 2e 6d 61 78 43 6f 75  ut, "mem5.maxCou
92cf0 6e 74 20 20 20 20 20 3d 20 25 75 5c 6e 22 2c 20  nt     = %u\n", 
92d00 6d 65 6d 35 2e 6d 61 78 43 6f 75 6e 74 29 3b 0a  mem5.maxCount);.
92d10 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
92d20 6d 65 6d 35 2e 6d 61 78 52 65 71 75 65 73 74 20  mem5.maxRequest 
92d30 20 20 3d 20 25 75 5c 6e 22 2c 20 6d 65 6d 35 2e    = %u\n", mem5.
92d40 6d 61 78 52 65 71 75 65 73 74 29 3b 0a 20 20 6d  maxRequest);.  m
92d50 65 6d 73 79 73 35 4c 65 61 76 65 28 29 3b 0a 20  emsys5Leave();. 
92d60 20 69 66 28 20 6f 75 74 3d 3d 73 74 64 6f 75 74   if( out==stdout
92d70 20 29 7b 0a 20 20 20 20 66 66 6c 75 73 68 28 73   ){.    fflush(s
92d80 74 64 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b  tdout);.  }else{
92d90 0a 20 20 20 20 66 63 6c 6f 73 65 28 6f 75 74 29  .    fclose(out)
92da0 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 55 4e  ;.  }.#else.  UN
92db0 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 7a  USED_PARAMETER(z
92dc0 46 69 6c 65 6e 61 6d 65 29 3b 0a 23 65 6e 64 69  Filename);.#endi
92dd0 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  f.}../*.** This 
92de0 72 6f 75 74 69 6e 65 20 69 73 20 74 68 65 20 6f  routine is the o
92df0 6e 6c 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74  nly routine in t
92e00 68 69 73 20 66 69 6c 65 20 77 69 74 68 20 65 78  his file with ex
92e10 74 65 72 6e 61 6c 20 0a 2a 2a 20 6c 69 6e 6b 61  ternal .** linka
92e20 67 65 2e 20 49 74 20 72 65 74 75 72 6e 73 20 61  ge. It returns a
92e30 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74   pointer to a st
92e40 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d 65 6d  atic sqlite3_mem
92e50 5f 6d 65 74 68 6f 64 73 0a 2a 2a 20 73 74 72 75  _methods.** stru
92e60 63 74 20 70 6f 70 75 6c 61 74 65 64 20 77 69 74  ct populated wit
92e70 68 20 74 68 65 20 6d 65 6d 73 79 73 35 20 6d 65  h the memsys5 me
92e80 74 68 6f 64 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  thods..*/.SQLITE
92e90 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20 73  _PRIVATE const s
92ea0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
92eb0 64 73 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65  ds *sqlite3MemGe
92ec0 74 4d 65 6d 73 79 73 35 28 76 6f 69 64 29 7b 0a  tMemsys5(void){.
92ed0 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
92ee0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
92ef0 64 73 20 6d 65 6d 73 79 73 35 4d 65 74 68 6f 64  ds memsys5Method
92f00 73 20 3d 20 7b 0a 20 20 20 20 20 6d 65 6d 73 79  s = {.     memsy
92f10 73 35 4d 61 6c 6c 6f 63 2c 0a 20 20 20 20 20 6d  s5Malloc,.     m
92f20 65 6d 73 79 73 35 46 72 65 65 2c 0a 20 20 20 20  emsys5Free,.    
92f30 20 6d 65 6d 73 79 73 35 52 65 61 6c 6c 6f 63 2c   memsys5Realloc,
92f40 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 53 69 7a  .     memsys5Siz
92f50 65 2c 0a 20 20 20 20 20 6d 65 6d 73 79 73 35 52  e,.     memsys5R
92f60 6f 75 6e 64 75 70 2c 0a 20 20 20 20 20 6d 65 6d  oundup,.     mem
92f70 73 79 73 35 49 6e 69 74 2c 0a 20 20 20 20 20 6d  sys5Init,.     m
92f80 65 6d 73 79 73 35 53 68 75 74 64 6f 77 6e 2c 0a  emsys5Shutdown,.
92f90 20 20 20 20 20 30 0a 20 20 7d 3b 0a 20 20 72 65       0.  };.  re
92fa0 74 75 72 6e 20 26 6d 65 6d 73 79 73 35 4d 65 74  turn &memsys5Met
92fb0 68 6f 64 73 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  hods;.}..#endif 
92fc0 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  /* SQLITE_ENABLE
92fd0 5f 4d 45 4d 53 59 53 35 20 2a 2f 0a 0a 2f 2a 2a  _MEMSYS5 */../**
92fe0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
92ff0 20 6f 66 20 6d 65 6d 35 2e 63 20 2a 2a 2a 2a 2a   of mem5.c *****
93000 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
93030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
93040 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 2e 63 20  in file mutex.c 
93050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
93070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
93080 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 31  ** 2007 August 1
93090 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  4.**.** The auth
930a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
930b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
930c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
930d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
930e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
930f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
93100 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
93110 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
93120 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
93130 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
93140 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
93150 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
93160 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
93170 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
93180 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
93190 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
931a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
931b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
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 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
93200 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
93210 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
93220 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 2e 0a 2a  ement mutexes..*
93230 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
93240 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74 68 61  ontains code tha
93250 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 61 63 72 6f  t is common acro
93260 73 73 20 61 6c 6c 20 6d 75 74 65 78 20 69 6d 70  ss all mutex imp
93270 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 2e 0a 0a 2a  lementations...*
93280 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 2e  *.** $Id: mutex.
93290 63 2c 76 20 31 2e 32 39 20 32 30 30 38 2f 31 30  c,v 1.29 2008/10
932a0 2f 30 37 20 31 35 3a 32 35 3a 34 38 20 64 72 68  /07 15:25:48 drh
932b0 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64   Exp $.*/..#ifnd
932c0 65 66 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ef SQLITE_MUTEX_
932d0 4f 4d 49 54 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  OMIT./*.** Initi
932e0 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78 20  alize the mutex 
932f0 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  system..*/.SQLIT
93300 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
93310 6c 69 74 65 33 4d 75 74 65 78 49 6e 69 74 28 76  lite3MutexInit(v
93320 6f 69 64 29 7b 20 0a 20 20 69 6e 74 20 72 63 20  oid){ .  int rc 
93330 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
93340 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  f( sqlite3Global
93350 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65  Config.bCoreMute
93360 78 20 29 7b 0a 20 20 20 20 69 66 28 20 21 73 71  x ){.    if( !sq
93370 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
93380 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
93390 6c 6f 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  loc ){.      /* 
933a0 49 66 20 74 68 65 20 78 4d 75 74 65 78 41 6c 6c  If the xMutexAll
933b0 6f 63 20 6d 65 74 68 6f 64 20 68 61 73 20 6e 6f  oc method has no
933c0 74 20 62 65 65 6e 20 73 65 74 2c 20 74 68 65 6e  t been set, then
933d0 20 74 68 65 20 75 73 65 72 20 64 69 64 20 6e 6f   the user did no
933e0 74 0a 20 20 20 20 20 20 2a 2a 20 69 6e 73 74 61  t.      ** insta
933f0 6c 6c 20 61 20 6d 75 74 65 78 20 69 6d 70 6c 65  ll a mutex imple
93400 6d 65 6e 74 61 74 69 6f 6e 20 76 69 61 20 73 71  mentation via sq
93410 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 70  lite3_config() p
93420 72 69 6f 72 20 74 6f 20 0a 20 20 20 20 20 20 2a  rior to .      *
93430 2a 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  * sqlite3_initia
93440 6c 69 7a 65 28 29 20 62 65 69 6e 67 20 63 61 6c  lize() being cal
93450 6c 65 64 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20  led. This block 
93460 63 6f 70 69 65 73 20 70 6f 69 6e 74 65 72 73 20  copies pointers 
93470 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
93480 64 65 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e  default implemen
93490 74 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65 20  tation into the 
934a0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
934b0 66 69 67 20 73 74 72 75 63 74 75 72 65 2e 0a 20  fig structure.. 
934c0 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
934d0 20 54 68 65 20 64 61 6e 67 65 72 20 69 73 20 74   The danger is t
934e0 68 61 74 20 61 6c 74 68 6f 75 67 68 20 73 71 6c  hat although sql
934f0 69 74 65 33 5f 63 6f 6e 66 69 67 28 29 20 69 73  ite3_config() is
93500 20 6e 6f 74 20 61 20 74 68 72 65 61 64 73 61 66   not a threadsaf
93510 65 0a 20 20 20 20 20 20 2a 2a 20 41 50 49 2c 20  e.      ** API, 
93520 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
93530 7a 65 28 29 20 69 73 2c 20 61 6e 64 20 73 6f 20  ze() is, and so 
93540 6d 75 6c 74 69 70 6c 65 20 74 68 72 65 61 64 73  multiple threads
93550 20 6d 61 79 20 62 65 0a 20 20 20 20 20 20 2a 2a   may be.      **
93560 20 61 74 74 65 6d 70 74 69 6e 67 20 74 6f 20 72   attempting to r
93570 75 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  un this function
93580 20 73 69 6d 75 6c 74 61 6e 65 6f 75 73 6c 79 2e   simultaneously.
93590 20 54 6f 20 67 75 61 72 64 20 77 72 69 74 65 0a   To guard write.
935a0 20 20 20 20 20 20 2a 2a 20 61 63 63 65 73 73 20        ** access 
935b0 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33 47 6c  to the sqlite3Gl
935c0 6f 62 61 6c 43 6f 6e 66 69 67 20 73 74 72 75 63  obalConfig struc
935d0 74 75 72 65 2c 20 74 68 65 20 27 4d 41 53 54 45  ture, the 'MASTE
935e0 52 27 20 73 74 61 74 69 63 20 6d 75 74 65 78 0a  R' static mutex.
935f0 20 20 20 20 20 20 2a 2a 20 69 73 20 6f 62 74 61        ** is obta
93600 69 6e 65 64 20 62 65 66 6f 72 65 20 6d 6f 64 69  ined before modi
93610 66 79 69 6e 67 20 69 74 2e 0a 20 20 20 20 20 20  fying it..      
93620 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
93630 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a  _mutex_methods *
93640 70 20 3d 20 73 71 6c 69 74 65 33 44 65 66 61 75  p = sqlite3Defau
93650 6c 74 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  ltMutex();.     
93660 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
93670 70 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 0a  pMaster = 0;.  .
93680 20 20 20 20 20 20 72 63 20 3d 20 70 2d 3e 78 4d        rc = p->xM
93690 75 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 20 20  utexInit();.    
936a0 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
936b0 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 70  _OK ){.        p
936c0 4d 61 73 74 65 72 20 3d 20 70 2d 3e 78 4d 75 74  Master = p->xMut
936d0 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
936e0 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
936f0 45 52 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  ER);.        ass
93700 65 72 74 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ert(pMaster);.  
93710 20 20 20 20 20 20 70 2d 3e 78 4d 75 74 65 78 45        p->xMutexE
93720 6e 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20  nter(pMaster);. 
93730 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 73         assert( s
93740 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
93750 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41  ig.mutex.xMutexA
93760 6c 6c 6f 63 3d 3d 30 20 0a 20 20 20 20 20 20 20  lloc==0 .       
93770 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74 65 33        || sqlite3
93780 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
93790 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 3d 3d  ex.xMutexAlloc==
937a0 70 2d 3e 78 4d 75 74 65 78 41 6c 6c 6f 63 0a 20  p->xMutexAlloc. 
937b0 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
937c0 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 47 6c    if( !sqlite3Gl
937d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
937e0 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 20 29 7b 0a  .xMutexAlloc ){.
937f0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
93800 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75  3GlobalConfig.mu
93810 74 65 78 20 3d 20 2a 70 3b 0a 20 20 20 20 20 20  tex = *p;.      
93820 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 78    }.        p->x
93830 4d 75 74 65 78 4c 65 61 76 65 28 70 4d 61 73 74  MutexLeave(pMast
93840 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  er);.      }.   
93850 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63   }else{.      rc
93860 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
93870 43 6f 6e 66 69 67 2e 6d 75 74 65 78 2e 78 4d 75  Config.mutex.xMu
93880 74 65 78 49 6e 69 74 28 29 3b 0a 20 20 20 20 7d  texInit();.    }
93890 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
938a0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 68 75 74  c;.}../*.** Shut
938b0 64 6f 77 6e 20 74 68 65 20 6d 75 74 65 78 20 73  down the mutex s
938c0 79 73 74 65 6d 2e 20 54 68 69 73 20 63 61 6c 6c  ystem. This call
938d0 20 66 72 65 65 73 20 72 65 73 6f 75 72 63 65 73   frees resources
938e0 20 61 6c 6c 6f 63 61 74 65 64 20 62 79 0a 2a 2a   allocated by.**
938f0 20 73 71 6c 69 74 65 33 4d 75 74 65 78 49 6e 69   sqlite3MutexIni
93900 74 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  t()..*/.SQLITE_P
93910 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
93920 65 33 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29  e3MutexEnd(void)
93930 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
93940 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20 3d 20 73  ITE_OK;.  rc = s
93950 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
93960 69 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45  ig.mutex.xMutexE
93970 6e 64 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  nd();.  return r
93980 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 72  c;.}../*.** Retr
93990 69 65 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74  ieve a pointer t
939a0 6f 20 61 20 73 74 61 74 69 63 20 6d 75 74 65 78  o a static mutex
939b0 20 6f 72 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e   or allocate a n
939c0 65 77 20 64 79 6e 61 6d 69 63 20 6f 6e 65 2e 0a  ew dynamic one..
939d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71  */.SQLITE_API sq
939e0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c  lite3_mutex *sql
939f0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
93a00 28 69 6e 74 20 69 64 29 7b 0a 23 69 66 6e 64 65  (int id){.#ifnde
93a10 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
93a20 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73 71 6c  TOINIT.  if( sql
93a30 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28  ite3_initialize(
93a40 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  ) ) return 0;.#e
93a50 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 73 71  ndif.  return sq
93a60 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
93a70 67 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 41 6c  g.mutex.xMutexAl
93a80 6c 6f 63 28 69 64 29 3b 0a 7d 0a 0a 53 51 4c 49  loc(id);.}..SQLI
93a90 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74  TE_PRIVATE sqlit
93aa0 65 33 5f 6d 75 74 65 78 20 2a 73 71 6c 69 74 65  e3_mutex *sqlite
93ab0 33 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20  3MutexAlloc(int 
93ac0 69 64 29 7b 0a 20 20 69 66 28 20 21 73 71 6c 69  id){.  if( !sqli
93ad0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
93ae0 62 43 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20  bCoreMutex ){.  
93af0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
93b00 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
93b10 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74  GlobalConfig.mut
93b20 65 78 2e 78 4d 75 74 65 78 41 6c 6c 6f 63 28 69  ex.xMutexAlloc(i
93b30 64 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65  d);.}../*.** Fre
93b40 65 20 61 20 64 79 6e 61 6d 69 63 20 6d 75 74 65  e a dynamic mute
93b50 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  x..*/.SQLITE_API
93b60 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d 75   void sqlite3_mu
93b70 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65 33  tex_free(sqlite3
93b80 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 66  _mutex *p){.  if
93b90 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ( p ){.    sqlit
93ba0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
93bb0 75 74 65 78 2e 78 4d 75 74 65 78 46 72 65 65 28  utex.xMutexFree(
93bc0 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
93bd0 20 4f 62 74 61 69 6e 20 74 68 65 20 6d 75 74 65   Obtain the mute
93be0 78 20 70 2e 20 49 66 20 73 6f 6d 65 20 6f 74 68  x p. If some oth
93bf0 65 72 20 74 68 72 65 61 64 20 61 6c 72 65 61 64  er thread alread
93c00 79 20 68 61 73 20 74 68 65 20 6d 75 74 65 78 2c  y has the mutex,
93c10 20 62 6c 6f 63 6b 0a 2a 2a 20 75 6e 74 69 6c 20   block.** until 
93c20 69 74 20 63 61 6e 20 62 65 20 6f 62 74 61 69 6e  it can be obtain
93c30 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ed..*/.SQLITE_AP
93c40 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 6d  I void sqlite3_m
93c50 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74  utex_enter(sqlit
93c60 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
93c70 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71 6c  if( p ){.    sql
93c80 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
93c90 2e 6d 75 74 65 78 2e 78 4d 75 74 65 78 45 6e 74  .mutex.xMutexEnt
93ca0 65 72 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  er(p);.  }.}../*
93cb0 0a 2a 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 6d  .** Obtain the m
93cc0 75 74 65 78 20 70 2e 20 49 66 20 73 75 63 63 65  utex p. If succe
93cd0 73 73 66 75 6c 2c 20 72 65 74 75 72 6e 20 53 51  ssful, return SQ
93ce0 4c 49 54 45 5f 4f 4b 2e 20 4f 74 68 65 72 77 69  LITE_OK. Otherwi
93cf0 73 65 2c 20 69 66 20 61 6e 6f 74 68 65 72 0a 2a  se, if another.*
93d00 2a 20 74 68 72 65 61 64 20 68 6f 6c 64 73 20 74  * thread holds t
93d10 68 65 20 6d 75 74 65 78 20 61 6e 64 20 69 74 20  he mutex and it 
93d20 63 61 6e 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e  cannot be obtain
93d30 65 64 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ed, return SQLIT
93d40 45 5f 42 55 53 59 2e 0a 2a 2f 0a 53 51 4c 49 54  E_BUSY..*/.SQLIT
93d50 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
93d60 33 5f 6d 75 74 65 78 5f 74 72 79 28 73 71 6c 69  3_mutex_try(sqli
93d70 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
93d80 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
93d90 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  _OK;.  if( p ){.
93da0 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
93db0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
93dc0 75 74 65 78 2e 78 4d 75 74 65 78 54 72 79 28 70  utex.xMutexTry(p
93dd0 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
93de0 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
93df0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
93e00 65 61 76 65 28 29 20 72 6f 75 74 69 6e 65 20 65  eave() routine e
93e10 78 69 74 73 20 61 20 6d 75 74 65 78 20 74 68 61  xits a mutex tha
93e20 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
93e30 0a 2a 2a 20 65 6e 74 65 72 65 64 20 62 79 20 74  .** entered by t
93e40 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
93e50 20 54 68 65 20 62 65 68 61 76 69 6f 72 20 69 73   The behavior is
93e60 20 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68   undefined if th
93e70 65 20 6d 75 74 65 78 20 0a 2a 2a 20 69 73 20 6e  e mutex .** is n
93e80 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 65 6e 74  ot currently ent
93e90 65 72 65 64 2e 20 49 66 20 61 20 4e 55 4c 4c 20  ered. If a NULL 
93ea0 70 6f 69 6e 74 65 72 20 69 73 20 70 61 73 73 65  pointer is passe
93eb0 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
93ec0 0a 2a 2a 20 74 68 69 73 20 66 75 6e 63 74 69 6f  .** this functio
93ed0 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
93ee0 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
93ef0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
93f00 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
93f10 65 78 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  ex *p){.  if( p 
93f20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
93f30 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
93f40 2e 78 4d 75 74 65 78 4c 65 61 76 65 28 70 29 3b  .xMutexLeave(p);
93f50 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  .  }.}..#ifndef 
93f60 4e 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65  NDEBUG./*.** The
93f70 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
93f80 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
93f90 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
93fa0 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a  ) routine are.**
93fb0 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
93fc0 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  e inside assert(
93fd0 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  ) statements..*/
93fe0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
93ff0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
94000 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
94010 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70   *p){.  return p
94020 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c  ==0 || sqlite3Gl
94030 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 75 74 65 78  obalConfig.mutex
94040 2e 78 4d 75 74 65 78 48 65 6c 64 28 70 29 3b 0a  .xMutexHeld(p);.
94050 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
94060 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e   sqlite3_mutex_n
94070 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
94080 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
94090 72 6e 20 70 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  rn p==0 || sqlit
940a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
940b0 75 74 65 78 2e 78 4d 75 74 65 78 4e 6f 74 68 65  utex.xMutexNothe
940c0 6c 64 28 70 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  ld(p);.}.#endif.
940d0 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
940e0 45 5f 4f 4d 49 54 5f 4d 55 54 45 58 20 2a 2f 0a  E_OMIT_MUTEX */.
940f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
94100 20 45 6e 64 20 6f 66 20 6d 75 74 65 78 2e 63 20   End of mutex.c 
94110 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94130 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
94140 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
94150 20 42 65 67 69 6e 20 66 69 6c 65 20 6d 75 74 65   Begin file mute
94160 78 5f 6e 6f 6f 70 2e 63 20 2a 2a 2a 2a 2a 2a 2a  x_noop.c *******
94170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94180 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
94190 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4f 63 74 6f  ./*.** 2008 Octo
941a0 62 65 72 20 30 37 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 07.**.** The
941b0 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
941c0 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
941d0 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
941e0 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
941f0 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
94200 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
94210 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
94220 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
94230 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
94240 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
94250 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
94260 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
94270 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
94280 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
94290 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
942a0 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
942b0 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
942c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
942d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
942e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
942f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
94300 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
94310 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
94320 43 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61 74  C functions that
94330 20 69 6d 70 6c 65 6d 65 6e 74 20 6d 75 74 65 78   implement mutex
94340 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  es..**.** This i
94350 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 6e  mplementation in
94360 20 74 68 69 73 20 66 69 6c 65 20 64 6f 65 73 20   this file does 
94370 6e 6f 74 20 70 72 6f 76 69 64 65 20 61 6e 79 20  not provide any 
94380 6d 75 74 75 61 6c 0a 2a 2a 20 65 78 63 6c 75 73  mutual.** exclus
94390 69 6f 6e 20 61 6e 64 20 69 73 20 74 68 75 73 20  ion and is thus 
943a0 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65  suitable for use
943b0 20 6f 6e 6c 79 20 69 6e 20 61 70 70 6c 69 63 61   only in applica
943c0 74 69 6f 6e 73 0a 2a 2a 20 74 68 61 74 20 75 73  tions.** that us
943d0 65 20 53 51 4c 69 74 65 20 69 6e 20 61 20 73 69  e SQLite in a si
943e0 6e 67 6c 65 20 74 68 72 65 61 64 2e 20 20 54 68  ngle thread.  Th
943f0 65 20 72 6f 75 74 69 6e 65 73 20 64 65 66 69 6e  e routines defin
94400 65 64 0a 2a 2a 20 68 65 72 65 20 61 72 65 20 70  ed.** here are p
94410 6c 61 63 65 2d 68 6f 6c 64 65 72 73 2e 20 20 41  lace-holders.  A
94420 70 70 6c 69 63 61 74 69 6f 6e 73 20 63 61 6e 20  pplications can 
94430 73 75 62 73 74 69 74 75 74 65 20 77 6f 72 6b 69  substitute worki
94440 6e 67 0a 2a 2a 20 6d 75 74 65 78 20 72 6f 75 74  ng.** mutex rout
94450 69 6e 65 73 20 61 74 20 73 74 61 72 74 2d 74 69  ines at start-ti
94460 6d 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 0a  me using the.**.
94470 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63  **     sqlite3_c
94480 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f 4e  onfig(SQLITE_CON
94490 46 49 47 5f 4d 55 54 45 58 2c 2e 2e 2e 29 0a 2a  FIG_MUTEX,...).*
944a0 2a 0a 2a 2a 20 69 6e 74 65 72 66 61 63 65 2e 0a  *.** interface..
944b0 2a 2a 0a 2a 2a 20 49 66 20 63 6f 6d 70 69 6c 65  **.** If compile
944c0 64 20 77 69 74 68 20 53 51 4c 49 54 45 5f 44 45  d with SQLITE_DE
944d0 42 55 47 2c 20 74 68 65 6e 20 61 64 64 69 74 69  BUG, then additi
944e0 6f 6e 61 6c 20 6c 6f 67 69 63 20 69 73 20 69 6e  onal logic is in
944f0 73 65 72 74 65 64 0a 2a 2a 20 74 68 61 74 20 64  serted.** that d
94500 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b 69  oes error checki
94510 6e 67 20 6f 6e 20 6d 75 74 65 78 65 73 20 74 6f  ng on mutexes to
94520 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 79 20   make sure they 
94530 61 72 65 20 62 65 69 6e 67 0a 2a 2a 20 63 61 6c  are being.** cal
94540 6c 65 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a  led correctly..*
94550 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65 78 5f  *.** $Id: mutex_
94560 6e 6f 6f 70 2e 63 2c 76 20 31 2e 32 20 32 30 30  noop.c,v 1.2 200
94570 38 2f 31 30 2f 31 35 20 31 39 3a 30 33 3a 30 33  8/10/15 19:03:03
94580 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
94590 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
945a0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26  TE_MUTEX_NOOP) &
945b0 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
945c0 45 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 53  E_DEBUG)./*.** S
945d0 74 75 62 20 72 6f 75 74 69 6e 65 73 20 66 6f 72  tub routines for
945e0 20 61 6c 6c 20 6d 75 74 65 78 20 6d 65 74 68 6f   all mutex metho
945f0 64 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ds..**.** This r
94600 6f 75 74 69 6e 65 73 20 70 72 6f 76 69 64 65 20  outines provide 
94610 6e 6f 20 6d 75 74 75 61 6c 20 65 78 63 6c 75 73  no mutual exclus
94620 69 6f 6e 20 6f 72 20 65 72 72 6f 72 20 63 68 65  ion or error che
94630 63 6b 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  cking..*/.static
94640 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65 78 48 65   int noopMutexHe
94650 6c 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ld(sqlite3_mutex
94660 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 31 3b 20   *p){ return 1; 
94670 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f  }.static int noo
94680 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 73 71  pMutexNotheld(sq
94690 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
946a0 20 72 65 74 75 72 6e 20 31 3b 20 7d 0a 73 74 61   return 1; }.sta
946b0 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65  tic int noopMute
946c0 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65 74  xInit(void){ ret
946d0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d  urn SQLITE_OK; }
946e0 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6f 70  .static int noop
946f0 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
94700 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
94710 3b 20 7d 0a 73 74 61 74 69 63 20 73 71 6c 69 74  ; }.static sqlit
94720 65 33 5f 6d 75 74 65 78 20 2a 6e 6f 6f 70 4d 75  e3_mutex *noopMu
94730 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 64 29  texAlloc(int id)
94740 7b 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74 65  { return (sqlite
94750 33 5f 6d 75 74 65 78 2a 29 38 3b 20 7d 0a 73 74  3_mutex*)8; }.st
94760 61 74 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75  atic void noopMu
94770 74 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f  texFree(sqlite3_
94780 6d 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72  mutex *p){ retur
94790 6e 3b 20 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  n; }.static void
947a0 20 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 28   noopMutexEnter(
947b0 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
947c0 29 7b 20 72 65 74 75 72 6e 3b 20 7d 0a 73 74 61  ){ return; }.sta
947d0 74 69 63 20 69 6e 74 20 6e 6f 6f 70 4d 75 74 65  tic int noopMute
947e0 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74  xTry(sqlite3_mut
947f0 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e 20 53  ex *p){ return S
94800 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74  QLITE_OK; }.stat
94810 69 63 20 76 6f 69 64 20 6e 6f 6f 70 4d 75 74 65  ic void noopMute
94820 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d  xLeave(sqlite3_m
94830 75 74 65 78 20 2a 70 29 7b 20 72 65 74 75 72 6e  utex *p){ return
94840 3b 20 7d 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56  ; }..SQLITE_PRIV
94850 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ATE sqlite3_mute
94860 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c 69 74  x_methods *sqlit
94870 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78 28 76  e3DefaultMutex(v
94880 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73  oid){.  static s
94890 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74  qlite3_mutex_met
948a0 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20 7b 0a  hods sMutex = {.
948b0 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 49 6e 69      noopMutexIni
948c0 74 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78  t,.    noopMutex
948d0 45 6e 64 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74  End,.    noopMut
948e0 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6e 6f 6f  exAlloc,.    noo
948f0 70 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  pMutexFree,.    
94900 6e 6f 6f 70 4d 75 74 65 78 45 6e 74 65 72 2c 0a  noopMutexEnter,.
94910 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 54 72 79      noopMutexTry
94920 2c 0a 20 20 20 20 6e 6f 6f 70 4d 75 74 65 78 4c  ,.    noopMutexL
94930 65 61 76 65 2c 0a 0a 20 20 20 20 6e 6f 6f 70 4d  eave,..    noopM
94940 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6e 6f  utexHeld,.    no
94950 6f 70 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 20  opMutexNotheld. 
94960 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26 73   };..  return &s
94970 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66 20  Mutex;.}.#endif 
94980 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  /* defined(SQLIT
94990 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26  E_MUTEX_NOOP) &&
949a0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
949b0 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 23 69 66 20  _DEBUG) */..#if 
949c0 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4d  defined(SQLITE_M
949d0 55 54 45 58 5f 4e 4f 4f 50 29 20 26 26 20 64 65  UTEX_NOOP) && de
949e0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42  fined(SQLITE_DEB
949f0 55 47 29 0a 2f 2a 0a 2a 2a 20 49 6e 20 74 68 69  UG)./*.** In thi
94a00 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  s implementation
94a10 2c 20 65 72 72 6f 72 20 63 68 65 63 6b 69 6e 67  , error checking
94a20 20 69 73 20 70 72 6f 76 69 64 65 64 20 66 6f 72   is provided for
94a30 20 74 65 73 74 69 6e 67 0a 2a 2a 20 61 6e 64 20   testing.** and 
94a40 64 65 62 75 67 67 69 6e 67 20 70 75 72 70 6f 73  debugging purpos
94a50 65 73 2e 20 20 54 68 65 20 6d 75 74 65 78 65 73  es.  The mutexes
94a60 20 73 74 69 6c 6c 20 64 6f 20 6e 6f 74 20 70 72   still do not pr
94a70 6f 76 69 64 65 20 61 6e 79 0a 2a 2a 20 6d 75 74  ovide any.** mut
94a80 75 61 6c 20 65 78 63 6c 75 73 69 6f 6e 2e 0a 2a  ual exclusion..*
94a90 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6d 75 74  /../*.** The mut
94aa0 65 78 20 6f 62 6a 65 63 74 0a 2a 2f 0a 73 74 72  ex object.*/.str
94ab0 75 63 74 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  uct sqlite3_mute
94ac0 78 20 7b 0a 20 20 69 6e 74 20 69 64 3b 20 20 20  x {.  int id;   
94ad0 20 20 2f 2a 20 54 68 65 20 6d 75 74 65 78 20 74    /* The mutex t
94ae0 79 70 65 20 2a 2f 0a 20 20 69 6e 74 20 63 6e 74  ype */.  int cnt
94af0 3b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  ;    /* Number o
94b00 66 20 65 6e 74 72 69 65 73 20 77 69 74 68 6f 75  f entries withou
94b10 74 20 61 20 6d 61 74 63 68 69 6e 67 20 6c 65 61  t a matching lea
94b20 76 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  ve */.};../*.** 
94b30 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  The sqlite3_mute
94b40 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71 6c  x_held() and sql
94b50 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65  ite3_mutex_nothe
94b60 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72 65  ld() routine are
94b70 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f 72  .** intended for
94b80 20 75 73 65 20 69 6e 73 69 64 65 20 61 73 73 65   use inside asse
94b90 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e  rt() statements.
94ba0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64  .*/.static int d
94bb0 65 62 75 67 4d 75 74 65 78 48 65 6c 64 28 73 71  ebugMutexHeld(sq
94bc0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b  lite3_mutex *p){
94bd0 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c  .  return p==0 |
94be0 7c 20 70 2d 3e 63 6e 74 3e 30 3b 0a 7d 0a 73 74  | p->cnt>0;.}.st
94bf0 61 74 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75  atic int debugMu
94c00 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74  texNotheld(sqlit
94c10 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
94c20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70  return p==0 || p
94c30 2d 3e 63 6e 74 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a  ->cnt==0;.}../*.
94c40 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 6e  ** Initialize an
94c50 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20 74  d deinitialize t
94c60 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
94c70 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  em..*/.static in
94c80 74 20 64 65 62 75 67 4d 75 74 65 78 49 6e 69 74  t debugMutexInit
94c90 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53  (void){ return S
94ca0 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74  QLITE_OK; }.stat
94cb0 69 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65  ic int debugMute
94cc0 78 45 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75  xEnd(void){ retu
94cd0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a  rn SQLITE_OK; }.
94ce0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74  ./*.** The sqlit
94cf0 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29  e3_mutex_alloc()
94d00 20 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74   routine allocat
94d10 65 73 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65  es a new.** mute
94d20 78 20 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20  x and returns a 
94d30 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20  pointer to it.  
94d40 49 66 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55  If it returns NU
94d50 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73  LL.** that means
94d60 20 74 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f   that a mutex co
94d70 75 6c 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63  uld not be alloc
94d80 61 74 65 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  ated. .*/.static
94d90 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a   sqlite3_mutex *
94da0 64 65 62 75 67 4d 75 74 65 78 41 6c 6c 6f 63 28  debugMutexAlloc(
94db0 69 6e 74 20 69 64 29 7b 0a 20 20 73 74 61 74 69  int id){.  stati
94dc0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
94dd0 61 53 74 61 74 69 63 5b 36 5d 3b 0a 20 20 73 71  aStatic[6];.  sq
94de0 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 4e 65  lite3_mutex *pNe
94df0 77 20 3d 20 30 3b 0a 20 20 73 77 69 74 63 68 28  w = 0;.  switch(
94e00 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20   id ){.    case 
94e10 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53  SQLITE_MUTEX_FAS
94e20 54 3a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  T:.    case SQLI
94e30 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
94e40 56 45 3a 20 7b 0a 20 20 20 20 20 20 70 4e 65 77  VE: {.      pNew
94e50 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
94e60 28 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29 3b  (sizeof(*pNew));
94e70 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
94e80 29 7b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 2d  ){.        pNew-
94e90 3e 69 64 20 3d 20 69 64 3b 0a 20 20 20 20 20 20  >id = id;.      
94ea0 20 20 70 4e 65 77 2d 3e 63 6e 74 20 3d 20 30 3b    pNew->cnt = 0;
94eb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
94ec0 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
94ed0 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
94ee0 20 61 73 73 65 72 74 28 20 69 64 2d 32 20 3e 3d   assert( id-2 >=
94ef0 20 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65   0 );.      asse
94f00 72 74 28 20 69 64 2d 32 20 3c 20 73 69 7a 65 6f  rt( id-2 < sizeo
94f10 66 28 61 53 74 61 74 69 63 29 2f 73 69 7a 65 6f  f(aStatic)/sizeo
94f20 66 28 61 53 74 61 74 69 63 5b 30 5d 29 20 29 3b  f(aStatic[0]) );
94f30 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 26 61  .      pNew = &a
94f40 53 74 61 74 69 63 5b 69 64 2d 32 5d 3b 0a 20 20  Static[id-2];.  
94f50 20 20 20 20 70 4e 65 77 2d 3e 69 64 20 3d 20 69      pNew->id = i
94f60 64 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  d;.      break;.
94f70 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
94f80 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
94f90 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64  * This routine d
94fa0 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70 72 65  eallocates a pre
94fb0 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
94fc0 64 20 6d 75 74 65 78 2e 0a 2a 2f 0a 73 74 61 74  d mutex..*/.stat
94fd0 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74  ic void debugMut
94fe0 65 78 46 72 65 65 28 73 71 6c 69 74 65 33 5f 6d  exFree(sqlite3_m
94ff0 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
95000 72 74 28 20 70 2d 3e 63 6e 74 3d 3d 30 20 29 3b  rt( p->cnt==0 );
95010 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 64  .  assert( p->id
95020 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  ==SQLITE_MUTEX_F
95030 41 53 54 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51  AST || p->id==SQ
95040 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52  LITE_MUTEX_RECUR
95050 53 49 56 45 20 29 3b 0a 20 20 73 71 6c 69 74 65  SIVE );.  sqlite
95060 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
95070 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
95080 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e  mutex_enter() an
95090 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
950a0 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61  try() routines a
950b0 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74  ttempt.** to ent
950c0 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20  er a mutex.  If 
950d0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
950e0 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  s already within
950f0 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73   the mutex,.** s
95100 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
95110 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20  er() will block 
95120 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
95130 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74  x_try() will ret
95140 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  urn.** SQLITE_BU
95150 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  SY.  The sqlite3
95160 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74  _mutex_try() int
95170 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53  erface returns S
95180 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e  QLITE_OK.** upon
95190 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
951a0 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61  y.  Mutexes crea
951b0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45  ted using SQLITE
951c0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
951d0 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72   can.** be enter
951e0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
951f0 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  s by the same th
95200 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63  read.  In such c
95210 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74  ases the,.** mut
95220 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65  ex must be exite
95230 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65  d an equal numbe
95240 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72  r of times befor
95250 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  e another thread
95260 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20  .** can enter.  
95270 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
95280 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
95290 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
952a0 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72   of mutex.** mor
952b0 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
952c0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
952d0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  efined..*/.stati
952e0 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75 74 65  c void debugMute
952f0 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33 5f 6d  xEnter(sqlite3_m
95300 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65  utex *p){.  asse
95310 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54  rt( p->id==SQLIT
95320 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
95330 45 20 7c 7c 20 64 65 62 75 67 4d 75 74 65 78 4e  E || debugMutexN
95340 6f 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 70  otheld(p) );.  p
95350 2d 3e 63 6e 74 2b 2b 3b 0a 7d 0a 73 74 61 74 69  ->cnt++;.}.stati
95360 63 20 69 6e 74 20 64 65 62 75 67 4d 75 74 65 78  c int debugMutex
95370 54 72 79 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  Try(sqlite3_mute
95380 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28  x *p){.  assert(
95390 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
953a0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
953b0 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68  | debugMutexNoth
953c0 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e 63  eld(p) );.  p->c
953d0 6e 74 2b 2b 3b 0a 20 20 72 65 74 75 72 6e 20 53  nt++;.  return S
953e0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
953f0 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  ** The sqlite3_m
95400 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75  utex_leave() rou
95410 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74  tine exits a mut
95420 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70  ex that was.** p
95430 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65  reviously entere
95440 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  d by the same th
95450 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61 76  read.  The behav
95460 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69  ior.** is undefi
95470 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65 78  ned if the mutex
95480 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
95490 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20  y entered or.** 
954a0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
954b0 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c   allocated.  SQL
954c0 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64  ite will never d
954d0 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  o either..*/.sta
954e0 74 69 63 20 76 6f 69 64 20 64 65 62 75 67 4d 75  tic void debugMu
954f0 74 65 78 4c 65 61 76 65 28 73 71 6c 69 74 65 33  texLeave(sqlite3
95500 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
95510 73 65 72 74 28 20 64 65 62 75 67 4d 75 74 65 78  sert( debugMutex
95520 48 65 6c 64 28 70 29 20 29 3b 0a 20 20 70 2d 3e  Held(p) );.  p->
95530 63 6e 74 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28  cnt--;.  assert(
95540 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
95550 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c  UTEX_RECURSIVE |
95560 7c 20 64 65 62 75 67 4d 75 74 65 78 4e 6f 74 68  | debugMutexNoth
95570 65 6c 64 28 70 29 20 29 3b 0a 7d 0a 0a 53 51 4c  eld(p) );.}..SQL
95580 49 54 45 5f 50 52 49 56 41 54 45 20 73 71 6c 69  ITE_PRIVATE sqli
95590 74 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64  te3_mutex_method
955a0 73 20 2a 73 71 6c 69 74 65 33 44 65 66 61 75 6c  s *sqlite3Defaul
955b0 74 4d 75 74 65 78 28 76 6f 69 64 29 7b 0a 20 20  tMutex(void){.  
955c0 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 6d  static sqlite3_m
955d0 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 73 4d 75  utex_methods sMu
955e0 74 65 78 20 3d 20 7b 0a 20 20 20 20 64 65 62 75  tex = {.    debu
955f0 67 4d 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20  gMutexInit,.    
95600 64 65 62 75 67 4d 75 74 65 78 45 6e 64 2c 0a 20  debugMutexEnd,. 
95610 20 20 20 64 65 62 75 67 4d 75 74 65 78 41 6c 6c     debugMutexAll
95620 6f 63 2c 0a 20 20 20 20 64 65 62 75 67 4d 75 74  oc,.    debugMut
95630 65 78 46 72 65 65 2c 0a 20 20 20 20 64 65 62 75  exFree,.    debu
95640 67 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  gMutexEnter,.   
95650 20 64 65 62 75 67 4d 75 74 65 78 54 72 79 2c 0a   debugMutexTry,.
95660 20 20 20 20 64 65 62 75 67 4d 75 74 65 78 4c 65      debugMutexLe
95670 61 76 65 2c 0a 0a 20 20 20 20 64 65 62 75 67 4d  ave,..    debugM
95680 75 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 64 65  utexHeld,.    de
95690 62 75 67 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a  bugMutexNotheld.
956a0 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e 20 26    };..  return &
956b0 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64 69 66  sMutex;.}.#endif
956c0 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
956d0 54 45 5f 4d 55 54 45 58 5f 4e 4f 4f 50 29 20 26  TE_MUTEX_NOOP) &
956e0 26 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  & defined(SQLITE
956f0 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a 2a 2a  _DEBUG) */../***
95700 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
95710 6f 66 20 6d 75 74 65 78 5f 6e 6f 6f 70 2e 63 20  of mutex_noop.c 
95720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
95750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
95760 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 6f 73 32  n file mutex_os2
95770 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
95780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
957a0 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 38  * 2007 August 28
957b0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
957c0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
957d0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
957e0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
957f0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
95800 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
95810 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
95820 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
95830 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
95840 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
95850 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
95860 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
95870 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
95880 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
95890 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
958a0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
958b0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
958c0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
958d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
958e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
958f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
95910 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
95920 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e 63  tains the C func
95930 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c 65  tions that imple
95940 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f 72  ment mutexes for
95950 20 4f 53 2f 32 0a 2a 2a 0a 2a 2a 20 24 49 64 3a   OS/2.**.** $Id:
95960 20 6d 75 74 65 78 5f 6f 73 32 2e 63 2c 76 20 31   mutex_os2.c,v 1
95970 2e 31 30 20 32 30 30 38 2f 30 36 2f 32 33 20 32  .10 2008/06/23 2
95980 32 3a 31 33 3a 32 38 20 70 77 65 69 6c 62 61 63  2:13:28 pweilbac
95990 68 65 72 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  her Exp $.*/../*
959a0 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
959b0 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c  this file is onl
959c0 79 20 75 73 65 64 20 69 66 20 53 51 4c 49 54 45  y used if SQLITE
959d0 5f 4d 55 54 45 58 5f 4f 53 32 20 69 73 20 64 65  _MUTEX_OS2 is de
959e0 66 69 6e 65 64 2e 0a 2a 2a 20 53 65 65 20 74 68  fined..** See th
959f0 65 20 6d 75 74 65 78 2e 68 20 66 69 6c 65 20 66  e mutex.h file f
95a00 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f 0a 23  or details..*/.#
95a10 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 55 54  ifdef SQLITE_MUT
95a20 45 58 5f 4f 53 32 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  EX_OS2../*******
95a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  *************** 
95a40 4f 53 2f 32 20 4d 75 74 65 78 20 49 6d 70 6c 65  OS/2 Mutex Imple
95a50 6d 65 6e 74 61 74 69 6f 6e 20 2a 2a 2a 2a 2a 2a  mentation ******
95a60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
95a70 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6d 70 6c  .**.** This impl
95a80 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 6d 75  ementation of mu
95a90 74 65 78 65 73 20 69 73 20 62 75 69 6c 74 20 75  texes is built u
95aa0 73 69 6e 67 20 74 68 65 20 4f 53 2f 32 20 41 50  sing the OS/2 AP
95ab0 49 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  I..*/../*.** The
95ac0 20 6d 75 74 65 78 20 6f 62 6a 65 63 74 0a 2a 2a   mutex object.**
95ad0 20 45 61 63 68 20 72 65 63 75 72 73 69 76 65 20   Each recursive 
95ae0 6d 75 74 65 78 20 69 73 20 61 6e 20 69 6e 73 74  mutex is an inst
95af0 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
95b00 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
95b10 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69 74  .*/.struct sqlit
95b20 65 33 5f 6d 75 74 65 78 20 7b 0a 20 20 48 4d 54  e3_mutex {.  HMT
95b30 58 20 6d 75 74 65 78 3b 20 20 20 20 20 20 20 2f  X mutex;       /
95b40 2a 20 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c  * Mutex controll
95b50 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a  ing the lock */.
95b60 20 20 69 6e 74 20 20 69 64 3b 20 20 20 20 20 20    int  id;      
95b70 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70      /* Mutex typ
95b80 65 20 2a 2f 0a 20 20 69 6e 74 20 20 6e 52 65 66  e */.  int  nRef
95b90 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  ;        /* Numb
95ba0 65 72 20 6f 66 20 72 65 66 65 72 65 6e 63 65 73  er of references
95bb0 20 2a 2f 0a 20 20 54 49 44 20 20 6f 77 6e 65 72   */.  TID  owner
95bc0 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61  ;       /* Threa
95bd0 64 20 68 6f 6c 64 69 6e 67 20 74 68 69 73 20 6d  d holding this m
95be0 75 74 65 78 20 2a 2f 0a 7d 3b 0a 0a 23 64 65 66  utex */.};..#def
95bf0 69 6e 65 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e  ine OS2_MUTEX_IN
95c00 49 54 49 41 4c 49 5a 45 52 20 20 20 30 2c 30 2c  ITIALIZER   0,0,
95c10 30 2c 30 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69  0,0../*.** Initi
95c20 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e 69 74  alize and deinit
95c30 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74 65 78  ialize the mutex
95c40 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f 0a 73   subsystem..*/.s
95c50 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75 74  tatic int os2Mut
95c60 65 78 49 6e 69 74 28 76 6f 69 64 29 7b 20 72 65  exInit(void){ re
95c70 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  turn SQLITE_OK; 
95c80 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32  }.static int os2
95c90 4d 75 74 65 78 45 6e 64 28 76 6f 69 64 29 7b 20  MutexEnd(void){ 
95ca0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
95cb0 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73  ; }../*.** The s
95cc0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
95cd0 6f 63 28 29 20 72 6f 75 74 69 6e 65 20 61 6c 6c  oc() routine all
95ce0 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a 20  ocates a new.** 
95cf0 6d 75 74 65 78 20 61 6e 64 20 72 65 74 75 72 6e  mutex and return
95d00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  s a pointer to i
95d10 74 2e 20 20 49 66 20 69 74 20 72 65 74 75 72 6e  t.  If it return
95d20 73 20 4e 55 4c 4c 0a 2a 2a 20 74 68 61 74 20 6d  s NULL.** that m
95d30 65 61 6e 73 20 74 68 61 74 20 61 20 6d 75 74 65  eans that a mute
95d40 78 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 61  x could not be a
95d50 6c 6c 6f 63 61 74 65 64 2e 20 0a 2a 2a 20 53 51  llocated. .** SQ
95d60 4c 69 74 65 20 77 69 6c 6c 20 75 6e 77 69 6e 64  Lite will unwind
95d70 20 69 74 73 20 73 74 61 63 6b 20 61 6e 64 20 72   its stack and r
95d80 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 20  eturn an error. 
95d90 20 54 68 65 20 61 72 67 75 6d 65 6e 74 0a 2a 2a   The argument.**
95da0 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   to sqlite3_mute
95db0 78 5f 61 6c 6c 6f 63 28 29 20 69 73 20 6f 6e 65  x_alloc() is one
95dc0 20 6f 66 20 74 68 65 73 65 20 69 6e 74 65 67 65   of these intege
95dd0 72 20 63 6f 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a  r constants:.**.
95de0 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20  ** <ul>.** <li> 
95df0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41   SQLITE_MUTEX_FA
95e00 53 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ST              
95e10 20 30 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49   0.** <li>  SQLI
95e20 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
95e30 56 45 20 20 20 20 20 20 20 20 20 20 31 0a 2a 2a  VE          1.**
95e40 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55   <li>  SQLITE_MU
95e50 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45  TEX_STATIC_MASTE
95e60 52 20 20 20 20 20 20 32 0a 2a 2a 20 3c 6c 69 3e  R      2.** <li>
95e70 20 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53    SQLITE_MUTEX_S
95e80 54 41 54 49 43 5f 4d 45 4d 20 20 20 20 20 20 20  TATIC_MEM       
95e90 20 20 33 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c    3.** <li>  SQL
95ea0 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
95eb0 5f 50 52 4e 47 20 20 20 20 20 20 20 20 34 0a 2a  _PRNG        4.*
95ec0 2a 20 3c 2f 75 6c 3e 0a 2a 2a 0a 2a 2a 20 54 68  * </ul>.**.** Th
95ed0 65 20 66 69 72 73 74 20 74 77 6f 20 63 6f 6e 73  e first two cons
95ee0 74 61 6e 74 73 20 63 61 75 73 65 20 73 71 6c 69  tants cause sqli
95ef0 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28  te3_mutex_alloc(
95f00 29 20 74 6f 20 63 72 65 61 74 65 0a 2a 2a 20 61  ) to create.** a
95f10 20 6e 65 77 20 6d 75 74 65 78 2e 20 20 54 68 65   new mutex.  The
95f20 20 6e 65 77 20 6d 75 74 65 78 20 69 73 20 72 65   new mutex is re
95f30 63 75 72 73 69 76 65 20 77 68 65 6e 20 53 51 4c  cursive when SQL
95f40 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
95f50 49 56 45 0a 2a 2a 20 69 73 20 75 73 65 64 20 62  IVE.** is used b
95f60 75 74 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 69  ut not necessari
95f70 6c 79 20 73 6f 20 77 68 65 6e 20 53 51 4c 49 54  ly so when SQLIT
95f80 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 73 20  E_MUTEX_FAST is 
95f90 75 73 65 64 2e 0a 2a 2a 20 54 68 65 20 6d 75 74  used..** The mut
95fa0 65 78 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ex implementatio
95fb0 6e 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65 64 20  n does not need 
95fc0 74 6f 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  to make a distin
95fd0 63 74 69 6f 6e 0a 2a 2a 20 62 65 74 77 65 65 6e  ction.** between
95fe0 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45   SQLITE_MUTEX_RE
95ff0 43 55 52 53 49 56 45 20 61 6e 64 20 53 51 4c 49  CURSIVE and SQLI
96000 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 69 66  TE_MUTEX_FAST if
96010 20 69 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20   it does.** not 
96020 77 61 6e 74 20 74 6f 2e 20 20 42 75 74 20 53 51  want to.  But SQ
96030 4c 69 74 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 72  Lite will only r
96040 65 71 75 65 73 74 20 61 20 72 65 63 75 72 73 69  equest a recursi
96050 76 65 20 6d 75 74 65 78 20 69 6e 0a 2a 2a 20 63  ve mutex in.** c
96060 61 73 65 73 20 77 68 65 72 65 20 69 74 20 72 65  ases where it re
96070 61 6c 6c 79 20 6e 65 65 64 73 20 6f 6e 65 2e 20  ally needs one. 
96080 20 49 66 20 61 20 66 61 73 74 65 72 20 6e 6f 6e   If a faster non
96090 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78  -recursive mutex
960a0 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  .** implementati
960b0 6f 6e 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20  on is available 
960c0 6f 6e 20 74 68 65 20 68 6f 73 74 20 70 6c 61 74  on the host plat
960d0 66 6f 72 6d 2c 20 74 68 65 20 6d 75 74 65 78 20  form, the mutex 
960e0 73 75 62 73 79 73 74 65 6d 0a 2a 2a 20 6d 69 67  subsystem.** mig
960f0 68 74 20 72 65 74 75 72 6e 20 73 75 63 68 20 61  ht return such a
96100 20 6d 75 74 65 78 20 69 6e 20 72 65 73 70 6f 6e   mutex in respon
96110 73 65 20 74 6f 20 53 51 4c 49 54 45 5f 4d 55 54  se to SQLITE_MUT
96120 45 58 5f 46 41 53 54 2e 0a 2a 2a 0a 2a 2a 20 54  EX_FAST..**.** T
96130 68 65 20 6f 74 68 65 72 20 61 6c 6c 6f 77 65 64  he other allowed
96140 20 70 61 72 61 6d 65 74 65 72 73 20 74 6f 20 73   parameters to s
96150 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c  qlite3_mutex_all
96160 6f 63 28 29 20 65 61 63 68 20 72 65 74 75 72 6e  oc() each return
96170 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  .** a pointer to
96180 20 61 20 73 74 61 74 69 63 20 70 72 65 65 78 69   a static preexi
96190 73 74 69 6e 67 20 6d 75 74 65 78 2e 20 20 54 68  sting mutex.  Th
961a0 72 65 65 20 73 74 61 74 69 63 20 6d 75 74 65 78  ree static mutex
961b0 65 73 20 61 72 65 0a 2a 2a 20 75 73 65 64 20 62  es are.** used b
961c0 79 20 74 68 65 20 63 75 72 72 65 6e 74 20 76 65  y the current ve
961d0 72 73 69 6f 6e 20 6f 66 20 53 51 4c 69 74 65 2e  rsion of SQLite.
961e0 20 20 46 75 74 75 72 65 20 76 65 72 73 69 6f 6e    Future version
961f0 73 20 6f 66 20 53 51 4c 69 74 65 0a 2a 2a 20 6d  s of SQLite.** m
96200 61 79 20 61 64 64 20 61 64 64 69 74 69 6f 6e 61  ay add additiona
96210 6c 20 73 74 61 74 69 63 20 6d 75 74 65 78 65 73  l static mutexes
96220 2e 20 20 53 74 61 74 69 63 20 6d 75 74 65 78 65  .  Static mutexe
96230 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72 6e  s are for intern
96240 61 6c 0a 2a 2a 20 75 73 65 20 62 79 20 53 51 4c  al.** use by SQL
96250 69 74 65 20 6f 6e 6c 79 2e 20 20 41 70 70 6c 69  ite only.  Appli
96260 63 61 74 69 6f 6e 73 20 74 68 61 74 20 75 73 65  cations that use
96270 20 53 51 4c 69 74 65 20 6d 75 74 65 78 65 73 20   SQLite mutexes 
96280 73 68 6f 75 6c 64 0a 2a 2a 20 75 73 65 20 6f 6e  should.** use on
96290 6c 79 20 74 68 65 20 64 79 6e 61 6d 69 63 20 6d  ly the dynamic m
962a0 75 74 65 78 65 73 20 72 65 74 75 72 6e 65 64 20  utexes returned 
962b0 62 79 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  by SQLITE_MUTEX_
962c0 46 41 53 54 20 6f 72 0a 2a 2a 20 53 51 4c 49 54  FAST or.** SQLIT
962d0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
962e0 45 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  E..**.** Note th
962f0 61 74 20 69 66 20 6f 6e 65 20 6f 66 20 74 68 65  at if one of the
96300 20 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 20 70   dynamic mutex p
96310 61 72 61 6d 65 74 65 72 73 20 28 53 51 4c 49 54  arameters (SQLIT
96320 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
96330 6f 72 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  or SQLITE_MUTEX_
96340 52 45 43 55 52 53 49 56 45 29 20 69 73 20 75 73  RECURSIVE) is us
96350 65 64 20 74 68 65 6e 20 73 71 6c 69 74 65 33 5f  ed then sqlite3_
96360 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 0a 2a 2a  mutex_alloc().**
96370 20 72 65 74 75 72 6e 73 20 61 20 64 69 66 66 65   returns a diffe
96380 72 65 6e 74 20 6d 75 74 65 78 20 6f 6e 20 65 76  rent mutex on ev
96390 65 72 79 20 63 61 6c 6c 2e 20 20 42 75 74 20 66  ery call.  But f
963a0 6f 72 20 74 68 65 20 73 74 61 74 69 63 0a 2a 2a  or the static.**
963b0 20 6d 75 74 65 78 20 74 79 70 65 73 2c 20 74 68   mutex types, th
963c0 65 20 73 61 6d 65 20 6d 75 74 65 78 20 69 73 20  e same mutex is 
963d0 72 65 74 75 72 6e 65 64 20 6f 6e 20 65 76 65 72  returned on ever
963e0 79 20 63 61 6c 6c 20 74 68 61 74 20 68 61 73 0a  y call that has.
963f0 2a 2a 20 74 68 65 20 73 61 6d 65 20 74 79 70 65  ** the same type
96400 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61 74   number..*/.stat
96410 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
96420 20 2a 6f 73 32 4d 75 74 65 78 41 6c 6c 6f 63 28   *os2MutexAlloc(
96430 69 6e 74 20 69 54 79 70 65 29 7b 0a 20 20 73 71  int iType){.  sq
96440 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 20 3d  lite3_mutex *p =
96450 20 4e 55 4c 4c 3b 0a 20 20 73 77 69 74 63 68 28   NULL;.  switch(
96460 20 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61   iType ){.    ca
96470 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  se SQLITE_MUTEX_
96480 46 41 53 54 3a 0a 20 20 20 20 63 61 73 65 20 53  FAST:.    case S
96490 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
964a0 52 53 49 56 45 3a 20 7b 0a 20 20 20 20 20 20 70  RSIVE: {.      p
964b0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
964c0 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
964d0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20   );.      if( p 
964e0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64  ){.        p->id
964f0 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
96500 20 20 69 66 28 20 44 6f 73 43 72 65 61 74 65 4d    if( DosCreateM
96510 75 74 65 78 53 65 6d 28 20 30 2c 20 26 70 2d 3e  utexSem( 0, &p->
96520 6d 75 74 65 78 2c 20 30 2c 20 46 41 4c 53 45 20  mutex, 0, FALSE 
96530 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  ) != NO_ERROR ){
96540 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
96550 65 33 5f 66 72 65 65 28 20 70 20 29 3b 0a 20 20  e3_free( p );.  
96560 20 20 20 20 20 20 20 20 70 20 3d 20 4e 55 4c 4c          p = NULL
96570 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
96580 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
96590 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
965a0 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74  lt: {.      stat
965b0 69 63 20 76 6f 6c 61 74 69 6c 65 20 69 6e 74 20  ic volatile int 
965c0 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 20 20  isInit = 0;.    
965d0 20 20 73 74 61 74 69 63 20 73 71 6c 69 74 65 33    static sqlite3
965e0 5f 6d 75 74 65 78 20 73 74 61 74 69 63 4d 75 74  _mutex staticMut
965f0 65 78 65 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  exes[] = {.     
96600 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49     { OS2_MUTEX_I
96610 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
96620 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54         { OS2_MUT
96630 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
96640 7d 2c 0a 20 20 20 20 20 20 20 20 7b 20 4f 53 32  },.        { OS2
96650 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a  _MUTEX_INITIALIZ
96660 45 52 2c 20 7d 2c 0a 20 20 20 20 20 20 20 20 7b  ER, },.        {
96670 20 4f 53 32 5f 4d 55 54 45 58 5f 49 4e 49 54 49   OS2_MUTEX_INITI
96680 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20 20 20 20 20  ALIZER, },.     
96690 20 20 20 7b 20 4f 53 32 5f 4d 55 54 45 58 5f 49     { OS2_MUTEX_I
966a0 4e 49 54 49 41 4c 49 5a 45 52 2c 20 7d 2c 0a 20  NITIALIZER, },. 
966b0 20 20 20 20 20 20 20 7b 20 4f 53 32 5f 4d 55 54         { OS2_MUT
966c0 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 20  EX_INITIALIZER, 
966d0 7d 2c 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20  },.      };.    
966e0 20 20 69 66 20 28 20 21 69 73 49 6e 69 74 20 29    if ( !isInit )
966f0 7b 0a 20 20 20 20 20 20 20 20 41 50 49 52 45 54  {.        APIRET
96700 20 72 63 3b 0a 20 20 20 20 20 20 20 20 50 54 49   rc;.        PTI
96710 42 20 70 74 69 62 3b 0a 20 20 20 20 20 20 20 20  B ptib;.        
96720 50 50 49 42 20 70 70 69 62 3b 0a 20 20 20 20 20  PPIB ppib;.     
96730 20 20 20 48 4d 54 58 20 6d 75 74 65 78 3b 0a 20     HMTX mutex;. 
96740 20 20 20 20 20 20 20 63 68 61 72 20 6e 61 6d 65         char name
96750 5b 33 32 5d 3b 0a 20 20 20 20 20 20 20 20 44 6f  [32];.        Do
96760 73 47 65 74 49 6e 66 6f 42 6c 6f 63 6b 73 28 20  sGetInfoBlocks( 
96770 26 70 74 69 62 2c 20 26 70 70 69 62 20 29 3b 0a  &ptib, &ppib );.
96780 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
96790 73 6e 70 72 69 6e 74 66 28 20 73 69 7a 65 6f 66  snprintf( sizeof
967a0 28 6e 61 6d 65 29 2c 20 6e 61 6d 65 2c 20 22 5c  (name), name, "\
967b0 5c 53 45 4d 33 32 5c 5c 53 51 4c 49 54 45 25 30  \SEM32\\SQLITE%0
967c0 34 78 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  4x",.           
967d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
967e0 70 69 62 2d 3e 70 69 62 5f 75 6c 70 69 64 20 29  pib->pib_ulpid )
967f0 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ;.        while(
96800 20 21 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20   !isInit ){.    
96810 20 20 20 20 20 20 6d 75 74 65 78 20 3d 20 30 3b        mutex = 0;
96820 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
96830 44 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65  DosCreateMutexSe
96840 6d 28 20 6e 61 6d 65 2c 20 26 6d 75 74 65 78 2c  m( name, &mutex,
96850 20 30 2c 20 46 41 4c 53 45 29 3b 0a 20 20 20 20   0, FALSE);.    
96860 20 20 20 20 20 20 69 66 28 20 72 63 20 3d 3d 20        if( rc == 
96870 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
96880 20 20 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20          int i;. 
96890 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21             if( !
968a0 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20  isInit ){.      
968b0 20 20 20 20 20 20 20 20 66 6f 72 28 20 69 20 3d          for( i =
968c0 20 30 3b 20 69 20 3c 20 73 69 7a 65 6f 66 28 73   0; i < sizeof(s
968d0 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f 73 69  taticMutexes)/si
968e0 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78  zeof(staticMutex
968f0 65 73 5b 30 5d 29 3b 20 69 2b 2b 20 29 7b 0a 20  es[0]); i++ ){. 
96900 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 44                 D
96910 6f 73 43 72 65 61 74 65 4d 75 74 65 78 53 65 6d  osCreateMutexSem
96920 28 20 30 2c 20 26 73 74 61 74 69 63 4d 75 74 65  ( 0, &staticMute
96930 78 65 73 5b 69 5d 2e 6d 75 74 65 78 2c 20 30 2c  xes[i].mutex, 0,
96940 20 46 41 4c 53 45 20 29 3b 0a 20 20 20 20 20 20   FALSE );.      
96950 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
96960 20 20 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d          isInit =
96970 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20   1;.            
96980 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 44 6f  }.            Do
96990 73 43 6c 6f 73 65 4d 75 74 65 78 53 65 6d 28 20  sCloseMutexSem( 
969a0 6d 75 74 65 78 20 29 3b 0a 20 20 20 20 20 20 20  mutex );.       
969b0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 20     }else if( rc 
969c0 3d 3d 20 45 52 52 4f 52 5f 44 55 50 4c 49 43 41  == ERROR_DUPLICA
969d0 54 45 5f 4e 41 4d 45 20 29 7b 0a 20 20 20 20 20  TE_NAME ){.     
969e0 20 20 20 20 20 20 20 44 6f 73 53 6c 65 65 70 28         DosSleep(
969f0 20 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20   1 );.          
96a00 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
96a10 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20     return p;.   
96a20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
96a30 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
96a40 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d 32   assert( iType-2
96a50 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 61   >= 0 );.      a
96a60 73 73 65 72 74 28 20 69 54 79 70 65 2d 32 20 3c  ssert( iType-2 <
96a70 20 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75   sizeof(staticMu
96a80 74 65 78 65 73 29 2f 73 69 7a 65 6f 66 28 73 74  texes)/sizeof(st
96a90 61 74 69 63 4d 75 74 65 78 65 73 5b 30 5d 29 20  aticMutexes[0]) 
96aa0 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 26 73 74  );.      p = &st
96ab0 61 74 69 63 4d 75 74 65 78 65 73 5b 69 54 79 70  aticMutexes[iTyp
96ac0 65 2d 32 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69  e-2];.      p->i
96ad0 64 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20  d = iType;.     
96ae0 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
96af0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
96b00 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
96b10 74 69 6e 65 20 64 65 61 6c 6c 6f 63 61 74 65 73  tine deallocates
96b20 20 61 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6c   a previously al
96b30 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 0a 2a  located mutex..*
96b40 2a 20 53 51 4c 69 74 65 20 69 73 20 63 61 72 65  * SQLite is care
96b50 66 75 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74  ful to deallocat
96b60 65 20 65 76 65 72 79 20 6d 75 74 65 78 20 74 68  e every mutex th
96b70 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
96b80 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
96b90 6f 73 32 4d 75 74 65 78 46 72 65 65 28 73 71 6c  os2MutexFree(sql
96ba0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
96bb0 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
96bc0 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70  urn;.  assert( p
96bd0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61  ->nRef==0 );.  a
96be0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
96bf0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
96c00 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
96c10 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
96c20 20 29 3b 0a 20 20 44 6f 73 43 6c 6f 73 65 4d 75   );.  DosCloseMu
96c30 74 65 78 53 65 6d 28 20 70 2d 3e 6d 75 74 65 78  texSem( p->mutex
96c40 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   );.  sqlite3_fr
96c50 65 65 28 20 70 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ee( p );.}../*.*
96c60 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75  * The sqlite3_mu
96c70 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e 64 20  tex_enter() and 
96c80 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72  sqlite3_mutex_tr
96c90 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61 74 74  y() routines att
96ca0 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74 65 72  empt.** to enter
96cb0 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20 61 6e   a mutex.  If an
96cc0 6f 74 68 65 72 20 74 68 72 65 61 64 20 69 73 20  other thread is 
96cd0 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e 20 74  already within t
96ce0 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73 71 6c  he mutex,.** sql
96cf0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
96d00 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e  () will block an
96d10 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
96d20 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74 75 72  try() will retur
96d30 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55 53 59  n.** SQLITE_BUSY
96d40 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d  .  The sqlite3_m
96d50 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74 65 72  utex_try() inter
96d60 66 61 63 65 20 72 65 74 75 72 6e 73 20 53 51 4c  face returns SQL
96d70 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73  ITE_OK.** upon s
96d80 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72 79 2e  uccessful entry.
96d90 20 20 4d 75 74 65 78 65 73 20 63 72 65 61 74 65    Mutexes create
96da0 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45 5f 4d  d using SQLITE_M
96db0 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 63  UTEX_RECURSIVE c
96dc0 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72 65 64  an.** be entered
96dd0 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20   multiple times 
96de0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
96df0 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63 61 73  ad.  In such cas
96e00 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74 65 78  es the,.** mutex
96e10 20 6d 75 73 74 20 62 65 20 65 78 69 74 65 64 20   must be exited 
96e20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65 72 20  an equal number 
96e30 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72 65 20  of times before 
96e40 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 0a 2a  another thread.*
96e50 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20 49 66  * can enter.  If
96e60 20 74 68 65 20 73 61 6d 65 20 74 68 72 65 61 64   the same thread
96e70 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65 72 20   tries to enter 
96e80 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64 20 6f  any other kind o
96e90 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72 65 20  f mutex.** more 
96ea0 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65 20 62  than once, the b
96eb0 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65 66  ehavior is undef
96ec0 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ined..*/.static 
96ed0 76 6f 69 64 20 6f 73 32 4d 75 74 65 78 45 6e 74  void os2MutexEnt
96ee0 65 72 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  er(sqlite3_mutex
96ef0 20 2a 70 29 7b 0a 20 20 54 49 44 20 74 69 64 3b   *p){.  TID tid;
96f00 0a 20 20 50 49 44 20 68 6f 6c 64 65 72 31 3b 0a  .  PID holder1;.
96f10 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65 72 32 3b    ULONG holder2;
96f20 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
96f30 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
96f40 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
96f50 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c  TEX_RECURSIVE ||
96f60 20 6f 73 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64   os2MutexNotheld
96f70 28 70 29 20 29 3b 0a 20 20 44 6f 73 52 65 71 75  (p) );.  DosRequ
96f80 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d  estMutexSem(p->m
96f90 75 74 65 78 2c 20 53 45 4d 5f 49 4e 44 45 46 49  utex, SEM_INDEFI
96fa0 4e 49 54 45 5f 57 41 49 54 29 3b 0a 20 20 44 6f  NITE_WAIT);.  Do
96fb0 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d 28 70  sQueryMutexSem(p
96fc0 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64 65 72  ->mutex, &holder
96fd0 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64 65 72  1, &tid, &holder
96fe0 32 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72 20 3d  2);.  p->owner =
96ff0 20 74 69 64 3b 0a 20 20 70 2d 3e 6e 52 65 66 2b   tid;.  p->nRef+
97000 2b 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  +;.}.static int 
97010 6f 73 32 4d 75 74 65 78 54 72 79 28 73 71 6c 69  os2MutexTry(sqli
97020 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
97030 20 69 6e 74 20 72 63 3b 0a 20 20 54 49 44 20 74   int rc;.  TID t
97040 69 64 3b 0a 20 20 50 49 44 20 68 6f 6c 64 65 72  id;.  PID holder
97050 31 3b 0a 20 20 55 4c 4f 4e 47 20 68 6f 6c 64 65  1;.  ULONG holde
97060 72 32 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29  r2;.  if( p==0 )
97070 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
97080 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  K;.  assert( p->
97090 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
970a0 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 6f 73  _RECURSIVE || os
970b0 32 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29  2MutexNotheld(p)
970c0 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 52 65 71   );.  if( DosReq
970d0 75 65 73 74 4d 75 74 65 78 53 65 6d 28 70 2d 3e  uestMutexSem(p->
970e0 6d 75 74 65 78 2c 20 53 45 4d 5f 49 4d 4d 45 44  mutex, SEM_IMMED
970f0 49 41 54 45 5f 52 45 54 55 52 4e 29 20 3d 3d 20  IATE_RETURN) == 
97100 4e 4f 5f 45 52 52 4f 52 29 20 7b 0a 20 20 20 20  NO_ERROR) {.    
97110 44 6f 73 51 75 65 72 79 4d 75 74 65 78 53 65 6d  DosQueryMutexSem
97120 28 70 2d 3e 6d 75 74 65 78 2c 20 26 68 6f 6c 64  (p->mutex, &hold
97130 65 72 31 2c 20 26 74 69 64 2c 20 26 68 6f 6c 64  er1, &tid, &hold
97140 65 72 32 29 3b 0a 20 20 20 20 70 2d 3e 6f 77 6e  er2);.    p->own
97150 65 72 20 3d 20 74 69 64 3b 0a 20 20 20 20 70 2d  er = tid;.    p-
97160 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 72 63 20  >nRef++;.    rc 
97170 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
97180 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 63 20 3d   else {.    rc =
97190 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
971a0 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
971b0 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  }../*.** The sql
971c0 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
971d0 28 29 20 72 6f 75 74 69 6e 65 20 65 78 69 74 73  () routine exits
971e0 20 61 20 6d 75 74 65 78 20 74 68 61 74 20 77 61   a mutex that wa
971f0 73 0a 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79 20  s.** previously 
97200 65 6e 74 65 72 65 64 20 62 79 20 74 68 65 20 73  entered by the s
97210 61 6d 65 20 74 68 72 65 61 64 2e 20 20 54 68 65  ame thread.  The
97220 20 62 65 68 61 76 69 6f 72 0a 2a 2a 20 69 73 20   behavior.** is 
97230 75 6e 64 65 66 69 6e 65 64 20 69 66 20 74 68 65  undefined if the
97240 20 6d 75 74 65 78 20 69 73 20 6e 6f 74 20 63 75   mutex is not cu
97250 72 72 65 6e 74 6c 79 20 65 6e 74 65 72 65 64 20  rrently entered 
97260 6f 72 0a 2a 2a 20 69 73 20 6e 6f 74 20 63 75 72  or.** is not cur
97270 72 65 6e 74 6c 79 20 61 6c 6c 6f 63 61 74 65 64  rently allocated
97280 2e 20 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e  .  SQLite will n
97290 65 76 65 72 20 64 6f 20 65 69 74 68 65 72 2e 0a  ever do either..
972a0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f  */.static void o
972b0 73 32 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c  s2MutexLeave(sql
972c0 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
972d0 20 20 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44    TID tid;.  PID
972e0 20 68 6f 6c 64 65 72 31 3b 0a 20 20 55 4c 4f 4e   holder1;.  ULON
972f0 47 20 68 6f 6c 64 65 72 32 3b 0a 20 20 69 66 28  G holder2;.  if(
97300 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   p==0 ) return;.
97310 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65    assert( p->nRe
97320 66 3e 30 20 29 3b 0a 20 20 44 6f 73 51 75 65 72  f>0 );.  DosQuer
97330 79 4d 75 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74  yMutexSem(p->mut
97340 65 78 2c 20 26 68 6f 6c 64 65 72 31 2c 20 26 74  ex, &holder1, &t
97350 69 64 2c 20 26 68 6f 6c 64 65 72 32 29 3b 0a 20  id, &holder2);. 
97360 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65   assert( p->owne
97370 72 3d 3d 74 69 64 20 29 3b 0a 20 20 70 2d 3e 6e  r==tid );.  p->n
97380 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74 28  Ref--;.  assert(
97390 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20 70   p->nRef==0 || p
973a0 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54  ->id==SQLITE_MUT
973b0 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a  EX_RECURSIVE );.
973c0 20 20 44 6f 73 52 65 6c 65 61 73 65 4d 75 74 65    DosReleaseMute
973d0 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 29 3b 0a  xSem(p->mutex);.
973e0 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
973f0 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65  _DEBUG./*.** The
97400 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
97410 65 6c 64 28 29 20 61 6e 64 20 73 71 6c 69 74 65  eld() and sqlite
97420 33 5f 6d 75 74 65 78 5f 6e 6f 74 68 65 6c 64 28  3_mutex_notheld(
97430 29 20 72 6f 75 74 69 6e 65 20 61 72 65 0a 2a 2a  ) routine are.**
97440 20 69 6e 74 65 6e 64 65 64 20 66 6f 72 20 75 73   intended for us
97450 65 20 69 6e 73 69 64 65 20 61 73 73 65 72 74 28  e inside assert(
97460 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f  ) statements..*/
97470 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d  .static int os2M
97480 75 74 65 78 48 65 6c 64 28 73 71 6c 69 74 65 33  utexHeld(sqlite3
97490 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 54 49  _mutex *p){.  TI
974a0 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70 69 64  D tid;.  PID pid
974b0 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f 75 6e  ;.  ULONG ulCoun
974c0 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62 3b 0a  t;.  PTIB ptib;.
974d0 20 20 69 66 28 20 70 21 3d 30 20 29 20 7b 0a 20    if( p!=0 ) {. 
974e0 20 20 20 44 6f 73 51 75 65 72 79 4d 75 74 65 78     DosQueryMutex
974f0 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c 20 26 70  Sem(p->mutex, &p
97500 69 64 2c 20 26 74 69 64 2c 20 26 75 6c 43 6f 75  id, &tid, &ulCou
97510 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  nt);.  } else {.
97520 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c      DosGetInfoBl
97530 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c 4c  ocks(&ptib, NULL
97540 29 3b 0a 20 20 20 20 74 69 64 20 3d 20 70 74 69  );.    tid = pti
97550 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d 3e 74 69  b->tib_ptib2->ti
97560 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d 0a 20 20  b2_ultid;.  }.  
97570 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 28  return p==0 || (
97580 70 2d 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d  p->nRef!=0 && p-
97590 3e 6f 77 6e 65 72 3d 3d 74 69 64 29 3b 0a 7d 0a  >owner==tid);.}.
975a0 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 4d 75  static int os2Mu
975b0 74 65 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74  texNotheld(sqlit
975c0 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20  e3_mutex *p){.  
975d0 54 49 44 20 74 69 64 3b 0a 20 20 50 49 44 20 70  TID tid;.  PID p
975e0 69 64 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 43 6f  id;.  ULONG ulCo
975f0 75 6e 74 3b 0a 20 20 50 54 49 42 20 70 74 69 62  unt;.  PTIB ptib
97600 3b 0a 20 20 69 66 28 20 70 21 3d 20 30 20 29 20  ;.  if( p!= 0 ) 
97610 7b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 4d 75  {.    DosQueryMu
97620 74 65 78 53 65 6d 28 70 2d 3e 6d 75 74 65 78 2c  texSem(p->mutex,
97630 20 26 70 69 64 2c 20 26 74 69 64 2c 20 26 75 6c   &pid, &tid, &ul
97640 43 6f 75 6e 74 29 3b 0a 20 20 7d 20 65 6c 73 65  Count);.  } else
97650 20 7b 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66   {.    DosGetInf
97660 6f 42 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e  oBlocks(&ptib, N
97670 55 4c 4c 29 3b 0a 20 20 20 20 74 69 64 20 3d 20  ULL);.    tid = 
97680 70 74 69 62 2d 3e 74 69 62 5f 70 74 69 62 32 2d  ptib->tib_ptib2-
97690 3e 74 69 62 32 5f 75 6c 74 69 64 3b 0a 20 20 7d  >tib2_ultid;.  }
976a0 0a 20 20 72 65 74 75 72 6e 20 70 3d 3d 30 20 7c  .  return p==0 |
976b0 7c 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20  | p->nRef==0 || 
976c0 70 2d 3e 6f 77 6e 65 72 21 3d 74 69 64 3b 0a 7d  p->owner!=tid;.}
976d0 0a 23 65 6e 64 69 66 0a 0a 53 51 4c 49 54 45 5f  .#endif..SQLITE_
976e0 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
976f0 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
97700 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
97710 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
97720 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
97730 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
97740 3d 20 7b 0a 20 20 20 20 6f 73 32 4d 75 74 65 78  = {.    os2Mutex
97750 49 6e 69 74 2c 0a 20 20 20 20 6f 73 32 4d 75 74  Init,.    os2Mut
97760 65 78 45 6e 64 2c 0a 20 20 20 20 6f 73 32 4d 75  exEnd,.    os2Mu
97770 74 65 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 6f 73  texAlloc,.    os
97780 32 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  2MutexFree,.    
97790 6f 73 32 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20  os2MutexEnter,. 
977a0 20 20 20 6f 73 32 4d 75 74 65 78 54 72 79 2c 0a     os2MutexTry,.
977b0 20 20 20 20 6f 73 32 4d 75 74 65 78 4c 65 61 76      os2MutexLeav
977c0 65 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  e,.#ifdef SQLITE
977d0 5f 44 45 42 55 47 0a 20 20 20 20 6f 73 32 4d 75  _DEBUG.    os2Mu
977e0 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 6f 73 32  texHeld,.    os2
977f0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e  MutexNotheld.#en
97800 64 69 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75  dif.  };..  retu
97810 72 6e 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65  rn &sMutex;.}.#e
97820 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
97830 55 54 45 58 5f 4f 53 32 20 2a 2f 0a 0a 2f 2a 2a  UTEX_OS2 */../**
97840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
97850 20 6f 66 20 6d 75 74 65 78 5f 6f 73 32 2e 63 20   of mutex_os2.c 
97860 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97870 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97880 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
97890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
978a0 69 6e 20 66 69 6c 65 20 6d 75 74 65 78 5f 75 6e  in file mutex_un
978b0 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
978c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
978d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
978e0 2a 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32  ** 2007 August 2
978f0 38 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  8.**.** The auth
97900 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
97910 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
97920 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
97930 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
97940 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
97950 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
97960 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
97970 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
97980 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
97990 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
979a0 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
979b0 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
979c0 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
979d0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
979e0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
979f0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
97a00 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
97a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97a40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
97a50 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
97a60 6e 74 61 69 6e 73 20 74 68 65 20 43 20 66 75 6e  ntains the C fun
97a70 63 74 69 6f 6e 73 20 74 68 61 74 20 69 6d 70 6c  ctions that impl
97a80 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 66 6f  ement mutexes fo
97a90 72 20 70 74 68 72 65 61 64 73 0a 2a 2a 0a 2a 2a  r pthreads.**.**
97aa0 20 24 49 64 3a 20 6d 75 74 65 78 5f 75 6e 69 78   $Id: mutex_unix
97ab0 2e 63 2c 76 20 31 2e 31 35 20 32 30 30 38 2f 31  .c,v 1.15 2008/1
97ac0 31 2f 31 37 20 31 39 3a 31 38 3a 35 35 20 64 61  1/17 19:18:55 da
97ad0 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
97ae0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f  */../*.** The co
97af0 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
97b00 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20  is only used if 
97b10 77 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67  we are compiling
97b20 20 74 68 72 65 61 64 73 61 66 65 0a 2a 2a 20 75   threadsafe.** u
97b30 6e 64 65 72 20 75 6e 69 78 20 77 69 74 68 20 70  nder unix with p
97b40 74 68 72 65 61 64 73 2e 0a 2a 2a 0a 2a 2a 20 4e  threads..**.** N
97b50 6f 74 65 20 74 68 61 74 20 74 68 69 73 20 69 6d  ote that this im
97b60 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71  plementation req
97b70 75 69 72 65 73 20 61 20 76 65 72 73 69 6f 6e 20  uires a version 
97b80 6f 66 20 70 74 68 72 65 61 64 73 20 74 68 61 74  of pthreads that
97b90 0a 2a 2a 20 73 75 70 70 6f 72 74 73 20 72 65 63  .** supports rec
97ba0 75 72 73 69 76 65 20 6d 75 74 65 78 65 73 2e 0a  ursive mutexes..
97bb0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
97bc0 5f 4d 55 54 45 58 5f 50 54 48 52 45 41 44 53 0a  _MUTEX_PTHREADS.
97bd0 0a 23 69 6e 63 6c 75 64 65 20 3c 70 74 68 72 65  .#include <pthre
97be0 61 64 2e 68 3e 0a 0a 0a 2f 2a 0a 2a 2a 20 45 61  ad.h>.../*.** Ea
97bf0 63 68 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  ch recursive mut
97c00 65 78 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63  ex is an instanc
97c10 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
97c20 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  ng structure..*/
97c30 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f  .struct sqlite3_
97c40 6d 75 74 65 78 20 7b 0a 20 20 70 74 68 72 65 61  mutex {.  pthrea
97c50 64 5f 6d 75 74 65 78 5f 74 20 6d 75 74 65 78 3b  d_mutex_t mutex;
97c60 20 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f       /* Mutex co
97c70 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 65 20 6c 6f  ntrolling the lo
97c80 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 64 3b 20  ck */.  int id; 
97c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97ca0 20 20 20 2f 2a 20 4d 75 74 65 78 20 74 79 70 65     /* Mutex type
97cb0 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 66 3b 20   */.  int nRef; 
97cc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97cd0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e   /* Number of en
97ce0 74 72 61 6e 63 65 73 20 2a 2f 0a 20 20 70 74 68  trances */.  pth
97cf0 72 65 61 64 5f 74 20 6f 77 6e 65 72 3b 20 20 20  read_t owner;   
97d00 20 20 20 20 20 20 20 20 2f 2a 20 54 68 72 65 61          /* Threa
97d10 64 20 74 68 61 74 20 69 73 20 77 69 74 68 69 6e  d that is within
97d20 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f 0a 23   this mutex */.#
97d30 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
97d40 55 47 0a 20 20 69 6e 74 20 74 72 61 63 65 3b 20  UG.  int trace; 
97d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
97d60 2f 2a 20 54 72 75 65 20 74 6f 20 74 72 61 63 65  /* True to trace
97d70 20 63 68 61 6e 67 65 73 20 2a 2f 0a 23 65 6e 64   changes */.#end
97d80 69 66 0a 7d 3b 0a 23 69 66 64 65 66 20 53 51 4c  if.};.#ifdef SQL
97d90 49 54 45 5f 44 45 42 55 47 0a 23 64 65 66 69 6e  ITE_DEBUG.#defin
97da0 65 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f  e SQLITE3_MUTEX_
97db0 49 4e 49 54 49 41 4c 49 5a 45 52 20 7b 20 50 54  INITIALIZER { PT
97dc0 48 52 45 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54  HREAD_MUTEX_INIT
97dd0 49 41 4c 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28  IALIZER, 0, 0, (
97de0 70 74 68 72 65 61 64 5f 74 29 30 2c 20 30 20 7d  pthread_t)0, 0 }
97df0 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 53  .#else.#define S
97e00 51 4c 49 54 45 33 5f 4d 55 54 45 58 5f 49 4e 49  QLITE3_MUTEX_INI
97e10 54 49 41 4c 49 5a 45 52 20 7b 20 50 54 48 52 45  TIALIZER { PTHRE
97e20 41 44 5f 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c  AD_MUTEX_INITIAL
97e30 49 5a 45 52 2c 20 30 2c 20 30 2c 20 28 70 74 68  IZER, 0, 0, (pth
97e40 72 65 61 64 5f 74 29 30 20 7d 0a 23 65 6e 64 69  read_t)0 }.#endi
97e50 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71 6c  f../*.** The sql
97e60 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
97e70 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75  ) and sqlite3_mu
97e80 74 65 78 5f 6e 6f 74 68 65 6c 64 28 29 20 72 6f  tex_notheld() ro
97e90 75 74 69 6e 65 20 61 72 65 0a 2a 2a 20 69 6e 74  utine are.** int
97ea0 65 6e 64 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  ended for use on
97eb0 6c 79 20 69 6e 73 69 64 65 20 61 73 73 65 72 74  ly inside assert
97ec0 28 29 20 73 74 61 74 65 6d 65 6e 74 73 2e 20 20  () statements.  
97ed0 4f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f 72 6d  On some platform
97ee0 73 2c 0a 2a 2a 20 74 68 65 72 65 20 6d 69 67 68  s,.** there migh
97ef0 74 20 62 65 20 72 61 63 65 20 63 6f 6e 64 69 74  t be race condit
97f00 69 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 63 61  ions that can ca
97f10 75 73 65 20 74 68 65 73 65 20 72 6f 75 74 69 6e  use these routin
97f20 65 73 20 74 6f 0a 2a 2a 20 64 65 6c 69 76 65 72  es to.** deliver
97f30 20 69 6e 63 6f 72 72 65 63 74 20 72 65 73 75 6c   incorrect resul
97f40 74 73 2e 20 20 49 6e 20 70 61 72 74 69 63 75 6c  ts.  In particul
97f50 61 72 2c 20 69 66 20 70 74 68 72 65 61 64 5f 65  ar, if pthread_e
97f60 71 75 61 6c 28 29 20 69 73 0a 2a 2a 20 6e 6f 74  qual() is.** not
97f70 20 61 6e 20 61 74 6f 6d 69 63 20 6f 70 65 72 61   an atomic opera
97f80 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 73 65  tion, then these
97f90 20 72 6f 75 74 69 6e 65 73 20 6d 69 67 68 74 20   routines might 
97fa0 64 65 6c 69 76 65 72 79 0a 2a 2a 20 69 6e 63 6f  delivery.** inco
97fb0 72 72 65 63 74 20 72 65 73 75 6c 74 73 2e 20 20  rrect results.  
97fc0 4f 6e 20 6d 6f 73 74 20 70 6c 61 74 66 6f 72 6d  On most platform
97fd0 73 2c 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c  s, pthread_equal
97fe0 28 29 20 69 73 20 61 20 0a 2a 2a 20 63 6f 6d 70  () is a .** comp
97ff0 61 72 69 73 6f 6e 20 6f 66 20 74 77 6f 20 69 6e  arison of two in
98000 74 65 67 65 72 73 20 61 6e 64 20 69 73 20 74 68  tegers and is th
98010 65 72 65 66 6f 72 65 20 61 74 6f 6d 69 63 2e 20  erefore atomic. 
98020 20 42 75 74 20 77 65 20 61 72 65 0a 2a 2a 20 74   But we are.** t
98030 6f 6c 64 20 74 68 61 74 20 48 50 55 58 20 69 73  old that HPUX is
98040 20 6e 6f 74 20 73 75 63 68 20 61 20 70 6c 61 74   not such a plat
98050 66 6f 72 6d 2e 20 20 49 66 20 73 6f 2c 20 74 68  form.  If so, th
98060 65 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  en these routine
98070 73 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 61 6c  s.** will not al
98080 77 61 79 73 20 77 6f 72 6b 20 63 6f 72 72 65 63  ways work correc
98090 74 6c 79 20 6f 6e 20 48 50 55 58 2e 0a 2a 2a 0a  tly on HPUX..**.
980a0 2a 2a 20 4f 6e 20 74 68 6f 73 65 20 70 6c 61 74  ** On those plat
980b0 66 6f 72 6d 73 20 77 68 65 72 65 20 70 74 68 72  forms where pthr
980c0 65 61 64 5f 65 71 75 61 6c 28 29 20 69 73 20 6e  ead_equal() is n
980d0 6f 74 20 61 74 6f 6d 69 63 2c 20 53 51 4c 69 74  ot atomic, SQLit
980e0 65 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63  e.** should be c
980f0 6f 6d 70 69 6c 65 64 20 77 69 74 68 6f 75 74 20  ompiled without 
98100 2d 44 53 51 4c 49 54 45 5f 44 45 42 55 47 20 61  -DSQLITE_DEBUG a
98110 6e 64 20 77 69 74 68 20 2d 44 4e 44 45 42 55 47  nd with -DNDEBUG
98120 20 74 6f 0a 2a 2a 20 6d 61 6b 65 20 73 75 72 65   to.** make sure
98130 20 6e 6f 20 61 73 73 65 72 74 28 29 20 73 74 61   no assert() sta
98140 74 65 6d 65 6e 74 73 20 61 72 65 20 65 76 61 6c  tements are eval
98150 75 61 74 65 64 20 61 6e 64 20 68 65 6e 63 65 20  uated and hence 
98160 74 68 65 73 65 0a 2a 2a 20 72 6f 75 74 69 6e 65  these.** routine
98170 73 20 61 72 65 20 6e 65 76 65 72 20 63 61 6c 6c  s are never call
98180 65 64 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69  ed..*/.#if !defi
98190 6e 65 64 28 4e 44 45 42 55 47 29 20 7c 7c 20 64  ned(NDEBUG) || d
981a0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44 45  efined(SQLITE_DE
981b0 42 55 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20  BUG).static int 
981c0 70 74 68 72 65 61 64 4d 75 74 65 78 48 65 6c 64  pthreadMutexHeld
981d0 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a  (sqlite3_mutex *
981e0 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 2d  p){.  return (p-
981f0 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 74 68 72  >nRef!=0 && pthr
98200 65 61 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e  ead_equal(p->own
98210 65 72 2c 20 70 74 68 72 65 61 64 5f 73 65 6c 66  er, pthread_self
98220 28 29 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69  ()));.}.static i
98230 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 4e  nt pthreadMutexN
98240 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33 5f 6d  otheld(sqlite3_m
98250 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65 74 75  utex *p){.  retu
98260 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c  rn p->nRef==0 ||
98270 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28 70   pthread_equal(p
98280 2d 3e 6f 77 6e 65 72 2c 20 70 74 68 72 65 61 64  ->owner, pthread
98290 5f 73 65 6c 66 28 29 29 3d 3d 30 3b 0a 7d 0a 23  _self())==0;.}.#
982a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69  endif../*.** Ini
982b0 74 69 61 6c 69 7a 65 20 61 6e 64 20 64 65 69 6e  tialize and dein
982c0 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6d 75 74  itialize the mut
982d0 65 78 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a 2f  ex subsystem..*/
982e0 0a 73 74 61 74 69 63 20 69 6e 74 20 70 74 68 72  .static int pthr
982f0 65 61 64 4d 75 74 65 78 49 6e 69 74 28 76 6f 69  eadMutexInit(voi
98300 64 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54  d){ return SQLIT
98310 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69 63 20 69  E_OK; }.static i
98320 6e 74 20 70 74 68 72 65 61 64 4d 75 74 65 78 45  nt pthreadMutexE
98330 6e 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e  nd(void){ return
98340 20 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f   SQLITE_OK; }../
98350 2a 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33  *.** The sqlite3
98360 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72  _mutex_alloc() r
98370 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73  outine allocates
98380 20 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20   a new.** mutex 
98390 61 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f  and returns a po
983a0 69 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66  inter to it.  If
983b0 20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c   it returns NULL
983c0 0a 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74  .** that means t
983d0 68 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c  hat a mutex coul
983e0 64 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74  d not be allocat
983f0 65 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77  ed.  SQLite.** w
98400 69 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73  ill unwind its s
98410 74 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20  tack and return 
98420 61 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61  an error.  The a
98430 72 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71  rgument.** to sq
98440 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f  lite3_mutex_allo
98450 63 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  c() is one of th
98460 65 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73  ese integer cons
98470 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c  tants:.**.** <ul
98480 3e 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  >.** <li>  SQLIT
98490 45 5f 4d 55 54 45 58 5f 46 41 53 54 0a 2a 2a 20  E_MUTEX_FAST.** 
984a0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
984b0 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
984c0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
984d0 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54 45 52  EX_STATIC_MASTER
984e0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
984f0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 45  _MUTEX_STATIC_ME
98500 4d 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  M.** <li>  SQLIT
98510 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
98520 45 4d 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c  EM2.** <li>  SQL
98530 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
98540 5f 50 52 4e 47 0a 2a 2a 20 3c 6c 69 3e 20 20 53  _PRNG.** <li>  S
98550 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54  QLITE_MUTEX_STAT
98560 49 43 5f 4c 52 55 0a 2a 2a 20 3c 2f 75 6c 3e 0a  IC_LRU.** </ul>.
98570 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20  **.** The first 
98580 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63 61  two constants ca
98590 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  use sqlite3_mute
985a0 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72 65  x_alloc() to cre
985b0 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75 74  ate.** a new mut
985c0 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75 74  ex.  The new mut
985d0 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65 20  ex is recursive 
985e0 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45  when SQLITE_MUTE
985f0 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20 69  X_RECURSIVE.** i
98600 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20 6e  s used but not n
98610 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77 68  ecessarily so wh
98620 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  en SQLITE_MUTEX_
98630 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a 2a  FAST is used..**
98640 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c 65   The mutex imple
98650 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20 6e  mentation does n
98660 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20  ot need to make 
98670 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a 2a  a distinction.**
98680 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45 5f   between SQLITE_
98690 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 20  MUTEX_RECURSIVE 
986a0 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  and SQLITE_MUTEX
986b0 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65 73  _FAST if it does
986c0 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f 2e  .** not want to.
986d0 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69 6c    But SQLite wil
986e0 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20 61  l only request a
986f0 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78   recursive mutex
98700 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68 65   in.** cases whe
98710 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65 65  re it really nee
98720 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66 61  ds one.  If a fa
98730 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73 69  ster non-recursi
98740 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70 6c  ve mutex.** impl
98750 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61 76  ementation is av
98760 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 68  ailable on the h
98770 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74 68  ost platform, th
98780 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  e mutex subsyste
98790 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75 72  m.** might retur
987a0 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20 69  n such a mutex i
987b0 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53 51  n response to SQ
987c0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 2e  LITE_MUTEX_FAST.
987d0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65 72  .**.** The other
987e0 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65 74   allowed paramet
987f0 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f 6d  ers to sqlite3_m
98800 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61 63  utex_alloc() eac
98810 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f  h return.** a po
98820 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74 69  inter to a stati
98830 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d 75  c preexisting mu
98840 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61 74  tex.  Three stat
98850 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a 2a  ic mutexes are.*
98860 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63 75  * used by the cu
98870 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f 66  rrent version of
98880 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72 65   SQLite.  Future
98890 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c   versions of SQL
988a0 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20 61  ite.** may add a
988b0 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69 63  dditional static
988c0 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74 69   mutexes.  Stati
988d0 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66 6f  c mutexes are fo
988e0 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75 73  r internal.** us
988f0 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c 79  e by SQLite only
98900 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73 20  .  Applications 
98910 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65 20  that use SQLite 
98920 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a 2a  mutexes should.*
98930 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20 64  * use only the d
98940 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20 72  ynamic mutexes r
98950 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49 54  eturned by SQLIT
98960 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72 0a  E_MUTEX_FAST or.
98970 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  ** SQLITE_MUTEX_
98980 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a 2a  RECURSIVE..**.**
98990 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f 6e   Note that if on
989a0 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69 63  e of the dynamic
989b0 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65 72   mutex parameter
989c0 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  s (SQLITE_MUTEX_
989d0 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49 54  FAST.** or SQLIT
989e0 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56  E_MUTEX_RECURSIV
989f0 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e 20  E) is used then 
98a00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
98a10 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e 73  loc().** returns
98a20 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75 74   a different mut
98a30 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ex on every call
98a40 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20 73  .  But for the s
98a50 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78 20  tatic .** mutex 
98a60 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65 20  types, the same 
98a70 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e 65  mutex is returne
98a80 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c 20  d on every call 
98a90 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65 20  that has.** the 
98aa0 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65 72  same type number
98ab0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c 69  ..*/.static sqli
98ac0 74 65 33 5f 6d 75 74 65 78 20 2a 70 74 68 72 65  te3_mutex *pthre
98ad0 61 64 4d 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74  adMutexAlloc(int
98ae0 20 69 54 79 70 65 29 7b 0a 20 20 73 74 61 74 69   iType){.  stati
98af0 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  c sqlite3_mutex 
98b00 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 5d 20  staticMutexes[] 
98b10 3d 20 7b 0a 20 20 20 20 53 51 4c 49 54 45 33 5f  = {.    SQLITE3_
98b20 4d 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45  MUTEX_INITIALIZE
98b30 52 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d  R,.    SQLITE3_M
98b40 55 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52  UTEX_INITIALIZER
98b50 2c 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55  ,.    SQLITE3_MU
98b60 54 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c  TEX_INITIALIZER,
98b70 0a 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54  .    SQLITE3_MUT
98b80 45 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a  EX_INITIALIZER,.
98b90 20 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45      SQLITE3_MUTE
98ba0 58 5f 49 4e 49 54 49 41 4c 49 5a 45 52 2c 0a 20  X_INITIALIZER,. 
98bb0 20 20 20 53 51 4c 49 54 45 33 5f 4d 55 54 45 58     SQLITE3_MUTEX
98bc0 5f 49 4e 49 54 49 41 4c 49 5a 45 52 0a 20 20 7d  _INITIALIZER.  }
98bd0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
98be0 78 20 2a 70 3b 0a 20 20 73 77 69 74 63 68 28 20  x *p;.  switch( 
98bf0 69 54 79 70 65 20 29 7b 0a 20 20 20 20 63 61 73  iType ){.    cas
98c00 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52  e SQLITE_MUTEX_R
98c10 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20 20  ECURSIVE: {.    
98c20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c    p = sqlite3Mal
98c30 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28  locZero( sizeof(
98c40 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66 28  *p) );.      if(
98c50 20 70 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c   p ){.#ifdef SQL
98c60 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52 45  ITE_HOMEGROWN_RE
98c70 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20 20  CURSIVE_MUTEX.  
98c80 20 20 20 20 20 20 2f 2a 20 49 66 20 72 65 63 75        /* If recu
98c90 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 61 72  rsive mutexes ar
98ca0 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c  e not available,
98cb0 20 77 65 20 77 69 6c 6c 20 68 61 76 65 20 74 6f   we will have to
98cc0 0a 20 20 20 20 20 20 20 20 2a 2a 20 62 75 69 6c  .        ** buil
98cd0 64 20 6f 75 72 20 6f 77 6e 2e 20 20 53 65 65 20  d our own.  See 
98ce0 62 65 6c 6f 77 2e 20 2a 2f 0a 20 20 20 20 20 20  below. */.      
98cf0 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
98d00 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
98d10 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20 20  0);.#else.      
98d20 20 20 2f 2a 20 55 73 65 20 61 20 72 65 63 75 72    /* Use a recur
98d30 73 69 76 65 20 6d 75 74 65 78 20 69 66 20 69 74  sive mutex if it
98d40 20 69 73 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f   is available */
98d50 0a 20 20 20 20 20 20 20 20 70 74 68 72 65 61 64  .        pthread
98d60 5f 6d 75 74 65 78 61 74 74 72 5f 74 20 72 65 63  _mutexattr_t rec
98d70 75 72 73 69 76 65 41 74 74 72 3b 0a 20 20 20 20  ursiveAttr;.    
98d80 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
98d90 78 61 74 74 72 5f 69 6e 69 74 28 26 72 65 63 75  xattr_init(&recu
98da0 72 73 69 76 65 41 74 74 72 29 3b 0a 20 20 20 20  rsiveAttr);.    
98db0 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65      pthread_mute
98dc0 78 61 74 74 72 5f 73 65 74 74 79 70 65 28 26 72  xattr_settype(&r
98dd0 65 63 75 72 73 69 76 65 41 74 74 72 2c 20 50 54  ecursiveAttr, PT
98de0 48 52 45 41 44 5f 4d 55 54 45 58 5f 52 45 43 55  HREAD_MUTEX_RECU
98df0 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 20 20  RSIVE);.        
98e00 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 69 6e  pthread_mutex_in
98e10 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20 26 72  it(&p->mutex, &r
98e20 65 63 75 72 73 69 76 65 41 74 74 72 29 3b 0a 20  ecursiveAttr);. 
98e30 20 20 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d         pthread_m
98e40 75 74 65 78 61 74 74 72 5f 64 65 73 74 72 6f 79  utexattr_destroy
98e50 28 26 72 65 63 75 72 73 69 76 65 41 74 74 72 29  (&recursiveAttr)
98e60 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20  ;.#endif.       
98e70 20 70 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a   p->id = iType;.
98e80 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
98e90 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
98ea0 61 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ase SQLITE_MUTEX
98eb0 5f 46 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 70  _FAST: {.      p
98ec0 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
98ed0 5a 65 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29  Zero( sizeof(*p)
98ee0 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 20   );.      if( p 
98ef0 29 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 69 64  ){.        p->id
98f00 20 3d 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20   = iType;.      
98f10 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f    pthread_mutex_
98f20 69 6e 69 74 28 26 70 2d 3e 6d 75 74 65 78 2c 20  init(&p->mutex, 
98f30 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
98f40 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
98f50 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
98f60 20 20 20 20 61 73 73 65 72 74 28 20 69 54 79 70      assert( iTyp
98f70 65 2d 32 20 3e 3d 20 30 20 29 3b 0a 20 20 20 20  e-2 >= 0 );.    
98f80 20 20 61 73 73 65 72 74 28 20 69 54 79 70 65 2d    assert( iType-
98f90 32 20 3c 20 41 72 72 61 79 53 69 7a 65 28 73 74  2 < ArraySize(st
98fa0 61 74 69 63 4d 75 74 65 78 65 73 29 20 29 3b 0a  aticMutexes) );.
98fb0 20 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69        p = &stati
98fc0 63 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32  cMutexes[iType-2
98fd0 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d  ];.      p->id =
98fe0 20 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72   iType;.      br
98ff0 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
99000 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f   return p;.}.../
99010 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
99020 65 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20  e deallocates a 
99030 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c  previously.** al
99040 6c 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20  located mutex.  
99050 53 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75  SQLite is carefu
99060 6c 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20  l to deallocate 
99070 65 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74  every.** mutex t
99080 68 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73  hat it allocates
99090 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
990a0 20 70 74 68 72 65 61 64 4d 75 74 65 78 46 72 65   pthreadMutexFre
990b0 65 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  e(sqlite3_mutex 
990c0 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  *p){.  assert( p
990d0 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 20 20 61  ->nRef==0 );.  a
990e0 73 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51  ssert( p->id==SQ
990f0 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20  LITE_MUTEX_FAST 
99100 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45  || p->id==SQLITE
99110 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
99120 20 29 3b 0a 20 20 70 74 68 72 65 61 64 5f 6d 75   );.  pthread_mu
99130 74 65 78 5f 64 65 73 74 72 6f 79 28 26 70 2d 3e  tex_destroy(&p->
99140 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
99150 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a  3_free(p);.}../*
99160 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
99170 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 61 6e  mutex_enter() an
99180 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  d sqlite3_mutex_
99190 74 72 79 28 29 20 72 6f 75 74 69 6e 65 73 20 61  try() routines a
991a0 74 74 65 6d 70 74 0a 2a 2a 20 74 6f 20 65 6e 74  ttempt.** to ent
991b0 65 72 20 61 20 6d 75 74 65 78 2e 20 20 49 66 20  er a mutex.  If 
991c0 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64 20 69  another thread i
991d0 73 20 61 6c 72 65 61 64 79 20 77 69 74 68 69 6e  s already within
991e0 20 74 68 65 20 6d 75 74 65 78 2c 0a 2a 2a 20 73   the mutex,.** s
991f0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
99200 65 72 28 29 20 77 69 6c 6c 20 62 6c 6f 63 6b 20  er() will block 
99210 61 6e 64 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  and sqlite3_mute
99220 78 5f 74 72 79 28 29 20 77 69 6c 6c 20 72 65 74  x_try() will ret
99230 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45 5f 42 55  urn.** SQLITE_BU
99240 53 59 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33  SY.  The sqlite3
99250 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 69 6e 74  _mutex_try() int
99260 65 72 66 61 63 65 20 72 65 74 75 72 6e 73 20 53  erface returns S
99270 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 75 70 6f 6e  QLITE_OK.** upon
99280 20 73 75 63 63 65 73 73 66 75 6c 20 65 6e 74 72   successful entr
99290 79 2e 20 20 4d 75 74 65 78 65 73 20 63 72 65 61  y.  Mutexes crea
992a0 74 65 64 20 75 73 69 6e 67 20 53 51 4c 49 54 45  ted using SQLITE
992b0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
992c0 20 63 61 6e 0a 2a 2a 20 62 65 20 65 6e 74 65 72   can.** be enter
992d0 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d 65  ed multiple time
992e0 73 20 62 79 20 74 68 65 20 73 61 6d 65 20 74 68  s by the same th
992f0 72 65 61 64 2e 20 20 49 6e 20 73 75 63 68 20 63  read.  In such c
99300 61 73 65 73 20 74 68 65 2c 0a 2a 2a 20 6d 75 74  ases the,.** mut
99310 65 78 20 6d 75 73 74 20 62 65 20 65 78 69 74 65  ex must be exite
99320 64 20 61 6e 20 65 71 75 61 6c 20 6e 75 6d 62 65  d an equal numbe
99330 72 20 6f 66 20 74 69 6d 65 73 20 62 65 66 6f 72  r of times befor
99340 65 20 61 6e 6f 74 68 65 72 20 74 68 72 65 61 64  e another thread
99350 0a 2a 2a 20 63 61 6e 20 65 6e 74 65 72 2e 20 20  .** can enter.  
99360 49 66 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  If the same thre
99370 61 64 20 74 72 69 65 73 20 74 6f 20 65 6e 74 65  ad tries to ente
99380 72 20 61 6e 79 20 6f 74 68 65 72 20 6b 69 6e 64  r any other kind
99390 20 6f 66 20 6d 75 74 65 78 0a 2a 2a 20 6d 6f 72   of mutex.** mor
993a0 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
993b0 20 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64   behavior is und
993c0 65 66 69 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  efined..*/.stati
993d0 63 20 76 6f 69 64 20 70 74 68 72 65 61 64 4d 75  c void pthreadMu
993e0 74 65 78 45 6e 74 65 72 28 73 71 6c 69 74 65 33  texEnter(sqlite3
993f0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 61 73  _mutex *p){.  as
99400 73 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c  sert( p->id==SQL
99410 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53  ITE_MUTEX_RECURS
99420 49 56 45 20 7c 7c 20 70 74 68 72 65 61 64 4d 75  IVE || pthreadMu
99430 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20 29 3b  texNotheld(p) );
99440 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
99450 48 4f 4d 45 47 52 4f 57 4e 5f 52 45 43 55 52 53  HOMEGROWN_RECURS
99460 49 56 45 5f 4d 55 54 45 58 0a 20 20 2f 2a 20 49  IVE_MUTEX.  /* I
99470 66 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  f recursive mute
99480 78 65 73 20 61 72 65 20 6e 6f 74 20 61 76 61 69  xes are not avai
99490 6c 61 62 6c 65 2c 20 74 68 65 6e 20 77 65 20 68  lable, then we h
994a0 61 76 65 20 74 6f 20 67 72 6f 77 0a 20 20 2a 2a  ave to grow.  **
994b0 20 6f 75 72 20 6f 77 6e 2e 20 20 54 68 69 73 20   our own.  This 
994c0 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61  implementation a
994d0 73 73 75 6d 65 73 20 74 68 61 74 20 70 74 68 72  ssumes that pthr
994e0 65 61 64 5f 65 71 75 61 6c 28 29 0a 20 20 2a 2a  ead_equal().  **
994f0 20 69 73 20 61 74 6f 6d 69 63 20 2d 20 74 68 61   is atomic - tha
99500 74 20 69 74 20 63 61 6e 6e 6f 74 20 62 65 20 64  t it cannot be d
99510 65 63 65 69 76 65 64 20 69 6e 74 6f 20 74 68 69  eceived into thi
99520 6e 6b 69 6e 67 20 73 65 6c 66 0a 20 20 2a 2a 20  nking self.  ** 
99530 61 6e 64 20 70 2d 3e 6f 77 6e 65 72 20 61 72 65  and p->owner are
99540 20 65 71 75 61 6c 20 69 66 20 70 2d 3e 6f 77 6e   equal if p->own
99550 65 72 20 63 68 61 6e 67 65 73 20 62 65 74 77 65  er changes betwe
99560 65 6e 20 74 77 6f 20 76 61 6c 75 65 73 0a 20 20  en two values.  
99570 2a 2a 20 74 68 61 74 20 61 72 65 20 6e 6f 74 20  ** that are not 
99580 65 71 75 61 6c 20 74 6f 20 73 65 6c 66 20 77 68  equal to self wh
99590 69 6c 65 20 74 68 65 20 63 6f 6d 70 61 72 69 73  ile the comparis
995a0 6f 6e 20 69 73 20 74 61 6b 69 6e 67 20 70 6c 61  on is taking pla
995b0 63 65 2e 0a 20 20 2a 2a 20 54 68 69 73 20 69 6d  ce..  ** This im
995c0 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 61 6c 73  plementation als
995d0 6f 20 61 73 73 75 6d 65 73 20 61 20 63 6f 68 65  o assumes a cohe
995e0 72 65 6e 74 20 63 61 63 68 65 20 2d 20 74 68 61  rent cache - tha
995f0 74 20 0a 20 20 2a 2a 20 73 65 70 61 72 61 74 65  t .  ** separate
99600 20 70 72 6f 63 65 73 73 65 73 20 63 61 6e 6e 6f   processes canno
99610 74 20 72 65 61 64 20 64 69 66 66 65 72 65 6e 74  t read different
99620 20 76 61 6c 75 65 73 20 66 72 6f 6d 20 74 68 65   values from the
99630 20 73 61 6d 65 0a 20 20 2a 2a 20 61 64 64 72 65   same.  ** addre
99640 73 73 20 61 74 20 74 68 65 20 73 61 6d 65 20 74  ss at the same t
99650 69 6d 65 2e 20 20 49 66 20 65 69 74 68 65 72 20  ime.  If either 
99660 6f 66 20 74 68 65 73 65 20 74 77 6f 20 63 6f 6e  of these two con
99670 64 69 74 69 6f 6e 73 0a 20 20 2a 2a 20 61 72 65  ditions.  ** are
99680 20 6e 6f 74 20 6d 65 74 2c 20 74 68 65 6e 20 74   not met, then t
99690 68 65 20 6d 75 74 65 78 65 73 20 77 69 6c 6c 20  he mutexes will 
996a0 66 61 69 6c 20 61 6e 64 20 70 72 6f 62 6c 65 6d  fail and problem
996b0 73 20 77 69 6c 6c 20 72 65 73 75 6c 74 2e 0a 20  s will result.. 
996c0 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 70 74 68 72   */.  {.    pthr
996d0 65 61 64 5f 74 20 73 65 6c 66 20 3d 20 70 74 68  ead_t self = pth
996e0 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 20  read_self();.   
996f0 20 69 66 28 20 70 2d 3e 6e 52 65 66 3e 30 20 26   if( p->nRef>0 &
99700 26 20 70 74 68 72 65 61 64 5f 65 71 75 61 6c 28  & pthread_equal(
99710 70 2d 3e 6f 77 6e 65 72 2c 20 73 65 6c 66 29 20  p->owner, self) 
99720 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 52 65 66  ){.      p->nRef
99730 2b 2b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ++;.    }else{. 
99740 20 20 20 20 20 70 74 68 72 65 61 64 5f 6d 75 74       pthread_mut
99750 65 78 5f 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65  ex_lock(&p->mute
99760 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  x);.      assert
99770 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a  ( p->nRef==0 );.
99780 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65 72 20 3d        p->owner =
99790 20 73 65 6c 66 3b 0a 20 20 20 20 20 20 70 2d 3e   self;.      p->
997a0 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  nRef = 1;.    }.
997b0 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 55    }.#else.  /* U
997c0 73 65 20 74 68 65 20 62 75 69 6c 74 2d 69 6e 20  se the built-in 
997d0 72 65 63 75 72 73 69 76 65 20 6d 75 74 65 78 65  recursive mutexe
997e0 73 20 69 66 20 74 68 65 79 20 61 72 65 20 61 76  s if they are av
997f0 61 69 6c 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  ailable..  */.  
99800 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 6c 6f  pthread_mutex_lo
99810 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20  ck(&p->mutex);. 
99820 20 70 2d 3e 6f 77 6e 65 72 20 3d 20 70 74 68 72   p->owner = pthr
99830 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20 70 2d  ead_self();.  p-
99840 3e 6e 52 65 66 2b 2b 3b 0a 23 65 6e 64 69 66 0a  >nRef++;.#endif.
99850 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
99860 45 42 55 47 0a 20 20 69 66 28 20 70 2d 3e 74 72  EBUG.  if( p->tr
99870 61 63 65 20 29 7b 0a 20 20 20 20 70 72 69 6e 74  ace ){.    print
99880 66 28 22 65 6e 74 65 72 20 6d 75 74 65 78 20 25  f("enter mutex %
99890 70 20 28 25 64 29 20 77 69 74 68 20 6e 52 65 66  p (%d) with nRef
998a0 3d 25 64 5c 6e 22 2c 20 70 2c 20 70 2d 3e 74 72  =%d\n", p, p->tr
998b0 61 63 65 2c 20 70 2d 3e 6e 52 65 66 29 3b 0a 20  ace, p->nRef);. 
998c0 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74   }.#endif.}.stat
998d0 69 63 20 69 6e 74 20 70 74 68 72 65 61 64 4d 75  ic int pthreadMu
998e0 74 65 78 54 72 79 28 73 71 6c 69 74 65 33 5f 6d  texTry(sqlite3_m
998f0 75 74 65 78 20 2a 70 29 7b 0a 20 20 69 6e 74 20  utex *p){.  int 
99900 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d  rc;.  assert( p-
99910 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45  >id==SQLITE_MUTE
99920 58 5f 52 45 43 55 52 53 49 56 45 20 7c 7c 20 70  X_RECURSIVE || p
99930 74 68 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65  threadMutexNothe
99940 6c 64 28 70 29 20 29 3b 0a 0a 23 69 66 64 65 66  ld(p) );..#ifdef
99950 20 53 51 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57   SQLITE_HOMEGROW
99960 4e 5f 52 45 43 55 52 53 49 56 45 5f 4d 55 54 45  N_RECURSIVE_MUTE
99970 58 0a 20 20 2f 2a 20 49 66 20 72 65 63 75 72 73  X.  /* If recurs
99980 69 76 65 20 6d 75 74 65 78 65 73 20 61 72 65 20  ive mutexes are 
99990 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 2c 20 74  not available, t
999a0 68 65 6e 20 77 65 20 68 61 76 65 20 74 6f 20 67  hen we have to g
999b0 72 6f 77 0a 20 20 2a 2a 20 6f 75 72 20 6f 77 6e  row.  ** our own
999c0 2e 20 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e  .  This implemen
999d0 74 61 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74  tation assumes t
999e0 68 61 74 20 70 74 68 72 65 61 64 5f 65 71 75 61  hat pthread_equa
999f0 6c 28 29 0a 20 20 2a 2a 20 69 73 20 61 74 6f 6d  l().  ** is atom
99a00 69 63 20 2d 20 74 68 61 74 20 69 74 20 63 61 6e  ic - that it can
99a10 6e 6f 74 20 62 65 20 64 65 63 65 69 76 65 64 20  not be deceived 
99a20 69 6e 74 6f 20 74 68 69 6e 6b 69 6e 67 20 73 65  into thinking se
99a30 6c 66 0a 20 20 2a 2a 20 61 6e 64 20 70 2d 3e 6f  lf.  ** and p->o
99a40 77 6e 65 72 20 61 72 65 20 65 71 75 61 6c 20 69  wner are equal i
99a50 66 20 70 2d 3e 6f 77 6e 65 72 20 63 68 61 6e 67  f p->owner chang
99a60 65 73 20 62 65 74 77 65 65 6e 20 74 77 6f 20 76  es between two v
99a70 61 6c 75 65 73 0a 20 20 2a 2a 20 74 68 61 74 20  alues.  ** that 
99a80 61 72 65 20 6e 6f 74 20 65 71 75 61 6c 20 74 6f  are not equal to
99a90 20 73 65 6c 66 20 77 68 69 6c 65 20 74 68 65 20   self while the 
99aa0 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74 61  comparison is ta
99ab0 6b 69 6e 67 20 70 6c 61 63 65 2e 0a 20 20 2a 2a  king place..  **
99ac0 20 54 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61   This implementa
99ad0 74 69 6f 6e 20 61 6c 73 6f 20 61 73 73 75 6d 65  tion also assume
99ae0 73 20 61 20 63 6f 68 65 72 65 6e 74 20 63 61 63  s a coherent cac
99af0 68 65 20 2d 20 74 68 61 74 20 0a 20 20 2a 2a 20  he - that .  ** 
99b00 73 65 70 61 72 61 74 65 20 70 72 6f 63 65 73 73  separate process
99b10 65 73 20 63 61 6e 6e 6f 74 20 72 65 61 64 20 64  es cannot read d
99b20 69 66 66 65 72 65 6e 74 20 76 61 6c 75 65 73 20  ifferent values 
99b30 66 72 6f 6d 20 74 68 65 20 73 61 6d 65 0a 20 20  from the same.  
99b40 2a 2a 20 61 64 64 72 65 73 73 20 61 74 20 74 68  ** address at th
99b50 65 20 73 61 6d 65 20 74 69 6d 65 2e 20 20 49 66  e same time.  If
99b60 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 73 65   either of these
99b70 20 74 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 0a   two conditions.
99b80 20 20 2a 2a 20 61 72 65 20 6e 6f 74 20 6d 65 74    ** are not met
99b90 2c 20 74 68 65 6e 20 74 68 65 20 6d 75 74 65 78  , then the mutex
99ba0 65 73 20 77 69 6c 6c 20 66 61 69 6c 20 61 6e 64  es will fail and
99bb0 20 70 72 6f 62 6c 65 6d 73 20 77 69 6c 6c 20 72   problems will r
99bc0 65 73 75 6c 74 2e 0a 20 20 2a 2f 0a 20 20 7b 0a  esult..  */.  {.
99bd0 20 20 20 20 70 74 68 72 65 61 64 5f 74 20 73 65      pthread_t se
99be0 6c 66 20 3d 20 70 74 68 72 65 61 64 5f 73 65 6c  lf = pthread_sel
99bf0 66 28 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  f();.    if( p->
99c00 6e 52 65 66 3e 30 20 26 26 20 70 74 68 72 65 61  nRef>0 && pthrea
99c10 64 5f 65 71 75 61 6c 28 70 2d 3e 6f 77 6e 65 72  d_equal(p->owner
99c20 2c 20 73 65 6c 66 29 20 29 7b 0a 20 20 20 20 20  , self) ){.     
99c30 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
99c40 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
99c50 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
99c60 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72  pthread_mutex_tr
99c70 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  ylock(&p->mutex)
99c80 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  ==0 ){.      ass
99c90 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  ert( p->nRef==0 
99ca0 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 77 6e 65  );.      p->owne
99cb0 72 20 3d 20 73 65 6c 66 3b 0a 20 20 20 20 20 20  r = self;.      
99cc0 70 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20 20  p->nRef = 1;.   
99cd0 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
99ce0 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  K;.    }else{.  
99cf0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
99d00 42 55 53 59 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  BUSY;.    }.  }.
99d10 23 65 6c 73 65 0a 20 20 2f 2a 20 55 73 65 20 74  #else.  /* Use t
99d20 68 65 20 62 75 69 6c 74 2d 69 6e 20 72 65 63 75  he built-in recu
99d30 72 73 69 76 65 20 6d 75 74 65 78 65 73 20 69 66  rsive mutexes if
99d40 20 74 68 65 79 20 61 72 65 20 61 76 61 69 6c 61   they are availa
99d50 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ble..  */.  if( 
99d60 70 74 68 72 65 61 64 5f 6d 75 74 65 78 5f 74 72  pthread_mutex_tr
99d70 79 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74 65 78 29  ylock(&p->mutex)
99d80 3d 3d 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 77  ==0 ){.    p->ow
99d90 6e 65 72 20 3d 20 70 74 68 72 65 61 64 5f 73 65  ner = pthread_se
99da0 6c 66 28 29 3b 0a 20 20 20 20 70 2d 3e 6e 52 65  lf();.    p->nRe
99db0 66 2b 2b 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  f++;.    rc = SQ
99dc0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65  LITE_OK;.  }else
99dd0 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
99de0 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 23 65 6e 64  E_BUSY;.  }.#end
99df0 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
99e00 45 5f 44 45 42 55 47 0a 20 20 69 66 28 20 72 63  E_DEBUG.  if( rc
99e10 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70  ==SQLITE_OK && p
99e20 2d 3e 74 72 61 63 65 20 29 7b 0a 20 20 20 20 70  ->trace ){.    p
99e30 72 69 6e 74 66 28 22 65 6e 74 65 72 20 6d 75 74  rintf("enter mut
99e40 65 78 20 25 70 20 28 25 64 29 20 77 69 74 68 20  ex %p (%d) with 
99e50 6e 52 65 66 3d 25 64 5c 6e 22 2c 20 70 2c 20 70  nRef=%d\n", p, p
99e60 2d 3e 74 72 61 63 65 2c 20 70 2d 3e 6e 52 65 66  ->trace, p->nRef
99e70 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
99e80 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
99e90 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
99ea0 6d 75 74 65 78 5f 6c 65 61 76 65 28 29 20 72 6f  mutex_leave() ro
99eb0 75 74 69 6e 65 20 65 78 69 74 73 20 61 20 6d 75  utine exits a mu
99ec0 74 65 78 20 74 68 61 74 20 77 61 73 0a 2a 2a 20  tex that was.** 
99ed0 70 72 65 76 69 6f 75 73 6c 79 20 65 6e 74 65 72  previously enter
99ee0 65 64 20 62 79 20 74 68 65 20 73 61 6d 65 20 74  ed by the same t
99ef0 68 72 65 61 64 2e 20 20 54 68 65 20 62 65 68 61  hread.  The beha
99f00 76 69 6f 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66  vior.** is undef
99f10 69 6e 65 64 20 69 66 20 74 68 65 20 6d 75 74 65  ined if the mute
99f20 78 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  x is not current
99f30 6c 79 20 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a  ly entered or.**
99f40 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c   is not currentl
99f50 79 20 61 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51  y allocated.  SQ
99f60 4c 69 74 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  Lite will never 
99f70 64 6f 20 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74  do either..*/.st
99f80 61 74 69 63 20 76 6f 69 64 20 70 74 68 72 65 61  atic void pthrea
99f90 64 4d 75 74 65 78 4c 65 61 76 65 28 73 71 6c 69  dMutexLeave(sqli
99fa0 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20  te3_mutex *p){. 
99fb0 20 61 73 73 65 72 74 28 20 70 74 68 72 65 61 64   assert( pthread
99fc0 4d 75 74 65 78 48 65 6c 64 28 70 29 20 29 3b 0a  MutexHeld(p) );.
99fd0 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 61    p->nRef--;.  a
99fe0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3d 3d  ssert( p->nRef==
99ff0 30 20 7c 7c 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  0 || p->id==SQLI
9a000 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
9a010 56 45 20 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  VE );..#ifdef SQ
9a020 4c 49 54 45 5f 48 4f 4d 45 47 52 4f 57 4e 5f 52  LITE_HOMEGROWN_R
9a030 45 43 55 52 53 49 56 45 5f 4d 55 54 45 58 0a 20  ECURSIVE_MUTEX. 
9a040 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
9a050 29 7b 0a 20 20 20 20 70 74 68 72 65 61 64 5f 6d  ){.    pthread_m
9a060 75 74 65 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e  utex_unlock(&p->
9a070 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 23 65 6c 73  mutex);.  }.#els
9a080 65 0a 20 20 70 74 68 72 65 61 64 5f 6d 75 74 65  e.  pthread_mute
9a090 78 5f 75 6e 6c 6f 63 6b 28 26 70 2d 3e 6d 75 74  x_unlock(&p->mut
9a0a0 65 78 29 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ex);.#endif..#if
9a0b0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
9a0c0 0a 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 20  .  if( p->trace 
9a0d0 29 7b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 6c  ){.    printf("l
9a0e0 65 61 76 65 20 6d 75 74 65 78 20 25 70 20 28 25  eave mutex %p (%
9a0f0 64 29 20 77 69 74 68 20 6e 52 65 66 3d 25 64 5c  d) with nRef=%d\
9a100 6e 22 2c 20 70 2c 20 70 2d 3e 74 72 61 63 65 2c  n", p, p->trace,
9a110 20 70 2d 3e 6e 52 65 66 29 3b 0a 20 20 7d 0a 23   p->nRef);.  }.#
9a120 65 6e 64 69 66 0a 7d 0a 0a 53 51 4c 49 54 45 5f  endif.}..SQLITE_
9a130 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
9a140 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73  mutex_methods *s
9a150 71 6c 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74  qlite3DefaultMut
9a160 65 78 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74  ex(void){.  stat
9a170 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
9a180 5f 6d 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20  _methods sMutex 
9a190 3d 20 7b 0a 20 20 20 20 70 74 68 72 65 61 64 4d  = {.    pthreadM
9a1a0 75 74 65 78 49 6e 69 74 2c 0a 20 20 20 20 70 74  utexInit,.    pt
9a1b0 68 72 65 61 64 4d 75 74 65 78 45 6e 64 2c 0a 20  hreadMutexEnd,. 
9a1c0 20 20 20 70 74 68 72 65 61 64 4d 75 74 65 78 41     pthreadMutexA
9a1d0 6c 6c 6f 63 2c 0a 20 20 20 20 70 74 68 72 65 61  lloc,.    pthrea
9a1e0 64 4d 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20  dMutexFree,.    
9a1f0 70 74 68 72 65 61 64 4d 75 74 65 78 45 6e 74 65  pthreadMutexEnte
9a200 72 2c 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  r,.    pthreadMu
9a210 74 65 78 54 72 79 2c 0a 20 20 20 20 70 74 68 72  texTry,.    pthr
9a220 65 61 64 4d 75 74 65 78 4c 65 61 76 65 2c 0a 23  eadMutexLeave,.#
9a230 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
9a240 55 47 0a 20 20 20 20 70 74 68 72 65 61 64 4d 75  UG.    pthreadMu
9a250 74 65 78 48 65 6c 64 2c 0a 20 20 20 20 70 74 68  texHeld,.    pth
9a260 72 65 61 64 4d 75 74 65 78 4e 6f 74 68 65 6c 64  readMutexNotheld
9a270 0a 23 65 6e 64 69 66 0a 20 20 7d 3b 0a 0a 20 20  .#endif.  };..  
9a280 72 65 74 75 72 6e 20 26 73 4d 75 74 65 78 3b 0a  return &sMutex;.
9a290 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  }..#endif /* SQL
9a2a0 49 54 45 5f 4d 55 54 45 58 5f 50 54 48 52 45 41  ITE_MUTEX_PTHREA
9a2b0 44 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  D */../*********
9a2c0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 75 74  ***** End of mut
9a2d0 65 78 5f 75 6e 69 78 2e 63 20 2a 2a 2a 2a 2a 2a  ex_unix.c ******
9a2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a2f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a300 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
9a310 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
9a320 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a 2a   mutex_w32.c ***
9a330 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a350 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37  ****/./*.** 2007
9a360 20 41 75 67 75 73 74 20 31 34 0a 2a 2a 0a 2a 2a   August 14.**.**
9a370 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
9a380 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
9a390 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
9a3a0 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
9a3b0 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
9a3c0 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
9a3d0 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
9a3e0 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
9a3f0 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
9a400 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
9a410 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
9a420 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
9a430 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
9a440 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
9a450 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
9a460 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
9a470 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
9a480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a4a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a4b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9a4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
9a4d0 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
9a4e0 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
9a4f0 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 6d  that implement m
9a500 75 74 65 78 65 73 20 66 6f 72 20 77 69 6e 33 32  utexes for win32
9a510 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6d 75 74 65  .**.** $Id: mute
9a520 78 5f 77 33 32 2e 63 2c 76 20 31 2e 31 32 20 32  x_w32.c,v 1.12 2
9a530 30 30 38 2f 31 31 2f 31 30 20 32 30 3a 30 31 3a  008/11/10 20:01:
9a540 34 31 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a  41 shane Exp $.*
9a550 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64  /../*.** The cod
9a560 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69  e in this file i
9a570 73 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 77  s only used if w
9a580 65 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 20  e are compiling 
9a590 6d 75 6c 74 69 74 68 72 65 61 64 65 64 0a 2a 2a  multithreaded.**
9a5a0 20 6f 6e 20 61 20 77 69 6e 33 32 20 73 79 73 74   on a win32 syst
9a5b0 65 6d 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  em..*/.#ifdef SQ
9a5c0 4c 49 54 45 5f 4d 55 54 45 58 5f 57 33 32 0a 0a  LITE_MUTEX_W32..
9a5d0 2f 2a 0a 2a 2a 20 45 61 63 68 20 72 65 63 75 72  /*.** Each recur
9a5e0 73 69 76 65 20 6d 75 74 65 78 20 69 73 20 61 6e  sive mutex is an
9a5f0 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
9a600 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63   following struc
9a610 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  ture..*/.struct 
9a620 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 7b 0a  sqlite3_mutex {.
9a630 20 20 43 52 49 54 49 43 41 4c 5f 53 45 43 54 49    CRITICAL_SECTI
9a640 4f 4e 20 6d 75 74 65 78 3b 20 20 20 20 2f 2a 20  ON mutex;    /* 
9a650 4d 75 74 65 78 20 63 6f 6e 74 72 6f 6c 6c 69 6e  Mutex controllin
9a660 67 20 74 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  g the lock */.  
9a670 69 6e 74 20 69 64 3b 20 20 20 20 20 20 20 20 20  int id;         
9a680 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 75             /* Mu
9a690 74 65 78 20 74 79 70 65 20 2a 2f 0a 20 20 69 6e  tex type */.  in
9a6a0 74 20 6e 52 65 66 3b 20 20 20 20 20 20 20 20 20  t nRef;         
9a6b0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
9a6c0 65 72 20 6f 66 20 65 6e 74 65 72 61 6e 63 65 73  er of enterances
9a6d0 20 2a 2f 0a 20 20 44 57 4f 52 44 20 6f 77 6e 65   */.  DWORD owne
9a6e0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
9a6f0 20 2f 2a 20 54 68 72 65 61 64 20 68 6f 6c 64 69   /* Thread holdi
9a700 6e 67 20 74 68 69 73 20 6d 75 74 65 78 20 2a 2f  ng this mutex */
9a710 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  .};../*.** Retur
9a720 6e 20 74 72 75 65 20 28 6e 6f 6e 2d 7a 65 72 6f  n true (non-zero
9a730 29 20 69 66 20 77 65 20 61 72 65 20 72 75 6e 6e  ) if we are runn
9a740 69 6e 67 20 75 6e 64 65 72 20 57 69 6e 4e 54 2c  ing under WinNT,
9a750 20 57 69 6e 32 4b 2c 20 57 69 6e 58 50 2c 0a 2a   Win2K, WinXP,.*
9a760 2a 20 6f 72 20 57 69 6e 43 45 2e 20 20 52 65 74  * or WinCE.  Ret
9a770 75 72 6e 20 66 61 6c 73 65 20 28 7a 65 72 6f 29  urn false (zero)
9a780 20 66 6f 72 20 57 69 6e 39 35 2c 20 57 69 6e 39   for Win95, Win9
9a790 38 2c 20 6f 72 20 57 69 6e 4d 45 2e 0a 2a 2a 0a  8, or WinME..**.
9a7a0 2a 2a 20 48 65 72 65 20 69 73 20 61 6e 20 69 6e  ** Here is an in
9a7b0 74 65 72 65 73 74 69 6e 67 20 6f 62 73 65 72 76  teresting observ
9a7c0 61 74 69 6f 6e 3a 20 20 57 69 6e 39 35 2c 20 57  ation:  Win95, W
9a7d0 69 6e 39 38 2c 20 61 6e 64 20 57 69 6e 4d 45 20  in98, and WinME 
9a7e0 6c 61 63 6b 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b  lack.** the Lock
9a7f0 46 69 6c 65 45 78 28 29 20 41 50 49 2e 20 20 42  FileEx() API.  B
9a800 75 74 20 77 65 20 63 61 6e 20 73 74 69 6c 6c 20  ut we can still 
9a810 73 74 61 74 69 63 61 6c 6c 79 20 6c 69 6e 6b 20  statically link 
9a820 61 67 61 69 6e 73 74 20 74 68 61 74 0a 2a 2a 20  against that.** 
9a830 41 50 49 20 61 73 20 6c 6f 6e 67 20 61 73 20 77  API as long as w
9a840 65 20 64 6f 6e 27 74 20 63 61 6c 6c 20 69 74 20  e don't call it 
9a850 77 69 6e 20 72 75 6e 6e 69 6e 67 20 57 69 6e 39  win running Win9
9a860 35 2f 39 38 2f 4d 45 2e 20 20 41 20 63 61 6c 6c  5/98/ME.  A call
9a870 20 74 6f 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74   to.** this rout
9a880 69 6e 65 20 69 73 20 75 73 65 64 20 74 6f 20 64  ine is used to d
9a890 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68 65 20  etermine if the 
9a8a0 68 6f 73 74 20 69 73 20 57 69 6e 39 35 2f 39 38  host is Win95/98
9a8b0 2f 4d 45 20 6f 72 0a 2a 2a 20 57 69 6e 4e 54 2f  /ME or.** WinNT/
9a8c0 32 4b 2f 58 50 20 73 6f 20 74 68 61 74 20 77 65  2K/XP so that we
9a8d0 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68 65 74 68   will know wheth
9a8e0 65 72 20 6f 72 20 6e 6f 74 20 77 65 20 63 61 6e  er or not we can
9a8f0 20 73 61 66 65 6c 79 20 63 61 6c 6c 0a 2a 2a 20   safely call.** 
9a900 74 68 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29  the LockFileEx()
9a910 20 41 50 49 2e 0a 2a 2a 0a 2a 2a 20 6d 75 74 65   API..**.** mute
9a920 78 49 73 4e 54 28 29 20 69 73 20 6f 6e 6c 79 20  xIsNT() is only 
9a930 75 73 65 64 20 66 6f 72 20 74 68 65 20 54 72 79  used for the Try
9a940 45 6e 74 65 72 43 72 69 74 69 63 61 6c 53 65 63  EnterCriticalSec
9a950 74 69 6f 6e 28 29 20 41 50 49 20 63 61 6c 6c 2c  tion() API call,
9a960 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 6f 6e 6c  .** which is onl
9a970 79 20 61 76 61 69 6c 61 62 6c 65 20 69 66 20 79  y available if y
9a980 6f 75 72 20 61 70 70 6c 69 63 61 74 69 6f 6e 20  our application 
9a990 77 61 73 20 63 6f 6d 70 69 6c 65 64 20 77 69 74  was compiled wit
9a9a0 68 20 0a 2a 2a 20 5f 57 49 4e 33 32 5f 57 49 4e  h .** _WIN32_WIN
9a9b0 4e 54 20 64 65 66 69 6e 65 64 20 74 6f 20 61 20  NT defined to a 
9a9c0 76 61 6c 75 65 20 3e 3d 20 30 78 30 34 30 30 2e  value >= 0x0400.
9a9d0 20 20 43 75 72 72 65 6e 74 6c 79 2c 20 74 68 65    Currently, the
9a9e0 20 6f 6e 6c 79 0a 2a 2a 20 63 61 6c 6c 20 74 6f   only.** call to
9a9f0 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63 61   TryEnterCritica
9aa00 6c 53 65 63 74 69 6f 6e 28 29 20 69 73 20 23 69  lSection() is #i
9aa10 66 64 65 66 27 65 64 20 6f 75 74 2c 20 73 6f 20  fdef'ed out, so 
9aa20 23 69 66 64 65 66 20 0a 2a 2a 20 74 68 69 73 20  #ifdef .** this 
9aa30 6f 75 74 20 61 73 20 77 65 6c 6c 2e 0a 2a 2f 0a  out as well..*/.
9aa40 23 69 66 20 30 0a 23 69 66 20 53 51 4c 49 54 45  #if 0.#if SQLITE
9aa50 5f 4f 53 5f 57 49 4e 43 45 0a 23 20 64 65 66 69  _OS_WINCE.# defi
9aa60 6e 65 20 6d 75 74 65 78 49 73 4e 54 28 29 20 20  ne mutexIsNT()  
9aa70 28 31 29 0a 23 65 6c 73 65 0a 20 20 73 74 61 74  (1).#else.  stat
9aa80 69 63 20 69 6e 74 20 6d 75 74 65 78 49 73 4e 54  ic int mutexIsNT
9aa90 28 76 6f 69 64 29 7b 0a 20 20 20 20 73 74 61 74  (void){.    stat
9aaa0 69 63 20 69 6e 74 20 6f 73 54 79 70 65 20 3d 20  ic int osType = 
9aab0 30 3b 0a 20 20 20 20 69 66 28 20 6f 73 54 79 70  0;.    if( osTyp
9aac0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 4f 53  e==0 ){.      OS
9aad0 56 45 52 53 49 4f 4e 49 4e 46 4f 20 73 49 6e 66  VERSIONINFO sInf
9aae0 6f 3b 0a 20 20 20 20 20 20 73 49 6e 66 6f 2e 64  o;.      sInfo.d
9aaf0 77 4f 53 56 65 72 73 69 6f 6e 49 6e 66 6f 53 69  wOSVersionInfoSi
9ab00 7a 65 20 3d 20 73 69 7a 65 6f 66 28 73 49 6e 66  ze = sizeof(sInf
9ab10 6f 29 3b 0a 20 20 20 20 20 20 47 65 74 56 65 72  o);.      GetVer
9ab20 73 69 6f 6e 45 78 28 26 73 49 6e 66 6f 29 3b 0a  sionEx(&sInfo);.
9ab30 20 20 20 20 20 20 6f 73 54 79 70 65 20 3d 20 73        osType = s
9ab40 49 6e 66 6f 2e 64 77 50 6c 61 74 66 6f 72 6d 49  Info.dwPlatformI
9ab50 64 3d 3d 56 45 52 5f 50 4c 41 54 46 4f 52 4d 5f  d==VER_PLATFORM_
9ab60 57 49 4e 33 32 5f 4e 54 20 3f 20 32 20 3a 20 31  WIN32_NT ? 2 : 1
9ab70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75  ;.    }.    retu
9ab80 72 6e 20 6f 73 54 79 70 65 3d 3d 32 3b 0a 20 20  rn osType==2;.  
9ab90 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
9aba0 54 45 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 23  TE_OS_WINCE */.#
9abb0 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
9abc0 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a  LITE_DEBUG./*.**
9abd0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
9abe0 65 78 5f 68 65 6c 64 28 29 20 61 6e 64 20 73 71  ex_held() and sq
9abf0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
9ac00 65 6c 64 28 29 20 72 6f 75 74 69 6e 65 20 61 72  eld() routine ar
9ac10 65 0a 2a 2a 20 69 6e 74 65 6e 64 65 64 20 66 6f  e.** intended fo
9ac20 72 20 75 73 65 20 6f 6e 6c 79 20 69 6e 73 69 64  r use only insid
9ac30 65 20 61 73 73 65 72 74 28 29 20 73 74 61 74 65  e assert() state
9ac40 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  ments..*/.static
9ac50 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 48 65 6c   int winMutexHel
9ac60 64 28 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20  d(sqlite3_mutex 
9ac70 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 2d  *p){.  return p-
9ac80 3e 6e 52 65 66 21 3d 30 20 26 26 20 70 2d 3e 6f  >nRef!=0 && p->o
9ac90 77 6e 65 72 3d 3d 47 65 74 43 75 72 72 65 6e 74  wner==GetCurrent
9aca0 54 68 72 65 61 64 49 64 28 29 3b 0a 7d 0a 73 74  ThreadId();.}.st
9acb0 61 74 69 63 20 69 6e 74 20 77 69 6e 4d 75 74 65  atic int winMute
9acc0 78 4e 6f 74 68 65 6c 64 28 73 71 6c 69 74 65 33  xNotheld(sqlite3
9acd0 5f 6d 75 74 65 78 20 2a 70 29 7b 0a 20 20 72 65  _mutex *p){.  re
9ace0 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3d 3d 30 20  turn p->nRef==0 
9acf0 7c 7c 20 70 2d 3e 6f 77 6e 65 72 21 3d 47 65 74  || p->owner!=Get
9ad00 43 75 72 72 65 6e 74 54 68 72 65 61 64 49 64 28  CurrentThreadId(
9ad10 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  );.}.#endif.../*
9ad20 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
9ad30 6e 64 20 64 65 69 6e 69 74 69 61 6c 69 7a 65 20  nd deinitialize 
9ad40 74 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73  the mutex subsys
9ad50 74 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  tem..*/.static i
9ad60 6e 74 20 77 69 6e 4d 75 74 65 78 49 6e 69 74 28  nt winMutexInit(
9ad70 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20 53 51  void){ return SQ
9ad80 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 73 74 61 74 69  LITE_OK; }.stati
9ad90 63 20 69 6e 74 20 77 69 6e 4d 75 74 65 78 45 6e  c int winMutexEn
9ada0 64 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  d(void){ return 
9adb0 53 51 4c 49 54 45 5f 4f 4b 3b 20 7d 0a 0a 2f 2a  SQLITE_OK; }../*
9adc0 0a 2a 2a 20 54 68 65 20 73 71 6c 69 74 65 33 5f  .** The sqlite3_
9add0 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 72 6f  mutex_alloc() ro
9ade0 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65 73 20  utine allocates 
9adf0 61 20 6e 65 77 0a 2a 2a 20 6d 75 74 65 78 20 61  a new.** mutex a
9ae00 6e 64 20 72 65 74 75 72 6e 73 20 61 20 70 6f 69  nd returns a poi
9ae10 6e 74 65 72 20 74 6f 20 69 74 2e 20 20 49 66 20  nter to it.  If 
9ae20 69 74 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 0a  it returns NULL.
9ae30 2a 2a 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68  ** that means th
9ae40 61 74 20 61 20 6d 75 74 65 78 20 63 6f 75 6c 64  at a mutex could
9ae50 20 6e 6f 74 20 62 65 20 61 6c 6c 6f 63 61 74 65   not be allocate
9ae60 64 2e 20 20 53 51 4c 69 74 65 0a 2a 2a 20 77 69  d.  SQLite.** wi
9ae70 6c 6c 20 75 6e 77 69 6e 64 20 69 74 73 20 73 74  ll unwind its st
9ae80 61 63 6b 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ack and return a
9ae90 6e 20 65 72 72 6f 72 2e 20 20 54 68 65 20 61 72  n error.  The ar
9aea0 67 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 73 71 6c  gument.** to sql
9aeb0 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63  ite3_mutex_alloc
9aec0 28 29 20 69 73 20 6f 6e 65 20 6f 66 20 74 68 65  () is one of the
9aed0 73 65 20 69 6e 74 65 67 65 72 20 63 6f 6e 73 74  se integer const
9aee0 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 3c 75 6c 3e  ants:.**.** <ul>
9aef0 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54 45  .** <li>  SQLITE
9af00 5f 4d 55 54 45 58 5f 46 41 53 54 20 20 20 20 20  _MUTEX_FAST     
9af10 20 20 20 20 20 20 20 20 20 20 30 0a 2a 2a 20 3c            0.** <
9af20 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54 45  li>  SQLITE_MUTE
9af30 58 5f 52 45 43 55 52 53 49 56 45 20 20 20 20 20  X_RECURSIVE     
9af40 20 20 20 20 20 31 0a 2a 2a 20 3c 6c 69 3e 20 20       1.** <li>  
9af50 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
9af60 54 49 43 5f 4d 41 53 54 45 52 20 20 20 20 20 20  TIC_MASTER      
9af70 32 0a 2a 2a 20 3c 6c 69 3e 20 20 53 51 4c 49 54  2.** <li>  SQLIT
9af80 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
9af90 45 4d 20 20 20 20 20 20 20 20 20 33 0a 2a 2a 20  EM         3.** 
9afa0 3c 6c 69 3e 20 20 53 51 4c 49 54 45 5f 4d 55 54  <li>  SQLITE_MUT
9afb0 45 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 20 20  EX_STATIC_PRNG  
9afc0 20 20 20 20 20 20 34 0a 2a 2a 20 3c 2f 75 6c 3e        4.** </ul>
9afd0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74  .**.** The first
9afe0 20 74 77 6f 20 63 6f 6e 73 74 61 6e 74 73 20 63   two constants c
9aff0 61 75 73 65 20 73 71 6c 69 74 65 33 5f 6d 75 74  ause sqlite3_mut
9b000 65 78 5f 61 6c 6c 6f 63 28 29 20 74 6f 20 63 72  ex_alloc() to cr
9b010 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6d 75  eate.** a new mu
9b020 74 65 78 2e 20 20 54 68 65 20 6e 65 77 20 6d 75  tex.  The new mu
9b030 74 65 78 20 69 73 20 72 65 63 75 72 73 69 76 65  tex is recursive
9b040 20 77 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54   when SQLITE_MUT
9b050 45 58 5f 52 45 43 55 52 53 49 56 45 0a 2a 2a 20  EX_RECURSIVE.** 
9b060 69 73 20 75 73 65 64 20 62 75 74 20 6e 6f 74 20  is used but not 
9b070 6e 65 63 65 73 73 61 72 69 6c 79 20 73 6f 20 77  necessarily so w
9b080 68 65 6e 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  hen SQLITE_MUTEX
9b090 5f 46 41 53 54 20 69 73 20 75 73 65 64 2e 0a 2a  _FAST is used..*
9b0a0 2a 20 54 68 65 20 6d 75 74 65 78 20 69 6d 70 6c  * The mutex impl
9b0b0 65 6d 65 6e 74 61 74 69 6f 6e 20 64 6f 65 73 20  ementation does 
9b0c0 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65  not need to make
9b0d0 20 61 20 64 69 73 74 69 6e 63 74 69 6f 6e 0a 2a   a distinction.*
9b0e0 2a 20 62 65 74 77 65 65 6e 20 53 51 4c 49 54 45  * between SQLITE
9b0f0 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45  _MUTEX_RECURSIVE
9b100 20 61 6e 64 20 53 51 4c 49 54 45 5f 4d 55 54 45   and SQLITE_MUTE
9b110 58 5f 46 41 53 54 20 69 66 20 69 74 20 64 6f 65  X_FAST if it doe
9b120 73 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74 20 74 6f  s.** not want to
9b130 2e 20 20 42 75 74 20 53 51 4c 69 74 65 20 77 69  .  But SQLite wi
9b140 6c 6c 20 6f 6e 6c 79 20 72 65 71 75 65 73 74 20  ll only request 
9b150 61 20 72 65 63 75 72 73 69 76 65 20 6d 75 74 65  a recursive mute
9b160 78 20 69 6e 0a 2a 2a 20 63 61 73 65 73 20 77 68  x in.** cases wh
9b170 65 72 65 20 69 74 20 72 65 61 6c 6c 79 20 6e 65  ere it really ne
9b180 65 64 73 20 6f 6e 65 2e 20 20 49 66 20 61 20 66  eds one.  If a f
9b190 61 73 74 65 72 20 6e 6f 6e 2d 72 65 63 75 72 73  aster non-recurs
9b1a0 69 76 65 20 6d 75 74 65 78 0a 2a 2a 20 69 6d 70  ive mutex.** imp
9b1b0 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
9b1c0 76 61 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20  vailable on the 
9b1d0 68 6f 73 74 20 70 6c 61 74 66 6f 72 6d 2c 20 74  host platform, t
9b1e0 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
9b1f0 65 6d 0a 2a 2a 20 6d 69 67 68 74 20 72 65 74 75  em.** might retu
9b200 72 6e 20 73 75 63 68 20 61 20 6d 75 74 65 78 20  rn such a mutex 
9b210 69 6e 20 72 65 73 70 6f 6e 73 65 20 74 6f 20 53  in response to S
9b220 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46 41 53 54  QLITE_MUTEX_FAST
9b230 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 74 68 65  ..**.** The othe
9b240 72 20 61 6c 6c 6f 77 65 64 20 70 61 72 61 6d 65  r allowed parame
9b250 74 65 72 73 20 74 6f 20 73 71 6c 69 74 65 33 5f  ters to sqlite3_
9b260 6d 75 74 65 78 5f 61 6c 6c 6f 63 28 29 20 65 61  mutex_alloc() ea
9b270 63 68 20 72 65 74 75 72 6e 0a 2a 2a 20 61 20 70  ch return.** a p
9b280 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 61 74  ointer to a stat
9b290 69 63 20 70 72 65 65 78 69 73 74 69 6e 67 20 6d  ic preexisting m
9b2a0 75 74 65 78 2e 20 20 54 68 72 65 65 20 73 74 61  utex.  Three sta
9b2b0 74 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 0a  tic mutexes are.
9b2c0 2a 2a 20 75 73 65 64 20 62 79 20 74 68 65 20 63  ** used by the c
9b2d0 75 72 72 65 6e 74 20 76 65 72 73 69 6f 6e 20 6f  urrent version o
9b2e0 66 20 53 51 4c 69 74 65 2e 20 20 46 75 74 75 72  f SQLite.  Futur
9b2f0 65 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51  e versions of SQ
9b300 4c 69 74 65 0a 2a 2a 20 6d 61 79 20 61 64 64 20  Lite.** may add 
9b310 61 64 64 69 74 69 6f 6e 61 6c 20 73 74 61 74 69  additional stati
9b320 63 20 6d 75 74 65 78 65 73 2e 20 20 53 74 61 74  c mutexes.  Stat
9b330 69 63 20 6d 75 74 65 78 65 73 20 61 72 65 20 66  ic mutexes are f
9b340 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 75  or internal.** u
9b350 73 65 20 62 79 20 53 51 4c 69 74 65 20 6f 6e 6c  se by SQLite onl
9b360 79 2e 20 20 41 70 70 6c 69 63 61 74 69 6f 6e 73  y.  Applications
9b370 20 74 68 61 74 20 75 73 65 20 53 51 4c 69 74 65   that use SQLite
9b380 20 6d 75 74 65 78 65 73 20 73 68 6f 75 6c 64 0a   mutexes should.
9b390 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68 65 20  ** use only the 
9b3a0 64 79 6e 61 6d 69 63 20 6d 75 74 65 78 65 73 20  dynamic mutexes 
9b3b0 72 65 74 75 72 6e 65 64 20 62 79 20 53 51 4c 49  returned by SQLI
9b3c0 54 45 5f 4d 55 54 45 58 5f 46 41 53 54 20 6f 72  TE_MUTEX_FAST or
9b3d0 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  .** SQLITE_MUTEX
9b3e0 5f 52 45 43 55 52 53 49 56 45 2e 0a 2a 2a 0a 2a  _RECURSIVE..**.*
9b3f0 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 66 20 6f  * Note that if o
9b400 6e 65 20 6f 66 20 74 68 65 20 64 79 6e 61 6d 69  ne of the dynami
9b410 63 20 6d 75 74 65 78 20 70 61 72 61 6d 65 74 65  c mutex paramete
9b420 72 73 20 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  rs (SQLITE_MUTEX
9b430 5f 46 41 53 54 0a 2a 2a 20 6f 72 20 53 51 4c 49  _FAST.** or SQLI
9b440 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
9b450 56 45 29 20 69 73 20 75 73 65 64 20 74 68 65 6e  VE) is used then
9b460 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61   sqlite3_mutex_a
9b470 6c 6c 6f 63 28 29 0a 2a 2a 20 72 65 74 75 72 6e  lloc().** return
9b480 73 20 61 20 64 69 66 66 65 72 65 6e 74 20 6d 75  s a different mu
9b490 74 65 78 20 6f 6e 20 65 76 65 72 79 20 63 61 6c  tex on every cal
9b4a0 6c 2e 20 20 42 75 74 20 66 6f 72 20 74 68 65 20  l.  But for the 
9b4b0 73 74 61 74 69 63 20 0a 2a 2a 20 6d 75 74 65 78  static .** mutex
9b4c0 20 74 79 70 65 73 2c 20 74 68 65 20 73 61 6d 65   types, the same
9b4d0 20 6d 75 74 65 78 20 69 73 20 72 65 74 75 72 6e   mutex is return
9b4e0 65 64 20 6f 6e 20 65 76 65 72 79 20 63 61 6c 6c  ed on every call
9b4f0 20 74 68 61 74 20 68 61 73 0a 2a 2a 20 74 68 65   that has.** the
9b500 20 73 61 6d 65 20 74 79 70 65 20 6e 75 6d 62 65   same type numbe
9b510 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73 71 6c  r..*/.static sql
9b520 69 74 65 33 5f 6d 75 74 65 78 20 2a 77 69 6e 4d  ite3_mutex *winM
9b530 75 74 65 78 41 6c 6c 6f 63 28 69 6e 74 20 69 54  utexAlloc(int iT
9b540 79 70 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ype){.  sqlite3_
9b550 6d 75 74 65 78 20 2a 70 3b 0a 0a 20 20 73 77 69  mutex *p;..  swi
9b560 74 63 68 28 20 69 54 79 70 65 20 29 7b 0a 20 20  tch( iType ){.  
9b570 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4d 55    case SQLITE_MU
9b580 54 45 58 5f 46 41 53 54 3a 0a 20 20 20 20 63 61  TEX_FAST:.    ca
9b590 73 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  se SQLITE_MUTEX_
9b5a0 52 45 43 55 52 53 49 56 45 3a 20 7b 0a 20 20 20  RECURSIVE: {.   
9b5b0 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61     p = sqlite3Ma
9b5c0 6c 6c 6f 63 5a 65 72 6f 28 20 73 69 7a 65 6f 66  llocZero( sizeof
9b5d0 28 2a 70 29 20 29 3b 0a 20 20 20 20 20 20 69 66  (*p) );.      if
9b5e0 28 20 70 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ( p ){.        p
9b5f0 2d 3e 69 64 20 3d 20 69 54 79 70 65 3b 0a 20 20  ->id = iType;.  
9b600 20 20 20 20 20 20 49 6e 69 74 69 61 6c 69 7a 65        Initialize
9b610 43 72 69 74 69 63 61 6c 53 65 63 74 69 6f 6e 28  CriticalSection(
9b620 26 70 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20  &p->mutex);.    
9b630 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
9b640 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
9b650 6c 74 3a 20 7b 0a 20 20 20 20 20 20 73 74 61 74  lt: {.      stat
9b660 69 63 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  ic sqlite3_mutex
9b670 20 73 74 61 74 69 63 4d 75 74 65 78 65 73 5b 36   staticMutexes[6
9b680 5d 3b 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  ];.      static 
9b690 69 6e 74 20 69 73 49 6e 69 74 20 3d 20 30 3b 0a  int isInit = 0;.
9b6a0 20 20 20 20 20 20 77 68 69 6c 65 28 20 21 69 73        while( !is
9b6b0 49 6e 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Init ){.        
9b6c0 73 74 61 74 69 63 20 6c 6f 6e 67 20 6c 6f 63 6b  static long lock
9b6d0 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
9b6e0 28 20 49 6e 74 65 72 6c 6f 63 6b 65 64 49 6e 63  ( InterlockedInc
9b6f0 72 65 6d 65 6e 74 28 26 6c 6f 63 6b 29 3d 3d 31  rement(&lock)==1
9b700 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e   ){.          in
9b710 74 20 69 3b 0a 20 20 20 20 20 20 20 20 20 20 66  t i;.          f
9b720 6f 72 28 69 3d 30 3b 20 69 3c 73 69 7a 65 6f 66  or(i=0; i<sizeof
9b730 28 73 74 61 74 69 63 4d 75 74 65 78 65 73 29 2f  (staticMutexes)/
9b740 73 69 7a 65 6f 66 28 73 74 61 74 69 63 4d 75 74  sizeof(staticMut
9b750 65 78 65 73 5b 30 5d 29 3b 20 69 2b 2b 29 7b 0a  exes[0]); i++){.
9b760 20 20 20 20 20 20 20 20 20 20 20 20 49 6e 69 74              Init
9b770 69 61 6c 69 7a 65 43 72 69 74 69 63 61 6c 53 65  ializeCriticalSe
9b780 63 74 69 6f 6e 28 26 73 74 61 74 69 63 4d 75 74  ction(&staticMut
9b790 65 78 65 73 5b 69 5d 2e 6d 75 74 65 78 29 3b 0a  exes[i].mutex);.
9b7a0 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
9b7b0 20 20 20 20 20 20 69 73 49 6e 69 74 20 3d 20 31        isInit = 1
9b7c0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
9b7d0 0a 20 20 20 20 20 20 20 20 20 20 53 6c 65 65 70  .          Sleep
9b7e0 28 31 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  (1);.        }. 
9b7f0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
9b800 65 72 74 28 20 69 54 79 70 65 2d 32 20 3e 3d 20  ert( iType-2 >= 
9b810 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
9b820 74 28 20 69 54 79 70 65 2d 32 20 3c 20 73 69 7a  t( iType-2 < siz
9b830 65 6f 66 28 73 74 61 74 69 63 4d 75 74 65 78 65  eof(staticMutexe
9b840 73 29 2f 73 69 7a 65 6f 66 28 73 74 61 74 69 63  s)/sizeof(static
9b850 4d 75 74 65 78 65 73 5b 30 5d 29 20 29 3b 0a 20  Mutexes[0]) );. 
9b860 20 20 20 20 20 70 20 3d 20 26 73 74 61 74 69 63       p = &static
9b870 4d 75 74 65 78 65 73 5b 69 54 79 70 65 2d 32 5d  Mutexes[iType-2]
9b880 3b 0a 20 20 20 20 20 20 70 2d 3e 69 64 20 3d 20  ;.      p->id = 
9b890 69 54 79 70 65 3b 0a 20 20 20 20 20 20 62 72 65  iType;.      bre
9b8a0 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ak;.    }.  }.  
9b8b0 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a  return p;.}.../*
9b8c0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
9b8d0 20 64 65 61 6c 6c 6f 63 61 74 65 73 20 61 20 70   deallocates a p
9b8e0 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 61 6c 6c  reviously.** all
9b8f0 6f 63 61 74 65 64 20 6d 75 74 65 78 2e 20 20 53  ocated mutex.  S
9b900 51 4c 69 74 65 20 69 73 20 63 61 72 65 66 75 6c  QLite is careful
9b910 20 74 6f 20 64 65 61 6c 6c 6f 63 61 74 65 20 65   to deallocate e
9b920 76 65 72 79 0a 2a 2a 20 6d 75 74 65 78 20 74 68  very.** mutex th
9b930 61 74 20 69 74 20 61 6c 6c 6f 63 61 74 65 73 2e  at it allocates.
9b940 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
9b950 77 69 6e 4d 75 74 65 78 46 72 65 65 28 73 71 6c  winMutexFree(sql
9b960 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29 7b 0a  ite3_mutex *p){.
9b970 20 20 61 73 73 65 72 74 28 20 70 20 29 3b 0a 20    assert( p );. 
9b980 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66   assert( p->nRef
9b990 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ==0 );.  assert(
9b9a0 20 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d   p->id==SQLITE_M
9b9b0 55 54 45 58 5f 46 41 53 54 20 7c 7c 20 70 2d 3e  UTEX_FAST || p->
9b9c0 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58  id==SQLITE_MUTEX
9b9d0 5f 52 45 43 55 52 53 49 56 45 20 29 3b 0a 20 20  _RECURSIVE );.  
9b9e0 44 65 6c 65 74 65 43 72 69 74 69 63 61 6c 53 65  DeleteCriticalSe
9b9f0 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65 78 29  ction(&p->mutex)
9ba00 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
9ba10 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  (p);.}../*.** Th
9ba20 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  e sqlite3_mutex_
9ba30 65 6e 74 65 72 28 29 20 61 6e 64 20 73 71 6c 69  enter() and sqli
9ba40 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20  te3_mutex_try() 
9ba50 72 6f 75 74 69 6e 65 73 20 61 74 74 65 6d 70 74  routines attempt
9ba60 0a 2a 2a 20 74 6f 20 65 6e 74 65 72 20 61 20 6d  .** to enter a m
9ba70 75 74 65 78 2e 20 20 49 66 20 61 6e 6f 74 68 65  utex.  If anothe
9ba80 72 20 74 68 72 65 61 64 20 69 73 20 61 6c 72 65  r thread is alre
9ba90 61 64 79 20 77 69 74 68 69 6e 20 74 68 65 20 6d  ady within the m
9baa0 75 74 65 78 2c 0a 2a 2a 20 73 71 6c 69 74 65 33  utex,.** sqlite3
9bab0 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 29 20 77  _mutex_enter() w
9bac0 69 6c 6c 20 62 6c 6f 63 6b 20 61 6e 64 20 73 71  ill block and sq
9bad0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79 28  lite3_mutex_try(
9bae0 29 20 77 69 6c 6c 20 72 65 74 75 72 6e 0a 2a 2a  ) will return.**
9baf0 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54   SQLITE_BUSY.  T
9bb00 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  he sqlite3_mutex
9bb10 5f 74 72 79 28 29 20 69 6e 74 65 72 66 61 63 65  _try() interface
9bb20 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f   returns SQLITE_
9bb30 4f 4b 0a 2a 2a 20 75 70 6f 6e 20 73 75 63 63 65  OK.** upon succe
9bb40 73 73 66 75 6c 20 65 6e 74 72 79 2e 20 20 4d 75  ssful entry.  Mu
9bb50 74 65 78 65 73 20 63 72 65 61 74 65 64 20 75 73  texes created us
9bb60 69 6e 67 20 53 51 4c 49 54 45 5f 4d 55 54 45 58  ing SQLITE_MUTEX
9bb70 5f 52 45 43 55 52 53 49 56 45 20 63 61 6e 0a 2a  _RECURSIVE can.*
9bb80 2a 20 62 65 20 65 6e 74 65 72 65 64 20 6d 75 6c  * be entered mul
9bb90 74 69 70 6c 65 20 74 69 6d 65 73 20 62 79 20 74  tiple times by t
9bba0 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 2e 20  he same thread. 
9bbb0 20 49 6e 20 73 75 63 68 20 63 61 73 65 73 20 74   In such cases t
9bbc0 68 65 2c 0a 2a 2a 20 6d 75 74 65 78 20 6d 75 73  he,.** mutex mus
9bbd0 74 20 62 65 20 65 78 69 74 65 64 20 61 6e 20 65  t be exited an e
9bbe0 71 75 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 74  qual number of t
9bbf0 69 6d 65 73 20 62 65 66 6f 72 65 20 61 6e 6f 74  imes before anot
9bc00 68 65 72 20 74 68 72 65 61 64 0a 2a 2a 20 63 61  her thread.** ca
9bc10 6e 20 65 6e 74 65 72 2e 20 20 49 66 20 74 68 65  n enter.  If the
9bc20 20 73 61 6d 65 20 74 68 72 65 61 64 20 74 72 69   same thread tri
9bc30 65 73 20 74 6f 20 65 6e 74 65 72 20 61 6e 79 20  es to enter any 
9bc40 6f 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 6d 75  other kind of mu
9bc50 74 65 78 0a 2a 2a 20 6d 6f 72 65 20 74 68 61 6e  tex.** more than
9bc60 20 6f 6e 63 65 2c 20 74 68 65 20 62 65 68 61 76   once, the behav
9bc70 69 6f 72 20 69 73 20 75 6e 64 65 66 69 6e 65 64  ior is undefined
9bc80 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
9bc90 20 77 69 6e 4d 75 74 65 78 45 6e 74 65 72 28 73   winMutexEnter(s
9bca0 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70 29  qlite3_mutex *p)
9bcb0 7b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69  {.  assert( p->i
9bcc0 64 3d 3d 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  d==SQLITE_MUTEX_
9bcd0 52 45 43 55 52 53 49 56 45 20 7c 7c 20 77 69 6e  RECURSIVE || win
9bce0 4d 75 74 65 78 4e 6f 74 68 65 6c 64 28 70 29 20  MutexNotheld(p) 
9bcf0 29 3b 0a 20 20 45 6e 74 65 72 43 72 69 74 69 63  );.  EnterCritic
9bd00 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
9bd10 74 65 78 29 3b 0a 20 20 70 2d 3e 6f 77 6e 65 72  tex);.  p->owner
9bd20 20 3d 20 47 65 74 43 75 72 72 65 6e 74 54 68 72   = GetCurrentThr
9bd30 65 61 64 49 64 28 29 3b 20 0a 20 20 70 2d 3e 6e  eadId(); .  p->n
9bd40 52 65 66 2b 2b 3b 0a 7d 0a 73 74 61 74 69 63 20  Ref++;.}.static 
9bd50 69 6e 74 20 77 69 6e 4d 75 74 65 78 54 72 79 28  int winMutexTry(
9bd60 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 70  sqlite3_mutex *p
9bd70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
9bd80 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 61 73 73  LITE_BUSY;.  ass
9bd90 65 72 74 28 20 70 2d 3e 69 64 3d 3d 53 51 4c 49  ert( p->id==SQLI
9bda0 54 45 5f 4d 55 54 45 58 5f 52 45 43 55 52 53 49  TE_MUTEX_RECURSI
9bdb0 56 45 20 7c 7c 20 77 69 6e 4d 75 74 65 78 4e 6f  VE || winMutexNo
9bdc0 74 68 65 6c 64 28 70 29 20 29 3b 0a 20 20 2f 2a  theld(p) );.  /*
9bdd0 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74 65  .  ** The sqlite
9bde0 33 5f 6d 75 74 65 78 5f 74 72 79 28 29 20 72 6f  3_mutex_try() ro
9bdf0 75 74 69 6e 65 20 69 73 20 76 65 72 79 20 72 61  utine is very ra
9be00 72 65 6c 79 20 75 73 65 64 2c 20 61 6e 64 20 77  rely used, and w
9be10 68 65 6e 20 69 74 0a 20 20 2a 2a 20 69 73 20 75  hen it.  ** is u
9be20 73 65 64 20 69 74 20 69 73 20 6d 65 72 65 6c 79  sed it is merely
9be30 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e   an optimization
9be40 2e 20 20 53 6f 20 69 74 20 69 73 20 4f 4b 20 66  .  So it is OK f
9be50 6f 72 20 69 74 20 74 6f 20 61 6c 77 61 79 73 0a  or it to always.
9be60 20 20 2a 2a 20 66 61 69 6c 2e 20 20 0a 20 20 2a    ** fail.  .  *
9be70 2a 0a 20 20 2a 2a 20 54 68 65 20 54 72 79 45 6e  *.  ** The TryEn
9be80 74 65 72 43 72 69 74 69 63 61 6c 53 65 63 74 69  terCriticalSecti
9be90 6f 6e 28 29 20 69 6e 74 65 72 66 61 63 65 20 69  on() interface i
9bea0 73 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  s only available
9beb0 20 6f 6e 20 57 69 6e 4e 54 2e 0a 20 20 2a 2a 20   on WinNT..  ** 
9bec0 41 6e 64 20 73 6f 6d 65 20 77 69 6e 64 6f 77 73  And some windows
9bed0 20 63 6f 6d 70 69 6c 65 72 73 20 63 6f 6d 70 6c   compilers compl
9bee0 61 69 6e 20 69 66 20 79 6f 75 20 74 72 79 20 74  ain if you try t
9bef0 6f 20 75 73 65 20 69 74 20 77 69 74 68 6f 75 74  o use it without
9bf00 0a 20 20 2a 2a 20 66 69 72 73 74 20 64 6f 69 6e  .  ** first doin
9bf10 67 20 73 6f 6d 65 20 23 64 65 66 69 6e 65 73 20  g some #defines 
9bf20 74 68 61 74 20 70 72 65 76 65 6e 74 20 53 51 4c  that prevent SQL
9bf30 69 74 65 20 66 72 6f 6d 20 62 75 69 6c 64 69 6e  ite from buildin
9bf40 67 20 6f 6e 20 57 69 6e 39 38 2e 0a 20 20 2a 2a  g on Win98..  **
9bf50 20 46 6f 72 20 74 68 61 74 20 72 65 61 73 6f 6e   For that reason
9bf60 2c 20 77 65 20 77 69 6c 6c 20 6f 6d 69 74 20 74  , we will omit t
9bf70 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
9bf80 20 66 6f 72 20 6e 6f 77 2e 20 20 53 65 65 0a 20   for now.  See. 
9bf90 20 2a 2a 20 74 69 63 6b 65 74 20 23 32 36 38 35   ** ticket #2685
9bfa0 2e 0a 20 20 2a 2f 0a 23 69 66 20 30 0a 20 20 69  ..  */.#if 0.  i
9bfb0 66 28 20 6d 75 74 65 78 49 73 4e 54 28 29 20 26  f( mutexIsNT() &
9bfc0 26 20 54 72 79 45 6e 74 65 72 43 72 69 74 69 63  & TryEnterCritic
9bfd0 61 6c 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75  alSection(&p->mu
9bfe0 74 65 78 29 20 29 7b 0a 20 20 20 20 70 2d 3e 6f  tex) ){.    p->o
9bff0 77 6e 65 72 20 3d 20 47 65 74 43 75 72 72 65 6e  wner = GetCurren
9c000 74 54 68 72 65 61 64 49 64 28 29 3b 0a 20 20 20  tThreadId();.   
9c010 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20   p->nRef++;.    
9c020 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
9c030 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74    }.#endif.  ret
9c040 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
9c050 20 54 68 65 20 73 71 6c 69 74 65 33 5f 6d 75 74   The sqlite3_mut
9c060 65 78 5f 6c 65 61 76 65 28 29 20 72 6f 75 74 69  ex_leave() routi
9c070 6e 65 20 65 78 69 74 73 20 61 20 6d 75 74 65 78  ne exits a mutex
9c080 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 70 72 65   that was.** pre
9c090 76 69 6f 75 73 6c 79 20 65 6e 74 65 72 65 64 20  viously entered 
9c0a0 62 79 20 74 68 65 20 73 61 6d 65 20 74 68 72 65  by the same thre
9c0b0 61 64 2e 20 20 54 68 65 20 62 65 68 61 76 69 6f  ad.  The behavio
9c0c0 72 0a 2a 2a 20 69 73 20 75 6e 64 65 66 69 6e 65  r.** is undefine
9c0d0 64 20 69 66 20 74 68 65 20 6d 75 74 65 78 20 69  d if the mutex i
9c0e0 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20  s not currently 
9c0f0 65 6e 74 65 72 65 64 20 6f 72 0a 2a 2a 20 69 73  entered or.** is
9c100 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 61   not currently a
9c110 6c 6c 6f 63 61 74 65 64 2e 20 20 53 51 4c 69 74  llocated.  SQLit
9c120 65 20 77 69 6c 6c 20 6e 65 76 65 72 20 64 6f 20  e will never do 
9c130 65 69 74 68 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  either..*/.stati
9c140 63 20 76 6f 69 64 20 77 69 6e 4d 75 74 65 78 4c  c void winMutexL
9c150 65 61 76 65 28 73 71 6c 69 74 65 33 5f 6d 75 74  eave(sqlite3_mut
9c160 65 78 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ex *p){.  assert
9c170 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ( p->nRef>0 );. 
9c180 20 61 73 73 65 72 74 28 20 70 2d 3e 6f 77 6e 65   assert( p->owne
9c190 72 3d 3d 47 65 74 43 75 72 72 65 6e 74 54 68 72  r==GetCurrentThr
9c1a0 65 61 64 49 64 28 29 20 29 3b 0a 20 20 70 2d 3e  eadId() );.  p->
9c1b0 6e 52 65 66 2d 2d 3b 0a 20 20 61 73 73 65 72 74  nRef--;.  assert
9c1c0 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20 7c 7c 20  ( p->nRef==0 || 
9c1d0 70 2d 3e 69 64 3d 3d 53 51 4c 49 54 45 5f 4d 55  p->id==SQLITE_MU
9c1e0 54 45 58 5f 52 45 43 55 52 53 49 56 45 20 29 3b  TEX_RECURSIVE );
9c1f0 0a 20 20 4c 65 61 76 65 43 72 69 74 69 63 61 6c  .  LeaveCritical
9c200 53 65 63 74 69 6f 6e 28 26 70 2d 3e 6d 75 74 65  Section(&p->mute
9c210 78 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f 50 52  x);.}..SQLITE_PR
9c220 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f 6d 75  IVATE sqlite3_mu
9c230 74 65 78 5f 6d 65 74 68 6f 64 73 20 2a 73 71 6c  tex_methods *sql
9c240 69 74 65 33 44 65 66 61 75 6c 74 4d 75 74 65 78  ite3DefaultMutex
9c250 28 76 6f 69 64 29 7b 0a 20 20 73 74 61 74 69 63  (void){.  static
9c260 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6d   sqlite3_mutex_m
9c270 65 74 68 6f 64 73 20 73 4d 75 74 65 78 20 3d 20  ethods sMutex = 
9c280 7b 0a 20 20 20 20 77 69 6e 4d 75 74 65 78 49 6e  {.    winMutexIn
9c290 69 74 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65 78  it,.    winMutex
9c2a0 45 6e 64 2c 0a 20 20 20 20 77 69 6e 4d 75 74 65  End,.    winMute
9c2b0 78 41 6c 6c 6f 63 2c 0a 20 20 20 20 77 69 6e 4d  xAlloc,.    winM
9c2c0 75 74 65 78 46 72 65 65 2c 0a 20 20 20 20 77 69  utexFree,.    wi
9c2d0 6e 4d 75 74 65 78 45 6e 74 65 72 2c 0a 20 20 20  nMutexEnter,.   
9c2e0 20 77 69 6e 4d 75 74 65 78 54 72 79 2c 0a 20 20   winMutexTry,.  
9c2f0 20 20 77 69 6e 4d 75 74 65 78 4c 65 61 76 65 2c    winMutexLeave,
9c300 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
9c310 45 42 55 47 0a 20 20 20 20 77 69 6e 4d 75 74 65  EBUG.    winMute
9c320 78 48 65 6c 64 2c 0a 20 20 20 20 77 69 6e 4d 75  xHeld,.    winMu
9c330 74 65 78 4e 6f 74 68 65 6c 64 0a 23 65 6e 64 69  texNotheld.#endi
9c340 66 0a 20 20 7d 3b 0a 0a 20 20 72 65 74 75 72 6e  f.  };..  return
9c350 20 26 73 4d 75 74 65 78 3b 0a 7d 0a 23 65 6e 64   &sMutex;.}.#end
9c360 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d 55 54  if /* SQLITE_MUT
9c370 45 58 5f 57 33 32 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  EX_W32 */../****
9c380 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
9c390 66 20 6d 75 74 65 78 5f 77 33 32 2e 63 20 2a 2a  f mutex_w32.c **
9c3a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c3b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c3c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
9c3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
9c3e0 20 66 69 6c 65 20 6d 61 6c 6c 6f 63 2e 63 20 2a   file malloc.c *
9c3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c400 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c410 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
9c420 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20   2001 September 
9c430 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  15.**.** The aut
9c440 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
9c450 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
9c460 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
9c470 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
9c480 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
9c490 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
9c4a0 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
9c4b0 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
9c4c0 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
9c4d0 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
9c4e0 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
9c4f0 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
9c500 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
9c510 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
9c520 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
9c530 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
9c540 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
9c550 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c570 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
9c590 2a 0a 2a 2a 0a 2a 2a 20 4d 65 6d 6f 72 79 20 61  *.**.** Memory a
9c5a0 6c 6c 6f 63 61 74 69 6f 6e 20 66 75 6e 63 74 69  llocation functi
9c5b0 6f 6e 73 20 75 73 65 64 20 74 68 72 6f 75 67 68  ons used through
9c5c0 6f 75 74 20 73 71 6c 69 74 65 2e 0a 2a 2a 0a 2a  out sqlite..**.*
9c5d0 2a 20 24 49 64 3a 20 6d 61 6c 6c 6f 63 2e 63 2c  * $Id: malloc.c,
9c5e0 76 20 31 2e 34 38 20 32 30 30 38 2f 31 31 2f 31  v 1.48 2008/11/1
9c5f0 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65  9 09:05:27 danie
9c600 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
9c610 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
9c620 69 6e 65 20 72 75 6e 73 20 77 68 65 6e 20 74 68  ine runs when th
9c630 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9c640 6f 72 20 73 65 65 73 20 74 68 61 74 20 74 68 65  or sees that the
9c650 0a 2a 2a 20 74 6f 74 61 6c 20 6d 65 6d 6f 72 79  .** total memory
9c660 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 61   allocation is a
9c670 62 6f 75 74 20 74 6f 20 65 78 63 65 65 64 20 74  bout to exceed t
9c680 68 65 20 73 6f 66 74 20 68 65 61 70 0a 2a 2a 20  he soft heap.** 
9c690 6c 69 6d 69 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  limit..*/.static
9c6a0 20 76 6f 69 64 20 73 6f 66 74 48 65 61 70 4c 69   void softHeapLi
9c6b0 6d 69 74 45 6e 66 6f 72 63 65 72 28 0a 20 20 76  mitEnforcer(.  v
9c6c0 6f 69 64 20 2a 4e 6f 74 55 73 65 64 2c 20 0a 20  oid *NotUsed, . 
9c6d0 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 4e   sqlite3_int64 N
9c6e0 6f 74 55 73 65 64 32 2c 0a 20 20 69 6e 74 20 61  otUsed2,.  int a
9c6f0 6c 6c 6f 63 53 69 7a 65 0a 29 7b 0a 20 20 55 4e  llocSize.){.  UN
9c700 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
9c710 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
9c720 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  2);.  sqlite3_re
9c730 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 28 61 6c 6c  lease_memory(all
9c740 6f 63 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  ocSize);.}../*.*
9c750 2a 20 53 65 74 20 74 68 65 20 73 6f 66 74 20 68  * Set the soft h
9c760 65 61 70 2d 73 69 7a 65 20 6c 69 6d 69 74 20 66  eap-size limit f
9c770 6f 72 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  or the library. 
9c780 50 61 73 73 69 6e 67 20 61 20 7a 65 72 6f 20 6f  Passing a zero o
9c790 72 20 0a 2a 2a 20 6e 65 67 61 74 69 76 65 20 76  r .** negative v
9c7a0 61 6c 75 65 20 69 6e 64 69 63 61 74 65 73 20 6e  alue indicates n
9c7b0 6f 20 6c 69 6d 69 74 2e 0a 2a 2f 0a 53 51 4c 49  o limit..*/.SQLI
9c7c0 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
9c7d0 74 65 33 5f 73 6f 66 74 5f 68 65 61 70 5f 6c 69  te3_soft_heap_li
9c7e0 6d 69 74 28 69 6e 74 20 6e 29 7b 0a 20 20 73 71  mit(int n){.  sq
9c7f0 6c 69 74 65 33 5f 75 69 6e 74 36 34 20 69 4c 69  lite3_uint64 iLi
9c800 6d 69 74 3b 0a 20 20 69 6e 74 20 6f 76 65 72 61  mit;.  int overa
9c810 67 65 3b 0a 20 20 69 66 28 20 6e 3c 30 20 29 7b  ge;.  if( n<0 ){
9c820 0a 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 3b  .    iLimit = 0;
9c830 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 4c  .  }else{.    iL
9c840 69 6d 69 74 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20  imit = n;.  }.  
9c850 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69  sqlite3_initiali
9c860 7a 65 28 29 3b 0a 20 20 69 66 28 20 69 4c 69 6d  ze();.  if( iLim
9c870 69 74 3e 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  it>0 ){.    sqli
9c880 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 73  te3MemoryAlarm(s
9c890 6f 66 74 48 65 61 70 4c 69 6d 69 74 45 6e 66 6f  oftHeapLimitEnfo
9c8a0 72 63 65 72 2c 20 30 2c 20 69 4c 69 6d 69 74 29  rcer, 0, iLimit)
9c8b0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
9c8c0 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72  qlite3MemoryAlar
9c8d0 6d 28 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a  m(0, 0, 0);.  }.
9c8e0 20 20 6f 76 65 72 61 67 65 20 3d 20 73 71 6c 69    overage = sqli
9c8f0 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 28  te3_memory_used(
9c900 29 20 2d 20 6e 3b 0a 20 20 69 66 28 20 6f 76 65  ) - n;.  if( ove
9c910 72 61 67 65 3e 30 20 29 7b 0a 20 20 20 20 73 71  rage>0 ){.    sq
9c920 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65  lite3_release_me
9c930 6d 6f 72 79 28 6f 76 65 72 61 67 65 29 3b 0a 20  mory(overage);. 
9c940 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65   }.}../*.** Atte
9c950 6d 70 74 20 74 6f 20 72 65 6c 65 61 73 65 20 75  mpt to release u
9c960 70 20 74 6f 20 6e 20 62 79 74 65 73 20 6f 66 20  p to n bytes of 
9c970 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65  non-essential me
9c980 6d 6f 72 79 20 63 75 72 72 65 6e 74 6c 79 0a 2a  mory currently.*
9c990 2a 20 68 65 6c 64 20 62 79 20 53 51 4c 69 74 65  * held by SQLite
9c9a0 2e 20 41 6e 20 65 78 61 6d 70 6c 65 20 6f 66 20  . An example of 
9c9b0 6e 6f 6e 2d 65 73 73 65 6e 74 69 61 6c 20 6d 65  non-essential me
9c9c0 6d 6f 72 79 20 69 73 20 6d 65 6d 6f 72 79 20 75  mory is memory u
9c9d0 73 65 64 20 74 6f 0a 2a 2a 20 63 61 63 68 65 20  sed to.** cache 
9c9e0 64 61 74 61 62 61 73 65 20 70 61 67 65 73 20 74  database pages t
9c9f0 68 61 74 20 61 72 65 20 6e 6f 74 20 63 75 72 72  hat are not curr
9ca00 65 6e 74 6c 79 20 69 6e 20 75 73 65 2e 0a 2a 2f  ently in use..*/
9ca10 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
9ca20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61 73 65 5f  sqlite3_release_
9ca30 6d 65 6d 6f 72 79 28 69 6e 74 20 6e 29 7b 0a 23  memory(int n){.#
9ca40 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
9ca50 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47  BLE_MEMORY_MANAG
9ca60 45 4d 45 4e 54 0a 20 20 69 6e 74 20 6e 52 65 74  EMENT.  int nRet
9ca70 20 3d 20 30 3b 0a 23 69 66 20 30 0a 20 20 6e 52   = 0;.#if 0.  nR
9ca80 65 74 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62  et += sqlite3Vdb
9ca90 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e  eReleaseMemory(n
9caa0 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 52 65 74  );.#endif.  nRet
9cab0 20 2b 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68   += sqlite3Pcach
9cac0 65 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 6e  eReleaseMemory(n
9cad0 2d 6e 52 65 74 29 3b 0a 20 20 72 65 74 75 72 6e  -nRet);.  return
9cae0 20 6e 52 65 74 3b 0a 23 65 6c 73 65 0a 20 20 55   nRet;.#else.  U
9caf0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
9cb00 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  n);.  return SQL
9cb10 49 54 45 5f 4f 4b 3b 0a 23 65 6e 64 69 66 0a 7d  ITE_OK;.#endif.}
9cb20 0a 0a 2f 2a 0a 2a 2a 20 53 74 61 74 65 20 69 6e  ../*.** State in
9cb30 66 6f 72 6d 61 74 69 6f 6e 20 6c 6f 63 61 6c 20  formation local 
9cb40 74 6f 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  to the memory al
9cb50 6c 6f 63 61 74 69 6f 6e 20 73 75 62 73 79 73 74  location subsyst
9cb60 65 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51  em..*/.static SQ
9cb70 4c 49 54 45 5f 57 53 44 20 73 74 72 75 63 74 20  LITE_WSD struct 
9cb80 4d 65 6d 30 47 6c 6f 62 61 6c 20 7b 0a 20 20 2f  Mem0Global {.  /
9cb90 2a 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65  * Number of free
9cba0 20 70 61 67 65 73 20 66 6f 72 20 73 63 72 61 74   pages for scrat
9cbb0 63 68 20 61 6e 64 20 70 61 67 65 2d 63 61 63 68  ch and page-cach
9cbc0 65 20 6d 65 6d 6f 72 79 20 2a 2f 0a 20 20 75 33  e memory */.  u3
9cbd0 32 20 6e 53 63 72 61 74 63 68 46 72 65 65 3b 0a  2 nScratchFree;.
9cbe0 20 20 75 33 32 20 6e 50 61 67 65 46 72 65 65 3b    u32 nPageFree;
9cbf0 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
9cc00 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20 20 20  x *mutex;       
9cc10 20 20 2f 2a 20 4d 75 74 65 78 20 74 6f 20 73 65    /* Mutex to se
9cc20 72 69 61 6c 69 7a 65 20 61 63 63 65 73 73 20 2a  rialize access *
9cc30 2f 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  /..  /*.  ** The
9cc40 20 61 6c 61 72 6d 20 63 61 6c 6c 62 61 63 6b 20   alarm callback 
9cc50 61 6e 64 20 69 74 73 20 61 72 67 75 6d 65 6e 74  and its argument
9cc60 73 2e 20 20 54 68 65 20 6d 65 6d 30 2e 6d 75 74  s.  The mem0.mut
9cc70 65 78 20 6c 6f 63 6b 20 77 69 6c 6c 0a 20 20 2a  ex lock will.  *
9cc80 2a 20 62 65 20 68 65 6c 64 20 77 68 69 6c 65 20  * be held while 
9cc90 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 69 73 20  the callback is 
9cca0 72 75 6e 6e 69 6e 67 2e 20 20 52 65 63 75 72 73  running.  Recurs
9ccb0 69 76 65 20 63 61 6c 6c 73 20 69 6e 74 6f 0a 20  ive calls into. 
9ccc0 20 2a 2a 20 74 68 65 20 6d 65 6d 6f 72 79 20 73   ** the memory s
9ccd0 75 62 73 79 73 74 65 6d 20 61 72 65 20 61 6c 6c  ubsystem are all
9cce0 6f 77 65 64 2c 20 62 75 74 20 6e 6f 20 6e 65 77  owed, but no new
9ccf0 20 63 61 6c 6c 62 61 63 6b 73 20 77 69 6c 6c 20   callbacks will 
9cd00 62 65 0a 20 20 2a 2a 20 69 73 73 75 65 64 2e 20  be.  ** issued. 
9cd10 20 54 68 65 20 61 6c 61 72 6d 42 75 73 79 20 76   The alarmBusy v
9cd20 61 72 69 61 62 6c 65 20 69 73 20 73 65 74 20 74  ariable is set t
9cd30 6f 20 70 72 65 76 65 6e 74 20 72 65 63 75 72 73  o prevent recurs
9cd40 69 76 65 0a 20 20 2a 2a 20 63 61 6c 6c 62 61 63  ive.  ** callbac
9cd50 6b 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  ks..  */.  sqlit
9cd60 65 33 5f 69 6e 74 36 34 20 61 6c 61 72 6d 54 68  e3_int64 alarmTh
9cd70 72 65 73 68 6f 6c 64 3b 0a 20 20 76 6f 69 64 20  reshold;.  void 
9cd80 28 2a 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 29  (*alarmCallback)
9cd90 28 76 6f 69 64 2a 2c 20 73 71 6c 69 74 65 33 5f  (void*, sqlite3_
9cda0 69 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 76 6f  int64,int);.  vo
9cdb0 69 64 20 2a 61 6c 61 72 6d 41 72 67 3b 0a 20 20  id *alarmArg;.  
9cdc0 69 6e 74 20 61 6c 61 72 6d 42 75 73 79 3b 0a 0a  int alarmBusy;..
9cdd0 20 20 2f 2a 0a 20 20 2a 2a 20 50 6f 69 6e 74 65    /*.  ** Pointe
9cde0 72 73 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  rs to the end of
9cdf0 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9ce00 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 61 6e  nfig.pScratch an
9ce10 64 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 47 6c  d.  ** sqlite3Gl
9ce20 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65  obalConfig.pPage
9ce30 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 6d   to a block of m
9ce40 65 6d 6f 72 79 20 74 68 61 74 20 72 65 63 6f 72  emory that recor
9ce50 64 73 0a 20 20 2a 2a 20 77 68 69 63 68 20 70 61  ds.  ** which pa
9ce60 67 65 73 20 61 72 65 20 61 76 61 69 6c 61 62 6c  ges are availabl
9ce70 65 2e 0a 20 20 2a 2f 0a 20 20 75 33 32 20 2a 61  e..  */.  u32 *a
9ce80 53 63 72 61 74 63 68 46 72 65 65 3b 0a 20 20 75  ScratchFree;.  u
9ce90 33 32 20 2a 61 50 61 67 65 46 72 65 65 3b 0a 7d  32 *aPageFree;.}
9cea0 20 6d 65 6d 30 20 3d 20 7b 20 36 32 35 36 30 39   mem0 = { 625609
9ceb0 35 35 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  55, 0, 0, 0, 0, 
9cec0 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 0a 23  0, 0, 0, 0 };..#
9ced0 64 65 66 69 6e 65 20 6d 65 6d 30 20 47 4c 4f 42  define mem0 GLOB
9cee0 41 4c 28 73 74 72 75 63 74 20 4d 65 6d 30 47 6c  AL(struct Mem0Gl
9cef0 6f 62 61 6c 2c 20 6d 65 6d 30 29 0a 0a 2f 2a 0a  obal, mem0)../*.
9cf00 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74 68  ** Initialize th
9cf10 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74  e memory allocat
9cf20 69 6f 6e 20 73 75 62 73 79 73 74 65 6d 2e 0a 2a  ion subsystem..*
9cf30 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
9cf40 20 69 6e 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c   int sqlite3Mall
9cf50 6f 63 49 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20  ocInit(void){.  
9cf60 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  if( sqlite3Globa
9cf70 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c 6c 6f  lConfig.m.xMallo
9cf80 63 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  c==0 ){.    sqli
9cf90 74 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74  te3MemSetDefault
9cfa0 28 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74  ();.  }.  memset
9cfb0 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a 65 6f  (&mem0, 0, sizeo
9cfc0 66 28 6d 65 6d 30 29 29 3b 0a 20 20 69 66 28 20  f(mem0));.  if( 
9cfd0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9cfe0 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78 20 29  fig.bCoreMutex )
9cff0 7b 0a 20 20 20 20 6d 65 6d 30 2e 6d 75 74 65 78  {.    mem0.mutex
9d000 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
9d010 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
9d020 58 5f 53 54 41 54 49 43 5f 4d 45 4d 29 3b 0a 20  X_STATIC_MEM);. 
9d030 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
9d040 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 53 63  GlobalConfig.pSc
9d050 72 61 74 63 68 20 26 26 20 73 71 6c 69 74 65 33  ratch && sqlite3
9d060 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53  GlobalConfig.szS
9d070 63 72 61 74 63 68 3e 3d 31 30 30 0a 20 20 20 20  cratch>=100.    
9d080 20 20 26 26 20 73 71 6c 69 74 65 33 47 6c 6f 62    && sqlite3Glob
9d090 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61 74 63  alConfig.nScratc
9d0a0 68 3e 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  h>=0 ){.    int 
9d0b0 69 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  i;.    sqlite3Gl
9d0c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9d0d0 61 74 63 68 20 2d 3d 20 34 3b 0a 20 20 20 20 6d  atch -= 4;.    m
9d0e0 65 6d 30 2e 61 53 63 72 61 74 63 68 46 72 65 65  em0.aScratchFree
9d0f0 20 3d 20 28 75 33 32 2a 29 26 28 28 63 68 61 72   = (u32*)&((char
9d100 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  *)sqlite3GlobalC
9d110 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 29 0a  onfig.pScratch).
9d120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
9d130 20 20 5b 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c    [sqlite3Global
9d140 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63 68  Config.szScratch
9d150 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f  *sqlite3GlobalCo
9d160 6e 66 69 67 2e 6e 53 63 72 61 74 63 68 5d 3b 0a  nfig.nScratch];.
9d170 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73      for(i=0; i<s
9d180 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9d190 69 67 2e 6e 53 63 72 61 74 63 68 3b 20 69 2b 2b  ig.nScratch; i++
9d1a0 29 7b 20 6d 65 6d 30 2e 61 53 63 72 61 74 63 68  ){ mem0.aScratch
9d1b0 46 72 65 65 5b 69 5d 20 3d 20 69 3b 20 7d 0a 20  Free[i] = i; }. 
9d1c0 20 20 20 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68     mem0.nScratch
9d1d0 46 72 65 65 20 3d 20 73 71 6c 69 74 65 33 47 6c  Free = sqlite3Gl
9d1e0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
9d1f0 74 63 68 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  tch;.  }else{.  
9d200 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
9d210 6f 6e 66 69 67 2e 70 53 63 72 61 74 63 68 20 3d  onfig.pScratch =
9d220 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47   0;.    sqlite3G
9d230 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63  lobalConfig.szSc
9d240 72 61 74 63 68 20 3d 20 30 3b 0a 20 20 7d 0a 20  ratch = 0;.  }. 
9d250 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9d260 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 26  alConfig.pPage &
9d270 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  & sqlite3GlobalC
9d280 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3e 3d 35 31  onfig.szPage>=51
9d290 32 0a 20 20 20 20 20 20 26 26 20 73 71 6c 69 74  2.      && sqlit
9d2a0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9d2b0 50 61 67 65 3e 3d 31 20 29 7b 0a 20 20 20 20 69  Page>=1 ){.    i
9d2c0 6e 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6f 76  nt i;.    int ov
9d2d0 65 72 68 65 61 64 3b 0a 20 20 20 20 69 6e 74 20  erhead;.    int 
9d2e0 73 7a 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  sz = sqlite3Glob
9d2f0 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 3b  alConfig.szPage;
9d300 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 71 6c  .    int n = sql
9d310 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d320 2e 6e 50 61 67 65 3b 0a 20 20 20 20 6f 76 65 72  .nPage;.    over
9d330 68 65 61 64 20 3d 20 28 34 2a 6e 20 2b 20 73 7a  head = (4*n + sz
9d340 20 2d 20 31 29 2f 73 7a 3b 0a 20 20 20 20 73 71   - 1)/sz;.    sq
9d350 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9d360 67 2e 6e 50 61 67 65 20 2d 3d 20 6f 76 65 72 68  g.nPage -= overh
9d370 65 61 64 3b 0a 20 20 20 20 6d 65 6d 30 2e 61 50  ead;.    mem0.aP
9d380 61 67 65 46 72 65 65 20 3d 20 28 75 33 32 2a 29  ageFree = (u32*)
9d390 26 28 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  &((char*)sqlite3
9d3a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61  GlobalConfig.pPa
9d3b0 67 65 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  ge).            
9d3c0 20 20 20 20 20 20 5b 73 71 6c 69 74 65 33 47 6c        [sqlite3Gl
9d3d0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9d3e0 65 2a 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  e*sqlite3GlobalC
9d3f0 6f 6e 66 69 67 2e 6e 50 61 67 65 5d 3b 0a 20 20  onfig.nPage];.  
9d400 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 71 6c    for(i=0; i<sql
9d410 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
9d420 2e 6e 50 61 67 65 3b 20 69 2b 2b 29 7b 20 6d 65  .nPage; i++){ me
9d430 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 20  m0.aPageFree[i] 
9d440 3d 20 69 3b 20 7d 0a 20 20 20 20 6d 65 6d 30 2e  = i; }.    mem0.
9d450 6e 50 61 67 65 46 72 65 65 20 3d 20 73 71 6c 69  nPageFree = sqli
9d460 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
9d470 6e 50 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a  nPage;.  }else{.
9d480 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
9d490 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20  lConfig.pPage = 
9d4a0 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  0;.    sqlite3Gl
9d4b0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67  obalConfig.szPag
9d4c0 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
9d4d0 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
9d4e0 6c 43 6f 6e 66 69 67 2e 6d 2e 78 49 6e 69 74 28  lConfig.m.xInit(
9d4f0 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9d500 66 69 67 2e 6d 2e 70 41 70 70 44 61 74 61 29 3b  fig.m.pAppData);
9d510 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 69 6e 69 74  .}../*.** Deinit
9d520 69 61 6c 69 7a 65 20 74 68 65 20 6d 65 6d 6f 72  ialize the memor
9d530 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 73 75 62  y allocation sub
9d540 73 79 73 74 65 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  system..*/.SQLIT
9d550 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
9d560 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64 28  qlite3MallocEnd(
9d570 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33  void){.  sqlite3
9d580 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9d590 53 68 75 74 64 6f 77 6e 28 73 71 6c 69 74 65 33  Shutdown(sqlite3
9d5a0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 70  GlobalConfig.m.p
9d5b0 41 70 70 44 61 74 61 29 3b 0a 20 20 6d 65 6d 73  AppData);.  mems
9d5c0 65 74 28 26 6d 65 6d 30 2c 20 30 2c 20 73 69 7a  et(&mem0, 0, siz
9d5d0 65 6f 66 28 6d 65 6d 30 29 29 3b 0a 7d 0a 0a 2f  eof(mem0));.}../
9d5e0 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
9d5f0 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
9d600 20 63 75 72 72 65 6e 74 6c 79 20 63 68 65 63 6b   currently check
9d610 65 64 20 6f 75 74 2e 0a 2a 2f 0a 53 51 4c 49 54  ed out..*/.SQLIT
9d620 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f 69 6e  E_API sqlite3_in
9d630 74 36 34 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f  t64 sqlite3_memo
9d640 72 79 5f 75 73 65 64 28 76 6f 69 64 29 7b 0a 20  ry_used(void){. 
9d650 20 69 6e 74 20 6e 2c 20 6d 78 3b 0a 20 20 73 71   int n, mx;.  sq
9d660 6c 69 74 65 33 5f 69 6e 74 36 34 20 72 65 73 3b  lite3_int64 res;
9d670 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75  .  sqlite3_statu
9d680 73 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  s(SQLITE_STATUS_
9d690 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 26 6e 2c  MEMORY_USED, &n,
9d6a0 20 26 6d 78 2c 20 30 29 3b 0a 20 20 72 65 73 20   &mx, 0);.  res 
9d6b0 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
9d6c0 29 6e 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f  )n;  /* Work aro
9d6d0 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61  und bug in Borla
9d6e0 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32  nd C. Ticket #32
9d6f0 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  16 */.  return r
9d700 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  es;.}../*.** Ret
9d710 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  urn the maximum 
9d720 61 6d 6f 75 6e 74 20 6f 66 20 6d 65 6d 6f 72 79  amount of memory
9d730 20 74 68 61 74 20 68 61 73 20 65 76 65 72 20 62   that has ever b
9d740 65 65 6e 0a 2a 2a 20 63 68 65 63 6b 65 64 20 6f  een.** checked o
9d750 75 74 20 73 69 6e 63 65 20 65 69 74 68 65 72 20  ut since either 
9d760 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
9d770 20 74 68 69 73 20 70 72 6f 63 65 73 73 0a 2a 2a   this process.**
9d780 20 6f 72 20 73 69 6e 63 65 20 74 68 65 20 6d 6f   or since the mo
9d790 73 74 20 72 65 63 65 6e 74 20 72 65 73 65 74 2e  st recent reset.
9d7a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 73  .*/.SQLITE_API s
9d7b0 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 73 71 6c  qlite3_int64 sql
9d7c0 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 68 69 67 68  ite3_memory_high
9d7d0 77 61 74 65 72 28 69 6e 74 20 72 65 73 65 74 46  water(int resetF
9d7e0 6c 61 67 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6d  lag){.  int n, m
9d7f0 78 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  x;.  sqlite3_int
9d800 36 34 20 72 65 73 3b 0a 20 20 73 71 6c 69 74 65  64 res;.  sqlite
9d810 33 5f 73 74 61 74 75 73 28 53 51 4c 49 54 45 5f  3_status(SQLITE_
9d820 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53  STATUS_MEMORY_US
9d830 45 44 2c 20 26 6e 2c 20 26 6d 78 2c 20 72 65 73  ED, &n, &mx, res
9d840 65 74 46 6c 61 67 29 3b 0a 20 20 72 65 73 20 3d  etFlag);.  res =
9d850 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 29   (sqlite3_int64)
9d860 6d 78 3b 20 20 2f 2a 20 57 6f 72 6b 20 61 72 6f  mx;  /* Work aro
9d870 75 6e 64 20 62 75 67 20 69 6e 20 42 6f 72 6c 61  und bug in Borla
9d880 6e 64 20 43 2e 20 54 69 63 6b 65 74 20 23 33 32  nd C. Ticket #32
9d890 31 36 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72  16 */.  return r
9d8a0 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  es;.}../*.** Cha
9d8b0 6e 67 65 20 74 68 65 20 61 6c 61 72 6d 20 63 61  nge the alarm ca
9d8c0 6c 6c 62 61 63 6b 0a 2a 2f 0a 53 51 4c 49 54 45  llback.*/.SQLITE
9d8d0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
9d8e0 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28  ite3MemoryAlarm(
9d8f0 0a 20 20 76 6f 69 64 28 2a 78 43 61 6c 6c 62 61  .  void(*xCallba
9d900 63 6b 29 28 76 6f 69 64 20 2a 70 41 72 67 2c 20  ck)(void *pArg, 
9d910 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 75 73  sqlite3_int64 us
9d920 65 64 2c 69 6e 74 20 4e 29 2c 0a 20 20 76 6f 69  ed,int N),.  voi
9d930 64 20 2a 70 41 72 67 2c 0a 20 20 73 71 6c 69 74  d *pArg,.  sqlit
9d940 65 33 5f 69 6e 74 36 34 20 69 54 68 72 65 73 68  e3_int64 iThresh
9d950 6f 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33  old.){.  sqlite3
9d960 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9d970 30 2e 6d 75 74 65 78 29 3b 0a 20 20 6d 65 6d 30  0.mutex);.  mem0
9d980 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 3d  .alarmCallback =
9d990 20 78 43 61 6c 6c 62 61 63 6b 3b 0a 20 20 6d 65   xCallback;.  me
9d9a0 6d 30 2e 61 6c 61 72 6d 41 72 67 20 3d 20 70 41  m0.alarmArg = pA
9d9b0 72 67 3b 0a 20 20 6d 65 6d 30 2e 61 6c 61 72 6d  rg;.  mem0.alarm
9d9c0 54 68 72 65 73 68 6f 6c 64 20 3d 20 69 54 68 72  Threshold = iThr
9d9d0 65 73 68 6f 6c 64 3b 0a 20 20 73 71 6c 69 74 65  eshold;.  sqlite
9d9e0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9d9f0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  m0.mutex);.  ret
9da00 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
9da10 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
9da20 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
9da30 0a 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65  ./*.** Deprecate
9da40 64 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72  d external inter
9da50 66 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f  face.  Internal/
9da60 63 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65  core SQLite code
9da70 0a 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20  .** should call 
9da80 73 71 6c 69 74 65 33 4d 65 6d 6f 72 79 41 6c 61  sqlite3MemoryAla
9da90 72 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  rm..*/.SQLITE_AP
9daa0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6d 65  I int sqlite3_me
9dab0 6d 6f 72 79 5f 61 6c 61 72 6d 28 0a 20 20 76 6f  mory_alarm(.  vo
9dac0 69 64 28 2a 78 43 61 6c 6c 62 61 63 6b 29 28 76  id(*xCallback)(v
9dad0 6f 69 64 20 2a 70 41 72 67 2c 20 73 71 6c 69 74  oid *pArg, sqlit
9dae0 65 33 5f 69 6e 74 36 34 20 75 73 65 64 2c 69 6e  e3_int64 used,in
9daf0 74 20 4e 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41  t N),.  void *pA
9db00 72 67 2c 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e  rg,.  sqlite3_in
9db10 74 36 34 20 69 54 68 72 65 73 68 6f 6c 64 0a 29  t64 iThreshold.)
9db20 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
9db30 65 33 4d 65 6d 6f 72 79 41 6c 61 72 6d 28 78 43  e3MemoryAlarm(xC
9db40 61 6c 6c 62 61 63 6b 2c 20 70 41 72 67 2c 20 69  allback, pArg, i
9db50 54 68 72 65 73 68 6f 6c 64 29 3b 0a 7d 0a 23 65  Threshold);.}.#e
9db60 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 72 69 67  ndif../*.** Trig
9db70 67 65 72 20 74 68 65 20 61 6c 61 72 6d 20 0a 2a  ger the alarm .*
9db80 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 71  /.static void sq
9db90 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d  lite3MallocAlarm
9dba0 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a 20 20 76  (int nByte){.  v
9dbb0 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63 6b 29  oid (*xCallback)
9dbc0 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65 33 5f 69  (void*,sqlite3_i
9dbd0 6e 74 36 34 2c 69 6e 74 29 3b 0a 20 20 73 71 6c  nt64,int);.  sql
9dbe0 69 74 65 33 5f 69 6e 74 36 34 20 6e 6f 77 55 73  ite3_int64 nowUs
9dbf0 65 64 3b 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  ed;.  void *pArg
9dc00 3b 0a 20 20 69 66 28 20 6d 65 6d 30 2e 61 6c 61  ;.  if( mem0.ala
9dc10 72 6d 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 7c 7c  rmCallback==0 ||
9dc20 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20   mem0.alarmBusy 
9dc30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d   ) return;.  mem
9dc40 30 2e 61 6c 61 72 6d 42 75 73 79 20 3d 20 31 3b  0.alarmBusy = 1;
9dc50 0a 20 20 78 43 61 6c 6c 62 61 63 6b 20 3d 20 6d  .  xCallback = m
9dc60 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61 63  em0.alarmCallbac
9dc70 6b 3b 0a 20 20 6e 6f 77 55 73 65 64 20 3d 20 73  k;.  nowUsed = s
9dc80 71 6c 69 74 65 33 53 74 61 74 75 73 56 61 6c 75  qlite3StatusValu
9dc90 65 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  e(SQLITE_STATUS_
9dca0 4d 45 4d 4f 52 59 5f 55 53 45 44 29 3b 0a 20 20  MEMORY_USED);.  
9dcb0 70 41 72 67 20 3d 20 6d 65 6d 30 2e 61 6c 61 72  pArg = mem0.alar
9dcc0 6d 41 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f  mArg;.  sqlite3_
9dcd0 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30  mutex_leave(mem0
9dce0 2e 6d 75 74 65 78 29 3b 0a 20 20 78 43 61 6c 6c  .mutex);.  xCall
9dcf0 62 61 63 6b 28 70 41 72 67 2c 20 6e 6f 77 55 73  back(pArg, nowUs
9dd00 65 64 2c 20 6e 42 79 74 65 29 3b 0a 20 20 73 71  ed, nByte);.  sq
9dd10 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
9dd20 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20  r(mem0.mutex);. 
9dd30 20 6d 65 6d 30 2e 61 6c 61 72 6d 42 75 73 79 20   mem0.alarmBusy 
9dd40 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f  = 0;.}../*.** Do
9dd50 20 61 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61   a memory alloca
9dd60 74 69 6f 6e 20 77 69 74 68 20 73 74 61 74 69 73  tion with statis
9dd70 74 69 63 73 20 61 6e 64 20 61 6c 61 72 6d 73 2e  tics and alarms.
9dd80 20 20 41 73 73 75 6d 65 20 74 68 65 0a 2a 2a 20    Assume the.** 
9dd90 6c 6f 63 6b 20 69 73 20 61 6c 72 65 61 64 79 20  lock is already 
9dda0 68 65 6c 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  held..*/.static 
9ddb0 69 6e 74 20 6d 61 6c 6c 6f 63 57 69 74 68 41 6c  int mallocWithAl
9ddc0 61 72 6d 28 69 6e 74 20 6e 2c 20 76 6f 69 64 20  arm(int n, void 
9ddd0 2a 2a 70 70 29 7b 0a 20 20 69 6e 74 20 6e 46 75  **pp){.  int nFu
9dde0 6c 6c 3b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20  ll;.  void *p;. 
9ddf0 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
9de00 5f 6d 75 74 65 78 5f 68 65 6c 64 28 6d 65 6d 30  _mutex_held(mem0
9de10 2e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 46 75  .mutex) );.  nFu
9de20 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62  ll = sqlite3Glob
9de30 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e  alConfig.m.xRoun
9de40 64 75 70 28 6e 29 3b 0a 20 20 73 71 6c 69 74 65  dup(n);.  sqlite
9de50 33 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54  3StatusSet(SQLIT
9de60 45 5f 53 54 41 54 55 53 5f 4d 41 4c 4c 4f 43 5f  E_STATUS_MALLOC_
9de70 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 69 66 28 20  SIZE, n);.  if( 
9de80 6d 65 6d 30 2e 61 6c 61 72 6d 43 61 6c 6c 62 61  mem0.alarmCallba
9de90 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ck!=0 ){.    int
9dea0 20 6e 55 73 65 64 20 3d 20 73 71 6c 69 74 65 33   nUsed = sqlite3
9deb0 53 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49  StatusValue(SQLI
9dec0 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59  TE_STATUS_MEMORY
9ded0 5f 55 53 45 44 29 3b 0a 20 20 20 20 69 66 28 20  _USED);.    if( 
9dee0 6e 55 73 65 64 2b 6e 46 75 6c 6c 20 3e 3d 20 6d  nUsed+nFull >= m
9def0 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68 6f  em0.alarmThresho
9df00 6c 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ld ){.      sqli
9df10 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72 6d 28 6e  te3MallocAlarm(n
9df20 46 75 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Full);.    }.  }
9df30 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c  .  p = sqlite3Gl
9df40 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61  obalConfig.m.xMa
9df50 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20 69  lloc(nFull);.  i
9df60 66 28 20 70 3d 3d 30 20 26 26 20 6d 65 6d 30 2e  f( p==0 && mem0.
9df70 61 6c 61 72 6d 43 61 6c 6c 62 61 63 6b 20 29 7b  alarmCallback ){
9df80 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c  .    sqlite3Mall
9df90 6f 63 41 6c 61 72 6d 28 6e 46 75 6c 6c 29 3b 0a  ocAlarm(nFull);.
9dfa0 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47      p = sqlite3G
9dfb0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d  lobalConfig.m.xM
9dfc0 61 6c 6c 6f 63 28 6e 46 75 6c 6c 29 3b 0a 20 20  alloc(nFull);.  
9dfd0 7d 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  }.  if( p ){.   
9dfe0 20 6e 46 75 6c 6c 20 3d 20 73 71 6c 69 74 65 33   nFull = sqlite3
9dff0 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20  MallocSize(p);. 
9e000 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9e010 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
9e020 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e  S_MEMORY_USED, n
9e030 46 75 6c 6c 29 3b 0a 20 20 7d 0a 20 20 2a 70 70  Full);.  }.  *pp
9e040 20 3d 20 70 3b 0a 20 20 72 65 74 75 72 6e 20 6e   = p;.  return n
9e050 46 75 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  Full;.}../*.** A
9e060 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 2e 20  llocate memory. 
9e070 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
9e080 20 6c 69 6b 65 20 73 71 6c 69 74 65 33 5f 6d 61   like sqlite3_ma
9e090 6c 6c 6f 63 28 29 20 65 78 63 65 70 74 20 74 68  lloc() except th
9e0a0 61 74 20 69 74 0a 2a 2a 20 61 73 73 75 6d 65 73  at it.** assumes
9e0b0 20 74 68 65 20 6d 65 6d 6f 72 79 20 73 75 62 73   the memory subs
9e0c0 79 73 74 65 6d 20 68 61 73 20 61 6c 72 65 61 64  ystem has alread
9e0d0 79 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a  y been initializ
9e0e0 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
9e0f0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
9e100 74 65 33 4d 61 6c 6c 6f 63 28 69 6e 74 20 6e 29  te3Malloc(int n)
9e110 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 69  {.  void *p;.  i
9e120 66 28 20 6e 3c 3d 30 20 29 7b 0a 20 20 20 20 70  f( n<=0 ){.    p
9e130 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
9e140 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
9e150 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61 74 20 29  onfig.bMemstat )
9e160 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  {.    sqlite3_mu
9e170 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d  tex_enter(mem0.m
9e180 75 74 65 78 29 3b 0a 20 20 20 20 6d 61 6c 6c 6f  utex);.    mallo
9e190 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20 26 70  cWithAlarm(n, &p
9e1a0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
9e1b0 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9e1c0 6d 75 74 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b  mutex);.  }else{
9e1d0 0a 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33  .    p = sqlite3
9e1e0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9e1f0 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 20  Malloc(n);.  }. 
9e200 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a   return p;.}../*
9e210 0a 2a 2a 20 54 68 69 73 20 76 65 72 73 69 6f 6e  .** This version
9e220 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 61   of the memory a
9e230 6c 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 6f 72  llocation is for
9e240 20 75 73 65 20 62 79 20 74 68 65 20 61 70 70 6c   use by the appl
9e250 69 63 61 74 69 6f 6e 2e 0a 2a 2a 20 46 69 72 73  ication..** Firs
9e260 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20  t make sure the 
9e270 6d 65 6d 6f 72 79 20 73 75 62 73 79 73 74 65 6d  memory subsystem
9e280 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c   is initialized,
9e290 20 74 68 65 6e 20 64 6f 20 74 68 65 0a 2a 2a 20   then do the.** 
9e2a0 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53  allocation..*/.S
9e2b0 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
9e2c0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 69  sqlite3_malloc(i
9e2d0 6e 74 20 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53  nt n){.#ifndef S
9e2e0 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49  QLITE_OMIT_AUTOI
9e2f0 4e 49 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65  NIT.  if( sqlite
9e300 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 29  3_initialize() )
9e310 20 72 65 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69   return 0;.#endi
9e320 66 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  f.  return sqlit
9e330 65 33 4d 61 6c 6c 6f 63 28 6e 29 3b 0a 7d 0a 0a  e3Malloc(n);.}..
9e340 2f 2a 0a 2a 2a 20 45 61 63 68 20 74 68 72 65 61  /*.** Each threa
9e350 64 20 6d 61 79 20 6f 6e 6c 79 20 68 61 76 65 20  d may only have 
9e360 61 20 73 69 6e 67 6c 65 20 6f 75 74 73 74 61 6e  a single outstan
9e370 64 69 6e 67 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  ding allocation 
9e380 66 72 6f 6d 0a 2a 2a 20 78 53 63 72 61 74 63 68  from.** xScratch
9e390 4d 61 6c 6c 6f 63 28 29 2e 20 20 57 65 20 76 65  Malloc().  We ve
9e3a0 72 69 66 79 20 74 68 69 73 20 63 6f 6e 73 74 72  rify this constr
9e3b0 61 69 6e 74 20 69 6e 20 74 68 65 20 73 69 6e 67  aint in the sing
9e3c0 6c 65 2d 74 68 72 65 61 64 65 64 0a 2a 2a 20 63  le-threaded.** c
9e3d0 61 73 65 20 62 79 20 73 65 74 74 69 6e 67 20 73  ase by setting s
9e3e0 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 74  cratchAllocOut t
9e3f0 6f 20 31 20 77 68 65 6e 20 61 6e 20 61 6c 6c 6f  o 1 when an allo
9e400 63 61 74 69 6f 6e 0a 2a 2a 20 69 73 20 6f 75 74  cation.** is out
9e410 73 74 61 6e 64 69 6e 67 20 63 6c 65 61 72 69 6e  standing clearin
9e420 67 20 69 74 20 77 68 65 6e 20 74 68 65 20 61 6c  g it when the al
9e430 6c 6f 63 61 74 69 6f 6e 20 69 73 20 66 72 65 65  location is free
9e440 64 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  d..*/.#if SQLITE
9e450 5f 54 48 52 45 41 44 53 41 46 45 3d 3d 30 20 26  _THREADSAFE==0 &
9e460 26 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55  & !defined(NDEBU
9e470 47 29 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63  G).static int sc
9e480 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20  ratchAllocOut = 
9e490 30 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  0;.#endif.../*.*
9e4a0 2a 20 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72  * Allocate memor
9e4b0 79 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  y that is to be 
9e4c0 75 73 65 64 20 61 6e 64 20 72 65 6c 65 61 73 65  used and release
9e4d0 64 20 72 69 67 68 74 20 61 77 61 79 2e 0a 2a 2a  d right away..**
9e4e0 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
9e4f0 20 73 69 6d 69 6c 61 72 20 74 6f 20 61 6c 6c 6f   similar to allo
9e500 63 61 28 29 20 69 6e 20 74 68 61 74 20 69 74 20  ca() in that it 
9e510 69 73 20 6e 6f 74 20 69 6e 74 65 6e 64 65 64 0a  is not intended.
9e520 2a 2a 20 66 6f 72 20 73 69 74 75 61 74 69 6f 6e  ** for situation
9e530 73 20 77 68 65 72 65 20 74 68 65 20 6d 65 6d 6f  s where the memo
9e540 72 79 20 6d 69 67 68 74 20 62 65 20 68 65 6c 64  ry might be held
9e550 20 6c 6f 6e 67 2d 74 65 72 6d 2e 20 20 54 68 69   long-term.  Thi
9e560 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20  s.** routine is 
9e570 69 6e 74 65 6e 64 65 64 20 74 6f 20 67 65 74 20  intended to get 
9e580 6d 65 6d 6f 72 79 20 74 6f 20 6f 6c 64 20 6c 61  memory to old la
9e590 72 67 65 20 74 72 61 6e 73 69 65 6e 74 20 64 61  rge transient da
9e5a0 74 61 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 73  ta.** structures
9e5b0 20 74 68 61 74 20 77 6f 75 6c 64 20 6e 6f 74 20   that would not 
9e5c0 6e 6f 72 6d 61 6c 6c 79 20 66 69 74 20 6f 6e 20  normally fit on 
9e5d0 74 68 65 20 73 74 61 63 6b 20 6f 66 20 61 6e 0a  the stack of an.
9e5e0 2a 2a 20 65 6d 62 65 64 64 65 64 20 70 72 6f 63  ** embedded proc
9e5f0 65 73 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  essor..*/.SQLITE
9e600 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73  _PRIVATE void *s
9e610 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61 6c  qlite3ScratchMal
9e620 6c 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  loc(int n){.  vo
9e630 69 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  id *p;.  assert(
9e640 20 6e 3e 30 20 29 3b 0a 0a 23 69 66 20 53 51 4c   n>0 );..#if SQL
9e650 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
9e660 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
9e670 45 42 55 47 29 0a 20 20 2f 2a 20 56 65 72 69 66  EBUG).  /* Verif
9e680 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65 20 74  y that no more t
9e690 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63 68 20  han one scratch 
9e6a0 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72 20 74  allocation per t
9e6b0 68 72 65 61 64 0a 20 20 2a 2a 20 69 73 20 6f 75  hread.  ** is ou
9e6c0 74 73 74 61 6e 64 69 6e 67 20 61 74 20 6f 6e 65  tstanding at one
9e6d0 20 74 69 6d 65 2e 20 20 28 54 68 69 73 20 69 73   time.  (This is
9e6e0 20 6f 6e 6c 79 20 63 68 65 63 6b 65 64 20 69 6e   only checked in
9e6f0 20 74 68 65 0a 20 20 2a 2a 20 73 69 6e 67 6c 65   the.  ** single
9e700 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 20 73  -threaded case s
9e710 69 6e 63 65 20 63 68 65 63 6b 69 6e 67 20 69 6e  ince checking in
9e720 20 74 68 65 20 6d 75 6c 74 69 2d 74 68 72 65 61   the multi-threa
9e730 64 65 64 20 63 61 73 65 0a 20 20 2a 2a 20 77 6f  ded case.  ** wo
9e740 75 6c 64 20 62 65 20 6d 75 63 68 20 6d 6f 72 65  uld be much more
9e750 20 63 6f 6d 70 6c 69 63 61 74 65 64 2e 29 20 2a   complicated.) *
9e760 2f 0a 20 20 61 73 73 65 72 74 28 20 73 63 72 61  /.  assert( scra
9e770 74 63 68 41 6c 6c 6f 63 4f 75 74 3d 3d 30 20 29  tchAllocOut==0 )
9e780 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20  ;.#endif..  if( 
9e790 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9e7a0 66 69 67 2e 73 7a 53 63 72 61 74 63 68 3c 6e 20  fig.szScratch<n 
9e7b0 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73 63 72 61  ){.    goto scra
9e7c0 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3b 0a 20 20  tch_overflow;.  
9e7d0 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 73 71 6c  }else{  .    sql
9e7e0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9e7f0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9e800 20 20 69 66 28 20 6d 65 6d 30 2e 6e 53 63 72 61    if( mem0.nScra
9e810 74 63 68 46 72 65 65 3d 3d 30 20 29 7b 0a 20 20  tchFree==0 ){.  
9e820 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9e830 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9e840 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  ex);.      goto 
9e850 73 63 72 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77  scratch_overflow
9e860 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
9e870 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
9e880 69 20 3d 20 6d 65 6d 30 2e 61 53 63 72 61 74 63  i = mem0.aScratc
9e890 68 46 72 65 65 5b 2d 2d 6d 65 6d 30 2e 6e 53 63  hFree[--mem0.nSc
9e8a0 72 61 74 63 68 46 72 65 65 5d 3b 0a 20 20 20 20  ratchFree];.    
9e8b0 20 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c    i *= sqlite3Gl
9e8c0 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
9e8d0 61 74 63 68 3b 0a 20 20 20 20 20 20 73 71 6c 69  atch;.      sqli
9e8e0 74 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c  te3StatusAdd(SQL
9e8f0 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54  ITE_STATUS_SCRAT
9e900 43 48 5f 55 53 45 44 2c 20 31 29 3b 0a 20 20 20  CH_USED, 1);.   
9e910 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
9e920 53 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Set(SQLITE_STATU
9e930 53 5f 53 43 52 41 54 43 48 5f 53 49 5a 45 2c 20  S_SCRATCH_SIZE, 
9e940 6e 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  n);.      sqlite
9e950 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9e960 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9e970 20 70 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63   p = (void*)&((c
9e980 68 61 72 2a 29 73 71 6c 69 74 65 33 47 6c 6f 62  har*)sqlite3Glob
9e990 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9e9a0 68 29 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d  h)[i];.    }.  }
9e9b0 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
9e9c0 41 44 53 41 46 45 3d 3d 30 20 26 26 20 21 64 65  ADSAFE==0 && !de
9e9d0 66 69 6e 65 64 28 4e 44 45 42 55 47 29 0a 20 20  fined(NDEBUG).  
9e9e0 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20  scratchAllocOut 
9e9f0 3d 20 70 21 3d 30 3b 0a 23 65 6e 64 69 66 0a 0a  = p!=0;.#endif..
9ea00 20 20 72 65 74 75 72 6e 20 70 3b 0a 0a 73 63 72    return p;..scr
9ea10 61 74 63 68 5f 6f 76 65 72 66 6c 6f 77 3a 0a 20  atch_overflow:. 
9ea20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9ea30 61 6c 43 6f 6e 66 69 67 2e 62 4d 65 6d 73 74 61  alConfig.bMemsta
9ea40 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
9ea50 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 65 6d  _mutex_enter(mem
9ea60 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71  0.mutex);.    sq
9ea70 6c 69 74 65 33 53 74 61 74 75 73 53 65 74 28 53  lite3StatusSet(S
9ea80 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43 52  QLITE_STATUS_SCR
9ea90 41 54 43 48 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20  ATCH_SIZE, n);. 
9eaa0 20 20 20 6e 20 3d 20 6d 61 6c 6c 6f 63 57 69 74     n = mallocWit
9eab0 68 41 6c 61 72 6d 28 6e 2c 20 26 70 29 3b 0a 20  hAlarm(n, &p);. 
9eac0 20 20 20 69 66 28 20 70 20 29 20 73 71 6c 69 74     if( p ) sqlit
9ead0 65 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49  e3StatusAdd(SQLI
9eae0 54 45 5f 53 54 41 54 55 53 5f 53 43 52 41 54 43  TE_STATUS_SCRATC
9eaf0 48 5f 4f 56 45 52 46 4c 4f 57 2c 20 6e 29 3b 0a  H_OVERFLOW, n);.
9eb00 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
9eb10 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74  x_leave(mem0.mut
9eb20 65 78 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ex);.  }else{.  
9eb30 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f    p = sqlite3Glo
9eb40 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d 61 6c  balConfig.m.xMal
9eb50 6c 6f 63 28 6e 29 3b 0a 20 20 7d 0a 23 69 66 20  loc(n);.  }.#if 
9eb60 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
9eb70 45 3d 3d 30 20 26 26 20 21 64 65 66 69 6e 65 64  E==0 && !defined
9eb80 28 4e 44 45 42 55 47 29 0a 20 20 73 63 72 61 74  (NDEBUG).  scrat
9eb90 63 68 41 6c 6c 6f 63 4f 75 74 20 3d 20 70 21 3d  chAllocOut = p!=
9eba0 30 3b 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75  0;.#endif.  retu
9ebb0 72 6e 20 70 3b 20 20 20 20 0a 7d 0a 53 51 4c 49  rn p;    .}.SQLI
9ebc0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9ebd0 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 46 72  sqlite3ScratchFr
9ebe0 65 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 69  ee(void *p){.  i
9ebf0 66 28 20 70 20 29 7b 0a 0a 23 69 66 20 53 51 4c  f( p ){..#if SQL
9ec00 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 3d 3d  ITE_THREADSAFE==
9ec10 30 20 26 26 20 21 64 65 66 69 6e 65 64 28 4e 44  0 && !defined(ND
9ec20 45 42 55 47 29 0a 20 20 20 20 2f 2a 20 56 65 72  EBUG).    /* Ver
9ec30 69 66 79 20 74 68 61 74 20 6e 6f 20 6d 6f 72 65  ify that no more
9ec40 20 74 68 61 6e 20 6f 6e 65 20 73 63 72 61 74 63   than one scratc
9ec50 68 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 65 72  h allocation per
9ec60 20 74 68 72 65 61 64 0a 20 20 20 20 2a 2a 20 69   thread.    ** i
9ec70 73 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 61 74  s outstanding at
9ec80 20 6f 6e 65 20 74 69 6d 65 2e 20 20 28 54 68 69   one time.  (Thi
9ec90 73 20 69 73 20 6f 6e 6c 79 20 63 68 65 63 6b 65  s is only checke
9eca0 64 20 69 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  d in the.    ** 
9ecb0 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64 20  single-threaded 
9ecc0 63 61 73 65 20 73 69 6e 63 65 20 63 68 65 63 6b  case since check
9ecd0 69 6e 67 20 69 6e 20 74 68 65 20 6d 75 6c 74 69  ing in the multi
9ece0 2d 74 68 72 65 61 64 65 64 20 63 61 73 65 0a 20  -threaded case. 
9ecf0 20 20 20 2a 2a 20 77 6f 75 6c 64 20 62 65 20 6d     ** would be m
9ed00 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 69 63  uch more complic
9ed10 61 74 65 64 2e 29 20 2a 2f 0a 20 20 20 20 61 73  ated.) */.    as
9ed20 73 65 72 74 28 20 73 63 72 61 74 63 68 41 6c 6c  sert( scratchAll
9ed30 6f 63 4f 75 74 3d 3d 31 20 29 3b 0a 20 20 20 20  ocOut==1 );.    
9ed40 73 63 72 61 74 63 68 41 6c 6c 6f 63 4f 75 74 20  scratchAllocOut 
9ed50 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20  = 0;.#endif..   
9ed60 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f 62   if( sqlite3Glob
9ed70 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9ed80 68 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20  h==0.           
9ed90 7c 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62  || p<sqlite3Glob
9eda0 61 6c 43 6f 6e 66 69 67 2e 70 53 63 72 61 74 63  alConfig.pScratc
9edb0 68 0a 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  h.           || 
9edc0 70 3e 3d 28 76 6f 69 64 2a 29 6d 65 6d 30 2e 61  p>=(void*)mem0.a
9edd0 53 63 72 61 74 63 68 46 72 65 65 20 29 7b 0a 20  ScratchFree ){. 
9ede0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
9edf0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
9ee00 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 20 20 20  mstat ){.       
9ee10 20 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c   int iSize = sql
9ee20 69 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70  ite3MallocSize(p
9ee30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
9ee40 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
9ee50 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9ee60 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
9ee70 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
9ee80 55 53 5f 53 43 52 41 54 43 48 5f 4f 56 45 52 46  US_SCRATCH_OVERF
9ee90 4c 4f 57 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20  LOW, -iSize);.  
9eea0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 61        sqlite3Sta
9eeb0 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
9eec0 41 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44  ATUS_MEMORY_USED
9eed0 2c 20 2d 69 53 69 7a 65 29 3b 0a 20 20 20 20 20  , -iSize);.     
9eee0 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
9eef0 43 6f 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70  Config.m.xFree(p
9ef00 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
9ef10 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d  e3_mutex_leave(m
9ef20 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20  em0.mutex);.    
9ef30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
9ef40 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
9ef50 6e 66 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b  nfig.m.xFree(p);
9ef60 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
9ef70 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b  se{.      int i;
9ef80 0a 20 20 20 20 20 20 69 20 3d 20 28 75 38 20 2a  .      i = (u8 *
9ef90 29 70 20 2d 20 28 75 38 20 2a 29 73 71 6c 69 74  )p - (u8 *)sqlit
9efa0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
9efb0 53 63 72 61 74 63 68 3b 0a 20 20 20 20 20 20 69  Scratch;.      i
9efc0 20 2f 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61   /= sqlite3Globa
9efd0 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72 61 74 63  lConfig.szScratc
9efe0 68 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  h;.      assert(
9eff0 20 69 3e 3d 30 20 26 26 20 69 3c 73 71 6c 69 74   i>=0 && i<sqlit
9f000 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
9f010 53 63 72 61 74 63 68 20 29 3b 0a 20 20 20 20 20  Scratch );.     
9f020 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9f030 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9f040 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
9f050 6d 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65  mem0.nScratchFre
9f060 65 3c 28 75 33 32 29 73 71 6c 69 74 65 33 47 6c  e<(u32)sqlite3Gl
9f070 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 53 63 72 61  obalConfig.nScra
9f080 74 63 68 20 29 3b 0a 20 20 20 20 20 20 6d 65 6d  tch );.      mem
9f090 30 2e 61 53 63 72 61 74 63 68 46 72 65 65 5b 6d  0.aScratchFree[m
9f0a0 65 6d 30 2e 6e 53 63 72 61 74 63 68 46 72 65 65  em0.nScratchFree
9f0b0 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20 20 20 73  ++] = i;.      s
9f0c0 71 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28  qlite3StatusAdd(
9f0d0 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 53 43  SQLITE_STATUS_SC
9f0e0 52 41 54 43 48 5f 55 53 45 44 2c 20 2d 31 29 3b  RATCH_USED, -1);
9f0f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
9f100 75 74 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e  utex_leave(mem0.
9f110 6d 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20  mutex);.    }.  
9f120 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  }.}../*.** Alloc
9f130 61 74 65 20 6d 65 6d 6f 72 79 20 74 6f 20 62 65  ate memory to be
9f140 20 75 73 65 64 20 62 79 20 74 68 65 20 70 61 67   used by the pag
9f150 65 20 63 61 63 68 65 2e 20 20 4d 61 6b 65 20 75  e cache.  Make u
9f160 73 65 20 6f 66 20 74 68 65 0a 2a 2a 20 6d 65 6d  se of the.** mem
9f170 6f 72 79 20 62 75 66 66 65 72 20 70 72 6f 76 69  ory buffer provi
9f180 64 65 64 20 62 79 20 53 51 4c 49 54 45 5f 43 4f  ded by SQLITE_CO
9f190 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20 69  NFIG_PAGECACHE i
9f1a0 66 20 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 2a  f there is one.*
9f1b0 2a 20 61 6e 64 20 74 68 61 74 20 6d 65 6d 6f 72  * and that memor
9f1c0 79 20 69 73 20 6f 66 20 74 68 65 20 72 69 67 68  y is of the righ
9f1d0 74 20 73 69 7a 65 20 61 6e 64 20 69 73 20 6e 6f  t size and is no
9f1e0 74 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 2a 2a 20  t completely.** 
9f1f0 63 6f 6e 73 75 6d 65 64 2e 20 20 4f 74 68 65 72  consumed.  Other
9f200 77 69 73 65 2c 20 66 61 69 6c 6f 76 65 72 20 74  wise, failover t
9f210 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  o sqlite3Malloc(
9f220 29 2e 0a 2a 2f 0a 23 69 66 20 30 0a 53 51 4c 49  )..*/.#if 0.SQLI
9f230 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
9f240 2a 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c  *sqlite3PageMall
9f250 6f 63 28 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69  oc(int n){.  voi
9f260 64 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20  d *p;.  assert( 
9f270 6e 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  n>0 );.  assert(
9f280 20 28 6e 20 26 20 28 6e 2d 31 29 29 3d 3d 30 20   (n & (n-1))==0 
9f290 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d  );.  assert( n>=
9f2a0 35 31 32 20 26 26 20 6e 3c 3d 33 32 37 36 38 20  512 && n<=32768 
9f2b0 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
9f2c0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a  3GlobalConfig.sz
9f2d0 50 61 67 65 3c 6e 20 29 7b 0a 20 20 20 20 67 6f  Page<n ){.    go
9f2e0 74 6f 20 70 61 67 65 5f 6f 76 65 72 66 6c 6f 77  to page_overflow
9f2f0 3b 0a 20 20 7d 65 6c 73 65 7b 20 20 0a 20 20 20  ;.  }else{  .   
9f300 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9f310 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9f320 3b 0a 20 20 20 20 69 66 28 20 6d 65 6d 30 2e 6e  ;.    if( mem0.n
9f330 50 61 67 65 46 72 65 65 3d 3d 30 20 29 7b 0a 20  PageFree==0 ){. 
9f340 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9f350 65 78 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75  ex_leave(mem0.mu
9f360 74 65 78 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  tex);.      goto
9f370 20 70 61 67 65 5f 6f 76 65 72 66 6c 6f 77 3b 0a   page_overflow;.
9f380 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
9f390 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 69 20   int i;.      i 
9f3a0 3d 20 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65  = mem0.aPageFree
9f3b0 5b 2d 2d 6d 65 6d 30 2e 6e 50 61 67 65 46 72 65  [--mem0.nPageFre
9f3c0 65 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e];.      sqlite
9f3d0 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 65  3_mutex_leave(me
9f3e0 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  m0.mutex);.     
9f3f0 20 69 20 2a 3d 20 73 71 6c 69 74 65 33 47 6c 6f   i *= sqlite3Glo
9f400 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65  balConfig.szPage
9f410 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53  ;.      sqlite3S
9f420 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45 5f  tatusSet(SQLITE_
9f430 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45  STATUS_PAGECACHE
9f440 5f 53 49 5a 45 2c 20 6e 29 3b 0a 20 20 20 20 20  _SIZE, n);.     
9f450 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9f460 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9f470 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c 20  PAGECACHE_USED, 
9f480 31 29 3b 0a 20 20 20 20 20 20 70 20 3d 20 28 76  1);.      p = (v
9f490 6f 69 64 2a 29 26 28 28 63 68 61 72 2a 29 73 71  oid*)&((char*)sq
9f4a0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9f4b0 67 2e 70 50 61 67 65 29 5b 69 5d 3b 0a 20 20 20  g.pPage)[i];.   
9f4c0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
9f4d0 70 3b 0a 0a 70 61 67 65 5f 6f 76 65 72 66 6c 6f  p;..page_overflo
9f4e0 77 3a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  w:.  if( sqlite3
9f4f0 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
9f500 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
9f510 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
9f520 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
9f530 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
9f540 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
9f550 5f 50 41 47 45 43 41 43 48 45 5f 53 49 5a 45 2c  _PAGECACHE_SIZE,
9f560 20 6e 29 3b 0a 20 20 20 20 6e 20 3d 20 6d 61 6c   n);.    n = mal
9f570 6c 6f 63 57 69 74 68 41 6c 61 72 6d 28 6e 2c 20  locWithAlarm(n, 
9f580 26 70 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29  &p);.    if( p )
9f590 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41 64   sqlite3StatusAd
9f5a0 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  d(SQLITE_STATUS_
9f5b0 50 41 47 45 43 41 43 48 45 5f 4f 56 45 52 46 4c  PAGECACHE_OVERFL
9f5c0 4f 57 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69  OW, n);.    sqli
9f5d0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9f5e0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20 7d  mem0.mutex);.  }
9f5f0 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20 73 71  else{.    p = sq
9f600 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9f610 67 2e 6d 2e 78 4d 61 6c 6c 6f 63 28 6e 29 3b 0a  g.m.xMalloc(n);.
9f620 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 20    }.  return p; 
9f630 20 20 20 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49     .}.SQLITE_PRI
9f640 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
9f650 33 50 61 67 65 46 72 65 65 28 76 6f 69 64 20 2a  3PageFree(void *
9f660 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
9f670 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c     if( sqlite3Gl
9f680 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65  obalConfig.pPage
9f690 3d 3d 30 0a 20 20 20 20 20 20 20 20 20 20 20 7c  ==0.           |
9f6a0 7c 20 70 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61  | p<sqlite3Globa
9f6b0 6c 43 6f 6e 66 69 67 2e 70 50 61 67 65 0a 20 20  lConfig.pPage.  
9f6c0 20 20 20 20 20 20 20 20 20 7c 7c 20 70 3e 3d 28           || p>=(
9f6d0 76 6f 69 64 2a 29 6d 65 6d 30 2e 61 50 61 67 65  void*)mem0.aPage
9f6e0 46 72 65 65 20 29 7b 0a 20 20 20 20 20 20 2f 2a  Free ){.      /*
9f6f0 20 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74   In this case, t
9f700 68 65 20 70 61 67 65 20 61 6c 6c 6f 63 61 74 69  he page allocati
9f710 6f 6e 20 77 61 73 20 6f 62 74 61 69 6e 65 64 20  on was obtained 
9f720 66 72 6f 6d 20 61 20 72 65 67 75 6c 61 72 20 0a  from a regular .
9f730 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 20 74 6f        ** call to
9f740 20 73 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74   sqlite3_mem_met
9f750 68 6f 64 73 2e 78 4d 61 6c 6c 6f 63 28 29 20 28  hods.xMalloc() (
9f760 61 20 70 61 67 65 2d 63 61 63 68 65 2d 6d 65 6d  a page-cache-mem
9f770 6f 72 79 20 0a 20 20 20 20 20 20 2a 2a 20 22 6f  ory .      ** "o
9f780 76 65 72 66 6c 6f 77 22 29 2e 20 46 72 65 65 20  verflow"). Free 
9f790 74 68 65 20 62 6c 6f 63 6b 20 77 69 74 68 20 73  the block with s
9f7a0 71 6c 69 74 65 33 5f 6d 65 6d 5f 6d 65 74 68 6f  qlite3_mem_metho
9f7b0 64 73 2e 78 46 72 65 65 28 29 2e 0a 20 20 20 20  ds.xFree()..    
9f7c0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 73    */.      if( s
9f7d0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
9f7e0 69 67 2e 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20  ig.bMemstat ){. 
9f7f0 20 20 20 20 20 20 20 69 6e 74 20 69 53 69 7a 65         int iSize
9f800 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
9f810 53 69 7a 65 28 70 29 3b 0a 20 20 20 20 20 20 20  Size(p);.       
9f820 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
9f830 6e 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29  nter(mem0.mutex)
9f840 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
9f850 33 53 74 61 74 75 73 41 64 64 28 53 51 4c 49 54  3StatusAdd(SQLIT
9f860 45 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43  E_STATUS_PAGECAC
9f870 48 45 5f 4f 56 45 52 46 4c 4f 57 2c 20 2d 69 53  HE_OVERFLOW, -iS
9f880 69 7a 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ize);.        sq
9f890 6c 69 74 65 33 53 74 61 74 75 73 41 64 64 28 53  lite3StatusAdd(S
9f8a0 51 4c 49 54 45 5f 53 54 41 54 55 53 5f 4d 45 4d  QLITE_STATUS_MEM
9f8b0 4f 52 59 5f 55 53 45 44 2c 20 2d 69 53 69 7a 65  ORY_USED, -iSize
9f8c0 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
9f8d0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d  e3GlobalConfig.m
9f8e0 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20  .xFree(p);.     
9f8f0 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78     sqlite3_mutex
9f900 5f 6c 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65  _leave(mem0.mute
9f910 78 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  x);.      }else{
9f920 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
9f930 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78  GlobalConfig.m.x
9f940 46 72 65 65 28 70 29 3b 0a 20 20 20 20 20 20 7d  Free(p);.      }
9f950 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
9f960 20 20 2f 2a 20 54 68 65 20 70 61 67 65 20 61 6c    /* The page al
9f970 6c 6f 63 61 74 69 6f 6e 20 77 61 73 20 61 6c 6c  location was all
9f980 6f 63 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20  ocated from the 
9f990 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
9f9a0 66 69 67 2e 70 50 61 67 65 0a 20 20 20 20 20 20  fig.pPage.      
9f9b0 2a 2a 20 62 75 66 66 65 72 2e 20 49 6e 20 74 68  ** buffer. In th
9f9c0 69 73 20 63 61 73 65 20 61 6c 6c 20 74 68 61 74  is case all that
9f9d0 20 69 73 20 61 64 64 20 74 68 65 20 69 6e 64 65   is add the inde
9f9e0 78 20 6f 66 20 74 68 65 20 70 61 67 65 20 69 6e  x of the page in
9f9f0 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 73 71  .      ** the sq
9fa00 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9fa10 67 2e 70 50 61 67 65 20 61 72 72 61 79 20 74 6f  g.pPage array to
9fa20 20 74 68 65 20 73 65 74 20 6f 66 20 66 72 65 65   the set of free
9fa30 20 69 6e 64 65 78 65 73 20 73 74 6f 72 65 64 0a   indexes stored.
9fa40 20 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20        ** in the 
9fa50 6d 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 5d  mem0.aPageFree[]
9fa60 20 61 72 72 61 79 2e 0a 20 20 20 20 20 20 2a 2f   array..      */
9fa70 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
9fa80 20 20 20 20 69 20 3d 20 28 75 38 20 2a 29 70 20      i = (u8 *)p 
9fa90 2d 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 47  - (u8 *)sqlite3G
9faa0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 50 61 67  lobalConfig.pPag
9fab0 65 3b 0a 20 20 20 20 20 20 69 20 2f 3d 20 73 71  e;.      i /= sq
9fac0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
9fad0 67 2e 73 7a 50 61 67 65 3b 0a 20 20 20 20 20 20  g.szPage;.      
9fae0 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20  assert( i>=0 && 
9faf0 69 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  i<sqlite3GlobalC
9fb00 6f 6e 66 69 67 2e 6e 50 61 67 65 20 29 3b 0a 20  onfig.nPage );. 
9fb10 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
9fb20 65 78 5f 65 6e 74 65 72 28 6d 65 6d 30 2e 6d 75  ex_enter(mem0.mu
9fb30 74 65 78 29 3b 0a 20 20 20 20 20 20 61 73 73 65  tex);.      asse
9fb40 72 74 28 20 6d 65 6d 30 2e 6e 50 61 67 65 46 72  rt( mem0.nPageFr
9fb50 65 65 3c 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  ee<sqlite3Global
9fb60 43 6f 6e 66 69 67 2e 6e 50 61 67 65 20 29 3b 0a  Config.nPage );.
9fb70 20 20 20 20 20 20 6d 65 6d 30 2e 61 50 61 67 65        mem0.aPage
9fb80 46 72 65 65 5b 6d 65 6d 30 2e 6e 50 61 67 65 46  Free[mem0.nPageF
9fb90 72 65 65 2b 2b 5d 20 3d 20 69 3b 0a 20 20 20 20  ree++] = i;.    
9fba0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 41    sqlite3StatusA
9fbb0 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  dd(SQLITE_STATUS
9fbc0 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45 44 2c  _PAGECACHE_USED,
9fbd0 20 2d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   -1);.      sqli
9fbe0 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
9fbf0 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 23 69 66  mem0.mutex);.#if
9fc00 20 21 64 65 66 69 6e 65 64 28 4e 44 45 42 55 47   !defined(NDEBUG
9fc10 29 20 26 26 20 30 0a 20 20 20 20 20 20 2f 2a 20  ) && 0.      /* 
9fc20 41 73 73 65 72 74 20 74 68 61 74 20 61 20 64 75  Assert that a du
9fc30 70 6c 69 63 61 74 65 20 77 61 73 20 6e 6f 74 20  plicate was not 
9fc40 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20 69 6e  just inserted in
9fc50 74 6f 20 61 50 61 67 65 46 72 65 65 5b 5d 2e 20  to aPageFree[]. 
9fc60 2a 2f 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  */.      for(i=0
9fc70 3b 20 69 3c 6d 65 6d 30 2e 6e 50 61 67 65 46 72  ; i<mem0.nPageFr
9fc80 65 65 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ee-1; i++){.    
9fc90 20 20 20 20 61 73 73 65 72 74 28 20 6d 65 6d 30      assert( mem0
9fca0 2e 61 50 61 67 65 46 72 65 65 5b 69 5d 21 3d 6d  .aPageFree[i]!=m
9fcb0 65 6d 30 2e 61 50 61 67 65 46 72 65 65 5b 6d 65  em0.aPageFree[me
9fcc0 6d 30 2e 6e 50 61 67 65 46 72 65 65 2d 31 5d 20  m0.nPageFree-1] 
9fcd0 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
9fce0 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23 65  f.    }.  }.}.#e
9fcf0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 52 55 45  ndif../*.** TRUE
9fd00 20 69 66 20 70 20 69 73 20 61 20 6c 6f 6f 6b 61   if p is a looka
9fd10 73 69 64 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  side memory allo
9fd20 63 61 74 69 6f 6e 20 66 72 6f 6d 20 64 62 0a 2a  cation from db.*
9fd30 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
9fd40 5f 4f 4d 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a  _OMIT_LOOKASIDE.
9fd50 73 74 61 74 69 63 20 69 6e 74 20 69 73 4c 6f 6f  static int isLoo
9fd60 6b 61 73 69 64 65 28 73 71 6c 69 74 65 33 20 2a  kaside(sqlite3 *
9fd70 64 62 2c 20 76 6f 69 64 20 2a 70 29 7b 0a 20 20  db, void *p){.  
9fd80 72 65 74 75 72 6e 20 64 62 20 26 26 20 70 20 26  return db && p &
9fd90 26 20 70 3e 3d 64 62 2d 3e 6c 6f 6f 6b 61 73 69  & p>=db->lookasi
9fda0 64 65 2e 70 53 74 61 72 74 20 26 26 20 70 3c 64  de.pStart && p<d
9fdb0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 45 6e  b->lookaside.pEn
9fdc0 64 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  d;.}.#else.#defi
9fdd0 6e 65 20 69 73 4c 6f 6f 6b 61 73 69 64 65 28 41  ne isLookaside(A
9fde0 2c 42 29 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ,B) 0.#endif../*
9fdf0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 73  .** Return the s
9fe00 69 7a 65 20 6f 66 20 61 20 6d 65 6d 6f 72 79 20  ize of a memory 
9fe10 61 6c 6c 6f 63 61 74 69 6f 6e 20 70 72 65 76 69  allocation previ
9fe20 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64 20 66  ously obtained f
9fe30 72 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 4d 61  rom.** sqlite3Ma
9fe40 6c 6c 6f 63 28 29 20 6f 72 20 73 71 6c 69 74 65  lloc() or sqlite
9fe50 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53  3_malloc()..*/.S
9fe60 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
9fe70 74 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  t sqlite3MallocS
9fe80 69 7a 65 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20  ize(void *p){.  
9fe90 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 47 6c  return sqlite3Gl
9fea0 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69  obalConfig.m.xSi
9feb0 7a 65 28 70 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  ze(p);.}.SQLITE_
9fec0 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
9fed0 74 65 33 44 62 4d 61 6c 6c 6f 63 53 69 7a 65 28  te3DbMallocSize(
9fee0 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
9fef0 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 69 73 4c  d *p){.  if( isL
9ff00 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20  ookaside(db, p) 
9ff10 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64 62  ){.    return db
9ff20 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 3b 0a  ->lookaside.sz;.
9ff30 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
9ff40 75 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  urn sqlite3Globa
9ff50 6c 43 6f 6e 66 69 67 2e 6d 2e 78 53 69 7a 65 28  lConfig.m.xSize(
9ff60 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  p);.  }.}../*.**
9ff70 20 46 72 65 65 20 6d 65 6d 6f 72 79 20 70 72 65   Free memory pre
9ff80 76 69 6f 75 73 6c 79 20 6f 62 74 61 69 6e 65 64  viously obtained
9ff90 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 4d 61 6c   from sqlite3Mal
9ffa0 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  loc()..*/.SQLITE
9ffb0 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
9ffc0 33 5f 66 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  3_free(void *p){
9ffd0 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
9ffe0 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69  turn;.  if( sqli
9fff0 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
a0000 62 4d 65 6d 73 74 61 74 20 29 7b 0a 20 20 20 20  bMemstat ){.    
a0010 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
a0020 74 65 72 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b  ter(mem0.mutex);
a0030 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74  .    sqlite3Stat
a0040 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41  usAdd(SQLITE_STA
a0050 54 55 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c  TUS_MEMORY_USED,
a0060 20 2d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53   -sqlite3MallocS
a0070 69 7a 65 28 70 29 29 3b 0a 20 20 20 20 73 71 6c  ize(p));.    sql
a0080 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a0090 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20 20 20  .m.xFree(p);.   
a00a0 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
a00b0 65 61 76 65 28 6d 65 6d 30 2e 6d 75 74 65 78 29  eave(mem0.mutex)
a00c0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73  ;.  }else{.    s
a00d0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
a00e0 69 67 2e 6d 2e 78 46 72 65 65 28 70 29 3b 0a 20  ig.m.xFree(p);. 
a00f0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65   }.}../*.** Free
a0100 20 6d 65 6d 6f 72 79 20 74 68 61 74 20 6d 69 67   memory that mig
a0110 68 74 20 62 65 20 61 73 73 6f 63 69 61 74 65 64  ht be associated
a0120 20 77 69 74 68 20 61 20 70 61 72 74 69 63 75 6c   with a particul
a0130 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  ar database.** c
a0140 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51  onnection..*/.SQ
a0150 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
a0160 64 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  d sqlite3DbFree(
a0170 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69  sqlite3 *db, voi
a0180 64 20 2a 70 29 7b 0a 20 20 69 66 28 20 69 73 4c  d *p){.  if( isL
a0190 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20  ookaside(db, p) 
a01a0 29 7b 0a 20 20 20 20 4c 6f 6f 6b 61 73 69 64 65  ){.    Lookaside
a01b0 53 6c 6f 74 20 2a 70 42 75 66 20 3d 20 28 4c 6f  Slot *pBuf = (Lo
a01c0 6f 6b 61 73 69 64 65 53 6c 6f 74 2a 29 70 3b 0a  okasideSlot*)p;.
a01d0 20 20 20 20 70 42 75 66 2d 3e 70 4e 65 78 74 20      pBuf->pNext 
a01e0 3d 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  = db->lookaside.
a01f0 70 46 72 65 65 3b 0a 20 20 20 20 64 62 2d 3e 6c  pFree;.    db->l
a0200 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65 20 3d  ookaside.pFree =
a0210 20 70 42 75 66 3b 0a 20 20 20 20 64 62 2d 3e 6c   pBuf;.    db->l
a0220 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 2d 2d 3b  ookaside.nOut--;
a0230 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
a0240 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b 0a 20  lite3_free(p);. 
a0250 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
a0260 67 65 20 74 68 65 20 73 69 7a 65 20 6f 66 20 61  ge the size of a
a0270 6e 20 65 78 69 73 74 69 6e 67 20 6d 65 6d 6f 72  n existing memor
a0280 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 0a 2a 2f 0a  y allocation.*/.
a0290 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
a02a0 6f 69 64 20 2a 73 71 6c 69 74 65 33 52 65 61 6c  oid *sqlite3Real
a02b0 6c 6f 63 28 76 6f 69 64 20 2a 70 4f 6c 64 2c 20  loc(void *pOld, 
a02c0 69 6e 74 20 6e 42 79 74 65 73 29 7b 0a 20 20 69  int nBytes){.  i
a02d0 6e 74 20 6e 4f 6c 64 2c 20 6e 4e 65 77 3b 0a 20  nt nOld, nNew;. 
a02e0 20 76 6f 69 64 20 2a 70 4e 65 77 3b 0a 20 20 69   void *pNew;.  i
a02f0 66 28 20 70 4f 6c 64 3d 3d 30 20 29 7b 0a 20 20  f( pOld==0 ){.  
a0300 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
a0310 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 73 29 3b 0a  Malloc(nBytes);.
a0320 20 20 7d 0a 20 20 69 66 28 20 6e 42 79 74 65 73    }.  if( nBytes
a0330 3c 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  <=0 ){.    sqlit
a0340 65 33 5f 66 72 65 65 28 70 4f 6c 64 29 3b 0a 20  e3_free(pOld);. 
a0350 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
a0360 0a 20 20 6e 4f 6c 64 20 3d 20 73 71 6c 69 74 65  .  nOld = sqlite
a0370 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4f 6c 64  3MallocSize(pOld
a0380 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
a0390 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 4d 65  GlobalConfig.bMe
a03a0 6d 73 74 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  mstat ){.    sql
a03b0 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
a03c0 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
a03d0 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53    sqlite3StatusS
a03e0 65 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53  et(SQLITE_STATUS
a03f0 5f 4d 41 4c 4c 4f 43 5f 53 49 5a 45 2c 20 6e 42  _MALLOC_SIZE, nB
a0400 79 74 65 73 29 3b 0a 20 20 20 20 6e 4e 65 77 20  ytes);.    nNew 
a0410 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
a0420 6f 6e 66 69 67 2e 6d 2e 78 52 6f 75 6e 64 75 70  onfig.m.xRoundup
a0430 28 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66  (nBytes);.    if
a0440 28 20 6e 4f 6c 64 3d 3d 6e 4e 65 77 20 29 7b 0a  ( nOld==nNew ){.
a0450 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 4f 6c        pNew = pOl
a0460 64 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  d;.    }else{.  
a0470 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
a0480 74 61 74 75 73 56 61 6c 75 65 28 53 51 4c 49 54  tatusValue(SQLIT
a0490 45 5f 53 54 41 54 55 53 5f 4d 45 4d 4f 52 59 5f  E_STATUS_MEMORY_
a04a0 55 53 45 44 29 2b 6e 4e 65 77 2d 6e 4f 6c 64 20  USED)+nNew-nOld 
a04b0 3e 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  >= .            
a04c0 6d 65 6d 30 2e 61 6c 61 72 6d 54 68 72 65 73 68  mem0.alarmThresh
a04d0 6f 6c 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73  old ){.        s
a04e0 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41 6c 61 72  qlite3MallocAlar
a04f0 6d 28 6e 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20  m(nNew-nOld);.  
a0500 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77      }.      pNew
a0510 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
a0520 43 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f  Config.m.xReallo
a0530 63 28 70 4f 6c 64 2c 20 6e 4e 65 77 29 3b 0a 20  c(pOld, nNew);. 
a0540 20 20 20 20 20 69 66 28 20 70 4e 65 77 3d 3d 30       if( pNew==0
a0550 20 26 26 20 6d 65 6d 30 2e 61 6c 61 72 6d 43 61   && mem0.alarmCa
a0560 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20 20 20  llback ){.      
a0570 20 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 41    sqlite3MallocA
a0580 6c 61 72 6d 28 6e 42 79 74 65 73 29 3b 0a 20 20  larm(nBytes);.  
a0590 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73 71 6c        pNew = sql
a05a0 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
a05b0 2e 6d 2e 78 52 65 61 6c 6c 6f 63 28 70 4f 6c 64  .m.xRealloc(pOld
a05c0 2c 20 6e 4e 65 77 29 3b 0a 20 20 20 20 20 20 7d  , nNew);.      }
a05d0 0a 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 20  .      if( pNew 
a05e0 29 7b 0a 20 20 20 20 20 20 20 20 6e 4e 65 77 20  ){.        nNew 
a05f0 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 53  = sqlite3MallocS
a0600 69 7a 65 28 70 4e 65 77 29 3b 0a 20 20 20 20 20  ize(pNew);.     
a0610 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75 73     sqlite3Status
a0620 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54 55  Add(SQLITE_STATU
a0630 53 5f 4d 45 4d 4f 52 59 5f 55 53 45 44 2c 20 6e  S_MEMORY_USED, n
a0640 4e 65 77 2d 6e 4f 6c 64 29 3b 0a 20 20 20 20 20  New-nOld);.     
a0650 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
a0660 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
a0670 28 6d 65 6d 30 2e 6d 75 74 65 78 29 3b 0a 20 20  (mem0.mutex);.  
a0680 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77 20  }else{.    pNew 
a0690 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
a06a0 6f 6e 66 69 67 2e 6d 2e 78 52 65 61 6c 6c 6f 63  onfig.m.xRealloc
a06b0 28 70 4f 6c 64 2c 20 6e 42 79 74 65 73 29 3b 0a  (pOld, nBytes);.
a06c0 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 4e 65    }.  return pNe
a06d0 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  w;.}../*.** The 
a06e0 70 75 62 6c 69 63 20 69 6e 74 65 72 66 61 63 65  public interface
a06f0 20 74 6f 20 73 71 6c 69 74 65 33 52 65 61 6c 6c   to sqlite3Reall
a0700 6f 63 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74  oc.  Make sure t
a0710 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79 0a 2a  hat the memory.*
a0720 2a 20 73 75 62 73 79 73 74 65 6d 20 69 73 20 69  * subsystem is i
a0730 6e 69 74 69 61 6c 69 7a 65 64 20 70 72 69 6f 72  nitialized prior
a0740 20 74 6f 20 69 6e 76 6f 6b 69 6e 67 20 73 71 6c   to invoking sql
a0750 69 74 65 52 65 61 6c 6c 6f 63 2e 0a 2a 2f 0a 53  iteRealloc..*/.S
a0760 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
a0770 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
a0780 76 6f 69 64 20 2a 70 4f 6c 64 2c 20 69 6e 74 20  void *pOld, int 
a0790 6e 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  n){.#ifndef SQLI
a07a0 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54  TE_OMIT_AUTOINIT
a07b0 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 69  .  if( sqlite3_i
a07c0 6e 69 74 69 61 6c 69 7a 65 28 29 20 29 20 72 65  nitialize() ) re
a07d0 74 75 72 6e 20 30 3b 0a 23 65 6e 64 69 66 0a 20  turn 0;.#endif. 
a07e0 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 52   return sqlite3R
a07f0 65 61 6c 6c 6f 63 28 70 4f 6c 64 2c 20 6e 29 3b  ealloc(pOld, n);
a0800 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .}.../*.** Alloc
a0810 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d 65 6d  ate and zero mem
a0820 6f 72 79 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f  ory..*/ .SQLITE_
a0830 50 52 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71  PRIVATE void *sq
a0840 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28  lite3MallocZero(
a0850 69 6e 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a  int n){.  void *
a0860 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  p = sqlite3Mallo
a0870 63 28 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b  c(n);.  if( p ){
a0880 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  .    memset(p, 0
a0890 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
a08a0 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn p;.}../*.** A
a08b0 6c 6c 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f  llocate and zero
a08c0 20 6d 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65   memory.  If the
a08d0 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c   allocation fail
a08e0 73 2c 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d  s, make.** the m
a08f0 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
a0900 20 69 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69   in the connecti
a0910 6f 6e 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53  on pointer..*/.S
a0920 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
a0930 69 64 20 2a 73 71 6c 69 74 65 33 44 62 4d 61 6c  id *sqlite3DbMal
a0940 6c 6f 63 5a 65 72 6f 28 73 71 6c 69 74 65 33 20  locZero(sqlite3 
a0950 2a 64 62 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76  *db, int n){.  v
a0960 6f 69 64 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  oid *p = sqlite3
a0970 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
a0980 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  n);.  if( p ){. 
a0990 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
a09a0 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
a09b0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c   p;.}../*.** All
a09c0 6f 63 61 74 65 20 61 6e 64 20 7a 65 72 6f 20 6d  ocate and zero m
a09d0 65 6d 6f 72 79 2e 20 20 49 66 20 74 68 65 20 61  emory.  If the a
a09e0 6c 6c 6f 63 61 74 69 6f 6e 20 66 61 69 6c 73 2c  llocation fails,
a09f0 20 6d 61 6b 65 0a 2a 2a 20 74 68 65 20 6d 61 6c   make.** the mal
a0a00 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 69  locFailed flag i
a0a10 6e 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  n the connection
a0a20 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
a0a30 49 66 20 64 62 21 3d 30 20 61 6e 64 20 64 62 2d  If db!=0 and db-
a0a40 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 69 73  >mallocFailed is
a0a50 20 74 72 75 65 20 28 69 6e 64 69 63 61 74 69 6e   true (indicatin
a0a60 67 20 61 20 70 72 69 6f 72 20 6d 61 6c 6c 6f 63  g a prior malloc
a0a70 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f 6e 20 74  .** failure on t
a0a80 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
a0a90 20 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 74 68 65   connection) the
a0aa0 6e 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 20  n always return 
a0ab0 30 2e 0a 2a 2a 20 48 65 6e 63 65 20 66 6f 72 20  0..** Hence for 
a0ac0 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  a particular dat
a0ad0 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
a0ae0 2c 20 6f 6e 63 65 20 6d 61 6c 6c 6f 63 20 73 74  , once malloc st
a0af0 61 72 74 73 0a 2a 2a 20 66 61 69 6c 69 6e 67 2c  arts.** failing,
a0b00 20 69 74 20 66 61 69 6c 73 20 63 6f 6e 73 69 73   it fails consis
a0b10 74 65 6e 74 6c 79 20 75 6e 74 69 6c 20 6d 61 6c  tently until mal
a0b20 6c 6f 63 46 61 69 6c 65 64 20 69 73 20 72 65 73  locFailed is res
a0b30 65 74 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20 61  et..** This is a
a0b40 6e 20 69 6d 70 6f 72 74 61 6e 74 20 61 73 73 75  n important assu
a0b50 6d 70 74 69 6f 6e 2e 20 20 54 68 65 72 65 20 61  mption.  There a
a0b60 72 65 20 6d 61 6e 79 20 70 6c 61 63 65 73 20 69  re many places i
a0b70 6e 20 74 68 65 0a 2a 2a 20 63 6f 64 65 20 74 68  n the.** code th
a0b80 61 74 20 64 6f 20 74 68 69 6e 67 73 20 6c 69 6b  at do things lik
a0b90 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
a0ba0 20 20 20 20 20 20 69 6e 74 20 2a 61 20 3d 20 28        int *a = (
a0bb0 69 6e 74 2a 29 73 71 6c 69 74 65 33 44 62 4d 61  int*)sqlite3DbMa
a0bc0 6c 6c 6f 63 52 61 77 28 64 62 2c 20 31 30 30 29  llocRaw(db, 100)
a0bd0 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 74  ;.**         int
a0be0 20 2a 62 20 3d 20 28 69 6e 74 2a 29 73 71 6c 69   *b = (int*)sqli
a0bf0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
a0c00 62 2c 20 32 30 30 29 3b 0a 2a 2a 20 20 20 20 20  b, 200);.**     
a0c10 20 20 20 20 69 66 28 20 62 20 29 20 61 5b 31 30      if( b ) a[10
a0c20 5d 20 3d 20 39 3b 0a 2a 2a 0a 2a 2a 20 49 6e 20  ] = 9;.**.** In 
a0c30 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
a0c40 61 20 73 75 62 73 65 71 75 65 6e 74 20 6d 61 6c  a subsequent mal
a0c50 6c 6f 63 20 28 65 78 3a 20 22 62 22 29 20 77 6f  loc (ex: "b") wo
a0c60 72 6b 65 64 2c 20 69 74 20 69 73 20 61 73 73 75  rked, it is assu
a0c70 6d 65 64 0a 2a 2a 20 74 68 61 74 20 61 6c 6c 20  med.** that all 
a0c80 70 72 69 6f 72 20 6d 61 6c 6c 6f 63 73 20 28 65  prior mallocs (e
a0c90 78 3a 20 22 61 22 29 20 77 6f 72 6b 65 64 20 74  x: "a") worked t
a0ca0 6f 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  oo..*/.SQLITE_PR
a0cb0 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
a0cc0 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
a0cd0 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
a0ce0 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 3b 0a 23  n){.  void *p;.#
a0cf0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a0d00 49 54 5f 4c 4f 4f 4b 41 53 49 44 45 0a 20 20 69  IT_LOOKASIDE.  i
a0d10 66 28 20 64 62 20 29 7b 0a 20 20 20 20 4c 6f 6f  f( db ){.    Loo
a0d20 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 42 75 66  kasideSlot *pBuf
a0d30 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61  ;.    if( db->ma
a0d40 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
a0d50 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
a0d60 20 20 7d 0a 20 20 20 20 69 66 28 20 64 62 2d 3e    }.    if( db->
a0d70 6c 6f 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c  lookaside.bEnabl
a0d80 65 64 20 26 26 20 6e 3c 3d 64 62 2d 3e 6c 6f 6f  ed && n<=db->loo
a0d90 6b 61 73 69 64 65 2e 73 7a 0a 20 20 20 20 20 20  kaside.sz.      
a0da0 20 20 20 26 26 20 28 70 42 75 66 20 3d 20 64 62     && (pBuf = db
a0db0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65  ->lookaside.pFre
a0dc0 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 64  e)!=0 ){.      d
a0dd0 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72  b->lookaside.pFr
a0de0 65 65 20 3d 20 70 42 75 66 2d 3e 70 4e 65 78 74  ee = pBuf->pNext
a0df0 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.      db->look
a0e00 61 73 69 64 65 2e 6e 4f 75 74 2b 2b 3b 0a 20 20  aside.nOut++;.  
a0e10 20 20 20 20 69 66 28 20 64 62 2d 3e 6c 6f 6f 6b      if( db->look
a0e20 61 73 69 64 65 2e 6e 4f 75 74 3e 64 62 2d 3e 6c  aside.nOut>db->l
a0e30 6f 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 29  ookaside.mxOut )
a0e40 7b 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 6c 6f  {.        db->lo
a0e50 6f 6b 61 73 69 64 65 2e 6d 78 4f 75 74 20 3d 20  okaside.mxOut = 
a0e60 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f  db->lookaside.nO
a0e70 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ut;.      }.    
a0e80 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a 29    return (void*)
a0e90 70 42 75 66 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  pBuf;.    }.  }.
a0ea0 23 65 6c 73 65 0a 20 20 69 66 28 20 64 62 20 26  #else.  if( db &
a0eb0 26 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  & db->mallocFail
a0ec0 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
a0ed0 20 30 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20   0;.  }.#endif. 
a0ee0 20 70 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c   p = sqlite3Mall
a0ef0 6f 63 28 6e 29 3b 0a 20 20 69 66 28 20 21 70 20  oc(n);.  if( !p 
a0f00 26 26 20 64 62 20 29 7b 0a 20 20 20 20 64 62 2d  && db ){.    db-
a0f10 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
a0f20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
a0f30 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 69  p;.}../*.** Resi
a0f40 7a 65 20 74 68 65 20 62 6c 6f 63 6b 20 6f 66 20  ze the block of 
a0f50 6d 65 6d 6f 72 79 20 70 6f 69 6e 74 65 64 20 74  memory pointed t
a0f60 6f 20 62 79 20 70 20 74 6f 20 6e 20 62 79 74 65  o by p to n byte
a0f70 73 2e 20 49 66 20 74 68 65 0a 2a 2a 20 72 65 73  s. If the.** res
a0f80 69 7a 65 20 66 61 69 6c 73 2c 20 73 65 74 20 74  ize fails, set t
a0f90 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  he mallocFailed 
a0fa0 66 6c 61 67 20 69 6e 20 74 68 65 20 63 6f 6e 6e  flag in the conn
a0fb0 65 63 74 69 6f 6e 20 6f 62 6a 65 63 74 2e 0a 2a  ection object..*
a0fc0 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
a0fd0 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
a0fe0 52 65 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33 20  Realloc(sqlite3 
a0ff0 2a 64 62 2c 20 76 6f 69 64 20 2a 70 2c 20 69 6e  *db, void *p, in
a1000 74 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4e  t n){.  void *pN
a1010 65 77 20 3d 20 30 3b 0a 20 20 69 66 28 20 64 62  ew = 0;.  if( db
a1020 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3d 3d  ->mallocFailed==
a1030 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 3d 3d  0 ){.    if( p==
a1040 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
a1050 6e 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  n sqlite3DbMallo
a1060 63 52 61 77 28 64 62 2c 20 6e 29 3b 0a 20 20 20  cRaw(db, n);.   
a1070 20 7d 0a 20 20 20 20 69 66 28 20 69 73 4c 6f 6f   }.    if( isLoo
a1080 6b 61 73 69 64 65 28 64 62 2c 20 70 29 20 29 7b  kaside(db, p) ){
a1090 0a 20 20 20 20 20 20 69 66 28 20 6e 3c 3d 64 62  .      if( n<=db
a10a0 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 20 29  ->lookaside.sz )
a10b0 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
a10c0 20 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   p;.      }.    
a10d0 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
a10e0 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
a10f0 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4e  n);.      if( pN
a1100 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 6d 65  ew ){.        me
a1110 6d 63 70 79 28 70 4e 65 77 2c 20 70 2c 20 64 62  mcpy(pNew, p, db
a1120 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 73 7a 29 3b  ->lookaside.sz);
a1130 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
a1140 44 62 46 72 65 65 28 64 62 2c 20 70 29 3b 0a 20  DbFree(db, p);. 
a1150 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
a1160 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 73  {.      pNew = s
a1170 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70  qlite3_realloc(p
a1180 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69 66 28 20  , n);.      if( 
a1190 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20 20  !pNew ){.       
a11a0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
a11b0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  d = 1;.      }. 
a11c0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
a11d0 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
a11e0 20 41 74 74 65 6d 70 74 20 74 6f 20 72 65 61 6c   Attempt to real
a11f0 6c 6f 63 61 74 65 20 70 2e 20 20 49 66 20 74 68  locate p.  If th
a1200 65 20 72 65 61 6c 6c 6f 63 61 74 69 6f 6e 20 66  e reallocation f
a1210 61 69 6c 73 2c 20 74 68 65 6e 20 66 72 65 65 20  ails, then free 
a1220 70 0a 2a 2a 20 61 6e 64 20 73 65 74 20 74 68 65  p.** and set the
a1230 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
a1240 61 67 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  ag in the databa
a1250 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
a1260 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
a1270 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 44 62   void *sqlite3Db
a1280 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 73 71  ReallocOrFree(sq
a1290 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20  lite3 *db, void 
a12a0 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 76 6f  *p, int n){.  vo
a12b0 69 64 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65 77  id *pNew;.  pNew
a12c0 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
a12d0 6c 6f 63 28 64 62 2c 20 70 2c 20 6e 29 3b 0a 20  loc(db, p, n);. 
a12e0 20 69 66 28 20 21 70 4e 65 77 20 29 7b 0a 20 20   if( !pNew ){.  
a12f0 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
a1300 64 62 2c 20 70 29 3b 0a 20 20 7d 0a 20 20 72 65  db, p);.  }.  re
a1310 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a  turn pNew;.}../*
a1320 0a 2a 2a 20 4d 61 6b 65 20 61 20 63 6f 70 79 20  .** Make a copy 
a1330 6f 66 20 61 20 73 74 72 69 6e 67 20 69 6e 20 6d  of a string in m
a1340 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66  emory obtained f
a1350 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63  rom sqliteMalloc
a1360 28 29 2e 20 54 68 65 73 65 20 0a 2a 2a 20 66 75  (). These .** fu
a1370 6e 63 74 69 6f 6e 73 20 63 61 6c 6c 20 73 71 6c  nctions call sql
a1380 69 74 65 33 4d 61 6c 6c 6f 63 52 61 77 28 29 20  ite3MallocRaw() 
a1390 64 69 72 65 63 74 6c 79 20 69 6e 73 74 65 61 64  directly instead
a13a0 20 6f 66 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63   of sqliteMalloc
a13b0 28 29 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20 62  (). This.** is b
a13c0 65 63 61 75 73 65 20 77 68 65 6e 20 6d 65 6d 6f  ecause when memo
a13d0 72 79 20 64 65 62 75 67 67 69 6e 67 20 69 73 20  ry debugging is 
a13e0 74 75 72 6e 65 64 20 6f 6e 2c 20 74 68 65 73 65  turned on, these
a13f0 20 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 61   two functions a
a1400 72 65 20 0a 2a 2a 20 63 61 6c 6c 65 64 20 76 69  re .** called vi
a1410 61 20 6d 61 63 72 6f 73 20 74 68 61 74 20 72 65  a macros that re
a1420 63 6f 72 64 20 74 68 65 20 63 75 72 72 65 6e 74  cord the current
a1430 20 66 69 6c 65 20 61 6e 64 20 6c 69 6e 65 20 6e   file and line n
a1440 75 6d 62 65 72 20 69 6e 20 74 68 65 0a 2a 2a 20  umber in the.** 
a1450 54 68 72 65 61 64 44 61 74 61 20 73 74 72 75 63  ThreadData struc
a1460 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
a1470 50 52 49 56 41 54 45 20 63 68 61 72 20 2a 73 71  PRIVATE char *sq
a1480 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 73 71  lite3DbStrDup(sq
a1490 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74  lite3 *db, const
a14a0 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 68 61   char *z){.  cha
a14b0 72 20 2a 7a 4e 65 77 3b 0a 20 20 73 69 7a 65 5f  r *zNew;.  size_
a14c0 74 20 6e 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20  t n;.  if( z==0 
a14d0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
a14e0 0a 20 20 7d 0a 20 20 6e 20 3d 20 73 74 72 6c 65  .  }.  n = strle
a14f0 6e 28 7a 29 2b 31 3b 0a 20 20 61 73 73 65 72 74  n(z)+1;.  assert
a1500 28 20 28 6e 26 30 78 37 66 66 66 66 66 66 66 29  ( (n&0x7fffffff)
a1510 3d 3d 6e 20 29 3b 0a 20 20 7a 4e 65 77 20 3d 20  ==n );.  zNew = 
a1520 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
a1530 61 77 28 64 62 2c 20 28 69 6e 74 29 6e 29 3b 0a  aw(db, (int)n);.
a1540 20 20 69 66 28 20 7a 4e 65 77 20 29 7b 0a 20 20    if( zNew ){.  
a1550 20 20 6d 65 6d 63 70 79 28 7a 4e 65 77 2c 20 7a    memcpy(zNew, z
a1560 2c 20 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  , n);.  }.  retu
a1570 72 6e 20 7a 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54  rn zNew;.}.SQLIT
a1580 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
a1590 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70  sqlite3DbStrNDup
a15a0 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f  (sqlite3 *db, co
a15b0 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
a15c0 20 6e 29 7b 0a 20 20 63 68 61 72 20 2a 7a 4e 65   n){.  char *zNe
a15d0 77 3b 0a 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b  w;.  if( z==0 ){
a15e0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
a15f0 20 7d 0a 20 20 61 73 73 65 72 74 28 20 28 6e 26   }.  assert( (n&
a1600 30 78 37 66 66 66 66 66 66 66 29 3d 3d 6e 20 29  0x7fffffff)==n )
a1610 3b 0a 20 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74  ;.  zNew = sqlit
a1620 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
a1630 2c 20 6e 2b 31 29 3b 0a 20 20 69 66 28 20 7a 4e  , n+1);.  if( zN
a1640 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70 79  ew ){.    memcpy
a1650 28 7a 4e 65 77 2c 20 7a 2c 20 6e 29 3b 0a 20 20  (zNew, z, n);.  
a1660 20 20 7a 4e 65 77 5b 6e 5d 20 3d 20 30 3b 0a 20    zNew[n] = 0;. 
a1670 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 4e 65 77   }.  return zNew
a1680 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
a1690 65 20 61 20 73 74 72 69 6e 67 20 66 72 6f 6d 20  e a string from 
a16a0 74 68 65 20 7a 46 72 6f 6d 61 74 20 61 72 67 75  the zFromat argu
a16b0 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 76 61 5f  ment and the va_
a16c0 6c 69 73 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77  list that follow
a16d0 73 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20  s..** Store the 
a16e0 73 74 72 69 6e 67 20 69 6e 20 6d 65 6d 6f 72 79  string in memory
a16f0 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
a1700 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 20 61 6e  qliteMalloc() an
a1710 64 20 6d 61 6b 65 20 2a 70 7a 0a 2a 2a 20 70 6f  d make *pz.** po
a1720 69 6e 74 20 74 6f 20 74 68 61 74 20 73 74 72 69  int to that stri
a1730 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
a1740 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a1750 65 33 53 65 74 53 74 72 69 6e 67 28 63 68 61 72  e3SetString(char
a1760 20 2a 2a 70 7a 2c 20 73 71 6c 69 74 65 33 20 2a   **pz, sqlite3 *
a1770 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  db, const char *
a1780 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
a1790 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63   va_list ap;.  c
a17a0 68 61 72 20 2a 7a 3b 0a 0a 20 20 76 61 5f 73 74  har *z;..  va_st
a17b0 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29  art(ap, zFormat)
a17c0 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56  ;.  z = sqlite3V
a17d0 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72  MPrintf(db, zFor
a17e0 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
a17f0 6e 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65  nd(ap);.  sqlite
a1800 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 29  3DbFree(db, *pz)
a1810 3b 0a 20 20 2a 70 7a 20 3d 20 7a 3b 0a 7d 0a 0a  ;.  *pz = z;.}..
a1820 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
a1830 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 63 61 6c  tion must be cal
a1840 6c 65 64 20 62 65 66 6f 72 65 20 65 78 69 74 69  led before exiti
a1850 6e 67 20 61 6e 79 20 41 50 49 20 66 75 6e 63 74  ng any API funct
a1860 69 6f 6e 20 28 69 2e 65 2e 20 0a 2a 2a 20 72 65  ion (i.e. .** re
a1870 74 75 72 6e 69 6e 67 20 63 6f 6e 74 72 6f 6c 20  turning control 
a1880 74 6f 20 74 68 65 20 75 73 65 72 29 20 74 68 61  to the user) tha
a1890 74 20 68 61 73 20 63 61 6c 6c 65 64 20 73 71 6c  t has called sql
a18a0 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 6f 72 0a 2a  ite3_malloc or.*
a18b0 2a 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  * sqlite3_reallo
a18c0 63 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74  c..**.** The ret
a18d0 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73 20 6e  urned value is n
a18e0 6f 72 6d 61 6c 6c 79 20 61 20 63 6f 70 79 20 6f  ormally a copy o
a18f0 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
a1900 75 6d 65 6e 74 20 74 6f 20 74 68 69 73 0a 2a 2a  ument to this.**
a1910 20 66 75 6e 63 74 69 6f 6e 2e 20 48 6f 77 65 76   function. Howev
a1920 65 72 2c 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28  er, if a malloc(
a1930 29 20 66 61 69 6c 75 72 65 20 68 61 73 20 6f 63  ) failure has oc
a1940 63 75 72 65 64 20 73 69 6e 63 65 20 74 68 65 20  cured since the 
a1950 70 72 65 76 69 6f 75 73 0a 2a 2a 20 69 6e 76 6f  previous.** invo
a1960 63 61 74 69 6f 6e 20 53 51 4c 49 54 45 5f 4e 4f  cation SQLITE_NO
a1970 4d 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20  MEM is returned 
a1980 69 6e 73 74 65 61 64 2e 20 0a 2a 2a 0a 2a 2a 20  instead. .**.** 
a1990 49 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67  If the first arg
a19a0 75 6d 65 6e 74 2c 20 64 62 2c 20 69 73 20 6e 6f  ument, db, is no
a19b0 74 20 4e 55 4c 4c 20 61 6e 64 20 61 20 6d 61 6c  t NULL and a mal
a19c0 6c 6f 63 28 29 20 65 72 72 6f 72 20 68 61 73 20  loc() error has 
a19d0 6f 63 63 75 72 65 64 2c 0a 2a 2a 20 74 68 65 6e  occured,.** then
a19e0 20 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20   the connection 
a19f0 65 72 72 6f 72 2d 63 6f 64 65 20 28 74 68 65 20  error-code (the 
a1a00 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62  value returned b
a1a10 79 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f 64  y sqlite3_errcod
a1a20 65 28 29 29 0a 2a 2a 20 69 73 20 73 65 74 20 74  e()).** is set t
a1a30 6f 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 2e 0a  o SQLITE_NOMEM..
a1a40 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
a1a50 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 70 69  E int sqlite3Api
a1a60 45 78 69 74 28 73 71 6c 69 74 65 33 2a 20 64 62  Exit(sqlite3* db
a1a70 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 2f 2a 20  , int rc){.  /* 
a1a80 49 66 20 74 68 65 20 64 62 20 68 61 6e 64 6c 65  If the db handle
a1a90 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68   is not NULL, th
a1aa0 65 6e 20 77 65 20 6d 75 73 74 20 68 6f 6c 64 20  en we must hold 
a1ab0 74 68 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 68  the connection h
a1ac0 61 6e 64 6c 65 0a 20 20 2a 2a 20 6d 75 74 65 78  andle.  ** mutex
a1ad0 20 68 65 72 65 2e 20 4f 74 68 65 72 77 69 73 65   here. Otherwise
a1ae0 20 74 68 65 20 72 65 61 64 20 28 61 6e 64 20 70   the read (and p
a1af0 6f 73 73 69 62 6c 65 20 77 72 69 74 65 29 20 6f  ossible write) o
a1b00 66 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  f db->mallocFail
a1b10 65 64 20 0a 20 20 2a 2a 20 69 73 20 75 6e 73 61  ed .  ** is unsa
a1b20 66 65 2c 20 61 73 20 69 73 20 74 68 65 20 63 61  fe, as is the ca
a1b30 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 45 72 72  ll to sqlite3Err
a1b40 6f 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  or()..  */.  ass
a1b50 65 72 74 28 20 21 64 62 20 7c 7c 20 73 71 6c 69  ert( !db || sqli
a1b60 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
a1b70 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69  b->mutex) );.  i
a1b80 66 28 20 64 62 20 26 26 20 28 64 62 2d 3e 6d 61  f( db && (db->ma
a1b90 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 72 63  llocFailed || rc
a1ba0 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4e  ==SQLITE_IOERR_N
a1bb0 4f 4d 45 4d 29 20 29 7b 0a 20 20 20 20 73 71 6c  OMEM) ){.    sql
a1bc0 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
a1bd0 4c 49 54 45 5f 4e 4f 4d 45 4d 2c 20 30 29 3b 0a  LITE_NOMEM, 0);.
a1be0 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
a1bf0 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20 20 72 63  iled = 0;.    rc
a1c00 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
a1c10 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
a1c20 20 26 20 28 64 62 20 3f 20 64 62 2d 3e 65 72 72   & (db ? db->err
a1c30 4d 61 73 6b 20 3a 20 30 78 66 66 29 3b 0a 7d 0a  Mask : 0xff);.}.
a1c40 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a1c50 20 45 6e 64 20 6f 66 20 6d 61 6c 6c 6f 63 2e 63   End of malloc.c
a1c60 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
a1c70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a1c90 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
a1ca0 20 42 65 67 69 6e 20 66 69 6c 65 20 70 72 69 6e   Begin file prin
a1cb0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tf.c ***********
a1cc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
a1ce0 0a 2f 2a 0a 2a 2a 20 54 68 65 20 22 70 72 69 6e  ./*.** The "prin
a1cf0 74 66 22 20 63 6f 64 65 20 74 68 61 74 20 66 6f  tf" code that fo
a1d00 6c 6c 6f 77 73 20 64 61 74 65 73 20 66 72 6f 6d  llows dates from
a1d10 20 74 68 65 20 31 39 38 30 27 73 2e 20 20 49 74   the 1980's.  It
a1d20 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 70 75   is in.** the pu
a1d30 62 6c 69 63 20 64 6f 6d 61 69 6e 2e 20 20 54 68  blic domain.  Th
a1d40 65 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6d 6d 65  e original comme
a1d50 6e 74 73 20 61 72 65 20 69 6e 63 6c 75 64 65 64  nts are included
a1d60 20 68 65 72 65 20 66 6f 72 0a 2a 2a 20 63 6f 6d   here for.** com
a1d70 70 6c 65 74 65 6e 65 73 73 2e 20 20 54 68 65 79  pleteness.  They
a1d80 20 61 72 65 20 76 65 72 79 20 6f 75 74 2d 6f 66   are very out-of
a1d90 2d 64 61 74 65 20 62 75 74 20 6d 69 67 68 74 20  -date but might 
a1da0 62 65 20 75 73 65 66 75 6c 20 61 73 0a 2a 2a 20  be useful as.** 
a1db0 61 6e 20 68 69 73 74 6f 72 69 63 61 6c 20 72 65  an historical re
a1dc0 66 65 72 65 6e 63 65 2e 20 20 4d 6f 73 74 20 6f  ference.  Most o
a1dd0 66 20 74 68 65 20 22 65 6e 68 61 6e 63 65 6d 65  f the "enhanceme
a1de0 6e 74 73 22 20 68 61 76 65 20 62 65 65 6e 20 62  nts" have been b
a1df0 61 63 6b 65 64 0a 2a 2a 20 6f 75 74 20 73 6f 20  acked.** out so 
a1e00 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
a1e10 6e 61 6c 69 74 79 20 69 73 20 6e 6f 77 20 74 68  nality is now th
a1e20 65 20 73 61 6d 65 20 61 73 20 73 74 61 6e 64 61  e same as standa
a1e30 72 64 20 70 72 69 6e 74 66 28 29 2e 0a 2a 2a 0a  rd printf()..**.
a1e40 2a 2a 20 24 49 64 3a 20 70 72 69 6e 74 66 2e 63  ** $Id: printf.c
a1e50 2c 76 20 31 2e 39 36 20 32 30 30 38 2f 31 31 2f  ,v 1.96 2008/11/
a1e60 32 30 20 31 38 3a 32 30 3a 32 38 20 64 72 68 20  20 18:20:28 drh 
a1e70 45 78 70 20 24 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  Exp $.**.*******
a1e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a1ec0 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ***.**.** The fo
a1ed0 6c 6c 6f 77 69 6e 67 20 6d 6f 64 75 6c 65 73 20  llowing modules 
a1ee0 69 73 20 61 6e 20 65 6e 68 61 6e 63 65 64 20 72  is an enhanced r
a1ef0 65 70 6c 61 63 65 6d 65 6e 74 20 66 6f 72 20 74  eplacement for t
a1f00 68 65 20 22 70 72 69 6e 74 66 22 20 73 75 62 72  he "printf" subr
a1f10 6f 75 74 69 6e 65 73 0a 2a 2a 20 66 6f 75 6e 64  outines.** found
a1f20 20 69 6e 20 74 68 65 20 73 74 61 6e 64 61 72 64   in the standard
a1f30 20 43 20 6c 69 62 72 61 72 79 2e 20 20 54 68 65   C library.  The
a1f40 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e 68 61 6e   following enhan
a1f50 63 65 6d 65 6e 74 73 20 61 72 65 0a 2a 2a 20 73  cements are.** s
a1f60 75 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20  upported:.**.** 
a1f70 20 20 20 20 20 2b 20 20 41 64 64 69 74 69 6f 6e       +  Addition
a1f80 61 6c 20 66 75 6e 63 74 69 6f 6e 73 2e 20 20 54  al functions.  T
a1f90 68 65 20 73 74 61 6e 64 61 72 64 20 73 65 74 20  he standard set 
a1fa0 6f 66 20 22 70 72 69 6e 74 66 22 20 66 75 6e 63  of "printf" func
a1fb0 74 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 20 20 20  tions.**        
a1fc0 20 69 6e 63 6c 75 64 65 73 20 70 72 69 6e 74 66   includes printf
a1fd0 2c 20 66 70 72 69 6e 74 66 2c 20 73 70 72 69 6e  , fprintf, sprin
a1fe0 74 66 2c 20 76 70 72 69 6e 74 66 2c 20 76 66 70  tf, vprintf, vfp
a1ff0 72 69 6e 74 66 2c 20 61 6e 64 0a 2a 2a 20 20 20  rintf, and.**   
a2000 20 20 20 20 20 20 76 73 70 72 69 6e 74 66 2e 20        vsprintf. 
a2010 20 54 68 69 73 20 6d 6f 64 75 6c 65 20 61 64 64   This module add
a2020 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  s the following:
a2030 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  .**.**          
a2040 20 2a 20 20 73 6e 70 72 69 6e 74 66 20 2d 2d 20   *  snprintf -- 
a2050 57 6f 72 6b 73 20 6c 69 6b 65 20 73 70 72 69 6e  Works like sprin
a2060 74 66 2c 20 62 75 74 20 68 61 73 20 61 6e 20 65  tf, but has an e
a2070 78 74 72 61 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  xtra argument.**
a2080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a2090 20 20 20 20 20 20 20 20 20 20 77 68 69 63 68 20            which 
a20a0 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
a20b0 68 65 20 62 75 66 66 65 72 20 77 72 69 74 74 65  he buffer writte
a20c0 6e 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  n to..**.**     
a20d0 20 20 20 20 20 20 2a 20 20 6d 70 72 69 6e 74 66        *  mprintf
a20e0 20 2d 2d 20 20 53 69 6d 69 6c 61 72 20 74 6f 20   --  Similar to 
a20f0 73 70 72 69 6e 74 66 2e 20 20 57 72 69 74 65 73  sprintf.  Writes
a2100 20 6f 75 74 70 75 74 20 74 6f 20 6d 65 6d 6f 72   output to memor
a2110 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  y.**            
a2120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 62                ob
a2130 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c  tained from mall
a2140 6f 63 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  oc..**.**       
a2150 20 20 20 20 2a 20 20 78 70 72 69 6e 74 66 20 2d      *  xprintf -
a2160 2d 20 20 43 61 6c 6c 73 20 61 20 66 75 6e 63 74  -  Calls a funct
a2170 69 6f 6e 20 74 6f 20 64 69 73 70 6f 73 65 20 6f  ion to dispose o
a2180 66 20 6f 75 74 70 75 74 2e 0a 2a 2a 0a 2a 2a 20  f output..**.** 
a2190 20 20 20 20 20 20 20 20 20 20 2a 20 20 6e 70 72            *  npr
a21a0 69 6e 74 66 20 2d 2d 20 20 4e 6f 20 6f 75 74 70  intf --  No outp
a21b0 75 74 2c 20 62 75 74 20 72 65 74 75 72 6e 73 20  ut, but returns 
a21c0 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
a21d0 61 72 61 63 74 65 72 73 0a 2a 2a 20 20 20 20 20  aracters.**     
a21e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a21f0 20 20 20 20 20 74 68 61 74 20 77 6f 75 6c 64 20       that would 
a2200 68 61 76 65 20 62 65 65 6e 20 6f 75 74 70 75 74  have been output
a2210 20 62 79 20 70 72 69 6e 74 66 2e 0a 2a 2a 0a 2a   by printf..**.*
a2220 2a 20 20 20 20 20 20 20 20 20 20 20 2a 20 20 41  *           *  A
a2230 20 76 2d 20 76 65 72 73 69 6f 6e 20 28 65 78 3a   v- version (ex:
a2240 20 76 73 6e 70 72 69 6e 74 66 29 20 6f 66 20 65   vsnprintf) of e
a2250 76 65 72 79 20 66 75 6e 63 74 69 6f 6e 20 69 73  very function is
a2260 20 61 6c 73 6f 0a 2a 2a 20 20 20 20 20 20 20 20   also.**        
a2270 20 20 20 20 20 20 73 75 70 70 6c 69 65 64 2e 0a        supplied..
a2280 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20 20 41 20  **.**      +  A 
a2290 66 65 77 20 65 78 74 65 6e 73 69 6f 6e 73 20 74  few extensions t
a22a0 6f 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67  o the formatting
a22b0 20 6e 6f 74 61 74 69 6f 6e 20 61 72 65 20 73 75   notation are su
a22c0 70 70 6f 72 74 65 64 3a 0a 2a 2a 0a 2a 2a 20 20  pported:.**.**  
a22d0 20 20 20 20 20 20 20 20 20 2a 20 20 54 68 65 20           *  The 
a22e0 22 3d 22 20 66 6c 61 67 20 28 73 69 6d 69 6c 61  "=" flag (simila
a22f0 72 20 74 6f 20 22 2d 22 29 20 63 61 75 73 65 73  r to "-") causes
a2300 20 74 68 65 20 6f 75 74 70 75 74 20 74 6f 20 62   the output to b
a2310 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  e.**            
a2320 20 20 62 65 20 63 65 6e 74 65 72 65 64 20 69 6e    be centered in
a2330 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65   the appropriate
a2340 6c 79 20 73 69 7a 65 64 20 66 69 65 6c 64 2e 0a  ly sized field..
a2350 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  **.**           
a2360 2a 20 20 54 68 65 20 25 62 20 66 69 65 6c 64 20  *  The %b field 
a2370 6f 75 74 70 75 74 73 20 61 6e 20 69 6e 74 65 67  outputs an integ
a2380 65 72 20 69 6e 20 62 69 6e 61 72 79 20 6e 6f 74  er in binary not
a2390 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ation..**.**    
a23a0 20 20 20 20 20 20 20 2a 20 20 54 68 65 20 25 63         *  The %c
a23b0 20 66 69 65 6c 64 20 6e 6f 77 20 61 63 63 65 70   field now accep
a23c0 74 73 20 61 20 70 72 65 63 69 73 69 6f 6e 2e 20  ts a precision. 
a23d0 20 54 68 65 20 63 68 61 72 61 63 74 65 72 20 6f   The character o
a23e0 75 74 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20  utput.**        
a23f0 20 20 20 20 20 20 69 73 20 72 65 70 65 61 74 65        is repeate
a2400 64 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20  d by the number 
a2410 6f 66 20 74 69 6d 65 73 20 74 68 65 20 70 72 65  of times the pre
a2420 63 69 73 69 6f 6e 20 73 70 65 63 69 66 69 65 73  cision specifies
a2430 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  ..**.**         
a2440 20 20 2a 20 20 54 68 65 20 25 27 20 66 69 65 6c    *  The %' fiel
a2450 64 20 77 6f 72 6b 73 20 6c 69 6b 65 20 25 63 2c  d works like %c,
a2460 20 62 75 74 20 74 61 6b 65 73 20 61 73 20 69 74   but takes as it
a2470 73 20 63 68 61 72 61 63 74 65 72 20 74 68 65 0a  s character the.
a2480 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a2490 6e 65 78 74 20 63 68 61 72 61 63 74 65 72 20 6f  next character o
a24a0 66 20 74 68 65 20 66 6f 72 6d 61 74 20 73 74 72  f the format str
a24b0 69 6e 67 2c 20 69 6e 73 74 65 61 64 20 6f 66 20  ing, instead of 
a24c0 74 68 65 20 6e 65 78 74 0a 2a 2a 20 20 20 20 20  the next.**     
a24d0 20 20 20 20 20 20 20 20 20 61 72 67 75 6d 65 6e           argumen
a24e0 74 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c  t.  For example,
a24f0 20 20 70 72 69 6e 74 66 28 22 25 2e 37 38 27 2d    printf("%.78'-
a2500 22 29 20 20 70 72 69 6e 74 73 20 37 38 20 6d 69  ")  prints 78 mi
a2510 6e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nus.**          
a2520 20 20 20 20 73 69 67 6e 73 2c 20 74 68 65 20 73      signs, the s
a2530 61 6d 65 20 61 73 20 20 70 72 69 6e 74 66 28 22  ame as  printf("
a2540 25 2e 37 38 63 22 2c 27 2d 27 29 2e 0a 2a 2a 0a  %.78c",'-')..**.
a2550 2a 2a 20 20 20 20 20 20 2b 20 20 57 68 65 6e 20  **      +  When 
a2560 63 6f 6d 70 69 6c 65 64 20 75 73 69 6e 67 20 47  compiled using G
a2570 43 43 20 6f 6e 20 61 20 53 50 41 52 43 2c 20 74  CC on a SPARC, t
a2580 68 69 73 20 76 65 72 73 69 6f 6e 20 6f 66 20 70  his version of p
a2590 72 69 6e 74 66 20 69 73 0a 2a 2a 20 20 20 20 20  rintf is.**     
a25a0 20 20 20 20 66 61 73 74 65 72 20 74 68 61 6e 20      faster than 
a25b0 74 68 65 20 6c 69 62 72 61 72 79 20 70 72 69 6e  the library prin
a25c0 74 66 20 66 6f 72 20 53 55 4e 20 4f 53 20 34 2e  tf for SUN OS 4.
a25d0 31 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 2b 20  1..**.**      + 
a25e0 20 41 6c 6c 20 66 75 6e 63 74 69 6f 6e 73 20 61   All functions a
a25f0 72 65 20 66 75 6c 6c 79 20 72 65 65 6e 74 72 61  re fully reentra
a2600 6e 74 2e 0a 2a 2a 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a  nt..**.*/../*.**
a2610 20 43 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65   Conversion type
a2620 73 20 66 61 6c 6c 20 69 6e 74 6f 20 76 61 72 69  s fall into vari
a2630 6f 75 73 20 63 61 74 65 67 6f 72 69 65 73 20 61  ous categories a
a2640 73 20 64 65 66 69 6e 65 64 20 62 79 20 74 68 65  s defined by the
a2650 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 6e  .** following en
a2660 75 6d 65 72 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64  umeration..*/.#d
a2670 65 66 69 6e 65 20 65 74 52 41 44 49 58 20 20 20  efine etRADIX   
a2680 20 20 20 20 31 20 2f 2a 20 49 6e 74 65 67 65 72      1 /* Integer
a2690 20 74 79 70 65 73 2e 20 20 25 64 2c 20 25 78 2c   types.  %d, %x,
a26a0 20 25 6f 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74   %o, and so fort
a26b0 68 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 46  h */.#define etF
a26c0 4c 4f 41 54 20 20 20 20 20 20 20 32 20 2f 2a 20  LOAT       2 /* 
a26d0 46 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 2e 20  Floating point. 
a26e0 20 25 66 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %f */.#define e
a26f0 74 45 58 50 20 20 20 20 20 20 20 20 20 33 20 2f  tEXP         3 /
a2700 2a 20 45 78 70 6f 6e 65 6e 74 69 6f 6e 61 6c 20  * Exponentional 
a2710 6e 6f 74 61 74 69 6f 6e 2e 20 25 65 20 61 6e 64  notation. %e and
a2720 20 25 45 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %E */.#define e
a2730 74 47 45 4e 45 52 49 43 20 20 20 20 20 34 20 2f  tGENERIC     4 /
a2740 2a 20 46 6c 6f 61 74 69 6e 67 20 6f 72 20 65 78  * Floating or ex
a2750 70 6f 6e 65 6e 74 69 61 6c 2c 20 64 65 70 65 6e  ponential, depen
a2760 64 69 6e 67 20 6f 6e 20 65 78 70 6f 6e 65 6e 74  ding on exponent
a2770 2e 20 25 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20  . %g */.#define 
a2780 65 74 53 49 5a 45 20 20 20 20 20 20 20 20 35 20  etSIZE        5 
a2790 2f 2a 20 52 65 74 75 72 6e 20 6e 75 6d 62 65 72  /* Return number
a27a0 20 6f 66 20 63 68 61 72 61 63 74 65 72 73 20 70   of characters p
a27b0 72 6f 63 65 73 73 65 64 20 73 6f 20 66 61 72 2e  rocessed so far.
a27c0 20 25 6e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65   %n */.#define e
a27d0 74 53 54 52 49 4e 47 20 20 20 20 20 20 36 20 2f  tSTRING      6 /
a27e0 2a 20 53 74 72 69 6e 67 73 2e 20 25 73 20 2a 2f  * Strings. %s */
a27f0 0a 23 64 65 66 69 6e 65 20 65 74 44 59 4e 53 54  .#define etDYNST
a2800 52 49 4e 47 20 20 20 37 20 2f 2a 20 44 79 6e 61  RING   7 /* Dyna
a2810 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65  mically allocate
a2820 64 20 73 74 72 69 6e 67 73 2e 20 25 7a 20 2a 2f  d strings. %z */
a2830 0a 23 64 65 66 69 6e 65 20 65 74 50 45 52 43 45  .#define etPERCE
a2840 4e 54 20 20 20 20 20 38 20 2f 2a 20 50 65 72 63  NT     8 /* Perc
a2850 65 6e 74 20 73 79 6d 62 6f 6c 2e 20 25 25 20 2a  ent symbol. %% *
a2860 2f 0a 23 64 65 66 69 6e 65 20 65 74 43 48 41 52  /.#define etCHAR
a2870 58 20 20 20 20 20 20 20 39 20 2f 2a 20 43 68 61  X       9 /* Cha
a2880 72 61 63 74 65 72 73 2e 20 25 63 20 2a 2f 0a 2f  racters. %c */./
a2890 2a 20 54 68 65 20 72 65 73 74 20 61 72 65 20 65  * The rest are e
a28a0 78 74 65 6e 73 69 6f 6e 73 2c 20 6e 6f 74 20 6e  xtensions, not n
a28b0 6f 72 6d 61 6c 6c 79 20 66 6f 75 6e 64 20 69 6e  ormally found in
a28c0 20 70 72 69 6e 74 66 28 29 20 2a 2f 0a 23 64 65   printf() */.#de
a28d0 66 69 6e 65 20 65 74 53 51 4c 45 53 43 41 50 45  fine etSQLESCAPE
a28e0 20 20 31 30 20 2f 2a 20 53 74 72 69 6e 67 73 20    10 /* Strings 
a28f0 77 69 74 68 20 27 5c 27 27 20 64 6f 75 62 6c 65  with '\'' double
a2900 64 2e 20 20 25 71 20 2a 2f 0a 23 64 65 66 69 6e  d.  %q */.#defin
a2910 65 20 65 74 53 51 4c 45 53 43 41 50 45 32 20 31  e etSQLESCAPE2 1
a2920 31 20 2f 2a 20 53 74 72 69 6e 67 73 20 77 69 74  1 /* Strings wit
a2930 68 20 27 5c 27 27 20 64 6f 75 62 6c 65 64 20 61  h '\'' doubled a
a2940 6e 64 20 65 6e 63 6c 6f 73 65 64 20 69 6e 20 27  nd enclosed in '
a2950 27 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ',.             
a2960 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55 4c               NUL
a2970 4c 20 70 6f 69 6e 74 65 72 73 20 72 65 70 6c 61  L pointers repla
a2980 63 65 64 20 62 79 20 53 51 4c 20 4e 55 4c 4c 2e  ced by SQL NULL.
a2990 20 20 25 51 20 2a 2f 0a 23 64 65 66 69 6e 65 20    %Q */.#define 
a29a0 65 74 54 4f 4b 45 4e 20 20 20 20 20 20 31 32 20  etTOKEN      12 
a29b0 2f 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  /* a pointer to 
a29c0 61 20 54 6f 6b 65 6e 20 73 74 72 75 63 74 75 72  a Token structur
a29d0 65 20 2a 2f 0a 23 64 65 66 69 6e 65 20 65 74 53  e */.#define etS
a29e0 52 43 4c 49 53 54 20 20 20 20 31 33 20 2f 2a 20  RCLIST    13 /* 
a29f0 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 53  a pointer to a S
a2a00 72 63 4c 69 73 74 20 2a 2f 0a 23 64 65 66 69 6e  rcList */.#defin
a2a10 65 20 65 74 50 4f 49 4e 54 45 52 20 20 20 20 31  e etPOINTER    1
a2a20 34 20 2f 2a 20 54 68 65 20 25 70 20 63 6f 6e 76  4 /* The %p conv
a2a30 65 72 73 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e  ersion */.#defin
a2a40 65 20 65 74 53 51 4c 45 53 43 41 50 45 33 20 31  e etSQLESCAPE3 1
a2a50 35 20 2f 2a 20 25 77 20 2d 3e 20 53 74 72 69 6e  5 /* %w -> Strin
a2a60 67 73 20 77 69 74 68 20 27 5c 22 27 20 64 6f 75  gs with '\"' dou
a2a70 62 6c 65 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20  bled */.#define 
a2a80 65 74 4f 52 44 49 4e 41 4c 20 20 20 20 31 36 20  etORDINAL    16 
a2a90 2f 2a 20 25 72 20 2d 3e 20 31 73 74 2c 20 32 6e  /* %r -> 1st, 2n
a2aa0 64 2c 20 33 72 64 2c 20 34 74 68 2c 20 65 74 63  d, 3rd, 4th, etc
a2ab0 2e 20 20 45 6e 67 6c 69 73 68 20 6f 6e 6c 79 20  .  English only 
a2ac0 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 22 65  */.../*.** An "e
a2ad0 74 42 79 74 65 22 20 69 73 20 61 6e 20 38 2d 62  tByte" is an 8-b
a2ae0 69 74 20 75 6e 73 69 67 6e 65 64 20 76 61 6c 75  it unsigned valu
a2af0 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 75 6e  e..*/.typedef un
a2b00 73 69 67 6e 65 64 20 63 68 61 72 20 65 74 42 79  signed char etBy
a2b10 74 65 3b 0a 0a 2f 2a 0a 2a 2a 20 45 61 63 68 20  te;../*.** Each 
a2b20 62 75 69 6c 74 69 6e 20 63 6f 6e 76 65 72 73 69  builtin conversi
a2b30 6f 6e 20 63 68 61 72 61 63 74 65 72 20 28 65 78  on character (ex
a2b40 3a 20 74 68 65 20 27 64 27 20 69 6e 20 22 25 64  : the 'd' in "%d
a2b50 22 29 20 69 73 20 64 65 73 63 72 69 62 65 64 0a  ") is described.
a2b60 2a 2a 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63  ** by an instanc
a2b70 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
a2b80 6e 67 20 73 74 72 75 63 74 75 72 65 0a 2a 2f 0a  ng structure.*/.
a2b90 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 65  typedef struct e
a2ba0 74 5f 69 6e 66 6f 20 7b 20 20 20 2f 2a 20 49 6e  t_info {   /* In
a2bb0 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
a2bc0 65 61 63 68 20 66 6f 72 6d 61 74 20 66 69 65 6c  each format fiel
a2bd0 64 20 2a 2f 0a 20 20 63 68 61 72 20 66 6d 74 74  d */.  char fmtt
a2be0 79 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ype;            
a2bf0 2f 2a 20 54 68 65 20 66 6f 72 6d 61 74 20 66 69  /* The format fi
a2c00 65 6c 64 20 63 6f 64 65 20 6c 65 74 74 65 72 20  eld code letter 
a2c10 2a 2f 0a 20 20 65 74 42 79 74 65 20 62 61 73 65  */.  etByte base
a2c20 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
a2c30 20 54 68 65 20 62 61 73 65 20 66 6f 72 20 72 61   The base for ra
a2c40 64 69 78 20 63 6f 6e 76 65 72 73 69 6f 6e 20 2a  dix conversion *
a2c50 2f 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 73  /.  etByte flags
a2c60 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
a2c70 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 6f 66 20 46  One or more of F
a2c80 4c 41 47 5f 20 63 6f 6e 73 74 61 6e 74 73 20 62  LAG_ constants b
a2c90 65 6c 6f 77 20 2a 2f 0a 20 20 65 74 42 79 74 65  elow */.  etByte
a2ca0 20 74 79 70 65 3b 20 20 20 20 20 20 20 20 20 20   type;          
a2cb0 20 20 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e     /* Conversion
a2cc0 20 70 61 72 61 64 69 67 6d 20 2a 2f 0a 20 20 65   paradigm */.  e
a2cd0 74 42 79 74 65 20 63 68 61 72 73 65 74 3b 20 20  tByte charset;  
a2ce0 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
a2cf0 74 20 69 6e 74 6f 20 61 44 69 67 69 74 73 5b 5d  t into aDigits[]
a2d00 20 6f 66 20 74 68 65 20 64 69 67 69 74 73 20 73   of the digits s
a2d10 74 72 69 6e 67 20 2a 2f 0a 20 20 65 74 42 79 74  tring */.  etByt
a2d20 65 20 70 72 65 66 69 78 3b 20 20 20 20 20 20 20  e prefix;       
a2d30 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e      /* Offset in
a2d40 74 6f 20 61 50 72 65 66 69 78 5b 5d 20 6f 66 20  to aPrefix[] of 
a2d50 74 68 65 20 70 72 65 66 69 78 20 73 74 72 69 6e  the prefix strin
a2d60 67 20 2a 2f 0a 7d 20 65 74 5f 69 6e 66 6f 3b 0a  g */.} et_info;.
a2d70 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76  ./*.** Allowed v
a2d80 61 6c 75 65 73 20 66 6f 72 20 65 74 5f 69 6e 66  alues for et_inf
a2d90 6f 2e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69  o.flags.*/.#defi
a2da0 6e 65 20 46 4c 41 47 5f 53 49 47 4e 45 44 20 20  ne FLAG_SIGNED  
a2db0 31 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66  1     /* True if
a2dc0 20 74 68 65 20 76 61 6c 75 65 20 74 6f 20 63 6f   the value to co
a2dd0 6e 76 65 72 74 20 69 73 20 73 69 67 6e 65 64 20  nvert is signed 
a2de0 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f  */.#define FLAG_
a2df0 49 4e 54 45 52 4e 20 20 32 20 20 20 20 20 2f 2a  INTERN  2     /*
a2e00 20 54 72 75 65 20 69 66 20 66 6f 72 20 69 6e 74   True if for int
a2e10 65 72 6e 61 6c 20 75 73 65 20 6f 6e 6c 79 20 2a  ernal use only *
a2e20 2f 0a 23 64 65 66 69 6e 65 20 46 4c 41 47 5f 53  /.#define FLAG_S
a2e30 54 52 49 4e 47 20 20 34 20 20 20 20 20 2f 2a 20  TRING  4     /* 
a2e40 41 6c 6c 6f 77 20 69 6e 66 69 6e 69 74 79 20 70  Allow infinity p
a2e50 72 65 63 69 73 69 6f 6e 20 2a 2f 0a 0a 0a 2f 2a  recision */.../*
a2e60 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
a2e70 67 20 74 61 62 6c 65 20 69 73 20 73 65 61 72 63  g table is searc
a2e80 68 65 64 20 6c 69 6e 65 61 72 6c 79 2c 20 73 6f  hed linearly, so
a2e90 20 69 74 20 69 73 20 67 6f 6f 64 20 74 6f 20 70   it is good to p
a2ea0 75 74 20 74 68 65 0a 2a 2a 20 6d 6f 73 74 20 66  ut the.** most f
a2eb0 72 65 71 75 65 6e 74 6c 79 20 75 73 65 64 20 63  requently used c
a2ec0 6f 6e 76 65 72 73 69 6f 6e 20 74 79 70 65 73 20  onversion types 
a2ed0 66 69 72 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  first..*/.static
a2ee0 20 63 6f 6e 73 74 20 63 68 61 72 20 61 44 69 67   const char aDig
a2ef0 69 74 73 5b 5d 20 3d 20 22 30 31 32 33 34 35 36  its[] = "0123456
a2f00 37 38 39 41 42 43 44 45 46 30 31 32 33 34 35 36  789ABCDEF0123456
a2f10 37 38 39 61 62 63 64 65 66 22 3b 0a 73 74 61 74  789abcdef";.stat
a2f20 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 61 50  ic const char aP
a2f30 72 65 66 69 78 5b 5d 20 3d 20 22 2d 78 30 5c 30  refix[] = "-x0\0
a2f40 30 30 58 30 22 3b 0a 73 74 61 74 69 63 20 63 6f  00X0";.static co
a2f50 6e 73 74 20 65 74 5f 69 6e 66 6f 20 66 6d 74 69  nst et_info fmti
a2f60 6e 66 6f 5b 5d 20 3d 20 7b 0a 20 20 7b 20 20 27  nfo[] = {.  {  '
a2f70 64 27 2c 20 31 30 2c 20 31 2c 20 65 74 52 41 44  d', 10, 1, etRAD
a2f80 49 58 2c 20 20 20 20 20 20 30 2c 20 20 30 20 7d  IX,      0,  0 }
a2f90 2c 0a 20 20 7b 20 20 27 73 27 2c 20 20 30 2c 20  ,.  {  's',  0, 
a2fa0 34 2c 20 65 74 53 54 52 49 4e 47 2c 20 20 20 20  4, etSTRING,    
a2fb0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a2fc0 67 27 2c 20 20 30 2c 20 31 2c 20 65 74 47 45 4e  g',  0, 1, etGEN
a2fd0 45 52 49 43 2c 20 20 20 20 33 30 2c 20 30 20 7d  ERIC,    30, 0 }
a2fe0 2c 0a 20 20 7b 20 20 27 7a 27 2c 20 20 30 2c 20  ,.  {  'z',  0, 
a2ff0 34 2c 20 65 74 44 59 4e 53 54 52 49 4e 47 2c 20  4, etDYNSTRING, 
a3000 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a3010 71 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  q',  0, 4, etSQL
a3020 45 53 43 41 50 45 2c 20 20 30 2c 20 20 30 20 7d  ESCAPE,  0,  0 }
a3030 2c 0a 20 20 7b 20 20 27 51 27 2c 20 20 30 2c 20  ,.  {  'Q',  0, 
a3040 34 2c 20 65 74 53 51 4c 45 53 43 41 50 45 32 2c  4, etSQLESCAPE2,
a3050 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a3060 77 27 2c 20 20 30 2c 20 34 2c 20 65 74 53 51 4c  w',  0, 4, etSQL
a3070 45 53 43 41 50 45 33 2c 20 30 2c 20 20 30 20 7d  ESCAPE3, 0,  0 }
a3080 2c 0a 20 20 7b 20 20 27 63 27 2c 20 20 30 2c 20  ,.  {  'c',  0, 
a3090 30 2c 20 65 74 43 48 41 52 58 2c 20 20 20 20 20  0, etCHARX,     
a30a0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a30b0 6f 27 2c 20 20 38 2c 20 30 2c 20 65 74 52 41 44  o',  8, 0, etRAD
a30c0 49 58 2c 20 20 20 20 20 20 30 2c 20 20 32 20 7d  IX,      0,  2 }
a30d0 2c 0a 20 20 7b 20 20 27 75 27 2c 20 31 30 2c 20  ,.  {  'u', 10, 
a30e0 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
a30f0 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20 20 27   0,  0 },.  {  '
a3100 78 27 2c 20 31 36 2c 20 30 2c 20 65 74 52 41 44  x', 16, 0, etRAD
a3110 49 58 2c 20 20 20 20 20 20 31 36 2c 20 31 20 7d  IX,      16, 1 }
a3120 2c 0a 20 20 7b 20 20 27 58 27 2c 20 31 36 2c 20  ,.  {  'X', 16, 
a3130 30 2c 20 65 74 52 41 44 49 58 2c 20 20 20 20 20  0, etRADIX,     
a3140 20 30 2c 20 20 34 20 7d 2c 0a 23 69 66 6e 64 65   0,  4 },.#ifnde
a3150 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c  f SQLITE_OMIT_FL
a3160 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 7b  OATING_POINT.  {
a3170 20 20 27 66 27 2c 20 20 30 2c 20 31 2c 20 65 74    'f',  0, 1, et
a3180 46 4c 4f 41 54 2c 20 20 20 20 20 20 30 2c 20 20  FLOAT,      0,  
a3190 30 20 7d 2c 0a 20 20 7b 20 20 27 65 27 2c 20 20  0 },.  {  'e',  
a31a0 30 2c 20 31 2c 20 65 74 45 58 50 2c 20 20 20 20  0, 1, etEXP,    
a31b0 20 20 20 20 33 30 2c 20 30 20 7d 2c 0a 20 20 7b      30, 0 },.  {
a31c0 20 20 27 45 27 2c 20 20 30 2c 20 31 2c 20 65 74    'E',  0, 1, et
a31d0 45 58 50 2c 20 20 20 20 20 20 20 20 31 34 2c 20  EXP,        14, 
a31e0 30 20 7d 2c 0a 20 20 7b 20 20 27 47 27 2c 20 20  0 },.  {  'G',  
a31f0 30 2c 20 31 2c 20 65 74 47 45 4e 45 52 49 43 2c  0, 1, etGENERIC,
a3200 20 20 20 20 31 34 2c 20 30 20 7d 2c 0a 23 65 6e      14, 0 },.#en
a3210 64 69 66 0a 20 20 7b 20 20 27 69 27 2c 20 31 30  dif.  {  'i', 10
a3220 2c 20 31 2c 20 65 74 52 41 44 49 58 2c 20 20 20  , 1, etRADIX,   
a3230 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
a3240 20 27 6e 27 2c 20 20 30 2c 20 30 2c 20 65 74 53   'n',  0, 0, etS
a3250 49 5a 45 2c 20 20 20 20 20 20 20 30 2c 20 20 30  IZE,       0,  0
a3260 20 7d 2c 0a 20 20 7b 20 20 27 25 27 2c 20 20 30   },.  {  '%',  0
a3270 2c 20 30 2c 20 65 74 50 45 52 43 45 4e 54 2c 20  , 0, etPERCENT, 
a3280 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
a3290 20 27 70 27 2c 20 31 36 2c 20 30 2c 20 65 74 50   'p', 16, 0, etP
a32a0 4f 49 4e 54 45 52 2c 20 20 20 20 30 2c 20 20 31  OINTER,    0,  1
a32b0 20 7d 2c 0a 20 20 7b 20 20 27 54 27 2c 20 20 30   },.  {  'T',  0
a32c0 2c 20 32 2c 20 65 74 54 4f 4b 45 4e 2c 20 20 20  , 2, etTOKEN,   
a32d0 20 20 20 30 2c 20 20 30 20 7d 2c 0a 20 20 7b 20     0,  0 },.  { 
a32e0 20 27 53 27 2c 20 20 30 2c 20 32 2c 20 65 74 53   'S',  0, 2, etS
a32f0 52 43 4c 49 53 54 2c 20 20 20 20 30 2c 20 20 30  RCLIST,    0,  0
a3300 20 7d 2c 0a 20 20 7b 20 20 27 72 27 2c 20 31 30   },.  {  'r', 10
a3310 2c 20 33 2c 20 65 74 4f 52 44 49 4e 41 4c 2c 20  , 3, etORDINAL, 
a3320 20 20 20 30 2c 20 20 30 20 7d 2c 0a 7d 3b 0a 0a     0,  0 },.};..
a3330 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  /*.** If SQLITE_
a3340 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
a3350 49 4e 54 20 69 73 20 64 65 66 69 6e 65 64 2c 20  INT is defined, 
a3360 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68 65  then none of the
a3370 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a   floating point.
a3380 2a 2a 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 77  ** conversions w
a3390 69 6c 6c 20 77 6f 72 6b 2e 0a 2a 2f 0a 23 69 66  ill work..*/.#if
a33a0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
a33b0 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
a33c0 2f 2a 0a 2a 2a 20 22 2a 76 61 6c 22 20 69 73 20  /*.** "*val" is 
a33d0 61 20 64 6f 75 62 6c 65 20 73 75 63 68 20 74 68  a double such th
a33e0 61 74 20 30 2e 31 20 3c 3d 20 2a 76 61 6c 20 3c  at 0.1 <= *val <
a33f0 20 31 30 2e 30 0a 2a 2a 20 52 65 74 75 72 6e 20   10.0.** Return 
a3400 74 68 65 20 61 73 63 69 69 20 63 6f 64 65 20 66  the ascii code f
a3410 6f 72 20 74 68 65 20 6c 65 61 64 69 6e 67 20 64  or the leading d
a3420 69 67 69 74 20 6f 66 20 2a 76 61 6c 2c 20 74 68  igit of *val, th
a3430 65 6e 0a 2a 2a 20 6d 75 6c 74 69 70 6c 79 20 22  en.** multiply "
a3440 2a 76 61 6c 22 20 62 79 20 31 30 2e 30 20 74 6f  *val" by 10.0 to
a3450 20 72 65 6e 6f 72 6d 61 6c 69 7a 65 2e 0a 2a 2a   renormalize..**
a3460 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 20  .** Example:.** 
a3470 20 20 20 20 69 6e 70 75 74 3a 20 20 20 20 20 2a      input:     *
a3480 76 61 6c 20 3d 20 33 2e 31 34 31 35 39 0a 2a 2a  val = 3.14159.**
a3490 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20       output:    
a34a0 2a 76 61 6c 20 3d 20 31 2e 34 31 35 39 20 20 20  *val = 1.4159   
a34b0 20 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e   function return
a34c0 20 3d 20 27 33 27 0a 2a 2a 0a 2a 2a 20 54 68 65   = '3'.**.** The
a34d0 20 63 6f 75 6e 74 65 72 20 2a 63 6e 74 20 69 73   counter *cnt is
a34e0 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 65 61 63   incremented eac
a34f0 68 20 74 69 6d 65 2e 20 20 41 66 74 65 72 20 63  h time.  After c
a3500 6f 75 6e 74 65 72 20 65 78 63 65 65 64 73 0a 2a  ounter exceeds.*
a3510 2a 20 31 36 20 28 74 68 65 20 6e 75 6d 62 65 72  * 16 (the number
a3520 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20   of significant 
a3530 64 69 67 69 74 73 20 69 6e 20 61 20 36 34 2d 62  digits in a 64-b
a3540 69 74 20 66 6c 6f 61 74 29 20 27 30 27 20 69 73  it float) '0' is
a3550 0a 2a 2a 20 61 6c 77 61 79 73 20 72 65 74 75 72  .** always retur
a3560 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
a3570 6e 74 20 65 74 5f 67 65 74 64 69 67 69 74 28 4c  nt et_getdigit(L
a3580 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 2a  ONGDOUBLE_TYPE *
a3590 76 61 6c 2c 20 69 6e 74 20 2a 63 6e 74 29 7b 0a  val, int *cnt){.
a35a0 20 20 69 6e 74 20 64 69 67 69 74 3b 0a 20 20 4c    int digit;.  L
a35b0 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20 64  ONGDOUBLE_TYPE d
a35c0 3b 0a 20 20 69 66 28 20 28 2a 63 6e 74 29 2b 2b  ;.  if( (*cnt)++
a35d0 20 3e 3d 20 31 36 20 29 20 72 65 74 75 72 6e 20   >= 16 ) return 
a35e0 27 30 27 3b 0a 20 20 64 69 67 69 74 20 3d 20 28  '0';.  digit = (
a35f0 69 6e 74 29 2a 76 61 6c 3b 0a 20 20 64 20 3d 20  int)*val;.  d = 
a3600 64 69 67 69 74 3b 0a 20 20 64 69 67 69 74 20 2b  digit;.  digit +
a3610 3d 20 27 30 27 3b 0a 20 20 2a 76 61 6c 20 3d 20  = '0';.  *val = 
a3620 28 2a 76 61 6c 20 2d 20 64 29 2a 31 30 2e 30 3b  (*val - d)*10.0;
a3630 0a 20 20 72 65 74 75 72 6e 20 64 69 67 69 74 3b  .  return digit;
a3640 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
a3650 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e  ITE_OMIT_FLOATIN
a3660 47 5f 50 4f 49 4e 54 20 2a 2f 0a 0a 2f 2a 0a 2a  G_POINT */../*.*
a3670 2a 20 41 70 70 65 6e 64 20 4e 20 73 70 61 63 65  * Append N space
a3680 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 74   characters to t
a3690 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 20  he given string 
a36a0 62 75 66 66 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  buffer..*/.stati
a36b0 63 20 76 6f 69 64 20 61 70 70 65 6e 64 53 70 61  c void appendSpa
a36c0 63 65 28 53 74 72 41 63 63 75 6d 20 2a 70 41 63  ce(StrAccum *pAc
a36d0 63 75 6d 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 73  cum, int N){.  s
a36e0 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
a36f0 20 7a 53 70 61 63 65 73 5b 5d 20 3d 20 22 20 20   zSpaces[] = "  
a3700 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3710 20 20 20 20 20 20 20 20 20 20 20 22 3b 0a 20 20             ";.  
a3720 77 68 69 6c 65 28 20 4e 3e 3d 28 69 6e 74 29 73  while( N>=(int)s
a3730 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31  izeof(zSpaces)-1
a3740 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
a3750 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41  trAccumAppend(pA
a3760 63 63 75 6d 2c 20 7a 53 70 61 63 65 73 2c 20 73  ccum, zSpaces, s
a3770 69 7a 65 6f 66 28 7a 53 70 61 63 65 73 29 2d 31  izeof(zSpaces)-1
a3780 29 3b 0a 20 20 20 20 4e 20 2d 3d 20 73 69 7a 65  );.    N -= size
a3790 6f 66 28 7a 53 70 61 63 65 73 29 2d 31 3b 0a 20  of(zSpaces)-1;. 
a37a0 20 7d 0a 20 20 69 66 28 20 4e 3e 30 20 29 7b 0a   }.  if( N>0 ){.
a37b0 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63      sqlite3StrAc
a37c0 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75 6d  cumAppend(pAccum
a37d0 2c 20 7a 53 70 61 63 65 73 2c 20 4e 29 3b 0a 20  , zSpaces, N);. 
a37e0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 20 6d   }.}../*.** On m
a37f0 61 63 68 69 6e 65 73 20 77 69 74 68 20 61 20 73  achines with a s
a3800 6d 61 6c 6c 20 73 74 61 63 6b 20 73 69 7a 65 2c  mall stack size,
a3810 20 79 6f 75 20 63 61 6e 20 72 65 64 65 66 69 6e   you can redefin
a3820 65 20 74 68 65 0a 2a 2a 20 53 51 4c 49 54 45 5f  e the.** SQLITE_
a3830 50 52 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 74  PRINT_BUF_SIZE t
a3840 6f 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20 33  o be less than 3
a3850 35 30 2e 20 20 42 75 74 20 62 65 77 61 72 65 20  50.  But beware 
a3860 2d 20 66 6f 72 0a 2a 2a 20 73 6d 61 6c 6c 65 72  - for.** smaller
a3870 20 76 61 6c 75 65 73 20 73 6f 6d 65 20 25 66 20   values some %f 
a3880 63 6f 6e 76 65 72 73 69 6f 6e 73 20 6d 61 79 20  conversions may 
a3890 67 6f 20 69 6e 74 6f 20 61 6e 20 69 6e 66 69 6e  go into an infin
a38a0 69 74 65 20 6c 6f 6f 70 2e 0a 2a 2f 0a 23 69 66  ite loop..*/.#if
a38b0 6e 64 65 66 20 53 51 4c 49 54 45 5f 50 52 49 4e  ndef SQLITE_PRIN
a38c0 54 5f 42 55 46 5f 53 49 5a 45 0a 23 20 64 65 66  T_BUF_SIZE.# def
a38d0 69 6e 65 20 53 51 4c 49 54 45 5f 50 52 49 4e 54  ine SQLITE_PRINT
a38e0 5f 42 55 46 5f 53 49 5a 45 20 33 35 30 0a 23 65  _BUF_SIZE 350.#e
a38f0 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 65 74 42  ndif.#define etB
a3900 55 46 53 49 5a 45 20 53 51 4c 49 54 45 5f 50 52  UFSIZE SQLITE_PR
a3910 49 4e 54 5f 42 55 46 5f 53 49 5a 45 20 20 2f 2a  INT_BUF_SIZE  /*
a3920 20 53 69 7a 65 20 6f 66 20 74 68 65 20 6f 75 74   Size of the out
a3930 70 75 74 20 62 75 66 66 65 72 20 2a 2f 0a 0a 2f  put buffer */../
a3940 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 72  *.** The root pr
a3950 6f 67 72 61 6d 2e 20 20 41 6c 6c 20 76 61 72 69  ogram.  All vari
a3960 61 74 69 6f 6e 73 20 63 61 6c 6c 20 74 68 69 73  ations call this
a3970 20 63 6f 72 65 2e 0a 2a 2a 0a 2a 2a 20 49 4e 50   core..**.** INP
a3980 55 54 53 3a 0a 2a 2a 20 20 20 66 75 6e 63 20 20  UTS:.**   func  
a3990 20 54 68 69 73 20 69 73 20 61 20 70 6f 69 6e 74   This is a point
a39a0 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e  er to a function
a39b0 20 74 61 6b 69 6e 67 20 74 68 72 65 65 20 61 72   taking three ar
a39c0 67 75 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20 20  guments.**      
a39d0 20 20 20 20 20 20 31 2e 20 41 20 70 6f 69 6e 74        1. A point
a39e0 65 72 20 74 6f 20 61 6e 79 74 68 69 6e 67 2e 20  er to anything. 
a39f0 20 53 61 6d 65 20 61 73 20 74 68 65 20 22 61 72   Same as the "ar
a3a00 67 22 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a 2a  g" parameter..**
a3a10 20 20 20 20 20 20 20 20 20 20 20 20 32 2e 20 41              2. A
a3a20 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
a3a30 6c 69 73 74 20 6f 66 20 63 68 61 72 61 63 74 65  list of characte
a3a40 72 73 20 74 6f 20 62 65 20 6f 75 74 70 75 74 0a  rs to be output.
a3a50 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a3a60 20 28 4e 6f 74 65 2c 20 74 68 69 73 20 6c 69 73   (Note, this lis
a3a70 74 20 69 73 20 4e 4f 54 20 6e 75 6c 6c 20 74 65  t is NOT null te
a3a80 72 6d 69 6e 61 74 65 64 2e 29 0a 2a 2a 20 20 20  rminated.).**   
a3a90 20 20 20 20 20 20 20 20 20 33 2e 20 41 6e 20 69           3. An i
a3aa0 6e 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66  nteger number of
a3ab0 20 63 68 61 72 61 63 74 65 72 73 20 74 6f 20 62   characters to b
a3ac0 65 20 6f 75 74 70 75 74 2e 0a 2a 2a 20 20 20 20  e output..**    
a3ad0 20 20 20 20 20 20 20 20 20 20 20 28 4e 6f 74 65             (Note
a3ae0 3a 20 54 68 69 73 20 6e 75 6d 62 65 72 20 6d 69  : This number mi
a3af0 67 68 74 20 62 65 20 7a 65 72 6f 2e 29 0a 2a 2a  ght be zero.).**
a3b00 0a 2a 2a 20 20 20 61 72 67 20 20 20 20 54 68 69  .**   arg    Thi
a3b10 73 20 69 73 20 74 68 65 20 70 6f 69 6e 74 65 72  s is the pointer
a3b20 20 74 6f 20 61 6e 79 74 68 69 6e 67 20 77 68 69   to anything whi
a3b30 63 68 20 77 69 6c 6c 20 62 65 20 70 61 73 73 65  ch will be passe
a3b40 64 20 61 73 20 74 68 65 0a 2a 2a 20 20 20 20 20  d as the.**     
a3b50 20 20 20 20 20 66 69 72 73 74 20 61 72 67 75 6d       first argum
a3b60 65 6e 74 20 74 6f 20 22 66 75 6e 63 22 2e 20 20  ent to "func".  
a3b70 55 73 65 20 69 74 20 66 6f 72 20 77 68 61 74 65  Use it for whate
a3b80 76 65 72 20 79 6f 75 20 6c 69 6b 65 2e 0a 2a 2a  ver you like..**
a3b90 0a 2a 2a 20 20 20 66 6d 74 20 20 20 20 54 68 69  .**   fmt    Thi
a3ba0 73 20 69 73 20 74 68 65 20 66 6f 72 6d 61 74 20  s is the format 
a3bb0 73 74 72 69 6e 67 2c 20 61 73 20 69 6e 20 74 68  string, as in th
a3bc0 65 20 75 73 75 61 6c 20 70 72 69 6e 74 2e 0a 2a  e usual print..*
a3bd0 2a 0a 2a 2a 20 20 20 61 70 20 20 20 20 20 54 68  *.**   ap     Th
a3be0 69 73 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  is is a pointer 
a3bf0 74 6f 20 61 20 6c 69 73 74 20 6f 66 20 61 72 67  to a list of arg
a3c00 75 6d 65 6e 74 73 2e 20 20 53 61 6d 65 20 61 73  uments.  Same as
a3c10 20 69 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   in.**          
a3c20 76 66 70 72 69 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f  vfprint..**.** O
a3c30 55 54 50 55 54 53 3a 0a 2a 2a 20 20 20 20 20 20  UTPUTS:.**      
a3c40 20 20 20 20 54 68 65 20 72 65 74 75 72 6e 20 76      The return v
a3c50 61 6c 75 65 20 69 73 20 74 68 65 20 74 6f 74 61  alue is the tota
a3c60 6c 20 6e 75 6d 62 65 72 20 6f 66 20 63 68 61 72  l number of char
a3c70 61 63 74 65 72 73 20 73 65 6e 74 20 74 6f 0a 2a  acters sent to.*
a3c80 2a 20 20 20 20 20 20 20 20 20 20 74 68 65 20 66  *          the f
a3c90 75 6e 63 74 69 6f 6e 20 22 66 75 6e 63 22 2e 20  unction "func". 
a3ca0 20 52 65 74 75 72 6e 73 20 2d 31 20 6f 6e 20 61   Returns -1 on a
a3cb0 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f   error..**.** No
a3cc0 74 65 20 74 68 61 74 20 74 68 65 20 6f 72 64 65  te that the orde
a3cd0 72 20 69 6e 20 77 68 69 63 68 20 61 75 74 6f 6d  r in which autom
a3ce0 61 74 69 63 20 76 61 72 69 61 62 6c 65 73 20 61  atic variables a
a3cf0 72 65 20 64 65 63 6c 61 72 65 64 20 62 65 6c 6f  re declared belo
a3d00 77 0a 2a 2a 20 73 65 65 6d 73 20 74 6f 20 6d 61  w.** seems to ma
a3d10 6b 65 20 61 20 62 69 67 20 64 69 66 66 65 72 65  ke a big differe
a3d20 6e 63 65 20 69 6e 20 64 65 74 65 72 6d 69 6e 69  nce in determini
a3d30 6e 67 20 68 6f 77 20 66 61 73 74 20 74 68 69 73  ng how fast this
a3d40 20 62 65 61 73 74 0a 2a 2a 20 77 69 6c 6c 20 72   beast.** will r
a3d50 75 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  un..*/.SQLITE_PR
a3d60 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
a3d70 65 33 56 58 50 72 69 6e 74 66 28 0a 20 20 53 74  e3VXPrintf(.  St
a3d80 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 2c 20  rAccum *pAccum, 
a3d90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3da0 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 72   /* Accumulate r
a3db0 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20  esults here */. 
a3dc0 20 69 6e 74 20 75 73 65 45 78 74 65 6e 64 65 64   int useExtended
a3dd0 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
a3de0 20 20 20 20 2f 2a 20 41 6c 6c 6f 77 20 65 78 74      /* Allow ext
a3df0 65 6e 64 65 64 20 25 2d 63 6f 6e 76 65 72 73 69  ended %-conversi
a3e00 6f 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ons */.  const c
a3e10 68 61 72 20 2a 66 6d 74 2c 20 20 20 20 20 20 20  har *fmt,       
a3e20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
a3e30 6f 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a  ormat string */.
a3e40 20 20 76 61 5f 6c 69 73 74 20 61 70 20 20 20 20    va_list ap    
a3e50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3e60 20 20 20 20 20 2f 2a 20 61 72 67 75 6d 65 6e 74       /* argument
a3e70 73 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 63 3b  s */.){.  int c;
a3e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a3e90 20 20 20 20 20 2f 2a 20 4e 65 78 74 20 63 68 61       /* Next cha
a3ea0 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 66 6f  racter in the fo
a3eb0 72 6d 61 74 20 73 74 72 69 6e 67 20 2a 2f 0a 20  rmat string */. 
a3ec0 20 63 68 61 72 20 2a 62 75 66 70 74 3b 20 20 20   char *bufpt;   
a3ed0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
a3ee0 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f  ointer to the co
a3ef0 6e 76 65 72 73 69 6f 6e 20 62 75 66 66 65 72 20  nversion buffer 
a3f00 2a 2f 0a 20 20 69 6e 74 20 70 72 65 63 69 73 69  */.  int precisi
a3f10 6f 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  on;             
a3f20 2f 2a 20 50 72 65 63 69 73 69 6f 6e 20 6f 66 20  /* Precision of 
a3f30 74 68 65 20 63 75 72 72 65 6e 74 20 66 69 65 6c  the current fiel
a3f40 64 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 67 74  d */.  int lengt
a3f50 68 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h;              
a3f60 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 74    /* Length of t
a3f70 68 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 69 6e  he field */.  in
a3f80 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
a3f90 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 67 65           /* A ge
a3fa0 6e 65 72 61 6c 20 70 75 72 70 6f 73 65 20 6c 6f  neral purpose lo
a3fb0 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  op counter */.  
a3fc0 69 6e 74 20 77 69 64 74 68 3b 20 20 20 20 20 20  int width;      
a3fd0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 69             /* Wi
a3fe0 64 74 68 20 6f 66 20 74 68 65 20 63 75 72 72 65  dth of the curre
a3ff0 6e 74 20 66 69 65 6c 64 20 2a 2f 0a 20 20 65 74  nt field */.  et
a4000 42 79 74 65 20 66 6c 61 67 5f 6c 65 66 74 6a 75  Byte flag_leftju
a4010 73 74 69 66 79 3b 20 20 20 2f 2a 20 54 72 75 65  stify;   /* True
a4020 20 69 66 20 22 2d 22 20 66 6c 61 67 20 69 73 20   if "-" flag is 
a4030 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a4040 79 74 65 20 66 6c 61 67 5f 70 6c 75 73 73 69 67  yte flag_plussig
a4050 6e 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20  n;      /* True 
a4060 69 66 20 22 2b 22 20 66 6c 61 67 20 69 73 20 70  if "+" flag is p
a4070 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79  resent */.  etBy
a4080 74 65 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67  te flag_blanksig
a4090 6e 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  n;     /* True i
a40a0 66 20 22 20 22 20 66 6c 61 67 20 69 73 20 70 72  f " " flag is pr
a40b0 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74  esent */.  etByt
a40c0 65 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65  e flag_alternate
a40d0 66 6f 72 6d 3b 20 2f 2a 20 54 72 75 65 20 69 66  form; /* True if
a40e0 20 22 23 22 20 66 6c 61 67 20 69 73 20 70 72 65   "#" flag is pre
a40f0 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65  sent */.  etByte
a4100 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 20   flag_altform2; 
a4110 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a4120 22 21 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  "!" flag is pres
a4130 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a4140 66 6c 61 67 5f 7a 65 72 6f 70 61 64 3b 20 20 20  flag_zeropad;   
a4150 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 66      /* True if f
a4160 69 65 6c 64 20 77 69 64 74 68 20 63 6f 6e 73 74  ield width const
a4170 61 6e 74 20 73 74 61 72 74 73 20 77 69 74 68 20  ant starts with 
a4180 7a 65 72 6f 20 2a 2f 0a 20 20 65 74 42 79 74 65  zero */.  etByte
a4190 20 66 6c 61 67 5f 6c 6f 6e 67 3b 20 20 20 20 20   flag_long;     
a41a0 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
a41b0 22 6c 22 20 66 6c 61 67 20 69 73 20 70 72 65 73  "l" flag is pres
a41c0 65 6e 74 20 2a 2f 0a 20 20 65 74 42 79 74 65 20  ent */.  etByte 
a41d0 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 3b 20 20  flag_longlong;  
a41e0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74      /* True if t
a41f0 68 65 20 22 6c 6c 22 20 66 6c 61 67 20 69 73 20  he "ll" flag is 
a4200 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 65 74 42  present */.  etB
a4210 79 74 65 20 64 6f 6e 65 3b 20 20 20 20 20 20 20  yte done;       
a4220 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
a4230 74 65 72 6d 69 6e 61 74 69 6f 6e 20 66 6c 61 67  termination flag
a4240 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 75 69 6e   */.  sqlite_uin
a4250 74 36 34 20 6c 6f 6e 67 76 61 6c 75 65 3b 20 20  t64 longvalue;  
a4260 20 2f 2a 20 56 61 6c 75 65 20 66 6f 72 20 69 6e   /* Value for in
a4270 74 65 67 65 72 20 74 79 70 65 73 20 2a 2f 0a 20  teger types */. 
a4280 20 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45   LONGDOUBLE_TYPE
a4290 20 72 65 61 6c 76 61 6c 75 65 3b 20 2f 2a 20 56   realvalue; /* V
a42a0 61 6c 75 65 20 66 6f 72 20 72 65 61 6c 20 74 79  alue for real ty
a42b0 70 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 65  pes */.  const e
a42c0 74 5f 69 6e 66 6f 20 2a 69 6e 66 6f 70 3b 20 20  t_info *infop;  
a42d0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
a42e0 6f 20 74 68 65 20 61 70 70 72 6f 70 72 69 61 74  o the appropriat
a42f0 65 20 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  e info structure
a4300 20 2a 2f 0a 20 20 63 68 61 72 20 62 75 66 5b 65   */.  char buf[e
a4310 74 42 55 46 53 49 5a 45 5d 3b 20 20 20 20 20 20  tBUFSIZE];      
a4320 20 2f 2a 20 43 6f 6e 76 65 72 73 69 6f 6e 20 62   /* Conversion b
a4330 75 66 66 65 72 20 2a 2f 0a 20 20 63 68 61 72 20  uffer */.  char 
a4340 70 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20  prefix;         
a4350 20 20 20 20 20 20 2f 2a 20 50 72 65 66 69 78 20        /* Prefix 
a4360 63 68 61 72 61 63 74 65 72 2e 20 20 22 2b 22 20  character.  "+" 
a4370 6f 72 20 22 2d 22 20 6f 72 20 22 20 22 20 6f 72  or "-" or " " or
a4380 20 27 5c 30 27 2e 20 2a 2f 0a 20 20 65 74 42 79   '\0'. */.  etBy
a4390 74 65 20 78 74 79 70 65 3b 20 20 20 20 20 20 20  te xtype;       
a43a0 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65 72         /* Conver
a43b0 73 69 6f 6e 20 70 61 72 61 64 69 67 6d 20 2a 2f  sion paradigm */
a43c0 0a 20 20 63 68 61 72 20 2a 7a 45 78 74 72 61 3b  .  char *zExtra;
a43d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a43e0 20 45 78 74 72 61 20 6d 65 6d 6f 72 79 20 75 73   Extra memory us
a43f0 65 64 20 66 6f 72 20 65 74 54 43 4c 45 53 43 41  ed for etTCLESCA
a4400 50 45 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 2a  PE conversions *
a4410 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
a4420 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50  _OMIT_FLOATING_P
a4430 4f 49 4e 54 0a 20 20 69 6e 74 20 20 65 78 70 2c  OINT.  int  exp,
a4440 20 65 32 3b 20 20 20 20 20 20 20 20 20 20 20 20   e2;            
a4450 20 20 2f 2a 20 65 78 70 6f 6e 65 6e 74 20 6f 66    /* exponent of
a4460 20 72 65 61 6c 20 6e 75 6d 62 65 72 73 20 2a 2f   real numbers */
a4470 0a 20 20 64 6f 75 62 6c 65 20 72 6f 75 6e 64 65  .  double rounde
a4480 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  r;            /*
a4490 20 55 73 65 64 20 66 6f 72 20 72 6f 75 6e 64 69   Used for roundi
a44a0 6e 67 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  ng floating poin
a44b0 74 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 65 74  t values */.  et
a44c0 42 79 74 65 20 66 6c 61 67 5f 64 70 3b 20 20 20  Byte flag_dp;   
a44d0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
a44e0 20 69 66 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e   if decimal poin
a44f0 74 20 73 68 6f 75 6c 64 20 62 65 20 73 68 6f 77  t should be show
a4500 6e 20 2a 2f 0a 20 20 65 74 42 79 74 65 20 66 6c  n */.  etByte fl
a4510 61 67 5f 72 74 7a 3b 20 20 20 20 20 20 20 20 20  ag_rtz;         
a4520 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 72 61    /* True if tra
a4530 69 6c 69 6e 67 20 7a 65 72 6f 73 20 73 68 6f 75  iling zeros shou
a4540 6c 64 20 62 65 20 72 65 6d 6f 76 65 64 20 2a 2f  ld be removed */
a4550 0a 20 20 65 74 42 79 74 65 20 66 6c 61 67 5f 65  .  etByte flag_e
a4560 78 70 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  xp;           /*
a4570 20 54 72 75 65 20 74 6f 20 66 6f 72 63 65 20 64   True to force d
a4580 69 73 70 6c 61 79 20 6f 66 20 74 68 65 20 65 78  isplay of the ex
a4590 70 6f 6e 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  ponent */.  int 
a45a0 6e 73 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  nsd;            
a45b0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
a45c0 20 6f 66 20 73 69 67 6e 69 66 69 63 61 6e 74 20   of significant 
a45d0 64 69 67 69 74 73 20 72 65 74 75 72 6e 65 64 20  digits returned 
a45e0 2a 2f 0a 23 65 6e 64 69 66 0a 0a 20 20 6c 65 6e  */.#endif..  len
a45f0 67 74 68 20 3d 20 30 3b 0a 20 20 62 75 66 70 74  gth = 0;.  bufpt
a4600 20 3d 20 30 3b 0a 20 20 66 6f 72 28 3b 20 28 63   = 0;.  for(; (c
a4610 3d 28 2a 66 6d 74 29 29 21 3d 30 3b 20 2b 2b 66  =(*fmt))!=0; ++f
a4620 6d 74 29 7b 0a 20 20 20 20 69 66 28 20 63 21 3d  mt){.    if( c!=
a4630 27 25 27 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  '%' ){.      int
a4640 20 61 6d 74 3b 0a 20 20 20 20 20 20 62 75 66 70   amt;.      bufp
a4650 74 20 3d 20 28 63 68 61 72 20 2a 29 66 6d 74 3b  t = (char *)fmt;
a4660 0a 20 20 20 20 20 20 61 6d 74 20 3d 20 31 3b 0a  .      amt = 1;.
a4670 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d        while( (c=
a4680 28 2a 2b 2b 66 6d 74 29 29 21 3d 27 25 27 20 26  (*++fmt))!='%' &
a4690 26 20 63 21 3d 30 20 29 20 61 6d 74 2b 2b 3b 0a  & c!=0 ) amt++;.
a46a0 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a46b0 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
a46c0 75 6d 2c 20 62 75 66 70 74 2c 20 61 6d 74 29 3b  um, bufpt, amt);
a46d0 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d 30 20  .      if( c==0 
a46e0 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20  ) break;.    }. 
a46f0 20 20 20 69 66 28 20 28 63 3d 28 2a 2b 2b 66 6d     if( (c=(*++fm
a4700 74 29 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  t))==0 ){.      
a4710 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
a4720 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 22 25  ppend(pAccum, "%
a4730 22 2c 20 31 29 3b 0a 20 20 20 20 20 20 62 72 65  ", 1);.      bre
a4740 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a  ak;.    }.    /*
a4750 20 46 69 6e 64 20 6f 75 74 20 77 68 61 74 20 66   Find out what f
a4760 6c 61 67 73 20 61 72 65 20 70 72 65 73 65 6e 74  lags are present
a4770 20 2a 2f 0a 20 20 20 20 66 6c 61 67 5f 6c 65 66   */.    flag_lef
a4780 74 6a 75 73 74 69 66 79 20 3d 20 66 6c 61 67 5f  tjustify = flag_
a4790 70 6c 75 73 73 69 67 6e 20 3d 20 66 6c 61 67 5f  plussign = flag_
a47a0 62 6c 61 6e 6b 73 69 67 6e 20 3d 20 0a 20 20 20  blanksign = .   
a47b0 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65    flag_alternate
a47c0 66 6f 72 6d 20 3d 20 66 6c 61 67 5f 61 6c 74 66  form = flag_altf
a47d0 6f 72 6d 32 20 3d 20 66 6c 61 67 5f 7a 65 72 6f  orm2 = flag_zero
a47e0 70 61 64 20 3d 20 30 3b 0a 20 20 20 20 64 6f 6e  pad = 0;.    don
a47f0 65 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20  e = 0;.    do{. 
a4800 20 20 20 20 20 73 77 69 74 63 68 28 20 63 20 29       switch( c )
a4810 7b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27  {.        case '
a4820 2d 27 3a 20 20 20 66 6c 61 67 5f 6c 65 66 74 6a  -':   flag_leftj
a4830 75 73 74 69 66 79 20 3d 20 31 3b 20 20 20 20 20  ustify = 1;     
a4840 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63  break;.        c
a4850 61 73 65 20 27 2b 27 3a 20 20 20 66 6c 61 67 5f  ase '+':   flag_
a4860 70 6c 75 73 73 69 67 6e 20 3d 20 31 3b 20 20 20  plussign = 1;   
a4870 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a4880 20 20 20 20 63 61 73 65 20 27 20 27 3a 20 20 20      case ' ':   
a4890 66 6c 61 67 5f 62 6c 61 6e 6b 73 69 67 6e 20 3d  flag_blanksign =
a48a0 20 31 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b   1;       break;
a48b0 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 27 23  .        case '#
a48c0 27 3a 20 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e  ':   flag_altern
a48d0 61 74 65 66 6f 72 6d 20 3d 20 31 3b 20 20 20 62  ateform = 1;   b
a48e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61  reak;.        ca
a48f0 73 65 20 27 21 27 3a 20 20 20 66 6c 61 67 5f 61  se '!':   flag_a
a4900 6c 74 66 6f 72 6d 32 20 3d 20 31 3b 20 20 20 20  ltform2 = 1;    
a4910 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
a4920 20 20 20 63 61 73 65 20 27 30 27 3a 20 20 20 66     case '0':   f
a4930 6c 61 67 5f 7a 65 72 6f 70 61 64 20 3d 20 31 3b  lag_zeropad = 1;
a4940 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
a4950 20 20 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a          default:
a4960 20 20 20 20 64 6f 6e 65 20 3d 20 31 3b 20 20 20      done = 1;   
a4970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
a4980 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
a4990 20 7d 77 68 69 6c 65 28 20 21 64 6f 6e 65 20 26   }while( !done &
a49a0 26 20 28 63 3d 28 2a 2b 2b 66 6d 74 29 29 21 3d  & (c=(*++fmt))!=
a49b0 30 20 29 3b 0a 20 20 20 20 2f 2a 20 47 65 74 20  0 );.    /* Get 
a49c0 74 68 65 20 66 69 65 6c 64 20 77 69 64 74 68 20  the field width 
a49d0 2a 2f 0a 20 20 20 20 77 69 64 74 68 20 3d 20 30  */.    width = 0
a49e0 3b 0a 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27  ;.    if( c=='*'
a49f0 20 29 7b 0a 20 20 20 20 20 20 77 69 64 74 68 20   ){.      width 
a4a00 3d 20 76 61 5f 61 72 67 28 61 70 2c 69 6e 74 29  = va_arg(ap,int)
a4a10 3b 0a 20 20 20 20 20 20 69 66 28 20 77 69 64 74  ;.      if( widt
a4a20 68 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66  h<0 ){.        f
a4a30 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
a4a40 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 77 69 64  = 1;.        wid
a4a50 74 68 20 3d 20 2d 77 69 64 74 68 3b 0a 20 20 20  th = -width;.   
a4a60 20 20 20 7d 0a 20 20 20 20 20 20 63 20 3d 20 2a     }.      c = *
a4a70 2b 2b 66 6d 74 3b 0a 20 20 20 20 7d 65 6c 73 65  ++fmt;.    }else
a4a80 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 63  {.      while( c
a4a90 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27 39 27 20  >='0' && c<='9' 
a4aa0 29 7b 0a 20 20 20 20 20 20 20 20 77 69 64 74 68  ){.        width
a4ab0 20 3d 20 77 69 64 74 68 2a 31 30 20 2b 20 63 20   = width*10 + c 
a4ac0 2d 20 27 30 27 3b 0a 20 20 20 20 20 20 20 20 63  - '0';.        c
a4ad0 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20 20 20 20   = *++fmt;.     
a4ae0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
a4af0 20 77 69 64 74 68 20 3e 20 65 74 42 55 46 53 49   width > etBUFSI
a4b00 5a 45 2d 31 30 20 29 7b 0a 20 20 20 20 20 20 77  ZE-10 ){.      w
a4b10 69 64 74 68 20 3d 20 65 74 42 55 46 53 49 5a 45  idth = etBUFSIZE
a4b20 2d 31 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f  -10;.    }.    /
a4b30 2a 20 47 65 74 20 74 68 65 20 70 72 65 63 69 73  * Get the precis
a4b40 69 6f 6e 20 2a 2f 0a 20 20 20 20 69 66 28 20 63  ion */.    if( c
a4b50 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 70  =='.' ){.      p
a4b60 72 65 63 69 73 69 6f 6e 20 3d 20 30 3b 0a 20 20  recision = 0;.  
a4b70 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a      c = *++fmt;.
a4b80 20 20 20 20 20 20 69 66 28 20 63 3d 3d 27 2a 27        if( c=='*'
a4b90 20 29 7b 0a 20 20 20 20 20 20 20 20 70 72 65 63   ){.        prec
a4ba0 69 73 69 6f 6e 20 3d 20 76 61 5f 61 72 67 28 61  ision = va_arg(a
a4bb0 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  p,int);.        
a4bc0 69 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20  if( precision<0 
a4bd0 29 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 2d 70  ) precision = -p
a4be0 72 65 63 69 73 69 6f 6e 3b 0a 20 20 20 20 20 20  recision;.      
a4bf0 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20 20    c = *++fmt;.  
a4c00 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
a4c10 20 20 20 77 68 69 6c 65 28 20 63 3e 3d 27 30 27     while( c>='0'
a4c20 20 26 26 20 63 3c 3d 27 39 27 20 29 7b 0a 20 20   && c<='9' ){.  
a4c30 20 20 20 20 20 20 20 20 70 72 65 63 69 73 69 6f          precisio
a4c40 6e 20 3d 20 70 72 65 63 69 73 69 6f 6e 2a 31 30  n = precision*10
a4c50 20 2b 20 63 20 2d 20 27 30 27 3b 0a 20 20 20 20   + c - '0';.    
a4c60 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
a4c70 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
a4c80 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
a4c90 20 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d       precision =
a4ca0 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f   -1;.    }.    /
a4cb0 2a 20 47 65 74 20 74 68 65 20 63 6f 6e 76 65 72  * Get the conver
a4cc0 73 69 6f 6e 20 74 79 70 65 20 6d 6f 64 69 66 69  sion type modifi
a4cd0 65 72 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 3d  er */.    if( c=
a4ce0 3d 27 6c 27 20 29 7b 0a 20 20 20 20 20 20 66 6c  ='l' ){.      fl
a4cf0 61 67 5f 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20 20  ag_long = 1;.   
a4d00 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74 3b 0a 20     c = *++fmt;. 
a4d10 20 20 20 20 20 69 66 28 20 63 3d 3d 27 6c 27 20       if( c=='l' 
a4d20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 5f  ){.        flag_
a4d30 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 31 3b 0a 20 20  longlong = 1;.  
a4d40 20 20 20 20 20 20 63 20 3d 20 2a 2b 2b 66 6d 74        c = *++fmt
a4d50 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
a4d60 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
a4d70 6c 6f 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20  long = 0;.      
a4d80 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
a4d90 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 20 3d 20 66     flag_long = f
a4da0 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67 20 3d 20 30  lag_longlong = 0
a4db0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 46  ;.    }.    /* F
a4dc0 65 74 63 68 20 74 68 65 20 69 6e 66 6f 20 65 6e  etch the info en
a4dd0 74 72 79 20 66 6f 72 20 74 68 65 20 66 69 65 6c  try for the fiel
a4de0 64 20 2a 2f 0a 20 20 20 20 69 6e 66 6f 70 20 3d  d */.    infop =
a4df0 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 64 78 3d   0;.    for(idx=
a4e00 30 3b 20 69 64 78 3c 41 72 72 61 79 53 69 7a 65  0; idx<ArraySize
a4e10 28 66 6d 74 69 6e 66 6f 29 3b 20 69 64 78 2b 2b  (fmtinfo); idx++
a4e20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 63 3d 3d  ){.      if( c==
a4e30 66 6d 74 69 6e 66 6f 5b 69 64 78 5d 2e 66 6d 74  fmtinfo[idx].fmt
a4e40 74 79 70 65 20 29 7b 0a 20 20 20 20 20 20 20 20  type ){.        
a4e50 69 6e 66 6f 70 20 3d 20 26 66 6d 74 69 6e 66 6f  infop = &fmtinfo
a4e60 5b 69 64 78 5d 3b 0a 20 20 20 20 20 20 20 20 69  [idx];.        i
a4e70 66 28 20 75 73 65 45 78 74 65 6e 64 65 64 20 7c  f( useExtended |
a4e80 7c 20 28 69 6e 66 6f 70 2d 3e 66 6c 61 67 73 20  | (infop->flags 
a4e90 26 20 46 4c 41 47 5f 49 4e 54 45 52 4e 29 3d 3d  & FLAG_INTERN)==
a4ea0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 78  0 ){.          x
a4eb0 74 79 70 65 20 3d 20 69 6e 66 6f 70 2d 3e 74 79  type = infop->ty
a4ec0 70 65 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  pe;.        }els
a4ed0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  e{.          ret
a4ee0 75 72 6e 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  urn;.        }. 
a4ef0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
a4f00 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
a4f10 7a 45 78 74 72 61 20 3d 20 30 3b 0a 20 20 20 20  zExtra = 0;.    
a4f20 69 66 28 20 69 6e 66 6f 70 3d 3d 30 20 29 7b 0a  if( infop==0 ){.
a4f30 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a4f40 20 20 7d 0a 0a 0a 20 20 20 20 2f 2a 20 4c 69 6d    }...    /* Lim
a4f50 69 74 20 74 68 65 20 70 72 65 63 69 73 69 6f 6e  it the precision
a4f60 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 65 72   to prevent over
a4f70 66 6c 6f 77 69 6e 67 20 62 75 66 5b 5d 20 64 75  flowing buf[] du
a4f80 72 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 20  ring conversion 
a4f90 2a 2f 0a 20 20 20 20 69 66 28 20 70 72 65 63 69  */.    if( preci
a4fa0 73 69 6f 6e 3e 65 74 42 55 46 53 49 5a 45 2d 34  sion>etBUFSIZE-4
a4fb0 30 20 26 26 20 28 69 6e 66 6f 70 2d 3e 66 6c 61  0 && (infop->fla
a4fc0 67 73 20 26 20 46 4c 41 47 5f 53 54 52 49 4e 47  gs & FLAG_STRING
a4fd0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 72  )==0 ){.      pr
a4fe0 65 63 69 73 69 6f 6e 20 3d 20 65 74 42 55 46 53  ecision = etBUFS
a4ff0 49 5a 45 2d 34 30 3b 0a 20 20 20 20 7d 0a 0a 20  IZE-40;.    }.. 
a5000 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 41 74 20     /*.    ** At 
a5010 74 68 69 73 20 70 6f 69 6e 74 2c 20 76 61 72 69  this point, vari
a5020 61 62 6c 65 73 20 61 72 65 20 69 6e 69 74 69 61  ables are initia
a5030 6c 69 7a 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73  lized as follows
a5040 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
a5050 20 20 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65    flag_alternate
a5060 66 6f 72 6d 20 20 20 20 20 20 20 20 20 20 54 52  form          TR
a5070 55 45 20 69 66 20 61 20 27 23 27 20 69 73 20 70  UE if a '#' is p
a5080 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20  resent..    **  
a5090 20 66 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 20   flag_altform2  
a50a0 20 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55               TRU
a50b0 45 20 69 66 20 61 20 27 21 27 20 69 73 20 70 72  E if a '!' is pr
a50c0 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20  esent..    **   
a50d0 66 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 20 20  flag_plussign   
a50e0 20 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45              TRUE
a50f0 20 69 66 20 61 20 27 2b 27 20 69 73 20 70 72 65   if a '+' is pre
a5100 73 65 6e 74 2e 0a 20 20 20 20 2a 2a 20 20 20 66  sent..    **   f
a5110 6c 61 67 5f 6c 65 66 74 6a 75 73 74 69 66 79 20  lag_leftjustify 
a5120 20 20 20 20 20 20 20 20 20 20 20 54 52 55 45 20             TRUE 
a5130 69 66 20 61 20 27 2d 27 20 69 73 20 70 72 65 73  if a '-' is pres
a5140 65 6e 74 20 6f 72 20 69 66 20 74 68 65 0a 20 20  ent or if the.  
a5150 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
a5160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5170 20 20 20 66 69 65 6c 64 20 77 69 64 74 68 20 77     field width w
a5180 61 73 20 6e 65 67 61 74 69 76 65 2e 0a 20 20 20  as negative..   
a5190 20 2a 2a 20 20 20 66 6c 61 67 5f 7a 65 72 6f 70   **   flag_zerop
a51a0 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ad              
a51b0 20 20 54 52 55 45 20 69 66 20 74 68 65 20 77 69    TRUE if the wi
a51c0 64 74 68 20 62 65 67 61 6e 20 77 69 74 68 20 30  dth began with 0
a51d0 2e 0a 20 20 20 20 2a 2a 20 20 20 66 6c 61 67 5f  ..    **   flag_
a51e0 6c 6f 6e 67 20 20 20 20 20 20 20 20 20 20 20 20  long            
a51f0 20 20 20 20 20 20 20 54 52 55 45 20 69 66 20 74         TRUE if t
a5200 68 65 20 6c 65 74 74 65 72 20 27 6c 27 20 28 65  he letter 'l' (e
a5210 6c 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 20  ll) prefixed.   
a5220 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20   **             
a5230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5240 20 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e    the conversion
a5250 20 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20   character..    
a5260 2a 2a 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f  **   flag_longlo
a5270 6e 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ng              
a5280 20 54 52 55 45 20 69 66 20 74 68 65 20 6c 65 74   TRUE if the let
a5290 74 65 72 20 27 6c 6c 27 20 28 65 6c 6c 20 65 6c  ter 'll' (ell el
a52a0 6c 29 20 70 72 65 66 69 78 65 64 0a 20 20 20 20  l) prefixed.    
a52b0 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
a52c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a52d0 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f 6e 20   the conversion 
a52e0 63 68 61 72 61 63 74 65 72 2e 0a 20 20 20 20 2a  character..    *
a52f0 2a 20 20 20 66 6c 61 67 5f 62 6c 61 6e 6b 73 69  *   flag_blanksi
a5300 67 6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20  gn              
a5310 54 52 55 45 20 69 66 20 61 20 27 20 27 20 69 73  TRUE if a ' ' is
a5320 20 70 72 65 73 65 6e 74 2e 0a 20 20 20 20 2a 2a   present..    **
a5330 20 20 20 77 69 64 74 68 20 20 20 20 20 20 20 20     width        
a5340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
a5350 68 65 20 73 70 65 63 69 66 69 65 64 20 66 69 65  he specified fie
a5360 6c 64 20 77 69 64 74 68 2e 20 20 54 68 69 73 20  ld width.  This 
a5370 69 73 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  is.    **       
a5380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5390 20 20 20 20 20 20 20 20 61 6c 77 61 79 73 20 6e          always n
a53a0 6f 6e 2d 6e 65 67 61 74 69 76 65 2e 20 20 5a 65  on-negative.  Ze
a53b0 72 6f 20 69 73 20 74 68 65 20 64 65 66 61 75 6c  ro is the defaul
a53c0 74 2e 0a 20 20 20 20 2a 2a 20 20 20 70 72 65 63  t..    **   prec
a53d0 69 73 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20  ision           
a53e0 20 20 20 20 20 20 20 20 54 68 65 20 73 70 65 63          The spec
a53f0 69 66 69 65 64 20 70 72 65 63 69 73 69 6f 6e 2e  ified precision.
a5400 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a 20 20    The default.  
a5410 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
a5420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5430 20 20 20 69 73 20 2d 31 2e 0a 20 20 20 20 2a 2a     is -1..    **
a5440 20 20 20 78 74 79 70 65 20 20 20 20 20 20 20 20     xtype        
a5450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 54                 T
a5460 68 65 20 63 6c 61 73 73 20 6f 66 20 74 68 65 20  he class of the 
a5470 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 20 20 20 20  conversion..    
a5480 2a 2a 20 20 20 69 6e 66 6f 70 20 20 20 20 20 20  **   infop      
a5490 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a54a0 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
a54b0 61 70 70 72 6f 70 72 69 61 74 65 20 69 6e 66 6f  appropriate info
a54c0 20 73 74 72 75 63 74 2e 0a 20 20 20 20 2a 2f 0a   struct..    */.
a54d0 20 20 20 20 73 77 69 74 63 68 28 20 78 74 79 70      switch( xtyp
a54e0 65 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65 20  e ){.      case 
a54f0 65 74 50 4f 49 4e 54 45 52 3a 0a 20 20 20 20 20  etPOINTER:.     
a5500 20 20 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f 6e 67     flag_longlong
a5510 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29   = sizeof(char*)
a5520 3d 3d 73 69 7a 65 6f 66 28 69 36 34 29 3b 0a 20  ==sizeof(i64);. 
a5530 20 20 20 20 20 20 20 66 6c 61 67 5f 6c 6f 6e 67         flag_long
a5540 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29   = sizeof(char*)
a5550 3d 3d 73 69 7a 65 6f 66 28 6c 6f 6e 67 20 69 6e  ==sizeof(long in
a5560 74 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46  t);.        /* F
a5570 61 6c 6c 20 74 68 72 6f 75 67 68 20 69 6e 74 6f  all through into
a5580 20 74 68 65 20 6e 65 78 74 20 63 61 73 65 20 2a   the next case *
a5590 2f 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 4f  /.      case etO
a55a0 52 44 49 4e 41 4c 3a 0a 20 20 20 20 20 20 63 61  RDINAL:.      ca
a55b0 73 65 20 65 74 52 41 44 49 58 3a 0a 20 20 20 20  se etRADIX:.    
a55c0 20 20 20 20 69 66 28 20 69 6e 66 6f 70 2d 3e 66      if( infop->f
a55d0 6c 61 67 73 20 26 20 46 4c 41 47 5f 53 49 47 4e  lags & FLAG_SIGN
a55e0 45 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ED ){.          
a55f0 69 36 34 20 76 3b 0a 20 20 20 20 20 20 20 20 20  i64 v;.         
a5600 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f   if( flag_longlo
a5610 6e 67 20 29 20 20 20 76 20 3d 20 76 61 5f 61 72  ng )   v = va_ar
a5620 67 28 61 70 2c 69 36 34 29 3b 0a 20 20 20 20 20  g(ap,i64);.     
a5630 20 20 20 20 20 65 6c 73 65 20 69 66 28 20 66 6c       else if( fl
a5640 61 67 5f 6c 6f 6e 67 20 29 20 20 76 20 3d 20 76  ag_long )  v = v
a5650 61 5f 61 72 67 28 61 70 2c 6c 6f 6e 67 20 69 6e  a_arg(ap,long in
a5660 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c  t);.          el
a5670 73 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  se              
a5680 20 20 20 20 76 20 3d 20 76 61 5f 61 72 67 28 61      v = va_arg(a
a5690 70 2c 69 6e 74 29 3b 0a 20 20 20 20 20 20 20 20  p,int);.        
a56a0 20 20 69 66 28 20 76 3c 30 20 29 7b 0a 20 20 20    if( v<0 ){.   
a56b0 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61 6c           longval
a56c0 75 65 20 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20  ue = -v;.       
a56d0 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2d       prefix = '-
a56e0 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  ';.          }el
a56f0 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
a5700 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76 3b 0a 20  longvalue = v;. 
a5710 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66             if( f
a5720 6c 61 67 5f 70 6c 75 73 73 69 67 6e 20 29 20 20  lag_plussign )  
a5730 20 20 20 20 20 20 70 72 65 66 69 78 20 3d 20 27        prefix = '
a5740 2b 27 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +';.            
a5750 65 6c 73 65 20 69 66 28 20 66 6c 61 67 5f 62 6c  else if( flag_bl
a5760 61 6e 6b 73 69 67 6e 20 29 20 20 70 72 65 66 69  anksign )  prefi
a5770 78 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20  x = ' ';.       
a5780 20 20 20 20 20 65 6c 73 65 20 20 20 20 20 20 20       else       
a5790 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a57a0 70 72 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20  prefix = 0;.    
a57b0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a57c0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
a57d0 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 6c 6f   if( flag_longlo
a57e0 6e 67 20 29 20 20 20 6c 6f 6e 67 76 61 6c 75 65  ng )   longvalue
a57f0 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 75 36 34   = va_arg(ap,u64
a5800 29 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73  );.          els
a5810 65 20 69 66 28 20 66 6c 61 67 5f 6c 6f 6e 67 20  e if( flag_long 
a5820 29 20 20 6c 6f 6e 67 76 61 6c 75 65 20 3d 20 76  )  longvalue = v
a5830 61 5f 61 72 67 28 61 70 2c 75 6e 73 69 67 6e 65  a_arg(ap,unsigne
a5840 64 20 6c 6f 6e 67 20 69 6e 74 29 3b 0a 20 20 20  d long int);.   
a5850 20 20 20 20 20 20 20 65 6c 73 65 20 20 20 20 20         else     
a5860 20 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f 6e               lon
a5870 67 76 61 6c 75 65 20 3d 20 76 61 5f 61 72 67 28  gvalue = va_arg(
a5880 61 70 2c 75 6e 73 69 67 6e 65 64 20 69 6e 74 29  ap,unsigned int)
a5890 3b 0a 20 20 20 20 20 20 20 20 20 20 70 72 65 66  ;.          pref
a58a0 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ix = 0;.        
a58b0 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 6c 6f  }.        if( lo
a58c0 6e 67 76 61 6c 75 65 3d 3d 30 20 29 20 66 6c 61  ngvalue==0 ) fla
a58d0 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72 6d 20  g_alternateform 
a58e0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 0;.        if(
a58f0 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 26 26   flag_zeropad &&
a5900 20 70 72 65 63 69 73 69 6f 6e 3c 77 69 64 74 68   precision<width
a5910 2d 28 70 72 65 66 69 78 21 3d 30 29 20 29 7b 0a  -(prefix!=0) ){.
a5920 20 20 20 20 20 20 20 20 20 20 70 72 65 63 69 73            precis
a5930 69 6f 6e 20 3d 20 77 69 64 74 68 2d 28 70 72 65  ion = width-(pre
a5940 66 69 78 21 3d 30 29 3b 0a 20 20 20 20 20 20 20  fix!=0);.       
a5950 20 7d 0a 20 20 20 20 20 20 20 20 62 75 66 70 74   }.        bufpt
a5960 20 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a   = &buf[etBUFSIZ
a5970 45 2d 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 66  E-1];.        if
a5980 28 20 78 74 79 70 65 3d 3d 65 74 4f 52 44 49 4e  ( xtype==etORDIN
a5990 41 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  AL ){.          
a59a0 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
a59b0 72 20 7a 4f 72 64 5b 5d 20 3d 20 22 74 68 73 74  r zOrd[] = "thst
a59c0 6e 64 72 64 22 3b 0a 20 20 20 20 20 20 20 20 20  ndrd";.         
a59d0 20 69 6e 74 20 78 20 3d 20 6c 6f 6e 67 76 61 6c   int x = longval
a59e0 75 65 20 25 20 31 30 3b 0a 20 20 20 20 20 20 20  ue % 10;.       
a59f0 20 20 20 69 66 28 20 78 3e 3d 34 20 7c 7c 20 28     if( x>=4 || (
a5a00 6c 6f 6e 67 76 61 6c 75 65 2f 31 30 29 25 31 30  longvalue/10)%10
a5a10 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==1 ){.         
a5a20 20 20 20 78 20 3d 20 30 3b 0a 20 20 20 20 20 20     x = 0;.      
a5a30 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
a5a40 62 75 66 5b 65 74 42 55 46 53 49 5a 45 2d 33 5d  buf[etBUFSIZE-3]
a5a50 20 3d 20 7a 4f 72 64 5b 78 2a 32 5d 3b 0a 20 20   = zOrd[x*2];.  
a5a60 20 20 20 20 20 20 20 20 62 75 66 5b 65 74 42 55          buf[etBU
a5a70 46 53 49 5a 45 2d 32 5d 20 3d 20 7a 4f 72 64 5b  FSIZE-2] = zOrd[
a5a80 78 2a 32 2b 31 5d 3b 0a 20 20 20 20 20 20 20 20  x*2+1];.        
a5a90 20 20 62 75 66 70 74 20 2d 3d 20 32 3b 0a 20 20    bufpt -= 2;.  
a5aa0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a5ab0 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 69  {.          regi
a5ac0 73 74 65 72 20 63 6f 6e 73 74 20 63 68 61 72 20  ster const char 
a5ad0 2a 63 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 55  *cset;      /* U
a5ae0 73 65 20 72 65 67 69 73 74 65 72 73 20 66 6f 72  se registers for
a5af0 20 73 70 65 65 64 20 2a 2f 0a 20 20 20 20 20 20   speed */.      
a5b00 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e 74      register int
a5b10 20 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20   base;.         
a5b20 20 63 73 65 74 20 3d 20 26 61 44 69 67 69 74 73   cset = &aDigits
a5b30 5b 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d  [infop->charset]
a5b40 3b 0a 20 20 20 20 20 20 20 20 20 20 62 61 73 65  ;.          base
a5b50 20 3d 20 69 6e 66 6f 70 2d 3e 62 61 73 65 3b 0a   = infop->base;.
a5b60 20 20 20 20 20 20 20 20 20 20 64 6f 7b 20 20 20            do{   
a5b70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5b90 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 76 65          /* Conve
a5ba0 72 74 20 74 6f 20 61 73 63 69 69 20 2a 2f 0a 20  rt to ascii */. 
a5bb0 20 20 20 20 20 20 20 20 20 20 20 2a 28 2d 2d 62             *(--b
a5bc0 75 66 70 74 29 20 3d 20 63 73 65 74 5b 6c 6f 6e  ufpt) = cset[lon
a5bd0 67 76 61 6c 75 65 25 62 61 73 65 5d 3b 0a 20 20  gvalue%base];.  
a5be0 20 20 20 20 20 20 20 20 20 20 6c 6f 6e 67 76 61            longva
a5bf0 6c 75 65 20 3d 20 6c 6f 6e 67 76 61 6c 75 65 2f  lue = longvalue/
a5c00 62 61 73 65 3b 0a 20 20 20 20 20 20 20 20 20 20  base;.          
a5c10 7d 77 68 69 6c 65 28 20 6c 6f 6e 67 76 61 6c 75  }while( longvalu
a5c20 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  e>0 );.        }
a5c30 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a5c40 3d 20 26 62 75 66 5b 65 74 42 55 46 53 49 5a 45  = &buf[etBUFSIZE
a5c50 2d 31 5d 2d 62 75 66 70 74 3b 0a 20 20 20 20 20  -1]-bufpt;.     
a5c60 20 20 20 66 6f 72 28 69 64 78 3d 70 72 65 63 69     for(idx=preci
a5c70 73 69 6f 6e 2d 6c 65 6e 67 74 68 3b 20 69 64 78  sion-length; idx
a5c80 3e 30 3b 20 69 64 78 2d 2d 29 7b 0a 20 20 20 20  >0; idx--){.    
a5c90 20 20 20 20 20 20 2a 28 2d 2d 62 75 66 70 74 29        *(--bufpt)
a5ca0 20 3d 20 27 30 27 3b 20 20 20 20 20 20 20 20 20   = '0';         
a5cb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a5cc0 20 20 20 20 2f 2a 20 5a 65 72 6f 20 70 61 64 20      /* Zero pad 
a5cd0 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  */.        }.   
a5ce0 20 20 20 20 20 69 66 28 20 70 72 65 66 69 78 20       if( prefix 
a5cf0 29 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 70  ) *(--bufpt) = p
a5d00 72 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20  refix;          
a5d10 20 20 20 20 20 2f 2a 20 41 64 64 20 73 69 67 6e       /* Add sign
a5d20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
a5d30 66 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f  flag_alternatefo
a5d40 72 6d 20 26 26 20 69 6e 66 6f 70 2d 3e 70 72 65  rm && infop->pre
a5d50 66 69 78 20 29 7b 20 20 20 20 20 20 2f 2a 20 41  fix ){      /* A
a5d60 64 64 20 22 30 22 20 6f 72 20 22 30 78 22 20 2a  dd "0" or "0x" *
a5d70 2f 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73  /.          cons
a5d80 74 20 63 68 61 72 20 2a 70 72 65 3b 0a 20 20 20  t char *pre;.   
a5d90 20 20 20 20 20 20 20 63 68 61 72 20 78 3b 0a 20         char x;. 
a5da0 20 20 20 20 20 20 20 20 20 70 72 65 20 3d 20 26           pre = &
a5db0 61 50 72 65 66 69 78 5b 69 6e 66 6f 70 2d 3e 70  aPrefix[infop->p
a5dc0 72 65 66 69 78 5d 3b 0a 20 20 20 20 20 20 20 20  refix];.        
a5dd0 20 20 66 6f 72 28 3b 20 28 78 3d 28 2a 70 72 65    for(; (x=(*pre
a5de0 29 29 21 3d 30 3b 20 70 72 65 2b 2b 29 20 2a 28  ))!=0; pre++) *(
a5df0 2d 2d 62 75 66 70 74 29 20 3d 20 78 3b 0a 20 20  --bufpt) = x;.  
a5e00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a5e10 6c 65 6e 67 74 68 20 3d 20 26 62 75 66 5b 65 74  length = &buf[et
a5e20 42 55 46 53 49 5a 45 2d 31 5d 2d 62 75 66 70 74  BUFSIZE-1]-bufpt
a5e30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
a5e40 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 46 4c  .      case etFL
a5e50 4f 41 54 3a 0a 20 20 20 20 20 20 63 61 73 65 20  OAT:.      case 
a5e60 65 74 45 58 50 3a 0a 20 20 20 20 20 20 63 61 73  etEXP:.      cas
a5e70 65 20 65 74 47 45 4e 45 52 49 43 3a 0a 20 20 20  e etGENERIC:.   
a5e80 20 20 20 20 20 72 65 61 6c 76 61 6c 75 65 20 3d       realvalue =
a5e90 20 76 61 5f 61 72 67 28 61 70 2c 64 6f 75 62 6c   va_arg(ap,doubl
a5ea0 65 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  e);.#ifndef SQLI
a5eb0 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47  TE_OMIT_FLOATING
a5ec0 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 20 20 69  _POINT.        i
a5ed0 66 28 20 70 72 65 63 69 73 69 6f 6e 3c 30 20 29  f( precision<0 )
a5ee0 20 70 72 65 63 69 73 69 6f 6e 20 3d 20 36 3b 20   precision = 6; 
a5ef0 20 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 64          /* Set d
a5f00 65 66 61 75 6c 74 20 70 72 65 63 69 73 69 6f 6e  efault precision
a5f10 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
a5f20 70 72 65 63 69 73 69 6f 6e 3e 65 74 42 55 46 53  precision>etBUFS
a5f30 49 5a 45 2f 32 2d 31 30 20 29 20 70 72 65 63 69  IZE/2-10 ) preci
a5f40 73 69 6f 6e 20 3d 20 65 74 42 55 46 53 49 5a 45  sion = etBUFSIZE
a5f50 2f 32 2d 31 30 3b 0a 20 20 20 20 20 20 20 20 69  /2-10;.        i
a5f60 66 28 20 72 65 61 6c 76 61 6c 75 65 3c 30 2e 30  f( realvalue<0.0
a5f70 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
a5f80 61 6c 76 61 6c 75 65 20 3d 20 2d 72 65 61 6c 76  alvalue = -realv
a5f90 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20  alue;.          
a5fa0 70 72 65 66 69 78 20 3d 20 27 2d 27 3b 0a 20 20  prefix = '-';.  
a5fb0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a5fc0 20 20 20 20 20 20 20 69 66 28 20 66 6c 61 67 5f         if( flag_
a5fd0 70 6c 75 73 73 69 67 6e 20 29 20 20 20 20 20 20  plussign )      
a5fe0 20 20 20 20 70 72 65 66 69 78 20 3d 20 27 2b 27      prefix = '+'
a5ff0 3b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 73 65  ;.          else
a6000 20 69 66 28 20 66 6c 61 67 5f 62 6c 61 6e 6b 73   if( flag_blanks
a6010 69 67 6e 20 29 20 20 20 20 70 72 65 66 69 78 20  ign )    prefix 
a6020 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 20 20 20  = ' ';.         
a6030 20 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20   else           
a6040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 72                pr
a6050 65 66 69 78 20 3d 20 30 3b 0a 20 20 20 20 20 20  efix = 0;.      
a6060 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
a6070 78 74 79 70 65 3d 3d 65 74 47 45 4e 45 52 49 43  xtype==etGENERIC
a6080 20 26 26 20 70 72 65 63 69 73 69 6f 6e 3e 30 20   && precision>0 
a6090 29 20 70 72 65 63 69 73 69 6f 6e 2d 2d 3b 0a 23  ) precision--;.#
a60a0 69 66 20 30 0a 20 20 20 20 20 20 20 20 2f 2a 20  if 0.        /* 
a60b0 52 6f 75 6e 64 69 6e 67 20 77 6f 72 6b 73 20 6c  Rounding works l
a60c0 69 6b 65 20 42 53 44 20 77 68 65 6e 20 74 68 65  ike BSD when the
a60d0 20 63 6f 6e 73 74 61 6e 74 20 30 2e 34 39 39 39   constant 0.4999
a60e0 20 69 73 20 75 73 65 64 2e 20 20 57 69 65 72 64   is used.  Wierd
a60f0 21 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72  ! */.        for
a6100 28 69 64 78 3d 70 72 65 63 69 73 69 6f 6e 2c 20  (idx=precision, 
a6110 72 6f 75 6e 64 65 72 3d 30 2e 34 39 39 39 3b 20  rounder=0.4999; 
a6120 69 64 78 3e 30 3b 20 69 64 78 2d 2d 2c 20 72 6f  idx>0; idx--, ro
a6130 75 6e 64 65 72 2a 3d 30 2e 31 29 3b 0a 23 65 6c  under*=0.1);.#el
a6140 73 65 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 74  se.        /* It
a6150 20 6d 61 6b 65 73 20 6d 6f 72 65 20 73 65 6e 73   makes more sens
a6160 65 20 74 6f 20 75 73 65 20 30 2e 35 20 2a 2f 0a  e to use 0.5 */.
a6170 20 20 20 20 20 20 20 20 66 6f 72 28 69 64 78 3d          for(idx=
a6180 70 72 65 63 69 73 69 6f 6e 2c 20 72 6f 75 6e 64  precision, round
a6190 65 72 3d 30 2e 35 3b 20 69 64 78 3e 30 3b 20 69  er=0.5; idx>0; i
a61a0 64 78 2d 2d 2c 20 72 6f 75 6e 64 65 72 2a 3d 30  dx--, rounder*=0
a61b0 2e 31 29 7b 7d 0a 23 65 6e 64 69 66 0a 20 20 20  .1){}.#endif.   
a61c0 20 20 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d       if( xtype==
a61d0 65 74 46 4c 4f 41 54 20 29 20 72 65 61 6c 76 61  etFLOAT ) realva
a61e0 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65 72 3b 0a  lue += rounder;.
a61f0 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 72 6d 61          /* Norma
a6200 6c 69 7a 65 20 72 65 61 6c 76 61 6c 75 65 20 74  lize realvalue t
a6210 6f 20 77 69 74 68 69 6e 20 31 30 2e 30 20 3e 20  o within 10.0 > 
a6220 72 65 61 6c 76 61 6c 75 65 20 3e 3d 20 31 2e 30  realvalue >= 1.0
a6230 20 2a 2f 0a 20 20 20 20 20 20 20 20 65 78 70 20   */.        exp 
a6240 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  = 0;.        if(
a6250 20 73 71 6c 69 74 65 33 49 73 4e 61 4e 28 72 65   sqlite3IsNaN(re
a6260 61 6c 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20  alvalue) ){.    
a6270 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 22 4e        bufpt = "N
a6280 61 4e 22 3b 0a 20 20 20 20 20 20 20 20 20 20 6c  aN";.          l
a6290 65 6e 67 74 68 20 3d 20 33 3b 0a 20 20 20 20 20  ength = 3;.     
a62a0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
a62b0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
a62c0 28 20 72 65 61 6c 76 61 6c 75 65 3e 30 2e 30 20  ( realvalue>0.0 
a62d0 29 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69  ){.          whi
a62e0 6c 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31  le( realvalue>=1
a62f0 65 33 32 20 26 26 20 65 78 70 3c 3d 33 35 30 20  e32 && exp<=350 
a6300 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20  ){ realvalue *= 
a6310 31 65 2d 33 32 3b 20 65 78 70 2b 3d 33 32 3b 20  1e-32; exp+=32; 
a6320 7d 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  }.          whil
a6330 65 28 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 65  e( realvalue>=1e
a6340 38 20 26 26 20 65 78 70 3c 3d 33 35 30 20 29 7b  8 && exp<=350 ){
a6350 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d 20 31 65   realvalue *= 1e
a6360 2d 38 3b 20 65 78 70 2b 3d 38 3b 20 7d 0a 20 20  -8; exp+=8; }.  
a6370 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 72          while( r
a6380 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30 20 26  ealvalue>=10.0 &
a6390 26 20 65 78 70 3c 3d 33 35 30 20 29 7b 20 72 65  & exp<=350 ){ re
a63a0 61 6c 76 61 6c 75 65 20 2a 3d 20 30 2e 31 3b 20  alvalue *= 0.1; 
a63b0 65 78 70 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20  exp++; }.       
a63c0 20 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61     while( realva
a63d0 6c 75 65 3c 31 65 2d 38 20 29 7b 20 72 65 61 6c  lue<1e-8 ){ real
a63e0 76 61 6c 75 65 20 2a 3d 20 31 65 38 3b 20 65 78  value *= 1e8; ex
a63f0 70 2d 3d 38 3b 20 7d 0a 20 20 20 20 20 20 20 20  p-=8; }.        
a6400 20 20 77 68 69 6c 65 28 20 72 65 61 6c 76 61 6c    while( realval
a6410 75 65 3c 31 2e 30 20 29 7b 20 72 65 61 6c 76 61  ue<1.0 ){ realva
a6420 6c 75 65 20 2a 3d 20 31 30 2e 30 3b 20 65 78 70  lue *= 10.0; exp
a6430 2d 2d 3b 20 7d 0a 20 20 20 20 20 20 20 20 20 20  --; }.          
a6440 69 66 28 20 65 78 70 3e 33 35 30 20 29 7b 0a 20  if( exp>350 ){. 
a6450 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70             if( p
a6460 72 65 66 69 78 3d 3d 27 2d 27 20 29 7b 0a 20 20  refix=='-' ){.  
a6470 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 70              bufp
a6480 74 20 3d 20 22 2d 49 6e 66 22 3b 0a 20 20 20 20  t = "-Inf";.    
a6490 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66          }else if
a64a0 28 20 70 72 65 66 69 78 3d 3d 27 2b 27 20 29 7b  ( prefix=='+' ){
a64b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62  .              b
a64c0 75 66 70 74 20 3d 20 22 2b 49 6e 66 22 3b 0a 20  ufpt = "+Inf";. 
a64d0 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65             }else
a64e0 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
a64f0 62 75 66 70 74 20 3d 20 22 49 6e 66 22 3b 0a 20  bufpt = "Inf";. 
a6500 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
a6510 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
a6520 3d 20 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b  = strlen(bufpt);
a6530 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65  .            bre
a6540 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ak;.          }.
a6550 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a6560 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20    bufpt = buf;. 
a6570 20 20 20 20 20 20 20 2f 2a 0a 20 20 20 20 20 20         /*.      
a6580 20 20 2a 2a 20 49 66 20 74 68 65 20 66 69 65 6c    ** If the fiel
a6590 64 20 74 79 70 65 20 69 73 20 65 74 47 45 4e 45  d type is etGENE
a65a0 52 49 43 2c 20 74 68 65 6e 20 63 6f 6e 76 65 72  RIC, then conver
a65b0 74 20 74 6f 20 65 69 74 68 65 72 20 65 74 45 58  t to either etEX
a65c0 50 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20  P.        ** or 
a65d0 65 74 46 4c 4f 41 54 2c 20 61 73 20 61 70 70 72  etFLOAT, as appr
a65e0 6f 70 72 69 61 74 65 2e 0a 20 20 20 20 20 20 20  opriate..       
a65f0 20 2a 2f 0a 20 20 20 20 20 20 20 20 66 6c 61 67   */.        flag
a6600 5f 65 78 70 20 3d 20 78 74 79 70 65 3d 3d 65 74  _exp = xtype==et
a6610 45 58 50 3b 0a 20 20 20 20 20 20 20 20 69 66 28  EXP;.        if(
a6620 20 78 74 79 70 65 21 3d 65 74 46 4c 4f 41 54 20   xtype!=etFLOAT 
a6630 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 61  ){.          rea
a6640 6c 76 61 6c 75 65 20 2b 3d 20 72 6f 75 6e 64 65  lvalue += rounde
a6650 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  r;.          if(
a6660 20 72 65 61 6c 76 61 6c 75 65 3e 3d 31 30 2e 30   realvalue>=10.0
a6670 20 29 7b 20 72 65 61 6c 76 61 6c 75 65 20 2a 3d   ){ realvalue *=
a6680 20 30 2e 31 3b 20 65 78 70 2b 2b 3b 20 7d 0a 20   0.1; exp++; }. 
a6690 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a66a0 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74 47 45   if( xtype==etGE
a66b0 4e 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20 20  NERIC ){.       
a66c0 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 21 66     flag_rtz = !f
a66d0 6c 61 67 5f 61 6c 74 65 72 6e 61 74 65 66 6f 72  lag_alternatefor
a66e0 6d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  m;.          if(
a66f0 20 65 78 70 3c 2d 34 20 7c 7c 20 65 78 70 3e 70   exp<-4 || exp>p
a6700 72 65 63 69 73 69 6f 6e 20 29 7b 0a 20 20 20 20  recision ){.    
a6710 20 20 20 20 20 20 20 20 78 74 79 70 65 20 3d 20          xtype = 
a6720 65 74 45 58 50 3b 0a 20 20 20 20 20 20 20 20 20  etEXP;.         
a6730 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a6740 20 20 20 20 70 72 65 63 69 73 69 6f 6e 20 3d 20      precision = 
a6750 70 72 65 63 69 73 69 6f 6e 20 2d 20 65 78 70 3b  precision - exp;
a6760 0a 20 20 20 20 20 20 20 20 20 20 20 20 78 74 79  .            xty
a6770 70 65 20 3d 20 65 74 46 4c 4f 41 54 3b 0a 20 20  pe = etFLOAT;.  
a6780 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a6790 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a67a0 20 20 20 66 6c 61 67 5f 72 74 7a 20 3d 20 30 3b     flag_rtz = 0;
a67b0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a67c0 20 20 20 69 66 28 20 78 74 79 70 65 3d 3d 65 74     if( xtype==et
a67d0 45 58 50 20 29 7b 0a 20 20 20 20 20 20 20 20 20  EXP ){.         
a67e0 20 65 32 20 3d 20 30 3b 0a 20 20 20 20 20 20 20   e2 = 0;.       
a67f0 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a6800 20 20 65 32 20 3d 20 65 78 70 3b 0a 20 20 20 20    e2 = exp;.    
a6810 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e 73      }.        ns
a6820 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 66  d = 0;.        f
a6830 6c 61 67 5f 64 70 20 3d 20 28 70 72 65 63 69 73  lag_dp = (precis
a6840 69 6f 6e 3e 30 29 20 7c 20 66 6c 61 67 5f 61 6c  ion>0) | flag_al
a6850 74 65 72 6e 61 74 65 66 6f 72 6d 20 7c 20 66 6c  ternateform | fl
a6860 61 67 5f 61 6c 74 66 6f 72 6d 32 3b 0a 20 20 20  ag_altform2;.   
a6870 20 20 20 20 20 2f 2a 20 54 68 65 20 73 69 67 6e       /* The sign
a6880 20 69 6e 20 66 72 6f 6e 74 20 6f 66 20 74 68 65   in front of the
a6890 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20   number */.     
a68a0 20 20 20 69 66 28 20 70 72 65 66 69 78 20 29 7b     if( prefix ){
a68b0 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
a68c0 70 74 2b 2b 29 20 3d 20 70 72 65 66 69 78 3b 0a  pt++) = prefix;.
a68d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a68e0 20 20 2f 2a 20 44 69 67 69 74 73 20 70 72 69 6f    /* Digits prio
a68f0 72 20 74 6f 20 74 68 65 20 64 65 63 69 6d 61 6c  r to the decimal
a6900 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 20 20 20 20   point */.      
a6910 20 20 69 66 28 20 65 32 3c 30 20 29 7b 0a 20 20    if( e2<0 ){.  
a6920 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b          *(bufpt+
a6930 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20 20  +) = '0';.      
a6940 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
a6950 20 20 20 66 6f 72 28 3b 20 65 32 3e 3d 30 3b 20     for(; e2>=0; 
a6960 65 32 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20  e2--){.         
a6970 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a6980 65 74 5f 67 65 74 64 69 67 69 74 28 26 72 65 61  et_getdigit(&rea
a6990 6c 76 61 6c 75 65 2c 26 6e 73 64 29 3b 0a 20 20  lvalue,&nsd);.  
a69a0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
a69b0 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20 54    }.        /* T
a69c0 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74  he decimal point
a69d0 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20   */.        if( 
a69e0 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20 20 20 20  flag_dp ){.     
a69f0 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20       *(bufpt++) 
a6a00 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20 20 20 7d  = '.';.        }
a6a10 0a 20 20 20 20 20 20 20 20 2f 2a 20 22 30 22 20  .        /* "0" 
a6a20 64 69 67 69 74 73 20 61 66 74 65 72 20 74 68 65  digits after the
a6a30 20 64 65 63 69 6d 61 6c 20 70 6f 69 6e 74 20 62   decimal point b
a6a40 75 74 20 62 65 66 6f 72 65 20 74 68 65 20 66 69  ut before the fi
a6a50 72 73 74 0a 20 20 20 20 20 20 20 20 2a 2a 20 73  rst.        ** s
a6a60 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69 74  ignificant digit
a6a70 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20 2a   of the number *
a6a80 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 65 32  /.        for(e2
a6a90 2b 2b 3b 20 65 32 3c 30 3b 20 70 72 65 63 69 73  ++; e2<0; precis
a6aa0 69 6f 6e 2d 2d 2c 20 65 32 2b 2b 29 7b 0a 20 20  ion--, e2++){.  
a6ab0 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
a6ac0 70 72 65 63 69 73 69 6f 6e 3e 30 20 29 3b 0a 20  precision>0 );. 
a6ad0 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70 74           *(bufpt
a6ae0 2b 2b 29 20 3d 20 27 30 27 3b 0a 20 20 20 20 20  ++) = '0';.     
a6af0 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
a6b00 53 69 67 6e 69 66 69 63 61 6e 74 20 64 69 67 69  Significant digi
a6b10 74 73 20 61 66 74 65 72 20 74 68 65 20 64 65 63  ts after the dec
a6b20 69 6d 61 6c 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  imal point */.  
a6b30 20 20 20 20 20 20 77 68 69 6c 65 28 20 28 70 72        while( (pr
a6b40 65 63 69 73 69 6f 6e 2d 2d 29 3e 30 20 29 7b 0a  ecision--)>0 ){.
a6b50 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66 70            *(bufp
a6b60 74 2b 2b 29 20 3d 20 65 74 5f 67 65 74 64 69 67  t++) = et_getdig
a6b70 69 74 28 26 72 65 61 6c 76 61 6c 75 65 2c 26 6e  it(&realvalue,&n
a6b80 73 64 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  sd);.        }. 
a6b90 20 20 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65         /* Remove
a6ba0 20 74 72 61 69 6c 69 6e 67 20 7a 65 72 6f 73 20   trailing zeros 
a6bb0 61 6e 64 20 74 68 65 20 22 2e 22 20 69 66 20 6e  and the "." if n
a6bc0 6f 20 64 69 67 69 74 73 20 66 6f 6c 6c 6f 77 20  o digits follow 
a6bd0 74 68 65 20 22 2e 22 20 2a 2f 0a 20 20 20 20 20  the "." */.     
a6be0 20 20 20 69 66 28 20 66 6c 61 67 5f 72 74 7a 20     if( flag_rtz 
a6bf0 26 26 20 66 6c 61 67 5f 64 70 20 29 7b 0a 20 20  && flag_dp ){.  
a6c00 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 62          while( b
a6c10 75 66 70 74 5b 2d 31 5d 3d 3d 27 30 27 20 29 20  ufpt[-1]=='0' ) 
a6c20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b 0a  *(--bufpt) = 0;.
a6c30 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
a6c40 28 20 62 75 66 70 74 3e 62 75 66 20 29 3b 0a 20  ( bufpt>buf );. 
a6c50 20 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66           if( buf
a6c60 70 74 5b 2d 31 5d 3d 3d 27 2e 27 20 29 7b 0a 20  pt[-1]=='.' ){. 
a6c70 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 66             if( f
a6c80 6c 61 67 5f 61 6c 74 66 6f 72 6d 32 20 29 7b 0a  lag_altform2 ){.
a6c90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 28                *(
a6ca0 62 75 66 70 74 2b 2b 29 20 3d 20 27 30 27 3b 0a  bufpt++) = '0';.
a6cb0 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73              }els
a6cc0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e{.             
a6cd0 20 2a 28 2d 2d 62 75 66 70 74 29 20 3d 20 30 3b   *(--bufpt) = 0;
a6ce0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
a6cf0 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
a6d00 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2f 2a 20     }.        /* 
a6d10 41 64 64 20 74 68 65 20 22 65 4e 4e 4e 22 20 73  Add the "eNNN" s
a6d20 75 66 66 69 78 20 2a 2f 0a 20 20 20 20 20 20 20  uffix */.       
a6d30 20 69 66 28 20 66 6c 61 67 5f 65 78 70 20 7c 7c   if( flag_exp ||
a6d40 20 78 74 79 70 65 3d 3d 65 74 45 58 50 20 29 7b   xtype==etEXP ){
a6d50 0a 20 20 20 20 20 20 20 20 20 20 2a 28 62 75 66  .          *(buf
a6d60 70 74 2b 2b 29 20 3d 20 61 44 69 67 69 74 73 5b  pt++) = aDigits[
a6d70 69 6e 66 6f 70 2d 3e 63 68 61 72 73 65 74 5d 3b  infop->charset];
a6d80 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 65  .          if( e
a6d90 78 70 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  xp<0 ){.        
a6da0 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d      *(bufpt++) =
a6db0 20 27 2d 27 3b 20 65 78 70 20 3d 20 2d 65 78 70   '-'; exp = -exp
a6dc0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
a6dd0 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e{.            *
a6de0 28 62 75 66 70 74 2b 2b 29 20 3d 20 27 2b 27 3b  (bufpt++) = '+';
a6df0 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
a6e00 20 20 20 20 20 20 20 69 66 28 20 65 78 70 3e 3d         if( exp>=
a6e10 31 30 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  100 ){.         
a6e20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29 20 3d 20     *(bufpt++) = 
a6e30 28 65 78 70 2f 31 30 30 29 2b 27 30 27 3b 20 20  (exp/100)+'0';  
a6e40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
a6e50 20 31 30 30 27 73 20 64 69 67 69 74 20 2a 2f 0a   100's digit */.
a6e60 20 20 20 20 20 20 20 20 20 20 20 20 65 78 70 20              exp 
a6e70 25 3d 20 31 30 30 3b 0a 20 20 20 20 20 20 20 20  %= 100;.        
a6e80 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 2a 28    }.          *(
a6e90 62 75 66 70 74 2b 2b 29 20 3d 20 65 78 70 2f 31  bufpt++) = exp/1
a6ea0 30 2b 27 30 27 3b 20 20 20 20 20 20 20 20 20 20  0+'0';          
a6eb0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31 30             /* 10
a6ec0 27 73 20 64 69 67 69 74 20 2a 2f 0a 20 20 20 20  's digit */.    
a6ed0 20 20 20 20 20 20 2a 28 62 75 66 70 74 2b 2b 29        *(bufpt++)
a6ee0 20 3d 20 65 78 70 25 31 30 2b 27 30 27 3b 20 20   = exp%10+'0';  
a6ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
a6f00 20 20 20 2f 2a 20 31 27 73 20 64 69 67 69 74 20     /* 1's digit 
a6f10 2a 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  */.        }.   
a6f20 20 20 20 20 20 2a 62 75 66 70 74 20 3d 20 30 3b       *bufpt = 0;
a6f30 0a 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ..        /* The
a6f40 20 63 6f 6e 76 65 72 74 65 64 20 6e 75 6d 62 65   converted numbe
a6f50 72 20 69 73 20 69 6e 20 62 75 66 5b 5d 20 61 6e  r is in buf[] an
a6f60 64 20 7a 65 72 6f 20 74 65 72 6d 69 6e 61 74 65  d zero terminate
a6f70 64 2e 20 4f 75 74 70 75 74 20 69 74 2e 0a 20 20  d. Output it..  
a6f80 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68        ** Note th
a6f90 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20 69 73  at the number is
a6fa0 20 69 6e 20 74 68 65 20 75 73 75 61 6c 20 6f 72   in the usual or
a6fb0 64 65 72 2c 20 6e 6f 74 20 72 65 76 65 72 73 65  der, not reverse
a6fc0 64 20 61 73 20 77 69 74 68 0a 20 20 20 20 20 20  d as with.      
a6fd0 20 20 2a 2a 20 69 6e 74 65 67 65 72 20 63 6f 6e    ** integer con
a6fe0 76 65 72 73 69 6f 6e 73 2e 20 2a 2f 0a 20 20 20  versions. */.   
a6ff0 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 62 75       length = bu
a7000 66 70 74 2d 62 75 66 3b 0a 20 20 20 20 20 20 20  fpt-buf;.       
a7010 20 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 0a 20   bufpt = buf;.. 
a7020 20 20 20 20 20 20 20 2f 2a 20 53 70 65 63 69 61         /* Specia
a7030 6c 20 63 61 73 65 3a 20 20 41 64 64 20 6c 65 61  l case:  Add lea
a7040 64 69 6e 67 20 7a 65 72 6f 73 20 69 66 20 74 68  ding zeros if th
a7050 65 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20 66  e flag_zeropad f
a7060 6c 61 67 20 69 73 0a 20 20 20 20 20 20 20 20 2a  lag is.        *
a7070 2a 20 73 65 74 20 61 6e 64 20 77 65 20 61 72 65  * set and we are
a7080 20 6e 6f 74 20 6c 65 66 74 20 6a 75 73 74 69 66   not left justif
a7090 69 65 64 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ied */.        i
a70a0 66 28 20 66 6c 61 67 5f 7a 65 72 6f 70 61 64 20  f( flag_zeropad 
a70b0 26 26 20 21 66 6c 61 67 5f 6c 65 66 74 6a 75 73  && !flag_leftjus
a70c0 74 69 66 79 20 26 26 20 6c 65 6e 67 74 68 20 3c  tify && length <
a70d0 20 77 69 64 74 68 29 7b 0a 20 20 20 20 20 20 20   width){.       
a70e0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
a70f0 20 20 20 20 69 6e 74 20 6e 50 61 64 20 3d 20 77      int nPad = w
a7100 69 64 74 68 20 2d 20 6c 65 6e 67 74 68 3b 0a 20  idth - length;. 
a7110 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 77           for(i=w
a7120 69 64 74 68 3b 20 69 3e 3d 6e 50 61 64 3b 20 69  idth; i>=nPad; i
a7130 2d 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  --){.           
a7140 20 62 75 66 70 74 5b 69 5d 20 3d 20 62 75 66 70   bufpt[i] = bufp
a7150 74 5b 69 2d 6e 50 61 64 5d 3b 0a 20 20 20 20 20  t[i-nPad];.     
a7160 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
a7170 20 69 20 3d 20 70 72 65 66 69 78 21 3d 30 3b 0a   i = prefix!=0;.
a7180 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65 28            while(
a7190 20 6e 50 61 64 2d 2d 20 29 20 62 75 66 70 74 5b   nPad-- ) bufpt[
a71a0 69 2b 2b 5d 20 3d 20 27 30 27 3b 0a 20 20 20 20  i++] = '0';.    
a71b0 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20 77        length = w
a71c0 69 64 74 68 3b 0a 20 20 20 20 20 20 20 20 7d 0a  idth;.        }.
a71d0 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 62  #endif.        b
a71e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
a71f0 20 65 74 53 49 5a 45 3a 0a 20 20 20 20 20 20 20   etSIZE:.       
a7200 20 2a 28 76 61 5f 61 72 67 28 61 70 2c 69 6e 74   *(va_arg(ap,int
a7210 2a 29 29 20 3d 20 70 41 63 63 75 6d 2d 3e 6e 43  *)) = pAccum->nC
a7220 68 61 72 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e  har;.        len
a7230 67 74 68 20 3d 20 77 69 64 74 68 20 3d 20 30 3b  gth = width = 0;
a7240 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
a7250 20 20 20 20 20 20 63 61 73 65 20 65 74 50 45 52        case etPER
a7260 43 45 4e 54 3a 0a 20 20 20 20 20 20 20 20 62 75  CENT:.        bu
a7270 66 5b 30 5d 20 3d 20 27 25 27 3b 0a 20 20 20 20  f[0] = '%';.    
a7280 20 20 20 20 62 75 66 70 74 20 3d 20 62 75 66 3b      bufpt = buf;
a7290 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20  .        length 
a72a0 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 1;.        bre
a72b0 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65  ak;.      case e
a72c0 74 43 48 41 52 58 3a 0a 20 20 20 20 20 20 20 20  tCHARX:.        
a72d0 63 20 3d 20 62 75 66 5b 30 5d 20 3d 20 76 61 5f  c = buf[0] = va_
a72e0 61 72 67 28 61 70 2c 69 6e 74 29 3b 0a 20 20 20  arg(ap,int);.   
a72f0 20 20 20 20 20 69 66 28 20 70 72 65 63 69 73 69       if( precisi
a7300 6f 6e 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  on>=0 ){.       
a7310 20 20 20 66 6f 72 28 69 64 78 3d 31 3b 20 69 64     for(idx=1; id
a7320 78 3c 70 72 65 63 69 73 69 6f 6e 3b 20 69 64 78  x<precision; idx
a7330 2b 2b 29 20 62 75 66 5b 69 64 78 5d 20 3d 20 63  ++) buf[idx] = c
a7340 3b 0a 20 20 20 20 20 20 20 20 20 20 6c 65 6e 67  ;.          leng
a7350 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 0a  th = precision;.
a7360 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
a7370 20 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20           length 
a7380 3d 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  =1;.        }.  
a7390 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 62 75        bufpt = bu
a73a0 66 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  f;.        break
a73b0 3b 0a 20 20 20 20 20 20 63 61 73 65 20 65 74 53  ;.      case etS
a73c0 54 52 49 4e 47 3a 0a 20 20 20 20 20 20 63 61 73  TRING:.      cas
a73d0 65 20 65 74 44 59 4e 53 54 52 49 4e 47 3a 0a 20  e etDYNSTRING:. 
a73e0 20 20 20 20 20 20 20 62 75 66 70 74 20 3d 20 76         bufpt = v
a73f0 61 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b  a_arg(ap,char*);
a7400 0a 20 20 20 20 20 20 20 20 69 66 28 20 62 75 66  .        if( buf
a7410 70 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pt==0 ){.       
a7420 20 20 20 62 75 66 70 74 20 3d 20 22 22 3b 0a 20     bufpt = "";. 
a7430 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
a7440 20 78 74 79 70 65 3d 3d 65 74 44 59 4e 53 54 52   xtype==etDYNSTR
a7450 49 4e 47 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ING ){.         
a7460 20 7a 45 78 74 72 61 20 3d 20 62 75 66 70 74 3b   zExtra = bufpt;
a7470 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
a7480 20 20 20 69 66 28 20 70 72 65 63 69 73 69 6f 6e     if( precision
a7490 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  >=0 ){.         
a74a0 20 66 6f 72 28 6c 65 6e 67 74 68 3d 30 3b 20 6c   for(length=0; l
a74b0 65 6e 67 74 68 3c 70 72 65 63 69 73 69 6f 6e 20  ength<precision 
a74c0 26 26 20 62 75 66 70 74 5b 6c 65 6e 67 74 68 5d  && bufpt[length]
a74d0 3b 20 6c 65 6e 67 74 68 2b 2b 29 7b 7d 0a 20 20  ; length++){}.  
a74e0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
a74f0 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a7500 73 74 72 6c 65 6e 28 62 75 66 70 74 29 3b 0a 20  strlen(bufpt);. 
a7510 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
a7520 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
a7530 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 3a 0a  se etSQLESCAPE:.
a7540 20 20 20 20 20 20 63 61 73 65 20 65 74 53 51 4c        case etSQL
a7550 45 53 43 41 50 45 32 3a 0a 20 20 20 20 20 20 63  ESCAPE2:.      c
a7560 61 73 65 20 65 74 53 51 4c 45 53 43 41 50 45 33  ase etSQLESCAPE3
a7570 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
a7580 69 2c 20 6a 2c 20 6e 2c 20 63 68 2c 20 69 73 6e  i, j, n, ch, isn
a7590 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69 6e 74  ull;.        int
a75a0 20 6e 65 65 64 51 75 6f 74 65 3b 0a 20 20 20 20   needQuote;.    
a75b0 20 20 20 20 63 68 61 72 20 71 20 3d 20 28 28 78      char q = ((x
a75c0 74 79 70 65 3d 3d 65 74 53 51 4c 45 53 43 41 50  type==etSQLESCAP
a75d0 45 33 29 3f 27 22 27 3a 27 5c 27 27 29 3b 20 20  E3)?'"':'\'');  
a75e0 20 2f 2a 20 51 75 6f 74 65 20 63 68 61 72 61 63   /* Quote charac
a75f0 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 20 20 63  ter */.        c
a7600 68 61 72 20 2a 65 73 63 61 72 67 20 3d 20 76 61  har *escarg = va
a7610 5f 61 72 67 28 61 70 2c 63 68 61 72 2a 29 3b 0a  _arg(ap,char*);.
a7620 20 20 20 20 20 20 20 20 69 73 6e 75 6c 6c 20 3d          isnull =
a7630 20 65 73 63 61 72 67 3d 3d 30 3b 0a 20 20 20 20   escarg==0;.    
a7640 20 20 20 20 69 66 28 20 69 73 6e 75 6c 6c 20 29      if( isnull )
a7650 20 65 73 63 61 72 67 20 3d 20 28 78 74 79 70 65   escarg = (xtype
a7660 3d 3d 65 74 53 51 4c 45 53 43 41 50 45 32 20 3f  ==etSQLESCAPE2 ?
a7670 20 22 4e 55 4c 4c 22 20 3a 20 22 28 4e 55 4c 4c   "NULL" : "(NULL
a7680 29 22 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  )");.        for
a7690 28 69 3d 6e 3d 30 3b 20 28 63 68 3d 65 73 63 61  (i=n=0; (ch=esca
a76a0 72 67 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b  rg[i])!=0; i++){
a76b0 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63  .          if( c
a76c0 68 3d 3d 71 20 29 20 20 6e 2b 2b 3b 0a 20 20 20  h==q )  n++;.   
a76d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6e       }.        n
a76e0 65 65 64 51 75 6f 74 65 20 3d 20 21 69 73 6e 75  eedQuote = !isnu
a76f0 6c 6c 20 26 26 20 78 74 79 70 65 3d 3d 65 74 53  ll && xtype==etS
a7700 51 4c 45 53 43 41 50 45 32 3b 0a 20 20 20 20 20  QLESCAPE2;.     
a7710 20 20 20 6e 20 2b 3d 20 69 20 2b 20 31 20 2b 20     n += i + 1 + 
a7720 6e 65 65 64 51 75 6f 74 65 2a 32 3b 0a 20 20 20  needQuote*2;.   
a7730 20 20 20 20 20 69 66 28 20 6e 3e 65 74 42 55 46       if( n>etBUF
a7740 53 49 5a 45 20 29 7b 0a 20 20 20 20 20 20 20 20  SIZE ){.        
a7750 20 20 62 75 66 70 74 20 3d 20 7a 45 78 74 72 61    bufpt = zExtra
a7760 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
a7770 28 20 6e 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ( n );.         
a7780 20 69 66 28 20 62 75 66 70 74 3d 3d 30 20 29 7b   if( bufpt==0 ){
a7790 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 41 63  .            pAc
a77a0 63 75 6d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  cum->mallocFaile
a77b0 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20  d = 1;.         
a77c0 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
a77d0 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
a77e0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
a77f0 62 75 66 70 74 20 3d 20 62 75 66 3b 0a 20 20 20  bufpt = buf;.   
a7800 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 6a       }.        j
a7810 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66   = 0;.        if
a7820 28 20 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75  ( needQuote ) bu
a7830 66 70 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20  fpt[j++] = q;.  
a7840 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 28        for(i=0; (
a7850 63 68 3d 65 73 63 61 72 67 5b 69 5d 29 21 3d 30  ch=escarg[i])!=0
a7860 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
a7870 20 20 62 75 66 70 74 5b 6a 2b 2b 5d 20 3d 20 63    bufpt[j++] = c
a7880 68 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  h;.          if(
a7890 20 63 68 3d 3d 71 20 29 20 62 75 66 70 74 5b 6a   ch==q ) bufpt[j
a78a0 2b 2b 5d 20 3d 20 63 68 3b 0a 20 20 20 20 20 20  ++] = ch;.      
a78b0 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
a78c0 6e 65 65 64 51 75 6f 74 65 20 29 20 62 75 66 70  needQuote ) bufp
a78d0 74 5b 6a 2b 2b 5d 20 3d 20 71 3b 0a 20 20 20 20  t[j++] = q;.    
a78e0 20 20 20 20 62 75 66 70 74 5b 6a 5d 20 3d 20 30      bufpt[j] = 0
a78f0 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 67 74 68  ;.        length
a7900 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20 2f 2a   = j;.        /*
a7910 20 54 68 65 20 70 72 65 63 69 73 69 6f 6e 20 69   The precision i
a7920 73 20 69 67 6e 6f 72 65 64 20 6f 6e 20 25 71 20  s ignored on %q 
a7930 61 6e 64 20 25 51 20 2a 2f 0a 20 20 20 20 20 20  and %Q */.      
a7940 20 20 2f 2a 20 69 66 28 20 70 72 65 63 69 73 69    /* if( precisi
a7950 6f 6e 3e 3d 30 20 26 26 20 70 72 65 63 69 73 69  on>=0 && precisi
a7960 6f 6e 3c 6c 65 6e 67 74 68 20 29 20 6c 65 6e 67  on<length ) leng
a7970 74 68 20 3d 20 70 72 65 63 69 73 69 6f 6e 3b 20  th = precision; 
a7980 2a 2f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  */.        break
a7990 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
a79a0 63 61 73 65 20 65 74 54 4f 4b 45 4e 3a 20 7b 0a  case etTOKEN: {.
a79b0 20 20 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a 70          Token *p
a79c0 54 6f 6b 65 6e 20 3d 20 76 61 5f 61 72 67 28 61  Token = va_arg(a
a79d0 70 2c 20 54 6f 6b 65 6e 2a 29 3b 0a 20 20 20 20  p, Token*);.    
a79e0 20 20 20 20 69 66 28 20 70 54 6f 6b 65 6e 20 29      if( pToken )
a79f0 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
a7a00 74 65 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e  te3StrAccumAppen
a7a10 64 28 70 41 63 63 75 6d 2c 20 28 63 6f 6e 73 74  d(pAccum, (const
a7a20 20 63 68 61 72 2a 29 70 54 6f 6b 65 6e 2d 3e 7a   char*)pToken->z
a7a30 2c 20 70 54 6f 6b 65 6e 2d 3e 6e 29 3b 0a 20 20  , pToken->n);.  
a7a40 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
a7a50 6c 65 6e 67 74 68 20 3d 20 77 69 64 74 68 20 3d  length = width =
a7a60 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   0;.        brea
a7a70 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
a7a80 20 63 61 73 65 20 65 74 53 52 43 4c 49 53 54 3a   case etSRCLIST:
a7a90 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63 4c 69   {.        SrcLi
a7aa0 73 74 20 2a 70 53 72 63 20 3d 20 76 61 5f 61 72  st *pSrc = va_ar
a7ab0 67 28 61 70 2c 20 53 72 63 4c 69 73 74 2a 29 3b  g(ap, SrcList*);
a7ac0 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6b 20 3d  .        int k =
a7ad0 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
a7ae0 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
a7af0 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
a7b00 49 74 65 6d 20 3d 20 26 70 53 72 63 2d 3e 61 5b  Item = &pSrc->a[
a7b10 6b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  k];.        asse
a7b20 72 74 28 20 6b 3e 3d 30 20 26 26 20 6b 3c 70 53  rt( k>=0 && k<pS
a7b30 72 63 2d 3e 6e 53 72 63 20 29 3b 0a 20 20 20 20  rc->nSrc );.    
a7b40 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 7a      if( pItem->z
a7b50 44 61 74 61 62 61 73 65 20 29 7b 0a 20 20 20 20  Database ){.    
a7b60 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72        sqlite3Str
a7b70 41 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63  AccumAppend(pAcc
a7b80 75 6d 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61  um, pItem->zData
a7b90 62 61 73 65 2c 20 2d 31 29 3b 0a 20 20 20 20 20  base, -1);.     
a7ba0 20 20 20 20 20 73 71 6c 69 74 65 33 53 74 72 41       sqlite3StrA
a7bb0 63 63 75 6d 41 70 70 65 6e 64 28 70 41 63 63 75  ccumAppend(pAccu
a7bc0 6d 2c 20 22 2e 22 2c 20 31 29 3b 0a 20 20 20 20  m, ".", 1);.    
a7bd0 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71      }.        sq
a7be0 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70 70  lite3StrAccumApp
a7bf0 65 6e 64 28 70 41 63 63 75 6d 2c 20 70 49 74 65  end(pAccum, pIte
a7c00 6d 2d 3e 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20  m->zName, -1);. 
a7c10 20 20 20 20 20 20 20 6c 65 6e 67 74 68 20 3d 20         length = 
a7c20 77 69 64 74 68 20 3d 20 30 3b 0a 20 20 20 20 20  width = 0;.     
a7c30 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
a7c40 7d 0a 20 20 20 20 7d 2f 2a 20 45 6e 64 20 73 77  }.    }/* End sw
a7c50 69 74 63 68 20 6f 76 65 72 20 74 68 65 20 66 6f  itch over the fo
a7c60 72 6d 61 74 20 74 79 70 65 20 2a 2f 0a 20 20 20  rmat type */.   
a7c70 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 74   /*.    ** The t
a7c80 65 78 74 20 6f 66 20 74 68 65 20 63 6f 6e 76 65  ext of the conve
a7c90 72 73 69 6f 6e 20 69 73 20 70 6f 69 6e 74 65 64  rsion is pointed
a7ca0 20 74 6f 20 62 79 20 22 62 75 66 70 74 22 20 61   to by "bufpt" a
a7cb0 6e 64 20 69 73 0a 20 20 20 20 2a 2a 20 22 6c 65  nd is.    ** "le
a7cc0 6e 67 74 68 22 20 63 68 61 72 61 63 74 65 72 73  ngth" characters
a7cd0 20 6c 6f 6e 67 2e 20 20 54 68 65 20 66 69 65 6c   long.  The fiel
a7ce0 64 20 77 69 64 74 68 20 69 73 20 22 77 69 64 74  d width is "widt
a7cf0 68 22 2e 20 20 44 6f 0a 20 20 20 20 2a 2a 20 74  h".  Do.    ** t
a7d00 68 65 20 6f 75 74 70 75 74 2e 0a 20 20 20 20 2a  he output..    *
a7d10 2f 0a 20 20 20 20 69 66 28 20 21 66 6c 61 67 5f  /.    if( !flag_
a7d20 6c 65 66 74 6a 75 73 74 69 66 79 20 29 7b 0a 20  leftjustify ){. 
a7d30 20 20 20 20 20 72 65 67 69 73 74 65 72 20 69 6e       register in
a7d40 74 20 6e 73 70 61 63 65 3b 0a 20 20 20 20 20 20  t nspace;.      
a7d50 6e 73 70 61 63 65 20 3d 20 77 69 64 74 68 2d 6c  nspace = width-l
a7d60 65 6e 67 74 68 3b 0a 20 20 20 20 20 20 69 66 28  ength;.      if(
a7d70 20 6e 73 70 61 63 65 3e 30 20 29 7b 0a 20 20 20   nspace>0 ){.   
a7d80 20 20 20 20 20 61 70 70 65 6e 64 53 70 61 63 65       appendSpace
a7d90 28 70 41 63 63 75 6d 2c 20 6e 73 70 61 63 65 29  (pAccum, nspace)
a7da0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
a7db0 20 20 20 20 69 66 28 20 6c 65 6e 67 74 68 3e 30      if( length>0
a7dc0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
a7dd0 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
a7de0 70 41 63 63 75 6d 2c 20 62 75 66 70 74 2c 20 6c  pAccum, bufpt, l
a7df0 65 6e 67 74 68 29 3b 0a 20 20 20 20 7d 0a 20 20  ength);.    }.  
a7e00 20 20 69 66 28 20 66 6c 61 67 5f 6c 65 66 74 6a    if( flag_leftj
a7e10 75 73 74 69 66 79 20 29 7b 0a 20 20 20 20 20 20  ustify ){.      
a7e20 72 65 67 69 73 74 65 72 20 69 6e 74 20 6e 73 70  register int nsp
a7e30 61 63 65 3b 0a 20 20 20 20 20 20 6e 73 70 61 63  ace;.      nspac
a7e40 65 20 3d 20 77 69 64 74 68 2d 6c 65 6e 67 74 68  e = width-length
a7e50 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 73 70 61  ;.      if( nspa
a7e60 63 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ce>0 ){.        
a7e70 61 70 70 65 6e 64 53 70 61 63 65 28 70 41 63 63  appendSpace(pAcc
a7e80 75 6d 2c 20 6e 73 70 61 63 65 29 3b 0a 20 20 20  um, nspace);.   
a7e90 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
a7ea0 66 28 20 7a 45 78 74 72 61 20 29 7b 0a 20 20 20  f( zExtra ){.   
a7eb0 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
a7ec0 7a 45 78 74 72 61 29 3b 0a 20 20 20 20 7d 0a 20  zExtra);.    }. 
a7ed0 20 7d 2f 2a 20 45 6e 64 20 66 6f 72 20 6c 6f 6f   }/* End for loo
a7ee0 70 20 6f 76 65 72 20 74 68 65 20 66 6f 72 6d 61  p over the forma
a7ef0 74 20 73 74 72 69 6e 67 20 2a 2f 0a 7d 20 2f 2a  t string */.} /*
a7f00 20 45 6e 64 20 6f 66 20 66 75 6e 63 74 69 6f 6e   End of function
a7f10 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e   */../*.** Appen
a7f20 64 20 4e 20 62 79 74 65 73 20 6f 66 20 74 65 78  d N bytes of tex
a7f30 74 20 66 72 6f 6d 20 7a 20 74 6f 20 74 68 65 20  t from z to the 
a7f40 53 74 72 41 63 63 75 6d 20 6f 62 6a 65 63 74 2e  StrAccum object.
a7f50 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
a7f60 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53  TE void sqlite3S
a7f70 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28 53 74  trAccumAppend(St
a7f80 72 41 63 63 75 6d 20 2a 70 2c 20 63 6f 6e 73 74  rAccum *p, const
a7f90 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 4e 29   char *z, int N)
a7fa0 7b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6f 42 69  {.  if( p->tooBi
a7fb0 67 20 7c 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69  g | p->mallocFai
a7fc0 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72  led ){.    retur
a7fd0 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 4e 3c 30  n;.  }.  if( N<0
a7fe0 20 29 7b 0a 20 20 20 20 4e 20 3d 20 73 74 72 6c   ){.    N = strl
a7ff0 65 6e 28 7a 29 3b 0a 20 20 7d 0a 20 20 69 66 28  en(z);.  }.  if(
a8000 20 4e 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74   N==0 ){.    ret
a8010 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  urn;.  }.  if( p
a8020 2d 3e 6e 43 68 61 72 2b 4e 20 3e 3d 20 70 2d 3e  ->nChar+N >= p->
a8030 6e 41 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 63 68  nAlloc ){.    ch
a8040 61 72 20 2a 7a 4e 65 77 3b 0a 20 20 20 20 69 66  ar *zNew;.    if
a8050 28 20 21 70 2d 3e 75 73 65 4d 61 6c 6c 6f 63 20  ( !p->useMalloc 
a8060 29 7b 0a 20 20 20 20 20 20 70 2d 3e 74 6f 6f 42  ){.      p->tooB
a8070 69 67 20 3d 20 31 3b 0a 20 20 20 20 20 20 4e 20  ig = 1;.      N 
a8080 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 2d 20 70 2d  = p->nAlloc - p-
a8090 3e 6e 43 68 61 72 20 2d 20 31 3b 0a 20 20 20 20  >nChar - 1;.    
a80a0 20 20 69 66 28 20 4e 3c 3d 30 20 29 7b 0a 20 20    if( N<=0 ){.  
a80b0 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
a80c0 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
a80d0 0a 20 20 20 20 20 20 69 36 34 20 73 7a 4e 65 77  .      i64 szNew
a80e0 20 3d 20 70 2d 3e 6e 43 68 61 72 3b 0a 20 20 20   = p->nChar;.   
a80f0 20 20 20 73 7a 4e 65 77 20 2b 3d 20 4e 20 2b 20     szNew += N + 
a8100 31 3b 0a 20 20 20 20 20 20 69 66 28 20 73 7a 4e  1;.      if( szN
a8110 65 77 20 3e 20 70 2d 3e 6d 78 41 6c 6c 6f 63 20  ew > p->mxAlloc 
a8120 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
a8130 65 33 53 74 72 41 63 63 75 6d 52 65 73 65 74 28  e3StrAccumReset(
a8140 70 29 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 74  p);.        p->t
a8150 6f 6f 42 69 67 20 3d 20 31 3b 0a 20 20 20 20 20  ooBig = 1;.     
a8160 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
a8170 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
a8180 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 73 7a 4e 65  p->nAlloc = szNe
a8190 77 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  w;.      }.     
a81a0 20 7a 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   zNew = sqlite3D
a81b0 62 4d 61 6c 6c 6f 63 52 61 77 28 70 2d 3e 64 62  bMallocRaw(p->db
a81c0 2c 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  , p->nAlloc );. 
a81d0 20 20 20 20 20 69 66 28 20 7a 4e 65 77 20 29 7b       if( zNew ){
a81e0 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28  .        memcpy(
a81f0 7a 4e 65 77 2c 20 70 2d 3e 7a 54 65 78 74 2c 20  zNew, p->zText, 
a8200 70 2d 3e 6e 43 68 61 72 29 3b 0a 20 20 20 20 20  p->nChar);.     
a8210 20 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63     sqlite3StrAcc
a8220 75 6d 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20  umReset(p);.    
a8230 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d 20 7a      p->zText = z
a8240 4e 65 77 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  New;.      }else
a8250 7b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6d 61 6c  {.        p->mal
a8260 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
a8270 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
a8280 72 41 63 63 75 6d 52 65 73 65 74 28 70 29 3b 0a  rAccumReset(p);.
a8290 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
a82a0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
a82b0 7d 0a 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e 7a  }.  memcpy(&p->z
a82c0 54 65 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 2c 20  Text[p->nChar], 
a82d0 7a 2c 20 4e 29 3b 0a 20 20 70 2d 3e 6e 43 68 61  z, N);.  p->nCha
a82e0 72 20 2b 3d 20 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  r += N;.}../*.**
a82f0 20 46 69 6e 69 73 68 20 6f 66 66 20 61 20 73 74   Finish off a st
a8300 72 69 6e 67 20 62 79 20 6d 61 6b 69 6e 67 20 73  ring by making s
a8310 75 72 65 20 69 74 20 69 73 20 7a 65 72 6f 2d 74  ure it is zero-t
a8320 65 72 6d 69 6e 61 74 65 64 2e 0a 2a 2a 20 52 65  erminated..** Re
a8330 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
a8340 6f 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20  o the resulting 
a8350 73 74 72 69 6e 67 2e 20 20 52 65 74 75 72 6e 20  string.  Return 
a8360 61 20 4e 55 4c 4c 0a 2a 2a 20 70 6f 69 6e 74 65  a NULL.** pointe
a8370 72 20 69 66 20 61 6e 79 20 6b 69 6e 64 20 6f 66  r if any kind of
a8380 20 65 72 72 6f 72 20 77 61 73 20 65 6e 63 6f 75   error was encou
a8390 6e 74 65 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ntered..*/.SQLIT
a83a0 45 5f 50 52 49 56 41 54 45 20 63 68 61 72 20 2a  E_PRIVATE char *
a83b0 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46  sqlite3StrAccumF
a83c0 69 6e 69 73 68 28 53 74 72 41 63 63 75 6d 20 2a  inish(StrAccum *
a83d0 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 7a 54 65  p){.  if( p->zTe
a83e0 78 74 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 54 65  xt ){.    p->zTe
a83f0 78 74 5b 70 2d 3e 6e 43 68 61 72 5d 20 3d 20 30  xt[p->nChar] = 0
a8400 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 65  ;.    if( p->use
a8410 4d 61 6c 6c 6f 63 20 26 26 20 70 2d 3e 7a 54 65  Malloc && p->zTe
a8420 78 74 3d 3d 70 2d 3e 7a 42 61 73 65 20 29 7b 0a  xt==p->zBase ){.
a8430 20 20 20 20 20 20 70 2d 3e 7a 54 65 78 74 20 3d        p->zText =
a8440 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
a8450 52 61 77 28 70 2d 3e 64 62 2c 20 70 2d 3e 6e 43  Raw(p->db, p->nC
a8460 68 61 72 2b 31 20 29 3b 0a 20 20 20 20 20 20 69  har+1 );.      i
a8470 66 28 20 70 2d 3e 7a 54 65 78 74 20 29 7b 0a 20  f( p->zText ){. 
a8480 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 2d         memcpy(p-
a8490 3e 7a 54 65 78 74 2c 20 70 2d 3e 7a 42 61 73 65  >zText, p->zBase
a84a0 2c 20 70 2d 3e 6e 43 68 61 72 2b 31 29 3b 0a 20  , p->nChar+1);. 
a84b0 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
a84c0 20 20 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69      p->mallocFai
a84d0 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  led = 1;.      }
a84e0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
a84f0 75 72 6e 20 70 2d 3e 7a 54 65 78 74 3b 0a 7d 0a  urn p->zText;.}.
a8500 0a 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 61 6e 20  ./*.** Reset an 
a8510 53 74 72 41 63 63 75 6d 20 73 74 72 69 6e 67 2e  StrAccum string.
a8520 20 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 61    Reclaim all ma
a8530 6c 6c 6f 63 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a  lloced memory..*
a8540 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
a8550 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 72   void sqlite3Str
a8560 41 63 63 75 6d 52 65 73 65 74 28 53 74 72 41 63  AccumReset(StrAc
a8570 63 75 6d 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  cum *p){.  if( p
a8580 2d 3e 7a 54 65 78 74 21 3d 70 2d 3e 7a 42 61 73  ->zText!=p->zBas
a8590 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  e ){.    sqlite3
a85a0 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d  DbFree(p->db, p-
a85b0 3e 7a 54 65 78 74 29 3b 0a 20 20 7d 0a 20 20 70  >zText);.  }.  p
a85c0 2d 3e 7a 54 65 78 74 20 3d 20 30 3b 0a 7d 0a 0a  ->zText = 0;.}..
a85d0 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
a85e0 20 61 20 73 74 72 69 6e 67 20 61 63 63 75 6d 75   a string accumu
a85f0 6c 61 74 6f 72 0a 2a 2f 0a 53 51 4c 49 54 45 5f  lator.*/.SQLITE_
a8600 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
a8610 69 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74  ite3StrAccumInit
a8620 28 53 74 72 41 63 63 75 6d 20 2a 70 2c 20 63 68  (StrAccum *p, ch
a8630 61 72 20 2a 7a 42 61 73 65 2c 20 69 6e 74 20 6e  ar *zBase, int n
a8640 2c 20 69 6e 74 20 6d 78 29 7b 0a 20 20 70 2d 3e  , int mx){.  p->
a8650 7a 54 65 78 74 20 3d 20 70 2d 3e 7a 42 61 73 65  zText = p->zBase
a8660 20 3d 20 7a 42 61 73 65 3b 0a 20 20 70 2d 3e 64   = zBase;.  p->d
a8670 62 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 43 68 61  b = 0;.  p->nCha
a8680 72 20 3d 20 30 3b 0a 20 20 70 2d 3e 6e 41 6c 6c  r = 0;.  p->nAll
a8690 6f 63 20 3d 20 6e 3b 0a 20 20 70 2d 3e 6d 78 41  oc = n;.  p->mxA
a86a0 6c 6c 6f 63 20 3d 20 6d 78 3b 0a 20 20 70 2d 3e  lloc = mx;.  p->
a86b0 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20  useMalloc = 1;. 
a86c0 20 70 2d 3e 74 6f 6f 42 69 67 20 3d 20 30 3b 0a   p->tooBig = 0;.
a86d0 20 20 70 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65    p->mallocFaile
a86e0 64 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  d = 0;.}../*.** 
a86f0 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f 72  Print into memor
a8700 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  y obtained from 
a8710 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29 2e 20  sqliteMalloc(). 
a8720 20 55 73 65 20 74 68 65 20 69 6e 74 65 72 6e 61   Use the interna
a8730 6c 0a 2a 2a 20 25 2d 63 6f 6e 76 65 72 73 69 6f  l.** %-conversio
a8740 6e 20 65 78 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f  n extensions..*/
a8750 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
a8760 63 68 61 72 20 2a 73 71 6c 69 74 65 33 56 4d 50  char *sqlite3VMP
a8770 72 69 6e 74 66 28 73 71 6c 69 74 65 33 20 2a 64  rintf(sqlite3 *d
a8780 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
a8790 46 6f 72 6d 61 74 2c 20 76 61 5f 6c 69 73 74 20  Format, va_list 
a87a0 61 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a  ap){.  char *z;.
a87b0 20 20 63 68 61 72 20 7a 42 61 73 65 5b 53 51 4c    char zBase[SQL
a87c0 49 54 45 5f 50 52 49 4e 54 5f 42 55 46 5f 53 49  ITE_PRINT_BUF_SI
a87d0 5a 45 5d 3b 0a 20 20 53 74 72 41 63 63 75 6d 20  ZE];.  StrAccum 
a87e0 61 63 63 3b 0a 20 20 73 71 6c 69 74 65 33 53 74  acc;.  sqlite3St
a87f0 72 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c  rAccumInit(&acc,
a8800 20 7a 42 61 73 65 2c 20 73 69 7a 65 6f 66 28 7a   zBase, sizeof(z
a8810 42 61 73 65 29 2c 0a 20 20 20 20 20 20 20 20 20  Base),.         
a8820 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 20               db 
a8830 3f 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ? db->aLimit[SQL
a8840 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
a8850 5d 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  ] : SQLITE_MAX_L
a8860 45 4e 47 54 48 29 3b 0a 20 20 61 63 63 2e 64 62  ENGTH);.  acc.db
a8870 20 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33   = db;.  sqlite3
a8880 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c 20 31  VXPrintf(&acc, 1
a8890 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
a88a0 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74 72    z = sqlite3Str
a88b0 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63 63  AccumFinish(&acc
a88c0 29 3b 0a 20 20 69 66 28 20 61 63 63 2e 6d 61 6c  );.  if( acc.mal
a88d0 6c 6f 63 46 61 69 6c 65 64 20 26 26 20 64 62 20  locFailed && db 
a88e0 29 7b 0a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f  ){.    db->mallo
a88f0 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d  cFailed = 1;.  }
a8900 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a  .  return z;.}..
a8910 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74 6f  /*.** Print into
a8920 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
a8930 20 66 72 6f 6d 20 73 71 6c 69 74 65 4d 61 6c 6c   from sqliteMall
a8940 6f 63 28 29 2e 20 20 55 73 65 20 74 68 65 20 69  oc().  Use the i
a8950 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e  nternal.** %-con
a8960 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f  version extensio
a8970 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ns..*/.SQLITE_PR
a8980 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
a8990 74 65 33 4d 50 72 69 6e 74 66 28 73 71 6c 69 74  te3MPrintf(sqlit
a89a0 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68  e3 *db, const ch
a89b0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
a89c0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
a89d0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 76 61  .  char *z;.  va
a89e0 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
a89f0 61 74 29 3b 0a 20 20 7a 20 3d 20 73 71 6c 69 74  at);.  z = sqlit
a8a00 65 33 56 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a  e3VMPrintf(db, z
a8a10 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76  Format, ap);.  v
a8a20 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74  a_end(ap);.  ret
a8a30 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn z;.}../*.** 
a8a40 4c 69 6b 65 20 73 71 6c 69 74 65 33 4d 50 72 69  Like sqlite3MPri
a8a50 6e 74 66 28 29 2c 20 62 75 74 20 63 61 6c 6c 20  ntf(), but call 
a8a60 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 29 20  sqlite3DbFree() 
a8a70 6f 6e 20 7a 53 74 72 20 61 66 74 65 72 20 66 6f  on zStr after fo
a8a80 72 6d 61 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20  rmatting.** the 
a8a90 73 74 72 69 6e 67 20 61 6e 64 20 62 65 66 6f 72  string and befor
a8aa0 65 20 72 65 74 75 72 6e 6e 69 6e 67 2e 20 20 54  e returnning.  T
a8ab0 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 69  his routine is i
a8ac0 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 75 73  ntended to be us
a8ad0 65 64 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66 79 20  ed.** to modify 
a8ae0 61 6e 20 65 78 69 73 74 69 6e 67 20 73 74 72 69  an existing stri
a8af0 6e 67 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c 65  ng.  For example
a8b00 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 78 20  :.**.**       x 
a8b10 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
a8b20 28 64 62 2c 20 78 2c 20 22 70 72 65 66 69 78 20  (db, x, "prefix 
a8b30 25 73 20 73 75 66 66 69 78 22 2c 20 78 29 3b 0a  %s suffix", x);.
a8b40 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  **.*/.SQLITE_PRI
a8b50 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69 74  VATE char *sqlit
a8b60 65 33 4d 41 70 70 65 6e 64 66 28 73 71 6c 69 74  e3MAppendf(sqlit
a8b70 65 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 7a 53  e3 *db, char *zS
a8b80 74 72 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tr, const char *
a8b90 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20  zFormat, ...){. 
a8ba0 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 63   va_list ap;.  c
a8bb0 68 61 72 20 2a 7a 3b 0a 20 20 76 61 5f 73 74 61  har *z;.  va_sta
a8bc0 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b  rt(ap, zFormat);
a8bd0 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d  .  z = sqlite3VM
a8be0 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d  Printf(db, zForm
a8bf0 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65 6e  at, ap);.  va_en
a8c00 64 28 61 70 29 3b 0a 20 20 73 71 6c 69 74 65 33  d(ap);.  sqlite3
a8c10 44 62 46 72 65 65 28 64 62 2c 20 7a 53 74 72 29  DbFree(db, zStr)
a8c20 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a  ;.  return z;.}.
a8c30 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 69 6e 74  ./*.** Print int
a8c40 6f 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65  o memory obtaine
a8c50 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 6d  d from sqlite3_m
a8c60 61 6c 6c 6f 63 28 29 2e 20 20 4f 6d 69 74 20 74  alloc().  Omit t
a8c70 68 65 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25  he internal.** %
a8c80 2d 63 6f 6e 76 65 72 73 69 6f 6e 20 65 78 74 65  -conversion exte
a8c90 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54  nsions..*/.SQLIT
a8ca0 45 5f 41 50 49 20 63 68 61 72 20 2a 73 71 6c 69  E_API char *sqli
a8cb0 74 65 33 5f 76 6d 70 72 69 6e 74 66 28 63 6f 6e  te3_vmprintf(con
a8cc0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
a8cd0 2c 20 76 61 5f 6c 69 73 74 20 61 70 29 7b 0a 20  , va_list ap){. 
a8ce0 20 63 68 61 72 20 2a 7a 3b 0a 20 20 63 68 61 72   char *z;.  char
a8cf0 20 7a 42 61 73 65 5b 53 51 4c 49 54 45 5f 50 52   zBase[SQLITE_PR
a8d00 49 4e 54 5f 42 55 46 5f 53 49 5a 45 5d 3b 0a 20  INT_BUF_SIZE];. 
a8d10 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a 23   StrAccum acc;.#
a8d20 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
a8d30 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66  IT_AUTOINIT.  if
a8d40 28 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61  ( sqlite3_initia
a8d50 6c 69 7a 65 28 29 20 29 20 72 65 74 75 72 6e 20  lize() ) return 
a8d60 30 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0;.#endif.  sqli
a8d70 74 65 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28  te3StrAccumInit(
a8d80 26 61 63 63 2c 20 7a 42 61 73 65 2c 20 73 69 7a  &acc, zBase, siz
a8d90 65 6f 66 28 7a 42 61 73 65 29 2c 20 53 51 4c 49  eof(zBase), SQLI
a8da0 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 29 3b 0a  TE_MAX_LENGTH);.
a8db0 20 20 73 71 6c 69 74 65 33 56 58 50 72 69 6e 74    sqlite3VXPrint
a8dc0 66 28 26 61 63 63 2c 20 30 2c 20 7a 46 6f 72 6d  f(&acc, 0, zForm
a8dd0 61 74 2c 20 61 70 29 3b 0a 20 20 7a 20 3d 20 73  at, ap);.  z = s
a8de0 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69  qlite3StrAccumFi
a8df0 6e 69 73 68 28 26 61 63 63 29 3b 0a 20 20 72 65  nish(&acc);.  re
a8e00 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn z;.}../*.**
a8e10 20 50 72 69 6e 74 20 69 6e 74 6f 20 6d 65 6d 6f   Print into memo
a8e20 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ry obtained from
a8e30 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
a8e40 29 28 29 2e 20 20 4f 6d 69 74 20 74 68 65 20 69  )().  Omit the i
a8e50 6e 74 65 72 6e 61 6c 0a 2a 2a 20 25 2d 63 6f 6e  nternal.** %-con
a8e60 76 65 72 73 69 6f 6e 20 65 78 74 65 6e 73 69 6f  version extensio
a8e70 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ns..*/.SQLITE_AP
a8e80 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
a8e90 6d 70 72 69 6e 74 66 28 63 6f 6e 73 74 20 63 68  mprintf(const ch
a8ea0 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e  ar *zFormat, ...
a8eb0 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b  ){.  va_list ap;
a8ec0 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 23 69 66 6e  .  char *z;.#ifn
a8ed0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
a8ee0 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28 20 73  AUTOINIT.  if( s
a8ef0 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
a8f00 65 28 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  e() ) return 0;.
a8f10 23 65 6e 64 69 66 0a 20 20 76 61 5f 73 74 61 72  #endif.  va_star
a8f20 74 28 61 70 2c 20 7a 46 6f 72 6d 61 74 29 3b 0a  t(ap, zFormat);.
a8f30 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 6d    z = sqlite3_vm
a8f40 70 72 69 6e 74 66 28 7a 46 6f 72 6d 61 74 2c 20  printf(zFormat, 
a8f50 61 70 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70  ap);.  va_end(ap
a8f60 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d  );.  return z;.}
a8f70 0a 0a 2f 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ../*.** sqlite3_
a8f80 73 6e 70 72 69 6e 74 66 28 29 20 77 6f 72 6b 73  snprintf() works
a8f90 20 6c 69 6b 65 20 73 6e 70 72 69 6e 74 66 28 29   like snprintf()
a8fa0 20 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20   except that it 
a8fb0 69 67 6e 6f 72 65 73 20 74 68 65 0a 2a 2a 20 63  ignores the.** c
a8fc0 75 72 72 65 6e 74 20 6c 6f 63 61 6c 65 20 73 65  urrent locale se
a8fd0 74 74 69 6e 67 73 2e 20 20 54 68 69 73 20 69 73  ttings.  This is
a8fe0 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f 72 20 53   important for S
a8ff0 51 4c 69 74 65 20 62 65 63 61 75 73 65 20 77 65  QLite because we
a9000 0a 2a 2a 20 61 72 65 20 6e 6f 74 20 61 62 6c 65  .** are not able
a9010 20 74 6f 20 75 73 65 20 61 20 22 2c 22 20 61 73   to use a "," as
a9020 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70 6f 69   the decimal poi
a9030 6e 74 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 22  nt in place of "
a9040 2e 22 20 61 73 0a 2a 2a 20 73 70 65 63 69 66 69  ." as.** specifi
a9050 65 64 20 62 79 20 73 6f 6d 65 20 6c 6f 63 61 6c  ed by some local
a9060 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
a9070 49 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f  I char *sqlite3_
a9080 73 6e 70 72 69 6e 74 66 28 69 6e 74 20 6e 2c 20  snprintf(int n, 
a9090 63 68 61 72 20 2a 7a 42 75 66 2c 20 63 6f 6e 73  char *zBuf, cons
a90a0 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
a90b0 20 2e 2e 2e 29 7b 0a 20 20 63 68 61 72 20 2a 7a   ...){.  char *z
a90c0 3b 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a  ;.  va_list ap;.
a90d0 20 20 53 74 72 41 63 63 75 6d 20 61 63 63 3b 0a    StrAccum acc;.
a90e0 0a 20 20 69 66 28 20 6e 3c 3d 30 20 29 7b 0a 20  .  if( n<=0 ){. 
a90f0 20 20 20 72 65 74 75 72 6e 20 7a 42 75 66 3b 0a     return zBuf;.
a9100 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72    }.  sqlite3Str
a9110 41 63 63 75 6d 49 6e 69 74 28 26 61 63 63 2c 20  AccumInit(&acc, 
a9120 7a 42 75 66 2c 20 6e 2c 20 30 29 3b 0a 20 20 61  zBuf, n, 0);.  a
a9130 63 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30  cc.useMalloc = 0
a9140 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c  ;.  va_start(ap,
a9150 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69  zFormat);.  sqli
a9160 74 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63  te3VXPrintf(&acc
a9170 2c 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  , 0, zFormat, ap
a9180 29 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b  );.  va_end(ap);
a9190 0a 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 53 74  .  z = sqlite3St
a91a0 72 41 63 63 75 6d 46 69 6e 69 73 68 28 26 61 63  rAccumFinish(&ac
a91b0 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  c);.  return z;.
a91c0 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  }..#if defined(S
a91d0 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 2f 2a 0a  QLITE_DEBUG)./*.
a91e0 2a 2a 20 41 20 76 65 72 73 69 6f 6e 20 6f 66 20  ** A version of 
a91f0 70 72 69 6e 74 66 28 29 20 74 68 61 74 20 75 6e  printf() that un
a9200 64 65 72 73 74 61 6e 64 73 20 25 6c 6c 64 2e 20  derstands %lld. 
a9210 20 55 73 65 64 20 66 6f 72 20 64 65 62 75 67 67   Used for debugg
a9220 69 6e 67 2e 0a 2a 2a 20 54 68 65 20 70 72 69 6e  ing..** The prin
a9230 74 66 28 29 20 62 75 69 6c 74 20 69 6e 74 6f 20  tf() built into 
a9240 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20 6f 66  some versions of
a9250 20 77 69 6e 64 6f 77 73 20 64 6f 65 73 20 6e 6f   windows does no
a9260 74 20 75 6e 64 65 72 73 74 61 6e 64 20 25 6c 6c  t understand %ll
a9270 64 0a 2a 2a 20 61 6e 64 20 73 65 67 66 61 75 6c  d.** and segfaul
a9280 74 73 20 69 66 20 79 6f 75 20 67 69 76 65 20 69  ts if you give i
a9290 74 20 61 20 6c 6f 6e 67 20 6c 6f 6e 67 20 69 6e  t a long long in
a92a0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
a92b0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
a92c0 33 44 65 62 75 67 50 72 69 6e 74 66 28 63 6f 6e  3DebugPrintf(con
a92d0 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74  st char *zFormat
a92e0 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73  , ...){.  va_lis
a92f0 74 20 61 70 3b 0a 20 20 53 74 72 41 63 63 75 6d  t ap;.  StrAccum
a9300 20 61 63 63 3b 0a 20 20 63 68 61 72 20 7a 42 75   acc;.  char zBu
a9310 66 5b 35 30 30 5d 3b 0a 20 20 73 71 6c 69 74 65  f[500];.  sqlite
a9320 33 53 74 72 41 63 63 75 6d 49 6e 69 74 28 26 61  3StrAccumInit(&a
a9330 63 63 2c 20 7a 42 75 66 2c 20 73 69 7a 65 6f 66  cc, zBuf, sizeof
a9340 28 7a 42 75 66 29 2c 20 30 29 3b 0a 20 20 61 63  (zBuf), 0);.  ac
a9350 63 2e 75 73 65 4d 61 6c 6c 6f 63 20 3d 20 30 3b  c.useMalloc = 0;
a9360 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 7a  .  va_start(ap,z
a9370 46 6f 72 6d 61 74 29 3b 0a 20 20 73 71 6c 69 74  Format);.  sqlit
a9380 65 33 56 58 50 72 69 6e 74 66 28 26 61 63 63 2c  e3VXPrintf(&acc,
a9390 20 30 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29   0, zFormat, ap)
a93a0 3b 0a 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a  ;.  va_end(ap);.
a93b0 20 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75    sqlite3StrAccu
a93c0 6d 46 69 6e 69 73 68 28 26 61 63 63 29 3b 0a 20  mFinish(&acc);. 
a93d0 20 66 70 72 69 6e 74 66 28 73 74 64 6f 75 74 2c   fprintf(stdout,
a93e0 22 25 73 22 2c 20 7a 42 75 66 29 3b 0a 20 20 66  "%s", zBuf);.  f
a93f0 66 6c 75 73 68 28 73 74 64 6f 75 74 29 3b 0a 7d  flush(stdout);.}
a9400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a  .#endif../******
a9410 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
a9420 70 72 69 6e 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a  printf.c *******
a9430 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9450 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
a9460 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
a9470 69 6c 65 20 72 61 6e 64 6f 6d 2e 63 20 2a 2a 2a  ile random.c ***
a9480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9490 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a94a0 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
a94b0 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
a94c0 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
a94d0 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
a94e0 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
a94f0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
a9500 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
a9510 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
a9520 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
a9530 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
a9540 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
a9550 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
a9560 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
a9570 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
a9580 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
a9590 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
a95a0 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
a95b0 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
a95c0 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
a95d0 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
a95e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a95f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9600 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
a9610 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
a9620 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
a9630 74 61 69 6e 73 20 63 6f 64 65 20 74 6f 20 69 6d  tains code to im
a9640 70 6c 65 6d 65 6e 74 20 61 20 70 73 65 75 64 6f  plement a pseudo
a9650 2d 72 61 6e 64 6f 6d 20 6e 75 6d 62 65 72 0a 2a  -random number.*
a9660 2a 20 67 65 6e 65 72 61 74 6f 72 20 28 50 52 4e  * generator (PRN
a9670 47 29 20 66 6f 72 20 53 51 4c 69 74 65 2e 0a 2a  G) for SQLite..*
a9680 2a 0a 2a 2a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62  *.** Random numb
a9690 65 72 73 20 61 72 65 20 75 73 65 64 20 62 79 20  ers are used by 
a96a0 73 6f 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  some of the data
a96b0 62 61 73 65 20 62 61 63 6b 65 6e 64 73 20 69 6e  base backends in
a96c0 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 67 65 6e   order.** to gen
a96d0 65 72 61 74 65 20 72 61 6e 64 6f 6d 20 69 6e 74  erate random int
a96e0 65 67 65 72 20 6b 65 79 73 20 66 6f 72 20 74 61  eger keys for ta
a96f0 62 6c 65 73 20 6f 72 20 72 61 6e 64 6f 6d 20 66  bles or random f
a9700 69 6c 65 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a 2a 20  ilenames..**.** 
a9710 24 49 64 3a 20 72 61 6e 64 6f 6d 2e 63 2c 76 20  $Id: random.c,v 
a9720 31 2e 32 37 20 32 30 30 38 2f 31 30 2f 30 37 20  1.27 2008/10/07 
a9730 31 35 3a 32 35 3a 34 38 20 64 72 68 20 45 78 70  15:25:48 drh Exp
a9740 20 24 0a 2a 2f 0a 0a 0a 2f 2a 20 41 6c 6c 20 74   $.*/.../* All t
a9750 68 72 65 61 64 73 20 73 68 61 72 65 20 61 20 73  hreads share a s
a9760 69 6e 67 6c 65 20 72 61 6e 64 6f 6d 20 6e 75 6d  ingle random num
a9770 62 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a  ber generator..*
a9780 2a 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65  * This structure
a9790 20 69 73 20 74 68 65 20 63 75 72 72 65 6e 74 20   is the current 
a97a0 73 74 61 74 65 20 6f 66 20 74 68 65 20 67 65 6e  state of the gen
a97b0 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  erator..*/.stati
a97c0 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74 72  c SQLITE_WSD str
a97d0 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54  uct sqlite3PrngT
a97e0 79 70 65 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ype {.  unsigned
a97f0 20 63 68 61 72 20 69 73 49 6e 69 74 3b 20 20 20   char isInit;   
a9800 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
a9810 66 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f  f initialized */
a9820 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a9830 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20   i, j;          
a9840 20 20 2f 2a 20 53 74 61 74 65 20 76 61 72 69 61    /* State varia
a9850 62 6c 65 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e  bles */.  unsign
a9860 65 64 20 63 68 61 72 20 73 5b 32 35 36 5d 3b 20  ed char s[256]; 
a9870 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
a9880 65 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 7d  e variables */.}
a9890 20 73 71 6c 69 74 65 33 50 72 6e 67 20 3d 20 7b   sqlite3Prng = {
a98a0 20 30 2c 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 47 65   0, };../*.** Ge
a98b0 74 20 61 20 73 69 6e 67 6c 65 20 38 2d 62 69 74  t a single 8-bit
a98c0 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 66 72   random value fr
a98d0 6f 6d 20 74 68 65 20 52 43 34 20 50 52 4e 47 2e  om the RC4 PRNG.
a98e0 20 20 54 68 65 20 4d 75 74 65 78 0a 2a 2a 20 6d    The Mutex.** m
a98f0 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 69 6c  ust be held whil
a9900 65 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73  e executing this
a9910 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20   routine..**.** 
a9920 57 68 79 20 6e 6f 74 20 6a 75 73 74 20 75 73 65  Why not just use
a9930 20 61 20 6c 69 62 72 61 72 79 20 72 61 6e 64 6f   a library rando
a9940 6d 20 67 65 6e 65 72 61 74 6f 72 20 6c 69 6b 65  m generator like
a9950 20 6c 72 61 6e 64 34 38 28 29 20 66 6f 72 20 74   lrand48() for t
a9960 68 69 73 3f 0a 2a 2a 20 42 65 63 61 75 73 65 20  his?.** Because 
a9970 74 68 65 20 4f 50 5f 4e 65 77 52 6f 77 69 64 20  the OP_NewRowid 
a9980 6f 70 63 6f 64 65 20 69 6e 20 74 68 65 20 56 44  opcode in the VD
a9990 42 45 20 64 65 70 65 6e 64 73 20 6f 6e 20 68 61  BE depends on ha
a99a0 76 69 6e 67 20 61 20 76 65 72 79 0a 2a 2a 20 67  ving a very.** g
a99b0 6f 6f 64 20 73 6f 75 72 63 65 20 6f 66 20 72 61  ood source of ra
a99c0 6e 64 6f 6d 20 6e 75 6d 62 65 72 73 2e 20 20 54  ndom numbers.  T
a99d0 68 65 20 6c 72 61 6e 64 34 38 28 29 20 6c 69 62  he lrand48() lib
a99e0 72 61 72 79 20 66 75 6e 63 74 69 6f 6e 20 6d 61  rary function ma
a99f0 79 0a 2a 2a 20 77 65 6c 6c 20 62 65 20 67 6f 6f  y.** well be goo
a9a00 64 20 65 6e 6f 75 67 68 2e 20 20 42 75 74 20 6d  d enough.  But m
a9a10 61 79 62 65 20 6e 6f 74 2e 20 20 4f 72 20 6d 61  aybe not.  Or ma
a9a20 79 62 65 20 6c 72 61 6e 64 34 38 28 29 20 68 61  ybe lrand48() ha
a9a30 73 20 73 6f 6d 65 0a 2a 2a 20 73 75 62 74 6c 65  s some.** subtle
a9a40 20 70 72 6f 62 6c 65 6d 73 20 6f 6e 20 73 6f 6d   problems on som
a9a50 65 20 73 79 73 74 65 6d 73 20 74 68 61 74 20 63  e systems that c
a9a60 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
a9a70 65 6d 73 2e 20 20 49 74 20 69 73 20 68 61 72 64  ems.  It is hard
a9a80 0a 2a 2a 20 74 6f 20 6b 6e 6f 77 2e 20 20 54 6f  .** to know.  To
a9a90 20 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 72 69   minimize the ri
a9aa0 73 6b 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20 64  sk of problems d
a9ab0 75 65 20 74 6f 20 62 61 64 20 6c 72 61 6e 64 34  ue to bad lrand4
a9ac0 38 28 29 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74  8().** implement
a9ad0 61 74 69 6f 6e 73 2c 20 53 51 4c 69 74 65 20 75  ations, SQLite u
a9ae0 73 65 73 20 74 68 69 73 20 72 61 6e 64 6f 6d 20  ses this random 
a9af0 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72  number generator
a9b00 20 62 61 73 65 64 0a 2a 2a 20 6f 6e 20 52 43 34   based.** on RC4
a9b10 2c 20 77 68 69 63 68 20 77 65 20 6b 6e 6f 77 20  , which we know 
a9b20 77 6f 72 6b 73 20 76 65 72 79 20 77 65 6c 6c 2e  works very well.
a9b30 0a 2a 2a 0a 2a 2a 20 28 4c 61 74 65 72 29 3a 20  .**.** (Later): 
a9b40 20 41 63 74 75 61 6c 6c 79 2c 20 4f 50 5f 4e 65   Actually, OP_Ne
a9b50 77 52 6f 77 69 64 20 64 6f 65 73 20 6e 6f 74 20  wRowid does not 
a9b60 64 65 70 65 6e 64 20 6f 6e 20 61 20 67 6f 6f 64  depend on a good
a9b70 20 73 6f 75 72 63 65 20 6f 66 0a 2a 2a 20 72 61   source of.** ra
a9b80 6e 64 6f 6d 6e 65 73 73 20 61 6e 79 20 6d 6f 72  ndomness any mor
a9b90 65 2e 20 20 42 75 74 20 77 65 20 77 69 6c 6c 20  e.  But we will 
a9ba0 6c 65 61 76 65 20 74 68 69 73 20 63 6f 64 65 20  leave this code 
a9bb0 69 6e 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 2e  in all the same.
a9bc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
a9bd0 61 6e 64 6f 6d 42 79 74 65 28 76 6f 69 64 29 7b  andomByte(void){
a9be0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
a9bf0 20 74 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 22   t;...  /* The "
a9c00 77 73 64 50 72 6e 67 22 20 6d 61 63 72 6f 20 77  wsdPrng" macro w
a9c10 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20 74  ill resolve to t
a9c20 68 65 20 70 73 65 75 64 6f 2d 72 61 6e 64 6f 6d  he pseudo-random
a9c30 20 6e 75 6d 62 65 72 20 67 65 6e 65 72 61 74 6f   number generato
a9c40 72 0a 20 20 2a 2a 20 73 74 61 74 65 20 76 65 63  r.  ** state vec
a9c50 74 6f 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c  tor.  If writabl
a9c60 65 20 73 74 61 74 69 63 20 64 61 74 61 20 69 73  e static data is
a9c70 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20   unsupported on 
a9c80 74 68 65 20 74 61 72 67 65 74 2c 0a 20 20 2a 2a  the target,.  **
a9c90 20 77 65 20 68 61 76 65 20 74 6f 20 6c 6f 63 61   we have to loca
a9ca0 74 65 20 74 68 65 20 73 74 61 74 65 20 76 65 63  te the state vec
a9cb0 74 6f 72 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e  tor at run-time.
a9cc0 20 20 49 6e 20 74 68 65 20 6d 6f 72 65 20 63 6f    In the more co
a9cd0 6d 6d 6f 6e 0a 20 20 2a 2a 20 63 61 73 65 20 77  mmon.  ** case w
a9ce0 68 65 72 65 20 77 72 69 74 61 62 6c 65 20 73 74  here writable st
a9cf0 61 74 69 63 20 64 61 74 61 20 69 73 20 73 75 70  atic data is sup
a9d00 70 6f 72 74 65 64 2c 20 77 73 64 50 72 6e 67 20  ported, wsdPrng 
a9d10 63 61 6e 20 72 65 66 65 72 20 64 69 72 65 63 74  can refer direct
a9d20 6c 79 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 22  ly.  ** to the "
a9d30 73 71 6c 69 74 65 33 50 72 6e 67 22 20 73 74 61  sqlite3Prng" sta
a9d40 74 65 20 76 65 63 74 6f 72 20 64 65 63 6c 61 72  te vector declar
a9d50 65 64 20 61 62 6f 76 65 2e 0a 20 20 2a 2f 0a 23  ed above..  */.#
a9d60 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
a9d70 54 5f 57 53 44 0a 20 20 73 74 72 75 63 74 20 73  T_WSD.  struct s
a9d80 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 2a  qlite3PrngType *
a9d90 70 20 3d 20 26 47 4c 4f 42 41 4c 28 73 74 72 75  p = &GLOBAL(stru
a9da0 63 74 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79  ct sqlite3PrngTy
a9db0 70 65 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29  pe, sqlite3Prng)
a9dc0 3b 0a 23 20 64 65 66 69 6e 65 20 77 73 64 50 72  ;.# define wsdPr
a9dd0 6e 67 20 70 5b 30 5d 0a 23 65 6c 73 65 0a 23 20  ng p[0].#else.# 
a9de0 64 65 66 69 6e 65 20 77 73 64 50 72 6e 67 20 73  define wsdPrng s
a9df0 71 6c 69 74 65 33 50 72 6e 67 0a 23 65 6e 64 69  qlite3Prng.#endi
a9e00 66 0a 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c  f...  /* Initial
a9e10 69 7a 65 20 74 68 65 20 73 74 61 74 65 20 6f 66  ize the state of
a9e20 20 74 68 65 20 72 61 6e 64 6f 6d 20 6e 75 6d 62   the random numb
a9e30 65 72 20 67 65 6e 65 72 61 74 6f 72 20 6f 6e 63  er generator onc
a9e40 65 2c 0a 20 20 2a 2a 20 74 68 65 20 66 69 72 73  e,.  ** the firs
a9e50 74 20 74 69 6d 65 20 74 68 69 73 20 72 6f 75 74  t time this rout
a9e60 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20 20  ine is called.  
a9e70 54 68 65 20 73 65 65 64 20 76 61 6c 75 65 20 64  The seed value d
a9e80 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20 6e 65 65  oes.  ** not nee
a9e90 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 6c  d to contain a l
a9ea0 6f 74 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73  ot of randomness
a9eb0 20 73 69 6e 63 65 20 77 65 20 61 72 65 20 6e 6f   since we are no
a9ec0 74 0a 20 20 2a 2a 20 74 72 79 69 6e 67 20 74 6f  t.  ** trying to
a9ed0 20 64 6f 20 73 65 63 75 72 65 20 65 6e 63 72 79   do secure encry
a9ee0 70 74 69 6f 6e 20 6f 72 20 61 6e 79 74 68 69 6e  ption or anythin
a9ef0 67 20 6c 69 6b 65 20 74 68 61 74 2e 2e 2e 0a 20  g like that.... 
a9f00 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74 68 69 6e 67   **.  ** Nothing
a9f10 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 72   in this file or
a9f20 20 61 6e 79 77 68 65 72 65 20 65 6c 73 65 20 69   anywhere else i
a9f30 6e 20 53 51 4c 69 74 65 20 64 6f 65 73 20 61 6e  n SQLite does an
a9f40 79 20 6b 69 6e 64 20 6f 66 0a 20 20 2a 2a 20 65  y kind of.  ** e
a9f50 6e 63 72 79 70 74 69 6f 6e 2e 20 20 54 68 65 20  ncryption.  The 
a9f60 52 43 34 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  RC4 algorithm is
a9f70 20 62 65 69 6e 67 20 75 73 65 64 20 61 73 20 61   being used as a
a9f80 20 50 52 4e 47 20 28 70 73 65 75 64 6f 2d 72 61   PRNG (pseudo-ra
a9f90 6e 64 6f 6d 0a 20 20 2a 2a 20 6e 75 6d 62 65 72  ndom.  ** number
a9fa0 20 67 65 6e 65 72 61 74 6f 72 29 20 6e 6f 74 20   generator) not 
a9fb0 61 73 20 61 6e 20 65 6e 63 72 79 70 74 69 6f 6e  as an encryption
a9fc0 20 64 65 76 69 63 65 2e 0a 20 20 2a 2f 0a 20 20   device..  */.  
a9fd0 69 66 28 20 21 77 73 64 50 72 6e 67 2e 69 73 49  if( !wsdPrng.isI
a9fe0 6e 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 69  nit ){.    int i
a9ff0 3b 0a 20 20 20 20 63 68 61 72 20 6b 5b 32 35 36  ;.    char k[256
aa000 5d 3b 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 6a  ];.    wsdPrng.j
aa010 20 3d 20 30 3b 0a 20 20 20 20 77 73 64 50 72 6e   = 0;.    wsdPrn
aa020 67 2e 69 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  g.i = 0;.    sql
aa030 69 74 65 33 4f 73 52 61 6e 64 6f 6d 6e 65 73 73  ite3OsRandomness
aa040 28 73 71 6c 69 74 65 33 5f 76 66 73 5f 66 69 6e  (sqlite3_vfs_fin
aa050 64 28 30 29 2c 20 32 35 36 2c 20 6b 29 3b 0a 20  d(0), 256, k);. 
aa060 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 35     for(i=0; i<25
aa070 36 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 77  6; i++){.      w
aa080 73 64 50 72 6e 67 2e 73 5b 69 5d 20 3d 20 69 3b  sdPrng.s[i] = i;
aa090 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69  .    }.    for(i
aa0a0 3d 30 3b 20 69 3c 32 35 36 3b 20 69 2b 2b 29 7b  =0; i<256; i++){
aa0b0 0a 20 20 20 20 20 20 77 73 64 50 72 6e 67 2e 6a  .      wsdPrng.j
aa0c0 20 2b 3d 20 77 73 64 50 72 6e 67 2e 73 5b 69 5d   += wsdPrng.s[i]
aa0d0 20 2b 20 6b 5b 69 5d 3b 0a 20 20 20 20 20 20 74   + k[i];.      t
aa0e0 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77 73 64   = wsdPrng.s[wsd
aa0f0 50 72 6e 67 2e 6a 5d 3b 0a 20 20 20 20 20 20 77  Prng.j];.      w
aa100 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67  sdPrng.s[wsdPrng
aa110 2e 6a 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b  .j] = wsdPrng.s[
aa120 69 5d 3b 0a 20 20 20 20 20 20 77 73 64 50 72 6e  i];.      wsdPrn
aa130 67 2e 73 5b 69 5d 20 3d 20 74 3b 0a 20 20 20 20  g.s[i] = t;.    
aa140 7d 0a 20 20 20 20 77 73 64 50 72 6e 67 2e 69 73  }.    wsdPrng.is
aa150 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20  Init = 1;.  }.. 
aa160 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 64   /* Generate and
aa170 20 72 65 74 75 72 6e 20 73 69 6e 67 6c 65 20 72   return single r
aa180 61 6e 64 6f 6d 20 62 79 74 65 0a 20 20 2a 2f 0a  andom byte.  */.
aa190 20 20 77 73 64 50 72 6e 67 2e 69 2b 2b 3b 0a 20    wsdPrng.i++;. 
aa1a0 20 74 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b 77   t = wsdPrng.s[w
aa1b0 73 64 50 72 6e 67 2e 69 5d 3b 0a 20 20 77 73 64  sdPrng.i];.  wsd
aa1c0 50 72 6e 67 2e 6a 20 2b 3d 20 74 3b 0a 20 20 77  Prng.j += t;.  w
aa1d0 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67  sdPrng.s[wsdPrng
aa1e0 2e 69 5d 20 3d 20 77 73 64 50 72 6e 67 2e 73 5b  .i] = wsdPrng.s[
aa1f0 77 73 64 50 72 6e 67 2e 6a 5d 3b 0a 20 20 77 73  wsdPrng.j];.  ws
aa200 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67 2e  dPrng.s[wsdPrng.
aa210 6a 5d 20 3d 20 74 3b 0a 20 20 74 20 2b 3d 20 77  j] = t;.  t += w
aa220 73 64 50 72 6e 67 2e 73 5b 77 73 64 50 72 6e 67  sdPrng.s[wsdPrng
aa230 2e 69 5d 3b 0a 20 20 72 65 74 75 72 6e 20 77 73  .i];.  return ws
aa240 64 50 72 6e 67 2e 73 5b 74 5d 3b 0a 7d 0a 0a 2f  dPrng.s[t];.}../
aa250 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 4e 20 72 61  *.** Return N ra
aa260 6e 64 6f 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 53  ndom bytes..*/.S
aa270 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
aa280 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73  qlite3_randomnes
aa290 73 28 69 6e 74 20 4e 2c 20 76 6f 69 64 20 2a 70  s(int N, void *p
aa2a0 42 75 66 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  Buf){.  unsigned
aa2b0 20 63 68 61 72 20 2a 7a 42 75 66 20 3d 20 70 42   char *zBuf = pB
aa2c0 75 66 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54  uf;.#if SQLITE_T
aa2d0 48 52 45 41 44 53 41 46 45 0a 20 20 73 71 6c 69  HREADSAFE.  sqli
aa2e0 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78  te3_mutex *mutex
aa2f0 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41   = sqlite3MutexA
aa300 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45  lloc(SQLITE_MUTE
aa310 58 5f 53 54 41 54 49 43 5f 50 52 4e 47 29 3b 0a  X_STATIC_PRNG);.
aa320 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
aa330 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74  _mutex_enter(mut
aa340 65 78 29 3b 0a 20 20 77 68 69 6c 65 28 20 4e 2d  ex);.  while( N-
aa350 2d 20 29 7b 0a 20 20 20 20 2a 28 7a 42 75 66 2b  - ){.    *(zBuf+
aa360 2b 29 20 3d 20 72 61 6e 64 6f 6d 42 79 74 65 28  +) = randomByte(
aa370 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
aa380 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 6d 75 74  _mutex_leave(mut
aa390 65 78 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  ex);.}..#ifndef 
aa3a0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55 49 4c  SQLITE_OMIT_BUIL
aa3b0 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 46  TIN_TEST./*.** F
aa3c0 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72 70 6f  or testing purpo
aa3d0 73 65 73 2c 20 77 65 20 73 6f 6d 65 74 69 6d 65  ses, we sometime
aa3e0 73 20 77 61 6e 74 20 74 6f 20 70 72 65 73 65 72  s want to preser
aa3f0 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 0a  ve the state of.
aa400 2a 2a 20 50 52 4e 47 20 61 6e 64 20 72 65 73 74  ** PRNG and rest
aa410 6f 72 65 20 74 68 65 20 50 52 4e 47 20 74 6f 20  ore the PRNG to 
aa420 69 74 73 20 73 61 76 65 64 20 73 74 61 74 65 20  its saved state 
aa430 61 74 20 61 20 6c 61 74 65 72 20 74 69 6d 65 2c  at a later time,
aa440 20 6f 72 0a 2a 2a 20 74 6f 20 72 65 73 65 74 20   or.** to reset 
aa450 74 68 65 20 50 52 4e 47 20 74 6f 20 69 74 73 20  the PRNG to its 
aa460 69 6e 69 74 69 61 6c 20 73 74 61 74 65 2e 20 20  initial state.  
aa470 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61  These routines a
aa480 63 63 6f 6d 70 6c 69 73 68 0a 2a 2a 20 74 68 6f  ccomplish.** tho
aa490 73 65 20 74 61 73 6b 73 2e 0a 2a 2a 0a 2a 2a 20  se tasks..**.** 
aa4a0 54 68 65 20 73 71 6c 69 74 65 33 5f 74 65 73 74  The sqlite3_test
aa4b0 5f 63 6f 6e 74 72 6f 6c 28 29 20 69 6e 74 65 72  _control() inter
aa4c0 66 61 63 65 20 63 61 6c 6c 73 20 74 68 65 73 65  face calls these
aa4d0 20 72 6f 75 74 69 6e 65 73 20 74 6f 0a 2a 2a 20   routines to.** 
aa4e0 63 6f 6e 74 72 6f 6c 20 74 68 65 20 50 52 4e 47  control the PRNG
aa4f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 53 51 4c 49  ..*/.static SQLI
aa500 54 45 5f 57 53 44 20 73 74 72 75 63 74 20 73 71  TE_WSD struct sq
aa510 6c 69 74 65 33 50 72 6e 67 54 79 70 65 20 73 71  lite3PrngType sq
aa520 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 20 3d  lite3SavedPrng =
aa530 20 7b 20 30 2c 20 7d 3b 0a 53 51 4c 49 54 45 5f   { 0, };.SQLITE_
aa540 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
aa550 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74  ite3PrngSaveStat
aa560 65 28 76 6f 69 64 29 7b 0a 20 20 6d 65 6d 63 70  e(void){.  memcp
aa570 79 28 0a 20 20 20 20 26 47 4c 4f 42 41 4c 28 73  y(.    &GLOBAL(s
aa580 74 72 75 63 74 20 73 71 6c 69 74 65 33 50 72 6e  truct sqlite3Prn
aa590 67 54 79 70 65 2c 20 73 71 6c 69 74 65 33 53 61  gType, sqlite3Sa
aa5a0 76 65 64 50 72 6e 67 29 2c 0a 20 20 20 20 26 47  vedPrng),.    &G
aa5b0 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c  LOBAL(struct sql
aa5c0 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71  ite3PrngType, sq
aa5d0 6c 69 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20  lite3Prng),.    
aa5e0 73 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 50 72  sizeof(sqlite3Pr
aa5f0 6e 67 29 0a 20 20 29 3b 0a 7d 0a 53 51 4c 49 54  ng).  );.}.SQLIT
aa600 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
aa610 71 6c 69 74 65 33 50 72 6e 67 52 65 73 74 6f 72  qlite3PrngRestor
aa620 65 53 74 61 74 65 28 76 6f 69 64 29 7b 0a 20 20  eState(void){.  
aa630 6d 65 6d 63 70 79 28 0a 20 20 20 20 26 47 4c 4f  memcpy(.    &GLO
aa640 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c 69 74  BAL(struct sqlit
aa650 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71 6c 69  e3PrngType, sqli
aa660 74 65 33 50 72 6e 67 29 2c 0a 20 20 20 20 26 47  te3Prng),.    &G
aa670 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 73 71 6c  LOBAL(struct sql
aa680 69 74 65 33 50 72 6e 67 54 79 70 65 2c 20 73 71  ite3PrngType, sq
aa690 6c 69 74 65 33 53 61 76 65 64 50 72 6e 67 29 2c  lite3SavedPrng),
aa6a0 0a 20 20 20 20 73 69 7a 65 6f 66 28 73 71 6c 69  .    sizeof(sqli
aa6b0 74 65 33 50 72 6e 67 29 0a 20 20 29 3b 0a 7d 0a  te3Prng).  );.}.
aa6c0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
aa6d0 6f 69 64 20 73 71 6c 69 74 65 33 50 72 6e 67 52  oid sqlite3PrngR
aa6e0 65 73 65 74 53 74 61 74 65 28 76 6f 69 64 29 7b  esetState(void){
aa6f0 0a 20 20 47 4c 4f 42 41 4c 28 73 74 72 75 63 74  .  GLOBAL(struct
aa700 20 73 71 6c 69 74 65 33 50 72 6e 67 54 79 70 65   sqlite3PrngType
aa710 2c 20 73 71 6c 69 74 65 33 50 72 6e 67 29 2e 69  , sqlite3Prng).i
aa720 73 49 6e 69 74 20 3d 20 30 3b 0a 7d 0a 23 65 6e  sInit = 0;.}.#en
aa730 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
aa740 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20  IT_BUILTIN_TEST 
aa750 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */../***********
aa760 2a 2a 2a 20 45 6e 64 20 6f 66 20 72 61 6e 64 6f  *** End of rando
aa770 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
aa780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa7a0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
aa7b0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75  *** Begin file u
aa7c0 74 66 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tf.c ***********
aa7d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa7e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa7f0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41  **/./*.** 2004 A
aa800 70 72 69 6c 20 31 33 0a 2a 2a 0a 2a 2a 20 54 68  pril 13.**.** Th
aa810 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
aa820 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
aa830 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
aa840 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
aa850 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
aa860 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
aa870 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
aa880 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
aa890 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
aa8a0 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
aa8b0 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
aa8c0 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
aa8d0 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
aa8e0 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
aa8f0 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
aa900 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
aa910 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
aa920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa940 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aa960 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
aa970 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 72 6f 75  ile contains rou
aa980 74 69 6e 65 73 20 75 73 65 64 20 74 6f 20 74 72  tines used to tr
aa990 61 6e 73 6c 61 74 65 20 62 65 74 77 65 65 6e 20  anslate between 
aa9a0 55 54 46 2d 38 2c 20 0a 2a 2a 20 55 54 46 2d 31  UTF-8, .** UTF-1
aa9b0 36 2c 20 55 54 46 2d 31 36 42 45 2c 20 61 6e 64  6, UTF-16BE, and
aa9c0 20 55 54 46 2d 31 36 4c 45 2e 0a 2a 2a 0a 2a 2a   UTF-16LE..**.**
aa9d0 20 24 49 64 3a 20 75 74 66 2e 63 2c 76 20 31 2e   $Id: utf.c,v 1.
aa9e0 36 36 20 32 30 30 38 2f 31 31 2f 30 37 20 30 33  66 2008/11/07 03
aa9f0 3a 32 39 3a 33 34 20 64 72 68 20 45 78 70 20 24  :29:34 drh Exp $
aaa00 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20  .**.** Notes on 
aaa10 55 54 46 2d 38 3a 0a 2a 2a 0a 2a 2a 20 20 20 42  UTF-8:.**.**   B
aaa20 79 74 65 2d 30 20 20 20 20 42 79 74 65 2d 31 20  yte-0    Byte-1 
aaa30 20 20 20 42 79 74 65 2d 32 20 20 20 20 42 79 74     Byte-2    Byt
aaa40 65 2d 33 20 20 20 20 56 61 6c 75 65 0a 2a 2a 20  e-3    Value.** 
aaa50 20 30 78 78 78 78 78 78 78 20 20 20 20 20 20 20   0xxxxxxx       
aaa60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aaa70 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
aaa80 30 30 20 30 30 30 30 30 30 30 30 20 30 78 78 78  00 00000000 0xxx
aaa90 78 78 78 78 0a 2a 2a 20 20 31 31 30 79 79 79 79  xxxx.**  110yyyy
aaaa0 79 20 20 31 30 78 78 78 78 78 78 20 20 20 20 20  y  10xxxxxx     
aaab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aaac0 20 20 30 30 30 30 30 30 30 30 20 30 30 30 30 30    00000000 00000
aaad0 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20  yyy yyxxxxxx.** 
aaae0 20 31 31 31 30 7a 7a 7a 7a 20 20 31 30 79 79 79   1110zzzz  10yyy
aaaf0 79 79 79 20 20 31 30 78 78 78 78 78 78 20 20 20  yyy  10xxxxxx   
aab00 20 20 20 20 20 20 20 20 20 20 30 30 30 30 30 30            000000
aab10 30 30 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78  00 zzzzyyyy yyxx
aab20 78 78 78 78 0a 2a 2a 20 20 31 31 31 31 30 75 75  xxxx.**  11110uu
aab30 75 20 20 31 30 75 75 7a 7a 7a 7a 20 20 31 30 79  u  10uuzzzz  10y
aab40 79 79 79 79 79 20 20 31 30 78 78 78 78 78 78 20  yyyyy  10xxxxxx 
aab50 20 20 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79    000uuuuu zzzzy
aab60 79 79 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 0a  yyy yyxxxxxx.**.
aab70 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 6f 6e 20 55  **.** Notes on U
aab80 54 46 2d 31 36 3a 20 20 28 77 69 74 68 20 77 77  TF-16:  (with ww
aab90 77 77 2b 31 3d 3d 75 75 75 75 75 29 0a 2a 2a 0a  ww+1==uuuuu).**.
aaba0 2a 2a 20 20 20 20 20 20 57 6f 72 64 2d 30 20 20  **      Word-0  
aabb0 20 20 20 20 20 20 20 20 20 20 20 20 20 57 6f 72               Wor
aabc0 64 2d 31 20 20 20 20 20 20 20 20 20 20 56 61 6c  d-1          Val
aabd0 75 65 0a 2a 2a 20 20 31 31 30 31 31 30 77 77 20  ue.**  110110ww 
aabe0 77 77 7a 7a 7a 7a 79 79 20 20 20 31 31 30 31 31  wwzzzzyy   11011
aabf0 31 79 79 20 79 79 78 78 78 78 78 78 20 20 20 20  1yy yyxxxxxx    
aac00 30 30 30 75 75 75 75 75 20 7a 7a 7a 7a 79 79 79  000uuuuu zzzzyyy
aac10 79 20 79 79 78 78 78 78 78 78 0a 2a 2a 20 20 7a  y yyxxxxxx.**  z
aac20 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78 78 78  zzzyyyy yyxxxxxx
aac30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aac40 20 20 20 20 20 20 20 20 30 30 30 30 30 30 30 30          00000000
aac50 20 7a 7a 7a 7a 79 79 79 79 20 79 79 78 78 78 78   zzzzyyyy yyxxxx
aac60 78 78 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 42 4f 4d 20  xx.**.**.** BOM 
aac70 6f 72 20 42 79 74 65 20 4f 72 64 65 72 20 4d 61  or Byte Order Ma
aac80 72 6b 3a 0a 2a 2a 20 20 20 20 20 30 78 66 66 20  rk:.**     0xff 
aac90 30 78 66 65 20 20 20 6c 69 74 74 6c 65 2d 65 6e  0xfe   little-en
aaca0 64 69 61 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c  dian utf-16 foll
aacb0 6f 77 73 0a 2a 2a 20 20 20 20 20 30 78 66 65 20  ows.**     0xfe 
aacc0 30 78 66 66 20 20 20 62 69 67 2d 65 6e 64 69 61  0xff   big-endia
aacd0 6e 20 75 74 66 2d 31 36 20 66 6f 6c 6c 6f 77 73  n utf-16 follows
aace0 0a 2a 2a 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  .**.*/./********
aacf0 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 76  ****** Include v
aad00 64 62 65 49 6e 74 2e 68 20 69 6e 20 74 68 65 20  dbeInt.h in the 
aad10 6d 69 64 64 6c 65 20 6f 66 20 75 74 66 2e 63 20  middle of utf.c 
aad20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aad30 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
aad40 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
aad50 65 20 76 64 62 65 49 6e 74 2e 68 20 2a 2a 2a 2a  e vdbeInt.h ****
aad60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aad70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aad80 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
aad90 33 20 53 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a  3 September 6.**
aada0 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
aadb0 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
aadc0 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
aadd0 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
aade0 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
aadf0 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
aae00 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
aae10 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
aae20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
aae30 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
aae40 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
aae50 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
aae60 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
aae70 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
aae80 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
aae90 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
aaea0 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
aaeb0 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
aaec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aaed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aaee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
aaef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
aaf00 54 68 69 73 20 69 73 20 74 68 65 20 68 65 61 64  This is the head
aaf10 65 72 20 66 69 6c 65 20 66 6f 72 20 69 6e 66 6f  er file for info
aaf20 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 69 73 20  rmation that is 
aaf30 70 72 69 76 61 74 65 20 74 6f 20 74 68 65 0a 2a  private to the.*
aaf40 2a 20 56 44 42 45 2e 20 20 54 68 69 73 20 69 6e  * VDBE.  This in
aaf50 66 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 74  formation used t
aaf60 6f 20 61 6c 6c 20 62 65 20 61 74 20 74 68 65 20  o all be at the 
aaf70 74 6f 70 20 6f 66 20 74 68 65 20 73 69 6e 67 6c  top of the singl
aaf80 65 0a 2a 2a 20 73 6f 75 72 63 65 20 63 6f 64 65  e.** source code
aaf90 20 66 69 6c 65 20 22 76 64 62 65 2e 63 22 2e 20   file "vdbe.c". 
aafa0 20 57 68 65 6e 20 74 68 61 74 20 66 69 6c 65 20   When that file 
aafb0 62 65 63 61 6d 65 20 74 6f 6f 20 62 69 67 20 28  became too big (
aafc0 6f 76 65 72 0a 2a 2a 20 36 30 30 30 20 6c 69 6e  over.** 6000 lin
aafd0 65 73 20 6c 6f 6e 67 29 20 69 74 20 77 61 73 20  es long) it was 
aafe0 73 70 6c 69 74 20 75 70 20 69 6e 74 6f 20 73 65  split up into se
aaff0 76 65 72 61 6c 20 73 6d 61 6c 6c 65 72 20 66 69  veral smaller fi
ab000 6c 65 73 20 61 6e 64 0a 2a 2a 20 74 68 69 73 20  les and.** this 
ab010 68 65 61 64 65 72 20 69 6e 66 6f 72 6d 61 74 69  header informati
ab020 6f 6e 20 77 61 73 20 66 61 63 74 6f 72 65 64 20  on was factored 
ab030 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  out..**.** $Id: 
ab040 76 64 62 65 49 6e 74 2e 68 2c 76 20 31 2e 31 35  vdbeInt.h,v 1.15
ab050 38 20 32 30 30 38 2f 31 31 2f 31 37 20 31 35 3a  8 2008/11/17 15:
ab060 33 31 3a 34 38 20 64 61 6e 69 65 6c 6b 31 39 37  31:48 danielk197
ab070 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64  7 Exp $.*/.#ifnd
ab080 65 66 20 5f 56 44 42 45 49 4e 54 5f 48 5f 0a 23  ef _VDBEINT_H_.#
ab090 64 65 66 69 6e 65 20 5f 56 44 42 45 49 4e 54 5f  define _VDBEINT_
ab0a0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 69 6e 74 54 6f 4b  H_../*.** intToK
ab0b0 65 79 28 29 20 61 6e 64 20 6b 65 79 54 6f 49 6e  ey() and keyToIn
ab0c0 74 28 29 20 75 73 65 64 20 74 6f 20 74 72 61 6e  t() used to tran
ab0d0 73 66 6f 72 6d 20 74 68 65 20 72 6f 77 69 64 2e  sform the rowid.
ab0e0 20 20 42 75 74 20 77 69 74 68 0a 2a 2a 20 74 68    But with.** th
ab0f0 65 20 6c 61 74 65 73 74 20 76 65 72 73 69 6f 6e  e latest version
ab100 73 20 6f 66 20 74 68 65 20 64 65 73 69 67 6e 20  s of the design 
ab110 74 68 65 79 20 61 72 65 20 6e 6f 2d 6f 70 73 2e  they are no-ops.
ab120 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 6b 65 79 54  .*/.#define keyT
ab130 6f 49 6e 74 28 58 29 20 20 20 28 58 29 0a 23 64  oInt(X)   (X).#d
ab140 65 66 69 6e 65 20 69 6e 74 54 6f 4b 65 79 28 58  efine intToKey(X
ab150 29 20 20 20 28 58 29 0a 0a 0a 2f 2a 0a 2a 2a 20  )   (X).../*.** 
ab160 53 51 4c 20 69 73 20 74 72 61 6e 73 6c 61 74 65  SQL is translate
ab170 64 20 69 6e 74 6f 20 61 20 73 65 71 75 65 6e 63  d into a sequenc
ab180 65 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  e of instruction
ab190 73 20 74 6f 20 62 65 0a 2a 2a 20 65 78 65 63 75  s to be.** execu
ab1a0 74 65 64 20 62 79 20 61 20 76 69 72 74 75 61 6c  ted by a virtual
ab1b0 20 6d 61 63 68 69 6e 65 2e 20 20 45 61 63 68 20   machine.  Each 
ab1c0 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61  instruction is a
ab1d0 6e 20 69 6e 73 74 61 6e 63 65 0a 2a 2a 20 6f 66  n instance.** of
ab1e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
ab1f0 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 74 79 70  tructure..*/.typ
ab200 65 64 65 66 20 73 74 72 75 63 74 20 56 64 62 65  edef struct Vdbe
ab210 4f 70 20 4f 70 3b 0a 0a 2f 2a 0a 2a 2a 20 42 6f  Op Op;../*.** Bo
ab220 6f 6c 65 61 6e 20 76 61 6c 75 65 73 0a 2a 2f 0a  olean values.*/.
ab230 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
ab240 20 63 68 61 72 20 42 6f 6f 6c 3b 0a 0a 2f 2a 0a   char Bool;../*.
ab250 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20 61  ** A cursor is a
ab260 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 61 20   pointer into a 
ab270 73 69 6e 67 6c 65 20 42 54 72 65 65 20 77 69 74  single BTree wit
ab280 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20 66  hin a database f
ab290 69 6c 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73  ile..** The curs
ab2a0 6f 72 20 63 61 6e 20 73 65 65 6b 20 74 6f 20 61  or can seek to a
ab2b0 20 42 54 72 65 65 20 65 6e 74 72 79 20 77 69 74   BTree entry wit
ab2c0 68 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 6b  h a particular k
ab2d0 65 79 2c 20 6f 72 0a 2a 2a 20 6c 6f 6f 70 20 6f  ey, or.** loop o
ab2e0 76 65 72 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  ver all entries 
ab2f0 6f 66 20 74 68 65 20 42 74 72 65 65 2e 20 20 59  of the Btree.  Y
ab300 6f 75 20 63 61 6e 20 61 6c 73 6f 20 69 6e 73 65  ou can also inse
ab310 72 74 20 6e 65 77 20 42 54 72 65 65 0a 2a 2a 20  rt new BTree.** 
ab320 65 6e 74 72 69 65 73 20 6f 72 20 72 65 74 72 69  entries or retri
ab330 65 76 65 20 74 68 65 20 6b 65 79 20 6f 72 20 64  eve the key or d
ab340 61 74 61 20 66 72 6f 6d 20 74 68 65 20 65 6e 74  ata from the ent
ab350 72 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73  ry that the curs
ab360 6f 72 0a 2a 2a 20 69 73 20 63 75 72 72 65 6e 74  or.** is current
ab370 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a  ly pointing to..
ab380 2a 2a 20 0a 2a 2a 20 45 76 65 72 79 20 63 75 72  ** .** Every cur
ab390 73 6f 72 20 74 68 61 74 20 74 68 65 20 76 69 72  sor that the vir
ab3a0 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 68 61 73  tual machine has
ab3b0 20 6f 70 65 6e 20 69 73 20 72 65 70 72 65 73 65   open is represe
ab3c0 6e 74 65 64 20 62 79 20 61 6e 0a 2a 2a 20 69 6e  nted by an.** in
ab3d0 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f  stance of the fo
ab3e0 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72  llowing structur
ab3f0 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
ab400 56 64 62 65 43 75 72 73 6f 72 2e 69 73 54 72 69  VdbeCursor.isTri
ab410 67 67 65 72 52 6f 77 20 66 6c 61 67 20 69 73 20  ggerRow flag is 
ab420 73 65 74 20 69 74 20 6d 65 61 6e 73 20 74 68 61  set it means tha
ab430 74 20 74 68 69 73 20 63 75 72 73 6f 72 20 69 73  t this cursor is
ab440 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 20 73 69 6e  .** really a sin
ab450 67 6c 65 20 72 6f 77 20 74 68 61 74 20 72 65 70  gle row that rep
ab460 72 65 73 65 6e 74 73 20 74 68 65 20 4e 45 57 20  resents the NEW 
ab470 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f 2d 74 61  or OLD pseudo-ta
ab480 62 6c 65 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20  ble of.** a row 
ab490 74 72 69 67 67 65 72 2e 20 20 54 68 65 20 64 61  trigger.  The da
ab4a0 74 61 20 66 6f 72 20 74 68 65 20 72 6f 77 20 69  ta for the row i
ab4b0 73 20 73 74 6f 72 65 64 20 69 6e 20 56 64 62 65  s stored in Vdbe
ab4c0 43 75 72 73 6f 72 2e 70 44 61 74 61 20 61 6e 64  Cursor.pData and
ab4d0 0a 2a 2a 20 74 68 65 20 72 6f 77 69 64 20 69 73  .** the rowid is
ab4e0 20 69 6e 20 56 64 62 65 43 75 72 73 6f 72 2e 69   in VdbeCursor.i
ab4f0 4b 65 79 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 56  Key..*/.struct V
ab500 64 62 65 43 75 72 73 6f 72 20 7b 0a 20 20 42 74  dbeCursor {.  Bt
ab510 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  Cursor *pCursor;
ab520 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
ab530 72 20 73 74 72 75 63 74 75 72 65 20 6f 66 20 74  r structure of t
ab540 68 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20 20  he backend */.  
ab550 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
ab560 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
ab570 66 20 63 75 72 73 6f 72 20 64 61 74 61 62 61 73  f cursor databas
ab580 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20 28  e in db->aDb[] (
ab590 6f 72 20 2d 31 29 20 2a 2f 0a 20 20 69 36 34 20  or -1) */.  i64 
ab5a0 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 20 20  lastRowid;      
ab5b0 20 20 2f 2a 20 4c 61 73 74 20 72 6f 77 69 64 20    /* Last rowid 
ab5c0 66 72 6f 6d 20 61 20 4e 65 78 74 20 6f 72 20 4e  from a Next or N
ab5d0 65 78 74 49 64 78 20 6f 70 65 72 61 74 69 6f 6e  extIdx operation
ab5e0 20 2a 2f 0a 20 20 69 36 34 20 6e 65 78 74 52 6f   */.  i64 nextRo
ab5f0 77 69 64 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e  wid;        /* N
ab600 65 78 74 20 72 6f 77 69 64 20 72 65 74 75 72 6e  ext rowid return
ab610 65 64 20 62 79 20 4f 50 5f 4e 65 77 52 6f 77 69  ed by OP_NewRowi
ab620 64 20 2a 2f 0a 20 20 42 6f 6f 6c 20 7a 65 72 6f  d */.  Bool zero
ab630 65 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ed;          /* 
ab640 54 72 75 65 20 69 66 20 7a 65 72 6f 65 64 20 6f  True if zeroed o
ab650 75 74 20 61 6e 64 20 72 65 61 64 79 20 66 6f 72  ut and ready for
ab660 20 72 65 75 73 65 20 2a 2f 0a 20 20 42 6f 6f 6c   reuse */.  Bool
ab670 20 72 6f 77 69 64 49 73 56 61 6c 69 64 3b 20 20   rowidIsValid;  
ab680 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 61 73    /* True if las
ab690 74 52 6f 77 69 64 20 69 73 20 76 61 6c 69 64 20  tRowid is valid 
ab6a0 2a 2f 0a 20 20 42 6f 6f 6c 20 61 74 46 69 72 73  */.  Bool atFirs
ab6b0 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  t;         /* Tr
ab6c0 75 65 20 69 66 20 70 6f 69 6e 74 69 6e 67 20 74  ue if pointing t
ab6d0 6f 20 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f  o first entry */
ab6e0 0a 20 20 42 6f 6f 6c 20 75 73 65 52 61 6e 64 6f  .  Bool useRando
ab6f0 6d 52 6f 77 69 64 3b 20 20 2f 2a 20 47 65 6e 65  mRowid;  /* Gene
ab700 72 61 74 65 20 6e 65 77 20 72 65 63 6f 72 64 20  rate new record 
ab710 6e 75 6d 62 65 72 73 20 73 65 6d 69 2d 72 61 6e  numbers semi-ran
ab720 64 6f 6d 6c 79 20 2a 2f 0a 20 20 42 6f 6f 6c 20  domly */.  Bool 
ab730 6e 75 6c 6c 52 6f 77 3b 20 20 20 20 20 20 20 20  nullRow;        
ab740 20 2f 2a 20 54 72 75 65 20 69 66 20 70 6f 69 6e   /* True if poin
ab750 74 69 6e 67 20 74 6f 20 61 20 72 6f 77 20 77 69  ting to a row wi
ab760 74 68 20 6e 6f 20 64 61 74 61 20 2a 2f 0a 20 20  th no data */.  
ab770 42 6f 6f 6c 20 6e 65 78 74 52 6f 77 69 64 56 61  Bool nextRowidVa
ab780 6c 69 64 3b 20 20 2f 2a 20 54 72 75 65 20 69 66  lid;  /* True if
ab790 20 74 68 65 20 6e 65 78 74 52 6f 77 69 64 20 66   the nextRowid f
ab7a0 69 65 6c 64 20 69 73 20 76 61 6c 69 64 20 2a 2f  ield is valid */
ab7b0 0a 20 20 42 6f 6f 6c 20 70 73 65 75 64 6f 54 61  .  Bool pseudoTa
ab7c0 62 6c 65 3b 20 20 20 20 20 2f 2a 20 54 68 69 73  ble;     /* This
ab7d0 20 69 73 20 61 20 4e 45 57 20 6f 72 20 4f 4c 44   is a NEW or OLD
ab7e0 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 73 20 6f   pseudo-tables o
ab7f0 66 20 61 20 74 72 69 67 67 65 72 20 2a 2f 0a 20  f a trigger */. 
ab800 20 42 6f 6f 6c 20 65 70 68 65 6d 50 73 65 75 64   Bool ephemPseud
ab810 6f 54 61 62 6c 65 3b 0a 20 20 42 6f 6f 6c 20 64  oTable;.  Bool d
ab820 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3b 20 20  eferredMoveto;  
ab830 2f 2a 20 41 20 63 61 6c 6c 20 74 6f 20 73 71 6c  /* A call to sql
ab840 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 28  ite3BtreeMoveto(
ab850 29 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20  ) is needed */. 
ab860 20 42 6f 6f 6c 20 69 73 54 61 62 6c 65 3b 20 20   Bool isTable;  
ab870 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
ab880 66 20 61 20 74 61 62 6c 65 20 72 65 71 75 69 72  f a table requir
ab890 69 6e 67 20 69 6e 74 65 67 65 72 20 6b 65 79 73  ing integer keys
ab8a0 20 2a 2f 0a 20 20 42 6f 6f 6c 20 69 73 49 6e 64   */.  Bool isInd
ab8b0 65 78 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54  ex;         /* T
ab8c0 72 75 65 20 69 66 20 61 6e 20 69 6e 64 65 78 20  rue if an index 
ab8d0 63 6f 6e 74 61 69 6e 69 6e 67 20 6b 65 79 73 20  containing keys 
ab8e0 6f 6e 6c 79 20 2d 20 6e 6f 20 64 61 74 61 20 2a  only - no data *
ab8f0 2f 0a 20 20 69 36 34 20 6d 6f 76 65 74 6f 54 61  /.  i64 movetoTa
ab900 72 67 65 74 3b 20 20 20 20 20 2f 2a 20 41 72 67  rget;     /* Arg
ab910 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 64 65 66  ument to the def
ab920 65 72 72 65 64 20 73 71 6c 69 74 65 33 42 74 72  erred sqlite3Btr
ab930 65 65 4d 6f 76 65 74 6f 28 29 20 2a 2f 0a 20 20  eeMoveto() */.  
ab940 42 74 72 65 65 20 2a 70 42 74 3b 20 20 20 20 20  Btree *pBt;     
ab950 20 20 20 20 20 20 2f 2a 20 53 65 70 61 72 61 74        /* Separat
ab960 65 20 66 69 6c 65 20 68 6f 6c 64 69 6e 67 20 74  e file holding t
ab970 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 2a  emporary table *
ab980 2f 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 20 20  /.  int nData;  
ab990 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ab9a0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
ab9b0 70 44 61 74 61 20 2a 2f 0a 20 20 63 68 61 72 20  pData */.  char 
ab9c0 2a 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  *pData;         
ab9d0 20 2f 2a 20 44 61 74 61 20 66 6f 72 20 61 20 4e   /* Data for a N
ab9e0 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75 64 6f  EW or OLD pseudo
ab9f0 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 69 36 34 20  -table */.  i64 
aba00 69 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20 20  iKey;           
aba10 20 20 2f 2a 20 4b 65 79 20 66 6f 72 20 74 68 65    /* Key for the
aba20 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65 75   NEW or OLD pseu
aba30 64 6f 2d 74 61 62 6c 65 20 72 6f 77 20 2a 2f 0a  do-table row */.
aba40 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
aba50 6e 66 6f 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20  nfo;    /* Info 
aba60 61 62 6f 75 74 20 69 6e 64 65 78 20 6b 65 79 73  about index keys
aba70 20 6e 65 65 64 65 64 20 62 79 20 69 6e 64 65 78   needed by index
aba80 20 63 75 72 73 6f 72 73 20 2a 2f 0a 20 20 69 6e   cursors */.  in
aba90 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20 20  t nField;       
abaa0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
abab0 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65 20 68   fields in the h
abac0 65 61 64 65 72 20 2a 2f 0a 20 20 69 36 34 20 73  eader */.  i64 s
abad0 65 71 43 6f 75 6e 74 3b 20 20 20 20 20 20 20 20  eqCount;        
abae0 20 2f 2a 20 53 65 71 75 65 6e 63 65 20 63 6f 75   /* Sequence cou
abaf0 6e 74 65 72 20 2a 2f 0a 20 20 73 71 6c 69 74 65  nter */.  sqlite
abb00 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70  3_vtab_cursor *p
abb10 56 74 61 62 43 75 72 73 6f 72 3b 20 20 2f 2a 20  VtabCursor;  /* 
abb20 54 68 65 20 63 75 72 73 6f 72 20 66 6f 72 20 61  The cursor for a
abb30 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 2a   virtual table *
abb40 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  /.  const sqlite
abb50 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  3_module *pModul
abb60 65 3b 20 20 20 20 20 2f 2a 20 4d 6f 64 75 6c 65  e;     /* Module
abb70 20 66 6f 72 20 63 75 72 73 6f 72 20 70 56 74 61   for cursor pVta
abb80 62 43 75 72 73 6f 72 20 2a 2f 0a 0a 20 20 2f 2a  bCursor */..  /*
abb90 20 43 61 63 68 65 64 20 69 6e 66 6f 72 6d 61 74   Cached informat
abba0 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 68 65  ion about the he
abbb0 61 64 65 72 20 66 6f 72 20 74 68 65 20 64 61 74  ader for the dat
abbc0 61 20 72 65 63 6f 72 64 20 74 68 61 74 20 74 68  a record that th
abbd0 65 0a 20 20 2a 2a 20 63 75 72 73 6f 72 20 69 73  e.  ** cursor is
abbe0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
abbf0 69 6e 67 20 74 6f 2e 20 20 4f 6e 6c 79 20 76 61  ing to.  Only va
abc00 6c 69 64 20 69 66 20 63 61 63 68 65 56 61 6c 69  lid if cacheVali
abc10 64 20 69 73 20 74 72 75 65 2e 0a 20 20 2a 2a 20  d is true..  ** 
abc20 61 52 6f 77 20 6d 69 67 68 74 20 70 6f 69 6e 74  aRow might point
abc30 20 74 6f 20 28 65 70 68 65 6d 65 72 61 6c 29 20   to (ephemeral) 
abc40 64 61 74 61 20 66 6f 72 20 74 68 65 20 63 75 72  data for the cur
abc50 72 65 6e 74 20 72 6f 77 2c 20 6f 72 20 69 74 20  rent row, or it 
abc60 6d 69 67 68 74 0a 20 20 2a 2a 20 62 65 20 4e 55  might.  ** be NU
abc70 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 63  LL..  */.  int c
abc80 61 63 68 65 53 74 61 74 75 73 3b 20 20 20 20 20  acheStatus;     
abc90 20 2f 2a 20 43 61 63 68 65 20 69 73 20 76 61 6c   /* Cache is val
abca0 69 64 20 69 66 20 74 68 69 73 20 6d 61 74 63 68  id if this match
abcb0 65 73 20 56 64 62 65 2e 63 61 63 68 65 43 74 72  es Vdbe.cacheCtr
abcc0 20 2a 2f 0a 20 20 69 6e 74 20 70 61 79 6c 6f 61   */.  int payloa
abcd0 64 53 69 7a 65 3b 20 20 20 20 20 20 2f 2a 20 54  dSize;      /* T
abce0 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 62  otal number of b
abcf0 79 74 65 73 20 69 6e 20 74 68 65 20 72 65 63 6f  ytes in the reco
abd00 72 64 20 2a 2f 0a 20 20 75 33 32 20 2a 61 54 79  rd */.  u32 *aTy
abd10 70 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  pe;           /*
abd20 20 54 79 70 65 20 76 61 6c 75 65 73 20 66 6f 72   Type values for
abd30 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 69 6e 20   all entries in 
abd40 74 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20  the record */.  
abd50 75 33 32 20 2a 61 4f 66 66 73 65 74 3b 20 20 20  u32 *aOffset;   
abd60 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65 64 20        /* Cached 
abd70 6f 66 66 73 65 74 73 20 74 6f 20 74 68 65 20 73  offsets to the s
abd80 74 61 72 74 20 6f 66 20 65 61 63 68 20 63 6f 6c  tart of each col
abd90 75 6d 6e 73 20 64 61 74 61 20 2a 2f 0a 20 20 75  umns data */.  u
abda0 38 20 2a 61 52 6f 77 3b 20 20 20 20 20 20 20 20  8 *aRow;        
abdb0 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72       /* Data for
abdc0 20 74 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77   the current row
abdd0 2c 20 69 66 20 61 6c 6c 20 6f 6e 20 6f 6e 65 20  , if all on one 
abde0 70 61 67 65 20 2a 2f 0a 7d 3b 0a 74 79 70 65 64  page */.};.typed
abdf0 65 66 20 73 74 72 75 63 74 20 56 64 62 65 43 75  ef struct VdbeCu
abe00 72 73 6f 72 20 56 64 62 65 43 75 72 73 6f 72 3b  rsor VdbeCursor;
abe10 0a 0a 2f 2a 0a 2a 2a 20 41 20 76 61 6c 75 65 20  ../*.** A value 
abe20 66 6f 72 20 56 64 62 65 43 75 72 73 6f 72 2e 63  for VdbeCursor.c
abe30 61 63 68 65 56 61 6c 69 64 20 74 68 61 74 20 6d  acheValid that m
abe40 65 61 6e 73 20 74 68 65 20 63 61 63 68 65 20 69  eans the cache i
abe50 73 20 61 6c 77 61 79 73 20 69 6e 76 61 6c 69 64  s always invalid
abe60 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 43 41 43  ..*/.#define CAC
abe70 48 45 5f 53 54 41 4c 45 20 30 0a 0a 2f 2a 0a 2a  HE_STALE 0../*.*
abe80 2a 20 49 6e 74 65 72 6e 61 6c 6c 79 2c 20 74 68  * Internally, th
abe90 65 20 76 64 62 65 20 6d 61 6e 69 70 75 6c 61 74  e vdbe manipulat
abea0 65 73 20 6e 65 61 72 6c 79 20 61 6c 6c 20 53 51  es nearly all SQ
abeb0 4c 20 76 61 6c 75 65 73 20 61 73 20 4d 65 6d 0a  L values as Mem.
abec0 2a 2a 20 73 74 72 75 63 74 75 72 65 73 2e 20 45  ** structures. E
abed0 61 63 68 20 4d 65 6d 20 73 74 72 75 63 74 20 6d  ach Mem struct m
abee0 61 79 20 63 61 63 68 65 20 6d 75 6c 74 69 70 6c  ay cache multipl
abef0 65 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  e representation
abf00 73 20 28 73 74 72 69 6e 67 2c 0a 2a 2a 20 69 6e  s (string,.** in
abf10 74 65 67 65 72 20 65 74 63 2e 29 20 6f 66 20 74  teger etc.) of t
abf20 68 65 20 73 61 6d 65 20 76 61 6c 75 65 2e 20 20  he same value.  
abf30 41 20 76 61 6c 75 65 20 28 61 6e 64 20 74 68 65  A value (and the
abf40 72 65 66 6f 72 65 20 4d 65 6d 20 73 74 72 75 63  refore Mem struc
abf50 74 75 72 65 29 0a 2a 2a 20 68 61 73 20 74 68 65  ture).** has the
abf60 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 72 6f 70 65   following prope
abf70 72 74 69 65 73 3a 0a 2a 2a 0a 2a 2a 20 45 61 63  rties:.**.** Eac
abf80 68 20 76 61 6c 75 65 20 68 61 73 20 61 20 6d 61  h value has a ma
abf90 6e 69 66 65 73 74 20 74 79 70 65 2e 20 54 68 65  nifest type. The
abfa0 20 6d 61 6e 69 66 65 73 74 20 74 79 70 65 20 6f   manifest type o
abfb0 66 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  f the value stor
abfc0 65 64 0a 2a 2a 20 69 6e 20 61 20 4d 65 6d 20 73  ed.** in a Mem s
abfd0 74 72 75 63 74 20 69 73 20 72 65 74 75 72 6e 65  truct is returne
abfe0 64 20 62 79 20 74 68 65 20 4d 65 6d 54 79 70 65  d by the MemType
abff0 28 4d 65 6d 2a 29 20 6d 61 63 72 6f 2e 20 54 68  (Mem*) macro. Th
ac000 65 20 74 79 70 65 20 69 73 0a 2a 2a 20 6f 6e 65  e type is.** one
ac010 20 6f 66 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c   of SQLITE_NULL,
ac020 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 2c   SQLITE_INTEGER,
ac030 20 53 51 4c 49 54 45 5f 52 45 41 4c 2c 20 53 51   SQLITE_REAL, SQ
ac040 4c 49 54 45 5f 54 45 58 54 20 6f 72 0a 2a 2a 20  LITE_TEXT or.** 
ac050 53 51 4c 49 54 45 5f 42 4c 4f 42 2e 0a 2a 2f 0a  SQLITE_BLOB..*/.
ac060 73 74 72 75 63 74 20 4d 65 6d 20 7b 0a 20 20 75  struct Mem {.  u
ac070 6e 69 6f 6e 20 7b 0a 20 20 20 20 69 36 34 20 69  nion {.    i64 i
ac080 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
ac090 2a 20 49 6e 74 65 67 65 72 20 76 61 6c 75 65 2e  * Integer value.
ac0a0 20 4f 72 20 46 75 6e 63 44 65 66 2a 20 77 68 65   Or FuncDef* whe
ac0b0 6e 20 66 6c 61 67 73 3d 3d 4d 45 4d 5f 41 67 67  n flags==MEM_Agg
ac0c0 20 2a 2f 0a 20 20 20 20 46 75 6e 63 44 65 66 20   */.    FuncDef 
ac0d0 2a 70 44 65 66 3b 20 20 20 20 20 20 2f 2a 20 55  *pDef;      /* U
ac0e0 73 65 64 20 6f 6e 6c 79 20 77 68 65 6e 20 66 6c  sed only when fl
ac0f0 61 67 73 3d 3d 4d 45 4d 5f 41 67 67 20 2a 2f 0a  ags==MEM_Agg */.
ac100 20 20 7d 20 75 3b 0a 20 20 64 6f 75 62 6c 65 20    } u;.  double 
ac110 72 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  r;           /* 
ac120 52 65 61 6c 20 76 61 6c 75 65 20 2a 2f 0a 20 20  Real value */.  
ac130 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20  sqlite3 *db;    
ac140 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63      /* The assoc
ac150 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20 63  iated database c
ac160 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63  onnection */.  c
ac170 68 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20  har *z;         
ac180 20 20 20 2f 2a 20 53 74 72 69 6e 67 20 6f 72 20     /* String or 
ac190 42 4c 4f 42 20 76 61 6c 75 65 20 2a 2f 0a 20 20  BLOB value */.  
ac1a0 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20  int n;          
ac1b0 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ac1c0 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 73   characters in s
ac1d0 74 72 69 6e 67 20 76 61 6c 75 65 2c 20 65 78 63  tring value, exc
ac1e0 6c 75 64 69 6e 67 20 27 5c 30 27 20 2a 2f 0a 20  luding '\0' */. 
ac1f0 20 75 31 36 20 66 6c 61 67 73 3b 20 20 20 20 20   u16 flags;     
ac200 20 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d       /* Some com
ac210 62 69 6e 61 74 69 6f 6e 20 6f 66 20 4d 45 4d 5f  bination of MEM_
ac220 4e 75 6c 6c 2c 20 4d 45 4d 5f 53 74 72 2c 20 4d  Null, MEM_Str, M
ac230 45 4d 5f 44 79 6e 2c 20 65 74 63 2e 20 2a 2f 0a  EM_Dyn, etc. */.
ac240 20 20 75 38 20 20 74 79 70 65 3b 20 20 20 20 20    u8  type;     
ac250 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
ac260 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c 20 53 51 4c  SQLITE_NULL, SQL
ac270 49 54 45 5f 54 45 58 54 2c 20 53 51 4c 49 54 45  ITE_TEXT, SQLITE
ac280 5f 49 4e 54 45 47 45 52 2c 20 65 74 63 20 2a 2f  _INTEGER, etc */
ac290 0a 20 20 75 38 20 20 65 6e 63 3b 20 20 20 20 20  .  u8  enc;     
ac2a0 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 49 54 45         /* SQLITE
ac2b0 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f 55 54  _UTF8, SQLITE_UT
ac2c0 46 31 36 42 45 2c 20 53 51 4c 49 54 45 5f 55 54  F16BE, SQLITE_UT
ac2d0 46 31 36 4c 45 20 2a 2f 0a 20 20 76 6f 69 64 20  F16LE */.  void 
ac2e0 28 2a 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 3b  (*xDel)(void *);
ac2f0 20 20 2f 2a 20 49 66 20 6e 6f 74 20 6e 75 6c 6c    /* If not null
ac300 2c 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  , call this func
ac310 74 69 6f 6e 20 74 6f 20 64 65 6c 65 74 65 20 4d  tion to delete M
ac320 65 6d 2e 7a 20 2a 2f 0a 20 20 63 68 61 72 20 2a  em.z */.  char *
ac330 7a 4d 61 6c 6c 6f 63 3b 20 20 20 20 20 20 2f 2a  zMalloc;      /*
ac340 20 44 79 6e 61 6d 69 63 20 62 75 66 66 65 72 20   Dynamic buffer 
ac350 61 6c 6c 6f 63 61 74 65 64 20 62 79 20 73 71 6c  allocated by sql
ac360 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 2a 2f  ite3_malloc() */
ac370 0a 7d 3b 0a 0a 2f 2a 20 4f 6e 65 20 6f 72 20 6d  .};../* One or m
ac380 6f 72 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f  ore of the follo
ac390 77 69 6e 67 20 66 6c 61 67 73 20 61 72 65 20 73  wing flags are s
ac3a0 65 74 20 74 6f 20 69 6e 64 69 63 61 74 65 20 74  et to indicate t
ac3b0 68 65 20 76 61 6c 69 64 4f 4b 0a 2a 2a 20 72 65  he validOK.** re
ac3c0 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 20 6f 66  presentations of
ac3d0 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72 65   the value store
ac3e0 64 20 69 6e 20 74 68 65 20 4d 65 6d 20 73 74 72  d in the Mem str
ac3f0 75 63 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  uct..**.** If th
ac400 65 20 4d 45 4d 5f 4e 75 6c 6c 20 66 6c 61 67 20  e MEM_Null flag 
ac410 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
ac420 20 76 61 6c 75 65 20 69 73 20 61 6e 20 53 51 4c   value is an SQL
ac430 20 4e 55 4c 4c 20 76 61 6c 75 65 2e 0a 2a 2a 20   NULL value..** 
ac440 4e 6f 20 6f 74 68 65 72 20 66 6c 61 67 73 20 6d  No other flags m
ac450 61 79 20 62 65 20 73 65 74 20 69 6e 20 74 68 69  ay be set in thi
ac460 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  s case..**.** If
ac470 20 74 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61   the MEM_Str fla
ac480 67 20 69 73 20 73 65 74 20 74 68 65 6e 20 4d 65  g is set then Me
ac490 6d 2e 7a 20 70 6f 69 6e 74 73 20 61 74 20 61 20  m.z points at a 
ac4a0 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
ac4b0 61 74 69 6f 6e 2e 0a 2a 2a 20 55 73 75 61 6c 6c  ation..** Usuall
ac4c0 79 20 74 68 69 73 20 69 73 20 65 6e 63 6f 64 65  y this is encode
ac4d0 64 20 69 6e 20 74 68 65 20 73 61 6d 65 20 75 6e  d in the same un
ac4e0 69 63 6f 64 65 20 65 6e 63 6f 64 69 6e 67 20 61  icode encoding a
ac4f0 73 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61  s the main.** da
ac500 74 61 62 61 73 65 20 28 73 65 65 20 62 65 6c 6f  tabase (see belo
ac510 77 20 66 6f 72 20 65 78 63 65 70 74 69 6f 6e 73  w for exceptions
ac520 29 2e 20 49 66 20 74 68 65 20 4d 45 4d 5f 54 65  ). If the MEM_Te
ac530 72 6d 20 66 6c 61 67 20 69 73 20 61 6c 73 6f 0a  rm flag is also.
ac540 2a 2a 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  ** set, then the
ac550 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6c 20 74   string is nul t
ac560 65 72 6d 69 6e 61 74 65 64 2e 20 54 68 65 20 4d  erminated. The M
ac570 45 4d 5f 49 6e 74 20 61 6e 64 20 4d 45 4d 5f 52  EM_Int and MEM_R
ac580 65 61 6c 20 0a 2a 2a 20 66 6c 61 67 73 20 6d 61  eal .** flags ma
ac590 79 20 63 6f 65 78 69 73 74 20 77 69 74 68 20 74  y coexist with t
ac5a0 68 65 20 4d 45 4d 5f 53 74 72 20 66 6c 61 67 2e  he MEM_Str flag.
ac5b0 0a 2a 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 65 20  .**.** Multiple 
ac5c0 6f 66 20 74 68 65 73 65 20 76 61 6c 75 65 73 20  of these values 
ac5d0 63 61 6e 20 61 70 70 65 61 72 20 69 6e 20 4d 65  can appear in Me
ac5e0 6d 2e 66 6c 61 67 73 2e 20 20 42 75 74 20 6f 6e  m.flags.  But on
ac5f0 6c 79 20 6f 6e 65 0a 2a 2a 20 61 74 20 61 20 74  ly one.** at a t
ac600 69 6d 65 20 63 61 6e 20 61 70 70 65 61 72 20 69  ime can appear i
ac610 6e 20 4d 65 6d 2e 74 79 70 65 2e 0a 2a 2f 0a 23  n Mem.type..*/.#
ac620 64 65 66 69 6e 65 20 4d 45 4d 5f 4e 75 6c 6c 20  define MEM_Null 
ac630 20 20 20 20 20 30 78 30 30 30 31 20 20 20 2f 2a       0x0001   /*
ac640 20 56 61 6c 75 65 20 69 73 20 4e 55 4c 4c 20 2a   Value is NULL *
ac650 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 53 74  /.#define MEM_St
ac660 72 20 20 20 20 20 20 20 30 78 30 30 30 32 20 20  r       0x0002  
ac670 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 73   /* Value is a s
ac680 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66 69 6e 65  tring */.#define
ac690 20 4d 45 4d 5f 49 6e 74 20 20 20 20 20 20 20 30   MEM_Int       0
ac6a0 78 30 30 30 34 20 20 20 2f 2a 20 56 61 6c 75 65  x0004   /* Value
ac6b0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 2a   is an integer *
ac6c0 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 52 65  /.#define MEM_Re
ac6d0 61 6c 20 20 20 20 20 20 30 78 30 30 30 38 20 20  al      0x0008  
ac6e0 20 2f 2a 20 56 61 6c 75 65 20 69 73 20 61 20 72   /* Value is a r
ac6f0 65 61 6c 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 64  eal number */.#d
ac700 65 66 69 6e 65 20 4d 45 4d 5f 42 6c 6f 62 20 20  efine MEM_Blob  
ac710 20 20 20 20 30 78 30 30 31 30 20 20 20 2f 2a 20      0x0010   /* 
ac720 56 61 6c 75 65 20 69 73 20 61 20 42 4c 4f 42 20  Value is a BLOB 
ac730 2a 2f 0a 0a 23 64 65 66 69 6e 65 20 4d 65 6d 53  */..#define MemS
ac740 65 74 54 79 70 65 46 6c 61 67 28 70 2c 20 66 29  etTypeFlag(p, f)
ac750 20 5c 0a 20 20 28 28 70 29 2d 3e 66 6c 61 67 73   \.  ((p)->flags
ac760 20 3d 20 28 28 70 29 2d 3e 66 6c 61 67 73 26 7e   = ((p)->flags&~
ac770 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61  (MEM_Int|MEM_Rea
ac780 6c 7c 4d 45 4d 5f 4e 75 6c 6c 7c 4d 45 4d 5f 42  l|MEM_Null|MEM_B
ac790 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29 29 7c 66 29  lob|MEM_Str))|f)
ac7a0 0a 0a 2f 2a 20 57 68 65 6e 65 76 65 72 20 4d 65  ../* Whenever Me
ac7b0 6d 20 63 6f 6e 74 61 69 6e 73 20 61 20 76 61 6c  m contains a val
ac7c0 69 64 20 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  id string or blo
ac7d0 62 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  b representation
ac7e0 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 74 68 65 20  , one of.** the 
ac7f0 66 6f 6c 6c 6f 77 69 6e 67 20 66 6c 61 67 73 20  following flags 
ac800 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 64  must be set to d
ac810 65 74 65 72 6d 69 6e 65 20 74 68 65 20 6d 65 6d  etermine the mem
ac820 6f 72 79 20 6d 61 6e 61 67 65 6d 65 6e 74 0a 2a  ory management.*
ac830 2a 20 70 6f 6c 69 63 79 20 66 6f 72 20 4d 65 6d  * policy for Mem
ac840 2e 7a 2e 20 20 54 68 65 20 4d 45 4d 5f 54 65 72  .z.  The MEM_Ter
ac850 6d 20 66 6c 61 67 20 74 65 6c 6c 73 20 75 73 20  m flag tells us 
ac860 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74  whether or not t
ac870 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20  he.** string is 
ac880 5c 30 30 30 20 6f 72 20 5c 75 30 30 30 30 20 74  \000 or \u0000 t
ac890 65 72 6d 69 6e 61 74 65 64 0a 2a 2f 0a 23 64 65  erminated.*/.#de
ac8a0 66 69 6e 65 20 4d 45 4d 5f 54 65 72 6d 20 20 20  fine MEM_Term   
ac8b0 20 20 20 30 78 30 30 32 30 20 20 20 2f 2a 20 53     0x0020   /* S
ac8c0 74 72 69 6e 67 20 72 65 70 20 69 73 20 6e 75 6c  tring rep is nul
ac8d0 20 74 65 72 6d 69 6e 61 74 65 64 20 2a 2f 0a 23   terminated */.#
ac8e0 64 65 66 69 6e 65 20 4d 45 4d 5f 44 79 6e 20 20  define MEM_Dyn  
ac8f0 20 20 20 20 20 30 78 30 30 34 30 20 20 20 2f 2a       0x0040   /*
ac900 20 4e 65 65 64 20 74 6f 20 63 61 6c 6c 20 73 71   Need to call sq
ac910 6c 69 74 65 46 72 65 65 28 29 20 6f 6e 20 4d 65  liteFree() on Me
ac920 6d 2e 7a 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d  m.z */.#define M
ac930 45 4d 5f 53 74 61 74 69 63 20 20 20 20 30 78 30  EM_Static    0x0
ac940 30 38 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70  080   /* Mem.z p
ac950 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 61 74 69  oints to a stati
ac960 63 20 73 74 72 69 6e 67 20 2a 2f 0a 23 64 65 66  c string */.#def
ac970 69 6e 65 20 4d 45 4d 5f 45 70 68 65 6d 20 20 20  ine MEM_Ephem   
ac980 20 20 30 78 30 31 30 30 20 20 20 2f 2a 20 4d 65    0x0100   /* Me
ac990 6d 2e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e  m.z points to an
ac9a0 20 65 70 68 65 6d 65 72 61 6c 20 73 74 72 69 6e   ephemeral strin
ac9b0 67 20 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d  g */.#define MEM
ac9c0 5f 41 67 67 20 20 20 20 20 20 20 30 78 30 34 30  _Agg       0x040
ac9d0 30 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69  0   /* Mem.z poi
ac9e0 6e 74 73 20 74 6f 20 61 6e 20 61 67 67 20 66 75  nts to an agg fu
ac9f0 6e 63 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a  nction context *
aca00 2f 0a 23 64 65 66 69 6e 65 20 4d 45 4d 5f 5a 65  /.#define MEM_Ze
aca10 72 6f 20 20 20 20 20 20 30 78 30 38 30 30 20 20  ro      0x0800  
aca20 20 2f 2a 20 4d 65 6d 2e 69 20 63 6f 6e 74 61 69   /* Mem.i contai
aca30 6e 73 20 63 6f 75 6e 74 20 6f 66 20 30 73 20 61  ns count of 0s a
aca40 70 70 65 6e 64 65 64 20 74 6f 20 62 6c 6f 62 20  ppended to blob 
aca50 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
aca60 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a  E_OMIT_INCRBLOB.
aca70 20 20 23 75 6e 64 65 66 20 4d 45 4d 5f 5a 65 72    #undef MEM_Zer
aca80 6f 0a 20 20 23 64 65 66 69 6e 65 20 4d 45 4d 5f  o.  #define MEM_
aca90 5a 65 72 6f 20 30 78 30 30 30 30 0a 23 65 6e 64  Zero 0x0000.#end
acaa0 69 66 0a 0a 0a 2f 2a 20 41 20 56 64 62 65 46 75  if.../* A VdbeFu
acab0 6e 63 20 69 73 20 6a 75 73 74 20 61 20 46 75 6e  nc is just a Fun
acac0 63 44 65 66 20 28 64 65 66 69 6e 65 64 20 69 6e  cDef (defined in
acad0 20 73 71 6c 69 74 65 49 6e 74 2e 68 29 20 74 68   sqliteInt.h) th
acae0 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61  at contains.** a
acaf0 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
acb00 61 74 69 6f 6e 20 61 62 6f 75 74 20 61 75 78 69  ation about auxi
acb10 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61 74 69 6f  liary informatio
acb20 6e 20 62 6f 75 6e 64 20 74 6f 20 61 72 67 75 6d  n bound to argum
acb30 65 6e 74 73 0a 2a 2a 20 6f 66 20 74 68 65 20 66  ents.** of the f
acb40 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20 69  unction.  This i
acb50 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
acb60 65 6e 74 20 74 68 65 20 73 71 6c 69 74 65 33 5f  ent the sqlite3_
acb70 67 65 74 5f 61 75 78 64 61 74 61 28 29 0a 2a 2a  get_auxdata().**
acb80 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 73 65 74   and sqlite3_set
acb90 5f 61 75 78 64 61 74 61 28 29 20 41 50 49 73 2e  _auxdata() APIs.
acba0 20 20 54 68 65 20 22 61 75 78 64 61 74 61 22 20    The "auxdata" 
acbb0 69 73 20 73 6f 6d 65 20 61 75 78 69 6c 69 61 72  is some auxiliar
acbc0 79 20 64 61 74 61 0a 2a 2a 20 74 68 61 74 20 63  y data.** that c
acbd0 61 6e 20 62 65 20 61 73 73 6f 63 69 61 74 65 64  an be associated
acbe0 20 77 69 74 68 20 61 20 63 6f 6e 73 74 61 6e 74   with a constant
acbf0 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 61 20 66   argument to a f
acc00 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 0a 2a  unction.  This.*
acc10 2a 20 61 6c 6c 6f 77 73 20 66 75 6e 63 74 69 6f  * allows functio
acc20 6e 73 20 73 75 63 68 20 61 73 20 22 72 65 67 65  ns such as "rege
acc30 78 70 22 20 74 6f 20 63 6f 6d 70 69 6c 65 20 74  xp" to compile t
acc40 68 65 69 72 20 63 6f 6e 73 74 61 6e 74 20 72 65  heir constant re
acc50 67 75 6c 61 72 0a 2a 2a 20 65 78 70 72 65 73 73  gular.** express
acc60 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 6e 63  ion argument onc
acc70 65 20 61 6e 64 20 72 65 75 73 65 64 20 74 68 65  e and reused the
acc80 20 63 6f 6d 70 69 6c 65 64 20 63 6f 64 65 20 66   compiled code f
acc90 6f 72 20 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 69  or multiple.** i
acca0 6e 76 6f 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 73  nvocations..*/.s
accb0 74 72 75 63 74 20 56 64 62 65 46 75 6e 63 20 7b  truct VdbeFunc {
accc0 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 46 75 6e  .  FuncDef *pFun
accd0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
acce0 20 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69   /* The definiti
accf0 6f 6e 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69  on of the functi
acd00 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 41 75 78  on */.  int nAux
acd10 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
acd20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
acd30 6f 66 20 65 6e 74 72 69 65 73 20 61 6c 6c 6f 63  of entries alloc
acd40 61 74 65 64 20 66 6f 72 20 61 70 41 75 78 5b 5d  ated for apAux[]
acd50 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 41 75 78   */.  struct Aux
acd60 44 61 74 61 20 7b 0a 20 20 20 20 76 6f 69 64 20  Data {.    void 
acd70 2a 70 41 75 78 3b 20 20 20 20 20 20 20 20 20 20  *pAux;          
acd80 20 20 20 20 20 20 20 20 20 2f 2a 20 41 75 78 20           /* Aux 
acd90 64 61 74 61 20 66 6f 72 20 74 68 65 20 69 2d 74  data for the i-t
acda0 68 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 20 20  h argument */.  
acdb0 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 65 74 65    void (*xDelete
acdc0 29 28 76 6f 69 64 20 2a 29 3b 20 20 20 20 20 20  )(void *);      
acdd0 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f  /* Destructor fo
acde0 72 20 74 68 65 20 61 75 78 20 64 61 74 61 20 2a  r the aux data *
acdf0 2f 0a 20 20 7d 20 61 70 41 75 78 5b 31 5d 3b 20  /.  } apAux[1]; 
ace00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ace10 20 20 2f 2a 20 4f 6e 65 20 73 6c 6f 74 20 66 6f    /* One slot fo
ace20 72 20 65 61 63 68 20 66 75 6e 63 74 69 6f 6e 20  r each function 
ace30 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 7d 3b 0a 0a  argument */.};..
ace40 2f 2a 0a 2a 2a 20 54 68 65 20 22 63 6f 6e 74 65  /*.** The "conte
ace50 78 74 22 20 61 72 67 75 6d 65 6e 74 20 66 6f 72  xt" argument for
ace60 20 61 20 69 6e 73 74 61 6c 6c 61 62 6c 65 20 66   a installable f
ace70 75 6e 63 74 69 6f 6e 2e 20 20 41 20 70 6f 69 6e  unction.  A poin
ace80 74 65 72 20 74 6f 20 61 6e 0a 2a 2a 20 69 6e 73  ter to an.** ins
ace90 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74  tance of this st
acea0 72 75 63 74 75 72 65 20 69 73 20 74 68 65 20 66  ructure is the f
aceb0 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  irst argument to
acec0 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 75 73   the routines us
aced0 65 64 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20  ed.** implement 
acee0 74 68 65 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  the SQL function
acef0 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69  s..**.** There i
acf00 73 20 61 20 74 79 70 65 64 65 66 20 66 6f 72 20  s a typedef for 
acf10 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69  this structure i
acf20 6e 20 73 71 6c 69 74 65 2e 68 2e 20 20 53 6f 20  n sqlite.h.  So 
acf30 61 6c 6c 20 72 6f 75 74 69 6e 65 73 2c 0a 2a 2a  all routines,.**
acf40 20 65 76 65 6e 20 74 68 65 20 70 75 62 6c 69 63   even the public
acf50 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 53 51   interface to SQ
acf60 4c 69 74 65 2c 20 63 61 6e 20 75 73 65 20 61 20  Lite, can use a 
acf70 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73 20  pointer to this 
acf80 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20 42 75  structure..** Bu
acf90 74 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 74  t this file is t
acfa0 68 65 20 6f 6e 6c 79 20 70 6c 61 63 65 20 77 68  he only place wh
acfb0 65 72 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  ere the internal
acfc0 20 64 65 74 61 69 6c 73 20 6f 66 20 74 68 69 73   details of this
acfd0 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20 61 72  .** structure ar
acfe0 65 20 6b 6e 6f 77 6e 2e 0a 2a 2a 0a 2a 2a 20 54  e known..**.** T
acff0 68 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73  his structure is
ad000 20 64 65 66 69 6e 65 64 20 69 6e 73 69 64 65 20   defined inside 
ad010 6f 66 20 76 64 62 65 49 6e 74 2e 68 20 62 65 63  of vdbeInt.h bec
ad020 61 75 73 65 20 69 74 20 75 73 65 73 20 73 75 62  ause it uses sub
ad030 73 74 72 75 63 74 75 72 65 73 0a 2a 2a 20 28 4d  structures.** (M
ad040 65 6d 29 20 77 68 69 63 68 20 61 72 65 20 6f 6e  em) which are on
ad050 6c 79 20 64 65 66 69 6e 65 64 20 74 68 65 72 65  ly defined there
ad060 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  ..*/.struct sqli
ad070 74 65 33 5f 63 6f 6e 74 65 78 74 20 7b 0a 20 20  te3_context {.  
ad080 46 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 3b 20  FuncDef *pFunc; 
ad090 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
ad0a0 20 74 6f 20 66 75 6e 63 74 69 6f 6e 20 69 6e 66   to function inf
ad0b0 6f 72 6d 61 74 69 6f 6e 2e 20 20 4d 55 53 54 20  ormation.  MUST 
ad0c0 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20 56 64  BE FIRST */.  Vd
ad0d0 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e  beFunc *pVdbeFun
ad0e0 63 3b 20 20 2f 2a 20 41 75 78 69 6c 61 72 79 20  c;  /* Auxilary 
ad0f0 64 61 74 61 2c 20 69 66 20 63 72 65 61 74 65 64  data, if created
ad100 2e 20 2a 2f 0a 20 20 4d 65 6d 20 73 3b 20 20 20  . */.  Mem s;   
ad110 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ad120 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
ad130 20 69 73 20 73 74 6f 72 65 64 20 68 65 72 65 20   is stored here 
ad140 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 3b 20  */.  Mem *pMem; 
ad150 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 65             /* Me
ad160 6d 6f 72 79 20 63 65 6c 6c 20 75 73 65 64 20 74  mory cell used t
ad170 6f 20 73 74 6f 72 65 20 61 67 67 72 65 67 61 74  o store aggregat
ad180 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69  e context */.  i
ad190 6e 74 20 69 73 45 72 72 6f 72 3b 20 20 20 20 20  nt isError;     
ad1a0 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f       /* Error co
ad1b0 64 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 74  de returned by t
ad1c0 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 2a 2f 0a  he function. */.
ad1d0 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
ad1e0 3b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c 6c 61  ;       /* Colla
ad1f0 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 2a 2f  ting sequence */
ad200 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 53 65 74  .};../*.** A Set
ad210 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75 73   structure is us
ad220 65 64 20 66 6f 72 20 71 75 69 63 6b 20 74 65 73  ed for quick tes
ad230 74 69 6e 67 20 74 6f 20 73 65 65 20 69 66 20 61  ting to see if a
ad240 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20 70 61 72   value.** is par
ad250 74 20 6f 66 20 61 20 73 6d 61 6c 6c 20 73 65 74  t of a small set
ad260 2e 20 20 53 65 74 73 20 61 72 65 20 75 73 65 64  .  Sets are used
ad270 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 63 6f   to implement co
ad280 64 65 20 6c 69 6b 65 0a 2a 2a 20 74 68 69 73 3a  de like.** this:
ad290 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 78  .**            x
ad2a0 2e 79 20 49 4e 20 28 27 68 69 27 2c 27 68 6f 6f  .y IN ('hi','hoo
ad2b0 27 2c 27 68 75 6d 27 29 0a 2a 2f 0a 74 79 70 65  ','hum').*/.type
ad2c0 64 65 66 20 73 74 72 75 63 74 20 53 65 74 20 53  def struct Set S
ad2d0 65 74 3b 0a 73 74 72 75 63 74 20 53 65 74 20 7b  et;.struct Set {
ad2e0 0a 20 20 48 61 73 68 20 68 61 73 68 3b 20 20 20  .  Hash hash;   
ad2f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 73            /* A s
ad300 65 74 20 69 73 20 6a 75 73 74 20 61 20 68 61 73  et is just a has
ad310 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 48 61 73  h table */.  Has
ad320 68 45 6c 65 6d 20 2a 70 72 65 76 3b 20 20 20 20  hElem *prev;    
ad330 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 6c      /* Previousl
ad340 79 20 61 63 63 65 73 73 65 64 20 68 61 73 68 20  y accessed hash 
ad350 65 6c 65 6d 65 6e 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  elemen */.};../*
ad360 0a 2a 2a 20 41 20 46 69 66 6f 50 61 67 65 20 73  .** A FifoPage s
ad370 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61  tructure holds a
ad380 20 73 69 6e 67 6c 65 20 70 61 67 65 20 6f 66 20   single page of 
ad390 76 61 6c 76 65 73 2e 20 20 50 61 67 65 73 20 61  valves.  Pages a
ad3a0 72 65 20 61 72 72 61 6e 67 65 64 0a 2a 2a 20 69  re arranged.** i
ad3b0 6e 20 61 20 6c 69 73 74 2e 0a 2a 2f 0a 74 79 70  n a list..*/.typ
ad3c0 65 64 65 66 20 73 74 72 75 63 74 20 46 69 66 6f  edef struct Fifo
ad3d0 50 61 67 65 20 46 69 66 6f 50 61 67 65 3b 0a 73  Page FifoPage;.s
ad3e0 74 72 75 63 74 20 46 69 66 6f 50 61 67 65 20 7b  truct FifoPage {
ad3f0 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20  .  int nSlot;   
ad400 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
ad410 6f 66 20 65 6e 74 72 69 65 73 20 61 53 6c 6f 74  of entries aSlot
ad420 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 57 72 69  [] */.  int iWri
ad430 74 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 50 75  te;        /* Pu
ad440 73 68 20 74 68 65 20 6e 65 78 74 20 76 61 6c 75  sh the next valu
ad450 65 20 69 6e 74 6f 20 74 68 69 73 20 65 6e 74 72  e into this entr
ad460 79 20 69 6e 20 61 53 6c 6f 74 5b 5d 20 2a 2f 0a  y in aSlot[] */.
ad470 20 20 69 6e 74 20 69 52 65 61 64 3b 20 20 20 20    int iRead;    
ad480 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65       /* Read the
ad490 20 6e 65 78 74 20 76 61 6c 75 65 20 66 72 6f 6d   next value from
ad4a0 20 74 68 69 73 20 65 6e 74 72 79 20 69 6e 20 61   this entry in a
ad4b0 53 6c 6f 74 5b 5d 20 2a 2f 0a 20 20 46 69 66 6f  Slot[] */.  Fifo
ad4c0 50 61 67 65 20 2a 70 4e 65 78 74 3b 20 20 20 2f  Page *pNext;   /
ad4d0 2a 20 4e 65 78 74 20 70 61 67 65 20 69 6e 20 74  * Next page in t
ad4e0 68 65 20 66 69 66 6f 20 2a 2f 0a 20 20 69 36 34  he fifo */.  i64
ad4f0 20 61 53 6c 6f 74 5b 31 5d 3b 20 20 20 20 20 20   aSlot[1];      
ad500 2f 2a 20 4f 6e 65 20 6f 72 20 6d 6f 72 65 20 73  /* One or more s
ad510 6c 6f 74 73 20 66 6f 72 20 72 6f 77 69 64 20 76  lots for rowid v
ad520 61 6c 75 65 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  alues */.};../*.
ad530 2a 2a 20 54 68 65 20 46 69 66 6f 20 73 74 72 75  ** The Fifo stru
ad540 63 74 75 72 65 20 69 73 20 74 79 70 65 64 65 66  cture is typedef
ad550 2d 65 64 20 69 6e 20 76 64 62 65 49 6e 74 2e 68  -ed in vdbeInt.h
ad560 2e 20 20 42 75 74 20 74 68 65 20 69 6d 70 6c 65  .  But the imple
ad570 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 6f 66 20  mentation.** of 
ad580 74 68 61 74 20 73 74 72 75 63 74 75 72 65 20 69  that structure i
ad590 73 20 70 72 69 76 61 74 65 20 74 6f 20 74 68 69  s private to thi
ad5a0 73 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  s file..**.** Th
ad5b0 65 20 46 69 66 6f 20 73 74 72 75 63 74 75 72 65  e Fifo structure
ad5c0 20 64 65 73 63 72 69 62 65 73 20 74 68 65 20 65   describes the e
ad5d0 6e 74 69 72 65 20 66 69 66 6f 2e 20 20 0a 2a 2f  ntire fifo.  .*/
ad5e0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
ad5f0 46 69 66 6f 20 46 69 66 6f 3b 0a 73 74 72 75 63  Fifo Fifo;.struc
ad600 74 20 46 69 66 6f 20 7b 0a 20 20 69 6e 74 20 6e  t Fifo {.  int n
ad610 45 6e 74 72 79 3b 20 20 20 20 20 20 20 20 20 2f  Entry;         /
ad620 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  * Total number o
ad630 66 20 65 6e 74 72 69 65 73 20 2a 2f 0a 20 20 73  f entries */.  s
ad640 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
ad650 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f 63 69     /* The associ
ad660 61 74 65 64 20 64 61 74 61 62 61 73 65 20 63 6f  ated database co
ad670 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 46 69  nnection */.  Fi
ad680 66 6f 50 61 67 65 20 2a 70 46 69 72 73 74 3b 20  foPage *pFirst; 
ad690 20 20 2f 2a 20 46 69 72 73 74 20 70 61 67 65 20    /* First page 
ad6a0 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f 0a 20  on the list */. 
ad6b0 20 46 69 66 6f 50 61 67 65 20 2a 70 4c 61 73 74   FifoPage *pLast
ad6c0 3b 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 61 67  ;    /* Last pag
ad6d0 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 20 2a 2f  e on the list */
ad6e0 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 43 6f 6e  .};../*.** A Con
ad6f0 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20  text stores the 
ad700 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 69  last insert rowi
ad710 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61 74  d, the last stat
ad720 65 6d 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75  ement change cou
ad730 6e 74 2c 0a 2a 2a 20 61 6e 64 20 74 68 65 20 63  nt,.** and the c
ad740 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e 74  urrent statement
ad750 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 28 69   change count (i
ad760 2e 65 2e 20 63 68 61 6e 67 65 73 20 73 69 6e 63  .e. changes sinc
ad770 65 20 6c 61 73 74 20 73 74 61 74 65 6d 65 6e 74  e last statement
ad780 29 2e 0a 2a 2a 20 54 68 65 20 63 75 72 72 65 6e  )..** The curren
ad790 74 20 6b 65 79 6c 69 73 74 20 69 73 20 61 6c 73  t keylist is als
ad7a0 6f 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20  o stored in the 
ad7b0 63 6f 6e 74 65 78 74 2e 0a 2a 2a 20 45 6c 65 6d  context..** Elem
ad7c0 65 6e 74 73 20 6f 66 20 43 6f 6e 74 65 78 74 20  ents of Context 
ad7d0 73 74 72 75 63 74 75 72 65 20 74 79 70 65 20 6d  structure type m
ad7e0 61 6b 65 20 75 70 20 74 68 65 20 43 6f 6e 74 65  ake up the Conte
ad7f0 78 74 53 74 61 63 6b 2c 20 77 68 69 63 68 20 69  xtStack, which i
ad800 73 0a 2a 2a 20 75 70 64 61 74 65 64 20 62 79 20  s.** updated by 
ad810 74 68 65 20 43 6f 6e 74 65 78 74 50 75 73 68 20  the ContextPush 
ad820 61 6e 64 20 43 6f 6e 74 65 78 74 50 6f 70 20 6f  and ContextPop o
ad830 70 63 6f 64 65 73 20 28 75 73 65 64 20 62 79 20  pcodes (used by 
ad840 74 72 69 67 67 65 72 73 29 2e 0a 2a 2a 20 54 68  triggers)..** Th
ad850 65 20 63 6f 6e 74 65 78 74 20 69 73 20 70 75 73  e context is pus
ad860 68 65 64 20 62 65 66 6f 72 65 20 65 78 65 63 75  hed before execu
ad870 74 69 6e 67 20 61 20 74 72 69 67 67 65 72 20 61  ting a trigger a
ad880 20 70 6f 70 70 65 64 20 77 68 65 6e 20 74 68 65   popped when the
ad890 0a 2a 2a 20 74 72 69 67 67 65 72 20 66 69 6e 69  .** trigger fini
ad8a0 73 68 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  shes..*/.typedef
ad8b0 20 73 74 72 75 63 74 20 43 6f 6e 74 65 78 74 20   struct Context 
ad8c0 43 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20  Context;.struct 
ad8d0 43 6f 6e 74 65 78 74 20 7b 0a 20 20 69 36 34 20  Context {.  i64 
ad8e0 6c 61 73 74 52 6f 77 69 64 3b 20 20 20 20 2f 2a  lastRowid;    /*
ad8f0 20 4c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77   Last insert row
ad900 69 64 20 28 73 71 6c 69 74 65 33 2e 6c 61 73 74  id (sqlite3.last
ad910 52 6f 77 69 64 29 20 2a 2f 0a 20 20 69 6e 74 20  Rowid) */.  int 
ad920 6e 43 68 61 6e 67 65 3b 20 20 20 20 20 20 2f 2a  nChange;      /*
ad930 20 53 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67   Statement chang
ad940 65 73 20 28 56 64 62 65 2e 6e 43 68 61 6e 67 65  es (Vdbe.nChange
ad950 73 29 20 20 20 20 20 2a 2f 0a 20 20 46 69 66 6f  s)     */.  Fifo
ad960 20 73 46 69 66 6f 3b 20 20 20 20 20 20 20 2f 2a   sFifo;       /*
ad970 20 52 65 63 6f 72 64 73 20 74 68 61 74 20 77 69   Records that wi
ad980 6c 6c 20 70 61 72 74 69 63 69 70 61 74 65 20 69  ll participate i
ad990 6e 20 61 20 44 45 4c 45 54 45 20 6f 72 20 55 50  n a DELETE or UP
ad9a0 44 41 54 45 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a  DATE */.};../*.*
ad9b0 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
ad9c0 20 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63   the virtual mac
ad9d0 68 69 6e 65 2e 20 20 54 68 69 73 20 73 74 72 75  hine.  This stru
ad9e0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 74  cture contains t
ad9f0 68 65 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a 20 73  he complete.** s
ada00 74 61 74 65 20 6f 66 20 74 68 65 20 76 69 72 74  tate of the virt
ada10 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a 0a  ual machine..**.
ada20 2a 2a 20 54 68 65 20 22 73 71 6c 69 74 65 33 5f  ** The "sqlite3_
ada30 73 74 6d 74 22 20 73 74 72 75 63 74 75 72 65 20  stmt" structure 
ada40 70 6f 69 6e 74 65 72 20 74 68 61 74 20 69 73 20  pointer that is 
ada50 72 65 74 75 72 6e 65 64 20 62 79 20 73 71 6c 69  returned by sqli
ada60 74 65 33 5f 63 6f 6d 70 69 6c 65 28 29 0a 2a 2a  te3_compile().**
ada70 20 69 73 20 72 65 61 6c 6c 79 20 61 20 70 6f 69   is really a poi
ada80 6e 74 65 72 20 74 6f 20 61 6e 20 69 6e 73 74 61  nter to an insta
ada90 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
adaa0 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  cture..**.** The
adab0 20 56 64 62 65 2e 69 6e 56 74 61 62 4d 65 74 68   Vdbe.inVtabMeth
adac0 6f 64 20 76 61 72 69 61 62 6c 65 20 69 73 20 73  od variable is s
adad0 65 74 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 20 66  et to non-zero f
adae0 6f 72 20 74 68 65 20 64 75 72 61 74 69 6f 6e 20  or the duration 
adaf0 6f 66 0a 2a 2a 20 61 6e 79 20 76 69 72 74 75 61  of.** any virtua
adb00 6c 20 74 61 62 6c 65 20 6d 65 74 68 6f 64 20 69  l table method i
adb10 6e 76 6f 63 61 74 69 6f 6e 73 20 6d 61 64 65 20  nvocations made 
adb20 62 79 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  by the vdbe prog
adb30 72 61 6d 2e 20 49 74 20 69 73 0a 2a 2a 20 73 65  ram. It is.** se
adb40 74 20 74 6f 20 32 20 66 6f 72 20 78 44 65 73 74  t to 2 for xDest
adb50 72 6f 79 20 6d 65 74 68 6f 64 20 63 61 6c 6c 73  roy method calls
adb60 20 61 6e 64 20 31 20 66 6f 72 20 61 6c 6c 20 6f   and 1 for all o
adb70 74 68 65 72 20 6d 65 74 68 6f 64 73 2e 20 54 68  ther methods. Th
adb80 69 73 0a 2a 2a 20 76 61 72 69 61 62 6c 65 20 69  is.** variable i
adb90 73 20 75 73 65 64 20 66 6f 72 20 74 77 6f 20 70  s used for two p
adba0 75 72 70 6f 73 65 73 3a 20 74 6f 20 61 6c 6c 6f  urposes: to allo
adbb0 77 20 78 44 65 73 74 72 6f 79 20 6d 65 74 68 6f  w xDestroy metho
adbc0 64 73 20 74 6f 20 65 78 65 63 75 74 65 0a 2a 2a  ds to execute.**
adbd0 20 22 44 52 4f 50 20 54 41 42 4c 45 22 20 73 74   "DROP TABLE" st
adbe0 61 74 65 6d 65 6e 74 73 20 61 6e 64 20 74 6f 20  atements and to 
adbf0 70 72 65 76 65 6e 74 20 73 6f 6d 65 20 6e 61 73  prevent some nas
adc00 74 79 20 73 69 64 65 20 65 66 66 65 63 74 73 20  ty side effects 
adc10 6f 66 0a 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69  of.** malloc fai
adc20 6c 75 72 65 20 77 68 65 6e 20 53 51 4c 69 74 65  lure when SQLite
adc30 20 69 73 20 69 6e 76 6f 6b 65 64 20 72 65 63 75   is invoked recu
adc40 72 73 69 76 65 6c 79 20 62 79 20 61 20 76 69 72  rsively by a vir
adc50 74 75 61 6c 20 74 61 62 6c 65 20 0a 2a 2a 20 6d  tual table .** m
adc60 65 74 68 6f 64 20 66 75 6e 63 74 69 6f 6e 2e 0a  ethod function..
adc70 2a 2f 0a 73 74 72 75 63 74 20 56 64 62 65 20 7b  */.struct Vdbe {
adc80 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
adc90 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77 68         /* The wh
adca0 6f 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  ole database */.
adcb0 20 20 56 64 62 65 20 2a 70 50 72 65 76 2c 2a 70    Vdbe *pPrev,*p
adcc0 4e 65 78 74 3b 20 2f 2a 20 4c 69 6e 6b 65 64 20  Next; /* Linked 
adcd0 6c 69 73 74 20 6f 66 20 56 44 42 45 73 20 77 69  list of VDBEs wi
adce0 74 68 20 74 68 65 20 73 61 6d 65 20 56 64 62 65  th the same Vdbe
adcf0 2e 64 62 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 70  .db */.  int nOp
add00 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
add10 4e 75 6d 62 65 72 20 6f 66 20 69 6e 73 74 72 75  Number of instru
add20 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20 70 72  ctions in the pr
add30 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e  ogram */.  int n
add40 4f 70 41 6c 6c 6f 63 3b 20 20 20 20 20 20 20 2f  OpAlloc;       /
add50 2a 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74  * Number of slot
add60 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20  s allocated for 
add70 61 4f 70 5b 5d 20 2a 2f 0a 20 20 4f 70 20 2a 61  aOp[] */.  Op *a
add80 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  Op;            /
add90 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
adda0 74 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68  the virtual mach
addb0 69 6e 65 27 73 20 70 72 6f 67 72 61 6d 20 2a 2f  ine's program */
addc0 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 3b 20 20  .  int nLabel;  
addd0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
adde0 20 6f 66 20 6c 61 62 65 6c 73 20 75 73 65 64 20   of labels used 
addf0 2a 2f 0a 20 20 69 6e 74 20 6e 4c 61 62 65 6c 41  */.  int nLabelA
ade00 6c 6c 6f 63 3b 20 20 20 20 2f 2a 20 4e 75 6d 62  lloc;    /* Numb
ade10 65 72 20 6f 66 20 73 6c 6f 74 73 20 61 6c 6c 6f  er of slots allo
ade20 63 61 74 65 64 20 69 6e 20 61 4c 61 62 65 6c 5b  cated in aLabel[
ade30 5d 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 4c 61 62  ] */.  int *aLab
ade40 65 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 70  el;        /* Sp
ade50 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  ace to hold the 
ade60 6c 61 62 65 6c 73 20 2a 2f 0a 20 20 4d 65 6d 20  labels */.  Mem 
ade70 2a 2a 61 70 41 72 67 3b 20 20 20 20 20 20 20 20  **apArg;        
ade80 2f 2a 20 41 72 67 75 6d 65 6e 74 73 20 74 6f 20  /* Arguments to 
ade90 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
adea0 69 6e 67 20 75 73 65 72 20 66 75 6e 63 74 69 6f  ing user functio
adeb0 6e 20 2a 2f 0a 20 20 4d 65 6d 20 2a 61 43 6f 6c  n */.  Mem *aCol
adec0 4e 61 6d 65 3b 20 20 20 20 20 20 2f 2a 20 43 6f  Name;      /* Co
aded0 6c 75 6d 6e 20 6e 61 6d 65 73 20 74 6f 20 72 65  lumn names to re
adee0 74 75 72 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 43  turn */.  int nC
adef0 75 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a  ursor;        /*
adf00 20 4e 75 6d 62 65 72 20 6f 66 20 73 6c 6f 74 73   Number of slots
adf10 20 69 6e 20 61 70 43 73 72 5b 5d 20 2a 2f 0a 20   in apCsr[] */. 
adf20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 2a 61 70   VdbeCursor **ap
adf30 43 73 72 3b 20 2f 2a 20 4f 6e 65 20 65 6c 65 6d  Csr; /* One elem
adf40 65 6e 74 20 6f 66 20 74 68 69 73 20 61 72 72 61  ent of this arra
adf50 79 20 66 6f 72 20 65 61 63 68 20 6f 70 65 6e 20  y for each open 
adf60 63 75 72 73 6f 72 20 2a 2f 0a 20 20 69 6e 74 20  cursor */.  int 
adf70 6e 56 61 72 3b 20 20 20 20 20 20 20 20 20 20 20  nVar;           
adf80 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74  /* Number of ent
adf90 72 69 65 73 20 69 6e 20 61 56 61 72 5b 5d 20 2a  ries in aVar[] *
adfa0 2f 0a 20 20 4d 65 6d 20 2a 61 56 61 72 3b 20 20  /.  Mem *aVar;  
adfb0 20 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65          /* Value
adfc0 73 20 66 6f 72 20 74 68 65 20 4f 50 5f 56 61 72  s for the OP_Var
adfd0 69 61 62 6c 65 20 6f 70 63 6f 64 65 2e 20 2a 2f  iable opcode. */
adfe0 0a 20 20 63 68 61 72 20 2a 2a 61 7a 56 61 72 3b  .  char **azVar;
adff0 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
ae000 66 20 76 61 72 69 61 62 6c 65 73 20 2a 2f 0a 20  f variables */. 
ae010 20 69 6e 74 20 6f 6b 56 61 72 3b 20 20 20 20 20   int okVar;     
ae020 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
ae030 61 7a 56 61 72 5b 5d 20 68 61 73 20 62 65 65 6e  azVar[] has been
ae040 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a   initialized */.
ae050 20 20 75 33 32 20 6d 61 67 69 63 3b 20 20 20 20    u32 magic;    
ae060 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 67            /* Mag
ae070 69 63 20 6e 75 6d 62 65 72 20 66 6f 72 20 73 61  ic number for sa
ae080 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f  nity checking */
ae090 0a 20 20 69 6e 74 20 6e 4d 65 6d 3b 20 20 20 20  .  int nMem;    
ae0a0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
ae0b0 6d 62 65 72 20 6f 66 20 6d 65 6d 6f 72 79 20 6c  mber of memory l
ae0c0 6f 63 61 74 69 6f 6e 73 20 63 75 72 72 65 6e 74  ocations current
ae0d0 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a  ly allocated */.
ae0e0 20 20 4d 65 6d 20 2a 61 4d 65 6d 3b 20 20 20 20    Mem *aMem;    
ae0f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
ae100 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
ae110 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 61 6c 6c  s */.  int nCall
ae120 62 61 63 6b 3b 20 20 20 20 20 20 20 20 20 20 2f  back;          /
ae130 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c  * Number of call
ae140 62 61 63 6b 73 20 69 6e 76 6f 6b 65 64 20 73 6f  backs invoked so
ae150 20 66 61 72 20 2a 2f 0a 20 20 69 6e 74 20 63 61   far */.  int ca
ae160 63 68 65 43 74 72 3b 20 20 20 20 20 20 20 20 20  cheCtr;         
ae170 20 20 2f 2a 20 56 64 62 65 43 75 72 73 6f 72 20    /* VdbeCursor 
ae180 72 6f 77 20 63 61 63 68 65 20 67 65 6e 65 72 61  row cache genera
ae190 74 69 6f 6e 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  tion counter */.
ae1a0 20 20 46 69 66 6f 20 73 46 69 66 6f 3b 20 20 20    Fifo sFifo;   
ae1b0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 6c            /* A l
ae1c0 69 73 74 20 6f 66 20 52 4f 57 49 44 73 20 2a 2f  ist of ROWIDs */
ae1d0 0a 20 20 69 6e 74 20 63 6f 6e 74 65 78 74 53 74  .  int contextSt
ae1e0 61 63 6b 54 6f 70 3b 20 20 20 20 2f 2a 20 49 6e  ackTop;    /* In
ae1f0 64 65 78 20 6f 66 20 74 6f 70 20 65 6c 65 6d 65  dex of top eleme
ae200 6e 74 20 69 6e 20 74 68 65 20 63 6f 6e 74 65 78  nt in the contex
ae210 74 20 73 74 61 63 6b 20 2a 2f 0a 20 20 69 6e 74  t stack */.  int
ae220 20 63 6f 6e 74 65 78 74 53 74 61 63 6b 44 65 70   contextStackDep
ae230 74 68 3b 20 20 2f 2a 20 54 68 65 20 73 69 7a 65  th;  /* The size
ae240 20 6f 66 20 74 68 65 20 22 63 6f 6e 74 65 78 74   of the "context
ae250 22 20 73 74 61 63 6b 20 2a 2f 0a 20 20 43 6f 6e  " stack */.  Con
ae260 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 53 74 61  text *contextSta
ae270 63 6b 3b 20 20 2f 2a 20 53 74 61 63 6b 20 75 73  ck;  /* Stack us
ae280 65 64 20 62 79 20 6f 70 63 6f 64 65 73 20 43 6f  ed by opcodes Co
ae290 6e 74 65 78 74 50 75 73 68 20 26 20 43 6f 6e 74  ntextPush & Cont
ae2a0 65 78 74 50 6f 70 2a 2f 0a 20 20 69 6e 74 20 70  extPop*/.  int p
ae2b0 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c;              
ae2c0 20 20 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61     /* The progra
ae2d0 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  m counter */.  i
ae2e0 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
ae2f0 20 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20         /* Value 
ae300 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20 75  to return */.  u
ae310 6e 73 69 67 6e 65 64 20 75 6e 69 71 75 65 43 6e  nsigned uniqueCn
ae320 74 3b 20 20 20 20 20 2f 2a 20 55 73 65 64 20 62  t;     /* Used b
ae330 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20  y OP_MakeRecord 
ae340 77 68 65 6e 20 50 32 21 3d 30 20 2a 2f 0a 20 20  when P2!=0 */.  
ae350 69 6e 74 20 65 72 72 6f 72 41 63 74 69 6f 6e 3b  int errorAction;
ae360 20 20 20 20 20 20 20 20 2f 2a 20 52 65 63 6f 76          /* Recov
ae370 65 72 79 20 61 63 74 69 6f 6e 20 74 6f 20 64 6f  ery action to do
ae380 20 69 6e 20 63 61 73 65 20 6f 66 20 61 6e 20 65   in case of an e
ae390 72 72 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  rror */.  int in
ae3a0 54 65 6d 70 54 72 61 6e 73 3b 20 20 20 20 20 20  TempTrans;      
ae3b0 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 65 6d    /* True if tem
ae3c0 70 20 64 61 74 61 62 61 73 65 20 69 73 20 74 72  p database is tr
ae3d0 61 6e 73 61 63 74 69 6f 6e 65 64 20 2a 2f 0a 20  ansactioned */. 
ae3e0 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d 6e 3b   int nResColumn;
ae3f0 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
ae400 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
ae410 20 6f 6e 65 20 72 6f 77 20 6f 66 20 74 68 65 20   one row of the 
ae420 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20  result set */.  
ae430 63 68 61 72 20 2a 2a 61 7a 52 65 73 43 6f 6c 75  char **azResColu
ae440 6d 6e 3b 20 20 20 20 20 2f 2a 20 56 61 6c 75 65  mn;     /* Value
ae450 73 20 66 6f 72 20 6f 6e 65 20 72 6f 77 20 6f 66  s for one row of
ae460 20 72 65 73 75 6c 74 20 2a 2f 20 0a 20 20 63 68   result */ .  ch
ae470 61 72 20 2a 7a 45 72 72 4d 73 67 3b 20 20 20 20  ar *zErrMsg;    
ae480 20 20 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d        /* Error m
ae490 65 73 73 61 67 65 20 77 72 69 74 74 65 6e 20 68  essage written h
ae4a0 65 72 65 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 52  ere */.  Mem *pR
ae4b0 65 73 75 6c 74 53 65 74 3b 20 20 20 20 20 20 20  esultSet;       
ae4c0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61   /* Pointer to a
ae4d0 6e 20 61 72 72 61 79 20 6f 66 20 72 65 73 75 6c  n array of resul
ae4e0 74 73 20 2a 2f 0a 20 20 75 38 20 65 78 70 6c 61  ts */.  u8 expla
ae4f0 69 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  in;             
ae500 2f 2a 20 54 72 75 65 20 69 66 20 45 58 50 4c 41  /* True if EXPLA
ae510 49 4e 20 70 72 65 73 65 6e 74 20 6f 6e 20 53 51  IN present on SQ
ae520 4c 20 63 6f 6d 6d 61 6e 64 20 2a 2f 0a 20 20 75  L command */.  u
ae530 38 20 63 68 61 6e 67 65 43 6e 74 4f 6e 3b 20 20  8 changeCntOn;  
ae540 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 74         /* True t
ae550 6f 20 75 70 64 61 74 65 20 74 68 65 20 63 68 61  o update the cha
ae560 6e 67 65 2d 63 6f 75 6e 74 65 72 20 2a 2f 0a 20  nge-counter */. 
ae570 20 75 38 20 65 78 70 69 72 65 64 3b 20 20 20 20   u8 expired;    
ae580 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
ae590 20 69 66 20 74 68 65 20 56 4d 20 6e 65 65 64 73   if the VM needs
ae5a0 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c 65   to be recompile
ae5b0 64 20 2a 2f 0a 20 20 75 38 20 6d 69 6e 57 72 69  d */.  u8 minWri
ae5c0 74 65 46 69 6c 65 46 6f 72 6d 61 74 3b 20 20 2f  teFileFormat;  /
ae5d0 2a 20 4d 69 6e 69 6d 75 6d 20 66 69 6c 65 20 66  * Minimum file f
ae5e0 6f 72 6d 61 74 20 66 6f 72 20 77 72 69 74 61 62  ormat for writab
ae5f0 6c 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  le database file
ae600 73 20 2a 2f 0a 20 20 75 38 20 69 6e 56 74 61 62  s */.  u8 inVtab
ae610 4d 65 74 68 6f 64 3b 20 20 20 20 20 20 20 20 2f  Method;        /
ae620 2a 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20 61  * See comments a
ae630 62 6f 76 65 20 2a 2f 0a 20 20 75 38 20 75 73 65  bove */.  u8 use
ae640 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 3b 20 20 20  sStmtJournal;   
ae650 20 20 2f 2a 20 54 72 75 65 20 69 66 20 75 73 65    /* True if use
ae660 73 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  s a statement jo
ae670 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 72 65  urnal */.  u8 re
ae680 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20 20 20  adOnly;         
ae690 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 72     /* True for r
ae6a0 65 61 64 2d 6f 6e 6c 79 20 73 74 61 74 65 6d 65  ead-only stateme
ae6b0 6e 74 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68  nts */.  int nCh
ae6c0 61 6e 67 65 3b 20 20 20 20 20 20 20 20 20 20 20  ange;           
ae6d0 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 64 62   /* Number of db
ae6e0 20 63 68 61 6e 67 65 73 20 6d 61 64 65 20 73 69   changes made si
ae6f0 6e 63 65 20 6c 61 73 74 20 72 65 73 65 74 20 2a  nce last reset *
ae700 2f 0a 20 20 69 36 34 20 73 74 61 72 74 54 69 6d  /.  i64 startTim
ae710 65 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e;          /* T
ae720 69 6d 65 20 77 68 65 6e 20 71 75 65 72 79 20 73  ime when query s
ae730 74 61 72 74 65 64 20 2d 20 75 73 65 64 20 66 6f  tarted - used fo
ae740 72 20 70 72 6f 66 69 6c 69 6e 67 20 2a 2f 0a 20  r profiling */. 
ae750 20 69 6e 74 20 62 74 72 65 65 4d 61 73 6b 3b 20   int btreeMask; 
ae760 20 20 20 20 20 20 20 20 20 2f 2a 20 42 69 74 6d           /* Bitm
ae770 61 73 6b 20 6f 66 20 64 62 2d 3e 61 44 62 5b 5d  ask of db->aDb[]
ae780 20 65 6e 74 72 69 65 73 20 72 65 66 65 72 65 6e   entries referen
ae790 63 65 64 20 2a 2f 0a 20 20 42 74 72 65 65 4d 75  ced */.  BtreeMu
ae7a0 74 65 78 41 72 72 61 79 20 61 4d 75 74 65 78 3b  texArray aMutex;
ae7b0 20 2f 2a 20 41 6e 20 61 72 72 61 79 20 6f 66 20   /* An array of 
ae7c0 42 74 72 65 65 20 75 73 65 64 20 68 65 72 65 20  Btree used here 
ae7d0 61 6e 64 20 6e 65 65 64 69 6e 67 20 6c 6f 63 6b  and needing lock
ae7e0 73 20 2a 2f 0a 20 20 69 6e 74 20 61 43 6f 75 6e  s */.  int aCoun
ae7f0 74 65 72 5b 32 5d 3b 20 20 20 20 20 20 20 20 2f  ter[2];        /
ae800 2a 20 43 6f 75 6e 74 65 72 73 20 75 73 65 64 20  * Counters used 
ae810 62 79 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 5f  by sqlite3_stmt_
ae820 73 74 61 74 75 73 28 29 20 2a 2f 0a 20 20 69 6e  status() */.  in
ae830 74 20 6e 53 71 6c 3b 20 20 20 20 20 20 20 20 20  t nSql;         
ae840 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ae850 20 62 79 74 65 73 20 69 6e 20 7a 53 71 6c 20 2a   bytes in zSql *
ae860 2f 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 3b 20  /.  char *zSql; 
ae870 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78            /* Tex
ae880 74 20 6f 66 20 74 68 65 20 53 51 4c 20 73 74 61  t of the SQL sta
ae890 74 65 6d 65 6e 74 20 74 68 61 74 20 67 65 6e 65  tement that gene
ae8a0 72 61 74 65 64 20 74 68 69 73 20 2a 2f 0a 23 69  rated this */.#i
ae8b0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
ae8c0 47 0a 20 20 46 49 4c 45 20 2a 74 72 61 63 65 3b  G.  FILE *trace;
ae8d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
ae8e0 74 65 20 61 6e 20 65 78 65 63 75 74 69 6f 6e 20  te an execution 
ae8f0 74 72 61 63 65 20 68 65 72 65 2c 20 69 66 20 6e  trace here, if n
ae900 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 23 65 6e 64 69  ot NULL */.#endi
ae910 66 0a 20 20 69 6e 74 20 6f 70 65 6e 65 64 53 74  f.  int openedSt
ae920 61 74 65 6d 65 6e 74 3b 20 20 2f 2a 20 54 72 75  atement;  /* Tru
ae930 65 20 69 66 20 74 68 69 73 20 56 4d 20 68 61 73  e if this VM has
ae940 20 6f 70 65 6e 65 64 20 61 20 73 74 61 74 65 6d   opened a statem
ae950 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 23  ent journal */.#
ae960 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45  ifdef SQLITE_SSE
ae970 0a 20 20 69 6e 74 20 66 65 74 63 68 49 64 3b 20  .  int fetchId; 
ae980 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 74           /* Stat
ae990 65 6d 65 6e 74 20 6e 75 6d 62 65 72 20 75 73 65  ement number use
ae9a0 64 20 62 79 20 73 71 6c 69 74 65 33 5f 66 65 74  d by sqlite3_fet
ae9b0 63 68 5f 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ch_statement */.
ae9c0 20 20 69 6e 74 20 6c 72 75 3b 20 20 20 20 20 20    int lru;      
ae9d0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74          /* Count
ae9e0 65 72 20 75 73 65 64 20 66 6f 72 20 4c 52 55 20  er used for LRU 
ae9f0 63 61 63 68 65 20 72 65 70 6c 61 63 65 6d 65 6e  cache replacemen
aea00 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 64  t */.#endif.#ifd
aea10 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
aea20 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
aea30 4e 54 0a 20 20 56 64 62 65 20 2a 70 4c 72 75 50  NT.  Vdbe *pLruP
aea40 72 65 76 3b 0a 20 20 56 64 62 65 20 2a 70 4c 72  rev;.  Vdbe *pLr
aea50 75 4e 65 78 74 3b 0a 23 65 6e 64 69 66 0a 7d 3b  uNext;.#endif.};
aea60 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
aea70 6f 77 69 6e 67 20 61 72 65 20 61 6c 6c 6f 77 65  owing are allowe
aea80 64 20 76 61 6c 75 65 73 20 66 6f 72 20 56 64 62  d values for Vdb
aea90 65 2e 6d 61 67 69 63 0a 2a 2f 0a 23 64 65 66 69  e.magic.*/.#defi
aeaa0 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  ne VDBE_MAGIC_IN
aeab0 49 54 20 20 20 20 20 30 78 32 36 62 63 65 61 61  IT     0x26bceaa
aeac0 35 20 20 20 20 2f 2a 20 42 75 69 6c 64 69 6e 67  5    /* Building
aead0 20 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20   a VDBE program 
aeae0 2a 2f 0a 23 64 65 66 69 6e 65 20 56 44 42 45 5f  */.#define VDBE_
aeaf0 4d 41 47 49 43 5f 52 55 4e 20 20 20 20 20 20 30  MAGIC_RUN      0
aeb00 78 62 64 66 32 30 64 61 33 20 20 20 20 2f 2a 20  xbdf20da3    /* 
aeb10 56 44 42 45 20 69 73 20 72 65 61 64 79 20 74 6f  VDBE is ready to
aeb20 20 65 78 65 63 75 74 65 20 2a 2f 0a 23 64 65 66   execute */.#def
aeb30 69 6e 65 20 56 44 42 45 5f 4d 41 47 49 43 5f 48  ine VDBE_MAGIC_H
aeb40 41 4c 54 20 20 20 20 20 30 78 35 31 39 63 32 39  ALT     0x519c29
aeb50 37 33 20 20 20 20 2f 2a 20 56 44 42 45 20 68 61  73    /* VDBE ha
aeb60 73 20 63 6f 6d 70 6c 65 74 65 64 20 65 78 65 63  s completed exec
aeb70 75 74 69 6f 6e 20 2a 2f 0a 23 64 65 66 69 6e 65  ution */.#define
aeb80 20 56 44 42 45 5f 4d 41 47 49 43 5f 44 45 41 44   VDBE_MAGIC_DEAD
aeb90 20 20 20 20 20 30 78 62 36 30 36 63 33 63 38 20       0xb606c3c8 
aeba0 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 68     /* The VDBE h
aebb0 61 73 20 62 65 65 6e 20 64 65 61 6c 6c 6f 63 61  as been dealloca
aebc0 74 65 64 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 75  ted */../*.** Fu
aebd0 6e 63 74 69 6f 6e 20 70 72 6f 74 6f 74 79 70 65  nction prototype
aebe0 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  s.*/.SQLITE_PRIV
aebf0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
aec00 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 56  VdbeFreeCursor(V
aec10 64 62 65 20 2a 2c 20 56 64 62 65 43 75 72 73 6f  dbe *, VdbeCurso
aec20 72 2a 29 3b 0a 76 6f 69 64 20 73 71 6c 69 74 65  r*);.void sqlite
aec30 56 64 62 65 50 6f 70 53 74 61 63 6b 28 56 64 62  VdbePopStack(Vdb
aec40 65 2a 2c 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f  e*,int);.SQLITE_
aec50 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
aec60 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
aec70 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 2a 29  eto(VdbeCursor*)
aec80 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51  ;.#if defined(SQ
aec90 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c 20 64  LITE_DEBUG) || d
aeca0 65 66 69 6e 65 64 28 56 44 42 45 5f 50 52 4f 46  efined(VDBE_PROF
aecb0 49 4c 45 29 0a 53 51 4c 49 54 45 5f 50 52 49 56  ILE).SQLITE_PRIV
aecc0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
aecd0 56 64 62 65 50 72 69 6e 74 4f 70 28 46 49 4c 45  VdbePrintOp(FILE
aece0 2a 2c 20 69 6e 74 2c 20 4f 70 2a 29 3b 0a 23 65  *, int, Op*);.#e
aecf0 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49 56  ndif.SQLITE_PRIV
aed00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
aed10 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
aed20 28 75 33 32 29 3b 0a 53 51 4c 49 54 45 5f 50 52  (u32);.SQLITE_PR
aed30 49 56 41 54 45 20 75 33 32 20 73 71 6c 69 74 65  IVATE u32 sqlite
aed40 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28  3VdbeSerialType(
aed50 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c 49  Mem*, int);.SQLI
aed60 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
aed70 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
aed80 50 75 74 28 75 6e 73 69 67 6e 65 64 20 63 68 61  Put(unsigned cha
aed90 72 2a 2c 20 69 6e 74 2c 20 4d 65 6d 2a 2c 20 69  r*, int, Mem*, i
aeda0 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  nt);.SQLITE_PRIV
aedb0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
aedc0 64 62 65 53 65 72 69 61 6c 47 65 74 28 63 6f 6e  dbeSerialGet(con
aedd0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
aede0 2a 2c 20 75 33 32 2c 20 4d 65 6d 2a 29 3b 0a 53  *, u32, Mem*);.S
aedf0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
aee00 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  id sqlite3VdbeDe
aee10 6c 65 74 65 41 75 78 44 61 74 61 28 56 64 62 65  leteAuxData(Vdbe
aee20 46 75 6e 63 2a 2c 20 69 6e 74 29 3b 0a 0a 69 6e  Func*, int);..in
aee30 74 20 73 71 6c 69 74 65 32 42 74 72 65 65 4b 65  t sqlite2BtreeKe
aee40 79 43 6f 6d 70 61 72 65 28 42 74 43 75 72 73 6f  yCompare(BtCurso
aee50 72 20 2a 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  r *, const void 
aee60 2a 2c 20 69 6e 74 2c 20 69 6e 74 2c 20 69 6e 74  *, int, int, int
aee70 20 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56   *);.SQLITE_PRIV
aee80 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
aee90 64 62 65 49 64 78 4b 65 79 43 6f 6d 70 61 72 65  dbeIdxKeyCompare
aeea0 28 56 64 62 65 43 75 72 73 6f 72 2a 2c 55 6e 70  (VdbeCursor*,Unp
aeeb0 61 63 6b 65 64 52 65 63 6f 72 64 2a 2c 69 6e 74  ackedRecord*,int
aeec0 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  *);.SQLITE_PRIVA
aeed0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
aeee0 62 65 49 64 78 52 6f 77 69 64 28 42 74 43 75 72  beIdxRowid(BtCur
aeef0 73 6f 72 20 2a 2c 20 69 36 34 20 2a 29 3b 0a 53  sor *, i64 *);.S
aef00 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
aef10 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70  t sqlite3MemComp
aef20 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20  are(const Mem*, 
aef30 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20 63 6f 6e 73  const Mem*, cons
aef40 74 20 43 6f 6c 6c 53 65 71 2a 29 3b 0a 53 51 4c  t CollSeq*);.SQL
aef50 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
aef60 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63 28  sqlite3VdbeExec(
aef70 56 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  Vdbe*);.SQLITE_P
aef80 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
aef90 65 33 56 64 62 65 4c 69 73 74 28 56 64 62 65 2a  e3VdbeList(Vdbe*
aefa0 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
aefb0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
aefc0 65 48 61 6c 74 28 56 64 62 65 2a 29 3b 0a 53 51  eHalt(Vdbe*);.SQ
aefd0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
aefe0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
aeff0 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d 20 2a  geEncoding(Mem *
af000 2c 20 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50  , int);.SQLITE_P
af010 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
af020 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
af030 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
af040 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
af050 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 4d 65 6d  3VdbeMemCopy(Mem
af060 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 29 3b 0a  *, const Mem*);.
af070 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
af080 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
af090 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79 28 4d 65  emShallowCopy(Me
af0a0 6d 2a 2c 20 63 6f 6e 73 74 20 4d 65 6d 2a 2c 20  m*, const Mem*, 
af0b0 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
af0c0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
af0d0 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 4d 65 6d  3VdbeMemMove(Mem
af0e0 2a 2c 20 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45  *, Mem*);.SQLITE
af0f0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
af100 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
af110 72 6d 69 6e 61 74 65 28 4d 65 6d 2a 29 3b 0a 53  rminate(Mem*);.S
af120 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
af130 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
af140 53 65 74 53 74 72 28 4d 65 6d 2a 2c 20 63 6f 6e  SetStr(Mem*, con
af150 73 74 20 63 68 61 72 2a 2c 20 69 6e 74 2c 20 75  st char*, int, u
af160 38 2c 20 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a  8, void(*)(void*
af170 29 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ));.SQLITE_PRIVA
af180 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
af190 64 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d  dbeMemSetInt64(M
af1a0 65 6d 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  em*, i64);.SQLIT
af1b0 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
af1c0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
af1d0 44 6f 75 62 6c 65 28 4d 65 6d 2a 2c 20 64 6f 75  Double(Mem*, dou
af1e0 62 6c 65 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ble);.SQLITE_PRI
af1f0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
af200 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28  3VdbeMemSetNull(
af210 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
af220 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
af230 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f  e3VdbeMemSetZero
af240 42 6c 6f 62 28 4d 65 6d 2a 2c 69 6e 74 29 3b 0a  Blob(Mem*,int);.
af250 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
af260 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
af270 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d  mMakeWriteable(M
af280 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
af290 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
af2a0 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69 66 79  VdbeMemStringify
af2b0 28 4d 65 6d 2a 2c 20 69 6e 74 29 3b 0a 53 51 4c  (Mem*, int);.SQL
af2c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34 20  ITE_PRIVATE i64 
af2d0 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56 61  sqlite3VdbeIntVa
af2e0 6c 75 65 28 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54  lue(Mem*);.SQLIT
af2f0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
af300 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
af310 67 65 72 69 66 79 28 4d 65 6d 2a 29 3b 0a 53 51  gerify(Mem*);.SQ
af320 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75  LITE_PRIVATE dou
af330 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52  ble sqlite3VdbeR
af340 65 61 6c 56 61 6c 75 65 28 4d 65 6d 2a 29 3b 0a  ealValue(Mem*);.
af350 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
af360 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49  oid sqlite3VdbeI
af370 6e 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d  ntegerAffinity(M
af380 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
af390 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
af3a0 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 4d  VdbeMemRealify(M
af3b0 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  em*);.SQLITE_PRI
af3c0 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
af3d0 56 64 62 65 4d 65 6d 4e 75 6d 65 72 69 66 79 28  VdbeMemNumerify(
af3e0 4d 65 6d 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  Mem*);.SQLITE_PR
af3f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
af400 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
af410 65 28 42 74 43 75 72 73 6f 72 2a 2c 69 6e 74 2c  e(BtCursor*,int,
af420 69 6e 74 2c 69 6e 74 2c 4d 65 6d 2a 29 3b 0a 53  int,int,Mem*);.S
af430 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
af440 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
af450 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29  mRelease(Mem *p)
af460 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ;.SQLITE_PRIVATE
af470 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
af480 65 4d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72  eMemReleaseExter
af490 6e 61 6c 28 4d 65 6d 20 2a 70 29 3b 0a 53 51 4c  nal(Mem *p);.SQL
af4a0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
af4b0 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 46 69  sqlite3VdbeMemFi
af4c0 6e 61 6c 69 7a 65 28 4d 65 6d 2a 2c 20 46 75 6e  nalize(Mem*, Fun
af4d0 63 44 65 66 2a 29 3b 0a 53 51 4c 49 54 45 5f 50  cDef*);.SQLITE_P
af4e0 52 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61  RIVATE const cha
af4f0 72 20 2a 73 71 6c 69 74 65 33 4f 70 63 6f 64 65  r *sqlite3Opcode
af500 4e 61 6d 65 28 69 6e 74 29 3b 0a 53 51 4c 49 54  Name(int);.SQLIT
af510 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
af520 6c 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48  lite3VdbeOpcodeH
af530 61 73 50 72 6f 70 65 72 74 79 28 69 6e 74 2c 20  asProperty(int, 
af540 69 6e 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  int);.SQLITE_PRI
af550 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
af560 56 64 62 65 4d 65 6d 47 72 6f 77 28 4d 65 6d 20  VdbeMemGrow(Mem 
af570 2a 70 4d 65 6d 2c 20 69 6e 74 20 6e 2c 20 69 6e  *pMem, int n, in
af580 74 20 70 72 65 73 65 72 76 65 29 3b 0a 23 69 66  t preserve);.#if
af590 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
af5a0 45 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d  E_MEMORY_MANAGEM
af5b0 45 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  ENT.SQLITE_PRIVA
af5c0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
af5d0 62 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73  beReleaseBuffers
af5e0 28 56 64 62 65 20 2a 70 29 3b 0a 23 65 6e 64 69  (Vdbe *p);.#endi
af5f0 66 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  f..#ifndef NDEBU
af600 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
af610 20 20 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56     void sqlite3V
af620 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65 6d  dbeMemSanity(Mem
af630 2a 29 3b 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54  *);.#endif.SQLIT
af640 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
af650 6c 69 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e  lite3VdbeMemTran
af660 73 6c 61 74 65 28 4d 65 6d 2a 2c 20 75 38 29 3b  slate(Mem*, u8);
af670 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
af680 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56  EBUG.SQLITE_PRIV
af690 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c 69 74  ATE   void sqlit
af6a0 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28 56  e3VdbePrintSql(V
af6b0 64 62 65 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  dbe*);.SQLITE_PR
af6c0 49 56 41 54 45 20 20 20 76 6f 69 64 20 73 71 6c  IVATE   void sql
af6d0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
af6e0 79 50 72 69 6e 74 28 4d 65 6d 20 2a 70 4d 65 6d  yPrint(Mem *pMem
af6f0 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 3b 0a 23  , char *zBuf);.#
af700 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f 50 52 49  endif.SQLITE_PRI
af710 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
af720 56 64 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d  VdbeMemHandleBom
af730 28 4d 65 6d 20 2a 70 4d 65 6d 29 3b 0a 53 51 4c  (Mem *pMem);.SQL
af740 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
af750 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f   sqlite3VdbeFifo
af760 49 6e 69 74 28 46 69 66 6f 2a 2c 20 73 71 6c 69  Init(Fifo*, sqli
af770 74 65 33 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52  te3*);.SQLITE_PR
af780 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
af790 33 56 64 62 65 46 69 66 6f 50 75 73 68 28 46 69  3VdbeFifoPush(Fi
af7a0 66 6f 2a 2c 20 69 36 34 29 3b 0a 53 51 4c 49 54  fo*, i64);.SQLIT
af7b0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
af7c0 6c 69 74 65 33 56 64 62 65 46 69 66 6f 50 6f 70  lite3VdbeFifoPop
af7d0 28 46 69 66 6f 2a 2c 20 69 36 34 2a 29 3b 0a 53  (Fifo*, i64*);.S
af7e0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
af7f0 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  id sqlite3VdbeFi
af800 66 6f 43 6c 65 61 72 28 46 69 66 6f 2a 29 3b 0a  foClear(Fifo*);.
af810 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
af820 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 53 51  OMIT_INCRBLOB.SQ
af830 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 69  LITE_PRIVATE   i
af840 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
af850 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d 20  mExpandBlob(Mem 
af860 2a 29 3b 0a 23 65 6c 73 65 0a 20 20 23 64 65 66  *);.#else.  #def
af870 69 6e 65 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ine sqlite3VdbeM
af880 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 78 29 20  emExpandBlob(x) 
af890 53 51 4c 49 54 45 5f 4f 4b 0a 23 65 6e 64 69 66  SQLITE_OK.#endif
af8a0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
af8b0 69 6e 65 64 28 5f 56 44 42 45 49 4e 54 5f 48 5f  ined(_VDBEINT_H_
af8c0 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
af8d0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
af8e0 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
af8f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af910 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
af920 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
af930 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
af940 66 66 20 69 6e 20 75 74 66 2e 63 20 2a 2a 2a 2a  ff in utf.c ****
af950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
af960 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ****/../*.** The
af970 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 6e 73 74   following const
af980 61 6e 74 20 76 61 6c 75 65 20 69 73 20 75 73 65  ant value is use
af990 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45 5f  d by the SQLITE_
af9a0 42 49 47 45 4e 44 49 41 4e 20 61 6e 64 0a 2a 2a  BIGENDIAN and.**
af9b0 20 53 51 4c 49 54 45 5f 4c 49 54 54 4c 45 45 4e   SQLITE_LITTLEEN
af9c0 44 49 41 4e 20 6d 61 63 72 6f 73 2e 0a 2a 2f 0a  DIAN macros..*/.
af9d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63  SQLITE_PRIVATE c
af9e0 6f 6e 73 74 20 69 6e 74 20 73 71 6c 69 74 65 33  onst int sqlite3
af9f0 6f 6e 65 20 3d 20 31 3b 0a 0a 2f 2a 0a 2a 2a 20  one = 1;../*.** 
afa00 54 68 69 73 20 6c 6f 6f 6b 75 70 20 74 61 62 6c  This lookup tabl
afa10 65 20 69 73 20 75 73 65 64 20 74 6f 20 68 65 6c  e is used to hel
afa20 70 20 64 65 63 6f 64 65 20 74 68 65 20 66 69 72  p decode the fir
afa30 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 61 20  st byte of.** a 
afa40 6d 75 6c 74 69 2d 62 79 74 65 20 55 54 46 38 20  multi-byte UTF8 
afa50 63 68 61 72 61 63 74 65 72 2e 0a 2a 2f 0a 73 74  character..*/.st
afa60 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
afa70 6e 65 64 20 63 68 61 72 20 73 71 6c 69 74 65 33  ned char sqlite3
afa80 55 74 66 54 72 61 6e 73 31 5b 5d 20 3d 20 7b 0a  UtfTrans1[] = {.
afa90 20 20 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78    0x00, 0x01, 0x
afaa0 30 32 2c 20 30 78 30 33 2c 20 30 78 30 34 2c 20  02, 0x03, 0x04, 
afab0 30 78 30 35 2c 20 30 78 30 36 2c 20 30 78 30 37  0x05, 0x06, 0x07
afac0 2c 0a 20 20 30 78 30 38 2c 20 30 78 30 39 2c 20  ,.  0x08, 0x09, 
afad0 30 78 30 61 2c 20 30 78 30 62 2c 20 30 78 30 63  0x0a, 0x0b, 0x0c
afae0 2c 20 30 78 30 64 2c 20 30 78 30 65 2c 20 30 78  , 0x0d, 0x0e, 0x
afaf0 30 66 2c 0a 20 20 30 78 31 30 2c 20 30 78 31 31  0f,.  0x10, 0x11
afb00 2c 20 30 78 31 32 2c 20 30 78 31 33 2c 20 30 78  , 0x12, 0x13, 0x
afb10 31 34 2c 20 30 78 31 35 2c 20 30 78 31 36 2c 20  14, 0x15, 0x16, 
afb20 30 78 31 37 2c 0a 20 20 30 78 31 38 2c 20 30 78  0x17,.  0x18, 0x
afb30 31 39 2c 20 30 78 31 61 2c 20 30 78 31 62 2c 20  19, 0x1a, 0x1b, 
afb40 30 78 31 63 2c 20 30 78 31 64 2c 20 30 78 31 65  0x1c, 0x1d, 0x1e
afb50 2c 20 30 78 31 66 2c 0a 20 20 30 78 30 30 2c 20  , 0x1f,.  0x00, 
afb60 30 78 30 31 2c 20 30 78 30 32 2c 20 30 78 30 33  0x01, 0x02, 0x03
afb70 2c 20 30 78 30 34 2c 20 30 78 30 35 2c 20 30 78  , 0x04, 0x05, 0x
afb80 30 36 2c 20 30 78 30 37 2c 0a 20 20 30 78 30 38  06, 0x07,.  0x08
afb90 2c 20 30 78 30 39 2c 20 30 78 30 61 2c 20 30 78  , 0x09, 0x0a, 0x
afba0 30 62 2c 20 30 78 30 63 2c 20 30 78 30 64 2c 20  0b, 0x0c, 0x0d, 
afbb0 30 78 30 65 2c 20 30 78 30 66 2c 0a 20 20 30 78  0x0e, 0x0f,.  0x
afbc0 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32 2c 20  00, 0x01, 0x02, 
afbd0 30 78 30 33 2c 20 30 78 30 34 2c 20 30 78 30 35  0x03, 0x04, 0x05
afbe0 2c 20 30 78 30 36 2c 20 30 78 30 37 2c 0a 20 20  , 0x06, 0x07,.  
afbf0 30 78 30 30 2c 20 30 78 30 31 2c 20 30 78 30 32  0x00, 0x01, 0x02
afc00 2c 20 30 78 30 33 2c 20 30 78 30 30 2c 20 30 78  , 0x03, 0x00, 0x
afc10 30 31 2c 20 30 78 30 30 2c 20 30 78 30 30 2c 0a  01, 0x00, 0x00,.
afc20 7d 3b 0a 0a 0a 23 64 65 66 69 6e 65 20 57 52 49  };...#define WRI
afc30 54 45 5f 55 54 46 38 28 7a 4f 75 74 2c 20 63 29  TE_UTF8(zOut, c)
afc40 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20   {              
afc50 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
afc60 69 66 28 20 63 3c 30 78 30 30 30 38 30 20 29 7b  if( c<0x00080 ){
afc70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afc80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afc90 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
afca0 2b 2b 20 3d 20 28 63 26 30 78 46 46 29 3b 20 20  ++ = (c&0xFF);  
afcb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afcc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
afcd0 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20 20    }             
afce0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afd00 20 20 20 20 20 20 20 5c 0a 20 20 65 6c 73 65 20         \.  else 
afd10 69 66 28 20 63 3c 30 78 30 30 38 30 30 20 29 7b  if( c<0x00800 ){
afd20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afd30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afd40 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20  \.    *zOut++ = 
afd50 30 78 43 30 20 2b 20 28 28 63 3e 3e 36 29 26 30  0xC0 + ((c>>6)&0
afd60 78 31 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x1F);           
afd70 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
afd80 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20  zOut++ = 0x80 + 
afd90 28 63 20 26 20 30 78 33 46 29 3b 20 20 20 20 20  (c & 0x3F);     
afda0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afdb0 20 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20    \.  }         
afdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afdd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afde0 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 65             \.  e
afdf0 6c 73 65 20 69 66 28 20 63 3c 30 78 31 30 30 30  lse if( c<0x1000
afe00 30 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  0 ){            
afe10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afe20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b      \.    *zOut+
afe30 2b 20 3d 20 30 78 45 30 20 2b 20 28 28 63 3e 3e  + = 0xE0 + ((c>>
afe40 31 32 29 26 30 78 30 46 29 3b 20 20 20 20 20 20  12)&0x0F);      
afe50 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
afe60 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38     *zOut++ = 0x8
afe70 30 20 2b 20 28 28 63 3e 3e 36 29 20 26 20 30 78  0 + ((c>>6) & 0x
afe80 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20  3F);            
afe90 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
afea0 74 2b 2b 20 3d 20 30 78 38 30 20 2b 20 28 63 20  t++ = 0x80 + (c 
afeb0 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20 20  & 0x3F);        
afec0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
afed0 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20  .  }else{       
afee0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
afef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aff00 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
aff10 4f 75 74 2b 2b 20 3d 20 30 78 46 30 20 2b 20 28  Out++ = 0xF0 + (
aff20 28 63 3e 3e 31 38 29 20 26 20 30 78 30 37 29 3b  (c>>18) & 0x07);
aff30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
aff40 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d   \.    *zOut++ =
aff50 20 30 78 38 30 20 2b 20 28 28 63 3e 3e 31 32 29   0x80 + ((c>>12)
aff60 20 26 20 30 78 33 46 29 3b 20 20 20 20 20 20 20   & 0x3F);       
aff70 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
aff80 2a 7a 4f 75 74 2b 2b 20 3d 20 30 78 38 30 20 2b  *zOut++ = 0x80 +
aff90 20 28 28 63 3e 3e 36 29 20 26 20 30 78 33 46 29   ((c>>6) & 0x3F)
affa0 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
affb0 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
affc0 20 3d 20 30 78 38 30 20 2b 20 28 63 20 26 20 30   = 0x80 + (c & 0
affd0 78 33 46 29 3b 20 20 20 20 20 20 20 20 20 20 20  x3F);           
affe0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
afff0 7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  }               
b0000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0010 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0020 20 20 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e       \.}..#defin
b0030 65 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28  e WRITE_UTF16LE(
b0040 7a 4f 75 74 2c 20 63 29 20 7b 20 20 20 20 20 20  zOut, c) {      
b0050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0060 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
b0070 28 20 63 3c 3d 30 78 46 46 46 46 20 29 7b 20 20  ( c<=0xFFFF ){  
b0080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b00a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
b00b0 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 63 26 30    *zOut++ = (c&0
b00c0 78 30 30 46 46 29 3b 20 20 20 20 20 20 20 20 20  x00FF);         
b00d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b00e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
b00f0 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28      *zOut++ = ((
b0100 63 3e 3e 38 29 26 30 78 30 30 46 46 29 3b 20 20  c>>8)&0x00FF);  
b0110 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0130 5c 0a 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20  \.  }else{      
b0140 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0170 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20    \.    *zOut++ 
b0180 3d 20 28 28 28 63 3e 3e 31 30 29 26 30 78 30 30  = (((c>>10)&0x00
b0190 33 46 29 20 2b 20 28 28 28 63 2d 30 78 31 30 30  3F) + (((c-0x100
b01a0 30 30 29 3e 3e 31 30 29 26 30 78 30 30 43 30 29  00)>>10)&0x00C0)
b01b0 29 3b 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b  );  \.    *zOut+
b01c0 2b 20 3d 20 28 30 78 30 30 44 38 20 2b 20 28 28  + = (0x00D8 + ((
b01d0 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 38 29  (c-0x10000)>>18)
b01e0 26 30 78 30 33 29 29 3b 20 20 20 20 20 20 20 20  &0x03));        
b01f0 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75        \.    *zOu
b0200 74 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29  t++ = (c&0x00FF)
b0210 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b0220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0230 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a          \.    *z
b0240 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 43 20  Out++ = (0x00DC 
b0250 2b 20 28 28 63 3e 3e 38 29 26 30 78 30 33 29 29  + ((c>>8)&0x03))
b0260 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
b0270 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 7d 20            \.  } 
b0280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0290 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b02a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b02b0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d 0a              \.}.
b02c0 0a 23 64 65 66 69 6e 65 20 57 52 49 54 45 5f 55  .#define WRITE_U
b02d0 54 46 31 36 42 45 28 7a 4f 75 74 2c 20 63 29 20  TF16BE(zOut, c) 
b02e0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
b02f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0300 20 5c 0a 20 20 69 66 28 20 63 3c 3d 30 78 46 46   \.  if( c<=0xFF
b0310 46 46 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  FF ){           
b0320 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0340 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b     \.    *zOut++
b0350 20 3d 20 28 28 63 3e 3e 38 29 26 30 78 30 30 46   = ((c>>8)&0x00F
b0360 46 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  F);             
b0370 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0380 20 20 20 20 20 5c 0a 20 20 20 20 2a 7a 4f 75 74       \.    *zOut
b0390 2b 2b 20 3d 20 28 63 26 30 78 30 30 46 46 29 3b  ++ = (c&0x00FF);
b03a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b03b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b03c0 20 20 20 20 20 20 20 5c 0a 20 20 7d 65 6c 73 65         \.  }else
b03d0 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {               
b03e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b03f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0400 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 2a           \.    *
b0410 7a 4f 75 74 2b 2b 20 3d 20 28 30 78 30 30 44 38  zOut++ = (0x00D8
b0420 20 2b 20 28 28 28 63 2d 30 78 31 30 30 30 30 29   + (((c-0x10000)
b0430 3e 3e 31 38 29 26 30 78 30 33 29 29 3b 20 20 20  >>18)&0x03));   
b0440 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
b0450 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 28 28 63 3e   *zOut++ = (((c>
b0460 3e 31 30 29 26 30 78 30 30 33 46 29 20 2b 20 28  >10)&0x003F) + (
b0470 28 28 63 2d 30 78 31 30 30 30 30 29 3e 3e 31 30  ((c-0x10000)>>10
b0480 29 26 30 78 30 30 43 30 29 29 3b 20 20 5c 0a 20  )&0x00C0));  \. 
b0490 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28 30 78     *zOut++ = (0x
b04a0 30 30 44 43 20 2b 20 28 28 63 3e 3e 38 29 26 30  00DC + ((c>>8)&0
b04b0 78 30 33 29 29 3b 20 20 20 20 20 20 20 20 20 20  x03));          
b04c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b04d0 0a 20 20 20 20 2a 7a 4f 75 74 2b 2b 20 3d 20 28  .    *zOut++ = (
b04e0 63 26 30 78 30 30 46 46 29 3b 20 20 20 20 20 20  c&0x00FF);      
b04f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0500 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0510 20 5c 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20   \.  }          
b0520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0530 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0550 20 20 20 5c 0a 7d 0a 0a 23 64 65 66 69 6e 65 20     \.}..#define 
b0560 52 45 41 44 5f 55 54 46 31 36 4c 45 28 7a 49 6e  READ_UTF16LE(zIn
b0570 2c 20 63 29 7b 20 20 20 20 20 20 20 20 20 20 20  , c){           
b0580 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0590 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
b05a0 20 20 63 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20    c = (*zIn++); 
b05b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b05c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b05d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b05e0 20 20 20 20 20 20 5c 0a 20 20 63 20 2b 3d 20 28        \.  c += (
b05f0 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20  (*zIn++)<<8);   
b0600 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
b0630 20 20 69 66 28 20 63 3e 3d 30 78 44 38 30 30 20    if( c>=0xD800 
b0640 26 26 20 63 3c 30 78 45 30 30 30 20 29 7b 20 20  && c<0xE000 ){  
b0650 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0660 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0670 20 20 20 20 20 5c 0a 20 20 20 20 69 6e 74 20 63       \.    int c
b0680 32 20 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20  2 = (*zIn++);   
b0690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b06a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b06b0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
b06c0 20 20 20 63 32 20 2b 3d 20 28 28 2a 7a 49 6e 2b     c2 += ((*zIn+
b06d0 2b 29 3c 3c 38 29 3b 20 20 20 20 20 20 20 20 20  +)<<8);         
b06e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b06f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0700 20 20 20 20 20 5c 0a 20 20 20 20 63 20 3d 20 28       \.    c = (
b0710 63 32 26 30 78 30 33 46 46 29 20 2b 20 28 28 63  c2&0x03FF) + ((c
b0720 26 30 78 30 30 33 46 29 3c 3c 31 30 29 20 2b 20  &0x003F)<<10) + 
b0730 28 28 28 63 26 30 78 30 33 43 30 29 2b 30 78 30  (((c&0x03C0)+0x0
b0740 30 34 30 29 3c 3c 31 30 29 3b 20 20 20 5c 0a 20  040)<<10);   \. 
b0750 20 20 20 69 66 28 20 28 63 20 26 20 30 78 46 46     if( (c & 0xFF
b0760 46 46 30 30 30 30 29 3d 3d 30 20 29 20 63 20 3d  FF0000)==0 ) c =
b0770 20 30 78 46 46 46 44 3b 20 20 20 20 20 20 20 20   0xFFFD;        
b0780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0790 20 20 20 20 20 5c 0a 20 20 7d 20 20 20 20 20 20       \.  }      
b07a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b07b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b07c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b07d0 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 7d               \.}
b07e0 0a 0a 23 64 65 66 69 6e 65 20 52 45 41 44 5f 55  ..#define READ_U
b07f0 54 46 31 36 42 45 28 7a 49 6e 2c 20 63 29 7b 20  TF16BE(zIn, c){ 
b0800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0810 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0820 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
b0830 28 28 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20  ((*zIn++)<<8);  
b0840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0860 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0870 5c 0a 20 20 63 20 2b 3d 20 28 2a 7a 49 6e 2b 2b  \.  c += (*zIn++
b0880 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  );              
b0890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b08a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b08b0 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66 28 20          \.  if( 
b08c0 63 3e 3d 30 78 44 38 30 30 20 26 26 20 63 3c 30  c>=0xD800 && c<0
b08d0 78 45 30 30 30 20 29 7b 20 20 20 20 20 20 20 20  xE000 ){        
b08e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b08f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b0900 0a 20 20 20 20 69 6e 74 20 63 32 20 3d 20 28 28  .    int c2 = ((
b0910 2a 7a 49 6e 2b 2b 29 3c 3c 38 29 3b 20 20 20 20  *zIn++)<<8);    
b0920 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0940 20 20 20 20 20 20 20 5c 0a 20 20 20 20 63 32 20         \.    c2 
b0950 2b 3d 20 28 2a 7a 49 6e 2b 2b 29 3b 20 20 20 20  += (*zIn++);    
b0960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b0990 0a 20 20 20 20 63 20 3d 20 28 63 32 26 30 78 30  .    c = (c2&0x0
b09a0 33 46 46 29 20 2b 20 28 28 63 26 30 78 30 30 33  3FF) + ((c&0x003
b09b0 46 29 3c 3c 31 30 29 20 2b 20 28 28 28 63 26 30  F)<<10) + (((c&0
b09c0 78 30 33 43 30 29 2b 30 78 30 30 34 30 29 3c 3c  x03C0)+0x0040)<<
b09d0 31 30 29 3b 20 20 20 5c 0a 20 20 20 20 69 66 28  10);   \.    if(
b09e0 20 28 63 20 26 20 30 78 46 46 46 46 30 30 30 30   (c & 0xFFFF0000
b09f0 29 3d 3d 30 20 29 20 63 20 3d 20 30 78 46 46 46  )==0 ) c = 0xFFF
b0a00 44 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  D;              
b0a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b0a20 0a 20 20 7d 20 20 20 20 20 20 20 20 20 20 20 20  .  }            
b0a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0a40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0a50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0a60 20 20 20 20 20 20 20 5c 0a 7d 0a 0a 2f 2a 0a 2a         \.}../*.*
b0a70 2a 20 54 72 61 6e 73 6c 61 74 65 20 61 20 73 69  * Translate a si
b0a80 6e 67 6c 65 20 55 54 46 2d 38 20 63 68 61 72 61  ngle UTF-8 chara
b0a90 63 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68  cter.  Return th
b0aa0 65 20 75 6e 69 63 6f 64 65 20 76 61 6c 75 65 2e  e unicode value.
b0ab0 0a 2a 2a 0a 2a 2a 20 44 75 72 69 6e 67 20 74 72  .**.** During tr
b0ac0 61 6e 73 6c 61 74 69 6f 6e 2c 20 61 73 73 75 6d  anslation, assum
b0ad0 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20  e that the byte 
b0ae0 74 68 61 74 20 7a 54 65 72 6d 20 70 6f 69 6e 74  that zTerm point
b0af0 73 0a 2a 2a 20 69 73 20 61 20 30 78 30 30 2e 0a  s.** is a 0x00..
b0b00 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20 70 6f  **.** Write a po
b0b10 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 78  inter to the nex
b0b20 74 20 75 6e 72 65 61 64 20 62 79 74 65 20 62 61  t unread byte ba
b0b30 63 6b 20 69 6e 74 6f 20 2a 70 7a 4e 65 78 74 2e  ck into *pzNext.
b0b40 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 73 20 4f 6e 20  .**.** Notes On 
b0b50 49 6e 76 61 6c 69 64 20 55 54 46 2d 38 3a 0a 2a  Invalid UTF-8:.*
b0b60 2a 0a 2a 2a 20 20 2a 20 20 54 68 69 73 20 72 6f  *.**  *  This ro
b0b70 75 74 69 6e 65 20 6e 65 76 65 72 20 61 6c 6c 6f  utine never allo
b0b80 77 73 20 61 20 37 2d 62 69 74 20 63 68 61 72 61  ws a 7-bit chara
b0b90 63 74 65 72 20 28 30 78 30 30 20 74 68 72 6f 75  cter (0x00 throu
b0ba0 67 68 20 30 78 37 66 29 20 74 6f 0a 2a 2a 20 20  gh 0x7f) to.**  
b0bb0 20 20 20 62 65 20 65 6e 63 6f 64 65 64 20 61 73     be encoded as
b0bc0 20 61 20 6d 75 6c 74 69 2d 62 79 74 65 20 63 68   a multi-byte ch
b0bd0 61 72 61 63 74 65 72 2e 20 20 41 6e 79 20 6d 75  aracter.  Any mu
b0be0 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74  lti-byte charact
b0bf0 65 72 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 61  er that.**     a
b0c00 74 74 65 6d 70 74 73 20 74 6f 20 65 6e 63 6f 64  ttempts to encod
b0c10 65 20 61 20 76 61 6c 75 65 20 62 65 74 77 65 65  e a value betwee
b0c20 6e 20 30 78 30 30 20 61 6e 64 20 30 78 37 66 20  n 0x00 and 0x7f 
b0c30 69 73 20 72 65 6e 64 65 72 65 64 20 61 73 20 30  is rendered as 0
b0c40 78 66 66 66 64 2e 0a 2a 2a 0a 2a 2a 20 20 2a 20  xfffd..**.**  * 
b0c50 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6e 65   This routine ne
b0c60 76 65 72 20 61 6c 6c 6f 77 73 20 61 20 55 54 46  ver allows a UTF
b0c70 31 36 20 73 75 72 72 6f 67 61 74 65 20 76 61 6c  16 surrogate val
b0c80 75 65 20 74 6f 20 62 65 20 65 6e 63 6f 64 65 64  ue to be encoded
b0c90 2e 0a 2a 2a 20 20 20 20 20 49 66 20 61 20 6d 75  ..**     If a mu
b0ca0 6c 74 69 2d 62 79 74 65 20 63 68 61 72 61 63 74  lti-byte charact
b0cb0 65 72 20 61 74 74 65 6d 70 74 73 20 74 6f 20 65  er attempts to e
b0cc0 6e 63 6f 64 65 20 61 20 76 61 6c 75 65 20 62 65  ncode a value be
b0cd0 74 77 65 65 6e 0a 2a 2a 20 20 20 20 20 30 78 64  tween.**     0xd
b0ce0 38 30 30 20 61 6e 64 20 30 78 65 30 30 30 20 74  800 and 0xe000 t
b0cf0 68 65 6e 20 69 74 20 69 73 20 72 65 6e 64 65 72  hen it is render
b0d00 65 64 20 61 73 20 30 78 66 66 66 64 2e 0a 2a 2a  ed as 0xfffd..**
b0d10 0a 2a 2a 20 20 2a 20 20 42 79 74 65 73 20 69 6e  .**  *  Bytes in
b0d20 20 74 68 65 20 72 61 6e 67 65 20 6f 66 20 30 78   the range of 0x
b0d30 38 30 20 74 68 72 6f 75 67 68 20 30 78 62 66 20  80 through 0xbf 
b0d40 77 68 69 63 68 20 6f 63 63 75 72 20 61 73 20 74  which occur as t
b0d50 68 65 20 66 69 72 73 74 0a 2a 2a 20 20 20 20 20  he first.**     
b0d60 62 79 74 65 20 6f 66 20 61 20 63 68 61 72 61 63  byte of a charac
b0d70 74 65 72 20 61 72 65 20 69 6e 74 65 72 70 72 65  ter are interpre
b0d80 74 65 64 20 61 73 20 73 69 6e 67 6c 65 2d 62 79  ted as single-by
b0d90 74 65 20 63 68 61 72 61 63 74 65 72 73 0a 2a 2a  te characters.**
b0da0 20 20 20 20 20 61 6e 64 20 72 65 6e 64 65 72 65       and rendere
b0db0 64 20 61 73 20 74 68 65 6d 73 65 6c 76 65 73 20  d as themselves 
b0dc0 65 76 65 6e 20 74 68 6f 75 67 68 20 74 68 65 79  even though they
b0dd0 20 61 72 65 20 74 65 63 68 6e 69 63 61 6c 6c 79   are technically
b0de0 0a 2a 2a 20 20 20 20 20 69 6e 76 61 6c 69 64 20  .**     invalid 
b0df0 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2a 0a 2a  characters..**.*
b0e00 2a 20 20 2a 20 20 54 68 69 73 20 72 6f 75 74 69  *  *  This routi
b0e10 6e 65 20 61 63 63 65 70 74 73 20 61 6e 20 69 6e  ne accepts an in
b0e20 66 69 6e 69 74 65 20 6e 75 6d 62 65 72 20 6f 66  finite number of
b0e30 20 64 69 66 66 65 72 65 6e 74 20 55 54 46 38 20   different UTF8 
b0e40 65 6e 63 6f 64 69 6e 67 73 0a 2a 2a 20 20 20 20  encodings.**    
b0e50 20 66 6f 72 20 75 6e 69 63 6f 64 65 20 76 61 6c   for unicode val
b0e60 75 65 73 20 30 78 38 30 20 61 6e 64 20 67 72 65  ues 0x80 and gre
b0e70 61 74 65 72 2e 20 20 49 74 20 64 6f 20 6e 6f 74  ater.  It do not
b0e80 20 63 68 61 6e 67 65 20 6f 76 65 72 2d 6c 65 6e   change over-len
b0e90 67 74 68 0a 2a 2a 20 20 20 20 20 65 6e 63 6f 64  gth.**     encod
b0ea0 69 6e 67 73 20 74 6f 20 30 78 66 66 66 64 20 61  ings to 0xfffd a
b0eb0 73 20 73 6f 6d 65 20 73 79 73 74 65 6d 73 20 72  s some systems r
b0ec0 65 63 6f 6d 6d 65 6e 64 2e 0a 2a 2f 0a 23 64 65  ecommend..*/.#de
b0ed0 66 69 6e 65 20 52 45 41 44 5f 55 54 46 38 28 7a  fine READ_UTF8(z
b0ee0 49 6e 2c 20 7a 54 65 72 6d 2c 20 63 29 20 20 20  In, zTerm, c)   
b0ef0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0f00 20 20 20 20 20 20 20 20 5c 0a 20 20 63 20 3d 20          \.  c = 
b0f10 2a 28 7a 49 6e 2b 2b 29 3b 20 20 20 20 20 20 20  *(zIn++);       
b0f20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0f30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0f40 20 20 20 20 20 5c 0a 20 20 69 66 28 20 63 3e 3d       \.  if( c>=
b0f50 30 78 63 30 20 29 7b 20 20 20 20 20 20 20 20 20  0xc0 ){         
b0f60 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b0f80 20 20 5c 0a 20 20 20 20 63 20 3d 20 73 71 6c 69    \.    c = sqli
b0f90 74 65 33 55 74 66 54 72 61 6e 73 31 5b 63 2d 30  te3UtfTrans1[c-0
b0fa0 78 63 30 5d 3b 20 20 20 20 20 20 20 20 20 20 20  xc0];           
b0fb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
b0fc0 0a 20 20 20 20 77 68 69 6c 65 28 20 7a 49 6e 21  .    while( zIn!
b0fd0 3d 7a 54 65 72 6d 20 26 26 20 28 2a 7a 49 6e 20  =zTerm && (*zIn 
b0fe0 26 20 30 78 63 30 29 3d 3d 30 78 38 30 20 29 7b  & 0xc0)==0x80 ){
b0ff0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
b1000 20 20 20 20 63 20 3d 20 28 63 3c 3c 36 29 20 2b      c = (c<<6) +
b1010 20 28 30 78 33 66 20 26 20 2a 28 7a 49 6e 2b 2b   (0x3f & *(zIn++
b1020 29 29 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ));             
b1030 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 7d           \.    }
b1040 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1050 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1060 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1070 20 20 20 20 20 20 5c 0a 20 20 20 20 69 66 28 20        \.    if( 
b1080 63 3c 30 78 38 30 20 20 20 20 20 20 20 20 20 20  c<0x80          
b1090 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b10a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b10b0 20 20 20 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20     \.        || 
b10c0 28 63 26 30 78 46 46 46 46 46 38 30 30 29 3d 3d  (c&0xFFFFF800)==
b10d0 30 78 44 38 30 30 20 20 20 20 20 20 20 20 20 20  0xD800          
b10e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b10f0 5c 0a 20 20 20 20 20 20 20 20 7c 7c 20 28 63 26  \.        || (c&
b1100 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30 78 46  0xFFFFFFFE)==0xF
b1110 46 46 45 20 29 7b 20 20 63 20 3d 20 30 78 46 46  FFE ){  c = 0xFF
b1120 46 44 3b 20 7d 20 20 20 20 20 20 20 20 5c 0a 20  FD; }        \. 
b1130 20 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54   }.SQLITE_PRIVAT
b1140 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
b1150 38 52 65 61 64 28 0a 20 20 63 6f 6e 73 74 20 75  8Read(.  const u
b1160 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c  nsigned char *z,
b1170 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
b1180 74 20 62 79 74 65 20 6f 66 20 55 54 46 2d 38 20  t byte of UTF-8 
b1190 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20 20 63  character */.  c
b11a0 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
b11b0 61 72 20 2a 7a 54 65 72 6d 2c 20 20 20 20 20 2f  ar *zTerm,     /
b11c0 2a 20 50 72 65 74 65 6e 64 20 74 68 69 73 20 62  * Pretend this b
b11d0 79 74 65 20 69 73 20 30 78 30 30 20 2a 2f 0a 20  yte is 0x00 */. 
b11e0 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
b11f0 63 68 61 72 20 2a 2a 70 7a 4e 65 78 74 20 20 20  char **pzNext   
b1200 20 2f 2a 20 57 72 69 74 65 20 66 69 72 73 74 20   /* Write first 
b1210 62 79 74 65 20 70 61 73 74 20 55 54 46 2d 38 20  byte past UTF-8 
b1220 63 68 61 72 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  char here */.){.
b1230 20 20 69 6e 74 20 63 3b 0a 20 20 52 45 41 44 5f    int c;.  READ_
b1240 55 54 46 38 28 7a 2c 20 7a 54 65 72 6d 2c 20 63  UTF8(z, zTerm, c
b1250 29 3b 0a 20 20 2a 70 7a 4e 65 78 74 20 3d 20 7a  );.  *pzNext = z
b1260 3b 0a 20 20 72 65 74 75 72 6e 20 63 3b 0a 7d 0a  ;.  return c;.}.
b1270 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65  ..../*.** If the
b1280 20 54 52 41 4e 53 4c 41 54 45 5f 54 52 41 43 45   TRANSLATE_TRACE
b1290 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69 6e 65   macro is define
b12a0 64 2c 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  d, the value of 
b12b0 65 61 63 68 20 4d 65 6d 20 69 73 0a 2a 2a 20 70  each Mem is.** p
b12c0 72 69 6e 74 65 64 20 6f 6e 20 73 74 64 65 72 72  rinted on stderr
b12d0 20 6f 6e 20 74 68 65 20 77 61 79 20 69 6e 74 6f   on the way into
b12e0 20 61 6e 64 20 6f 75 74 20 6f 66 20 73 71 6c 69   and out of sqli
b12f0 74 65 33 56 64 62 65 4d 65 6d 54 72 61 6e 73 6c  te3VdbeMemTransl
b1300 61 74 65 28 29 2e 0a 2a 2f 20 0a 2f 2a 20 23 64  ate()..*/ ./* #d
b1310 65 66 69 6e 65 20 54 52 41 4e 53 4c 41 54 45 5f  efine TRANSLATE_
b1320 54 52 41 43 45 20 31 20 2a 2f 0a 0a 23 69 66 6e  TRACE 1 */..#ifn
b1330 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
b1340 55 54 46 31 36 0a 2f 2a 0a 2a 2a 20 54 68 69 73  UTF16./*.** This
b1350 20 72 6f 75 74 69 6e 65 20 74 72 61 6e 73 66 6f   routine transfo
b1360 72 6d 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  rms the internal
b1370 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 75   text encoding u
b1380 73 65 64 20 62 79 20 70 4d 65 6d 20 74 6f 0a 2a  sed by pMem to.*
b1390 2a 20 64 65 73 69 72 65 64 45 6e 63 2e 20 49 74  * desiredEnc. It
b13a0 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20   is an error if 
b13b0 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 61 6c  the string is al
b13c0 72 65 61 64 79 20 6f 66 20 74 68 65 20 64 65 73  ready of the des
b13d0 69 72 65 64 0a 2a 2a 20 65 6e 63 6f 64 69 6e 67  ired.** encoding
b13e0 2c 20 6f 72 20 69 66 20 2a 70 4d 65 6d 20 64 6f  , or if *pMem do
b13f0 65 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61  es not contain a
b1400 20 73 74 72 69 6e 67 20 76 61 6c 75 65 2e 0a 2a   string value..*
b1410 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b1420 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
b1430 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 4d 65 6d  MemTranslate(Mem
b1440 20 2a 70 4d 65 6d 2c 20 75 38 20 64 65 73 69 72   *pMem, u8 desir
b1450 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 6c 65  edEnc){.  int le
b1460 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n;              
b1470 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
b1480 20 6c 65 6e 67 74 68 20 6f 66 20 6f 75 74 70 75   length of outpu
b1490 74 20 73 74 72 69 6e 67 20 69 6e 20 62 79 74 65  t string in byte
b14a0 73 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  s */.  unsigned 
b14b0 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20  char *zOut;     
b14c0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
b14d0 4f 75 74 70 75 74 20 62 75 66 66 65 72 20 2a 2f  Output buffer */
b14e0 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
b14f0 20 2a 7a 49 6e 3b 20 20 20 20 20 20 20 20 20 20   *zIn;          
b1500 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 70 75           /* Inpu
b1510 74 20 69 74 65 72 61 74 6f 72 20 2a 2f 0a 20 20  t iterator */.  
b1520 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
b1530 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20  Term;           
b1540 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20        /* End of 
b1550 69 6e 70 75 74 20 2a 2f 0a 20 20 75 6e 73 69 67  input */.  unsig
b1560 6e 65 64 20 63 68 61 72 20 2a 7a 3b 20 20 20 20  ned char *z;    
b1570 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
b1580 20 2f 2a 20 4f 75 74 70 75 74 20 69 74 65 72 61   /* Output itera
b1590 74 6f 72 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65  tor */.  unsigne
b15a0 64 20 69 6e 74 20 63 3b 0a 0a 20 20 61 73 73 65  d int c;..  asse
b15b0 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20  rt( pMem->db==0 
b15c0 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  || sqlite3_mutex
b15d0 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e  _held(pMem->db->
b15e0 6d 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65  mutex) );.  asse
b15f0 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  rt( pMem->flags&
b1600 4d 45 4d 5f 53 74 72 20 29 3b 0a 20 20 61 73 73  MEM_Str );.  ass
b1610 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e 63 21 3d  ert( pMem->enc!=
b1620 64 65 73 69 72 65 64 45 6e 63 20 29 3b 0a 20 20  desiredEnc );.  
b1630 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 65 6e  assert( pMem->en
b1640 63 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  c!=0 );.  assert
b1650 28 20 70 4d 65 6d 2d 3e 6e 3e 3d 30 20 29 3b 0a  ( pMem->n>=0 );.
b1660 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 52 41  .#if defined(TRA
b1670 4e 53 4c 41 54 45 5f 54 52 41 43 45 29 20 26 26  NSLATE_TRACE) &&
b1680 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
b1690 44 45 42 55 47 29 0a 20 20 7b 0a 20 20 20 20 63  DEBUG).  {.    c
b16a0 68 61 72 20 7a 42 75 66 5b 31 30 30 5d 3b 0a 20  har zBuf[100];. 
b16b0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
b16c0 6d 50 72 65 74 74 79 50 72 69 6e 74 28 70 4d 65  mPrettyPrint(pMe
b16d0 6d 2c 20 7a 42 75 66 29 3b 0a 20 20 20 20 66 70  m, zBuf);.    fp
b16e0 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22 49  rintf(stderr, "I
b16f0 4e 50 55 54 3a 20 20 25 73 5c 6e 22 2c 20 7a 42  NPUT:  %s\n", zB
b1700 75 66 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  uf);.  }.#endif.
b1710 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74 72 61  .  /* If the tra
b1720 6e 73 6c 61 74 69 6f 6e 20 69 73 20 62 65 74 77  nslation is betw
b1730 65 65 6e 20 55 54 46 2d 31 36 20 6c 69 74 74 6c  een UTF-16 littl
b1740 65 20 61 6e 64 20 62 69 67 20 65 6e 64 69 61 6e  e and big endian
b1750 2c 20 74 68 65 6e 20 0a 20 20 2a 2a 20 61 6c 6c  , then .  ** all
b1760 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65   that is require
b1770 64 20 69 73 20 74 6f 20 73 77 61 70 20 74 68 65  d is to swap the
b1780 20 62 79 74 65 20 6f 72 64 65 72 2e 20 54 68 69   byte order. Thi
b1790 73 20 63 61 73 65 20 69 73 20 68 61 6e 64 6c 65  s case is handle
b17a0 64 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e 74  d.  ** different
b17b0 6c 79 20 66 72 6f 6d 20 74 68 65 20 6f 74 68 65  ly from the othe
b17c0 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  rs..  */.  if( p
b17d0 4d 65 6d 2d 3e 65 6e 63 21 3d 53 51 4c 49 54 45  Mem->enc!=SQLITE
b17e0 5f 55 54 46 38 20 26 26 20 64 65 73 69 72 65 64  _UTF8 && desired
b17f0 45 6e 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38  Enc!=SQLITE_UTF8
b1800 20 29 7b 0a 20 20 20 20 75 38 20 74 65 6d 70 3b   ){.    u8 temp;
b1810 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20  .    int rc;.   
b1820 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
b1830 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c  eMemMakeWriteabl
b1840 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28  e(pMem);.    if(
b1850 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
b1860 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
b1870 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
b1880 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
b1890 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
b18a0 20 20 20 7d 0a 20 20 20 20 7a 49 6e 20 3d 20 28     }.    zIn = (
b18b0 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20  u8*)pMem->z;.   
b18c0 20 7a 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d   zTerm = &zIn[pM
b18d0 65 6d 2d 3e 6e 26 7e 31 5d 3b 0a 20 20 20 20 77  em->n&~1];.    w
b18e0 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20  hile( zIn<zTerm 
b18f0 29 7b 0a 20 20 20 20 20 20 74 65 6d 70 20 3d 20  ){.      temp = 
b1900 2a 7a 49 6e 3b 0a 20 20 20 20 20 20 2a 7a 49 6e  *zIn;.      *zIn
b1910 20 3d 20 2a 28 7a 49 6e 2b 31 29 3b 0a 20 20 20   = *(zIn+1);.   
b1920 20 20 20 7a 49 6e 2b 2b 3b 0a 20 20 20 20 20 20     zIn++;.      
b1930 2a 7a 49 6e 2b 2b 20 3d 20 74 65 6d 70 3b 0a 20  *zIn++ = temp;. 
b1940 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 65     }.    pMem->e
b1950 6e 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63 3b  nc = desiredEnc;
b1960 0a 20 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 6c  .    goto transl
b1970 61 74 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ate_out;.  }..  
b1980 2f 2a 20 53 65 74 20 6c 65 6e 20 74 6f 20 74 68  /* Set len to th
b1990 65 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72  e maximum number
b19a0 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69 72   of bytes requir
b19b0 65 64 20 69 6e 20 74 68 65 20 6f 75 74 70 75 74  ed in the output
b19c0 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 66   buffer. */.  if
b19d0 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51  ( desiredEnc==SQ
b19e0 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20  LITE_UTF8 ){.   
b19f0 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76 65 72 74   /* When convert
b1a00 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d 31 36 2c  ing from UTF-16,
b1a10 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 67 72 6f   the maximum gro
b1a20 77 74 68 20 72 65 73 75 6c 74 73 20 66 72 6f 6d  wth results from
b1a30 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c 61 74  .    ** translat
b1a40 69 6e 67 20 61 20 32 2d 62 79 74 65 20 63 68 61  ing a 2-byte cha
b1a50 72 61 63 74 65 72 20 74 6f 20 61 20 34 2d 62 79  racter to a 4-by
b1a60 74 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74  te UTF-8 charact
b1a70 65 72 2e 0a 20 20 20 20 2a 2a 20 41 20 73 69 6e  er..    ** A sin
b1a80 67 6c 65 20 62 79 74 65 20 69 73 20 72 65 71 75  gle byte is requ
b1a90 69 72 65 64 20 66 6f 72 20 74 68 65 20 6f 75 74  ired for the out
b1aa0 70 75 74 20 73 74 72 69 6e 67 0a 20 20 20 20 2a  put string.    *
b1ab0 2a 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  * nul-terminator
b1ac0 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 4d 65  ..    */.    pMe
b1ad0 6d 2d 3e 6e 20 26 3d 20 7e 31 3b 0a 20 20 20 20  m->n &= ~1;.    
b1ae0 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 20 2a 20  len = pMem->n * 
b1af0 32 20 2b 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  2 + 1;.  }else{.
b1b00 20 20 20 20 2f 2a 20 57 68 65 6e 20 63 6f 6e 76      /* When conv
b1b10 65 72 74 69 6e 67 20 66 72 6f 6d 20 55 54 46 2d  erting from UTF-
b1b20 38 20 74 6f 20 55 54 46 2d 31 36 20 74 68 65 20  8 to UTF-16 the 
b1b30 6d 61 78 69 6d 75 6d 20 67 72 6f 77 74 68 20 69  maximum growth i
b1b40 73 20 63 61 75 73 65 64 0a 20 20 20 20 2a 2a 20  s caused.    ** 
b1b50 77 68 65 6e 20 61 20 31 2d 62 79 74 65 20 55 54  when a 1-byte UT
b1b60 46 2d 38 20 63 68 61 72 61 63 74 65 72 20 69 73  F-8 character is
b1b70 20 74 72 61 6e 73 6c 61 74 65 64 20 69 6e 74 6f   translated into
b1b80 20 61 20 32 2d 62 79 74 65 20 55 54 46 2d 31 36   a 2-byte UTF-16
b1b90 0a 20 20 20 20 2a 2a 20 63 68 61 72 61 63 74 65  .    ** characte
b1ba0 72 2e 20 54 77 6f 20 62 79 74 65 73 20 61 72 65  r. Two bytes are
b1bb0 20 72 65 71 75 69 72 65 64 20 69 6e 20 74 68 65   required in the
b1bc0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 66   output buffer f
b1bd0 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 75  or the.    ** nu
b1be0 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 2e 0a 20 20  l-terminator..  
b1bf0 20 20 2a 2f 0a 20 20 20 20 6c 65 6e 20 3d 20 70    */.    len = p
b1c00 4d 65 6d 2d 3e 6e 20 2a 20 32 20 2b 20 32 3b 0a  Mem->n * 2 + 2;.
b1c10 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20 7a 49    }..  /* Set zI
b1c20 6e 20 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68  n to point at th
b1c30 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20 69  e start of the i
b1c40 6e 70 75 74 20 62 75 66 66 65 72 20 61 6e 64 20  nput buffer and 
b1c50 7a 54 65 72 6d 20 74 6f 20 70 6f 69 6e 74 20 31  zTerm to point 1
b1c60 0a 20 20 2a 2a 20 62 79 74 65 20 70 61 73 74 20  .  ** byte past 
b1c70 74 68 65 20 65 6e 64 2e 0a 20 20 2a 2a 0a 20 20  the end..  **.  
b1c80 2a 2a 20 56 61 72 69 61 62 6c 65 20 7a 4f 75 74  ** Variable zOut
b1c90 20 69 73 20 73 65 74 20 74 6f 20 70 6f 69 6e 74   is set to point
b1ca0 20 61 74 20 74 68 65 20 6f 75 74 70 75 74 20 62   at the output b
b1cb0 75 66 66 65 72 2c 20 73 70 61 63 65 20 6f 62 74  uffer, space obt
b1cc0 61 69 6e 65 64 0a 20 20 2a 2a 20 66 72 6f 6d 20  ained.  ** from 
b1cd0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
b1ce0 2e 0a 20 20 2a 2f 0a 20 20 7a 49 6e 20 3d 20 28  ..  */.  zIn = (
b1cf0 75 38 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 7a  u8*)pMem->z;.  z
b1d00 54 65 72 6d 20 3d 20 26 7a 49 6e 5b 70 4d 65 6d  Term = &zIn[pMem
b1d10 2d 3e 6e 5d 3b 0a 20 20 7a 4f 75 74 20 3d 20 73  ->n];.  zOut = s
b1d20 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
b1d30 77 28 70 4d 65 6d 2d 3e 64 62 2c 20 6c 65 6e 29  w(pMem->db, len)
b1d40 3b 0a 20 20 69 66 28 20 21 7a 4f 75 74 20 29 7b  ;.  if( !zOut ){
b1d50 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
b1d60 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
b1d70 7a 20 3d 20 7a 4f 75 74 3b 0a 0a 20 20 69 66 28  z = zOut;..  if(
b1d80 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49   pMem->enc==SQLI
b1d90 54 45 5f 55 54 46 38 20 29 7b 0a 20 20 20 20 69  TE_UTF8 ){.    i
b1da0 66 28 20 64 65 73 69 72 65 64 45 6e 63 3d 3d 53  f( desiredEnc==S
b1db0 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 29 7b  QLITE_UTF16LE ){
b1dc0 0a 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20  .      /* UTF-8 
b1dd0 2d 3e 20 55 54 46 2d 31 36 20 4c 69 74 74 6c 65  -> UTF-16 Little
b1de0 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20  -endian */.     
b1df0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
b1e00 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  m ){.        /* 
b1e10 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
b1e20 65 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  ead(zIn, zTerm, 
b1e30 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e  (const u8**)&zIn
b1e40 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45  ); */.        RE
b1e50 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65  AD_UTF8(zIn, zTe
b1e60 72 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20  rm, c);.        
b1e70 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28 7a 2c  WRITE_UTF16LE(z,
b1e80 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   c);.      }.   
b1e90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
b1ea0 73 65 72 74 28 20 64 65 73 69 72 65 64 45 6e 63  sert( desiredEnc
b1eb0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
b1ec0 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 55 54 46   );.      /* UTF
b1ed0 2d 38 20 2d 3e 20 55 54 46 2d 31 36 20 42 69 67  -8 -> UTF-16 Big
b1ee0 2d 65 6e 64 69 61 6e 20 2a 2f 0a 20 20 20 20 20  -endian */.     
b1ef0 20 77 68 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72   while( zIn<zTer
b1f00 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  m ){.        /* 
b1f10 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
b1f20 65 61 64 28 7a 49 6e 2c 20 7a 54 65 72 6d 2c 20  ead(zIn, zTerm, 
b1f30 28 63 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 49 6e  (const u8**)&zIn
b1f40 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20 20 52 45  ); */.        RE
b1f50 41 44 5f 55 54 46 38 28 7a 49 6e 2c 20 7a 54 65  AD_UTF8(zIn, zTe
b1f60 72 6d 2c 20 63 29 3b 0a 20 20 20 20 20 20 20 20  rm, c);.        
b1f70 57 52 49 54 45 5f 55 54 46 31 36 42 45 28 7a 2c  WRITE_UTF16BE(z,
b1f80 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   c);.      }.   
b1f90 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d   }.    pMem->n =
b1fa0 20 7a 20 2d 20 7a 4f 75 74 3b 0a 20 20 20 20 2a   z - zOut;.    *
b1fb0 7a 2b 2b 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  z++ = 0;.  }else
b1fc0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 65  {.    assert( de
b1fd0 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49 54 45  siredEnc==SQLITE
b1fe0 5f 55 54 46 38 20 29 3b 0a 20 20 20 20 69 66 28  _UTF8 );.    if(
b1ff0 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49   pMem->enc==SQLI
b2000 54 45 5f 55 54 46 31 36 4c 45 20 29 7b 0a 20 20  TE_UTF16LE ){.  
b2010 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 4c 69      /* UTF-16 Li
b2020 74 74 6c 65 2d 65 6e 64 69 61 6e 20 2d 3e 20 55  ttle-endian -> U
b2030 54 46 2d 38 20 2a 2f 0a 20 20 20 20 20 20 77 68  TF-8 */.      wh
b2040 69 6c 65 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29  ile( zIn<zTerm )
b2050 7b 0a 20 20 20 20 20 20 20 20 52 45 41 44 5f 55  {.        READ_U
b2060 54 46 31 36 4c 45 28 7a 49 6e 2c 20 63 29 3b 20  TF16LE(zIn, c); 
b2070 0a 20 20 20 20 20 20 20 20 57 52 49 54 45 5f 55  .        WRITE_U
b2080 54 46 38 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20  TF8(z, c);.     
b2090 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
b20a0 20 20 20 20 2f 2a 20 55 54 46 2d 31 36 20 42 69      /* UTF-16 Bi
b20b0 67 2d 65 6e 64 69 61 6e 20 2d 3e 20 55 54 46 2d  g-endian -> UTF-
b20c0 38 20 2a 2f 0a 20 20 20 20 20 20 77 68 69 6c 65  8 */.      while
b20d0 28 20 7a 49 6e 3c 7a 54 65 72 6d 20 29 7b 0a 20  ( zIn<zTerm ){. 
b20e0 20 20 20 20 20 20 20 52 45 41 44 5f 55 54 46 31         READ_UTF1
b20f0 36 42 45 28 7a 49 6e 2c 20 63 29 3b 20 0a 20 20  6BE(zIn, c); .  
b2100 20 20 20 20 20 20 57 52 49 54 45 5f 55 54 46 38        WRITE_UTF8
b2110 28 7a 2c 20 63 29 3b 0a 20 20 20 20 20 20 7d 0a  (z, c);.      }.
b2120 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e      }.    pMem->
b2130 6e 20 3d 20 7a 20 2d 20 7a 4f 75 74 3b 0a 20 20  n = z - zOut;.  
b2140 7d 0a 20 20 2a 7a 20 3d 20 30 3b 0a 20 20 61 73  }.  *z = 0;.  as
b2150 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 6e 2b 28  sert( (pMem->n+(
b2160 64 65 73 69 72 65 64 45 6e 63 3d 3d 53 51 4c 49  desiredEnc==SQLI
b2170 54 45 5f 55 54 46 38 3f 31 3a 32 29 29 3c 3d 6c  TE_UTF8?1:2))<=l
b2180 65 6e 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33  en );..  sqlite3
b2190 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70  VdbeMemRelease(p
b21a0 4d 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c  Mem);.  pMem->fl
b21b0 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f 53 74 61  ags &= ~(MEM_Sta
b21c0 74 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f  tic|MEM_Dyn|MEM_
b21d0 45 70 68 65 6d 29 3b 0a 20 20 70 4d 65 6d 2d 3e  Ephem);.  pMem->
b21e0 65 6e 63 20 3d 20 64 65 73 69 72 65 64 45 6e 63  enc = desiredEnc
b21f0 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  ;.  pMem->flags 
b2200 7c 3d 20 28 4d 45 4d 5f 54 65 72 6d 7c 4d 45 4d  |= (MEM_Term|MEM
b2210 5f 44 79 6e 29 3b 0a 20 20 70 4d 65 6d 2d 3e 7a  _Dyn);.  pMem->z
b2220 20 3d 20 28 63 68 61 72 2a 29 7a 4f 75 74 3b 0a   = (char*)zOut;.
b2230 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20    pMem->zMalloc 
b2240 3d 20 70 4d 65 6d 2d 3e 7a 3b 0a 0a 74 72 61 6e  = pMem->z;..tran
b2250 73 6c 61 74 65 5f 6f 75 74 3a 0a 23 69 66 20 64  slate_out:.#if d
b2260 65 66 69 6e 65 64 28 54 52 41 4e 53 4c 41 54 45  efined(TRANSLATE
b2270 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e  _TRACE) && defin
b2280 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
b2290 0a 20 20 7b 0a 20 20 20 20 63 68 61 72 20 7a 42  .  {.    char zB
b22a0 75 66 5b 31 30 30 5d 3b 0a 20 20 20 20 73 71 6c  uf[100];.    sql
b22b0 69 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74  ite3VdbeMemPrett
b22c0 79 50 72 69 6e 74 28 70 4d 65 6d 2c 20 7a 42 75  yPrint(pMem, zBu
b22d0 66 29 3b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  f);.    fprintf(
b22e0 73 74 64 65 72 72 2c 20 22 4f 55 54 50 55 54 3a  stderr, "OUTPUT:
b22f0 20 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20   %s\n", zBuf);. 
b2300 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72 65 74 75   }.#endif.  retu
b2310 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
b2320 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
b2330 69 6e 65 20 63 68 65 63 6b 73 20 66 6f 72 20 61  ine checks for a
b2340 20 62 79 74 65 2d 6f 72 64 65 72 20 6d 61 72 6b   byte-order mark
b2350 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
b2360 67 20 6f 66 20 74 68 65 20 0a 2a 2a 20 55 54 46  g of the .** UTF
b2370 2d 31 36 20 73 74 72 69 6e 67 20 73 74 6f 72 65  -16 string store
b2380 64 20 69 6e 20 2a 70 4d 65 6d 2e 20 49 66 20 6f  d in *pMem. If o
b2390 6e 65 20 69 73 20 70 72 65 73 65 6e 74 2c 20 69  ne is present, i
b23a0 74 20 69 73 20 72 65 6d 6f 76 65 64 20 61 6e 64  t is removed and
b23b0 0a 2a 2a 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  .** the encoding
b23c0 20 6f 66 20 74 68 65 20 4d 65 6d 20 61 64 6a 75   of the Mem adju
b23d0 73 74 65 64 2e 20 54 68 69 73 20 72 6f 75 74 69  sted. This routi
b23e0 6e 65 20 64 6f 65 73 20 6e 6f 74 20 64 6f 20 61  ne does not do a
b23f0 6e 79 0a 2a 2a 20 62 79 74 65 2d 73 77 61 70 70  ny.** byte-swapp
b2400 69 6e 67 2c 20 69 74 20 6a 75 73 74 20 73 65 74  ing, it just set
b2410 73 20 4d 65 6d 2e 65 6e 63 20 61 70 70 72 6f 70  s Mem.enc approp
b2420 72 69 61 74 65 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54  riately..**.** T
b2430 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 28 73  he allocation (s
b2440 74 61 74 69 63 2c 20 64 79 6e 61 6d 69 63 20 65  tatic, dynamic e
b2450 74 63 2e 29 20 61 6e 64 20 65 6e 63 6f 64 69 6e  tc.) and encodin
b2460 67 20 6f 66 20 74 68 65 20 4d 65 6d 20 6d 61 79  g of the Mem may
b2470 20 62 65 0a 2a 2a 20 63 68 61 6e 67 65 64 20 62   be.** changed b
b2480 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  y this function.
b2490 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
b24a0 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
b24b0 62 65 4d 65 6d 48 61 6e 64 6c 65 42 6f 6d 28 4d  beMemHandleBom(M
b24c0 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74  em *pMem){.  int
b24d0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
b24e0 0a 20 20 75 38 20 62 6f 6d 20 3d 20 30 3b 0a 0a  .  u8 bom = 0;..
b24f0 20 20 69 66 28 20 70 4d 65 6d 2d 3e 6e 3c 30 20    if( pMem->n<0 
b2500 7c 7c 20 70 4d 65 6d 2d 3e 6e 3e 31 20 29 7b 0a  || pMem->n>1 ){.
b2510 20 20 20 20 75 38 20 62 31 20 3d 20 2a 28 75 38      u8 b1 = *(u8
b2520 20 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20 20   *)pMem->z;.    
b2530 75 38 20 62 32 20 3d 20 2a 28 28 28 75 38 20 2a  u8 b2 = *(((u8 *
b2540 29 70 4d 65 6d 2d 3e 7a 29 20 2b 20 31 29 3b 0a  )pMem->z) + 1);.
b2550 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78 46 45      if( b1==0xFE
b2560 20 26 26 20 62 32 3d 3d 30 78 46 46 20 29 7b 0a   && b2==0xFF ){.
b2570 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51 4c 49        bom = SQLI
b2580 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 20 20  TE_UTF16BE;.    
b2590 7d 0a 20 20 20 20 69 66 28 20 62 31 3d 3d 30 78  }.    if( b1==0x
b25a0 46 46 20 26 26 20 62 32 3d 3d 30 78 46 45 20 29  FF && b2==0xFE )
b25b0 7b 0a 20 20 20 20 20 20 62 6f 6d 20 3d 20 53 51  {.      bom = SQ
b25c0 4c 49 54 45 5f 55 54 46 31 36 4c 45 3b 0a 20 20  LITE_UTF16LE;.  
b25d0 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28    }.  }.  .  if(
b25e0 20 62 6f 6d 20 29 7b 0a 20 20 20 20 72 63 20 3d   bom ){.    rc =
b25f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d   sqlite3VdbeMemM
b2600 61 6b 65 57 72 69 74 65 61 62 6c 65 28 70 4d 65  akeWriteable(pMe
b2610 6d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  m);.    if( rc==
b2620 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
b2630 20 20 20 70 4d 65 6d 2d 3e 6e 20 2d 3d 20 32 3b     pMem->n -= 2;
b2640 0a 20 20 20 20 20 20 6d 65 6d 6d 6f 76 65 28 70  .      memmove(p
b2650 4d 65 6d 2d 3e 7a 2c 20 26 70 4d 65 6d 2d 3e 7a  Mem->z, &pMem->z
b2660 5b 32 5d 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20  [2], pMem->n);. 
b2670 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65       pMem->z[pMe
b2680 6d 2d 3e 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20  m->n] = '\0';.  
b2690 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d      pMem->z[pMem
b26a0 2d 3e 6e 2b 31 5d 20 3d 20 27 5c 30 27 3b 0a 20  ->n+1] = '\0';. 
b26b0 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
b26c0 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20   |= MEM_Term;.  
b26d0 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
b26e0 62 6f 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  bom;.    }.  }. 
b26f0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
b2700 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
b2710 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
b2720 0a 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d  .** pZ is a UTF-
b2730 38 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64  8 encoded unicod
b2740 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 42 79  e string. If nBy
b2750 74 65 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  te is less than 
b2760 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  zero,.** return 
b2770 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 75 6e  the number of un
b2780 69 63 6f 64 65 20 63 68 61 72 61 63 74 65 72 73  icode characters
b2790 20 69 6e 20 70 5a 20 75 70 20 74 6f 20 28 62 75   in pZ up to (bu
b27a0 74 20 6e 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29  t not including)
b27b0 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 30 78  .** the first 0x
b27c0 30 30 20 62 79 74 65 2e 20 49 66 20 6e 42 79 74  00 byte. If nByt
b27d0 65 20 69 73 20 6e 6f 74 20 6c 65 73 73 20 74 68  e is not less th
b27e0 61 6e 20 7a 65 72 6f 2c 20 72 65 74 75 72 6e 20  an zero, return 
b27f0 74 68 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66  the.** number of
b2800 20 75 6e 69 63 6f 64 65 20 63 68 61 72 61 63 74   unicode charact
b2810 65 72 73 20 69 6e 20 74 68 65 20 66 69 72 73 74  ers in the first
b2820 20 6e 42 79 74 65 20 6f 66 20 70 5a 20 28 6f 72   nByte of pZ (or
b2830 20 75 70 20 74 6f 20 0a 2a 2a 20 74 68 65 20 66   up to .** the f
b2840 69 72 73 74 20 30 78 30 30 2c 20 77 68 69 63 68  irst 0x00, which
b2850 65 76 65 72 20 63 6f 6d 65 73 20 66 69 72 73 74  ever comes first
b2860 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
b2870 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b2880 55 74 66 38 43 68 61 72 4c 65 6e 28 63 6f 6e 73  Utf8CharLen(cons
b2890 74 20 63 68 61 72 20 2a 7a 49 6e 2c 20 69 6e 74  t char *zIn, int
b28a0 20 6e 42 79 74 65 29 7b 0a 20 20 69 6e 74 20 72   nByte){.  int r
b28b0 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 38   = 0;.  const u8
b28c0 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 75 38 2a   *z = (const u8*
b28d0 29 7a 49 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 38  )zIn;.  const u8
b28e0 20 2a 7a 54 65 72 6d 3b 0a 20 20 69 66 28 20 6e   *zTerm;.  if( n
b28f0 42 79 74 65 3e 3d 30 20 29 7b 0a 20 20 20 20 7a  Byte>=0 ){.    z
b2900 54 65 72 6d 20 3d 20 26 7a 5b 6e 42 79 74 65 5d  Term = &z[nByte]
b2910 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a  ;.  }else{.    z
b2920 54 65 72 6d 20 3d 20 28 63 6f 6e 73 74 20 75 38  Term = (const u8
b2930 2a 29 28 2d 31 29 3b 0a 20 20 7d 0a 20 20 61 73  *)(-1);.  }.  as
b2940 73 65 72 74 28 20 7a 3c 3d 7a 54 65 72 6d 20 29  sert( z<=zTerm )
b2950 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 21 3d 30  ;.  while( *z!=0
b2960 20 26 26 20 7a 3c 7a 54 65 72 6d 20 29 7b 0a 20   && z<zTerm ){. 
b2970 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
b2980 54 46 38 28 7a 29 3b 0a 20 20 20 20 72 2b 2b 3b  TF8(z);.    r++;
b2990 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b  .  }.  return r;
b29a0 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 74 65 73 74  .}../* This test
b29b0 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 74   function is not
b29c0 20 63 75 72 72 65 6e 74 6c 79 20 75 73 65 64 20   currently used 
b29d0 62 79 20 74 68 65 20 61 75 74 6f 6d 61 74 65 64  by the automated
b29e0 20 74 65 73 74 2d 73 75 69 74 65 2e 20 0a 2a 2a   test-suite. .**
b29f0 20 48 65 6e 63 65 20 69 74 20 69 73 20 6f 6e 6c   Hence it is onl
b2a00 79 20 61 76 61 69 6c 61 62 6c 65 20 69 6e 20 64  y available in d
b2a10 65 62 75 67 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  ebug builds..*/.
b2a20 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c 49  #if defined(SQLI
b2a30 54 45 5f 54 45 53 54 29 20 26 26 20 64 65 66 69  TE_TEST) && defi
b2a40 6e 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47  ned(SQLITE_DEBUG
b2a50 29 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74  )./*.** Translat
b2a60 65 20 55 54 46 2d 38 20 74 6f 20 55 54 46 2d 38  e UTF-8 to UTF-8
b2a70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 61 73  ..**.** This has
b2a80 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 6d   the effect of m
b2a90 61 6b 69 6e 67 20 73 75 72 65 20 74 68 61 74 20  aking sure that 
b2aa0 74 68 65 20 73 74 72 69 6e 67 20 69 73 20 77 65  the string is we
b2ab0 6c 6c 2d 66 6f 72 6d 65 64 0a 2a 2a 20 55 54 46  ll-formed.** UTF
b2ac0 2d 38 2e 20 20 4d 69 73 63 6f 64 65 64 20 63 68  -8.  Miscoded ch
b2ad0 61 72 61 63 74 65 72 73 20 61 72 65 20 72 65 6d  aracters are rem
b2ae0 6f 76 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  oved..**.** The 
b2af0 74 72 61 6e 73 6c 61 74 69 6f 6e 20 69 73 20 64  translation is d
b2b00 6f 6e 65 20 69 6e 2d 70 6c 61 63 65 20 28 73 69  one in-place (si
b2b10 6e 63 65 20 69 74 20 69 73 20 69 6d 70 6f 73 73  nce it is imposs
b2b20 69 62 6c 65 20 66 6f 72 20 74 68 65 0a 2a 2a 20  ible for the.** 
b2b30 63 6f 72 72 65 63 74 20 55 54 46 2d 38 20 65 6e  correct UTF-8 en
b2b40 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 6c 6f 6e  coding to be lon
b2b50 67 65 72 20 74 68 61 6e 20 61 20 6d 61 6c 66 6f  ger than a malfo
b2b60 72 6d 65 64 20 65 6e 63 6f 64 69 6e 67 29 2e 0a  rmed encoding)..
b2b70 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b2b80 45 20 69 6e 74 20 73 71 6c 69 74 65 33 55 74 66  E int sqlite3Utf
b2b90 38 54 6f 38 28 75 6e 73 69 67 6e 65 64 20 63 68  8To8(unsigned ch
b2ba0 61 72 20 2a 7a 49 6e 29 7b 0a 20 20 75 6e 73 69  ar *zIn){.  unsi
b2bb0 67 6e 65 64 20 63 68 61 72 20 2a 7a 4f 75 74 20  gned char *zOut 
b2bc0 3d 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65  = zIn;.  unsigne
b2bd0 64 20 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d  d char *zStart =
b2be0 20 7a 49 6e 3b 0a 20 20 75 6e 73 69 67 6e 65 64   zIn;.  unsigned
b2bf0 20 63 68 61 72 20 2a 7a 54 65 72 6d 20 3d 20 26   char *zTerm = &
b2c00 7a 49 6e 5b 73 74 72 6c 65 6e 28 28 63 68 61 72  zIn[strlen((char
b2c10 20 2a 29 7a 49 6e 29 5d 3b 0a 20 20 75 33 32 20   *)zIn)];.  u32 
b2c20 63 3b 0a 0a 20 20 77 68 69 6c 65 28 20 7a 49 6e  c;..  while( zIn
b2c30 5b 30 5d 20 29 7b 0a 20 20 20 20 63 20 3d 20 73  [0] ){.    c = s
b2c40 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
b2c50 49 6e 2c 20 7a 54 65 72 6d 2c 20 28 63 6f 6e 73  In, zTerm, (cons
b2c60 74 20 75 38 2a 2a 29 26 7a 49 6e 29 3b 0a 20 20  t u8**)&zIn);.  
b2c70 20 20 69 66 28 20 63 21 3d 30 78 66 66 66 64 20    if( c!=0xfffd 
b2c80 29 7b 0a 20 20 20 20 20 20 57 52 49 54 45 5f 55  ){.      WRITE_U
b2c90 54 46 38 28 7a 4f 75 74 2c 20 63 29 3b 0a 20 20  TF8(zOut, c);.  
b2ca0 20 20 7d 0a 20 20 7d 0a 20 20 2a 7a 4f 75 74 20    }.  }.  *zOut 
b2cb0 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 7a 4f  = 0;.  return zO
b2cc0 75 74 20 2d 20 7a 53 74 61 72 74 3b 0a 7d 0a 23  ut - zStart;.}.#
b2cd0 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53  endif..#ifndef S
b2ce0 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
b2cf0 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
b2d00 20 55 54 46 2d 31 36 20 73 74 72 69 6e 67 20 69   UTF-16 string i
b2d10 6e 20 74 68 65 20 6e 61 74 69 76 65 20 65 6e 63  n the native enc
b2d20 6f 64 69 6e 67 20 69 6e 74 6f 20 61 20 55 54 46  oding into a UTF
b2d30 2d 38 20 73 74 72 69 6e 67 2e 0a 2a 2a 20 4d 65  -8 string..** Me
b2d40 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
b2d50 20 55 54 46 2d 38 20 73 74 72 69 6e 67 20 69 73   UTF-8 string is
b2d60 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
b2d70 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 20 61 6e  qlite3_malloc an
b2d80 64 20 6d 75 73 74 0a 2a 2a 20 62 65 20 66 72 65  d must.** be fre
b2d90 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ed by the callin
b2da0 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a  g function..**.*
b2db0 2a 20 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e  * NULL is return
b2dc0 65 64 20 69 66 20 74 68 65 72 65 20 69 73 20 61  ed if there is a
b2dd0 6e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  n allocation err
b2de0 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  or..*/.SQLITE_PR
b2df0 49 56 41 54 45 20 63 68 61 72 20 2a 73 71 6c 69  IVATE char *sqli
b2e00 74 65 33 55 74 66 31 36 74 6f 38 28 73 71 6c 69  te3Utf16to8(sqli
b2e10 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 76  te3 *db, const v
b2e20 6f 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 42 79 74  oid *z, int nByt
b2e30 65 29 7b 0a 20 20 4d 65 6d 20 6d 3b 0a 20 20 6d  e){.  Mem m;.  m
b2e40 65 6d 73 65 74 28 26 6d 2c 20 30 2c 20 73 69 7a  emset(&m, 0, siz
b2e50 65 6f 66 28 6d 29 29 3b 0a 20 20 6d 2e 64 62 20  eof(m));.  m.db 
b2e60 3d 20 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 56  = db;.  sqlite3V
b2e70 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 6d 2c  dbeMemSetStr(&m,
b2e80 20 7a 2c 20 6e 42 79 74 65 2c 20 53 51 4c 49 54   z, nByte, SQLIT
b2e90 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53  E_UTF16NATIVE, S
b2ea0 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
b2eb0 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
b2ec0 67 65 45 6e 63 6f 64 69 6e 67 28 26 6d 2c 20 53  geEncoding(&m, S
b2ed0 51 4c 49 54 45 5f 55 54 46 38 29 3b 0a 20 20 69  QLITE_UTF8);.  i
b2ee0 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
b2ef0 6c 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  led ){.    sqlit
b2f00 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
b2f10 28 26 6d 29 3b 0a 20 20 20 20 6d 2e 7a 20 3d 20  (&m);.    m.z = 
b2f20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  0;.  }.  assert(
b2f30 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   (m.flags & MEM_
b2f40 54 65 72 6d 29 21 3d 30 20 7c 7c 20 64 62 2d 3e  Term)!=0 || db->
b2f50 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
b2f60 20 20 61 73 73 65 72 74 28 20 28 6d 2e 66 6c 61    assert( (m.fla
b2f70 67 73 20 26 20 4d 45 4d 5f 53 74 72 29 21 3d 30  gs & MEM_Str)!=0
b2f80 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
b2f90 69 6c 65 64 20 29 3b 0a 20 20 72 65 74 75 72 6e  iled );.  return
b2fa0 20 28 6d 2e 66 6c 61 67 73 20 26 20 4d 45 4d 5f   (m.flags & MEM_
b2fb0 44 79 6e 29 21 3d 30 20 3f 20 6d 2e 7a 20 3a 20  Dyn)!=0 ? m.z : 
b2fc0 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
b2fd0 64 62 2c 20 6d 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a  db, m.z);.}../*.
b2fe0 2a 2a 20 70 5a 20 69 73 20 61 20 55 54 46 2d 31  ** pZ is a UTF-1
b2ff0 36 20 65 6e 63 6f 64 65 64 20 75 6e 69 63 6f 64  6 encoded unicod
b3000 65 20 73 74 72 69 6e 67 2e 20 49 66 20 6e 43 68  e string. If nCh
b3010 61 72 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20  ar is less than 
b3020 7a 65 72 6f 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  zero,.** return 
b3030 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
b3040 74 65 73 20 75 70 20 74 6f 20 28 62 75 74 20 6e  tes up to (but n
b3050 6f 74 20 69 6e 63 6c 75 64 69 6e 67 29 2c 20 74  ot including), t
b3060 68 65 20 66 69 72 73 74 20 70 61 69 72 0a 2a 2a  he first pair.**
b3070 20 6f 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20   of consecutive 
b3080 30 78 30 30 20 62 79 74 65 73 20 69 6e 20 70 5a  0x00 bytes in pZ
b3090 2e 20 49 66 20 6e 43 68 61 72 20 69 73 20 6e 6f  . If nChar is no
b30a0 74 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  t less than zero
b30b0 2c 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e  ,.** then return
b30c0 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
b30d0 79 74 65 73 20 69 6e 20 74 68 65 20 66 69 72 73  ytes in the firs
b30e0 74 20 6e 43 68 61 72 20 75 6e 69 63 6f 64 65 20  t nChar unicode 
b30f0 63 68 61 72 61 63 74 65 72 73 0a 2a 2a 20 69 6e  characters.** in
b3100 20 70 5a 20 28 6f 72 20 75 70 20 75 6e 74 69 6c   pZ (or up until
b3110 20 74 68 65 20 66 69 72 73 74 20 70 61 69 72 20   the first pair 
b3120 6f 66 20 30 78 30 30 20 62 79 74 65 73 2c 20 77  of 0x00 bytes, w
b3130 68 69 63 68 65 76 65 72 20 63 6f 6d 65 73 20 66  hichever comes f
b3140 69 72 73 74 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45  irst)..*/.SQLITE
b3150 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b3160 69 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e  ite3Utf16ByteLen
b3170 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 49 6e  (const void *zIn
b3180 2c 20 69 6e 74 20 6e 43 68 61 72 29 7b 0a 20 20  , int nChar){.  
b3190 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 63 20 3d  unsigned int c =
b31a0 20 31 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74   1;.  char const
b31b0 20 2a 7a 20 3d 20 7a 49 6e 3b 0a 20 20 69 6e 74   *z = zIn;.  int
b31c0 20 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20 53 51   n = 0;.  if( SQ
b31d0 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45  LITE_UTF16NATIVE
b31e0 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
b31f0 20 29 7b 0a 20 20 20 20 2f 2a 20 55 73 69 6e 67   ){.    /* Using
b3200 20 61 6e 20 22 69 66 20 28 53 51 4c 49 54 45 5f   an "if (SQLITE_
b3210 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c  UTF16NATIVE==SQL
b3220 49 54 45 5f 55 54 46 31 36 42 45 29 22 20 63 6f  ITE_UTF16BE)" co
b3230 6e 73 74 72 75 63 74 20 68 65 72 65 0a 20 20 20  nstruct here.   
b3240 20 2a 2a 20 61 6e 64 20 69 6e 20 6f 74 68 65 72   ** and in other
b3250 20 70 61 72 74 73 20 6f 66 20 74 68 69 73 20 66   parts of this f
b3260 69 6c 65 20 6d 65 61 6e 73 20 74 68 61 74 20 61  ile means that a
b3270 74 20 6f 6e 65 20 62 72 61 6e 63 68 20 77 69 6c  t one branch wil
b3280 6c 0a 20 20 20 20 2a 2a 20 6e 6f 74 20 62 65 20  l.    ** not be 
b3290 63 6f 76 65 72 65 64 20 62 79 20 63 6f 76 65 72  covered by cover
b32a0 61 67 65 20 74 65 73 74 69 6e 67 20 6f 6e 20 61  age testing on a
b32b0 6e 79 20 73 69 6e 67 6c 65 20 68 6f 73 74 2e 20  ny single host. 
b32c0 42 75 74 20 63 6f 76 65 72 61 67 65 0a 20 20 20  But coverage.   
b32d0 20 2a 2a 20 77 69 6c 6c 20 62 65 20 63 6f 6d 70   ** will be comp
b32e0 6c 65 74 65 20 69 66 20 74 68 65 20 74 65 73 74  lete if the test
b32f0 73 20 61 72 65 20 72 75 6e 20 6f 6e 20 62 6f 74  s are run on bot
b3300 68 20 61 20 6c 69 74 74 6c 65 2d 65 6e 64 69 61  h a little-endia
b3310 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 62 69  n and .    ** bi
b3320 67 2d 65 6e 64 69 61 6e 20 68 6f 73 74 2e 20 42  g-endian host. B
b3330 65 63 61 75 73 65 20 62 6f 74 68 20 74 68 65 20  ecause both the 
b3340 55 54 46 31 36 4e 41 54 49 56 45 20 61 6e 64 20  UTF16NATIVE and 
b3350 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 0a 20  SQLITE_UTF16BE. 
b3360 20 20 20 2a 2a 20 6d 61 63 72 6f 73 20 61 72 65     ** macros are
b3370 20 63 6f 6e 73 74 61 6e 74 20 61 74 20 63 6f 6d   constant at com
b3380 70 69 6c 65 20 74 69 6d 65 20 74 68 65 20 63 6f  pile time the co
b3390 6d 70 69 6c 65 72 20 63 61 6e 20 64 65 74 65 72  mpiler can deter
b33a0 6d 69 6e 65 0a 20 20 20 20 2a 2a 20 77 68 69 63  mine.    ** whic
b33b0 68 20 62 72 61 6e 63 68 20 77 69 6c 6c 20 62 65  h branch will be
b33c0 20 66 6f 6c 6c 6f 77 65 64 2e 20 49 74 20 69 73   followed. It is
b33d0 20 74 68 65 72 65 66 6f 72 65 20 61 73 73 75 6d   therefore assum
b33e0 65 64 20 74 68 61 74 20 6e 6f 20 72 75 6e 74 69  ed that no runti
b33f0 6d 65 0a 20 20 20 20 2a 2a 20 70 65 6e 61 6c 74  me.    ** penalt
b3400 79 20 69 73 20 70 61 69 64 20 66 6f 72 20 74 68  y is paid for th
b3410 69 73 20 22 69 66 22 20 73 74 61 74 65 6d 65 6e  is "if" statemen
b3420 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  t..    */.    wh
b3430 69 6c 65 28 20 63 20 26 26 20 28 28 6e 43 68 61  ile( c && ((nCha
b3440 72 3c 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29  r<0) || n<nChar)
b3450 20 29 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55   ){.      READ_U
b3460 54 46 31 36 42 45 28 7a 2c 20 63 29 3b 0a 20 20  TF16BE(z, c);.  
b3470 20 20 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20      n++;.    }. 
b3480 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68 69 6c   }else{.    whil
b3490 65 28 20 63 20 26 26 20 28 28 6e 43 68 61 72 3c  e( c && ((nChar<
b34a0 30 29 20 7c 7c 20 6e 3c 6e 43 68 61 72 29 20 29  0) || n<nChar) )
b34b0 7b 0a 20 20 20 20 20 20 52 45 41 44 5f 55 54 46  {.      READ_UTF
b34c0 31 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20  16LE(z, c);.    
b34d0 20 20 6e 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d    n++;.    }.  }
b34e0 0a 20 20 72 65 74 75 72 6e 20 28 7a 2d 28 63 68  .  return (z-(ch
b34f0 61 72 20 63 6f 6e 73 74 20 2a 29 7a 49 6e 29 2d  ar const *)zIn)-
b3500 28 28 63 3d 3d 30 29 3f 32 3a 30 29 3b 0a 7d 0a  ((c==0)?2:0);.}.
b3510 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
b3520 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a 2a 2a 20  ITE_TEST)./*.** 
b3530 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
b3540 63 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20  called from the 
b3550 54 43 4c 20 74 65 73 74 20 66 75 6e 63 74 69 6f  TCL test functio
b3560 6e 20 22 74 72 61 6e 73 6c 61 74 65 5f 73 65 6c  n "translate_sel
b3570 66 74 65 73 74 22 2e 0a 2a 2a 20 49 74 20 63 68  ftest"..** It ch
b3580 65 63 6b 73 20 74 68 61 74 20 74 68 65 20 70 72  ecks that the pr
b3590 69 6d 69 74 69 76 65 73 20 66 6f 72 20 73 65 72  imitives for ser
b35a0 69 61 6c 69 7a 69 6e 67 20 61 6e 64 20 64 65 73  ializing and des
b35b0 65 72 69 61 6c 69 7a 69 6e 67 0a 2a 2a 20 63 68  erializing.** ch
b35c0 61 72 61 63 74 65 72 73 20 69 6e 20 65 61 63 68  aracters in each
b35d0 20 65 6e 63 6f 64 69 6e 67 20 61 72 65 20 69 6e   encoding are in
b35e0 76 65 72 73 65 73 20 6f 66 20 65 61 63 68 20 6f  verses of each o
b35f0 74 68 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ther..*/.SQLITE_
b3600 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
b3610 69 74 65 33 55 74 66 53 65 6c 66 54 65 73 74 28  ite3UtfSelfTest(
b3620 76 6f 69 64 29 7b 0a 20 20 75 6e 73 69 67 6e 65  void){.  unsigne
b3630 64 20 69 6e 74 20 69 2c 20 74 3b 0a 20 20 75 6e  d int i, t;.  un
b3640 73 69 67 6e 65 64 20 63 68 61 72 20 7a 42 75 66  signed char zBuf
b3650 5b 32 30 5d 3b 0a 20 20 75 6e 73 69 67 6e 65 64  [20];.  unsigned
b3660 20 63 68 61 72 20 2a 7a 3b 0a 20 20 75 6e 73 69   char *z;.  unsi
b3670 67 6e 65 64 20 63 68 61 72 20 2a 7a 54 65 72 6d  gned char *zTerm
b3680 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 75 6e 73  ;.  int n;.  uns
b3690 69 67 6e 65 64 20 69 6e 74 20 63 3b 0a 0a 20 20  igned int c;..  
b36a0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b36b0 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b36c0 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 57   z = zBuf;.    W
b36d0 52 49 54 45 5f 55 54 46 38 28 7a 2c 20 69 29 3b  RITE_UTF8(z, i);
b36e0 0a 20 20 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b  .    n = z-zBuf;
b36f0 0a 20 20 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20  .    z[0] = 0;. 
b3700 20 20 20 7a 54 65 72 6d 20 3d 20 7a 3b 0a 20 20     zTerm = z;.  
b3710 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20    z = zBuf;.    
b3720 63 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52  c = sqlite3Utf8R
b3730 65 61 64 28 7a 2c 20 7a 54 65 72 6d 2c 20 28 63  ead(z, zTerm, (c
b3740 6f 6e 73 74 20 75 38 2a 2a 29 26 7a 29 3b 0a 20  onst u8**)&z);. 
b3750 20 20 20 74 20 3d 20 69 3b 0a 20 20 20 20 69 66     t = i;.    if
b3760 28 20 69 3e 3d 30 78 44 38 30 30 20 26 26 20 69  ( i>=0xD800 && i
b3770 3c 3d 30 78 44 46 46 46 20 29 20 74 20 3d 20 30  <=0xDFFF ) t = 0
b3780 78 46 46 46 44 3b 0a 20 20 20 20 69 66 28 20 28  xFFFD;.    if( (
b3790 69 26 30 78 46 46 46 46 46 46 46 45 29 3d 3d 30  i&0xFFFFFFFE)==0
b37a0 78 46 46 46 45 20 29 20 74 20 3d 20 30 78 46 46  xFFFE ) t = 0xFF
b37b0 46 44 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  FD;.    assert( 
b37c0 63 3d 3d 74 20 29 3b 0a 20 20 20 20 61 73 73 65  c==t );.    asse
b37d0 72 74 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20  rt( (z-zBuf)==n 
b37e0 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
b37f0 3b 20 69 3c 30 78 30 30 31 31 30 30 30 30 3b 20  ; i<0x00110000; 
b3800 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 69 3e  i++){.    if( i>
b3810 3d 30 78 44 38 30 30 20 26 26 20 69 3c 30 78 45  =0xD800 && i<0xE
b3820 30 30 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  000 ) continue;.
b3830 20 20 20 20 7a 20 3d 20 7a 42 75 66 3b 0a 20 20      z = zBuf;.  
b3840 20 20 57 52 49 54 45 5f 55 54 46 31 36 4c 45 28    WRITE_UTF16LE(
b3850 7a 2c 20 69 29 3b 0a 20 20 20 20 6e 20 3d 20 7a  z, i);.    n = z
b3860 2d 7a 42 75 66 3b 0a 20 20 20 20 7a 5b 30 5d 20  -zBuf;.    z[0] 
b3870 3d 20 30 3b 0a 20 20 20 20 7a 20 3d 20 7a 42 75  = 0;.    z = zBu
b3880 66 3b 0a 20 20 20 20 52 45 41 44 5f 55 54 46 31  f;.    READ_UTF1
b3890 36 4c 45 28 7a 2c 20 63 29 3b 0a 20 20 20 20 61  6LE(z, c);.    a
b38a0 73 73 65 72 74 28 20 63 3d 3d 69 20 29 3b 0a 20  ssert( c==i );. 
b38b0 20 20 20 61 73 73 65 72 74 28 20 28 7a 2d 7a 42     assert( (z-zB
b38c0 75 66 29 3d 3d 6e 20 29 3b 0a 20 20 7d 0a 20 20  uf)==n );.  }.  
b38d0 66 6f 72 28 69 3d 30 3b 20 69 3c 30 78 30 30 31  for(i=0; i<0x001
b38e0 31 30 30 30 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  10000; i++){.   
b38f0 20 69 66 28 20 69 3e 3d 30 78 44 38 30 30 20 26   if( i>=0xD800 &
b3900 26 20 69 3c 30 78 45 30 30 30 20 29 20 63 6f 6e  & i<0xE000 ) con
b3910 74 69 6e 75 65 3b 0a 20 20 20 20 7a 20 3d 20 7a  tinue;.    z = z
b3920 42 75 66 3b 0a 20 20 20 20 57 52 49 54 45 5f 55  Buf;.    WRITE_U
b3930 54 46 31 36 42 45 28 7a 2c 20 69 29 3b 0a 20 20  TF16BE(z, i);.  
b3940 20 20 6e 20 3d 20 7a 2d 7a 42 75 66 3b 0a 20 20    n = z-zBuf;.  
b3950 20 20 7a 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20    z[0] = 0;.    
b3960 7a 20 3d 20 7a 42 75 66 3b 0a 20 20 20 20 52 45  z = zBuf;.    RE
b3970 41 44 5f 55 54 46 31 36 42 45 28 7a 2c 20 63 29  AD_UTF16BE(z, c)
b3980 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 3d  ;.    assert( c=
b3990 3d 69 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =i );.    assert
b39a0 28 20 28 7a 2d 7a 42 75 66 29 3d 3d 6e 20 29 3b  ( (z-zBuf)==n );
b39b0 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  .  }.}.#endif /*
b39c0 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2a 2f 0a   SQLITE_TEST */.
b39d0 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
b39e0 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a  _OMIT_UTF16 */..
b39f0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
b3a00 45 6e 64 20 6f 66 20 75 74 66 2e 63 20 2a 2a 2a  End of utf.c ***
b3a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3a20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3a30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
b3a40 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
b3a50 42 65 67 69 6e 20 66 69 6c 65 20 75 74 69 6c 2e  Begin file util.
b3a60 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
b3a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
b3a90 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
b3aa0 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
b3ab0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
b3ac0 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
b3ad0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
b3ae0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
b3af0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
b3b00 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
b3b10 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
b3b20 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
b3b30 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
b3b40 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
b3b50 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
b3b60 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
b3b70 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
b3b80 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
b3b90 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
b3ba0 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
b3bb0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
b3bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3be0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3bf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b3c00 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 55 74 69 6c 69 74  ******.** Utilit
b3c10 79 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64  y functions used
b3c20 20 74 68 72 6f 75 67 68 6f 75 74 20 73 71 6c 69   throughout sqli
b3c30 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  te..**.** This f
b3c40 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 66 75 6e  ile contains fun
b3c50 63 74 69 6f 6e 73 20 66 6f 72 20 61 6c 6c 6f 63  ctions for alloc
b3c60 61 74 69 6e 67 20 6d 65 6d 6f 72 79 2c 20 63 6f  ating memory, co
b3c70 6d 70 61 72 69 6e 67 0a 2a 2a 20 73 74 72 69 6e  mparing.** strin
b3c80 67 73 2c 20 61 6e 64 20 73 74 75 66 66 20 6c 69  gs, and stuff li
b3c90 6b 65 20 74 68 61 74 2e 0a 2a 2a 0a 2a 2a 20 24  ke that..**.** $
b3ca0 49 64 3a 20 75 74 69 6c 2e 63 2c 76 20 31 2e 32  Id: util.c,v 1.2
b3cb0 34 32 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39  42 2008/11/17 19
b3cc0 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39  :18:55 danielk19
b3cd0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a  77 Exp $.*/.../*
b3ce0 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
b3cf0 69 66 20 74 68 65 20 66 6c 6f 61 74 69 6e 67 20  if the floating 
b3d00 70 6f 69 6e 74 20 76 61 6c 75 65 20 69 73 20 4e  point value is N
b3d10 6f 74 20 61 20 4e 75 6d 62 65 72 20 28 4e 61 4e  ot a Number (NaN
b3d20 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  )..*/.SQLITE_PRI
b3d30 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
b3d40 49 73 4e 61 4e 28 64 6f 75 62 6c 65 20 78 29 7b  IsNaN(double x){
b3d50 0a 20 20 2f 2a 20 54 68 69 73 20 4e 61 4e 20 74  .  /* This NaN t
b3d60 65 73 74 20 73 6f 6d 65 74 69 6d 65 73 20 66 61  est sometimes fa
b3d70 69 6c 73 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ils if compiled 
b3d80 6f 6e 20 47 43 43 20 77 69 74 68 20 2d 66 66 61  on GCC with -ffa
b3d90 73 74 2d 6d 61 74 68 2e 0a 20 20 2a 2a 20 4f 6e  st-math..  ** On
b3da0 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 2c   the other hand,
b3db0 20 74 68 65 20 75 73 65 20 6f 66 20 2d 66 66 61   the use of -ffa
b3dc0 73 74 2d 6d 61 74 68 20 63 6f 6d 65 73 20 77 69  st-math comes wi
b3dd0 74 68 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  th the following
b3de0 0a 20 20 2a 2a 20 77 61 72 6e 69 6e 67 3a 0a 20  .  ** warning:. 
b3df0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 54 68   **.  **      Th
b3e00 69 73 20 6f 70 74 69 6f 6e 20 5b 2d 66 66 61 73  is option [-ffas
b3e10 74 2d 6d 61 74 68 5d 20 73 68 6f 75 6c 64 20 6e  t-math] should n
b3e20 65 76 65 72 20 62 65 20 74 75 72 6e 65 64 20 6f  ever be turned o
b3e30 6e 20 62 79 20 61 6e 79 0a 20 20 2a 2a 20 20 20  n by any.  **   
b3e40 20 20 20 2d 4f 20 6f 70 74 69 6f 6e 20 73 69 6e     -O option sin
b3e50 63 65 20 69 74 20 63 61 6e 20 72 65 73 75 6c 74  ce it can result
b3e60 20 69 6e 20 69 6e 63 6f 72 72 65 63 74 20 6f 75   in incorrect ou
b3e70 74 70 75 74 20 66 6f 72 20 70 72 6f 67 72 61 6d  tput for program
b3e80 73 0a 20 20 2a 2a 20 20 20 20 20 20 77 68 69 63  s.  **      whic
b3e90 68 20 64 65 70 65 6e 64 20 6f 6e 20 61 6e 20 65  h depend on an e
b3ea0 78 61 63 74 20 69 6d 70 6c 65 6d 65 6e 74 61 74  xact implementat
b3eb0 69 6f 6e 20 6f 66 20 49 45 45 45 20 6f 72 20 49  ion of IEEE or I
b3ec0 53 4f 20 0a 20 20 2a 2a 20 20 20 20 20 20 72 75  SO .  **      ru
b3ed0 6c 65 73 2f 73 70 65 63 69 66 69 63 61 74 69 6f  les/specificatio
b3ee0 6e 73 20 66 6f 72 20 6d 61 74 68 20 66 75 6e 63  ns for math func
b3ef0 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
b3f00 20 55 6e 64 65 72 20 4d 53 56 43 2c 20 74 68 69   Under MSVC, thi
b3f10 73 20 4e 61 4e 20 74 65 73 74 20 6d 61 79 20 66  s NaN test may f
b3f20 61 69 6c 20 69 66 20 63 6f 6d 70 69 6c 65 64 20  ail if compiled 
b3f30 77 69 74 68 20 61 20 66 6c 6f 61 74 69 6e 67 2d  with a floating-
b3f40 0a 20 20 2a 2a 20 70 6f 69 6e 74 20 70 72 65 63  .  ** point prec
b3f50 69 73 69 6f 6e 20 6d 6f 64 65 20 6f 74 68 65 72  ision mode other
b3f60 20 74 68 61 6e 20 2f 66 70 3a 70 72 65 63 69 73   than /fp:precis
b3f70 65 2e 20 20 46 72 6f 6d 20 74 68 65 20 4d 53 44  e.  From the MSD
b3f80 4e 20 0a 20 20 2a 2a 20 64 6f 63 75 6d 65 6e 74  N .  ** document
b3f90 61 74 69 6f 6e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  ation:.  **.  **
b3fa0 20 20 20 20 20 20 54 68 65 20 63 6f 6d 70 69 6c        The compil
b3fb0 65 72 20 5b 77 69 74 68 20 2f 66 70 3a 70 72 65  er [with /fp:pre
b3fc0 63 69 73 65 5d 20 77 69 6c 6c 20 70 72 6f 70 65  cise] will prope
b3fd0 72 6c 79 20 68 61 6e 64 6c 65 20 63 6f 6d 70 61  rly handle compa
b3fe0 72 69 73 6f 6e 73 20 0a 20 20 2a 2a 20 20 20 20  risons .  **    
b3ff0 20 20 69 6e 76 6f 6c 76 69 6e 67 20 4e 61 4e 2e    involving NaN.
b4000 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 78 20   For example, x 
b4010 21 3d 20 78 20 65 76 61 6c 75 61 74 65 73 20 74  != x evaluates t
b4020 6f 20 74 72 75 65 20 69 66 20 78 20 69 73 20 4e  o true if x is N
b4030 61 4e 20 0a 20 20 2a 2a 20 20 20 20 20 20 2e 2e  aN .  **      ..
b4040 2e 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 5f 5f  ..  */.#ifdef __
b4050 46 41 53 54 5f 4d 41 54 48 5f 5f 0a 23 20 65 72  FAST_MATH__.# er
b4060 72 6f 72 20 53 51 4c 69 74 65 20 77 69 6c 6c 20  ror SQLite will 
b4070 6e 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74  not work correct
b4080 6c 79 20 77 69 74 68 20 74 68 65 20 2d 66 66 61  ly with the -ffa
b4090 73 74 2d 6d 61 74 68 20 6f 70 74 69 6f 6e 20 6f  st-math option o
b40a0 66 20 47 43 43 2e 0a 23 65 6e 64 69 66 0a 20 20  f GCC..#endif.  
b40b0 76 6f 6c 61 74 69 6c 65 20 64 6f 75 62 6c 65 20  volatile double 
b40c0 79 20 3d 20 78 3b 0a 20 20 76 6f 6c 61 74 69 6c  y = x;.  volatil
b40d0 65 20 64 6f 75 62 6c 65 20 7a 20 3d 20 79 3b 0a  e double z = y;.
b40e0 20 20 72 65 74 75 72 6e 20 79 21 3d 7a 3b 0a 7d    return y!=z;.}
b40f0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
b4100 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 61 20 73  he length of a s
b4110 74 72 69 6e 67 2c 20 65 78 63 65 70 74 20 64 6f  tring, except do
b4120 20 6e 6f 74 20 61 6c 6c 6f 77 20 74 68 65 20 73   not allow the s
b4130 74 72 69 6e 67 20 6c 65 6e 67 74 68 0a 2a 2a 20  tring length.** 
b4140 74 6f 20 65 78 63 65 65 64 20 74 68 65 20 53 51  to exceed the SQ
b4150 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
b4160 48 20 73 65 74 74 69 6e 67 2e 0a 2a 2f 0a 53 51  H setting..*/.SQ
b4170 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b4180 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 73   sqlite3Strlen(s
b4190 71 6c 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73  qlite3 *db, cons
b41a0 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 63 6f  t char *z){.  co
b41b0 6e 73 74 20 63 68 61 72 20 2a 7a 32 20 3d 20 7a  nst char *z2 = z
b41c0 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69  ;.  int len;.  i
b41d0 6e 74 20 78 3b 0a 20 20 77 68 69 6c 65 28 20 2a  nt x;.  while( *
b41e0 7a 32 20 29 7b 20 7a 32 2b 2b 3b 20 7d 0a 20 20  z2 ){ z2++; }.  
b41f0 78 20 3d 20 7a 32 20 2d 20 7a 3b 0a 20 20 6c 65  x = z2 - z;.  le
b4200 6e 20 3d 20 30 78 37 66 66 66 66 66 66 66 20 26  n = 0x7fffffff &
b4210 20 78 3b 0a 20 20 69 66 28 20 6c 65 6e 21 3d 78   x;.  if( len!=x
b4220 20 7c 7c 20 6c 65 6e 20 3e 20 64 62 2d 3e 61 4c   || len > db->aL
b4230 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
b4240 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
b4250 20 72 65 74 75 72 6e 20 64 62 2d 3e 61 4c 69 6d   return db->aLim
b4260 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
b4270 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d 65 6c 73 65  LENGTH];.  }else
b4280 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 65 6e  {.    return len
b4290 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
b42a0 65 74 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  et the most rece
b42b0 6e 74 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e  nt error code an
b42c0 64 20 65 72 72 6f 72 20 73 74 72 69 6e 67 20 66  d error string f
b42d0 6f 72 20 74 68 65 20 73 71 6c 69 74 65 0a 2a 2a  or the sqlite.**
b42e0 20 68 61 6e 64 6c 65 20 22 64 62 22 2e 20 54 68   handle "db". Th
b42f0 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20  e error code is 
b4300 73 65 74 20 74 6f 20 22 65 72 72 5f 63 6f 64 65  set to "err_code
b4310 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 74 20 69  "..**.** If it i
b4320 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 74 72 69  s not NULL, stri
b4330 6e 67 20 7a 46 6f 72 6d 61 74 20 73 70 65 63 69  ng zFormat speci
b4340 66 69 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20  fies the format 
b4350 6f 66 20 74 68 65 0a 2a 2a 20 65 72 72 6f 72 20  of the.** error 
b4360 73 74 72 69 6e 67 20 69 6e 20 74 68 65 20 73 74  string in the st
b4370 79 6c 65 20 6f 66 20 74 68 65 20 70 72 69 6e 74  yle of the print
b4380 66 20 66 75 6e 63 74 69 6f 6e 73 3a 20 54 68 65  f functions: The
b4390 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 66 6f   following.** fo
b43a0 72 6d 61 74 20 63 68 61 72 61 63 74 65 72 73 20  rmat characters 
b43b0 61 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a  are allowed:.**.
b43c0 2a 2a 20 20 20 20 20 20 25 73 20 20 20 20 20 20  **      %s      
b43d0 49 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a  Insert a string.
b43e0 2a 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20 20  **      %z      
b43f0 41 20 73 74 72 69 6e 67 20 74 68 61 74 20 73 68  A string that sh
b4400 6f 75 6c 64 20 62 65 20 66 72 65 65 64 20 61 66  ould be freed af
b4410 74 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  ter use.**      
b4420 25 64 20 20 20 20 20 20 49 6e 73 65 72 74 20 61  %d      Insert a
b4430 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  n integer.**    
b4440 20 20 25 54 20 20 20 20 20 20 49 6e 73 65 72 74    %T      Insert
b4450 20 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20   a token.**     
b4460 20 25 53 20 20 20 20 20 20 49 6e 73 65 72 74 20   %S      Insert 
b4470 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e  the first elemen
b4480 74 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a  t of a SrcList.*
b4490 2a 0a 2a 2a 20 7a 46 6f 72 6d 61 74 20 61 6e 64  *.** zFormat and
b44a0 20 61 6e 79 20 73 74 72 69 6e 67 20 74 6f 6b 65   any string toke
b44b0 6e 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20 69  ns that follow i
b44c0 74 20 61 72 65 20 61 73 73 75 6d 65 64 20 74 6f  t are assumed to
b44d0 20 62 65 0a 2a 2a 20 65 6e 63 6f 64 65 64 20 69   be.** encoded i
b44e0 6e 20 55 54 46 2d 38 2e 0a 2a 2a 0a 2a 2a 20 54  n UTF-8..**.** T
b44f0 6f 20 63 6c 65 61 72 20 74 68 65 20 6d 6f 73 74  o clear the most
b4500 20 72 65 63 65 6e 74 20 65 72 72 6f 72 20 66 6f   recent error fo
b4510 72 20 73 71 6c 69 74 65 20 68 61 6e 64 6c 65 20  r sqlite handle 
b4520 22 64 62 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "db", sqlite3Err
b4530 6f 72 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  or.** should be 
b4540 63 61 6c 6c 65 64 20 77 69 74 68 20 65 72 72 5f  called with err_
b4550 63 6f 64 65 20 73 65 74 20 74 6f 20 53 51 4c 49  code set to SQLI
b4560 54 45 5f 4f 4b 20 61 6e 64 20 7a 46 6f 72 6d 61  TE_OK and zForma
b4570 74 20 73 65 74 0a 2a 2a 20 74 6f 20 4e 55 4c 4c  t set.** to NULL
b4580 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b4590 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
b45a0 45 72 72 6f 72 28 73 71 6c 69 74 65 33 20 2a 64  Error(sqlite3 *d
b45b0 62 2c 20 69 6e 74 20 65 72 72 5f 63 6f 64 65 2c  b, int err_code,
b45c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f   const char *zFo
b45d0 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20 69 66  rmat, ...){.  if
b45e0 28 20 64 62 20 26 26 20 28 64 62 2d 3e 70 45 72  ( db && (db->pEr
b45f0 72 20 7c 7c 20 28 64 62 2d 3e 70 45 72 72 20 3d  r || (db->pErr =
b4600 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65 77   sqlite3ValueNew
b4610 28 64 62 29 29 21 3d 30 29 20 29 7b 0a 20 20 20  (db))!=0) ){.   
b4620 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 65   db->errCode = e
b4630 72 72 5f 63 6f 64 65 3b 0a 20 20 20 20 69 66 28  rr_code;.    if(
b4640 20 7a 46 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20   zFormat ){.    
b4650 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 20 20 20    char *z;.     
b4660 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 20   va_list ap;.   
b4670 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c 20     va_start(ap, 
b4680 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 20 20  zFormat);.      
b4690 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
b46a0 6e 74 66 28 64 62 2c 20 7a 46 6f 72 6d 61 74 2c  ntf(db, zFormat,
b46b0 20 61 70 29 3b 0a 20 20 20 20 20 20 76 61 5f 65   ap);.      va_e
b46c0 6e 64 28 61 70 29 3b 0a 20 20 20 20 20 20 73 71  nd(ap);.      sq
b46d0 6c 69 74 65 33 56 61 6c 75 65 53 65 74 53 74 72  lite3ValueSetStr
b46e0 28 64 62 2d 3e 70 45 72 72 2c 20 2d 31 2c 20 7a  (db->pErr, -1, z
b46f0 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 53  , SQLITE_UTF8, S
b4700 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a  QLITE_DYNAMIC);.
b4710 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
b4720 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
b4730 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 30 2c  Str(db->pErr, 0,
b4740 20 30 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   0, SQLITE_UTF8,
b4750 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
b4760 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
b4770 0a 2a 2a 20 41 64 64 20 61 6e 20 65 72 72 6f 72  .** Add an error
b4780 20 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 72   message to pPar
b4790 73 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20  se->zErrMsg and 
b47a0 69 6e 63 72 65 6d 65 6e 74 20 70 50 61 72 73 65  increment pParse
b47b0 2d 3e 6e 45 72 72 2e 0a 2a 2a 20 54 68 65 20 66  ->nErr..** The f
b47c0 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 61 74 74  ollowing formatt
b47d0 69 6e 67 20 63 68 61 72 61 63 74 65 72 73 20 61  ing characters a
b47e0 72 65 20 61 6c 6c 6f 77 65 64 3a 0a 2a 2a 0a 2a  re allowed:.**.*
b47f0 2a 20 20 20 20 20 20 25 73 20 20 20 20 20 20 49  *      %s      I
b4800 6e 73 65 72 74 20 61 20 73 74 72 69 6e 67 0a 2a  nsert a string.*
b4810 2a 20 20 20 20 20 20 25 7a 20 20 20 20 20 20 41  *      %z      A
b4820 20 73 74 72 69 6e 67 20 74 68 61 74 20 73 68 6f   string that sho
b4830 75 6c 64 20 62 65 20 66 72 65 65 64 20 61 66 74  uld be freed aft
b4840 65 72 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 25  er use.**      %
b4850 64 20 20 20 20 20 20 49 6e 73 65 72 74 20 61 6e  d      Insert an
b4860 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20   integer.**     
b4870 20 25 54 20 20 20 20 20 20 49 6e 73 65 72 74 20   %T      Insert 
b4880 61 20 74 6f 6b 65 6e 0a 2a 2a 20 20 20 20 20 20  a token.**      
b4890 25 53 20 20 20 20 20 20 49 6e 73 65 72 74 20 74  %S      Insert t
b48a0 68 65 20 66 69 72 73 74 20 65 6c 65 6d 65 6e 74  he first element
b48b0 20 6f 66 20 61 20 53 72 63 4c 69 73 74 0a 2a 2a   of a SrcList.**
b48c0 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
b48d0 6e 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  n should be used
b48e0 20 74 6f 20 72 65 70 6f 72 74 20 61 6e 79 20 65   to report any e
b48f0 72 72 6f 72 20 74 68 61 74 20 6f 63 63 75 72 73  rror that occurs
b4900 20 77 68 69 6c 73 74 0a 2a 2a 20 63 6f 6d 70 69   whilst.** compi
b4910 6c 69 6e 67 20 61 6e 20 53 51 4c 20 73 74 61 74  ling an SQL stat
b4920 65 6d 65 6e 74 20 28 69 2e 65 2e 20 77 69 74 68  ement (i.e. with
b4930 69 6e 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  in sqlite3_prepa
b4940 72 65 28 29 29 2e 20 54 68 65 0a 2a 2a 20 6c 61  re()). The.** la
b4950 73 74 20 74 68 69 6e 67 20 74 68 65 20 73 71 6c  st thing the sql
b4960 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 66  ite3_prepare() f
b4970 75 6e 63 74 69 6f 6e 20 64 6f 65 73 20 69 73 20  unction does is 
b4980 63 6f 70 79 20 74 68 65 20 65 72 72 6f 72 0a 2a  copy the error.*
b4990 2a 20 73 74 6f 72 65 64 20 62 79 20 74 68 69 73  * stored by this
b49a0 20 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 74   function into t
b49b0 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
b49c0 6c 65 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  le using sqlite3
b49d0 45 72 72 6f 72 28 29 2e 0a 2a 2a 20 46 75 6e 63  Error()..** Func
b49e0 74 69 6f 6e 20 73 71 6c 69 74 65 33 45 72 72 6f  tion sqlite3Erro
b49f0 72 28 29 20 73 68 6f 75 6c 64 20 62 65 20 75 73  r() should be us
b4a00 65 64 20 64 75 72 69 6e 67 20 73 74 61 74 65 6d  ed during statem
b4a10 65 6e 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a  ent execution.**
b4a20 20 28 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29   (sqlite3_step()
b4a30 20 65 74 63 2e 29 2e 0a 2a 2f 0a 53 51 4c 49 54   etc.)..*/.SQLIT
b4a40 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
b4a50 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 50  qlite3ErrorMsg(P
b4a60 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 63 6f  arse *pParse, co
b4a70 6e 73 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61  nst char *zForma
b4a80 74 2c 20 2e 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69  t, ...){.  va_li
b4a90 73 74 20 61 70 3b 0a 20 20 73 71 6c 69 74 65 33  st ap;.  sqlite3
b4aa0 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
b4ab0 62 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 45 72  b;.  pParse->nEr
b4ac0 72 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  r++;.  sqlite3Db
b4ad0 46 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d  Free(db, pParse-
b4ae0 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 76 61 5f  >zErrMsg);.  va_
b4af0 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d 61  start(ap, zForma
b4b00 74 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45  t);.  pParse->zE
b4b10 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 56  rrMsg = sqlite3V
b4b20 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72  MPrintf(db, zFor
b4b30 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
b4b40 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 50  nd(ap);.  if( pP
b4b50 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
b4b60 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 72 73  _OK ){.    pPars
b4b70 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45  e->rc = SQLITE_E
b4b80 52 52 4f 52 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  RROR;.  }.}../*.
b4b90 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 65 72 72  ** Clear the err
b4ba0 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70 50  or message in pP
b4bb0 61 72 73 65 2c 20 69 66 20 61 6e 79 0a 2a 2f 0a  arse, if any.*/.
b4bc0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
b4bd0 6f 69 64 20 73 71 6c 69 74 65 33 45 72 72 6f 72  oid sqlite3Error
b4be0 43 6c 65 61 72 28 50 61 72 73 65 20 2a 70 50 61  Clear(Parse *pPa
b4bf0 72 73 65 29 7b 0a 20 20 73 71 6c 69 74 65 33 44  rse){.  sqlite3D
b4c00 62 46 72 65 65 28 70 50 61 72 73 65 2d 3e 64 62  bFree(pParse->db
b4c10 2c 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73  , pParse->zErrMs
b4c20 67 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 45  g);.  pParse->zE
b4c30 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 70 50 61  rrMsg = 0;.  pPa
b4c40 72 73 65 2d 3e 6e 45 72 72 20 3d 20 30 3b 0a 7d  rse->nErr = 0;.}
b4c50 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  ../*.** Convert 
b4c60 61 6e 20 53 51 4c 2d 73 74 79 6c 65 20 71 75 6f  an SQL-style quo
b4c70 74 65 64 20 73 74 72 69 6e 67 20 69 6e 74 6f 20  ted string into 
b4c80 61 20 6e 6f 72 6d 61 6c 20 73 74 72 69 6e 67 20  a normal string 
b4c90 62 79 20 72 65 6d 6f 76 69 6e 67 0a 2a 2a 20 74  by removing.** t
b4ca0 68 65 20 71 75 6f 74 65 20 63 68 61 72 61 63 74  he quote charact
b4cb0 65 72 73 2e 20 20 54 68 65 20 63 6f 6e 76 65 72  ers.  The conver
b4cc0 73 69 6f 6e 20 69 73 20 64 6f 6e 65 20 69 6e 2d  sion is done in-
b4cd0 70 6c 61 63 65 2e 20 20 49 66 20 74 68 65 0a 2a  place.  If the.*
b4ce0 2a 20 69 6e 70 75 74 20 64 6f 65 73 20 6e 6f 74  * input does not
b4cf0 20 62 65 67 69 6e 20 77 69 74 68 20 61 20 71 75   begin with a qu
b4d00 6f 74 65 20 63 68 61 72 61 63 74 65 72 2c 20 74  ote character, t
b4d10 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
b4d20 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  .** is a no-op..
b4d30 2a 2a 0a 2a 2a 20 32 30 30 32 2d 46 65 62 2d 31  **.** 2002-Feb-1
b4d40 34 3a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  4: This routine 
b4d50 69 73 20 65 78 74 65 6e 64 65 64 20 74 6f 20 72  is extended to r
b4d60 65 6d 6f 76 65 20 4d 53 2d 41 63 63 65 73 73 20  emove MS-Access 
b4d70 73 74 79 6c 65 0a 2a 2a 20 62 72 61 63 6b 65 74  style.** bracket
b4d80 73 20 66 72 6f 6d 20 61 72 6f 75 6e 64 20 69 64  s from around id
b4d90 65 6e 74 69 66 65 72 73 2e 20 20 46 6f 72 20 65  entifers.  For e
b4da0 78 61 6d 70 6c 65 3a 20 20 22 5b 61 2d 62 2d 63  xample:  "[a-b-c
b4db0 5d 22 20 62 65 63 6f 6d 65 73 0a 2a 2a 20 22 61  ]" becomes.** "a
b4dc0 2d 62 2d 63 22 2e 0a 2a 2f 0a 53 51 4c 49 54 45  -b-c"..*/.SQLITE
b4dd0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
b4de0 6c 69 74 65 33 44 65 71 75 6f 74 65 28 63 68 61  lite3Dequote(cha
b4df0 72 20 2a 7a 29 7b 0a 20 20 69 6e 74 20 71 75 6f  r *z){.  int quo
b4e00 74 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  te;.  int i, j;.
b4e10 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74    if( z==0 ) ret
b4e20 75 72 6e 3b 0a 20 20 71 75 6f 74 65 20 3d 20 7a  urn;.  quote = z
b4e30 5b 30 5d 3b 0a 20 20 73 77 69 74 63 68 28 20 71  [0];.  switch( q
b4e40 75 6f 74 65 20 29 7b 0a 20 20 20 20 63 61 73 65  uote ){.    case
b4e50 20 27 5c 27 27 3a 20 20 62 72 65 61 6b 3b 0a 20   '\'':  break;. 
b4e60 20 20 20 63 61 73 65 20 27 22 27 3a 20 20 20 62     case '"':   b
b4e70 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 27  reak;.    case '
b4e80 60 27 3a 20 20 20 62 72 65 61 6b 3b 20 20 20 20  `':   break;    
b4e90 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
b4ea0 6f 72 20 4d 79 53 51 4c 20 63 6f 6d 70 61 74 69  or MySQL compati
b4eb0 62 69 6c 69 74 79 20 2a 2f 0a 20 20 20 20 63 61  bility */.    ca
b4ec0 73 65 20 27 5b 27 3a 20 20 20 71 75 6f 74 65 20  se '[':   quote 
b4ed0 3d 20 27 5d 27 3b 20 20 62 72 65 61 6b 3b 20 20  = ']';  break;  
b4ee0 2f 2a 20 46 6f 72 20 4d 53 20 53 71 6c 53 65 72  /* For MS SqlSer
b4ef0 76 65 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  ver compatibilit
b4f00 79 20 2a 2f 0a 20 20 20 20 64 65 66 61 75 6c 74  y */.    default
b4f10 3a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  :    return;.  }
b4f20 0a 20 20 66 6f 72 28 69 3d 31 2c 20 6a 3d 30 3b  .  for(i=1, j=0;
b4f30 20 7a 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20   z[i]; i++){.   
b4f40 20 69 66 28 20 7a 5b 69 5d 3d 3d 71 75 6f 74 65   if( z[i]==quote
b4f50 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b   ){.      if( z[
b4f60 69 2b 31 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20  i+1]==quote ){. 
b4f70 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20         z[j++] = 
b4f80 71 75 6f 74 65 3b 0a 20 20 20 20 20 20 20 20 69  quote;.        i
b4f90 2b 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ++;.      }else{
b4fa0 0a 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20  .        z[j++] 
b4fb0 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65  = 0;.        bre
b4fc0 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
b4fd0 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 7a 5b 6a  }else{.      z[j
b4fe0 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20 20 20  ++] = z[i];.    
b4ff0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 43 6f 6e 76  }.  }.}../* Conv
b5000 65 6e 69 65 6e 74 20 73 68 6f 72 74 2d 68 61 6e  enient short-han
b5010 64 20 2a 2f 0a 23 64 65 66 69 6e 65 20 55 70 70  d */.#define Upp
b5020 65 72 54 6f 4c 6f 77 65 72 20 73 71 6c 69 74 65  erToLower sqlite
b5030 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 0a 0a 2f  3UpperToLower../
b5040 2a 0a 2a 2a 20 53 6f 6d 65 20 73 79 73 74 65 6d  *.** Some system
b5050 73 20 68 61 76 65 20 73 74 72 69 63 6d 70 28 29  s have stricmp()
b5060 2e 20 20 4f 74 68 65 72 73 20 68 61 76 65 20 73  .  Others have s
b5070 74 72 63 61 73 65 63 6d 70 28 29 2e 20 20 42 65  trcasecmp().  Be
b5080 63 61 75 73 65 0a 2a 2a 20 74 68 65 72 65 20 69  cause.** there i
b5090 73 20 6e 6f 20 63 6f 6e 73 69 73 74 65 6e 63 79  s no consistency
b50a0 2c 20 77 65 20 77 69 6c 6c 20 64 65 66 69 6e 65  , we will define
b50b0 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f 0a 53 51 4c   our own..*/.SQL
b50c0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
b50d0 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 63  sqlite3StrICmp(c
b50e0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74  onst char *zLeft
b50f0 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52  , const char *zR
b5100 69 67 68 74 29 7b 0a 20 20 72 65 67 69 73 74 65  ight){.  registe
b5110 72 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  r unsigned char 
b5120 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d 20 28 75  *a, *b;.  a = (u
b5130 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a  nsigned char *)z
b5140 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28 75 6e 73  Left;.  b = (uns
b5150 69 67 6e 65 64 20 63 68 61 72 20 2a 29 7a 52 69  igned char *)zRi
b5160 67 68 74 3b 0a 20 20 77 68 69 6c 65 28 20 2a 61  ght;.  while( *a
b5170 21 3d 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f  !=0 && UpperToLo
b5180 77 65 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f  wer[*a]==UpperTo
b5190 4c 6f 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b  Lower[*b]){ a++;
b51a0 20 62 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e   b++; }.  return
b51b0 20 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 2a 61   UpperToLower[*a
b51c0 5d 20 2d 20 55 70 70 65 72 54 6f 4c 6f 77 65 72  ] - UpperToLower
b51d0 5b 2a 62 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  [*b];.}.SQLITE_P
b51e0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
b51f0 65 33 53 74 72 4e 49 43 6d 70 28 63 6f 6e 73 74  e3StrNICmp(const
b5200 20 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f   char *zLeft, co
b5210 6e 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74  nst char *zRight
b5220 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 67 69  , int N){.  regi
b5230 73 74 65 72 20 75 6e 73 69 67 6e 65 64 20 63 68  ster unsigned ch
b5240 61 72 20 2a 61 2c 20 2a 62 3b 0a 20 20 61 20 3d  ar *a, *b;.  a =
b5250 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
b5260 2a 29 7a 4c 65 66 74 3b 0a 20 20 62 20 3d 20 28  *)zLeft;.  b = (
b5270 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
b5280 7a 52 69 67 68 74 3b 0a 20 20 77 68 69 6c 65 28  zRight;.  while(
b5290 20 4e 2d 2d 20 3e 20 30 20 26 26 20 2a 61 21 3d   N-- > 0 && *a!=
b52a0 30 20 26 26 20 55 70 70 65 72 54 6f 4c 6f 77 65  0 && UpperToLowe
b52b0 72 5b 2a 61 5d 3d 3d 55 70 70 65 72 54 6f 4c 6f  r[*a]==UpperToLo
b52c0 77 65 72 5b 2a 62 5d 29 7b 20 61 2b 2b 3b 20 62  wer[*b]){ a++; b
b52d0 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 4e  ++; }.  return N
b52e0 3c 30 20 3f 20 30 20 3a 20 55 70 70 65 72 54 6f  <0 ? 0 : UpperTo
b52f0 4c 6f 77 65 72 5b 2a 61 5d 20 2d 20 55 70 70 65  Lower[*a] - Uppe
b5300 72 54 6f 4c 6f 77 65 72 5b 2a 62 5d 3b 0a 7d 0a  rToLower[*b];.}.
b5310 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52  ./*.** Return TR
b5320 55 45 20 69 66 20 7a 20 69 73 20 61 20 70 75 72  UE if z is a pur
b5330 65 20 6e 75 6d 65 72 69 63 20 73 74 72 69 6e 67  e numeric string
b5340 2e 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20  .  Return FALSE 
b5350 69 66 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67  if the.** string
b5360 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 63 68   contains any ch
b5370 61 72 61 63 74 65 72 20 77 68 69 63 68 20 69 73  aracter which is
b5380 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61 20 6e   not part of a n
b5390 75 6d 62 65 72 2e 20 49 66 0a 2a 2a 20 74 68 65  umber. If.** the
b53a0 20 73 74 72 69 6e 67 20 69 73 20 6e 75 6d 65 72   string is numer
b53b0 69 63 20 61 6e 64 20 63 6f 6e 74 61 69 6e 73 20  ic and contains 
b53c0 74 68 65 20 27 2e 27 20 63 68 61 72 61 63 74 65  the '.' characte
b53d0 72 2c 20 73 65 74 20 2a 72 65 61 6c 6e 75 6d 0a  r, set *realnum.
b53e0 2a 2a 20 74 6f 20 54 52 55 45 20 28 6f 74 68 65  ** to TRUE (othe
b53f0 72 77 69 73 65 20 46 41 4c 53 45 29 2e 0a 2a 2a  rwise FALSE)..**
b5400 0a 2a 2a 20 41 6e 20 65 6d 70 74 79 20 73 74 72  .** An empty str
b5410 69 6e 67 20 69 73 20 63 6f 6e 73 69 64 65 72 65  ing is considere
b5420 64 20 6e 6f 6e 2d 6e 75 6d 65 72 69 63 2e 0a 2a  d non-numeric..*
b5430 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b5440 20 69 6e 74 20 73 71 6c 69 74 65 33 49 73 4e 75   int sqlite3IsNu
b5450 6d 62 65 72 28 63 6f 6e 73 74 20 63 68 61 72 20  mber(const char 
b5460 2a 7a 2c 20 69 6e 74 20 2a 72 65 61 6c 6e 75 6d  *z, int *realnum
b5470 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 69 6e 74  , u8 enc){.  int
b5480 20 69 6e 63 72 20 3d 20 28 65 6e 63 3d 3d 53 51   incr = (enc==SQ
b5490 4c 49 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a  LITE_UTF8?1:2);.
b54a0 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49 54    if( enc==SQLIT
b54b0 45 5f 55 54 46 31 36 42 45 20 29 20 7a 2b 2b 3b  E_UTF16BE ) z++;
b54c0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 7c  .  if( *z=='-' |
b54d0 7c 20 2a 7a 3d 3d 27 2b 27 20 29 20 7a 20 2b 3d  | *z=='+' ) z +=
b54e0 20 69 6e 63 72 3b 0a 20 20 69 66 28 20 21 69 73   incr;.  if( !is
b54f0 64 69 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29  digit(*(u8*)z) )
b5500 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
b5510 20 20 7d 0a 20 20 7a 20 2b 3d 20 69 6e 63 72 3b    }.  z += incr;
b5520 0a 20 20 69 66 28 20 72 65 61 6c 6e 75 6d 20 29  .  if( realnum )
b5530 20 2a 72 65 61 6c 6e 75 6d 20 3d 20 30 3b 0a 20   *realnum = 0;. 
b5540 20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28   while( isdigit(
b5550 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20 2b 3d  *(u8*)z) ){ z +=
b5560 20 69 6e 63 72 3b 20 7d 0a 20 20 69 66 28 20 2a   incr; }.  if( *
b5570 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20 7a 20  z=='.' ){.    z 
b5580 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66 28  += incr;.    if(
b5590 20 21 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29   !isdigit(*(u8*)
b55a0 7a 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  z) ) return 0;. 
b55b0 20 20 20 77 68 69 6c 65 28 20 69 73 64 69 67 69     while( isdigi
b55c0 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 20 7a 20  t(*(u8*)z) ){ z 
b55d0 2b 3d 20 69 6e 63 72 3b 20 7d 0a 20 20 20 20 69  += incr; }.    i
b55e0 66 28 20 72 65 61 6c 6e 75 6d 20 29 20 2a 72 65  f( realnum ) *re
b55f0 61 6c 6e 75 6d 20 3d 20 31 3b 0a 20 20 7d 0a 20  alnum = 1;.  }. 
b5600 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c 7c 20   if( *z=='e' || 
b5610 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20 20 7a  *z=='E' ){.    z
b5620 20 2b 3d 20 69 6e 63 72 3b 0a 20 20 20 20 69 66   += incr;.    if
b5630 28 20 2a 7a 3d 3d 27 2b 27 20 7c 7c 20 2a 7a 3d  ( *z=='+' || *z=
b5640 3d 27 2d 27 20 29 20 7a 20 2b 3d 20 69 6e 63 72  ='-' ) z += incr
b5650 3b 0a 20 20 20 20 69 66 28 20 21 69 73 64 69 67  ;.    if( !isdig
b5660 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 20 72 65  it(*(u8*)z) ) re
b5670 74 75 72 6e 20 30 3b 0a 20 20 20 20 77 68 69 6c  turn 0;.    whil
b5680 65 28 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a  e( isdigit(*(u8*
b5690 29 7a 29 20 29 7b 20 7a 20 2b 3d 20 69 6e 63 72  )z) ){ z += incr
b56a0 3b 20 7d 0a 20 20 20 20 69 66 28 20 72 65 61 6c  ; }.    if( real
b56b0 6e 75 6d 20 29 20 2a 72 65 61 6c 6e 75 6d 20 3d  num ) *realnum =
b56c0 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
b56d0 20 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   *z==0;.}../*.**
b56e0 20 54 68 65 20 73 74 72 69 6e 67 20 7a 5b 5d 20   The string z[] 
b56f0 69 73 20 61 6e 20 61 73 63 69 69 20 72 65 70 72  is an ascii repr
b5700 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20  esentation of a 
b5710 72 65 61 6c 20 6e 75 6d 62 65 72 2e 0a 2a 2a 20  real number..** 
b5720 43 6f 6e 76 65 72 74 20 74 68 69 73 20 73 74 72  Convert this str
b5730 69 6e 67 20 74 6f 20 61 20 64 6f 75 62 6c 65 2e  ing to a double.
b5740 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
b5750 69 6e 65 20 61 73 73 75 6d 65 73 20 74 68 61 74  ine assumes that
b5760 20 7a 5b 5d 20 72 65 61 6c 6c 79 20 69 73 20 61   z[] really is a
b5770 20 76 61 6c 69 64 20 6e 75 6d 62 65 72 2e 20 20   valid number.  
b5780 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c  If it.** is not,
b5790 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 75   the result is u
b57a0 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  ndefined..**.** 
b57b0 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
b57c0 75 73 65 64 20 69 6e 73 74 65 61 64 20 6f 66 20  used instead of 
b57d0 74 68 65 20 6c 69 62 72 61 72 79 20 61 74 6f 66  the library atof
b57e0 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65 63 61  () function beca
b57f0 75 73 65 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61  use.** the libra
b5800 72 79 20 61 74 6f 66 28 29 20 6d 69 67 68 74 20  ry atof() might 
b5810 77 61 6e 74 20 74 6f 20 75 73 65 20 22 2c 22 20  want to use "," 
b5820 61 73 20 74 68 65 20 64 65 63 69 6d 61 6c 20 70  as the decimal p
b5830 6f 69 6e 74 20 69 6e 73 74 65 61 64 0a 2a 2a 20  oint instead.** 
b5840 6f 66 20 22 2e 22 20 64 65 70 65 6e 64 69 6e 67  of "." depending
b5850 20 6f 6e 20 68 6f 77 20 6c 6f 63 61 6c 65 20 69   on how locale i
b5860 73 20 73 65 74 2e 20 20 42 75 74 20 74 68 61 74  s set.  But that
b5870 20 77 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f   would cause pro
b5880 62 6c 65 6d 73 0a 2a 2a 20 66 6f 72 20 53 51 4c  blems.** for SQL
b5890 2e 20 20 53 6f 20 74 68 69 73 20 72 6f 75 74 69  .  So this routi
b58a0 6e 65 20 61 6c 77 61 79 73 20 75 73 65 73 20 22  ne always uses "
b58b0 2e 22 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66  ." regardless of
b58c0 20 6c 6f 63 61 6c 65 2e 0a 2a 2f 0a 53 51 4c 49   locale..*/.SQLI
b58d0 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
b58e0 71 6c 69 74 65 33 41 74 6f 46 28 63 6f 6e 73 74  qlite3AtoF(const
b58f0 20 63 68 61 72 20 2a 7a 2c 20 64 6f 75 62 6c 65   char *z, double
b5900 20 2a 70 52 65 73 75 6c 74 29 7b 0a 23 69 66 6e   *pResult){.#ifn
b5910 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
b5920 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20  FLOATING_POINT. 
b5930 20 69 6e 74 20 73 69 67 6e 20 3d 20 31 3b 0a 20   int sign = 1;. 
b5940 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 42 65   const char *zBe
b5950 67 69 6e 20 3d 20 7a 3b 0a 20 20 4c 4f 4e 47 44  gin = z;.  LONGD
b5960 4f 55 42 4c 45 5f 54 59 50 45 20 76 31 20 3d 20  OUBLE_TYPE v1 = 
b5970 30 2e 30 3b 0a 20 20 69 6e 74 20 6e 53 69 67 6e  0.0;.  int nSign
b5980 69 66 69 63 61 6e 74 20 3d 20 30 3b 0a 20 20 77  ificant = 0;.  w
b5990 68 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28  hile( isspace(*(
b59a0 75 38 2a 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20  u8*)z) ) z++;.  
b59b0 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20  if( *z=='-' ){. 
b59c0 20 20 20 73 69 67 6e 20 3d 20 2d 31 3b 0a 20 20     sign = -1;.  
b59d0 20 20 7a 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20 69    z++;.  }else i
b59e0 66 28 20 2a 7a 3d 3d 27 2b 27 20 29 7b 0a 20 20  f( *z=='+' ){.  
b59f0 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 77 68 69    z++;.  }.  whi
b5a00 6c 65 28 20 7a 5b 30 5d 3d 3d 27 30 27 20 29 7b  le( z[0]=='0' ){
b5a10 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20  .    z++;.  }.  
b5a20 77 68 69 6c 65 28 20 69 73 64 69 67 69 74 28 2a  while( isdigit(*
b5a30 28 75 38 2a 29 7a 29 20 29 7b 0a 20 20 20 20 76  (u8*)z) ){.    v
b5a40 31 20 3d 20 76 31 2a 31 30 2e 30 20 2b 20 28 2a  1 = v1*10.0 + (*
b5a50 7a 20 2d 20 27 30 27 29 3b 0a 20 20 20 20 7a 2b  z - '0');.    z+
b5a60 2b 3b 0a 20 20 20 20 6e 53 69 67 6e 69 66 69 63  +;.    nSignific
b5a70 61 6e 74 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28  ant++;.  }.  if(
b5a80 20 2a 7a 3d 3d 27 2e 27 20 29 7b 0a 20 20 20 20   *z=='.' ){.    
b5a90 4c 4f 4e 47 44 4f 55 42 4c 45 5f 54 59 50 45 20  LONGDOUBLE_TYPE 
b5aa0 64 69 76 69 73 6f 72 20 3d 20 31 2e 30 3b 0a 20  divisor = 1.0;. 
b5ab0 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 69 66 28 20     z++;.    if( 
b5ac0 6e 53 69 67 6e 69 66 69 63 61 6e 74 3d 3d 30 20  nSignificant==0 
b5ad0 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20  ){.      while( 
b5ae0 7a 5b 30 5d 3d 3d 27 30 27 20 29 7b 0a 20 20 20  z[0]=='0' ){.   
b5af0 20 20 20 20 20 64 69 76 69 73 6f 72 20 2a 3d 20       divisor *= 
b5b00 31 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 7a 2b  10.0;.        z+
b5b10 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  +;.      }.    }
b5b20 0a 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69  .    while( isdi
b5b30 67 69 74 28 2a 28 75 38 2a 29 7a 29 20 29 7b 0a  git(*(u8*)z) ){.
b5b40 20 20 20 20 20 20 69 66 28 20 6e 53 69 67 6e 69        if( nSigni
b5b50 66 69 63 61 6e 74 3c 31 38 20 29 7b 0a 20 20 20  ficant<18 ){.   
b5b60 20 20 20 20 20 76 31 20 3d 20 76 31 2a 31 30 2e       v1 = v1*10.
b5b70 30 20 2b 20 28 2a 7a 20 2d 20 27 30 27 29 3b 0a  0 + (*z - '0');.
b5b80 20 20 20 20 20 20 20 20 64 69 76 69 73 6f 72 20          divisor 
b5b90 2a 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 20  *= 10.0;.       
b5ba0 20 6e 53 69 67 6e 69 66 69 63 61 6e 74 2b 2b 3b   nSignificant++;
b5bb0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7a  .      }.      z
b5bc0 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 76 31  ++;.    }.    v1
b5bd0 20 2f 3d 20 64 69 76 69 73 6f 72 3b 0a 20 20 7d   /= divisor;.  }
b5be0 0a 20 20 69 66 28 20 2a 7a 3d 3d 27 65 27 20 7c  .  if( *z=='e' |
b5bf0 7c 20 2a 7a 3d 3d 27 45 27 20 29 7b 0a 20 20 20  | *z=='E' ){.   
b5c00 20 69 6e 74 20 65 73 69 67 6e 20 3d 20 31 3b 0a   int esign = 1;.
b5c10 20 20 20 20 69 6e 74 20 65 76 61 6c 20 3d 20 30      int eval = 0
b5c20 3b 0a 20 20 20 20 4c 4f 4e 47 44 4f 55 42 4c 45  ;.    LONGDOUBLE
b5c30 5f 54 59 50 45 20 73 63 61 6c 65 20 3d 20 31 2e  _TYPE scale = 1.
b5c40 30 3b 0a 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  0;.    z++;.    
b5c50 69 66 28 20 2a 7a 3d 3d 27 2d 27 20 29 7b 0a 20  if( *z=='-' ){. 
b5c60 20 20 20 20 20 65 73 69 67 6e 20 3d 20 2d 31 3b       esign = -1;
b5c70 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
b5c80 7d 65 6c 73 65 20 69 66 28 20 2a 7a 3d 3d 27 2b  }else if( *z=='+
b5c90 27 20 29 7b 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a  ' ){.      z++;.
b5ca0 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65 28      }.    while(
b5cb0 20 69 73 64 69 67 69 74 28 2a 28 75 38 2a 29 7a   isdigit(*(u8*)z
b5cc0 29 20 29 7b 0a 20 20 20 20 20 20 65 76 61 6c 20  ) ){.      eval 
b5cd0 3d 20 65 76 61 6c 2a 31 30 20 2b 20 2a 7a 20 2d  = eval*10 + *z -
b5ce0 20 27 30 27 3b 0a 20 20 20 20 20 20 7a 2b 2b 3b   '0';.      z++;
b5cf0 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65  .    }.    while
b5d00 28 20 65 76 61 6c 3e 3d 36 34 20 29 7b 20 73 63  ( eval>=64 ){ sc
b5d10 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 36 34 3b 20  ale *= 1.0e+64; 
b5d20 65 76 61 6c 20 2d 3d 20 36 34 3b 20 7d 0a 20 20  eval -= 64; }.  
b5d30 20 20 77 68 69 6c 65 28 20 65 76 61 6c 3e 3d 31    while( eval>=1
b5d40 36 20 29 7b 20 73 63 61 6c 65 20 2a 3d 20 31 2e  6 ){ scale *= 1.
b5d50 30 65 2b 31 36 3b 20 65 76 61 6c 20 2d 3d 20 31  0e+16; eval -= 1
b5d60 36 3b 20 7d 0a 20 20 20 20 77 68 69 6c 65 28 20  6; }.    while( 
b5d70 65 76 61 6c 3e 3d 34 20 29 7b 20 73 63 61 6c 65  eval>=4 ){ scale
b5d80 20 2a 3d 20 31 2e 30 65 2b 34 3b 20 65 76 61 6c   *= 1.0e+4; eval
b5d90 20 2d 3d 20 34 3b 20 7d 0a 20 20 20 20 77 68 69   -= 4; }.    whi
b5da0 6c 65 28 20 65 76 61 6c 3e 3d 31 20 29 7b 20 73  le( eval>=1 ){ s
b5db0 63 61 6c 65 20 2a 3d 20 31 2e 30 65 2b 31 3b 20  cale *= 1.0e+1; 
b5dc0 65 76 61 6c 20 2d 3d 20 31 3b 20 7d 0a 20 20 20  eval -= 1; }.   
b5dd0 20 69 66 28 20 65 73 69 67 6e 3c 30 20 29 7b 0a   if( esign<0 ){.
b5de0 20 20 20 20 20 20 76 31 20 2f 3d 20 73 63 61 6c        v1 /= scal
b5df0 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  e;.    }else{.  
b5e00 20 20 20 20 76 31 20 2a 3d 20 73 63 61 6c 65 3b      v1 *= scale;
b5e10 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70 52  .    }.  }.  *pR
b5e20 65 73 75 6c 74 20 3d 20 73 69 67 6e 3c 30 20 3f  esult = sign<0 ?
b5e30 20 2d 76 31 20 3a 20 76 31 3b 0a 20 20 72 65 74   -v1 : v1;.  ret
b5e40 75 72 6e 20 7a 20 2d 20 7a 42 65 67 69 6e 3b 0a  urn z - zBegin;.
b5e50 23 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 73  #else.  return s
b5e60 71 6c 69 74 65 33 41 74 6f 69 36 34 28 7a 2c 20  qlite3Atoi64(z, 
b5e70 70 52 65 73 75 6c 74 29 3b 0a 23 65 6e 64 69 66  pResult);.#endif
b5e80 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
b5e90 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 20 2a  FLOATING_POINT *
b5ea0 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 61  /.}../*.** Compa
b5eb0 72 65 20 74 68 65 20 31 39 2d 63 68 61 72 61 63  re the 19-charac
b5ec0 74 65 72 20 73 74 72 69 6e 67 20 7a 4e 75 6d 20  ter string zNum 
b5ed0 61 67 61 69 6e 73 74 20 74 68 65 20 74 65 78 74  against the text
b5ee0 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 0a   representation.
b5ef0 2a 2a 20 76 61 6c 75 65 20 32 5e 36 33 3a 20 20  ** value 2^63:  
b5f00 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35  9223372036854775
b5f10 38 30 38 2e 20 20 52 65 74 75 72 6e 20 6e 65 67  808.  Return neg
b5f20 61 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20  ative, zero, or 
b5f30 70 6f 73 69 74 69 76 65 0a 2a 2a 20 69 66 20 7a  positive.** if z
b5f40 4e 75 6d 20 69 73 20 6c 65 73 73 20 74 68 61 6e  Num is less than
b5f50 2c 20 65 71 75 61 6c 20 74 6f 2c 20 6f 72 20 67  , equal to, or g
b5f60 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
b5f70 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 55 6e  string..**.** Un
b5f80 6c 69 6b 65 20 6d 65 6d 63 6d 70 28 29 20 74 68  like memcmp() th
b5f90 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 67 75  is routine is gu
b5fa0 61 72 61 6e 74 65 65 64 20 74 6f 20 72 65 74 75  aranteed to retu
b5fb0 72 6e 20 74 68 65 20 64 69 66 66 65 72 65 6e 63  rn the differenc
b5fc0 65 0a 2a 2a 20 69 6e 20 74 68 65 20 76 61 6c 75  e.** in the valu
b5fd0 65 73 20 6f 66 20 74 68 65 20 6c 61 73 74 20 64  es of the last d
b5fe0 69 67 69 74 20 69 66 20 74 68 65 20 6f 6e 6c 79  igit if the only
b5ff0 20 64 69 66 66 65 72 65 6e 63 65 20 69 73 20 69   difference is i
b6000 6e 20 74 68 65 0a 2a 2a 20 6c 61 73 74 20 64 69  n the.** last di
b6010 67 69 74 2e 20 20 53 6f 2c 20 66 6f 72 20 65 78  git.  So, for ex
b6020 61 6d 70 6c 65 2c 0a 2a 2a 0a 2a 2a 20 20 20 20  ample,.**.**    
b6030 20 20 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28    compare2pow63(
b6040 22 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37  "922337203685477
b6050 35 38 30 30 22 29 0a 2a 2a 0a 2a 2a 20 77 69 6c  5800").**.** wil
b6060 6c 20 72 65 74 75 72 6e 20 2d 38 2e 0a 2a 2f 0a  l return -8..*/.
b6070 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6d 70 61  static int compa
b6080 72 65 32 70 6f 77 36 33 28 63 6f 6e 73 74 20 63  re2pow63(const c
b6090 68 61 72 20 2a 7a 4e 75 6d 29 7b 0a 20 20 69 6e  har *zNum){.  in
b60a0 74 20 63 3b 0a 20 20 63 20 3d 20 6d 65 6d 63 6d  t c;.  c = memcm
b60b0 70 28 7a 4e 75 6d 2c 22 39 32 32 33 33 37 32 30  p(zNum,"92233720
b60c0 33 36 38 35 34 37 37 35 38 30 22 2c 31 38 29 3b  3685477580",18);
b60d0 0a 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20  .  if( c==0 ){. 
b60e0 20 20 20 63 20 3d 20 7a 4e 75 6d 5b 31 38 5d 20     c = zNum[18] 
b60f0 2d 20 27 38 27 3b 0a 20 20 7d 0a 20 20 72 65 74  - '8';.  }.  ret
b6100 75 72 6e 20 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn c;.}.../*.**
b6110 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
b6120 7a 4e 75 6d 20 69 73 20 61 20 36 34 2d 62 69 74  zNum is a 64-bit
b6130 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20   signed integer 
b6140 61 6e 64 20 77 72 69 74 65 0a 2a 2a 20 74 68 65  and write.** the
b6150 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 69 6e   value of the in
b6160 74 65 67 65 72 20 69 6e 74 6f 20 2a 70 4e 75 6d  teger into *pNum
b6170 2e 20 20 49 66 20 7a 4e 75 6d 20 69 73 20 6e 6f  .  If zNum is no
b6180 74 20 61 6e 20 69 6e 74 65 67 65 72 0a 2a 2a 20  t an integer.** 
b6190 6f 72 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72  or is an integer
b61a0 20 74 68 61 74 20 69 73 20 74 6f 6f 20 6c 61 72   that is too lar
b61b0 67 65 20 74 6f 20 62 65 20 65 78 70 72 65 73 73  ge to be express
b61c0 65 64 20 77 69 74 68 20 36 34 20 62 69 74 73 2c  ed with 64 bits,
b61d0 0a 2a 2a 20 74 68 65 6e 20 72 65 74 75 72 6e 20  .** then return 
b61e0 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  false..**.** Whe
b61f0 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77  n this routine w
b6200 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 20 77 72  as originally wr
b6210 69 74 74 65 6e 20 69 74 20 64 65 61 6c 74 20 77  itten it dealt w
b6220 69 74 68 20 6f 6e 6c 79 0a 2a 2a 20 33 32 2d 62  ith only.** 32-b
b6230 69 74 20 6e 75 6d 62 65 72 73 2e 20 20 41 74 20  it numbers.  At 
b6240 74 68 61 74 20 74 69 6d 65 2c 20 69 74 20 77 61  that time, it wa
b6250 73 20 6d 75 63 68 20 66 61 73 74 65 72 20 74 68  s much faster th
b6260 61 6e 20 74 68 65 0a 2a 2a 20 61 74 6f 69 28 29  an the.** atoi()
b6270 20 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65   library routine
b6280 20 69 6e 20 52 65 64 48 61 74 20 37 2e 32 2e 0a   in RedHat 7.2..
b6290 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b62a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 41 74 6f  E int sqlite3Ato
b62b0 69 36 34 28 63 6f 6e 73 74 20 63 68 61 72 20 2a  i64(const char *
b62c0 7a 4e 75 6d 2c 20 69 36 34 20 2a 70 4e 75 6d 29  zNum, i64 *pNum)
b62d0 7b 0a 20 20 69 36 34 20 76 20 3d 20 30 3b 0a 20  {.  i64 v = 0;. 
b62e0 20 69 6e 74 20 6e 65 67 3b 0a 20 20 69 6e 74 20   int neg;.  int 
b62f0 69 2c 20 63 3b 0a 20 20 63 6f 6e 73 74 20 63 68  i, c;.  const ch
b6300 61 72 20 2a 7a 53 74 61 72 74 3b 0a 20 20 77 68  ar *zStart;.  wh
b6310 69 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75  ile( isspace(*(u
b6320 38 2a 29 7a 4e 75 6d 29 20 29 20 7a 4e 75 6d 2b  8*)zNum) ) zNum+
b6330 2b 3b 0a 20 20 69 66 28 20 2a 7a 4e 75 6d 3d 3d  +;.  if( *zNum==
b6340 27 2d 27 20 29 7b 0a 20 20 20 20 6e 65 67 20 3d  '-' ){.    neg =
b6350 20 31 3b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   1;.    zNum++;.
b6360 20 20 7d 65 6c 73 65 20 69 66 28 20 2a 7a 4e 75    }else if( *zNu
b6370 6d 3d 3d 27 2b 27 20 29 7b 0a 20 20 20 20 6e 65  m=='+' ){.    ne
b6380 67 20 3d 20 30 3b 0a 20 20 20 20 7a 4e 75 6d 2b  g = 0;.    zNum+
b6390 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  +;.  }else{.    
b63a0 6e 65 67 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 7a  neg = 0;.  }.  z
b63b0 53 74 61 72 74 20 3d 20 7a 4e 75 6d 3b 0a 20 20  Start = zNum;.  
b63c0 77 68 69 6c 65 28 20 7a 4e 75 6d 5b 30 5d 3d 3d  while( zNum[0]==
b63d0 27 30 27 20 29 7b 20 7a 4e 75 6d 2b 2b 3b 20 7d  '0' ){ zNum++; }
b63e0 20 2f 2a 20 53 6b 69 70 20 6f 76 65 72 20 6c 65   /* Skip over le
b63f0 61 64 69 6e 67 20 7a 65 72 6f 73 2e 20 54 69 63  ading zeros. Tic
b6400 6b 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 66  ket #2454 */.  f
b6410 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e 75 6d 5b  or(i=0; (c=zNum[
b6420 69 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d 27  i])>='0' && c<='
b6430 39 27 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 76 20  9'; i++){.    v 
b6440 3d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27  = v*10 + c - '0'
b6450 3b 0a 20 20 7d 0a 20 20 2a 70 4e 75 6d 20 3d 20  ;.  }.  *pNum = 
b6460 6e 65 67 20 3f 20 2d 76 20 3a 20 76 3b 0a 20 20  neg ? -v : v;.  
b6470 69 66 28 20 63 21 3d 30 20 7c 7c 20 28 69 3d 3d  if( c!=0 || (i==
b6480 30 20 26 26 20 7a 53 74 61 72 74 3d 3d 7a 4e 75  0 && zStart==zNu
b6490 6d 29 20 7c 7c 20 69 3e 31 39 20 29 7b 0a 20 20  m) || i>19 ){.  
b64a0 20 20 2f 2a 20 7a 4e 75 6d 20 69 73 20 65 6d 70    /* zNum is emp
b64b0 74 79 20 6f 72 20 63 6f 6e 74 61 69 6e 73 20 6e  ty or contains n
b64c0 6f 6e 2d 6e 75 6d 65 72 69 63 20 74 65 78 74 20  on-numeric text 
b64d0 6f 72 20 69 73 20 6c 6f 6e 67 65 72 0a 20 20 20  or is longer.   
b64e0 20 2a 2a 20 74 68 61 6e 20 31 39 20 64 69 67 69   ** than 19 digi
b64f0 74 73 20 28 74 68 75 73 20 67 75 61 72 61 6e 74  ts (thus guarant
b6500 69 6e 67 20 74 68 61 74 20 69 74 20 69 73 20 74  ing that it is t
b6510 6f 6f 20 6c 61 72 67 65 29 20 2a 2f 0a 20 20 20  oo large) */.   
b6520 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c   return 0;.  }el
b6530 73 65 20 69 66 28 20 69 3c 31 39 20 29 7b 0a 20  se if( i<19 ){. 
b6540 20 20 20 2f 2a 20 4c 65 73 73 20 74 68 61 6e 20     /* Less than 
b6550 31 39 20 64 69 67 69 74 73 2c 20 73 6f 20 77 65  19 digits, so we
b6560 20 6b 6e 6f 77 20 74 68 61 74 20 69 74 20 66 69   know that it fi
b6570 74 73 20 69 6e 20 36 34 20 62 69 74 73 20 2a 2f  ts in 64 bits */
b6580 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
b6590 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 31   }else{.    /* 1
b65a0 39 2d 64 69 67 69 74 20 6e 75 6d 62 65 72 73 20  9-digit numbers 
b65b0 6d 75 73 74 20 62 65 20 6e 6f 20 6c 61 72 67 65  must be no large
b65c0 72 20 74 68 61 6e 20 39 32 32 33 33 37 32 30 33  r than 922337203
b65d0 36 38 35 34 37 37 35 38 30 37 20 69 66 20 70 6f  6854775807 if po
b65e0 73 69 74 69 76 65 0a 20 20 20 20 2a 2a 20 6f 72  sitive.    ** or
b65f0 20 39 32 32 33 33 37 32 30 33 36 38 35 34 37 37   922337203685477
b6600 35 38 30 38 20 69 66 20 6e 65 67 61 74 69 76 65  5808 if negative
b6610 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 39 32 32  .  Note that 922
b6620 33 33 37 32 30 33 36 38 35 34 36 36 35 38 30 38  3372036854665808
b6630 0a 20 20 20 20 2a 2a 20 69 73 20 32 5e 36 33 2e  .    ** is 2^63.
b6640 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 63   */.    return c
b6650 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e 75  ompare2pow63(zNu
b6660 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f  m)<neg;.  }.}../
b6670 2a 0a 2a 2a 20 54 68 65 20 73 74 72 69 6e 67 20  *.** The string 
b6680 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20  zNum represents 
b6690 61 6e 20 69 6e 74 65 67 65 72 2e 20 20 54 68 65  an integer.  The
b66a0 72 65 20 6d 69 67 68 74 20 62 65 20 73 6f 6d 65  re might be some
b66b0 20 6f 74 68 65 72 0a 2a 2a 20 69 6e 66 6f 72 6d   other.** inform
b66c0 61 74 69 6f 6e 20 66 6f 6c 6c 6f 77 69 6e 67 20  ation following 
b66d0 74 68 65 20 69 6e 74 65 67 65 72 20 74 6f 6f 2c  the integer too,
b66e0 20 62 75 74 20 74 68 61 74 20 70 61 72 74 20 69   but that part i
b66f0 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 20 49 66  s ignored..** If
b6700 20 74 68 65 20 69 6e 74 65 67 65 72 20 74 68 61   the integer tha
b6710 74 20 74 68 65 20 70 72 65 66 69 78 20 6f 66 20  t the prefix of 
b6720 7a 4e 75 6d 20 72 65 70 72 65 73 65 6e 74 73 20  zNum represents 
b6730 77 69 6c 6c 20 66 69 74 20 69 6e 20 61 0a 2a 2a  will fit in a.**
b6740 20 36 34 2d 62 69 74 20 73 69 67 6e 65 64 20 69   64-bit signed i
b6750 6e 74 65 67 65 72 2c 20 72 65 74 75 72 6e 20 54  nteger, return T
b6760 52 55 45 2e 20 20 4f 74 68 65 72 77 69 73 65 20  RUE.  Otherwise 
b6770 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a 2a  return FALSE..**
b6780 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
b6790 20 72 65 74 75 72 6e 73 20 46 41 4c 53 45 20 66   returns FALSE f
b67a0 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 2d 39  or the string -9
b67b0 32 32 33 33 37 32 30 33 36 38 35 34 37 37 35 38  2233720368547758
b67c0 30 38 20 65 76 65 6e 20 74 68 61 74 0a 2a 2a 20  08 even that.** 
b67d0 74 68 61 74 20 6e 75 6d 62 65 72 20 77 69 6c 6c  that number will
b67e0 2c 20 69 6e 20 74 68 65 6f 72 79 20 66 69 74 20  , in theory fit 
b67f0 69 6e 20 61 20 36 34 2d 62 69 74 20 69 6e 74 65  in a 64-bit inte
b6800 67 65 72 2e 20 20 50 6f 73 69 74 69 76 65 0a 2a  ger.  Positive.*
b6810 2a 20 39 32 32 33 33 37 33 30 33 36 38 35 34 37  * 92233730368547
b6820 37 35 38 30 38 20 77 69 6c 6c 20 6e 6f 74 20 66  75808 will not f
b6830 69 74 20 69 6e 20 36 34 20 62 69 74 73 2e 20 20  it in 64 bits.  
b6840 53 6f 20 69 74 20 73 65 65 6d 73 20 73 61 66 65  So it seems safe
b6850 72 20 74 6f 20 72 65 74 75 72 6e 0a 2a 2a 20 66  r to return.** f
b6860 61 6c 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alse..*/.SQLITE_
b6870 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
b6880 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
b6890 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 75 6d  const char *zNum
b68a0 2c 20 69 6e 74 20 6e 65 67 46 6c 61 67 29 7b 0a  , int negFlag){.
b68b0 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e    int i, c;.  in
b68c0 74 20 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28  t neg = 0;.  if(
b68d0 20 2a 7a 4e 75 6d 3d 3d 27 2d 27 20 29 7b 0a 20   *zNum=='-' ){. 
b68e0 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20     neg = 1;.    
b68f0 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20  zNum++;.  }else 
b6900 69 66 28 20 2a 7a 4e 75 6d 3d 3d 27 2b 27 20 29  if( *zNum=='+' )
b6910 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a 20 20  {.    zNum++;.  
b6920 7d 0a 20 20 69 66 28 20 6e 65 67 46 6c 61 67 20  }.  if( negFlag 
b6930 29 20 6e 65 67 20 3d 20 31 2d 6e 65 67 3b 0a 20  ) neg = 1-neg;. 
b6940 20 77 68 69 6c 65 28 20 2a 7a 4e 75 6d 3d 3d 27   while( *zNum=='
b6950 30 27 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b  0' ){.    zNum++
b6960 3b 20 20 20 2f 2a 20 53 6b 69 70 20 6c 65 61 64  ;   /* Skip lead
b6970 69 6e 67 20 7a 65 72 6f 73 2e 20 20 54 69 63 6b  ing zeros.  Tick
b6980 65 74 20 23 32 34 35 34 20 2a 2f 0a 20 20 7d 0a  et #2454 */.  }.
b6990 20 20 66 6f 72 28 69 3d 30 3b 20 28 63 3d 7a 4e    for(i=0; (c=zN
b69a0 75 6d 5b 69 5d 29 3e 3d 27 30 27 20 26 26 20 63  um[i])>='0' && c
b69b0 3c 3d 27 39 27 3b 20 69 2b 2b 29 7b 7d 0a 20 20  <='9'; i++){}.  
b69c0 69 66 28 20 69 3c 31 39 20 29 7b 0a 20 20 20 20  if( i<19 ){.    
b69d0 2f 2a 20 47 75 61 72 61 6e 74 65 65 64 20 74 6f  /* Guaranteed to
b69e0 20 66 69 74 20 69 66 20 6c 65 73 73 20 74 68 61   fit if less tha
b69f0 6e 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20  n 19 digits */. 
b6a00 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
b6a10 65 6c 73 65 20 69 66 28 20 69 3e 31 39 20 29 7b  else if( i>19 ){
b6a20 0a 20 20 20 20 2f 2a 20 47 75 61 72 61 6e 74 65  .    /* Guarante
b6a30 65 64 20 74 6f 20 62 65 20 74 6f 6f 20 62 69 67  ed to be too big
b6a40 20 69 66 20 67 72 65 61 74 65 72 20 74 68 61 6e   if greater than
b6a50 20 31 39 20 64 69 67 69 74 73 20 2a 2f 0a 20 20   19 digits */.  
b6a60 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
b6a70 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f 6d 70  lse{.    /* Comp
b6a80 61 72 65 20 61 67 61 69 6e 73 74 20 32 5e 36 33  are against 2^63
b6a90 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20  . */.    return 
b6aa0 63 6f 6d 70 61 72 65 32 70 6f 77 36 33 28 7a 4e  compare2pow63(zN
b6ab0 75 6d 29 3c 6e 65 67 3b 0a 20 20 7d 0a 7d 0a 0a  um)<neg;.  }.}..
b6ac0 2f 2a 0a 2a 2a 20 49 66 20 7a 4e 75 6d 20 72 65  /*.** If zNum re
b6ad0 70 72 65 73 65 6e 74 73 20 61 6e 20 69 6e 74 65  presents an inte
b6ae0 67 65 72 20 74 68 61 74 20 77 69 6c 6c 20 66 69  ger that will fi
b6af0 74 20 69 6e 20 33 32 2d 62 69 74 73 2c 20 74 68  t in 32-bits, th
b6b00 65 6e 20 73 65 74 0a 2a 2a 20 2a 70 56 61 6c 75  en set.** *pValu
b6b10 65 20 74 6f 20 74 68 61 74 20 69 6e 74 65 67 65  e to that intege
b6b20 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74 72 75  r and return tru
b6b30 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72 65  e.  Otherwise re
b6b40 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a  turn false..**.*
b6b50 2a 20 41 6e 79 20 6e 6f 6e 2d 6e 75 6d 65 72 69  * Any non-numeri
b6b60 63 20 63 68 61 72 61 63 74 65 72 73 20 74 68 61  c characters tha
b6b70 74 20 66 6f 6c 6c 6f 77 69 6e 67 20 7a 4e 75 6d  t following zNum
b6b80 20 61 72 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a   are ignored..**
b6b90 20 54 68 69 73 20 69 73 20 64 69 66 66 65 72 65   This is differe
b6ba0 6e 74 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 41  nt from sqlite3A
b6bb0 74 6f 69 36 34 28 29 20 77 68 69 63 68 20 72 65  toi64() which re
b6bc0 71 75 69 72 65 73 20 74 68 65 0a 2a 2a 20 69 6e  quires the.** in
b6bd0 70 75 74 20 6e 75 6d 62 65 72 20 74 6f 20 62 65  put number to be
b6be0 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 64   zero-terminated
b6bf0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b6c00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
b6c10 65 74 49 6e 74 33 32 28 63 6f 6e 73 74 20 63 68  etInt32(const ch
b6c20 61 72 20 2a 7a 4e 75 6d 2c 20 69 6e 74 20 2a 70  ar *zNum, int *p
b6c30 56 61 6c 75 65 29 7b 0a 20 20 73 71 6c 69 74 65  Value){.  sqlite
b6c40 5f 69 6e 74 36 34 20 76 20 3d 20 30 3b 0a 20 20  _int64 v = 0;.  
b6c50 69 6e 74 20 69 2c 20 63 3b 0a 20 20 69 6e 74 20  int i, c;.  int 
b6c60 6e 65 67 20 3d 20 30 3b 0a 20 20 69 66 28 20 7a  neg = 0;.  if( z
b6c70 4e 75 6d 5b 30 5d 3d 3d 27 2d 27 20 29 7b 0a 20  Num[0]=='-' ){. 
b6c80 20 20 20 6e 65 67 20 3d 20 31 3b 0a 20 20 20 20     neg = 1;.    
b6c90 7a 4e 75 6d 2b 2b 3b 0a 20 20 7d 65 6c 73 65 20  zNum++;.  }else 
b6ca0 69 66 28 20 7a 4e 75 6d 5b 30 5d 3d 3d 27 2b 27  if( zNum[0]=='+'
b6cb0 20 29 7b 0a 20 20 20 20 7a 4e 75 6d 2b 2b 3b 0a   ){.    zNum++;.
b6cc0 20 20 7d 0a 20 20 77 68 69 6c 65 28 20 7a 4e 75    }.  while( zNu
b6cd0 6d 5b 30 5d 3d 3d 27 30 27 20 29 20 7a 4e 75 6d  m[0]=='0' ) zNum
b6ce0 2b 2b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ++;.  for(i=0; i
b6cf0 3c 31 31 20 26 26 20 28 63 20 3d 20 7a 4e 75 6d  <11 && (c = zNum
b6d00 5b 69 5d 20 2d 20 27 30 27 29 3e 3d 30 20 26 26  [i] - '0')>=0 &&
b6d10 20 63 3c 3d 39 3b 20 69 2b 2b 29 7b 0a 20 20 20   c<=9; i++){.   
b6d20 20 76 20 3d 20 76 2a 31 30 20 2b 20 63 3b 0a 20   v = v*10 + c;. 
b6d30 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 6c 6f 6e   }..  /* The lon
b6d40 67 65 73 74 20 64 65 63 69 6d 61 6c 20 72 65 70  gest decimal rep
b6d50 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61  resentation of a
b6d60 20 33 32 20 62 69 74 20 69 6e 74 65 67 65 72 20   32 bit integer 
b6d70 69 73 20 31 30 20 64 69 67 69 74 73 3a 0a 20 20  is 10 digits:.  
b6d80 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20  **.  **         
b6d90 20 20 20 20 31 32 33 34 35 36 37 38 39 30 0a 20      1234567890. 
b6da0 20 2a 2a 20 20 20 20 20 32 5e 33 31 20 2d 3e 20   **     2^31 -> 
b6db0 32 31 34 37 34 38 33 36 34 38 0a 20 20 2a 2f 0a  2147483648.  */.
b6dc0 20 20 69 66 28 20 69 3e 31 30 20 29 7b 0a 20 20    if( i>10 ){.  
b6dd0 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
b6de0 20 20 69 66 28 20 76 2d 6e 65 67 3e 32 31 34 37    if( v-neg>2147
b6df0 34 38 33 36 34 37 20 29 7b 0a 20 20 20 20 72 65  483647 ){.    re
b6e00 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
b6e10 28 20 6e 65 67 20 29 7b 0a 20 20 20 20 76 20 3d  ( neg ){.    v =
b6e20 20 2d 76 3b 0a 20 20 7d 0a 20 20 2a 70 56 61 6c   -v;.  }.  *pVal
b6e30 75 65 20 3d 20 28 69 6e 74 29 76 3b 0a 20 20 72  ue = (int)v;.  r
b6e40 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 1;.}../*.*
b6e50 2a 20 54 68 65 20 76 61 72 69 61 62 6c 65 2d 6c  * The variable-l
b6e60 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 65 6e  ength integer en
b6e70 63 6f 64 69 6e 67 20 69 73 20 61 73 20 66 6f 6c  coding is as fol
b6e80 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b 45 59 3a  lows:.**.** KEY:
b6e90 0a 2a 2a 20 20 20 20 20 20 20 20 20 41 20 3d 20  .**         A = 
b6ea0 30 78 78 78 78 78 78 78 20 20 20 20 37 20 62 69  0xxxxxxx    7 bi
b6eb0 74 73 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f  ts of data and o
b6ec0 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a 2a 20 20  ne flag bit.**  
b6ed0 20 20 20 20 20 20 20 42 20 3d 20 31 78 78 78 78         B = 1xxxx
b6ee0 78 78 78 20 20 20 20 37 20 62 69 74 73 20 6f 66  xxx    7 bits of
b6ef0 20 64 61 74 61 20 61 6e 64 20 6f 6e 65 20 66 6c   data and one fl
b6f00 61 67 20 62 69 74 0a 2a 2a 20 20 20 20 20 20 20  ag bit.**       
b6f10 20 20 43 20 3d 20 78 78 78 78 78 78 78 78 20 20    C = xxxxxxxx  
b6f20 20 20 38 20 62 69 74 73 20 6f 66 20 64 61 74 61    8 bits of data
b6f30 0a 2a 2a 0a 2a 2a 20 20 37 20 62 69 74 73 20 2d  .**.**  7 bits -
b6f40 20 41 0a 2a 2a 20 31 34 20 62 69 74 73 20 2d 20   A.** 14 bits - 
b6f50 42 41 0a 2a 2a 20 32 31 20 62 69 74 73 20 2d 20  BA.** 21 bits - 
b6f60 42 42 41 0a 2a 2a 20 32 38 20 62 69 74 73 20 2d  BBA.** 28 bits -
b6f70 20 42 42 42 41 0a 2a 2a 20 33 35 20 62 69 74 73   BBBA.** 35 bits
b6f80 20 2d 20 42 42 42 42 41 0a 2a 2a 20 34 32 20 62   - BBBBA.** 42 b
b6f90 69 74 73 20 2d 20 42 42 42 42 42 41 0a 2a 2a 20  its - BBBBBA.** 
b6fa0 34 39 20 62 69 74 73 20 2d 20 42 42 42 42 42 42  49 bits - BBBBBB
b6fb0 41 0a 2a 2a 20 35 36 20 62 69 74 73 20 2d 20 42  A.** 56 bits - B
b6fc0 42 42 42 42 42 42 41 0a 2a 2a 20 36 34 20 62 69  BBBBBBA.** 64 bi
b6fd0 74 73 20 2d 20 42 42 42 42 42 42 42 42 43 0a 2a  ts - BBBBBBBBC.*
b6fe0 2f 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61  /../*.** Write a
b6ff0 20 36 34 2d 62 69 74 20 76 61 72 69 61 62 6c 65   64-bit variable
b7000 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20  -length integer 
b7010 74 6f 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69  to memory starti
b7020 6e 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 54  ng at p[0]..** T
b7030 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 64 61 74  he length of dat
b7040 61 20 77 72 69 74 65 20 77 69 6c 6c 20 62 65 20  a write will be 
b7050 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 39 20  between 1 and 9 
b7060 62 79 74 65 73 2e 20 20 54 68 65 20 6e 75 6d 62  bytes.  The numb
b7070 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73 20 77  er.** of bytes w
b7080 72 69 74 74 65 6e 20 69 73 20 72 65 74 75 72 6e  ritten is return
b7090 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 61 72 69  ed..**.** A vari
b70a0 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65  able-length inte
b70b0 67 65 72 20 63 6f 6e 73 69 73 74 73 20 6f 66 20  ger consists of 
b70c0 74 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73  the lower 7 bits
b70d0 20 6f 66 20 65 61 63 68 20 62 79 74 65 0a 2a 2a   of each byte.**
b70e0 20 66 6f 72 20 61 6c 6c 20 62 79 74 65 73 20 74   for all bytes t
b70f0 68 61 74 20 68 61 76 65 20 74 68 65 20 38 74 68  hat have the 8th
b7100 20 62 69 74 20 73 65 74 20 61 6e 64 20 6f 6e 65   bit set and one
b7110 20 62 79 74 65 20 77 69 74 68 20 74 68 65 20 38   byte with the 8
b7120 74 68 0a 2a 2a 20 62 69 74 20 63 6c 65 61 72 2e  th.** bit clear.
b7130 20 20 45 78 63 65 70 74 2c 20 69 66 20 77 65 20    Except, if we 
b7140 67 65 74 20 74 6f 20 74 68 65 20 39 74 68 20 62  get to the 9th b
b7150 79 74 65 2c 20 69 74 20 73 74 6f 72 65 73 20 74  yte, it stores t
b7160 68 65 20 66 75 6c 6c 0a 2a 2a 20 38 20 62 69 74  he full.** 8 bit
b7170 73 20 61 6e 64 20 69 73 20 74 68 65 20 6c 61 73  s and is the las
b7180 74 20 62 79 74 65 2e 0a 2a 2f 0a 53 51 4c 49 54  t byte..*/.SQLIT
b7190 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
b71a0 6c 69 74 65 33 50 75 74 56 61 72 69 6e 74 28 75  lite3PutVarint(u
b71b0 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 2c  nsigned char *p,
b71c0 20 75 36 34 20 76 29 7b 0a 20 20 69 6e 74 20 69   u64 v){.  int i
b71d0 2c 20 6a 2c 20 6e 3b 0a 20 20 75 38 20 62 75 66  , j, n;.  u8 buf
b71e0 5b 31 30 5d 3b 0a 20 20 69 66 28 20 76 20 26 20  [10];.  if( v & 
b71f0 28 28 28 75 36 34 29 30 78 66 66 30 30 30 30 30  (((u64)0xff00000
b7200 30 29 3c 3c 33 32 29 20 29 7b 0a 20 20 20 20 70  0)<<32) ){.    p
b7210 5b 38 5d 20 3d 20 76 3b 0a 20 20 20 20 76 20 3e  [8] = v;.    v >
b7220 3e 3d 20 38 3b 0a 20 20 20 20 66 6f 72 28 69 3d  >= 8;.    for(i=
b7230 37 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  7; i>=0; i--){. 
b7240 20 20 20 20 20 70 5b 69 5d 20 3d 20 28 76 20 26       p[i] = (v &
b7250 20 30 78 37 66 29 20 7c 20 30 78 38 30 3b 0a 20   0x7f) | 0x80;. 
b7260 20 20 20 20 20 76 20 3e 3e 3d 20 37 3b 0a 20 20       v >>= 7;.  
b7270 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 39    }.    return 9
b7280 3b 0a 20 20 7d 20 20 20 20 0a 20 20 6e 20 3d 20  ;.  }    .  n = 
b7290 30 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 62 75 66  0;.  do{.    buf
b72a0 5b 6e 2b 2b 5d 20 3d 20 28 76 20 26 20 30 78 37  [n++] = (v & 0x7
b72b0 66 29 20 7c 20 30 78 38 30 3b 0a 20 20 20 20 76  f) | 0x80;.    v
b72c0 20 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65   >>= 7;.  }while
b72d0 28 20 76 21 3d 30 20 29 3b 0a 20 20 62 75 66 5b  ( v!=0 );.  buf[
b72e0 30 5d 20 26 3d 20 30 78 37 66 3b 0a 20 20 61 73  0] &= 0x7f;.  as
b72f0 73 65 72 74 28 20 6e 3c 3d 39 20 29 3b 0a 20 20  sert( n<=9 );.  
b7300 66 6f 72 28 69 3d 30 2c 20 6a 3d 6e 2d 31 3b 20  for(i=0, j=n-1; 
b7310 6a 3e 3d 30 3b 20 6a 2d 2d 2c 20 69 2b 2b 29 7b  j>=0; j--, i++){
b7320 0a 20 20 20 20 70 5b 69 5d 20 3d 20 62 75 66 5b  .    p[i] = buf[
b7330 6a 5d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  j];.  }.  return
b7340 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   n;.}../*.** Thi
b7350 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 66  s routine is a f
b7360 61 73 74 65 72 20 76 65 72 73 69 6f 6e 20 6f 66  aster version of
b7370 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69 6e   sqlite3PutVarin
b7380 74 28 29 20 74 68 61 74 20 6f 6e 6c 79 0a 2a 2a  t() that only.**
b7390 20 77 6f 72 6b 73 20 66 6f 72 20 33 32 2d 62 69   works for 32-bi
b73a0 74 20 70 6f 73 69 74 69 76 65 20 69 6e 74 65 67  t positive integ
b73b0 65 72 73 20 61 6e 64 20 77 68 69 63 68 20 69 73  ers and which is
b73c0 20 6f 70 74 69 6d 69 7a 65 64 20 66 6f 72 0a 2a   optimized for.*
b73d0 2a 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 63 61 73  * the common cas
b73e0 65 20 6f 66 20 73 6d 61 6c 6c 20 69 6e 74 65 67  e of small integ
b73f0 65 72 73 2e 20 20 41 20 4d 41 43 52 4f 20 76 65  ers.  A MACRO ve
b7400 72 73 69 6f 6e 2c 20 70 75 74 56 61 72 69 6e 74  rsion, putVarint
b7410 33 32 2c 0a 2a 2a 20 69 73 20 70 72 6f 76 69 64  32,.** is provid
b7420 65 64 20 77 68 69 63 68 20 69 6e 6c 69 6e 65 73  ed which inlines
b7430 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79 74 65   the single-byte
b7440 20 63 61 73 65 2e 20 20 41 6c 6c 20 63 6f 64 65   case.  All code
b7450 20 73 68 6f 75 6c 64 20 75 73 65 0a 2a 2a 20 74   should use.** t
b7460 68 65 20 4d 41 43 52 4f 20 76 65 72 73 69 6f 6e  he MACRO version
b7470 20 61 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f   as this functio
b7480 6e 20 61 73 73 75 6d 65 73 20 74 68 65 20 73 69  n assumes the si
b7490 6e 67 6c 65 2d 62 79 74 65 20 63 61 73 65 20 68  ngle-byte case h
b74a0 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65  as.** already be
b74b0 65 6e 20 68 61 6e 64 6c 65 64 2e 0a 2a 2f 0a 53  en handled..*/.S
b74c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
b74d0 74 20 73 71 6c 69 74 65 33 50 75 74 56 61 72 69  t sqlite3PutVari
b74e0 6e 74 33 32 28 75 6e 73 69 67 6e 65 64 20 63 68  nt32(unsigned ch
b74f0 61 72 20 2a 70 2c 20 75 33 32 20 76 29 7b 0a 23  ar *p, u32 v){.#
b7500 69 66 6e 64 65 66 20 70 75 74 56 61 72 69 6e 74  ifndef putVarint
b7510 33 32 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30  32.  if( (v & ~0
b7520 78 37 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70  x7f)==0 ){.    p
b7530 5b 30 5d 20 3d 20 76 3b 0a 20 20 20 20 72 65 74  [0] = v;.    ret
b7540 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69  urn 1;.  }.#endi
b7550 66 0a 20 20 69 66 28 20 28 76 20 26 20 7e 30 78  f.  if( (v & ~0x
b7560 33 66 66 66 29 3d 3d 30 20 29 7b 0a 20 20 20 20  3fff)==0 ){.    
b7570 70 5b 30 5d 20 3d 20 28 76 3e 3e 37 29 20 7c 20  p[0] = (v>>7) | 
b7580 30 78 38 30 3b 0a 20 20 20 20 70 5b 31 5d 20 3d  0x80;.    p[1] =
b7590 20 76 20 26 20 30 78 37 66 3b 0a 20 20 20 20 72   v & 0x7f;.    r
b75a0 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 20 20 72  eturn 2;.  }.  r
b75b0 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 75 74  eturn sqlite3Put
b75c0 56 61 72 69 6e 74 28 70 2c 20 76 29 3b 0a 7d 0a  Varint(p, v);.}.
b75d0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 36 34  ./*.** Read a 64
b75e0 2d 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65  -bit variable-le
b75f0 6e 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f  ngth integer fro
b7600 6d 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e  m memory startin
b7610 67 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65  g at p[0]..** Re
b7620 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20  turn the number 
b7630 6f 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20  of bytes read.  
b7640 54 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f  The value is sto
b7650 72 65 64 20 69 6e 20 2a 76 2e 0a 2a 2f 0a 53 51  red in *v..*/.SQ
b7660 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b7670 20 73 71 6c 69 74 65 33 47 65 74 56 61 72 69 6e   sqlite3GetVarin
b7680 74 28 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  t(const unsigned
b7690 20 63 68 61 72 20 2a 70 2c 20 75 36 34 20 2a 76   char *p, u64 *v
b76a0 29 7b 0a 20 20 75 33 32 20 61 2c 62 2c 73 3b 0a  ){.  u32 a,b,s;.
b76b0 0a 20 20 61 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20  .  a = *p;.  /* 
b76c0 61 3a 20 70 30 20 28 75 6e 6d 61 73 6b 65 64 29  a: p0 (unmasked)
b76d0 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b76e0 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 2a 76 20  80)).  {.    *v 
b76f0 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = a;.    return 
b7700 31 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a 20  1;.  }..  p++;. 
b7710 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a   b = *p;.  /* b:
b7720 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p1 (unmasked) *
b7730 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30  /.  if (!(b&0x80
b7740 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20  )).  {.    a &= 
b7750 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20 61 3c  0x7f;.    a = a<
b7760 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a  <7;.    a |= b;.
b7770 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20 20      *v = a;.    
b7780 72 65 74 75 72 6e 20 32 3b 0a 20 20 7d 0a 0a 20  return 2;.  }.. 
b7790 20 70 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31   p++;.  a = a<<1
b77a0 34 3b 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20  4;.  a |= *p;.  
b77b0 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c 20 70  /* a: p0<<14 | p
b77c0 32 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a  2 (unmasked) */.
b77d0 20 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29    if (!(a&0x80))
b77e0 0a 20 20 7b 0a 20 20 20 20 61 20 26 3d 20 28 30  .  {.    a &= (0
b77f0 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b7800 0a 20 20 20 20 62 20 26 3d 20 30 78 37 66 3b 0a  .    b &= 0x7f;.
b7810 20 20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20      b = b<<7;.  
b7820 20 20 61 20 7c 3d 20 62 3b 0a 20 20 20 20 2a 76    a |= b;.    *v
b7830 20 3d 20 61 3b 0a 20 20 20 20 72 65 74 75 72 6e   = a;.    return
b7840 20 33 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 53   3;.  }..  /* CS
b7850 45 31 20 66 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f  E1 from below */
b7860 0a 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  a &= (0x7f<<1
b7870 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 70 2b 2b  4)|(0x7f);.  p++
b7880 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
b7890 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
b78a0 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75  : p1<<14 | p3 (u
b78b0 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
b78c0 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
b78d0 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c  .    b &= (0x7f<
b78e0 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
b78f0 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 31 20 75   /* moved CSE1 u
b7900 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d  p */.    /* a &=
b7910 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b7920 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61  f); */.    a = a
b7930 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b  <<7;.    a |= b;
b7940 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a 20 20 20  .    *v = a;.   
b7950 20 72 65 74 75 72 6e 20 34 3b 0a 20 20 7d 0a 0a   return 4;.  }..
b7960 20 20 2f 2a 20 61 3a 20 70 30 3c 3c 31 34 20 7c    /* a: p0<<14 |
b7970 20 70 32 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a   p2 (masked) */.
b7980 20 20 2f 2a 20 62 3a 20 70 31 3c 3c 31 34 20 7c    /* b: p1<<14 |
b7990 20 70 33 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a   p3 (unmasked) *
b79a0 2f 0a 20 20 2f 2a 20 31 3a 73 61 76 65 20 6f 66  /.  /* 1:save of
b79b0 66 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c 3c 31  f p0<<21 | p1<<1
b79c0 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33 20 28  4 | p2<<7 | p3 (
b79d0 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 2f 2a 20  masked) */.  /* 
b79e0 6d 6f 76 65 64 20 43 53 45 31 20 75 70 20 2a 2f  moved CSE1 up */
b79f0 0a 20 20 2f 2a 20 61 20 26 3d 20 28 30 78 37 66  .  /* a &= (0x7f
b7a00 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 20 2a 2f  <<14)|(0x7f); */
b7a10 0a 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 31  .  b &= (0x7f<<1
b7a20 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 73 20 3d  4)|(0x7f);.  s =
b7a30 20 61 3b 0a 20 20 2f 2a 20 73 3a 20 70 30 3c 3c   a;.  /* s: p0<<
b7a40 31 34 20 7c 20 70 32 20 28 6d 61 73 6b 65 64 29  14 | p2 (masked)
b7a50 20 2a 2f 0a 0a 20 20 70 2b 2b 3b 0a 20 20 61 20   */..  p++;.  a 
b7a60 3d 20 61 3c 3c 31 34 3b 0a 20 20 61 20 7c 3d 20  = a<<14;.  a |= 
b7a70 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 3c 3c  *p;.  /* a: p0<<
b7a80 32 38 20 7c 20 70 32 3c 3c 31 34 20 7c 20 70 34  28 | p2<<14 | p4
b7a90 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20   (unmasked) */. 
b7aa0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
b7ab0 20 20 7b 0a 20 20 20 20 2f 2a 20 77 65 20 63 61    {.    /* we ca
b7ac0 6e 20 73 6b 69 70 20 74 68 65 73 65 20 63 61 75  n skip these cau
b7ad0 73 65 20 74 68 65 79 20 77 65 72 65 20 28 65 66  se they were (ef
b7ae0 66 65 63 74 69 76 65 6c 79 29 20 64 6f 6e 65 20  fectively) done 
b7af0 61 62 6f 76 65 20 69 6e 20 63 61 6c 63 27 69 6e  above in calc'in
b7b00 67 20 73 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20  g s */.    /* a 
b7b10 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30  &= (0x7f<<28)|(0
b7b20 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b  x7f<<14)|(0x7f);
b7b30 20 2a 2f 0a 20 20 20 20 2f 2a 20 62 20 26 3d 20   */.    /* b &= 
b7b40 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b7b50 29 3b 20 2a 2f 0a 20 20 20 20 62 20 3d 20 62 3c  ); */.    b = b<
b7b60 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a  <7;.    a |= b;.
b7b70 20 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20      s = s>>18;. 
b7b80 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
b7b90 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65  <<32 | a;.    re
b7ba0 74 75 72 6e 20 35 3b 0a 20 20 7d 0a 0a 20 20 2f  turn 5;.  }..  /
b7bb0 2a 20 32 3a 73 61 76 65 20 6f 66 66 20 70 30 3c  * 2:save off p0<
b7bc0 3c 32 31 20 7c 20 70 31 3c 3c 31 34 20 7c 20 70  <21 | p1<<14 | p
b7bd0 32 3c 3c 37 20 7c 20 70 33 20 28 6d 61 73 6b 65  2<<7 | p3 (maske
b7be0 64 29 20 2a 2f 0a 20 20 73 20 3d 20 73 3c 3c 37  d) */.  s = s<<7
b7bf0 3b 0a 20 20 73 20 7c 3d 20 62 3b 0a 20 20 2f 2a  ;.  s |= b;.  /*
b7c00 20 73 3a 20 70 30 3c 3c 32 31 20 7c 20 70 31 3c   s: p0<<21 | p1<
b7c10 3c 31 34 20 7c 20 70 32 3c 3c 37 20 7c 20 70 33  <14 | p2<<7 | p3
b7c20 20 28 6d 61 73 6b 65 64 29 20 2a 2f 0a 0a 20 20   (masked) */..  
b7c30 70 2b 2b 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34  p++;.  b = b<<14
b7c40 3b 0a 20 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f  ;.  b |= *p;.  /
b7c50 2a 20 62 3a 20 70 31 3c 3c 32 38 20 7c 20 70 33  * b: p1<<28 | p3
b7c60 3c 3c 31 34 20 7c 20 70 35 20 28 75 6e 6d 61 73  <<14 | p5 (unmas
b7c70 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28  ked) */.  if (!(
b7c80 62 26 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20  b&0x80)).  {.   
b7c90 20 2f 2a 20 77 65 20 63 61 6e 20 73 6b 69 70 20   /* we can skip 
b7ca0 74 68 69 73 20 63 61 75 73 65 20 69 74 20 77 61  this cause it wa
b7cb0 73 20 28 65 66 66 65 63 74 69 76 65 6c 79 29 20  s (effectively) 
b7cc0 64 6f 6e 65 20 61 62 6f 76 65 20 69 6e 20 63 61  done above in ca
b7cd0 6c 63 27 69 6e 67 20 73 20 2a 2f 0a 20 20 20 20  lc'ing s */.    
b7ce0 2f 2a 20 62 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* b &= (0x7f<<2
b7cf0 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28 30  8)|(0x7f<<14)|(0
b7d00 78 37 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 26  x7f); */.    a &
b7d10 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78  = (0x7f<<14)|(0x
b7d20 37 66 29 3b 0a 20 20 20 20 61 20 3d 20 61 3c 3c  7f);.    a = a<<
b7d30 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b 0a 20  7;.    a |= b;. 
b7d40 20 20 20 73 20 3d 20 73 3e 3e 31 38 3b 0a 20 20     s = s>>18;.  
b7d50 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29 3c    *v = ((u64)s)<
b7d60 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65 74  <32 | a;.    ret
b7d70 75 72 6e 20 36 3b 0a 20 20 7d 0a 0a 20 20 70 2b  urn 6;.  }..  p+
b7d80 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a  +;.  a = a<<14;.
b7d90 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20    a |= *p;.  /* 
b7da0 61 3a 20 70 32 3c 3c 32 38 20 7c 20 70 34 3c 3c  a: p2<<28 | p4<<
b7db0 31 34 20 7c 20 70 36 20 28 75 6e 6d 61 73 6b 65  14 | p6 (unmaske
b7dc0 64 29 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26  d) */.  if (!(a&
b7dd0 30 78 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61  0x80)).  {.    a
b7de0 20 26 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28   &= (0x7f<<28)|(
b7df0 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29  0x7f<<14)|(0x7f)
b7e00 3b 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66  ;.    b &= (0x7f
b7e10 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20  <<14)|(0x7f);.  
b7e20 20 20 62 20 3d 20 62 3c 3c 37 3b 0a 20 20 20 20    b = b<<7;.    
b7e30 61 20 7c 3d 20 62 3b 0a 20 20 20 20 73 20 3d 20  a |= b;.    s = 
b7e40 73 3e 3e 31 31 3b 0a 20 20 20 20 2a 76 20 3d 20  s>>11;.    *v = 
b7e50 28 28 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61  ((u64)s)<<32 | a
b7e60 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a  ;.    return 7;.
b7e70 20 20 7d 0a 0a 20 20 2f 2a 20 43 53 45 32 20 66    }..  /* CSE2 f
b7e80 72 6f 6d 20 62 65 6c 6f 77 20 2a 2f 0a 20 20 61  rom below */.  a
b7e90 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29 7c 28   &= (0x7f<<14)|(
b7ea0 30 78 37 66 29 3b 0a 20 20 70 2b 2b 3b 0a 20 20  0x7f);.  p++;.  
b7eb0 62 20 3d 20 62 3c 3c 31 34 3b 0a 20 20 62 20 7c  b = b<<14;.  b |
b7ec0 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62 3a 20 70 33  = *p;.  /* b: p3
b7ed0 3c 3c 32 38 20 7c 20 70 35 3c 3c 31 34 20 7c 20  <<28 | p5<<14 | 
b7ee0 70 37 20 28 75 6e 6d 61 73 6b 65 64 29 20 2a 2f  p7 (unmasked) */
b7ef0 0a 20 20 69 66 20 28 21 28 62 26 30 78 38 30 29  .  if (!(b&0x80)
b7f00 29 0a 20 20 7b 0a 20 20 20 20 62 20 26 3d 20 28  ).  {.    b &= (
b7f10 30 78 37 66 3c 3c 32 38 29 7c 28 30 78 37 66 3c  0x7f<<28)|(0x7f<
b7f20 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
b7f30 20 2f 2a 20 6d 6f 76 65 64 20 43 53 45 32 20 75   /* moved CSE2 u
b7f40 70 20 2a 2f 0a 20 20 20 20 2f 2a 20 61 20 26 3d  p */.    /* a &=
b7f50 20 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37   (0x7f<<14)|(0x7
b7f60 66 29 3b 20 2a 2f 0a 20 20 20 20 61 20 3d 20 61  f); */.    a = a
b7f70 3c 3c 37 3b 0a 20 20 20 20 61 20 7c 3d 20 62 3b  <<7;.    a |= b;
b7f80 0a 20 20 20 20 73 20 3d 20 73 3e 3e 34 3b 0a 20  .    s = s>>4;. 
b7f90 20 20 20 2a 76 20 3d 20 28 28 75 36 34 29 73 29     *v = ((u64)s)
b7fa0 3c 3c 33 32 20 7c 20 61 3b 0a 20 20 20 20 72 65  <<32 | a;.    re
b7fb0 74 75 72 6e 20 38 3b 0a 20 20 7d 0a 0a 20 20 70  turn 8;.  }..  p
b7fc0 2b 2b 3b 0a 20 20 61 20 3d 20 61 3c 3c 31 35 3b  ++;.  a = a<<15;
b7fd0 0a 20 20 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a  .  a |= *p;.  /*
b7fe0 20 61 3a 20 70 34 3c 3c 32 39 20 7c 20 70 36 3c   a: p4<<29 | p6<
b7ff0 3c 31 35 20 7c 20 70 38 20 28 75 6e 6d 61 73 6b  <15 | p8 (unmask
b8000 65 64 29 20 2a 2f 0a 0a 20 20 2f 2a 20 6d 6f 76  ed) */..  /* mov
b8010 65 64 20 43 53 45 32 20 75 70 20 2a 2f 0a 20 20  ed CSE2 up */.  
b8020 2f 2a 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 32  /* a &= (0x7f<<2
b8030 39 29 7c 28 30 78 37 66 3c 3c 31 35 29 7c 28 30  9)|(0x7f<<15)|(0
b8040 78 66 66 29 3b 20 2a 2f 0a 20 20 62 20 26 3d 20  xff); */.  b &= 
b8050 28 30 78 37 66 3c 3c 31 34 29 7c 28 30 78 37 66  (0x7f<<14)|(0x7f
b8060 29 3b 0a 20 20 62 20 3d 20 62 3c 3c 38 3b 0a 20  );.  b = b<<8;. 
b8070 20 61 20 7c 3d 20 62 3b 0a 0a 20 20 73 20 3d 20   a |= b;..  s = 
b8080 73 3c 3c 34 3b 0a 20 20 62 20 3d 20 70 5b 2d 34  s<<4;.  b = p[-4
b8090 5d 3b 0a 20 20 62 20 26 3d 20 30 78 37 66 3b 0a  ];.  b &= 0x7f;.
b80a0 20 20 62 20 3d 20 62 3e 3e 33 3b 0a 20 20 73 20    b = b>>3;.  s 
b80b0 7c 3d 20 62 3b 0a 0a 20 20 2a 76 20 3d 20 28 28  |= b;..  *v = ((
b80c0 75 36 34 29 73 29 3c 3c 33 32 20 7c 20 61 3b 0a  u64)s)<<32 | a;.
b80d0 0a 20 20 72 65 74 75 72 6e 20 39 3b 0a 7d 0a 0a  .  return 9;.}..
b80e0 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 33 32 2d  /*.** Read a 32-
b80f0 62 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e  bit variable-len
b8100 67 74 68 20 69 6e 74 65 67 65 72 20 66 72 6f 6d  gth integer from
b8110 20 6d 65 6d 6f 72 79 20 73 74 61 72 74 69 6e 67   memory starting
b8120 20 61 74 20 70 5b 30 5d 2e 0a 2a 2a 20 52 65 74   at p[0]..** Ret
b8130 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
b8140 66 20 62 79 74 65 73 20 72 65 61 64 2e 20 20 54  f bytes read.  T
b8150 68 65 20 76 61 6c 75 65 20 69 73 20 73 74 6f 72  he value is stor
b8160 65 64 20 69 6e 20 2a 76 2e 0a 2a 2a 20 41 20 4d  ed in *v..** A M
b8170 41 43 52 4f 20 76 65 72 73 69 6f 6e 2c 20 67 65  ACRO version, ge
b8180 74 56 61 72 69 6e 74 33 32 2c 20 69 73 20 70 72  tVarint32, is pr
b8190 6f 76 69 64 65 64 20 77 68 69 63 68 20 69 6e 6c  ovided which inl
b81a0 69 6e 65 73 20 74 68 65 20 0a 2a 2a 20 73 69 6e  ines the .** sin
b81b0 67 6c 65 2d 62 79 74 65 20 63 61 73 65 2e 20 20  gle-byte case.  
b81c0 41 6c 6c 20 63 6f 64 65 20 73 68 6f 75 6c 64 20  All code should 
b81d0 75 73 65 20 74 68 65 20 4d 41 43 52 4f 20 76 65  use the MACRO ve
b81e0 72 73 69 6f 6e 20 61 73 20 0a 2a 2a 20 74 68 69  rsion as .** thi
b81f0 73 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d  s function assum
b8200 65 73 20 74 68 65 20 73 69 6e 67 6c 65 2d 62 79  es the single-by
b8210 74 65 20 63 61 73 65 20 68 61 73 20 61 6c 72 65  te case has alre
b8220 61 64 79 20 62 65 65 6e 20 68 61 6e 64 6c 65 64  ady been handled
b8230 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
b8240 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 47  ATE int sqlite3G
b8250 65 74 56 61 72 69 6e 74 33 32 28 63 6f 6e 73 74  etVarint32(const
b8260 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
b8270 70 2c 20 75 33 32 20 2a 76 29 7b 0a 20 20 75 33  p, u32 *v){.  u3
b8280 32 20 61 2c 62 3b 0a 0a 20 20 61 20 3d 20 2a 70  2 a,b;..  a = *p
b8290 3b 0a 20 20 2f 2a 20 61 3a 20 70 30 20 28 75 6e  ;.  /* a: p0 (un
b82a0 6d 61 73 6b 65 64 29 20 2a 2f 0a 23 69 66 6e 64  masked) */.#ifnd
b82b0 65 66 20 67 65 74 56 61 72 69 6e 74 33 32 0a 20  ef getVarint32. 
b82c0 20 69 66 20 28 21 28 61 26 30 78 38 30 29 29 0a   if (!(a&0x80)).
b82d0 20 20 7b 0a 20 20 20 20 2a 76 20 3d 20 61 3b 0a    {.    *v = a;.
b82e0 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
b82f0 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70 2b 2b 3b  }.#endif..  p++;
b8300 0a 20 20 62 20 3d 20 2a 70 3b 0a 20 20 2f 2a 20  .  b = *p;.  /* 
b8310 62 3a 20 70 31 20 28 75 6e 6d 61 73 6b 65 64 29  b: p1 (unmasked)
b8320 20 2a 2f 0a 20 20 69 66 20 28 21 28 62 26 30 78   */.  if (!(b&0x
b8330 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
b8340 3d 20 30 78 37 66 3b 0a 20 20 20 20 61 20 3d 20  = 0x7f;.    a = 
b8350 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61  a<<7;.    *v = a
b8360 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e   | b;.    return
b8370 20 32 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b 0a   2;.  }..  p++;.
b8380 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20 61    a = a<<14;.  a
b8390 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a 20   |= *p;.  /* a: 
b83a0 70 30 3c 3c 31 34 20 7c 20 70 32 20 28 75 6e 6d  p0<<14 | p2 (unm
b83b0 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66 20 28  asked) */.  if (
b83c0 21 28 61 26 30 78 38 30 29 29 0a 20 20 7b 0a 20  !(a&0x80)).  {. 
b83d0 20 20 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31     a &= (0x7f<<1
b83e0 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 62  4)|(0x7f);.    b
b83f0 20 26 3d 20 30 78 37 66 3b 0a 20 20 20 20 62 20   &= 0x7f;.    b 
b8400 3d 20 62 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d  = b<<7;.    *v =
b8410 20 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75   a | b;.    retu
b8420 72 6e 20 33 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b  rn 3;.  }..  p++
b8430 3b 0a 20 20 62 20 3d 20 62 3c 3c 31 34 3b 0a 20  ;.  b = b<<14;. 
b8440 20 62 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 62   b |= *p;.  /* b
b8450 3a 20 70 31 3c 3c 31 34 20 7c 20 70 33 20 28 75  : p1<<14 | p3 (u
b8460 6e 6d 61 73 6b 65 64 29 20 2a 2f 0a 20 20 69 66  nmasked) */.  if
b8470 20 28 21 28 62 26 30 78 38 30 29 29 0a 20 20 7b   (!(b&0x80)).  {
b8480 0a 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c  .    b &= (0x7f<
b8490 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a 20 20 20  <14)|(0x7f);.   
b84a0 20 61 20 26 3d 20 28 30 78 37 66 3c 3c 31 34 29   a &= (0x7f<<14)
b84b0 7c 28 30 78 37 66 29 3b 0a 20 20 20 20 61 20 3d  |(0x7f);.    a =
b84c0 20 61 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20   a<<7;.    *v = 
b84d0 61 20 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72  a | b;.    retur
b84e0 6e 20 34 3b 0a 20 20 7d 0a 0a 20 20 70 2b 2b 3b  n 4;.  }..  p++;
b84f0 0a 20 20 61 20 3d 20 61 3c 3c 31 34 3b 0a 20 20  .  a = a<<14;.  
b8500 61 20 7c 3d 20 2a 70 3b 0a 20 20 2f 2a 20 61 3a  a |= *p;.  /* a:
b8510 20 70 30 3c 3c 32 38 20 7c 20 70 32 3c 3c 31 34   p0<<28 | p2<<14
b8520 20 7c 20 70 34 20 28 75 6e 6d 61 73 6b 65 64 29   | p4 (unmasked)
b8530 20 2a 2f 0a 20 20 69 66 20 28 21 28 61 26 30 78   */.  if (!(a&0x
b8540 38 30 29 29 0a 20 20 7b 0a 20 20 20 20 61 20 26  80)).  {.    a &
b8550 3d 20 28 30 78 37 66 3c 3c 32 38 29 7c 28 30 78  = (0x7f<<28)|(0x
b8560 37 66 3c 3c 31 34 29 7c 28 30 78 37 66 29 3b 0a  7f<<14)|(0x7f);.
b8570 20 20 20 20 62 20 26 3d 20 28 30 78 37 66 3c 3c      b &= (0x7f<<
b8580 32 38 29 7c 28 30 78 37 66 3c 3c 31 34 29 7c 28  28)|(0x7f<<14)|(
b8590 30 78 37 66 29 3b 0a 20 20 20 20 62 20 3d 20 62  0x7f);.    b = b
b85a0 3c 3c 37 3b 0a 20 20 20 20 2a 76 20 3d 20 61 20  <<7;.    *v = a 
b85b0 7c 20 62 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  | b;.    return 
b85c0 35 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20  5;.  }..  /* We 
b85d0 63 61 6e 20 6f 6e 6c 79 20 72 65 61 63 68 20 74  can only reach t
b85e0 68 69 73 20 70 6f 69 6e 74 20 77 68 65 6e 20 72  his point when r
b85f0 65 61 64 69 6e 67 20 61 20 63 6f 72 72 75 70 74  eading a corrupt
b8600 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a 20 66   database.  ** f
b8610 69 6c 65 2e 20 20 49 6e 20 74 68 61 74 20 63 61  ile.  In that ca
b8620 73 65 20 77 65 20 61 72 65 20 6e 6f 74 20 69 6e  se we are not in
b8630 20 61 6e 79 20 68 75 72 72 79 2e 20 20 55 73 65   any hurry.  Use
b8640 20 74 68 65 20 28 72 65 6c 61 74 69 76 65 6c 79   the (relatively
b8650 0a 20 20 2a 2a 20 73 6c 6f 77 29 20 67 65 6e 65  .  ** slow) gene
b8660 72 61 6c 2d 70 75 72 70 6f 73 65 20 73 71 6c 69  ral-purpose sqli
b8670 74 65 33 47 65 74 56 61 72 69 6e 74 28 29 20 72  te3GetVarint() r
b8680 6f 75 74 69 6e 65 20 74 6f 20 65 78 74 72 61 63  outine to extrac
b8690 74 20 74 68 65 0a 20 20 2a 2a 20 76 61 6c 75 65  t the.  ** value
b86a0 2e 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 36 34  . */.  {.    u64
b86b0 20 76 36 34 3b 0a 20 20 20 20 69 6e 74 20 6e 3b   v64;.    int n;
b86c0 0a 0a 20 20 20 20 70 20 2d 3d 20 34 3b 0a 20 20  ..    p -= 4;.  
b86d0 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74    n = sqlite3Get
b86e0 56 61 72 69 6e 74 28 70 2c 20 26 76 36 34 29 3b  Varint(p, &v64);
b86f0 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e 35  .    assert( n>5
b8700 20 26 26 20 6e 3c 3d 39 20 29 3b 0a 20 20 20 20   && n<=9 );.    
b8710 2a 76 20 3d 20 28 75 33 32 29 76 36 34 3b 0a 20  *v = (u32)v64;. 
b8720 20 20 20 72 65 74 75 72 6e 20 6e 3b 0a 20 20 7d     return n;.  }
b8730 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
b8740 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62   the number of b
b8750 79 74 65 73 20 74 68 61 74 20 77 69 6c 6c 20 62  ytes that will b
b8760 65 20 6e 65 65 64 65 64 20 74 6f 20 73 74 6f 72  e needed to stor
b8770 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a 20 36  e the given.** 6
b8780 34 2d 62 69 74 20 69 6e 74 65 67 65 72 2e 0a 2a  4-bit integer..*
b8790 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b87a0 20 69 6e 74 20 73 71 6c 69 74 65 33 56 61 72 69   int sqlite3Vari
b87b0 6e 74 4c 65 6e 28 75 36 34 20 76 29 7b 0a 20 20  ntLen(u64 v){.  
b87c0 69 6e 74 20 69 20 3d 20 30 3b 0a 20 20 64 6f 7b  int i = 0;.  do{
b87d0 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 76 20  .    i++;.    v 
b87e0 3e 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28  >>= 7;.  }while(
b87f0 20 76 21 3d 30 20 26 26 20 69 3c 39 20 29 3b 0a   v!=0 && i<9 );.
b8800 20 20 72 65 74 75 72 6e 20 69 3b 0a 7d 0a 0a 0a    return i;.}...
b8810 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f 72 20 77 72  /*.** Read or wr
b8820 69 74 65 20 61 20 66 6f 75 72 2d 62 79 74 65 20  ite a four-byte 
b8830 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67  big-endian integ
b8840 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c  er value..*/.SQL
b8850 49 54 45 5f 50 52 49 56 41 54 45 20 75 33 32 20  ITE_PRIVATE u32 
b8860 73 71 6c 69 74 65 33 47 65 74 34 62 79 74 65 28  sqlite3Get4byte(
b8870 63 6f 6e 73 74 20 75 38 20 2a 70 29 7b 0a 20 20  const u8 *p){.  
b8880 72 65 74 75 72 6e 20 28 70 5b 30 5d 3c 3c 32 34  return (p[0]<<24
b8890 29 20 7c 20 28 70 5b 31 5d 3c 3c 31 36 29 20 7c  ) | (p[1]<<16) |
b88a0 20 28 70 5b 32 5d 3c 3c 38 29 20 7c 20 70 5b 33   (p[2]<<8) | p[3
b88b0 5d 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  ];.}.SQLITE_PRIV
b88c0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
b88d0 50 75 74 34 62 79 74 65 28 75 6e 73 69 67 6e 65  Put4byte(unsigne
b88e0 64 20 63 68 61 72 20 2a 70 2c 20 75 33 32 20 76  d char *p, u32 v
b88f0 29 7b 0a 20 20 70 5b 30 5d 20 3d 20 76 3e 3e 32  ){.  p[0] = v>>2
b8900 34 3b 0a 20 20 70 5b 31 5d 20 3d 20 76 3e 3e 31  4;.  p[1] = v>>1
b8910 36 3b 0a 20 20 70 5b 32 5d 20 3d 20 76 3e 3e 38  6;.  p[2] = v>>8
b8920 3b 0a 20 20 70 5b 33 5d 20 3d 20 76 3b 0a 7d 0a  ;.  p[3] = v;.}.
b8930 0a 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  ...#if !defined(
b8940 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
b8950 5f 4c 49 54 45 52 41 4c 29 20 7c 7c 20 64 65 66  _LITERAL) || def
b8960 69 6e 65 64 28 53 51 4c 49 54 45 5f 48 41 53 5f  ined(SQLITE_HAS_
b8970 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a 20 54 72 61  CODEC)./*.** Tra
b8980 6e 73 6c 61 74 65 20 61 20 73 69 6e 67 6c 65 20  nslate a single 
b8990 62 79 74 65 20 6f 66 20 48 65 78 20 69 6e 74 6f  byte of Hex into
b89a0 20 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20   an integer..** 
b89b0 54 68 69 73 20 72 6f 75 74 69 6e 65 6e 20 6f 6e  This routinen on
b89c0 6c 79 20 77 6f 72 6b 73 20 69 66 20 68 20 72 65  ly works if h re
b89d0 61 6c 6c 79 20 69 73 20 61 20 76 61 6c 69 64 20  ally is a valid 
b89e0 68 65 78 61 64 65 63 69 6d 61 6c 0a 2a 2a 20 63  hexadecimal.** c
b89f0 68 61 72 61 63 74 65 72 3a 20 20 30 2e 2e 39 61  haracter:  0..9a
b8a00 2e 2e 66 41 2e 2e 46 0a 2a 2f 0a 73 74 61 74 69  ..fA..F.*/.stati
b8a10 63 20 69 6e 74 20 68 65 78 54 6f 49 6e 74 28 69  c int hexToInt(i
b8a20 6e 74 20 68 29 7b 0a 20 20 61 73 73 65 72 74 28  nt h){.  assert(
b8a30 20 28 68 3e 3d 27 30 27 20 26 26 20 68 3c 3d 27   (h>='0' && h<='
b8a40 39 27 29 20 7c 7c 20 20 28 68 3e 3d 27 61 27 20  9') ||  (h>='a' 
b8a50 26 26 20 68 3c 3d 27 66 27 29 20 7c 7c 20 20 28  && h<='f') ||  (
b8a60 68 3e 3d 27 41 27 20 26 26 20 68 3c 3d 27 46 27  h>='A' && h<='F'
b8a70 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  ) );.#ifdef SQLI
b8a80 54 45 5f 41 53 43 49 49 0a 20 20 68 20 2b 3d 20  TE_ASCII.  h += 
b8a90 39 2a 28 31 26 28 68 3e 3e 36 29 29 3b 0a 23 65  9*(1&(h>>6));.#e
b8aa0 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
b8ab0 54 45 5f 45 42 43 44 49 43 0a 20 20 68 20 2b 3d  TE_EBCDIC.  h +=
b8ac0 20 39 2a 28 31 26 7e 28 68 3e 3e 34 29 29 3b 0a   9*(1&~(h>>4));.
b8ad0 23 65 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20  #endif.  return 
b8ae0 68 20 26 20 30 78 66 3b 0a 7d 0a 23 65 6e 64 69  h & 0xf;.}.#endi
b8af0 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f 4f 4d 49  f /* !SQLITE_OMI
b8b00 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 20 7c  T_BLOB_LITERAL |
b8b10 7c 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  | SQLITE_HAS_COD
b8b20 45 43 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69  EC */..#if !defi
b8b30 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
b8b40 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 29 20 7c 7c  BLOB_LITERAL) ||
b8b50 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
b8b60 48 41 53 5f 43 4f 44 45 43 29 0a 2f 2a 0a 2a 2a  HAS_CODEC)./*.**
b8b70 20 43 6f 6e 76 65 72 74 20 61 20 42 4c 4f 42 20   Convert a BLOB 
b8b80 6c 69 74 65 72 61 6c 20 6f 66 20 74 68 65 20 66  literal of the f
b8b90 6f 72 6d 20 22 78 27 68 68 68 68 68 68 27 22 20  orm "x'hhhhhh'" 
b8ba0 69 6e 74 6f 20 69 74 73 20 62 69 6e 61 72 79 0a  into its binary.
b8bb0 2a 2a 20 76 61 6c 75 65 2e 20 20 52 65 74 75 72  ** value.  Retur
b8bc0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  n a pointer to i
b8bd0 74 73 20 62 69 6e 61 72 79 20 76 61 6c 75 65 2e  ts binary value.
b8be0 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
b8bf0 74 68 65 0a 2a 2a 20 62 69 6e 61 72 79 20 76 61  the.** binary va
b8c00 6c 75 65 20 68 61 73 20 62 65 65 6e 20 6f 62 74  lue has been obt
b8c10 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f  ained from mallo
b8c20 63 20 61 6e 64 20 6d 75 73 74 20 62 65 20 66 72  c and must be fr
b8c30 65 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63 61  eed by.** the ca
b8c40 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 2e 0a 2a  lling routine..*
b8c50 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
b8c60 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 65   void *sqlite3He
b8c70 78 54 6f 42 6c 6f 62 28 73 71 6c 69 74 65 33 20  xToBlob(sqlite3 
b8c80 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *db, const char 
b8c90 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 63 68  *z, int n){.  ch
b8ca0 61 72 20 2a 7a 42 6c 6f 62 3b 0a 20 20 69 6e 74  ar *zBlob;.  int
b8cb0 20 69 3b 0a 0a 20 20 7a 42 6c 6f 62 20 3d 20 28   i;..  zBlob = (
b8cc0 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 44 62  char *)sqlite3Db
b8cd0 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 6e 2f  MallocRaw(db, n/
b8ce0 32 20 2b 20 31 29 3b 0a 20 20 6e 2d 2d 3b 0a 20  2 + 1);.  n--;. 
b8cf0 20 69 66 28 20 7a 42 6c 6f 62 20 29 7b 0a 20 20   if( zBlob ){.  
b8d00 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20    for(i=0; i<n; 
b8d10 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 7a 42 6c  i+=2){.      zBl
b8d20 6f 62 5b 69 2f 32 5d 20 3d 20 28 68 65 78 54 6f  ob[i/2] = (hexTo
b8d30 49 6e 74 28 7a 5b 69 5d 29 3c 3c 34 29 20 7c 20  Int(z[i])<<4) | 
b8d40 68 65 78 54 6f 49 6e 74 28 7a 5b 69 2b 31 5d 29  hexToInt(z[i+1])
b8d50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 6c 6f  ;.    }.    zBlo
b8d60 62 5b 69 2f 32 5d 20 3d 20 30 3b 0a 20 20 7d 0a  b[i/2] = 0;.  }.
b8d70 20 20 72 65 74 75 72 6e 20 7a 42 6c 6f 62 3b 0a    return zBlob;.
b8d80 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
b8d90 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49  ITE_OMIT_BLOB_LI
b8da0 54 45 52 41 4c 20 7c 7c 20 53 51 4c 49 54 45 5f  TERAL || SQLITE_
b8db0 48 41 53 5f 43 4f 44 45 43 20 2a 2f 0a 0a 0a 2f  HAS_CODEC */.../
b8dc0 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20  *.** Change the 
b8dd0 73 71 6c 69 74 65 2e 6d 61 67 69 63 20 66 72 6f  sqlite.magic fro
b8de0 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f  m SQLITE_MAGIC_O
b8df0 50 45 4e 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41  PEN to SQLITE_MA
b8e00 47 49 43 5f 42 55 53 59 2e 0a 2a 2a 20 52 65 74  GIC_BUSY..** Ret
b8e10 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e 6f  urn an error (no
b8e20 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20 6d  n-zero) if the m
b8e30 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51 4c  agic was not SQL
b8e40 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a  ITE_MAGIC_OPEN.*
b8e50 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75 74  * when this rout
b8e60 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  ine is called..*
b8e70 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
b8e80 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e  e is called when
b8e90 20 65 6e 74 65 72 69 6e 67 20 61 6e 20 53 51 4c   entering an SQL
b8ea0 69 74 65 20 41 50 49 2e 20 20 54 68 65 20 53 51  ite API.  The SQ
b8eb0 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a  LITE_MAGIC_OPEN.
b8ec0 2a 2a 20 76 61 6c 75 65 20 69 6e 64 69 63 61 74  ** value indicat
b8ed0 65 73 20 74 68 61 74 20 74 68 65 20 64 61 74 61  es that the data
b8ee0 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
b8ef0 70 61 73 73 65 64 20 69 6e 74 6f 20 74 68 65 20  passed into the 
b8f00 41 50 49 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 61  API is.** open a
b8f10 6e 64 20 69 73 20 6e 6f 74 20 62 65 69 6e 67 20  nd is not being 
b8f20 75 73 65 64 20 62 79 20 61 6e 6f 74 68 65 72 20  used by another 
b8f30 74 68 72 65 61 64 2e 20 20 42 79 20 63 68 61 6e  thread.  By chan
b8f40 67 69 6e 67 20 74 68 65 20 76 61 6c 75 65 0a 2a  ging the value.*
b8f50 2a 20 74 6f 20 53 51 4c 49 54 45 5f 4d 41 47 49  * to SQLITE_MAGI
b8f60 43 5f 42 55 53 59 20 77 65 20 69 6e 64 69 63 61  C_BUSY we indica
b8f70 74 65 20 74 68 61 74 20 74 68 65 20 63 6f 6e 6e  te that the conn
b8f80 65 63 74 69 6f 6e 20 69 73 20 69 6e 20 75 73 65  ection is in use
b8f90 2e 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66 65  ..** sqlite3Safe
b8fa0 74 79 4f 66 66 28 29 20 62 65 6c 6f 77 20 77 69  tyOff() below wi
b8fb0 6c 6c 20 63 68 61 6e 67 65 20 74 68 65 20 76 61  ll change the va
b8fc0 6c 75 65 20 62 61 63 6b 20 74 6f 20 53 51 4c 49  lue back to SQLI
b8fd0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 0a 2a 2a  TE_MAGIC_OPEN.**
b8fe0 20 77 68 65 6e 20 74 68 65 20 41 50 49 20 65 78   when the API ex
b8ff0 69 74 73 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73  its. .**.** This
b9000 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 61 74   routine is a at
b9010 74 65 6d 70 74 20 74 6f 20 64 65 74 65 63 74 20  tempt to detect 
b9020 69 66 20 74 77 6f 20 74 68 72 65 61 64 73 20 75  if two threads u
b9030 73 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 73  se the.** same s
b9040 71 6c 69 74 65 2a 20 70 6f 69 6e 74 65 72 20 61  qlite* pointer a
b9050 74 20 74 68 65 20 73 61 6d 65 20 74 69 6d 65 2e  t the same time.
b9060 20 20 54 68 65 72 65 20 69 73 20 61 20 72 61 63    There is a rac
b9070 65 20 0a 2a 2a 20 63 6f 6e 64 69 74 69 6f 6e 20  e .** condition 
b9080 73 6f 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  so it is possibl
b9090 65 20 74 68 61 74 20 74 68 65 20 65 72 72 6f 72  e that the error
b90a0 20 69 73 20 6e 6f 74 20 64 65 74 65 63 74 65 64   is not detected
b90b0 2e 0a 2a 2a 20 42 75 74 20 75 73 75 61 6c 6c 79  ..** But usually
b90c0 20 74 68 65 20 70 72 6f 62 6c 65 6d 20 77 69 6c   the problem wil
b90d0 6c 20 62 65 20 73 65 65 6e 2e 20 20 54 68 65 20  l be seen.  The 
b90e0 72 65 73 75 6c 74 20 77 69 6c 6c 20 62 65 20 61  result will be a
b90f0 6e 0a 2a 2a 20 65 72 72 6f 72 20 77 68 69 63 68  n.** error which
b9100 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
b9110 64 65 62 75 67 20 74 68 65 20 61 70 70 6c 69 63  debug the applic
b9120 61 74 69 6f 6e 20 74 68 61 74 20 69 73 0a 2a 2a  ation that is.**
b9130 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 69 6e   using SQLite in
b9140 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2a 0a 2a 2a  correctly..**.**
b9150 20 54 69 63 6b 65 74 20 23 32 30 32 3a 20 20 49   Ticket #202:  I
b9160 66 20 64 62 2d 3e 6d 61 67 69 63 20 69 73 20 6e  f db->magic is n
b9170 6f 74 20 61 20 76 61 6c 69 64 20 6f 70 65 6e 20  ot a valid open 
b9180 76 61 6c 75 65 2c 20 74 61 6b 65 20 63 61 72 65  value, take care
b9190 20 6e 6f 74 0a 2a 2a 20 74 6f 20 6d 6f 64 69 66   not.** to modif
b91a0 79 20 74 68 65 20 64 62 20 73 74 72 75 63 74 75  y the db structu
b91b0 72 65 20 61 74 20 61 6c 6c 2e 20 20 49 74 20 63  re at all.  It c
b91c0 6f 75 6c 64 20 62 65 20 74 68 61 74 20 64 62 20  ould be that db 
b91d0 69 73 20 61 20 73 74 61 6c 65 0a 2a 2a 20 70 6f  is a stale.** po
b91e0 69 6e 74 65 72 2e 20 20 49 6e 20 6f 74 68 65 72  inter.  In other
b91f0 20 77 6f 72 64 73 2c 20 69 74 20 63 6f 75 6c 64   words, it could
b9200 20 62 65 20 74 68 61 74 20 74 68 65 72 65 20 68   be that there h
b9210 61 73 20 62 65 65 6e 20 61 20 70 72 69 6f 72 0a  as been a prior.
b9220 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  ** call to sqlit
b9230 65 33 5f 63 6c 6f 73 65 28 64 62 29 20 61 6e 64  e3_close(db) and
b9240 20 64 62 20 68 61 73 20 62 65 65 6e 20 64 65 61   db has been dea
b9250 6c 6c 6f 63 61 74 65 64 2e 20 20 41 6e 64 20 77  llocated.  And w
b9260 65 20 64 6f 0a 2a 2a 20 6e 6f 74 20 77 61 6e 74  e do.** not want
b9270 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f 20 64   to write into d
b9280 65 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72  eallocated memor
b9290 79 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  y..*/.#ifdef SQL
b92a0 49 54 45 5f 44 45 42 55 47 0a 53 51 4c 49 54 45  ITE_DEBUG.SQLITE
b92b0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
b92c0 69 74 65 33 53 61 66 65 74 79 4f 6e 28 73 71 6c  ite3SafetyOn(sql
b92d0 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28  ite3 *db){.  if(
b92e0 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49   db->magic==SQLI
b92f0 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e 20 29 7b  TE_MAGIC_OPEN ){
b9300 0a 20 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d  .    db->magic =
b9310 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55   SQLITE_MAGIC_BU
b9320 53 59 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  SY;.    assert( 
b9330 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
b9340 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
b9350 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
b9360 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d 3e 6d   }else if( db->m
b9370 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
b9380 49 43 5f 42 55 53 59 20 29 7b 0a 20 20 20 20 64  IC_BUSY ){.    d
b9390 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
b93a0 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a 20  E_MAGIC_ERROR;. 
b93b0 20 20 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65     db->u1.isInte
b93c0 72 72 75 70 74 65 64 20 3d 20 31 3b 0a 20 20 7d  rrupted = 1;.  }
b93d0 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
b93e0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  endif../*.** Cha
b93f0 6e 67 65 20 74 68 65 20 6d 61 67 69 63 20 66 72  nge the magic fr
b9400 6f 6d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  om SQLITE_MAGIC_
b9410 42 55 53 59 20 74 6f 20 53 51 4c 49 54 45 5f 4d  BUSY to SQLITE_M
b9420 41 47 49 43 5f 4f 50 45 4e 2e 0a 2a 2a 20 52 65  AGIC_OPEN..** Re
b9430 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 28 6e  turn an error (n
b9440 6f 6e 2d 7a 65 72 6f 29 20 69 66 20 74 68 65 20  on-zero) if the 
b9450 6d 61 67 69 63 20 77 61 73 20 6e 6f 74 20 53 51  magic was not SQ
b9460 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 0a  LITE_MAGIC_BUSY.
b9470 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  ** when this rou
b9480 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 0a  tine is called..
b9490 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
b94a0 5f 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52  _DEBUG.SQLITE_PR
b94b0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
b94c0 33 53 61 66 65 74 79 4f 66 66 28 73 71 6c 69 74  3SafetyOff(sqlit
b94d0 65 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64  e3 *db){.  if( d
b94e0 62 2d 3e 6d 61 67 69 63 3d 3d 53 51 4c 49 54 45  b->magic==SQLITE
b94f0 5f 4d 41 47 49 43 5f 42 55 53 59 20 29 7b 0a 20  _MAGIC_BUSY ){. 
b9500 20 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53     db->magic = S
b9510 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45 4e  QLITE_MAGIC_OPEN
b9520 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71  ;.    assert( sq
b9530 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
b9540 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
b9550 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
b9560 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6d 61  else{.    db->ma
b9570 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47  gic = SQLITE_MAG
b9580 49 43 5f 45 52 52 4f 52 3b 0a 20 20 20 20 64 62  IC_ERROR;.    db
b9590 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74  ->u1.isInterrupt
b95a0 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
b95b0 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  rn 1;.  }.}.#end
b95c0 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  if../*.** Check 
b95d0 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 77 65 20  to make sure we 
b95e0 68 61 76 65 20 61 20 76 61 6c 69 64 20 64 62 20  have a valid db 
b95f0 70 6f 69 6e 74 65 72 2e 20 20 54 68 69 73 20 74  pointer.  This t
b9600 65 73 74 20 69 73 20 6e 6f 74 0a 2a 2a 20 66 6f  est is not.** fo
b9610 6f 6c 70 72 6f 6f 66 20 62 75 74 20 69 74 20 64  olproof but it d
b9620 6f 65 73 20 70 72 6f 76 69 64 65 20 73 6f 6d 65  oes provide some
b9630 20 6d 65 61 73 75 72 65 20 6f 66 20 70 72 6f 74   measure of prot
b9640 65 63 74 69 6f 6e 20 61 67 61 69 6e 73 74 0a 2a  ection against.*
b9650 2a 20 6d 69 73 75 73 65 20 6f 66 20 74 68 65 20  * misuse of the 
b9660 69 6e 74 65 72 66 61 63 65 20 73 75 63 68 20 61  interface such a
b9670 73 20 70 61 73 73 69 6e 67 20 69 6e 20 64 62 20  s passing in db 
b9680 70 6f 69 6e 74 65 72 73 20 74 68 61 74 20 61 72  pointers that ar
b9690 65 0a 2a 2a 20 4e 55 4c 4c 20 6f 72 20 77 68 69  e.** NULL or whi
b96a0 63 68 20 68 61 76 65 20 62 65 65 6e 20 70 72 65  ch have been pre
b96b0 76 69 6f 75 73 6c 79 20 63 6c 6f 73 65 64 2e 20  viously closed. 
b96c0 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
b96d0 20 72 65 74 75 72 6e 73 0a 2a 2a 20 31 20 69 74   returns.** 1 it
b96e0 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20   means that the 
b96f0 64 62 20 70 6f 69 6e 74 65 72 20 69 73 20 76 61  db pointer is va
b9700 6c 69 64 20 61 6e 64 20 30 20 69 66 20 69 74 20  lid and 0 if it 
b9710 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a  should not be.**
b9720 20 64 65 72 65 66 65 72 65 6e 63 65 64 20 66 6f   dereferenced fo
b9730 72 20 61 6e 79 20 72 65 61 73 6f 6e 2e 20 20 54  r any reason.  T
b9740 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
b9750 69 6f 6e 20 73 68 6f 75 6c 64 20 69 6e 76 6f 6b  ion should invok
b9760 65 0a 2a 2a 20 53 51 4c 49 54 45 5f 4d 49 53 55  e.** SQLITE_MISU
b9770 53 45 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 0a  SE immediately..
b9780 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 53 61 66  **.** sqlite3Saf
b9790 65 74 79 43 68 65 63 6b 4f 6b 28 29 20 72 65 71  etyCheckOk() req
b97a0 75 69 72 65 73 20 74 68 61 74 20 74 68 65 20 64  uires that the d
b97b0 62 20 70 6f 69 6e 74 65 72 20 62 65 20 76 61 6c  b pointer be val
b97c0 69 64 20 66 6f 72 0a 2a 2a 20 75 73 65 2e 20 20  id for.** use.  
b97d0 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68 65  sqlite3SafetyChe
b97e0 63 6b 53 69 63 6b 4f 72 4f 6b 28 29 20 61 6c 6c  ckSickOrOk() all
b97f0 6f 77 73 20 61 20 64 62 20 70 6f 69 6e 74 65 72  ows a db pointer
b9800 20 74 68 61 74 20 66 61 69 6c 65 64 20 74 6f 0a   that failed to.
b9810 2a 2a 20 6f 70 65 6e 20 70 72 6f 70 65 72 6c 79  ** open properly
b9820 20 61 6e 64 20 69 73 20 6e 6f 74 20 66 69 74 20   and is not fit 
b9830 66 6f 72 20 67 65 6e 65 72 61 6c 20 75 73 65 20  for general use 
b9840 62 75 74 20 77 68 69 63 68 20 63 61 6e 20 62 65  but which can be
b9850 0a 2a 2a 20 75 73 65 64 20 61 73 20 61 6e 20 61  .** used as an a
b9860 72 67 75 6d 65 6e 74 20 74 6f 20 73 71 6c 69 74  rgument to sqlit
b9870 65 33 5f 65 72 72 6d 73 67 28 29 20 6f 72 20 73  e3_errmsg() or s
b9880 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 29 2e 0a  qlite3_close()..
b9890 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
b98a0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 53 61 66  E int sqlite3Saf
b98b0 65 74 79 43 68 65 63 6b 4f 6b 28 73 71 6c 69 74  etyCheckOk(sqlit
b98c0 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20 6d  e3 *db){.  u32 m
b98d0 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d 3d  agic;.  if( db==
b98e0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
b98f0 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67 69  magic = db->magi
b9900 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21 3d  c;.  if( magic!=
b9910 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 4f 50 45  SQLITE_MAGIC_OPE
b9920 4e 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69 63  N &&.      magic
b9930 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42  !=SQLITE_MAGIC_B
b9940 55 53 59 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  USY ) return 0;.
b9950 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 53 51    return 1;.}.SQ
b9960 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
b9970 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 43 68   sqlite3SafetyCh
b9980 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 73 71 6c 69  eckSickOrOk(sqli
b9990 74 65 33 20 2a 64 62 29 7b 0a 20 20 75 33 32 20  te3 *db){.  u32 
b99a0 6d 61 67 69 63 3b 0a 20 20 69 66 28 20 64 62 3d  magic;.  if( db=
b99b0 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
b99c0 20 6d 61 67 69 63 20 3d 20 64 62 2d 3e 6d 61 67   magic = db->mag
b99d0 69 63 3b 0a 20 20 69 66 28 20 6d 61 67 69 63 21  ic;.  if( magic!
b99e0 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 53 49  =SQLITE_MAGIC_SI
b99f0 43 4b 20 26 26 0a 20 20 20 20 20 20 6d 61 67 69  CK &&.      magi
b9a00 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f  c!=SQLITE_MAGIC_
b9a10 4f 50 45 4e 20 26 26 0a 20 20 20 20 20 20 6d 61  OPEN &&.      ma
b9a20 67 69 63 21 3d 53 51 4c 49 54 45 5f 4d 41 47 49  gic!=SQLITE_MAGI
b9a30 43 5f 42 55 53 59 20 29 20 72 65 74 75 72 6e 20  C_BUSY ) return 
b9a40 30 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  0;.  return 1;.}
b9a50 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
b9a60 2a 20 45 6e 64 20 6f 66 20 75 74 69 6c 2e 63 20  * End of util.c 
b9a70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9a90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9aa0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
b9ab0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 61 73  * Begin file has
b9ac0 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h.c ************
b9ad0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9ae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9af0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
b9b00 74 65 6d 62 65 72 20 32 32 0a 2a 2a 0a 2a 2a 20  tember 22.**.** 
b9b10 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
b9b20 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
b9b30 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
b9b40 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
b9b50 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
b9b60 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
b9b70 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
b9b80 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
b9b90 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
b9ba0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
b9bb0 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
b9bc0 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
b9bd0 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
b9be0 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
b9bf0 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
b9c00 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
b9c10 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
b9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9c30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9c40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9c50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
b9c60 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
b9c70 20 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e   is the implemen
b9c80 74 61 74 69 6f 6e 20 6f 66 20 67 65 6e 65 72 69  tation of generi
b9c90 63 20 68 61 73 68 2d 74 61 62 6c 65 73 0a 2a 2a  c hash-tables.**
b9ca0 20 75 73 65 64 20 69 6e 20 53 51 4c 69 74 65 2e   used in SQLite.
b9cb0 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 61 73 68  .**.** $Id: hash
b9cc0 2e 63 2c 76 20 31 2e 33 31 20 32 30 30 38 2f 31  .c,v 1.31 2008/1
b9cd0 30 2f 31 30 20 31 37 3a 34 31 3a 32 39 20 64 72  0/10 17:41:29 dr
b9ce0 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 54  h Exp $.*/../* T
b9cf0 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f 72 79 20  urn bulk memory 
b9d00 69 6e 74 6f 20 61 20 68 61 73 68 20 74 61 62 6c  into a hash tabl
b9d10 65 20 6f 62 6a 65 63 74 20 62 79 20 69 6e 69 74  e object by init
b9d20 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a 2a 2a 20  ializing the.** 
b9d30 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 48 61  fields of the Ha
b9d40 73 68 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  sh structure..**
b9d50 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73 20 61 20  .** "pNew" is a 
b9d60 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 68  pointer to the h
b9d70 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20 69  ash table that i
b9d80 73 20 74 6f 20 62 65 20 69 6e 69 74 69 61 6c 69  s to be initiali
b9d90 7a 65 64 2e 0a 2a 2a 20 22 63 6f 70 79 4b 65 79  zed..** "copyKey
b9da0 22 20 69 73 20 74 72 75 65 20 69 66 20 74 68 65  " is true if the
b9db0 20 68 61 73 68 20 74 61 62 6c 65 20 73 68 6f 75   hash table shou
b9dc0 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77 6e 20  ld make its own 
b9dd0 70 72 69 76 61 74 65 0a 2a 2a 20 63 6f 70 79 20  private.** copy 
b9de0 6f 66 20 6b 65 79 73 20 61 6e 64 20 66 61 6c 73  of keys and fals
b9df0 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64 20 6a  e if it should j
b9e00 75 73 74 20 75 73 65 20 74 68 65 20 73 75 70 70  ust use the supp
b9e10 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f  lied pointer..*/
b9e20 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
b9e30 76 6f 69 64 20 73 71 6c 69 74 65 33 48 61 73 68  void sqlite3Hash
b9e40 49 6e 69 74 28 48 61 73 68 20 2a 70 4e 65 77 2c  Init(Hash *pNew,
b9e50 20 69 6e 74 20 63 6f 70 79 4b 65 79 29 7b 0a 20   int copyKey){. 
b9e60 20 61 73 73 65 72 74 28 20 70 4e 65 77 21 3d 30   assert( pNew!=0
b9e70 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 70 79   );.  pNew->copy
b9e80 4b 65 79 20 3d 20 63 6f 70 79 4b 65 79 21 3d 30  Key = copyKey!=0
b9e90 3b 0a 20 20 70 4e 65 77 2d 3e 66 69 72 73 74 20  ;.  pNew->first 
b9ea0 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 63 6f 75  = 0;.  pNew->cou
b9eb0 6e 74 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  nt = 0;.  pNew->
b9ec0 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 70 4e  htsize = 0;.  pN
b9ed0 65 77 2d 3e 68 74 20 3d 20 30 3b 0a 7d 0a 0a 2f  ew->ht = 0;.}../
b9ee0 2a 20 52 65 6d 6f 76 65 20 61 6c 6c 20 65 6e 74  * Remove all ent
b9ef0 72 69 65 73 20 66 72 6f 6d 20 61 20 68 61 73 68  ries from a hash
b9f00 20 74 61 62 6c 65 2e 20 20 52 65 63 6c 61 69 6d   table.  Reclaim
b9f10 20 61 6c 6c 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20   all memory..** 
b9f20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e  Call this routin
b9f30 65 20 74 6f 20 64 65 6c 65 74 65 20 61 20 68 61  e to delete a ha
b9f40 73 68 20 74 61 62 6c 65 20 6f 72 20 74 6f 20 72  sh table or to r
b9f50 65 73 65 74 20 61 20 68 61 73 68 20 74 61 62 6c  eset a hash tabl
b9f60 65 0a 2a 2a 20 74 6f 20 74 68 65 20 65 6d 70 74  e.** to the empt
b9f70 79 20 73 74 61 74 65 2e 0a 2a 2f 0a 53 51 4c 49  y state..*/.SQLI
b9f80 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
b9f90 73 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72  sqlite3HashClear
b9fa0 28 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 48 61  (Hash *pH){.  Ha
b9fb0 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20  shElem *elem;   
b9fc0 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
b9fd0 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20 65 6c  ping over all el
b9fe0 65 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 74 61  ements of the ta
b9ff0 62 6c 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74  ble */..  assert
ba000 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 65 6c 65  ( pH!=0 );.  ele
ba010 6d 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20  m = pH->first;. 
ba020 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a   pH->first = 0;.
ba030 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
ba040 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68 74  H->ht);.  pH->ht
ba050 20 3d 20 30 3b 0a 20 20 70 48 2d 3e 68 74 73 69   = 0;.  pH->htsi
ba060 7a 65 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  ze = 0;.  while(
ba070 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 48 61 73   elem ){.    Has
ba080 68 45 6c 65 6d 20 2a 6e 65 78 74 5f 65 6c 65 6d  hElem *next_elem
ba090 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
ba0a0 20 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79 4b     if( pH->copyK
ba0b0 65 79 20 26 26 20 65 6c 65 6d 2d 3e 70 4b 65 79  ey && elem->pKey
ba0c0 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
ba0d0 33 5f 66 72 65 65 28 65 6c 65 6d 2d 3e 70 4b 65  3_free(elem->pKe
ba0e0 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  y);.    }.    sq
ba0f0 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d 29  lite3_free(elem)
ba100 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20 6e 65 78  ;.    elem = nex
ba110 74 5f 65 6c 65 6d 3b 0a 20 20 7d 0a 20 20 70 48  t_elem;.  }.  pH
ba120 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a 7d 0a 0a  ->count = 0;.}..
ba130 2f 2a 0a 2a 2a 20 48 61 73 68 20 61 6e 64 20 63  /*.** Hash and c
ba140 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69  omparison functi
ba150 6f 6e 73 20 77 68 65 6e 20 74 68 65 20 6d 6f 64  ons when the mod
ba160 65 20 69 73 20 53 51 4c 49 54 45 5f 48 41 53 48  e is SQLITE_HASH
ba170 5f 53 54 52 49 4e 47 0a 2a 2f 0a 73 74 61 74 69  _STRING.*/.stati
ba180 63 20 69 6e 74 20 73 74 72 48 61 73 68 28 63 6f  c int strHash(co
ba190 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20  nst void *pKey, 
ba1a0 69 6e 74 20 6e 4b 65 79 29 7b 0a 20 20 63 6f 6e  int nKey){.  con
ba1b0 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f  st char *z = (co
ba1c0 6e 73 74 20 63 68 61 72 20 2a 29 70 4b 65 79 3b  nst char *)pKey;
ba1d0 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20 20  .  int h = 0;.  
ba1e0 69 66 28 20 6e 4b 65 79 3c 3d 30 20 29 20 6e 4b  if( nKey<=0 ) nK
ba1f0 65 79 20 3d 20 73 74 72 6c 65 6e 28 7a 29 3b 0a  ey = strlen(z);.
ba200 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20    while( nKey > 
ba210 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68  0  ){.    h = (h
ba220 3c 3c 33 29 20 5e 20 68 20 5e 20 73 71 6c 69 74  <<3) ^ h ^ sqlit
ba230 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28  e3UpperToLower[(
ba240 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 2a 7a  unsigned char)*z
ba250 2b 2b 5d 3b 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b  ++];.    nKey--;
ba260 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 20  .  }.  return h 
ba270 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 7d 0a  & 0x7fffffff;.}.
ba280 73 74 61 74 69 63 20 69 6e 74 20 73 74 72 43 6f  static int strCo
ba290 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
ba2a0 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c   *pKey1, int n1,
ba2b0 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
ba2c0 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69  y2, int n2){.  i
ba2d0 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75  f( n1!=n2 ) retu
ba2e0 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73  rn 1;.  return s
ba2f0 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28  qlite3StrNICmp((
ba300 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 4b 65 79  const char*)pKey
ba310 31 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70  1,(const char*)p
ba320 4b 65 79 32 2c 6e 31 29 3b 0a 7d 0a 0a 0a 2f 2a  Key2,n1);.}.../*
ba330 20 4c 69 6e 6b 20 61 6e 20 65 6c 65 6d 65 6e 74   Link an element
ba340 20 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74   into the hash t
ba350 61 62 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 76  able.*/.static v
ba360 6f 69 64 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e  oid insertElemen
ba370 74 28 0a 20 20 48 61 73 68 20 2a 70 48 2c 20 20  t(.  Hash *pH,  
ba380 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
ba390 68 65 20 63 6f 6d 70 6c 65 74 65 20 68 61 73 68  he complete hash
ba3a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 73 74 72 75   table */.  stru
ba3b0 63 74 20 5f 68 74 20 2a 70 45 6e 74 72 79 2c 20  ct _ht *pEntry, 
ba3c0 20 20 20 2f 2a 20 54 68 65 20 65 6e 74 72 79 20     /* The entry 
ba3d0 69 6e 74 6f 20 77 68 69 63 68 20 70 4e 65 77 20  into which pNew 
ba3e0 69 73 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20  is inserted */. 
ba3f0 20 48 61 73 68 45 6c 65 6d 20 2a 70 4e 65 77 20   HashElem *pNew 
ba400 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65          /* The e
ba410 6c 65 6d 65 6e 74 20 74 6f 20 62 65 20 69 6e 73  lement to be ins
ba420 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 48 61  erted */.){.  Ha
ba430 73 68 45 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20  shElem *pHead;  
ba440 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c       /* First el
ba450 65 6d 65 6e 74 20 61 6c 72 65 61 64 79 20 69 6e  ement already in
ba460 20 70 45 6e 74 72 79 20 2a 2f 0a 20 20 70 48 65   pEntry */.  pHe
ba470 61 64 20 3d 20 70 45 6e 74 72 79 2d 3e 63 68 61  ad = pEntry->cha
ba480 69 6e 3b 0a 20 20 69 66 28 20 70 48 65 61 64 20  in;.  if( pHead 
ba490 29 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78  ){.    pNew->nex
ba4a0 74 20 3d 20 70 48 65 61 64 3b 0a 20 20 20 20 70  t = pHead;.    p
ba4b0 4e 65 77 2d 3e 70 72 65 76 20 3d 20 70 48 65 61  New->prev = pHea
ba4c0 64 2d 3e 70 72 65 76 3b 0a 20 20 20 20 69 66 28  d->prev;.    if(
ba4d0 20 70 48 65 61 64 2d 3e 70 72 65 76 20 29 7b 20   pHead->prev ){ 
ba4e0 70 48 65 61 64 2d 3e 70 72 65 76 2d 3e 6e 65 78  pHead->prev->nex
ba4f0 74 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20 20 20  t = pNew; }.    
ba500 65 6c 73 65 20 20 20 20 20 20 20 20 20 20 20 20  else            
ba510 20 7b 20 70 48 2d 3e 66 69 72 73 74 20 3d 20 70   { pH->first = p
ba520 4e 65 77 3b 20 7d 0a 20 20 20 20 70 48 65 61 64  New; }.    pHead
ba530 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b 0a 20  ->prev = pNew;. 
ba540 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 4e 65 77   }else{.    pNew
ba550 2d 3e 6e 65 78 74 20 3d 20 70 48 2d 3e 66 69 72  ->next = pH->fir
ba560 73 74 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e  st;.    if( pH->
ba570 66 69 72 73 74 20 29 7b 20 70 48 2d 3e 66 69 72  first ){ pH->fir
ba580 73 74 2d 3e 70 72 65 76 20 3d 20 70 4e 65 77 3b  st->prev = pNew;
ba590 20 7d 0a 20 20 20 20 70 4e 65 77 2d 3e 70 72 65   }.    pNew->pre
ba5a0 76 20 3d 20 30 3b 0a 20 20 20 20 70 48 2d 3e 66  v = 0;.    pH->f
ba5b0 69 72 73 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d  irst = pNew;.  }
ba5c0 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f 75 6e 74  .  pEntry->count
ba5d0 2b 2b 3b 0a 20 20 70 45 6e 74 72 79 2d 3e 63 68  ++;.  pEntry->ch
ba5e0 61 69 6e 20 3d 20 70 4e 65 77 3b 0a 7d 0a 0a 0a  ain = pNew;.}...
ba5f0 2f 2a 20 52 65 73 69 7a 65 20 74 68 65 20 68 61  /* Resize the ha
ba600 73 68 20 74 61 62 6c 65 20 73 6f 20 74 68 61 74  sh table so that
ba610 20 69 74 20 63 61 6e 74 61 69 6e 73 20 22 6e 65   it cantains "ne
ba620 77 5f 73 69 7a 65 22 20 62 75 63 6b 65 74 73 2e  w_size" buckets.
ba630 0a 2a 2a 20 22 6e 65 77 5f 73 69 7a 65 22 20 6d  .** "new_size" m
ba640 75 73 74 20 62 65 20 61 20 70 6f 77 65 72 20 6f  ust be a power o
ba650 66 20 32 2e 20 20 54 68 65 20 68 61 73 68 20 74  f 2.  The hash t
ba660 61 62 6c 65 20 6d 69 67 68 74 20 66 61 69 6c 20  able might fail 
ba670 0a 2a 2a 20 74 6f 20 72 65 73 69 7a 65 20 69 66  .** to resize if
ba680 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
ba690 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74  ) fails..*/.stat
ba6a0 69 63 20 76 6f 69 64 20 72 65 68 61 73 68 28 48  ic void rehash(H
ba6b0 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65 77  ash *pH, int new
ba6c0 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63 74  _size){.  struct
ba6d0 20 5f 68 74 20 2a 6e 65 77 5f 68 74 3b 20 20 20   _ht *new_ht;   
ba6e0 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
ba6f0 6e 65 77 20 68 61 73 68 20 74 61 62 6c 65 20 2a  new hash table *
ba700 2f 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  /.  HashElem *el
ba710 65 6d 2c 20 2a 6e 65 78 74 5f 65 6c 65 6d 3b 20  em, *next_elem; 
ba720 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e     /* For loopin
ba730 67 20 6f 76 65 72 20 65 78 69 73 74 69 6e 67 20  g over existing 
ba740 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 0a 23 69 66  elements */..#if
ba750 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  def SQLITE_MALLO
ba760 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 20 20 69  C_SOFT_LIMIT.  i
ba770 66 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a 65  f( new_size*size
ba780 6f 66 28 73 74 72 75 63 74 20 5f 68 74 29 3e 53  of(struct _ht)>S
ba790 51 4c 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46  QLITE_MALLOC_SOF
ba7a0 54 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20 20 6e  T_LIMIT ){.    n
ba7b0 65 77 5f 73 69 7a 65 20 3d 20 53 51 4c 49 54 45  ew_size = SQLITE
ba7c0 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f 4c 49 4d  _MALLOC_SOFT_LIM
ba7d0 49 54 2f 73 69 7a 65 6f 66 28 73 74 72 75 63 74  IT/sizeof(struct
ba7e0 20 5f 68 74 29 3b 0a 20 20 7d 0a 20 20 69 66 28   _ht);.  }.  if(
ba7f0 20 6e 65 77 5f 73 69 7a 65 3d 3d 70 48 2d 3e 68   new_size==pH->h
ba800 74 73 69 7a 65 20 29 20 72 65 74 75 72 6e 3b 0a  tsize ) return;.
ba810 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 54 68 65  #endif..  /* The
ba820 72 65 20 69 73 20 61 20 63 61 6c 6c 20 74 6f 20  re is a call to 
ba830 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
ba840 20 69 6e 73 69 64 65 20 72 65 68 61 73 68 28 29   inside rehash()
ba850 2e 20 49 66 20 74 68 65 72 65 20 69 73 0a 20 20  . If there is.  
ba860 2a 2a 20 61 6c 72 65 61 64 79 20 61 6e 20 61 6c  ** already an al
ba870 6c 6f 63 61 74 69 6f 6e 20 61 74 20 70 48 2d 3e  location at pH->
ba880 68 74 2c 20 74 68 65 6e 20 69 66 20 74 68 69 73  ht, then if this
ba890 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20   malloc() fails 
ba8a0 69 74 0a 20 20 2a 2a 20 69 73 20 62 65 6e 69 67  it.  ** is benig
ba8b0 6e 20 28 73 69 6e 63 65 20 66 61 69 6c 69 6e 67  n (since failing
ba8c0 20 74 6f 20 72 65 73 69 7a 65 20 61 20 68 61 73   to resize a has
ba8d0 68 20 74 61 62 6c 65 20 69 73 20 61 20 70 65 72  h table is a per
ba8e0 66 6f 72 6d 61 6e 63 65 0a 20 20 2a 2a 20 68 69  formance.  ** hi
ba8f0 74 20 6f 6e 6c 79 2c 20 6e 6f 74 20 61 20 66 61  t only, not a fa
ba900 74 61 6c 20 65 72 72 6f 72 29 2e 0a 20 20 2a 2f  tal error)..  */
ba910 0a 20 20 69 66 28 20 70 48 2d 3e 68 74 73 69 7a  .  if( pH->htsiz
ba920 65 3e 30 20 29 20 73 71 6c 69 74 65 33 42 65 67  e>0 ) sqlite3Beg
ba930 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  inBenignMalloc()
ba940 3b 0a 20 20 6e 65 77 5f 68 74 20 3d 20 28 73 74  ;.  new_ht = (st
ba950 72 75 63 74 20 5f 68 74 20 2a 29 73 71 6c 69 74  ruct _ht *)sqlit
ba960 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 6e 65  e3MallocZero( ne
ba970 77 5f 73 69 7a 65 2a 73 69 7a 65 6f 66 28 73 74  w_size*sizeof(st
ba980 72 75 63 74 20 5f 68 74 29 20 29 3b 0a 20 20 69  ruct _ht) );.  i
ba990 66 28 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20  f( pH->htsize>0 
ba9a0 29 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69  ) sqlite3EndBeni
ba9b0 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20 69  gnMalloc();..  i
ba9c0 66 28 20 6e 65 77 5f 68 74 3d 3d 30 20 29 20 72  f( new_ht==0 ) r
ba9d0 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
ba9e0 5f 66 72 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20  _free(pH->ht);. 
ba9f0 20 70 48 2d 3e 68 74 20 3d 20 6e 65 77 5f 68 74   pH->ht = new_ht
baa00 3b 0a 20 20 70 48 2d 3e 68 74 73 69 7a 65 20 3d  ;.  pH->htsize =
baa10 20 6e 65 77 5f 73 69 7a 65 3b 0a 20 20 66 6f 72   new_size;.  for
baa20 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72 73 74 2c  (elem=pH->first,
baa30 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b 20 65 6c   pH->first=0; el
baa40 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65 78 74 5f  em; elem = next_
baa50 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e 74 20 68  elem){.    int h
baa60 20 3d 20 73 74 72 48 61 73 68 28 65 6c 65 6d 2d   = strHash(elem-
baa70 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d 3e 6e 4b 65  >pKey, elem->nKe
baa80 79 29 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31  y) & (new_size-1
baa90 29 3b 0a 20 20 20 20 6e 65 78 74 5f 65 6c 65 6d  );.    next_elem
baaa0 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20   = elem->next;. 
baab0 20 20 20 69 6e 73 65 72 74 45 6c 65 6d 65 6e 74     insertElement
baac0 28 70 48 2c 20 26 6e 65 77 5f 68 74 5b 68 5d 2c  (pH, &new_ht[h],
baad0 20 65 6c 65 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   elem);.  }.}../
baae0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
baaf0 28 66 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73  (for internal us
bab00 65 20 6f 6e 6c 79 29 20 6c 6f 63 61 74 65 73 20  e only) locates 
bab10 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 20 61 6e  an element in an
bab20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 20 74  .** hash table t
bab30 68 61 74 20 6d 61 74 63 68 65 73 20 74 68 65 20  hat matches the 
bab40 67 69 76 65 6e 20 6b 65 79 2e 20 20 54 68 65 20  given key.  The 
bab50 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b 65  hash for this ke
bab60 79 20 68 61 73 0a 2a 2a 20 61 6c 72 65 61 64 79  y has.** already
bab70 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 20 61   been computed a
bab80 6e 64 20 69 73 20 70 61 73 73 65 64 20 61 73 20  nd is passed as 
bab90 74 68 65 20 34 74 68 20 70 61 72 61 6d 65 74 65  the 4th paramete
baba0 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 48 61 73  r..*/.static Has
babb0 68 45 6c 65 6d 20 2a 66 69 6e 64 45 6c 65 6d 65  hElem *findEleme
babc0 6e 74 47 69 76 65 6e 48 61 73 68 28 0a 20 20 63  ntGivenHash(.  c
babd0 6f 6e 73 74 20 48 61 73 68 20 2a 70 48 2c 20 20  onst Hash *pH,  
babe0 20 20 20 2f 2a 20 54 68 65 20 70 48 20 74 6f 20     /* The pH to 
babf0 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20  be searched */. 
bac00 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
bac10 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79 20  y,   /* The key 
bac20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e 67  we are searching
bac30 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 4b   for */.  int nK
bac40 65 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20 20  ey,.  int h     
bac50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
bac60 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20 6b   hash for this k
bac70 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 48 61 73 68  ey. */.){.  Hash
bac80 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20  Elem *elem;     
bac90 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55 73             /* Us
baca0 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75 20  ed to loop thru 
bacb0 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73 74  the element list
bacc0 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74 3b   */.  int count;
bacd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bace0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
bacf0 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74 20  f elements left 
bad00 74 6f 20 74 65 73 74 20 2a 2f 0a 0a 20 20 69 66  to test */..  if
bad10 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20  ( pH->ht ){.    
bad20 73 74 72 75 63 74 20 5f 68 74 20 2a 70 45 6e 74  struct _ht *pEnt
bad30 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68 5d 3b  ry = &pH->ht[h];
bad40 0a 20 20 20 20 65 6c 65 6d 20 3d 20 70 45 6e 74  .    elem = pEnt
bad50 72 79 2d 3e 63 68 61 69 6e 3b 0a 20 20 20 20 63  ry->chain;.    c
bad60 6f 75 6e 74 20 3d 20 70 45 6e 74 72 79 2d 3e 63  ount = pEntry->c
bad70 6f 75 6e 74 3b 0a 20 20 20 20 77 68 69 6c 65 28  ount;.    while(
bad80 20 63 6f 75 6e 74 2d 2d 20 26 26 20 65 6c 65 6d   count-- && elem
bad90 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 74   ){.      if( st
bada0 72 43 6f 6d 70 61 72 65 28 65 6c 65 6d 2d 3e 70  rCompare(elem->p
badb0 4b 65 79 2c 65 6c 65 6d 2d 3e 6e 4b 65 79 2c 70  Key,elem->nKey,p
badc0 4b 65 79 2c 6e 4b 65 79 29 3d 3d 30 20 29 7b 20  Key,nKey)==0 ){ 
badd0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
bade0 65 6c 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  elem;.      }.  
badf0 20 20 20 20 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d      elem = elem-
bae00 3e 6e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  >next;.    }.  }
bae10 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a  .  return 0;.}..
bae20 2f 2a 20 52 65 6d 6f 76 65 20 61 20 73 69 6e 67  /* Remove a sing
bae30 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68  le entry from th
bae40 65 20 68 61 73 68 20 74 61 62 6c 65 20 67 69 76  e hash table giv
bae50 65 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  en a pointer to 
bae60 74 68 61 74 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20  that.** element 
bae70 61 6e 64 20 61 20 68 61 73 68 20 6f 6e 20 74 68  and a hash on th
bae80 65 20 65 6c 65 6d 65 6e 74 27 73 20 6b 65 79 2e  e element's key.
bae90 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
baea0 72 65 6d 6f 76 65 45 6c 65 6d 65 6e 74 47 69 76  removeElementGiv
baeb0 65 6e 48 61 73 68 28 0a 20 20 48 61 73 68 20 2a  enHash(.  Hash *
baec0 70 48 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  pH,         /* T
baed0 68 65 20 70 48 20 63 6f 6e 74 61 69 6e 69 6e 67  he pH containing
baee0 20 22 65 6c 65 6d 22 20 2a 2f 0a 20 20 48 61 73   "elem" */.  Has
baef0 68 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f  hElem* elem,   /
baf00 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f  * The element to
baf10 20 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   be removed from
baf20 20 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74   the pH */.  int
baf30 20 68 20 20 20 20 20 20 20 20 20 20 20 20 20 2f   h             /
baf40 2a 20 48 61 73 68 20 76 61 6c 75 65 20 66 6f 72  * Hash value for
baf50 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a   the element */.
baf60 29 7b 0a 20 20 73 74 72 75 63 74 20 5f 68 74 20  ){.  struct _ht 
baf70 2a 70 45 6e 74 72 79 3b 0a 20 20 69 66 28 20 65  *pEntry;.  if( e
baf80 6c 65 6d 2d 3e 70 72 65 76 20 29 7b 0a 20 20 20  lem->prev ){.   
baf90 20 65 6c 65 6d 2d 3e 70 72 65 76 2d 3e 6e 65 78   elem->prev->nex
bafa0 74 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 20  t = elem->next; 
bafb0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 48  .  }else{.    pH
bafc0 2d 3e 66 69 72 73 74 20 3d 20 65 6c 65 6d 2d 3e  ->first = elem->
bafd0 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  next;.  }.  if( 
bafe0 65 6c 65 6d 2d 3e 6e 65 78 74 20 29 7b 0a 20 20  elem->next ){.  
baff0 20 20 65 6c 65 6d 2d 3e 6e 65 78 74 2d 3e 70 72    elem->next->pr
bb000 65 76 20 3d 20 65 6c 65 6d 2d 3e 70 72 65 76 3b  ev = elem->prev;
bb010 0a 20 20 7d 0a 20 20 70 45 6e 74 72 79 20 3d 20  .  }.  pEntry = 
bb020 26 70 48 2d 3e 68 74 5b 68 5d 3b 0a 20 20 69 66  &pH->ht[h];.  if
bb030 28 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3d  ( pEntry->chain=
bb040 3d 65 6c 65 6d 20 29 7b 0a 20 20 20 20 70 45 6e  =elem ){.    pEn
bb050 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 65 6c 65  try->chain = ele
bb060 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d 0a 20 20 70  m->next;.  }.  p
bb070 45 6e 74 72 79 2d 3e 63 6f 75 6e 74 2d 2d 3b 0a  Entry->count--;.
bb080 20 20 69 66 28 20 70 45 6e 74 72 79 2d 3e 63 6f    if( pEntry->co
bb090 75 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 70 45  unt<=0 ){.    pE
bb0a0 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d 20 30 3b  ntry->chain = 0;
bb0b0 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e 63  .  }.  if( pH->c
bb0c0 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20 20 73 71  opyKey ){.    sq
bb0d0 6c 69 74 65 33 5f 66 72 65 65 28 65 6c 65 6d 2d  lite3_free(elem-
bb0e0 3e 70 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 73 71  >pKey);.  }.  sq
bb0f0 6c 69 74 65 33 5f 66 72 65 65 28 20 65 6c 65 6d  lite3_free( elem
bb100 20 29 3b 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 2d   );.  pH->count-
bb110 2d 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 75  -;.  if( pH->cou
bb120 6e 74 3c 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  nt<=0 ){.    ass
bb130 65 72 74 28 20 70 48 2d 3e 66 69 72 73 74 3d 3d  ert( pH->first==
bb140 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
bb150 20 70 48 2d 3e 63 6f 75 6e 74 3d 3d 30 20 29 3b   pH->count==0 );
bb160 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68  .    sqlite3Hash
bb170 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d 0a 7d  Clear(pH);.  }.}
bb180 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74 6f 20  ../* Attempt to 
bb190 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e  locate an elemen
bb1a0 74 20 6f 66 20 74 68 65 20 68 61 73 68 20 74 61  t of the hash ta
bb1b0 62 6c 65 20 70 48 20 77 69 74 68 20 61 20 6b 65  ble pH with a ke
bb1c0 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63 68 65  y.** that matche
bb1d0 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65  s pKey,nKey.  Re
bb1e0 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
bb1f0 6f 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  o the correspond
bb200 69 6e 67 20 0a 2a 2a 20 48 61 73 68 45 6c 65 6d  ing .** HashElem
bb210 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 74   structure for t
bb220 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69  his element if i
bb230 74 20 69 73 20 66 6f 75 6e 64 2c 20 6f 72 20 4e  t is found, or N
bb240 55 4c 4c 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65  ULL.** otherwise
bb250 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
bb260 41 54 45 20 48 61 73 68 45 6c 65 6d 20 2a 73 71  ATE HashElem *sq
bb270 6c 69 74 65 33 48 61 73 68 46 69 6e 64 45 6c 65  lite3HashFindEle
bb280 6d 28 63 6f 6e 73 74 20 48 61 73 68 20 2a 70 48  m(const Hash *pH
bb290 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
bb2a0 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20  ey, int nKey){. 
bb2b0 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
bb2c0 20 20 20 20 2f 2a 20 41 20 68 61 73 68 20 6f 6e      /* A hash on
bb2d0 20 6b 65 79 20 2a 2f 0a 20 20 48 61 73 68 45 6c   key */.  HashEl
bb2e0 65 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 2f 2a 20  em *elem;    /* 
bb2f0 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 68 61 74  The element that
bb300 20 6d 61 74 63 68 65 73 20 6b 65 79 20 2a 2f 0a   matches key */.
bb310 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c 7c 20  .  if( pH==0 || 
bb320 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65 74 75  pH->ht==0 ) retu
bb330 72 6e 20 30 3b 0a 20 20 68 20 3d 20 73 74 72 48  rn 0;.  h = strH
bb340 61 73 68 28 70 4b 65 79 2c 6e 4b 65 79 29 3b 0a  ash(pKey,nKey);.
bb350 20 20 65 6c 65 6d 20 3d 20 66 69 6e 64 45 6c 65    elem = findEle
bb360 6d 65 6e 74 47 69 76 65 6e 48 61 73 68 28 70 48  mentGivenHash(pH
bb370 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 25 20  ,pKey,nKey, h % 
bb380 70 48 2d 3e 68 74 73 69 7a 65 29 3b 0a 20 20 72  pH->htsize);.  r
bb390 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 7d 0a 0a 2f  eturn elem;.}../
bb3a0 2a 20 41 74 74 65 6d 70 74 20 74 6f 20 6c 6f 63  * Attempt to loc
bb3b0 61 74 65 20 61 6e 20 65 6c 65 6d 65 6e 74 20 6f  ate an element o
bb3c0 66 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65  f the hash table
bb3d0 20 70 48 20 77 69 74 68 20 61 20 6b 65 79 0a 2a   pH with a key.*
bb3e0 2a 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 70  * that matches p
bb3f0 4b 65 79 2c 6e 4b 65 79 2e 20 20 52 65 74 75 72  Key,nKey.  Retur
bb400 6e 20 74 68 65 20 64 61 74 61 20 66 6f 72 20 74  n the data for t
bb410 68 69 73 20 65 6c 65 6d 65 6e 74 20 69 66 20 69  his element if i
bb420 74 20 69 73 0a 2a 2a 20 66 6f 75 6e 64 2c 20 6f  t is.** found, o
bb430 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20  r NULL if there 
bb440 69 73 20 6e 6f 20 6d 61 74 63 68 2e 0a 2a 2f 0a  is no match..*/.
bb450 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
bb460 6f 69 64 20 2a 73 71 6c 69 74 65 33 48 61 73 68  oid *sqlite3Hash
bb470 46 69 6e 64 28 63 6f 6e 73 74 20 48 61 73 68 20  Find(const Hash 
bb480 2a 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  *pH, const void 
bb490 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
bb4a0 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20 2a 65 6c  {.  HashElem *el
bb4b0 65 6d 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c  em;    /* The el
bb4c0 65 6d 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68  ement that match
bb4d0 65 73 20 6b 65 79 20 2a 2f 0a 20 20 65 6c 65 6d  es key */.  elem
bb4e0 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69   = sqlite3HashFi
bb4f0 6e 64 45 6c 65 6d 28 70 48 2c 20 70 4b 65 79 2c  ndElem(pH, pKey,
bb500 20 6e 4b 65 79 29 3b 0a 20 20 72 65 74 75 72 6e   nKey);.  return
bb510 20 65 6c 65 6d 20 3f 20 65 6c 65 6d 2d 3e 64 61   elem ? elem->da
bb520 74 61 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6e  ta : 0;.}../* In
bb530 73 65 72 74 20 61 6e 20 65 6c 65 6d 65 6e 74 20  sert an element 
bb540 69 6e 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  into the hash ta
bb550 62 6c 65 20 70 48 2e 20 20 54 68 65 20 6b 65 79  ble pH.  The key
bb560 20 69 73 20 70 4b 65 79 2c 6e 4b 65 79 0a 2a 2a   is pKey,nKey.**
bb570 20 61 6e 64 20 74 68 65 20 64 61 74 61 20 69 73   and the data is
bb580 20 22 64 61 74 61 22 2e 0a 2a 2a 0a 2a 2a 20 49   "data"..**.** I
bb590 66 20 6e 6f 20 65 6c 65 6d 65 6e 74 20 65 78 69  f no element exi
bb5a0 73 74 73 20 77 69 74 68 20 61 20 6d 61 74 63 68  sts with a match
bb5b0 69 6e 67 20 6b 65 79 2c 20 74 68 65 6e 20 61 20  ing key, then a 
bb5c0 6e 65 77 0a 2a 2a 20 65 6c 65 6d 65 6e 74 20 69  new.** element i
bb5d0 73 20 63 72 65 61 74 65 64 2e 20 20 41 20 63 6f  s created.  A co
bb5e0 70 79 20 6f 66 20 74 68 65 20 6b 65 79 20 69 73  py of the key is
bb5f0 20 6d 61 64 65 20 69 66 20 74 68 65 20 63 6f 70   made if the cop
bb600 79 4b 65 79 0a 2a 2a 20 66 6c 61 67 20 69 73 20  yKey.** flag is 
bb610 73 65 74 2e 20 20 4e 55 4c 4c 20 69 73 20 72 65  set.  NULL is re
bb620 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  turned..**.** If
bb630 20 61 6e 6f 74 68 65 72 20 65 6c 65 6d 65 6e 74   another element
bb640 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 20   already exists 
bb650 77 69 74 68 20 74 68 65 20 73 61 6d 65 20 6b 65  with the same ke
bb660 79 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 6e  y, then the.** n
bb670 65 77 20 64 61 74 61 20 72 65 70 6c 61 63 65 73  ew data replaces
bb680 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 61 6e   the old data an
bb690 64 20 74 68 65 20 6f 6c 64 20 64 61 74 61 20 69  d the old data i
bb6a0 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 20 54  s returned..** T
bb6b0 68 65 20 6b 65 79 20 69 73 20 6e 6f 74 20 63 6f  he key is not co
bb6c0 70 69 65 64 20 69 6e 20 74 68 69 73 20 69 6e 73  pied in this ins
bb6d0 74 61 6e 63 65 2e 20 20 49 66 20 61 20 6d 61 6c  tance.  If a mal
bb6e0 6c 6f 63 20 66 61 69 6c 73 2c 20 74 68 65 6e 0a  loc fails, then.
bb6f0 2a 2a 20 74 68 65 20 6e 65 77 20 64 61 74 61 20  ** the new data 
bb700 69 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20  is returned and 
bb710 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 69  the hash table i
bb720 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a  s unchanged..**.
bb730 2a 2a 20 49 66 20 74 68 65 20 22 64 61 74 61 22  ** If the "data"
bb740 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 74 68   parameter to th
bb750 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 4e  is function is N
bb760 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a  ULL, then the.**
bb770 20 65 6c 65 6d 65 6e 74 20 63 6f 72 72 65 73 70   element corresp
bb780 6f 6e 64 69 6e 67 20 74 6f 20 22 6b 65 79 22 20  onding to "key" 
bb790 69 73 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  is removed from 
bb7a0 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a  the hash table..
bb7b0 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
bb7c0 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 48  E void *sqlite3H
bb7d0 61 73 68 49 6e 73 65 72 74 28 48 61 73 68 20 2a  ashInsert(Hash *
bb7e0 70 48 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  pH, const void *
bb7f0 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 2c 20  pKey, int nKey, 
bb800 76 6f 69 64 20 2a 64 61 74 61 29 7b 0a 20 20 69  void *data){.  i
bb810 6e 74 20 68 72 61 77 3b 20 20 20 20 20 20 20 20  nt hraw;        
bb820 20 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73 68       /* Raw hash
bb830 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b 65   value of the ke
bb840 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20 20  y */.  int h;   
bb850 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
bb860 74 68 65 20 68 61 73 68 20 6f 66 20 74 68 65 20  the hash of the 
bb870 6b 65 79 20 6d 6f 64 75 6c 6f 20 68 61 73 68 20  key modulo hash 
bb880 74 61 62 6c 65 20 73 69 7a 65 20 2a 2f 0a 20 20  table size */.  
bb890 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b 20  HashElem *elem; 
bb8a0 20 20 20 20 20 20 2f 2a 20 55 73 65 64 20 74 6f        /* Used to
bb8b0 20 6c 6f 6f 70 20 74 68 72 75 20 74 68 65 20 65   loop thru the e
bb8c0 6c 65 6d 65 6e 74 20 6c 69 73 74 20 2a 2f 0a 20  lement list */. 
bb8d0 20 48 61 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65   HashElem *new_e
bb8e0 6c 65 6d 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c  lem;   /* New el
bb8f0 65 6d 65 6e 74 20 61 64 64 65 64 20 74 6f 20 74  ement added to t
bb900 68 65 20 70 48 20 2a 2f 0a 0a 20 20 61 73 73 65  he pH */..  asse
bb910 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20 20 68  rt( pH!=0 );.  h
bb920 72 61 77 20 3d 20 73 74 72 48 61 73 68 28 70 4b  raw = strHash(pK
bb930 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 69 66 28  ey, nKey);.  if(
bb940 20 70 48 2d 3e 68 74 73 69 7a 65 20 29 7b 0a 20   pH->htsize ){. 
bb950 20 20 20 68 20 3d 20 68 72 61 77 20 25 20 70 48     h = hraw % pH
bb960 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 20 20 65 6c  ->htsize;.    el
bb970 65 6d 20 3d 20 66 69 6e 64 45 6c 65 6d 65 6e 74  em = findElement
bb980 47 69 76 65 6e 48 61 73 68 28 70 48 2c 70 4b 65  GivenHash(pH,pKe
bb990 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 20 20 69  y,nKey,h);.    i
bb9a0 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20  f( elem ){.     
bb9b0 20 76 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20   void *old_data 
bb9c0 3d 20 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20  = elem->data;.  
bb9d0 20 20 20 20 69 66 28 20 64 61 74 61 3d 3d 30 20      if( data==0 
bb9e0 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6d 6f 76  ){.        remov
bb9f0 65 45 6c 65 6d 65 6e 74 47 69 76 65 6e 48 61 73  eElementGivenHas
bba00 68 28 70 48 2c 65 6c 65 6d 2c 68 29 3b 0a 20 20  h(pH,elem,h);.  
bba10 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
bba20 20 20 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3d 20     elem->data = 
bba30 64 61 74 61 3b 0a 20 20 20 20 20 20 20 20 69 66  data;.        if
bba40 28 20 21 70 48 2d 3e 63 6f 70 79 4b 65 79 20 29  ( !pH->copyKey )
bba50 7b 0a 20 20 20 20 20 20 20 20 20 20 65 6c 65 6d  {.          elem
bba60 2d 3e 70 4b 65 79 20 3d 20 28 76 6f 69 64 20 2a  ->pKey = (void *
bba70 29 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 7d  )pKey;.        }
bba80 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
bba90 6e 4b 65 79 3d 3d 65 6c 65 6d 2d 3e 6e 4b 65 79  nKey==elem->nKey
bbaa0 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
bbab0 20 72 65 74 75 72 6e 20 6f 6c 64 5f 64 61 74 61   return old_data
bbac0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
bbad0 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75  ( data==0 ) retu
bbae0 72 6e 20 30 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d  rn 0;.  new_elem
bbaf0 20 3d 20 28 48 61 73 68 45 6c 65 6d 2a 29 73 71   = (HashElem*)sq
bbb00 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73 69 7a  lite3Malloc( siz
bbb10 65 6f 66 28 48 61 73 68 45 6c 65 6d 29 20 29 3b  eof(HashElem) );
bbb20 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 3d  .  if( new_elem=
bbb30 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74 61  =0 ) return data
bbb40 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70 79  ;.  if( pH->copy
bbb50 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20 29  Key && pKey!=0 )
bbb60 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e  {.    new_elem->
bbb70 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61  pKey = sqlite3Ma
bbb80 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a 20 20  lloc( nKey );.  
bbb90 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d 2d 3e    if( new_elem->
bbba0 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  pKey==0 ){.     
bbbb0 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e 65   sqlite3_free(ne
bbbc0 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72  w_elem);.      r
bbbd0 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20  eturn data;.    
bbbe0 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28 76 6f  }.    memcpy((vo
bbbf0 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b  id*)new_elem->pK
bbc00 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79 29 3b  ey, pKey, nKey);
bbc10 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 65  .  }else{.    ne
bbc20 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d 20 28  w_elem->pKey = (
bbc30 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20 7d 0a  void*)pKey;.  }.
bbc40 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b 65 79    new_elem->nKey
bbc50 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d 3e 63   = nKey;.  pH->c
bbc60 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20 70 48  ount++;.  if( pH
bbc70 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20  ->htsize==0 ){. 
bbc80 20 20 20 72 65 68 61 73 68 28 70 48 2c 20 31 32     rehash(pH, 12
bbc90 38 2f 73 69 7a 65 6f 66 28 70 48 2d 3e 68 74 5b  8/sizeof(pH->ht[
bbca0 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20 70 48  0]));.    if( pH
bbcb0 2d 3e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20  ->htsize==0 ){. 
bbcc0 20 20 20 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d       pH->count =
bbcd0 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 70 48   0;.      if( pH
bbce0 2d 3e 63 6f 70 79 4b 65 79 20 29 7b 0a 20 20 20  ->copyKey ){.   
bbcf0 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
bbd00 65 28 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79  e(new_elem->pKey
bbd10 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
bbd20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 6e 65   sqlite3_free(ne
bbd30 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20 20 72  w_elem);.      r
bbd40 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20 20 20  eturn data;.    
bbd50 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 48 2d 3e  }.  }.  if( pH->
bbd60 63 6f 75 6e 74 20 3e 20 70 48 2d 3e 68 74 73 69  count > pH->htsi
bbd70 7a 65 20 29 7b 0a 20 20 20 20 72 65 68 61 73 68  ze ){.    rehash
bbd80 28 70 48 2c 70 48 2d 3e 68 74 73 69 7a 65 2a 32  (pH,pH->htsize*2
bbd90 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  );.  }.  assert(
bbda0 20 70 48 2d 3e 68 74 73 69 7a 65 3e 30 20 29 3b   pH->htsize>0 );
bbdb0 0a 20 20 68 20 3d 20 68 72 61 77 20 25 20 70 48  .  h = hraw % pH
bbdc0 2d 3e 68 74 73 69 7a 65 3b 0a 20 20 69 6e 73 65  ->htsize;.  inse
bbdd0 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20 26 70  rtElement(pH, &p
bbde0 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f 65 6c  H->ht[h], new_el
bbdf0 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d  em);.  new_elem-
bbe00 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20  >data = data;.  
bbe10 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 2a  return 0;.}../**
bbe20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
bbe30 20 6f 66 20 68 61 73 68 2e 63 20 2a 2a 2a 2a 2a   of hash.c *****
bbe40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbe50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbe60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
bbe70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
bbe80 69 6e 20 66 69 6c 65 20 6f 70 63 6f 64 65 73 2e  in file opcodes.
bbe90 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
bbea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bbeb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20  ***********/./* 
bbec0 41 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65  Automatically ge
bbed0 6e 65 72 61 74 65 64 2e 20 20 44 6f 20 6e 6f 74  nerated.  Do not
bbee0 20 65 64 69 74 20 2a 2f 0a 2f 2a 20 53 65 65 20   edit */./* See 
bbef0 74 68 65 20 6d 6b 6f 70 63 6f 64 65 63 2e 61 77  the mkopcodec.aw
bbf00 6b 20 73 63 72 69 70 74 20 66 6f 72 20 64 65 74  k script for det
bbf10 61 69 6c 73 2e 20 2a 2f 0a 23 69 66 20 21 64 65  ails. */.#if !de
bbf20 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
bbf30 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64  T_EXPLAIN) || !d
bbf40 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 7c  efined(NDEBUG) |
bbf50 7c 20 64 65 66 69 6e 65 64 28 56 44 42 45 5f 50  | defined(VDBE_P
bbf60 52 4f 46 49 4c 45 29 20 7c 7c 20 64 65 66 69 6e  ROFILE) || defin
bbf70 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
bbf80 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
bbf90 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
bbfa0 74 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 69 6e  te3OpcodeName(in
bbfb0 74 20 69 29 7b 0a 20 73 74 61 74 69 63 20 63 6f  t i){. static co
bbfc0 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20  nst char *const 
bbfd0 61 7a 4e 61 6d 65 5b 5d 20 3d 20 7b 20 22 3f 22  azName[] = { "?"
bbfe0 2c 0a 20 20 20 20 20 2f 2a 20 20 20 31 20 2a 2f  ,.     /*   1 */
bbff0 20 22 56 4e 65 78 74 22 2c 0a 20 20 20 20 20 2f   "VNext",.     /
bc000 2a 20 20 20 32 20 2a 2f 20 22 41 66 66 69 6e 69  *   2 */ "Affini
bc010 74 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 33  ty",.     /*   3
bc020 20 2a 2f 20 22 43 6f 6c 75 6d 6e 22 2c 0a 20 20   */ "Column",.  
bc030 20 20 20 2f 2a 20 20 20 34 20 2a 2f 20 22 53 65     /*   4 */ "Se
bc040 74 43 6f 6f 6b 69 65 22 2c 0a 20 20 20 20 20 2f  tCookie",.     /
bc050 2a 20 20 20 35 20 2a 2f 20 22 53 65 71 75 65 6e  *   5 */ "Sequen
bc060 63 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 20 36  ce",.     /*   6
bc070 20 2a 2f 20 22 4d 6f 76 65 47 74 22 2c 0a 20 20   */ "MoveGt",.  
bc080 20 20 20 2f 2a 20 20 20 37 20 2a 2f 20 22 52 6f     /*   7 */ "Ro
bc090 77 4b 65 79 22 2c 0a 20 20 20 20 20 2f 2a 20 20  wKey",.     /*  
bc0a0 20 38 20 2a 2f 20 22 53 43 6f 70 79 22 2c 0a 20   8 */ "SCopy",. 
bc0b0 20 20 20 20 2f 2a 20 20 20 39 20 2a 2f 20 22 4f      /*   9 */ "O
bc0c0 70 65 6e 57 72 69 74 65 22 2c 0a 20 20 20 20 20  penWrite",.     
bc0d0 2f 2a 20 20 31 30 20 2a 2f 20 22 49 66 22 2c 0a  /*  10 */ "If",.
bc0e0 20 20 20 20 20 2f 2a 20 20 31 31 20 2a 2f 20 22       /*  11 */ "
bc0f0 56 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a  VRowid",.     /*
bc100 20 20 31 32 20 2a 2f 20 22 43 6f 6c 6c 53 65 71    12 */ "CollSeq
bc110 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 33 20 2a  ",.     /*  13 *
bc120 2f 20 22 4f 70 65 6e 52 65 61 64 22 2c 0a 20 20  / "OpenRead",.  
bc130 20 20 20 2f 2a 20 20 31 34 20 2a 2f 20 22 45 78     /*  14 */ "Ex
bc140 70 69 72 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  pire",.     /*  
bc150 31 35 20 2a 2f 20 22 41 75 74 6f 43 6f 6d 6d 69  15 */ "AutoCommi
bc160 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 36 20  t",.     /*  16 
bc170 2a 2f 20 22 4e 6f 74 22 2c 0a 20 20 20 20 20 2f  */ "Not",.     /
bc180 2a 20 20 31 37 20 2a 2f 20 22 50 61 67 65 63 6f  *  17 */ "Pageco
bc190 75 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31  unt",.     /*  1
bc1a0 38 20 2a 2f 20 22 49 6e 74 65 67 72 69 74 79 43  8 */ "IntegrityC
bc1b0 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 31 39 20  k",.     /*  19 
bc1c0 2a 2f 20 22 53 6f 72 74 22 2c 0a 20 20 20 20 20  */ "Sort",.     
bc1d0 2f 2a 20 20 32 30 20 2a 2f 20 22 43 6f 70 79 22  /*  20 */ "Copy"
bc1e0 2c 0a 20 20 20 20 20 2f 2a 20 20 32 31 20 2a 2f  ,.     /*  21 */
bc1f0 20 22 54 72 61 63 65 22 2c 0a 20 20 20 20 20 2f   "Trace",.     /
bc200 2a 20 20 32 32 20 2a 2f 20 22 46 75 6e 63 74 69  *  22 */ "Functi
bc210 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 33  on",.     /*  23
bc220 20 2a 2f 20 22 49 66 4e 65 67 22 2c 0a 20 20 20   */ "IfNeg",.   
bc230 20 20 2f 2a 20 20 32 34 20 2a 2f 20 22 4e 6f 6f    /*  24 */ "Noo
bc240 70 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 35 20  p",.     /*  25 
bc250 2a 2f 20 22 52 65 74 75 72 6e 22 2c 0a 20 20 20  */ "Return",.   
bc260 20 20 2f 2a 20 20 32 36 20 2a 2f 20 22 4e 65 77    /*  26 */ "New
bc270 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20  Rowid",.     /* 
bc280 20 32 37 20 2a 2f 20 22 56 61 72 69 61 62 6c 65   27 */ "Variable
bc290 22 2c 0a 20 20 20 20 20 2f 2a 20 20 32 38 20 2a  ",.     /*  28 *
bc2a0 2f 20 22 53 74 72 69 6e 67 22 2c 0a 20 20 20 20  / "String",.    
bc2b0 20 2f 2a 20 20 32 39 20 2a 2f 20 22 52 65 61 6c   /*  29 */ "Real
bc2c0 41 66 66 69 6e 69 74 79 22 2c 0a 20 20 20 20 20  Affinity",.     
bc2d0 2f 2a 20 20 33 30 20 2a 2f 20 22 56 52 65 6e 61  /*  30 */ "VRena
bc2e0 6d 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 31  me",.     /*  31
bc2f0 20 2a 2f 20 22 50 61 72 73 65 53 63 68 65 6d 61   */ "ParseSchema
bc300 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 32 20 2a  ",.     /*  32 *
bc310 2f 20 22 56 4f 70 65 6e 22 2c 0a 20 20 20 20 20  / "VOpen",.     
bc320 2f 2a 20 20 33 33 20 2a 2f 20 22 43 6c 6f 73 65  /*  33 */ "Close
bc330 22 2c 0a 20 20 20 20 20 2f 2a 20 20 33 34 20 2a  ",.     /*  34 *
bc340 2f 20 22 43 72 65 61 74 65 49 6e 64 65 78 22 2c  / "CreateIndex",
bc350 0a 20 20 20 20 20 2f 2a 20 20 33 35 20 2a 2f 20  .     /*  35 */ 
bc360 22 49 73 55 6e 69 71 75 65 22 2c 0a 20 20 20 20  "IsUnique",.    
bc370 20 2f 2a 20 20 33 36 20 2a 2f 20 22 4e 6f 74 46   /*  36 */ "NotF
bc380 6f 75 6e 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ound",.     /*  
bc390 33 37 20 2a 2f 20 22 49 6e 74 36 34 22 2c 0a 20  37 */ "Int64",. 
bc3a0 20 20 20 20 2f 2a 20 20 33 38 20 2a 2f 20 22 4d      /*  38 */ "M
bc3b0 75 73 74 42 65 49 6e 74 22 2c 0a 20 20 20 20 20  ustBeInt",.     
bc3c0 2f 2a 20 20 33 39 20 2a 2f 20 22 48 61 6c 74 22  /*  39 */ "Halt"
bc3d0 2c 0a 20 20 20 20 20 2f 2a 20 20 34 30 20 2a 2f  ,.     /*  40 */
bc3e0 20 22 52 6f 77 69 64 22 2c 0a 20 20 20 20 20 2f   "Rowid",.     /
bc3f0 2a 20 20 34 31 20 2a 2f 20 22 49 64 78 4c 54 22  *  41 */ "IdxLT"
bc400 2c 0a 20 20 20 20 20 2f 2a 20 20 34 32 20 2a 2f  ,.     /*  42 */
bc410 20 22 41 64 64 49 6d 6d 22 2c 0a 20 20 20 20 20   "AddImm",.     
bc420 2f 2a 20 20 34 33 20 2a 2f 20 22 53 74 61 74 65  /*  43 */ "State
bc430 6d 65 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20  ment",.     /*  
bc440 34 34 20 2a 2f 20 22 52 6f 77 44 61 74 61 22 2c  44 */ "RowData",
bc450 0a 20 20 20 20 20 2f 2a 20 20 34 35 20 2a 2f 20  .     /*  45 */ 
bc460 22 4d 65 6d 4d 61 78 22 2c 0a 20 20 20 20 20 2f  "MemMax",.     /
bc470 2a 20 20 34 36 20 2a 2f 20 22 4e 6f 74 45 78 69  *  46 */ "NotExi
bc480 73 74 73 22 2c 0a 20 20 20 20 20 2f 2a 20 20 34  sts",.     /*  4
bc490 37 20 2a 2f 20 22 47 6f 73 75 62 22 2c 0a 20 20  7 */ "Gosub",.  
bc4a0 20 20 20 2f 2a 20 20 34 38 20 2a 2f 20 22 49 6e     /*  48 */ "In
bc4b0 74 65 67 65 72 22 2c 0a 20 20 20 20 20 2f 2a 20  teger",.     /* 
bc4c0 20 34 39 20 2a 2f 20 22 50 72 65 76 22 2c 0a 20   49 */ "Prev",. 
bc4d0 20 20 20 20 2f 2a 20 20 35 30 20 2a 2f 20 22 56      /*  50 */ "V
bc4e0 43 6f 6c 75 6d 6e 22 2c 0a 20 20 20 20 20 2f 2a  Column",.     /*
bc4f0 20 20 35 31 20 2a 2f 20 22 43 72 65 61 74 65 54    51 */ "CreateT
bc500 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  able",.     /*  
bc510 35 32 20 2a 2f 20 22 4c 61 73 74 22 2c 0a 20 20  52 */ "Last",.  
bc520 20 20 20 2f 2a 20 20 35 33 20 2a 2f 20 22 49 6e     /*  53 */ "In
bc530 63 72 56 61 63 75 75 6d 22 2c 0a 20 20 20 20 20  crVacuum",.     
bc540 2f 2a 20 20 35 34 20 2a 2f 20 22 49 64 78 52 6f  /*  54 */ "IdxRo
bc550 77 69 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35  wid",.     /*  5
bc560 35 20 2a 2f 20 22 52 65 73 65 74 43 6f 75 6e 74  5 */ "ResetCount
bc570 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 36 20 2a  ",.     /*  56 *
bc580 2f 20 22 46 69 66 6f 57 72 69 74 65 22 2c 0a 20  / "FifoWrite",. 
bc590 20 20 20 20 2f 2a 20 20 35 37 20 2a 2f 20 22 43      /*  57 */ "C
bc5a0 6f 6e 74 65 78 74 50 75 73 68 22 2c 0a 20 20 20  ontextPush",.   
bc5b0 20 20 2f 2a 20 20 35 38 20 2a 2f 20 22 59 69 65    /*  58 */ "Yie
bc5c0 6c 64 22 2c 0a 20 20 20 20 20 2f 2a 20 20 35 39  ld",.     /*  59
bc5d0 20 2a 2f 20 22 44 72 6f 70 54 72 69 67 67 65 72   */ "DropTrigger
bc5e0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36 30 20 2a  ",.     /*  60 *
bc5f0 2f 20 22 4f 72 22 2c 0a 20 20 20 20 20 2f 2a 20  / "Or",.     /* 
bc600 20 36 31 20 2a 2f 20 22 41 6e 64 22 2c 0a 20 20   61 */ "And",.  
bc610 20 20 20 2f 2a 20 20 36 32 20 2a 2f 20 22 44 72     /*  62 */ "Dr
bc620 6f 70 49 6e 64 65 78 22 2c 0a 20 20 20 20 20 2f  opIndex",.     /
bc630 2a 20 20 36 33 20 2a 2f 20 22 49 64 78 47 45 22  *  63 */ "IdxGE"
bc640 2c 0a 20 20 20 20 20 2f 2a 20 20 36 34 20 2a 2f  ,.     /*  64 */
bc650 20 22 49 64 78 44 65 6c 65 74 65 22 2c 0a 20 20   "IdxDelete",.  
bc660 20 20 20 2f 2a 20 20 36 35 20 2a 2f 20 22 49 73     /*  65 */ "Is
bc670 4e 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 20  Null",.     /*  
bc680 36 36 20 2a 2f 20 22 4e 6f 74 4e 75 6c 6c 22 2c  66 */ "NotNull",
bc690 0a 20 20 20 20 20 2f 2a 20 20 36 37 20 2a 2f 20  .     /*  67 */ 
bc6a0 22 4e 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 36  "Ne",.     /*  6
bc6b0 38 20 2a 2f 20 22 45 71 22 2c 0a 20 20 20 20 20  8 */ "Eq",.     
bc6c0 2f 2a 20 20 36 39 20 2a 2f 20 22 47 74 22 2c 0a  /*  69 */ "Gt",.
bc6d0 20 20 20 20 20 2f 2a 20 20 37 30 20 2a 2f 20 22       /*  70 */ "
bc6e0 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 31  Le",.     /*  71
bc6f0 20 2a 2f 20 22 4c 74 22 2c 0a 20 20 20 20 20 2f   */ "Lt",.     /
bc700 2a 20 20 37 32 20 2a 2f 20 22 47 65 22 2c 0a 20  *  72 */ "Ge",. 
bc710 20 20 20 20 2f 2a 20 20 37 33 20 2a 2f 20 22 56      /*  73 */ "V
bc720 61 63 75 75 6d 22 2c 0a 20 20 20 20 20 2f 2a 20  acuum",.     /* 
bc730 20 37 34 20 2a 2f 20 22 42 69 74 41 6e 64 22 2c   74 */ "BitAnd",
bc740 0a 20 20 20 20 20 2f 2a 20 20 37 35 20 2a 2f 20  .     /*  75 */ 
bc750 22 42 69 74 4f 72 22 2c 0a 20 20 20 20 20 2f 2a  "BitOr",.     /*
bc760 20 20 37 36 20 2a 2f 20 22 53 68 69 66 74 4c 65    76 */ "ShiftLe
bc770 66 74 22 2c 0a 20 20 20 20 20 2f 2a 20 20 37 37  ft",.     /*  77
bc780 20 2a 2f 20 22 53 68 69 66 74 52 69 67 68 74 22   */ "ShiftRight"
bc790 2c 0a 20 20 20 20 20 2f 2a 20 20 37 38 20 2a 2f  ,.     /*  78 */
bc7a0 20 22 41 64 64 22 2c 0a 20 20 20 20 20 2f 2a 20   "Add",.     /* 
bc7b0 20 37 39 20 2a 2f 20 22 53 75 62 74 72 61 63 74   79 */ "Subtract
bc7c0 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 30 20 2a  ",.     /*  80 *
bc7d0 2f 20 22 4d 75 6c 74 69 70 6c 79 22 2c 0a 20 20  / "Multiply",.  
bc7e0 20 20 20 2f 2a 20 20 38 31 20 2a 2f 20 22 44 69     /*  81 */ "Di
bc7f0 76 69 64 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20  vide",.     /*  
bc800 38 32 20 2a 2f 20 22 52 65 6d 61 69 6e 64 65 72  82 */ "Remainder
bc810 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 33 20 2a  ",.     /*  83 *
bc820 2f 20 22 43 6f 6e 63 61 74 22 2c 0a 20 20 20 20  / "Concat",.    
bc830 20 2f 2a 20 20 38 34 20 2a 2f 20 22 4d 6f 76 65   /*  84 */ "Move
bc840 4c 65 22 2c 0a 20 20 20 20 20 2f 2a 20 20 38 35  Le",.     /*  85
bc850 20 2a 2f 20 22 49 66 4e 6f 74 22 2c 0a 20 20 20   */ "IfNot",.   
bc860 20 20 2f 2a 20 20 38 36 20 2a 2f 20 22 44 72 6f    /*  86 */ "Dro
bc870 70 54 61 62 6c 65 22 2c 0a 20 20 20 20 20 2f 2a  pTable",.     /*
bc880 20 20 38 37 20 2a 2f 20 22 42 69 74 4e 6f 74 22    87 */ "BitNot"
bc890 2c 0a 20 20 20 20 20 2f 2a 20 20 38 38 20 2a 2f  ,.     /*  88 */
bc8a0 20 22 53 74 72 69 6e 67 38 22 2c 0a 20 20 20 20   "String8",.    
bc8b0 20 2f 2a 20 20 38 39 20 2a 2f 20 22 4d 61 6b 65   /*  89 */ "Make
bc8c0 52 65 63 6f 72 64 22 2c 0a 20 20 20 20 20 2f 2a  Record",.     /*
bc8d0 20 20 39 30 20 2a 2f 20 22 52 65 73 75 6c 74 52    90 */ "ResultR
bc8e0 6f 77 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 31  ow",.     /*  91
bc8f0 20 2a 2f 20 22 44 65 6c 65 74 65 22 2c 0a 20 20   */ "Delete",.  
bc900 20 20 20 2f 2a 20 20 39 32 20 2a 2f 20 22 41 67     /*  92 */ "Ag
bc910 67 46 69 6e 61 6c 22 2c 0a 20 20 20 20 20 2f 2a  gFinal",.     /*
bc920 20 20 39 33 20 2a 2f 20 22 43 6f 6d 70 61 72 65    93 */ "Compare
bc930 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39 34 20 2a  ",.     /*  94 *
bc940 2f 20 22 47 6f 74 6f 22 2c 0a 20 20 20 20 20 2f  / "Goto",.     /
bc950 2a 20 20 39 35 20 2a 2f 20 22 54 61 62 6c 65 4c  *  95 */ "TableL
bc960 6f 63 6b 22 2c 0a 20 20 20 20 20 2f 2a 20 20 39  ock",.     /*  9
bc970 36 20 2a 2f 20 22 46 69 66 6f 52 65 61 64 22 2c  6 */ "FifoRead",
bc980 0a 20 20 20 20 20 2f 2a 20 20 39 37 20 2a 2f 20  .     /*  97 */ 
bc990 22 43 6c 65 61 72 22 2c 0a 20 20 20 20 20 2f 2a  "Clear",.     /*
bc9a0 20 20 39 38 20 2a 2f 20 22 4d 6f 76 65 4c 74 22    98 */ "MoveLt"
bc9b0 2c 0a 20 20 20 20 20 2f 2a 20 20 39 39 20 2a 2f  ,.     /*  99 */
bc9c0 20 22 56 65 72 69 66 79 43 6f 6f 6b 69 65 22 2c   "VerifyCookie",
bc9d0 0a 20 20 20 20 20 2f 2a 20 31 30 30 20 2a 2f 20  .     /* 100 */ 
bc9e0 22 41 67 67 53 74 65 70 22 2c 0a 20 20 20 20 20  "AggStep",.     
bc9f0 2f 2a 20 31 30 31 20 2a 2f 20 22 53 65 74 4e 75  /* 101 */ "SetNu
bca00 6d 43 6f 6c 75 6d 6e 73 22 2c 0a 20 20 20 20 20  mColumns",.     
bca10 2f 2a 20 31 30 32 20 2a 2f 20 22 54 72 61 6e 73  /* 102 */ "Trans
bca20 61 63 74 69 6f 6e 22 2c 0a 20 20 20 20 20 2f 2a  action",.     /*
bca30 20 31 30 33 20 2a 2f 20 22 56 46 69 6c 74 65 72   103 */ "VFilter
bca40 22 2c 0a 20 20 20 20 20 2f 2a 20 31 30 34 20 2a  ",.     /* 104 *
bca50 2f 20 22 56 44 65 73 74 72 6f 79 22 2c 0a 20 20  / "VDestroy",.  
bca60 20 20 20 2f 2a 20 31 30 35 20 2a 2f 20 22 43 6f     /* 105 */ "Co
bca70 6e 74 65 78 74 50 6f 70 22 2c 0a 20 20 20 20 20  ntextPop",.     
bca80 2f 2a 20 31 30 36 20 2a 2f 20 22 4e 65 78 74 22  /* 106 */ "Next"
bca90 2c 0a 20 20 20 20 20 2f 2a 20 31 30 37 20 2a 2f  ,.     /* 107 */
bcaa0 20 22 49 64 78 49 6e 73 65 72 74 22 2c 0a 20 20   "IdxInsert",.  
bcab0 20 20 20 2f 2a 20 31 30 38 20 2a 2f 20 22 49 6e     /* 108 */ "In
bcac0 73 65 72 74 22 2c 0a 20 20 20 20 20 2f 2a 20 31  sert",.     /* 1
bcad0 30 39 20 2a 2f 20 22 44 65 73 74 72 6f 79 22 2c  09 */ "Destroy",
bcae0 0a 20 20 20 20 20 2f 2a 20 31 31 30 20 2a 2f 20  .     /* 110 */ 
bcaf0 22 52 65 61 64 43 6f 6f 6b 69 65 22 2c 0a 20 20  "ReadCookie",.  
bcb00 20 20 20 2f 2a 20 31 31 31 20 2a 2f 20 22 46 6f     /* 111 */ "Fo
bcb10 72 63 65 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a  rceInt",.     /*
bcb20 20 31 31 32 20 2a 2f 20 22 4c 6f 61 64 41 6e 61   112 */ "LoadAna
bcb30 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 2f 2a 20  lysis",.     /* 
bcb40 31 31 33 20 2a 2f 20 22 45 78 70 6c 61 69 6e 22  113 */ "Explain"
bcb50 2c 0a 20 20 20 20 20 2f 2a 20 31 31 34 20 2a 2f  ,.     /* 114 */
bcb60 20 22 4f 70 65 6e 50 73 65 75 64 6f 22 2c 0a 20   "OpenPseudo",. 
bcb70 20 20 20 20 2f 2a 20 31 31 35 20 2a 2f 20 22 4f      /* 115 */ "O
bcb80 70 65 6e 45 70 68 65 6d 65 72 61 6c 22 2c 0a 20  penEphemeral",. 
bcb90 20 20 20 20 2f 2a 20 31 31 36 20 2a 2f 20 22 4e      /* 116 */ "N
bcba0 75 6c 6c 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31  ull",.     /* 11
bcbb0 37 20 2a 2f 20 22 4d 6f 76 65 22 2c 0a 20 20 20  7 */ "Move",.   
bcbc0 20 20 2f 2a 20 31 31 38 20 2a 2f 20 22 42 6c 6f    /* 118 */ "Blo
bcbd0 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 31 39 20  b",.     /* 119 
bcbe0 2a 2f 20 22 52 65 77 69 6e 64 22 2c 0a 20 20 20  */ "Rewind",.   
bcbf0 20 20 2f 2a 20 31 32 30 20 2a 2f 20 22 4d 6f 76    /* 120 */ "Mov
bcc00 65 47 65 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32  eGe",.     /* 12
bcc10 31 20 2a 2f 20 22 56 42 65 67 69 6e 22 2c 0a 20  1 */ "VBegin",. 
bcc20 20 20 20 20 2f 2a 20 31 32 32 20 2a 2f 20 22 56      /* 122 */ "V
bcc30 55 70 64 61 74 65 22 2c 0a 20 20 20 20 20 2f 2a  Update",.     /*
bcc40 20 31 32 33 20 2a 2f 20 22 49 66 5a 65 72 6f 22   123 */ "IfZero"
bcc50 2c 0a 20 20 20 20 20 2f 2a 20 31 32 34 20 2a 2f  ,.     /* 124 */
bcc60 20 22 56 43 72 65 61 74 65 22 2c 0a 20 20 20 20   "VCreate",.    
bcc70 20 2f 2a 20 31 32 35 20 2a 2f 20 22 46 6f 75 6e   /* 125 */ "Foun
bcc80 64 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 36 20  d",.     /* 126 
bcc90 2a 2f 20 22 52 65 61 6c 22 2c 0a 20 20 20 20 20  */ "Real",.     
bcca0 2f 2a 20 31 32 37 20 2a 2f 20 22 49 66 50 6f 73  /* 127 */ "IfPos
bccb0 22 2c 0a 20 20 20 20 20 2f 2a 20 31 32 38 20 2a  ",.     /* 128 *
bccc0 2f 20 22 4e 75 6c 6c 52 6f 77 22 2c 0a 20 20 20  / "NullRow",.   
bccd0 20 20 2f 2a 20 31 32 39 20 2a 2f 20 22 4a 75 6d    /* 129 */ "Jum
bcce0 70 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 30 20  p",.     /* 130 
bccf0 2a 2f 20 22 50 65 72 6d 75 74 61 74 69 6f 6e 22  */ "Permutation"
bcd00 2c 0a 20 20 20 20 20 2f 2a 20 31 33 31 20 2a 2f  ,.     /* 131 */
bcd10 20 22 4e 6f 74 55 73 65 64 5f 31 33 31 22 2c 0a   "NotUsed_131",.
bcd20 20 20 20 20 20 2f 2a 20 31 33 32 20 2a 2f 20 22       /* 132 */ "
bcd30 4e 6f 74 55 73 65 64 5f 31 33 32 22 2c 0a 20 20  NotUsed_132",.  
bcd40 20 20 20 2f 2a 20 31 33 33 20 2a 2f 20 22 4e 6f     /* 133 */ "No
bcd50 74 55 73 65 64 5f 31 33 33 22 2c 0a 20 20 20 20  tUsed_133",.    
bcd60 20 2f 2a 20 31 33 34 20 2a 2f 20 22 4e 6f 74 55   /* 134 */ "NotU
bcd70 73 65 64 5f 31 33 34 22 2c 0a 20 20 20 20 20 2f  sed_134",.     /
bcd80 2a 20 31 33 35 20 2a 2f 20 22 4e 6f 74 55 73 65  * 135 */ "NotUse
bcd90 64 5f 31 33 35 22 2c 0a 20 20 20 20 20 2f 2a 20  d_135",.     /* 
bcda0 31 33 36 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f  136 */ "NotUsed_
bcdb0 31 33 36 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33  136",.     /* 13
bcdc0 37 20 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33  7 */ "NotUsed_13
bcdd0 37 22 2c 0a 20 20 20 20 20 2f 2a 20 31 33 38 20  7",.     /* 138 
bcde0 2a 2f 20 22 4e 6f 74 55 73 65 64 5f 31 33 38 22  */ "NotUsed_138"
bcdf0 2c 0a 20 20 20 20 20 2f 2a 20 31 33 39 20 2a 2f  ,.     /* 139 */
bce00 20 22 54 6f 54 65 78 74 22 2c 0a 20 20 20 20 20   "ToText",.     
bce10 2f 2a 20 31 34 30 20 2a 2f 20 22 54 6f 42 6c 6f  /* 140 */ "ToBlo
bce20 62 22 2c 0a 20 20 20 20 20 2f 2a 20 31 34 31 20  b",.     /* 141 
bce30 2a 2f 20 22 54 6f 4e 75 6d 65 72 69 63 22 2c 0a  */ "ToNumeric",.
bce40 20 20 20 20 20 2f 2a 20 31 34 32 20 2a 2f 20 22       /* 142 */ "
bce50 54 6f 49 6e 74 22 2c 0a 20 20 20 20 20 2f 2a 20  ToInt",.     /* 
bce60 31 34 33 20 2a 2f 20 22 54 6f 52 65 61 6c 22 2c  143 */ "ToReal",
bce70 0a 20 20 7d 3b 0a 20 20 72 65 74 75 72 6e 20 61  .  };.  return a
bce80 7a 4e 61 6d 65 5b 69 5d 3b 0a 7d 0a 23 65 6e 64  zName[i];.}.#end
bce90 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  if../***********
bcea0 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 70 63 6f 64  *** End of opcod
bceb0 65 73 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.c ***********
bcec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bced0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcee0 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
bcef0 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
bcf00 73 5f 6f 73 32 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  s_os2.c ********
bcf10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bcf30 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 46  **/./*.** 2006 F
bcf40 65 62 20 31 34 0a 2a 2a 0a 2a 2a 20 54 68 65 20  eb 14.**.** The 
bcf50 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
bcf60 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
bcf70 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
bcf80 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
bcf90 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
bcfa0 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
bcfb0 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
bcfc0 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
bcfd0 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
bcfe0 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
bcff0 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
bd000 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
bd010 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
bd020 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
bd030 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
bd040 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
bd050 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
bd060 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd070 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd0a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
bd0b0 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
bd0c0 6e 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20  ns code that is 
bd0d0 73 70 65 63 69 66 69 63 20 74 6f 20 4f 53 2f 32  specific to OS/2
bd0e0 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f 73 5f  ..**.** $Id: os_
bd0f0 6f 73 32 2e 63 2c 76 20 31 2e 35 39 20 32 30 30  os2.c,v 1.59 200
bd100 38 2f 31 31 2f 31 38 20 32 33 3a 30 33 3a 34 30  8/11/18 23:03:40
bd110 20 70 77 65 69 6c 62 61 63 68 65 72 20 45 78 70   pweilbacher Exp
bd120 20 24 0a 2a 2f 0a 0a 0a 23 69 66 20 53 51 4c 49   $.*/...#if SQLI
bd130 54 45 5f 4f 53 5f 4f 53 32 0a 0a 2f 2a 0a 2a 2a  TE_OS_OS2../*.**
bd140 20 41 20 4e 6f 74 65 20 41 62 6f 75 74 20 4d 65   A Note About Me
bd150 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69 6f 6e 3a  mory Allocation:
bd160 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64 72 69 76  .**.** This driv
bd170 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f 63 28 29  er uses malloc()
bd180 2f 66 72 65 65 28 29 20 64 69 72 65 63 74 6c 79  /free() directly
bd190 20 72 61 74 68 65 72 20 74 68 61 6e 20 67 6f 69   rather than goi
bd1a0 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 68  ng through.** th
bd1b0 65 20 53 51 4c 69 74 65 2d 77 72 61 70 70 65 72  e SQLite-wrapper
bd1c0 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  s sqlite3_malloc
bd1d0 28 29 2f 73 71 6c 69 74 65 33 5f 66 72 65 65 28  ()/sqlite3_free(
bd1e0 29 2e 20 20 54 68 6f 73 65 20 77 72 61 70 70 65  ).  Those wrappe
bd1f0 72 73 0a 2a 2a 20 61 72 65 20 64 65 73 69 67 6e  rs.** are design
bd200 65 64 20 66 6f 72 20 75 73 65 20 6f 6e 20 65 6d  ed for use on em
bd210 62 65 64 64 65 64 20 73 79 73 74 65 6d 73 20 77  bedded systems w
bd220 68 65 72 65 20 6d 65 6d 6f 72 79 20 69 73 20 73  here memory is s
bd230 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20 6d 61 6c  carce and.** mal
bd240 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 68 61 70  loc failures hap
bd250 70 65 6e 20 66 72 65 71 75 65 6e 74 6c 79 2e 20  pen frequently. 
bd260 20 4f 53 2f 32 20 64 6f 65 73 20 6e 6f 74 20 74   OS/2 does not t
bd270 79 70 69 63 61 6c 6c 79 20 72 75 6e 20 6f 6e 0a  ypically run on.
bd280 2a 2a 20 65 6d 62 65 64 64 65 64 20 73 79 73 74  ** embedded syst
bd290 65 6d 73 2c 20 61 6e 64 20 77 68 65 6e 20 69 74  ems, and when it
bd2a0 20 64 6f 65 73 20 74 68 65 20 64 65 76 65 6c 6f   does the develo
bd2b0 70 65 72 73 20 6e 6f 72 6d 61 6c 6c 79 20 68 61  pers normally ha
bd2c0 76 65 20 62 69 67 67 65 72 0a 2a 2a 20 70 72 6f  ve bigger.** pro
bd2d0 62 6c 65 6d 73 20 74 6f 20 77 6f 72 72 79 20 61  blems to worry a
bd2e0 62 6f 75 74 20 74 68 61 6e 20 72 75 6e 6e 69 6e  bout than runnin
bd2f0 67 20 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e  g out of memory.
bd300 20 20 53 6f 20 74 68 65 72 65 20 69 73 20 6e 6f    So there is no
bd310 74 0a 2a 2a 20 61 20 63 6f 6d 70 65 6c 6c 69 6e  t.** a compellin
bd320 67 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68  g need to use th
bd330 65 20 77 72 61 70 70 65 72 73 2e 0a 2a 2a 0a 2a  e wrappers..**.*
bd340 2a 20 42 75 74 20 74 68 65 72 65 20 69 73 20 61  * But there is a
bd350 20 67 6f 6f 64 20 72 65 61 73 6f 6e 20 74 6f 20   good reason to 
bd360 6e 6f 74 20 75 73 65 20 74 68 65 20 77 72 61 70  not use the wrap
bd370 70 65 72 73 2e 20 20 49 66 20 77 65 20 75 73 65  pers.  If we use
bd380 20 74 68 65 0a 2a 2a 20 77 72 61 70 70 65 72 73   the.** wrappers
bd390 20 74 68 65 6e 20 77 65 20 77 69 6c 6c 20 67 65   then we will ge
bd3a0 74 20 73 69 6d 75 6c 61 74 65 64 20 6d 61 6c 6c  t simulated mall
bd3b0 6f 63 28 29 20 66 61 69 6c 75 72 65 73 20 77 69  oc() failures wi
bd3c0 74 68 69 6e 20 74 68 69 73 0a 2a 2a 20 64 72 69  thin this.** dri
bd3d0 76 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 63  ver.  And that c
bd3e0 61 75 73 65 73 20 61 6c 6c 20 6b 69 6e 64 73 20  auses all kinds 
bd3f0 6f 66 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20  of problems for 
bd400 6f 75 72 20 74 65 73 74 73 2e 20 20 57 65 0a 2a  our tests.  We.*
bd410 2a 20 63 6f 75 6c 64 20 65 6e 68 61 6e 63 65 20  * could enhance 
bd420 53 51 4c 69 74 65 20 74 6f 20 64 65 61 6c 20 77  SQLite to deal w
bd430 69 74 68 20 73 69 6d 75 6c 61 74 65 64 20 6d 61  ith simulated ma
bd440 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20 77 69  lloc failures wi
bd450 74 68 69 6e 0a 2a 2a 20 74 68 65 20 4f 53 20 64  thin.** the OS d
bd460 72 69 76 65 72 2c 20 62 75 74 20 74 68 65 20 63  river, but the c
bd470 6f 64 65 20 74 6f 20 64 65 61 6c 20 77 69 74 68  ode to deal with
bd480 20 74 68 6f 73 65 20 66 61 69 6c 75 72 65 20 77   those failure w
bd490 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65 20 65  ould not.** be e
bd4a0 78 65 72 63 69 73 65 64 20 6f 6e 20 4c 69 6e 75  xercised on Linu
bd4b0 78 20 28 77 68 69 63 68 20 64 6f 65 73 20 6e 6f  x (which does no
bd4c0 74 20 6e 65 65 64 20 74 6f 20 6d 61 6c 6c 6f 63  t need to malloc
bd4d0 28 29 20 69 6e 20 74 68 65 20 64 72 69 76 65 72  () in the driver
bd4e0 29 0a 2a 2a 20 61 6e 64 20 73 6f 20 77 65 20 77  ).** and so we w
bd4f0 6f 75 6c 64 20 68 61 76 65 20 64 69 66 66 69 63  ould have diffic
bd500 75 6c 74 79 20 77 72 69 74 69 6e 67 20 63 6f 76  ulty writing cov
bd510 65 72 61 67 65 20 74 65 73 74 73 20 66 6f 72 20  erage tests for 
bd520 74 68 61 74 0a 2a 2a 20 63 6f 64 65 2e 20 20 42  that.** code.  B
bd530 65 74 74 65 72 20 74 6f 20 6c 65 61 76 65 20 74  etter to leave t
bd540 68 65 20 63 6f 64 65 20 6f 75 74 2c 20 77 65 20  he code out, we 
bd550 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  think..**.** The
bd560 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 64   point of this d
bd570 69 73 63 75 73 73 69 6f 6e 20 69 73 20 61 73 20  iscussion is as 
bd580 66 6f 6c 6c 6f 77 73 3a 20 20 57 68 65 6e 20 63  follows:  When c
bd590 72 65 61 74 69 6e 67 20 61 20 6e 65 77 0a 2a 2a  reating a new.**
bd5a0 20 4f 53 20 6c 61 79 65 72 20 66 6f 72 20 61 6e   OS layer for an
bd5b0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
bd5c0 2c 20 69 66 20 79 6f 75 20 75 73 65 20 74 68 69  , if you use thi
bd5d0 73 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61  s file as an exa
bd5e0 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f 69 64 20 74  mple,.** avoid t
bd5f0 68 65 20 75 73 65 20 6f 66 20 6d 61 6c 6c 6f 63  he use of malloc
bd600 28 29 2f 66 72 65 65 28 29 2e 20 20 54 68 6f 73  ()/free().  Thos
bd610 65 20 72 6f 75 74 69 6e 65 73 20 77 6f 72 6b 20  e routines work 
bd620 6f 6b 20 6f 6e 20 4f 53 2f 32 0a 2a 2a 20 64 65  ok on OS/2.** de
bd630 73 6b 74 6f 70 73 20 62 75 74 20 6e 6f 74 20 73  sktops but not s
bd640 6f 20 77 65 6c 6c 20 69 6e 20 65 6d 62 65 64 64  o well in embedd
bd650 65 64 20 73 79 73 74 65 6d 73 2e 0a 2a 2f 0a 0a  ed systems..*/..
bd660 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65  /*.** Macros use
bd670 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
bd680 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f  hether or not to
bd690 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f   use threads..*/
bd6a0 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
bd6b0 49 54 45 5f 54 48 52 45 41 44 53 41 46 45 29 20  ITE_THREADSAFE) 
bd6c0 26 26 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  && SQLITE_THREAD
bd6d0 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20 53 51  SAFE.# define SQ
bd6e0 4c 49 54 45 5f 4f 53 32 5f 54 48 52 45 41 44 53  LITE_OS2_THREADS
bd6f0 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   1.#endif../*.**
bd700 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68   Include code th
bd710 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20  at is common to 
bd720 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73  all os_*.c files
bd730 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .*/./***********
bd740 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63  *** Include os_c
bd750 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d  ommon.h in the m
bd760 69 64 64 6c 65 20 6f 66 20 6f 73 5f 6f 73 32 2e  iddle of os_os2.
bd770 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
bd780 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
bd790 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f  *** Begin file o
bd7a0 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a  s_common.h *****
bd7b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd7d0 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d  **/./*.** 2004 M
bd7e0 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ay 22.**.** The 
bd7f0 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
bd800 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
bd810 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
bd820 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
bd830 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
bd840 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
bd850 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
bd860 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
bd870 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
bd880 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
bd890 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
bd8a0 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
bd8b0 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
bd8c0 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
bd8d0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
bd8e0 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
bd8f0 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
bd900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd920 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd930 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bd940 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
bd950 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
bd960 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20  ns macros and a 
bd970 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f  little bit of co
bd980 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f  de that is commo
bd990 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74  n to.** all of t
bd9a0 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63  he platform-spec
bd9b0 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a  ific files (os_*
bd9c0 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c  .c) and is #incl
bd9d0 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a  uded into those.
bd9e0 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20  ** files..**.** 
bd9f0 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64  This file should
bda00 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79   be #included by
bda10 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65   the os_*.c file
bda20 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e  s only.  It is n
bda30 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20  ot a.** general 
bda40 70 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66  purpose header f
bda50 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ile..**.** $Id: 
bda60 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e  os_common.h,v 1.
bda70 33 37 20 32 30 30 38 2f 30 35 2f 32 39 20 32 30  37 2008/05/29 20
bda80 3a 32 32 3a 33 37 20 73 68 61 6e 65 20 45 78 70  :22:37 shane Exp
bda90 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f   $.*/.#ifndef _O
bdaa0 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66  S_COMMON_H_.#def
bdab0 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ine _OS_COMMON_H
bdac0 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73  _../*.** At leas
bdad0 74 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20  t two bugs have 
bdae0 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75  slipped in becau
bdaf0 73 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68  se we changed th
bdb00 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a  e MEMORY_DEBUG.*
bdb10 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54  * macro to SQLIT
bdb20 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65  E_DEBUG and some
bdb30 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73   older makefiles
bdb40 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61   have not yet ma
bdb50 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68  de the.** switch
bdb60 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
bdb70 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74   code should cat
bdb80 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20  ch this problem 
bdb90 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e  at compile-time.
bdba0 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52  .*/.#ifdef MEMOR
bdbb0 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20  Y_DEBUG.# error 
bdbc0 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55  "The MEMORY_DEBU
bdbd0 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c  G macro is obsol
bdbe0 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45  ete.  Use SQLITE
bdbf0 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22  _DEBUG instead."
bdc00 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20  .#endif.../*. * 
bdc10 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68  When testing, th
bdc20 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  is global variab
bdc30 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f  le stores the lo
bdc40 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a  cation of the. *
bdc50 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e   pending-byte in
bdc60 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
bdc70 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53  le.. */.#ifdef S
bdc80 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
bdc90 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 64 20 69  E_API unsigned i
bdca0 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69  nt sqlite3_pendi
bdcb0 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 30 30 30  ng_byte = 0x4000
bdcc0 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69  0000;.#endif..#i
bdcd0 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
bdce0 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  G.SQLITE_PRIVATE
bdcf0 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72   int sqlite3OSTr
bdd00 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  ace = 0;.#define
bdd10 20 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20   OSTRACE1(X)    
bdd20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
bdd30 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
bdd40 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a  3DebugPrintf(X).
bdd50 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32  #define OSTRACE2
bdd60 28 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20  (X,Y)       if( 
bdd70 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
bdd80 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
bdd90 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65  ntf(X,Y).#define
bdda0 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29   OSTRACE3(X,Y,Z)
bddb0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
bddc0 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  OSTrace ) sqlite
bddd0 33 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59  3DebugPrintf(X,Y
bdde0 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Z).#define OSTR
bddf0 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20  ACE4(X,Y,Z,A)   
bde00 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61  if( sqlite3OSTra
bde10 63 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75  ce ) sqlite3Debu
bde20 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29  gPrintf(X,Y,Z,A)
bde30 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
bde40 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28  5(X,Y,Z,A,B) if(
bde50 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20   sqlite3OSTrace 
bde60 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
bde70 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a  intf(X,Y,Z,A,B).
bde80 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36  #define OSTRACE6
bde90 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a  (X,Y,Z,A,B,C) \.
bdea0 20 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53      if(sqlite3OS
bdeb0 54 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65  Trace) sqlite3De
bdec0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
bded0 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f  A,B,C).#define O
bdee0 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c  STRACE7(X,Y,Z,A,
bdef0 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28  B,C,D) \.    if(
bdf00 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20  sqlite3OSTrace) 
bdf10 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
bdf20 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44  tf(X,Y,Z,A,B,C,D
bdf30 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ).#else.#define 
bdf40 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66  OSTRACE1(X).#def
bdf50 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59  ine OSTRACE2(X,Y
bdf60 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
bdf70 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e  E3(X,Y,Z).#defin
bdf80 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a  e OSTRACE4(X,Y,Z
bdf90 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,A).#define OSTR
bdfa0 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a  ACE5(X,Y,Z,A,B).
bdfb0 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36  #define OSTRACE6
bdfc0 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64  (X,Y,Z,A,B,C).#d
bdfd0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58  efine OSTRACE7(X
bdfe0 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65  ,Y,Z,A,B,C,D).#e
bdff0 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72  ndif../*.** Macr
be000 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e  os for performan
be010 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72  ce tracing.  Nor
be020 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66  mally turned off
be030 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a  .  Only works.**
be040 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72   on i486 hardwar
be050 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e..*/.#ifdef SQL
be060 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f  ITE_PERFORMANCE_
be070 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77  TRACE../* .** hw
be080 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20  time.h contains 
be090 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72  inline assembler
be0a0 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d   code for implem
be0b0 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d  enting .** high-
be0c0 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69  performance timi
be0d0 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a  ng routines..*/.
be0e0 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
be0f0 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68  Include hwtime.h
be100 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f   in the middle o
be110 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a  f os_common.h **
be120 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
be130 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
be140 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d  Begin file hwtim
be150 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.h ************
be160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
be180 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
be190 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  7.**.** The auth
be1a0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
be1b0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
be1c0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
be1d0 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
be1e0 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
be1f0 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
be200 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
be210 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
be220 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
be230 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
be240 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
be250 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
be260 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
be270 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
be280 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
be290 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
be2a0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
be2b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be2c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be2d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be2e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
be2f0 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
be300 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69   file contains i
be310 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66  nline asm code f
be320 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68  or retrieving "h
be330 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22  igh-performance"
be340 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72  .** counters for
be350 20 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e   x86 class CPUs.
be360 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69  .**.** $Id: hwti
be370 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f  me.h,v 1.3 2008/
be380 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73  08/01 14:33:15 s
be390 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69  hane Exp $.*/.#i
be3a0 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f  fndef _HWTIME_H_
be3b0 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45  .#define _HWTIME
be3c0 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  _H_../*.** The f
be3d0 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
be3e0 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70   only works on p
be3f0 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72  entium-class (or
be400 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f   newer) processo
be410 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74  rs..** It uses t
be420 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20  he RDTSC opcode 
be430 74 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c  to read the cycl
be440 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75  e count value ou
be450 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63  t of the.** proc
be460 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e  essor and return
be470 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54  s that value.  T
be480 68 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20  his can be used 
be490 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20  for high-res.** 
be4a0 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69  profiling..*/.#i
be4b0 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55  f (defined(__GNU
be4c0 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  C__) || defined(
be4d0 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a  _MSC_VER)) && \.
be4e0 20 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69        (defined(i
be4f0 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  386) || defined(
be500 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66  __i386__) || def
be510 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a  ined(_M_IX86))..
be520 20 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f    #if defined(__
be530 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c  GNUC__)..  __inl
be540 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e  ine__ sqlite_uin
be550 74 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d  t64 sqlite3Hwtim
be560 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e  e(void){.     un
be570 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68  signed int lo, h
be580 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20  i;.     __asm__ 
be590 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72  __volatile__ ("r
be5a0 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f  dtsc" : "=a" (lo
be5b0 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20  ), "=d" (hi));. 
be5c0 20 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69      return (sqli
be5d0 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20  te_uint64)hi << 
be5e0 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20  32 | lo;.  }..  
be5f0 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d  #elif defined(_M
be600 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63  SC_VER)..  __dec
be610 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69  lspec(naked) __i
be620 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e  nline sqlite_uin
be630 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69  t64 __cdecl sqli
be640 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b  te3Hwtime(void){
be650 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20  .     __asm {.  
be660 20 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20        rdtsc.    
be670 20 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20      ret       ; 
be680 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20  return value at 
be690 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20  EDX:EAX.     }. 
be6a0 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65   }..  #endif..#e
be6b0 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  lif (defined(__G
be6c0 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65  NUC__) && define
be6d0 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a  d(__x86_64__))..
be6e0 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c    __inline__ sql
be6f0 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74  ite_uint64 sqlit
be700 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
be710 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c        unsigned l
be720 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f  ong val;.      _
be730 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c  _asm__ __volatil
be740 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22  e__ ("rdtsc" : "
be750 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20  =A" (val));.    
be760 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20    return val;.  
be770 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e  }. .#elif (defin
be780 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20  ed(__GNUC__) && 
be790 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29  defined(__ppc__)
be7a0 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20  )..  __inline__ 
be7b0 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71  sqlite_uint64 sq
be7c0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
be7d0 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  ){.      unsigne
be7e0 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76  d long long retv
be7f0 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e  al;.      unsign
be800 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20  ed long junk;.  
be810 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
be820 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20  latile__ ("\n\. 
be830 20 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20           1:     
be840 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20   mftbu   %1\n\. 
be850 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be860 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a   mftb    %L0\n\.
be870 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be880 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a    mftbu   %0\n\.
be890 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
be8a0 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c    cmpw    %0,%1\
be8b0 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n\.             
be8c0 20 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22       bne     1b"
be8d0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
be8e0 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61     : "=r" (retva
be8f0 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29  l), "=r" (junk))
be900 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
be910 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73  etval;.  }..#els
be920 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64  e..  #error Need
be930 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
be940 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  of sqlite3Hwtime
be950 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
be960 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  form...  /*.  **
be970 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   To compile with
be980 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67  out implementing
be990 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
be9a0 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f   for your platfo
be9b0 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e  rm,.  ** you can
be9c0 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76   remove the abov
be9d0 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65  e #error and use
be9e0 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20   the following. 
be9f0 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f   ** stub functio
bea00 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73  n.  You will los
bea10 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74  e timing support
bea20 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f   for many.  ** o
bea30 66 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20  f the debugging 
bea40 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c  and testing util
bea50 69 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68  ities, but it sh
bea60 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61  ould at.  ** lea
bea70 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72  st compile and r
bea80 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f  un..  */.SQLITE_
bea90 50 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65  PRIVATE   sqlite
beaa0 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48  _uint64 sqlite3H
beab0 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74  wtime(void){ ret
beac0 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e  urn ((sqlite_uin
bead0 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69  t64)0); }..#endi
beae0 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
beaf0 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f  fined(_HWTIME_H_
beb00 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
beb10 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74  ***** End of hwt
beb20 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
beb30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
beb40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
beb50 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
beb60 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
beb70 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
beb80 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e  ff in os_common.
beb90 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  h **************
beba0 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71  ****/..static sq
bebb0 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74  lite_uint64 g_st
bebc0 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69  art;.static sqli
bebd0 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70  te_uint64 g_elap
bebe0 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d  sed;.#define TIM
bebf0 45 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67  ER_START       g
bec00 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77  _start=sqlite3Hw
bec10 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54  time().#define T
bec20 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20  IMER_END        
bec30 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74   g_elapsed=sqlit
bec40 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61  e3Hwtime()-g_sta
bec50 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  rt.#define TIMER
bec60 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65  _ELAPSED     g_e
bec70 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65  lapsed.#else.#de
bec80 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54  fine TIMER_START
bec90 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45  .#define TIMER_E
beca0 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  ND.#define TIMER
becb0 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73  _ELAPSED     ((s
becc0 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a  qlite_uint64)0).
becd0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
bece0 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68   we compile with
becf0 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54   the SQLITE_TEST
bed00 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e   macro set, then
bed10 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62   the following b
bed20 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20  lock.** of code 
bed30 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65  will give us the
bed40 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75   ability to simu
bed50 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20  late a disk I/O 
bed60 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20  error.  This.** 
bed70 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
bed80 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f  ing the I/O reco
bed90 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23  very logic..*/.#
beda0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
bedb0 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  T.SQLITE_API int
bedc0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bedd0 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20  r_hit = 0;      
bede0 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e        /* Total n
bedf0 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72  umber of I/O Err
bee00 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ors */.SQLITE_AP
bee10 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
bee20 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d  _error_hardhit =
bee30 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75   0;        /* Nu
bee40 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69  mber of non-beni
bee50 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c  gn errors */.SQL
bee60 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
bee70 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e  te3_io_error_pen
bee80 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20  ding = 0;       
bee90 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74   /* Count down t
beea0 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f  o first I/O erro
beeb0 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  r */.SQLITE_API 
beec0 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  int sqlite3_io_e
beed0 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30  rror_persist = 0
beee0 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
beef0 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70   if I/O errors p
bef00 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45  ersist */.SQLITE
bef10 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
bef20 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e  _io_error_benign
bef30 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a   = 0;         /*
bef40 20 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20   True if errors 
bef50 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51  are benign */.SQ
bef60 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
bef70 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65  ite3_diskfull_pe
bef80 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54  nding = 0;.SQLIT
bef90 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
befa0 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a  3_diskfull = 0;.
befb0 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
befc0 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29  IOErrorBenign(X)
befd0 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
befe0 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65  r_benign=(X).#de
beff0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45  fine SimulateIOE
bf000 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20  rror(CODE)  \.  
bf010 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f  if( (sqlite3_io_
bf020 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26  error_persist &&
bf030 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f   sqlite3_io_erro
bf040 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20  r_hit) \.       
bf050 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  || sqlite3_io_er
bf060 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d  ror_pending-- ==
bf070 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20   1 )  \.        
bf080 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f        { local_io
bf090 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73  err(); CODE; }.s
bf0a0 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c  tatic void local
bf0b0 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52  _ioerr(){.  IOTR
bf0c0 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29  ACE(("IOERR\n"))
bf0d0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65  ;.  sqlite3_io_e
bf0e0 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66  rror_hit++;.  if
bf0f0 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  ( !sqlite3_io_er
bf100 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c  ror_benign ) sql
bf110 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61  ite3_io_error_ha
bf120 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69  rdhit++;.}.#defi
bf130 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66  ne SimulateDiskf
bf140 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c  ullError(CODE) \
bf150 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  .   if( sqlite3_
bf160 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67  diskfull_pending
bf170 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73   ){ \.     if( s
bf180 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
bf190 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20  pending == 1 ){ 
bf1a0 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69  \.       local_i
bf1b0 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20  oerr(); \.      
bf1c0 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
bf1d0 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20  l = 1; \.       
bf1e0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
bf1f0 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20  _hit = 1; \.    
bf200 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20     CODE; \.     
bf210 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20  }else{ \.       
bf220 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
bf230 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20  _pending--; \.  
bf240 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73     } \.   }.#els
bf250 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  e.#define Simula
bf260 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
bf270 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c  X).#define Simul
bf280 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64  ateIOError(A).#d
bf290 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69  efine SimulateDi
bf2a0 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23  skfullError(A).#
bf2b0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65  endif../*.** Whe
bf2c0 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20  n testing, keep 
bf2d0 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e  a count of the n
bf2e0 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69  umber of open fi
bf2f0 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  les..*/.#ifdef S
bf300 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54  QLITE_TEST.SQLIT
bf310 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
bf320 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e  3_open_file_coun
bf330 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f  t = 0;.#define O
bf340 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73  penCounter(X)  s
bf350 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65  qlite3_open_file
bf360 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73  _count+=(X).#els
bf370 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f  e.#define OpenCo
bf380 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a  unter(X).#endif.
bf390 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69  .#endif /* !defi
bf3a0 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48  ned(_OS_COMMON_H
bf3b0 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  _) */../********
bf3c0 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
bf3d0 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
bf3e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf3f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf400 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
bf410 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e  ****** Continuin
bf420 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20  g where we left 
bf430 6f 66 66 20 69 6e 20 6f 73 5f 6f 73 32 2e 63 20  off in os_os2.c 
bf440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf450 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  *****/../*.** Th
bf460 65 20 6f 73 32 46 69 6c 65 20 73 74 72 75 63 74  e os2File struct
bf470 75 72 65 20 69 73 20 73 75 62 63 6c 61 73 73 20  ure is subclass 
bf480 6f 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  of sqlite3_file 
bf490 73 70 65 63 69 66 69 63 20 66 6f 72 20 74 68 65  specific for the
bf4a0 20 4f 53 2f 32 0a 2a 2a 20 70 72 6f 74 61 62 69   OS/2.** protabi
bf4b0 6c 69 74 79 20 6c 61 79 65 72 2e 0a 2a 2f 0a 74  lity layer..*/.t
bf4c0 79 70 65 64 65 66 20 73 74 72 75 63 74 20 6f 73  ypedef struct os
bf4d0 32 46 69 6c 65 20 6f 73 32 46 69 6c 65 3b 0a 73  2File os2File;.s
bf4e0 74 72 75 63 74 20 6f 73 32 46 69 6c 65 20 7b 0a  truct os2File {.
bf4f0 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
bf500 69 6f 5f 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74  io_methods *pMet
bf510 68 6f 64 3b 20 20 2f 2a 20 41 6c 77 61 79 73 20  hod;  /* Always 
bf520 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20  the first entry 
bf530 2a 2f 0a 20 20 48 46 49 4c 45 20 68 3b 20 20 20  */.  HFILE h;   
bf540 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
bf550 2a 20 48 61 6e 64 6c 65 20 66 6f 72 20 61 63 63  * Handle for acc
bf560 65 73 73 69 6e 67 20 74 68 65 20 66 69 6c 65 20  essing the file 
bf570 2a 2f 0a 20 20 63 68 61 72 2a 20 70 61 74 68 54  */.  char* pathT
bf580 6f 44 65 6c 3b 20 20 20 20 20 20 20 20 20 20 2f  oDel;          /
bf590 2a 20 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74  * Name of file t
bf5a0 6f 20 64 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73  o delete on clos
bf5b0 65 2c 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 2a  e, NULL if not *
bf5c0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
bf5d0 72 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a  r locktype;   /*
bf5e0 20 54 79 70 65 20 6f 66 20 6c 6f 63 6b 20 63 75   Type of lock cu
bf5f0 72 72 65 6e 74 6c 79 20 68 65 6c 64 20 6f 6e 20  rrently held on 
bf600 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a  this file */.};.
bf610 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 5f 54 49  .#define LOCK_TI
bf620 4d 45 4f 55 54 20 31 30 4c 20 2f 2a 20 74 68 65  MEOUT 10L /* the
bf630 20 64 65 66 61 75 6c 74 20 6c 6f 63 6b 69 6e 67   default locking
bf640 20 74 69 6d 65 6f 75 74 20 2a 2f 0a 0a 2f 2a 2a   timeout */../**
bf650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf690 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
bf6a0 68 65 20 6e 65 78 74 20 67 72 6f 75 70 20 6f 66  he next group of
bf6b0 20 72 6f 75 74 69 6e 65 73 20 69 6d 70 6c 65 6d   routines implem
bf6c0 65 6e 74 20 74 68 65 20 49 2f 4f 20 6d 65 74 68  ent the I/O meth
bf6d0 6f 64 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  ods specified.**
bf6e0 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 33 5f   by the sqlite3_
bf6f0 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63  io_methods objec
bf700 74 2e 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  t..*************
bf710 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf720 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
bf750 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20  */../*.** Close 
bf760 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  a file..*/.stati
bf770 63 20 69 6e 74 20 6f 73 32 43 6c 6f 73 65 28 20  c int os2Close( 
bf780 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
bf790 20 29 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20   ){.  APIRET rc 
bf7a0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 6f 73  = NO_ERROR;.  os
bf7b0 32 46 69 6c 65 20 2a 70 46 69 6c 65 3b 0a 20 20  2File *pFile;.  
bf7c0 69 66 28 20 69 64 20 26 26 20 28 70 46 69 6c 65  if( id && (pFile
bf7d0 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 29   = (os2File*)id)
bf7e0 20 21 3d 20 30 20 29 7b 0a 20 20 20 20 4f 53 54   != 0 ){.    OST
bf7f0 52 41 43 45 32 28 20 22 43 4c 4f 53 45 20 25 64  RACE2( "CLOSE %d
bf800 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 20 29 3b  \n", pFile->h );
bf810 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 43 6c 6f  .    rc = DosClo
bf820 73 65 28 20 70 46 69 6c 65 2d 3e 68 20 29 3b 0a  se( pFile->h );.
bf830 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74      pFile->lockt
bf840 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20  ype = NO_LOCK;. 
bf850 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 70 61     if( pFile->pa
bf860 74 68 54 6f 44 65 6c 20 21 3d 20 4e 55 4c 4c 20  thToDel != NULL 
bf870 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 44 6f  ){.      rc = Do
bf880 73 46 6f 72 63 65 44 65 6c 65 74 65 28 20 28 50  sForceDelete( (P
bf890 53 5a 29 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f  SZ)pFile->pathTo
bf8a0 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 66 72 65  Del );.      fre
bf8b0 65 28 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f  e( pFile->pathTo
bf8c0 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 46 69  Del );.      pFi
bf8d0 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d 20  le->pathToDel = 
bf8e0 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 20 20  NULL;.    }.    
bf8f0 69 64 20 3d 20 30 3b 0a 20 20 20 20 4f 70 65 6e  id = 0;.    Open
bf900 43 6f 75 6e 74 65 72 28 20 2d 31 20 29 3b 0a 20  Counter( -1 );. 
bf910 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 20   }..  return rc 
bf920 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51  == NO_ERROR ? SQ
bf930 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45  LITE_OK : SQLITE
bf940 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _IOERR;.}../*.**
bf950 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20   Read data from 
bf960 61 20 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75  a file into a bu
bf970 66 66 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51  ffer.  Return SQ
bf980 4c 49 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a  LITE_OK if all.*
bf990 2a 20 62 79 74 65 73 20 77 65 72 65 20 72 65 61  * bytes were rea
bf9a0 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61  d successfully a
bf9b0 6e 64 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20  nd SQLITE_IOERR 
bf9c0 69 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73  if anything goes
bf9d0 0a 2a 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74  .** wrong..*/.st
bf9e0 61 74 69 63 20 69 6e 74 20 6f 73 32 52 65 61 64  atic int os2Read
bf9f0 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  (.  sqlite3_file
bfa00 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 20 20   *id,           
bfa10 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 72      /* File to r
bfa20 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 76 6f  ead from */.  vo
bfa30 69 64 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20  id *pBuf,       
bfa40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
bfa50 20 57 72 69 74 65 20 63 6f 6e 74 65 6e 74 20 69   Write content i
bfa60 6e 74 6f 20 74 68 69 73 20 62 75 66 66 65 72 20  nto this buffer 
bfa70 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20 20  */.  int amt,   
bfa80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfa90 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
bfaa0 66 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20  f bytes to read 
bfab0 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74  */.  sqlite3_int
bfac0 36 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20  64 offset       
bfad0 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65       /* Begin re
bfae0 61 64 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66  ading at this of
bfaf0 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f  fset */.){.  ULO
bfb00 4e 47 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20  NG fileLocation 
bfb10 3d 20 30 4c 3b 0a 20 20 55 4c 4f 4e 47 20 67 6f  = 0L;.  ULONG go
bfb20 74 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46  t;.  os2File *pF
bfb30 69 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29  ile = (os2File*)
bfb40 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64  id;.  assert( id
bfb50 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74  !=0 );.  Simulat
bfb60 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
bfb70 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
bfb80 41 44 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33  AD );.  OSTRACE3
bfb90 28 20 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d  ( "READ %d lock=
bfba0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
bfbb0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
bfbc0 20 29 3b 0a 20 20 69 66 28 20 44 6f 73 53 65 74   );.  if( DosSet
bfbd0 46 69 6c 65 50 74 72 28 70 46 69 6c 65 2d 3e 68  FilePtr(pFile->h
bfbe0 2c 20 6f 66 66 73 65 74 2c 20 46 49 4c 45 5f 42  , offset, FILE_B
bfbf0 45 47 49 4e 2c 20 26 66 69 6c 65 4c 6f 63 61 74  EGIN, &fileLocat
bfc00 69 6f 6e 29 20 21 3d 20 4e 4f 5f 45 52 52 4f 52  ion) != NO_ERROR
bfc10 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
bfc20 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d  QLITE_IOERR;.  }
bfc30 0a 20 20 69 66 28 20 44 6f 73 52 65 61 64 28 20  .  if( DosRead( 
bfc40 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20  pFile->h, pBuf, 
bfc50 61 6d 74 2c 20 26 67 6f 74 20 29 20 21 3d 20 4e  amt, &got ) != N
bfc60 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 72  O_ERROR ){.    r
bfc70 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
bfc80 52 52 5f 52 45 41 44 3b 0a 20 20 7d 0a 20 20 69  RR_READ;.  }.  i
bfc90 66 28 20 67 6f 74 20 3d 3d 20 28 55 4c 4f 4e 47  f( got == (ULONG
bfca0 29 61 6d 74 20 29 0a 20 20 20 20 72 65 74 75 72  )amt ).    retur
bfcb0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 65  n SQLITE_OK;.  e
bfcc0 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 55 6e 72  lse {.    /* Unr
bfcd0 65 61 64 20 70 6f 72 74 69 6f 6e 73 20 6f 66 20  ead portions of 
bfce0 74 68 65 20 69 6e 70 75 74 20 62 75 66 66 65 72  the input buffer
bfcf0 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69   must be zero-fi
bfd00 6c 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73  lled */.    mems
bfd10 65 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 66  et(&((char*)pBuf
bfd20 29 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67  )[got], 0, amt-g
bfd30 6f 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ot);.    return 
bfd40 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
bfd50 52 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a  RT_READ;.  }.}..
bfd60 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61  /*.** Write data
bfd70 20 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69   from a buffer i
bfd80 6e 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74  nto a file.  Ret
bfd90 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
bfda0 20 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73   success.** or s
bfdb0 6f 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20  ome other error 
bfdc0 63 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e  code on failure.
bfdd0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
bfde0 73 32 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74  s2Write(.  sqlit
bfdf0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20  e3_file *id,    
bfe00 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69             /* Fi
bfe10 6c 65 20 74 6f 20 77 72 69 74 65 20 69 6e 74 6f  le to write into
bfe20 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
bfe30 20 2a 70 42 75 66 2c 20 20 20 20 20 20 20 20 20   *pBuf,         
bfe40 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 79 74        /* The byt
bfe50 65 73 20 74 6f 20 62 65 20 77 72 69 74 74 65 6e  es to be written
bfe60 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74 2c 20 20   */.  int amt,  
bfe70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
bfe80 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
bfe90 6f 66 20 62 79 74 65 73 20 74 6f 20 77 72 69 74  of bytes to writ
bfea0 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  e */.  sqlite3_i
bfeb0 6e 74 36 34 20 6f 66 66 73 65 74 20 20 20 20 20  nt64 offset     
bfec0 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74         /* Offset
bfed0 20 69 6e 74 6f 20 74 68 65 20 66 69 6c 65 20 74   into the file t
bfee0 6f 20 62 65 67 69 6e 20 77 72 69 74 69 6e 67 20  o begin writing 
bfef0 61 74 20 2a 2f 0a 29 7b 0a 20 20 55 4c 4f 4e 47  at */.){.  ULONG
bff00 20 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 20 3d 20   fileLocation = 
bff10 30 4c 3b 0a 20 20 41 50 49 52 45 54 20 72 63 20  0L;.  APIRET rc 
bff20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 55 4c  = NO_ERROR;.  UL
bff30 4f 4e 47 20 77 72 6f 74 65 3b 0a 20 20 6f 73 32  ONG wrote;.  os2
bff40 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
bff50 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73  s2File*)id;.  as
bff60 73 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20  sert( id!=0 );. 
bff70 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
bff80 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ( return SQLITE_
bff90 49 4f 45 52 52 5f 57 52 49 54 45 20 29 3b 0a 20  IOERR_WRITE );. 
bffa0 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75 6c   SimulateDiskful
bffb0 6c 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  lError( return S
bffc0 51 4c 49 54 45 5f 46 55 4c 4c 20 29 3b 0a 20 20  QLITE_FULL );.  
bffd0 4f 53 54 52 41 43 45 33 28 20 22 57 52 49 54 45  OSTRACE3( "WRITE
bffe0 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20   %d lock=%d\n", 
bfff0 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d  pFile->h, pFile-
c0000 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 69  >locktype );.  i
c0010 66 28 20 44 6f 73 53 65 74 46 69 6c 65 50 74 72  f( DosSetFilePtr
c0020 28 70 46 69 6c 65 2d 3e 68 2c 20 6f 66 66 73 65  (pFile->h, offse
c0030 74 2c 20 46 49 4c 45 5f 42 45 47 49 4e 2c 20 26  t, FILE_BEGIN, &
c0040 66 69 6c 65 4c 6f 63 61 74 69 6f 6e 29 20 21 3d  fileLocation) !=
c0050 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c0060 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
c0070 4f 45 52 52 3b 0a 20 20 7d 0a 20 20 61 73 73 65  OERR;.  }.  asse
c0080 72 74 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77  rt( amt>0 );.  w
c0090 68 69 6c 65 28 20 61 6d 74 20 3e 20 30 20 26 26  hile( amt > 0 &&
c00a0 0a 20 20 20 20 20 20 20 20 20 28 20 72 63 20 3d  .         ( rc =
c00b0 20 44 6f 73 57 72 69 74 65 28 20 70 46 69 6c 65   DosWrite( pFile
c00c0 2d 3e 68 2c 20 28 50 56 4f 49 44 29 70 42 75 66  ->h, (PVOID)pBuf
c00d0 2c 20 61 6d 74 2c 20 26 77 72 6f 74 65 20 29 20  , amt, &wrote ) 
c00e0 29 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 26 26  ) == NO_ERROR &&
c00f0 0a 20 20 20 20 20 20 20 20 20 77 72 6f 74 65 20  .         wrote 
c0100 3e 20 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74  > 0.  ){.    amt
c0110 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70   -= wrote;.    p
c0120 42 75 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70  Buf = &((char*)p
c0130 42 75 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d  Buf)[wrote];.  }
c0140 0a 0a 20 20 72 65 74 75 72 6e 20 28 20 72 63 20  ..  return ( rc 
c0150 21 3d 20 4e 4f 5f 45 52 52 4f 52 20 7c 7c 20 61  != NO_ERROR || a
c0160 6d 74 20 3e 20 28 69 6e 74 29 77 72 6f 74 65 20  mt > (int)wrote 
c0170 29 20 3f 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  ) ? SQLITE_FULL 
c0180 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  : SQLITE_OK;.}..
c0190 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 61  /*.** Truncate a
c01a0 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20 61  n open file to a
c01b0 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65 0a   specified size.
c01c0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c01d0 32 54 72 75 6e 63 61 74 65 28 20 73 71 6c 69 74  2Truncate( sqlit
c01e0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 36 34  e3_file *id, i64
c01f0 20 6e 42 79 74 65 20 29 7b 0a 20 20 41 50 49 52   nByte ){.  APIR
c0200 45 54 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52  ET rc = NO_ERROR
c0210 3b 0a 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69  ;.  os2File *pFi
c0220 6c 65 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69  le = (os2File*)i
c0230 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22  d;.  OSTRACE3( "
c0240 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64  TRUNCATE %d %lld
c0250 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e  \n", pFile->h, n
c0260 42 79 74 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61  Byte );.  Simula
c0270 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
c0280 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54  n SQLITE_IOERR_T
c0290 52 55 4e 43 41 54 45 20 29 3b 0a 20 20 72 63 20  RUNCATE );.  rc 
c02a0 3d 20 44 6f 73 53 65 74 46 69 6c 65 53 69 7a 65  = DosSetFileSize
c02b0 28 20 70 46 69 6c 65 2d 3e 68 2c 20 6e 42 79 74  ( pFile->h, nByt
c02c0 65 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  e );.  return rc
c02d0 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53   == NO_ERROR ? S
c02e0 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54  QLITE_OK : SQLIT
c02f0 45 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45  E_IOERR_TRUNCATE
c0300 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
c0310 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f  TE_TEST./*.** Co
c0320 75 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  unt the number o
c0330 66 20 66 75 6c 6c 73 79 6e 63 73 20 61 6e 64 20  f fullsyncs and 
c0340 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e 20 20 54  normal syncs.  T
c0350 68 69 73 20 69 73 20 75 73 65 64 20 74 6f 20 74  his is used to t
c0360 65 73 74 0a 2a 2a 20 74 68 61 74 20 73 79 6e 63  est.** that sync
c0370 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e 63 73 20  s and fullsyncs 
c0380 61 72 65 20 6f 63 63 75 72 69 6e 67 20 61 74 20  are occuring at 
c0390 74 68 65 20 72 69 67 68 74 20 74 69 6d 65 73 2e  the right times.
c03a0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
c03b0 6e 74 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f  nt sqlite3_sync_
c03c0 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51 4c 49 54  count = 0;.SQLIT
c03d0 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
c03e0 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
c03f0 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a   = 0;.#endif../*
c0400 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 61 6c  .** Make sure al
c0410 6c 20 77 72 69 74 65 73 20 74 6f 20 61 20 70 61  l writes to a pa
c0420 72 74 69 63 75 6c 61 72 20 66 69 6c 65 20 61 72  rticular file ar
c0430 65 20 63 6f 6d 6d 69 74 74 65 64 20 74 6f 20 64  e committed to d
c0440 69 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  isk..*/.static i
c0450 6e 74 20 6f 73 32 53 79 6e 63 28 20 73 71 6c 69  nt os2Sync( sqli
c0460 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
c0470 74 20 66 6c 61 67 73 20 29 7b 0a 20 20 6f 73 32  t flags ){.  os2
c0480 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 6f  File *pFile = (o
c0490 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 4f 53  s2File*)id;.  OS
c04a0 54 52 41 43 45 33 28 20 22 53 59 4e 43 20 25 64  TRACE3( "SYNC %d
c04b0 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c 20 70 46 69   lock=%d\n", pFi
c04c0 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 6c 6f  le->h, pFile->lo
c04d0 63 6b 74 79 70 65 20 29 3b 0a 23 69 66 64 65 66  cktype );.#ifdef
c04e0 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
c04f0 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c0500 45 5f 53 59 4e 43 5f 46 55 4c 4c 29 7b 0a 20 20  E_SYNC_FULL){.  
c0510 20 20 73 71 6c 69 74 65 33 5f 66 75 6c 6c 73 79    sqlite3_fullsy
c0520 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 20 20 7d 0a  nc_count++;.  }.
c0530 20 20 73 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63    sqlite3_sync_c
c0540 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20  ount++;.#endif. 
c0550 20 2f 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c   /* If we compil
c0560 65 64 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  ed with the SQLI
c0570 54 45 5f 4e 4f 5f 53 59 4e 43 20 66 6c 61 67 2c  TE_NO_SYNC flag,
c0580 20 74 68 65 6e 20 73 79 6e 63 69 6e 67 20 69 73   then syncing is
c0590 20 61 0a 20 20 2a 2a 20 6e 6f 2d 6f 70 0a 20 20   a.  ** no-op.  
c05a0 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  */.#ifdef SQLITE
c05b0 5f 4e 4f 5f 53 59 4e 43 0a 20 20 72 65 74 75 72  _NO_SYNC.  retur
c05c0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6c  n SQLITE_OK;.#el
c05d0 73 65 0a 20 20 72 65 74 75 72 6e 20 44 6f 73 52  se.  return DosR
c05e0 65 73 65 74 42 75 66 66 65 72 28 20 70 46 69 6c  esetBuffer( pFil
c05f0 65 2d 3e 68 20 29 20 3d 3d 20 4e 4f 5f 45 52 52  e->h ) == NO_ERR
c0600 4f 52 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a  OR ? SQLITE_OK :
c0610 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 3b 0a 23   SQLITE_IOERR;.#
c0620 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  endif.}../*.** D
c0630 65 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72  etermine the cur
c0640 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66  rent size of a f
c0650 69 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a  ile in bytes.*/.
c0660 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 46 69  static int os2Fi
c0670 6c 65 53 69 7a 65 28 20 73 71 6c 69 74 65 33 5f  leSize( sqlite3_
c0680 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65  file *id, sqlite
c0690 33 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 20 29  3_int64 *pSize )
c06a0 7b 0a 20 20 41 50 49 52 45 54 20 72 63 20 3d 20  {.  APIRET rc = 
c06b0 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 46 49 4c 45  NO_ERROR;.  FILE
c06c0 53 54 41 54 55 53 33 20 66 73 74 73 33 46 69 6c  STATUS3 fsts3Fil
c06d0 65 49 6e 66 6f 3b 0a 20 20 6d 65 6d 73 65 74 28  eInfo;.  memset(
c06e0 26 66 73 74 73 33 46 69 6c 65 49 6e 66 6f 2c 20  &fsts3FileInfo, 
c06f0 30 2c 20 73 69 7a 65 6f 66 28 66 73 74 73 33 46  0, sizeof(fsts3F
c0700 69 6c 65 49 6e 66 6f 29 29 3b 0a 20 20 61 73 73  ileInfo));.  ass
c0710 65 72 74 28 20 69 64 21 3d 30 20 29 3b 0a 20 20  ert( id!=0 );.  
c0720 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
c0730 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
c0740 4f 45 52 52 5f 46 53 54 41 54 20 29 3b 0a 20 20  OERR_FSTAT );.  
c0750 72 63 20 3d 20 44 6f 73 51 75 65 72 79 46 69 6c  rc = DosQueryFil
c0760 65 49 6e 66 6f 28 20 28 28 6f 73 32 46 69 6c 65  eInfo( ((os2File
c0770 2a 29 69 64 29 2d 3e 68 2c 20 46 49 4c 5f 53 54  *)id)->h, FIL_ST
c0780 41 4e 44 41 52 44 2c 20 26 66 73 74 73 33 46 69  ANDARD, &fsts3Fi
c0790 6c 65 49 6e 66 6f 2c 20 73 69 7a 65 6f 66 28 46  leInfo, sizeof(F
c07a0 49 4c 45 53 54 41 54 55 53 33 29 20 29 3b 0a 20  ILESTATUS3) );. 
c07b0 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52   if( rc == NO_ER
c07c0 52 4f 52 20 29 7b 0a 20 20 20 20 2a 70 53 69 7a  ROR ){.    *pSiz
c07d0 65 20 3d 20 66 73 74 73 33 46 69 6c 65 49 6e 66  e = fsts3FileInf
c07e0 6f 2e 63 62 46 69 6c 65 3b 0a 20 20 20 20 72 65  o.cbFile;.    re
c07f0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c0800 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
c0810 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52  urn SQLITE_IOERR
c0820 5f 46 53 54 41 54 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _FSTAT;.  }.}../
c0830 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20 61 20 72  *.** Acquire a r
c0840 65 61 64 65 72 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73  eader lock..*/.s
c0850 74 61 74 69 63 20 69 6e 74 20 67 65 74 52 65 61  tatic int getRea
c0860 64 4c 6f 63 6b 28 20 6f 73 32 46 69 6c 65 20 2a  dLock( os2File *
c0870 70 46 69 6c 65 20 29 7b 0a 20 20 46 49 4c 45 4c  pFile ){.  FILEL
c0880 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20  OCK  LockArea,. 
c0890 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63             Unloc
c08a0 6b 41 72 65 61 3b 0a 20 20 41 50 49 52 45 54 20  kArea;.  APIRET 
c08b0 72 65 73 3b 0a 20 20 6d 65 6d 73 65 74 28 26 4c  res;.  memset(&L
c08c0 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65  ockArea, 0, size
c08d0 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29 3b 0a 20  of(LockArea));. 
c08e0 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f 63 6b 41   memset(&UnlockA
c08f0 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 55  rea, 0, sizeof(U
c0900 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a 20 20 4c  nlockArea));.  L
c0910 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c0920 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b 0a  = SHARED_FIRST;.
c0930 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67    LockArea.lRang
c0940 65 20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b  e = SHARED_SIZE;
c0950 0a 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f  .  UnlockArea.lO
c0960 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20 55 6e  ffset = 0L;.  Un
c0970 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c0980 3d 20 30 4c 3b 0a 20 20 72 65 73 20 3d 20 44 6f  = 0L;.  res = Do
c0990 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70  sSetFileLocks( p
c09a0 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b  File->h, &Unlock
c09b0 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c  Area, &LockArea,
c09c0 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 31   LOCK_TIMEOUT, 1
c09d0 4c 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  L );.  OSTRACE3(
c09e0 20 22 47 45 54 52 45 41 44 4c 4f 43 4b 20 25 64   "GETREADLOCK %d
c09f0 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c   res=%d\n", pFil
c0a00 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 72  e->h, res );.  r
c0a10 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a  eturn res;.}../*
c0a20 0a 2a 2a 20 55 6e 64 6f 20 61 20 72 65 61 64 6c  .** Undo a readl
c0a30 6f 63 6b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ock.*/.static in
c0a40 74 20 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b  t unlockReadLock
c0a50 28 20 6f 73 32 46 69 6c 65 20 2a 69 64 20 29 7b  ( os2File *id ){
c0a60 0a 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63  .  FILELOCK  Loc
c0a70 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20  kArea,.         
c0a80 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20     UnlockArea;. 
c0a90 20 41 50 49 52 45 54 20 72 65 73 3b 0a 20 20 6d   APIRET res;.  m
c0aa0 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
c0ab0 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
c0ac0 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  rea));.  memset(
c0ad0 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
c0ae0 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
c0af0 61 29 29 3b 0a 20 20 4c 6f 63 6b 41 72 65 61 2e  a));.  LockArea.
c0b00 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a 20 20  lOffset = 0L;.  
c0b10 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  LockArea.lRange 
c0b20 3d 20 30 4c 3b 0a 20 20 55 6e 6c 6f 63 6b 41 72  = 0L;.  UnlockAr
c0b30 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 53 48 41  ea.lOffset = SHA
c0b40 52 45 44 5f 46 49 52 53 54 3b 0a 20 20 55 6e 6c  RED_FIRST;.  Unl
c0b50 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c0b60 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20 20   SHARED_SIZE;.  
c0b70 72 65 73 20 3d 20 44 6f 73 53 65 74 46 69 6c 65  res = DosSetFile
c0b80 4c 6f 63 6b 73 28 20 69 64 2d 3e 68 2c 20 26 55  Locks( id->h, &U
c0b90 6e 6c 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b  nlockArea, &Lock
c0ba0 41 72 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f  Area, LOCK_TIMEO
c0bb0 55 54 2c 20 31 4c 20 29 3b 0a 20 20 4f 53 54 52  UT, 1L );.  OSTR
c0bc0 41 43 45 33 28 20 22 55 4e 4c 4f 43 4b 2d 52 45  ACE3( "UNLOCK-RE
c0bd0 41 44 4c 4f 43 4b 20 66 69 6c 65 20 68 61 6e 64  ADLOCK file hand
c0be0 6c 65 3d 25 64 20 72 65 73 3d 25 64 3f 5c 6e 22  le=%d res=%d?\n"
c0bf0 2c 20 69 64 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  , id->h, res );.
c0c00 20 20 72 65 74 75 72 6e 20 72 65 73 3b 0a 7d 0a    return res;.}.
c0c10 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74 68 65 20  ./*.** Lock the 
c0c20 66 69 6c 65 20 77 69 74 68 20 74 68 65 20 6c 6f  file with the lo
c0c30 63 6b 20 73 70 65 63 69 66 69 65 64 20 62 79 20  ck specified by 
c0c40 70 61 72 61 6d 65 74 65 72 20 6c 6f 63 6b 74 79  parameter lockty
c0c50 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74  pe - one.** of t
c0c60 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a  he following:.**
c0c70 0a 2a 2a 20 20 20 20 20 28 31 29 20 53 48 41 52  .**     (1) SHAR
c0c80 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28  ED_LOCK.**     (
c0c90 32 29 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  2) RESERVED_LOCK
c0ca0 0a 2a 2a 20 20 20 20 20 28 33 29 20 50 45 4e 44  .**     (3) PEND
c0cb0 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20  ING_LOCK.**     
c0cc0 28 34 29 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  (4) EXCLUSIVE_LO
c0cd0 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d  CK.**.** Sometim
c0ce0 65 73 20 77 68 65 6e 20 72 65 71 75 65 73 74 69  es when requesti
c0cf0 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73 74 61 74  ng one lock stat
c0d00 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c 20 6c 6f  e, additional lo
c0d10 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20 61 72 65  ck states.** are
c0d20 20 69 6e 73 65 72 74 65 64 20 69 6e 20 62 65 74   inserted in bet
c0d30 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f 63 6b 69  ween.  The locki
c0d40 6e 67 20 6d 69 67 68 74 20 66 61 69 6c 20 6f 6e  ng might fail on
c0d50 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c 61 74 65   one of the late
c0d60 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73  r.** transitions
c0d70 20 6c 65 61 76 69 6e 67 20 74 68 65 20 6c 6f 63   leaving the loc
c0d80 6b 20 73 74 61 74 65 20 64 69 66 66 65 72 65 6e  k state differen
c0d90 74 20 66 72 6f 6d 20 77 68 61 74 20 69 74 20 73  t from what it s
c0da0 74 61 72 74 65 64 20 62 75 74 0a 2a 2a 20 73 74  tarted but.** st
c0db0 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20 69 74 73  ill short of its
c0dc0 20 67 6f 61 6c 2e 20 20 54 68 65 20 66 6f 6c 6c   goal.  The foll
c0dd0 6f 77 69 6e 67 20 63 68 61 72 74 20 73 68 6f 77  owing chart show
c0de0 73 20 74 68 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a  s the allowed.**
c0df0 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 61 6e 64   transitions and
c0e00 20 74 68 65 20 69 6e 73 65 72 74 65 64 20 69 6e   the inserted in
c0e10 74 65 72 6d 65 64 69 61 74 65 20 73 74 61 74 65  termediate state
c0e20 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f  s:.**.**    UNLO
c0e30 43 4b 45 44 20 2d 3e 20 53 48 41 52 45 44 0a 2a  CKED -> SHARED.*
c0e40 2a 20 20 20 20 53 48 41 52 45 44 20 2d 3e 20 52  *    SHARED -> R
c0e50 45 53 45 52 56 45 44 0a 2a 2a 20 20 20 20 53 48  ESERVED.**    SH
c0e60 41 52 45 44 20 2d 3e 20 28 50 45 4e 44 49 4e 47  ARED -> (PENDING
c0e70 29 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  ) -> EXCLUSIVE.*
c0e80 2a 20 20 20 20 52 45 53 45 52 56 45 44 20 2d 3e  *    RESERVED ->
c0e90 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e 20 45 58   (PENDING) -> EX
c0ea0 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20 20 50 45  CLUSIVE.**    PE
c0eb0 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c 55 53 49  NDING -> EXCLUSI
c0ec0 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  VE.**.** This ro
c0ed0 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e 6c 79 20  utine will only 
c0ee0 69 6e 63 72 65 61 73 65 20 61 20 6c 6f 63 6b 2e  increase a lock.
c0ef0 20 20 54 68 65 20 6f 73 32 55 6e 6c 6f 63 6b 28    The os2Unlock(
c0f00 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 65 72 61  ) routine.** era
c0f10 73 65 73 20 61 6c 6c 20 6c 6f 63 6b 73 20 61 74  ses all locks at
c0f20 20 6f 6e 63 65 20 61 6e 64 20 72 65 74 75 72 6e   once and return
c0f30 73 20 75 73 20 69 6d 6d 65 64 69 61 74 65 6c 79  s us immediately
c0f40 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65   to locking leve
c0f50 6c 20 30 2e 0a 2a 2a 20 49 74 20 69 73 20 6e 6f  l 0..** It is no
c0f60 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 6c 6f  t possible to lo
c0f70 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  wer the locking 
c0f80 6c 65 76 65 6c 20 6f 6e 65 20 73 74 65 70 20 61  level one step a
c0f90 74 20 61 20 74 69 6d 65 2e 20 20 59 6f 75 0a 2a  t a time.  You.*
c0fa0 2a 20 6d 75 73 74 20 67 6f 20 73 74 72 61 69 67  * must go straig
c0fb0 68 74 20 74 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65  ht to locking le
c0fc0 76 65 6c 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63  vel 0..*/.static
c0fd0 20 69 6e 74 20 6f 73 32 4c 6f 63 6b 28 20 73 71   int os2Lock( sq
c0fe0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
c0ff0 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  int locktype ){.
c1000 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
c1010 45 5f 4f 4b 3b 20 20 20 20 20 20 20 2f 2a 20 52  E_OK;       /* R
c1020 65 74 75 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20  eturn code from 
c1030 73 75 62 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20  subroutines */. 
c1040 20 41 50 49 52 45 54 20 72 65 73 20 3d 20 4e 4f   APIRET res = NO
c1050 5f 45 52 52 4f 52 3b 20 20 20 20 2f 2a 20 52 65  _ERROR;    /* Re
c1060 73 75 6c 74 20 6f 66 20 61 6e 20 4f 53 2f 32 20  sult of an OS/2 
c1070 6c 6f 63 6b 20 63 61 6c 6c 20 2a 2f 0a 20 20 69  lock call */.  i
c1080 6e 74 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 20  nt newLocktype; 
c1090 20 20 20 20 20 20 2f 2a 20 53 65 74 20 70 46 69        /* Set pFi
c10a0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 74 6f 20  le->locktype to 
c10b0 74 68 69 73 20 76 61 6c 75 65 20 62 65 66 6f 72  this value befor
c10c0 65 20 65 78 69 74 69 6e 67 20 2a 2f 0a 20 20 69  e exiting */.  i
c10d0 6e 74 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63  nt gotPendingLoc
c10e0 6b 20 3d 20 30 3b 2f 2a 20 54 72 75 65 20 69 66  k = 0;/* True if
c10f0 20 77 65 20 61 63 71 75 69 72 65 64 20 61 20 50   we acquired a P
c1100 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 69 73  ENDING lock this
c1110 20 74 69 6d 65 20 2a 2f 0a 20 20 46 49 4c 45 4c   time */.  FILEL
c1120 4f 43 4b 20 20 4c 6f 63 6b 41 72 65 61 2c 0a 20  OCK  LockArea,. 
c1130 20 20 20 20 20 20 20 20 20 20 20 55 6e 6c 6f 63             Unloc
c1140 6b 41 72 65 61 3b 0a 20 20 6f 73 32 46 69 6c 65  kArea;.  os2File
c1150 20 2a 70 46 69 6c 65 20 3d 20 28 6f 73 32 46 69   *pFile = (os2Fi
c1160 6c 65 2a 29 69 64 3b 0a 20 20 6d 65 6d 73 65 74  le*)id;.  memset
c1170 28 26 4c 6f 63 6b 41 72 65 61 2c 20 30 2c 20 73  (&LockArea, 0, s
c1180 69 7a 65 6f 66 28 4c 6f 63 6b 41 72 65 61 29 29  izeof(LockArea))
c1190 3b 0a 20 20 6d 65 6d 73 65 74 28 26 55 6e 6c 6f  ;.  memset(&Unlo
c11a0 63 6b 41 72 65 61 2c 20 30 2c 20 73 69 7a 65 6f  ckArea, 0, sizeo
c11b0 66 28 55 6e 6c 6f 63 6b 41 72 65 61 29 29 3b 0a  f(UnlockArea));.
c11c0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21    assert( pFile!
c11d0 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 34  =0 );.  OSTRACE4
c11e0 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61  ( "LOCK %d %d wa
c11f0 73 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s %d\n", pFile->
c1200 68 2c 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69  h, locktype, pFi
c1210 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a  le->locktype );.
c1220 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69  .  /* If there i
c1230 73 20 61 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b  s already a lock
c1240 20 6f 66 20 74 68 69 73 20 74 79 70 65 20 6f 72   of this type or
c1250 20 6d 6f 72 65 20 72 65 73 74 72 69 63 74 69 76   more restrictiv
c1260 65 20 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 6f 73  e on the.  ** os
c1270 32 46 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e  2File, do nothin
c1280 67 2e 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65  g. Don't use the
c1290 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69 74 20   end_lock: exit 
c12a0 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20 73 71  path, as.  ** sq
c12b0 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
c12c0 72 28 29 20 68 61 73 6e 27 74 20 62 65 65 6e 20  r() hasn't been 
c12d0 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20 20 2a 2f  called yet..  */
c12e0 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
c12f0 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74 79 70 65  cktype>=locktype
c1300 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43 45 33   ){.    OSTRACE3
c1310 28 20 22 4c 4f 43 4b 20 25 64 20 25 64 20 6f 6b  ( "LOCK %d %d ok
c1320 20 28 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c   (already held)\
c1330 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
c1340 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 65  cktype );.    re
c1350 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c1360 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73    }..  /* Make s
c1370 75 72 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ure the locking 
c1380 73 65 71 75 65 6e 63 65 20 69 73 20 63 6f 72 72  sequence is corr
c1390 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ect.  */.  asser
c13a0 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
c13b0 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c  pe!=NO_LOCK || l
c13c0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
c13d0 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
c13e0 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44  ( locktype!=PEND
c13f0 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73  ING_LOCK );.  as
c1400 73 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d  sert( locktype!=
c1410 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c  RESERVED_LOCK ||
c1420 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
c1430 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b  ==SHARED_LOCK );
c1440 0a 0a 20 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20  ..  /* Lock the 
c1450 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74  PENDING_LOCK byt
c1460 65 20 69 66 20 77 65 20 6e 65 65 64 20 74 6f 20  e if we need to 
c1470 61 63 71 75 69 72 65 20 61 20 50 45 4e 44 49 4e  acquire a PENDIN
c1480 47 20 6c 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61  G lock or.  ** a
c1490 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49   SHARED lock.  I
c14a0 66 20 77 65 20 61 72 65 20 61 63 71 75 69 72 69  f we are acquiri
c14b0 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ng a SHARED lock
c14c0 2c 20 74 68 65 20 61 63 71 75 69 73 69 74 69 6f  , the acquisitio
c14d0 6e 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 50 45  n of.  ** the PE
c14e0 4e 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20  NDING_LOCK byte 
c14f0 69 73 20 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20  is temporary..  
c1500 2a 2f 0a 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65  */.  newLocktype
c1510 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79   = pFile->lockty
c1520 70 65 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 2d  pe;.  if( pFile-
c1530 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f  >locktype==NO_LO
c1540 43 4b 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63  CK.      || (loc
c1550 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
c1560 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e  _LOCK && pFile->
c1570 6c 6f 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56  locktype==RESERV
c1580 45 44 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20  ED_LOCK).  ){.  
c1590 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73    LockArea.lOffs
c15a0 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54  et = PENDING_BYT
c15b0 45 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  E;.    LockArea.
c15c0 6c 52 61 6e 67 65 20 3d 20 31 4c 3b 0a 20 20 20  lRange = 1L;.   
c15d0 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66   UnlockArea.lOff
c15e0 73 65 74 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  set = 0L;.    Un
c15f0 6c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20  lockArea.lRange 
c1600 3d 20 30 4c 3b 0a 0a 20 20 20 20 2f 2a 20 77 61  = 0L;..    /* wa
c1610 69 74 20 6c 6f 6e 67 65 72 20 74 68 61 6e 20 4c  it longer than L
c1620 4f 43 4b 5f 54 49 4d 45 4f 55 54 20 68 65 72 65  OCK_TIMEOUT here
c1630 20 6e 6f 74 20 74 6f 20 68 61 76 65 20 74 6f 20   not to have to 
c1640 74 72 79 20 6d 75 6c 74 69 70 6c 65 20 74 69 6d  try multiple tim
c1650 65 73 20 2a 2f 0a 20 20 20 20 72 65 73 20 3d 20  es */.    res = 
c1660 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c1670 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c1680 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c1690 61 2c 20 31 30 30 4c 2c 20 30 4c 20 29 3b 0a 20  a, 100L, 0L );. 
c16a0 20 20 20 69 66 28 20 72 65 73 20 3d 3d 20 4e 4f     if( res == NO
c16b0 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 20 20  _ERROR ){.      
c16c0 67 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d  gotPendingLock =
c16d0 20 31 3b 0a 20 20 20 20 20 20 4f 53 54 52 41 43   1;.      OSTRAC
c16e0 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 70 65 6e  E3( "LOCK %d pen
c16f0 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f 6c 65 61  ding lock boolea
c1700 6e 20 73 65 74 2e 20 20 72 65 73 3d 25 64 5c 6e  n set.  res=%d\n
c1710 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73  ", pFile->h, res
c1720 20 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20   );.    }.  }.. 
c1730 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 73 68   /* Acquire a sh
c1740 61 72 65 64 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  ared lock.  */. 
c1750 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53   if( locktype==S
c1760 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 72 65  HARED_LOCK && re
c1770 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
c1780 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 46 69  .    assert( pFi
c1790 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f  le->locktype==NO
c17a0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 72 65 73  _LOCK );.    res
c17b0 20 3d 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 70   = getReadLock(p
c17c0 46 69 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 72  File);.    if( r
c17d0 65 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29  es == NO_ERROR )
c17e0 7b 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74  {.      newLockt
c17f0 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43  ype = SHARED_LOC
c1800 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 4f 53 54  K;.    }.    OST
c1810 52 41 43 45 33 28 20 22 4c 4f 43 4b 20 25 64 20  RACE3( "LOCK %d 
c1820 61 63 71 75 69 72 65 20 73 68 61 72 65 64 20 6c  acquire shared l
c1830 6f 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20  ock. res=%d\n", 
c1840 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c1850 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
c1860 72 65 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  re a RESERVED lo
c1870 63 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f  ck.  */.  if( lo
c1880 63 6b 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44  cktype==RESERVED
c1890 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20  _LOCK && res == 
c18a0 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20  NO_ERROR ){.    
c18b0 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
c18c0 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
c18d0 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 4c 6f 63 6b  LOCK );.    Lock
c18e0 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52  Area.lOffset = R
c18f0 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20  ESERVED_BYTE;.  
c1900 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67    LockArea.lRang
c1910 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f  e = 1L;.    Unlo
c1920 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c1930 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   0L;.    UnlockA
c1940 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
c1950 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65  .    res = DosSe
c1960 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
c1970 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
c1980 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f  a, &LockArea, LO
c1990 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29  CK_TIMEOUT, 0L )
c19a0 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d 3d  ;.    if( res ==
c19b0 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20   NO_ERROR ){.   
c19c0 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
c19d0 20 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 3b 0a   RESERVED_LOCK;.
c19e0 20 20 20 20 7d 0a 20 20 20 20 4f 53 54 52 41 43      }.    OSTRAC
c19f0 45 33 28 20 22 4c 4f 43 4b 20 25 64 20 61 63 71  E3( "LOCK %d acq
c1a00 75 69 72 65 20 72 65 73 65 72 76 65 64 20 6c 6f  uire reserved lo
c1a10 63 6b 2e 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70  ck. res=%d\n", p
c1a20 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a  File->h, res );.
c1a30 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69 72    }..  /* Acquir
c1a40 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b  e a PENDING lock
c1a50 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
c1a60 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
c1a70 4c 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e  LOCK && res == N
c1a80 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 6e  O_ERROR ){.    n
c1a90 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e  ewLocktype = PEN
c1aa0 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 67  DING_LOCK;.    g
c1ab0 6f 74 50 65 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20  otPendingLock = 
c1ac0 30 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 32 28  0;.    OSTRACE2(
c1ad0 20 22 4c 4f 43 4b 20 25 64 20 61 63 71 75 69 72   "LOCK %d acquir
c1ae0 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 2e 20  e pending lock. 
c1af0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 62 6f 6f  pending lock boo
c1b00 6c 65 61 6e 20 75 6e 73 65 74 2e 5c 6e 22 2c 20  lean unset.\n", 
c1b10 70 46 69 6c 65 2d 3e 68 20 29 3b 0a 20 20 7d 0a  pFile->h );.  }.
c1b20 0a 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 6e  .  /* Acquire an
c1b30 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 0a   EXCLUSIVE lock.
c1b40 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b 74    */.  if( lockt
c1b50 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
c1b60 4f 43 4b 20 26 26 20 72 65 73 20 3d 3d 20 4e 4f  OCK && res == NO
c1b70 5f 45 52 52 4f 52 20 29 7b 0a 20 20 20 20 61 73  _ERROR ){.    as
c1b80 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
c1b90 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f  ktype>=SHARED_LO
c1ba0 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20  CK );.    res = 
c1bb0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70  unlockReadLock(p
c1bc0 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41  File);.    OSTRA
c1bd0 43 45 32 28 20 22 75 6e 72 65 61 64 6c 6f 63 6b  CE2( "unreadlock
c1be0 20 3d 20 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b   = %d\n", res );
c1bf0 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 4f  .    LockArea.lO
c1c00 66 66 73 65 74 20 3d 20 53 48 41 52 45 44 5f 46  ffset = SHARED_F
c1c10 49 52 53 54 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  IRST;.    LockAr
c1c20 65 61 2e 6c 52 61 6e 67 65 20 3d 20 53 48 41 52  ea.lRange = SHAR
c1c30 45 44 5f 53 49 5a 45 3b 0a 20 20 20 20 55 6e 6c  ED_SIZE;.    Unl
c1c40 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c1c50 3d 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  = 0L;.    Unlock
c1c60 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c  Area.lRange = 0L
c1c70 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53  ;.    res = DosS
c1c80 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
c1c90 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
c1ca0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
c1cb0 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
c1cc0 29 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 3d  );.    if( res =
c1cd0 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c1ce0 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
c1cf0 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b  = EXCLUSIVE_LOCK
c1d00 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
c1d10 20 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 53     OSTRACE2( "OS
c1d20 2f 32 20 65 72 72 6f 72 2d 63 6f 64 65 20 3d 20  /2 error-code = 
c1d30 25 64 5c 6e 22 2c 20 72 65 73 20 29 3b 0a 20 20  %d\n", res );.  
c1d40 20 20 20 20 67 65 74 52 65 61 64 4c 6f 63 6b 28      getReadLock(
c1d50 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  pFile);.    }.  
c1d60 20 20 4f 53 54 52 41 43 45 33 28 20 22 4c 4f 43    OSTRACE3( "LOC
c1d70 4b 20 25 64 20 61 63 71 75 69 72 65 20 65 78 63  K %d acquire exc
c1d80 6c 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 72 65  lusive lock.  re
c1d90 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  s=%d\n", pFile->
c1da0 68 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 0a 20  h, res );.  }.. 
c1db0 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 68 6f   /* If we are ho
c1dc0 6c 64 69 6e 67 20 61 20 50 45 4e 44 49 4e 47 20  lding a PENDING 
c1dd0 6c 6f 63 6b 20 74 68 61 74 20 6f 75 67 68 74 20  lock that ought 
c1de0 74 6f 20 62 65 20 72 65 6c 65 61 73 65 64 2c 20  to be released, 
c1df0 74 68 65 6e 0a 20 20 2a 2a 20 72 65 6c 65 61 73  then.  ** releas
c1e00 65 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20  e it now..  */. 
c1e10 20 69 66 28 20 67 6f 74 50 65 6e 64 69 6e 67 4c   if( gotPendingL
c1e20 6f 63 6b 20 26 26 20 6c 6f 63 6b 74 79 70 65 3d  ock && locktype=
c1e30 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a  =SHARED_LOCK ){.
c1e40 20 20 20 20 69 6e 74 20 72 3b 0a 20 20 20 20 4c      int r;.    L
c1e50 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20  ockArea.lOffset 
c1e60 3d 20 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72  = 0L;.    LockAr
c1e70 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a  ea.lRange = 0L;.
c1e80 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c      UnlockArea.l
c1e90 4f 66 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47  Offset = PENDING
c1ea0 5f 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63  _BYTE;.    Unloc
c1eb0 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31  kArea.lRange = 1
c1ec0 4c 3b 0a 20 20 20 20 72 20 3d 20 44 6f 73 53 65  L;.    r = DosSe
c1ed0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
c1ee0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
c1ef0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f  a, &LockArea, LO
c1f00 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29  CK_TIMEOUT, 0L )
c1f10 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  ;.    OSTRACE3( 
c1f20 22 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 69  "LOCK %d unlocki
c1f30 6e 67 20 70 65 6e 64 69 6e 67 2f 69 73 20 73 68  ng pending/is sh
c1f40 61 72 65 64 2e 20 72 3d 25 64 5c 6e 22 2c 20 70  ared. r=%d\n", p
c1f50 46 69 6c 65 2d 3e 68 2c 20 72 20 29 3b 0a 20 20  File->h, r );.  
c1f60 7d 0a 0a 20 20 2f 2a 20 55 70 64 61 74 65 20 74  }..  /* Update t
c1f70 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
c1f80 6c 6f 63 6b 20 68 61 73 20 68 65 6c 64 20 69 6e  lock has held in
c1f90 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
c1fa0 70 74 6f 72 20 74 68 65 6e 0a 20 20 2a 2a 20 72  ptor then.  ** r
c1fb0 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f 70  eturn the approp
c1fc0 72 69 61 74 65 20 72 65 73 75 6c 74 20 63 6f 64  riate result cod
c1fd0 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65  e..  */.  if( re
c1fe0 73 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b  s == NO_ERROR ){
c1ff0 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
c2000 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
c2010 20 20 4f 53 54 52 41 43 45 34 28 20 22 4c 4f 43    OSTRACE4( "LOC
c2020 4b 20 46 41 49 4c 45 44 20 25 64 20 74 72 79 69  K FAILED %d tryi
c2030 6e 67 20 66 6f 72 20 25 64 20 62 75 74 20 67 6f  ng for %d but go
c2040 74 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e  t %d\n", pFile->
c2050 68 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  h,.             
c2060 20 6c 6f 63 6b 74 79 70 65 2c 20 6e 65 77 4c 6f   locktype, newLo
c2070 63 6b 74 79 70 65 20 29 3b 0a 20 20 20 20 72 63  cktype );.    rc
c2080 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a   = SQLITE_BUSY;.
c2090 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63    }.  pFile->loc
c20a0 6b 74 79 70 65 20 3d 20 6e 65 77 4c 6f 63 6b 74  ktype = newLockt
c20b0 79 70 65 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  ype;.  OSTRACE3(
c20c0 20 22 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64   "LOCK %d now %d
c20d0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
c20e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
c20f0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
c2100 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
c2110 74 69 6e 65 20 63 68 65 63 6b 73 20 69 66 20 74  tine checks if t
c2120 68 65 72 65 20 69 73 20 61 20 52 45 53 45 52 56  here is a RESERV
c2130 45 44 20 6c 6f 63 6b 20 68 65 6c 64 20 6f 6e 20  ED lock held on 
c2140 74 68 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a  the specified.**
c2150 20 66 69 6c 65 20 62 79 20 74 68 69 73 20 6f 72   file by this or
c2160 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65   any other proce
c2170 73 73 2e 20 49 66 20 73 75 63 68 20 61 20 6c 6f  ss. If such a lo
c2180 63 6b 20 69 73 20 68 65 6c 64 2c 20 72 65 74 75  ck is held, retu
c2190 72 6e 0a 2a 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  rn.** non-zero, 
c21a0 6f 74 68 65 72 77 69 73 65 20 7a 65 72 6f 2e 0a  otherwise zero..
c21b0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c21c0 32 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  2CheckReservedLo
c21d0 63 6b 28 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ck( sqlite3_file
c21e0 20 2a 69 64 2c 20 69 6e 74 20 2a 70 4f 75 74 20   *id, int *pOut 
c21f0 29 7b 0a 20 20 69 6e 74 20 72 20 3d 20 30 3b 0a  ){.  int r = 0;.
c2200 20 20 6f 73 32 46 69 6c 65 20 2a 70 46 69 6c 65    os2File *pFile
c2210 20 3d 20 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b   = (os2File*)id;
c2220 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
c2230 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46 69  !=0 );.  if( pFi
c2240 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 52 45  le->locktype>=RE
c2250 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20  SERVED_LOCK ){. 
c2260 20 20 20 72 20 3d 20 31 3b 0a 20 20 20 20 4f 53     r = 1;.    OS
c2270 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52  TRACE3( "TEST WR
c2280 2d 4c 4f 43 4b 20 25 64 20 25 64 20 28 6c 6f 63  -LOCK %d %d (loc
c2290 61 6c 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  al)\n", pFile->h
c22a0 2c 20 72 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  , r );.  }else{.
c22b0 20 20 20 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f      FILELOCK  Lo
c22c0 63 6b 41 72 65 61 2c 0a 20 20 20 20 20 20 20 20  ckArea,.        
c22d0 20 20 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61        UnlockArea
c22e0 3b 0a 20 20 20 20 41 50 49 52 45 54 20 72 63 20  ;.    APIRET rc 
c22f0 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20 20 20 20  = NO_ERROR;.    
c2300 6d 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61  memset(&LockArea
c2310 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b  , 0, sizeof(Lock
c2320 41 72 65 61 29 29 3b 0a 20 20 20 20 6d 65 6d 73  Area));.    mems
c2330 65 74 28 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20  et(&UnlockArea, 
c2340 30 2c 20 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b  0, sizeof(Unlock
c2350 41 72 65 61 29 29 3b 0a 20 20 20 20 4c 6f 63 6b  Area));.    Lock
c2360 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 52  Area.lOffset = R
c2370 45 53 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20  ESERVED_BYTE;.  
c2380 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67    LockArea.lRang
c2390 65 20 3d 20 31 4c 3b 0a 20 20 20 20 55 6e 6c 6f  e = 1L;.    Unlo
c23a0 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d  ckArea.lOffset =
c23b0 20 30 4c 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41   0L;.    UnlockA
c23c0 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b  rea.lRange = 0L;
c23d0 0a 20 20 20 20 72 63 20 3d 20 44 6f 73 53 65 74  .    rc = DosSet
c23e0 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c 65  FileLocks( pFile
c23f0 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65 61  ->h, &UnlockArea
c2400 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f 43  , &LockArea, LOC
c2410 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29 3b  K_TIMEOUT, 0L );
c2420 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20 22  .    OSTRACE3( "
c2430 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20  TEST WR-LOCK %d 
c2440 6c 6f 63 6b 20 72 65 73 65 72 76 65 64 20 62 79  lock reserved by
c2450 74 65 20 72 63 3d 25 64 5c 6e 22 2c 20 70 46 69  te rc=%d\n", pFi
c2460 6c 65 2d 3e 68 2c 20 72 63 20 29 3b 0a 20 20 20  le->h, rc );.   
c2470 20 69 66 28 20 72 63 20 3d 3d 20 4e 4f 5f 45 52   if( rc == NO_ER
c2480 52 4f 52 20 29 7b 0a 20 20 20 20 20 20 41 50 49  ROR ){.      API
c2490 52 45 54 20 72 63 75 20 3d 20 4e 4f 5f 45 52 52  RET rcu = NO_ERR
c24a0 4f 52 3b 20 2f 2a 20 72 65 74 75 72 6e 20 63 6f  OR; /* return co
c24b0 64 65 20 66 6f 72 20 75 6e 6c 6f 63 6b 69 6e 67  de for unlocking
c24c0 20 2a 2f 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72   */.      LockAr
c24d0 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b  ea.lOffset = 0L;
c24e0 0a 20 20 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e  .      LockArea.
c24f0 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20  lRange = 0L;.   
c2500 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f     UnlockArea.lO
c2510 66 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44  ffset = RESERVED
c2520 5f 42 59 54 45 3b 0a 20 20 20 20 20 20 55 6e 6c  _BYTE;.      Unl
c2530 6f 63 6b 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d  ockArea.lRange =
c2540 20 31 4c 3b 0a 20 20 20 20 20 20 72 63 75 20 3d   1L;.      rcu =
c2550 20 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73   DosSetFileLocks
c2560 28 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c  ( pFile->h, &Unl
c2570 6f 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72  ockArea, &LockAr
c2580 65 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54  ea, LOCK_TIMEOUT
c2590 2c 20 30 4c 20 29 3b 0a 20 20 20 20 20 20 4f 53  , 0L );.      OS
c25a0 54 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52  TRACE3( "TEST WR
c25b0 2d 4c 4f 43 4b 20 25 64 20 75 6e 6c 6f 63 6b 20  -LOCK %d unlock 
c25c0 72 65 73 65 72 76 65 64 20 62 79 74 65 20 72 3d  reserved byte r=
c25d0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
c25e0 20 72 63 75 20 29 3b 0a 20 20 20 20 7d 0a 20 20   rcu );.    }.  
c25f0 20 20 72 20 3d 20 21 28 72 63 20 3d 3d 20 4e 4f    r = !(rc == NO
c2600 5f 45 52 52 4f 52 29 3b 0a 20 20 20 20 4f 53 54  _ERROR);.    OST
c2610 52 41 43 45 33 28 20 22 54 45 53 54 20 57 52 2d  RACE3( "TEST WR-
c2620 4c 4f 43 4b 20 25 64 20 25 64 20 28 72 65 6d 6f  LOCK %d %d (remo
c2630 74 65 29 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  te)\n", pFile->h
c2640 2c 20 72 20 29 3b 0a 20 20 7d 0a 20 20 2a 70 4f  , r );.  }.  *pO
c2650 75 74 20 3d 20 72 3b 0a 20 20 72 65 74 75 72 6e  ut = r;.  return
c2660 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
c2670 2a 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c  *.** Lower the l
c2680 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20  ocking level on 
c2690 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
c26a0 69 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20  id to locktype. 
c26b0 20 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73   locktype.** mus
c26c0 74 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c  t be either NO_L
c26d0 4f 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f  OCK or SHARED_LO
c26e0 43 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  CK..**.** If the
c26f0 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f   locking level o
c2700 66 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72  f the file descr
c2710 69 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79  iptor is already
c2720 20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20   at or below.** 
c2730 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f  the requested lo
c2740 63 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69  cking level, thi
c2750 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
c2760 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69  o-op..**.** It i
c2770 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66  s not possible f
c2780 6f 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  or this routine 
c2790 74 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 73  to fail if the s
c27a0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
c27b0 2a 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49  * is NO_LOCK.  I
c27c0 66 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  f the second arg
c27d0 75 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f  ument is SHARED_
c27e0 4c 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72  LOCK then this r
c27f0 6f 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20  outine.** might 
c2800 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
c2810 45 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ERR;.*/.static i
c2820 6e 74 20 6f 73 32 55 6e 6c 6f 63 6b 28 20 73 71  nt os2Unlock( sq
c2830 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
c2840 69 6e 74 20 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  int locktype ){.
c2850 20 20 69 6e 74 20 74 79 70 65 3b 0a 20 20 6f 73    int type;.  os
c2860 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28  2File *pFile = (
c2870 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20 41  os2File*)id;.  A
c2880 50 49 52 45 54 20 72 63 20 3d 20 53 51 4c 49 54  PIRET rc = SQLIT
c2890 45 5f 4f 4b 3b 0a 20 20 41 50 49 52 45 54 20 72  E_OK;.  APIRET r
c28a0 65 73 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a 20  es = NO_ERROR;. 
c28b0 20 46 49 4c 45 4c 4f 43 4b 20 20 4c 6f 63 6b 41   FILELOCK  LockA
c28c0 72 65 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  rea,.           
c28d0 20 55 6e 6c 6f 63 6b 41 72 65 61 3b 0a 20 20 6d   UnlockArea;.  m
c28e0 65 6d 73 65 74 28 26 4c 6f 63 6b 41 72 65 61 2c  emset(&LockArea,
c28f0 20 30 2c 20 73 69 7a 65 6f 66 28 4c 6f 63 6b 41   0, sizeof(LockA
c2900 72 65 61 29 29 3b 0a 20 20 6d 65 6d 73 65 74 28  rea));.  memset(
c2910 26 55 6e 6c 6f 63 6b 41 72 65 61 2c 20 30 2c 20  &UnlockArea, 0, 
c2920 73 69 7a 65 6f 66 28 55 6e 6c 6f 63 6b 41 72 65  sizeof(UnlockAre
c2930 61 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  a));.  assert( p
c2940 46 69 6c 65 21 3d 30 20 29 3b 0a 20 20 61 73 73  File!=0 );.  ass
c2950 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53  ert( locktype<=S
c2960 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20  HARED_LOCK );.  
c2970 4f 53 54 52 41 43 45 34 28 20 22 55 4e 4c 4f 43  OSTRACE4( "UNLOC
c2980 4b 20 25 64 20 74 6f 20 25 64 20 77 61 73 20 25  K %d to %d was %
c2990 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
c29a0 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d  locktype, pFile-
c29b0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 74  >locktype );.  t
c29c0 79 70 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63  ype = pFile->loc
c29d0 6b 74 79 70 65 3b 0a 20 20 69 66 28 20 74 79 70  ktype;.  if( typ
c29e0 65 3e 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  e>=EXCLUSIVE_LOC
c29f0 4b 20 29 7b 0a 20 20 20 20 4c 6f 63 6b 41 72 65  K ){.    LockAre
c2a00 61 2e 6c 4f 66 66 73 65 74 20 3d 20 30 4c 3b 0a  a.lOffset = 0L;.
c2a10 20 20 20 20 4c 6f 63 6b 41 72 65 61 2e 6c 52 61      LockArea.lRa
c2a20 6e 67 65 20 3d 20 30 4c 3b 0a 20 20 20 20 55 6e  nge = 0L;.    Un
c2a30 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74  lockArea.lOffset
c2a40 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
c2a50 0a 20 20 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e  .    UnlockArea.
c2a60 6c 52 61 6e 67 65 20 3d 20 53 48 41 52 45 44 5f  lRange = SHARED_
c2a70 53 49 5a 45 3b 0a 20 20 20 20 72 65 73 20 3d 20  SIZE;.    res = 
c2a80 44 6f 73 53 65 74 46 69 6c 65 4c 6f 63 6b 73 28  DosSetFileLocks(
c2a90 20 70 46 69 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f   pFile->h, &Unlo
c2aa0 63 6b 41 72 65 61 2c 20 26 4c 6f 63 6b 41 72 65  ckArea, &LockAre
c2ab0 61 2c 20 4c 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c  a, LOCK_TIMEOUT,
c2ac0 20 30 4c 20 29 3b 0a 20 20 20 20 4f 53 54 52 41   0L );.    OSTRA
c2ad0 43 45 33 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20  CE3( "UNLOCK %d 
c2ae0 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 72  exclusive lock r
c2af0 65 73 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  es=%d\n", pFile-
c2b00 3e 68 2c 20 72 65 73 20 29 3b 0a 20 20 20 20 69  >h, res );.    i
c2b10 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
c2b20 52 45 44 5f 4c 4f 43 4b 20 26 26 20 67 65 74 52  RED_LOCK && getR
c2b30 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29 20 21  eadLock(pFile) !
c2b40 3d 20 4e 4f 5f 45 52 52 4f 52 20 29 7b 0a 20 20  = NO_ERROR ){.  
c2b50 20 20 20 20 2f 2a 20 54 68 69 73 20 73 68 6f 75      /* This shou
c2b60 6c 64 20 6e 65 76 65 72 20 68 61 70 70 65 6e 2e  ld never happen.
c2b70 20 20 57 65 20 73 68 6f 75 6c 64 20 61 6c 77 61    We should alwa
c2b80 79 73 20 62 65 20 61 62 6c 65 20 74 6f 0a 20 20  ys be able to.  
c2b90 20 20 20 20 2a 2a 20 72 65 61 63 71 75 69 72 65      ** reacquire
c2ba0 20 74 68 65 20 72 65 61 64 20 6c 6f 63 6b 20 2a   the read lock *
c2bb0 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33  /.      OSTRACE3
c2bc0 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 74 6f 20  ( "UNLOCK %d to 
c2bd0 25 64 20 67 65 74 52 65 61 64 4c 6f 63 6b 28 29  %d getReadLock()
c2be0 20 66 61 69 6c 65 64 5c 6e 22 2c 20 70 46 69 6c   failed\n", pFil
c2bf0 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 20 29  e->h, locktype )
c2c00 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  ;.      rc = SQL
c2c10 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
c2c20 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
c2c30 28 20 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44  ( type>=RESERVED
c2c40 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63  _LOCK ){.    Loc
c2c50 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
c2c60 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  0L;.    LockArea
c2c70 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
c2c80 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c2c90 66 73 65 74 20 3d 20 52 45 53 45 52 56 45 44 5f  fset = RESERVED_
c2ca0 42 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b  BYTE;.    Unlock
c2cb0 41 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c  Area.lRange = 1L
c2cc0 3b 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53  ;.    res = DosS
c2cd0 65 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69  etFileLocks( pFi
c2ce0 6c 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72  le->h, &UnlockAr
c2cf0 65 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c  ea, &LockArea, L
c2d00 4f 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20  OCK_TIMEOUT, 0L 
c2d10 29 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28  );.    OSTRACE3(
c2d20 20 22 55 4e 4c 4f 43 4b 20 25 64 20 72 65 73 65   "UNLOCK %d rese
c2d30 72 76 65 64 20 72 65 73 3d 25 64 5c 6e 22 2c 20  rved res=%d\n", 
c2d40 70 46 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b  pFile->h, res );
c2d50 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63 6b 74  .  }.  if( lockt
c2d60 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26 26 20  ype==NO_LOCK && 
c2d70 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43  type>=SHARED_LOC
c2d80 4b 20 29 7b 0a 20 20 20 20 72 65 73 20 3d 20 75  K ){.    res = u
c2d90 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70 46  nlockReadLock(pF
c2da0 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41 43  ile);.    OSTRAC
c2db0 45 35 28 20 22 55 4e 4c 4f 43 4b 20 25 64 20 69  E5( "UNLOCK %d i
c2dc0 73 20 25 64 20 77 61 6e 74 20 25 64 20 72 65 73  s %d want %d res
c2dd0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
c2de0 2c 20 74 79 70 65 2c 20 6c 6f 63 6b 74 79 70 65  , type, locktype
c2df0 2c 20 72 65 73 20 29 3b 0a 20 20 7d 0a 20 20 69  , res );.  }.  i
c2e00 66 28 20 74 79 70 65 3e 3d 50 45 4e 44 49 4e 47  f( type>=PENDING
c2e10 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 4c 6f 63  _LOCK ){.    Loc
c2e20 6b 41 72 65 61 2e 6c 4f 66 66 73 65 74 20 3d 20  kArea.lOffset = 
c2e30 30 4c 3b 0a 20 20 20 20 4c 6f 63 6b 41 72 65 61  0L;.    LockArea
c2e40 2e 6c 52 61 6e 67 65 20 3d 20 30 4c 3b 0a 20 20  .lRange = 0L;.  
c2e50 20 20 55 6e 6c 6f 63 6b 41 72 65 61 2e 6c 4f 66    UnlockArea.lOf
c2e60 66 73 65 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42  fset = PENDING_B
c2e70 59 54 45 3b 0a 20 20 20 20 55 6e 6c 6f 63 6b 41  YTE;.    UnlockA
c2e80 72 65 61 2e 6c 52 61 6e 67 65 20 3d 20 31 4c 3b  rea.lRange = 1L;
c2e90 0a 20 20 20 20 72 65 73 20 3d 20 44 6f 73 53 65  .    res = DosSe
c2ea0 74 46 69 6c 65 4c 6f 63 6b 73 28 20 70 46 69 6c  tFileLocks( pFil
c2eb0 65 2d 3e 68 2c 20 26 55 6e 6c 6f 63 6b 41 72 65  e->h, &UnlockAre
c2ec0 61 2c 20 26 4c 6f 63 6b 41 72 65 61 2c 20 4c 4f  a, &LockArea, LO
c2ed0 43 4b 5f 54 49 4d 45 4f 55 54 2c 20 30 4c 20 29  CK_TIMEOUT, 0L )
c2ee0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 33 28 20  ;.    OSTRACE3( 
c2ef0 22 55 4e 4c 4f 43 4b 20 25 64 20 70 65 6e 64 69  "UNLOCK %d pendi
c2f00 6e 67 20 72 65 73 3d 25 64 5c 6e 22 2c 20 70 46  ng res=%d\n", pF
c2f10 69 6c 65 2d 3e 68 2c 20 72 65 73 20 29 3b 0a 20  ile->h, res );. 
c2f20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b   }.  pFile->lock
c2f30 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b  type = locktype;
c2f40 0a 20 20 4f 53 54 52 41 43 45 33 28 20 22 55 4e  .  OSTRACE3( "UN
c2f50 4c 4f 43 4b 20 25 64 20 6e 6f 77 20 25 64 5c 6e  LOCK %d now %d\n
c2f60 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69  ", pFile->h, pFi
c2f70 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a  le->locktype );.
c2f80 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
c2f90 2f 2a 0a 2a 2a 20 43 6f 6e 74 72 6f 6c 20 61 6e  /*.** Control an
c2fa0 64 20 71 75 65 72 79 20 6f 66 20 74 68 65 20 6f  d query of the o
c2fb0 70 65 6e 20 66 69 6c 65 20 68 61 6e 64 6c 65 2e  pen file handle.
c2fc0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  .*/.static int o
c2fd0 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 28 73 71  s2FileControl(sq
c2fe0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20  lite3_file *id, 
c2ff0 69 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41  int op, void *pA
c3000 72 67 29 7b 0a 20 20 73 77 69 74 63 68 28 20 6f  rg){.  switch( o
c3010 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  p ){.    case SQ
c3020 4c 49 54 45 5f 46 43 4e 54 4c 5f 4c 4f 43 4b 53  LITE_FCNTL_LOCKS
c3030 54 41 54 45 3a 20 7b 0a 20 20 20 20 20 20 2a 28  TATE: {.      *(
c3040 69 6e 74 2a 29 70 41 72 67 20 3d 20 28 28 6f 73  int*)pArg = ((os
c3050 32 46 69 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b  2File*)id)->lock
c3060 74 79 70 65 3b 0a 20 20 20 20 20 20 4f 53 54 52  type;.      OSTR
c3070 41 43 45 33 28 20 22 46 43 4e 54 4c 5f 4c 4f 43  ACE3( "FCNTL_LOC
c3080 4b 53 54 41 54 45 20 25 64 20 6c 6f 63 6b 3d 25  KSTATE %d lock=%
c3090 64 5c 6e 22 2c 20 28 28 6f 73 32 46 69 6c 65 2a  d\n", ((os2File*
c30a0 29 69 64 29 2d 3e 68 2c 20 28 28 6f 73 32 46 69  )id)->h, ((os2Fi
c30b0 6c 65 2a 29 69 64 29 2d 3e 6c 6f 63 6b 74 79 70  le*)id)->locktyp
c30c0 65 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  e );.      retur
c30d0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  n SQLITE_OK;.   
c30e0 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
c30f0 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a  SQLITE_ERROR;.}.
c3100 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
c3110 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20 69 6e  e sector size in
c3120 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 75 6e   bytes of the un
c3130 64 65 72 6c 79 69 6e 67 20 62 6c 6f 63 6b 20 64  derlying block d
c3140 65 76 69 63 65 20 66 6f 72 0a 2a 2a 20 74 68 65  evice for.** the
c3150 20 73 70 65 63 69 66 69 65 64 20 66 69 6c 65 2e   specified file.
c3160 20 54 68 69 73 20 69 73 20 61 6c 6d 6f 73 74 20   This is almost 
c3170 61 6c 77 61 79 73 20 35 31 32 20 62 79 74 65 73  always 512 bytes
c3180 2c 20 62 75 74 20 6d 61 79 20 62 65 0a 2a 2a 20  , but may be.** 
c3190 6c 61 72 67 65 72 20 66 6f 72 20 73 6f 6d 65 20  larger for some 
c31a0 64 65 76 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53  devices..**.** S
c31b0 51 4c 69 74 65 20 63 6f 64 65 20 61 73 73 75 6d  QLite code assum
c31c0 65 73 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  es this function
c31d0 20 63 61 6e 6e 6f 74 20 66 61 69 6c 2e 20 49 74   cannot fail. It
c31e0 20 61 6c 73 6f 20 61 73 73 75 6d 65 73 20 74 68   also assumes th
c31f0 61 74 0a 2a 2a 20 69 66 20 74 77 6f 20 66 69 6c  at.** if two fil
c3200 65 73 20 61 72 65 20 63 72 65 61 74 65 64 20 69  es are created i
c3210 6e 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 2d  n the same file-
c3220 73 79 73 74 65 6d 20 64 69 72 65 63 74 6f 72 79  system directory
c3230 20 28 69 2e 65 2e 0a 2a 2a 20 61 20 64 61 74 61   (i.e..** a data
c3240 62 61 73 65 20 61 6e 64 20 69 74 73 20 6a 6f 75  base and its jou
c3250 72 6e 61 6c 20 66 69 6c 65 29 20 74 68 61 74 20  rnal file) that 
c3260 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a 65 20  the sector size 
c3270 77 69 6c 6c 20 62 65 20 74 68 65 0a 2a 2a 20 73  will be the.** s
c3280 61 6d 65 20 66 6f 72 20 62 6f 74 68 2e 0a 2a 2f  ame for both..*/
c3290 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53  .static int os2S
c32a0 65 63 74 6f 72 53 69 7a 65 28 73 71 6c 69 74 65  ectorSize(sqlite
c32b0 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 72  3_file *id){.  r
c32c0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 45 46  eturn SQLITE_DEF
c32d0 41 55 4c 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45  AULT_SECTOR_SIZE
c32e0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
c32f0 6e 20 61 20 76 65 63 74 6f 72 20 6f 66 20 64 65  n a vector of de
c3300 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73  vice characteris
c3310 74 69 63 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tics..*/.static 
c3320 69 6e 74 20 6f 73 32 44 65 76 69 63 65 43 68 61  int os2DeviceCha
c3330 72 61 63 74 65 72 69 73 74 69 63 73 28 73 71 6c  racteristics(sql
c3340 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a  ite3_file *id){.
c3350 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a    return 0;.}...
c3360 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74 65 72 20  /*.** Character 
c3370 73 65 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20 6f  set conversion o
c3380 62 6a 65 63 74 73 20 75 73 65 64 20 62 79 20 63  bjects used by c
c3390 6f 6e 76 65 72 73 69 6f 6e 20 72 6f 75 74 69 6e  onversion routin
c33a0 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 55 63  es..*/.static Uc
c33b0 6f 6e 76 4f 62 6a 65 63 74 20 75 63 55 74 66 38  onvObject ucUtf8
c33c0 20 3d 20 4e 55 4c 4c 3b 20 2f 2a 20 63 6f 6e 76   = NULL; /* conv
c33d0 65 72 74 20 62 65 74 77 65 65 6e 20 55 54 46 2d  ert between UTF-
c33e0 38 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a 73  8 and UCS-2 */.s
c33f0 74 61 74 69 63 20 55 63 6f 6e 76 4f 62 6a 65 63  tatic UconvObjec
c3400 74 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c 3b 20  t uclCp = NULL; 
c3410 20 2f 2a 20 63 6f 6e 76 65 72 74 20 62 65 74 77   /* convert betw
c3420 65 65 6e 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61  een local codepa
c3430 67 65 20 61 6e 64 20 55 43 53 2d 32 20 2a 2f 0a  ge and UCS-2 */.
c3440 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75  ./*.** Helper fu
c3450 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 69 74 69 61  nction to initia
c3460 6c 69 7a 65 20 74 68 65 20 63 6f 6e 76 65 72 73  lize the convers
c3470 69 6f 6e 20 6f 62 6a 65 63 74 73 20 66 72 6f 6d  ion objects from
c3480 20 61 6e 64 20 74 6f 20 55 54 46 2d 38 2e 0a 2a   and to UTF-8..*
c3490 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
c34a0 69 74 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 20  itUconvObjects( 
c34b0 76 6f 69 64 20 29 7b 0a 20 20 69 66 28 20 55 6e  void ){.  if( Un
c34c0 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62 6a 65  iCreateUconvObje
c34d0 63 74 28 20 55 54 46 5f 38 2c 20 26 75 63 55 74  ct( UTF_8, &ucUt
c34e0 66 38 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43  f8 ) != ULS_SUCC
c34f0 45 53 53 20 29 0a 20 20 20 20 75 63 55 74 66 38  ESS ).    ucUtf8
c3500 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66 20 28 20   = NULL;.  if ( 
c3510 55 6e 69 43 72 65 61 74 65 55 63 6f 6e 76 4f 62  UniCreateUconvOb
c3520 6a 65 63 74 28 20 28 55 6e 69 43 68 61 72 20 2a  ject( (UniChar *
c3530 29 4c 22 40 70 61 74 68 3d 79 65 73 22 2c 20 26  )L"@path=yes", &
c3540 75 63 6c 43 70 20 29 20 21 3d 20 55 4c 53 5f 53  uclCp ) != ULS_S
c3550 55 43 43 45 53 53 20 29 0a 20 20 20 20 75 63 6c  UCCESS ).    ucl
c3560 43 70 20 3d 20 4e 55 4c 4c 3b 0a 7d 0a 0a 2f 2a  Cp = NULL;.}../*
c3570 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74  .** Helper funct
c3580 69 6f 6e 20 74 6f 20 66 72 65 65 20 74 68 65 20  ion to free the 
c3590 63 6f 6e 76 65 72 73 69 6f 6e 20 6f 62 6a 65 63  conversion objec
c35a0 74 73 20 66 72 6f 6d 20 61 6e 64 20 74 6f 20 55  ts from and to U
c35b0 54 46 2d 38 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  TF-8..*/.static 
c35c0 76 6f 69 64 20 66 72 65 65 55 63 6f 6e 76 4f 62  void freeUconvOb
c35d0 6a 65 63 74 73 28 20 76 6f 69 64 20 29 7b 0a 20  jects( void ){. 
c35e0 20 69 66 20 28 20 75 63 55 74 66 38 20 29 0a 20   if ( ucUtf8 ). 
c35f0 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e 76 4f     UniFreeUconvO
c3600 62 6a 65 63 74 28 20 75 63 55 74 66 38 20 29 3b  bject( ucUtf8 );
c3610 0a 20 20 69 66 20 28 20 75 63 6c 43 70 20 29 0a  .  if ( uclCp ).
c3620 20 20 20 20 55 6e 69 46 72 65 65 55 63 6f 6e 76      UniFreeUconv
c3630 4f 62 6a 65 63 74 28 20 75 63 6c 43 70 20 29 3b  Object( uclCp );
c3640 0a 20 20 75 63 55 74 66 38 20 3d 20 4e 55 4c 4c  .  ucUtf8 = NULL
c3650 3b 0a 20 20 75 63 6c 43 70 20 3d 20 4e 55 4c 4c  ;.  uclCp = NULL
c3660 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65  ;.}../*.** Helpe
c3670 72 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f  r function to co
c3680 6e 76 65 72 74 20 55 54 46 2d 38 20 66 69 6c 65  nvert UTF-8 file
c3690 6e 61 6d 65 73 20 74 6f 20 6c 6f 63 61 6c 20 4f  names to local O
c36a0 53 2f 32 20 63 6f 64 65 70 61 67 65 2e 0a 2a 2a  S/2 codepage..**
c36b0 20 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72   The two-step pr
c36c0 6f 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e  ocess: first con
c36d0 76 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e  vert the incomin
c36e0 67 20 55 54 46 2d 38 20 73 74 72 69 6e 67 0a 2a  g UTF-8 string.*
c36f0 2a 20 69 6e 74 6f 20 55 43 53 2d 32 20 61 6e 64  * into UCS-2 and
c3700 20 74 68 65 6e 20 66 72 6f 6d 20 55 43 53 2d 32   then from UCS-2
c3710 20 74 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   to the current 
c3720 63 6f 64 65 70 61 67 65 2e 0a 2a 2a 20 54 68 65  codepage..** The
c3730 20 72 65 74 75 72 6e 65 64 20 63 68 61 72 20 70   returned char p
c3740 6f 69 6e 74 65 72 20 68 61 73 20 74 6f 20 62 65  ointer has to be
c3750 20 66 72 65 65 64 2e 0a 2a 2f 0a 73 74 61 74 69   freed..*/.stati
c3760 63 20 63 68 61 72 20 2a 63 6f 6e 76 65 72 74 55  c char *convertU
c3770 74 66 38 50 61 74 68 54 6f 43 70 28 20 63 6f 6e  tf8PathToCp( con
c3780 73 74 20 63 68 61 72 20 2a 69 6e 20 29 7b 0a 20  st char *in ){. 
c3790 20 55 6e 69 43 68 61 72 20 74 65 6d 70 50 61 74   UniChar tempPat
c37a0 68 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a 20  h[CCHMAXPATH];. 
c37b0 20 63 68 61 72 20 2a 6f 75 74 20 3d 20 28 63 68   char *out = (ch
c37c0 61 72 20 2a 29 63 61 6c 6c 6f 63 28 20 43 43 48  ar *)calloc( CCH
c37d0 4d 41 58 50 41 54 48 2c 20 31 20 29 3b 0a 0a 20  MAXPATH, 1 );.. 
c37e0 20 69 66 28 20 21 6f 75 74 20 29 0a 20 20 20 20   if( !out ).    
c37f0 72 65 74 75 72 6e 20 4e 55 4c 4c 3b 0a 0a 20 20  return NULL;..  
c3800 69 66 28 20 21 75 63 55 74 66 38 20 7c 7c 20 21  if( !ucUtf8 || !
c3810 75 63 6c 43 70 20 29 0a 20 20 20 20 69 6e 69 74  uclCp ).    init
c3820 55 63 6f 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a  UconvObjects();.
c3830 0a 20 20 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20  .  /* determine 
c3840 73 74 72 69 6e 67 20 66 6f 72 20 74 68 65 20 63  string for the c
c3850 6f 6e 76 65 72 73 69 6f 6e 20 6f 66 20 55 54 46  onversion of UTF
c3860 2d 38 20 77 68 69 63 68 20 69 73 20 43 50 31 32  -8 which is CP12
c3870 30 38 20 2a 2f 0a 20 20 69 66 28 20 55 6e 69 53  08 */.  if( UniS
c3880 74 72 54 6f 55 63 73 28 20 75 63 55 74 66 38 2c  trToUcs( ucUtf8,
c3890 20 74 65 6d 70 50 61 74 68 2c 20 28 63 68 61 72   tempPath, (char
c38a0 20 2a 29 69 6e 2c 20 43 43 48 4d 41 58 50 41 54   *)in, CCHMAXPAT
c38b0 48 20 29 20 21 3d 20 55 4c 53 5f 53 55 43 43 45  H ) != ULS_SUCCE
c38c0 53 53 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20  SS ).    return 
c38d0 6f 75 74 3b 20 2f 2a 20 69 66 20 63 6f 6e 76 65  out; /* if conve
c38e0 72 73 69 6f 6e 20 66 61 69 6c 73 2c 20 72 65 74  rsion fails, ret
c38f0 75 72 6e 20 74 68 65 20 65 6d 70 74 79 20 73 74  urn the empty st
c3900 72 69 6e 67 20 2a 2f 0a 0a 20 20 2f 2a 20 63 6f  ring */..  /* co
c3910 6e 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72  nversion for cur
c3920 72 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68  rent codepage wh
c3930 69 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20  ich can be used 
c3940 66 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 55  for paths */.  U
c3950 6e 69 53 74 72 46 72 6f 6d 55 63 73 28 20 75 63  niStrFromUcs( uc
c3960 6c 43 70 2c 20 6f 75 74 2c 20 74 65 6d 70 50 61  lCp, out, tempPa
c3970 74 68 2c 20 43 43 48 4d 41 58 50 41 54 48 20 29  th, CCHMAXPATH )
c3980 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6f 75 74 3b  ;..  return out;
c3990 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 65 6c 70 65 72  .}../*.** Helper
c39a0 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 6f 6e   function to con
c39b0 76 65 72 74 20 66 69 6c 65 6e 61 6d 65 73 20 66  vert filenames f
c39c0 72 6f 6d 20 6c 6f 63 61 6c 20 63 6f 64 65 70 61  rom local codepa
c39d0 67 65 20 74 6f 20 55 54 46 2d 38 2e 0a 2a 2a 20  ge to UTF-8..** 
c39e0 54 68 65 20 74 77 6f 2d 73 74 65 70 20 70 72 6f  The two-step pro
c39f0 63 65 73 73 3a 20 66 69 72 73 74 20 63 6f 6e 76  cess: first conv
c3a00 65 72 74 20 74 68 65 20 69 6e 63 6f 6d 69 6e 67  ert the incoming
c3a10 20 63 6f 64 65 70 61 67 65 2d 73 70 65 63 69 66   codepage-specif
c3a20 69 63 0a 2a 2a 20 73 74 72 69 6e 67 20 69 6e 74  ic.** string int
c3a30 6f 20 55 43 53 2d 32 20 61 6e 64 20 74 68 65 6e  o UCS-2 and then
c3a40 20 66 72 6f 6d 20 55 43 53 2d 32 20 74 6f 20 74   from UCS-2 to t
c3a50 68 65 20 63 6f 64 65 70 61 67 65 20 6f 66 20 55  he codepage of U
c3a60 54 46 2d 38 2e 0a 2a 2a 20 54 68 65 20 72 65 74  TF-8..** The ret
c3a70 75 72 6e 65 64 20 63 68 61 72 20 70 6f 69 6e 74  urned char point
c3a80 65 72 20 68 61 73 20 74 6f 20 62 65 20 66 72 65  er has to be fre
c3a90 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ed..**.** This f
c3aa0 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 6e 2d 73  unction is non-s
c3ab0 74 61 74 69 63 20 74 6f 20 62 65 20 61 62 6c 65  tatic to be able
c3ac0 20 74 6f 20 75 73 65 20 74 68 69 73 20 69 6e 20   to use this in 
c3ad0 73 68 65 6c 6c 2e 63 20 61 6e 64 0a 2a 2a 20 73  shell.c and.** s
c3ae0 69 6d 69 6c 61 72 20 61 70 70 6c 69 63 61 74 69  imilar applicati
c3af0 6f 6e 73 20 74 68 61 74 20 74 61 6b 65 20 63 6f  ons that take co
c3b00 6d 6d 61 6e 64 20 6c 69 6e 65 20 61 72 67 75 6d  mmand line argum
c3b10 65 6e 74 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a 63  ents..*/.char *c
c3b20 6f 6e 76 65 72 74 43 70 50 61 74 68 54 6f 55 74  onvertCpPathToUt
c3b30 66 38 28 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  f8( const char *
c3b40 69 6e 20 29 7b 0a 20 20 55 6e 69 43 68 61 72 20  in ){.  UniChar 
c3b50 74 65 6d 70 50 61 74 68 5b 43 43 48 4d 41 58 50  tempPath[CCHMAXP
c3b60 41 54 48 5d 3b 0a 20 20 63 68 61 72 20 2a 6f 75  ATH];.  char *ou
c3b70 74 20 3d 20 28 63 68 61 72 20 2a 29 63 61 6c 6c  t = (char *)call
c3b80 6f 63 28 20 43 43 48 4d 41 58 50 41 54 48 2c 20  oc( CCHMAXPATH, 
c3b90 31 20 29 3b 0a 0a 20 20 69 66 28 20 21 6f 75 74  1 );..  if( !out
c3ba0 20 29 0a 20 20 20 20 72 65 74 75 72 6e 20 4e 55   ).    return NU
c3bb0 4c 4c 3b 0a 0a 20 20 69 66 28 20 21 75 63 55 74  LL;..  if( !ucUt
c3bc0 66 38 20 7c 7c 20 21 75 63 6c 43 70 20 29 0a 20  f8 || !uclCp ). 
c3bd0 20 20 20 69 6e 69 74 55 63 6f 6e 76 4f 62 6a 65     initUconvObje
c3be0 63 74 73 28 29 3b 0a 0a 20 20 2f 2a 20 63 6f 6e  cts();..  /* con
c3bf0 76 65 72 73 69 6f 6e 20 66 6f 72 20 63 75 72 72  version for curr
c3c00 65 6e 74 20 63 6f 64 65 70 61 67 65 20 77 68 69  ent codepage whi
c3c10 63 68 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  ch can be used f
c3c20 6f 72 20 70 61 74 68 73 20 2a 2f 0a 20 20 69 66  or paths */.  if
c3c30 28 20 55 6e 69 53 74 72 54 6f 55 63 73 28 20 75  ( UniStrToUcs( u
c3c40 63 6c 43 70 2c 20 74 65 6d 70 50 61 74 68 2c 20  clCp, tempPath, 
c3c50 28 63 68 61 72 20 2a 29 69 6e 2c 20 43 43 48 4d  (char *)in, CCHM
c3c60 41 58 50 41 54 48 20 29 20 21 3d 20 55 4c 53 5f  AXPATH ) != ULS_
c3c70 53 55 43 43 45 53 53 20 29 0a 20 20 20 20 72 65  SUCCESS ).    re
c3c80 74 75 72 6e 20 6f 75 74 3b 20 2f 2a 20 69 66 20  turn out; /* if 
c3c90 63 6f 6e 76 65 72 73 69 6f 6e 20 66 61 69 6c 73  conversion fails
c3ca0 2c 20 72 65 74 75 72 6e 20 74 68 65 20 65 6d 70  , return the emp
c3cb0 74 79 20 73 74 72 69 6e 67 20 2a 2f 0a 0a 20 20  ty string */..  
c3cc0 2f 2a 20 64 65 74 65 72 6d 69 6e 65 20 73 74 72  /* determine str
c3cd0 69 6e 67 20 66 6f 72 20 74 68 65 20 63 6f 6e 76  ing for the conv
c3ce0 65 72 73 69 6f 6e 20 6f 66 20 55 54 46 2d 38 20  ersion of UTF-8 
c3cf0 77 68 69 63 68 20 69 73 20 43 50 31 32 30 38 20  which is CP1208 
c3d00 2a 2f 0a 20 20 55 6e 69 53 74 72 46 72 6f 6d 55  */.  UniStrFromU
c3d10 63 73 28 20 75 63 55 74 66 38 2c 20 6f 75 74 2c  cs( ucUtf8, out,
c3d20 20 74 65 6d 70 50 61 74 68 2c 20 43 43 48 4d 41   tempPath, CCHMA
c3d30 58 50 41 54 48 20 29 3b 0a 0a 20 20 72 65 74 75  XPATH );..  retu
c3d40 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  rn out;.}../*.**
c3d50 20 54 68 69 73 20 76 65 63 74 6f 72 20 64 65 66   This vector def
c3d60 69 6e 65 73 20 61 6c 6c 20 74 68 65 20 6d 65 74  ines all the met
c3d70 68 6f 64 73 20 74 68 61 74 20 63 61 6e 20 6f 70  hods that can op
c3d80 65 72 61 74 65 20 6f 6e 20 61 6e 0a 2a 2a 20 73  erate on an.** s
c3d90 71 6c 69 74 65 33 5f 66 69 6c 65 20 66 6f 72 20  qlite3_file for 
c3da0 6f 73 32 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  os2..*/.static c
c3db0 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f  onst sqlite3_io_
c3dc0 6d 65 74 68 6f 64 73 20 6f 73 32 49 6f 4d 65 74  methods os2IoMet
c3dd0 68 6f 64 20 3d 20 7b 0a 20 20 31 2c 20 20 20 20  hod = {.  1,    
c3de0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c3df0 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
c3e00 2a 2f 0a 20 20 6f 73 32 43 6c 6f 73 65 2c 0a 20  */.  os2Close,. 
c3e10 20 6f 73 32 52 65 61 64 2c 0a 20 20 6f 73 32 57   os2Read,.  os2W
c3e20 72 69 74 65 2c 0a 20 20 6f 73 32 54 72 75 6e 63  rite,.  os2Trunc
c3e30 61 74 65 2c 0a 20 20 6f 73 32 53 79 6e 63 2c 0a  ate,.  os2Sync,.
c3e40 20 20 6f 73 32 46 69 6c 65 53 69 7a 65 2c 0a 20    os2FileSize,. 
c3e50 20 6f 73 32 4c 6f 63 6b 2c 0a 20 20 6f 73 32 55   os2Lock,.  os2U
c3e60 6e 6c 6f 63 6b 2c 0a 20 20 6f 73 32 43 68 65 63  nlock,.  os2Chec
c3e70 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 0a 20  kReservedLock,. 
c3e80 20 6f 73 32 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c   os2FileControl,
c3e90 0a 20 20 6f 73 32 53 65 63 74 6f 72 53 69 7a 65  .  os2SectorSize
c3ea0 2c 0a 20 20 6f 73 32 44 65 76 69 63 65 43 68 61  ,.  os2DeviceCha
c3eb0 72 61 63 74 65 72 69 73 74 69 63 73 0a 7d 3b 0a  racteristics.};.
c3ec0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
c3ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3f00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
c3f10 20 48 65 72 65 20 65 6e 64 73 20 74 68 65 20 49   Here ends the I
c3f20 2f 4f 20 6d 65 74 68 6f 64 73 20 74 68 61 74 20  /O methods that 
c3f30 66 6f 72 6d 20 74 68 65 20 73 71 6c 69 74 65 33  form the sqlite3
c3f40 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 6f 62 6a 65  _io_methods obje
c3f50 63 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  ct..**.** The ne
c3f60 78 74 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64 65  xt block of code
c3f70 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20   implements the 
c3f80 56 46 53 20 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a  VFS methods..***
c3f90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3fa0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3fb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3fc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c3fd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
c3fe0 2a 20 43 72 65 61 74 65 20 61 20 74 65 6d 70 6f  * Create a tempo
c3ff0 72 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 69  rary file name i
c4000 6e 20 7a 42 75 66 2e 20 20 7a 42 75 66 20 6d 75  n zBuf.  zBuf mu
c4010 73 74 20 62 65 20 62 69 67 20 65 6e 6f 75 67 68  st be big enough
c4020 20 74 6f 0a 2a 2a 20 68 6f 6c 64 20 61 74 20 70   to.** hold at p
c4030 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 20  Vfs->mxPathname 
c4040 63 68 61 72 61 63 74 65 72 73 2e 0a 2a 2f 0a 73  characters..*/.s
c4050 74 61 74 69 63 20 69 6e 74 20 67 65 74 54 65 6d  tatic int getTem
c4060 70 6e 61 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20  pname(int nBuf, 
c4070 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a 20 20  char *zBuf ){.  
c4080 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
c4090 69 67 6e 65 64 20 63 68 61 72 20 7a 43 68 61 72  igned char zChar
c40a0 73 5b 5d 20 3d 0a 20 20 20 20 22 61 62 63 64 65  s[] =.    "abcde
c40b0 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75  fghijklmnopqrstu
c40c0 76 77 78 79 7a 22 0a 20 20 20 20 22 41 42 43 44  vwxyz".    "ABCD
c40d0 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54  EFGHIJKLMNOPQRST
c40e0 55 56 57 58 59 5a 22 0a 20 20 20 20 22 30 31 32  UVWXYZ".    "012
c40f0 33 34 35 36 37 38 39 22 3b 0a 20 20 69 6e 74 20  3456789";.  int 
c4100 69 2c 20 6a 3b 0a 20 20 63 68 61 72 20 7a 54 65  i, j;.  char zTe
c4110 6d 70 50 61 74 68 42 75 66 5b 33 5d 3b 0a 20 20  mpPathBuf[3];.  
c4120 50 53 5a 20 7a 54 65 6d 70 50 61 74 68 20 3d 20  PSZ zTempPath = 
c4130 28 50 53 5a 29 26 7a 54 65 6d 70 50 61 74 68 42  (PSZ)&zTempPathB
c4140 75 66 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  uf;.  if( sqlite
c4150 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
c4160 20 29 7b 0a 20 20 20 20 7a 54 65 6d 70 50 61 74   ){.    zTempPat
c4170 68 20 3d 20 73 71 6c 69 74 65 33 5f 74 65 6d 70  h = sqlite3_temp
c4180 5f 64 69 72 65 63 74 6f 72 79 3b 0a 20 20 7d 65  _directory;.  }e
c4190 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 44 6f 73  lse{.    if( Dos
c41a0 53 63 61 6e 45 6e 76 28 20 28 50 53 5a 29 22 54  ScanEnv( (PSZ)"T
c41b0 45 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61 74 68  EMP", &zTempPath
c41c0 20 29 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20   ) ){.      if( 
c41d0 44 6f 73 53 63 61 6e 45 6e 76 28 20 28 50 53 5a  DosScanEnv( (PSZ
c41e0 29 22 54 4d 50 22 2c 20 26 7a 54 65 6d 70 50 61  )"TMP", &zTempPa
c41f0 74 68 20 29 20 29 7b 0a 20 20 20 20 20 20 20 20  th ) ){.        
c4200 69 66 28 20 44 6f 73 53 63 61 6e 45 6e 76 28 20  if( DosScanEnv( 
c4210 28 50 53 5a 29 22 54 4d 50 44 49 52 22 2c 20 26  (PSZ)"TMPDIR", &
c4220 7a 54 65 6d 70 50 61 74 68 20 29 20 29 7b 0a 20  zTempPath ) ){. 
c4230 20 20 20 20 20 20 20 20 20 20 55 4c 4f 4e 47 20            ULONG 
c4240 75 6c 44 72 69 76 65 4e 75 6d 20 3d 20 30 2c 20  ulDriveNum = 0, 
c4250 75 6c 44 72 69 76 65 4d 61 70 20 3d 20 30 3b 0a  ulDriveMap = 0;.
c4260 20 20 20 20 20 20 20 20 20 20 20 44 6f 73 51 75             DosQu
c4270 65 72 79 43 75 72 72 65 6e 74 44 69 73 6b 28 20  eryCurrentDisk( 
c4280 26 75 6c 44 72 69 76 65 4e 75 6d 2c 20 26 75 6c  &ulDriveNum, &ul
c4290 44 72 69 76 65 4d 61 70 20 29 3b 0a 20 20 20 20  DriveMap );.    
c42a0 20 20 20 20 20 20 20 73 70 72 69 6e 74 66 28 20         sprintf( 
c42b0 28 63 68 61 72 2a 29 7a 54 65 6d 70 50 61 74 68  (char*)zTempPath
c42c0 2c 20 22 25 63 3a 22 2c 20 28 63 68 61 72 29 28  , "%c:", (char)(
c42d0 20 27 41 27 20 2b 20 75 6c 44 72 69 76 65 4e 75   'A' + ulDriveNu
c42e0 6d 20 2d 20 31 20 29 20 29 3b 0a 20 20 20 20 20  m - 1 ) );.     
c42f0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
c4300 20 7d 0a 20 20 7d 0a 20 20 2f 2a 20 53 74 72 69   }.  }.  /* Stri
c4310 70 20 6f 66 66 20 61 20 74 72 61 69 6c 69 6e 67  p off a trailing
c4320 20 73 6c 61 73 68 65 73 20 6f 72 20 62 61 63 6b   slashes or back
c4330 73 6c 61 73 68 65 73 2c 20 6f 74 68 65 72 77 69  slashes, otherwi
c4340 73 65 20 77 65 20 77 6f 75 6c 64 20 67 65 74 20  se we would get 
c4350 2a 0a 20 20 20 2a 20 6d 75 6c 74 69 70 6c 65 20  *.   * multiple 
c4360 28 62 61 63 6b 29 73 6c 61 73 68 65 73 20 77 68  (back)slashes wh
c4370 69 63 68 20 63 61 75 73 65 73 20 44 6f 73 4f 70  ich causes DosOp
c4380 65 6e 28 29 20 74 6f 20 66 61 69 6c 2e 20 20 20  en() to fail.   
c4390 20 20 20 20 20 20 20 20 20 20 20 2a 0a 20 20 20             *.   
c43a0 2a 20 54 72 61 69 6c 69 6e 67 20 73 70 61 63 65  * Trailing space
c43b0 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77 65  s are not allowe
c43c0 64 2c 20 65 69 74 68 65 72 2e 20 20 20 20 20 20  d, either.      
c43d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c43e0 20 20 20 20 20 20 2a 2f 0a 20 20 6a 20 3d 20 73        */.  j = s
c43f0 74 72 6c 65 6e 28 7a 54 65 6d 70 50 61 74 68 29  trlen(zTempPath)
c4400 3b 0a 20 20 77 68 69 6c 65 28 20 6a 20 3e 20 30  ;.  while( j > 0
c4410 20 26 26 20 28 20 7a 54 65 6d 70 50 61 74 68 5b   && ( zTempPath[
c4420 6a 2d 31 5d 20 3d 3d 20 27 5c 5c 27 20 7c 7c 20  j-1] == '\\' || 
c4430 7a 54 65 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d  zTempPath[j-1] =
c4440 3d 20 27 2f 27 0a 20 20 20 20 20 20 20 20 20 20  = '/'.          
c4450 20 20 20 20 20 20 20 20 20 20 7c 7c 20 7a 54 65            || zTe
c4460 6d 70 50 61 74 68 5b 6a 2d 31 5d 20 3d 3d 20 27  mpPath[j-1] == '
c4470 20 27 20 29 20 29 7b 0a 20 20 20 20 6a 2d 2d 3b   ' ) ){.    j--;
c4480 0a 20 20 7d 0a 20 20 7a 54 65 6d 70 50 61 74 68  .  }.  zTempPath
c4490 5b 6a 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69 66  [j] = '\0';.  if
c44a0 28 20 21 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f  ( !sqlite3_temp_
c44b0 64 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20  directory ){.   
c44c0 20 63 68 61 72 20 2a 7a 54 65 6d 70 50 61 74 68   char *zTempPath
c44d0 55 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50  UTF = convertCpP
c44e0 61 74 68 54 6f 55 74 66 38 28 20 7a 54 65 6d 70  athToUtf8( zTemp
c44f0 50 61 74 68 20 29 3b 0a 20 20 20 20 73 71 6c 69  Path );.    sqli
c4500 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42  te3_snprintf( nB
c4510 75 66 2d 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20  uf-30, zBuf,.   
c4520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4530 20 20 20 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f     "%s\\"SQLITE_
c4540 54 45 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58  TEMP_FILE_PREFIX
c4550 2c 20 7a 54 65 6d 70 50 61 74 68 55 54 46 20 29  , zTempPathUTF )
c4560 3b 0a 20 20 20 20 66 72 65 65 28 20 7a 54 65 6d  ;.    free( zTem
c4570 70 50 61 74 68 55 54 46 20 29 3b 0a 20 20 7d 65  pPathUTF );.  }e
c4580 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
c4590 5f 73 6e 70 72 69 6e 74 66 28 20 6e 42 75 66 2d  _snprintf( nBuf-
c45a0 33 30 2c 20 7a 42 75 66 2c 0a 20 20 20 20 20 20  30, zBuf,.      
c45b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c45c0 22 25 73 5c 5c 22 53 51 4c 49 54 45 5f 54 45 4d  "%s\\"SQLITE_TEM
c45d0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
c45e0 54 65 6d 70 50 61 74 68 20 29 3b 0a 20 20 7d 0a  TempPath );.  }.
c45f0 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28 20 7a 42    j = strlen( zB
c4600 75 66 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  uf );.  sqlite3_
c4610 72 61 6e 64 6f 6d 6e 65 73 73 28 20 32 30 2c 20  randomness( 20, 
c4620 26 7a 42 75 66 5b 6a 5d 20 29 3b 0a 20 20 66 6f  &zBuf[j] );.  fo
c4630 72 28 20 69 20 3d 20 30 3b 20 69 20 3c 20 32 30  r( i = 0; i < 20
c4640 3b 20 69 2b 2b 2c 20 6a 2b 2b 20 29 7b 0a 20 20  ; i++, j++ ){.  
c4650 20 20 7a 42 75 66 5b 6a 5d 20 3d 20 28 63 68 61    zBuf[j] = (cha
c4660 72 29 7a 43 68 61 72 73 5b 20 28 28 75 6e 73 69  r)zChars[ ((unsi
c4670 67 6e 65 64 20 63 68 61 72 29 7a 42 75 66 5b 6a  gned char)zBuf[j
c4680 5d 29 25 28 73 69 7a 65 6f 66 28 7a 43 68 61 72  ])%(sizeof(zChar
c4690 73 29 2d 31 29 20 5d 3b 0a 20 20 7d 0a 20 20 7a  s)-1) ];.  }.  z
c46a0 42 75 66 5b 6a 5d 20 3d 20 30 3b 0a 20 20 4f 53  Buf[j] = 0;.  OS
c46b0 54 52 41 43 45 32 28 20 22 54 45 4d 50 20 46 49  TRACE2( "TEMP FI
c46c0 4c 45 4e 41 4d 45 3a 20 25 73 5c 6e 22 2c 20 7a  LENAME: %s\n", z
c46d0 42 75 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  Buf );.  return 
c46e0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f  SQLITE_OK;.}.../
c46f0 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72 65 6c 61  *.** Turn a rela
c4700 74 69 76 65 20 70 61 74 68 6e 61 6d 65 20 69 6e  tive pathname in
c4710 74 6f 20 61 20 66 75 6c 6c 20 70 61 74 68 6e 61  to a full pathna
c4720 6d 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 66  me.  Write the f
c4730 75 6c 6c 0a 2a 2a 20 70 61 74 68 6e 61 6d 65 20  ull.** pathname 
c4740 69 6e 74 6f 20 7a 46 75 6c 6c 5b 5d 2e 20 20 7a  into zFull[].  z
c4750 46 75 6c 6c 5b 5d 20 77 69 6c 6c 20 62 65 20 61  Full[] will be a
c4760 74 20 6c 65 61 73 74 20 70 56 66 73 2d 3e 6d 78  t least pVfs->mx
c4770 50 61 74 68 6e 61 6d 65 0a 2a 2a 20 62 79 74 65  Pathname.** byte
c4780 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2f 0a 73 74  s in size..*/.st
c4790 61 74 69 63 20 69 6e 74 20 6f 73 32 46 75 6c 6c  atic int os2Full
c47a0 50 61 74 68 6e 61 6d 65 28 0a 20 20 73 71 6c 69  Pathname(.  sqli
c47b0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20  te3_vfs *pVfs,  
c47c0 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
c47d0 65 72 20 74 6f 20 76 66 73 20 6f 62 6a 65 63 74  er to vfs object
c47e0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
c47f0 20 2a 7a 52 65 6c 61 74 69 76 65 2c 20 20 20 20   *zRelative,    
c4800 20 20 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65    /* Possibly re
c4810 6c 61 74 69 76 65 20 69 6e 70 75 74 20 70 61 74  lative input pat
c4820 68 20 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6c 6c  h */.  int nFull
c4830 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
c4840 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75     /* Size of ou
c4850 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62  tput buffer in b
c4860 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ytes */.  char *
c4870 7a 46 75 6c 6c 20 20 20 20 20 20 20 20 20 20 20  zFull           
c4880 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
c4890 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 20 20 63  buffer */.){.  c
c48a0 68 61 72 20 2a 7a 52 65 6c 61 74 69 76 65 43 70  har *zRelativeCp
c48b0 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61   = convertUtf8Pa
c48c0 74 68 54 6f 43 70 28 20 7a 52 65 6c 61 74 69 76  thToCp( zRelativ
c48d0 65 20 29 3b 0a 20 20 63 68 61 72 20 7a 46 75 6c  e );.  char zFul
c48e0 6c 43 70 5b 43 43 48 4d 41 58 50 41 54 48 5d 20  lCp[CCHMAXPATH] 
c48f0 3d 20 22 5c 30 22 3b 0a 20 20 63 68 61 72 20 2a  = "\0";.  char *
c4900 7a 46 75 6c 6c 55 54 46 3b 0a 20 20 41 50 49 52  zFullUTF;.  APIR
c4910 45 54 20 72 63 20 3d 20 44 6f 73 51 75 65 72 79  ET rc = DosQuery
c4920 50 61 74 68 49 6e 66 6f 28 20 7a 52 65 6c 61 74  PathInfo( zRelat
c4930 69 76 65 43 70 2c 20 46 49 4c 5f 51 55 45 52 59  iveCp, FIL_QUERY
c4940 46 55 4c 4c 4e 41 4d 45 2c 20 7a 46 75 6c 6c 43  FULLNAME, zFullC
c4950 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  p,.             
c4960 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c4970 20 20 20 43 43 48 4d 41 58 50 41 54 48 20 29 3b     CCHMAXPATH );
c4980 0a 20 20 66 72 65 65 28 20 7a 52 65 6c 61 74 69  .  free( zRelati
c4990 76 65 43 70 20 29 3b 0a 20 20 7a 46 75 6c 6c 55  veCp );.  zFullU
c49a0 54 46 20 3d 20 63 6f 6e 76 65 72 74 43 70 50 61  TF = convertCpPa
c49b0 74 68 54 6f 55 74 66 38 28 20 7a 46 75 6c 6c 43  thToUtf8( zFullC
c49c0 70 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  p );.  sqlite3_s
c49d0 6e 70 72 69 6e 74 66 28 20 6e 46 75 6c 6c 2c 20  nprintf( nFull, 
c49e0 7a 46 75 6c 6c 2c 20 7a 46 75 6c 6c 55 54 46 20  zFull, zFullUTF 
c49f0 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 75 6c 6c  );.  free( zFull
c4a00 55 54 46 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  UTF );.  return 
c4a10 72 63 20 3d 3d 20 4e 4f 5f 45 52 52 4f 52 20 3f  rc == NO_ERROR ?
c4a20 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53 51 4c   SQLITE_OK : SQL
c4a30 49 54 45 5f 49 4f 45 52 52 3b 0a 7d 0a 0a 0a 2f  ITE_IOERR;.}.../
c4a40 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 66 69 6c 65  *.** Open a file
c4a50 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
c4a60 6f 73 32 4f 70 65 6e 28 0a 20 20 73 71 6c 69 74  os2Open(.  sqlit
c4a70 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
c4a80 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20           /* Not 
c4a90 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  used */.  const 
c4aa0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
c4ab0 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
c4ac0 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 20  of the file */. 
c4ad0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69   sqlite3_file *i
c4ae0 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  d,             /
c4af0 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69  * Write the SQLi
c4b00 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68  te file handle h
c4b10 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ere */.  int fla
c4b20 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
c4b30 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 6d         /* Open m
c4b40 6f 64 65 20 66 6c 61 67 73 20 2a 2f 0a 20 20 69  ode flags */.  i
c4b50 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 20 20 20  nt *pOutFlags   
c4b60 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
c4b70 53 74 61 74 75 73 20 72 65 74 75 72 6e 20 66 6c  Status return fl
c4b80 61 67 73 20 2a 2f 0a 29 7b 0a 20 20 48 46 49 4c  ags */.){.  HFIL
c4b90 45 20 68 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 46  E h;.  ULONG ulF
c4ba0 69 6c 65 41 74 74 72 69 62 75 74 65 20 3d 20 46  ileAttribute = F
c4bb0 49 4c 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 55 4c  ILE_NORMAL;.  UL
c4bc0 4f 4e 47 20 75 6c 4f 70 65 6e 46 6c 61 67 73 20  ONG ulOpenFlags 
c4bd0 3d 20 30 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c 4f  = 0;.  ULONG ulO
c4be0 70 65 6e 4d 6f 64 65 20 3d 20 30 3b 0a 20 20 6f  penMode = 0;.  o
c4bf0 73 32 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  s2File *pFile = 
c4c00 28 6f 73 32 46 69 6c 65 2a 29 69 64 3b 0a 20 20  (os2File*)id;.  
c4c10 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
c4c20 52 52 4f 52 3b 0a 20 20 55 4c 4f 4e 47 20 75 6c  RROR;.  ULONG ul
c4c30 41 63 74 69 6f 6e 3b 0a 20 20 63 68 61 72 20 2a  Action;.  char *
c4c40 7a 4e 61 6d 65 43 70 3b 0a 20 20 63 68 61 72 20  zNameCp;.  char 
c4c50 7a 54 6d 70 6e 61 6d 65 5b 43 43 48 4d 41 58 50  zTmpname[CCHMAXP
c4c60 41 54 48 2b 31 5d 3b 20 20 20 20 2f 2a 20 42 75  ATH+1];    /* Bu
c4c70 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 6e 61 6d  ffer to hold nam
c4c80 65 20 6f 66 20 74 65 6d 70 20 66 69 6c 65 20 2a  e of temp file *
c4c90 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73  /..  /* If the s
c4ca0 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74  econd argument t
c4cb0 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  o this function 
c4cc0 69 73 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74  is NULL, generat
c4cd0 65 20 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72  e a .  ** tempor
c4ce0 61 72 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f  ary file name to
c4cf0 20 75 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28   use .  */.  if(
c4d00 20 21 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 69   !zName ){.    i
c4d10 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d 70 6e  nt rc = getTempn
c4d20 61 6d 65 28 43 43 48 4d 41 58 50 41 54 48 2b 31  ame(CCHMAXPATH+1
c4d30 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20  , zTmpname);.   
c4d40 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
c4d50 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
c4d60 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
c4d70 20 7a 4e 61 6d 65 20 3d 20 7a 54 6d 70 6e 61 6d   zName = zTmpnam
c4d80 65 3b 0a 20 20 7d 0a 0a 0a 20 20 6d 65 6d 73 65  e;.  }...  memse
c4d90 74 28 20 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a  t( pFile, 0, siz
c4da0 65 6f 66 28 2a 70 46 69 6c 65 29 20 29 3b 0a 0a  eof(*pFile) );..
c4db0 20 20 4f 53 54 52 41 43 45 32 28 20 22 4f 50 45    OSTRACE2( "OPE
c4dc0 4e 20 77 61 6e 74 20 25 64 5c 6e 22 2c 20 66 6c  N want %d\n", fl
c4dd0 61 67 73 20 29 3b 0a 0a 20 20 69 66 28 20 66 6c  ags );..  if( fl
c4de0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
c4df0 4e 5f 52 45 41 44 57 52 49 54 45 20 29 7b 0a 20  N_READWRITE ){. 
c4e00 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d     ulOpenMode |=
c4e10 20 4f 50 45 4e 5f 41 43 43 45 53 53 5f 52 45 41   OPEN_ACCESS_REA
c4e20 44 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52  DWRITE;.    OSTR
c4e30 41 43 45 31 28 20 22 4f 50 45 4e 20 72 65 61 64  ACE1( "OPEN read
c4e40 2f 77 72 69 74 65 5c 6e 22 20 29 3b 0a 20 20 7d  /write\n" );.  }
c4e50 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f 70 65 6e  else{.    ulOpen
c4e60 4d 6f 64 65 20 7c 3d 20 4f 50 45 4e 5f 41 43 43  Mode |= OPEN_ACC
c4e70 45 53 53 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ESS_READONLY;.  
c4e80 20 20 4f 53 54 52 41 43 45 31 28 20 22 4f 50 45    OSTRACE1( "OPE
c4e90 4e 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29  N read only\n" )
c4ea0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61  ;.  }..  if( fla
c4eb0 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
c4ec0 5f 43 52 45 41 54 45 20 29 7b 0a 20 20 20 20 75  _CREATE ){.    u
c4ed0 6c 4f 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50  lOpenFlags |= OP
c4ee0 45 4e 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49  EN_ACTION_OPEN_I
c4ef0 46 5f 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f  F_EXISTS | OPEN_
c4f00 41 43 54 49 4f 4e 5f 43 52 45 41 54 45 5f 49 46  ACTION_CREATE_IF
c4f10 5f 4e 45 57 3b 0a 20 20 20 20 4f 53 54 52 41 43  _NEW;.    OSTRAC
c4f20 45 31 28 20 22 4f 50 45 4e 20 6f 70 65 6e 20 6e  E1( "OPEN open n
c4f30 65 77 2f 63 72 65 61 74 65 5c 6e 22 20 29 3b 0a  ew/create\n" );.
c4f40 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 6c 4f    }else{.    ulO
c4f50 70 65 6e 46 6c 61 67 73 20 7c 3d 20 4f 50 45 4e  penFlags |= OPEN
c4f60 5f 41 43 54 49 4f 4e 5f 4f 50 45 4e 5f 49 46 5f  _ACTION_OPEN_IF_
c4f70 45 58 49 53 54 53 20 7c 20 4f 50 45 4e 5f 41 43  EXISTS | OPEN_AC
c4f80 54 49 4f 4e 5f 46 41 49 4c 5f 49 46 5f 4e 45 57  TION_FAIL_IF_NEW
c4f90 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20  ;.    OSTRACE1( 
c4fa0 22 4f 50 45 4e 20 6f 70 65 6e 20 65 78 69 73 74  "OPEN open exist
c4fb0 69 6e 67 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20  ing\n" );.  }.. 
c4fc0 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
c4fd0 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
c4fe0 20 29 7b 0a 20 20 20 20 75 6c 4f 70 65 6e 4d 6f   ){.    ulOpenMo
c4ff0 64 65 20 7c 3d 20 4f 50 45 4e 5f 53 48 41 52 45  de |= OPEN_SHARE
c5000 5f 44 45 4e 59 4e 4f 4e 45 3b 0a 20 20 20 20 4f  _DENYNONE;.    O
c5010 53 54 52 41 43 45 31 28 20 22 4f 50 45 4e 20 73  STRACE1( "OPEN s
c5020 68 61 72 65 20 72 65 61 64 2f 77 72 69 74 65 5c  hare read/write\
c5030 6e 22 20 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  n" );.  }else{. 
c5040 20 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d     ulOpenMode |=
c5050 20 4f 50 45 4e 5f 53 48 41 52 45 5f 44 45 4e 59   OPEN_SHARE_DENY
c5060 57 52 49 54 45 3b 0a 20 20 20 20 4f 53 54 52 41  WRITE;.    OSTRA
c5070 43 45 31 28 20 22 4f 50 45 4e 20 73 68 61 72 65  CE1( "OPEN share
c5080 20 72 65 61 64 20 6f 6e 6c 79 5c 6e 22 20 29 3b   read only\n" );
c5090 0a 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67  .  }..  if( flag
c50a0 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
c50b0 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b  DELETEONCLOSE ){
c50c0 0a 20 20 20 20 63 68 61 72 20 70 61 74 68 55 74  .    char pathUt
c50d0 66 38 5b 43 43 48 4d 41 58 50 41 54 48 5d 3b 0a  f8[CCHMAXPATH];.
c50e0 23 69 66 64 65 66 20 4e 44 45 42 55 47 20 2f 2a  #ifdef NDEBUG /*
c50f0 20 77 68 65 6e 20 64 65 62 75 67 67 69 6e 67 20   when debugging 
c5100 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65 20  we want to make 
c5110 73 75 72 65 20 69 74 20 69 73 20 64 65 6c 65 74  sure it is delet
c5120 65 64 20 2a 2f 0a 20 20 20 20 75 6c 46 69 6c 65  ed */.    ulFile
c5130 41 74 74 72 69 62 75 74 65 20 3d 20 46 49 4c 45  Attribute = FILE
c5140 5f 48 49 44 44 45 4e 3b 0a 23 65 6e 64 69 66 0a  _HIDDEN;.#endif.
c5150 20 20 20 20 6f 73 32 46 75 6c 6c 50 61 74 68 6e      os2FullPathn
c5160 61 6d 65 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65  ame( pVfs, zName
c5170 2c 20 43 43 48 4d 41 58 50 41 54 48 2c 20 70 61  , CCHMAXPATH, pa
c5180 74 68 55 74 66 38 20 29 3b 0a 20 20 20 20 70 46  thUtf8 );.    pF
c5190 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c 20 3d  ile->pathToDel =
c51a0 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74 68   convertUtf8Path
c51b0 54 6f 43 70 28 20 70 61 74 68 55 74 66 38 20 29  ToCp( pathUtf8 )
c51c0 3b 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 20  ;.    OSTRACE1( 
c51d0 22 4f 50 45 4e 20 68 69 64 64 65 6e 2f 64 65 6c  "OPEN hidden/del
c51e0 65 74 65 20 6f 6e 20 63 6c 6f 73 65 20 66 69 6c  ete on close fil
c51f0 65 20 61 74 74 72 69 62 75 74 65 73 5c 6e 22 20  e attributes\n" 
c5200 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
c5210 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44 65 6c  pFile->pathToDel
c5220 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 4f 53 54   = NULL;.    OST
c5230 52 41 43 45 31 28 20 22 4f 50 45 4e 20 6e 6f 72  RACE1( "OPEN nor
c5240 6d 61 6c 20 66 69 6c 65 20 61 74 74 72 69 62 75  mal file attribu
c5250 74 65 5c 6e 22 20 29 3b 0a 20 20 7d 0a 0a 20 20  te\n" );.  }..  
c5260 2f 2a 20 61 6c 77 61 79 73 20 6f 70 65 6e 20 69  /* always open i
c5270 6e 20 72 61 6e 64 6f 6d 20 61 63 63 65 73 73 20  n random access 
c5280 6d 6f 64 65 20 66 6f 72 20 70 6f 73 73 69 62 6c  mode for possibl
c5290 79 20 62 65 74 74 65 72 20 73 70 65 65 64 20 2a  y better speed *
c52a0 2f 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c  /.  ulOpenMode |
c52b0 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f 52 41 4e  = OPEN_FLAGS_RAN
c52c0 44 4f 4d 3b 0a 20 20 75 6c 4f 70 65 6e 4d 6f 64  DOM;.  ulOpenMod
c52d0 65 20 7c 3d 20 4f 50 45 4e 5f 46 4c 41 47 53 5f  e |= OPEN_FLAGS_
c52e0 46 41 49 4c 5f 4f 4e 5f 45 52 52 4f 52 3b 0a 20  FAIL_ON_ERROR;. 
c52f0 20 75 6c 4f 70 65 6e 4d 6f 64 65 20 7c 3d 20 4f   ulOpenMode |= O
c5300 50 45 4e 5f 46 4c 41 47 53 5f 4e 4f 49 4e 48 45  PEN_FLAGS_NOINHE
c5310 52 49 54 3b 0a 0a 20 20 7a 4e 61 6d 65 43 70 20  RIT;..  zNameCp 
c5320 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50 61 74  = convertUtf8Pat
c5330 68 54 6f 43 70 28 20 7a 4e 61 6d 65 20 29 3b 0a  hToCp( zName );.
c5340 20 20 72 63 20 3d 20 44 6f 73 4f 70 65 6e 28 20    rc = DosOpen( 
c5350 28 50 53 5a 29 7a 4e 61 6d 65 43 70 2c 0a 20 20  (PSZ)zNameCp,.  
c5360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 26 68                &h
c5370 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c5380 20 20 26 75 6c 41 63 74 69 6f 6e 2c 0a 20 20 20    &ulAction,.   
c5390 20 20 20 20 20 20 20 20 20 20 20 20 20 30 4c 2c               0L,
c53a0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
c53b0 20 75 6c 46 69 6c 65 41 74 74 72 69 62 75 74 65   ulFileAttribute
c53c0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c53d0 20 20 75 6c 4f 70 65 6e 46 6c 61 67 73 2c 0a 20    ulOpenFlags,. 
c53e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 75                 u
c53f0 6c 4f 70 65 6e 4d 6f 64 65 2c 0a 20 20 20 20 20  lOpenMode,.     
c5400 20 20 20 20 20 20 20 20 20 20 20 28 50 45 41 4f             (PEAO
c5410 50 32 29 4e 55 4c 4c 20 29 3b 0a 20 20 66 72 65  P2)NULL );.  fre
c5420 65 28 20 7a 4e 61 6d 65 43 70 20 29 3b 0a 20 20  e( zNameCp );.  
c5430 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45 52 52  if( rc != NO_ERR
c5440 4f 52 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43  OR ){.    OSTRAC
c5450 45 37 28 20 22 4f 50 45 4e 20 49 6e 76 61 6c 69  E7( "OPEN Invali
c5460 64 20 68 61 6e 64 6c 65 20 72 63 3d 25 64 3a 20  d handle rc=%d: 
c5470 7a 4e 61 6d 65 3d 25 73 2c 20 75 6c 41 63 74 69  zName=%s, ulActi
c5480 6f 6e 3d 25 23 6c 78 2c 20 75 6c 41 74 74 72 3d  on=%#lx, ulAttr=
c5490 25 23 6c 78 2c 20 75 6c 46 6c 61 67 73 3d 25 23  %#lx, ulFlags=%#
c54a0 6c 78 2c 20 75 6c 4d 6f 64 65 3d 25 23 6c 78 5c  lx, ulMode=%#lx\
c54b0 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
c54c0 20 20 72 63 2c 20 7a 4e 61 6d 65 2c 20 75 6c 41    rc, zName, ulA
c54d0 63 74 69 6f 6e 2c 20 75 6c 46 69 6c 65 41 74 74  ction, ulFileAtt
c54e0 72 69 62 75 74 65 2c 20 75 6c 4f 70 65 6e 46 6c  ribute, ulOpenFl
c54f0 61 67 73 2c 20 75 6c 4f 70 65 6e 4d 6f 64 65 20  ags, ulOpenMode 
c5500 29 3b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  );.    if( pFile
c5510 2d 3e 70 61 74 68 54 6f 44 65 6c 20 29 0a 20 20  ->pathToDel ).  
c5520 20 20 20 20 66 72 65 65 28 20 70 46 69 6c 65 2d      free( pFile-
c5530 3e 70 61 74 68 54 6f 44 65 6c 20 29 3b 0a 20 20  >pathToDel );.  
c5540 20 20 70 46 69 6c 65 2d 3e 70 61 74 68 54 6f 44    pFile->pathToD
c5550 65 6c 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69  el = NULL;.    i
c5560 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c 49 54  f( flags & SQLIT
c5570 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
c5580 20 29 7b 0a 20 20 20 20 20 20 4f 53 54 52 41 43   ){.      OSTRAC
c5590 45 32 28 20 22 4f 50 45 4e 20 25 64 20 49 6e 76  E2( "OPEN %d Inv
c55a0 61 6c 69 64 20 68 61 6e 64 6c 65 5c 6e 22 2c 20  alid handle\n", 
c55b0 28 28 66 6c 61 67 73 20 7c 20 53 51 4c 49 54 45  ((flags | SQLITE
c55c0 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 29 20  _OPEN_READONLY) 
c55d0 26 20 7e 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52  & ~SQLITE_OPEN_R
c55e0 45 41 44 57 52 49 54 45 29 20 29 3b 0a 20 20 20  EADWRITE) );.   
c55f0 20 20 20 72 65 74 75 72 6e 20 6f 73 32 4f 70 65     return os2Ope
c5600 6e 28 20 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20  n( pVfs, zName, 
c5610 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  id,.            
c5620 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67            ((flag
c5630 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s | SQLITE_OPEN_
c5640 52 45 41 44 4f 4e 4c 59 29 20 26 20 7e 53 51 4c  READONLY) & ~SQL
c5650 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
c5660 54 45 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  TE),.           
c5670 20 20 20 20 20 20 20 20 20 20 20 70 4f 75 74 46             pOutF
c5680 6c 61 67 73 20 29 3b 0a 20 20 20 20 7d 65 6c 73  lags );.    }els
c5690 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  e{.      return 
c56a0 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b  SQLITE_CANTOPEN;
c56b0 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
c56c0 28 20 70 4f 75 74 46 6c 61 67 73 20 29 7b 0a 20  ( pOutFlags ){. 
c56d0 20 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20     *pOutFlags = 
c56e0 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f  flags & SQLITE_O
c56f0 50 45 4e 5f 52 45 41 44 57 52 49 54 45 20 3f 20  PEN_READWRITE ? 
c5700 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
c5710 57 52 49 54 45 20 3a 20 53 51 4c 49 54 45 5f 4f  WRITE : SQLITE_O
c5720 50 45 4e 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  PEN_READONLY;.  
c5730 7d 0a 0a 20 20 70 46 69 6c 65 2d 3e 70 4d 65 74  }..  pFile->pMet
c5740 68 6f 64 20 3d 20 26 6f 73 32 49 6f 4d 65 74 68  hod = &os2IoMeth
c5750 6f 64 3b 0a 20 20 70 46 69 6c 65 2d 3e 68 20 3d  od;.  pFile->h =
c5760 20 68 3b 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65   h;.  OpenCounte
c5770 72 28 2b 31 29 3b 0a 20 20 4f 53 54 52 41 43 45  r(+1);.  OSTRACE
c5780 33 28 20 22 4f 50 45 4e 20 25 64 20 70 4f 75 74  3( "OPEN %d pOut
c5790 46 6c 61 67 73 3d 25 64 5c 6e 22 2c 20 70 46 69  Flags=%d\n", pFi
c57a0 6c 65 2d 3e 68 2c 20 70 4f 75 74 46 6c 61 67 73  le->h, pOutFlags
c57b0 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c   );.  return SQL
c57c0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
c57d0 20 44 65 6c 65 74 65 20 74 68 65 20 6e 61 6d 65   Delete the name
c57e0 64 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  d file..*/.stati
c57f0 63 20 69 6e 74 20 6f 73 32 44 65 6c 65 74 65 28  c int os2Delete(
c5800 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
c5810 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
c5820 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74            /* Not
c5830 20 75 73 65 64 20 6f 6e 20 6f 73 32 20 2a 2f 0a   used on os2 */.
c5840 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
c5850 69 6c 65 6e 61 6d 65 2c 20 20 20 20 20 20 20 20  ilename,        
c5860 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65           /* Name
c5870 20 6f 66 20 66 69 6c 65 20 74 6f 20 64 65 6c 65   of file to dele
c5880 74 65 20 2a 2f 0a 20 20 69 6e 74 20 73 79 6e 63  te */.  int sync
c5890 44 69 72 20 20 20 20 20 20 20 20 20 20 20 20 20  Dir             
c58a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c58b0 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 6f 73  * Not used on os
c58c0 32 20 2a 2f 0a 29 7b 0a 20 20 41 50 49 52 45 54  2 */.){.  APIRET
c58d0 20 72 63 20 3d 20 4e 4f 5f 45 52 52 4f 52 3b 0a   rc = NO_ERROR;.
c58e0 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d    char *zFilenam
c58f0 65 43 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66  eCp = convertUtf
c5900 38 50 61 74 68 54 6f 43 70 28 20 7a 46 69 6c 65  8PathToCp( zFile
c5910 6e 61 6d 65 20 29 3b 0a 20 20 53 69 6d 75 6c 61  name );.  Simula
c5920 74 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72  teIOError( retur
c5930 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 44  n SQLITE_IOERR_D
c5940 45 4c 45 54 45 20 29 3b 0a 20 20 72 63 20 3d 20  ELETE );.  rc = 
c5950 44 6f 73 44 65 6c 65 74 65 28 20 28 50 53 5a 29  DosDelete( (PSZ)
c5960 7a 46 69 6c 65 6e 61 6d 65 43 70 20 29 3b 0a 20  zFilenameCp );. 
c5970 20 66 72 65 65 28 20 7a 46 69 6c 65 6e 61 6d 65   free( zFilename
c5980 43 70 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  Cp );.  OSTRACE2
c5990 28 20 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22  ( "DELETE \"%s\"
c59a0 5c 6e 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 20 29  \n", zFilename )
c59b0 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 3d 3d  ;.  return rc ==
c59c0 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 53 51 4c 49   NO_ERROR ? SQLI
c59d0 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 49  TE_OK : SQLITE_I
c59e0 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a 0a  OERR_DELETE;.}..
c59f0 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20  /*.** Check the 
c5a00 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73 74  existance and st
c5a10 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e 0a  atus of a file..
c5a20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 73  */.static int os
c5a30 32 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69 74  2Access(.  sqlit
c5a40 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20 20  e3_vfs *pVfs,   
c5a50 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73 65 64       /* Not used
c5a60 20 6f 6e 20 6f 73 32 20 2a 2f 0a 20 20 63 6f 6e   on os2 */.  con
c5a70 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61  st char *zFilena
c5a80 6d 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f  me,    /* Name o
c5a90 66 20 66 69 6c 65 20 74 6f 20 63 68 65 63 6b 20  f file to check 
c5aa0 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20  */.  int flags, 
c5ab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
c5ac0 2a 20 54 79 70 65 20 6f 66 20 74 65 73 74 20 74  * Type of test t
c5ad0 6f 20 6d 61 6b 65 20 6f 6e 20 74 68 69 73 20 66  o make on this f
c5ae0 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4f  ile */.  int *pO
c5af0 75 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ut              
c5b00 20 20 20 2f 2a 20 57 72 69 74 65 20 72 65 73 75     /* Write resu
c5b10 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20  lts here */.){. 
c5b20 20 46 49 4c 45 53 54 41 54 55 53 33 20 66 73 74   FILESTATUS3 fst
c5b30 73 33 43 6f 6e 66 69 67 49 6e 66 6f 3b 0a 20 20  s3ConfigInfo;.  
c5b40 41 50 49 52 45 54 20 72 63 20 3d 20 4e 4f 5f 45  APIRET rc = NO_E
c5b50 52 52 4f 52 3b 0a 20 20 63 68 61 72 20 2a 7a 46  RROR;.  char *zF
c5b60 69 6c 65 6e 61 6d 65 43 70 20 3d 20 63 6f 6e 76  ilenameCp = conv
c5b70 65 72 74 55 74 66 38 50 61 74 68 54 6f 43 70 28  ertUtf8PathToCp(
c5b80 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 0a 20   zFilename );.. 
c5b90 20 6d 65 6d 73 65 74 28 20 26 66 73 74 73 33 43   memset( &fsts3C
c5ba0 6f 6e 66 69 67 49 6e 66 6f 2c 20 30 2c 20 73 69  onfigInfo, 0, si
c5bb0 7a 65 6f 66 28 66 73 74 73 33 43 6f 6e 66 69 67  zeof(fsts3Config
c5bc0 49 6e 66 6f 29 20 29 3b 0a 20 20 72 63 20 3d 20  Info) );.  rc = 
c5bd0 44 6f 73 51 75 65 72 79 50 61 74 68 49 6e 66 6f  DosQueryPathInfo
c5be0 28 20 28 50 53 5a 29 7a 46 69 6c 65 6e 61 6d 65  ( (PSZ)zFilename
c5bf0 43 70 2c 20 46 49 4c 5f 53 54 41 4e 44 41 52 44  Cp, FIL_STANDARD
c5c00 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
c5c10 20 20 20 20 20 20 20 20 20 20 20 26 66 73 74 73             &fsts
c5c20 33 43 6f 6e 66 69 67 49 6e 66 6f 2c 20 73 69 7a  3ConfigInfo, siz
c5c30 65 6f 66 28 46 49 4c 45 53 54 41 54 55 53 33 29  eof(FILESTATUS3)
c5c40 20 29 3b 0a 20 20 66 72 65 65 28 20 7a 46 69 6c   );.  free( zFil
c5c50 65 6e 61 6d 65 43 70 20 29 3b 0a 20 20 4f 53 54  enameCp );.  OST
c5c60 52 41 43 45 34 28 20 22 41 43 43 45 53 53 20 66  RACE4( "ACCESS f
c5c70 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61  sts3ConfigInfo.a
c5c80 74 74 72 46 69 6c 65 3d 25 64 20 66 6c 61 67 73  ttrFile=%d flags
c5c90 3d 25 64 20 72 63 3d 25 64 5c 6e 22 2c 0a 20 20  =%d rc=%d\n",.  
c5ca0 20 20 20 20 20 20 20 20 20 20 66 73 74 73 33 43            fsts3C
c5cb0 6f 6e 66 69 67 49 6e 66 6f 2e 61 74 74 72 46 69  onfigInfo.attrFi
c5cc0 6c 65 2c 20 66 6c 61 67 73 2c 20 72 63 20 29 3b  le, flags, rc );
c5cd0 0a 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73  .  switch( flags
c5ce0 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c   ){.    case SQL
c5cf0 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a  ITE_ACCESS_READ:
c5d00 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
c5d10 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a  _ACCESS_EXISTS:.
c5d20 20 20 20 20 20 20 72 63 20 3d 20 28 72 63 20 3d        rc = (rc =
c5d30 3d 20 4e 4f 5f 45 52 52 4f 52 29 3b 0a 20 20 20  = NO_ERROR);.   
c5d40 20 20 20 4f 53 54 52 41 43 45 33 28 20 22 41 43     OSTRACE3( "AC
c5d50 43 45 53 53 20 25 73 20 61 63 63 65 73 73 20 6f  CESS %s access o
c5d60 66 20 72 65 61 64 20 61 6e 64 20 65 78 69 73 74  f read and exist
c5d70 73 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69  s  rc=%d\n", zFi
c5d80 6c 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20  lename, rc );.  
c5d90 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63      break;.    c
c5da0 61 73 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ase SQLITE_ACCES
c5db0 53 5f 52 45 41 44 57 52 49 54 45 3a 0a 20 20 20  S_READWRITE:.   
c5dc0 20 20 20 72 63 20 3d 20 28 72 63 20 3d 3d 20 4e     rc = (rc == N
c5dd0 4f 5f 45 52 52 4f 52 29 20 26 26 20 28 20 28 66  O_ERROR) && ( (f
c5de0 73 74 73 33 43 6f 6e 66 69 67 49 6e 66 6f 2e 61  sts3ConfigInfo.a
c5df0 74 74 72 46 69 6c 65 20 26 20 46 49 4c 45 5f 52  ttrFile & FILE_R
c5e00 45 41 44 4f 4e 4c 59 29 20 3d 3d 20 30 20 29 3b  EADONLY) == 0 );
c5e10 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 33 28  .      OSTRACE3(
c5e20 20 22 41 43 43 45 53 53 20 25 73 20 61 63 63 65   "ACCESS %s acce
c5e30 73 73 20 6f 66 20 72 65 61 64 2f 77 72 69 74 65  ss of read/write
c5e40 20 20 72 63 3d 25 64 5c 6e 22 2c 20 7a 46 69 6c    rc=%d\n", zFil
c5e50 65 6e 61 6d 65 2c 20 72 63 20 29 3b 0a 20 20 20  ename, rc );.   
c5e60 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
c5e70 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73  fault:.      ass
c5e80 65 72 74 28 20 21 22 49 6e 76 61 6c 69 64 20 66  ert( !"Invalid f
c5e90 6c 61 67 73 20 61 72 67 75 6d 65 6e 74 22 20 29  lags argument" )
c5ea0 3b 0a 20 20 7d 0a 20 20 2a 70 4f 75 74 20 3d 20  ;.  }.  *pOut = 
c5eb0 72 63 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  rc;.  return SQL
c5ec0 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 6e  ITE_OK;.}...#ifn
c5ed0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
c5ee0 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 2f  LOAD_EXTENSION./
c5ef0 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65 73 20  *.** Interfaces 
c5f00 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20 73 68  for opening a sh
c5f10 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20 66 69  ared library, fi
c5f20 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f 69 6e  nding entry poin
c5f30 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ts.** within the
c5f40 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c   shared library,
c5f50 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74 68 65   and closing the
c5f60 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2e   shared library.
c5f70 0a 2a 2f 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66  .*/./*.** Interf
c5f80 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67  aces for opening
c5f90 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61 72   a shared librar
c5fa0 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72 79  y, finding entry
c5fb0 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69   points.** withi
c5fc0 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  n the shared lib
c5fd0 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e  rary, and closin
c5fe0 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62  g the shared lib
c5ff0 72 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rary..*/.static 
c6000 76 6f 69 64 20 2a 6f 73 32 44 6c 4f 70 65 6e 28  void *os2DlOpen(
c6010 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
c6020 73 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  s, const char *z
c6030 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 55 43 48  Filename){.  UCH
c6040 41 52 20 6c 6f 61 64 45 72 72 5b 32 35 36 5d 3b  AR loadErr[256];
c6050 0a 20 20 48 4d 4f 44 55 4c 45 20 68 6d 6f 64 3b  .  HMODULE hmod;
c6060 0a 20 20 41 50 49 52 45 54 20 72 63 3b 0a 20 20  .  APIRET rc;.  
c6070 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 43  char *zFilenameC
c6080 70 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 50  p = convertUtf8P
c6090 61 74 68 54 6f 43 70 28 7a 46 69 6c 65 6e 61 6d  athToCp(zFilenam
c60a0 65 29 3b 0a 20 20 72 63 20 3d 20 44 6f 73 4c 6f  e);.  rc = DosLo
c60b0 61 64 4d 6f 64 75 6c 65 28 28 50 53 5a 29 6c 6f  adModule((PSZ)lo
c60c0 61 64 45 72 72 2c 20 73 69 7a 65 6f 66 28 6c 6f  adErr, sizeof(lo
c60d0 61 64 45 72 72 29 2c 20 7a 46 69 6c 65 6e 61 6d  adErr), zFilenam
c60e0 65 43 70 2c 20 26 68 6d 6f 64 29 3b 0a 20 20 66  eCp, &hmod);.  f
c60f0 72 65 65 28 7a 46 69 6c 65 6e 61 6d 65 43 70 29  ree(zFilenameCp)
c6100 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d  ;.  return rc !=
c6110 20 4e 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20   NO_ERROR ? 0 : 
c6120 28 76 6f 69 64 2a 29 68 6d 6f 64 3b 0a 7d 0a 2f  (void*)hmod;.}./
c6130 2a 0a 2a 2a 20 41 20 6e 6f 2d 6f 70 20 73 69 6e  *.** A no-op sin
c6140 63 65 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ce the error cod
c6150 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  e is returned on
c6160 20 74 68 65 20 44 6f 73 4c 6f 61 64 4d 6f 64 75   the DosLoadModu
c6170 6c 65 20 63 61 6c 6c 2e 0a 2a 2a 20 6f 73 32 44  le call..** os2D
c6180 6c 6f 70 65 6e 20 72 65 74 75 72 6e 73 20 7a 65  lopen returns ze
c6190 72 6f 20 69 66 20 44 6f 73 4c 6f 61 64 4d 6f 64  ro if DosLoadMod
c61a0 75 6c 65 20 69 73 20 6e 6f 74 20 73 75 63 63 65  ule is not succe
c61b0 73 73 66 75 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssful..*/.static
c61c0 20 76 6f 69 64 20 6f 73 32 44 6c 45 72 72 6f 72   void os2DlError
c61d0 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56  (sqlite3_vfs *pV
c61e0 66 73 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68  fs, int nBuf, ch
c61f0 61 72 20 2a 7a 42 75 66 4f 75 74 29 7b 0a 2f 2a  ar *zBufOut){./*
c6200 20 6e 6f 2d 6f 70 20 2a 2f 0a 7d 0a 73 74 61 74   no-op */.}.stat
c6210 69 63 20 76 6f 69 64 20 2a 6f 73 32 44 6c 53 79  ic void *os2DlSy
c6220 6d 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70  m(sqlite3_vfs *p
c6230 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64  Vfs, void *pHand
c6240 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  le, const char *
c6250 7a 53 79 6d 62 6f 6c 29 7b 0a 20 20 50 46 4e 20  zSymbol){.  PFN 
c6260 70 66 6e 3b 0a 20 20 41 50 49 52 45 54 20 72 63  pfn;.  APIRET rc
c6270 3b 0a 20 20 72 63 20 3d 20 44 6f 73 51 75 65 72  ;.  rc = DosQuer
c6280 79 50 72 6f 63 41 64 64 72 28 28 48 4d 4f 44 55  yProcAddr((HMODU
c6290 4c 45 29 70 48 61 6e 64 6c 65 2c 20 30 4c 2c 20  LE)pHandle, 0L, 
c62a0 7a 53 79 6d 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a  zSymbol, &pfn);.
c62b0 20 20 69 66 28 20 72 63 20 21 3d 20 4e 4f 5f 45    if( rc != NO_E
c62c0 52 52 4f 52 20 29 7b 0a 20 20 20 20 2f 2a 20 69  RROR ){.    /* i
c62d0 66 20 74 68 65 20 73 79 6d 62 6f 6c 20 69 74 73  f the symbol its
c62e0 65 6c 66 20 77 61 73 20 6e 6f 74 20 66 6f 75 6e  elf was not foun
c62f0 64 2c 20 73 65 61 72 63 68 20 61 67 61 69 6e 20  d, search again 
c6300 66 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20  for the same.   
c6310 20 20 2a 20 73 79 6d 62 6f 6c 20 77 69 74 68 20    * symbol with 
c6320 61 6e 20 65 78 74 72 61 20 75 6e 64 65 72 73 63  an extra undersc
c6330 6f 72 65 2c 20 74 68 61 74 20 6d 69 67 68 74 20  ore, that might 
c6340 62 65 20 6e 65 65 64 65 64 20 64 65 70 65 6e 64  be needed depend
c6350 69 6e 67 0a 20 20 20 20 20 2a 20 6f 6e 20 74 68  ing.     * on th
c6360 65 20 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e  e calling conven
c6370 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 63 68 61 72  tion */.    char
c6380 20 5f 7a 53 79 6d 62 6f 6c 5b 32 35 36 5d 20 3d   _zSymbol[256] =
c6390 20 22 5f 22 3b 0a 20 20 20 20 73 74 72 6e 63 61   "_";.    strnca
c63a0 74 28 5f 7a 53 79 6d 62 6f 6c 2c 20 7a 53 79 6d  t(_zSymbol, zSym
c63b0 62 6f 6c 2c 20 32 35 35 29 3b 0a 20 20 20 20 72  bol, 255);.    r
c63c0 63 20 3d 20 44 6f 73 51 75 65 72 79 50 72 6f 63  c = DosQueryProc
c63d0 41 64 64 72 28 28 48 4d 4f 44 55 4c 45 29 70 48  Addr((HMODULE)pH
c63e0 61 6e 64 6c 65 2c 20 30 4c 2c 20 5f 7a 53 79 6d  andle, 0L, _zSym
c63f0 62 6f 6c 2c 20 26 70 66 6e 29 3b 0a 20 20 7d 0a  bol, &pfn);.  }.
c6400 20 20 72 65 74 75 72 6e 20 72 63 20 21 3d 20 4e    return rc != N
c6410 4f 5f 45 52 52 4f 52 20 3f 20 30 20 3a 20 28 76  O_ERROR ? 0 : (v
c6420 6f 69 64 2a 29 70 66 6e 3b 0a 7d 0a 73 74 61 74  oid*)pfn;.}.stat
c6430 69 63 20 76 6f 69 64 20 6f 73 32 44 6c 43 6c 6f  ic void os2DlClo
c6440 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  se(sqlite3_vfs *
c6450 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61 6e  pVfs, void *pHan
c6460 64 6c 65 29 7b 0a 20 20 44 6f 73 46 72 65 65 4d  dle){.  DosFreeM
c6470 6f 64 75 6c 65 28 28 48 4d 4f 44 55 4c 45 29 70  odule((HMODULE)p
c6480 48 61 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65  Handle);.}.#else
c6490 20 2f 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d   /* if SQLITE_OM
c64a0 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f  IT_LOAD_EXTENSIO
c64b0 4e 20 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f  N is defined: */
c64c0 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32 44 6c  .  #define os2Dl
c64d0 4f 70 65 6e 20 30 0a 20 20 23 64 65 66 69 6e 65  Open 0.  #define
c64e0 20 6f 73 32 44 6c 45 72 72 6f 72 20 30 0a 20 20   os2DlError 0.  
c64f0 23 64 65 66 69 6e 65 20 6f 73 32 44 6c 53 79 6d  #define os2DlSym
c6500 20 30 0a 20 20 23 64 65 66 69 6e 65 20 6f 73 32   0.  #define os2
c6510 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69 66  DlClose 0.#endif
c6520 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 75  .../*.** Write u
c6530 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73 20  p to nBuf bytes 
c6540 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69 6e  of randomness in
c6550 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61 74  to zBuf..*/.stat
c6560 69 63 20 69 6e 74 20 6f 73 32 52 61 6e 64 6f 6d  ic int os2Random
c6570 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66 73  ness(sqlite3_vfs
c6580 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
c6590 2c 20 63 68 61 72 20 2a 7a 42 75 66 20 29 7b 0a  , char *zBuf ){.
c65a0 20 20 55 4c 4f 4e 47 20 73 69 7a 65 6f 66 55 4c    ULONG sizeofUL
c65b0 6f 6e 67 20 3d 20 73 69 7a 65 6f 66 28 55 4c 4f  ong = sizeof(ULO
c65c0 4e 47 29 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30  NG);.  int n = 0
c65d0 3b 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 28 44  ;.  if( sizeof(D
c65e0 41 54 45 54 49 4d 45 29 20 3c 3d 20 6e 42 75 66  ATETIME) <= nBuf
c65f0 20 2d 20 6e 20 29 7b 0a 20 20 20 20 44 41 54 45   - n ){.    DATE
c6600 54 49 4d 45 20 78 3b 0a 20 20 20 20 44 6f 73 47  TIME x;.    DosG
c6610 65 74 44 61 74 65 54 69 6d 65 28 26 78 29 3b 0a  etDateTime(&x);.
c6620 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66      memcpy(&zBuf
c6630 5b 6e 5d 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28  [n], &x, sizeof(
c6640 78 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  x));.    n += si
c6650 7a 65 6f 66 28 78 29 3b 0a 20 20 7d 0a 0a 20 20  zeof(x);.  }..  
c6660 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20  if( sizeofULong 
c6670 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20  <= nBuf - n ){. 
c6680 20 20 20 50 50 49 42 20 70 70 69 62 3b 0a 20 20     PPIB ppib;.  
c6690 20 20 44 6f 73 47 65 74 49 6e 66 6f 42 6c 6f 63    DosGetInfoBloc
c66a0 6b 73 28 4e 55 4c 4c 2c 20 26 70 70 69 62 29 3b  ks(NULL, &ppib);
c66b0 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75  .    memcpy(&zBu
c66c0 66 5b 6e 5d 2c 20 26 70 70 69 62 2d 3e 70 69 62  f[n], &ppib->pib
c66d0 5f 75 6c 70 69 64 2c 20 73 69 7a 65 6f 66 55 4c  _ulpid, sizeofUL
c66e0 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  ong);.    n += s
c66f0 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a  izeofULong;.  }.
c6700 0a 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f  .  if( sizeofULo
c6710 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29  ng <= nBuf - n )
c6720 7b 0a 20 20 20 20 50 54 49 42 20 70 74 69 62 3b  {.    PTIB ptib;
c6730 0a 20 20 20 20 44 6f 73 47 65 74 49 6e 66 6f 42  .    DosGetInfoB
c6740 6c 6f 63 6b 73 28 26 70 74 69 62 2c 20 4e 55 4c  locks(&ptib, NUL
c6750 4c 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26  L);.    memcpy(&
c6760 7a 42 75 66 5b 6e 5d 2c 20 26 70 74 69 62 2d 3e  zBuf[n], &ptib->
c6770 74 69 62 5f 70 74 69 62 32 2d 3e 74 69 62 32 5f  tib_ptib2->tib2_
c6780 75 6c 74 69 64 2c 20 73 69 7a 65 6f 66 55 4c 6f  ultid, sizeofULo
c6790 6e 67 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69  ng);.    n += si
c67a0 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 7d 0a 0a  zeofULong;.  }..
c67b0 20 20 2f 2a 20 69 66 20 77 65 20 73 74 69 6c 6c    /* if we still
c67c0 20 68 61 76 65 6e 27 74 20 66 69 6c 6c 65 64 20   haven't filled 
c67d0 74 68 65 20 62 75 66 66 65 72 20 79 65 74 20 74  the buffer yet t
c67e0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 77 69 6c  he following wil
c67f0 6c 20 2a 2f 0a 20 20 2f 2a 20 67 72 61 62 20 65  l */.  /* grab e
c6800 76 65 72 79 74 68 69 6e 67 20 6f 6e 63 65 20 69  verything once i
c6810 6e 73 74 65 61 64 20 6f 66 20 6d 61 6b 69 6e 67  nstead of making
c6820 20 73 65 76 65 72 61 6c 20 63 61 6c 6c 73 20 66   several calls f
c6830 6f 72 20 61 20 73 69 6e 67 6c 65 20 69 74 65 6d  or a single item
c6840 20 2a 2f 0a 20 20 69 66 28 20 73 69 7a 65 6f 66   */.  if( sizeof
c6850 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20  ULong <= nBuf - 
c6860 6e 20 29 7b 0a 20 20 20 20 55 4c 4f 4e 47 20 75  n ){.    ULONG u
c6870 6c 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 41 58  lSysInfo[QSV_MAX
c6880 5d 3b 0a 20 20 20 20 44 6f 73 51 75 65 72 79 53  ];.    DosQueryS
c6890 79 73 49 6e 66 6f 28 31 4c 2c 20 51 53 56 5f 4d  ysInfo(1L, QSV_M
c68a0 41 58 2c 20 75 6c 53 79 73 49 6e 66 6f 2c 20 73  AX, ulSysInfo, s
c68b0 69 7a 65 6f 66 55 4c 6f 6e 67 20 2a 20 51 53 56  izeofULong * QSV
c68c0 5f 4d 41 58 29 3b 0a 0a 20 20 20 20 6d 65 6d 63  _MAX);..    memc
c68d0 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c  py(&zBuf[n], &ul
c68e0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 4d 53 5f 43  SysInfo[QSV_MS_C
c68f0 4f 55 4e 54 20 2d 20 31 5d 2c 20 73 69 7a 65 6f  OUNT - 1], sizeo
c6900 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20 6e 20 2b  fULong);.    n +
c6910 3d 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 0a  = sizeofULong;..
c6920 20 20 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c      if( sizeofUL
c6930 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20  ong <= nBuf - n 
c6940 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28  ){.      memcpy(
c6950 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73  &zBuf[n], &ulSys
c6960 49 6e 66 6f 5b 51 53 56 5f 54 49 4d 45 52 5f 49  Info[QSV_TIMER_I
c6970 4e 54 45 52 56 41 4c 20 2d 20 31 5d 2c 20 73 69  NTERVAL - 1], si
c6980 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20  zeofULong);.    
c6990 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f    n += sizeofULo
c69a0 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ng;.    }.    if
c69b0 28 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 20 3c 3d  ( sizeofULong <=
c69c0 20 6e 42 75 66 20 2d 20 6e 20 29 7b 0a 20 20 20   nBuf - n ){.   
c69d0 20 20 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b     memcpy(&zBuf[
c69e0 6e 5d 2c 20 26 75 6c 53 79 73 49 6e 66 6f 5b 51  n], &ulSysInfo[Q
c69f0 53 56 5f 54 49 4d 45 5f 4c 4f 57 20 2d 20 31 5d  SV_TIME_LOW - 1]
c6a00 2c 20 73 69 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a  , sizeofULong);.
c6a10 20 20 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f        n += sizeo
c6a20 66 55 4c 6f 6e 67 3b 0a 20 20 20 20 7d 0a 20 20  fULong;.    }.  
c6a30 20 20 69 66 28 20 73 69 7a 65 6f 66 55 4c 6f 6e    if( sizeofULon
c6a40 67 20 3c 3d 20 6e 42 75 66 20 2d 20 6e 20 29 7b  g <= nBuf - n ){
c6a50 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 7a  .      memcpy(&z
c6a60 42 75 66 5b 6e 5d 2c 20 26 75 6c 53 79 73 49 6e  Buf[n], &ulSysIn
c6a70 66 6f 5b 51 53 56 5f 54 49 4d 45 5f 48 49 47 48  fo[QSV_TIME_HIGH
c6a80 20 2d 20 31 5d 2c 20 73 69 7a 65 6f 66 55 4c 6f   - 1], sizeofULo
c6a90 6e 67 29 3b 0a 20 20 20 20 20 20 6e 20 2b 3d 20  ng);.      n += 
c6aa0 73 69 7a 65 6f 66 55 4c 6f 6e 67 3b 0a 20 20 20  sizeofULong;.   
c6ab0 20 7d 0a 20 20 20 20 69 66 28 20 73 69 7a 65 6f   }.    if( sizeo
c6ac0 66 55 4c 6f 6e 67 20 3c 3d 20 6e 42 75 66 20 2d  fULong <= nBuf -
c6ad0 20 6e 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63   n ){.      memc
c6ae0 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 75 6c  py(&zBuf[n], &ul
c6af0 53 79 73 49 6e 66 6f 5b 51 53 56 5f 54 4f 54 41  SysInfo[QSV_TOTA
c6b00 56 41 49 4c 4d 45 4d 20 2d 20 31 5d 2c 20 73 69  VAILMEM - 1], si
c6b10 7a 65 6f 66 55 4c 6f 6e 67 29 3b 0a 20 20 20 20  zeofULong);.    
c6b20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 55 4c 6f    n += sizeofULo
c6b30 6e 67 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  ng;.    }.  }.. 
c6b40 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a   return n;.}../*
c6b50 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72 20 61 20  .** Sleep for a 
c6b60 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e 20 20 52  little while.  R
c6b70 65 74 75 72 6e 20 74 68 65 20 61 6d 6f 75 6e 74  eturn the amount
c6b80 20 6f 66 20 74 69 6d 65 20 73 6c 65 70 74 2e 0a   of time slept..
c6b90 2a 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  ** The argument 
c6ba0 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
c6bb0 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 77 65   microseconds we
c6bc0 20 77 61 6e 74 20 74 6f 20 73 6c 65 65 70 2e 0a   want to sleep..
c6bd0 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 20 76 61  ** The return va
c6be0 6c 75 65 20 69 73 20 74 68 65 20 6e 75 6d 62 65  lue is the numbe
c6bf0 72 20 6f 66 20 6d 69 63 72 6f 73 65 63 6f 6e 64  r of microsecond
c6c00 73 20 6f 66 20 73 6c 65 65 70 20 61 63 74 75 61  s of sleep actua
c6c10 6c 6c 79 0a 2a 2a 20 72 65 71 75 65 73 74 65 64  lly.** requested
c6c20 20 66 72 6f 6d 20 74 68 65 20 75 6e 64 65 72 6c   from the underl
c6c30 79 69 6e 67 20 6f 70 65 72 61 74 69 6e 67 20 73  ying operating s
c6c40 79 73 74 65 6d 2c 20 61 20 6e 75 6d 62 65 72 20  ystem, a number 
c6c50 77 68 69 63 68 0a 2a 2a 20 6d 69 67 68 74 20 62  which.** might b
c6c60 65 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  e greater than o
c6c70 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 61  r equal to the a
c6c80 72 67 75 6d 65 6e 74 2c 20 62 75 74 20 6e 6f 74  rgument, but not
c6c90 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68   less.** than th
c6ca0 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73  e argument..*/.s
c6cb0 74 61 74 69 63 20 69 6e 74 20 6f 73 32 53 6c 65  tatic int os2Sle
c6cc0 65 70 28 20 73 71 6c 69 74 65 33 5f 76 66 73 20  ep( sqlite3_vfs 
c6cd0 2a 70 56 66 73 2c 20 69 6e 74 20 6d 69 63 72 6f  *pVfs, int micro
c6ce0 73 65 63 20 29 7b 0a 20 20 44 6f 73 53 6c 65 65  sec ){.  DosSlee
c6cf0 70 28 20 28 6d 69 63 72 6f 73 65 63 2f 31 30 30  p( (microsec/100
c6d00 30 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 6d  0) );.  return m
c6d10 69 63 72 6f 73 65 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  icrosec;.}../*.*
c6d20 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
c6d30 76 61 72 69 61 62 6c 65 2c 20 69 66 20 73 65 74  variable, if set
c6d40 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
c6d50 61 6c 75 65 2c 20 62 65 63 6f 6d 65 73 20 74 68  alue, becomes th
c6d60 65 20 72 65 73 75 6c 74 0a 2a 2a 20 72 65 74 75  e result.** retu
c6d70 72 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  rned from sqlite
c6d80 33 4f 73 43 75 72 72 65 6e 74 54 69 6d 65 28 29  3OsCurrentTime()
c6d90 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20  .  This is used 
c6da0 66 6f 72 20 74 65 73 74 69 6e 67 2e 0a 2a 2f 0a  for testing..*/.
c6db0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
c6dc0 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
c6dd0 74 20 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e  t sqlite3_curren
c6de0 74 5f 74 69 6d 65 20 3d 20 30 3b 0a 23 65 6e 64  t_time = 0;.#end
c6df0 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74  if../*.** Find t
c6e00 68 65 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  he current time 
c6e10 28 69 6e 20 55 6e 69 76 65 72 73 61 6c 20 43 6f  (in Universal Co
c6e20 6f 72 64 69 6e 61 74 65 64 20 54 69 6d 65 29 2e  ordinated Time).
c6e30 20 20 57 72 69 74 65 20 74 68 65 0a 2a 2a 20 63    Write the.** c
c6e40 75 72 72 65 6e 74 20 74 69 6d 65 20 61 6e 64 20  urrent time and 
c6e50 64 61 74 65 20 61 73 20 61 20 4a 75 6c 69 61 6e  date as a Julian
c6e60 20 44 61 79 20 6e 75 6d 62 65 72 20 69 6e 74 6f   Day number into
c6e70 20 2a 70 72 4e 6f 77 20 61 6e 64 0a 2a 2a 20 72   *prNow and.** r
c6e80 65 74 75 72 6e 20 30 2e 20 20 52 65 74 75 72 6e  eturn 0.  Return
c6e90 20 31 20 69 66 20 74 68 65 20 74 69 6d 65 20 61   1 if the time a
c6ea0 6e 64 20 64 61 74 65 20 63 61 6e 6e 6f 74 20 62  nd date cannot b
c6eb0 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a 69 6e 74 20  e found..*/.int 
c6ec0 6f 73 32 43 75 72 72 65 6e 74 54 69 6d 65 28 20  os2CurrentTime( 
c6ed0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66  sqlite3_vfs *pVf
c6ee0 73 2c 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77  s, double *prNow
c6ef0 20 29 7b 0a 20 20 64 6f 75 62 6c 65 20 6e 6f 77   ){.  double now
c6f00 3b 0a 20 20 53 48 4f 52 54 20 6d 69 6e 75 74 65  ;.  SHORT minute
c6f10 3b 20 2f 2a 20 6e 65 65 64 73 20 74 6f 20 62 65  ; /* needs to be
c6f20 20 61 62 6c 65 20 74 6f 20 63 6f 70 65 20 77 69   able to cope wi
c6f30 74 68 20 6e 65 67 61 74 69 76 65 20 74 69 6d 65  th negative time
c6f40 7a 6f 6e 65 20 6f 66 66 73 65 74 20 2a 2f 0a 20  zone offset */. 
c6f50 20 55 53 48 4f 52 54 20 73 65 63 6f 6e 64 2c 20   USHORT second, 
c6f60 68 6f 75 72 2c 0a 20 20 20 20 20 20 20 20 20 64  hour,.         d
c6f70 61 79 2c 20 6d 6f 6e 74 68 2c 20 79 65 61 72 3b  ay, month, year;
c6f80 0a 20 20 44 41 54 45 54 49 4d 45 20 64 74 3b 0a  .  DATETIME dt;.
c6f90 20 20 44 6f 73 47 65 74 44 61 74 65 54 69 6d 65    DosGetDateTime
c6fa0 28 20 26 64 74 20 29 3b 0a 20 20 73 65 63 6f 6e  ( &dt );.  secon
c6fb0 64 20 3d 20 28 55 53 48 4f 52 54 29 64 74 2e 73  d = (USHORT)dt.s
c6fc0 65 63 6f 6e 64 73 3b 0a 20 20 6d 69 6e 75 74 65  econds;.  minute
c6fd0 20 3d 20 28 53 48 4f 52 54 29 64 74 2e 6d 69 6e   = (SHORT)dt.min
c6fe0 75 74 65 73 20 2b 20 64 74 2e 74 69 6d 65 7a 6f  utes + dt.timezo
c6ff0 6e 65 3b 0a 20 20 68 6f 75 72 20 3d 20 28 55 53  ne;.  hour = (US
c7000 48 4f 52 54 29 64 74 2e 68 6f 75 72 73 3b 0a 20  HORT)dt.hours;. 
c7010 20 64 61 79 20 3d 20 28 55 53 48 4f 52 54 29 64   day = (USHORT)d
c7020 74 2e 64 61 79 3b 0a 20 20 6d 6f 6e 74 68 20 3d  t.day;.  month =
c7030 20 28 55 53 48 4f 52 54 29 64 74 2e 6d 6f 6e 74   (USHORT)dt.mont
c7040 68 3b 0a 20 20 79 65 61 72 20 3d 20 28 55 53 48  h;.  year = (USH
c7050 4f 52 54 29 64 74 2e 79 65 61 72 3b 0a 0a 20 20  ORT)dt.year;..  
c7060 2f 2a 20 43 61 6c 63 75 6c 61 74 69 6f 6e 73 20  /* Calculations 
c7070 66 72 6f 6d 20 68 74 74 70 3a 2f 2f 77 77 77 2e  from http://www.
c7080 61 73 74 72 6f 2e 6b 65 65 6c 65 2e 61 63 2e 75  astro.keele.ac.u
c7090 6b 2f 7e 72 6e 6f 2f 41 73 74 72 6f 6e 6f 6d 79  k/~rno/Astronomy
c70a0 2f 68 6a 64 2e 68 74 6d 6c 0a 20 20 20 20 20 68  /hjd.html.     h
c70b0 74 74 70 3a 2f 2f 77 77 77 2e 61 73 74 72 6f 2e  ttp://www.astro.
c70c0 6b 65 65 6c 65 2e 61 63 2e 75 6b 2f 7e 72 6e 6f  keele.ac.uk/~rno
c70d0 2f 41 73 74 72 6f 6e 6f 6d 79 2f 68 6a 64 2d 30  /Astronomy/hjd-0
c70e0 2e 31 2e 63 20 2a 2f 0a 20 20 2f 2a 20 43 61 6c  .1.c */.  /* Cal
c70f0 63 75 6c 61 74 65 20 74 68 65 20 4a 75 6c 69 61  culate the Julia
c7100 6e 20 64 61 79 73 20 2a 2f 0a 20 20 6e 6f 77 20  n days */.  now 
c7110 3d 20 64 61 79 20 2d 20 33 32 30 37 36 20 2b 0a  = day - 32076 +.
c7120 20 20 20 20 31 34 36 31 2a 28 79 65 61 72 20 2b      1461*(year +
c7130 20 34 38 30 30 20 2b 20 28 6d 6f 6e 74 68 20 2d   4800 + (month -
c7140 20 31 34 29 2f 31 32 29 2f 34 20 2b 0a 20 20 20   14)/12)/4 +.   
c7150 20 33 36 37 2a 28 6d 6f 6e 74 68 20 2d 20 32 20   367*(month - 2 
c7160 2d 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31  - (month - 14)/1
c7170 32 2a 31 32 29 2f 31 32 20 2d 0a 20 20 20 20 33  2*12)/12 -.    3
c7180 2a 28 28 79 65 61 72 20 2b 20 34 39 30 30 20 2b  *((year + 4900 +
c7190 20 28 6d 6f 6e 74 68 20 2d 20 31 34 29 2f 31 32   (month - 14)/12
c71a0 29 2f 31 30 30 29 2f 34 3b 0a 0a 20 20 2f 2a 20  )/100)/4;..  /* 
c71b0 41 64 64 20 74 68 65 20 66 72 61 63 74 69 6f 6e  Add the fraction
c71c0 61 6c 20 68 6f 75 72 73 2c 20 6d 69 6e 73 20 61  al hours, mins a
c71d0 6e 64 20 73 65 63 6f 6e 64 73 20 2a 2f 0a 20 20  nd seconds */.  
c71e0 6e 6f 77 20 2b 3d 20 28 68 6f 75 72 20 2b 20 31  now += (hour + 1
c71f0 32 2e 30 29 2f 32 34 2e 30 3b 0a 20 20 6e 6f 77  2.0)/24.0;.  now
c7200 20 2b 3d 20 6d 69 6e 75 74 65 2f 31 34 34 30 2e   += minute/1440.
c7210 30 3b 0a 20 20 6e 6f 77 20 2b 3d 20 73 65 63 6f  0;.  now += seco
c7220 6e 64 2f 38 36 34 30 30 2e 30 3b 0a 20 20 2a 70  nd/86400.0;.  *p
c7230 72 4e 6f 77 20 3d 20 6e 6f 77 3b 0a 23 69 66 64  rNow = now;.#ifd
c7240 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20  ef SQLITE_TEST. 
c7250 20 69 66 28 20 73 71 6c 69 74 65 33 5f 63 75 72   if( sqlite3_cur
c7260 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a 20 20 20  rent_time ){.   
c7270 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c 69 74 65   *prNow = sqlite
c7280 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d 65 2f 38  3_current_time/8
c7290 36 34 30 30 2e 30 20 2b 20 32 34 34 30 35 38 37  6400.0 + 2440587
c72a0 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  .5;.  }.#endif. 
c72b0 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 73 74   return 0;.}..st
c72c0 61 74 69 63 20 69 6e 74 20 6f 73 32 47 65 74 4c  atic int os2GetL
c72d0 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65 33  astError(sqlite3
c72e0 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74 20  _vfs *pVfs, int 
c72f0 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66  nBuf, char *zBuf
c7300 29 7b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  ){.  return 0;.}
c7310 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
c7320 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c  ze and deinitial
c7330 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e  ize the operatin
c7340 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61  g system interfa
c7350 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
c7360 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
c7370 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73  _init(void){.  s
c7380 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
c7390 73 20 6f 73 32 56 66 73 20 3d 20 7b 0a 20 20 20  s os2Vfs = {.   
c73a0 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
c73b0 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
c73c0 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 6f 73  */.    sizeof(os
c73d0 32 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f  2File),   /* szO
c73e0 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 43 43 48  sFile */.    CCH
c73f0 4d 41 58 50 41 54 48 2c 20 20 20 20 20 20 20 20  MAXPATH,        
c7400 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f  /* mxPathname */
c7410 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
c7420 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
c7430 20 2a 2f 0a 20 20 20 20 22 6f 73 32 22 2c 20 20   */.    "os2",  
c7440 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
c7450 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
c7460 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c7470 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 0a 20 20   pAppData */..  
c7480 20 20 6f 73 32 4f 70 65 6e 2c 20 20 20 20 20 20    os2Open,      
c7490 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a 2f       /* xOpen */
c74a0 0a 20 20 20 20 6f 73 32 44 65 6c 65 74 65 2c 20  .    os2Delete, 
c74b0 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65          /* xDele
c74c0 74 65 20 2a 2f 0a 20 20 20 20 6f 73 32 41 63 63  te */.    os2Acc
c74d0 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  ess,         /* 
c74e0 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20 6f  xAccess */.    o
c74f0 73 32 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20  s2FullPathname, 
c7500 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61    /* xFullPathna
c7510 6d 65 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 4f  me */.    os2DlO
c7520 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  pen,         /* 
c7530 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20 6f  xDlOpen */.    o
c7540 73 32 44 6c 45 72 72 6f 72 2c 20 20 20 20 20 20  s2DlError,      
c7550 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a 2f    /* xDlError */
c7560 0a 20 20 20 20 6f 73 32 44 6c 53 79 6d 2c 20 20  .    os2DlSym,  
c7570 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53 79          /* xDlSy
c7580 6d 20 2a 2f 0a 20 20 20 20 6f 73 32 44 6c 43 6c  m */.    os2DlCl
c7590 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 78  ose,        /* x
c75a0 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20 6f  DlClose */.    o
c75b0 73 32 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20 20  s2Randomness,   
c75c0 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73 73    /* xRandomness
c75d0 20 2a 2f 0a 20 20 20 20 6f 73 32 53 6c 65 65 70   */.    os2Sleep
c75e0 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 53  ,          /* xS
c75f0 6c 65 65 70 20 2a 2f 0a 20 20 20 20 6f 73 32 43  leep */.    os2C
c7600 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20 2f  urrentTime,    /
c7610 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20 2a  * xCurrentTime *
c7620 2f 0a 20 20 20 20 6f 73 32 47 65 74 4c 61 73 74  /.    os2GetLast
c7630 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65 74  Error    /* xGet
c7640 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20 7d  LastError */.  }
c7650 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 5f  ;.  sqlite3_vfs_
c7660 72 65 67 69 73 74 65 72 28 26 6f 73 32 56 66 73  register(&os2Vfs
c7670 2c 20 31 29 3b 0a 20 20 69 6e 69 74 55 63 6f 6e  , 1);.  initUcon
c7680 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 72 65  vObjects();.  re
c7690 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
c76a0 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
c76b0 20 73 71 6c 69 74 65 33 5f 6f 73 5f 65 6e 64 28   sqlite3_os_end(
c76c0 76 6f 69 64 29 7b 0a 20 20 66 72 65 65 55 63 6f  void){.  freeUco
c76d0 6e 76 4f 62 6a 65 63 74 73 28 29 3b 0a 20 20 72  nvObjects();.  r
c76e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
c76f0 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
c7700 4c 49 54 45 5f 4f 53 5f 4f 53 32 20 2a 2f 0a 0a  LITE_OS_OS2 */..
c7710 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c7720 45 6e 64 20 6f 66 20 6f 73 5f 6f 73 32 2e 63 20  End of os_os2.c 
c7730 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7740 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7750 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c7760 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
c7770 42 65 67 69 6e 20 66 69 6c 65 20 6f 73 5f 75 6e  Begin file os_un
c7780 69 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ix.c ***********
c7790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c77a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
c77b0 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32  /*.** 2004 May 2
c77c0 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  2.**.** The auth
c77d0 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
c77e0 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
c77f0 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
c7800 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
c7810 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
c7820 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
c7830 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
c7840 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
c7850 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
c7860 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
c7870 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
c7880 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
c7890 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
c78a0 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
c78b0 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
c78c0 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
c78d0 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
c78e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c78f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7910 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c7920 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
c7930 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63   file contains c
c7940 6f 64 65 20 74 68 61 74 20 69 73 20 73 70 65 63  ode that is spec
c7950 69 66 69 63 20 74 6f 20 55 6e 69 78 20 73 79 73  ific to Unix sys
c7960 74 65 6d 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  tems..**.** $Id:
c7970 20 6f 73 5f 75 6e 69 78 2e 63 2c 76 20 31 2e 32   os_unix.c,v 1.2
c7980 31 36 20 32 30 30 38 2f 31 31 2f 31 39 20 31 36  16 2008/11/19 16
c7990 3a 35 32 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39  :52:44 danielk19
c79a0 37 37 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66 20  77 Exp $.*/.#if 
c79b0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 20  SQLITE_OS_UNIX  
c79c0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
c79d0 68 69 73 20 66 69 6c 65 20 69 73 20 75 73 65 64  his file is used
c79e0 20 6f 6e 20 75 6e 69 78 20 6f 6e 6c 79 20 2a 2f   on unix only */
c79f0 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 53 51 4c 49 54  ../*.** If SQLIT
c7a00 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c7a10 5f 53 54 59 4c 45 20 69 73 20 64 65 66 69 6e 65  _STYLE is define
c7a20 64 20 61 6e 64 20 69 73 20 6e 6f 6e 2d 7a 65 72  d and is non-zer
c7a30 6f 2c 20 74 68 65 6e 20 73 65 76 65 72 61 6c 0a  o, then several.
c7a40 2a 2a 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6c  ** alternative l
c7a50 6f 63 6b 69 6e 67 20 69 6d 70 6c 65 6d 65 6e 74  ocking implement
c7a60 61 74 69 6f 6e 73 20 61 72 65 20 70 72 6f 76 69  ations are provi
c7a70 64 65 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 50  ded:.**.**   * P
c7a80 4f 53 49 58 20 6c 6f 63 6b 69 6e 67 20 28 74 68  OSIX locking (th
c7a90 65 20 64 65 66 61 75 6c 74 29 2c 0a 2a 2a 20 20  e default),.**  
c7aa0 20 2a 20 4e 6f 20 6c 6f 63 6b 69 6e 67 2c 0a 2a   * No locking,.*
c7ab0 2a 20 20 20 2a 20 44 6f 74 2d 66 69 6c 65 20 6c  *   * Dot-file l
c7ac0 6f 63 6b 69 6e 67 2c 0a 2a 2a 20 20 20 2a 20 66  ocking,.**   * f
c7ad0 6c 6f 63 6b 28 29 20 6c 6f 63 6b 69 6e 67 2c 0a  lock() locking,.
c7ae0 2a 2a 20 20 20 2a 20 41 46 50 20 6c 6f 63 6b 69  **   * AFP locki
c7af0 6e 67 20 28 4f 53 58 20 6f 6e 6c 79 29 2c 0a 2a  ng (OSX only),.*
c7b00 2a 20 20 20 2a 20 4e 61 6d 65 64 20 50 4f 53 49  *   * Named POSI
c7b10 58 20 73 65 6d 61 70 68 6f 72 65 73 20 28 56 58  X semaphores (VX
c7b20 57 6f 72 6b 73 20 6f 6e 6c 79 29 2e 0a 2a 2a 0a  Works only)..**.
c7b30 2a 2a 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ** SQLITE_ENABLE
c7b40 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 6f  _LOCKING_STYLE o
c7b50 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 61 20 4d  nly works on a M
c7b60 61 63 2e 20 49 74 20 69 73 20 74 75 72 6e 65 64  ac. It is turned
c7b70 20 6f 6e 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c   on by.** defaul
c7b80 74 20 6f 6e 20 61 20 4d 61 63 20 61 6e 64 20 64  t on a Mac and d
c7b90 69 73 61 62 6c 65 64 20 6f 6e 20 61 6c 6c 20 6f  isabled on all o
c7ba0 74 68 65 72 20 70 6f 73 69 78 20 70 6c 61 74 66  ther posix platf
c7bb0 6f 72 6d 73 2e 0a 2a 2f 0a 23 69 66 20 21 64 65  orms..*/.#if !de
c7bc0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
c7bd0 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
c7be0 45 29 0a 23 20 20 69 66 20 64 65 66 69 6e 65 64  E).#  if defined
c7bf0 28 5f 5f 44 41 52 57 49 4e 5f 5f 29 0a 23 20 20  (__DARWIN__).#  
c7c00 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f    define SQLITE_
c7c10 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53  ENABLE_LOCKING_S
c7c20 54 59 4c 45 20 31 0a 23 20 20 65 6c 73 65 0a 23  TYLE 1.#  else.#
c7c30 20 20 20 20 64 65 66 69 6e 65 20 53 51 4c 49 54      define SQLIT
c7c40 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47  E_ENABLE_LOCKING
c7c50 5f 53 54 59 4c 45 20 30 0a 23 20 20 65 6e 64 69  _STYLE 0.#  endi
c7c60 66 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  f.#endif../*.** 
c7c70 44 65 66 69 6e 65 20 74 68 65 20 49 53 5f 56 58  Define the IS_VX
c7c80 57 4f 52 4b 53 20 70 72 65 2d 70 72 6f 63 65 73  WORKS pre-proces
c7c90 73 6f 72 20 6d 61 63 72 6f 20 74 6f 20 31 20 69  sor macro to 1 i
c7ca0 66 20 62 75 69 6c 64 69 6e 67 20 6f 6e 20 0a 2a  f building on .*
c7cb0 2a 20 76 78 77 6f 72 6b 73 2c 20 6f 72 20 30 20  * vxworks, or 0 
c7cc0 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 23 69  otherwise..*/.#i
c7cd0 66 20 64 65 66 69 6e 65 64 28 5f 5f 52 54 50 5f  f defined(__RTP_
c7ce0 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f 57  _) || defined(_W
c7cf0 52 53 5f 4b 45 52 4e 45 4c 29 0a 23 20 20 64 65  RS_KERNEL).#  de
c7d00 66 69 6e 65 20 49 53 5f 56 58 57 4f 52 4b 53 20  fine IS_VXWORKS 
c7d10 31 0a 23 65 6c 73 65 0a 23 20 20 64 65 66 69 6e  1.#else.#  defin
c7d20 65 20 49 53 5f 56 58 57 4f 52 4b 53 20 30 0a 23  e IS_VXWORKS 0.#
c7d30 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  endif../*.** The
c7d40 73 65 20 23 64 65 66 69 6e 65 73 20 73 68 6f 75  se #defines shou
c7d50 6c 64 20 65 6e 61 62 6c 65 20 3e 32 47 42 20 66  ld enable >2GB f
c7d60 69 6c 65 20 73 75 70 70 6f 72 74 20 6f 6e 20 50  ile support on P
c7d70 6f 73 69 78 20 69 66 20 74 68 65 0a 2a 2a 20 75  osix if the.** u
c7d80 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72 61 74  nderlying operat
c7d90 69 6e 67 20 73 79 73 74 65 6d 20 73 75 70 70 6f  ing system suppo
c7da0 72 74 73 20 69 74 2e 20 20 49 66 20 74 68 65 20  rts it.  If the 
c7db0 4f 53 20 6c 61 63 6b 73 0a 2a 2a 20 6c 61 72 67  OS lacks.** larg
c7dc0 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74 2c 20  e file support, 
c7dd0 74 68 65 73 65 20 73 68 6f 75 6c 64 20 62 65 20  these should be 
c7de0 6e 6f 2d 6f 70 73 2e 0a 2a 2a 0a 2a 2a 20 4c 61  no-ops..**.** La
c7df0 72 67 65 20 66 69 6c 65 20 73 75 70 70 6f 72 74  rge file support
c7e00 20 63 61 6e 20 62 65 20 64 69 73 61 62 6c 65 64   can be disabled
c7e10 20 75 73 69 6e 67 20 74 68 65 20 2d 44 53 51 4c   using the -DSQL
c7e20 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46 53 20  ITE_DISABLE_LFS 
c7e30 73 77 69 74 63 68 0a 2a 2a 20 6f 6e 20 74 68 65  switch.** on the
c7e40 20 63 6f 6d 70 69 6c 65 72 20 63 6f 6d 6d 61 6e   compiler comman
c7e50 64 20 6c 69 6e 65 2e 20 20 54 68 69 73 20 69 73  d line.  This is
c7e60 20 6e 65 63 65 73 73 61 72 79 20 69 66 20 79 6f   necessary if yo
c7e70 75 20 61 72 65 20 63 6f 6d 70 69 6c 69 6e 67 0a  u are compiling.
c7e80 2a 2a 20 6f 6e 20 61 20 72 65 63 65 6e 74 20 6d  ** on a recent m
c7e90 61 63 68 69 6e 65 20 28 65 78 3a 20 52 65 64 48  achine (ex: RedH
c7ea0 61 74 20 37 2e 32 29 20 62 75 74 20 79 6f 75 20  at 7.2) but you 
c7eb0 77 61 6e 74 20 79 6f 75 72 20 63 6f 64 65 20 74  want your code t
c7ec0 6f 20 77 6f 72 6b 0a 2a 2a 20 6f 6e 20 61 6e 20  o work.** on an 
c7ed0 6f 6c 64 65 72 20 6d 61 63 68 69 6e 65 20 28 65  older machine (e
c7ee0 78 3a 20 52 65 64 48 61 74 20 36 2e 30 29 2e 20  x: RedHat 6.0). 
c7ef0 20 49 66 20 79 6f 75 20 63 6f 6d 70 69 6c 65 20   If you compile 
c7f00 6f 6e 20 52 65 64 48 61 74 20 37 2e 32 0a 2a 2a  on RedHat 7.2.**
c7f10 20 77 69 74 68 6f 75 74 20 74 68 69 73 20 6f 70   without this op
c7f20 74 69 6f 6e 2c 20 4c 46 53 20 69 73 20 65 6e 61  tion, LFS is ena
c7f30 62 6c 65 2e 20 20 42 75 74 20 4c 46 53 20 64 6f  ble.  But LFS do
c7f40 65 73 20 6e 6f 74 20 65 78 69 73 74 20 69 6e 20  es not exist in 
c7f50 74 68 65 20 6b 65 72 6e 65 6c 0a 2a 2a 20 69 6e  the kernel.** in
c7f60 20 52 65 64 48 61 74 20 36 2e 30 2c 20 73 6f 20   RedHat 6.0, so 
c7f70 74 68 65 20 63 6f 64 65 20 77 6f 6e 27 74 20 77  the code won't w
c7f80 6f 72 6b 2e 20 20 48 65 6e 63 65 2c 20 66 6f 72  ork.  Hence, for
c7f90 20 6d 61 78 69 6d 75 6d 20 62 69 6e 61 72 79 0a   maximum binary.
c7fa0 2a 2a 20 70 6f 72 74 61 62 69 6c 69 74 79 20 79  ** portability y
c7fb0 6f 75 20 73 68 6f 75 6c 64 20 6f 6d 69 74 20 4c  ou should omit L
c7fc0 46 53 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  FS..*/.#ifndef S
c7fd0 51 4c 49 54 45 5f 44 49 53 41 42 4c 45 5f 4c 46  QLITE_DISABLE_LF
c7fe0 53 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47  S.# define _LARG
c7ff0 45 5f 46 49 4c 45 20 20 20 20 20 20 20 31 0a 23  E_FILE       1.#
c8000 20 69 66 6e 64 65 66 20 5f 46 49 4c 45 5f 4f 46   ifndef _FILE_OF
c8010 46 53 45 54 5f 42 49 54 53 0a 23 20 20 20 64 65  FSET_BITS.#   de
c8020 66 69 6e 65 20 5f 46 49 4c 45 5f 4f 46 46 53 45  fine _FILE_OFFSE
c8030 54 5f 42 49 54 53 20 36 34 0a 23 20 65 6e 64 69  T_BITS 64.# endi
c8040 66 0a 23 20 64 65 66 69 6e 65 20 5f 4c 41 52 47  f.# define _LARG
c8050 45 46 49 4c 45 5f 53 4f 55 52 43 45 20 31 0a 23  EFILE_SOURCE 1.#
c8060 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 73 74 61  endif../*.** sta
c8070 6e 64 61 72 64 20 69 6e 63 6c 75 64 65 20 66 69  ndard include fi
c8080 6c 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65  les..*/.#include
c8090 20 3c 73 79 73 2f 74 79 70 65 73 2e 68 3e 0a 23   <sys/types.h>.#
c80a0 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 73 74 61  include <sys/sta
c80b0 74 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 66  t.h>.#include <f
c80c0 63 6e 74 6c 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  cntl.h>.#include
c80d0 20 3c 75 6e 69 73 74 64 2e 68 3e 0a 23 69 6e 63   <unistd.h>.#inc
c80e0 6c 75 64 65 20 3c 73 79 73 2f 74 69 6d 65 2e 68  lude <sys/time.h
c80f0 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 65 72 72 6e  >.#include <errn
c8100 6f 2e 68 3e 0a 0a 23 69 66 20 53 51 4c 49 54 45  o.h>..#if SQLITE
c8110 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f  _ENABLE_LOCKING_
c8120 53 54 59 4c 45 0a 23 20 69 6e 63 6c 75 64 65 20  STYLE.# include 
c8130 3c 73 79 73 2f 69 6f 63 74 6c 2e 68 3e 0a 23 20  <sys/ioctl.h>.# 
c8140 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 23 20  if IS_VXWORKS.# 
c8150 20 64 65 66 69 6e 65 20 6c 73 74 61 74 20 73 74   define lstat st
c8160 61 74 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73  at.#  include <s
c8170 65 6d 61 70 68 6f 72 65 2e 68 3e 0a 23 20 20 69  emaphore.h>.#  i
c8180 6e 63 6c 75 64 65 20 3c 6c 69 6d 69 74 73 2e 68  nclude <limits.h
c8190 3e 0a 23 20 65 6c 73 65 0a 23 20 20 69 6e 63 6c  >.# else.#  incl
c81a0 75 64 65 20 3c 73 79 73 2f 70 61 72 61 6d 2e 68  ude <sys/param.h
c81b0 3e 0a 23 20 20 69 6e 63 6c 75 64 65 20 3c 73 79  >.#  include <sy
c81c0 73 2f 6d 6f 75 6e 74 2e 68 3e 0a 23 20 65 6e 64  s/mount.h>.# end
c81d0 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  if.#endif /* SQL
c81e0 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
c81f0 4e 47 5f 53 54 59 4c 45 20 2a 2f 0a 0a 2f 2a 0a  NG_STYLE */../*.
c8200 2a 2a 20 49 66 20 77 65 20 61 72 65 20 74 6f 20  ** If we are to 
c8210 62 65 20 74 68 72 65 61 64 2d 73 61 66 65 2c 20  be thread-safe, 
c8220 69 6e 63 6c 75 64 65 20 74 68 65 20 70 74 68 72  include the pthr
c8230 65 61 64 73 20 68 65 61 64 65 72 20 61 6e 64 20  eads header and 
c8240 64 65 66 69 6e 65 0a 2a 2a 20 74 68 65 20 53 51  define.** the SQ
c8250 4c 49 54 45 5f 55 4e 49 58 5f 54 48 52 45 41 44  LITE_UNIX_THREAD
c8260 53 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23 69 66 20  S macro..*/.#if 
c8270 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53 41 46  SQLITE_THREADSAF
c8280 45 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  E.# define SQLIT
c8290 45 5f 55 4e 49 58 5f 54 48 52 45 41 44 53 20 31  E_UNIX_THREADS 1
c82a0 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44  .#endif../*.** D
c82b0 65 66 61 75 6c 74 20 70 65 72 6d 69 73 73 69 6f  efault permissio
c82c0 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ns when creating
c82d0 20 61 20 6e 65 77 20 66 69 6c 65 0a 2a 2f 0a 23   a new file.*/.#
c82e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 44 45  ifndef SQLITE_DE
c82f0 46 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49  FAULT_FILE_PERMI
c8300 53 53 49 4f 4e 53 0a 23 20 64 65 66 69 6e 65 20  SSIONS.# define 
c8310 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 46  SQLITE_DEFAULT_F
c8320 49 4c 45 5f 50 45 52 4d 49 53 53 49 4f 4e 53 20  ILE_PERMISSIONS 
c8330 30 36 34 34 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0644.#endif../*.
c8340 2a 2a 20 4d 61 78 69 6d 75 6d 20 73 75 70 70 6f  ** Maximum suppo
c8350 72 74 65 64 20 70 61 74 68 2d 6c 65 6e 67 74 68  rted path-length
c8360 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 41 58  ..*/.#define MAX
c8370 5f 50 41 54 48 4e 41 4d 45 20 35 31 32 0a 0a 0a  _PATHNAME 512...
c8380 2f 2a 0a 2a 2a 20 54 68 65 20 75 6e 69 78 46 69  /*.** The unixFi
c8390 6c 65 20 73 74 72 75 63 74 75 72 65 20 69 73 20  le structure is 
c83a0 73 75 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69  subclass of sqli
c83b0 74 65 33 5f 66 69 6c 65 20 73 70 65 63 69 66 69  te3_file specifi
c83c0 63 20 66 6f 72 20 74 68 65 20 75 6e 69 78 0a 2a  c for the unix.*
c83d0 2a 20 70 72 6f 74 61 62 69 6c 69 74 79 20 6c 61  * protability la
c83e0 79 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  yer..*/.typedef 
c83f0 73 74 72 75 63 74 20 75 6e 69 78 46 69 6c 65 20  struct unixFile 
c8400 75 6e 69 78 46 69 6c 65 3b 0a 73 74 72 75 63 74  unixFile;.struct
c8410 20 75 6e 69 78 46 69 6c 65 20 7b 0a 20 20 73 71   unixFile {.  sq
c8420 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73  lite3_io_methods
c8430 20 63 6f 6e 73 74 20 2a 70 4d 65 74 68 6f 64 3b   const *pMethod;
c8440 20 20 2f 2a 20 41 6c 77 61 79 73 20 74 68 65 20    /* Always the 
c8450 66 69 72 73 74 20 65 6e 74 72 79 20 2a 2f 0a 23  first entry */.#
c8460 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
c8470 54 0a 20 20 2f 2a 20 49 6e 20 74 65 73 74 20 6d  T.  /* In test m
c8480 6f 64 65 2c 20 69 6e 63 72 65 61 73 65 20 74 68  ode, increase th
c8490 65 20 73 69 7a 65 20 6f 66 20 74 68 69 73 20 73  e size of this s
c84a0 74 72 75 63 74 75 72 65 20 61 20 62 69 74 20 73  tructure a bit s
c84b0 6f 20 74 68 61 74 20 0a 20 20 2a 2a 20 69 74 20  o that .  ** it 
c84c0 69 73 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  is larger than t
c84d0 68 65 20 73 74 72 75 63 74 20 43 72 61 73 68 46  he struct CrashF
c84e0 69 6c 65 20 64 65 66 69 6e 65 64 20 69 6e 20 74  ile defined in t
c84f0 65 73 74 36 2e 63 2e 0a 20 20 2a 2f 0a 20 20 63  est6.c..  */.  c
c8500 68 61 72 20 61 50 61 64 64 69 6e 67 5b 33 32 5d  har aPadding[32]
c8510 3b 0a 23 65 6e 64 69 66 0a 20 20 73 74 72 75 63  ;.#endif.  struc
c8520 74 20 6f 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e  t openCnt *pOpen
c8530 3b 20 20 20 20 2f 2a 20 49 6e 66 6f 20 61 62 6f  ;    /* Info abo
c8540 75 74 20 61 6c 6c 20 6f 70 65 6e 20 66 64 27 73  ut all open fd's
c8550 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20 2a   on this inode *
c8560 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49  /.  struct lockI
c8570 6e 66 6f 20 2a 70 4c 6f 63 6b 3b 20 20 20 2f 2a  nfo *pLock;   /*
c8580 20 49 6e 66 6f 20 61 62 6f 75 74 20 6c 6f 63 6b   Info about lock
c8590 73 20 6f 6e 20 74 68 69 73 20 69 6e 6f 64 65 20  s on this inode 
c85a0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  */.#if SQLITE_EN
c85b0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
c85c0 4c 45 0a 20 20 76 6f 69 64 20 2a 6c 6f 63 6b 69  LE.  void *locki
c85d0 6e 67 43 6f 6e 74 65 78 74 3b 20 20 20 20 20 2f  ngContext;     /
c85e0 2a 20 4c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20  * Locking style 
c85f0 73 70 65 63 69 66 69 63 20 73 74 61 74 65 20 2a  specific state *
c8600 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 68  /.#endif.  int h
c8610 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
c8620 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65       /* The file
c8630 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
c8640 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6c   unsigned char l
c8650 6f 63 6b 74 79 70 65 3b 20 20 20 2f 2a 20 54 68  ocktype;   /* Th
c8660 65 20 74 79 70 65 20 6f 66 20 6c 6f 63 6b 20 68  e type of lock h
c8670 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 64 20 2a  eld on this fd *
c8680 2f 0a 20 20 69 6e 74 20 64 69 72 66 64 3b 20 20  /.  int dirfd;  
c8690 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
c86a0 20 46 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   File descriptor
c86b0 20 66 6f 72 20 74 68 65 20 64 69 72 65 63 74 6f   for the directo
c86c0 72 79 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45  ry */.#if SQLITE
c86d0 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 70 74  _THREADSAFE.  pt
c86e0 68 72 65 61 64 5f 74 20 74 69 64 3b 20 20 20 20  hread_t tid;    
c86f0 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
c8700 68 72 65 61 64 20 74 68 61 74 20 22 6f 77 6e 73  hread that "owns
c8710 22 20 74 68 69 73 20 75 6e 69 78 46 69 6c 65 20  " this unixFile 
c8720 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20  */.#endif.  int 
c8730 6c 61 73 74 45 72 72 6e 6f 3b 20 20 20 20 20 20  lastErrno;      
c8740 20 20 20 20 20 20 2f 2a 20 54 68 65 20 75 6e 69        /* The uni
c8750 78 20 65 72 72 6e 6f 20 66 72 6f 6d 20 74 68 65  x errno from the
c8760 20 6c 61 73 74 20 49 2f 4f 20 65 72 72 6f 72 20   last I/O error 
c8770 2a 2f 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b  */.#if IS_VXWORK
c8780 53 0a 20 20 69 6e 74 20 69 73 44 65 6c 65 74 65  S.  int isDelete
c8790 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
c87a0 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c 6f 73 65   Delete on close
c87b0 20 69 66 20 74 72 75 65 20 2a 2f 0a 20 20 63 68   if true */.  ch
c87c0 61 72 20 2a 7a 52 65 61 6c 70 61 74 68 3b 0a 23  ar *zRealpath;.#
c87d0 65 6e 64 69 66 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  endif.};../*.** 
c87e0 49 6e 63 6c 75 64 65 20 63 6f 64 65 20 74 68 61  Include code tha
c87f0 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74 6f 20 61  t is common to a
c8800 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73 0a  ll os_*.c files.
c8810 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
c8820 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73 5f 63 6f  ** Include os_co
c8830 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65 20 6d 69  mmon.h in the mi
c8840 64 64 6c 65 20 6f 66 20 6f 73 5f 75 6e 69 78 2e  ddle of os_unix.
c8850 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
c8860 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
c8870 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73  ** Begin file os
c8880 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a  _common.h ******
c8890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c88a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c88b0 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61  */./*.** 2004 Ma
c88c0 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 22.**.** The a
c88d0 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
c88e0 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
c88f0 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
c8900 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
c8910 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
c8920 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
c8930 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
c8940 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
c8950 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
c8960 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
c8970 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
c8980 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
c8990 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
c89a0 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
c89b0 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
c89c0 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
c89d0 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
c89e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c89f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c8a20 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
c8a30 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
c8a40 73 20 6d 61 63 72 6f 73 20 61 6e 64 20 61 20 6c  s macros and a l
c8a50 69 74 74 6c 65 20 62 69 74 20 6f 66 20 63 6f 64  ittle bit of cod
c8a60 65 20 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e  e that is common
c8a70 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66 20 74 68   to.** all of th
c8a80 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70 65 63 69  e platform-speci
c8a90 66 69 63 20 66 69 6c 65 73 20 28 6f 73 5f 2a 2e  fic files (os_*.
c8aa0 63 29 20 61 6e 64 20 69 73 20 23 69 6e 63 6c 75  c) and is #inclu
c8ab0 64 65 64 20 69 6e 74 6f 20 74 68 6f 73 65 0a 2a  ded into those.*
c8ac0 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54  * files..**.** T
c8ad0 68 69 73 20 66 69 6c 65 20 73 68 6f 75 6c 64 20  his file should 
c8ae0 62 65 20 23 69 6e 63 6c 75 64 65 64 20 62 79 20  be #included by 
c8af0 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69 6c 65 73  the os_*.c files
c8b00 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73 20 6e 6f   only.  It is no
c8b10 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61 6c 20 70  t a.** general p
c8b20 75 72 70 6f 73 65 20 68 65 61 64 65 72 20 66 69  urpose header fi
c8b30 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  le..**.** $Id: o
c8b40 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20 31 2e 33  s_common.h,v 1.3
c8b50 37 20 32 30 30 38 2f 30 35 2f 32 39 20 32 30 3a  7 2008/05/29 20:
c8b60 32 32 3a 33 37 20 73 68 61 6e 65 20 45 78 70 20  22:37 shane Exp 
c8b70 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 4f 53  $.*/.#ifndef _OS
c8b80 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64 65 66 69  _COMMON_H_.#defi
c8b90 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ne _OS_COMMON_H_
c8ba0 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65 61 73 74  ../*.** At least
c8bb0 20 74 77 6f 20 62 75 67 73 20 68 61 76 65 20 73   two bugs have s
c8bc0 6c 69 70 70 65 64 20 69 6e 20 62 65 63 61 75 73  lipped in becaus
c8bd0 65 20 77 65 20 63 68 61 6e 67 65 64 20 74 68 65  e we changed the
c8be0 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47 0a 2a 2a   MEMORY_DEBUG.**
c8bf0 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c 49 54 45   macro to SQLITE
c8c00 5f 44 45 42 55 47 20 61 6e 64 20 73 6f 6d 65 20  _DEBUG and some 
c8c10 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c 65 73 20  older makefiles 
c8c20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 6d 61 64  have not yet mad
c8c30 65 20 74 68 65 0a 2a 2a 20 73 77 69 74 63 68 2e  e the.** switch.
c8c40 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20    The following 
c8c50 63 6f 64 65 20 73 68 6f 75 6c 64 20 63 61 74 63  code should catc
c8c60 68 20 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 61  h this problem a
c8c70 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 2e 0a  t compile-time..
c8c80 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d 4f 52 59  */.#ifdef MEMORY
c8c90 5f 44 45 42 55 47 0a 23 20 65 72 72 6f 72 20 22  _DEBUG.# error "
c8ca0 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  The MEMORY_DEBUG
c8cb0 20 6d 61 63 72 6f 20 69 73 20 6f 62 73 6f 6c 65   macro is obsole
c8cc0 74 65 2e 20 20 55 73 65 20 53 51 4c 49 54 45 5f  te.  Use SQLITE_
c8cd0 44 45 42 55 47 20 69 6e 73 74 65 61 64 2e 22 0a  DEBUG instead.".
c8ce0 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20 2a 20 57  #endif.../*. * W
c8cf0 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 74 68 69  hen testing, thi
c8d00 73 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c  s global variabl
c8d10 65 20 73 74 6f 72 65 73 20 74 68 65 20 6c 6f 63  e stores the loc
c8d20 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 2a 20  ation of the. * 
c8d30 70 65 6e 64 69 6e 67 2d 62 79 74 65 20 69 6e 20  pending-byte in 
c8d40 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
c8d50 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66 20 53 51  e.. */.#ifdef SQ
c8d60 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
c8d70 5f 41 50 49 20 75 6e 73 69 67 6e 65 64 20 69 6e  _API unsigned in
c8d80 74 20 73 71 6c 69 74 65 33 5f 70 65 6e 64 69 6e  t sqlite3_pendin
c8d90 67 5f 62 79 74 65 20 3d 20 30 78 34 30 30 30 30  g_byte = 0x40000
c8da0 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66  000;.#endif..#if
c8db0 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47  def SQLITE_DEBUG
c8dc0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
c8dd0 69 6e 74 20 73 71 6c 69 74 65 33 4f 53 54 72 61  int sqlite3OSTra
c8de0 63 65 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20  ce = 0;.#define 
c8df0 4f 53 54 52 41 43 45 31 28 58 29 20 20 20 20 20  OSTRACE1(X)     
c8e00 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
c8e10 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
c8e20 44 65 62 75 67 50 72 69 6e 74 66 28 58 29 0a 23  DebugPrintf(X).#
c8e30 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28  define OSTRACE2(
c8e40 58 2c 59 29 20 20 20 20 20 20 20 69 66 28 20 73  X,Y)       if( s
c8e50 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29 20  qlite3OSTrace ) 
c8e60 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
c8e70 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e 65 20  tf(X,Y).#define 
c8e80 4f 53 54 52 41 43 45 33 28 58 2c 59 2c 5a 29 20  OSTRACE3(X,Y,Z) 
c8e90 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f      if( sqlite3O
c8ea0 53 54 72 61 63 65 20 29 20 73 71 6c 69 74 65 33  STrace ) sqlite3
c8eb0 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
c8ec0 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  Z).#define OSTRA
c8ed0 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20 20 20 69  CE4(X,Y,Z,A)   i
c8ee0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
c8ef0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
c8f00 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 29 0a  Printf(X,Y,Z,A).
c8f10 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 35  #define OSTRACE5
c8f20 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69 66 28 20  (X,Y,Z,A,B) if( 
c8f30 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65 20 29  sqlite3OSTrace )
c8f40 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
c8f50 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  ntf(X,Y,Z,A,B).#
c8f60 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
c8f70 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20 5c 0a 20  X,Y,Z,A,B,C) \. 
c8f80 20 20 20 69 66 28 73 71 6c 69 74 65 33 4f 53 54     if(sqlite3OST
c8f90 72 61 63 65 29 20 73 71 6c 69 74 65 33 44 65 62  race) sqlite3Deb
c8fa0 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41  ugPrintf(X,Y,Z,A
c8fb0 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,B,C).#define OS
c8fc0 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE7(X,Y,Z,A,B
c8fd0 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69 66 28 73  ,C,D) \.    if(s
c8fe0 71 6c 69 74 65 33 4f 53 54 72 61 63 65 29 20 73  qlite3OSTrace) s
c8ff0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
c9000 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29  f(X,Y,Z,A,B,C,D)
c9010 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f  .#else.#define O
c9020 53 54 52 41 43 45 31 28 58 29 0a 23 64 65 66 69  STRACE1(X).#defi
c9030 6e 65 20 4f 53 54 52 41 43 45 32 28 58 2c 59 29  ne OSTRACE2(X,Y)
c9040 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45  .#define OSTRACE
c9050 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65  3(X,Y,Z).#define
c9060 20 4f 53 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c   OSTRACE4(X,Y,Z,
c9070 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
c9080 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 0a 23  CE5(X,Y,Z,A,B).#
c9090 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 36 28  define OSTRACE6(
c90a0 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a 23 64 65  X,Y,Z,A,B,C).#de
c90b0 66 69 6e 65 20 4f 53 54 52 41 43 45 37 28 58 2c  fine OSTRACE7(X,
c90c0 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a 23 65 6e  Y,Z,A,B,C,D).#en
c90d0 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f  dif../*.** Macro
c90e0 73 20 66 6f 72 20 70 65 72 66 6f 72 6d 61 6e 63  s for performanc
c90f0 65 20 74 72 61 63 69 6e 67 2e 20 20 4e 6f 72 6d  e tracing.  Norm
c9100 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f 66 66 2e  ally turned off.
c9110 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a 2a 2a 20    Only works.** 
c9120 6f 6e 20 69 34 38 36 20 68 61 72 64 77 61 72 65  on i486 hardware
c9130 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
c9140 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43 45 5f 54  TE_PERFORMANCE_T
c9150 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20 68 77 74  RACE../* .** hwt
c9160 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e 73 20 69  ime.h contains i
c9170 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c 65 72 20  nline assembler 
c9180 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c 65 6d 65  code for impleme
c9190 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67 68 2d 70  nting .** high-p
c91a0 65 72 66 6f 72 6d 61 6e 63 65 20 74 69 6d 69 6e  erformance timin
c91b0 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 2f  g routines..*/./
c91c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49  ************** I
c91d0 6e 63 6c 75 64 65 20 68 77 74 69 6d 65 2e 68 20  nclude hwtime.h 
c91e0 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
c91f0 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
c9200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
c9210 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
c9220 65 67 69 6e 20 66 69 6c 65 20 68 77 74 69 6d 65  egin file hwtime
c9230 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
c9240 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9250 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
c9260 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32 37  *.** 2008 May 27
c9270 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
c9280 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
c9290 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
c92a0 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
c92b0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
c92c0 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
c92d0 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
c92e0 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
c92f0 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
c9300 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
c9310 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
c9320 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
c9330 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
c9340 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
c9350 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
c9360 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
c9370 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
c9380 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
c9390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c93a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c93b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c93c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c93d0 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ****.**.** This 
c93e0 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e  file contains in
c93f0 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65 20 66 6f  line asm code fo
c9400 72 20 72 65 74 72 69 65 76 69 6e 67 20 22 68 69  r retrieving "hi
c9410 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 22 0a  gh-performance".
c9420 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66 6f 72 20  ** counters for 
c9430 78 38 36 20 63 6c 61 73 73 20 43 50 55 73 2e 0a  x86 class CPUs..
c9440 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77 74 69 6d  **.** $Id: hwtim
c9450 65 2e 68 2c 76 20 31 2e 33 20 32 30 30 38 2f 30  e.h,v 1.3 2008/0
c9460 38 2f 30 31 20 31 34 3a 33 33 3a 31 35 20 73 68  8/01 14:33:15 sh
c9470 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a 23 69 66  ane Exp $.*/.#if
c9480 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f 48 5f 0a  ndef _HWTIME_H_.
c9490 23 64 65 66 69 6e 65 20 5f 48 57 54 49 4d 45 5f  #define _HWTIME_
c94a0 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  H_../*.** The fo
c94b0 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20  llowing routine 
c94c0 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 70 65  only works on pe
c94d0 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28 6f 72 20  ntium-class (or 
c94e0 6e 65 77 65 72 29 20 70 72 6f 63 65 73 73 6f 72  newer) processor
c94f0 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73 20 74 68  s..** It uses th
c9500 65 20 52 44 54 53 43 20 6f 70 63 6f 64 65 20 74  e RDTSC opcode t
c9510 6f 20 72 65 61 64 20 74 68 65 20 63 79 63 6c 65  o read the cycle
c9520 20 63 6f 75 6e 74 20 76 61 6c 75 65 20 6f 75 74   count value out
c9530 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72 6f 63 65   of the.** proce
c9540 73 73 6f 72 20 61 6e 64 20 72 65 74 75 72 6e 73  ssor and returns
c9550 20 74 68 61 74 20 76 61 6c 75 65 2e 20 20 54 68   that value.  Th
c9560 69 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  is can be used f
c9570 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a 2a 20 70  or high-res.** p
c9580 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a 23 69 66  rofiling..*/.#if
c9590 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43   (defined(__GNUC
c95a0 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  __) || defined(_
c95b0 4d 53 43 5f 56 45 52 29 29 20 26 26 20 5c 0a 20  MSC_VER)) && \. 
c95c0 20 20 20 20 20 28 64 65 66 69 6e 65 64 28 69 33       (defined(i3
c95d0 38 36 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 5f  86) || defined(_
c95e0 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64 65 66 69  _i386__) || defi
c95f0 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29 0a 0a 20  ned(_M_IX86)).. 
c9600 20 23 69 66 20 64 65 66 69 6e 65 64 28 5f 5f 47   #if defined(__G
c9610 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69 6e 6c 69  NUC__)..  __inli
c9620 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
c9630 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
c9640 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 75 6e 73  (void){.     uns
c9650 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c 20 68 69  igned int lo, hi
c9660 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f  ;.     __asm__ _
c9670 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64  _volatile__ ("rd
c9680 74 73 63 22 20 3a 20 22 3d 61 22 20 28 6c 6f 29  tsc" : "=a" (lo)
c9690 2c 20 22 3d 64 22 20 28 68 69 29 29 3b 0a 20 20  , "=d" (hi));.  
c96a0 20 20 20 72 65 74 75 72 6e 20 28 73 71 6c 69 74     return (sqlit
c96b0 65 5f 75 69 6e 74 36 34 29 68 69 20 3c 3c 20 33  e_uint64)hi << 3
c96c0 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a 20 20 23  2 | lo;.  }..  #
c96d0 65 6c 69 66 20 64 65 66 69 6e 65 64 28 5f 4d 53  elif defined(_MS
c96e0 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64 65 63 6c  C_VER)..  __decl
c96f0 73 70 65 63 28 6e 61 6b 65 64 29 20 5f 5f 69 6e  spec(naked) __in
c9700 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75 69 6e 74  line sqlite_uint
c9710 36 34 20 5f 5f 63 64 65 63 6c 20 73 71 6c 69 74  64 __cdecl sqlit
c9720 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a  e3Hwtime(void){.
c9730 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a 20 20 20       __asm {.   
c9740 20 20 20 20 20 72 64 74 73 63 0a 20 20 20 20 20       rdtsc.     
c9750 20 20 20 72 65 74 20 20 20 20 20 20 20 3b 20 72     ret       ; r
c9760 65 74 75 72 6e 20 76 61 6c 75 65 20 61 74 20 45  eturn value at E
c9770 44 58 3a 45 41 58 0a 20 20 20 20 20 7d 0a 20 20  DX:EAX.     }.  
c9780 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a 23 65 6c  }..  #endif..#el
c9790 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47 4e  if (defined(__GN
c97a0 55 43 5f 5f 29 20 26 26 20 64 65 66 69 6e 65 64  UC__) && defined
c97b0 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29 0a 0a 20  (__x86_64__)).. 
c97c0 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69   __inline__ sqli
c97d0 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
c97e0 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20  3Hwtime(void){. 
c97f0 20 20 20 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f       unsigned lo
c9800 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20 20 5f 5f  ng val;.      __
c9810 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65  asm__ __volatile
c9820 5f 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d  __ ("rdtsc" : "=
c9830 41 22 20 28 76 61 6c 29 29 3b 0a 20 20 20 20 20  A" (val));.     
c9840 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 20 20 7d   return val;.  }
c9850 0a 20 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  . .#elif (define
c9860 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
c9870 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f 5f 29 29  efined(__ppc__))
c9880 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
c9890 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
c98a0 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
c98b0 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
c98c0 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 76 61   long long retva
c98d0 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65  l;.      unsigne
c98e0 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a 20 20 20  d long junk;.   
c98f0 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c     __asm__ __vol
c9900 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c 0a 20 20  atile__ ("\n\.  
c9910 20 20 20 20 20 20 20 20 31 3a 20 20 20 20 20 20          1:      
c9920 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c 0a 20 20  mftbu   %1\n\.  
c9930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9940 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e 5c 0a 20  mftb    %L0\n\. 
c9950 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9960 20 6d 66 74 62 75 20 20 20 25 30 5c 6e 5c 0a 20   mftbu   %0\n\. 
c9970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c9980 20 63 6d 70 77 20 20 20 20 25 30 2c 25 31 5c 6e   cmpw    %0,%1\n
c9990 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
c99a0 20 20 20 20 62 6e 65 20 20 20 20 20 31 62 22 0a      bne     1b".
c99b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
c99c0 20 20 3a 20 22 3d 72 22 20 28 72 65 74 76 61 6c    : "=r" (retval
c99d0 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b 29 29 3b  ), "=r" (junk));
c99e0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 65  .      return re
c99f0 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65 6c 73 65  tval;.  }..#else
c9a00 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65 65 64 20  ..  #error Need 
c9a10 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
c9a20 66 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28  f sqlite3Hwtime(
c9a30 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66  ) for your platf
c9a40 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  orm...  /*.  ** 
c9a50 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 6f  To compile witho
c9a60 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  ut implementing 
c9a70 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20  sqlite3Hwtime() 
c9a80 66 6f 72 20 79 6f 75 72 20 70 6c 61 74 66 6f 72  for your platfor
c9a90 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63 61 6e 20  m,.  ** you can 
c9aa0 72 65 6d 6f 76 65 20 74 68 65 20 61 62 6f 76 65  remove the above
c9ab0 20 23 65 72 72 6f 72 20 61 6e 64 20 75 73 65 20   #error and use 
c9ac0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
c9ad0 2a 2a 20 73 74 75 62 20 66 75 6e 63 74 69 6f 6e  ** stub function
c9ae0 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c 6f 73 65  .  You will lose
c9af0 20 74 69 6d 69 6e 67 20 73 75 70 70 6f 72 74 20   timing support 
c9b00 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a 20 6f 66  for many.  ** of
c9b10 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 20 61   the debugging a
c9b20 6e 64 20 74 65 73 74 69 6e 67 20 75 74 69 6c 69  nd testing utili
c9b30 74 69 65 73 2c 20 62 75 74 20 69 74 20 73 68 6f  ties, but it sho
c9b40 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c 65 61 73  uld at.  ** leas
c9b50 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75  t compile and ru
c9b60 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54 45 5f 50  n..  */.SQLITE_P
c9b70 52 49 56 41 54 45 20 20 20 73 71 6c 69 74 65 5f  RIVATE   sqlite_
c9b80 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77  uint64 sqlite3Hw
c9b90 74 69 6d 65 28 76 6f 69 64 29 7b 20 72 65 74 75  time(void){ retu
c9ba0 72 6e 20 28 28 73 71 6c 69 74 65 5f 75 69 6e 74  rn ((sqlite_uint
c9bb0 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e 64 69 66  64)0); }..#endif
c9bc0 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  ..#endif /* !def
c9bd0 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f 48 5f 29  ined(_HWTIME_H_)
c9be0 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
c9bf0 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68 77 74 69  **** End of hwti
c9c00 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  me.h ***********
c9c10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9c20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
c9c30 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
c9c40 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
c9c50 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
c9c60 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68  f in os_common.h
c9c70 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
c9c80 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20 73 71 6c  ***/..static sql
c9c90 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 73 74 61  ite_uint64 g_sta
c9ca0 72 74 3b 0a 73 74 61 74 69 63 20 73 71 6c 69 74  rt;.static sqlit
c9cb0 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c 61 70 73  e_uint64 g_elaps
c9cc0 65 64 3b 0a 23 64 65 66 69 6e 65 20 54 49 4d 45  ed;.#define TIME
c9cd0 52 5f 53 54 41 52 54 20 20 20 20 20 20 20 67 5f  R_START       g_
c9ce0 73 74 61 72 74 3d 73 71 6c 69 74 65 33 48 77 74  start=sqlite3Hwt
c9cf0 69 6d 65 28 29 0a 23 64 65 66 69 6e 65 20 54 49  ime().#define TI
c9d00 4d 45 52 5f 45 4e 44 20 20 20 20 20 20 20 20 20  MER_END         
c9d10 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c 69 74 65  g_elapsed=sqlite
c9d20 33 48 77 74 69 6d 65 28 29 2d 67 5f 73 74 61 72  3Hwtime()-g_star
c9d30 74 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  t.#define TIMER_
c9d40 45 4c 41 50 53 45 44 20 20 20 20 20 67 5f 65 6c  ELAPSED     g_el
c9d50 61 70 73 65 64 0a 23 65 6c 73 65 0a 23 64 65 66  apsed.#else.#def
c9d60 69 6e 65 20 54 49 4d 45 52 5f 53 54 41 52 54 0a  ine TIMER_START.
c9d70 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 45 4e  #define TIMER_EN
c9d80 44 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52 5f  D.#define TIMER_
c9d90 45 4c 41 50 53 45 44 20 20 20 20 20 28 28 73 71  ELAPSED     ((sq
c9da0 6c 69 74 65 5f 75 69 6e 74 36 34 29 30 29 0a 23  lite_uint64)0).#
c9db0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66 20  endif../*.** If 
c9dc0 77 65 20 63 6f 6d 70 69 6c 65 20 77 69 74 68 20  we compile with 
c9dd0 74 68 65 20 53 51 4c 49 54 45 5f 54 45 53 54 20  the SQLITE_TEST 
c9de0 6d 61 63 72 6f 20 73 65 74 2c 20 74 68 65 6e 20  macro set, then 
c9df0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 62 6c  the following bl
c9e00 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64 65 20 77  ock.** of code w
c9e10 69 6c 6c 20 67 69 76 65 20 75 73 20 74 68 65 20  ill give us the 
c9e20 61 62 69 6c 69 74 79 20 74 6f 20 73 69 6d 75 6c  ability to simul
c9e30 61 74 65 20 61 20 64 69 73 6b 20 49 2f 4f 20 65  ate a disk I/O e
c9e40 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a 2a 20 69  rror.  This.** i
c9e50 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69  s used for testi
c9e60 6e 67 20 74 68 65 20 49 2f 4f 20 72 65 63 6f 76  ng the I/O recov
c9e70 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f 0a 23 69  ery logic..*/.#i
c9e80 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
c9e90 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
c9ea0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
c9eb0 5f 68 69 74 20 3d 20 30 3b 20 20 20 20 20 20 20  _hit = 0;       
c9ec0 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75       /* Total nu
c9ed0 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45 72 72 6f  mber of I/O Erro
c9ee0 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  rs */.SQLITE_API
c9ef0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f   int sqlite3_io_
c9f00 65 72 72 6f 72 5f 68 61 72 64 68 69 74 20 3d 20  error_hardhit = 
c9f10 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  0;        /* Num
c9f20 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65 6e 69 67  ber of non-benig
c9f30 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49  n errors */.SQLI
c9f40 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
c9f50 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64  e3_io_error_pend
c9f60 69 6e 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  ing = 0;        
c9f70 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e 20 74 6f  /* Count down to
c9f80 20 66 69 72 73 74 20 49 2f 4f 20 65 72 72 6f 72   first I/O error
c9f90 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
c9fa0 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
c9fb0 72 6f 72 5f 70 65 72 73 69 73 74 20 3d 20 30 3b  ror_persist = 0;
c9fc0 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
c9fd0 69 66 20 49 2f 4f 20 65 72 72 6f 72 73 20 70 65  if I/O errors pe
c9fe0 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49 54 45 5f  rsist */.SQLITE_
c9ff0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
ca000 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20  io_error_benign 
ca010 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
ca020 54 72 75 65 20 69 66 20 65 72 72 6f 72 73 20 61  True if errors a
ca030 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a 53 51 4c  re benign */.SQL
ca040 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
ca050 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e  te3_diskfull_pen
ca060 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c 49 54 45  ding = 0;.SQLITE
ca070 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
ca080 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30 3b 0a 23  _diskfull = 0;.#
ca090 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
ca0a0 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58 29 20  OErrorBenign(X) 
ca0b0 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
ca0c0 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23 64 65 66  _benign=(X).#def
ca0d0 69 6e 65 20 53 69 6d 75 6c 61 74 65 49 4f 45 72  ine SimulateIOEr
ca0e0 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a 20 20 69  ror(CODE)  \.  i
ca0f0 66 28 20 28 73 71 6c 69 74 65 33 5f 69 6f 5f 65  f( (sqlite3_io_e
ca100 72 72 6f 72 5f 70 65 72 73 69 73 74 20 26 26 20  rror_persist && 
ca110 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
ca120 5f 68 69 74 29 20 5c 0a 20 20 20 20 20 20 20 7c  _hit) \.       |
ca130 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72  | sqlite3_io_err
ca140 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20 3d 3d 20  or_pending-- == 
ca150 31 20 29 20 20 5c 0a 20 20 20 20 20 20 20 20 20  1 )  \.         
ca160 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f 69 6f 65       { local_ioe
ca170 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d 0a 73 74  rr(); CODE; }.st
ca180 61 74 69 63 20 76 6f 69 64 20 6c 6f 63 61 6c 5f  atic void local_
ca190 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f 54 52 41  ioerr(){.  IOTRA
ca1a0 43 45 28 28 22 49 4f 45 52 52 5c 6e 22 29 29 3b  CE(("IOERR\n"));
ca1b0 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  .  sqlite3_io_er
ca1c0 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20 69 66 28  ror_hit++;.  if(
ca1d0 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72   !sqlite3_io_err
ca1e0 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73 71 6c 69  or_benign ) sqli
ca1f0 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 68 61 72  te3_io_error_har
ca200 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65 66 69 6e  dhit++;.}.#defin
ca210 65 20 53 69 6d 75 6c 61 74 65 44 69 73 6b 66 75  e SimulateDiskfu
ca220 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29 20 5c 0a  llError(CODE) \.
ca230 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 64     if( sqlite3_d
ca240 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69 6e 67 20  iskfull_pending 
ca250 29 7b 20 5c 0a 20 20 20 20 20 69 66 28 20 73 71  ){ \.     if( sq
ca260 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f 70  lite3_diskfull_p
ca270 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29 7b 20 5c  ending == 1 ){ \
ca280 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c 5f 69 6f  .       local_io
ca290 65 72 72 28 29 3b 20 5c 0a 20 20 20 20 20 20 20  err(); \.       
ca2a0 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c  sqlite3_diskfull
ca2b0 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20 20 20 73   = 1; \.       s
ca2c0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
ca2d0 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  hit = 1; \.     
ca2e0 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20 20 20 7d    CODE; \.     }
ca2f0 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20 20 20 73  else{ \.       s
ca300 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
ca310 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a 20 20 20  pending--; \.   
ca320 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65 6c 73 65    } \.   }.#else
ca330 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74  .#define Simulat
ca340 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28 58  eIOErrorBenign(X
ca350 29 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ).#define Simula
ca360 74 65 49 4f 45 72 72 6f 72 28 41 29 0a 23 64 65  teIOError(A).#de
ca370 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
ca380 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29 0a 23 65  kfullError(A).#e
ca390 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 68 65 6e  ndif../*.** When
ca3a0 20 74 65 73 74 69 6e 67 2c 20 6b 65 65 70 20 61   testing, keep a
ca3b0 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e 75   count of the nu
ca3c0 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 66 69 6c  mber of open fil
ca3d0 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  es..*/.#ifdef SQ
ca3e0 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
ca3f0 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
ca400 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f 75 6e 74  _open_file_count
ca410 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65 20 4f 70   = 0;.#define Op
ca420 65 6e 43 6f 75 6e 74 65 72 28 58 29 20 20 73 71  enCounter(X)  sq
ca430 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f  lite3_open_file_
ca440 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65 6c 73 65  count+=(X).#else
ca450 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e 43 6f 75  .#define OpenCou
ca460 6e 74 65 72 28 58 29 0a 23 65 6e 64 69 66 0a 0a  nter(X).#endif..
ca470 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
ca480 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f  ed(_OS_COMMON_H_
ca490 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ) */../*********
ca4a0 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f  ***** End of os_
ca4b0 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a  common.h *******
ca4c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca4d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca4e0 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
ca4f0 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67  ***** Continuing
ca500 20 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f   where we left o
ca510 66 66 20 69 6e 20 6f 73 5f 75 6e 69 78 2e 63 20  ff in os_unix.c 
ca520 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ca530 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 44 65 66  ****/../*.** Def
ca540 69 6e 65 20 76 61 72 69 6f 75 73 20 6d 61 63 72  ine various macr
ca550 6f 73 20 74 68 61 74 20 61 72 65 20 6d 69 73 73  os that are miss
ca560 69 6e 67 20 66 72 6f 6d 20 73 6f 6d 65 20 73 79  ing from some sy
ca570 73 74 65 6d 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65  stems..*/.#ifnde
ca580 66 20 4f 5f 4c 41 52 47 45 46 49 4c 45 0a 23 20  f O_LARGEFILE.# 
ca590 64 65 66 69 6e 65 20 4f 5f 4c 41 52 47 45 46 49  define O_LARGEFI
ca5a0 4c 45 20 30 0a 23 65 6e 64 69 66 0a 23 69 66 64  LE 0.#endif.#ifd
ca5b0 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42 4c  ef SQLITE_DISABL
ca5c0 45 5f 4c 46 53 0a 23 20 75 6e 64 65 66 20 4f 5f  E_LFS.# undef O_
ca5d0 4c 41 52 47 45 46 49 4c 45 0a 23 20 64 65 66 69  LARGEFILE.# defi
ca5e0 6e 65 20 4f 5f 4c 41 52 47 45 46 49 4c 45 20 30  ne O_LARGEFILE 0
ca5f0 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
ca600 4f 5f 4e 4f 46 4f 4c 4c 4f 57 0a 23 20 64 65 66  O_NOFOLLOW.# def
ca610 69 6e 65 20 4f 5f 4e 4f 46 4f 4c 4c 4f 57 20 30  ine O_NOFOLLOW 0
ca620 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
ca630 4f 5f 42 49 4e 41 52 59 0a 23 20 64 65 66 69 6e  O_BINARY.# defin
ca640 65 20 4f 5f 42 49 4e 41 52 59 20 30 0a 23 65 6e  e O_BINARY 0.#en
ca650 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 44  dif../*.** The D
ca660 4a 47 50 50 20 63 6f 6d 70 69 6c 65 72 20 65 6e  JGPP compiler en
ca670 76 69 72 6f 6e 6d 65 6e 74 20 6c 6f 6f 6b 73 20  vironment looks 
ca680 6d 6f 73 74 6c 79 20 6c 69 6b 65 20 55 6e 69 78  mostly like Unix
ca690 2c 20 62 75 74 20 69 74 0a 2a 2a 20 6c 61 63 6b  , but it.** lack
ca6a0 73 20 74 68 65 20 66 63 6e 74 6c 28 29 20 73 79  s the fcntl() sy
ca6b0 73 74 65 6d 20 63 61 6c 6c 2e 20 20 53 6f 20 72  stem call.  So r
ca6c0 65 64 65 66 69 6e 65 20 66 63 6e 74 6c 28 29 20  edefine fcntl() 
ca6d0 74 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 0a  to be something.
ca6e0 2a 2a 20 74 68 61 74 20 61 6c 77 61 79 73 20 73  ** that always s
ca6f0 75 63 63 65 65 64 73 2e 20 20 54 68 69 73 20 6d  ucceeds.  This m
ca700 65 61 6e 73 20 74 68 61 74 20 6c 6f 63 6b 69 6e  eans that lockin
ca710 67 20 64 6f 65 73 20 6e 6f 74 20 6f 63 63 75 72  g does not occur
ca720 20 75 6e 64 65 72 0a 2a 2a 20 44 4a 47 50 50 2e   under.** DJGPP.
ca730 20 20 42 75 74 20 69 74 20 69 73 20 44 4f 53 20    But it is DOS 
ca740 2d 20 77 68 61 74 20 64 69 64 20 79 6f 75 20 65  - what did you e
ca750 78 70 65 63 74 3f 0a 2a 2f 0a 23 69 66 64 65 66  xpect?.*/.#ifdef
ca760 20 5f 5f 44 4a 47 50 50 5f 5f 0a 23 20 64 65 66   __DJGPP__.# def
ca770 69 6e 65 20 66 63 6e 74 6c 28 41 2c 42 2c 43 29  ine fcntl(A,B,C)
ca780 20 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a   0.#endif../*.**
ca790 20 54 68 65 20 74 68 72 65 61 64 69 64 20 6d 61   The threadid ma
ca7a0 63 72 6f 20 72 65 73 6f 6c 76 65 73 20 74 6f 20  cro resolves to 
ca7b0 74 68 65 20 74 68 72 65 61 64 2d 69 64 20 6f 72  the thread-id or
ca7c0 20 74 6f 20 30 2e 20 20 55 73 65 64 20 66 6f 72   to 0.  Used for
ca7d0 0a 2a 2a 20 74 65 73 74 69 6e 67 20 61 6e 64 20  .** testing and 
ca7e0 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a  debugging only..
ca7f0 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  */.#if SQLITE_TH
ca800 52 45 41 44 53 41 46 45 0a 23 64 65 66 69 6e 65  READSAFE.#define
ca810 20 74 68 72 65 61 64 69 64 20 70 74 68 72 65 61   threadid pthrea
ca820 64 5f 73 65 6c 66 28 29 0a 23 65 6c 73 65 0a 23  d_self().#else.#
ca830 64 65 66 69 6e 65 20 74 68 72 65 61 64 69 64 20  define threadid 
ca840 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
ca850 53 65 74 20 6f 72 20 63 68 65 63 6b 20 74 68 65  Set or check the
ca860 20 75 6e 69 78 46 69 6c 65 2e 74 69 64 20 66 69   unixFile.tid fi
ca870 65 6c 64 2e 20 20 54 68 69 73 20 66 69 65 6c 64  eld.  This field
ca880 20 69 73 20 73 65 74 20 77 68 65 6e 20 61 6e 20   is set when an 
ca890 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 69 73 20 66  unixFile.** is f
ca8a0 69 72 73 74 20 6f 70 65 6e 65 64 2e 20 20 41 6c  irst opened.  Al
ca8b0 6c 20 73 75 62 73 65 71 75 65 6e 74 20 75 73 65  l subsequent use
ca8c0 73 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69 6c  s of the unixFil
ca8d0 65 20 76 65 72 69 66 79 20 74 68 61 74 20 74 68  e verify that th
ca8e0 65 0a 2a 2a 20 73 61 6d 65 20 74 68 72 65 61 64  e.** same thread
ca8f0 20 69 73 20 6f 70 65 72 61 74 69 6e 67 20 6f 6e   is operating on
ca900 20 74 68 65 20 75 6e 69 78 46 69 6c 65 2e 20 20   the unixFile.  
ca910 53 6f 6d 65 20 6f 70 65 72 61 74 69 6e 67 20 73  Some operating s
ca920 79 73 74 65 6d 73 20 64 6f 0a 2a 2a 20 6e 6f 74  ystems do.** not
ca930 20 61 6c 6c 6f 77 20 6c 6f 63 6b 73 20 74 6f 20   allow locks to 
ca940 62 65 20 6f 76 65 72 72 69 64 64 65 6e 20 62 79  be overridden by
ca950 20 6f 74 68 65 72 20 74 68 72 65 61 64 73 20 61   other threads a
ca960 6e 64 20 74 68 61 74 20 72 65 73 74 72 69 63 74  nd that restrict
ca970 69 6f 6e 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61  ion.** means tha
ca980 74 20 73 71 6c 69 74 65 33 2a 20 64 61 74 61 62  t sqlite3* datab
ca990 61 73 65 20 68 61 6e 64 6c 65 73 20 63 61 6e 6e  ase handles cann
ca9a0 6f 74 20 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d  ot be moved from
ca9b0 20 6f 6e 65 20 74 68 72 65 61 64 0a 2a 2a 20 74   one thread.** t
ca9c0 6f 20 61 6e 6f 74 68 65 72 2e 20 20 54 68 69 73  o another.  This
ca9d0 20 6c 6f 67 69 63 20 6d 61 6b 65 73 20 73 75 72   logic makes sur
ca9e0 65 20 61 20 75 73 65 72 20 64 6f 65 73 20 6e 6f  e a user does no
ca9f0 74 20 74 72 79 20 74 6f 20 64 6f 20 74 68 61 74  t try to do that
caa00 0a 2a 2a 20 62 79 20 6d 69 73 74 61 6b 65 2e 0a  .** by mistake..
caa10 2a 2a 0a 2a 2a 20 56 65 72 73 69 6f 6e 20 33 2e  **.** Version 3.
caa20 33 2e 31 20 28 32 30 30 36 2d 30 31 2d 31 35 29  3.1 (2006-01-15)
caa30 3a 20 20 75 6e 69 78 46 69 6c 65 20 63 61 6e 20  :  unixFile can 
caa40 62 65 20 6d 6f 76 65 64 20 66 72 6f 6d 20 6f 6e  be moved from on
caa50 65 20 74 68 72 65 61 64 20 74 6f 0a 2a 2a 20 61  e thread to.** a
caa60 6e 6f 74 68 65 72 20 61 73 20 6c 6f 6e 67 20 61  nother as long a
caa70 73 20 77 65 20 61 72 65 20 72 75 6e 6e 69 6e 67  s we are running
caa80 20 6f 6e 20 61 20 73 79 73 74 65 6d 20 74 68 61   on a system tha
caa90 74 20 73 75 70 70 6f 72 74 73 20 74 68 72 65 61  t supports threa
caaa0 64 73 0a 2a 2a 20 6f 76 65 72 72 69 64 69 6e 67  ds.** overriding
caab0 20 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63   each others loc
caac0 6b 73 20 28 77 68 69 63 68 20 6e 6f 77 20 74 68  ks (which now th
caad0 65 20 6d 6f 73 74 20 63 6f 6d 6d 6f 6e 20 62 65  e most common be
caae0 68 61 76 69 6f 72 29 0a 2a 2a 20 6f 72 20 69 66  havior).** or if
caaf0 20 6e 6f 20 6c 6f 63 6b 73 20 61 72 65 20 68 65   no locks are he
cab00 6c 64 2e 20 20 42 75 74 20 74 68 65 20 75 6e 69  ld.  But the uni
cab10 78 46 69 6c 65 2e 70 4c 6f 63 6b 20 66 69 65 6c  xFile.pLock fiel
cab20 64 20 6e 65 65 64 73 20 74 6f 20 62 65 0a 2a 2a  d needs to be.**
cab30 20 72 65 63 6f 6d 70 75 74 65 64 20 62 65 63 61   recomputed beca
cab40 75 73 65 20 69 74 73 20 6b 65 79 20 69 6e 63 6c  use its key incl
cab50 75 64 65 73 20 74 68 65 20 74 68 72 65 61 64 2d  udes the thread-
cab60 69 64 2e 20 20 53 65 65 20 74 68 65 20 0a 2a 2a  id.  See the .**
cab70 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72 73 68   transferOwnersh
cab80 69 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 62 65  ip() function be
cab90 6c 6f 77 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  low for addition
caba0 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a  al information.*
cabb0 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  /.#if SQLITE_THR
cabc0 45 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65  EADSAFE.# define
cabd0 20 53 45 54 5f 54 48 52 45 41 44 49 44 28 58 29   SET_THREADID(X)
cabe0 20 20 20 28 58 29 2d 3e 74 69 64 20 3d 20 70 74     (X)->tid = pt
cabf0 68 72 65 61 64 5f 73 65 6c 66 28 29 0a 23 20 64  hread_self().# d
cac00 65 66 69 6e 65 20 43 48 45 43 4b 5f 54 48 52 45  efine CHECK_THRE
cac10 41 44 49 44 28 58 29 20 28 74 68 72 65 61 64 73  ADID(X) (threads
cac20 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74 68 65  OverrideEachOthe
cac30 72 73 4c 6f 63 6b 73 3d 3d 30 20 26 26 20 5c 0a  rsLocks==0 && \.
cac40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cac50 20 20 20 20 20 20 20 20 20 20 20 20 21 70 74 68              !pth
cac60 72 65 61 64 5f 65 71 75 61 6c 28 28 58 29 2d 3e  read_equal((X)->
cac70 74 69 64 2c 20 70 74 68 72 65 61 64 5f 73 65 6c  tid, pthread_sel
cac80 66 28 29 29 29 0a 23 65 6c 73 65 0a 23 20 64 65  f())).#else.# de
cac90 66 69 6e 65 20 53 45 54 5f 54 48 52 45 41 44 49  fine SET_THREADI
caca0 44 28 58 29 0a 23 20 64 65 66 69 6e 65 20 43 48  D(X).# define CH
cacb0 45 43 4b 5f 54 48 52 45 41 44 49 44 28 58 29 20  ECK_THREADID(X) 
cacc0 30 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  0.#endif../*.** 
cacd0 48 65 72 65 20 69 73 20 74 68 65 20 64 69 72 74  Here is the dirt
cace0 20 6f 6e 20 50 4f 53 49 58 20 61 64 76 69 73 6f   on POSIX adviso
cacf0 72 79 20 6c 6f 63 6b 73 3a 20 20 41 4e 53 49 20  ry locks:  ANSI 
cad00 53 54 44 20 31 30 30 33 2e 31 20 28 31 39 39 36  STD 1003.1 (1996
cad10 29 0a 2a 2a 20 73 65 63 74 69 6f 6e 20 36 2e 35  ).** section 6.5
cad20 2e 32 2e 32 20 6c 69 6e 65 73 20 34 38 33 20 74  .2.2 lines 483 t
cad30 68 72 6f 75 67 68 20 34 39 30 20 73 70 65 63 69  hrough 490 speci
cad40 66 79 20 74 68 61 74 20 77 68 65 6e 20 61 20 70  fy that when a p
cad50 72 6f 63 65 73 73 0a 2a 2a 20 73 65 74 73 20 6f  rocess.** sets o
cad60 72 20 63 6c 65 61 72 73 20 61 20 6c 6f 63 6b 2c  r clears a lock,
cad70 20 74 68 61 74 20 6f 70 65 72 61 74 69 6f 6e 20   that operation 
cad80 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20 70 72  overrides any pr
cad90 69 6f 72 20 6c 6f 63 6b 73 20 73 65 74 0a 2a 2a  ior locks set.**
cada0 20 62 79 20 74 68 65 20 73 61 6d 65 20 70 72 6f   by the same pro
cadb0 63 65 73 73 2e 20 20 49 74 20 64 6f 65 73 20 6e  cess.  It does n
cadc0 6f 74 20 65 78 70 6c 69 63 69 74 6c 79 20 73 61  ot explicitly sa
cadd0 79 20 73 6f 2c 20 62 75 74 20 74 68 69 73 20 69  y so, but this i
cade0 6d 70 6c 69 65 73 0a 2a 2a 20 74 68 61 74 20 69  mplies.** that i
cadf0 74 20 6f 76 65 72 72 69 64 65 73 20 6c 6f 63 6b  t overrides lock
cae00 73 20 73 65 74 20 62 79 20 74 68 65 20 73 61 6d  s set by the sam
cae10 65 20 70 72 6f 63 65 73 73 20 75 73 69 6e 67 20  e process using 
cae20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a 2a 20 66  a different.** f
cae30 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 2e 20  ile descriptor. 
cae40 20 43 6f 6e 73 69 64 65 72 20 74 68 69 73 20 74   Consider this t
cae50 65 73 74 20 63 61 73 65 3a 0a 2a 2a 20 20 20 20  est case:.**    
cae60 20 20 20 69 6e 74 20 66 64 32 20 3d 20 6f 70 65     int fd2 = ope
cae70 6e 28 22 2e 2f 66 69 6c 65 32 22 2c 20 4f 5f 52  n("./file2", O_R
cae80 44 57 52 7c 4f 5f 43 52 45 41 54 2c 20 30 36 34  DWR|O_CREAT, 064
cae90 34 29 3b 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73  4);.**.** Suppos
caea0 65 20 2e 2f 66 69 6c 65 31 20 61 6e 64 20 2e 2f  e ./file1 and ./
caeb0 66 69 6c 65 32 20 61 72 65 20 72 65 61 6c 6c 79  file2 are really
caec0 20 74 68 65 20 73 61 6d 65 20 66 69 6c 65 20 28   the same file (
caed0 62 65 63 61 75 73 65 0a 2a 2a 20 6f 6e 65 20 69  because.** one i
caee0 73 20 61 20 68 61 72 64 20 6f 72 20 73 79 6d 62  s a hard or symb
caef0 6f 6c 69 63 20 6c 69 6e 6b 20 74 6f 20 74 68 65  olic link to the
caf00 20 6f 74 68 65 72 29 20 74 68 65 6e 20 69 66 20   other) then if 
caf10 79 6f 75 20 73 65 74 0a 2a 2a 20 61 6e 20 65 78  you set.** an ex
caf20 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 6f 6e 20  clusive lock on 
caf30 66 64 31 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  fd1, then try to
caf40 20 67 65 74 20 61 6e 20 65 78 63 6c 75 73 69 76   get an exclusiv
caf50 65 20 6c 6f 63 6b 0a 2a 2a 20 6f 6e 20 66 64 32  e lock.** on fd2
caf60 2c 20 69 74 20 77 6f 72 6b 73 2e 20 20 49 20 77  , it works.  I w
caf70 6f 75 6c 64 20 68 61 76 65 20 65 78 70 65 63 74  ould have expect
caf80 65 64 20 74 68 65 20 73 65 63 6f 6e 64 20 6c 6f  ed the second lo
caf90 63 6b 20 74 6f 0a 2a 2a 20 66 61 69 6c 20 73 69  ck to.** fail si
cafa0 6e 63 65 20 74 68 65 72 65 20 77 61 73 20 61 6c  nce there was al
cafb0 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 6e 20  ready a lock on 
cafc0 74 68 65 20 66 69 6c 65 20 64 75 65 20 74 6f 20  the file due to 
cafd0 66 64 31 2e 0a 2a 2a 20 42 75 74 20 6e 6f 74 20  fd1..** But not 
cafe0 73 6f 2e 20 20 53 69 6e 63 65 20 62 6f 74 68 20  so.  Since both 
caff0 6c 6f 63 6b 73 20 63 61 6d 65 20 66 72 6f 6d 20  locks came from 
cb000 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73  the same process
cb010 2c 20 74 68 65 0a 2a 2a 20 73 65 63 6f 6e 64 20  , the.** second 
cb020 6f 76 65 72 72 69 64 65 73 20 74 68 65 20 66 69  overrides the fi
cb030 72 73 74 2c 20 65 76 65 6e 20 74 68 6f 75 67 68  rst, even though
cb040 20 74 68 65 79 20 77 65 72 65 20 6f 6e 20 64 69   they were on di
cb050 66 66 65 72 65 6e 74 0a 2a 2a 20 66 69 6c 65 20  fferent.** file 
cb060 64 65 73 63 72 69 70 74 6f 72 73 20 6f 70 65 6e  descriptors open
cb070 65 64 20 6f 6e 20 64 69 66 66 65 72 65 6e 74 20  ed on different 
cb080 66 69 6c 65 20 6e 61 6d 65 73 2e 0a 2a 2a 0a 2a  file names..**.*
cb090 2a 20 42 75 6d 6d 65 72 2e 20 20 49 66 20 79 6f  * Bummer.  If yo
cb0a0 75 20 61 73 6b 20 6d 65 2c 20 74 68 69 73 20 69  u ask me, this i
cb0b0 73 20 62 72 6f 6b 65 6e 2e 20 20 42 61 64 6c 79  s broken.  Badly
cb0c0 20 62 72 6f 6b 65 6e 2e 20 20 49 74 20 6d 65 61   broken.  It mea
cb0d0 6e 73 0a 2a 2a 20 74 68 61 74 20 77 65 20 63 61  ns.** that we ca
cb0e0 6e 6e 6f 74 20 75 73 65 20 50 4f 53 49 58 20 6c  nnot use POSIX l
cb0f0 6f 63 6b 73 20 74 6f 20 73 79 6e 63 68 72 6f 6e  ocks to synchron
cb100 69 7a 65 20 66 69 6c 65 20 61 63 63 65 73 73 20  ize file access 
cb110 61 6d 6f 6e 67 0a 2a 2a 20 63 6f 6d 70 65 74 69  among.** competi
cb120 6e 67 20 74 68 72 65 61 64 73 20 6f 66 20 74 68  ng threads of th
cb130 65 20 73 61 6d 65 20 70 72 6f 63 65 73 73 2e 20  e same process. 
cb140 20 50 4f 53 49 58 20 6c 6f 63 6b 73 20 77 69 6c   POSIX locks wil
cb150 6c 20 77 6f 72 6b 20 66 69 6e 65 0a 2a 2a 20 74  l work fine.** t
cb160 6f 20 73 79 6e 63 68 72 6f 6e 69 7a 65 20 61 63  o synchronize ac
cb170 63 65 73 73 20 66 6f 72 20 74 68 72 65 61 64 73  cess for threads
cb180 20 69 6e 20 73 65 70 61 72 61 74 65 20 70 72 6f   in separate pro
cb190 63 65 73 73 65 73 2c 20 62 75 74 20 6e 6f 74 0a  cesses, but not.
cb1a0 2a 2a 20 74 68 72 65 61 64 73 20 77 69 74 68 69  ** threads withi
cb1b0 6e 20 74 68 65 20 73 61 6d 65 20 70 72 6f 63 65  n the same proce
cb1c0 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 77 6f 72  ss..**.** To wor
cb1d0 6b 20 61 72 6f 75 6e 64 20 74 68 65 20 70 72 6f  k around the pro
cb1e0 62 6c 65 6d 2c 20 53 51 4c 69 74 65 20 68 61 73  blem, SQLite has
cb1f0 20 74 6f 20 6d 61 6e 61 67 65 20 66 69 6c 65 20   to manage file 
cb200 6c 6f 63 6b 73 20 69 6e 74 65 72 6e 61 6c 6c 79  locks internally
cb210 0a 2a 2a 20 6f 6e 20 69 74 73 20 6f 77 6e 2e 20  .** on its own. 
cb220 20 57 68 65 6e 65 76 65 72 20 61 20 6e 65 77 20   Whenever a new 
cb230 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65 6e  database is open
cb240 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f 20 66  ed, we have to f
cb250 69 6e 64 20 74 68 65 0a 2a 2a 20 73 70 65 63 69  ind the.** speci
cb260 66 69 63 20 69 6e 6f 64 65 20 6f 66 20 74 68 65  fic inode of the
cb270 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 28   database file (
cb280 74 68 65 20 69 6e 6f 64 65 20 69 73 20 64 65 74  the inode is det
cb290 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 0a 2a  ermined by the.*
cb2a0 2a 20 73 74 5f 64 65 76 20 61 6e 64 20 73 74 5f  * st_dev and st_
cb2b0 69 6e 6f 20 66 69 65 6c 64 73 20 6f 66 20 74 68  ino fields of th
cb2c0 65 20 73 74 61 74 20 73 74 72 75 63 74 75 72 65  e stat structure
cb2d0 20 74 68 61 74 20 66 73 74 61 74 28 29 20 66 69   that fstat() fi
cb2e0 6c 6c 73 20 69 6e 29 0a 2a 2a 20 61 6e 64 20 63  lls in).** and c
cb2f0 68 65 63 6b 20 66 6f 72 20 6c 6f 63 6b 73 20 61  heck for locks a
cb300 6c 72 65 61 64 79 20 65 78 69 73 74 69 6e 67 20  lready existing 
cb310 6f 6e 20 74 68 61 74 20 69 6e 6f 64 65 2e 20 20  on that inode.  
cb320 57 68 65 6e 20 6c 6f 63 6b 73 20 61 72 65 0a 2a  When locks are.*
cb330 2a 20 63 72 65 61 74 65 64 20 6f 72 20 72 65 6d  * created or rem
cb340 6f 76 65 64 2c 20 77 65 20 68 61 76 65 20 74 6f  oved, we have to
cb350 20 6c 6f 6f 6b 20 61 74 20 6f 75 72 20 6f 77 6e   look at our own
cb360 20 69 6e 74 65 72 6e 61 6c 20 72 65 63 6f 72 64   internal record
cb370 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 6f 63 6b 73   of the.** locks
cb380 20 74 6f 20 73 65 65 20 69 66 20 61 6e 6f 74 68   to see if anoth
cb390 65 72 20 74 68 72 65 61 64 20 68 61 73 20 70 72  er thread has pr
cb3a0 65 76 69 6f 75 73 6c 79 20 73 65 74 20 61 20 6c  eviously set a l
cb3b0 6f 63 6b 20 6f 6e 20 74 68 61 74 20 73 61 6d 65  ock on that same
cb3c0 0a 2a 2a 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a  .** inode..**.**
cb3d0 20 54 68 65 20 73 71 6c 69 74 65 33 5f 66 69 6c   The sqlite3_fil
cb3e0 65 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20  e structure for 
cb3f0 50 4f 53 49 58 20 69 73 20 6e 6f 20 6c 6f 6e 67  POSIX is no long
cb400 65 72 20 6a 75 73 74 20 61 6e 20 69 6e 74 65 67  er just an integ
cb410 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73 63 72  er file.** descr
cb420 69 70 74 6f 72 2e 20 20 49 74 20 69 73 20 6e 6f  iptor.  It is no
cb430 77 20 61 20 73 74 72 75 63 74 75 72 65 20 74 68  w a structure th
cb440 61 74 20 68 6f 6c 64 73 20 74 68 65 20 69 6e 74  at holds the int
cb450 65 67 65 72 20 66 69 6c 65 0a 2a 2a 20 64 65 73  eger file.** des
cb460 63 72 69 70 74 6f 72 20 61 6e 64 20 61 20 70 6f  criptor and a po
cb470 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72 75 63  inter to a struc
cb480 74 75 72 65 20 74 68 61 74 20 64 65 73 63 72 69  ture that descri
cb490 62 65 73 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  bes the internal
cb4a0 0a 2a 2a 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 65  .** locks on the
cb4b0 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 69   corresponding i
cb4c0 6e 6f 64 65 2e 20 20 54 68 65 72 65 20 69 73 20  node.  There is 
cb4d0 6f 6e 65 20 6c 6f 63 6b 69 6e 67 20 73 74 72 75  one locking stru
cb4e0 63 74 75 72 65 0a 2a 2a 20 70 65 72 20 69 6e 6f  cture.** per ino
cb4f0 64 65 2c 20 73 6f 20 69 66 20 74 68 65 20 73 61  de, so if the sa
cb500 6d 65 20 69 6e 6f 64 65 20 69 73 20 6f 70 65 6e  me inode is open
cb510 65 64 20 74 77 69 63 65 2c 20 62 6f 74 68 20 75  ed twice, both u
cb520 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75 72  nixFile structur
cb530 65 73 0a 2a 2a 20 70 6f 69 6e 74 20 74 6f 20 74  es.** point to t
cb540 68 65 20 73 61 6d 65 20 6c 6f 63 6b 69 6e 67 20  he same locking 
cb550 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65 20  structure.  The 
cb560 6c 6f 63 6b 69 6e 67 20 73 74 72 75 63 74 75 72  locking structur
cb570 65 20 6b 65 65 70 73 0a 2a 2a 20 61 20 72 65 66  e keeps.** a ref
cb580 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 28 73 6f  erence count (so
cb590 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20 77 68   we will know wh
cb5a0 65 6e 20 74 6f 20 64 65 6c 65 74 65 20 69 74 29  en to delete it)
cb5b0 20 61 6e 64 20 61 20 22 63 6e 74 22 0a 2a 2a 20   and a "cnt".** 
cb5c0 66 69 65 6c 64 20 74 68 61 74 20 74 65 6c 6c 73  field that tells
cb5d0 20 75 73 20 69 74 73 20 69 6e 74 65 72 6e 61 6c   us its internal
cb5e0 20 6c 6f 63 6b 20 73 74 61 74 75 73 2e 20 20 63   lock status.  c
cb5f0 6e 74 3d 3d 30 20 6d 65 61 6e 73 20 74 68 65 0a  nt==0 means the.
cb600 2a 2a 20 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63  ** file is unloc
cb610 6b 65 64 2e 20 20 63 6e 74 3d 3d 2d 31 20 6d 65  ked.  cnt==-1 me
cb620 61 6e 73 20 74 68 65 20 66 69 6c 65 20 68 61 73  ans the file has
cb630 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
cb640 63 6b 2e 0a 2a 2a 20 63 6e 74 3e 30 20 6d 65 61  ck..** cnt>0 mea
cb650 6e 73 20 74 68 65 72 65 20 61 72 65 20 63 6e 74  ns there are cnt
cb660 20 73 68 61 72 65 64 20 6c 6f 63 6b 73 20 6f 6e   shared locks on
cb670 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a   the file..**.**
cb680 20 41 6e 79 20 61 74 74 65 6d 70 74 20 74 6f 20   Any attempt to 
cb690 6c 6f 63 6b 20 6f 72 20 75 6e 6c 6f 63 6b 20 61  lock or unlock a
cb6a0 20 66 69 6c 65 20 66 69 72 73 74 20 63 68 65 63   file first chec
cb6b0 6b 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 0a 2a  ks the locking.*
cb6c0 2a 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68  * structure.  Th
cb6d0 65 20 66 63 6e 74 6c 28 29 20 73 79 73 74 65 6d  e fcntl() system
cb6e0 20 63 61 6c 6c 20 69 73 20 6f 6e 6c 79 20 69 6e   call is only in
cb6f0 76 6f 6b 65 64 20 74 6f 20 73 65 74 20 61 20 0a  voked to set a .
cb700 2a 2a 20 50 4f 53 49 58 20 6c 6f 63 6b 20 69 66  ** POSIX lock if
cb710 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 6c 6f   the internal lo
cb720 63 6b 20 73 74 72 75 63 74 75 72 65 20 74 72 61  ck structure tra
cb730 6e 73 69 74 69 6f 6e 73 20 62 65 74 77 65 65 6e  nsitions between
cb740 0a 2a 2a 20 61 20 6c 6f 63 6b 65 64 20 61 6e 64  .** a locked and
cb750 20 61 6e 20 75 6e 6c 6f 63 6b 65 64 20 73 74 61   an unlocked sta
cb760 74 65 2e 0a 2a 2a 0a 2a 2a 20 32 30 30 34 2d 4a  te..**.** 2004-J
cb770 61 6e 2d 31 31 3a 0a 2a 2a 20 4d 6f 72 65 20 72  an-11:.** More r
cb780 65 63 65 6e 74 20 64 69 73 63 6f 76 65 72 69 65  ecent discoverie
cb790 73 20 61 62 6f 75 74 20 50 4f 53 49 58 20 61 64  s about POSIX ad
cb7a0 76 69 73 6f 72 79 20 6c 6f 63 6b 73 2e 20 20 28  visory locks.  (
cb7b0 54 68 65 20 6d 6f 72 65 0a 2a 2a 20 49 20 64 69  The more.** I di
cb7c0 73 63 6f 76 65 72 2c 20 74 68 65 20 6d 6f 72 65  scover, the more
cb7d0 20 49 20 72 65 61 6c 69 7a 65 20 74 68 65 20 61   I realize the a
cb7e0 20 50 4f 53 49 58 20 61 64 76 69 73 6f 72 79 20   POSIX advisory 
cb7f0 6c 6f 63 6b 73 20 61 72 65 0a 2a 2a 20 61 6e 20  locks are.** an 
cb800 61 62 6f 6d 69 6e 61 74 69 6f 6e 2e 29 0a 2a 2a  abomination.).**
cb810 0a 2a 2a 20 49 66 20 79 6f 75 20 63 6c 6f 73 65  .** If you close
cb820 20 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74   a file descript
cb830 6f 72 20 74 68 61 74 20 70 6f 69 6e 74 73 20 74  or that points t
cb840 6f 20 61 20 66 69 6c 65 20 74 68 61 74 20 68 61  o a file that ha
cb850 73 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 61 6c 6c 20  s locks,.** all 
cb860 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74 20 66 69  locks on that fi
cb870 6c 65 20 74 68 61 74 20 61 72 65 20 6f 77 6e 65  le that are owne
cb880 64 20 62 79 20 74 68 65 20 63 75 72 72 65 6e 74  d by the current
cb890 20 70 72 6f 63 65 73 73 20 61 72 65 0a 2a 2a 20   process are.** 
cb8a0 72 65 6c 65 61 73 65 64 2e 20 20 54 6f 20 77 6f  released.  To wo
cb8b0 72 6b 20 61 72 6f 75 6e 64 20 74 68 69 73 20 70  rk around this p
cb8c0 72 6f 62 6c 65 6d 2c 20 65 61 63 68 20 75 6e 69  roblem, each uni
cb8d0 78 46 69 6c 65 20 73 74 72 75 63 74 75 72 65 20  xFile structure 
cb8e0 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f  contains.** a po
cb8f0 69 6e 74 65 72 20 74 6f 20 61 6e 20 6f 70 65 6e  inter to an open
cb900 43 6e 74 20 73 74 72 75 63 74 75 72 65 2e 20 20  Cnt structure.  
cb910 54 68 65 72 65 20 69 73 20 6f 6e 65 20 6f 70 65  There is one ope
cb920 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 0a 2a  nCnt structure.*
cb930 2a 20 70 65 72 20 6f 70 65 6e 20 69 6e 6f 64 65  * per open inode
cb940 2c 20 77 68 69 63 68 20 6d 65 61 6e 73 20 74 68  , which means th
cb950 61 74 20 6d 75 6c 74 69 70 6c 65 20 75 6e 69 78  at multiple unix
cb960 46 69 6c 65 20 63 61 6e 20 70 6f 69 6e 74 20 74  File can point t
cb970 6f 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6f 70  o a single.** op
cb980 65 6e 43 6e 74 2e 20 20 57 68 65 6e 20 61 6e 20  enCnt.  When an 
cb990 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20  attempt is made 
cb9a0 74 6f 20 63 6c 6f 73 65 20 61 6e 20 75 6e 69 78  to close an unix
cb9b0 46 69 6c 65 2c 20 69 66 20 74 68 65 72 65 20 61  File, if there a
cb9c0 72 65 0a 2a 2a 20 6f 74 68 65 72 20 75 6e 69 78  re.** other unix
cb9d0 46 69 6c 65 20 6f 70 65 6e 20 6f 6e 20 74 68 65  File open on the
cb9e0 20 73 61 6d 65 20 69 6e 6f 64 65 20 74 68 61 74   same inode that
cb9f0 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 6c 6f 63   are holding loc
cba00 6b 73 2c 20 74 68 65 20 63 61 6c 6c 0a 2a 2a 20  ks, the call.** 
cba10 74 6f 20 63 6c 6f 73 65 28 29 20 74 68 65 20 66  to close() the f
cba20 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
cba30 73 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c  s deferred until
cba40 20 61 6c 6c 20 6f 66 20 74 68 65 20 6c 6f 63 6b   all of the lock
cba50 73 20 63 6c 65 61 72 2e 0a 2a 2a 20 54 68 65 20  s clear..** The 
cba60 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  openCnt structur
cba70 65 20 6b 65 65 70 73 20 61 20 6c 69 73 74 20 6f  e keeps a list o
cba80 66 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  f file descripto
cba90 72 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 0a  rs that need to.
cbaa0 2a 2a 20 62 65 20 63 6c 6f 73 65 64 20 61 6e 64  ** be closed and
cbab0 20 74 68 61 74 20 6c 69 73 74 20 69 73 20 77 61   that list is wa
cbac0 6c 6b 65 64 20 28 61 6e 64 20 63 6c 65 61 72 65  lked (and cleare
cbad0 64 29 20 77 68 65 6e 20 74 68 65 20 6c 61 73 74  d) when the last
cbae0 20 6c 6f 63 6b 0a 2a 2a 20 63 6c 65 61 72 73 2e   lock.** clears.
cbaf0 0a 2a 2a 0a 2a 2a 20 46 69 72 73 74 2c 20 75 6e  .**.** First, un
cbb00 64 65 72 20 4c 69 6e 75 78 20 74 68 72 65 61 64  der Linux thread
cbb10 73 2c 20 62 65 63 61 75 73 65 20 65 61 63 68 20  s, because each 
cbb20 74 68 72 65 61 64 20 68 61 73 20 61 20 73 65 70  thread has a sep
cbb30 61 72 61 74 65 0a 2a 2a 20 70 72 6f 63 65 73 73  arate.** process
cbb40 20 49 44 2c 20 6c 6f 63 6b 20 6f 70 65 72 61 74   ID, lock operat
cbb50 69 6f 6e 73 20 69 6e 20 6f 6e 65 20 74 68 72 65  ions in one thre
cbb60 61 64 20 64 6f 20 6e 6f 74 20 6f 76 65 72 72 69  ad do not overri
cbb70 64 65 20 6c 6f 63 6b 73 0a 2a 2a 20 74 6f 20 74  de locks.** to t
cbb80 68 65 20 73 61 6d 65 20 66 69 6c 65 20 69 6e 20  he same file in 
cbb90 6f 74 68 65 72 20 74 68 72 65 61 64 73 2e 20 20  other threads.  
cbba0 4c 69 6e 75 78 20 74 68 72 65 61 64 73 20 62 65  Linux threads be
cbbb0 68 61 76 65 20 6c 69 6b 65 0a 2a 2a 20 73 65 70  have like.** sep
cbbc0 61 72 61 74 65 20 70 72 6f 63 65 73 73 65 73 20  arate processes 
cbbd0 69 6e 20 74 68 69 73 20 72 65 73 70 65 63 74 2e  in this respect.
cbbe0 20 20 42 75 74 2c 20 69 66 20 79 6f 75 20 63 6c    But, if you cl
cbbf0 6f 73 65 20 61 20 66 69 6c 65 0a 2a 2a 20 64 65  ose a file.** de
cbc00 73 63 72 69 70 74 6f 72 20 69 6e 20 6c 69 6e 75  scriptor in linu
cbc10 78 20 74 68 72 65 61 64 73 2c 20 61 6c 6c 20 6c  x threads, all l
cbc20 6f 63 6b 73 20 61 72 65 20 63 6c 65 61 72 65 64  ocks are cleared
cbc30 2c 20 65 76 65 6e 20 6c 6f 63 6b 73 0a 2a 2a 20  , even locks.** 
cbc40 6f 6e 20 6f 74 68 65 72 20 74 68 72 65 61 64 73  on other threads
cbc50 20 61 6e 64 20 65 76 65 6e 20 74 68 6f 75 67 68   and even though
cbc60 20 74 68 65 20 6f 74 68 65 72 20 74 68 72 65 61   the other threa
cbc70 64 73 20 68 61 76 65 20 64 69 66 66 65 72 65 6e  ds have differen
cbc80 74 0a 2a 2a 20 70 72 6f 63 65 73 73 20 49 44 73  t.** process IDs
cbc90 2e 20 20 4c 69 6e 75 78 20 74 68 72 65 61 64 73  .  Linux threads
cbca0 20 69 73 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74   is inconsistent
cbcb0 20 69 6e 20 74 68 69 73 20 72 65 73 70 65 63 74   in this respect
cbcc0 2e 0a 2a 2a 20 28 49 27 6d 20 62 65 67 69 6e 6e  ..** (I'm beginn
cbcd0 69 6e 67 20 74 6f 20 74 68 69 6e 6b 20 74 68 61  ing to think tha
cbce0 74 20 6c 69 6e 75 78 20 74 68 72 65 61 64 73 20  t linux threads 
cbcf0 69 73 20 61 6e 20 61 62 6f 6d 69 6e 61 74 69 6f  is an abominatio
cbd00 6e 20 74 6f 6f 2e 29 0a 2a 2a 20 54 68 65 20 63  n too.).** The c
cbd10 6f 6e 73 65 71 75 65 6e 63 65 20 6f 66 20 74 68  onsequence of th
cbd20 69 73 20 61 6c 6c 20 69 73 20 74 68 61 74 20 74  is all is that t
cbd30 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 66 6f  he hash table fo
cbd40 72 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f 0a 2a  r the lockInfo.*
cbd50 2a 20 73 74 72 75 63 74 75 72 65 20 68 61 73 20  * structure has 
cbd60 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 65 20 70  to include the p
cbd70 72 6f 63 65 73 73 20 69 64 20 61 73 20 70 61 72  rocess id as par
cbd80 74 20 6f 66 20 69 74 73 20 6b 65 79 20 62 65 63  t of its key bec
cbd90 61 75 73 65 0a 2a 2a 20 6c 6f 63 6b 73 20 69 6e  ause.** locks in
cbda0 20 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61   different threa
cbdb0 64 73 20 61 72 65 20 74 72 65 61 74 65 64 20 61  ds are treated a
cbdc0 73 20 64 69 73 74 69 6e 63 74 2e 20 20 42 75 74  s distinct.  But
cbdd0 20 74 68 65 20 0a 2a 2a 20 6f 70 65 6e 43 6e 74   the .** openCnt
cbde0 20 73 74 72 75 63 74 75 72 65 20 73 68 6f 75 6c   structure shoul
cbdf0 64 20 6e 6f 74 20 69 6e 63 6c 75 64 65 20 74 68  d not include th
cbe00 65 20 70 72 6f 63 65 73 73 20 69 64 20 69 6e 20  e process id in 
cbe10 69 74 73 0a 2a 2a 20 6b 65 79 20 62 65 63 61 75  its.** key becau
cbe20 73 65 20 63 6c 6f 73 65 28 29 20 63 6c 65 61 72  se close() clear
cbe30 73 20 6c 6f 63 6b 20 6f 6e 20 61 6c 6c 20 74 68  s lock on all th
cbe40 72 65 61 64 73 2c 20 6e 6f 74 20 6a 75 73 74 20  reads, not just 
cbe50 74 68 65 20 63 75 72 72 65 6e 74 0a 2a 2a 20 74  the current.** t
cbe60 68 72 65 61 64 2e 20 20 57 65 72 65 20 69 74 20  hread.  Were it 
cbe70 6e 6f 74 20 66 6f 72 20 74 68 69 73 20 67 6f 6f  not for this goo
cbe80 66 69 6e 65 73 73 20 69 6e 20 6c 69 6e 75 78 20  finess in linux 
cbe90 74 68 72 65 61 64 73 2c 20 77 65 20 63 6f 75 6c  threads, we coul
cbea0 64 0a 2a 2a 20 63 6f 6d 62 69 6e 65 20 74 68 65  d.** combine the
cbeb0 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70   lockInfo and op
cbec0 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72 65 73  enCnt structures
cbed0 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 73   into a single s
cbee0 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
cbef0 32 30 30 34 2d 4a 75 6e 2d 32 38 3a 0a 2a 2a 20  2004-Jun-28:.** 
cbf00 4f 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73  On some versions
cbf10 20 6f 66 20 6c 69 6e 75 78 2c 20 74 68 72 65 61   of linux, threa
cbf20 64 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20  ds can override 
cbf30 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
cbf40 73 2e 0a 2a 2a 20 4f 6e 20 6f 74 68 65 72 73 20  s..** On others 
cbf50 6e 6f 74 2e 20 20 53 6f 6d 65 74 69 6d 65 73 20  not.  Sometimes 
cbf60 79 6f 75 20 63 61 6e 20 63 68 61 6e 67 65 20 74  you can change t
cbf70 68 65 20 62 65 68 61 76 69 6f 72 20 6f 6e 20 74  he behavior on t
cbf80 68 65 20 73 61 6d 65 0a 2a 2a 20 73 79 73 74 65  he same.** syste
cbf90 6d 20 62 79 20 73 65 74 74 69 6e 67 20 74 68 65  m by setting the
cbfa0 20 4c 44 5f 41 53 53 55 4d 45 5f 4b 45 52 4e 45   LD_ASSUME_KERNE
cbfb0 4c 20 65 6e 76 69 72 6f 6e 6d 65 6e 74 20 76 61  L environment va
cbfc0 72 69 61 62 6c 65 2e 20 20 54 68 65 0a 2a 2a 20  riable.  The.** 
cbfd0 50 4f 53 49 58 20 73 74 61 6e 64 61 72 64 20 69  POSIX standard i
cbfe0 73 20 73 69 6c 65 6e 74 20 61 73 20 74 6f 20 77  s silent as to w
cbff0 68 69 63 68 20 62 65 68 61 76 69 6f 72 20 69 73  hich behavior is
cc000 20 63 6f 72 72 65 63 74 2c 20 61 73 20 66 61 72   correct, as far
cc010 0a 2a 2a 20 61 73 20 49 20 63 61 6e 20 74 65 6c  .** as I can tel
cc020 6c 2c 20 73 6f 20 6f 74 68 65 72 20 76 65 72 73  l, so other vers
cc030 69 6f 6e 73 20 6f 66 20 75 6e 69 78 20 6d 69 67  ions of unix mig
cc040 68 74 20 73 68 6f 77 20 74 68 65 20 73 61 6d 65  ht show the same
cc050 0a 2a 2a 20 69 6e 63 6f 6e 73 69 73 74 65 6e 63  .** inconsistenc
cc060 79 2e 20 20 54 68 65 72 65 20 69 73 20 6e 6f 20  y.  There is no 
cc070 6c 69 74 74 6c 65 20 64 6f 75 62 74 20 69 6e 20  little doubt in 
cc080 6d 79 20 6d 69 6e 64 20 74 68 61 74 20 70 6f 73  my mind that pos
cc090 69 78 0a 2a 2a 20 61 64 76 69 73 6f 72 79 20 6c  ix.** advisory l
cc0a0 6f 63 6b 73 20 61 6e 64 20 6c 69 6e 75 78 20 74  ocks and linux t
cc0b0 68 72 65 61 64 73 20 61 72 65 20 70 72 6f 66 6f  hreads are profo
cc0c0 75 6e 64 6c 79 20 62 72 6f 6b 65 6e 2e 0a 2a 2a  undly broken..**
cc0d0 0a 2a 2a 20 54 6f 20 77 6f 72 6b 20 61 72 6f 75  .** To work arou
cc0e0 6e 64 20 74 68 65 20 69 6e 63 6f 6e 73 69 73 74  nd the inconsist
cc0f0 65 6e 63 69 65 73 2c 20 77 65 20 68 61 76 65 20  encies, we have 
cc100 74 6f 20 74 65 73 74 20 61 74 20 72 75 6e 74 69  to test at runti
cc110 6d 65 20 0a 2a 2a 20 77 68 65 74 68 65 72 20 6f  me .** whether o
cc120 72 20 6e 6f 74 20 74 68 72 65 61 64 73 20 63 61  r not threads ca
cc130 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
cc140 6f 74 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 54  others locks.  T
cc150 68 69 73 20 74 65 73 74 0a 2a 2a 20 69 73 20 72  his test.** is r
cc160 75 6e 20 6f 6e 63 65 2c 20 74 68 65 20 66 69 72  un once, the fir
cc170 73 74 20 74 69 6d 65 20 61 6e 79 20 6c 6f 63 6b  st time any lock
cc180 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20 20   is attempted.  
cc190 41 20 73 74 61 74 69 63 20 0a 2a 2a 20 76 61 72  A static .** var
cc1a0 69 61 62 6c 65 20 69 73 20 73 65 74 20 74 6f 20  iable is set to 
cc1b0 72 65 63 6f 72 64 20 74 68 65 20 72 65 73 75 6c  record the resul
cc1c0 74 73 20 6f 66 20 74 68 69 73 20 74 65 73 74 20  ts of this test 
cc1d0 66 6f 72 20 66 75 74 75 72 65 0a 2a 2a 20 75 73  for future.** us
cc1e0 65 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20  e..*/../*.** An 
cc1f0 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
cc200 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74  following struct
cc210 75 72 65 20 73 65 72 76 65 73 20 61 73 20 74 68  ure serves as th
cc220 65 20 6b 65 79 20 75 73 65 64 0a 2a 2a 20 74 6f  e key used.** to
cc230 20 6c 6f 63 61 74 65 20 61 20 70 61 72 74 69 63   locate a partic
cc240 75 6c 61 72 20 6c 6f 63 6b 49 6e 66 6f 20 73 74  ular lockInfo st
cc250 72 75 63 74 75 72 65 20 67 69 76 65 6e 20 69 74  ructure given it
cc260 73 20 69 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 49  s inode..**.** I
cc270 66 20 74 68 72 65 61 64 73 20 63 61 6e 6e 6f 74  f threads cannot
cc280 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20 6f   override each o
cc290 74 68 65 72 73 20 6c 6f 63 6b 73 2c 20 74 68 65  thers locks, the
cc2a0 6e 20 77 65 20 73 65 74 20 74 68 65 0a 2a 2a 20  n we set the.** 
cc2b0 6c 6f 63 6b 4b 65 79 2e 74 69 64 20 66 69 65 6c  lockKey.tid fiel
cc2c0 64 20 74 6f 20 74 68 65 20 74 68 72 65 61 64 20  d to the thread 
cc2d0 49 44 2e 20 20 49 66 20 74 68 72 65 61 64 73 20  ID.  If threads 
cc2e0 63 61 6e 20 6f 76 65 72 72 69 64 65 0a 2a 2a 20  can override.** 
cc2f0 65 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b  each others lock
cc300 73 20 74 68 65 6e 20 74 69 64 20 69 73 20 61 6c  s then tid is al
cc310 77 61 79 73 20 73 65 74 20 74 6f 20 7a 65 72 6f  ways set to zero
cc320 2e 20 20 74 69 64 20 69 73 20 6f 6d 69 74 74 65  .  tid is omitte
cc330 64 0a 2a 2a 20 69 66 20 77 65 20 63 6f 6d 70 69  d.** if we compi
cc340 6c 65 20 77 69 74 68 6f 75 74 20 74 68 72 65 61  le without threa
cc350 64 69 6e 67 20 73 75 70 70 6f 72 74 2e 0a 2a 2f  ding support..*/
cc360 0a 73 74 72 75 63 74 20 6c 6f 63 6b 4b 65 79 20  .struct lockKey 
cc370 7b 0a 20 20 64 65 76 5f 74 20 64 65 76 3b 20 20  {.  dev_t dev;  
cc380 20 20 20 20 20 2f 2a 20 44 65 76 69 63 65 20 6e       /* Device n
cc390 75 6d 62 65 72 20 2a 2f 0a 23 69 66 20 49 53 5f  umber */.#if IS_
cc3a0 56 58 57 4f 52 4b 53 0a 20 20 76 6f 69 64 20 2a  VXWORKS.  void *
cc3b0 72 6e 61 6d 3b 20 20 20 20 20 20 2f 2a 20 52 65  rnam;      /* Re
cc3c0 61 6c 6e 61 6d 65 20 73 69 6e 63 65 20 69 6e 6f  alname since ino
cc3d0 64 65 20 75 6e 75 73 61 62 6c 65 20 2a 2f 0a 23  de unusable */.#
cc3e0 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69 6e 6f  else.  ino_t ino
cc3f0 3b 20 20 20 20 20 20 20 2f 2a 20 49 6e 6f 64 65  ;       /* Inode
cc400 20 6e 75 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69   number */.#endi
cc410 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52  f.#if SQLITE_THR
cc420 45 41 44 53 41 46 45 0a 20 20 70 74 68 72 65 61  EADSAFE.  pthrea
cc430 64 5f 74 20 74 69 64 3b 20 20 20 2f 2a 20 54 68  d_t tid;   /* Th
cc440 72 65 61 64 20 49 44 20 6f 72 20 7a 65 72 6f 20  read ID or zero 
cc450 69 66 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f  if threads can o
cc460 76 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68  verride each oth
cc470 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a 7d 3b 0a  er */.#endif.};.
cc480 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
cc490 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
cc4a0 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 69 73  ing structure is
cc4b0 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72 20 65   allocated for e
cc4c0 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69 6e 6f 64  ach open.** inod
cc4d0 65 20 6f 6e 20 65 61 63 68 20 74 68 72 65 61 64  e on each thread
cc4e0 20 77 69 74 68 20 61 20 64 69 66 66 65 72 65 6e   with a differen
cc4f0 74 20 70 72 6f 63 65 73 73 20 49 44 2e 20 20 28  t process ID.  (
cc500 54 68 72 65 61 64 73 20 68 61 76 65 0a 2a 2a 20  Threads have.** 
cc510 64 69 66 66 65 72 65 6e 74 20 70 72 6f 63 65 73  different proces
cc520 73 20 49 44 73 20 6f 6e 20 6c 69 6e 75 78 2c 20  s IDs on linux, 
cc530 62 75 74 20 6e 6f 74 20 6f 6e 20 6d 6f 73 74 20  but not on most 
cc540 6f 74 68 65 72 20 75 6e 69 78 65 73 2e 29 0a 2a  other unixes.).*
cc550 2a 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 69 6e  *.** A single in
cc560 6f 64 65 20 63 61 6e 20 68 61 76 65 20 6d 75 6c  ode can have mul
cc570 74 69 70 6c 65 20 66 69 6c 65 20 64 65 73 63 72  tiple file descr
cc580 69 70 74 6f 72 73 2c 20 73 6f 20 65 61 63 68 20  iptors, so each 
cc590 75 6e 69 78 46 69 6c 65 0a 2a 2a 20 73 74 72 75  unixFile.** stru
cc5a0 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61  cture contains a
cc5b0 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20 69   pointer to an i
cc5c0 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20  nstance of this 
cc5d0 6f 62 6a 65 63 74 20 61 6e 64 20 74 68 69 73 0a  object and this.
cc5e0 2a 2a 20 6f 62 6a 65 63 74 20 6b 65 65 70 73 20  ** object keeps 
cc5f0 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 6e  a count of the n
cc600 75 6d 62 65 72 20 6f 66 20 75 6e 69 78 46 69 6c  umber of unixFil
cc610 65 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69 74  e pointing to it
cc620 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 6c 6f 63 6b  ..*/.struct lock
cc630 49 6e 66 6f 20 7b 0a 20 20 73 74 72 75 63 74 20  Info {.  struct 
cc640 6c 6f 63 6b 4b 65 79 20 6b 65 79 3b 20 20 2f 2a  lockKey key;  /*
cc650 20 54 68 65 20 6c 6f 6f 6b 75 70 20 6b 65 79 20   The lookup key 
cc660 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 3b 20 20 20  */.  int cnt;   
cc670 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
cc680 62 65 72 20 6f 66 20 53 48 41 52 45 44 20 6c 6f  ber of SHARED lo
cc690 63 6b 73 20 68 65 6c 64 20 2a 2f 0a 20 20 69 6e  cks held */.  in
cc6a0 74 20 6c 6f 63 6b 74 79 70 65 3b 20 20 20 20 20  t locktype;     
cc6b0 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 48 41     /* One of SHA
cc6c0 52 45 44 5f 4c 4f 43 4b 2c 20 52 45 53 45 52 56  RED_LOCK, RESERV
cc6d0 45 44 5f 4c 4f 43 4b 20 65 74 63 2e 20 2a 2f 0a  ED_LOCK etc. */.
cc6e0 20 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20    int nRef;     
cc6f0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
cc700 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
cc710 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
cc720 2f 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49  /.  struct lockI
cc730 6e 66 6f 20 2a 70 4e 65 78 74 2c 20 2a 70 50 72  nfo *pNext, *pPr
cc740 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66  ev;   /* List of
cc750 20 61 6c 6c 20 6c 6f 63 6b 49 6e 66 6f 20 6f 62   all lockInfo ob
cc760 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  jects */.};../*.
cc770 2a 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f  ** An instance o
cc780 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
cc790 73 74 72 75 63 74 75 72 65 20 73 65 72 76 65 73  structure serves
cc7a0 20 61 73 20 74 68 65 20 6b 65 79 20 75 73 65 64   as the key used
cc7b0 0a 2a 2a 20 74 6f 20 6c 6f 63 61 74 65 20 61 20  .** to locate a 
cc7c0 70 61 72 74 69 63 75 6c 61 72 20 6f 70 65 6e 43  particular openC
cc7d0 6e 74 20 73 74 72 75 63 74 75 72 65 20 67 69 76  nt structure giv
cc7e0 65 6e 20 69 74 73 20 69 6e 6f 64 65 2e 20 20 54  en its inode.  T
cc7f0 68 69 73 0a 2a 2a 20 69 73 20 74 68 65 20 73 61  his.** is the sa
cc800 6d 65 20 61 73 20 74 68 65 20 6c 6f 63 6b 4b 65  me as the lockKe
cc810 79 20 65 78 63 65 70 74 20 74 68 61 74 20 74 68  y except that th
cc820 65 20 74 68 72 65 61 64 20 49 44 20 69 73 20 6f  e thread ID is o
cc830 6d 69 74 74 65 64 2e 0a 2a 2f 0a 73 74 72 75 63  mitted..*/.struc
cc840 74 20 6f 70 65 6e 4b 65 79 20 7b 0a 20 20 64 65  t openKey {.  de
cc850 76 5f 74 20 64 65 76 3b 20 20 20 2f 2a 20 44 65  v_t dev;   /* De
cc860 76 69 63 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 23  vice number */.#
cc870 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if IS_VXWORKS.  
cc880 76 6f 69 64 20 2a 72 6e 61 6d 3b 20 20 2f 2a 20  void *rnam;  /* 
cc890 52 65 61 6c 6e 61 6d 65 20 73 69 6e 63 65 20 69  Realname since i
cc8a0 6e 6f 64 65 20 75 6e 75 73 61 62 6c 65 20 2a 2f  node unusable */
cc8b0 0a 23 65 6c 73 65 0a 20 20 69 6e 6f 5f 74 20 69  .#else.  ino_t i
cc8c0 6e 6f 3b 20 20 20 2f 2a 20 49 6e 6f 64 65 20 6e  no;   /* Inode n
cc8d0 75 6d 62 65 72 20 2a 2f 0a 23 65 6e 64 69 66 0a  umber */.#endif.
cc8e0 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73  };../*.** An ins
cc8f0 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c  tance of the fol
cc900 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65  lowing structure
cc910 20 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f   is allocated fo
cc920 72 20 65 61 63 68 20 6f 70 65 6e 0a 2a 2a 20 69  r each open.** i
cc930 6e 6f 64 65 2e 20 20 54 68 69 73 20 73 74 72 75  node.  This stru
cc940 63 74 75 72 65 20 6b 65 65 70 73 20 74 72 61 63  cture keeps trac
cc950 6b 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72 20  k of the number 
cc960 6f 66 20 6c 6f 63 6b 73 20 6f 6e 20 74 68 61 74  of locks on that
cc970 0a 2a 2a 20 69 6e 6f 64 65 2e 20 20 49 66 20 61  .** inode.  If a
cc980 20 63 6c 6f 73 65 20 69 73 20 61 74 74 65 6d 70   close is attemp
cc990 74 65 64 20 61 67 61 69 6e 73 74 20 61 6e 20 69  ted against an i
cc9a0 6e 6f 64 65 20 74 68 61 74 20 69 73 20 68 6f 6c  node that is hol
cc9b0 64 69 6e 67 0a 2a 2a 20 6c 6f 63 6b 73 2c 20 74  ding.** locks, t
cc9c0 68 65 20 63 6c 6f 73 65 20 69 73 20 64 65 66 65  he close is defe
cc9d0 72 72 65 64 20 75 6e 74 69 6c 20 61 6c 6c 20 6c  rred until all l
cc9e0 6f 63 6b 73 20 63 6c 65 61 72 20 62 79 20 61 64  ocks clear by ad
cc9f0 64 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 6c 65  ding the.** file
cca00 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f 20 62   descriptor to b
cca10 65 20 63 6c 6f 73 65 64 20 74 6f 20 74 68 65 20  e closed to the 
cca20 70 65 6e 64 69 6e 67 20 6c 69 73 74 2e 0a 2a 2f  pending list..*/
cca30 0a 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20  .struct openCnt 
cca40 7b 0a 20 20 73 74 72 75 63 74 20 6f 70 65 6e 4b  {.  struct openK
cca50 65 79 20 6b 65 79 3b 20 20 20 2f 2a 20 54 68 65  ey key;   /* The
cca60 20 6c 6f 6f 6b 75 70 20 6b 65 79 20 2a 2f 0a 20   lookup key */. 
cca70 20 69 6e 74 20 6e 52 65 66 3b 20 20 20 20 20 20   int nRef;      
cca80 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
cca90 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74 6f 20   of pointers to 
ccaa0 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a  this structure *
ccab0 2f 0a 20 20 69 6e 74 20 6e 4c 6f 63 6b 3b 20 20  /.  int nLock;  
ccac0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
ccad0 62 65 72 20 6f 66 20 6f 75 74 73 74 61 6e 64 69  ber of outstandi
ccae0 6e 67 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20 69 6e  ng locks */.  in
ccaf0 74 20 6e 50 65 6e 64 69 6e 67 3b 20 20 20 20 20  t nPending;     
ccb00 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
ccb10 20 70 65 6e 64 69 6e 67 20 63 6c 6f 73 65 28 29   pending close()
ccb20 20 6f 70 65 72 61 74 69 6f 6e 73 20 2a 2f 0a 20   operations */. 
ccb30 20 69 6e 74 20 2a 61 50 65 6e 64 69 6e 67 3b 20   int *aPending; 
ccb40 20 20 20 20 20 20 20 2f 2a 20 4d 61 6c 6c 6f 63         /* Malloc
ccb50 65 64 20 73 70 61 63 65 20 68 6f 6c 64 69 6e 67  ed space holding
ccb60 20 66 64 27 73 20 61 77 61 69 74 69 6e 67 20 61   fd's awaiting a
ccb70 20 63 6c 6f 73 65 28 29 20 2a 2f 0a 23 69 66 20   close() */.#if 
ccb80 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 73 65 6d  IS_VXWORKS.  sem
ccb90 5f 74 20 2a 70 53 65 6d 3b 20 20 20 20 20 20 20  _t *pSem;       
ccba0 20 20 20 2f 2a 20 4e 61 6d 65 64 20 50 4f 53 49     /* Named POSI
ccbb0 58 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 20  X semaphore */. 
ccbc0 20 63 68 61 72 20 61 53 65 6d 4e 61 6d 65 5b 4d   char aSemName[M
ccbd0 41 58 5f 50 41 54 48 4e 41 4d 45 2b 31 5d 3b 20  AX_PATHNAME+1]; 
ccbe0 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 61    /* Name of tha
ccbf0 74 20 73 65 6d 61 70 68 6f 72 65 20 2a 2f 0a 23  t semaphore */.#
ccc00 65 6e 64 69 66 0a 20 20 73 74 72 75 63 74 20 6f  endif.  struct o
ccc10 70 65 6e 43 6e 74 20 2a 70 4e 65 78 74 2c 20 2a  penCnt *pNext, *
ccc20 70 50 72 65 76 3b 20 20 20 2f 2a 20 4c 69 73 74  pPrev;   /* List
ccc30 20 6f 66 20 61 6c 6c 20 6f 70 65 6e 43 6e 74 20   of all openCnt 
ccc40 6f 62 6a 65 63 74 73 20 2a 2f 0a 7d 3b 0a 0a 2f  objects */.};../
ccc50 2a 0a 2a 2a 20 4c 69 73 74 20 6f 66 20 61 6c 6c  *.** List of all
ccc60 20 6c 6f 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70   lockInfo and op
ccc70 65 6e 43 6e 74 20 6f 62 6a 65 63 74 73 2e 20 20  enCnt objects.  
ccc80 54 68 69 73 20 75 73 65 64 20 74 6f 20 62 65 20  This used to be 
ccc90 61 20 68 61 73 68 0a 2a 2a 20 74 61 62 6c 65 2e  a hash.** table.
ccca0 20 20 42 75 74 20 74 68 65 20 6e 75 6d 62 65 72    But the number
cccb0 20 6f 66 20 6f 62 6a 65 63 74 73 20 69 73 20 72   of objects is r
cccc0 61 72 65 6c 79 20 6d 6f 72 65 20 74 68 61 6e 20  arely more than 
cccd0 61 20 64 6f 7a 65 6e 20 61 6e 64 0a 2a 2a 20 6e  a dozen and.** n
ccce0 65 76 65 72 20 65 78 63 65 65 64 73 20 61 20 66  ever exceeds a f
cccf0 65 77 20 74 68 6f 75 73 61 6e 64 2e 20 20 41 6e  ew thousand.  An
ccd00 64 20 6c 6f 6f 6b 75 70 20 69 73 20 6e 6f 74 20  d lookup is not 
ccd10 6f 6e 20 61 20 63 72 69 74 69 63 61 6c 0a 2a 2a  on a critical.**
ccd20 20 70 61 74 68 20 6f 6f 20 61 20 73 69 6d 70 6c   path oo a simpl
ccd30 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 77 69  e linked list wi
ccd40 6c 6c 20 73 75 66 66 69 63 65 2e 0a 2a 2f 0a 73  ll suffice..*/.s
ccd50 74 61 74 69 63 20 73 74 72 75 63 74 20 6c 6f 63  tatic struct loc
ccd60 6b 49 6e 66 6f 20 2a 6c 6f 63 6b 4c 69 73 74 20  kInfo *lockList 
ccd70 3d 20 30 3b 0a 73 74 61 74 69 63 20 73 74 72 75  = 0;.static stru
ccd80 63 74 20 6f 70 65 6e 43 6e 74 20 2a 6f 70 65 6e  ct openCnt *open
ccd90 4c 69 73 74 20 3d 20 30 3b 0a 0a 23 69 66 20 49  List = 0;..#if I
ccda0 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 0a 2a 2a 20  S_VXWORKS./*.** 
ccdb0 54 68 69 73 20 68 61 73 68 20 74 61 62 6c 65 20  This hash table 
ccdc0 69 73 20 75 73 65 64 20 74 6f 20 62 69 6e 64 20  is used to bind 
ccdd0 74 68 65 20 63 61 6e 6f 6e 69 63 61 6c 20 66 69  the canonical fi
ccde0 6c 65 20 6e 61 6d 65 20 74 6f 20 61 0a 2a 2a 20  le name to a.** 
ccdf0 75 6e 69 78 46 69 6c 65 20 73 74 72 75 63 74 75  unixFile structu
cce00 72 65 20 61 6e 64 20 75 73 65 20 74 68 65 20 68  re and use the h
cce10 61 73 68 20 6b 65 79 20 28 3d 20 63 61 6e 6f 6e  ash key (= canon
cce20 69 63 61 6c 20 6e 61 6d 65 29 0a 2a 2a 20 69 6e  ical name).** in
cce30 73 74 65 61 64 20 6f 66 20 74 68 65 20 49 6e 6f  stead of the Ino
cce40 64 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  de number of the
cce50 20 66 69 6c 65 20 74 6f 20 66 69 6e 64 20 74 68   file to find th
cce60 65 20 6d 61 74 63 68 69 6e 67 0a 2a 2a 20 6c 6f  e matching.** lo
cce70 63 6b 49 6e 66 6f 20 61 6e 64 20 6f 70 65 6e 43  ckInfo and openC
cce80 6e 74 20 73 74 72 75 63 74 75 72 65 73 2e 20 49  nt structures. I
cce90 74 20 61 6c 73 6f 20 68 65 6c 70 73 20 74 6f 20  t also helps to 
ccea0 6d 61 6b 65 20 74 68 65 0a 2a 2a 20 6e 61 6d 65  make the.** name
cceb0 20 6f 66 20 74 68 65 20 73 65 6d 61 70 68 6f 72   of the semaphor
ccec0 65 20 77 68 65 6e 20 4c 4f 43 4b 49 4e 47 5f 53  e when LOCKING_S
cced0 54 59 4c 45 5f 4e 41 4d 45 44 53 45 4d 20 69 73  TYLE_NAMEDSEM is
ccee0 20 75 73 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65   used.** for the
ccef0 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   file..*/.static
ccf00 20 48 61 73 68 20 6e 61 6d 65 48 61 73 68 3b 0a   Hash nameHash;.
ccf10 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68  #endif../*.** Th
ccf20 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 73  e locking styles
ccf30 20 61 72 65 20 61 73 73 6f 63 69 61 74 65 64 20   are associated 
ccf40 77 69 74 68 20 74 68 65 20 64 69 66 66 65 72 65  with the differe
ccf50 6e 74 20 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a  nt file locking.
ccf60 2a 2a 20 63 61 70 61 62 69 6c 69 74 69 65 73 20  ** capabilities 
ccf70 73 75 70 70 6f 72 74 65 64 20 62 79 20 64 69 66  supported by dif
ccf80 66 65 72 65 6e 74 20 66 69 6c 65 20 73 79 73 74  ferent file syst
ccf90 65 6d 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 50 4f 53  ems.  .**.** POS
ccfa0 49 58 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65  IX locking style
ccfb0 20 66 75 6c 6c 79 20 73 75 70 70 6f 72 74 73 20   fully supports 
ccfc0 73 68 61 72 65 64 20 61 6e 64 20 65 78 63 6c 75  shared and exclu
ccfd0 73 69 76 65 20 62 79 74 65 2d 72 61 6e 67 65 20  sive byte-range 
ccfe0 6c 6f 63 6b 73 20 0a 2a 2a 20 41 46 50 20 6c 6f  locks .** AFP lo
ccff0 63 6b 69 6e 67 20 6f 6e 6c 79 20 73 75 70 70 6f  cking only suppo
cd000 72 74 73 20 65 78 63 6c 75 73 69 76 65 20 62 79  rts exclusive by
cd010 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b 73 0a 2a  te-range locks.*
cd020 2a 20 46 4c 4f 43 4b 20 6f 6e 6c 79 20 73 75 70  * FLOCK only sup
cd030 70 6f 72 74 73 20 61 20 73 69 6e 67 6c 65 20 66  ports a single f
cd040 69 6c 65 2d 67 6c 6f 62 61 6c 20 65 78 63 6c 75  ile-global exclu
cd050 73 69 76 65 20 6c 6f 63 6b 0a 2a 2a 20 44 4f 54  sive lock.** DOT
cd060 4c 4f 43 4b 20 69 73 6e 27 74 20 61 20 74 72 75  LOCK isn't a tru
cd070 65 20 6c 6f 63 6b 69 6e 67 20 73 74 79 6c 65 2c  e locking style,
cd080 20 69 74 20 72 65 66 65 72 73 20 74 6f 20 74 68   it refers to th
cd090 65 20 75 73 65 20 6f 66 20 61 20 73 70 65 63 69  e use of a speci
cd0a0 61 6c 0a 2a 2a 20 20 20 66 69 6c 65 20 6e 61 6d  al.**   file nam
cd0b0 65 64 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  ed the same as t
cd0c0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
cd0d0 20 77 69 74 68 20 61 20 27 2e 6c 6f 63 6b 27 20   with a '.lock' 
cd0e0 65 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a  extension, this.
cd0f0 2a 2a 20 20 20 63 61 6e 20 62 65 20 75 73 65 64  **   can be used
cd100 20 6f 6e 20 66 69 6c 65 20 73 79 73 74 65 6d 73   on file systems
cd110 20 74 68 61 74 20 64 6f 20 6e 6f 74 20 6f 66 66   that do not off
cd120 65 72 20 61 6e 79 20 72 65 6c 69 61 62 6c 65 20  er any reliable 
cd130 66 69 6c 65 20 6c 6f 63 6b 69 6e 67 0a 2a 2a 20  file locking.** 
cd140 4e 4f 20 6c 6f 63 6b 69 6e 67 20 6d 65 61 6e 73  NO locking means
cd150 20 74 68 61 74 20 6e 6f 20 6c 6f 63 6b 69 6e 67   that no locking
cd160 20 77 69 6c 6c 20 62 65 20 61 74 74 65 6d 70 74   will be attempt
cd170 65 64 2c 20 74 68 69 73 20 69 73 20 6f 6e 6c 79  ed, this is only
cd180 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 20 20 72   used for.**   r
cd190 65 61 64 2d 6f 6e 6c 79 20 66 69 6c 65 20 73 79  ead-only file sy
cd1a0 73 74 65 6d 73 20 63 75 72 72 65 6e 74 6c 79 0a  stems currently.
cd1b0 2a 2a 20 4e 41 4d 45 44 53 45 4d 20 69 73 20 73  ** NAMEDSEM is s
cd1c0 69 6d 69 6c 61 72 20 74 6f 20 44 4f 54 4c 4f 43  imilar to DOTLOC
cd1d0 4b 20 62 75 74 20 75 73 65 73 20 61 20 6e 61 6d  K but uses a nam
cd1e0 65 64 20 73 65 6d 61 70 68 6f 72 65 20 69 6e 73  ed semaphore ins
cd1f0 74 65 61 64 20 6f 66 20 61 6e 0a 2a 2a 20 20 20  tead of an.**   
cd200 69 6e 64 69 63 61 74 6f 72 20 66 69 6c 65 2e 0a  indicator file..
cd210 2a 2a 20 55 4e 53 55 50 50 4f 52 54 45 44 20 6d  ** UNSUPPORTED m
cd220 65 61 6e 73 20 74 68 61 74 20 6e 6f 20 6c 6f 63  eans that no loc
cd230 6b 69 6e 67 20 77 69 6c 6c 20 62 65 20 61 74 74  king will be att
cd240 65 6d 70 74 65 64 2c 20 74 68 69 73 20 69 73 20  empted, this is 
cd250 6f 6e 6c 79 20 75 73 65 64 20 66 6f 72 0a 2a 2a  only used for.**
cd260 20 20 20 66 69 6c 65 20 73 79 73 74 65 6d 73 20     file systems 
cd270 74 68 61 74 20 61 72 65 20 6b 6e 6f 77 6e 20 74  that are known t
cd280 6f 20 62 65 20 75 6e 73 75 70 70 6f 72 74 65 64  o be unsupported
cd290 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b  .*/.#define LOCK
cd2a0 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 20  ING_STYLE_POSIX 
cd2b0 20 20 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65         1.#define
cd2c0 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e   LOCKING_STYLE_N
cd2d0 4f 4e 45 20 20 20 20 20 20 20 20 20 32 0a 23 64  ONE         2.#d
cd2e0 65 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 54  efine LOCKING_ST
cd2f0 59 4c 45 5f 44 4f 54 46 49 4c 45 20 20 20 20 20  YLE_DOTFILE     
cd300 20 33 0a 23 64 65 66 69 6e 65 20 4c 4f 43 4b 49   3.#define LOCKI
cd310 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f 43 4b 20 20  NG_STYLE_FLOCK  
cd320 20 20 20 20 20 20 34 0a 23 64 65 66 69 6e 65 20        4.#define 
cd330 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 41 46  LOCKING_STYLE_AF
cd340 50 20 20 20 20 20 20 20 20 20 20 35 0a 23 64 65  P          5.#de
cd350 66 69 6e 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59  fine LOCKING_STY
cd360 4c 45 5f 4e 41 4d 45 44 53 45 4d 20 20 20 20 20  LE_NAMEDSEM     
cd370 36 0a 0a 2f 2a 0a 2a 2a 20 4f 6e 6c 79 20 73 65  6../*.** Only se
cd380 74 20 74 68 65 20 6c 61 73 74 45 72 72 6e 6f 20  t the lastErrno 
cd390 69 66 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  if the error cod
cd3a0 65 20 69 73 20 61 20 72 65 61 6c 20 65 72 72 6f  e is a real erro
cd3b0 72 20 61 6e 64 20 6e 6f 74 20 0a 2a 2a 20 61 20  r and not .** a 
cd3c0 6e 6f 72 6d 61 6c 20 65 78 70 65 63 74 65 64 20  normal expected 
cd3d0 72 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53  return code of S
cd3e0 51 4c 49 54 45 5f 42 55 53 59 20 6f 72 20 53 51  QLITE_BUSY or SQ
cd3f0 4c 49 54 45 5f 4f 4b 0a 2a 2f 0a 23 64 65 66 69  LITE_OK.*/.#defi
cd400 6e 65 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  ne IS_LOCK_ERROR
cd410 28 78 29 20 20 28 28 78 20 21 3d 20 53 51 4c 49  (x)  ((x != SQLI
cd420 54 45 5f 4f 4b 29 20 26 26 20 28 78 20 21 3d 20  TE_OK) && (x != 
cd430 53 51 4c 49 54 45 5f 42 55 53 59 29 29 0a 0a 2f  SQLITE_BUSY))../
cd440 2a 0a 2a 2a 20 48 65 6c 70 65 72 20 66 75 6e 63  *.** Helper func
cd450 74 69 6f 6e 73 20 74 6f 20 6f 62 74 61 69 6e 20  tions to obtain 
cd460 61 6e 64 20 72 65 6c 69 6e 71 75 69 73 68 20 74  and relinquish t
cd470 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 2e  he global mutex.
cd480 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
cd490 65 6e 74 65 72 4d 75 74 65 78 28 76 6f 69 64 29  enterMutex(void)
cd4a0 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  {.  sqlite3_mute
cd4b0 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d  x_enter(sqlite3M
cd4c0 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
cd4d0 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41  _MUTEX_STATIC_MA
cd4e0 53 54 45 52 29 29 3b 0a 7d 0a 73 74 61 74 69 63  STER));.}.static
cd4f0 20 76 6f 69 64 20 6c 65 61 76 65 4d 75 74 65 78   void leaveMutex
cd500 28 76 6f 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  (void){.  sqlite
cd510 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71  3_mutex_leave(sq
cd520 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
cd530 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
cd540 54 49 43 5f 4d 41 53 54 45 52 29 29 3b 0a 7d 0a  TIC_MASTER));.}.
cd550 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45  .#if SQLITE_THRE
cd560 41 44 53 41 46 45 0a 2f 2a 0a 2a 2a 20 54 68 69  ADSAFE./*.** Thi
cd570 73 20 76 61 72 69 61 62 6c 65 20 72 65 63 6f 72  s variable recor
cd580 64 73 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f  ds whether or no
cd590 74 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76  t threads can ov
cd5a0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
cd5b0 72 73 0a 2a 2a 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a  rs.** locks..**.
cd5c0 2a 2a 20 20 20 20 30 3a 20 20 4e 6f 2e 20 20 54  **    0:  No.  T
cd5d0 68 72 65 61 64 73 20 63 61 6e 6e 6f 74 20 6f 76  hreads cannot ov
cd5e0 65 72 72 69 64 65 20 65 61 63 68 20 6f 74 68 65  erride each othe
cd5f0 72 73 20 6c 6f 63 6b 73 2e 0a 2a 2a 20 20 20 20  rs locks..**    
cd600 31 3a 20 20 59 65 73 2e 20 20 54 68 72 65 61 64  1:  Yes.  Thread
cd610 73 20 63 61 6e 20 6f 76 65 72 72 69 64 65 20 65  s can override e
cd620 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
cd630 2e 0a 2a 2a 20 20 20 2d 31 3a 20 20 57 65 20 64  ..**   -1:  We d
cd640 6f 6e 27 74 20 6b 6e 6f 77 20 79 65 74 2e 0a 2a  on't know yet..*
cd650 2a 0a 2a 2a 20 4f 6e 20 73 6f 6d 65 20 73 79 73  *.** On some sys
cd660 74 65 6d 73 2c 20 77 65 20 6b 6e 6f 77 20 61 74  tems, we know at
cd670 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 69 66   compile-time if
cd680 20 74 68 72 65 61 64 73 20 63 61 6e 20 6f 76 65   threads can ove
cd690 72 72 69 64 65 20 65 61 63 68 0a 2a 2a 20 6f 74  rride each.** ot
cd6a0 68 65 72 73 20 6c 6f 63 6b 73 2e 20 20 4f 6e 20  hers locks.  On 
cd6b0 74 68 6f 73 65 20 73 79 73 74 65 6d 73 2c 20 74  those systems, t
cd6c0 68 65 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  he SQLITE_THREAD
cd6d0 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 20 6d  _OVERRIDE_LOCK m
cd6e0 61 63 72 6f 0a 2a 2a 20 77 69 6c 6c 20 62 65 20  acro.** will be 
cd6f0 73 65 74 20 61 70 70 72 6f 70 72 69 61 74 65 6c  set appropriatel
cd700 79 2e 20 20 4f 6e 20 6f 74 68 65 72 20 73 79 73  y.  On other sys
cd710 74 65 6d 73 2c 20 77 65 20 68 61 76 65 20 74 6f  tems, we have to
cd720 20 63 68 65 63 6b 20 61 74 0a 2a 2a 20 72 75 6e   check at.** run
cd730 74 69 6d 65 2e 20 20 4f 6e 20 74 68 65 73 65 20  time.  On these 
cd740 6c 61 74 74 65 72 20 73 79 73 74 65 6d 73 2c 20  latter systems, 
cd750 53 51 4c 54 49 45 5f 54 48 52 45 41 44 5f 4f 56  SQLTIE_THREAD_OV
cd760 45 52 52 49 44 45 5f 4c 4f 43 4b 20 69 73 0a 2a  ERRIDE_LOCK is.*
cd770 2a 20 75 6e 64 65 66 69 6e 65 64 2e 0a 2a 2a 0a  * undefined..**.
cd780 2a 2a 20 54 68 69 73 20 76 61 72 69 61 62 6c 65  ** This variable
cd790 20 6e 6f 72 6d 61 6c 6c 79 20 68 61 73 20 66 69   normally has fi
cd7a0 6c 65 20 73 63 6f 70 65 20 6f 6e 6c 79 2e 20 20  le scope only.  
cd7b0 42 75 74 20 64 75 72 69 6e 67 20 74 65 73 74 69  But during testi
cd7c0 6e 67 2c 20 77 65 20 6d 61 6b 65 0a 2a 2a 20 69  ng, we make.** i
cd7d0 74 20 61 20 67 6c 6f 62 61 6c 20 73 6f 20 74 68  t a global so th
cd7e0 61 74 20 74 68 65 20 74 65 73 74 20 63 6f 64 65  at the test code
cd7f0 20 63 61 6e 20 63 68 61 6e 67 65 20 69 74 73 20   can change its 
cd800 76 61 6c 75 65 20 69 6e 20 6f 72 64 65 72 20 74  value in order t
cd810 6f 20 76 65 72 69 66 79 0a 2a 2a 20 74 68 61 74  o verify.** that
cd820 20 74 68 65 20 72 69 67 68 74 20 73 74 75 66 66   the right stuff
cd830 20 68 61 70 70 65 6e 73 20 69 6e 20 65 69 74 68   happens in eith
cd840 65 72 20 63 61 73 65 2e 0a 2a 2f 0a 23 69 66 6e  er case..*/.#ifn
cd850 64 65 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  def SQLITE_THREA
cd860 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43 4b 0a  D_OVERRIDE_LOCK.
cd870 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f  # define SQLITE_
cd880 54 48 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f  THREAD_OVERRIDE_
cd890 4c 4f 43 4b 20 2d 31 0a 23 65 6e 64 69 66 0a 23  LOCK -1.#endif.#
cd8a0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
cd8b0 54 0a 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65  T.int threadsOve
cd8c0 72 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c  rrideEachOthersL
cd8d0 6f 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48  ocks = SQLITE_TH
cd8e0 52 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f  READ_OVERRIDE_LO
cd8f0 43 4b 3b 0a 23 65 6c 73 65 0a 73 74 61 74 69 63  CK;.#else.static
cd900 20 69 6e 74 20 74 68 72 65 61 64 73 4f 76 65 72   int threadsOver
cd910 72 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f  rideEachOthersLo
cd920 63 6b 73 20 3d 20 53 51 4c 49 54 45 5f 54 48 52  cks = SQLITE_THR
cd930 45 41 44 5f 4f 56 45 52 52 49 44 45 5f 4c 4f 43  EAD_OVERRIDE_LOC
cd940 4b 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  K;.#endif../*.**
cd950 20 54 68 69 73 20 73 74 72 75 63 74 75 72 65 20   This structure 
cd960 68 6f 6c 64 73 20 69 6e 66 6f 72 6d 61 74 69 6f  holds informatio
cd970 6e 20 70 61 73 73 65 64 20 69 6e 74 6f 20 69 6e  n passed into in
cd980 64 69 76 69 64 75 61 6c 20 74 65 73 74 0a 2a 2a  dividual test.**
cd990 20 74 68 72 65 61 64 73 20 62 79 20 74 68 65 20   threads by the 
cd9a0 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69 6e  testThreadLockin
cd9b0 67 42 65 68 61 76 69 6f 72 28 29 20 72 6f 75 74  gBehavior() rout
cd9c0 69 6e 65 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 74  ine..*/.struct t
cd9d0 68 72 65 61 64 54 65 73 74 44 61 74 61 20 7b 0a  hreadTestData {.
cd9e0 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20 20    int fd;       
cd9f0 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65           /* File
cda00 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a 2f   to be locked */
cda10 0a 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20  .  struct flock 
cda20 6c 6f 63 6b 3b 20 20 20 20 20 2f 2a 20 54 68 65  lock;     /* The
cda30 20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69   locking operati
cda40 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 75  on */.  int resu
cda50 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  lt;            /
cda60 2a 20 52 65 73 75 6c 74 20 6f 66 20 74 68 65 20  * Result of the 
cda70 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
cda80 6e 20 2a 2f 0a 7d 3b 0a 0a 23 69 66 64 65 66 20  n */.};..#ifdef 
cda90 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
cdaa0 45 0a 2f 2a 0a 2a 2a 20 50 72 69 6e 74 20 6f 75  E./*.** Print ou
cdab0 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62  t information ab
cdac0 6f 75 74 20 61 6c 6c 20 6c 6f 63 6b 69 6e 67 20  out all locking 
cdad0 6f 70 65 72 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a  operations..**.*
cdae0 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
cdaf0 73 20 75 73 65 64 20 66 6f 72 20 74 72 6f 75 62  s used for troub
cdb00 6c 65 73 68 6f 6f 74 69 6e 67 20 6c 6f 63 6b 73  leshooting locks
cdb10 20 6f 6e 20 6d 75 6c 74 69 74 68 72 65 61 64 65   on multithreade
cdb20 64 0a 2a 2a 20 70 6c 61 74 66 6f 72 6d 73 2e 20  d.** platforms. 
cdb30 20 45 6e 61 62 6c 65 20 62 79 20 63 6f 6d 70 69   Enable by compi
cdb40 6c 69 6e 67 20 77 69 74 68 20 74 68 65 20 2d 44  ling with the -D
cdb50 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54 52 41 43  SQLITE_LOCK_TRAC
cdb60 45 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e  E.** command-lin
cdb70 65 20 6f 70 74 69 6f 6e 20 6f 6e 20 74 68 65 20  e option on the 
cdb80 63 6f 6d 70 69 6c 65 72 2e 20 20 54 68 69 73 20  compiler.  This 
cdb90 63 6f 64 65 20 69 73 20 6e 6f 72 6d 61 6c 6c 79  code is normally
cdba0 0a 2a 2a 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a  .** turned off..
cdbb0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f  */.static int lo
cdbc0 63 6b 54 72 61 63 65 28 69 6e 74 20 66 64 2c 20  ckTrace(int fd, 
cdbd0 69 6e 74 20 6f 70 2c 20 73 74 72 75 63 74 20 66  int op, struct f
cdbe0 6c 6f 63 6b 20 2a 70 29 7b 0a 20 20 63 68 61 72  lock *p){.  char
cdbf0 20 2a 7a 4f 70 4e 61 6d 65 2c 20 2a 7a 54 79 70   *zOpName, *zTyp
cdc00 65 3b 0a 20 20 69 6e 74 20 73 3b 0a 20 20 69 6e  e;.  int s;.  in
cdc10 74 20 73 61 76 65 64 45 72 72 6e 6f 3b 0a 20 20  t savedErrno;.  
cdc20 69 66 28 20 6f 70 3d 3d 46 5f 47 45 54 4c 4b 20  if( op==F_GETLK 
cdc30 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65 20 3d  ){.    zOpName =
cdc40 20 22 47 45 54 4c 4b 22 3b 0a 20 20 7d 65 6c 73   "GETLK";.  }els
cdc50 65 20 69 66 28 20 6f 70 3d 3d 46 5f 53 45 54 4c  e if( op==F_SETL
cdc60 4b 20 29 7b 0a 20 20 20 20 7a 4f 70 4e 61 6d 65  K ){.    zOpName
cdc70 20 3d 20 22 53 45 54 4c 4b 22 3b 0a 20 20 7d 65   = "SETLK";.  }e
cdc80 6c 73 65 7b 0a 20 20 20 20 73 20 3d 20 66 63 6e  lse{.    s = fcn
cdc90 74 6c 28 66 64 2c 20 6f 70 2c 20 70 29 3b 0a 20  tl(fd, op, p);. 
cdca0 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
cdcb0 72 69 6e 74 66 28 22 66 63 6e 74 6c 20 75 6e 6b  rintf("fcntl unk
cdcc0 6e 6f 77 6e 20 25 64 20 25 64 20 25 64 5c 6e 22  nown %d %d %d\n"
cdcd0 2c 20 66 64 2c 20 6f 70 2c 20 73 29 3b 0a 20 20  , fd, op, s);.  
cdce0 20 20 72 65 74 75 72 6e 20 73 3b 0a 20 20 7d 0a    return s;.  }.
cdcf0 20 20 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d    if( p->l_type=
cdd00 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_RDLCK ){.    
cdd10 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b 22 3b  zType = "RDLCK";
cdd20 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
cdd30 6c 5f 74 79 70 65 3d 3d 46 5f 57 52 4c 43 4b 20  l_type==F_WRLCK 
cdd40 29 7b 0a 20 20 20 20 7a 54 79 70 65 20 3d 20 22  ){.    zType = "
cdd50 57 52 4c 43 4b 22 3b 0a 20 20 7d 65 6c 73 65 20  WRLCK";.  }else 
cdd60 69 66 28 20 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46  if( p->l_type==F
cdd70 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20 7a 54  _UNLCK ){.    zT
cdd80 79 70 65 20 3d 20 22 55 4e 4c 43 4b 22 3b 0a 20  ype = "UNLCK";. 
cdd90 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
cdda0 72 74 28 20 30 20 29 3b 0a 20 20 7d 0a 20 20 61  rt( 0 );.  }.  a
cddb0 73 73 65 72 74 28 20 70 2d 3e 6c 5f 77 68 65 6e  ssert( p->l_when
cddc0 63 65 3d 3d 53 45 45 4b 5f 53 45 54 20 29 3b 0a  ce==SEEK_SET );.
cddd0 20 20 73 20 3d 20 66 63 6e 74 6c 28 66 64 2c 20    s = fcntl(fd, 
cdde0 6f 70 2c 20 70 29 3b 0a 20 20 73 61 76 65 64 45  op, p);.  savedE
cddf0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
cde00 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
cde10 74 66 28 22 66 63 6e 74 6c 20 25 64 20 25 64 20  tf("fcntl %d %d 
cde20 25 73 20 25 73 20 25 64 20 25 64 20 25 64 20 25  %s %s %d %d %d %
cde30 64 5c 6e 22 2c 0a 20 20 20 20 20 74 68 72 65 61  d\n",.     threa
cde40 64 69 64 2c 20 66 64 2c 20 7a 4f 70 4e 61 6d 65  did, fd, zOpName
cde50 2c 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 70 2d  , zType, (int)p-
cde60 3e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 70  >l_start, (int)p
cde70 2d 3e 6c 5f 6c 65 6e 2c 0a 20 20 20 20 20 28 69  ->l_len,.     (i
cde80 6e 74 29 70 2d 3e 6c 5f 70 69 64 2c 20 73 29 3b  nt)p->l_pid, s);
cde90 0a 20 20 69 66 28 20 73 3d 3d 28 2d 31 29 20 26  .  if( s==(-1) &
cdea0 26 20 6f 70 3d 3d 46 5f 53 45 54 4c 4b 20 26 26  & op==F_SETLK &&
cdeb0 20 28 70 2d 3e 6c 5f 74 79 70 65 3d 3d 46 5f 52   (p->l_type==F_R
cdec0 44 4c 43 4b 20 7c 7c 20 70 2d 3e 6c 5f 74 79 70  DLCK || p->l_typ
cded0 65 3d 3d 46 5f 57 52 4c 43 4b 29 20 29 7b 0a 20  e==F_WRLCK) ){. 
cdee0 20 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20     struct flock 
cdef0 6c 32 3b 0a 20 20 20 20 6c 32 20 3d 20 2a 70 3b  l2;.    l2 = *p;
cdf00 0a 20 20 20 20 66 63 6e 74 6c 28 66 64 2c 20 46  .    fcntl(fd, F
cdf10 5f 47 45 54 4c 4b 2c 20 26 6c 32 29 3b 0a 20 20  _GETLK, &l2);.  
cdf20 20 20 69 66 28 20 6c 32 2e 6c 5f 74 79 70 65 3d    if( l2.l_type=
cdf30 3d 46 5f 52 44 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_RDLCK ){.    
cdf40 20 20 7a 54 79 70 65 20 3d 20 22 52 44 4c 43 4b    zType = "RDLCK
cdf50 22 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ";.    }else if(
cdf60 20 6c 32 2e 6c 5f 74 79 70 65 3d 3d 46 5f 57 52   l2.l_type==F_WR
cdf70 4c 43 4b 20 29 7b 0a 20 20 20 20 20 20 7a 54 79  LCK ){.      zTy
cdf80 70 65 20 3d 20 22 57 52 4c 43 4b 22 3b 0a 20 20  pe = "WRLCK";.  
cdf90 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 32 2e 6c    }else if( l2.l
cdfa0 5f 74 79 70 65 3d 3d 46 5f 55 4e 4c 43 4b 20 29  _type==F_UNLCK )
cdfb0 7b 0a 20 20 20 20 20 20 7a 54 79 70 65 20 3d 20  {.      zType = 
cdfc0 22 55 4e 4c 43 4b 22 3b 0a 20 20 20 20 7d 65 6c  "UNLCK";.    }el
cdfd0 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
cdfe0 28 20 30 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ( 0 );.    }.   
cdff0 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
ce000 6e 74 66 28 22 66 63 6e 74 6c 2d 66 61 69 6c 75  ntf("fcntl-failu
ce010 72 65 2d 72 65 61 73 6f 6e 3a 20 25 73 20 25 64  re-reason: %s %d
ce020 20 25 64 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20   %d %d\n",.     
ce030 20 20 7a 54 79 70 65 2c 20 28 69 6e 74 29 6c 32    zType, (int)l2
ce040 2e 6c 5f 73 74 61 72 74 2c 20 28 69 6e 74 29 6c  .l_start, (int)l
ce050 32 2e 6c 5f 6c 65 6e 2c 20 28 69 6e 74 29 6c 32  2.l_len, (int)l2
ce060 2e 6c 5f 70 69 64 29 3b 0a 20 20 7d 0a 20 20 65  .l_pid);.  }.  e
ce070 72 72 6e 6f 20 3d 20 73 61 76 65 64 45 72 72 6e  rrno = savedErrn
ce080 6f 3b 0a 20 20 72 65 74 75 72 6e 20 73 3b 0a 7d  o;.  return s;.}
ce090 0a 23 64 65 66 69 6e 65 20 66 63 6e 74 6c 20 6c  .#define fcntl l
ce0a0 6f 63 6b 54 72 61 63 65 0a 23 65 6e 64 69 66 20  ockTrace.#endif 
ce0b0 2f 2a 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 5f 54  /* SQLITE_LOCK_T
ce0c0 52 41 43 45 20 2a 2f 0a 0a 23 69 66 64 65 66 20  RACE */..#ifdef 
ce0d0 5f 5f 6c 69 6e 75 78 5f 5f 0a 2f 2a 0a 2a 2a 20  __linux__./*.** 
ce0e0 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
ce0f0 20 75 73 65 64 20 61 73 20 74 68 65 20 6d 61 69   used as the mai
ce100 6e 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 20  n routine for a 
ce110 74 68 72 65 61 64 20 6c 61 75 6e 63 68 65 64 20  thread launched 
ce120 62 79 0a 2a 2a 20 74 65 73 74 54 68 72 65 61 64  by.** testThread
ce130 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28  LockingBehavior(
ce140 29 2e 20 49 74 20 74 65 73 74 73 20 77 68 65 74  ). It tests whet
ce150 68 65 72 20 74 68 65 20 73 68 61 72 65 64 2d 6c  her the shared-l
ce160 6f 63 6b 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20  ock obtained.** 
ce170 62 79 20 74 68 65 20 6d 61 69 6e 20 74 68 72 65  by the main thre
ce180 61 64 20 69 6e 20 74 65 73 74 54 68 72 65 61 64  ad in testThread
ce190 4c 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28  LockingBehavior(
ce1a0 29 20 63 6f 6e 66 6c 69 63 74 73 20 77 69 74 68  ) conflicts with
ce1b0 20 61 0a 2a 2a 20 68 79 70 6f 74 68 65 74 69 63   a.** hypothetic
ce1c0 61 6c 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 62  al write-lock ob
ce1d0 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20 74  tained by this t
ce1e0 68 72 65 61 64 20 6f 6e 20 74 68 65 20 73 61 6d  hread on the sam
ce1f0 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  e file..**.** Th
ce200 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69 73 20  e write-lock is 
ce210 6e 6f 74 20 61 63 74 75 61 6c 6c 79 20 61 63 71  not actually acq
ce220 75 69 72 65 64 2c 20 61 73 20 74 68 69 73 20 69  uired, as this i
ce230 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 69  s not possible i
ce240 66 20 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 69  f .** the file i
ce250 73 20 6f 70 65 6e 20 69 6e 20 72 65 61 64 2d 6f  s open in read-o
ce260 6e 6c 79 20 6d 6f 64 65 20 28 73 65 65 20 74 69  nly mode (see ti
ce270 63 6b 65 74 20 23 33 34 37 32 29 2e 0a 2a 2f 20  cket #3472)..*/ 
ce280 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 74 68  .static void *th
ce290 72 65 61 64 4c 6f 63 6b 69 6e 67 54 65 73 74 28  readLockingTest(
ce2a0 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
ce2b0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
ce2c0 44 61 74 61 20 2a 70 44 61 74 61 20 3d 20 28 73  Data *pData = (s
ce2d0 74 72 75 63 74 20 74 68 72 65 61 64 54 65 73 74  truct threadTest
ce2e0 44 61 74 61 2a 29 70 41 72 67 3b 0a 20 20 70 44  Data*)pArg;.  pD
ce2f0 61 74 61 2d 3e 72 65 73 75 6c 74 20 3d 20 66 63  ata->result = fc
ce300 6e 74 6c 28 70 44 61 74 61 2d 3e 66 64 2c 20 46  ntl(pData->fd, F
ce310 5f 47 45 54 4c 4b 2c 20 26 70 44 61 74 61 2d 3e  _GETLK, &pData->
ce320 6c 6f 63 6b 29 3b 0a 20 20 72 65 74 75 72 6e 20  lock);.  return 
ce330 70 41 72 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  pArg;.}../*.** T
ce340 68 69 73 20 70 72 6f 63 65 64 75 72 65 20 61 74  his procedure at
ce350 74 65 6d 70 74 73 20 74 6f 20 64 65 74 65 72 6d  tempts to determ
ce360 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ine whether or n
ce370 6f 74 20 74 68 72 65 61 64 73 0a 2a 2a 20 63 61  ot threads.** ca
ce380 6e 20 6f 76 65 72 72 69 64 65 20 65 61 63 68 20  n override each 
ce390 6f 74 68 65 72 73 20 6c 6f 63 6b 73 20 74 68 65  others locks the
ce3a0 6e 20 73 65 74 73 20 74 68 65 20 0a 2a 2a 20 74  n sets the .** t
ce3b0 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45 61  hreadsOverrideEa
ce3c0 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 76 61  chOthersLocks va
ce3d0 72 69 61 62 6c 65 20 61 70 70 72 6f 70 72 69 61  riable appropria
ce3e0 74 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tely..*/.static 
ce3f0 76 6f 69 64 20 74 65 73 74 54 68 72 65 61 64 4c  void testThreadL
ce400 6f 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 69  ockingBehavior(i
ce410 6e 74 20 66 64 5f 6f 72 69 67 29 7b 0a 20 20 69  nt fd_orig){.  i
ce420 6e 74 20 66 64 3b 0a 20 20 69 6e 74 20 72 63 3b  nt fd;.  int rc;
ce430 0a 20 20 73 74 72 75 63 74 20 74 68 72 65 61 64  .  struct thread
ce440 54 65 73 74 44 61 74 61 20 64 3b 0a 20 20 73 74  TestData d;.  st
ce450 72 75 63 74 20 66 6c 6f 63 6b 20 6c 3b 0a 20 20  ruct flock l;.  
ce460 70 74 68 72 65 61 64 5f 74 20 74 3b 0a 0a 20 20  pthread_t t;..  
ce470 66 64 20 3d 20 64 75 70 28 66 64 5f 6f 72 69 67  fd = dup(fd_orig
ce480 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 20  );.  if( fd<0 ) 
ce490 72 65 74 75 72 6e 3b 0a 20 20 6d 65 6d 73 65 74  return;.  memset
ce4a0 28 26 6c 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6c  (&l, 0, sizeof(l
ce4b0 29 29 3b 0a 20 20 6c 2e 6c 5f 74 79 70 65 20 3d  ));.  l.l_type =
ce4c0 20 46 5f 52 44 4c 43 4b 3b 0a 20 20 6c 2e 6c 5f   F_RDLCK;.  l.l_
ce4d0 6c 65 6e 20 3d 20 31 3b 0a 20 20 6c 2e 6c 5f 73  len = 1;.  l.l_s
ce4e0 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 2e 6c 5f  tart = 0;.  l.l_
ce4f0 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
ce500 54 3b 0a 20 20 72 63 20 3d 20 66 63 6e 74 6c 28  T;.  rc = fcntl(
ce510 66 64 5f 6f 72 69 67 2c 20 46 5f 53 45 54 4c 4b  fd_orig, F_SETLK
ce520 2c 20 26 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  , &l);.  if( rc!
ce530 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6d  =0 ) return;.  m
ce540 65 6d 73 65 74 28 26 64 2c 20 30 2c 20 73 69 7a  emset(&d, 0, siz
ce550 65 6f 66 28 64 29 29 3b 0a 20 20 64 2e 66 64 20  eof(d));.  d.fd 
ce560 3d 20 66 64 3b 0a 20 20 64 2e 6c 6f 63 6b 20 3d  = fd;.  d.lock =
ce570 20 6c 3b 0a 20 20 64 2e 6c 6f 63 6b 2e 6c 5f 74   l;.  d.lock.l_t
ce580 79 70 65 20 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20  ype = F_WRLCK;. 
ce590 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28   pthread_create(
ce5a0 26 74 2c 20 30 2c 20 74 68 72 65 61 64 4c 6f 63  &t, 0, threadLoc
ce5b0 6b 69 6e 67 54 65 73 74 2c 20 26 64 29 3b 0a 20  kingTest, &d);. 
ce5c0 20 70 74 68 72 65 61 64 5f 6a 6f 69 6e 28 74 2c   pthread_join(t,
ce5d0 20 30 29 3b 0a 20 20 63 6c 6f 73 65 28 66 64 29   0);.  close(fd)
ce5e0 3b 0a 20 20 69 66 28 20 64 2e 72 65 73 75 6c 74  ;.  if( d.result
ce5f0 21 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  !=0 ) return;.  
ce600 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
ce610 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 3d  achOthersLocks =
ce620 20 28 64 2e 6c 6f 63 6b 2e 6c 5f 74 79 70 65 3d   (d.lock.l_type=
ce630 3d 46 5f 55 4e 4c 43 4b 29 3b 0a 7d 0a 23 65 6c  =F_UNLCK);.}.#el
ce640 73 65 0a 2f 2a 0a 2a 2a 20 4f 6e 20 61 6e 79 74  se./*.** On anyt
ce650 68 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20  hing other than 
ce660 6c 69 6e 75 78 2c 20 61 73 73 75 6d 65 20 74 68  linux, assume th
ce670 72 65 61 64 73 20 6f 76 65 72 72 69 64 65 20 65  reads override e
ce680 61 63 68 20 6f 74 68 65 72 73 20 6c 6f 63 6b 73  ach others locks
ce690 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
ce6a0 20 74 65 73 74 54 68 72 65 61 64 4c 6f 63 6b 69   testThreadLocki
ce6b0 6e 67 42 65 68 61 76 69 6f 72 28 69 6e 74 20 66  ngBehavior(int f
ce6c0 64 5f 6f 72 69 67 29 7b 0a 20 20 74 68 72 65 61  d_orig){.  threa
ce6d0 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f 74  dsOverrideEachOt
ce6e0 68 65 72 73 4c 6f 63 6b 73 20 3d 20 31 3b 0a 7d  hersLocks = 1;.}
ce6f0 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 5f 6c 69 6e  .#endif /* __lin
ce700 75 78 5f 5f 20 2a 2f 0a 0a 23 65 6e 64 69 66 20  ux__ */..#endif 
ce710 2f 2a 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  /* SQLITE_THREAD
ce720 53 41 46 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  SAFE */../*.** R
ce730 65 6c 65 61 73 65 20 61 20 6c 6f 63 6b 49 6e 66  elease a lockInf
ce740 6f 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76  o structure prev
ce750 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64  iously allocated
ce760 20 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f   by findLockInfo
ce770 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
ce780 69 64 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e  id releaseLockIn
ce790 66 6f 28 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e  fo(struct lockIn
ce7a0 66 6f 20 2a 70 4c 6f 63 6b 29 7b 0a 20 20 69 66  fo *pLock){.  if
ce7b0 28 20 70 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 70  ( pLock ){.    p
ce7c0 4c 6f 63 6b 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20  Lock->nRef--;.  
ce7d0 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 6e 52 65    if( pLock->nRe
ce7e0 66 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66  f==0 ){.      if
ce7f0 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 20 29  ( pLock->pPrev )
ce800 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
ce810 28 20 70 4c 6f 63 6b 2d 3e 70 50 72 65 76 2d 3e  ( pLock->pPrev->
ce820 70 4e 65 78 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a  pNext==pLock );.
ce830 20 20 20 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70          pLock->p
ce840 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4c  Prev->pNext = pL
ce850 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ock->pNext;.    
ce860 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
ce870 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 4c 69 73   assert( lockLis
ce880 74 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20 20  t==pLock );.    
ce890 20 20 20 20 6c 6f 63 6b 4c 69 73 74 20 3d 20 70      lockList = p
ce8a0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  Lock->pNext;.   
ce8b0 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
ce8c0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  Lock->pNext ){. 
ce8d0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
ce8e0 4c 6f 63 6b 2d 3e 70 4e 65 78 74 2d 3e 70 50 72  Lock->pNext->pPr
ce8f0 65 76 3d 3d 70 4c 6f 63 6b 20 29 3b 0a 20 20 20  ev==pLock );.   
ce900 20 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 4e 65 78       pLock->pNex
ce910 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c 6f 63 6b  t->pPrev = pLock
ce920 2d 3e 70 50 72 65 76 3b 0a 20 20 20 20 20 20 7d  ->pPrev;.      }
ce930 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66  .      sqlite3_f
ce940 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20 20 20 20  ree(pLock);.    
ce950 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
ce960 65 6c 65 61 73 65 20 61 20 6f 70 65 6e 43 6e 74  elease a openCnt
ce970 20 73 74 72 75 63 74 75 72 65 20 70 72 65 76 69   structure previ
ce980 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20  ously allocated 
ce990 62 79 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28  by findLockInfo(
ce9a0 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  )..*/.static voi
ce9b0 64 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74  d releaseOpenCnt
ce9c0 28 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20  (struct openCnt 
ce9d0 2a 70 4f 70 65 6e 29 7b 0a 20 20 69 66 28 20 70  *pOpen){.  if( p
ce9e0 4f 70 65 6e 20 29 7b 0a 20 20 20 20 70 4f 70 65  Open ){.    pOpe
ce9f0 6e 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 20 20 69  n->nRef--;.    i
cea00 66 28 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 3d 3d  f( pOpen->nRef==
cea10 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
cea20 4f 70 65 6e 2d 3e 70 50 72 65 76 20 29 7b 0a 20  Open->pPrev ){. 
cea30 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
cea40 4f 70 65 6e 2d 3e 70 50 72 65 76 2d 3e 70 4e 65  Open->pPrev->pNe
cea50 78 74 3d 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20  xt==pOpen );.   
cea60 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 50 72 65       pOpen->pPre
cea70 76 2d 3e 70 4e 65 78 74 20 3d 20 70 4f 70 65 6e  v->pNext = pOpen
cea80 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 7d  ->pNext;.      }
cea90 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
ceaa0 73 65 72 74 28 20 6f 70 65 6e 4c 69 73 74 3d 3d  sert( openList==
ceab0 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20 20  pOpen );.       
ceac0 20 6f 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65   openList = pOpe
cead0 6e 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  n->pNext;.      
ceae0 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 65  }.      if( pOpe
ceaf0 6e 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  n->pNext ){.    
ceb00 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 65      assert( pOpe
ceb10 6e 2d 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 3d  n->pNext->pPrev=
ceb20 3d 70 4f 70 65 6e 20 29 3b 0a 20 20 20 20 20 20  =pOpen );.      
ceb30 20 20 70 4f 70 65 6e 2d 3e 70 4e 65 78 74 2d 3e    pOpen->pNext->
ceb40 70 50 72 65 76 20 3d 20 70 4f 70 65 6e 2d 3e 70  pPrev = pOpen->p
ceb50 50 72 65 76 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Prev;.      }.  
ceb60 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
ceb70 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67  (pOpen->aPending
ceb80 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
ceb90 5f 66 72 65 65 28 70 4f 70 65 6e 29 3b 0a 20 20  _free(pOpen);.  
ceba0 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 49    }.  }.}..#if I
cebb0 53 5f 56 58 57 4f 52 4b 53 0a 2f 2a 0a 2a 2a 20  S_VXWORKS./*.** 
cebc0 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
cebd0 66 20 61 20 72 65 61 6c 70 61 74 68 28 29 20 6c  f a realpath() l
cebe0 69 6b 65 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  ike function for
cebf0 20 76 78 57 6f 72 6b 73 0a 2a 2a 20 74 6f 20 64   vxWorks.** to d
cec00 65 74 65 72 6d 69 6e 65 20 63 61 6e 6f 6e 69 63  etermine canonic
cec10 61 6c 20 70 61 74 68 20 6e 61 6d 65 20 66 72 6f  al path name fro
cec20 6d 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20 49 74  m given name. It
cec30 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 73 75 70   does.** not sup
cec40 70 6f 72 74 20 73 79 6d 6c 69 6e 6b 73 2e 20 4e  port symlinks. N
cec50 65 69 74 68 65 72 20 64 6f 65 73 20 69 74 20 68  either does it h
cec60 61 6e 64 6c 65 20 76 6f 6c 75 6d 65 20 70 72 65  andle volume pre
cec70 66 69 78 65 73 2e 0a 2a 2f 0a 63 68 61 72 20 2a  fixes..*/.char *
cec80 0a 76 78 72 65 61 6c 70 61 74 68 28 63 6f 6e 73  .vxrealpath(cons
cec90 74 20 63 68 61 72 20 2a 70 61 74 68 6e 61 6d 65  t char *pathname
ceca0 2c 20 69 6e 74 20 64 6f 73 74 61 74 29 0a 7b 0a  , int dostat).{.
cecb0 20 20 73 74 72 75 63 74 20 73 74 61 74 20 73 62    struct stat sb
cecc0 75 66 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20  uf;.  int len;. 
cecd0 20 63 68 61 72 20 2a 77 68 65 72 65 2c 20 2a 70   char *where, *p
cece0 74 72 2c 20 2a 6c 61 73 74 3b 0a 20 20 63 68 61  tr, *last;.  cha
cecf0 72 20 2a 72 65 73 75 6c 74 2c 20 2a 63 75 72 70  r *result, *curp
ced00 61 74 68 2c 20 2a 77 6f 72 6b 70 61 74 68 2c 20  ath, *workpath, 
ced10 2a 6e 61 6d 65 62 75 66 3b 0a 0a 20 20 6c 65 6e  *namebuf;..  len
ced20 20 3d 20 70 61 74 68 63 6f 6e 66 28 70 61 74 68   = pathconf(path
ced30 6e 61 6d 65 2c 20 5f 50 43 5f 50 41 54 48 5f 4d  name, _PC_PATH_M
ced40 41 58 29 3b 0a 20 20 69 66 28 20 6c 65 6e 3c 30  AX);.  if( len<0
ced50 20 29 7b 0a 20 20 20 20 6c 65 6e 20 3d 20 50 41   ){.    len = PA
ced60 54 48 5f 4d 41 58 3b 0a 20 20 7d 0a 20 20 72 65  TH_MAX;.  }.  re
ced70 73 75 6c 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d  sult = sqlite3_m
ced80 61 6c 6c 6f 63 28 6c 65 6e 20 2a 20 34 29 3b 0a  alloc(len * 4);.
ced90 20 20 69 66 28 20 21 72 65 73 75 6c 74 20 29 7b    if( !result ){
ceda0 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
cedb0 20 7d 0a 20 20 63 75 72 70 61 74 68 20 3d 20 72   }.  curpath = r
cedc0 65 73 75 6c 74 20 2b 20 6c 65 6e 3b 0a 20 20 77  esult + len;.  w
cedd0 6f 72 6b 70 61 74 68 20 3d 20 63 75 72 70 61 74  orkpath = curpat
cede0 68 20 2b 20 6c 65 6e 3b 0a 20 20 6e 61 6d 65 62  h + len;.  nameb
cedf0 75 66 20 3d 20 77 6f 72 6b 70 61 74 68 20 2b 20  uf = workpath + 
cee00 6c 65 6e 3b 0a 20 20 73 74 72 63 70 79 28 63 75  len;.  strcpy(cu
cee10 72 70 61 74 68 2c 20 70 61 74 68 6e 61 6d 65 29  rpath, pathname)
cee20 3b 0a 20 20 69 66 28 20 2a 70 61 74 68 6e 61 6d  ;.  if( *pathnam
cee30 65 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 69 66  e!='/' ){.    if
cee40 28 20 21 67 65 74 63 77 64 28 77 6f 72 6b 70 61  ( !getcwd(workpa
cee50 74 68 2c 20 6c 65 6e 29 20 29 7b 0a 20 20 20 20  th, len) ){.    
cee60 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72    sqlite3_free(r
cee70 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 72 65  esult);.      re
cee80 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20  turn 0;.    }.  
cee90 7d 65 6c 73 65 7b 0a 20 20 20 20 2a 77 6f 72 6b  }else{.    *work
ceea0 70 61 74 68 20 3d 20 27 5c 30 27 3b 0a 20 20 7d  path = '\0';.  }
ceeb0 0a 20 20 77 68 65 72 65 20 3d 20 63 75 72 70 61  .  where = curpa
ceec0 74 68 3b 0a 20 20 77 68 69 6c 65 28 20 2a 77 68  th;.  while( *wh
ceed0 65 72 65 20 29 7b 0a 20 20 20 20 69 66 28 20 21  ere ){.    if( !
ceee0 73 74 72 63 6d 70 28 77 68 65 72 65 2c 20 22 2e  strcmp(where, ".
ceef0 22 29 20 29 7b 0a 20 20 20 20 20 20 77 68 65 72  ") ){.      wher
cef00 65 2b 2b 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  e++;.      conti
cef10 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nue;.    }.    i
cef20 66 28 20 21 73 74 72 6e 63 6d 70 28 77 68 65 72  f( !strncmp(wher
cef30 65 2c 20 22 2e 2f 22 2c 20 32 29 20 29 7b 0a 20  e, "./", 2) ){. 
cef40 20 20 20 20 20 77 68 65 72 65 20 2b 3d 20 32 3b       where += 2;
cef50 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
cef60 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21  .    }.    if( !
cef70 73 74 72 6e 63 6d 70 28 77 68 65 72 65 2c 20 22  strncmp(where, "
cef80 2e 2e 2f 22 2c 20 33 29 20 29 7b 0a 20 20 20 20  ../", 3) ){.    
cef90 20 20 77 68 65 72 65 20 2b 3d 20 33 3b 0a 20 20    where += 3;.  
cefa0 20 20 20 20 70 74 72 20 3d 20 6c 61 73 74 20 3d      ptr = last =
cefb0 20 77 6f 72 6b 70 61 74 68 3b 0a 20 20 20 20 20   workpath;.     
cefc0 20 77 68 69 6c 65 28 20 2a 70 74 72 20 29 7b 0a   while( *ptr ){.
cefd0 20 20 20 20 20 20 20 20 69 66 28 20 2a 70 74 72          if( *ptr
cefe0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20  =='/' ){.       
ceff0 20 20 20 6c 61 73 74 20 3d 20 70 74 72 3b 0a 20     last = ptr;. 
cf000 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
cf010 20 70 74 72 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a   ptr++;.      }.
cf020 20 20 20 20 20 20 2a 6c 61 73 74 20 3d 20 27 5c        *last = '\
cf030 30 27 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  0';.      contin
cf040 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 74  ue;.    }.    pt
cf050 72 20 3d 20 73 74 72 63 68 72 28 77 68 65 72 65  r = strchr(where
cf060 2c 20 27 2f 27 29 3b 0a 20 20 20 20 69 66 28 20  , '/');.    if( 
cf070 21 70 74 72 20 29 7b 0a 20 20 20 20 20 20 70 74  !ptr ){.      pt
cf080 72 20 3d 20 77 68 65 72 65 20 2b 20 73 74 72 6c  r = where + strl
cf090 65 6e 28 77 68 65 72 65 29 20 2d 20 31 3b 0a 20  en(where) - 1;. 
cf0a0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
cf0b0 2a 70 74 72 20 3d 20 27 5c 30 27 3b 0a 20 20 20  *ptr = '\0';.   
cf0c0 20 7d 0a 20 20 20 20 73 74 72 63 70 79 28 6e 61   }.    strcpy(na
cf0d0 6d 65 62 75 66 2c 20 77 6f 72 6b 70 61 74 68 29  mebuf, workpath)
cf0e0 3b 0a 20 20 20 20 66 6f 72 28 20 6c 61 73 74 20  ;.    for( last 
cf0f0 3d 20 6e 61 6d 65 62 75 66 3b 20 2a 6c 61 73 74  = namebuf; *last
cf100 3b 20 6c 61 73 74 2b 2b 20 29 7b 0a 20 20 20 20  ; last++ ){.    
cf110 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
cf120 7d 0a 20 20 20 20 69 66 28 20 2a 2d 2d 6c 61 73  }.    if( *--las
cf130 74 21 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20  t!='/' ){.      
cf140 73 74 72 63 61 74 28 6e 61 6d 65 62 75 66 2c 20  strcat(namebuf, 
cf150 22 2f 22 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  "/");.    }.    
cf160 73 74 72 63 61 74 28 6e 61 6d 65 62 75 66 2c 20  strcat(namebuf, 
cf170 77 68 65 72 65 29 3b 0a 20 20 20 20 77 68 65 72  where);.    wher
cf180 65 20 3d 20 2b 2b 70 74 72 3b 0a 20 20 20 20 69  e = ++ptr;.    i
cf190 66 28 20 64 6f 73 74 61 74 20 29 7b 0a 20 20 20  f( dostat ){.   
cf1a0 20 20 20 69 66 28 20 73 74 61 74 28 6e 61 6d 65     if( stat(name
cf1b0 62 75 66 2c 20 26 73 62 75 66 29 3d 3d 2d 31 20  buf, &sbuf)==-1 
cf1c0 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
cf1d0 65 33 5f 66 72 65 65 28 72 65 73 75 6c 74 29 3b  e3_free(result);
cf1e0 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
cf1f0 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  0;.      }.     
cf200 20 69 66 28 20 28 73 62 75 66 2e 73 74 5f 6d 6f   if( (sbuf.st_mo
cf210 64 65 20 26 20 53 5f 49 46 44 49 52 29 3d 3d 53  de & S_IFDIR)==S
cf220 5f 49 46 44 49 52 20 29 7b 0a 20 20 20 20 20 20  _IFDIR ){.      
cf230 20 20 73 74 72 63 70 79 28 77 6f 72 6b 70 61 74    strcpy(workpat
cf240 68 2c 20 6e 61 6d 65 62 75 66 29 3b 0a 20 20 20  h, namebuf);.   
cf250 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
cf260 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
cf270 20 2a 77 68 65 72 65 20 29 7b 0a 20 20 20 20 20   *where ){.     
cf280 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
cf290 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20  result);.       
cf2a0 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20 20   return 0;.     
cf2b0 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 74 72   }.    }.    str
cf2c0 63 70 79 28 77 6f 72 6b 70 61 74 68 2c 20 6e 61  cpy(workpath, na
cf2d0 6d 65 62 75 66 29 3b 0a 20 20 7d 0a 20 20 73 74  mebuf);.  }.  st
cf2e0 72 63 70 79 28 72 65 73 75 6c 74 2c 20 77 6f 72  rcpy(result, wor
cf2f0 6b 70 61 74 68 29 3b 0a 20 20 72 65 74 75 72 6e  kpath);.  return
cf300 20 72 65 73 75 6c 74 3b 0a 7d 0a 23 65 6e 64 69   result;.}.#endi
cf310 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 45 4e  f..#if SQLITE_EN
cf320 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
cf330 4c 45 0a 2f 2a 0a 2a 2a 20 54 65 73 74 73 20 61  LE./*.** Tests a
cf340 20 62 79 74 65 2d 72 61 6e 67 65 20 6c 6f 63 6b   byte-range lock
cf350 69 6e 67 20 71 75 65 72 79 20 74 6f 20 73 65 65  ing query to see
cf360 20 69 66 20 62 79 74 65 20 72 61 6e 67 65 20 6c   if byte range l
cf370 6f 63 6b 73 20 61 72 65 20 0a 2a 2a 20 73 75 70  ocks are .** sup
cf380 70 6f 72 74 65 64 2c 20 69 66 20 6e 6f 74 20 77  ported, if not w
cf390 65 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 64  e fall back to d
cf3a0 6f 74 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 53 74 79  otlockLockingSty
cf3b0 6c 65 2e 0a 2a 2a 20 4f 6e 20 76 78 57 6f 72 6b  le..** On vxWork
cf3c0 73 20 77 65 20 66 61 6c 6c 20 62 61 63 6b 20 74  s we fall back t
cf3d0 6f 20 6e 61 6d 65 64 73 65 6d 4c 6f 63 6b 69 6e  o namedsemLockin
cf3e0 67 53 74 79 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  gStyle..*/.stati
cf3f0 63 20 69 6e 74 20 74 65 73 74 4c 6f 63 6b 69 6e  c int testLockin
cf400 67 53 74 79 6c 65 28 69 6e 74 20 66 64 29 7b 0a  gStyle(int fd){.
cf410 20 20 73 74 72 75 63 74 20 66 6c 6f 63 6b 20 6c    struct flock l
cf420 6f 63 6b 49 6e 66 6f 3b 0a 0a 20 20 2f 2a 20 54  ockInfo;..  /* T
cf430 65 73 74 20 62 79 74 65 2d 72 61 6e 67 65 20 6c  est byte-range l
cf440 6f 63 6b 20 75 73 69 6e 67 20 66 63 6e 74 6c 28  ock using fcntl(
cf450 29 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 20 73  ). If the call s
cf460 75 63 63 65 65 64 73 2c 20 0a 20 20 2a 2a 20 61  ucceeds, .  ** a
cf470 73 73 75 6d 65 20 74 68 61 74 20 74 68 65 20 66  ssume that the f
cf480 69 6c 65 2d 73 79 73 74 65 6d 20 73 75 70 70 6f  ile-system suppo
cf490 72 74 73 20 50 4f 53 49 58 20 73 74 79 6c 65 20  rts POSIX style 
cf4a0 6c 6f 63 6b 73 2e 20 0a 20 20 2a 2f 0a 20 20 6c  locks. .  */.  l
cf4b0 6f 63 6b 49 6e 66 6f 2e 6c 5f 6c 65 6e 20 3d 20  ockInfo.l_len = 
cf4c0 31 3b 0a 20 20 6c 6f 63 6b 49 6e 66 6f 2e 6c 5f  1;.  lockInfo.l_
cf4d0 73 74 61 72 74 20 3d 20 30 3b 0a 20 20 6c 6f 63  start = 0;.  loc
cf4e0 6b 49 6e 66 6f 2e 6c 5f 77 68 65 6e 63 65 20 3d  kInfo.l_whence =
cf4f0 20 53 45 45 4b 5f 53 45 54 3b 0a 20 20 6c 6f 63   SEEK_SET;.  loc
cf500 6b 49 6e 66 6f 2e 6c 5f 74 79 70 65 20 3d 20 46  kInfo.l_type = F
cf510 5f 52 44 4c 43 4b 3b 0a 20 20 69 66 28 20 66 63  _RDLCK;.  if( fc
cf520 6e 74 6c 28 66 64 2c 20 46 5f 47 45 54 4c 4b 2c  ntl(fd, F_GETLK,
cf530 20 26 6c 6f 63 6b 49 6e 66 6f 29 21 3d 2d 31 20   &lockInfo)!=-1 
cf540 29 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 4c  ) {.    return L
cf550 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53  OCKING_STYLE_POS
cf560 49 58 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20  IX;.  }.  .  /* 
cf570 54 65 73 74 69 6e 67 20 66 6f 72 20 66 6c 6f 63  Testing for floc
cf580 6b 28 29 20 63 61 6e 20 67 69 76 65 20 66 61 6c  k() can give fal
cf590 73 65 20 70 6f 73 69 74 69 76 65 73 2e 20 20 53  se positives.  S
cf5a0 6f 20 69 66 20 69 66 20 74 68 65 20 61 62 6f 76  o if if the abov
cf5b0 65 20 0a 20 20 2a 2a 20 74 65 73 74 20 66 61 69  e .  ** test fai
cf5c0 6c 73 2c 20 74 68 65 6e 20 77 65 20 66 61 6c 6c  ls, then we fall
cf5d0 20 62 61 63 6b 20 74 6f 20 75 73 69 6e 67 20 64   back to using d
cf5e0 6f 74 2d 66 69 6c 65 20 73 74 79 6c 65 20 6c 6f  ot-file style lo
cf5f0 63 6b 69 6e 67 20 28 6f 72 0a 20 20 2a 2a 20 6e  cking (or.  ** n
cf600 61 6d 65 64 2d 73 65 6d 61 70 68 6f 72 65 20 6c  amed-semaphore l
cf610 6f 63 6b 69 6e 67 20 6f 6e 20 76 78 77 6f 72 6b  ocking on vxwork
cf620 73 29 2e 0a 20 20 2a 2f 0a 20 20 72 65 74 75 72  s)..  */.  retur
cf630 6e 20 28 49 53 5f 56 58 57 4f 52 4b 53 20 3f 20  n (IS_VXWORKS ? 
cf640 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 41  LOCKING_STYLE_NA
cf650 4d 45 44 53 45 4d 20 3a 20 4c 4f 43 4b 49 4e 47  MEDSEM : LOCKING
cf660 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 29 3b  _STYLE_DOTFILE);
cf670 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 0a 2a  .}.#endif../* .*
cf680 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  * If SQLITE_ENAB
cf690 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  LE_LOCKING_STYLE
cf6a0 20 69 73 20 64 65 66 69 6e 65 64 2c 20 74 68 69   is defined, thi
cf6b0 73 20 66 75 6e 63 74 69 6f 6e 20 45 78 61 6d 69  s function Exami
cf6c0 6e 65 73 20 74 68 65 20 0a 2a 2a 20 66 5f 66 73  nes the .** f_fs
cf6d0 74 79 70 65 6e 61 6d 65 20 65 6e 74 72 79 20 69  typename entry i
cf6e0 6e 20 74 68 65 20 73 74 61 74 66 73 20 73 74 72  n the statfs str
cf6f0 75 63 74 75 72 65 20 61 73 20 72 65 74 75 72 6e  ucture as return
cf700 65 64 20 62 79 20 73 74 61 74 28 29 20 66 6f 72  ed by stat() for
cf710 20 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 73 79   .** the file sy
cf720 73 74 65 6d 20 68 6f 73 74 69 6e 67 20 74 68 65  stem hosting the
cf730 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
cf740 6e 64 20 73 65 6c 65 63 74 73 20 20 74 68 65 20  nd selects  the 
cf750 61 70 70 72 6f 70 72 69 61 74 65 0a 2a 2a 20 6c  appropriate.** l
cf760 6f 63 6b 69 6e 67 20 73 74 79 6c 65 20 62 61 73  ocking style bas
cf770 65 64 20 6f 6e 20 69 74 73 20 76 61 6c 75 65 2e  ed on its value.
cf780 20 20 54 68 65 73 65 20 76 61 6c 75 65 73 20 61    These values a
cf790 6e 64 20 61 73 73 69 67 6e 6d 65 6e 74 73 20 61  nd assignments a
cf7a0 72 65 20 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20  re .** based on 
cf7b0 44 61 72 77 69 6e 2f 4f 53 58 20 62 65 68 61 76  Darwin/OSX behav
cf7c0 69 6f 72 20 61 6e 64 20 68 61 76 65 20 6e 6f 74  ior and have not
cf7d0 20 62 65 65 6e 20 74 68 6f 72 6f 75 67 68 6c 79   been thoroughly
cf7e0 20 74 65 73 74 65 64 20 6f 6e 20 0a 2a 2a 20 6f   tested on .** o
cf7f0 74 68 65 72 20 73 79 73 74 65 6d 73 2e 0a 2a 2a  ther systems..**
cf800 0a 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f 45 4e  .** If SQLITE_EN
cf810 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
cf820 4c 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65  LE is not define
cf830 64 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  d, this function
cf840 20 61 6c 77 61 79 73 0a 2a 2a 20 72 65 74 75 72   always.** retur
cf850 6e 73 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  ns LOCKING_STYLE
cf860 5f 50 4f 53 49 58 2e 0a 2a 2f 0a 23 69 66 20 53  _POSIX..*/.#if S
cf870 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43  QLITE_ENABLE_LOC
cf880 4b 49 4e 47 5f 53 54 59 4c 45 0a 73 74 61 74 69  KING_STYLE.stati
cf890 63 20 69 6e 74 20 64 65 74 65 63 74 4c 6f 63 6b  c int detectLock
cf8a0 69 6e 67 53 74 79 6c 65 28 0a 20 20 73 71 6c 69  ingStyle(.  sqli
cf8b0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 0a 20  te3_vfs *pVfs,. 
cf8c0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69 6c   const char *fil
cf8d0 65 50 61 74 68 2c 20 0a 20 20 69 6e 74 20 66 64  ePath, .  int fd
cf8e0 0a 29 7b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52  .){.#if IS_VXWOR
cf8f0 4b 53 0a 20 20 69 66 28 20 21 66 69 6c 65 50 61  KS.  if( !filePa
cf900 74 68 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  th ){.    return
cf910 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e   LOCKING_STYLE_N
cf920 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  ONE;.  }.  if( p
cf930 56 66 73 2d 3e 70 41 70 70 44 61 74 61 20 29 7b  Vfs->pAppData ){
cf940 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
cf950 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 70 56  TE_PTR_TO_INT(pV
cf960 66 73 2d 3e 70 41 70 70 44 61 74 61 29 3b 0a 20  fs->pAppData);. 
cf970 20 7d 0a 20 20 69 66 20 28 61 63 63 65 73 73 28   }.  if (access(
cf980 66 69 6c 65 50 61 74 68 2c 20 30 29 20 21 3d 20  filePath, 0) != 
cf990 2d 31 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  -1){.    return 
cf9a0 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74 79 6c 65  testLockingStyle
cf9b0 28 66 64 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a  (fd);.  }.#else.
cf9c0 20 20 73 74 72 75 63 74 20 4d 61 70 70 69 6e 67    struct Mapping
cf9d0 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61   {.    const cha
cf9e0 72 20 2a 7a 46 69 6c 65 73 79 73 74 65 6d 3b 0a  r *zFilesystem;.
cf9f0 20 20 20 20 69 6e 74 20 65 4c 6f 63 6b 69 6e 67      int eLocking
cfa00 53 74 79 6c 65 3b 0a 20 20 7d 20 61 4d 61 70 5b  Style;.  } aMap[
cfa10 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22 68 66 73  ] = {.    { "hfs
cfa20 22 2c 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54  ",    LOCKING_ST
cfa30 59 4c 45 5f 50 4f 53 49 58 20 7d 2c 0a 20 20 20  YLE_POSIX },.   
cfa40 20 7b 20 22 75 66 73 22 2c 20 20 20 20 4c 4f 43   { "ufs",    LOC
cfa50 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58  KING_STYLE_POSIX
cfa60 20 7d 2c 0a 20 20 20 20 7b 20 22 61 66 70 66 73   },.    { "afpfs
cfa70 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  ",  LOCKING_STYL
cfa80 45 5f 41 46 50 20 7d 2c 0a 23 69 66 64 65 66 20  E_AFP },.#ifdef 
cfa90 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41 46  SQLITE_ENABLE_AF
cfaa0 50 5f 4c 4f 43 4b 49 4e 47 5f 53 4d 42 0a 20 20  P_LOCKING_SMB.  
cfab0 20 20 7b 20 22 73 6d 62 66 73 22 2c 20 20 4c 4f    { "smbfs",  LO
cfac0 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 41 46 50 20  CKING_STYLE_AFP 
cfad0 7d 2c 0a 23 65 6c 73 65 0a 20 20 20 20 7b 20 22  },.#else.    { "
cfae0 73 6d 62 66 73 22 2c 20 20 4c 4f 43 4b 49 4e 47  smbfs",  LOCKING
cfaf0 5f 53 54 59 4c 45 5f 46 4c 4f 43 4b 20 7d 2c 0a  _STYLE_FLOCK },.
cfb00 23 65 6e 64 69 66 0a 20 20 20 20 7b 20 22 6d 73  #endif.    { "ms
cfb10 64 6f 73 22 2c 20 20 4c 4f 43 4b 49 4e 47 5f 53  dos",  LOCKING_S
cfb20 54 59 4c 45 5f 44 4f 54 46 49 4c 45 20 7d 2c 0a  TYLE_DOTFILE },.
cfb30 20 20 20 20 7b 20 22 77 65 62 64 61 76 22 2c 20      { "webdav", 
cfb40 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f  LOCKING_STYLE_NO
cfb50 4e 45 20 7d 2c 0a 20 20 20 20 7b 20 30 2c 20 30  NE },.    { 0, 0
cfb60 20 7d 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b   }.  };.  int i;
cfb70 0a 20 20 73 74 72 75 63 74 20 73 74 61 74 66 73  .  struct statfs
cfb80 20 66 73 49 6e 66 6f 3b 0a 0a 20 20 69 66 28 20   fsInfo;..  if( 
cfb90 21 66 69 6c 65 50 61 74 68 20 29 7b 0a 20 20 20  !filePath ){.   
cfba0 20 72 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f   return LOCKING_
cfbb0 53 54 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a  STYLE_NONE;.  }.
cfbc0 20 20 69 66 28 20 70 56 66 73 2d 3e 70 41 70 70    if( pVfs->pApp
cfbd0 44 61 74 61 20 29 7b 0a 20 20 20 20 72 65 74 75  Data ){.    retu
cfbe0 72 6e 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f  rn SQLITE_PTR_TO
cfbf0 5f 49 4e 54 28 70 56 66 73 2d 3e 70 41 70 70 44  _INT(pVfs->pAppD
cfc00 61 74 61 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ata);.  }..  if(
cfc10 20 73 74 61 74 66 73 28 66 69 6c 65 50 61 74 68   statfs(filePath
cfc20 2c 20 26 66 73 49 6e 66 6f 29 20 21 3d 20 2d 31  , &fsInfo) != -1
cfc30 20 29 7b 0a 20 20 20 20 69 66 28 20 66 73 49 6e   ){.    if( fsIn
cfc40 66 6f 2e 66 5f 66 6c 61 67 73 20 26 20 4d 4e 54  fo.f_flags & MNT
cfc50 5f 52 44 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20  _RDONLY ){.     
cfc60 20 72 65 74 75 72 6e 20 4c 4f 43 4b 49 4e 47 5f   return LOCKING_
cfc70 53 54 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 20 20  STYLE_NONE;.    
cfc80 7d 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 61  }.    for(i=0; a
cfc90 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73 74  Map[i].zFilesyst
cfca0 65 6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  em; i++){.      
cfcb0 69 66 28 20 73 74 72 63 6d 70 28 66 73 49 6e 66  if( strcmp(fsInf
cfcc0 6f 2e 66 5f 66 73 74 79 70 65 6e 61 6d 65 2c 20  o.f_fstypename, 
cfcd0 61 4d 61 70 5b 69 5d 2e 7a 46 69 6c 65 73 79 73  aMap[i].zFilesys
cfce0 74 65 6d 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  tem)==0 ){.     
cfcf0 20 20 20 72 65 74 75 72 6e 20 61 4d 61 70 5b 69     return aMap[i
cfd00 5d 2e 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 3b  ].eLockingStyle;
cfd10 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
cfd20 20 7d 0a 0a 20 20 2f 2a 20 44 65 66 61 75 6c 74   }..  /* Default
cfd30 20 63 61 73 65 2e 20 48 61 6e 64 6c 65 73 2c 20   case. Handles, 
cfd40 61 6d 6f 6e 67 73 74 20 6f 74 68 65 72 73 2c 20  amongst others, 
cfd50 22 6e 66 73 22 2e 20 2a 2f 0a 20 20 72 65 74 75  "nfs". */.  retu
cfd60 72 6e 20 74 65 73 74 4c 6f 63 6b 69 6e 67 53 74  rn testLockingSt
cfd70 79 6c 65 28 66 64 29 3b 20 20 0a 23 65 6e 64 69  yle(fd);  .#endi
cfd80 66 20 2f 2a 20 69 66 20 49 53 5f 56 58 57 4f 52  f /* if IS_VXWOR
cfd90 4b 53 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 4c  KS */.  return L
cfda0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53  OCKING_STYLE_POS
cfdb0 49 58 3b 0a 7d 0a 23 65 6c 73 65 0a 20 20 23 64  IX;.}.#else.  #d
cfdc0 65 66 69 6e 65 20 64 65 74 65 63 74 4c 6f 63 6b  efine detectLock
cfdd0 69 6e 67 53 74 79 6c 65 28 78 2c 79 2c 7a 29 20  ingStyle(x,y,z) 
cfde0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 50 4f  LOCKING_STYLE_PO
cfdf0 53 49 58 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  SIX.#endif /* if
cfe00 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
cfe10 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20  E_LOCKING_STYLE 
cfe20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20  */../*.** Given 
cfe30 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
cfe40 72 2c 20 6c 6f 63 61 74 65 20 6c 6f 63 6b 49 6e  r, locate lockIn
cfe50 66 6f 20 61 6e 64 20 6f 70 65 6e 43 6e 74 20 73  fo and openCnt s
cfe60 74 72 75 63 74 75 72 65 73 20 74 68 61 74 0a 2a  tructures that.*
cfe70 2a 20 64 65 73 63 72 69 62 65 73 20 74 68 61 74  * describes that
cfe80 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
cfe90 2e 20 20 43 72 65 61 74 65 20 6e 65 77 20 6f 6e  .  Create new on
cfea0 65 73 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  es if necessary.
cfeb0 20 20 54 68 65 0a 2a 2a 20 72 65 74 75 72 6e 20    The.** return 
cfec0 76 61 6c 75 65 73 20 6d 69 67 68 74 20 62 65 20  values might be 
cfed0 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 20 69 66  uninitialized if
cfee0 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
cfef0 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61  ..**.** Return a
cff00 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
cff10 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a 73 74 61  ror code..*/.sta
cff20 74 69 63 20 69 6e 74 20 66 69 6e 64 4c 6f 63 6b  tic int findLock
cff30 49 6e 66 6f 28 0a 20 20 69 6e 74 20 66 64 2c 20  Info(.  int fd, 
cff40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
cff50 20 20 20 20 20 2f 2a 20 54 68 65 20 66 69 6c 65       /* The file
cff60 20 64 65 73 63 72 69 70 74 6f 72 20 75 73 65 64   descriptor used
cff70 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f 0a 23   in the key */.#
cff80 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if IS_VXWORKS.  
cff90 76 6f 69 64 20 2a 72 6e 61 6d 2c 20 20 20 20 20  void *rnam,     
cffa0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
cffb0 76 78 57 6f 72 6b 73 20 72 65 61 6c 6e 61 6d 65  vxWorks realname
cffc0 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 73 74 72   */.#endif.  str
cffd0 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 2a 70  uct lockInfo **p
cffe0 70 4c 6f 63 6b 2c 20 20 20 20 2f 2a 20 52 65 74  pLock,    /* Ret
cfff0 75 72 6e 20 74 68 65 20 6c 6f 63 6b 49 6e 66 6f  urn the lockInfo
d0000 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65 20   structure here 
d0010 2a 2f 0a 20 20 73 74 72 75 63 74 20 6f 70 65 6e  */.  struct open
d0020 43 6e 74 20 2a 2a 70 70 4f 70 65 6e 20 20 20 20  Cnt **ppOpen    
d0030 20 20 2f 2a 20 52 65 74 75 72 6e 20 74 68 65 20    /* Return the 
d0040 6f 70 65 6e 43 6e 74 20 73 74 72 75 63 74 75 72  openCnt structur
d0050 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  e here */.){.  i
d0060 6e 74 20 72 63 3b 0a 20 20 73 74 72 75 63 74 20  nt rc;.  struct 
d0070 6c 6f 63 6b 4b 65 79 20 6b 65 79 31 3b 0a 20 20  lockKey key1;.  
d0080 73 74 72 75 63 74 20 6f 70 65 6e 4b 65 79 20 6b  struct openKey k
d0090 65 79 32 3b 0a 20 20 73 74 72 75 63 74 20 73 74  ey2;.  struct st
d00a0 61 74 20 73 74 61 74 62 75 66 3b 0a 20 20 73 74  at statbuf;.  st
d00b0 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20 2a 70  ruct lockInfo *p
d00c0 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74 20 6f  Lock;.  struct o
d00d0 70 65 6e 43 6e 74 20 2a 70 4f 70 65 6e 3b 0a 20  penCnt *pOpen;. 
d00e0 20 72 63 20 3d 20 66 73 74 61 74 28 66 64 2c 20   rc = fstat(fd, 
d00f0 26 73 74 61 74 62 75 66 29 3b 0a 20 20 69 66 28  &statbuf);.  if(
d0100 20 72 63 21 3d 30 20 29 7b 0a 23 69 66 64 65 66   rc!=0 ){.#ifdef
d0110 20 45 4f 56 45 52 46 4c 4f 57 0a 20 20 20 20 69   EOVERFLOW.    i
d0120 66 28 20 65 72 72 6e 6f 3d 3d 45 4f 56 45 52 46  f( errno==EOVERF
d0130 4c 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c  LOW ) return SQL
d0140 49 54 45 5f 4e 4f 4c 46 53 3b 0a 23 65 6e 64 69  ITE_NOLFS;.#endi
d0150 66 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  f.    return SQL
d0160 49 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 0a  ITE_IOERR;.  }..
d0170 20 20 2f 2a 20 4f 6e 20 4f 53 20 58 20 6f 6e 20    /* On OS X on 
d0180 61 6e 20 6d 73 64 6f 73 20 66 69 6c 65 73 79 73  an msdos filesys
d0190 74 65 6d 2c 20 74 68 65 20 69 6e 6f 64 65 20 6e  tem, the inode n
d01a0 75 6d 62 65 72 20 69 73 20 72 65 70 6f 72 74 65  umber is reporte
d01b0 64 0a 20 20 2a 2a 20 69 6e 63 6f 72 72 65 63 74  d.  ** incorrect
d01c0 6c 79 20 66 6f 72 20 7a 65 72 6f 2d 73 69 7a 65  ly for zero-size
d01d0 20 66 69 6c 65 73 2e 20 20 53 65 65 20 74 69 63   files.  See tic
d01e0 6b 65 74 20 23 33 32 36 30 2e 20 20 54 6f 20 77  ket #3260.  To w
d01f0 6f 72 6b 0a 20 20 2a 2a 20 61 72 6f 75 6e 64 20  ork.  ** around 
d0200 74 68 69 73 20 70 72 6f 62 6c 65 6d 20 28 77 65  this problem (we
d0210 20 63 6f 6e 73 69 64 65 72 20 69 74 20 61 20 62   consider it a b
d0220 75 67 20 69 6e 20 4f 53 20 58 2c 20 6e 6f 74 20  ug in OS X, not 
d0230 53 51 4c 69 74 65 29 0a 20 20 2a 2a 20 77 65 20  SQLite).  ** we 
d0240 61 6c 77 61 79 73 20 69 6e 63 72 65 61 73 65 20  always increase 
d0250 74 68 65 20 66 69 6c 65 20 73 69 7a 65 20 74 6f  the file size to
d0260 20 31 20 62 79 20 77 72 69 74 69 6e 67 20 61 20   1 by writing a 
d0270 73 69 6e 67 6c 65 20 62 79 74 65 0a 20 20 2a 2a  single byte.  **
d0280 20 70 72 69 6f 72 20 74 6f 20 61 63 63 65 73 73   prior to access
d0290 69 6e 67 20 74 68 65 20 69 6e 6f 64 65 20 6e 75  ing the inode nu
d02a0 6d 62 65 72 2e 20 20 54 68 65 20 6f 6e 65 20 62  mber.  The one b
d02b0 79 74 65 20 77 72 69 74 74 65 6e 20 69 73 0a 20  yte written is. 
d02c0 20 2a 2a 20 61 6e 20 41 53 43 49 49 20 27 53 27   ** an ASCII 'S'
d02d0 20 63 68 61 72 61 63 74 65 72 20 77 68 69 63 68   character which
d02e0 20 61 6c 73 6f 20 68 61 70 70 65 6e 73 20 74 6f   also happens to
d02f0 20 62 65 20 74 68 65 20 66 69 72 73 74 20 62 79   be the first by
d0300 74 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 68  te.  ** in the h
d0310 65 61 64 65 72 20 6f 66 20 65 76 65 72 79 20 53  eader of every S
d0320 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 2e 20  QLite database. 
d0330 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 69 66   In this way, if
d0340 20 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61   there.  ** is a
d0350 20 72 61 63 65 20 63 6f 6e 64 69 74 69 6f 6e 20   race condition 
d0360 73 75 63 68 20 74 68 61 74 20 61 6e 6f 74 68 65  such that anothe
d0370 72 20 74 68 72 65 61 64 20 68 61 73 20 61 6c 72  r thread has alr
d0380 65 61 64 79 20 70 6f 70 75 6c 61 74 65 64 0a 20  eady populated. 
d0390 20 2a 2a 20 74 68 65 20 66 69 72 73 74 20 70 61   ** the first pa
d03a0 67 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ge of the databa
d03b0 73 65 2c 20 6e 6f 20 64 61 6d 61 67 65 20 69 73  se, no damage is
d03c0 20 64 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20 69 66   done..  */.  if
d03d0 28 20 73 74 61 74 62 75 66 2e 73 74 5f 73 69 7a  ( statbuf.st_siz
d03e0 65 3d 3d 30 20 29 7b 0a 20 20 20 20 77 72 69 74  e==0 ){.    writ
d03f0 65 28 66 64 2c 20 22 53 22 2c 20 31 29 3b 0a 20  e(fd, "S", 1);. 
d0400 20 20 20 72 63 20 3d 20 66 73 74 61 74 28 66 64     rc = fstat(fd
d0410 2c 20 26 73 74 61 74 62 75 66 29 3b 0a 20 20 20  , &statbuf);.   
d0420 20 69 66 28 20 72 63 21 3d 30 20 29 7b 0a 20 20   if( rc!=0 ){.  
d0430 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
d0440 45 5f 49 4f 45 52 52 3b 0a 20 20 20 20 7d 0a 20  E_IOERR;.    }. 
d0450 20 7d 0a 0a 20 20 6d 65 6d 73 65 74 28 26 6b 65   }..  memset(&ke
d0460 79 31 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65  y1, 0, sizeof(ke
d0470 79 31 29 29 3b 0a 20 20 6b 65 79 31 2e 64 65 76  y1));.  key1.dev
d0480 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f 64 65   = statbuf.st_de
d0490 76 3b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b  v;.#if IS_VXWORK
d04a0 53 0a 20 20 6b 65 79 31 2e 72 6e 61 6d 20 3d 20  S.  key1.rnam = 
d04b0 72 6e 61 6d 3b 0a 23 65 6c 73 65 0a 20 20 6b 65  rnam;.#else.  ke
d04c0 79 31 2e 69 6e 6f 20 3d 20 73 74 61 74 62 75 66  y1.ino = statbuf
d04d0 2e 73 74 5f 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a  .st_ino;.#endif.
d04e0 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
d04f0 44 53 41 46 45 0a 20 20 69 66 28 20 74 68 72 65  DSAFE.  if( thre
d0500 61 64 73 4f 76 65 72 72 69 64 65 45 61 63 68 4f  adsOverrideEachO
d0510 74 68 65 72 73 4c 6f 63 6b 73 3c 30 20 29 7b 0a  thersLocks<0 ){.
d0520 20 20 20 20 74 65 73 74 54 68 72 65 61 64 4c 6f      testThreadLo
d0530 63 6b 69 6e 67 42 65 68 61 76 69 6f 72 28 66 64  ckingBehavior(fd
d0540 29 3b 0a 20 20 7d 0a 20 20 6b 65 79 31 2e 74 69  );.  }.  key1.ti
d0550 64 20 3d 20 74 68 72 65 61 64 73 4f 76 65 72 72  d = threadsOverr
d0560 69 64 65 45 61 63 68 4f 74 68 65 72 73 4c 6f 63  ideEachOthersLoc
d0570 6b 73 20 3f 20 30 20 3a 20 70 74 68 72 65 61 64  ks ? 0 : pthread
d0580 5f 73 65 6c 66 28 29 3b 0a 23 65 6e 64 69 66 0a  _self();.#endif.
d0590 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 32 2c 20    memset(&key2, 
d05a0 30 2c 20 73 69 7a 65 6f 66 28 6b 65 79 32 29 29  0, sizeof(key2))
d05b0 3b 0a 20 20 6b 65 79 32 2e 64 65 76 20 3d 20 73  ;.  key2.dev = s
d05c0 74 61 74 62 75 66 2e 73 74 5f 64 65 76 3b 0a 23  tatbuf.st_dev;.#
d05d0 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if IS_VXWORKS.  
d05e0 6b 65 79 32 2e 72 6e 61 6d 20 3d 20 72 6e 61 6d  key2.rnam = rnam
d05f0 3b 0a 23 65 6c 73 65 0a 20 20 6b 65 79 32 2e 69  ;.#else.  key2.i
d0600 6e 6f 20 3d 20 73 74 61 74 62 75 66 2e 73 74 5f  no = statbuf.st_
d0610 69 6e 6f 3b 0a 23 65 6e 64 69 66 0a 20 20 70 4c  ino;.#endif.  pL
d0620 6f 63 6b 20 3d 20 6c 6f 63 6b 4c 69 73 74 3b 0a  ock = lockList;.
d0630 20 20 77 68 69 6c 65 28 20 70 4c 6f 63 6b 20 26    while( pLock &
d0640 26 20 6d 65 6d 63 6d 70 28 26 6b 65 79 31 2c 20  & memcmp(&key1, 
d0650 26 70 4c 6f 63 6b 2d 3e 6b 65 79 2c 20 73 69 7a  &pLock->key, siz
d0660 65 6f 66 28 6b 65 79 31 29 29 20 29 7b 0a 20 20  eof(key1)) ){.  
d0670 20 20 70 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 2d    pLock = pLock-
d0680 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 69 66  >pNext;.  }.  if
d0690 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a 20 20  ( pLock==0 ){.  
d06a0 20 20 70 4c 6f 63 6b 20 3d 20 73 71 6c 69 74 65    pLock = sqlite
d06b0 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  3_malloc( sizeof
d06c0 28 2a 70 4c 6f 63 6b 29 20 29 3b 0a 20 20 20 20  (*pLock) );.    
d06d0 69 66 28 20 70 4c 6f 63 6b 3d 3d 30 20 29 7b 0a  if( pLock==0 ){.
d06e0 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
d06f0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67  E_NOMEM;.      g
d0700 6f 74 6f 20 65 78 69 74 5f 66 69 6e 64 6c 6f 63  oto exit_findloc
d0710 6b 69 6e 66 6f 3b 0a 20 20 20 20 7d 0a 20 20 20  kinfo;.    }.   
d0720 20 70 4c 6f 63 6b 2d 3e 6b 65 79 20 3d 20 6b 65   pLock->key = ke
d0730 79 31 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e  y1;.    pLock->n
d0740 52 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4c 6f  Ref = 1;.    pLo
d0750 63 6b 2d 3e 63 6e 74 20 3d 20 30 3b 0a 20 20 20  ck->cnt = 0;.   
d0760 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d0770 20 3d 20 30 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d   = 0;.    pLock-
d0780 3e 70 4e 65 78 74 20 3d 20 6c 6f 63 6b 4c 69 73  >pNext = lockLis
d0790 74 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 70 50  t;.    pLock->pP
d07a0 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  rev = 0;.    if(
d07b0 20 6c 6f 63 6b 4c 69 73 74 20 29 20 6c 6f 63 6b   lockList ) lock
d07c0 4c 69 73 74 2d 3e 70 50 72 65 76 20 3d 20 70 4c  List->pPrev = pL
d07d0 6f 63 6b 3b 0a 20 20 20 20 6c 6f 63 6b 4c 69 73  ock;.    lockLis
d07e0 74 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d 65 6c  t = pLock;.  }el
d07f0 73 65 7b 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 6e  se{.    pLock->n
d0800 52 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 70  Ref++;.  }.  *pp
d0810 4c 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20  Lock = pLock;.  
d0820 69 66 28 20 70 70 4f 70 65 6e 21 3d 30 20 29 7b  if( ppOpen!=0 ){
d0830 0a 20 20 20 20 70 4f 70 65 6e 20 3d 20 6f 70 65  .    pOpen = ope
d0840 6e 4c 69 73 74 3b 0a 20 20 20 20 77 68 69 6c 65  nList;.    while
d0850 28 20 70 4f 70 65 6e 20 26 26 20 6d 65 6d 63 6d  ( pOpen && memcm
d0860 70 28 26 6b 65 79 32 2c 20 26 70 4f 70 65 6e 2d  p(&key2, &pOpen-
d0870 3e 6b 65 79 2c 20 73 69 7a 65 6f 66 28 6b 65 79  >key, sizeof(key
d0880 32 29 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 70  2)) ){.      pOp
d0890 65 6e 20 3d 20 70 4f 70 65 6e 2d 3e 70 4e 65 78  en = pOpen->pNex
d08a0 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
d08b0 20 70 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20   pOpen==0 ){.   
d08c0 20 20 20 70 4f 70 65 6e 20 3d 20 73 71 6c 69 74     pOpen = sqlit
d08d0 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f  e3_malloc( sizeo
d08e0 66 28 2a 70 4f 70 65 6e 29 20 29 3b 0a 20 20 20  f(*pOpen) );.   
d08f0 20 20 20 69 66 28 20 70 4f 70 65 6e 3d 3d 30 20     if( pOpen==0 
d0900 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61  ){.        relea
d0910 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 4c 6f 63 6b  seLockInfo(pLock
d0920 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  );.        rc = 
d0930 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
d0940 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
d0950 66 69 6e 64 6c 6f 63 6b 69 6e 66 6f 3b 0a 20 20  findlockinfo;.  
d0960 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 65      }.      pOpe
d0970 6e 2d 3e 6b 65 79 20 3d 20 6b 65 79 32 3b 0a 20  n->key = key2;. 
d0980 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66       pOpen->nRef
d0990 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4f 70 65   = 1;.      pOpe
d09a0 6e 2d 3e 6e 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  n->nLock = 0;.  
d09b0 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64      pOpen->nPend
d09c0 69 6e 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 70  ing = 0;.      p
d09d0 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d  Open->aPending =
d09e0 20 30 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d   0;.      pOpen-
d09f0 3e 70 4e 65 78 74 20 3d 20 6f 70 65 6e 4c 69 73  >pNext = openLis
d0a00 74 3b 0a 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e  t;.      pOpen->
d0a10 70 50 72 65 76 20 3d 20 30 3b 0a 20 20 20 20 20  pPrev = 0;.     
d0a20 20 69 66 28 20 6f 70 65 6e 4c 69 73 74 20 29 20   if( openList ) 
d0a30 6f 70 65 6e 4c 69 73 74 2d 3e 70 50 72 65 76 20  openList->pPrev 
d0a40 3d 20 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 6f  = pOpen;.      o
d0a50 70 65 6e 4c 69 73 74 20 3d 20 70 4f 70 65 6e 3b  penList = pOpen;
d0a60 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a  .#if IS_VXWORKS.
d0a70 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 70 53 65        pOpen->pSe
d0a80 6d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20  m = NULL;.      
d0a90 70 4f 70 65 6e 2d 3e 61 53 65 6d 4e 61 6d 65 5b  pOpen->aSemName[
d0aa0 30 5d 20 3d 20 27 5c 30 27 3b 0a 23 65 6e 64 69  0] = '\0';.#endi
d0ab0 66 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  f.    }else{.   
d0ac0 20 20 20 70 4f 70 65 6e 2d 3e 6e 52 65 66 2b 2b     pOpen->nRef++
d0ad0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70 4f  ;.    }.    *ppO
d0ae0 70 65 6e 20 3d 20 70 4f 70 65 6e 3b 0a 20 20 7d  pen = pOpen;.  }
d0af0 0a 0a 65 78 69 74 5f 66 69 6e 64 6c 6f 63 6b 69  ..exit_findlocki
d0b00 6e 66 6f 3a 0a 20 20 72 65 74 75 72 6e 20 72 63  nfo:.  return rc
d0b10 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
d0b20 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 48  TE_DEBUG./*.** H
d0b30 65 6c 70 65 72 20 66 75 6e 63 74 69 6f 6e 20 66  elper function f
d0b40 6f 72 20 70 72 69 6e 74 69 6e 67 20 6f 75 74 20  or printing out 
d0b50 74 72 61 63 65 20 69 6e 66 6f 72 6d 61 74 69 6f  trace informatio
d0b60 6e 20 66 72 6f 6d 20 64 65 62 75 67 67 69 6e 67  n from debugging
d0b70 0a 2a 2a 20 62 69 6e 61 72 69 65 73 2e 20 54 68  .** binaries. Th
d0b80 69 73 20 72 65 74 75 72 6e 73 20 74 68 65 20 73  is returns the s
d0b90 74 72 69 6e 67 20 72 65 70 72 65 73 65 74 61 74  tring represetat
d0ba0 69 6f 6e 20 6f 66 20 74 68 65 20 73 75 70 70 6c  ion of the suppl
d0bb0 69 65 64 0a 2a 2a 20 69 6e 74 65 67 65 72 20 6c  ied.** integer l
d0bc0 6f 63 6b 2d 74 79 70 65 2e 0a 2a 2f 0a 73 74 61  ock-type..*/.sta
d0bd0 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
d0be0 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 69 6e 74  locktypeName(int
d0bf0 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 73 77   locktype){.  sw
d0c00 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29  itch( locktype )
d0c10 7b 0a 20 20 63 61 73 65 20 4e 4f 5f 4c 4f 43 4b  {.  case NO_LOCK
d0c20 3a 20 72 65 74 75 72 6e 20 22 4e 4f 4e 45 22 3b  : return "NONE";
d0c30 0a 20 20 63 61 73 65 20 53 48 41 52 45 44 5f 4c  .  case SHARED_L
d0c40 4f 43 4b 3a 20 72 65 74 75 72 6e 20 22 53 48 41  OCK: return "SHA
d0c50 52 45 44 22 3b 0a 20 20 63 61 73 65 20 52 45 53  RED";.  case RES
d0c60 45 52 56 45 44 5f 4c 4f 43 4b 3a 20 72 65 74 75  ERVED_LOCK: retu
d0c70 72 6e 20 22 52 45 53 45 52 56 45 44 22 3b 0a 20  rn "RESERVED";. 
d0c80 20 63 61 73 65 20 50 45 4e 44 49 4e 47 5f 4c 4f   case PENDING_LO
d0c90 43 4b 3a 20 72 65 74 75 72 6e 20 22 50 45 4e 44  CK: return "PEND
d0ca0 49 4e 47 22 3b 0a 20 20 63 61 73 65 20 45 58 43  ING";.  case EXC
d0cb0 4c 55 53 49 56 45 5f 4c 4f 43 4b 3a 20 72 65 74  LUSIVE_LOCK: ret
d0cc0 75 72 6e 20 22 45 58 43 4c 55 53 49 56 45 22 3b  urn "EXCLUSIVE";
d0cd0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 22 45  .  }.  return "E
d0ce0 52 52 4f 52 22 3b 0a 7d 0a 23 65 6e 64 69 66 0a  RROR";.}.#endif.
d0cf0 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20 61 72 65  ./*.** If we are
d0d00 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61 20   currently in a 
d0d10 64 69 66 66 65 72 65 6e 74 20 74 68 72 65 61 64  different thread
d0d20 20 74 68 61 6e 20 74 68 65 20 74 68 72 65 61 64   than the thread
d0d30 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 6e 69   that the.** uni
d0d40 78 46 69 6c 65 20 61 72 67 75 6d 65 6e 74 20 62  xFile argument b
d0d50 65 6c 6f 6e 67 73 20 74 6f 2c 20 74 68 65 6e 20  elongs to, then 
d0d60 74 72 61 6e 73 66 65 72 20 6f 77 6e 65 72 73 68  transfer ownersh
d0d70 69 70 20 6f 66 20 74 68 65 20 75 6e 69 78 46 69  ip of the unixFi
d0d80 6c 65 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 74 68  le.** over to th
d0d90 65 20 63 75 72 72 65 6e 74 20 74 68 72 65 61 64  e current thread
d0da0 2e 0a 2a 2a 0a 2a 2a 20 41 20 75 6e 69 78 46 69  ..**.** A unixFi
d0db0 6c 65 20 69 73 20 6f 6e 6c 79 20 6f 77 6e 65 64  le is only owned
d0dc0 20 62 79 20 61 20 74 68 72 65 61 64 20 6f 6e 20   by a thread on 
d0dd0 73 79 73 74 65 6d 73 20 77 68 65 72 65 20 6f 6e  systems where on
d0de0 65 20 74 68 72 65 61 64 20 69 73 0a 2a 2a 20 75  e thread is.** u
d0df0 6e 61 62 6c 65 20 74 6f 20 6f 76 65 72 72 69 64  nable to overrid
d0e00 65 20 6c 6f 63 6b 73 20 63 72 65 61 74 65 64 20  e locks created 
d0e10 62 79 20 61 20 64 69 66 66 65 72 65 6e 74 20 74  by a different t
d0e20 68 72 65 61 64 2e 20 20 52 65 64 48 61 74 39 20  hread.  RedHat9 
d0e30 69 73 0a 2a 2a 20 61 6e 20 65 78 61 6d 70 6c 65  is.** an example
d0e40 20 6f 66 20 73 75 63 68 20 61 20 73 79 73 74 65   of such a syste
d0e50 6d 2e 0a 2a 2a 0a 2a 2a 20 4f 77 6e 65 72 73 68  m..**.** Ownersh
d0e60 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 6f  ip transfer is o
d0e70 6e 6c 79 20 61 6c 6c 6f 77 65 64 20 69 66 20 74  nly allowed if t
d0e80 68 65 20 75 6e 69 78 46 69 6c 65 20 69 73 20 63  he unixFile is c
d0e90 75 72 72 65 6e 74 6c 79 20 75 6e 6c 6f 63 6b 65  urrently unlocke
d0ea0 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 75 6e 69  d..** If the uni
d0eb0 78 46 69 6c 65 20 69 73 20 6c 6f 63 6b 65 64 20  xFile is locked 
d0ec0 61 6e 64 20 61 6e 20 6f 77 6e 65 72 73 68 69 70  and an ownership
d0ed0 20 69 73 20 77 72 6f 6e 67 2c 20 74 68 65 6e 20   is wrong, then 
d0ee0 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54 45  return.** SQLITE
d0ef0 5f 4d 49 53 55 53 45 2e 20 20 53 51 4c 49 54 45  _MISUSE.  SQLITE
d0f00 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
d0f10 69 66 20 65 76 65 72 79 74 68 69 6e 67 20 77 6f  if everything wo
d0f20 72 6b 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49  rks..*/.#if SQLI
d0f30 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 73 74  TE_THREADSAFE.st
d0f40 61 74 69 63 20 69 6e 74 20 74 72 61 6e 73 66 65  atic int transfe
d0f50 72 4f 77 6e 65 72 73 68 69 70 28 75 6e 69 78 46  rOwnership(unixF
d0f60 69 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69  ile *pFile){.  i
d0f70 6e 74 20 72 63 3b 0a 20 20 70 74 68 72 65 61 64  nt rc;.  pthread
d0f80 5f 74 20 68 53 65 6c 66 3b 0a 20 20 69 66 28 20  _t hSelf;.  if( 
d0f90 74 68 72 65 61 64 73 4f 76 65 72 72 69 64 65 45  threadsOverrideE
d0fa0 61 63 68 4f 74 68 65 72 73 4c 6f 63 6b 73 20 29  achOthersLocks )
d0fb0 7b 0a 20 20 20 20 2f 2a 20 4f 77 6e 65 72 73 68  {.    /* Ownersh
d0fc0 69 70 20 74 72 61 6e 73 66 65 72 73 20 6e 6f 74  ip transfers not
d0fd0 20 6e 65 65 64 65 64 20 6f 6e 20 74 68 69 73 20   needed on this 
d0fe0 73 79 73 74 65 6d 20 2a 2f 0a 20 20 20 20 72 65  system */.    re
d0ff0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d1000 20 20 7d 0a 20 20 68 53 65 6c 66 20 3d 20 70 74    }.  hSelf = pt
d1010 68 72 65 61 64 5f 73 65 6c 66 28 29 3b 0a 20 20  hread_self();.  
d1020 69 66 28 20 70 74 68 72 65 61 64 5f 65 71 75 61  if( pthread_equa
d1030 6c 28 70 46 69 6c 65 2d 3e 74 69 64 2c 20 68 53  l(pFile->tid, hS
d1040 65 6c 66 29 20 29 7b 0a 20 20 20 20 2f 2a 20 57  elf) ){.    /* W
d1050 65 20 61 72 65 20 73 74 69 6c 6c 20 69 6e 20 74  e are still in t
d1060 68 65 20 73 61 6d 65 20 74 68 72 65 61 64 20 2a  he same thread *
d1070 2f 0a 20 20 20 20 4f 53 54 52 41 43 45 31 28 22  /.    OSTRACE1("
d1080 4e 6f 2d 74 72 61 6e 73 66 65 72 2c 20 73 61 6d  No-transfer, sam
d1090 65 20 74 68 72 65 61 64 5c 6e 22 29 3b 0a 20 20  e thread\n");.  
d10a0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d10b0 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 46  OK;.  }.  if( pF
d10c0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e  ile->locktype!=N
d10d0 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 2f 2a  O_LOCK ){.    /*
d10e0 20 57 65 20 63 61 6e 6e 6f 74 20 63 68 61 6e 67   We cannot chang
d10f0 65 20 6f 77 6e 65 72 73 68 69 70 20 77 68 69 6c  e ownership whil
d1100 65 20 77 65 20 61 72 65 20 68 6f 6c 64 69 6e 67  e we are holding
d1110 20 61 20 6c 6f 63 6b 21 20 2a 2f 0a 20 20 20 20   a lock! */.    
d1120 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
d1130 53 55 53 45 3b 0a 20 20 7d 0a 20 20 4f 53 54 52  SUSE;.  }.  OSTR
d1140 41 43 45 34 28 22 54 72 61 6e 73 66 65 72 20 6f  ACE4("Transfer o
d1150 77 6e 65 72 73 68 69 70 20 6f 66 20 25 64 20 66  wnership of %d f
d1160 72 6f 6d 20 25 64 20 74 6f 20 25 64 5c 6e 22 2c  rom %d to %d\n",
d1170 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 46 69  .            pFi
d1180 6c 65 2d 3e 68 2c 20 70 46 69 6c 65 2d 3e 74 69  le->h, pFile->ti
d1190 64 2c 20 68 53 65 6c 66 29 3b 0a 20 20 70 46 69  d, hSelf);.  pFi
d11a0 6c 65 2d 3e 74 69 64 20 3d 20 68 53 65 6c 66 3b  le->tid = hSelf;
d11b0 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 70 4c  .  if (pFile->pL
d11c0 6f 63 6b 20 21 3d 20 4e 55 4c 4c 29 20 7b 0a 20  ock != NULL) {. 
d11d0 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
d11e0 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29  fo(pFile->pLock)
d11f0 3b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53  ;.#if IS_VXWORKS
d1200 0a 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 6f  .    rc = findLo
d1210 63 6b 49 6e 66 6f 28 70 46 69 6c 65 2d 3e 68 2c  ckInfo(pFile->h,
d1220 20 70 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74   pFile->zRealpat
d1230 68 2c 20 26 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b  h, &pFile->pLock
d1240 2c 20 30 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20  , 0);.#else.    
d1250 72 63 20 3d 20 66 69 6e 64 4c 6f 63 6b 49 6e 66  rc = findLockInf
d1260 6f 28 70 46 69 6c 65 2d 3e 68 2c 20 26 70 46 69  o(pFile->h, &pFi
d1270 6c 65 2d 3e 70 4c 6f 63 6b 2c 20 30 29 3b 0a 23  le->pLock, 0);.#
d1280 65 6e 64 69 66 0a 20 20 20 20 4f 53 54 52 41 43  endif.    OSTRAC
d1290 45 35 28 22 4c 4f 43 4b 20 20 20 20 25 64 20 69  E5("LOCK    %d i
d12a0 73 20 6e 6f 77 20 25 73 28 25 73 2c 25 64 29 5c  s now %s(%s,%d)\
d12b0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20  n", pFile->h,.  
d12c0 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70           locktyp
d12d0 65 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63  eName(pFile->loc
d12e0 6b 74 79 70 65 29 2c 0a 20 20 20 20 20 20 20 20  ktype),.        
d12f0 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28     locktypeName(
d1300 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f  pFile->pLock->lo
d1310 63 6b 74 79 70 65 29 2c 20 70 46 69 6c 65 2d 3e  cktype), pFile->
d1320 70 4c 6f 63 6b 2d 3e 63 6e 74 29 3b 0a 20 20 20  pLock->cnt);.   
d1330 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20   return rc;.  } 
d1340 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75 72  else {.    retur
d1350 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
d1360 0a 7d 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 4f 6e  .}.#else.  /* On
d1370 20 73 69 6e 67 6c 65 2d 74 68 72 65 61 64 65 64   single-threaded
d1380 20 62 75 69 6c 64 73 2c 20 6f 77 6e 65 72 73 68   builds, ownersh
d1390 69 70 20 74 72 61 6e 73 66 65 72 20 69 73 20 61  ip transfer is a
d13a0 20 6e 6f 2d 6f 70 20 2a 2f 0a 23 20 64 65 66 69   no-op */.# defi
d13b0 6e 65 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72  ne transferOwner
d13c0 73 68 69 70 28 58 29 20 53 51 4c 49 54 45 5f 4f  ship(X) SQLITE_O
d13d0 4b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  K.#endif../*.** 
d13e0 53 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73  Seek to the offs
d13f0 65 74 20 70 61 73 73 65 64 20 61 73 20 74 68 65  et passed as the
d1400 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
d1410 2c 20 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20  , then read cnt 
d1420 0a 2a 2a 20 62 79 74 65 73 20 69 6e 74 6f 20 70  .** bytes into p
d1430 42 75 66 2e 20 52 65 74 75 72 6e 20 74 68 65 20  Buf. Return the 
d1440 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
d1450 61 63 74 75 61 6c 6c 79 20 72 65 61 64 2e 0a 2a  actually read..*
d1460 2a 0a 2a 2a 20 4e 42 3a 20 20 49 66 20 79 6f 75  *.** NB:  If you
d1470 20 64 65 66 69 6e 65 20 55 53 45 5f 50 52 45 41   define USE_PREA
d1480 44 20 6f 72 20 55 53 45 5f 50 52 45 41 44 36 34  D or USE_PREAD64
d1490 2c 20 74 68 65 6e 20 69 74 20 6d 69 67 68 74 20  , then it might 
d14a0 61 6c 73 6f 0a 2a 2a 20 62 65 20 6e 65 63 65 73  also.** be neces
d14b0 73 61 72 79 20 74 6f 20 64 65 66 69 6e 65 20 5f  sary to define _
d14c0 58 4f 50 45 4e 5f 53 4f 55 52 43 45 20 74 6f 20  XOPEN_SOURCE to 
d14d0 62 65 20 35 30 30 2e 20 20 54 68 69 73 20 76 61  be 500.  This va
d14e0 72 69 65 73 20 66 72 6f 6d 0a 2a 2a 20 6f 6e 65  ries from.** one
d14f0 20 73 79 73 74 65 6d 20 74 6f 20 61 6e 6f 74 68   system to anoth
d1500 65 72 2e 20 20 53 69 6e 63 65 20 53 51 4c 69 74  er.  Since SQLit
d1510 65 20 64 6f 65 73 20 6e 6f 74 20 64 65 66 69 6e  e does not defin
d1520 65 20 55 53 45 5f 50 52 45 41 44 0a 2a 2a 20 61  e USE_PREAD.** a
d1530 6e 79 20 61 6e 79 20 66 6f 72 6d 20 62 79 20 64  ny any form by d
d1540 65 66 61 75 6c 74 2c 20 77 65 20 77 69 6c 6c 20  efault, we will 
d1550 6e 6f 74 20 61 74 74 65 6d 70 74 20 74 6f 20 64  not attempt to d
d1560 65 66 69 6e 65 20 5f 58 4f 50 45 4e 5f 53 4f 55  efine _XOPEN_SOU
d1570 52 43 45 2e 0a 2a 2a 20 53 65 65 20 74 69 63 6b  RCE..** See tick
d1580 65 74 73 20 23 32 37 34 31 20 61 6e 64 20 23 32  ets #2741 and #2
d1590 36 38 31 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  681..*/.static i
d15a0 6e 74 20 73 65 65 6b 41 6e 64 52 65 61 64 28 75  nt seekAndRead(u
d15b0 6e 69 78 46 69 6c 65 20 2a 69 64 2c 20 73 71 6c  nixFile *id, sql
d15c0 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73 65  ite3_int64 offse
d15d0 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 2c 20 69  t, void *pBuf, i
d15e0 6e 74 20 63 6e 74 29 7b 0a 20 20 69 6e 74 20 67  nt cnt){.  int g
d15f0 6f 74 3b 0a 20 20 69 36 34 20 6e 65 77 4f 66 66  ot;.  i64 newOff
d1600 73 65 74 3b 0a 20 20 54 49 4d 45 52 5f 53 54 41  set;.  TIMER_STA
d1610 52 54 3b 0a 23 69 66 20 64 65 66 69 6e 65 64 28  RT;.#if defined(
d1620 55 53 45 5f 50 52 45 41 44 29 0a 20 20 67 6f 74  USE_PREAD).  got
d1630 20 3d 20 70 72 65 61 64 28 69 64 2d 3e 68 2c 20   = pread(id->h, 
d1640 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65  pBuf, cnt, offse
d1650 74 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f  t);.  SimulateIO
d1660 45 72 72 6f 72 28 20 67 6f 74 20 3d 20 2d 31 20  Error( got = -1 
d1670 29 3b 0a 23 65 6c 69 66 20 64 65 66 69 6e 65 64  );.#elif defined
d1680 28 55 53 45 5f 50 52 45 41 44 36 34 29 0a 20 20  (USE_PREAD64).  
d1690 67 6f 74 20 3d 20 70 72 65 61 64 36 34 28 69 64  got = pread64(id
d16a0 2d 3e 68 2c 20 70 42 75 66 2c 20 63 6e 74 2c 20  ->h, pBuf, cnt, 
d16b0 6f 66 66 73 65 74 29 3b 0a 20 20 53 69 6d 75 6c  offset);.  Simul
d16c0 61 74 65 49 4f 45 72 72 6f 72 28 20 67 6f 74 20  ateIOError( got 
d16d0 3d 20 2d 31 20 29 3b 0a 23 65 6c 73 65 0a 20 20  = -1 );.#else.  
d16e0 6e 65 77 4f 66 66 73 65 74 20 3d 20 6c 73 65 65  newOffset = lsee
d16f0 6b 28 69 64 2d 3e 68 2c 20 6f 66 66 73 65 74 2c  k(id->h, offset,
d1700 20 53 45 45 4b 5f 53 45 54 29 3b 0a 20 20 53 69   SEEK_SET);.  Si
d1710 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 6e  mulateIOError( n
d1720 65 77 4f 66 66 73 65 74 2d 2d 20 29 3b 0a 20 20  ewOffset-- );.  
d1730 69 66 28 20 6e 65 77 4f 66 66 73 65 74 21 3d 6f  if( newOffset!=o
d1740 66 66 73 65 74 20 29 7b 0a 20 20 20 20 72 65 74  ffset ){.    ret
d1750 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a 20 20 67 6f  urn -1;.  }.  go
d1760 74 20 3d 20 72 65 61 64 28 69 64 2d 3e 68 2c 20  t = read(id->h, 
d1770 70 42 75 66 2c 20 63 6e 74 29 3b 0a 23 65 6e 64  pBuf, cnt);.#end
d1780 69 66 0a 20 20 54 49 4d 45 52 5f 45 4e 44 3b 0a  if.  TIMER_END;.
d1790 20 20 4f 53 54 52 41 43 45 35 28 22 52 45 41 44    OSTRACE5("READ
d17a0 20 20 20 20 25 2d 33 64 20 25 35 64 20 25 37 6c      %-3d %5d %7l
d17b0 6c 64 20 25 6c 6c 75 5c 6e 22 2c 20 69 64 2d 3e  ld %llu\n", id->
d17c0 68 2c 20 67 6f 74 2c 20 6f 66 66 73 65 74 2c 20  h, got, offset, 
d17d0 54 49 4d 45 52 5f 45 4c 41 50 53 45 44 29 3b 0a  TIMER_ELAPSED);.
d17e0 20 20 72 65 74 75 72 6e 20 67 6f 74 3b 0a 7d 0a    return got;.}.
d17f0 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 64 61 74 61  ./*.** Read data
d1800 20 66 72 6f 6d 20 61 20 66 69 6c 65 20 69 6e 74   from a file int
d1810 6f 20 61 20 62 75 66 66 65 72 2e 20 20 52 65 74  o a buffer.  Ret
d1820 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 69 66  urn SQLITE_OK if
d1830 20 61 6c 6c 0a 2a 2a 20 62 79 74 65 73 20 77 65   all.** bytes we
d1840 72 65 20 72 65 61 64 20 73 75 63 63 65 73 73 66  re read successf
d1850 75 6c 6c 79 20 61 6e 64 20 53 51 4c 49 54 45 5f  ully and SQLITE_
d1860 49 4f 45 52 52 20 69 66 20 61 6e 79 74 68 69 6e  IOERR if anythin
d1870 67 20 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e  g goes.** wrong.
d1880 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75  .*/.static int u
d1890 6e 69 78 52 65 61 64 28 0a 20 20 73 71 6c 69 74  nixRead(.  sqlit
d18a0 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20  e3_file *id, .  
d18b0 76 6f 69 64 20 2a 70 42 75 66 2c 20 0a 20 20 69  void *pBuf, .  i
d18c0 6e 74 20 61 6d 74 2c 0a 20 20 73 71 6c 69 74 65  nt amt,.  sqlite
d18d0 33 5f 69 6e 74 36 34 20 6f 66 66 73 65 74 0a 29  3_int64 offset.)
d18e0 7b 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 61  {.  int got;.  a
d18f0 73 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 67  ssert( id );.  g
d1900 6f 74 20 3d 20 73 65 65 6b 41 6e 64 52 65 61 64  ot = seekAndRead
d1910 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 2c 20  ((unixFile*)id, 
d1920 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20 61 6d  offset, pBuf, am
d1930 74 29 3b 0a 20 20 69 66 28 20 67 6f 74 3d 3d 61  t);.  if( got==a
d1940 6d 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  mt ){.    return
d1950 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65   SQLITE_OK;.  }e
d1960 6c 73 65 20 69 66 28 20 67 6f 74 3c 30 20 29 7b  lse if( got<0 ){
d1970 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d1980 54 45 5f 49 4f 45 52 52 5f 52 45 41 44 3b 0a 20  TE_IOERR_READ;. 
d1990 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 55   }else{.    /* U
d19a0 6e 72 65 61 64 20 70 61 72 74 73 20 6f 66 20 74  nread parts of t
d19b0 68 65 20 62 75 66 66 65 72 20 6d 75 73 74 20 62  he buffer must b
d19c0 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 2a 2f  e zero-filled */
d19d0 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 28 28 63  .    memset(&((c
d19e0 68 61 72 2a 29 70 42 75 66 29 5b 67 6f 74 5d 2c  har*)pBuf)[got],
d19f0 20 30 2c 20 61 6d 74 2d 67 6f 74 29 3b 0a 20 20   0, amt-got);.  
d1a00 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d1a10 49 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44  IOERR_SHORT_READ
d1a20 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
d1a30 65 65 6b 20 74 6f 20 74 68 65 20 6f 66 66 73 65  eek to the offse
d1a40 74 20 69 6e 20 69 64 2d 3e 6f 66 66 73 65 74 20  t in id->offset 
d1a50 74 68 65 6e 20 72 65 61 64 20 63 6e 74 20 62 79  then read cnt by
d1a60 74 65 73 20 69 6e 74 6f 20 70 42 75 66 2e 0a 2a  tes into pBuf..*
d1a70 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
d1a80 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74  ber of bytes act
d1a90 75 61 6c 6c 79 20 72 65 61 64 2e 20 20 55 70 64  ually read.  Upd
d1aa0 61 74 65 20 74 68 65 20 6f 66 66 73 65 74 2e 0a  ate the offset..
d1ab0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
d1ac0 65 6b 41 6e 64 57 72 69 74 65 28 75 6e 69 78 46  ekAndWrite(unixF
d1ad0 69 6c 65 20 2a 69 64 2c 20 69 36 34 20 6f 66 66  ile *id, i64 off
d1ae0 73 65 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  set, const void 
d1af0 2a 70 42 75 66 2c 20 69 6e 74 20 63 6e 74 29 7b  *pBuf, int cnt){
d1b00 0a 20 20 69 6e 74 20 67 6f 74 3b 0a 20 20 69 36  .  int got;.  i6
d1b10 34 20 6e 65 77 4f 66 66 73 65 74 3b 0a 20 20 54  4 newOffset;.  T
d1b20 49 4d 45 52 5f 53 54 41 52 54 3b 0a 23 69 66 20  IMER_START;.#if 
d1b30 64 65 66 69 6e 65 64 28 55 53 45 5f 50 52 45 41  defined(USE_PREA
d1b40 44 29 0a 20 20 67 6f 74 20 3d 20 70 77 72 69 74  D).  got = pwrit
d1b50 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c 20 63  e(id->h, pBuf, c
d1b60 6e 74 2c 20 6f 66 66 73 65 74 29 3b 0a 23 65 6c  nt, offset);.#el
d1b70 69 66 20 64 65 66 69 6e 65 64 28 55 53 45 5f 50  if defined(USE_P
d1b80 52 45 41 44 36 34 29 0a 20 20 67 6f 74 20 3d 20  READ64).  got = 
d1b90 70 77 72 69 74 65 36 34 28 69 64 2d 3e 68 2c 20  pwrite64(id->h, 
d1ba0 70 42 75 66 2c 20 63 6e 74 2c 20 6f 66 66 73 65  pBuf, cnt, offse
d1bb0 74 29 3b 0a 23 65 6c 73 65 0a 20 20 6e 65 77 4f  t);.#else.  newO
d1bc0 66 66 73 65 74 20 3d 20 6c 73 65 65 6b 28 69 64  ffset = lseek(id
d1bd0 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 53 45 45  ->h, offset, SEE
d1be0 4b 5f 53 45 54 29 3b 0a 20 20 69 66 28 20 6e 65  K_SET);.  if( ne
d1bf0 77 4f 66 66 73 65 74 21 3d 6f 66 66 73 65 74 20  wOffset!=offset 
d1c00 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 2d 31  ){.    return -1
d1c10 3b 0a 20 20 7d 0a 20 20 67 6f 74 20 3d 20 77 72  ;.  }.  got = wr
d1c20 69 74 65 28 69 64 2d 3e 68 2c 20 70 42 75 66 2c  ite(id->h, pBuf,
d1c30 20 63 6e 74 29 3b 0a 23 65 6e 64 69 66 0a 20 20   cnt);.#endif.  
d1c40 54 49 4d 45 52 5f 45 4e 44 3b 0a 20 20 4f 53 54  TIMER_END;.  OST
d1c50 52 41 43 45 35 28 22 57 52 49 54 45 20 20 20 25  RACE5("WRITE   %
d1c60 2d 33 64 20 25 35 64 20 25 37 6c 6c 64 20 25 6c  -3d %5d %7lld %l
d1c70 6c 75 5c 6e 22 2c 20 69 64 2d 3e 68 2c 20 67 6f  lu\n", id->h, go
d1c80 74 2c 20 6f 66 66 73 65 74 2c 20 54 49 4d 45 52  t, offset, TIMER
d1c90 5f 45 4c 41 50 53 45 44 29 3b 0a 20 20 72 65 74  _ELAPSED);.  ret
d1ca0 75 72 6e 20 67 6f 74 3b 0a 7d 0a 0a 0a 2f 2a 0a  urn got;.}.../*.
d1cb0 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20 66 72  ** Write data fr
d1cc0 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e 74 6f  om a buffer into
d1cd0 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75 72 6e   a file.  Return
d1ce0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
d1cf0 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f 6d 65  ccess.** or some
d1d00 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f 64   other error cod
d1d10 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 2a 2f  e on failure..*/
d1d20 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
d1d30 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65 33  Write(.  sqlite3
d1d40 5f 66 69 6c 65 20 2a 69 64 2c 20 0a 20 20 63 6f  _file *id, .  co
d1d50 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c 20  nst void *pBuf, 
d1d60 0a 20 20 69 6e 74 20 61 6d 74 2c 0a 20 20 73 71  .  int amt,.  sq
d1d70 6c 69 74 65 33 5f 69 6e 74 36 34 20 6f 66 66 73  lite3_int64 offs
d1d80 65 74 20 0a 29 7b 0a 20 20 69 6e 74 20 77 72 6f  et .){.  int wro
d1d90 74 65 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74  te = 0;.  assert
d1da0 28 20 69 64 20 29 3b 0a 20 20 61 73 73 65 72 74  ( id );.  assert
d1db0 28 20 61 6d 74 3e 30 20 29 3b 0a 20 20 77 68 69  ( amt>0 );.  whi
d1dc0 6c 65 28 20 61 6d 74 3e 30 20 26 26 20 28 77 72  le( amt>0 && (wr
d1dd0 6f 74 65 20 3d 20 73 65 65 6b 41 6e 64 57 72 69  ote = seekAndWri
d1de0 74 65 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64  te((unixFile*)id
d1df0 2c 20 6f 66 66 73 65 74 2c 20 70 42 75 66 2c 20  , offset, pBuf, 
d1e00 61 6d 74 29 29 3e 30 20 29 7b 0a 20 20 20 20 61  amt))>0 ){.    a
d1e10 6d 74 20 2d 3d 20 77 72 6f 74 65 3b 0a 20 20 20  mt -= wrote;.   
d1e20 20 6f 66 66 73 65 74 20 2b 3d 20 77 72 6f 74 65   offset += wrote
d1e30 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 26 28 28  ;.    pBuf = &((
d1e40 63 68 61 72 2a 29 70 42 75 66 29 5b 77 72 6f 74  char*)pBuf)[wrot
d1e50 65 5d 3b 0a 20 20 7d 0a 20 20 53 69 6d 75 6c 61  e];.  }.  Simula
d1e60 74 65 49 4f 45 72 72 6f 72 28 28 20 77 72 6f 74  teIOError(( wrot
d1e70 65 3d 28 2d 31 29 2c 20 61 6d 74 3d 31 20 29 29  e=(-1), amt=1 ))
d1e80 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73 6b  ;.  SimulateDisk
d1e90 66 75 6c 6c 45 72 72 6f 72 28 28 20 77 72 6f 74  fullError(( wrot
d1ea0 65 3d 30 2c 20 61 6d 74 3d 31 20 29 29 3b 0a 20  e=0, amt=1 ));. 
d1eb0 20 69 66 28 20 61 6d 74 3e 30 20 29 7b 0a 20 20   if( amt>0 ){.  
d1ec0 20 20 69 66 28 20 77 72 6f 74 65 3c 30 20 29 7b    if( wrote<0 ){
d1ed0 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
d1ee0 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45  LITE_IOERR_WRITE
d1ef0 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
d1f00 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d1f10 5f 46 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 20 7d  _FULL;.    }.  }
d1f20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d1f30 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53  _OK;.}..#ifdef S
d1f40 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  QLITE_TEST./*.**
d1f50 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d 62 65   Count the numbe
d1f60 72 20 6f 66 20 66 75 6c 6c 73 79 6e 63 73 20 61  r of fullsyncs a
d1f70 6e 64 20 6e 6f 72 6d 61 6c 20 73 79 6e 63 73 2e  nd normal syncs.
d1f80 20 20 54 68 69 73 20 69 73 20 75 73 65 64 20 74    This is used t
d1f90 6f 20 74 65 73 74 0a 2a 2a 20 74 68 61 74 20 73  o test.** that s
d1fa0 79 6e 63 73 20 61 6e 64 20 66 75 6c 6c 73 79 6e  yncs and fullsyn
d1fb0 63 73 20 61 72 65 20 6f 63 63 75 72 69 6e 67 20  cs are occuring 
d1fc0 61 74 20 74 68 65 20 72 69 67 68 74 20 74 69 6d  at the right tim
d1fd0 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  es..*/.SQLITE_AP
d1fe0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 79  I int sqlite3_sy
d1ff0 6e 63 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 53 51  nc_count = 0;.SQ
d2000 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
d2010 69 74 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f  ite3_fullsync_co
d2020 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
d2030 0a 2f 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 66  ./*.** Use the f
d2040 64 61 74 61 73 79 6e 63 28 29 20 41 50 49 20 6f  datasync() API o
d2050 6e 6c 79 20 69 66 20 74 68 65 20 48 41 56 45 5f  nly if the HAVE_
d2060 46 44 41 54 41 53 59 4e 43 20 6d 61 63 72 6f 20  FDATASYNC macro 
d2070 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2a 20 4f  is defined..** O
d2080 74 68 65 72 77 69 73 65 20 75 73 65 20 66 73 79  therwise use fsy
d2090 6e 63 28 29 20 69 6e 20 69 74 73 20 70 6c 61 63  nc() in its plac
d20a0 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 48 41  e..*/.#ifndef HA
d20b0 56 45 5f 46 44 41 54 41 53 59 4e 43 0a 23 20 64  VE_FDATASYNC.# d
d20c0 65 66 69 6e 65 20 66 64 61 74 61 73 79 6e 63 20  efine fdatasync 
d20d0 66 73 79 6e 63 0a 23 65 6e 64 69 66 0a 0a 2f 2a  fsync.#endif../*
d20e0 0a 2a 2a 20 44 65 66 69 6e 65 20 48 41 56 45 5f  .** Define HAVE_
d20f0 46 55 4c 4c 46 53 59 4e 43 20 74 6f 20 30 20 6f  FULLFSYNC to 0 o
d2100 72 20 31 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e  r 1 depending on
d2110 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 0a   whether or not.
d2120 2a 2a 20 74 68 65 20 46 5f 46 55 4c 4c 46 53 59  ** the F_FULLFSY
d2130 4e 43 20 6d 61 63 72 6f 20 69 73 20 64 65 66 69  NC macro is defi
d2140 6e 65 64 2e 20 20 46 5f 46 55 4c 4c 46 53 59 4e  ned.  F_FULLFSYN
d2150 43 20 69 73 20 63 75 72 72 65 6e 74 6c 79 0a 2a  C is currently.*
d2160 2a 20 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65  * only available
d2170 20 6f 6e 20 4d 61 63 20 4f 53 20 58 2e 20 20 42   on Mac OS X.  B
d2180 75 74 20 74 68 61 74 20 63 6f 75 6c 64 20 63 68  ut that could ch
d2190 61 6e 67 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  ange..*/.#ifdef 
d21a0 46 5f 46 55 4c 4c 46 53 59 4e 43 0a 23 20 64 65  F_FULLFSYNC.# de
d21b0 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53  fine HAVE_FULLFS
d21c0 59 4e 43 20 31 0a 23 65 6c 73 65 0a 23 20 64 65  YNC 1.#else.# de
d21d0 66 69 6e 65 20 48 41 56 45 5f 46 55 4c 4c 46 53  fine HAVE_FULLFS
d21e0 59 4e 43 20 30 0a 23 65 6e 64 69 66 0a 0a 0a 2f  YNC 0.#endif.../
d21f0 2a 0a 2a 2a 20 54 68 65 20 66 73 79 6e 63 28 29  *.** The fsync()
d2200 20 73 79 73 74 65 6d 20 63 61 6c 6c 20 64 6f 65   system call doe
d2210 73 20 6e 6f 74 20 77 6f 72 6b 20 61 73 20 61 64  s not work as ad
d2220 76 65 72 74 69 73 65 64 20 6f 6e 20 6d 61 6e 79  vertised on many
d2230 0a 2a 2a 20 75 6e 69 78 20 73 79 73 74 65 6d 73  .** unix systems
d2240 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  .  The following
d2250 20 70 72 6f 63 65 64 75 72 65 20 69 73 20 61 6e   procedure is an
d2260 20 61 74 74 65 6d 70 74 20 74 6f 20 6d 61 6b 65   attempt to make
d2270 0a 2a 2a 20 69 74 20 77 6f 72 6b 20 62 65 74 74  .** it work bett
d2280 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 53 51  er..**.** The SQ
d2290 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 6d 61 63  LITE_NO_SYNC mac
d22a0 72 6f 20 64 69 73 61 62 6c 65 73 20 61 6c 6c 20  ro disables all 
d22b0 66 73 79 6e 63 28 29 73 2e 20 20 54 68 69 73 20  fsync()s.  This 
d22c0 69 73 20 75 73 65 66 75 6c 0a 2a 2a 20 66 6f 72  is useful.** for
d22d0 20 74 65 73 74 69 6e 67 20 77 68 65 6e 20 77 65   testing when we
d22e0 20 77 61 6e 74 20 74 6f 20 72 75 6e 20 74 68 72   want to run thr
d22f0 6f 75 67 68 20 74 68 65 20 74 65 73 74 20 73 75  ough the test su
d2300 69 74 65 20 71 75 69 63 6b 6c 79 2e 0a 2a 2a 20  ite quickly..** 
d2310 59 6f 75 20 61 72 65 20 73 74 72 6f 6e 67 6c 79  You are strongly
d2320 20 61 64 76 69 73 65 64 20 2a 6e 6f 74 2a 20 74   advised *not* t
d2330 6f 20 64 65 70 6c 6f 79 20 77 69 74 68 20 53 51  o deploy with SQ
d2340 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 2a 2a 20  LITE_NO_SYNC.** 
d2350 65 6e 61 62 6c 65 64 2c 20 68 6f 77 65 76 65 72  enabled, however
d2360 2c 20 73 69 6e 63 65 20 77 69 74 68 20 53 51 4c  , since with SQL
d2370 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6e 61 62  ITE_NO_SYNC enab
d2380 6c 65 64 2c 20 61 6e 20 4f 53 20 63 72 61 73 68  led, an OS crash
d2390 0a 2a 2a 20 6f 72 20 70 6f 77 65 72 20 66 61 69  .** or power fai
d23a0 6c 75 72 65 20 77 69 6c 6c 20 6c 69 6b 65 6c 79  lure will likely
d23b0 20 63 6f 72 72 75 70 74 20 74 68 65 20 64 61 74   corrupt the dat
d23c0 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  abase file..*/.s
d23d0 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 5f 66  tatic int full_f
d23e0 73 79 6e 63 28 69 6e 74 20 66 64 2c 20 69 6e 74  sync(int fd, int
d23f0 20 66 75 6c 6c 53 79 6e 63 2c 20 69 6e 74 20 64   fullSync, int d
d2400 61 74 61 4f 6e 6c 79 29 7b 0a 20 20 69 6e 74 20  ataOnly){.  int 
d2410 72 63 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f  rc;..  /* The fo
d2420 6c 6c 6f 77 69 6e 67 20 22 69 66 64 65 66 2f 65  llowing "ifdef/e
d2430 6c 69 66 2f 65 6c 73 65 2f 22 20 62 6c 6f 63 6b  lif/else/" block
d2440 20 68 61 73 20 74 68 65 20 73 61 6d 65 20 73 74   has the same st
d2450 72 75 63 74 75 72 65 20 61 73 0a 20 20 2a 2a 20  ructure as.  ** 
d2460 74 68 65 20 6f 6e 65 20 62 65 6c 6f 77 2e 20 49  the one below. I
d2470 74 20 69 73 20 72 65 70 6c 69 63 61 74 65 64 20  t is replicated 
d2480 68 65 72 65 20 73 6f 6c 65 6c 79 20 74 6f 20 61  here solely to a
d2490 76 6f 69 64 20 63 6c 75 74 74 65 72 69 6e 67 20  void cluttering 
d24a0 0a 20 20 2a 2a 20 75 70 20 74 68 65 20 72 65 61  .  ** up the rea
d24b0 6c 20 63 6f 64 65 20 77 69 74 68 20 74 68 65 20  l code with the 
d24c0 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
d24d0 28 29 20 6d 61 63 72 6f 73 2e 0a 20 20 2a 2f 0a  () macros..  */.
d24e0 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4e 4f  #ifdef SQLITE_NO
d24f0 5f 53 59 4e 43 0a 20 20 55 4e 55 53 45 44 5f 50  _SYNC.  UNUSED_P
d2500 41 52 41 4d 45 54 45 52 28 66 64 29 3b 0a 20 20  ARAMETER(fd);.  
d2510 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
d2520 28 66 75 6c 6c 53 79 6e 63 29 3b 0a 20 20 55 4e  (fullSync);.  UN
d2530 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 64  USED_PARAMETER(d
d2540 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c 69 66 20  ataOnly);.#elif 
d2550 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 0a 20  HAVE_FULLFSYNC. 
d2560 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
d2570 52 28 64 61 74 61 4f 6e 6c 79 29 3b 0a 23 65 6c  R(dataOnly);.#el
d2580 73 65 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  se.  UNUSED_PARA
d2590 4d 45 54 45 52 28 66 75 6c 6c 53 79 6e 63 29 3b  METER(fullSync);
d25a0 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65  .#endif..  /* Re
d25b0 63 6f 72 64 20 74 68 65 20 6e 75 6d 62 65 72 20  cord the number 
d25c0 6f 66 20 74 69 6d 65 73 20 74 68 61 74 20 77 65  of times that we
d25d0 20 64 6f 20 61 20 6e 6f 72 6d 61 6c 20 66 73 79   do a normal fsy
d25e0 6e 63 28 29 20 61 6e 64 20 0a 20 20 2a 2a 20 46  nc() and .  ** F
d25f0 55 4c 4c 53 59 4e 43 2e 20 20 54 68 69 73 20 69  ULLSYNC.  This i
d2600 73 20 75 73 65 64 20 64 75 72 69 6e 67 20 74 65  s used during te
d2610 73 74 69 6e 67 20 74 6f 20 76 65 72 69 66 79 20  sting to verify 
d2620 74 68 61 74 20 74 68 69 73 20 70 72 6f 63 65 64  that this proced
d2630 75 72 65 0a 20 20 2a 2a 20 67 65 74 73 20 63 61  ure.  ** gets ca
d2640 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 63 6f  lled with the co
d2650 72 72 65 63 74 20 61 72 67 75 6d 65 6e 74 73 2e  rrect arguments.
d2660 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
d2670 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20 66  ITE_TEST.  if( f
d2680 75 6c 6c 53 79 6e 63 20 29 20 73 71 6c 69 74 65  ullSync ) sqlite
d2690 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74  3_fullsync_count
d26a0 2b 2b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 79  ++;.  sqlite3_sy
d26b0 6e 63 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64  nc_count++;.#end
d26c0 69 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63  if..  /* If we c
d26d0 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
d26e0 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20   SQLITE_NO_SYNC 
d26f0 66 6c 61 67 2c 20 74 68 65 6e 20 73 79 6e 63 69  flag, then synci
d2700 6e 67 20 69 73 20 61 0a 20 20 2a 2a 20 6e 6f 2d  ng is a.  ** no-
d2710 6f 70 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53  op.  */.#ifdef S
d2720 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 0a 20 20  QLITE_NO_SYNC.  
d2730 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
d2740 23 65 6c 69 66 20 48 41 56 45 5f 46 55 4c 4c 46  #elif HAVE_FULLF
d2750 53 59 4e 43 0a 20 20 69 66 28 20 66 75 6c 6c 53  SYNC.  if( fullS
d2760 79 6e 63 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ync ){.    rc = 
d2770 66 63 6e 74 6c 28 66 64 2c 20 46 5f 46 55 4c 4c  fcntl(fd, F_FULL
d2780 46 53 59 4e 43 2c 20 30 29 3b 0a 20 20 7d 65 6c  FSYNC, 0);.  }el
d2790 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a  se{.    rc = 1;.
d27a0 20 20 7d 0a 20 20 2f 2a 20 49 66 20 74 68 65 20    }.  /* If the 
d27b0 46 55 4c 4c 46 53 59 4e 43 20 66 61 69 6c 65 64  FULLFSYNC failed
d27c0 2c 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 61  , fall back to a
d27d0 74 74 65 6d 70 74 69 6e 67 20 61 6e 20 66 73 79  ttempting an fsy
d27e0 6e 63 28 29 2e 0a 20 20 20 2a 20 49 74 20 73 68  nc()..   * It sh
d27f0 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
d2800 62 6c 65 20 66 6f 72 20 66 75 6c 6c 66 73 79 6e  ble for fullfsyn
d2810 63 20 74 6f 20 66 61 69 6c 20 6f 6e 20 74 68 65  c to fail on the
d2820 20 6c 6f 63 61 6c 20 0a 20 20 20 2a 20 66 69 6c   local .   * fil
d2830 65 20 73 79 73 74 65 6d 20 28 6f 6e 20 4f 53 58  e system (on OSX
d2840 29 2c 20 73 6f 20 66 61 69 6c 75 72 65 20 69 6e  ), so failure in
d2850 64 69 63 61 74 65 73 20 74 68 61 74 20 46 55 4c  dicates that FUL
d2860 4c 46 53 59 4e 43 0a 20 20 20 2a 20 69 73 6e 27  LFSYNC.   * isn'
d2870 74 20 73 75 70 70 6f 72 74 65 64 20 66 6f 72 20  t supported for 
d2880 74 68 69 73 20 66 69 6c 65 20 73 79 73 74 65 6d  this file system
d2890 2e 20 53 6f 2c 20 61 74 74 65 6d 70 74 20 61 6e  . So, attempt an
d28a0 20 66 73 79 6e 63 20 0a 20 20 20 2a 20 61 6e 64   fsync .   * and
d28b0 20 28 66 6f 72 20 6e 6f 77 29 20 69 67 6e 6f 72   (for now) ignor
d28c0 65 20 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f  e the overhead o
d28d0 66 20 61 20 73 75 70 65 72 66 6c 75 6f 75 73 20  f a superfluous 
d28e0 66 63 6e 74 6c 20 63 61 6c 6c 2e 20 20 0a 20 20  fcntl call.  .  
d28f0 20 2a 20 49 74 27 64 20 62 65 20 62 65 74 74 65   * It'd be bette
d2900 72 20 74 6f 20 64 65 74 65 63 74 20 66 75 6c 6c  r to detect full
d2910 66 73 79 6e 63 20 73 75 70 70 6f 72 74 20 6f 6e  fsync support on
d2920 63 65 20 61 6e 64 20 61 76 6f 69 64 20 0a 20 20  ce and avoid .  
d2930 20 2a 20 74 68 65 20 66 63 6e 74 6c 20 63 61 6c   * the fcntl cal
d2940 6c 20 65 76 65 72 79 20 74 69 6d 65 20 73 79 6e  l every time syn
d2950 63 20 69 73 20 63 61 6c 6c 65 64 2e 0a 20 20 20  c is called..   
d2960 2a 2f 0a 20 20 69 66 28 20 72 63 20 29 20 72 63  */.  if( rc ) rc
d2970 20 3d 20 66 73 79 6e 63 28 66 64 29 3b 0a 0a 23   = fsync(fd);..#
d2980 65 6c 73 65 20 0a 20 20 69 66 28 20 64 61 74 61  else .  if( data
d2990 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  Only ){.    rc =
d29a0 20 66 64 61 74 61 73 79 6e 63 28 66 64 29 3b 0a   fdatasync(fd);.
d29b0 20 20 20 20 69 66 28 20 49 53 5f 56 58 57 4f 52      if( IS_VXWOR
d29c0 4b 53 20 26 26 20 72 63 3d 3d 2d 31 20 26 26 20  KS && rc==-1 && 
d29d0 65 72 72 6e 6f 3d 3d 45 4e 4f 54 53 55 50 20 29  errno==ENOTSUP )
d29e0 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 66 73 79  {.      rc = fsy
d29f0 6e 63 28 66 64 29 3b 0a 20 20 20 20 7d 0a 20 20  nc(fd);.    }.  
d2a00 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20  }else{.    rc = 
d2a10 66 73 79 6e 63 28 66 64 29 3b 0a 20 20 7d 0a 23  fsync(fd);.  }.#
d2a20 65 6e 64 69 66 20 2f 2a 20 69 66 64 65 66 20 53  endif /* ifdef S
d2a30 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e 43 20 65 6c  QLITE_NO_SYNC el
d2a40 69 66 20 48 41 56 45 5f 46 55 4c 4c 46 53 59 4e  if HAVE_FULLFSYN
d2a50 43 20 2a 2f 0a 0a 20 20 69 66 28 20 49 53 5f 56  C */..  if( IS_V
d2a60 58 57 4f 52 4b 53 20 26 26 20 72 63 21 3d 20 2d  XWORKS && rc!= -
d2a70 31 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 30 3b  1 ){.    rc = 0;
d2a80 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
d2a90 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20  ;.}../*.** Make 
d2aa0 73 75 72 65 20 61 6c 6c 20 77 72 69 74 65 73 20  sure all writes 
d2ab0 74 6f 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  to a particular 
d2ac0 66 69 6c 65 20 61 72 65 20 63 6f 6d 6d 69 74 74  file are committ
d2ad0 65 64 20 74 6f 20 64 69 73 6b 2e 0a 2a 2a 0a 2a  ed to disk..**.*
d2ae0 2a 20 49 66 20 64 61 74 61 4f 6e 6c 79 3d 3d 30  * If dataOnly==0
d2af0 20 74 68 65 6e 20 62 6f 74 68 20 74 68 65 20 66   then both the f
d2b00 69 6c 65 20 69 74 73 65 6c 66 20 61 6e 64 20 69  ile itself and i
d2b10 74 73 20 6d 65 74 61 64 61 74 61 20 28 66 69 6c  ts metadata (fil
d2b20 65 0a 2a 2a 20 73 69 7a 65 2c 20 61 63 63 65 73  e.** size, acces
d2b30 73 20 74 69 6d 65 2c 20 65 74 63 29 20 61 72 65  s time, etc) are
d2b40 20 73 79 6e 63 65 64 2e 20 20 49 66 20 64 61 74   synced.  If dat
d2b50 61 4f 6e 6c 79 21 3d 30 20 74 68 65 6e 20 6f 6e  aOnly!=0 then on
d2b60 6c 79 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20 64  ly the.** file d
d2b70 61 74 61 20 69 73 20 73 79 6e 63 65 64 2e 0a 2a  ata is synced..*
d2b80 2a 0a 2a 2a 20 55 6e 64 65 72 20 55 6e 69 78 2c  *.** Under Unix,
d2b90 20 61 6c 73 6f 20 6d 61 6b 65 20 73 75 72 65 20   also make sure 
d2ba0 74 68 61 74 20 74 68 65 20 64 69 72 65 63 74 6f  that the directo
d2bb0 72 79 20 65 6e 74 72 79 20 66 6f 72 20 74 68 65  ry entry for the
d2bc0 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65   file.** has bee
d2bd0 6e 20 63 72 65 61 74 65 64 20 62 79 20 66 73 79  n created by fsy
d2be0 6e 63 2d 69 6e 67 20 74 68 65 20 64 69 72 65 63  nc-ing the direc
d2bf0 74 6f 72 79 20 74 68 61 74 20 63 6f 6e 74 61 69  tory that contai
d2c00 6e 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20  ns the file..** 
d2c10 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 64 6f 20  If we do not do 
d2c20 74 68 69 73 20 61 6e 64 20 77 65 20 65 6e 63 6f  this and we enco
d2c30 75 6e 74 65 72 20 61 20 70 6f 77 65 72 20 66 61  unter a power fa
d2c40 69 6c 75 72 65 2c 20 74 68 65 20 64 69 72 65 63  ilure, the direc
d2c50 74 6f 72 79 0a 2a 2a 20 65 6e 74 72 79 20 66 6f  tory.** entry fo
d2c60 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6d 69  r the journal mi
d2c70 67 68 74 20 6e 6f 74 20 65 78 69 73 74 20 61 66  ght not exist af
d2c80 74 65 72 20 77 65 20 72 65 62 6f 6f 74 2e 20 20  ter we reboot.  
d2c90 54 68 65 20 6e 65 78 74 0a 2a 2a 20 53 51 4c 69  The next.** SQLi
d2ca0 74 65 20 74 6f 20 61 63 63 65 73 73 20 74 68 65  te to access the
d2cb0 20 66 69 6c 65 20 77 69 6c 6c 20 6e 6f 74 20 6b   file will not k
d2cc0 6e 6f 77 20 74 68 61 74 20 74 68 65 20 6a 6f 75  now that the jou
d2cd0 72 6e 61 6c 20 65 78 69 73 74 73 20 28 62 65 63  rnal exists (bec
d2ce0 61 75 73 65 0a 2a 2a 20 74 68 65 20 64 69 72 65  ause.** the dire
d2cf0 63 74 6f 72 79 20 65 6e 74 72 79 20 66 6f 72 20  ctory entry for 
d2d00 74 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20  the journal was 
d2d10 6e 65 76 65 72 20 63 72 65 61 74 65 64 29 20 61  never created) a
d2d20 6e 64 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  nd the transacti
d2d30 6f 6e 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 72  on.** will not r
d2d40 6f 6c 6c 20 62 61 63 6b 20 2d 20 70 6f 73 73 69  oll back - possi
d2d50 62 6c 79 20 6c 65 61 64 69 6e 67 20 74 6f 20 64  bly leading to d
d2d60 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74 69  atabase corrupti
d2d70 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
d2d80 74 20 75 6e 69 78 53 79 6e 63 28 73 71 6c 69 74  t unixSync(sqlit
d2d90 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d2da0 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74 20 72   flags){.  int r
d2db0 63 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  c;.  unixFile *p
d2dc0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d2dd0 2a 29 69 64 3b 0a 0a 20 20 69 6e 74 20 69 73 44  *)id;..  int isD
d2de0 61 74 61 4f 6e 6c 79 20 3d 20 28 66 6c 61 67 73  ataOnly = (flags
d2df0 26 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  &SQLITE_SYNC_DAT
d2e00 41 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73  AONLY);.  int is
d2e10 46 75 6c 6c 73 79 6e 63 20 3d 20 28 66 6c 61 67  Fullsync = (flag
d2e20 73 26 30 78 30 46 29 3d 3d 53 51 4c 49 54 45 5f  s&0x0F)==SQLITE_
d2e30 53 59 4e 43 5f 46 55 4c 4c 3b 0a 0a 20 20 2f 2a  SYNC_FULL;..  /*
d2e40 20 43 68 65 63 6b 20 74 68 61 74 20 6f 6e 65 20   Check that one 
d2e50 6f 66 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  of SQLITE_SYNC_N
d2e60 4f 52 4d 41 4c 20 6f 72 20 46 55 4c 4c 20 77 61  ORMAL or FULL wa
d2e70 73 20 70 61 73 73 65 64 20 2a 2f 0a 20 20 61 73  s passed */.  as
d2e80 73 65 72 74 28 28 66 6c 61 67 73 26 30 78 30 46  sert((flags&0x0F
d2e90 29 3d 3d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e  )==SQLITE_SYNC_N
d2ea0 4f 52 4d 41 4c 0a 20 20 20 20 20 20 7c 7c 20 28  ORMAL.      || (
d2eb0 66 6c 61 67 73 26 30 78 30 46 29 3d 3d 53 51 4c  flags&0x0F)==SQL
d2ec0 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c 0a 20 20  ITE_SYNC_FULL.  
d2ed0 29 3b 0a 0a 20 20 2f 2a 20 55 6e 69 78 20 63 61  );..  /* Unix ca
d2ee0 6e 6e 6f 74 2c 20 62 75 74 20 73 6f 6d 65 20 73  nnot, but some s
d2ef0 79 73 74 65 6d 73 20 6d 61 79 20 72 65 74 75 72  ystems may retur
d2f00 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 66 72  n SQLITE_FULL fr
d2f10 6f 6d 20 68 65 72 65 2e 20 54 68 69 73 0a 20 20  om here. This.  
d2f20 2a 2a 20 6c 69 6e 65 20 69 73 20 74 6f 20 74 65  ** line is to te
d2f30 73 74 20 74 68 61 74 20 64 6f 69 6e 67 20 73 6f  st that doing so
d2f40 20 64 6f 65 73 20 6e 6f 74 20 63 61 75 73 65 20   does not cause 
d2f50 61 6e 79 20 70 72 6f 62 6c 65 6d 73 2e 0a 20 20  any problems..  
d2f60 2a 2f 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73  */.  SimulateDis
d2f70 6b 66 75 6c 6c 45 72 72 6f 72 28 20 72 65 74 75  kfullError( retu
d2f80 72 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20 29  rn SQLITE_FULL )
d2f90 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  ;..  assert( pFi
d2fa0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 32  le );.  OSTRACE2
d2fb0 28 22 53 59 4e 43 20 20 20 20 25 2d 33 64 5c 6e  ("SYNC    %-3d\n
d2fc0 22 2c 20 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20  ", pFile->h);.  
d2fd0 72 63 20 3d 20 66 75 6c 6c 5f 66 73 79 6e 63 28  rc = full_fsync(
d2fe0 70 46 69 6c 65 2d 3e 68 2c 20 69 73 46 75 6c 6c  pFile->h, isFull
d2ff0 73 79 6e 63 2c 20 69 73 44 61 74 61 4f 6e 6c 79  sync, isDataOnly
d3000 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  );.  SimulateIOE
d3010 72 72 6f 72 28 20 72 63 3d 31 20 29 3b 0a 20 20  rror( rc=1 );.  
d3020 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65  if( rc ){.    re
d3030 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45 52  turn SQLITE_IOER
d3040 52 5f 46 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 69  R_FSYNC;.  }.  i
d3050 66 28 20 70 46 69 6c 65 2d 3e 64 69 72 66 64 3e  f( pFile->dirfd>
d3060 3d 30 20 29 7b 0a 20 20 20 20 4f 53 54 52 41 43  =0 ){.    OSTRAC
d3070 45 34 28 22 44 49 52 53 59 4e 43 20 25 2d 33 64  E4("DIRSYNC %-3d
d3080 20 28 68 61 76 65 5f 66 75 6c 6c 66 73 79 6e 63   (have_fullfsync
d3090 3d 25 64 20 66 75 6c 6c 73 79 6e 63 3d 25 64 29  =%d fullsync=%d)
d30a0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 64 69 72 66  \n", pFile->dirf
d30b0 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 48  d,.            H
d30c0 41 56 45 5f 46 55 4c 4c 46 53 59 4e 43 2c 20 69  AVE_FULLFSYNC, i
d30d0 73 46 75 6c 6c 73 79 6e 63 29 3b 0a 23 69 66 6e  sFullsync);.#ifn
d30e0 64 65 66 20 53 51 4c 49 54 45 5f 44 49 53 41 42  def SQLITE_DISAB
d30f0 4c 45 5f 44 49 52 53 59 4e 43 0a 20 20 20 20 2f  LE_DIRSYNC.    /
d3100 2a 20 54 68 65 20 64 69 72 65 63 74 6f 72 79 20  * The directory 
d3110 73 79 6e 63 20 69 73 20 6f 6e 6c 79 20 61 74 74  sync is only att
d3120 65 6d 70 74 65 64 20 69 66 20 66 75 6c 6c 5f 66  empted if full_f
d3130 73 79 6e 63 20 69 73 0a 20 20 20 20 2a 2a 20 74  sync is.    ** t
d3140 75 72 6e 65 64 20 6f 66 66 20 6f 72 20 75 6e 61  urned off or una
d3150 76 61 69 6c 61 62 6c 65 2e 20 20 49 66 20 61 20  vailable.  If a 
d3160 66 75 6c 6c 5f 66 73 79 6e 63 20 6f 63 63 75 72  full_fsync occur
d3170 72 65 64 20 61 62 6f 76 65 2c 0a 20 20 20 20 2a  red above,.    *
d3180 2a 20 74 68 65 6e 20 74 68 65 20 64 69 72 65 63  * then the direc
d3190 74 6f 72 79 20 73 79 6e 63 20 69 73 20 73 75 70  tory sync is sup
d31a0 65 72 66 6c 75 6f 75 73 2e 0a 20 20 20 20 2a 2f  erfluous..    */
d31b0 0a 20 20 20 20 69 66 28 20 28 21 48 41 56 45 5f  .    if( (!HAVE_
d31c0 46 55 4c 4c 46 53 59 4e 43 20 7c 7c 20 21 69 73  FULLFSYNC || !is
d31d0 46 75 6c 6c 73 79 6e 63 29 20 26 26 20 66 75 6c  Fullsync) && ful
d31e0 6c 5f 66 73 79 6e 63 28 70 46 69 6c 65 2d 3e 64  l_fsync(pFile->d
d31f0 69 72 66 64 2c 30 2c 30 29 20 29 7b 0a 20 20 20  irfd,0,0) ){.   
d3200 20 20 20 20 2f 2a 0a 20 20 20 20 20 20 20 2a 2a      /*.       **
d3210 20 57 65 20 68 61 76 65 20 72 65 63 65 69 76 65   We have receive
d3220 64 20 6d 75 6c 74 69 70 6c 65 20 72 65 70 6f 72  d multiple repor
d3230 74 73 20 6f 66 20 66 73 79 6e 63 28 29 20 72 65  ts of fsync() re
d3240 74 75 72 6e 69 6e 67 0a 20 20 20 20 20 20 20 2a  turning.       *
d3250 2a 20 65 72 72 6f 72 73 20 77 68 65 6e 20 61 70  * errors when ap
d3260 70 6c 69 65 64 20 74 6f 20 64 69 72 65 63 74 6f  plied to directo
d3270 72 69 65 73 20 6f 6e 20 63 65 72 74 61 69 6e 20  ries on certain 
d3280 66 69 6c 65 20 73 79 73 74 65 6d 73 2e 0a 20 20  file systems..  
d3290 20 20 20 20 20 2a 2a 20 41 20 66 61 69 6c 65 64       ** A failed
d32a0 20 64 69 72 65 63 74 6f 72 79 20 73 79 6e 63 20   directory sync 
d32b0 69 73 20 6e 6f 74 20 61 20 62 69 67 20 64 65 61  is not a big dea
d32c0 6c 2e 20 20 53 6f 20 69 74 20 73 65 65 6d 73 0a  l.  So it seems.
d32d0 20 20 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72         ** better
d32e0 20 74 6f 20 69 67 6e 6f 72 65 20 74 68 65 20 65   to ignore the e
d32f0 72 72 6f 72 2e 20 20 54 69 63 6b 65 74 20 23 31  rror.  Ticket #1
d3300 36 35 37 0a 20 20 20 20 20 20 20 2a 2f 0a 20 20  657.       */.  
d3310 20 20 20 20 20 2f 2a 20 72 65 74 75 72 6e 20 53       /* return S
d3320 51 4c 49 54 45 5f 49 4f 45 52 52 3b 20 2a 2f 0a  QLITE_IOERR; */.
d3330 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
d3340 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d 3e 64 69   close(pFile->di
d3350 72 66 64 29 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6e  rfd);  /* Only n
d3360 65 65 64 20 74 6f 20 73 79 6e 63 20 6f 6e 63 65  eed to sync once
d3370 2c 20 73 6f 20 63 6c 6f 73 65 20 74 68 65 20 64  , so close the d
d3380 69 72 65 63 74 6f 72 79 20 2a 2f 0a 20 20 20 20  irectory */.    
d3390 70 46 69 6c 65 2d 3e 64 69 72 66 64 20 3d 20 2d  pFile->dirfd = -
d33a0 31 3b 20 20 20 20 2f 2a 20 77 68 65 6e 20 77 65  1;    /* when we
d33b0 20 61 72 65 20 64 6f 6e 65 2e 20 2a 2f 0a 20 20   are done. */.  
d33c0 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
d33d0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
d33e0 72 75 6e 63 61 74 65 20 61 6e 20 6f 70 65 6e 20  runcate an open 
d33f0 66 69 6c 65 20 74 6f 20 61 20 73 70 65 63 69 66  file to a specif
d3400 69 65 64 20 73 69 7a 65 0a 2a 2f 0a 73 74 61 74  ied size.*/.stat
d3410 69 63 20 69 6e 74 20 75 6e 69 78 54 72 75 6e 63  ic int unixTrunc
d3420 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ate(sqlite3_file
d3430 20 2a 69 64 2c 20 69 36 34 20 6e 42 79 74 65 29   *id, i64 nByte)
d3440 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
d3450 73 65 72 74 28 20 69 64 20 29 3b 0a 20 20 53 69  sert( id );.  Si
d3460 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
d3470 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d3480 52 52 5f 54 52 55 4e 43 41 54 45 20 29 3b 0a 20  RR_TRUNCATE );. 
d3490 20 72 63 20 3d 20 66 74 72 75 6e 63 61 74 65 28   rc = ftruncate(
d34a0 28 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d  ((unixFile*)id)-
d34b0 3e 68 2c 20 28 6f 66 66 5f 74 29 6e 42 79 74 65  >h, (off_t)nByte
d34c0 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a 20  );.  if( rc ){. 
d34d0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d34e0 5f 49 4f 45 52 52 5f 54 52 55 4e 43 41 54 45 3b  _IOERR_TRUNCATE;
d34f0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65  .  }else{.    re
d3500 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
d3510 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 74    }.}../*.** Det
d3520 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72 65  ermine the curre
d3530 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69 6c  nt size of a fil
d3540 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73 74  e in bytes.*/.st
d3550 61 74 69 63 20 69 6e 74 20 75 6e 69 78 46 69 6c  atic int unixFil
d3560 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
d3570 6c 65 20 2a 69 64 2c 20 69 36 34 20 2a 70 53 69  le *id, i64 *pSi
d3580 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ze){.  int rc;. 
d3590 20 73 74 72 75 63 74 20 73 74 61 74 20 62 75 66   struct stat buf
d35a0 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 20 29  ;.  assert( id )
d35b0 3b 0a 20 20 72 63 20 3d 20 66 73 74 61 74 28 28  ;.  rc = fstat((
d35c0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 29 2d 3e  (unixFile*)id)->
d35d0 68 2c 20 26 62 75 66 29 3b 0a 20 20 53 69 6d 75  h, &buf);.  Simu
d35e0 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72 63 3d  lateIOError( rc=
d35f0 31 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30  1 );.  if( rc!=0
d3600 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
d3610 51 4c 49 54 45 5f 49 4f 45 52 52 5f 46 53 54 41  QLITE_IOERR_FSTA
d3620 54 3b 0a 20 20 7d 0a 20 20 2a 70 53 69 7a 65 20  T;.  }.  *pSize 
d3630 3d 20 62 75 66 2e 73 74 5f 73 69 7a 65 3b 0a 0a  = buf.st_size;..
d3640 20 20 2f 2a 20 57 68 65 6e 20 6f 70 65 6e 69 6e    /* When openin
d3650 67 20 61 20 7a 65 72 6f 2d 73 69 7a 65 20 64 61  g a zero-size da
d3660 74 61 62 61 73 65 2c 20 74 68 65 20 66 69 6e 64  tabase, the find
d3670 4c 6f 63 6b 49 6e 66 6f 28 29 20 70 72 6f 63 65  LockInfo() proce
d3680 64 75 72 65 0a 20 20 2a 2a 20 77 72 69 74 65 73  dure.  ** writes
d3690 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 69   a single byte i
d36a0 6e 74 6f 20 74 68 61 74 20 66 69 6c 65 20 69 6e  nto that file in
d36b0 20 6f 72 64 65 72 20 74 6f 20 77 6f 72 6b 20 61   order to work a
d36c0 72 6f 75 6e 64 20 61 20 62 75 67 0a 20 20 2a 2a  round a bug.  **
d36d0 20 69 6e 20 74 68 65 20 4f 53 2d 58 20 6d 73 64   in the OS-X msd
d36e0 6f 73 20 66 69 6c 65 73 79 73 74 65 6d 2e 20 20  os filesystem.  
d36f0 49 6e 20 6f 72 64 65 72 20 74 6f 20 61 76 6f 69  In order to avoi
d3700 64 20 70 72 6f 62 6c 65 6d 73 20 77 69 74 68 20  d problems with 
d3710 75 70 70 65 72 0a 20 20 2a 2a 20 6c 61 79 65 72  upper.  ** layer
d3720 73 2c 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65  s, we need to re
d3730 70 6f 72 74 20 74 68 69 73 20 66 69 6c 65 20 73  port this file s
d3740 69 7a 65 20 61 73 20 7a 65 72 6f 20 65 76 65 6e  ize as zero even
d3750 20 74 68 6f 75 67 68 20 69 74 20 69 73 0a 20 20   though it is.  
d3760 2a 2a 20 72 65 61 6c 6c 79 20 31 2e 20 20 20 54  ** really 1.   T
d3770 69 63 6b 65 74 20 23 33 32 36 30 2e 0a 20 20 2a  icket #3260..  *
d3780 2f 0a 20 20 69 66 28 20 2a 70 53 69 7a 65 3d 3d  /.  if( *pSize==
d3790 31 20 29 20 2a 70 53 69 7a 65 20 3d 20 30 3b 0a  1 ) *pSize = 0;.
d37a0 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
d37b0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  E_OK;.}../*.** T
d37c0 68 69 73 20 72 6f 75 74 69 6e 65 20 74 72 61 6e  his routine tran
d37d0 73 6c 61 74 65 73 20 61 20 73 74 61 6e 64 61 72  slates a standar
d37e0 64 20 50 4f 53 49 58 20 65 72 72 6e 6f 20 63 6f  d POSIX errno co
d37f0 64 65 20 69 6e 74 6f 20 73 6f 6d 65 74 68 69 6e  de into somethin
d3800 67 0a 2a 2a 20 75 73 65 66 75 6c 20 74 6f 20 74  g.** useful to t
d3810 68 65 20 63 6c 69 65 6e 74 73 20 6f 66 20 74 68  he clients of th
d3820 65 20 73 71 6c 69 74 65 33 20 66 75 6e 63 74 69  e sqlite3 functi
d3830 6f 6e 73 2e 20 20 53 70 65 63 69 66 69 63 61 6c  ons.  Specifical
d3840 6c 79 2c 20 69 74 20 69 73 0a 2a 2a 20 69 6e 74  ly, it is.** int
d3850 65 6e 64 65 64 20 74 6f 20 74 72 61 6e 73 6c 61  ended to transla
d3860 74 65 20 61 20 76 61 72 69 65 74 79 20 6f 66 20  te a variety of 
d3870 22 74 72 79 20 61 67 61 69 6e 22 20 65 72 72 6f  "try again" erro
d3880 72 73 20 69 6e 74 6f 20 53 51 4c 49 54 45 5f 42  rs into SQLITE_B
d3890 55 53 59 0a 2a 2a 20 61 6e 64 20 61 20 76 61 72  USY.** and a var
d38a0 69 65 74 79 20 6f 66 20 22 70 6c 65 61 73 65 20  iety of "please 
d38b0 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 20 64  close the file d
d38c0 65 73 63 72 69 70 74 6f 72 20 4e 4f 57 22 20 65  escriptor NOW" e
d38d0 72 72 6f 72 73 20 69 6e 74 6f 20 0a 2a 2a 20 53  rrors into .** S
d38e0 51 4c 49 54 45 5f 49 4f 45 52 52 0a 2a 2a 20 0a  QLITE_IOERR.** .
d38f0 2a 2a 20 45 72 72 6f 72 73 20 64 75 72 69 6e 67  ** Errors during
d3900 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20   initialization 
d3910 6f 66 20 6c 6f 63 6b 73 2c 20 6f 72 20 66 69 6c  of locks, or fil
d3920 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72 74  e system support
d3930 20 66 6f 72 20 6c 6f 63 6b 73 2c 0a 2a 2a 20 73   for locks,.** s
d3940 68 6f 75 6c 64 20 68 61 6e 64 6c 65 20 45 4e 4f  hould handle ENO
d3950 4c 43 4b 2c 20 45 4e 4f 54 53 55 50 2c 20 45 4f  LCK, ENOTSUP, EO
d3960 50 4e 4f 54 53 55 50 50 20 73 65 70 61 72 61 74  PNOTSUPP separat
d3970 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ely..*/.static i
d3980 6e 74 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72  nt sqliteErrorFr
d3990 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 69 6e 74  omPosixError(int
d39a0 20 70 6f 73 69 78 45 72 72 6f 72 2c 20 69 6e 74   posixError, int
d39b0 20 73 71 6c 69 74 65 49 4f 45 72 72 29 20 7b 0a   sqliteIOErr) {.
d39c0 20 20 73 77 69 74 63 68 20 28 70 6f 73 69 78 45    switch (posixE
d39d0 72 72 6f 72 29 20 7b 0a 20 20 63 61 73 65 20 30  rror) {.  case 0
d39e0 3a 20 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  : .    return SQ
d39f0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 0a 20 20  LITE_OK;.    .  
d3a00 63 61 73 65 20 45 41 47 41 49 4e 3a 0a 20 20 63  case EAGAIN:.  c
d3a10 61 73 65 20 45 54 49 4d 45 44 4f 55 54 3a 0a 20  ase ETIMEDOUT:. 
d3a20 20 63 61 73 65 20 45 42 55 53 59 3a 0a 20 20 63   case EBUSY:.  c
d3a30 61 73 65 20 45 49 4e 54 52 3a 0a 20 20 63 61 73  ase EINTR:.  cas
d3a40 65 20 45 4e 4f 4c 43 4b 3a 20 20 0a 20 20 20 20  e ENOLCK:  .    
d3a50 2f 2a 20 72 61 6e 64 6f 6d 20 4e 46 53 20 72 65  /* random NFS re
d3a60 74 72 79 20 65 72 72 6f 72 2c 20 75 6e 6c 65 73  try error, unles
d3a70 73 20 64 75 72 69 6e 67 20 66 69 6c 65 20 73 79  s during file sy
d3a80 73 74 65 6d 20 73 75 70 70 6f 72 74 20 0a 20 20  stem support .  
d3a90 20 20 20 2a 20 69 6e 74 72 6f 73 70 65 63 74 69     * introspecti
d3aa0 6f 6e 2c 20 69 6e 20 77 68 69 63 68 20 69 74 20  on, in which it 
d3ab0 61 63 74 75 61 6c 6c 79 20 6d 65 61 6e 73 20 77  actually means w
d3ac0 68 61 74 20 69 74 20 73 61 79 73 20 2a 2f 0a 20  hat it says */. 
d3ad0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
d3ae0 5f 42 55 53 59 3b 0a 20 20 20 20 0a 20 20 63 61  _BUSY;.    .  ca
d3af0 73 65 20 45 41 43 43 45 53 3a 20 0a 20 20 20 20  se EACCES: .    
d3b00 2f 2a 20 45 41 43 43 45 53 20 69 73 20 6c 69 6b  /* EACCES is lik
d3b10 65 20 45 41 47 41 49 4e 20 64 75 72 69 6e 67 20  e EAGAIN during 
d3b20 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
d3b30 6e 73 2c 20 62 75 74 20 6e 6f 74 20 61 6e 79 20  ns, but not any 
d3b40 6f 74 68 65 72 20 74 69 6d 65 2a 2f 0a 20 20 20  other time*/.   
d3b50 20 69 66 28 20 28 73 71 6c 69 74 65 49 4f 45 72   if( (sqliteIOEr
d3b60 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  r == SQLITE_IOER
d3b70 52 5f 4c 4f 43 4b 29 20 7c 7c 20 0a 09 28 73 71  R_LOCK) || ..(sq
d3b80 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20 53 51 4c  liteIOErr == SQL
d3b90 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
d3ba0 29 20 7c 7c 20 0a 09 28 73 71 6c 69 74 65 49 4f  ) || ..(sqliteIO
d3bb0 45 72 72 20 3d 3d 20 53 51 4c 49 54 45 5f 49 4f  Err == SQLITE_IO
d3bc0 45 52 52 5f 52 44 4c 4f 43 4b 29 20 7c 7c 0a 09  ERR_RDLOCK) ||..
d3bd0 28 73 71 6c 69 74 65 49 4f 45 72 72 20 3d 3d 20  (sqliteIOErr == 
d3be0 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45  SQLITE_IOERR_CHE
d3bf0 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29 20  CKRESERVEDLOCK) 
d3c00 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
d3c10 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
d3c20 20 7d 0a 20 20 20 20 2f 2a 20 65 6c 73 65 20 66   }.    /* else f
d3c30 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20  all through */. 
d3c40 20 63 61 73 65 20 45 50 45 52 4d 3a 20 0a 20 20   case EPERM: .  
d3c50 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d3c60 50 45 52 4d 3b 0a 20 20 20 20 0a 20 20 63 61 73  PERM;.    .  cas
d3c70 65 20 45 44 45 41 44 4c 4b 3a 0a 20 20 20 20 72  e EDEADLK:.    r
d3c80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
d3c90 52 52 5f 42 4c 4f 43 4b 45 44 3b 0a 20 20 20 20  RR_BLOCKED;.    
d3ca0 0a 23 69 66 20 45 4f 50 4e 4f 54 53 55 50 50 21  .#if EOPNOTSUPP!
d3cb0 3d 45 4e 4f 54 53 55 50 0a 20 20 63 61 73 65 20  =ENOTSUP.  case 
d3cc0 45 4f 50 4e 4f 54 53 55 50 50 3a 20 0a 20 20 20  EOPNOTSUPP: .   
d3cd0 20 2f 2a 20 73 6f 6d 65 74 68 69 6e 67 20 77 65   /* something we
d3ce0 6e 74 20 74 65 72 72 69 62 6c 79 20 61 77 72 79  nt terribly awry
d3cf0 2c 20 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20  , unless during 
d3d00 66 69 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70  file system supp
d3d10 6f 72 74 20 0a 20 20 20 20 20 2a 20 69 6e 74 72  ort .     * intr
d3d20 6f 73 70 65 63 74 69 6f 6e 2c 20 69 6e 20 77 68  ospection, in wh
d3d30 69 63 68 20 69 74 20 61 63 74 75 61 6c 6c 79 20  ich it actually 
d3d40 6d 65 61 6e 73 20 77 68 61 74 20 69 74 20 73 61  means what it sa
d3d50 79 73 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66  ys */.#endif.#if
d3d60 64 65 66 20 45 4e 4f 54 53 55 50 0a 20 20 63 61  def ENOTSUP.  ca
d3d70 73 65 20 45 4e 4f 54 53 55 50 3a 20 0a 20 20 20  se ENOTSUP: .   
d3d80 20 2f 2a 20 69 6e 76 61 6c 69 64 20 66 64 2c 20   /* invalid fd, 
d3d90 75 6e 6c 65 73 73 20 64 75 72 69 6e 67 20 66 69  unless during fi
d3da0 6c 65 20 73 79 73 74 65 6d 20 73 75 70 70 6f 72  le system suppor
d3db0 74 20 69 6e 74 72 6f 73 70 65 63 74 69 6f 6e 2c  t introspection,
d3dc0 20 69 6e 20 77 68 69 63 68 20 0a 20 20 20 20 20   in which .     
d3dd0 2a 20 69 74 20 61 63 74 75 61 6c 6c 79 20 6d 65  * it actually me
d3de0 61 6e 73 20 77 68 61 74 20 69 74 20 73 61 79 73  ans what it says
d3df0 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 63 61 73   */.#endif.  cas
d3e00 65 20 45 49 4f 3a 0a 20 20 63 61 73 65 20 45 42  e EIO:.  case EB
d3e10 41 44 46 3a 0a 20 20 63 61 73 65 20 45 49 4e 56  ADF:.  case EINV
d3e20 41 4c 3a 0a 20 20 63 61 73 65 20 45 4e 4f 54 43  AL:.  case ENOTC
d3e30 4f 4e 4e 3a 0a 20 20 63 61 73 65 20 45 4e 4f 44  ONN:.  case ENOD
d3e40 45 56 3a 0a 20 20 63 61 73 65 20 45 4e 58 49 4f  EV:.  case ENXIO
d3e50 3a 0a 20 20 63 61 73 65 20 45 4e 4f 45 4e 54 3a  :.  case ENOENT:
d3e60 0a 20 20 63 61 73 65 20 45 53 54 41 4c 45 3a 0a  .  case ESTALE:.
d3e70 20 20 63 61 73 65 20 45 4e 4f 53 59 53 3a 0a 20    case ENOSYS:. 
d3e80 20 20 20 2f 2a 20 74 68 65 73 65 20 73 68 6f 75     /* these shou
d3e90 6c 64 20 66 6f 72 63 65 20 74 68 65 20 63 6c 69  ld force the cli
d3ea0 65 6e 74 20 74 6f 20 63 6c 6f 73 65 20 74 68 65  ent to close the
d3eb0 20 66 69 6c 65 20 61 6e 64 20 72 65 63 6f 6e 6e   file and reconn
d3ec0 65 63 74 20 2a 2f 0a 20 20 20 20 0a 20 20 64 65  ect */.    .  de
d3ed0 66 61 75 6c 74 3a 20 0a 20 20 20 20 72 65 74 75  fault: .    retu
d3ee0 72 6e 20 73 71 6c 69 74 65 49 4f 45 72 72 3b 0a  rn sqliteIOErr;.
d3ef0 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69    }.}../*.** Thi
d3f00 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73  s routine checks
d3f10 20 69 66 20 74 68 65 72 65 20 69 73 20 61 20 52   if there is a R
d3f20 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 68 65 6c  ESERVED lock hel
d3f30 64 20 6f 6e 20 74 68 65 20 73 70 65 63 69 66 69  d on the specifi
d3f40 65 64 0a 2a 2a 20 66 69 6c 65 20 62 79 20 74 68  ed.** file by th
d3f50 69 73 20 6f 72 20 61 6e 79 20 6f 74 68 65 72 20  is or any other 
d3f60 70 72 6f 63 65 73 73 2e 20 49 66 20 73 75 63 68  process. If such
d3f70 20 61 20 6c 6f 63 6b 20 69 73 20 68 65 6c 64 2c   a lock is held,
d3f80 20 73 65 74 20 2a 70 52 65 73 4f 75 74 0a 2a 2a   set *pResOut.**
d3f90 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65 72 6f 20 76   to a non-zero v
d3fa0 61 6c 75 65 20 6f 74 68 65 72 77 69 73 65 20 2a  alue otherwise *
d3fb0 70 52 65 73 4f 75 74 20 69 73 20 73 65 74 20 74  pResOut is set t
d3fc0 6f 20 7a 65 72 6f 2e 20 20 54 68 65 20 72 65 74  o zero.  The ret
d3fd0 75 72 6e 20 76 61 6c 75 65 0a 2a 2a 20 69 73 20  urn value.** is 
d3fe0 73 65 74 20 74 6f 20 53 51 4c 49 54 45 5f 4f 4b  set to SQLITE_OK
d3ff0 20 75 6e 6c 65 73 73 20 61 6e 20 49 2f 4f 20 65   unless an I/O e
d4000 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
d4010 6e 67 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67  ng lock checking
d4020 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d4030 75 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65  unixCheckReserve
d4040 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  dLock(sqlite3_fi
d4050 6c 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65  le *id, int *pRe
d4060 73 4f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20  sOut){.  int rc 
d4070 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
d4080 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b  nt reserved = 0;
d4090 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d40a0 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d40b0 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49  id;..  SimulateI
d40c0 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
d40d0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
d40e0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29  KRESERVEDLOCK; )
d40f0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  ;..  assert( pFi
d4100 6c 65 20 29 3b 0a 20 20 65 6e 74 65 72 4d 75 74  le );.  enterMut
d4110 65 78 28 29 3b 20 2f 2a 20 42 65 63 61 75 73 65  ex(); /* Because
d4120 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 20 69 73   pFile->pLock is
d4130 20 73 68 61 72 65 64 20 61 63 72 6f 73 73 20 74   shared across t
d4140 68 72 65 61 64 73 20 2a 2f 0a 0a 20 20 2f 2a 20  hreads */..  /* 
d4150 43 68 65 63 6b 20 69 66 20 61 20 74 68 72 65 61  Check if a threa
d4160 64 20 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73  d in this proces
d4170 73 20 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c  s holds such a l
d4180 6f 63 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69  ock */.  if( pFi
d4190 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74  le->pLock->lockt
d41a0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
d41b0 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
d41c0 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f  = 1;.  }..  /* O
d41d0 74 68 65 72 77 69 73 65 20 73 65 65 20 69 66 20  therwise see if 
d41e0 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65  some other proce
d41f0 73 73 20 68 6f 6c 64 73 20 69 74 2e 0a 20 20 2a  ss holds it..  *
d4200 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65  /.  if( !reserve
d4210 64 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20  d ){.    struct 
d4220 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 20 20  flock lock;.    
d4230 6c 6f 63 6b 2e 6c 5f 77 68 65 6e 63 65 20 3d 20  lock.l_whence = 
d4240 53 45 45 4b 5f 53 45 54 3b 0a 20 20 20 20 6c 6f  SEEK_SET;.    lo
d4250 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 52 45 53  ck.l_start = RES
d4260 45 52 56 45 44 5f 42 59 54 45 3b 0a 20 20 20 20  ERVED_BYTE;.    
d4270 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 3b 0a  lock.l_len = 1;.
d4280 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20      lock.l_type 
d4290 3d 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 69  = F_WRLCK;.    i
d42a0 66 20 28 2d 31 20 3d 3d 20 66 63 6e 74 6c 28 70  f (-1 == fcntl(p
d42b0 46 69 6c 65 2d 3e 68 2c 20 46 5f 47 45 54 4c 4b  File->h, F_GETLK
d42c0 2c 20 26 6c 6f 63 6b 29 29 20 7b 0a 20 20 20 20  , &lock)) {.    
d42d0 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d42e0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d  rrno;.      rc =
d42f0 20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d   sqliteErrorFrom
d4300 50 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e  PosixError(tErrn
d4310 6f 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  o, SQLITE_IOERR_
d4320 43 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43  CHECKRESERVEDLOC
d4330 4b 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  K);.      pFile-
d4340 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
d4350 72 6e 6f 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  rno;.    } else 
d4360 69 66 28 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 21  if( lock.l_type!
d4370 3d 46 5f 55 4e 4c 43 4b 20 29 7b 0a 20 20 20 20  =F_UNLCK ){.    
d4380 20 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a    reserved = 1;.
d4390 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 6c      }.  }.  .  l
d43a0 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 4f  eaveMutex();.  O
d43b0 53 54 52 41 43 45 34 28 22 54 45 53 54 20 57 52  STRACE4("TEST WR
d43c0 2d 4c 4f 43 4b 20 25 64 20 25 64 20 25 64 5c 6e  -LOCK %d %d %d\n
d43d0 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 2c  ", pFile->h, rc,
d43e0 20 72 65 73 65 72 76 65 64 29 3b 0a 0a 20 20 2a   reserved);..  *
d43f0 70 52 65 73 4f 75 74 20 3d 20 72 65 73 65 72 76  pResOut = reserv
d4400 65 64 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ed;.  return rc;
d4410 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
d4420 68 65 20 66 69 6c 65 20 77 69 74 68 20 74 68 65  he file with the
d4430 20 6c 6f 63 6b 20 73 70 65 63 69 66 69 65 64 20   lock specified 
d4440 62 79 20 70 61 72 61 6d 65 74 65 72 20 6c 6f 63  by parameter loc
d4450 6b 74 79 70 65 20 2d 20 6f 6e 65 0a 2a 2a 20 6f  ktype - one.** o
d4460 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  f the following:
d4470 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 31 29 20 53  .**.**     (1) S
d4480 48 41 52 45 44 5f 4c 4f 43 4b 0a 2a 2a 20 20 20  HARED_LOCK.**   
d4490 20 20 28 32 29 20 52 45 53 45 52 56 45 44 5f 4c    (2) RESERVED_L
d44a0 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 33 29 20 50  OCK.**     (3) P
d44b0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 0a 2a 2a 20 20  ENDING_LOCK.**  
d44c0 20 20 20 28 34 29 20 45 58 43 4c 55 53 49 56 45     (4) EXCLUSIVE
d44d0 5f 4c 4f 43 4b 0a 2a 2a 0a 2a 2a 20 53 6f 6d 65  _LOCK.**.** Some
d44e0 74 69 6d 65 73 20 77 68 65 6e 20 72 65 71 75 65  times when reque
d44f0 73 74 69 6e 67 20 6f 6e 65 20 6c 6f 63 6b 20 73  sting one lock s
d4500 74 61 74 65 2c 20 61 64 64 69 74 69 6f 6e 61 6c  tate, additional
d4510 20 6c 6f 63 6b 20 73 74 61 74 65 73 0a 2a 2a 20   lock states.** 
d4520 61 72 65 20 69 6e 73 65 72 74 65 64 20 69 6e 20  are inserted in 
d4530 62 65 74 77 65 65 6e 2e 20 20 54 68 65 20 6c 6f  between.  The lo
d4540 63 6b 69 6e 67 20 6d 69 67 68 74 20 66 61 69 6c  cking might fail
d4550 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 6c   on one of the l
d4560 61 74 65 72 0a 2a 2a 20 74 72 61 6e 73 69 74 69  ater.** transiti
d4570 6f 6e 73 20 6c 65 61 76 69 6e 67 20 74 68 65 20  ons leaving the 
d4580 6c 6f 63 6b 20 73 74 61 74 65 20 64 69 66 66 65  lock state diffe
d4590 72 65 6e 74 20 66 72 6f 6d 20 77 68 61 74 20 69  rent from what i
d45a0 74 20 73 74 61 72 74 65 64 20 62 75 74 0a 2a 2a  t started but.**
d45b0 20 73 74 69 6c 6c 20 73 68 6f 72 74 20 6f 66 20   still short of 
d45c0 69 74 73 20 67 6f 61 6c 2e 20 20 54 68 65 20 66  its goal.  The f
d45d0 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72 74 20 73  ollowing chart s
d45e0 68 6f 77 73 20 74 68 65 20 61 6c 6c 6f 77 65 64  hows the allowed
d45f0 0a 2a 2a 20 74 72 61 6e 73 69 74 69 6f 6e 73 20  .** transitions 
d4600 61 6e 64 20 74 68 65 20 69 6e 73 65 72 74 65 64  and the inserted
d4610 20 69 6e 74 65 72 6d 65 64 69 61 74 65 20 73 74   intermediate st
d4620 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 55  ates:.**.**    U
d4630 4e 4c 4f 43 4b 45 44 20 2d 3e 20 53 48 41 52 45  NLOCKED -> SHARE
d4640 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44 20 2d  D.**    SHARED -
d4650 3e 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 20 20  > RESERVED.**   
d4660 20 53 48 41 52 45 44 20 2d 3e 20 28 50 45 4e 44   SHARED -> (PEND
d4670 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53 49 56  ING) -> EXCLUSIV
d4680 45 0a 2a 2a 20 20 20 20 52 45 53 45 52 56 45 44  E.**    RESERVED
d4690 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
d46a0 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
d46b0 20 50 45 4e 44 49 4e 47 20 2d 3e 20 45 58 43 4c   PENDING -> EXCL
d46c0 55 53 49 56 45 0a 2a 2a 0a 2a 2a 20 54 68 69 73  USIVE.**.** This
d46d0 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 6f 6e   routine will on
d46e0 6c 79 20 69 6e 63 72 65 61 73 65 20 61 20 6c 6f  ly increase a lo
d46f0 63 6b 2e 20 20 55 73 65 20 74 68 65 20 73 71 6c  ck.  Use the sql
d4700 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 29 0a 2a  ite3OsUnlock().*
d4710 2a 20 72 6f 75 74 69 6e 65 20 74 6f 20 6c 6f 77  * routine to low
d4720 65 72 20 61 20 6c 6f 63 6b 69 6e 67 20 6c 65 76  er a locking lev
d4730 65 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  el..*/.static in
d4740 74 20 75 6e 69 78 4c 6f 63 6b 28 73 71 6c 69 74  t unixLock(sqlit
d4750 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
d4760 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 2f 2a   locktype){.  /*
d4770 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 64   The following d
d4780 65 73 63 72 69 62 65 73 20 74 68 65 20 69 6d 70  escribes the imp
d4790 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
d47a0 68 65 20 76 61 72 69 6f 75 73 20 6c 6f 63 6b 73  he various locks
d47b0 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f 63 6b 20 74   and.  ** lock t
d47c0 72 61 6e 73 69 74 69 6f 6e 73 20 69 6e 20 74 65  ransitions in te
d47d0 72 6d 73 20 6f 66 20 74 68 65 20 50 4f 53 49 58  rms of the POSIX
d47e0 20 61 64 76 69 73 6f 72 79 20 73 68 61 72 65 64   advisory shared
d47f0 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 20   and exclusive. 
d4800 20 2a 2a 20 6c 6f 63 6b 20 70 72 69 6d 69 74 69   ** lock primiti
d4810 76 65 73 20 28 63 61 6c 6c 65 64 20 72 65 61 64  ves (called read
d4820 2d 6c 6f 63 6b 73 20 61 6e 64 20 77 72 69 74 65  -locks and write
d4830 2d 6c 6f 63 6b 73 20 62 65 6c 6f 77 2c 20 74 6f  -locks below, to
d4840 20 61 76 6f 69 64 0a 20 20 2a 2a 20 63 6f 6e 66   avoid.  ** conf
d4850 75 73 69 6f 6e 20 77 69 74 68 20 53 51 4c 69 74  usion with SQLit
d4860 65 20 6c 6f 63 6b 20 6e 61 6d 65 73 29 2e 20 54  e lock names). T
d4870 68 65 20 61 6c 67 6f 72 69 74 68 6d 73 20 61 72  he algorithms ar
d4880 65 20 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 20  e complicated.  
d4890 2a 2a 20 73 6c 69 67 68 74 6c 79 20 69 6e 20 6f  ** slightly in o
d48a0 72 64 65 72 20 74 6f 20 62 65 20 63 6f 6d 70 61  rder to be compa
d48b0 74 69 62 6c 65 20 77 69 74 68 20 77 69 6e 64 6f  tible with windo
d48c0 77 73 20 73 79 73 74 65 6d 73 20 73 69 6d 75 6c  ws systems simul
d48d0 74 61 6e 65 6f 75 73 6c 79 0a 20 20 2a 2a 20 61  taneously.  ** a
d48e0 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61 6d  ccessing the sam
d48f0 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  e database file,
d4900 20 69 6e 20 63 61 73 65 20 74 68 61 74 20 69 73   in case that is
d4910 20 65 76 65 72 20 72 65 71 75 69 72 65 64 2e 0a   ever required..
d4920 20 20 2a 2a 0a 20 20 2a 2a 20 53 79 6d 62 6f 6c    **.  ** Symbol
d4930 73 20 64 65 66 69 6e 65 64 20 69 6e 20 6f 73 2e  s defined in os.
d4940 68 20 69 6e 64 65 6e 74 69 66 79 20 74 68 65 20  h indentify the 
d4950 27 70 65 6e 64 69 6e 67 20 62 79 74 65 27 20 61  'pending byte' a
d4960 6e 64 20 74 68 65 20 27 72 65 73 65 72 76 65 64  nd the 'reserved
d4970 0a 20 20 2a 2a 20 62 79 74 65 27 2c 20 65 61 63  .  ** byte', eac
d4980 68 20 73 69 6e 67 6c 65 20 62 79 74 65 73 20 61  h single bytes a
d4990 74 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  t well known off
d49a0 73 65 74 73 2c 20 61 6e 64 20 74 68 65 20 27 73  sets, and the 's
d49b0 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a 20  hared byte.  ** 
d49c0 72 61 6e 67 65 27 2c 20 61 20 72 61 6e 67 65 20  range', a range 
d49d0 6f 66 20 35 31 30 20 62 79 74 65 73 20 61 74 20  of 510 bytes at 
d49e0 61 20 77 65 6c 6c 20 6b 6e 6f 77 6e 20 6f 66 66  a well known off
d49f0 73 65 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  set..  **.  ** T
d4a00 6f 20 6f 62 74 61 69 6e 20 61 20 53 48 41 52 45  o obtain a SHARE
d4a10 44 20 6c 6f 63 6b 2c 20 61 20 72 65 61 64 2d 6c  D lock, a read-l
d4a20 6f 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ock is obtained 
d4a30 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 0a  on the 'pending.
d4a40 20 20 2a 2a 20 62 79 74 65 27 2e 20 20 49 66 20    ** byte'.  If 
d4a50 74 68 69 73 20 69 73 20 73 75 63 63 65 73 73 66  this is successf
d4a60 75 6c 2c 20 61 20 72 61 6e 64 6f 6d 20 62 79 74  ul, a random byt
d4a70 65 20 66 72 6f 6d 20 74 68 65 20 27 73 68 61 72  e from the 'shar
d4a80 65 64 20 62 79 74 65 0a 20 20 2a 2a 20 72 61 6e  ed byte.  ** ran
d4a90 67 65 27 20 69 73 20 72 65 61 64 2d 6c 6f 63 6b  ge' is read-lock
d4aa0 65 64 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  ed and the lock 
d4ab0 6f 6e 20 74 68 65 20 27 70 65 6e 64 69 6e 67 20  on the 'pending 
d4ac0 62 79 74 65 27 20 72 65 6c 65 61 73 65 64 2e 0a  byte' released..
d4ad0 20 20 2a 2a 0a 20 20 2a 2a 20 41 20 70 72 6f 63    **.  ** A proc
d4ae0 65 73 73 20 6d 61 79 20 6f 6e 6c 79 20 6f 62 74  ess may only obt
d4af0 61 69 6e 20 61 20 52 45 53 45 52 56 45 44 20 6c  ain a RESERVED l
d4b00 6f 63 6b 20 61 66 74 65 72 20 69 74 20 68 61 73  ock after it has
d4b10 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2e 0a   a SHARED lock..
d4b20 20 20 2a 2a 20 41 20 52 45 53 45 52 56 45 44 20    ** A RESERVED 
d4b30 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d 65 6e  lock is implemen
d4b40 74 65 64 20 62 79 20 67 72 61 62 62 69 6e 67 20  ted by grabbing 
d4b50 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
d4b60 74 68 65 0a 20 20 2a 2a 20 27 72 65 73 65 72 76  the.  ** 'reserv
d4b70 65 64 20 62 79 74 65 27 2e 20 0a 20 20 2a 2a 0a  ed byte'. .  **.
d4b80 20 20 2a 2a 20 41 20 70 72 6f 63 65 73 73 20 6d    ** A process m
d4b90 61 79 20 6f 6e 6c 79 20 6f 62 74 61 69 6e 20 61  ay only obtain a
d4ba0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 61 66   PENDING lock af
d4bb0 74 65 72 20 69 74 20 68 61 73 20 6f 62 74 61 69  ter it has obtai
d4bc0 6e 65 64 20 61 0a 20 20 2a 2a 20 53 48 41 52 45  ned a.  ** SHARE
d4bd0 44 20 6c 6f 63 6b 2e 20 41 20 50 45 4e 44 49 4e  D lock. A PENDIN
d4be0 47 20 6c 6f 63 6b 20 69 73 20 69 6d 70 6c 65 6d  G lock is implem
d4bf0 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e 69  ented by obtaini
d4c00 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 0a  ng a write-lock.
d4c10 20 20 2a 2a 20 6f 6e 20 74 68 65 20 27 70 65 6e    ** on the 'pen
d4c20 64 69 6e 67 20 62 79 74 65 27 2e 20 54 68 69 73  ding byte'. This
d4c30 20 65 6e 73 75 72 65 73 20 74 68 61 74 20 6e 6f   ensures that no
d4c40 20 6e 65 77 20 53 48 41 52 45 44 20 6c 6f 63 6b   new SHARED lock
d4c50 73 20 63 61 6e 20 62 65 0a 20 20 2a 2a 20 6f 62  s can be.  ** ob
d4c60 74 61 69 6e 65 64 2c 20 62 75 74 20 65 78 69 73  tained, but exis
d4c70 74 69 6e 67 20 53 48 41 52 45 44 20 6c 6f 63 6b  ting SHARED lock
d4c80 73 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f  s are allowed to
d4c90 20 70 65 72 73 69 73 74 2e 20 41 20 70 72 6f 63   persist. A proc
d4ca0 65 73 73 0a 20 20 2a 2a 20 64 6f 65 73 20 6e 6f  ess.  ** does no
d4cb0 74 20 68 61 76 65 20 74 6f 20 6f 62 74 61 69 6e  t have to obtain
d4cc0 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f 63 6b   a RESERVED lock
d4cd0 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f 20 61   on the way to a
d4ce0 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 0a 20   PENDING lock.. 
d4cf0 20 2a 2a 20 54 68 69 73 20 70 72 6f 70 65 72 74   ** This propert
d4d00 79 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65  y is used by the
d4d10 20 61 6c 67 6f 72 69 74 68 6d 20 66 6f 72 20 72   algorithm for r
d4d20 6f 6c 6c 69 6e 67 20 62 61 63 6b 20 61 20 6a 6f  olling back a jo
d4d30 75 72 6e 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20  urnal file.  ** 
d4d40 61 66 74 65 72 20 61 20 63 72 61 73 68 2e 0a 20  after a crash.. 
d4d50 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 45 58 43 4c   **.  ** An EXCL
d4d60 55 53 49 56 45 20 6c 6f 63 6b 2c 20 6f 62 74 61  USIVE lock, obta
d4d70 69 6e 65 64 20 61 66 74 65 72 20 61 20 50 45 4e  ined after a PEN
d4d80 44 49 4e 47 20 6c 6f 63 6b 20 69 73 20 68 65 6c  DING lock is hel
d4d90 64 2c 20 69 73 0a 20 20 2a 2a 20 69 6d 70 6c 65  d, is.  ** imple
d4da0 6d 65 6e 74 65 64 20 62 79 20 6f 62 74 61 69 6e  mented by obtain
d4db0 69 6e 67 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b  ing a write-lock
d4dc0 20 6f 6e 20 74 68 65 20 65 6e 74 69 72 65 20 27   on the entire '
d4dd0 73 68 61 72 65 64 20 62 79 74 65 0a 20 20 2a 2a  shared byte.  **
d4de0 20 72 61 6e 67 65 27 2e 20 53 69 6e 63 65 20 61   range'. Since a
d4df0 6c 6c 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20 72  ll other locks r
d4e00 65 71 75 69 72 65 20 61 20 72 65 61 64 2d 6c 6f  equire a read-lo
d4e10 63 6b 20 6f 6e 20 6f 6e 65 20 6f 66 20 74 68 65  ck on one of the
d4e20 20 62 79 74 65 73 0a 20 20 2a 2a 20 77 69 74 68   bytes.  ** with
d4e30 69 6e 20 74 68 69 73 20 72 61 6e 67 65 2c 20 74  in this range, t
d4e40 68 69 73 20 65 6e 73 75 72 65 73 20 74 68 61 74  his ensures that
d4e50 20 6e 6f 20 6f 74 68 65 72 20 6c 6f 63 6b 73 20   no other locks 
d4e60 61 72 65 20 68 65 6c 64 20 6f 6e 20 74 68 65 0a  are held on the.
d4e70 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2e 20 0a    ** database. .
d4e80 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 72 65    **.  ** The re
d4e90 61 73 6f 6e 20 61 20 73 69 6e 67 6c 65 20 62 79  ason a single by
d4ea0 74 65 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  te cannot be use
d4eb0 64 20 69 6e 73 74 65 61 64 20 6f 66 20 74 68 65  d instead of the
d4ec0 20 27 73 68 61 72 65 64 20 62 79 74 65 0a 20 20   'shared byte.  
d4ed0 2a 2a 20 72 61 6e 67 65 27 20 69 73 20 74 68 61  ** range' is tha
d4ee0 74 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  t some versions 
d4ef0 6f 66 20 77 69 6e 64 6f 77 73 20 64 6f 20 6e 6f  of windows do no
d4f00 74 20 73 75 70 70 6f 72 74 20 72 65 61 64 2d 6c  t support read-l
d4f10 6f 63 6b 73 2e 20 42 79 0a 20 20 2a 2a 20 6c 6f  ocks. By.  ** lo
d4f20 63 6b 69 6e 67 20 61 20 72 61 6e 64 6f 6d 20 62  cking a random b
d4f30 79 74 65 20 66 72 6f 6d 20 61 20 72 61 6e 67 65  yte from a range
d4f40 2c 20 63 6f 6e 63 75 72 72 65 6e 74 20 53 48 41  , concurrent SHA
d4f50 52 45 44 20 6c 6f 63 6b 73 20 6d 61 79 20 65 78  RED locks may ex
d4f60 69 73 74 0a 20 20 2a 2a 20 65 76 65 6e 20 69 66  ist.  ** even if
d4f70 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 70 72 69   the locking pri
d4f80 6d 69 74 69 76 65 20 75 73 65 64 20 69 73 20 61  mitive used is a
d4f90 6c 77 61 79 73 20 61 20 77 72 69 74 65 2d 6c 6f  lways a write-lo
d4fa0 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 72  ck..  */.  int r
d4fb0 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
d4fc0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d4fd0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d4fe0 3b 0a 20 20 73 74 72 75 63 74 20 6c 6f 63 6b 49  ;.  struct lockI
d4ff0 6e 66 6f 20 2a 70 4c 6f 63 6b 20 3d 20 70 46 69  nfo *pLock = pFi
d5000 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 73 74 72  le->pLock;.  str
d5010 75 63 74 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a  uct flock lock;.
d5020 20 20 69 6e 74 20 73 3b 0a 0a 20 20 61 73 73 65    int s;..  asse
d5030 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f  rt( pFile );.  O
d5040 53 54 52 41 43 45 37 28 22 4c 4f 43 4b 20 20 20  STRACE7("LOCK   
d5050 20 25 64 20 25 73 20 77 61 73 20 25 73 28 25 73   %d %s was %s(%s
d5060 2c 25 64 29 20 70 69 64 3d 25 64 5c 6e 22 2c 20  ,%d) pid=%d\n", 
d5070 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20  pFile->h,.      
d5080 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
d5090 6b 74 79 70 65 29 2c 20 6c 6f 63 6b 74 79 70 65  ktype), locktype
d50a0 4e 61 6d 65 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b  Name(pFile->lock
d50b0 74 79 70 65 29 2c 0a 20 20 20 20 20 20 6c 6f 63  type),.      loc
d50c0 6b 74 79 70 65 4e 61 6d 65 28 70 4c 6f 63 6b 2d  ktypeName(pLock-
d50d0 3e 6c 6f 63 6b 74 79 70 65 29 2c 20 70 4c 6f 63  >locktype), pLoc
d50e0 6b 2d 3e 63 6e 74 20 2c 20 67 65 74 70 69 64 28  k->cnt , getpid(
d50f0 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ));..  /* If the
d5100 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
d5110 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
d5120 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
d5130 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
d5140 2a 20 75 6e 69 78 46 69 6c 65 2c 20 64 6f 20 6e  * unixFile, do n
d5150 6f 74 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73  othing. Don't us
d5160 65 20 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20  e the end_lock: 
d5170 65 78 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20  exit path, as.  
d5180 2a 2a 20 65 6e 74 65 72 4d 75 74 65 78 28 29 20  ** enterMutex() 
d5190 68 61 73 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c  hasn't been call
d51a0 65 64 20 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69  ed yet..  */.  i
d51b0 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  f( pFile->lockty
d51c0 70 65 3e 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a  pe>=locktype ){.
d51d0 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4c 4f      OSTRACE3("LO
d51e0 43 4b 20 20 20 20 25 64 20 25 73 20 6f 6b 20 28  CK    %d %s ok (
d51f0 61 6c 72 65 61 64 79 20 68 65 6c 64 29 5c 6e 22  already held)\n"
d5200 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20  , pFile->h,.    
d5210 20 20 20 20 20 20 20 20 6c 6f 63 6b 74 79 70 65          locktype
d5220 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 29 3b  Name(locktype));
d5230 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
d5240 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
d5250 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 6c   Make sure the l
d5260 6f 63 6b 69 6e 67 20 73 65 71 75 65 6e 63 65 20  ocking sequence 
d5270 69 73 20 63 6f 72 72 65 63 74 0a 20 20 2a 2f 0a  is correct.  */.
d5280 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d    assert( pFile-
d5290 3e 6c 6f 63 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f  >locktype!=NO_LO
d52a0 43 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d  CK || locktype==
d52b0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20  SHARED_LOCK );. 
d52c0 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d52d0 65 21 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20  e!=PENDING_LOCK 
d52e0 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c 6f 63  );.  assert( loc
d52f0 6b 74 79 70 65 21 3d 52 45 53 45 52 56 45 44 5f  ktype!=RESERVED_
d5300 4c 4f 43 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c  LOCK || pFile->l
d5310 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d5320 4c 4f 43 4b 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  LOCK );..  /* Th
d5330 69 73 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64  is mutex is need
d5340 65 64 20 62 65 63 61 75 73 65 20 70 46 69 6c 65  ed because pFile
d5350 2d 3e 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65  ->pLock is share
d5360 64 20 61 63 72 6f 73 73 20 74 68 72 65 61 64 73  d across threads
d5370 0a 20 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74  .  */.  enterMut
d5380 65 78 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65  ex();..  /* Make
d5390 20 73 75 72 65 20 74 68 65 20 63 75 72 72 65 6e   sure the curren
d53a0 74 20 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68  t thread owns th
d53b0 65 20 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20  e pFile..  */.  
d53c0 72 63 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e  rc = transferOwn
d53d0 65 72 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20  ership(pFile);. 
d53e0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
d53f0 4f 4b 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d  OK ){.    leaveM
d5400 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75  utex();.    retu
d5410 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 4c 6f  rn rc;.  }.  pLo
d5420 63 6b 20 3d 20 70 46 69 6c 65 2d 3e 70 4c 6f 63  ck = pFile->pLoc
d5430 6b 3b 0a 0a 20 20 2f 2a 20 49 66 20 73 6f 6d 65  k;..  /* If some
d5440 20 74 68 72 65 61 64 20 75 73 69 6e 67 20 74 68   thread using th
d5450 69 73 20 50 49 44 20 68 61 73 20 61 20 6c 6f 63  is PID has a loc
d5460 6b 20 76 69 61 20 61 20 64 69 66 66 65 72 65 6e  k via a differen
d5470 74 20 75 6e 69 78 46 69 6c 65 2a 0a 20 20 2a 2a  t unixFile*.  **
d5480 20 68 61 6e 64 6c 65 20 74 68 61 74 20 70 72 65   handle that pre
d5490 63 6c 75 64 65 73 20 74 68 65 20 72 65 71 75 65  cludes the reque
d54a0 73 74 65 64 20 6c 6f 63 6b 2c 20 72 65 74 75 72  sted lock, retur
d54b0 6e 20 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69  n BUSY..  */.  i
d54c0 66 28 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  f( (pFile->lockt
d54d0 79 70 65 21 3d 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  ype!=pLock->lock
d54e0 74 79 70 65 20 26 26 20 0a 20 20 20 20 20 20 20  type && .       
d54f0 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74     (pLock->lockt
d5500 79 70 65 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43  ype>=PENDING_LOC
d5510 4b 20 7c 7c 20 6c 6f 63 6b 74 79 70 65 3e 53 48  K || locktype>SH
d5520 41 52 45 44 5f 4c 4f 43 4b 29 29 0a 20 20 29 7b  ARED_LOCK)).  ){
d5530 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
d5540 5f 42 55 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20  _BUSY;.    goto 
d5550 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20  end_lock;.  }.. 
d5560 20 2f 2a 20 49 66 20 61 20 53 48 41 52 45 44 20   /* If a SHARED 
d5570 6c 6f 63 6b 20 69 73 20 72 65 71 75 65 73 74 65  lock is requeste
d5580 64 2c 20 61 6e 64 20 73 6f 6d 65 20 74 68 72 65  d, and some thre
d5590 61 64 20 75 73 69 6e 67 20 74 68 69 73 20 50 49  ad using this PI
d55a0 44 20 61 6c 72 65 61 64 79 0a 20 20 2a 2a 20 68  D already.  ** h
d55b0 61 73 20 61 20 53 48 41 52 45 44 20 6f 72 20 52  as a SHARED or R
d55c0 45 53 45 52 56 45 44 20 6c 6f 63 6b 2c 20 74 68  ESERVED lock, th
d55d0 65 6e 20 69 6e 63 72 65 6d 65 6e 74 20 72 65 66  en increment ref
d55e0 65 72 65 6e 63 65 20 63 6f 75 6e 74 73 20 61 6e  erence counts an
d55f0 64 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20 53 51  d.  ** return SQ
d5600 4c 49 54 45 5f 4f 4b 2e 0a 20 20 2a 2f 0a 20 20  LITE_OK..  */.  
d5610 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
d5620 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 0a 20 20  ARED_LOCK && .  
d5630 20 20 20 20 28 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b      (pLock->lock
d5640 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d5650 4b 20 7c 7c 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b  K || pLock->lock
d5660 74 79 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c  type==RESERVED_L
d5670 4f 43 4b 29 20 29 7b 0a 20 20 20 20 61 73 73 65  OCK) ){.    asse
d5680 72 74 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  rt( locktype==SH
d5690 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 20  ARED_LOCK );.   
d56a0 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e   assert( pFile->
d56b0 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 20  locktype==0 );. 
d56c0 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b     assert( pLock
d56d0 2d 3e 63 6e 74 3e 30 20 29 3b 0a 20 20 20 20 70  ->cnt>0 );.    p
d56e0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
d56f0 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a 20 20   SHARED_LOCK;.  
d5700 20 20 70 4c 6f 63 6b 2d 3e 63 6e 74 2b 2b 3b 0a    pLock->cnt++;.
d5710 20 20 20 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e      pFile->pOpen
d5720 2d 3e 6e 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 67  ->nLock++;.    g
d5730 6f 74 6f 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  oto end_lock;.  
d5740 7d 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  }..  lock.l_len 
d5750 3d 20 31 4c 3b 0a 0a 20 20 6c 6f 63 6b 2e 6c 5f  = 1L;..  lock.l_
d5760 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45  whence = SEEK_SE
d5770 54 3b 0a 0a 20 20 2f 2a 20 41 20 50 45 4e 44 49  T;..  /* A PENDI
d5780 4e 47 20 6c 6f 63 6b 20 69 73 20 6e 65 65 64 65  NG lock is neede
d5790 64 20 62 65 66 6f 72 65 20 61 63 71 75 69 72 69  d before acquiri
d57a0 6e 67 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b  ng a SHARED lock
d57b0 20 61 6e 64 20 62 65 66 6f 72 65 0a 20 20 2a 2a   and before.  **
d57c0 20 61 63 71 75 69 72 69 6e 67 20 61 6e 20 45 58   acquiring an EX
d57d0 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 46  CLUSIVE lock.  F
d57e0 6f 72 20 74 68 65 20 53 48 41 52 45 44 20 6c 6f  or the SHARED lo
d57f0 63 6b 2c 20 74 68 65 20 50 45 4e 44 49 4e 47 20  ck, the PENDING 
d5800 77 69 6c 6c 0a 20 20 2a 2a 20 62 65 20 72 65 6c  will.  ** be rel
d5810 65 61 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  eased..  */.  if
d5820 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  ( locktype==SHAR
d5830 45 44 5f 4c 4f 43 4b 20 0a 20 20 20 20 20 20 7c  ED_LOCK .      |
d5840 7c 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43  | (locktype==EXC
d5850 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 26 26 20 70  LUSIVE_LOCK && p
d5860 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c 50  File->locktype<P
d5870 45 4e 44 49 4e 47 5f 4c 4f 43 4b 29 0a 20 20 29  ENDING_LOCK).  )
d5880 7b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  {.    lock.l_typ
d5890 65 20 3d 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53  e = (locktype==S
d58a0 48 41 52 45 44 5f 4c 4f 43 4b 3f 46 5f 52 44 4c  HARED_LOCK?F_RDL
d58b0 43 4b 3a 46 5f 57 52 4c 43 4b 29 3b 0a 20 20 20  CK:F_WRLCK);.   
d58c0 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20   lock.l_start = 
d58d0 50 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20  PENDING_BYTE;.  
d58e0 20 20 73 20 3d 20 66 63 6e 74 6c 28 70 46 69 6c    s = fcntl(pFil
d58f0 65 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26  e->h, F_SETLK, &
d5900 6c 6f 63 6b 29 3b 0a 20 20 20 20 69 66 28 20 73  lock);.    if( s
d5910 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  ==(-1) ){.      
d5920 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
d5930 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  no;.      rc = s
d5940 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d5950 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d5960 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 4c 4f   SQLITE_IOERR_LO
d5970 43 4b 29 3b 0a 20 20 20 20 20 20 69 66 28 20 49  CK);.      if( I
d5980 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63 29  S_LOCK_ERROR(rc)
d5990 20 29 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c   ){.        pFil
d59a0 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74  e->lastErrno = t
d59b0 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20  Errno;.      }. 
d59c0 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6c 6f       goto end_lo
d59d0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 0a  ck;.    }.  }...
d59e0 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c 20    /* If control 
d59f0 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f 69  gets to this poi
d5a00 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c 6c  nt, then actuall
d5a10 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 6d  y go ahead and m
d5a20 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74 69  ake.  ** operati
d5a30 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73 20  ng system calls 
d5a40 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69 65  for the specifie
d5a50 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  d lock..  */.  i
d5a60 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f( locktype==SHA
d5a70 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  RED_LOCK ){.    
d5a80 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 30 3b 0a  int tErrno = 0;.
d5a90 20 20 20 20 61 73 73 65 72 74 28 20 70 4c 6f 63      assert( pLoc
d5aa0 6b 2d 3e 63 6e 74 3d 3d 30 20 29 3b 0a 20 20 20  k->cnt==0 );.   
d5ab0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d5ac0 6c 6f 63 6b 74 79 70 65 3d 3d 30 20 29 3b 0a 0a  locktype==0 );..
d5ad0 20 20 20 20 2f 2a 20 4e 6f 77 20 67 65 74 20 74      /* Now get t
d5ae0 68 65 20 72 65 61 64 2d 6c 6f 63 6b 20 2a 2f 0a  he read-lock */.
d5af0 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
d5b00 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
d5b10 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20  .    lock.l_len 
d5b20 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 3b 0a 20  = SHARED_SIZE;. 
d5b30 20 20 20 69 66 28 20 28 73 20 3d 20 66 63 6e 74     if( (s = fcnt
d5b40 6c 28 70 46 69 6c 65 2d 3e 68 2c 20 46 5f 53 45  l(pFile->h, F_SE
d5b50 54 4c 4b 2c 20 26 6c 6f 63 6b 29 29 3d 3d 28 2d  TLK, &lock))==(-
d5b60 31 29 20 29 7b 0a 20 20 20 20 20 20 74 45 72 72  1) ){.      tErr
d5b70 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
d5b80 7d 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 74 68  }.    /* Drop th
d5b90 65 20 74 65 6d 70 6f 72 61 72 79 20 50 45 4e 44  e temporary PEND
d5ba0 49 4e 47 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  ING lock */.    
d5bb0 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74 20 3d 20 50  lock.l_start = P
d5bc0 45 4e 44 49 4e 47 5f 42 59 54 45 3b 0a 20 20 20  ENDING_BYTE;.   
d5bd0 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 31 4c   lock.l_len = 1L
d5be0 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70  ;.    lock.l_typ
d5bf0 65 20 3d 20 46 5f 55 4e 4c 43 4b 3b 0a 20 20 20  e = F_UNLCK;.   
d5c00 20 69 66 28 20 66 63 6e 74 6c 28 70 46 69 6c 65   if( fcntl(pFile
d5c10 2d 3e 68 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c  ->h, F_SETLK, &l
d5c20 6f 63 6b 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ock)!=0 ){.     
d5c30 20 69 66 28 20 73 20 21 3d 20 2d 31 20 29 7b 0a   if( s != -1 ){.
d5c40 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
d5c50 63 6f 75 6c 64 20 68 61 70 70 65 6e 20 77 69 74  could happen wit
d5c60 68 20 61 20 6e 65 74 77 6f 72 6b 20 6d 6f 75 6e  h a network moun
d5c70 74 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 45 72  t */.        tEr
d5c80 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 20 0a 20 20  rno = errno; .  
d5c90 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d5ca0 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d5cb0 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d5cc0 49 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b  ITE_IOERR_UNLOCK
d5cd0 29 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20  ); .        if( 
d5ce0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72 63  IS_LOCK_ERROR(rc
d5cf0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
d5d00 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d5d10 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d5d20 20 20 7d 0a 20 20 20 20 20 20 20 20 67 6f 74 6f    }.        goto
d5d30 20 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20 20   end_lock;.     
d5d40 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
d5d50 20 73 3d 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20   s==(-1) ){.    
d5d60 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d5d70 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d5d80 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
d5d90 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20  IOERR_LOCK);.   
d5da0 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
d5db0 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
d5dc0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d5dd0 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20  rrno = tErrno;. 
d5de0 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
d5df0 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c  {.      pFile->l
d5e00 6f 63 6b 74 79 70 65 20 3d 20 53 48 41 52 45 44  ocktype = SHARED
d5e10 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 70 46 69  _LOCK;.      pFi
d5e20 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b  le->pOpen->nLock
d5e30 2b 2b 3b 0a 20 20 20 20 20 20 70 4c 6f 63 6b 2d  ++;.      pLock-
d5e40 3e 63 6e 74 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  >cnt = 1;.    }.
d5e50 20 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b    }else if( lock
d5e60 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f  type==EXCLUSIVE_
d5e70 4c 4f 43 4b 20 26 26 20 70 4c 6f 63 6b 2d 3e 63  LOCK && pLock->c
d5e80 6e 74 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 57  nt>1 ){.    /* W
d5e90 65 20 61 72 65 20 74 72 79 69 6e 67 20 66 6f 72  e are trying for
d5ea0 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f   an exclusive lo
d5eb0 63 6b 20 62 75 74 20 61 6e 6f 74 68 65 72 20 74  ck but another t
d5ec0 68 72 65 61 64 20 69 6e 20 74 68 69 73 0a 20 20  hread in this.  
d5ed0 20 20 2a 2a 20 73 61 6d 65 20 70 72 6f 63 65 73    ** same proces
d5ee0 73 20 69 73 20 73 74 69 6c 6c 20 68 6f 6c 64 69  s is still holdi
d5ef0 6e 67 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  ng a shared lock
d5f00 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 53 51  . */.    rc = SQ
d5f10 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c  LITE_BUSY;.  }el
d5f20 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72  se{.    /* The r
d5f30 65 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61  equest was for a
d5f40 20 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43   RESERVED or EXC
d5f50 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74  LUSIVE lock.  It
d5f60 20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d   is.    ** assum
d5f70 65 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73  ed that there is
d5f80 20 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65   a SHARED or gre
d5f90 61 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  ater lock on the
d5fa0 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72   file.    ** alr
d5fb0 65 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eady..    */.   
d5fc0 20 61 73 73 65 72 74 28 20 30 21 3d 70 46 69 6c   assert( 0!=pFil
d5fd0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20  e->locktype );. 
d5fe0 20 20 20 6c 6f 63 6b 2e 6c 5f 74 79 70 65 20 3d     lock.l_type =
d5ff0 20 46 5f 57 52 4c 43 4b 3b 0a 20 20 20 20 73 77   F_WRLCK;.    sw
d6000 69 74 63 68 28 20 6c 6f 63 6b 74 79 70 65 20 29  itch( locktype )
d6010 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 45 53  {.      case RES
d6020 45 52 56 45 44 5f 4c 4f 43 4b 3a 0a 20 20 20 20  ERVED_LOCK:.    
d6030 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
d6040 20 3d 20 52 45 53 45 52 56 45 44 5f 42 59 54 45   = RESERVED_BYTE
d6050 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
d6060 0a 20 20 20 20 20 20 63 61 73 65 20 45 58 43 4c  .      case EXCL
d6070 55 53 49 56 45 5f 4c 4f 43 4b 3a 0a 20 20 20 20  USIVE_LOCK:.    
d6080 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72 74      lock.l_start
d6090 20 3d 20 53 48 41 52 45 44 5f 46 49 52 53 54 3b   = SHARED_FIRST;
d60a0 0a 20 20 20 20 20 20 20 20 6c 6f 63 6b 2e 6c 5f  .        lock.l_
d60b0 6c 65 6e 20 3d 20 53 48 41 52 45 44 5f 53 49 5a  len = SHARED_SIZ
d60c0 45 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  E;.        break
d60d0 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a  ;.      default:
d60e0 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
d60f0 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 20  0);.    }.    s 
d6100 3d 20 66 63 6e 74 6c 28 70 46 69 6c 65 2d 3e 68  = fcntl(pFile->h
d6110 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
d6120 29 3b 0a 20 20 20 20 69 66 28 20 73 3d 3d 28 2d  );.    if( s==(-
d6130 31 29 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  1) ){.      int 
d6140 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d6150 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
d6160 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45  eErrorFromPosixE
d6170 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c  rror(tErrno, SQL
d6180 49 54 45 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b  ITE_IOERR_LOCK);
d6190 0a 20 20 20 20 20 20 69 66 28 20 49 53 5f 4c 4f  .      if( IS_LO
d61a0 43 4b 5f 45 52 52 4f 52 28 72 63 29 20 29 7b 0a  CK_ERROR(rc) ){.
d61b0 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c          pFile->l
d61c0 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e  astErrno = tErrn
d61d0 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  o;.      }.    }
d61e0 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 72 63  .  }.  .  if( rc
d61f0 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
d6200 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79     pFile->lockty
d6210 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
d6220 20 20 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79     pLock->lockty
d6230 70 65 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20  pe = locktype;. 
d6240 20 7d 65 6c 73 65 20 69 66 28 20 6c 6f 63 6b 74   }else if( lockt
d6250 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c  ype==EXCLUSIVE_L
d6260 4f 43 4b 20 29 7b 0a 20 20 20 20 70 46 69 6c 65  OCK ){.    pFile
d6270 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 50 45 4e  ->locktype = PEN
d6280 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20 20 20 20 70  DING_LOCK;.    p
d6290 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  Lock->locktype =
d62a0 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a 20   PENDING_LOCK;. 
d62b0 20 7d 0a 0a 65 6e 64 5f 6c 6f 63 6b 3a 0a 20 20   }..end_lock:.  
d62c0 6c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  leaveMutex();.  
d62d0 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20  OSTRACE4("LOCK  
d62e0 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70    %d %s %s\n", p
d62f0 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
d6300 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
d6310 20 0a 20 20 20 20 20 20 72 63 3d 3d 53 51 4c 49   .      rc==SQLI
d6320 54 45 5f 4f 4b 20 3f 20 22 6f 6b 22 20 3a 20 22  TE_OK ? "ok" : "
d6330 66 61 69 6c 65 64 22 29 3b 0a 20 20 72 65 74 75  failed");.  retu
d6340 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
d6350 4c 6f 77 65 72 20 74 68 65 20 6c 6f 63 6b 69 6e  Lower the lockin
d6360 67 20 6c 65 76 65 6c 20 6f 6e 20 66 69 6c 65 20  g level on file 
d6370 64 65 73 63 72 69 70 74 6f 72 20 70 46 69 6c 65  descriptor pFile
d6380 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20 6c   to locktype.  l
d6390 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74 20  ocktype.** must 
d63a0 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f 43  be either NO_LOC
d63b0 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43 4b  K or SHARED_LOCK
d63c0 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6c  ..**.** If the l
d63d0 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66 20  ocking level of 
d63e0 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69 70  the file descrip
d63f0 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20 61  tor is already a
d6400 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74 68  t or below.** th
d6410 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63 6b  e requested lock
d6420 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73 20  ing level, this 
d6430 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
d6440 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  op..*/.static in
d6450 74 20 75 6e 69 78 55 6e 6c 6f 63 6b 28 73 71 6c  t unixUnlock(sql
d6460 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69  ite3_file *id, i
d6470 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20  nt locktype){.  
d6480 73 74 72 75 63 74 20 6c 6f 63 6b 49 6e 66 6f 20  struct lockInfo 
d6490 2a 70 4c 6f 63 6b 3b 0a 20 20 73 74 72 75 63 74  *pLock;.  struct
d64a0 20 66 6c 6f 63 6b 20 6c 6f 63 6b 3b 0a 20 20 69   flock lock;.  i
d64b0 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
d64c0 4b 3b 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70  K;.  unixFile *p
d64d0 46 69 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65  File = (unixFile
d64e0 2a 29 69 64 3b 0a 20 20 69 6e 74 20 68 3b 0a 0a  *)id;.  int h;..
d64f0 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 20    assert( pFile 
d6500 29 3b 0a 20 20 4f 53 54 52 41 43 45 37 28 22 55  );.  OSTRACE7("U
d6510 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73  NLOCK  %d %d was
d6520 20 25 64 28 25 64 2c 25 64 29 20 70 69 64 3d 25   %d(%d,%d) pid=%
d6530 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
d6540 6c 6f 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20  locktype,.      
d6550 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c  pFile->locktype,
d6560 20 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 2d 3e 6c   pFile->pLock->l
d6570 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65 2d 3e  ocktype, pFile->
d6580 70 4c 6f 63 6b 2d 3e 63 6e 74 2c 20 67 65 74 70  pLock->cnt, getp
d6590 69 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74  id());..  assert
d65a0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
d65b0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28  ED_LOCK );.  if(
d65c0 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
d65d0 3c 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20  <=locktype ){.  
d65e0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
d65f0 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48  OK;.  }.  if( CH
d6600 45 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69  ECK_THREADID(pFi
d6610 6c 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  le) ){.    retur
d6620 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
d6630 0a 20 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65  .  }.  enterMute
d6640 78 28 29 3b 0a 20 20 68 20 3d 20 70 46 69 6c 65  x();.  h = pFile
d6650 2d 3e 68 3b 0a 20 20 70 4c 6f 63 6b 20 3d 20 70  ->h;.  pLock = p
d6660 46 69 6c 65 2d 3e 70 4c 6f 63 6b 3b 0a 20 20 61  File->pLock;.  a
d6670 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e 63 6e  ssert( pLock->cn
d6680 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 46  t!=0 );.  if( pF
d6690 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48  ile->locktype>SH
d66a0 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d66b0 20 61 73 73 65 72 74 28 20 70 4c 6f 63 6b 2d 3e   assert( pLock->
d66c0 6c 6f 63 6b 74 79 70 65 3d 3d 70 46 69 6c 65 2d  locktype==pFile-
d66d0 3e 6c 6f 63 6b 74 79 70 65 20 29 3b 0a 20 20 20  >locktype );.   
d66e0 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72   SimulateIOError
d66f0 42 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 53  Benign(1);.    S
d6700 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20  imulateIOError( 
d6710 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 53 69 6d  h=(-1) ).    Sim
d6720 75 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69  ulateIOErrorBeni
d6730 67 6e 28 30 29 3b 0a 20 20 20 20 69 66 28 20 6c  gn(0);.    if( l
d6740 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
d6750 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20 6c 6f  LOCK ){.      lo
d6760 63 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 52 44  ck.l_type = F_RD
d6770 4c 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  LCK;.      lock.
d6780 6c 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f  l_whence = SEEK_
d6790 53 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e  SET;.      lock.
d67a0 6c 5f 73 74 61 72 74 20 3d 20 53 48 41 52 45 44  l_start = SHARED
d67b0 5f 46 49 52 53 54 3b 0a 20 20 20 20 20 20 6c 6f  _FIRST;.      lo
d67c0 63 6b 2e 6c 5f 6c 65 6e 20 3d 20 53 48 41 52 45  ck.l_len = SHARE
d67d0 44 5f 53 49 5a 45 3b 0a 20 20 20 20 20 20 69 66  D_SIZE;.      if
d67e0 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53 45 54  ( fcntl(h, F_SET
d67f0 4c 4b 2c 20 26 6c 6f 63 6b 29 3d 3d 28 2d 31 29  LK, &lock)==(-1)
d6800 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
d6810 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a  tErrno = errno;.
d6820 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
d6830 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69  iteErrorFromPosi
d6840 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53  xError(tErrno, S
d6850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 44 4c 4f  QLITE_IOERR_RDLO
d6860 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  CK);.        if(
d6870 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 72   IS_LOCK_ERROR(r
d6880 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
d6890 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d68a0 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20   = tErrno;.     
d68b0 20 20 20 7d 0a 09 09 09 09 67 6f 74 6f 20 65 6e     }.....goto en
d68c0 64 5f 75 6e 6c 6f 63 6b 3b 0a 20 20 20 20 20 20  d_unlock;.      
d68d0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 6c 6f 63 6b  }.    }.    lock
d68e0 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c 43  .l_type = F_UNLC
d68f0 4b 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 77 68  K;.    lock.l_wh
d6900 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53 45 54 3b  ence = SEEK_SET;
d6910 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 73 74 61 72  .    lock.l_star
d6920 74 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  t = PENDING_BYTE
d6930 3b 0a 20 20 20 20 6c 6f 63 6b 2e 6c 5f 6c 65 6e  ;.    lock.l_len
d6940 20 3d 20 32 4c 3b 20 20 61 73 73 65 72 74 28 20   = 2L;  assert( 
d6950 50 45 4e 44 49 4e 47 5f 42 59 54 45 2b 31 3d 3d  PENDING_BYTE+1==
d6960 52 45 53 45 52 56 45 44 5f 42 59 54 45 20 29 3b  RESERVED_BYTE );
d6970 0a 20 20 20 20 69 66 28 20 66 63 6e 74 6c 28 68  .    if( fcntl(h
d6980 2c 20 46 5f 53 45 54 4c 4b 2c 20 26 6c 6f 63 6b  , F_SETLK, &lock
d6990 29 21 3d 28 2d 31 29 20 29 7b 0a 20 20 20 20 20  )!=(-1) ){.     
d69a0 20 70 4c 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65   pLock->locktype
d69b0 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b 3b 0a   = SHARED_LOCK;.
d69c0 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
d69d0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
d69e0 72 6e 6f 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rno;.      rc = 
d69f0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
d6a00 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
d6a10 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
d6a20 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 69 66  NLOCK);.      if
d6a30 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28  ( IS_LOCK_ERROR(
d6a40 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  rc) ){.        p
d6a50 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
d6a60 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
d6a70 7d 0a 09 09 09 67 6f 74 6f 20 65 6e 64 5f 75 6e  }....goto end_un
d6a80 6c 6f 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lock;.    }.  }.
d6a90 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
d6aa0 4e 4f 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 73  NO_LOCK ){.    s
d6ab0 74 72 75 63 74 20 6f 70 65 6e 43 6e 74 20 2a 70  truct openCnt *p
d6ac0 4f 70 65 6e 3b 0a 0a 20 20 20 20 2f 2a 20 44 65  Open;..    /* De
d6ad0 63 72 65 6d 65 6e 74 20 74 68 65 20 73 68 61 72  crement the shar
d6ae0 65 64 20 6c 6f 63 6b 20 63 6f 75 6e 74 65 72 2e  ed lock counter.
d6af0 20 20 52 65 6c 65 61 73 65 20 74 68 65 20 6c 6f    Release the lo
d6b00 63 6b 20 75 73 69 6e 67 20 61 6e 0a 20 20 20 20  ck using an.    
d6b10 2a 2a 20 4f 53 20 63 61 6c 6c 20 6f 6e 6c 79 20  ** OS call only 
d6b20 77 68 65 6e 20 61 6c 6c 20 74 68 72 65 61 64 73  when all threads
d6b30 20 69 6e 20 74 68 69 73 20 73 61 6d 65 20 70 72   in this same pr
d6b40 6f 63 65 73 73 20 68 61 76 65 20 72 65 6c 65 61  ocess have relea
d6b50 73 65 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6c  sed.    ** the l
d6b60 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ock..    */.    
d6b70 70 4c 6f 63 6b 2d 3e 63 6e 74 2d 2d 3b 0a 20 20  pLock->cnt--;.  
d6b80 20 20 69 66 28 20 70 4c 6f 63 6b 2d 3e 63 6e 74    if( pLock->cnt
d6b90 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6c 6f 63  ==0 ){.      loc
d6ba0 6b 2e 6c 5f 74 79 70 65 20 3d 20 46 5f 55 4e 4c  k.l_type = F_UNL
d6bb0 43 4b 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  CK;.      lock.l
d6bc0 5f 77 68 65 6e 63 65 20 3d 20 53 45 45 4b 5f 53  _whence = SEEK_S
d6bd0 45 54 3b 0a 20 20 20 20 20 20 6c 6f 63 6b 2e 6c  ET;.      lock.l
d6be0 5f 73 74 61 72 74 20 3d 20 6c 6f 63 6b 2e 6c 5f  _start = lock.l_
d6bf0 6c 65 6e 20 3d 20 30 4c 3b 0a 20 20 20 20 20 20  len = 0L;.      
d6c00 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
d6c10 65 6e 69 67 6e 28 31 29 3b 0a 20 20 20 20 20 20  enign(1);.      
d6c20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
d6c30 20 68 3d 28 2d 31 29 20 29 0a 20 20 20 20 20 20   h=(-1) ).      
d6c40 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 42  SimulateIOErrorB
d6c50 65 6e 69 67 6e 28 30 29 3b 0a 20 20 20 20 20 20  enign(0);.      
d6c60 69 66 28 20 66 63 6e 74 6c 28 68 2c 20 46 5f 53  if( fcntl(h, F_S
d6c70 45 54 4c 4b 2c 20 26 6c 6f 63 6b 29 21 3d 28 2d  ETLK, &lock)!=(-
d6c80 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c  1) ){.        pL
d6c90 6f 63 6b 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ock->locktype = 
d6ca0 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 20 20 7d  NO_LOCK;.      }
d6cb0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e  else{.        in
d6cc0 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
d6cd0 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  ;.        rc = s
d6ce0 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
d6cf0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
d6d00 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55 4e   SQLITE_IOERR_UN
d6d10 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20 20 69  LOCK);.        i
d6d20 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52  f( IS_LOCK_ERROR
d6d30 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  (rc) ){.        
d6d40 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
d6d50 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
d6d60 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
d6d70 4c 6f 63 6b 2d 3e 63 6e 74 20 3d 20 31 3b 0a 09  Lock->cnt = 1;..
d6d80 09 09 09 67 6f 74 6f 20 65 6e 64 5f 75 6e 6c 6f  ...goto end_unlo
d6d90 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ck;.      }.    
d6da0 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 72 65 6d  }..    /* Decrem
d6db0 65 6e 74 20 74 68 65 20 63 6f 75 6e 74 20 6f 66  ent the count of
d6dc0 20 6c 6f 63 6b 73 20 61 67 61 69 6e 73 74 20 74   locks against t
d6dd0 68 69 73 20 73 61 6d 65 20 66 69 6c 65 2e 20 20  his same file.  
d6de0 57 68 65 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20  When the.    ** 
d6df0 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65  count reaches ze
d6e00 72 6f 2c 20 63 6c 6f 73 65 20 61 6e 79 20 6f 74  ro, close any ot
d6e10 68 65 72 20 66 69 6c 65 20 64 65 73 63 72 69 70  her file descrip
d6e20 74 6f 72 73 20 77 68 6f 73 65 20 63 6c 6f 73 65  tors whose close
d6e30 0a 20 20 20 20 2a 2a 20 77 61 73 20 64 65 66 65  .    ** was defe
d6e40 72 72 65 64 20 62 65 63 61 75 73 65 20 6f 66 20  rred because of 
d6e50 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b  outstanding lock
d6e60 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  s..    */.    if
d6e70 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
d6e80 29 7b 0a 20 20 20 20 20 20 70 4f 70 65 6e 20 3d  ){.      pOpen =
d6e90 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 3b 0a 20   pFile->pOpen;. 
d6ea0 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63       pOpen->nLoc
d6eb0 6b 2d 2d 3b 0a 20 20 20 20 20 20 61 73 73 65 72  k--;.      asser
d6ec0 74 28 20 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3e  t( pOpen->nLock>
d6ed0 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  =0 );.      if( 
d6ee0 70 4f 70 65 6e 2d 3e 6e 4c 6f 63 6b 3d 3d 30 20  pOpen->nLock==0 
d6ef0 26 26 20 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69  && pOpen->nPendi
d6f00 6e 67 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ng>0 ){.        
d6f10 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66  int i;.        f
d6f20 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 65 6e 2d  or(i=0; i<pOpen-
d6f30 3e 6e 50 65 6e 64 69 6e 67 3b 20 69 2b 2b 29 7b  >nPending; i++){
d6f40 0a 20 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65  .          close
d6f50 28 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e 67  (pOpen->aPending
d6f60 5b 69 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  [i]);.        }.
d6f70 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
d6f80 66 72 65 65 28 70 4f 70 65 6e 2d 3e 61 50 65 6e  free(pOpen->aPen
d6f90 64 69 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 70  ding);.        p
d6fa0 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e 67 20 3d  Open->nPending =
d6fb0 20 30 3b 0a 20 20 20 20 20 20 20 20 70 4f 70 65   0;.        pOpe
d6fc0 6e 2d 3e 61 50 65 6e 64 69 6e 67 20 3d 20 30 3b  n->aPending = 0;
d6fd0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d6fe0 20 7d 0a 09 0a 65 6e 64 5f 75 6e 6c 6f 63 6b 3a   }...end_unlock:
d6ff0 0a 20 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b  .  leaveMutex();
d7000 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
d7010 45 5f 4f 4b 20 29 20 70 46 69 6c 65 2d 3e 6c 6f  E_OK ) pFile->lo
d7020 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79 70  cktype = locktyp
d7030 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  e;.  return rc;.
d7040 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
d7050 6e 63 74 69 6f 6e 20 70 65 72 66 6f 72 6d 73 20  nction performs 
d7060 74 68 65 20 70 61 72 74 73 20 6f 66 20 74 68 65  the parts of the
d7070 20 22 63 6c 6f 73 65 20 66 69 6c 65 22 20 6f 70   "close file" op
d7080 65 72 61 74 69 6f 6e 20 0a 2a 2a 20 63 6f 6d 6d  eration .** comm
d7090 6f 6e 20 74 6f 20 61 6c 6c 20 6c 6f 63 6b 69 6e  on to all lockin
d70a0 67 20 73 63 68 65 6d 65 73 2e 20 49 74 20 63 6c  g schemes. It cl
d70b0 6f 73 65 73 20 74 68 65 20 64 69 72 65 63 74 6f  oses the directo
d70c0 72 79 20 61 6e 64 20 66 69 6c 65 0a 2a 2a 20 68  ry and file.** h
d70d0 61 6e 64 6c 65 73 2c 20 69 66 20 74 68 65 79 20  andles, if they 
d70e0 61 72 65 20 76 61 6c 69 64 2c 20 61 6e 64 20 73  are valid, and s
d70f0 65 74 73 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f  ets all fields o
d7100 66 20 74 68 65 20 75 6e 69 78 46 69 6c 65 0a 2a  f the unixFile.*
d7110 2a 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 30  * structure to 0
d7120 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
d7130 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 73 71  closeUnixFile(sq
d7140 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 7b  lite3_file *id){
d7150 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
d7160 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
d7170 69 64 3b 0a 20 20 69 66 28 20 70 46 69 6c 65 20  id;.  if( pFile 
d7180 29 7b 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65  ){.    if( pFile
d7190 2d 3e 64 69 72 66 64 3e 3d 30 20 29 7b 0a 20 20  ->dirfd>=0 ){.  
d71a0 20 20 20 20 63 6c 6f 73 65 28 70 46 69 6c 65 2d      close(pFile-
d71b0 3e 64 69 72 66 64 29 3b 0a 20 20 20 20 7d 0a 20  >dirfd);.    }. 
d71c0 20 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 68 3e     if( pFile->h>
d71d0 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73  =0 ){.      clos
d71e0 65 28 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 20  e(pFile->h);.   
d71f0 20 7d 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b   }.#if IS_VXWORK
d7200 53 0a 20 20 20 20 69 66 28 20 70 46 69 6c 65 2d  S.    if( pFile-
d7210 3e 69 73 44 65 6c 65 74 65 20 26 26 20 70 46 69  >isDelete && pFi
d7220 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 20 29 7b  le->zRealpath ){
d7230 0a 20 20 20 20 20 20 75 6e 6c 69 6e 6b 28 70 46  .      unlink(pF
d7240 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 29 3b  ile->zRealpath);
d7250 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
d7260 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 20  File->zRealpath 
d7270 29 7b 0a 20 20 20 20 20 20 48 61 73 68 45 6c 65  ){.      HashEle
d7280 6d 20 2a 70 45 6c 65 6d 3b 0a 20 20 20 20 20 20  m *pElem;.      
d7290 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 70  int n = strlen(p
d72a0 46 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 29  File->zRealpath)
d72b0 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 45 6c 65   + 1;.      pEle
d72c0 6d 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46  m = sqlite3HashF
d72d0 69 6e 64 45 6c 65 6d 28 26 6e 61 6d 65 48 61 73  indElem(&nameHas
d72e0 68 2c 20 70 46 69 6c 65 2d 3e 7a 52 65 61 6c 70  h, pFile->zRealp
d72f0 61 74 68 2c 20 6e 29 3b 0a 20 20 20 20 20 20 69  ath, n);.      i
d7300 66 28 20 70 45 6c 65 6d 20 29 7b 0a 20 20 20 20  f( pElem ){.    
d7310 20 20 20 20 6c 6f 6e 67 20 63 6e 74 20 3d 20 28      long cnt = (
d7320 6c 6f 6e 67 29 70 45 6c 65 6d 2d 3e 64 61 74 61  long)pElem->data
d7330 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 2d 2d 3b  ;.        cnt--;
d7340 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e 74  .        if( cnt
d7350 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
d7360 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
d7370 72 74 28 26 6e 61 6d 65 48 61 73 68 2c 20 70 46  rt(&nameHash, pF
d7380 69 6c 65 2d 3e 7a 52 65 61 6c 70 61 74 68 2c 20  ile->zRealpath, 
d7390 6e 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d  n, 0);.        }
d73a0 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
d73b0 70 45 6c 65 6d 2d 3e 64 61 74 61 20 3d 20 28 76  pElem->data = (v
d73c0 6f 69 64 2a 29 63 6e 74 3b 0a 20 20 20 20 20 20  oid*)cnt;.      
d73d0 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
d73e0 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 4f 53 54  }.#endif.    OST
d73f0 52 41 43 45 32 28 22 43 4c 4f 53 45 20 20 20 25  RACE2("CLOSE   %
d7400 2d 33 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  -3d\n", pFile->h
d7410 29 3b 0a 20 20 20 20 4f 70 65 6e 43 6f 75 6e 74  );.    OpenCount
d7420 65 72 28 2d 31 29 3b 0a 20 20 20 20 6d 65 6d 73  er(-1);.    mems
d7430 65 74 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a  et(pFile, 0, siz
d7440 65 6f 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a  eof(unixFile));.
d7450 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
d7460 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
d7470 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a   Close a file..*
d7480 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69  /.static int uni
d7490 78 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  xClose(sqlite3_f
d74a0 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69 66 28 20  ile *id){.  if( 
d74b0 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69  id ){.    unixFi
d74c0 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
d74d0 78 46 69 6c 65 20 2a 29 69 64 3b 0a 20 20 20 20  xFile *)id;.    
d74e0 75 6e 69 78 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e  unixUnlock(id, N
d74f0 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 65 6e 74  O_LOCK);.    ent
d7500 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 69  erMutex();.    i
d7510 66 28 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 20  f( pFile->pOpen 
d7520 26 26 20 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d  && pFile->pOpen-
d7530 3e 6e 4c 6f 63 6b 20 29 7b 0a 20 20 20 20 20 20  >nLock ){.      
d7540 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65 20  /* If there are 
d7550 6f 75 74 73 74 61 6e 64 69 6e 67 20 6c 6f 63 6b  outstanding lock
d7560 73 2c 20 64 6f 20 6e 6f 74 20 61 63 74 75 61 6c  s, do not actual
d7570 6c 79 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c  ly close the fil
d7580 65 20 6a 75 73 74 0a 20 20 20 20 20 20 2a 2a 20  e just.      ** 
d7590 79 65 74 20 62 65 63 61 75 73 65 20 74 68 61 74  yet because that
d75a0 20 77 6f 75 6c 64 20 63 6c 65 61 72 20 74 68 6f   would clear tho
d75b0 73 65 20 6c 6f 63 6b 73 2e 20 20 49 6e 73 74 65  se locks.  Inste
d75c0 61 64 2c 20 61 64 64 20 74 68 65 20 66 69 6c 65  ad, add the file
d75d0 0a 20 20 20 20 20 20 2a 2a 20 64 65 73 63 72 69  .      ** descri
d75e0 70 74 6f 72 20 74 6f 20 70 4f 70 65 6e 2d 3e 61  ptor to pOpen->a
d75f0 50 65 6e 64 69 6e 67 2e 20 20 49 74 20 77 69 6c  Pending.  It wil
d7600 6c 20 62 65 20 61 75 74 6f 6d 61 74 69 63 61 6c  l be automatical
d7610 6c 79 20 63 6c 6f 73 65 64 20 77 68 65 6e 0a 20  ly closed when. 
d7620 20 20 20 20 20 2a 2a 20 74 68 65 20 6c 61 73 74       ** the last
d7630 20 6c 6f 63 6b 20 69 73 20 63 6c 65 61 72 65 64   lock is cleared
d7640 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
d7650 20 69 6e 74 20 2a 61 4e 65 77 3b 0a 20 20 20 20   int *aNew;.    
d7660 20 20 73 74 72 75 63 74 20 6f 70 65 6e 43 6e 74    struct openCnt
d7670 20 2a 70 4f 70 65 6e 20 3d 20 70 46 69 6c 65 2d   *pOpen = pFile-
d7680 3e 70 4f 70 65 6e 3b 0a 20 20 20 20 20 20 61 4e  >pOpen;.      aN
d7690 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
d76a0 6c 6c 6f 63 28 70 4f 70 65 6e 2d 3e 61 50 65 6e  lloc(pOpen->aPen
d76b0 64 69 6e 67 2c 20 28 70 4f 70 65 6e 2d 3e 6e 50  ding, (pOpen->nP
d76c0 65 6e 64 69 6e 67 2b 31 29 2a 73 69 7a 65 6f 66  ending+1)*sizeof
d76d0 28 69 6e 74 29 20 29 3b 0a 20 20 20 20 20 20 69  (int) );.      i
d76e0 66 28 20 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20  f( aNew==0 ){.  
d76f0 20 20 20 20 20 20 2f 2a 20 49 66 20 61 20 6d 61        /* If a ma
d7700 6c 6c 6f 63 20 66 61 69 6c 73 2c 20 6a 75 73 74  lloc fails, just
d7710 20 6c 65 61 6b 20 74 68 65 20 66 69 6c 65 20 64   leak the file d
d7720 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 20  escriptor */.   
d7730 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
d7740 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e    pOpen->aPendin
d7750 67 20 3d 20 61 4e 65 77 3b 0a 20 20 20 20 20 20  g = aNew;.      
d7760 20 20 70 4f 70 65 6e 2d 3e 61 50 65 6e 64 69 6e    pOpen->aPendin
d7770 67 5b 70 4f 70 65 6e 2d 3e 6e 50 65 6e 64 69 6e  g[pOpen->nPendin
d7780 67 5d 20 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20  g] = pFile->h;. 
d7790 20 20 20 20 20 20 20 70 4f 70 65 6e 2d 3e 6e 50         pOpen->nP
d77a0 65 6e 64 69 6e 67 2b 2b 3b 0a 20 20 20 20 20 20  ending++;.      
d77b0 20 20 70 46 69 6c 65 2d 3e 68 20 3d 20 2d 31 3b    pFile->h = -1;
d77c0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
d77d0 20 20 20 72 65 6c 65 61 73 65 4c 6f 63 6b 49 6e     releaseLockIn
d77e0 66 6f 28 70 46 69 6c 65 2d 3e 70 4c 6f 63 6b 29  fo(pFile->pLock)
d77f0 3b 0a 20 20 20 20 72 65 6c 65 61 73 65 4f 70 65  ;.    releaseOpe
d7800 6e 43 6e 74 28 70 46 69 6c 65 2d 3e 70 4f 70 65  nCnt(pFile->pOpe
d7810 6e 29 3b 0a 20 20 20 20 63 6c 6f 73 65 55 6e 69  n);.    closeUni
d7820 78 46 69 6c 65 28 69 64 29 3b 0a 20 20 20 20 6c  xFile(id);.    l
d7830 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 7d  eaveMutex();.  }
d7840 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
d7850 5f 4f 4b 3b 0a 7d 0a 0a 0a 23 69 66 20 53 51 4c  _OK;.}...#if SQL
d7860 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49  ITE_ENABLE_LOCKI
d7870 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66 20 21 49  NG_STYLE..#if !I
d7880 53 5f 56 58 57 4f 52 4b 53 0a 23 70 72 61 67 6d  S_VXWORKS.#pragm
d7890 61 20 6d 61 72 6b 20 41 46 50 20 53 75 70 70 6f  a mark AFP Suppo
d78a0 72 74 0a 0a 2f 2a 0a 20 2a 2a 20 54 68 65 20 61  rt../*. ** The a
d78b0 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
d78c0 20 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61   structure conta
d78d0 69 6e 73 20 61 6c 6c 20 61 66 70 20 6c 6f 63 6b  ins all afp lock
d78e0 20 73 70 65 63 69 66 69 63 20 73 74 61 74 65 0a   specific state.
d78f0 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75   */.typedef stru
d7900 63 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  ct afpLockingCon
d7910 74 65 78 74 20 61 66 70 4c 6f 63 6b 69 6e 67 43  text afpLockingC
d7920 6f 6e 74 65 78 74 3b 0a 73 74 72 75 63 74 20 61  ontext;.struct a
d7930 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  fpLockingContext
d7940 20 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c 6f   {.  unsigned lo
d7950 6e 67 20 6c 6f 6e 67 20 73 68 61 72 65 64 4c 6f  ng long sharedLo
d7960 63 6b 42 79 74 65 3b 0a 20 20 63 6f 6e 73 74 20  ckByte;.  const 
d7970 63 68 61 72 20 2a 66 69 6c 65 50 61 74 68 3b 0a  char *filePath;.
d7980 7d 3b 0a 0a 73 74 72 75 63 74 20 42 79 74 65 52  };..struct ByteR
d7990 61 6e 67 65 4c 6f 63 6b 50 42 32 0a 7b 0a 20 20  angeLockPB2.{.  
d79a0 75 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f  unsigned long lo
d79b0 6e 67 20 6f 66 66 73 65 74 3b 20 20 20 20 20 20  ng offset;      
d79c0 20 20 2f 2a 20 6f 66 66 73 65 74 20 74 6f 20 66    /* offset to f
d79d0 69 72 73 74 20 62 79 74 65 20 74 6f 20 6c 6f 63  irst byte to loc
d79e0 6b 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  k */.  unsigned 
d79f0 6c 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68  long long length
d7a00 3b 20 20 20 20 20 20 20 20 2f 2a 20 6e 62 72 20  ;        /* nbr 
d7a10 6f 66 20 62 79 74 65 73 20 74 6f 20 6c 6f 63 6b  of bytes to lock
d7a20 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c   */.  unsigned l
d7a30 6f 6e 67 20 6c 6f 6e 67 20 72 65 74 52 61 6e 67  ong long retRang
d7a40 65 53 74 61 72 74 3b 20 2f 2a 20 6e 62 72 20 6f  eStart; /* nbr o
d7a50 66 20 31 73 74 20 62 79 74 65 20 6c 6f 63 6b 65  f 1st byte locke
d7a60 64 20 69 66 20 73 75 63 63 65 73 73 66 75 6c 20  d if successful 
d7a70 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
d7a80 61 72 20 75 6e 4c 6f 63 6b 46 6c 61 67 3b 20 20  ar unLockFlag;  
d7a90 20 20 20 20 20 20 20 2f 2a 20 31 20 3d 20 75 6e         /* 1 = un
d7aa0 6c 6f 63 6b 2c 20 30 20 3d 20 6c 6f 63 6b 20 2a  lock, 0 = lock *
d7ab0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
d7ac0 72 20 73 74 61 72 74 45 6e 64 46 6c 61 67 3b 20  r startEndFlag; 
d7ad0 20 20 20 20 20 20 2f 2a 20 31 3d 72 65 6c 20 74        /* 1=rel t
d7ae0 6f 20 65 6e 64 20 6f 66 20 66 6f 72 6b 2c 20 30  o end of fork, 0
d7af0 3d 72 65 6c 20 74 6f 20 73 74 61 72 74 20 2a 2f  =rel to start */
d7b00 0a 20 20 69 6e 74 20 66 64 3b 20 20 20 20 20 20  .  int fd;      
d7b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7b20 20 20 20 20 20 2f 2a 20 66 69 6c 65 20 64 65 73       /* file des
d7b30 63 20 74 6f 20 61 73 73 6f 63 20 74 68 69 73 20  c to assoc this 
d7b40 6c 6f 63 6b 20 77 69 74 68 20 2a 2f 0a 7d 3b 0a  lock with */.};.
d7b50 0a 23 64 65 66 69 6e 65 20 61 66 70 66 73 42 79  .#define afpfsBy
d7b60 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46 53 43 54  teRangeLock2FSCT
d7b70 4c 20 20 20 20 20 20 20 20 5f 49 4f 57 52 28 27  L        _IOWR('
d7b80 7a 27 2c 20 32 33 2c 20 73 74 72 75 63 74 20 42  z', 23, struct B
d7b90 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32 29  yteRangeLockPB2)
d7ba0 0a 0a 2f 2a 20 0a 20 2a 2a 20 52 65 74 75 72 6e  ../* . ** Return
d7bb0 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75   SQLITE_OK on su
d7bc0 63 63 65 73 73 2c 20 53 51 4c 49 54 45 5f 42 55  ccess, SQLITE_BU
d7bd0 53 59 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a 20  SY on failure.. 
d7be0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 5f 41  */.static int _A
d7bf0 46 50 46 53 53 65 74 4c 6f 63 6b 28 0a 20 20 63  FPFSSetLock(.  c
d7c00 6f 6e 73 74 20 63 68 61 72 20 2a 70 61 74 68 2c  onst char *path,
d7c10 20 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46   .  unixFile *pF
d7c20 69 6c 65 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64  ile, .  unsigned
d7c30 20 6c 6f 6e 67 20 6c 6f 6e 67 20 6f 66 66 73 65   long long offse
d7c40 74 2c 20 0a 20 20 75 6e 73 69 67 6e 65 64 20 6c  t, .  unsigned l
d7c50 6f 6e 67 20 6c 6f 6e 67 20 6c 65 6e 67 74 68 2c  ong long length,
d7c60 20 0a 20 20 69 6e 74 20 73 65 74 4c 6f 63 6b 46   .  int setLockF
d7c70 6c 61 67 0a 29 7b 0a 20 20 73 74 72 75 63 74 20  lag.){.  struct 
d7c80 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 50 42 32  ByteRangeLockPB2
d7c90 20 20 20 20 20 20 20 70 62 3b 0a 20 20 69 6e 74         pb;.  int
d7ca0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d7cb0 20 20 20 20 20 65 72 72 3b 0a 20 20 0a 20 20 70       err;.  .  p
d7cc0 62 2e 75 6e 4c 6f 63 6b 46 6c 61 67 20 3d 20 73  b.unLockFlag = s
d7cd0 65 74 4c 6f 63 6b 46 6c 61 67 20 3f 20 30 20 3a  etLockFlag ? 0 :
d7ce0 20 31 3b 0a 20 20 70 62 2e 73 74 61 72 74 45 6e   1;.  pb.startEn
d7cf0 64 46 6c 61 67 20 3d 20 30 3b 0a 20 20 70 62 2e  dFlag = 0;.  pb.
d7d00 6f 66 66 73 65 74 20 3d 20 6f 66 66 73 65 74 3b  offset = offset;
d7d10 0a 20 20 70 62 2e 6c 65 6e 67 74 68 20 3d 20 6c  .  pb.length = l
d7d20 65 6e 67 74 68 3b 20 0a 20 20 70 62 2e 66 64 20  ength; .  pb.fd 
d7d30 3d 20 70 46 69 6c 65 2d 3e 68 3b 0a 20 20 4f 53  = pFile->h;.  OS
d7d40 54 52 41 43 45 35 28 22 41 46 50 4c 4f 43 4b 20  TRACE5("AFPLOCK 
d7d50 73 65 74 74 69 6e 67 20 6c 6f 63 6b 20 25 73 20  setting lock %s 
d7d60 66 6f 72 20 25 64 20 69 6e 20 72 61 6e 67 65 20  for %d in range 
d7d70 25 6c 6c 78 3a 25 6c 6c 78 5c 6e 22 2c 20 0a 20  %llx:%llx\n", . 
d7d80 20 20 20 28 73 65 74 4c 6f 63 6b 46 6c 61 67 3f     (setLockFlag?
d7d90 22 4f 4e 22 3a 22 4f 46 46 22 29 2c 20 70 46 69  "ON":"OFF"), pFi
d7da0 6c 65 2d 3e 68 2c 20 6f 66 66 73 65 74 2c 20 6c  le->h, offset, l
d7db0 65 6e 67 74 68 29 3b 0a 20 20 65 72 72 20 3d 20  ength);.  err = 
d7dc0 66 73 63 74 6c 28 70 61 74 68 2c 20 61 66 70 66  fsctl(path, afpf
d7dd0 73 42 79 74 65 52 61 6e 67 65 4c 6f 63 6b 32 46  sByteRangeLock2F
d7de0 53 43 54 4c 2c 20 26 70 62 2c 20 30 29 3b 0a 20  SCTL, &pb, 0);. 
d7df0 20 69 66 20 28 20 65 72 72 3d 3d 2d 31 20 29 20   if ( err==-1 ) 
d7e00 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
d7e10 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65    int tErrno = e
d7e20 72 72 6e 6f 3b 0a 20 20 20 20 4f 53 54 52 41 43  rrno;.    OSTRAC
d7e30 45 34 28 22 41 46 50 4c 4f 43 4b 20 66 61 69 6c  E4("AFPLOCK fail
d7e40 65 64 20 74 6f 20 66 73 63 74 6c 28 29 20 27 25  ed to fsctl() '%
d7e50 73 27 20 25 64 20 25 73 5c 6e 22 2c 20 70 61 74  s' %d %s\n", pat
d7e60 68 2c 20 74 45 72 72 6e 6f 2c 20 73 74 72 65 72  h, tErrno, strer
d7e70 72 6f 72 28 74 45 72 72 6e 6f 29 29 3b 0a 20 20  ror(tErrno));.  
d7e80 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
d7e90 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
d7ea0 28 74 45 72 72 6e 6f 2c 20 73 65 74 4c 6f 63 6b  (tErrno, setLock
d7eb0 46 6c 61 67 20 3f 20 53 51 4c 49 54 45 5f 49 4f  Flag ? SQLITE_IO
d7ec0 45 52 52 5f 4c 4f 43 4b 20 3a 20 53 51 4c 49 54  ERR_LOCK : SQLIT
d7ed0 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b  E_IOERR_UNLOCK);
d7ee0 20 2f 2a 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20   /* error */.   
d7ef0 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
d7f00 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20 20  OR(rc) ){.      
d7f10 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f  pFile->lastErrno
d7f20 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 7d   = tErrno;.    }
d7f30 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
d7f40 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 72    } else {.    r
d7f50 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
d7f60 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 41 46 50 2d 73  .  }.}../* AFP-s
d7f70 74 79 6c 65 20 72 65 73 65 72 76 65 64 20 6c 6f  tyle reserved lo
d7f80 63 6b 20 63 68 65 63 6b 69 6e 67 20 66 6f 6c 6c  ck checking foll
d7f90 6f 77 69 6e 67 20 74 68 65 20 62 65 68 61 76 69  owing the behavi
d7fa0 6f 72 20 6f 66 20 0a 2a 2a 20 75 6e 69 78 43 68  or of .** unixCh
d7fb0 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c  eckReservedLock,
d7fc0 20 73 65 65 20 74 68 65 20 75 6e 69 78 43 68 65   see the unixChe
d7fd0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 20 66  ckReservedLock f
d7fe0 75 6e 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73  unction comments
d7ff0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61   */.static int a
d8000 66 70 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  fpCheckReservedL
d8010 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
d8020 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f   *id, int *pResO
d8030 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ut){.  int rc = 
d8040 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
d8050 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
d8060 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
d8070 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
d8080 3b 0a 20 20 0a 20 20 53 69 6d 75 6c 61 74 65 49  ;.  .  SimulateI
d8090 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
d80a0 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
d80b0 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29  KRESERVEDLOCK; )
d80c0 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
d80d0 46 69 6c 65 20 29 3b 0a 20 20 61 66 70 4c 6f 63  File );.  afpLoc
d80e0 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63 6f 6e  kingContext *con
d80f0 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63 6b 69  text = (afpLocki
d8100 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70 46 69  ngContext *) pFi
d8110 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65  le->lockingConte
d8120 78 74 3b 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63  xt;.  .  /* Chec
d8130 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
d8140 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
d8150 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
d8160 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
d8170 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
d8180 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
d8190 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  rved = 1;.  }.  
d81a0 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  .  /* Otherwise 
d81b0 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  see if some othe
d81c0 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  r process holds 
d81d0 69 74 2e 0a 20 20 20 2a 2f 0a 20 20 69 66 28 20  it..   */.  if( 
d81e0 21 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20  !reserved ){.   
d81f0 20 2f 2a 20 6c 6f 63 6b 20 74 68 65 20 52 45 53   /* lock the RES
d8200 45 52 56 45 44 20 62 79 74 65 20 2a 2f 0a 20 20  ERVED byte */.  
d8210 20 20 69 6e 74 20 6c 72 63 20 3d 20 5f 41 46 50    int lrc = _AFP
d8220 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  FSSetLock(contex
d8230 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69  t->filePath, pFi
d8240 6c 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54  le, RESERVED_BYT
d8250 45 2c 20 31 2c 31 29 3b 20 20 0a 20 20 20 20 69  E, 1,1);  .    i
d8260 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 6c 72  f( SQLITE_OK==lr
d8270 63 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 69 66  c ){.      /* if
d8280 20 77 65 20 73 75 63 63 65 65 64 65 64 20 69 6e   we succeeded in
d8290 20 74 61 6b 69 6e 67 20 74 68 65 20 72 65 73 65   taking the rese
d82a0 72 76 65 64 20 6c 6f 63 6b 2c 20 75 6e 6c 6f 63  rved lock, unloc
d82b0 6b 20 69 74 20 74 6f 20 72 65 73 74 6f 72 65 0a  k it to restore.
d82c0 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6f 72 69        ** the ori
d82d0 67 69 6e 61 6c 20 73 74 61 74 65 20 2a 2f 0a 20  ginal state */. 
d82e0 20 20 20 20 20 6c 72 63 20 3d 20 5f 41 46 50 46       lrc = _AFPF
d82f0 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  SSetLock(context
d8300 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c  ->filePath, pFil
d8310 65 2c 20 52 45 53 45 52 56 45 44 5f 42 59 54 45  e, RESERVED_BYTE
d8320 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 7d 20 65  , 1, 0);.    } e
d8330 6c 73 65 20 7b 0a 20 20 20 20 20 20 2f 2a 20 69  lse {.      /* i
d8340 66 20 77 65 20 66 61 69 6c 65 64 20 74 6f 20 67  f we failed to g
d8350 65 74 20 74 68 65 20 6c 6f 63 6b 20 74 68 65 6e  et the lock then
d8360 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65 20 6d 75   someone else mu
d8370 73 74 20 68 61 76 65 20 69 74 20 2a 2f 0a 20 20  st have it */.  
d8380 20 20 20 20 72 65 73 65 72 76 65 64 20 3d 20 31      reserved = 1
d8390 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
d83a0 49 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72  IS_LOCK_ERROR(lr
d83b0 63 29 20 29 7b 0a 20 20 20 20 20 20 72 63 3d 6c  c) ){.      rc=l
d83c0 72 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  rc;.    }.  }.  
d83d0 0a 20 20 4f 53 54 52 41 43 45 34 28 22 54 45 53  .  OSTRACE4("TES
d83e0 54 20 57 52 2d 4c 4f 43 4b 20 25 64 20 25 64 20  T WR-LOCK %d %d 
d83f0 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c  %d\n", pFile->h,
d8400 20 72 63 2c 20 72 65 73 65 72 76 65 64 29 3b 0a   rc, reserved);.
d8410 20 20 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20    .  *pResOut = 
d8420 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65 74 75  reserved;.  retu
d8430 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 46 50  rn rc;.}../* AFP
d8440 2d 73 74 79 6c 65 20 6c 6f 63 6b 69 6e 67 20 66  -style locking f
d8450 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 62 65 68  ollowing the beh
d8460 61 76 69 6f 72 20 6f 66 20 75 6e 69 78 4c 6f 63  avior of unixLoc
d8470 6b 2c 20 73 65 65 20 74 68 65 20 75 6e 69 78 4c  k, see the unixL
d8480 6f 63 6b 20 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  ock .** function
d8490 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72 20 64 65   comments for de
d84a0 74 61 69 6c 73 20 6f 66 20 6c 6f 63 6b 20 6d 61  tails of lock ma
d84b0 6e 61 67 65 6d 65 6e 74 2e 20 2a 2f 0a 73 74 61  nagement. */.sta
d84c0 74 69 63 20 69 6e 74 20 61 66 70 4c 6f 63 6b 28  tic int afpLock(
d84d0 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
d84e0 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29 7b  , int locktype){
d84f0 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
d8500 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46 69 6c  TE_OK;.  unixFil
d8510 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
d8520 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 66 70 4c  File*)id;.  afpL
d8530 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 63  ockingContext *c
d8540 6f 6e 74 65 78 74 20 3d 20 28 61 66 70 4c 6f 63  ontext = (afpLoc
d8550 6b 69 6e 67 43 6f 6e 74 65 78 74 20 2a 29 20 70  kingContext *) p
d8560 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e  File->lockingCon
d8570 74 65 78 74 3b 0a 20 20 0a 20 20 61 73 73 65 72  text;.  .  asser
d8580 74 28 20 70 46 69 6c 65 20 29 3b 0a 20 20 4f 53  t( pFile );.  OS
d8590 54 52 41 43 45 35 28 22 4c 4f 43 4b 20 20 20 20  TRACE5("LOCK    
d85a0 25 64 20 25 73 20 77 61 73 20 25 73 20 70 69 64  %d %s was %s pid
d85b0 3d 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68  =%d\n", pFile->h
d85c0 2c 0a 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74  ,.         lockt
d85d0 79 70 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65  ypeName(locktype
d85e0 29 2c 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28  ), locktypeName(
d85f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
d8600 2c 20 67 65 74 70 69 64 28 29 29 3b 0a 0a 20 20  , getpid());..  
d8610 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
d8620 6c 72 65 61 64 79 20 61 20 6c 6f 63 6b 20 6f 66  lready a lock of
d8630 20 74 68 69 73 20 74 79 70 65 20 6f 72 20 6d 6f   this type or mo
d8640 72 65 20 72 65 73 74 72 69 63 74 69 76 65 20 6f  re restrictive o
d8650 6e 20 74 68 65 0a 20 20 2a 2a 20 75 6e 69 78 46  n the.  ** unixF
d8660 69 6c 65 2c 20 64 6f 20 6e 6f 74 68 69 6e 67 2e  ile, do nothing.
d8670 20 44 6f 6e 27 74 20 75 73 65 20 74 68 65 20 61   Don't use the a
d8680 66 70 5f 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78 69  fp_end_lock: exi
d8690 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a 20  t path, as.  ** 
d86a0 65 6e 74 65 72 4d 75 74 65 78 28 29 20 68 61 73  enterMutex() has
d86b0 6e 27 74 20 62 65 65 6e 20 63 61 6c 6c 65 64 20  n't been called 
d86c0 79 65 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  yet..  */.  if( 
d86d0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e  pFile->locktype>
d86e0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
d86f0 20 4f 53 54 52 41 43 45 33 28 22 4c 4f 43 4b 20   OSTRACE3("LOCK 
d8700 20 20 20 25 64 20 25 73 20 6f 6b 20 28 61 6c 72     %d %s ok (alr
d8710 65 61 64 79 20 68 65 6c 64 29 5c 6e 22 2c 20 70  eady held)\n", p
d8720 46 69 6c 65 2d 3e 68 2c 0a 20 20 20 20 20 20 20  File->h,.       
d8730 20 20 20 20 6c 6f 63 6b 74 79 70 65 4e 61 6d 65      locktypeName
d8740 28 6c 6f 63 6b 74 79 70 65 29 29 3b 0a 20 20 20  (locktype));.   
d8750 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d8760 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  K;.  }..  /* Mak
d8770 65 20 73 75 72 65 20 74 68 65 20 6c 6f 63 6b 69  e sure the locki
d8780 6e 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 63  ng sequence is c
d8790 6f 72 72 65 63 74 0a 20 20 2a 2f 0a 20 20 61 73  orrect.  */.  as
d87a0 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
d87b0 6b 74 79 70 65 21 3d 4e 4f 5f 4c 4f 43 4b 20 7c  ktype!=NO_LOCK |
d87c0 7c 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52  | locktype==SHAR
d87d0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73  ED_LOCK );.  ass
d87e0 65 72 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 50  ert( locktype!=P
d87f0 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29 3b 0a 20  ENDING_LOCK );. 
d8800 20 61 73 73 65 72 74 28 20 6c 6f 63 6b 74 79 70   assert( locktyp
d8810 65 21 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b  e!=RESERVED_LOCK
d8820 20 7c 7c 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74   || pFile->lockt
d8830 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d8840 20 29 3b 0a 20 20 0a 20 20 2f 2a 20 54 68 69 73   );.  .  /* This
d8850 20 6d 75 74 65 78 20 69 73 20 6e 65 65 64 65 64   mutex is needed
d8860 20 62 65 63 61 75 73 65 20 70 46 69 6c 65 2d 3e   because pFile->
d8870 70 4c 6f 63 6b 20 69 73 20 73 68 61 72 65 64 20  pLock is shared 
d8880 61 63 72 6f 73 73 20 74 68 72 65 61 64 73 0a 20  across threads. 
d8890 20 2a 2f 0a 20 20 65 6e 74 65 72 4d 75 74 65 78   */.  enterMutex
d88a0 28 29 3b 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73  ();..  /* Make s
d88b0 75 72 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ure the current 
d88c0 74 68 72 65 61 64 20 6f 77 6e 73 20 74 68 65 20  thread owns the 
d88d0 70 46 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72 63  pFile..  */.  rc
d88e0 20 3d 20 74 72 61 6e 73 66 65 72 4f 77 6e 65 72   = transferOwner
d88f0 73 68 69 70 28 70 46 69 6c 65 29 3b 0a 20 20 69  ship(pFile);.  i
d8900 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
d8910 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 4d 75 74   ){.    leaveMut
d8920 65 78 28 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ex();.    return
d8930 20 72 63 3b 0a 20 20 7d 0a 20 20 20 20 0a 20 20   rc;.  }.    .  
d8940 2f 2a 20 41 20 50 45 4e 44 49 4e 47 20 6c 6f 63  /* A PENDING loc
d8950 6b 20 69 73 20 6e 65 65 64 65 64 20 62 65 66 6f  k is needed befo
d8960 72 65 20 61 63 71 75 69 72 69 6e 67 20 61 20 53  re acquiring a S
d8970 48 41 52 45 44 20 6c 6f 63 6b 20 61 6e 64 20 62  HARED lock and b
d8980 65 66 6f 72 65 0a 20 20 2a 2a 20 61 63 71 75 69  efore.  ** acqui
d8990 72 69 6e 67 20 61 6e 20 45 58 43 4c 55 53 49 56  ring an EXCLUSIV
d89a0 45 20 6c 6f 63 6b 2e 20 20 46 6f 72 20 74 68 65  E lock.  For the
d89b0 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74 68   SHARED lock, th
d89c0 65 20 50 45 4e 44 49 4e 47 20 77 69 6c 6c 0a 20  e PENDING will. 
d89d0 20 2a 2a 20 62 65 20 72 65 6c 65 61 73 65 64 2e   ** be released.
d89e0 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
d89f0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
d8a00 4b 20 0a 20 20 20 20 20 20 7c 7c 20 28 6c 6f 63  K .      || (loc
d8a10 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
d8a20 5f 4c 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e  _LOCK && pFile->
d8a30 6c 6f 63 6b 74 79 70 65 3c 50 45 4e 44 49 4e 47  locktype<PENDING
d8a40 5f 4c 4f 43 4b 29 0a 20 20 29 7b 0a 20 20 20 20  _LOCK).  ){.    
d8a50 69 6e 74 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  int failed;.    
d8a60 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53  failed = _AFPFSS
d8a70 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e  etLock(context->
d8a80 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c  filePath, pFile,
d8a90 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 31   PENDING_BYTE, 1
d8aa0 2c 20 31 29 3b 0a 20 20 20 20 69 66 20 28 66 61  , 1);.    if (fa
d8ab0 69 6c 65 64 29 20 7b 0a 20 20 20 20 20 20 72 63  iled) {.      rc
d8ac0 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20   = failed;.     
d8ad0 20 67 6f 74 6f 20 61 66 70 5f 65 6e 64 5f 6c 6f   goto afp_end_lo
d8ae0 63 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ck;.    }.  }.  
d8af0 0a 20 20 2f 2a 20 49 66 20 63 6f 6e 74 72 6f 6c  .  /* If control
d8b00 20 67 65 74 73 20 74 6f 20 74 68 69 73 20 70 6f   gets to this po
d8b10 69 6e 74 2c 20 74 68 65 6e 20 61 63 74 75 61 6c  int, then actual
d8b20 6c 79 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20  ly go ahead and 
d8b30 6d 61 6b 65 0a 20 20 2a 2a 20 6f 70 65 72 61 74  make.  ** operat
d8b40 69 6e 67 20 73 79 73 74 65 6d 20 63 61 6c 6c 73  ing system calls
d8b50 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
d8b60 65 64 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20 20  ed lock..  */.  
d8b70 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 53 48  if( locktype==SH
d8b80 41 52 45 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20  ARED_LOCK ){.   
d8b90 20 69 6e 74 20 6c 6b 2c 20 6c 72 63 31 2c 20 6c   int lk, lrc1, l
d8ba0 72 63 32 2c 20 6c 72 63 31 45 72 72 6e 6f 3b 0a  rc2, lrc1Errno;.
d8bb0 20 20 20 20 0a 20 20 20 20 2f 2a 20 4e 6f 77 20      .    /* Now 
d8bc0 67 65 74 20 74 68 65 20 72 65 61 64 2d 6c 6f 63  get the read-loc
d8bd0 6b 20 53 48 41 52 45 44 5f 4c 4f 43 4b 20 2a 2f  k SHARED_LOCK */
d8be0 0a 20 20 20 20 2f 2a 20 6e 6f 74 65 20 74 68 61  .    /* note tha
d8bf0 74 20 74 68 65 20 71 75 61 6c 69 74 79 20 6f 66  t the quality of
d8c00 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73 20   the randomness 
d8c10 64 6f 65 73 6e 27 74 20 6d 61 74 74 65 72 20 74  doesn't matter t
d8c20 68 61 74 20 6d 75 63 68 20 2a 2f 0a 20 20 20 20  hat much */.    
d8c30 6c 6b 20 3d 20 72 61 6e 64 6f 6d 28 29 3b 20 0a  lk = random(); .
d8c40 20 20 20 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61      context->sha
d8c50 72 65 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 6c  redLockByte = (l
d8c60 6b 20 26 20 30 78 37 66 66 66 66 66 66 66 29 25  k & 0x7fffffff)%
d8c70 28 53 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31  (SHARED_SIZE - 1
d8c80 29 3b 0a 20 20 20 20 6c 72 63 31 20 3d 20 5f 41  );.    lrc1 = _A
d8c90 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
d8ca0 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
d8cb0 46 69 6c 65 2c 20 0a 20 20 20 20 20 20 20 20 20  File, .         
d8cc0 20 53 48 41 52 45 44 5f 46 49 52 53 54 2b 63 6f   SHARED_FIRST+co
d8cd0 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63  ntext->sharedLoc
d8ce0 6b 42 79 74 65 2c 20 31 2c 20 31 29 3b 0a 20 20  kByte, 1, 1);.  
d8cf0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d8d00 52 4f 52 28 6c 72 63 31 29 20 29 7b 0a 20 20 20  ROR(lrc1) ){.   
d8d10 20 20 20 6c 72 63 31 45 72 72 6e 6f 20 3d 20 70     lrc1Errno = p
d8d20 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 3b  File->lastErrno;
d8d30 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 44 72  .    }.    /* Dr
d8d40 6f 70 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  op the temporary
d8d50 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 2a 2f   PENDING lock */
d8d60 0a 20 20 20 20 6c 72 63 32 20 3d 20 5f 41 46 50  .    lrc2 = _AFP
d8d70 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78  FSSetLock(contex
d8d80 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69  t->filePath, pFi
d8d90 6c 65 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  le, PENDING_BYTE
d8da0 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 0a 20 20  , 1, 0);.    .  
d8db0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
d8dc0 52 4f 52 28 6c 72 63 31 29 20 29 20 7b 0a 20 20  ROR(lrc1) ) {.  
d8dd0 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45      pFile->lastE
d8de0 72 72 6e 6f 20 3d 20 6c 72 63 31 45 72 72 6e 6f  rrno = lrc1Errno
d8df0 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63  ;.      rc = lrc
d8e00 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66  1;.      goto af
d8e10 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20  p_end_lock;.    
d8e20 7d 20 65 6c 73 65 20 69 66 28 20 49 53 5f 4c 4f  } else if( IS_LO
d8e30 43 4b 5f 45 52 52 4f 52 28 6c 72 63 32 29 20 29  CK_ERROR(lrc2) )
d8e40 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63  {.      rc = lrc
d8e50 32 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 66  2;.      goto af
d8e60 70 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 20 20  p_end_lock;.    
d8e70 7d 20 65 6c 73 65 20 69 66 28 20 6c 72 63 31 20  } else if( lrc1 
d8e80 21 3d 20 53 51 4c 49 54 45 5f 4f 4b 20 29 20 7b  != SQLITE_OK ) {
d8e90 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 72 63 31  .      rc = lrc1
d8ea0 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20  ;.    } else {. 
d8eb0 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b       pFile->lock
d8ec0 74 79 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f  type = SHARED_LO
d8ed0 43 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  CK;.    }.  }els
d8ee0 65 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65  e{.    /* The re
d8ef0 71 75 65 73 74 20 77 61 73 20 66 6f 72 20 61 20  quest was for a 
d8f00 52 45 53 45 52 56 45 44 20 6f 72 20 45 58 43 4c  RESERVED or EXCL
d8f10 55 53 49 56 45 20 6c 6f 63 6b 2e 20 20 49 74 20  USIVE lock.  It 
d8f20 69 73 0a 20 20 20 20 2a 2a 20 61 73 73 75 6d 65  is.    ** assume
d8f30 64 20 74 68 61 74 20 74 68 65 72 65 20 69 73 20  d that there is 
d8f40 61 20 53 48 41 52 45 44 20 6f 72 20 67 72 65 61  a SHARED or grea
d8f50 74 65 72 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  ter lock on the 
d8f60 66 69 6c 65 0a 20 20 20 20 2a 2a 20 61 6c 72 65  file.    ** alre
d8f70 61 64 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ady..    */.    
d8f80 69 6e 74 20 66 61 69 6c 65 64 20 3d 20 30 3b 0a  int failed = 0;.
d8f90 20 20 20 20 61 73 73 65 72 74 28 20 30 21 3d 70      assert( 0!=p
d8fa0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 29  File->locktype )
d8fb0 3b 0a 20 20 20 20 69 66 20 28 6c 6f 63 6b 74 79  ;.    if (lockty
d8fc0 70 65 20 3e 3d 20 52 45 53 45 52 56 45 44 5f 4c  pe >= RESERVED_L
d8fd0 4f 43 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f  OCK && pFile->lo
d8fe0 63 6b 74 79 70 65 20 3c 20 52 45 53 45 52 56 45  cktype < RESERVE
d8ff0 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20  D_LOCK) {.      
d9000 20 20 2f 2a 20 41 63 71 75 69 72 65 20 61 20 52    /* Acquire a R
d9010 45 53 45 52 56 45 44 20 6c 6f 63 6b 20 2a 2f 0a  ESERVED lock */.
d9020 20 20 20 20 20 20 20 20 66 61 69 6c 65 64 20 3d          failed =
d9030 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63   _AFPFSSetLock(c
d9040 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68  ontext->filePath
d9050 2c 20 70 46 69 6c 65 2c 20 52 45 53 45 52 56 45  , pFile, RESERVE
d9060 44 5f 42 59 54 45 2c 20 31 2c 31 29 3b 0a 20 20  D_BYTE, 1,1);.  
d9070 20 20 7d 0a 20 20 20 20 69 66 20 28 21 66 61 69    }.    if (!fai
d9080 6c 65 64 20 26 26 20 6c 6f 63 6b 74 79 70 65 20  led && locktype 
d9090 3d 3d 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  == EXCLUSIVE_LOC
d90a0 4b 29 20 7b 0a 20 20 20 20 20 20 2f 2a 20 41 63  K) {.      /* Ac
d90b0 71 75 69 72 65 20 61 6e 20 45 58 43 4c 55 53 49  quire an EXCLUSI
d90c0 56 45 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  VE lock */.     
d90d0 20 20 20 0a 20 20 20 20 20 20 2f 2a 20 52 65 6d     .      /* Rem
d90e0 6f 76 65 20 74 68 65 20 73 68 61 72 65 64 20 6c  ove the shared l
d90f0 6f 63 6b 20 62 65 66 6f 72 65 20 74 72 79 69 6e  ock before tryin
d9100 67 20 74 68 65 20 72 61 6e 67 65 2e 20 20 77 65  g the range.  we
d9110 27 6c 6c 20 6e 65 65 64 20 74 6f 20 0a 20 20 20  'll need to .   
d9120 20 20 20 2a 2a 20 72 65 65 73 74 61 62 6c 69 73     ** reestablis
d9130 68 20 74 68 65 20 73 68 61 72 65 64 20 6c 6f 63  h the shared loc
d9140 6b 20 69 66 20 77 65 20 63 61 6e 27 74 20 67 65  k if we can't ge
d9150 74 20 74 68 65 20 20 61 66 70 55 6e 6c 6f 63 6b  t the  afpUnlock
d9160 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
d9170 69 66 20 28 21 28 66 61 69 6c 65 64 20 3d 20 5f  if (!(failed = _
d9180 41 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e  AFPFSSetLock(con
d9190 74 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20  text->filePath, 
d91a0 70 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49  pFile, SHARED_FI
d91b0 52 53 54 20 2b 0a 20 20 20 20 20 20 20 20 20 20  RST +.          
d91c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
d91d0 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f  ontext->sharedLo
d91e0 63 6b 42 79 74 65 2c 20 31 2c 20 30 29 29 29 20  ckByte, 1, 0))) 
d91f0 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 77  {.        /* now
d9200 20 61 74 74 65 6d 6d 70 74 20 74 6f 20 67 65 74   attemmpt to get
d9210 20 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c   the exclusive l
d9220 6f 63 6b 20 72 61 6e 67 65 20 2a 2f 0a 20 20 20  ock range */.   
d9230 20 20 20 20 20 66 61 69 6c 65 64 20 3d 20 5f 41       failed = _A
d9240 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
d9250 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
d9260 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52  File, SHARED_FIR
d9270 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ST, .           
d9280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9290 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a 45 2c      SHARED_SIZE,
d92a0 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69 66 20   1);.        if 
d92b0 28 66 61 69 6c 65 64 20 26 26 20 28 66 61 69 6c  (failed && (fail
d92c0 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f  ed = _AFPFSSetLo
d92d0 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65  ck(context->file
d92e0 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20  Path, pFile, .  
d92f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9300 20 20 20 20 20 53 48 41 52 45 44 5f 46 49 52 53       SHARED_FIRS
d9310 54 20 2b 20 63 6f 6e 74 65 78 74 2d 3e 73 68 61  T + context->sha
d9320 72 65 64 4c 6f 63 6b 42 79 74 65 2c 20 31 2c 20  redLockByte, 1, 
d9330 31 29 29 29 20 7b 0a 20 20 20 20 20 20 20 20 20  1))) {.         
d9340 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a 20 20   rc = failed;.  
d9350 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
d9360 65 6c 73 65 20 7b 0a 20 20 20 20 20 20 20 20 72  else {.        r
d9370 63 20 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 20  c = failed; .   
d9380 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
d9390 66 28 20 66 61 69 6c 65 64 20 29 7b 0a 20 20 20  f( failed ){.   
d93a0 20 20 20 72 63 20 3d 20 66 61 69 6c 65 64 3b 0a     rc = failed;.
d93b0 20 20 20 20 7d 0a 20 20 7d 0a 20 20 0a 20 20 69      }.  }.  .  i
d93c0 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
d93d0 20 29 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c   ){.    pFile->l
d93e0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
d93f0 70 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  pe;.  }else if( 
d9400 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53  locktype==EXCLUS
d9410 49 56 45 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20  IVE_LOCK ){.    
d9420 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
d9430 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
d9440 20 20 7d 0a 20 20 0a 61 66 70 5f 65 6e 64 5f 6c    }.  .afp_end_l
d9450 6f 63 6b 3a 0a 20 20 6c 65 61 76 65 4d 75 74 65  ock:.  leaveMute
d9460 78 28 29 3b 0a 20 20 4f 53 54 52 41 43 45 34 28  x();.  OSTRACE4(
d9470 22 4c 4f 43 4b 20 20 20 20 25 64 20 25 73 20 25  "LOCK    %d %s %
d9480 73 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  s\n", pFile->h, 
d9490 6c 6f 63 6b 74 79 70 65 4e 61 6d 65 28 6c 6f 63  locktypeName(loc
d94a0 6b 74 79 70 65 29 2c 20 0a 20 20 20 20 20 20 20  ktype), .       
d94b0 20 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20    rc==SQLITE_OK 
d94c0 3f 20 22 6f 6b 22 20 3a 20 22 66 61 69 6c 65 64  ? "ok" : "failed
d94d0 22 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  ");.  return rc;
d94e0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 77 65 72 20  .}../*.** Lower 
d94f0 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
d9500 6c 20 6f 6e 20 66 69 6c 65 20 64 65 73 63 72 69  l on file descri
d9510 70 74 6f 72 20 70 46 69 6c 65 20 74 6f 20 6c 6f  ptor pFile to lo
d9520 63 6b 74 79 70 65 2e 20 20 6c 6f 63 6b 74 79 70  cktype.  locktyp
d9530 65 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 69 74  e.** must be eit
d9540 68 65 72 20 4e 4f 5f 4c 4f 43 4b 20 6f 72 20 53  her NO_LOCK or S
d9550 48 41 52 45 44 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a  HARED_LOCK..**.*
d9560 2a 20 49 66 20 74 68 65 20 6c 6f 63 6b 69 6e 67  * If the locking
d9570 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 66 69   level of the fi
d9580 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69 73  le descriptor is
d9590 20 61 6c 72 65 61 64 79 20 61 74 20 6f 72 20 62   already at or b
d95a0 65 6c 6f 77 0a 2a 2a 20 74 68 65 20 72 65 71 75  elow.** the requ
d95b0 65 73 74 65 64 20 6c 6f 63 6b 69 6e 67 20 6c 65  ested locking le
d95c0 76 65 6c 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  vel, this routin
d95d0 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  e is a no-op..*/
d95e0 0a 73 74 61 74 69 63 20 69 6e 74 20 61 66 70 55  .static int afpU
d95f0 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
d9600 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
d9610 74 79 70 65 29 20 7b 0a 20 20 69 6e 74 20 72 63  type) {.  int rc
d9620 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d9630 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20  unixFile *pFile 
d9640 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b  = (unixFile*)id;
d9650 0a 20 20 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e  .  afpLockingCon
d9660 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 20 3d 20  text *context = 
d9670 28 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65  (afpLockingConte
d9680 78 74 20 2a 29 20 70 46 69 6c 65 2d 3e 6c 6f 63  xt *) pFile->loc
d9690 6b 69 6e 67 43 6f 6e 74 65 78 74 3b 0a 0a 20 20  kingContext;..  
d96a0 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
d96b0 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c  .  OSTRACE5("UNL
d96c0 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25  OCK  %d %d was %
d96d0 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69  d pid=%d\n", pFi
d96e0 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
d96f0 0a 20 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  .         pFile-
d9700 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69  >locktype, getpi
d9710 64 28 29 29 3b 0a 0a 20 20 61 73 73 65 72 74 28  d());..  assert(
d9720 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45   locktype<=SHARE
d9730 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 69 66 28 20  D_LOCK );.  if( 
d9740 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3c  pFile->locktype<
d9750 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
d9760 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
d9770 4b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 43 48 45  K;.  }.  if( CHE
d9780 43 4b 5f 54 48 52 45 41 44 49 44 28 70 46 69 6c  CK_THREADID(pFil
d9790 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  e) ){.    return
d97a0 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
d97b0 20 20 7d 0a 20 20 65 6e 74 65 72 4d 75 74 65 78    }.  enterMutex
d97c0 28 29 3b 0a 20 20 69 6e 74 20 66 61 69 6c 65 64  ();.  int failed
d97d0 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
d97e0 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
d97f0 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
d9800 29 7b 0a 20 20 20 20 69 66 28 20 6c 6f 63 6b 74  ){.    if( lockt
d9810 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b  ype==SHARED_LOCK
d9820 20 29 7b 0a 0a 20 20 20 20 20 20 2f 2a 20 75 6e   ){..      /* un
d9830 6c 6f 63 6b 20 74 68 65 20 65 78 63 6c 75 73 69  lock the exclusi
d9840 76 65 20 72 61 6e 67 65 20 2d 20 74 68 65 6e 20  ve range - then 
d9850 72 65 2d 65 73 74 61 62 6c 69 73 68 20 74 68 65  re-establish the
d9860 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a   shared lock */.
d9870 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d        if (pFile-
d9880 3e 6c 6f 63 6b 74 79 70 65 3d 3d 45 58 43 4c 55  >locktype==EXCLU
d9890 53 49 56 45 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20  SIVE_LOCK) {.   
d98a0 20 20 20 20 20 66 61 69 6c 65 64 20 3d 20 5f 41       failed = _A
d98b0 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
d98c0 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
d98d0 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52  File, SHARED_FIR
d98e0 53 54 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  ST, .           
d98f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9900 20 20 20 20 20 20 53 48 41 52 45 44 5f 53 49 5a        SHARED_SIZ
d9910 45 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69  E, 0);.        i
d9920 66 20 28 21 66 61 69 6c 65 64 29 20 7b 0a 20 20  f (!failed) {.  
d9930 20 20 20 20 20 20 20 20 2f 2a 20 73 75 63 63 65          /* succe
d9940 73 73 66 75 6c 6c 79 20 72 65 6d 6f 76 65 64 20  ssfully removed 
d9950 74 68 65 20 65 78 63 6c 75 73 69 76 65 20 6c 6f  the exclusive lo
d9960 63 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  ck */.          
d9970 69 66 20 28 28 66 61 69 6c 65 64 20 3d 20 5f 41  if ((failed = _A
d9980 46 50 46 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74  FPFSSetLock(cont
d9990 65 78 74 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70  ext->filePath, p
d99a0 46 69 6c 65 2c 20 53 48 41 52 45 44 5f 46 49 52  File, SHARED_FIR
d99b0 53 54 2b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ST+.            
d99c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d99d0 63 6f 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c  context->sharedL
d99e0 6f 63 6b 42 79 74 65 2c 20 31 2c 20 31 29 29 29  ockByte, 1, 1)))
d99f0 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f   {.            /
d9a00 2a 20 66 61 69 6c 65 64 20 74 6f 20 72 65 2d 65  * failed to re-e
d9a10 73 74 61 62 6c 69 73 68 20 6f 75 72 20 73 68 61  stablish our sha
d9a20 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20  red lock */.    
d9a30 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61 69          rc = fai
d9a40 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  led;.          }
d9a50 0a 20 20 20 20 20 20 20 20 7d 20 65 6c 73 65 20  .        } else 
d9a60 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  {.          rc =
d9a70 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 20   failed;.       
d9a80 20 7d 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   } .      }.    
d9a90 7d 0a 20 20 20 20 69 66 20 28 72 63 20 3d 3d 20  }.    if (rc == 
d9aa0 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 46 69  SQLITE_OK && pFi
d9ab0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3e 3d 50 45  le->locktype>=PE
d9ac0 4e 44 49 4e 47 5f 4c 4f 43 4b 29 20 7b 0a 20 20  NDING_LOCK) {.  
d9ad0 20 20 20 20 69 66 20 28 28 66 61 69 6c 65 64 20      if ((failed 
d9ae0 3d 20 5f 41 46 50 46 53 53 65 74 4c 6f 63 6b 28  = _AFPFSSetLock(
d9af0 63 6f 6e 74 65 78 74 2d 3e 66 69 6c 65 50 61 74  context->filePat
d9b00 68 2c 20 70 46 69 6c 65 2c 20 0a 20 20 20 20 20  h, pFile, .     
d9b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9b20 20 20 20 20 20 20 20 20 20 20 20 20 20 50 45 4e               PEN
d9b30 44 49 4e 47 5f 42 59 54 45 2c 20 31 2c 20 30 29  DING_BYTE, 1, 0)
d9b40 29 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 66  )){.        /* f
d9b50 61 69 6c 65 64 20 74 6f 20 72 65 6c 65 61 73 65  ailed to release
d9b60 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c 6f 63   the pending loc
d9b70 6b 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 63 20  k */.        rc 
d9b80 3d 20 66 61 69 6c 65 64 3b 20 0a 20 20 20 20 20  = failed; .     
d9b90 20 7d 0a 20 20 20 20 7d 20 0a 20 20 20 20 69 66   }.    } .    if
d9ba0 20 28 72 63 20 3d 3d 20 53 51 4c 49 54 45 5f 4f   (rc == SQLITE_O
d9bb0 4b 20 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b  K && pFile->lock
d9bc0 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44 5f 4c  type>=RESERVED_L
d9bd0 4f 43 4b 29 20 7b 0a 20 20 20 20 20 20 69 66 20  OCK) {.      if 
d9be0 28 28 66 61 69 6c 65 64 20 3d 20 5f 41 46 50 46  ((failed = _AFPF
d9bf0 53 53 65 74 4c 6f 63 6b 28 63 6f 6e 74 65 78 74  SSetLock(context
d9c00 2d 3e 66 69 6c 65 50 61 74 68 2c 20 70 46 69 6c  ->filePath, pFil
d9c10 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
d9c20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9c30 20 20 20 20 20 20 52 45 53 45 52 56 45 44 5f 42        RESERVED_B
d9c40 59 54 45 2c 20 31 2c 20 30 29 29 29 20 7b 0a 20  YTE, 1, 0))) {. 
d9c50 20 20 20 20 20 20 20 2f 2a 20 66 61 69 6c 65 64         /* failed
d9c60 20 74 6f 20 72 65 6c 65 61 73 65 20 74 68 65 20   to release the 
d9c70 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f  reserved lock */
d9c80 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 66 61  .        rc = fa
d9c90 69 6c 65 64 3b 20 20 0a 20 20 20 20 20 20 7d 0a  iled;  .      }.
d9ca0 20 20 20 20 7d 20 0a 20 20 7d 0a 20 20 69 66 28      } .  }.  if(
d9cb0 20 6c 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f   locktype==NO_LO
d9cc0 43 4b 20 29 7b 0a 20 20 20 20 69 6e 74 20 66 61  CK ){.    int fa
d9cd0 69 6c 65 64 20 3d 20 5f 41 46 50 46 53 53 65 74  iled = _AFPFSSet
d9ce0 4c 6f 63 6b 28 63 6f 6e 74 65 78 74 2d 3e 66 69  Lock(context->fi
d9cf0 6c 65 50 61 74 68 2c 20 70 46 69 6c 65 2c 20 0a  lePath, pFile, .
d9d00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
d9d10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53                 S
d9d20 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20 63 6f  HARED_FIRST + co
d9d30 6e 74 65 78 74 2d 3e 73 68 61 72 65 64 4c 6f 63  ntext->sharedLoc
d9d40 6b 42 79 74 65 2c 20 31 2c 20 30 29 3b 0a 20 20  kByte, 1, 0);.  
d9d50 20 20 69 66 20 28 66 61 69 6c 65 64 29 20 7b 0a    if (failed) {.
d9d60 20 20 20 20 20 20 72 63 20 3d 20 66 61 69 6c 65        rc = faile
d9d70 64 3b 20 20 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  d;  .    }.  }. 
d9d80 20 69 66 20 28 72 63 20 3d 3d 20 53 51 4c 49 54   if (rc == SQLIT
d9d90 45 5f 4f 4b 29 0a 20 20 20 20 70 46 69 6c 65 2d  E_OK).    pFile-
d9da0 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b  >locktype = lock
d9db0 74 79 70 65 3b 0a 20 20 6c 65 61 76 65 4d 75 74  type;.  leaveMut
d9dc0 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ex();.  return r
d9dd0 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73  c;.}../*.** Clos
d9de0 65 20 61 20 66 69 6c 65 20 26 20 63 6c 65 61 6e  e a file & clean
d9df0 75 70 20 41 46 50 20 73 70 65 63 69 66 69 63 20  up AFP specific 
d9e00 6c 6f 63 6b 69 6e 67 20 63 6f 6e 74 65 78 74 20  locking context 
d9e10 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61  .*/.static int a
d9e20 66 70 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  fpClose(sqlite3_
d9e30 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 66  file *id) {.  if
d9e40 28 20 69 64 20 29 7b 0a 20 20 20 20 75 6e 69 78  ( id ){.    unix
d9e50 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75  File *pFile = (u
d9e60 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20  nixFile*)id;.   
d9e70 20 61 66 70 55 6e 6c 6f 63 6b 28 69 64 2c 20 4e   afpUnlock(id, N
d9e80 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71 6c  O_LOCK);.    sql
d9e90 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65 2d  ite3_free(pFile-
d9ea0 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 29  >lockingContext)
d9eb0 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
d9ec0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
d9ed0 3b 0a 7d 0a 0a 0a 23 70 72 61 67 6d 61 20 6d 61  ;.}...#pragma ma
d9ee0 72 6b 20 66 6c 6f 63 6b 28 29 20 73 74 79 6c 65  rk flock() style
d9ef0 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 0a 2a 2a 20   locking../*.** 
d9f00 54 68 65 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  The flockLocking
d9f10 43 6f 6e 74 65 78 74 20 69 73 20 6e 6f 74 20 75  Context is not u
d9f20 73 65 64 0a 2a 2f 0a 74 79 70 65 64 65 66 20 76  sed.*/.typedef v
d9f30 6f 69 64 20 66 6c 6f 63 6b 4c 6f 63 6b 69 6e 67  oid flockLocking
d9f40 43 6f 6e 74 65 78 74 3b 0a 0a 2f 2a 20 66 6c 6f  Context;../* flo
d9f50 63 6b 2d 73 74 79 6c 65 20 72 65 73 65 72 76 65  ck-style reserve
d9f60 64 20 6c 6f 63 6b 20 63 68 65 63 6b 69 6e 67 20  d lock checking 
d9f70 66 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 20 62 65  following the be
d9f80 68 61 76 69 6f 72 20 6f 66 20 0a 20 2a 2a 20 75  havior of . ** u
d9f90 6e 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64  nixCheckReserved
d9fa0 4c 6f 63 6b 2c 20 73 65 65 20 74 68 65 20 75 6e  Lock, see the un
d9fb0 69 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c  ixCheckReservedL
d9fc0 6f 63 6b 20 66 75 6e 63 74 69 6f 6e 20 63 6f 6d  ock function com
d9fd0 6d 65 6e 74 73 20 2a 2f 0a 73 74 61 74 69 63 20  ments */.static 
d9fe0 69 6e 74 20 66 6c 6f 63 6b 43 68 65 63 6b 52 65  int flockCheckRe
d9ff0 73 65 72 76 65 64 4c 6f 63 6b 28 73 71 6c 69 74  servedLock(sqlit
da000 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
da010 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 69 6e   *pResOut){.  in
da020 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
da030 3b 0a 20 20 69 6e 74 20 72 65 73 65 72 76 65 64  ;.  int reserved
da040 20 3d 20 30 3b 0a 20 20 75 6e 69 78 46 69 6c 65   = 0;.  unixFile
da050 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78 46   *pFile = (unixF
da060 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20 53 69  ile*)id;.  .  Si
da070 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
da080 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
da090 52 52 5f 43 48 45 43 4b 52 45 53 45 52 56 45 44  RR_CHECKRESERVED
da0a0 4c 4f 43 4b 3b 20 29 3b 0a 20 20 0a 20 20 61 73  LOCK; );.  .  as
da0b0 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a 20  sert( pFile );. 
da0c0 20 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20   .  /* Check if 
da0d0 61 20 74 68 72 65 61 64 20 69 6e 20 74 68 69 73  a thread in this
da0e0 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20 73   process holds s
da0f0 75 63 68 20 61 20 6c 6f 63 6b 20 2a 2f 0a 20 20  uch a lock */.  
da100 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  if( pFile->lockt
da110 79 70 65 3e 53 48 41 52 45 44 5f 4c 4f 43 4b 20  ype>SHARED_LOCK 
da120 29 7b 0a 20 20 20 20 72 65 73 65 72 76 65 64 20  ){.    reserved 
da130 3d 20 31 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a  = 1;.  }.  .  /*
da140 20 4f 74 68 65 72 77 69 73 65 20 73 65 65 20 69   Otherwise see i
da150 66 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f  f some other pro
da160 63 65 73 73 20 68 6f 6c 64 73 20 69 74 2e 20 2a  cess holds it. *
da170 2f 0a 20 20 69 66 28 20 21 72 65 73 65 72 76 65  /.  if( !reserve
da180 64 20 29 7b 0a 20 20 20 20 2f 2a 20 61 74 74 65  d ){.    /* atte
da190 6d 70 74 20 74 6f 20 67 65 74 20 74 68 65 20 6c  mpt to get the l
da1a0 6f 63 6b 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6c  ock */.    int l
da1b0 72 63 20 3d 20 66 6c 6f 63 6b 28 70 46 69 6c 65  rc = flock(pFile
da1c0 2d 3e 68 2c 20 4c 4f 43 4b 5f 45 58 20 7c 20 4c  ->h, LOCK_EX | L
da1d0 4f 43 4b 5f 4e 42 29 3b 0a 20 20 20 20 69 66 28  OCK_NB);.    if(
da1e0 20 21 6c 72 63 20 29 7b 0a 20 20 20 20 20 20 2f   !lrc ){.      /
da1f0 2a 20 67 6f 74 20 74 68 65 20 6c 6f 63 6b 2c 20  * got the lock, 
da200 75 6e 6c 6f 63 6b 20 69 74 20 2a 2f 0a 20 20 20  unlock it */.   
da210 20 20 20 6c 72 63 20 3d 20 66 6c 6f 63 6b 28 70     lrc = flock(p
da220 46 69 6c 65 2d 3e 68 2c 20 4c 4f 43 4b 5f 55 4e  File->h, LOCK_UN
da230 29 3b 0a 20 20 20 20 20 20 69 66 20 28 20 6c 72  );.      if ( lr
da240 63 20 29 20 7b 0a 20 20 20 20 20 20 20 20 69 6e  c ) {.        in
da250 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f  t tErrno = errno
da260 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 75 6e 6c  ;.        /* unl
da270 6f 63 6b 20 66 61 69 6c 65 64 20 77 69 74 68 20  ock failed with 
da280 61 6e 20 65 72 72 6f 72 20 2a 2f 0a 20 20 20 20  an error */.    
da290 20 20 20 20 6c 72 63 20 3d 20 73 71 6c 69 74 65      lrc = sqlite
da2a0 45 72 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72  ErrorFromPosixEr
da2b0 72 6f 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49  ror(tErrno, SQLI
da2c0 54 45 5f 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29  TE_IOERR_UNLOCK)
da2d0 3b 20 0a 20 20 20 20 20 20 20 20 69 66 28 20 49  ; .        if( I
da2e0 53 5f 4c 4f 43 4b 5f 45 52 52 4f 52 28 6c 72 63  S_LOCK_ERROR(lrc
da2f0 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ) ){.          p
da300 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  File->lastErrno 
da310 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20 20 20  = tErrno;.      
da320 20 20 20 20 72 63 20 3d 20 6c 72 63 3b 0a 20 20      rc = lrc;.  
da330 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
da340 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
da350 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20     int tErrno = 
da360 65 72 72 6e 6f 3b 0a 20 20 20 20 20 20 72 65 73  errno;.      res
da370 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 20  erved = 1;.     
da380 20 2f 2a 20 73 6f 6d 65 6f 6e 65 20 65 6c 73 65   /* someone else
da390 20 6d 69 67 68 74 20 68 61 76 65 20 69 74 20 72   might have it r
da3a0 65 73 65 72 76 65 64 20 2a 2f 0a 20 20 20 20 20  eserved */.     
da3b0 20 6c 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72   lrc = sqliteErr
da3c0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
da3d0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
da3e0 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 20 0a 20 20  IOERR_LOCK); .  
da3f0 20 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f      if( IS_LOCK_
da400 45 52 52 4f 52 28 6c 72 63 29 20 29 7b 0a 20 20  ERROR(lrc) ){.  
da410 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 61 73        pFile->las
da420 74 45 72 72 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b  tErrno = tErrno;
da430 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c 72  .        rc = lr
da440 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  c;.      }.    }
da450 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45 34 28  .  }.  OSTRACE4(
da460 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20 25 64  "TEST WR-LOCK %d
da470 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65   %d %d\n", pFile
da480 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72 76 65  ->h, rc, reserve
da490 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75 74 20  d);..  *pResOut 
da4a0 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20 72 65  = reserved;.  re
da4b0 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74  turn rc;.}..stat
da4c0 69 63 20 69 6e 74 20 66 6c 6f 63 6b 4c 6f 63 6b  ic int flockLock
da4d0 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69  (sqlite3_file *i
da4e0 64 2c 20 69 6e 74 20 6c 6f 63 6b 74 79 70 65 29  d, int locktype)
da4f0 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51   {.  int rc = SQ
da500 4c 49 54 45 5f 4f 4b 3b 0a 20 20 75 6e 69 78 46  LITE_OK;.  unixF
da510 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
da520 69 78 46 69 6c 65 2a 29 69 64 3b 0a 0a 20 20 61  ixFile*)id;..  a
da530 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b 0a  ssert( pFile );.
da540 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65  .  /* if we alre
da550 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c  ady have a lock,
da560 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65   it is exclusive
da570 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64  .  .  ** Just ad
da580 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70  just level and p
da590 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72  unt on outta her
da5a0 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c  e. */.  if (pFil
da5b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
da5c0 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69  _LOCK) {.    pFi
da5d0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
da5e0 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74  ocktype;.    ret
da5f0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
da600 20 7d 0a 20 20 0a 20 20 2f 2a 20 67 72 61 62 20   }.  .  /* grab 
da610 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63  an exclusive loc
da620 6b 20 2a 2f 0a 20 20 0a 20 20 69 66 20 28 66 6c  k */.  .  if (fl
da630 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  ock(pFile->h, LO
da640 43 4b 5f 45 58 20 7c 20 4c 4f 43 4b 5f 4e 42 29  CK_EX | LOCK_NB)
da650 29 20 7b 0a 20 20 20 20 69 6e 74 20 74 45 72 72  ) {.    int tErr
da660 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20  no = errno;.    
da670 2f 2a 20 64 69 64 6e 27 74 20 67 65 74 2c 20 6d  /* didn't get, m
da680 75 73 74 20 62 65 20 62 75 73 79 20 2a 2f 0a 20  ust be busy */. 
da690 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72     rc = sqliteEr
da6a0 72 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f  rorFromPosixErro
da6b0 72 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45  r(tErrno, SQLITE
da6c0 5f 49 4f 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20  _IOERR_LOCK);.  
da6d0 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
da6e0 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
da6f0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
da700 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
da710 7d 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20  }.  } else {.   
da720 20 2f 2a 20 67 6f 74 20 69 74 2c 20 73 65 74 20   /* got it, set 
da730 74 68 65 20 74 79 70 65 20 61 6e 64 20 72 65 74  the type and ret
da740 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20 20 20 70 46  urn ok */.    pF
da750 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
da760 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 7d 0a 20 20  locktype;.  }.  
da770 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 20  OSTRACE4("LOCK  
da780 20 20 25 64 20 25 73 20 25 73 5c 6e 22 2c 20 70    %d %s %s\n", p
da790 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70  File->h, locktyp
da7a0 65 4e 61 6d 65 28 6c 6f 63 6b 74 79 70 65 29 2c  eName(locktype),
da7b0 20 0a 20 20 20 20 20 20 20 20 20 20 20 72 63 3d   .           rc=
da7c0 3d 53 51 4c 49 54 45 5f 4f 4b 20 3f 20 22 6f 6b  =SQLITE_OK ? "ok
da7d0 22 20 3a 20 22 66 61 69 6c 65 64 22 29 3b 0a 20  " : "failed");. 
da7e0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
da7f0 74 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 55  tatic int flockU
da800 6e 6c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69  nlock(sqlite3_fi
da810 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b  le *id, int lock
da820 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69  type) {.  unixFi
da830 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69  le *pFile = (uni
da840 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 0a 20 20  xFile*)id;.  .  
da850 61 73 73 65 72 74 28 20 70 46 69 6c 65 20 29 3b  assert( pFile );
da860 0a 20 20 4f 53 54 52 41 43 45 35 28 22 55 4e 4c  .  OSTRACE5("UNL
da870 4f 43 4b 20 20 25 64 20 25 64 20 77 61 73 20 25  OCK  %d %d was %
da880 64 20 70 69 64 3d 25 64 5c 6e 22 2c 20 70 46 69  d pid=%d\n", pFi
da890 6c 65 2d 3e 68 2c 20 6c 6f 63 6b 74 79 70 65 2c  le->h, locktype,
da8a0 0a 20 20 20 20 20 20 20 20 20 20 20 70 46 69 6c  .           pFil
da8b0 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74  e->locktype, get
da8c0 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74  pid());.  assert
da8d0 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52  ( locktype<=SHAR
da8e0 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20  ED_LOCK );.  .  
da8f0 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73  /* no-op if poss
da900 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46  ible */.  if( pF
da910 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c  ile->locktype==l
da920 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72  ocktype ){.    r
da930 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
da940 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61  .  }.  .  /* sha
da950 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20  red can just be 
da960 73 65 74 20 62 65 63 61 75 73 65 20 77 65 20 61  set because we a
da970 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20 65 78  lways have an ex
da980 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20  clusive */.  if 
da990 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45  (locktype==SHARE
da9a0 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46  D_LOCK) {.    pF
da9b0 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20  ile->locktype = 
da9c0 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65  locktype;.    re
da9d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
da9e0 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20    }.  .  /* no, 
da9f0 72 65 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20  really, unlock. 
daa00 2a 2f 0a 20 20 69 6e 74 20 72 63 20 3d 20 66 6c  */.  int rc = fl
daa10 6f 63 6b 28 70 46 69 6c 65 2d 3e 68 2c 20 4c 4f  ock(pFile->h, LO
daa20 43 4b 5f 55 4e 29 3b 0a 20 20 69 66 20 28 72 63  CK_UN);.  if (rc
daa30 29 20 7b 0a 20 20 20 20 69 6e 74 20 72 2c 20 74  ) {.    int r, t
daa40 45 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20  Errno = errno;. 
daa50 20 20 20 72 20 3d 20 73 71 6c 69 74 65 45 72 72     r = sqliteErr
daa60 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
daa70 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
daa80 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
daa90 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
daaa0 52 52 4f 52 28 72 29 20 29 7b 0a 20 20 20 20 20  RROR(r) ){.     
daab0 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
daac0 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
daad0 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 3b 0a  }.    return r;.
daae0 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 70    } else {.    p
daaf0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
dab00 20 4e 4f 5f 4c 4f 43 4b 3b 0a 20 20 20 20 72 65   NO_LOCK;.    re
dab10 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
dab20 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f    }.}../*.** Clo
dab30 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74  se a file..*/.st
dab40 61 74 69 63 20 69 6e 74 20 66 6c 6f 63 6b 43 6c  atic int flockCl
dab50 6f 73 65 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ose(sqlite3_file
dab60 20 2a 69 64 29 20 7b 0a 20 20 69 66 28 20 69 64   *id) {.  if( id
dab70 20 29 7b 0a 20 20 20 20 66 6c 6f 63 6b 55 6e 6c   ){.    flockUnl
dab80 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b 29  ock(id, NO_LOCK)
dab90 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 63  ;.  }.  return c
daba0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
dabb0 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  ;.}..#endif /* !
dabc0 49 53 5f 56 58 57 4f 52 4b 53 20 2a 2f 0a 0a 23  IS_VXWORKS */..#
dabd0 70 72 61 67 6d 61 20 6d 61 72 6b 20 4f 6c 64 2d  pragma mark Old-
dabe0 53 63 68 6f 6f 6c 20 2e 6c 6f 63 6b 20 66 69 6c  School .lock fil
dabf0 65 20 62 61 73 65 64 20 6c 6f 63 6b 69 6e 67 0a  e based locking.
dac00 0a 2f 2a 20 44 6f 74 6c 6f 63 6b 2d 73 74 79 6c  ./* Dotlock-styl
dac10 65 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20  e reserved lock 
dac20 63 68 65 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69  checking followi
dac30 6e 67 20 74 68 65 20 62 65 68 61 76 69 6f 72 20  ng the behavior 
dac40 6f 66 20 0a 2a 2a 20 75 6e 69 78 43 68 65 63 6b  of .** unixCheck
dac50 52 65 73 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65  ReservedLock, se
dac60 65 20 74 68 65 20 75 6e 69 78 43 68 65 63 6b 52  e the unixCheckR
dac70 65 73 65 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63  eservedLock func
dac80 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f  tion comments */
dac90 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
daca0 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76 65 64  ockCheckReserved
dacb0 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
dacc0 65 20 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73  e *id, int *pRes
dacd0 4f 75 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20  Out) {.  int rc 
dace0 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
dacf0 6e 74 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b  nt reserved = 0;
dad00 0a 20 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69  .  unixFile *pFi
dad10 6c 65 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29  le = (unixFile*)
dad20 69 64 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49  id;..  SimulateI
dad30 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53  OError( return S
dad40 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43  QLITE_IOERR_CHEC
dad50 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29  KRESERVEDLOCK; )
dad60 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 70  ;.  .  assert( p
dad70 46 69 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68  File );..  /* Ch
dad80 65 63 6b 20 69 66 20 61 20 74 68 72 65 61 64 20  eck if a thread 
dad90 69 6e 20 74 68 69 73 20 70 72 6f 63 65 73 73 20  in this process 
dada0 68 6f 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63  holds such a loc
dadb0 6b 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65  k */.  if( pFile
dadc0 2d 3e 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45  ->locktype>SHARE
dadd0 44 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65  D_LOCK ){.    re
dade0 73 65 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  served = 1;.  }.
dadf0 20 20 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73    .  /* Otherwis
dae00 65 20 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74  e see if some ot
dae10 68 65 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64  her process hold
dae20 73 20 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21  s it. */.  if( !
dae30 72 65 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20  reserved ){.    
dae40 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 20  char *zLockFile 
dae50 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65 2d  = (char *)pFile-
dae60 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 3b  >lockingContext;
dae70 0a 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74  .    struct stat
dae80 20 73 74 61 74 42 75 66 3b 0a 20 20 20 20 0a 20   statBuf;.    . 
dae90 20 20 20 69 66 28 20 6c 73 74 61 74 28 7a 4c 6f     if( lstat(zLo
daea0 63 6b 46 69 6c 65 2c 20 26 73 74 61 74 42 75 66  ckFile, &statBuf
daeb0 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  )==0 ){.      /*
daec0 20 66 69 6c 65 20 65 78 69 73 74 73 2c 20 73 6f   file exists, so
daed0 6d 65 6f 6e 65 20 65 6c 73 65 20 68 61 73 20 74  meone else has t
daee0 68 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20  he lock */.     
daef0 20 72 65 73 65 72 76 65 64 20 3d 20 31 3b 0a 20   reserved = 1;. 
daf00 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
daf10 2f 2a 20 66 69 6c 65 20 64 6f 65 73 20 6e 6f 74  /* file does not
daf20 20 65 78 69 73 74 2c 20 77 65 20 63 6f 75 6c 64   exist, we could
daf30 20 68 61 76 65 20 69 74 20 69 66 20 77 65 20 77   have it if we w
daf40 61 6e 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20  ant it */.      
daf50 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72 72  int tErrno = err
daf60 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45 4e  no;.      if( EN
daf70 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e 6f 20 29  OENT != tErrno )
daf80 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
daf90 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50 6f  qliteErrorFromPo
dafa0 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f 2c  sixError(tErrno,
dafb0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43 48   SQLITE_IOERR_CH
dafc0 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b 29  ECKRESERVEDLOCK)
dafd0 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65 2d  ;.        pFile-
dafe0 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45 72  >lastErrno = tEr
daff0 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rno;.      }.   
db000 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43 45   }.  }.  OSTRACE
db010 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  4("TEST WR-LOCK 
db020 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46 69  %d %d %d\n", pFi
db030 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65 72  le->h, rc, reser
db040 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f 75  ved);..  *pResOu
db050 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20 20  t = reserved;.  
db060 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
db070 61 74 69 63 20 69 6e 74 20 64 6f 74 6c 6f 63 6b  atic int dotlock
db080 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c  Lock(sqlite3_fil
db090 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63 6b 74  e *id, int lockt
db0a0 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46 69 6c  ype) {.  unixFil
db0b0 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
db0c0 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
db0d0 66 64 3b 0a 20 20 63 68 61 72 20 2a 7a 4c 6f 63  fd;.  char *zLoc
db0e0 6b 46 69 6c 65 20 3d 20 28 63 68 61 72 20 2a 29  kFile = (char *)
db0f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 69 6e 67 43 6f  pFile->lockingCo
db100 6e 74 65 78 74 3b 0a 20 20 69 6e 74 20 72 63 3d  ntext;.  int rc=
db110 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f 2a  SQLITE_OK;..  /*
db120 20 69 66 20 77 65 20 61 6c 72 65 61 64 79 20 68   if we already h
db130 61 76 65 20 61 20 6c 6f 63 6b 2c 20 69 74 20 69  ave a lock, it i
db140 73 20 65 78 63 6c 75 73 69 76 65 2e 20 20 0a 20  s exclusive.  . 
db150 20 2a 2a 20 4a 75 73 74 20 61 64 6a 75 73 74 20   ** Just adjust 
db160 6c 65 76 65 6c 20 61 6e 64 20 70 75 6e 74 20 6f  level and punt o
db170 6e 20 6f 75 74 74 61 20 68 65 72 65 2e 20 2a 2f  n outta here. */
db180 0a 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f  .  if (pFile->lo
db190 63 6b 74 79 70 65 20 3e 20 4e 4f 5f 4c 4f 43 4b  cktype > NO_LOCK
db1a0 29 20 7b 0a 20 20 20 20 70 46 69 6c 65 2d 3e 6c  ) {.    pFile->l
db1b0 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74 79  ocktype = lockty
db1c0 70 65 3b 0a 23 69 66 20 21 49 53 5f 56 58 57 4f  pe;.#if !IS_VXWO
db1d0 52 4b 53 0a 20 20 20 20 2f 2a 20 41 6c 77 61 79  RKS.    /* Alway
db1e0 73 20 75 70 64 61 74 65 20 74 68 65 20 74 69 6d  s update the tim
db1f0 65 73 74 61 6d 70 20 6f 6e 20 74 68 65 20 6f 6c  estamp on the ol
db200 64 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 75 74  d file */.    ut
db210 69 6d 65 73 28 7a 4c 6f 63 6b 46 69 6c 65 2c 20  imes(zLockFile, 
db220 4e 55 4c 4c 29 3b 0a 23 65 6e 64 69 66 0a 20 20  NULL);.#endif.  
db230 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
db240 3b 0a 20 20 20 20 67 6f 74 6f 20 64 6f 74 6c 6f  ;.    goto dotlo
db250 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d  ck_end_lock;.  }
db260 0a 20 20 0a 20 20 2f 2a 20 63 68 65 63 6b 20 74  .  .  /* check t
db270 6f 20 73 65 65 20 69 66 20 6c 6f 63 6b 20 66 69  o see if lock fi
db280 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69 73 74  le already exist
db290 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 74  s */.  struct st
db2a0 61 74 20 73 74 61 74 42 75 66 3b 0a 20 20 69 66  at statBuf;.  if
db2b0 20 28 6c 73 74 61 74 28 7a 4c 6f 63 6b 46 69 6c   (lstat(zLockFil
db2c0 65 2c 26 73 74 61 74 42 75 66 29 20 3d 3d 20 30  e,&statBuf) == 0
db2d0 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
db2e0 54 45 5f 42 55 53 59 3b 20 2f 2a 20 69 74 20 64  TE_BUSY; /* it d
db2f0 6f 65 73 2c 20 62 75 73 79 20 2a 2f 0a 20 20 20  oes, busy */.   
db300 20 67 6f 74 6f 20 64 6f 74 6c 6f 63 6b 5f 65 6e   goto dotlock_en
db310 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a 20  d_lock;.  }.  . 
db320 20 2f 2a 20 67 72 61 62 20 61 6e 20 65 78 63 6c   /* grab an excl
db330 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  usive lock */.  
db340 66 64 20 3d 20 6f 70 65 6e 28 7a 4c 6f 63 6b 46  fd = open(zLockF
db350 69 6c 65 2c 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 43  ile,O_RDONLY|O_C
db360 52 45 41 54 7c 4f 5f 45 58 43 4c 2c 30 36 30 30  REAT|O_EXCL,0600
db370 29 3b 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b  );.  if( fd<0 ){
db380 0a 20 20 20 20 2f 2a 20 66 61 69 6c 65 64 20 74  .    /* failed t
db390 6f 20 6f 70 65 6e 2f 63 72 65 61 74 65 20 74 68  o open/create th
db3a0 65 20 66 69 6c 65 2c 20 73 6f 6d 65 6f 6e 65 20  e file, someone 
db3b0 65 6c 73 65 20 6d 61 79 20 68 61 76 65 20 73 74  else may have st
db3c0 6f 6c 65 6e 20 74 68 65 20 6c 6f 63 6b 20 2a 2f  olen the lock */
db3d0 0a 20 20 20 20 69 6e 74 20 74 45 72 72 6e 6f 20  .    int tErrno 
db3e0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28  = errno;.    if(
db3f0 20 45 45 58 49 53 54 20 3d 3d 20 74 45 72 72 6e   EEXIST == tErrn
db400 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  o ){.      rc = 
db410 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20  SQLITE_BUSY;.   
db420 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20 20   } else {.      
db430 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72 6f 72  rc = sqliteError
db440 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72 28 74  FromPosixError(t
db450 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f 49 4f  Errno, SQLITE_IO
db460 45 52 52 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20  ERR_LOCK);.     
db470 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52 52   if( IS_LOCK_ERR
db480 4f 52 28 72 63 29 20 29 7b 0a 09 70 46 69 6c 65  OR(rc) ){..pFile
db490 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
db4a0 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rrno;.      }.  
db4b0 20 20 7d 0a 20 20 20 20 67 6f 74 6f 20 64 6f 74    }.    goto dot
db4c0 6c 6f 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20  lock_end_lock;. 
db4d0 20 7d 20 0a 20 20 63 6c 6f 73 65 28 66 64 29 3b   } .  close(fd);
db4e0 0a 20 20 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c  .  .  /* got it,
db4f0 20 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e   set the type an
db500 64 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20  d return ok */. 
db510 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65   pFile->locktype
db520 20 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 64   = locktype;.. d
db530 6f 74 6c 6f 63 6b 5f 65 6e 64 5f 6c 6f 63 6b 3a  otlock_end_lock:
db540 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
db550 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 74 6c  .static int dotl
db560 6f 63 6b 55 6e 6c 6f 63 6b 28 73 71 6c 69 74 65  ockUnlock(sqlite
db570 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20  3_file *id, int 
db580 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e  locktype) {.  un
db590 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20  ixFile *pFile = 
db5a0 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20  (unixFile*)id;. 
db5b0 20 63 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65   char *zLockFile
db5c0 20 3d 20 28 63 68 61 72 20 2a 29 70 46 69 6c 65   = (char *)pFile
db5d0 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
db5e0 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 46 69  ;..  assert( pFi
db5f0 6c 65 20 29 3b 0a 20 20 4f 53 54 52 41 43 45 35  le );.  OSTRACE5
db600 28 22 55 4e 4c 4f 43 4b 20 20 25 64 20 25 64 20  ("UNLOCK  %d %d 
db610 77 61 73 20 25 64 20 70 69 64 3d 25 64 5c 6e 22  was %d pid=%d\n"
db620 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 63 6b  , pFile->h, lock
db630 74 79 70 65 2c 0a 09 20 20 20 70 46 69 6c 65 2d  type,..   pFile-
db640 3e 6c 6f 63 6b 74 79 70 65 2c 20 67 65 74 70 69  >locktype, getpi
db650 64 28 29 29 3b 0a 20 20 61 73 73 65 72 74 28 20  d());.  assert( 
db660 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
db670 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a 20 20 2f 2a  _LOCK );.  .  /*
db680 20 6e 6f 2d 6f 70 20 69 66 20 70 6f 73 73 69 62   no-op if possib
db690 6c 65 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c  le */.  if( pFil
db6a0 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 6c 6f 63  e->locktype==loc
db6b0 6b 74 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74  ktype ){.    ret
db6c0 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
db6d0 20 7d 0a 20 20 0a 20 20 2f 2a 20 73 68 61 72 65   }.  .  /* share
db6e0 64 20 63 61 6e 20 6a 75 73 74 20 62 65 20 73 65  d can just be se
db6f0 74 20 62 65 63 61 75 73 65 20 77 65 20 61 6c 77  t because we alw
db700 61 79 73 20 68 61 76 65 20 61 6e 20 65 78 63 6c  ays have an excl
db710 75 73 69 76 65 20 2a 2f 0a 20 20 69 66 20 28 6c  usive */.  if (l
db720 6f 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f  ocktype==SHARED_
db730 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69 6c  LOCK) {.    pFil
db740 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f  e->locktype = lo
db750 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 65 74 75  cktype;.    retu
db760 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
db770 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f 2c 20 72 65  }.  .  /* no, re
db780 61 6c 6c 79 2c 20 75 6e 6c 6f 63 6b 2e 20 2a 2f  ally, unlock. */
db790 0a 20 20 69 66 20 28 75 6e 6c 69 6e 6b 28 7a 4c  .  if (unlink(zL
db7a0 6f 63 6b 46 69 6c 65 29 20 29 20 7b 0a 20 20 20  ockFile) ) {.   
db7b0 20 69 6e 74 20 72 63 2c 20 74 45 72 72 6e 6f 20   int rc, tErrno 
db7c0 3d 20 65 72 72 6e 6f 3b 0a 20 20 20 20 69 66 28  = errno;.    if(
db7d0 20 45 4e 4f 45 4e 54 20 21 3d 20 74 45 72 72 6e   ENOENT != tErrn
db7e0 6f 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  o ){.      rc = 
db7f0 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
db800 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
db810 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 55  , SQLITE_IOERR_U
db820 4e 4c 4f 43 4b 29 3b 0a 20 20 20 20 7d 0a 20 20  NLOCK);.    }.  
db830 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45 52    if( IS_LOCK_ER
db840 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20 20  ROR(rc) ){.     
db850 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72 6e   pFile->lastErrn
db860 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20 20  o = tErrno;.    
db870 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  }.    return rc;
db880 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e 6c   .  }.  pFile->l
db890 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f 43  ocktype = NO_LOC
db8a0 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  K;.  return SQLI
db8b0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a 2a  TE_OK;.}../*. **
db8c0 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 20   Close a file.. 
db8d0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f  */.static int do
db8e0 74 6c 6f 63 6b 43 6c 6f 73 65 28 73 71 6c 69 74  tlockClose(sqlit
db8f0 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b 0a 20  e3_file *id) {. 
db900 20 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 69   int rc;.  if( i
db910 64 20 29 7b 0a 20 20 20 20 75 6e 69 78 46 69 6c  d ){.    unixFil
db920 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e 69 78  e *pFile = (unix
db930 46 69 6c 65 2a 29 69 64 3b 0a 20 20 20 20 64 6f  File*)id;.    do
db940 74 6c 6f 63 6b 55 6e 6c 6f 63 6b 28 69 64 2c 20  tlockUnlock(id, 
db950 4e 4f 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 73 71  NO_LOCK);.    sq
db960 6c 69 74 65 33 5f 66 72 65 65 28 70 46 69 6c 65  lite3_free(pFile
db970 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  ->lockingContext
db980 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 49 53 5f  );.  }.  if( IS_
db990 56 58 57 4f 52 4b 53 20 29 20 65 6e 74 65 72 4d  VXWORKS ) enterM
db9a0 75 74 65 78 28 29 3b 0a 20 20 72 63 20 3d 20 63  utex();.  rc = c
db9b0 6c 6f 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29  loseUnixFile(id)
db9c0 3b 0a 20 20 69 66 28 20 49 53 5f 56 58 57 4f 52  ;.  if( IS_VXWOR
db9d0 4b 53 20 29 20 6c 65 61 76 65 4d 75 74 65 78 28  KS ) leaveMutex(
db9e0 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
db9f0 7d 0a 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b  }..#if IS_VXWORK
dba00 53 0a 0a 23 70 72 61 67 6d 61 20 6d 61 72 6b 20  S..#pragma mark 
dba10 50 4f 53 49 58 2f 76 78 57 6f 72 6b 73 20 6e 61  POSIX/vxWorks na
dba20 6d 65 64 20 73 65 6d 61 70 68 6f 72 65 20 62 61  med semaphore ba
dba30 73 65 64 20 6c 6f 63 6b 69 6e 67 0a 0a 2f 2a 20  sed locking../* 
dba40 4e 61 6d 65 64 73 65 6d 2d 73 74 79 6c 65 20 72  Namedsem-style r
dba50 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 63 68 65  eserved lock che
dba60 63 6b 69 6e 67 20 66 6f 6c 6c 6f 77 69 6e 67 20  cking following 
dba70 74 68 65 20 62 65 68 61 76 69 6f 72 20 6f 66 20  the behavior of 
dba80 0a 2a 2a 20 75 6e 69 78 43 68 65 63 6b 52 65 73  .** unixCheckRes
dba90 65 72 76 65 64 4c 6f 63 6b 2c 20 73 65 65 20 74  ervedLock, see t
dbaa0 68 65 20 75 6e 69 78 43 68 65 63 6b 52 65 73 65  he unixCheckRese
dbab0 72 76 65 64 4c 6f 63 6b 20 66 75 6e 63 74 69 6f  rvedLock functio
dbac0 6e 20 63 6f 6d 6d 65 6e 74 73 20 2a 2f 0a 73 74  n comments */.st
dbad0 61 74 69 63 20 69 6e 74 20 6e 61 6d 65 64 73 65  atic int namedse
dbae0 6d 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  mCheckReservedLo
dbaf0 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ck(sqlite3_file 
dbb00 2a 69 64 2c 20 69 6e 74 20 2a 70 52 65 73 4f 75  *id, int *pResOu
dbb10 74 29 20 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t) {.  int rc = 
dbb20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74  SQLITE_OK;.  int
dbb30 20 72 65 73 65 72 76 65 64 20 3d 20 30 3b 0a 20   reserved = 0;. 
dbb40 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
dbb50 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
dbb60 3b 0a 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45  ;..  SimulateIOE
dbb70 72 72 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c  rror( return SQL
dbb80 49 54 45 5f 49 4f 45 52 52 5f 43 48 45 43 4b 52  ITE_IOERR_CHECKR
dbb90 45 53 45 52 56 45 44 4c 4f 43 4b 3b 20 29 3b 0a  ESERVEDLOCK; );.
dbba0 20 20 0a 20 20 61 73 73 65 72 74 28 20 70 46 69    .  assert( pFi
dbbb0 6c 65 20 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  le );..  /* Chec
dbbc0 6b 20 69 66 20 61 20 74 68 72 65 61 64 20 69 6e  k if a thread in
dbbd0 20 74 68 69 73 20 70 72 6f 63 65 73 73 20 68 6f   this process ho
dbbe0 6c 64 73 20 73 75 63 68 20 61 20 6c 6f 63 6b 20  lds such a lock 
dbbf0 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e  */.  if( pFile->
dbc00 6c 6f 63 6b 74 79 70 65 3e 53 48 41 52 45 44 5f  locktype>SHARED_
dbc10 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 65 73 65  LOCK ){.    rese
dbc20 72 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  rved = 1;.  }.  
dbc30 0a 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65 20  .  /* Otherwise 
dbc40 73 65 65 20 69 66 20 73 6f 6d 65 20 6f 74 68 65  see if some othe
dbc50 72 20 70 72 6f 63 65 73 73 20 68 6f 6c 64 73 20  r process holds 
dbc60 69 74 2e 20 2a 2f 0a 20 20 69 66 28 20 21 72 65  it. */.  if( !re
dbc70 73 65 72 76 65 64 20 29 7b 0a 20 20 20 20 73 65  served ){.    se
dbc80 6d 5f 74 20 2a 70 53 65 6d 20 3d 20 70 46 69 6c  m_t *pSem = pFil
dbc90 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3b 0a  e->pOpen->pSem;.
dbca0 20 20 20 20 73 74 72 75 63 74 20 73 74 61 74 20      struct stat 
dbcb0 73 74 61 74 42 75 66 3b 0a 0a 20 20 20 20 69 66  statBuf;..    if
dbcc0 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70 53  ( sem_trywait(pS
dbcd0 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 20  em)==-1 ){.     
dbce0 20 69 6e 74 20 74 45 72 72 6e 6f 20 3d 20 65 72   int tErrno = er
dbcf0 72 6e 6f 3b 0a 20 20 20 20 20 20 69 66 28 20 45  rno;.      if( E
dbd00 41 47 41 49 4e 20 21 3d 20 74 45 72 72 6e 6f 20  AGAIN != tErrno 
dbd10 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
dbd20 73 71 6c 69 74 65 45 72 72 6f 72 46 72 6f 6d 50  sqliteErrorFromP
dbd30 6f 73 69 78 45 72 72 6f 72 28 74 45 72 72 6e 6f  osixError(tErrno
dbd40 2c 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 43  , SQLITE_IOERR_C
dbd50 48 45 43 4b 52 45 53 45 52 56 45 44 4c 4f 43 4b  HECKRESERVEDLOCK
dbd60 29 3b 0a 20 20 20 20 20 20 20 20 70 46 69 6c 65  );.        pFile
dbd70 2d 3e 6c 61 73 74 45 72 72 6e 6f 20 3d 20 74 45  ->lastErrno = tE
dbd80 72 72 6e 6f 3b 0a 20 20 20 20 20 20 7d 20 65 6c  rrno;.      } el
dbd90 73 65 20 7b 0a 09 2f 2a 20 73 6f 6d 65 6f 6e 65  se {../* someone
dbda0 20 65 6c 73 65 20 68 61 73 20 74 68 65 20 6c 6f   else has the lo
dbdb0 63 6b 20 77 68 65 6e 20 77 65 20 61 72 65 20 69  ck when we are i
dbdc0 6e 20 4e 4f 5f 4c 4f 43 4b 20 2a 2f 0a 09 72 65  n NO_LOCK */..re
dbdd0 73 65 72 76 65 64 20 3d 20 28 70 46 69 6c 65 2d  served = (pFile-
dbde0 3e 6c 6f 63 6b 74 79 70 65 20 3c 20 53 48 41 52  >locktype < SHAR
dbdf0 45 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20  ED_LOCK);.      
dbe00 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  }.    }else{.   
dbe10 20 20 20 2f 2a 20 77 65 20 63 6f 75 6c 64 20 68     /* we could h
dbe20 61 76 65 20 69 74 20 69 66 20 77 65 20 77 61 6e  ave it if we wan
dbe30 74 20 69 74 20 2a 2f 0a 20 20 20 20 20 20 73 65  t it */.      se
dbe40 6d 5f 70 6f 73 74 28 70 53 65 6d 29 3b 0a 20 20  m_post(pSem);.  
dbe50 20 20 7d 0a 20 20 7d 0a 20 20 4f 53 54 52 41 43    }.  }.  OSTRAC
dbe60 45 34 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b  E4("TEST WR-LOCK
dbe70 20 25 64 20 25 64 20 25 64 5c 6e 22 2c 20 70 46   %d %d %d\n", pF
dbe80 69 6c 65 2d 3e 68 2c 20 72 63 2c 20 72 65 73 65  ile->h, rc, rese
dbe90 72 76 65 64 29 3b 0a 0a 20 20 2a 70 52 65 73 4f  rved);..  *pResO
dbea0 75 74 20 3d 20 72 65 73 65 72 76 65 64 3b 0a 20  ut = reserved;. 
dbeb0 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73   return rc;.}..s
dbec0 74 61 74 69 63 20 69 6e 74 20 6e 61 6d 65 64 73  tatic int nameds
dbed0 65 6d 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  emLock(sqlite3_f
dbee0 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c 6f 63  ile *id, int loc
dbef0 6b 74 79 70 65 29 20 7b 0a 20 20 75 6e 69 78 46  ktype) {.  unixF
dbf00 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 75 6e  ile *pFile = (un
dbf10 69 78 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e  ixFile*)id;.  in
dbf20 74 20 66 64 3b 0a 20 20 73 65 6d 5f 74 20 2a 70  t fd;.  sem_t *p
dbf30 53 65 6d 20 3d 20 70 46 69 6c 65 2d 3e 70 4f 70  Sem = pFile->pOp
dbf40 65 6e 2d 3e 70 53 65 6d 3b 0a 20 20 69 6e 74 20  en->pSem;.  int 
dbf50 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
dbf60 0a 20 20 2f 2a 20 69 66 20 77 65 20 61 6c 72 65  .  /* if we alre
dbf70 61 64 79 20 68 61 76 65 20 61 20 6c 6f 63 6b 2c  ady have a lock,
dbf80 20 69 74 20 69 73 20 65 78 63 6c 75 73 69 76 65   it is exclusive
dbf90 2e 20 20 0a 20 20 2a 2a 20 4a 75 73 74 20 61 64  .  .  ** Just ad
dbfa0 6a 75 73 74 20 6c 65 76 65 6c 20 61 6e 64 20 70  just level and p
dbfb0 75 6e 74 20 6f 6e 20 6f 75 74 74 61 20 68 65 72  unt on outta her
dbfc0 65 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c  e. */.  if (pFil
dbfd0 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3e 20 4e 4f  e->locktype > NO
dbfe0 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20 70 46 69  _LOCK) {.    pFi
dbff0 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d 20 6c  le->locktype = l
dc000 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 72 63 20  ocktype;.    rc 
dc010 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
dc020 20 67 6f 74 6f 20 6e 61 6d 65 64 73 65 6d 5f 65   goto namedsem_e
dc030 6e 64 5f 6c 6f 63 6b 3b 0a 20 20 7d 0a 20 20 0a  nd_lock;.  }.  .
dc040 20 20 2f 2a 20 6c 6f 63 6b 20 73 65 6d 61 70 68    /* lock semaph
dc050 6f 72 65 20 6e 6f 77 20 62 75 74 20 62 61 69 6c  ore now but bail
dc060 20 6f 75 74 20 77 68 65 6e 20 61 6c 72 65 61 64   out when alread
dc070 79 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 69  y locked. */.  i
dc080 66 28 20 73 65 6d 5f 74 72 79 77 61 69 74 28 70  f( sem_trywait(p
dc090 53 65 6d 29 3d 3d 2d 31 20 29 7b 0a 20 20 20 20  Sem)==-1 ){.    
dc0a0 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59  rc = SQLITE_BUSY
dc0b0 3b 0a 20 20 20 20 67 6f 74 6f 20 6e 61 6d 65 64  ;.    goto named
dc0c0 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3b 0a 20 20  sem_end_lock;.  
dc0d0 7d 0a 0a 20 20 2f 2a 20 67 6f 74 20 69 74 2c 20  }..  /* got it, 
dc0e0 73 65 74 20 74 68 65 20 74 79 70 65 20 61 6e 64  set the type and
dc0f0 20 72 65 74 75 72 6e 20 6f 6b 20 2a 2f 0a 20 20   return ok */.  
dc100 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
dc110 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 0a 20 6e 61  = locktype;.. na
dc120 6d 65 64 73 65 6d 5f 65 6e 64 5f 6c 6f 63 6b 3a  medsem_end_lock:
dc130 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
dc140 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 61 6d 65  .static int name
dc150 64 73 65 6d 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  dsemUnlock(sqlit
dc160 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74  e3_file *id, int
dc170 20 6c 6f 63 6b 74 79 70 65 29 20 7b 0a 20 20 75   locktype) {.  u
dc180 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d  nixFile *pFile =
dc190 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64 3b 0a   (unixFile*)id;.
dc1a0 20 20 73 65 6d 5f 74 20 2a 70 53 65 6d 20 3d 20    sem_t *pSem = 
dc1b0 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 2d 3e 70 53  pFile->pOpen->pS
dc1c0 65 6d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  em;..  assert( p
dc1d0 46 69 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74  File );.  assert
dc1e0 28 20 70 53 65 6d 20 29 3b 0a 20 20 4f 53 54 52  ( pSem );.  OSTR
dc1f0 41 43 45 35 28 22 55 4e 4c 4f 43 4b 20 20 25 64  ACE5("UNLOCK  %d
dc200 20 25 64 20 77 61 73 20 25 64 20 70 69 64 3d 25   %d was %d pid=%
dc210 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
dc220 6c 6f 63 6b 74 79 70 65 2c 0a 09 20 20 20 70 46  locktype,..   pF
dc230 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 67  ile->locktype, g
dc240 65 74 70 69 64 28 29 29 3b 0a 20 20 61 73 73 65  etpid());.  asse
dc250 72 74 28 20 6c 6f 63 6b 74 79 70 65 3c 3d 53 48  rt( locktype<=SH
dc260 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 0a  ARED_LOCK );.  .
dc270 20 20 2f 2a 20 6e 6f 2d 6f 70 20 69 66 20 70 6f    /* no-op if po
dc280 73 73 69 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20  ssible */.  if( 
dc290 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d  pFile->locktype=
dc2a0 3d 6c 6f 63 6b 74 79 70 65 20 29 7b 0a 20 20 20  =locktype ){.   
dc2b0 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
dc2c0 4b 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 73  K;.  }.  .  /* s
dc2d0 68 61 72 65 64 20 63 61 6e 20 6a 75 73 74 20 62  hared can just b
dc2e0 65 20 73 65 74 20 62 65 63 61 75 73 65 20 77 65  e set because we
dc2f0 20 61 6c 77 61 79 73 20 68 61 76 65 20 61 6e 20   always have an 
dc300 65 78 63 6c 75 73 69 76 65 20 2a 2f 0a 20 20 69  exclusive */.  i
dc310 66 20 28 6c 6f 63 6b 74 79 70 65 3d 3d 53 48 41  f (locktype==SHA
dc320 52 45 44 5f 4c 4f 43 4b 29 20 7b 0a 20 20 20 20  RED_LOCK) {.    
dc330 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20  pFile->locktype 
dc340 3d 20 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20  = locktype;.    
dc350 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
dc360 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 6e 6f  ;.  }.  .  /* no
dc370 2c 20 72 65 61 6c 6c 79 20 75 6e 6c 6f 63 6b 2e  , really unlock.
dc380 20 2a 2f 0a 20 20 69 66 20 28 20 73 65 6d 5f 70   */.  if ( sem_p
dc390 6f 73 74 28 70 53 65 6d 29 3d 3d 2d 31 20 29 20  ost(pSem)==-1 ) 
dc3a0 7b 0a 20 20 20 20 69 6e 74 20 72 63 2c 20 74 45  {.    int rc, tE
dc3b0 72 72 6e 6f 20 3d 20 65 72 72 6e 6f 3b 0a 20 20  rrno = errno;.  
dc3c0 20 20 72 63 20 3d 20 73 71 6c 69 74 65 45 72 72    rc = sqliteErr
dc3d0 6f 72 46 72 6f 6d 50 6f 73 69 78 45 72 72 6f 72  orFromPosixError
dc3e0 28 74 45 72 72 6e 6f 2c 20 53 51 4c 49 54 45 5f  (tErrno, SQLITE_
dc3f0 49 4f 45 52 52 5f 55 4e 4c 4f 43 4b 29 3b 0a 20  IOERR_UNLOCK);. 
dc400 20 20 20 69 66 28 20 49 53 5f 4c 4f 43 4b 5f 45     if( IS_LOCK_E
dc410 52 52 4f 52 28 72 63 29 20 29 7b 0a 20 20 20 20  RROR(rc) ){.    
dc420 20 20 70 46 69 6c 65 2d 3e 6c 61 73 74 45 72 72    pFile->lastErr
dc430 6e 6f 20 3d 20 74 45 72 72 6e 6f 3b 0a 20 20 20  no = tErrno;.   
dc440 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63   }.    return rc
dc450 3b 20 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  ; .  }.  pFile->
dc460 6c 6f 63 6b 74 79 70 65 20 3d 20 4e 4f 5f 4c 4f  locktype = NO_LO
dc470 43 4b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  CK;.  return SQL
dc480 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 20 2a  ITE_OK;.}../*. *
dc490 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a  * Close a file..
dc4a0 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e   */.static int n
dc4b0 61 6d 65 64 73 65 6d 43 6c 6f 73 65 28 73 71 6c  amedsemClose(sql
dc4c0 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 29 20 7b  ite3_file *id) {
dc4d0 0a 20 20 69 66 28 20 69 64 20 29 7b 0a 20 20 20  .  if( id ){.   
dc4e0 20 75 6e 69 78 46 69 6c 65 20 2a 70 46 69 6c 65   unixFile *pFile
dc4f0 20 3d 20 28 75 6e 69 78 46 69 6c 65 2a 29 69 64   = (unixFile*)id
dc500 3b 0a 20 20 20 20 6e 61 6d 65 64 73 65 6d 55 6e  ;.    namedsemUn
dc510 6c 6f 63 6b 28 69 64 2c 20 4e 4f 5f 4c 4f 43 4b  lock(id, NO_LOCK
dc520 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
dc530 46 69 6c 65 20 29 3b 0a 20 20 20 20 65 6e 74 65  File );.    ente
dc540 72 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 72 65  rMutex();.    re
dc550 6c 65 61 73 65 4c 6f 63 6b 49 6e 66 6f 28 70 46  leaseLockInfo(pF
dc560 69 6c 65 2d 3e 70 4c 6f 63 6b 29 3b 0a 20 20 20  ile->pLock);.   
dc570 20 72 65 6c 65 61 73 65 4f 70 65 6e 43 6e 74 28   releaseOpenCnt(
dc580 70 46 69 6c 65 2d 3e 70 4f 70 65 6e 29 3b 0a 20  pFile->pOpen);. 
dc590 20 20 20 63 6c 6f 73 65 55 6e 69 78 46 69 6c 65     closeUnixFile
dc5a0 28 69 64 29 3b 0a 20 20 20 20 6c 65 61 76 65 4d  (id);.    leaveM
dc5b0 75 74 65 78 28 29 3b 0a 20 20 7d 0a 20 20 72 65  utex();.  }.  re
dc5c0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
dc5d0 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 49 53 5f  }..#endif /* IS_
dc5e0 56 58 57 4f 52 4b 53 20 2a 2f 0a 0a 23 65 6e 64  VXWORKS */..#end
dc5f0 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 45 4e 41  if /* SQLITE_ENA
dc600 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c  BLE_LOCKING_STYL
dc610 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  E */../*.** The 
dc620 6e 6f 6c 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e  nolockLockingCon
dc630 74 65 78 74 20 69 73 20 76 6f 69 64 0a 2a 2f 0a  text is void.*/.
dc640 74 79 70 65 64 65 66 20 76 6f 69 64 20 6e 6f 6c  typedef void nol
dc650 6f 63 6b 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  ockLockingContex
dc660 74 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6e  t;..static int n
dc670 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76  olockCheckReserv
dc680 65 64 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  edLock(sqlite3_f
dc690 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  ile *NotUsed, in
dc6a0 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a 20 20 55  t *pResOut){.  U
dc6b0 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
dc6c0 4e 6f 74 55 73 65 64 29 3b 0a 20 20 2a 70 52 65  NotUsed);.  *pRe
dc6d0 73 4f 75 74 20 3d 20 30 3b 0a 20 20 72 65 74 75  sOut = 0;.  retu
dc6e0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
dc6f0 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f  .static int nolo
dc700 63 6b 4c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66  ckLock(sqlite3_f
dc710 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e  ile *NotUsed, in
dc720 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55  t NotUsed2){.  U
dc730 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
dc740 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
dc750 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  d2);.  return SQ
dc760 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74  LITE_OK;.}..stat
dc770 69 63 20 69 6e 74 20 6e 6f 6c 6f 63 6b 55 6e 6c  ic int nolockUnl
dc780 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
dc790 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e   *NotUsed, int N
dc7a0 6f 74 55 73 65 64 32 29 7b 0a 20 20 55 4e 55 53  otUsed2){.  UNUS
dc7b0 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e 6f  ED_PARAMETER2(No
dc7c0 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32 29  tUsed, NotUsed2)
dc7d0 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
dc7e0 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
dc7f0 6c 6f 73 65 20 61 20 66 69 6c 65 2e 0a 2a 2f 0a  lose a file..*/.
dc800 73 74 61 74 69 63 20 69 6e 74 20 6e 6f 6c 6f 63  static int noloc
dc810 6b 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f 66  kClose(sqlite3_f
dc820 69 6c 65 20 2a 69 64 29 20 7b 0a 20 20 69 6e 74  ile *id) {.  int
dc830 20 72 63 3b 0a 20 20 69 66 28 20 49 53 5f 56 58   rc;.  if( IS_VX
dc840 57 4f 52 4b 53 20 29 20 65 6e 74 65 72 4d 75 74  WORKS ) enterMut
dc850 65 78 28 29 3b 0a 20 20 72 63 20 3d 20 63 6c 6f  ex();.  rc = clo
dc860 73 65 55 6e 69 78 46 69 6c 65 28 69 64 29 3b 0a  seUnixFile(id);.
dc870 20 20 69 66 28 20 49 53 5f 56 58 57 4f 52 4b 53    if( IS_VXWORKS
dc880 20 29 20 6c 65 61 76 65 4d 75 74 65 78 28 29 3b   ) leaveMutex();
dc890 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
dc8a0 0a 0a 2f 2a 0a 2a 2a 20 49 6e 66 6f 72 6d 61 74  ../*.** Informat
dc8b0 69 6f 6e 20 61 6e 64 20 63 6f 6e 74 72 6f 6c 20  ion and control 
dc8c0 6f 66 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20  of an open file 
dc8d0 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  handle..*/.stati
dc8e0 63 20 69 6e 74 20 75 6e 69 78 46 69 6c 65 43 6f  c int unixFileCo
dc8f0 6e 74 72 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69  ntrol(sqlite3_fi
dc900 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20  le *id, int op, 
dc910 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a 20 20 73  void *pArg){.  s
dc920 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20  witch( op ){.   
dc930 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46 43 4e   case SQLITE_FCN
dc940 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a  TL_LOCKSTATE: {.
dc950 20 20 20 20 20 20 2a 28 69 6e 74 2a 29 70 41 72        *(int*)pAr
dc960 67 20 3d 20 28 28 75 6e 69 78 46 69 6c 65 2a 29  g = ((unixFile*)
dc970 69 64 29 2d 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20  id)->locktype;. 
dc980 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
dc990 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
dc9a0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
dc9b0 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _ERROR;.}../*.**
dc9c0 20 52 65 74 75 72 6e 20 74 68 65 20 73 65 63 74   Return the sect
dc9d0 6f 72 20 73 69 7a 65 20 69 6e 20 62 79 74 65 73  or size in bytes
dc9e0 20 6f 66 20 74 68 65 20 75 6e 64 65 72 6c 79 69   of the underlyi
dc9f0 6e 67 20 62 6c 6f 63 6b 20 64 65 76 69 63 65 20  ng block device 
dca00 66 6f 72 0a 2a 2a 20 74 68 65 20 73 70 65 63 69  for.** the speci
dca10 66 69 65 64 20 66 69 6c 65 2e 20 54 68 69 73 20  fied file. This 
dca20 69 73 20 61 6c 6d 6f 73 74 20 61 6c 77 61 79 73  is almost always
dca30 20 35 31 32 20 62 79 74 65 73 2c 20 62 75 74 20   512 bytes, but 
dca40 6d 61 79 20 62 65 0a 2a 2a 20 6c 61 72 67 65 72  may be.** larger
dca50 20 66 6f 72 20 73 6f 6d 65 20 64 65 76 69 63 65   for some device
dca60 73 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  s..**.** SQLite 
dca70 63 6f 64 65 20 61 73 73 75 6d 65 73 20 74 68 69  code assumes thi
dca80 73 20 66 75 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f  s function canno
dca90 74 20 66 61 69 6c 2e 20 49 74 20 61 6c 73 6f 20  t fail. It also 
dcaa0 61 73 73 75 6d 65 73 20 74 68 61 74 0a 2a 2a 20  assumes that.** 
dcab0 69 66 20 74 77 6f 20 66 69 6c 65 73 20 61 72 65  if two files are
dcac0 20 63 72 65 61 74 65 64 20 69 6e 20 74 68 65 20   created in the 
dcad0 73 61 6d 65 20 66 69 6c 65 2d 73 79 73 74 65 6d  same file-system
dcae0 20 64 69 72 65 63 74 6f 72 79 20 28 69 2e 65 2e   directory (i.e.
dcaf0 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20 61  .** a database a
dcb00 6e 64 20 69 74 73 20 6a 6f 75 72 6e 61 6c 20 66  nd its journal f
dcb10 69 6c 65 29 20 74 68 61 74 20 74 68 65 20 73 65  ile) that the se
dcb20 63 74 6f 72 20 73 69 7a 65 20 77 69 6c 6c 20 62  ctor size will b
dcb30 65 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 66 6f  e the.** same fo
dcb40 72 20 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69  r both..*/.stati
dcb50 63 20 69 6e 74 20 75 6e 69 78 53 65 63 74 6f 72  c int unixSector
dcb60 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  Size(sqlite3_fil
dcb70 65 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 55  e *NotUsed){.  U
dcb80 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
dcb90 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65 74 75  NotUsed);.  retu
dcba0 72 6e 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c  rn SQLITE_DEFAUL
dcbb0 54 5f 53 45 43 54 4f 52 5f 53 49 5a 45 3b 0a 7d  T_SECTOR_SIZE;.}
dcbc0 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
dcbd0 68 65 20 64 65 76 69 63 65 20 63 68 61 72 61 63  he device charac
dcbe0 74 65 72 69 73 74 69 63 73 20 66 6f 72 20 74 68  teristics for th
dcbf0 65 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20  e file. This is 
dcc00 61 6c 77 61 79 73 20 30 20 66 6f 72 20 75 6e 69  always 0 for uni
dcc10 78 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  x..*/.static int
dcc20 20 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61   unixDeviceChara
dcc30 63 74 65 72 69 73 74 69 63 73 28 73 71 6c 69 74  cteristics(sqlit
dcc40 65 33 5f 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64  e3_file *NotUsed
dcc50 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ){.  UNUSED_PARA
dcc60 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
dcc70 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
dcc80 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
dcc90 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20  the contents of 
dcca0 74 68 65 20 75 6e 69 78 46 69 6c 65 20 73 74 72  the unixFile str
dccb0 75 63 74 75 72 65 20 70 6f 69 6e 74 65 64 20 74  ucture pointed t
dccc0 6f 20 62 79 20 70 49 64 2e 0a 2a 2a 0a 2a 2a 20  o by pId..**.** 
dccd0 57 68 65 6e 20 6c 6f 63 6b 69 6e 67 20 65 78 74  When locking ext
dcce0 65 6e 73 69 6f 6e 73 20 61 72 65 20 65 6e 61 62  ensions are enab
dccf0 6c 65 64 2c 20 74 68 65 20 66 69 6c 65 70 61 74  led, the filepat
dcd00 68 20 61 6e 64 20 6c 6f 63 6b 69 6e 67 20 73 74  h and locking st
dcd10 79 6c 65 20 0a 2a 2a 20 61 72 65 20 6e 65 65 64  yle .** are need
dcd20 65 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  ed to determine 
dcd30 74 68 65 20 75 6e 69 78 46 69 6c 65 20 70 4d 65  the unixFile pMe
dcd40 74 68 6f 64 20 74 6f 20 75 73 65 20 66 6f 72 20  thod to use for 
dcd50 6c 6f 63 6b 69 6e 67 20 6f 70 65 72 61 74 69 6f  locking operatio
dcd60 6e 73 2e 0a 2a 2a 20 54 68 65 20 6c 6f 63 6b 69  ns..** The locki
dcd70 6e 67 2d 73 74 79 6c 65 20 73 70 65 63 69 66 69  ng-style specifi
dcd80 63 20 6c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74  c lockingContext
dcd90 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
dcda0 69 73 20 63 72 65 61 74 65 64 20 0a 2a 2a 20 61  is created .** a
dcdb0 6e 64 20 61 73 73 69 67 6e 65 64 20 68 65 72 65  nd assigned here
dcdc0 20 61 6c 73 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63   also..*/.static
dcdd0 20 69 6e 74 20 66 69 6c 6c 49 6e 55 6e 69 78 46   int fillInUnixF
dcde0 69 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76  ile(.  sqlite3_v
dcdf0 66 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 2f  fs *pVfs,      /
dce00 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76 66 73  * Pointer to vfs
dce10 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 69 6e 74   object */.  int
dce20 20 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   h,             
dce30 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20 66 69 6c       /* Open fil
dce40 65 20 64 65 73 63 72 69 70 74 6f 72 20 6f 66 20  e descriptor of 
dce50 66 69 6c 65 20 62 65 69 6e 67 20 6f 70 65 6e 65  file being opene
dce60 64 20 2a 2f 0a 20 20 69 6e 74 20 64 69 72 66 64  d */.  int dirfd
dce70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
dce80 2a 20 44 69 72 65 63 74 6f 72 79 20 66 69 6c 65  * Directory file
dce90 20 64 65 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20   descriptor */. 
dcea0 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
dceb0 49 64 2c 20 20 20 20 20 20 2f 2a 20 57 72 69 74  Id,      /* Writ
dcec0 65 20 74 6f 20 74 68 65 20 75 6e 69 78 46 69 6c  e to the unixFil
dced0 65 20 73 74 72 75 63 74 75 72 65 20 68 65 72 65  e structure here
dcee0 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
dcef0 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20 2f 2a   *zFilename,  /*
dcf00 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 69 6c   Name of the fil
dcf10 65 20 62 65 69 6e 67 20 6f 70 65 6e 65 64 20 2a  e being opened *
dcf20 2f 0a 20 20 69 6e 74 20 6e 6f 4c 6f 63 6b 2c 20  /.  int noLock, 
dcf30 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
dcf40 6d 69 74 20 6c 6f 63 6b 69 6e 67 20 69 66 20 74  mit locking if t
dcf50 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44  rue */.  int isD
dcf60 65 6c 65 74 65 20 20 20 20 20 20 20 20 20 20 20  elete           
dcf70 20 2f 2a 20 44 65 6c 65 74 65 20 6f 6e 20 63 6c   /* Delete on cl
dcf80 6f 73 65 20 69 66 20 74 72 75 65 20 2a 2f 0a 29  ose if true */.)
dcf90 7b 0a 20 20 69 6e 74 20 65 4c 6f 63 6b 69 6e 67  {.  int eLocking
dcfa0 53 74 79 6c 65 3b 0a 20 20 75 6e 69 78 46 69 6c  Style;.  unixFil
dcfb0 65 20 2a 70 4e 65 77 20 3d 20 28 75 6e 69 78 46  e *pNew = (unixF
dcfc0 69 6c 65 20 2a 29 70 49 64 3b 0a 20 20 69 6e 74  ile *)pId;.  int
dcfd0 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
dcfe0 0a 0a 20 20 2f 2a 20 4d 61 63 72 6f 20 74 6f 20  ..  /* Macro to 
dcff0 64 65 66 69 6e 65 20 74 68 65 20 73 74 61 74 69  define the stati
dd000 63 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 61 6e  c contents of an
dd010 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
dd020 6f 64 73 20 0a 20 20 2a 2a 20 73 74 72 75 63 74  ods .  ** struct
dd030 75 72 65 20 66 6f 72 20 61 20 75 6e 69 78 20 62  ure for a unix b
dd040 61 63 6b 65 6e 64 20 66 69 6c 65 2e 20 44 69 66  ackend file. Dif
dd050 66 65 72 65 6e 74 20 6c 6f 63 6b 69 6e 67 20 6d  ferent locking m
dd060 65 74 68 6f 64 73 0a 20 20 2a 2a 20 72 65 71 75  ethods.  ** requ
dd070 69 72 65 20 64 69 66 66 65 72 65 6e 74 20 66 75  ire different fu
dd080 6e 63 74 69 6f 6e 73 20 66 6f 72 20 74 68 65 20  nctions for the 
dd090 78 43 6c 6f 73 65 2c 20 78 4c 6f 63 6b 2c 20 78  xClose, xLock, x
dd0a0 55 6e 6c 6f 63 6b 20 61 6e 64 0a 20 20 2a 2a 20  Unlock and.  ** 
dd0b0 78 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f  xCheckReservedLo
dd0c0 63 6b 20 6d 65 74 68 6f 64 73 2e 0a 20 20 2a 2f  ck methods..  */
dd0d0 0a 20 20 23 64 65 66 69 6e 65 20 49 4f 4d 45 54  .  #define IOMET
dd0e0 48 4f 44 53 28 78 43 6c 6f 73 65 2c 20 78 4c 6f  HODS(xClose, xLo
dd0f0 63 6b 2c 20 78 55 6e 6c 6f 63 6b 2c 20 78 43 68  ck, xUnlock, xCh
dd100 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29  eckReservedLock)
dd110 20 7b 20 20 20 20 5c 0a 20 20 20 20 31 2c 20 20   {    \.    1,  
dd120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd130 20 20 20 20 20 20 20 20 2f 2a 20 69 56 65 72 73          /* iVers
dd140 69 6f 6e 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ion */          
dd150 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd160 20 5c 0a 20 20 20 20 78 43 6c 6f 73 65 2c 20 20   \.    xClose,  
dd170 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd180 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 20     /* xClose */ 
dd190 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd1a0 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
dd1b0 20 20 75 6e 69 78 52 65 61 64 2c 20 20 20 20 20    unixRead,     
dd1c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
dd1d0 20 78 52 65 61 64 20 2a 2f 20 20 20 20 20 20 20   xRead */       
dd1e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd1f0 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69         \.    uni
dd200 78 57 72 69 74 65 2c 20 20 20 20 20 20 20 20 20  xWrite,         
dd210 20 20 20 20 20 20 20 20 20 2f 2a 20 78 57 72 69           /* xWri
dd220 74 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  te */           
dd230 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd240 20 20 5c 0a 20 20 20 20 75 6e 69 78 54 72 75 6e    \.    unixTrun
dd250 63 61 74 65 2c 20 20 20 20 20 20 20 20 20 20 20  cate,           
dd260 20 20 20 20 2f 2a 20 78 54 72 75 6e 63 61 74 65      /* xTruncate
dd270 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
dd280 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20               \. 
dd290 20 20 20 75 6e 69 78 53 79 6e 63 2c 20 20 20 20     unixSync,    
dd2a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
dd2b0 2a 20 78 53 79 6e 63 20 2a 2f 20 20 20 20 20 20  * xSync */      
dd2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd2d0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
dd2e0 69 78 46 69 6c 65 53 69 7a 65 2c 20 20 20 20 20  ixFileSize,     
dd2f0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
dd300 6c 65 53 69 7a 65 20 2a 2f 20 20 20 20 20 20 20  leSize */       
dd310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd320 20 20 20 5c 0a 20 20 20 20 78 4c 6f 63 6b 2c 20     \.    xLock, 
dd330 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd340 20 20 20 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f       /* xLock */
dd350 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd360 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
dd370 20 20 20 20 78 55 6e 6c 6f 63 6b 2c 20 20 20 20      xUnlock,    
dd380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd390 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 20 20 20  /* xUnlock */   
dd3a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
dd3b0 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 78           \.    x
dd3c0 43 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63  CheckReservedLoc
dd3d0 6b 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43  k,         /* xC
dd3e0 68 65 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b  heckReservedLock
dd3f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
dd400 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 46 69      \.    unixFi
dd410 6c 65 43 6f 6e 74 72 6f 6c 2c 20 20 20 20 20 20  leControl,      
dd420 20 20 20 20 20 20 2f 2a 20 78 46 69 6c 65 43 6f        /* xFileCo
dd430 6e 74 72 6f 6c 20 2a 2f 20 20 20 20 20 20 20 20  ntrol */        
dd440 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
dd450 0a 20 20 20 20 75 6e 69 78 53 65 63 74 6f 72 53  .    unixSectorS
dd460 69 7a 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ize,            
dd470 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a 65 20   /* xSectorSize 
dd480 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
dd490 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
dd4a0 75 6e 69 78 44 65 76 69 63 65 43 68 61 72 61 63  unixDeviceCharac
dd4b0 74 65 72 69 73 74 69 63 73 20 20 20 2f 2a 20 78  teristics   /* x
dd4c0 44 65 76 69 63 65 43 61 70 61 62 69 6c 69 74 69  DeviceCapabiliti
dd4d0 65 73 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  es */           
dd4e0 20 20 20 20 20 5c 0a 20 20 7d 0a 20 20 73 74 61       \.  }.  sta
dd4f0 74 69 63 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d  tic sqlite3_io_m
dd500 65 74 68 6f 64 73 20 61 49 6f 4d 65 74 68 6f 64  ethods aIoMethod
dd510 5b 5d 20 3d 20 7b 0a 20 20 20 20 49 4f 4d 45 54  [] = {.    IOMET
dd520 48 4f 44 53 28 75 6e 69 78 43 6c 6f 73 65 2c 20  HODS(unixClose, 
dd530 75 6e 69 78 4c 6f 63 6b 2c 20 75 6e 69 78 55 6e  unixLock, unixUn
dd540 6c 6f 63 6b 2c 20 75 6e 69 78 43 68 65 63 6b 52  lock, unixCheckR
dd550 65 73 65 72 76 65 64 4c 6f 63 6b 29 20 0a 20 20  eservedLock) .  
dd560 20 2c 49 4f 4d 45 54 48 4f 44 53 28 6e 6f 6c 6f   ,IOMETHODS(nolo
dd570 63 6b 43 6c 6f 73 65 2c 20 6e 6f 6c 6f 63 6b 4c  ckClose, nolockL
dd580 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63  ock, nolockUnloc
dd590 6b 2c 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65  k, nolockCheckRe
dd5a0 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 69 66 20  servedLock).#if 
dd5b0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
dd5c0 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20 20 2c  CKING_STYLE.   ,
dd5d0 49 4f 4d 45 54 48 4f 44 53 28 64 6f 74 6c 6f 63  IOMETHODS(dotloc
dd5e0 6b 43 6c 6f 73 65 2c 20 64 6f 74 6c 6f 63 6b 4c  kClose, dotlockL
dd5f0 6f 63 6b 2c 20 64 6f 74 6c 6f 63 6b 55 6e 6c 6f  ock, dotlockUnlo
dd600 63 6b 2c 64 6f 74 6c 6f 63 6b 43 68 65 63 6b 52  ck,dotlockCheckR
dd610 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 69 66  eservedLock).#if
dd620 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20 2c   IS_VXWORKS.   ,
dd630 49 4f 4d 45 54 48 4f 44 53 28 6e 6f 6c 6f 63 6b  IOMETHODS(nolock
dd640 43 6c 6f 73 65 2c 20 6e 6f 6c 6f 63 6b 4c 6f 63  Close, nolockLoc
dd650 6b 2c 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c  k, nolockUnlock,
dd660 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65   nolockCheckRese
dd670 72 76 65 64 4c 6f 63 6b 29 0a 20 20 20 2c 49 4f  rvedLock).   ,IO
dd680 4d 45 54 48 4f 44 53 28 6e 6f 6c 6f 63 6b 43 6c  METHODS(nolockCl
dd690 6f 73 65 2c 20 6e 6f 6c 6f 63 6b 4c 6f 63 6b 2c  ose, nolockLock,
dd6a0 20 6e 6f 6c 6f 63 6b 55 6e 6c 6f 63 6b 2c 20 6e   nolockUnlock, n
dd6b0 6f 6c 6f 63 6b 43 68 65 63 6b 52 65 73 65 72 76  olockCheckReserv
dd6c0 65 64 4c 6f 63 6b 29 0a 20 20 20 2c 49 4f 4d 45  edLock).   ,IOME
dd6d0 54 48 4f 44 53 28 6e 61 6d 65 64 73 65 6d 43 6c  THODS(namedsemCl
dd6e0 6f 73 65 2c 20 6e 61 6d 65 64 73 65 6d 4c 6f 63  ose, namedsemLoc
dd6f0 6b 2c 20 6e 61 6d 65 64 73 65 6d 55 6e 6c 6f 63  k, namedsemUnloc
dd700 6b 2c 20 6e 61 6d 65 64 73 65 6d 43 68 65 63 6b  k, namedsemCheck
dd710 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 65  ReservedLock).#e
dd720 6c 73 65 0a 20 20 20 2c 49 4f 4d 45 54 48 4f 44  lse.   ,IOMETHOD
dd730 53 28 66 6c 6f 63 6b 43 6c 6f 73 65 2c 20 66 6c  S(flockClose, fl
dd740 6f 63 6b 4c 6f 63 6b 2c 20 66 6c 6f 63 6b 55 6e  ockLock, flockUn
dd750 6c 6f 63 6b 2c 20 66 6c 6f 63 6b 43 68 65 63 6b  lock, flockCheck
dd760 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 20 20  ReservedLock).  
dd770 20 2c 49 4f 4d 45 54 48 4f 44 53 28 61 66 70 43   ,IOMETHODS(afpC
dd780 6c 6f 73 65 2c 20 61 66 70 4c 6f 63 6b 2c 20 61  lose, afpLock, a
dd790 66 70 55 6e 6c 6f 63 6b 2c 20 61 66 70 43 68 65  fpUnlock, afpChe
dd7a0 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a  ckReservedLock).
dd7b0 20 20 20 2c 49 4f 4d 45 54 48 4f 44 53 28 6e 6f     ,IOMETHODS(no
dd7c0 6c 6f 63 6b 43 6c 6f 73 65 2c 20 6e 6f 6c 6f 63  lockClose, noloc
dd7d0 6b 4c 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 55 6e 6c  kLock, nolockUnl
dd7e0 6f 63 6b 2c 20 6e 6f 6c 6f 63 6b 43 68 65 63 6b  ock, nolockCheck
dd7f0 52 65 73 65 72 76 65 64 4c 6f 63 6b 29 0a 23 65  ReservedLock).#e
dd800 6e 64 69 66 0a 23 65 6e 64 69 66 0a 20 20 7d 3b  ndif.#endif.  };
dd810 0a 20 20 2f 2a 20 54 68 65 20 6f 72 64 65 72 20  .  /* The order 
dd820 6f 66 20 74 68 65 20 49 4f 4d 45 54 48 4f 44 53  of the IOMETHODS
dd830 20 6d 61 63 72 6f 73 20 61 62 6f 76 65 20 69 73   macros above is
dd840 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 49 74 20   important.  It 
dd850 6d 75 73 74 20 62 65 20 74 68 65 0a 20 20 2a 2a  must be the.  **
dd860 20 73 61 6d 65 20 6f 72 64 65 72 20 61 73 20 74   same order as t
dd870 68 65 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45  he LOCKING_STYLE
dd880 20 6e 75 6d 62 65 72 73 0a 20 20 2a 2f 0a 20 20   numbers.  */.  
dd890 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47 5f 53  assert(LOCKING_S
dd8a0 54 59 4c 45 5f 50 4f 53 49 58 3d 3d 31 29 3b 0a  TYLE_POSIX==1);.
dd8b0 20 20 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e 47    assert(LOCKING
dd8c0 5f 53 54 59 4c 45 5f 4e 4f 4e 45 3d 3d 32 29 3b  _STYLE_NONE==2);
dd8d0 0a 20 20 61 73 73 65 72 74 28 4c 4f 43 4b 49 4e  .  assert(LOCKIN
dd8e0 47 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 3d  G_STYLE_DOTFILE=
dd8f0 3d 33 29 3b 0a 20 20 61 73 73 65 72 74 28 4c 4f  =3);.  assert(LO
dd900 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f 43  CKING_STYLE_FLOC
dd910 4b 3d 3d 34 29 3b 0a 20 20 61 73 73 65 72 74 28  K==4);.  assert(
dd920 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 41 46  LOCKING_STYLE_AF
dd930 50 3d 3d 35 29 3b 0a 20 20 61 73 73 65 72 74 28  P==5);.  assert(
dd940 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 41  LOCKING_STYLE_NA
dd950 4d 45 44 53 45 4d 3d 3d 36 29 3b 0a 0a 20 20 61  MEDSEM==6);..  a
dd960 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 4c 6f  ssert( pNew->pLo
dd970 63 6b 3d 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61 73  ck==NULL );.  as
dd980 73 65 72 74 28 20 70 4e 65 77 2d 3e 70 4f 70 65  sert( pNew->pOpe
dd990 6e 3d 3d 4e 55 4c 4c 20 29 3b 0a 0a 20 20 2f 2a  n==NULL );..  /*
dd9a0 20 50 61 72 61 6d 65 74 65 72 20 69 73 44 65 6c   Parameter isDel
dd9b0 65 74 65 20 69 73 20 6f 6e 6c 79 20 75 73 65 64  ete is only used
dd9c0 20 6f 6e 20 76 78 77 6f 72 6b 73 2e 20 50 61 72   on vxworks. Par
dd9d0 61 6d 65 74 65 72 20 70 56 66 73 20 69 73 20 6f  ameter pVfs is o
dd9e0 6e 6c 79 0a 20 20 2a 2a 20 75 73 65 64 20 69 66  nly.  ** used if
dd9f0 20 45 4e 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f   ENABLE_LOCKING_
dda00 53 54 59 4c 45 20 69 73 20 64 65 66 69 6e 65 64  STYLE is defined
dda10 2e 20 45 78 70 72 65 73 73 20 74 68 69 73 20 65  . Express this e
dda20 78 70 6c 69 63 69 74 6c 79 20 0a 20 20 2a 2a 20  xplicitly .  ** 
dda30 68 65 72 65 20 74 6f 20 70 72 65 76 65 6e 74 20  here to prevent 
dda40 63 6f 6d 70 69 6c 65 72 20 77 61 72 6e 69 6e 67  compiler warning
dda50 73 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 70  s about unused p
dda60 61 72 61 6d 65 74 65 72 73 2e 0a 20 20 2a 2f 0a  arameters..  */.
dda70 20 20 69 66 28 20 21 49 53 5f 56 58 57 4f 52 4b    if( !IS_VXWORK
dda80 53 20 29 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  S ) UNUSED_PARAM
dda90 45 54 45 52 28 69 73 44 65 6c 65 74 65 29 3b 0a  ETER(isDelete);.
ddaa0 20 20 69 66 28 20 21 53 51 4c 49 54 45 5f 45 4e    if( !SQLITE_EN
ddab0 41 42 4c 45 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59  ABLE_LOCKING_STY
ddac0 4c 45 20 29 20 55 4e 55 53 45 44 5f 50 41 52 41  LE ) UNUSED_PARA
ddad0 4d 45 54 45 52 28 70 56 66 73 29 3b 0a 20 20 69  METER(pVfs);.  i
ddae0 66 28 20 21 49 53 5f 56 58 57 4f 52 4b 53 20 26  f( !IS_VXWORKS &
ddaf0 26 20 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  & !SQLITE_ENABLE
ddb00 5f 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 20 29  _LOCKING_STYLE )
ddb10 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
ddb20 52 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 0a 20  R(zFilename);.. 
ddb30 20 4f 53 54 52 41 43 45 33 28 22 4f 50 45 4e 20   OSTRACE3("OPEN 
ddb40 20 20 20 25 2d 33 64 20 25 73 5c 6e 22 2c 20 68     %-3d %s\n", h
ddb50 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 20 20 20  , zFilename);   
ddb60 20 0a 20 20 70 4e 65 77 2d 3e 68 20 3d 20 68 3b   .  pNew->h = h;
ddb70 0a 20 20 70 4e 65 77 2d 3e 64 69 72 66 64 20 3d  .  pNew->dirfd =
ddb80 20 64 69 72 66 64 3b 0a 20 20 53 45 54 5f 54 48   dirfd;.  SET_TH
ddb90 52 45 41 44 49 44 28 70 4e 65 77 29 3b 0a 0a 23  READID(pNew);..#
ddba0 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if IS_VXWORKS.  
ddbb0 7b 0a 20 20 20 20 48 61 73 68 45 6c 65 6d 20 2a  {.    HashElem *
ddbc0 70 45 6c 65 6d 3b 0a 20 20 20 20 63 68 61 72 20  pElem;.    char 
ddbd0 2a 7a 52 65 61 6c 6e 61 6d 65 20 3d 20 76 78 72  *zRealname = vxr
ddbe0 65 61 6c 70 61 74 68 28 7a 46 69 6c 65 6e 61 6d  ealpath(zFilenam
ddbf0 65 2c 20 31 29 3b 0a 20 20 20 20 69 6e 74 20 6e  e, 1);.    int n
ddc00 3b 0a 20 20 20 20 70 4e 65 77 2d 3e 7a 52 65 61  ;.    pNew->zRea
ddc10 6c 70 61 74 68 20 3d 20 30 3b 0a 20 20 20 20 69  lpath = 0;.    i
ddc20 66 28 20 21 7a 52 65 61 6c 6e 61 6d 65 20 29 7b  f( !zRealname ){
ddc30 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
ddc40 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
ddc50 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20  eLockingStyle = 
ddc60 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f  LOCKING_STYLE_NO
ddc70 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  NE;.    }else{. 
ddc80 20 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28       n = strlen(
ddc90 7a 52 65 61 6c 6e 61 6d 65 29 20 2b 20 31 3b 0a  zRealname) + 1;.
ddca0 20 20 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78        enterMutex
ddcb0 28 29 3b 0a 20 20 20 20 20 20 70 45 6c 65 6d 20  ();.      pElem 
ddcc0 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
ddcd0 64 45 6c 65 6d 28 26 6e 61 6d 65 48 61 73 68 2c  dElem(&nameHash,
ddce0 20 7a 52 65 61 6c 6e 61 6d 65 2c 20 6e 29 3b 0a   zRealname, n);.
ddcf0 20 20 20 20 20 20 69 66 28 20 70 45 6c 65 6d 20        if( pElem 
ddd00 29 7b 0a 20 20 20 20 20 20 20 20 6c 6f 6e 67 20  ){.        long 
ddd10 63 6e 74 20 3d 20 28 6c 6f 6e 67 29 70 45 6c 65  cnt = (long)pEle
ddd20 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20 20 20 20  m->data;.       
ddd30 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20   cnt++;.        
ddd40 70 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68 20  pNew->zRealpath 
ddd50 3d 20 70 45 6c 65 6d 2d 3e 70 4b 65 79 3b 0a 20  = pElem->pKey;. 
ddd60 20 20 20 20 20 20 20 70 45 6c 65 6d 2d 3e 64 61         pElem->da
ddd70 74 61 20 3d 20 28 76 6f 69 64 2a 29 63 6e 74 3b  ta = (void*)cnt;
ddd80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
ddd90 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
ddda0 33 48 61 73 68 49 6e 73 65 72 74 28 26 6e 61 6d  3HashInsert(&nam
dddb0 65 48 61 73 68 2c 20 7a 52 65 61 6c 6e 61 6d 65  eHash, zRealname
dddc0 2c 20 6e 2c 20 28 76 6f 69 64 2a 29 31 29 3d 3d  , n, (void*)1)==
dddd0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  0 ){.          p
ddde0 45 6c 65 6d 20 3d 20 73 71 6c 69 74 65 33 48 61  Elem = sqlite3Ha
dddf0 73 68 46 69 6e 64 45 6c 65 6d 28 26 6e 61 6d 65  shFindElem(&name
dde00 48 61 73 68 2c 20 7a 52 65 61 6c 6e 61 6d 65 2c  Hash, zRealname,
dde10 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69   n);.          i
dde20 66 28 20 70 45 6c 65 6d 20 29 7b 0a 20 20 20 20  f( pElem ){.    
dde30 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 7a 52          pNew->zR
dde40 65 61 6c 70 61 74 68 20 3d 20 70 45 6c 65 6d 2d  ealpath = pElem-
dde50 3e 70 4b 65 79 3b 0a 20 20 20 20 20 20 20 20 20  >pKey;.         
dde60 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
dde70 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
dde80 6e 73 65 72 74 28 26 6e 61 6d 65 48 61 73 68 2c  nsert(&nameHash,
dde90 20 7a 52 65 61 6c 6e 61 6d 65 2c 20 6e 2c 20 30   zRealname, n, 0
ddea0 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 72  );.            r
ddeb0 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
ddec0 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 65 4c  ;.            eL
dded0 6f 63 6b 69 6e 67 53 74 79 6c 65 20 3d 20 4c 4f  ockingStyle = LO
ddee0 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45  CKING_STYLE_NONE
ddef0 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
ddf00 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
ddf10 20 20 20 20 20 20 6c 65 61 76 65 4d 75 74 65 78        leaveMutex
ddf20 28 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  ();.      sqlite
ddf30 33 5f 66 72 65 65 28 7a 52 65 61 6c 6e 61 6d 65  3_free(zRealname
ddf40 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  );.    }.  }.#en
ddf50 64 69 66 0a 0a 20 20 69 66 28 20 6e 6f 4c 6f 63  dif..  if( noLoc
ddf60 6b 20 29 7b 0a 20 20 20 20 65 4c 6f 63 6b 69 6e  k ){.    eLockin
ddf70 67 53 74 79 6c 65 20 3d 20 4c 4f 43 4b 49 4e 47  gStyle = LOCKING
ddf80 5f 53 54 59 4c 45 5f 4e 4f 4e 45 3b 0a 20 20 7d  _STYLE_NONE;.  }
ddf90 65 6c 73 65 7b 0a 20 20 20 20 65 4c 6f 63 6b 69  else{.    eLocki
ddfa0 6e 67 53 74 79 6c 65 20 3d 20 64 65 74 65 63 74  ngStyle = detect
ddfb0 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 28 70 56 66  LockingStyle(pVf
ddfc0 73 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 68 29  s, zFilename, h)
ddfd0 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68 28  ;.  }..  switch(
ddfe0 20 65 4c 6f 63 6b 69 6e 67 53 74 79 6c 65 20 29   eLockingStyle )
ddff0 7b 0a 0a 20 20 20 20 63 61 73 65 20 4c 4f 43 4b  {..    case LOCK
de000 49 4e 47 5f 53 54 59 4c 45 5f 50 4f 53 49 58 3a  ING_STYLE_POSIX:
de010 20 7b 0a 20 20 20 20 20 20 65 6e 74 65 72 4d 75   {.      enterMu
de020 74 65 78 28 29 3b 0a 23 69 66 20 49 53 5f 56 58  tex();.#if IS_VX
de030 57 4f 52 4b 53 0a 20 20 20 20 20 20 72 63 20 3d  WORKS.      rc =
de040 20 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c   findLockInfo(h,
de050 20 70 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68   pNew->zRealpath
de060 2c 20 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20  , &pNew->pLock, 
de070 26 70 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 23  &pNew->pOpen);.#
de080 65 6c 73 65 0a 20 20 20 20 20 20 72 63 20 3d 20  else.      rc = 
de090 66 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20  findLockInfo(h, 
de0a0 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70  &pNew->pLock, &p
de0b0 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 23 65 6e  New->pOpen);.#en
de0c0 64 69 66 0a 20 20 20 20 20 20 6c 65 61 76 65 4d  dif.      leaveM
de0d0 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 62 72  utex();.      br
de0e0 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 20  eak;.    }..#if 
de0f0 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4c 4f  SQLITE_ENABLE_LO
de100 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 0a 23 69 66  CKING_STYLE..#if
de110 20 21 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20 20   !IS_VXWORKS.   
de120 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53 54   case LOCKING_ST
de130 59 4c 45 5f 41 46 50 3a 20 7b 0a 20 20 20 20 20  YLE_AFP: {.     
de140 20 2f 2a 20 41 46 50 20 6c 6f 63 6b 69 6e 67 20   /* AFP locking 
de150 75 73 65 73 20 74 68 65 20 66 69 6c 65 20 70 61  uses the file pa
de160 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73 20 74  th so it needs t
de170 6f 20 62 65 20 69 6e 63 6c 75 64 65 64 20 69 6e  o be included in
de180 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 61 66  .      ** the af
de190 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 2e  pLockingContext.
de1a0 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
de1b0 61 66 70 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78  afpLockingContex
de1c0 74 20 2a 70 43 74 78 3b 0a 20 20 20 20 20 20 70  t *pCtx;.      p
de1d0 4e 65 77 2d 3e 6c 6f 63 6b 69 6e 67 43 6f 6e 74  New->lockingCont
de1e0 65 78 74 20 3d 20 70 43 74 78 20 3d 20 73 71 6c  ext = pCtx = sql
de1f0 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 20 73 69 7a  ite3_malloc( siz
de200 65 6f 66 28 2a 70 43 74 78 29 20 29 3b 0a 20 20  eof(*pCtx) );.  
de210 20 20 20 20 69 66 28 20 70 43 74 78 3d 3d 30 20      if( pCtx==0 
de220 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
de230 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
de240 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
de250 20 20 20 2f 2a 20 4e 42 3a 20 7a 46 69 6c 65 6e     /* NB: zFilen
de260 61 6d 65 20 65 78 69 73 74 73 20 61 6e 64 20 72  ame exists and r
de270 65 6d 61 69 6e 73 20 76 61 6c 69 64 20 75 6e 74  emains valid unt
de280 69 6c 20 74 68 65 20 66 69 6c 65 20 69 73 20 63  il the file is c
de290 6c 6f 73 65 64 0a 20 20 20 20 20 20 20 20 2a 2a  losed.        **
de2a0 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 72 65   according to re
de2b0 71 75 69 72 65 6d 65 6e 74 20 46 31 31 31 34 31  quirement F11141
de2c0 2e 20 20 53 6f 20 77 65 20 64 6f 20 6e 6f 74 20  .  So we do not 
de2d0 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20 61 0a 20  need to make a. 
de2e0 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 79 20 6f         ** copy o
de2f0 66 20 74 68 65 20 66 69 6c 65 6e 61 6d 65 2e 20  f the filename. 
de300 2a 2f 0a 20 20 20 20 20 20 20 20 70 43 74 78 2d  */.        pCtx-
de310 3e 66 69 6c 65 50 61 74 68 20 3d 20 7a 46 69 6c  >filePath = zFil
de320 65 6e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 73  ename;.        s
de330 72 61 6e 64 6f 6d 64 65 76 28 29 3b 0a 20 20 20  randomdev();.   
de340 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
de350 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
de360 20 20 20 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47      case LOCKING
de370 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 3a 20  _STYLE_DOTFILE: 
de380 7b 0a 20 20 20 20 20 20 2f 2a 20 44 6f 74 66 69  {.      /* Dotfi
de390 6c 65 20 6c 6f 63 6b 69 6e 67 20 75 73 65 73 20  le locking uses 
de3a0 74 68 65 20 66 69 6c 65 20 70 61 74 68 20 73 6f  the file path so
de3b0 20 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20   it needs to be 
de3c0 69 6e 63 6c 75 64 65 64 20 69 6e 0a 20 20 20 20  included in.    
de3d0 20 20 2a 2a 20 74 68 65 20 64 6f 74 6c 6f 63 6b    ** the dotlock
de3e0 4c 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 0a  LockingContext .
de3f0 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63        */.      c
de400 68 61 72 20 2a 7a 4c 6f 63 6b 46 69 6c 65 3b 0a  har *zLockFile;.
de410 20 20 20 20 20 20 69 6e 74 20 6e 46 69 6c 65 6e        int nFilen
de420 61 6d 65 3b 0a 20 20 20 20 20 20 6e 46 69 6c 65  ame;.      nFile
de430 6e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 46  name = strlen(zF
de440 69 6c 65 6e 61 6d 65 29 20 2b 20 36 3b 0a 20 20  ilename) + 6;.  
de450 20 20 20 20 7a 4c 6f 63 6b 46 69 6c 65 20 3d 20      zLockFile = 
de460 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f  (char *)sqlite3_
de470 6d 61 6c 6c 6f 63 28 6e 46 69 6c 65 6e 61 6d 65  malloc(nFilename
de480 29 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 4c 6f  );.      if( zLo
de490 63 6b 46 69 6c 65 3d 3d 30 20 29 7b 0a 20 20 20  ckFile==0 ){.   
de4a0 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
de4b0 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65  _NOMEM;.      }e
de4c0 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
de4d0 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 46  ite3_snprintf(nF
de4e0 69 6c 65 6e 61 6d 65 2c 20 7a 4c 6f 63 6b 46 69  ilename, zLockFi
de4f0 6c 65 2c 20 22 25 73 2e 6c 6f 63 6b 22 2c 20 7a  le, "%s.lock", z
de500 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 20  Filename);.     
de510 20 7d 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 6c   }.      pNew->l
de520 6f 63 6b 69 6e 67 43 6f 6e 74 65 78 74 20 3d 20  ockingContext = 
de530 7a 4c 6f 63 6b 46 69 6c 65 3b 0a 20 20 20 20 20  zLockFile;.     
de540 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23   break;.    }..#
de550 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a 20 20  if IS_VXWORKS.  
de560 20 20 63 61 73 65 20 4c 4f 43 4b 49 4e 47 5f 53    case LOCKING_S
de570 54 59 4c 45 5f 4e 41 4d 45 44 53 45 4d 3a 20 7b  TYLE_NAMEDSEM: {
de580 0a 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 64 20  .      /* Named 
de590 73 65 6d 61 70 68 6f 72 65 20 6c 6f 63 6b 69 6e  semaphore lockin
de5a0 67 20 75 73 65 73 20 74 68 65 20 66 69 6c 65 20  g uses the file 
de5b0 70 61 74 68 20 73 6f 20 69 74 20 6e 65 65 64 73  path so it needs
de5c0 20 74 6f 20 62 65 0a 20 20 20 20 20 20 2a 2a 20   to be.      ** 
de5d0 69 6e 63 6c 75 64 65 64 20 69 6e 20 74 68 65 20  included in the 
de5e0 6e 61 6d 65 64 73 65 6d 4c 6f 63 6b 69 6e 67 43  namedsemLockingC
de5f0 6f 6e 74 65 78 74 0a 20 20 20 20 20 20 2a 2f 0a  ontext.      */.
de600 20 20 20 20 20 20 65 6e 74 65 72 4d 75 74 65 78        enterMutex
de610 28 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 66  ();.      rc = f
de620 69 6e 64 4c 6f 63 6b 49 6e 66 6f 28 68 2c 20 70  indLockInfo(h, p
de630 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68 2c 20  New->zRealpath, 
de640 26 70 4e 65 77 2d 3e 70 4c 6f 63 6b 2c 20 26 70  &pNew->pLock, &p
de650 4e 65 77 2d 3e 70 4f 70 65 6e 29 3b 0a 20 20 20  New->pOpen);.   
de660 20 20 20 69 66 28 20 28 72 63 3d 3d 53 51 4c 49     if( (rc==SQLI
de670 54 45 5f 4f 4b 29 20 26 26 20 28 70 4e 65 77 2d  TE_OK) && (pNew-
de680 3e 70 4f 70 65 6e 2d 3e 70 53 65 6d 3d 3d 4e 55  >pOpen->pSem==NU
de690 4c 4c 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63  LL) ){.        c
de6a0 68 61 72 20 2a 7a 53 65 6d 4e 61 6d 65 20 3d 20  har *zSemName = 
de6b0 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d 3e 61 53 65  pNew->pOpen->aSe
de6c0 6d 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 69  mName;.        i
de6d0 6e 74 20 6e 3b 0a 20 20 20 20 20 20 20 20 73 71  nt n;.        sq
de6e0 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 4d  lite3_snprintf(M
de6f0 41 58 5f 50 41 54 48 4e 41 4d 45 2c 20 7a 53 65  AX_PATHNAME, zSe
de700 6d 4e 61 6d 65 2c 20 22 25 73 2e 73 65 6d 22 2c  mName, "%s.sem",
de710 20 70 4e 65 77 2d 3e 7a 52 65 61 6c 70 61 74 68   pNew->zRealpath
de720 29 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 20  );.        for( 
de730 6e 3d 30 3b 20 7a 53 65 6d 4e 61 6d 65 5b 6e 5d  n=0; zSemName[n]
de740 3b 20 6e 2b 2b 20 29 0a 20 20 20 20 20 20 20 20  ; n++ ).        
de750 20 20 69 66 28 20 7a 53 65 6d 4e 61 6d 65 5b 6e    if( zSemName[n
de760 5d 3d 3d 27 2f 27 20 29 20 7a 53 65 6d 4e 61 6d  ]=='/' ) zSemNam
de770 65 5b 6e 5d 20 3d 20 27 5f 27 3b 0a 20 20 20 20  e[n] = '_';.    
de780 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d      pNew->pOpen-
de790 3e 70 53 65 6d 20 3d 20 73 65 6d 5f 6f 70 65 6e  >pSem = sem_open
de7a0 28 7a 53 65 6d 4e 61 6d 65 2c 20 4f 5f 43 52 45  (zSemName, O_CRE
de7b0 41 54 2c 20 30 36 36 36 2c 20 31 29 3b 0a 20 20  AT, 0666, 1);.  
de7c0 20 20 20 20 20 20 69 66 28 20 70 4e 65 77 2d 3e        if( pNew->
de7d0 70 4f 70 65 6e 2d 3e 70 53 65 6d 20 3d 3d 20 53  pOpen->pSem == S
de7e0 45 4d 5f 46 41 49 4c 45 44 20 29 7b 0a 20 20 20  EM_FAILED ){.   
de7f0 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
de800 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20  TE_NOMEM;.      
de810 20 20 20 20 70 4e 65 77 2d 3e 70 4f 70 65 6e 2d      pNew->pOpen-
de820 3e 61 53 65 6d 4e 61 6d 65 5b 30 5d 20 3d 20 27  >aSemName[0] = '
de830 5c 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  \0';.        }. 
de840 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c 65 61       }.      lea
de850 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 20  veMutex();.     
de860 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65   break;.    }.#e
de870 6e 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20 4c  ndif..    case L
de880 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 46 4c 4f  OCKING_STYLE_FLO
de890 43 4b 3a 20 0a 20 20 20 20 63 61 73 65 20 4c 4f  CK: .    case LO
de8a0 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e 45  CKING_STYLE_NONE
de8b0 3a 20 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  : .      break;.
de8c0 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 0a 20 20  #endif.  }.  .  
de8d0 70 4e 65 77 2d 3e 6c 61 73 74 45 72 72 6e 6f 20  pNew->lastErrno 
de8e0 3d 20 30 3b 0a 23 69 66 20 49 53 5f 56 58 57 4f  = 0;.#if IS_VXWO
de8f0 52 4b 53 0a 20 20 69 66 28 20 72 63 21 3d 53 51  RKS.  if( rc!=SQ
de900 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75  LITE_OK ){.    u
de910 6e 6c 69 6e 6b 28 7a 46 69 6c 65 6e 61 6d 65 29  nlink(zFilename)
de920 3b 0a 20 20 20 20 69 73 44 65 6c 65 74 65 20 3d  ;.    isDelete =
de930 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e   0;.  }.  pNew->
de940 69 73 44 65 6c 65 74 65 20 3d 20 69 73 44 65 6c  isDelete = isDel
de950 65 74 65 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66  ete;.#endif.  if
de960 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
de970 29 7b 0a 20 20 20 20 69 66 28 20 64 69 72 66 64  ){.    if( dirfd
de980 3e 3d 30 20 29 20 63 6c 6f 73 65 28 64 69 72 66  >=0 ) close(dirf
de990 64 29 3b 0a 20 20 20 20 63 6c 6f 73 65 28 68 29  d);.    close(h)
de9a0 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
de9b0 4e 65 77 2d 3e 70 4d 65 74 68 6f 64 20 3d 20 26  New->pMethod = &
de9c0 61 49 6f 4d 65 74 68 6f 64 5b 65 4c 6f 63 6b 69  aIoMethod[eLocki
de9d0 6e 67 53 74 79 6c 65 2d 31 5d 3b 0a 20 20 20 20  ngStyle-1];.    
de9e0 4f 70 65 6e 43 6f 75 6e 74 65 72 28 2b 31 29 3b  OpenCounter(+1);
de9f0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
dea00 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
dea10 61 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f  a file descripto
dea20 72 20 74 6f 20 74 68 65 20 64 69 72 65 63 74 6f  r to the directo
dea30 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 66 69  ry containing fi
dea40 6c 65 20 7a 46 69 6c 65 6e 61 6d 65 2e 0a 2a 2a  le zFilename..**
dea50 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c 20   If successful, 
dea60 2a 70 46 64 20 69 73 20 73 65 74 20 74 6f 20 74  *pFd is set to t
dea70 68 65 20 6f 70 65 6e 65 64 20 66 69 6c 65 20 64  he opened file d
dea80 65 73 63 72 69 70 74 6f 72 20 61 6e 64 0a 2a 2a  escriptor and.**
dea90 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
deaa0 74 75 72 6e 65 64 2e 20 49 66 20 61 6e 20 65 72  turned. If an er
deab0 72 6f 72 20 6f 63 63 75 72 73 2c 20 65 69 74 68  ror occurs, eith
deac0 65 72 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a  er SQLITE_NOMEM.
dead0 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 43 41 4e  ** or SQLITE_CAN
deae0 54 4f 50 45 4e 20 69 73 20 72 65 74 75 72 6e 65  TOPEN is returne
deaf0 64 20 61 6e 64 20 2a 70 46 64 20 69 73 20 73 65  d and *pFd is se
deb00 74 20 74 6f 20 61 6e 20 75 6e 64 65 66 69 6e 65  t to an undefine
deb10 64 0a 2a 2a 20 76 61 6c 75 65 2e 0a 2a 2a 0a 2a  d.** value..**.*
deb20 2a 20 49 66 20 53 51 4c 49 54 45 5f 4f 4b 20 69  * If SQLITE_OK i
deb30 73 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 20  s returned, the 
deb40 63 61 6c 6c 65 72 20 69 73 20 72 65 73 70 6f 6e  caller is respon
deb50 73 69 62 6c 65 20 66 6f 72 20 63 6c 6f 73 69 6e  sible for closin
deb60 67 0a 2a 2a 20 74 68 65 20 66 69 6c 65 20 64 65  g.** the file de
deb70 73 63 72 69 70 74 6f 72 20 2a 70 46 64 20 75 73  scriptor *pFd us
deb80 69 6e 67 20 63 6c 6f 73 65 28 29 2e 0a 2a 2f 0a  ing close()..*/.
deb90 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e 44  static int openD
deba0 69 72 65 63 74 6f 72 79 28 63 6f 6e 73 74 20 63  irectory(const c
debb0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
debc0 69 6e 74 20 2a 70 46 64 29 7b 0a 20 20 69 6e 74  int *pFd){.  int
debd0 20 69 69 3b 0a 20 20 69 6e 74 20 66 64 20 3d 20   ii;.  int fd = 
debe0 2d 31 3b 0a 20 20 63 68 61 72 20 7a 44 69 72 6e  -1;.  char zDirn
debf0 61 6d 65 5b 4d 41 58 5f 50 41 54 48 4e 41 4d 45  ame[MAX_PATHNAME
dec00 2b 31 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  +1];..  sqlite3_
dec10 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
dec20 48 4e 41 4d 45 2c 20 7a 44 69 72 6e 61 6d 65 2c  HNAME, zDirname,
dec30 20 22 25 73 22 2c 20 7a 46 69 6c 65 6e 61 6d 65   "%s", zFilename
dec40 29 3b 0a 20 20 66 6f 72 28 69 69 3d 73 74 72 6c  );.  for(ii=strl
dec50 65 6e 28 7a 44 69 72 6e 61 6d 65 29 3b 20 69 69  en(zDirname); ii
dec60 3e 3d 30 20 26 26 20 7a 44 69 72 6e 61 6d 65 5b  >=0 && zDirname[
dec70 69 69 5d 21 3d 27 2f 27 3b 20 69 69 2d 2d 29 3b  ii]!='/'; ii--);
dec80 0a 20 20 69 66 28 20 69 69 3e 30 20 29 7b 0a 20  .  if( ii>0 ){. 
dec90 20 20 20 7a 44 69 72 6e 61 6d 65 5b 69 69 5d 20     zDirname[ii] 
deca0 3d 20 27 5c 30 27 3b 0a 20 20 20 20 66 64 20 3d  = '\0';.    fd =
decb0 20 6f 70 65 6e 28 7a 44 69 72 6e 61 6d 65 2c 20   open(zDirname, 
decc0 4f 5f 52 44 4f 4e 4c 59 7c 4f 5f 42 49 4e 41 52  O_RDONLY|O_BINAR
decd0 59 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 66  Y, 0);.    if( f
dece0 64 3e 3d 30 20 29 7b 0a 23 69 66 64 65 66 20 46  d>=0 ){.#ifdef F
decf0 44 5f 43 4c 4f 45 58 45 43 0a 20 20 20 20 20 20  D_CLOEXEC.      
ded00 66 63 6e 74 6c 28 66 64 2c 20 46 5f 53 45 54 46  fcntl(fd, F_SETF
ded10 44 2c 20 66 63 6e 74 6c 28 66 64 2c 20 46 5f 47  D, fcntl(fd, F_G
ded20 45 54 46 44 2c 20 30 29 20 7c 20 46 44 5f 43 4c  ETFD, 0) | FD_CL
ded30 4f 45 58 45 43 29 3b 0a 23 65 6e 64 69 66 0a 20  OEXEC);.#endif. 
ded40 20 20 20 20 20 4f 53 54 52 41 43 45 33 28 22 4f       OSTRACE3("O
ded50 50 45 4e 44 49 52 20 25 2d 33 64 20 25 73 5c 6e  PENDIR %-3d %s\n
ded60 22 2c 20 66 64 2c 20 7a 44 69 72 6e 61 6d 65 29  ", fd, zDirname)
ded70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 70  ;.    }.  }.  *p
ded80 46 64 20 3d 20 66 64 3b 0a 20 20 72 65 74 75 72  Fd = fd;.  retur
ded90 6e 20 28 66 64 3e 3d 30 3f 53 51 4c 49 54 45 5f  n (fd>=0?SQLITE_
deda0 4f 4b 3a 53 51 4c 49 54 45 5f 43 41 4e 54 4f 50  OK:SQLITE_CANTOP
dedb0 45 4e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72  EN);.}../*.** Cr
dedc0 65 61 74 65 20 61 20 74 65 6d 70 6f 72 61 72 79  eate a temporary
dedd0 20 66 69 6c 65 20 6e 61 6d 65 20 69 6e 20 7a 42   file name in zB
dede0 75 66 2e 20 20 7a 42 75 66 20 6d 75 73 74 20 62  uf.  zBuf must b
dedf0 65 20 61 6c 6c 6f 63 61 74 65 64 0a 2a 2a 20 62  e allocated.** b
dee00 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 70 72  y the calling pr
dee10 6f 63 65 73 73 20 61 6e 64 20 6d 75 73 74 20 62  ocess and must b
dee20 65 20 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20  e big enough to 
dee30 68 6f 6c 64 20 61 74 20 6c 65 61 73 74 0a 2a 2a  hold at least.**
dee40 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
dee50 65 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74  e bytes..*/.stat
dee60 69 63 20 69 6e 74 20 67 65 74 54 65 6d 70 6e 61  ic int getTempna
dee70 6d 65 28 69 6e 74 20 6e 42 75 66 2c 20 63 68 61  me(int nBuf, cha
dee80 72 20 2a 7a 42 75 66 29 7b 0a 20 20 73 74 61 74  r *zBuf){.  stat
dee90 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 61  ic const char *a
deea0 7a 44 69 72 73 5b 5d 20 3d 20 7b 0a 20 20 20 20  zDirs[] = {.    
deeb0 20 30 2c 0a 20 20 20 20 20 22 2f 76 61 72 2f 74   0,.     "/var/t
deec0 6d 70 22 2c 0a 20 20 20 20 20 22 2f 75 73 72 2f  mp",.     "/usr/
deed0 74 6d 70 22 2c 0a 20 20 20 20 20 22 2f 74 6d 70  tmp",.     "/tmp
deee0 22 2c 0a 20 20 20 20 20 22 2e 22 2c 0a 20 20 7d  ",.     ".",.  }
deef0 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  ;.  static const
def00 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 7a   unsigned char z
def10 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22 61  Chars[] =.    "a
def20 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71  bcdefghijklmnopq
def30 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20 22  rstuvwxyz".    "
def40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50  ABCDEFGHIJKLMNOP
def50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20 20  QRSTUVWXYZ".    
def60 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20 20  "0123456789";.  
def70 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 74 72 75  int i, j;.  stru
def80 63 74 20 73 74 61 74 20 62 75 66 3b 0a 20 20 63  ct stat buf;.  c
def90 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 69 72 20  onst char *zDir 
defa0 3d 20 22 2e 22 3b 0a 0a 20 20 2f 2a 20 49 74 27  = ".";..  /* It'
defb0 73 20 6f 64 64 20 74 6f 20 73 69 6d 75 6c 61 74  s odd to simulat
defc0 65 20 61 6e 20 69 6f 2d 65 72 72 6f 72 20 68 65  e an io-error he
defd0 72 65 2c 20 62 75 74 20 72 65 61 6c 6c 79 20 74  re, but really t
defe0 68 69 73 20 69 73 20 6a 75 73 74 0a 20 20 2a 2a  his is just.  **
deff0 20 75 73 69 6e 67 20 74 68 65 20 69 6f 2d 65 72   using the io-er
df000 72 6f 72 20 69 6e 66 72 61 73 74 72 75 63 74 75  ror infrastructu
df010 72 65 20 74 6f 20 74 65 73 74 20 74 68 61 74 20  re to test that 
df020 53 51 4c 69 74 65 20 68 61 6e 64 6c 65 73 20 74  SQLite handles t
df030 68 69 73 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  his.  ** functio
df040 6e 20 66 61 69 6c 69 6e 67 2e 20 0a 20 20 2a 2f  n failing. .  */
df050 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
df060 6f 72 28 20 72 65 74 75 72 6e 20 53 51 4c 49 54  or( return SQLIT
df070 45 5f 49 4f 45 52 52 20 29 3b 0a 0a 20 20 61 7a  E_IOERR );..  az
df080 44 69 72 73 5b 30 5d 20 3d 20 73 71 6c 69 74 65  Dirs[0] = sqlite
df090 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f 72 79  3_temp_directory
df0a0 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41  ;.  for(i=0; i<A
df0b0 72 72 61 79 53 69 7a 65 28 61 7a 44 69 72 73 29  rraySize(azDirs)
df0c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
df0d0 61 7a 44 69 72 73 5b 69 5d 3d 3d 30 20 29 20 63  azDirs[i]==0 ) c
df0e0 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28  ontinue;.    if(
df0f0 20 73 74 61 74 28 61 7a 44 69 72 73 5b 69 5d 2c   stat(azDirs[i],
df100 20 26 62 75 66 29 20 29 20 63 6f 6e 74 69 6e 75   &buf) ) continu
df110 65 3b 0a 20 20 20 20 69 66 28 20 21 53 5f 49 53  e;.    if( !S_IS
df120 44 49 52 28 62 75 66 2e 73 74 5f 6d 6f 64 65 29  DIR(buf.st_mode)
df130 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
df140 20 69 66 28 20 61 63 63 65 73 73 28 61 7a 44 69   if( access(azDi
df150 72 73 5b 69 5d 2c 20 30 37 29 20 29 20 63 6f 6e  rs[i], 07) ) con
df160 74 69 6e 75 65 3b 0a 20 20 20 20 7a 44 69 72 20  tinue;.    zDir 
df170 3d 20 61 7a 44 69 72 73 5b 69 5d 3b 0a 20 20 20  = azDirs[i];.   
df180 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f   break;.  }..  /
df190 2a 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65  * Check that the
df1a0 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 20 69   output buffer i
df1b0 73 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 66  s large enough f
df1c0 6f 72 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79  or the temporary
df1d0 20 66 69 6c 65 20 0a 20 20 2a 2a 20 6e 61 6d 65   file .  ** name
df1e0 2e 20 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20  . If it is not, 
df1f0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
df200 52 4f 52 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ROR..  */.  if( 
df210 28 73 74 72 6c 65 6e 28 7a 44 69 72 29 20 2b 20  (strlen(zDir) + 
df220 73 74 72 6c 65 6e 28 53 51 4c 49 54 45 5f 54 45  strlen(SQLITE_TE
df230 4d 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 29 20  MP_FILE_PREFIX) 
df240 2b 20 31 37 29 20 3e 3d 20 28 73 69 7a 65 5f 74  + 17) >= (size_t
df250 29 6e 42 75 66 20 29 7b 0a 20 20 20 20 72 65 74  )nBuf ){.    ret
df260 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
df270 3b 0a 20 20 7d 0a 0a 20 20 64 6f 7b 0a 20 20 20  ;.  }..  do{.   
df280 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
df290 66 28 6e 42 75 66 2d 31 37 2c 20 7a 42 75 66 2c  f(nBuf-17, zBuf,
df2a0 20 22 25 73 2f 22 53 51 4c 49 54 45 5f 54 45 4d   "%s/"SQLITE_TEM
df2b0 50 5f 46 49 4c 45 5f 50 52 45 46 49 58 2c 20 7a  P_FILE_PREFIX, z
df2c0 44 69 72 29 3b 0a 20 20 20 20 6a 20 3d 20 73 74  Dir);.    j = st
df2d0 72 6c 65 6e 28 7a 42 75 66 29 3b 0a 20 20 20 20  rlen(zBuf);.    
df2e0 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65  sqlite3_randomne
df2f0 73 73 28 31 35 2c 20 26 7a 42 75 66 5b 6a 5d 29  ss(15, &zBuf[j])
df300 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
df310 3c 31 35 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a  <15; i++, j++){.
df320 20 20 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d 20        zBuf[j] = 
df330 28 63 68 61 72 29 7a 43 68 61 72 73 5b 20 28 28  (char)zChars[ ((
df340 75 6e 73 69 67 6e 65 64 20 63 68 61 72 29 7a 42  unsigned char)zB
df350 75 66 5b 6a 5d 29 25 28 73 69 7a 65 6f 66 28 7a  uf[j])%(sizeof(z
df360 43 68 61 72 73 29 2d 31 29 20 5d 3b 0a 20 20 20  Chars)-1) ];.   
df370 20 7d 0a 20 20 20 20 7a 42 75 66 5b 6a 5d 20 3d   }.    zBuf[j] =
df380 20 30 3b 0a 20 20 7d 77 68 69 6c 65 28 20 61 63   0;.  }while( ac
df390 63 65 73 73 28 7a 42 75 66 2c 30 29 3d 3d 30 20  cess(zBuf,0)==0 
df3a0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
df3b0 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  TE_OK;.}.../*.**
df3c0 20 4f 70 65 6e 20 74 68 65 20 66 69 6c 65 20 7a   Open the file z
df3d0 50 61 74 68 2e 0a 2a 2a 20 0a 2a 2a 20 50 72 65  Path..** .** Pre
df3e0 76 69 6f 75 73 6c 79 2c 20 74 68 65 20 53 51 4c  viously, the SQL
df3f0 69 74 65 20 4f 53 20 6c 61 79 65 72 20 75 73 65  ite OS layer use
df400 64 20 74 68 72 65 65 20 66 75 6e 63 74 69 6f 6e  d three function
df410 73 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 74 68  s in place of th
df420 69 73 0a 2a 2a 20 6f 6e 65 3a 0a 2a 2a 0a 2a 2a  is.** one:.**.**
df430 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f 70       sqlite3OsOp
df440 65 6e 52 65 61 64 57 72 69 74 65 28 29 3b 0a 2a  enReadWrite();.*
df450 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f  *     sqlite3OsO
df460 70 65 6e 52 65 61 64 4f 6e 6c 79 28 29 3b 0a 2a  penReadOnly();.*
df470 2a 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 4f  *     sqlite3OsO
df480 70 65 6e 45 78 63 6c 75 73 69 76 65 28 29 3b 0a  penExclusive();.
df490 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 63 61 6c 6c  **.** These call
df4a0 73 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  s correspond to 
df4b0 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f  the following co
df4c0 6d 62 69 6e 61 74 69 6f 6e 73 20 6f 66 20 66 6c  mbinations of fl
df4d0 61 67 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 52  ags:.**.**     R
df4e0 65 61 64 57 72 69 74 65 28 29 20 2d 3e 20 20 20  eadWrite() ->   
df4f0 20 20 28 52 45 41 44 57 52 49 54 45 20 7c 20 43    (READWRITE | C
df500 52 45 41 54 45 29 0a 2a 2a 20 20 20 20 20 52 65  REATE).**     Re
df510 61 64 4f 6e 6c 79 28 29 20 20 2d 3e 20 20 20 20  adOnly()  ->    
df520 20 28 52 45 41 44 4f 4e 4c 59 29 20 0a 2a 2a 20   (READONLY) .** 
df530 20 20 20 20 4f 70 65 6e 45 78 63 6c 75 73 69 76      OpenExclusiv
df540 65 28 29 20 2d 3e 20 28 52 45 41 44 57 52 49 54  e() -> (READWRIT
df550 45 20 7c 20 43 52 45 41 54 45 20 7c 20 45 58 43  E | CREATE | EXC
df560 4c 55 53 49 56 45 29 0a 2a 2a 0a 2a 2a 20 54 68  LUSIVE).**.** Th
df570 65 20 6f 6c 64 20 4f 70 65 6e 45 78 63 6c 75 73  e old OpenExclus
df580 69 76 65 28 29 20 61 63 63 65 70 74 65 64 20 61  ive() accepted a
df590 20 62 6f 6f 6c 65 61 6e 20 61 72 67 75 6d 65 6e   boolean argumen
df5a0 74 20 2d 20 22 64 65 6c 46 6c 61 67 22 2e 20 49  t - "delFlag". I
df5b0 66 0a 2a 2a 20 74 72 75 65 2c 20 74 68 65 20 66  f.** true, the f
df5c0 69 6c 65 20 77 61 73 20 63 6f 6e 66 69 67 75 72  ile was configur
df5d0 65 64 20 74 6f 20 62 65 20 61 75 74 6f 6d 61 74  ed to be automat
df5e0 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 64 20 77  ically deleted w
df5f0 68 65 6e 20 74 68 65 0a 2a 2a 20 66 69 6c 65 20  hen the.** file 
df600 68 61 6e 64 6c 65 20 63 6c 6f 73 65 64 2e 20 54  handle closed. T
df610 6f 20 61 63 68 69 65 76 65 20 74 68 65 20 73 61  o achieve the sa
df620 6d 65 20 65 66 66 65 63 74 20 75 73 69 6e 67 20  me effect using 
df630 74 68 69 73 20 6e 65 77 20 0a 2a 2a 20 69 6e 74  this new .** int
df640 65 72 66 61 63 65 2c 20 61 64 64 20 74 68 65 20  erface, add the 
df650 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 66 6c  DELETEONCLOSE fl
df660 61 67 20 74 6f 20 74 68 6f 73 65 20 73 70 65 63  ag to those spec
df670 69 66 69 65 64 20 61 62 6f 76 65 20 66 6f 72 20  ified above for 
df680 0a 2a 2a 20 4f 70 65 6e 45 78 63 6c 75 73 69 76  .** OpenExclusiv
df690 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  e()..*/.static i
df6a0 6e 74 20 75 6e 69 78 4f 70 65 6e 28 0a 20 20 73  nt unixOpen(.  s
df6b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
df6c0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
df6d0 2a 7a 50 61 74 68 2c 20 0a 20 20 73 71 6c 69 74  *zPath, .  sqlit
df6e0 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c 65 2c 0a  e3_file *pFile,.
df6f0 20 20 69 6e 74 20 66 6c 61 67 73 2c 0a 20 20 69    int flags,.  i
df700 6e 74 20 2a 70 4f 75 74 46 6c 61 67 73 0a 29 7b  nt *pOutFlags.){
df710 0a 20 20 69 6e 74 20 66 64 20 3d 20 30 3b 20 20  .  int fd = 0;  
df720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
df730 20 20 2f 2a 20 46 69 6c 65 20 64 65 73 63 72 69    /* File descri
df740 70 74 6f 72 20 72 65 74 75 72 6e 65 64 20 62 79  ptor returned by
df750 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74   open() */.  int
df760 20 64 69 72 66 64 20 3d 20 2d 31 3b 20 20 20 20   dirfd = -1;    
df770 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
df780 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64 65  irectory file de
df790 73 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 69 6e  scriptor */.  in
df7a0 74 20 6f 66 6c 61 67 73 20 3d 20 30 3b 20 20 20  t oflags = 0;   
df7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
df7c0 46 6c 61 67 73 20 74 6f 20 70 61 73 73 20 74 6f  Flags to pass to
df7d0 20 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e 74   open() */.  int
df7e0 20 65 54 79 70 65 20 3d 20 66 6c 61 67 73 26 30   eType = flags&0
df7f0 78 46 46 46 46 46 46 30 30 3b 20 20 2f 2a 20 54  xFFFFFF00;  /* T
df800 79 70 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 6f  ype of file to o
df810 70 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 4c  pen */.  int noL
df820 6f 63 6b 3b 20 20 20 20 20 20 20 20 20 20 20 20  ock;            
df830 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
df840 74 6f 20 6f 6d 69 74 20 6c 6f 63 6b 69 6e 67 20  to omit locking 
df850 70 72 69 6d 69 74 69 76 65 73 20 2a 2f 0a 0a 20  primitives */.. 
df860 20 69 6e 74 20 69 73 45 78 63 6c 75 73 69 76 65   int isExclusive
df870 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
df880 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
df890 56 45 29 3b 0a 20 20 69 6e 74 20 69 73 44 65 6c  VE);.  int isDel
df8a0 65 74 65 20 20 20 20 20 3d 20 28 66 6c 61 67 73  ete     = (flags
df8b0 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44   & SQLITE_OPEN_D
df8c0 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 29 3b 0a 20  ELETEONCLOSE);. 
df8d0 20 69 6e 74 20 69 73 43 72 65 61 74 65 20 20 20   int isCreate   
df8e0 20 20 3d 20 28 66 6c 61 67 73 20 26 20 53 51 4c    = (flags & SQL
df8f0 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29  ITE_OPEN_CREATE)
df900 3b 0a 20 20 69 6e 74 20 69 73 52 65 61 64 6f 6e  ;.  int isReadon
df910 6c 79 20 20 20 3d 20 28 66 6c 61 67 73 20 26 20  ly   = (flags & 
df920 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
df930 4f 4e 4c 59 29 3b 0a 20 20 69 6e 74 20 69 73 52  ONLY);.  int isR
df940 65 61 64 57 72 69 74 65 20 20 3d 20 28 66 6c 61  eadWrite  = (fla
df950 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e  gs & SQLITE_OPEN
df960 5f 52 45 41 44 57 52 49 54 45 29 3b 0a 0a 20 20  _READWRITE);..  
df970 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67 20 61  /* If creating a
df980 20 6d 61 73 74 65 72 20 6f 72 20 6d 61 69 6e 2d   master or main-
df990 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 68  file journal, th
df9a0 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  is function will
df9b0 20 6f 70 65 6e 0a 20 20 2a 2a 20 61 20 66 69 6c   open.  ** a fil
df9c0 65 2d 64 65 73 63 72 69 70 74 6f 72 20 6f 6e 20  e-descriptor on 
df9d0 74 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 6f  the directory to
df9e0 6f 2e 20 54 68 65 20 66 69 72 73 74 20 74 69 6d  o. The first tim
df9f0 65 20 75 6e 69 78 53 79 6e 63 28 29 0a 20 20 2a  e unixSync().  *
dfa00 2a 20 69 73 20 63 61 6c 6c 65 64 20 74 68 65 20  * is called the 
dfa10 64 69 72 65 63 74 6f 72 79 20 66 69 6c 65 20 64  directory file d
dfa20 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62  escriptor will b
dfa30 65 20 66 73 79 6e 63 28 29 65 64 20 61 6e 64 20  e fsync()ed and 
dfa40 63 6c 6f 73 65 28 29 64 2e 0a 20 20 2a 2f 0a 20  close()d..  */. 
dfa50 20 69 6e 74 20 69 73 4f 70 65 6e 44 69 72 65 63   int isOpenDirec
dfa60 74 6f 72 79 20 3d 20 28 69 73 43 72 65 61 74 65  tory = (isCreate
dfa70 20 26 26 20 0a 20 20 20 20 20 20 28 65 54 79 70   && .      (eTyp
dfa80 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  e==SQLITE_OPEN_M
dfa90 41 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 7c  ASTER_JOURNAL ||
dfaa0 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f   eType==SQLITE_O
dfab0 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c  PEN_MAIN_JOURNAL
dfac0 29 0a 20 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  ).  );..  /* If 
dfad0 61 72 67 75 6d 65 6e 74 20 7a 50 61 74 68 20 69  argument zPath i
dfae0 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  s a NULL pointer
dfaf0 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
dfb00 69 73 20 72 65 71 75 69 72 65 64 20 74 6f 20 6f  is required to o
dfb10 70 65 6e 0a 20 20 2a 2a 20 61 20 74 65 6d 70 6f  pen.  ** a tempo
dfb20 72 61 72 79 20 66 69 6c 65 2e 20 55 73 65 20 74  rary file. Use t
dfb30 68 69 73 20 62 75 66 66 65 72 20 74 6f 20 73 74  his buffer to st
dfb40 6f 72 65 20 74 68 65 20 66 69 6c 65 20 6e 61 6d  ore the file nam
dfb50 65 20 69 6e 2e 0a 20 20 2a 2f 0a 20 20 63 68 61  e in..  */.  cha
dfb60 72 20 7a 54 6d 70 6e 61 6d 65 5b 4d 41 58 5f 50  r zTmpname[MAX_P
dfb70 41 54 48 4e 41 4d 45 2b 31 5d 3b 0a 20 20 63 6f  ATHNAME+1];.  co
dfb80 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20  nst char *zName 
dfb90 3d 20 7a 50 61 74 68 3b 0a 0a 20 20 2f 2a 20 43  = zPath;..  /* C
dfba0 68 65 63 6b 20 74 68 65 20 66 6f 6c 6c 6f 77 69  heck the followi
dfbb0 6e 67 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  ng statements ar
dfbc0 65 20 74 72 75 65 3a 20 0a 20 20 2a 2a 0a 20 20  e true: .  **.  
dfbd0 2a 2a 20 20 20 28 61 29 20 45 78 61 63 74 6c 79  **   (a) Exactly
dfbe0 20 6f 6e 65 20 6f 66 20 74 68 65 20 52 45 41 44   one of the READ
dfbf0 57 52 49 54 45 20 61 6e 64 20 52 45 41 44 4f 4e  WRITE and READON
dfc00 4c 59 20 66 6c 61 67 73 20 6d 75 73 74 20 62 65  LY flags must be
dfc10 20 73 65 74 2c 20 61 6e 64 20 0a 20 20 2a 2a 20   set, and .  ** 
dfc20 20 20 28 62 29 20 69 66 20 43 52 45 41 54 45 20    (b) if CREATE 
dfc30 69 73 20 73 65 74 2c 20 74 68 65 6e 20 52 45 41  is set, then REA
dfc40 44 57 52 49 54 45 20 6d 75 73 74 20 61 6c 73 6f  DWRITE must also
dfc50 20 62 65 20 73 65 74 2c 20 61 6e 64 0a 20 20 2a   be set, and.  *
dfc60 2a 20 20 20 28 63 29 20 69 66 20 45 58 43 4c 55  *   (c) if EXCLU
dfc70 53 49 56 45 20 69 73 20 73 65 74 2c 20 74 68 65  SIVE is set, the
dfc80 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20 61 6c  n CREATE must al
dfc90 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a 2a 20  so be set..  ** 
dfca0 20 20 28 64 29 20 69 66 20 44 45 4c 45 54 45 4f    (d) if DELETEO
dfcb0 4e 43 4c 4f 53 45 20 69 73 20 73 65 74 2c 20 74  NCLOSE is set, t
dfcc0 68 65 6e 20 43 52 45 41 54 45 20 6d 75 73 74 20  hen CREATE must 
dfcd0 61 6c 73 6f 20 62 65 20 73 65 74 2e 0a 20 20 2a  also be set..  *
dfce0 2f 0a 20 20 61 73 73 65 72 74 28 28 69 73 52 65  /.  assert((isRe
dfcf0 61 64 6f 6e 6c 79 3d 3d 30 20 7c 7c 20 69 73 52  adonly==0 || isR
dfd00 65 61 64 57 72 69 74 65 3d 3d 30 29 20 26 26 20  eadWrite==0) && 
dfd10 28 69 73 52 65 61 64 57 72 69 74 65 20 7c 7c 20  (isReadWrite || 
dfd20 69 73 52 65 61 64 6f 6e 6c 79 29 29 3b 0a 20 20  isReadonly));.  
dfd30 61 73 73 65 72 74 28 69 73 43 72 65 61 74 65 3d  assert(isCreate=
dfd40 3d 30 20 7c 7c 20 69 73 52 65 61 64 57 72 69 74  =0 || isReadWrit
dfd50 65 29 3b 0a 20 20 61 73 73 65 72 74 28 69 73 45  e);.  assert(isE
dfd60 78 63 6c 75 73 69 76 65 3d 3d 30 20 7c 7c 20 69  xclusive==0 || i
dfd70 73 43 72 65 61 74 65 29 3b 0a 20 20 61 73 73 65  sCreate);.  asse
dfd80 72 74 28 69 73 44 65 6c 65 74 65 3d 3d 30 20 7c  rt(isDelete==0 |
dfd90 7c 20 69 73 43 72 65 61 74 65 29 3b 0a 0a 20 20  | isCreate);..  
dfda0 2f 2a 20 54 68 65 20 6d 61 69 6e 20 44 42 2c 20  /* The main DB, 
dfdb0 6d 61 69 6e 20 6a 6f 75 72 6e 61 6c 2c 20 61 6e  main journal, an
dfdc0 64 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  d master journal
dfdd0 20 61 72 65 20 6e 65 76 65 72 20 61 75 74 6f 6d   are never autom
dfde0 61 74 69 63 61 6c 6c 79 0a 20 20 2a 2a 20 64 65  atically.  ** de
dfdf0 6c 65 74 65 64 0a 20 20 2a 2f 0a 20 20 61 73 73  leted.  */.  ass
dfe00 65 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49  ert( eType!=SQLI
dfe10 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 20  TE_OPEN_MAIN_DB 
dfe20 7c 7c 20 21 69 73 44 65 6c 65 74 65 20 29 3b 0a  || !isDelete );.
dfe30 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21    assert( eType!
dfe40 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49  =SQLITE_OPEN_MAI
dfe50 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 7c 20 21 69 73  N_JOURNAL || !is
dfe60 44 65 6c 65 74 65 20 29 3b 0a 20 20 61 73 73 65  Delete );.  asse
dfe70 72 74 28 20 65 54 79 70 65 21 3d 53 51 4c 49 54  rt( eType!=SQLIT
dfe80 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
dfe90 55 52 4e 41 4c 20 7c 7c 20 21 69 73 44 65 6c 65  URNAL || !isDele
dfea0 74 65 20 29 3b 0a 0a 20 20 2f 2a 20 41 73 73 65  te );..  /* Asse
dfeb0 72 74 20 74 68 61 74 20 74 68 65 20 75 70 70 65  rt that the uppe
dfec0 72 20 6c 61 79 65 72 20 68 61 73 20 73 65 74 20  r layer has set 
dfed0 6f 6e 65 20 6f 66 20 74 68 65 20 22 66 69 6c 65  one of the "file
dfee0 2d 74 79 70 65 22 20 66 6c 61 67 73 2e 20 2a 2f  -type" flags. */
dfef0 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
dff00 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  ==SQLITE_OPEN_MA
dff10 49 4e 5f 44 42 20 20 20 20 20 20 7c 7c 20 65 54  IN_DB      || eT
dff20 79 70 65 3d 3d 53 51 4c 49 54 45 5f 4f 50 45 4e  ype==SQLITE_OPEN
dff30 5f 54 45 4d 50 5f 44 42 20 0a 20 20 20 20 20 20  _TEMP_DB .      
dff40 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54   || eType==SQLIT
dff50 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52  E_OPEN_MAIN_JOUR
dff60 4e 41 4c 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51  NAL || eType==SQ
dff70 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a  LITE_OPEN_TEMP_J
dff80 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c  OURNAL .       |
dff90 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
dffa0 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20  OPEN_SUBJOURNAL 
dffb0 20 20 7c 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49    || eType==SQLI
dffc0 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a  TE_OPEN_MASTER_J
dffd0 4f 55 52 4e 41 4c 20 0a 20 20 20 20 20 20 20 7c  OURNAL .       |
dffe0 7c 20 65 54 79 70 65 3d 3d 53 51 4c 49 54 45 5f  | eType==SQLITE_
dfff0 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44  OPEN_TRANSIENT_D
e0000 42 0a 20 20 29 3b 0a 0a 20 20 6d 65 6d 73 65 74  B.  );..  memset
e0010 28 70 46 69 6c 65 2c 20 30 2c 20 73 69 7a 65 6f  (pFile, 0, sizeo
e0020 66 28 75 6e 69 78 46 69 6c 65 29 29 3b 0a 0a 20  f(unixFile));.. 
e0030 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 7b 0a 20   if( !zName ){. 
e0040 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61     int rc;.    a
e0050 73 73 65 72 74 28 69 73 44 65 6c 65 74 65 20 26  ssert(isDelete &
e0060 26 20 21 69 73 4f 70 65 6e 44 69 72 65 63 74 6f  & !isOpenDirecto
e0070 72 79 29 3b 0a 20 20 20 20 72 63 20 3d 20 67 65  ry);.    rc = ge
e0080 74 54 65 6d 70 6e 61 6d 65 28 4d 41 58 5f 50 41  tTempname(MAX_PA
e0090 54 48 4e 41 4d 45 2b 31 2c 20 7a 54 6d 70 6e 61  THNAME+1, zTmpna
e00a0 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  me);.    if( rc!
e00b0 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
e00c0 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
e00d0 20 20 20 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d     }.    zName =
e00e0 20 7a 54 6d 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a   zTmpname;.  }..
e00f0 20 20 69 66 28 20 69 73 52 65 61 64 6f 6e 6c 79    if( isReadonly
e0100 20 29 20 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f   )  oflags |= O_
e0110 52 44 4f 4e 4c 59 3b 0a 20 20 69 66 28 20 69 73  RDONLY;.  if( is
e0120 52 65 61 64 57 72 69 74 65 20 29 20 6f 66 6c 61  ReadWrite ) ofla
e0130 67 73 20 7c 3d 20 4f 5f 52 44 57 52 3b 0a 20 20  gs |= O_RDWR;.  
e0140 69 66 28 20 69 73 43 72 65 61 74 65 20 29 20 20  if( isCreate )  
e0150 20 20 6f 66 6c 61 67 73 20 7c 3d 20 4f 5f 43 52    oflags |= O_CR
e0160 45 41 54 3b 0a 20 20 69 66 28 20 69 73 45 78 63  EAT;.  if( isExc
e0170 6c 75 73 69 76 65 20 29 20 6f 66 6c 61 67 73 20  lusive ) oflags 
e0180 7c 3d 20 28 4f 5f 45 58 43 4c 7c 4f 5f 4e 4f 46  |= (O_EXCL|O_NOF
e0190 4f 4c 4c 4f 57 29 3b 0a 20 20 6f 66 6c 61 67 73  OLLOW);.  oflags
e01a0 20 7c 3d 20 28 4f 5f 4c 41 52 47 45 46 49 4c 45   |= (O_LARGEFILE
e01b0 7c 4f 5f 42 49 4e 41 52 59 29 3b 0a 0a 20 20 66  |O_BINARY);..  f
e01c0 64 20 3d 20 6f 70 65 6e 28 7a 4e 61 6d 65 2c 20  d = open(zName, 
e01d0 6f 66 6c 61 67 73 2c 20 69 73 44 65 6c 65 74 65  oflags, isDelete
e01e0 3f 30 36 30 30 3a 53 51 4c 49 54 45 5f 44 45 46  ?0600:SQLITE_DEF
e01f0 41 55 4c 54 5f 46 49 4c 45 5f 50 45 52 4d 49 53  AULT_FILE_PERMIS
e0200 53 49 4f 4e 53 29 3b 0a 20 20 4f 53 54 52 41 43  SIONS);.  OSTRAC
e0210 45 34 28 22 4f 50 45 4e 58 20 20 20 25 2d 33 64  E4("OPENX   %-3d
e0220 20 25 73 20 30 25 6f 5c 6e 22 2c 20 66 64 2c 20   %s 0%o\n", fd, 
e0230 7a 4e 61 6d 65 2c 20 6f 66 6c 61 67 73 29 3b 0a  zName, oflags);.
e0240 20 20 69 66 28 20 66 64 3c 30 20 26 26 20 65 72    if( fd<0 && er
e0250 72 6e 6f 21 3d 45 49 53 44 49 52 20 26 26 20 69  rno!=EISDIR && i
e0260 73 52 65 61 64 57 72 69 74 65 20 26 26 20 21 69  sReadWrite && !i
e0270 73 45 78 63 6c 75 73 69 76 65 20 29 7b 0a 20 20  sExclusive ){.  
e0280 20 20 2f 2a 20 46 61 69 6c 65 64 20 74 6f 20 6f    /* Failed to o
e0290 70 65 6e 20 74 68 65 20 66 69 6c 65 20 66 6f 72  pen the file for
e02a0 20 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65   read/write acce
e02b0 73 73 2e 20 54 72 79 20 72 65 61 64 2d 6f 6e 6c  ss. Try read-onl
e02c0 79 2e 20 2a 2f 0a 20 20 20 20 66 6c 61 67 73 20  y. */.    flags 
e02d0 26 3d 20 7e 28 53 51 4c 49 54 45 5f 4f 50 45 4e  &= ~(SQLITE_OPEN
e02e0 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
e02f0 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 29 3b 0a  E_OPEN_CREATE);.
e0300 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 53 51 4c      flags |= SQL
e0310 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c  ITE_OPEN_READONL
e0320 59 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 75 6e  Y;.    return un
e0330 69 78 4f 70 65 6e 28 70 56 66 73 2c 20 7a 50 61  ixOpen(pVfs, zPa
e0340 74 68 2c 20 70 46 69 6c 65 2c 20 66 6c 61 67 73  th, pFile, flags
e0350 2c 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20  , pOutFlags);.  
e0360 7d 0a 20 20 69 66 28 20 66 64 3c 30 20 29 7b 0a  }.  if( fd<0 ){.
e0370 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
e0380 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a  E_CANTOPEN;.  }.
e0390 20 20 69 66 28 20 69 73 44 65 6c 65 74 65 20 29    if( isDelete )
e03a0 7b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53  {.#if IS_VXWORKS
e03b0 0a 20 20 20 20 7a 50 61 74 68 20 3d 20 7a 4e 61  .    zPath = zNa
e03c0 6d 65 3b 0a 23 65 6c 73 65 0a 20 20 20 20 75 6e  me;.#else.    un
e03d0 6c 69 6e 6b 28 7a 4e 61 6d 65 29 3b 0a 23 65 6e  link(zName);.#en
e03e0 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 70 4f  dif.  }.  if( pO
e03f0 75 74 46 6c 61 67 73 20 29 7b 0a 20 20 20 20 2a  utFlags ){.    *
e0400 70 4f 75 74 46 6c 61 67 73 20 3d 20 66 6c 61 67  pOutFlags = flag
e0410 73 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  s;.  }..  assert
e0420 28 66 64 21 3d 30 29 3b 0a 20 20 69 66 28 20 69  (fd!=0);.  if( i
e0430 73 4f 70 65 6e 44 69 72 65 63 74 6f 72 79 20 29  sOpenDirectory )
e0440 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 6f  {.    int rc = o
e0450 70 65 6e 44 69 72 65 63 74 6f 72 79 28 7a 50 61  penDirectory(zPa
e0460 74 68 2c 20 26 64 69 72 66 64 29 3b 0a 20 20 20  th, &dirfd);.   
e0470 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
e0480 4f 4b 20 29 7b 0a 20 20 20 20 20 20 63 6c 6f 73  OK ){.      clos
e0490 65 28 66 64 29 3b 0a 20 20 20 20 20 20 72 65 74  e(fd);.      ret
e04a0 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
e04b0 7d 0a 0a 23 69 66 64 65 66 20 46 44 5f 43 4c 4f  }..#ifdef FD_CLO
e04c0 45 58 45 43 0a 20 20 66 63 6e 74 6c 28 66 64 2c  EXEC.  fcntl(fd,
e04d0 20 46 5f 53 45 54 46 44 2c 20 66 63 6e 74 6c 28   F_SETFD, fcntl(
e04e0 66 64 2c 20 46 5f 47 45 54 46 44 2c 20 30 29 20  fd, F_GETFD, 0) 
e04f0 7c 20 46 44 5f 43 4c 4f 45 58 45 43 29 3b 0a 23  | FD_CLOEXEC);.#
e0500 65 6e 64 69 66 0a 0a 20 20 6e 6f 4c 6f 63 6b 20  endif..  noLock 
e0510 3d 20 65 54 79 70 65 21 3d 53 51 4c 49 54 45 5f  = eType!=SQLITE_
e0520 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42 3b 0a 20 20  OPEN_MAIN_DB;.  
e0530 72 65 74 75 72 6e 20 66 69 6c 6c 49 6e 55 6e 69  return fillInUni
e0540 78 46 69 6c 65 28 70 56 66 73 2c 20 66 64 2c 20  xFile(pVfs, fd, 
e0550 64 69 72 66 64 2c 20 70 46 69 6c 65 2c 20 7a 50  dirfd, pFile, zP
e0560 61 74 68 2c 20 6e 6f 4c 6f 63 6b 2c 20 69 73 44  ath, noLock, isD
e0570 65 6c 65 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  elete);.}../*.**
e0580 20 44 65 6c 65 74 65 20 74 68 65 20 66 69 6c 65   Delete the file
e0590 20 61 74 20 7a 50 61 74 68 2e 20 49 66 20 74 68   at zPath. If th
e05a0 65 20 64 69 72 53 79 6e 63 20 61 72 67 75 6d 65  e dirSync argume
e05b0 6e 74 20 69 73 20 74 72 75 65 2c 20 66 73 79 6e  nt is true, fsyn
e05c0 63 28 29 0a 2a 2a 20 74 68 65 20 64 69 72 65 63  c().** the direc
e05d0 74 6f 72 79 20 61 66 74 65 72 20 64 65 6c 65 74  tory after delet
e05e0 69 6e 67 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ing the file..*/
e05f0 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78  .static int unix
e0600 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 5f 76  Delete(sqlite3_v
e0610 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 63 6f 6e  fs *NotUsed, con
e0620 73 74 20 63 68 61 72 20 2a 7a 50 61 74 68 2c 20  st char *zPath, 
e0630 69 6e 74 20 64 69 72 53 79 6e 63 29 7b 0a 20 20  int dirSync){.  
e0640 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
e0650 4f 4b 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  OK;.  UNUSED_PAR
e0660 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e0670 0a 20 20 53 69 6d 75 6c 61 74 65 49 4f 45 72 72  .  SimulateIOErr
e0680 6f 72 28 72 65 74 75 72 6e 20 53 51 4c 49 54 45  or(return SQLITE
e0690 5f 49 4f 45 52 52 5f 44 45 4c 45 54 45 29 3b 0a  _IOERR_DELETE);.
e06a0 20 20 75 6e 6c 69 6e 6b 28 7a 50 61 74 68 29 3b    unlink(zPath);
e06b0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
e06c0 44 49 53 41 42 4c 45 5f 44 49 52 53 59 4e 43 0a  DISABLE_DIRSYNC.
e06d0 20 20 69 66 28 20 64 69 72 53 79 6e 63 20 29 7b    if( dirSync ){
e06e0 0a 20 20 20 20 69 6e 74 20 66 64 3b 0a 20 20 20  .    int fd;.   
e06f0 20 72 63 20 3d 20 6f 70 65 6e 44 69 72 65 63 74   rc = openDirect
e0700 6f 72 79 28 7a 50 61 74 68 2c 20 26 66 64 29 3b  ory(zPath, &fd);
e0710 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
e0720 49 54 45 5f 4f 4b 20 29 7b 0a 23 69 66 20 49 53  ITE_OK ){.#if IS
e0730 5f 56 58 57 4f 52 4b 53 0a 20 20 20 20 20 20 69  _VXWORKS.      i
e0740 66 28 20 66 73 79 6e 63 28 66 64 29 3d 3d 2d 31  f( fsync(fd)==-1
e0750 20 29 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 69   ).#else.      i
e0760 66 28 20 66 73 79 6e 63 28 66 64 29 20 29 0a 23  f( fsync(fd) ).#
e0770 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20 20  endif.      {.  
e0780 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
e0790 45 5f 49 4f 45 52 52 5f 44 49 52 5f 46 53 59 4e  E_IOERR_DIR_FSYN
e07a0 43 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  C;.      }.     
e07b0 20 63 6c 6f 73 65 28 66 64 29 3b 0a 20 20 20 20   close(fd);.    
e07c0 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  }.  }.#endif.  r
e07d0 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
e07e0 2a 2a 20 54 65 73 74 20 74 68 65 20 65 78 69 73  ** Test the exis
e07f0 74 61 6e 63 65 20 6f 66 20 6f 72 20 61 63 63 65  tance of or acce
e0800 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 73 20 6f  ss permissions o
e0810 66 20 66 69 6c 65 20 7a 50 61 74 68 2e 20 54 68  f file zPath. Th
e0820 65 0a 2a 2a 20 74 65 73 74 20 70 65 72 66 6f 72  e.** test perfor
e0830 6d 65 64 20 64 65 70 65 6e 64 73 20 6f 6e 20 74  med depends on t
e0840 68 65 20 76 61 6c 75 65 20 6f 66 20 66 6c 61 67  he value of flag
e0850 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 51 4c  s:.**.**     SQL
e0860 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49 53 54  ITE_ACCESS_EXIST
e0870 53 3a 20 52 65 74 75 72 6e 20 31 20 69 66 20 74  S: Return 1 if t
e0880 68 65 20 66 69 6c 65 20 65 78 69 73 74 73 0a 2a  he file exists.*
e0890 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 41 43 43  *     SQLITE_ACC
e08a0 45 53 53 5f 52 45 41 44 57 52 49 54 45 3a 20 52  ESS_READWRITE: R
e08b0 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 66  eturn 1 if the f
e08c0 69 6c 65 20 69 73 20 72 65 61 64 20 61 6e 64 20  ile is read and 
e08d0 77 72 69 74 61 62 6c 65 2e 0a 2a 2a 20 20 20 20  writable..**    
e08e0 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52   SQLITE_ACCESS_R
e08f0 45 41 44 4f 4e 4c 59 3a 20 52 65 74 75 72 6e 20  EADONLY: Return 
e0900 31 20 69 66 20 74 68 65 20 66 69 6c 65 20 69 73  1 if the file is
e0910 20 72 65 61 64 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a   readable..**.**
e0920 20 4f 74 68 65 72 77 69 73 65 20 72 65 74 75 72   Otherwise retur
e0930 6e 20 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  n 0..*/.static i
e0940 6e 74 20 75 6e 69 78 41 63 63 65 73 73 28 0a 20  nt unixAccess(. 
e0950 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f   sqlite3_vfs *No
e0960 74 55 73 65 64 2c 20 0a 20 20 63 6f 6e 73 74 20  tUsed, .  const 
e0970 63 68 61 72 20 2a 7a 50 61 74 68 2c 20 0a 20 20  char *zPath, .  
e0980 69 6e 74 20 66 6c 61 67 73 2c 20 0a 20 20 69 6e  int flags, .  in
e0990 74 20 2a 70 52 65 73 4f 75 74 0a 29 7b 0a 20 20  t *pResOut.){.  
e09a0 69 6e 74 20 61 6d 6f 64 65 20 3d 20 30 3b 0a 20  int amode = 0;. 
e09b0 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e09c0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 53 69  R(NotUsed);.  Si
e09d0 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 20 72  mulateIOError( r
e09e0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
e09f0 52 52 5f 41 43 43 45 53 53 3b 20 29 3b 0a 20 20  RR_ACCESS; );.  
e0a00 73 77 69 74 63 68 28 20 66 6c 61 67 73 20 29 7b  switch( flags ){
e0a10 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e0a20 5f 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a  _ACCESS_EXISTS:.
e0a30 20 20 20 20 20 20 61 6d 6f 64 65 20 3d 20 46 5f        amode = F_
e0a40 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  OK;.      break;
e0a50 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
e0a60 5f 41 43 43 45 53 53 5f 52 45 41 44 57 52 49 54  _ACCESS_READWRIT
e0a70 45 3a 0a 20 20 20 20 20 20 61 6d 6f 64 65 20 3d  E:.      amode =
e0a80 20 57 5f 4f 4b 7c 52 5f 4f 4b 3b 0a 20 20 20 20   W_OK|R_OK;.    
e0a90 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
e0aa0 65 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f  e SQLITE_ACCESS_
e0ab0 52 45 41 44 3a 0a 20 20 20 20 20 20 61 6d 6f 64  READ:.      amod
e0ac0 65 20 3d 20 52 5f 4f 4b 3b 0a 20 20 20 20 20 20  e = R_OK;.      
e0ad0 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61  break;..    defa
e0ae0 75 6c 74 3a 0a 20 20 20 20 20 20 61 73 73 65 72  ult:.      asser
e0af0 74 28 21 22 49 6e 76 61 6c 69 64 20 66 6c 61 67  t(!"Invalid flag
e0b00 73 20 61 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20  s argument");.  
e0b10 7d 0a 20 20 2a 70 52 65 73 4f 75 74 20 3d 20 28  }.  *pResOut = (
e0b20 61 63 63 65 73 73 28 7a 50 61 74 68 2c 20 61 6d  access(zPath, am
e0b30 6f 64 65 29 3d 3d 30 29 3b 0a 20 20 72 65 74 75  ode)==0);.  retu
e0b40 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
e0b50 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e 20 61 20 72  ../*.** Turn a r
e0b60 65 6c 61 74 69 76 65 20 70 61 74 68 6e 61 6d 65  elative pathname
e0b70 20 69 6e 74 6f 20 61 20 66 75 6c 6c 20 70 61 74   into a full pat
e0b80 68 6e 61 6d 65 2e 20 54 68 65 20 72 65 6c 61 74  hname. The relat
e0b90 69 76 65 20 70 61 74 68 0a 2a 2a 20 69 73 20 73  ive path.** is s
e0ba0 74 6f 72 65 64 20 61 73 20 61 20 6e 75 6c 2d 74  tored as a nul-t
e0bb0 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e 67  erminated string
e0bc0 20 69 6e 20 74 68 65 20 62 75 66 66 65 72 20 70   in the buffer p
e0bd0 6f 69 6e 74 65 64 20 74 6f 20 62 79 0a 2a 2a 20  ointed to by.** 
e0be0 7a 50 61 74 68 2e 20 0a 2a 2a 0a 2a 2a 20 7a 4f  zPath. .**.** zO
e0bf0 75 74 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 62  ut points to a b
e0c00 75 66 66 65 72 20 6f 66 20 61 74 20 6c 65 61 73  uffer of at leas
e0c10 74 20 73 71 6c 69 74 65 33 5f 76 66 73 2e 6d 78  t sqlite3_vfs.mx
e0c20 50 61 74 68 6e 61 6d 65 20 62 79 74 65 73 20 0a  Pathname bytes .
e0c30 2a 2a 20 28 69 6e 20 74 68 69 73 20 63 61 73 65  ** (in this case
e0c40 2c 20 4d 41 58 5f 50 41 54 48 4e 41 4d 45 20 62  , MAX_PATHNAME b
e0c50 79 74 65 73 29 2e 20 54 68 65 20 66 75 6c 6c 2d  ytes). The full-
e0c60 70 61 74 68 20 69 73 20 77 72 69 74 74 65 6e 20  path is written 
e0c70 74 6f 0a 2a 2a 20 74 68 69 73 20 62 75 66 66 65  to.** this buffe
e0c80 72 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69  r before returni
e0c90 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ng..*/.static in
e0ca0 74 20 75 6e 69 78 46 75 6c 6c 50 61 74 68 6e 61  t unixFullPathna
e0cb0 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  me(.  sqlite3_vf
e0cc0 73 20 2a 70 56 66 73 2c 20 20 20 20 20 20 20 20  s *pVfs,        
e0cd0 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74      /* Pointer t
e0ce0 6f 20 76 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a  o vfs object */.
e0cf0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 50    const char *zP
e0d00 61 74 68 2c 20 20 20 20 20 20 20 20 20 20 20 20  ath,            
e0d10 2f 2a 20 50 6f 73 73 69 62 6c 79 20 72 65 6c 61  /* Possibly rela
e0d20 74 69 76 65 20 69 6e 70 75 74 20 70 61 74 68 20  tive input path 
e0d30 2a 2f 0a 20 20 69 6e 74 20 6e 4f 75 74 2c 20 20  */.  int nOut,  
e0d40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e0d50 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75     /* Size of ou
e0d60 74 70 75 74 20 62 75 66 66 65 72 20 69 6e 20 62  tput buffer in b
e0d70 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ytes */.  char *
e0d80 7a 4f 75 74 20 20 20 20 20 20 20 20 20 20 20 20  zOut            
e0d90 20 20 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75          /* Outpu
e0da0 74 20 62 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a  t buffer */.){..
e0db0 20 20 2f 2a 20 49 74 27 73 20 6f 64 64 20 74 6f    /* It's odd to
e0dc0 20 73 69 6d 75 6c 61 74 65 20 61 6e 20 69 6f 2d   simulate an io-
e0dd0 65 72 72 6f 72 20 68 65 72 65 2c 20 62 75 74 20  error here, but 
e0de0 72 65 61 6c 6c 79 20 74 68 69 73 20 69 73 20 6a  really this is j
e0df0 75 73 74 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74  ust.  ** using t
e0e00 68 65 20 69 6f 2d 65 72 72 6f 72 20 69 6e 66 72  he io-error infr
e0e10 61 73 74 72 75 63 74 75 72 65 20 74 6f 20 74 65  astructure to te
e0e20 73 74 20 74 68 61 74 20 53 51 4c 69 74 65 20 68  st that SQLite h
e0e30 61 6e 64 6c 65 73 20 74 68 69 73 0a 20 20 2a 2a  andles this.  **
e0e40 20 66 75 6e 63 74 69 6f 6e 20 66 61 69 6c 69 6e   function failin
e0e50 67 2e 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  g. This function
e0e60 20 63 6f 75 6c 64 20 66 61 69 6c 20 69 66 2c 20   could fail if, 
e0e70 66 6f 72 20 65 78 61 6d 70 6c 65 2c 20 74 68 65  for example, the
e0e80 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 77 6f  .  ** current wo
e0e90 72 6b 69 6e 67 20 64 69 72 65 63 74 6c 79 20 68  rking directly h
e0ea0 61 73 20 62 65 65 6e 20 75 6e 6c 69 6e 6b 65 64  as been unlinked
e0eb0 2e 0a 20 20 2a 2f 0a 20 20 53 69 6d 75 6c 61 74  ..  */.  Simulat
e0ec0 65 49 4f 45 72 72 6f 72 28 20 72 65 74 75 72 6e  eIOError( return
e0ed0 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b   SQLITE_ERROR );
e0ee0 0a 0a 20 20 61 73 73 65 72 74 28 20 70 56 66 73  ..  assert( pVfs
e0ef0 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 3d 3d 4d 41  ->mxPathname==MA
e0f00 58 5f 50 41 54 48 4e 41 4d 45 20 29 3b 0a 20 20  X_PATHNAME );.  
e0f10 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
e0f20 28 70 56 66 73 29 3b 0a 0a 23 69 66 20 49 53 5f  (pVfs);..#if IS_
e0f30 56 58 57 4f 52 4b 53 0a 20 20 7b 0a 20 20 20 20  VXWORKS.  {.    
e0f40 63 68 61 72 20 2a 7a 52 65 61 6c 6e 61 6d 65 20  char *zRealname 
e0f50 3d 20 76 78 72 65 61 6c 70 61 74 68 28 7a 50 61  = vxrealpath(zPa
e0f60 74 68 2c 20 30 29 3b 0a 20 20 20 20 7a 4f 75 74  th, 0);.    zOut
e0f70 5b 30 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20  [0] = '\0';.    
e0f80 69 66 28 20 21 7a 52 65 61 6c 6e 61 6d 65 20 29  if( !zRealname )
e0f90 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
e0fa0 51 4c 49 54 45 5f 43 41 4e 54 4f 50 45 4e 3b 0a  QLITE_CANTOPEN;.
e0fb0 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
e0fc0 33 5f 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c  3_snprintf(nOut,
e0fd0 20 7a 4f 75 74 2c 20 22 25 73 22 2c 20 7a 52 65   zOut, "%s", zRe
e0fe0 61 6c 6e 61 6d 65 29 3b 0a 20 20 20 20 73 71 6c  alname);.    sql
e0ff0 69 74 65 33 5f 66 72 65 65 28 7a 52 65 61 6c 6e  ite3_free(zRealn
e1000 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
e1010 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
e1020 23 65 6c 73 65 0a 20 20 7a 4f 75 74 5b 6e 4f 75  #else.  zOut[nOu
e1030 74 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 69  t-1] = '\0';.  i
e1040 66 28 20 7a 50 61 74 68 5b 30 5d 3d 3d 27 2f 27  f( zPath[0]=='/'
e1050 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
e1060 73 6e 70 72 69 6e 74 66 28 6e 4f 75 74 2c 20 7a  snprintf(nOut, z
e1070 4f 75 74 2c 20 22 25 73 22 2c 20 7a 50 61 74 68  Out, "%s", zPath
e1080 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
e1090 69 6e 74 20 6e 43 77 64 3b 0a 20 20 20 20 69 66  int nCwd;.    if
e10a0 28 20 67 65 74 63 77 64 28 7a 4f 75 74 2c 20 6e  ( getcwd(zOut, n
e10b0 4f 75 74 2d 31 29 3d 3d 30 20 29 7b 0a 20 20 20  Out-1)==0 ){.   
e10c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e10d0 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 7d  _CANTOPEN;.    }
e10e0 0a 20 20 20 20 6e 43 77 64 20 3d 20 73 74 72 6c  .    nCwd = strl
e10f0 65 6e 28 7a 4f 75 74 29 3b 0a 20 20 20 20 73 71  en(zOut);.    sq
e1100 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
e1110 4f 75 74 2d 6e 43 77 64 2c 20 26 7a 4f 75 74 5b  Out-nCwd, &zOut[
e1120 6e 43 77 64 5d 2c 20 22 2f 25 73 22 2c 20 7a 50  nCwd], "/%s", zP
e1130 61 74 68 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  ath);.  }.  retu
e1140 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 23  rn SQLITE_OK;..#
e1150 69 66 20 30 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52  if 0.  /*.  ** R
e1160 65 6d 6f 76 65 20 22 2f 2e 2f 22 20 70 61 74 68  emove "/./" path
e1170 20 65 6c 65 6d 65 6e 74 73 20 61 6e 64 20 63 6f   elements and co
e1180 6e 76 65 72 74 20 22 2f 41 2f 2e 2f 22 20 70 61  nvert "/A/./" pa
e1190 74 68 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a  th elements.  **
e11a0 20 74 6f 20 6a 75 73 74 20 22 2f 22 2e 0a 20 20   to just "/"..  
e11b0 2a 2f 0a 20 20 69 66 28 20 7a 46 75 6c 6c 20 29  */.  if( zFull )
e11c0 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b 0a  {.    int i, j;.
e11d0 20 20 20 20 66 6f 72 28 69 3d 6a 3d 30 3b 20 7a      for(i=j=0; z
e11e0 46 75 6c 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  Full[i]; i++){. 
e11f0 20 20 20 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69       if( zFull[i
e1200 5d 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20  ]=='/' ){.      
e1210 20 20 69 66 28 20 7a 46 75 6c 6c 5b 69 2b 31 5d    if( zFull[i+1]
e1220 3d 3d 27 2f 27 20 29 20 63 6f 6e 74 69 6e 75 65  =='/' ) continue
e1230 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 46  ;.        if( zF
e1240 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20 26 26  ull[i+1]=='.' &&
e1250 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27 2f 27   zFull[i+2]=='/'
e1260 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 20   ){.          i 
e1270 2b 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20  += 1;.          
e1280 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
e1290 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20    }.        if( 
e12a0 7a 46 75 6c 6c 5b 69 2b 31 5d 3d 3d 27 2e 27 20  zFull[i+1]=='.' 
e12b0 26 26 20 7a 46 75 6c 6c 5b 69 2b 32 5d 3d 3d 27  && zFull[i+2]=='
e12c0 2e 27 20 26 26 20 7a 46 75 6c 6c 5b 69 2b 33 5d  .' && zFull[i+3]
e12d0 3d 3d 27 2f 27 20 29 7b 0a 20 20 20 20 20 20 20  =='/' ){.       
e12e0 20 20 20 77 68 69 6c 65 28 20 6a 3e 30 20 26 26     while( j>0 &&
e12f0 20 7a 46 75 6c 6c 5b 6a 2d 31 5d 21 3d 27 2f 27   zFull[j-1]!='/'
e1300 20 29 7b 20 6a 2d 2d 3b 20 7d 0a 20 20 20 20 20   ){ j--; }.     
e1310 20 20 20 20 20 69 20 2b 3d 20 33 3b 0a 20 20 20       i += 3;.   
e1320 20 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b         continue;
e1330 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
e1340 20 7d 0a 20 20 20 20 20 20 7a 46 75 6c 6c 5b 6a   }.      zFull[j
e1350 2b 2b 5d 20 3d 20 7a 46 75 6c 6c 5b 69 5d 3b 0a  ++] = zFull[i];.
e1360 20 20 20 20 7d 0a 20 20 20 20 7a 46 75 6c 6c 5b      }.    zFull[
e1370 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 23 65 6e 64  j] = 0;.  }.#end
e1380 69 66 0a 23 65 6e 64 69 66 0a 7d 0a 0a 0a 23 69  if.#endif.}...#i
e1390 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
e13a0 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e  T_LOAD_EXTENSION
e13b0 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 66 61 63 65  ./*.** Interface
e13c0 73 20 66 6f 72 20 6f 70 65 6e 69 6e 67 20 61 20  s for opening a 
e13d0 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 2c 20  shared library, 
e13e0 66 69 6e 64 69 6e 67 20 65 6e 74 72 79 20 70 6f  finding entry po
e13f0 69 6e 74 73 0a 2a 2a 20 77 69 74 68 69 6e 20 74  ints.** within t
e1400 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
e1410 79 2c 20 61 6e 64 20 63 6c 6f 73 69 6e 67 20 74  y, and closing t
e1420 68 65 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  he shared librar
e1430 79 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 3c  y..*/.#include <
e1440 64 6c 66 63 6e 2e 68 3e 0a 73 74 61 74 69 63 20  dlfcn.h>.static 
e1450 76 6f 69 64 20 2a 75 6e 69 78 44 6c 4f 70 65 6e  void *unixDlOpen
e1460 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f  (sqlite3_vfs *No
e1470 74 55 73 65 64 2c 20 63 6f 6e 73 74 20 63 68 61  tUsed, const cha
e1480 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a 20  r *zFilename){. 
e1490 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
e14a0 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 72 65  R(NotUsed);.  re
e14b0 74 75 72 6e 20 64 6c 6f 70 65 6e 28 7a 46 69 6c  turn dlopen(zFil
e14c0 65 6e 61 6d 65 2c 20 52 54 4c 44 5f 4e 4f 57 20  ename, RTLD_NOW 
e14d0 7c 20 52 54 4c 44 5f 47 4c 4f 42 41 4c 29 3b 0a  | RTLD_GLOBAL);.
e14e0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 51 4c 69 74 65 20  }../*.** SQLite 
e14f0 63 61 6c 6c 73 20 74 68 69 73 20 66 75 6e 63 74  calls this funct
e1500 69 6f 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ion immediately 
e1510 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20  after a call to 
e1520 75 6e 69 78 44 6c 53 79 6d 28 29 20 6f 72 0a 2a  unixDlSym() or.*
e1530 2a 20 75 6e 69 78 44 6c 4f 70 65 6e 28 29 20 66  * unixDlOpen() f
e1540 61 69 6c 73 20 28 72 65 74 75 72 6e 73 20 61 20  ails (returns a 
e1550 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 29 2e 20 49  null pointer). I
e1560 66 20 61 20 6d 6f 72 65 20 64 65 74 61 69 6c 65  f a more detaile
e1570 64 20 65 72 72 6f 72 0a 2a 2a 20 6d 65 73 73 61  d error.** messa
e1580 67 65 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c  ge is available,
e1590 20 69 74 20 69 73 20 77 72 69 74 74 65 6e 20 74   it is written t
e15a0 6f 20 7a 42 75 66 4f 75 74 2e 20 49 66 20 6e 6f  o zBufOut. If no
e15b0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
e15c0 2a 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2c 20  * is available, 
e15d0 7a 42 75 66 4f 75 74 20 69 73 20 6c 65 66 74 20  zBufOut is left 
e15e0 75 6e 6d 6f 64 69 66 69 65 64 20 61 6e 64 20 53  unmodified and S
e15f0 51 4c 69 74 65 20 75 73 65 73 20 61 20 64 65 66  QLite uses a def
e1600 61 75 6c 74 0a 2a 2a 20 65 72 72 6f 72 20 6d 65  ault.** error me
e1610 73 73 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  ssage..*/.static
e1620 20 76 6f 69 64 20 75 6e 69 78 44 6c 45 72 72 6f   void unixDlErro
e1630 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  r(sqlite3_vfs *N
e1640 6f 74 55 73 65 64 2c 20 69 6e 74 20 6e 42 75 66  otUsed, int nBuf
e1650 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29  , char *zBufOut)
e1660 7b 0a 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a  {.  char *zErr;.
e1670 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e1680 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 65  ER(NotUsed);.  e
e1690 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 7a  nterMutex();.  z
e16a0 45 72 72 20 3d 20 64 6c 65 72 72 6f 72 28 29 3b  Err = dlerror();
e16b0 0a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20  .  if( zErr ){. 
e16c0 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
e16d0 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75  ntf(nBuf, zBufOu
e16e0 74 2c 20 22 25 73 22 2c 20 7a 45 72 72 29 3b 0a  t, "%s", zErr);.
e16f0 20 20 7d 0a 20 20 6c 65 61 76 65 4d 75 74 65 78    }.  leaveMutex
e1700 28 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69  ();.}.static voi
e1710 64 20 2a 75 6e 69 78 44 6c 53 79 6d 28 73 71 6c  d *unixDlSym(sql
e1720 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65  ite3_vfs *NotUse
e1730 64 2c 20 76 6f 69 64 20 2a 70 48 61 6e 64 6c 65  d, void *pHandle
e1740 2c 20 63 6f 6e 73 74 20 63 68 61 72 2a 7a 53 79  , const char*zSy
e1750 6d 62 6f 6c 29 7b 0a 20 20 55 4e 55 53 45 44 5f  mbol){.  UNUSED_
e1760 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e1770 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 64 6c 73  d);.  return dls
e1780 79 6d 28 70 48 61 6e 64 6c 65 2c 20 7a 53 79 6d  ym(pHandle, zSym
e1790 62 6f 6c 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  bol);.}.static v
e17a0 6f 69 64 20 75 6e 69 78 44 6c 43 6c 6f 73 65 28  oid unixDlClose(
e17b0 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e 6f 74  sqlite3_vfs *Not
e17c0 55 73 65 64 2c 20 76 6f 69 64 20 2a 70 48 61 6e  Used, void *pHan
e17d0 64 6c 65 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50  dle){.  UNUSED_P
e17e0 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64  ARAMETER(NotUsed
e17f0 29 3b 0a 20 20 64 6c 63 6c 6f 73 65 28 70 48 61  );.  dlclose(pHa
e1800 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f  ndle);.}.#else /
e1810 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * if SQLITE_OMIT
e1820 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
e1830 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20  is defined: */. 
e1840 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 4f   #define unixDlO
e1850 70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65  pen  0.  #define
e1860 20 75 6e 69 78 44 6c 45 72 72 6f 72 20 30 0a 20   unixDlError 0. 
e1870 20 23 64 65 66 69 6e 65 20 75 6e 69 78 44 6c 53   #define unixDlS
e1880 79 6d 20 20 20 30 0a 20 20 23 64 65 66 69 6e 65  ym   0.  #define
e1890 20 75 6e 69 78 44 6c 43 6c 6f 73 65 20 30 0a 23   unixDlClose 0.#
e18a0 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  endif../*.** Wri
e18b0 74 65 20 6e 42 75 66 20 62 79 74 65 73 20 6f 66  te nBuf bytes of
e18c0 20 72 61 6e 64 6f 6d 20 64 61 74 61 20 74 6f 20   random data to 
e18d0 74 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66  the supplied buf
e18e0 66 65 72 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61  fer zBuf..*/.sta
e18f0 74 69 63 20 69 6e 74 20 75 6e 69 78 52 61 6e 64  tic int unixRand
e1900 6f 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76  omness(sqlite3_v
e1910 66 73 20 2a 4e 6f 74 55 73 65 64 2c 20 69 6e 74  fs *NotUsed, int
e1920 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
e1930 66 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  f){.  UNUSED_PAR
e1940 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
e1950 0a 20 20 61 73 73 65 72 74 28 28 73 69 7a 65 5f  .  assert((size_
e1960 74 29 6e 42 75 66 3e 3d 28 73 69 7a 65 6f 66 28  t)nBuf>=(sizeof(
e1970 74 69 6d 65 5f 74 29 2b 73 69 7a 65 6f 66 28 69  time_t)+sizeof(i
e1980 6e 74 29 29 29 3b 0a 0a 20 20 2f 2a 20 57 65 20  nt)));..  /* We 
e1990 68 61 76 65 20 74 6f 20 69 6e 69 74 69 61 6c 69  have to initiali
e19a0 7a 65 20 7a 42 75 66 20 74 6f 20 70 72 65 76 65  ze zBuf to preve
e19b0 6e 74 20 76 61 6c 67 72 69 6e 64 20 66 72 6f 6d  nt valgrind from
e19c0 20 72 65 70 6f 72 74 69 6e 67 0a 20 20 2a 2a 20   reporting.  ** 
e19d0 65 72 72 6f 72 73 2e 20 20 54 68 65 20 72 65 70  errors.  The rep
e19e0 6f 72 74 73 20 69 73 73 75 65 64 20 62 79 20 76  orts issued by v
e19f0 61 6c 67 72 69 6e 64 20 61 72 65 20 69 6e 63 6f  algrind are inco
e1a00 72 72 65 63 74 20 2d 20 77 65 20 77 6f 75 6c 64  rrect - we would
e1a10 0a 20 20 2a 2a 20 70 72 65 66 65 72 20 74 68 61  .  ** prefer tha
e1a20 74 20 74 68 65 20 72 61 6e 64 6f 6d 6e 65 73 73  t the randomness
e1a30 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 62 79   be increased by
e1a40 20 6d 61 6b 69 6e 67 20 75 73 65 20 6f 66 20 74   making use of t
e1a50 68 65 0a 20 20 2a 2a 20 75 6e 69 6e 69 74 69 61  he.  ** uninitia
e1a60 6c 69 7a 65 64 20 73 70 61 63 65 20 69 6e 20 7a  lized space in z
e1a70 42 75 66 20 2d 20 62 75 74 20 76 61 6c 67 72 69  Buf - but valgri
e1a80 6e 64 20 65 72 72 6f 72 73 20 74 65 6e 64 20 74  nd errors tend t
e1a90 6f 20 77 6f 72 72 79 0a 20 20 2a 2a 20 73 6f 6d  o worry.  ** som
e1aa0 65 20 75 73 65 72 73 2e 20 20 52 61 74 68 65 72  e users.  Rather
e1ab0 20 74 68 61 6e 20 61 72 67 75 65 2c 20 69 74 20   than argue, it 
e1ac0 73 65 65 6d 73 20 65 61 73 69 65 72 20 6a 75 73  seems easier jus
e1ad0 74 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 0a  t to initialize.
e1ae0 20 20 2a 2a 20 74 68 65 20 77 68 6f 6c 65 20 61    ** the whole a
e1af0 72 72 61 79 20 61 6e 64 20 73 69 6c 65 6e 63 65  rray and silence
e1b00 20 76 61 6c 67 72 69 6e 64 2c 20 65 76 65 6e 20   valgrind, even 
e1b10 69 66 20 74 68 61 74 20 6d 65 61 6e 73 20 6c 65  if that means le
e1b20 73 73 20 72 61 6e 64 6f 6d 6e 65 73 73 0a 20 20  ss randomness.  
e1b30 2a 2a 20 69 6e 20 74 68 65 20 72 61 6e 64 6f 6d  ** in the random
e1b40 20 73 65 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a   seed..  **.  **
e1b50 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 69   When testing, i
e1b60 6e 69 74 69 61 6c 69 7a 69 6e 67 20 7a 42 75 66  nitializing zBuf
e1b70 5b 5d 20 74 6f 20 7a 65 72 6f 20 69 73 20 61 6c  [] to zero is al
e1b80 6c 20 77 65 20 64 6f 2e 20 20 54 68 61 74 20 6d  l we do.  That m
e1b90 65 61 6e 73 0a 20 20 2a 2a 20 74 68 61 74 20 77  eans.  ** that w
e1ba0 65 20 61 6c 77 61 79 73 20 75 73 65 20 74 68 65  e always use the
e1bb0 20 73 61 6d 65 20 72 61 6e 64 6f 6d 20 6e 75 6d   same random num
e1bc0 62 65 72 20 73 65 71 75 65 6e 63 65 2e 20 20 54  ber sequence.  T
e1bd0 68 69 73 20 6d 61 6b 65 73 20 74 68 65 0a 20 20  his makes the.  
e1be0 2a 2a 20 74 65 73 74 73 20 72 65 70 65 61 74 61  ** tests repeata
e1bf0 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73  ble..  */.  mems
e1c00 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42 75 66  et(zBuf, 0, nBuf
e1c10 29 3b 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  );.#if !defined(
e1c20 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 20 20 7b  SQLITE_TEST).  {
e1c30 0a 20 20 20 20 69 6e 74 20 70 69 64 2c 20 66 64  .    int pid, fd
e1c40 3b 0a 20 20 20 20 66 64 20 3d 20 6f 70 65 6e 28  ;.    fd = open(
e1c50 22 2f 64 65 76 2f 75 72 61 6e 64 6f 6d 22 2c 20  "/dev/urandom", 
e1c60 4f 5f 52 44 4f 4e 4c 59 29 3b 0a 20 20 20 20 69  O_RDONLY);.    i
e1c70 66 28 20 66 64 3c 30 20 29 7b 0a 20 20 20 20 20  f( fd<0 ){.     
e1c80 20 74 69 6d 65 5f 74 20 74 3b 0a 20 20 20 20 20   time_t t;.     
e1c90 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 20 20 20   time(&t);.     
e1ca0 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20 26 74   memcpy(zBuf, &t
e1cb0 2c 20 73 69 7a 65 6f 66 28 74 29 29 3b 0a 20 20  , sizeof(t));.  
e1cc0 20 20 20 20 70 69 64 20 3d 20 67 65 74 70 69 64      pid = getpid
e1cd0 28 29 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  ();.      memcpy
e1ce0 28 26 7a 42 75 66 5b 73 69 7a 65 6f 66 28 74 29  (&zBuf[sizeof(t)
e1cf0 5d 2c 20 26 70 69 64 2c 20 73 69 7a 65 6f 66 28  ], &pid, sizeof(
e1d00 70 69 64 29 29 3b 0a 20 20 20 20 20 20 61 73 73  pid));.      ass
e1d10 65 72 74 28 20 73 69 7a 65 6f 66 28 74 29 2b 73  ert( sizeof(t)+s
e1d20 69 7a 65 6f 66 28 70 69 64 29 3c 3d 28 73 69 7a  izeof(pid)<=(siz
e1d30 65 5f 74 29 6e 42 75 66 20 29 3b 0a 20 20 20 20  e_t)nBuf );.    
e1d40 20 20 6e 42 75 66 20 3d 20 73 69 7a 65 6f 66 28    nBuf = sizeof(
e1d50 74 29 20 2b 20 73 69 7a 65 6f 66 28 70 69 64 29  t) + sizeof(pid)
e1d60 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
e1d70 20 20 20 6e 42 75 66 20 3d 20 72 65 61 64 28 66     nBuf = read(f
e1d80 64 2c 20 7a 42 75 66 2c 20 6e 42 75 66 29 3b 0a  d, zBuf, nBuf);.
e1d90 20 20 20 20 20 20 63 6c 6f 73 65 28 66 64 29 3b        close(fd);
e1da0 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69  .    }.  }.#endi
e1db0 66 0a 20 20 72 65 74 75 72 6e 20 6e 42 75 66 3b  f.  return nBuf;
e1dc0 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70  .}.../*.** Sleep
e1dd0 20 66 6f 72 20 61 20 6c 69 74 74 6c 65 20 77 68   for a little wh
e1de0 69 6c 65 2e 20 20 52 65 74 75 72 6e 20 74 68 65  ile.  Return the
e1df0 20 61 6d 6f 75 6e 74 20 6f 66 20 74 69 6d 65 20   amount of time 
e1e00 73 6c 65 70 74 2e 0a 2a 2a 20 54 68 65 20 61 72  slept..** The ar
e1e10 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e 75  gument is the nu
e1e20 6d 62 65 72 20 6f 66 20 6d 69 63 72 6f 73 65 63  mber of microsec
e1e30 6f 6e 64 73 20 77 65 20 77 61 6e 74 20 74 6f 20  onds we want to 
e1e40 73 6c 65 65 70 2e 0a 2a 2a 20 54 68 65 20 72 65  sleep..** The re
e1e50 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68  turn value is th
e1e60 65 20 6e 75 6d 62 65 72 20 6f 66 20 6d 69 63 72  e number of micr
e1e70 6f 73 65 63 6f 6e 64 73 20 6f 66 20 73 6c 65 65  oseconds of slee
e1e80 70 20 61 63 74 75 61 6c 6c 79 0a 2a 2a 20 72 65  p actually.** re
e1e90 71 75 65 73 74 65 64 20 66 72 6f 6d 20 74 68 65  quested from the
e1ea0 20 75 6e 64 65 72 6c 79 69 6e 67 20 6f 70 65 72   underlying oper
e1eb0 61 74 69 6e 67 20 73 79 73 74 65 6d 2c 20 61 20  ating system, a 
e1ec0 6e 75 6d 62 65 72 20 77 68 69 63 68 0a 2a 2a 20  number which.** 
e1ed0 6d 69 67 68 74 20 62 65 20 67 72 65 61 74 65 72  might be greater
e1ee0 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
e1ef0 6f 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2c 20  o the argument, 
e1f00 62 75 74 20 6e 6f 74 20 6c 65 73 73 0a 2a 2a 20  but not less.** 
e1f10 74 68 61 6e 20 74 68 65 20 61 72 67 75 6d 65 6e  than the argumen
e1f20 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
e1f30 20 75 6e 69 78 53 6c 65 65 70 28 73 71 6c 69 74   unixSleep(sqlit
e1f40 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e1f50 20 69 6e 74 20 6d 69 63 72 6f 73 65 63 6f 6e 64   int microsecond
e1f60 73 29 7b 0a 23 69 66 20 49 53 5f 56 58 57 4f 52  s){.#if IS_VXWOR
e1f70 4b 53 0a 20 20 73 74 72 75 63 74 20 74 69 6d 65  KS.  struct time
e1f80 73 70 65 63 20 73 70 3b 0a 0a 20 20 73 70 2e 74  spec sp;..  sp.t
e1f90 76 5f 73 65 63 20 3d 20 6d 69 63 72 6f 73 65 63  v_sec = microsec
e1fa0 6f 6e 64 73 20 2f 20 31 30 30 30 30 30 30 3b 0a  onds / 1000000;.
e1fb0 20 20 73 70 2e 74 76 5f 6e 73 65 63 20 3d 20 28    sp.tv_nsec = (
e1fc0 6d 69 63 72 6f 73 65 63 6f 6e 64 73 20 25 20 31  microseconds % 1
e1fd0 30 30 30 30 30 30 29 20 2a 20 31 30 30 30 3b 0a  000000) * 1000;.
e1fe0 20 20 6e 61 6e 6f 73 6c 65 65 70 28 26 73 70 2c    nanosleep(&sp,
e1ff0 20 4e 55 4c 4c 29 3b 0a 20 20 72 65 74 75 72 6e   NULL);.  return
e2000 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 3b 0a 23   microseconds;.#
e2010 65 6c 69 66 20 64 65 66 69 6e 65 64 28 48 41 56  elif defined(HAV
e2020 45 5f 55 53 4c 45 45 50 29 20 26 26 20 48 41 56  E_USLEEP) && HAV
e2030 45 5f 55 53 4c 45 45 50 0a 20 20 75 73 6c 65 65  E_USLEEP.  uslee
e2040 70 28 6d 69 63 72 6f 73 65 63 6f 6e 64 73 29 3b  p(microseconds);
e2050 0a 20 20 72 65 74 75 72 6e 20 6d 69 63 72 6f 73  .  return micros
e2060 65 63 6f 6e 64 73 3b 0a 23 65 6c 73 65 0a 20 20  econds;.#else.  
e2070 69 6e 74 20 73 65 63 6f 6e 64 73 20 3d 20 28 6d  int seconds = (m
e2080 69 63 72 6f 73 65 63 6f 6e 64 73 2b 39 39 39 39  icroseconds+9999
e2090 39 39 29 2f 31 30 30 30 30 30 30 3b 0a 20 20 73  99)/1000000;.  s
e20a0 6c 65 65 70 28 73 65 63 6f 6e 64 73 29 3b 0a 20  leep(seconds);. 
e20b0 20 72 65 74 75 72 6e 20 73 65 63 6f 6e 64 73 2a   return seconds*
e20c0 31 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a  1000000;.#endif.
e20d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
e20e0 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 7d 0a 0a  ER(NotUsed);.}..
e20f0 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
e2100 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20 69 66  ing variable, if
e2110 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d 7a 65   set to a non-ze
e2120 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f 6d 65  ro value, become
e2130 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20  s the result.** 
e2140 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 73 71  returned from sq
e2150 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74 54 69  lite3OsCurrentTi
e2160 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73 20 75  me().  This is u
e2170 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 2e  sed for testing.
e2180 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
e2190 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
e21a0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 75  I int sqlite3_cu
e21b0 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30 3b 0a  rrent_time = 0;.
e21c0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 69  #endif../*.** Fi
e21d0 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74 20 74  nd the current t
e21e0 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72 73 61  ime (in Universa
e21f0 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20 54 69  l Coordinated Ti
e2200 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68 65 0a  me).  Write the.
e2210 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d 65 20  ** current time 
e2220 61 6e 64 20 64 61 74 65 20 61 73 20 61 20 4a 75  and date as a Ju
e2230 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65 72 20  lian Day number 
e2240 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e 64 0a  into *prNow and.
e2250 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20 52 65  ** return 0.  Re
e2260 74 75 72 6e 20 31 20 69 66 20 74 68 65 20 74 69  turn 1 if the ti
e2270 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61 6e 6e  me and date cann
e2280 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a 2f 0a  ot be found..*/.
e2290 73 74 61 74 69 63 20 69 6e 74 20 75 6e 69 78 43  static int unixC
e22a0 75 72 72 65 6e 74 54 69 6d 65 28 73 71 6c 69 74  urrentTime(sqlit
e22b0 65 33 5f 76 66 73 20 2a 4e 6f 74 55 73 65 64 2c  e3_vfs *NotUsed,
e22c0 20 64 6f 75 62 6c 65 20 2a 70 72 4e 6f 77 29 7b   double *prNow){
e22d0 0a 23 69 66 20 49 53 5f 56 58 57 4f 52 4b 53 0a  .#if IS_VXWORKS.
e22e0 20 20 73 74 72 75 63 74 20 74 69 6d 65 73 70 65    struct timespe
e22f0 63 20 73 4e 6f 77 3b 0a 20 20 63 6c 6f 63 6b 5f  c sNow;.  clock_
e2300 67 65 74 74 69 6d 65 28 43 4c 4f 43 4b 5f 52 45  gettime(CLOCK_RE
e2310 41 4c 54 49 4d 45 2c 20 26 73 4e 6f 77 29 3b 0a  ALTIME, &sNow);.
e2320 20 20 2a 70 72 4e 6f 77 20 3d 20 32 34 34 30 35    *prNow = 24405
e2330 38 37 2e 35 20 2b 20 73 4e 6f 77 2e 74 76 5f 73  87.5 + sNow.tv_s
e2340 65 63 2f 38 36 34 30 30 2e 30 20 2b 20 73 4e 6f  ec/86400.0 + sNo
e2350 77 2e 74 76 5f 6e 73 65 63 2f 38 36 34 30 30 30  w.tv_nsec/864000
e2360 30 30 30 30 30 30 30 30 2e 30 3b 0a 23 65 6c 69  00000000.0;.#eli
e2370 66 20 64 65 66 69 6e 65 64 28 4e 4f 5f 47 45 54  f defined(NO_GET
e2380 54 4f 44 29 0a 20 20 74 69 6d 65 5f 74 20 74 3b  TOD).  time_t t;
e2390 0a 20 20 74 69 6d 65 28 26 74 29 3b 0a 20 20 2a  .  time(&t);.  *
e23a0 70 72 4e 6f 77 20 3d 20 74 2f 38 36 34 30 30 2e  prNow = t/86400.
e23b0 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 23  0 + 2440587.5;.#
e23c0 65 6c 73 65 0a 20 20 73 74 72 75 63 74 20 74 69  else.  struct ti
e23d0 6d 65 76 61 6c 20 73 4e 6f 77 3b 0a 20 20 67 65  meval sNow;.  ge
e23e0 74 74 69 6d 65 6f 66 64 61 79 28 26 73 4e 6f 77  ttimeofday(&sNow
e23f0 2c 20 30 29 3b 0a 20 20 2a 70 72 4e 6f 77 20 3d  , 0);.  *prNow =
e2400 20 32 34 34 30 35 38 37 2e 35 20 2b 20 73 4e 6f   2440587.5 + sNo
e2410 77 2e 74 76 5f 73 65 63 2f 38 36 34 30 30 2e 30  w.tv_sec/86400.0
e2420 20 2b 20 73 4e 6f 77 2e 74 76 5f 75 73 65 63 2f   + sNow.tv_usec/
e2430 38 36 34 30 30 30 30 30 30 30 30 2e 30 3b 0a 23  86400000000.0;.#
e2440 65 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51  endif..#ifdef SQ
e2450 4c 49 54 45 5f 54 45 53 54 0a 20 20 69 66 28 20  LITE_TEST.  if( 
e2460 73 71 6c 69 74 65 33 5f 63 75 72 72 65 6e 74 5f  sqlite3_current_
e2470 74 69 6d 65 20 29 7b 0a 20 20 20 20 2a 70 72 4e  time ){.    *prN
e2480 6f 77 20 3d 20 73 71 6c 69 74 65 33 5f 63 75 72  ow = sqlite3_cur
e2490 72 65 6e 74 5f 74 69 6d 65 2f 38 36 34 30 30 2e  rent_time/86400.
e24a0 30 20 2b 20 32 34 34 30 35 38 37 2e 35 3b 0a 20  0 + 2440587.5;. 
e24b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 55 4e 55 53   }.#endif.  UNUS
e24c0 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
e24d0 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
e24e0 30 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  0;.}..static int
e24f0 20 75 6e 69 78 47 65 74 4c 61 73 74 45 72 72 6f   unixGetLastErro
e2500 72 28 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 4e  r(sqlite3_vfs *N
e2510 6f 74 55 73 65 64 2c 20 69 6e 74 20 4e 6f 74 55  otUsed, int NotU
e2520 73 65 64 32 2c 20 63 68 61 72 20 2a 4e 6f 74 55  sed2, char *NotU
e2530 73 65 64 33 29 7b 0a 20 20 55 4e 55 53 45 44 5f  sed3){.  UNUSED_
e2540 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
e2550 64 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52  d);.  UNUSED_PAR
e2560 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 32 29  AMETER(NotUsed2)
e2570 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
e2580 45 54 45 52 28 4e 6f 74 55 73 65 64 33 29 3b 0a  ETER(NotUsed3);.
e2590 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
e25a0 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20  *.** Initialize 
e25b0 74 68 65 20 6f 70 65 72 61 74 69 6e 67 20 73 79  the operating sy
e25c0 73 74 65 6d 20 69 6e 74 65 72 66 61 63 65 2e 0a  stem interface..
e25d0 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
e25e0 74 20 73 71 6c 69 74 65 33 5f 6f 73 5f 69 6e 69  t sqlite3_os_ini
e25f0 74 28 76 6f 69 64 29 7b 20 0a 20 20 2f 2a 20 4d  t(void){ .  /* M
e2600 61 63 72 6f 20 74 6f 20 64 65 66 69 6e 65 20 74  acro to define t
e2610 68 65 20 73 74 61 74 69 63 20 63 6f 6e 74 65 6e  he static conten
e2620 74 73 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33  ts of an sqlite3
e2630 5f 76 66 73 20 73 74 72 75 63 74 75 72 65 20 66  _vfs structure f
e2640 6f 72 0a 20 20 2a 2a 20 74 68 65 20 75 6e 69 78  or.  ** the unix
e2650 20 62 61 63 6b 65 6e 64 2e 20 54 68 65 20 74 77   backend. The tw
e2660 6f 20 70 61 72 61 6d 65 74 65 72 73 20 61 72 65  o parameters are
e2670 20 74 68 65 20 76 61 6c 75 65 73 20 74 6f 20 75   the values to u
e2680 73 65 20 66 6f 72 0a 20 20 2a 2a 20 74 68 65 20  se for.  ** the 
e2690 73 71 6c 69 74 65 33 5f 76 66 73 2e 7a 4e 61 6d  sqlite3_vfs.zNam
e26a0 65 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 76 66  e and sqlite3_vf
e26b0 73 2e 70 41 70 70 44 61 74 61 20 66 69 65 6c 64  s.pAppData field
e26c0 73 2c 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  s, respectively.
e26d0 0a 20 20 2a 2a 20 0a 20 20 2a 2f 0a 20 20 23 64  .  ** .  */.  #d
e26e0 65 66 69 6e 65 20 55 4e 49 58 56 46 53 28 7a 56  efine UNIXVFS(zV
e26f0 66 73 4e 61 6d 65 2c 20 70 56 66 73 41 70 70 44  fsName, pVfsAppD
e2700 61 74 61 29 20 7b 20 20 20 20 20 20 20 20 20 20  ata) {          
e2710 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 31 2c          \.    1,
e2720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2730 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
e2740 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e2750 20 20 20 20 20 20 5c 0a 20 20 20 20 73 69 7a 65        \.    size
e2760 6f 66 28 75 6e 69 78 46 69 6c 65 29 2c 20 20 20  of(unixFile),   
e2770 20 20 2f 2a 20 73 7a 4f 73 46 69 6c 65 20 2a 2f    /* szOsFile */
e2780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2790 20 20 20 20 5c 0a 20 20 20 20 4d 41 58 5f 50 41      \.    MAX_PA
e27a0 54 48 4e 41 4d 45 2c 20 20 20 20 20 20 20 20 20  THNAME,         
e27b0 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f  /* mxPathname */
e27c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e27d0 20 20 5c 0a 20 20 20 20 30 2c 20 20 20 20 20 20    \.    0,      
e27e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
e27f0 20 70 4e 65 78 74 20 2a 2f 20 20 20 20 20 20 20   pNext */       
e2800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2810 5c 0a 20 20 20 20 7a 56 66 73 4e 61 6d 65 2c 20  \.    zVfsName, 
e2820 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a              /* z
e2830 4e 61 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20  Name */         
e2840 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e2850 20 20 20 20 28 76 6f 69 64 20 2a 29 70 56 66 73      (void *)pVfs
e2860 41 70 70 44 61 74 61 2c 20 20 2f 2a 20 70 41 70  AppData,  /* pAp
e2870 70 44 61 74 61 20 2a 2f 20 20 20 20 20 20 20 20  pData */        
e2880 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e2890 20 20 75 6e 69 78 4f 70 65 6e 2c 20 20 20 20 20    unixOpen,     
e28a0 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
e28b0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e28c0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
e28d0 75 6e 69 78 44 65 6c 65 74 65 2c 20 20 20 20 20  unixDelete,     
e28e0 20 20 20 20 20 20 2f 2a 20 78 44 65 6c 65 74 65        /* xDelete
e28f0 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20   */             
e2900 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
e2910 69 78 41 63 63 65 73 73 2c 20 20 20 20 20 20 20  ixAccess,       
e2920 20 20 20 20 2f 2a 20 78 41 63 63 65 73 73 20 2a      /* xAccess *
e2930 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  /               
e2940 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
e2950 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 20 20  FullPathname,   
e2960 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e 61    /* xFullPathna
e2970 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  me */           
e2980 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c      \.    unixDl
e2990 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20  Open,           
e29a0 2f 2a 20 78 44 6c 4f 70 65 6e 20 2a 2f 20 20 20  /* xDlOpen */   
e29b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e29c0 20 20 5c 0a 20 20 20 20 75 6e 69 78 44 6c 45 72    \.    unixDlEr
e29d0 72 6f 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ror,          /*
e29e0 20 78 44 6c 45 72 72 6f 72 20 2a 2f 20 20 20 20   xDlError */    
e29f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e2a00 5c 0a 20 20 20 20 75 6e 69 78 44 6c 53 79 6d 2c  \.    unixDlSym,
e2a10 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
e2a20 44 6c 53 79 6d 20 2a 2f 20 20 20 20 20 20 20 20  DlSym */        
e2a30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
e2a40 20 20 20 20 75 6e 69 78 44 6c 43 6c 6f 73 65 2c      unixDlClose,
e2a50 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c            /* xDl
e2a60 43 6c 6f 73 65 20 2a 2f 20 20 20 20 20 20 20 20  Close */        
e2a70 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
e2a80 20 20 75 6e 69 78 52 61 6e 64 6f 6d 6e 65 73 73    unixRandomness
e2a90 2c 20 20 20 20 20 20 20 2f 2a 20 78 52 61 6e 64  ,       /* xRand
e2aa0 6f 6d 6e 65 73 73 20 2a 2f 20 20 20 20 20 20 20  omness */       
e2ab0 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20            \.    
e2ac0 75 6e 69 78 53 6c 65 65 70 2c 20 20 20 20 20 20  unixSleep,      
e2ad0 20 20 20 20 20 20 2f 2a 20 78 53 6c 65 65 70 20        /* xSleep 
e2ae0 2a 2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20  */              
e2af0 20 20 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e          \.    un
e2b00 69 78 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20  ixCurrentTime,  
e2b10 20 20 20 20 2f 2a 20 78 43 75 72 72 65 6e 74 54      /* xCurrentT
e2b20 69 6d 65 20 2a 2f 20 20 20 20 20 20 20 20 20 20  ime */          
e2b30 20 20 20 20 20 20 5c 0a 20 20 20 20 75 6e 69 78        \.    unix
e2b40 47 65 74 4c 61 73 74 45 72 72 6f 72 20 20 20 20  GetLastError    
e2b50 20 20 2f 2a 20 78 47 65 74 4c 61 73 74 45 72 72    /* xGetLastErr
e2b60 6f 72 20 2a 2f 20 20 20 20 20 20 20 20 20 20 20  or */           
e2b70 20 20 20 20 5c 0a 20 20 7d 0a 0a 20 20 73 74 61      \.  }..  sta
e2b80 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66 73 20  tic sqlite3_vfs 
e2b90 75 6e 69 78 56 66 73 20 3d 20 55 4e 49 58 56 46  unixVfs = UNIXVF
e2ba0 53 28 22 75 6e 69 78 22 2c 20 30 29 3b 0a 23 69  S("unix", 0);.#i
e2bb0 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
e2bc0 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 0a 20 20  LOCKING_STYLE.  
e2bd0 69 6e 74 20 69 3b 0a 20 20 73 74 61 74 69 63 20  int i;.  static 
e2be0 73 71 6c 69 74 65 33 5f 76 66 73 20 61 56 66 73  sqlite3_vfs aVfs
e2bf0 5b 5d 20 3d 20 7b 0a 20 20 20 20 55 4e 49 58 56  [] = {.    UNIXV
e2c00 46 53 28 22 75 6e 69 78 2d 70 6f 73 69 78 22 2c  FS("unix-posix",
e2c10 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45     LOCKING_STYLE
e2c20 5f 50 4f 53 49 58 29 2c 20 0a 20 20 20 20 55 4e  _POSIX), .    UN
e2c30 49 58 56 46 53 28 22 75 6e 69 78 2d 61 66 70 22  IXVFS("unix-afp"
e2c40 2c 20 20 20 20 20 4c 4f 43 4b 49 4e 47 5f 53 54  ,     LOCKING_ST
e2c50 59 4c 45 5f 41 46 50 29 2c 20 0a 20 20 20 20 55  YLE_AFP), .    U
e2c60 4e 49 58 56 46 53 28 22 75 6e 69 78 2d 66 6c 6f  NIXVFS("unix-flo
e2c70 63 6b 22 2c 20 20 20 4c 4f 43 4b 49 4e 47 5f 53  ck",   LOCKING_S
e2c80 54 59 4c 45 5f 46 4c 4f 43 4b 29 2c 20 0a 20 20  TYLE_FLOCK), .  
e2c90 20 20 55 4e 49 58 56 46 53 28 22 75 6e 69 78 2d    UNIXVFS("unix-
e2ca0 64 6f 74 66 69 6c 65 22 2c 20 4c 4f 43 4b 49 4e  dotfile", LOCKIN
e2cb0 47 5f 53 54 59 4c 45 5f 44 4f 54 46 49 4c 45 29  G_STYLE_DOTFILE)
e2cc0 2c 20 0a 20 20 20 20 55 4e 49 58 56 46 53 28 22  , .    UNIXVFS("
e2cd0 75 6e 69 78 2d 6e 6f 6e 65 22 2c 20 20 20 20 4c  unix-none",    L
e2ce0 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 4f 4e  OCKING_STYLE_NON
e2cf0 45 29 2c 0a 20 20 20 20 55 4e 49 58 56 46 53 28  E),.    UNIXVFS(
e2d00 22 75 6e 69 78 2d 6e 61 6d 65 64 73 65 6d 22 2c  "unix-namedsem",
e2d10 4c 4f 43 4b 49 4e 47 5f 53 54 59 4c 45 5f 4e 41  LOCKING_STYLE_NA
e2d20 4d 45 44 53 45 4d 29 2c 0a 20 20 7d 3b 0a 20 20  MEDSEM),.  };.  
e2d30 66 6f 72 28 69 3d 30 3b 20 69 3c 28 73 69 7a 65  for(i=0; i<(size
e2d40 6f 66 28 61 56 66 73 29 2f 73 69 7a 65 6f 66 28  of(aVfs)/sizeof(
e2d50 73 71 6c 69 74 65 33 5f 76 66 73 29 29 3b 20 69  sqlite3_vfs)); i
e2d60 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ++){.    sqlite3
e2d70 5f 76 66 73 5f 72 65 67 69 73 74 65 72 28 26 61  _vfs_register(&a
e2d80 56 66 73 5b 69 5d 2c 20 30 29 3b 0a 20 20 7d 0a  Vfs[i], 0);.  }.
e2d90 23 65 6e 64 69 66 0a 23 69 66 20 49 53 5f 56 58  #endif.#if IS_VX
e2da0 57 4f 52 4b 53 0a 20 20 73 71 6c 69 74 65 33 48  WORKS.  sqlite3H
e2db0 61 73 68 49 6e 69 74 28 26 6e 61 6d 65 48 61 73  ashInit(&nameHas
e2dc0 68 2c 20 31 29 3b 0a 23 65 6e 64 69 66 0a 20 20  h, 1);.#endif.  
e2dd0 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
e2de0 73 74 65 72 28 26 75 6e 69 78 56 66 73 2c 20 31  ster(&unixVfs, 1
e2df0 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
e2e00 54 45 5f 4f 4b 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a  TE_OK; .}../*.**
e2e10 20 53 68 75 74 64 6f 77 6e 20 74 68 65 20 6f 70   Shutdown the op
e2e20 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69  erating system i
e2e30 6e 74 65 72 66 61 63 65 2e 20 54 68 69 73 20 69  nterface. This i
e2e40 73 20 61 20 6e 6f 2d 6f 70 20 66 6f 72 20 75 6e  s a no-op for un
e2e50 69 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ix..*/.SQLITE_AP
e2e60 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
e2e70 5f 65 6e 64 28 76 6f 69 64 29 7b 20 0a 20 20 72  _end(void){ .  r
e2e80 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
e2e90 20 0a 7d 0a 20 0a 23 65 6e 64 69 66 20 2f 2a 20   .}. .#endif /* 
e2ea0 53 51 4c 49 54 45 5f 4f 53 5f 55 4e 49 58 20 2a  SQLITE_OS_UNIX *
e2eb0 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /../************
e2ec0 2a 2a 20 45 6e 64 20 6f 66 20 6f 73 5f 75 6e 69  ** End of os_uni
e2ed0 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  x.c ************
e2ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2f00 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
e2f10 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6f 73  ** Begin file os
e2f20 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _win.c *********
e2f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e2f50 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 4d 61  */./*.** 2004 Ma
e2f60 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  y 22.**.** The a
e2f70 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20  uthor disclaims 
e2f80 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69  copyright to thi
e2f90 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20  s source code.  
e2fa0 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61  In place of.** a
e2fb0 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68   legal notice, h
e2fc0 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e  ere is a blessin
e2fd0 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20  g:.**.**    May 
e2fe0 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20  you do good and 
e2ff0 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20  not evil..**    
e3000 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72  May you find for
e3010 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75  giveness for you
e3020 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76  rself and forgiv
e3030 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20  e others..**    
e3040 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72  May you share fr
e3050 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69  eely, never taki
e3060 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75  ng more than you
e3070 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a   give..**.******
e3080 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3090 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e30a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e30b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e30c0 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54  ********.**.** T
e30d0 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
e30e0 73 20 63 6f 64 65 20 74 68 61 74 20 69 73 20 73  s code that is s
e30f0 70 65 63 69 66 69 63 20 74 6f 20 77 69 6e 64 6f  pecific to windo
e3100 77 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6f  ws..**.** $Id: o
e3110 73 5f 77 69 6e 2e 63 2c 76 20 31 2e 31 34 30 20  s_win.c,v 1.140 
e3120 32 30 30 38 2f 31 31 2f 31 39 20 32 31 3a 33 35  2008/11/19 21:35
e3130 3a 34 37 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :47 shane Exp $.
e3140 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  */.#if SQLITE_OS
e3150 5f 57 49 4e 20 20 20 20 20 20 20 20 20 20 20 20  _WIN            
e3160 20 20 20 2f 2a 20 54 68 69 73 20 66 69 6c 65 20     /* This file 
e3170 69 73 20 75 73 65 64 20 66 6f 72 20 77 69 6e 64  is used for wind
e3180 6f 77 73 20 6f 6e 6c 79 20 2a 2f 0a 0a 0a 2f 2a  ows only */.../*
e3190 0a 2a 2a 20 41 20 4e 6f 74 65 20 41 62 6f 75 74  .** A Note About
e31a0 20 4d 65 6d 6f 72 79 20 41 6c 6c 6f 63 61 74 69   Memory Allocati
e31b0 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 64  on:.**.** This d
e31c0 72 69 76 65 72 20 75 73 65 73 20 6d 61 6c 6c 6f  river uses mallo
e31d0 63 28 29 2f 66 72 65 65 28 29 20 64 69 72 65 63  c()/free() direc
e31e0 74 6c 79 20 72 61 74 68 65 72 20 74 68 61 6e 20  tly rather than 
e31f0 67 6f 69 6e 67 20 74 68 72 6f 75 67 68 0a 2a 2a  going through.**
e3200 20 74 68 65 20 53 51 4c 69 74 65 2d 77 72 61 70   the SQLite-wrap
e3210 70 65 72 73 20 73 71 6c 69 74 65 33 5f 6d 61 6c  pers sqlite3_mal
e3220 6c 6f 63 28 29 2f 73 71 6c 69 74 65 33 5f 66 72  loc()/sqlite3_fr
e3230 65 65 28 29 2e 20 20 54 68 6f 73 65 20 77 72 61  ee().  Those wra
e3240 70 70 65 72 73 0a 2a 2a 20 61 72 65 20 64 65 73  ppers.** are des
e3250 69 67 6e 65 64 20 66 6f 72 20 75 73 65 20 6f 6e  igned for use on
e3260 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
e3270 73 20 77 68 65 72 65 20 6d 65 6d 6f 72 79 20 69  s where memory i
e3280 73 20 73 63 61 72 63 65 20 61 6e 64 0a 2a 2a 20  s scarce and.** 
e3290 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65 73 20  malloc failures 
e32a0 68 61 70 70 65 6e 20 66 72 65 71 75 65 6e 74 6c  happen frequentl
e32b0 79 2e 20 20 57 69 6e 33 32 20 64 6f 65 73 20 6e  y.  Win32 does n
e32c0 6f 74 20 74 79 70 69 63 61 6c 6c 79 20 72 75 6e  ot typically run
e32d0 20 6f 6e 0a 2a 2a 20 65 6d 62 65 64 64 65 64 20   on.** embedded 
e32e0 73 79 73 74 65 6d 73 2c 20 61 6e 64 20 77 68 65  systems, and whe
e32f0 6e 20 69 74 20 64 6f 65 73 20 74 68 65 20 64 65  n it does the de
e3300 76 65 6c 6f 70 65 72 73 20 6e 6f 72 6d 61 6c 6c  velopers normall
e3310 79 20 68 61 76 65 20 62 69 67 67 65 72 0a 2a 2a  y have bigger.**
e3320 20 70 72 6f 62 6c 65 6d 73 20 74 6f 20 77 6f 72   problems to wor
e3330 72 79 20 61 62 6f 75 74 20 74 68 61 6e 20 72 75  ry about than ru
e3340 6e 6e 69 6e 67 20 6f 75 74 20 6f 66 20 6d 65 6d  nning out of mem
e3350 6f 72 79 2e 20 20 53 6f 20 74 68 65 72 65 20 69  ory.  So there i
e3360 73 20 6e 6f 74 0a 2a 2a 20 61 20 63 6f 6d 70 65  s not.** a compe
e3370 6c 6c 69 6e 67 20 6e 65 65 64 20 74 6f 20 75 73  lling need to us
e3380 65 20 74 68 65 20 77 72 61 70 70 65 72 73 2e 0a  e the wrappers..
e3390 2a 2a 0a 2a 2a 20 42 75 74 20 74 68 65 72 65 20  **.** But there 
e33a0 69 73 20 61 20 67 6f 6f 64 20 72 65 61 73 6f 6e  is a good reason
e33b0 20 74 6f 20 6e 6f 74 20 75 73 65 20 74 68 65 20   to not use the 
e33c0 77 72 61 70 70 65 72 73 2e 20 20 49 66 20 77 65  wrappers.  If we
e33d0 20 75 73 65 20 74 68 65 0a 2a 2a 20 77 72 61 70   use the.** wrap
e33e0 70 65 72 73 20 74 68 65 6e 20 77 65 20 77 69 6c  pers then we wil
e33f0 6c 20 67 65 74 20 73 69 6d 75 6c 61 74 65 64 20  l get simulated 
e3400 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 75 72 65  malloc() failure
e3410 73 20 77 69 74 68 69 6e 20 74 68 69 73 0a 2a 2a  s within this.**
e3420 20 64 72 69 76 65 72 2e 20 20 41 6e 64 20 74 68   driver.  And th
e3430 61 74 20 63 61 75 73 65 73 20 61 6c 6c 20 6b 69  at causes all ki
e3440 6e 64 73 20 6f 66 20 70 72 6f 62 6c 65 6d 73 20  nds of problems 
e3450 66 6f 72 20 6f 75 72 20 74 65 73 74 73 2e 20 20  for our tests.  
e3460 57 65 0a 2a 2a 20 63 6f 75 6c 64 20 65 6e 68 61  We.** could enha
e3470 6e 63 65 20 53 51 4c 69 74 65 20 74 6f 20 64 65  nce SQLite to de
e3480 61 6c 20 77 69 74 68 20 73 69 6d 75 6c 61 74 65  al with simulate
e3490 64 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75 72 65  d malloc failure
e34a0 73 20 77 69 74 68 69 6e 0a 2a 2a 20 74 68 65 20  s within.** the 
e34b0 4f 53 20 64 72 69 76 65 72 2c 20 62 75 74 20 74  OS driver, but t
e34c0 68 65 20 63 6f 64 65 20 74 6f 20 64 65 61 6c 20  he code to deal 
e34d0 77 69 74 68 20 74 68 6f 73 65 20 66 61 69 6c 75  with those failu
e34e0 72 65 20 77 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20  re would not.** 
e34f0 62 65 20 65 78 65 72 63 69 73 65 64 20 6f 6e 20  be exercised on 
e3500 4c 69 6e 75 78 20 28 77 68 69 63 68 20 64 6f 65  Linux (which doe
e3510 73 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20 6d 61  s not need to ma
e3520 6c 6c 6f 63 28 29 20 69 6e 20 74 68 65 20 64 72  lloc() in the dr
e3530 69 76 65 72 29 0a 2a 2a 20 61 6e 64 20 73 6f 20  iver).** and so 
e3540 77 65 20 77 6f 75 6c 64 20 68 61 76 65 20 64 69  we would have di
e3550 66 66 69 63 75 6c 74 79 20 77 72 69 74 69 6e 67  fficulty writing
e3560 20 63 6f 76 65 72 61 67 65 20 74 65 73 74 73 20   coverage tests 
e3570 66 6f 72 20 74 68 61 74 0a 2a 2a 20 63 6f 64 65  for that.** code
e3580 2e 20 20 42 65 74 74 65 72 20 74 6f 20 6c 65 61  .  Better to lea
e3590 76 65 20 74 68 65 20 63 6f 64 65 20 6f 75 74 2c  ve the code out,
e35a0 20 77 65 20 74 68 69 6e 6b 2e 0a 2a 2a 0a 2a 2a   we think..**.**
e35b0 20 54 68 65 20 70 6f 69 6e 74 20 6f 66 20 74 68   The point of th
e35c0 69 73 20 64 69 73 63 75 73 73 69 6f 6e 20 69 73  is discussion is
e35d0 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 20 57 68   as follows:  Wh
e35e0 65 6e 20 63 72 65 61 74 69 6e 67 20 61 20 6e 65  en creating a ne
e35f0 77 0a 2a 2a 20 4f 53 20 6c 61 79 65 72 20 66 6f  w.** OS layer fo
e3600 72 20 61 6e 20 65 6d 62 65 64 64 65 64 20 73 79  r an embedded sy
e3610 73 74 65 6d 2c 20 69 66 20 79 6f 75 20 75 73 65  stem, if you use
e3620 20 74 68 69 73 20 66 69 6c 65 20 61 73 20 61 6e   this file as an
e3630 20 65 78 61 6d 70 6c 65 2c 0a 2a 2a 20 61 76 6f   example,.** avo
e3640 69 64 20 74 68 65 20 75 73 65 20 6f 66 20 6d 61  id the use of ma
e3650 6c 6c 6f 63 28 29 2f 66 72 65 65 28 29 2e 20 20  lloc()/free().  
e3660 54 68 6f 73 65 20 72 6f 75 74 69 6e 65 73 20 77  Those routines w
e3670 6f 72 6b 20 6f 6b 20 6f 6e 20 77 69 6e 64 6f 77  ork ok on window
e3680 73 0a 2a 2a 20 64 65 73 6b 74 6f 70 73 20 62 75  s.** desktops bu
e3690 74 20 6e 6f 74 20 73 6f 20 77 65 6c 6c 20 69 6e  t not so well in
e36a0 20 65 6d 62 65 64 64 65 64 20 73 79 73 74 65 6d   embedded system
e36b0 73 2e 0a 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20  s..*/..#include 
e36c0 3c 77 69 6e 62 61 73 65 2e 68 3e 0a 0a 23 69 66  <winbase.h>..#if
e36d0 64 65 66 20 5f 5f 43 59 47 57 49 4e 5f 5f 0a 23  def __CYGWIN__.#
e36e0 20 69 6e 63 6c 75 64 65 20 3c 73 79 73 2f 63 79   include <sys/cy
e36f0 67 77 69 6e 2e 68 3e 0a 23 65 6e 64 69 66 0a 0a  gwin.h>.#endif..
e3700 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 75 73 65  /*.** Macros use
e3710 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 77  d to determine w
e3720 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f  hether or not to
e3730 20 75 73 65 20 74 68 72 65 61 64 73 2e 0a 2a 2f   use threads..*/
e3740 0a 23 69 66 20 64 65 66 69 6e 65 64 28 54 48 52  .#if defined(THR
e3750 45 41 44 53 41 46 45 29 20 26 26 20 54 48 52 45  EADSAFE) && THRE
e3760 41 44 53 41 46 45 0a 23 20 64 65 66 69 6e 65 20  ADSAFE.# define 
e3770 53 51 4c 49 54 45 5f 57 33 32 5f 54 48 52 45 41  SQLITE_W32_THREA
e3780 44 53 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  DS 1.#endif../*.
e3790 2a 2a 20 49 6e 63 6c 75 64 65 20 63 6f 64 65 20  ** Include code 
e37a0 74 68 61 74 20 69 73 20 63 6f 6d 6d 6f 6e 20 74  that is common t
e37b0 6f 20 61 6c 6c 20 6f 73 5f 2a 2e 63 20 66 69 6c  o all os_*.c fil
e37c0 65 73 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  es.*/./*********
e37d0 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 6f 73  ***** Include os
e37e0 5f 63 6f 6d 6d 6f 6e 2e 68 20 69 6e 20 74 68 65  _common.h in the
e37f0 20 6d 69 64 64 6c 65 20 6f 66 20 6f 73 5f 77 69   middle of os_wi
e3800 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.c ************
e3810 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
e3820 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
e3830 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a   os_common.h ***
e3840 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e3860 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
e3870 20 4d 61 79 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68   May 22.**.** Th
e3880 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
e3890 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
e38a0 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
e38b0 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
e38c0 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
e38d0 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
e38e0 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
e38f0 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
e3900 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
e3910 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
e3920 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
e3930 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
e3940 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
e3950 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
e3960 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
e3970 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
e3980 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
e3990 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e39a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e39b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e39c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e39d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a  ***********.**.*
e39e0 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
e39f0 61 69 6e 73 20 6d 61 63 72 6f 73 20 61 6e 64 20  ains macros and 
e3a00 61 20 6c 69 74 74 6c 65 20 62 69 74 20 6f 66 20  a little bit of 
e3a10 63 6f 64 65 20 74 68 61 74 20 69 73 20 63 6f 6d  code that is com
e3a20 6d 6f 6e 20 74 6f 0a 2a 2a 20 61 6c 6c 20 6f 66  mon to.** all of
e3a30 20 74 68 65 20 70 6c 61 74 66 6f 72 6d 2d 73 70   the platform-sp
e3a40 65 63 69 66 69 63 20 66 69 6c 65 73 20 28 6f 73  ecific files (os
e3a50 5f 2a 2e 63 29 20 61 6e 64 20 69 73 20 23 69 6e  _*.c) and is #in
e3a60 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68 6f 73  cluded into thos
e3a70 65 0a 2a 2a 20 66 69 6c 65 73 2e 0a 2a 2a 0a 2a  e.** files..**.*
e3a80 2a 20 54 68 69 73 20 66 69 6c 65 20 73 68 6f 75  * This file shou
e3a90 6c 64 20 62 65 20 23 69 6e 63 6c 75 64 65 64 20  ld be #included 
e3aa0 62 79 20 74 68 65 20 6f 73 5f 2a 2e 63 20 66 69  by the os_*.c fi
e3ab0 6c 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20 69 73  les only.  It is
e3ac0 20 6e 6f 74 20 61 0a 2a 2a 20 67 65 6e 65 72 61   not a.** genera
e3ad0 6c 20 70 75 72 70 6f 73 65 20 68 65 61 64 65 72  l purpose header
e3ae0 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64   file..**.** $Id
e3af0 3a 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 2c 76 20  : os_common.h,v 
e3b00 31 2e 33 37 20 32 30 30 38 2f 30 35 2f 32 39 20  1.37 2008/05/29 
e3b10 32 30 3a 32 32 3a 33 37 20 73 68 61 6e 65 20 45  20:22:37 shane E
e3b20 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  xp $.*/.#ifndef 
e3b30 5f 4f 53 5f 43 4f 4d 4d 4f 4e 5f 48 5f 0a 23 64  _OS_COMMON_H_.#d
e3b40 65 66 69 6e 65 20 5f 4f 53 5f 43 4f 4d 4d 4f 4e  efine _OS_COMMON
e3b50 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 41 74 20 6c 65  _H_../*.** At le
e3b60 61 73 74 20 74 77 6f 20 62 75 67 73 20 68 61 76  ast two bugs hav
e3b70 65 20 73 6c 69 70 70 65 64 20 69 6e 20 62 65 63  e slipped in bec
e3b80 61 75 73 65 20 77 65 20 63 68 61 6e 67 65 64 20  ause we changed 
e3b90 74 68 65 20 4d 45 4d 4f 52 59 5f 44 45 42 55 47  the MEMORY_DEBUG
e3ba0 0a 2a 2a 20 6d 61 63 72 6f 20 74 6f 20 53 51 4c  .** macro to SQL
e3bb0 49 54 45 5f 44 45 42 55 47 20 61 6e 64 20 73 6f  ITE_DEBUG and so
e3bc0 6d 65 20 6f 6c 64 65 72 20 6d 61 6b 65 66 69 6c  me older makefil
e3bd0 65 73 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20  es have not yet 
e3be0 6d 61 64 65 20 74 68 65 0a 2a 2a 20 73 77 69 74  made the.** swit
e3bf0 63 68 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ch.  The followi
e3c00 6e 67 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 63  ng code should c
e3c10 61 74 63 68 20 74 68 69 73 20 70 72 6f 62 6c 65  atch this proble
e3c20 6d 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  m at compile-tim
e3c30 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 4d 45 4d  e..*/.#ifdef MEM
e3c40 4f 52 59 5f 44 45 42 55 47 0a 23 20 65 72 72 6f  ORY_DEBUG.# erro
e3c50 72 20 22 54 68 65 20 4d 45 4d 4f 52 59 5f 44 45  r "The MEMORY_DE
e3c60 42 55 47 20 6d 61 63 72 6f 20 69 73 20 6f 62 73  BUG macro is obs
e3c70 6f 6c 65 74 65 2e 20 20 55 73 65 20 53 51 4c 49  olete.  Use SQLI
e3c80 54 45 5f 44 45 42 55 47 20 69 6e 73 74 65 61 64  TE_DEBUG instead
e3c90 2e 22 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 20  .".#endif.../*. 
e3ca0 2a 20 57 68 65 6e 20 74 65 73 74 69 6e 67 2c 20  * When testing, 
e3cb0 74 68 69 73 20 67 6c 6f 62 61 6c 20 76 61 72 69  this global vari
e3cc0 61 62 6c 65 20 73 74 6f 72 65 73 20 74 68 65 20  able stores the 
e3cd0 6c 6f 63 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  location of the.
e3ce0 20 2a 20 70 65 6e 64 69 6e 67 2d 62 79 74 65 20   * pending-byte 
e3cf0 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
e3d00 66 69 6c 65 2e 0a 20 2a 2f 0a 23 69 66 64 65 66  file.. */.#ifdef
e3d10 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
e3d20 49 54 45 5f 41 50 49 20 75 6e 73 69 67 6e 65 64  ITE_API unsigned
e3d30 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 70 65 6e   int sqlite3_pen
e3d40 64 69 6e 67 5f 62 79 74 65 20 3d 20 30 78 34 30  ding_byte = 0x40
e3d50 30 30 30 30 30 30 3b 0a 23 65 6e 64 69 66 0a 0a  000000;.#endif..
e3d60 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
e3d70 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
e3d80 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f 53  TE int sqlite3OS
e3d90 54 72 61 63 65 20 3d 20 30 3b 0a 23 64 65 66 69  Trace = 0;.#defi
e3da0 6e 65 20 4f 53 54 52 41 43 45 31 28 58 29 20 20  ne OSTRACE1(X)  
e3db0 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
e3dc0 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
e3dd0 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
e3de0 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
e3df0 45 32 28 58 2c 59 29 20 20 20 20 20 20 20 69 66  E2(X,Y)       if
e3e00 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  ( sqlite3OSTrace
e3e10 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50   ) sqlite3DebugP
e3e20 72 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69  rintf(X,Y).#defi
e3e30 6e 65 20 4f 53 54 52 41 43 45 33 28 58 2c 59 2c  ne OSTRACE3(X,Y,
e3e40 5a 29 20 20 20 20 20 69 66 28 20 73 71 6c 69 74  Z)     if( sqlit
e3e50 65 33 4f 53 54 72 61 63 65 20 29 20 73 71 6c 69  e3OSTrace ) sqli
e3e60 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 58  te3DebugPrintf(X
e3e70 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Y,Z).#define OS
e3e80 54 52 41 43 45 34 28 58 2c 59 2c 5a 2c 41 29 20  TRACE4(X,Y,Z,A) 
e3e90 20 20 69 66 28 20 73 71 6c 69 74 65 33 4f 53 54    if( sqlite3OST
e3ea0 72 61 63 65 20 29 20 73 71 6c 69 74 65 33 44 65  race ) sqlite3De
e3eb0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c  bugPrintf(X,Y,Z,
e3ec0 41 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41  A).#define OSTRA
e3ed0 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42 29 20 69  CE5(X,Y,Z,A,B) i
e3ee0 66 28 20 73 71 6c 69 74 65 33 4f 53 54 72 61 63  f( sqlite3OSTrac
e3ef0 65 20 29 20 73 71 6c 69 74 65 33 44 65 62 75 67  e ) sqlite3Debug
e3f00 50 72 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42  Printf(X,Y,Z,A,B
e3f10 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
e3f20 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 20  E6(X,Y,Z,A,B,C) 
e3f30 5c 0a 20 20 20 20 69 66 28 73 71 6c 69 74 65 33  \.    if(sqlite3
e3f40 4f 53 54 72 61 63 65 29 20 73 71 6c 69 74 65 33  OSTrace) sqlite3
e3f50 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
e3f60 5a 2c 41 2c 42 2c 43 29 0a 23 64 65 66 69 6e 65  Z,A,B,C).#define
e3f70 20 4f 53 54 52 41 43 45 37 28 58 2c 59 2c 5a 2c   OSTRACE7(X,Y,Z,
e3f80 41 2c 42 2c 43 2c 44 29 20 5c 0a 20 20 20 20 69  A,B,C,D) \.    i
e3f90 66 28 73 71 6c 69 74 65 33 4f 53 54 72 61 63 65  f(sqlite3OSTrace
e3fa0 29 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72  ) sqlite3DebugPr
e3fb0 69 6e 74 66 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43  intf(X,Y,Z,A,B,C
e3fc0 2c 44 29 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e  ,D).#else.#defin
e3fd0 65 20 4f 53 54 52 41 43 45 31 28 58 29 0a 23 64  e OSTRACE1(X).#d
e3fe0 65 66 69 6e 65 20 4f 53 54 52 41 43 45 32 28 58  efine OSTRACE2(X
e3ff0 2c 59 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52  ,Y).#define OSTR
e4000 41 43 45 33 28 58 2c 59 2c 5a 29 0a 23 64 65 66  ACE3(X,Y,Z).#def
e4010 69 6e 65 20 4f 53 54 52 41 43 45 34 28 58 2c 59  ine OSTRACE4(X,Y
e4020 2c 5a 2c 41 29 0a 23 64 65 66 69 6e 65 20 4f 53  ,Z,A).#define OS
e4030 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 41 2c 42  TRACE5(X,Y,Z,A,B
e4040 29 0a 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43  ).#define OSTRAC
e4050 45 36 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 29 0a  E6(X,Y,Z,A,B,C).
e4060 23 64 65 66 69 6e 65 20 4f 53 54 52 41 43 45 37  #define OSTRACE7
e4070 28 58 2c 59 2c 5a 2c 41 2c 42 2c 43 2c 44 29 0a  (X,Y,Z,A,B,C,D).
e4080 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 4d 61  #endif../*.** Ma
e4090 63 72 6f 73 20 66 6f 72 20 70 65 72 66 6f 72 6d  cros for perform
e40a0 61 6e 63 65 20 74 72 61 63 69 6e 67 2e 20 20 4e  ance tracing.  N
e40b0 6f 72 6d 61 6c 6c 79 20 74 75 72 6e 65 64 20 6f  ormally turned o
e40c0 66 66 2e 20 20 4f 6e 6c 79 20 77 6f 72 6b 73 0a  ff.  Only works.
e40d0 2a 2a 20 6f 6e 20 69 34 38 36 20 68 61 72 64 77  ** on i486 hardw
e40e0 61 72 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  are..*/.#ifdef S
e40f0 51 4c 49 54 45 5f 50 45 52 46 4f 52 4d 41 4e 43  QLITE_PERFORMANC
e4100 45 5f 54 52 41 43 45 0a 0a 2f 2a 20 0a 2a 2a 20  E_TRACE../* .** 
e4110 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74 61 69 6e  hwtime.h contain
e4120 73 20 69 6e 6c 69 6e 65 20 61 73 73 65 6d 62 6c  s inline assembl
e4130 65 72 20 63 6f 64 65 20 66 6f 72 20 69 6d 70 6c  er code for impl
e4140 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20 68 69 67  ementing .** hig
e4150 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65 20 74 69  h-performance ti
e4160 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73 2e 0a 2a  ming routines..*
e4170 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
e4180 2a 20 49 6e 63 6c 75 64 65 20 68 77 74 69 6d 65  * Include hwtime
e4190 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  .h in the middle
e41a0 20 6f 66 20 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20   of os_common.h 
e41b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e41c0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
e41d0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 68 77 74  * Begin file hwt
e41e0 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ime.h **********
e41f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4200 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4210 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79  /./*.** 2008 May
e4220 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   27.**.** The au
e4230 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
e4240 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
e4250 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
e4260 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
e4270 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
e4280 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
e4290 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
e42a0 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
e42b0 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
e42c0 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
e42d0 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
e42e0 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
e42f0 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
e4300 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
e4310 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
e4320 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
e4330 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
e4340 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4350 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4360 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4370 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4380 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68  *******.**.** Th
e4390 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
e43a0 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63 6f 64 65   inline asm code
e43b0 20 66 6f 72 20 72 65 74 72 69 65 76 69 6e 67 20   for retrieving 
e43c0 22 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63  "high-performanc
e43d0 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72 73 20 66  e".** counters f
e43e0 6f 72 20 78 38 36 20 63 6c 61 73 73 20 43 50 55  or x86 class CPU
e43f0 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 68 77  s..**.** $Id: hw
e4400 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20 32 30 30  time.h,v 1.3 200
e4410 38 2f 30 38 2f 30 31 20 31 34 3a 33 33 3a 31 35  8/08/01 14:33:15
e4420 20 73 68 61 6e 65 20 45 78 70 20 24 0a 2a 2f 0a   shane Exp $.*/.
e4430 23 69 66 6e 64 65 66 20 5f 48 57 54 49 4d 45 5f  #ifndef _HWTIME_
e4440 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48 57 54 49  H_.#define _HWTI
e4450 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  ME_H_../*.** The
e4460 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69   following routi
e4470 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e  ne only works on
e4480 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73 73 20 28   pentium-class (
e4490 6f 72 20 6e 65 77 65 72 29 20 70 72 6f 63 65 73  or newer) proces
e44a0 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75 73 65 73  sors..** It uses
e44b0 20 74 68 65 20 52 44 54 53 43 20 6f 70 63 6f 64   the RDTSC opcod
e44c0 65 20 74 6f 20 72 65 61 64 20 74 68 65 20 63 79  e to read the cy
e44d0 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c 75 65 20  cle count value 
e44e0 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a 20 70 72  out of the.** pr
e44f0 6f 63 65 73 73 6f 72 20 61 6e 64 20 72 65 74 75  ocessor and retu
e4500 72 6e 73 20 74 68 61 74 20 76 61 6c 75 65 2e 20  rns that value. 
e4510 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
e4520 64 20 66 6f 72 20 68 69 67 68 2d 72 65 73 0a 2a  d for high-res.*
e4530 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a 2a 2f 0a  * profiling..*/.
e4540 23 69 66 20 28 64 65 66 69 6e 65 64 28 5f 5f 47  #if (defined(__G
e4550 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66 69 6e 65  NUC__) || define
e4560 64 28 5f 4d 53 43 5f 56 45 52 29 29 20 26 26 20  d(_MSC_VER)) && 
e4570 5c 0a 20 20 20 20 20 20 28 64 65 66 69 6e 65 64  \.      (defined
e4580 28 69 33 38 36 29 20 7c 7c 20 64 65 66 69 6e 65  (i386) || define
e4590 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c 7c 20 64  d(__i386__) || d
e45a0 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38 36 29 29  efined(_M_IX86))
e45b0 0a 0a 20 20 23 69 66 20 64 65 66 69 6e 65 64 28  ..  #if defined(
e45c0 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20 5f 5f 69  __GNUC__)..  __i
e45d0 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75  nline__ sqlite_u
e45e0 69 6e 74 36 34 20 73 71 6c 69 74 65 33 48 77 74  int64 sqlite3Hwt
e45f0 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20 20 20 20  ime(void){.     
e4600 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 6c 6f 2c  unsigned int lo,
e4610 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61 73 6d 5f   hi;.     __asm_
e4620 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28  _ __volatile__ (
e4630 22 72 64 74 73 63 22 20 3a 20 22 3d 61 22 20 28  "rdtsc" : "=a" (
e4640 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69 29 29 3b  lo), "=d" (hi));
e4650 0a 20 20 20 20 20 72 65 74 75 72 6e 20 28 73 71  .     return (sq
e4660 6c 69 74 65 5f 75 69 6e 74 36 34 29 68 69 20 3c  lite_uint64)hi <
e4670 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20 7d 0a 0a  < 32 | lo;.  }..
e4680 20 20 23 65 6c 69 66 20 64 65 66 69 6e 65 64 28    #elif defined(
e4690 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20 5f 5f 64  _MSC_VER)..  __d
e46a0 65 63 6c 73 70 65 63 28 6e 61 6b 65 64 29 20 5f  eclspec(naked) _
e46b0 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74 65 5f 75  _inline sqlite_u
e46c0 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c 20 73 71  int64 __cdecl sq
e46d0 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64  lite3Hwtime(void
e46e0 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d 20 7b 0a  ){.     __asm {.
e46f0 20 20 20 20 20 20 20 20 72 64 74 73 63 0a 20 20          rdtsc.  
e4700 20 20 20 20 20 20 72 65 74 20 20 20 20 20 20 20        ret       
e4710 3b 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 61  ; return value a
e4720 74 20 45 44 58 3a 45 41 58 0a 20 20 20 20 20 7d  t EDX:EAX.     }
e4730 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69 66 0a 0a  .  }..  #endif..
e4740 23 65 6c 69 66 20 28 64 65 66 69 6e 65 64 28 5f  #elif (defined(_
e4750 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64 65 66 69  _GNUC__) && defi
e4760 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f 5f 29 29  ned(__x86_64__))
e4770 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73  ..  __inline__ s
e4780 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
e4790 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
e47a0 7b 0a 20 20 20 20 20 20 75 6e 73 69 67 6e 65 64  {.      unsigned
e47b0 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20 20 20 20   long val;.     
e47c0 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74   __asm__ __volat
e47d0 69 6c 65 5f 5f 20 28 22 72 64 74 73 63 22 20 3a  ile__ ("rdtsc" :
e47e0 20 22 3d 41 22 20 28 76 61 6c 29 29 3b 0a 20 20   "=A" (val));.  
e47f0 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
e4800 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28 64 65 66    }. .#elif (def
e4810 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26  ined(__GNUC__) &
e4820 26 20 64 65 66 69 6e 65 64 28 5f 5f 70 70 63 5f  & defined(__ppc_
e4830 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
e4840 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
e4850 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
e4860 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
e4870 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67 20 72 65  ned long long re
e4880 74 76 61 6c 3b 0a 20 20 20 20 20 20 75 6e 73 69  tval;.      unsi
e4890 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e 6b 3b 0a  gned long junk;.
e48a0 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f        __asm__ __
e48b0 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22 5c 6e 5c  volatile__ ("\n\
e48c0 0a 20 20 20 20 20 20 20 20 20 20 31 3a 20 20 20  .          1:   
e48d0 20 20 20 6d 66 74 62 75 20 20 20 25 31 5c 6e 5c     mftbu   %1\n\
e48e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
e48f0 20 20 20 6d 66 74 62 20 20 20 20 25 4c 30 5c 6e     mftb    %L0\n
e4900 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
e4910 20 20 20 20 6d 66 74 62 75 20 20 20 25 30 5c 6e      mftbu   %0\n
e4920 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  \.              
e4930 20 20 20 20 63 6d 70 77 20 20 20 20 25 30 2c 25      cmpw    %0,%
e4940 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  1\n\.           
e4950 20 20 20 20 20 20 20 62 6e 65 20 20 20 20 20 31         bne     1
e4960 62 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  b".             
e4970 20 20 20 20 20 3a 20 22 3d 72 22 20 28 72 65 74       : "=r" (ret
e4980 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a 75 6e 6b  val), "=r" (junk
e4990 29 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ));.      return
e49a0 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a 0a 23 65   retval;.  }..#e
e49b0 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72 20 4e 65  lse..  #error Ne
e49c0 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ed implementatio
e49d0 6e 20 6f 66 20 73 71 6c 69 74 65 33 48 77 74 69  n of sqlite3Hwti
e49e0 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c  me() for your pl
e49f0 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a 0a 20 20  atform...  /*.  
e4a00 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65 20 77 69  ** To compile wi
e4a10 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65 6e 74 69  thout implementi
e4a20 6e 67 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  ng sqlite3Hwtime
e4a30 28 29 20 66 6f 72 20 79 6f 75 72 20 70 6c 61 74  () for your plat
e4a40 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f 75 20 63  form,.  ** you c
e4a50 61 6e 20 72 65 6d 6f 76 65 20 74 68 65 20 61 62  an remove the ab
e4a60 6f 76 65 20 23 65 72 72 6f 72 20 61 6e 64 20 75  ove #error and u
e4a70 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  se the following
e4a80 0a 20 20 2a 2a 20 73 74 75 62 20 66 75 6e 63 74  .  ** stub funct
e4a90 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c 6c 20 6c  ion.  You will l
e4aa0 6f 73 65 20 74 69 6d 69 6e 67 20 73 75 70 70 6f  ose timing suppo
e4ab0 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20 20 2a 2a  rt for many.  **
e4ac0 20 6f 66 20 74 68 65 20 64 65 62 75 67 67 69 6e   of the debuggin
e4ad0 67 20 61 6e 64 20 74 65 73 74 69 6e 67 20 75 74  g and testing ut
e4ae0 69 6c 69 74 69 65 73 2c 20 62 75 74 20 69 74 20  ilities, but it 
e4af0 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a 2a 20 6c  should at.  ** l
e4b00 65 61 73 74 20 63 6f 6d 70 69 6c 65 20 61 6e 64  east compile and
e4b10 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51 4c 49 54   run..  */.SQLIT
e4b20 45 5f 50 52 49 56 41 54 45 20 20 20 73 71 6c 69  E_PRIVATE   sqli
e4b30 74 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65  te_uint64 sqlite
e4b40 33 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 20 72  3Hwtime(void){ r
e4b50 65 74 75 72 6e 20 28 28 73 71 6c 69 74 65 5f 75  eturn ((sqlite_u
e4b60 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a 23 65 6e  int64)0); }..#en
e4b70 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21  dif..#endif /* !
e4b80 64 65 66 69 6e 65 64 28 5f 48 57 54 49 4d 45 5f  defined(_HWTIME_
e4b90 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  H_) */../*******
e4ba0 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 68  ******* End of h
e4bb0 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  wtime.h ********
e4bc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4bd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e4be0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
e4bf0 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69  ******* Continui
e4c00 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66 74  ng where we left
e4c10 20 6f 66 66 20 69 6e 20 6f 73 5f 63 6f 6d 6d 6f   off in os_commo
e4c20 6e 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  n.h ************
e4c30 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 69 63 20  ******/..static 
e4c40 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f  sqlite_uint64 g_
e4c50 73 74 61 72 74 3b 0a 73 74 61 74 69 63 20 73 71  start;.static sq
e4c60 6c 69 74 65 5f 75 69 6e 74 36 34 20 67 5f 65 6c  lite_uint64 g_el
e4c70 61 70 73 65 64 3b 0a 23 64 65 66 69 6e 65 20 54  apsed;.#define T
e4c80 49 4d 45 52 5f 53 54 41 52 54 20 20 20 20 20 20  IMER_START      
e4c90 20 67 5f 73 74 61 72 74 3d 73 71 6c 69 74 65 33   g_start=sqlite3
e4ca0 48 77 74 69 6d 65 28 29 0a 23 64 65 66 69 6e 65  Hwtime().#define
e4cb0 20 54 49 4d 45 52 5f 45 4e 44 20 20 20 20 20 20   TIMER_END      
e4cc0 20 20 20 67 5f 65 6c 61 70 73 65 64 3d 73 71 6c     g_elapsed=sql
e4cd0 69 74 65 33 48 77 74 69 6d 65 28 29 2d 67 5f 73  ite3Hwtime()-g_s
e4ce0 74 61 72 74 0a 23 64 65 66 69 6e 65 20 54 49 4d  tart.#define TIM
e4cf0 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 67  ER_ELAPSED     g
e4d00 5f 65 6c 61 70 73 65 64 0a 23 65 6c 73 65 0a 23  _elapsed.#else.#
e4d10 64 65 66 69 6e 65 20 54 49 4d 45 52 5f 53 54 41  define TIMER_STA
e4d20 52 54 0a 23 64 65 66 69 6e 65 20 54 49 4d 45 52  RT.#define TIMER
e4d30 5f 45 4e 44 0a 23 64 65 66 69 6e 65 20 54 49 4d  _END.#define TIM
e4d40 45 52 5f 45 4c 41 50 53 45 44 20 20 20 20 20 28  ER_ELAPSED     (
e4d50 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 30  (sqlite_uint64)0
e4d60 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ).#endif../*.** 
e4d70 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20 77 69  If we compile wi
e4d80 74 68 20 74 68 65 20 53 51 4c 49 54 45 5f 54 45  th the SQLITE_TE
e4d90 53 54 20 6d 61 63 72 6f 20 73 65 74 2c 20 74 68  ST macro set, th
e4da0 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  en the following
e4db0 20 62 6c 6f 63 6b 0a 2a 2a 20 6f 66 20 63 6f 64   block.** of cod
e4dc0 65 20 77 69 6c 6c 20 67 69 76 65 20 75 73 20 74  e will give us t
e4dd0 68 65 20 61 62 69 6c 69 74 79 20 74 6f 20 73 69  he ability to si
e4de0 6d 75 6c 61 74 65 20 61 20 64 69 73 6b 20 49 2f  mulate a disk I/
e4df0 4f 20 65 72 72 6f 72 2e 20 20 54 68 69 73 0a 2a  O error.  This.*
e4e00 2a 20 69 73 20 75 73 65 64 20 66 6f 72 20 74 65  * is used for te
e4e10 73 74 69 6e 67 20 74 68 65 20 49 2f 4f 20 72 65  sting the I/O re
e4e20 63 6f 76 65 72 79 20 6c 6f 67 69 63 2e 0a 2a 2f  covery logic..*/
e4e30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
e4e40 45 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  EST.SQLITE_API i
e4e50 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  nt sqlite3_io_er
e4e60 72 6f 72 5f 68 69 74 20 3d 20 30 3b 20 20 20 20  ror_hit = 0;    
e4e70 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c          /* Total
e4e80 20 6e 75 6d 62 65 72 20 6f 66 20 49 2f 4f 20 45   number of I/O E
e4e90 72 72 6f 72 73 20 2a 2f 0a 53 51 4c 49 54 45 5f  rrors */.SQLITE_
e4ea0 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
e4eb0 69 6f 5f 65 72 72 6f 72 5f 68 61 72 64 68 69 74  io_error_hardhit
e4ec0 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20   = 0;        /* 
e4ed0 4e 75 6d 62 65 72 20 6f 66 20 6e 6f 6e 2d 62 65  Number of non-be
e4ee0 6e 69 67 6e 20 65 72 72 6f 72 73 20 2a 2f 0a 53  nign errors */.S
e4ef0 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
e4f00 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 70  lite3_io_error_p
e4f10 65 6e 64 69 6e 67 20 3d 20 30 3b 20 20 20 20 20  ending = 0;     
e4f20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 64 6f 77 6e     /* Count down
e4f30 20 74 6f 20 66 69 72 73 74 20 49 2f 4f 20 65 72   to first I/O er
e4f40 72 6f 72 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ror */.SQLITE_AP
e4f50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69 6f  I int sqlite3_io
e4f60 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20 3d  _error_persist =
e4f70 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72   0;        /* Tr
e4f80 75 65 20 69 66 20 49 2f 4f 20 65 72 72 6f 72 73  ue if I/O errors
e4f90 20 70 65 72 73 69 73 74 20 2a 2f 0a 53 51 4c 49   persist */.SQLI
e4fa0 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
e4fb0 65 33 5f 69 6f 5f 65 72 72 6f 72 5f 62 65 6e 69  e3_io_error_beni
e4fc0 67 6e 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  gn = 0;         
e4fd0 2f 2a 20 54 72 75 65 20 69 66 20 65 72 72 6f 72  /* True if error
e4fe0 73 20 61 72 65 20 62 65 6e 69 67 6e 20 2a 2f 0a  s are benign */.
e4ff0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
e5000 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c 6c 5f  qlite3_diskfull_
e5010 70 65 6e 64 69 6e 67 20 3d 20 30 3b 0a 53 51 4c  pending = 0;.SQL
e5020 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
e5030 74 65 33 5f 64 69 73 6b 66 75 6c 6c 20 3d 20 30  te3_diskfull = 0
e5040 3b 0a 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61  ;.#define Simula
e5050 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67 6e 28  teIOErrorBenign(
e5060 58 29 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  X) sqlite3_io_er
e5070 72 6f 72 5f 62 65 6e 69 67 6e 3d 28 58 29 0a 23  ror_benign=(X).#
e5080 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 49  define SimulateI
e5090 4f 45 72 72 6f 72 28 43 4f 44 45 29 20 20 5c 0a  OError(CODE)  \.
e50a0 20 20 69 66 28 20 28 73 71 6c 69 74 65 33 5f 69    if( (sqlite3_i
e50b0 6f 5f 65 72 72 6f 72 5f 70 65 72 73 69 73 74 20  o_error_persist 
e50c0 26 26 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72  && sqlite3_io_er
e50d0 72 6f 72 5f 68 69 74 29 20 5c 0a 20 20 20 20 20  ror_hit) \.     
e50e0 20 20 7c 7c 20 73 71 6c 69 74 65 33 5f 69 6f 5f    || sqlite3_io_
e50f0 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 2d 2d 20  error_pending-- 
e5100 3d 3d 20 31 20 29 20 20 5c 0a 20 20 20 20 20 20  == 1 )  \.      
e5110 20 20 20 20 20 20 20 20 7b 20 6c 6f 63 61 6c 5f          { local_
e5120 69 6f 65 72 72 28 29 3b 20 43 4f 44 45 3b 20 7d  ioerr(); CODE; }
e5130 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 6f 63  .static void loc
e5140 61 6c 5f 69 6f 65 72 72 28 29 7b 0a 20 20 49 4f  al_ioerr(){.  IO
e5150 54 52 41 43 45 28 28 22 49 4f 45 52 52 5c 6e 22  TRACE(("IOERR\n"
e5160 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f  ));.  sqlite3_io
e5170 5f 65 72 72 6f 72 5f 68 69 74 2b 2b 3b 0a 20 20  _error_hit++;.  
e5180 69 66 28 20 21 73 71 6c 69 74 65 33 5f 69 6f 5f  if( !sqlite3_io_
e5190 65 72 72 6f 72 5f 62 65 6e 69 67 6e 20 29 20 73  error_benign ) s
e51a0 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
e51b0 68 61 72 64 68 69 74 2b 2b 3b 0a 7d 0a 23 64 65  hardhit++;.}.#de
e51c0 66 69 6e 65 20 53 69 6d 75 6c 61 74 65 44 69 73  fine SimulateDis
e51d0 6b 66 75 6c 6c 45 72 72 6f 72 28 43 4f 44 45 29  kfullError(CODE)
e51e0 20 5c 0a 20 20 20 69 66 28 20 73 71 6c 69 74 65   \.   if( sqlite
e51f0 33 5f 64 69 73 6b 66 75 6c 6c 5f 70 65 6e 64 69  3_diskfull_pendi
e5200 6e 67 20 29 7b 20 5c 0a 20 20 20 20 20 69 66 28  ng ){ \.     if(
e5210 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75 6c   sqlite3_diskful
e5220 6c 5f 70 65 6e 64 69 6e 67 20 3d 3d 20 31 20 29  l_pending == 1 )
e5230 7b 20 5c 0a 20 20 20 20 20 20 20 6c 6f 63 61 6c  { \.       local
e5240 5f 69 6f 65 72 72 28 29 3b 20 5c 0a 20 20 20 20  _ioerr(); \.    
e5250 20 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66     sqlite3_diskf
e5260 75 6c 6c 20 3d 20 31 3b 20 5c 0a 20 20 20 20 20  ull = 1; \.     
e5270 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72    sqlite3_io_err
e5280 6f 72 5f 68 69 74 20 3d 20 31 3b 20 5c 0a 20 20  or_hit = 1; \.  
e5290 20 20 20 20 20 43 4f 44 45 3b 20 5c 0a 20 20 20       CODE; \.   
e52a0 20 20 7d 65 6c 73 65 7b 20 5c 0a 20 20 20 20 20    }else{ \.     
e52b0 20 20 73 71 6c 69 74 65 33 5f 64 69 73 6b 66 75    sqlite3_diskfu
e52c0 6c 6c 5f 70 65 6e 64 69 6e 67 2d 2d 3b 20 5c 0a  ll_pending--; \.
e52d0 20 20 20 20 20 7d 20 5c 0a 20 20 20 7d 0a 23 65       } \.   }.#e
e52e0 6c 73 65 0a 23 64 65 66 69 6e 65 20 53 69 6d 75  lse.#define Simu
e52f0 6c 61 74 65 49 4f 45 72 72 6f 72 42 65 6e 69 67  lateIOErrorBenig
e5300 6e 28 58 29 0a 23 64 65 66 69 6e 65 20 53 69 6d  n(X).#define Sim
e5310 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 41 29 0a  ulateIOError(A).
e5320 23 64 65 66 69 6e 65 20 53 69 6d 75 6c 61 74 65  #define Simulate
e5330 44 69 73 6b 66 75 6c 6c 45 72 72 6f 72 28 41 29  DiskfullError(A)
e5340 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
e5350 68 65 6e 20 74 65 73 74 69 6e 67 2c 20 6b 65 65  hen testing, kee
e5360 70 20 61 20 63 6f 75 6e 74 20 6f 66 20 74 68 65  p a count of the
e5370 20 6e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20   number of open 
e5380 66 69 6c 65 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  files..*/.#ifdef
e5390 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
e53a0 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
e53b0 74 65 33 5f 6f 70 65 6e 5f 66 69 6c 65 5f 63 6f  te3_open_file_co
e53c0 75 6e 74 20 3d 20 30 3b 0a 23 64 65 66 69 6e 65  unt = 0;.#define
e53d0 20 4f 70 65 6e 43 6f 75 6e 74 65 72 28 58 29 20   OpenCounter(X) 
e53e0 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 66 69   sqlite3_open_fi
e53f0 6c 65 5f 63 6f 75 6e 74 2b 3d 28 58 29 0a 23 65  le_count+=(X).#e
e5400 6c 73 65 0a 23 64 65 66 69 6e 65 20 4f 70 65 6e  lse.#define Open
e5410 43 6f 75 6e 74 65 72 28 58 29 0a 23 65 6e 64 69  Counter(X).#endi
e5420 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65  f..#endif /* !de
e5430 66 69 6e 65 64 28 5f 4f 53 5f 43 4f 4d 4d 4f 4e  fined(_OS_COMMON
e5440 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  _H_) */../******
e5450 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
e5460 6f 73 5f 63 6f 6d 6d 6f 6e 2e 68 20 2a 2a 2a 2a  os_common.h ****
e5470 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e5490 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
e54a0 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
e54b0 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
e54c0 74 20 6f 66 66 20 69 6e 20 6f 73 5f 77 69 6e 2e  t off in os_win.
e54d0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
e54e0 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  *******/../*.** 
e54f0 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63  Some microsoft c
e5500 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68  ompilers lack th
e5510 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a  is definition..*
e5520 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49  /.#ifndef INVALI
e5530 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
e5540 53 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41 4c  S.# define INVAL
e5550 49 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54  ID_FILE_ATTRIBUT
e5560 45 53 20 28 28 44 57 4f 52 44 29 2d 31 29 20 0a  ES ((DWORD)-1) .
e5570 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65  #endif../*.** De
e5580 74 65 72 6d 69 6e 65 20 69 66 20 77 65 20 61 72  termine if we ar
e5590 65 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 57  e dealing with W
e55a0 69 6e 64 6f 77 73 43 45 20 2d 20 77 68 69 63 68  indowsCE - which
e55b0 20 68 61 73 20 61 20 6d 75 63 68 0a 2a 2a 20 72   has a much.** r
e55c0 65 64 75 63 65 64 20 41 50 49 2e 0a 2a 2f 0a 23  educed API..*/.#
e55d0 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
e55e0 43 45 0a 23 20 64 65 66 69 6e 65 20 41 72 65 46  CE.# define AreF
e55f0 69 6c 65 41 70 69 73 41 4e 53 49 28 29 20 31 0a  ileApisANSI() 1.
e5600 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 69  #endif../*.** Wi
e5610 6e 43 45 20 6c 61 63 6b 73 20 6e 61 74 69 76 65  nCE lacks native
e5620 20 73 75 70 70 6f 72 74 20 66 6f 72 20 66 69 6c   support for fil
e5630 65 20 6c 6f 63 6b 69 6e 67 20 73 6f 20 77 65 20  e locking so we 
e5640 68 61 76 65 20 74 6f 20 66 61 6b 65 20 69 74 0a  have to fake it.
e5650 2a 2a 20 77 69 74 68 20 73 6f 6d 65 20 63 6f 64  ** with some cod
e5660 65 20 6f 66 20 6f 75 72 20 6f 77 6e 2e 0a 2a 2f  e of our own..*/
e5670 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
e5680 49 4e 43 45 0a 74 79 70 65 64 65 66 20 73 74 72  INCE.typedef str
e5690 75 63 74 20 77 69 6e 63 65 4c 6f 63 6b 20 7b 0a  uct winceLock {.
e56a0 20 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b 20    int nReaders; 
e56b0 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
e56c0 6f 66 20 72 65 61 64 65 72 20 6c 6f 63 6b 73 20  of reader locks 
e56d0 6f 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f  obtained */.  BO
e56e0 4f 4c 20 62 50 65 6e 64 69 6e 67 3b 20 20 20 20  OL bPending;    
e56f0 20 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61    /* Indicates a
e5700 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 68 61   pending lock ha
e5710 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20  s been obtained 
e5720 2a 2f 0a 20 20 42 4f 4f 4c 20 62 52 65 73 65 72  */.  BOOL bReser
e5730 76 65 64 3b 20 20 20 20 20 2f 2a 20 49 6e 64 69  ved;     /* Indi
e5740 63 61 74 65 73 20 61 20 72 65 73 65 72 76 65 64  cates a reserved
e5750 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e 20 6f   lock has been o
e5760 62 74 61 69 6e 65 64 20 2a 2f 0a 20 20 42 4f 4f  btained */.  BOO
e5770 4c 20 62 45 78 63 6c 75 73 69 76 65 3b 20 20 20  L bExclusive;   
e5780 20 2f 2a 20 49 6e 64 69 63 61 74 65 73 20 61 6e   /* Indicates an
e5790 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
e57a0 68 61 73 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  has been obtaine
e57b0 64 20 2a 2f 0a 7d 20 77 69 6e 63 65 4c 6f 63 6b  d */.} winceLock
e57c0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
e57d0 54 68 65 20 77 69 6e 46 69 6c 65 20 73 74 72 75  The winFile stru
e57e0 63 74 75 72 65 20 69 73 20 61 20 73 75 62 63 6c  cture is a subcl
e57f0 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33 5f 66  ass of sqlite3_f
e5800 69 6c 65 2a 20 73 70 65 63 69 66 69 63 20 74 6f  ile* specific to
e5810 20 74 68 65 20 77 69 6e 33 32 0a 2a 2a 20 70 6f   the win32.** po
e5820 72 74 61 62 69 6c 69 74 79 20 6c 61 79 65 72 2e  rtability layer.
e5830 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
e5840 63 74 20 77 69 6e 46 69 6c 65 20 77 69 6e 46 69  ct winFile winFi
e5850 6c 65 3b 0a 73 74 72 75 63 74 20 77 69 6e 46 69  le;.struct winFi
e5860 6c 65 20 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  le {.  const sql
e5870 69 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20  ite3_io_methods 
e5880 2a 70 4d 65 74 68 6f 64 3b 2f 2a 20 4d 75 73 74  *pMethod;/* Must
e5890 20 62 65 20 66 69 72 73 74 20 2a 2f 0a 20 20 48   be first */.  H
e58a0 41 4e 44 4c 45 20 68 3b 20 20 20 20 20 20 20 20  ANDLE h;        
e58b0 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c 65         /* Handle
e58c0 20 66 6f 72 20 61 63 63 65 73 73 69 6e 67 20 74   for accessing t
e58d0 68 65 20 66 69 6c 65 20 2a 2f 0a 20 20 75 6e 73  he file */.  uns
e58e0 69 67 6e 65 64 20 63 68 61 72 20 6c 6f 63 6b 74  igned char lockt
e58f0 79 70 65 3b 20 2f 2a 20 54 79 70 65 20 6f 66 20  ype; /* Type of 
e5900 6c 6f 63 6b 20 63 75 72 72 65 6e 74 6c 79 20 68  lock currently h
e5910 65 6c 64 20 6f 6e 20 74 68 69 73 20 66 69 6c 65  eld on this file
e5920 20 2a 2f 0a 20 20 73 68 6f 72 74 20 73 68 61 72   */.  short shar
e5930 65 64 4c 6f 63 6b 42 79 74 65 3b 20 20 20 2f 2a  edLockByte;   /*
e5940 20 52 61 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e   Randomly chosen
e5950 20 62 79 74 65 20 75 73 65 64 20 61 73 20 61 20   byte used as a 
e5960 73 68 61 72 65 64 20 6c 6f 63 6b 20 2a 2f 0a 23  shared lock */.#
e5970 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
e5980 43 45 0a 20 20 57 43 48 41 52 20 2a 7a 44 65 6c  CE.  WCHAR *zDel
e5990 65 74 65 4f 6e 43 6c 6f 73 65 3b 20 20 2f 2a 20  eteOnClose;  /* 
e59a0 4e 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20  Name of file to 
e59b0 64 65 6c 65 74 65 20 77 68 65 6e 20 63 6c 6f 73  delete when clos
e59c0 69 6e 67 20 2a 2f 0a 20 20 48 41 4e 44 4c 45 20  ing */.  HANDLE 
e59d0 68 4d 75 74 65 78 3b 20 20 20 20 20 20 20 20 20  hMutex;         
e59e0 20 2f 2a 20 4d 75 74 65 78 20 75 73 65 64 20 74   /* Mutex used t
e59f0 6f 20 63 6f 6e 74 72 6f 6c 20 61 63 63 65 73 73  o control access
e5a00 20 74 6f 20 73 68 61 72 65 64 20 6c 6f 63 6b 20   to shared lock 
e5a10 2a 2f 20 20 0a 20 20 48 41 4e 44 4c 45 20 68 53  */  .  HANDLE hS
e5a20 68 61 72 65 64 3b 20 20 20 20 20 20 20 20 20 2f  hared;         /
e5a30 2a 20 53 68 61 72 65 64 20 6d 65 6d 6f 72 79 20  * Shared memory 
e5a40 73 65 67 6d 65 6e 74 20 75 73 65 64 20 66 6f 72  segment used for
e5a50 20 6c 6f 63 6b 69 6e 67 20 2a 2f 0a 20 20 77 69   locking */.  wi
e5a60 6e 63 65 4c 6f 63 6b 20 6c 6f 63 61 6c 3b 20 20  nceLock local;  
e5a70 20 20 20 20 20 20 2f 2a 20 4c 6f 63 6b 73 20 6f        /* Locks o
e5a80 62 74 61 69 6e 65 64 20 62 79 20 74 68 69 73 20  btained by this 
e5a90 69 6e 73 74 61 6e 63 65 20 6f 66 20 77 69 6e 46  instance of winF
e5aa0 69 6c 65 20 2a 2f 0a 20 20 77 69 6e 63 65 4c 6f  ile */.  winceLo
e5ab0 63 6b 20 2a 73 68 61 72 65 64 3b 20 20 20 20 20  ck *shared;     
e5ac0 20 2f 2a 20 47 6c 6f 62 61 6c 20 73 68 61 72 65   /* Global share
e5ad0 64 20 6c 6f 63 6b 20 6d 65 6d 6f 72 79 20 66 6f  d lock memory fo
e5ae0 72 20 74 68 65 20 66 69 6c 65 20 20 2a 2f 0a 23  r the file  */.#
e5af0 65 6e 64 69 66 0a 7d 3b 0a 0a 0a 2f 2a 0a 2a 2a  endif.};.../*.**
e5b00 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76   The following v
e5b10 61 72 69 61 62 6c 65 20 69 73 20 28 6e 6f 72 6d  ariable is (norm
e5b20 61 6c 6c 79 29 20 73 65 74 20 6f 6e 63 65 20 61  ally) set once a
e5b30 6e 64 20 6e 65 76 65 72 20 63 68 61 6e 67 65 73  nd never changes
e5b40 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 2e 20  .** thereafter. 
e5b50 20 49 74 20 72 65 63 6f 72 64 73 20 77 68 65 74   It records whet
e5b60 68 65 72 20 74 68 65 20 6f 70 65 72 61 74 69 6e  her the operatin
e5b70 67 20 73 79 73 74 65 6d 20 69 73 20 57 69 6e 39  g system is Win9
e5b80 35 0a 2a 2a 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a  5.** or WinNT..*
e5b90 2a 0a 2a 2a 20 30 3a 20 20 20 4f 70 65 72 61 74  *.** 0:   Operat
e5ba0 69 6e 67 20 73 79 73 74 65 6d 20 75 6e 6b 6e 6f  ing system unkno
e5bb0 77 6e 2e 0a 2a 2a 20 31 3a 20 20 20 4f 70 65 72  wn..** 1:   Oper
e5bc0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 69 73 20  ating system is 
e5bd0 57 69 6e 39 35 2e 0a 2a 2a 20 32 3a 20 20 20 4f  Win95..** 2:   O
e5be0 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20  perating system 
e5bf0 69 73 20 57 69 6e 4e 54 2e 0a 2a 2a 0a 2a 2a 20  is WinNT..**.** 
e5c00 49 6e 20 6f 72 64 65 72 20 74 6f 20 66 61 63 69  In order to faci
e5c10 6c 69 74 61 74 65 20 74 65 73 74 69 6e 67 20 6f  litate testing o
e5c20 6e 20 61 20 57 69 6e 4e 54 20 73 79 73 74 65 6d  n a WinNT system
e5c30 2c 20 74 68 65 20 74 65 73 74 20 66 69 78 74 75  , the test fixtu
e5c40 72 65 0a 2a 2a 20 63 61 6e 20 6d 61 6e 75 61 6c  re.** can manual
e5c50 6c 79 20 73 65 74 20 74 68 69 73 20 76 61 6c 75  ly set this valu
e5c60 65 20 74 6f 20 31 20 74 6f 20 65 6d 75 6c 61 74  e to 1 to emulat
e5c70 65 20 57 69 6e 39 38 20 62 65 68 61 76 69 6f 72  e Win98 behavior
e5c80 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
e5c90 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41  TE_TEST.SQLITE_A
e5ca0 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f  PI int sqlite3_o
e5cb0 73 5f 74 79 70 65 20 3d 20 30 3b 0a 23 65 6c 73  s_type = 0;.#els
e5cc0 65 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  e.static int sql
e5cd0 69 74 65 33 5f 6f 73 5f 74 79 70 65 20 3d 20 30  ite3_os_type = 0
e5ce0 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
e5cf0 52 65 74 75 72 6e 20 74 72 75 65 20 28 6e 6f 6e  Return true (non
e5d00 2d 7a 65 72 6f 29 20 69 66 20 77 65 20 61 72 65  -zero) if we are
e5d10 20 72 75 6e 6e 69 6e 67 20 75 6e 64 65 72 20 57   running under W
e5d20 69 6e 4e 54 2c 20 57 69 6e 32 4b 2c 20 57 69 6e  inNT, Win2K, Win
e5d30 58 50 2c 0a 2a 2a 20 6f 72 20 57 69 6e 43 45 2e  XP,.** or WinCE.
e5d40 20 20 52 65 74 75 72 6e 20 66 61 6c 73 65 20 28    Return false (
e5d50 7a 65 72 6f 29 20 66 6f 72 20 57 69 6e 39 35 2c  zero) for Win95,
e5d60 20 57 69 6e 39 38 2c 20 6f 72 20 57 69 6e 4d 45   Win98, or WinME
e5d70 2e 0a 2a 2a 0a 2a 2a 20 48 65 72 65 20 69 73 20  ..**.** Here is 
e5d80 61 6e 20 69 6e 74 65 72 65 73 74 69 6e 67 20 6f  an interesting o
e5d90 62 73 65 72 76 61 74 69 6f 6e 3a 20 20 57 69 6e  bservation:  Win
e5da0 39 35 2c 20 57 69 6e 39 38 2c 20 61 6e 64 20 57  95, Win98, and W
e5db0 69 6e 4d 45 20 6c 61 63 6b 0a 2a 2a 20 74 68 65  inME lack.** the
e5dc0 20 4c 6f 63 6b 46 69 6c 65 45 78 28 29 20 41 50   LockFileEx() AP
e5dd0 49 2e 20 20 42 75 74 20 77 65 20 63 61 6e 20 73  I.  But we can s
e5de0 74 69 6c 6c 20 73 74 61 74 69 63 61 6c 6c 79 20  till statically 
e5df0 6c 69 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 61  link against tha
e5e00 74 0a 2a 2a 20 41 50 49 20 61 73 20 6c 6f 6e 67  t.** API as long
e5e10 20 61 73 20 77 65 20 64 6f 6e 27 74 20 63 61 6c   as we don't cal
e5e20 6c 20 69 74 20 77 69 6e 20 72 75 6e 6e 69 6e 67  l it win running
e5e30 20 57 69 6e 39 35 2f 39 38 2f 4d 45 2e 20 20 41   Win95/98/ME.  A
e5e40 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 74 68 69 73   call to.** this
e5e50 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
e5e60 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
e5e70 20 74 68 65 20 68 6f 73 74 20 69 73 20 57 69 6e   the host is Win
e5e80 39 35 2f 39 38 2f 4d 45 20 6f 72 0a 2a 2a 20 57  95/98/ME or.** W
e5e90 69 6e 4e 54 2f 32 4b 2f 58 50 20 73 6f 20 74 68  inNT/2K/XP so th
e5ea0 61 74 20 77 65 20 77 69 6c 6c 20 6b 6e 6f 77 20  at we will know 
e5eb0 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 77  whether or not w
e5ec0 65 20 63 61 6e 20 73 61 66 65 6c 79 20 63 61 6c  e can safely cal
e5ed0 6c 0a 2a 2a 20 74 68 65 20 4c 6f 63 6b 46 69 6c  l.** the LockFil
e5ee0 65 45 78 28 29 20 41 50 49 2e 0a 2a 2f 0a 23 69  eEx() API..*/.#i
e5ef0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
e5f00 45 0a 23 20 64 65 66 69 6e 65 20 69 73 4e 54 28  E.# define isNT(
e5f10 29 20 20 28 31 29 0a 23 65 6c 73 65 0a 20 20 73  )  (1).#else.  s
e5f20 74 61 74 69 63 20 69 6e 74 20 69 73 4e 54 28 76  tatic int isNT(v
e5f30 6f 69 64 29 7b 0a 20 20 20 20 69 66 28 20 73 71  oid){.    if( sq
e5f40 6c 69 74 65 33 5f 6f 73 5f 74 79 70 65 3d 3d 30  lite3_os_type==0
e5f50 20 29 7b 0a 20 20 20 20 20 20 4f 53 56 45 52 53   ){.      OSVERS
e5f60 49 4f 4e 49 4e 46 4f 20 73 49 6e 66 6f 3b 0a 20  IONINFO sInfo;. 
e5f70 20 20 20 20 20 73 49 6e 66 6f 2e 64 77 4f 53 56       sInfo.dwOSV
e5f80 65 72 73 69 6f 6e 49 6e 66 6f 53 69 7a 65 20 3d  ersionInfoSize =
e5f90 20 73 69 7a 65 6f 66 28 73 49 6e 66 6f 29 3b 0a   sizeof(sInfo);.
e5fa0 20 20 20 20 20 20 47 65 74 56 65 72 73 69 6f 6e        GetVersion
e5fb0 45 78 28 26 73 49 6e 66 6f 29 3b 0a 20 20 20 20  Ex(&sInfo);.    
e5fc0 20 20 73 71 6c 69 74 65 33 5f 6f 73 5f 74 79 70    sqlite3_os_typ
e5fd0 65 20 3d 20 73 49 6e 66 6f 2e 64 77 50 6c 61 74  e = sInfo.dwPlat
e5fe0 66 6f 72 6d 49 64 3d 3d 56 45 52 5f 50 4c 41 54  formId==VER_PLAT
e5ff0 46 4f 52 4d 5f 57 49 4e 33 32 5f 4e 54 20 3f 20  FORM_WIN32_NT ? 
e6000 32 20 3a 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  2 : 1;.    }.   
e6010 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
e6020 6f 73 5f 74 79 70 65 3d 3d 32 3b 0a 20 20 7d 0a  os_type==2;.  }.
e6030 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
e6040 5f 4f 53 5f 57 49 4e 43 45 20 2a 2f 0a 0a 2f 2a  _OS_WINCE */../*
e6050 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61 20 55 54  .** Convert a UT
e6060 46 2d 38 20 73 74 72 69 6e 67 20 74 6f 20 6d 69  F-8 string to mi
e6070 63 72 6f 73 6f 66 74 20 75 6e 69 63 6f 64 65 20  crosoft unicode 
e6080 28 55 54 46 2d 31 36 3f 29 2e 20 0a 2a 2a 0a 2a  (UTF-16?). .**.*
e6090 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20  * Space to hold 
e60a0 74 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72  the returned str
e60b0 69 6e 67 20 69 73 20 6f 62 74 61 69 6e 65 64 20  ing is obtained 
e60c0 66 72 6f 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a  from malloc..*/.
e60d0 73 74 61 74 69 63 20 57 43 48 41 52 20 2a 75 74  static WCHAR *ut
e60e0 66 38 54 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73  f8ToUnicode(cons
e60f0 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
e6100 65 29 7b 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b  e){.  int nChar;
e6110 0a 20 20 57 43 48 41 52 20 2a 7a 57 69 64 65 46  .  WCHAR *zWideF
e6120 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e 43 68 61  ilename;..  nCha
e6130 72 20 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57  r = MultiByteToW
e6140 69 64 65 43 68 61 72 28 43 50 5f 55 54 46 38 2c  ideChar(CP_UTF8,
e6150 20 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d   0, zFilename, -
e6160 31 2c 20 4e 55 4c 4c 2c 20 30 29 3b 0a 20 20 7a  1, NULL, 0);.  z
e6170 57 69 64 65 46 69 6c 65 6e 61 6d 65 20 3d 20 6d  WideFilename = m
e6180 61 6c 6c 6f 63 28 20 6e 43 68 61 72 2a 73 69 7a  alloc( nChar*siz
e6190 65 6f 66 28 7a 57 69 64 65 46 69 6c 65 6e 61 6d  eof(zWideFilenam
e61a0 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66 28 20 7a  e[0]) );.  if( z
e61b0 57 69 64 65 46 69 6c 65 6e 61 6d 65 3d 3d 30 20  WideFilename==0 
e61c0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
e61d0 0a 20 20 7d 0a 20 20 6e 43 68 61 72 20 3d 20 4d  .  }.  nChar = M
e61e0 75 6c 74 69 42 79 74 65 54 6f 57 69 64 65 43 68  ultiByteToWideCh
e61f0 61 72 28 43 50 5f 55 54 46 38 2c 20 30 2c 20 7a  ar(CP_UTF8, 0, z
e6200 46 69 6c 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 57  Filename, -1, zW
e6210 69 64 65 46 69 6c 65 6e 61 6d 65 2c 20 6e 43 68  ideFilename, nCh
e6220 61 72 29 3b 0a 20 20 69 66 28 20 6e 43 68 61 72  ar);.  if( nChar
e6230 3d 3d 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28  ==0 ){.    free(
e6240 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 3b 0a  zWideFilename);.
e6250 20 20 20 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d      zWideFilenam
e6260 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  e = 0;.  }.  ret
e6270 75 72 6e 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d  urn zWideFilenam
e6280 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  e;.}../*.** Conv
e6290 65 72 74 20 6d 69 63 72 6f 73 6f 66 74 20 75 6e  ert microsoft un
e62a0 69 63 6f 64 65 20 74 6f 20 55 54 46 2d 38 2e 20  icode to UTF-8. 
e62b0 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
e62c0 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
e62d0 6e 67 20 69 73 0a 2a 2a 20 6f 62 74 61 69 6e 65  ng is.** obtaine
e62e0 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e  d from malloc().
e62f0 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20  .*/.static char 
e6300 2a 75 6e 69 63 6f 64 65 54 6f 55 74 66 38 28 63  *unicodeToUtf8(c
e6310 6f 6e 73 74 20 57 43 48 41 52 20 2a 7a 57 69 64  onst WCHAR *zWid
e6320 65 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 69 6e  eFilename){.  in
e6330 74 20 6e 42 79 74 65 3b 0a 20 20 63 68 61 72 20  t nByte;.  char 
e6340 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a 0a 20 20 6e  *zFilename;..  n
e6350 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72 54  Byte = WideCharT
e6360 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55 54  oMultiByte(CP_UT
e6370 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65  F8, 0, zWideFile
e6380 6e 61 6d 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20  name, -1, 0, 0, 
e6390 30 2c 20 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61  0, 0);.  zFilena
e63a0 6d 65 20 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79  me = malloc( nBy
e63b0 74 65 20 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c  te );.  if( zFil
e63c0 65 6e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20  ename==0 ){.    
e63d0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
e63e0 6e 42 79 74 65 20 3d 20 57 69 64 65 43 68 61 72  nByte = WideChar
e63f0 54 6f 4d 75 6c 74 69 42 79 74 65 28 43 50 5f 55  ToMultiByte(CP_U
e6400 54 46 38 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c  TF8, 0, zWideFil
e6410 65 6e 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65  ename, -1, zFile
e6420 6e 61 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20  name, nByte,.   
e6430 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6440 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30 29             0, 0)
e6450 3b 0a 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d  ;.  if( nByte ==
e6460 20 30 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a   0 ){.    free(z
e6470 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a  Filename);.    z
e6480 46 69 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20  Filename = 0;.  
e6490 7d 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65  }.  return zFile
e64a0 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  name;.}../*.** C
e64b0 6f 6e 76 65 72 74 20 61 6e 20 61 6e 73 69 20 73  onvert an ansi s
e64c0 74 72 69 6e 67 20 74 6f 20 6d 69 63 72 6f 73 6f  tring to microso
e64d0 66 74 20 75 6e 69 63 6f 64 65 2c 20 62 61 73 65  ft unicode, base
e64e0 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72 72  d on the.** curr
e64f0 65 6e 74 20 63 6f 64 65 70 61 67 65 20 73 65 74  ent codepage set
e6500 74 69 6e 67 73 20 66 6f 72 20 66 69 6c 65 20 61  tings for file a
e6510 70 69 73 2e 0a 2a 2a 20 0a 2a 2a 20 53 70 61 63  pis..** .** Spac
e6520 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 65  e to hold the re
e6530 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73  turned string is
e6540 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f   obtained.** fro
e6550 6d 20 6d 61 6c 6c 6f 63 2e 0a 2a 2f 0a 73 74 61  m malloc..*/.sta
e6560 74 69 63 20 57 43 48 41 52 20 2a 6d 62 63 73 54  tic WCHAR *mbcsT
e6570 6f 55 6e 69 63 6f 64 65 28 63 6f 6e 73 74 20 63  oUnicode(const c
e6580 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
e6590 0a 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20  .  int nByte;.  
e65a0 57 43 48 41 52 20 2a 7a 4d 62 63 73 46 69 6c 65  WCHAR *zMbcsFile
e65b0 6e 61 6d 65 3b 0a 20 20 69 6e 74 20 63 6f 64 65  name;.  int code
e65c0 70 61 67 65 20 3d 20 41 72 65 46 69 6c 65 41 70  page = AreFileAp
e65d0 69 73 41 4e 53 49 28 29 20 3f 20 43 50 5f 41 43  isANSI() ? CP_AC
e65e0 50 20 3a 20 43 50 5f 4f 45 4d 43 50 3b 0a 0a 20  P : CP_OEMCP;.. 
e65f0 20 6e 42 79 74 65 20 3d 20 4d 75 6c 74 69 42 79   nByte = MultiBy
e6600 74 65 54 6f 57 69 64 65 43 68 61 72 28 63 6f 64  teToWideChar(cod
e6610 65 70 61 67 65 2c 20 30 2c 20 7a 46 69 6c 65 6e  epage, 0, zFilen
e6620 61 6d 65 2c 20 2d 31 2c 20 4e 55 4c 4c 2c 30 29  ame, -1, NULL,0)
e6630 2a 73 69 7a 65 6f 66 28 57 43 48 41 52 29 3b 0a  *sizeof(WCHAR);.
e6640 20 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 20    zMbcsFilename 
e6650 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a  = malloc( nByte*
e6660 73 69 7a 65 6f 66 28 7a 4d 62 63 73 46 69 6c 65  sizeof(zMbcsFile
e6670 6e 61 6d 65 5b 30 5d 29 20 29 3b 0a 20 20 69 66  name[0]) );.  if
e6680 28 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 3d  ( zMbcsFilename=
e6690 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
e66a0 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20   0;.  }.  nByte 
e66b0 3d 20 4d 75 6c 74 69 42 79 74 65 54 6f 57 69 64  = MultiByteToWid
e66c0 65 43 68 61 72 28 63 6f 64 65 70 61 67 65 2c 20  eChar(codepage, 
e66d0 30 2c 20 7a 46 69 6c 65 6e 61 6d 65 2c 20 2d 31  0, zFilename, -1
e66e0 2c 20 7a 4d 62 63 73 46 69 6c 65 6e 61 6d 65 2c  , zMbcsFilename,
e66f0 20 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 6e   nByte);.  if( n
e6700 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 66  Byte==0 ){.    f
e6710 72 65 65 28 7a 4d 62 63 73 46 69 6c 65 6e 61 6d  ree(zMbcsFilenam
e6720 65 29 3b 0a 20 20 20 20 7a 4d 62 63 73 46 69 6c  e);.    zMbcsFil
e6730 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  ename = 0;.  }. 
e6740 20 72 65 74 75 72 6e 20 7a 4d 62 63 73 46 69 6c   return zMbcsFil
e6750 65 6e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  ename;.}../*.** 
e6760 43 6f 6e 76 65 72 74 20 6d 69 63 72 6f 73 6f 66  Convert microsof
e6770 74 20 75 6e 69 63 6f 64 65 20 74 6f 20 6d 75 6c  t unicode to mul
e6780 74 69 62 79 74 65 20 63 68 61 72 61 63 74 65 72  tibyte character
e6790 20 73 74 72 69 6e 67 2c 20 62 61 73 65 64 20 6f   string, based o
e67a0 6e 20 74 68 65 0a 2a 2a 20 75 73 65 72 27 73 20  n the.** user's 
e67b0 41 6e 73 69 20 63 6f 64 65 70 61 67 65 2e 0a 2a  Ansi codepage..*
e67c0 2a 0a 2a 2a 20 53 70 61 63 65 20 74 6f 20 68 6f  *.** Space to ho
e67d0 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20  ld the returned 
e67e0 73 74 72 69 6e 67 20 69 73 20 6f 62 74 61 69 6e  string is obtain
e67f0 65 64 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f  ed from.** mallo
e6800 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  c()..*/.static c
e6810 68 61 72 20 2a 75 6e 69 63 6f 64 65 54 6f 4d 62  har *unicodeToMb
e6820 63 73 28 63 6f 6e 73 74 20 57 43 48 41 52 20 2a  cs(const WCHAR *
e6830 7a 57 69 64 65 46 69 6c 65 6e 61 6d 65 29 7b 0a  zWideFilename){.
e6840 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 63    int nByte;.  c
e6850 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 3b 0a  har *zFilename;.
e6860 20 20 69 6e 74 20 63 6f 64 65 70 61 67 65 20 3d    int codepage =
e6870 20 41 72 65 46 69 6c 65 41 70 69 73 41 4e 53 49   AreFileApisANSI
e6880 28 29 20 3f 20 43 50 5f 41 43 50 20 3a 20 43 50  () ? CP_ACP : CP
e6890 5f 4f 45 4d 43 50 3b 0a 0a 20 20 6e 42 79 74 65  _OEMCP;..  nByte
e68a0 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d 75 6c   = WideCharToMul
e68b0 74 69 42 79 74 65 28 63 6f 64 65 70 61 67 65 2c  tiByte(codepage,
e68c0 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e 61 6d   0, zWideFilenam
e68d0 65 2c 20 2d 31 2c 20 30 2c 20 30 2c 20 30 2c 20  e, -1, 0, 0, 0, 
e68e0 30 29 3b 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 20  0);.  zFilename 
e68f0 3d 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 20  = malloc( nByte 
e6900 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  );.  if( zFilena
e6910 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  me==0 ){.    ret
e6920 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 6e 42 79  urn 0;.  }.  nBy
e6930 74 65 20 3d 20 57 69 64 65 43 68 61 72 54 6f 4d  te = WideCharToM
e6940 75 6c 74 69 42 79 74 65 28 63 6f 64 65 70 61 67  ultiByte(codepag
e6950 65 2c 20 30 2c 20 7a 57 69 64 65 46 69 6c 65 6e  e, 0, zWideFilen
e6960 61 6d 65 2c 20 2d 31 2c 20 7a 46 69 6c 65 6e 61  ame, -1, zFilena
e6970 6d 65 2c 20 6e 42 79 74 65 2c 0a 20 20 20 20 20  me, nByte,.     
e6980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e6990 20 20 20 20 20 20 20 20 20 30 2c 20 30 29 3b 0a           0, 0);.
e69a0 20 20 69 66 28 20 6e 42 79 74 65 20 3d 3d 20 30    if( nByte == 0
e69b0 20 29 7b 0a 20 20 20 20 66 72 65 65 28 7a 46 69   ){.    free(zFi
e69c0 6c 65 6e 61 6d 65 29 3b 0a 20 20 20 20 7a 46 69  lename);.    zFi
e69d0 6c 65 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 7d 0a  lename = 0;.  }.
e69e0 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e 61    return zFilena
e69f0 6d 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  me;.}../*.** Con
e6a00 76 65 72 74 20 6d 75 6c 74 69 62 79 74 65 20 63  vert multibyte c
e6a10 68 61 72 61 63 74 65 72 20 73 74 72 69 6e 67 20  haracter string 
e6a20 74 6f 20 55 54 46 2d 38 2e 20 20 53 70 61 63 65  to UTF-8.  Space
e6a30 20 74 6f 20 68 6f 6c 64 20 74 68 65 0a 2a 2a 20   to hold the.** 
e6a40 72 65 74 75 72 6e 65 64 20 73 74 72 69 6e 67 20  returned string 
e6a50 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  is obtained from
e6a60 20 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51   malloc()..*/.SQ
e6a70 4c 49 54 45 5f 41 50 49 20 63 68 61 72 20 2a 73  LITE_API char *s
e6a80 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d 62 63  qlite3_win32_mbc
e6a90 73 5f 74 6f 5f 75 74 66 38 28 63 6f 6e 73 74 20  s_to_utf8(const 
e6aa0 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29  char *zFilename)
e6ab0 7b 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e  {.  char *zFilen
e6ac0 61 6d 65 55 74 66 38 3b 0a 20 20 57 43 48 41 52  ameUtf8;.  WCHAR
e6ad0 20 2a 7a 54 6d 70 57 69 64 65 3b 0a 0a 20 20 7a   *zTmpWide;..  z
e6ae0 54 6d 70 57 69 64 65 20 3d 20 6d 62 63 73 54 6f  TmpWide = mbcsTo
e6af0 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d  Unicode(zFilenam
e6b00 65 29 3b 0a 20 20 69 66 28 20 7a 54 6d 70 57 69  e);.  if( zTmpWi
e6b10 64 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  de==0 ){.    ret
e6b20 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 46 69  urn 0;.  }.  zFi
e6b30 6c 65 6e 61 6d 65 55 74 66 38 20 3d 20 75 6e 69  lenameUtf8 = uni
e6b40 63 6f 64 65 54 6f 55 74 66 38 28 7a 54 6d 70 57  codeToUtf8(zTmpW
e6b50 69 64 65 29 3b 0a 20 20 66 72 65 65 28 7a 54 6d  ide);.  free(zTm
e6b60 70 57 69 64 65 29 3b 0a 20 20 72 65 74 75 72 6e  pWide);.  return
e6b70 20 7a 46 69 6c 65 6e 61 6d 65 55 74 66 38 3b 0a   zFilenameUtf8;.
e6b80 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74  }../*.** Convert
e6b90 20 55 54 46 2d 38 20 74 6f 20 6d 75 6c 74 69 62   UTF-8 to multib
e6ba0 79 74 65 20 63 68 61 72 61 63 74 65 72 20 73 74  yte character st
e6bb0 72 69 6e 67 2e 20 20 53 70 61 63 65 20 74 6f 20  ring.  Space to 
e6bc0 68 6f 6c 64 20 74 68 65 20 0a 2a 2a 20 72 65 74  hold the .** ret
e6bd0 75 72 6e 65 64 20 73 74 72 69 6e 67 20 69 73 20  urned string is 
e6be0 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d 61  obtained from ma
e6bf0 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  lloc()..*/.stati
e6c00 63 20 63 68 61 72 20 2a 75 74 66 38 54 6f 4d 62  c char *utf8ToMb
e6c10 63 73 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  cs(const char *z
e6c20 46 69 6c 65 6e 61 6d 65 29 7b 0a 20 20 63 68 61  Filename){.  cha
e6c30 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 4d 62 63 73  r *zFilenameMbcs
e6c40 3b 0a 20 20 57 43 48 41 52 20 2a 7a 54 6d 70 57  ;.  WCHAR *zTmpW
e6c50 69 64 65 3b 0a 0a 20 20 7a 54 6d 70 57 69 64 65  ide;..  zTmpWide
e6c60 20 3d 20 75 74 66 38 54 6f 55 6e 69 63 6f 64 65   = utf8ToUnicode
e6c70 28 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69  (zFilename);.  i
e6c80 66 28 20 7a 54 6d 70 57 69 64 65 3d 3d 30 20 29  f( zTmpWide==0 )
e6c90 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
e6ca0 20 20 7d 0a 20 20 7a 46 69 6c 65 6e 61 6d 65 4d    }.  zFilenameM
e6cb0 62 63 73 20 3d 20 75 6e 69 63 6f 64 65 54 6f 4d  bcs = unicodeToM
e6cc0 62 63 73 28 7a 54 6d 70 57 69 64 65 29 3b 0a 20  bcs(zTmpWide);. 
e6cd0 20 66 72 65 65 28 7a 54 6d 70 57 69 64 65 29 3b   free(zTmpWide);
e6ce0 0a 20 20 72 65 74 75 72 6e 20 7a 46 69 6c 65 6e  .  return zFilen
e6cf0 61 6d 65 4d 62 63 73 3b 0a 7d 0a 0a 23 69 66 20  ameMbcs;.}..#if 
e6d00 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a  SQLITE_OS_WINCE.
e6d10 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
e6d20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6d30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6d40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e6d50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
e6d60 69 73 20 73 65 63 74 69 6f 6e 20 63 6f 6e 74 61  is section conta
e6d70 69 6e 73 20 63 6f 64 65 20 66 6f 72 20 57 69 6e  ins code for Win
e6d80 43 45 20 6f 6e 6c 79 2e 0a 2a 2f 0a 2f 2a 0a 2a  CE only..*/./*.*
e6d90 2a 20 57 69 6e 64 6f 77 73 43 45 20 64 6f 65 73  * WindowsCE does
e6da0 20 6e 6f 74 20 68 61 76 65 20 61 20 6c 6f 63 61   not have a loca
e6db0 6c 74 69 6d 65 28 29 20 66 75 6e 63 74 69 6f 6e  ltime() function
e6dc0 2e 20 20 53 6f 20 63 72 65 61 74 65 20 61 0a 2a  .  So create a.*
e6dd0 2a 20 73 75 62 73 74 69 74 75 74 65 2e 0a 2a 2f  * substitute..*/
e6de0 0a 73 74 72 75 63 74 20 74 6d 20 2a 5f 5f 63 64  .struct tm *__cd
e6df0 65 63 6c 20 6c 6f 63 61 6c 74 69 6d 65 28 63 6f  ecl localtime(co
e6e00 6e 73 74 20 74 69 6d 65 5f 74 20 2a 74 29 0a 7b  nst time_t *t).{
e6e10 0a 20 20 73 74 61 74 69 63 20 73 74 72 75 63 74  .  static struct
e6e20 20 74 6d 20 79 3b 0a 20 20 46 49 4c 45 54 49 4d   tm y;.  FILETIM
e6e30 45 20 75 54 6d 2c 20 6c 54 6d 3b 0a 20 20 53 59  E uTm, lTm;.  SY
e6e40 53 54 45 4d 54 49 4d 45 20 70 54 6d 3b 0a 20 20  STEMTIME pTm;.  
e6e50 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 74 36  sqlite3_int64 t6
e6e60 34 3b 0a 20 20 74 36 34 20 3d 20 2a 74 3b 0a 20  4;.  t64 = *t;. 
e6e70 20 74 36 34 20 3d 20 28 74 36 34 20 2b 20 31 31   t64 = (t64 + 11
e6e80 36 34 34 34 37 33 36 30 30 29 2a 31 30 30 30 30  644473600)*10000
e6e90 30 30 30 3b 0a 20 20 75 54 6d 2e 64 77 4c 6f 77  000;.  uTm.dwLow
e6ea0 44 61 74 65 54 69 6d 65 20 3d 20 74 36 34 20 26  DateTime = t64 &
e6eb0 20 30 78 46 46 46 46 46 46 46 46 3b 0a 20 20 75   0xFFFFFFFF;.  u
e6ec0 54 6d 2e 64 77 48 69 67 68 44 61 74 65 54 69 6d  Tm.dwHighDateTim
e6ed0 65 3d 20 74 36 34 20 3e 3e 20 33 32 3b 0a 20 20  e= t64 >> 32;.  
e6ee0 46 69 6c 65 54 69 6d 65 54 6f 4c 6f 63 61 6c 46  FileTimeToLocalF
e6ef0 69 6c 65 54 69 6d 65 28 26 75 54 6d 2c 26 6c 54  ileTime(&uTm,&lT
e6f00 6d 29 3b 0a 20 20 46 69 6c 65 54 69 6d 65 54 6f  m);.  FileTimeTo
e6f10 53 79 73 74 65 6d 54 69 6d 65 28 26 6c 54 6d 2c  SystemTime(&lTm,
e6f20 26 70 54 6d 29 3b 0a 20 20 79 2e 74 6d 5f 79 65  &pTm);.  y.tm_ye
e6f30 61 72 20 3d 20 70 54 6d 2e 77 59 65 61 72 20 2d  ar = pTm.wYear -
e6f40 20 31 39 30 30 3b 0a 20 20 79 2e 74 6d 5f 6d 6f   1900;.  y.tm_mo
e6f50 6e 20 3d 20 70 54 6d 2e 77 4d 6f 6e 74 68 20 2d  n = pTm.wMonth -
e6f60 20 31 3b 0a 20 20 79 2e 74 6d 5f 77 64 61 79 20   1;.  y.tm_wday 
e6f70 3d 20 70 54 6d 2e 77 44 61 79 4f 66 57 65 65 6b  = pTm.wDayOfWeek
e6f80 3b 0a 20 20 79 2e 74 6d 5f 6d 64 61 79 20 3d 20  ;.  y.tm_mday = 
e6f90 70 54 6d 2e 77 44 61 79 3b 0a 20 20 79 2e 74 6d  pTm.wDay;.  y.tm
e6fa0 5f 68 6f 75 72 20 3d 20 70 54 6d 2e 77 48 6f 75  _hour = pTm.wHou
e6fb0 72 3b 0a 20 20 79 2e 74 6d 5f 6d 69 6e 20 3d 20  r;.  y.tm_min = 
e6fc0 70 54 6d 2e 77 4d 69 6e 75 74 65 3b 0a 20 20 79  pTm.wMinute;.  y
e6fd0 2e 74 6d 5f 73 65 63 20 3d 20 70 54 6d 2e 77 53  .tm_sec = pTm.wS
e6fe0 65 63 6f 6e 64 3b 0a 20 20 72 65 74 75 72 6e 20  econd;.  return 
e6ff0 26 79 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 77  &y;.}../* This w
e7000 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 63 61 6c  ill never be cal
e7010 6c 65 64 2c 20 62 75 74 20 64 65 66 69 6e 65 64  led, but defined
e7020 20 74 6f 20 6d 61 6b 65 20 74 68 65 20 63 6f 64   to make the cod
e7030 65 20 63 6f 6d 70 69 6c 65 20 2a 2f 0a 23 64 65  e compile */.#de
e7040 66 69 6e 65 20 47 65 74 54 65 6d 70 50 61 74 68  fine GetTempPath
e7050 41 28 61 2c 62 29 0a 0a 23 64 65 66 69 6e 65 20  A(a,b)..#define 
e7060 4c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63 2c 64  LockFile(a,b,c,d
e7070 2c 65 29 20 20 20 20 20 20 20 77 69 6e 63 65 4c  ,e)       winceL
e7080 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c 20 63  ockFile(&a, b, c
e7090 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e 65 20  , d, e).#define 
e70a0 55 6e 6c 6f 63 6b 46 69 6c 65 28 61 2c 62 2c 63  UnlockFile(a,b,c
e70b0 2c 64 2c 65 29 20 20 20 20 20 77 69 6e 63 65 55  ,d,e)     winceU
e70c0 6e 6c 6f 63 6b 46 69 6c 65 28 26 61 2c 20 62 2c  nlockFile(&a, b,
e70d0 20 63 2c 20 64 2c 20 65 29 0a 23 64 65 66 69 6e   c, d, e).#defin
e70e0 65 20 4c 6f 63 6b 46 69 6c 65 45 78 28 61 2c 62  e LockFileEx(a,b
e70f0 2c 63 2c 64 2c 65 2c 66 29 20 20 20 77 69 6e 63  ,c,d,e,f)   winc
e7100 65 4c 6f 63 6b 46 69 6c 65 45 78 28 26 61 2c 20  eLockFileEx(&a, 
e7110 62 2c 20 63 2c 20 64 2c 20 65 2c 20 66 29 0a 0a  b, c, d, e, f)..
e7120 23 64 65 66 69 6e 65 20 48 41 4e 44 4c 45 5f 54  #define HANDLE_T
e7130 4f 5f 57 49 4e 46 49 4c 45 28 61 29 20 28 77 69  O_WINFILE(a) (wi
e7140 6e 46 69 6c 65 2a 29 26 28 28 63 68 61 72 2a 29  nFile*)&((char*)
e7150 61 29 5b 2d 6f 66 66 73 65 74 6f 66 28 77 69 6e  a)[-offsetof(win
e7160 46 69 6c 65 2c 68 29 5d 0a 0a 2f 2a 0a 2a 2a 20  File,h)]../*.** 
e7170 41 63 71 75 69 72 65 20 61 20 6c 6f 63 6b 20 6f  Acquire a lock o
e7180 6e 20 74 68 65 20 68 61 6e 64 6c 65 20 68 0a 2a  n the handle h.*
e7190 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 69  /.static void wi
e71a0 6e 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28  nceMutexAcquire(
e71b0 48 41 4e 44 4c 45 20 68 29 7b 0a 20 20 20 44 57  HANDLE h){.   DW
e71c0 4f 52 44 20 64 77 45 72 72 3b 0a 20 20 20 64 6f  ORD dwErr;.   do
e71d0 20 7b 0a 20 20 20 20 20 64 77 45 72 72 20 3d 20   {.     dwErr = 
e71e0 57 61 69 74 46 6f 72 53 69 6e 67 6c 65 4f 62 6a  WaitForSingleObj
e71f0 65 63 74 28 68 2c 20 49 4e 46 49 4e 49 54 45 29  ect(h, INFINITE)
e7200 3b 0a 20 20 20 7d 20 77 68 69 6c 65 20 28 64 77  ;.   } while (dw
e7210 45 72 72 20 21 3d 20 57 41 49 54 5f 4f 42 4a 45  Err != WAIT_OBJE
e7220 43 54 5f 30 20 26 26 20 64 77 45 72 72 20 21 3d  CT_0 && dwErr !=
e7230 20 57 41 49 54 5f 41 42 41 4e 44 4f 4e 45 44 29   WAIT_ABANDONED)
e7240 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73  ;.}./*.** Releas
e7250 65 20 61 20 6c 6f 63 6b 20 61 63 71 75 69 72 65  e a lock acquire
e7260 64 20 62 79 20 77 69 6e 63 65 4d 75 74 65 78 41  d by winceMutexA
e7270 63 71 75 69 72 65 28 29 0a 2a 2f 0a 23 64 65 66  cquire().*/.#def
e7280 69 6e 65 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ine winceMutexRe
e7290 6c 65 61 73 65 28 68 29 20 52 65 6c 65 61 73 65  lease(h) Release
e72a0 4d 75 74 65 78 28 68 29 0a 0a 2f 2a 0a 2a 2a 20  Mutex(h)../*.** 
e72b0 43 72 65 61 74 65 20 74 68 65 20 6d 75 74 65 78  Create the mutex
e72c0 20 61 6e 64 20 73 68 61 72 65 64 20 6d 65 6d 6f   and shared memo
e72d0 72 79 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  ry used for lock
e72e0 69 6e 67 20 69 6e 20 74 68 65 20 66 69 6c 65 0a  ing in the file.
e72f0 2a 2a 20 64 65 73 63 72 69 70 74 6f 72 20 70 46  ** descriptor pF
e7300 69 6c 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f  ile.*/.static BO
e7310 4f 4c 20 77 69 6e 63 65 43 72 65 61 74 65 4c 6f  OL winceCreateLo
e7320 63 6b 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  ck(const char *z
e7330 46 69 6c 65 6e 61 6d 65 2c 20 77 69 6e 46 69 6c  Filename, winFil
e7340 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 57 43 48  e *pFile){.  WCH
e7350 41 52 20 2a 7a 54 6f 6b 3b 0a 20 20 57 43 48 41  AR *zTok;.  WCHA
e7360 52 20 2a 7a 4e 61 6d 65 20 3d 20 75 74 66 38 54  R *zName = utf8T
e7370 6f 55 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61  oUnicode(zFilena
e7380 6d 65 29 3b 0a 20 20 42 4f 4f 4c 20 62 49 6e 69  me);.  BOOL bIni
e7390 74 20 3d 20 54 52 55 45 3b 0a 0a 20 20 2f 2a 20  t = TRUE;..  /* 
e73a0 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 6c  Initialize the l
e73b0 6f 63 61 6c 20 6c 6f 63 6b 64 61 74 61 20 2a 2f  ocal lockdata */
e73c0 0a 20 20 5a 65 72 6f 4d 65 6d 6f 72 79 28 26 70  .  ZeroMemory(&p
e73d0 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2c 20 73 69 7a  File->local, siz
e73e0 65 6f 66 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  eof(pFile->local
e73f0 29 29 3b 0a 0a 20 20 2f 2a 20 52 65 70 6c 61 63  ));..  /* Replac
e7400 65 20 74 68 65 20 62 61 63 6b 73 6c 61 73 68 65  e the backslashe
e7410 73 20 66 72 6f 6d 20 74 68 65 20 66 69 6c 65 6e  s from the filen
e7420 61 6d 65 20 61 6e 64 20 6c 6f 77 65 72 63 61 73  ame and lowercas
e7430 65 20 69 74 0a 20 20 2a 2a 20 74 6f 20 64 65 72  e it.  ** to der
e7440 69 76 65 20 61 20 6d 75 74 65 78 20 6e 61 6d 65  ive a mutex name
e7450 2e 20 2a 2f 0a 20 20 7a 54 6f 6b 20 3d 20 43 68  . */.  zTok = Ch
e7460 61 72 4c 6f 77 65 72 57 28 7a 4e 61 6d 65 29 3b  arLowerW(zName);
e7470 0a 20 20 66 6f 72 20 28 3b 2a 7a 54 6f 6b 3b 7a  .  for (;*zTok;z
e7480 54 6f 6b 2b 2b 29 7b 0a 20 20 20 20 69 66 20 28  Tok++){.    if (
e7490 2a 7a 54 6f 6b 20 3d 3d 20 27 5c 5c 27 29 20 2a  *zTok == '\\') *
e74a0 7a 54 6f 6b 20 3d 20 27 5f 27 3b 0a 20 20 7d 0a  zTok = '_';.  }.
e74b0 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 6f 70 65  .  /* Create/ope
e74c0 6e 20 74 68 65 20 6e 61 6d 65 64 20 6d 75 74 65  n the named mute
e74d0 78 20 2a 2f 0a 20 20 70 46 69 6c 65 2d 3e 68 4d  x */.  pFile->hM
e74e0 75 74 65 78 20 3d 20 43 72 65 61 74 65 4d 75 74  utex = CreateMut
e74f0 65 78 57 28 4e 55 4c 4c 2c 20 46 41 4c 53 45 2c  exW(NULL, FALSE,
e7500 20 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 20 28 21   zName);.  if (!
e7510 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a  pFile->hMutex){.
e7520 20 20 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b      free(zName);
e7530 0a 20 20 20 20 72 65 74 75 72 6e 20 46 41 4c 53  .    return FALS
e7540 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71  E;.  }..  /* Acq
e7550 75 69 72 65 20 74 68 65 20 6d 75 74 65 78 20 62  uire the mutex b
e7560 65 66 6f 72 65 20 63 6f 6e 74 69 6e 75 69 6e 67  efore continuing
e7570 20 2a 2f 0a 20 20 77 69 6e 63 65 4d 75 74 65 78   */.  winceMutex
e7580 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e 68  Acquire(pFile->h
e7590 4d 75 74 65 78 29 3b 0a 20 20 0a 20 20 2f 2a 20  Mutex);.  .  /* 
e75a0 53 69 6e 63 65 20 74 68 65 20 6e 61 6d 65 73 20  Since the names 
e75b0 6f 66 20 6e 61 6d 65 64 20 6d 75 74 65 78 65 73  of named mutexes
e75c0 2c 20 73 65 6d 61 70 68 6f 72 65 73 2c 20 66 69  , semaphores, fi
e75d0 6c 65 20 6d 61 70 70 69 6e 67 73 20 65 74 63 20  le mappings etc 
e75e0 61 72 65 20 0a 20 20 2a 2a 20 63 61 73 65 2d 73  are .  ** case-s
e75f0 65 6e 73 69 74 69 76 65 2c 20 74 61 6b 65 20 61  ensitive, take a
e7600 64 76 61 6e 74 61 67 65 20 6f 66 20 74 68 61 74  dvantage of that
e7610 20 62 79 20 75 70 70 65 72 63 61 73 69 6e 67 20   by uppercasing 
e7620 74 68 65 20 6d 75 74 65 78 20 6e 61 6d 65 0a 20  the mutex name. 
e7630 20 2a 2a 20 61 6e 64 20 75 73 69 6e 67 20 74 68   ** and using th
e7640 61 74 20 61 73 20 74 68 65 20 73 68 61 72 65 64  at as the shared
e7650 20 66 69 6c 65 6d 61 70 70 69 6e 67 20 6e 61 6d   filemapping nam
e7660 65 2e 0a 20 20 2a 2f 0a 20 20 43 68 61 72 55 70  e..  */.  CharUp
e7670 70 65 72 57 28 7a 4e 61 6d 65 29 3b 0a 20 20 70  perW(zName);.  p
e7680 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d 20  File->hShared = 
e7690 43 72 65 61 74 65 46 69 6c 65 4d 61 70 70 69 6e  CreateFileMappin
e76a0 67 57 28 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c  gW(INVALID_HANDL
e76b0 45 5f 56 41 4c 55 45 2c 20 4e 55 4c 4c 2c 0a 20  E_VALUE, NULL,. 
e76c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e76d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e76e0 20 20 20 20 20 20 50 41 47 45 5f 52 45 41 44 57        PAGE_READW
e76f0 52 49 54 45 2c 20 30 2c 20 73 69 7a 65 6f 66 28  RITE, 0, sizeof(
e7700 77 69 6e 63 65 4c 6f 63 6b 29 2c 0a 20 20 20 20  winceLock),.    
e7710 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7720 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
e7730 20 20 20 7a 4e 61 6d 65 29 3b 20 20 0a 0a 20 20     zName);  ..  
e7740 2f 2a 20 53 65 74 20 61 20 66 6c 61 67 20 74 68  /* Set a flag th
e7750 61 74 20 69 6e 64 69 63 61 74 65 73 20 77 65 27  at indicates we'
e7760 72 65 20 74 68 65 20 66 69 72 73 74 20 74 6f 20  re the first to 
e7770 63 72 65 61 74 65 20 74 68 65 20 6d 65 6d 6f 72  create the memor
e7780 79 20 73 6f 20 69 74 20 0a 20 20 2a 2a 20 6d 75  y so it .  ** mu
e7790 73 74 20 62 65 20 7a 65 72 6f 2d 69 6e 69 74 69  st be zero-initi
e77a0 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 69 66 20 28  alized */.  if (
e77b0 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 20 3d  GetLastError() =
e77c0 3d 20 45 52 52 4f 52 5f 41 4c 52 45 41 44 59 5f  = ERROR_ALREADY_
e77d0 45 58 49 53 54 53 29 7b 0a 20 20 20 20 62 49 6e  EXISTS){.    bIn
e77e0 69 74 20 3d 20 46 41 4c 53 45 3b 0a 20 20 7d 0a  it = FALSE;.  }.
e77f0 0a 20 20 66 72 65 65 28 7a 4e 61 6d 65 29 3b 0a  .  free(zName);.
e7800 0a 20 20 2f 2a 20 49 66 20 77 65 20 73 75 63 63  .  /* If we succ
e7810 65 65 64 65 64 20 69 6e 20 6d 61 6b 69 6e 67 20  eeded in making 
e7820 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
e7830 79 20 68 61 6e 64 6c 65 2c 20 6d 61 70 20 69 74  y handle, map it
e7840 2e 20 2a 2f 0a 20 20 69 66 20 28 70 46 69 6c 65  . */.  if (pFile
e7850 2d 3e 68 53 68 61 72 65 64 29 7b 0a 20 20 20 20  ->hShared){.    
e7860 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 20 3d 20  pFile->shared = 
e7870 28 77 69 6e 63 65 4c 6f 63 6b 2a 29 4d 61 70 56  (winceLock*)MapV
e7880 69 65 77 4f 66 46 69 6c 65 28 70 46 69 6c 65 2d  iewOfFile(pFile-
e7890 3e 68 53 68 61 72 65 64 2c 20 0a 20 20 20 20 20  >hShared, .     
e78a0 20 20 20 20 20 20 20 20 46 49 4c 45 5f 4d 41 50          FILE_MAP
e78b0 5f 52 45 41 44 7c 46 49 4c 45 5f 4d 41 50 5f 57  _READ|FILE_MAP_W
e78c0 52 49 54 45 2c 20 30 2c 20 30 2c 20 73 69 7a 65  RITE, 0, 0, size
e78d0 6f 66 28 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a  of(winceLock));.
e78e0 20 20 20 20 2f 2a 20 49 66 20 6d 61 70 70 69 6e      /* If mappin
e78f0 67 20 66 61 69 6c 65 64 2c 20 63 6c 6f 73 65 20  g failed, close 
e7900 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d 6f 72  the shared memor
e7910 79 20 68 61 6e 64 6c 65 20 61 6e 64 20 65 72 61  y handle and era
e7920 73 65 20 69 74 20 2a 2f 0a 20 20 20 20 69 66 20  se it */.    if 
e7930 28 21 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 29  (!pFile->shared)
e7940 7b 0a 20 20 20 20 20 20 43 6c 6f 73 65 48 61 6e  {.      CloseHan
e7950 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  dle(pFile->hShar
e7960 65 64 29 3b 0a 20 20 20 20 20 20 70 46 69 6c 65  ed);.      pFile
e7970 2d 3e 68 53 68 61 72 65 64 20 3d 20 4e 55 4c 4c  ->hShared = NULL
e7980 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
e7990 2a 20 49 66 20 73 68 61 72 65 64 20 6d 65 6d 6f  * If shared memo
e79a0 72 79 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ry could not be 
e79b0 63 72 65 61 74 65 64 2c 20 74 68 65 6e 20 63 6c  created, then cl
e79c0 6f 73 65 20 74 68 65 20 6d 75 74 65 78 20 61 6e  ose the mutex an
e79d0 64 20 66 61 69 6c 20 2a 2f 0a 20 20 69 66 20 28  d fail */.  if (
e79e0 70 46 69 6c 65 2d 3e 68 53 68 61 72 65 64 20 3d  pFile->hShared =
e79f0 3d 20 4e 55 4c 4c 29 7b 0a 20 20 20 20 77 69 6e  = NULL){.    win
e7a00 63 65 4d 75 74 65 78 52 65 6c 65 61 73 65 28 70  ceMutexRelease(p
e7a10 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20  File->hMutex);. 
e7a20 20 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70     CloseHandle(p
e7a30 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20  File->hMutex);. 
e7a40 20 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78     pFile->hMutex
e7a50 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74   = NULL;.    ret
e7a60 75 72 6e 20 46 41 4c 53 45 3b 0a 20 20 7d 0a 20  urn FALSE;.  }. 
e7a70 20 0a 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a   .  /* Initializ
e7a80 65 20 74 68 65 20 73 68 61 72 65 64 20 6d 65 6d  e the shared mem
e7a90 6f 72 79 20 69 66 20 77 65 27 72 65 20 73 75 70  ory if we're sup
e7aa0 70 6f 73 65 64 20 74 6f 20 2a 2f 0a 20 20 69 66  posed to */.  if
e7ab0 20 28 62 49 6e 69 74 29 20 7b 0a 20 20 20 20 5a   (bInit) {.    Z
e7ac0 65 72 6f 4d 65 6d 6f 72 79 28 70 46 69 6c 65 2d  eroMemory(pFile-
e7ad0 3e 73 68 61 72 65 64 2c 20 73 69 7a 65 6f 66 28  >shared, sizeof(
e7ae0 77 69 6e 63 65 4c 6f 63 6b 29 29 3b 0a 20 20 7d  winceLock));.  }
e7af0 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ..  winceMutexRe
e7b00 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75  lease(pFile->hMu
e7b10 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 54  tex);.  return T
e7b20 52 55 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  RUE;.}../*.** De
e7b30 73 74 72 6f 79 20 74 68 65 20 70 61 72 74 20 6f  stroy the part o
e7b40 66 20 77 69 6e 46 69 6c 65 20 74 68 61 74 20 64  f winFile that d
e7b50 65 61 6c 73 20 77 69 74 68 20 77 69 6e 63 65 20  eals with wince 
e7b60 6c 6f 63 6b 73 0a 2a 2f 0a 73 74 61 74 69 63 20  locks.*/.static 
e7b70 76 6f 69 64 20 77 69 6e 63 65 44 65 73 74 72 6f  void winceDestro
e7b80 79 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20 2a 70  yLock(winFile *p
e7b90 46 69 6c 65 29 7b 0a 20 20 69 66 20 28 70 46 69  File){.  if (pFi
e7ba0 6c 65 2d 3e 68 4d 75 74 65 78 29 7b 0a 20 20 20  le->hMutex){.   
e7bb0 20 2f 2a 20 41 63 71 75 69 72 65 20 74 68 65 20   /* Acquire the 
e7bc0 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 77 69 6e  mutex */.    win
e7bd0 63 65 4d 75 74 65 78 41 63 71 75 69 72 65 28 70  ceMutexAcquire(p
e7be0 46 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 0a  File->hMutex);..
e7bf0 20 20 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f      /* The follo
e7c00 77 69 6e 67 20 62 6c 6f 63 6b 73 20 73 68 6f 75  wing blocks shou
e7c10 6c 64 20 70 72 6f 62 61 62 6c 79 20 61 73 73 65  ld probably asse
e7c20 72 74 20 69 6e 20 64 65 62 75 67 20 6d 6f 64 65  rt in debug mode
e7c30 2c 20 62 75 74 20 74 68 65 79 0a 20 20 20 20 20  , but they.     
e7c40 20 20 61 72 65 20 74 6f 20 63 6c 65 61 6e 75 70    are to cleanup
e7c50 20 69 6e 20 63 61 73 65 20 61 6e 79 20 6c 6f 63   in case any loc
e7c60 6b 73 20 72 65 6d 61 69 6e 65 64 20 6f 70 65 6e  ks remained open
e7c70 20 2a 2f 0a 20 20 20 20 69 66 20 28 70 46 69 6c   */.    if (pFil
e7c80 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
e7c90 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c 65 2d  s){.      pFile-
e7ca0 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64 65 72  >shared->nReader
e7cb0 73 20 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  s --;.    }.    
e7cc0 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c  if (pFile->local
e7cd0 2e 62 52 65 73 65 72 76 65 64 29 7b 0a 20 20 20  .bReserved){.   
e7ce0 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64     pFile->shared
e7cf0 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 20 46 41  ->bReserved = FA
e7d00 4c 53 45 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  LSE;.    }.    i
e7d10 66 20 28 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e  f (pFile->local.
e7d20 62 50 65 6e 64 69 6e 67 29 7b 0a 20 20 20 20 20  bPending){.     
e7d30 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e   pFile->shared->
e7d40 62 50 65 6e 64 69 6e 67 20 3d 20 46 41 4c 53 45  bPending = FALSE
e7d50 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 20 28  ;.    }.    if (
e7d60 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78  pFile->local.bEx
e7d70 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20  clusive){.      
e7d80 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62  pFile->shared->b
e7d90 45 78 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53  Exclusive = FALS
e7da0 45 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  E;.    }..    /*
e7db0 20 44 65 2d 72 65 66 65 72 65 6e 63 65 20 61 6e   De-reference an
e7dc0 64 20 63 6c 6f 73 65 20 6f 75 72 20 63 6f 70 79  d close our copy
e7dd0 20 6f 66 20 74 68 65 20 73 68 61 72 65 64 20 6d   of the shared m
e7de0 65 6d 6f 72 79 20 68 61 6e 64 6c 65 20 2a 2f 0a  emory handle */.
e7df0 20 20 20 20 55 6e 6d 61 70 56 69 65 77 4f 66 46      UnmapViewOfF
e7e00 69 6c 65 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  ile(pFile->share
e7e10 64 29 3b 0a 20 20 20 20 43 6c 6f 73 65 48 61 6e  d);.    CloseHan
e7e20 64 6c 65 28 70 46 69 6c 65 2d 3e 68 53 68 61 72  dle(pFile->hShar
e7e30 65 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 6f 6e  ed);..    /* Don
e7e40 65 20 77 69 74 68 20 74 68 65 20 6d 75 74 65 78  e with the mutex
e7e50 20 2a 2f 0a 20 20 20 20 77 69 6e 63 65 4d 75 74   */.    winceMut
e7e60 65 78 52 65 6c 65 61 73 65 28 70 46 69 6c 65 2d  exRelease(pFile-
e7e70 3e 68 4d 75 74 65 78 29 3b 20 20 20 20 0a 20 20  >hMutex);    .  
e7e80 20 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28 70 46    CloseHandle(pF
e7e90 69 6c 65 2d 3e 68 4d 75 74 65 78 29 3b 0a 20 20  ile->hMutex);.  
e7ea0 20 20 70 46 69 6c 65 2d 3e 68 4d 75 74 65 78 20    pFile->hMutex 
e7eb0 3d 20 4e 55 4c 4c 3b 0a 20 20 7d 0a 7d 0a 0a 2f  = NULL;.  }.}../
e7ec0 2a 20 0a 2a 2a 20 41 6e 20 69 6d 70 6c 65 6d 65  * .** An impleme
e7ed0 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c  ntation of the L
e7ee0 6f 63 6b 46 69 6c 65 28 29 20 41 50 49 20 6f 66  ockFile() API of
e7ef0 20 77 69 6e 64 6f 77 73 20 66 6f 72 20 77 69 6e   windows for win
e7f00 63 65 0a 2a 2f 0a 73 74 61 74 69 63 20 42 4f 4f  ce.*/.static BOO
e7f10 4c 20 77 69 6e 63 65 4c 6f 63 6b 46 69 6c 65 28  L winceLockFile(
e7f20 0a 20 20 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c  .  HANDLE *phFil
e7f30 65 2c 0a 20 20 44 57 4f 52 44 20 64 77 46 69 6c  e,.  DWORD dwFil
e7f40 65 4f 66 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57  eOffsetLow,.  DW
e7f50 4f 52 44 20 64 77 46 69 6c 65 4f 66 66 73 65 74  ORD dwFileOffset
e7f60 48 69 67 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  High,.  DWORD nN
e7f70 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
e7f80 63 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e  ckLow,.  DWORD n
e7f90 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
e7fa0 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69 6e  ockHigh.){.  win
e7fb0 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48 41  File *pFile = HA
e7fc0 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45 28  NDLE_TO_WINFILE(
e7fd0 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c 20  phFile);.  BOOL 
e7fe0 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45 3b  bReturn = FALSE;
e7ff0 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d 3e  ..  if (!pFile->
e8000 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20 54  hMutex) return T
e8010 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74 65  RUE;.  winceMute
e8020 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d 3e  xAcquire(pFile->
e8030 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20 57  hMutex);..  /* W
e8040 61 6e 74 69 6e 67 20 61 6e 20 65 78 63 6c 75 73  anting an exclus
e8050 69 76 65 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20 69  ive lock? */.  i
e8060 66 20 28 64 77 46 69 6c 65 4f 66 66 73 65 74 4c  f (dwFileOffsetL
e8070 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 46 49 52  ow == SHARED_FIR
e8080 53 54 0a 20 20 20 20 20 20 20 26 26 20 6e 4e 75  ST.       && nNu
e8090 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
e80a0 6b 4c 6f 77 20 3d 3d 20 53 48 41 52 45 44 5f 53  kLow == SHARED_S
e80b0 49 5a 45 29 7b 0a 20 20 20 20 69 66 20 28 70 46  IZE){.    if (pF
e80c0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65  ile->shared->nRe
e80d0 61 64 65 72 73 20 3d 3d 20 30 20 26 26 20 70 46  aders == 0 && pF
e80e0 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45 78  ile->shared->bEx
e80f0 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a 20  clusive == 0){. 
e8100 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61        pFile->sha
e8110 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65 20  red->bExclusive 
e8120 3d 20 54 52 55 45 3b 0a 20 20 20 20 20 20 20 70  = TRUE;.       p
e8130 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78 63  File->local.bExc
e8140 6c 75 73 69 76 65 20 3d 20 54 52 55 45 3b 0a 20  lusive = TRUE;. 
e8150 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
e8160 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
e8170 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 61  .  /* Want a rea
e8180 64 2d 6f 6e 6c 79 20 6c 6f 63 6b 3f 20 2a 2f 0a  d-only lock? */.
e8190 20 20 65 6c 73 65 20 69 66 20 28 28 64 77 46 69    else if ((dwFi
e81a0 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3e 3d 20 53  leOffsetLow >= S
e81b0 48 41 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20  HARED_FIRST &&. 
e81c0 20 20 20 20 20 20 20 20 20 20 20 64 77 46 69 6c             dwFil
e81d0 65 4f 66 66 73 65 74 4c 6f 77 20 3c 20 53 48 41  eOffsetLow < SHA
e81e0 52 45 44 5f 46 49 52 53 54 20 2b 20 53 48 41 52  RED_FIRST + SHAR
e81f0 45 44 5f 53 49 5a 45 29 20 26 26 0a 20 20 20 20  ED_SIZE) &&.    
e8200 20 20 20 20 20 20 20 20 6e 4e 75 6d 62 65 72 4f          nNumberO
e8210 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77 20  fBytesToLockLow 
e8220 3d 3d 20 31 29 7b 0a 20 20 20 20 69 66 20 28 70  == 1){.    if (p
e8230 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 45  File->shared->bE
e8240 78 63 6c 75 73 69 76 65 20 3d 3d 20 30 29 7b 0a  xclusive == 0){.
e8250 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f 63        pFile->loc
e8260 61 6c 2e 6e 52 65 61 64 65 72 73 20 2b 2b 3b 0a  al.nReaders ++;.
e8270 20 20 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d        if (pFile-
e8280 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72 73 20  >local.nReaders 
e8290 3d 3d 20 31 29 7b 0a 20 20 20 20 20 20 20 20 70  == 1){.        p
e82a0 46 69 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52  File->shared->nR
e82b0 65 61 64 65 72 73 20 2b 2b 3b 0a 20 20 20 20 20  eaders ++;.     
e82c0 20 7d 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e   }.      bReturn
e82d0 20 3d 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20   = TRUE;.    }. 
e82e0 20 7d 0a 0a 20 20 2f 2a 20 57 61 6e 74 20 61 20   }..  /* Want a 
e82f0 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 3f 20 2a 2f  pending lock? */
e8300 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46 69  .  else if (dwFi
e8310 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 50  leOffsetLow == P
e8320 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20 6e  ENDING_BYTE && n
e8330 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c  NumberOfBytesToL
e8340 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20  ockLow == 1){.  
e8350 20 20 2f 2a 20 49 66 20 6e 6f 20 70 65 6e 64 69    /* If no pendi
e8360 6e 67 20 6c 6f 63 6b 20 68 61 73 20 62 65 65 6e  ng lock has been
e8370 20 61 63 71 75 69 72 65 64 2c 20 74 68 65 6e 20   acquired, then 
e8380 61 63 71 75 69 72 65 20 69 74 20 2a 2f 0a 20 20  acquire it */.  
e8390 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61    if (pFile->sha
e83a0 72 65 64 2d 3e 62 50 65 6e 64 69 6e 67 20 3d 3d  red->bPending ==
e83b0 20 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c   0) {.      pFil
e83c0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
e83d0 69 6e 67 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  ing = TRUE;.    
e83e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
e83f0 50 65 6e 64 69 6e 67 20 3d 20 54 52 55 45 3b 0a  Pending = TRUE;.
e8400 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
e8410 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
e8420 20 20 2f 2a 20 57 61 6e 74 20 61 20 72 65 73 65    /* Want a rese
e8430 72 76 65 64 20 6c 6f 63 6b 3f 20 2a 2f 0a 20 20  rved lock? */.  
e8440 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65 4f  else if (dwFileO
e8450 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 45 53 45  ffsetLow == RESE
e8460 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e 4e 75  RVED_BYTE && nNu
e8470 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
e8480 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20 20 20 20  kLow == 1){.    
e8490 69 66 20 28 70 46 69 6c 65 2d 3e 73 68 61 72 65  if (pFile->share
e84a0 64 2d 3e 62 52 65 73 65 72 76 65 64 20 3d 3d 20  d->bReserved == 
e84b0 30 29 20 7b 0a 20 20 20 20 20 20 70 46 69 6c 65  0) {.      pFile
e84c0 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73 65 72  ->shared->bReser
e84d0 76 65 64 20 3d 20 54 52 55 45 3b 0a 20 20 20 20  ved = TRUE;.    
e84e0 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62    pFile->local.b
e84f0 52 65 73 65 72 76 65 64 20 3d 20 54 52 55 45 3b  Reserved = TRUE;
e8500 0a 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d  .      bReturn =
e8510 20 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d   TRUE;.    }.  }
e8520 0a 0a 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65  ..  winceMutexRe
e8530 6c 65 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75  lease(pFile->hMu
e8540 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62  tex);.  return b
e8550 52 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Return;.}../*.**
e8560 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69   An implementati
e8570 6f 6e 20 6f 66 20 74 68 65 20 55 6e 6c 6f 63 6b  on of the Unlock
e8580 46 69 6c 65 20 41 50 49 20 6f 66 20 77 69 6e 64  File API of wind
e8590 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f  ows for wince.*/
e85a0 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e  .static BOOL win
e85b0 63 65 55 6e 6c 6f 63 6b 46 69 6c 65 28 0a 20 20  ceUnlockFile(.  
e85c0 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a  HANDLE *phFile,.
e85d0 20 20 44 57 4f 52 44 20 64 77 46 69 6c 65 4f 66    DWORD dwFileOf
e85e0 66 73 65 74 4c 6f 77 2c 0a 20 20 44 57 4f 52 44  fsetLow,.  DWORD
e85f0 20 64 77 46 69 6c 65 4f 66 66 73 65 74 48 69 67   dwFileOffsetHig
e8600 68 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75 6d 62  h,.  DWORD nNumb
e8610 65 72 4f 66 42 79 74 65 73 54 6f 55 6e 6c 6f 63  erOfBytesToUnloc
e8620 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  kLow,.  DWORD nN
e8630 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
e8640 6c 6f 63 6b 48 69 67 68 0a 29 7b 0a 20 20 77 69  lockHigh.){.  wi
e8650 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 48  nFile *pFile = H
e8660 41 4e 44 4c 45 5f 54 4f 5f 57 49 4e 46 49 4c 45  ANDLE_TO_WINFILE
e8670 28 70 68 46 69 6c 65 29 3b 0a 20 20 42 4f 4f 4c  (phFile);.  BOOL
e8680 20 62 52 65 74 75 72 6e 20 3d 20 46 41 4c 53 45   bReturn = FALSE
e8690 3b 0a 0a 20 20 69 66 20 28 21 70 46 69 6c 65 2d  ;..  if (!pFile-
e86a0 3e 68 4d 75 74 65 78 29 20 72 65 74 75 72 6e 20  >hMutex) return 
e86b0 54 52 55 45 3b 0a 20 20 77 69 6e 63 65 4d 75 74  TRUE;.  winceMut
e86c0 65 78 41 63 71 75 69 72 65 28 70 46 69 6c 65 2d  exAcquire(pFile-
e86d0 3e 68 4d 75 74 65 78 29 3b 0a 0a 20 20 2f 2a 20  >hMutex);..  /* 
e86e0 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65 61 64  Releasing a read
e86f0 65 72 20 6c 6f 63 6b 20 6f 72 20 61 6e 20 65 78  er lock or an ex
e8700 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20 2a 2f 0a  clusive lock */.
e8710 20 20 69 66 20 28 64 77 46 69 6c 65 4f 66 66 73    if (dwFileOffs
e8720 65 74 4c 6f 77 20 3e 3d 20 53 48 41 52 45 44 5f  etLow >= SHARED_
e8730 46 49 52 53 54 20 26 26 0a 20 20 20 20 20 20 20  FIRST &&.       
e8740 64 77 46 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20  dwFileOffsetLow 
e8750 3c 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b  < SHARED_FIRST +
e8760 20 53 48 41 52 45 44 5f 53 49 5a 45 29 7b 0a 20   SHARED_SIZE){. 
e8770 20 20 20 2f 2a 20 44 69 64 20 77 65 20 68 61 76     /* Did we hav
e8780 65 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  e an exclusive l
e8790 6f 63 6b 3f 20 2a 2f 0a 20 20 20 20 69 66 20 28  ock? */.    if (
e87a0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78  pFile->local.bEx
e87b0 63 6c 75 73 69 76 65 29 7b 0a 20 20 20 20 20 20  clusive){.      
e87c0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 62 45 78  pFile->local.bEx
e87d0 63 6c 75 73 69 76 65 20 3d 20 46 41 4c 53 45 3b  clusive = FALSE;
e87e0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 73 68  .      pFile->sh
e87f0 61 72 65 64 2d 3e 62 45 78 63 6c 75 73 69 76 65  ared->bExclusive
e8800 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20 20 20 20   = FALSE;.      
e8810 62 52 65 74 75 72 6e 20 3d 20 54 52 55 45 3b 0a  bReturn = TRUE;.
e8820 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 69      }..    /* Di
e8830 64 20 77 65 20 6a 75 73 74 20 68 61 76 65 20 61  d we just have a
e8840 20 72 65 61 64 65 72 20 6c 6f 63 6b 3f 20 2a 2f   reader lock? */
e8850 0a 20 20 20 20 65 6c 73 65 20 69 66 20 28 70 46  .    else if (pF
e8860 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64  ile->local.nRead
e8870 65 72 73 29 7b 0a 20 20 20 20 20 20 70 46 69 6c  ers){.      pFil
e8880 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65 61 64 65 72  e->local.nReader
e8890 73 20 2d 2d 3b 0a 20 20 20 20 20 20 69 66 20 28  s --;.      if (
e88a0 70 46 69 6c 65 2d 3e 6c 6f 63 61 6c 2e 6e 52 65  pFile->local.nRe
e88b0 61 64 65 72 73 20 3d 3d 20 30 29 0a 20 20 20 20  aders == 0).    
e88c0 20 20 7b 0a 20 20 20 20 20 20 20 20 70 46 69 6c    {.        pFil
e88d0 65 2d 3e 73 68 61 72 65 64 2d 3e 6e 52 65 61 64  e->shared->nRead
e88e0 65 72 73 20 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  ers --;.      }.
e88f0 20 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20        bReturn = 
e8900 54 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  TRUE;.    }.  }.
e8910 0a 20 20 2f 2a 20 52 65 6c 65 61 73 69 6e 67 20  .  /* Releasing 
e8920 61 20 70 65 6e 64 69 6e 67 20 6c 6f 63 6b 20 2a  a pending lock *
e8930 2f 0a 20 20 65 6c 73 65 20 69 66 20 28 64 77 46  /.  else if (dwF
e8940 69 6c 65 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20  ileOffsetLow == 
e8950 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 26 26 20  PENDING_BYTE && 
e8960 6e 4e 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f  nNumberOfBytesTo
e8970 55 6e 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b  UnlockLow == 1){
e8980 0a 20 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e  .    if (pFile->
e8990 6c 6f 63 61 6c 2e 62 50 65 6e 64 69 6e 67 29 7b  local.bPending){
e89a0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
e89b0 63 61 6c 2e 62 50 65 6e 64 69 6e 67 20 3d 20 46  cal.bPending = F
e89c0 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69 6c  ALSE;.      pFil
e89d0 65 2d 3e 73 68 61 72 65 64 2d 3e 62 50 65 6e 64  e->shared->bPend
e89e0 69 6e 67 20 3d 20 46 41 4c 53 45 3b 0a 20 20 20  ing = FALSE;.   
e89f0 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54 52 55     bReturn = TRU
e8a00 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  E;.    }.  }.  /
e8a10 2a 20 52 65 6c 65 61 73 69 6e 67 20 61 20 72 65  * Releasing a re
e8a20 73 65 72 76 65 64 20 6c 6f 63 6b 20 2a 2f 0a 20  served lock */. 
e8a30 20 65 6c 73 65 20 69 66 20 28 64 77 46 69 6c 65   else if (dwFile
e8a40 4f 66 66 73 65 74 4c 6f 77 20 3d 3d 20 52 45 53  OffsetLow == RES
e8a50 45 52 56 45 44 5f 42 59 54 45 20 26 26 20 6e 4e  ERVED_BYTE && nN
e8a60 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 55 6e  umberOfBytesToUn
e8a70 6c 6f 63 6b 4c 6f 77 20 3d 3d 20 31 29 7b 0a 20  lockLow == 1){. 
e8a80 20 20 20 69 66 20 28 70 46 69 6c 65 2d 3e 6c 6f     if (pFile->lo
e8a90 63 61 6c 2e 62 52 65 73 65 72 76 65 64 29 20 7b  cal.bReserved) {
e8aa0 0a 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 6c 6f  .      pFile->lo
e8ab0 63 61 6c 2e 62 52 65 73 65 72 76 65 64 20 3d 20  cal.bReserved = 
e8ac0 46 41 4c 53 45 3b 0a 20 20 20 20 20 20 70 46 69  FALSE;.      pFi
e8ad0 6c 65 2d 3e 73 68 61 72 65 64 2d 3e 62 52 65 73  le->shared->bRes
e8ae0 65 72 76 65 64 20 3d 20 46 41 4c 53 45 3b 0a 20  erved = FALSE;. 
e8af0 20 20 20 20 20 62 52 65 74 75 72 6e 20 3d 20 54       bReturn = T
e8b00 52 55 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  RUE;.    }.  }..
e8b10 20 20 77 69 6e 63 65 4d 75 74 65 78 52 65 6c 65    winceMutexRele
e8b20 61 73 65 28 70 46 69 6c 65 2d 3e 68 4d 75 74 65  ase(pFile->hMute
e8b30 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 62 52 65  x);.  return bRe
e8b40 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  turn;.}../*.** A
e8b50 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
e8b60 20 6f 66 20 74 68 65 20 4c 6f 63 6b 46 69 6c 65   of the LockFile
e8b70 45 78 28 29 20 41 50 49 20 6f 66 20 77 69 6e 64  Ex() API of wind
e8b80 6f 77 73 20 66 6f 72 20 77 69 6e 63 65 0a 2a 2f  ows for wince.*/
e8b90 0a 73 74 61 74 69 63 20 42 4f 4f 4c 20 77 69 6e  .static BOOL win
e8ba0 63 65 4c 6f 63 6b 46 69 6c 65 45 78 28 0a 20 20  ceLockFileEx(.  
e8bb0 48 41 4e 44 4c 45 20 2a 70 68 46 69 6c 65 2c 0a  HANDLE *phFile,.
e8bc0 20 20 44 57 4f 52 44 20 64 77 46 6c 61 67 73 2c    DWORD dwFlags,
e8bd0 0a 20 20 44 57 4f 52 44 20 64 77 52 65 73 65 72  .  DWORD dwReser
e8be0 76 65 64 2c 0a 20 20 44 57 4f 52 44 20 6e 4e 75  ved,.  DWORD nNu
e8bf0 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f 63  mberOfBytesToLoc
e8c00 6b 4c 6f 77 2c 0a 20 20 44 57 4f 52 44 20 6e 4e  kLow,.  DWORD nN
e8c10 75 6d 62 65 72 4f 66 42 79 74 65 73 54 6f 4c 6f  umberOfBytesToLo
e8c20 63 6b 48 69 67 68 2c 0a 20 20 4c 50 4f 56 45 52  ckHigh,.  LPOVER
e8c30 4c 41 50 50 45 44 20 6c 70 4f 76 65 72 6c 61 70  LAPPED lpOverlap
e8c40 70 65 64 0a 29 7b 0a 20 20 2f 2a 20 49 66 20 74  ped.){.  /* If t
e8c50 68 65 20 63 61 6c 6c 65 72 20 77 61 6e 74 73 20  he caller wants 
e8c60 61 20 73 68 61 72 65 64 20 72 65 61 64 20 6c 6f  a shared read lo
e8c70 63 6b 2c 20 66 6f 72 77 61 72 64 20 74 68 69 73  ck, forward this
e8c80 20 63 61 6c 6c 0a 20 20 2a 2a 20 74 6f 20 77 69   call.  ** to wi
e8c90 6e 63 65 4c 6f 63 6b 46 69 6c 65 20 2a 2f 0a 20  nceLockFile */. 
e8ca0 20 69 66 20 28 6c 70 4f 76 65 72 6c 61 70 70 65   if (lpOverlappe
e8cb0 64 2d 3e 4f 66 66 73 65 74 20 3d 3d 20 53 48 41  d->Offset == SHA
e8cc0 52 45 44 5f 46 49 52 53 54 20 26 26 0a 20 20 20  RED_FIRST &&.   
e8cd0 20 20 20 64 77 46 6c 61 67 73 20 3d 3d 20 31 20     dwFlags == 1 
e8ce0 26 26 0a 20 20 20 20 20 20 6e 4e 75 6d 62 65 72  &&.      nNumber
e8cf0 4f 66 42 79 74 65 73 54 6f 4c 6f 63 6b 4c 6f 77  OfBytesToLockLow
e8d00 20 3d 3d 20 53 48 41 52 45 44 5f 53 49 5a 45 29   == SHARED_SIZE)
e8d10 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 77 69 6e  {.    return win
e8d20 63 65 4c 6f 63 6b 46 69 6c 65 28 70 68 46 69 6c  ceLockFile(phFil
e8d30 65 2c 20 53 48 41 52 45 44 5f 46 49 52 53 54 2c  e, SHARED_FIRST,
e8d40 20 30 2c 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20   0, 1, 0);.  }. 
e8d50 20 72 65 74 75 72 6e 20 46 41 4c 53 45 3b 0a 7d   return FALSE;.}
e8d60 0a 2f 2a 0a 2a 2a 20 45 6e 64 20 6f 66 20 74 68  ./*.** End of th
e8d70 65 20 73 70 65 63 69 61 6c 20 63 6f 64 65 20 66  e special code f
e8d80 6f 72 20 77 69 6e 63 65 0a 2a 2a 2a 2a 2a 2a 2a  or wince.*******
e8d90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8da0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8db0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8dd0 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 20 2f  ******/.#endif /
e8de0 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  * SQLITE_OS_WINC
e8df0 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  E */../*********
e8e00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8e10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8e20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8e40 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74  ****.** The next
e8e50 20 67 72 6f 75 70 20 6f 66 20 72 6f 75 74 69 6e   group of routin
e8e60 65 73 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  es implement the
e8e70 20 49 2f 4f 20 6d 65 74 68 6f 64 73 20 73 70 65   I/O methods spe
e8e80 63 69 66 69 65 64 0a 2a 2a 20 62 79 20 74 68 65  cified.** by the
e8e90 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
e8ea0 6f 64 73 20 6f 62 6a 65 63 74 2e 0a 2a 2a 2a 2a  ods object..****
e8eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
e8ef0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
e8f00 2a 2a 20 43 6c 6f 73 65 20 61 20 66 69 6c 65 2e  ** Close a file.
e8f10 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 72 65 70  .**.** It is rep
e8f20 6f 72 74 65 64 20 74 68 61 74 20 61 6e 20 61 74  orted that an at
e8f30 74 65 6d 70 74 20 74 6f 20 63 6c 6f 73 65 20 61  tempt to close a
e8f40 20 68 61 6e 64 6c 65 20 6d 69 67 68 74 20 73 6f   handle might so
e8f50 6d 65 74 69 6d 65 73 0a 2a 2a 20 66 61 69 6c 2e  metimes.** fail.
e8f60 20 20 54 68 69 73 20 69 73 20 61 20 76 65 72 79    This is a very
e8f70 20 75 6e 72 65 61 73 6f 6e 61 62 6c 65 20 72 65   unreasonable re
e8f80 73 75 6c 74 2c 20 62 75 74 20 77 69 6e 64 6f 77  sult, but window
e8f90 73 20 69 73 20 6e 6f 74 6f 72 69 6f 75 73 0a 2a  s is notorious.*
e8fa0 2a 20 66 6f 72 20 62 65 69 6e 67 20 75 6e 72 65  * for being unre
e8fb0 61 73 6f 6e 61 62 6c 65 20 73 6f 20 49 20 64 6f  asonable so I do
e8fc0 20 6e 6f 74 20 64 6f 75 62 74 20 74 68 61 74 20   not doubt that 
e8fd0 69 74 20 6d 69 67 68 74 20 68 61 70 70 65 6e 2e  it might happen.
e8fe0 20 20 49 66 0a 2a 2a 20 74 68 65 20 63 6c 6f 73    If.** the clos
e8ff0 65 20 66 61 69 6c 73 2c 20 77 65 20 70 61 75 73  e fails, we paus
e9000 65 20 66 6f 72 20 31 30 30 20 6d 69 6c 6c 69 73  e for 100 millis
e9010 65 63 6f 6e 64 73 20 61 6e 64 20 74 72 79 20 61  econds and try a
e9020 67 61 69 6e 2e 20 20 41 73 0a 2a 2a 20 6d 61 6e  gain.  As.** man
e9030 79 20 61 73 20 4d 58 5f 43 4c 4f 53 45 5f 41 54  y as MX_CLOSE_AT
e9040 54 45 4d 50 54 20 61 74 74 65 6d 70 74 73 20 74  TEMPT attempts t
e9050 6f 20 63 6c 6f 73 65 20 74 68 65 20 68 61 6e 64  o close the hand
e9060 6c 65 20 61 72 65 20 6d 61 64 65 20 62 65 66 6f  le are made befo
e9070 72 65 0a 2a 2a 20 67 69 76 69 6e 67 20 75 70 20  re.** giving up 
e9080 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e  and returning an
e9090 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69   error..*/.#defi
e90a0 6e 65 20 4d 58 5f 43 4c 4f 53 45 5f 41 54 54 45  ne MX_CLOSE_ATTE
e90b0 4d 50 54 20 33 0a 73 74 61 74 69 63 20 69 6e 74  MPT 3.static int
e90c0 20 77 69 6e 43 6c 6f 73 65 28 73 71 6c 69 74 65   winClose(sqlite
e90d0 33 5f 66 69 6c 65 20 2a 69 64 29 7b 0a 20 20 69  3_file *id){.  i
e90e0 6e 74 20 72 63 2c 20 63 6e 74 20 3d 20 30 3b 0a  nt rc, cnt = 0;.
e90f0 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65    winFile *pFile
e9100 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b   = (winFile*)id;
e9110 0a 20 20 4f 53 54 52 41 43 45 32 28 22 43 4c 4f  .  OSTRACE2("CLO
e9120 53 45 20 25 64 5c 6e 22 2c 20 70 46 69 6c 65 2d  SE %d\n", pFile-
e9130 3e 68 29 3b 0a 20 20 64 6f 7b 0a 20 20 20 20 72  >h);.  do{.    r
e9140 63 20 3d 20 43 6c 6f 73 65 48 61 6e 64 6c 65 28  c = CloseHandle(
e9150 70 46 69 6c 65 2d 3e 68 29 3b 0a 20 20 7d 77 68  pFile->h);.  }wh
e9160 69 6c 65 28 20 72 63 3d 3d 30 20 26 26 20 2b 2b  ile( rc==0 && ++
e9170 63 6e 74 20 3c 20 4d 58 5f 43 4c 4f 53 45 5f 41  cnt < MX_CLOSE_A
e9180 54 54 45 4d 50 54 20 26 26 20 28 53 6c 65 65 70  TTEMPT && (Sleep
e9190 28 31 30 30 29 2c 20 31 29 20 29 3b 0a 23 69 66  (100), 1) );.#if
e91a0 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
e91b0 0a 23 64 65 66 69 6e 65 20 57 49 4e 43 45 5f 44  .#define WINCE_D
e91c0 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54 53  ELETION_ATTEMPTS
e91d0 20 33 0a 20 20 77 69 6e 63 65 44 65 73 74 72 6f   3.  winceDestro
e91e0 79 4c 6f 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20  yLock(pFile);.  
e91f0 69 66 28 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65  if( pFile->zDele
e9200 74 65 4f 6e 43 6c 6f 73 65 20 29 7b 0a 20 20 20  teOnClose ){.   
e9210 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
e9220 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20    while(.       
e9230 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 57 28      DeleteFileW(
e9240 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74 65 4f 6e  pFile->zDeleteOn
e9250 43 6c 6f 73 65 29 3d 3d 30 0a 20 20 20 20 20 20  Close)==0.      
e9260 20 20 26 26 20 47 65 74 46 69 6c 65 41 74 74 72    && GetFileAttr
e9270 69 62 75 74 65 73 57 28 70 46 69 6c 65 2d 3e 7a  ibutesW(pFile->z
e9280 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 21 3d  DeleteOnClose)!=
e9290 30 78 66 66 66 66 66 66 66 66 20 0a 20 20 20 20  0xffffffff .    
e92a0 20 20 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 57      && cnt++ < W
e92b0 49 4e 43 45 5f 44 45 4c 45 54 49 4f 4e 5f 41 54  INCE_DELETION_AT
e92c0 54 45 4d 50 54 53 0a 20 20 20 20 29 7b 0a 20 20  TEMPTS.    ){.  
e92d0 20 20 20 20 20 53 6c 65 65 70 28 31 30 30 29 3b       Sleep(100);
e92e0 20 20 2f 2a 20 57 61 69 74 20 61 20 6c 69 74 74    /* Wait a litt
e92f0 6c 65 20 62 65 66 6f 72 65 20 74 72 79 69 6e 67  le before trying
e9300 20 61 67 61 69 6e 20 2a 2f 0a 20 20 20 20 7d 0a   again */.    }.
e9310 20 20 20 20 66 72 65 65 28 70 46 69 6c 65 2d 3e      free(pFile->
e9320 7a 44 65 6c 65 74 65 4f 6e 43 6c 6f 73 65 29 3b  zDeleteOnClose);
e9330 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 4f 70  .  }.#endif.  Op
e9340 65 6e 43 6f 75 6e 74 65 72 28 2d 31 29 3b 0a 20  enCounter(-1);. 
e9350 20 72 65 74 75 72 6e 20 72 63 20 3f 20 53 51 4c   return rc ? SQL
e9360 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
e9370 49 4f 45 52 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  IOERR;.}../*.** 
e9380 53 6f 6d 65 20 6d 69 63 72 6f 73 6f 66 74 20 63  Some microsoft c
e9390 6f 6d 70 69 6c 65 72 73 20 6c 61 63 6b 20 74 68  ompilers lack th
e93a0 69 73 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 2a  is definition..*
e93b0 2f 0a 23 69 66 6e 64 65 66 20 49 4e 56 41 4c 49  /.#ifndef INVALI
e93c0 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e 54  D_SET_FILE_POINT
e93d0 45 52 0a 23 20 64 65 66 69 6e 65 20 49 4e 56 41  ER.# define INVA
e93e0 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49  LID_SET_FILE_POI
e93f0 4e 54 45 52 20 28 28 44 57 4f 52 44 29 2d 31 29  NTER ((DWORD)-1)
e9400 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52  .#endif../*.** R
e9410 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 61 20  ead data from a 
e9420 66 69 6c 65 20 69 6e 74 6f 20 61 20 62 75 66 66  file into a buff
e9430 65 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49  er.  Return SQLI
e9440 54 45 5f 4f 4b 20 69 66 20 61 6c 6c 0a 2a 2a 20  TE_OK if all.** 
e9450 62 79 74 65 73 20 77 65 72 65 20 72 65 61 64 20  bytes were read 
e9460 73 75 63 63 65 73 73 66 75 6c 6c 79 20 61 6e 64  successfully and
e9470 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 20 69 66   SQLITE_IOERR if
e9480 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a   anything goes.*
e9490 2a 20 77 72 6f 6e 67 2e 0a 2a 2f 0a 73 74 61 74  * wrong..*/.stat
e94a0 69 63 20 69 6e 74 20 77 69 6e 52 65 61 64 28 0a  ic int winRead(.
e94b0 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
e94c0 69 64 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  id,          /* 
e94d0 46 69 6c 65 20 74 6f 20 72 65 61 64 20 66 72 6f  File to read fro
e94e0 6d 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75  m */.  void *pBu
e94f0 66 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f,              
e9500 20 20 2f 2a 20 57 72 69 74 65 20 63 6f 6e 74 65    /* Write conte
e9510 6e 74 20 69 6e 74 6f 20 74 68 69 73 20 62 75 66  nt into this buf
e9520 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74  fer */.  int amt
e9530 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e9540 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
e9550 20 62 79 74 65 73 20 74 6f 20 72 65 61 64 20 2a   bytes to read *
e9560 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
e9570 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 20 2f  4 offset       /
e9580 2a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20  * Begin reading 
e9590 61 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a  at this offset *
e95a0 2f 0a 29 7b 0a 20 20 4c 4f 4e 47 20 75 70 70 65  /.){.  LONG uppe
e95b0 72 42 69 74 73 20 3d 20 28 6f 66 66 73 65 74 3e  rBits = (offset>
e95c0 3e 33 32 29 20 26 20 30 78 37 66 66 66 66 66 66  >32) & 0x7ffffff
e95d0 66 3b 0a 20 20 4c 4f 4e 47 20 6c 6f 77 65 72 42  f;.  LONG lowerB
e95e0 69 74 73 20 3d 20 6f 66 66 73 65 74 20 26 20 30  its = offset & 0
e95f0 78 66 66 66 66 66 66 66 66 3b 0a 20 20 44 57 4f  xffffffff;.  DWO
e9600 52 44 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 67  RD rc;.  DWORD g
e9610 6f 74 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70  ot;.  winFile *p
e9620 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a  File = (winFile*
e9630 29 69 64 3b 0a 20 20 61 73 73 65 72 74 28 20 69  )id;.  assert( i
e9640 64 21 3d 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61  d!=0 );.  Simula
e9650 74 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e  teIOError(return
e9660 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45   SQLITE_IOERR_RE
e9670 41 44 29 3b 0a 20 20 4f 53 54 52 41 43 45 33 28  AD);.  OSTRACE3(
e9680 22 52 45 41 44 20 25 64 20 6c 6f 63 6b 3d 25 64  "READ %d lock=%d
e9690 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 70  \n", pFile->h, p
e96a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b  File->locktype);
e96b0 0a 20 20 72 63 20 3d 20 53 65 74 46 69 6c 65 50  .  rc = SetFileP
e96c0 6f 69 6e 74 65 72 28 70 46 69 6c 65 2d 3e 68 2c  ointer(pFile->h,
e96d0 20 6c 6f 77 65 72 42 69 74 73 2c 20 26 75 70 70   lowerBits, &upp
e96e0 65 72 42 69 74 73 2c 20 46 49 4c 45 5f 42 45 47  erBits, FILE_BEG
e96f0 49 4e 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 49  IN);.  if( rc==I
e9700 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49 4c 45 5f  NVALID_SET_FILE_
e9710 50 4f 49 4e 54 45 52 20 26 26 20 47 65 74 4c 61  POINTER && GetLa
e9720 73 74 45 72 72 6f 72 28 29 21 3d 4e 4f 5f 45 52  stError()!=NO_ER
e9730 52 4f 52 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ROR ){.    retur
e9740 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20  n SQLITE_FULL;. 
e9750 20 7d 0a 20 20 69 66 28 20 21 52 65 61 64 46 69   }.  if( !ReadFi
e9760 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 70 42 75  le(pFile->h, pBu
e9770 66 2c 20 61 6d 74 2c 20 26 67 6f 74 2c 20 30 29  f, amt, &got, 0)
e9780 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
e9790 51 4c 49 54 45 5f 49 4f 45 52 52 5f 52 45 41 44  QLITE_IOERR_READ
e97a0 3b 0a 20 20 7d 0a 20 20 69 66 28 20 67 6f 74 3d  ;.  }.  if( got=
e97b0 3d 28 44 57 4f 52 44 29 61 6d 74 20 29 7b 0a 20  =(DWORD)amt ){. 
e97c0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
e97d0 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  _OK;.  }else{.  
e97e0 20 20 2f 2a 20 55 6e 72 65 61 64 20 70 61 72 74    /* Unread part
e97f0 73 20 6f 66 20 74 68 65 20 62 75 66 66 65 72 20  s of the buffer 
e9800 6d 75 73 74 20 62 65 20 7a 65 72 6f 2d 66 69 6c  must be zero-fil
e9810 6c 65 64 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65  led */.    memse
e9820 74 28 26 28 28 63 68 61 72 2a 29 70 42 75 66 29  t(&((char*)pBuf)
e9830 5b 67 6f 74 5d 2c 20 30 2c 20 61 6d 74 2d 67 6f  [got], 0, amt-go
e9840 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  t);.    return S
e9850 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f 52  QLITE_IOERR_SHOR
e9860 54 5f 52 45 41 44 3b 0a 20 20 7d 0a 7d 0a 0a 2f  T_READ;.  }.}../
e9870 2a 0a 2a 2a 20 57 72 69 74 65 20 64 61 74 61 20  *.** Write data 
e9880 66 72 6f 6d 20 61 20 62 75 66 66 65 72 20 69 6e  from a buffer in
e9890 74 6f 20 61 20 66 69 6c 65 2e 20 20 52 65 74 75  to a file.  Retu
e98a0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
e98b0 73 75 63 63 65 73 73 0a 2a 2a 20 6f 72 20 73 6f  success.** or so
e98c0 6d 65 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63  me other error c
e98d0 6f 64 65 20 6f 6e 20 66 61 69 6c 75 72 65 2e 0a  ode on failure..
e98e0 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 69  */.static int wi
e98f0 6e 57 72 69 74 65 28 0a 20 20 73 71 6c 69 74 65  nWrite(.  sqlite
e9900 33 5f 66 69 6c 65 20 2a 69 64 2c 20 20 20 20 20  3_file *id,     
e9910 20 20 20 20 2f 2a 20 46 69 6c 65 20 74 6f 20 77      /* File to w
e9920 72 69 74 65 20 69 6e 74 6f 20 2a 2f 0a 20 20 63  rite into */.  c
e9930 6f 6e 73 74 20 76 6f 69 64 20 2a 70 42 75 66 2c  onst void *pBuf,
e9940 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
e9950 62 79 74 65 73 20 74 6f 20 62 65 20 77 72 69 74  bytes to be writ
e9960 74 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 61 6d 74  ten */.  int amt
e9970 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
e9980 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
e9990 62 79 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a  bytes to write *
e99a0 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36  /.  sqlite3_int6
e99b0 34 20 6f 66 66 73 65 74 20 20 20 20 20 20 2f 2a  4 offset      /*
e99c0 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 74 68 65   Offset into the
e99d0 20 66 69 6c 65 20 74 6f 20 62 65 67 69 6e 20 77   file to begin w
e99e0 72 69 74 69 6e 67 20 61 74 20 2a 2f 0a 29 7b 0a  riting at */.){.
e99f0 20 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73    LONG upperBits
e9a00 20 3d 20 28 6f 66 66 73 65 74 3e 3e 33 32 29 20   = (offset>>32) 
e9a10 26 20 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20  & 0x7fffffff;.  
e9a20 4c 4f 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d  LONG lowerBits =
e9a30 20 6f 66 66 73 65 74 20 26 20 30 78 66 66 66 66   offset & 0xffff
e9a40 66 66 66 66 3b 0a 20 20 44 57 4f 52 44 20 72 63  ffff;.  DWORD rc
e9a50 3b 0a 20 20 44 57 4f 52 44 20 77 72 6f 74 65 3b  ;.  DWORD wrote;
e9a60 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c  .  winFile *pFil
e9a70 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64  e = (winFile*)id
e9a80 3b 0a 20 20 61 73 73 65 72 74 28 20 69 64 21 3d  ;.  assert( id!=
e9a90 30 20 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 49  0 );.  SimulateI
e9aa0 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20 53 51  OError(return SQ
e9ab0 4c 49 54 45 5f 49 4f 45 52 52 5f 57 52 49 54 45  LITE_IOERR_WRITE
e9ac0 29 3b 0a 20 20 53 69 6d 75 6c 61 74 65 44 69 73  );.  SimulateDis
e9ad0 6b 66 75 6c 6c 45 72 72 6f 72 28 72 65 74 75 72  kfullError(retur
e9ae0 6e 20 53 51 4c 49 54 45 5f 46 55 4c 4c 29 3b 0a  n SQLITE_FULL);.
e9af0 20 20 4f 53 54 52 41 43 45 33 28 22 57 52 49 54    OSTRACE3("WRIT
e9b00 45 20 25 64 20 6c 6f 63 6b 3d 25 64 5c 6e 22 2c  E %d lock=%d\n",
e9b10 20 70 46 69 6c 65 2d 3e 68 2c 20 70 46 69 6c 65   pFile->h, pFile
e9b20 2d 3e 6c 6f 63 6b 74 79 70 65 29 3b 0a 20 20 72  ->locktype);.  r
e9b30 63 20 3d 20 53 65 74 46 69 6c 65 50 6f 69 6e 74  c = SetFilePoint
e9b40 65 72 28 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f 77  er(pFile->h, low
e9b50 65 72 42 69 74 73 2c 20 26 75 70 70 65 72 42 69  erBits, &upperBi
e9b60 74 73 2c 20 46 49 4c 45 5f 42 45 47 49 4e 29 3b  ts, FILE_BEGIN);
e9b70 0a 20 20 69 66 28 20 72 63 3d 3d 49 4e 56 41 4c  .  if( rc==INVAL
e9b80 49 44 5f 53 45 54 5f 46 49 4c 45 5f 50 4f 49 4e  ID_SET_FILE_POIN
e9b90 54 45 52 20 26 26 20 47 65 74 4c 61 73 74 45 72  TER && GetLastEr
e9ba0 72 6f 72 28 29 21 3d 4e 4f 5f 45 52 52 4f 52 20  ror()!=NO_ERROR 
e9bb0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
e9bc0 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 7d 0a 20  LITE_FULL;.  }. 
e9bd0 20 61 73 73 65 72 74 28 20 61 6d 74 3e 30 20 29   assert( amt>0 )
e9be0 3b 0a 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20  ;.  while(.     
e9bf0 61 6d 74 3e 30 0a 20 20 20 20 20 26 26 20 28 72  amt>0.     && (r
e9c00 63 20 3d 20 57 72 69 74 65 46 69 6c 65 28 70 46  c = WriteFile(pF
e9c10 69 6c 65 2d 3e 68 2c 20 70 42 75 66 2c 20 61 6d  ile->h, pBuf, am
e9c20 74 2c 20 26 77 72 6f 74 65 2c 20 30 29 29 21 3d  t, &wrote, 0))!=
e9c30 30 0a 20 20 20 20 20 26 26 20 77 72 6f 74 65 3e  0.     && wrote>
e9c40 30 0a 20 20 29 7b 0a 20 20 20 20 61 6d 74 20 2d  0.  ){.    amt -
e9c50 3d 20 77 72 6f 74 65 3b 0a 20 20 20 20 70 42 75  = wrote;.    pBu
e9c60 66 20 3d 20 26 28 28 63 68 61 72 2a 29 70 42 75  f = &((char*)pBu
e9c70 66 29 5b 77 72 6f 74 65 5d 3b 0a 20 20 7d 0a 20  f)[wrote];.  }. 
e9c80 20 69 66 28 20 21 72 63 20 7c 7c 20 61 6d 74 3e   if( !rc || amt>
e9c90 28 69 6e 74 29 77 72 6f 74 65 20 29 7b 0a 20 20  (int)wrote ){.  
e9ca0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e9cb0 46 55 4c 4c 3b 0a 20 20 7d 0a 20 20 72 65 74 75  FULL;.  }.  retu
e9cc0 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
e9cd0 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
e9ce0 61 6e 20 6f 70 65 6e 20 66 69 6c 65 20 74 6f 20  an open file to 
e9cf0 61 20 73 70 65 63 69 66 69 65 64 20 73 69 7a 65  a specified size
e9d00 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
e9d10 69 6e 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74  inTruncate(sqlit
e9d20 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 73 71 6c  e3_file *id, sql
e9d30 69 74 65 33 5f 69 6e 74 36 34 20 6e 42 79 74 65  ite3_int64 nByte
e9d40 29 7b 0a 20 20 44 57 4f 52 44 20 72 63 3b 0a 20  ){.  DWORD rc;. 
e9d50 20 4c 4f 4e 47 20 75 70 70 65 72 42 69 74 73 20   LONG upperBits 
e9d60 3d 20 28 6e 42 79 74 65 3e 3e 33 32 29 20 26 20  = (nByte>>32) & 
e9d70 30 78 37 66 66 66 66 66 66 66 3b 0a 20 20 4c 4f  0x7fffffff;.  LO
e9d80 4e 47 20 6c 6f 77 65 72 42 69 74 73 20 3d 20 6e  NG lowerBits = n
e9d90 42 79 74 65 20 26 20 30 78 66 66 66 66 66 66 66  Byte & 0xfffffff
e9da0 66 3b 0a 20 20 77 69 6e 46 69 6c 65 20 2a 70 46  f;.  winFile *pF
e9db0 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65 2a 29  ile = (winFile*)
e9dc0 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33 28 22  id;.  OSTRACE3("
e9dd0 54 52 55 4e 43 41 54 45 20 25 64 20 25 6c 6c 64  TRUNCATE %d %lld
e9de0 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6e  \n", pFile->h, n
e9df0 42 79 74 65 29 3b 0a 20 20 53 69 6d 75 6c 61 74  Byte);.  Simulat
e9e00 65 49 4f 45 72 72 6f 72 28 72 65 74 75 72 6e 20  eIOError(return 
e9e10 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 54 52 55  SQLITE_IOERR_TRU
e9e20 4e 43 41 54 45 29 3b 0a 20 20 72 63 20 3d 20 53  NCATE);.  rc = S
e9e30 65 74 46 69 6c 65 50 6f 69 6e 74 65 72 28 70 46  etFilePointer(pF
e9e40 69 6c 65 2d 3e 68 2c 20 6c 6f 77 65 72 42 69 74  ile->h, lowerBit
e9e50 73 2c 20 26 75 70 70 65 72 42 69 74 73 2c 20 46  s, &upperBits, F
e9e60 49 4c 45 5f 42 45 47 49 4e 29 3b 0a 20 20 69 66  ILE_BEGIN);.  if
e9e70 28 20 49 4e 56 41 4c 49 44 5f 53 45 54 5f 46 49  ( INVALID_SET_FI
e9e80 4c 45 5f 50 4f 49 4e 54 45 52 20 21 3d 20 72 63  LE_POINTER != rc
e9e90 20 29 7b 0a 20 20 20 20 2f 2a 20 53 65 74 45 6e   ){.    /* SetEn
e9ea0 64 4f 66 46 69 6c 65 20 77 69 6c 6c 20 66 61 69  dOfFile will fai
e9eb0 6c 20 69 66 20 6e 42 79 74 65 20 69 73 20 6e 65  l if nByte is ne
e9ec0 67 61 74 69 76 65 20 2a 2f 0a 20 20 20 20 69 66  gative */.    if
e9ed0 28 20 53 65 74 45 6e 64 4f 66 46 69 6c 65 28 70  ( SetEndOfFile(p
e9ee0 46 69 6c 65 2d 3e 68 29 20 29 7b 0a 20 20 20 20  File->h) ){.    
e9ef0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
e9f00 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
e9f10 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
e9f20 45 52 52 5f 54 52 55 4e 43 41 54 45 3b 0a 7d 0a  ERR_TRUNCATE;.}.
e9f30 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
e9f40 45 53 54 0a 2f 2a 0a 2a 2a 20 43 6f 75 6e 74 20  EST./*.** Count 
e9f50 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66 75  the number of fu
e9f60 6c 6c 73 79 6e 63 73 20 61 6e 64 20 6e 6f 72 6d  llsyncs and norm
e9f70 61 6c 20 73 79 6e 63 73 2e 20 20 54 68 69 73 20  al syncs.  This 
e9f80 69 73 20 75 73 65 64 20 74 6f 20 74 65 73 74 0a  is used to test.
e9f90 2a 2a 20 74 68 61 74 20 73 79 6e 63 73 20 61 6e  ** that syncs an
e9fa0 64 20 66 75 6c 6c 73 79 6e 63 73 20 61 72 65 20  d fullsyncs are 
e9fb0 6f 63 63 75 72 69 6e 67 20 61 74 20 74 68 65 20  occuring at the 
e9fc0 72 69 67 68 74 20 74 69 6d 65 73 2e 0a 2a 2f 0a  right times..*/.
e9fd0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
e9fe0 71 6c 69 74 65 33 5f 73 79 6e 63 5f 63 6f 75 6e  qlite3_sync_coun
e9ff0 74 20 3d 20 30 3b 0a 53 51 4c 49 54 45 5f 41 50  t = 0;.SQLITE_AP
ea000 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66 75  I int sqlite3_fu
ea010 6c 6c 73 79 6e 63 5f 63 6f 75 6e 74 20 3d 20 30  llsync_count = 0
ea020 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
ea030 4d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 77 72  Make sure all wr
ea040 69 74 65 73 20 74 6f 20 61 20 70 61 72 74 69 63  ites to a partic
ea050 75 6c 61 72 20 66 69 6c 65 20 61 72 65 20 63 6f  ular file are co
ea060 6d 6d 69 74 74 65 64 20 74 6f 20 64 69 73 6b 2e  mmitted to disk.
ea070 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
ea080 69 6e 53 79 6e 63 28 73 71 6c 69 74 65 33 5f 66  inSync(sqlite3_f
ea090 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 66 6c 61  ile *id, int fla
ea0a0 67 73 29 7b 0a 20 20 77 69 6e 46 69 6c 65 20 2a  gs){.  winFile *
ea0b0 70 46 69 6c 65 20 3d 20 28 77 69 6e 46 69 6c 65  pFile = (winFile
ea0c0 2a 29 69 64 3b 0a 20 20 4f 53 54 52 41 43 45 33  *)id;.  OSTRACE3
ea0d0 28 22 53 59 4e 43 20 25 64 20 6c 6f 63 6b 3d 25  ("SYNC %d lock=%
ea0e0 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20  d\n", pFile->h, 
ea0f0 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 29  pFile->locktype)
ea100 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
ea110 54 45 53 54 0a 20 20 69 66 28 20 66 6c 61 67 73  TEST.  if( flags
ea120 20 26 20 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46   & SQLITE_SYNC_F
ea130 55 4c 4c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ULL ){.    sqlit
ea140 65 33 5f 66 75 6c 6c 73 79 6e 63 5f 63 6f 75 6e  e3_fullsync_coun
ea150 74 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  t++;.  }.  sqlit
ea160 65 33 5f 73 79 6e 63 5f 63 6f 75 6e 74 2b 2b 3b  e3_sync_count++;
ea170 0a 23 65 6e 64 69 66 0a 20 20 2f 2a 20 49 66 20  .#endif.  /* If 
ea180 77 65 20 63 6f 6d 70 69 6c 65 64 20 77 69 74 68  we compiled with
ea190 20 74 68 65 20 53 51 4c 49 54 45 5f 4e 4f 5f 53   the SQLITE_NO_S
ea1a0 59 4e 43 20 66 6c 61 67 2c 20 74 68 65 6e 20 73  YNC flag, then s
ea1b0 79 6e 63 69 6e 67 20 69 73 20 61 0a 20 20 2a 2a  yncing is a.  **
ea1c0 20 6e 6f 2d 6f 70 0a 20 20 2a 2f 0a 23 69 66 64   no-op.  */.#ifd
ea1d0 65 66 20 53 51 4c 49 54 45 5f 4e 4f 5f 53 59 4e  ef SQLITE_NO_SYN
ea1e0 43 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  C.    return SQL
ea1f0 49 54 45 5f 4f 4b 3b 0a 23 65 6c 73 65 0a 20 20  ITE_OK;.#else.  
ea200 69 66 28 20 46 6c 75 73 68 46 69 6c 65 42 75 66  if( FlushFileBuf
ea210 66 65 72 73 28 70 46 69 6c 65 2d 3e 68 29 20 29  fers(pFile->h) )
ea220 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
ea230 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
ea240 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
ea250 54 45 5f 49 4f 45 52 52 3b 0a 20 20 7d 0a 23 65  TE_IOERR;.  }.#e
ea260 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  ndif.}../*.** De
ea270 74 65 72 6d 69 6e 65 20 74 68 65 20 63 75 72 72  termine the curr
ea280 65 6e 74 20 73 69 7a 65 20 6f 66 20 61 20 66 69  ent size of a fi
ea290 6c 65 20 69 6e 20 62 79 74 65 73 0a 2a 2f 0a 73  le in bytes.*/.s
ea2a0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 46 69 6c  tatic int winFil
ea2b0 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
ea2c0 6c 65 20 2a 69 64 2c 20 73 71 6c 69 74 65 33 5f  le *id, sqlite3_
ea2d0 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20  int64 *pSize){. 
ea2e0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
ea2f0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
ea300 20 20 44 57 4f 52 44 20 75 70 70 65 72 42 69 74    DWORD upperBit
ea310 73 2c 20 6c 6f 77 65 72 42 69 74 73 3b 0a 20 20  s, lowerBits;.  
ea320 53 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28  SimulateIOError(
ea330 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f  return SQLITE_IO
ea340 45 52 52 5f 46 53 54 41 54 29 3b 0a 20 20 6c 6f  ERR_FSTAT);.  lo
ea350 77 65 72 42 69 74 73 20 3d 20 47 65 74 46 69 6c  werBits = GetFil
ea360 65 53 69 7a 65 28 70 46 69 6c 65 2d 3e 68 2c 20  eSize(pFile->h, 
ea370 26 75 70 70 65 72 42 69 74 73 29 3b 0a 20 20 2a  &upperBits);.  *
ea380 70 53 69 7a 65 20 3d 20 28 28 28 73 71 6c 69 74  pSize = (((sqlit
ea390 65 33 5f 69 6e 74 36 34 29 75 70 70 65 72 42 69  e3_int64)upperBi
ea3a0 74 73 29 3c 3c 33 32 29 20 2b 20 6c 6f 77 65 72  ts)<<32) + lower
ea3b0 42 69 74 73 3b 0a 20 20 72 65 74 75 72 6e 20 53  Bits;.  return S
ea3c0 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
ea3d0 2a 2a 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c  ** LOCKFILE_FAIL
ea3e0 5f 49 4d 4d 45 44 49 41 54 45 4c 59 20 69 73 20  _IMMEDIATELY is 
ea3f0 75 6e 64 65 66 69 6e 65 64 20 6f 6e 20 73 6f 6d  undefined on som
ea400 65 20 57 69 6e 64 6f 77 73 20 73 79 73 74 65 6d  e Windows system
ea410 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4c 4f  s..*/.#ifndef LO
ea420 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49 4d 4d 45  CKFILE_FAIL_IMME
ea430 44 49 41 54 45 4c 59 0a 23 20 64 65 66 69 6e 65  DIATELY.# define
ea440 20 4c 4f 43 4b 46 49 4c 45 5f 46 41 49 4c 5f 49   LOCKFILE_FAIL_I
ea450 4d 4d 45 44 49 41 54 45 4c 59 20 31 0a 23 65 6e  MMEDIATELY 1.#en
ea460 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69  dif../*.** Acqui
ea470 72 65 20 61 20 72 65 61 64 65 72 20 6c 6f 63 6b  re a reader lock
ea480 2e 0a 2a 2a 20 44 69 66 66 65 72 65 6e 74 20 41  ..** Different A
ea490 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  PI routines are 
ea4a0 63 61 6c 6c 65 64 20 64 65 70 65 6e 64 69 6e 67  called depending
ea4b0 20 6f 6e 20 77 68 65 74 68 65 72 20 6f 72 20 6e   on whether or n
ea4c0 6f 74 20 74 68 69 73 0a 2a 2a 20 69 73 20 57 69  ot this.** is Wi
ea4d0 6e 39 35 20 6f 72 20 57 69 6e 4e 54 2e 0a 2a 2f  n95 or WinNT..*/
ea4e0 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 52  .static int getR
ea4f0 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69 6c 65 20  eadLock(winFile 
ea500 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e 74 20 72  *pFile){.  int r
ea510 65 73 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  es;.  if( isNT()
ea520 20 29 7b 0a 20 20 20 20 4f 56 45 52 4c 41 50 50   ){.    OVERLAPP
ea530 45 44 20 6f 76 6c 70 3b 0a 20 20 20 20 6f 76 6c  ED ovlp;.    ovl
ea540 70 2e 4f 66 66 73 65 74 20 3d 20 53 48 41 52 45  p.Offset = SHARE
ea550 44 5f 46 49 52 53 54 3b 0a 20 20 20 20 6f 76 6c  D_FIRST;.    ovl
ea560 70 2e 4f 66 66 73 65 74 48 69 67 68 20 3d 20 30  p.OffsetHigh = 0
ea570 3b 0a 20 20 20 20 6f 76 6c 70 2e 68 45 76 65 6e  ;.    ovlp.hEven
ea580 74 20 3d 20 30 3b 0a 20 20 20 20 72 65 73 20 3d  t = 0;.    res =
ea590 20 4c 6f 63 6b 46 69 6c 65 45 78 28 70 46 69 6c   LockFileEx(pFil
ea5a0 65 2d 3e 68 2c 20 4c 4f 43 4b 46 49 4c 45 5f 46  e->h, LOCKFILE_F
ea5b0 41 49 4c 5f 49 4d 4d 45 44 49 41 54 45 4c 59 2c  AIL_IMMEDIATELY,
ea5c0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ea5d0 20 20 20 20 20 20 30 2c 20 53 48 41 52 45 44 5f        0, SHARED_
ea5e0 53 49 5a 45 2c 20 30 2c 20 26 6f 76 6c 70 29 3b  SIZE, 0, &ovlp);
ea5f0 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20  ./* isNT() is 1 
ea600 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ea610 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65  CE==1, so this e
ea620 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65  lse is never exe
ea630 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66 20 53  cuted. .*/.#if S
ea640 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d  QLITE_OS_WINCE==
ea650 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69  0.  }else{.    i
ea660 6e 74 20 6c 6b 3b 0a 20 20 20 20 73 71 6c 69 74  nt lk;.    sqlit
ea670 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73 69  e3_randomness(si
ea680 7a 65 6f 66 28 6c 6b 29 2c 20 26 6c 6b 29 3b 0a  zeof(lk), &lk);.
ea690 20 20 20 20 70 46 69 6c 65 2d 3e 73 68 61 72 65      pFile->share
ea6a0 64 4c 6f 63 6b 42 79 74 65 20 3d 20 28 6c 6b 20  dLockByte = (lk 
ea6b0 26 20 30 78 37 66 66 66 66 66 66 66 29 25 28 53  & 0x7fffffff)%(S
ea6c0 48 41 52 45 44 5f 53 49 5a 45 20 2d 20 31 29 3b  HARED_SIZE - 1);
ea6d0 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46  .    res = LockF
ea6e0 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48  ile(pFile->h, SH
ea6f0 41 52 45 44 5f 46 49 52 53 54 2b 70 46 69 6c 65  ARED_FIRST+pFile
ea700 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74 65  ->sharedLockByte
ea710 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 23 65 6e 64  , 0, 1, 0);.#end
ea720 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  if.  }.  return 
ea730 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  res;.}../*.** Un
ea740 64 6f 20 61 20 72 65 61 64 6c 6f 63 6b 0a 2a 2f  do a readlock.*/
ea750 0a 73 74 61 74 69 63 20 69 6e 74 20 75 6e 6c 6f  .static int unlo
ea760 63 6b 52 65 61 64 4c 6f 63 6b 28 77 69 6e 46 69  ckReadLock(winFi
ea770 6c 65 20 2a 70 46 69 6c 65 29 7b 0a 20 20 69 6e  le *pFile){.  in
ea780 74 20 72 65 73 3b 0a 20 20 69 66 28 20 69 73 4e  t res;.  if( isN
ea790 54 28 29 20 29 7b 0a 20 20 20 20 72 65 73 20 3d  T() ){.    res =
ea7a0 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   UnlockFile(pFil
ea7b0 65 2d 3e 68 2c 20 53 48 41 52 45 44 5f 46 49 52  e->h, SHARED_FIR
ea7c0 53 54 2c 20 30 2c 20 53 48 41 52 45 44 5f 53 49  ST, 0, SHARED_SI
ea7d0 5a 45 2c 20 30 29 3b 0a 2f 2a 20 69 73 4e 54 28  ZE, 0);./* isNT(
ea7e0 29 20 69 73 20 31 20 69 66 20 53 51 4c 49 54 45  ) is 1 if SQLITE
ea7f0 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f  _OS_WINCE==1, so
ea800 20 74 68 69 73 20 65 6c 73 65 20 69 73 20 6e 65   this else is ne
ea810 76 65 72 20 65 78 65 63 75 74 65 64 2e 20 0a 2a  ver executed. .*
ea820 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f  /.#if SQLITE_OS_
ea830 57 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65  WINCE==0.  }else
ea840 7b 0a 20 20 20 20 72 65 73 20 3d 20 55 6e 6c 6f  {.    res = Unlo
ea850 63 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c  ckFile(pFile->h,
ea860 20 53 48 41 52 45 44 5f 46 49 52 53 54 20 2b 20   SHARED_FIRST + 
ea870 70 46 69 6c 65 2d 3e 73 68 61 72 65 64 4c 6f 63  pFile->sharedLoc
ea880 6b 42 79 74 65 2c 20 30 2c 20 31 2c 20 30 29 3b  kByte, 0, 1, 0);
ea890 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
ea8a0 74 75 72 6e 20 72 65 73 3b 0a 7d 0a 0a 2f 2a 0a  turn res;.}../*.
ea8b0 2a 2a 20 4c 6f 63 6b 20 74 68 65 20 66 69 6c 65  ** Lock the file
ea8c0 20 77 69 74 68 20 74 68 65 20 6c 6f 63 6b 20 73   with the lock s
ea8d0 70 65 63 69 66 69 65 64 20 62 79 20 70 61 72 61  pecified by para
ea8e0 6d 65 74 65 72 20 6c 6f 63 6b 74 79 70 65 20 2d  meter locktype -
ea8f0 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 20 66   one.** of the f
ea900 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
ea910 20 20 20 20 28 31 29 20 53 48 41 52 45 44 5f 4c      (1) SHARED_L
ea920 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 32 29 20 52  OCK.**     (2) R
ea930 45 53 45 52 56 45 44 5f 4c 4f 43 4b 0a 2a 2a 20  ESERVED_LOCK.** 
ea940 20 20 20 20 28 33 29 20 50 45 4e 44 49 4e 47 5f      (3) PENDING_
ea950 4c 4f 43 4b 0a 2a 2a 20 20 20 20 20 28 34 29 20  LOCK.**     (4) 
ea960 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 0a 2a  EXCLUSIVE_LOCK.*
ea970 2a 0a 2a 2a 20 53 6f 6d 65 74 69 6d 65 73 20 77  *.** Sometimes w
ea980 68 65 6e 20 72 65 71 75 65 73 74 69 6e 67 20 6f  hen requesting o
ea990 6e 65 20 6c 6f 63 6b 20 73 74 61 74 65 2c 20 61  ne lock state, a
ea9a0 64 64 69 74 69 6f 6e 61 6c 20 6c 6f 63 6b 20 73  dditional lock s
ea9b0 74 61 74 65 73 0a 2a 2a 20 61 72 65 20 69 6e 73  tates.** are ins
ea9c0 65 72 74 65 64 20 69 6e 20 62 65 74 77 65 65 6e  erted in between
ea9d0 2e 20 20 54 68 65 20 6c 6f 63 6b 69 6e 67 20 6d  .  The locking m
ea9e0 69 67 68 74 20 66 61 69 6c 20 6f 6e 20 6f 6e 65  ight fail on one
ea9f0 20 6f 66 20 74 68 65 20 6c 61 74 65 72 0a 2a 2a   of the later.**
eaa00 20 74 72 61 6e 73 69 74 69 6f 6e 73 20 6c 65 61   transitions lea
eaa10 76 69 6e 67 20 74 68 65 20 6c 6f 63 6b 20 73 74  ving the lock st
eaa20 61 74 65 20 64 69 66 66 65 72 65 6e 74 20 66 72  ate different fr
eaa30 6f 6d 20 77 68 61 74 20 69 74 20 73 74 61 72 74  om what it start
eaa40 65 64 20 62 75 74 0a 2a 2a 20 73 74 69 6c 6c 20  ed but.** still 
eaa50 73 68 6f 72 74 20 6f 66 20 69 74 73 20 67 6f 61  short of its goa
eaa60 6c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  l.  The followin
eaa70 67 20 63 68 61 72 74 20 73 68 6f 77 73 20 74 68  g chart shows th
eaa80 65 20 61 6c 6c 6f 77 65 64 0a 2a 2a 20 74 72 61  e allowed.** tra
eaa90 6e 73 69 74 69 6f 6e 73 20 61 6e 64 20 74 68 65  nsitions and the
eaaa0 20 69 6e 73 65 72 74 65 64 20 69 6e 74 65 72 6d   inserted interm
eaab0 65 64 69 61 74 65 20 73 74 61 74 65 73 3a 0a 2a  ediate states:.*
eaac0 2a 0a 2a 2a 20 20 20 20 55 4e 4c 4f 43 4b 45 44  *.**    UNLOCKED
eaad0 20 2d 3e 20 53 48 41 52 45 44 0a 2a 2a 20 20 20   -> SHARED.**   
eaae0 20 53 48 41 52 45 44 20 2d 3e 20 52 45 53 45 52   SHARED -> RESER
eaaf0 56 45 44 0a 2a 2a 20 20 20 20 53 48 41 52 45 44  VED.**    SHARED
eab00 20 2d 3e 20 28 50 45 4e 44 49 4e 47 29 20 2d 3e   -> (PENDING) ->
eab10 20 45 58 43 4c 55 53 49 56 45 0a 2a 2a 20 20 20   EXCLUSIVE.**   
eab20 20 52 45 53 45 52 56 45 44 20 2d 3e 20 28 50 45   RESERVED -> (PE
eab30 4e 44 49 4e 47 29 20 2d 3e 20 45 58 43 4c 55 53  NDING) -> EXCLUS
eab40 49 56 45 0a 2a 2a 20 20 20 20 50 45 4e 44 49 4e  IVE.**    PENDIN
eab50 47 20 2d 3e 20 45 58 43 4c 55 53 49 56 45 0a 2a  G -> EXCLUSIVE.*
eab60 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
eab70 65 20 77 69 6c 6c 20 6f 6e 6c 79 20 69 6e 63 72  e will only incr
eab80 65 61 73 65 20 61 20 6c 6f 63 6b 2e 20 20 54 68  ease a lock.  Th
eab90 65 20 77 69 6e 55 6e 6c 6f 63 6b 28 29 20 72 6f  e winUnlock() ro
eaba0 75 74 69 6e 65 0a 2a 2a 20 65 72 61 73 65 73 20  utine.** erases 
eabb0 61 6c 6c 20 6c 6f 63 6b 73 20 61 74 20 6f 6e 63  all locks at onc
eabc0 65 20 61 6e 64 20 72 65 74 75 72 6e 73 20 75 73  e and returns us
eabd0 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74 6f 20   immediately to 
eabe0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 30 2e  locking level 0.
eabf0 0a 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20 70 6f  .** It is not po
eac00 73 73 69 62 6c 65 20 74 6f 20 6c 6f 77 65 72 20  ssible to lower 
eac10 74 68 65 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65  the locking leve
eac20 6c 20 6f 6e 65 20 73 74 65 70 20 61 74 20 61 20  l one step at a 
eac30 74 69 6d 65 2e 20 20 59 6f 75 0a 2a 2a 20 6d 75  time.  You.** mu
eac40 73 74 20 67 6f 20 73 74 72 61 69 67 68 74 20 74  st go straight t
eac50 6f 20 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20  o locking level 
eac60 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  0..*/.static int
eac70 20 77 69 6e 4c 6f 63 6b 28 73 71 6c 69 74 65 33   winLock(sqlite3
eac80 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e 74 20 6c  _file *id, int l
eac90 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69 6e 74 20  ocktype){.  int 
eaca0 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
eacb0 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64     /* Return cod
eacc0 65 20 66 72 6f 6d 20 73 75 62 72 6f 75 74 69 6e  e from subroutin
eacd0 65 73 20 2a 2f 0a 20 20 69 6e 74 20 72 65 73 20  es */.  int res 
eace0 3d 20 31 3b 20 20 20 20 20 20 20 20 20 20 20 2f  = 1;           /
eacf0 2a 20 52 65 73 75 6c 74 20 6f 66 20 61 20 77 69  * Result of a wi
ead00 6e 64 6f 77 73 20 6c 6f 63 6b 20 63 61 6c 6c 20  ndows lock call 
ead10 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 4c 6f 63 6b  */.  int newLock
ead20 74 79 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 53  type;       /* S
ead30 65 74 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  et pFile->lockty
ead40 70 65 20 74 6f 20 74 68 69 73 20 76 61 6c 75 65  pe to this value
ead50 20 62 65 66 6f 72 65 20 65 78 69 74 69 6e 67 20   before exiting 
ead60 2a 2f 0a 20 20 69 6e 74 20 67 6f 74 50 65 6e 64  */.  int gotPend
ead70 69 6e 67 4c 6f 63 6b 20 3d 20 30 3b 2f 2a 20 54  ingLock = 0;/* T
ead80 72 75 65 20 69 66 20 77 65 20 61 63 71 75 69 72  rue if we acquir
ead90 65 64 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  ed a PENDING loc
eada0 6b 20 74 68 69 73 20 74 69 6d 65 20 2a 2f 0a 20  k this time */. 
eadb0 20 77 69 6e 46 69 6c 65 20 2a 70 46 69 6c 65 20   winFile *pFile 
eadc0 3d 20 28 77 69 6e 46 69 6c 65 2a 29 69 64 3b 0a  = (winFile*)id;.
eadd0 0a 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65  .  assert( pFile
eade0 21 3d 30 20 29 3b 0a 20 20 4f 53 54 52 41 43 45  !=0 );.  OSTRACE
eadf0 35 28 22 4c 4f 43 4b 20 25 64 20 25 64 20 77 61  5("LOCK %d %d wa
eae00 73 20 25 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20  s %d(%d)\n",.   
eae10 20 20 20 20 20 20 20 70 46 69 6c 65 2d 3e 68 2c         pFile->h,
eae20 20 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c 65   locktype, pFile
eae30 2d 3e 6c 6f 63 6b 74 79 70 65 2c 20 70 46 69 6c  ->locktype, pFil
eae40 65 2d 3e 73 68 61 72 65 64 4c 6f 63 6b 42 79 74  e->sharedLockByt
eae50 65 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  e);..  /* If the
eae60 72 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20  re is already a 
eae70 6c 6f 63 6b 20 6f 66 20 74 68 69 73 20 74 79 70  lock of this typ
eae80 65 20 6f 72 20 6d 6f 72 65 20 72 65 73 74 72 69  e or more restri
eae90 63 74 69 76 65 20 6f 6e 20 74 68 65 0a 20 20 2a  ctive on the.  *
eaea0 2a 20 4f 73 46 69 6c 65 2c 20 64 6f 20 6e 6f 74  * OsFile, do not
eaeb0 68 69 6e 67 2e 20 44 6f 6e 27 74 20 75 73 65 20  hing. Don't use 
eaec0 74 68 65 20 65 6e 64 5f 6c 6f 63 6b 3a 20 65 78  the end_lock: ex
eaed0 69 74 20 70 61 74 68 2c 20 61 73 0a 20 20 2a 2a  it path, as.  **
eaee0 20 73 71 6c 69 74 65 33 4f 73 45 6e 74 65 72 4d   sqlite3OsEnterM
eaef0 75 74 65 78 28 29 20 68 61 73 6e 27 74 20 62 65  utex() hasn't be
eaf00 65 6e 20 63 61 6c 6c 65 64 20 79 65 74 2e 0a 20  en called yet.. 
eaf10 20 2a 2f 0a 20 20 69 66 28 20 70 46 69 6c 65 2d   */.  if( pFile-
eaf20 3e 6c 6f 63 6b 74 79 70 65 3e 3d 6c 6f 63 6b 74  >locktype>=lockt
eaf30 79 70 65 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ype ){.    retur
eaf40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
eaf50 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
eaf60 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 73 65 71   the locking seq
eaf70 75 65 6e 63 65 20 69 73 20 63 6f 72 72 65 63 74  uence is correct
eaf80 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
eaf90 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 21  pFile->locktype!
eafa0 3d 4e 4f 5f 4c 4f 43 4b 20 7c 7c 20 6c 6f 63 6b  =NO_LOCK || lock
eafb0 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
eafc0 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6c  K );.  assert( l
eafd0 6f 63 6b 74 79 70 65 21 3d 50 45 4e 44 49 4e 47  ocktype!=PENDING
eafe0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72  _LOCK );.  asser
eaff0 74 28 20 6c 6f 63 6b 74 79 70 65 21 3d 52 45 53  t( locktype!=RES
eb000 45 52 56 45 44 5f 4c 4f 43 4b 20 7c 7c 20 70 46  ERVED_LOCK || pF
eb010 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3d 3d 53  ile->locktype==S
eb020 48 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 0a 20  HARED_LOCK );.. 
eb030 20 2f 2a 20 4c 6f 63 6b 20 74 68 65 20 50 45 4e   /* Lock the PEN
eb040 44 49 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69  DING_LOCK byte i
eb050 66 20 77 65 20 6e 65 65 64 20 74 6f 20 61 63 71  f we need to acq
eb060 75 69 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c  uire a PENDING l
eb070 6f 63 6b 20 6f 72 0a 20 20 2a 2a 20 61 20 53 48  ock or.  ** a SH
eb080 41 52 45 44 20 6c 6f 63 6b 2e 20 20 49 66 20 77  ARED lock.  If w
eb090 65 20 61 72 65 20 61 63 71 75 69 72 69 6e 67 20  e are acquiring 
eb0a0 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 2c 20 74  a SHARED lock, t
eb0b0 68 65 20 61 63 71 75 69 73 69 74 69 6f 6e 20 6f  he acquisition o
eb0c0 66 0a 20 20 2a 2a 20 74 68 65 20 50 45 4e 44 49  f.  ** the PENDI
eb0d0 4e 47 5f 4c 4f 43 4b 20 62 79 74 65 20 69 73 20  NG_LOCK byte is 
eb0e0 74 65 6d 70 6f 72 61 72 79 2e 0a 20 20 2a 2f 0a  temporary..  */.
eb0f0 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d 20    newLocktype = 
eb100 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 3b  pFile->locktype;
eb110 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
eb120 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 0a  cktype==NO_LOCK.
eb130 20 20 20 7c 7c 20 28 6c 6f 63 6b 74 79 70 65 3d     || (locktype=
eb140 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
eb150 26 26 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  && pFile->lockty
eb160 70 65 3d 3d 52 45 53 45 52 56 45 44 5f 4c 4f 43  pe==RESERVED_LOC
eb170 4b 29 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20  K).  ){.    int 
eb180 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 77 68 69  cnt = 3;.    whi
eb190 6c 65 28 20 63 6e 74 2d 2d 3e 30 20 26 26 20 28  le( cnt-->0 && (
eb1a0 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70  res = LockFile(p
eb1b0 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49 4e 47  File->h, PENDING
eb1c0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 29  _BYTE, 0, 1, 0))
eb1d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
eb1e0 54 72 79 20 33 20 74 69 6d 65 73 20 74 6f 20 67  Try 3 times to g
eb1f0 65 74 20 74 68 65 20 70 65 6e 64 69 6e 67 20 6c  et the pending l
eb200 6f 63 6b 2e 20 20 54 68 65 20 70 65 6e 64 69 6e  ock.  The pendin
eb210 67 20 6c 6f 63 6b 20 6d 69 67 68 74 20 62 65 0a  g lock might be.
eb220 20 20 20 20 20 20 2a 2a 20 68 65 6c 64 20 62 79        ** held by
eb230 20 61 6e 6f 74 68 65 72 20 72 65 61 64 65 72 20   another reader 
eb240 70 72 6f 63 65 73 73 20 77 68 6f 20 77 69 6c 6c  process who will
eb250 20 72 65 6c 65 61 73 65 20 69 74 20 6d 6f 6d 65   release it mome
eb260 6e 74 61 72 69 6c 79 2e 0a 20 20 20 20 20 20 2a  ntarily..      *
eb270 2f 0a 20 20 20 20 20 20 4f 53 54 52 41 43 45 32  /.      OSTRACE2
eb280 28 22 63 6f 75 6c 64 20 6e 6f 74 20 67 65 74 20  ("could not get 
eb290 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63 6b 2e 20  a PENDING lock. 
eb2a0 63 6e 74 3d 25 64 5c 6e 22 2c 20 63 6e 74 29 3b  cnt=%d\n", cnt);
eb2b0 0a 20 20 20 20 20 20 53 6c 65 65 70 28 31 29 3b  .      Sleep(1);
eb2c0 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 50 65  .    }.    gotPe
eb2d0 6e 64 69 6e 67 4c 6f 63 6b 20 3d 20 72 65 73 3b  ndingLock = res;
eb2e0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
eb2f0 72 65 20 61 20 73 68 61 72 65 64 20 6c 6f 63 6b  re a shared lock
eb300 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63 6b  .  */.  if( lock
eb310 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43  type==SHARED_LOC
eb320 4b 20 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20  K && res ){.    
eb330 61 73 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c  assert( pFile->l
eb340 6f 63 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b  ocktype==NO_LOCK
eb350 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 67 65   );.    res = ge
eb360 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  tReadLock(pFile)
eb370 3b 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b  ;.    if( res ){
eb380 0a 20 20 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79  .      newLockty
eb390 70 65 20 3d 20 53 48 41 52 45 44 5f 4c 4f 43 4b  pe = SHARED_LOCK
eb3a0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
eb3b0 2a 20 41 63 71 75 69 72 65 20 61 20 52 45 53 45  * Acquire a RESE
eb3c0 52 56 45 44 20 6c 6f 63 6b 0a 20 20 2a 2f 0a 20  RVED lock.  */. 
eb3d0 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d 52   if( locktype==R
eb3e0 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 26 26 20  ESERVED_LOCK && 
eb3f0 72 65 73 20 29 7b 0a 20 20 20 20 61 73 73 65 72  res ){.    asser
eb400 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79  t( pFile->lockty
eb410 70 65 3d 3d 53 48 41 52 45 44 5f 4c 4f 43 4b 20  pe==SHARED_LOCK 
eb420 29 3b 0a 20 20 20 20 72 65 73 20 3d 20 4c 6f 63  );.    res = Loc
eb430 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
eb440 52 45 53 45 52 56 45 44 5f 42 59 54 45 2c 20 30  RESERVED_BYTE, 0
eb450 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  , 1, 0);.    if(
eb460 20 72 65 73 20 29 7b 0a 20 20 20 20 20 20 6e 65   res ){.      ne
eb470 77 4c 6f 63 6b 74 79 70 65 20 3d 20 52 45 53 45  wLocktype = RESE
eb480 52 56 45 44 5f 4c 4f 43 4b 3b 0a 20 20 20 20 7d  RVED_LOCK;.    }
eb490 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 63 71 75 69  .  }..  /* Acqui
eb4a0 72 65 20 61 20 50 45 4e 44 49 4e 47 20 6c 6f 63  re a PENDING loc
eb4b0 6b 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6c 6f 63  k.  */.  if( loc
eb4c0 6b 74 79 70 65 3d 3d 45 58 43 4c 55 53 49 56 45  ktype==EXCLUSIVE
eb4d0 5f 4c 4f 43 4b 20 26 26 20 72 65 73 20 29 7b 0a  _LOCK && res ){.
eb4e0 20 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20      newLocktype 
eb4f0 3d 20 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 3b 0a  = PENDING_LOCK;.
eb500 20 20 20 20 67 6f 74 50 65 6e 64 69 6e 67 4c 6f      gotPendingLo
eb510 63 6b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  ck = 0;.  }..  /
eb520 2a 20 41 63 71 75 69 72 65 20 61 6e 20 45 58 43  * Acquire an EXC
eb530 4c 55 53 49 56 45 20 6c 6f 63 6b 0a 20 20 2a 2f  LUSIVE lock.  */
eb540 0a 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d  .  if( locktype=
eb550 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
eb560 26 26 20 72 65 73 20 29 7b 0a 20 20 20 20 61 73  && res ){.    as
eb570 73 65 72 74 28 20 70 46 69 6c 65 2d 3e 6c 6f 63  sert( pFile->loc
eb580 6b 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c 4f  ktype>=SHARED_LO
eb590 43 4b 20 29 3b 0a 20 20 20 20 72 65 73 20 3d 20  CK );.    res = 
eb5a0 75 6e 6c 6f 63 6b 52 65 61 64 4c 6f 63 6b 28 70  unlockReadLock(p
eb5b0 46 69 6c 65 29 3b 0a 20 20 20 20 4f 53 54 52 41  File);.    OSTRA
eb5c0 43 45 32 28 22 75 6e 72 65 61 64 6c 6f 63 6b 20  CE2("unreadlock 
eb5d0 3d 20 25 64 5c 6e 22 2c 20 72 65 73 29 3b 0a 20  = %d\n", res);. 
eb5e0 20 20 20 72 65 73 20 3d 20 4c 6f 63 6b 46 69 6c     res = LockFil
eb5f0 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
eb600 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41  ED_FIRST, 0, SHA
eb610 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
eb620 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20 20    if( res ){.   
eb630 20 20 20 6e 65 77 4c 6f 63 6b 74 79 70 65 20 3d     newLocktype =
eb640 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 3b   EXCLUSIVE_LOCK;
eb650 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
eb660 20 20 4f 53 54 52 41 43 45 32 28 22 65 72 72 6f    OSTRACE2("erro
eb670 72 2d 63 6f 64 65 20 3d 20 25 64 5c 6e 22 2c 20  r-code = %d\n", 
eb680 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 3b  GetLastError());
eb690 0a 20 20 20 20 20 20 67 65 74 52 65 61 64 4c 6f  .      getReadLo
eb6a0 63 6b 28 70 46 69 6c 65 29 3b 0a 20 20 20 20 7d  ck(pFile);.    }
eb6b0 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65  .  }..  /* If we
eb6c0 20 61 72 65 20 68 6f 6c 64 69 6e 67 20 61 20 50   are holding a P
eb6d0 45 4e 44 49 4e 47 20 6c 6f 63 6b 20 74 68 61 74  ENDING lock that
eb6e0 20 6f 75 67 68 74 20 74 6f 20 62 65 20 72 65 6c   ought to be rel
eb6f0 65 61 73 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a  eased, then.  **
eb700 20 72 65 6c 65 61 73 65 20 69 74 20 6e 6f 77 2e   release it now.
eb710 0a 20 20 2a 2f 0a 20 20 69 66 28 20 67 6f 74 50  .  */.  if( gotP
eb720 65 6e 64 69 6e 67 4c 6f 63 6b 20 26 26 20 6c 6f  endingLock && lo
eb730 63 6b 74 79 70 65 3d 3d 53 48 41 52 45 44 5f 4c  cktype==SHARED_L
eb740 4f 43 4b 20 29 7b 0a 20 20 20 20 55 6e 6c 6f 63  OCK ){.    Unloc
eb750 6b 46 69 6c 65 28 70 46 69 6c 65 2d 3e 68 2c 20  kFile(pFile->h, 
eb760 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 20 30 2c  PENDING_BYTE, 0,
eb770 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f   1, 0);.  }..  /
eb780 2a 20 55 70 64 61 74 65 20 74 68 65 20 73 74 61  * Update the sta
eb790 74 65 20 6f 66 20 74 68 65 20 6c 6f 63 6b 20 68  te of the lock h
eb7a0 61 73 20 68 65 6c 64 20 69 6e 20 74 68 65 20 66  as held in the f
eb7b0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74  ile descriptor t
eb7c0 68 65 6e 0a 20 20 2a 2a 20 72 65 74 75 72 6e 20  hen.  ** return 
eb7d0 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
eb7e0 72 65 73 75 6c 74 20 63 6f 64 65 2e 0a 20 20 2a  result code..  *
eb7f0 2f 0a 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20  /.  if( res ){. 
eb800 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
eb810 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
eb820 4f 53 54 52 41 43 45 34 28 22 4c 4f 43 4b 20 46  OSTRACE4("LOCK F
eb830 41 49 4c 45 44 20 25 64 20 74 72 79 69 6e 67 20  AILED %d trying 
eb840 66 6f 72 20 25 64 20 62 75 74 20 67 6f 74 20 25  for %d but got %
eb850 64 5c 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 0a  d\n", pFile->h,.
eb860 20 20 20 20 20 20 20 20 20 20 20 6c 6f 63 6b 74             lockt
eb870 79 70 65 2c 20 6e 65 77 4c 6f 63 6b 74 79 70 65  ype, newLocktype
eb880 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
eb890 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a 20 20 70  TE_BUSY;.  }.  p
eb8a0 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70 65 20 3d  File->locktype =
eb8b0 20 6e 65 77 4c 6f 63 6b 74 79 70 65 3b 0a 20 20   newLocktype;.  
eb8c0 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
eb8d0 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
eb8e0 20 63 68 65 63 6b 73 20 69 66 20 74 68 65 72 65   checks if there
eb8f0 20 69 73 20 61 20 52 45 53 45 52 56 45 44 20 6c   is a RESERVED l
eb900 6f 63 6b 20 68 65 6c 64 20 6f 6e 20 74 68 65 20  ock held on the 
eb910 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 66 69 6c  specified.** fil
eb920 65 20 62 79 20 74 68 69 73 20 6f 72 20 61 6e 79  e by this or any
eb930 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73 2e 20   other process. 
eb940 49 66 20 73 75 63 68 20 61 20 6c 6f 63 6b 20 69  If such a lock i
eb950 73 20 68 65 6c 64 2c 20 72 65 74 75 72 6e 0a 2a  s held, return.*
eb960 2a 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 6f 74 68 65  * non-zero, othe
eb970 72 77 69 73 65 20 7a 65 72 6f 2e 0a 2a 2f 0a 73  rwise zero..*/.s
eb980 74 61 74 69 63 20 69 6e 74 20 77 69 6e 43 68 65  tatic int winChe
eb990 63 6b 52 65 73 65 72 76 65 64 4c 6f 63 6b 28 73  ckReservedLock(s
eb9a0 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c  qlite3_file *id,
eb9b0 20 69 6e 74 20 2a 70 52 65 73 4f 75 74 29 7b 0a   int *pResOut){.
eb9c0 20 20 69 6e 74 20 72 63 3b 0a 20 20 77 69 6e 46    int rc;.  winF
eb9d0 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
eb9e0 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 61 73 73  nFile*)id;.  ass
eb9f0 65 72 74 28 20 70 46 69 6c 65 21 3d 30 20 29 3b  ert( pFile!=0 );
eba00 0a 20 20 69 66 28 20 70 46 69 6c 65 2d 3e 6c 6f  .  if( pFile->lo
eba10 63 6b 74 79 70 65 3e 3d 52 45 53 45 52 56 45 44  cktype>=RESERVED
eba20 5f 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 72 63 20  _LOCK ){.    rc 
eba30 3d 20 31 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  = 1;.    OSTRACE
eba40 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  3("TEST WR-LOCK 
eba50 25 64 20 25 64 20 28 6c 6f 63 61 6c 29 5c 6e 22  %d %d (local)\n"
eba60 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29 3b  , pFile->h, rc);
eba70 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
eba80 20 3d 20 4c 6f 63 6b 46 69 6c 65 28 70 46 69 6c   = LockFile(pFil
eba90 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44 5f 42  e->h, RESERVED_B
ebaa0 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b 0a 20  YTE, 0, 1, 0);. 
ebab0 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
ebac0 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28 70 46     UnlockFile(pF
ebad0 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56 45 44  ile->h, RESERVED
ebae0 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30 29 3b  _BYTE, 0, 1, 0);
ebaf0 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
ebb00 21 72 63 3b 0a 20 20 20 20 4f 53 54 52 41 43 45  !rc;.    OSTRACE
ebb10 33 28 22 54 45 53 54 20 57 52 2d 4c 4f 43 4b 20  3("TEST WR-LOCK 
ebb20 25 64 20 25 64 20 28 72 65 6d 6f 74 65 29 5c 6e  %d %d (remote)\n
ebb30 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 72 63 29  ", pFile->h, rc)
ebb40 3b 0a 20 20 7d 0a 20 20 2a 70 52 65 73 4f 75 74  ;.  }.  *pResOut
ebb50 20 3d 20 72 63 3b 0a 20 20 72 65 74 75 72 6e 20   = rc;.  return 
ebb60 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
ebb70 0a 2a 2a 20 4c 6f 77 65 72 20 74 68 65 20 6c 6f  .** Lower the lo
ebb80 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 6e 20 66  cking level on f
ebb90 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 69  ile descriptor i
ebba0 64 20 74 6f 20 6c 6f 63 6b 74 79 70 65 2e 20 20  d to locktype.  
ebbb0 6c 6f 63 6b 74 79 70 65 0a 2a 2a 20 6d 75 73 74  locktype.** must
ebbc0 20 62 65 20 65 69 74 68 65 72 20 4e 4f 5f 4c 4f   be either NO_LO
ebbd0 43 4b 20 6f 72 20 53 48 41 52 45 44 5f 4c 4f 43  CK or SHARED_LOC
ebbe0 4b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  K..**.** If the 
ebbf0 6c 6f 63 6b 69 6e 67 20 6c 65 76 65 6c 20 6f 66  locking level of
ebc00 20 74 68 65 20 66 69 6c 65 20 64 65 73 63 72 69   the file descri
ebc10 70 74 6f 72 20 69 73 20 61 6c 72 65 61 64 79 20  ptor is already 
ebc20 61 74 20 6f 72 20 62 65 6c 6f 77 0a 2a 2a 20 74  at or below.** t
ebc30 68 65 20 72 65 71 75 65 73 74 65 64 20 6c 6f 63  he requested loc
ebc40 6b 69 6e 67 20 6c 65 76 65 6c 2c 20 74 68 69 73  king level, this
ebc50 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f   routine is a no
ebc60 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73  -op..**.** It is
ebc70 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 66 6f   not possible fo
ebc80 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 74  r this routine t
ebc90 6f 20 66 61 69 6c 20 69 66 20 74 68 65 20 73 65  o fail if the se
ebca0 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a 2a  cond argument.**
ebcb0 20 69 73 20 4e 4f 5f 4c 4f 43 4b 2e 20 20 49 66   is NO_LOCK.  If
ebcc0 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75   the second argu
ebcd0 6d 65 6e 74 20 69 73 20 53 48 41 52 45 44 5f 4c  ment is SHARED_L
ebce0 4f 43 4b 20 74 68 65 6e 20 74 68 69 73 20 72 6f  OCK then this ro
ebcf0 75 74 69 6e 65 0a 2a 2a 20 6d 69 67 68 74 20 72  utine.** might r
ebd00 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
ebd10 52 52 3b 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  RR;.*/.static in
ebd20 74 20 77 69 6e 55 6e 6c 6f 63 6b 28 73 71 6c 69  t winUnlock(sqli
ebd30 74 65 33 5f 66 69 6c 65 20 2a 69 64 2c 20 69 6e  te3_file *id, in
ebd40 74 20 6c 6f 63 6b 74 79 70 65 29 7b 0a 20 20 69  t locktype){.  i
ebd50 6e 74 20 74 79 70 65 3b 0a 20 20 77 69 6e 46 69  nt type;.  winFi
ebd60 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69 6e  le *pFile = (win
ebd70 46 69 6c 65 2a 29 69 64 3b 0a 20 20 69 6e 74 20  File*)id;.  int 
ebd80 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
ebd90 20 20 61 73 73 65 72 74 28 20 70 46 69 6c 65 21    assert( pFile!
ebda0 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
ebdb0 6c 6f 63 6b 74 79 70 65 3c 3d 53 48 41 52 45 44  locktype<=SHARED
ebdc0 5f 4c 4f 43 4b 20 29 3b 0a 20 20 4f 53 54 52 41  _LOCK );.  OSTRA
ebdd0 43 45 35 28 22 55 4e 4c 4f 43 4b 20 25 64 20 74  CE5("UNLOCK %d t
ebde0 6f 20 25 64 20 77 61 73 20 25 64 28 25 64 29 5c  o %d was %d(%d)\
ebdf0 6e 22 2c 20 70 46 69 6c 65 2d 3e 68 2c 20 6c 6f  n", pFile->h, lo
ebe00 63 6b 74 79 70 65 2c 0a 20 20 20 20 20 20 20 20  cktype,.        
ebe10 20 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74 79 70    pFile->locktyp
ebe20 65 2c 20 70 46 69 6c 65 2d 3e 73 68 61 72 65 64  e, pFile->shared
ebe30 4c 6f 63 6b 42 79 74 65 29 3b 0a 20 20 74 79 70  LockByte);.  typ
ebe40 65 20 3d 20 70 46 69 6c 65 2d 3e 6c 6f 63 6b 74  e = pFile->lockt
ebe50 79 70 65 3b 0a 20 20 69 66 28 20 74 79 70 65 3e  ype;.  if( type>
ebe60 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20  =EXCLUSIVE_LOCK 
ebe70 29 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c  ){.    UnlockFil
ebe80 65 28 70 46 69 6c 65 2d 3e 68 2c 20 53 48 41 52  e(pFile->h, SHAR
ebe90 45 44 5f 46 49 52 53 54 2c 20 30 2c 20 53 48 41  ED_FIRST, 0, SHA
ebea0 52 45 44 5f 53 49 5a 45 2c 20 30 29 3b 0a 20 20  RED_SIZE, 0);.  
ebeb0 20 20 69 66 28 20 6c 6f 63 6b 74 79 70 65 3d 3d    if( locktype==
ebec0 53 48 41 52 45 44 5f 4c 4f 43 4b 20 26 26 20 21  SHARED_LOCK && !
ebed0 67 65 74 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c  getReadLock(pFil
ebee0 65 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  e) ){.      /* T
ebef0 68 69 73 20 73 68 6f 75 6c 64 20 6e 65 76 65 72  his should never
ebf00 20 68 61 70 70 65 6e 2e 20 20 57 65 20 73 68 6f   happen.  We sho
ebf10 75 6c 64 20 61 6c 77 61 79 73 20 62 65 20 61 62  uld always be ab
ebf20 6c 65 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 72  le to.      ** r
ebf30 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65 61  eacquire the rea
ebf40 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 20 20 20 20  d lock */.      
ebf50 72 63 20 3d 20 53 51 4c 49 54 45 5f 49 4f 45 52  rc = SQLITE_IOER
ebf60 52 5f 55 4e 4c 4f 43 4b 3b 0a 20 20 20 20 7d 0a  R_UNLOCK;.    }.
ebf70 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65 3e 3d    }.  if( type>=
ebf80 52 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 29 7b  RESERVED_LOCK ){
ebf90 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65 28  .    UnlockFile(
ebfa0 70 46 69 6c 65 2d 3e 68 2c 20 52 45 53 45 52 56  pFile->h, RESERV
ebfb0 45 44 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30  ED_BYTE, 0, 1, 0
ebfc0 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6c 6f 63  );.  }.  if( loc
ebfd0 6b 74 79 70 65 3d 3d 4e 4f 5f 4c 4f 43 4b 20 26  ktype==NO_LOCK &
ebfe0 26 20 74 79 70 65 3e 3d 53 48 41 52 45 44 5f 4c  & type>=SHARED_L
ebff0 4f 43 4b 20 29 7b 0a 20 20 20 20 75 6e 6c 6f 63  OCK ){.    unloc
ec000 6b 52 65 61 64 4c 6f 63 6b 28 70 46 69 6c 65 29  kReadLock(pFile)
ec010 3b 0a 20 20 7d 0a 20 20 69 66 28 20 74 79 70 65  ;.  }.  if( type
ec020 3e 3d 50 45 4e 44 49 4e 47 5f 4c 4f 43 4b 20 29  >=PENDING_LOCK )
ec030 7b 0a 20 20 20 20 55 6e 6c 6f 63 6b 46 69 6c 65  {.    UnlockFile
ec040 28 70 46 69 6c 65 2d 3e 68 2c 20 50 45 4e 44 49  (pFile->h, PENDI
ec050 4e 47 5f 42 59 54 45 2c 20 30 2c 20 31 2c 20 30  NG_BYTE, 0, 1, 0
ec060 29 3b 0a 20 20 7d 0a 20 20 70 46 69 6c 65 2d 3e  );.  }.  pFile->
ec070 6c 6f 63 6b 74 79 70 65 20 3d 20 6c 6f 63 6b 74  locktype = lockt
ec080 79 70 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ype;.  return rc
ec090 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 74 72  ;.}../*.** Contr
ec0a0 6f 6c 20 61 6e 64 20 71 75 65 72 79 20 6f 66 20  ol and query of 
ec0b0 74 68 65 20 6f 70 65 6e 20 66 69 6c 65 20 68 61  the open file ha
ec0c0 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ndle..*/.static 
ec0d0 69 6e 74 20 77 69 6e 46 69 6c 65 43 6f 6e 74 72  int winFileContr
ec0e0 6f 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ol(sqlite3_file 
ec0f0 2a 69 64 2c 20 69 6e 74 20 6f 70 2c 20 76 6f 69  *id, int op, voi
ec100 64 20 2a 70 41 72 67 29 7b 0a 20 20 73 77 69 74  d *pArg){.  swit
ec110 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63 61  ch( op ){.    ca
ec120 73 65 20 53 51 4c 49 54 45 5f 46 43 4e 54 4c 5f  se SQLITE_FCNTL_
ec130 4c 4f 43 4b 53 54 41 54 45 3a 20 7b 0a 20 20 20  LOCKSTATE: {.   
ec140 20 20 20 2a 28 69 6e 74 2a 29 70 41 72 67 20 3d     *(int*)pArg =
ec150 20 28 28 77 69 6e 46 69 6c 65 2a 29 69 64 29 2d   ((winFile*)id)-
ec160 3e 6c 6f 63 6b 74 79 70 65 3b 0a 20 20 20 20 20  >locktype;.     
ec170 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
ec180 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  K;.    }.  }.  r
ec190 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
ec1a0 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  OR;.}../*.** Ret
ec1b0 75 72 6e 20 74 68 65 20 73 65 63 74 6f 72 20 73  urn the sector s
ec1c0 69 7a 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20  ize in bytes of 
ec1d0 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 62  the underlying b
ec1e0 6c 6f 63 6b 20 64 65 76 69 63 65 20 66 6f 72 0a  lock device for.
ec1f0 2a 2a 20 74 68 65 20 73 70 65 63 69 66 69 65 64  ** the specified
ec200 20 66 69 6c 65 2e 20 54 68 69 73 20 69 73 20 61   file. This is a
ec210 6c 6d 6f 73 74 20 61 6c 77 61 79 73 20 35 31 32  lmost always 512
ec220 20 62 79 74 65 73 2c 20 62 75 74 20 6d 61 79 20   bytes, but may 
ec230 62 65 0a 2a 2a 20 6c 61 72 67 65 72 20 66 6f 72  be.** larger for
ec240 20 73 6f 6d 65 20 64 65 76 69 63 65 73 2e 0a 2a   some devices..*
ec250 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 63 6f 64 65  *.** SQLite code
ec260 20 61 73 73 75 6d 65 73 20 74 68 69 73 20 66 75   assumes this fu
ec270 6e 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 20 66 61  nction cannot fa
ec280 69 6c 2e 20 49 74 20 61 6c 73 6f 20 61 73 73 75  il. It also assu
ec290 6d 65 73 20 74 68 61 74 0a 2a 2a 20 69 66 20 74  mes that.** if t
ec2a0 77 6f 20 66 69 6c 65 73 20 61 72 65 20 63 72 65  wo files are cre
ec2b0 61 74 65 64 20 69 6e 20 74 68 65 20 73 61 6d 65  ated in the same
ec2c0 20 66 69 6c 65 2d 73 79 73 74 65 6d 20 64 69 72   file-system dir
ec2d0 65 63 74 6f 72 79 20 28 69 2e 65 2e 0a 2a 2a 20  ectory (i.e..** 
ec2e0 61 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 69  a database and i
ec2f0 74 73 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 29  ts journal file)
ec300 20 74 68 61 74 20 74 68 65 20 73 65 63 74 6f 72   that the sector
ec310 20 73 69 7a 65 20 77 69 6c 6c 20 62 65 20 74 68   size will be th
ec320 65 0a 2a 2a 20 73 61 6d 65 20 66 6f 72 20 62 6f  e.** same for bo
ec330 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  th..*/.static in
ec340 74 20 77 69 6e 53 65 63 74 6f 72 53 69 7a 65 28  t winSectorSize(
ec350 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 69 64  sqlite3_file *id
ec360 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
ec370 54 45 5f 44 45 46 41 55 4c 54 5f 53 45 43 54 4f  TE_DEFAULT_SECTO
ec380 52 5f 53 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  R_SIZE;.}../*.**
ec390 20 52 65 74 75 72 6e 20 61 20 76 65 63 74 6f 72   Return a vector
ec3a0 20 6f 66 20 64 65 76 69 63 65 20 63 68 61 72 61   of device chara
ec3b0 63 74 65 72 69 73 74 69 63 73 2e 0a 2a 2f 0a 73  cteristics..*/.s
ec3c0 74 61 74 69 63 20 69 6e 74 20 77 69 6e 44 65 76  tatic int winDev
ec3d0 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
ec3e0 63 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  cs(sqlite3_file 
ec3f0 2a 69 64 29 7b 0a 20 20 72 65 74 75 72 6e 20 30  *id){.  return 0
ec400 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
ec410 76 65 63 74 6f 72 20 64 65 66 69 6e 65 73 20 61  vector defines a
ec420 6c 6c 20 74 68 65 20 6d 65 74 68 6f 64 73 20 74  ll the methods t
ec430 68 61 74 20 63 61 6e 20 6f 70 65 72 61 74 65 20  hat can operate 
ec440 6f 6e 20 61 6e 0a 2a 2a 20 73 71 6c 69 74 65 33  on an.** sqlite3
ec450 5f 66 69 6c 65 20 66 6f 72 20 77 69 6e 33 32 2e  _file for win32.
ec460 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
ec470 20 73 71 6c 69 74 65 33 5f 69 6f 5f 6d 65 74 68   sqlite3_io_meth
ec480 6f 64 73 20 77 69 6e 49 6f 4d 65 74 68 6f 64 20  ods winIoMethod 
ec490 3d 20 7b 0a 20 20 31 2c 20 20 20 20 20 20 20 20  = {.  1,        
ec4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ec4b0 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20  /* iVersion */. 
ec4c0 20 77 69 6e 43 6c 6f 73 65 2c 0a 20 20 77 69 6e   winClose,.  win
ec4d0 52 65 61 64 2c 0a 20 20 77 69 6e 57 72 69 74 65  Read,.  winWrite
ec4e0 2c 0a 20 20 77 69 6e 54 72 75 6e 63 61 74 65 2c  ,.  winTruncate,
ec4f0 0a 20 20 77 69 6e 53 79 6e 63 2c 0a 20 20 77 69  .  winSync,.  wi
ec500 6e 46 69 6c 65 53 69 7a 65 2c 0a 20 20 77 69 6e  nFileSize,.  win
ec510 4c 6f 63 6b 2c 0a 20 20 77 69 6e 55 6e 6c 6f 63  Lock,.  winUnloc
ec520 6b 2c 0a 20 20 77 69 6e 43 68 65 63 6b 52 65 73  k,.  winCheckRes
ec530 65 72 76 65 64 4c 6f 63 6b 2c 0a 20 20 77 69 6e  ervedLock,.  win
ec540 46 69 6c 65 43 6f 6e 74 72 6f 6c 2c 0a 20 20 77  FileControl,.  w
ec550 69 6e 53 65 63 74 6f 72 53 69 7a 65 2c 0a 20 20  inSectorSize,.  
ec560 77 69 6e 44 65 76 69 63 65 43 68 61 72 61 63 74  winDeviceCharact
ec570 65 72 69 73 74 69 63 73 0a 7d 3b 0a 0a 2f 2a 2a  eristics.};../**
ec580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec590 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec5a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec5b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec5c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 48 65 72  *********.** Her
ec5d0 65 20 65 6e 64 73 20 74 68 65 20 49 2f 4f 20 6d  e ends the I/O m
ec5e0 65 74 68 6f 64 73 20 74 68 61 74 20 66 6f 72 6d  ethods that form
ec5f0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6f 5f   the sqlite3_io_
ec600 6d 65 74 68 6f 64 73 20 6f 62 6a 65 63 74 2e 0a  methods object..
ec610 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 62  **.** The next b
ec620 6c 6f 63 6b 20 6f 66 20 63 6f 64 65 20 69 6d 70  lock of code imp
ec630 6c 65 6d 65 6e 74 73 20 74 68 65 20 56 46 53 20  lements the VFS 
ec640 6d 65 74 68 6f 64 73 2e 0a 2a 2a 2a 2a 2a 2a 2a  methods..*******
ec650 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec660 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec670 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec680 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
ec690 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  *****/../*.** Co
ec6a0 6e 76 65 72 74 20 61 20 55 54 46 2d 38 20 66 69  nvert a UTF-8 fi
ec6b0 6c 65 6e 61 6d 65 20 69 6e 74 6f 20 77 68 61 74  lename into what
ec6c0 65 76 65 72 20 66 6f 72 6d 20 74 68 65 20 75 6e  ever form the un
ec6d0 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 6f 70 65 72  derlying.** oper
ec6e0 61 74 69 6e 67 20 73 79 73 74 65 6d 20 77 61 6e  ating system wan
ec6f0 74 73 20 66 69 6c 65 6e 61 6d 65 73 20 69 6e 2e  ts filenames in.
ec700 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20    Space to hold 
ec710 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  the result.** is
ec720 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 6d   obtained from m
ec730 61 6c 6c 6f 63 20 61 6e 64 20 6d 75 73 74 20 62  alloc and must b
ec740 65 20 66 72 65 65 64 20 62 79 20 74 68 65 20 63  e freed by the c
ec750 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
ec760 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
ec770 69 64 20 2a 63 6f 6e 76 65 72 74 55 74 66 38 46  id *convertUtf8F
ec780 69 6c 65 6e 61 6d 65 28 63 6f 6e 73 74 20 63 68  ilename(const ch
ec790 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b 0a  ar *zFilename){.
ec7a0 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74    void *zConvert
ec7b0 65 64 20 3d 20 30 3b 0a 20 20 69 66 28 20 69 73  ed = 0;.  if( is
ec7c0 4e 54 28 29 20 29 7b 0a 20 20 20 20 7a 43 6f 6e  NT() ){.    zCon
ec7d0 76 65 72 74 65 64 20 3d 20 75 74 66 38 54 6f 55  verted = utf8ToU
ec7e0 6e 69 63 6f 64 65 28 7a 46 69 6c 65 6e 61 6d 65  nicode(zFilename
ec7f0 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
ec800 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
ec810 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
ec820 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
ec830 78 65 63 75 74 65 64 2e 20 0a 2a 2f 0a 23 69 66  xecuted. .*/.#if
ec840 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ec850 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ==0.  }else{.   
ec860 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20 75 74   zConverted = ut
ec870 66 38 54 6f 4d 62 63 73 28 7a 46 69 6c 65 6e 61  f8ToMbcs(zFilena
ec880 6d 65 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d 0a  me);.#endif.  }.
ec890 20 20 2f 2a 20 63 61 6c 6c 65 72 20 77 69 6c 6c    /* caller will
ec8a0 20 68 61 6e 64 6c 65 20 6f 75 74 20 6f 66 20 6d   handle out of m
ec8b0 65 6d 6f 72 79 20 2a 2f 0a 20 20 72 65 74 75 72  emory */.  retur
ec8c0 6e 20 7a 43 6f 6e 76 65 72 74 65 64 3b 0a 7d 0a  n zConverted;.}.
ec8d0 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
ec8e0 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 20 6e  temporary file n
ec8f0 61 6d 65 20 69 6e 20 7a 42 75 66 2e 20 20 7a 42  ame in zBuf.  zB
ec900 75 66 20 6d 75 73 74 20 62 65 20 62 69 67 20 65  uf must be big e
ec910 6e 6f 75 67 68 20 74 6f 0a 2a 2a 20 68 6f 6c 64  nough to.** hold
ec920 20 61 74 20 70 56 66 73 2d 3e 6d 78 50 61 74 68   at pVfs->mxPath
ec930 6e 61 6d 65 20 63 68 61 72 61 63 74 65 72 73 2e  name characters.
ec940 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
ec950 65 74 54 65 6d 70 6e 61 6d 65 28 69 6e 74 20 6e  etTempname(int n
ec960 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Buf, char *zBuf)
ec970 7b 0a 20 20 73 74 61 74 69 63 20 63 68 61 72 20  {.  static char 
ec980 7a 43 68 61 72 73 5b 5d 20 3d 0a 20 20 20 20 22  zChars[] =.    "
ec990 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70  abcdefghijklmnop
ec9a0 71 72 73 74 75 76 77 78 79 7a 22 0a 20 20 20 20  qrstuvwxyz".    
ec9b0 22 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f  "ABCDEFGHIJKLMNO
ec9c0 50 51 52 53 54 55 56 57 58 59 5a 22 0a 20 20 20  PQRSTUVWXYZ".   
ec9d0 20 22 30 31 32 33 34 35 36 37 38 39 22 3b 0a 20   "0123456789";. 
ec9e0 20 73 69 7a 65 5f 74 20 69 2c 20 6a 3b 0a 20 20   size_t i, j;.  
ec9f0 63 68 61 72 20 7a 54 65 6d 70 50 61 74 68 5b 4d  char zTempPath[M
eca00 41 58 5f 50 41 54 48 2b 31 5d 3b 0a 20 20 69 66  AX_PATH+1];.  if
eca10 28 20 73 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64  ( sqlite3_temp_d
eca20 69 72 65 63 74 6f 72 79 20 29 7b 0a 20 20 20 20  irectory ){.    
eca30 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
eca40 28 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54  (MAX_PATH-30, zT
eca50 65 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 73  empPath, "%s", s
eca60 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
eca70 63 74 6f 72 79 29 3b 0a 20 20 7d 65 6c 73 65 20  ctory);.  }else 
eca80 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20  if( isNT() ){.  
eca90 20 20 63 68 61 72 20 2a 7a 4d 75 6c 74 69 3b 0a    char *zMulti;.
ecaa0 20 20 20 20 57 43 48 41 52 20 7a 57 69 64 65 50      WCHAR zWideP
ecab0 61 74 68 5b 4d 41 58 5f 50 41 54 48 5d 3b 0a 20  ath[MAX_PATH];. 
ecac0 20 20 20 47 65 74 54 65 6d 70 50 61 74 68 57 28     GetTempPathW(
ecad0 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 57 69  MAX_PATH-30, zWi
ecae0 64 65 50 61 74 68 29 3b 0a 20 20 20 20 7a 4d 75  dePath);.    zMu
ecaf0 6c 74 69 20 3d 20 75 6e 69 63 6f 64 65 54 6f 55  lti = unicodeToU
ecb00 74 66 38 28 7a 57 69 64 65 50 61 74 68 29 3b 0a  tf8(zWidePath);.
ecb10 20 20 20 20 69 66 28 20 7a 4d 75 6c 74 69 20 29      if( zMulti )
ecb20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
ecb30 73 6e 70 72 69 6e 74 66 28 4d 41 58 5f 50 41 54  snprintf(MAX_PAT
ecb40 48 2d 33 30 2c 20 7a 54 65 6d 70 50 61 74 68 2c  H-30, zTempPath,
ecb50 20 22 25 73 22 2c 20 7a 4d 75 6c 74 69 29 3b 0a   "%s", zMulti);.
ecb60 20 20 20 20 20 20 66 72 65 65 28 7a 4d 75 6c 74        free(zMult
ecb70 69 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  i);.    }else{. 
ecb80 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
ecb90 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
ecba0 2f 2a 20 69 73 4e 54 28 29 20 69 73 20 31 20 69  /* isNT() is 1 i
ecbb0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
ecbc0 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73 20 65 6c  E==1, so this el
ecbd0 73 65 20 69 73 20 6e 65 76 65 72 20 65 78 65 63  se is never exec
ecbe0 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e 63 65 20  uted. .** Since 
ecbf0 74 68 65 20 41 53 43 49 49 20 76 65 72 73 69 6f  the ASCII versio
ecc00 6e 20 6f 66 20 74 68 65 73 65 20 57 69 6e 64 6f  n of these Windo
ecc10 77 73 20 41 50 49 20 64 6f 20 6e 6f 74 20 65 78  ws API do not ex
ecc20 69 73 74 20 66 6f 72 20 57 49 4e 43 45 2c 0a 2a  ist for WINCE,.*
ecc30 2a 20 69 74 27 73 20 69 6d 70 6f 72 74 61 6e 74  * it's important
ecc40 20 74 6f 20 6e 6f 74 20 72 65 66 65 72 65 6e 63   to not referenc
ecc50 65 20 74 68 65 6d 20 66 6f 72 20 57 49 4e 43 45  e them for WINCE
ecc60 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 20   builds..*/.#if 
ecc70 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d  SQLITE_OS_WINCE=
ecc80 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  =0.  }else{.    
ecc90 63 68 61 72 20 2a 7a 55 74 66 38 3b 0a 20 20 20  char *zUtf8;.   
ecca0 20 63 68 61 72 20 7a 4d 62 63 73 50 61 74 68 5b   char zMbcsPath[
eccb0 4d 41 58 5f 50 41 54 48 5d 3b 0a 20 20 20 20 47  MAX_PATH];.    G
eccc0 65 74 54 65 6d 70 50 61 74 68 41 28 4d 41 58 5f  etTempPathA(MAX_
eccd0 50 41 54 48 2d 33 30 2c 20 7a 4d 62 63 73 50 61  PATH-30, zMbcsPa
ecce0 74 68 29 3b 0a 20 20 20 20 7a 55 74 66 38 20 3d  th);.    zUtf8 =
eccf0 20 73 71 6c 69 74 65 33 5f 77 69 6e 33 32 5f 6d   sqlite3_win32_m
ecd00 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a 4d 62 63  bcs_to_utf8(zMbc
ecd10 73 50 61 74 68 29 3b 0a 20 20 20 20 69 66 28 20  sPath);.    if( 
ecd20 7a 55 74 66 38 20 29 7b 0a 20 20 20 20 20 20 73  zUtf8 ){.      s
ecd30 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
ecd40 4d 41 58 5f 50 41 54 48 2d 33 30 2c 20 7a 54 65  MAX_PATH-30, zTe
ecd50 6d 70 50 61 74 68 2c 20 22 25 73 22 2c 20 7a 55  mpPath, "%s", zU
ecd60 74 66 38 29 3b 0a 20 20 20 20 20 20 66 72 65 65  tf8);.      free
ecd70 28 7a 55 74 66 38 29 3b 0a 20 20 20 20 7d 65 6c  (zUtf8);.    }el
ecd80 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  se{.      return
ecd90 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
ecda0 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a     }.#endif.  }.
ecdb0 20 20 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 7a    for(i=strlen(z
ecdc0 54 65 6d 70 50 61 74 68 29 3b 20 69 3e 30 20 26  TempPath); i>0 &
ecdd0 26 20 7a 54 65 6d 70 50 61 74 68 5b 69 2d 31 5d  & zTempPath[i-1]
ecde0 3d 3d 27 5c 5c 27 3b 20 69 2d 2d 29 7b 7d 0a 20  =='\\'; i--){}. 
ecdf0 20 7a 54 65 6d 70 50 61 74 68 5b 69 5d 20 3d 20   zTempPath[i] = 
ece00 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  0;.  sqlite3_snp
ece10 72 69 6e 74 66 28 6e 42 75 66 2d 33 30 2c 20 7a  rintf(nBuf-30, z
ece20 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Buf,.           
ece30 20 20 20 20 20 20 20 20 22 25 73 5c 5c 22 53 51          "%s\\"SQ
ece40 4c 49 54 45 5f 54 45 4d 50 5f 46 49 4c 45 5f 50  LITE_TEMP_FILE_P
ece50 52 45 46 49 58 2c 20 7a 54 65 6d 70 50 61 74 68  REFIX, zTempPath
ece60 29 3b 0a 20 20 6a 20 3d 20 73 74 72 6c 65 6e 28  );.  j = strlen(
ece70 7a 42 75 66 29 3b 0a 20 20 73 71 6c 69 74 65 33  zBuf);.  sqlite3
ece80 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 32 30 2c 20  _randomness(20, 
ece90 26 7a 42 75 66 5b 6a 5d 29 3b 0a 20 20 66 6f 72  &zBuf[j]);.  for
ecea0 28 69 3d 30 3b 20 69 3c 32 30 3b 20 69 2b 2b 2c  (i=0; i<20; i++,
eceb0 20 6a 2b 2b 29 7b 0a 20 20 20 20 7a 42 75 66 5b   j++){.    zBuf[
ecec0 6a 5d 20 3d 20 28 63 68 61 72 29 7a 43 68 61 72  j] = (char)zChar
eced0 73 5b 20 28 28 75 6e 73 69 67 6e 65 64 20 63 68  s[ ((unsigned ch
ecee0 61 72 29 7a 42 75 66 5b 6a 5d 29 25 28 73 69 7a  ar)zBuf[j])%(siz
ecef0 65 6f 66 28 7a 43 68 61 72 73 29 2d 31 29 20 5d  eof(zChars)-1) ]
ecf00 3b 0a 20 20 7d 0a 20 20 7a 42 75 66 5b 6a 5d 20  ;.  }.  zBuf[j] 
ecf10 3d 20 30 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  = 0;.  OSTRACE2(
ecf20 22 54 45 4d 50 20 46 49 4c 45 4e 41 4d 45 3a 20  "TEMP FILENAME: 
ecf30 25 73 5c 6e 22 2c 20 7a 42 75 66 29 3b 0a 20 20  %s\n", zBuf);.  
ecf40 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ecf50 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ; .}../*.** The 
ecf60 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
ecf70 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73 67 0a  getLastErrorMsg.
ecf80 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74 68  ** is zero if th
ecf90 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
ecfa0 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66 66  fits in the buff
ecfb0 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 0a  er, or non-zero.
ecfc0 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69 66  ** otherwise (if
ecfd0 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61 73   the message was
ecfe0 20 74 72 75 6e 63 61 74 65 64 29 2e 0a 2a 2f 0a   truncated)..*/.
ecff0 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 4c 61  static int getLa
ed000 73 74 45 72 72 6f 72 4d 73 67 28 69 6e 74 20 6e  stErrorMsg(int n
ed010 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29  Buf, char *zBuf)
ed020 7b 0a 20 20 44 57 4f 52 44 20 65 72 72 6f 72 20  {.  DWORD error 
ed030 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72 28 29  = GetLastError()
ed040 3b 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53  ;..#if SQLITE_OS
ed050 5f 57 49 4e 43 45 0a 20 20 73 71 6c 69 74 65 33  _WINCE.  sqlite3
ed060 5f 73 6e 70 72 69 6e 74 66 28 6e 42 75 66 2c 20  _snprintf(nBuf, 
ed070 7a 42 75 66 2c 20 22 4f 73 45 72 72 6f 72 20 30  zBuf, "OsError 0
ed080 78 25 78 20 28 25 75 29 22 2c 20 65 72 72 6f 72  x%x (%u)", error
ed090 2c 20 65 72 72 6f 72 29 3b 0a 23 65 6c 73 65 0a  , error);.#else.
ed0a0 20 20 2f 2a 20 46 6f 72 6d 61 74 4d 65 73 73 61    /* FormatMessa
ed0b0 67 65 20 72 65 74 75 72 6e 73 20 30 20 6f 6e 20  ge returns 0 on 
ed0c0 66 61 69 6c 75 72 65 2e 20 20 4f 74 68 65 72 77  failure.  Otherw
ed0d0 69 73 65 20 69 74 0a 20 20 2a 2a 20 72 65 74 75  ise it.  ** retu
ed0e0 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  rns the number o
ed0f0 66 20 54 43 48 41 52 73 20 77 72 69 74 74 65 6e  f TCHARs written
ed100 20 74 6f 20 74 68 65 20 6f 75 74 70 75 74 0a 20   to the output. 
ed110 20 2a 2a 20 62 75 66 66 65 72 2c 20 65 78 63 6c   ** buffer, excl
ed120 75 64 69 6e 67 20 74 68 65 20 74 65 72 6d 69 6e  uding the termin
ed130 61 74 69 6e 67 20 6e 75 6c 6c 20 63 68 61 72 2e  ating null char.
ed140 0a 20 20 2a 2f 0a 20 20 69 66 20 28 21 46 6f 72  .  */.  if (!For
ed150 6d 61 74 4d 65 73 73 61 67 65 41 28 46 4f 52 4d  matMessageA(FORM
ed160 41 54 5f 4d 45 53 53 41 47 45 5f 46 52 4f 4d 5f  AT_MESSAGE_FROM_
ed170 53 59 53 54 45 4d 2c 0a 20 20 20 20 20 20 20 20  SYSTEM,.        
ed180 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4e 55                NU
ed190 4c 4c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  LL,.            
ed1a0 20 20 20 20 20 20 20 20 20 20 65 72 72 6f 72 2c            error,
ed1b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
ed1c0 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20         0,.      
ed1d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ed1e0 7a 42 75 66 2c 0a 20 20 20 20 20 20 20 20 20 20  zBuf,.          
ed1f0 20 20 20 20 20 20 20 20 20 20 20 20 6e 42 75 66              nBuf
ed200 2d 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  -1,.            
ed210 20 20 20 20 20 20 20 20 20 20 30 29 29 0a 20 20            0)).  
ed220 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  {.    sqlite3_sn
ed230 70 72 69 6e 74 66 28 6e 42 75 66 2c 20 7a 42 75  printf(nBuf, zBu
ed240 66 2c 20 22 4f 73 45 72 72 6f 72 20 30 78 25 78  f, "OsError 0x%x
ed250 20 28 25 75 29 22 2c 20 65 72 72 6f 72 2c 20 65   (%u)", error, e
ed260 72 72 6f 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  rror);.  }.#endi
ed270 66 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  f..  return 0;.}
ed280 0a 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20  .../*.** Open a 
ed290 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
ed2a0 69 6e 74 20 77 69 6e 4f 70 65 6e 28 0a 20 20 73  int winOpen(.  s
ed2b0 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
ed2c0 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20  ,        /* Not 
ed2d0 75 73 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  used */.  const 
ed2e0 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20  char *zName,    
ed2f0 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
ed300 68 65 20 66 69 6c 65 20 28 55 54 46 2d 38 29 20  he file (UTF-8) 
ed310 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
ed320 65 20 2a 69 64 2c 20 20 20 20 20 20 20 20 20 2f  e *id,         /
ed330 2a 20 57 72 69 74 65 20 74 68 65 20 53 51 4c 69  * Write the SQLi
ed340 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 68  te file handle h
ed350 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  ere */.  int fla
ed360 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
ed370 20 20 20 2f 2a 20 4f 70 65 6e 20 6d 6f 64 65 20     /* Open mode 
ed380 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e 74 20 2a  flags */.  int *
ed390 70 4f 75 74 46 6c 61 67 73 20 20 20 20 20 20 20  pOutFlags       
ed3a0 20 20 20 20 20 2f 2a 20 53 74 61 74 75 73 20 72       /* Status r
ed3b0 65 74 75 72 6e 20 66 6c 61 67 73 20 2a 2f 0a 29  eturn flags */.)
ed3c0 7b 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20  {.  HANDLE h;.  
ed3d0 44 57 4f 52 44 20 64 77 44 65 73 69 72 65 64 41  DWORD dwDesiredA
ed3e0 63 63 65 73 73 3b 0a 20 20 44 57 4f 52 44 20 64  ccess;.  DWORD d
ed3f0 77 53 68 61 72 65 4d 6f 64 65 3b 0a 20 20 44 57  wShareMode;.  DW
ed400 4f 52 44 20 64 77 43 72 65 61 74 69 6f 6e 44 69  ORD dwCreationDi
ed410 73 70 6f 73 69 74 69 6f 6e 3b 0a 20 20 44 57 4f  sposition;.  DWO
ed420 52 44 20 64 77 46 6c 61 67 73 41 6e 64 41 74 74  RD dwFlagsAndAtt
ed430 72 69 62 75 74 65 73 20 3d 20 30 3b 0a 23 69 66  ributes = 0;.#if
ed440 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ed450 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 20 3d 20  .  int isTemp = 
ed460 30 3b 0a 23 65 6e 64 69 66 0a 20 20 77 69 6e 46  0;.#endif.  winF
ed470 69 6c 65 20 2a 70 46 69 6c 65 20 3d 20 28 77 69  ile *pFile = (wi
ed480 6e 46 69 6c 65 2a 29 69 64 3b 0a 20 20 76 6f 69  nFile*)id;.  voi
ed490 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 3b 20 20  d *zConverted;  
ed4a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ed4b0 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20 4f 53  * Filename in OS
ed4c0 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 63   encoding */.  c
ed4d0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 55 74 66 38  onst char *zUtf8
ed4e0 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 20 20 20  Name = zName;   
ed4f0 20 2f 2a 20 46 69 6c 65 6e 61 6d 65 20 69 6e 20   /* Filename in 
ed500 55 54 46 2d 38 20 65 6e 63 6f 64 69 6e 67 20 2a  UTF-8 encoding *
ed510 2f 0a 20 20 63 68 61 72 20 7a 54 6d 70 6e 61 6d  /.  char zTmpnam
ed520 65 5b 4d 41 58 5f 50 41 54 48 2b 31 5d 3b 20 20  e[MAX_PATH+1];  
ed530 20 20 20 20 20 20 2f 2a 20 42 75 66 66 65 72 20        /* Buffer 
ed540 75 73 65 64 20 74 6f 20 63 72 65 61 74 65 20 74  used to create t
ed550 65 6d 70 20 66 69 6c 65 6e 61 6d 65 20 2a 2f 0a  emp filename */.
ed560 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65 63  .  /* If the sec
ed570 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 74 6f 20  ond argument to 
ed580 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  this function is
ed590 20 4e 55 4c 4c 2c 20 67 65 6e 65 72 61 74 65 20   NULL, generate 
ed5a0 61 20 0a 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72  a .  ** temporar
ed5b0 79 20 66 69 6c 65 20 6e 61 6d 65 20 74 6f 20 75  y file name to u
ed5c0 73 65 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  se .  */.  if( !
ed5d0 7a 55 74 66 38 4e 61 6d 65 20 29 7b 0a 20 20 20  zUtf8Name ){.   
ed5e0 20 69 6e 74 20 72 63 20 3d 20 67 65 74 54 65 6d   int rc = getTem
ed5f0 70 6e 61 6d 65 28 4d 41 58 5f 50 41 54 48 2b 31  pname(MAX_PATH+1
ed600 2c 20 7a 54 6d 70 6e 61 6d 65 29 3b 0a 20 20 20  , zTmpname);.   
ed610 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
ed620 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
ed630 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
ed640 20 7a 55 74 66 38 4e 61 6d 65 20 3d 20 7a 54 6d   zUtf8Name = zTm
ed650 70 6e 61 6d 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  pname;.  }..  /*
ed660 20 43 6f 6e 76 65 72 74 20 74 68 65 20 66 69 6c   Convert the fil
ed670 65 6e 61 6d 65 20 74 6f 20 74 68 65 20 73 79 73  ename to the sys
ed680 74 65 6d 20 65 6e 63 6f 64 69 6e 67 2e 20 2a 2f  tem encoding. */
ed690 0a 20 20 7a 43 6f 6e 76 65 72 74 65 64 20 3d 20  .  zConverted = 
ed6a0 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c 65 6e  convertUtf8Filen
ed6b0 61 6d 65 28 7a 55 74 66 38 4e 61 6d 65 29 3b 0a  ame(zUtf8Name);.
ed6c0 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74 65 64    if( zConverted
ed6d0 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
ed6e0 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
ed6f0 20 20 7d 0a 0a 20 20 69 66 28 20 66 6c 61 67 73    }..  if( flags
ed700 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
ed710 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
ed720 64 77 44 65 73 69 72 65 64 41 63 63 65 73 73 20  dwDesiredAccess 
ed730 3d 20 47 45 4e 45 52 49 43 5f 52 45 41 44 20 7c  = GENERIC_READ |
ed740 20 47 45 4e 45 52 49 43 5f 57 52 49 54 45 3b 0a   GENERIC_WRITE;.
ed750 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 44    }else{.    dwD
ed760 65 73 69 72 65 64 41 63 63 65 73 73 20 3d 20 47  esiredAccess = G
ed770 45 4e 45 52 49 43 5f 52 45 41 44 3b 0a 20 20 7d  ENERIC_READ;.  }
ed780 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20 53  .  if( flags & S
ed790 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
ed7a0 45 20 29 7b 0a 20 20 20 20 64 77 43 72 65 61 74  E ){.    dwCreat
ed7b0 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e 20 3d  ionDisposition =
ed7c0 20 4f 50 45 4e 5f 41 4c 57 41 59 53 3b 0a 20 20   OPEN_ALWAYS;.  
ed7d0 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77 43 72 65  }else{.    dwCre
ed7e0 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69 6f 6e  ationDisposition
ed7f0 20 3d 20 4f 50 45 4e 5f 45 58 49 53 54 49 4e 47   = OPEN_EXISTING
ed800 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67  ;.  }.  if( flag
ed810 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s & SQLITE_OPEN_
ed820 4d 41 49 4e 5f 44 42 20 29 7b 0a 20 20 20 20 64  MAIN_DB ){.    d
ed830 77 53 68 61 72 65 4d 6f 64 65 20 3d 20 46 49 4c  wShareMode = FIL
ed840 45 5f 53 48 41 52 45 5f 52 45 41 44 20 7c 20 46  E_SHARE_READ | F
ed850 49 4c 45 5f 53 48 41 52 45 5f 57 52 49 54 45 3b  ILE_SHARE_WRITE;
ed860 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 77  .  }else{.    dw
ed870 53 68 61 72 65 4d 6f 64 65 20 3d 20 30 3b 0a 20  ShareMode = 0;. 
ed880 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26   }.  if( flags &
ed890 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c   SQLITE_OPEN_DEL
ed8a0 45 54 45 4f 4e 43 4c 4f 53 45 20 29 7b 0a 23 69  ETEONCLOSE ){.#i
ed8b0 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43  f SQLITE_OS_WINC
ed8c0 45 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64  E.    dwFlagsAnd
ed8d0 41 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c  Attributes = FIL
ed8e0 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49 44 44  E_ATTRIBUTE_HIDD
ed8f0 45 4e 3b 0a 20 20 20 20 69 73 54 65 6d 70 20 3d  EN;.    isTemp =
ed900 20 31 3b 0a 23 65 6c 73 65 0a 20 20 20 20 64 77   1;.#else.    dw
ed910 46 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74  FlagsAndAttribut
ed920 65 73 20 3d 20 46 49 4c 45 5f 41 54 54 52 49 42  es = FILE_ATTRIB
ed930 55 54 45 5f 54 45 4d 50 4f 52 41 52 59 0a 20 20  UTE_TEMPORARY.  
ed940 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ed950 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 46               | F
ed960 49 4c 45 5f 41 54 54 52 49 42 55 54 45 5f 48 49  ILE_ATTRIBUTE_HI
ed970 44 44 45 4e 0a 20 20 20 20 20 20 20 20 20 20 20  DDEN.           
ed980 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ed990 20 20 20 20 7c 20 46 49 4c 45 5f 46 4c 41 47 5f      | FILE_FLAG_
ed9a0 44 45 4c 45 54 45 5f 4f 4e 5f 43 4c 4f 53 45 3b  DELETE_ON_CLOSE;
ed9b0 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b  .#endif.  }else{
ed9c0 0a 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41  .    dwFlagsAndA
ed9d0 74 74 72 69 62 75 74 65 73 20 3d 20 46 49 4c 45  ttributes = FILE
ed9e0 5f 41 54 54 52 49 42 55 54 45 5f 4e 4f 52 4d 41  _ATTRIBUTE_NORMA
ed9f0 4c 3b 0a 20 20 7d 0a 20 20 2f 2a 20 52 65 70 6f  L;.  }.  /* Repo
eda00 72 74 73 20 66 72 6f 6d 20 74 68 65 20 69 6e 74  rts from the int
eda10 65 72 6e 65 74 20 61 72 65 20 74 68 61 74 20 70  ernet are that p
eda20 65 72 66 6f 72 6d 61 6e 63 65 20 69 73 20 61 6c  erformance is al
eda30 77 61 79 73 0a 20 20 2a 2a 20 62 65 74 74 65 72  ways.  ** better
eda40 20 69 66 20 46 49 4c 45 5f 46 4c 41 47 5f 52 41   if FILE_FLAG_RA
eda50 4e 44 4f 4d 5f 41 43 43 45 53 53 20 69 73 20 75  NDOM_ACCESS is u
eda60 73 65 64 2e 20 20 54 69 63 6b 65 74 20 23 32 36  sed.  Ticket #26
eda70 39 39 2e 20 2a 2f 0a 23 69 66 20 53 51 4c 49 54  99. */.#if SQLIT
eda80 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 64 77 46  E_OS_WINCE.  dwF
eda90 6c 61 67 73 41 6e 64 41 74 74 72 69 62 75 74 65  lagsAndAttribute
edaa0 73 20 7c 3d 20 46 49 4c 45 5f 46 4c 41 47 5f 52  s |= FILE_FLAG_R
edab0 41 4e 44 4f 4d 5f 41 43 43 45 53 53 3b 0a 23 65  ANDOM_ACCESS;.#e
edac0 6e 64 69 66 0a 20 20 69 66 28 20 69 73 4e 54 28  ndif.  if( isNT(
edad0 29 20 29 7b 0a 20 20 20 20 68 20 3d 20 43 72 65  ) ){.    h = Cre
edae0 61 74 65 46 69 6c 65 57 28 28 57 43 48 41 52 2a  ateFileW((WCHAR*
edaf0 29 7a 43 6f 6e 76 65 72 74 65 64 2c 0a 20 20 20  )zConverted,.   
edb00 20 20 20 20 64 77 44 65 73 69 72 65 64 41 63 63      dwDesiredAcc
edb10 65 73 73 2c 0a 20 20 20 20 20 20 20 64 77 53 68  ess,.       dwSh
edb20 61 72 65 4d 6f 64 65 2c 0a 20 20 20 20 20 20 20  areMode,.       
edb30 4e 55 4c 4c 2c 0a 20 20 20 20 20 20 20 64 77 43  NULL,.       dwC
edb40 72 65 61 74 69 6f 6e 44 69 73 70 6f 73 69 74 69  reationDispositi
edb50 6f 6e 2c 0a 20 20 20 20 20 20 20 64 77 46 6c 61  on,.       dwFla
edb60 67 73 41 6e 64 41 74 74 72 69 62 75 74 65 73 2c  gsAndAttributes,
edb70 0a 20 20 20 20 20 20 20 4e 55 4c 4c 0a 20 20 20  .       NULL.   
edb80 20 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73   );./* isNT() is
edb90 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f   1 if SQLITE_OS_
edba0 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69  WINCE==1, so thi
edbb0 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20  s else is never 
edbc0 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69  executed. .** Si
edbd0 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65  nce the ASCII ve
edbe0 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57  rsion of these W
edbf0 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f  indows API do no
edc00 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43  t exist for WINC
edc10 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72  E,.** it's impor
edc20 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65  tant to not refe
edc30 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57  rence them for W
edc40 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a  INCE builds..*/.
edc50 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49  #if SQLITE_OS_WI
edc60 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a  NCE==0.  }else{.
edc70 20 20 20 20 68 20 3d 20 43 72 65 61 74 65 46 69      h = CreateFi
edc80 6c 65 41 28 28 63 68 61 72 2a 29 7a 43 6f 6e 76  leA((char*)zConv
edc90 65 72 74 65 64 2c 0a 20 20 20 20 20 20 20 64 77  erted,.       dw
edca0 44 65 73 69 72 65 64 41 63 63 65 73 73 2c 0a 20  DesiredAccess,. 
edcb0 20 20 20 20 20 20 64 77 53 68 61 72 65 4d 6f 64        dwShareMod
edcc0 65 2c 0a 20 20 20 20 20 20 20 4e 55 4c 4c 2c 0a  e,.       NULL,.
edcd0 20 20 20 20 20 20 20 64 77 43 72 65 61 74 69 6f         dwCreatio
edce0 6e 44 69 73 70 6f 73 69 74 69 6f 6e 2c 0a 20 20  nDisposition,.  
edcf0 20 20 20 20 20 64 77 46 6c 61 67 73 41 6e 64 41       dwFlagsAndA
edd00 74 74 72 69 62 75 74 65 73 2c 0a 20 20 20 20 20  ttributes,.     
edd10 20 20 4e 55 4c 4c 0a 20 20 20 20 29 3b 0a 23 65    NULL.    );.#e
edd20 6e 64 69 66 0a 20 20 7d 0a 20 20 69 66 28 20 68  ndif.  }.  if( h
edd30 3d 3d 49 4e 56 41 4c 49 44 5f 48 41 4e 44 4c 45  ==INVALID_HANDLE
edd40 5f 56 41 4c 55 45 20 29 7b 0a 20 20 20 20 66 72  _VALUE ){.    fr
edd50 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
edd60 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
edd70 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
edd80 57 52 49 54 45 20 29 7b 0a 20 20 20 20 20 20 72  WRITE ){.      r
edd90 65 74 75 72 6e 20 77 69 6e 4f 70 65 6e 28 30 2c  eturn winOpen(0,
edda0 20 7a 4e 61 6d 65 2c 20 69 64 2c 20 0a 20 20 20   zName, id, .   
eddb0 20 20 20 20 20 20 20 20 20 20 28 28 66 6c 61 67            ((flag
eddc0 73 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45  s|SQLITE_OPEN_RE
eddd0 41 44 4f 4e 4c 59 29 26 7e 53 51 4c 49 54 45 5f  ADONLY)&~SQLITE_
edde0 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45 29 2c  OPEN_READWRITE),
eddf0 20 70 4f 75 74 46 6c 61 67 73 29 3b 0a 20 20 20   pOutFlags);.   
ede00 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
ede10 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 41 4e 54  turn SQLITE_CANT
ede20 4f 50 45 4e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  OPEN;.    }.  }.
ede30 20 20 69 66 28 20 70 4f 75 74 46 6c 61 67 73 20    if( pOutFlags 
ede40 29 7b 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73  ){.    if( flags
ede50 20 26 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52   & SQLITE_OPEN_R
ede60 45 41 44 57 52 49 54 45 20 29 7b 0a 20 20 20 20  EADWRITE ){.    
ede70 20 20 2a 70 4f 75 74 46 6c 61 67 73 20 3d 20 53    *pOutFlags = S
ede80 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57  QLITE_OPEN_READW
ede90 52 49 54 45 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  RITE;.    }else{
edea0 0a 20 20 20 20 20 20 2a 70 4f 75 74 46 6c 61 67  .      *pOutFlag
edeb0 73 20 3d 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f  s = SQLITE_OPEN_
edec0 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 7d 0a  READONLY;.    }.
eded0 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 46 69    }.  memset(pFi
edee0 6c 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70  le, 0, sizeof(*p
edef0 46 69 6c 65 29 29 3b 0a 20 20 70 46 69 6c 65 2d  File));.  pFile-
edf00 3e 70 4d 65 74 68 6f 64 20 3d 20 26 77 69 6e 49  >pMethod = &winI
edf10 6f 4d 65 74 68 6f 64 3b 0a 20 20 70 46 69 6c 65  oMethod;.  pFile
edf20 2d 3e 68 20 3d 20 68 3b 0a 23 69 66 20 53 51 4c  ->h = h;.#if SQL
edf30 49 54 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 69  ITE_OS_WINCE.  i
edf40 66 28 20 28 66 6c 61 67 73 20 26 20 28 53 51 4c  f( (flags & (SQL
edf50 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49  ITE_OPEN_READWRI
edf60 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d  TE|SQLITE_OPEN_M
edf70 41 49 4e 5f 44 42 29 29 20 3d 3d 0a 20 20 20 20  AIN_DB)) ==.    
edf80 20 20 20 20 20 20 20 20 20 20 20 28 53 51 4c 49             (SQLI
edf90 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54  TE_OPEN_READWRIT
edfa0 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41  E|SQLITE_OPEN_MA
edfb0 49 4e 5f 44 42 29 0a 20 20 20 20 20 20 20 26 26  IN_DB).       &&
edfc0 20 21 77 69 6e 63 65 43 72 65 61 74 65 4c 6f 63   !winceCreateLoc
edfd0 6b 28 7a 4e 61 6d 65 2c 20 70 46 69 6c 65 29 0a  k(zName, pFile).
edfe0 20 20 29 7b 0a 20 20 20 20 43 6c 6f 73 65 48 61    ){.    CloseHa
edff0 6e 64 6c 65 28 68 29 3b 0a 20 20 20 20 66 72 65  ndle(h);.    fre
ee000 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ee010 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
ee020 5f 43 41 4e 54 4f 50 45 4e 3b 0a 20 20 7d 0a 20  _CANTOPEN;.  }. 
ee030 20 69 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20   if( isTemp ){. 
ee040 20 20 20 70 46 69 6c 65 2d 3e 7a 44 65 6c 65 74     pFile->zDelet
ee050 65 4f 6e 43 6c 6f 73 65 20 3d 20 7a 43 6f 6e 76  eOnClose = zConv
ee060 65 72 74 65 64 3b 0a 20 20 7d 65 6c 73 65 0a 23  erted;.  }else.#
ee070 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20 20 66 72  endif.  {.    fr
ee080 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
ee090 20 20 7d 0a 20 20 4f 70 65 6e 43 6f 75 6e 74 65    }.  OpenCounte
ee0a0 72 28 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  r(+1);.  return 
ee0b0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
ee0c0 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20 6e  .** Delete the n
ee0d0 61 6d 65 64 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a  amed file..**.**
ee0e0 20 4e 6f 74 65 20 74 68 61 74 20 77 69 6e 64 6f   Note that windo
ee0f0 77 73 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f  ws does not allo
ee100 77 20 61 20 66 69 6c 65 20 74 6f 20 62 65 20 64  w a file to be d
ee110 65 6c 65 74 65 64 20 69 66 20 73 6f 6d 65 20 6f  eleted if some o
ee120 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65 73 73 20  ther.** process 
ee130 68 61 73 20 69 74 20 6f 70 65 6e 2e 20 20 53 6f  has it open.  So
ee140 6d 65 74 69 6d 65 73 20 61 20 76 69 72 75 73 20  metimes a virus 
ee150 73 63 61 6e 6e 65 72 20 6f 72 20 69 6e 64 65 78  scanner or index
ee160 69 6e 67 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 77  ing program.** w
ee170 69 6c 6c 20 6f 70 65 6e 20 61 20 6a 6f 75 72 6e  ill open a journ
ee180 61 6c 20 66 69 6c 65 20 73 68 6f 72 74 6c 79 20  al file shortly 
ee190 61 66 74 65 72 20 69 74 20 69 73 20 63 72 65 61  after it is crea
ee1a0 74 65 64 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  ted in order to 
ee1b0 64 6f 0a 2a 2a 20 77 68 61 74 65 76 65 72 20 69  do.** whatever i
ee1c0 74 20 64 6f 65 73 2e 20 20 57 68 69 6c 65 20 74  t does.  While t
ee1d0 68 69 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73  his other proces
ee1e0 73 20 69 73 20 68 6f 6c 64 69 6e 67 20 74 68 65  s is holding the
ee1f0 0a 2a 2a 20 66 69 6c 65 20 6f 70 65 6e 2c 20 77  .** file open, w
ee200 65 20 77 69 6c 6c 20 62 65 20 75 6e 61 62 6c 65  e will be unable
ee210 20 74 6f 20 64 65 6c 65 74 65 20 69 74 2e 20 20   to delete it.  
ee220 54 6f 20 77 6f 72 6b 20 61 72 6f 75 6e 64 20 74  To work around t
ee230 68 69 73 0a 2a 2a 20 70 72 6f 62 6c 65 6d 2c 20  his.** problem, 
ee240 77 65 20 64 65 6c 61 79 20 31 30 30 20 6d 69 6c  we delay 100 mil
ee250 6c 69 73 65 63 6f 6e 64 73 20 61 6e 64 20 74 72  liseconds and tr
ee260 79 20 74 6f 20 64 65 6c 65 74 65 20 61 67 61 69  y to delete agai
ee270 6e 2e 20 20 55 70 0a 2a 2a 20 74 6f 20 4d 58 5f  n.  Up.** to MX_
ee280 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d 50 54  DELETION_ATTEMPT
ee290 73 20 64 65 6c 65 74 69 6f 6e 20 61 74 74 65 6d  s deletion attem
ee2a0 70 74 73 20 61 72 65 20 72 75 6e 20 62 65 66 6f  pts are run befo
ee2b0 72 65 20 67 69 76 69 6e 67 0a 2a 2a 20 75 70 20  re giving.** up 
ee2c0 61 6e 64 20 72 65 74 75 72 6e 69 6e 67 20 61 6e  and returning an
ee2d0 20 65 72 72 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69   error..*/.#defi
ee2e0 6e 65 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f 41  ne MX_DELETION_A
ee2f0 54 54 45 4d 50 54 53 20 35 0a 73 74 61 74 69 63  TTEMPTS 5.static
ee300 20 69 6e 74 20 77 69 6e 44 65 6c 65 74 65 28 0a   int winDelete(.
ee310 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70    sqlite3_vfs *p
ee320 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Vfs,          /*
ee330 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69 6e   Not used on win
ee340 33 32 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  32 */.  const ch
ee350 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20 20  ar *zFilename,  
ee360 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 66      /* Name of f
ee370 69 6c 65 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  ile to delete */
ee380 0a 20 20 69 6e 74 20 73 79 6e 63 44 69 72 20 20  .  int syncDir  
ee390 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ee3a0 2a 20 4e 6f 74 20 75 73 65 64 20 6f 6e 20 77 69  * Not used on wi
ee3b0 6e 33 32 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  n32 */.){.  int 
ee3c0 63 6e 74 20 3d 20 30 3b 0a 20 20 44 57 4f 52 44  cnt = 0;.  DWORD
ee3d0 20 72 63 3b 0a 20 20 44 57 4f 52 44 20 65 72 72   rc;.  DWORD err
ee3e0 6f 72 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e  or;.  void *zCon
ee3f0 76 65 72 74 65 64 20 3d 20 63 6f 6e 76 65 72 74  verted = convert
ee400 55 74 66 38 46 69 6c 65 6e 61 6d 65 28 7a 46 69  Utf8Filename(zFi
ee410 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a  lename);.  if( z
ee420 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20 29 7b 0a  Converted==0 ){.
ee430 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
ee440 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 53  E_NOMEM;.  }.  S
ee450 69 6d 75 6c 61 74 65 49 4f 45 72 72 6f 72 28 72  imulateIOError(r
ee460 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49 4f 45  eturn SQLITE_IOE
ee470 52 52 5f 44 45 4c 45 54 45 29 3b 0a 20 20 69 66  RR_DELETE);.  if
ee480 28 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20  ( isNT() ){.    
ee490 64 6f 7b 0a 20 20 20 20 20 20 44 65 6c 65 74 65  do{.      Delete
ee4a0 46 69 6c 65 57 28 7a 43 6f 6e 76 65 72 74 65 64  FileW(zConverted
ee4b0 29 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28 20 20  );.    }while(  
ee4c0 20 28 20 20 20 28 28 72 63 20 3d 20 47 65 74 46   (   ((rc = GetF
ee4d0 69 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 7a  ileAttributesW(z
ee4e0 43 6f 6e 76 65 72 74 65 64 29 29 20 21 3d 20 49  Converted)) != I
ee4f0 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54 52  NVALID_FILE_ATTR
ee500 49 42 55 54 45 53 29 0a 20 20 20 20 20 20 20 20  IBUTES).        
ee510 20 20 20 20 20 20 20 7c 7c 20 28 28 65 72 72 6f         || ((erro
ee520 72 20 3d 20 47 65 74 4c 61 73 74 45 72 72 6f 72  r = GetLastError
ee530 28 29 29 20 3d 3d 20 45 52 52 4f 52 5f 41 43 43  ()) == ERROR_ACC
ee540 45 53 53 5f 44 45 4e 49 45 44 29 29 0a 20 20 20  ESS_DENIED)).   
ee550 20 20 20 20 20 20 20 20 26 26 20 28 2b 2b 63 6e          && (++cn
ee560 74 20 3c 20 4d 58 5f 44 45 4c 45 54 49 4f 4e 5f  t < MX_DELETION_
ee570 41 54 54 45 4d 50 54 53 29 0a 20 20 20 20 20 20  ATTEMPTS).      
ee580 20 20 20 20 20 26 26 20 28 53 6c 65 65 70 28 31       && (Sleep(1
ee590 30 30 29 2c 20 31 29 20 29 3b 0a 2f 2a 20 69 73  00), 1) );./* is
ee5a0 4e 54 28 29 20 69 73 20 31 20 69 66 20 53 51 4c  NT() is 1 if SQL
ee5b0 49 54 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 31 2c  ITE_OS_WINCE==1,
ee5c0 20 73 6f 20 74 68 69 73 20 65 6c 73 65 20 69 73   so this else is
ee5d0 20 6e 65 76 65 72 20 65 78 65 63 75 74 65 64 2e   never executed.
ee5e0 20 0a 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 41   .** Since the A
ee5f0 53 43 49 49 20 76 65 72 73 69 6f 6e 20 6f 66 20  SCII version of 
ee600 74 68 65 73 65 20 57 69 6e 64 6f 77 73 20 41 50  these Windows AP
ee610 49 20 64 6f 20 6e 6f 74 20 65 78 69 73 74 20 66  I do not exist f
ee620 6f 72 20 57 49 4e 43 45 2c 0a 2a 2a 20 69 74 27  or WINCE,.** it'
ee630 73 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6e  s important to n
ee640 6f 74 20 72 65 66 65 72 65 6e 63 65 20 74 68 65  ot reference the
ee650 6d 20 66 6f 72 20 57 49 4e 43 45 20 62 75 69 6c  m for WINCE buil
ee660 64 73 2e 0a 2a 2f 0a 23 69 66 20 53 51 4c 49 54  ds..*/.#if SQLIT
ee670 45 5f 4f 53 5f 57 49 4e 43 45 3d 3d 30 0a 20 20  E_OS_WINCE==0.  
ee680 7d 65 6c 73 65 7b 0a 20 20 20 20 64 6f 7b 0a 20  }else{.    do{. 
ee690 20 20 20 20 20 44 65 6c 65 74 65 46 69 6c 65 41       DeleteFileA
ee6a0 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20 20  (zConverted);.  
ee6b0 20 20 7d 77 68 69 6c 65 28 20 20 20 28 20 20 20    }while(   (   
ee6c0 28 28 72 63 20 3d 20 47 65 74 46 69 6c 65 41 74  ((rc = GetFileAt
ee6d0 74 72 69 62 75 74 65 73 41 28 7a 43 6f 6e 76 65  tributesA(zConve
ee6e0 72 74 65 64 29 29 20 21 3d 20 49 4e 56 41 4c 49  rted)) != INVALI
ee6f0 44 5f 46 49 4c 45 5f 41 54 54 52 49 42 55 54 45  D_FILE_ATTRIBUTE
ee700 53 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  S).             
ee710 20 20 7c 7c 20 28 28 65 72 72 6f 72 20 3d 20 47    || ((error = G
ee720 65 74 4c 61 73 74 45 72 72 6f 72 28 29 29 20 3d  etLastError()) =
ee730 3d 20 45 52 52 4f 52 5f 41 43 43 45 53 53 5f 44  = ERROR_ACCESS_D
ee740 45 4e 49 45 44 29 29 0a 20 20 20 20 20 20 20 20  ENIED)).        
ee750 20 20 20 26 26 20 28 2b 2b 63 6e 74 20 3c 20 4d     && (++cnt < M
ee760 58 5f 44 45 4c 45 54 49 4f 4e 5f 41 54 54 45 4d  X_DELETION_ATTEM
ee770 50 54 53 29 0a 20 20 20 20 20 20 20 20 20 20 20  PTS).           
ee780 26 26 20 28 53 6c 65 65 70 28 31 30 30 29 2c 20  && (Sleep(100), 
ee790 31 29 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 7d  1) );.#endif.  }
ee7a0 0a 20 20 66 72 65 65 28 7a 43 6f 6e 76 65 72 74  .  free(zConvert
ee7b0 65 64 29 3b 0a 20 20 4f 53 54 52 41 43 45 32 28  ed);.  OSTRACE2(
ee7c0 22 44 45 4c 45 54 45 20 5c 22 25 73 5c 22 5c 6e  "DELETE \"%s\"\n
ee7d0 22 2c 20 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  ", zFilename);. 
ee7e0 20 72 65 74 75 72 6e 20 28 20 20 20 28 72 63 20   return (   (rc 
ee7f0 3d 3d 20 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f  == INVALID_FILE_
ee800 41 54 54 52 49 42 55 54 45 53 29 20 0a 20 20 20  ATTRIBUTES) .   
ee810 20 20 20 20 20 20 20 26 26 20 28 65 72 72 6f 72         && (error
ee820 20 3d 3d 20 45 52 52 4f 52 5f 46 49 4c 45 5f 4e   == ERROR_FILE_N
ee830 4f 54 5f 46 4f 55 4e 44 29 29 20 3f 20 53 51 4c  OT_FOUND)) ? SQL
ee840 49 54 45 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f  ITE_OK : SQLITE_
ee850 49 4f 45 52 52 5f 44 45 4c 45 54 45 3b 0a 7d 0a  IOERR_DELETE;.}.
ee860 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65  ./*.** Check the
ee870 20 65 78 69 73 74 61 6e 63 65 20 61 6e 64 20 73   existance and s
ee880 74 61 74 75 73 20 6f 66 20 61 20 66 69 6c 65 2e  tatus of a file.
ee890 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
ee8a0 69 6e 41 63 63 65 73 73 28 0a 20 20 73 71 6c 69  inAccess(.  sqli
ee8b0 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 20  te3_vfs *pVfs,  
ee8c0 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 75 73         /* Not us
ee8d0 65 64 20 6f 6e 20 77 69 6e 33 32 20 2a 2f 0a 20  ed on win32 */. 
ee8e0 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69   const char *zFi
ee8f0 6c 65 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e  lename,     /* N
ee900 61 6d 65 20 6f 66 20 66 69 6c 65 20 74 6f 20 63  ame of file to c
ee910 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 66 6c  heck */.  int fl
ee920 61 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20  ags,            
ee930 20 20 20 20 20 2f 2a 20 54 79 70 65 20 6f 66 20       /* Type of 
ee940 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 6f 6e 20  test to make on 
ee950 74 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 69  this file */.  i
ee960 6e 74 20 2a 70 52 65 73 4f 75 74 20 20 20 20 20  nt *pResOut     
ee970 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
ee980 3a 20 52 65 73 75 6c 74 20 2a 2f 0a 29 7b 0a 20  : Result */.){. 
ee990 20 44 57 4f 52 44 20 61 74 74 72 3b 0a 20 20 69   DWORD attr;.  i
ee9a0 6e 74 20 72 63 3b 0a 20 20 76 6f 69 64 20 2a 7a  nt rc;.  void *z
ee9b0 43 6f 6e 76 65 72 74 65 64 20 3d 20 63 6f 6e 76  Converted = conv
ee9c0 65 72 74 55 74 66 38 46 69 6c 65 6e 61 6d 65 28  ertUtf8Filename(
ee9d0 7a 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 69 66  zFilename);.  if
ee9e0 28 20 7a 43 6f 6e 76 65 72 74 65 64 3d 3d 30 20  ( zConverted==0 
ee9f0 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
eea00 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
eea10 20 20 69 66 28 20 69 73 4e 54 28 29 20 29 7b 0a    if( isNT() ){.
eea20 20 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69      attr = GetFi
eea30 6c 65 41 74 74 72 69 62 75 74 65 73 57 28 28 57  leAttributesW((W
eea40 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74 65 64  CHAR*)zConverted
eea50 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
eea60 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
eea70 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
eea80 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
eea90 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
eeaa0 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
eeab0 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
eeac0 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
eead0 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
eeae0 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
eeaf0 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
eeb00 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
eeb10 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
eeb20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
eeb30 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
eeb40 20 20 20 61 74 74 72 20 3d 20 47 65 74 46 69 6c     attr = GetFil
eeb50 65 41 74 74 72 69 62 75 74 65 73 41 28 28 63 68  eAttributesA((ch
eeb60 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ar*)zConverted);
eeb70 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 66 72  .#endif.  }.  fr
eeb80 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a  ee(zConverted);.
eeb90 20 20 73 77 69 74 63 68 28 20 66 6c 61 67 73 20    switch( flags 
eeba0 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ){.    case SQLI
eebb0 54 45 5f 41 43 43 45 53 53 5f 52 45 41 44 3a 0a  TE_ACCESS_READ:.
eebc0 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
eebd0 41 43 43 45 53 53 5f 45 58 49 53 54 53 3a 0a 20  ACCESS_EXISTS:. 
eebe0 20 20 20 20 20 72 63 20 3d 20 61 74 74 72 21 3d       rc = attr!=
eebf0 49 4e 56 41 4c 49 44 5f 46 49 4c 45 5f 41 54 54  INVALID_FILE_ATT
eec00 52 49 42 55 54 45 53 3b 0a 20 20 20 20 20 20 62  RIBUTES;.      b
eec10 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
eec20 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 52 45 41  QLITE_ACCESS_REA
eec30 44 57 52 49 54 45 3a 0a 20 20 20 20 20 20 72 63  DWRITE:.      rc
eec40 20 3d 20 28 61 74 74 72 20 26 20 46 49 4c 45 5f   = (attr & FILE_
eec50 41 54 54 52 49 42 55 54 45 5f 52 45 41 44 4f 4e  ATTRIBUTE_READON
eec60 4c 59 29 3d 3d 30 3b 0a 20 20 20 20 20 20 62 72  LY)==0;.      br
eec70 65 61 6b 3b 0a 20 20 20 20 64 65 66 61 75 6c 74  eak;.    default
eec80 3a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 21  :.      assert(!
eec90 22 49 6e 76 61 6c 69 64 20 66 6c 61 67 73 20 61  "Invalid flags a
eeca0 72 67 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 0a 20  rgument");.  }. 
eecb0 20 2a 70 52 65 73 4f 75 74 20 3d 20 72 63 3b 0a   *pResOut = rc;.
eecc0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
eecd0 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 75  OK;.}.../*.** Tu
eece0 72 6e 20 61 20 72 65 6c 61 74 69 76 65 20 70 61  rn a relative pa
eecf0 74 68 6e 61 6d 65 20 69 6e 74 6f 20 61 20 66 75  thname into a fu
eed00 6c 6c 20 70 61 74 68 6e 61 6d 65 2e 20 20 57 72  ll pathname.  Wr
eed10 69 74 65 20 74 68 65 20 66 75 6c 6c 0a 2a 2a 20  ite the full.** 
eed20 70 61 74 68 6e 61 6d 65 20 69 6e 74 6f 20 7a 4f  pathname into zO
eed30 75 74 5b 5d 2e 20 20 7a 4f 75 74 5b 5d 20 77 69  ut[].  zOut[] wi
eed40 6c 6c 20 62 65 20 61 74 20 6c 65 61 73 74 20 70  ll be at least p
eed50 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 0a  Vfs->mxPathname.
eed60 2a 2a 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65  ** bytes in size
eed70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
eed80 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 28  winFullPathname(
eed90 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
eeda0 70 56 66 73 2c 20 20 20 20 20 20 20 20 20 20 20  pVfs,           
eedb0 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 76   /* Pointer to v
eedc0 66 73 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 63  fs object */.  c
eedd0 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 6c 61  onst char *zRela
eede0 74 69 76 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  tive,        /* 
eedf0 50 6f 73 73 69 62 6c 79 20 72 65 6c 61 74 69 76  Possibly relativ
eee00 65 20 69 6e 70 75 74 20 70 61 74 68 20 2a 2f 0a  e input path */.
eee10 20 20 69 6e 74 20 6e 46 75 6c 6c 2c 20 20 20 20    int nFull,    
eee20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
eee30 2f 2a 20 53 69 7a 65 20 6f 66 20 6f 75 74 70 75  /* Size of outpu
eee40 74 20 62 75 66 66 65 72 20 69 6e 20 62 79 74 65  t buffer in byte
eee50 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 75  s */.  char *zFu
eee60 6c 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ll              
eee70 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20 62       /* Output b
eee80 75 66 66 65 72 20 2a 2f 0a 29 7b 0a 0a 23 69 66  uffer */.){..#if
eee90 20 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57 49   defined(__CYGWI
eeea0 4e 5f 5f 29 0a 20 20 63 79 67 77 69 6e 5f 63 6f  N__).  cygwin_co
eeeb0 6e 76 5f 74 6f 5f 66 75 6c 6c 5f 77 69 6e 33 32  nv_to_full_win32
eeec0 5f 70 61 74 68 28 7a 52 65 6c 61 74 69 76 65 2c  _path(zRelative,
eeed0 20 7a 46 75 6c 6c 29 3b 0a 20 20 72 65 74 75 72   zFull);.  retur
eeee0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 23 65 6e  n SQLITE_OK;.#en
eeef0 64 69 66 0a 0a 23 69 66 20 53 51 4c 49 54 45 5f  dif..#if SQLITE_
eef00 4f 53 5f 57 49 4e 43 45 0a 20 20 2f 2a 20 57 69  OS_WINCE.  /* Wi
eef10 6e 43 45 20 68 61 73 20 6e 6f 20 63 6f 6e 63 65  nCE has no conce
eef20 70 74 20 6f 66 20 61 20 72 65 6c 61 74 69 76 65  pt of a relative
eef30 20 70 61 74 68 6e 61 6d 65 2c 20 6f 72 20 73 6f   pathname, or so
eef40 20 49 20 61 6d 20 74 6f 6c 64 2e 20 2a 2f 0a 20   I am told. */. 
eef50 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
eef60 66 28 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61  f(pVfs->mxPathna
eef70 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22 25 73 22 2c  me, zFull, "%s",
eef80 20 7a 52 65 6c 61 74 69 76 65 29 3b 0a 20 20 72   zRelative);.  r
eef90 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
eefa0 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 53 51  .#endif..#if !SQ
eefb0 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45 20 26 26  LITE_OS_WINCE &&
eefc0 20 21 64 65 66 69 6e 65 64 28 5f 5f 43 59 47 57   !defined(__CYGW
eefd0 49 4e 5f 5f 29 0a 20 20 69 6e 74 20 6e 42 79 74  IN__).  int nByt
eefe0 65 3b 0a 20 20 76 6f 69 64 20 2a 7a 43 6f 6e 76  e;.  void *zConv
eeff0 65 72 74 65 64 3b 0a 20 20 63 68 61 72 20 2a 7a  erted;.  char *z
ef000 4f 75 74 3b 0a 20 20 7a 43 6f 6e 76 65 72 74 65  Out;.  zConverte
ef010 64 20 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46  d = convertUtf8F
ef020 69 6c 65 6e 61 6d 65 28 7a 52 65 6c 61 74 69 76  ilename(zRelativ
ef030 65 29 3b 0a 20 20 69 66 28 20 69 73 4e 54 28 29  e);.  if( isNT()
ef040 20 29 7b 0a 20 20 20 20 57 43 48 41 52 20 2a 7a   ){.    WCHAR *z
ef050 54 65 6d 70 3b 0a 20 20 20 20 6e 42 79 74 65 20  Temp;.    nByte 
ef060 3d 20 47 65 74 46 75 6c 6c 50 61 74 68 4e 61 6d  = GetFullPathNam
ef070 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76  eW((WCHAR*)zConv
ef080 65 72 74 65 64 2c 20 30 2c 20 30 2c 20 30 29 20  erted, 0, 0, 0) 
ef090 2b 20 33 3b 0a 20 20 20 20 7a 54 65 6d 70 20 3d  + 3;.    zTemp =
ef0a0 20 6d 61 6c 6c 6f 63 28 20 6e 42 79 74 65 2a 73   malloc( nByte*s
ef0b0 69 7a 65 6f 66 28 7a 54 65 6d 70 5b 30 5d 29 20  izeof(zTemp[0]) 
ef0c0 29 3b 0a 20 20 20 20 69 66 28 20 7a 54 65 6d 70  );.    if( zTemp
ef0d0 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 72 65  ==0 ){.      fre
ef0e0 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b 0a 20  e(zConverted);. 
ef0f0 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
ef100 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
ef110 20 20 20 20 47 65 74 46 75 6c 6c 50 61 74 68 4e      GetFullPathN
ef120 61 6d 65 57 28 28 57 43 48 41 52 2a 29 7a 43 6f  ameW((WCHAR*)zCo
ef130 6e 76 65 72 74 65 64 2c 20 6e 42 79 74 65 2c 20  nverted, nByte, 
ef140 7a 54 65 6d 70 2c 20 30 29 3b 0a 20 20 20 20 66  zTemp, 0);.    f
ef150 72 65 65 28 7a 43 6f 6e 76 65 72 74 65 64 29 3b  ree(zConverted);
ef160 0a 20 20 20 20 7a 4f 75 74 20 3d 20 75 6e 69 63  .    zOut = unic
ef170 6f 64 65 54 6f 55 74 66 38 28 7a 54 65 6d 70 29  odeToUtf8(zTemp)
ef180 3b 0a 20 20 20 20 66 72 65 65 28 7a 54 65 6d 70  ;.    free(zTemp
ef190 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69 73 20  );./* isNT() is 
ef1a0 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  1 if SQLITE_OS_W
ef1b0 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68 69 73  INCE==1, so this
ef1c0 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72 20 65   else is never e
ef1d0 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53 69 6e  xecuted. .** Sin
ef1e0 63 65 20 74 68 65 20 41 53 43 49 49 20 76 65 72  ce the ASCII ver
ef1f0 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20 57 69  sion of these Wi
ef200 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e 6f 74  ndows API do not
ef210 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e 43 45   exist for WINCE
ef220 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f 72 74  ,.** it's import
ef230 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66 65 72  ant to not refer
ef240 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20 57 49  ence them for WI
ef250 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f 0a 23  NCE builds..*/.#
ef260 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e  if SQLITE_OS_WIN
ef270 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b 0a 20  CE==0.  }else{. 
ef280 20 20 20 63 68 61 72 20 2a 7a 54 65 6d 70 3b 0a     char *zTemp;.
ef290 20 20 20 20 6e 42 79 74 65 20 3d 20 47 65 74 46      nByte = GetF
ef2a0 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63 68  ullPathNameA((ch
ef2b0 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c 20  ar*)zConverted, 
ef2c0 30 2c 20 30 2c 20 30 29 20 2b 20 33 3b 0a 20 20  0, 0, 0) + 3;.  
ef2d0 20 20 7a 54 65 6d 70 20 3d 20 6d 61 6c 6c 6f 63    zTemp = malloc
ef2e0 28 20 6e 42 79 74 65 2a 73 69 7a 65 6f 66 28 7a  ( nByte*sizeof(z
ef2f0 54 65 6d 70 5b 30 5d 29 20 29 3b 0a 20 20 20 20  Temp[0]) );.    
ef300 69 66 28 20 7a 54 65 6d 70 3d 3d 30 20 29 7b 0a  if( zTemp==0 ){.
ef310 20 20 20 20 20 20 66 72 65 65 28 7a 43 6f 6e 76        free(zConv
ef320 65 72 74 65 64 29 3b 0a 20 20 20 20 20 20 72 65  erted);.      re
ef330 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
ef340 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 47 65 74  M;.    }.    Get
ef350 46 75 6c 6c 50 61 74 68 4e 61 6d 65 41 28 28 63  FullPathNameA((c
ef360 68 61 72 2a 29 7a 43 6f 6e 76 65 72 74 65 64 2c  har*)zConverted,
ef370 20 6e 42 79 74 65 2c 20 7a 54 65 6d 70 2c 20 30   nByte, zTemp, 0
ef380 29 3b 0a 20 20 20 20 66 72 65 65 28 7a 43 6f 6e  );.    free(zCon
ef390 76 65 72 74 65 64 29 3b 0a 20 20 20 20 7a 4f 75  verted);.    zOu
ef3a0 74 20 3d 20 73 71 6c 69 74 65 33 5f 77 69 6e 33  t = sqlite3_win3
ef3b0 32 5f 6d 62 63 73 5f 74 6f 5f 75 74 66 38 28 7a  2_mbcs_to_utf8(z
ef3c0 54 65 6d 70 29 3b 0a 20 20 20 20 66 72 65 65 28  Temp);.    free(
ef3d0 7a 54 65 6d 70 29 3b 0a 23 65 6e 64 69 66 0a 20  zTemp);.#endif. 
ef3e0 20 7d 0a 20 20 69 66 28 20 7a 4f 75 74 20 29 7b   }.  if( zOut ){
ef3f0 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
ef400 72 69 6e 74 66 28 70 56 66 73 2d 3e 6d 78 50 61  rintf(pVfs->mxPa
ef410 74 68 6e 61 6d 65 2c 20 7a 46 75 6c 6c 2c 20 22  thname, zFull, "
ef420 25 73 22 2c 20 7a 4f 75 74 29 3b 0a 20 20 20 20  %s", zOut);.    
ef430 66 72 65 65 28 7a 4f 75 74 29 3b 0a 20 20 20 20  free(zOut);.    
ef440 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
ef450 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
ef460 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
ef470 45 4d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d  EM;.  }.#endif.}
ef480 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
ef490 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e  _OMIT_LOAD_EXTEN
ef4a0 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  SION./*.** Inter
ef4b0 66 61 63 65 73 20 66 6f 72 20 6f 70 65 6e 69 6e  faces for openin
ef4c0 67 20 61 20 73 68 61 72 65 64 20 6c 69 62 72 61  g a shared libra
ef4d0 72 79 2c 20 66 69 6e 64 69 6e 67 20 65 6e 74 72  ry, finding entr
ef4e0 79 20 70 6f 69 6e 74 73 0a 2a 2a 20 77 69 74 68  y points.** with
ef4f0 69 6e 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  in the shared li
ef500 62 72 61 72 79 2c 20 61 6e 64 20 63 6c 6f 73 69  brary, and closi
ef510 6e 67 20 74 68 65 20 73 68 61 72 65 64 20 6c 69  ng the shared li
ef520 62 72 61 72 79 2e 0a 2a 2f 0a 2f 2a 0a 2a 2a 20  brary..*/./*.** 
ef530 49 6e 74 65 72 66 61 63 65 73 20 66 6f 72 20 6f  Interfaces for o
ef540 70 65 6e 69 6e 67 20 61 20 73 68 61 72 65 64 20  pening a shared 
ef550 6c 69 62 72 61 72 79 2c 20 66 69 6e 64 69 6e 67  library, finding
ef560 20 65 6e 74 72 79 20 70 6f 69 6e 74 73 0a 2a 2a   entry points.**
ef570 20 77 69 74 68 69 6e 20 74 68 65 20 73 68 61 72   within the shar
ef580 65 64 20 6c 69 62 72 61 72 79 2c 20 61 6e 64 20  ed library, and 
ef590 63 6c 6f 73 69 6e 67 20 74 68 65 20 73 68 61 72  closing the shar
ef5a0 65 64 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 73  ed library..*/.s
ef5b0 74 61 74 69 63 20 76 6f 69 64 20 2a 77 69 6e 44  tatic void *winD
ef5c0 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 66  lOpen(sqlite3_vf
ef5d0 73 20 2a 70 56 66 73 2c 20 63 6f 6e 73 74 20 63  s *pVfs, const c
ef5e0 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 29 7b  har *zFilename){
ef5f0 0a 20 20 48 41 4e 44 4c 45 20 68 3b 0a 20 20 76  .  HANDLE h;.  v
ef600 6f 69 64 20 2a 7a 43 6f 6e 76 65 72 74 65 64 20  oid *zConverted 
ef610 3d 20 63 6f 6e 76 65 72 74 55 74 66 38 46 69 6c  = convertUtf8Fil
ef620 65 6e 61 6d 65 28 7a 46 69 6c 65 6e 61 6d 65 29  ename(zFilename)
ef630 3b 0a 20 20 69 66 28 20 7a 43 6f 6e 76 65 72 74  ;.  if( zConvert
ef640 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  ed==0 ){.    ret
ef650 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28  urn 0;.  }.  if(
ef660 20 69 73 4e 54 28 29 20 29 7b 0a 20 20 20 20 68   isNT() ){.    h
ef670 20 3d 20 4c 6f 61 64 4c 69 62 72 61 72 79 57 28   = LoadLibraryW(
ef680 28 57 43 48 41 52 2a 29 7a 43 6f 6e 76 65 72 74  (WCHAR*)zConvert
ef690 65 64 29 3b 0a 2f 2a 20 69 73 4e 54 28 29 20 69  ed);./* isNT() i
ef6a0 73 20 31 20 69 66 20 53 51 4c 49 54 45 5f 4f 53  s 1 if SQLITE_OS
ef6b0 5f 57 49 4e 43 45 3d 3d 31 2c 20 73 6f 20 74 68  _WINCE==1, so th
ef6c0 69 73 20 65 6c 73 65 20 69 73 20 6e 65 76 65 72  is else is never
ef6d0 20 65 78 65 63 75 74 65 64 2e 20 0a 2a 2a 20 53   executed. .** S
ef6e0 69 6e 63 65 20 74 68 65 20 41 53 43 49 49 20 76  ince the ASCII v
ef6f0 65 72 73 69 6f 6e 20 6f 66 20 74 68 65 73 65 20  ersion of these 
ef700 57 69 6e 64 6f 77 73 20 41 50 49 20 64 6f 20 6e  Windows API do n
ef710 6f 74 20 65 78 69 73 74 20 66 6f 72 20 57 49 4e  ot exist for WIN
ef720 43 45 2c 0a 2a 2a 20 69 74 27 73 20 69 6d 70 6f  CE,.** it's impo
ef730 72 74 61 6e 74 20 74 6f 20 6e 6f 74 20 72 65 66  rtant to not ref
ef740 65 72 65 6e 63 65 20 74 68 65 6d 20 66 6f 72 20  erence them for 
ef750 57 49 4e 43 45 20 62 75 69 6c 64 73 2e 0a 2a 2f  WINCE builds..*/
ef760 0a 23 69 66 20 53 51 4c 49 54 45 5f 4f 53 5f 57  .#if SQLITE_OS_W
ef770 49 4e 43 45 3d 3d 30 0a 20 20 7d 65 6c 73 65 7b  INCE==0.  }else{
ef780 0a 20 20 20 20 68 20 3d 20 4c 6f 61 64 4c 69 62  .    h = LoadLib
ef790 72 61 72 79 41 28 28 63 68 61 72 2a 29 7a 43 6f  raryA((char*)zCo
ef7a0 6e 76 65 72 74 65 64 29 3b 0a 23 65 6e 64 69 66  nverted);.#endif
ef7b0 0a 20 20 7d 0a 20 20 66 72 65 65 28 7a 43 6f 6e  .  }.  free(zCon
ef7c0 76 65 72 74 65 64 29 3b 0a 20 20 72 65 74 75 72  verted);.  retur
ef7d0 6e 20 28 76 6f 69 64 2a 29 68 3b 0a 7d 0a 73 74  n (void*)h;.}.st
ef7e0 61 74 69 63 20 76 6f 69 64 20 77 69 6e 44 6c 45  atic void winDlE
ef7f0 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 76 66 73  rror(sqlite3_vfs
ef800 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75 66   *pVfs, int nBuf
ef810 2c 20 63 68 61 72 20 2a 7a 42 75 66 4f 75 74 29  , char *zBufOut)
ef820 7b 0a 20 20 67 65 74 4c 61 73 74 45 72 72 6f 72  {.  getLastError
ef830 4d 73 67 28 6e 42 75 66 2c 20 7a 42 75 66 4f 75  Msg(nBuf, zBufOu
ef840 74 29 3b 0a 7d 0a 76 6f 69 64 20 2a 77 69 6e 44  t);.}.void *winD
ef850 6c 53 79 6d 28 73 71 6c 69 74 65 33 5f 76 66 73  lSym(sqlite3_vfs
ef860 20 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48   *pVfs, void *pH
ef870 61 6e 64 6c 65 2c 20 63 6f 6e 73 74 20 63 68 61  andle, const cha
ef880 72 20 2a 7a 53 79 6d 62 6f 6c 29 7b 0a 23 69 66  r *zSymbol){.#if
ef890 20 53 51 4c 49 54 45 5f 4f 53 5f 57 49 4e 43 45   SQLITE_OS_WINCE
ef8a0 0a 20 20 2f 2a 20 54 68 65 20 47 65 74 50 72 6f  .  /* The GetPro
ef8b0 63 41 64 64 72 65 73 73 41 28 29 20 72 6f 75 74  cAddressA() rout
ef8c0 69 6e 65 20 69 73 20 6f 6e 6c 79 20 61 76 61 69  ine is only avai
ef8d0 6c 61 62 6c 65 20 6f 6e 20 77 69 6e 63 65 2e 20  lable on wince. 
ef8e0 2a 2f 0a 20 20 72 65 74 75 72 6e 20 47 65 74 50  */.  return GetP
ef8f0 72 6f 63 41 64 64 72 65 73 73 41 28 28 48 41 4e  rocAddressA((HAN
ef900 44 4c 45 29 70 48 61 6e 64 6c 65 2c 20 7a 53 79  DLE)pHandle, zSy
ef910 6d 62 6f 6c 29 3b 0a 23 65 6c 73 65 0a 20 20 2f  mbol);.#else.  /
ef920 2a 20 41 6c 6c 20 6f 74 68 65 72 20 77 69 6e 64  * All other wind
ef930 6f 77 73 20 70 6c 61 74 66 6f 72 6d 73 20 65 78  ows platforms ex
ef940 70 65 63 74 20 47 65 74 50 72 6f 63 41 64 64 72  pect GetProcAddr
ef950 65 73 73 28 29 20 74 6f 20 74 61 6b 65 0a 20 20  ess() to take.  
ef960 2a 2a 20 61 6e 20 41 6e 73 69 20 73 74 72 69 6e  ** an Ansi strin
ef970 67 20 72 65 67 61 72 64 6c 65 73 73 20 6f 66 20  g regardless of 
ef980 74 68 65 20 5f 55 4e 49 43 4f 44 45 20 73 65 74  the _UNICODE set
ef990 74 69 6e 67 20 2a 2f 0a 20 20 72 65 74 75 72 6e  ting */.  return
ef9a0 20 47 65 74 50 72 6f 63 41 64 64 72 65 73 73 28   GetProcAddress(
ef9b0 28 48 41 4e 44 4c 45 29 70 48 61 6e 64 6c 65 2c  (HANDLE)pHandle,
ef9c0 20 7a 53 79 6d 62 6f 6c 29 3b 0a 23 65 6e 64 69   zSymbol);.#endi
ef9d0 66 0a 7d 0a 76 6f 69 64 20 77 69 6e 44 6c 43 6c  f.}.void winDlCl
ef9e0 6f 73 65 28 73 71 6c 69 74 65 33 5f 76 66 73 20  ose(sqlite3_vfs 
ef9f0 2a 70 56 66 73 2c 20 76 6f 69 64 20 2a 70 48 61  *pVfs, void *pHa
efa00 6e 64 6c 65 29 7b 0a 20 20 46 72 65 65 4c 69 62  ndle){.  FreeLib
efa10 72 61 72 79 28 28 48 41 4e 44 4c 45 29 70 48 61  rary((HANDLE)pHa
efa20 6e 64 6c 65 29 3b 0a 7d 0a 23 65 6c 73 65 20 2f  ndle);.}.#else /
efa30 2a 20 69 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  * if SQLITE_OMIT
efa40 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20  _LOAD_EXTENSION 
efa50 69 73 20 64 65 66 69 6e 65 64 3a 20 2a 2f 0a 20  is defined: */. 
efa60 20 23 64 65 66 69 6e 65 20 77 69 6e 44 6c 4f 70   #define winDlOp
efa70 65 6e 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20  en  0.  #define 
efa80 77 69 6e 44 6c 45 72 72 6f 72 20 30 0a 20 20 23  winDlError 0.  #
efa90 64 65 66 69 6e 65 20 77 69 6e 44 6c 53 79 6d 20  define winDlSym 
efaa0 20 20 30 0a 20 20 23 64 65 66 69 6e 65 20 77 69    0.  #define wi
efab0 6e 44 6c 43 6c 6f 73 65 20 30 0a 23 65 6e 64 69  nDlClose 0.#endi
efac0 66 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  f.../*.** Write 
efad0 75 70 20 74 6f 20 6e 42 75 66 20 62 79 74 65 73  up to nBuf bytes
efae0 20 6f 66 20 72 61 6e 64 6f 6d 6e 65 73 73 20 69   of randomness i
efaf0 6e 74 6f 20 7a 42 75 66 2e 0a 2a 2f 0a 73 74 61  nto zBuf..*/.sta
efb00 74 69 63 20 69 6e 74 20 77 69 6e 52 61 6e 64 6f  tic int winRando
efb10 6d 6e 65 73 73 28 73 71 6c 69 74 65 33 5f 76 66  mness(sqlite3_vf
efb20 73 20 2a 70 56 66 73 2c 20 69 6e 74 20 6e 42 75  s *pVfs, int nBu
efb30 66 2c 20 63 68 61 72 20 2a 7a 42 75 66 29 7b 0a  f, char *zBuf){.
efb40 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 20 20 55    int n = 0;.  U
efb50 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28  NUSED_PARAMETER(
efb60 70 56 66 73 29 3b 0a 23 69 66 20 64 65 66 69 6e  pVfs);.#if defin
efb70 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 0a  ed(SQLITE_TEST).
efb80 20 20 6e 20 3d 20 6e 42 75 66 3b 0a 20 20 6d 65    n = nBuf;.  me
efb90 6d 73 65 74 28 7a 42 75 66 2c 20 30 2c 20 6e 42  mset(zBuf, 0, nB
efba0 75 66 29 3b 0a 23 65 6c 73 65 0a 20 20 69 66 28  uf);.#else.  if(
efbb0 20 73 69 7a 65 6f 66 28 53 59 53 54 45 4d 54 49   sizeof(SYSTEMTI
efbc0 4d 45 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20  ME)<=nBuf-n ){. 
efbd0 20 20 20 53 59 53 54 45 4d 54 49 4d 45 20 78 3b     SYSTEMTIME x;
efbe0 0a 20 20 20 20 47 65 74 53 79 73 74 65 6d 54 69  .    GetSystemTi
efbf0 6d 65 28 26 78 29 3b 0a 20 20 20 20 6d 65 6d 63  me(&x);.    memc
efc00 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20 26 78 2c  py(&zBuf[n], &x,
efc10 20 73 69 7a 65 6f 66 28 78 29 29 3b 0a 20 20 20   sizeof(x));.   
efc20 20 6e 20 2b 3d 20 73 69 7a 65 6f 66 28 78 29 3b   n += sizeof(x);
efc30 0a 20 20 7d 0a 20 20 69 66 28 20 73 69 7a 65 6f  .  }.  if( sizeo
efc40 66 28 44 57 4f 52 44 29 3c 3d 6e 42 75 66 2d 6e  f(DWORD)<=nBuf-n
efc50 20 29 7b 0a 20 20 20 20 44 57 4f 52 44 20 70 69   ){.    DWORD pi
efc60 64 20 3d 20 47 65 74 43 75 72 72 65 6e 74 50 72  d = GetCurrentPr
efc70 6f 63 65 73 73 49 64 28 29 3b 0a 20 20 20 20 6d  ocessId();.    m
efc80 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d 2c 20  emcpy(&zBuf[n], 
efc90 26 70 69 64 2c 20 73 69 7a 65 6f 66 28 70 69 64  &pid, sizeof(pid
efca0 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a  ));.    n += siz
efcb0 65 6f 66 28 70 69 64 29 3b 0a 20 20 7d 0a 20 20  eof(pid);.  }.  
efcc0 69 66 28 20 73 69 7a 65 6f 66 28 44 57 4f 52 44  if( sizeof(DWORD
efcd0 29 3c 3d 6e 42 75 66 2d 6e 20 29 7b 0a 20 20 20  )<=nBuf-n ){.   
efce0 20 44 57 4f 52 44 20 63 6e 74 20 3d 20 47 65 74   DWORD cnt = Get
efcf0 54 69 63 6b 43 6f 75 6e 74 28 29 3b 0a 20 20 20  TickCount();.   
efd00 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
efd10 2c 20 26 63 6e 74 2c 20 73 69 7a 65 6f 66 28 63  , &cnt, sizeof(c
efd20 6e 74 29 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 73  nt));.    n += s
efd30 69 7a 65 6f 66 28 63 6e 74 29 3b 0a 20 20 7d 0a  izeof(cnt);.  }.
efd40 20 20 69 66 28 20 73 69 7a 65 6f 66 28 4c 41 52    if( sizeof(LAR
efd50 47 45 5f 49 4e 54 45 47 45 52 29 3c 3d 6e 42 75  GE_INTEGER)<=nBu
efd60 66 2d 6e 20 29 7b 0a 20 20 20 20 4c 41 52 47 45  f-n ){.    LARGE
efd70 5f 49 4e 54 45 47 45 52 20 69 3b 0a 20 20 20 20  _INTEGER i;.    
efd80 51 75 65 72 79 50 65 72 66 6f 72 6d 61 6e 63 65  QueryPerformance
efd90 43 6f 75 6e 74 65 72 28 26 69 29 3b 0a 20 20 20  Counter(&i);.   
efda0 20 6d 65 6d 63 70 79 28 26 7a 42 75 66 5b 6e 5d   memcpy(&zBuf[n]
efdb0 2c 20 26 69 2c 20 73 69 7a 65 6f 66 28 69 29 29  , &i, sizeof(i))
efdc0 3b 0a 20 20 20 20 6e 20 2b 3d 20 73 69 7a 65 6f  ;.    n += sizeo
efdd0 66 28 69 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  f(i);.  }.#endif
efde0 0a 20 20 72 65 74 75 72 6e 20 6e 3b 0a 7d 0a 0a  .  return n;.}..
efdf0 0a 2f 2a 0a 2a 2a 20 53 6c 65 65 70 20 66 6f 72  ./*.** Sleep for
efe00 20 61 20 6c 69 74 74 6c 65 20 77 68 69 6c 65 2e   a little while.
efe10 20 20 52 65 74 75 72 6e 20 74 68 65 20 61 6d 6f    Return the amo
efe20 75 6e 74 20 6f 66 20 74 69 6d 65 20 73 6c 65 70  unt of time slep
efe30 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  t..*/.static int
efe40 20 77 69 6e 53 6c 65 65 70 28 73 71 6c 69 74 65   winSleep(sqlite
efe50 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
efe60 20 6d 69 63 72 6f 73 65 63 29 7b 0a 20 20 53 6c   microsec){.  Sl
efe70 65 65 70 28 28 6d 69 63 72 6f 73 65 63 2b 39 39  eep((microsec+99
efe80 39 29 2f 31 30 30 30 29 3b 0a 20 20 72 65 74 75  9)/1000);.  retu
efe90 72 6e 20 28 28 6d 69 63 72 6f 73 65 63 2b 39 39  rn ((microsec+99
efea0 39 29 2f 31 30 30 30 29 2a 31 30 30 30 3b 0a 7d  9)/1000)*1000;.}
efeb0 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
efec0 6f 77 69 6e 67 20 76 61 72 69 61 62 6c 65 2c 20  owing variable, 
efed0 69 66 20 73 65 74 20 74 6f 20 61 20 6e 6f 6e 2d  if set to a non-
efee0 7a 65 72 6f 20 76 61 6c 75 65 2c 20 62 65 63 6f  zero value, beco
efef0 6d 65 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a  mes the result.*
eff00 2a 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20  * returned from 
eff10 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
eff20 54 69 6d 65 28 29 2e 20 20 54 68 69 73 20 69 73  Time().  This is
eff30 20 75 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e   used for testin
eff40 67 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  g..*/.#ifdef SQL
eff50 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f  ITE_TEST.SQLITE_
eff60 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
eff70 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 3d 20 30  current_time = 0
eff80 3b 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  ;.#endif../*.** 
eff90 46 69 6e 64 20 74 68 65 20 63 75 72 72 65 6e 74  Find the current
effa0 20 74 69 6d 65 20 28 69 6e 20 55 6e 69 76 65 72   time (in Univer
effb0 73 61 6c 20 43 6f 6f 72 64 69 6e 61 74 65 64 20  sal Coordinated 
effc0 54 69 6d 65 29 2e 20 20 57 72 69 74 65 20 74 68  Time).  Write th
effd0 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 74 69 6d  e.** current tim
effe0 65 20 61 6e 64 20 64 61 74 65 20 61 73 20 61 20  e and date as a 
efff0 4a 75 6c 69 61 6e 20 44 61 79 20 6e 75 6d 62 65  Julian Day numbe
f0000 72 20 69 6e 74 6f 20 2a 70 72 4e 6f 77 20 61 6e  r into *prNow an
f0010 64 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 20 20  d.** return 0.  
f0020 52 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 20  Return 1 if the 
f0030 74 69 6d 65 20 61 6e 64 20 64 61 74 65 20 63 61  time and date ca
f0040 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2e 0a 2a  nnot be found..*
f0050 2f 0a 69 6e 74 20 77 69 6e 43 75 72 72 65 6e 74  /.int winCurrent
f0060 54 69 6d 65 28 73 71 6c 69 74 65 33 5f 76 66 73  Time(sqlite3_vfs
f0070 20 2a 70 56 66 73 2c 20 64 6f 75 62 6c 65 20 2a   *pVfs, double *
f0080 70 72 4e 6f 77 29 7b 0a 20 20 46 49 4c 45 54 49  prNow){.  FILETI
f0090 4d 45 20 66 74 3b 0a 20 20 2f 2a 20 46 49 4c 45  ME ft;.  /* FILE
f00a0 54 49 4d 45 20 73 74 72 75 63 74 75 72 65 20 69  TIME structure i
f00b0 73 20 61 20 36 34 2d 62 69 74 20 76 61 6c 75 65  s a 64-bit value
f00c0 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
f00d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 0a 20 20 20  e number of .   
f00e0 20 20 31 30 30 2d 6e 61 6e 6f 73 65 63 6f 6e 64    100-nanosecond
f00f0 20 69 6e 74 65 72 76 61 6c 73 20 73 69 6e 63 65   intervals since
f0100 20 4a 61 6e 75 61 72 79 20 31 2c 20 31 36 30 31   January 1, 1601
f0110 20 28 3d 20 4a 44 20 32 33 30 35 38 31 33 2e 35   (= JD 2305813.5
f0120 29 2e 20 0a 20 20 2a 2f 0a 20 20 64 6f 75 62 6c  ). .  */.  doubl
f0130 65 20 6e 6f 77 3b 0a 23 69 66 20 53 51 4c 49 54  e now;.#if SQLIT
f0140 45 5f 4f 53 5f 57 49 4e 43 45 0a 20 20 53 59 53  E_OS_WINCE.  SYS
f0150 54 45 4d 54 49 4d 45 20 74 69 6d 65 3b 0a 20 20  TEMTIME time;.  
f0160 47 65 74 53 79 73 74 65 6d 54 69 6d 65 28 26 74  GetSystemTime(&t
f0170 69 6d 65 29 3b 0a 20 20 2f 2a 20 69 66 20 53 79  ime);.  /* if Sy
f0180 73 74 65 6d 54 69 6d 65 54 6f 46 69 6c 65 54 69  stemTimeToFileTi
f0190 6d 65 28 29 20 66 61 69 6c 73 2c 20 69 74 20 72  me() fails, it r
f01a0 65 74 75 72 6e 73 20 7a 65 72 6f 2e 20 2a 2f 0a  eturns zero. */.
f01b0 20 20 69 66 20 28 21 53 79 73 74 65 6d 54 69 6d    if (!SystemTim
f01c0 65 54 6f 46 69 6c 65 54 69 6d 65 28 26 74 69 6d  eToFileTime(&tim
f01d0 65 2c 26 66 74 29 29 7b 0a 20 20 20 20 72 65 74  e,&ft)){.    ret
f01e0 75 72 6e 20 31 3b 0a 20 20 7d 0a 23 65 6c 73 65  urn 1;.  }.#else
f01f0 0a 20 20 47 65 74 53 79 73 74 65 6d 54 69 6d 65  .  GetSystemTime
f0200 41 73 46 69 6c 65 54 69 6d 65 28 20 26 66 74 20  AsFileTime( &ft 
f0210 29 3b 0a 23 65 6e 64 69 66 0a 20 20 6e 6f 77 20  );.#endif.  now 
f0220 3d 20 28 28 64 6f 75 62 6c 65 29 66 74 2e 64 77  = ((double)ft.dw
f0230 48 69 67 68 44 61 74 65 54 69 6d 65 29 20 2a 20  HighDateTime) * 
f0240 34 32 39 34 39 36 37 32 39 36 2e 30 3b 20 0a 20  4294967296.0; . 
f0250 20 2a 70 72 4e 6f 77 20 3d 20 28 6e 6f 77 20 2b   *prNow = (now +
f0260 20 66 74 2e 64 77 4c 6f 77 44 61 74 65 54 69 6d   ft.dwLowDateTim
f0270 65 29 2f 38 36 34 30 30 30 30 30 30 30 30 30 2e  e)/864000000000.
f0280 30 20 2b 20 32 33 30 35 38 31 33 2e 35 3b 0a 23  0 + 2305813.5;.#
f0290 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
f02a0 54 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  T.  if( sqlite3_
f02b0 63 75 72 72 65 6e 74 5f 74 69 6d 65 20 29 7b 0a  current_time ){.
f02c0 20 20 20 20 2a 70 72 4e 6f 77 20 3d 20 73 71 6c      *prNow = sql
f02d0 69 74 65 33 5f 63 75 72 72 65 6e 74 5f 74 69 6d  ite3_current_tim
f02e0 65 2f 38 36 34 30 30 2e 30 20 2b 20 32 34 34 30  e/86400.0 + 2440
f02f0 35 38 37 2e 35 3b 0a 20 20 7d 0a 23 65 6e 64 69  587.5;.  }.#endi
f0300 66 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  f.  return 0;.}.
f0310 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 64 65 61 20  ./*.** The idea 
f0320 69 73 20 74 68 61 74 20 74 68 69 73 20 66 75 6e  is that this fun
f0330 63 74 69 6f 6e 20 77 6f 72 6b 73 20 6c 69 6b 65  ction works like
f0340 20 61 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f   a combination o
f0350 66 0a 2a 2a 20 47 65 74 4c 61 73 74 45 72 72 6f  f.** GetLastErro
f0360 72 28 29 20 61 6e 64 20 46 6f 72 6d 61 74 4d 65  r() and FormatMe
f0370 73 73 61 67 65 28 29 20 6f 6e 20 77 69 6e 64 6f  ssage() on windo
f0380 77 73 20 28 6f 72 20 65 72 72 6e 6f 20 61 6e 64  ws (or errno and
f0390 0a 2a 2a 20 73 74 72 65 72 72 6f 72 5f 72 28 29  .** strerror_r()
f03a0 20 6f 6e 20 75 6e 69 78 29 2e 20 41 66 74 65 72   on unix). After
f03b0 20 61 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74   an error is ret
f03c0 75 72 6e 65 64 20 62 79 20 61 6e 20 4f 53 0a 2a  urned by an OS.*
f03d0 2a 20 66 75 6e 63 74 69 6f 6e 2c 20 53 51 4c 69  * function, SQLi
f03e0 74 65 20 63 61 6c 6c 73 20 74 68 69 73 20 66 75  te calls this fu
f03f0 6e 63 74 69 6f 6e 20 77 69 74 68 20 7a 42 75 66  nction with zBuf
f0400 20 70 6f 69 6e 74 69 6e 67 20 74 6f 0a 2a 2a 20   pointing to.** 
f0410 61 20 62 75 66 66 65 72 20 6f 66 20 6e 42 75 66  a buffer of nBuf
f0420 20 62 79 74 65 73 2e 20 54 68 65 20 4f 53 20 6c   bytes. The OS l
f0430 61 79 65 72 20 73 68 6f 75 6c 64 20 70 6f 70 75  ayer should popu
f0440 6c 61 74 65 20 74 68 65 0a 2a 2a 20 62 75 66 66  late the.** buff
f0450 65 72 20 77 69 74 68 20 61 20 6e 75 6c 2d 74 65  er with a nul-te
f0460 72 6d 69 6e 61 74 65 64 20 55 54 46 2d 38 20 65  rminated UTF-8 e
f0470 6e 63 6f 64 65 64 20 65 72 72 6f 72 20 6d 65 73  ncoded error mes
f0480 73 61 67 65 0a 2a 2a 20 64 65 73 63 72 69 62 69  sage.** describi
f0490 6e 67 20 74 68 65 20 6c 61 73 74 20 49 4f 20 65  ng the last IO e
f04a0 72 72 6f 72 20 74 6f 20 68 61 76 65 20 6f 63 63  rror to have occ
f04b0 75 72 65 64 20 77 69 74 68 69 6e 20 74 68 65 20  ured within the 
f04c0 63 61 6c 6c 69 6e 67 0a 2a 2a 20 74 68 72 65 61  calling.** threa
f04d0 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
f04e0 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73  error message is
f04f0 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74   too large for t
f0500 68 65 20 73 75 70 70 6c 69 65 64 20 62 75 66 66  he supplied buff
f0510 65 72 2c 0a 2a 2a 20 69 74 20 73 68 6f 75 6c 64  er,.** it should
f0520 20 62 65 20 74 72 75 6e 63 61 74 65 64 2e 20 54   be truncated. T
f0530 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
f0540 6f 66 20 78 47 65 74 4c 61 73 74 45 72 72 6f 72  of xGetLastError
f0550 0a 2a 2a 20 69 73 20 7a 65 72 6f 20 69 66 20 74  .** is zero if t
f0560 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
f0570 20 66 69 74 73 20 69 6e 20 74 68 65 20 62 75 66   fits in the buf
f0580 66 65 72 2c 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f  fer, or non-zero
f0590 0a 2a 2a 20 6f 74 68 65 72 77 69 73 65 20 28 69  .** otherwise (i
f05a0 66 20 74 68 65 20 6d 65 73 73 61 67 65 20 77 61  f the message wa
f05b0 73 20 74 72 75 6e 63 61 74 65 64 29 2e 20 49 66  s truncated). If
f05c0 20 6e 6f 6e 2d 7a 65 72 6f 20 69 73 20 72 65 74   non-zero is ret
f05d0 75 72 6e 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 69  urned,.** then i
f05e0 74 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73 61  t is not necessa
f05f0 72 79 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68  ry to include th
f0600 65 20 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72  e nul-terminator
f0610 20 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 69 6e   character.** in
f0620 20 74 68 65 20 6f 75 74 70 75 74 20 62 75 66 66   the output buff
f0630 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 20 73 75  er..**.** Not su
f0640 70 70 6c 79 69 6e 67 20 61 6e 20 65 72 72 6f 72  pplying an error
f0650 20 6d 65 73 73 61 67 65 20 77 69 6c 6c 20 68 61   message will ha
f0660 76 65 20 6e 6f 20 61 64 76 65 72 73 65 20 65 66  ve no adverse ef
f0670 66 65 63 74 0a 2a 2a 20 6f 6e 20 53 51 4c 69 74  fect.** on SQLit
f0680 65 2e 20 49 74 20 69 73 20 66 69 6e 65 20 74 6f  e. It is fine to
f0690 20 68 61 76 65 20 61 6e 20 69 6d 70 6c 65 6d 65   have an impleme
f06a0 6e 74 61 74 69 6f 6e 20 74 68 61 74 20 6e 65 76  ntation that nev
f06b0 65 72 0a 2a 2a 20 72 65 74 75 72 6e 73 20 61 6e  er.** returns an
f06c0 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 3a 0a   error message:.
f06d0 2a 2a 0a 2a 2a 20 20 20 69 6e 74 20 78 47 65 74  **.**   int xGet
f06e0 4c 61 73 74 45 72 72 6f 72 28 73 71 6c 69 74 65  LastError(sqlite
f06f0 33 5f 76 66 73 20 2a 70 56 66 73 2c 20 69 6e 74  3_vfs *pVfs, int
f0700 20 6e 42 75 66 2c 20 63 68 61 72 20 2a 7a 42 75   nBuf, char *zBu
f0710 66 29 7b 0a 2a 2a 20 20 20 20 20 61 73 73 65 72  f){.**     asser
f0720 74 28 7a 42 75 66 5b 30 5d 3d 3d 27 5c 30 27 29  t(zBuf[0]=='\0')
f0730 3b 0a 2a 2a 20 20 20 20 20 72 65 74 75 72 6e 20  ;.**     return 
f0740 30 3b 0a 2a 2a 20 20 20 7d 0a 2a 2a 0a 2a 2a 20  0;.**   }.**.** 
f0750 48 6f 77 65 76 65 72 20 69 66 20 61 6e 20 65 72  However if an er
f0760 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 73  ror message is s
f0770 75 70 70 6c 69 65 64 2c 20 69 74 20 77 69 6c 6c  upplied, it will
f0780 20 62 65 20 69 6e 63 6f 72 70 6f 72 61 74 65 64   be incorporated
f0790 0a 2a 2a 20 62 79 20 73 71 6c 69 74 65 20 69 6e  .** by sqlite in
f07a0 74 6f 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73  to the error mes
f07b0 73 61 67 65 20 61 76 61 69 6c 61 62 6c 65 20 74  sage available t
f07c0 6f 20 74 68 65 20 75 73 65 72 20 75 73 69 6e 67  o the user using
f07d0 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  .** sqlite3_errm
f07e0 73 67 28 29 2c 20 70 6f 73 73 69 62 6c 79 20 6d  sg(), possibly m
f07f0 61 6b 69 6e 67 20 49 4f 20 65 72 72 6f 72 73 20  aking IO errors 
f0800 65 61 73 69 65 72 20 74 6f 20 64 65 62 75 67 2e  easier to debug.
f0810 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77  .*/.static int w
f0820 69 6e 47 65 74 4c 61 73 74 45 72 72 6f 72 28 73  inGetLastError(s
f0830 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
f0840 2c 20 69 6e 74 20 6e 42 75 66 2c 20 63 68 61 72  , int nBuf, char
f0850 20 2a 7a 42 75 66 29 7b 0a 20 20 72 65 74 75 72   *zBuf){.  retur
f0860 6e 20 67 65 74 4c 61 73 74 45 72 72 6f 72 4d 73  n getLastErrorMs
f0870 67 28 6e 42 75 66 2c 20 7a 42 75 66 29 3b 0a 7d  g(nBuf, zBuf);.}
f0880 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69  ../*.** Initiali
f0890 7a 65 20 61 6e 64 20 64 65 69 6e 69 74 69 61 6c  ze and deinitial
f08a0 69 7a 65 20 74 68 65 20 6f 70 65 72 61 74 69 6e  ize the operatin
f08b0 67 20 73 79 73 74 65 6d 20 69 6e 74 65 72 66 61  g system interfa
f08c0 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  ce..*/.SQLITE_AP
f08d0 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 73  I int sqlite3_os
f08e0 5f 69 6e 69 74 28 76 6f 69 64 29 7b 0a 20 20 73  _init(void){.  s
f08f0 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f 76 66  tatic sqlite3_vf
f0900 73 20 77 69 6e 56 66 73 20 3d 20 7b 0a 20 20 20  s winVfs = {.   
f0910 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   1,             
f0920 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
f0930 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 77 69  */.    sizeof(wi
f0940 6e 46 69 6c 65 29 2c 20 20 20 2f 2a 20 73 7a 4f  nFile),   /* szO
f0950 73 46 69 6c 65 20 2a 2f 0a 20 20 20 20 4d 41 58  sFile */.    MAX
f0960 5f 50 41 54 48 2c 20 20 20 20 20 20 20 20 20 20  _PATH,          
f0970 2f 2a 20 6d 78 50 61 74 68 6e 61 6d 65 20 2a 2f  /* mxPathname */
f0980 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
f0990 20 20 20 20 20 20 20 20 2f 2a 20 70 4e 65 78 74          /* pNext
f09a0 20 2a 2f 0a 20 20 20 20 22 77 69 6e 33 32 22 2c   */.    "win32",
f09b0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 4e             /* zN
f09c0 61 6d 65 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20  ame */.    0,   
f09d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f09e0 20 70 41 70 70 44 61 74 61 20 2a 2f 0a 20 0a 20   pAppData */. . 
f09f0 20 20 20 77 69 6e 4f 70 65 6e 2c 20 20 20 20 20     winOpen,     
f0a00 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e 20 2a        /* xOpen *
f0a10 2f 0a 20 20 20 20 77 69 6e 44 65 6c 65 74 65 2c  /.    winDelete,
f0a20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 65 6c           /* xDel
f0a30 65 74 65 20 2a 2f 0a 20 20 20 20 77 69 6e 41 63  ete */.    winAc
f0a40 63 65 73 73 2c 20 20 20 20 20 20 20 20 20 2f 2a  cess,         /*
f0a50 20 78 41 63 63 65 73 73 20 2a 2f 0a 20 20 20 20   xAccess */.    
f0a60 77 69 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c  winFullPathname,
f0a70 20 20 20 2f 2a 20 78 46 75 6c 6c 50 61 74 68 6e     /* xFullPathn
f0a80 61 6d 65 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c  ame */.    winDl
f0a90 4f 70 65 6e 2c 20 20 20 20 20 20 20 20 20 2f 2a  Open,         /*
f0aa0 20 78 44 6c 4f 70 65 6e 20 2a 2f 0a 20 20 20 20   xDlOpen */.    
f0ab0 77 69 6e 44 6c 45 72 72 6f 72 2c 20 20 20 20 20  winDlError,     
f0ac0 20 20 20 2f 2a 20 78 44 6c 45 72 72 6f 72 20 2a     /* xDlError *
f0ad0 2f 0a 20 20 20 20 77 69 6e 44 6c 53 79 6d 2c 20  /.    winDlSym, 
f0ae0 20 20 20 20 20 20 20 20 20 2f 2a 20 78 44 6c 53           /* xDlS
f0af0 79 6d 20 2a 2f 0a 20 20 20 20 77 69 6e 44 6c 43  ym */.    winDlC
f0b00 6c 6f 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20  lose,        /* 
f0b10 78 44 6c 43 6c 6f 73 65 20 2a 2f 0a 20 20 20 20  xDlClose */.    
f0b20 77 69 6e 52 61 6e 64 6f 6d 6e 65 73 73 2c 20 20  winRandomness,  
f0b30 20 20 20 2f 2a 20 78 52 61 6e 64 6f 6d 6e 65 73     /* xRandomnes
f0b40 73 20 2a 2f 0a 20 20 20 20 77 69 6e 53 6c 65 65  s */.    winSlee
f0b50 70 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78  p,          /* x
f0b60 53 6c 65 65 70 20 2a 2f 0a 20 20 20 20 77 69 6e  Sleep */.    win
f0b70 43 75 72 72 65 6e 74 54 69 6d 65 2c 20 20 20 20  CurrentTime,    
f0b80 2f 2a 20 78 43 75 72 72 65 6e 74 54 69 6d 65 20  /* xCurrentTime 
f0b90 2a 2f 0a 20 20 20 20 77 69 6e 47 65 74 4c 61 73  */.    winGetLas
f0ba0 74 45 72 72 6f 72 20 20 20 20 2f 2a 20 78 47 65  tError    /* xGe
f0bb0 74 4c 61 73 74 45 72 72 6f 72 20 2a 2f 0a 20 20  tLastError */.  
f0bc0 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73  };.  sqlite3_vfs
f0bd0 5f 72 65 67 69 73 74 65 72 28 26 77 69 6e 56 66  _register(&winVf
f0be0 73 2c 20 31 29 3b 0a 20 20 72 65 74 75 72 6e 20  s, 1);.  return 
f0bf0 53 51 4c 49 54 45 5f 4f 4b 3b 20 0a 7d 0a 53 51  SQLITE_OK; .}.SQ
f0c00 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
f0c10 69 74 65 33 5f 6f 73 5f 65 6e 64 28 76 6f 69 64  ite3_os_end(void
f0c20 29 7b 20 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  ){ .  return SQL
f0c30 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69  ITE_OK;.}..#endi
f0c40 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 53 5f 57  f /* SQLITE_OS_W
f0c50 49 4e 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  IN */../********
f0c60 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6f 73  ****** End of os
f0c70 5f 77 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _win.c *********
f0c80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0ca0 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
f0cb0 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
f0cc0 65 20 62 69 74 76 65 63 2e 63 20 2a 2a 2a 2a 2a  e bitvec.c *****
f0cd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0ce0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0cf0 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
f0d00 38 20 46 65 62 72 75 61 72 79 20 31 36 0a 2a 2a  8 February 16.**
f0d10 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
f0d20 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
f0d30 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
f0d40 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
f0d50 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
f0d60 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
f0d70 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
f0d80 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
f0d90 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
f0da0 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
f0db0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
f0dc0 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
f0dd0 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
f0de0 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
f0df0 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
f0e00 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
f0e10 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
f0e20 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
f0e30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0e40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0e50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f0e60 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
f0e70 54 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  This file implem
f0e80 65 6e 74 73 20 61 6e 20 6f 62 6a 65 63 74 20 74  ents an object t
f0e90 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 61  hat represents a
f0ea0 20 66 69 78 65 64 2d 6c 65 6e 67 74 68 0a 2a 2a   fixed-length.**
f0eb0 20 62 69 74 6d 61 70 2e 20 20 42 69 74 73 20 61   bitmap.  Bits a
f0ec0 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72  re numbered star
f0ed0 74 69 6e 67 20 77 69 74 68 20 31 2e 0a 2a 2a 0a  ting with 1..**.
f0ee0 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73 20 75  ** A bitmap is u
f0ef0 73 65 64 20 74 6f 20 72 65 63 6f 72 64 20 77 68  sed to record wh
f0f00 69 63 68 20 70 61 67 65 73 20 6f 66 20 61 20 64  ich pages of a d
f0f10 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61 76  atabase file hav
f0f20 65 20 62 65 65 6e 0a 2a 2a 20 6a 6f 75 72 6e 61  e been.** journa
f0f30 6c 6c 65 64 20 64 75 72 69 6e 67 20 61 20 74 72  lled during a tr
f0f40 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 77 68  ansaction, or wh
f0f50 69 63 68 20 70 61 67 65 73 20 68 61 76 65 20 74  ich pages have t
f0f60 68 65 20 22 64 6f 6e 74 2d 77 72 69 74 65 22 0a  he "dont-write".
f0f70 2a 2a 20 70 72 6f 70 65 72 74 79 2e 20 20 55 73  ** property.  Us
f0f80 75 61 6c 6c 79 20 6f 6e 6c 79 20 61 20 66 65 77  ually only a few
f0f90 20 70 61 67 65 73 20 61 72 65 20 6d 65 65 74 20   pages are meet 
f0fa0 65 69 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e  either condition
f0fb0 2e 0a 2a 2a 20 53 6f 20 74 68 65 20 62 69 74 6d  ..** So the bitm
f0fc0 61 70 20 69 73 20 75 73 75 61 6c 6c 79 20 73 70  ap is usually sp
f0fd0 61 72 73 65 20 61 6e 64 20 68 61 73 20 6c 6f 77  arse and has low
f0fe0 20 63 61 72 64 69 6e 61 6c 69 74 79 2e 0a 2a 2a   cardinality..**
f0ff0 20 42 75 74 20 73 6f 6d 65 74 69 6d 65 73 20 28   But sometimes (
f1000 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 68 65 6e  for example when
f1010 20 64 75 72 69 6e 67 20 61 20 44 52 4f 50 20 6f   during a DROP o
f1020 66 20 61 20 6c 61 72 67 65 20 74 61 62 6c 65 29  f a large table)
f1030 20 6d 6f 73 74 0a 2a 2a 20 6f 72 20 61 6c 6c 20   most.** or all 
f1040 6f 66 20 74 68 65 20 70 61 67 65 73 20 69 6e 20  of the pages in 
f1050 61 20 64 61 74 61 62 61 73 65 20 63 61 6e 20 67  a database can g
f1060 65 74 20 6a 6f 75 72 6e 61 6c 6c 65 64 2e 20 20  et journalled.  
f1070 49 6e 20 74 68 6f 73 65 20 63 61 73 65 73 2c 20  In those cases, 
f1080 0a 2a 2a 20 74 68 65 20 62 69 74 6d 61 70 20 62  .** the bitmap b
f1090 65 63 6f 6d 65 73 20 64 65 6e 73 65 20 77 69 74  ecomes dense wit
f10a0 68 20 68 69 67 68 20 63 61 72 64 69 6e 61 6c 69  h high cardinali
f10b0 74 79 2e 20 20 54 68 65 20 61 6c 67 6f 72 69 74  ty.  The algorit
f10c0 68 6d 20 6e 65 65 64 73 20 0a 2a 2a 20 74 6f 20  hm needs .** to 
f10d0 68 61 6e 64 6c 65 20 62 6f 74 68 20 63 61 73 65  handle both case
f10e0 73 20 77 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68  s well..**.** Th
f10f0 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20 62 69  e size of the bi
f1100 74 6d 61 70 20 69 73 20 66 69 78 65 64 20 77 68  tmap is fixed wh
f1110 65 6e 20 74 68 65 20 6f 62 6a 65 63 74 20 69 73  en the object is
f1120 20 63 72 65 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   created..**.** 
f1130 41 6c 6c 20 62 69 74 73 20 61 72 65 20 63 6c 65  All bits are cle
f1140 61 72 20 77 68 65 6e 20 74 68 65 20 62 69 74 6d  ar when the bitm
f1150 61 70 20 69 73 20 63 72 65 61 74 65 64 2e 20 20  ap is created.  
f1160 49 6e 64 69 76 69 64 75 61 6c 20 62 69 74 73 0a  Individual bits.
f1170 2a 2a 20 6d 61 79 20 62 65 20 73 65 74 20 6f 72  ** may be set or
f1180 20 63 6c 65 61 72 65 64 20 6f 6e 65 20 61 74 20   cleared one at 
f1190 61 20 74 69 6d 65 2e 0a 2a 2a 0a 2a 2a 20 54 65  a time..**.** Te
f11a0 73 74 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72  st operations ar
f11b0 65 20 61 62 6f 75 74 20 31 30 30 20 74 69 6d 65  e about 100 time
f11c0 73 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e 20 74 68  s more common th
f11d0 61 74 20 73 65 74 20 6f 70 65 72 61 74 69 6f 6e  at set operation
f11e0 73 2e 0a 2a 2a 20 43 6c 65 61 72 20 6f 70 65 72  s..** Clear oper
f11f0 61 74 69 6f 6e 73 20 61 72 65 20 65 78 63 65 65  ations are excee
f1200 64 69 6e 67 6c 79 20 72 61 72 65 2e 20 20 54 68  dingly rare.  Th
f1210 65 72 65 20 61 72 65 20 75 73 75 61 6c 6c 79 20  ere are usually 
f1220 62 65 74 77 65 65 6e 0a 2a 2a 20 35 20 61 6e 64  between.** 5 and
f1230 20 35 30 30 20 73 65 74 20 6f 70 65 72 61 74 69   500 set operati
f1240 6f 6e 73 20 70 65 72 20 42 69 74 76 65 63 20 6f  ons per Bitvec o
f1250 62 6a 65 63 74 2c 20 74 68 6f 75 67 68 20 74 68  bject, though th
f1260 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 74 73  e number of sets
f1270 20 63 61 6e 0a 2a 2a 20 73 6f 6d 65 74 69 6d 65   can.** sometime
f1280 73 20 67 72 6f 77 20 69 6e 74 6f 20 74 65 6e 73  s grow into tens
f1290 20 6f 66 20 74 68 6f 75 73 61 6e 64 73 20 6f 72   of thousands or
f12a0 20 6c 61 72 67 65 72 2e 20 20 54 68 65 20 73 69   larger.  The si
f12b0 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 42 69 74  ze of the.** Bit
f12c0 76 65 63 20 6f 62 6a 65 63 74 20 69 73 20 74 68  vec object is th
f12d0 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  e number of page
f12e0 73 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73  s in the databas
f12f0 65 20 66 69 6c 65 20 61 74 20 74 68 65 0a 2a 2a  e file at the.**
f1300 20 73 74 61 72 74 20 6f 66 20 61 20 74 72 61 6e   start of a tran
f1310 73 61 63 74 69 6f 6e 2c 20 61 6e 64 20 69 73 20  saction, and is 
f1320 74 68 75 73 20 75 73 75 61 6c 6c 79 20 6c 65 73  thus usually les
f1330 73 20 74 68 61 6e 20 61 20 66 65 77 20 74 68 6f  s than a few tho
f1340 75 73 61 6e 64 2c 0a 2a 2a 20 62 75 74 20 63 61  usand,.** but ca
f1350 6e 20 62 65 20 61 73 20 6c 61 72 67 65 20 61 73  n be as large as
f1360 20 32 20 62 69 6c 6c 69 6f 6e 20 66 6f 72 20 61   2 billion for a
f1370 20 72 65 61 6c 6c 79 20 62 69 67 20 64 61 74 61   really big data
f1380 62 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  base..**.** @(#)
f1390 20 24 49 64 3a 20 62 69 74 76 65 63 2e 63 2c 76   $Id: bitvec.c,v
f13a0 20 31 2e 39 20 32 30 30 38 2f 31 31 2f 31 39 20   1.9 2008/11/19 
f13b0 31 38 3a 33 30 3a 33 35 20 73 68 61 6e 65 20 45  18:30:35 shane E
f13c0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 53 69 7a 65  xp $.*/../* Size
f13d0 20 6f 66 20 74 68 65 20 42 69 74 76 65 63 20 73   of the Bitvec s
f13e0 74 72 75 63 74 75 72 65 20 69 6e 20 62 79 74 65  tructure in byte
f13f0 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  s. */.#define BI
f1400 54 56 45 43 5f 53 5a 20 20 20 20 20 20 20 20 35  TVEC_SZ        5
f1410 31 32 0a 0a 2f 2a 20 52 6f 75 6e 64 20 74 68 65  12../* Round the
f1420 20 75 6e 69 6f 6e 20 73 69 7a 65 20 64 6f 77 6e   union size down
f1430 20 74 6f 20 74 68 65 20 6e 65 61 72 65 73 74 20   to the nearest 
f1440 70 6f 69 6e 74 65 72 20 62 6f 75 6e 64 61 72 79  pointer boundary
f1450 2c 20 73 69 6e 63 65 20 74 68 61 74 27 73 20 68  , since that's h
f1460 6f 77 20 0a 2a 2a 20 69 74 20 77 69 6c 6c 20 62  ow .** it will b
f1470 65 20 61 6c 69 67 6e 65 64 20 77 69 74 68 69 6e  e aligned within
f1480 20 74 68 65 20 42 69 74 76 65 63 20 73 74 72 75   the Bitvec stru
f1490 63 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  ct. */.#define B
f14a0 49 54 56 45 43 5f 55 53 49 5a 45 20 20 20 20 20  ITVEC_USIZE     
f14b0 28 28 28 42 49 54 56 45 43 5f 53 5a 2d 28 33 2a  (((BITVEC_SZ-(3*
f14c0 73 69 7a 65 6f 66 28 75 33 32 29 29 29 2f 73 69  sizeof(u32)))/si
f14d0 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 2a 73  zeof(Bitvec*))*s
f14e0 69 7a 65 6f 66 28 42 69 74 76 65 63 2a 29 29 0a  izeof(Bitvec*)).
f14f0 0a 2f 2a 20 54 79 70 65 20 6f 66 20 74 68 65 20  ./* Type of the 
f1500 61 72 72 61 79 20 22 65 6c 65 6d 65 6e 74 22 20  array "element" 
f1510 66 6f 72 20 74 68 65 20 62 69 74 6d 61 70 20 72  for the bitmap r
f1520 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 20 0a  epresentation. .
f1530 2a 2a 20 53 68 6f 75 6c 64 20 62 65 20 61 20 70  ** Should be a p
f1540 6f 77 65 72 20 6f 66 20 32 2c 20 61 6e 64 20 69  ower of 2, and i
f1550 64 65 61 6c 6c 79 2c 20 65 76 65 6e 6c 79 20 64  deally, evenly d
f1560 69 76 69 64 65 20 69 6e 74 6f 20 42 49 54 56 45  ivide into BITVE
f1570 43 5f 55 53 49 5a 45 2e 20 0a 2a 2a 20 53 65 74  C_USIZE. .** Set
f1580 74 69 6e 67 20 74 68 69 73 20 74 6f 20 74 68 65  ting this to the
f1590 20 22 6e 61 74 75 72 61 6c 20 77 6f 72 64 22 20   "natural word" 
f15a0 73 69 7a 65 20 6f 66 20 79 6f 75 72 20 43 50 55  size of your CPU
f15b0 20 6d 61 79 20 69 6d 70 72 6f 76 65 0a 2a 2a 20   may improve.** 
f15c0 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 2a 2f 0a  performance. */.
f15d0 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 54  #define BITVEC_T
f15e0 45 4c 45 4d 20 20 20 20 20 75 38 0a 2f 2a 20 53  ELEM     u8./* S
f15f0 69 7a 65 2c 20 69 6e 20 62 69 74 73 2c 20 6f 66  ize, in bits, of
f1600 20 74 68 65 20 62 69 74 6d 61 70 20 65 6c 65 6d   the bitmap elem
f1610 65 6e 74 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  ent. */.#define 
f1620 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 20 20 20  BITVEC_SZELEM   
f1630 20 38 0a 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20   8./* Number of 
f1640 65 6c 65 6d 65 6e 74 73 20 69 6e 20 61 20 62 69  elements in a bi
f1650 74 6d 61 70 20 61 72 72 61 79 2e 20 2a 2f 0a 23  tmap array. */.#
f1660 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f 4e 45  define BITVEC_NE
f1670 4c 45 4d 20 20 20 20 20 28 42 49 54 56 45 43 5f  LEM     (BITVEC_
f1680 55 53 49 5a 45 2f 73 69 7a 65 6f 66 28 42 49 54  USIZE/sizeof(BIT
f1690 56 45 43 5f 54 45 4c 45 4d 29 29 0a 2f 2a 20 4e  VEC_TELEM))./* N
f16a0 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 69 6e  umber of bits in
f16b0 20 74 68 65 20 62 69 74 6d 61 70 20 61 72 72 61   the bitmap arra
f16c0 79 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49  y. */.#define BI
f16d0 54 56 45 43 5f 4e 42 49 54 20 20 20 20 20 20 28  TVEC_NBIT      (
f16e0 42 49 54 56 45 43 5f 4e 45 4c 45 4d 2a 42 49 54  BITVEC_NELEM*BIT
f16f0 56 45 43 5f 53 5a 45 4c 45 4d 29 0a 0a 2f 2a 20  VEC_SZELEM)../* 
f1700 4e 75 6d 62 65 72 20 6f 66 20 75 33 32 20 76 61  Number of u32 va
f1710 6c 75 65 73 20 69 6e 20 68 61 73 68 20 74 61 62  lues in hash tab
f1720 6c 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42  le. */.#define B
f1730 49 54 56 45 43 5f 4e 49 4e 54 20 20 20 20 20 20  ITVEC_NINT      
f1740 28 42 49 54 56 45 43 5f 55 53 49 5a 45 2f 73 69  (BITVEC_USIZE/si
f1750 7a 65 6f 66 28 75 33 32 29 29 0a 2f 2a 20 4d 61  zeof(u32))./* Ma
f1760 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
f1770 65 6e 74 72 69 65 73 20 69 6e 20 68 61 73 68 20  entries in hash 
f1780 74 61 62 6c 65 20 62 65 66 6f 72 65 20 0a 2a 2a  table before .**
f1790 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61 6e   sub-dividing an
f17a0 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a 2f  d re-hashing. */
f17b0 0a 23 64 65 66 69 6e 65 20 42 49 54 56 45 43 5f  .#define BITVEC_
f17c0 4d 58 48 41 53 48 20 20 20 20 28 42 49 54 56 45  MXHASH    (BITVE
f17d0 43 5f 4e 49 4e 54 2f 32 29 0a 2f 2a 20 48 61 73  C_NINT/2)./* Has
f17e0 68 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 66 6f  hing function fo
f17f0 72 20 74 68 65 20 61 48 61 73 68 20 72 65 70 72  r the aHash repr
f1800 65 73 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 45  esentation..** E
f1810 6d 70 69 72 69 63 61 6c 20 74 65 73 74 69 6e 67  mpirical testing
f1820 20 73 68 6f 77 65 64 20 74 68 61 74 20 74 68 65   showed that the
f1830 20 2a 33 37 20 6d 75 6c 74 69 70 6c 69 65 72 20   *37 multiplier 
f1840 0a 2a 2a 20 28 61 6e 20 61 72 62 69 74 72 61 72  .** (an arbitrar
f1850 79 20 70 72 69 6d 65 29 69 6e 20 74 68 65 20 68  y prime)in the h
f1860 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 70 72 6f  ash function pro
f1870 76 69 64 65 64 20 0a 2a 2a 20 6e 6f 20 66 65 77  vided .** no few
f1880 65 72 20 63 6f 6c 6c 69 73 69 6f 6e 73 20 74 68  er collisions th
f1890 61 6e 20 74 68 65 20 6e 6f 2d 6f 70 20 2a 31 2e  an the no-op *1.
f18a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 42 49 54 56   */.#define BITV
f18b0 45 43 5f 48 41 53 48 28 58 29 20 20 20 28 28 28  EC_HASH(X)   (((
f18c0 58 29 2a 31 29 25 42 49 54 56 45 43 5f 4e 49 4e  X)*1)%BITVEC_NIN
f18d0 54 29 0a 0a 23 64 65 66 69 6e 65 20 42 49 54 56  T)..#define BITV
f18e0 45 43 5f 4e 50 54 52 20 20 20 20 20 20 28 42 49  EC_NPTR      (BI
f18f0 54 56 45 43 5f 55 53 49 5a 45 2f 73 69 7a 65 6f  TVEC_USIZE/sizeo
f1900 66 28 42 69 74 76 65 63 20 2a 29 29 0a 0a 0a 2f  f(Bitvec *)).../
f1910 2a 0a 2a 2a 20 41 20 62 69 74 6d 61 70 20 69 73  *.** A bitmap is
f1920 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
f1930 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74  the following st
f1940 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54  ructure..**.** T
f1950 68 69 73 20 62 69 74 6d 61 70 20 72 65 63 6f 72  his bitmap recor
f1960 64 73 20 74 68 65 20 65 78 69 73 74 61 6e 63 65  ds the existance
f1970 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65   of zero or more
f1980 20 62 69 74 73 0a 2a 2a 20 77 69 74 68 20 76 61   bits.** with va
f1990 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31 20 61  lues between 1 a
f19a0 6e 64 20 69 53 69 7a 65 2c 20 69 6e 63 6c 75 73  nd iSize, inclus
f19b0 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ive..**.** There
f19c0 20 61 72 65 20 74 68 72 65 65 20 70 6f 73 73 69   are three possi
f19d0 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74 69  ble representati
f19e0 6f 6e 73 20 6f 66 20 74 68 65 20 62 69 74 6d 61  ons of the bitma
f19f0 70 2e 0a 2a 2a 20 49 66 20 69 53 69 7a 65 3c 3d  p..** If iSize<=
f1a00 42 49 54 56 45 43 5f 4e 42 49 54 2c 20 74 68 65  BITVEC_NBIT, the
f1a10 6e 20 42 69 74 76 65 63 2e 75 2e 61 42 69 74 6d  n Bitvec.u.aBitm
f1a20 61 70 5b 5d 20 69 73 20 61 20 73 74 72 61 69 67  ap[] is a straig
f1a30 68 74 0a 2a 2a 20 62 69 74 6d 61 70 2e 20 20 54  ht.** bitmap.  T
f1a40 68 65 20 6c 65 61 73 74 20 73 69 67 6e 69 66 69  he least signifi
f1a50 63 61 6e 74 20 62 69 74 20 69 73 20 62 69 74 20  cant bit is bit 
f1a60 31 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69 53 69 7a  1..**.** If iSiz
f1a70 65 3e 42 49 54 56 45 43 5f 4e 42 49 54 20 61 6e  e>BITVEC_NBIT an
f1a80 64 20 69 44 69 76 69 73 6f 72 3d 3d 30 20 74 68  d iDivisor==0 th
f1a90 65 6e 20 42 69 74 76 65 63 2e 75 2e 61 48 61 73  en Bitvec.u.aHas
f1aa0 68 5b 5d 20 69 73 0a 2a 2a 20 61 20 68 61 73 68  h[] is.** a hash
f1ab0 20 74 61 62 6c 65 20 74 68 61 74 20 77 69 6c 6c   table that will
f1ac0 20 68 6f 6c 64 20 75 70 20 74 6f 20 42 49 54 56   hold up to BITV
f1ad0 45 43 5f 4d 58 48 41 53 48 20 64 69 73 74 69 6e  EC_MXHASH distin
f1ae0 63 74 20 76 61 6c 75 65 73 2e 0a 2a 2a 0a 2a 2a  ct values..**.**
f1af0 20 4f 74 68 65 72 77 69 73 65 2c 20 74 68 65 20   Otherwise, the 
f1b00 76 61 6c 75 65 20 69 20 69 73 20 72 65 64 69 72  value i is redir
f1b10 65 63 74 65 64 20 69 6e 74 6f 20 6f 6e 65 20 6f  ected into one o
f1b20 66 20 42 49 54 56 45 43 5f 4e 50 54 52 0a 2a 2a  f BITVEC_NPTR.**
f1b30 20 73 75 62 2d 62 69 74 6d 61 70 73 20 70 6f 69   sub-bitmaps poi
f1b40 6e 74 65 64 20 74 6f 20 62 79 20 42 69 74 76 65  nted to by Bitve
f1b50 63 2e 75 2e 61 70 53 75 62 5b 5d 2e 20 20 45 61  c.u.apSub[].  Ea
f1b60 63 68 20 73 75 62 62 69 74 6d 61 70 0a 2a 2a 20  ch subbitmap.** 
f1b70 68 61 6e 64 6c 65 73 20 75 70 20 74 6f 20 69 44  handles up to iD
f1b80 69 76 69 73 6f 72 20 73 65 70 61 72 61 74 65 20  ivisor separate 
f1b90 76 61 6c 75 65 73 20 6f 66 20 69 2e 20 20 61 70  values of i.  ap
f1ba0 53 75 62 5b 30 5d 20 68 6f 6c 64 73 0a 2a 2a 20  Sub[0] holds.** 
f1bb0 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e 20 31  values between 1
f1bc0 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e 20 20   and iDivisor.  
f1bd0 61 70 53 75 62 5b 31 5d 20 68 6f 6c 64 73 20 76  apSub[1] holds v
f1be0 61 6c 75 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a  alues between.**
f1bf0 20 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20   iDivisor+1 and 
f1c00 32 2a 69 44 69 76 69 73 6f 72 2e 20 20 61 70 53  2*iDivisor.  apS
f1c10 75 62 5b 4e 5d 20 68 6f 6c 64 73 20 76 61 6c 75  ub[N] holds valu
f1c20 65 73 20 62 65 74 77 65 65 6e 0a 2a 2a 20 4e 2a  es between.** N*
f1c30 69 44 69 76 69 73 6f 72 2b 31 20 61 6e 64 20 28  iDivisor+1 and (
f1c40 4e 2b 31 29 2a 69 44 69 76 69 73 6f 72 2e 20 20  N+1)*iDivisor.  
f1c50 45 61 63 68 20 73 75 62 62 69 74 6d 61 70 20 69  Each subbitmap i
f1c60 73 20 6e 6f 72 6d 61 6c 69 7a 65 64 0a 2a 2a 20  s normalized.** 
f1c70 74 6f 20 68 6f 6c 64 20 64 65 61 6c 20 77 69 74  to hold deal wit
f1c80 68 20 76 61 6c 75 65 73 20 62 65 74 77 65 65 6e  h values between
f1c90 20 31 20 61 6e 64 20 69 44 69 76 69 73 6f 72 2e   1 and iDivisor.
f1ca0 0a 2a 2f 0a 73 74 72 75 63 74 20 42 69 74 76 65  .*/.struct Bitve
f1cb0 63 20 7b 0a 20 20 75 33 32 20 69 53 69 7a 65 3b  c {.  u32 iSize;
f1cc0 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d        /* Maximum
f1cd0 20 62 69 74 20 69 6e 64 65 78 2e 20 20 4d 61 78   bit index.  Max
f1ce0 20 69 53 69 7a 65 20 69 73 20 34 2c 32 39 34 2c   iSize is 4,294,
f1cf0 39 36 37 2c 32 39 36 2e 20 2a 2f 0a 20 20 75 33  967,296. */.  u3
f1d00 32 20 6e 53 65 74 3b 20 20 20 20 20 20 20 2f 2a  2 nSet;       /*
f1d10 20 4e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20   Number of bits 
f1d20 74 68 61 74 20 61 72 65 20 73 65 74 20 2d 20 6f  that are set - o
f1d30 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20 61 48  nly valid for aH
f1d40 61 73 68 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  ash element */. 
f1d50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1d60 20 2f 2a 20 4d 61 78 20 6e 53 65 74 20 69 73 20   /* Max nSet is 
f1d70 42 49 54 56 45 43 5f 4e 49 4e 54 2e 20 20 46 6f  BITVEC_NINT.  Fo
f1d80 72 20 42 49 54 56 45 43 5f 53 5a 20 6f 66 20 35  r BITVEC_SZ of 5
f1d90 31 32 2c 20 74 68 69 73 20 77 6f 75 6c 64 20 62  12, this would b
f1da0 65 20 31 32 35 2e 20 2a 2f 0a 20 20 75 33 32 20  e 125. */.  u32 
f1db0 69 44 69 76 69 73 6f 72 3b 20 20 20 2f 2a 20 4e  iDivisor;   /* N
f1dc0 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 68 61  umber of bits ha
f1dd0 6e 64 6c 65 64 20 62 79 20 65 61 63 68 20 61 70  ndled by each ap
f1de0 53 75 62 5b 5d 20 65 6e 74 72 79 2e 20 2a 2f 0a  Sub[] entry. */.
f1df0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f1e00 20 20 2f 2a 20 53 68 6f 75 6c 64 20 3e 3d 30 20    /* Should >=0 
f1e10 66 6f 72 20 61 70 53 75 62 20 65 6c 65 6d 65 6e  for apSub elemen
f1e20 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  t. */.          
f1e30 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 20 69          /* Max i
f1e40 44 69 76 69 73 6f 72 20 69 73 20 6d 61 78 28 75  Divisor is max(u
f1e50 33 32 29 20 2f 20 42 49 54 56 45 43 5f 4e 50 54  32) / BITVEC_NPT
f1e60 52 20 2b 20 31 2e 20 20 2a 2f 0a 20 20 20 20 20  R + 1.  */.     
f1e70 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f1e80 46 6f 72 20 61 20 42 49 54 56 45 43 5f 53 5a 20  For a BITVEC_SZ 
f1e90 6f 66 20 35 31 32 2c 20 74 68 69 73 20 77 6f 75  of 512, this wou
f1ea0 6c 64 20 62 65 20 33 34 2c 33 35 39 2c 37 33 39  ld be 34,359,739
f1eb0 2e 20 2a 2f 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20  . */.  union {. 
f1ec0 20 20 20 42 49 54 56 45 43 5f 54 45 4c 45 4d 20     BITVEC_TELEM 
f1ed0 61 42 69 74 6d 61 70 5b 42 49 54 56 45 43 5f 4e  aBitmap[BITVEC_N
f1ee0 45 4c 45 4d 5d 3b 20 20 20 20 2f 2a 20 42 69 74  ELEM];    /* Bit
f1ef0 6d 61 70 20 72 65 70 72 65 73 65 6e 74 61 74 69  map representati
f1f00 6f 6e 20 2a 2f 0a 20 20 20 20 75 33 32 20 61 48  on */.    u32 aH
f1f10 61 73 68 5b 42 49 54 56 45 43 5f 4e 49 4e 54 5d  ash[BITVEC_NINT]
f1f20 3b 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 74  ;      /* Hash t
f1f30 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74  able representat
f1f40 69 6f 6e 20 2a 2f 0a 20 20 20 20 42 69 74 76 65  ion */.    Bitve
f1f50 63 20 2a 61 70 53 75 62 5b 42 49 54 56 45 43 5f  c *apSub[BITVEC_
f1f60 4e 50 54 52 5d 3b 20 20 2f 2a 20 52 65 63 75 72  NPTR];  /* Recur
f1f70 73 69 76 65 20 72 65 70 72 65 73 65 6e 74 61 74  sive representat
f1f80 69 6f 6e 20 2a 2f 0a 20 20 7d 20 75 3b 0a 7d 3b  ion */.  } u;.};
f1f90 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61  ../*.** Create a
f1fa0 20 6e 65 77 20 62 69 74 6d 61 70 20 6f 62 6a 65   new bitmap obje
f1fb0 63 74 20 61 62 6c 65 20 74 6f 20 68 61 6e 64 6c  ct able to handl
f1fc0 65 20 62 69 74 73 20 62 65 74 77 65 65 6e 20 30  e bits between 0
f1fd0 20 61 6e 64 20 69 53 69 7a 65 2c 0a 2a 2a 20 69   and iSize,.** i
f1fe0 6e 63 6c 75 73 69 76 65 2e 20 20 52 65 74 75 72  nclusive.  Retur
f1ff0 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
f2000 68 65 20 6e 65 77 20 6f 62 6a 65 63 74 2e 20 20  he new object.  
f2010 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 0a  Return NULL if .
f2020 2a 2a 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e  ** malloc fails.
f2030 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f2040 54 45 20 42 69 74 76 65 63 20 2a 73 71 6c 69 74  TE Bitvec *sqlit
f2050 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28 75  e3BitvecCreate(u
f2060 33 32 20 69 53 69 7a 65 29 7b 0a 20 20 42 69 74  32 iSize){.  Bit
f2070 76 65 63 20 2a 70 3b 0a 20 20 61 73 73 65 72 74  vec *p;.  assert
f2080 28 20 73 69 7a 65 6f 66 28 2a 70 29 3d 3d 42 49  ( sizeof(*p)==BI
f2090 54 56 45 43 5f 53 5a 20 29 3b 0a 20 20 70 20 3d  TVEC_SZ );.  p =
f20a0 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65   sqlite3MallocZe
f20b0 72 6f 28 20 73 69 7a 65 6f 66 28 2a 70 29 20 29  ro( sizeof(*p) )
f20c0 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
f20d0 20 70 2d 3e 69 53 69 7a 65 20 3d 20 69 53 69 7a   p->iSize = iSiz
f20e0 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
f20f0 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63  p;.}../*.** Chec
f2100 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 65 20  k to see if the 
f2110 69 2d 74 68 20 62 69 74 20 69 73 20 73 65 74 2e  i-th bit is set.
f2120 20 20 52 65 74 75 72 6e 20 74 72 75 65 20 6f 72    Return true or
f2130 20 66 61 6c 73 65 2e 0a 2a 2a 20 49 66 20 70 20   false..** If p 
f2140 69 73 20 4e 55 4c 4c 20 28 69 66 20 74 68 65 20  is NULL (if the 
f2150 62 69 74 6d 61 70 20 68 61 73 20 6e 6f 74 20 62  bitmap has not b
f2160 65 65 6e 20 63 72 65 61 74 65 64 29 20 6f 72 20  een created) or 
f2170 69 66 0a 2a 2a 20 69 20 69 73 20 6f 75 74 20 6f  if.** i is out o
f2180 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 72 65  f range, then re
f2190 74 75 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 53  turn false..*/.S
f21a0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
f21b0 74 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 54  t sqlite3BitvecT
f21c0 65 73 74 28 42 69 74 76 65 63 20 2a 70 2c 20 75  est(Bitvec *p, u
f21d0 33 32 20 69 29 7b 0a 20 20 69 66 28 20 70 3d 3d  32 i){.  if( p==
f21e0 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
f21f0 69 66 28 20 69 3e 70 2d 3e 69 53 69 7a 65 20 7c  if( i>p->iSize |
f2200 7c 20 69 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  | i==0 ) return 
f2210 30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c  0;.  i--;.  whil
f2220 65 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29  e( p->iDivisor )
f2230 7b 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20  {.    u32 bin = 
f2240 69 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20  i/p->iDivisor;. 
f2250 20 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76     i = i%p->iDiv
f2260 69 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d  isor;.    p = p-
f2270 3e 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20  >u.apSub[bin];. 
f2280 20 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20     if (!p) {.   
f2290 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20     return 0;.   
f22a0 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e   }.  }.  if( p->
f22b0 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42  iSize<=BITVEC_NB
f22c0 49 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  IT ){.    return
f22d0 20 28 70 2d 3e 75 2e 61 42 69 74 6d 61 70 5b 69   (p->u.aBitmap[i
f22e0 2f 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d 5d 20  /BITVEC_SZELEM] 
f22f0 26 20 28 31 3c 3c 28 69 26 28 42 49 54 56 45 43  & (1<<(i&(BITVEC
f2300 5f 53 5a 45 4c 45 4d 2d 31 29 29 29 29 21 3d 30  _SZELEM-1))))!=0
f2310 3b 0a 20 20 7d 20 65 6c 73 65 7b 0a 20 20 20 20  ;.  } else{.    
f2320 75 33 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48  u32 h = BITVEC_H
f2330 41 53 48 28 69 2b 2b 29 3b 0a 20 20 20 20 77 68  ASH(i++);.    wh
f2340 69 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b  ile( p->u.aHash[
f2350 68 5d 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  h] ){.      if( 
f2360 70 2d 3e 75 2e 61 48 61 73 68 5b 68 5d 3d 3d 69  p->u.aHash[h]==i
f2370 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20   ) return 1;.   
f2380 20 20 20 68 2b 2b 3b 0a 20 20 20 20 20 20 69 66     h++;.      if
f2390 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54  ( h>=BITVEC_NINT
f23a0 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 7d 0a   ) h = 0;.    }.
f23b0 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
f23c0 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  }.}../*.** Set t
f23d0 68 65 20 69 2d 74 68 20 62 69 74 2e 20 20 52 65  he i-th bit.  Re
f23e0 74 75 72 6e 20 30 20 6f 6e 20 73 75 63 63 65 73  turn 0 on succes
f23f0 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63  s and an error c
f2400 6f 64 65 20 69 66 0a 2a 2a 20 61 6e 79 74 68 69  ode if.** anythi
f2410 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a  ng goes wrong..*
f2420 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
f2430 65 20 6d 69 67 68 74 20 63 61 75 73 65 20 73 75  e might cause su
f2440 62 2d 62 69 74 6d 61 70 73 20 74 6f 20 62 65 20  b-bitmaps to be 
f2450 61 6c 6c 6f 63 61 74 65 64 2e 20 20 46 61 69 6c  allocated.  Fail
f2460 69 6e 67 0a 2a 2a 20 74 6f 20 67 65 74 20 74 68  ing.** to get th
f2470 65 20 6d 65 6d 6f 72 79 20 6e 65 65 64 65 64 20  e memory needed 
f2480 74 6f 20 68 6f 6c 64 20 74 68 65 20 73 75 62 2d  to hold the sub-
f2490 62 69 74 6d 61 70 20 69 73 20 74 68 65 20 6f 6e  bitmap is the on
f24a0 6c 79 0a 2a 2a 20 74 68 61 74 20 63 61 6e 20 67  ly.** that can g
f24b0 6f 20 77 72 6f 6e 67 20 77 69 74 68 20 61 6e 20  o wrong with an 
f24c0 69 6e 73 65 72 74 2c 20 61 73 73 75 6d 69 6e 67  insert, assuming
f24d0 20 70 20 61 6e 64 20 69 20 61 72 65 20 76 61 6c   p and i are val
f24e0 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  id..**.** The ca
f24f0 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 6d  lling function m
f2500 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20  ust ensure that 
f2510 70 20 69 73 20 61 20 76 61 6c 69 64 20 42 69 74  p is a valid Bit
f2520 76 65 63 20 6f 62 6a 65 63 74 0a 2a 2a 20 61 6e  vec object.** an
f2530 64 20 74 68 61 74 20 74 68 65 20 76 61 6c 75 65  d that the value
f2540 20 66 6f 72 20 22 69 22 20 69 73 20 77 69 74 68   for "i" is with
f2550 69 6e 20 72 61 6e 67 65 20 6f 66 20 74 68 65 20  in range of the 
f2560 42 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 0a 2a  Bitvec object..*
f2570 2a 20 4f 74 68 65 72 77 69 73 65 20 74 68 65 20  * Otherwise the 
f2580 62 65 68 61 76 69 6f 72 20 69 73 20 75 6e 64 65  behavior is unde
f2590 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  fined..*/.SQLITE
f25a0 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
f25b0 69 74 65 33 42 69 74 76 65 63 53 65 74 28 42 69  ite3BitvecSet(Bi
f25c0 74 76 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b  tvec *p, u32 i){
f25d0 0a 20 20 75 33 32 20 68 3b 0a 20 20 61 73 73 65  .  u32 h;.  asse
f25e0 72 74 28 20 70 21 3d 30 20 29 3b 0a 20 20 61 73  rt( p!=0 );.  as
f25f0 73 65 72 74 28 20 69 3e 30 20 29 3b 0a 20 20 61  sert( i>0 );.  a
f2600 73 73 65 72 74 28 20 69 3c 3d 70 2d 3e 69 53 69  ssert( i<=p->iSi
f2610 7a 65 20 29 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77  ze );.  i--;.  w
f2620 68 69 6c 65 28 28 70 2d 3e 69 53 69 7a 65 20 3e  hile((p->iSize >
f2630 20 42 49 54 56 45 43 5f 4e 42 49 54 29 20 26 26   BITVEC_NBIT) &&
f2640 20 70 2d 3e 69 44 69 76 69 73 6f 72 29 20 7b 0a   p->iDivisor) {.
f2650 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69 2f      u32 bin = i/
f2660 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20 20  p->iDivisor;.   
f2670 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69 73   i = i%p->iDivis
f2680 6f 72 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75  or;.    if( p->u
f2690 2e 61 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29  .apSub[bin]==0 )
f26a0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
f26b0 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
f26c0 28 29 3b 0a 20 20 20 20 20 20 70 2d 3e 75 2e 61  ();.      p->u.a
f26d0 70 53 75 62 5b 62 69 6e 5d 20 3d 20 73 71 6c 69  pSub[bin] = sqli
f26e0 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
f26f0 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 3b 0a   p->iDivisor );.
f2700 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 6e 64        sqlite3End
f2710 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
f2720 20 20 20 20 20 20 69 66 28 20 70 2d 3e 75 2e 61        if( p->u.a
f2730 70 53 75 62 5b 62 69 6e 5d 3d 3d 30 20 29 20 72  pSub[bin]==0 ) r
f2740 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
f2750 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20  EM;.    }.    p 
f2760 3d 20 70 2d 3e 75 2e 61 70 53 75 62 5b 62 69 6e  = p->u.apSub[bin
f2770 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  ];.  }.  if( p->
f2780 69 53 69 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42  iSize<=BITVEC_NB
f2790 49 54 20 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61  IT ){.    p->u.a
f27a0 42 69 74 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f  Bitmap[i/BITVEC_
f27b0 53 5a 45 4c 45 4d 5d 20 7c 3d 20 31 20 3c 3c 20  SZELEM] |= 1 << 
f27c0 28 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45  (i&(BITVEC_SZELE
f27d0 4d 2d 31 29 29 3b 0a 20 20 20 20 72 65 74 75 72  M-1));.    retur
f27e0 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
f27f0 0a 20 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41  .  h = BITVEC_HA
f2800 53 48 28 69 2b 2b 29 3b 0a 20 20 2f 2a 20 69 66  SH(i++);.  /* if
f2810 20 74 68 65 72 65 20 77 61 73 6e 27 74 20 61 20   there wasn't a 
f2820 68 61 73 68 20 63 6f 6c 6c 69 73 69 6f 6e 2c 20  hash collision, 
f2830 61 6e 64 20 74 68 69 73 20 64 6f 65 73 6e 27 74  and this doesn't
f2840 20 2a 2f 0a 20 20 2f 2a 20 63 6f 6d 70 6c 65 74   */.  /* complet
f2850 65 6c 79 20 66 69 6c 6c 20 74 68 65 20 68 61 73  ely fill the has
f2860 68 2c 20 74 68 65 6e 20 6a 75 73 74 20 61 64 64  h, then just add
f2870 20 69 74 20 77 69 74 68 6f 75 74 20 2a 2f 0a 20   it without */. 
f2880 20 2f 2a 20 77 6f 72 72 69 6e 67 20 61 62 6f 75   /* worring abou
f2890 74 20 73 75 62 2d 64 69 76 69 64 69 6e 67 20 61  t sub-dividing a
f28a0 6e 64 20 72 65 2d 68 61 73 68 69 6e 67 2e 20 2a  nd re-hashing. *
f28b0 2f 0a 20 20 69 66 28 20 21 70 2d 3e 75 2e 61 48  /.  if( !p->u.aH
f28c0 61 73 68 5b 68 5d 20 29 7b 0a 20 20 20 20 69 66  ash[h] ){.    if
f28d0 20 28 70 2d 3e 6e 53 65 74 3c 28 42 49 54 56 45   (p->nSet<(BITVE
f28e0 43 5f 4e 49 4e 54 2d 31 29 29 20 7b 0a 20 20 20  C_NINT-1)) {.   
f28f0 20 20 20 67 6f 74 6f 20 62 69 74 76 65 63 5f 73     goto bitvec_s
f2900 65 74 5f 65 6e 64 3b 0a 20 20 20 20 7d 20 65 6c  et_end;.    } el
f2910 73 65 20 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20  se {.      goto 
f2920 62 69 74 76 65 63 5f 73 65 74 5f 72 65 68 61 73  bitvec_set_rehas
f2930 68 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f  h;.    }.  }.  /
f2940 2a 20 74 68 65 72 65 20 77 61 73 20 61 20 63 6f  * there was a co
f2950 6c 6c 69 73 69 6f 6e 2c 20 63 68 65 63 6b 20 74  llision, check t
f2960 6f 20 73 65 65 20 69 66 20 69 74 27 73 20 61 6c  o see if it's al
f2970 72 65 61 64 79 20 2a 2f 0a 20 20 2f 2a 20 69 6e  ready */.  /* in
f2980 20 68 61 73 68 2c 20 69 66 20 6e 6f 74 2c 20 74   hash, if not, t
f2990 72 79 20 74 6f 20 66 69 6e 64 20 61 20 73 70 6f  ry to find a spo
f29a0 74 20 66 6f 72 20 69 74 20 2a 2f 0a 20 20 64 6f  t for it */.  do
f29b0 20 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 2e   {.    if( p->u.
f29c0 61 48 61 73 68 5b 68 5d 3d 3d 69 20 29 20 72 65  aHash[h]==i ) re
f29d0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
f29e0 20 20 20 20 68 2b 2b 3b 0a 20 20 20 20 69 66 28      h++;.    if(
f29f0 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54 20   h>=BITVEC_NINT 
f2a00 29 20 68 20 3d 20 30 3b 0a 20 20 7d 20 77 68 69  ) h = 0;.  } whi
f2a10 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  le( p->u.aHash[h
f2a20 5d 20 29 3b 0a 20 20 2f 2a 20 77 65 20 64 69 64  ] );.  /* we did
f2a30 6e 27 74 20 66 69 6e 64 20 69 74 20 69 6e 20 74  n't find it in t
f2a40 68 65 20 68 61 73 68 2e 20 20 68 20 70 6f 69 6e  he hash.  h poin
f2a50 74 73 20 74 6f 20 74 68 65 20 66 69 72 73 74 20  ts to the first 
f2a60 2a 2f 0a 20 20 2f 2a 20 61 76 61 69 6c 61 62 6c  */.  /* availabl
f2a70 65 20 66 72 65 65 20 73 70 6f 74 2e 20 63 68 65  e free spot. che
f2a80 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68 69  ck to see if thi
f2a90 73 20 69 73 20 67 6f 69 6e 67 20 74 6f 20 2a 2f  s is going to */
f2aa0 0a 20 20 2f 2a 20 6d 61 6b 65 20 6f 75 72 20 68  .  /* make our h
f2ab0 61 73 68 20 74 6f 6f 20 22 66 75 6c 6c 22 2e 20  ash too "full". 
f2ac0 20 2a 2f 0a 62 69 74 76 65 63 5f 73 65 74 5f 72   */.bitvec_set_r
f2ad0 65 68 61 73 68 3a 0a 20 20 69 66 28 20 70 2d 3e  ehash:.  if( p->
f2ae0 6e 53 65 74 3e 3d 42 49 54 56 45 43 5f 4d 58 48  nSet>=BITVEC_MXH
f2af0 41 53 48 20 29 7b 0a 20 20 20 20 75 6e 73 69 67  ASH ){.    unsig
f2b00 6e 65 64 20 69 6e 74 20 6a 3b 0a 20 20 20 20 69  ned int j;.    i
f2b10 6e 74 20 72 63 3b 0a 20 20 20 20 75 33 32 20 61  nt rc;.    u32 a
f2b20 69 56 61 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e  iValues[BITVEC_N
f2b30 49 4e 54 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79  INT];.    memcpy
f2b40 28 61 69 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e  (aiValues, p->u.
f2b50 61 48 61 73 68 2c 20 73 69 7a 65 6f 66 28 61 69  aHash, sizeof(ai
f2b60 56 61 6c 75 65 73 29 29 3b 0a 20 20 20 20 6d 65  Values));.    me
f2b70 6d 73 65 74 28 70 2d 3e 75 2e 61 70 53 75 62 2c  mset(p->u.apSub,
f2b80 20 30 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c   0, sizeof(aiVal
f2b90 75 65 73 29 29 3b 0a 20 20 20 20 70 2d 3e 69 44  ues));.    p->iD
f2ba0 69 76 69 73 6f 72 20 3d 20 28 70 2d 3e 69 53 69  ivisor = (p->iSi
f2bb0 7a 65 20 2b 20 42 49 54 56 45 43 5f 4e 50 54 52  ze + BITVEC_NPTR
f2bc0 20 2d 20 31 29 2f 42 49 54 56 45 43 5f 4e 50 54   - 1)/BITVEC_NPT
f2bd0 52 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  R;.    rc = sqli
f2be0 74 65 33 42 69 74 76 65 63 53 65 74 28 70 2c 20  te3BitvecSet(p, 
f2bf0 69 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b  i);.    for(j=0;
f2c00 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b 20   j<BITVEC_NINT; 
f2c10 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  j++){.      if( 
f2c20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 29 20 72 63  aiValues[j] ) rc
f2c30 20 7c 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65   |= sqlite3Bitve
f2c40 63 53 65 74 28 70 2c 20 61 69 56 61 6c 75 65 73  cSet(p, aiValues
f2c50 5b 6a 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  [j]);.    }.    
f2c60 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 62  return rc;.  }.b
f2c70 69 74 76 65 63 5f 73 65 74 5f 65 6e 64 3a 0a 20  itvec_set_end:. 
f2c80 20 70 2d 3e 6e 53 65 74 2b 2b 3b 0a 20 20 70 2d   p->nSet++;.  p-
f2c90 3e 75 2e 61 48 61 73 68 5b 68 5d 20 3d 20 69 3b  >u.aHash[h] = i;
f2ca0 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
f2cb0 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c  _OK;.}../*.** Cl
f2cc0 65 61 72 20 74 68 65 20 69 2d 74 68 20 62 69 74  ear the i-th bit
f2cd0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f2ce0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f2cf0 42 69 74 76 65 63 43 6c 65 61 72 28 42 69 74 76  BitvecClear(Bitv
f2d00 65 63 20 2a 70 2c 20 75 33 32 20 69 29 7b 0a 20  ec *p, u32 i){. 
f2d10 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b   assert( p!=0 );
f2d20 0a 20 20 61 73 73 65 72 74 28 20 69 3e 30 20 29  .  assert( i>0 )
f2d30 3b 0a 20 20 69 2d 2d 3b 0a 20 20 77 68 69 6c 65  ;.  i--;.  while
f2d40 28 20 70 2d 3e 69 44 69 76 69 73 6f 72 20 29 7b  ( p->iDivisor ){
f2d50 0a 20 20 20 20 75 33 32 20 62 69 6e 20 3d 20 69  .    u32 bin = i
f2d60 2f 70 2d 3e 69 44 69 76 69 73 6f 72 3b 0a 20 20  /p->iDivisor;.  
f2d70 20 20 69 20 3d 20 69 25 70 2d 3e 69 44 69 76 69    i = i%p->iDivi
f2d80 73 6f 72 3b 0a 20 20 20 20 70 20 3d 20 70 2d 3e  sor;.    p = p->
f2d90 75 2e 61 70 53 75 62 5b 62 69 6e 5d 3b 0a 20 20  u.apSub[bin];.  
f2da0 20 20 69 66 20 28 21 70 29 20 7b 0a 20 20 20 20    if (!p) {.    
f2db0 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
f2dc0 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 69 53 69    }.  if( p->iSi
f2dd0 7a 65 3c 3d 42 49 54 56 45 43 5f 4e 42 49 54 20  ze<=BITVEC_NBIT 
f2de0 29 7b 0a 20 20 20 20 70 2d 3e 75 2e 61 42 69 74  ){.    p->u.aBit
f2df0 6d 61 70 5b 69 2f 42 49 54 56 45 43 5f 53 5a 45  map[i/BITVEC_SZE
f2e00 4c 45 4d 5d 20 26 3d 20 7e 28 31 20 3c 3c 20 28  LEM] &= ~(1 << (
f2e10 69 26 28 42 49 54 56 45 43 5f 53 5a 45 4c 45 4d  i&(BITVEC_SZELEM
f2e20 2d 31 29 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  -1)));.  }else{.
f2e30 20 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74      unsigned int
f2e40 20 6a 3b 0a 20 20 20 20 75 33 32 20 61 69 56 61   j;.    u32 aiVa
f2e50 6c 75 65 73 5b 42 49 54 56 45 43 5f 4e 49 4e 54  lues[BITVEC_NINT
f2e60 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 61 69  ];.    memcpy(ai
f2e70 56 61 6c 75 65 73 2c 20 70 2d 3e 75 2e 61 48 61  Values, p->u.aHa
f2e80 73 68 2c 20 73 69 7a 65 6f 66 28 61 69 56 61 6c  sh, sizeof(aiVal
f2e90 75 65 73 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65  ues));.    memse
f2ea0 74 28 70 2d 3e 75 2e 61 48 61 73 68 2c 20 30 2c  t(p->u.aHash, 0,
f2eb0 20 73 69 7a 65 6f 66 28 61 69 56 61 6c 75 65 73   sizeof(aiValues
f2ec0 29 29 3b 0a 20 20 20 20 70 2d 3e 6e 53 65 74 20  ));.    p->nSet 
f2ed0 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 30  = 0;.    for(j=0
f2ee0 3b 20 6a 3c 42 49 54 56 45 43 5f 4e 49 4e 54 3b  ; j<BITVEC_NINT;
f2ef0 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28   j++){.      if(
f2f00 20 61 69 56 61 6c 75 65 73 5b 6a 5d 20 26 26 20   aiValues[j] && 
f2f10 61 69 56 61 6c 75 65 73 5b 6a 5d 21 3d 28 69 2b  aiValues[j]!=(i+
f2f20 31 29 20 29 7b 0a 20 20 20 20 20 20 20 20 75 33  1) ){.        u3
f2f30 32 20 68 20 3d 20 42 49 54 56 45 43 5f 48 41 53  2 h = BITVEC_HAS
f2f40 48 28 61 69 56 61 6c 75 65 73 5b 6a 5d 2d 31 29  H(aiValues[j]-1)
f2f50 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 6e 53 65  ;.        p->nSe
f2f60 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69  t++;.        whi
f2f70 6c 65 28 20 70 2d 3e 75 2e 61 48 61 73 68 5b 68  le( p->u.aHash[h
f2f80 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 68  ] ){.          h
f2f90 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  ++;.          if
f2fa0 28 20 68 3e 3d 42 49 54 56 45 43 5f 4e 49 4e 54  ( h>=BITVEC_NINT
f2fb0 20 29 20 68 20 3d 20 30 3b 0a 20 20 20 20 20 20   ) h = 0;.      
f2fc0 20 20 7d 0a 20 20 20 20 20 20 20 20 70 2d 3e 75    }.        p->u
f2fd0 2e 61 48 61 73 68 5b 68 5d 20 3d 20 61 69 56 61  .aHash[h] = aiVa
f2fe0 6c 75 65 73 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d  lues[j];.      }
f2ff0 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
f3000 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 62 69  .** Destroy a bi
f3010 74 6d 61 70 20 6f 62 6a 65 63 74 2e 20 20 52 65  tmap object.  Re
f3020 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f 72 79  claim all memory
f3030 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45   used..*/.SQLITE
f3040 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
f3050 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
f3060 6f 79 28 42 69 74 76 65 63 20 2a 70 29 7b 0a 20  oy(Bitvec *p){. 
f3070 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75   if( p==0 ) retu
f3080 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 69 44 69  rn;.  if( p->iDi
f3090 76 69 73 6f 72 20 29 7b 0a 20 20 20 20 75 6e 73  visor ){.    uns
f30a0 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a 20 20 20  igned int i;.   
f30b0 20 66 6f 72 28 69 3d 30 3b 20 69 3c 42 49 54 56   for(i=0; i<BITV
f30c0 45 43 5f 4e 50 54 52 3b 20 69 2b 2b 29 7b 0a 20  EC_NPTR; i++){. 
f30d0 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
f30e0 65 63 44 65 73 74 72 6f 79 28 70 2d 3e 75 2e 61  ecDestroy(p->u.a
f30f0 70 53 75 62 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a  pSub[i]);.    }.
f3100 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72    }.  sqlite3_fr
f3110 65 65 28 70 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  ee(p);.}..#ifnde
f3120 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 55  f SQLITE_OMIT_BU
f3130 49 4c 54 49 4e 5f 54 45 53 54 0a 2f 2a 0a 2a 2a  ILTIN_TEST./*.**
f3140 20 4c 65 74 20 56 5b 5d 20 62 65 20 61 6e 20 61   Let V[] be an a
f3150 72 72 61 79 20 6f 66 20 75 6e 73 69 67 6e 65 64  rray of unsigned
f3160 20 63 68 61 72 61 63 74 65 72 73 20 73 75 66 66   characters suff
f3170 69 63 69 65 6e 74 20 74 6f 20 68 6f 6c 64 0a 2a  icient to hold.*
f3180 2a 20 75 70 20 74 6f 20 4e 20 62 69 74 73 2e 20  * up to N bits. 
f3190 20 4c 65 74 20 49 20 62 65 20 61 6e 20 69 6e 74   Let I be an int
f31a0 65 67 65 72 20 62 65 74 77 65 65 6e 20 30 20 61  eger between 0 a
f31b0 6e 64 20 4e 2e 20 20 30 3c 3d 49 3c 4e 2e 0a 2a  nd N.  0<=I<N..*
f31c0 2a 20 54 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f  * Then the follo
f31d0 77 69 6e 67 20 6d 61 63 72 6f 73 20 63 61 6e 20  wing macros can 
f31e0 62 65 20 75 73 65 64 20 74 6f 20 73 65 74 2c 20  be used to set, 
f31f0 63 6c 65 61 72 2c 20 6f 72 20 74 65 73 74 0a 2a  clear, or test.*
f3200 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 62 69 74  * individual bit
f3210 73 20 77 69 74 68 69 6e 20 56 2e 0a 2a 2f 0a 23  s within V..*/.#
f3220 64 65 66 69 6e 65 20 53 45 54 42 49 54 28 56 2c  define SETBIT(V,
f3230 49 29 20 20 20 20 20 20 56 5b 49 3e 3e 33 5d 20  I)      V[I>>3] 
f3240 7c 3d 20 28 31 3c 3c 28 49 26 37 29 29 0a 23 64  |= (1<<(I&7)).#d
f3250 65 66 69 6e 65 20 43 4c 45 41 52 42 49 54 28 56  efine CLEARBIT(V
f3260 2c 49 29 20 20 20 20 56 5b 49 3e 3e 33 5d 20 26  ,I)    V[I>>3] &
f3270 3d 20 7e 28 31 3c 3c 28 49 26 37 29 29 0a 23 64  = ~(1<<(I&7)).#d
f3280 65 66 69 6e 65 20 54 45 53 54 42 49 54 28 56 2c  efine TESTBIT(V,
f3290 49 29 20 20 20 20 20 28 56 5b 49 3e 3e 33 5d 26  I)     (V[I>>3]&
f32a0 28 31 3c 3c 28 49 26 37 29 29 29 21 3d 30 0a 0a  (1<<(I&7)))!=0..
f32b0 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
f32c0 6e 65 20 72 75 6e 73 20 61 6e 20 65 78 74 65 6e  ne runs an exten
f32d0 73 69 76 65 20 74 65 73 74 20 6f 66 20 74 68 65  sive test of the
f32e0 20 42 69 74 76 65 63 20 63 6f 64 65 2e 0a 2a 2a   Bitvec code..**
f32f0 0a 2a 2a 20 54 68 65 20 69 6e 70 75 74 20 69 73  .** The input is
f3300 20 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 74   an array of int
f3310 65 67 65 72 73 20 74 68 61 74 20 61 63 74 73 20  egers that acts 
f3320 61 73 20 61 20 70 72 6f 67 72 61 6d 0a 2a 2a 20  as a program.** 
f3330 74 6f 20 74 65 73 74 20 74 68 65 20 42 69 74 76  to test the Bitv
f3340 65 63 2e 20 20 54 68 65 20 69 6e 74 65 67 65 72  ec.  The integer
f3350 73 20 61 72 65 20 6f 70 63 6f 64 65 73 20 66 6f  s are opcodes fo
f3360 6c 6c 6f 77 65 64 0a 2a 2a 20 62 79 20 30 2c 20  llowed.** by 0, 
f3370 31 2c 20 6f 72 20 33 20 6f 70 65 72 61 6e 64 73  1, or 3 operands
f3380 2c 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 74  , depending on t
f3390 68 65 20 6f 70 63 6f 64 65 2e 20 20 41 6e 6f 74  he opcode.  Anot
f33a0 68 65 72 0a 2a 2a 20 6f 70 63 6f 64 65 20 66 6f  her.** opcode fo
f33b0 6c 6c 6f 77 73 20 69 6d 6d 65 64 69 61 74 65 6c  llows immediatel
f33c0 79 20 61 66 74 65 72 20 74 68 65 20 6c 61 73 74  y after the last
f33d0 20 6f 70 65 72 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   operand..**.** 
f33e0 54 68 65 72 65 20 61 72 65 20 36 20 6f 70 63 6f  There are 6 opco
f33f0 64 65 73 20 6e 75 6d 62 65 72 65 64 20 66 72 6f  des numbered fro
f3400 6d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 20 20  m 0 through 5.  
f3410 30 20 69 73 20 74 68 65 0a 2a 2a 20 22 68 61 6c  0 is the.** "hal
f3420 74 22 20 6f 70 63 6f 64 65 20 61 6e 64 20 63 61  t" opcode and ca
f3430 75 73 65 73 20 74 68 65 20 74 65 73 74 20 74 6f  uses the test to
f3440 20 65 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30   end..**.**    0
f3450 20 20 20 20 20 20 20 20 20 20 48 61 6c 74 20 61            Halt a
f3460 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 6e 75  nd return the nu
f3470 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 0a 2a  mber of errors.*
f3480 2a 20 20 20 20 31 20 4e 20 53 20 58 20 20 20 20  *    1 N S X    
f3490 53 65 74 20 4e 20 62 69 74 73 20 62 65 67 69 6e  Set N bits begin
f34a0 6e 69 6e 67 20 77 69 74 68 20 53 20 61 6e 64 20  ning with S and 
f34b0 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20 62 79 20  incrementing by 
f34c0 58 0a 2a 2a 20 20 20 20 32 20 4e 20 53 20 58 20  X.**    2 N S X 
f34d0 20 20 20 43 6c 65 61 72 20 4e 20 62 69 74 73 20     Clear N bits 
f34e0 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 53  beginning with S
f34f0 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 69 6e   and incrementin
f3500 67 20 62 79 20 58 0a 2a 2a 20 20 20 20 33 20 4e  g by X.**    3 N
f3510 20 20 20 20 20 20 20 20 53 65 74 20 4e 20 72 61          Set N ra
f3520 6e 64 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69  ndomly chosen bi
f3530 74 73 0a 2a 2a 20 20 20 20 34 20 4e 20 20 20 20  ts.**    4 N    
f3540 20 20 20 20 43 6c 65 61 72 20 4e 20 72 61 6e 64      Clear N rand
f3550 6f 6d 6c 79 20 63 68 6f 73 65 6e 20 62 69 74 73  omly chosen bits
f3560 0a 2a 2a 20 20 20 20 35 20 4e 20 53 20 58 20 20  .**    5 N S X  
f3570 20 20 53 65 74 20 4e 20 62 69 74 73 20 66 72 6f    Set N bits fro
f3580 6d 20 53 20 69 6e 63 72 65 6d 65 6e 74 20 58 20  m S increment X 
f3590 69 6e 20 61 72 72 61 79 20 6f 6e 6c 79 2c 20 6e  in array only, n
f35a0 6f 74 20 69 6e 20 62 69 74 76 65 63 0a 2a 2a 0a  ot in bitvec.**.
f35b0 2a 2a 20 54 68 65 20 6f 70 63 6f 64 65 73 20 31  ** The opcodes 1
f35c0 20 74 68 72 6f 75 67 68 20 34 20 70 65 72 66 6f   through 4 perfo
f35d0 72 6d 20 73 65 74 20 61 6e 64 20 63 6c 65 61 72  rm set and clear
f35e0 20 6f 70 65 72 61 74 69 6f 6e 73 20 61 72 65 20   operations are 
f35f0 70 65 72 66 6f 72 6d 65 64 0a 2a 2a 20 6f 6e 20  performed.** on 
f3600 62 6f 74 68 20 61 20 42 69 74 76 65 63 20 6f 62  both a Bitvec ob
f3610 6a 65 63 74 20 61 6e 64 20 6f 6e 20 61 20 6c 69  ject and on a li
f3620 6e 65 61 72 20 61 72 72 61 79 20 6f 66 20 62 69  near array of bi
f3630 74 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ts obtained from
f3640 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a 20 4f 70 63 6f   malloc..** Opco
f3650 64 65 20 35 20 77 6f 72 6b 73 20 6f 6e 20 74 68  de 5 works on th
f3660 65 20 6c 69 6e 65 61 72 20 61 72 72 61 79 20 6f  e linear array o
f3670 6e 6c 79 2c 20 6e 6f 74 20 6f 6e 20 74 68 65 20  nly, not on the 
f3680 42 69 74 76 65 63 2e 0a 2a 2a 20 4f 70 63 6f 64  Bitvec..** Opcod
f3690 65 20 35 20 69 73 20 75 73 65 64 20 74 6f 20 64  e 5 is used to d
f36a0 65 6c 69 62 65 72 61 74 65 6c 79 20 69 6e 64 75  eliberately indu
f36b0 63 65 20 61 20 66 61 75 6c 74 20 69 6e 20 6f 72  ce a fault in or
f36c0 64 65 72 20 74 6f 0a 2a 2a 20 63 6f 6e 66 69 72  der to.** confir
f36d0 6d 20 74 68 61 74 20 65 72 72 6f 72 20 64 65 74  m that error det
f36e0 65 63 74 69 6f 6e 20 77 6f 72 6b 73 2e 0a 2a 2a  ection works..**
f36f0 0a 2a 2a 20 41 74 20 74 68 65 20 63 6f 6e 63 6c  .** At the concl
f3700 75 73 69 6f 6e 20 6f 66 20 74 68 65 20 74 65 73  usion of the tes
f3710 74 20 74 68 65 20 6c 69 6e 65 61 72 20 61 72 72  t the linear arr
f3720 61 79 20 69 73 20 63 6f 6d 70 61 72 65 64 0a 2a  ay is compared.*
f3730 2a 20 61 67 61 69 6e 73 74 20 74 68 65 20 42 69  * against the Bi
f3740 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 49 66  tvec object.  If
f3750 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 64   there are any d
f3760 69 66 66 65 72 65 6e 63 65 73 2c 0a 2a 2a 20 61  ifferences,.** a
f3770 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75 72  n error is retur
f3780 6e 65 64 2e 20 20 49 66 20 74 68 65 79 20 61 72  ned.  If they ar
f3790 65 20 74 68 65 20 73 61 6d 65 2c 20 7a 65 72 6f  e the same, zero
f37a0 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a   is returned..**
f37b0 0a 2a 2a 20 49 66 20 61 20 6d 65 6d 6f 72 79 20  .** If a memory 
f37c0 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72  allocation error
f37d0 20 6f 63 63 75 72 73 2c 20 72 65 74 75 72 6e 20   occurs, return 
f37e0 2d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  -1..*/.SQLITE_PR
f37f0 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
f3800 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
f3810 73 74 28 69 6e 74 20 73 7a 2c 20 69 6e 74 20 2a  st(int sz, int *
f3820 61 4f 70 29 7b 0a 20 20 42 69 74 76 65 63 20 2a  aOp){.  Bitvec *
f3830 70 42 69 74 76 65 63 20 3d 20 30 3b 0a 20 20 75  pBitvec = 0;.  u
f3840 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 56  nsigned char *pV
f3850 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
f3860 20 2d 31 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 78   -1;.  int i, nx
f3870 2c 20 70 63 2c 20 6f 70 3b 0a 0a 20 20 2f 2a 20  , pc, op;..  /* 
f3880 41 6c 6c 6f 63 61 74 65 20 74 68 65 20 42 69 74  Allocate the Bit
f3890 76 65 63 20 74 6f 20 62 65 20 74 65 73 74 65 64  vec to be tested
f38a0 20 61 6e 64 20 61 20 6c 69 6e 65 61 72 20 61 72   and a linear ar
f38b0 72 61 79 20 6f 66 0a 20 20 2a 2a 20 62 69 74 73  ray of.  ** bits
f38c0 20 74 6f 20 61 63 74 20 61 73 20 74 68 65 20 72   to act as the r
f38d0 65 66 65 72 65 6e 63 65 20 2a 2f 0a 20 20 70 42  eference */.  pB
f38e0 69 74 76 65 63 20 3d 20 73 71 6c 69 74 65 33 42  itvec = sqlite3B
f38f0 69 74 76 65 63 43 72 65 61 74 65 28 20 73 7a 20  itvecCreate( sz 
f3900 29 3b 0a 20 20 70 56 20 3d 20 73 71 6c 69 74 65  );.  pV = sqlite
f3910 33 5f 6d 61 6c 6c 6f 63 28 20 28 73 7a 2b 37 29  3_malloc( (sz+7)
f3920 2f 38 20 2b 20 31 20 29 3b 0a 20 20 69 66 28 20  /8 + 1 );.  if( 
f3930 70 42 69 74 76 65 63 3d 3d 30 20 7c 7c 20 70 56  pBitvec==0 || pV
f3940 3d 3d 30 20 29 20 67 6f 74 6f 20 62 69 74 76 65  ==0 ) goto bitve
f3950 63 5f 65 6e 64 3b 0a 20 20 6d 65 6d 73 65 74 28  c_end;.  memset(
f3960 70 56 2c 20 30 2c 20 28 73 7a 2b 37 29 2f 38 20  pV, 0, (sz+7)/8 
f3970 2b 20 31 29 3b 0a 0a 20 20 2f 2a 20 52 75 6e 20  + 1);..  /* Run 
f3980 74 68 65 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  the program */. 
f3990 20 70 63 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65   pc = 0;.  while
f39a0 28 20 28 6f 70 20 3d 20 61 4f 70 5b 70 63 5d 29  ( (op = aOp[pc])
f39b0 21 3d 30 20 29 7b 0a 20 20 20 20 73 77 69 74 63  !=0 ){.    switc
f39c0 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  h( op ){.      c
f39d0 61 73 65 20 31 3a 0a 20 20 20 20 20 20 63 61 73  ase 1:.      cas
f39e0 65 20 32 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 2:.      case 
f39f0 35 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20  5: {.        nx 
f3a00 3d 20 34 3b 0a 20 20 20 20 20 20 20 20 69 20 3d  = 4;.        i =
f3a10 20 61 4f 70 5b 70 63 2b 32 5d 20 2d 20 31 3b 0a   aOp[pc+2] - 1;.
f3a20 20 20 20 20 20 20 20 20 61 4f 70 5b 70 63 2b 32          aOp[pc+2
f3a30 5d 20 2b 3d 20 61 4f 70 5b 70 63 2b 33 5d 3b 0a  ] += aOp[pc+3];.
f3a40 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
f3a50 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
f3a60 65 20 33 3a 0a 20 20 20 20 20 20 63 61 73 65 20  e 3:.      case 
f3a70 34 3a 20 0a 20 20 20 20 20 20 64 65 66 61 75 6c  4: .      defaul
f3a80 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 6e 78 20  t: {.        nx 
f3a90 3d 20 32 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = 2;.        sql
f3aa0 69 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28  ite3_randomness(
f3ab0 73 69 7a 65 6f 66 28 69 29 2c 20 26 69 29 3b 0a  sizeof(i), &i);.
f3ac0 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
f3ad0 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
f3ae0 20 69 66 28 20 28 2d 2d 61 4f 70 5b 70 63 2b 31   if( (--aOp[pc+1
f3af0 5d 29 20 3e 20 30 20 29 20 6e 78 20 3d 20 30 3b  ]) > 0 ) nx = 0;
f3b00 0a 20 20 20 20 70 63 20 2b 3d 20 6e 78 3b 0a 20  .    pc += nx;. 
f3b10 20 20 20 69 20 3d 20 28 69 20 26 20 30 78 37 66     i = (i & 0x7f
f3b20 66 66 66 66 66 66 29 25 73 7a 3b 0a 20 20 20 20  ffffff)%sz;.    
f3b30 69 66 28 20 28 6f 70 20 26 20 31 29 21 3d 30 20  if( (op & 1)!=0 
f3b40 29 7b 0a 20 20 20 20 20 20 53 45 54 42 49 54 28  ){.      SETBIT(
f3b50 70 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20  pV, (i+1));.    
f3b60 20 20 69 66 28 20 6f 70 21 3d 35 20 29 7b 0a 20    if( op!=5 ){. 
f3b70 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74         if( sqlit
f3b80 65 33 42 69 74 76 65 63 53 65 74 28 70 42 69 74  e3BitvecSet(pBit
f3b90 76 65 63 2c 20 69 2b 31 29 20 29 20 67 6f 74 6f  vec, i+1) ) goto
f3ba0 20 62 69 74 76 65 63 5f 65 6e 64 3b 0a 20 20 20   bitvec_end;.   
f3bb0 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a     }.    }else{.
f3bc0 20 20 20 20 20 20 43 4c 45 41 52 42 49 54 28 70        CLEARBIT(p
f3bd0 56 2c 20 28 69 2b 31 29 29 3b 0a 20 20 20 20 20  V, (i+1));.     
f3be0 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 43 6c   sqlite3BitvecCl
f3bf0 65 61 72 28 70 42 69 74 76 65 63 2c 20 69 2b 31  ear(pBitvec, i+1
f3c00 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
f3c10 2f 2a 20 54 65 73 74 20 74 6f 20 6d 61 6b 65 20  /* Test to make 
f3c20 73 75 72 65 20 74 68 65 20 6c 69 6e 65 61 72 20  sure the linear 
f3c30 61 72 72 61 79 20 65 78 61 63 74 6c 79 20 6d 61  array exactly ma
f3c40 74 63 68 65 73 20 74 68 65 0a 20 20 2a 2a 20 42  tches the.  ** B
f3c50 69 74 76 65 63 20 6f 62 6a 65 63 74 2e 20 20 53  itvec object.  S
f3c60 74 61 72 74 20 77 69 74 68 20 74 68 65 20 61 73  tart with the as
f3c70 73 75 6d 70 74 69 6f 6e 20 74 68 61 74 20 74 68  sumption that th
f3c80 65 79 20 64 6f 0a 20 20 2a 2a 20 6d 61 74 63 68  ey do.  ** match
f3c90 20 28 72 63 3d 3d 30 29 2e 20 20 43 68 61 6e 67   (rc==0).  Chang
f3ca0 65 20 72 63 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f  e rc to non-zero
f3cb0 20 69 66 20 61 20 64 69 73 63 72 65 70 61 6e 63   if a discrepanc
f3cc0 79 0a 20 20 2a 2a 20 69 73 20 66 6f 75 6e 64 2e  y.  ** is found.
f3cd0 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
f3ce0 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28 30  ite3BitvecTest(0
f3cf0 2c 30 29 20 2b 20 73 71 6c 69 74 65 33 42 69 74  ,0) + sqlite3Bit
f3d00 76 65 63 54 65 73 74 28 70 42 69 74 76 65 63 2c  vecTest(pBitvec,
f3d10 20 73 7a 2b 31 29 0a 20 20 20 20 20 20 20 20 20   sz+1).         
f3d20 20 2b 20 73 71 6c 69 74 65 33 42 69 74 76 65 63   + sqlite3Bitvec
f3d30 54 65 73 74 28 70 42 69 74 76 65 63 2c 20 30 29  Test(pBitvec, 0)
f3d40 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d  ;.  for(i=1; i<=
f3d50 73 7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  sz; i++){.    if
f3d60 28 20 20 28 54 45 53 54 42 49 54 28 70 56 2c 69  (  (TESTBIT(pV,i
f3d70 29 29 21 3d 73 71 6c 69 74 65 33 42 69 74 76 65  ))!=sqlite3Bitve
f3d80 63 54 65 73 74 28 70 42 69 74 76 65 63 2c 69 29  cTest(pBitvec,i)
f3d90 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 69   ){.      rc = i
f3da0 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
f3db0 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46     }.  }..  /* F
f3dc0 72 65 65 20 61 6c 6c 6f 63 61 74 65 64 20 73 74  ree allocated st
f3dd0 72 75 63 74 75 72 65 20 2a 2f 0a 62 69 74 76 65  ructure */.bitve
f3de0 63 5f 65 6e 64 3a 0a 20 20 73 71 6c 69 74 65 33  c_end:.  sqlite3
f3df0 5f 66 72 65 65 28 70 56 29 3b 0a 20 20 73 71 6c  _free(pV);.  sql
f3e00 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f  ite3BitvecDestro
f3e10 79 28 70 42 69 74 76 65 63 29 3b 0a 20 20 72 65  y(pBitvec);.  re
f3e20 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69  turn rc;.}.#endi
f3e30 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
f3e40 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54 20 2a 2f  _BUILTIN_TEST */
f3e50 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
f3e60 2a 20 45 6e 64 20 6f 66 20 62 69 74 76 65 63 2e  * End of bitvec.
f3e70 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
f3e80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3e90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ea0 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
f3eb0 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70 63 61  * Begin file pca
f3ec0 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  che.c **********
f3ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f3ef0 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20 41 75 67  /./*.** 2008 Aug
f3f00 75 73 74 20 30 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ust 05.**.** The
f3f10 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
f3f20 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
f3f30 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
f3f40 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
f3f50 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
f3f60 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
f3f70 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
f3f80 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
f3f90 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
f3fa0 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
f3fb0 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
f3fc0 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
f3fd0 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
f3fe0 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
f3ff0 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
f4000 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
f4010 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
f4020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4050 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4060 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
f4070 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68  le implements th
f4080 61 74 20 70 61 67 65 20 63 61 63 68 65 2e 0a 2a  at page cache..*
f4090 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 70  *.** @(#) $Id: p
f40a0 63 61 63 68 65 2e 63 2c 76 20 31 2e 33 38 20 32  cache.c,v 1.38 2
f40b0 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a  008/11/19 16:52:
f40c0 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  44 danielk1977 E
f40d0 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41  xp $.*/../*.** A
f40e0 20 63 6f 6d 70 6c 65 74 65 20 70 61 67 65 20 63   complete page c
f40f0 61 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61  ache is an insta
f4100 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
f4110 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 72 75 63 74  cture..*/.struct
f4120 20 50 43 61 63 68 65 20 7b 0a 20 20 50 67 48 64   PCache {.  PgHd
f4130 72 20 2a 70 44 69 72 74 79 2c 20 2a 70 44 69 72  r *pDirty, *pDir
f4140 74 79 54 61 69 6c 3b 20 20 20 20 20 20 20 20 20  tyTail;         
f4150 2f 2a 20 4c 69 73 74 20 6f 66 20 64 69 72 74 79  /* List of dirty
f4160 20 70 61 67 65 73 20 69 6e 20 4c 52 55 20 6f 72   pages in LRU or
f4170 64 65 72 20 2a 2f 0a 20 20 50 67 48 64 72 20 2a  der */.  PgHdr *
f4180 70 53 79 6e 63 65 64 3b 20 20 20 20 20 20 20 20  pSynced;        
f4190 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f41a0 4c 61 73 74 20 73 79 6e 63 65 64 20 70 61 67 65  Last synced page
f41b0 20 69 6e 20 64 69 72 74 79 20 70 61 67 65 20 6c   in dirty page l
f41c0 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65  ist */.  int nRe
f41d0 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  f;              
f41e0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f41f0 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72 65  Number of refere
f4200 6e 63 65 64 20 70 61 67 65 73 20 2a 2f 0a 20 20  nced pages */.  
f4210 69 6e 74 20 6e 4d 61 78 3b 20 20 20 20 20 20 20  int nMax;       
f4220 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4230 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65      /* Configure
f4240 64 20 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a  d cache size */.
f4250 20 20 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20    int nMin;     
f4260 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f4270 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75        /* Configu
f4280 72 65 64 20 6d 69 6e 69 6d 75 6d 20 63 61 63 68  red minimum cach
f4290 65 20 73 69 7a 65 20 2a 2f 0a 20 20 69 6e 74 20  e size */.  int 
f42a0 73 7a 50 61 67 65 3b 20 20 20 20 20 20 20 20 20  szPage;         
f42b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f42c0 2f 2a 20 53 69 7a 65 20 6f 66 20 65 76 65 72 79  /* Size of every
f42d0 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 63 61   page in this ca
f42e0 63 68 65 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 45  che */.  int szE
f42f0 78 74 72 61 3b 20 20 20 20 20 20 20 20 20 20 20  xtra;           
f4300 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f4310 53 69 7a 65 20 6f 66 20 65 78 74 72 61 20 73 70  Size of extra sp
f4320 61 63 65 20 66 6f 72 20 65 61 63 68 20 70 61 67  ace for each pag
f4330 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50 75 72 67  e */.  int bPurg
f4340 65 61 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20  eable;          
f4350 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72             /* Tr
f4360 75 65 20 69 66 20 70 61 67 65 73 20 61 72 65 20  ue if pages are 
f4370 6f 6e 20 62 61 63 6b 69 6e 67 20 73 74 6f 72 65  on backing store
f4380 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 53 74 72   */.  int (*xStr
f4390 65 73 73 29 28 76 6f 69 64 2a 2c 50 67 48 64 72  ess)(void*,PgHdr
f43a0 2a 29 3b 20 20 20 20 20 20 20 2f 2a 20 43 61 6c  *);       /* Cal
f43b0 6c 20 74 6f 20 74 72 79 20 6d 61 6b 65 20 61 20  l to try make a 
f43c0 70 61 67 65 20 63 6c 65 61 6e 20 2a 2f 0a 20 20  page clean */.  
f43d0 76 6f 69 64 20 2a 70 53 74 72 65 73 73 3b 20 20  void *pStress;  
f43e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f43f0 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
f4400 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20  to xStress */.  
f4410 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a  sqlite3_pcache *
f4420 70 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20  pCache;         
f4430 20 20 20 20 2f 2a 20 50 6c 75 67 67 61 62 6c 65      /* Pluggable
f4440 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 20 2a 2f   cache module */
f4450 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 31  .  PgHdr *pPage1
f4460 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65  ;.};../*.** Some
f4470 20 6f 66 20 74 68 65 20 61 73 73 65 72 74 28 29   of the assert()
f4480 20 6d 61 63 72 6f 73 20 69 6e 20 74 68 69 73 20   macros in this 
f4490 63 6f 64 65 20 61 72 65 20 74 6f 6f 20 65 78 70  code are too exp
f44a0 65 6e 73 69 76 65 20 74 6f 20 72 75 6e 0a 2a 2a  ensive to run.**
f44b0 20 65 76 65 6e 20 64 75 72 69 6e 67 20 6e 6f 72   even during nor
f44c0 6d 61 6c 20 64 65 62 75 67 67 69 6e 67 2e 20 20  mal debugging.  
f44d0 55 73 65 20 74 68 65 6d 20 6f 6e 6c 79 20 72 61  Use them only ra
f44e0 72 65 6c 79 20 6f 6e 20 6c 6f 6e 67 2d 72 75 6e  rely on long-run
f44f0 6e 69 6e 67 0a 2a 2a 20 74 65 73 74 73 2e 20 20  ning.** tests.  
f4500 45 6e 61 62 6c 65 20 74 68 65 20 65 78 70 65 6e  Enable the expen
f4510 73 69 76 65 20 61 73 73 65 72 74 73 20 75 73 69  sive asserts usi
f4520 6e 67 20 74 68 65 0a 2a 2a 20 2d 44 53 51 4c 49  ng the.** -DSQLI
f4530 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53  TE_ENABLE_EXPENS
f4540 49 56 45 5f 41 53 53 45 52 54 3d 31 20 63 6f 6d  IVE_ASSERT=1 com
f4550 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
f4560 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
f4570 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53  TE_ENABLE_EXPENS
f4580 49 56 45 5f 41 53 53 45 52 54 0a 23 20 64 65 66  IVE_ASSERT.# def
f4590 69 6e 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73  ine expensive_as
f45a0 73 65 72 74 28 58 29 20 20 61 73 73 65 72 74 28  sert(X)  assert(
f45b0 58 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  X).#else.# defin
f45c0 65 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65  e expensive_asse
f45d0 72 74 28 58 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a  rt(X).#endif../*
f45e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f45f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f4600 2a 20 4c 69 6e 6b 65 64 20 4c 69 73 74 20 4d 61  * Linked List Ma
f4610 6e 61 67 65 6d 65 6e 74 20 2a 2a 2a 2a 2a 2a 2a  nagement *******
f4620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
f4630 23 69 66 20 21 64 65 66 69 6e 65 64 28 4e 44 45  #if !defined(NDE
f4640 42 55 47 29 20 26 26 20 64 65 66 69 6e 65 64 28  BUG) && defined(
f4650 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 45 58  SQLITE_ENABLE_EX
f4660 50 45 4e 53 49 56 45 5f 41 53 53 45 52 54 29 0a  PENSIVE_ASSERT).
f4670 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61 74  /*.** Check that
f4680 20 74 68 65 20 70 43 61 63 68 65 2d 3e 70 53 79   the pCache->pSy
f4690 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69 73  nced variable is
f46a0 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20   set correctly. 
f46b0 49 66 20 69 74 0a 2a 2a 20 69 73 20 6e 6f 74 2c  If it.** is not,
f46c0 20 65 69 74 68 65 72 20 66 61 69 6c 20 61 6e 20   either fail an 
f46d0 61 73 73 65 72 74 20 6f 72 20 72 65 74 75 72 6e  assert or return
f46e0 20 7a 65 72 6f 2e 20 4f 74 68 65 72 77 69 73 65   zero. Otherwise
f46f0 2c 20 72 65 74 75 72 6e 0a 2a 2a 20 6e 6f 6e 2d  , return.** non-
f4700 7a 65 72 6f 2e 20 54 68 69 73 20 69 73 20 6f 6e  zero. This is on
f4710 6c 79 20 75 73 65 64 20 69 6e 20 64 65 62 75 67  ly used in debug
f4720 67 69 6e 67 20 62 75 69 6c 64 73 2c 20 61 73 20  ging builds, as 
f4730 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
f4740 20 65 78 70 65 6e 73 69 76 65 5f 61 73 73 65 72   expensive_asser
f4750 74 28 20 70 63 61 63 68 65 43 68 65 63 6b 53 79  t( pcacheCheckSy
f4760 6e 63 65 64 28 70 43 61 63 68 65 29 20 29 3b 0a  nced(pCache) );.
f4770 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 63  */.static int pc
f4780 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28  acheCheckSynced(
f4790 50 43 61 63 68 65 20 2a 70 43 61 63 68 65 29 7b  PCache *pCache){
f47a0 0a 20 20 50 67 48 64 72 20 2a 70 3b 0a 20 20 66  .  PgHdr *p;.  f
f47b0 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69  or(p=pCache->pDi
f47c0 72 74 79 54 61 69 6c 3b 20 70 21 3d 70 43 61 63  rtyTail; p!=pCac
f47d0 68 65 2d 3e 70 53 79 6e 63 65 64 3b 20 70 3d 70  he->pSynced; p=p
f47e0 2d 3e 70 44 69 72 74 79 50 72 65 76 29 7b 0a 20  ->pDirtyPrev){. 
f47f0 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6e 52     assert( p->nR
f4800 65 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26  ef || (p->flags&
f4810 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
f4820 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   );.  }.  return
f4830 20 28 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 52 65   (p==0 || p->nRe
f4840 66 20 7c 7c 20 28 70 2d 3e 66 6c 61 67 73 26 50  f || (p->flags&P
f4850 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29 3d  GHDR_NEED_SYNC)=
f4860 3d 30 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  =0);.}.#endif /*
f4870 20 21 4e 44 45 42 55 47 20 26 26 20 53 51 4c 49   !NDEBUG && SQLI
f4880 54 45 5f 45 4e 41 42 4c 45 5f 45 58 50 45 4e 53  TE_ENABLE_EXPENS
f4890 49 56 45 5f 41 53 53 45 52 54 20 2a 2f 0a 0a 2f  IVE_ASSERT */../
f48a0 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 70 61 67 65  *.** Remove page
f48b0 20 70 50 61 67 65 20 66 72 6f 6d 20 74 68 65 20   pPage from the 
f48c0 6c 69 73 74 20 6f 66 20 64 69 72 74 79 20 70 61  list of dirty pa
f48d0 67 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ges..*/.static v
f48e0 6f 69 64 20 70 63 61 63 68 65 52 65 6d 6f 76 65  oid pcacheRemove
f48f0 46 72 6f 6d 44 69 72 74 79 4c 69 73 74 28 50 67  FromDirtyList(Pg
f4900 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20 50  Hdr *pPage){.  P
f4910 43 61 63 68 65 20 2a 70 20 3d 20 70 50 61 67 65  Cache *p = pPage
f4920 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20 61 73 73  ->pCache;..  ass
f4930 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72  ert( pPage->pDir
f4940 74 79 4e 65 78 74 20 7c 7c 20 70 50 61 67 65 3d  tyNext || pPage=
f4950 3d 70 2d 3e 70 44 69 72 74 79 54 61 69 6c 20 29  =p->pDirtyTail )
f4960 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ;.  assert( pPag
f4970 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20 7c 7c  e->pDirtyPrev ||
f4980 20 70 50 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74   pPage==p->pDirt
f4990 79 20 29 3b 0a 0a 20 20 2f 2a 20 55 70 64 61 74  y );..  /* Updat
f49a0 65 20 74 68 65 20 50 43 61 63 68 65 31 2e 70 53  e the PCache1.pS
f49b0 79 6e 63 65 64 20 76 61 72 69 61 62 6c 65 20 69  ynced variable i
f49c0 66 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f 0a  f necessary. */.
f49d0 20 20 69 66 28 20 70 2d 3e 70 53 79 6e 63 65 64    if( p->pSynced
f49e0 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20 50  ==pPage ){.    P
f49f0 67 48 64 72 20 2a 70 53 79 6e 63 65 64 20 3d 20  gHdr *pSynced = 
f4a00 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
f4a10 76 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 70 53  v;.    while( pS
f4a20 79 6e 63 65 64 20 26 26 20 28 70 53 79 6e 63 65  ynced && (pSynce
f4a30 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  d->flags&PGHDR_N
f4a40 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20 20  EED_SYNC) ){.   
f4a50 20 20 20 70 53 79 6e 63 65 64 20 3d 20 70 53 79     pSynced = pSy
f4a60 6e 63 65 64 2d 3e 70 44 69 72 74 79 50 72 65 76  nced->pDirtyPrev
f4a70 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e 70  ;.    }.    p->p
f4a80 53 79 6e 63 65 64 20 3d 20 70 53 79 6e 63 65 64  Synced = pSynced
f4a90 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61  ;.  }..  if( pPa
f4aa0 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 29  ge->pDirtyNext )
f4ab0 7b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 44 69  {.    pPage->pDi
f4ac0 72 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50  rtyNext->pDirtyP
f4ad0 72 65 76 20 3d 20 70 50 61 67 65 2d 3e 70 44 69  rev = pPage->pDi
f4ae0 72 74 79 50 72 65 76 3b 0a 20 20 7d 65 6c 73 65  rtyPrev;.  }else
f4af0 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
f4b00 61 67 65 3d 3d 70 2d 3e 70 44 69 72 74 79 54 61  age==p->pDirtyTa
f4b10 69 6c 20 29 3b 0a 20 20 20 20 70 2d 3e 70 44 69  il );.    p->pDi
f4b20 72 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 2d  rtyTail = pPage-
f4b30 3e 70 44 69 72 74 79 50 72 65 76 3b 0a 20 20 7d  >pDirtyPrev;.  }
f4b40 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 44  .  if( pPage->pD
f4b50 69 72 74 79 50 72 65 76 20 29 7b 0a 20 20 20 20  irtyPrev ){.    
f4b60 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
f4b70 76 2d 3e 70 44 69 72 74 79 4e 65 78 74 20 3d 20  v->pDirtyNext = 
f4b80 70 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78  pPage->pDirtyNex
f4b90 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
f4ba0 61 73 73 65 72 74 28 20 70 50 61 67 65 3d 3d 70  assert( pPage==p
f4bb0 2d 3e 70 44 69 72 74 79 20 29 3b 0a 20 20 20 20  ->pDirty );.    
f4bc0 70 2d 3e 70 44 69 72 74 79 20 3d 20 70 50 61 67  p->pDirty = pPag
f4bd0 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20  e->pDirtyNext;. 
f4be0 20 7d 0a 20 20 70 50 61 67 65 2d 3e 70 44 69 72   }.  pPage->pDir
f4bf0 74 79 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70 50  tyNext = 0;.  pP
f4c00 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65 76 20  age->pDirtyPrev 
f4c10 3d 20 30 3b 0a 0a 20 20 65 78 70 65 6e 73 69 76  = 0;..  expensiv
f4c20 65 5f 61 73 73 65 72 74 28 20 70 63 61 63 68 65  e_assert( pcache
f4c30 43 68 65 63 6b 53 79 6e 63 65 64 28 70 29 20 29  CheckSynced(p) )
f4c40 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 70  ;.}../*.** Add p
f4c50 61 67 65 20 70 50 61 67 65 20 74 6f 20 74 68 65  age pPage to the
f4c60 20 68 65 61 64 20 6f 66 20 74 68 65 20 64 69 72   head of the dir
f4c70 74 79 20 6c 69 73 74 20 28 50 43 61 63 68 65 31  ty list (PCache1
f4c80 2e 70 44 69 72 74 79 20 69 73 20 73 65 74 20 74  .pDirty is set t
f4c90 6f 0a 2a 2a 20 70 50 61 67 65 29 2e 0a 2a 2f 0a  o.** pPage)..*/.
f4ca0 73 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63  static void pcac
f4cb0 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74  heAddToDirtyList
f4cc0 28 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a  (PgHdr *pPage){.
f4cd0 20 20 50 43 61 63 68 65 20 2a 70 20 3d 20 70 50    PCache *p = pP
f4ce0 61 67 65 2d 3e 70 43 61 63 68 65 3b 0a 0a 20 20  age->pCache;..  
f4cf0 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
f4d00 44 69 72 74 79 4e 65 78 74 3d 3d 30 20 26 26 20  DirtyNext==0 && 
f4d10 70 50 61 67 65 2d 3e 70 44 69 72 74 79 50 72 65  pPage->pDirtyPre
f4d20 76 3d 3d 30 20 26 26 20 70 2d 3e 70 44 69 72 74  v==0 && p->pDirt
f4d30 79 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20 20 70  y!=pPage );..  p
f4d40 50 61 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74  Page->pDirtyNext
f4d50 20 3d 20 70 2d 3e 70 44 69 72 74 79 3b 0a 20 20   = p->pDirty;.  
f4d60 69 66 28 20 70 50 61 67 65 2d 3e 70 44 69 72 74  if( pPage->pDirt
f4d70 79 4e 65 78 74 20 29 7b 0a 20 20 20 20 61 73 73  yNext ){.    ass
f4d80 65 72 74 28 20 70 50 61 67 65 2d 3e 70 44 69 72  ert( pPage->pDir
f4d90 74 79 4e 65 78 74 2d 3e 70 44 69 72 74 79 50 72  tyNext->pDirtyPr
f4da0 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 70 50 61  ev==0 );.    pPa
f4db0 67 65 2d 3e 70 44 69 72 74 79 4e 65 78 74 2d 3e  ge->pDirtyNext->
f4dc0 70 44 69 72 74 79 50 72 65 76 20 3d 20 70 50 61  pDirtyPrev = pPa
f4dd0 67 65 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 44 69  ge;.  }.  p->pDi
f4de0 72 74 79 20 3d 20 70 50 61 67 65 3b 0a 20 20 69  rty = pPage;.  i
f4df0 66 28 20 21 70 2d 3e 70 44 69 72 74 79 54 61 69  f( !p->pDirtyTai
f4e00 6c 20 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69 72  l ){.    p->pDir
f4e10 74 79 54 61 69 6c 20 3d 20 70 50 61 67 65 3b 0a  tyTail = pPage;.
f4e20 20 20 7d 0a 20 20 69 66 28 20 21 70 2d 3e 70 53    }.  if( !p->pS
f4e30 79 6e 63 65 64 20 26 26 20 30 3d 3d 28 70 50 61  ynced && 0==(pPa
f4e40 67 65 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  ge->flags&PGHDR_
f4e50 4e 45 45 44 5f 53 59 4e 43 29 20 29 7b 0a 20 20  NEED_SYNC) ){.  
f4e60 20 20 70 2d 3e 70 53 79 6e 63 65 64 20 3d 20 70    p->pSynced = p
f4e70 50 61 67 65 3b 0a 20 20 7d 0a 20 20 65 78 70 65  Page;.  }.  expe
f4e80 6e 73 69 76 65 5f 61 73 73 65 72 74 28 20 70 63  nsive_assert( pc
f4e90 61 63 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28  acheCheckSynced(
f4ea0 70 29 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  p) );.}../*.** W
f4eb0 72 61 70 70 65 72 20 61 72 6f 75 6e 64 20 74 68  rapper around th
f4ec0 65 20 70 6c 75 67 67 61 62 6c 65 20 63 61 63 68  e pluggable cach
f4ed0 65 73 20 78 55 6e 70 69 6e 20 6d 65 74 68 6f 64  es xUnpin method
f4ee0 2e 20 49 66 20 74 68 65 20 63 61 63 68 65 20 69  . If the cache i
f4ef0 73 0a 2a 2a 20 62 65 69 6e 67 20 75 73 65 64 20  s.** being used 
f4f00 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
f4f10 20 64 61 74 61 62 61 73 65 2c 20 74 68 69 73 20   database, this 
f4f20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61 20 6e 6f  function is a no
f4f30 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  -op..*/.static v
f4f40 6f 69 64 20 70 63 61 63 68 65 55 6e 70 69 6e 28  oid pcacheUnpin(
f4f50 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 61  PgHdr *p){.  PCa
f4f60 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20 70 2d  che *pCache = p-
f4f70 3e 70 43 61 63 68 65 3b 0a 20 20 69 66 28 20 70  >pCache;.  if( p
f4f80 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c  Cache->bPurgeabl
f4f90 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e  e ){.    if( p->
f4fa0 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20  pgno==1 ){.     
f4fb0 20 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20   pCache->pPage1 
f4fc0 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  = 0;.    }.    s
f4fd0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
f4fe0 69 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e  ig.pcache.xUnpin
f4ff0 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c  (pCache->pCache,
f5000 20 70 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   p, 0);.  }.}../
f5010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5020 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5030 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f5040 2a 2a 2a 20 47 65 6e 65 72 61 6c 20 49 6e 74 65  *** General Inte
f5050 72 66 61 63 65 73 20 2a 2a 2a 2a 2a 2a 0a 2a 2a  rfaces ******.**
f5060 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 61  .** Initialize a
f5070 6e 64 20 73 68 75 74 64 6f 77 6e 20 74 68 65 20  nd shutdown the 
f5080 70 61 67 65 20 63 61 63 68 65 20 73 75 62 73 79  page cache subsy
f5090 73 74 65 6d 2e 20 4e 65 69 74 68 65 72 20 6f 66  stem. Neither of
f50a0 20 74 68 65 73 65 20 0a 2a 2a 20 66 75 6e 63 74   these .** funct
f50b0 69 6f 6e 73 20 61 72 65 20 74 68 72 65 61 64 73  ions are threads
f50c0 61 66 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  afe..*/.SQLITE_P
f50d0 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
f50e0 65 33 50 63 61 63 68 65 49 6e 69 74 69 61 6c 69  e3PcacheInitiali
f50f0 7a 65 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20  ze(void){.  if( 
f5100 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
f5110 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e 69 74  fig.pcache.xInit
f5120 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
f5130 65 33 50 43 61 63 68 65 53 65 74 44 65 66 61 75  e3PCacheSetDefau
f5140 6c 74 28 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  lt();.  }.  retu
f5150 72 6e 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  rn sqlite3Global
f5160 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49  Config.pcache.xI
f5170 6e 69 74 28 73 71 6c 69 74 65 33 47 6c 6f 62 61  nit(sqlite3Globa
f5180 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 70  lConfig.pcache.p
f5190 41 72 67 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  Arg);.}.SQLITE_P
f51a0 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
f51b0 74 65 33 50 63 61 63 68 65 53 68 75 74 64 6f 77  te3PcacheShutdow
f51c0 6e 28 76 6f 69 64 29 7b 0a 20 20 69 66 28 20 73  n(void){.  if( s
f51d0 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
f51e0 69 67 2e 70 63 61 63 68 65 2e 78 53 68 75 74 64  ig.pcache.xShutd
f51f0 6f 77 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  own ){.    sqlit
f5200 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
f5210 63 61 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 28  cache.xShutdown(
f5220 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
f5230 66 69 67 2e 70 63 61 63 68 65 2e 70 41 72 67 29  fig.pcache.pArg)
f5240 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ;.  }.}../*.** R
f5250 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 69  eturn the size i
f5260 6e 20 62 79 74 65 73 20 6f 66 20 61 20 50 43 61  n bytes of a PCa
f5270 63 68 65 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 53  che object..*/.S
f5280 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
f5290 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  t sqlite3PcacheS
f52a0 69 7a 65 28 76 6f 69 64 29 7b 20 72 65 74 75 72  ize(void){ retur
f52b0 6e 20 73 69 7a 65 6f 66 28 50 43 61 63 68 65 29  n sizeof(PCache)
f52c0 3b 20 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ; }../*.** Creat
f52d0 65 20 61 20 6e 65 77 20 50 43 61 63 68 65 20 6f  e a new PCache o
f52e0 62 6a 65 63 74 2e 20 53 74 6f 72 61 67 65 20 73  bject. Storage s
f52f0 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
f5300 20 6f 62 6a 65 63 74 0a 2a 2a 20 68 61 73 20 61   object.** has a
f5310 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c 6c 6f  lready been allo
f5320 63 61 74 65 64 20 61 6e 64 20 69 73 20 70 61 73  cated and is pas
f5330 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 70 20  sed in as the p 
f5340 70 6f 69 6e 74 65 72 2e 20 0a 2a 2a 20 54 68 65  pointer. .** The
f5350 20 63 61 6c 6c 65 72 20 64 69 73 63 6f 76 65 72   caller discover
f5360 73 20 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65  s how much space
f5370 20 6e 65 65 64 73 20 74 6f 20 62 65 20 61 6c 6c   needs to be all
f5380 6f 63 61 74 65 64 20 62 79 20 0a 2a 2a 20 63 61  ocated by .** ca
f5390 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 50 63 61  lling sqlite3Pca
f53a0 63 68 65 53 69 7a 65 28 29 2e 0a 2a 2f 0a 53 51  cheSize()..*/.SQ
f53b0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
f53c0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4f  d sqlite3PcacheO
f53d0 70 65 6e 28 0a 20 20 69 6e 74 20 73 7a 50 61 67  pen(.  int szPag
f53e0 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
f53f0 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 65      /* Size of e
f5400 76 65 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69  very page */.  i
f5410 6e 74 20 73 7a 45 78 74 72 61 2c 20 20 20 20 20  nt szExtra,     
f5420 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
f5430 78 74 72 61 20 73 70 61 63 65 20 61 73 73 6f 63  xtra space assoc
f5440 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20  iated with each 
f5450 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 62 50  page */.  int bP
f5460 75 72 67 65 61 62 6c 65 2c 20 20 20 20 20 20 20  urgeable,       
f5470 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
f5480 66 20 70 61 67 65 73 20 61 72 65 20 6f 6e 20 62  f pages are on b
f5490 61 63 6b 69 6e 67 20 73 74 6f 72 65 20 2a 2f 0a  acking store */.
f54a0 20 20 69 6e 74 20 28 2a 78 53 74 72 65 73 73 29    int (*xStress)
f54b0 28 76 6f 69 64 2a 2c 50 67 48 64 72 2a 29 2c 2f  (void*,PgHdr*),/
f54c0 2a 20 43 61 6c 6c 20 74 6f 20 74 72 79 20 74 6f  * Call to try to
f54d0 20 6d 61 6b 65 20 70 61 67 65 73 20 63 6c 65 61   make pages clea
f54e0 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 53 74  n */.  void *pSt
f54f0 72 65 73 73 2c 20 20 20 20 20 20 20 20 20 20 20  ress,           
f5500 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20      /* Argument 
f5510 74 6f 20 78 53 74 72 65 73 73 20 2a 2f 0a 20 20  to xStress */.  
f5520 50 43 61 63 68 65 20 2a 70 20 20 20 20 20 20 20  PCache *p       
f5530 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
f5540 50 72 65 61 6c 6c 6f 63 61 74 65 64 20 73 70 61  Preallocated spa
f5550 63 65 20 66 6f 72 20 74 68 65 20 50 43 61 63 68  ce for the PCach
f5560 65 20 2a 2f 0a 29 7b 0a 20 20 6d 65 6d 73 65 74  e */.){.  memset
f5570 28 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 43  (p, 0, sizeof(PC
f5580 61 63 68 65 29 29 3b 0a 20 20 70 2d 3e 73 7a 50  ache));.  p->szP
f5590 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20 20  age = szPage;.  
f55a0 70 2d 3e 73 7a 45 78 74 72 61 20 3d 20 73 7a 45  p->szExtra = szE
f55b0 78 74 72 61 3b 0a 20 20 70 2d 3e 62 50 75 72 67  xtra;.  p->bPurg
f55c0 65 61 62 6c 65 20 3d 20 62 50 75 72 67 65 61 62  eable = bPurgeab
f55d0 6c 65 3b 0a 20 20 70 2d 3e 78 53 74 72 65 73 73  le;.  p->xStress
f55e0 20 3d 20 78 53 74 72 65 73 73 3b 0a 20 20 70 2d   = xStress;.  p-
f55f0 3e 70 53 74 72 65 73 73 20 3d 20 70 53 74 72 65  >pStress = pStre
f5600 73 73 3b 0a 20 20 70 2d 3e 6e 4d 61 78 20 3d 20  ss;.  p->nMax = 
f5610 31 30 30 3b 0a 20 20 70 2d 3e 6e 4d 69 6e 20 3d  100;.  p->nMin =
f5620 20 31 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68   10;.}../*.** Ch
f5630 61 6e 67 65 20 74 68 65 20 70 61 67 65 20 73 69  ange the page si
f5640 7a 65 20 66 6f 72 20 50 43 61 63 68 65 20 6f 62  ze for PCache ob
f5650 6a 65 63 74 2e 20 54 68 65 20 63 61 6c 6c 65 72  ject. The caller
f5660 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61   must ensure tha
f5670 74 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 6e  t there.** are n
f5680 6f 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  o outstanding pa
f5690 67 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68  ge references wh
f56a0 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  en this function
f56b0 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 53   is called..*/.S
f56c0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
f56d0 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  id sqlite3Pcache
f56e0 53 65 74 50 61 67 65 53 69 7a 65 28 50 43 61 63  SetPageSize(PCac
f56f0 68 65 20 2a 70 43 61 63 68 65 2c 20 69 6e 74 20  he *pCache, int 
f5700 73 7a 50 61 67 65 29 7b 0a 20 20 61 73 73 65 72  szPage){.  asser
f5710 74 28 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 3d  t( pCache->nRef=
f5720 3d 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 44  =0 && pCache->pD
f5730 69 72 74 79 3d 3d 30 20 29 3b 0a 20 20 69 66 28  irty==0 );.  if(
f5740 20 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20   pCache->pCache 
f5750 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c  ){.    sqlite3Gl
f5760 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68  obalConfig.pcach
f5770 65 2e 78 44 65 73 74 72 6f 79 28 70 43 61 63 68  e.xDestroy(pCach
f5780 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20 20 20 20  e->pCache);.    
f5790 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 3d  pCache->pCache =
f57a0 20 30 3b 0a 20 20 7d 0a 20 20 70 43 61 63 68 65   0;.  }.  pCache
f57b0 2d 3e 73 7a 50 61 67 65 20 3d 20 73 7a 50 61 67  ->szPage = szPag
f57c0 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 79 20  e;.}../*.** Try 
f57d0 74 6f 20 6f 62 74 61 69 6e 20 61 20 70 61 67 65  to obtain a page
f57e0 20 66 72 6f 6d 20 74 68 65 20 63 61 63 68 65 2e   from the cache.
f57f0 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f5800 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63  TE int sqlite3Pc
f5810 61 63 68 65 46 65 74 63 68 28 0a 20 20 50 43 61  acheFetch(.  PCa
f5820 63 68 65 20 2a 70 43 61 63 68 65 2c 20 20 20 20  che *pCache,    
f5830 20 20 20 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65     /* Obtain the
f5840 20 70 61 67 65 20 66 72 6f 6d 20 74 68 69 73 20   page from this 
f5850 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  cache */.  Pgno 
f5860 70 67 6e 6f 2c 20 20 20 20 20 20 20 20 20 20 20  pgno,           
f5870 20 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20   /* Page number 
f5880 74 6f 20 6f 62 74 61 69 6e 20 2a 2f 0a 20 20 69  to obtain */.  i
f5890 6e 74 20 63 72 65 61 74 65 46 6c 61 67 2c 20 20  nt createFlag,  
f58a0 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c       /* If true,
f58b0 20 63 72 65 61 74 65 20 70 61 67 65 20 69 66 20   create page if 
f58c0 69 74 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73  it does not exis
f58d0 74 20 61 6c 72 65 61 64 79 20 2a 2f 0a 20 20 50  t already */.  P
f58e0 67 48 64 72 20 2a 2a 70 70 50 61 67 65 20 20 20  gHdr **ppPage   
f58f0 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
f5900 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f 0a 29  e page here */.)
f5910 7b 0a 20 20 50 67 48 64 72 20 2a 70 50 61 67 65  {.  PgHdr *pPage
f5920 20 3d 20 30 3b 0a 20 20 69 6e 74 20 65 43 72 65   = 0;.  int eCre
f5930 61 74 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ate;..  assert( 
f5940 70 43 61 63 68 65 21 3d 30 20 29 3b 0a 20 20 61  pCache!=0 );.  a
f5950 73 73 65 72 74 28 20 70 67 6e 6f 3e 30 20 29 3b  ssert( pgno>0 );
f5960 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 6c  ..  /* If the pl
f5970 75 67 67 61 62 6c 65 20 63 61 63 68 65 20 28 73  uggable cache (s
f5980 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a 29 20  qlite3_pcache*) 
f5990 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61 6c 6c  has not been all
f59a0 6f 63 61 74 65 64 2c 0a 20 20 2a 2a 20 61 6c 6c  ocated,.  ** all
f59b0 6f 63 61 74 65 20 69 74 20 6e 6f 77 2e 0a 20 20  ocate it now..  
f59c0 2a 2f 0a 20 20 69 66 28 20 21 70 43 61 63 68 65  */.  if( !pCache
f59d0 2d 3e 70 43 61 63 68 65 20 26 26 20 63 72 65 61  ->pCache && crea
f59e0 74 65 46 6c 61 67 20 29 7b 0a 20 20 20 20 73 71  teFlag ){.    sq
f59f0 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 3b  lite3_pcache *p;
f5a00 0a 20 20 20 20 69 6e 74 20 6e 42 79 74 65 3b 0a  .    int nByte;.
f5a10 20 20 20 20 6e 42 79 74 65 20 3d 20 70 43 61 63      nByte = pCac
f5a20 68 65 2d 3e 73 7a 50 61 67 65 20 2b 20 70 43 61  he->szPage + pCa
f5a30 63 68 65 2d 3e 73 7a 45 78 74 72 61 20 2b 20 73  che->szExtra + s
f5a40 69 7a 65 6f 66 28 50 67 48 64 72 29 3b 0a 20 20  izeof(PgHdr);.  
f5a50 20 20 70 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f    p = sqlite3Glo
f5a60 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65  balConfig.pcache
f5a70 2e 78 43 72 65 61 74 65 28 6e 42 79 74 65 2c 20  .xCreate(nByte, 
f5a80 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
f5a90 6c 65 29 3b 0a 20 20 20 20 69 66 28 20 21 70 20  le);.    if( !p 
f5aa0 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
f5ab0 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
f5ac0 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 47    }.    sqlite3G
f5ad0 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63  lobalConfig.pcac
f5ae0 68 65 2e 78 43 61 63 68 65 73 69 7a 65 28 70 2c  he.xCachesize(p,
f5af0 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a   pCache->nMax);.
f5b00 20 20 20 20 70 43 61 63 68 65 2d 3e 70 43 61 63      pCache->pCac
f5b10 68 65 20 3d 20 70 3b 0a 20 20 7d 0a 0a 20 20 65  he = p;.  }..  e
f5b20 43 72 65 61 74 65 20 3d 20 63 72 65 61 74 65 46  Create = createF
f5b30 6c 61 67 20 3f 20 31 20 3a 20 30 3b 0a 20 20 69  lag ? 1 : 0;.  i
f5b40 66 28 20 65 43 72 65 61 74 65 20 26 26 20 28 21  f( eCreate && (!
f5b50 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62  pCache->bPurgeab
f5b60 6c 65 20 7c 7c 20 21 70 43 61 63 68 65 2d 3e 70  le || !pCache->p
f5b70 44 69 72 74 79 29 20 29 7b 0a 20 20 20 20 65 43  Dirty) ){.    eC
f5b80 72 65 61 74 65 20 3d 20 32 3b 0a 20 20 7d 0a 20  reate = 2;.  }. 
f5b90 20 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61   if( pCache->pCa
f5ba0 63 68 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65  che ){.    pPage
f5bb0 20 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c   = sqlite3Global
f5bc0 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 46  Config.pcache.xF
f5bd0 65 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 61  etch(pCache->pCa
f5be0 63 68 65 2c 20 70 67 6e 6f 2c 20 65 43 72 65 61  che, pgno, eCrea
f5bf0 74 65 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  te);.  }..  if( 
f5c00 21 70 50 61 67 65 20 26 26 20 65 43 72 65 61 74  !pPage && eCreat
f5c10 65 3d 3d 31 20 29 7b 0a 20 20 20 20 50 67 48 64  e==1 ){.    PgHd
f5c20 72 20 2a 70 50 67 3b 0a 0a 20 20 20 20 2f 2a 20  r *pPg;..    /* 
f5c30 46 69 6e 64 20 61 20 64 69 72 74 79 20 70 61 67  Find a dirty pag
f5c40 65 20 74 6f 20 77 72 69 74 65 2d 6f 75 74 20 61  e to write-out a
f5c50 6e 64 20 72 65 63 79 63 6c 65 2e 20 46 69 72 73  nd recycle. Firs
f5c60 74 20 74 72 79 20 74 6f 20 66 69 6e 64 20 61 20  t try to find a 
f5c70 0a 20 20 20 20 2a 2a 20 70 61 67 65 20 74 68 61  .    ** page tha
f5c80 74 20 64 6f 65 73 20 6e 6f 74 20 72 65 71 75 69  t does not requi
f5c90 72 65 20 61 20 6a 6f 75 72 6e 61 6c 2d 73 79 6e  re a journal-syn
f5ca0 63 20 28 6f 6e 65 20 77 69 74 68 20 50 47 48 44  c (one with PGHD
f5cb0 52 5f 4e 45 45 44 5f 53 59 4e 43 0a 20 20 20 20  R_NEED_SYNC.    
f5cc0 2a 2a 20 63 6c 65 61 72 65 64 29 2c 20 62 75 74  ** cleared), but
f5cd0 20 69 66 20 74 68 61 74 20 69 73 20 6e 6f 74 20   if that is not 
f5ce0 70 6f 73 73 69 62 6c 65 20 73 65 74 74 6c 65 20  possible settle 
f5cf0 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 0a 20  for any other . 
f5d00 20 20 20 2a 2a 20 75 6e 72 65 66 65 72 65 6e 63     ** unreferenc
f5d10 65 64 20 64 69 72 74 79 20 70 61 67 65 2e 0a 20  ed dirty page.. 
f5d20 20 20 20 2a 2f 0a 20 20 20 20 65 78 70 65 6e 73     */.    expens
f5d30 69 76 65 5f 61 73 73 65 72 74 28 20 70 63 61 63  ive_assert( pcac
f5d40 68 65 43 68 65 63 6b 53 79 6e 63 65 64 28 70 43  heCheckSynced(pC
f5d50 61 63 68 65 29 20 29 3b 0a 20 20 20 20 66 6f 72  ache) );.    for
f5d60 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70 53 79  (pPg=pCache->pSy
f5d70 6e 63 65 64 3b 20 0a 20 20 20 20 20 20 20 20 70  nced; .        p
f5d80 50 67 20 26 26 20 28 70 50 67 2d 3e 6e 52 65 66  Pg && (pPg->nRef
f5d90 20 7c 7c 20 28 70 50 67 2d 3e 66 6c 61 67 73 26   || (pPg->flags&
f5da0 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 29  PGHDR_NEED_SYNC)
f5db0 29 3b 20 0a 20 20 20 20 20 20 20 20 70 50 67 3d  ); .        pPg=
f5dc0 70 50 67 2d 3e 70 44 69 72 74 79 50 72 65 76 0a  pPg->pDirtyPrev.
f5dd0 20 20 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21      );.    if( !
f5de0 70 50 67 20 29 7b 0a 20 20 20 20 20 20 66 6f 72  pPg ){.      for
f5df0 28 70 50 67 3d 70 43 61 63 68 65 2d 3e 70 44 69  (pPg=pCache->pDi
f5e00 72 74 79 54 61 69 6c 3b 20 70 50 67 20 26 26 20  rtyTail; pPg && 
f5e10 70 50 67 2d 3e 6e 52 65 66 3b 20 70 50 67 3d 70  pPg->nRef; pPg=p
f5e20 50 67 2d 3e 70 44 69 72 74 79 50 72 65 76 29 3b  Pg->pDirtyPrev);
f5e30 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
f5e40 50 67 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  Pg ){.      int 
f5e50 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 70  rc;.      rc = p
f5e60 43 61 63 68 65 2d 3e 78 53 74 72 65 73 73 28 70  Cache->xStress(p
f5e70 43 61 63 68 65 2d 3e 70 53 74 72 65 73 73 2c 20  Cache->pStress, 
f5e80 70 50 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20  pPg);.      if( 
f5e90 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc!=SQLITE_OK &&
f5ea0 20 72 63 21 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc!=SQLITE_BUSY
f5eb0 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
f5ec0 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
f5ed0 20 20 20 7d 0a 0a 20 20 20 20 70 50 61 67 65 20     }..    pPage 
f5ee0 3d 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  = sqlite3GlobalC
f5ef0 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 46 65  onfig.pcache.xFe
f5f00 74 63 68 28 70 43 61 63 68 65 2d 3e 70 43 61 63  tch(pCache->pCac
f5f10 68 65 2c 20 70 67 6e 6f 2c 20 32 29 3b 0a 20 20  he, pgno, 2);.  
f5f20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67 65 20 29  }..  if( pPage )
f5f30 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 50 61  {.    if( 0==pPa
f5f40 67 65 2d 3e 6e 52 65 66 20 29 7b 0a 20 20 20 20  ge->nRef ){.    
f5f50 20 20 70 43 61 63 68 65 2d 3e 6e 52 65 66 2b 2b    pCache->nRef++
f5f60 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67  ;.    }.    pPag
f5f70 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 70  e->nRef++;.    p
f5f80 50 61 67 65 2d 3e 70 44 61 74 61 20 3d 20 28 76  Page->pData = (v
f5f90 6f 69 64 2a 29 26 70 50 61 67 65 5b 31 5d 3b 0a  oid*)&pPage[1];.
f5fa0 20 20 20 20 70 50 61 67 65 2d 3e 70 45 78 74 72      pPage->pExtr
f5fb0 61 20 3d 20 28 76 6f 69 64 2a 29 26 28 28 63 68  a = (void*)&((ch
f5fc0 61 72 2a 29 70 50 61 67 65 2d 3e 70 44 61 74 61  ar*)pPage->pData
f5fd0 29 5b 70 43 61 63 68 65 2d 3e 73 7a 50 61 67 65  )[pCache->szPage
f5fe0 5d 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 70 43  ];.    pPage->pC
f5ff0 61 63 68 65 20 3d 20 70 43 61 63 68 65 3b 0a 20  ache = pCache;. 
f6000 20 20 20 70 50 61 67 65 2d 3e 70 67 6e 6f 20 3d     pPage->pgno =
f6010 20 70 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 70   pgno;.    if( p
f6020 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20  gno==1 ){.      
f6030 70 43 61 63 68 65 2d 3e 70 50 61 67 65 31 20 3d  pCache->pPage1 =
f6040 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20   pPage;.    }.  
f6050 7d 0a 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50  }.  *ppPage = pP
f6060 61 67 65 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  age;.  return (p
f6070 50 61 67 65 3d 3d 30 20 26 26 20 65 43 72 65 61  Page==0 && eCrea
f6080 74 65 29 20 3f 20 53 51 4c 49 54 45 5f 4e 4f 4d  te) ? SQLITE_NOM
f6090 45 4d 20 3a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  EM : SQLITE_OK;.
f60a0 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65 6d 65  }../*.** Decreme
f60b0 6e 74 20 74 68 65 20 72 65 66 65 72 65 6e 63 65  nt the reference
f60c0 20 63 6f 75 6e 74 20 6f 6e 20 61 20 70 61 67 65   count on a page
f60d0 2e 20 49 66 20 74 68 65 20 70 61 67 65 20 69 73  . If the page is
f60e0 20 63 6c 65 61 6e 20 61 6e 64 20 74 68 65 0a 2a   clean and the.*
f60f0 2a 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  * reference coun
f6100 74 20 64 72 6f 70 73 20 74 6f 20 30 2c 20 74 68  t drops to 0, th
f6110 65 6e 20 69 74 20 69 73 20 6d 61 64 65 20 65 6c  en it is made el
f6120 69 62 6c 65 20 66 6f 72 20 72 65 63 79 63 6c 69  ible for recycli
f6130 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
f6140 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
f6150 65 33 50 63 61 63 68 65 52 65 6c 65 61 73 65 28  e3PcacheRelease(
f6160 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 61 73 73  PgHdr *p){.  ass
f6170 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
f6180 3b 0a 20 20 70 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ;.  p->nRef--;. 
f6190 20 69 66 28 20 70 2d 3e 6e 52 65 66 3d 3d 30 20   if( p->nRef==0 
f61a0 29 7b 0a 20 20 20 20 50 43 61 63 68 65 20 2a 70  ){.    PCache *p
f61b0 43 61 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68  Cache = p->pCach
f61c0 65 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 6e  e;.    pCache->n
f61d0 52 65 66 2d 2d 3b 0a 20 20 20 20 69 66 28 20 28  Ref--;.    if( (
f61e0 70 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  p->flags&PGHDR_D
f61f0 49 52 54 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20  IRTY)==0 ){.    
f6200 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70 29    pcacheUnpin(p)
f6210 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
f6220 20 20 20 2f 2a 20 4d 6f 76 65 20 74 68 65 20 70     /* Move the p
f6230 61 67 65 20 74 6f 20 74 68 65 20 68 65 61 64 20  age to the head 
f6240 6f 66 20 74 68 65 20 64 69 72 74 79 20 6c 69 73  of the dirty lis
f6250 74 2e 20 2a 2f 0a 20 20 20 20 20 20 70 63 61 63  t. */.      pcac
f6260 68 65 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74  heRemoveFromDirt
f6270 79 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 20 20  yList(p);.      
f6280 70 63 61 63 68 65 41 64 64 54 6f 44 69 72 74 79  pcacheAddToDirty
f6290 4c 69 73 74 28 70 29 3b 0a 20 20 20 20 7d 0a 20  List(p);.    }. 
f62a0 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72   }.}../*.** Incr
f62b0 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e  ease the referen
f62c0 63 65 20 63 6f 75 6e 74 20 6f 66 20 61 20 73 75  ce count of a su
f62d0 70 70 6c 69 65 64 20 70 61 67 65 20 62 79 20 31  pplied page by 1
f62e0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f62f0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f6300 50 63 61 63 68 65 52 65 66 28 50 67 48 64 72 20  PcacheRef(PgHdr 
f6310 2a 70 29 7b 0a 20 20 61 73 73 65 72 74 28 70 2d  *p){.  assert(p-
f6320 3e 6e 52 65 66 3e 30 29 3b 0a 20 20 70 2d 3e 6e  >nRef>0);.  p->n
f6330 52 65 66 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Ref++;.}../*.** 
f6340 44 72 6f 70 20 61 20 70 61 67 65 20 66 72 6f 6d  Drop a page from
f6350 20 74 68 65 20 63 61 63 68 65 2e 20 54 68 65 72   the cache. Ther
f6360 65 20 6d 75 73 74 20 62 65 20 65 78 61 63 74 6c  e must be exactl
f6370 79 20 6f 6e 65 20 72 65 66 65 72 65 6e 63 65 20  y one reference 
f6380 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65 2e 20  to the.** page. 
f6390 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 64 65  This function de
f63a0 6c 65 74 65 73 20 74 68 61 74 20 72 65 66 65 72  letes that refer
f63b0 65 6e 63 65 2c 20 73 6f 20 61 66 74 65 72 20 69  ence, so after i
f63c0 74 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a  t returns the.**
f63d0 20 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f   page pointed to
f63e0 20 62 79 20 70 20 69 73 20 69 6e 76 61 6c 69 64   by p is invalid
f63f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f6400 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f6410 50 63 61 63 68 65 44 72 6f 70 28 50 67 48 64 72  PcacheDrop(PgHdr
f6420 20 2a 70 29 7b 0a 20 20 50 43 61 63 68 65 20 2a   *p){.  PCache *
f6430 70 43 61 63 68 65 3b 0a 20 20 61 73 73 65 72 74  pCache;.  assert
f6440 28 20 70 2d 3e 6e 52 65 66 3d 3d 31 20 29 3b 0a  ( p->nRef==1 );.
f6450 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 26 50    if( p->flags&P
f6460 47 48 44 52 5f 44 49 52 54 59 20 29 7b 0a 20 20  GHDR_DIRTY ){.  
f6470 20 20 70 63 61 63 68 65 52 65 6d 6f 76 65 46 72    pcacheRemoveFr
f6480 6f 6d 44 69 72 74 79 4c 69 73 74 28 70 29 3b 0a  omDirtyList(p);.
f6490 20 20 7d 0a 20 20 70 43 61 63 68 65 20 3d 20 70    }.  pCache = p
f64a0 2d 3e 70 43 61 63 68 65 3b 0a 20 20 70 43 61 63  ->pCache;.  pCac
f64b0 68 65 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66  he->nRef--;.  if
f64c0 28 20 70 2d 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a  ( p->pgno==1 ){.
f64d0 20 20 20 20 70 43 61 63 68 65 2d 3e 70 50 61 67      pCache->pPag
f64e0 65 31 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71  e1 = 0;.  }.  sq
f64f0 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
f6500 67 2e 70 63 61 63 68 65 2e 78 55 6e 70 69 6e 28  g.pcache.xUnpin(
f6510 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20  pCache->pCache, 
f6520 70 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  p, 1);.}../*.** 
f6530 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20 70 61  Make sure the pa
f6540 67 65 20 69 73 20 6d 61 72 6b 65 64 20 61 73 20  ge is marked as 
f6550 64 69 72 74 79 2e 20 49 66 20 69 74 20 69 73 6e  dirty. If it isn
f6560 27 74 20 64 69 72 74 79 20 61 6c 72 65 61 64 79  't dirty already
f6570 2c 0a 2a 2a 20 6d 61 6b 65 20 69 74 20 73 6f 2e  ,.** make it so.
f6580 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
f6590 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50  TE void sqlite3P
f65a0 63 61 63 68 65 4d 61 6b 65 44 69 72 74 79 28 50  cacheMakeDirty(P
f65b0 67 48 64 72 20 2a 70 29 7b 0a 20 20 50 43 61 63  gHdr *p){.  PCac
f65c0 68 65 20 2a 70 43 61 63 68 65 3b 0a 20 20 70 2d  he *pCache;.  p-
f65d0 3e 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52  >flags &= ~PGHDR
f65e0 5f 44 4f 4e 54 5f 57 52 49 54 45 3b 0a 20 20 61  _DONT_WRITE;.  a
f65f0 73 73 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30  ssert( p->nRef>0
f6600 20 29 3b 0a 20 20 69 66 28 20 30 3d 3d 28 70 2d   );.  if( 0==(p-
f6610 3e 66 6c 61 67 73 20 26 20 50 47 48 44 52 5f 44  >flags & PGHDR_D
f6620 49 52 54 59 29 20 29 7b 0a 20 20 20 20 70 43 61  IRTY) ){.    pCa
f6630 63 68 65 20 3d 20 70 2d 3e 70 43 61 63 68 65 3b  che = p->pCache;
f6640 0a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d  .    p->flags |=
f6650 20 50 47 48 44 52 5f 44 49 52 54 59 3b 0a 20 20   PGHDR_DIRTY;.  
f6660 20 20 70 63 61 63 68 65 41 64 64 54 6f 44 69 72    pcacheAddToDir
f6670 74 79 4c 69 73 74 28 20 70 29 3b 0a 20 20 7d 0a  tyList( p);.  }.
f6680 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75  }../*.** Make su
f6690 72 65 20 74 68 65 20 70 61 67 65 20 69 73 20 6d  re the page is m
f66a0 61 72 6b 65 64 20 61 73 20 63 6c 65 61 6e 2e 20  arked as clean. 
f66b0 49 66 20 69 74 20 69 73 6e 27 74 20 63 6c 65 61  If it isn't clea
f66c0 6e 20 61 6c 72 65 61 64 79 2c 0a 2a 2a 20 6d 61  n already,.** ma
f66d0 6b 65 20 69 74 20 73 6f 2e 0a 2a 2f 0a 53 51 4c  ke it so..*/.SQL
f66e0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f66f0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61   sqlite3PcacheMa
f6700 6b 65 43 6c 65 61 6e 28 50 67 48 64 72 20 2a 70  keClean(PgHdr *p
f6710 29 7b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61  ){.  if( (p->fla
f6720 67 73 20 26 20 50 47 48 44 52 5f 44 49 52 54 59  gs & PGHDR_DIRTY
f6730 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65 52  ) ){.    pcacheR
f6740 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c 69  emoveFromDirtyLi
f6750 73 74 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c  st(p);.    p->fl
f6760 61 67 73 20 26 3d 20 7e 28 50 47 48 44 52 5f 44  ags &= ~(PGHDR_D
f6770 49 52 54 59 7c 50 47 48 44 52 5f 4e 45 45 44 5f  IRTY|PGHDR_NEED_
f6780 53 59 4e 43 29 3b 0a 20 20 20 20 69 66 28 20 70  SYNC);.    if( p
f6790 2d 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20  ->nRef==0 ){.   
f67a0 20 20 20 70 63 61 63 68 65 55 6e 70 69 6e 28 70     pcacheUnpin(p
f67b0 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
f67c0 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 65 76 65 72 79  /*.** Make every
f67d0 20 70 61 67 65 20 69 6e 20 74 68 65 20 63 61 63   page in the cac
f67e0 68 65 20 63 6c 65 61 6e 2e 0a 2a 2f 0a 53 51 4c  he clean..*/.SQL
f67f0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f6800 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c   sqlite3PcacheCl
f6810 65 61 6e 41 6c 6c 28 50 43 61 63 68 65 20 2a 70  eanAll(PCache *p
f6820 43 61 63 68 65 29 7b 0a 20 20 50 67 48 64 72 20  Cache){.  PgHdr 
f6830 2a 70 3b 0a 20 20 77 68 69 6c 65 28 20 28 70 20  *p;.  while( (p 
f6840 3d 20 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  = pCache->pDirty
f6850 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
f6860 74 65 33 50 63 61 63 68 65 4d 61 6b 65 43 6c 65  te3PcacheMakeCle
f6870 61 6e 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  an(p);.  }.}../*
f6880 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 50 47  .** Clear the PG
f6890 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 66 6c  HDR_NEED_SYNC fl
f68a0 61 67 20 66 72 6f 6d 20 61 6c 6c 20 64 69 72 74  ag from all dirt
f68b0 79 20 70 61 67 65 73 2e 0a 2a 2f 0a 53 51 4c 49  y pages..*/.SQLI
f68c0 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
f68d0 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
f68e0 61 72 53 79 6e 63 46 6c 61 67 73 28 50 43 61 63  arSyncFlags(PCac
f68f0 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50  he *pCache){.  P
f6900 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  gHdr *p;.  for(p
f6910 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b  =pCache->pDirty;
f6920 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e   p; p=p->pDirtyN
f6930 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 66 6c 61  ext){.    p->fla
f6940 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45  gs &= ~PGHDR_NEE
f6950 44 5f 53 59 4e 43 3b 0a 20 20 7d 0a 20 20 70 43  D_SYNC;.  }.  pC
f6960 61 63 68 65 2d 3e 70 53 79 6e 63 65 64 20 3d 20  ache->pSynced = 
f6970 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 54 61  pCache->pDirtyTa
f6980 69 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61  il;.}../*.** Cha
f6990 6e 67 65 20 74 68 65 20 70 61 67 65 20 6e 75 6d  nge the page num
f69a0 62 65 72 20 6f 66 20 70 61 67 65 20 70 20 74 6f  ber of page p to
f69b0 20 6e 65 77 50 67 6e 6f 2e 20 0a 2a 2f 0a 53 51   newPgno. .*/.SQ
f69c0 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
f69d0 64 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d  d sqlite3PcacheM
f69e0 6f 76 65 28 50 67 48 64 72 20 2a 70 2c 20 50 67  ove(PgHdr *p, Pg
f69f0 6e 6f 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 50  no newPgno){.  P
f6a00 43 61 63 68 65 20 2a 70 43 61 63 68 65 20 3d 20  Cache *pCache = 
f6a10 70 2d 3e 70 43 61 63 68 65 3b 0a 20 20 61 73 73  p->pCache;.  ass
f6a20 65 72 74 28 20 70 2d 3e 6e 52 65 66 3e 30 20 29  ert( p->nRef>0 )
f6a30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 65 77 50  ;.  assert( newP
f6a40 67 6e 6f 3e 30 20 29 3b 0a 20 20 73 71 6c 69 74  gno>0 );.  sqlit
f6a50 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
f6a60 63 61 63 68 65 2e 78 52 65 6b 65 79 28 70 43 61  cache.xRekey(pCa
f6a70 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 2c 20  che->pCache, p, 
f6a80 70 2d 3e 70 67 6e 6f 2c 20 6e 65 77 50 67 6e 6f  p->pgno, newPgno
f6a90 29 3b 0a 20 20 70 2d 3e 70 67 6e 6f 20 3d 20 6e  );.  p->pgno = n
f6aa0 65 77 50 67 6e 6f 3b 0a 20 20 69 66 28 20 28 70  ewPgno;.  if( (p
f6ab0 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49  ->flags&PGHDR_DI
f6ac0 52 54 59 29 20 26 26 20 28 70 2d 3e 66 6c 61 67  RTY) && (p->flag
f6ad0 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e  s&PGHDR_NEED_SYN
f6ae0 43 29 20 29 7b 0a 20 20 20 20 70 63 61 63 68 65  C) ){.    pcache
f6af0 52 65 6d 6f 76 65 46 72 6f 6d 44 69 72 74 79 4c  RemoveFromDirtyL
f6b00 69 73 74 28 70 29 3b 0a 20 20 20 20 70 63 61 63  ist(p);.    pcac
f6b10 68 65 41 64 64 54 6f 44 69 72 74 79 4c 69 73 74  heAddToDirtyList
f6b20 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  (p);.  }.}../*.*
f6b30 2a 20 44 72 6f 70 20 65 76 65 72 79 20 63 61 63  * Drop every cac
f6b40 68 65 20 65 6e 74 72 79 20 77 68 6f 73 65 20 70  he entry whose p
f6b50 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 67 72  age number is gr
f6b60 65 61 74 65 72 20 74 68 61 6e 20 22 70 67 6e 6f  eater than "pgno
f6b70 22 2e 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 65 72  ". The.** caller
f6b80 20 6d 75 73 74 20 65 6e 73 75 72 65 20 74 68 61   must ensure tha
f6b90 74 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 6f  t there are no o
f6ba0 75 74 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72  utstanding refer
f6bb0 65 6e 63 65 73 20 74 6f 20 61 6e 79 20 70 61 67  ences to any pag
f6bc0 65 73 0a 2a 2a 20 6f 74 68 65 72 20 74 68 61 6e  es.** other than
f6bd0 20 70 61 67 65 20 31 20 77 69 74 68 20 61 20 70   page 1 with a p
f6be0 61 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61 74  age number great
f6bf0 65 72 20 74 68 61 6e 20 70 67 6e 6f 2e 0a 2a 2a  er than pgno..**
f6c00 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20  .** If there is 
f6c10 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  a reference to p
f6c20 61 67 65 20 31 20 61 6e 64 20 74 68 65 20 70 67  age 1 and the pg
f6c30 6e 6f 20 70 61 72 61 6d 65 74 65 72 20 70 61 73  no parameter pas
f6c40 73 65 64 20 74 6f 20 74 68 69 73 0a 2a 2a 20 66  sed to this.** f
f6c50 75 6e 63 74 69 6f 6e 20 69 73 20 30 2c 20 74 68  unction is 0, th
f6c60 65 6e 20 74 68 65 20 64 61 74 61 20 61 72 65 61  en the data area
f6c70 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
f6c80 20 70 61 67 65 20 31 20 69 73 20 7a 65 72 6f 65   page 1 is zeroe
f6c90 64 2c 20 62 75 74 0a 2a 2a 20 74 68 65 20 70 61  d, but.** the pa
f6ca0 67 65 20 6f 62 6a 65 63 74 20 69 73 20 6e 6f 74  ge object is not
f6cb0 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53 51 4c   dropped..*/.SQL
f6cc0 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f6cd0 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 54 72   sqlite3PcacheTr
f6ce0 75 6e 63 61 74 65 28 50 43 61 63 68 65 20 2a 70  uncate(PCache *p
f6cf0 43 61 63 68 65 2c 20 50 67 6e 6f 20 70 67 6e 6f  Cache, Pgno pgno
f6d00 29 7b 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  ){.  if( pCache-
f6d10 3e 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 50  >pCache ){.    P
f6d20 67 48 64 72 20 2a 70 3b 0a 20 20 20 20 50 67 48  gHdr *p;.    PgH
f6d30 64 72 20 2a 70 4e 65 78 74 3b 0a 20 20 20 20 66  dr *pNext;.    f
f6d40 6f 72 28 70 3d 70 43 61 63 68 65 2d 3e 70 44 69  or(p=pCache->pDi
f6d50 72 74 79 3b 20 70 3b 20 70 3d 70 4e 65 78 74 29  rty; p; p=pNext)
f6d60 7b 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20  {.      pNext = 
f6d70 70 2d 3e 70 44 69 72 74 79 4e 65 78 74 3b 0a 20  p->pDirtyNext;. 
f6d80 20 20 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f       if( p->pgno
f6d90 3e 70 67 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20  >pgno ){.       
f6da0 20 61 73 73 65 72 74 28 20 70 2d 3e 66 6c 61 67   assert( p->flag
f6db0 73 26 50 47 48 44 52 5f 44 49 52 54 59 20 29 3b  s&PGHDR_DIRTY );
f6dc0 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
f6dd0 50 63 61 63 68 65 4d 61 6b 65 43 6c 65 61 6e 28  PcacheMakeClean(
f6de0 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
f6df0 7d 0a 20 20 20 20 69 66 28 20 70 67 6e 6f 3d 3d  }.    if( pgno==
f6e00 30 20 26 26 20 70 43 61 63 68 65 2d 3e 70 50 61  0 && pCache->pPa
f6e10 67 65 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  ge1 ){.      mem
f6e20 73 65 74 28 70 43 61 63 68 65 2d 3e 70 50 61 67  set(pCache->pPag
f6e30 65 31 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 43  e1->pData, 0, pC
f6e40 61 63 68 65 2d 3e 73 7a 50 61 67 65 29 3b 0a 20  ache->szPage);. 
f6e50 20 20 20 20 20 70 67 6e 6f 20 3d 20 31 3b 0a 20       pgno = 1;. 
f6e60 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
f6e70 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63 61  GlobalConfig.pca
f6e80 63 68 65 2e 78 54 72 75 6e 63 61 74 65 28 70 43  che.xTruncate(pC
f6e90 61 63 68 65 2d 3e 70 43 61 63 68 65 2c 20 70 67  ache->pCache, pg
f6ea0 6e 6f 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  no+1);.  }.}../*
f6eb0 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 63 61 63 68  .** Close a cach
f6ec0 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
f6ed0 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
f6ee0 33 50 63 61 63 68 65 43 6c 6f 73 65 28 50 43 61  3PcacheClose(PCa
f6ef0 63 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20  che *pCache){.  
f6f00 69 66 28 20 70 43 61 63 68 65 2d 3e 70 43 61 63  if( pCache->pCac
f6f10 68 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  he ){.    sqlite
f6f20 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 63  3GlobalConfig.pc
f6f30 61 63 68 65 2e 78 44 65 73 74 72 6f 79 28 70 43  ache.xDestroy(pC
f6f40 61 63 68 65 2d 3e 70 43 61 63 68 65 29 3b 0a 20  ache->pCache);. 
f6f50 20 7d 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 44 69 73   }.}../* .** Dis
f6f60 63 61 72 64 20 74 68 65 20 63 6f 6e 74 65 6e 74  card the content
f6f70 73 20 6f 66 20 74 68 65 20 63 61 63 68 65 2e 0a  s of the cache..
f6f80 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
f6f90 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
f6fa0 63 68 65 43 6c 65 61 72 28 50 43 61 63 68 65 20  cheClear(PCache 
f6fb0 2a 70 43 61 63 68 65 29 7b 0a 20 20 73 71 6c 69  *pCache){.  sqli
f6fc0 74 65 33 50 63 61 63 68 65 54 72 75 6e 63 61 74  te3PcacheTruncat
f6fd0 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20 20  e(pCache, 0);.  
f6fe0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
f6ff0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 72 67 65  ;.}../*.** Merge
f7000 20 74 77 6f 20 6c 69 73 74 73 20 6f 66 20 70 61   two lists of pa
f7010 67 65 73 20 63 6f 6e 6e 65 63 74 65 64 20 62 79  ges connected by
f7020 20 70 44 69 72 74 79 20 61 6e 64 20 69 6e 20 70   pDirty and in p
f7030 67 6e 6f 20 6f 72 64 65 72 2e 0a 2a 2a 20 44 6f  gno order..** Do
f7040 20 6e 6f 74 20 62 6f 74 68 20 66 69 78 69 6e 67   not both fixing
f7050 20 74 68 65 20 70 44 69 72 74 79 50 72 65 76 20   the pDirtyPrev 
f7060 70 6f 69 6e 74 65 72 73 2e 0a 2a 2f 0a 73 74 61  pointers..*/.sta
f7070 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68  tic PgHdr *pcach
f7080 65 4d 65 72 67 65 44 69 72 74 79 4c 69 73 74 28  eMergeDirtyList(
f7090 50 67 48 64 72 20 2a 70 41 2c 20 50 67 48 64 72  PgHdr *pA, PgHdr
f70a0 20 2a 70 42 29 7b 0a 20 20 50 67 48 64 72 20 72   *pB){.  PgHdr r
f70b0 65 73 75 6c 74 2c 20 2a 70 54 61 69 6c 3b 0a 20  esult, *pTail;. 
f70c0 20 70 54 61 69 6c 20 3d 20 26 72 65 73 75 6c 74   pTail = &result
f70d0 3b 0a 20 20 77 68 69 6c 65 28 20 70 41 20 26 26  ;.  while( pA &&
f70e0 20 70 42 20 29 7b 0a 20 20 20 20 69 66 28 20 70   pB ){.    if( p
f70f0 41 2d 3e 70 67 6e 6f 3c 70 42 2d 3e 70 67 6e 6f  A->pgno<pB->pgno
f7100 20 29 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d   ){.      pTail-
f7110 3e 70 44 69 72 74 79 20 3d 20 70 41 3b 0a 20 20  >pDirty = pA;.  
f7120 20 20 20 20 70 54 61 69 6c 20 3d 20 70 41 3b 0a      pTail = pA;.
f7130 20 20 20 20 20 20 70 41 20 3d 20 70 41 2d 3e 70        pA = pA->p
f7140 44 69 72 74 79 3b 0a 20 20 20 20 7d 65 6c 73 65  Dirty;.    }else
f7150 7b 0a 20 20 20 20 20 20 70 54 61 69 6c 2d 3e 70  {.      pTail->p
f7160 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 20 20  Dirty = pB;.    
f7170 20 20 70 54 61 69 6c 20 3d 20 70 42 3b 0a 20 20    pTail = pB;.  
f7180 20 20 20 20 70 42 20 3d 20 70 42 2d 3e 70 44 69      pB = pB->pDi
f7190 72 74 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  rty;.    }.  }. 
f71a0 20 69 66 28 20 70 41 20 29 7b 0a 20 20 20 20 70   if( pA ){.    p
f71b0 54 61 69 6c 2d 3e 70 44 69 72 74 79 20 3d 20 70  Tail->pDirty = p
f71c0 41 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  A;.  }else if( p
f71d0 42 20 29 7b 0a 20 20 20 20 70 54 61 69 6c 2d 3e  B ){.    pTail->
f71e0 70 44 69 72 74 79 20 3d 20 70 42 3b 0a 20 20 7d  pDirty = pB;.  }
f71f0 65 6c 73 65 7b 0a 20 20 20 20 70 54 61 69 6c 2d  else{.    pTail-
f7200 3e 70 44 69 72 74 79 20 3d 20 30 3b 0a 20 20 7d  >pDirty = 0;.  }
f7210 0a 20 20 72 65 74 75 72 6e 20 72 65 73 75 6c 74  .  return result
f7220 2e 70 44 69 72 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a  .pDirty;.}../*.*
f7230 2a 20 53 6f 72 74 20 74 68 65 20 6c 69 73 74 20  * Sort the list 
f7240 6f 66 20 70 61 67 65 73 20 69 6e 20 61 63 63 65  of pages in acce
f7250 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 70  nding order by p
f7260 67 6e 6f 2e 20 20 50 61 67 65 73 20 61 72 65 0a  gno.  Pages are.
f7270 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 20  ** connected by 
f7280 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 73 2e  pDirty pointers.
f7290 20 20 54 68 65 20 70 44 69 72 74 79 50 72 65 76    The pDirtyPrev
f72a0 20 70 6f 69 6e 74 65 72 73 20 61 72 65 0a 2a 2a   pointers are.**
f72b0 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 74 68   corrupted by th
f72c0 69 73 20 73 6f 72 74 2e 0a 2a 2f 0a 23 64 65 66  is sort..*/.#def
f72d0 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45  ine N_SORT_BUCKE
f72e0 54 5f 41 4c 4c 4f 43 20 32 35 0a 23 64 65 66 69  T_ALLOC 25.#defi
f72f0 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54  ne N_SORT_BUCKET
f7300 20 20 20 20 20 20 20 32 35 0a 23 69 66 64 65 66         25.#ifdef
f7310 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
f7320 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72  nt sqlite3_pager
f7330 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74 20 3d  _n_sort_bucket =
f7340 20 30 3b 0a 20 20 23 75 6e 64 65 66 20 4e 5f 53   0;.  #undef N_S
f7350 4f 52 54 5f 42 55 43 4b 45 54 0a 20 20 23 64 65  ORT_BUCKET.  #de
f7360 66 69 6e 65 20 4e 5f 53 4f 52 54 5f 42 55 43 4b  fine N_SORT_BUCK
f7370 45 54 20 5c 0a 20 20 20 28 73 71 6c 69 74 65 33  ET \.   (sqlite3
f7380 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74 5f 62 75  _pager_n_sort_bu
f7390 63 6b 65 74 3f 73 71 6c 69 74 65 33 5f 70 61 67  cket?sqlite3_pag
f73a0 65 72 5f 6e 5f 73 6f 72 74 5f 62 75 63 6b 65 74  er_n_sort_bucket
f73b0 3a 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 5f 41  :N_SORT_BUCKET_A
f73c0 4c 4c 4f 43 29 0a 23 65 6e 64 69 66 0a 73 74 61  LLOC).#endif.sta
f73d0 74 69 63 20 50 67 48 64 72 20 2a 70 63 61 63 68  tic PgHdr *pcach
f73e0 65 53 6f 72 74 44 69 72 74 79 4c 69 73 74 28 50  eSortDirtyList(P
f73f0 67 48 64 72 20 2a 70 49 6e 29 7b 0a 20 20 50 67  gHdr *pIn){.  Pg
f7400 48 64 72 20 2a 61 5b 4e 5f 53 4f 52 54 5f 42 55  Hdr *a[N_SORT_BU
f7410 43 4b 45 54 5f 41 4c 4c 4f 43 5d 2c 20 2a 70 3b  CKET_ALLOC], *p;
f7420 0a 20 20 69 6e 74 20 69 3b 0a 20 20 6d 65 6d 73  .  int i;.  mems
f7430 65 74 28 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28  et(a, 0, sizeof(
f7440 61 29 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 49  a));.  while( pI
f7450 6e 20 29 7b 0a 20 20 20 20 70 20 3d 20 70 49 6e  n ){.    p = pIn
f7460 3b 0a 20 20 20 20 70 49 6e 20 3d 20 70 2d 3e 70  ;.    pIn = p->p
f7470 44 69 72 74 79 3b 0a 20 20 20 20 70 2d 3e 70 44  Dirty;.    p->pD
f7480 69 72 74 79 20 3d 20 30 3b 0a 20 20 20 20 66 6f  irty = 0;.    fo
f7490 72 28 69 3d 30 3b 20 69 3c 4e 5f 53 4f 52 54 5f  r(i=0; i<N_SORT_
f74a0 42 55 43 4b 45 54 2d 31 3b 20 69 2b 2b 29 7b 0a  BUCKET-1; i++){.
f74b0 20 20 20 20 20 20 69 66 28 20 61 5b 69 5d 3d 3d        if( a[i]==
f74c0 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 5b 69  0 ){.        a[i
f74d0 5d 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 62  ] = p;.        b
f74e0 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  reak;.      }els
f74f0 65 7b 0a 20 20 20 20 20 20 20 20 70 20 3d 20 70  e{.        p = p
f7500 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c  cacheMergeDirtyL
f7510 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20  ist(a[i], p);.  
f7520 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 30 3b 0a        a[i] = 0;.
f7530 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
f7540 20 20 69 66 28 20 69 3d 3d 4e 5f 53 4f 52 54 5f    if( i==N_SORT_
f7550 42 55 43 4b 45 54 2d 31 20 29 7b 0a 20 20 20 20  BUCKET-1 ){.    
f7560 20 20 2f 2a 20 43 6f 76 65 72 61 67 65 3a 20 54    /* Coverage: T
f7570 6f 20 67 65 74 20 68 65 72 65 2c 20 74 68 65 72  o get here, ther
f7580 65 20 6e 65 65 64 20 74 6f 20 62 65 20 32 5e 28  e need to be 2^(
f7590 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 29 20 0a  N_SORT_BUCKET) .
f75a0 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74        ** element
f75b0 73 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 6c  s in the input l
f75c0 69 73 74 2e 20 54 68 69 73 20 69 73 20 70 6f 73  ist. This is pos
f75d0 73 69 62 6c 65 2c 20 62 75 74 20 69 6d 70 72 61  sible, but impra
f75e0 63 74 69 63 61 6c 2e 0a 20 20 20 20 20 20 2a 2a  ctical..      **
f75f0 20 54 65 73 74 69 6e 67 20 74 68 69 73 20 6c 69   Testing this li
f7600 6e 65 20 69 73 20 74 68 65 20 70 6f 69 6e 74 20  ne is the point 
f7610 6f 66 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  of global variab
f7620 6c 65 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  le.      ** sqli
f7630 74 65 33 5f 70 61 67 65 72 5f 6e 5f 73 6f 72 74  te3_pager_n_sort
f7640 5f 62 75 63 6b 65 74 2e 0a 20 20 20 20 20 20 2a  _bucket..      *
f7650 2f 0a 20 20 20 20 20 20 61 5b 69 5d 20 3d 20 70  /.      a[i] = p
f7660 63 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c  cacheMergeDirtyL
f7670 69 73 74 28 61 5b 69 5d 2c 20 70 29 3b 0a 20 20  ist(a[i], p);.  
f7680 20 20 7d 0a 20 20 7d 0a 20 20 70 20 3d 20 61 5b    }.  }.  p = a[
f7690 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  0];.  for(i=1; i
f76a0 3c 4e 5f 53 4f 52 54 5f 42 55 43 4b 45 54 3b 20  <N_SORT_BUCKET; 
f76b0 69 2b 2b 29 7b 0a 20 20 20 20 70 20 3d 20 70 63  i++){.    p = pc
f76c0 61 63 68 65 4d 65 72 67 65 44 69 72 74 79 4c 69  acheMergeDirtyLi
f76d0 73 74 28 70 2c 20 61 5b 69 5d 29 3b 0a 20 20 7d  st(p, a[i]);.  }
f76e0 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
f76f0 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 6c  /*.** Return a l
f7700 69 73 74 20 6f 66 20 61 6c 6c 20 64 69 72 74 79  ist of all dirty
f7710 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 63 61   pages in the ca
f7720 63 68 65 2c 20 73 6f 72 74 65 64 20 62 79 20 70  che, sorted by p
f7730 61 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 53  age number..*/.S
f7740 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 67  QLITE_PRIVATE Pg
f7750 48 64 72 20 2a 73 71 6c 69 74 65 33 50 63 61 63  Hdr *sqlite3Pcac
f7760 68 65 44 69 72 74 79 4c 69 73 74 28 50 43 61 63  heDirtyList(PCac
f7770 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 50  he *pCache){.  P
f7780 67 48 64 72 20 2a 70 3b 0a 20 20 66 6f 72 28 70  gHdr *p;.  for(p
f7790 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79 3b  =pCache->pDirty;
f77a0 20 70 3b 20 70 3d 70 2d 3e 70 44 69 72 74 79 4e   p; p=p->pDirtyN
f77b0 65 78 74 29 7b 0a 20 20 20 20 70 2d 3e 70 44 69  ext){.    p->pDi
f77c0 72 74 79 20 3d 20 70 2d 3e 70 44 69 72 74 79 4e  rty = p->pDirtyN
f77d0 65 78 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ext;.  }.  retur
f77e0 6e 20 70 63 61 63 68 65 53 6f 72 74 44 69 72 74  n pcacheSortDirt
f77f0 79 4c 69 73 74 28 70 43 61 63 68 65 2d 3e 70 44  yList(pCache->pD
f7800 69 72 74 79 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  irty);.}../* .**
f7810 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61   Return the tota
f7820 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 65 66 65  l number of refe
f7830 72 65 6e 63 65 64 20 70 61 67 65 73 20 68 65 6c  renced pages hel
f7840 64 20 62 79 20 74 68 65 20 63 61 63 68 65 2e 0a  d by the cache..
f7850 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
f7860 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 63 61  E int sqlite3Pca
f7870 63 68 65 52 65 66 43 6f 75 6e 74 28 50 43 61 63  cheRefCount(PCac
f7880 68 65 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 72  he *pCache){.  r
f7890 65 74 75 72 6e 20 70 43 61 63 68 65 2d 3e 6e 52  eturn pCache->nR
f78a0 65 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ef;.}../*.** Ret
f78b0 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
f78c0 66 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20  f references to 
f78d0 74 68 65 20 70 61 67 65 20 73 75 70 70 6c 69 65  the page supplie
f78e0 64 20 61 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  d as an argument
f78f0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f7900 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
f7910 63 61 63 68 65 50 61 67 65 52 65 66 63 6f 75 6e  cachePageRefcoun
f7920 74 28 50 67 48 64 72 20 2a 70 29 7b 0a 20 20 72  t(PgHdr *p){.  r
f7930 65 74 75 72 6e 20 70 2d 3e 6e 52 65 66 3b 0a 7d  eturn p->nRef;.}
f7940 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20  ../* .** Return 
f7950 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72  the total number
f7960 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68 65   of pages in the
f7970 20 63 61 63 68 65 2e 0a 2a 2f 0a 53 51 4c 49 54   cache..*/.SQLIT
f7980 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
f7990 6c 69 74 65 33 50 63 61 63 68 65 50 61 67 65 63  lite3PcachePagec
f79a0 6f 75 6e 74 28 50 43 61 63 68 65 20 2a 70 43 61  ount(PCache *pCa
f79b0 63 68 65 29 7b 0a 20 20 69 6e 74 20 6e 50 61 67  che){.  int nPag
f79c0 65 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 43 61  e = 0;.  if( pCa
f79d0 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b 0a 20  che->pCache ){. 
f79e0 20 20 20 6e 50 61 67 65 20 3d 20 73 71 6c 69 74     nPage = sqlit
f79f0 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70  e3GlobalConfig.p
f7a00 63 61 63 68 65 2e 78 50 61 67 65 63 6f 75 6e 74  cache.xPagecount
f7a10 28 70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 29  (pCache->pCache)
f7a20 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e  ;.  }.  return n
f7a30 50 61 67 65 3b 0a 7d 0a 0a 23 69 66 64 65 66 20  Page;.}..#ifdef 
f7a40 53 51 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a  SQLITE_TEST./*.*
f7a50 2a 20 47 65 74 20 74 68 65 20 73 75 67 67 65 73  * Get the sugges
f7a60 74 65 64 20 63 61 63 68 65 2d 73 69 7a 65 20 76  ted cache-size v
f7a70 61 6c 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  alue..*/.SQLITE_
f7a80 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
f7a90 74 65 33 50 63 61 63 68 65 47 65 74 43 61 63 68  te3PcacheGetCach
f7aa0 65 73 69 7a 65 28 50 43 61 63 68 65 20 2a 70 43  esize(PCache *pC
f7ab0 61 63 68 65 29 7b 0a 20 20 72 65 74 75 72 6e 20  ache){.  return 
f7ac0 70 43 61 63 68 65 2d 3e 6e 4d 61 78 3b 0a 7d 0a  pCache->nMax;.}.
f7ad0 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 65  #endif../*.** Se
f7ae0 74 20 74 68 65 20 73 75 67 67 65 73 74 65 64 20  t the suggested 
f7af0 63 61 63 68 65 2d 73 69 7a 65 20 76 61 6c 75 65  cache-size value
f7b00 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
f7b10 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
f7b20 50 63 61 63 68 65 53 65 74 43 61 63 68 65 73 69  PcacheSetCachesi
f7b30 7a 65 28 50 43 61 63 68 65 20 2a 70 43 61 63 68  ze(PCache *pCach
f7b40 65 2c 20 69 6e 74 20 6d 78 50 61 67 65 29 7b 0a  e, int mxPage){.
f7b50 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 20 3d    pCache->nMax =
f7b60 20 6d 78 50 61 67 65 3b 0a 20 20 69 66 28 20 70   mxPage;.  if( p
f7b70 43 61 63 68 65 2d 3e 70 43 61 63 68 65 20 29 7b  Cache->pCache ){
f7b80 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
f7b90 61 6c 43 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e  alConfig.pcache.
f7ba0 78 43 61 63 68 65 73 69 7a 65 28 70 43 61 63 68  xCachesize(pCach
f7bb0 65 2d 3e 70 43 61 63 68 65 2c 20 6d 78 50 61 67  e->pCache, mxPag
f7bc0 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64 65  e);.  }.}..#ifde
f7bd0 66 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  f SQLITE_CHECK_P
f7be0 41 47 45 53 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 61  AGES./*.** For a
f7bf0 6c 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 63  ll dirty pages c
f7c00 75 72 72 65 6e 74 6c 79 20 69 6e 20 74 68 65 20  urrently in the 
f7c10 63 61 63 68 65 2c 20 69 6e 76 6f 6b 65 20 74 68  cache, invoke th
f7c20 65 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20 63  e specified.** c
f7c30 61 6c 6c 62 61 63 6b 2e 20 54 68 69 73 20 69 73  allback. This is
f7c40 20 6f 6e 6c 79 20 75 73 65 64 20 69 66 20 74 68   only used if th
f7c50 65 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50  e SQLITE_CHECK_P
f7c60 41 47 45 53 20 6d 61 63 72 6f 20 69 73 0a 2a 2a  AGES macro is.**
f7c70 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a 53 51 4c   defined..*/.SQL
f7c80 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
f7c90 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49 74   sqlite3PcacheIt
f7ca0 65 72 61 74 65 44 69 72 74 79 28 50 43 61 63 68  erateDirty(PCach
f7cb0 65 20 2a 70 43 61 63 68 65 2c 20 76 6f 69 64 20  e *pCache, void 
f7cc0 28 2a 78 49 74 65 72 29 28 50 67 48 64 72 20 2a  (*xIter)(PgHdr *
f7cd0 29 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 44 69  )){.  PgHdr *pDi
f7ce0 72 74 79 3b 0a 20 20 66 6f 72 28 70 44 69 72 74  rty;.  for(pDirt
f7cf0 79 3d 70 43 61 63 68 65 2d 3e 70 44 69 72 74 79  y=pCache->pDirty
f7d00 3b 20 70 44 69 72 74 79 3b 20 70 44 69 72 74 79  ; pDirty; pDirty
f7d10 3d 70 44 69 72 74 79 2d 3e 70 44 69 72 74 79 4e  =pDirty->pDirtyN
f7d20 65 78 74 29 7b 0a 20 20 20 20 78 49 74 65 72 28  ext){.    xIter(
f7d30 70 44 69 72 74 79 29 3b 0a 20 20 7d 0a 7d 0a 23  pDirty);.  }.}.#
f7d40 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  endif.../*******
f7d50 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
f7d60 63 61 63 68 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  cache.c ********
f7d70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7d80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7d90 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
f7da0 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
f7db0 6c 65 20 70 63 61 63 68 65 31 2e 63 20 2a 2a 2a  le pcache1.c ***
f7dc0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7dd0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7de0 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
f7df0 30 38 20 4e 6f 76 65 6d 62 65 72 20 30 35 0a 2a  08 November 05.*
f7e00 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
f7e10 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
f7e20 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
f7e30 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
f7e40 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
f7e50 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
f7e60 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
f7e70 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
f7e80 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
f7e90 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
f7ea0 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
f7eb0 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
f7ec0 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
f7ed0 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
f7ee0 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
f7ef0 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
f7f00 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
f7f10 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
f7f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7f40 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f7f50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a  *************.**
f7f60 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
f7f70 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 64 65 66  plements the def
f7f80 61 75 6c 74 20 70 61 67 65 20 63 61 63 68 65 20  ault page cache 
f7f90 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 28  implementation (
f7fa0 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 70  the.** sqlite3_p
f7fb0 63 61 63 68 65 20 69 6e 74 65 72 66 61 63 65 29  cache interface)
f7fc0 2e 20 49 74 20 61 6c 73 6f 20 63 6f 6e 74 61 69  . It also contai
f7fd0 6e 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69  ns part of the i
f7fe0 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a  mplementation.**
f7ff0 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 43   of the SQLITE_C
f8000 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 20  ONFIG_PAGECACHE 
f8010 61 6e 64 20 73 71 6c 69 74 65 33 5f 72 65 6c 65  and sqlite3_rele
f8020 61 73 65 5f 6d 65 6d 6f 72 79 28 29 20 66 65 61  ase_memory() fea
f8030 74 75 72 65 73 2e 0a 2a 2a 20 49 66 20 74 68 65  tures..** If the
f8040 20 64 65 66 61 75 6c 74 20 70 61 67 65 20 63 61   default page ca
f8050 63 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  che implementati
f8060 6f 6e 20 69 73 20 6f 76 65 72 72 69 64 65 6e 2c  on is overriden,
f8070 20 74 68 65 6e 20 6e 65 69 74 68 65 72 20 6f 66   then neither of
f8080 0a 2a 2a 20 74 68 65 73 65 20 74 77 6f 20 66 65  .** these two fe
f8090 61 74 75 72 65 73 20 61 72 65 20 61 76 61 69 6c  atures are avail
f80a0 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  able..**.** @(#)
f80b0 20 24 49 64 3a 20 70 63 61 63 68 65 31 2e 63 2c   $Id: pcache1.c,
f80c0 76 20 31 2e 33 20 32 30 30 38 2f 31 31 2f 31 39  v 1.3 2008/11/19
f80d0 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 6c   09:05:27 daniel
f80e0 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
f80f0 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
f8100 50 43 61 63 68 65 31 20 50 43 61 63 68 65 31 3b  PCache1 PCache1;
f8110 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
f8120 50 67 48 64 72 31 20 50 67 48 64 72 31 3b 0a 74  PgHdr1 PgHdr1;.t
f8130 79 70 65 64 65 66 20 73 74 72 75 63 74 20 50 67  ypedef struct Pg
f8140 46 72 65 65 73 6c 6f 74 20 50 67 46 72 65 65 73  Freeslot PgFrees
f8150 6c 6f 74 3b 0a 0a 2f 2a 20 50 6f 69 6e 74 65 72  lot;../* Pointer
f8160 73 20 74 6f 20 73 74 72 75 63 74 75 72 65 73 20  s to structures 
f8170 6f 66 20 74 68 69 73 20 74 79 70 65 20 61 72 65  of this type are
f8180 20 63 61 73 74 20 61 6e 64 20 72 65 74 75 72 6e   cast and return
f8190 65 64 20 61 73 20 0a 2a 2a 20 6f 70 61 71 75 65  ed as .** opaque
f81a0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2a   sqlite3_pcache*
f81b0 20 68 61 6e 64 6c 65 73 0a 2a 2f 0a 73 74 72 75   handles.*/.stru
f81c0 63 74 20 50 43 61 63 68 65 31 20 7b 0a 20 20 2f  ct PCache1 {.  /
f81d0 2a 20 43 61 63 68 65 20 63 6f 6e 66 69 67 75 72  * Cache configur
f81e0 61 74 69 6f 6e 20 70 61 72 61 6d 65 74 65 72 73  ation parameters
f81f0 2e 20 50 61 67 65 20 73 69 7a 65 20 28 73 7a 50  . Page size (szP
f8200 61 67 65 29 20 61 6e 64 20 74 68 65 20 70 75 72  age) and the pur
f8210 67 65 61 62 6c 65 0a 20 20 2a 2a 20 66 6c 61 67  geable.  ** flag
f8220 20 28 62 50 75 72 67 65 61 62 6c 65 29 20 61 72   (bPurgeable) ar
f8230 65 20 73 65 74 20 77 68 65 6e 20 74 68 65 20 63  e set when the c
f8240 61 63 68 65 20 69 73 20 63 72 65 61 74 65 64 2e  ache is created.
f8250 20 6e 4d 61 78 20 6d 61 79 20 62 65 20 0a 20 20   nMax may be .  
f8260 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 74 20 61  ** modified at a
f8270 6e 79 20 74 69 6d 65 20 62 79 20 61 20 63 61 6c  ny time by a cal
f8280 6c 20 74 6f 20 74 68 65 20 70 63 61 63 68 65 31  l to the pcache1
f8290 43 61 63 68 65 53 69 7a 65 28 29 20 6d 65 74 68  CacheSize() meth
f82a0 6f 64 2e 0a 20 20 2a 2a 20 54 68 65 20 67 6c 6f  od..  ** The glo
f82b0 62 61 6c 20 6d 75 74 65 78 20 6d 75 73 74 20 62  bal mutex must b
f82c0 65 20 68 65 6c 64 20 77 68 65 6e 20 61 63 63 65  e held when acce
f82d0 73 73 69 6e 67 20 6e 4d 61 78 2e 0a 20 20 2a 2f  ssing nMax..  */
f82e0 0a 20 20 69 6e 74 20 73 7a 50 61 67 65 3b 20 20  .  int szPage;  
f82f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8300 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
f8310 66 20 61 6c 6c 6f 63 61 74 65 64 20 70 61 67 65  f allocated page
f8320 73 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20  s in bytes */.  
f8330 69 6e 74 20 62 50 75 72 67 65 61 62 6c 65 3b 20  int bPurgeable; 
f8340 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8350 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
f8360 61 63 68 65 20 69 73 20 70 75 72 67 65 61 62 6c  ache is purgeabl
f8370 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  e */.  unsigned 
f8380 69 6e 74 20 6e 4d 69 6e 3b 20 20 20 20 20 20 20  int nMin;       
f8390 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 69             /* Mi
f83a0 6e 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  nimum number of 
f83b0 70 61 67 65 73 20 72 65 73 65 72 76 65 64 20 2a  pages reserved *
f83c0 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74  /.  unsigned int
f83d0 20 6e 4d 61 78 3b 20 20 20 20 20 20 20 20 20 20   nMax;          
f83e0 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69          /* Confi
f83f0 67 75 72 65 64 20 22 63 61 63 68 65 5f 73 69 7a  gured "cache_siz
f8400 65 22 20 76 61 6c 75 65 20 2a 2f 0a 0a 20 20 2f  e" value */..  /
f8410 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f 66 20  * Hash table of 
f8420 61 6c 6c 20 70 61 67 65 73 2e 20 54 68 65 20 66  all pages. The f
f8430 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62 6c  ollowing variabl
f8440 65 73 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 61  es may only be a
f8450 63 63 65 73 73 65 64 0a 20 20 2a 2a 20 77 68 65  ccessed.  ** whe
f8460 6e 20 74 68 65 20 61 63 63 65 73 73 6f 72 20 69  n the accessor i
f8470 73 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 67 6c  s holding the gl
f8480 6f 62 61 6c 20 6d 75 74 65 78 20 28 73 65 65 20  obal mutex (see 
f8490 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75 74 65  pcache1EnterMute
f84a0 78 28 29 20 0a 20 20 2a 2a 20 61 6e 64 20 70 63  x() .  ** and pc
f84b0 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28  ache1LeaveMutex(
f84c0 29 29 2e 0a 20 20 2a 2f 0a 20 20 75 6e 73 69 67  ))..  */.  unsig
f84d0 6e 65 64 20 69 6e 74 20 6e 52 65 63 79 63 6c 61  ned int nRecycla
f84e0 62 6c 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ble;           /
f84f0 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
f8500 73 20 69 6e 20 74 68 65 20 4c 52 55 20 6c 69 73  s in the LRU lis
f8510 74 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  t */.  unsigned 
f8520 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20 20  int nPage;      
f8530 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f             /* To
f8540 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61  tal number of pa
f8550 67 65 73 20 69 6e 20 61 70 48 61 73 68 20 2a 2f  ges in apHash */
f8560 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
f8570 6e 48 61 73 68 3b 20 20 20 20 20 20 20 20 20 20  nHash;          
f8580 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
f8590 20 6f 66 20 73 6c 6f 74 73 20 69 6e 20 61 70 48   of slots in apH
f85a0 61 73 68 5b 5d 20 2a 2f 0a 20 20 50 67 48 64 72  ash[] */.  PgHdr
f85b0 31 20 2a 2a 61 70 48 61 73 68 3b 20 20 20 20 20  1 **apHash;     
f85c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f85d0 2a 20 48 61 73 68 20 74 61 62 6c 65 20 66 6f 72  * Hash table for
f85e0 20 66 61 73 74 20 6c 6f 6f 6b 75 70 20 62 79 20   fast lookup by 
f85f0 6b 65 79 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  key */.};../*.**
f8600 20 45 61 63 68 20 63 61 63 68 65 20 65 6e 74 72   Each cache entr
f8610 79 20 69 73 20 72 65 70 72 65 73 65 6e 74 65 64  y is represented
f8620 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65 20   by an instance 
f8630 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  of the following
f8640 20 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 2e 20   .** structure. 
f8650 41 20 62 75 66 66 65 72 20 6f 66 20 50 67 48 64  A buffer of PgHd
f8660 72 31 2e 70 43 61 63 68 65 2d 3e 73 7a 50 61 67  r1.pCache->szPag
f8670 65 20 62 79 74 65 73 20 69 73 20 61 6c 6c 6f 63  e bytes is alloc
f8680 61 74 65 64 20 0a 2a 2a 20 64 69 72 65 63 74 6c  ated .** directl
f8690 79 20 61 66 74 65 72 20 74 68 65 20 73 74 72 75  y after the stru
f86a0 63 74 75 72 65 20 69 6e 20 6d 65 6d 6f 72 79 20  cture in memory 
f86b0 28 73 65 65 20 74 68 65 20 50 47 48 44 52 31 5f  (see the PGHDR1_
f86c0 54 4f 5f 50 41 47 45 28 29 20 0a 2a 2a 20 6d 61  TO_PAGE() .** ma
f86d0 63 72 6f 20 62 65 6c 6f 77 29 2e 0a 2a 2f 0a 73  cro below)..*/.s
f86e0 74 72 75 63 74 20 50 67 48 64 72 31 20 7b 0a 20  truct PgHdr1 {. 
f86f0 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b   unsigned int iK
f8700 65 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ey;             
f8710 2f 2a 20 4b 65 79 20 76 61 6c 75 65 20 28 70 61  /* Key value (pa
f8720 67 65 20 6e 75 6d 62 65 72 29 20 2a 2f 0a 20 20  ge number) */.  
f8730 50 67 48 64 72 31 20 2a 70 4e 65 78 74 3b 20 20  PgHdr1 *pNext;  
f8740 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f8750 2a 20 4e 65 78 74 20 69 6e 20 68 61 73 68 20 74  * Next in hash t
f8760 61 62 6c 65 20 63 68 61 69 6e 20 2a 2f 0a 20 20  able chain */.  
f8770 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 3b  PCache1 *pCache;
f8780 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
f8790 2a 20 43 61 63 68 65 20 74 68 61 74 20 63 75 72  * Cache that cur
f87a0 72 65 6e 74 6c 79 20 6f 77 6e 73 20 74 68 69 73  rently owns this
f87b0 20 70 61 67 65 20 2a 2f 0a 20 20 50 67 48 64 72   page */.  PgHdr
f87c0 31 20 2a 70 4c 72 75 4e 65 78 74 3b 20 20 20 20  1 *pLruNext;    
f87d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78            /* Nex
f87e0 74 20 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66  t in LRU list of
f87f0 20 75 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20   unpinned pages 
f8800 2a 2f 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72  */.  PgHdr1 *pLr
f8810 75 50 72 65 76 3b 20 20 20 20 20 20 20 20 20 20  uPrev;          
f8820 20 20 20 20 2f 2a 20 50 72 65 76 69 6f 75 73 20      /* Previous 
f8830 69 6e 20 4c 52 55 20 6c 69 73 74 20 6f 66 20 75  in LRU list of u
f8840 6e 70 69 6e 6e 65 64 20 70 61 67 65 73 20 2a 2f  npinned pages */
f8850 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  .};../*.** Free 
f8860 73 6c 6f 74 73 20 69 6e 20 74 68 65 20 61 6c 6c  slots in the all
f8870 6f 63 61 74 6f 72 20 75 73 65 64 20 74 6f 20 64  ocator used to d
f8880 69 76 69 64 65 20 75 70 20 74 68 65 20 62 75 66  ivide up the buf
f8890 66 65 72 20 70 72 6f 76 69 64 65 64 20 75 73 69  fer provided usi
f88a0 6e 67 0a 2a 2a 20 74 68 65 20 53 51 4c 49 54 45  ng.** the SQLITE
f88b0 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
f88c0 45 20 6d 65 63 68 61 6e 69 73 6d 2e 0a 2a 2f 0a  E mechanism..*/.
f88d0 73 74 72 75 63 74 20 50 67 46 72 65 65 73 6c 6f  struct PgFreeslo
f88e0 74 20 7b 0a 20 20 50 67 46 72 65 65 73 6c 6f 74  t {.  PgFreeslot
f88f0 20 2a 70 4e 65 78 74 3b 20 20 2f 2a 20 4e 65 78   *pNext;  /* Nex
f8900 74 20 66 72 65 65 20 73 6c 6f 74 20 2a 2f 0a 7d  t free slot */.}
f8910 3b 0a 0a 2f 2a 0a 2a 2a 20 47 6c 6f 62 61 6c 20  ;../*.** Global 
f8920 64 61 74 61 20 75 73 65 64 20 62 79 20 74 68 69  data used by thi
f8930 73 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74  s cache..*/.stat
f8940 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73 74  ic SQLITE_WSD st
f8950 72 75 63 74 20 50 43 61 63 68 65 47 6c 6f 62 61  ruct PCacheGloba
f8960 6c 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  l {.  sqlite3_mu
f8970 74 65 78 20 2a 6d 75 74 65 78 3b 20 20 20 20 20  tex *mutex;     
f8980 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 74 61            /* sta
f8990 74 69 63 20 6d 75 74 65 78 20 4d 55 54 45 58 5f  tic mutex MUTEX_
f89a0 53 54 41 54 49 43 5f 4c 52 55 20 2a 2f 0a 0a 20  STATIC_LRU */.. 
f89b0 20 69 6e 74 20 6e 4d 61 78 50 61 67 65 3b 20 20   int nMaxPage;  
f89c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f89d0 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20 6e       /* Sum of n
f89e0 4d 61 78 50 61 67 65 20 66 6f 72 20 70 75 72 67  MaxPage for purg
f89f0 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a  eable caches */.
f8a00 20 20 69 6e 74 20 6e 4d 69 6e 50 61 67 65 3b 20    int nMinPage; 
f8a10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
f8a20 20 20 20 20 20 20 2f 2a 20 53 75 6d 20 6f 66 20        /* Sum of 
f8a30 6e 4d 69 6e 50 61 67 65 20 66 6f 72 20 70 75 72  nMinPage for pur
f8a40 67 65 61 62 6c 65 20 63 61 63 68 65 73 20 2a 2f  geable caches */
f8a50 0a 20 20 69 6e 74 20 6e 43 75 72 72 65 6e 74 50  .  int nCurrentP
f8a60 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
f8a70 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
f8a80 20 6f 66 20 70 75 72 67 65 61 62 6c 65 20 70 61   of purgeable pa
f8a90 67 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 2a 2f  ges allocated */
f8aa0 0a 20 20 50 67 48 64 72 31 20 2a 70 4c 72 75 48  .  PgHdr1 *pLruH
f8ab0 65 61 64 2c 20 2a 70 4c 72 75 54 61 69 6c 3b 20  ead, *pLruTail; 
f8ac0 20 20 20 20 20 20 20 2f 2a 20 4c 52 55 20 6c 69         /* LRU li
f8ad0 73 74 20 6f 66 20 75 6e 70 69 6e 6e 65 64 20 70  st of unpinned p
f8ae0 61 67 65 73 20 2a 2f 0a 0a 20 20 2f 2a 20 56 61  ages */..  /* Va
f8af0 72 69 61 62 6c 65 73 20 72 65 6c 61 74 65 64 20  riables related 
f8b00 74 6f 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47  to SQLITE_CONFIG
f8b10 5f 50 41 47 45 43 41 43 48 45 20 73 65 74 74 69  _PAGECACHE setti
f8b20 6e 67 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 7a  ngs. */.  int sz
f8b30 53 6c 6f 74 3b 20 20 20 20 20 20 20 20 20 20 20  Slot;           
f8b40 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f8b50 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20 66 72   Size of each fr
f8b60 65 65 20 73 6c 6f 74 20 2a 2f 0a 20 20 76 6f 69  ee slot */.  voi
f8b70 64 20 2a 70 53 74 61 72 74 2c 20 2a 70 45 6e 64  d *pStart, *pEnd
f8b80 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
f8b90 20 2f 2a 20 42 6f 75 6e 64 73 20 6f 66 20 70 61   /* Bounds of pa
f8ba0 67 65 63 61 63 68 65 20 6d 61 6c 6c 6f 63 20 72  gecache malloc r
f8bb0 61 6e 67 65 20 2a 2f 0a 20 20 50 67 46 72 65 65  ange */.  PgFree
f8bc0 73 6c 6f 74 20 2a 70 46 72 65 65 3b 20 20 20 20  slot *pFree;    
f8bd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
f8be0 20 46 72 65 65 20 70 61 67 65 20 62 6c 6f 63 6b   Free page block
f8bf0 73 20 2a 2f 0a 7d 20 70 63 61 63 68 65 31 5f 67  s */.} pcache1_g
f8c00 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20 63 6f 64  ;../*.** All cod
f8c10 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 73  e in this file s
f8c20 68 6f 75 6c 64 20 61 63 63 65 73 73 20 74 68 65  hould access the
f8c30 20 67 6c 6f 62 61 6c 20 73 74 72 75 63 74 75 72   global structur
f8c40 65 20 61 62 6f 76 65 20 76 69 61 20 74 68 65 0a  e above via the.
f8c50 2a 2a 20 61 6c 69 61 73 20 22 70 63 61 63 68 65  ** alias "pcache
f8c60 31 22 2e 20 54 68 69 73 20 65 6e 73 75 72 65 73  1". This ensures
f8c70 20 74 68 61 74 20 74 68 65 20 57 53 44 20 65 6d   that the WSD em
f8c80 75 6c 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20  ulation is used 
f8c90 77 68 65 6e 0a 2a 2a 20 63 6f 6d 70 69 6c 69 6e  when.** compilin
f8ca0 67 20 66 6f 72 20 73 79 73 74 65 6d 73 20 74 68  g for systems th
f8cb0 61 74 20 64 6f 20 6e 6f 74 20 73 75 70 70 6f 72  at do not suppor
f8cc0 74 20 72 65 61 6c 20 57 53 44 2e 0a 2a 2f 0a 23  t real WSD..*/.#
f8cd0 64 65 66 69 6e 65 20 70 63 61 63 68 65 31 20 28  define pcache1 (
f8ce0 47 4c 4f 42 41 4c 28 73 74 72 75 63 74 20 50 43  GLOBAL(struct PC
f8cf0 61 63 68 65 47 6c 6f 62 61 6c 2c 20 70 63 61 63  acheGlobal, pcac
f8d00 68 65 31 5f 67 29 29 0a 0a 2f 2a 0a 2a 2a 20 57  he1_g))../*.** W
f8d10 68 65 6e 20 61 20 50 67 48 64 72 31 20 73 74 72  hen a PgHdr1 str
f8d20 75 63 74 75 72 65 20 69 73 20 61 6c 6c 6f 63 61  ucture is alloca
f8d30 74 65 64 2c 20 74 68 65 20 61 73 73 6f 63 69 61  ted, the associa
f8d40 74 65 64 20 50 43 61 63 68 65 31 2e 73 7a 50 61  ted PCache1.szPa
f8d50 67 65 0a 2a 2a 20 62 79 74 65 73 20 6f 66 20 64  ge.** bytes of d
f8d60 61 74 61 20 61 72 65 20 6c 6f 63 61 74 65 64 20  ata are located 
f8d70 64 69 72 65 63 74 6c 79 20 61 66 74 65 72 20 69  directly after i
f8d80 74 20 69 6e 20 6d 65 6d 6f 72 79 20 28 69 2e 65  t in memory (i.e
f8d90 2e 20 74 68 65 20 74 6f 74 61 6c 0a 2a 2a 20 73  . the total.** s
f8da0 69 7a 65 20 6f 66 20 74 68 65 20 61 6c 6c 6f 63  ize of the alloc
f8db0 61 74 69 6f 6e 20 69 73 20 73 69 7a 65 6f 66 28  ation is sizeof(
f8dc0 50 67 48 64 72 31 29 2b 50 43 61 63 68 65 31 2e  PgHdr1)+PCache1.
f8dd0 73 7a 50 61 67 65 20 62 79 74 65 29 2e 20 54 68  szPage byte). Th
f8de0 65 0a 2a 2a 20 50 47 48 44 52 31 5f 54 4f 5f 50  e.** PGHDR1_TO_P
f8df0 41 47 45 28 29 20 6d 61 63 72 6f 20 74 61 6b 65  AGE() macro take
f8e00 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
f8e10 20 50 67 48 64 72 31 20 73 74 72 75 63 74 75 72   PgHdr1 structur
f8e20 65 20 61 73 0a 2a 2a 20 61 6e 20 61 72 67 75 6d  e as.** an argum
f8e30 65 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 73 20  ent and returns 
f8e40 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
f8e50 20 61 73 73 6f 63 69 61 74 65 64 20 62 6c 6f 63   associated bloc
f8e60 6b 20 6f 66 20 73 7a 50 61 67 65 0a 2a 2a 20 62  k of szPage.** b
f8e70 79 74 65 73 2e 20 54 68 65 20 50 41 47 45 5f 54  ytes. The PAGE_T
f8e80 4f 5f 50 47 48 44 52 31 28 29 20 6d 61 63 72 6f  O_PGHDR1() macro
f8e90 20 64 6f 65 73 20 74 68 65 20 6f 70 70 6f 73 69   does the opposi
f8ea0 74 65 3a 20 69 74 73 20 61 72 67 75 6d 65 6e 74  te: its argument
f8eb0 20 69 73 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72   is.** a pointer
f8ec0 20 74 6f 20 61 20 62 6c 6f 63 6b 20 6f 66 20 73   to a block of s
f8ed0 7a 50 61 67 65 20 62 79 74 65 73 20 6f 66 20 64  zPage bytes of d
f8ee0 61 74 61 20 61 6e 64 20 74 68 65 20 72 65 74 75  ata and the retu
f8ef0 72 6e 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61  rn value is.** a
f8f00 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
f8f10 61 73 73 6f 63 69 61 74 65 64 20 50 67 48 64 72  associated PgHdr
f8f20 31 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a  1 structure..**.
f8f30 2a 2a 20 20 20 61 73 73 65 72 74 28 20 50 47 48  **   assert( PGH
f8f40 44 52 31 5f 54 4f 5f 50 41 47 45 28 50 41 47 45  DR1_TO_PAGE(PAGE
f8f50 5f 54 4f 5f 50 47 48 44 52 31 28 58 29 29 3d 3d  _TO_PGHDR1(X))==
f8f60 58 20 29 3b 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  X );.*/.#define 
f8f70 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70  PGHDR1_TO_PAGE(p
f8f80 29 20 28 76 6f 69 64 20 2a 29 28 26 28 28 75 6e  ) (void *)(&((un
f8f90 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 29  signed char *)p)
f8fa0 5b 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 5d  [sizeof(PgHdr1)]
f8fb0 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45 5f 54  ).#define PAGE_T
f8fc0 4f 5f 50 47 48 44 52 31 28 70 29 20 28 50 67 48  O_PGHDR1(p) (PgH
f8fd0 64 72 31 20 2a 29 28 26 28 28 75 6e 73 69 67 6e  dr1 *)(&((unsign
f8fe0 65 64 20 63 68 61 72 20 2a 29 70 29 5b 2d 31 2a  ed char *)p)[-1*
f8ff0 73 69 7a 65 6f 66 28 50 67 48 64 72 31 29 5d 29  sizeof(PgHdr1)])
f9000 0a 0a 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 74  ../*.** Macros t
f9010 6f 20 65 6e 74 65 72 20 61 6e 64 20 6c 65 61 76  o enter and leav
f9020 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 4c 52 55  e the global LRU
f9030 20 6d 75 74 65 78 2e 0a 2a 2f 0a 23 64 65 66 69   mutex..*/.#defi
f9040 6e 65 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d  ne pcache1EnterM
f9050 75 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d  utex() sqlite3_m
f9060 75 74 65 78 5f 65 6e 74 65 72 28 70 63 61 63 68  utex_enter(pcach
f9070 65 31 2e 6d 75 74 65 78 29 0a 23 64 65 66 69 6e  e1.mutex).#defin
f9080 65 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d 75  e pcache1LeaveMu
f9090 74 65 78 28 29 20 73 71 6c 69 74 65 33 5f 6d 75  tex() sqlite3_mu
f90a0 74 65 78 5f 6c 65 61 76 65 28 70 63 61 63 68 65  tex_leave(pcache
f90b0 31 2e 6d 75 74 65 78 29 0a 0a 2f 2a 2a 2a 2a 2a  1.mutex)../*****
f90c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f90d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f90e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f90f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9100 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
f9110 2a 2a 2a 2a 20 50 61 67 65 20 41 6c 6c 6f 63 61  **** Page Alloca
f9120 74 69 6f 6e 2f 53 51 4c 49 54 45 5f 43 4f 4e 46  tion/SQLITE_CONF
f9130 49 47 5f 50 43 41 43 48 45 20 52 65 6c 61 74 65  IG_PCACHE Relate
f9140 64 20 46 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a  d Functions ****
f9150 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a  **********/../*.
f9160 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
f9170 20 69 73 20 63 61 6c 6c 65 64 20 64 75 72 69 6e   is called durin
f9180 67 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e  g initialization
f9190 20 69 66 20 61 20 73 74 61 74 69 63 20 62 75 66   if a static buf
f91a0 66 65 72 20 69 73 20 0a 2a 2a 20 73 75 70 70 6c  fer is .** suppl
f91b0 69 65 64 20 74 6f 20 75 73 65 20 66 6f 72 20 74  ied to use for t
f91c0 68 65 20 70 61 67 65 2d 63 61 63 68 65 20 62 79  he page-cache by
f91d0 20 70 61 73 73 69 6e 67 20 74 68 65 20 53 51 4c   passing the SQL
f91e0 49 54 45 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43  ITE_CONFIG_PAGEC
f91f0 41 43 48 45 0a 2a 2a 20 76 65 72 62 20 74 6f 20  ACHE.** verb to 
f9200 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28 29  sqlite3_config()
f9210 2e 20 50 61 72 61 6d 65 74 65 72 20 70 42 75 66  . Parameter pBuf
f9220 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20 61 6c   points to an al
f9230 6c 6f 63 61 74 69 6f 6e 20 6c 61 72 67 65 0a 2a  location large.*
f9240 2a 20 65 6e 6f 75 67 68 20 74 6f 20 63 6f 6e 74  * enough to cont
f9250 61 69 6e 20 27 6e 27 20 62 75 66 66 65 72 73 20  ain 'n' buffers 
f9260 6f 66 20 27 73 7a 27 20 62 79 74 65 73 20 65 61  of 'sz' bytes ea
f9270 63 68 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ch..*/.SQLITE_PR
f9280 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
f9290 65 33 50 43 61 63 68 65 42 75 66 66 65 72 53 65  e3PCacheBufferSe
f92a0 74 75 70 28 76 6f 69 64 20 2a 70 42 75 66 2c 20  tup(void *pBuf, 
f92b0 69 6e 74 20 73 7a 2c 20 69 6e 74 20 6e 29 7b 0a  int sz, int n){.
f92c0 20 20 50 67 46 72 65 65 73 6c 6f 74 20 2a 70 3b    PgFreeslot *p;
f92d0 0a 20 20 73 7a 20 26 3d 20 7e 37 3b 0a 20 20 70  .  sz &= ~7;.  p
f92e0 63 61 63 68 65 31 2e 73 7a 53 6c 6f 74 20 3d 20  cache1.szSlot = 
f92f0 73 7a 3b 0a 20 20 70 63 61 63 68 65 31 2e 70 53  sz;.  pcache1.pS
f9300 74 61 72 74 20 3d 20 70 42 75 66 3b 0a 20 20 70  tart = pBuf;.  p
f9310 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 30  cache1.pFree = 0
f9320 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d 20 29  ;.  while( n-- )
f9330 7b 0a 20 20 20 20 70 20 3d 20 28 50 67 46 72 65  {.    p = (PgFre
f9340 65 73 6c 6f 74 2a 29 70 42 75 66 3b 0a 20 20 20  eslot*)pBuf;.   
f9350 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 63 61 63   p->pNext = pcac
f9360 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70  he1.pFree;.    p
f9370 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70  cache1.pFree = p
f9380 3b 0a 20 20 20 20 70 42 75 66 20 3d 20 28 76 6f  ;.    pBuf = (vo
f9390 69 64 2a 29 26 28 28 63 68 61 72 2a 29 70 42 75  id*)&((char*)pBu
f93a0 66 29 5b 73 7a 5d 3b 0a 20 20 7d 0a 20 20 70 63  f)[sz];.  }.  pc
f93b0 61 63 68 65 31 2e 70 45 6e 64 20 3d 20 70 42 75  ache1.pEnd = pBu
f93c0 66 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c  f;.}../*.** Mall
f93d0 6f 63 20 66 75 6e 63 74 69 6f 6e 20 75 73 65 64  oc function used
f93e0 20 77 69 74 68 69 6e 20 74 68 69 73 20 66 69 6c   within this fil
f93f0 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 73 70  e to allocate sp
f9400 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62 75 66  ace from the buf
f9410 66 65 72 0a 2a 2a 20 63 6f 6e 66 69 67 75 72 65  fer.** configure
f9420 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  d using sqlite3_
f9430 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f 43 4f  config(SQLITE_CO
f9440 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45 29 20  NFIG_PAGECACHE) 
f9450 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20 0a 2a  option. If no .*
f9460 2a 20 73 75 63 68 20 62 75 66 66 65 72 20 65 78  * such buffer ex
f9470 69 73 74 73 20 6f 72 20 74 68 65 72 65 20 69 73  ists or there is
f9480 20 6e 6f 20 73 70 61 63 65 20 6c 65 66 74 20 69   no space left i
f9490 6e 20 69 74 2c 20 74 68 69 73 20 66 75 6e 63 74  n it, this funct
f94a0 69 6f 6e 20 66 61 6c 6c 73 20 0a 2a 2a 20 62 61  ion falls .** ba
f94b0 63 6b 20 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c  ck to sqlite3Mal
f94c0 6c 6f 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  loc()..*/.static
f94d0 20 76 6f 69 64 20 2a 70 63 61 63 68 65 31 41 6c   void *pcache1Al
f94e0 6c 6f 63 28 69 6e 74 20 6e 42 79 74 65 29 7b 0a  loc(int nByte){.
f94f0 20 20 76 6f 69 64 20 2a 70 3b 0a 20 20 61 73 73    void *p;.  ass
f9500 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
f9510 65 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e  ex_held(pcache1.
f9520 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20  mutex) );.  if( 
f9530 6e 42 79 74 65 3c 3d 70 63 61 63 68 65 31 2e 73  nByte<=pcache1.s
f9540 7a 53 6c 6f 74 20 26 26 20 70 63 61 63 68 65 31  zSlot && pcache1
f9550 2e 70 46 72 65 65 20 29 7b 0a 20 20 20 20 70 20  .pFree ){.    p 
f9560 3d 20 28 50 67 48 64 72 31 20 2a 29 70 63 61 63  = (PgHdr1 *)pcac
f9570 68 65 31 2e 70 46 72 65 65 3b 0a 20 20 20 20 70  he1.pFree;.    p
f9580 63 61 63 68 65 31 2e 70 46 72 65 65 20 3d 20 70  cache1.pFree = p
f9590 63 61 63 68 65 31 2e 70 46 72 65 65 2d 3e 70 4e  cache1.pFree->pN
f95a0 65 78 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  ext;.    sqlite3
f95b0 53 74 61 74 75 73 53 65 74 28 53 51 4c 49 54 45  StatusSet(SQLITE
f95c0 5f 53 54 41 54 55 53 5f 50 41 47 45 43 41 43 48  _STATUS_PAGECACH
f95d0 45 5f 53 49 5a 45 2c 20 6e 42 79 74 65 29 3b 0a  E_SIZE, nByte);.
f95e0 20 20 20 20 73 71 6c 69 74 65 33 53 74 61 74 75      sqlite3Statu
f95f0 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54 41 54  sAdd(SQLITE_STAT
f9600 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55 53 45  US_PAGECACHE_USE
f9610 44 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  D, 1);.  }else{.
f9620 0a 20 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  .    /* Allocate
f9630 20 61 20 6e 65 77 20 62 75 66 66 65 72 20 75 73   a new buffer us
f9640 69 6e 67 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  ing sqlite3Mallo
f9650 63 2e 20 42 65 66 6f 72 65 20 64 6f 69 6e 67 20  c. Before doing 
f9660 73 6f 2c 20 65 78 69 74 20 74 68 65 0a 20 20 20  so, exit the.   
f9670 20 2a 2a 20 67 6c 6f 62 61 6c 20 70 63 61 63 68   ** global pcach
f9680 65 20 6d 75 74 65 78 20 61 6e 64 20 75 6e 6c 6f  e mutex and unlo
f9690 63 6b 20 74 68 65 20 70 61 67 65 72 2d 63 61 63  ck the pager-cac
f96a0 68 65 20 6f 62 6a 65 63 74 20 70 43 61 63 68 65  he object pCache
f96b0 2e 20 54 68 69 73 20 69 73 20 0a 20 20 20 20 2a  . This is .    *
f96c0 2a 20 73 6f 20 74 68 61 74 20 69 66 20 74 68 65  * so that if the
f96d0 20 61 74 74 65 6d 70 74 20 74 6f 20 61 6c 6c 6f   attempt to allo
f96e0 63 61 74 65 20 61 20 6e 65 77 20 62 75 66 66 65  cate a new buffe
f96f0 72 20 63 61 75 73 65 73 20 74 68 65 20 74 68 65  r causes the the
f9700 20 0a 20 20 20 20 2a 2a 20 63 6f 6e 66 69 67 75   .    ** configu
f9710 72 65 64 20 73 6f 66 74 2d 68 65 61 70 2d 6c 69  red soft-heap-li
f9720 6d 69 74 20 74 6f 20 62 65 20 62 72 65 61 63 68  mit to be breach
f9730 65 64 2c 20 69 74 20 77 69 6c 6c 20 62 65 20 70  ed, it will be p
f9740 6f 73 73 69 62 6c 65 20 74 6f 0a 20 20 20 20 2a  ossible to.    *
f9750 2a 20 72 65 63 6c 61 69 6d 20 6d 65 6d 6f 72 79  * reclaim memory
f9760 20 66 72 6f 6d 20 74 68 69 73 20 70 61 67 65 72   from this pager
f9770 2d 63 61 63 68 65 2e 0a 20 20 20 20 2a 2f 0a 20  -cache..    */. 
f9780 20 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d     pcache1LeaveM
f9790 75 74 65 78 28 29 3b 0a 20 20 20 20 70 20 3d 20  utex();.    p = 
f97a0 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 42  sqlite3Malloc(nB
f97b0 79 74 65 29 3b 0a 20 20 20 20 70 63 61 63 68 65  yte);.    pcache
f97c0 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20  1EnterMutex();. 
f97d0 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20     if( p ){.    
f97e0 20 20 69 6e 74 20 73 7a 20 3d 20 73 71 6c 69 74    int sz = sqlit
f97f0 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b  e3MallocSize(p);
f9800 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74  .      sqlite3St
f9810 61 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53  atusAdd(SQLITE_S
f9820 54 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f  TATUS_PAGECACHE_
f9830 4f 56 45 52 46 4c 4f 57 2c 20 73 7a 29 3b 0a 20  OVERFLOW, sz);. 
f9840 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
f9850 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72  n p;.}../*.** Fr
f9860 65 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20  ee an allocated 
f9870 62 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20  buffer obtained 
f9880 66 72 6f 6d 20 70 63 61 63 68 65 31 41 6c 6c 6f  from pcache1Allo
f9890 63 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  c()..*/.static v
f98a0 6f 69 64 20 70 63 61 63 68 65 31 46 72 65 65 28  oid pcache1Free(
f98b0 76 6f 69 64 20 2a 70 29 7b 0a 20 20 61 73 73 65  void *p){.  asse
f98c0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
f98d0 78 5f 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d  x_held(pcache1.m
f98e0 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70  utex) );.  if( p
f98f0 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
f9900 69 66 28 20 70 3e 3d 70 63 61 63 68 65 31 2e 70  if( p>=pcache1.p
f9910 53 74 61 72 74 20 26 26 20 70 3c 70 63 61 63 68  Start && p<pcach
f9920 65 31 2e 70 45 6e 64 20 29 7b 0a 20 20 20 20 50  e1.pEnd ){.    P
f9930 67 46 72 65 65 73 6c 6f 74 20 2a 70 53 6c 6f 74  gFreeslot *pSlot
f9940 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
f9950 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
f9960 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 55  ATUS_PAGECACHE_U
f9970 53 45 44 2c 20 2d 31 29 3b 0a 20 20 20 20 70 53  SED, -1);.    pS
f9980 6c 6f 74 20 3d 20 28 50 67 46 72 65 65 73 6c 6f  lot = (PgFreeslo
f9990 74 2a 29 70 3b 0a 20 20 20 20 70 53 6c 6f 74 2d  t*)p;.    pSlot-
f99a0 3e 70 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31  >pNext = pcache1
f99b0 2e 70 46 72 65 65 3b 0a 20 20 20 20 70 63 61 63  .pFree;.    pcac
f99c0 68 65 31 2e 70 46 72 65 65 20 3d 20 70 53 6c 6f  he1.pFree = pSlo
f99d0 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  t;.  }else{.    
f99e0 69 6e 74 20 69 53 69 7a 65 20 3d 20 73 71 6c 69  int iSize = sqli
f99f0 74 65 33 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29  te3MallocSize(p)
f9a00 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74 61  ;.    sqlite3Sta
f9a10 74 75 73 41 64 64 28 53 51 4c 49 54 45 5f 53 54  tusAdd(SQLITE_ST
f9a20 41 54 55 53 5f 50 41 47 45 43 41 43 48 45 5f 4f  ATUS_PAGECACHE_O
f9a30 56 45 52 46 4c 4f 57 2c 20 2d 69 53 69 7a 65 29  VERFLOW, -iSize)
f9a40 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
f9a50 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ee(p);.  }.}../*
f9a60 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
f9a70 65 77 20 70 61 67 65 20 6f 62 6a 65 63 74 20 69  ew page object i
f9a80 6e 69 74 69 61 6c 6c 79 20 61 73 73 6f 63 69 61  nitially associa
f9a90 74 65 64 20 77 69 74 68 20 63 61 63 68 65 20 70  ted with cache p
f9aa0 43 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  Cache..*/.static
f9ab0 20 50 67 48 64 72 31 20 2a 70 63 61 63 68 65 31   PgHdr1 *pcache1
f9ac0 41 6c 6c 6f 63 50 61 67 65 28 50 43 61 63 68 65  AllocPage(PCache
f9ad0 31 20 2a 70 43 61 63 68 65 29 7b 0a 20 20 69 6e  1 *pCache){.  in
f9ae0 74 20 6e 42 79 74 65 20 3d 20 73 69 7a 65 6f 66  t nByte = sizeof
f9af0 28 50 67 48 64 72 31 29 20 2b 20 70 43 61 63 68  (PgHdr1) + pCach
f9b00 65 2d 3e 73 7a 50 61 67 65 3b 0a 20 20 50 67 48  e->szPage;.  PgH
f9b10 64 72 31 20 2a 70 20 3d 20 28 50 67 48 64 72 31  dr1 *p = (PgHdr1
f9b20 20 2a 29 70 63 61 63 68 65 31 41 6c 6c 6f 63 28   *)pcache1Alloc(
f9b30 6e 42 79 74 65 29 3b 0a 20 20 69 66 28 20 70 20  nByte);.  if( p 
f9b40 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 2c  ){.    memset(p,
f9b50 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20   0, nByte);.    
f9b60 69 66 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72  if( pCache->bPur
f9b70 67 65 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  geable ){.      
f9b80 70 63 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74  pcache1.nCurrent
f9b90 50 61 67 65 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20  Page++;.    }.  
f9ba0 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a  }.  return p;.}.
f9bb0 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 20 70 61  ./*.** Free a pa
f9bc0 67 65 20 6f 62 6a 65 63 74 20 61 6c 6c 6f 63 61  ge object alloca
f9bd0 74 65 64 20 62 79 20 70 63 61 63 68 65 31 41 6c  ted by pcache1Al
f9be0 6c 6f 63 50 61 67 65 28 29 2e 0a 2a 2f 0a 73 74  locPage()..*/.st
f9bf0 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
f9c00 31 46 72 65 65 50 61 67 65 28 50 67 48 64 72 31  1FreePage(PgHdr1
f9c10 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
f9c20 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 43 61 63  .    if( p->pCac
f9c30 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 29  he->bPurgeable )
f9c40 7b 0a 20 20 20 20 20 20 70 63 61 63 68 65 31 2e  {.      pcache1.
f9c50 6e 43 75 72 72 65 6e 74 50 61 67 65 2d 2d 3b 0a  nCurrentPage--;.
f9c60 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63 68 65      }.    pcache
f9c70 31 46 72 65 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a  1Free(p);.  }.}.
f9c80 0a 2f 2a 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 66 75  ./*.** Malloc fu
f9c90 6e 63 74 69 6f 6e 20 75 73 65 64 20 62 79 20 53  nction used by S
f9ca0 51 4c 69 74 65 20 74 6f 20 6f 62 74 61 69 6e 20  QLite to obtain 
f9cb0 73 70 61 63 65 20 66 72 6f 6d 20 74 68 65 20 62  space from the b
f9cc0 75 66 66 65 72 20 63 6f 6e 66 69 67 75 72 65 64  uffer configured
f9cd0 0a 2a 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65  .** using sqlite
f9ce0 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45 5f  3_config(SQLITE_
f9cf0 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48 45  CONFIG_PAGECACHE
f9d00 29 20 6f 70 74 69 6f 6e 2e 20 49 66 20 6e 6f 20  ) option. If no 
f9d10 73 75 63 68 20 62 75 66 66 65 72 0a 2a 2a 20 65  such buffer.** e
f9d20 78 69 73 74 73 2c 20 74 68 69 73 20 66 75 6e 63  xists, this func
f9d30 74 69 6f 6e 20 66 61 6c 6c 73 20 62 61 63 6b 20  tion falls back 
f9d40 74 6f 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63  to sqlite3Malloc
f9d50 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ()..*/.SQLITE_PR
f9d60 49 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69  IVATE void *sqli
f9d70 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63 28 69 6e  te3PageMalloc(in
f9d80 74 20 73 7a 29 7b 0a 20 20 76 6f 69 64 20 2a 70  t sz){.  void *p
f9d90 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
f9da0 4d 75 74 65 78 28 29 3b 0a 20 20 70 20 3d 20 70  Mutex();.  p = p
f9db0 63 61 63 68 65 31 41 6c 6c 6f 63 28 73 7a 29 3b  cache1Alloc(sz);
f9dc0 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
f9dd0 75 74 65 78 28 29 3b 0a 20 20 72 65 74 75 72 6e  utex();.  return
f9de0 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65   p;.}../*.** Fre
f9df0 65 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  e an allocated b
f9e00 75 66 66 65 72 20 6f 62 74 61 69 6e 65 64 20 66  uffer obtained f
f9e10 72 6f 6d 20 73 71 6c 69 74 65 33 50 61 67 65 4d  rom sqlite3PageM
f9e20 61 6c 6c 6f 63 28 29 2e 0a 2a 2f 0a 53 51 4c 49  alloc()..*/.SQLI
f9e30 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
f9e40 73 71 6c 69 74 65 33 50 61 67 65 46 72 65 65 28  sqlite3PageFree(
f9e50 76 6f 69 64 20 2a 70 29 7b 0a 20 20 70 63 61 63  void *p){.  pcac
f9e60 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
f9e70 0a 20 20 70 63 61 63 68 65 31 46 72 65 65 28 70  .  pcache1Free(p
f9e80 29 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76  );.  pcache1Leav
f9e90 65 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 2a  eMutex();.}../**
f9ea0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9eb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ed0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9ee0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
f9ef0 2a 2a 2a 2a 2a 2a 2a 20 47 65 6e 65 72 61 6c 20  ******* General 
f9f00 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 46  Implementation F
f9f10 75 6e 63 74 69 6f 6e 73 20 2a 2a 2a 2a 2a 2a 2a  unctions *******
f9f20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
f9f30 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
f9f40 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
f9f50 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
f9f60 65 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74  esize the hash t
f9f70 61 62 6c 65 20 75 73 65 64 20 62 79 20 74 68 65  able used by the
f9f80 20 63 61 63 68 65 20 70 61 73 73 65 64 0a 2a 2a   cache passed.**
f9f90 20 61 73 20 74 68 65 20 66 69 72 73 74 20 61 72   as the first ar
f9fa0 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  gument..**.** Th
f9fb0 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20 6d  e global mutex m
f9fc0 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65 6e  ust be held when
f9fd0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
f9fe0 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61  s called..*/.sta
f9ff0 74 69 63 20 69 6e 74 20 70 63 61 63 68 65 31 52  tic int pcache1R
fa000 65 73 69 7a 65 48 61 73 68 28 50 43 61 63 68 65  esizeHash(PCache
fa010 31 20 2a 70 29 7b 0a 20 20 50 67 48 64 72 31 20  1 *p){.  PgHdr1 
fa020 2a 2a 61 70 4e 65 77 3b 0a 20 20 75 6e 73 69 67  **apNew;.  unsig
fa030 6e 65 64 20 69 6e 74 20 6e 4e 65 77 3b 0a 20 20  ned int nNew;.  
fa040 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 3b 0a  unsigned int i;.
fa050 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
fa060 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63  e3_mutex_held(pc
fa070 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a  ache1.mutex) );.
fa080 0a 20 20 6e 4e 65 77 20 3d 20 70 2d 3e 6e 48 61  .  nNew = p->nHa
fa090 73 68 2a 32 3b 0a 20 20 69 66 28 20 6e 4e 65 77  sh*2;.  if( nNew
fa0a0 3c 32 35 36 20 29 7b 0a 20 20 20 20 6e 4e 65 77  <256 ){.    nNew
fa0b0 20 3d 20 32 35 36 3b 0a 20 20 7d 0a 0a 20 20 70   = 256;.  }..  p
fa0c0 63 61 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78  cache1LeaveMutex
fa0d0 28 29 3b 0a 20 20 61 70 4e 65 77 20 3d 20 28 50  ();.  apNew = (P
fa0e0 67 48 64 72 31 20 2a 2a 29 73 71 6c 69 74 65 33  gHdr1 **)sqlite3
fa0f0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  _malloc(sizeof(P
fa100 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77 29 3b 0a  gHdr1 *)*nNew);.
fa110 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d 75    pcache1EnterMu
fa120 74 65 78 28 29 3b 0a 20 20 69 66 28 20 61 70 4e  tex();.  if( apN
fa130 65 77 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74  ew ){.    memset
fa140 28 61 70 4e 65 77 2c 20 30 2c 20 73 69 7a 65 6f  (apNew, 0, sizeo
fa150 66 28 50 67 48 64 72 31 20 2a 29 2a 6e 4e 65 77  f(PgHdr1 *)*nNew
fa160 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
fa170 69 3c 70 2d 3e 6e 48 61 73 68 3b 20 69 2b 2b 29  i<p->nHash; i++)
fa180 7b 0a 20 20 20 20 20 20 50 67 48 64 72 31 20 2a  {.      PgHdr1 *
fa190 70 50 61 67 65 3b 0a 20 20 20 20 20 20 50 67 48  pPage;.      PgH
fa1a0 64 72 31 20 2a 70 4e 65 78 74 20 3d 20 70 2d 3e  dr1 *pNext = p->
fa1b0 61 70 48 61 73 68 5b 69 5d 3b 0a 20 20 20 20 20  apHash[i];.     
fa1c0 20 77 68 69 6c 65 28 20 28 70 50 61 67 65 20 3d   while( (pPage =
fa1d0 20 70 4e 65 78 74 29 20 29 7b 0a 20 20 20 20 20   pNext) ){.     
fa1e0 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
fa1f0 68 20 3d 20 70 50 61 67 65 2d 3e 69 4b 65 79 20  h = pPage->iKey 
fa200 25 20 6e 4e 65 77 3b 0a 20 20 20 20 20 20 20 20  % nNew;.        
fa210 70 4e 65 78 74 20 3d 20 70 50 61 67 65 2d 3e 70  pNext = pPage->p
fa220 4e 65 78 74 3b 0a 20 20 20 20 20 20 20 20 70 50  Next;.        pP
fa230 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20 61 70 4e  age->pNext = apN
fa240 65 77 5b 68 5d 3b 0a 20 20 20 20 20 20 20 20 61  ew[h];.        a
fa250 70 4e 65 77 5b 68 5d 20 3d 20 70 50 61 67 65 3b  pNew[h] = pPage;
fa260 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
fa270 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
fa280 70 2d 3e 61 70 48 61 73 68 29 3b 0a 20 20 20 20  p->apHash);.    
fa290 70 2d 3e 61 70 48 61 73 68 20 3d 20 61 70 4e 65  p->apHash = apNe
fa2a0 77 3b 0a 20 20 20 20 70 2d 3e 6e 48 61 73 68 20  w;.    p->nHash 
fa2b0 3d 20 6e 4e 65 77 3b 0a 20 20 7d 0a 0a 20 20 72  = nNew;.  }..  r
fa2c0 65 74 75 72 6e 20 28 70 2d 3e 61 70 48 61 73 68  eturn (p->apHash
fa2d0 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20 3a 20 53   ? SQLITE_OK : S
fa2e0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 7d 0a  QLITE_NOMEM);.}.
fa2f0 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
fa300 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e 74  tion is used int
fa310 65 72 6e 61 6c 6c 79 20 74 6f 20 72 65 6d 6f 76  ernally to remov
fa320 65 20 74 68 65 20 70 61 67 65 20 70 50 61 67 65  e the page pPage
fa330 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 67 6c   from the .** gl
fa340 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74 2c 20 69  obal LRU list, i
fa350 66 20 69 73 20 70 61 72 74 20 6f 66 20 69 74 2e  f is part of it.
fa360 20 49 66 20 70 50 61 67 65 20 69 73 20 6e 6f 74   If pPage is not
fa370 20 70 61 72 74 20 6f 66 20 74 68 65 20 67 6c 6f   part of the glo
fa380 62 61 6c 0a 2a 2a 20 4c 52 55 20 6c 69 73 74 2c  bal.** LRU list,
fa390 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
fa3a0 69 6f 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ion is a no-op..
fa3b0 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c  **.** The global
fa3c0 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
fa3d0 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
fa3e0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
fa3f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
fa400 20 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28   pcache1PinPage(
fa410 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a  PgHdr1 *pPage){.
fa420 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
fa430 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 63 61  3_mutex_held(pca
fa440 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b 0a 20  che1.mutex) );. 
fa450 20 69 66 28 20 70 50 61 67 65 20 26 26 20 28 70   if( pPage && (p
fa460 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20 7c  Page->pLruNext |
fa470 7c 20 70 50 61 67 65 3d 3d 70 63 61 63 68 65 31  | pPage==pcache1
fa480 2e 70 4c 72 75 54 61 69 6c 29 20 29 7b 0a 20 20  .pLruTail) ){.  
fa490 20 20 69 66 28 20 70 50 61 67 65 2d 3e 70 4c 72    if( pPage->pLr
fa4a0 75 50 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70  uPrev ){.      p
fa4b0 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 2d 3e  Page->pLruPrev->
fa4c0 70 4c 72 75 4e 65 78 74 20 3d 20 70 50 61 67 65  pLruNext = pPage
fa4d0 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20 20 20  ->pLruNext;.    
fa4e0 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  }.    if( pPage-
fa4f0 3e 70 4c 72 75 4e 65 78 74 20 29 7b 0a 20 20 20  >pLruNext ){.   
fa500 20 20 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65     pPage->pLruNe
fa510 78 74 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 70  xt->pLruPrev = p
fa520 50 61 67 65 2d 3e 70 4c 72 75 50 72 65 76 3b 0a  Page->pLruPrev;.
fa530 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 63      }.    if( pc
fa540 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3d 3d  ache1.pLruHead==
fa550 70 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 70  pPage ){.      p
fa560 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64 20  cache1.pLruHead 
fa570 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78  = pPage->pLruNex
fa580 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  t;.    }.    if(
fa590 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61 69   pcache1.pLruTai
fa5a0 6c 3d 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  l==pPage ){.    
fa5b0 20 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54 61    pcache1.pLruTa
fa5c0 69 6c 20 3d 20 70 50 61 67 65 2d 3e 70 4c 72 75  il = pPage->pLru
fa5d0 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Prev;.    }.    
fa5e0 70 50 61 67 65 2d 3e 70 4c 72 75 4e 65 78 74 20  pPage->pLruNext 
fa5f0 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  = 0;.    pPage->
fa600 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20  pLruPrev = 0;.  
fa610 20 20 70 50 61 67 65 2d 3e 70 43 61 63 68 65 2d    pPage->pCache-
fa620 3e 6e 52 65 63 79 63 6c 61 62 6c 65 2d 2d 3b 0a  >nRecyclable--;.
fa630 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65    }.}.../*.** Re
fa640 6d 6f 76 65 20 74 68 65 20 70 61 67 65 20 73 75  move the page su
fa650 70 70 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67  pplied as an arg
fa660 75 6d 65 6e 74 20 66 72 6f 6d 20 74 68 65 20 68  ument from the h
fa670 61 73 68 20 74 61 62 6c 65 20 0a 2a 2a 20 28 50  ash table .** (P
fa680 43 61 63 68 65 31 2e 61 70 48 61 73 68 20 73 74  Cache1.apHash st
fa690 72 75 63 74 75 72 65 29 20 74 68 61 74 20 69 74  ructure) that it
fa6a0 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 73 74   is currently st
fa6b0 6f 72 65 64 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ored in..**.** T
fa6c0 68 65 20 67 6c 6f 62 61 6c 20 6d 75 74 65 78 20  he global mutex 
fa6d0 6d 75 73 74 20 62 65 20 68 65 6c 64 20 77 68 65  must be held whe
fa6e0 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
fa6f0 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74  is called..*/.st
fa700 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68 65  atic void pcache
fa710 31 52 65 6d 6f 76 65 46 72 6f 6d 48 61 73 68 28  1RemoveFromHash(
fa720 50 67 48 64 72 31 20 2a 70 50 61 67 65 29 7b 0a  PgHdr1 *pPage){.
fa730 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68    unsigned int h
fa740 3b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  ;.  PCache1 *pCa
fa750 63 68 65 20 3d 20 70 50 61 67 65 2d 3e 70 43 61  che = pPage->pCa
fa760 63 68 65 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a  che;.  PgHdr1 **
fa770 70 70 3b 0a 0a 20 20 68 20 3d 20 70 50 61 67 65  pp;..  h = pPage
fa780 2d 3e 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d  ->iKey % pCache-
fa790 3e 6e 48 61 73 68 3b 0a 20 20 66 6f 72 28 70 70  >nHash;.  for(pp
fa7a0 3d 26 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68  =&pCache->apHash
fa7b0 5b 68 5d 3b 20 28 2a 70 70 29 21 3d 70 50 61 67  [h]; (*pp)!=pPag
fa7c0 65 3b 20 70 70 3d 26 28 2a 70 70 29 2d 3e 70 4e  e; pp=&(*pp)->pN
fa7d0 65 78 74 29 3b 0a 20 20 2a 70 70 20 3d 20 28 2a  ext);.  *pp = (*
fa7e0 70 70 29 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 70  pp)->pNext;..  p
fa7f0 43 61 63 68 65 2d 3e 6e 50 61 67 65 2d 2d 3b 0a  Cache->nPage--;.
fa800 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 72  }../*.** If ther
fa810 65 20 61 72 65 20 63 75 72 72 65 6e 74 6c 79 20  e are currently 
fa820 6d 6f 72 65 20 74 68 61 6e 20 70 63 61 63 68 65  more than pcache
fa830 2e 6e 4d 61 78 50 61 67 65 20 70 61 67 65 73 20  .nMaxPage pages 
fa840 61 6c 6c 6f 63 61 74 65 64 2c 20 74 72 79 0a 2a  allocated, try.*
fa850 2a 20 74 6f 20 72 65 63 79 63 6c 65 20 70 61 67  * to recycle pag
fa860 65 73 20 74 6f 20 72 65 64 75 63 65 20 74 68 65  es to reduce the
fa870 20 6e 75 6d 62 65 72 20 61 6c 6c 6f 63 61 74 65   number allocate
fa880 64 20 74 6f 20 70 63 61 63 68 65 2e 6e 4d 61 78  d to pcache.nMax
fa890 50 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Page..*/.static 
fa8a0 76 6f 69 64 20 70 63 61 63 68 65 31 45 6e 66 6f  void pcache1Enfo
fa8b0 72 63 65 4d 61 78 50 61 67 65 28 76 6f 69 64 29  rceMaxPage(void)
fa8c0 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
fa8d0 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
fa8e0 63 61 63 68 65 31 2e 6d 75 74 65 78 29 20 29 3b  cache1.mutex) );
fa8f0 0a 20 20 77 68 69 6c 65 28 20 70 63 61 63 68 65  .  while( pcache
fa900 31 2e 6e 43 75 72 72 65 6e 74 50 61 67 65 3e 70  1.nCurrentPage>p
fa910 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67 65 20  cache1.nMaxPage 
fa920 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54  && pcache1.pLruT
fa930 61 69 6c 20 29 7b 0a 20 20 20 20 50 67 48 64 72  ail ){.    PgHdr
fa940 31 20 2a 70 20 3d 20 70 63 61 63 68 65 31 2e 70  1 *p = pcache1.p
fa950 4c 72 75 54 61 69 6c 3b 0a 20 20 20 20 70 63 61  LruTail;.    pca
fa960 63 68 65 31 50 69 6e 50 61 67 65 28 70 29 3b 0a  che1PinPage(p);.
fa970 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76      pcache1Remov
fa980 65 46 72 6f 6d 48 61 73 68 28 70 29 3b 0a 20 20  eFromHash(p);.  
fa990 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61 67    pcache1FreePag
fa9a0 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  e(p);.  }.}../*.
fa9b0 2a 2a 20 44 69 73 63 61 72 64 20 61 6c 6c 20 70  ** Discard all p
fa9c0 61 67 65 73 20 66 72 6f 6d 20 63 61 63 68 65 20  ages from cache 
fa9d0 70 43 61 63 68 65 20 77 69 74 68 20 61 20 70 61  pCache with a pa
fa9e0 67 65 20 6e 75 6d 62 65 72 20 28 6b 65 79 20 76  ge number (key v
fa9f0 61 6c 75 65 29 20 0a 2a 2a 20 67 72 65 61 74 65  alue) .** greate
faa00 72 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  r than or equal 
faa10 74 6f 20 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70  to iLimit. Any p
faa20 69 6e 6e 65 64 20 70 61 67 65 73 20 74 68 61 74  inned pages that
faa30 20 6d 65 65 74 20 74 68 69 73 20 0a 2a 2a 20 63   meet this .** c
faa40 72 69 74 65 72 69 61 20 61 72 65 20 75 6e 70 69  riteria are unpi
faa50 6e 6e 65 64 20 62 65 66 6f 72 65 20 74 68 65 79  nned before they
faa60 20 61 72 65 20 64 69 73 63 61 72 64 65 64 2e 0a   are discarded..
faa70 2a 2a 0a 2a 2a 20 54 68 65 20 67 6c 6f 62 61 6c  **.** The global
faa80 20 6d 75 74 65 78 20 6d 75 73 74 20 62 65 20 68   mutex must be h
faa90 65 6c 64 20 77 68 65 6e 20 74 68 69 73 20 66 75  eld when this fu
faaa0 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
faab0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
faac0 20 70 63 61 63 68 65 31 54 72 75 6e 63 61 74 65   pcache1Truncate
faad0 55 6e 73 61 66 65 28 0a 20 20 50 43 61 63 68 65  Unsafe(.  PCache
faae0 31 20 2a 70 43 61 63 68 65 2c 20 0a 20 20 75 6e  1 *pCache, .  un
faaf0 73 69 67 6e 65 64 20 69 6e 74 20 69 4c 69 6d 69  signed int iLimi
fab00 74 20 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  t .){.  unsigned
fab10 20 69 6e 74 20 68 3b 0a 20 20 61 73 73 65 72 74   int h;.  assert
fab20 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
fab30 68 65 6c 64 28 70 63 61 63 68 65 31 2e 6d 75 74  held(pcache1.mut
fab40 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 68 3d 30  ex) );.  for(h=0
fab50 3b 20 68 3c 70 43 61 63 68 65 2d 3e 6e 48 61 73  ; h<pCache->nHas
fab60 68 3b 20 68 2b 2b 29 7b 0a 20 20 20 20 50 67 48  h; h++){.    PgH
fab70 64 72 31 20 2a 2a 70 70 20 3d 20 26 70 43 61 63  dr1 **pp = &pCac
fab80 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 3b 20 0a  he->apHash[h]; .
fab90 20 20 20 20 50 67 48 64 72 31 20 2a 70 50 61 67      PgHdr1 *pPag
faba0 65 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 70  e;.    while( (p
fabb0 50 61 67 65 20 3d 20 2a 70 70 29 20 29 7b 0a 20  Page = *pp) ){. 
fabc0 20 20 20 20 20 69 66 28 20 70 50 61 67 65 2d 3e       if( pPage->
fabd0 69 4b 65 79 3e 3d 69 4c 69 6d 69 74 20 29 7b 0a  iKey>=iLimit ){.
fabe0 20 20 20 20 20 20 20 20 70 63 61 63 68 65 31 50          pcache1P
fabf0 69 6e 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  inPage(pPage);. 
fac00 20 20 20 20 20 20 20 2a 70 70 20 3d 20 70 50 61         *pp = pPa
fac10 67 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  ge->pNext;.     
fac20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50 61     pcache1FreePa
fac30 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20  ge(pPage);.     
fac40 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
fac50 70 70 20 3d 20 26 70 50 61 67 65 2d 3e 70 4e 65  pp = &pPage->pNe
fac60 78 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  xt;.      }.    
fac70 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  }.  }.}../******
fac80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fac90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
faca0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
facb0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
facc0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
facd0 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 70 63 61 63  *** sqlite3_pcac
face0 68 65 20 4d 65 74 68 6f 64 73 20 2a 2a 2a 2a 2a  he Methods *****
facf0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fad00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fad10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 0a 2a  *********/../*.*
fad20 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
fad30 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
fad40 70 63 61 63 68 65 2e 78 49 6e 69 74 20 6d 65 74  pcache.xInit met
fad50 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
fad60 6e 74 20 70 63 61 63 68 65 31 49 6e 69 74 28 76  nt pcache1Init(v
fad70 6f 69 64 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20  oid *NotUsed){. 
fad80 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
fad90 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 6d 65  R(NotUsed);.  me
fada0 6d 73 65 74 28 26 70 63 61 63 68 65 31 2c 20 30  mset(&pcache1, 0
fadb0 2c 20 73 69 7a 65 6f 66 28 70 63 61 63 68 65 31  , sizeof(pcache1
fadc0 29 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  ));.  if( sqlite
fadd0 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43  3GlobalConfig.bC
fade0 6f 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20  oreMutex ){.    
fadf0 70 63 61 63 68 65 31 2e 6d 75 74 65 78 20 3d 20  pcache1.mutex = 
fae00 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
fae10 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58  loc(SQLITE_MUTEX
fae20 5f 53 54 41 54 49 43 5f 4c 52 55 29 3b 0a 20 20  _STATIC_LRU);.  
fae30 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
fae40 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
fae50 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
fae60 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70 63 61   the sqlite3_pca
fae70 63 68 65 2e 78 53 68 75 74 64 6f 77 6e 20 6d 65  che.xShutdown me
fae80 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
fae90 76 6f 69 64 20 70 63 61 63 68 65 31 53 68 75 74  void pcache1Shut
faea0 64 6f 77 6e 28 76 6f 69 64 20 2a 4e 6f 74 55 73  down(void *NotUs
faeb0 65 64 29 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41  ed){.  UNUSED_PA
faec0 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29  RAMETER(NotUsed)
faed0 3b 0a 20 20 2f 2a 20 6e 6f 2d 6f 70 20 2a 2f 0a  ;.  /* no-op */.
faee0 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  }../*.** Impleme
faef0 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 73  ntation of the s
faf00 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e 78 43  qlite3_pcache.xC
faf10 72 65 61 74 65 20 6d 65 74 68 6f 64 2e 0a 2a 2a  reate method..**
faf20 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e  .** Allocate a n
faf30 65 77 20 63 61 63 68 65 2e 0a 2a 2f 0a 73 74 61  ew cache..*/.sta
faf40 74 69 63 20 73 71 6c 69 74 65 33 5f 70 63 61 63  tic sqlite3_pcac
faf50 68 65 20 2a 70 63 61 63 68 65 31 43 72 65 61 74  he *pcache1Creat
faf60 65 28 69 6e 74 20 73 7a 50 61 67 65 2c 20 69 6e  e(int szPage, in
faf70 74 20 62 50 75 72 67 65 61 62 6c 65 29 7b 0a 20  t bPurgeable){. 
faf80 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68 65   PCache1 *pCache
faf90 3b 0a 0a 20 20 70 43 61 63 68 65 20 3d 20 28 50  ;..  pCache = (P
fafa0 43 61 63 68 65 31 20 2a 29 73 71 6c 69 74 65 33  Cache1 *)sqlite3
fafb0 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 50  _malloc(sizeof(P
fafc0 43 61 63 68 65 31 29 29 3b 0a 20 20 69 66 28 20  Cache1));.  if( 
fafd0 70 43 61 63 68 65 20 29 7b 0a 20 20 20 20 6d 65  pCache ){.    me
fafe0 6d 73 65 74 28 70 43 61 63 68 65 2c 20 30 2c 20  mset(pCache, 0, 
faff0 73 69 7a 65 6f 66 28 50 43 61 63 68 65 31 29 29  sizeof(PCache1))
fb000 3b 0a 20 20 20 20 70 43 61 63 68 65 2d 3e 73 7a  ;.    pCache->sz
fb010 50 61 67 65 20 3d 20 73 7a 50 61 67 65 3b 0a 20  Page = szPage;. 
fb020 20 20 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67     pCache->bPurg
fb030 65 61 62 6c 65 20 3d 20 28 62 50 75 72 67 65 61  eable = (bPurgea
fb040 62 6c 65 20 3f 20 31 20 3a 20 30 29 3b 0a 20 20  ble ? 1 : 0);.  
fb050 20 20 69 66 28 20 62 50 75 72 67 65 61 62 6c 65    if( bPurgeable
fb060 20 29 7b 0a 20 20 20 20 20 20 70 43 61 63 68 65   ){.      pCache
fb070 2d 3e 6e 4d 69 6e 20 3d 20 31 30 3b 0a 20 20 20  ->nMin = 10;.   
fb080 20 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d     pcache1EnterM
fb090 75 74 65 78 28 29 3b 0a 20 20 20 20 20 20 70 63  utex();.      pc
fb0a0 61 63 68 65 31 2e 6e 4d 69 6e 50 61 67 65 20 2b  ache1.nMinPage +
fb0b0 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 69 6e 3b 0a  = pCache->nMin;.
fb0c0 20 20 20 20 20 20 70 63 61 63 68 65 31 4c 65 61        pcache1Lea
fb0d0 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 20 20 7d  veMutex();.    }
fb0e0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 73  .  }.  return (s
fb0f0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 29  qlite3_pcache *)
fb100 70 43 61 63 68 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pCache;.}../*.**
fb110 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
fb120 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
fb130 63 61 63 68 65 2e 78 43 61 63 68 65 73 69 7a 65  cache.xCachesize
fb140 20 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20   method. .**.** 
fb150 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 63 61  Configure the ca
fb160 63 68 65 5f 73 69 7a 65 20 6c 69 6d 69 74 20 66  che_size limit f
fb170 6f 72 20 61 20 63 61 63 68 65 2e 0a 2a 2f 0a 73  or a cache..*/.s
fb180 74 61 74 69 63 20 76 6f 69 64 20 70 63 61 63 68  tatic void pcach
fb190 65 31 43 61 63 68 65 73 69 7a 65 28 73 71 6c 69  e1Cachesize(sqli
fb1a0 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 69  te3_pcache *p, i
fb1b0 6e 74 20 6e 4d 61 78 29 7b 0a 20 20 50 43 61 63  nt nMax){.  PCac
fb1c0 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28 50  he1 *pCache = (P
fb1d0 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 69 66  Cache1 *)p;.  if
fb1e0 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  ( pCache->bPurge
fb1f0 61 62 6c 65 20 29 7b 0a 20 20 20 20 70 63 61 63  able ){.    pcac
fb200 68 65 31 45 6e 74 65 72 4d 75 74 65 78 28 29 3b  he1EnterMutex();
fb210 0a 20 20 20 20 70 63 61 63 68 65 31 2e 6e 4d 61  .    pcache1.nMa
fb220 78 50 61 67 65 20 2b 3d 20 28 6e 4d 61 78 20 2d  xPage += (nMax -
fb230 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78 29 3b 0a   pCache->nMax);.
fb240 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 4d 61 78      pCache->nMax
fb250 20 3d 20 6e 4d 61 78 3b 0a 20 20 20 20 70 63 61   = nMax;.    pca
fb260 63 68 65 31 45 6e 66 6f 72 63 65 4d 61 78 50 61  che1EnforceMaxPa
fb270 67 65 28 29 3b 0a 20 20 20 20 70 63 61 63 68 65  ge();.    pcache
fb280 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20  1LeaveMutex();. 
fb290 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
fb2a0 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
fb2b0 65 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65  e sqlite3_pcache
fb2c0 2e 78 50 61 67 65 63 6f 75 6e 74 20 6d 65 74 68  .xPagecount meth
fb2d0 6f 64 2e 20 0a 2a 2f 0a 73 74 61 74 69 63 20 69  od. .*/.static i
fb2e0 6e 74 20 70 63 61 63 68 65 31 50 61 67 65 63 6f  nt pcache1Pageco
fb2f0 75 6e 74 28 73 71 6c 69 74 65 33 5f 70 63 61 63  unt(sqlite3_pcac
fb300 68 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 3b  he *p){.  int n;
fb310 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72 4d  .  pcache1EnterM
fb320 75 74 65 78 28 29 3b 0a 20 20 6e 20 3d 20 28 28  utex();.  n = ((
fb330 50 43 61 63 68 65 31 20 2a 29 70 29 2d 3e 6e 50  PCache1 *)p)->nP
fb340 61 67 65 3b 0a 20 20 70 63 61 63 68 65 31 4c 65  age;.  pcache1Le
fb350 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20 72 65  aveMutex();.  re
fb360 74 75 72 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn n;.}../*.**
fb370 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
fb380 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
fb390 63 61 63 68 65 2e 78 46 65 74 63 68 20 6d 65 74  cache.xFetch met
fb3a0 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 46 65 74 63  hod. .**.** Fetc
fb3b0 68 20 61 20 70 61 67 65 20 62 79 20 6b 65 79 20  h a page by key 
fb3c0 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 57 68 65  value..**.** Whe
fb3d0 74 68 65 72 20 6f 72 20 6e 6f 74 20 61 20 6e 65  ther or not a ne
fb3e0 77 20 70 61 67 65 20 6d 61 79 20 62 65 20 61 6c  w page may be al
fb3f0 6c 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20  located by this 
fb400 66 75 6e 63 74 69 6f 6e 20 64 65 70 65 6e 64 73  function depends
fb410 20 6f 6e 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65   on.** the value
fb420 20 6f 66 20 74 68 65 20 63 72 65 61 74 65 46 6c   of the createFl
fb430 61 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  ag argument..**.
fb440 2a 2a 20 54 68 65 72 65 20 61 72 65 20 74 68 72  ** There are thr
fb450 65 65 20 64 69 66 66 65 72 65 6e 74 20 61 70 70  ee different app
fb460 72 6f 61 63 68 65 73 20 74 6f 20 6f 62 74 61 69  roaches to obtai
fb470 6e 69 6e 67 20 73 70 61 63 65 20 66 6f 72 20 61  ning space for a
fb480 20 70 61 67 65 2c 0a 2a 2a 20 64 65 70 65 6e 64   page,.** depend
fb490 69 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65  ing on the value
fb4a0 20 6f 66 20 70 61 72 61 6d 65 74 65 72 20 63 72   of parameter cr
fb4b0 65 61 74 65 46 6c 61 67 20 28 77 68 69 63 68 20  eateFlag (which 
fb4c0 6d 61 79 20 62 65 20 30 2c 20 31 20 6f 72 20 32  may be 0, 1 or 2
fb4d0 29 2e 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 52 65  )..**.**   1. Re
fb4e0 67 61 72 64 6c 65 73 73 20 6f 66 20 74 68 65 20  gardless of the 
fb4f0 76 61 6c 75 65 20 6f 66 20 63 72 65 61 74 65 46  value of createF
fb500 6c 61 67 2c 20 74 68 65 20 63 61 63 68 65 20 69  lag, the cache i
fb510 73 20 73 65 61 72 63 68 65 64 20 66 6f 72 20 61  s searched for a
fb520 20 0a 2a 2a 20 20 20 20 20 20 63 6f 70 79 20 6f   .**      copy o
fb530 66 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  f the requested 
fb540 70 61 67 65 2e 20 49 66 20 6f 6e 65 20 69 73 20  page. If one is 
fb550 66 6f 75 6e 64 2c 20 69 74 20 69 73 20 72 65 74  found, it is ret
fb560 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32  urned..**.**   2
fb570 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 3d  . If createFlag=
fb580 3d 30 20 61 6e 64 20 74 68 65 20 70 61 67 65 20  =0 and the page 
fb590 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69  is not already i
fb5a0 6e 20 74 68 65 20 63 61 63 68 65 2c 20 4e 55 4c  n the cache, NUL
fb5b0 4c 20 69 73 0a 2a 2a 20 20 20 20 20 20 72 65 74  L is.**      ret
fb5c0 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 33  urned..**.**   3
fb5d0 2e 20 49 66 20 63 72 65 61 74 65 46 6c 61 67 20  . If createFlag 
fb5e0 69 73 20 31 2c 20 74 68 65 20 63 61 63 68 65 20  is 1, the cache 
fb5f0 69 73 20 6d 61 72 6b 65 64 20 61 73 20 70 75 72  is marked as pur
fb600 67 65 61 62 6c 65 20 61 6e 64 20 74 68 65 20 70  geable and the p
fb610 61 67 65 20 69 73 20 0a 2a 2a 20 20 20 20 20 20  age is .**      
fb620 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e 20 74  not already in t
fb630 68 65 20 63 61 63 68 65 2c 20 61 6e 64 20 69 66  he cache, and if
fb640 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 66   either of the f
fb650 6f 6c 6c 6f 77 69 6e 67 20 61 72 65 20 74 72 75  ollowing are tru
fb660 65 2c 20 0a 2a 2a 20 20 20 20 20 20 72 65 74 75  e, .**      retu
fb670 72 6e 20 4e 55 4c 4c 3a 0a 2a 2a 0a 2a 2a 20 20  rn NULL:.**.**  
fb680 20 20 20 20 20 28 61 29 20 74 68 65 20 6e 75 6d       (a) the num
fb690 62 65 72 20 6f 66 20 70 61 67 65 73 20 70 69 6e  ber of pages pin
fb6a0 6e 65 64 20 62 79 20 74 68 65 20 63 61 63 68 65  ned by the cache
fb6b0 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
fb6c0 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43  .**           PC
fb6d0 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a  ache1.nMax, or.*
fb6e0 2a 20 20 20 20 20 20 20 28 62 29 20 74 68 65 20  *       (b) the 
fb6f0 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  number of pages 
fb700 70 69 6e 6e 65 64 20 62 79 20 74 68 65 20 63 61  pinned by the ca
fb710 63 68 65 20 69 73 20 67 72 65 61 74 65 72 20 74  che is greater t
fb720 68 61 6e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  han.**          
fb730 20 74 68 65 20 73 75 6d 20 6f 66 20 6e 4d 61 78   the sum of nMax
fb740 20 66 6f 72 20 61 6c 6c 20 70 75 72 67 65 61 62   for all purgeab
fb750 6c 65 20 63 61 63 68 65 73 2c 20 6c 65 73 73 20  le caches, less 
fb760 74 68 65 20 73 75 6d 20 6f 66 20 0a 2a 2a 20 20  the sum of .**  
fb770 20 20 20 20 20 20 20 20 20 6e 4d 69 6e 20 66 6f           nMin fo
fb780 72 20 61 6c 6c 20 6f 74 68 65 72 20 70 75 72 67  r all other purg
fb790 65 61 62 6c 65 20 63 61 63 68 65 73 2e 20 0a 2a  eable caches. .*
fb7a0 2a 0a 2a 2a 20 20 20 34 2e 20 49 66 20 6e 6f 6e  *.**   4. If non
fb7b0 65 20 6f 66 20 74 68 65 20 66 69 72 73 74 20 74  e of the first t
fb7c0 68 72 65 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20  hree conditions 
fb7d0 61 70 70 6c 79 20 61 6e 64 20 74 68 65 20 63 61  apply and the ca
fb7e0 63 68 65 20 69 73 20 6d 61 72 6b 65 64 0a 2a 2a  che is marked.**
fb7f0 20 20 20 20 20 20 61 73 20 70 75 72 67 65 61 62        as purgeab
fb800 6c 65 2c 20 61 6e 64 20 69 66 20 6f 6e 65 20 6f  le, and if one o
fb810 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
fb820 69 73 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20  is true:.**.**  
fb830 20 20 20 20 20 28 61 29 20 54 68 65 20 6e 75 6d       (a) The num
fb840 62 65 72 20 6f 66 20 70 61 67 65 73 20 61 6c 6c  ber of pages all
fb850 6f 63 61 74 65 64 20 66 6f 72 20 74 68 65 20 63  ocated for the c
fb860 61 63 68 65 20 69 73 20 61 6c 72 65 61 64 79 20  ache is already 
fb870 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 50 43  .**           PC
fb880 61 63 68 65 31 2e 6e 4d 61 78 2c 20 6f 72 0a 2a  ache1.nMax, or.*
fb890 2a 0a 2a 2a 20 20 20 20 20 20 20 28 62 29 20 54  *.**       (b) T
fb8a0 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  he number of pag
fb8b0 65 73 20 61 6c 6c 6f 63 61 74 65 64 20 66 6f 72  es allocated for
fb8c0 20 61 6c 6c 20 70 75 72 67 65 61 62 6c 65 20 63   all purgeable c
fb8d0 61 63 68 65 73 20 69 73 0a 2a 2a 20 20 20 20 20  aches is.**     
fb8e0 20 20 20 20 20 20 61 6c 72 65 61 64 79 20 65 71        already eq
fb8f0 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74 65  ual to or greate
fb900 72 20 74 68 61 6e 20 74 68 65 20 73 75 6d 20 6f  r than the sum o
fb910 66 20 6e 4d 61 78 20 66 6f 72 20 61 6c 6c 0a 2a  f nMax for all.*
fb920 2a 20 20 20 20 20 20 20 20 20 20 20 70 75 72 67  *           purg
fb930 65 61 62 6c 65 20 63 61 63 68 65 73 2c 0a 2a 2a  eable caches,.**
fb940 0a 2a 2a 20 20 20 20 20 20 74 68 65 6e 20 61 74  .**      then at
fb950 74 65 6d 70 74 20 74 6f 20 72 65 63 79 63 6c 65  tempt to recycle
fb960 20 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65   a page from the
fb970 20 4c 52 55 20 6c 69 73 74 2e 20 49 66 20 69 74   LRU list. If it
fb980 20 69 73 20 74 68 65 20 72 69 67 68 74 0a 2a 2a   is the right.**
fb990 20 20 20 20 20 20 73 69 7a 65 2c 20 72 65 74 75        size, retu
fb9a0 72 6e 20 74 68 65 20 72 65 63 79 63 6c 65 64 20  rn the recycled 
fb9b0 62 75 66 66 65 72 2e 20 4f 74 68 65 72 77 69 73  buffer. Otherwis
fb9c0 65 2c 20 66 72 65 65 20 74 68 65 20 62 75 66 66  e, free the buff
fb9d0 65 72 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 70  er and.**      p
fb9e0 72 6f 63 65 65 64 20 74 6f 20 73 74 65 70 20 35  roceed to step 5
fb9f0 2e 20 0a 2a 2a 0a 2a 2a 20 20 20 35 2e 20 4f 74  . .**.**   5. Ot
fba00 68 65 72 77 69 73 65 2c 20 61 6c 6c 6f 63 61 74  herwise, allocat
fba10 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61 20 6e  e and return a n
fba20 65 77 20 70 61 67 65 20 62 75 66 66 65 72 2e 0a  ew page buffer..
fba30 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a  */.static void *
fba40 70 63 61 63 68 65 31 46 65 74 63 68 28 73 71 6c  pcache1Fetch(sql
fba50 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20  ite3_pcache *p, 
fba60 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4b 65  unsigned int iKe
fba70 79 2c 20 69 6e 74 20 63 72 65 61 74 65 46 6c 61  y, int createFla
fba80 67 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  g){.  unsigned i
fba90 6e 74 20 6e 50 69 6e 6e 65 64 3b 0a 20 20 50 43  nt nPinned;.  PC
fbaa0 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20  ache1 *pCache = 
fbab0 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20  (PCache1 *)p;.  
fbac0 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20  PgHdr1 *pPage = 
fbad0 30 3b 0a 0a 20 20 70 63 61 63 68 65 31 45 6e 74  0;..  pcache1Ent
fbae0 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 69 66 28  erMutex();.  if(
fbaf0 20 63 72 65 61 74 65 46 6c 61 67 3d 3d 31 20 29   createFlag==1 )
fbb00 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e   sqlite3BeginBen
fbb10 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 0a 20 20  ignMalloc();..  
fbb20 2f 2a 20 53 65 61 72 63 68 20 74 68 65 20 68 61  /* Search the ha
fbb30 73 68 20 74 61 62 6c 65 20 66 6f 72 20 61 6e 20  sh table for an 
fbb40 65 78 69 73 74 69 6e 67 20 65 6e 74 72 79 2e 20  existing entry. 
fbb50 2a 2f 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  */.  if( pCache-
fbb60 3e 6e 48 61 73 68 3e 30 20 29 7b 0a 20 20 20 20  >nHash>0 ){.    
fbb70 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 68 20 3d  unsigned int h =
fbb80 20 69 4b 65 79 20 25 20 70 43 61 63 68 65 2d 3e   iKey % pCache->
fbb90 6e 48 61 73 68 3b 0a 20 20 20 20 66 6f 72 28 70  nHash;.    for(p
fbba0 50 61 67 65 3d 70 43 61 63 68 65 2d 3e 61 70 48  Page=pCache->apH
fbbb0 61 73 68 5b 68 5d 3b 20 70 50 61 67 65 26 26 70  ash[h]; pPage&&p
fbbc0 50 61 67 65 2d 3e 69 4b 65 79 21 3d 69 4b 65 79  Page->iKey!=iKey
fbbd0 3b 20 70 50 61 67 65 3d 70 50 61 67 65 2d 3e 70  ; pPage=pPage->p
fbbe0 4e 65 78 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66  Next);.  }..  if
fbbf0 28 20 70 50 61 67 65 20 7c 7c 20 63 72 65 61 74  ( pPage || creat
fbc00 65 46 6c 61 67 3d 3d 30 20 29 7b 0a 20 20 20 20  eFlag==0 ){.    
fbc10 70 63 61 63 68 65 31 50 69 6e 50 61 67 65 28 70  pcache1PinPage(p
fbc20 50 61 67 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20  Page);.    goto 
fbc30 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  fetch_out;.  }..
fbc40 20 20 2f 2a 20 53 74 65 70 20 33 20 6f 66 20 68    /* Step 3 of h
fbc50 65 61 64 65 72 20 63 6f 6d 6d 65 6e 74 2e 20 2a  eader comment. *
fbc60 2f 0a 20 20 6e 50 69 6e 6e 65 64 20 3d 20 70 43  /.  nPinned = pC
fbc70 61 63 68 65 2d 3e 6e 50 61 67 65 20 2d 20 70 43  ache->nPage - pC
fbc80 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62 6c  ache->nRecyclabl
fbc90 65 3b 0a 20 20 69 66 28 20 63 72 65 61 74 65 46  e;.  if( createF
fbca0 6c 61 67 3d 3d 31 20 26 26 20 70 43 61 63 68 65  lag==1 && pCache
fbcb0 2d 3e 62 50 75 72 67 65 61 62 6c 65 20 26 26 20  ->bPurgeable && 
fbcc0 28 0a 20 20 20 20 20 20 20 20 6e 50 69 6e 6e 65  (.        nPinne
fbcd0 64 3e 3d 28 70 63 61 63 68 65 31 2e 6e 4d 61 78  d>=(pcache1.nMax
fbce0 50 61 67 65 2b 70 43 61 63 68 65 2d 3e 6e 4d 69  Page+pCache->nMi
fbcf0 6e 2d 70 63 61 63 68 65 31 2e 6e 4d 69 6e 50 61  n-pcache1.nMinPa
fbd00 67 65 29 0a 20 20 20 20 20 7c 7c 20 6e 50 69 6e  ge).     || nPin
fbd10 6e 65 64 3e 3d 28 70 43 61 63 68 65 2d 3e 6e 4d  ned>=(pCache->nM
fbd20 61 78 29 0a 20 20 29 29 7b 0a 20 20 20 20 67 6f  ax).  )){.    go
fbd30 74 6f 20 66 65 74 63 68 5f 6f 75 74 3b 0a 20 20  to fetch_out;.  
fbd40 7d 0a 0a 20 20 69 66 28 20 70 43 61 63 68 65 2d  }..  if( pCache-
fbd50 3e 6e 50 61 67 65 3e 3d 70 43 61 63 68 65 2d 3e  >nPage>=pCache->
fbd60 6e 48 61 73 68 20 26 26 20 70 63 61 63 68 65 31  nHash && pcache1
fbd70 52 65 73 69 7a 65 48 61 73 68 28 70 43 61 63 68  ResizeHash(pCach
fbd80 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66  e) ){.    goto f
fbd90 65 74 63 68 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20  etch_out;.  }.. 
fbda0 20 2f 2a 20 53 74 65 70 20 34 2e 20 54 72 79 20   /* Step 4. Try 
fbdb0 74 6f 20 72 65 63 79 63 6c 65 20 61 20 70 61 67  to recycle a pag
fbdc0 65 20 62 75 66 66 65 72 20 69 66 20 61 70 70 72  e buffer if appr
fbdd0 6f 70 72 69 61 74 65 2e 20 2a 2f 0a 20 20 69 66  opriate. */.  if
fbde0 28 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67 65  ( pCache->bPurge
fbdf0 61 62 6c 65 20 26 26 20 70 63 61 63 68 65 31 2e  able && pcache1.
fbe00 70 4c 72 75 54 61 69 6c 20 26 26 20 28 0a 20 20  pLruTail && (.  
fbe10 20 20 20 20 70 43 61 63 68 65 2d 3e 6e 50 61 67      pCache->nPag
fbe20 65 3e 3d 70 43 61 63 68 65 2d 3e 6e 4d 61 78 2d  e>=pCache->nMax-
fbe30 31 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43 75  1 || pcache1.nCu
fbe40 72 72 65 6e 74 50 61 67 65 3e 3d 70 63 61 63 68  rrentPage>=pcach
fbe50 65 31 2e 6e 4d 61 78 50 61 67 65 0a 20 20 29 29  e1.nMaxPage.  ))
fbe60 7b 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 63  {.    pPage = pc
fbe70 61 63 68 65 31 2e 70 4c 72 75 54 61 69 6c 3b 0a  ache1.pLruTail;.
fbe80 20 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76      pcache1Remov
fbe90 65 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29  eFromHash(pPage)
fbea0 3b 0a 20 20 20 20 70 63 61 63 68 65 31 50 69 6e  ;.    pcache1Pin
fbeb0 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
fbec0 20 69 66 28 20 70 50 61 67 65 2d 3e 70 43 61 63   if( pPage->pCac
fbed0 68 65 2d 3e 73 7a 50 61 67 65 21 3d 70 43 61 63  he->szPage!=pCac
fbee0 68 65 2d 3e 73 7a 50 61 67 65 20 29 7b 0a 20 20  he->szPage ){.  
fbef0 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65 50      pcache1FreeP
fbf00 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20  age(pPage);.    
fbf10 20 20 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 20    pPage = 0;.   
fbf20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 63   }else{.      pc
fbf30 61 63 68 65 31 2e 6e 43 75 72 72 65 6e 74 50 61  ache1.nCurrentPa
fbf40 67 65 20 2d 3d 20 28 70 50 61 67 65 2d 3e 70 43  ge -= (pPage->pC
fbf50 61 63 68 65 2d 3e 62 50 75 72 67 65 61 62 6c 65  ache->bPurgeable
fbf60 20 2d 20 70 43 61 63 68 65 2d 3e 62 50 75 72 67   - pCache->bPurg
fbf70 65 61 62 6c 65 29 3b 0a 20 20 20 20 7d 0a 20 20  eable);.    }.  
fbf80 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 35 2e 20  }..  /* Step 5. 
fbf90 49 66 20 61 20 75 73 61 62 6c 65 20 70 61 67 65  If a usable page
fbfa0 20 62 75 66 66 65 72 20 68 61 73 20 73 74 69 6c   buffer has stil
fbfb0 6c 20 6e 6f 74 20 62 65 65 6e 20 66 6f 75 6e 64  l not been found
fbfc0 2c 20 0a 20 20 2a 2a 20 61 74 74 65 6d 70 74 20  , .  ** attempt 
fbfd0 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65  to allocate a ne
fbfe0 77 20 6f 6e 65 2e 20 0a 20 20 2a 2f 0a 20 20 69  w one. .  */.  i
fbff0 66 28 20 21 70 50 61 67 65 20 29 7b 0a 20 20 20  f( !pPage ){.   
fc000 20 70 50 61 67 65 20 3d 20 70 63 61 63 68 65 31   pPage = pcache1
fc010 41 6c 6c 6f 63 50 61 67 65 28 70 43 61 63 68 65  AllocPage(pCache
fc020 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  );.  }..  if( pP
fc030 61 67 65 20 29 7b 0a 20 20 20 20 75 6e 73 69 67  age ){.    unsig
fc040 6e 65 64 20 69 6e 74 20 68 20 3d 20 69 4b 65 79  ned int h = iKey
fc050 20 25 20 70 43 61 63 68 65 2d 3e 6e 48 61 73 68   % pCache->nHash
fc060 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 70 50 61  ;.    memset(pPa
fc070 67 65 2c 20 30 2c 20 70 43 61 63 68 65 2d 3e 73  ge, 0, pCache->s
fc080 7a 50 61 67 65 20 2b 20 73 69 7a 65 6f 66 28 50  zPage + sizeof(P
fc090 67 48 64 72 31 29 29 3b 0a 20 20 20 20 70 43 61  gHdr1));.    pCa
fc0a0 63 68 65 2d 3e 6e 50 61 67 65 2b 2b 3b 0a 20 20  che->nPage++;.  
fc0b0 20 20 70 50 61 67 65 2d 3e 69 4b 65 79 20 3d 20    pPage->iKey = 
fc0c0 69 4b 65 79 3b 0a 20 20 20 20 70 50 61 67 65 2d  iKey;.    pPage-
fc0d0 3e 70 4e 65 78 74 20 3d 20 70 43 61 63 68 65 2d  >pNext = pCache-
fc0e0 3e 61 70 48 61 73 68 5b 68 5d 3b 0a 20 20 20 20  >apHash[h];.    
fc0f0 70 50 61 67 65 2d 3e 70 43 61 63 68 65 20 3d 20  pPage->pCache = 
fc100 70 43 61 63 68 65 3b 0a 20 20 20 20 70 43 61 63  pCache;.    pCac
fc110 68 65 2d 3e 61 70 48 61 73 68 5b 68 5d 20 3d 20  he->apHash[h] = 
fc120 70 50 61 67 65 3b 0a 20 20 7d 0a 0a 66 65 74 63  pPage;.  }..fetc
fc130 68 5f 6f 75 74 3a 0a 20 20 69 66 28 20 63 72 65  h_out:.  if( cre
fc140 61 74 65 46 6c 61 67 3d 3d 31 20 29 20 73 71 6c  ateFlag==1 ) sql
fc150 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
fc160 6c 6f 63 28 29 3b 0a 20 20 70 63 61 63 68 65 31  loc();.  pcache1
fc170 4c 65 61 76 65 4d 75 74 65 78 28 29 3b 0a 20 20  LeaveMutex();.  
fc180 72 65 74 75 72 6e 20 28 70 50 61 67 65 20 3f 20  return (pPage ? 
fc190 50 47 48 44 52 31 5f 54 4f 5f 50 41 47 45 28 70  PGHDR1_TO_PAGE(p
fc1a0 50 61 67 65 29 20 3a 20 30 29 3b 0a 7d 0a 0a 0a  Page) : 0);.}...
fc1b0 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
fc1c0 74 69 6f 6e 20 6f 66 20 74 68 65 20 73 71 6c 69  tion of the sqli
fc1d0 74 65 33 5f 70 63 61 63 68 65 2e 78 55 6e 70 69  te3_pcache.xUnpi
fc1e0 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2a 0a 2a 2a 20  n method..**.** 
fc1f0 4d 61 72 6b 20 61 20 70 61 67 65 20 61 73 20 75  Mark a page as u
fc200 6e 70 69 6e 6e 65 64 20 28 65 6c 69 67 69 62 6c  npinned (eligibl
fc210 65 20 66 6f 72 20 61 73 79 6e 63 68 72 6f 6e 6f  e for asynchrono
fc220 75 73 20 72 65 63 79 63 6c 69 6e 67 29 2e 0a 2a  us recycling)..*
fc230 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 63  /.static void pc
fc240 61 63 68 65 31 55 6e 70 69 6e 28 73 71 6c 69 74  ache1Unpin(sqlit
fc250 65 33 5f 70 63 61 63 68 65 20 2a 70 2c 20 76 6f  e3_pcache *p, vo
fc260 69 64 20 2a 70 50 67 2c 20 69 6e 74 20 72 65 75  id *pPg, int reu
fc270 73 65 55 6e 6c 69 6b 65 6c 79 29 7b 0a 20 20 50  seUnlikely){.  P
fc280 43 61 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d  Cache1 *pCache =
fc290 20 28 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20   (PCache1 *)p;. 
fc2a0 20 50 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d   PgHdr1 *pPage =
fc2b0 20 50 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28   PAGE_TO_PGHDR1(
fc2c0 70 50 67 29 3b 0a 0a 20 20 70 63 61 63 68 65 31  pPg);..  pcache1
fc2d0 45 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20  EnterMutex();.. 
fc2e0 20 2f 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72   /* It is an err
fc2f0 6f 72 20 74 6f 20 63 61 6c 6c 20 74 68 69 73 20  or to call this 
fc300 66 75 6e 63 74 69 6f 6e 20 69 66 20 74 68 65 20  function if the 
fc310 70 61 67 65 20 69 73 20 61 6c 72 65 61 64 79 20  page is already 
fc320 0a 20 20 2a 2a 20 70 61 72 74 20 6f 66 20 74 68  .  ** part of th
fc330 65 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73  e global LRU lis
fc340 74 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  t..  */.  assert
fc350 28 20 70 50 61 67 65 2d 3e 70 4c 72 75 50 72 65  ( pPage->pLruPre
fc360 76 3d 3d 30 20 26 26 20 70 50 61 67 65 2d 3e 70  v==0 && pPage->p
fc370 4c 72 75 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20  LruNext==0 );.  
fc380 61 73 73 65 72 74 28 20 70 63 61 63 68 65 31 2e  assert( pcache1.
fc390 70 4c 72 75 48 65 61 64 21 3d 70 50 61 67 65 20  pLruHead!=pPage 
fc3a0 26 26 20 70 63 61 63 68 65 31 2e 70 4c 72 75 54  && pcache1.pLruT
fc3b0 61 69 6c 21 3d 70 50 61 67 65 20 29 3b 0a 0a 20  ail!=pPage );.. 
fc3c0 20 69 66 28 20 72 65 75 73 65 55 6e 6c 69 6b 65   if( reuseUnlike
fc3d0 6c 79 20 7c 7c 20 70 63 61 63 68 65 31 2e 6e 43  ly || pcache1.nC
fc3e0 75 72 72 65 6e 74 50 61 67 65 3e 70 63 61 63 68  urrentPage>pcach
fc3f0 65 31 2e 6e 4d 61 78 50 61 67 65 20 29 7b 0a 20  e1.nMaxPage ){. 
fc400 20 20 20 70 63 61 63 68 65 31 52 65 6d 6f 76 65     pcache1Remove
fc410 46 72 6f 6d 48 61 73 68 28 70 50 61 67 65 29 3b  FromHash(pPage);
fc420 0a 20 20 20 20 70 63 61 63 68 65 31 46 72 65 65  .    pcache1Free
fc430 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d  Page(pPage);.  }
fc440 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 41 64 64  else{.    /* Add
fc450 20 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65   the page to the
fc460 20 67 6c 6f 62 61 6c 20 4c 52 55 20 6c 69 73 74   global LRU list
fc470 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 74 68 65 20  . Normally, the 
fc480 70 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f  page is added to
fc490 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 61 64  .    ** the head
fc4a0 20 6f 66 20 74 68 65 20 6c 69 73 74 20 28 6c 61   of the list (la
fc4b0 73 74 20 70 61 67 65 20 74 6f 20 62 65 20 72 65  st page to be re
fc4c0 63 79 63 6c 65 64 29 2e 20 48 6f 77 65 76 65 72  cycled). However
fc4d0 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 2a 2a  , if the .    **
fc4e0 20 72 65 75 73 65 55 6e 6c 69 6b 65 6c 79 20 66   reuseUnlikely f
fc4f0 6c 61 67 20 70 61 73 73 65 64 20 74 6f 20 74 68  lag passed to th
fc500 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 74  is function is t
fc510 72 75 65 2c 20 74 68 65 20 70 61 67 65 20 69 73  rue, the page is
fc520 20 61 64 64 65 64 0a 20 20 20 20 2a 2a 20 74 6f   added.    ** to
fc530 20 74 68 65 20 74 61 69 6c 20 6f 66 20 74 68 65   the tail of the
fc540 20 6c 69 73 74 20 28 66 69 72 73 74 20 70 61 67   list (first pag
fc550 65 20 74 6f 20 62 65 20 72 65 63 79 63 6c 65 64  e to be recycled
fc560 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  )..    */.    if
fc570 28 20 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65  ( pcache1.pLruHe
fc580 61 64 20 29 7b 0a 20 20 20 20 20 20 70 63 61 63  ad ){.      pcac
fc590 68 65 31 2e 70 4c 72 75 48 65 61 64 2d 3e 70 4c  he1.pLruHead->pL
fc5a0 72 75 50 72 65 76 20 3d 20 70 50 61 67 65 3b 0a  ruPrev = pPage;.
fc5b0 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70 4c 72        pPage->pLr
fc5c0 75 4e 65 78 74 20 3d 20 70 63 61 63 68 65 31 2e  uNext = pcache1.
fc5d0 70 4c 72 75 48 65 61 64 3b 0a 20 20 20 20 20 20  pLruHead;.      
fc5e0 70 63 61 63 68 65 31 2e 70 4c 72 75 48 65 61 64  pcache1.pLruHead
fc5f0 20 3d 20 70 50 61 67 65 3b 0a 20 20 20 20 7d 65   = pPage;.    }e
fc600 6c 73 65 7b 0a 20 20 20 20 20 20 70 63 61 63 68  lse{.      pcach
fc610 65 31 2e 70 4c 72 75 54 61 69 6c 20 3d 20 70 50  e1.pLruTail = pP
fc620 61 67 65 3b 0a 20 20 20 20 20 20 70 63 61 63 68  age;.      pcach
fc630 65 31 2e 70 4c 72 75 48 65 61 64 20 3d 20 70 50  e1.pLruHead = pP
fc640 61 67 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  age;.    }.    p
fc650 43 61 63 68 65 2d 3e 6e 52 65 63 79 63 6c 61 62  Cache->nRecyclab
fc660 6c 65 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 70 63 61  le++;.  }..  pca
fc670 63 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29  che1LeaveMutex()
fc680 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
fc690 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
fc6a0 20 73 71 6c 69 74 65 33 5f 70 63 61 63 68 65 2e   sqlite3_pcache.
fc6b0 78 52 65 6b 65 79 20 6d 65 74 68 6f 64 2e 20 0a  xRekey method. .
fc6c0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
fc6d0 63 61 63 68 65 31 52 65 6b 65 79 28 0a 20 20 73  cache1Rekey(.  s
fc6e0 71 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70  qlite3_pcache *p
fc6f0 2c 0a 20 20 76 6f 69 64 20 2a 70 50 67 2c 0a 20  ,.  void *pPg,. 
fc700 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 69 4f   unsigned int iO
fc710 6c 64 2c 0a 20 20 75 6e 73 69 67 6e 65 64 20 69  ld,.  unsigned i
fc720 6e 74 20 69 4e 65 77 0a 29 7b 0a 20 20 50 43 61  nt iNew.){.  PCa
fc730 63 68 65 31 20 2a 70 43 61 63 68 65 20 3d 20 28  che1 *pCache = (
fc740 50 43 61 63 68 65 31 20 2a 29 70 3b 0a 20 20 50  PCache1 *)p;.  P
fc750 67 48 64 72 31 20 2a 70 50 61 67 65 20 3d 20 50  gHdr1 *pPage = P
fc760 41 47 45 5f 54 4f 5f 50 47 48 44 52 31 28 70 50  AGE_TO_PGHDR1(pP
fc770 67 29 3b 0a 20 20 50 67 48 64 72 31 20 2a 2a 70  g);.  PgHdr1 **p
fc780 70 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e  p;.  unsigned in
fc790 74 20 68 3b 20 0a 20 20 61 73 73 65 72 74 28 20  t h; .  assert( 
fc7a0 70 50 61 67 65 2d 3e 69 4b 65 79 3d 3d 69 4f 6c  pPage->iKey==iOl
fc7b0 64 20 29 3b 0a 0a 20 20 70 63 61 63 68 65 31 45  d );..  pcache1E
fc7c0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 0a 20 20  nterMutex();..  
fc7d0 68 20 3d 20 69 4f 6c 64 25 70 43 61 63 68 65 2d  h = iOld%pCache-
fc7e0 3e 6e 48 61 73 68 3b 0a 20 20 70 70 20 3d 20 26  >nHash;.  pp = &
fc7f0 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
fc800 5d 3b 0a 20 20 77 68 69 6c 65 28 20 28 2a 70 70  ];.  while( (*pp
fc810 29 21 3d 70 50 61 67 65 20 29 7b 0a 20 20 20 20  )!=pPage ){.    
fc820 70 70 20 3d 20 26 28 2a 70 70 29 2d 3e 70 4e 65  pp = &(*pp)->pNe
fc830 78 74 3b 0a 20 20 7d 0a 20 20 2a 70 70 20 3d 20  xt;.  }.  *pp = 
fc840 70 50 61 67 65 2d 3e 70 4e 65 78 74 3b 0a 0a 20  pPage->pNext;.. 
fc850 20 68 20 3d 20 69 4e 65 77 25 70 43 61 63 68 65   h = iNew%pCache
fc860 2d 3e 6e 48 61 73 68 3b 0a 20 20 70 50 61 67 65  ->nHash;.  pPage
fc870 2d 3e 69 4b 65 79 20 3d 20 69 4e 65 77 3b 0a 20  ->iKey = iNew;. 
fc880 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d 20   pPage->pNext = 
fc890 70 43 61 63 68 65 2d 3e 61 70 48 61 73 68 5b 68  pCache->apHash[h
fc8a0 5d 3b 0a 20 20 70 43 61 63 68 65 2d 3e 61 70 48  ];.  pCache->apH
fc8b0 61 73 68 5b 68 5d 20 3d 20 70 50 61 67 65 3b 0a  ash[h] = pPage;.
fc8c0 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65 4d  .  pcache1LeaveM
fc8d0 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  utex();.}../*.**
fc8e0 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
fc8f0 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
fc900 63 61 63 68 65 2e 78 54 72 75 6e 63 61 74 65 20  cache.xTruncate 
fc910 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44  method. .**.** D
fc920 69 73 63 61 72 64 20 61 6c 6c 20 75 6e 70 69 6e  iscard all unpin
fc930 6e 65 64 20 70 61 67 65 73 20 69 6e 20 74 68 65  ned pages in the
fc940 20 63 61 63 68 65 20 77 69 74 68 20 61 20 70 61   cache with a pa
fc950 67 65 20 6e 75 6d 62 65 72 20 65 71 75 61 6c 20  ge number equal 
fc960 74 6f 0a 2a 2a 20 6f 72 20 67 72 65 61 74 65 72  to.** or greater
fc970 20 74 68 61 6e 20 70 61 72 61 6d 65 74 65 72 20   than parameter 
fc980 69 4c 69 6d 69 74 2e 20 41 6e 79 20 70 69 6e 6e  iLimit. Any pinn
fc990 65 64 20 70 61 67 65 73 20 77 69 74 68 20 61 20  ed pages with a 
fc9a0 70 61 67 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 65  page number.** e
fc9b0 71 75 61 6c 20 74 6f 20 6f 72 20 67 72 65 61 74  qual to or great
fc9c0 65 72 20 74 68 61 6e 20 69 4c 69 6d 69 74 20 61  er than iLimit a
fc9d0 72 65 20 69 6d 70 6c 69 63 69 74 6c 79 20 75 6e  re implicitly un
fc9e0 70 69 6e 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  pinned..*/.stati
fc9f0 63 20 76 6f 69 64 20 70 63 61 63 68 65 31 54 72  c void pcache1Tr
fca00 75 6e 63 61 74 65 28 73 71 6c 69 74 65 33 5f 70  uncate(sqlite3_p
fca10 63 61 63 68 65 20 2a 70 2c 20 75 6e 73 69 67 6e  cache *p, unsign
fca20 65 64 20 69 6e 74 20 69 4c 69 6d 69 74 29 7b 0a  ed int iLimit){.
fca30 20 20 50 43 61 63 68 65 31 20 2a 70 43 61 63 68    PCache1 *pCach
fca40 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a 29 70  e = (PCache1 *)p
fca50 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74 65 72  ;.  pcache1Enter
fca60 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61 63 68  Mutex();.  pcach
fca70 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61 66 65  e1TruncateUnsafe
fca80 28 70 43 61 63 68 65 2c 20 69 4c 69 6d 69 74 29  (pCache, iLimit)
fca90 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ;.  pcache1Leave
fcaa0 4d 75 74 65 78 28 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Mutex();.}../*.*
fcab0 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
fcac0 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f   of the sqlite3_
fcad0 70 63 61 63 68 65 2e 78 44 65 73 74 72 6f 79 20  pcache.xDestroy 
fcae0 6d 65 74 68 6f 64 2e 20 0a 2a 2a 0a 2a 2a 20 44  method. .**.** D
fcaf0 65 73 74 72 6f 79 20 61 20 63 61 63 68 65 20 61  estroy a cache a
fcb00 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 70  llocated using p
fcb10 63 61 63 68 65 31 43 72 65 61 74 65 28 29 2e 0a  cache1Create()..
fcb20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70  */.static void p
fcb30 63 61 63 68 65 31 44 65 73 74 72 6f 79 28 73 71  cache1Destroy(sq
fcb40 6c 69 74 65 33 5f 70 63 61 63 68 65 20 2a 70 29  lite3_pcache *p)
fcb50 7b 0a 20 20 50 43 61 63 68 65 31 20 2a 70 43 61  {.  PCache1 *pCa
fcb60 63 68 65 20 3d 20 28 50 43 61 63 68 65 31 20 2a  che = (PCache1 *
fcb70 29 70 3b 0a 20 20 70 63 61 63 68 65 31 45 6e 74  )p;.  pcache1Ent
fcb80 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 70 63 61  erMutex();.  pca
fcb90 63 68 65 31 54 72 75 6e 63 61 74 65 55 6e 73 61  che1TruncateUnsa
fcba0 66 65 28 70 43 61 63 68 65 2c 20 30 29 3b 0a 20  fe(pCache, 0);. 
fcbb0 20 70 63 61 63 68 65 31 2e 6e 4d 61 78 50 61 67   pcache1.nMaxPag
fcbc0 65 20 2d 3d 20 70 43 61 63 68 65 2d 3e 6e 4d 61  e -= pCache->nMa
fcbd0 78 3b 0a 20 20 70 63 61 63 68 65 31 2e 6e 4d 69  x;.  pcache1.nMi
fcbe0 6e 50 61 67 65 20 2d 3d 20 70 43 61 63 68 65 2d  nPage -= pCache-
fcbf0 3e 6e 4d 69 6e 3b 0a 20 20 70 63 61 63 68 65 31  >nMin;.  pcache1
fcc00 45 6e 66 6f 72 63 65 4d 61 78 50 61 67 65 28 29  EnforceMaxPage()
fcc10 3b 0a 20 20 70 63 61 63 68 65 31 4c 65 61 76 65  ;.  pcache1Leave
fcc20 4d 75 74 65 78 28 29 3b 0a 20 20 73 71 6c 69 74  Mutex();.  sqlit
fcc30 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 2d 3e  e3_free(pCache->
fcc40 61 70 48 61 73 68 29 3b 0a 20 20 73 71 6c 69 74  apHash);.  sqlit
fcc50 65 33 5f 66 72 65 65 28 70 43 61 63 68 65 29 3b  e3_free(pCache);
fcc60 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  .}../*.** This f
fcc70 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
fcc80 64 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c  d during initial
fcc90 69 7a 61 74 69 6f 6e 20 28 73 71 6c 69 74 65 33  ization (sqlite3
fcca0 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 29 20 74  _initialize()) t
fccb0 6f 0a 2a 2a 20 69 6e 73 74 61 6c 6c 20 74 68 65  o.** install the
fccc0 20 64 65 66 61 75 6c 74 20 70 6c 75 67 67 61 62   default pluggab
fccd0 6c 65 20 63 61 63 68 65 20 6d 6f 64 75 6c 65 2c  le cache module,
fcce0 20 61 73 73 75 6d 69 6e 67 20 74 68 65 20 75 73   assuming the us
fccf0 65 72 20 68 61 73 20 6e 6f 74 0a 2a 2a 20 61 6c  er has not.** al
fcd00 72 65 61 64 79 20 70 72 6f 76 69 64 65 64 20 61  ready provided a
fcd10 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 2e 0a 2a  n alternative..*
fcd20 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
fcd30 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 43 61   void sqlite3PCa
fcd40 63 68 65 53 65 74 44 65 66 61 75 6c 74 28 76 6f  cheSetDefault(vo
fcd50 69 64 29 7b 0a 20 20 73 74 61 74 69 63 20 73 71  id){.  static sq
fcd60 6c 69 74 65 33 5f 70 63 61 63 68 65 5f 6d 65 74  lite3_pcache_met
fcd70 68 6f 64 73 20 64 65 66 61 75 6c 74 4d 65 74 68  hods defaultMeth
fcd80 6f 64 73 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20  ods = {.    0,  
fcd90 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fcda0 20 20 20 20 20 2f 2a 20 70 41 72 67 20 2a 2f 0a       /* pArg */.
fcdb0 20 20 20 20 70 63 61 63 68 65 31 49 6e 69 74 2c      pcache1Init,
fcdc0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
fcdd0 78 49 6e 69 74 20 2a 2f 0a 20 20 20 20 70 63 61  xInit */.    pca
fcde0 63 68 65 31 53 68 75 74 64 6f 77 6e 2c 20 20 20  che1Shutdown,   
fcdf0 20 20 20 20 20 20 2f 2a 20 78 53 68 75 74 64 6f        /* xShutdo
fce00 77 6e 20 2a 2f 0a 20 20 20 20 70 63 61 63 68 65  wn */.    pcache
fce10 31 43 72 65 61 74 65 2c 20 20 20 20 20 20 20 20  1Create,        
fce20 20 20 20 2f 2a 20 78 43 72 65 61 74 65 20 2a 2f     /* xCreate */
fce30 0a 20 20 20 20 70 63 61 63 68 65 31 43 61 63 68  .    pcache1Cach
fce40 65 73 69 7a 65 2c 20 20 20 20 20 20 20 20 2f 2a  esize,        /*
fce50 20 78 43 61 63 68 65 73 69 7a 65 20 2a 2f 0a 20   xCachesize */. 
fce60 20 20 20 70 63 61 63 68 65 31 50 61 67 65 63 6f     pcache1Pageco
fce70 75 6e 74 2c 20 20 20 20 20 20 20 20 2f 2a 20 78  unt,        /* x
fce80 50 61 67 65 63 6f 75 6e 74 20 2a 2f 0a 20 20 20  Pagecount */.   
fce90 20 70 63 61 63 68 65 31 46 65 74 63 68 2c 20 20   pcache1Fetch,  
fcea0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 65            /* xFe
fceb0 74 63 68 20 2a 2f 0a 20 20 20 20 70 63 61 63 68  tch */.    pcach
fcec0 65 31 55 6e 70 69 6e 2c 20 20 20 20 20 20 20 20  e1Unpin,        
fced0 20 20 20 20 2f 2a 20 78 55 6e 70 69 6e 20 2a 2f      /* xUnpin */
fcee0 0a 20 20 20 20 70 63 61 63 68 65 31 52 65 6b 65  .    pcache1Reke
fcef0 79 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  y,            /*
fcf00 20 78 52 65 6b 65 79 20 2a 2f 0a 20 20 20 20 70   xRekey */.    p
fcf10 63 61 63 68 65 31 54 72 75 6e 63 61 74 65 2c 20  cache1Truncate, 
fcf20 20 20 20 20 20 20 20 20 2f 2a 20 78 54 72 75 6e          /* xTrun
fcf30 63 61 74 65 20 2a 2f 0a 20 20 20 20 70 63 61 63  cate */.    pcac
fcf40 68 65 31 44 65 73 74 72 6f 79 20 20 20 20 20 20  he1Destroy      
fcf50 20 20 20 20 20 2f 2a 20 78 44 65 73 74 72 6f 79       /* xDestroy
fcf60 20 2a 2f 0a 20 20 7d 3b 0a 20 20 73 71 6c 69 74   */.  };.  sqlit
fcf70 65 33 5f 63 6f 6e 66 69 67 28 53 51 4c 49 54 45  e3_config(SQLITE
fcf80 5f 43 4f 4e 46 49 47 5f 50 43 41 43 48 45 2c 20  _CONFIG_PCACHE, 
fcf90 26 64 65 66 61 75 6c 74 4d 65 74 68 6f 64 73 29  &defaultMethods)
fcfa0 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  ;.}..#ifdef SQLI
fcfb0 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59  TE_ENABLE_MEMORY
fcfc0 5f 4d 41 4e 41 47 45 4d 45 4e 54 0a 2f 2a 0a 2a  _MANAGEMENT./*.*
fcfd0 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
fcfe0 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 66 72 65  is called to fre
fcff0 65 20 73 75 70 65 72 66 6c 75 6f 75 73 20 64 79  e superfluous dy
fd000 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c 6f 63 61  namically alloca
fd010 74 65 64 20 6d 65 6d 6f 72 79 0a 2a 2a 20 68 65  ted memory.** he
fd020 6c 64 20 62 79 20 74 68 65 20 70 61 67 65 72 20  ld by the pager 
fd030 73 79 73 74 65 6d 2e 20 4d 65 6d 6f 72 79 20 69  system. Memory i
fd040 6e 20 75 73 65 20 62 79 20 61 6e 79 20 53 51 4c  n use by any SQL
fd050 69 74 65 20 70 61 67 65 72 20 61 6c 6c 6f 63 61  ite pager alloca
fd060 74 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 63 75  ted.** by the cu
fd070 72 72 65 6e 74 20 74 68 72 65 61 64 20 6d 61 79  rrent thread may
fd080 20 62 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65   be sqlite3_free
fd090 28 29 65 64 2e 0a 2a 2a 0a 2a 2a 20 6e 52 65 71  ()ed..**.** nReq
fd0a0 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   is the number o
fd0b0 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72  f bytes of memor
fd0c0 79 20 72 65 71 75 69 72 65 64 2e 20 4f 6e 63 65  y required. Once
fd0d0 20 74 68 69 73 20 6d 75 63 68 20 68 61 73 0a 2a   this much has.*
fd0e0 2a 20 62 65 65 6e 20 72 65 6c 65 61 73 65 64 2c  * been released,
fd0f0 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
fd100 74 75 72 6e 73 2e 20 54 68 65 20 72 65 74 75 72  turns. The retur
fd110 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20 74  n value is the t
fd120 6f 74 61 6c 20 6e 75 6d 62 65 72 20 0a 2a 2a 20  otal number .** 
fd130 6f 66 20 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f  of bytes of memo
fd140 72 79 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2f 0a  ry released..*/.
fd150 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
fd160 6e 74 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  nt sqlite3Pcache
fd170 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
fd180 74 20 6e 52 65 71 29 7b 0a 20 20 69 6e 74 20 6e  t nReq){.  int n
fd190 46 72 65 65 20 3d 20 30 3b 0a 20 20 69 66 28 20  Free = 0;.  if( 
fd1a0 70 63 61 63 68 65 31 2e 70 53 74 61 72 74 3d 3d  pcache1.pStart==
fd1b0 30 20 29 7b 0a 20 20 20 20 50 67 48 64 72 31 20  0 ){.    PgHdr1 
fd1c0 2a 70 3b 0a 20 20 20 20 70 63 61 63 68 65 31 45  *p;.    pcache1E
fd1d0 6e 74 65 72 4d 75 74 65 78 28 29 3b 0a 20 20 20  nterMutex();.   
fd1e0 20 77 68 69 6c 65 28 20 28 6e 52 65 71 3c 30 20   while( (nReq<0 
fd1f0 7c 7c 20 6e 46 72 65 65 3c 6e 52 65 71 29 20 26  || nFree<nReq) &
fd200 26 20 28 70 3d 70 63 61 63 68 65 31 2e 70 4c 72  & (p=pcache1.pLr
fd210 75 54 61 69 6c 29 20 29 7b 0a 20 20 20 20 20 20  uTail) ){.      
fd220 6e 46 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33  nFree += sqlite3
fd230 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 29 3b 0a 20  MallocSize(p);. 
fd240 20 20 20 20 20 70 63 61 63 68 65 31 50 69 6e 50       pcache1PinP
fd250 61 67 65 28 70 29 3b 0a 20 20 20 20 20 20 70 63  age(p);.      pc
fd260 61 63 68 65 31 52 65 6d 6f 76 65 46 72 6f 6d 48  ache1RemoveFromH
fd270 61 73 68 28 70 29 3b 0a 20 20 20 20 20 20 70 63  ash(p);.      pc
fd280 61 63 68 65 31 46 72 65 65 50 61 67 65 28 70 29  ache1FreePage(p)
fd290 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 63 61 63  ;.    }.    pcac
fd2a0 68 65 31 4c 65 61 76 65 4d 75 74 65 78 28 29 3b  he1LeaveMutex();
fd2b0 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 46  .  }.  return nF
fd2c0 72 65 65 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  ree;.}.#endif /*
fd2d0 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d   SQLITE_ENABLE_M
fd2e0 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
fd2f0 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49   */..#ifdef SQLI
fd300 54 45 5f 54 45 53 54 0a 2f 2a 0a 2a 2a 20 54 68  TE_TEST./*.** Th
fd310 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75  is function is u
fd320 73 65 64 20 62 79 20 74 65 73 74 20 70 72 6f 63  sed by test proc
fd330 65 64 75 72 65 73 20 74 6f 20 69 6e 73 70 65 63  edures to inspec
fd340 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
fd350 74 61 74 65 0a 2a 2a 20 6f 66 20 74 68 65 20 67  tate.** of the g
fd360 6c 6f 62 61 6c 20 63 61 63 68 65 2e 0a 2a 2f 0a  lobal cache..*/.
fd370 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
fd380 6f 69 64 20 73 71 6c 69 74 65 33 50 63 61 63 68  oid sqlite3Pcach
fd390 65 53 74 61 74 73 28 0a 20 20 69 6e 74 20 2a 70  eStats(.  int *p
fd3a0 6e 43 75 72 72 65 6e 74 2c 20 20 20 20 20 20 2f  nCurrent,      /
fd3b0 2a 20 4f 55 54 3a 20 54 6f 74 61 6c 20 6e 75 6d  * OUT: Total num
fd3c0 62 65 72 20 6f 66 20 70 61 67 65 73 20 63 61 63  ber of pages cac
fd3d0 68 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  hed */.  int *pn
fd3e0 4d 61 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Max,          /*
fd3f0 20 4f 55 54 3a 20 47 6c 6f 62 61 6c 20 6d 61 78   OUT: Global max
fd400 69 6d 75 6d 20 63 61 63 68 65 20 73 69 7a 65 20  imum cache size 
fd410 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 4d 69 6e 2c  */.  int *pnMin,
fd420 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54            /* OUT
fd430 3a 20 53 75 6d 20 6f 66 20 50 43 61 63 68 65 31  : Sum of PCache1
fd440 2e 6e 4d 69 6e 20 66 6f 72 20 70 75 72 67 65 61  .nMin for purgea
fd450 62 6c 65 20 63 61 63 68 65 73 20 2a 2f 0a 20 20  ble caches */.  
fd460 69 6e 74 20 2a 70 6e 52 65 63 79 63 6c 61 62 6c  int *pnRecyclabl
fd470 65 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54 6f 74  e    /* OUT: Tot
fd480 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67  al number of pag
fd490 65 73 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72  es available for
fd4a0 20 72 65 63 79 63 6c 69 6e 67 20 2a 2f 0a 29 7b   recycling */.){
fd4b0 0a 20 20 50 67 48 64 72 31 20 2a 70 3b 0a 20 20  .  PgHdr1 *p;.  
fd4c0 69 6e 74 20 6e 52 65 63 79 63 6c 61 62 6c 65 20  int nRecyclable 
fd4d0 3d 20 30 3b 0a 20 20 66 6f 72 28 70 3d 70 63 61  = 0;.  for(p=pca
fd4e0 63 68 65 31 2e 70 4c 72 75 48 65 61 64 3b 20 70  che1.pLruHead; p
fd4f0 3b 20 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29  ; p=p->pLruNext)
fd500 7b 0a 20 20 20 20 6e 52 65 63 79 63 6c 61 62 6c  {.    nRecyclabl
fd510 65 2b 2b 3b 0a 20 20 7d 0a 20 20 2a 70 6e 43 75  e++;.  }.  *pnCu
fd520 72 72 65 6e 74 20 3d 20 70 63 61 63 68 65 31 2e  rrent = pcache1.
fd530 6e 43 75 72 72 65 6e 74 50 61 67 65 3b 0a 20 20  nCurrentPage;.  
fd540 2a 70 6e 4d 61 78 20 3d 20 70 63 61 63 68 65 31  *pnMax = pcache1
fd550 2e 6e 4d 61 78 50 61 67 65 3b 0a 20 20 2a 70 6e  .nMaxPage;.  *pn
fd560 4d 69 6e 20 3d 20 70 63 61 63 68 65 31 2e 6e 4d  Min = pcache1.nM
fd570 69 6e 50 61 67 65 3b 0a 20 20 2a 70 6e 52 65 63  inPage;.  *pnRec
fd580 79 63 6c 61 62 6c 65 20 3d 20 6e 52 65 63 79 63  yclable = nRecyc
fd590 6c 61 62 6c 65 3b 0a 7d 0a 23 65 6e 64 69 66 0a  lable;.}.#endif.
fd5a0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
fd5b0 20 45 6e 64 20 6f 66 20 70 63 61 63 68 65 31 2e   End of pcache1.
fd5c0 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
fd5d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
fd5f0 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
fd600 20 42 65 67 69 6e 20 66 69 6c 65 20 70 61 67 65   Begin file page
fd610 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
fd620 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
fd640 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74  ./*.** 2001 Sept
fd650 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54  ember 15.**.** T
fd660 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
fd670 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
fd680 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
fd690 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
fd6a0 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
fd6b0 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
fd6c0 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
fd6d0 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
fd6e0 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
fd6f0 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
fd700 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
fd710 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
fd720 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
fd730 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
fd740 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
fd750 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
fd760 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
fd770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd780 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd790 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
fd7b0 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20  *******.** This 
fd7c0 69 73 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  is the implement
fd7d0 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 61 67  ation of the pag
fd7e0 65 20 63 61 63 68 65 20 73 75 62 73 79 73 74 65  e cache subsyste
fd7f0 6d 20 6f 72 20 22 70 61 67 65 72 22 2e 0a 2a 2a  m or "pager"..**
fd800 20 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 69   .** The pager i
fd810 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  s used to access
fd820 20 61 20 64 61 74 61 62 61 73 65 20 64 69 73 6b   a database disk
fd830 20 66 69 6c 65 2e 20 20 49 74 20 69 6d 70 6c 65   file.  It imple
fd840 6d 65 6e 74 73 0a 2a 2a 20 61 74 6f 6d 69 63 20  ments.** atomic 
fd850 63 6f 6d 6d 69 74 20 61 6e 64 20 72 6f 6c 6c 62  commit and rollb
fd860 61 63 6b 20 74 68 72 6f 75 67 68 20 74 68 65 20  ack through the 
fd870 75 73 65 20 6f 66 20 61 20 6a 6f 75 72 6e 61 6c  use of a journal
fd880 20 66 69 6c 65 20 74 68 61 74 0a 2a 2a 20 69 73   file that.** is
fd890 20 73 65 70 61 72 61 74 65 20 66 72 6f 6d 20 74   separate from t
fd8a0 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
fd8b0 2e 20 20 54 68 65 20 70 61 67 65 72 20 61 6c 73  .  The pager als
fd8c0 6f 20 69 6d 70 6c 65 6d 65 6e 74 73 20 66 69 6c  o implements fil
fd8d0 65 0a 2a 2a 20 6c 6f 63 6b 69 6e 67 20 74 6f 20  e.** locking to 
fd8e0 70 72 65 76 65 6e 74 20 74 77 6f 20 70 72 6f 63  prevent two proc
fd8f0 65 73 73 65 73 20 66 72 6f 6d 20 77 72 69 74 69  esses from writi
fd900 6e 67 20 74 68 65 20 73 61 6d 65 20 64 61 74 61  ng the same data
fd910 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 73 69 6d  base.** file sim
fd920 75 6c 74 61 6e 65 6f 75 73 6c 79 2c 20 6f 72 20  ultaneously, or 
fd930 6f 6e 65 20 70 72 6f 63 65 73 73 20 66 72 6f 6d  one process from
fd940 20 72 65 61 64 69 6e 67 20 74 68 65 20 64 61 74   reading the dat
fd950 61 62 61 73 65 20 77 68 69 6c 65 0a 2a 2a 20 61  abase while.** a
fd960 6e 6f 74 68 65 72 20 69 73 20 77 72 69 74 69 6e  nother is writin
fd970 67 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49  g..**.** @(#) $I
fd980 64 3a 20 70 61 67 65 72 2e 63 2c 76 20 31 2e 35  d: pager.c,v 1.5
fd990 30 36 20 32 30 30 38 2f 31 31 2f 31 39 20 31 38  06 2008/11/19 18
fd9a0 3a 33 30 3a 32 39 20 64 72 68 20 45 78 70 20 24  :30:29 drh Exp $
fd9b0 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
fd9c0 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 0a  TE_OMIT_DISKIO..
fd9d0 2f 2a 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72  /*.** Macros for
fd9e0 20 74 72 6f 75 62 6c 65 73 68 6f 6f 74 69 6e 67   troubleshooting
fd9f0 2e 20 20 4e 6f 72 6d 61 6c 6c 79 20 74 75 72 6e  .  Normally turn
fda00 65 64 20 6f 66 66 0a 2a 2f 0a 23 69 66 20 30 0a  ed off.*/.#if 0.
fda10 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 44  #define sqlite3D
fda20 65 62 75 67 50 72 69 6e 74 66 20 70 72 69 6e 74  ebugPrintf print
fda30 66 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54  f.#define PAGERT
fda40 52 41 43 45 31 28 58 29 20 20 20 20 20 20 20 73  RACE1(X)       s
fda50 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
fda60 66 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47  f(X).#define PAG
fda70 45 52 54 52 41 43 45 32 28 58 2c 59 29 20 20 20  ERTRACE2(X,Y)   
fda80 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
fda90 69 6e 74 66 28 58 2c 59 29 0a 23 64 65 66 69 6e  intf(X,Y).#defin
fdaa0 65 20 50 41 47 45 52 54 52 41 43 45 33 28 58 2c  e PAGERTRACE3(X,
fdab0 59 2c 5a 29 20 20 20 73 71 6c 69 74 65 33 44 65  Y,Z)   sqlite3De
fdac0 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c 5a 29  bugPrintf(X,Y,Z)
fdad0 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 54 52  .#define PAGERTR
fdae0 41 43 45 34 28 58 2c 59 2c 5a 2c 57 29 20 73 71  ACE4(X,Y,Z,W) sq
fdaf0 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
fdb00 28 58 2c 59 2c 5a 2c 57 29 0a 23 64 65 66 69 6e  (X,Y,Z,W).#defin
fdb10 65 20 50 41 47 45 52 54 52 41 43 45 35 28 58 2c  e PAGERTRACE5(X,
fdb20 59 2c 5a 2c 57 2c 56 29 20 73 71 6c 69 74 65 33  Y,Z,W,V) sqlite3
fdb30 44 65 62 75 67 50 72 69 6e 74 66 28 58 2c 59 2c  DebugPrintf(X,Y,
fdb40 5a 2c 57 2c 56 29 0a 23 65 6c 73 65 0a 23 64 65  Z,W,V).#else.#de
fdb50 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 31  fine PAGERTRACE1
fdb60 28 58 29 0a 23 64 65 66 69 6e 65 20 50 41 47 45  (X).#define PAGE
fdb70 52 54 52 41 43 45 32 28 58 2c 59 29 0a 23 64 65  RTRACE2(X,Y).#de
fdb80 66 69 6e 65 20 50 41 47 45 52 54 52 41 43 45 33  fine PAGERTRACE3
fdb90 28 58 2c 59 2c 5a 29 0a 23 64 65 66 69 6e 65 20  (X,Y,Z).#define 
fdba0 50 41 47 45 52 54 52 41 43 45 34 28 58 2c 59 2c  PAGERTRACE4(X,Y,
fdbb0 5a 2c 57 29 0a 23 64 65 66 69 6e 65 20 50 41 47  Z,W).#define PAG
fdbc0 45 52 54 52 41 43 45 35 28 58 2c 59 2c 5a 2c 57  ERTRACE5(X,Y,Z,W
fdbd0 2c 56 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ,V).#endif../*.*
fdbe0 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
fdbf0 74 77 6f 20 6d 61 63 72 6f 73 20 61 72 65 20 75  two macros are u
fdc00 73 65 64 20 77 69 74 68 69 6e 20 74 68 65 20 50  sed within the P
fdc10 41 47 45 52 54 52 41 43 45 58 28 29 20 6d 61 63  AGERTRACEX() mac
fdc20 72 6f 73 20 61 62 6f 76 65 0a 2a 2a 20 74 6f 20  ros above.** to 
fdc30 70 72 69 6e 74 20 6f 75 74 20 66 69 6c 65 2d 64  print out file-d
fdc40 65 73 63 72 69 70 74 6f 72 73 2e 20 0a 2a 2a 0a  escriptors. .**.
fdc50 2a 2a 20 50 41 47 45 52 49 44 28 29 20 74 61 6b  ** PAGERID() tak
fdc60 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  es a pointer to 
fdc70 61 20 50 61 67 65 72 20 73 74 72 75 63 74 20 61  a Pager struct a
fdc80 73 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 20  s its argument. 
fdc90 54 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74 65  The.** associate
fdca0 64 20 66 69 6c 65 2d 64 65 73 63 72 69 70 74 6f  d file-descripto
fdcb0 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 46  r is returned. F
fdcc0 49 4c 45 48 41 4e 44 4c 45 49 44 28 29 20 74 61  ILEHANDLEID() ta
fdcd0 6b 65 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 66  kes an sqlite3_f
fdce0 69 6c 65 0a 2a 2a 20 73 74 72 75 63 74 20 61 73  ile.** struct as
fdcf0 20 69 74 73 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   its argument..*
fdd00 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 49  /.#define PAGERI
fdd10 44 28 70 29 20 28 28 69 6e 74 29 28 70 2d 3e 66  D(p) ((int)(p->f
fdd20 64 29 29 0a 23 64 65 66 69 6e 65 20 46 49 4c 45  d)).#define FILE
fdd30 48 41 4e 44 4c 45 49 44 28 66 64 29 20 28 28 69  HANDLEID(fd) ((i
fdd40 6e 74 29 66 64 29 0a 0a 2f 2a 0a 2a 2a 20 54 68  nt)fd)../*.** Th
fdd50 65 20 70 61 67 65 20 63 61 63 68 65 20 61 73 20  e page cache as 
fdd60 61 20 77 68 6f 6c 65 20 69 73 20 61 6c 77 61 79  a whole is alway
fdd70 73 20 69 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20  s in one of the 
fdd80 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 73 74 61  following.** sta
fdd90 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47  tes:.**.**   PAG
fdda0 45 52 5f 55 4e 4c 4f 43 4b 20 20 20 20 20 20 20  ER_UNLOCK       
fddb0 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
fddc0 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79  is not currently
fddd0 20 72 65 61 64 69 6e 67 20 6f 72 20 0a 2a 2a 20   reading or .** 
fdde0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fddf0 20 20 20 20 20 20 77 72 69 74 69 6e 67 20 74 68        writing th
fde00 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  e database file.
fde10 20 20 54 68 65 72 65 20 69 73 20 6e 6f 0a 2a 2a    There is no.**
fde20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fde30 20 20 20 20 20 20 20 64 61 74 61 20 68 65 6c 64         data held
fde40 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20 54 68 69   in memory.  Thi
fde50 73 20 69 73 20 74 68 65 20 69 6e 69 74 69 61 6c  s is the initial
fde60 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
fde70 20 20 20 20 20 20 20 20 20 20 73 74 61 74 65 2e            state.
fde80 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53  .**.**   PAGER_S
fde90 48 41 52 45 44 20 20 20 20 20 20 20 20 54 68 65  HARED        The
fdea0 20 70 61 67 65 20 63 61 63 68 65 20 69 73 20 72   page cache is r
fdeb0 65 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 62  eading the datab
fdec0 61 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20  ase..**         
fded0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 57 72                Wr
fdee0 69 74 69 6e 67 20 69 73 20 6e 6f 74 20 70 65 72  iting is not per
fdef0 6d 69 74 74 65 64 2e 20 20 54 68 65 72 65 20 63  mitted.  There c
fdf00 61 6e 20 62 65 0a 2a 2a 20 20 20 20 20 20 20 20  an be.**        
fdf10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
fdf20 75 6c 74 69 70 6c 65 20 72 65 61 64 65 72 73 20  ultiple readers 
fdf30 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 73 61  accessing the sa
fdf40 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 20  me database.**  
fdf50 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fdf60 20 20 20 20 20 66 69 6c 65 20 61 74 20 74 68 65       file at the
fdf70 20 73 61 6d 65 20 74 69 6d 65 2e 0a 2a 2a 0a 2a   same time..**.*
fdf80 2a 20 20 20 50 41 47 45 52 5f 52 45 53 45 52 56  *   PAGER_RESERV
fdf90 45 44 20 20 20 20 20 20 54 68 69 73 20 70 72 6f  ED      This pro
fdfa0 63 65 73 73 20 68 61 73 20 72 65 73 65 72 76 65  cess has reserve
fdfb0 64 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  d the database f
fdfc0 6f 72 20 77 72 69 74 69 6e 67 0a 2a 2a 20 20 20  or writing.**   
fdfd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fdfe0 20 20 20 20 62 75 74 20 68 61 73 20 6e 6f 74 20      but has not 
fdff0 79 65 74 20 6d 61 64 65 20 61 6e 79 20 63 68 61  yet made any cha
fe000 6e 67 65 73 2e 20 20 4f 6e 6c 79 20 6f 6e 65 20  nges.  Only one 
fe010 70 72 6f 63 65 73 73 0a 2a 2a 20 20 20 20 20 20  process.**      
fe020 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe030 20 61 74 20 61 20 74 69 6d 65 20 63 61 6e 20 72   at a time can r
fe040 65 73 65 72 76 65 20 74 68 65 20 64 61 74 61 62  eserve the datab
fe050 61 73 65 2e 20 20 54 68 65 20 6f 72 69 67 69 6e  ase.  The origin
fe060 61 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  al.**           
fe070 20 20 20 20 20 20 20 20 20 20 20 20 64 61 74 61              data
fe080 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 6e 6f  base file has no
fe090 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  t been modified 
fe0a0 73 6f 20 6f 74 68 65 72 0a 2a 2a 20 20 20 20 20  so other.**     
fe0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe0c0 20 20 70 72 6f 63 65 73 73 65 73 20 6d 61 79 20    processes may 
fe0d0 73 74 69 6c 6c 20 62 65 20 72 65 61 64 69 6e 67  still be reading
fe0e0 20 74 68 65 20 6f 6e 2d 64 69 73 6b 0a 2a 2a 20   the on-disk.** 
fe0f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe100 20 20 20 20 20 20 64 61 74 61 62 61 73 65 20 66        database f
fe110 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 50 41 47  ile..**.**   PAG
fe120 45 52 5f 45 58 43 4c 55 53 49 56 45 20 20 20 20  ER_EXCLUSIVE    
fe130 20 54 68 65 20 70 61 67 65 20 63 61 63 68 65 20   The page cache 
fe140 69 73 20 77 72 69 74 69 6e 67 20 74 68 65 20 64  is writing the d
fe150 61 74 61 62 61 73 65 2e 0a 2a 2a 20 20 20 20 20  atabase..**     
fe160 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe170 20 20 41 63 63 65 73 73 20 69 73 20 65 78 63 6c    Access is excl
fe180 75 73 69 76 65 2e 20 20 4e 6f 20 6f 74 68 65 72  usive.  No other
fe190 20 70 72 6f 63 65 73 73 65 73 20 6f 72 0a 2a 2a   processes or.**
fe1a0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe1b0 20 20 20 20 20 20 20 74 68 72 65 61 64 73 20 63         threads c
fe1c0 61 6e 20 62 65 20 72 65 61 64 69 6e 67 20 6f 72  an be reading or
fe1d0 20 77 72 69 74 69 6e 67 20 77 68 69 6c 65 20 6f   writing while o
fe1e0 6e 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ne.**           
fe1f0 20 20 20 20 20 20 20 20 20 20 20 20 70 72 6f 63              proc
fe200 65 73 73 20 69 73 20 77 72 69 74 69 6e 67 2e 0a  ess is writing..
fe210 2a 2a 0a 2a 2a 20 20 20 50 41 47 45 52 5f 53 59  **.**   PAGER_SY
fe220 4e 43 45 44 20 20 20 20 20 20 20 20 54 68 65 20  NCED        The 
fe230 70 61 67 65 72 20 6d 6f 76 65 73 20 74 6f 20 74  pager moves to t
fe240 68 69 73 20 73 74 61 74 65 20 66 72 6f 6d 20 50  his state from P
fe250 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 0a 2a  AGER_EXCLUSIVE.*
fe260 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
fe270 20 20 20 20 20 20 20 20 61 66 74 65 72 20 61 6c          after al
fe280 6c 20 64 69 72 74 79 20 70 61 67 65 73 20 68 61  l dirty pages ha
fe290 76 65 20 62 65 65 6e 20 77 72 69 74 74 65 6e 20  ve been written 
fe2a0 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  to the.**       
fe2b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe2c0 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
fe2d0 64 20 74 68 65 20 66 69 6c 65 20 68 61 73 20 62  d the file has b
fe2e0 65 65 6e 20 73 79 6e 63 65 64 20 74 6f 0a 2a 2a  een synced to.**
fe2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe300 20 20 20 20 20 20 20 64 69 73 6b 2e 20 41 6c 6c         disk. All
fe310 20 74 68 61 74 20 72 65 6d 61 69 6e 73 20 74 6f   that remains to
fe320 20 64 6f 20 69 73 20 74 6f 20 72 65 6d 6f 76 65   do is to remove
fe330 20 6f 72 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   or.**          
fe340 20 20 20 20 20 20 20 20 20 20 20 20 20 74 72 75               tru
fe350 6e 63 61 74 65 20 74 68 65 20 6a 6f 75 72 6e 61  ncate the journa
fe360 6c 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20 74  l file and the t
fe370 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a 2a 20 20  ransaction .**  
fe380 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
fe390 20 20 20 20 20 77 69 6c 6c 20 62 65 20 63 6f 6d       will be com
fe3a0 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  mitted..**.** Th
fe3b0 65 20 70 61 67 65 20 63 61 63 68 65 20 63 6f 6d  e page cache com
fe3c0 65 73 20 75 70 20 69 6e 20 50 41 47 45 52 5f 55  es up in PAGER_U
fe3d0 4e 4c 4f 43 4b 2e 20 20 54 68 65 20 66 69 72 73  NLOCK.  The firs
fe3e0 74 20 74 69 6d 65 20 61 0a 2a 2a 20 73 71 6c 69  t time a.** sqli
fe3f0 74 65 33 50 61 67 65 72 47 65 74 28 29 20 6f 63  te3PagerGet() oc
fe400 63 75 72 73 2c 20 74 68 65 20 73 74 61 74 65 20  curs, the state 
fe410 74 72 61 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50  transitions to P
fe420 41 47 45 52 5f 53 48 41 52 45 44 2e 0a 2a 2a 20  AGER_SHARED..** 
fe430 41 66 74 65 72 20 61 6c 6c 20 70 61 67 65 73 20  After all pages 
fe440 68 61 76 65 20 62 65 65 6e 20 72 65 6c 65 61 73  have been releas
fe450 65 64 20 75 73 69 6e 67 20 73 71 6c 69 74 65 5f  ed using sqlite_
fe460 70 61 67 65 5f 75 6e 72 65 66 28 29 2c 0a 2a 2a  page_unref(),.**
fe470 20 74 68 65 20 73 74 61 74 65 20 74 72 61 6e 73   the state trans
fe480 69 74 69 6f 6e 73 20 62 61 63 6b 20 74 6f 20 50  itions back to P
fe490 41 47 45 52 5f 55 4e 4c 4f 43 4b 2e 20 20 54 68  AGER_UNLOCK.  Th
fe4a0 65 20 66 69 72 73 74 20 74 69 6d 65 0a 2a 2a 20  e first time.** 
fe4b0 74 68 61 74 20 73 71 6c 69 74 65 33 50 61 67 65  that sqlite3Page
fe4c0 72 57 72 69 74 65 28 29 20 69 73 20 63 61 6c 6c  rWrite() is call
fe4d0 65 64 2c 20 74 68 65 20 73 74 61 74 65 20 74 72  ed, the state tr
fe4e0 61 6e 73 69 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20  ansitions to.** 
fe4f0 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 2e 20  PAGER_RESERVED. 
fe500 20 28 4e 6f 74 65 20 74 68 61 74 20 73 71 6c 69   (Note that sqli
fe510 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29 20  te3PagerWrite() 
fe520 63 61 6e 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 63  can only be.** c
fe530 61 6c 6c 65 64 20 6f 6e 20 61 6e 20 6f 75 74 73  alled on an outs
fe540 74 61 6e 64 69 6e 67 20 70 61 67 65 20 77 68 69  tanding page whi
fe550 63 68 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68  ch means that th
fe560 65 20 70 61 67 65 72 20 6d 75 73 74 0a 2a 2a 20  e pager must.** 
fe570 62 65 20 69 6e 20 50 41 47 45 52 5f 53 48 41 52  be in PAGER_SHAR
fe580 45 44 20 62 65 66 6f 72 65 20 69 74 20 74 72 61  ED before it tra
fe590 6e 73 69 74 69 6f 6e 73 20 74 6f 20 50 41 47 45  nsitions to PAGE
fe5a0 52 5f 52 45 53 45 52 56 45 44 2e 29 0a 2a 2a 20  R_RESERVED.).** 
fe5b0 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 6d  PAGER_RESERVED m
fe5c0 65 61 6e 73 20 74 68 61 74 20 74 68 65 72 65 20  eans that there 
fe5d0 69 73 20 61 6e 20 6f 70 65 6e 20 72 6f 6c 6c 62  is an open rollb
fe5e0 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20  ack journal..** 
fe5f0 54 68 65 20 74 72 61 6e 73 69 74 69 6f 6e 20 74  The transition t
fe600 6f 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  o PAGER_EXCLUSIV
fe610 45 20 6f 63 63 75 72 73 20 62 65 66 6f 72 65 20  E occurs before 
fe620 61 6e 79 20 63 68 61 6e 67 65 73 0a 2a 2a 20 61  any changes.** a
fe630 72 65 20 6d 61 64 65 20 74 6f 20 74 68 65 20 64  re made to the d
fe640 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
fe650 6f 75 67 68 20 77 72 69 74 65 73 20 74 6f 20 74  ough writes to t
fe660 68 65 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6a  he rollback.** j
fe670 6f 75 72 6e 61 6c 20 6f 63 63 75 72 73 20 77 69  ournal occurs wi
fe680 74 68 20 6a 75 73 74 20 50 41 47 45 52 5f 52 45  th just PAGER_RE
fe690 53 45 52 56 45 44 2e 20 20 41 66 74 65 72 20 61  SERVED.  After a
fe6a0 6e 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f  n sqlite3PagerRo
fe6b0 6c 6c 62 61 63 6b 28 29 0a 2a 2a 20 6f 72 20 73  llback().** or s
fe6c0 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69  qlite3PagerCommi
fe6d0 74 50 68 61 73 65 54 77 6f 28 29 2c 20 74 68 65  tPhaseTwo(), the
fe6e0 20 73 74 61 74 65 20 63 61 6e 20 67 6f 20 62 61   state can go ba
fe6f0 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52  ck to PAGER_SHAR
fe700 45 44 2c 0a 2a 2a 20 6f 72 20 69 74 20 63 61 6e  ED,.** or it can
fe710 20 73 74 61 79 20 61 74 20 50 41 47 45 52 5f 45   stay at PAGER_E
fe720 58 43 4c 55 53 49 56 45 20 69 66 20 77 65 20 61  XCLUSIVE if we a
fe730 72 65 20 69 6e 20 65 78 63 6c 75 73 69 76 65 20  re in exclusive 
fe740 61 63 63 65 73 73 20 6d 6f 64 65 2e 0a 2a 2f 0a  access mode..*/.
fe750 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f 55 4e  #define PAGER_UN
fe760 4c 4f 43 4b 20 20 20 20 20 20 30 0a 23 64 65 66  LOCK      0.#def
fe770 69 6e 65 20 50 41 47 45 52 5f 53 48 41 52 45 44  ine PAGER_SHARED
fe780 20 20 20 20 20 20 31 20 20 20 2f 2a 20 73 61 6d        1   /* sam
fe790 65 20 61 73 20 53 48 41 52 45 44 5f 4c 4f 43 4b  e as SHARED_LOCK
fe7a0 20 2a 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45   */.#define PAGE
fe7b0 52 5f 52 45 53 45 52 56 45 44 20 20 20 20 32 20  R_RESERVED    2 
fe7c0 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 52 45 53    /* same as RES
fe7d0 45 52 56 45 44 5f 4c 4f 43 4b 20 2a 2f 0a 23 64  ERVED_LOCK */.#d
fe7e0 65 66 69 6e 65 20 50 41 47 45 52 5f 45 58 43 4c  efine PAGER_EXCL
fe7f0 55 53 49 56 45 20 20 20 34 20 20 20 2f 2a 20 73  USIVE   4   /* s
fe800 61 6d 65 20 61 73 20 45 58 43 4c 55 53 49 56 45  ame as EXCLUSIVE
fe810 5f 4c 4f 43 4b 20 2a 2f 0a 23 64 65 66 69 6e 65  _LOCK */.#define
fe820 20 50 41 47 45 52 5f 53 59 4e 43 45 44 20 20 20   PAGER_SYNCED   
fe830 20 20 20 35 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74     5../*.** If t
fe840 68 65 20 53 51 4c 49 54 45 5f 42 55 53 59 5f 52  he SQLITE_BUSY_R
fe850 45 53 45 52 56 45 44 5f 4c 4f 43 4b 20 6d 61 63  ESERVED_LOCK mac
fe860 72 6f 20 69 73 20 73 65 74 20 74 6f 20 74 72 75  ro is set to tru
fe870 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d  e at compile-tim
fe880 65 2c 0a 2a 2a 20 74 68 65 6e 20 66 61 69 6c 65  e,.** then faile
fe890 64 20 61 74 74 65 6d 70 74 73 20 74 6f 20 67 65  d attempts to ge
fe8a0 74 20 61 20 72 65 73 65 72 76 65 64 20 6c 6f 63  t a reserved loc
fe8b0 6b 20 77 69 6c 6c 20 69 6e 76 6f 6b 65 20 74 68  k will invoke th
fe8c0 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 2e  e busy callback.
fe8d0 0a 2a 2a 20 54 68 69 73 20 69 73 20 6f 66 66 20  .** This is off 
fe8e0 62 79 20 64 65 66 61 75 6c 74 2e 20 20 54 6f 20  by default.  To 
fe8f0 73 65 65 20 77 68 79 2c 20 63 6f 6e 73 69 64 65  see why, conside
fe900 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  r the following 
fe910 73 63 65 6e 61 72 69 6f 3a 0a 2a 2a 20 0a 2a 2a  scenario:.** .**
fe920 20 53 75 70 70 6f 73 65 20 74 68 72 65 61 64 20   Suppose thread 
fe930 41 20 61 6c 72 65 61 64 79 20 68 61 73 20 61 20  A already has a 
fe940 73 68 61 72 65 64 20 6c 6f 63 6b 20 61 6e 64 20  shared lock and 
fe950 77 61 6e 74 73 20 61 20 72 65 73 65 72 76 65 64  wants a reserved
fe960 20 6c 6f 63 6b 2e 0a 2a 2a 20 54 68 72 65 61 64   lock..** Thread
fe970 20 42 20 61 6c 72 65 61 64 79 20 68 61 73 20 61   B already has a
fe980 20 72 65 73 65 72 76 65 64 20 6c 6f 63 6b 20 61   reserved lock a
fe990 6e 64 20 77 61 6e 74 73 20 61 6e 20 65 78 63 6c  nd wants an excl
fe9a0 75 73 69 76 65 20 6c 6f 63 6b 2e 20 20 49 66 0a  usive lock.  If.
fe9b0 2a 2a 20 62 6f 74 68 20 74 68 72 65 61 64 73 20  ** both threads 
fe9c0 61 72 65 20 75 73 69 6e 67 20 74 68 65 69 72 20  are using their 
fe9d0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 73 2c 20  busy callbacks, 
fe9e0 69 74 20 6d 69 67 68 74 20 62 65 20 61 20 6c 6f  it might be a lo
fe9f0 6e 67 20 74 69 6d 65 0a 2a 2a 20 62 65 20 66 6f  ng time.** be fo
fea00 72 20 6f 6e 65 20 6f 66 20 74 68 65 20 74 68 72  r one of the thr
fea10 65 61 64 73 20 67 69 76 65 20 75 70 20 61 6e 64  eads give up and
fea20 20 61 6c 6c 6f 77 73 20 74 68 65 20 6f 74 68 65   allows the othe
fea30 72 20 74 6f 20 70 72 6f 63 65 65 64 2e 0a 2a 2a  r to proceed..**
fea40 20 42 75 74 20 69 66 20 74 68 65 20 74 68 72 65   But if the thre
fea50 61 64 20 74 72 79 69 6e 67 20 74 6f 20 67 65 74  ad trying to get
fea60 20 74 68 65 20 72 65 73 65 72 76 65 64 20 6c 6f   the reserved lo
fea70 63 6b 20 67 69 76 65 73 20 75 70 20 71 75 69 63  ck gives up quic
fea80 6b 6c 79 0a 2a 2a 20 28 69 66 20 69 74 20 6e 65  kly.** (if it ne
fea90 76 65 72 20 69 6e 76 6f 6b 65 73 20 69 74 73 20  ver invokes its 
feaa0 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 29 20 74  busy callback) t
feab0 68 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 69  hen the contenti
feac0 6f 6e 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 72 65  on will be.** re
fead0 73 6f 6c 76 65 64 20 71 75 69 63 6b 6c 79 2e 0a  solved quickly..
feae0 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
feaf0 45 5f 42 55 53 59 5f 52 45 53 45 52 56 45 44 5f  E_BUSY_RESERVED_
feb00 4c 4f 43 4b 0a 23 20 64 65 66 69 6e 65 20 53 51  LOCK.# define SQ
feb10 4c 49 54 45 5f 42 55 53 59 5f 52 45 53 45 52 56  LITE_BUSY_RESERV
feb20 45 44 5f 4c 4f 43 4b 20 30 0a 23 65 6e 64 69 66  ED_LOCK 0.#endif
feb30 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63  ../*.** This mac
feb40 72 6f 20 72 6f 75 6e 64 73 20 76 61 6c 75 65 73  ro rounds values
feb50 20 75 70 20 73 6f 20 74 68 61 74 20 69 66 20 74   up so that if t
feb60 68 65 20 76 61 6c 75 65 20 69 73 20 61 6e 20 61  he value is an a
feb70 64 64 72 65 73 73 20 69 74 0a 2a 2a 20 69 73 20  ddress it.** is 
feb80 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 62 65  guaranteed to be
feb90 20 61 6e 20 61 64 64 72 65 73 73 20 74 68 61 74   an address that
feba0 20 69 73 20 61 6c 69 67 6e 65 64 20 74 6f 20 61   is aligned to a
febb0 6e 20 38 2d 62 79 74 65 20 62 6f 75 6e 64 61 72  n 8-byte boundar
febc0 79 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46 4f  y..*/.#define FO
febd0 52 43 45 5f 41 4c 49 47 4e 4d 45 4e 54 28 58 29  RCE_ALIGNMENT(X)
febe0 20 20 20 28 28 28 58 29 2b 37 29 26 7e 37 29 0a     (((X)+7)&~7).
febf0 0a 2f 2a 0a 2a 2a 20 41 20 6d 61 63 72 6f 20 75  ./*.** A macro u
fec00 73 65 64 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67  sed for invoking
fec10 20 74 68 65 20 63 6f 64 65 63 20 69 66 20 74 68   the codec if th
fec20 65 72 65 20 69 73 20 6f 6e 65 0a 2a 2f 0a 23 69  ere is one.*/.#i
fec30 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53 5f  fdef SQLITE_HAS_
fec40 43 4f 44 45 43 0a 23 20 64 65 66 69 6e 65 20 43  CODEC.# define C
fec50 4f 44 45 43 31 28 50 2c 44 2c 4e 2c 58 29 20 69  ODEC1(P,D,N,X) i
fec60 66 28 20 50 2d 3e 78 43 6f 64 65 63 21 3d 30 20  f( P->xCodec!=0 
fec70 29 7b 20 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e  ){ P->xCodec(P->
fec80 70 43 6f 64 65 63 41 72 67 2c 44 2c 4e 2c 58 29  pCodecArg,D,N,X)
fec90 3b 20 7d 0a 23 20 64 65 66 69 6e 65 20 43 4f 44  ; }.# define COD
feca0 45 43 32 28 50 2c 44 2c 4e 2c 58 29 20 28 28 63  EC2(P,D,N,X) ((c
fecb0 68 61 72 2a 29 28 50 2d 3e 78 43 6f 64 65 63 21  har*)(P->xCodec!
fecc0 3d 30 3f 50 2d 3e 78 43 6f 64 65 63 28 50 2d 3e  =0?P->xCodec(P->
fecd0 70 43 6f 64 65 63 41 72 67 2c 44 2c 4e 2c 58 29  pCodecArg,D,N,X)
fece0 3a 44 29 29 0a 23 65 6c 73 65 0a 23 20 64 65 66  :D)).#else.# def
fecf0 69 6e 65 20 43 4f 44 45 43 31 28 50 2c 44 2c 4e  ine CODEC1(P,D,N
fed00 2c 58 29 20 2f 2a 20 4e 4f 2d 4f 50 20 2a 2f 0a  ,X) /* NO-OP */.
fed10 23 20 64 65 66 69 6e 65 20 43 4f 44 45 43 32 28  # define CODEC2(
fed20 50 2c 44 2c 4e 2c 58 29 20 28 28 63 68 61 72 2a  P,D,N,X) ((char*
fed30 29 44 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  )D).#endif../*.*
fed40 2a 20 41 20 6f 70 65 6e 20 70 61 67 65 20 63 61  * A open page ca
fed50 63 68 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e  che is an instan
fed60 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ce of the follow
fed70 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  ing structure..*
fed80 2a 0a 2a 2a 20 50 61 67 65 72 2e 65 72 72 43 6f  *.** Pager.errCo
fed90 64 65 20 6d 61 79 20 62 65 20 73 65 74 20 74 6f  de may be set to
feda0 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c 20 53   SQLITE_IOERR, S
fedb0 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c 20 6f  QLITE_CORRUPT, o
fedc0 72 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 46  r.** or SQLITE_F
fedd0 55 4c 4c 2e 20 4f 6e 63 65 20 6f 6e 65 20 6f 66  ULL. Once one of
fede0 20 74 68 65 20 66 69 72 73 74 20 74 68 72 65 65   the first three
fedf0 20 65 72 72 6f 72 73 20 6f 63 63 75 72 73 2c 20   errors occurs, 
fee00 69 74 20 70 65 72 73 69 73 74 73 0a 2a 2a 20 61  it persists.** a
fee10 6e 64 20 69 73 20 72 65 74 75 72 6e 65 64 20 61  nd is returned a
fee20 73 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  s the result of 
fee30 65 76 65 72 79 20 6d 61 6a 6f 72 20 70 61 67 65  every major page
fee40 72 20 41 50 49 20 63 61 6c 6c 2e 20 20 54 68 65  r API call.  The
fee50 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  .** SQLITE_FULL 
fee60 72 65 74 75 72 6e 20 63 6f 64 65 20 69 73 20 73  return code is s
fee70 6c 69 67 68 74 6c 79 20 64 69 66 66 65 72 65 6e  lightly differen
fee80 74 2e 20 49 74 20 70 65 72 73 69 73 74 73 20 6f  t. It persists o
fee90 6e 6c 79 20 75 6e 74 69 6c 20 74 68 65 0a 2a 2a  nly until the.**
feea0 20 6e 65 78 74 20 73 75 63 63 65 73 73 66 75 6c   next successful
feeb0 20 72 6f 6c 6c 62 61 63 6b 20 69 73 20 70 65 72   rollback is per
feec0 66 6f 72 6d 65 64 20 6f 6e 20 74 68 65 20 70 61  formed on the pa
feed0 67 65 72 20 63 61 63 68 65 2e 20 41 6c 73 6f 2c  ger cache. Also,
feee0 0a 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4c 4c 20  .** SQLITE_FULL 
feef0 64 6f 65 73 20 6e 6f 74 20 61 66 66 65 63 74 20  does not affect 
fef00 74 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72  the sqlite3Pager
fef10 47 65 74 28 29 20 61 6e 64 20 73 71 6c 69 74 65  Get() and sqlite
fef20 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 29 0a 2a  3PagerLookup().*
fef30 2a 20 41 50 49 73 2c 20 74 68 65 79 20 6d 61 79  * APIs, they may
fef40 20 73 74 69 6c 6c 20 62 65 20 75 73 65 64 20 73   still be used s
fef50 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a 2a 2f 0a  uccessfully..*/.
fef60 73 74 72 75 63 74 20 50 61 67 65 72 20 7b 0a 20  struct Pager {. 
fef70 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56   sqlite3_vfs *pV
fef80 66 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  fs;          /* 
fef90 4f 53 20 66 75 6e 63 74 69 6f 6e 73 20 74 6f 20  OS functions to 
fefa0 75 73 65 20 66 6f 72 20 49 4f 20 2a 2f 0a 20 20  use for IO */.  
fefb0 75 38 20 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3b 20  u8 journalOpen; 
fefc0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
fefd0 72 75 65 20 69 66 20 6a 6f 75 72 6e 61 6c 20 66  rue if journal f
fefe0 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 73 20  ile descriptors 
feff0 69 73 20 76 61 6c 69 64 20 2a 2f 0a 20 20 75 38  is valid */.  u8
ff000 20 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 3b   journalStarted;
ff010 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
ff020 65 20 69 66 20 68 65 61 64 65 72 20 6f 66 20 6a  e if header of j
ff030 6f 75 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64  ournal is synced
ff040 20 2a 2f 0a 20 20 75 38 20 75 73 65 4a 6f 75 72   */.  u8 useJour
ff050 6e 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  nal;            
ff060 20 20 2f 2a 20 55 73 65 20 61 20 72 6f 6c 6c 62    /* Use a rollb
ff070 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20 74  ack journal on t
ff080 68 69 73 20 66 69 6c 65 20 2a 2f 0a 20 20 75 38  his file */.  u8
ff090 20 6e 6f 52 65 61 64 6c 6f 63 6b 3b 20 20 20 20   noReadlock;    
ff0a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 6f 20            /* Do 
ff0b0 6e 6f 74 20 62 6f 74 68 65 72 20 74 6f 20 6f 62  not bother to ob
ff0c0 74 61 69 6e 20 72 65 61 64 6c 6f 63 6b 73 20 2a  tain readlocks *
ff0d0 2f 0a 20 20 75 38 20 73 74 6d 74 4f 70 65 6e 3b  /.  u8 stmtOpen;
ff0e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff0f0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 73  /* True if the s
ff100 74 61 74 65 6d 65 6e 74 20 73 75 62 6a 6f 75 72  tatement subjour
ff110 6e 61 6c 20 69 73 20 6f 70 65 6e 20 2a 2f 0a 20  nal is open */. 
ff120 20 75 38 20 73 74 6d 74 49 6e 55 73 65 3b 20 20   u8 stmtInUse;  
ff130 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ff140 54 72 75 65 20 77 65 20 61 72 65 20 69 6e 20 61  True we are in a
ff150 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72   statement subtr
ff160 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75  ansaction */.  u
ff170 38 20 73 74 6d 74 41 75 74 6f 6f 70 65 6e 3b 20  8 stmtAutoopen; 
ff180 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 70             /* Op
ff190 65 6e 20 73 74 6d 74 20 6a 6f 75 72 6e 61 6c 20  en stmt journal 
ff1a0 77 68 65 6e 20 6d 61 69 6e 20 6a 6f 75 72 6e 61  when main journa
ff1b0 6c 20 69 73 20 6f 70 65 6e 65 64 2a 2f 0a 20 20  l is opened*/.  
ff1c0 75 38 20 6e 6f 53 79 6e 63 3b 20 20 20 20 20 20  u8 noSync;      
ff1d0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
ff1e0 6f 20 6e 6f 74 20 73 79 6e 63 20 74 68 65 20 6a  o not sync the j
ff1f0 6f 75 72 6e 61 6c 20 69 66 20 74 72 75 65 20 2a  ournal if true *
ff200 2f 0a 20 20 75 38 20 66 75 6c 6c 53 79 6e 63 3b  /.  u8 fullSync;
ff210 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff220 2f 2a 20 44 6f 20 65 78 74 72 61 20 73 79 6e 63  /* Do extra sync
ff230 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s of the journal
ff240 20 66 6f 72 20 72 6f 62 75 73 74 6e 65 73 73 20   for robustness 
ff250 2a 2f 0a 20 20 75 38 20 73 79 6e 63 5f 66 6c 61  */.  u8 sync_fla
ff260 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
ff270 20 2f 2a 20 4f 6e 65 20 6f 66 20 53 59 4e 43 5f   /* One of SYNC_
ff280 4e 4f 52 4d 41 4c 20 6f 72 20 53 59 4e 43 5f 46  NORMAL or SYNC_F
ff290 55 4c 4c 20 2a 2f 0a 20 20 75 38 20 73 74 61 74  ULL */.  u8 stat
ff2a0 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
ff2b0 20 20 20 20 20 2f 2a 20 50 41 47 45 52 5f 55 4e       /* PAGER_UN
ff2c0 4c 4f 43 4b 2c 20 5f 53 48 41 52 45 44 2c 20 5f  LOCK, _SHARED, _
ff2d0 52 45 53 45 52 56 45 44 2c 20 65 74 63 2e 20 2a  RESERVED, etc. *
ff2e0 2f 0a 20 20 75 38 20 74 65 6d 70 46 69 6c 65 3b  /.  u8 tempFile;
ff2f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff300 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20  /* zFilename is 
ff310 61 20 74 65 6d 70 6f 72 61 72 79 20 66 69 6c 65  a temporary file
ff320 20 2a 2f 0a 20 20 75 38 20 72 65 61 64 4f 6e 6c   */.  u8 readOnl
ff330 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  y;              
ff340 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
ff350 72 65 61 64 2d 6f 6e 6c 79 20 64 61 74 61 62 61  read-only databa
ff360 73 65 20 2a 2f 0a 20 20 75 38 20 6e 65 65 64 53  se */.  u8 needS
ff370 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  ync;            
ff380 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
ff390 6e 20 66 73 79 6e 63 28 29 20 69 73 20 6e 65 65  n fsync() is nee
ff3a0 64 65 64 20 6f 6e 20 74 68 65 20 6a 6f 75 72 6e  ded on the journ
ff3b0 61 6c 20 2a 2f 0a 20 20 75 38 20 64 69 72 74 79  al */.  u8 dirty
ff3c0 43 61 63 68 65 3b 20 20 20 20 20 20 20 20 20 20  Cache;          
ff3d0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63      /* True if c
ff3e0 61 63 68 65 64 20 70 61 67 65 73 20 68 61 76 65  ached pages have
ff3f0 20 63 68 61 6e 67 65 64 20 2a 2f 0a 20 20 75 38   changed */.  u8
ff400 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 3b   alwaysRollback;
ff410 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73            /* Dis
ff420 61 62 6c 65 20 44 6f 6e 74 52 6f 6c 6c 62 61 63  able DontRollbac
ff430 6b 28 29 20 66 6f 72 20 61 6c 6c 20 70 61 67 65  k() for all page
ff440 73 20 2a 2f 0a 20 20 75 38 20 6d 65 6d 44 62 3b  s */.  u8 memDb;
ff450 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff460 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 69 6e     /* True to in
ff470 68 69 62 69 74 20 61 6c 6c 20 66 69 6c 65 20 49  hibit all file I
ff480 2f 4f 20 2a 2f 0a 20 20 75 38 20 73 65 74 4d 61  /O */.  u8 setMa
ff490 73 74 65 72 3b 20 20 20 20 20 20 20 20 20 20 20  ster;           
ff4a0 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61      /* True if a
ff4b0 20 6d 2d 6a 20 6e 61 6d 65 20 68 61 73 20 62 65   m-j name has be
ff4c0 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20 6a 72  en written to jr
ff4d0 6e 6c 20 2a 2f 0a 20 20 75 38 20 64 6f 4e 6f 74  nl */.  u8 doNot
ff4e0 53 79 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20  Sync;           
ff4f0 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20      /* Boolean. 
ff500 57 68 69 6c 65 20 74 72 75 65 2c 20 64 6f 20 6e  While true, do n
ff510 6f 74 20 73 70 69 6c 6c 20 74 68 65 20 63 61 63  ot spill the cac
ff520 68 65 20 2a 2f 0a 20 20 75 38 20 65 78 63 6c 75  he */.  u8 exclu
ff530 73 69 76 65 4d 6f 64 65 3b 20 20 20 20 20 20 20  siveMode;       
ff540 20 20 20 20 2f 2a 20 42 6f 6f 6c 65 61 6e 2e 20      /* Boolean. 
ff550 54 72 75 65 20 69 66 20 6c 6f 63 6b 69 6e 67 5f  True if locking_
ff560 6d 6f 64 65 3d 3d 45 58 43 4c 55 53 49 56 45 20  mode==EXCLUSIVE 
ff570 2a 2f 0a 20 20 75 38 20 6a 6f 75 72 6e 61 6c 4d  */.  u8 journalM
ff580 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ode;            
ff590 20 2f 2a 20 4f 6e 20 6f 66 20 74 68 65 20 50 41   /* On of the PA
ff5a0 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
ff5b0 2a 20 76 61 6c 75 65 73 20 2a 2f 0a 20 20 75 38  * values */.  u8
ff5c0 20 64 62 4d 6f 64 69 66 69 65 64 3b 20 20 20 20   dbModified;    
ff5d0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
ff5e0 65 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  e if there are a
ff5f0 6e 79 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  ny changes to th
ff600 65 20 44 62 20 2a 2f 0a 20 20 75 38 20 63 68 61  e Db */.  u8 cha
ff610 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 3b 20 20 20  ngeCountDone;   
ff620 20 20 20 20 20 20 2f 2a 20 53 65 74 20 61 66 74        /* Set aft
ff630 65 72 20 69 6e 63 72 65 6d 65 6e 74 69 6e 67 20  er incrementing 
ff640 74 68 65 20 63 68 61 6e 67 65 2d 63 6f 75 6e 74  the change-count
ff650 65 72 20 2a 2f 0a 20 20 75 38 20 64 62 53 69 7a  er */.  u8 dbSiz
ff660 65 56 61 6c 69 64 3b 20 20 20 20 20 20 20 20 20  eValid;         
ff670 20 20 20 20 2f 2a 20 53 65 74 20 77 68 65 6e 20      /* Set when 
ff680 64 62 53 69 7a 65 20 69 73 20 63 6f 72 72 65 63  dbSize is correc
ff690 74 20 2a 2f 0a 20 20 75 33 32 20 76 66 73 46 6c  t */.  u32 vfsFl
ff6a0 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
ff6b0 20 20 20 2f 2a 20 46 6c 61 67 73 20 66 6f 72 20     /* Flags for 
ff6c0 73 71 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65  sqlite3_vfs.xOpe
ff6d0 6e 28 29 20 2a 2f 0a 20 20 69 6e 74 20 65 72 72  n() */.  int err
ff6e0 43 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20  Code;           
ff6f0 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 73       /* One of s
ff700 65 76 65 72 61 6c 20 6b 69 6e 64 73 20 6f 66 20  everal kinds of 
ff710 65 72 72 6f 72 73 20 2a 2f 0a 20 20 50 67 6e 6f  errors */.  Pgno
ff720 20 64 62 53 69 7a 65 3b 20 20 20 20 20 20 20 20   dbSize;        
ff730 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
ff740 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 74 68  r of pages in th
ff750 65 20 66 69 6c 65 20 2a 2f 0a 20 20 50 67 6e 6f  e file */.  Pgno
ff760 20 6f 72 69 67 44 62 53 69 7a 65 3b 20 20 20 20   origDbSize;    
ff770 20 20 20 20 20 20 20 20 2f 2a 20 64 62 53 69 7a          /* dbSiz
ff780 65 20 62 65 66 6f 72 65 20 74 68 65 20 63 75 72  e before the cur
ff790 72 65 6e 74 20 63 68 61 6e 67 65 20 2a 2f 0a 20  rent change */. 
ff7a0 20 50 67 6e 6f 20 73 74 6d 74 53 69 7a 65 3b 20   Pgno stmtSize; 
ff7b0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ff7c0 53 69 7a 65 20 6f 66 20 64 61 74 61 62 61 73 65  Size of database
ff7d0 20 28 69 6e 20 70 61 67 65 73 29 20 61 74 20 73   (in pages) at s
ff7e0 74 6d 74 5f 62 65 67 69 6e 28 29 20 2a 2f 0a 20  tmt_begin() */. 
ff7f0 20 69 6e 74 20 6e 52 65 63 3b 20 20 20 20 20 20   int nRec;      
ff800 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ff810 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
ff820 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6a  written to the j
ff830 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 75 33 32 20  ournal */.  u32 
ff840 63 6b 73 75 6d 49 6e 69 74 3b 20 20 20 20 20 20  cksumInit;      
ff850 20 20 20 20 20 20 20 20 2f 2a 20 51 75 61 73 69          /* Quasi
ff860 2d 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 61 64  -random value ad
ff870 64 65 64 20 74 6f 20 65 76 65 72 79 20 63 68 65  ded to every che
ff880 63 6b 73 75 6d 20 2a 2f 0a 20 20 69 6e 74 20 73  cksum */.  int s
ff890 74 6d 74 4e 52 65 63 3b 20 20 20 20 20 20 20 20  tmtNRec;        
ff8a0 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
ff8b0 20 6f 66 20 72 65 63 6f 72 64 73 20 69 6e 20 73   of records in s
ff8c0 74 6d 74 20 73 75 62 6a 6f 75 72 6e 61 6c 20 2a  tmt subjournal *
ff8d0 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 3b 20  /.  int nExtra; 
ff8e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff8f0 2f 2a 20 41 64 64 20 74 68 69 73 20 6d 61 6e 79  /* Add this many
ff900 20 62 79 74 65 73 20 74 6f 20 65 61 63 68 20 69   bytes to each i
ff910 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 20 2a 2f  n-memory page */
ff920 0a 20 20 69 6e 74 20 70 61 67 65 53 69 7a 65 3b  .  int pageSize;
ff930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
ff940 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
ff950 73 20 69 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20  s in a page */. 
ff960 20 69 6e 74 20 6e 50 61 67 65 3b 20 20 20 20 20   int nPage;     
ff970 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ff980 54 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  Total number of 
ff990 69 6e 2d 6d 65 6d 6f 72 79 20 70 61 67 65 73 20  in-memory pages 
ff9a0 2a 2f 0a 20 20 69 6e 74 20 6d 78 50 61 67 65 3b  */.  int mxPage;
ff9b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
ff9c0 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62   /* Maximum numb
ff9d0 65 72 20 6f 66 20 70 61 67 65 73 20 74 6f 20 68  er of pages to h
ff9e0 6f 6c 64 20 69 6e 20 63 61 63 68 65 20 2a 2f 0a  old in cache */.
ff9f0 20 20 50 67 6e 6f 20 6d 78 50 67 6e 6f 3b 20 20    Pgno mxPgno;  
ffa00 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
ffa10 20 4d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64   Maximum allowed
ffa20 20 73 69 7a 65 20 6f 66 20 74 68 65 20 64 61 74   size of the dat
ffa30 61 62 61 73 65 20 2a 2f 0a 20 20 42 69 74 76 65  abase */.  Bitve
ffa40 63 20 2a 70 49 6e 4a 6f 75 72 6e 61 6c 3b 20 20  c *pInJournal;  
ffa50 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69         /* One bi
ffa60 74 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  t for each page 
ffa70 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
ffa80 66 69 6c 65 20 2a 2f 0a 20 20 42 69 74 76 65 63  file */.  Bitvec
ffa90 20 2a 70 49 6e 53 74 6d 74 3b 20 20 20 20 20 20   *pInStmt;      
ffaa0 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 62 69 74        /* One bit
ffab0 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20 69   for each page i
ffac0 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  n the database *
ffad0 2f 0a 20 20 42 69 74 76 65 63 20 2a 70 41 6c 77  /.  Bitvec *pAlw
ffae0 61 79 73 52 6f 6c 6c 62 61 63 6b 3b 20 20 20 20  aysRollback;    
ffaf0 2f 2a 20 4f 6e 65 20 62 69 74 20 66 6f 72 20 65  /* One bit for e
ffb00 61 63 68 20 70 61 67 65 20 6d 61 72 6b 65 64 20  ach page marked 
ffb10 61 6c 77 61 79 73 2d 72 6f 6c 6c 62 61 63 6b 20  always-rollback 
ffb20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 46 69 6c 65  */.  char *zFile
ffb30 6e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  name;           
ffb40 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
ffb50 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
ffb60 0a 20 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61  .  char *zJourna
ffb70 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  l;             /
ffb80 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f  * Name of the jo
ffb90 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20  urnal file */.  
ffba0 63 68 61 72 20 2a 7a 44 69 72 65 63 74 6f 72 79  char *zDirectory
ffbb0 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44  ;           /* D
ffbc0 69 72 65 63 74 6f 72 79 20 68 6f 6c 64 20 64 61  irectory hold da
ffbd0 74 61 62 61 73 65 20 61 6e 64 20 6a 6f 75 72 6e  tabase and journ
ffbe0 61 6c 20 66 69 6c 65 73 20 2a 2f 0a 20 20 73 71  al files */.  sq
ffbf0 6c 69 74 65 33 5f 66 69 6c 65 20 2a 66 64 2c 20  lite3_file *fd, 
ffc00 2a 6a 66 64 3b 20 20 20 20 20 2f 2a 20 46 69 6c  *jfd;     /* Fil
ffc10 65 20 64 65 73 63 72 69 70 74 6f 72 73 20 66 6f  e descriptors fo
ffc20 72 20 64 61 74 61 62 61 73 65 20 61 6e 64 20 6a  r database and j
ffc30 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 73 71 6c 69  ournal */.  sqli
ffc40 74 65 33 5f 66 69 6c 65 20 2a 73 74 66 64 3b 20  te3_file *stfd; 
ffc50 20 20 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20          /* File 
ffc60 64 65 73 63 72 69 70 74 6f 72 20 66 6f 72 20 74  descriptor for t
ffc70 68 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 62  he statement sub
ffc80 6a 6f 75 72 6e 61 6c 2a 2f 0a 20 20 69 6e 74 20  journal*/.  int 
ffc90 28 2a 78 42 75 73 79 48 61 6e 64 6c 65 72 29 28  (*xBusyHandler)(
ffca0 76 6f 69 64 2a 29 3b 20 2f 2a 20 46 75 6e 63 74  void*); /* Funct
ffcb0 69 6f 6e 20 74 6f 20 63 61 6c 6c 20 77 68 65 6e  ion to call when
ffcc0 20 62 75 73 79 20 2a 2f 0a 20 20 76 6f 69 64 20   busy */.  void 
ffcd0 2a 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67  *pBusyHandlerArg
ffce0 3b 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78  ;      /* Contex
ffcf0 74 20 61 72 67 75 6d 65 6e 74 20 66 6f 72 20 78  t argument for x
ffd00 42 75 73 79 48 61 6e 64 6c 65 72 20 2a 2f 0a 20  BusyHandler */. 
ffd10 20 69 36 34 20 6a 6f 75 72 6e 61 6c 4f 66 66 3b   i64 journalOff;
ffd20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ffd30 43 75 72 72 65 6e 74 20 62 79 74 65 20 6f 66 66  Current byte off
ffd40 73 65 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  set in the journ
ffd50 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36 34  al file */.  i64
ffd60 20 6a 6f 75 72 6e 61 6c 48 64 72 3b 20 20 20 20   journalHdr;    
ffd70 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65           /* Byte
ffd80 20 6f 66 66 73 65 74 20 74 6f 20 70 72 65 76 69   offset to previ
ffd90 6f 75 73 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  ous journal head
ffda0 65 72 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74  er */.  i64 stmt
ffdb0 48 64 72 4f 66 66 3b 20 20 20 20 20 20 20 20 20  HdrOff;         
ffdc0 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6a 6f 75      /* First jou
ffdd0 72 6e 61 6c 20 68 65 61 64 65 72 20 77 72 69 74  rnal header writ
ffde0 74 65 6e 20 74 68 69 73 20 73 74 61 74 65 6d 65  ten this stateme
ffdf0 6e 74 20 2a 2f 0a 20 20 69 36 34 20 73 74 6d 74  nt */.  i64 stmt
ffe00 43 6b 73 75 6d 3b 20 20 20 20 20 20 20 20 20 20  Cksum;          
ffe10 20 20 20 20 2f 2a 20 63 6b 73 75 6d 49 6e 69 74      /* cksumInit
ffe20 20 77 68 65 6e 20 73 74 61 74 65 6d 65 6e 74 20   when statement 
ffe30 77 61 73 20 73 74 61 72 74 65 64 20 2a 2f 0a 20  was started */. 
ffe40 20 69 36 34 20 73 74 6d 74 4a 53 69 7a 65 3b 20   i64 stmtJSize; 
ffe50 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
ffe60 53 69 7a 65 20 6f 66 20 6a 6f 75 72 6e 61 6c 20  Size of journal 
ffe70 61 74 20 73 74 6d 74 5f 62 65 67 69 6e 28 29 20  at stmt_begin() 
ffe80 2a 2f 0a 20 20 75 33 32 20 73 65 63 74 6f 72 53  */.  u32 sectorS
ffe90 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
ffea0 20 2f 2a 20 41 73 73 75 6d 65 64 20 73 65 63 74   /* Assumed sect
ffeb0 6f 72 20 73 69 7a 65 20 64 75 72 69 6e 67 20 72  or size during r
ffec0 6f 6c 6c 62 61 63 6b 20 2a 2f 0a 23 69 66 64 65  ollback */.#ifde
ffed0 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
ffee0 69 6e 74 20 6e 48 69 74 2c 20 6e 4d 69 73 73 3b  int nHit, nMiss;
ffef0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
fff00 61 63 68 65 20 68 69 74 73 20 61 6e 64 20 6d 69  ache hits and mi
fff10 73 73 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e  ssing */.  int n
fff20 52 65 61 64 2c 20 6e 57 72 69 74 65 3b 20 20 20  Read, nWrite;   
fff30 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
fff40 73 65 20 70 61 67 65 73 20 72 65 61 64 2f 77 72  se pages read/wr
fff50 69 74 74 65 6e 20 2a 2f 0a 23 65 6e 64 69 66 0a  itten */.#endif.
fff60 20 20 76 6f 69 64 20 28 2a 78 52 65 69 6e 69 74    void (*xReinit
fff70 65 72 29 28 44 62 50 61 67 65 2a 29 3b 20 2f 2a  er)(DbPage*); /*
fff80 20 43 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69   Call this routi
fff90 6e 65 20 77 68 65 6e 20 72 65 6c 6f 61 64 69 6e  ne when reloadin
fffa0 67 20 70 61 67 65 73 20 2a 2f 0a 23 69 66 64 65  g pages */.#ifde
fffb0 66 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44  f SQLITE_HAS_COD
fffc0 45 43 0a 20 20 76 6f 69 64 20 2a 28 2a 78 43 6f  EC.  void *(*xCo
fffd0 64 65 63 29 28 76 6f 69 64 2a 2c 76 6f 69 64 2a  dec)(void*,void*
fffe0 2c 50 67 6e 6f 2c 69 6e 74 29 3b 20 2f 2a 20 52  ,Pgno,int); /* R
ffff0 6f 75 74 69 6e 65 20 66 6f 72 20 65 6e 2f 64 65  outine for en/de
10000 63 6f 64 69 6e 67 20 64 61 74 61 20 2a 2f 0a 20  coding data */. 
10001 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41 72 67   void *pCodecArg
10002 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
10003 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 74  First argument t
10004 6f 20 78 43 6f 64 65 63 28 29 20 2a 2f 0a 23 65  o xCodec() */.#e
10005 6e 64 69 66 0a 20 20 63 68 61 72 20 2a 70 54 6d  ndif.  char *pTm
10006 70 53 70 61 63 65 3b 20 20 20 20 20 20 20 20 20  pSpace;         
10007 20 20 20 2f 2a 20 50 61 67 65 72 2e 70 61 67 65     /* Pager.page
10008 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 73 70  Size bytes of sp
10009 61 63 65 20 66 6f 72 20 74 6d 70 20 75 73 65 20  ace for tmp use 
1000a 2a 2f 0a 20 20 63 68 61 72 20 64 62 46 69 6c 65  */.  char dbFile
1000b 56 65 72 73 5b 31 36 5d 3b 20 20 20 20 20 20 20  Vers[16];       
1000c 20 2f 2a 20 43 68 61 6e 67 65 73 20 77 68 65 6e   /* Changes when
1000d 65 76 65 72 20 64 61 74 61 62 61 73 65 20 66 69  ever database fi
1000e 6c 65 20 63 68 61 6e 67 65 73 20 2a 2f 0a 20 20  le changes */.  
1000f 69 36 34 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 4c  i64 journalSizeL
10010 69 6d 69 74 3b 20 20 20 20 20 20 20 2f 2a 20 53  imit;       /* S
10011 69 7a 65 20 6c 69 6d 69 74 20 66 6f 72 20 70 65  ize limit for pe
10012 72 73 69 73 74 65 6e 74 20 6a 6f 75 72 6e 61 6c  rsistent journal
10013 20 66 69 6c 65 73 20 2a 2f 0a 20 20 50 43 61 63   files */.  PCac
10014 68 65 20 2a 70 50 43 61 63 68 65 3b 20 20 20 20  he *pPCache;    
10015 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
10016 65 72 20 74 6f 20 70 61 67 65 20 63 61 63 68 65  er to page cache
10017 20 6f 62 6a 65 63 74 20 2a 2f 0a 7d 3b 0a 0a 2f   object */.};../
10018 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69  *.** The followi
10019 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62  ng global variab
1001a 6c 65 73 20 68 6f 6c 64 20 63 6f 75 6e 74 65 72  les hold counter
1001b 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65  s used for.** te
1001c 73 74 69 6e 67 20 70 75 72 70 6f 73 65 73 20 6f  sting purposes o
1001d 6e 6c 79 2e 20 20 54 68 65 73 65 20 76 61 72 69  nly.  These vari
1001e 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 65 78 69  ables do not exi
1001f 73 74 20 69 6e 0a 2a 2a 20 61 20 6e 6f 6e 2d 74  st in.** a non-t
10020 65 73 74 69 6e 67 20 62 75 69 6c 64 2e 20 20 54  esting build.  T
10021 68 65 73 65 20 76 61 72 69 61 62 6c 65 73 20 61  hese variables a
10022 72 65 20 6e 6f 74 20 74 68 72 65 61 64 2d 73 61  re not thread-sa
10023 66 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  fe..*/.#ifdef SQ
10024 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c 49 54 45  LITE_TEST.SQLITE
10025 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
10026 5f 70 61 67 65 72 5f 72 65 61 64 64 62 5f 63 6f  _pager_readdb_co
10027 75 6e 74 20 3d 20 30 3b 20 20 20 20 2f 2a 20 4e  unt = 0;    /* N
10028 75 6d 62 65 72 20 6f 66 20 66 75 6c 6c 20 70 61  umber of full pa
10029 67 65 73 20 72 65 61 64 20 66 72 6f 6d 20 44 42  ges read from DB
1002a 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69   */.SQLITE_API i
1002b 6e 74 20 73 71 6c 69 74 65 33 5f 70 61 67 65 72  nt sqlite3_pager
1002c 5f 77 72 69 74 65 64 62 5f 63 6f 75 6e 74 20 3d  _writedb_count =
1002d 20 30 3b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20   0;   /* Number 
1002e 6f 66 20 66 75 6c 6c 20 70 61 67 65 73 20 77 72  of full pages wr
1002f 69 74 74 65 6e 20 74 6f 20 44 42 20 2a 2f 0a 53  itten to DB */.S
10030 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
10031 6c 69 74 65 33 5f 70 61 67 65 72 5f 77 72 69 74  lite3_pager_writ
10032 65 6a 5f 63 6f 75 6e 74 20 3d 20 30 3b 20 20 20  ej_count = 0;   
10033 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61   /* Number of pa
10034 67 65 73 20 77 72 69 74 74 65 6e 20 74 6f 20 6a  ges written to j
10035 6f 75 72 6e 61 6c 20 2a 2f 0a 23 20 64 65 66 69  ournal */.# defi
10036 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28 76 29  ne PAGER_INCR(v)
10037 20 20 76 2b 2b 0a 23 65 6c 73 65 0a 23 20 64 65    v++.#else.# de
10038 66 69 6e 65 20 50 41 47 45 52 5f 49 4e 43 52 28  fine PAGER_INCR(
10039 76 29 0a 23 65 6e 64 69 66 0a 0a 0a 0a 2f 2a 0a  v).#endif..../*.
1003a 2a 2a 20 4a 6f 75 72 6e 61 6c 20 66 69 6c 65 73  ** Journal files
1003b 20 62 65 67 69 6e 20 77 69 74 68 20 74 68 65 20   begin with the 
1003c 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 67 69 63 20  following magic 
1003d 73 74 72 69 6e 67 2e 20 20 54 68 65 20 64 61 74  string.  The dat
1003e 61 0a 2a 2a 20 77 61 73 20 6f 62 74 61 69 6e 65  a.** was obtaine
1003f 64 20 66 72 6f 6d 20 2f 64 65 76 2f 72 61 6e 64  d from /dev/rand
10040 6f 6d 2e 20 20 49 74 20 69 73 20 75 73 65 64 20  om.  It is used 
10041 6f 6e 6c 79 20 61 73 20 61 20 73 61 6e 69 74 79  only as a sanity
10042 20 63 68 65 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 69   check..**.** Si
10043 6e 63 65 20 76 65 72 73 69 6f 6e 20 32 2e 38 2e  nce version 2.8.
10044 30 2c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  0, the journal f
10045 6f 72 6d 61 74 20 63 6f 6e 74 61 69 6e 73 20 61  ormat contains a
10046 64 64 69 74 69 6f 6e 61 6c 20 73 61 6e 69 74 79  dditional sanity
10047 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 69 6e 66  .** checking inf
10048 6f 72 6d 61 74 69 6f 6e 2e 20 20 49 66 20 74 68  ormation.  If th
10049 65 20 70 6f 77 65 72 20 66 61 69 6c 73 20 77 68  e power fails wh
1004a 69 6c 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ile the journal 
1004b 69 73 20 62 65 67 69 6e 0a 2a 2a 20 77 72 69 74  is begin.** writ
1004c 74 65 6e 2c 20 73 65 6d 69 2d 72 61 6e 64 6f 6d  ten, semi-random
1004d 20 67 61 72 62 61 67 65 20 64 61 74 61 20 6d 69   garbage data mi
1004e 67 68 74 20 61 70 70 65 61 72 20 69 6e 20 74 68  ght appear in th
1004f 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c  e journal.** fil
10050 65 20 61 66 74 65 72 20 70 6f 77 65 72 20 69 73  e after power is
10051 20 72 65 73 74 6f 72 65 64 2e 20 20 49 66 20 61   restored.  If a
10052 6e 20 61 74 74 65 6d 70 74 20 69 73 20 74 68 65  n attempt is the
10053 6e 20 6d 61 64 65 0a 2a 2a 20 74 6f 20 72 6f 6c  n made.** to rol
10054 6c 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62 61  l the journal ba
10055 63 6b 2c 20 74 68 65 20 64 61 74 61 62 61 73 65  ck, the database
10056 20 63 6f 75 6c 64 20 62 65 20 63 6f 72 72 75 70   could be corrup
10057 74 65 64 2e 20 20 54 68 65 20 61 64 64 69 74 69  ted.  The additi
10058 6f 6e 61 6c 0a 2a 2a 20 73 61 6e 69 74 79 20 63  onal.** sanity c
10059 68 65 63 6b 69 6e 67 20 64 61 74 61 20 69 73 20  hecking data is 
1005a 61 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 64 69  an attempt to di
1005b 73 63 6f 76 65 72 20 74 68 65 20 67 61 72 62 61  scover the garba
1005c 67 65 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75  ge in the.** jou
1005d 72 6e 61 6c 20 61 6e 64 20 69 67 6e 6f 72 65 20  rnal and ignore 
1005e 69 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 61  it..**.** The sa
1005f 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 20 69 6e  nity checking in
10060 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74 68  formation for th
10061 65 20 6e 65 77 20 6a 6f 75 72 6e 61 6c 20 66 6f  e new journal fo
10062 72 6d 61 74 20 63 6f 6e 73 69 73 74 73 0a 2a 2a  rmat consists.**
10063 20 6f 66 20 61 20 33 32 2d 62 69 74 20 63 68 65   of a 32-bit che
10064 63 6b 73 75 6d 20 6f 6e 20 65 61 63 68 20 70 61  cksum on each pa
10065 67 65 20 6f 66 20 64 61 74 61 2e 20 20 54 68 65  ge of data.  The
10066 20 63 68 65 63 6b 73 75 6d 20 63 6f 76 65 72 73   checksum covers
10067 20 62 6f 74 68 0a 2a 2a 20 74 68 65 20 70 61 67   both.** the pag
10068 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 74 68 65  e number and the
10069 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
1006a 65 20 62 79 74 65 73 20 6f 66 20 64 61 74 61 20  e bytes of data 
1006b 66 6f 72 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a  for the page..**
1006c 20 54 68 69 73 20 63 6b 73 75 6d 20 69 73 20 69   This cksum is i
1006d 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 61 20  nitialized to a 
1006e 33 32 2d 62 69 74 20 72 61 6e 64 6f 6d 20 76 61  32-bit random va
1006f 6c 75 65 20 74 68 61 74 20 61 70 70 65 61 72 73  lue that appears
10070 20 69 6e 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e   in the.** journ
10071 61 6c 20 66 69 6c 65 20 72 69 67 68 74 20 61 66  al file right af
10072 74 65 72 20 74 68 65 20 68 65 61 64 65 72 2e 20  ter the header. 
10073 20 54 68 65 20 72 61 6e 64 6f 6d 20 69 6e 69 74   The random init
10074 69 61 6c 69 7a 65 72 20 69 73 20 69 6d 70 6f 72  ializer is impor
10075 74 61 6e 74 2c 0a 2a 2a 20 62 65 63 61 75 73 65  tant,.** because
10076 20 67 61 72 62 61 67 65 20 64 61 74 61 20 74 68   garbage data th
10077 61 74 20 61 70 70 65 61 72 73 20 61 74 20 74 68  at appears at th
10078 65 20 65 6e 64 20 6f 66 20 61 20 6a 6f 75 72 6e  e end of a journ
10079 61 6c 20 69 73 20 6c 69 6b 65 6c 79 0a 2a 2a 20  al is likely.** 
1007a 64 61 74 61 20 74 68 61 74 20 77 61 73 20 6f 6e  data that was on
1007b 63 65 20 69 6e 20 6f 74 68 65 72 20 66 69 6c 65  ce in other file
1007c 73 20 74 68 61 74 20 68 61 76 65 20 6e 6f 77 20  s that have now 
1007d 62 65 65 6e 20 64 65 6c 65 74 65 64 2e 20 20 49  been deleted.  I
1007e 66 20 74 68 65 0a 2a 2a 20 67 61 72 62 61 67 65  f the.** garbage
1007f 20 64 61 74 61 20 63 61 6d 65 20 66 72 6f 6d 20   data came from 
10080 61 6e 20 6f 62 73 6f 6c 65 74 65 20 6a 6f 75 72  an obsolete jour
10081 6e 61 6c 20 66 69 6c 65 2c 20 74 68 65 20 63 68  nal file, the ch
10082 65 63 6b 73 75 6d 73 20 6d 69 67 68 74 0a 2a 2a  ecksums might.**
10083 20 62 65 20 63 6f 72 72 65 63 74 2e 20 20 42 75   be correct.  Bu
10084 74 20 62 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e  t by initializin
10085 67 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 74  g the checksum t
10086 6f 20 72 61 6e 64 6f 6d 20 76 61 6c 75 65 20 77  o random value w
10087 68 69 63 68 0a 2a 2a 20 69 73 20 64 69 66 66 65  hich.** is diffe
10088 72 65 6e 74 20 66 6f 72 20 65 76 65 72 79 20 6a  rent for every j
10089 6f 75 72 6e 61 6c 2c 20 77 65 20 6d 69 6e 69 6d  ournal, we minim
1008a 69 7a 65 20 74 68 61 74 20 72 69 73 6b 2e 0a 2a  ize that risk..*
1008b 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75  /.static const u
1008c 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 4a 6f  nsigned char aJo
1008d 75 72 6e 61 6c 4d 61 67 69 63 5b 5d 20 3d 20 7b  urnalMagic[] = {
1008e 0a 20 20 30 78 64 39 2c 20 30 78 64 35 2c 20 30  .  0xd9, 0xd5, 0
1008f 78 30 35 2c 20 30 78 66 39 2c 20 30 78 32 30 2c  x05, 0xf9, 0x20,
10090 20 30 78 61 31 2c 20 30 78 36 33 2c 20 30 78 64   0xa1, 0x63, 0xd
10091 37 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  7,.};../*.** The
10092 20 73 69 7a 65 20 6f 66 20 74 68 65 20 68 65 61   size of the hea
10093 64 65 72 20 61 6e 64 20 6f 66 20 65 61 63 68 20  der and of each 
10094 70 61 67 65 20 69 6e 20 74 68 65 20 6a 6f 75 72  page in the jour
10095 6e 61 6c 20 69 73 20 64 65 74 65 72 6d 69 6e 65  nal is determine
10096 64 0a 2a 2a 20 62 79 20 74 68 65 20 66 6f 6c 6c  d.** by the foll
10097 6f 77 69 6e 67 20 6d 61 63 72 6f 73 2e 0a 2a 2f  owing macros..*/
10098 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52 4e 41 4c  .#define JOURNAL
10099 5f 50 47 5f 53 5a 28 70 50 61 67 65 72 29 20 20  _PG_SZ(pPager)  
1009a 28 28 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  ((pPager->pageSi
1009b 7a 65 29 20 2b 20 38 29 0a 0a 2f 2a 0a 2a 2a 20  ze) + 8)../*.** 
1009c 54 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64  The journal head
1009d 65 72 20 73 69 7a 65 20 66 6f 72 20 74 68 69 73  er size for this
1009e 20 70 61 67 65 72 2e 20 49 6e 20 74 68 65 20 66   pager. In the f
1009f 75 74 75 72 65 2c 20 74 68 69 73 20 63 6f 75 6c  uture, this coul
100a0 64 20 62 65 0a 2a 2a 20 73 65 74 20 74 6f 20 73  d be.** set to s
100a1 6f 6d 65 20 76 61 6c 75 65 20 72 65 61 64 20 66  ome value read f
100a2 72 6f 6d 20 74 68 65 20 64 69 73 6b 20 63 6f 6e  rom the disk con
100a3 74 72 6f 6c 6c 65 72 2e 20 54 68 65 20 69 6d 70  troller. The imp
100a4 6f 72 74 61 6e 74 0a 2a 2a 20 63 68 61 72 61 63  ortant.** charac
100a5 74 65 72 69 73 74 69 63 20 69 73 20 74 68 61 74  teristic is that
100a6 20 69 74 20 69 73 20 74 68 65 20 73 61 6d 65 20   it is the same 
100a7 73 69 7a 65 20 61 73 20 61 20 64 69 73 6b 20 73  size as a disk s
100a8 65 63 74 6f 72 2e 0a 2a 2f 0a 23 64 65 66 69 6e  ector..*/.#defin
100a9 65 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a  e JOURNAL_HDR_SZ
100aa 28 70 50 61 67 65 72 29 20 28 70 50 61 67 65 72  (pPager) (pPager
100ab 2d 3e 73 65 63 74 6f 72 53 69 7a 65 29 0a 0a 2f  ->sectorSize)../
100ac 2a 0a 2a 2a 20 54 68 65 20 6d 61 63 72 6f 20 4d  *.** The macro M
100ad 45 4d 44 42 20 69 73 20 74 72 75 65 20 69 66 20  EMDB is true if 
100ae 77 65 20 61 72 65 20 64 65 61 6c 69 6e 67 20 77  we are dealing w
100af 69 74 68 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  ith an in-memory
100b0 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 20 57 65   database..** We
100b1 20 64 6f 20 74 68 69 73 20 61 73 20 61 20 6d 61   do this as a ma
100b2 63 72 6f 20 73 6f 20 74 68 61 74 20 69 66 20 74  cro so that if t
100b3 68 65 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d  he SQLITE_OMIT_M
100b4 45 4d 4f 52 59 44 42 20 6d 61 63 72 6f 20 69 73  EMORYDB macro is
100b5 20 73 65 74 2c 0a 2a 2a 20 74 68 65 20 76 61 6c   set,.** the val
100b6 75 65 20 6f 66 20 4d 45 4d 44 42 20 77 69 6c 6c  ue of MEMDB will
100b7 20 62 65 20 61 20 63 6f 6e 73 74 61 6e 74 20 61   be a constant a
100b8 6e 64 20 74 68 65 20 63 6f 6d 70 69 6c 65 72 20  nd the compiler 
100b9 77 69 6c 6c 20 6f 70 74 69 6d 69 7a 65 0a 2a 2a  will optimize.**
100ba 20 6f 75 74 20 63 6f 64 65 20 74 68 61 74 20 77   out code that w
100bb 6f 75 6c 64 20 6e 65 76 65 72 20 65 78 65 63 75  ould never execu
100bc 74 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51  te..*/.#ifdef SQ
100bd 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45 4d 4f 52 59  LITE_OMIT_MEMORY
100be 44 42 0a 23 20 64 65 66 69 6e 65 20 4d 45 4d 44  DB.# define MEMD
100bf 42 20 30 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  B 0.#else.# defi
100c0 6e 65 20 4d 45 4d 44 42 20 70 50 61 67 65 72 2d  ne MEMDB pPager-
100c1 3e 6d 65 6d 44 62 0a 23 65 6e 64 69 66 0a 0a 2f  >memDb.#endif../
100c2 2a 0a 2a 2a 20 50 61 67 65 20 6e 75 6d 62 65 72  *.** Page number
100c3 20 50 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 20 69   PAGER_MJ_PGNO i
100c4 73 20 6e 65 76 65 72 20 75 73 65 64 20 69 6e 20  s never used in 
100c5 61 6e 20 53 51 4c 69 74 65 20 64 61 74 61 62 61  an SQLite databa
100c6 73 65 20 28 69 74 20 69 73 0a 2a 2a 20 72 65 73  se (it is.** res
100c7 65 72 76 65 64 20 66 6f 72 20 77 6f 72 6b 69 6e  erved for workin
100c8 67 20 61 72 6f 75 6e 64 20 61 20 77 69 6e 64 6f  g around a windo
100c9 77 73 2f 70 6f 73 69 78 20 69 6e 63 6f 6d 70 61  ws/posix incompa
100ca 74 69 62 69 6c 69 74 79 29 2e 20 49 74 20 69 73  tibility). It is
100cb 0a 2a 2a 20 75 73 65 64 20 69 6e 20 74 68 65 20  .** used in the 
100cc 6a 6f 75 72 6e 61 6c 20 74 6f 20 73 69 67 6e 69  journal to signi
100cd 66 79 20 74 68 61 74 20 74 68 65 20 72 65 6d 61  fy that the rema
100ce 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75  inder of the jou
100cf 72 6e 61 6c 20 66 69 6c 65 20 0a 2a 2a 20 69 73  rnal file .** is
100d0 20 64 65 76 6f 74 65 64 20 74 6f 20 73 74 6f 72   devoted to stor
100d1 69 6e 67 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  ing a master jou
100d2 72 6e 61 6c 20 6e 61 6d 65 20 2d 20 74 68 65 72  rnal name - ther
100d3 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 70 61  e are no more pa
100d4 67 65 73 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 20 62  ges to.** roll b
100d5 61 63 6b 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74  ack. See comment
100d6 73 20 66 6f 72 20 66 75 6e 63 74 69 6f 6e 20 77  s for function w
100d7 72 69 74 65 4d 61 73 74 65 72 4a 6f 75 72 6e 61  riteMasterJourna
100d8 6c 28 29 20 66 6f 72 20 64 65 74 61 69 6c 73 2e  l() for details.
100d9 0a 2a 2f 0a 2f 2a 20 23 64 65 66 69 6e 65 20 50  .*/./* #define P
100da 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 78 29 20  AGER_MJ_PGNO(x) 
100db 28 50 45 4e 44 49 4e 47 5f 42 59 54 45 2f 28 28  (PENDING_BYTE/((
100dc 78 29 2d 3e 70 61 67 65 53 69 7a 65 29 29 20 2a  x)->pageSize)) *
100dd 2f 0a 23 64 65 66 69 6e 65 20 50 41 47 45 52 5f  /.#define PAGER_
100de 4d 4a 5f 50 47 4e 4f 28 78 29 20 28 28 50 67 6e  MJ_PGNO(x) ((Pgn
100df 6f 29 28 28 50 45 4e 44 49 4e 47 5f 42 59 54 45  o)((PENDING_BYTE
100e0 2f 28 28 78 29 2d 3e 70 61 67 65 53 69 7a 65 29  /((x)->pageSize)
100e1 29 2b 31 29 29 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  )+1))../*.** The
100e2 20 6d 61 78 69 6d 75 6d 20 6c 65 67 61 6c 20 70   maximum legal p
100e3 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 28 32  age number is (2
100e4 5e 33 31 20 2d 20 31 29 2e 0a 2a 2f 0a 23 64 65  ^31 - 1)..*/.#de
100e5 66 69 6e 65 20 50 41 47 45 52 5f 4d 41 58 5f 50  fine PAGER_MAX_P
100e6 47 4e 4f 20 32 31 34 37 34 38 33 36 34 37 0a 0a  GNO 2147483647..
100e7 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  /*.** Return tru
100e8 65 20 69 66 20 70 61 67 65 20 2a 70 50 67 20 68  e if page *pPg h
100e9 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
100ea 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 73  written to the s
100eb 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 6a 6f 75 72  tatement.** jour
100ec 6e 61 6c 20 28 6f 72 20 73 74 61 74 65 6d 65 6e  nal (or statemen
100ed 74 20 73 6e 61 70 73 68 6f 74 20 68 61 73 20 62  t snapshot has b
100ee 65 65 6e 20 63 72 65 61 74 65 64 2c 20 69 66 20  een created, if 
100ef 2a 70 50 67 20 69 73 20 70 61 72 74 0a 2a 2a 20  *pPg is part.** 
100f0 6f 66 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20  of an in-memory 
100f1 64 61 74 61 62 61 73 65 29 2e 0a 2a 2f 0a 73 74  database)..*/.st
100f2 61 74 69 63 20 69 6e 74 20 70 61 67 65 49 6e 53  atic int pageInS
100f3 74 61 74 65 6d 65 6e 74 28 50 67 48 64 72 20 2a  tatement(PgHdr *
100f4 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70  pPg){.  Pager *p
100f5 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61  Pager = pPg->pPa
100f6 67 65 72 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  ger;.  return sq
100f7 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74 28  lite3BitvecTest(
100f8 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c  pPager->pInStmt,
100f9 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 7d 0a 0a   pPg->pgno);.}..
100fa 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 49  static int pageI
100fb 6e 4a 6f 75 72 6e 61 6c 28 50 67 48 64 72 20 2a  nJournal(PgHdr *
100fc 70 50 67 29 7b 0a 20 20 72 65 74 75 72 6e 20 73  pPg){.  return s
100fd 71 6c 69 74 65 33 42 69 74 76 65 63 54 65 73 74  qlite3BitvecTest
100fe 28 70 50 67 2d 3e 70 50 61 67 65 72 2d 3e 70 49  (pPg->pPager->pI
100ff 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e 70  nJournal, pPg->p
10100 67 6e 6f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  gno);.}../*.** R
10101 65 61 64 20 61 20 33 32 2d 62 69 74 20 69 6e 74  ead a 32-bit int
10102 65 67 65 72 20 66 72 6f 6d 20 74 68 65 20 67 69  eger from the gi
10103 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70  ven file descrip
10104 74 6f 72 2e 20 20 53 74 6f 72 65 20 74 68 65 20  tor.  Store the 
10105 69 6e 74 65 67 65 72 0a 2a 2a 20 74 68 61 74 20  integer.** that 
10106 69 73 20 72 65 61 64 20 69 6e 20 2a 70 52 65 73  is read in *pRes
10107 2e 20 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  .  Return SQLITE
10108 5f 4f 4b 20 69 66 20 65 76 65 72 79 74 68 69 6e  _OK if everythin
10109 67 20 77 6f 72 6b 65 64 2c 20 6f 72 20 61 6e 0a  g worked, or an.
1010a 2a 2a 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73  ** error code is
1010b 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20   something goes 
1010c 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c  wrong..**.** All
1010d 20 76 61 6c 75 65 73 20 61 72 65 20 73 74 6f 72   values are stor
1010e 65 64 20 6f 6e 20 64 69 73 6b 20 61 73 20 62 69  ed on disk as bi
1010f 67 2d 65 6e 64 69 61 6e 2e 0a 2a 2f 0a 73 74 61  g-endian..*/.sta
10110 74 69 63 20 69 6e 74 20 72 65 61 64 33 32 62 69  tic int read32bi
10111 74 73 28 73 71 6c 69 74 65 33 5f 66 69 6c 65 20  ts(sqlite3_file 
10112 2a 66 64 2c 20 69 36 34 20 6f 66 66 73 65 74 2c  *fd, i64 offset,
10113 20 75 33 32 20 2a 70 52 65 73 29 7b 0a 20 20 75   u32 *pRes){.  u
10114 6e 73 69 67 6e 65 64 20 63 68 61 72 20 61 63 5b  nsigned char ac[
10115 34 5d 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  4];.  int rc = s
10116 71 6c 69 74 65 33 4f 73 52 65 61 64 28 66 64 2c  qlite3OsRead(fd,
10117 20 61 63 2c 20 73 69 7a 65 6f 66 28 61 63 29 2c   ac, sizeof(ac),
10118 20 6f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20   offset);.  if( 
10119 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1011a 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 73 71 6c  .    *pRes = sql
1011b 69 74 65 33 47 65 74 34 62 79 74 65 28 61 63 29  ite3Get4byte(ac)
1011c 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1011d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  c;.}../*.** Writ
1011e 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65 67  e a 32-bit integ
1011f 65 72 20 69 6e 74 6f 20 61 20 73 74 72 69 6e 67  er into a string
10120 20 62 75 66 66 65 72 20 69 6e 20 62 69 67 2d 65   buffer in big-e
10121 6e 64 69 61 6e 20 62 79 74 65 20 6f 72 64 65 72  ndian byte order
10122 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 70 75 74  ..*/.#define put
10123 33 32 62 69 74 73 28 41 2c 42 29 20 20 73 71 6c  32bits(A,B)  sql
10124 69 74 65 33 50 75 74 34 62 79 74 65 28 28 75 38  ite3Put4byte((u8
10125 2a 29 41 2c 42 29 0a 0a 2f 2a 0a 2a 2a 20 57 72  *)A,B)../*.** Wr
10126 69 74 65 20 61 20 33 32 2d 62 69 74 20 69 6e 74  ite a 32-bit int
10127 65 67 65 72 20 69 6e 74 6f 20 74 68 65 20 67 69  eger into the gi
10128 76 65 6e 20 66 69 6c 65 20 64 65 73 63 72 69 70  ven file descrip
10129 74 6f 72 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  tor.  Return SQL
1012a 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75 63  ITE_OK.** on suc
1012b 63 65 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72  cess or an error
1012c 20 63 6f 64 65 20 69 73 20 73 6f 6d 65 74 68 69   code is somethi
1012d 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2e 0a 2a  ng goes wrong..*
1012e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  /.static int wri
1012f 74 65 33 32 62 69 74 73 28 73 71 6c 69 74 65 33  te32bits(sqlite3
10130 5f 66 69 6c 65 20 2a 66 64 2c 20 69 36 34 20 6f  _file *fd, i64 o
10131 66 66 73 65 74 2c 20 75 33 32 20 76 61 6c 29 7b  ffset, u32 val){
10132 0a 20 20 63 68 61 72 20 61 63 5b 34 5d 3b 0a 20  .  char ac[4];. 
10133 20 70 75 74 33 32 62 69 74 73 28 61 63 2c 20 76   put32bits(ac, v
10134 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71  al);.  return sq
10135 6c 69 74 65 33 4f 73 57 72 69 74 65 28 66 64 2c  lite3OsWrite(fd,
10136 20 61 63 2c 20 34 2c 20 6f 66 66 73 65 74 29 3b   ac, 4, offset);
10137 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 66 69 6c  .}../*.** If fil
10138 65 20 70 46 64 20 69 73 20 6f 70 65 6e 2c 20 63  e pFd is open, c
10139 61 6c 6c 20 73 71 6c 69 74 65 33 4f 73 55 6e 6c  all sqlite3OsUnl
1013a 6f 63 6b 28 29 20 6f 6e 20 69 74 2e 0a 2a 2f 0a  ock() on it..*/.
1013b 73 74 61 74 69 63 20 69 6e 74 20 6f 73 55 6e 6c  static int osUnl
1013c 6f 63 6b 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  ock(sqlite3_file
1013d 20 2a 70 46 64 2c 20 69 6e 74 20 65 4c 6f 63 6b   *pFd, int eLock
1013e 29 7b 0a 20 20 69 66 28 20 21 70 46 64 2d 3e 70  ){.  if( !pFd->p
1013f 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 72  Methods ){.    r
10140 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
10141 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
10142 6c 69 74 65 33 4f 73 55 6e 6c 6f 63 6b 28 70 46  lite3OsUnlock(pF
10143 64 2c 20 65 4c 6f 63 6b 29 3b 0a 7d 0a 0a 2f 2a  d, eLock);.}../*
10144 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
10145 6e 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 65  n determines whe
10146 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68 65 20  ther or not the 
10147 61 74 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74  atomic-write opt
10148 69 6d 69 7a 61 74 69 6f 6e 0a 2a 2a 20 63 61 6e  imization.** can
10149 20 62 65 20 75 73 65 64 20 77 69 74 68 20 74 68   be used with th
1014a 69 73 20 70 61 67 65 72 2e 20 54 68 65 20 6f 70  is pager. The op
1014b 74 69 6d 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62  timization can b
1014c 65 20 75 73 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a  e used if:.**.**
1014d 20 20 28 61 29 20 74 68 65 20 76 61 6c 75 65 20    (a) the value 
1014e 72 65 74 75 72 6e 65 64 20 62 79 20 4f 73 44 65  returned by OsDe
1014f 76 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74  viceCharacterist
10150 69 63 73 28 29 20 69 6e 64 69 63 61 74 65 73 20  ics() indicates 
10151 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 61 20 64  that.**      a d
10152 61 74 61 62 61 73 65 20 70 61 67 65 20 6d 61 79  atabase page may
10153 20 62 65 20 77 72 69 74 74 65 6e 20 61 74 6f 6d   be written atom
10154 69 63 61 6c 6c 79 2c 20 61 6e 64 0a 2a 2a 20 20  ically, and.**  
10155 28 62 29 20 74 68 65 20 76 61 6c 75 65 20 72 65  (b) the value re
10156 74 75 72 6e 65 64 20 62 79 20 4f 73 53 65 63 74  turned by OsSect
10157 6f 72 53 69 7a 65 28 29 20 69 73 20 6c 65 73 73  orSize() is less
10158 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 0a 2a   than or equal.*
10159 2a 20 20 20 20 20 20 74 6f 20 74 68 65 20 70 61  *      to the pa
1015a 67 65 20 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 20 49  ge size..**.** I
1015b 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69  f the optimizati
1015c 6f 6e 20 63 61 6e 6e 6f 74 20 62 65 20 75 73 65  on cannot be use
1015d 64 2c 20 30 20 69 73 20 72 65 74 75 72 6e 65 64  d, 0 is returned
1015e 2e 20 49 66 20 69 74 20 63 61 6e 20 62 65 20 75  . If it can be u
1015f 73 65 64 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  sed,.** then the
10160 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20   value returned 
10161 69 73 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74  is the size of t
10162 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
10163 77 68 65 6e 20 69 74 0a 2a 2a 20 63 6f 6e 74 61  when it.** conta
10164 69 6e 73 20 72 6f 6c 6c 62 61 63 6b 20 64 61 74  ins rollback dat
10165 61 20 66 6f 72 20 65 78 61 63 74 6c 79 20 6f 6e  a for exactly on
10166 65 20 70 61 67 65 2e 0a 2a 2f 0a 23 69 66 64 65  e page..*/.#ifde
10167 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  f SQLITE_ENABLE_
10168 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a 73 74 61  ATOMIC_WRITE.sta
10169 74 69 63 20 69 6e 74 20 6a 72 6e 6c 42 75 66 66  tic int jrnlBuff
1016a 65 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50  erSize(Pager *pP
1016b 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 64 63 3b  ager){.  int dc;
1016c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
1016d 76 69 63 65 20 63 68 61 72 61 63 74 65 72 69 73  vice characteris
1016e 74 69 63 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 53  tics */.  int nS
1016f 65 63 74 6f 72 3b 20 20 20 20 20 20 2f 2a 20 53  ector;      /* S
10170 65 63 74 6f 72 20 73 69 7a 65 20 2a 2f 0a 20 20  ector size */.  
10171 69 6e 74 20 73 7a 50 61 67 65 3b 20 20 20 20 20  int szPage;     
10172 20 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20     /* Page size 
10173 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  */.  sqlite3_fil
10174 65 20 2a 66 64 20 3d 20 70 50 61 67 65 72 2d 3e  e *fd = pPager->
10175 66 64 3b 0a 0a 20 20 69 66 28 20 66 64 2d 3e 70  fd;..  if( fd->p
10176 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20 20 64  Methods ){.    d
10177 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 76  c = sqlite3OsDev
10178 69 63 65 43 68 61 72 61 63 74 65 72 69 73 74 69  iceCharacteristi
10179 63 73 28 66 64 29 3b 0a 20 20 20 20 6e 53 65 63  cs(fd);.    nSec
1017a 74 6f 72 20 3d 20 73 71 6c 69 74 65 33 4f 73 53  tor = sqlite3OsS
1017b 65 63 74 6f 72 53 69 7a 65 28 66 64 29 3b 0a 20  ectorSize(fd);. 
1017c 20 20 20 73 7a 50 61 67 65 20 3d 20 70 50 61 67     szPage = pPag
1017d 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20  er->pageSize;.  
1017e 7d 0a 0a 20 20 61 73 73 65 72 74 28 53 51 4c 49  }..  assert(SQLI
1017f 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35  TE_IOCAP_ATOMIC5
10180 31 32 3d 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20  12==(512>>8));. 
10181 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f 49   assert(SQLITE_I
10182 4f 43 41 50 5f 41 54 4f 4d 49 43 36 34 4b 3d 3d  OCAP_ATOMIC64K==
10183 28 36 35 35 33 36 3e 3e 38 29 29 3b 0a 0a 20 20  (65536>>8));..  
10184 69 66 28 20 21 66 64 2d 3e 70 4d 65 74 68 6f 64  if( !fd->pMethod
10185 73 20 7c 7c 20 0a 20 20 20 20 20 20 20 28 64 63  s || .       (dc
10186 20 26 20 28 53 51 4c 49 54 45 5f 49 4f 43 41 50   & (SQLITE_IOCAP
10187 5f 41 54 4f 4d 49 43 7c 28 73 7a 50 61 67 65 3e  _ATOMIC|(szPage>
10188 3e 38 29 29 20 26 26 20 6e 53 65 63 74 6f 72 3c  >8)) && nSector<
10189 3d 73 7a 50 61 67 65 29 20 29 7b 0a 20 20 20 20  =szPage) ){.    
1018a 72 65 74 75 72 6e 20 4a 4f 55 52 4e 41 4c 5f 48  return JOURNAL_H
1018b 44 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20  DR_SZ(pPager) + 
1018c 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70 50  JOURNAL_PG_SZ(pP
1018d 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74  ager);.  }.  ret
1018e 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a  urn 0;.}.#endif.
1018f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63  ./*.** This func
10190 74 69 6f 6e 20 73 68 6f 75 6c 64 20 62 65 20 63  tion should be c
10191 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20 65 72  alled when an er
10192 72 6f 72 20 6f 63 63 75 72 73 20 77 69 74 68 69  ror occurs withi
10193 6e 20 74 68 65 20 70 61 67 65 72 0a 2a 2a 20 63  n the pager.** c
10194 6f 64 65 2e 20 54 68 65 20 66 69 72 73 74 20 61  ode. The first a
10195 72 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69  rgument is a poi
10196 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67 65  nter to the page
10197 72 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65  r structure, the
10198 0a 2a 2a 20 73 65 63 6f 6e 64 20 74 68 65 20 65  .** second the e
10199 72 72 6f 72 2d 63 6f 64 65 20 61 62 6f 75 74 20  rror-code about 
1019a 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20 62  to be returned b
1019b 79 20 61 20 70 61 67 65 72 20 41 50 49 20 66 75  y a pager API fu
1019c 6e 63 74 69 6f 6e 2e 20 0a 2a 2a 20 54 68 65 20  nction. .** The 
1019d 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69  value returned i
1019e 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  s a copy of the 
1019f 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
101a0 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  to this function
101a1 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  . .**.** If the 
101a2 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20  second argument 
101a3 69 73 20 53 51 4c 49 54 45 5f 49 4f 45 52 52 2c  is SQLITE_IOERR,
101a4 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 2c   SQLITE_CORRUPT,
101a5 20 6f 72 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a   or SQLITE_FULL.
101a6 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 62 65 63  ** the error bec
101a7 6f 6d 65 73 20 70 65 72 73 69 73 74 65 6e 74 2e  omes persistent.
101a8 20 55 6e 74 69 6c 20 74 68 65 20 70 65 72 73 69   Until the persi
101a9 73 74 65 6e 20 65 72 72 6f 72 20 69 73 20 63 6c  sten error is cl
101aa 65 61 72 65 64 2c 0a 2a 2a 20 73 75 62 73 65 71  eared,.** subseq
101ab 75 65 6e 74 20 41 50 49 20 63 61 6c 6c 73 20 6f  uent API calls o
101ac 6e 20 74 68 69 73 20 50 61 67 65 72 20 77 69 6c  n this Pager wil
101ad 6c 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 72 65  l immediately re
101ae 74 75 72 6e 20 74 68 65 20 73 61 6d 65 20 0a 2a  turn the same .*
101af 2a 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2a  * error code..**
101b0 0a 2a 2a 20 41 20 70 65 72 73 69 73 74 65 6e 74  .** A persistent
101b1 20 65 72 72 6f 72 20 69 6e 64 69 63 61 74 65 73   error indicates
101b2 20 74 68 61 74 20 74 68 65 20 63 6f 6e 74 65 6e   that the conten
101b3 74 73 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d  ts of the pager-
101b4 63 61 63 68 65 20 0a 2a 2a 20 63 61 6e 6e 6f 74  cache .** cannot
101b5 20 62 65 20 74 72 75 73 74 65 64 2e 20 54 68 69   be trusted. Thi
101b6 73 20 73 74 61 74 65 20 63 61 6e 20 62 65 20 63  s state can be c
101b7 6c 65 61 72 65 64 20 62 79 20 63 6f 6d 70 6c 65  leared by comple
101b8 74 65 6c 79 20 64 69 73 63 61 72 64 69 6e 67 20  tely discarding 
101b9 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 73  .** the contents
101ba 20 6f 66 20 74 68 65 20 70 61 67 65 72 2d 63 61   of the pager-ca
101bb 63 68 65 2e 20 49 66 20 61 20 74 72 61 6e 73 61  che. If a transa
101bc 63 74 69 6f 6e 20 77 61 73 20 61 63 74 69 76 65  ction was active
101bd 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 70 65 72   when.** the per
101be 73 69 73 74 65 6e 74 20 65 72 72 6f 72 20 6f 63  sistent error oc
101bf 63 75 72 65 64 2c 20 74 68 65 6e 20 74 68 65 20  cured, then the 
101c0 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
101c1 20 6d 61 79 20 6e 65 65 64 0a 2a 2a 20 74 6f 20   may need.** to 
101c2 62 65 20 72 65 70 6c 61 79 65 64 2e 0a 2a 2f 0a  be replayed..*/.
101c3 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65  static void page
101c4 72 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a  r_unlock(Pager *
101c5 70 50 61 67 65 72 29 3b 0a 73 74 61 74 69 63 20  pPager);.static 
101c6 69 6e 74 20 70 61 67 65 72 5f 65 72 72 6f 72 28  int pager_error(
101c7 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69  Pager *pPager, i
101c8 6e 74 20 72 63 29 7b 0a 20 20 69 6e 74 20 72 63  nt rc){.  int rc
101c9 32 20 3d 20 72 63 20 26 20 30 78 66 66 3b 0a 20  2 = rc & 0xff;. 
101ca 20 61 73 73 65 72 74 28 0a 20 20 20 20 20 20 20   assert(.       
101cb 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
101cc 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 20 7c 7c 0a  =SQLITE_FULL ||.
101cd 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65         pPager->e
101ce 72 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f  rrCode==SQLITE_O
101cf 4b 20 7c 7c 0a 20 20 20 20 20 20 20 28 70 50 61  K ||.       (pPa
101d0 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 26 20 30  ger->errCode & 0
101d1 78 66 66 29 3d 3d 53 51 4c 49 54 45 5f 49 4f 45  xff)==SQLITE_IOE
101d2 52 52 0a 20 20 29 3b 0a 20 20 69 66 28 0a 20 20  RR.  );.  if(.  
101d3 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 46 55    rc2==SQLITE_FU
101d4 4c 4c 20 7c 7c 0a 20 20 20 20 72 63 32 3d 3d 53  LL ||.    rc2==S
101d5 51 4c 49 54 45 5f 49 4f 45 52 52 20 7c 7c 0a 20  QLITE_IOERR ||. 
101d6 20 20 20 72 63 32 3d 3d 53 51 4c 49 54 45 5f 43     rc2==SQLITE_C
101d7 4f 52 52 55 50 54 0a 20 20 29 7b 0a 20 20 20 20  ORRUPT.  ){.    
101d8 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
101d9 3d 20 72 63 3b 0a 20 20 20 20 69 66 28 20 70 50  = rc;.    if( pP
101da 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47  ager->state==PAG
101db 45 52 5f 55 4e 4c 4f 43 4b 20 0a 20 20 20 20 20  ER_UNLOCK .     
101dc 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  && sqlite3Pcache
101dd 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
101de 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20 20  >pPCache)==0 .  
101df 20 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66    ){.      /* If
101e0 20 74 68 65 20 70 61 67 65 72 20 69 73 20 61 6c   the pager is al
101e1 72 65 61 64 79 20 75 6e 6c 6f 63 6b 65 64 2c 20  ready unlocked, 
101e2 63 61 6c 6c 20 70 61 67 65 72 5f 75 6e 6c 6f 63  call pager_unloc
101e3 6b 28 29 20 6e 6f 77 20 74 6f 0a 20 20 20 20 20  k() now to.     
101e4 20 2a 2a 20 63 6c 65 61 72 20 74 68 65 20 65 72   ** clear the er
101e5 72 6f 72 20 73 74 61 74 65 20 61 6e 64 20 65 6e  ror state and en
101e6 73 75 72 65 20 74 68 61 74 20 74 68 65 20 70 61  sure that the pa
101e7 67 65 72 2d 63 61 63 68 65 20 69 73 20 0a 20 20  ger-cache is .  
101e8 20 20 20 20 2a 2a 20 63 6f 6d 70 6c 65 74 65 6c      ** completel
101e9 79 20 65 6d 70 74 79 2e 0a 20 20 20 20 20 20 2a  y empty..      *
101ea 2f 0a 20 20 20 20 20 20 70 61 67 65 72 5f 75 6e  /.      pager_un
101eb 6c 6f 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  lock(pPager);.  
101ec 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
101ed 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66   rc;.}../*.** If
101ee 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
101ef 47 45 53 20 69 73 20 64 65 66 69 6e 65 64 20 74  GES is defined t
101f0 68 65 6e 20 77 65 20 64 6f 20 73 6f 6d 65 20 73  hen we do some s
101f1 61 6e 69 74 79 20 63 68 65 63 6b 69 6e 67 0a 2a  anity checking.*
101f2 2a 20 6f 6e 20 74 68 65 20 63 61 63 68 65 20 75  * on the cache u
101f3 73 69 6e 67 20 61 20 68 61 73 68 20 66 75 6e 63  sing a hash func
101f4 74 69 6f 6e 2e 20 20 54 68 69 73 20 69 73 20 75  tion.  This is u
101f5 73 65 64 20 66 6f 72 20 74 65 73 74 69 6e 67 0a  sed for testing.
101f6 2a 2a 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ** and debugging
101f7 20 6f 6e 6c 79 2e 0a 2a 2f 0a 23 69 66 64 65 66   only..*/.#ifdef
101f8 20 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41   SQLITE_CHECK_PA
101f9 47 45 53 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  GES./*.** Return
101fa 20 61 20 33 32 2d 62 69 74 20 68 61 73 68 20 6f   a 32-bit hash o
101fb 66 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20  f the page data 
101fc 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f 0a 73 74  for pPage..*/.st
101fd 61 74 69 63 20 75 33 32 20 70 61 67 65 72 5f 64  atic u32 pager_d
101fe 61 74 61 68 61 73 68 28 69 6e 74 20 6e 42 79 74  atahash(int nByt
101ff 65 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  e, unsigned char
10200 20 2a 70 44 61 74 61 29 7b 0a 20 20 75 33 32 20   *pData){.  u32 
10201 68 61 73 68 20 3d 20 30 3b 0a 20 20 69 6e 74 20  hash = 0;.  int 
10202 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
10203 6e 42 79 74 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  nByte; i++){.   
10204 20 68 61 73 68 20 3d 20 28 68 61 73 68 2a 31 30   hash = (hash*10
10205 33 39 29 20 2b 20 70 44 61 74 61 5b 69 5d 3b 0a  39) + pData[i];.
10206 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 68 61 73    }.  return has
10207 68 3b 0a 7d 0a 73 74 61 74 69 63 20 75 33 32 20  h;.}.static u32 
10208 70 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 50  pager_pagehash(P
10209 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20 20  gHdr *pPage){.  
1020a 72 65 74 75 72 6e 20 70 61 67 65 72 5f 64 61 74  return pager_dat
1020b 61 68 61 73 68 28 70 50 61 67 65 2d 3e 70 50 61  ahash(pPage->pPa
1020c 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 28  ger->pageSize, (
1020d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
1020e 70 50 61 67 65 2d 3e 70 44 61 74 61 29 3b 0a 7d  pPage->pData);.}
1020f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 70 61 67  .static void pag
10210 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 28  er_set_pagehash(
10211 50 67 48 64 72 20 2a 70 50 61 67 65 29 7b 0a 20  PgHdr *pPage){. 
10212 20 70 50 61 67 65 2d 3e 70 61 67 65 48 61 73 68   pPage->pageHash
10213 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61 73   = pager_pagehas
10214 68 28 70 50 61 67 65 29 3b 0a 7d 0a 0a 2f 2a 0a  h(pPage);.}../*.
10215 2a 2a 20 54 68 65 20 43 48 45 43 4b 5f 50 41 47  ** The CHECK_PAG
10216 45 20 6d 61 63 72 6f 20 74 61 6b 65 73 20 61 20  E macro takes a 
10217 50 67 48 64 72 2a 20 61 73 20 61 6e 20 61 72 67  PgHdr* as an arg
10218 75 6d 65 6e 74 2e 20 49 66 20 53 51 4c 49 54 45  ument. If SQLITE
10219 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 2a 2a 20  _CHECK_PAGES.** 
1021a 69 73 20 64 65 66 69 6e 65 64 2c 20 61 6e 64 20  is defined, and 
1021b 4e 44 45 42 55 47 20 69 73 20 6e 6f 74 20 64 65  NDEBUG is not de
1021c 66 69 6e 65 64 2c 20 61 6e 20 61 73 73 65 72 74  fined, an assert
1021d 28 29 20 73 74 61 74 65 6d 65 6e 74 20 63 68 65  () statement che
1021e 63 6b 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20  cks.** that the 
1021f 70 61 67 65 20 69 73 20 65 69 74 68 65 72 20 64  page is either d
10220 69 72 74 79 20 6f 72 20 73 74 69 6c 6c 20 6d 61  irty or still ma
10221 74 63 68 65 73 20 74 68 65 20 63 61 6c 63 75 6c  tches the calcul
10222 61 74 65 64 20 70 61 67 65 2d 68 61 73 68 2e 0a  ated page-hash..
10223 2a 2f 0a 23 64 65 66 69 6e 65 20 43 48 45 43 4b  */.#define CHECK
10224 5f 50 41 47 45 28 78 29 20 63 68 65 63 6b 50 61  _PAGE(x) checkPa
10225 67 65 28 78 29 0a 73 74 61 74 69 63 20 76 6f 69  ge(x).static voi
10226 64 20 63 68 65 63 6b 50 61 67 65 28 50 67 48 64  d checkPage(PgHd
10227 72 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72  r *pPg){.  Pager
10228 20 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e   *pPager = pPg->
10229 70 50 61 67 65 72 3b 0a 20 20 61 73 73 65 72 74  pPager;.  assert
1022a 28 20 21 70 50 67 2d 3e 70 61 67 65 48 61 73 68  ( !pPg->pageHash
1022b 20 7c 7c 20 70 50 61 67 65 72 2d 3e 65 72 72 43   || pPager->errC
1022c 6f 64 65 0a 20 20 20 20 20 20 7c 7c 20 28 70 50  ode.      || (pP
1022d 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 44  g->flags&PGHDR_D
1022e 49 52 54 59 29 20 7c 7c 20 70 50 67 2d 3e 70 61  IRTY) || pPg->pa
1022f 67 65 48 61 73 68 3d 3d 70 61 67 65 72 5f 70 61  geHash==pager_pa
10230 67 65 68 61 73 68 28 70 50 67 29 20 29 3b 0a 7d  gehash(pPg) );.}
10231 0a 0a 23 65 6c 73 65 0a 23 64 65 66 69 6e 65 20  ..#else.#define 
10232 70 61 67 65 72 5f 64 61 74 61 68 61 73 68 28 58  pager_datahash(X
10233 2c 59 29 20 20 30 0a 23 64 65 66 69 6e 65 20 70  ,Y)  0.#define p
10234 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 58 29  ager_pagehash(X)
10235 20 20 30 0a 23 64 65 66 69 6e 65 20 43 48 45 43    0.#define CHEC
10236 4b 5f 50 41 47 45 28 78 29 0a 23 65 6e 64 69 66  K_PAGE(x).#endif
10237 20 20 2f 2a 20 53 51 4c 49 54 45 5f 43 48 45 43    /* SQLITE_CHEC
10238 4b 5f 50 41 47 45 53 20 2a 2f 0a 0a 2f 2a 0a 2a  K_PAGES */../*.*
10239 2a 20 57 68 65 6e 20 74 68 69 73 20 69 73 20 63  * When this is c
1023a 61 6c 6c 65 64 20 74 68 65 20 6a 6f 75 72 6e 61  alled the journa
1023b 6c 20 66 69 6c 65 20 66 6f 72 20 70 61 67 65 72  l file for pager
1023c 20 70 50 61 67 65 72 20 6d 75 73 74 20 62 65 20   pPager must be 
1023d 6f 70 65 6e 2e 0a 2a 2a 20 54 68 65 20 6d 61 73  open..** The mas
1023e 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
1023f 20 6e 61 6d 65 20 69 73 20 72 65 61 64 20 66 72   name is read fr
10240 6f 6d 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  om the end of th
10241 65 20 66 69 6c 65 20 61 6e 64 20 0a 2a 2a 20 77  e file and .** w
10242 72 69 74 74 65 6e 20 69 6e 74 6f 20 6d 65 6d 6f  ritten into memo
10243 72 79 20 73 75 70 70 6c 69 65 64 20 62 79 20 74  ry supplied by t
10244 68 65 20 63 61 6c 6c 65 72 2e 20 0a 2a 2a 0a 2a  he caller. .**.*
10245 2a 20 7a 4d 61 73 74 65 72 20 6d 75 73 74 20 70  * zMaster must p
10246 6f 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72  oint to a buffer
10247 20 6f 66 20 61 74 20 6c 65 61 73 74 20 6e 4d 61   of at least nMa
10248 73 74 65 72 20 62 79 74 65 73 20 61 6c 6c 6f 63  ster bytes alloc
10249 61 74 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 63  ated by.** the c
1024a 61 6c 6c 65 72 2e 20 54 68 69 73 20 73 68 6f 75  aller. This shou
1024b 6c 64 20 62 65 20 73 71 6c 69 74 65 33 5f 76 66  ld be sqlite3_vf
1024c 73 2e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 20 28  s.mxPathname+1 (
1024d 74 6f 20 65 6e 73 75 72 65 20 74 68 65 72 65 20  to ensure there 
1024e 69 73 0a 2a 2a 20 65 6e 6f 75 67 68 20 73 70 61  is.** enough spa
1024f 63 65 20 74 6f 20 77 72 69 74 65 20 74 68 65 20  ce to write the 
10250 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
10251 61 6d 65 29 2e 20 49 66 20 74 68 65 20 6d 61 73  ame). If the mas
10252 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 6e  ter journal.** n
10253 61 6d 65 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  ame in the journ
10254 61 6c 20 69 73 20 6c 6f 6e 67 65 72 20 74 68 61  al is longer tha
10255 6e 20 6e 4d 61 73 74 65 72 20 62 79 74 65 73 20  n nMaster bytes 
10256 28 69 6e 63 6c 75 64 69 6e 67 20 61 0a 2a 2a 20  (including a.** 
10257 6e 75 6c 2d 74 65 72 6d 69 6e 61 74 6f 72 29 2c  nul-terminator),
10258 20 74 68 65 6e 20 74 68 69 73 20 69 73 20 68 61   then this is ha
10259 6e 64 6c 65 64 20 61 73 20 69 66 20 6e 6f 20 6d  ndled as if no m
1025a 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e 61  aster journal na
1025b 6d 65 0a 2a 2a 20 77 65 72 65 20 70 72 65 73 65  me.** were prese
1025c 6e 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e 61  nt in the journa
1025d 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 6d  l..**.** If no m
1025e 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
1025f 6c 65 20 6e 61 6d 65 20 69 73 20 70 72 65 73 65  le name is prese
10260 6e 74 20 7a 4d 61 73 74 65 72 5b 30 5d 20 69 73  nt zMaster[0] is
10261 20 73 65 74 20 74 6f 20 30 20 61 6e 64 0a 2a 2a   set to 0 and.**
10262 20 53 51 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72   SQLITE_OK retur
10263 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
10264 6e 74 20 72 65 61 64 4d 61 73 74 65 72 4a 6f 75  nt readMasterJou
10265 72 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c  rnal(sqlite3_fil
10266 65 20 2a 70 4a 72 6e 6c 2c 20 63 68 61 72 20 2a  e *pJrnl, char *
10267 7a 4d 61 73 74 65 72 2c 20 75 33 32 20 6e 4d 61  zMaster, u32 nMa
10268 73 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  ster){.  int rc;
10269 0a 20 20 75 33 32 20 6c 65 6e 3b 0a 20 20 69 36  .  u32 len;.  i6
1026a 34 20 73 7a 4a 3b 0a 20 20 75 33 32 20 63 6b 73  4 szJ;.  u32 cks
1026b 75 6d 3b 0a 20 20 75 33 32 20 75 3b 20 20 20 20  um;.  u32 u;    
1026c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1026d 2a 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20  * Unsigned loop 
1026e 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 6e 73  counter */.  uns
1026f 69 67 6e 65 64 20 63 68 61 72 20 61 4d 61 67 69  igned char aMagi
10270 63 5b 38 5d 3b 20 2f 2a 20 41 20 62 75 66 66 65  c[8]; /* A buffe
10271 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 6d 61  r to hold the ma
10272 67 69 63 20 68 65 61 64 65 72 20 2a 2f 0a 0a 20  gic header */.. 
10273 20 7a 4d 61 73 74 65 72 5b 30 5d 20 3d 20 27 5c   zMaster[0] = '\
10274 30 27 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  0';..  rc = sqli
10275 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4a  te3OsFileSize(pJ
10276 72 6e 6c 2c 20 26 73 7a 4a 29 3b 0a 20 20 69 66  rnl, &szJ);.  if
10277 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
10278 7c 7c 20 73 7a 4a 3c 31 36 20 29 20 72 65 74 75  || szJ<16 ) retu
10279 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72  rn rc;..  rc = r
1027a 65 61 64 33 32 62 69 74 73 28 70 4a 72 6e 6c 2c  ead32bits(pJrnl,
1027b 20 73 7a 4a 2d 31 36 2c 20 26 6c 65 6e 29 3b 0a   szJ-16, &len);.
1027c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1027d 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
1027e 0a 0a 20 20 69 66 28 20 6c 65 6e 3e 3d 6e 4d 61  ..  if( len>=nMa
1027f 73 74 65 72 20 29 7b 0a 20 20 20 20 72 65 74 75  ster ){.    retu
10280 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
10281 7d 0a 0a 20 20 72 63 20 3d 20 72 65 61 64 33 32  }..  rc = read32
10282 62 69 74 73 28 70 4a 72 6e 6c 2c 20 73 7a 4a 2d  bits(pJrnl, szJ-
10283 31 32 2c 20 26 63 6b 73 75 6d 29 3b 0a 20 20 69  12, &cksum);.  i
10284 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
10285 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
10286 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
10287 65 61 64 28 70 4a 72 6e 6c 2c 20 61 4d 61 67 69  ead(pJrnl, aMagi
10288 63 2c 20 38 2c 20 73 7a 4a 2d 38 29 3b 0a 20 20  c, 8, szJ-8);.  
10289 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1028a 4b 20 7c 7c 20 6d 65 6d 63 6d 70 28 61 4d 61 67  K || memcmp(aMag
1028b 69 63 2c 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  ic, aJournalMagi
1028c 63 2c 20 38 29 20 29 20 72 65 74 75 72 6e 20 72  c, 8) ) return r
1028d 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
1028e 65 33 4f 73 52 65 61 64 28 70 4a 72 6e 6c 2c 20  e3OsRead(pJrnl, 
1028f 7a 4d 61 73 74 65 72 2c 20 6c 65 6e 2c 20 73 7a  zMaster, len, sz
10290 4a 2d 31 36 2d 6c 65 6e 29 3b 0a 20 20 69 66 28  J-16-len);.  if(
10291 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
10292 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
10293 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65 72 5b 6c  .  }.  zMaster[l
10294 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 2f  en] = '\0';..  /
10295 2a 20 53 65 65 20 69 66 20 74 68 65 20 63 68 65  * See if the che
10296 63 6b 73 75 6d 20 6d 61 74 63 68 65 73 20 74 68  cksum matches th
10297 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
10298 20 6e 61 6d 65 20 2a 2f 0a 20 20 66 6f 72 28 75   name */.  for(u
10299 3d 30 3b 20 75 3c 6c 65 6e 3b 20 75 2b 2b 29 7b  =0; u<len; u++){
1029a 0a 20 20 20 20 63 6b 73 75 6d 20 2d 3d 20 7a 4d  .    cksum -= zM
1029b 61 73 74 65 72 5b 75 5d 3b 0a 20 20 20 7d 0a 20  aster[u];.   }. 
1029c 20 69 66 28 20 63 6b 73 75 6d 20 29 7b 0a 20 20   if( cksum ){.  
1029d 20 20 2f 2a 20 49 66 20 74 68 65 20 63 68 65 63    /* If the chec
1029e 6b 73 75 6d 20 64 6f 65 73 6e 27 74 20 61 64 64  ksum doesn't add
1029f 20 75 70 2c 20 74 68 65 6e 20 6f 6e 65 20 6f 72   up, then one or
102a0 20 6d 6f 72 65 20 6f 66 20 74 68 65 20 64 69 73   more of the dis
102a1 6b 20 73 65 63 74 6f 72 73 0a 20 20 20 20 2a 2a  k sectors.    **
102a2 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
102a3 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
102a4 69 6c 65 6e 61 6d 65 20 69 73 20 63 6f 72 72 75  ilename is corru
102a5 70 74 65 64 2e 20 54 68 69 73 20 6d 65 61 6e 73  pted. This means
102a6 0a 20 20 20 20 2a 2a 20 64 65 66 69 6e 69 74 65  .    ** definite
102a7 6c 79 20 72 6f 6c 6c 20 62 61 63 6b 2c 20 73 6f  ly roll back, so
102a8 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c   just return SQL
102a9 49 54 45 5f 4f 4b 20 61 6e 64 20 72 65 70 6f 72  ITE_OK and repor
102aa 74 20 61 20 28 6e 75 6c 29 0a 20 20 20 20 2a 2a  t a (nul).    **
102ab 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61 6c 20   master-journal 
102ac 66 69 6c 65 6e 61 6d 65 2e 0a 20 20 20 20 2a 2f  filename..    */
102ad 0a 20 20 20 20 7a 4d 61 73 74 65 72 5b 30 5d 20  .    zMaster[0] 
102ae 3d 20 27 5c 30 27 3b 0a 20 20 7d 0a 20 20 20 0a  = '\0';.  }.   .
102af 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
102b0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 65  OK;.}../*.** See
102b1 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  k the journal fi
102b2 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20 74 6f  le descriptor to
102b3 20 74 68 65 20 6e 65 78 74 20 73 65 63 74 6f 72   the next sector
102b4 20 62 6f 75 6e 64 61 72 79 20 77 68 65 72 65 20   boundary where 
102b5 61 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 68 65 61  a.** journal hea
102b6 64 65 72 20 6d 61 79 20 62 65 20 72 65 61 64 20  der may be read 
102b7 6f 72 20 77 72 69 74 74 65 6e 2e 20 50 61 67 65  or written. Page
102b8 72 2e 6a 6f 75 72 6e 61 6c 4f 66 66 20 69 73 20  r.journalOff is 
102b9 75 70 64 61 74 65 64 20 77 69 74 68 0a 2a 2a 20  updated with.** 
102ba 74 68 65 20 6e 65 77 20 73 65 65 6b 20 6f 66 66  the new seek off
102bb 73 65 74 2e 0a 2a 2a 0a 2a 2a 20 69 2e 65 20 66  set..**.** i.e f
102bc 6f 72 20 61 20 73 65 63 74 6f 72 20 73 69 7a 65  or a sector size
102bd 20 6f 66 20 35 31 32 3a 0a 2a 2a 0a 2a 2a 20 49   of 512:.**.** I
102be 6e 70 75 74 20 4f 66 66 73 65 74 20 20 20 20 20  nput Offset     
102bf 20 20 20 20 20 20 20 20 20 4f 75 74 70 75 74 20           Output 
102c0 4f 66 66 73 65 74 0a 2a 2a 20 2d 2d 2d 2d 2d 2d  Offset.** ------
102c1 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
102c2 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
102c3 2d 0a 2a 2a 20 30 20 20 20 20 20 20 20 20 20 20  -.** 0          
102c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
102c5 0a 2a 2a 20 35 31 32 20 20 20 20 20 20 20 20 20  .** 512         
102c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35 31                51
102c7 32 0a 2a 2a 20 31 30 30 20 20 20 20 20 20 20 20  2.** 100        
102c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 35                 5
102c9 31 32 0a 2a 2a 20 32 30 30 30 20 20 20 20 20 20  12.** 2000      
102ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
102cb 32 30 34 38 0a 2a 2a 20 0a 2a 2f 0a 73 74 61 74  2048.** .*/.stat
102cc 69 63 20 76 6f 69 64 20 73 65 65 6b 4a 6f 75 72  ic void seekJour
102cd 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50  nalHdr(Pager *pP
102ce 61 67 65 72 29 7b 0a 20 20 69 36 34 20 6f 66 66  ager){.  i64 off
102cf 73 65 74 20 3d 20 30 3b 0a 20 20 69 36 34 20 63  set = 0;.  i64 c
102d0 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e   = pPager->journ
102d1 61 6c 4f 66 66 3b 0a 20 20 69 66 28 20 63 20 29  alOff;.  if( c )
102d2 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 3d 20 28  {.    offset = (
102d3 28 63 2d 31 29 2f 4a 4f 55 52 4e 41 4c 5f 48 44  (c-1)/JOURNAL_HD
102d4 52 5f 53 5a 28 70 50 61 67 65 72 29 20 2b 20 31  R_SZ(pPager) + 1
102d5 29 20 2a 20 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ) * JOURNAL_HDR_
102d6 53 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 7d 0a  SZ(pPager);.  }.
102d7 20 20 61 73 73 65 72 74 28 20 6f 66 66 73 65 74    assert( offset
102d8 25 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  %JOURNAL_HDR_SZ(
102d9 70 50 61 67 65 72 29 3d 3d 30 20 29 3b 0a 20 20  pPager)==0 );.  
102da 61 73 73 65 72 74 28 20 6f 66 66 73 65 74 3e 3d  assert( offset>=
102db 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28  c );.  assert( (
102dc 6f 66 66 73 65 74 2d 63 29 3c 4a 4f 55 52 4e 41  offset-c)<JOURNA
102dd 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
102de 20 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f   );.  pPager->jo
102df 75 72 6e 61 6c 4f 66 66 20 3d 20 6f 66 66 73 65  urnalOff = offse
102e0 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74  t;.}../*.** Writ
102e1 65 20 7a 65 72 6f 73 20 6f 76 65 72 20 74 68 65  e zeros over the
102e2 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20 6a   header of the j
102e3 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 20 54 68  ournal file.  Th
102e4 69 73 20 68 61 73 20 74 68 65 0a 2a 2a 20 65 66  is has the.** ef
102e5 66 65 63 74 20 6f 66 20 69 6e 76 61 6c 69 64 61  fect of invalida
102e6 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ting the journal
102e7 20 66 69 6c 65 20 61 6e 64 20 63 6f 6d 6d 69 74   file and commit
102e8 74 69 6e 67 20 74 68 65 0a 2a 2a 20 74 72 61 6e  ting the.** tran
102e9 73 61 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74  saction..*/.stat
102ea 69 63 20 69 6e 74 20 7a 65 72 6f 4a 6f 75 72 6e  ic int zeroJourn
102eb 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50 61  alHdr(Pager *pPa
102ec 67 65 72 2c 20 69 6e 74 20 64 6f 54 72 75 6e 63  ger, int doTrunc
102ed 61 74 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ate){.  int rc =
102ee 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 73 74   SQLITE_OK;.  st
102ef 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
102f0 7a 65 72 6f 48 64 72 5b 32 38 5d 20 3d 20 7b 30  zeroHdr[28] = {0
102f1 7d 3b 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72  };..  if( pPager
102f2 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 29 7b 0a  ->journalOff ){.
102f3 20 20 20 20 69 36 34 20 69 4c 69 6d 69 74 20 3d      i64 iLimit =
102f4 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
102f5 53 69 7a 65 4c 69 6d 69 74 3b 0a 0a 20 20 20 20  SizeLimit;..    
102f6 49 4f 54 52 41 43 45 28 28 22 4a 5a 45 52 4f 48  IOTRACE(("JZEROH
102f7 44 52 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72  DR %p\n", pPager
102f8 29 29 0a 20 20 20 20 69 66 28 20 64 6f 54 72 75  )).    if( doTru
102f9 6e 63 61 74 65 20 7c 7c 20 69 4c 69 6d 69 74 3d  ncate || iLimit=
102fa 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
102fb 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61   sqlite3OsTrunca
102fc 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20  te(pPager->jfd, 
102fd 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  0);.    }else{. 
102fe 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
102ff 33 4f 73 57 72 69 74 65 28 70 50 61 67 65 72 2d  3OsWrite(pPager-
10300 3e 6a 66 64 2c 20 7a 65 72 6f 48 64 72 2c 20 73  >jfd, zeroHdr, s
10301 69 7a 65 6f 66 28 7a 65 72 6f 48 64 72 29 2c 20  izeof(zeroHdr), 
10302 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  0);.    }.    if
10303 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
10304 26 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79  && !pPager->noSy
10305 6e 63 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  nc ){.      rc =
10306 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
10307 50 61 67 65 72 2d 3e 6a 66 64 2c 20 53 51 4c 49  Pager->jfd, SQLI
10308 54 45 5f 53 59 4e 43 5f 44 41 54 41 4f 4e 4c 59  TE_SYNC_DATAONLY
10309 7c 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c  |pPager->sync_fl
1030a 61 67 73 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  ags);.    }..   
1030b 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e   /* At this poin
1030c 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
1030d 6e 20 69 73 20 63 6f 6d 6d 69 74 74 65 64 20 62  n is committed b
1030e 75 74 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63  ut the write loc
1030f 6b 20 0a 20 20 20 20 2a 2a 20 69 73 20 73 74 69  k .    ** is sti
10310 6c 6c 20 68 65 6c 64 20 6f 6e 20 74 68 65 20 66  ll held on the f
10311 69 6c 65 2e 20 49 66 20 74 68 65 72 65 20 69 73  ile. If there is
10312 20 61 20 73 69 7a 65 20 6c 69 6d 69 74 20 63 6f   a size limit co
10313 6e 66 69 67 75 72 65 64 20 66 6f 72 20 0a 20 20  nfigured for .  
10314 20 20 2a 2a 20 74 68 65 20 70 65 72 73 69 73 74    ** the persist
10315 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ent journal and 
10316 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
10317 20 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 75   currently consu
10318 6d 65 73 20 6d 6f 72 65 0a 20 20 20 20 2a 2a 20  mes more.    ** 
10319 73 70 61 63 65 20 74 68 61 6e 20 74 68 61 74 20  space than that 
1031a 6c 69 6d 69 74 20 61 6c 6c 6f 77 73 20 66 6f 72  limit allows for
1031b 2c 20 74 72 75 6e 63 61 74 65 20 69 74 20 6e 6f  , truncate it no
1031c 77 2e 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e  w. There is no n
1031d 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 73 79  eed.    ** to sy
1031e 6e 63 20 74 68 65 20 66 69 6c 65 20 66 6f 6c 6c  nc the file foll
1031f 6f 77 69 6e 67 20 74 68 69 73 20 6f 70 65 72 61  owing this opera
10320 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
10321 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
10322 4f 4b 20 26 26 20 69 4c 69 6d 69 74 3e 30 20 29  OK && iLimit>0 )
10323 7b 0a 20 20 20 20 20 20 69 36 34 20 73 7a 3b 0a  {.      i64 sz;.
10324 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
10325 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50 61  e3OsFileSize(pPa
10326 67 65 72 2d 3e 6a 66 64 2c 20 26 73 7a 29 3b 0a  ger->jfd, &sz);.
10327 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
10328 4c 49 54 45 5f 4f 4b 20 26 26 20 73 7a 3e 69 4c  LITE_OK && sz>iL
10329 69 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20  imit ){.        
1032a 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72  rc = sqlite3OsTr
1032b 75 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a  uncate(pPager->j
1032c 66 64 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  fd, iLimit);.   
1032d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1032e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1032f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c  *.** The journal
10330 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20 6f 70   file must be op
10331 65 6e 20 77 68 65 6e 20 74 68 69 73 20 72 6f 75  en when this rou
10332 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e 20  tine is called. 
10333 41 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 68 65 61  A journal.** hea
10334 64 65 72 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52  der (JOURNAL_HDR
10335 5f 53 5a 20 62 79 74 65 73 29 20 69 73 20 77 72  _SZ bytes) is wr
10336 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20 6a  itten into the j
10337 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61 74 20 74  ournal file at t
10338 68 65 0a 2a 2a 20 63 75 72 72 65 6e 74 20 6c 6f  he.** current lo
10339 63 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  cation..**.** Th
1033a 65 20 66 6f 72 6d 61 74 20 66 6f 72 20 74 68 65  e format for the
1033b 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65 72 20   journal header 
1033c 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  is as follows:.*
1033d 2a 20 2d 20 38 20 62 79 74 65 73 3a 20 4d 61 67  * - 8 bytes: Mag
1033e 69 63 20 69 64 65 6e 74 69 66 79 69 6e 67 20 6a  ic identifying j
1033f 6f 75 72 6e 61 6c 20 66 6f 72 6d 61 74 2e 0a 2a  ournal format..*
10340 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 4e 75 6d  * - 4 bytes: Num
10341 62 65 72 20 6f 66 20 72 65 63 6f 72 64 73 20 69  ber of records i
10342 6e 20 6a 6f 75 72 6e 61 6c 2c 20 6f 72 20 2d 31  n journal, or -1
10343 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 20 69 73   no-sync mode is
10344 20 6f 6e 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65   on..** - 4 byte
10345 73 3a 20 52 61 6e 64 6f 6d 20 6e 75 6d 62 65 72  s: Random number
10346 20 75 73 65 64 20 66 6f 72 20 70 61 67 65 20 68   used for page h
10347 61 73 68 2e 0a 2a 2a 20 2d 20 34 20 62 79 74 65  ash..** - 4 byte
10348 73 3a 20 49 6e 69 74 69 61 6c 20 64 61 74 61 62  s: Initial datab
10349 61 73 65 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a  ase page count..
1034a 2a 2a 20 2d 20 34 20 62 79 74 65 73 3a 20 53 65  ** - 4 bytes: Se
1034b 63 74 6f 72 20 73 69 7a 65 20 75 73 65 64 20 62  ctor size used b
1034c 79 20 74 68 65 20 70 72 6f 63 65 73 73 20 74 68  y the process th
1034d 61 74 20 77 72 6f 74 65 20 74 68 69 73 20 6a 6f  at wrote this jo
1034e 75 72 6e 61 6c 2e 0a 2a 2a 20 2d 20 34 20 62 79  urnal..** - 4 by
1034f 74 65 73 3a 20 44 61 74 61 62 61 73 65 20 70 61  tes: Database pa
10350 67 65 20 73 69 7a 65 2e 0a 2a 2a 20 0a 2a 2a 20  ge size..** .** 
10351 46 6f 6c 6c 6f 77 65 64 20 62 79 20 28 4a 4f 55  Followed by (JOU
10352 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 2d 20 32 38  RNAL_HDR_SZ - 28
10353 29 20 62 79 74 65 73 20 6f 66 20 75 6e 75 73 65  ) bytes of unuse
10354 64 20 73 70 61 63 65 2e 0a 2a 2f 0a 73 74 61 74  d space..*/.stat
10355 69 63 20 69 6e 74 20 77 72 69 74 65 4a 6f 75 72  ic int writeJour
10356 6e 61 6c 48 64 72 28 50 61 67 65 72 20 2a 70 50  nalHdr(Pager *pP
10357 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ager){.  int rc 
10358 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63  = SQLITE_OK;.  c
10359 68 61 72 20 2a 7a 48 65 61 64 65 72 20 3d 20 70  har *zHeader = p
1035a 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65  Pager->pTmpSpace
1035b 3b 0a 20 20 75 33 32 20 6e 48 65 61 64 65 72 20  ;.  u32 nHeader 
1035c 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  = pPager->pageSi
1035d 7a 65 3b 0a 20 20 75 33 32 20 6e 57 72 69 74 65  ze;.  u32 nWrite
1035e 3b 0a 0a 20 20 69 66 28 20 6e 48 65 61 64 65 72  ;..  if( nHeader
1035f 3e 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f 53 5a 28  >JOURNAL_HDR_SZ(
10360 70 50 61 67 65 72 29 20 29 7b 0a 20 20 20 20 6e  pPager) ){.    n
10361 48 65 61 64 65 72 20 3d 20 4a 4f 55 52 4e 41 4c  Header = JOURNAL
10362 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b  _HDR_SZ(pPager);
10363 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 67  .  }..  if( pPag
10364 65 72 2d 3e 73 74 6d 74 48 64 72 4f 66 66 3d 3d  er->stmtHdrOff==
10365 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  0 ){.    pPager-
10366 3e 73 74 6d 74 48 64 72 4f 66 66 20 3d 20 70 50  >stmtHdrOff = pP
10367 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
10368 3b 0a 20 20 7d 0a 0a 20 20 73 65 65 6b 4a 6f 75  ;.  }..  seekJou
10369 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b  rnalHdr(pPager);
1036a 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
1036b 61 6c 48 64 72 20 3d 20 70 50 61 67 65 72 2d 3e  alHdr = pPager->
1036c 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 6d  journalOff;..  m
1036d 65 6d 63 70 79 28 7a 48 65 61 64 65 72 2c 20 61  emcpy(zHeader, a
1036e 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
1036f 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
10370 69 63 29 29 3b 0a 0a 20 20 2f 2a 20 0a 20 20 2a  ic));..  /* .  *
10371 2a 20 57 72 69 74 65 20 74 68 65 20 6e 52 65 63  * Write the nRec
10372 20 46 69 65 6c 64 20 2d 20 74 68 65 20 6e 75 6d   Field - the num
10373 62 65 72 20 6f 66 20 70 61 67 65 20 72 65 63 6f  ber of page reco
10374 72 64 73 20 74 68 61 74 20 66 6f 6c 6c 6f 77 20  rds that follow 
10375 74 68 69 73 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61  this.  ** journa
10376 6c 20 68 65 61 64 65 72 2e 20 4e 6f 72 6d 61 6c  l header. Normal
10377 6c 79 2c 20 7a 65 72 6f 20 69 73 20 77 72 69 74  ly, zero is writ
10378 74 65 6e 20 74 6f 20 74 68 69 73 20 76 61 6c 75  ten to this valu
10379 65 20 61 74 20 74 68 69 73 20 74 69 6d 65 2e 0a  e at this time..
1037a 20 20 2a 2a 20 41 66 74 65 72 20 74 68 65 20 72    ** After the r
1037b 65 63 6f 72 64 73 20 61 72 65 20 61 64 64 65 64  ecords are added
1037c 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   to the journal 
1037d 28 61 6e 64 20 74 68 65 20 6a 6f 75 72 6e 61 6c  (and the journal
1037e 20 73 79 6e 63 65 64 2c 20 0a 20 20 2a 2a 20 69   synced, .  ** i
1037f 66 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63 20 6d  f in full-sync m
10380 6f 64 65 29 2c 20 74 68 65 20 7a 65 72 6f 20 69  ode), the zero i
10381 73 20 6f 76 65 72 77 72 69 74 74 65 6e 20 77 69  s overwritten wi
10382 74 68 20 74 68 65 20 74 72 75 65 20 6e 75 6d 62  th the true numb
10383 65 72 0a 20 20 2a 2a 20 6f 66 20 72 65 63 6f 72  er.  ** of recor
10384 64 73 20 28 73 65 65 20 73 79 6e 63 4a 6f 75 72  ds (see syncJour
10385 6e 61 6c 28 29 29 2e 0a 20 20 2a 2a 0a 20 20 2a  nal())..  **.  *
10386 2a 20 41 20 66 61 73 74 65 72 20 61 6c 74 65 72  * A faster alter
10387 6e 61 74 69 76 65 20 69 73 20 74 6f 20 77 72 69  native is to wri
10388 74 65 20 30 78 46 46 46 46 46 46 46 46 20 74 6f  te 0xFFFFFFFF to
10389 20 74 68 65 20 6e 52 65 63 20 66 69 65 6c 64 2e   the nRec field.
1038a 20 57 68 65 6e 0a 20 20 2a 2a 20 72 65 61 64 69   When.  ** readi
1038b 6e 67 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74  ng the journal t
1038c 68 69 73 20 76 61 6c 75 65 20 74 65 6c 6c 73 20  his value tells 
1038d 53 51 4c 69 74 65 20 74 6f 20 61 73 73 75 6d 65  SQLite to assume
1038e 20 74 68 61 74 20 74 68 65 0a 20 20 2a 2a 20 72   that the.  ** r
1038f 65 73 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  est of the journ
10390 61 6c 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  al file contains
10391 20 76 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f   valid page reco
10392 72 64 73 2e 20 54 68 69 73 20 61 73 73 75 6d 70  rds. This assump
10393 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 64 61 6e  tion.  ** is dan
10394 67 65 72 6f 75 73 2c 20 61 73 20 69 66 20 61 20  gerous, as if a 
10395 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 20  failure occured 
10396 77 68 69 6c 73 74 20 77 72 69 74 69 6e 67 20 74  whilst writing t
10397 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a 20 20  o the journal.  
10398 2a 2a 20 66 69 6c 65 20 69 74 20 6d 61 79 20 63  ** file it may c
10399 6f 6e 74 61 69 6e 20 73 6f 6d 65 20 67 61 72 62  ontain some garb
1039a 61 67 65 20 64 61 74 61 2e 20 54 68 65 72 65 20  age data. There 
1039b 61 72 65 20 74 77 6f 20 73 63 65 6e 61 72 69 6f  are two scenario
1039c 73 0a 20 20 2a 2a 20 77 68 65 72 65 20 74 68 69  s.  ** where thi
1039d 73 20 72 69 73 6b 20 63 61 6e 20 62 65 20 69 67  s risk can be ig
1039e 6e 6f 72 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  nored:.  **.  **
1039f 20 20 20 2a 20 57 68 65 6e 20 74 68 65 20 70 61     * When the pa
103a0 67 65 72 20 69 73 20 69 6e 20 6e 6f 2d 73 79 6e  ger is in no-syn
103a1 63 20 6d 6f 64 65 2e 20 43 6f 72 72 75 70 74 69  c mode. Corrupti
103a2 6f 6e 20 63 61 6e 20 66 6f 6c 6c 6f 77 20 61 0a  on can follow a.
103a3 20 20 2a 2a 20 20 20 20 20 70 6f 77 65 72 20 66    **     power f
103a4 61 69 6c 75 72 65 20 69 6e 20 74 68 69 73 20 63  ailure in this c
103a5 61 73 65 20 61 6e 79 77 61 79 2e 0a 20 20 2a 2a  ase anyway..  **
103a6 0a 20 20 2a 2a 20 20 20 2a 20 57 68 65 6e 20 74  .  **   * When t
103a7 68 65 20 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  he SQLITE_IOCAP_
103a8 53 41 46 45 5f 41 50 50 45 4e 44 20 66 6c 61 67  SAFE_APPEND flag
103a9 20 69 73 20 73 65 74 2e 20 54 68 69 73 20 67 75   is set. This gu
103aa 61 72 61 6e 74 65 65 73 0a 20 20 2a 2a 20 20 20  arantees.  **   
103ab 20 20 74 68 61 74 20 67 61 72 62 61 67 65 20 64    that garbage d
103ac 61 74 61 20 69 73 20 6e 65 76 65 72 20 61 70 70  ata is never app
103ad 65 6e 64 65 64 20 74 6f 20 74 68 65 20 6a 6f 75  ended to the jou
103ae 72 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 2a 2f 0a  rnal file..  */.
103af 20 20 61 73 73 65 72 74 28 70 50 61 67 65 72 2d    assert(pPager-
103b0 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70  >fd->pMethods||p
103b1 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 29 3b 0a  Pager->noSync);.
103b2 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 6e    if( (pPager->n
103b3 6f 53 79 6e 63 29 20 7c 7c 20 28 70 50 61 67 65  oSync) || (pPage
103b4 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d  r->journalMode==
103b5 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
103b6 45 5f 4d 45 4d 4f 52 59 29 0a 20 20 20 7c 7c 20  E_MEMORY).   || 
103b7 28 73 71 6c 69 74 65 33 4f 73 44 65 76 69 63 65  (sqlite3OsDevice
103b8 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 28  Characteristics(
103b9 70 50 61 67 65 72 2d 3e 66 64 29 26 53 51 4c 49  pPager->fd)&SQLI
103ba 54 45 5f 49 4f 43 41 50 5f 53 41 46 45 5f 41 50  TE_IOCAP_SAFE_AP
103bb 50 45 4e 44 29 20 0a 20 20 29 7b 0a 20 20 20 20  PEND) .  ){.    
103bc 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
103bd 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
103be 61 6c 4d 61 67 69 63 29 5d 2c 20 30 78 66 66 66  alMagic)], 0xfff
103bf 66 66 66 66 66 29 3b 0a 20 20 7d 65 6c 73 65 7b  fffff);.  }else{
103c0 0a 20 20 20 20 70 75 74 33 32 62 69 74 73 28 26  .    put32bits(&
103c1 7a 48 65 61 64 65 72 5b 73 69 7a 65 6f 66 28 61  zHeader[sizeof(a
103c2 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 5d 2c 20  JournalMagic)], 
103c3 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68  0);.  }..  /* Th
103c4 65 20 72 61 6e 64 6f 6d 20 63 68 65 63 6b 2d 68  e random check-h
103c5 61 73 68 20 69 6e 69 74 69 61 6c 69 73 65 72 20  ash initialiser 
103c6 2a 2f 20 0a 20 20 73 71 6c 69 74 65 33 5f 72 61  */ .  sqlite3_ra
103c7 6e 64 6f 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28  ndomness(sizeof(
103c8 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
103c9 74 29 2c 20 26 70 50 61 67 65 72 2d 3e 63 6b 73  t), &pPager->cks
103ca 75 6d 49 6e 69 74 29 3b 0a 20 20 70 75 74 33 32  umInit);.  put32
103cb 62 69 74 73 28 26 7a 48 65 61 64 65 72 5b 73 69  bits(&zHeader[si
103cc 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
103cd 69 63 29 2b 34 5d 2c 20 70 50 61 67 65 72 2d 3e  ic)+4], pPager->
103ce 63 6b 73 75 6d 49 6e 69 74 29 3b 0a 20 20 2f 2a  cksumInit);.  /*
103cf 20 54 68 65 20 69 6e 69 74 69 61 6c 20 64 61 74   The initial dat
103d0 61 62 61 73 65 20 73 69 7a 65 20 2a 2f 0a 20 20  abase size */.  
103d1 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64  put32bits(&zHead
103d2 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e  er[sizeof(aJourn
103d3 61 6c 4d 61 67 69 63 29 2b 38 5d 2c 20 70 50 61  alMagic)+8], pPa
103d4 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b 0a 20 20  ger->dbSize);.  
103d5 2f 2a 20 54 68 65 20 61 73 73 75 6d 65 64 20 73  /* The assumed s
103d6 65 63 74 6f 72 20 73 69 7a 65 20 66 6f 72 20 74  ector size for t
103d7 68 69 73 20 70 72 6f 63 65 73 73 20 2a 2f 0a 20  his process */. 
103d8 20 70 75 74 33 32 62 69 74 73 28 26 7a 48 65 61   put32bits(&zHea
103d9 64 65 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72  der[sizeof(aJour
103da 6e 61 6c 4d 61 67 69 63 29 2b 31 32 5d 2c 20 70  nalMagic)+12], p
103db 50 61 67 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a  Pager->sectorSiz
103dc 65 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72  e);.  if( pPager
103dd 2d 3e 6a 6f 75 72 6e 61 6c 48 64 72 3d 3d 30 20  ->journalHdr==0 
103de 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ){.    /* The pa
103df 67 65 20 73 69 7a 65 20 2a 2f 0a 20 20 20 20 70  ge size */.    p
103e0 75 74 33 32 62 69 74 73 28 26 7a 48 65 61 64 65  ut32bits(&zHeade
103e1 72 5b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61  r[sizeof(aJourna
103e2 6c 4d 61 67 69 63 29 2b 31 36 5d 2c 20 70 50 61  lMagic)+16], pPa
103e3 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  ger->pageSize);.
103e4 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 57 72 69 74    }..  for(nWrit
103e5 65 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  e=0; rc==SQLITE_
103e6 4f 4b 26 26 6e 57 72 69 74 65 3c 4a 4f 55 52 4e  OK&&nWrite<JOURN
103e7 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
103e8 29 3b 20 6e 57 72 69 74 65 2b 3d 6e 48 65 61 64  ); nWrite+=nHead
103e9 65 72 29 7b 0a 20 20 20 20 49 4f 54 52 41 43 45  er){.    IOTRACE
103ea 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64 20  (("JHDR %p %lld 
103eb 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 70  %d\n", pPager, p
103ec 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 48 64  Pager->journalHd
103ed 72 2c 20 6e 48 65 61 64 65 72 29 29 0a 20 20 20  r, nHeader)).   
103ee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
103ef 72 69 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  rite(pPager->jfd
103f0 2c 20 7a 48 65 61 64 65 72 2c 20 6e 48 65 61 64  , zHeader, nHead
103f1 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  er, pPager->jour
103f2 6e 61 6c 4f 66 66 29 3b 0a 20 20 20 20 70 50 61  nalOff);.    pPa
103f3 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20  ger->journalOff 
103f4 2b 3d 20 6e 48 65 61 64 65 72 3b 0a 20 20 7d 0a  += nHeader;.  }.
103f5 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
103f6 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e  ./*.** The journ
103f7 61 6c 20 66 69 6c 65 20 6d 75 73 74 20 62 65 20  al file must be 
103f8 6f 70 65 6e 20 77 68 65 6e 20 74 68 69 73 20 69  open when this i
103f9 73 20 63 61 6c 6c 65 64 2e 20 41 20 6a 6f 75 72  s called. A jour
103fa 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c 65 0a  nal header file.
103fb 2a 2a 20 28 4a 4f 55 52 4e 41 4c 5f 48 44 52 5f  ** (JOURNAL_HDR_
103fc 53 5a 20 62 79 74 65 73 29 20 69 73 20 72 65 61  SZ bytes) is rea
103fd 64 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72 65  d from the curre
103fe 6e 74 20 6c 6f 63 61 74 69 6f 6e 20 69 6e 20 74  nt location in t
103ff 68 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66 69  he journal.** fi
10400 6c 65 2e 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73  le. See comments
10401 20 61 62 6f 76 65 20 66 75 6e 63 74 69 6f 6e 20   above function 
10402 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72 28  writeJournalHdr(
10403 29 20 66 6f 72 20 61 20 64 65 73 63 72 69 70 74  ) for a descript
10404 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 6a 6f  ion of.** the jo
10405 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 6f 72  urnal header for
10406 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  mat..**.** If th
10407 65 20 68 65 61 64 65 72 20 69 73 20 72 65 61 64  e header is read
10408 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2c 20 2a   successfully, *
10409 6e 52 65 63 20 69 73 20 73 65 74 20 74 6f 20 74  nRec is set to t
1040a 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a 20  he number of.** 
1040b 70 61 67 65 20 72 65 63 6f 72 64 73 20 66 6f 6c  page records fol
1040c 6c 6f 77 69 6e 67 20 74 68 69 73 20 68 65 61 64  lowing this head
1040d 65 72 20 61 6e 64 20 2a 64 62 53 69 7a 65 20 69  er and *dbSize i
1040e 73 20 73 65 74 20 74 6f 20 74 68 65 20 73 69 7a  s set to the siz
1040f 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74 61  e of the.** data
10410 62 61 73 65 20 62 65 66 6f 72 65 20 74 68 65 20  base before the 
10411 74 72 61 6e 73 61 63 74 69 6f 6e 20 62 65 67 61  transaction bega
10412 6e 2c 20 69 6e 20 70 61 67 65 73 2e 20 41 6c 73  n, in pages. Als
10413 6f 2c 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d  o, pPager->cksum
10414 49 6e 69 74 0a 2a 2a 20 69 73 20 73 65 74 20 74  Init.** is set t
10415 6f 20 74 68 65 20 76 61 6c 75 65 20 72 65 61 64  o the value read
10416 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e 61   from the journa
10417 6c 20 68 65 61 64 65 72 2e 20 53 51 4c 49 54 45  l header. SQLITE
10418 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 0a  _OK is returned.
10419 2a 2a 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  ** in this case.
1041a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6a 6f  .**.** If the jo
1041b 75 72 6e 61 6c 20 68 65 61 64 65 72 20 66 69 6c  urnal header fil
1041c 65 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  e appears to be 
1041d 63 6f 72 72 75 70 74 65 64 2c 20 53 51 4c 49 54  corrupted, SQLIT
1041e 45 5f 44 4f 4e 45 20 69 73 0a 2a 2a 20 72 65 74  E_DONE is.** ret
1041f 75 72 6e 65 64 20 61 6e 64 20 2a 6e 52 65 63 20  urned and *nRec 
10420 61 6e 64 20 2a 64 62 53 69 7a 65 20 61 72 65 20  and *dbSize are 
10421 6e 6f 74 20 73 65 74 2e 20 20 49 66 20 4a 4f 55  not set.  If JOU
10422 52 4e 41 4c 5f 48 44 52 5f 53 5a 20 62 79 74 65  RNAL_HDR_SZ byte
10423 73 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 72  s.** cannot be r
10424 65 61 64 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75  ead from the jou
10425 72 6e 61 6c 20 66 69 6c 65 20 61 6e 20 65 72 72  rnal file an err
10426 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75 72  or code is retur
10427 6e 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ned..*/.static i
10428 6e 74 20 72 65 61 64 4a 6f 75 72 6e 61 6c 48 64  nt readJournalHd
10429 72 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  r(.  Pager *pPag
1042a 65 72 2c 20 0a 20 20 69 36 34 20 6a 6f 75 72 6e  er, .  i64 journ
1042b 61 6c 53 69 7a 65 2c 0a 20 20 75 33 32 20 2a 70  alSize,.  u32 *p
1042c 4e 52 65 63 2c 20 0a 20 20 75 33 32 20 2a 70 44  NRec, .  u32 *pD
1042d 62 53 69 7a 65 0a 29 7b 0a 20 20 69 6e 74 20 72  bSize.){.  int r
1042e 63 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  c;.  unsigned ch
1042f 61 72 20 61 4d 61 67 69 63 5b 38 5d 3b 20 2f 2a  ar aMagic[8]; /*
10430 20 41 20 62 75 66 66 65 72 20 74 6f 20 68 6f 6c   A buffer to hol
10431 64 20 74 68 65 20 6d 61 67 69 63 20 68 65 61 64  d the magic head
10432 65 72 20 2a 2f 0a 20 20 69 36 34 20 6a 72 6e 6c  er */.  i64 jrnl
10433 4f 66 66 3b 0a 20 20 69 6e 74 20 69 50 61 67 65  Off;.  int iPage
10434 53 69 7a 65 3b 0a 0a 20 20 73 65 65 6b 4a 6f 75  Size;..  seekJou
10435 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b  rnalHdr(pPager);
10436 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a  .  if( pPager->j
10437 6f 75 72 6e 61 6c 4f 66 66 2b 4a 4f 55 52 4e 41  ournalOff+JOURNA
10438 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29  L_HDR_SZ(pPager)
10439 20 3e 20 6a 6f 75 72 6e 61 6c 53 69 7a 65 20 29   > journalSize )
1043a 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1043b 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20  ITE_DONE;.  }.  
1043c 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 61 67 65 72  jrnlOff = pPager
1043d 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20  ->journalOff;.. 
1043e 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
1043f 65 61 64 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c  ead(pPager->jfd,
10440 20 61 4d 61 67 69 63 2c 20 73 69 7a 65 6f 66 28   aMagic, sizeof(
10441 61 4d 61 67 69 63 29 2c 20 6a 72 6e 6c 4f 66 66  aMagic), jrnlOff
10442 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72 65  );.  if( rc ) re
10443 74 75 72 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f  turn rc;.  jrnlO
10444 66 66 20 2b 3d 20 73 69 7a 65 6f 66 28 61 4d 61  ff += sizeof(aMa
10445 67 69 63 29 3b 0a 0a 20 20 69 66 28 20 6d 65 6d  gic);..  if( mem
10446 63 6d 70 28 61 4d 61 67 69 63 2c 20 61 4a 6f 75  cmp(aMagic, aJou
10447 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69 7a 65 6f  rnalMagic, sizeo
10448 66 28 61 4d 61 67 69 63 29 29 21 3d 30 20 29 7b  f(aMagic))!=0 ){
10449 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
1044a 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20 20  TE_DONE;.  }..  
1044b 72 63 20 3d 20 72 65 61 64 33 32 62 69 74 73 28  rc = read32bits(
1044c 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e  pPager->jfd, jrn
1044d 6c 4f 66 66 2c 20 70 4e 52 65 63 29 3b 0a 20 20  lOff, pNRec);.  
1044e 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1044f 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61 64  rc;..  rc = read
10450 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
10451 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 34 2c 20 26  fd, jrnlOff+4, &
10452 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d 49 6e 69  pPager->cksumIni
10453 74 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 72  t);.  if( rc ) r
10454 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
10455 3d 20 72 65 61 64 33 32 62 69 74 73 28 70 50 61  = read32bits(pPa
10456 67 65 72 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66  ger->jfd, jrnlOf
10457 66 2b 38 2c 20 70 44 62 53 69 7a 65 29 3b 0a 20  f+8, pDbSize);. 
10458 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
10459 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 61   rc;..  rc = rea
1045a 64 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  d32bits(pPager->
1045b 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 31 36 2c  jfd, jrnlOff+16,
1045c 20 28 75 33 32 20 2a 29 26 69 50 61 67 65 53 69   (u32 *)&iPageSi
1045d 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53  ze);.  if( rc==S
1045e 51 4c 49 54 45 5f 4f 4b 20 0a 20 20 20 26 26 20  QLITE_OK .   && 
1045f 69 50 61 67 65 53 69 7a 65 3e 3d 35 31 32 20 0a  iPageSize>=512 .
10460 20 20 20 26 26 20 69 50 61 67 65 53 69 7a 65 3c     && iPageSize<
10461 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45  =SQLITE_MAX_PAGE
10462 5f 53 49 5a 45 20 0a 20 20 20 26 26 20 28 28 69  _SIZE .   && ((i
10463 50 61 67 65 53 69 7a 65 2d 31 29 26 69 50 61 67  PageSize-1)&iPag
10464 65 53 69 7a 65 29 3d 3d 30 20 0a 20 20 29 7b 0a  eSize)==0 .  ){.
10465 20 20 20 20 75 31 36 20 70 61 67 65 73 69 7a 65      u16 pagesize
10466 20 3d 20 69 50 61 67 65 53 69 7a 65 3b 0a 20 20   = iPageSize;.  
10467 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
10468 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70  gerSetPagesize(p
10469 50 61 67 65 72 2c 20 26 70 61 67 65 73 69 7a 65  Pager, &pagesize
1046a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 20  );.  }.  if( rc 
1046b 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
1046c 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61 73  /* Update the as
1046d 73 75 6d 65 64 20 73 65 63 74 6f 72 2d 73 69 7a  sumed sector-siz
1046e 65 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 76  e to match the v
1046f 61 6c 75 65 20 75 73 65 64 20 62 79 20 0a 20 20  alue used by .  
10470 2a 2a 20 74 68 65 20 70 72 6f 63 65 73 73 20 74  ** the process t
10471 68 61 74 20 63 72 65 61 74 65 64 20 74 68 69 73  hat created this
10472 20 6a 6f 75 72 6e 61 6c 2e 20 49 66 20 74 68 69   journal. If thi
10473 73 20 6a 6f 75 72 6e 61 6c 20 77 61 73 0a 20 20  s journal was.  
10474 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20  ** created by a 
10475 70 72 6f 63 65 73 73 20 6f 74 68 65 72 20 74 68  process other th
10476 61 6e 20 74 68 69 73 20 6f 6e 65 2c 20 74 68 65  an this one, the
10477 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 20  n this routine. 
10478 20 2a 2a 20 69 73 20 62 65 69 6e 67 20 63 61 6c   ** is being cal
10479 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
1047a 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 29  pager_playback()
1047b 2e 20 54 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75  . The local valu
1047c 65 0a 20 20 2a 2a 20 6f 66 20 50 61 67 65 72 2e  e.  ** of Pager.
1047d 73 65 63 74 6f 72 53 69 7a 65 20 69 73 20 72 65  sectorSize is re
1047e 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65 6e  stored at the en
1047f 64 20 6f 66 20 74 68 61 74 20 72 6f 75 74 69 6e  d of that routin
10480 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 72  e..  */.  rc = r
10481 65 61 64 33 32 62 69 74 73 28 70 50 61 67 65 72  ead32bits(pPager
10482 2d 3e 6a 66 64 2c 20 6a 72 6e 6c 4f 66 66 2b 31  ->jfd, jrnlOff+1
10483 32 2c 20 26 70 50 61 67 65 72 2d 3e 73 65 63 74  2, &pPager->sect
10484 6f 72 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 72  orSize);.  if( r
10485 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
10486 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e 73 65   if( (pPager->se
10487 63 74 6f 72 53 69 7a 65 20 26 20 28 70 50 61 67  ctorSize & (pPag
10488 65 72 2d 3e 73 65 63 74 6f 72 53 69 7a 65 2d 31  er->sectorSize-1
10489 29 29 21 3d 30 0a 20 20 20 20 20 20 20 20 20 7c  ))!=0.         |
1048a 7c 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f 72  | pPager->sector
1048b 53 69 7a 65 3e 30 78 31 30 30 30 30 30 30 20 29  Size>0x1000000 )
1048c 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1048d 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 0a 20  ITE_DONE;.  }.. 
1048e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
1048f 4f 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 48  Off += JOURNAL_H
10490 44 52 5f 53 5a 28 70 50 61 67 65 72 29 3b 0a 20  DR_SZ(pPager);. 
10491 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
10492 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 57 72 69  K;.}.../*.** Wri
10493 74 65 20 74 68 65 20 73 75 70 70 6c 69 65 64 20  te the supplied 
10494 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 6e  master journal n
10495 61 6d 65 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ame into the jou
10496 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 61  rnal file for pa
10497 67 65 72 0a 2a 2a 20 70 50 61 67 65 72 20 61 74  ger.** pPager at
10498 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 6f 63   the current loc
10499 61 74 69 6f 6e 2e 20 54 68 65 20 6d 61 73 74 65  ation. The maste
1049a 72 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 6d  r journal name m
1049b 75 73 74 20 62 65 20 74 68 65 20 6c 61 73 74 0a  ust be the last.
1049c 2a 2a 20 74 68 69 6e 67 20 77 72 69 74 74 65 6e  ** thing written
1049d 20 74 6f 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69   to a journal fi
1049e 6c 65 2e 20 49 66 20 74 68 65 20 70 61 67 65 72  le. If the pager
1049f 20 69 73 20 69 6e 20 66 75 6c 6c 2d 73 79 6e 63   is in full-sync
104a0 20 6d 6f 64 65 2c 20 74 68 65 0a 2a 2a 20 6a 6f   mode, the.** jo
104a1 75 72 6e 61 6c 20 66 69 6c 65 20 64 65 73 63 72  urnal file descr
104a2 69 70 74 6f 72 20 69 73 20 61 64 76 61 6e 63 65  iptor is advance
104a3 64 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 65  d to the next se
104a4 63 74 6f 72 20 62 6f 75 6e 64 61 72 79 20 62 65  ctor boundary be
104a5 66 6f 72 65 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  fore.** anything
104a6 20 69 73 20 77 72 69 74 74 65 6e 2e 20 54 68 65   is written. The
104a7 20 66 6f 72 6d 61 74 20 69 73 3a 0a 2a 2a 0a 2a   format is:.**.*
104a8 2a 20 2b 20 34 20 62 79 74 65 73 3a 20 50 41 47  * + 4 bytes: PAG
104a9 45 52 5f 4d 4a 5f 50 47 4e 4f 2e 0a 2a 2a 20 2b  ER_MJ_PGNO..** +
104aa 20 4e 20 62 79 74 65 73 3a 20 6c 65 6e 67 74 68   N bytes: length
104ab 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
104ac 61 6c 20 6e 61 6d 65 2e 0a 2a 2a 20 2b 20 34 20  al name..** + 4 
104ad 62 79 74 65 73 3a 20 4e 0a 2a 2a 20 2b 20 34 20  bytes: N.** + 4 
104ae 62 79 74 65 73 3a 20 4d 61 73 74 65 72 20 6a 6f  bytes: Master jo
104af 75 72 6e 61 6c 20 6e 61 6d 65 20 63 68 65 63 6b  urnal name check
104b0 73 75 6d 2e 0a 2a 2a 20 2b 20 38 20 62 79 74 65  sum..** + 8 byte
104b1 73 3a 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63  s: aJournalMagic
104b2 5b 5d 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61  []..**.** The ma
104b3 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70 61 67  ster journal pag
104b4 65 20 63 68 65 63 6b 73 75 6d 20 69 73 20 74 68  e checksum is th
104b5 65 20 73 75 6d 20 6f 66 20 74 68 65 20 62 79 74  e sum of the byt
104b6 65 73 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72  es in the master
104b7 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65  .** journal name
104b8 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 4d 61 73 74  ..**.** If zMast
104b9 65 72 20 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69  er is a NULL poi
104ba 6e 74 65 72 20 28 6f 63 63 75 72 73 20 66 6f 72  nter (occurs for
104bb 20 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61   a single databa
104bc 73 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 29 2c  se transaction),
104bd 20 0a 2a 2a 20 74 68 69 73 20 63 61 6c 6c 20 69   .** this call i
104be 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
104bf 61 74 69 63 20 69 6e 74 20 77 72 69 74 65 4d 61  atic int writeMa
104c0 73 74 65 72 4a 6f 75 72 6e 61 6c 28 50 61 67 65  sterJournal(Page
104c1 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73 74  r *pPager, const
104c2 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29 7b   char *zMaster){
104c3 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
104c4 20 6c 65 6e 3b 20 0a 20 20 69 6e 74 20 69 3b 20   len; .  int i; 
104c5 0a 20 20 69 36 34 20 6a 72 6e 6c 4f 66 66 3b 0a  .  i64 jrnlOff;.
104c6 20 20 69 36 34 20 6a 72 6e 6c 53 69 7a 65 3b 0a    i64 jrnlSize;.
104c7 20 20 75 33 32 20 63 6b 73 75 6d 20 3d 20 30 3b    u32 cksum = 0;
104c8 0a 20 20 63 68 61 72 20 7a 42 75 66 5b 73 69 7a  .  char zBuf[siz
104c9 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69  eof(aJournalMagi
104ca 63 29 2b 32 2a 34 5d 3b 0a 0a 20 20 69 66 28 20  c)+2*4];..  if( 
104cb 21 7a 4d 61 73 74 65 72 20 7c 7c 20 70 50 61 67  !zMaster || pPag
104cc 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 20  er->setMaster ) 
104cd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
104ce 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ;.  if( pPager->
104cf 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41 47  journalMode==PAG
104d0 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 4d  ER_JOURNALMODE_M
104d1 45 4d 4f 52 59 20 29 20 72 65 74 75 72 6e 20 53  EMORY ) return S
104d2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 70 50 61 67  QLITE_OK;.  pPag
104d3 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 3d 20  er->setMaster = 
104d4 31 3b 0a 0a 20 20 6c 65 6e 20 3d 20 73 74 72 6c  1;..  len = strl
104d5 65 6e 28 7a 4d 61 73 74 65 72 29 3b 0a 20 20 66  en(zMaster);.  f
104d6 6f 72 28 69 3d 30 3b 20 69 3c 6c 65 6e 3b 20 69  or(i=0; i<len; i
104d7 2b 2b 29 7b 0a 20 20 20 20 63 6b 73 75 6d 20 2b  ++){.    cksum +
104d8 3d 20 7a 4d 61 73 74 65 72 5b 69 5d 3b 0a 20 20  = zMaster[i];.  
104d9 7d 0a 0a 20 20 2f 2a 20 49 66 20 69 6e 20 66 75  }..  /* If in fu
104da 6c 6c 2d 73 79 6e 63 20 6d 6f 64 65 2c 20 61 64  ll-sync mode, ad
104db 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
104dc 74 20 64 69 73 6b 20 73 65 63 74 6f 72 20 62 65  t disk sector be
104dd 66 6f 72 65 20 77 72 69 74 69 6e 67 0a 20 20 2a  fore writing.  *
104de 2a 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  * the master jou
104df 72 6e 61 6c 20 6e 61 6d 65 2e 20 54 68 69 73 20  rnal name. This 
104e0 69 73 20 69 6e 20 63 61 73 65 20 74 68 65 20 70  is in case the p
104e1 72 65 76 69 6f 75 73 20 70 61 67 65 20 77 72 69  revious page wri
104e2 74 74 65 6e 20 74 6f 0a 20 20 2a 2a 20 74 68 65  tten to.  ** the
104e3 20 6a 6f 75 72 6e 61 6c 20 68 61 73 20 61 6c 72   journal has alr
104e4 65 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64  eady been synced
104e5 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
104e6 67 65 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 29 7b  ger->fullSync ){
104e7 0a 20 20 20 20 73 65 65 6b 4a 6f 75 72 6e 61 6c  .    seekJournal
104e8 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  Hdr(pPager);.  }
104e9 0a 20 20 6a 72 6e 6c 4f 66 66 20 3d 20 70 50 61  .  jrnlOff = pPa
104ea 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b  ger->journalOff;
104eb 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
104ec 61 6c 4f 66 66 20 2b 3d 20 28 6c 65 6e 2b 32 30  alOff += (len+20
104ed 29 3b 0a 0a 20 20 72 63 20 3d 20 77 72 69 74 65  );..  rc = write
104ee 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
104ef 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 50 41 47  fd, jrnlOff, PAG
104f0 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67 65  ER_MJ_PGNO(pPage
104f1 72 29 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  r));.  if( rc!=S
104f2 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
104f3 6e 20 72 63 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20  n rc;.  jrnlOff 
104f4 2b 3d 20 34 3b 0a 0a 20 20 72 63 20 3d 20 73 71  += 4;..  rc = sq
104f5 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61  lite3OsWrite(pPa
104f6 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73 74 65  ger->jfd, zMaste
104f7 72 2c 20 6c 65 6e 2c 20 6a 72 6e 6c 4f 66 66 29  r, len, jrnlOff)
104f8 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
104f9 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
104fa 63 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20  c;.  jrnlOff += 
104fb 6c 65 6e 3b 0a 0a 20 20 70 75 74 33 32 62 69 74  len;..  put32bit
104fc 73 28 7a 42 75 66 2c 20 6c 65 6e 29 3b 0a 20 20  s(zBuf, len);.  
104fd 70 75 74 33 32 62 69 74 73 28 26 7a 42 75 66 5b  put32bits(&zBuf[
104fe 34 5d 2c 20 63 6b 73 75 6d 29 3b 0a 20 20 6d 65  4], cksum);.  me
104ff 6d 63 70 79 28 26 7a 42 75 66 5b 38 5d 2c 20 61  mcpy(&zBuf[8], a
10500 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 2c 20 73 69  JournalMagic, si
10501 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67  zeof(aJournalMag
10502 69 63 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  ic));.  rc = sql
10503 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
10504 65 72 2d 3e 6a 66 64 2c 20 7a 42 75 66 2c 20 38  er->jfd, zBuf, 8
10505 2b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  +sizeof(aJournal
10506 4d 61 67 69 63 29 2c 20 6a 72 6e 6c 4f 66 66 29  Magic), jrnlOff)
10507 3b 0a 20 20 6a 72 6e 6c 4f 66 66 20 2b 3d 20 38  ;.  jrnlOff += 8
10508 2b 73 69 7a 65 6f 66 28 61 4a 6f 75 72 6e 61 6c  +sizeof(aJournal
10509 4d 61 67 69 63 29 3b 0a 20 20 70 50 61 67 65 72  Magic);.  pPager
1050a 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 21 70 50  ->needSync = !pP
1050b 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 0a 20  ager->noSync;.. 
1050c 20 2f 2a 20 49 66 20 74 68 65 20 70 61 67 65 72   /* If the pager
1050d 20 69 73 20 69 6e 20 70 65 72 69 73 74 65 6e 74   is in peristent
1050e 2d 6a 6f 75 72 6e 61 6c 20 6d 6f 64 65 2c 20 74  -journal mode, t
1050f 68 65 6e 20 74 68 65 20 70 68 79 73 69 63 61 6c  hen the physical
10510 20 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 66   .  ** journal-f
10511 69 6c 65 20 6d 61 79 20 65 78 74 65 6e 64 20 70  ile may extend p
10512 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74  ast the end of t
10513 68 65 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e 61  he master-journa
10514 6c 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 6e 64 20  l name.  ** and 
10515 38 20 62 79 74 65 73 20 6f 66 20 6d 61 67 69 63  8 bytes of magic
10516 20 64 61 74 61 20 6a 75 73 74 20 77 72 69 74 74   data just writt
10517 65 6e 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 20  en to the file. 
10518 54 68 69 73 20 69 73 20 0a 20 20 2a 2a 20 64 61  This is .  ** da
10519 6e 67 65 72 6f 75 73 20 62 65 63 61 75 73 65 20  ngerous because 
1051a 74 68 65 20 63 6f 64 65 20 74 6f 20 72 6f 6c 6c  the code to roll
1051b 62 61 63 6b 20 61 20 68 6f 74 2d 6a 6f 75 72 6e  back a hot-journ
1051c 61 6c 20 66 69 6c 65 0a 20 20 2a 2a 20 77 69 6c  al file.  ** wil
1051d 6c 20 6e 6f 74 20 62 65 20 61 62 6c 65 20 74 6f  l not be able to
1051e 20 66 69 6e 64 20 74 68 65 20 6d 61 73 74 65 72   find the master
1051f 2d 6a 6f 75 72 6e 61 6c 20 6e 61 6d 65 20 74 6f  -journal name to
10520 20 64 65 74 65 72 6d 69 6e 65 20 0a 20 20 2a 2a   determine .  **
10521 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20   whether or not 
10522 74 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 68  the journal is h
10523 6f 74 2e 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  ot. .  **.  ** E
10524 61 73 69 65 73 74 20 74 68 69 6e 67 20 74 6f 20  asiest thing to 
10525 64 6f 20 69 6e 20 74 68 69 73 20 73 63 65 6e 61  do in this scena
10526 72 69 6f 20 69 73 20 74 6f 20 74 72 75 6e 63 61  rio is to trunca
10527 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 0a  te the journal .
10528 20 20 2a 2a 20 66 69 6c 65 20 74 6f 20 74 68 65    ** file to the
10529 20 72 65 71 75 69 72 65 64 20 73 69 7a 65 2e 0a   required size..
1052a 20 20 2a 2f 20 0a 20 20 69 66 28 20 28 72 63 3d    */ .  if( (rc=
1052b 3d 53 51 4c 49 54 45 5f 4f 4b 29 0a 20 20 20 26  =SQLITE_OK).   &
1052c 26 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  & (rc = sqlite3O
1052d 73 46 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72  sFileSize(pPager
1052e 2d 3e 6a 66 64 2c 20 26 6a 72 6e 6c 53 69 7a 65  ->jfd, &jrnlSize
1052f 29 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 0a 20 20  ))==SQLITE_OK.  
10530 20 26 26 20 6a 72 6e 6c 53 69 7a 65 3e 6a 72 6e   && jrnlSize>jrn
10531 6c 4f 66 66 0a 20 20 29 7b 0a 20 20 20 20 72 63  lOff.  ){.    rc
10532 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75 6e   = sqlite3OsTrun
10533 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  cate(pPager->jfd
10534 2c 20 6a 72 6e 6c 4f 66 66 29 3b 0a 20 20 7d 0a  , jrnlOff);.  }.
10535 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
10536 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61 20 70 61 67  /*.** Find a pag
10537 65 20 69 6e 20 74 68 65 20 68 61 73 68 20 74 61  e in the hash ta
10538 62 6c 65 20 67 69 76 65 6e 20 69 74 73 20 70 61  ble given its pa
10539 67 65 20 6e 75 6d 62 65 72 2e 20 20 52 65 74 75  ge number.  Retu
1053a 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20  rn.** a pointer 
1053b 74 6f 20 74 68 65 20 70 61 67 65 20 6f 72 20 4e  to the page or N
1053c 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64  ULL if not found
1053d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 50 67 48 64  ..*/.static PgHd
1053e 72 20 2a 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28  r *pager_lookup(
1053f 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 50  Pager *pPager, P
10540 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48  gno pgno){.  PgH
10541 64 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33  dr *p;.  sqlite3
10542 50 63 61 63 68 65 46 65 74 63 68 28 70 50 61 67  PcacheFetch(pPag
10543 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e  er->pPCache, pgn
10544 6f 2c 20 30 2c 20 26 70 29 3b 0a 20 20 72 65 74  o, 0, &p);.  ret
10545 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn p;.}../*.** 
10546 43 6c 65 61 72 20 74 68 65 20 69 6e 2d 6d 65 6d  Clear the in-mem
10547 6f 72 79 20 63 61 63 68 65 2e 20 20 54 68 69 73  ory cache.  This
10548 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 65 74 73   routine.** sets
10549 20 74 68 65 20 73 74 61 74 65 20 6f 66 20 74 68   the state of th
1054a 65 20 70 61 67 65 72 20 62 61 63 6b 20 74 6f 20  e pager back to 
1054b 77 68 61 74 20 69 74 20 77 61 73 20 77 68 65 6e  what it was when
1054c 20 69 74 20 77 61 73 20 66 69 72 73 74 0a 2a 2a   it was first.**
1054d 20 6f 70 65 6e 65 64 2e 20 20 41 6e 79 20 6f 75   opened.  Any ou
1054e 74 73 74 61 6e 64 69 6e 67 20 70 61 67 65 73 20  tstanding pages 
1054f 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 20  are invalidated 
10550 61 6e 64 20 73 75 62 73 65 71 75 65 6e 74 20 61  and subsequent a
10551 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f 20 61 63  ttempts.** to ac
10552 63 65 73 73 20 74 68 6f 73 65 20 70 61 67 65 73  cess those pages
10553 20 77 69 6c 6c 20 6c 69 6b 65 6c 79 20 72 65 73   will likely res
10554 75 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d  ult in a coredum
10555 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  p..*/.static voi
10556 64 20 70 61 67 65 72 5f 72 65 73 65 74 28 50 61  d pager_reset(Pa
10557 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
10558 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43  if( pPager->errC
10559 6f 64 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ode ) return;.  
1055a 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 65  sqlite3PcacheCle
1055b 61 72 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ar(pPager->pPCac
1055c 68 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e  he);.}../*.** Un
1055d 6c 6f 63 6b 20 74 68 65 20 64 61 74 61 62 61 73  lock the databas
1055e 65 20 66 69 6c 65 2e 20 0a 2a 2a 0a 2a 2a 20 49  e file. .**.** I
1055f 66 20 74 68 65 20 70 61 67 65 72 20 69 73 20 63  f the pager is c
10560 75 72 72 65 6e 74 6c 79 20 69 6e 20 65 72 72 6f  urrently in erro
10561 72 20 73 74 61 74 65 2c 20 64 69 73 63 61 72 64  r state, discard
10562 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
10563 20 0a 2a 2a 20 74 68 65 20 63 61 63 68 65 20 61   .** the cache a
10564 6e 64 20 72 65 73 65 74 20 74 68 65 20 50 61 67  nd reset the Pag
10565 65 72 20 73 74 72 75 63 74 75 72 65 20 69 6e 74  er structure int
10566 65 72 6e 61 6c 20 73 74 61 74 65 2e 20 49 66 20  ernal state. If 
10567 74 68 65 72 65 20 69 73 0a 2a 2a 20 61 6e 20 6f  there is.** an o
10568 70 65 6e 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65  pen journal-file
10569 2c 20 74 68 65 6e 20 74 68 65 20 6e 65 78 74 20  , then the next 
1056a 74 69 6d 65 20 61 20 73 68 61 72 65 64 2d 6c 6f  time a shared-lo
1056b 63 6b 20 69 73 20 6f 62 74 61 69 6e 65 64 0a 2a  ck is obtained.*
1056c 2a 20 6f 6e 20 74 68 65 20 70 61 67 65 72 20 66  * on the pager f
1056d 69 6c 65 20 28 62 79 20 74 68 69 73 20 6f 72 20  ile (by this or 
1056e 61 6e 79 20 6f 74 68 65 72 20 70 72 6f 63 65 73  any other proces
1056f 73 29 2c 20 69 74 20 77 69 6c 6c 20 62 65 0a 2a  s), it will be.*
10570 2a 20 74 72 65 61 74 65 64 20 61 73 20 61 20 68  * treated as a h
10571 6f 74 2d 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 72  ot-journal and r
10572 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a 2f 0a 73  olled back..*/.s
10573 74 61 74 69 63 20 76 6f 69 64 20 70 61 67 65 72  tatic void pager
10574 5f 75 6e 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70  _unlock(Pager *p
10575 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20 21 70  Pager){.  if( !p
10576 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65  Pager->exclusive
10577 4d 6f 64 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  Mode ){.    int 
10578 72 63 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50  rc = osUnlock(pP
10579 61 67 65 72 2d 3e 66 64 2c 20 4e 4f 5f 4c 4f 43  ager->fd, NO_LOC
1057a 4b 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  K);.    if( rc )
1057b 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
1057c 20 3d 20 72 63 3b 0a 20 20 20 20 70 50 61 67 65   = rc;.    pPage
1057d 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d  r->dbSizeValid =
1057e 20 30 3b 0a 20 20 20 20 49 4f 54 52 41 43 45 28   0;.    IOTRACE(
1057f 28 22 55 4e 4c 4f 43 4b 20 25 70 5c 6e 22 2c 20  ("UNLOCK %p\n", 
10580 70 50 61 67 65 72 29 29 0a 0a 20 20 20 20 2f 2a  pPager))..    /*
10581 20 41 6c 77 61 79 73 20 63 6c 6f 73 65 20 74 68   Always close th
10582 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 77  e journal file w
10583 68 65 6e 20 64 72 6f 70 70 69 6e 67 20 74 68 65  hen dropping the
10584 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 2e 0a   database lock..
10585 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69 73 65      ** Otherwise
10586 2c 20 61 6e 6f 74 68 65 72 20 63 6f 6e 6e 65 63  , another connec
10587 74 69 6f 6e 20 77 69 74 68 20 6a 6f 75 72 6e 61  tion with journa
10588 6c 5f 6d 6f 64 65 3d 64 65 6c 65 74 65 20 6d 69  l_mode=delete mi
10589 67 68 74 0a 20 20 20 20 2a 2a 20 64 65 6c 65 74  ght.    ** delet
1058a 65 20 74 68 65 20 66 69 6c 65 20 6f 75 74 20 66  e the file out f
1058b 72 6f 6d 20 75 6e 64 65 72 20 75 73 2e 0a 20 20  rom under us..  
1058c 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61    */.    if( pPa
1058d 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e  ger->journalOpen
1058e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1058f 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72 2d  3OsClose(pPager-
10590 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50 61  >jfd);.      pPa
10591 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e  ger->journalOpen
10592 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
10593 74 65 33 42 69 74 76 65 63 44 65 73 74 72 6f 79  te3BitvecDestroy
10594 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72  (pPager->pInJour
10595 6e 61 6c 29 3b 0a 20 20 20 20 20 20 70 50 61 67  nal);.      pPag
10596 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d  er->pInJournal =
10597 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
10598 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
10599 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f  Pager->pAlwaysRo
1059a 6c 6c 62 61 63 6b 29 3b 0a 20 20 20 20 20 20 70  llback);.      p
1059b 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73 52 6f  Pager->pAlwaysRo
1059c 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 20 20  llback = 0;.    
1059d 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 50 61 67  }..    /* If Pag
1059e 65 72 2e 65 72 72 43 6f 64 65 20 69 73 20 73 65  er.errCode is se
1059f 74 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  t, the contents 
105a0 6f 66 20 74 68 65 20 70 61 67 65 72 20 63 61 63  of the pager cac
105a1 68 65 20 63 61 6e 6e 6f 74 20 62 65 0a 20 20 20  he cannot be.   
105a2 20 2a 2a 20 74 72 75 73 74 65 64 2e 20 4e 6f 77   ** trusted. Now
105a3 20 74 68 61 74 20 74 68 65 20 70 61 67 65 72 20   that the pager 
105a4 66 69 6c 65 20 69 73 20 75 6e 6c 6f 63 6b 65 64  file is unlocked
105a5 2c 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f  , the contents o
105a6 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 63  f the.    ** cac
105a7 68 65 20 63 61 6e 20 62 65 20 64 69 73 63 61 72  he can be discar
105a8 64 65 64 20 61 6e 64 20 74 68 65 20 65 72 72 6f  ded and the erro
105a9 72 20 63 6f 64 65 20 73 61 66 65 6c 79 20 63 6c  r code safely cl
105aa 65 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20  eared..    */.  
105ab 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72    if( pPager->er
105ac 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 69  rCode ){.      i
105ad 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
105ae 20 29 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   ) pPager->errCo
105af 64 65 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  de = SQLITE_OK;.
105b0 20 20 20 20 20 20 70 61 67 65 72 5f 72 65 73 65        pager_rese
105b1 74 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20  t(pPager);.     
105b2 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 6d   if( pPager->stm
105b3 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 20  tOpen ){.       
105b4 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
105b5 70 50 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a 20  pPager->stfd);. 
105b6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69         sqlite3Bi
105b7 74 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67  tvecDestroy(pPag
105b8 65 72 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 20  er->pInStmt);.  
105b9 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 70 49        pPager->pI
105ba 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20  nStmt = 0;.     
105bb 20 7d 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d   }.      pPager-
105bc 3e 73 74 6d 74 4f 70 65 6e 20 3d 20 30 3b 0a 20  >stmtOpen = 0;. 
105bd 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d       pPager->stm
105be 74 49 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 20  tInUse = 0;.    
105bf 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
105c0 6c 4f 66 66 20 3d 20 30 3b 0a 20 20 20 20 20 20  lOff = 0;.      
105c1 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 53  pPager->journalS
105c2 74 61 72 74 65 64 20 3d 20 30 3b 0a 20 20 20 20  tarted = 0;.    
105c3 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41 75    pPager->stmtAu
105c4 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 20 20  toopen = 0;.    
105c5 20 20 70 50 61 67 65 72 2d 3e 6f 72 69 67 44 62    pPager->origDb
105c6 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Size = 0;.    }.
105c7 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61  .    pPager->sta
105c8 74 65 20 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43  te = PAGER_UNLOC
105c9 4b 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 63  K;.    pPager->c
105ca 68 61 6e 67 65 43 6f 75 6e 74 44 6f 6e 65 20 3d  hangeCountDone =
105cb 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a   0;.  }.}../*.**
105cc 20 45 78 65 63 75 74 65 20 61 20 72 6f 6c 6c 62   Execute a rollb
105cd 61 63 6b 20 69 66 20 61 20 74 72 61 6e 73 61 63  ack if a transac
105ce 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 20 61  tion is active a
105cf 6e 64 20 75 6e 6c 6f 63 6b 20 74 68 65 20 0a 2a  nd unlock the .*
105d0 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  * database file.
105d1 20 49 66 20 74 68 65 20 70 61 67 65 72 20 68 61   If the pager ha
105d2 73 20 61 6c 72 65 61 64 79 20 65 6e 74 65 72 65  s already entere
105d3 64 20 74 68 65 20 65 72 72 6f 72 20 73 74 61 74  d the error stat
105d4 65 2c 20 0a 2a 2a 20 64 6f 20 6e 6f 74 20 61 74  e, .** do not at
105d5 74 65 6d 70 74 20 74 68 65 20 72 6f 6c 6c 62 61  tempt the rollba
105d6 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ck..*/.static vo
105d7 69 64 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e  id pagerUnlockAn
105d8 64 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20  dRollback(Pager 
105d9 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 65 72  *p){.  if( p->er
105da 72 43 6f 64 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b  rCode==SQLITE_OK
105db 20 26 26 20 70 2d 3e 73 74 61 74 65 3e 3d 50 41   && p->state>=PA
105dc 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 7b 0a  GER_RESERVED ){.
105dd 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
105de 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a  BenignMalloc();.
105df 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
105e0 52 6f 6c 6c 62 61 63 6b 28 70 29 3b 0a 20 20 20  Rollback(p);.   
105e1 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
105e2 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 7d 0a 20  nMalloc();.  }. 
105e3 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70 29   pager_unlock(p)
105e4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
105e5 72 6f 75 74 69 6e 65 20 65 6e 64 73 20 61 20 74  routine ends a t
105e6 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 41 20 74  ransaction.  A t
105e7 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 65 6e  ransaction is en
105e8 64 65 64 20 62 79 20 65 69 74 68 65 72 0a 2a 2a  ded by either.**
105e9 20 61 20 43 4f 4d 4d 49 54 20 6f 72 20 61 20 52   a COMMIT or a R
105ea 4f 4c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 20 57  OLLBACK..**.** W
105eb 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
105ec 20 69 73 20 63 61 6c 6c 65 64 2c 20 74 68 65 20   is called, the 
105ed 70 61 67 65 72 20 68 61 73 20 74 68 65 20 6a 6f  pager has the jo
105ee 75 72 6e 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20  urnal file open 
105ef 61 6e 64 0a 2a 2a 20 61 20 52 45 53 45 52 56 45  and.** a RESERVE
105f0 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20 6c  D or EXCLUSIVE l
105f1 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62  ock on the datab
105f2 61 73 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ase.  This routi
105f3 6e 65 20 77 69 6c 6c 20 72 65 6c 65 61 73 65 0a  ne will release.
105f4 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
105f5 6c 6f 63 6b 20 61 6e 64 20 61 63 71 75 69 72 65  lock and acquire
105f6 73 20 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 20  s a SHARED lock 
105f7 69 6e 20 69 74 73 20 70 6c 61 63 65 20 69 66 20  in its place if 
105f8 74 68 61 74 20 69 73 0a 2a 2a 20 74 68 65 20 61  that is.** the a
105f9 70 70 72 6f 70 72 69 61 74 65 20 74 68 69 6e 67  ppropriate thing
105fa 20 74 6f 20 64 6f 2e 20 20 52 65 6c 65 61 73 65   to do.  Release
105fb 20 6c 6f 63 6b 73 20 75 73 75 61 6c 6c 79 20 69   locks usually i
105fc 73 20 61 70 70 72 6f 70 72 69 61 74 65 2c 0a 2a  s appropriate,.*
105fd 2a 20 75 6e 6c 65 73 73 20 77 65 20 61 72 65 20  * unless we are 
105fe 69 6e 20 65 78 63 6c 75 73 69 76 65 20 61 63 63  in exclusive acc
105ff 65 73 73 20 6d 6f 64 65 20 6f 72 20 75 6e 6c 65  ess mode or unle
10600 73 73 20 74 68 69 73 20 69 73 20 61 20 0a 2a 2a  ss this is a .**
10601 20 43 4f 4d 4d 49 54 20 41 4e 44 20 42 45 47 49   COMMIT AND BEGI
10602 4e 20 6f 72 20 52 4f 4c 4c 42 41 43 4b 20 41 4e  N or ROLLBACK AN
10603 44 20 42 45 47 49 4e 20 6f 70 65 72 61 74 69 6f  D BEGIN operatio
10604 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75  n..**.** The jou
10605 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 65 69 74  rnal file is eit
10606 68 65 72 20 64 65 6c 65 74 65 64 20 6f 72 20 74  her deleted or t
10607 72 75 6e 63 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20  runcated..**.** 
10608 54 4f 44 4f 3a 20 43 6f 6e 73 69 64 65 72 20 6b  TODO: Consider k
10609 65 65 70 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e  eeping the journ
1060a 61 6c 20 66 69 6c 65 20 6f 70 65 6e 20 66 6f 72  al file open for
1060b 20 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62   temporary datab
1060c 61 73 65 73 2e 0a 2a 2a 20 54 68 69 73 20 6d 69  ases..** This mi
1060d 67 68 74 20 67 69 76 65 20 61 20 70 65 72 66 6f  ght give a perfo
1060e 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65  rmance improveme
1060f 6e 74 20 6f 6e 20 77 69 6e 64 6f 77 73 20 77 68  nt on windows wh
10610 65 72 65 20 6f 70 65 6e 69 6e 67 0a 2a 2a 20 61  ere opening.** a
10611 20 66 69 6c 65 20 69 73 20 61 6e 20 65 78 70 65   file is an expe
10612 6e 73 69 76 65 20 6f 70 65 72 61 74 69 6f 6e 2e  nsive operation.
10613 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
10614 61 67 65 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63  ager_end_transac
10615 74 69 6f 6e 28 50 61 67 65 72 20 2a 70 50 61 67  tion(Pager *pPag
10616 65 72 2c 20 69 6e 74 20 68 61 73 4d 61 73 74 65  er, int hasMaste
10617 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  r){.  int rc = S
10618 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
10619 72 63 32 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  rc2 = SQLITE_OK;
1061a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
1061b 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52  tate<PAGER_RESER
1061c 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  VED ){.    retur
1061d 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1061e 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53  .  sqlite3PagerS
1061f 74 6d 74 43 6f 6d 6d 69 74 28 70 50 61 67 65 72  tmtCommit(pPager
10620 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
10621 3e 73 74 6d 74 4f 70 65 6e 20 26 26 20 21 70 50  >stmtOpen && !pP
10622 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
10623 6f 64 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ode ){.    sqlit
10624 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
10625 2d 3e 73 74 66 64 29 3b 0a 20 20 20 20 70 50 61  ->stfd);.    pPa
10626 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 3d 20  ger->stmtOpen = 
10627 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  0;.  }.  if( pPa
10628 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e  ger->journalOpen
10629 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61 67   ){.    if( pPag
1062a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d  er->journalMode=
1062b 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
1062c 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20  DE_MEMORY ){.   
1062d 20 20 20 69 6e 74 20 69 73 4d 65 6d 6f 72 79 4a     int isMemoryJ
1062e 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33  ournal = sqlite3
1062f 49 73 4d 65 6d 4a 6f 75 72 6e 61 6c 28 70 50 61  IsMemJournal(pPa
10630 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20  ger->jfd);.     
10631 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28   sqlite3OsClose(
10632 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20  pPager->jfd);.  
10633 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
10634 6e 61 6c 4f 70 65 6e 20 3d 20 30 3b 0a 20 20 20  nalOpen = 0;.   
10635 20 20 20 69 66 28 20 21 69 73 4d 65 6d 6f 72 79     if( !isMemory
10636 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20  Journal ){.     
10637 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
10638 73 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e  sDelete(pPager->
10639 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
1063a 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20  ournal, 0);.    
1063b 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
1063c 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
1063d 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
1063e 52 4e 41 4c 4d 4f 44 45 5f 54 52 55 4e 43 41 54  RNALMODE_TRUNCAT
1063f 45 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 72  E.         && (r
10640 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54 72 75  c = sqlite3OsTru
10641 6e 63 61 74 65 28 70 50 61 67 65 72 2d 3e 6a 66  ncate(pPager->jf
10642 64 2c 20 30 29 29 3d 3d 53 51 4c 49 54 45 5f 4f  d, 0))==SQLITE_O
10643 4b 20 29 7b 0a 20 20 20 20 20 20 70 50 61 67 65  K ){.      pPage
10644 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
10645 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  0;.      pPager-
10646 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20  >journalStarted 
10647 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  = 0;.    }else i
10648 66 28 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75  f( pPager->exclu
10649 73 69 76 65 4d 6f 64 65 20 0a 20 20 20 20 20 7c  siveMode .     |
1064a 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  | pPager->journa
1064b 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
1064c 52 4e 41 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54  RNALMODE_PERSIST
1064d 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63  .    ){.      rc
1064e 20 3d 20 7a 65 72 6f 4a 6f 75 72 6e 61 6c 48 64   = zeroJournalHd
1064f 72 28 70 50 61 67 65 72 2c 20 68 61 73 4d 61 73  r(pPager, hasMas
10650 74 65 72 29 3b 0a 20 20 20 20 20 20 70 61 67 65  ter);.      page
10651 72 5f 65 72 72 6f 72 28 70 50 61 67 65 72 2c 20  r_error(pPager, 
10652 72 63 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65  rc);.      pPage
10653 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
10654 30 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d  0;.      pPager-
10655 3e 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20  >journalStarted 
10656 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 0;.    }else{.
10657 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50        assert( pP
10658 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64  ager->journalMod
10659 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c  e==PAGER_JOURNAL
1065a 4d 4f 44 45 5f 44 45 4c 45 54 45 20 7c 7c 20 72  MODE_DELETE || r
1065b 63 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  c );.      sqlit
1065c 65 33 4f 73 43 6c 6f 73 65 28 70 50 61 67 65 72  e3OsClose(pPager
1065d 2d 3e 6a 66 64 29 3b 0a 20 20 20 20 20 20 70 50  ->jfd);.      pP
1065e 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
1065f 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28  n = 0;.      if(
10660 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
10661 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46  & !pPager->tempF
10662 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ile ){.        r
10663 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 44 65 6c  c = sqlite3OsDel
10664 65 74 65 28 70 50 61 67 65 72 2d 3e 70 56 66 73  ete(pPager->pVfs
10665 2c 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e  , pPager->zJourn
10666 61 6c 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  al, 0);.      }.
10667 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
10668 33 42 69 74 76 65 63 44 65 73 74 72 6f 79 28 70  3BitvecDestroy(p
10669 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61  Pager->pInJourna
1066a 6c 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e  l);.    pPager->
1066b 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30 3b 0a  pInJournal = 0;.
1066c 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
1066d 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
1066e 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b  >pAlwaysRollback
1066f 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70  );.    pPager->p
10670 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d  AlwaysRollback =
10671 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54   0;.#ifdef SQLIT
10672 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a 20 20  E_CHECK_PAGES.  
10673 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49    sqlite3PcacheI
10674 74 65 72 61 74 65 44 69 72 74 79 28 70 50 61 67  terateDirty(pPag
10675 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70 61 67  er->pPCache, pag
10676 65 72 5f 73 65 74 5f 70 61 67 65 68 61 73 68 29  er_set_pagehash)
10677 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
10678 69 74 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41  ite3PcacheCleanA
10679 6c 6c 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ll(pPager->pPCac
1067a 68 65 29 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  he);.    pPager-
1067b 3e 64 69 72 74 79 43 61 63 68 65 20 3d 20 30 3b  >dirtyCache = 0;
1067c 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e 52 65  .    pPager->nRe
1067d 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  c = 0;.  }else{.
1067e 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
1067f 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d  er->pInJournal==
10680 30 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  0 );.  }..  if( 
10681 21 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69  !pPager->exclusi
10682 76 65 4d 6f 64 65 20 29 7b 0a 20 20 20 20 72 63  veMode ){.    rc
10683 32 20 3d 20 6f 73 55 6e 6c 6f 63 6b 28 70 50 61  2 = osUnlock(pPa
10684 67 65 72 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f  ger->fd, SHARED_
10685 4c 4f 43 4b 29 3b 0a 20 20 20 20 70 50 61 67 65  LOCK);.    pPage
10686 72 2d 3e 73 74 61 74 65 20 3d 20 50 41 47 45 52  r->state = PAGER
10687 5f 53 48 41 52 45 44 3b 0a 20 20 7d 65 6c 73 65  _SHARED;.  }else
10688 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
10689 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45 44  te==PAGER_SYNCED
1068a 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
1068b 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58  state = PAGER_EX
1068c 43 4c 55 53 49 56 45 3b 0a 20 20 7d 0a 20 20 70  CLUSIVE;.  }.  p
1068d 50 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a  Pager->origDbSiz
1068e 65 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 72 2d  e = 0;.  pPager-
1068f 3e 73 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a  >setMaster = 0;.
10690 20 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79    pPager->needSy
10691 6e 63 20 3d 20 30 3b 0a 20 20 2f 2a 20 6c 72 75  nc = 0;.  /* lru
10692 4c 69 73 74 53 65 74 46 69 72 73 74 53 79 6e 63  ListSetFirstSync
10693 65 64 28 70 50 61 67 65 72 29 3b 20 2a 2f 0a 20  ed(pPager); */. 
10694 20 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20   if( !MEMDB ){. 
10695 20 20 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a     pPager->dbSiz
10696 65 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 7d 0a  eValid = 0;.  }.
10697 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69    pPager->dbModi
10698 66 69 65 64 20 3d 20 30 3b 0a 0a 20 20 72 65 74  fied = 0;..  ret
10699 75 72 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f  urn (rc==SQLITE_
1069a 4f 4b 3f 72 63 32 3a 72 63 29 3b 0a 7d 0a 0a 2f  OK?rc2:rc);.}../
1069b 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 61 6e 64  *.** Compute and
1069c 20 72 65 74 75 72 6e 20 61 20 63 68 65 63 6b 73   return a checks
1069d 75 6d 20 66 6f 72 20 74 68 65 20 70 61 67 65 20  um for the page 
1069e 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 54  of data..**.** T
1069f 68 69 73 20 69 73 20 6e 6f 74 20 61 20 72 65 61  his is not a rea
106a0 6c 20 63 68 65 63 6b 73 75 6d 2e 20 20 49 74 20  l checksum.  It 
106a1 69 73 20 72 65 61 6c 6c 79 20 6a 75 73 74 20 74  is really just t
106a2 68 65 20 73 75 6d 20 6f 66 20 74 68 65 20 0a 2a  he sum of the .*
106a3 2a 20 72 61 6e 64 6f 6d 20 69 6e 69 74 69 61 6c  * random initial
106a4 20 76 61 6c 75 65 20 61 6e 64 20 74 68 65 20 70   value and the p
106a5 61 67 65 20 6e 75 6d 62 65 72 2e 20 20 57 65 20  age number.  We 
106a6 65 78 70 65 72 69 6d 65 6e 74 65 64 20 77 69 74  experimented wit
106a7 68 0a 2a 2a 20 61 20 63 68 65 63 6b 73 75 6d 20  h.** a checksum 
106a8 6f 66 20 74 68 65 20 65 6e 74 69 72 65 20 64 61  of the entire da
106a9 74 61 2c 20 62 75 74 20 74 68 61 74 20 77 61 73  ta, but that was
106aa 20 66 6f 75 6e 64 20 74 6f 20 62 65 20 74 6f 6f   found to be too
106ab 20 73 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74   slow..**.** Not
106ac 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
106ad 6e 75 6d 62 65 72 20 69 73 20 73 74 6f 72 65 64  number is stored
106ae 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
106af 67 20 6f 66 20 64 61 74 61 20 61 6e 64 0a 2a 2a  g of data and.**
106b0 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 69 73   the checksum is
106b1 20 73 74 6f 72 65 64 20 61 74 20 74 68 65 20 65   stored at the e
106b2 6e 64 2e 20 20 54 68 69 73 20 69 73 20 69 6d 70  nd.  This is imp
106b3 6f 72 74 61 6e 74 2e 20 20 49 66 20 6a 6f 75 72  ortant.  If jour
106b4 6e 61 6c 0a 2a 2a 20 63 6f 72 72 75 70 74 69 6f  nal.** corruptio
106b5 6e 20 6f 63 63 75 72 73 20 64 75 65 20 74 6f 20  n occurs due to 
106b6 61 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c  a power failure,
106b7 20 74 68 65 20 6d 6f 73 74 20 6c 69 6b 65 6c 79   the most likely
106b8 20 73 63 65 6e 61 72 69 6f 0a 2a 2a 20 69 73 20   scenario.** is 
106b9 74 68 61 74 20 6f 6e 65 20 65 6e 64 20 6f 72 20  that one end or 
106ba 74 68 65 20 6f 74 68 65 72 20 6f 66 20 74 68 65  the other of the
106bb 20 72 65 63 6f 72 64 20 77 69 6c 6c 20 62 65 20   record will be 
106bc 63 68 61 6e 67 65 64 2e 20 20 49 74 20 69 73 0a  changed.  It is.
106bd 2a 2a 20 6d 75 63 68 20 6c 65 73 73 20 6c 69 6b  ** much less lik
106be 65 6c 79 20 74 68 61 74 20 74 68 65 20 74 77 6f  ely that the two
106bf 20 65 6e 64 73 20 6f 66 20 74 68 65 20 6a 6f 75   ends of the jou
106c0 72 6e 61 6c 20 72 65 63 6f 72 64 20 77 69 6c 6c  rnal record will
106c1 20 62 65 0a 2a 2a 20 63 6f 72 72 65 63 74 20 61   be.** correct a
106c2 6e 64 20 74 68 65 20 6d 69 64 64 6c 65 20 62 65  nd the middle be
106c3 20 63 6f 72 72 75 70 74 2e 20 20 54 68 75 73 2c   corrupt.  Thus,
106c4 20 74 68 69 73 20 22 63 68 65 63 6b 73 75 6d 22   this "checksum"
106c5 20 73 63 68 65 6d 65 2c 0a 2a 2a 20 74 68 6f 75   scheme,.** thou
106c6 67 68 20 66 61 73 74 20 61 6e 64 20 73 69 6d 70  gh fast and simp
106c7 6c 65 2c 20 63 61 74 63 68 65 73 20 74 68 65 20  le, catches the 
106c8 6d 6f 73 74 6c 79 20 6c 69 6b 65 6c 79 20 6b 69  mostly likely ki
106c9 6e 64 20 6f 66 20 63 6f 72 72 75 70 74 69 6f 6e  nd of corruption
106ca 2e 0a 2a 2a 0a 2a 2a 20 46 49 58 20 4d 45 3a 20  ..**.** FIX ME: 
106cb 20 43 6f 6e 73 69 64 65 72 20 61 64 64 69 6e 67   Consider adding
106cc 20 65 76 65 72 79 20 32 30 30 74 68 20 28 6f 72   every 200th (or
106cd 20 73 6f 29 20 62 79 74 65 20 6f 66 20 74 68 65   so) byte of the
106ce 20 64 61 74 61 20 74 6f 20 74 68 65 0a 2a 2a 20   data to the.** 
106cf 63 68 65 63 6b 73 75 6d 2e 20 20 54 68 61 74 20  checksum.  That 
106d0 77 61 79 20 69 66 20 61 20 73 69 6e 67 6c 65 20  way if a single 
106d1 70 61 67 65 20 73 70 61 6e 73 20 33 20 6f 72 20  page spans 3 or 
106d2 6d 6f 72 65 20 64 69 73 6b 20 73 65 63 74 6f 72  more disk sector
106d3 73 20 61 6e 64 0a 2a 2a 20 6f 6e 6c 79 20 74 68  s and.** only th
106d4 65 20 6d 69 64 64 6c 65 20 73 65 63 74 6f 72 20  e middle sector 
106d5 69 73 20 63 6f 72 72 75 70 74 2c 20 77 65 20 77  is corrupt, we w
106d6 69 6c 6c 20 73 74 69 6c 6c 20 68 61 76 65 20 61  ill still have a
106d7 20 72 65 61 73 6f 6e 61 62 6c 65 0a 2a 2a 20 63   reasonable.** c
106d8 68 61 6e 63 65 20 6f 66 20 66 61 69 6c 69 6e 67  hance of failing
106d9 20 74 68 65 20 63 68 65 63 6b 73 75 6d 20 61 6e   the checksum an
106da 64 20 74 68 75 73 20 64 65 74 65 63 74 69 6e 67  d thus detecting
106db 20 74 68 65 20 70 72 6f 62 6c 65 6d 2e 0a 2a 2f   the problem..*/
106dc 0a 73 74 61 74 69 63 20 75 33 32 20 70 61 67 65  .static u32 page
106dd 72 5f 63 6b 73 75 6d 28 50 61 67 65 72 20 2a 70  r_cksum(Pager *p
106de 50 61 67 65 72 2c 20 63 6f 6e 73 74 20 75 38 20  Pager, const u8 
106df 2a 61 44 61 74 61 29 7b 0a 20 20 75 33 32 20 63  *aData){.  u32 c
106e0 6b 73 75 6d 20 3d 20 70 50 61 67 65 72 2d 3e 63  ksum = pPager->c
106e1 6b 73 75 6d 49 6e 69 74 3b 0a 20 20 69 6e 74 20  ksumInit;.  int 
106e2 69 20 3d 20 70 50 61 67 65 72 2d 3e 70 61 67 65  i = pPager->page
106e3 53 69 7a 65 2d 32 30 30 3b 0a 20 20 77 68 69 6c  Size-200;.  whil
106e4 65 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 63 6b  e( i>0 ){.    ck
106e5 73 75 6d 20 2b 3d 20 61 44 61 74 61 5b 69 5d 3b  sum += aData[i];
106e6 0a 20 20 20 20 69 20 2d 3d 20 32 30 30 3b 0a 20  .    i -= 200;. 
106e7 20 7d 0a 20 20 72 65 74 75 72 6e 20 63 6b 73 75   }.  return cksu
106e8 6d 3b 0a 7d 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  m;.}../* Forward
106e9 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
106ea 73 74 61 74 69 63 20 76 6f 69 64 20 6d 61 6b 65  static void make
106eb 43 6c 65 61 6e 28 50 67 48 64 72 2a 29 3b 0a 0a  Clean(PgHdr*);..
106ec 2f 2a 0a 2a 2a 20 52 65 61 64 20 61 20 73 69 6e  /*.** Read a sin
106ed 67 6c 65 20 70 61 67 65 20 66 72 6f 6d 20 74 68  gle page from th
106ee 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6f  e journal file o
106ef 70 65 6e 65 64 20 6f 6e 20 66 69 6c 65 20 64 65  pened on file de
106f0 73 63 72 69 70 74 6f 72 0a 2a 2a 20 6a 66 64 2e  scriptor.** jfd.
106f1 20 20 50 6c 61 79 62 61 63 6b 20 74 68 69 73 20    Playback this 
106f2 6f 6e 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  one page..**.** 
106f3 54 68 65 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 66  The isMainJrnl f
106f4 6c 61 67 20 69 73 20 74 72 75 65 20 69 66 20 74  lag is true if t
106f5 68 69 73 20 69 73 20 74 68 65 20 6d 61 69 6e 20  his is the main 
106f6 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c  rollback journal
106f7 20 61 6e 64 0a 2a 2a 20 66 61 6c 73 65 20 66 6f   and.** false fo
106f8 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  r the statement 
106f9 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61  journal.  The ma
106fa 69 6e 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72  in rollback jour
106fb 6e 61 6c 20 75 73 65 73 0a 2a 2a 20 63 68 65 63  nal uses.** chec
106fc 6b 73 75 6d 73 20 2d 20 74 68 65 20 73 74 61 74  ksums - the stat
106fd 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 64 6f  ement journal do
106fe 65 73 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69  es not..*/.stati
106ff 63 20 69 6e 74 20 70 61 67 65 72 5f 70 6c 61 79  c int pager_play
10700 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 0a 20  back_one_page(. 
10701 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20   Pager *pPager, 
10702 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67        /* The pag
10703 65 72 20 62 65 69 6e 67 20 70 6c 61 79 65 64 20  er being played 
10704 62 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65  back */.  sqlite
10705 33 5f 66 69 6c 65 20 2a 6a 66 64 2c 20 20 20 2f  3_file *jfd,   /
10706 2a 20 54 68 65 20 66 69 6c 65 20 74 68 61 74 20  * The file that 
10707 69 73 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 62  is the journal b
10708 65 69 6e 67 20 72 6f 6c 6c 65 64 20 62 61 63 6b  eing rolled back
10709 20 2a 2f 0a 20 20 69 36 34 20 6f 66 66 73 65 74   */.  i64 offset
1070a 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 66  ,          /* Of
1070b 66 73 65 74 20 6f 66 20 74 68 65 20 70 61 67 65  fset of the page
1070c 20 77 69 74 68 69 6e 20 74 68 65 20 6a 6f 75 72   within the jour
1070d 6e 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4d  nal */.  int isM
1070e 61 69 6e 4a 72 6e 6c 20 20 20 20 20 20 20 2f 2a  ainJrnl       /*
1070f 20 54 72 75 65 20 66 6f 72 20 6d 61 69 6e 20 72   True for main r
10710 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e  ollback journal.
10711 20 46 61 6c 73 65 20 66 6f 72 20 53 74 6d 74 20   False for Stmt 
10712 6a 72 6e 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  jrnl */.){.  int
10713 20 72 63 3b 0a 20 20 50 67 48 64 72 20 2a 70 50   rc;.  PgHdr *pP
10714 67 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  g;              
10715 20 20 20 20 20 2f 2a 20 41 6e 20 65 78 69 73 74       /* An exist
10716 69 6e 67 20 70 61 67 65 20 69 6e 20 74 68 65 20  ing page in the 
10717 63 61 63 68 65 20 2a 2f 0a 20 20 50 67 6e 6f 20  cache */.  Pgno 
10718 70 67 6e 6f 3b 20 20 20 20 20 20 20 20 20 20 20  pgno;           
10719 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
1071a 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61  page number of a
1071b 20 70 61 67 65 20 69 6e 20 6a 6f 75 72 6e 61 6c   page in journal
1071c 20 2a 2f 0a 20 20 75 33 32 20 63 6b 73 75 6d 3b   */.  u32 cksum;
1071d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1071e 20 20 20 20 2f 2a 20 43 68 65 63 6b 73 75 6d 20      /* Checksum 
1071f 75 73 65 64 20 66 6f 72 20 73 61 6e 69 74 79 20  used for sanity 
10720 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 75 38  checking */.  u8
10721 20 2a 61 44 61 74 61 20 3d 20 28 75 38 20 2a 29   *aData = (u8 *)
10722 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61 63  pPager->pTmpSpac
10723 65 3b 20 20 20 2f 2a 20 54 65 6d 70 20 73 74 6f  e;   /* Temp sto
10724 72 61 67 65 20 66 6f 72 20 61 20 70 61 67 65 20  rage for a page 
10725 2a 2f 0a 0a 20 20 2f 2a 20 69 73 4d 61 69 6e 4a  */..  /* isMainJ
10726 72 6e 6c 20 73 68 6f 75 6c 64 20 62 65 20 74 72  rnl should be tr
10727 75 65 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20  ue for the main 
10728 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 66 61 6c 73  journal and fals
10729 65 20 66 6f 72 0a 20 20 2a 2a 20 73 74 61 74 65  e for.  ** state
1072a 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 73 2e 20 20  ment journals.  
1072b 56 65 72 69 66 79 20 74 68 61 74 20 74 68 69 73  Verify that this
1072c 20 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 63   is always the c
1072d 61 73 65 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  ase.  */.  asser
1072e 74 28 20 6a 66 64 20 3d 3d 20 28 69 73 4d 61 69  t( jfd == (isMai
1072f 6e 4a 72 6e 6c 20 3f 20 70 50 61 67 65 72 2d 3e  nJrnl ? pPager->
10730 6a 66 64 20 3a 20 70 50 61 67 65 72 2d 3e 73 74  jfd : pPager->st
10731 66 64 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  fd) );.  assert(
10732 20 61 44 61 74 61 20 29 3b 0a 0a 20 20 72 63 20   aData );..  rc 
10733 3d 20 72 65 61 64 33 32 62 69 74 73 28 6a 66 64  = read32bits(jfd
10734 2c 20 6f 66 66 73 65 74 2c 20 26 70 67 6e 6f 29  , offset, &pgno)
10735 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
10736 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
10737 63 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  c;.  rc = sqlite
10738 33 4f 73 52 65 61 64 28 6a 66 64 2c 20 61 44 61  3OsRead(jfd, aDa
10739 74 61 2c 20 70 50 61 67 65 72 2d 3e 70 61 67 65  ta, pPager->page
1073a 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b 34 29 3b  Size, offset+4);
1073b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
1073c 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
1073d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ;.  pPager->jour
1073e 6e 61 6c 4f 66 66 20 2b 3d 20 70 50 61 67 65 72  nalOff += pPager
1073f 2d 3e 70 61 67 65 53 69 7a 65 20 2b 20 34 3b 0a  ->pageSize + 4;.
10740 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63 68 65  .  /* Sanity che
10741 63 6b 69 6e 67 20 6f 6e 20 74 68 65 20 70 61 67  cking on the pag
10742 65 2e 20 20 54 68 69 73 20 69 73 20 6d 6f 72 65  e.  This is more
10743 20 69 6d 70 6f 72 74 61 6e 74 20 74 68 61 74 20   important that 
10744 49 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20 2a  I originally.  *
10745 2a 20 74 68 6f 75 67 68 74 2e 20 20 49 66 20 61  * thought.  If a
10746 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f   power failure o
10747 63 63 75 72 73 20 77 68 69 6c 65 20 74 68 65 20  ccurs while the 
10748 6a 6f 75 72 6e 61 6c 20 69 73 20 62 65 69 6e 67  journal is being
10749 20 77 72 69 74 74 65 6e 2c 0a 20 20 2a 2a 20 69   written,.  ** i
1074a 74 20 63 6f 75 6c 64 20 63 61 75 73 65 20 69 6e  t could cause in
1074b 76 61 6c 69 64 20 64 61 74 61 20 74 6f 20 62 65  valid data to be
1074c 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68   written into th
1074d 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 57 65 20 6e  e journal.  We n
1074e 65 65 64 20 74 6f 0a 20 20 2a 2a 20 64 65 74 65  eed to.  ** dete
1074f 63 74 20 74 68 69 73 20 69 6e 76 61 6c 69 64 20  ct this invalid 
10750 64 61 74 61 20 28 77 69 74 68 20 68 69 67 68 20  data (with high 
10751 70 72 6f 62 61 62 69 6c 69 74 79 29 20 61 6e 64  probability) and
10752 20 69 67 6e 6f 72 65 20 69 74 2e 0a 20 20 2a 2f   ignore it..  */
10753 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 7c  .  if( pgno==0 |
10754 7c 20 70 67 6e 6f 3d 3d 50 41 47 45 52 5f 4d 4a  | pgno==PAGER_MJ
10755 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 20 29 7b  _PGNO(pPager) ){
10756 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
10757 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a 20 20 69  TE_DONE;.  }.  i
10758 66 28 20 70 67 6e 6f 3e 28 75 6e 73 69 67 6e 65  f( pgno>(unsigne
10759 64 29 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65  d)pPager->dbSize
1075a 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1075b 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
1075c 69 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29  if( isMainJrnl )
1075d 7b 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 33  {.    rc = read3
1075e 32 62 69 74 73 28 6a 66 64 2c 20 6f 66 66 73 65  2bits(jfd, offse
1075f 74 2b 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69  t+pPager->pageSi
10760 7a 65 2b 34 2c 20 26 63 6b 73 75 6d 29 3b 0a 20  ze+4, &cksum);. 
10761 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75     if( rc ) retu
10762 72 6e 20 72 63 3b 0a 20 20 20 20 70 50 61 67 65  rn rc;.    pPage
10763 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d  r->journalOff +=
10764 20 34 3b 0a 20 20 20 20 69 66 28 20 70 61 67 65   4;.    if( page
10765 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72 2c 20  r_cksum(pPager, 
10766 61 44 61 74 61 29 21 3d 63 6b 73 75 6d 20 29 7b  aData)!=cksum ){
10767 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
10768 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d  LITE_DONE;.    }
10769 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20  .  }..  assert( 
1076a 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
1076b 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 7c 7c  AGER_RESERVED ||
1076c 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
1076d 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
1076e 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
1076f 70 61 67 65 72 20 69 73 20 69 6e 20 52 45 53 45  pager is in RESE
10770 52 56 45 44 20 73 74 61 74 65 2c 20 74 68 65 6e  RVED state, then
10771 20 74 68 65 72 65 20 6d 75 73 74 20 62 65 20 61   there must be a
10772 20 63 6f 70 79 20 6f 66 20 74 68 69 73 0a 20 20   copy of this.  
10773 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 70  ** page in the p
10774 61 67 65 72 20 63 61 63 68 65 2e 20 49 6e 20 74  ager cache. In t
10775 68 69 73 20 63 61 73 65 20 6a 75 73 74 20 75 70  his case just up
10776 64 61 74 65 20 74 68 65 20 70 61 67 65 72 20 63  date the pager c
10777 61 63 68 65 2c 0a 20 20 2a 2a 20 6e 6f 74 20 74  ache,.  ** not t
10778 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
10779 2e 20 54 68 65 20 70 61 67 65 20 69 73 20 6c 65  . The page is le
1077a 66 74 20 6d 61 72 6b 65 64 20 64 69 72 74 79 20  ft marked dirty 
1077b 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20  in this case..  
1077c 2a 2a 0a 20 20 2a 2a 20 41 6e 20 65 78 63 65 70  **.  ** An excep
1077d 74 69 6f 6e 20 74 6f 20 74 68 65 20 61 62 6f 76  tion to the abov
1077e 65 20 72 75 6c 65 3a 20 49 66 20 74 68 65 20 64  e rule: If the d
1077f 61 74 61 62 61 73 65 20 69 73 20 69 6e 20 6e 6f  atabase is in no
10780 2d 73 79 6e 63 20 6d 6f 64 65 0a 20 20 2a 2a 20  -sync mode.  ** 
10781 61 6e 64 20 61 20 70 61 67 65 20 69 73 20 6d 6f  and a page is mo
10782 76 65 64 20 64 75 72 69 6e 67 20 61 6e 20 69 6e  ved during an in
10783 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
10784 20 74 68 65 6e 20 74 68 65 20 70 61 67 65 20 6d   then the page m
10785 61 79 0a 20 20 2a 2a 20 6e 6f 74 20 62 65 20 69  ay.  ** not be i
10786 6e 20 74 68 65 20 70 61 67 65 72 20 63 61 63 68  n the pager cach
10787 65 2e 20 4c 61 74 65 72 3a 20 69 66 20 61 20 6d  e. Later: if a m
10788 61 6c 6c 6f 63 28 29 20 6f 72 20 49 4f 20 65 72  alloc() or IO er
10789 72 6f 72 20 6f 63 63 75 72 73 0a 20 20 2a 2a 20  ror occurs.  ** 
1078a 64 75 72 69 6e 67 20 61 20 4d 6f 76 65 70 61 67  during a Movepag
1078b 65 28 29 20 63 61 6c 6c 2c 20 74 68 65 6e 20 74  e() call, then t
1078c 68 65 20 70 61 67 65 20 6d 61 79 20 6e 6f 74 20  he page may not 
1078d 62 65 20 69 6e 20 74 68 65 20 63 61 63 68 65 0a  be in the cache.
1078e 20 20 2a 2a 20 65 69 74 68 65 72 2e 20 53 6f 20    ** either. So 
1078f 74 68 65 20 63 6f 6e 64 69 74 69 6f 6e 20 64 65  the condition de
10790 73 63 72 69 62 65 64 20 69 6e 20 74 68 65 20 61  scribed in the a
10791 62 6f 76 65 20 70 61 72 61 67 72 61 70 68 20 69  bove paragraph i
10792 73 20 6e 6f 74 0a 20 20 2a 2a 20 61 73 73 65 72  s not.  ** asser
10793 74 28 29 61 62 6c 65 2e 0a 20 20 2a 2a 0a 20 20  t()able..  **.  
10794 2a 2a 20 49 66 20 69 6e 20 45 58 43 4c 55 53 49  ** If in EXCLUSI
10795 56 45 20 73 74 61 74 65 2c 20 74 68 65 6e 20 77  VE state, then w
10796 65 20 75 70 64 61 74 65 20 74 68 65 20 70 61 67  e update the pag
10797 65 72 20 63 61 63 68 65 20 69 66 20 69 74 20 65  er cache if it e
10798 78 69 73 74 73 0a 20 20 2a 2a 20 61 6e 64 20 74  xists.  ** and t
10799 68 65 20 6d 61 69 6e 20 66 69 6c 65 2e 20 54 68  he main file. Th
1079a 65 20 70 61 67 65 20 69 73 20 74 68 65 6e 20 6d  e page is then m
1079b 61 72 6b 65 64 20 6e 6f 74 20 64 69 72 74 79 2e  arked not dirty.
1079c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 69 63 6b 65  .  **.  ** Ticke
1079d 74 20 23 31 31 37 31 3a 20 20 54 68 65 20 73 74  t #1171:  The st
1079e 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20  atement journal 
1079f 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e 20 70 61  might contain pa
107a0 67 65 20 63 6f 6e 74 65 6e 74 20 74 68 61 74 20  ge content that 
107a1 69 73 0a 20 20 2a 2a 20 64 69 66 66 65 72 65 6e  is.  ** differen
107a2 74 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 20  t from the page 
107a3 63 6f 6e 74 65 6e 74 20 61 74 20 74 68 65 20 73  content at the s
107a4 74 61 72 74 20 6f 66 20 74 68 65 20 74 72 61 6e  tart of the tran
107a5 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68  saction..  ** Th
107a6 69 73 20 6f 63 63 75 72 73 20 77 68 65 6e 20 61  is occurs when a
107a7 20 70 61 67 65 20 69 73 20 63 68 61 6e 67 65 64   page is changed
107a8 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20 73 74   prior to the st
107a9 61 72 74 20 6f 66 20 61 20 73 74 61 74 65 6d 65  art of a stateme
107aa 6e 74 0a 20 20 2a 2a 20 74 68 65 6e 20 63 68 61  nt.  ** then cha
107ab 6e 67 65 64 20 61 67 61 69 6e 20 77 69 74 68 69  nged again withi
107ac 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 2e  n the statement.
107ad 20 20 57 68 65 6e 20 72 6f 6c 6c 69 6e 67 20 62    When rolling b
107ae 61 63 6b 20 73 75 63 68 20 61 0a 20 20 2a 2a 20  ack such a.  ** 
107af 73 74 61 74 65 6d 65 6e 74 20 77 65 20 6d 75 73  statement we mus
107b0 74 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74  t not write to t
107b1 68 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61  he original data
107b2 62 61 73 65 20 75 6e 6c 65 73 73 20 77 65 20 6b  base unless we k
107b3 6e 6f 77 0a 20 20 2a 2a 20 66 6f 72 20 63 65 72  now.  ** for cer
107b4 74 61 69 6e 20 74 68 61 74 20 6f 72 69 67 69 6e  tain that origin
107b5 61 6c 20 70 61 67 65 20 63 6f 6e 74 65 6e 74 73  al page contents
107b6 20 61 72 65 20 73 79 6e 63 65 64 20 69 6e 74 6f   are synced into
107b7 20 74 68 65 20 6d 61 69 6e 20 72 6f 6c 6c 62 61   the main rollba
107b8 63 6b 0a 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 2e  ck.  ** journal.
107b9 20 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70    Otherwise, a p
107ba 6f 77 65 72 20 6c 6f 73 73 20 6d 69 67 68 74 20  ower loss might 
107bb 6c 65 61 76 65 20 6d 6f 64 69 66 69 65 64 20 64  leave modified d
107bc 61 74 61 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20  ata in the.  ** 
107bd 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77 69  database file wi
107be 74 68 6f 75 74 20 61 6e 20 65 6e 74 72 79 20 69  thout an entry i
107bf 6e 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a  n the rollback j
107c0 6f 75 72 6e 61 6c 20 74 68 61 74 20 63 61 6e 0a  ournal that can.
107c1 20 20 2a 2a 20 72 65 73 74 6f 72 65 20 74 68 65    ** restore the
107c2 20 64 61 74 61 62 61 73 65 20 74 6f 20 69 74 73   database to its
107c3 20 6f 72 69 67 69 6e 61 6c 20 66 6f 72 6d 2e 20   original form. 
107c4 20 54 77 6f 20 63 6f 6e 64 69 74 69 6f 6e 73 20   Two conditions 
107c5 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 6d 65 74  must be.  ** met
107c6 20 62 65 66 6f 72 65 20 77 72 69 74 69 6e 67 20   before writing 
107c7 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
107c8 66 69 6c 65 73 2e 20 28 31 29 20 74 68 65 20 64  files. (1) the d
107c9 61 74 61 62 61 73 65 20 6d 75 73 74 20 62 65 0a  atabase must be.
107ca 20 20 2a 2a 20 6c 6f 63 6b 65 64 2e 20 20 28 32    ** locked.  (2
107cb 29 20 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 74  ) we know that t
107cc 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67 65  he original page
107cd 20 63 6f 6e 74 65 6e 74 20 69 73 20 66 75 6c 6c   content is full
107ce 79 20 73 79 6e 63 65 64 0a 20 20 2a 2a 20 69 6e  y synced.  ** in
107cf 20 74 68 65 20 6d 61 69 6e 20 6a 6f 75 72 6e 61   the main journa
107d0 6c 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65  l either because
107d1 20 74 68 65 20 70 61 67 65 20 69 73 20 6e 6f 74   the page is not
107d2 20 69 6e 20 63 61 63 68 65 20 6f 72 20 65 6c 73   in cache or els
107d3 65 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65 20  e.  ** the page 
107d4 69 73 20 6d 61 72 6b 65 64 20 61 73 20 6e 65 65  is marked as nee
107d5 64 53 79 6e 63 3d 3d 30 2e 0a 20 20 2a 2a 0a 20  dSync==0..  **. 
107d6 20 2a 2a 20 32 30 30 38 2d 30 34 2d 31 34 3a 20   ** 2008-04-14: 
107d7 20 57 68 65 6e 20 61 74 74 65 6d 70 74 69 6e 67   When attempting
107d8 20 74 6f 20 76 61 63 75 75 6d 20 61 20 63 6f 72   to vacuum a cor
107d9 72 75 70 74 20 64 61 74 61 62 61 73 65 20 66 69  rupt database fi
107da 6c 65 2c 20 69 74 0a 20 20 2a 2a 20 69 73 20 70  le, it.  ** is p
107db 6f 73 73 69 62 6c 65 20 74 6f 20 66 61 69 6c 20  ossible to fail 
107dc 61 20 73 74 61 74 65 6d 65 6e 74 20 6f 6e 20 61  a statement on a
107dd 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20 64   database that d
107de 6f 65 73 20 6e 6f 74 20 79 65 74 20 65 78 69 73  oes not yet exis
107df 74 2e 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20 61  t..  ** Do not a
107e0 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74 65 20  ttempt to write 
107e1 69 66 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  if database file
107e2 20 68 61 73 20 6e 65 76 65 72 20 62 65 65 6e 20   has never been 
107e3 6f 70 65 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 70  opened..  */.  p
107e4 50 67 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75  Pg = pager_looku
107e5 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29 3b  p(pPager, pgno);
107e6 0a 20 20 50 41 47 45 52 54 52 41 43 45 34 28 22  .  PAGERTRACE4("
107e7 50 4c 41 59 42 41 43 4b 20 25 64 20 70 61 67 65  PLAYBACK %d page
107e8 20 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e   %d hash(%08x)\n
107e9 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
107ea 20 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72    PAGERID(pPager
107eb 29 2c 20 70 67 6e 6f 2c 20 70 61 67 65 72 5f 64  ), pgno, pager_d
107ec 61 74 61 68 61 73 68 28 70 50 61 67 65 72 2d 3e  atahash(pPager->
107ed 70 61 67 65 53 69 7a 65 2c 20 61 44 61 74 61 29  pageSize, aData)
107ee 29 3b 0a 20 20 69 66 28 20 28 70 50 61 67 65 72  );.  if( (pPager
107ef 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 45  ->state>=PAGER_E
107f0 58 43 4c 55 53 49 56 45 29 0a 20 20 20 26 26 20  XCLUSIVE).   && 
107f1 28 70 50 67 3d 3d 30 20 7c 7c 20 30 3d 3d 28 70  (pPg==0 || 0==(p
107f2 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f  Pg->flags&PGHDR_
107f3 4e 45 45 44 5f 53 59 4e 43 29 29 0a 20 20 20 26  NEED_SYNC)).   &
107f4 26 20 28 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70  & (pPager->fd->p
107f5 4d 65 74 68 6f 64 73 29 0a 20 20 29 7b 0a 20 20  Methods).  ){.  
107f6 20 20 69 36 34 20 6f 66 73 74 20 3d 20 28 70 67    i64 ofst = (pg
107f7 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65  no-1)*(i64)pPage
107f8 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  r->pageSize;.   
107f9 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 57   rc = sqlite3OsW
107fa 72 69 74 65 28 70 50 61 67 65 72 2d 3e 66 64 2c  rite(pPager->fd,
107fb 20 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e   aData, pPager->
107fc 70 61 67 65 53 69 7a 65 2c 20 6f 66 73 74 29 3b  pageSize, ofst);
107fd 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 67 20 29  .  }.  if( pPg )
107fe 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 70 61 67 65  {.    /* No page
107ff 20 73 68 6f 75 6c 64 20 65 76 65 72 20 62 65 20   should ever be 
10800 65 78 70 6c 69 63 69 74 6c 79 20 72 6f 6c 6c 65  explicitly rolle
10801 64 20 62 61 63 6b 20 74 68 61 74 20 69 73 20 69  d back that is i
10802 6e 20 75 73 65 2c 20 65 78 63 65 70 74 0a 20 20  n use, except.  
10803 20 20 2a 2a 20 66 6f 72 20 70 61 67 65 20 31 20    ** for page 1 
10804 77 68 69 63 68 20 69 73 20 68 65 6c 64 20 69 6e  which is held in
10805 20 75 73 65 20 69 6e 20 6f 72 64 65 72 20 74 6f   use in order to
10806 20 6b 65 65 70 20 74 68 65 20 6c 6f 63 6b 20 6f   keep the lock o
10807 6e 20 74 68 65 0a 20 20 20 20 2a 2a 20 64 61 74  n the.    ** dat
10808 61 62 61 73 65 20 61 63 74 69 76 65 2e 20 48 6f  abase active. Ho
10809 77 65 76 65 72 20 73 75 63 68 20 61 20 70 61 67  wever such a pag
1080a 65 20 6d 61 79 20 62 65 20 72 6f 6c 6c 65 64 20  e may be rolled 
1080b 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c 74  back as a result
1080c 0a 20 20 20 20 2a 2a 20 6f 66 20 61 6e 20 69 6e  .    ** of an in
1080d 74 65 72 6e 61 6c 20 65 72 72 6f 72 20 72 65 73  ternal error res
1080e 75 6c 74 69 6e 67 20 69 6e 20 61 6e 20 61 75 74  ulting in an aut
1080f 6f 6d 61 74 69 63 20 63 61 6c 6c 20 74 6f 0a 20  omatic call to. 
10810 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 50 61 67     ** sqlite3Pag
10811 65 72 52 6f 6c 6c 62 61 63 6b 28 29 2e 0a 20 20  erRollback()..  
10812 20 20 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70    */.    void *p
10813 44 61 74 61 3b 0a 20 20 20 20 70 44 61 74 61 20  Data;.    pData 
10814 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20 20  = pPg->pData;.  
10815 20 20 6d 65 6d 63 70 79 28 70 44 61 74 61 2c 20    memcpy(pData, 
10816 61 44 61 74 61 2c 20 70 50 61 67 65 72 2d 3e 70  aData, pPager->p
10817 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 69 66  ageSize);.    if
10818 28 20 70 50 61 67 65 72 2d 3e 78 52 65 69 6e 69  ( pPager->xReini
10819 74 65 72 20 29 7b 0a 20 20 20 20 20 20 70 50 61  ter ){.      pPa
1081a 67 65 72 2d 3e 78 52 65 69 6e 69 74 65 72 28 70  ger->xReiniter(p
1081b 50 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  Pg);.    }.    i
1081c 66 28 20 69 73 4d 61 69 6e 4a 72 6e 6c 20 29 20  f( isMainJrnl ) 
1081d 6d 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a  makeClean(pPg);.
1081e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 48  #ifdef SQLITE_CH
1081f 45 43 4b 5f 50 41 47 45 53 0a 20 20 20 20 70 50  ECK_PAGES.    pP
10820 67 2d 3e 70 61 67 65 48 61 73 68 20 3d 20 70 61  g->pageHash = pa
10821 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50 67  ger_pagehash(pPg
10822 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 2f 2a  );.#endif.    /*
10823 20 49 66 20 74 68 69 73 20 77 61 73 20 70 61 67   If this was pag
10824 65 20 31 2c 20 74 68 65 6e 20 72 65 73 74 6f 72  e 1, then restor
10825 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 50  e the value of P
10826 61 67 65 72 2e 64 62 46 69 6c 65 56 65 72 73 2e  ager.dbFileVers.
10827 0a 20 20 20 20 2a 2a 20 44 6f 20 74 68 69 73 20  .    ** Do this 
10828 62 65 66 6f 72 65 20 61 6e 79 20 64 65 63 6f 64  before any decod
10829 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ing. */.    if( 
1082a 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 20  pgno==1 ){.     
1082b 20 6d 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d   memcpy(&pPager-
1082c 3e 64 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28  >dbFileVers, &((
1082d 75 38 2a 29 70 44 61 74 61 29 5b 32 34 5d 2c 73  u8*)pData)[24],s
1082e 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e 64 62  izeof(pPager->db
1082f 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20 20 20  FileVers));.    
10830 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 63 6f 64 65  }..    /* Decode
10831 20 74 68 65 20 70 61 67 65 20 6a 75 73 74 20 72   the page just r
10832 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 20 2a 2f  ead from disk */
10833 0a 20 20 20 20 43 4f 44 45 43 31 28 70 50 61 67  .    CODEC1(pPag
10834 65 72 2c 20 70 44 61 74 61 2c 20 70 50 67 2d 3e  er, pData, pPg->
10835 70 67 6e 6f 2c 20 33 29 3b 0a 20 20 20 20 73 71  pgno, 3);.    sq
10836 6c 69 74 65 33 50 63 61 63 68 65 52 65 6c 65 61  lite3PcacheRelea
10837 73 65 28 70 50 67 29 3b 0a 20 20 7d 0a 20 20 72  se(pPg);.  }.  r
10838 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
10839 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 7a 4d 61  ** Parameter zMa
1083a 73 74 65 72 20 69 73 20 74 68 65 20 6e 61 6d 65  ster is the name
1083b 20 6f 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75   of a master jou
1083c 72 6e 61 6c 20 66 69 6c 65 2e 20 41 20 73 69 6e  rnal file. A sin
1083d 67 6c 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 66  gle journal.** f
1083e 69 6c 65 20 74 68 61 74 20 72 65 66 65 72 72 65  ile that referre
1083f 64 20 74 6f 20 74 68 65 20 6d 61 73 74 65 72 20  d to the master 
10840 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 68 61 73  journal file has
10841 20 6a 75 73 74 20 62 65 65 6e 20 72 6f 6c 6c 65   just been rolle
10842 64 20 62 61 63 6b 2e 0a 2a 2a 20 54 68 69 73 20  d back..** This 
10843 72 6f 75 74 69 6e 65 20 63 68 65 63 6b 73 20 69  routine checks i
10844 66 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65  f it is possible
10845 20 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d   to delete the m
10846 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69  aster journal fi
10847 6c 65 2c 0a 2a 2a 20 61 6e 64 20 64 6f 65 73 20  le,.** and does 
10848 73 6f 20 69 66 20 69 74 20 69 73 2e 0a 2a 2a 0a  so if it is..**.
10849 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a 4d 61 73  ** Argument zMas
1084a 74 65 72 20 6d 61 79 20 70 6f 69 6e 74 20 74 6f  ter may point to
1084b 20 50 61 67 65 72 2e 70 54 6d 70 53 70 61 63 65   Pager.pTmpSpace
1084c 2e 20 53 6f 20 74 68 61 74 20 62 75 66 66 65 72  . So that buffer
1084d 20 69 73 20 6e 6f 74 20 0a 2a 2a 20 61 76 61 69   is not .** avai
1084e 6c 61 62 6c 65 20 66 6f 72 20 75 73 65 20 77 69  lable for use wi
1084f 74 68 69 6e 20 74 68 69 73 20 66 75 6e 63 74 69  thin this functi
10850 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 65  on..**.**.** The
10851 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
10852 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68  file contains th
10853 65 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63  e names of all c
10854 68 69 6c 64 20 6a 6f 75 72 6e 61 6c 73 2e 0a 2a  hild journals..*
10855 2a 20 54 6f 20 74 65 6c 6c 20 69 66 20 61 20 6d  * To tell if a m
10856 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63 61  aster journal ca
10857 6e 20 62 65 20 64 65 6c 65 74 65 64 2c 20 63 68  n be deleted, ch
10858 65 63 6b 20 74 6f 20 65 61 63 68 20 6f 66 20 74  eck to each of t
10859 68 65 0a 2a 2a 20 63 68 69 6c 64 72 65 6e 2e 20  he.** children. 
1085a 20 49 66 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e   If all children
1085b 20 61 72 65 20 65 69 74 68 65 72 20 6d 69 73 73   are either miss
1085c 69 6e 67 20 6f 72 20 64 6f 20 6e 6f 74 20 72 65  ing or do not re
1085d 66 65 72 20 74 6f 0a 2a 2a 20 61 20 64 69 66 66  fer to.** a diff
1085e 65 72 65 6e 74 20 6d 61 73 74 65 72 20 6a 6f 75  erent master jou
1085f 72 6e 61 6c 2c 20 74 68 65 6e 20 74 68 69 73 20  rnal, then this 
10860 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 63  master journal c
10861 61 6e 20 62 65 20 64 65 6c 65 74 65 64 2e 0a 2a  an be deleted..*
10862 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
10863 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 50 61 67  er_delmaster(Pag
10864 65 72 20 2a 70 50 61 67 65 72 2c 20 63 6f 6e 73  er *pPager, cons
10865 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 29  t char *zMaster)
10866 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
10867 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
10868 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  pVfs;.  int rc;.
10869 20 20 69 6e 74 20 6d 61 73 74 65 72 5f 6f 70 65    int master_ope
1086a 6e 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  n = 0;.  sqlite3
1086b 5f 66 69 6c 65 20 2a 70 4d 61 73 74 65 72 3b 0a  _file *pMaster;.
1086c 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1086d 70 4a 6f 75 72 6e 61 6c 3b 0a 20 20 63 68 61 72  pJournal;.  char
1086e 20 2a 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   *zMasterJournal
1086f 20 3d 20 30 3b 20 2f 2a 20 43 6f 6e 74 65 6e 74   = 0; /* Content
10870 73 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72  s of master jour
10871 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20 20 69 36  nal file */.  i6
10872 34 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  4 nMasterJournal
10873 3b 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20  ;       /* Size 
10874 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  of master journa
10875 6c 20 66 69 6c 65 20 2a 2f 0a 0a 20 20 2f 2a 20  l file */..  /* 
10876 4f 70 65 6e 20 74 68 65 20 6d 61 73 74 65 72 20  Open the master 
10877 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 65 78 63  journal file exc
10878 6c 75 73 69 76 65 6c 79 20 69 6e 20 63 61 73 65  lusively in case
10879 20 73 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63   some other proc
1087a 65 73 73 0a 20 20 2a 2a 20 69 73 20 72 75 6e 6e  ess.  ** is runn
1087b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
1087c 20 61 6c 73 6f 2e 20 4e 6f 74 20 74 68 61 74 20   also. Not that 
1087d 69 74 20 6d 61 6b 65 73 20 74 6f 6f 20 6d 75 63  it makes too muc
1087e 68 20 64 69 66 66 65 72 65 6e 63 65 2e 0a 20 20  h difference..  
1087f 2a 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 28  */.  pMaster = (
10880 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 73  sqlite3_file *)s
10881 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 56 66  qlite3Malloc(pVf
10882 73 2d 3e 73 7a 4f 73 46 69 6c 65 20 2a 20 32 29  s->szOsFile * 2)
10883 3b 0a 20 20 70 4a 6f 75 72 6e 61 6c 20 3d 20 28  ;.  pJournal = (
10884 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 29 28  sqlite3_file *)(
10885 28 28 75 38 20 2a 29 70 4d 61 73 74 65 72 29 20  ((u8 *)pMaster) 
10886 2b 20 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  + pVfs->szOsFile
10887 29 3b 0a 20 20 69 66 28 20 21 70 4d 61 73 74 65  );.  if( !pMaste
10888 72 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  r ){.    rc = SQ
10889 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65  LITE_NOMEM;.  }e
1088a 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 66 6c 61  lse{.    int fla
1088b 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45  gs = (SQLITE_OPE
1088c 4e 5f 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54  N_READONLY|SQLIT
1088d 45 5f 4f 50 45 4e 5f 4d 41 53 54 45 52 5f 4a 4f  E_OPEN_MASTER_JO
1088e 55 52 4e 41 4c 29 3b 0a 20 20 20 20 72 63 20 3d  URNAL);.    rc =
1088f 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
10890 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20 70 4d  Vfs, zMaster, pM
10891 61 73 74 65 72 2c 20 66 6c 61 67 73 2c 20 30 29  aster, flags, 0)
10892 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 21 3d  ;.  }.  if( rc!=
10893 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
10894 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a   delmaster_out;.
10895 20 20 6d 61 73 74 65 72 5f 6f 70 65 6e 20 3d 20    master_open = 
10896 31 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  1;..  rc = sqlit
10897 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 4d 61  e3OsFileSize(pMa
10898 73 74 65 72 2c 20 26 6e 4d 61 73 74 65 72 4a 6f  ster, &nMasterJo
10899 75 72 6e 61 6c 29 3b 0a 20 20 69 66 28 20 72 63  urnal);.  if( rc
1089a 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
1089b 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
1089c 3b 0a 0a 20 20 69 66 28 20 6e 4d 61 73 74 65 72  ;..  if( nMaster
1089d 4a 6f 75 72 6e 61 6c 3e 30 20 29 7b 0a 20 20 20  Journal>0 ){.   
1089e 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61 6c 3b   char *zJournal;
1089f 0a 20 20 20 20 63 68 61 72 20 2a 7a 4d 61 73 74  .    char *zMast
108a0 65 72 50 74 72 20 3d 20 30 3b 0a 20 20 20 20 69  erPtr = 0;.    i
108a1 6e 74 20 6e 4d 61 73 74 65 72 50 74 72 20 3d 20  nt nMasterPtr = 
108a2 70 50 61 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78  pPager->pVfs->mx
108a3 50 61 74 68 6e 61 6d 65 2b 31 3b 0a 0a 20 20 20  Pathname+1;..   
108a4 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65 6e 74   /* Load the ent
108a5 69 72 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  ire master journ
108a6 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20 73 70 61  al file into spa
108a7 63 65 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d  ce obtained from
108a8 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  .    ** sqlite3_
108a9 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 6f 69  malloc() and poi
108aa 6e 74 65 64 20 74 6f 20 62 79 20 7a 4d 61 73 74  nted to by zMast
108ab 65 72 4a 6f 75 72 6e 61 6c 2e 20 0a 20 20 20 20  erJournal. .    
108ac 2a 2f 0a 20 20 20 20 7a 4d 61 73 74 65 72 4a 6f  */.    zMasterJo
108ad 75 72 6e 61 6c 20 3d 20 28 63 68 61 72 20 2a 29  urnal = (char *)
108ae 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 4d  sqlite3Malloc(nM
108af 61 73 74 65 72 4a 6f 75 72 6e 61 6c 20 2b 20 6e  asterJournal + n
108b0 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20  MasterPtr);.    
108b1 69 66 28 20 21 7a 4d 61 73 74 65 72 4a 6f 75 72  if( !zMasterJour
108b2 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  nal ){.      rc 
108b3 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
108b4 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61        goto delma
108b5 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  ster_out;.    }.
108b6 20 20 20 20 7a 4d 61 73 74 65 72 50 74 72 20 3d      zMasterPtr =
108b7 20 26 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c   &zMasterJournal
108b8 5b 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 5d  [nMasterJournal]
108b9 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
108ba 65 33 4f 73 52 65 61 64 28 70 4d 61 73 74 65 72  e3OsRead(pMaster
108bb 2c 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  , zMasterJournal
108bc 2c 20 6e 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  , nMasterJournal
108bd 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
108be 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
108bf 74 6f 20 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74  to delmaster_out
108c0 3b 0a 0a 20 20 20 20 7a 4a 6f 75 72 6e 61 6c 20  ;..    zJournal 
108c1 3d 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  = zMasterJournal
108c2 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 28 7a 4a  ;.    while( (zJ
108c3 6f 75 72 6e 61 6c 2d 7a 4d 61 73 74 65 72 4a 6f  ournal-zMasterJo
108c4 75 72 6e 61 6c 29 3c 6e 4d 61 73 74 65 72 4a 6f  urnal)<nMasterJo
108c5 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20 20 69  urnal ){.      i
108c6 6e 74 20 65 78 69 73 74 73 3b 0a 20 20 20 20 20  nt exists;.     
108c7 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41   rc = sqlite3OsA
108c8 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4a 6f 75  ccess(pVfs, zJou
108c9 72 6e 61 6c 2c 20 53 51 4c 49 54 45 5f 41 43 43  rnal, SQLITE_ACC
108ca 45 53 53 5f 45 58 49 53 54 53 2c 20 26 65 78 69  ESS_EXISTS, &exi
108cb 73 74 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20  sts);.      if( 
108cc 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
108cd 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65  .        goto de
108ce 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20  lmaster_out;.   
108cf 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65     }.      if( e
108d0 78 69 73 74 73 20 29 7b 0a 20 20 20 20 20 20 20  xists ){.       
108d1 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 6a   /* One of the j
108d2 6f 75 72 6e 61 6c 73 20 70 6f 69 6e 74 65 64 20  ournals pointed 
108d3 74 6f 20 62 79 20 74 68 65 20 6d 61 73 74 65 72  to by the master
108d4 20 6a 6f 75 72 6e 61 6c 20 65 78 69 73 74 73 2e   journal exists.
108d5 0a 20 20 20 20 20 20 20 20 2a 2a 20 4f 70 65 6e  .        ** Open
108d6 20 69 74 20 61 6e 64 20 63 68 65 63 6b 20 69 66   it and check if
108d7 20 69 74 20 70 6f 69 6e 74 73 20 61 74 20 74 68   it points at th
108d8 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c  e master journal
108d9 2e 20 49 66 0a 20 20 20 20 20 20 20 20 2a 2a 20  . If.        ** 
108da 73 6f 2c 20 72 65 74 75 72 6e 20 77 69 74 68 6f  so, return witho
108db 75 74 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20  ut deleting the 
108dc 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66  master journal f
108dd 69 6c 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ile..        */.
108de 20 20 20 20 20 20 20 20 69 6e 74 20 63 3b 0a 20          int c;. 
108df 20 20 20 20 20 20 20 69 6e 74 20 66 6c 61 67 73         int flags
108e0 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50 45 4e 5f   = (SQLITE_OPEN_
108e1 52 45 41 44 4f 4e 4c 59 7c 53 51 4c 49 54 45 5f  READONLY|SQLITE_
108e2 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41  OPEN_MAIN_JOURNA
108e3 4c 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  L);.        rc =
108e4 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70   sqlite3OsOpen(p
108e5 56 66 73 2c 20 7a 4a 6f 75 72 6e 61 6c 2c 20 70  Vfs, zJournal, p
108e6 4a 6f 75 72 6e 61 6c 2c 20 66 6c 61 67 73 2c 20  Journal, flags, 
108e7 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  0);.        if( 
108e8 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
108e9 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
108ea 64 65 6c 6d 61 73 74 65 72 5f 6f 75 74 3b 0a 20  delmaster_out;. 
108eb 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
108ec 20 20 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65    rc = readMaste
108ed 72 4a 6f 75 72 6e 61 6c 28 70 4a 6f 75 72 6e 61  rJournal(pJourna
108ee 6c 2c 20 7a 4d 61 73 74 65 72 50 74 72 2c 20 6e  l, zMasterPtr, n
108ef 4d 61 73 74 65 72 50 74 72 29 3b 0a 20 20 20 20  MasterPtr);.    
108f0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f      sqlite3OsClo
108f1 73 65 28 70 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20  se(pJournal);.  
108f2 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
108f3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
108f4 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d 61 73       goto delmas
108f5 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20 20 20  ter_out;.       
108f6 20 7d 0a 0a 20 20 20 20 20 20 20 20 63 20 3d 20   }..        c = 
108f7 7a 4d 61 73 74 65 72 50 74 72 5b 30 5d 21 3d 30  zMasterPtr[0]!=0
108f8 20 26 26 20 73 74 72 63 6d 70 28 7a 4d 61 73 74   && strcmp(zMast
108f9 65 72 50 74 72 2c 20 7a 4d 61 73 74 65 72 29 3d  erPtr, zMaster)=
108fa 3d 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  =0;.        if( 
108fb 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  c ){.          /
108fc 2a 20 57 65 20 68 61 76 65 20 61 20 6d 61 74 63  * We have a matc
108fd 68 2e 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74 65  h. Do not delete
108fe 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
108ff 6e 61 6c 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  nal file. */.   
10900 20 20 20 20 20 20 20 67 6f 74 6f 20 64 65 6c 6d         goto delm
10901 61 73 74 65 72 5f 6f 75 74 3b 0a 20 20 20 20 20  aster_out;.     
10902 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
10903 20 20 20 7a 4a 6f 75 72 6e 61 6c 20 2b 3d 20 28     zJournal += (
10904 73 74 72 6c 65 6e 28 7a 4a 6f 75 72 6e 61 6c 29  strlen(zJournal)
10905 2b 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  +1);.    }.  }. 
10906 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33   .  rc = sqlite3
10907 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 7a  OsDelete(pVfs, z
10908 4d 61 73 74 65 72 2c 20 30 29 3b 0a 0a 64 65 6c  Master, 0);..del
10909 6d 61 73 74 65 72 5f 6f 75 74 3a 0a 20 20 69 66  master_out:.  if
1090a 28 20 7a 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c  ( zMasterJournal
1090b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
1090c 66 72 65 65 28 7a 4d 61 73 74 65 72 4a 6f 75 72  free(zMasterJour
1090d 6e 61 6c 29 3b 0a 20 20 7d 20 20 0a 20 20 69 66  nal);.  }  .  if
1090e 28 20 6d 61 73 74 65 72 5f 6f 70 65 6e 20 29 7b  ( master_open ){
1090f 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
10910 6f 73 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ose(pMaster);.  
10911 7d 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  }.  sqlite3_free
10912 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72 65 74  (pMaster);.  ret
10913 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 73 74 61 74  urn rc;.}...stat
10914 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 74 72  ic void pager_tr
10915 75 6e 63 61 74 65 5f 63 61 63 68 65 28 50 61 67  uncate_cache(Pag
10916 65 72 20 2a 70 50 61 67 65 72 29 3b 0a 0a 2f 2a  er *pPager);../*
10917 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65  .** Truncate the
10918 20 6d 61 69 6e 20 66 69 6c 65 20 6f 66 20 74 68   main file of th
10919 65 20 67 69 76 65 6e 20 70 61 67 65 72 20 74 6f  e given pager to
1091a 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1091b 61 67 65 73 0a 2a 2a 20 69 6e 64 69 63 61 74 65  ages.** indicate
1091c 64 2e 20 41 6c 73 6f 20 74 72 75 6e 63 61 74 65  d. Also truncate
1091d 20 74 68 65 20 63 61 63 68 65 64 20 72 65 70 72   the cached repr
1091e 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  esentation of th
1091f 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 4d 69  e file..**.** Mi
10920 67 68 74 20 6d 69 67 68 74 20 62 65 20 74 68 65  ght might be the
10921 20 63 61 73 65 20 74 68 61 74 20 74 68 65 20 66   case that the f
10922 69 6c 65 20 6f 6e 20 64 69 73 6b 20 69 73 20 73  ile on disk is s
10923 6d 61 6c 6c 65 72 20 74 68 61 6e 20 6e 50 61 67  maller than nPag
10924 65 2e 0a 2a 2a 20 54 68 69 73 20 63 61 6e 20 68  e..** This can h
10925 61 70 70 65 6e 2c 20 66 6f 72 20 65 78 61 6d 70  appen, for examp
10926 6c 65 2c 20 69 66 20 77 65 20 61 72 65 20 69 6e  le, if we are in
10927 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 61   the middle of a
10928 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
10929 77 68 69 63 68 20 68 61 73 20 65 78 74 65 6e 64  which has extend
1092a 65 64 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65  ed the file size
1092b 20 61 6e 64 20 74 68 65 20 6e 65 77 20 70 61 67   and the new pag
1092c 65 73 20 61 72 65 20 73 74 69 6c 6c 20 61 6c 6c  es are still all
1092d 20 68 65 6c 64 0a 2a 2a 20 69 6e 20 63 61 63 68   held.** in cach
1092e 65 2c 20 74 68 65 6e 20 61 6e 20 49 4e 53 45 52  e, then an INSER
1092f 54 20 6f 72 20 55 50 44 41 54 45 20 64 6f 65 73  T or UPDATE does
10930 20 61 20 73 74 61 74 65 6d 65 6e 74 20 72 6f 6c   a statement rol
10931 6c 62 61 63 6b 2e 20 20 53 6f 6d 65 0a 2a 2a 20  lback.  Some.** 
10932 6f 70 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d  operating system
10933 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73   implementations
10934 20 63 61 6e 20 67 65 74 20 63 6f 6e 66 75 73 65   can get confuse
10935 64 20 69 66 20 79 6f 75 20 74 72 79 20 74 6f 0a  d if you try to.
10936 2a 2a 20 74 72 75 6e 63 61 74 65 20 61 20 66 69  ** truncate a fi
10937 6c 65 20 74 6f 20 73 6f 6d 65 20 73 69 7a 65 20  le to some size 
10938 74 68 61 74 20 69 73 20 6c 61 72 67 65 72 20 74  that is larger t
10939 68 61 6e 20 69 74 20 63 75 72 72 65 6e 74 6c 79  han it currently
1093a 20 69 73 2c 0a 2a 2a 20 73 6f 20 64 65 74 65 63   is,.** so detec
1093b 74 20 74 68 69 73 20 63 61 73 65 20 61 6e 64 20  t this case and 
1093c 77 72 69 74 65 20 61 20 73 69 6e 67 6c 65 20 7a  write a single z
1093d 65 72 6f 20 62 79 74 65 20 74 6f 20 74 68 65 20  ero byte to the 
1093e 65 6e 64 20 6f 66 20 74 68 65 20 6e 65 77 0a 2a  end of the new.*
1093f 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 2e 0a  * file instead..
10940 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
10941 67 65 72 5f 74 72 75 6e 63 61 74 65 28 50 61 67  ger_truncate(Pag
10942 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
10943 20 6e 50 61 67 65 29 7b 0a 20 20 69 6e 74 20 72   nPage){.  int r
10944 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
10945 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
10946 74 65 3e 3d 50 41 47 45 52 5f 45 58 43 4c 55 53  te>=PAGER_EXCLUS
10947 49 56 45 20 26 26 20 70 50 61 67 65 72 2d 3e 66  IVE && pPager->f
10948 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20  d->pMethods ){. 
10949 20 20 20 69 36 34 20 63 75 72 72 65 6e 74 53 69     i64 currentSi
1094a 7a 65 2c 20 6e 65 77 53 69 7a 65 3b 0a 20 20 20  ze, newSize;.   
1094b 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 46   rc = sqlite3OsF
1094c 69 6c 65 53 69 7a 65 28 70 50 61 67 65 72 2d 3e  ileSize(pPager->
1094d 66 64 2c 20 26 63 75 72 72 65 6e 74 53 69 7a 65  fd, &currentSize
1094e 29 3b 0a 20 20 20 20 6e 65 77 53 69 7a 65 20 3d  );.    newSize =
1094f 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
10950 65 2a 28 69 36 34 29 6e 50 61 67 65 3b 0a 20 20  e*(i64)nPage;.  
10951 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
10952 5f 4f 4b 20 26 26 20 63 75 72 72 65 6e 74 53 69  _OK && currentSi
10953 7a 65 21 3d 6e 65 77 53 69 7a 65 20 29 7b 0a 20  ze!=newSize ){. 
10954 20 20 20 20 20 69 66 28 20 63 75 72 72 65 6e 74       if( current
10955 53 69 7a 65 3e 6e 65 77 53 69 7a 65 20 29 7b 0a  Size>newSize ){.
10956 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
10957 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
10958 50 61 67 65 72 2d 3e 66 64 2c 20 6e 65 77 53 69  Pager->fd, newSi
10959 7a 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ze);.      }else
1095a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
1095b 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70 50  qlite3OsWrite(pP
1095c 61 67 65 72 2d 3e 66 64 2c 20 22 22 2c 20 31 2c  ager->fd, "", 1,
1095d 20 6e 65 77 53 69 7a 65 2d 31 29 3b 0a 20 20 20   newSize-1);.   
1095e 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
1095f 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
10960 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72  OK ){.    pPager
10961 2d 3e 64 62 53 69 7a 65 20 3d 20 6e 50 61 67 65  ->dbSize = nPage
10962 3b 0a 20 20 20 20 70 61 67 65 72 5f 74 72 75 6e  ;.    pager_trun
10963 63 61 74 65 5f 63 61 63 68 65 28 70 50 61 67 65  cate_cache(pPage
10964 72 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  r);.  }.  return
10965 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65   rc;.}../*.** Se
10966 74 20 74 68 65 20 73 65 63 74 6f 72 53 69 7a 65  t the sectorSize
10967 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70   for the given p
10968 61 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ager..**.** The 
10969 73 65 63 74 6f 72 20 73 69 7a 65 20 69 73 20 61  sector size is a
1096a 74 20 6c 65 61 73 74 20 61 73 20 62 69 67 20 61  t least as big a
1096b 73 20 74 68 65 20 73 65 63 74 6f 72 20 73 69 7a  s the sector siz
1096c 65 20 72 65 70 6f 72 74 65 64 0a 2a 2a 20 62 79  e reported.** by
1096d 20 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72   sqlite3OsSector
1096e 53 69 7a 65 28 29 2e 20 20 54 68 65 20 6d 69 6e  Size().  The min
1096f 69 6d 75 6d 20 73 65 63 74 6f 72 20 73 69 7a 65  imum sector size
10970 20 69 73 20 35 31 32 2e 0a 2a 2f 0a 73 74 61 74   is 512..*/.stat
10971 69 63 20 76 6f 69 64 20 73 65 74 53 65 63 74 6f  ic void setSecto
10972 72 53 69 7a 65 28 50 61 67 65 72 20 2a 70 50 61  rSize(Pager *pPa
10973 67 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 70  ger){.  assert(p
10974 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68  Pager->fd->pMeth
10975 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65 6d  ods||pPager->tem
10976 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 21 70  pFile);.  if( !p
10977 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20  Pager->tempFile 
10978 29 7b 0a 20 20 20 20 2f 2a 20 53 65 63 74 6f 72  ){.    /* Sector
10979 20 73 69 7a 65 20 64 6f 65 73 6e 27 74 20 6d 61   size doesn't ma
1097a 74 74 65 72 20 66 6f 72 20 74 65 6d 70 6f 72 61  tter for tempora
1097b 72 79 20 66 69 6c 65 73 2e 20 41 6c 73 6f 2c 20  ry files. Also, 
1097c 74 68 65 20 66 69 6c 65 0a 20 20 20 20 2a 2a 20  the file.    ** 
1097d 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 62 65 65  may not have bee
1097e 6e 20 6f 70 65 6e 65 64 20 79 65 74 2c 20 69 6e  n opened yet, in
1097f 20 77 68 63 69 68 20 63 61 73 65 20 74 68 65 20   whcih case the 
10980 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 29 0a 20  OsSectorSize(). 
10981 20 20 20 2a 2a 20 63 61 6c 6c 20 77 69 6c 6c 20     ** call will 
10982 73 65 67 66 61 75 6c 74 2e 0a 20 20 20 20 2a 2f  segfault..    */
10983 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63  .    pPager->sec
10984 74 6f 72 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  torSize = sqlite
10985 33 4f 73 53 65 63 74 6f 72 53 69 7a 65 28 70 50  3OsSectorSize(pP
10986 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 7d 0a 20  ager->fd);.  }. 
10987 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 65 63   if( pPager->sec
10988 74 6f 72 53 69 7a 65 3c 35 31 32 20 29 7b 0a 20  torSize<512 ){. 
10989 20 20 20 70 50 61 67 65 72 2d 3e 73 65 63 74 6f     pPager->secto
1098a 72 53 69 7a 65 20 3d 20 35 31 32 3b 0a 20 20 7d  rSize = 512;.  }
1098b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 6c 61 79 62 61  .}../*.** Playba
1098c 63 6b 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 61  ck the journal a
1098d 6e 64 20 74 68 75 73 20 72 65 73 74 6f 72 65 20  nd thus restore 
1098e 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1098f 65 20 74 6f 0a 2a 2a 20 74 68 65 20 73 74 61 74  e to.** the stat
10990 65 20 69 74 20 77 61 73 20 69 6e 20 62 65 66 6f  e it was in befo
10991 72 65 20 77 65 20 73 74 61 72 74 65 64 20 6d 61  re we started ma
10992 6b 69 6e 67 20 63 68 61 6e 67 65 73 2e 20 20 0a  king changes.  .
10993 2a 2a 0a 2a 2a 20 54 68 65 20 6a 6f 75 72 6e 61  **.** The journa
10994 6c 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 69 73  l file format is
10995 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 20 0a 2a 2a   as follows: .**
10996 0a 2a 2a 20 20 28 31 29 20 20 38 20 62 79 74 65  .**  (1)  8 byte
10997 20 70 72 65 66 69 78 2e 20 20 41 20 63 6f 70 79   prefix.  A copy
10998 20 6f 66 20 61 4a 6f 75 72 6e 61 6c 4d 61 67 69   of aJournalMagi
10999 63 5b 5d 2e 0a 2a 2a 20 20 28 32 29 20 20 34 20  c[]..**  (2)  4 
1099a 62 79 74 65 20 62 69 67 2d 65 6e 64 69 61 6e 20  byte big-endian 
1099b 69 6e 74 65 67 65 72 20 77 68 69 63 68 20 69 73  integer which is
1099c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 76   the number of v
1099d 61 6c 69 64 20 70 61 67 65 20 72 65 63 6f 72 64  alid page record
1099e 73 0a 2a 2a 20 20 20 20 20 20 20 69 6e 20 74 68  s.**       in th
1099f 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 66 20 74  e journal.  If t
109a0 68 69 73 20 76 61 6c 75 65 20 69 73 20 30 78 66  his value is 0xf
109a1 66 66 66 66 66 66 66 2c 20 74 68 65 6e 20 63 6f  fffffff, then co
109a2 6d 70 75 74 65 20 74 68 65 0a 2a 2a 20 20 20 20  mpute the.**    
109a3 20 20 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67     number of pag
109a4 65 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 74  e records from t
109a5 68 65 20 6a 6f 75 72 6e 61 6c 20 73 69 7a 65 2e  he journal size.
109a6 0a 2a 2a 20 20 28 33 29 20 20 34 20 62 79 74 65  .**  (3)  4 byte
109a7 20 62 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65   big-endian inte
109a8 67 65 72 20 77 68 69 63 68 20 69 73 20 74 68 65  ger which is the
109a9 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 66   initial value f
109aa 6f 72 20 74 68 65 20 0a 2a 2a 20 20 20 20 20 20  or the .**      
109ab 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73 75 6d   sanity checksum
109ac 2e 0a 2a 2a 20 20 28 34 29 20 20 34 20 62 79 74  ..**  (4)  4 byt
109ad 65 20 69 6e 74 65 67 65 72 20 77 68 69 63 68 20  e integer which 
109ae 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
109af 20 70 61 67 65 73 20 74 6f 20 74 72 75 6e 63 61   pages to trunca
109b0 74 65 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20  te the.**       
109b1 64 61 74 61 62 61 73 65 20 74 6f 20 64 75 72 69  database to duri
109b2 6e 67 20 61 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a  ng a rollback..*
109b3 2a 20 20 28 35 29 20 20 34 20 62 79 74 65 20 62  *  (5)  4 byte b
109b4 69 67 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65  ig-endian intege
109b5 72 20 77 68 69 63 68 20 69 73 20 74 68 65 20 73  r which is the s
109b6 65 63 74 6f 72 20 73 69 7a 65 2e 20 20 54 68 65  ector size.  The
109b7 20 68 65 61 64 65 72 0a 2a 2a 20 20 20 20 20 20   header.**      
109b8 20 69 73 20 74 68 69 73 20 6d 61 6e 79 20 62 79   is this many by
109b9 74 65 73 20 69 6e 20 73 69 7a 65 2e 0a 2a 2a 20  tes in size..** 
109ba 20 28 36 29 20 20 34 20 62 79 74 65 20 62 69 67   (6)  4 byte big
109bb 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
109bc 77 68 69 63 68 20 69 73 20 74 68 65 20 70 61 67  which is the pag
109bd 65 20 63 61 73 65 2e 0a 2a 2a 20 20 28 37 29 20  e case..**  (7) 
109be 20 34 20 62 79 74 65 20 69 6e 74 65 67 65 72 20   4 byte integer 
109bf 77 68 69 63 68 20 69 73 20 74 68 65 20 6e 75 6d  which is the num
109c0 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
109c1 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
109c2 61 6c 0a 2a 2a 20 20 20 20 20 20 20 6e 61 6d 65  al.**       name
109c3 2e 20 20 54 68 65 20 76 61 6c 75 65 20 6d 61 79  .  The value may
109c4 20 62 65 20 7a 65 72 6f 20 28 69 6e 64 69 63 61   be zero (indica
109c5 74 65 20 74 68 61 74 20 74 68 65 72 65 20 69 73  te that there is
109c6 20 6e 6f 20 6d 61 73 74 65 72 0a 2a 2a 20 20 20   no master.**   
109c7 20 20 20 20 6a 6f 75 72 6e 61 6c 2e 29 0a 2a 2a      journal.).**
109c8 20 20 28 38 29 20 20 4e 20 62 79 74 65 73 20 6f    (8)  N bytes o
109c9 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75  f the master jou
109ca 72 6e 61 6c 20 6e 61 6d 65 2e 20 20 54 68 65 20  rnal name.  The 
109cb 6e 61 6d 65 20 77 69 6c 6c 20 62 65 20 6e 75 6c  name will be nul
109cc 2d 74 65 72 6d 69 6e 61 74 65 64 0a 2a 2a 20 20  -terminated.**  
109cd 20 20 20 20 20 61 6e 64 20 6d 69 67 68 74 20 62       and might b
109ce 65 20 73 68 6f 72 74 65 72 20 74 68 61 6e 20 74  e shorter than t
109cf 68 65 20 76 61 6c 75 65 20 72 65 61 64 20 66 72  he value read fr
109d0 6f 6d 20 28 35 29 2e 20 20 49 66 20 74 68 65 20  om (5).  If the 
109d1 66 69 72 73 74 20 62 79 74 65 0a 2a 2a 20 20 20  first byte.**   
109d2 20 20 20 20 6f 66 20 74 68 65 20 6e 61 6d 65 20      of the name 
109d3 69 73 20 5c 30 30 30 20 74 68 65 6e 20 74 68 65  is \000 then the
109d4 72 65 20 69 73 20 6e 6f 20 6d 61 73 74 65 72 20  re is no master 
109d5 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 20 6d 61  journal.  The ma
109d6 73 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 6a 6f  ster.**       jo
109d7 75 72 6e 61 6c 20 6e 61 6d 65 20 69 73 20 73 74  urnal name is st
109d8 6f 72 65 64 20 69 6e 20 55 54 46 2d 38 2e 0a 2a  ored in UTF-8..*
109d9 2a 20 20 28 39 29 20 20 5a 65 72 6f 20 6f 72 20  *  (9)  Zero or 
109da 6d 6f 72 65 20 70 61 67 65 73 20 69 6e 73 74 61  more pages insta
109db 6e 63 65 73 2c 20 65 61 63 68 20 61 73 20 66 6f  nces, each as fo
109dc 6c 6c 6f 77 73 3a 0a 2a 2a 20 20 20 20 20 20 20  llows:.**       
109dd 20 2b 20 20 34 20 62 79 74 65 20 70 61 67 65 20   +  4 byte page 
109de 6e 75 6d 62 65 72 2e 0a 2a 2a 20 20 20 20 20 20  number..**      
109df 20 20 2b 20 20 70 50 61 67 65 72 2d 3e 70 61 67    +  pPager->pag
109e0 65 53 69 7a 65 20 62 79 74 65 73 20 6f 66 20 64  eSize bytes of d
109e1 61 74 61 2e 0a 2a 2a 20 20 20 20 20 20 20 20 2b  ata..**        +
109e2 20 20 34 20 62 79 74 65 20 63 68 65 63 6b 73 75    4 byte checksu
109e3 6d 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 77 65 20  m.**.** When we 
109e4 73 70 65 61 6b 20 6f 66 20 74 68 65 20 6a 6f 75  speak of the jou
109e5 72 6e 61 6c 20 68 65 61 64 65 72 2c 20 77 65 20  rnal header, we 
109e6 6d 65 61 6e 20 74 68 65 20 66 69 72 73 74 20 38  mean the first 8
109e7 20 69 74 65 6d 73 20 61 62 6f 76 65 2e 0a 2a 2a   items above..**
109e8 20 45 61 63 68 20 65 6e 74 72 79 20 69 6e 20 74   Each entry in t
109e9 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20 61 6e  he journal is an
109ea 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65   instance of the
109eb 20 39 74 68 20 69 74 65 6d 2e 0a 2a 2a 0a 2a 2a   9th item..**.**
109ec 20 43 61 6c 6c 20 74 68 65 20 76 61 6c 75 65 20   Call the value 
109ed 66 72 6f 6d 20 74 68 65 20 73 65 63 6f 6e 64 20  from the second 
109ee 62 75 6c 6c 65 74 20 22 6e 52 65 63 22 2e 20 20  bullet "nRec".  
109ef 6e 52 65 63 20 69 73 20 74 68 65 20 6e 75 6d 62  nRec is the numb
109f0 65 72 20 6f 66 0a 2a 2a 20 76 61 6c 69 64 20 70  er of.** valid p
109f1 61 67 65 20 65 6e 74 72 69 65 73 20 69 6e 20 74  age entries in t
109f2 68 65 20 6a 6f 75 72 6e 61 6c 2e 20 20 49 6e 20  he journal.  In 
109f3 6d 6f 73 74 20 63 61 73 65 73 2c 20 79 6f 75 20  most cases, you 
109f4 63 61 6e 20 63 6f 6d 70 75 74 65 20 74 68 65 0a  can compute the.
109f5 2a 2a 20 76 61 6c 75 65 20 6f 66 20 6e 52 65 63  ** value of nRec
109f6 20 66 72 6f 6d 20 74 68 65 20 73 69 7a 65 20 6f   from the size o
109f7 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
109f8 6c 65 2e 20 20 42 75 74 20 69 66 20 61 20 70 6f  le.  But if a po
109f9 77 65 72 0a 2a 2a 20 66 61 69 6c 75 72 65 20 6f  wer.** failure o
109fa 63 63 75 72 72 65 64 20 77 68 69 6c 65 20 74 68  ccurred while th
109fb 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62 65  e journal was be
109fc 69 6e 67 20 77 72 69 74 74 65 6e 2c 20 69 74 20  ing written, it 
109fd 63 6f 75 6c 64 20 62 65 20 74 68 65 0a 2a 2a 20  could be the.** 
109fe 63 61 73 65 20 74 68 61 74 20 74 68 65 20 73 69  case that the si
109ff 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ze of the journa
10a00 6c 20 66 69 6c 65 20 68 61 64 20 61 6c 72 65 61  l file had alrea
10a01 64 79 20 62 65 65 6e 20 69 6e 63 72 65 61 73 65  dy been increase
10a02 64 20 62 75 74 0a 2a 2a 20 74 68 65 20 65 78 74  d but.** the ext
10a03 72 61 20 65 6e 74 72 69 65 73 20 68 61 64 20 6e  ra entries had n
10a04 6f 74 20 79 65 74 20 6d 61 64 65 20 69 74 20 73  ot yet made it s
10a05 61 66 65 6c 79 20 74 6f 20 64 69 73 6b 2e 20 20  afely to disk.  
10a06 49 6e 20 73 75 63 68 20 61 20 63 61 73 65 2c 0a  In such a case,.
10a07 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ** the value of 
10a08 6e 52 65 63 20 63 6f 6d 70 75 74 65 64 20 66 72  nRec computed fr
10a09 6f 6d 20 74 68 65 20 66 69 6c 65 20 73 69 7a 65  om the file size
10a0a 20 77 6f 75 6c 64 20 62 65 20 74 6f 6f 20 6c 61   would be too la
10a0b 72 67 65 2e 20 20 46 6f 72 0a 2a 2a 20 74 68 61  rge.  For.** tha
10a0c 74 20 72 65 61 73 6f 6e 2c 20 77 65 20 61 6c 77  t reason, we alw
10a0d 61 79 73 20 75 73 65 20 74 68 65 20 6e 52 65 63  ays use the nRec
10a0e 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 68 65   value in the he
10a0f 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ader..**.** If t
10a10 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69 73  he nRec value is
10a11 20 30 78 66 66 66 66 66 66 66 66 20 69 74 20 6d   0xffffffff it m
10a12 65 61 6e 73 20 74 68 61 74 20 6e 52 65 63 20 73  eans that nRec s
10a13 68 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65  hould be compute
10a14 64 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 66 69  d.** from the fi
10a15 6c 65 20 73 69 7a 65 2e 20 20 54 68 69 73 20 76  le size.  This v
10a16 61 6c 75 65 20 69 73 20 75 73 65 64 20 77 68 65  alue is used whe
10a17 6e 20 74 68 65 20 75 73 65 72 20 73 65 6c 65 63  n the user selec
10a18 74 73 20 74 68 65 0a 2a 2a 20 6e 6f 2d 73 79 6e  ts the.** no-syn
10a19 63 20 6f 70 74 69 6f 6e 20 66 6f 72 20 74 68 65  c option for the
10a1a 20 6a 6f 75 72 6e 61 6c 2e 20 20 41 20 70 6f 77   journal.  A pow
10a1b 65 72 20 66 61 69 6c 75 72 65 20 63 6f 75 6c 64  er failure could
10a1c 20 6c 65 61 64 20 74 6f 20 63 6f 72 72 75 70 74   lead to corrupt
10a1d 69 6f 6e 0a 2a 2a 20 69 6e 20 74 68 69 73 20 63  ion.** in this c
10a1e 61 73 65 2e 20 20 42 75 74 20 66 6f 72 20 74 68  ase.  But for th
10a1f 69 6e 67 73 20 6c 69 6b 65 20 74 65 6d 70 6f 72  ings like tempor
10a20 61 72 79 20 74 61 62 6c 65 20 28 77 68 69 63 68  ary table (which
10a21 20 77 69 6c 6c 20 62 65 0a 2a 2a 20 64 65 6c 65   will be.** dele
10a22 74 65 64 20 77 68 65 6e 20 74 68 65 20 70 6f 77  ted when the pow
10a23 65 72 20 69 73 20 72 65 73 74 6f 72 65 64 29 20  er is restored) 
10a24 77 65 20 64 6f 6e 27 74 20 63 61 72 65 2e 20 20  we don't care.  
10a25 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 69  .**.** If the fi
10a26 6c 65 20 6f 70 65 6e 65 64 20 61 73 20 74 68 65  le opened as the
10a27 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
10a28 20 6e 6f 74 20 61 20 77 65 6c 6c 2d 66 6f 72 6d   not a well-form
10a29 65 64 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69  ed.** journal fi
10a2a 6c 65 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65  le then all page
10a2b 73 20 75 70 20 74 6f 20 74 68 65 20 66 69 72 73  s up to the firs
10a2c 74 20 63 6f 72 72 75 70 74 65 64 20 70 61 67 65  t corrupted page
10a2d 20 61 72 65 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62   are rolled.** b
10a2e 61 63 6b 20 28 6f 72 20 6e 6f 20 70 61 67 65 73  ack (or no pages
10a2f 20 69 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   if the journal 
10a30 68 65 61 64 65 72 20 69 73 20 63 6f 72 72 75 70  header is corrup
10a31 74 65 64 29 2e 20 54 68 65 20 6a 6f 75 72 6e 61  ted). The journa
10a32 6c 20 66 69 6c 65 0a 2a 2a 20 69 73 20 74 68 65  l file.** is the
10a33 6e 20 64 65 6c 65 74 65 64 20 61 6e 64 20 53 51  n deleted and SQ
10a34 4c 49 54 45 5f 4f 4b 20 72 65 74 75 72 6e 65 64  LITE_OK returned
10a35 2c 20 6a 75 73 74 20 61 73 20 69 66 20 6e 6f 20  , just as if no 
10a36 63 6f 72 72 75 70 74 69 6f 6e 20 68 61 64 0a 2a  corruption had.*
10a37 2a 20 62 65 65 6e 20 65 6e 63 6f 75 6e 74 65 72  * been encounter
10a38 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  ed..**.** If an 
10a39 49 2f 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20  I/O or malloc() 
10a3a 65 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 74 68  error occurs, th
10a3b 65 20 6a 6f 75 72 6e 61 6c 2d 66 69 6c 65 20 69  e journal-file i
10a3c 73 20 6e 6f 74 20 64 65 6c 65 74 65 64 0a 2a 2a  s not deleted.**
10a3d 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f   and an error co
10a3e 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  de is returned..
10a3f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61  */.static int pa
10a40 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 50 61 67  ger_playback(Pag
10a41 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
10a42 69 73 48 6f 74 29 7b 0a 20 20 73 71 6c 69 74 65  isHot){.  sqlite
10a43 33 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 70 50  3_vfs *pVfs = pP
10a44 61 67 65 72 2d 3e 70 56 66 73 3b 0a 20 20 69 36  ager->pVfs;.  i6
10a45 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
10a46 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
10a47 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
10a48 6c 65 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20  le in bytes */. 
10a49 20 75 33 32 20 6e 52 65 63 3b 20 20 20 20 20 20   u32 nRec;      
10a4a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
10a4b 62 65 72 20 6f 66 20 52 65 63 6f 72 64 73 20 69  ber of Records i
10a4c 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 2a 2f  n the journal */
10a4d 0a 20 20 75 33 32 20 75 3b 20 20 20 20 20 20 20  .  u32 u;       
10a4e 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
10a4f 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63 6f 75  nsigned loop cou
10a50 6e 74 65 72 20 2a 2f 0a 20 20 50 67 6e 6f 20 6d  nter */.  Pgno m
10a51 78 50 67 20 3d 20 30 3b 20 20 20 20 20 20 20 20  xPg = 0;        
10a52 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68     /* Size of th
10a53 65 20 6f 72 69 67 69 6e 61 6c 20 66 69 6c 65 20  e original file 
10a54 69 6e 20 70 61 67 65 73 20 2a 2f 0a 20 20 69 6e  in pages */.  in
10a55 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20 20  t rc;           
10a56 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
10a57 20 63 6f 64 65 20 6f 66 20 61 20 73 75 62 72 6f   code of a subro
10a58 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72  utine */.  int r
10a59 65 73 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  es = 1;         
10a5a 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65 74      /* Value ret
10a5b 75 72 6e 65 64 20 62 79 20 73 71 6c 69 74 65 33  urned by sqlite3
10a5c 4f 73 41 63 63 65 73 73 28 29 20 2a 2f 0a 20 20  OsAccess() */.  
10a5d 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d 20  char *zMaster = 
10a5e 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65  0;       /* Name
10a5f 20 6f 66 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e   of master journ
10a60 61 6c 20 66 69 6c 65 20 69 66 20 61 6e 79 20 2a  al file if any *
10a61 2f 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  /..  /* Figure o
10a62 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f  ut how many reco
10a63 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 6a  rds are in the j
10a64 6f 75 72 6e 61 6c 2e 20 20 41 62 6f 72 74 20 65  ournal.  Abort e
10a65 61 72 6c 79 20 69 66 0a 20 20 2a 2a 20 74 68 65  arly if.  ** the
10a66 20 6a 6f 75 72 6e 61 6c 20 69 73 20 65 6d 70 74   journal is empt
10a67 79 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  y..  */.  assert
10a68 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
10a69 6c 4f 70 65 6e 20 29 3b 0a 20 20 72 63 20 3d 20  lOpen );.  rc = 
10a6a 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53 69 7a  sqlite3OsFileSiz
10a6b 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 26  e(pPager->jfd, &
10a6c 73 7a 4a 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  szJ);.  if( rc!=
10a6d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 73 7a 4a  SQLITE_OK || szJ
10a6e 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
10a6f 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  end_playback;.  
10a70 7d 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  }..  /* Read the
10a71 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
10a72 6e 61 6d 65 20 66 72 6f 6d 20 74 68 65 20 6a 6f  name from the jo
10a73 75 72 6e 61 6c 2c 20 69 66 20 69 74 20 69 73 20  urnal, if it is 
10a74 70 72 65 73 65 6e 74 2e 0a 20 20 2a 2a 20 49 66  present..  ** If
10a75 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
10a76 6c 20 66 69 6c 65 20 6e 61 6d 65 20 69 73 20 73  l file name is s
10a77 70 65 63 69 66 69 65 64 2c 20 62 75 74 20 74 68  pecified, but th
10a78 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 0a 20 20  e file is not.  
10a79 2a 2a 20 70 72 65 73 65 6e 74 20 6f 6e 20 64 69  ** present on di
10a7a 73 6b 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75  sk, then the jou
10a7b 72 6e 61 6c 20 69 73 20 6e 6f 74 20 68 6f 74 20  rnal is not hot 
10a7c 61 6e 64 20 64 6f 65 73 20 6e 6f 74 20 6e 65 65  and does not nee
10a7d 64 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 6c 61  d to be.  ** pla
10a7e 79 65 64 20 62 61 63 6b 2e 0a 20 20 2a 2f 0a 20  yed back..  */. 
10a7f 20 7a 4d 61 73 74 65 72 20 3d 20 70 50 61 67 65   zMaster = pPage
10a80 72 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20  r->pTmpSpace;.  
10a81 72 63 20 3d 20 72 65 61 64 4d 61 73 74 65 72 4a  rc = readMasterJ
10a82 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 6a  ournal(pPager->j
10a83 66 64 2c 20 7a 4d 61 73 74 65 72 2c 20 70 50 61  fd, zMaster, pPa
10a84 67 65 72 2d 3e 70 56 66 73 2d 3e 6d 78 50 61 74  ger->pVfs->mxPat
10a85 68 6e 61 6d 65 2b 31 29 3b 0a 20 20 69 66 28 20  hname+1);.  if( 
10a86 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
10a87 20 7a 4d 61 73 74 65 72 5b 30 5d 20 29 7b 0a 20   zMaster[0] ){. 
10a88 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
10a89 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 7a 4d  sAccess(pVfs, zM
10a8a 61 73 74 65 72 2c 20 53 51 4c 49 54 45 5f 41 43  aster, SQLITE_AC
10a8b 43 45 53 53 5f 45 58 49 53 54 53 2c 20 26 72 65  CESS_EXISTS, &re
10a8c 73 29 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73 74 65  s);.  }.  zMaste
10a8d 72 20 3d 20 30 3b 0a 20 20 69 66 28 20 72 63 21  r = 0;.  if( rc!
10a8e 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 72  =SQLITE_OK || !r
10a8f 65 73 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  es ){.    goto e
10a90 6e 64 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20 7d  nd_playback;.  }
10a91 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  .  pPager->journ
10a92 61 6c 4f 66 66 20 3d 20 30 3b 0a 0a 20 20 2f 2a  alOff = 0;..  /*
10a93 20 54 68 69 73 20 6c 6f 6f 70 20 74 65 72 6d 69   This loop termi
10a94 6e 61 74 65 73 20 65 69 74 68 65 72 20 77 68 65  nates either whe
10a95 6e 20 74 68 65 20 72 65 61 64 4a 6f 75 72 6e 61  n the readJourna
10a96 6c 48 64 72 28 29 20 63 61 6c 6c 20 72 65 74 75  lHdr() call retu
10a97 72 6e 73 0a 20 20 2a 2a 20 53 51 4c 49 54 45 5f  rns.  ** SQLITE_
10a98 44 4f 4e 45 20 6f 72 20 61 6e 20 49 4f 20 65 72  DONE or an IO er
10a99 72 6f 72 20 6f 63 63 75 72 73 2e 20 2a 2f 0a 20  ror occurs. */. 
10a9a 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 0a 20 20   while( 1 ){..  
10a9b 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 6e 65    /* Read the ne
10a9c 78 74 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  xt journal heade
10a9d 72 20 66 72 6f 6d 20 74 68 65 20 6a 6f 75 72 6e  r from the journ
10a9e 61 6c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65  al file.  If the
10a9f 72 65 20 61 72 65 0a 20 20 20 20 2a 2a 20 6e 6f  re are.    ** no
10aa0 74 20 65 6e 6f 75 67 68 20 62 79 74 65 73 20 6c  t enough bytes l
10aa1 65 66 74 20 69 6e 20 74 68 65 20 6a 6f 75 72 6e  eft in the journ
10aa2 61 6c 20 66 69 6c 65 20 66 6f 72 20 61 20 63 6f  al file for a co
10aa3 6d 70 6c 65 74 65 20 68 65 61 64 65 72 2c 20 6f  mplete header, o
10aa4 72 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 63  r.    ** it is c
10aa5 6f 72 72 75 70 74 65 64 2c 20 74 68 65 6e 20 61  orrupted, then a
10aa6 20 70 72 6f 63 65 73 73 20 6d 75 73 74 20 6f 66   process must of
10aa7 20 66 61 69 6c 65 64 20 77 68 69 6c 65 20 77 72   failed while wr
10aa8 69 74 69 6e 67 20 69 74 2e 0a 20 20 20 20 2a 2a  iting it..    **
10aa9 20 54 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   This indicates 
10aaa 6e 6f 74 68 69 6e 67 20 6d 6f 72 65 20 6e 65 65  nothing more nee
10aab 64 73 20 74 6f 20 62 65 20 72 6f 6c 6c 65 64 20  ds to be rolled 
10aac 62 61 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  back..    */.   
10aad 20 72 63 20 3d 20 72 65 61 64 4a 6f 75 72 6e 61   rc = readJourna
10aae 6c 48 64 72 28 70 50 61 67 65 72 2c 20 73 7a 4a  lHdr(pPager, szJ
10aaf 2c 20 26 6e 52 65 63 2c 20 26 6d 78 50 67 29 3b  , &nRec, &mxPg);
10ab0 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
10ab1 49 54 45 5f 4f 4b 20 29 7b 20 0a 20 20 20 20 20  ITE_OK ){ .     
10ab2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
10ab3 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 20 20  DONE ){.        
10ab4 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
10ab5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f        }.      go
10ab6 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b  to end_playback;
10ab7 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
10ab8 66 20 6e 52 65 63 20 69 73 20 30 78 66 66 66 66  f nRec is 0xffff
10ab9 66 66 66 66 2c 20 74 68 65 6e 20 74 68 69 73 20  ffff, then this 
10aba 6a 6f 75 72 6e 61 6c 20 77 61 73 20 63 72 65 61  journal was crea
10abb 74 65 64 20 62 79 20 61 20 70 72 6f 63 65 73 73  ted by a process
10abc 0a 20 20 20 20 2a 2a 20 77 6f 72 6b 69 6e 67 20  .    ** working 
10abd 69 6e 20 6e 6f 2d 73 79 6e 63 20 6d 6f 64 65 2e  in no-sync mode.
10abe 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68 61 74   This means that
10abf 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
10ac0 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20   journal.    ** 
10ac1 66 69 6c 65 20 63 6f 6e 73 69 73 74 73 20 6f 66  file consists of
10ac2 20 70 61 67 65 73 2c 20 74 68 65 72 65 20 61 72   pages, there ar
10ac3 65 20 6e 6f 20 6d 6f 72 65 20 6a 6f 75 72 6e 61  e no more journa
10ac4 6c 20 68 65 61 64 65 72 73 2e 20 43 6f 6d 70 75  l headers. Compu
10ac5 74 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 76 61  te.    ** the va
10ac6 6c 75 65 20 6f 66 20 6e 52 65 63 20 62 61 73 65  lue of nRec base
10ac7 64 20 6f 6e 20 74 68 69 73 20 61 73 73 75 6d 70  d on this assump
10ac8 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  tion..    */.   
10ac9 20 69 66 28 20 6e 52 65 63 3d 3d 30 78 66 66 66   if( nRec==0xfff
10aca 66 66 66 66 66 20 29 7b 0a 20 20 20 20 20 20 61  fffff ){.      a
10acb 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 6a  ssert( pPager->j
10acc 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52 4e  ournalOff==JOURN
10acd 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72  AL_HDR_SZ(pPager
10ace 29 20 29 3b 0a 20 20 20 20 20 20 6e 52 65 63 20  ) );.      nRec 
10acf 3d 20 28 73 7a 4a 20 2d 20 4a 4f 55 52 4e 41 4c  = (szJ - JOURNAL
10ad0 5f 48 44 52 5f 53 5a 28 70 50 61 67 65 72 29 29  _HDR_SZ(pPager))
10ad1 2f 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53 5a 28 70  /JOURNAL_PG_SZ(p
10ad2 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20  Pager);.    }.. 
10ad3 20 20 20 2f 2a 20 49 66 20 6e 52 65 63 20 69 73     /* If nRec is
10ad4 20 30 20 61 6e 64 20 74 68 69 73 20 72 6f 6c 6c   0 and this roll
10ad5 62 61 63 6b 20 69 73 20 6f 66 20 61 20 74 72 61  back is of a tra
10ad6 6e 73 61 63 74 69 6f 6e 20 63 72 65 61 74 65 64  nsaction created
10ad7 20 62 79 20 74 68 69 73 0a 20 20 20 20 2a 2a 20   by this.    ** 
10ad8 70 72 6f 63 65 73 73 20 61 6e 64 20 69 66 20 74  process and if t
10ad9 68 69 73 20 69 73 20 74 68 65 20 66 69 6e 61 6c  his is the final
10ada 20 68 65 61 64 65 72 20 69 6e 20 74 68 65 20 6a   header in the j
10adb 6f 75 72 6e 61 6c 2c 20 74 68 65 6e 20 69 74 20  ournal, then it 
10adc 6d 65 61 6e 73 0a 20 20 20 20 2a 2a 20 74 68 61  means.    ** tha
10add 74 20 74 68 69 73 20 70 61 72 74 20 6f 66 20 74  t this part of t
10ade 68 65 20 6a 6f 75 72 6e 61 6c 20 77 61 73 20 62  he journal was b
10adf 65 69 6e 67 20 66 69 6c 6c 65 64 20 62 75 74 20  eing filled but 
10ae0 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
10ae1 0a 20 20 20 20 2a 2a 20 73 79 6e 63 65 64 20 74  .    ** synced t
10ae2 6f 20 64 69 73 6b 2e 20 20 43 6f 6d 70 75 74 65  o disk.  Compute
10ae3 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
10ae4 61 67 65 73 20 62 61 73 65 64 20 6f 6e 20 74 68  ages based on th
10ae5 65 20 72 65 6d 61 69 6e 69 6e 67 0a 20 20 20 20  e remaining.    
10ae6 2a 2a 20 73 69 7a 65 20 6f 66 20 74 68 65 20 66  ** size of the f
10ae7 69 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ile..    **.    
10ae8 2a 2a 20 54 68 65 20 74 68 69 72 64 20 74 65 72  ** The third ter
10ae9 6d 20 6f 66 20 74 68 65 20 74 65 73 74 20 77 61  m of the test wa
10aea 73 20 61 64 64 65 64 20 74 6f 20 66 69 78 20 74  s added to fix t
10aeb 69 63 6b 65 74 20 23 32 35 36 35 2e 0a 20 20 20  icket #2565..   
10aec 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 52 65 63   */.    if( nRec
10aed 3d 3d 30 20 26 26 20 21 69 73 48 6f 74 20 26 26  ==0 && !isHot &&
10aee 0a 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d  .        pPager-
10aef 3e 6a 6f 75 72 6e 61 6c 48 64 72 2b 4a 4f 55 52  >journalHdr+JOUR
10af0 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
10af1 72 29 3d 3d 70 50 61 67 65 72 2d 3e 6a 6f 75 72  r)==pPager->jour
10af2 6e 61 6c 4f 66 66 20 29 7b 0a 20 20 20 20 20 20  nalOff ){.      
10af3 6e 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 70 50  nRec = (szJ - pP
10af4 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
10af5 29 20 2f 20 4a 4f 55 52 4e 41 4c 5f 50 47 5f 53  ) / JOURNAL_PG_S
10af6 5a 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d  Z(pPager);.    }
10af7 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73  ..    /* If this
10af8 20 69 73 20 74 68 65 20 66 69 72 73 74 20 68 65   is the first he
10af9 61 64 65 72 20 72 65 61 64 20 66 72 6f 6d 20 74  ader read from t
10afa 68 65 20 6a 6f 75 72 6e 61 6c 2c 20 74 72 75 6e  he journal, trun
10afb 63 61 74 65 20 74 68 65 0a 20 20 20 20 2a 2a 20  cate the.    ** 
10afc 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 62 61  database file ba
10afd 63 6b 20 74 6f 20 69 74 73 20 6f 72 69 67 69 6e  ck to its origin
10afe 61 6c 20 73 69 7a 65 2e 0a 20 20 20 20 2a 2f 0a  al size..    */.
10aff 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e      if( pPager->
10b00 6a 6f 75 72 6e 61 6c 4f 66 66 3d 3d 4a 4f 55 52  journalOff==JOUR
10b01 4e 41 4c 5f 48 44 52 5f 53 5a 28 70 50 61 67 65  NAL_HDR_SZ(pPage
10b02 72 29 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  r) ){.      rc =
10b03 20 70 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28   pager_truncate(
10b04 70 50 61 67 65 72 2c 20 6d 78 50 67 29 3b 0a 20  pPager, mxPg);. 
10b05 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
10b06 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
10b07 20 20 67 6f 74 6f 20 65 6e 64 5f 70 6c 61 79 62    goto end_playb
10b08 61 63 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ack;.      }.   
10b09 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 70 79 20   }..    /* Copy 
10b0a 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 73 20 6f  original pages o
10b0b 75 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61  ut of the journa
10b0c 6c 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20  l and back into 
10b0d 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
10b0e 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f  e..    */.    fo
10b0f 72 28 75 3d 30 3b 20 75 3c 6e 52 65 63 3b 20 75  r(u=0; u<nRec; u
10b10 2b 2b 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  ++){.      rc = 
10b11 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f  pager_playback_o
10b12 6e 65 5f 70 61 67 65 28 70 50 61 67 65 72 2c 20  ne_page(pPager, 
10b13 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61  pPager->jfd, pPa
10b14 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c  ger->journalOff,
10b15 20 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72   1);.      if( r
10b16 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
10b17 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
10b18 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
10b19 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
10b1a 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
10b1b 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
10b1c 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a 20 20 20  alOff = szJ;.   
10b1d 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
10b1e 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
10b1f 20 20 20 20 20 20 20 2f 2a 20 49 66 20 77 65 20         /* If we 
10b20 61 72 65 20 75 6e 61 62 6c 65 20 74 6f 20 72 6f  are unable to ro
10b21 6c 6c 62 61 63 6b 2c 20 74 68 65 6e 20 74 68 65  llback, then the
10b22 20 64 61 74 61 62 61 73 65 20 69 73 20 70 72 6f   database is pro
10b23 62 61 62 6c 79 0a 20 20 20 20 20 20 20 20 20 20  bably.          
10b24 2a 2a 20 67 6f 69 6e 67 20 74 6f 20 65 6e 64 20  ** going to end 
10b25 75 70 20 62 65 69 6e 67 20 63 6f 72 72 75 70 74  up being corrupt
10b26 2e 20 20 49 74 20 69 73 20 63 6f 72 72 75 70 74  .  It is corrupt
10b27 20 74 6f 20 75 73 2c 20 61 6e 79 68 6f 77 2e 0a   to us, anyhow..
10b28 20 20 20 20 20 20 20 20 20 20 2a 2a 20 50 65 72            ** Per
10b29 68 61 70 73 20 74 68 65 20 6e 65 78 74 20 70 72  haps the next pr
10b2a 6f 63 65 73 73 20 74 6f 20 63 6f 6d 65 20 61 6c  ocess to come al
10b2b 6f 6e 67 20 63 61 6e 20 66 69 78 20 69 74 2e 2e  ong can fix it..
10b2c 2e 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  ...          */.
10b2d 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
10b2e 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
10b2f 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
10b30 74 6f 20 65 6e 64 5f 70 6c 61 79 62 61 63 6b 3b  to end_playback;
10b31 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
10b32 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2f   }.    }.  }.  /
10b33 2a 4e 4f 54 52 45 41 43 48 45 44 2a 2f 0a 20 20  *NOTREACHED*/.  
10b34 61 73 73 65 72 74 28 20 30 20 29 3b 0a 0a 65 6e  assert( 0 );..en
10b35 64 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66  d_playback:.  if
10b36 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
10b37 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 20 3d  ){.    zMaster =
10b38 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53 70 61   pPager->pTmpSpa
10b39 63 65 3b 0a 20 20 20 20 72 63 20 3d 20 72 65 61  ce;.    rc = rea
10b3a 64 4d 61 73 74 65 72 4a 6f 75 72 6e 61 6c 28 70  dMasterJournal(p
10b3b 50 61 67 65 72 2d 3e 6a 66 64 2c 20 7a 4d 61 73  Pager->jfd, zMas
10b3c 74 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 56 66  ter, pPager->pVf
10b3d 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31 29  s->mxPathname+1)
10b3e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d  ;.  }.  if( rc==
10b3f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10b40 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e 64 5f   rc = pager_end_
10b41 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61 67  transaction(pPag
10b42 65 72 2c 20 7a 4d 61 73 74 65 72 5b 30 5d 21 3d  er, zMaster[0]!=
10b43 27 5c 30 27 29 3b 0a 20 20 7d 0a 20 20 69 66 28  '\0');.  }.  if(
10b44 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
10b45 26 20 7a 4d 61 73 74 65 72 5b 30 5d 20 26 26 20  & zMaster[0] && 
10b46 72 65 73 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  res ){.    /* If
10b47 20 74 68 65 72 65 20 77 61 73 20 61 20 6d 61 73   there was a mas
10b48 74 65 72 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  ter journal and 
10b49 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69 6c  this routine wil
10b4a 6c 20 72 65 74 75 72 6e 20 73 75 63 63 65 73 73  l return success
10b4b 2c 0a 20 20 20 20 2a 2a 20 73 65 65 20 69 66 20  ,.    ** see if 
10b4c 69 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 74  it is possible t
10b4d 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6d 61 73  o delete the mas
10b4e 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 0a 20 20 20  ter journal..   
10b4f 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 61 67   */.    rc = pag
10b50 65 72 5f 64 65 6c 6d 61 73 74 65 72 28 70 50 61  er_delmaster(pPa
10b51 67 65 72 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  ger, zMaster);. 
10b52 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 50 61 67   }..  /* The Pag
10b53 65 72 2e 73 65 63 74 6f 72 53 69 7a 65 20 76 61  er.sectorSize va
10b54 72 69 61 62 6c 65 20 6d 61 79 20 68 61 76 65 20  riable may have 
10b55 62 65 65 6e 20 75 70 64 61 74 65 64 20 77 68 69  been updated whi
10b56 6c 65 20 72 6f 6c 6c 69 6e 67 0a 20 20 2a 2a 20  le rolling.  ** 
10b57 62 61 63 6b 20 61 20 6a 6f 75 72 6e 61 6c 20 63  back a journal c
10b58 72 65 61 74 65 64 20 62 79 20 61 20 70 72 6f 63  reated by a proc
10b59 65 73 73 20 77 69 74 68 20 61 20 64 69 66 66 65  ess with a diffe
10b5a 72 65 6e 74 20 73 65 63 74 6f 72 20 73 69 7a 65  rent sector size
10b5b 0a 20 20 2a 2a 20 76 61 6c 75 65 2e 20 52 65 73  .  ** value. Res
10b5c 65 74 20 69 74 20 74 6f 20 74 68 65 20 63 6f 72  et it to the cor
10b5d 72 65 63 74 20 76 61 6c 75 65 20 66 6f 72 20 74  rect value for t
10b5e 68 69 73 20 70 72 6f 63 65 73 73 2e 0a 20 20 2a  his process..  *
10b5f 2f 0a 20 20 73 65 74 53 65 63 74 6f 72 53 69 7a  /.  setSectorSiz
10b60 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74  e(pPager);.  ret
10b61 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
10b62 20 50 6c 61 79 62 61 63 6b 20 74 68 65 20 73 74   Playback the st
10b63 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e  atement journal.
10b64 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 73  .**.** This is s
10b65 69 6d 69 6c 61 72 20 74 6f 20 70 6c 61 79 69 6e  imilar to playin
10b66 67 20 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73  g back the trans
10b67 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 62  action journal b
10b68 75 74 20 77 69 74 68 0a 2a 2a 20 61 20 66 65 77  ut with.** a few
10b69 20 65 78 74 72 61 20 74 77 69 73 74 73 2e 0a 2a   extra twists..*
10b6a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 54 68 65  *.**    (1)  The
10b6b 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
10b6c 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
10b6d 20 66 69 6c 65 20 61 74 20 74 68 65 20 73 74 61   file at the sta
10b6e 72 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  rt of.**        
10b6f 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69   the statement i
10b70 73 20 73 74 6f 72 65 64 20 69 6e 20 70 50 61 67  s stored in pPag
10b71 65 72 2d 3e 73 74 6d 74 53 69 7a 65 2c 20 6e 6f  er->stmtSize, no
10b72 74 20 69 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20  t in the.**     
10b73 20 20 20 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65      journal file
10b74 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a 2a 20 20   itself..**.**  
10b75 20 20 28 32 29 20 20 49 6e 20 61 64 64 69 74 69    (2)  In additi
10b76 6f 6e 20 74 6f 20 70 6c 61 79 69 6e 67 20 62 61  on to playing ba
10b77 63 6b 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  ck the statement
10b78 20 6a 6f 75 72 6e 61 6c 2c 20 61 6c 73 6f 0a 2a   journal, also.*
10b79 2a 20 20 20 20 20 20 20 20 20 70 6c 61 79 62 61  *         playba
10b7a 63 6b 20 61 6c 6c 20 70 61 67 65 73 20 6f 66 20  ck all pages of 
10b7b 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
10b7c 6a 6f 75 72 6e 61 6c 20 62 65 67 69 6e 6e 69 6e  journal beginnin
10b7d 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 74 20  g.**         at 
10b7e 6f 66 66 73 65 74 20 70 50 61 67 65 72 2d 3e 73  offset pPager->s
10b7f 74 6d 74 4a 53 69 7a 65 2e 0a 2a 2f 0a 73 74 61  tmtJSize..*/.sta
10b80 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f 73 74  tic int pager_st
10b81 6d 74 5f 70 6c 61 79 62 61 63 6b 28 50 61 67 65  mt_playback(Page
10b82 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 36  r *pPager){.  i6
10b83 34 20 73 7a 4a 3b 20 20 20 20 20 20 20 20 20 20  4 szJ;          
10b84 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
10b85 66 20 74 68 65 20 66 75 6c 6c 20 6a 6f 75 72 6e  f the full journ
10b86 61 6c 20 2a 2f 0a 20 20 69 36 34 20 68 64 72 4f  al */.  i64 hdrO
10b87 66 66 3b 0a 20 20 69 6e 74 20 6e 52 65 63 3b 20  ff;.  int nRec; 
10b88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
10b89 2a 20 4e 75 6d 62 65 72 20 6f 66 20 52 65 63 6f  * Number of Reco
10b8a 72 64 73 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20  rds */.  int i; 
10b8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10b8c 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
10b8d 72 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  r */.  int rc;..
10b8e 20 20 73 7a 4a 20 3d 20 70 50 61 67 65 72 2d 3e    szJ = pPager->
10b8f 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 0a 20 20 2f  journalOff;..  /
10b90 2a 20 53 65 74 20 68 64 72 4f 66 66 20 74 6f 20  * Set hdrOff to 
10b91 62 65 20 74 68 65 20 6f 66 66 73 65 74 20 6a 75  be the offset ju
10b92 73 74 20 61 66 74 65 72 20 74 68 65 20 65 6e 64  st after the end
10b93 20 6f 66 20 74 68 65 20 6c 61 73 74 20 6a 6f 75   of the last jou
10b94 72 6e 61 6c 0a 20 20 2a 2a 20 70 61 67 65 20 77  rnal.  ** page w
10b95 72 69 74 74 65 6e 20 62 65 66 6f 72 65 20 74 68  ritten before th
10b96 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61 6c 2d  e first journal-
10b97 68 65 61 64 65 72 20 66 6f 72 20 74 68 69 73 20  header for this 
10b98 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74  statement.  ** t
10b99 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73 20 77  ransaction was w
10b9a 72 69 74 74 65 6e 2c 20 6f 72 20 74 68 65 20 65  ritten, or the e
10b9b 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  nd of the file i
10b9c 66 20 6e 6f 20 6a 6f 75 72 6e 61 6c 0a 20 20 2a  f no journal.  *
10b9d 2a 20 68 65 61 64 65 72 20 77 61 73 20 77 72 69  * header was wri
10b9e 74 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 68 64 72  tten..  */.  hdr
10b9f 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  Off = pPager->st
10ba0 6d 74 48 64 72 4f 66 66 3b 0a 20 20 61 73 73 65  mtHdrOff;.  asse
10ba1 72 74 28 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c  rt( pPager->full
10ba2 53 79 6e 63 20 7c 7c 20 21 68 64 72 4f 66 66 20  Sync || !hdrOff 
10ba3 29 3b 0a 20 20 69 66 28 20 21 68 64 72 4f 66 66  );.  if( !hdrOff
10ba4 20 29 7b 0a 20 20 20 20 68 64 72 4f 66 66 20 3d   ){.    hdrOff =
10ba5 20 73 7a 4a 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f   szJ;.  }.  .  /
10ba6 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65 20 64  * Truncate the d
10ba7 61 74 61 62 61 73 65 20 62 61 63 6b 20 74 6f 20  atabase back to 
10ba8 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 69 7a  its original siz
10ba9 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70  e..  */.  rc = p
10baa 61 67 65 72 5f 74 72 75 6e 63 61 74 65 28 70 50  ager_truncate(pP
10bab 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 74  ager, pPager->st
10bac 6d 74 53 69 7a 65 29 3b 0a 20 20 61 73 73 65 72  mtSize);.  asser
10bad 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65  t( pPager->state
10bae 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 29  >=PAGER_SHARED )
10baf 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  ;..  /* Figure o
10bb0 75 74 20 68 6f 77 20 6d 61 6e 79 20 72 65 63 6f  ut how many reco
10bb1 72 64 73 20 61 72 65 20 69 6e 20 74 68 65 20 73  rds are in the s
10bb2 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
10bb3 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28  ..  */.  assert(
10bb4 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55   pPager->stmtInU
10bb5 73 65 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  se && pPager->jo
10bb6 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 20 20 6e  urnalOpen );.  n
10bb7 52 65 63 20 3d 20 70 50 61 67 65 72 2d 3e 73 74  Rec = pPager->st
10bb8 6d 74 4e 52 65 63 3b 0a 20 20 0a 20 20 2f 2a 20  mtNRec;.  .  /* 
10bb9 43 6f 70 79 20 6f 72 69 67 69 6e 61 6c 20 70 61  Copy original pa
10bba 67 65 73 20 6f 75 74 20 6f 66 20 74 68 65 20 73  ges out of the s
10bbb 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c  tatement journal
10bbc 20 61 6e 64 20 62 61 63 6b 20 69 6e 74 6f 20 74   and back into t
10bbd 68 65 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65  he.  ** database
10bbe 20 66 69 6c 65 2e 20 20 4e 6f 74 65 20 74 68 61   file.  Note tha
10bbf 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  t the statement 
10bc0 6a 6f 75 72 6e 61 6c 20 6f 6d 69 74 73 20 63 68  journal omits ch
10bc1 65 63 6b 73 75 6d 73 20 66 72 6f 6d 0a 20 20 2a  ecksums from.  *
10bc2 2a 20 65 61 63 68 20 72 65 63 6f 72 64 20 73 69  * each record si
10bc3 6e 63 65 20 70 6f 77 65 72 2d 66 61 69 6c 75 72  nce power-failur
10bc4 65 20 72 65 63 6f 76 65 72 79 20 69 73 20 6e 6f  e recovery is no
10bc5 74 20 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 73  t important to s
10bc6 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 6a 6f  tatement.  ** jo
10bc7 75 72 6e 61 6c 73 2e 0a 20 20 2a 2f 0a 20 20 66  urnals..  */.  f
10bc8 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 63 3b 20  or(i=0; i<nRec; 
10bc9 69 2b 2b 29 7b 0a 20 20 20 20 69 36 34 20 6f 66  i++){.    i64 of
10bca 66 73 65 74 20 3d 20 69 2a 28 34 2b 70 50 61 67  fset = i*(4+pPag
10bcb 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
10bcc 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c     rc = pager_pl
10bcd 61 79 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28  ayback_one_page(
10bce 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
10bcf 73 74 66 64 2c 20 6f 66 66 73 65 74 2c 20 30 29  stfd, offset, 0)
10bd0 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
10bd1 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
10bd2 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
10bd3 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
10bd4 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b  d_stmt_playback;
10bd5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 72  .  }..  /* Now r
10bd6 6f 6c 6c 20 73 6f 6d 65 20 70 61 67 65 73 20 62  oll some pages b
10bd7 61 63 6b 20 66 72 6f 6d 20 74 68 65 20 74 72 61  ack from the tra
10bd8 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c  nsaction journal
10bd9 2e 20 50 61 67 65 72 2e 73 74 6d 74 4a 53 69 7a  . Pager.stmtJSiz
10bda 65 0a 20 20 2a 2a 20 77 61 73 20 74 68 65 20 73  e.  ** was the s
10bdb 69 7a 65 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e  ize of the journ
10bdc 61 6c 20 66 69 6c 65 20 77 68 65 6e 20 74 68 69  al file when thi
10bdd 73 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20  s statement was 
10bde 73 74 61 72 74 65 64 2c 20 73 6f 0a 20 20 2a 2a  started, so.  **
10bdf 20 65 76 65 72 79 74 68 69 6e 67 20 61 66 74 65   everything afte
10be0 72 20 74 68 61 74 20 6e 65 65 64 73 20 74 6f 20  r that needs to 
10be1 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2c 20  be rolled back, 
10be2 65 69 74 68 65 72 20 69 6e 74 6f 20 74 68 65 0a  either into the.
10be3 20 20 2a 2a 20 64 61 74 61 62 61 73 65 2c 20 74    ** database, t
10be4 68 65 20 6d 65 6d 6f 72 79 20 63 61 63 68 65 2c  he memory cache,
10be5 20 6f 72 20 62 6f 74 68 2e 0a 20 20 2a 2a 0a 20   or both..  **. 
10be6 20 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74   ** If it is not
10be7 20 7a 65 72 6f 2c 20 74 68 65 6e 20 50 61 67 65   zero, then Page
10be8 72 2e 73 74 6d 74 48 64 72 4f 66 66 20 69 73 20  r.stmtHdrOff is 
10be9 74 68 65 20 6f 66 66 73 65 74 20 74 6f 20 74 68  the offset to th
10bea 65 20 73 74 61 72 74 0a 20 20 2a 2a 20 6f 66 20  e start.  ** of 
10beb 74 68 65 20 66 69 72 73 74 20 6a 6f 75 72 6e 61  the first journa
10bec 6c 20 68 65 61 64 65 72 20 77 72 69 74 74 65 6e  l header written
10bed 20 64 75 72 69 6e 67 20 74 68 69 73 20 73 74 61   during this sta
10bee 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
10bef 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 70 50 61 67 65  on..  */.  pPage
10bf0 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
10bf1 70 50 61 67 65 72 2d 3e 73 74 6d 74 4a 53 69 7a  pPager->stmtJSiz
10bf2 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 63 6b 73  e;.  pPager->cks
10bf3 75 6d 49 6e 69 74 20 3d 20 70 50 61 67 65 72 2d  umInit = pPager-
10bf4 3e 73 74 6d 74 43 6b 73 75 6d 3b 0a 20 20 77 68  >stmtCksum;.  wh
10bf5 69 6c 65 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75  ile( pPager->jou
10bf6 72 6e 61 6c 4f 66 66 20 3c 20 68 64 72 4f 66 66  rnalOff < hdrOff
10bf7 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70 61 67   ){.    rc = pag
10bf8 65 72 5f 70 6c 61 79 62 61 63 6b 5f 6f 6e 65 5f  er_playback_one_
10bf9 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 61  page(pPager, pPa
10bfa 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67 65 72  ger->jfd, pPager
10bfb 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 31 29  ->journalOff, 1)
10bfc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63  ;.    assert( rc
10bfd 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
10bfe 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
10bff 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
10c00 64 5f 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b  d_stmt_playback;
10c01 0a 20 20 7d 0a 0a 20 20 77 68 69 6c 65 28 20 70  .  }..  while( p
10c02 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66  Pager->journalOf
10c03 66 20 3c 20 73 7a 4a 20 29 7b 0a 20 20 20 20 75  f < szJ ){.    u
10c04 33 32 20 6e 4a 52 65 63 3b 20 20 20 20 20 20 20  32 nJRec;       
10c05 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 4a    /* Number of J
10c06 6f 75 72 6e 61 6c 20 52 65 63 6f 72 64 73 20 2a  ournal Records *
10c07 2f 0a 20 20 20 20 75 33 32 20 64 75 6d 6d 79 3b  /.    u32 dummy;
10c08 0a 20 20 20 20 72 63 20 3d 20 72 65 61 64 4a 6f  .    rc = readJo
10c09 75 72 6e 61 6c 48 64 72 28 70 50 61 67 65 72 2c  urnalHdr(pPager,
10c0a 20 73 7a 4a 2c 20 26 6e 4a 52 65 63 2c 20 26 64   szJ, &nJRec, &d
10c0b 75 6d 6d 79 29 3b 0a 20 20 20 20 69 66 28 20 72  ummy);.    if( r
10c0c 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
10c0d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
10c0e 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 3b  !=SQLITE_DONE );
10c0f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f  .      goto end_
10c10 73 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a 20  stmt_playback;. 
10c11 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 4a 52     }.    if( nJR
10c12 65 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e  ec==0 ){.      n
10c13 4a 52 65 63 20 3d 20 28 73 7a 4a 20 2d 20 70 50  JRec = (szJ - pP
10c14 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
10c15 29 20 2f 20 28 70 50 61 67 65 72 2d 3e 70 61 67  ) / (pPager->pag
10c16 65 53 69 7a 65 2b 38 29 3b 0a 20 20 20 20 7d 0a  eSize+8);.    }.
10c17 20 20 20 20 66 6f 72 28 69 3d 6e 4a 52 65 63 2d      for(i=nJRec-
10c18 31 3b 20 69 3e 3d 30 20 26 26 20 70 50 61 67 65  1; i>=0 && pPage
10c19 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3c 20  r->journalOff < 
10c1a 73 7a 4a 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 20  szJ; i--){.     
10c1b 20 72 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79   rc = pager_play
10c1c 62 61 63 6b 5f 6f 6e 65 5f 70 61 67 65 28 70 50  back_one_page(pP
10c1d 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 6a 66  ager, pPager->jf
10c1e 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  d, pPager->journ
10c1f 61 6c 4f 66 66 2c 20 31 29 3b 0a 20 20 20 20 20  alOff, 1);.     
10c20 20 61 73 73 65 72 74 28 20 72 63 21 3d 53 51 4c   assert( rc!=SQL
10c21 49 54 45 5f 44 4f 4e 45 20 29 3b 0a 20 20 20 20  ITE_DONE );.    
10c22 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
10c23 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73  _OK ) goto end_s
10c24 74 6d 74 5f 70 6c 61 79 62 61 63 6b 3b 0a 20 20  tmt_playback;.  
10c25 20 20 7d 0a 20 20 7d 0a 0a 20 20 70 50 61 67 65    }.  }..  pPage
10c26 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
10c27 73 7a 4a 3b 0a 20 20 0a 65 6e 64 5f 73 74 6d 74  szJ;.  .end_stmt
10c28 5f 70 6c 61 79 62 61 63 6b 3a 0a 20 20 69 66 28  _playback:.  if(
10c29 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 29 20   rc==SQLITE_OK) 
10c2a 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f  {.    pPager->jo
10c2b 75 72 6e 61 6c 4f 66 66 20 3d 20 73 7a 4a 3b 0a  urnalOff = szJ;.
10c2c 20 20 20 20 2f 2a 20 70 61 67 65 72 5f 72 65 6c      /* pager_rel
10c2d 6f 61 64 5f 63 61 63 68 65 28 70 50 61 67 65 72  oad_cache(pPager
10c2e 29 3b 20 2a 2f 0a 20 20 7d 0a 20 20 72 65 74 75  ); */.  }.  retu
10c2f 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
10c30 43 68 61 6e 67 65 20 74 68 65 20 6d 61 78 69 6d  Change the maxim
10c31 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 2d  um number of in-
10c32 6d 65 6d 6f 72 79 20 70 61 67 65 73 20 74 68 61  memory pages tha
10c33 74 20 61 72 65 20 61 6c 6c 6f 77 65 64 2e 0a 2a  t are allowed..*
10c34 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10c35 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67   void sqlite3Pag
10c36 65 72 53 65 74 43 61 63 68 65 73 69 7a 65 28 50  erSetCachesize(P
10c37 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e  ager *pPager, in
10c38 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 73 71 6c  t mxPage){.  sql
10c39 69 74 65 33 50 63 61 63 68 65 53 65 74 43 61 63  ite3PcacheSetCac
10c3a 68 65 73 69 7a 65 28 70 50 61 67 65 72 2d 3e 70  hesize(pPager->p
10c3b 50 43 61 63 68 65 2c 20 6d 78 50 61 67 65 29 3b  PCache, mxPage);
10c3c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 6a 75 73 74  .}../*.** Adjust
10c3d 20 74 68 65 20 72 6f 62 75 73 74 6e 65 73 73 20   the robustness 
10c3e 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
10c3f 74 6f 20 64 61 6d 61 67 65 20 64 75 65 20 74 6f  to damage due to
10c40 20 4f 53 20 63 72 61 73 68 65 73 0a 2a 2a 20 6f   OS crashes.** o
10c41 72 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 73  r power failures
10c42 20 62 79 20 63 68 61 6e 67 69 6e 67 20 74 68 65   by changing the
10c43 20 6e 75 6d 62 65 72 20 6f 66 20 73 79 6e 63 73   number of syncs
10c44 28 29 73 20 77 68 65 6e 20 77 72 69 74 69 6e 67  ()s when writing
10c45 0a 2a 2a 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b  .** the rollback
10c46 20 6a 6f 75 72 6e 61 6c 2e 20 20 54 68 65 72 65   journal.  There
10c47 20 61 72 65 20 74 68 72 65 65 20 6c 65 76 65 6c   are three level
10c48 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4f 46 46 20  s:.**.**    OFF 
10c49 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 53        sqlite3OsS
10c4a 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20 63  ync() is never c
10c4b 61 6c 6c 65 64 2e 20 20 54 68 69 73 20 69 73 20  alled.  This is 
10c4c 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20  the default.**  
10c4d 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72 20              for 
10c4e 74 65 6d 70 6f 72 61 72 79 20 61 6e 64 20 74 72  temporary and tr
10c4f 61 6e 73 69 65 6e 74 20 66 69 6c 65 73 2e 0a 2a  ansient files..*
10c50 2a 0a 2a 2a 20 20 20 20 4e 4f 52 4d 41 4c 20 20  *.**    NORMAL  
10c51 20 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73    The journal is
10c52 20 73 79 6e 63 65 64 20 6f 6e 63 65 20 62 65 66   synced once bef
10c53 6f 72 65 20 77 72 69 74 65 73 20 62 65 67 69 6e  ore writes begin
10c54 20 6f 6e 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   on the.**      
10c55 20 20 20 20 20 20 20 20 64 61 74 61 62 61 73 65          database
10c56 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72 6d 61  .  This is norma
10c57 6c 6c 79 20 61 64 65 71 75 61 74 65 20 70 72 6f  lly adequate pro
10c58 74 65 63 74 69 6f 6e 2c 20 62 75 74 0a 2a 2a 20  tection, but.** 
10c59 20 20 20 20 20 20 20 20 20 20 20 20 20 69 74 20               it 
10c5a 69 73 20 74 68 65 6f 72 65 74 69 63 61 6c 6c 79  is theoretically
10c5b 20 70 6f 73 73 69 62 6c 65 2c 20 74 68 6f 75 67   possible, thoug
10c5c 68 20 76 65 72 79 20 75 6e 6c 69 6b 65 6c 79 2c  h very unlikely,
10c5d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
10c5e 20 74 68 61 74 20 61 6e 20 69 6e 6f 70 65 72 74   that an inopert
10c5f 75 6e 65 20 70 6f 77 65 72 20 66 61 69 6c 75 72  une power failur
10c60 65 20 63 6f 75 6c 64 20 6c 65 61 76 65 20 74 68  e could leave th
10c61 65 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 20 20 20  e journal.**    
10c62 20 20 20 20 20 20 20 20 20 20 69 6e 20 61 20 73            in a s
10c63 74 61 74 65 20 77 68 69 63 68 20 77 6f 75 6c 64  tate which would
10c64 20 63 61 75 73 65 20 64 61 6d 61 67 65 20 74 6f   cause damage to
10c65 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a   the database.**
10c66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 77 68                wh
10c67 65 6e 20 69 74 20 69 73 20 72 6f 6c 6c 65 64 20  en it is rolled 
10c68 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 46  back..**.**    F
10c69 55 4c 4c 20 20 20 20 20 20 54 68 65 20 6a 6f 75  ULL      The jou
10c6a 72 6e 61 6c 20 69 73 20 73 79 6e 63 65 64 20 74  rnal is synced t
10c6b 77 69 63 65 20 62 65 66 6f 72 65 20 77 72 69 74  wice before writ
10c6c 65 73 20 62 65 67 69 6e 20 6f 6e 20 74 68 65 0a  es begin on the.
10c6d 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
10c6e 64 61 74 61 62 61 73 65 20 28 77 69 74 68 20 73  database (with s
10c6f 6f 6d 65 20 61 64 64 69 74 69 6f 6e 61 6c 20 69  ome additional i
10c70 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20 74 68 65  nformation - the
10c71 20 6e 52 65 63 20 66 69 65 6c 64 0a 2a 2a 20 20   nRec field.**  
10c72 20 20 20 20 20 20 20 20 20 20 20 20 6f 66 20 74              of t
10c73 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65 61 64 65  he journal heade
10c74 72 20 2d 20 62 65 69 6e 67 20 77 72 69 74 74 65  r - being writte
10c75 6e 20 69 6e 20 62 65 74 77 65 65 6e 20 74 68 65  n in between the
10c76 20 74 77 6f 0a 2a 2a 20 20 20 20 20 20 20 20 20   two.**         
10c77 20 20 20 20 20 73 79 6e 63 73 29 2e 20 20 49 66       syncs).  If
10c78 20 77 65 20 61 73 73 75 6d 65 20 74 68 61 74 20   we assume that 
10c79 77 72 69 74 69 6e 67 20 61 0a 2a 2a 20 20 20 20  writing a.**    
10c7a 20 20 20 20 20 20 20 20 20 20 73 69 6e 67 6c 65            single
10c7b 20 64 69 73 6b 20 73 65 63 74 6f 72 20 69 73 20   disk sector is 
10c7c 61 74 6f 6d 69 63 2c 20 74 68 65 6e 20 74 68 69  atomic, then thi
10c7d 73 20 6d 6f 64 65 20 70 72 6f 76 69 64 65 73 0a  s mode provides.
10c7e 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
10c7f 61 73 73 75 72 61 6e 63 65 20 74 68 61 74 20 74  assurance that t
10c80 68 65 20 6a 6f 75 72 6e 61 6c 20 77 69 6c 6c 20  he journal will 
10c81 6e 6f 74 20 62 65 20 63 6f 72 72 75 70 74 65 64  not be corrupted
10c82 20 74 6f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20   to the.**      
10c83 20 20 20 20 20 20 20 20 70 6f 69 6e 74 20 6f 66          point of
10c84 20 63 61 75 73 69 6e 67 20 64 61 6d 61 67 65 20   causing damage 
10c85 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20  to the database 
10c86 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 2e  during rollback.
10c87 0a 2a 2a 0a 2a 2a 20 4e 75 6d 65 72 69 63 20 76  .**.** Numeric v
10c88 61 6c 75 65 73 20 61 73 73 6f 63 69 61 74 65 64  alues associated
10c89 20 77 69 74 68 20 74 68 65 73 65 20 73 74 61 74   with these stat
10c8a 65 73 20 61 72 65 20 4f 46 46 3d 3d 31 2c 20 4e  es are OFF==1, N
10c8b 4f 52 4d 41 4c 3d 32 2c 0a 2a 2a 20 61 6e 64 20  ORMAL=2,.** and 
10c8c 46 55 4c 4c 3d 33 2e 0a 2a 2f 0a 23 69 66 6e 64  FULL=3..*/.#ifnd
10c8d 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50  ef SQLITE_OMIT_P
10c8e 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 53 51 4c  AGER_PRAGMAS.SQL
10c8f 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
10c90 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
10c91 53 61 66 65 74 79 4c 65 76 65 6c 28 50 61 67 65  SafetyLevel(Page
10c92 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 6c  r *pPager, int l
10c93 65 76 65 6c 2c 20 69 6e 74 20 62 46 75 6c 6c 46  evel, int bFullF
10c94 73 79 6e 63 29 7b 0a 20 20 70 50 61 67 65 72 2d  sync){.  pPager-
10c95 3e 6e 6f 53 79 6e 63 20 3d 20 20 6c 65 76 65 6c  >noSync =  level
10c96 3d 3d 31 20 7c 7c 20 70 50 61 67 65 72 2d 3e 74  ==1 || pPager->t
10c97 65 6d 70 46 69 6c 65 3b 0a 20 20 70 50 61 67 65  empFile;.  pPage
10c98 72 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 6c 65  r->fullSync = le
10c99 76 65 6c 3d 3d 33 20 26 26 20 21 70 50 61 67 65  vel==3 && !pPage
10c9a 72 2d 3e 74 65 6d 70 46 69 6c 65 3b 0a 20 20 70  r->tempFile;.  p
10c9b 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67  Pager->sync_flag
10c9c 73 20 3d 20 28 62 46 75 6c 6c 46 73 79 6e 63 3f  s = (bFullFsync?
10c9d 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
10c9e 3a 53 51 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52  :SQLITE_SYNC_NOR
10c9f 4d 41 4c 29 3b 0a 20 20 69 66 28 20 70 50 61 67  MAL);.  if( pPag
10ca0 65 72 2d 3e 6e 6f 53 79 6e 63 20 29 20 70 50 61  er->noSync ) pPa
10ca1 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20  ger->needSync = 
10ca2 30 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  0;.}.#endif../*.
10ca3 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
10ca4 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
10ca5 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   is incremented 
10ca6 77 68 65 6e 65 76 65 72 20 74 68 65 20 6c 69 62  whenever the lib
10ca7 72 61 72 79 0a 2a 2a 20 61 74 74 65 6d 70 74 73  rary.** attempts
10ca8 20 74 6f 20 6f 70 65 6e 20 61 20 74 65 6d 70 6f   to open a tempo
10ca9 72 61 72 79 20 66 69 6c 65 2e 20 20 54 68 69 73  rary file.  This
10caa 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20   information is 
10cab 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74 65 73 74  used for.** test
10cac 69 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73  ing and analysis
10cad 20 6f 6e 6c 79 2e 20 20 0a 2a 2f 0a 23 69 66 64   only.  .*/.#ifd
10cae 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53  ef SQLITE_TEST.S
10caf 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
10cb0 6c 69 74 65 33 5f 6f 70 65 6e 74 65 6d 70 5f 63  lite3_opentemp_c
10cb1 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
10cb2 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 74  ../*.** Open a t
10cb3 65 6d 70 6f 72 61 72 79 20 66 69 6c 65 2e 20 0a  emporary file. .
10cb4 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20  **.** Write the 
10cb5 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72 20  file descriptor 
10cb6 69 6e 74 6f 20 2a 66 64 2e 20 20 52 65 74 75 72  into *fd.  Retur
10cb7 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73  n SQLITE_OK on s
10cb8 75 63 63 65 73 73 20 6f 72 20 73 6f 6d 65 0a 2a  uccess or some.*
10cb9 2a 20 6f 74 68 65 72 20 65 72 72 6f 72 20 63 6f  * other error co
10cba 64 65 20 69 66 20 77 65 20 66 61 69 6c 2e 20 54  de if we fail. T
10cbb 68 65 20 4f 53 20 77 69 6c 6c 20 61 75 74 6f 6d  he OS will autom
10cbc 61 74 69 63 61 6c 6c 79 20 64 65 6c 65 74 65 20  atically delete 
10cbd 74 68 65 20 74 65 6d 70 6f 72 61 72 79 0a 2a 2a  the temporary.**
10cbe 20 66 69 6c 65 20 77 68 65 6e 20 69 74 20 69 73   file when it is
10cbf 20 63 6c 6f 73 65 64 2e 0a 2a 2f 0a 73 74 61 74   closed..*/.stat
10cc0 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61  ic int sqlite3Pa
10cc1 67 65 72 4f 70 65 6e 74 65 6d 70 28 0a 20 20 50  gerOpentemp(.  P
10cc2 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 20 20  ager *pPager,   
10cc3 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65       /* The page
10cc4 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71  r object */.  sq
10cc5 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
10cc6 65 2c 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65  e,  /* Write the
10cc7 20 66 69 6c 65 20 64 65 73 63 72 69 70 74 6f 72   file descriptor
10cc8 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 76   here */.  int v
10cc9 66 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20  fsFlags         
10cca 20 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64   /* Flags passed
10ccb 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
10ccc 56 46 53 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  VFS */.){.  int 
10ccd 72 63 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49  rc;..#ifdef SQLI
10cce 54 45 5f 54 45 53 54 0a 20 20 73 71 6c 69 74 65  TE_TEST.  sqlite
10ccf 33 5f 6f 70 65 6e 74 65 6d 70 5f 63 6f 75 6e 74  3_opentemp_count
10cd0 2b 2b 3b 20 20 2f 2a 20 55 73 65 64 20 66 6f 72  ++;  /* Used for
10cd1 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e 61   testing and ana
10cd2 6c 79 73 69 73 20 6f 6e 6c 79 20 2a 2f 0a 23 65  lysis only */.#e
10cd3 6e 64 69 66 0a 0a 20 20 76 66 73 46 6c 61 67 73  ndif..  vfsFlags
10cd4 20 7c 3d 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e   |=  SQLITE_OPEN
10cd5 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c  _READWRITE | SQL
10cd6 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
10cd7 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 53 51  |.            SQ
10cd8 4c 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53  LITE_OPEN_EXCLUS
10cd9 49 56 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  IVE | SQLITE_OPE
10cda 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53 45 3b  N_DELETEONCLOSE;
10cdb 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
10cdc 73 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 70 56  sOpen(pPager->pV
10cdd 66 73 2c 20 30 2c 20 70 46 69 6c 65 2c 20 76 66  fs, 0, pFile, vf
10cde 73 46 6c 61 67 73 2c 20 30 29 3b 0a 20 20 61 73  sFlags, 0);.  as
10cdf 73 65 72 74 28 20 72 63 21 3d 53 51 4c 49 54 45  sert( rc!=SQLITE
10ce0 5f 4f 4b 20 7c 7c 20 70 46 69 6c 65 2d 3e 70 4d  _OK || pFile->pM
10ce1 65 74 68 6f 64 73 20 29 3b 0a 20 20 72 65 74 75  ethods );.  retu
10ce2 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
10ce3 20 69 6e 74 20 70 61 67 65 72 53 74 72 65 73 73   int pagerStress
10ce4 28 76 6f 69 64 20 2a 2c 50 67 48 64 72 20 2a 29  (void *,PgHdr *)
10ce5 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  ;../*.** Create 
10ce6 61 20 6e 65 77 20 70 61 67 65 20 63 61 63 68 65  a new page cache
10ce7 20 61 6e 64 20 70 75 74 20 61 20 70 6f 69 6e 74   and put a point
10ce8 65 72 20 74 6f 20 74 68 65 20 70 61 67 65 20 63  er to the page c
10ce9 61 63 68 65 20 69 6e 20 2a 70 70 50 61 67 65 72  ache in *ppPager
10cea 2e 0a 2a 2a 20 54 68 65 20 66 69 6c 65 20 74 6f  ..** The file to
10ceb 20 62 65 20 63 61 63 68 65 64 20 6e 65 65 64 20   be cached need 
10cec 6e 6f 74 20 65 78 69 73 74 2e 20 20 54 68 65 20  not exist.  The 
10ced 66 69 6c 65 20 69 73 20 6e 6f 74 20 6c 6f 63 6b  file is not lock
10cee 65 64 20 75 6e 74 69 6c 0a 2a 2a 20 74 68 65 20  ed until.** the 
10cef 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 73 71  first call to sq
10cf0 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 29 20  lite3PagerGet() 
10cf1 61 6e 64 20 69 73 20 6f 6e 6c 79 20 68 65 6c 64  and is only held
10cf2 20 6f 70 65 6e 20 75 6e 74 69 6c 20 74 68 65 0a   open until the.
10cf3 2a 2a 20 6c 61 73 74 20 70 61 67 65 20 69 73 20  ** last page is 
10cf4 72 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 73  released using s
10cf5 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
10cf6 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69  ()..**.** If zFi
10cf7 6c 65 6e 61 6d 65 20 69 73 20 4e 55 4c 4c 20 74  lename is NULL t
10cf8 68 65 6e 20 61 20 72 61 6e 64 6f 6d 6c 79 2d 6e  hen a randomly-n
10cf9 61 6d 65 64 20 74 65 6d 70 6f 72 61 72 79 20 66  amed temporary f
10cfa 69 6c 65 20 69 73 20 63 72 65 61 74 65 64 0a 2a  ile is created.*
10cfb 2a 20 61 6e 64 20 75 73 65 64 20 61 73 20 74 68  * and used as th
10cfc 65 20 66 69 6c 65 20 74 6f 20 62 65 20 63 61 63  e file to be cac
10cfd 68 65 64 2e 20 20 54 68 65 20 66 69 6c 65 20 77  hed.  The file w
10cfe 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64 0a 2a  ill be deleted.*
10cff 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20  * automatically 
10d00 77 68 65 6e 20 69 74 20 69 73 20 63 6c 6f 73 65  when it is close
10d01 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 46 69 6c  d..**.** If zFil
10d02 65 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72  ename is ":memor
10d03 79 3a 22 20 74 68 65 6e 20 61 6c 6c 20 69 6e 66  y:" then all inf
10d04 6f 72 6d 61 74 69 6f 6e 20 69 73 20 68 65 6c 64  ormation is held
10d05 20 69 6e 20 63 61 63 68 65 2e 0a 2a 2a 20 49 74   in cache..** It
10d06 20 69 73 20 6e 65 76 65 72 20 77 72 69 74 74 65   is never writte
10d07 6e 20 74 6f 20 64 69 73 6b 2e 20 20 54 68 69 73  n to disk.  This
10d08 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
10d09 69 6d 70 6c 65 6d 65 6e 74 20 61 6e 0a 2a 2a 20  implement an.** 
10d0a 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74 61 62 61  in-memory databa
10d0b 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  se..*/.SQLITE_PR
10d0c 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
10d0d 33 50 61 67 65 72 4f 70 65 6e 28 0a 20 20 73 71  3PagerOpen(.  sq
10d0e 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 2c  lite3_vfs *pVfs,
10d0f 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69         /* The vi
10d10 72 74 75 61 6c 20 66 69 6c 65 20 73 79 73 74 65  rtual file syste
10d11 6d 20 74 6f 20 75 73 65 20 2a 2f 0a 20 20 50 61  m to use */.  Pa
10d12 67 65 72 20 2a 2a 70 70 50 61 67 65 72 2c 20 20  ger **ppPager,  
10d13 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75 72 6e         /* Return
10d14 20 74 68 65 20 50 61 67 65 72 20 73 74 72 75 63   the Pager struc
10d15 74 75 72 65 20 68 65 72 65 20 2a 2f 0a 20 20 63  ture here */.  c
10d16 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
10d17 6e 61 6d 65 2c 20 20 20 2f 2a 20 4e 61 6d 65 20  name,   /* Name 
10d18 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  of the database 
10d19 66 69 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a  file to open */.
10d1a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c 20 20 20    int nExtra,   
10d1b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78             /* Ex
10d1c 74 72 61 20 62 79 74 65 73 20 61 70 70 65 6e 64  tra bytes append
10d1d 20 74 6f 20 65 61 63 68 20 69 6e 2d 6d 65 6d 6f   to each in-memo
10d1e 72 79 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74  ry page */.  int
10d1f 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
10d20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 63        /* flags c
10d21 6f 6e 74 72 6f 6c 6c 69 6e 67 20 74 68 69 73 20  ontrolling this 
10d22 66 69 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 76 66  file */.  int vf
10d23 73 46 6c 61 67 73 20 20 20 20 20 20 20 20 20 20  sFlags          
10d24 20 20 20 2f 2a 20 66 6c 61 67 73 20 70 61 73 73     /* flags pass
10d25 65 64 20 74 68 72 6f 75 67 68 20 74 6f 20 73 71  ed through to sq
10d26 6c 69 74 65 33 5f 76 66 73 2e 78 4f 70 65 6e 28  lite3_vfs.xOpen(
10d27 29 20 2a 2f 0a 29 7b 0a 20 20 75 38 20 2a 70 50  ) */.){.  u8 *pP
10d28 74 72 3b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  tr;.  Pager *pPa
10d29 67 65 72 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72  ger = 0;.  int r
10d2a 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
10d2b 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 74 65   int i;.  int te
10d2c 6d 70 46 69 6c 65 20 3d 20 30 3b 0a 20 20 69 6e  mpFile = 0;.  in
10d2d 74 20 6d 65 6d 44 62 20 3d 20 30 3b 0a 20 20 69  t memDb = 0;.  i
10d2e 6e 74 20 72 65 61 64 4f 6e 6c 79 20 3d 20 30 3b  nt readOnly = 0;
10d2f 0a 20 20 69 6e 74 20 75 73 65 4a 6f 75 72 6e 61  .  int useJourna
10d30 6c 20 3d 20 28 66 6c 61 67 73 20 26 20 50 41 47  l = (flags & PAG
10d31 45 52 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 29  ER_OMIT_JOURNAL)
10d32 3d 3d 30 3b 0a 20 20 69 6e 74 20 6e 6f 52 65 61  ==0;.  int noRea
10d33 64 6c 6f 63 6b 20 3d 20 28 66 6c 61 67 73 20 26  dlock = (flags &
10d34 20 50 41 47 45 52 5f 4e 4f 5f 52 45 41 44 4c 4f   PAGER_NO_READLO
10d35 43 4b 29 21 3d 30 3b 0a 20 20 69 6e 74 20 6a 6f  CK)!=0;.  int jo
10d36 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 3b 0a 20  urnalFileSize;. 
10d37 20 69 6e 74 20 70 63 61 63 68 65 53 69 7a 65 20   int pcacheSize 
10d38 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 53  = sqlite3PcacheS
10d39 69 7a 65 28 29 3b 0a 20 20 69 6e 74 20 73 7a 50  ize();.  int szP
10d3a 61 67 65 44 66 6c 74 20 3d 20 53 51 4c 49 54 45  ageDflt = SQLITE
10d3b 5f 44 45 46 41 55 4c 54 5f 50 41 47 45 5f 53 49  _DEFAULT_PAGE_SI
10d3c 5a 45 3b 0a 20 20 63 68 61 72 20 2a 7a 50 61 74  ZE;.  char *zPat
10d3d 68 6e 61 6d 65 20 3d 20 30 3b 0a 20 20 69 6e 74  hname = 0;.  int
10d3e 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20 30 3b 0a   nPathname = 0;.
10d3f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 4a 6f  .  if( sqlite3Jo
10d40 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 3e  urnalSize(pVfs)>
10d41 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
10d42 6c 53 69 7a 65 28 29 20 29 7b 0a 20 20 20 20 6a  lSize() ){.    j
10d43 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 3d  ournalFileSize =
10d44 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 53   sqlite3JournalS
10d45 69 7a 65 28 70 56 66 73 29 3b 0a 20 20 7d 65 6c  ize(pVfs);.  }el
10d46 73 65 7b 0a 20 20 20 20 6a 6f 75 72 6e 61 6c 46  se{.    journalF
10d47 69 6c 65 53 69 7a 65 20 3d 20 73 71 6c 69 74 65  ileSize = sqlite
10d48 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53 69 7a 65 28  3MemJournalSize(
10d49 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65  );.  }..  /* The
10d4a 20 64 65 66 61 75 6c 74 20 72 65 74 75 72 6e 20   default return 
10d4b 69 73 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65  is a NULL pointe
10d4c 72 20 2a 2f 0a 20 20 2a 70 70 50 61 67 65 72 20  r */.  *ppPager 
10d4d 3d 20 30 3b 0a 0a 20 20 2f 2a 20 43 6f 6d 70 75  = 0;..  /* Compu
10d4e 74 65 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  te and store the
10d4f 20 66 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 69   full pathname i
10d50 6e 20 61 6e 20 61 6c 6c 6f 63 61 74 65 64 20 62  n an allocated b
10d51 75 66 66 65 72 20 70 6f 69 6e 74 65 64 0a 20 20  uffer pointed.  
10d52 2a 2a 20 74 6f 20 62 79 20 7a 50 61 74 68 6e 61  ** to by zPathna
10d53 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 50 61 74 68  me, length nPath
10d54 6e 61 6d 65 2e 20 4f 72 2c 20 69 66 20 74 68 69  name. Or, if thi
10d55 73 20 69 73 20 61 20 74 65 6d 70 6f 72 61 72 79  s is a temporary
10d56 20 66 69 6c 65 2c 0a 20 20 2a 2a 20 6c 65 61 76   file,.  ** leav
10d57 65 20 62 6f 74 68 20 6e 50 61 74 68 6e 61 6d 65  e both nPathname
10d58 20 61 6e 64 20 7a 50 61 74 68 6e 61 6d 65 20 73   and zPathname s
10d59 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a 20 20  et to 0..  */.  
10d5a 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26  if( zFilename &&
10d5b 20 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 20 29 7b   zFilename[0] ){
10d5c 0a 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d  .    nPathname =
10d5d 20 70 56 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d   pVfs->mxPathnam
10d5e 65 2b 31 3b 0a 20 20 20 20 7a 50 61 74 68 6e 61  e+1;.    zPathna
10d5f 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c  me = sqlite3Mall
10d60 6f 63 28 6e 50 61 74 68 6e 61 6d 65 2a 32 29 3b  oc(nPathname*2);
10d61 0a 20 20 20 20 69 66 28 20 7a 50 61 74 68 6e 61  .    if( zPathna
10d62 6d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  me==0 ){.      r
10d63 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
10d64 45 4d 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  EM;.    }.#ifnde
10d65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4d 45  f SQLITE_OMIT_ME
10d66 4d 4f 52 59 44 42 0a 20 20 20 20 69 66 28 20 73  MORYDB.    if( s
10d67 74 72 63 6d 70 28 7a 46 69 6c 65 6e 61 6d 65 2c  trcmp(zFilename,
10d68 22 3a 6d 65 6d 6f 72 79 3a 22 29 3d 3d 30 20 29  ":memory:")==0 )
10d69 7b 0a 20 20 20 20 20 20 6d 65 6d 44 62 20 3d 20  {.      memDb = 
10d6a 31 3b 0a 20 20 20 20 20 20 7a 50 61 74 68 6e 61  1;.      zPathna
10d6b 6d 65 5b 30 5d 20 3d 20 30 3b 0a 20 20 20 20 7d  me[0] = 0;.    }
10d6c 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20  else.#endif.    
10d6d 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  {.      rc = sql
10d6e 69 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61  ite3OsFullPathna
10d6f 6d 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61  me(pVfs, zFilena
10d70 6d 65 2c 20 6e 50 61 74 68 6e 61 6d 65 2c 20 7a  me, nPathname, z
10d71 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d  Pathname);.    }
10d72 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
10d73 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
10d74 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 50 61  sqlite3_free(zPa
10d75 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 20 20 72  thname);.      r
10d76 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
10d77 20 20 20 20 6e 50 61 74 68 6e 61 6d 65 20 3d 20      nPathname = 
10d78 73 74 72 6c 65 6e 28 7a 50 61 74 68 6e 61 6d 65  strlen(zPathname
10d79 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c  );.  }..  /* All
10d7a 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 6f 72  ocate memory for
10d7b 20 74 68 65 20 70 61 67 65 72 20 73 74 72 75 63   the pager struc
10d7c 74 75 72 65 20 2a 2f 0a 20 20 70 50 61 67 65 72  ture */.  pPager
10d7d 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
10d7e 5a 65 72 6f 28 0a 20 20 20 20 73 69 7a 65 6f 66  Zero(.    sizeof
10d7f 28 2a 70 50 61 67 65 72 29 20 2b 20 20 20 20 20  (*pPager) +     
10d80 20 20 20 20 20 20 2f 2a 20 50 61 67 65 72 20 73        /* Pager s
10d81 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20  tructure */.    
10d82 70 63 61 63 68 65 53 69 7a 65 20 20 20 20 20 20  pcacheSize      
10d83 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50  +           /* P
10d84 43 61 63 68 65 20 6f 62 6a 65 63 74 20 2a 2f 0a  Cache object */.
10d85 20 20 20 20 6a 6f 75 72 6e 61 6c 46 69 6c 65 53      journalFileS
10d86 69 7a 65 20 2b 20 20 20 20 20 20 20 20 20 20 20  ize +           
10d87 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
10d88 69 6c 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ile structure */
10d89 20 0a 20 20 20 20 70 56 66 73 2d 3e 73 7a 4f 73   .    pVfs->szOs
10d8a 46 69 6c 65 20 20 2b 20 20 20 20 20 20 20 20 20  File  +         
10d8b 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64 62    /* The main db
10d8c 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 6a 6f 75   file */.    jou
10d8d 72 6e 61 6c 46 69 6c 65 53 69 7a 65 20 2a 20 32  rnalFileSize * 2
10d8e 20 2b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20   +       /* The 
10d8f 74 77 6f 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  two journal file
10d90 73 20 2a 2f 20 0a 20 20 20 20 33 2a 6e 50 61 74  s */ .    3*nPat
10d91 68 6e 61 6d 65 20 2b 20 34 30 20 20 20 20 20 20  hname + 40      
10d92 20 20 20 20 20 20 2f 2a 20 7a 46 69 6c 65 6e 61        /* zFilena
10d93 6d 65 2c 20 7a 44 69 72 65 63 74 6f 72 79 2c 20  me, zDirectory, 
10d94 7a 4a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 29 3b  zJournal */.  );
10d95 0a 20 20 69 66 28 20 21 70 50 61 67 65 72 20 29  .  if( !pPager )
10d96 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
10d97 65 65 28 7a 50 61 74 68 6e 61 6d 65 29 3b 0a 20  ee(zPathname);. 
10d98 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
10d99 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 70 50  _NOMEM;.  }.  pP
10d9a 61 67 65 72 2d 3e 70 50 43 61 63 68 65 20 3d 20  ager->pPCache = 
10d9b 28 50 43 61 63 68 65 20 2a 29 26 70 50 61 67 65  (PCache *)&pPage
10d9c 72 5b 31 5d 3b 0a 20 20 70 50 74 72 20 3d 20 28  r[1];.  pPtr = (
10d9d 28 75 38 20 2a 29 26 70 50 61 67 65 72 5b 31 5d  (u8 *)&pPager[1]
10d9e 29 20 2b 20 70 63 61 63 68 65 53 69 7a 65 3b 0a  ) + pcacheSize;.
10d9f 20 20 70 50 61 67 65 72 2d 3e 76 66 73 46 6c 61    pPager->vfsFla
10da0 67 73 20 3d 20 76 66 73 46 6c 61 67 73 3b 0a 20  gs = vfsFlags;. 
10da1 20 70 50 61 67 65 72 2d 3e 66 64 20 3d 20 28 73   pPager->fd = (s
10da2 71 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50  qlite3_file*)&pP
10da3 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c  tr[pVfs->szOsFil
10da4 65 2a 30 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e  e*0];.  pPager->
10da5 73 74 66 64 20 3d 20 28 73 71 6c 69 74 65 33 5f  stfd = (sqlite3_
10da6 66 69 6c 65 2a 29 26 70 50 74 72 5b 70 56 66 73  file*)&pPtr[pVfs
10da7 2d 3e 73 7a 4f 73 46 69 6c 65 5d 3b 0a 20 20 70  ->szOsFile];.  p
10da8 50 61 67 65 72 2d 3e 6a 66 64 20 3d 20 28 73 71  Pager->jfd = (sq
10da9 6c 69 74 65 33 5f 66 69 6c 65 2a 29 26 70 50 74  lite3_file*)&pPt
10daa 72 5b 70 56 66 73 2d 3e 73 7a 4f 73 46 69 6c 65  r[pVfs->szOsFile
10dab 2b 6a 6f 75 72 6e 61 6c 46 69 6c 65 53 69 7a 65  +journalFileSize
10dac 5d 3b 0a 20 20 70 50 61 67 65 72 2d 3e 7a 46 69  ];.  pPager->zFi
10dad 6c 65 6e 61 6d 65 20 3d 20 28 63 68 61 72 2a 29  lename = (char*)
10dae 26 70 50 74 72 5b 70 56 66 73 2d 3e 73 7a 4f 73  &pPtr[pVfs->szOs
10daf 46 69 6c 65 2b 32 2a 6a 6f 75 72 6e 61 6c 46 69  File+2*journalFi
10db0 6c 65 53 69 7a 65 5d 3b 0a 20 20 70 50 61 67 65  leSize];.  pPage
10db1 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 20 3d 20  r->zDirectory = 
10db2 26 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61  &pPager->zFilena
10db3 6d 65 5b 6e 50 61 74 68 6e 61 6d 65 2b 31 5d 3b  me[nPathname+1];
10db4 0a 20 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  .  pPager->zJour
10db5 6e 61 6c 20 3d 20 26 70 50 61 67 65 72 2d 3e 7a  nal = &pPager->z
10db6 44 69 72 65 63 74 6f 72 79 5b 6e 50 61 74 68 6e  Directory[nPathn
10db7 61 6d 65 2b 31 5d 3b 0a 20 20 70 50 61 67 65 72  ame+1];.  pPager
10db8 2d 3e 70 56 66 73 20 3d 20 70 56 66 73 3b 0a 20  ->pVfs = pVfs;. 
10db9 20 69 66 28 20 7a 50 61 74 68 6e 61 6d 65 20 29   if( zPathname )
10dba 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 50 61  {.    memcpy(pPa
10dbb 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
10dbc 7a 50 61 74 68 6e 61 6d 65 2c 20 6e 50 61 74 68  zPathname, nPath
10dbd 6e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 73 71 6c  name+1);.    sql
10dbe 69 74 65 33 5f 66 72 65 65 28 7a 50 61 74 68 6e  ite3_free(zPathn
10dbf 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ame);.  }..  /* 
10dc0 4f 70 65 6e 20 74 68 65 20 70 61 67 65 72 20 66  Open the pager f
10dc1 69 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ile..  */.  if( 
10dc2 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20 7a 46 69  zFilename && zFi
10dc3 6c 65 6e 61 6d 65 5b 30 5d 20 26 26 20 21 6d 65  lename[0] && !me
10dc4 6d 44 62 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  mDb ){.    if( n
10dc5 50 61 74 68 6e 61 6d 65 3e 28 70 56 66 73 2d 3e  Pathname>(pVfs->
10dc6 6d 78 50 61 74 68 6e 61 6d 65 20 2d 20 28 69 6e  mxPathname - (in
10dc7 74 29 73 69 7a 65 6f 66 28 22 2d 6a 6f 75 72 6e  t)sizeof("-journ
10dc8 61 6c 22 29 29 20 29 7b 0a 20 20 20 20 20 20 72  al")) ){.      r
10dc9 63 20 3d 20 53 51 4c 49 54 45 5f 43 41 4e 54 4f  c = SQLITE_CANTO
10dca 50 45 4e 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  PEN;.    }else{.
10dcb 20 20 20 20 20 20 69 6e 74 20 66 6f 75 74 20 3d        int fout =
10dcc 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   0;.      rc = s
10dcd 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28 70 56 66  qlite3OsOpen(pVf
10dce 73 2c 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65  s, pPager->zFile
10dcf 6e 61 6d 65 2c 20 70 50 61 67 65 72 2d 3e 66 64  name, pPager->fd
10dd0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
10dd1 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65             pPage
10dd2 72 2d 3e 76 66 73 46 6c 61 67 73 2c 20 26 66 6f  r->vfsFlags, &fo
10dd3 75 74 29 3b 0a 20 20 20 20 20 20 72 65 61 64 4f  ut);.      readO
10dd4 6e 6c 79 20 3d 20 28 66 6f 75 74 26 53 51 4c 49  nly = (fout&SQLI
10dd5 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
10dd6 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66 20  );..      /* If 
10dd7 74 68 65 20 66 69 6c 65 20 77 61 73 20 73 75 63  the file was suc
10dd8 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e 65 64  cessfully opened
10dd9 20 66 6f 72 20 72 65 61 64 2f 77 72 69 74 65 20   for read/write 
10dda 61 63 63 65 73 73 2c 0a 20 20 20 20 20 20 2a 2a  access,.      **
10ddb 20 63 68 6f 6f 73 65 20 61 20 64 65 66 61 75 6c   choose a defaul
10ddc 74 20 70 61 67 65 20 73 69 7a 65 20 69 6e 20 63  t page size in c
10ddd 61 73 65 20 77 65 20 68 61 76 65 20 74 6f 20 63  ase we have to c
10dde 72 65 61 74 65 20 74 68 65 0a 20 20 20 20 20 20  reate the.      
10ddf 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
10de0 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 70 61  . The default pa
10de1 67 65 20 73 69 7a 65 20 69 73 20 74 68 65 20 6d  ge size is the m
10de2 61 78 69 6d 75 6d 20 6f 66 3a 0a 20 20 20 20 20  aximum of:.     
10de3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
10de4 2b 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  + SQLITE_DEFAULT
10de5 5f 50 41 47 45 5f 53 49 5a 45 2c 0a 20 20 20 20  _PAGE_SIZE,.    
10de6 20 20 2a 2a 20 20 20 20 2b 20 54 68 65 20 76 61    **    + The va
10de7 6c 75 65 20 72 65 74 75 72 6e 65 64 20 62 79 20  lue returned by 
10de8 73 71 6c 69 74 65 33 4f 73 53 65 63 74 6f 72 53  sqlite3OsSectorS
10de9 69 7a 65 28 29 0a 20 20 20 20 20 20 2a 2a 20 20  ize().      **  
10dea 20 20 2b 20 54 68 65 20 6c 61 72 67 65 73 74 20    + The largest 
10deb 70 61 67 65 20 73 69 7a 65 20 74 68 61 74 20 63  page size that c
10dec 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 61 74  an be written at
10ded 6f 6d 69 63 61 6c 6c 79 2e 0a 20 20 20 20 20 20  omically..      
10dee 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  */.      if( rc=
10def 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 72  =SQLITE_OK && !r
10df0 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  eadOnly ){.     
10df1 20 20 20 69 6e 74 20 69 53 65 63 74 6f 72 53 69     int iSectorSi
10df2 7a 65 20 3d 20 73 71 6c 69 74 65 33 4f 73 53 65  ze = sqlite3OsSe
10df3 63 74 6f 72 53 69 7a 65 28 70 50 61 67 65 72 2d  ctorSize(pPager-
10df4 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66  >fd);.        if
10df5 28 20 73 7a 50 61 67 65 44 66 6c 74 3c 69 53 65  ( szPageDflt<iSe
10df6 63 74 6f 72 53 69 7a 65 20 29 7b 0a 20 20 20 20  ctorSize ){.    
10df7 20 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74        szPageDflt
10df8 20 3d 20 69 53 65 63 74 6f 72 53 69 7a 65 3b 0a   = iSectorSize;.
10df9 20 20 20 20 20 20 20 20 7d 0a 23 69 66 64 65 66          }.#ifdef
10dfa 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
10dfb 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 20 20  TOMIC_WRITE.    
10dfc 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 20 20      {.          
10dfd 69 6e 74 20 69 44 63 20 3d 20 73 71 6c 69 74 65  int iDc = sqlite
10dfe 33 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74  3OsDeviceCharact
10dff 65 72 69 73 74 69 63 73 28 70 50 61 67 65 72 2d  eristics(pPager-
10e00 3e 66 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20  >fd);.          
10e01 69 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20  int ii;.        
10e02 20 20 61 73 73 65 72 74 28 53 51 4c 49 54 45 5f    assert(SQLITE_
10e03 49 4f 43 41 50 5f 41 54 4f 4d 49 43 35 31 32 3d  IOCAP_ATOMIC512=
10e04 3d 28 35 31 32 3e 3e 38 29 29 3b 0a 20 20 20 20  =(512>>8));.    
10e05 20 20 20 20 20 20 61 73 73 65 72 74 28 53 51 4c        assert(SQL
10e06 49 54 45 5f 49 4f 43 41 50 5f 41 54 4f 4d 49 43  ITE_IOCAP_ATOMIC
10e07 36 34 4b 3d 3d 28 36 35 35 33 36 3e 3e 38 29 29  64K==(65536>>8))
10e08 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65  ;.          asse
10e09 72 74 28 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45  rt(SQLITE_MAX_DE
10e0a 46 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3c  FAULT_PAGE_SIZE<
10e0b 3d 36 35 35 33 36 29 3b 0a 20 20 20 20 20 20 20  =65536);.       
10e0c 20 20 20 66 6f 72 28 69 69 3d 73 7a 50 61 67 65     for(ii=szPage
10e0d 44 66 6c 74 3b 20 69 69 3c 3d 53 51 4c 49 54 45  Dflt; ii<=SQLITE
10e0e 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41 47  _MAX_DEFAULT_PAG
10e0f 45 5f 53 49 5a 45 3b 20 69 69 3d 69 69 2a 32 29  E_SIZE; ii=ii*2)
10e10 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  {.            if
10e11 28 20 69 44 63 26 28 53 51 4c 49 54 45 5f 49 4f  ( iDc&(SQLITE_IO
10e12 43 41 50 5f 41 54 4f 4d 49 43 7c 28 69 69 3e 3e  CAP_ATOMIC|(ii>>
10e13 38 29 29 20 29 20 73 7a 50 61 67 65 44 66 6c 74  8)) ) szPageDflt
10e14 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 20 20 20   = ii;.         
10e15 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 23 65 6e   }.        }.#en
10e16 64 69 66 0a 20 20 20 20 20 20 20 20 69 66 28 20  dif.        if( 
10e17 73 7a 50 61 67 65 44 66 6c 74 3e 53 51 4c 49 54  szPageDflt>SQLIT
10e18 45 5f 4d 41 58 5f 44 45 46 41 55 4c 54 5f 50 41  E_MAX_DEFAULT_PA
10e19 47 45 5f 53 49 5a 45 20 29 7b 0a 20 20 20 20 20  GE_SIZE ){.     
10e1a 20 20 20 20 20 73 7a 50 61 67 65 44 66 6c 74 20       szPageDflt 
10e1b 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 44 45 46  = SQLITE_MAX_DEF
10e1c 41 55 4c 54 5f 50 41 47 45 5f 53 49 5a 45 3b 0a  AULT_PAGE_SIZE;.
10e1d 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
10e1e 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  }.    }.  }else{
10e1f 0a 20 20 20 20 2f 2a 20 49 66 20 61 20 74 65 6d  .    /* If a tem
10e20 70 6f 72 61 72 79 20 66 69 6c 65 20 69 73 20 72  porary file is r
10e21 65 71 75 65 73 74 65 64 2c 20 69 74 20 69 73 20  equested, it is 
10e22 6e 6f 74 20 6f 70 65 6e 65 64 20 69 6d 6d 65 64  not opened immed
10e23 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a 20 49  iately..    ** I
10e24 6e 20 74 68 69 73 20 63 61 73 65 20 77 65 20 61  n this case we a
10e25 63 63 65 70 74 20 74 68 65 20 64 65 66 61 75 6c  ccept the defaul
10e26 74 20 70 61 67 65 20 73 69 7a 65 20 61 6e 64 20  t page size and 
10e27 64 65 6c 61 79 20 61 63 74 75 61 6c 6c 79 0a 20  delay actually. 
10e28 20 20 20 2a 2a 20 6f 70 65 6e 69 6e 67 20 74 68     ** opening th
10e29 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 74 68 65  e file until the
10e2a 20 66 69 72 73 74 20 63 61 6c 6c 20 74 6f 20 4f   first call to O
10e2b 73 57 72 69 74 65 28 29 2e 0a 20 20 20 20 2a 2a  sWrite()..    **
10e2c 0a 20 20 20 20 2a 2a 20 54 68 69 73 20 62 72 61  .    ** This bra
10e2d 6e 63 68 20 69 73 20 61 6c 73 6f 20 72 75 6e 20  nch is also run 
10e2e 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
10e2f 20 64 61 74 61 62 61 73 65 2e 20 41 6e 20 69 6e   database. An in
10e30 2d 6d 65 6d 6f 72 79 0a 20 20 20 20 2a 2a 20 64  -memory.    ** d
10e31 61 74 61 62 61 73 65 20 69 73 20 74 68 65 20 73  atabase is the s
10e32 61 6d 65 20 61 73 20 61 20 74 65 6d 70 2d 66 69  ame as a temp-fi
10e33 6c 65 20 74 68 61 74 20 69 73 20 6e 65 76 65 72  le that is never
10e34 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74 6f 0a   written out to.
10e35 20 20 20 20 2a 2a 20 64 69 73 6b 20 61 6e 64 20      ** disk and 
10e36 75 73 65 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  uses an in-memor
10e37 79 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  y rollback journ
10e38 61 6c 2e 0a 20 20 20 20 2a 2f 20 0a 20 20 20 20  al..    */ .    
10e39 74 65 6d 70 46 69 6c 65 20 3d 20 31 3b 0a 20 20  tempFile = 1;.  
10e3a 20 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20    pPager->state 
10e3b 3d 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  = PAGER_EXCLUSIV
10e3c 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50  E;.  }..  if( pP
10e3d 61 67 65 72 20 26 26 20 72 63 3d 3d 53 51 4c 49  ager && rc==SQLI
10e3e 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50 61  TE_OK ){.    pPa
10e3f 67 65 72 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  ger->pTmpSpace =
10e40 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
10e41 6f 63 28 73 7a 50 61 67 65 44 66 6c 74 29 3b 0a  oc(szPageDflt);.
10e42 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 20    }..  /* If an 
10e43 65 72 72 6f 72 20 6f 63 63 75 72 65 64 20 69 6e  error occured in
10e44 20 65 69 74 68 65 72 20 6f 66 20 74 68 65 20 62   either of the b
10e45 6c 6f 63 6b 73 20 61 62 6f 76 65 2e 0a 20 20 2a  locks above..  *
10e46 2a 20 46 72 65 65 20 74 68 65 20 50 61 67 65 72  * Free the Pager
10e47 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 63   structure and c
10e48 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a 20  lose the file.. 
10e49 20 2a 2a 20 53 69 6e 63 65 20 74 68 65 20 70 61   ** Since the pa
10e4a 67 65 72 20 69 73 20 6e 6f 74 20 61 6c 6c 6f 63  ger is not alloc
10e4b 61 74 65 64 20 74 68 65 72 65 20 69 73 20 6e 6f  ated there is no
10e4c 20 6e 65 65 64 20 74 6f 20 73 65 74 20 0a 20 20   need to set .  
10e4d 2a 2a 20 61 6e 79 20 50 61 67 65 72 2e 65 72 72  ** any Pager.err
10e4e 4d 61 73 6b 20 76 61 72 69 61 62 6c 65 73 2e 0a  Mask variables..
10e4f 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 50 61 67    */.  if( !pPag
10e50 65 72 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e 70  er || !pPager->p
10e51 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20  TmpSpace ){.    
10e52 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70  sqlite3OsClose(p
10e53 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20  Pager->fd);.    
10e54 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 50 61  sqlite3_free(pPa
10e55 67 65 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ger);.    return
10e56 20 28 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b   ((rc==SQLITE_OK
10e57 29 3f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3a 72  )?SQLITE_NOMEM:r
10e58 63 29 3b 0a 20 20 7d 0a 20 20 6e 45 78 74 72 61  c);.  }.  nExtra
10e59 20 3d 20 46 4f 52 43 45 5f 41 4c 49 47 4e 4d 45   = FORCE_ALIGNME
10e5a 4e 54 28 6e 45 78 74 72 61 29 3b 0a 20 20 73 71  NT(nExtra);.  sq
10e5b 6c 69 74 65 33 50 63 61 63 68 65 4f 70 65 6e 28  lite3PcacheOpen(
10e5c 73 7a 50 61 67 65 44 66 6c 74 2c 20 6e 45 78 74  szPageDflt, nExt
10e5d 72 61 2c 20 21 6d 65 6d 44 62 2c 0a 20 20 20 20  ra, !memDb,.    
10e5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
10e5f 21 6d 65 6d 44 62 3f 70 61 67 65 72 53 74 72 65  !memDb?pagerStre
10e60 73 73 3a 30 2c 20 28 76 6f 69 64 20 2a 29 70 50  ss:0, (void *)pP
10e61 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 70 50  ager, pPager->pP
10e62 43 61 63 68 65 29 3b 0a 0a 20 20 50 41 47 45 52  Cache);..  PAGER
10e63 54 52 41 43 45 33 28 22 4f 50 45 4e 20 25 64 20  TRACE3("OPEN %d 
10e64 25 73 5c 6e 22 2c 20 46 49 4c 45 48 41 4e 44 4c  %s\n", FILEHANDL
10e65 45 49 44 28 70 50 61 67 65 72 2d 3e 66 64 29 2c  EID(pPager->fd),
10e66 20 70 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61   pPager->zFilena
10e67 6d 65 29 3b 0a 20 20 49 4f 54 52 41 43 45 28 28  me);.  IOTRACE((
10e68 22 4f 50 45 4e 20 25 70 20 25 73 5c 6e 22 2c 20  "OPEN %p %s\n", 
10e69 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
10e6a 7a 46 69 6c 65 6e 61 6d 65 29 29 0a 0a 20 20 2f  zFilename))..  /
10e6b 2a 20 46 69 6c 6c 20 69 6e 20 50 61 67 65 72 2e  * Fill in Pager.
10e6c 7a 44 69 72 65 63 74 6f 72 79 5b 5d 20 2a 2f 0a  zDirectory[] */.
10e6d 20 20 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d    memcpy(pPager-
10e6e 3e 7a 44 69 72 65 63 74 6f 72 79 2c 20 70 50 61  >zDirectory, pPa
10e6f 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20  ger->zFilename, 
10e70 6e 50 61 74 68 6e 61 6d 65 2b 31 29 3b 0a 20 20  nPathname+1);.  
10e71 66 6f 72 28 69 3d 73 74 72 6c 65 6e 28 70 50 61  for(i=strlen(pPa
10e72 67 65 72 2d 3e 7a 44 69 72 65 63 74 6f 72 79 29  ger->zDirectory)
10e73 3b 20 69 3e 30 20 26 26 20 70 50 61 67 65 72 2d  ; i>0 && pPager-
10e74 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31 5d  >zDirectory[i-1]
10e75 21 3d 27 2f 27 3b 20 69 2d 2d 29 7b 7d 0a 20 20  !='/'; i--){}.  
10e76 69 66 28 20 69 3e 30 20 29 20 70 50 61 67 65 72  if( i>0 ) pPager
10e77 2d 3e 7a 44 69 72 65 63 74 6f 72 79 5b 69 2d 31  ->zDirectory[i-1
10e78 5d 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 46 69 6c  ] = 0;..  /* Fil
10e79 6c 20 69 6e 20 50 61 67 65 72 2e 7a 4a 6f 75 72  l in Pager.zJour
10e7a 6e 61 6c 5b 5d 20 2a 2f 0a 20 20 69 66 28 20 7a  nal[] */.  if( z
10e7b 50 61 74 68 6e 61 6d 65 20 29 7b 0a 20 20 20 20  Pathname ){.    
10e7c 6d 65 6d 63 70 79 28 70 50 61 67 65 72 2d 3e 7a  memcpy(pPager->z
10e7d 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67 65 72 2d  Journal, pPager-
10e7e 3e 7a 46 69 6c 65 6e 61 6d 65 2c 20 6e 50 61 74  >zFilename, nPat
10e7f 68 6e 61 6d 65 29 3b 0a 20 20 20 20 6d 65 6d 63  hname);.    memc
10e80 70 79 28 26 70 50 61 67 65 72 2d 3e 7a 4a 6f 75  py(&pPager->zJou
10e81 72 6e 61 6c 5b 6e 50 61 74 68 6e 61 6d 65 5d 2c  rnal[nPathname],
10e82 20 22 2d 6a 6f 75 72 6e 61 6c 22 2c 20 39 29 3b   "-journal", 9);
10e83 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50  .  }else{.    pP
10e84 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 20 3d  ager->zJournal =
10e85 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 70 50   0;.  }..  /* pP
10e86 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
10e87 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  n = 0; */.  pPag
10e88 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 3d  er->useJournal =
10e89 20 75 73 65 4a 6f 75 72 6e 61 6c 3b 0a 20 20 70   useJournal;.  p
10e8a 50 61 67 65 72 2d 3e 6e 6f 52 65 61 64 6c 6f 63  Pager->noReadloc
10e8b 6b 20 3d 20 6e 6f 52 65 61 64 6c 6f 63 6b 20 26  k = noReadlock &
10e8c 26 20 72 65 61 64 4f 6e 6c 79 3b 0a 20 20 2f 2a  & readOnly;.  /*
10e8d 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65   pPager->stmtOpe
10e8e 6e 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  n = 0; */.  /* p
10e8f 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65  Pager->stmtInUse
10e90 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70 50   = 0; */.  /* pP
10e91 61 67 65 72 2d 3e 6e 52 65 66 20 3d 20 30 3b 20  ager->nRef = 0; 
10e92 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 53  */.  pPager->dbS
10e93 69 7a 65 56 61 6c 69 64 20 3d 20 6d 65 6d 44 62  izeValid = memDb
10e94 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70 61 67 65  ;.  pPager->page
10e95 53 69 7a 65 20 3d 20 73 7a 50 61 67 65 44 66 6c  Size = szPageDfl
10e96 74 3b 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e  t;.  /* pPager->
10e97 73 74 6d 74 53 69 7a 65 20 3d 20 30 3b 20 2a 2f  stmtSize = 0; */
10e98 0a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 73 74  .  /* pPager->st
10e99 6d 74 4a 53 69 7a 65 20 3d 20 30 3b 20 2a 2f 0a  mtJSize = 0; */.
10e9a 20 20 2f 2a 20 70 50 61 67 65 72 2d 3e 6e 50 61    /* pPager->nPa
10e9b 67 65 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61  ge = 0; */.  pPa
10e9c 67 65 72 2d 3e 6d 78 50 61 67 65 20 3d 20 31 30  ger->mxPage = 10
10e9d 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6d 78 50  0;.  pPager->mxP
10e9e 67 6e 6f 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58  gno = SQLITE_MAX
10e9f 5f 50 41 47 45 5f 43 4f 55 4e 54 3b 0a 20 20 2f  _PAGE_COUNT;.  /
10ea0 2a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 20  * pPager->state 
10ea1 3d 20 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 3b 20  = PAGER_UNLOCK; 
10ea2 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
10ea3 67 65 72 2d 3e 73 74 61 74 65 20 3d 3d 20 28 74  ger->state == (t
10ea4 65 6d 70 46 69 6c 65 20 3f 20 50 41 47 45 52 5f  empFile ? PAGER_
10ea5 45 58 43 4c 55 53 49 56 45 20 3a 20 50 41 47 45  EXCLUSIVE : PAGE
10ea6 52 5f 55 4e 4c 4f 43 4b 29 20 29 3b 0a 20 20 2f  R_UNLOCK) );.  /
10ea7 2a 20 70 50 61 67 65 72 2d 3e 65 72 72 4d 61 73  * pPager->errMas
10ea8 6b 20 3d 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67  k = 0; */.  pPag
10ea9 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 3d 20 74  er->tempFile = t
10eaa 65 6d 70 46 69 6c 65 3b 0a 20 20 61 73 73 65 72  empFile;.  asser
10eab 74 28 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41 47  t( tempFile==PAG
10eac 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e  ER_LOCKINGMODE_N
10ead 4f 52 4d 41 4c 20 0a 20 20 20 20 20 20 20 20 20  ORMAL .         
10eae 20 7c 7c 20 74 65 6d 70 46 69 6c 65 3d 3d 50 41   || tempFile==PA
10eaf 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f  GER_LOCKINGMODE_
10eb0 45 58 43 4c 55 53 49 56 45 20 29 3b 0a 20 20 61  EXCLUSIVE );.  a
10eb1 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43  ssert( PAGER_LOC
10eb2 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
10eb3 56 45 3d 3d 31 20 29 3b 0a 20 20 70 50 61 67 65  VE==1 );.  pPage
10eb4 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64 65  r->exclusiveMode
10eb5 20 3d 20 74 65 6d 70 46 69 6c 65 3b 20 0a 20 20   = tempFile; .  
10eb6 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 3d 20  pPager->memDb = 
10eb7 6d 65 6d 44 62 3b 0a 20 20 70 50 61 67 65 72 2d  memDb;.  pPager-
10eb8 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 72 65 61 64  >readOnly = read
10eb9 4f 6e 6c 79 3b 0a 20 20 2f 2a 20 70 50 61 67 65  Only;.  /* pPage
10eba 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  r->needSync = 0;
10ebb 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d 3e 6e 6f   */.  pPager->no
10ebc 53 79 6e 63 20 3d 20 70 50 61 67 65 72 2d 3e 74  Sync = pPager->t
10ebd 65 6d 70 46 69 6c 65 20 7c 7c 20 21 75 73 65 4a  empFile || !useJ
10ebe 6f 75 72 6e 61 6c 3b 0a 20 20 70 50 61 67 65 72  ournal;.  pPager
10ebf 2d 3e 66 75 6c 6c 53 79 6e 63 20 3d 20 28 70 50  ->fullSync = (pP
10ec0 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3f 30 3a 31  ager->noSync?0:1
10ec1 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 79 6e  );.  pPager->syn
10ec2 63 5f 66 6c 61 67 73 20 3d 20 53 51 4c 49 54 45  c_flags = SQLITE
10ec3 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c 3b 0a 20 20  _SYNC_NORMAL;.  
10ec4 2f 2a 20 70 50 61 67 65 72 2d 3e 70 46 69 72 73  /* pPager->pFirs
10ec5 74 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a 20 70  t = 0; */.  /* p
10ec6 50 61 67 65 72 2d 3e 70 46 69 72 73 74 53 79 6e  Pager->pFirstSyn
10ec7 63 65 64 20 3d 20 30 3b 20 2a 2f 0a 20 20 2f 2a  ced = 0; */.  /*
10ec8 20 70 50 61 67 65 72 2d 3e 70 4c 61 73 74 20 3d   pPager->pLast =
10ec9 20 30 3b 20 2a 2f 0a 20 20 70 50 61 67 65 72 2d   0; */.  pPager-
10eca 3e 6e 45 78 74 72 61 20 3d 20 6e 45 78 74 72 61  >nExtra = nExtra
10ecb 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72  ;.  pPager->jour
10ecc 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 53  nalSizeLimit = S
10ecd 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 4a 4f  QLITE_DEFAULT_JO
10ece 55 52 4e 41 4c 5f 53 49 5a 45 5f 4c 49 4d 49 54  URNAL_SIZE_LIMIT
10ecf 3b 0a 20 20 61 73 73 65 72 74 28 70 50 61 67 65  ;.  assert(pPage
10ed0 72 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 7c  r->fd->pMethods|
10ed1 7c 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20 73 65  |tempFile);.  se
10ed2 74 53 65 63 74 6f 72 53 69 7a 65 28 70 50 61 67  tSectorSize(pPag
10ed3 65 72 29 3b 0a 20 20 69 66 28 20 6d 65 6d 44 62  er);.  if( memDb
10ed4 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e   ){.    pPager->
10ed5 6a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20 50 41  journalMode = PA
10ed6 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
10ed7 4d 45 4d 4f 52 59 3b 0a 20 20 7d 0a 20 20 2f 2a  MEMORY;.  }.  /*
10ed8 20 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61   pPager->xBusyHa
10ed9 6e 64 6c 65 72 20 3d 20 30 3b 20 2a 2f 0a 20 20  ndler = 0; */.  
10eda 2f 2a 20 70 50 61 67 65 72 2d 3e 70 42 75 73 79  /* pPager->pBusy
10edb 48 61 6e 64 6c 65 72 41 72 67 20 3d 20 30 3b 20  HandlerArg = 0; 
10edc 2a 2f 0a 20 20 2f 2a 20 6d 65 6d 73 65 74 28 70  */.  /* memset(p
10edd 50 61 67 65 72 2d 3e 61 48 61 73 68 2c 20 30 2c  Pager->aHash, 0,
10ede 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e   sizeof(pPager->
10edf 61 48 61 73 68 29 29 3b 20 2a 2f 0a 20 20 2a 70  aHash)); */.  *p
10ee0 70 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b  pPager = pPager;
10ee1 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
10ee2 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  _OK;.}../*.** Se
10ee3 74 20 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c  t the busy handl
10ee4 65 72 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a  er function..*/.
10ee5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
10ee6 6f 69 64 20 73 71 6c 69 74 65 33 50 61 67 65 72  oid sqlite3Pager
10ee7 53 65 74 42 75 73 79 68 61 6e 64 6c 65 72 28 0a  SetBusyhandler(.
10ee8 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c    Pager *pPager,
10ee9 20 0a 20 20 69 6e 74 20 28 2a 78 42 75 73 79 48   .  int (*xBusyH
10eea 61 6e 64 6c 65 72 29 28 76 6f 69 64 20 2a 29 2c  andler)(void *),
10eeb 0a 20 20 76 6f 69 64 20 2a 70 42 75 73 79 48 61  .  void *pBusyHa
10eec 6e 64 6c 65 72 41 72 67 0a 29 7b 20 20 0a 20 20  ndlerArg.){  .  
10eed 70 50 61 67 65 72 2d 3e 78 42 75 73 79 48 61 6e  pPager->xBusyHan
10eee 64 6c 65 72 20 3d 20 78 42 75 73 79 48 61 6e 64  dler = xBusyHand
10eef 6c 65 72 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70  ler;.  pPager->p
10ef0 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67 20 3d  BusyHandlerArg =
10ef1 20 70 42 75 73 79 48 61 6e 64 6c 65 72 41 72 67   pBusyHandlerArg
10ef2 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
10ef3 68 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72  he reinitializer
10ef4 20 66 6f 72 20 74 68 69 73 20 70 61 67 65 72 2e   for this pager.
10ef5 20 20 49 66 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74    If not NULL, t
10ef6 68 65 20 72 65 69 6e 69 74 69 61 6c 69 7a 65 72  he reinitializer
10ef7 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 77 68  .** is called wh
10ef8 65 6e 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  en the content o
10ef9 66 20 61 20 70 61 67 65 20 69 6e 20 63 61 63 68  f a page in cach
10efa 65 20 69 73 20 72 65 73 74 6f 72 65 64 20 74 6f  e is restored to
10efb 20 69 74 73 20 6f 72 69 67 69 6e 61 6c 0a 2a 2a   its original.**
10efc 20 76 61 6c 75 65 20 61 73 20 61 20 72 65 73 75   value as a resu
10efd 6c 74 20 6f 66 20 61 20 72 6f 6c 6c 62 61 63 6b  lt of a rollback
10efe 2e 20 20 54 68 65 20 63 61 6c 6c 62 61 63 6b 20  .  The callback 
10eff 67 69 76 65 73 20 68 69 67 68 65 72 2d 6c 65 76  gives higher-lev
10f00 65 6c 20 63 6f 64 65 0a 2a 2a 20 61 6e 20 6f 70  el code.** an op
10f01 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 72 65 73  portunity to res
10f02 74 6f 72 65 20 74 68 65 20 45 58 54 52 41 20 73  tore the EXTRA s
10f03 65 63 74 69 6f 6e 20 74 6f 20 61 67 72 65 65 20  ection to agree 
10f04 77 69 74 68 20 74 68 65 20 72 65 73 74 6f 72 65  with the restore
10f05 64 0a 2a 2a 20 70 61 67 65 20 64 61 74 61 2e 0a  d.** page data..
10f06 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
10f07 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61  E void sqlite3Pa
10f08 67 65 72 53 65 74 52 65 69 6e 69 74 65 72 28 50  gerSetReiniter(P
10f09 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 76 6f  ager *pPager, vo
10f0a 69 64 20 28 2a 78 52 65 69 6e 69 74 29 28 44 62  id (*xReinit)(Db
10f0b 50 61 67 65 2a 29 29 7b 0a 20 20 70 50 61 67 65  Page*)){.  pPage
10f0c 72 2d 3e 78 52 65 69 6e 69 74 65 72 20 3d 20 78  r->xReiniter = x
10f0d 52 65 69 6e 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Reinit;.}../*.**
10f0e 20 53 65 74 20 74 68 65 20 70 61 67 65 20 73 69   Set the page si
10f0f 7a 65 20 74 6f 20 2a 70 50 61 67 65 53 69 7a 65  ze to *pPageSize
10f10 2e 20 49 66 20 74 68 65 20 73 75 67 67 65 73 74  . If the suggest
10f11 20 6e 65 77 20 70 61 67 65 20 73 69 7a 65 20 69   new page size i
10f12 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69 61  s.** inappropria
10f13 74 65 2c 20 74 68 65 6e 20 61 6e 20 61 6c 74 65  te, then an alte
10f14 72 6e 61 74 69 76 65 20 70 61 67 65 20 73 69 7a  rnative page siz
10f15 65 20 69 73 20 73 65 74 20 74 6f 20 74 68 61 74  e is set to that
10f16 0a 2a 2a 20 76 61 6c 75 65 20 62 65 66 6f 72 65  .** value before
10f17 20 72 65 74 75 72 6e 69 6e 67 2e 0a 2a 2f 0a 53   returning..*/.S
10f18 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10f19 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65  t sqlite3PagerSe
10f1a 74 50 61 67 65 73 69 7a 65 28 50 61 67 65 72 20  tPagesize(Pager 
10f1b 2a 70 50 61 67 65 72 2c 20 75 31 36 20 2a 70 50  *pPager, u16 *pP
10f1c 61 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20  ageSize){.  int 
10f1d 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  rc = pPager->err
10f1e 43 6f 64 65 3b 0a 20 20 69 66 28 20 72 63 3d 3d  Code;.  if( rc==
10f1f 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
10f20 20 75 31 36 20 70 61 67 65 53 69 7a 65 20 3d 20   u16 pageSize = 
10f21 2a 70 50 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  *pPageSize;.    
10f22 61 73 73 65 72 74 28 20 70 61 67 65 53 69 7a 65  assert( pageSize
10f23 3d 3d 30 20 7c 7c 20 28 70 61 67 65 53 69 7a 65  ==0 || (pageSize
10f24 3e 3d 35 31 32 20 26 26 20 70 61 67 65 53 69 7a  >=512 && pageSiz
10f25 65 3c 3d 53 51 4c 49 54 45 5f 4d 41 58 5f 50 41  e<=SQLITE_MAX_PA
10f26 47 45 5f 53 49 5a 45 29 20 29 3b 0a 20 20 20 20  GE_SIZE) );.    
10f27 69 66 28 20 70 61 67 65 53 69 7a 65 20 26 26 20  if( pageSize && 
10f28 70 61 67 65 53 69 7a 65 21 3d 70 50 61 67 65 72  pageSize!=pPager
10f29 2d 3e 70 61 67 65 53 69 7a 65 20 0a 20 20 20 20  ->pageSize .    
10f2a 20 26 26 20 28 70 50 61 67 65 72 2d 3e 6d 65 6d   && (pPager->mem
10f2b 44 62 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  Db==0 || pPager-
10f2c 3e 64 62 53 69 7a 65 3d 3d 30 29 0a 20 20 20 20  >dbSize==0).    
10f2d 20 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68   && sqlite3Pcach
10f2e 65 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72  eRefCount(pPager
10f2f 2d 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 0a 20  ->pPCache)==0 . 
10f30 20 20 20 29 7b 0a 20 20 20 20 20 20 63 68 61 72     ){.      char
10f31 20 2a 70 4e 65 77 20 3d 20 28 63 68 61 72 20 2a   *pNew = (char *
10f32 29 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c  )sqlite3PageMall
10f33 6f 63 28 70 61 67 65 53 69 7a 65 29 3b 0a 20 20  oc(pageSize);.  
10f34 20 20 20 20 69 66 28 20 21 70 4e 65 77 20 29 7b      if( !pNew ){
10f35 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51  .        rc = SQ
10f36 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
10f37 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
10f38 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
10f39 67 65 72 29 3b 0a 20 20 20 20 20 20 20 20 70 50  ger);.        pP
10f3a 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 20 3d  ager->pageSize =
10f3b 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 20   pageSize;.     
10f3c 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
10f3d 6d 65 6d 44 62 20 29 20 73 65 74 53 65 63 74 6f  memDb ) setSecto
10f3e 72 53 69 7a 65 28 70 50 61 67 65 72 29 3b 0a 20  rSize(pPager);. 
10f3f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
10f40 67 65 46 72 65 65 28 70 50 61 67 65 72 2d 3e 70  geFree(pPager->p
10f41 54 6d 70 53 70 61 63 65 29 3b 0a 20 20 20 20 20  TmpSpace);.     
10f42 20 20 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53     pPager->pTmpS
10f43 70 61 63 65 20 3d 20 70 4e 65 77 3b 0a 20 20 20  pace = pNew;.   
10f44 20 20 20 20 20 73 71 6c 69 74 65 33 50 63 61 63       sqlite3Pcac
10f45 68 65 53 65 74 50 61 67 65 53 69 7a 65 28 70 50  heSetPageSize(pP
10f46 61 67 65 72 2d 3e 70 50 43 61 63 68 65 2c 20 70  ager->pPCache, p
10f47 61 67 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20  ageSize);.      
10f48 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 50 61  }.    }.    *pPa
10f49 67 65 53 69 7a 65 20 3d 20 70 50 61 67 65 72 2d  geSize = pPager-
10f4a 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 7d 0a 20  >pageSize;.  }. 
10f4b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
10f4c 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
10f4d 69 6e 74 65 72 20 74 6f 20 74 68 65 20 22 74 65  inter to the "te
10f4e 6d 70 6f 72 61 72 79 20 70 61 67 65 22 20 62 75  mporary page" bu
10f4f 66 66 65 72 20 68 65 6c 64 20 69 6e 74 65 72 6e  ffer held intern
10f50 61 6c 6c 79 0a 2a 2a 20 62 79 20 74 68 65 20 70  ally.** by the p
10f51 61 67 65 72 2e 20 20 54 68 69 73 20 69 73 20 61  ager.  This is a
10f52 20 62 75 66 66 65 72 20 74 68 61 74 20 69 73 20   buffer that is 
10f53 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  big enough to ho
10f54 6c 64 20 74 68 65 0a 2a 2a 20 65 6e 74 69 72 65  ld the.** entire
10f55 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 64 61   content of a da
10f56 74 61 62 61 73 65 20 70 61 67 65 2e 20 20 54 68  tabase page.  Th
10f57 69 73 20 62 75 66 66 65 72 20 69 73 20 75 73 65  is buffer is use
10f58 64 20 69 6e 74 65 72 6e 61 6c 6c 79 0a 2a 2a 20  d internally.** 
10f59 64 75 72 69 6e 67 20 72 6f 6c 6c 62 61 63 6b 20  during rollback 
10f5a 61 6e 64 20 77 69 6c 6c 20 62 65 20 6f 76 65 72  and will be over
10f5b 77 72 69 74 74 65 6e 20 77 68 65 6e 65 76 65 72  written whenever
10f5c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f   a rollback.** o
10f5d 63 63 75 72 73 2e 20 20 42 75 74 20 6f 74 68 65  ccurs.  But othe
10f5e 72 20 6d 6f 64 75 6c 65 73 20 61 72 65 20 66 72  r modules are fr
10f5f 65 65 20 74 6f 20 75 73 65 20 69 74 20 74 6f 6f  ee to use it too
10f60 2c 20 61 73 20 6c 6f 6e 67 20 61 73 0a 2a 2a 20  , as long as.** 
10f61 6e 6f 20 72 6f 6c 6c 62 61 63 6b 73 20 61 72 65  no rollbacks are
10f62 20 68 61 70 70 65 6e 69 6e 67 2e 0a 2a 2f 0a 53   happening..*/.S
10f63 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
10f64 69 64 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  id *sqlite3Pager
10f65 54 65 6d 70 53 70 61 63 65 28 50 61 67 65 72 20  TempSpace(Pager 
10f66 2a 70 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75  *pPager){.  retu
10f67 72 6e 20 70 50 61 67 65 72 2d 3e 70 54 6d 70 53  rn pPager->pTmpS
10f68 70 61 63 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  pace;.}../*.** A
10f69 74 74 65 6d 70 74 20 74 6f 20 73 65 74 20 74 68  ttempt to set th
10f6a 65 20 6d 61 78 69 6d 75 6d 20 64 61 74 61 62 61  e maximum databa
10f6b 73 65 20 70 61 67 65 20 63 6f 75 6e 74 20 69 66  se page count if
10f6c 20 6d 78 50 61 67 65 20 69 73 20 70 6f 73 69 74   mxPage is posit
10f6d 69 76 65 2e 20 0a 2a 2a 20 4d 61 6b 65 20 6e 6f  ive. .** Make no
10f6e 20 63 68 61 6e 67 65 73 20 69 66 20 6d 78 50 61   changes if mxPa
10f6f 67 65 20 69 73 20 7a 65 72 6f 20 6f 72 20 6e 65  ge is zero or ne
10f70 67 61 74 69 76 65 2e 20 20 41 6e 64 20 6e 65 76  gative.  And nev
10f71 65 72 20 72 65 64 75 63 65 20 74 68 65 0a 2a 2a  er reduce the.**
10f72 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63 6f   maximum page co
10f73 75 6e 74 20 62 65 6c 6f 77 20 74 68 65 20 63 75  unt below the cu
10f74 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74 68  rrent size of th
10f75 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a  e database..**.*
10f76 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f 66 20  * Regardless of 
10f77 6d 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74  mxPage, return t
10f78 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78 69 6d  he current maxim
10f79 75 6d 20 70 61 67 65 20 63 6f 75 6e 74 2e 0a 2a  um page count..*
10f7a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
10f7b 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
10f7c 72 4d 61 78 50 61 67 65 43 6f 75 6e 74 28 50 61  rMaxPageCount(Pa
10f7d 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
10f7e 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 66 28 20   mxPage){.  if( 
10f7f 6d 78 50 61 67 65 3e 30 20 29 7b 0a 20 20 20 20  mxPage>0 ){.    
10f80 70 50 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 3d  pPager->mxPgno =
10f81 20 6d 78 50 61 67 65 3b 0a 20 20 7d 0a 20 20 73   mxPage;.  }.  s
10f82 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63  qlite3PagerPagec
10f83 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30 29 3b  ount(pPager, 0);
10f84 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67 65 72  .  return pPager
10f85 2d 3e 6d 78 50 67 6e 6f 3b 0a 7d 0a 0a 2f 2a 0a  ->mxPgno;.}../*.
10f86 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
10f87 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
10f88 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 69 73   are used to dis
10f89 61 62 6c 65 20 74 68 65 20 73 69 6d 75 6c 61 74  able the simulat
10f8a 65 64 0a 2a 2a 20 49 2f 4f 20 65 72 72 6f 72 20  ed.** I/O error 
10f8b 6d 65 63 68 61 6e 69 73 6d 2e 20 20 54 68 65 73  mechanism.  Thes
10f8c 65 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75  e routines are u
10f8d 73 65 64 20 74 6f 20 61 76 6f 69 64 20 73 69 6d  sed to avoid sim
10f8e 75 6c 61 74 65 64 0a 2a 2a 20 65 72 72 6f 72 73  ulated.** errors
10f8f 20 69 6e 20 70 6c 61 63 65 73 20 77 68 65 72 65   in places where
10f90 20 77 65 20 64 6f 20 6e 6f 74 20 63 61 72 65 20   we do not care 
10f91 61 62 6f 75 74 20 65 72 72 6f 72 73 2e 0a 2a 2a  about errors..**
10f92 0a 2a 2a 20 55 6e 6c 65 73 73 20 2d 44 53 51 4c  .** Unless -DSQL
10f93 49 54 45 5f 54 45 53 54 3d 31 20 69 73 20 75 73  ITE_TEST=1 is us
10f94 65 64 2c 20 74 68 65 73 65 20 72 6f 75 74 69 6e  ed, these routin
10f95 65 73 20 61 72 65 20 61 6c 6c 20 6e 6f 2d 6f 70  es are all no-op
10f96 73 0a 2a 2a 20 61 6e 64 20 67 65 6e 65 72 61 74  s.** and generat
10f97 65 20 6e 6f 20 63 6f 64 65 2e 0a 2a 2f 0a 23 69  e no code..*/.#i
10f98 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
10f99 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65  .SQLITE_API exte
10f9a 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  rn int sqlite3_i
10f9b 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 3b  o_error_pending;
10f9c 0a 53 51 4c 49 54 45 5f 41 50 49 20 65 78 74 65  .SQLITE_API exte
10f9d 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 69  rn int sqlite3_i
10f9e 6f 5f 65 72 72 6f 72 5f 68 69 74 3b 0a 73 74 61  o_error_hit;.sta
10f9f 74 69 63 20 69 6e 74 20 73 61 76 65 64 5f 63 6e  tic int saved_cn
10fa0 74 3b 0a 76 6f 69 64 20 64 69 73 61 62 6c 65 5f  t;.void disable_
10fa1 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72  simulated_io_err
10fa2 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73 61 76  ors(void){.  sav
10fa3 65 64 5f 63 6e 74 20 3d 20 73 71 6c 69 74 65 33  ed_cnt = sqlite3
10fa4 5f 69 6f 5f 65 72 72 6f 72 5f 70 65 6e 64 69 6e  _io_error_pendin
10fa5 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f  g;.  sqlite3_io_
10fa6 65 72 72 6f 72 5f 70 65 6e 64 69 6e 67 20 3d 20  error_pending = 
10fa7 2d 31 3b 0a 7d 0a 76 6f 69 64 20 65 6e 61 62 6c  -1;.}.void enabl
10fa8 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
10fa9 72 72 6f 72 73 28 76 6f 69 64 29 7b 0a 20 20 73  rrors(void){.  s
10faa 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72 5f  qlite3_io_error_
10fab 70 65 6e 64 69 6e 67 20 3d 20 73 61 76 65 64 5f  pending = saved_
10fac 63 6e 74 3b 0a 7d 0a 23 65 6c 73 65 0a 23 20 64  cnt;.}.#else.# d
10fad 65 66 69 6e 65 20 64 69 73 61 62 6c 65 5f 73 69  efine disable_si
10fae 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
10faf 73 28 29 0a 23 20 64 65 66 69 6e 65 20 65 6e 61  s().# define ena
10fb0 62 6c 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f  ble_simulated_io
10fb1 5f 65 72 72 6f 72 73 28 29 0a 23 65 6e 64 69 66  _errors().#endif
10fb2 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65  ../*.** Read the
10fb3 20 66 69 72 73 74 20 4e 20 62 79 74 65 73 20 66   first N bytes f
10fb4 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
10fb5 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e  g of the file in
10fb6 74 6f 20 6d 65 6d 6f 72 79 0a 2a 2a 20 74 68 61  to memory.** tha
10fb7 74 20 70 44 65 73 74 20 70 6f 69 6e 74 73 20 74  t pDest points t
10fb8 6f 2e 20 0a 2a 2a 0a 2a 2a 20 4e 6f 20 65 72 72  o. .**.** No err
10fb9 6f 72 20 63 68 65 63 6b 69 6e 67 20 69 73 20 64  or checking is d
10fba 6f 6e 65 2e 20 54 68 65 20 72 61 74 69 6f 6e 61  one. The rationa
10fbb 6c 20 66 6f 72 20 74 68 69 73 20 69 73 20 74 68  l for this is th
10fbc 61 74 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  at this function
10fbd 20 0a 2a 2a 20 6d 61 79 20 62 65 20 63 61 6c 6c   .** may be call
10fbe 65 64 20 65 76 65 6e 20 69 66 20 74 68 65 20 66  ed even if the f
10fbf 69 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ile does not exi
10fc0 73 74 20 6f 72 20 63 6f 6e 74 61 69 6e 20 61 20  st or contain a 
10fc1 68 65 61 64 65 72 2e 20 49 6e 20 0a 2a 2a 20 74  header. In .** t
10fc2 68 65 73 65 20 63 61 73 65 73 20 73 71 6c 69 74  hese cases sqlit
10fc3 65 33 4f 73 52 65 61 64 28 29 20 77 69 6c 6c 20  e3OsRead() will 
10fc4 72 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 2c  return an error,
10fc5 20 74 6f 20 77 68 69 63 68 20 74 68 65 20 63 6f   to which the co
10fc6 72 72 65 63 74 20 0a 2a 2a 20 72 65 73 70 6f 6e  rrect .** respon
10fc7 73 65 20 69 73 20 74 6f 20 7a 65 72 6f 20 74 68  se is to zero th
10fc8 65 20 6d 65 6d 6f 72 79 20 61 74 20 70 44 65 73  e memory at pDes
10fc9 74 20 61 6e 64 20 63 6f 6e 74 69 6e 75 65 2e 20  t and continue. 
10fca 20 41 20 72 65 61 6c 20 49 4f 20 65 72 72 6f 72   A real IO error
10fcb 20 0a 2a 2a 20 77 69 6c 6c 20 70 72 65 73 75 6d   .** will presum
10fcc 61 62 6c 79 20 72 65 63 75 72 20 61 6e 64 20 62  ably recur and b
10fcd 65 20 70 69 63 6b 65 64 20 75 70 20 6c 61 74 65  e picked up late
10fce 72 20 28 54 6f 64 6f 3a 20 54 68 69 6e 6b 20 61  r (Todo: Think a
10fcf 62 6f 75 74 20 74 68 69 73 29 2e 0a 2a 2f 0a 53  bout this)..*/.S
10fd0 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
10fd1 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  t sqlite3PagerRe
10fd2 61 64 46 69 6c 65 68 65 61 64 65 72 28 50 61 67  adFileheader(Pag
10fd3 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
10fd4 4e 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  N, unsigned char
10fd5 20 2a 70 44 65 73 74 29 7b 0a 20 20 69 6e 74 20   *pDest){.  int 
10fd6 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
10fd7 20 20 6d 65 6d 73 65 74 28 70 44 65 73 74 2c 20    memset(pDest, 
10fd8 30 2c 20 4e 29 3b 0a 20 20 61 73 73 65 72 74 28  0, N);.  assert(
10fd9 70 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74  pPager->fd->pMet
10fda 68 6f 64 73 7c 7c 70 50 61 67 65 72 2d 3e 74 65  hods||pPager->te
10fdb 6d 70 46 69 6c 65 29 3b 0a 20 20 69 66 28 20 70  mpFile);.  if( p
10fdc 50 61 67 65 72 2d 3e 66 64 2d 3e 70 4d 65 74 68  Pager->fd->pMeth
10fdd 6f 64 73 20 29 7b 0a 20 20 20 20 49 4f 54 52 41  ods ){.    IOTRA
10fde 43 45 28 28 22 44 42 48 44 52 20 25 70 20 30 20  CE(("DBHDR %p 0 
10fdf 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20 4e  %d\n", pPager, N
10fe0 29 29 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  )).    rc = sqli
10fe1 74 65 33 4f 73 52 65 61 64 28 70 50 61 67 65 72  te3OsRead(pPager
10fe2 2d 3e 66 64 2c 20 70 44 65 73 74 2c 20 4e 2c 20  ->fd, pDest, N, 
10fe3 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  0);.    if( rc==
10fe4 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f 53 48 4f  SQLITE_IOERR_SHO
10fe5 52 54 5f 52 45 41 44 20 29 7b 0a 20 20 20 20 20  RT_READ ){.     
10fe6 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
10fe7 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
10fe8 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
10fe9 20 52 65 74 75 72 6e 20 74 68 65 20 74 6f 74 61   Return the tota
10fea 6c 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  l number of page
10feb 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66 69  s in the disk fi
10fec 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
10fed 74 68 0a 2a 2a 20 70 50 61 67 65 72 2e 20 0a 2a  th.** pPager. .*
10fee 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50 45 4e 44  *.** If the PEND
10fef 49 4e 47 5f 42 59 54 45 20 6c 69 65 73 20 6f 6e  ING_BYTE lies on
10ff0 20 74 68 65 20 70 61 67 65 20 64 69 72 65 63 74   the page direct
10ff1 6c 79 20 61 66 74 65 72 20 74 68 65 20 65 6e 64  ly after the end
10ff2 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69 6c 65 2c   of the.** file,
10ff3 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72 20 74   then consider t
10ff4 68 69 73 20 70 61 67 65 20 70 61 72 74 20 6f 66  his page part of
10ff5 20 74 68 65 20 66 69 6c 65 20 74 6f 6f 2e 20 46   the file too. F
10ff6 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69 66 0a 2a  or example, if.*
10ff7 2a 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 69  * PENDING_BYTE i
10ff8 73 20 62 79 74 65 20 34 30 39 36 20 28 74 68 65  s byte 4096 (the
10ff9 20 66 69 72 73 74 20 62 79 74 65 20 6f 66 20 70   first byte of p
10ffa 61 67 65 20 35 29 20 61 6e 64 20 74 68 65 20 73  age 5) and the s
10ffb 69 7a 65 20 6f 66 20 74 68 65 0a 2a 2a 20 66 69  ize of the.** fi
10ffc 6c 65 20 69 73 20 34 30 39 36 20 62 79 74 65 73  le is 4096 bytes
10ffd 2c 20 35 20 69 73 20 72 65 74 75 72 6e 65 64 20  , 5 is returned 
10ffe 69 6e 73 74 65 61 64 20 6f 66 20 34 2e 0a 2a 2f  instead of 4..*/
10fff 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
11000 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72  int sqlite3Pager
11001 50 61 67 65 63 6f 75 6e 74 28 50 61 67 65 72 20  Pagecount(Pager 
11002 2a 70 50 61 67 65 72 2c 20 69 6e 74 20 2a 70 6e  *pPager, int *pn
11003 50 61 67 65 29 7b 0a 20 20 69 36 34 20 6e 20 3d  Page){.  i64 n =
11004 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20   0;.  int rc;.  
11005 61 73 73 65 72 74 28 20 70 50 61 67 65 72 21 3d  assert( pPager!=
11006 30 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65  0 );.  if( pPage
11007 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20  r->errCode ){.  
11008 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
11009 72 72 43 6f 64 65 3b 0a 20 20 20 20 72 65 74 75  rrCode;.    retu
1100a 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66 28  rn rc;.  }.  if(
1100b 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56   pPager->dbSizeV
1100c 61 6c 69 64 20 29 7b 0a 20 20 20 20 6e 20 3d 20  alid ){.    n = 
1100d 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a  pPager->dbSize;.
1100e 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 61    } else {.    a
1100f 73 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64  ssert(pPager->fd
11010 2d 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67  ->pMethods||pPag
11011 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20  er->tempFile);. 
11012 20 20 20 69 66 28 20 28 70 50 61 67 65 72 2d 3e     if( (pPager->
11013 66 64 2d 3e 70 4d 65 74 68 6f 64 73 29 0a 20 20  fd->pMethods).  
11014 20 20 20 26 26 20 28 72 63 20 3d 20 73 71 6c 69     && (rc = sqli
11015 74 65 33 4f 73 46 69 6c 65 53 69 7a 65 28 70 50  te3OsFileSize(pP
11016 61 67 65 72 2d 3e 66 64 2c 20 26 6e 29 29 21 3d  ager->fd, &n))!=
11017 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
11018 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70     pager_error(p
11019 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20  Pager, rc);.    
1101a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
1101b 20 7d 0a 20 20 20 20 69 66 28 20 6e 3e 30 20 26   }.    if( n>0 &
1101c 26 20 6e 3c 70 50 61 67 65 72 2d 3e 70 61 67 65  & n<pPager->page
1101d 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 6e 20  Size ){.      n 
1101e 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
1101f 20 20 20 20 20 20 6e 20 2f 3d 20 70 50 61 67 65        n /= pPage
11020 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  r->pageSize;.   
11021 20 7d 0a 20 20 20 20 69 66 28 20 70 50 61 67 65   }.    if( pPage
11022 72 2d 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f  r->state!=PAGER_
11023 55 4e 4c 4f 43 4b 20 29 7b 0a 20 20 20 20 20 20  UNLOCK ){.      
11024 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 20 3d  pPager->dbSize =
11025 20 6e 3b 0a 20 20 20 20 20 20 70 50 61 67 65 72   n;.      pPager
11026 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3d 20  ->dbSizeValid = 
11027 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69  1;.    }.  }.  i
11028 66 28 20 6e 3d 3d 28 50 45 4e 44 49 4e 47 5f 42  f( n==(PENDING_B
11029 59 54 45 2f 70 50 61 67 65 72 2d 3e 70 61 67 65  YTE/pPager->page
1102a 53 69 7a 65 29 20 29 7b 0a 20 20 20 20 6e 2b 2b  Size) ){.    n++
1102b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 3e 70 50  ;.  }.  if( n>pP
1102c 61 67 65 72 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a  ager->mxPgno ){.
1102d 20 20 20 20 70 50 61 67 65 72 2d 3e 6d 78 50 67      pPager->mxPg
1102e 6e 6f 20 3d 20 6e 3b 0a 20 20 7d 0a 20 20 69 66  no = n;.  }.  if
1102f 28 20 70 6e 50 61 67 65 20 29 7b 0a 20 20 20 20  ( pnPage ){.    
11030 2a 70 6e 50 61 67 65 20 3d 20 6e 3b 0a 20 20 7d  *pnPage = n;.  }
11031 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
11032 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f  _OK;.}../*.** Fo
11033 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
11034 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  n.*/.static int 
11035 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 50 61 67 65  syncJournal(Page
11036 72 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  r*);../*.** This
11037 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64   routine is used
11038 20 74 6f 20 74 72 75 6e 63 61 74 65 20 74 68 65   to truncate the
11039 20 63 61 63 68 65 20 77 68 65 6e 20 61 20 64 61   cache when a da
1103a 74 61 62 61 73 65 0a 2a 2a 20 69 73 20 74 72 75  tabase.** is tru
1103b 6e 63 61 74 65 64 2e 20 20 44 72 6f 70 20 66 72  ncated.  Drop fr
1103c 6f 6d 20 74 68 65 20 63 61 63 68 65 20 61 6c 6c  om the cache all
1103d 20 70 61 67 65 73 20 77 68 6f 73 65 20 70 67 6e   pages whose pgn
1103e 6f 20 69 73 0a 2a 2a 20 6c 61 72 67 65 72 20 74  o is.** larger t
1103f 68 61 6e 20 70 50 61 67 65 72 2d 3e 64 62 53 69  han pPager->dbSi
11040 7a 65 20 61 6e 64 20 69 73 20 75 6e 72 65 66 65  ze and is unrefe
11041 72 65 6e 63 65 64 2e 0a 2a 2a 0a 2a 2a 20 52 65  renced..**.** Re
11042 66 65 72 65 6e 63 65 64 20 70 61 67 65 73 20 6c  ferenced pages l
11043 61 72 67 65 72 20 74 68 61 6e 20 70 50 61 67 65  arger than pPage
11044 72 2d 3e 64 62 53 69 7a 65 20 61 72 65 20 7a 65  r->dbSize are ze
11045 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 63 74 75  roed..**.** Actu
11046 61 6c 6c 79 2c 20 61 74 20 74 68 65 20 70 6f 69  ally, at the poi
11047 6e 74 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nt this routine 
11048 69 73 20 63 61 6c 6c 65 64 2c 20 69 74 20 77 6f  is called, it wo
11049 75 6c 64 20 62 65 0a 2a 2a 20 61 6e 20 65 72 72  uld be.** an err
1104a 6f 72 20 74 6f 20 68 61 76 65 20 61 20 72 65 66  or to have a ref
1104b 65 72 65 6e 63 65 64 20 70 61 67 65 2e 20 20 42  erenced page.  B
1104c 75 74 20 72 61 74 68 65 72 20 74 68 61 6e 20 64  ut rather than d
1104d 65 6c 65 74 65 0a 2a 2a 20 74 68 61 74 20 70 61  elete.** that pa
1104e 67 65 20 61 6e 64 20 67 75 61 72 61 6e 74 65 65  ge and guarantee
1104f 20 61 20 73 75 62 73 65 71 75 65 6e 74 20 73 65   a subsequent se
11050 67 66 61 75 6c 74 2c 20 69 74 20 73 65 65 6d 73  gfault, it seems
11051 20 62 65 74 74 65 72 0a 2a 2a 20 74 6f 20 7a 65   better.** to ze
11052 72 6f 20 69 74 20 61 6e 64 20 68 6f 70 65 20 74  ro it and hope t
11053 68 61 74 20 77 65 20 65 72 72 6f 72 20 6f 75 74  hat we error out
11054 20 73 61 6e 65 6c 79 2e 0a 2a 2f 0a 73 74 61 74   sanely..*/.stat
11055 69 63 20 76 6f 69 64 20 70 61 67 65 72 5f 74 72  ic void pager_tr
11056 75 6e 63 61 74 65 5f 63 61 63 68 65 28 50 61 67  uncate_cache(Pag
11057 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20 73  er *pPager){.  s
11058 71 6c 69 74 65 33 50 63 61 63 68 65 54 72 75 6e  qlite3PcacheTrun
11059 63 61 74 65 28 70 50 61 67 65 72 2d 3e 70 50 43  cate(pPager->pPC
1105a 61 63 68 65 2c 20 70 50 61 67 65 72 2d 3e 64 62  ache, pPager->db
1105b 53 69 7a 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Size);.}../*.** 
1105c 54 72 79 20 74 6f 20 6f 62 74 61 69 6e 20 61 20  Try to obtain a 
1105d 6c 6f 63 6b 20 6f 6e 20 61 20 66 69 6c 65 2e 20  lock on a file. 
1105e 20 49 6e 76 6f 6b 65 20 74 68 65 20 62 75 73 79   Invoke the busy
1105f 20 63 61 6c 6c 62 61 63 6b 20 69 66 20 74 68 65   callback if the
11060 20 6c 6f 63 6b 0a 2a 2a 20 69 73 20 63 75 72 72   lock.** is curr
11061 65 6e 74 6c 79 20 6e 6f 74 20 61 76 61 69 6c 61  ently not availa
11062 62 6c 65 2e 20 20 52 65 70 65 61 74 20 75 6e 74  ble.  Repeat unt
11063 69 6c 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c  il the busy call
11064 62 61 63 6b 20 72 65 74 75 72 6e 73 0a 2a 2a 20  back returns.** 
11065 66 61 6c 73 65 20 6f 72 20 75 6e 74 69 6c 20 74  false or until t
11066 68 65 20 6c 6f 63 6b 20 73 75 63 63 65 65 64 73  he lock succeeds
11067 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53  ..**.** Return S
11068 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63  QLITE_OK on succ
11069 65 73 73 20 61 6e 64 20 61 6e 20 65 72 72 6f 72  ess and an error
1106a 20 63 6f 64 65 20 69 66 20 77 65 20 63 61 6e 6e   code if we cann
1106b 6f 74 20 6f 62 74 61 69 6e 0a 2a 2a 20 74 68 65  ot obtain.** the
1106c 20 6c 6f 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63   lock..*/.static
1106d 20 69 6e 74 20 70 61 67 65 72 5f 77 61 69 74 5f   int pager_wait_
1106e 6f 6e 5f 6c 6f 63 6b 28 50 61 67 65 72 20 2a 70  on_lock(Pager *p
1106f 50 61 67 65 72 2c 20 69 6e 74 20 6c 6f 63 6b 74  Pager, int lockt
11070 79 70 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ype){.  int rc;.
11071 0a 20 20 2f 2a 20 54 68 65 20 4f 53 20 6c 6f 63  .  /* The OS loc
11072 6b 20 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65  k values must be
11073 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
11074 20 50 61 67 65 72 20 6c 6f 63 6b 20 76 61 6c 75   Pager lock valu
11075 65 73 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  es */.  assert( 
11076 50 41 47 45 52 5f 53 48 41 52 45 44 3d 3d 53 48  PAGER_SHARED==SH
11077 41 52 45 44 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61  ARED_LOCK );.  a
11078 73 73 65 72 74 28 20 50 41 47 45 52 5f 52 45 53  ssert( PAGER_RES
11079 45 52 56 45 44 3d 3d 52 45 53 45 52 56 45 44 5f  ERVED==RESERVED_
1107a 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65 72 74  LOCK );.  assert
1107b 28 20 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56  ( PAGER_EXCLUSIV
1107c 45 3d 3d 45 58 43 4c 55 53 49 56 45 5f 4c 4f 43  E==EXCLUSIVE_LOC
1107d 4b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  K );..  /* If th
1107e 65 20 66 69 6c 65 20 69 73 20 63 75 72 72 65 6e  e file is curren
1107f 74 6c 79 20 75 6e 6c 6f 63 6b 65 64 20 74 68 65  tly unlocked the
11080 6e 20 74 68 65 20 73 69 7a 65 20 6d 75 73 74 20  n the size must 
11081 62 65 20 75 6e 6b 6e 6f 77 6e 20 2a 2f 0a 20 20  be unknown */.  
11082 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
11083 73 74 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41  state>=PAGER_SHA
11084 52 45 44 20 7c 7c 20 70 50 61 67 65 72 2d 3e 64  RED || pPager->d
11085 62 53 69 7a 65 56 61 6c 69 64 3d 3d 30 20 29 3b  bSizeValid==0 );
11086 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
11087 73 74 61 74 65 3e 3d 6c 6f 63 6b 74 79 70 65 20  state>=locktype 
11088 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
11089 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  TE_OK;.  }else{.
1108a 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 72      do {.      r
1108b 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63  c = sqlite3OsLoc
1108c 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c 20 6c 6f  k(pPager->fd, lo
1108d 63 6b 74 79 70 65 29 3b 0a 20 20 20 20 7d 77 68  cktype);.    }wh
1108e 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
1108f 42 55 53 59 20 26 26 20 70 50 61 67 65 72 2d 3e  BUSY && pPager->
11090 78 42 75 73 79 48 61 6e 64 6c 65 72 28 70 50 61  xBusyHandler(pPa
11091 67 65 72 2d 3e 70 42 75 73 79 48 61 6e 64 6c 65  ger->pBusyHandle
11092 72 41 72 67 29 20 29 3b 0a 20 20 20 20 69 66 28  rArg) );.    if(
11093 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
11094 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
11095 73 74 61 74 65 20 3d 20 6c 6f 63 6b 74 79 70 65  state = locktype
11096 3b 0a 20 20 20 20 20 20 49 4f 54 52 41 43 45 28  ;.      IOTRACE(
11097 28 22 4c 4f 43 4b 20 25 70 20 25 64 5c 6e 22 2c  ("LOCK %p %d\n",
11098 20 70 50 61 67 65 72 2c 20 6c 6f 63 6b 74 79 70   pPager, locktyp
11099 65 29 29 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  e)).    }.  }.  
1109a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1109b 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20 74 68 65  .** Truncate the
1109c 20 66 69 6c 65 20 74 6f 20 74 68 65 20 6e 75 6d   file to the num
1109d 62 65 72 20 6f 66 20 70 61 67 65 73 20 73 70 65  ber of pages spe
1109e 63 69 66 69 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  cified..*/.SQLIT
1109f 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
110a0 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
110a1 74 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  te(Pager *pPager
110a2 2c 20 50 67 6e 6f 20 6e 50 61 67 65 29 7b 0a 20  , Pgno nPage){. 
110a3 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
110a4 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70  _OK;.  assert( p
110a5 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41  Pager->state>=PA
110a6 47 45 52 5f 53 48 41 52 45 44 20 29 3b 0a 0a 20  GER_SHARED );.. 
110a7 20 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67   sqlite3PagerPag
110a8 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c 20 30  ecount(pPager, 0
110a9 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
110aa 3e 65 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20  >errCode ){.    
110ab 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72  rc = pPager->err
110ac 43 6f 64 65 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Code;.  }else if
110ad 28 20 6e 50 61 67 65 3c 70 50 61 67 65 72 2d 3e  ( nPage<pPager->
110ae 64 62 53 69 7a 65 20 29 7b 0a 20 20 20 20 72 63  dbSize ){.    rc
110af 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
110b0 50 61 67 65 72 29 3b 0a 20 20 20 20 69 66 28 20  Pager);.    if( 
110b1 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
110b2 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61 6e  .      /* Get an
110b3 20 65 78 63 6c 75 73 69 76 65 20 6c 6f 63 6b 20   exclusive lock 
110b4 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
110b5 62 65 66 6f 72 65 20 74 72 75 6e 63 61 74 69 6e  before truncatin
110b6 67 2e 20 2a 2f 0a 20 20 20 20 20 20 72 63 20 3d  g. */.      rc =
110b7 20 70 61 67 65 72 5f 77 61 69 74 5f 6f 6e 5f 6c   pager_wait_on_l
110b8 6f 63 6b 28 70 50 61 67 65 72 2c 20 45 58 43 4c  ock(pPager, EXCL
110b9 55 53 49 56 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20  USIVE_LOCK);.   
110ba 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53   }.    if( rc==S
110bb 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
110bc 20 20 72 63 20 3d 20 70 61 67 65 72 5f 74 72 75    rc = pager_tru
110bd 6e 63 61 74 65 28 70 50 61 67 65 72 2c 20 6e 50  ncate(pPager, nP
110be 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
110bf 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
110c0 0a 2f 2a 0a 2a 2a 20 53 68 75 74 64 6f 77 6e 20  ./*.** Shutdown 
110c1 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e 20  the page cache. 
110c2 20 46 72 65 65 20 61 6c 6c 20 6d 65 6d 6f 72 79   Free all memory
110c3 20 61 6e 64 20 63 6c 6f 73 65 20 61 6c 6c 20 66   and close all f
110c4 69 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  iles..**.** If a
110c5 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
110c6 20 69 6e 20 70 72 6f 67 72 65 73 73 20 77 68 65   in progress whe
110c7 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  n this routine i
110c8 73 20 63 61 6c 6c 65 64 2c 20 74 68 61 74 0a 2a  s called, that.*
110c9 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
110ca 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 20 41   rolled back.  A
110cb 6c 6c 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70  ll outstanding p
110cc 61 67 65 73 20 61 72 65 20 69 6e 76 61 6c 69 64  ages are invalid
110cd 61 74 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65 69  ated.** and thei
110ce 72 20 6d 65 6d 6f 72 79 20 69 73 20 66 72 65 65  r memory is free
110cf 64 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20  d.  Any attempt 
110d0 74 6f 20 75 73 65 20 61 20 70 61 67 65 20 61 73  to use a page as
110d1 73 6f 63 69 61 74 65 64 0a 2a 2a 20 77 69 74 68  sociated.** with
110d2 20 74 68 69 73 20 70 61 67 65 20 63 61 63 68 65   this page cache
110d3 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63   after this func
110d4 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 77 69 6c  tion returns wil
110d5 6c 20 6c 69 6b 65 6c 79 0a 2a 2a 20 72 65 73 75  l likely.** resu
110d6 6c 74 20 69 6e 20 61 20 63 6f 72 65 64 75 6d 70  lt in a coredump
110d7 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ..**.** This fun
110d8 63 74 69 6f 6e 20 61 6c 77 61 79 73 20 73 75 63  ction always suc
110d9 63 65 65 64 73 2e 20 49 66 20 61 20 74 72 61 6e  ceeds. If a tran
110da 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76  saction is activ
110db 65 20 61 6e 20 61 74 74 65 6d 70 74 0a 2a 2a 20  e an attempt.** 
110dc 69 73 20 6d 61 64 65 20 74 6f 20 72 6f 6c 6c 20  is made to roll 
110dd 69 74 20 62 61 63 6b 2e 20 49 66 20 61 6e 20 65  it back. If an e
110de 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
110df 6e 67 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  ng the rollback 
110e0 0a 2a 2a 20 61 20 68 6f 74 20 6a 6f 75 72 6e 61  .** a hot journa
110e1 6c 20 6d 61 79 20 62 65 20 6c 65 66 74 20 69 6e  l may be left in
110e2 20 74 68 65 20 66 69 6c 65 73 79 73 74 65 6d 20   the filesystem 
110e3 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 69 73 20  but no error is 
110e4 72 65 74 75 72 6e 65 64 0a 2a 2a 20 74 6f 20 74  returned.** to t
110e5 68 65 20 63 61 6c 6c 65 72 2e 0a 2a 2f 0a 53 51  he caller..*/.SQ
110e6 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
110e7 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
110e8 73 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  se(Pager *pPager
110e9 29 7b 0a 0a 20 20 64 69 73 61 62 6c 65 5f 73 69  ){..  disable_si
110ea 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f 72  mulated_io_error
110eb 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 65  s();.  sqlite3Be
110ec 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
110ed 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 65 72 72  );.  pPager->err
110ee 43 6f 64 65 20 3d 20 30 3b 0a 20 20 70 50 61 67  Code = 0;.  pPag
110ef 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d 6f 64  er->exclusiveMod
110f0 65 20 3d 20 30 3b 0a 20 20 70 61 67 65 72 5f 72  e = 0;.  pager_r
110f1 65 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20  eset(pPager);.  
110f2 69 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20  if( !MEMDB ){.  
110f3 20 20 70 61 67 65 72 55 6e 6c 6f 63 6b 41 6e 64    pagerUnlockAnd
110f4 52 6f 6c 6c 62 61 63 6b 28 70 50 61 67 65 72 29  Rollback(pPager)
110f5 3b 0a 20 20 7d 0a 20 20 65 6e 61 62 6c 65 5f 73  ;.  }.  enable_s
110f6 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65 72 72 6f  imulated_io_erro
110f7 72 73 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  rs();.  sqlite3E
110f8 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29  ndBenignMalloc()
110f9 3b 0a 20 20 50 41 47 45 52 54 52 41 43 45 32 28  ;.  PAGERTRACE2(
110fa 22 43 4c 4f 53 45 20 25 64 5c 6e 22 2c 20 50 41  "CLOSE %d\n", PA
110fb 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a  GERID(pPager));.
110fc 20 20 49 4f 54 52 41 43 45 28 28 22 43 4c 4f 53    IOTRACE(("CLOS
110fd 45 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29  E %p\n", pPager)
110fe 29 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ).  if( pPager->
110ff 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20  journalOpen ){. 
11100 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73     sqlite3OsClos
11101 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a  e(pPager->jfd);.
11102 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 69 74    }.  sqlite3Bit
11103 76 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65  vecDestroy(pPage
11104 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 29 3b 0a  r->pInJournal);.
11105 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 44    sqlite3BitvecD
11106 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d 3e 70  estroy(pPager->p
11107 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 29 3b  AlwaysRollback);
11108 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
11109 74 6d 74 4f 70 65 6e 20 29 7b 0a 20 20 20 20 73  tmtOpen ){.    s
1110a 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 28 70 50  qlite3OsClose(pP
1110b 61 67 65 72 2d 3e 73 74 66 64 29 3b 0a 20 20 7d  ager->stfd);.  }
1110c 0a 20 20 73 71 6c 69 74 65 33 4f 73 43 6c 6f 73  .  sqlite3OsClos
1110d 65 28 70 50 61 67 65 72 2d 3e 66 64 29 3b 0a 20  e(pPager->fd);. 
1110e 20 2f 2a 20 54 65 6d 70 20 66 69 6c 65 73 20 61   /* Temp files a
1110f 72 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  re automatically
11110 20 64 65 6c 65 74 65 64 20 62 79 20 74 68 65 20   deleted by the 
11111 4f 53 0a 20 20 2a 2a 20 69 66 28 20 70 50 61 67  OS.  ** if( pPag
11112 65 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a  er->tempFile ){.
11113 20 20 2a 2a 20 20 20 73 71 6c 69 74 65 33 4f 73    **   sqlite3Os
11114 44 65 6c 65 74 65 28 70 50 61 67 65 72 2d 3e 7a  Delete(pPager->z
11115 46 69 6c 65 6e 61 6d 65 29 3b 0a 20 20 2a 2a 20  Filename);.  ** 
11116 7d 0a 20 20 2a 2f 0a 0a 20 20 73 71 6c 69 74 65  }.  */..  sqlite
11117 33 50 61 67 65 46 72 65 65 28 70 50 61 67 65 72  3PageFree(pPager
11118 2d 3e 70 54 6d 70 53 70 61 63 65 29 3b 0a 20 20  ->pTmpSpace);.  
11119 73 71 6c 69 74 65 33 50 63 61 63 68 65 43 6c 6f  sqlite3PcacheClo
1111a 73 65 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  se(pPager->pPCac
1111b 68 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  he);.  sqlite3_f
1111c 72 65 65 28 70 50 61 67 65 72 29 3b 0a 20 20 72  ree(pPager);.  r
1111d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1111e 0a 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .}..#if !defined
1111f 28 4e 44 45 42 55 47 29 20 7c 7c 20 64 65 66 69  (NDEBUG) || defi
11120 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
11121 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
11122 65 20 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f  e page number fo
11123 72 20 74 68 65 20 67 69 76 65 6e 20 70 61 67 65  r the given page
11124 20 64 61 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45   data..*/.SQLITE
11125 5f 50 52 49 56 41 54 45 20 50 67 6e 6f 20 73 71  _PRIVATE Pgno sq
11126 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 6e 75  lite3PagerPagenu
11127 6d 62 65 72 28 44 62 50 61 67 65 20 2a 70 29 7b  mber(DbPage *p){
11128 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 67 6e  .  return p->pgn
11129 6f 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  o;.}.#endif../*.
1112a 2a 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  ** Increment the
1112b 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74   reference count
1112c 20 66 6f 72 20 61 20 70 61 67 65 2e 20 20 54 68   for a page.  Th
1112d 65 20 69 6e 70 75 74 20 70 6f 69 6e 74 65 72 20  e input pointer 
1112e 69 73 0a 2a 2a 20 61 20 72 65 66 65 72 65 6e 63  is.** a referenc
1112f 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 64 61  e to the page da
11130 74 61 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ta..*/.SQLITE_PR
11131 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
11132 33 50 61 67 65 72 52 65 66 28 44 62 50 61 67 65  3PagerRef(DbPage
11133 20 2a 70 50 67 29 7b 0a 20 20 73 71 6c 69 74 65   *pPg){.  sqlite
11134 33 50 63 61 63 68 65 52 65 66 28 70 50 67 29 3b  3PcacheRef(pPg);
11135 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
11136 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 79  _OK;.}../*.** Sy
11137 6e 63 20 74 68 65 20 6a 6f 75 72 6e 61 6c 2e 20  nc the journal. 
11138 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c   In other words,
11139 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 74   make sure all t
1113a 68 65 20 70 61 67 65 73 20 74 68 61 74 20 68 61  he pages that ha
1113b 76 65 0a 2a 2a 20 62 65 65 6e 20 77 72 69 74 74  ve.** been writt
1113c 65 6e 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  en to the journa
1113d 6c 20 68 61 76 65 20 61 63 74 75 61 6c 6c 79 20  l have actually 
1113e 72 65 61 63 68 65 64 20 74 68 65 20 73 75 72 66  reached the surf
1113f 61 63 65 20 6f 66 20 74 68 65 0a 2a 2a 20 64 69  ace of the.** di
11140 73 6b 2e 20 20 49 74 20 69 73 20 6e 6f 74 20 73  sk.  It is not s
11141 61 66 65 20 74 6f 20 6d 6f 64 69 66 79 20 74 68  afe to modify th
11142 65 20 6f 72 69 67 69 6e 61 6c 20 64 61 74 61 62  e original datab
11143 61 73 65 20 66 69 6c 65 20 75 6e 74 69 6c 20 61  ase file until a
11144 66 74 65 72 0a 2a 2a 20 74 68 65 20 6a 6f 75 72  fter.** the jour
11145 6e 61 6c 20 68 61 73 20 62 65 65 6e 20 73 79 6e  nal has been syn
11146 63 65 64 2e 20 20 49 66 20 74 68 65 20 6f 72 69  ced.  If the ori
11147 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 69  ginal database i
11148 73 20 6d 6f 64 69 66 69 65 64 20 62 65 66 6f 72  s modified befor
11149 65 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c  e.** the journal
1114a 20 69 73 20 73 79 6e 63 65 64 20 61 6e 64 20 61   is synced and a
1114b 20 70 6f 77 65 72 20 66 61 69 6c 75 72 65 20 6f   power failure o
1114c 63 63 75 72 73 2c 20 74 68 65 20 75 6e 73 79 6e  ccurs, the unsyn
1114d 63 65 64 20 6a 6f 75 72 6e 61 6c 0a 2a 2a 20 64  ced journal.** d
1114e 61 74 61 20 77 6f 75 6c 64 20 62 65 20 6c 6f 73  ata would be los
1114f 74 20 61 6e 64 20 77 65 20 77 6f 75 6c 64 20 62  t and we would b
11150 65 20 75 6e 61 62 6c 65 20 74 6f 20 63 6f 6d 70  e unable to comp
11151 6c 65 74 65 6c 79 20 72 6f 6c 6c 62 61 63 6b 20  letely rollback 
11152 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
11153 63 68 61 6e 67 65 73 2e 20 20 44 61 74 61 62 61  changes.  Databa
11154 73 65 20 63 6f 72 72 75 70 74 69 6f 6e 20 77 6f  se corruption wo
11155 75 6c 64 20 6f 63 63 75 72 2e 0a 2a 2a 20 0a 2a  uld occur..** .*
11156 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
11157 6c 73 6f 20 75 70 64 61 74 65 73 20 74 68 65 20  lso updates the 
11158 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74 68  nRec field in th
11159 65 20 68 65 61 64 65 72 20 6f 66 20 74 68 65 20  e header of the 
1115a 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 28 53 65 65  journal..** (See
1115b 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20 74 68 65   comments on the
1115c 20 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28   pager_playback(
1115d 29 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61 64  ) routine for ad
1115e 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61  ditional informa
1115f 74 69 6f 6e 2e 29 0a 2a 2a 20 49 66 20 74 68 65  tion.).** If the
11160 20 73 79 6e 63 20 6d 6f 64 65 20 69 73 20 46 55   sync mode is FU
11161 4c 4c 2c 20 74 77 6f 20 73 79 6e 63 73 20 77 69  LL, two syncs wi
11162 6c 6c 20 6f 63 63 75 72 2e 20 20 46 69 72 73 74  ll occur.  First
11163 20 74 68 65 20 77 68 6f 6c 65 20 6a 6f 75 72 6e   the whole journ
11164 61 6c 0a 2a 2a 20 69 73 20 73 79 6e 63 65 64 2c  al.** is synced,
11165 20 74 68 65 6e 20 74 68 65 20 6e 52 65 63 20 66   then the nRec f
11166 69 65 6c 64 20 69 73 20 75 70 64 61 74 65 64 2c  ield is updated,
11167 20 74 68 65 6e 20 61 20 73 65 63 6f 6e 64 20 73   then a second s
11168 79 6e 63 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a  ync occurs..**.*
11169 2a 20 46 6f 72 20 74 65 6d 70 6f 72 61 72 79 20  * For temporary 
1116a 64 61 74 61 62 61 73 65 73 2c 20 77 65 20 64 6f  databases, we do
1116b 20 6e 6f 74 20 63 61 72 65 20 69 66 20 77 65 20   not care if we 
1116c 61 72 65 20 61 62 6c 65 20 74 6f 20 72 6f 6c 6c  are able to roll
1116d 62 61 63 6b 0a 2a 2a 20 61 66 74 65 72 20 61 20  back.** after a 
1116e 70 6f 77 65 72 20 66 61 69 6c 75 72 65 2c 20 73  power failure, s
1116f 6f 20 6e 6f 20 73 79 6e 63 20 6f 63 63 75 72 73  o no sync occurs
11170 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 49  ..**.** If the I
11171 4f 43 41 50 5f 53 45 51 55 45 4e 54 49 41 4c 20  OCAP_SEQUENTIAL 
11172 66 6c 61 67 20 69 73 20 73 65 74 20 66 6f 72 20  flag is set for 
11173 74 68 65 20 70 65 72 73 69 73 74 65 6e 74 20 6d  the persistent m
11174 65 64 69 61 20 6f 6e 20 77 68 69 63 68 0a 2a 2a  edia on which.**
11175 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
11176 20 73 74 6f 72 65 64 2c 20 74 68 65 6e 20 4f 73   stored, then Os
11177 53 79 6e 63 28 29 20 69 73 20 6e 65 76 65 72 20  Sync() is never 
11178 63 61 6c 6c 65 64 20 6f 6e 20 74 68 65 20 6a 6f  called on the jo
11179 75 72 6e 61 6c 0a 2a 2a 20 66 69 6c 65 2e 20 49  urnal.** file. I
1117a 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20  n this case all 
1117b 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65 64  that is required
1117c 20 69 73 20 74 6f 20 75 70 64 61 74 65 20 74 68   is to update th
1117d 65 20 6e 52 65 63 20 66 69 65 6c 64 20 69 6e 0a  e nRec field in.
1117e 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  ** the journal h
1117f 65 61 64 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  eader..**.** Thi
11180 73 20 72 6f 75 74 69 6e 65 20 63 6c 65 61 72 73  s routine clears
11181 20 74 68 65 20 6e 65 65 64 53 79 6e 63 20 66 69   the needSync fi
11182 65 6c 64 20 6f 66 20 65 76 65 72 79 20 70 61 67  eld of every pag
11183 65 20 63 75 72 72 65 6e 74 20 68 65 6c 64 20 69  e current held i
11184 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  n.** memory..*/.
11185 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 63 4a  static int syncJ
11186 6f 75 72 6e 61 6c 28 50 61 67 65 72 20 2a 70 50  ournal(Pager *pP
11187 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20  ager){.  int rc 
11188 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  = SQLITE_OK;..  
11189 2f 2a 20 53 79 6e 63 20 74 68 65 20 6a 6f 75 72  /* Sync the jour
1118a 6e 61 6c 20 62 65 66 6f 72 65 20 6d 6f 64 69 66  nal before modif
1118b 79 69 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61  ying the main da
1118c 74 61 62 61 73 65 0a 20 20 2a 2a 20 28 61 73 73  tabase.  ** (ass
1118d 75 6d 69 6e 67 20 74 68 65 72 65 20 69 73 20 61  uming there is a
1118e 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20 69 74 20   journal and it 
1118f 6e 65 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63  needs to be sync
11190 65 64 2e 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ed.).  */.  if( 
11191 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63  pPager->needSync
11192 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
11193 21 70 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c  !pPager->tempFil
11194 65 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  e );.    if( pPa
11195 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
11196 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
11197 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 7b 0a 20 20  ODE_MEMORY ){.  
11198 20 20 20 20 69 6e 74 20 69 44 63 20 3d 20 73 71      int iDc = sq
11199 6c 69 74 65 33 4f 73 44 65 76 69 63 65 43 68 61  lite3OsDeviceCha
1119a 72 61 63 74 65 72 69 73 74 69 63 73 28 70 50 61  racteristics(pPa
1119b 67 65 72 2d 3e 66 64 29 3b 0a 20 20 20 20 20 20  ger->fd);.      
1119c 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
1119d 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a 0a  journalOpen );..
1119e 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44        if( 0==(iD
1119f 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
111a0 41 46 45 5f 41 50 50 45 4e 44 29 20 29 7b 0a 20  AFE_APPEND) ){. 
111a1 20 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20         /* Write 
111a2 74 68 65 20 6e 52 65 63 20 76 61 6c 75 65 20 69  the nRec value i
111a3 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  nto the journal 
111a4 66 69 6c 65 20 68 65 61 64 65 72 2e 20 49 66 20  file header. If 
111a5 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 66 75  in.        ** fu
111a6 6c 6c 2d 73 79 6e 63 68 72 6f 6e 6f 75 73 20 6d  ll-synchronous m
111a7 6f 64 65 2c 20 73 79 6e 63 20 74 68 65 20 6a 6f  ode, sync the jo
111a8 75 72 6e 61 6c 20 66 69 72 73 74 2e 20 54 68 69  urnal first. Thi
111a9 73 20 65 6e 73 75 72 65 73 20 74 68 61 74 0a 20  s ensures that. 
111aa 20 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 64 61         ** all da
111ab 74 61 20 68 61 73 20 72 65 61 6c 6c 79 20 68 69  ta has really hi
111ac 74 20 74 68 65 20 64 69 73 6b 20 62 65 66 6f 72  t the disk befor
111ad 65 20 6e 52 65 63 20 69 73 20 75 70 64 61 74 65  e nRec is update
111ae 64 20 74 6f 20 6d 61 72 6b 0a 20 20 20 20 20 20  d to mark.      
111af 20 20 2a 2a 20 69 74 20 61 73 20 61 20 63 61 6e    ** it as a can
111b0 64 69 64 61 74 65 20 66 6f 72 20 72 6f 6c 6c 62  didate for rollb
111b1 61 63 6b 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  ack..        **.
111b2 20 20 20 20 20 20 20 20 2a 2a 20 54 68 69 73 20          ** This 
111b3 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20  is not required 
111b4 69 66 20 74 68 65 20 70 65 72 73 69 73 74 65 6e  if the persisten
111b5 74 20 6d 65 64 69 61 20 73 75 70 70 6f 72 74 73  t media supports
111b6 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
111b7 53 41 46 45 5f 41 50 50 45 4e 44 20 70 72 6f 70  SAFE_APPEND prop
111b8 65 72 74 79 2e 20 42 65 63 61 75 73 65 20 69 6e  erty. Because in
111b9 20 74 68 69 73 20 63 61 73 65 20 69 74 20 69 73   this case it is
111ba 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 20 0a 20   not possible . 
111bb 20 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 67 61         ** for ga
111bc 72 62 61 67 65 20 64 61 74 61 20 74 6f 20 62 65  rbage data to be
111bd 20 61 70 70 65 6e 64 65 64 20 74 6f 20 74 68 65   appended to the
111be 20 66 69 6c 65 2c 20 74 68 65 20 6e 52 65 63 20   file, the nRec 
111bf 66 69 65 6c 64 0a 20 20 20 20 20 20 20 20 2a 2a  field.        **
111c0 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 77 69   is populated wi
111c1 74 68 20 30 78 46 46 46 46 46 46 46 46 20 77 68  th 0xFFFFFFFF wh
111c2 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68  en the journal h
111c3 65 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e  eader is written
111c4 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6e 64 20  .        ** and 
111c5 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20 62  never needs to b
111c6 65 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20 20  e updated..     
111c7 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 36     */.        i6
111c8 34 20 6a 72 6e 6c 4f 66 66 3b 0a 20 20 20 20 20  4 jrnlOff;.     
111c9 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 66     if( pPager->f
111ca 75 6c 6c 53 79 6e 63 20 26 26 20 30 3d 3d 28 69  ullSync && 0==(i
111cb 44 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f  Dc&SQLITE_IOCAP_
111cc 53 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20  SEQUENTIAL) ){. 
111cd 20 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52           PAGERTR
111ce 41 43 45 32 28 22 53 59 4e 43 20 6a 6f 75 72 6e  ACE2("SYNC journ
111cf 61 6c 20 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47  al of %d\n", PAG
111d0 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20  ERID(pPager));. 
111d1 20 20 20 20 20 20 20 20 20 49 4f 54 52 41 43 45           IOTRACE
111d2 28 28 22 4a 53 59 4e 43 20 25 70 5c 6e 22 2c 20  (("JSYNC %p\n", 
111d3 70 50 61 67 65 72 29 29 0a 20 20 20 20 20 20 20  pPager)).       
111d4 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
111d5 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 6a 66  sSync(pPager->jf
111d6 64 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f  d, pPager->sync_
111d7 66 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 20 20  flags);.        
111d8 20 20 69 66 28 20 72 63 21 3d 30 20 29 20 72 65    if( rc!=0 ) re
111d9 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20  turn rc;.       
111da 20 7d 0a 0a 20 20 20 20 20 20 20 20 6a 72 6e 6c   }..        jrnl
111db 4f 66 66 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f  Off = pPager->jo
111dc 75 72 6e 61 6c 48 64 72 20 2b 20 73 69 7a 65 6f  urnalHdr + sizeo
111dd 66 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29  f(aJournalMagic)
111de 3b 0a 20 20 20 20 20 20 20 20 49 4f 54 52 41 43  ;.        IOTRAC
111df 45 28 28 22 4a 48 44 52 20 25 70 20 25 6c 6c 64  E(("JHDR %p %lld
111e0 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72 2c 20   %d\n", pPager, 
111e1 6a 72 6e 6c 4f 66 66 2c 20 34 29 29 3b 0a 20 20  jrnlOff, 4));.  
111e2 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
111e3 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
111e4 66 64 2c 20 6a 72 6e 6c 4f 66 66 2c 20 70 50 61  fd, jrnlOff, pPa
111e5 67 65 72 2d 3e 6e 52 65 63 29 3b 0a 20 20 20 20  ger->nRec);.    
111e6 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
111e7 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
111e8 20 20 20 20 20 20 69 66 28 20 30 3d 3d 28 69 44        if( 0==(iD
111e9 63 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53  c&SQLITE_IOCAP_S
111ea 45 51 55 45 4e 54 49 41 4c 29 20 29 7b 0a 20 20  EQUENTIAL) ){.  
111eb 20 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45        PAGERTRACE
111ec 32 28 22 53 59 4e 43 20 6a 6f 75 72 6e 61 6c 20  2("SYNC journal 
111ed 6f 66 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  of %d\n", PAGERI
111ee 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 20 20  D(pPager));.    
111ef 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 53      IOTRACE(("JS
111f0 59 4e 43 20 25 70 5c 6e 22 2c 20 70 50 61 67 65  YNC %p\n", pPage
111f1 72 29 29 0a 20 20 20 20 20 20 20 20 72 63 20 3d  r)).        rc =
111f2 20 73 71 6c 69 74 65 33 4f 73 53 79 6e 63 28 70   sqlite3OsSync(p
111f3 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 50 61 67  Pager->jfd, pPag
111f4 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 7c 20  er->sync_flags| 
111f5 0a 20 20 20 20 20 20 20 20 20 20 28 70 50 61 67  .          (pPag
111f6 65 72 2d 3e 73 79 6e 63 5f 66 6c 61 67 73 3d 3d  er->sync_flags==
111f7 53 51 4c 49 54 45 5f 53 59 4e 43 5f 46 55 4c 4c  SQLITE_SYNC_FULL
111f8 3f 53 51 4c 49 54 45 5f 53 59 4e 43 5f 44 41 54  ?SQLITE_SYNC_DAT
111f9 41 4f 4e 4c 59 3a 30 29 0a 20 20 20 20 20 20 20  AONLY:0).       
111fa 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
111fb 72 63 21 3d 30 20 29 20 72 65 74 75 72 6e 20 72  rc!=0 ) return r
111fc 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
111fd 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
111fe 53 74 61 72 74 65 64 20 3d 20 31 3b 0a 20 20 20  Started = 1;.   
111ff 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 6e   }.    pPager->n
11200 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 0a 20 20  eedSync = 0;..  
11201 20 20 2f 2a 20 45 72 61 73 65 20 74 68 65 20 6e    /* Erase the n
11202 65 65 64 53 79 6e 63 20 66 6c 61 67 20 66 72 6f  eedSync flag fro
11203 6d 20 65 76 65 72 79 20 70 61 67 65 2e 0a 20 20  m every page..  
11204 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
11205 50 63 61 63 68 65 43 6c 65 61 72 53 79 6e 63 46  PcacheClearSyncF
11206 6c 61 67 73 28 70 50 61 67 65 72 2d 3e 70 50 43  lags(pPager->pPC
11207 61 63 68 65 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  ache);.  }..  re
11208 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
11209 2a 20 47 69 76 65 6e 20 61 20 6c 69 73 74 20 6f  * Given a list o
1120a 66 20 70 61 67 65 73 20 28 63 6f 6e 6e 65 63 74  f pages (connect
1120b 65 64 20 62 79 20 74 68 65 20 50 67 48 64 72 2e  ed by the PgHdr.
1120c 70 44 69 72 74 79 20 70 6f 69 6e 74 65 72 29 20  pDirty pointer) 
1120d 77 72 69 74 65 0a 2a 2a 20 65 76 65 72 79 20 6f  write.** every o
1120e 6e 65 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65  ne of those page
1120f 73 20 6f 75 74 20 74 6f 20 74 68 65 20 64 61 74  s out to the dat
11210 61 62 61 73 65 20 66 69 6c 65 2e 20 4e 6f 20 63  abase file. No c
11211 61 6c 6c 73 20 61 72 65 20 6d 61 64 65 0a 2a 2a  alls are made.**
11212 20 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63   to the page-cac
11213 68 65 20 74 6f 20 6d 61 72 6b 20 74 68 65 20 70  he to mark the p
11214 61 67 65 73 20 61 73 20 63 6c 65 61 6e 2e 20 49  ages as clean. I
11215 74 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e 73  t is the respons
11216 69 62 69 6c 69 74 79 0a 2a 2a 20 6f 66 20 74 68  ibility.** of th
11217 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65 20  e caller to use 
11218 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c 28 29  PcacheCleanAll()
11219 20 6f 72 20 50 63 61 63 68 65 4d 61 6b 65 43 6c   or PcacheMakeCl
1121a 65 61 6e 28 29 20 74 6f 20 6d 61 72 6b 0a 2a 2a  ean() to mark.**
1121b 20 74 68 65 20 70 61 67 65 73 20 61 73 20 63 6c   the pages as cl
1121c 65 61 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ean..*/.static i
1121d 6e 74 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70  nt pager_write_p
1121e 61 67 65 6c 69 73 74 28 50 67 48 64 72 20 2a 70  agelist(PgHdr *p
1121f 4c 69 73 74 29 7b 0a 20 20 50 61 67 65 72 20 2a  List){.  Pager *
11220 70 50 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63  pPager;.  int rc
11221 3b 0a 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  ;..  if( pList==
11222 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
11223 45 5f 4f 4b 3b 0a 20 20 70 50 61 67 65 72 20 3d  E_OK;.  pPager =
11224 20 70 4c 69 73 74 2d 3e 70 50 61 67 65 72 3b 0a   pList->pPager;.
11225 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f  .  /* At this po
11226 69 6e 74 20 74 68 65 72 65 20 6d 61 79 20 62 65  int there may be
11227 20 65 69 74 68 65 72 20 61 20 52 45 53 45 52 56   either a RESERV
11228 45 44 20 6f 72 20 45 58 43 4c 55 53 49 56 45 20  ED or EXCLUSIVE 
11229 6c 6f 63 6b 20 6f 6e 20 74 68 65 0a 20 20 2a 2a  lock on the.  **
1122a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
1122b 49 66 20 74 68 65 72 65 20 69 73 20 61 6c 72 65  If there is alre
1122c 61 64 79 20 61 6e 20 45 58 43 4c 55 53 49 56 45  ady an EXCLUSIVE
1122d 20 6c 6f 63 6b 2c 20 74 68 65 20 66 6f 6c 6c 6f   lock, the follo
1122e 77 69 6e 67 0a 20 20 2a 2a 20 63 61 6c 6c 73 20  wing.  ** calls 
1122f 74 6f 20 73 71 6c 69 74 65 33 4f 73 4c 6f 63 6b  to sqlite3OsLock
11230 28 29 20 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 20  () are no-ops.. 
11231 20 2a 2a 0a 20 20 2a 2a 20 4d 6f 76 69 6e 67 20   **.  ** Moving 
11232 74 68 65 20 6c 6f 63 6b 20 66 72 6f 6d 20 52 45  the lock from RE
11233 53 45 52 56 45 44 20 74 6f 20 45 58 43 4c 55 53  SERVED to EXCLUS
11234 49 56 45 20 61 63 74 75 61 6c 6c 79 20 69 6e 76  IVE actually inv
11235 6f 6c 76 65 73 20 67 6f 69 6e 67 0a 20 20 2a 2a  olves going.  **
11236 20 74 68 72 6f 75 67 68 20 61 6e 20 69 6e 74 65   through an inte
11237 72 6d 65 64 69 61 74 65 20 73 74 61 74 65 20 50  rmediate state P
11238 45 4e 44 49 4e 47 2e 20 20 20 41 20 50 45 4e 44  ENDING.   A PEND
11239 49 4e 47 20 6c 6f 63 6b 20 70 72 65 76 65 6e 74  ING lock prevent
1123a 73 20 6e 65 77 0a 20 20 2a 2a 20 72 65 61 64 65  s new.  ** reade
1123b 72 73 20 66 72 6f 6d 20 61 74 74 61 63 68 69 6e  rs from attachin
1123c 67 20 74 6f 20 74 68 65 20 64 61 74 61 62 61 73  g to the databas
1123d 65 20 62 75 74 20 69 73 20 75 6e 73 75 66 66 69  e but is unsuffi
1123e 63 69 65 6e 74 20 66 6f 72 20 75 73 20 74 6f 0a  cient for us to.
1123f 20 20 2a 2a 20 77 72 69 74 65 2e 20 20 54 68 65    ** write.  The
11240 20 69 64 65 61 20 6f 66 20 61 20 50 45 4e 44 49   idea of a PENDI
11241 4e 47 20 6c 6f 63 6b 20 69 73 20 74 6f 20 70 72  NG lock is to pr
11242 65 76 65 6e 74 20 6e 65 77 20 72 65 61 64 65 72  event new reader
11243 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 63 6f 6d 69  s from.  ** comi
11244 6e 67 20 69 6e 20 77 68 69 6c 65 20 77 65 20 77  ng in while we w
11245 61 69 74 20 66 6f 72 20 65 78 69 73 74 69 6e 67  ait for existing
11246 20 72 65 61 64 65 72 73 20 74 6f 20 63 6c 65 61   readers to clea
11247 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 68 69  r..  **.  ** Whi
11248 6c 65 20 74 68 65 20 70 61 67 65 72 20 69 73 20  le the pager is 
11249 69 6e 20 74 68 65 20 52 45 53 45 52 56 45 44 20  in the RESERVED 
1124a 73 74 61 74 65 2c 20 74 68 65 20 6f 72 69 67 69  state, the origi
1124b 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66 69 6c  nal database fil
1124c 65 0a 20 20 2a 2a 20 69 73 20 75 6e 63 68 61 6e  e.  ** is unchan
1124d 67 65 64 20 61 6e 64 20 77 65 20 63 61 6e 20 72  ged and we can r
1124e 6f 6c 6c 62 61 63 6b 20 77 69 74 68 6f 75 74 20  ollback without 
1124f 68 61 76 69 6e 67 20 74 6f 20 70 6c 61 79 62 61  having to playba
11250 63 6b 20 74 68 65 0a 20 20 2a 2a 20 6a 6f 75 72  ck the.  ** jour
11251 6e 61 6c 20 69 6e 74 6f 20 74 68 65 20 6f 72 69  nal into the ori
11252 67 69 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ginal database f
11253 69 6c 65 2e 20 20 4f 6e 63 65 20 77 65 20 74 72  ile.  Once we tr
11254 61 6e 73 69 74 69 6f 6e 20 74 6f 0a 20 20 2a 2a  ansition to.  **
11255 20 45 58 43 4c 55 53 49 56 45 2c 20 69 74 20 6d   EXCLUSIVE, it m
11256 65 61 6e 73 20 74 68 65 20 64 61 74 61 62 61 73  eans the databas
11257 65 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20  e file has been 
11258 63 68 61 6e 67 65 64 20 61 6e 64 20 61 6e 79 20  changed and any 
11259 72 6f 6c 6c 62 61 63 6b 0a 20 20 2a 2a 20 77 69  rollback.  ** wi
1125a 6c 6c 20 72 65 71 75 69 72 65 20 61 20 6a 6f 75  ll require a jou
1125b 72 6e 61 6c 20 70 6c 61 79 62 61 63 6b 2e 0a 20  rnal playback.. 
1125c 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65 72   */.  rc = pager
1125d 5f 77 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50  _wait_on_lock(pP
1125e 61 67 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f  ager, EXCLUSIVE_
1125f 4c 4f 43 4b 29 3b 0a 20 20 69 66 28 20 72 63 21  LOCK);.  if( rc!
11260 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11261 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
11262 0a 0a 20 20 77 68 69 6c 65 28 20 70 4c 69 73 74  ..  while( pList
11263 20 29 7b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74   ){..    /* If t
11264 68 65 20 66 69 6c 65 20 68 61 73 20 6e 6f 74 20  he file has not 
11265 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64 2c  yet been opened,
11266 20 6f 70 65 6e 20 69 74 20 6e 6f 77 2e 20 2a 2f   open it now. */
11267 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72  .    if( !pPager
11268 2d 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29  ->fd->pMethods )
11269 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70  {.      assert(p
1126a 50 61 67 65 72 2d 3e 74 65 6d 70 46 69 6c 65 29  Pager->tempFile)
1126b 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1126c 69 74 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d  ite3PagerOpentem
1126d 70 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  p(pPager, pPager
1126e 2d 3e 66 64 2c 20 70 50 61 67 65 72 2d 3e 76 66  ->fd, pPager->vf
1126f 73 46 6c 61 67 73 29 3b 0a 20 20 20 20 20 20 69  sFlags);.      i
11270 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
11271 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  c;.    }..    /*
11272 20 49 66 20 74 68 65 72 65 20 61 72 65 20 64 69   If there are di
11273 72 74 79 20 70 61 67 65 73 20 69 6e 20 74 68 65  rty pages in the
11274 20 70 61 67 65 20 63 61 63 68 65 20 77 69 74 68   page cache with
11275 20 70 61 67 65 20 6e 75 6d 62 65 72 73 20 67 72   page numbers gr
11276 65 61 74 65 72 0a 20 20 20 20 2a 2a 20 74 68 61  eater.    ** tha
11277 6e 20 50 61 67 65 72 2e 64 62 53 69 7a 65 2c 20  n Pager.dbSize, 
11278 74 68 69 73 20 6d 65 61 6e 73 20 73 71 6c 69 74  this means sqlit
11279 65 33 50 61 67 65 72 54 72 75 6e 63 61 74 65 28  e3PagerTruncate(
1127a 29 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 0a  ) was called to.
1127b 20 20 20 20 2a 2a 20 6d 61 6b 65 20 74 68 65 20      ** make the 
1127c 66 69 6c 65 20 73 6d 61 6c 6c 65 72 20 28 70 72  file smaller (pr
1127d 65 73 75 6d 61 62 6c 79 20 62 79 20 61 75 74 6f  esumably by auto
1127e 2d 76 61 63 75 75 6d 20 63 6f 64 65 29 2e 20 44  -vacuum code). D
1127f 6f 20 6e 6f 74 20 77 72 69 74 65 0a 20 20 20 20  o not write.    
11280 2a 2a 20 61 6e 79 20 73 75 63 68 20 70 61 67 65  ** any such page
11281 73 20 74 6f 20 74 68 65 20 66 69 6c 65 2e 0a 20  s to the file.. 
11282 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4c     */.    if( pL
11283 69 73 74 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67 65  ist->pgno<=pPage
11284 72 2d 3e 64 62 53 69 7a 65 20 26 26 20 30 3d 3d  r->dbSize && 0==
11285 28 70 4c 69 73 74 2d 3e 66 6c 61 67 73 26 50 47  (pList->flags&PG
11286 48 44 52 5f 44 4f 4e 54 5f 57 52 49 54 45 29 20  HDR_DONT_WRITE) 
11287 29 7b 0a 20 20 20 20 20 20 69 36 34 20 6f 66 66  ){.      i64 off
11288 73 65 74 20 3d 20 28 70 4c 69 73 74 2d 3e 70 67  set = (pList->pg
11289 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50 61 67 65  no-1)*(i64)pPage
1128a 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 20 20 20  r->pageSize;.   
1128b 20 20 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d     char *pData =
1128c 20 43 4f 44 45 43 32 28 70 50 61 67 65 72 2c 20   CODEC2(pPager, 
1128d 70 4c 69 73 74 2d 3e 70 44 61 74 61 2c 20 70 4c  pList->pData, pL
1128e 69 73 74 2d 3e 70 67 6e 6f 2c 20 36 29 3b 0a 20  ist->pgno, 6);. 
1128f 20 20 20 20 20 50 41 47 45 52 54 52 41 43 45 34       PAGERTRACE4
11290 28 22 53 54 4f 52 45 20 25 64 20 70 61 67 65 20  ("STORE %d page 
11291 25 64 20 68 61 73 68 28 25 30 38 78 29 5c 6e 22  %d hash(%08x)\n"
11292 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
11293 20 20 20 20 20 50 41 47 45 52 49 44 28 70 50 61       PAGERID(pPa
11294 67 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e  ger), pList->pgn
11295 6f 2c 20 70 61 67 65 72 5f 70 61 67 65 68 61 73  o, pager_pagehas
11296 68 28 70 4c 69 73 74 29 29 3b 0a 20 20 20 20 20  h(pList));.     
11297 20 49 4f 54 52 41 43 45 28 28 22 50 47 4f 55 54   IOTRACE(("PGOUT
11298 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65   %p %d\n", pPage
11299 72 2c 20 70 4c 69 73 74 2d 3e 70 67 6e 6f 29 29  r, pList->pgno))
1129a 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  ;.      rc = sql
1129b 69 74 65 33 4f 73 57 72 69 74 65 28 70 50 61 67  ite3OsWrite(pPag
1129c 65 72 2d 3e 66 64 2c 20 70 44 61 74 61 2c 20 70  er->fd, pData, p
1129d 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c  Pager->pageSize,
1129e 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20 20 20   offset);.      
1129f 50 41 47 45 52 5f 49 4e 43 52 28 73 71 6c 69 74  PAGER_INCR(sqlit
112a0 65 33 5f 70 61 67 65 72 5f 77 72 69 74 65 64 62  e3_pager_writedb
112a1 5f 63 6f 75 6e 74 29 3b 0a 20 20 20 20 20 20 50  _count);.      P
112a2 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67 65 72  AGER_INCR(pPager
112a3 2d 3e 6e 57 72 69 74 65 29 3b 0a 20 20 20 20 20  ->nWrite);.     
112a4 20 69 66 28 20 70 4c 69 73 74 2d 3e 70 67 6e 6f   if( pList->pgno
112a5 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 6d  ==1 ){.        m
112a6 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64  emcpy(&pPager->d
112a7 62 46 69 6c 65 56 65 72 73 2c 20 26 70 44 61 74  bFileVers, &pDat
112a8 61 5b 32 34 5d 2c 20 73 69 7a 65 6f 66 28 70 50  a[24], sizeof(pP
112a9 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
112aa 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
112ab 7d 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  }.#ifndef NDEBUG
112ac 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20 20 20  .    else{.     
112ad 20 50 41 47 45 52 54 52 41 43 45 33 28 22 4e 4f   PAGERTRACE3("NO
112ae 53 54 4f 52 45 20 25 64 20 70 61 67 65 20 25 64  STORE %d page %d
112af 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61  \n", PAGERID(pPa
112b0 67 65 72 29 2c 20 70 4c 69 73 74 2d 3e 70 67 6e  ger), pList->pgn
112b1 6f 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  o);.    }.#endif
112b2 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65  .    if( rc ) re
112b3 74 75 72 6e 20 72 63 3b 0a 23 69 66 64 65 66 20  turn rc;.#ifdef 
112b4 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
112b5 45 53 0a 20 20 20 20 70 4c 69 73 74 2d 3e 70 61  ES.    pList->pa
112b6 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
112b7 61 67 65 68 61 73 68 28 70 4c 69 73 74 29 3b 0a  agehash(pList);.
112b8 23 65 6e 64 69 66 0a 20 20 20 20 70 4c 69 73 74  #endif.    pList
112b9 20 3d 20 70 4c 69 73 74 2d 3e 70 44 69 72 74 79   = pList->pDirty
112ba 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
112bb 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
112bc 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
112bd 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74  n is called by t
112be 68 65 20 70 63 61 63 68 65 20 6c 61 79 65 72 20  he pcache layer 
112bf 77 68 65 6e 20 69 74 20 68 61 73 20 72 65 61 63  when it has reac
112c0 68 65 64 20 73 6f 6d 65 0a 2a 2a 20 73 6f 66 74  hed some.** soft
112c1 20 6d 65 6d 6f 72 79 20 6c 69 6d 69 74 2e 20 54   memory limit. T
112c2 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61  he argument is a
112c3 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70 75   pointer to a pu
112c4 72 67 65 61 62 6c 65 20 50 61 67 65 72 20 0a 2a  rgeable Pager .*
112c5 2a 20 6f 62 6a 65 63 74 2e 20 54 68 69 73 20 66  * object. This f
112c6 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70 74 73  unction attempts
112c7 20 74 6f 20 6d 61 6b 65 20 61 20 73 69 6e 67 6c   to make a singl
112c8 65 20 64 69 72 74 79 20 70 61 67 65 20 74 68 61  e dirty page tha
112c9 74 20 68 61 73 20 6e 6f 0a 2a 2a 20 6f 75 74 73  t has no.** outs
112ca 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e 63  tanding referenc
112cb 65 73 20 28 69 66 20 6f 6e 65 20 65 78 69 73 74  es (if one exist
112cc 73 29 20 63 6c 65 61 6e 20 73 6f 20 74 68 61 74  s) clean so that
112cd 20 69 74 20 63 61 6e 20 62 65 20 72 65 63 79 63   it can be recyc
112ce 6c 65 64 20 0a 2a 2a 20 62 79 20 74 68 65 20 70  led .** by the p
112cf 63 61 63 68 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a  cache layer..*/.
112d0 73 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72  static int pager
112d1 53 74 72 65 73 73 28 76 6f 69 64 20 2a 70 2c 20  Stress(void *p, 
112d2 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 50  PgHdr *pPg){.  P
112d3 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 28  ager *pPager = (
112d4 50 61 67 65 72 20 2a 29 70 3b 0a 20 20 69 6e 74  Pager *)p;.  int
112d5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
112d6 0a 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  ..  if( pPager->
112d7 64 6f 4e 6f 74 53 79 6e 63 20 29 7b 0a 20 20 20  doNotSync ){.   
112d8 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
112d9 4b 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  K;.  }..  assert
112da 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  ( pPg->flags&PGH
112db 44 52 5f 44 49 52 54 59 20 29 3b 0a 20 20 69 66  DR_DIRTY );.  if
112dc 28 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64  ( pPager->errCod
112dd 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  e==SQLITE_OK ){.
112de 20 20 20 20 69 66 28 20 70 50 67 2d 3e 66 6c 61      if( pPg->fla
112df 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
112e0 4e 43 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  NC ){.      rc =
112e1 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70 50 61   syncJournal(pPa
112e2 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ger);.      if( 
112e3 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
112e4 20 70 50 61 67 65 72 2d 3e 66 75 6c 6c 53 79 6e   pPager->fullSyn
112e5 63 20 26 26 20 0a 20 20 20 20 20 20 20 20 21 28  c && .        !(
112e6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d  pPager->journalM
112e7 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
112e8 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 29 20 26  ALMODE_MEMORY) &
112e9 26 0a 20 20 20 20 20 20 20 20 21 28 73 71 6c 69  &.        !(sqli
112ea 74 65 33 4f 73 44 65 76 69 63 65 43 68 61 72 61  te3OsDeviceChara
112eb 63 74 65 72 69 73 74 69 63 73 28 70 50 61 67 65  cteristics(pPage
112ec 72 2d 3e 66 64 29 26 53 51 4c 49 54 45 5f 49 4f  r->fd)&SQLITE_IO
112ed 43 41 50 5f 53 41 46 45 5f 41 50 50 45 4e 44 29  CAP_SAFE_APPEND)
112ee 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
112ef 20 20 70 50 61 67 65 72 2d 3e 6e 52 65 63 20 3d    pPager->nRec =
112f0 20 30 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   0;.        rc =
112f1 20 77 72 69 74 65 4a 6f 75 72 6e 61 6c 48 64 72   writeJournalHdr
112f2 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20  (pPager);.      
112f3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  }.    }.    if( 
112f4 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
112f5 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 44 69 72  .      pPg->pDir
112f6 74 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63  ty = 0;.      rc
112f7 20 3d 20 70 61 67 65 72 5f 77 72 69 74 65 5f 70   = pager_write_p
112f8 61 67 65 6c 69 73 74 28 70 50 67 29 3b 0a 20 20  agelist(pPg);.  
112f9 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d    }.    if( rc!=
112fa 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
112fb 20 20 20 70 61 67 65 72 5f 65 72 72 6f 72 28 70     pager_error(p
112fc 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20 20 20  Pager, rc);.    
112fd 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  }.  }..  if( rc=
112fe 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
112ff 20 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 4d    sqlite3PcacheM
11300 61 6b 65 43 6c 65 61 6e 28 70 50 67 29 3b 0a 20  akeClean(pPg);. 
11301 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
11302 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  }.../*.** Return
11303 20 31 20 69 66 20 74 68 65 72 65 20 69 73 20 61   1 if there is a
11304 20 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 6f 6e 20   hot journal on 
11305 74 68 65 20 67 69 76 65 6e 20 70 61 67 65 72 2e  the given pager.
11306 0a 2a 2a 20 41 20 68 6f 74 20 6a 6f 75 72 6e 61  .** A hot journa
11307 6c 20 69 73 20 6f 6e 65 20 74 68 61 74 20 6e 65  l is one that ne
11308 65 64 73 20 74 6f 20 62 65 20 70 6c 61 79 65 64  eds to be played
11309 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   back..**.** If 
1130a 74 68 65 20 63 75 72 72 65 6e 74 20 73 69 7a 65  the current size
1130b 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1130c 20 66 69 6c 65 20 69 73 20 30 20 62 75 74 20 61   file is 0 but a
1130d 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 0a 2a 2a   journal file.**
1130e 20 65 78 69 73 74 73 2c 20 74 68 61 74 20 69 73   exists, that is
1130f 20 70 72 6f 62 61 62 6c 79 20 61 6e 20 6f 6c 64   probably an old
11310 20 6a 6f 75 72 6e 61 6c 20 6c 65 66 74 20 6f 76   journal left ov
11311 65 72 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 0a  er from a prior.
11312 2a 2a 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ** database with
11313 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65 2e 20   the same name. 
11314 20 4a 75 73 74 20 64 65 6c 65 74 65 20 74 68 65   Just delete the
11315 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20   journal..**.** 
11316 52 65 74 75 72 6e 20 6e 65 67 61 74 69 76 65 20  Return negative 
11317 69 66 20 75 6e 61 62 6c 65 20 74 6f 20 64 65 74  if unable to det
11318 65 72 6d 69 6e 65 20 74 68 65 20 73 74 61 74 75  ermine the statu
11319 73 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  s of the journal
1131a 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1131b 74 69 6e 65 20 64 6f 65 73 20 6e 6f 74 20 6f 70  tine does not op
1131c 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66  en the journal f
1131d 69 6c 65 20 74 6f 20 65 78 61 6d 69 6e 65 20 69  ile to examine i
1131e 74 73 0a 2a 2a 20 63 6f 6e 74 65 6e 74 2e 20 20  ts.** content.  
1131f 48 65 6e 63 65 2c 20 74 68 65 20 6a 6f 75 72 6e  Hence, the journ
11320 61 6c 20 6d 69 67 68 74 20 63 6f 6e 74 61 69 6e  al might contain
11321 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 6d   the name of a m
11322 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  aster.** journal
11323 20 66 69 6c 65 20 74 68 61 74 20 68 61 73 20 62   file that has b
11324 65 65 6e 20 64 65 6c 65 74 65 64 2c 20 61 6e 64  een deleted, and
11325 20 68 65 6e 63 65 20 6e 6f 74 20 62 65 20 68 6f   hence not be ho
11326 74 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 20 68 65  t.  Or.** the he
11327 61 64 65 72 20 6f 66 20 74 68 65 20 6a 6f 75 72  ader of the jour
11328 6e 61 6c 20 6d 69 67 68 74 20 62 65 20 7a 65 72  nal might be zer
11329 6f 65 64 20 6f 75 74 2e 20 20 54 68 69 73 20 72  oed out.  This r
1132a 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65 73 20 6e  outine.** does n
1132b 6f 74 20 64 69 73 63 6f 76 65 72 20 74 68 65 73  ot discover thes
1132c 65 20 63 61 73 65 73 20 6f 66 20 61 20 6e 6f 6e  e cases of a non
1132d 2d 68 6f 74 20 6a 6f 75 72 6e 61 6c 20 2d 20 69  -hot journal - i
1132e 66 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  f the.** journal
1132f 20 66 69 6c 65 20 65 78 69 73 74 73 20 61 6e 64   file exists and
11330 20 69 73 20 6e 6f 74 20 65 6d 70 74 79 20 74 68   is not empty th
11331 69 73 20 72 6f 75 74 69 6e 65 20 61 73 73 75 6d  is routine assum
11332 65 73 20 69 74 0a 2a 2a 20 69 73 20 68 6f 74 2e  es it.** is hot.
11333 20 20 54 68 65 20 70 61 67 65 72 5f 70 6c 61 79    The pager_play
11334 62 61 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 77  back() routine w
11335 69 6c 6c 20 64 69 73 63 6f 76 65 72 20 74 68 61  ill discover tha
11336 74 20 74 68 65 0a 2a 2a 20 6a 6f 75 72 6e 61 6c  t the.** journal
11337 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 72 65 61   file is not rea
11338 6c 6c 79 20 68 6f 74 20 61 6e 64 20 77 69 6c 6c  lly hot and will
11339 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69   no-op..*/.stati
1133a 63 20 69 6e 74 20 68 61 73 48 6f 74 4a 6f 75 72  c int hasHotJour
1133b 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
1133c 72 2c 20 69 6e 74 20 2a 70 45 78 69 73 74 73 29  r, int *pExists)
1133d 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20  {.  sqlite3_vfs 
1133e 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d 3e  *pVfs = pPager->
1133f 70 56 66 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d  pVfs;.  int rc =
11340 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e   SQLITE_OK;.  in
11341 74 20 65 78 69 73 74 73 3b 0a 20 20 69 6e 74 20  t exists;.  int 
11342 6c 6f 63 6b 65 64 3b 0a 20 20 61 73 73 65 72 74  locked;.  assert
11343 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20  ( pPager!=0 );. 
11344 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
11345 3e 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20  >useJournal );. 
11346 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
11347 3e 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29 3b  >fd->pMethods );
11348 0a 20 20 2a 70 45 78 69 73 74 73 20 3d 20 30 3b  .  *pExists = 0;
11349 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f  .  rc = sqlite3O
1134a 73 41 63 63 65 73 73 28 70 56 66 73 2c 20 70 50  sAccess(pVfs, pP
1134b 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
1134c 53 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58  SQLITE_ACCESS_EX
1134d 49 53 54 53 2c 20 26 65 78 69 73 74 73 29 3b 0a  ISTS, &exists);.
1134e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1134f 5f 4f 4b 20 26 26 20 65 78 69 73 74 73 20 29 7b  _OK && exists ){
11350 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
11351 33 4f 73 43 68 65 63 6b 52 65 73 65 72 76 65 64  3OsCheckReserved
11352 4c 6f 63 6b 28 70 50 61 67 65 72 2d 3e 66 64 2c  Lock(pPager->fd,
11353 20 26 6c 6f 63 6b 65 64 29 3b 0a 20 20 7d 0a 20   &locked);.  }. 
11354 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11355 4f 4b 20 26 26 20 65 78 69 73 74 73 20 26 26 20  OK && exists && 
11356 21 6c 6f 63 6b 65 64 20 29 7b 0a 20 20 20 20 69  !locked ){.    i
11357 6e 74 20 6e 50 61 67 65 3b 0a 20 20 20 20 72 63  nt nPage;.    rc
11358 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 50   = sqlite3PagerP
11359 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
1135a 20 26 6e 50 61 67 65 29 3b 0a 20 20 20 20 69 66   &nPage);.    if
1135b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
1135c 29 7b 0a 20 20 20 20 20 69 66 28 20 6e 50 61 67  ){.     if( nPag
1135d 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  e==0 ){.        
1135e 73 71 6c 69 74 65 33 4f 73 44 65 6c 65 74 65 28  sqlite3OsDelete(
1135f 70 56 66 73 2c 20 70 50 61 67 65 72 2d 3e 7a 4a  pVfs, pPager->zJ
11360 6f 75 72 6e 61 6c 2c 20 30 29 3b 0a 20 20 20 20  ournal, 0);.    
11361 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
11362 20 2a 70 45 78 69 73 74 73 20 3d 20 31 3b 0a 20   *pExists = 1;. 
11363 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
11364 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
11365 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20  ./*.** Read the 
11366 63 6f 6e 74 65 6e 74 20 6f 66 20 70 61 67 65 20  content of page 
11367 70 50 67 20 6f 75 74 20 6f 66 20 74 68 65 20 64  pPg out of the d
11368 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f  atabase file..*/
11369 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 61 64  .static int read
1136a 44 62 50 61 67 65 28 50 61 67 65 72 20 2a 70 50  DbPage(Pager *pP
1136b 61 67 65 72 2c 20 50 67 48 64 72 20 2a 70 50 67  ager, PgHdr *pPg
1136c 2c 20 50 67 6e 6f 20 70 67 6e 6f 29 7b 0a 20 20  , Pgno pgno){.  
1136d 69 6e 74 20 72 63 3b 0a 20 20 69 36 34 20 6f 66  int rc;.  i64 of
1136e 66 73 65 74 3b 0a 20 20 61 73 73 65 72 74 28 20  fset;.  assert( 
1136f 4d 45 4d 44 42 3d 3d 30 20 29 3b 0a 20 20 61 73  MEMDB==0 );.  as
11370 73 65 72 74 28 70 50 61 67 65 72 2d 3e 66 64 2d  sert(pPager->fd-
11371 3e 70 4d 65 74 68 6f 64 73 7c 7c 70 50 61 67 65  >pMethods||pPage
11372 72 2d 3e 74 65 6d 70 46 69 6c 65 29 3b 0a 20 20  r->tempFile);.  
11373 69 66 28 20 21 70 50 61 67 65 72 2d 3e 66 64 2d  if( !pPager->fd-
11374 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  >pMethods ){.   
11375 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 49   return SQLITE_I
11376 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 3b  OERR_SHORT_READ;
11377 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74 20 3d 20  .  }.  offset = 
11378 28 70 67 6e 6f 2d 31 29 2a 28 69 36 34 29 70 50  (pgno-1)*(i64)pP
11379 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 3b 0a  ager->pageSize;.
1137a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
1137b 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
1137c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70 50 61   pPg->pData, pPa
1137d 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2c 20 6f  ger->pageSize, o
1137e 66 66 73 65 74 29 3b 0a 20 20 50 41 47 45 52 5f  ffset);.  PAGER_
1137f 49 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67  INCR(sqlite3_pag
11380 65 72 5f 72 65 61 64 64 62 5f 63 6f 75 6e 74 29  er_readdb_count)
11381 3b 0a 20 20 50 41 47 45 52 5f 49 4e 43 52 28 70  ;.  PAGER_INCR(p
11382 50 61 67 65 72 2d 3e 6e 52 65 61 64 29 3b 0a 20  Pager->nRead);. 
11383 20 49 4f 54 52 41 43 45 28 28 22 50 47 49 4e 20   IOTRACE(("PGIN 
11384 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
11385 2c 20 70 67 6e 6f 29 29 3b 0a 20 20 69 66 28 20  , pgno));.  if( 
11386 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20 6d  pgno==1 ){.    m
11387 65 6d 63 70 79 28 26 70 50 61 67 65 72 2d 3e 64  emcpy(&pPager->d
11388 62 46 69 6c 65 56 65 72 73 2c 20 26 28 28 75 38  bFileVers, &((u8
11389 2a 29 70 50 67 2d 3e 70 44 61 74 61 29 5b 32 34  *)pPg->pData)[24
1138a 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
1138b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1138c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1138d 20 73 69 7a 65 6f 66 28 70 50 61 67 65 72 2d 3e   sizeof(pPager->
1138e 64 62 46 69 6c 65 56 65 72 73 29 29 3b 0a 20 20  dbFileVers));.  
1138f 7d 0a 20 20 43 4f 44 45 43 31 28 70 50 61 67 65  }.  CODEC1(pPage
11390 72 2c 20 70 50 67 2d 3e 70 44 61 74 61 2c 20 70  r, pPg->pData, p
11391 50 67 2d 3e 70 67 6e 6f 2c 20 33 29 3b 0a 20 20  Pg->pgno, 3);.  
11392 50 41 47 45 52 54 52 41 43 45 34 28 22 46 45 54  PAGERTRACE4("FET
11393 43 48 20 25 64 20 70 61 67 65 20 25 64 20 68 61  CH %d page %d ha
11394 73 68 28 25 30 38 78 29 5c 6e 22 2c 0a 20 20 20  sh(%08x)\n",.   
11395 20 20 20 20 20 20 20 20 20 20 20 20 50 41 47 45              PAGE
11396 52 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67  RID(pPager), pPg
11397 2d 3e 70 67 6e 6f 2c 20 70 61 67 65 72 5f 70 61  ->pgno, pager_pa
11398 67 65 68 61 73 68 28 70 50 67 29 29 3b 0a 20 20  gehash(pPg));.  
11399 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f  return rc;.}.../
1139a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1139b 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  on is called to 
1139c 6f 62 74 61 69 6e 20 74 68 65 20 73 68 61 72 65  obtain the share
1139d 64 20 6c 6f 63 6b 20 72 65 71 75 69 72 65 64 20  d lock required 
1139e 62 65 66 6f 72 65 0a 2a 2a 20 64 61 74 61 20 6d  before.** data m
1139f 61 79 20 62 65 20 72 65 61 64 20 66 72 6f 6d 20  ay be read from 
113a0 74 68 65 20 70 61 67 65 72 20 63 61 63 68 65 2e  the pager cache.
113a1 20 49 66 20 74 68 65 20 73 68 61 72 65 64 20 6c   If the shared l
113a2 6f 63 6b 20 68 61 73 20 61 6c 72 65 61 64 79 0a  ock has already.
113a3 2a 2a 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64  ** been obtained
113a4 2c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  , this function 
113a5 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a  is a no-op..**.*
113a6 2a 20 49 6d 6d 65 64 69 61 74 65 6c 79 20 61 66  * Immediately af
113a7 74 65 72 20 6f 62 74 61 69 6e 69 6e 67 20 74 68  ter obtaining th
113a8 65 20 73 68 61 72 65 64 20 6c 6f 63 6b 20 28 69  e shared lock (i
113a9 66 20 72 65 71 75 69 72 65 64 29 2c 20 74 68 69  f required), thi
113aa 73 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 68  s function.** ch
113ab 65 63 6b 73 20 66 6f 72 20 61 20 68 6f 74 2d 6a  ecks for a hot-j
113ac 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 49 66 20  ournal file. If 
113ad 6f 6e 65 20 69 73 20 66 6f 75 6e 64 2c 20 61 6e  one is found, an
113ae 20 65 6d 65 72 67 65 6e 63 79 20 72 6f 6c 6c 62   emergency rollb
113af 61 63 6b 0a 2a 2a 20 69 73 20 70 65 72 66 6f 72  ack.** is perfor
113b0 6d 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e  med immediately.
113b1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
113b2 61 67 65 72 53 68 61 72 65 64 4c 6f 63 6b 28 50  agerSharedLock(P
113b3 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20  ager *pPager){. 
113b4 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
113b5 5f 4f 4b 3b 0a 20 20 69 6e 74 20 69 73 45 72 72  _OK;.  int isErr
113b6 6f 72 52 65 73 65 74 20 3d 20 30 3b 0a 0a 20 20  orReset = 0;..  
113b7 2f 2a 20 49 66 20 74 68 69 73 20 64 61 74 61 62  /* If this datab
113b8 61 73 65 20 69 73 20 6f 70 65 6e 65 64 20 66 6f  ase is opened fo
113b9 72 20 65 78 63 6c 75 73 69 76 65 20 61 63 63 65  r exclusive acce
113ba 73 73 2c 20 68 61 73 20 6e 6f 20 6f 75 74 73 74  ss, has no outst
113bb 61 6e 64 69 6e 67 20 0a 20 20 2a 2a 20 70 61 67  anding .  ** pag
113bc 65 20 72 65 66 65 72 65 6e 63 65 73 20 61 6e 64  e references and
113bd 20 69 73 20 69 6e 20 61 6e 20 65 72 72 6f 72 2d   is in an error-
113be 73 74 61 74 65 2c 20 6e 6f 77 20 69 73 20 74 68  state, now is th
113bf 65 20 63 68 61 6e 63 65 20 74 6f 20 63 6c 65 61  e chance to clea
113c0 72 0a 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72  r.  ** the error
113c1 2e 20 44 69 73 63 61 72 64 20 74 68 65 20 63 6f  . Discard the co
113c2 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 70 61  ntents of the pa
113c3 67 65 72 2d 63 61 63 68 65 20 61 6e 64 20 74 72  ger-cache and tr
113c4 65 61 74 20 61 6e 79 0a 20 20 2a 2a 20 6f 70 65  eat any.  ** ope
113c5 6e 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 61  n journal file a
113c6 73 20 61 20 68 6f 74 2d 6a 6f 75 72 6e 61 6c 2e  s a hot-journal.
113c7 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 4d 45 4d  .  */.  if( !MEM
113c8 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e 65 78  DB && pPager->ex
113c9 63 6c 75 73 69 76 65 4d 6f 64 65 20 0a 20 20 20  clusiveMode .   
113ca 26 26 20 73 71 6c 69 74 65 33 50 63 61 63 68 65  && sqlite3Pcache
113cb 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
113cc 3e 70 50 43 61 63 68 65 29 3d 3d 30 20 26 26 20  >pPCache)==0 && 
113cd 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20  pPager->errCode 
113ce 0a 20 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50  .  ){.    if( pP
113cf 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65  ager->journalOpe
113d0 6e 20 29 7b 0a 20 20 20 20 20 20 69 73 45 72 72  n ){.      isErr
113d1 6f 72 52 65 73 65 74 20 3d 20 31 3b 0a 20 20 20  orReset = 1;.   
113d2 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 65   }.    pPager->e
113d3 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
113d4 4f 4b 3b 0a 20 20 20 20 70 61 67 65 72 5f 72 65  OK;.    pager_re
113d5 73 65 74 28 70 50 61 67 65 72 29 3b 0a 20 20 7d  set(pPager);.  }
113d6 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 61  ..  /* If the pa
113d7 67 65 72 20 69 73 20 73 74 69 6c 6c 20 69 6e 20  ger is still in 
113d8 61 6e 20 65 72 72 6f 72 20 73 74 61 74 65 2c 20  an error state, 
113d9 64 6f 20 6e 6f 74 20 70 72 6f 63 65 65 64 2e 20  do not proceed. 
113da 54 68 65 20 65 72 72 6f 72 20 0a 20 20 2a 2a 20  The error .  ** 
113db 73 74 61 74 65 20 77 69 6c 6c 20 62 65 20 63 6c  state will be cl
113dc 65 61 72 65 64 20 61 74 20 73 6f 6d 65 20 70 6f  eared at some po
113dd 69 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75 72  int in the futur
113de 65 20 77 68 65 6e 20 61 6c 6c 20 70 61 67 65 20  e when all page 
113df 0a 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73  .  ** references
113e0 20 61 72 65 20 64 72 6f 70 70 65 64 20 61 6e 64   are dropped and
113e1 20 74 68 65 20 63 61 63 68 65 20 63 61 6e 20 62   the cache can b
113e2 65 20 64 69 73 63 61 72 64 65 64 2e 0a 20 20 2a  e discarded..  *
113e3 2f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e  /.  if( pPager->
113e4 65 72 72 43 6f 64 65 20 26 26 20 70 50 61 67 65  errCode && pPage
113e5 72 2d 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49  r->errCode!=SQLI
113e6 54 45 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 72  TE_FULL ){.    r
113e7 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72  eturn pPager->er
113e8 72 43 6f 64 65 3b 0a 20 20 7d 0a 0a 20 20 69 66  rCode;.  }..  if
113e9 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d  ( pPager->state=
113ea 3d 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 7c 7c  =PAGER_UNLOCK ||
113eb 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 29 7b   isErrorReset ){
113ec 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 66 73  .    sqlite3_vfs
113ed 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72 2d   *pVfs = pPager-
113ee 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 69  >pVfs;.    int i
113ef 73 48 6f 74 4a 6f 75 72 6e 61 6c 3b 0a 20 20 20  sHotJournal;.   
113f0 20 61 73 73 65 72 74 28 20 21 4d 45 4d 44 42 20   assert( !MEMDB 
113f1 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  );.    assert( s
113f2 71 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43  qlite3PcacheRefC
113f3 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43  ount(pPager->pPC
113f4 61 63 68 65 29 3d 3d 30 20 29 3b 0a 20 20 20 20  ache)==0 );.    
113f5 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f 52  if( !pPager->noR
113f6 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 20  eadlock ){.     
113f7 20 72 63 20 3d 20 70 61 67 65 72 5f 77 61 69 74   rc = pager_wait
113f8 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67 65 72 2c  _on_lock(pPager,
113f9 20 53 48 41 52 45 44 5f 4c 4f 43 4b 29 3b 0a 20   SHARED_LOCK);. 
113fa 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
113fb 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
113fc 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
113fd 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52 5f 55  ->state==PAGER_U
113fe 4e 4c 4f 43 4b 20 29 3b 0a 20 20 20 20 20 20 20  NLOCK );.       
113ff 20 72 65 74 75 72 6e 20 70 61 67 65 72 5f 65 72   return pager_er
11400 72 6f 72 28 70 50 61 67 65 72 2c 20 72 63 29 3b  ror(pPager, rc);
11401 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61  .      }.      a
11402 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
11403 74 61 74 65 3e 3d 53 48 41 52 45 44 5f 4c 4f 43  tate>=SHARED_LOC
11404 4b 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  K );.    }..    
11405 2f 2a 20 49 66 20 61 20 6a 6f 75 72 6e 61 6c 20  /* If a journal 
11406 66 69 6c 65 20 65 78 69 73 74 73 2c 20 61 6e 64  file exists, and
11407 20 74 68 65 72 65 20 69 73 20 6e 6f 20 52 45 53   there is no RES
11408 45 52 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68  ERVED lock on th
11409 65 0a 20 20 20 20 2a 2a 20 64 61 74 61 62 61 73  e.    ** databas
1140a 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 69 74 20  e file, then it 
1140b 65 69 74 68 65 72 20 6e 65 65 64 73 20 74 6f 20  either needs to 
1140c 62 65 20 70 6c 61 79 65 64 20 62 61 63 6b 20 6f  be played back o
1140d 72 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  r deleted..    *
1140e 2f 0a 20 20 20 20 69 66 28 20 21 69 73 45 72 72  /.    if( !isErr
1140f 6f 72 52 65 73 65 74 20 29 7b 0a 20 20 20 20 20  orReset ){.     
11410 20 72 63 20 3d 20 68 61 73 48 6f 74 4a 6f 75 72   rc = hasHotJour
11411 6e 61 6c 28 70 50 61 67 65 72 2c 20 26 69 73 48  nal(pPager, &isH
11412 6f 74 4a 6f 75 72 6e 61 6c 29 3b 0a 20 20 20 20  otJournal);.    
11413 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
11414 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
11415 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20  oto failed;.    
11416 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
11417 28 20 69 73 45 72 72 6f 72 52 65 73 65 74 20 7c  ( isErrorReset |
11418 7c 20 69 73 48 6f 74 4a 6f 75 72 6e 61 6c 20 29  | isHotJournal )
11419 7b 0a 20 20 20 20 20 20 2f 2a 20 47 65 74 20 61  {.      /* Get a
1141a 6e 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b  n EXCLUSIVE lock
1141b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
1141c 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70   file. At this p
1141d 6f 69 6e 74 20 69 74 20 69 73 0a 20 20 20 20 20  oint it is.     
1141e 20 2a 2a 20 69 6d 70 6f 72 74 61 6e 74 20 74 68   ** important th
1141f 61 74 20 61 20 52 45 53 45 52 56 45 44 20 6c 6f  at a RESERVED lo
11420 63 6b 20 69 73 20 6e 6f 74 20 6f 62 74 61 69 6e  ck is not obtain
11421 65 64 20 6f 6e 20 74 68 65 20 77 61 79 20 74 6f  ed on the way to
11422 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 45 58   the.      ** EX
11423 43 4c 55 53 49 56 45 20 6c 6f 63 6b 2e 20 49 66  CLUSIVE lock. If
11424 20 69 74 20 77 65 72 65 2c 20 61 6e 6f 74 68 65   it were, anothe
11425 72 20 70 72 6f 63 65 73 73 20 6d 69 67 68 74 20  r process might 
11426 6f 70 65 6e 20 74 68 65 0a 20 20 20 20 20 20 2a  open the.      *
11427 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2c  * database file,
11428 20 64 65 74 65 63 74 20 74 68 65 20 52 45 53 45   detect the RESE
11429 52 56 45 44 20 6c 6f 63 6b 2c 20 61 6e 64 20 63  RVED lock, and c
1142a 6f 6e 63 6c 75 64 65 20 74 68 61 74 20 74 68 65  onclude that the
1142b 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61  .      ** databa
1142c 73 65 20 69 73 20 73 61 66 65 20 74 6f 20 72 65  se is safe to re
1142d 61 64 20 77 68 69 6c 65 20 74 68 69 73 20 70 72  ad while this pr
1142e 6f 63 65 73 73 20 69 73 20 73 74 69 6c 6c 20 72  ocess is still r
1142f 6f 6c 6c 69 6e 67 20 69 74 20 0a 20 20 20 20 20  olling it .     
11430 20 2a 2a 20 62 61 63 6b 2e 0a 20 20 20 20 20 20   ** back..      
11431 2a 2a 20 0a 20 20 20 20 20 20 2a 2a 20 42 65 63  ** .      ** Bec
11432 61 75 73 65 20 74 68 65 20 69 6e 74 65 72 6d 65  ause the interme
11433 64 69 61 74 65 20 52 45 53 45 52 56 45 44 20 6c  diate RESERVED l
11434 6f 63 6b 20 69 73 20 6e 6f 74 20 72 65 71 75 65  ock is not reque
11435 73 74 65 64 2c 20 74 68 65 0a 20 20 20 20 20 20  sted, the.      
11436 2a 2a 20 73 65 63 6f 6e 64 20 70 72 6f 63 65 73  ** second proces
11437 73 20 77 69 6c 6c 20 67 65 74 20 74 6f 20 74 68  s will get to th
11438 69 73 20 70 6f 69 6e 74 20 69 6e 20 74 68 65 20  is point in the 
11439 63 6f 64 65 20 61 6e 64 20 66 61 69 6c 20 74 6f  code and fail to
1143a 0a 20 20 20 20 20 20 2a 2a 20 6f 62 74 61 69 6e  .      ** obtain
1143b 20 69 74 73 20 6f 77 6e 20 45 58 43 4c 55 53 49   its own EXCLUSI
1143c 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64  VE lock on the d
1143d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20  atabase file..  
1143e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
1143f 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3c 45   pPager->state<E
11440 58 43 4c 55 53 49 56 45 5f 4c 4f 43 4b 20 29 7b  XCLUSIVE_LOCK ){
11441 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
11442 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67  lite3OsLock(pPag
11443 65 72 2d 3e 66 64 2c 20 45 58 43 4c 55 53 49 56  er->fd, EXCLUSIV
11444 45 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 20 20 20  E_LOCK);.       
11445 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
11446 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
11447 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72  rc = pager_error
11448 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
11449 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66 61 69          goto fai
1144a 6c 65 64 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  led;.        }. 
1144b 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73         pPager->s
1144c 74 61 74 65 20 3d 20 50 41 47 45 52 5f 45 58 43  tate = PAGER_EXC
1144d 4c 55 53 49 56 45 3b 0a 20 20 20 20 20 20 7d 0a  LUSIVE;.      }.
1144e 20 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20   .      /* Open 
1144f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 6f 72 20  the journal for 
11450 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
11451 73 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75  s. This is becau
11452 73 65 20 69 6e 20 0a 20 20 20 20 20 20 2a 2a 20  se in .      ** 
11453 65 78 63 6c 75 73 69 76 65 2d 61 63 63 65 73 73  exclusive-access
11454 20 6d 6f 64 65 20 74 68 65 20 66 69 6c 65 20 64   mode the file d
11455 65 73 63 72 69 70 74 6f 72 20 77 69 6c 6c 20 62  escriptor will b
11456 65 20 6b 65 70 74 20 6f 70 65 6e 20 61 6e 64 0a  e kept open and.
11457 20 20 20 20 20 20 2a 2a 20 70 6f 73 73 69 62 6c        ** possibl
11458 79 20 75 73 65 64 20 66 6f 72 20 61 20 74 72 61  y used for a tra
11459 6e 73 61 63 74 69 6f 6e 20 6c 61 74 65 72 20 6f  nsaction later o
1145a 6e 2e 20 4f 6e 20 73 6f 6d 65 20 73 79 73 74 65  n. On some syste
1145b 6d 73 2c 20 74 68 65 0a 20 20 20 20 20 20 2a 2a  ms, the.      **
1145c 20 4f 73 54 72 75 6e 63 61 74 65 28 29 20 63 61   OsTruncate() ca
1145d 6c 6c 20 75 73 65 64 20 69 6e 20 65 78 63 6c 75  ll used in exclu
1145e 73 69 76 65 2d 61 63 63 65 73 73 20 6d 6f 64 65  sive-access mode
1145f 20 61 6c 73 6f 20 72 65 71 75 69 72 65 73 0a 20   also requires. 
11460 20 20 20 20 20 2a 2a 20 61 20 72 65 61 64 2f 77       ** a read/w
11461 72 69 74 65 20 66 69 6c 65 20 68 61 6e 64 6c 65  rite file handle
11462 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
11463 20 69 66 28 20 21 69 73 45 72 72 6f 72 52 65 73   if( !isErrorRes
11464 65 74 20 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f  et && pPager->jo
11465 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 7b 0a  urnalOpen==0 ){.
11466 20 20 20 20 20 20 20 20 69 6e 74 20 72 65 73 3b          int res;
11467 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71  .        rc = sq
11468 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 70 56  lite3OsAccess(pV
11469 66 73 2c 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72  fs,pPager->zJour
1146a 6e 61 6c 2c 53 51 4c 49 54 45 5f 41 43 43 45 53  nal,SQLITE_ACCES
1146b 53 5f 45 58 49 53 54 53 2c 26 72 65 73 29 3b 0a  S_EXISTS,&res);.
1146c 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
1146d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1146e 20 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29         if( res )
1146f 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 6e  {.            in
11470 74 20 66 6f 75 74 20 3d 20 30 3b 0a 20 20 20 20  t fout = 0;.    
11471 20 20 20 20 20 20 20 20 69 6e 74 20 66 20 3d 20          int f = 
11472 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44  SQLITE_OPEN_READ
11473 57 52 49 54 45 7c 53 51 4c 49 54 45 5f 4f 50 45  WRITE|SQLITE_OPE
11474 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41 4c 3b 0a  N_MAIN_JOURNAL;.
11475 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65              asse
11476 72 74 28 20 21 70 50 61 67 65 72 2d 3e 74 65 6d  rt( !pPager->tem
11477 70 46 69 6c 65 20 29 3b 0a 20 20 20 20 20 20 20  pFile );.       
11478 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
11479 33 4f 73 4f 70 65 6e 28 70 56 66 73 2c 20 70 50  3OsOpen(pVfs, pP
1147a 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20  ager->zJournal, 
1147b 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 2c 20  pPager->jfd, f, 
1147c 26 66 6f 75 74 29 3b 0a 20 20 20 20 20 20 20 20  &fout);.        
1147d 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21 3d      assert( rc!=
1147e 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50 61  SQLITE_OK || pPa
1147f 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68 6f  ger->jfd->pMetho
11480 64 73 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ds );.          
11481 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
11482 5f 4f 4b 20 26 26 20 66 6f 75 74 26 53 51 4c 49  _OK && fout&SQLI
11483 54 45 5f 4f 50 45 4e 5f 52 45 41 44 4f 4e 4c 59  TE_OPEN_READONLY
11484 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
11485 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 41    rc = SQLITE_CA
11486 4e 54 4f 50 45 4e 3b 0a 20 20 20 20 20 20 20 20  NTOPEN;.        
11487 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43        sqlite3OsC
11488 6c 6f 73 65 28 70 50 61 67 65 72 2d 3e 6a 66 64  lose(pPager->jfd
11489 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  );.            }
1148a 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65  .          }else
1148b 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  {.            /*
1148c 20 49 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20   If the journal 
1148d 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
1148e 74 68 61 74 20 6d 65 61 6e 73 20 73 6f 6d 65 20  that means some 
1148f 6f 74 68 65 72 20 70 72 6f 63 65 73 73 0a 20 20  other process.  
11490 20 20 20 20 20 20 20 20 20 20 2a 2a 20 68 61 73            ** has
11491 20 61 6c 72 65 61 64 79 20 72 6f 6c 6c 65 64 20   already rolled 
11492 69 74 20 62 61 63 6b 20 2a 2f 0a 20 20 20 20 20  it back */.     
11493 20 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49         rc = SQLI
11494 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20  TE_BUSY;.       
11495 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
11496 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
11497 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
11498 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 66  {.        goto f
11499 61 69 6c 65 64 3b 0a 20 20 20 20 20 20 7d 0a 20  ailed;.      }. 
1149a 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75       pPager->jou
1149b 72 6e 61 6c 4f 70 65 6e 20 3d 20 31 3b 0a 20 20  rnalOpen = 1;.  
1149c 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
1149d 6e 61 6c 53 74 61 72 74 65 64 20 3d 20 30 3b 0a  nalStarted = 0;.
1149e 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
1149f 75 72 6e 61 6c 4f 66 66 20 3d 20 30 3b 0a 20 20  urnalOff = 0;.  
114a0 20 20 20 20 70 50 61 67 65 72 2d 3e 73 65 74 4d      pPager->setM
114a1 61 73 74 65 72 20 3d 20 30 3b 0a 20 20 20 20 20  aster = 0;.     
114a2 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
114a3 48 64 72 20 3d 20 30 3b 0a 20 0a 20 20 20 20 20  Hdr = 0;. .     
114a4 20 2f 2a 20 50 6c 61 79 62 61 63 6b 20 61 6e 64   /* Playback and
114a5 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72   delete the jour
114a6 6e 61 6c 2e 20 20 44 72 6f 70 20 74 68 65 20 64  nal.  Drop the d
114a7 61 74 61 62 61 73 65 20 77 72 69 74 65 0a 20 20  atabase write.  
114a8 20 20 20 20 2a 2a 20 6c 6f 63 6b 20 61 6e 64 20      ** lock and 
114a9 72 65 61 63 71 75 69 72 65 20 74 68 65 20 72 65  reacquire the re
114aa 61 64 20 6c 6f 63 6b 2e 0a 20 20 20 20 20 20 2a  ad lock..      *
114ab 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67  /.      rc = pag
114ac 65 72 5f 70 6c 61 79 62 61 63 6b 28 70 50 61 67  er_playback(pPag
114ad 65 72 2c 20 31 29 3b 0a 20 20 20 20 20 20 69 66  er, 1);.      if
114ae 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
114af 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
114b0 70 61 67 65 72 5f 65 72 72 6f 72 28 70 50 61 67  pager_error(pPag
114b1 65 72 2c 20 72 63 29 3b 0a 20 20 20 20 20 20 20  er, rc);.       
114b2 20 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20   goto failed;.  
114b3 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73 65      }.      asse
114b4 72 74 28 70 50 61 67 65 72 2d 3e 73 74 61 74 65  rt(pPager->state
114b5 3d 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20 7c  ==PAGER_SHARED |
114b6 7c 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 50  | .          (pP
114b7 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76 65 4d  ager->exclusiveM
114b8 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d 3e 73  ode && pPager->s
114b9 74 61 74 65 3e 50 41 47 45 52 5f 53 48 41 52 45  tate>PAGER_SHARE
114ba 44 29 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  D).      );.    
114bb 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  }..    if( sqlit
114bc 65 33 50 63 61 63 68 65 50 61 67 65 63 6f 75 6e  e3PcachePagecoun
114bd 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
114be 65 29 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a  e)>0 ){.      /*
114bf 20 54 68 65 20 73 68 61 72 65 64 2d 6c 6f 63 6b   The shared-lock
114c0 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 61   has just been a
114c1 63 71 75 69 72 65 64 20 6f 6e 20 74 68 65 20 64  cquired on the d
114c2 61 74 61 62 61 73 65 20 66 69 6c 65 0a 20 20 20  atabase file.   
114c3 20 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20     ** and there 
114c4 61 72 65 20 61 6c 72 65 61 64 79 20 70 61 67 65  are already page
114c5 73 20 69 6e 20 74 68 65 20 63 61 63 68 65 20 28  s in the cache (
114c6 66 72 6f 6d 20 61 20 70 72 65 76 69 6f 75 73 0a  from a previous.
114c7 20 20 20 20 20 20 2a 2a 20 72 65 61 64 20 6f 72        ** read or
114c8 20 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69   write transacti
114c9 6f 6e 29 2e 20 20 43 68 65 63 6b 20 74 6f 20 73  on).  Check to s
114ca 65 65 20 69 66 20 74 68 65 20 64 61 74 61 62 61  ee if the databa
114cb 73 65 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20  se.      ** has 
114cc 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 2e 20 20  been modified.  
114cd 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
114ce 68 61 73 20 63 68 61 6e 67 65 64 2c 20 66 6c 75  has changed, flu
114cf 73 68 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  sh the.      ** 
114d0 63 61 63 68 65 2e 0a 20 20 20 20 20 20 2a 2a 0a  cache..      **.
114d1 20 20 20 20 20 20 2a 2a 20 44 61 74 61 62 61 73        ** Databas
114d2 65 20 63 68 61 6e 67 65 73 20 69 73 20 64 65 74  e changes is det
114d3 65 63 74 65 64 20 62 79 20 6c 6f 6f 6b 69 6e 67  ected by looking
114d4 20 61 74 20 31 35 20 62 79 74 65 73 20 62 65 67   at 15 bytes beg
114d5 69 6e 6e 69 6e 67 0a 20 20 20 20 20 20 2a 2a 20  inning.      ** 
114d6 61 74 20 6f 66 66 73 65 74 20 32 34 20 69 6e 74  at offset 24 int
114d7 6f 20 74 68 65 20 66 69 6c 65 2e 20 20 54 68 65  o the file.  The
114d8 20 66 69 72 73 74 20 34 20 6f 66 20 74 68 65 73   first 4 of thes
114d9 65 20 31 36 20 62 79 74 65 73 20 61 72 65 0a 20  e 16 bytes are. 
114da 20 20 20 20 20 2a 2a 20 61 20 33 32 2d 62 69 74       ** a 32-bit
114db 20 63 6f 75 6e 74 65 72 20 74 68 61 74 20 69 73   counter that is
114dc 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77 69 74   incremented wit
114dd 68 20 65 61 63 68 20 63 68 61 6e 67 65 2e 20 20  h each change.  
114de 54 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f 74 68  The.      ** oth
114df 65 72 20 62 79 74 65 73 20 63 68 61 6e 67 65 20  er bytes change 
114e0 72 61 6e 64 6f 6d 6c 79 20 77 69 74 68 20 65 61  randomly with ea
114e1 63 68 20 66 69 6c 65 20 63 68 61 6e 67 65 20 77  ch file change w
114e2 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 61 20 63  hen.      ** a c
114e3 6f 64 65 63 20 69 73 20 69 6e 20 75 73 65 2e 0a  odec is in use..
114e4 20 20 20 20 20 20 2a 2a 20 0a 20 20 20 20 20 20        ** .      
114e5 2a 2a 20 54 68 65 72 65 20 69 73 20 61 20 76 61  ** There is a va
114e6 6e 69 73 68 69 6e 67 6c 79 20 73 6d 61 6c 6c 20  nishingly small 
114e7 63 68 61 6e 63 65 20 74 68 61 74 20 61 20 63 68  chance that a ch
114e8 61 6e 67 65 20 77 69 6c 6c 20 6e 6f 74 20 62 65  ange will not be
114e9 20 0a 20 20 20 20 20 20 2a 2a 20 64 65 74 65 63   .      ** detec
114ea 74 65 64 2e 20 20 54 68 65 20 63 68 61 6e 63 65  ted.  The chance
114eb 20 6f 66 20 61 6e 20 75 6e 64 65 74 65 63 74 65   of an undetecte
114ec 64 20 63 68 61 6e 67 65 20 69 73 20 73 6f 20 73  d change is so s
114ed 6d 61 6c 6c 20 74 68 61 74 0a 20 20 20 20 20 20  mall that.      
114ee 2a 2a 20 69 74 20 63 61 6e 20 62 65 20 6e 65 67  ** it can be neg
114ef 6c 65 63 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f  lected..      */
114f0 0a 20 20 20 20 20 20 63 68 61 72 20 64 62 46 69  .      char dbFi
114f1 6c 65 56 65 72 73 5b 73 69 7a 65 6f 66 28 70 50  leVers[sizeof(pP
114f2 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72 73  ager->dbFileVers
114f3 29 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  )];.      sqlite
114f4 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
114f5 70 50 61 67 65 72 2c 20 30 29 3b 0a 0a 20 20 20  pPager, 0);..   
114f6 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65     if( pPager->e
114f7 72 72 43 6f 64 65 20 29 7b 0a 20 20 20 20 20 20  rrCode ){.      
114f8 20 20 72 63 20 3d 20 70 50 61 67 65 72 2d 3e 65    rc = pPager->e
114f9 72 72 43 6f 64 65 3b 0a 20 20 20 20 20 20 20 20  rrCode;.        
114fa 67 6f 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20  goto failed;.   
114fb 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65     }..      asse
114fc 72 74 28 20 70 50 61 67 65 72 2d 3e 64 62 53 69  rt( pPager->dbSi
114fd 7a 65 56 61 6c 69 64 20 29 3b 0a 20 20 20 20 20  zeValid );.     
114fe 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 53   if( pPager->dbS
114ff 69 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20  ize>0 ){.       
11500 20 49 4f 54 52 41 43 45 28 28 22 43 4b 56 45 52   IOTRACE(("CKVER
11501 53 20 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67  S %p %d\n", pPag
11502 65 72 2c 20 73 69 7a 65 6f 66 28 64 62 46 69 6c  er, sizeof(dbFil
11503 65 56 65 72 73 29 29 29 3b 0a 20 20 20 20 20 20  eVers)));.      
11504 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11505 52 65 61 64 28 70 50 61 67 65 72 2d 3e 66 64 2c  Read(pPager->fd,
11506 20 26 64 62 46 69 6c 65 56 65 72 73 2c 20 73 69   &dbFileVers, si
11507 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73 29  zeof(dbFileVers)
11508 2c 20 32 34 29 3b 0a 20 20 20 20 20 20 20 20 69  , 24);.        i
11509 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
1150a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
1150b 74 6f 20 66 61 69 6c 65 64 3b 0a 20 20 20 20 20  to failed;.     
1150c 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65     }.      }else
1150d 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74  {.        memset
1150e 28 64 62 46 69 6c 65 56 65 72 73 2c 20 30 2c 20  (dbFileVers, 0, 
1150f 73 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72  sizeof(dbFileVer
11510 73 29 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  s));.      }..  
11511 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28 70      if( memcmp(p
11512 50 61 67 65 72 2d 3e 64 62 46 69 6c 65 56 65 72  Pager->dbFileVer
11513 73 2c 20 64 62 46 69 6c 65 56 65 72 73 2c 20 73  s, dbFileVers, s
11514 69 7a 65 6f 66 28 64 62 46 69 6c 65 56 65 72 73  izeof(dbFileVers
11515 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  ))!=0 ){.       
11516 20 70 61 67 65 72 5f 72 65 73 65 74 28 70 50 61   pager_reset(pPa
11517 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ger);.      }.  
11518 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
11519 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73 69 76  pPager->exclusiv
1151a 65 4d 6f 64 65 20 7c 7c 20 70 50 61 67 65 72 2d  eMode || pPager-
1151b 3e 73 74 61 74 65 3c 3d 50 41 47 45 52 5f 53 48  >state<=PAGER_SH
1151c 41 52 45 44 20 29 3b 0a 20 20 20 20 69 66 28 20  ARED );.    if( 
1151d 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3d 3d 50  pPager->state==P
1151e 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 7b 0a 20  AGER_UNLOCK ){. 
1151f 20 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 61       pPager->sta
11520 74 65 20 3d 20 50 41 47 45 52 5f 53 48 41 52 45  te = PAGER_SHARE
11521 44 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 66  D;.    }.  }.. f
11522 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 72 63 21  ailed:.  if( rc!
11523 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11524 20 20 2f 2a 20 70 61 67 65 72 5f 75 6e 6c 6f 63    /* pager_unloc
11525 6b 28 29 20 69 73 20 61 20 6e 6f 2d 6f 70 20 66  k() is a no-op f
11526 6f 72 20 65 78 63 6c 75 73 69 76 65 20 6d 6f 64  or exclusive mod
11527 65 20 61 6e 64 20 69 6e 2d 6d 65 6d 6f 72 79 20  e and in-memory 
11528 64 61 74 61 62 61 73 65 73 2e 20 2a 2f 0a 20 20  databases. */.  
11529 20 20 70 61 67 65 72 5f 75 6e 6c 6f 63 6b 28 70    pager_unlock(p
1152a 50 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 72 65  Pager);.  }.  re
1152b 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1152c 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68  * Make sure we h
1152d 61 76 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  ave the content 
1152e 66 6f 72 20 61 20 70 61 67 65 2e 20 20 49 66 20  for a page.  If 
1152f 74 68 65 20 70 61 67 65 20 77 61 73 0a 2a 2a 20  the page was.** 
11530 70 72 65 76 69 6f 75 73 6c 79 20 61 63 71 75 69  previously acqui
11531 72 65 64 20 77 69 74 68 20 6e 6f 43 6f 6e 74 65  red with noConte
11532 6e 74 3d 3d 31 2c 20 74 68 65 6e 20 74 68 65 20  nt==1, then the 
11533 63 6f 6e 74 65 6e 74 20 77 61 73 0a 2a 2a 20 6a  content was.** j
11534 75 73 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 20  ust initialized 
11535 74 6f 20 7a 65 72 6f 73 20 69 6e 73 74 65 61 64  to zeros instead
11536 20 6f 66 20 62 65 69 6e 67 20 72 65 61 64 20 66   of being read f
11537 72 6f 6d 20 64 69 73 6b 2e 0a 2a 2a 20 42 75 74  rom disk..** But
11538 20 6e 6f 77 20 77 65 20 6e 65 65 64 20 74 68 65   now we need the
11539 20 72 65 61 6c 20 64 61 74 61 20 6f 66 66 20 6f   real data off o
1153a 66 20 64 69 73 6b 2e 20 20 53 6f 20 6d 61 6b 65  f disk.  So make
1153b 20 73 75 72 65 20 77 65 0a 2a 2a 20 68 61 76 65   sure we.** have
1153c 20 69 74 2e 20 20 52 65 61 64 20 69 74 20 69 6e   it.  Read it in
1153d 20 69 66 20 77 65 20 64 6f 20 6e 6f 74 20 68 61   if we do not ha
1153e 76 65 20 69 74 20 61 6c 72 65 61 64 79 2e 0a 2a  ve it already..*
1153f 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70 61 67  /.static int pag
11540 65 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 50  er_get_content(P
11541 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 69 66  gHdr *pPg){.  if
11542 28 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48  ( pPg->flags&PGH
11543 44 52 5f 4e 45 45 44 5f 52 45 41 44 20 29 7b 0a  DR_NEED_READ ){.
11544 20 20 20 20 69 6e 74 20 72 63 20 3d 20 72 65 61      int rc = rea
11545 64 44 62 50 61 67 65 28 70 50 67 2d 3e 70 50 61  dDbPage(pPg->pPa
11546 67 65 72 2c 20 70 50 67 2c 20 70 50 67 2d 3e 70  ger, pPg, pPg->p
11547 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28 20 72 63  gno);.    if( rc
11548 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
11549 20 20 20 20 20 70 50 67 2d 3e 66 6c 61 67 73 20       pPg->flags 
1154a 26 3d 20 7e 50 47 48 44 52 5f 4e 45 45 44 5f 52  &= ~PGHDR_NEED_R
1154b 45 41 44 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  EAD;.    }else{.
1154c 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
1154d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
1154e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1154f 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 72  ../*.** If the r
11550 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 68  eference count h
11551 61 73 20 72 65 61 63 68 65 64 20 7a 65 72 6f 2c  as reached zero,
11552 20 61 6e 64 20 74 68 65 20 70 61 67 65 72 20 69   and the pager i
11553 73 20 6e 6f 74 20 69 6e 20 74 68 65 0a 2a 2a 20  s not in the.** 
11554 6d 69 64 64 6c 65 20 6f 66 20 61 20 77 72 69 74  middle of a writ
11555 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 72  e transaction or
11556 20 6f 70 65 6e 65 64 20 69 6e 20 65 78 63 6c 75   opened in exclu
11557 73 69 76 65 20 6d 6f 64 65 2c 20 75 6e 6c 6f 63  sive mode, unloc
11558 6b 20 69 74 2e 0a 2a 2f 20 0a 73 74 61 74 69 63  k it..*/ .static
11559 20 76 6f 69 64 20 70 61 67 65 72 55 6e 6c 6f 63   void pagerUnloc
1155a 6b 49 66 55 6e 75 73 65 64 28 50 61 67 65 72 20  kIfUnused(Pager 
1155b 2a 70 50 61 67 65 72 29 7b 0a 20 20 69 66 28 20  *pPager){.  if( 
1155c 28 73 71 6c 69 74 65 33 50 63 61 63 68 65 52 65  (sqlite3PcacheRe
1155d 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d 3e 70  fCount(pPager->p
1155e 50 43 61 63 68 65 29 3d 3d 30 29 0a 20 20 20 20  PCache)==0).    
1155f 26 26 20 28 21 70 50 61 67 65 72 2d 3e 65 78 63  && (!pPager->exc
11560 6c 75 73 69 76 65 4d 6f 64 65 20 7c 7c 20 70 50  lusiveMode || pP
11561 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66  ager->journalOff
11562 3e 30 29 20 0a 20 20 29 7b 0a 20 20 20 20 70 61  >0) .  ){.    pa
11563 67 65 72 55 6e 6c 6f 63 6b 41 6e 64 52 6f 6c 6c  gerUnlockAndRoll
11564 62 61 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20  back(pPager);.  
11565 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20  }.}../*.** Drop 
11566 61 20 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20  a page from the 
11567 63 61 63 68 65 20 75 73 69 6e 67 20 73 71 6c 69  cache using sqli
11568 74 65 33 50 63 61 63 68 65 44 72 6f 70 28 29 2e  te3PcacheDrop().
11569 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d  .**.** If this m
1156a 65 61 6e 73 20 74 68 65 72 65 20 61 72 65 20 6e  eans there are n
1156b 6f 77 20 6e 6f 20 70 61 67 65 73 20 77 69 74 68  ow no pages with
1156c 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
1156d 68 65 6d 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 0a  hem, a rollback.
1156e 2a 2a 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68  ** occurs and th
1156f 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61  e lock on the da
11570 74 61 62 61 73 65 20 69 73 20 72 65 6d 6f 76 65  tabase is remove
11571 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  d..*/.static voi
11572 64 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28  d pagerDropPage(
11573 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20 20  DbPage *pPg){.  
11574 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
11575 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 73  pPg->pPager;.  s
11576 71 6c 69 74 65 33 50 63 61 63 68 65 44 72 6f 70  qlite3PcacheDrop
11577 28 70 50 67 29 3b 0a 20 20 70 61 67 65 72 55 6e  (pPg);.  pagerUn
11578 6c 6f 63 6b 49 66 55 6e 75 73 65 64 28 70 50 61  lockIfUnused(pPa
11579 67 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  ger);.}../*.** A
1157a 63 71 75 69 72 65 20 61 20 70 61 67 65 2e 0a 2a  cquire a page..*
1157b 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6c 6f 63 6b  *.** A read lock
1157c 20 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c   on the disk fil
1157d 65 20 69 73 20 6f 62 74 61 69 6e 65 64 20 77 68  e is obtained wh
1157e 65 6e 20 74 68 65 20 66 69 72 73 74 20 70 61 67  en the first pag
1157f 65 20 69 73 20 61 63 71 75 69 72 65 64 2e 20 0a  e is acquired. .
11580 2a 2a 20 54 68 69 73 20 72 65 61 64 20 6c 6f 63  ** This read loc
11581 6b 20 69 73 20 64 72 6f 70 70 65 64 20 77 68 65  k is dropped whe
11582 6e 20 74 68 65 20 6c 61 73 74 20 70 61 67 65 20  n the last page 
11583 69 73 20 72 65 6c 65 61 73 65 64 2e 0a 2a 2a 0a  is released..**.
11584 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
11585 77 6f 72 6b 73 20 66 6f 72 20 61 6e 79 20 70 61  works for any pa
11586 67 65 20 6e 75 6d 62 65 72 20 67 72 65 61 74 65  ge number greate
11587 72 20 74 68 61 6e 20 30 2e 20 20 49 66 20 74 68  r than 0.  If th
11588 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66 69  e database.** fi
11589 6c 65 20 69 73 20 73 6d 61 6c 6c 65 72 20 74 68  le is smaller th
1158a 61 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64  an the requested
1158b 20 70 61 67 65 2c 20 74 68 65 6e 20 6e 6f 20 61   page, then no a
1158c 63 74 75 61 6c 20 64 69 73 6b 0a 2a 2a 20 72 65  ctual disk.** re
1158d 61 64 20 6f 63 63 75 72 73 20 61 6e 64 20 74 68  ad occurs and th
1158e 65 20 6d 65 6d 6f 72 79 20 69 6d 61 67 65 20 6f  e memory image o
1158f 66 20 74 68 65 20 70 61 67 65 20 69 73 20 69 6e  f the page is in
11590 69 74 69 61 6c 69 7a 65 64 20 74 6f 0a 2a 2a 20  itialized to.** 
11591 61 6c 6c 20 7a 65 72 6f 73 2e 20 20 54 68 65 20  all zeros.  The 
11592 65 78 74 72 61 20 64 61 74 61 20 61 70 70 65 6e  extra data appen
11593 64 65 64 20 74 6f 20 61 20 70 61 67 65 20 69 73  ded to a page is
11594 20 61 6c 77 61 79 73 20 69 6e 69 74 69 61 6c 69   always initiali
11595 7a 65 64 0a 2a 2a 20 74 6f 20 7a 65 72 6f 73 20  zed.** to zeros 
11596 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 61  the first time a
11597 20 70 61 67 65 20 69 73 20 6c 6f 61 64 65 64 20   page is loaded 
11598 69 6e 74 6f 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a  into memory..**.
11599 2a 2a 20 54 68 65 20 61 63 71 75 69 73 69 74 69  ** The acquisiti
1159a 6f 6e 20 6d 69 67 68 74 20 66 61 69 6c 20 66 6f  on might fail fo
1159b 72 20 73 65 76 65 72 61 6c 20 72 65 61 73 6f 6e  r several reason
1159c 73 2e 20 20 49 6e 20 61 6c 6c 20 63 61 73 65 73  s.  In all cases
1159d 2c 0a 2a 2a 20 61 6e 20 61 70 70 72 6f 70 72 69  ,.** an appropri
1159e 61 74 65 20 65 72 72 6f 72 20 63 6f 64 65 20 69  ate error code i
1159f 73 20 72 65 74 75 72 6e 65 64 20 61 6e 64 20 2a  s returned and *
115a0 70 70 50 61 67 65 20 69 73 20 73 65 74 20 74 6f  ppPage is set to
115a1 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   NULL..**.** See
115a2 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61 67   also sqlite3Pag
115a3 65 72 4c 6f 6f 6b 75 70 28 29 2e 20 20 42 6f 74  erLookup().  Bot
115a4 68 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  h this routine a
115a5 6e 64 20 4c 6f 6f 6b 75 70 28 29 20 61 74 74 65  nd Lookup() atte
115a6 6d 70 74 0a 2a 2a 20 74 6f 20 66 69 6e 64 20 61  mpt.** to find a
115a7 20 70 61 67 65 20 69 6e 20 74 68 65 20 69 6e 2d   page in the in-
115a8 6d 65 6d 6f 72 79 20 63 61 63 68 65 20 66 69 72  memory cache fir
115a9 73 74 2e 20 20 49 66 20 74 68 65 20 70 61 67 65  st.  If the page
115aa 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 0a   is not already.
115ab 2a 2a 20 69 6e 20 6d 65 6d 6f 72 79 2c 20 74 68  ** in memory, th
115ac 69 73 20 72 6f 75 74 69 6e 65 20 67 6f 65 73 20  is routine goes 
115ad 74 6f 20 64 69 73 6b 20 74 6f 20 72 65 61 64 20  to disk to read 
115ae 69 74 20 69 6e 20 77 68 65 72 65 61 73 20 4c 6f  it in whereas Lo
115af 6f 6b 75 70 28 29 0a 2a 2a 20 6a 75 73 74 20 72  okup().** just r
115b0 65 74 75 72 6e 73 20 30 2e 20 20 54 68 69 73 20  eturns 0.  This 
115b1 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72 65 73  routine acquires
115b2 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 74 68 65   a read-lock the
115b3 20 66 69 72 73 74 20 74 69 6d 65 20 69 74 0a 2a   first time it.*
115b4 2a 20 68 61 73 20 74 6f 20 67 6f 20 74 6f 20 64  * has to go to d
115b5 69 73 6b 2c 20 61 6e 64 20 63 6f 75 6c 64 20 61  isk, and could a
115b6 6c 73 6f 20 70 6c 61 79 62 61 63 6b 20 61 6e 20  lso playback an 
115b7 6f 6c 64 20 6a 6f 75 72 6e 61 6c 20 69 66 20 6e  old journal if n
115b8 65 63 65 73 73 61 72 79 2e 0a 2a 2a 20 53 69 6e  ecessary..** Sin
115b9 63 65 20 4c 6f 6f 6b 75 70 28 29 20 6e 65 76 65  ce Lookup() neve
115ba 72 20 67 6f 65 73 20 74 6f 20 64 69 73 6b 2c 20  r goes to disk, 
115bb 69 74 20 6e 65 76 65 72 20 68 61 73 20 74 6f 20  it never has to 
115bc 64 65 61 6c 20 77 69 74 68 20 6c 6f 63 6b 73 0a  deal with locks.
115bd 2a 2a 20 6f 72 20 6a 6f 75 72 6e 61 6c 20 66 69  ** or journal fi
115be 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f  les..**.** If no
115bf 43 6f 6e 74 65 6e 74 20 69 73 20 66 61 6c 73 65  Content is false
115c0 2c 20 74 68 65 20 70 61 67 65 20 63 6f 6e 74 65  , the page conte
115c1 6e 74 73 20 61 72 65 20 61 63 74 75 61 6c 6c 79  nts are actually
115c2 20 72 65 61 64 20 66 72 6f 6d 20 64 69 73 6b 2e   read from disk.
115c3 0a 2a 2a 20 49 66 20 6e 6f 43 6f 6e 74 65 6e 74  .** If noContent
115c4 20 69 73 20 74 72 75 65 2c 20 69 74 20 6d 65 61   is true, it mea
115c5 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f  ns that we do no
115c6 74 20 63 61 72 65 20 61 62 6f 75 74 20 74 68 65  t care about the
115c7 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20   contents.** of 
115c8 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69 73  the page at this
115c9 20 74 69 6d 65 2c 20 73 6f 20 64 6f 20 6e 6f 74   time, so do not
115ca 20 64 6f 20 61 20 64 69 73 6b 20 72 65 61 64 2e   do a disk read.
115cb 20 20 4a 75 73 74 20 66 69 6c 6c 20 69 6e 20 74    Just fill in t
115cc 68 65 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 65  he.** page conte
115cd 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 2e 20 20  nt with zeros.  
115ce 42 75 74 20 6d 61 72 6b 20 74 68 65 20 66 61 63  But mark the fac
115cf 74 20 74 68 61 74 20 77 65 20 68 61 76 65 20 6e  t that we have n
115d0 6f 74 20 72 65 61 64 20 74 68 65 0a 2a 2a 20 63  ot read the.** c
115d1 6f 6e 74 65 6e 74 20 62 79 20 73 65 74 74 69 6e  ontent by settin
115d2 67 20 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64  g the PgHdr.need
115d3 52 65 61 64 20 66 6c 61 67 2e 20 20 4c 61 74 65  Read flag.  Late
115d4 72 20 6f 6e 2c 20 69 66 20 0a 2a 2a 20 73 71 6c  r on, if .** sql
115d5 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
115d6 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 74 68   is called on th
115d7 69 73 20 70 61 67 65 20 6f 72 20 69 66 20 74 68  is page or if th
115d8 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a 2a  is routine is.**
115d9 20 63 61 6c 6c 65 64 20 61 67 61 69 6e 20 77 69   called again wi
115da 74 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d 30 2c  th noContent==0,
115db 20 74 68 61 74 20 6d 65 61 6e 73 20 74 68 61 74   that means that
115dc 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 69 73 20   the content is 
115dd 6e 65 65 64 65 64 0a 2a 2a 20 61 6e 64 20 74 68  needed.** and th
115de 65 20 64 69 73 6b 20 72 65 61 64 20 73 68 6f 75  e disk read shou
115df 6c 64 20 6f 63 63 75 72 20 61 74 20 74 68 61 74  ld occur at that
115e0 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54   point..*/.SQLIT
115e1 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
115e2 6c 69 74 65 33 50 61 67 65 72 41 63 71 75 69 72  lite3PagerAcquir
115e3 65 28 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67  e(.  Pager *pPag
115e4 65 72 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  er,      /* The 
115e5 70 61 67 65 72 20 6f 70 65 6e 20 6f 6e 20 74 68  pager open on th
115e6 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
115e7 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20  */.  Pgno pgno, 
115e8 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
115e9 20 6e 75 6d 62 65 72 20 74 6f 20 66 65 74 63 68   number to fetch
115ea 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 2a 70   */.  DbPage **p
115eb 70 50 61 67 65 2c 20 20 20 20 2f 2a 20 57 72 69  pPage,    /* Wri
115ec 74 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  te a pointer to 
115ed 74 68 65 20 70 61 67 65 20 68 65 72 65 20 2a 2f  the page here */
115ee 0a 20 20 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74  .  int noContent
115ef 20 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74         /* Do not
115f0 20 62 6f 74 68 65 72 20 72 65 61 64 69 6e 67 20   bother reading 
115f1 63 6f 6e 74 65 6e 74 20 66 72 6f 6d 20 64 69 73  content from dis
115f2 6b 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a  k if true */.){.
115f3 20 20 50 67 48 64 72 20 2a 70 50 67 20 3d 20 30    PgHdr *pPg = 0
115f4 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  ;.  int rc;..  a
115f5 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
115f6 74 61 74 65 3d 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate==PAGER_UNLO
115f7 43 4b 20 0a 20 20 20 20 20 20 20 7c 7c 20 73 71  CK .       || sq
115f8 6c 69 74 65 33 50 63 61 63 68 65 52 65 66 43 6f  lite3PcacheRefCo
115f9 75 6e 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61  unt(pPager->pPCa
115fa 63 68 65 29 3e 30 20 0a 20 20 20 20 20 20 20 7c  che)>0 .       |
115fb 7c 20 70 67 6e 6f 3d 3d 31 0a 20 20 29 3b 0a 0a  | pgno==1.  );..
115fc 20 20 2f 2a 20 54 68 65 20 6d 61 78 69 6d 75 6d    /* The maximum
115fd 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20   page number is 
115fe 32 5e 33 31 2e 20 52 65 74 75 72 6e 20 53 51 4c  2^31. Return SQL
115ff 49 54 45 5f 43 4f 52 52 55 50 54 20 69 66 20 61  ITE_CORRUPT if a
11600 20 70 61 67 65 0a 20 20 2a 2a 20 6e 75 6d 62 65   page.  ** numbe
11601 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
11602 68 69 73 2c 20 6f 72 20 7a 65 72 6f 2c 20 69 73  his, or zero, is
11603 20 72 65 71 75 65 73 74 65 64 2e 0a 20 20 2a 2f   requested..  */
11604 0a 20 20 69 66 28 20 70 67 6e 6f 3e 50 41 47 45  .  if( pgno>PAGE
11605 52 5f 4d 41 58 5f 50 47 4e 4f 20 7c 7c 20 70 67  R_MAX_PGNO || pg
11606 6e 6f 3d 3d 30 20 7c 7c 20 70 67 6e 6f 3d 3d 50  no==0 || pgno==P
11607 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
11608 67 65 72 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ger) ){.    retu
11609 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1160a 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20 20 2f  T_BKPT;.  }..  /
1160b 2a 20 4d 61 6b 65 20 73 75 72 65 20 77 65 20 68  * Make sure we h
1160c 61 76 65 20 6e 6f 74 20 68 69 74 20 61 6e 79 20  ave not hit any 
1160d 63 72 69 74 69 63 61 6c 20 65 72 72 6f 72 73 2e  critical errors.
1160e 0a 20 20 2a 2f 20 0a 20 20 61 73 73 65 72 74 28  .  */ .  assert(
1160f 20 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20   pPager!=0 );.  
11610 2a 70 70 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20  *ppPage = 0;..  
11611 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 74 68  /* If this is th
11612 65 20 66 69 72 73 74 20 70 61 67 65 20 61 63 63  e first page acc
11613 65 73 73 65 64 2c 20 74 68 65 6e 20 67 65 74 20  essed, then get 
11614 61 20 53 48 41 52 45 44 20 6c 6f 63 6b 0a 20 20  a SHARED lock.  
11615 2a 2a 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ** on the databa
11616 73 65 20 66 69 6c 65 2e 20 70 61 67 65 72 53 68  se file. pagerSh
11617 61 72 65 64 4c 6f 63 6b 28 29 20 69 73 20 61 20  aredLock() is a 
11618 6e 6f 2d 6f 70 20 69 66 20 0a 20 20 2a 2a 20 61  no-op if .  ** a
11619 20 64 61 74 61 62 61 73 65 20 6c 6f 63 6b 20 69   database lock i
1161a 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 0a  s already held..
1161b 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67 65    */.  rc = page
1161c 72 53 68 61 72 65 64 4c 6f 63 6b 28 70 50 61 67  rSharedLock(pPag
1161d 65 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  er);.  if( rc!=S
1161e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1161f 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
11620 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
11621 3e 73 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e  >state!=PAGER_UN
11622 4c 4f 43 4b 20 29 3b 0a 0a 20 20 72 63 20 3d 20  LOCK );..  rc = 
11623 73 71 6c 69 74 65 33 50 63 61 63 68 65 46 65 74  sqlite3PcacheFet
11624 63 68 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63  ch(pPager->pPCac
11625 68 65 2c 20 70 67 6e 6f 2c 20 31 2c 20 26 70 50  he, pgno, 1, &pP
11626 67 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  g);.  if( rc!=SQ
11627 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
11628 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
11629 69 66 28 20 70 50 67 2d 3e 70 50 61 67 65 72 3d  if( pPg->pPager=
1162a 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =0 ){.    /* The
1162b 20 70 61 67 65 72 20 63 61 63 68 65 20 68 61 73   pager cache has
1162c 20 63 72 65 61 74 65 64 20 61 20 6e 65 77 20 70   created a new p
1162d 61 67 65 2e 20 49 74 73 20 63 6f 6e 74 65 6e 74  age. Its content
1162e 20 6e 65 65 64 73 20 74 6f 20 0a 20 20 20 20 2a   needs to .    *
1162f 2a 20 62 65 20 69 6e 69 74 69 61 6c 69 7a 65 64  * be initialized
11630 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
11631 20 6e 4d 61 78 3b 0a 20 20 20 20 50 41 47 45 52   nMax;.    PAGER
11632 5f 49 4e 43 52 28 70 50 61 67 65 72 2d 3e 6e 4d  _INCR(pPager->nM
11633 69 73 73 29 3b 0a 20 20 20 20 70 50 67 2d 3e 70  iss);.    pPg->p
11634 50 61 67 65 72 20 3d 20 70 50 61 67 65 72 3b 0a  Pager = pPager;.
11635 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67 2d 3e      memset(pPg->
11636 70 45 78 74 72 61 2c 20 30 2c 20 70 50 61 67 65  pExtra, 0, pPage
11637 72 2d 3e 6e 45 78 74 72 61 29 3b 0a 0a 20 20 20  r->nExtra);..   
11638 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
11639 65 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67  erPagecount(pPag
1163a 65 72 2c 20 26 6e 4d 61 78 29 3b 0a 20 20 20 20  er, &nMax);.    
1163b 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1163c 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
1163d 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50 67  e3PagerUnref(pPg
1163e 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1163f 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  rc;.    }..    i
11640 66 28 20 6e 4d 61 78 3c 28 69 6e 74 29 70 67 6e  f( nMax<(int)pgn
11641 6f 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 6e 6f  o || MEMDB || no
11642 43 6f 6e 74 65 6e 74 20 29 7b 0a 20 20 20 20 20  Content ){.     
11643 20 69 66 28 20 70 67 6e 6f 3e 70 50 61 67 65 72   if( pgno>pPager
11644 2d 3e 6d 78 50 67 6e 6f 20 29 7b 0a 20 20 20 20  ->mxPgno ){.    
11645 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
11646 55 6e 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20  Unref(pPg);.    
11647 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
11648 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 20 20 7d 0a  E_FULL;.      }.
11649 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70 50 67        memset(pPg
1164a 2d 3e 70 44 61 74 61 2c 20 30 2c 20 70 50 61 67  ->pData, 0, pPag
1164b 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20  er->pageSize);. 
1164c 20 20 20 20 20 69 66 28 20 6e 6f 43 6f 6e 74 65       if( noConte
1164d 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50  nt ){.        pP
1164e 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48 44  g->flags |= PGHD
1164f 52 5f 4e 45 45 44 5f 52 45 41 44 3b 0a 20 20 20  R_NEED_READ;.   
11650 20 20 20 7d 0a 20 20 20 20 20 20 49 4f 54 52 41     }.      IOTRA
11651 43 45 28 28 22 5a 45 52 4f 20 25 70 20 25 64 5c  CE(("ZERO %p %d\
11652 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 67 6e 6f  n", pPager, pgno
11653 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ));.    }else{. 
11654 20 20 20 20 20 72 63 20 3d 20 72 65 61 64 44 62       rc = readDb
11655 50 61 67 65 28 70 50 61 67 65 72 2c 20 70 50 67  Page(pPager, pPg
11656 2c 20 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69  , pgno);.      i
11657 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
11658 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 49   && rc!=SQLITE_I
11659 4f 45 52 52 5f 53 48 4f 52 54 5f 52 45 41 44 20  OERR_SHORT_READ 
1165a 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 73 71  ){.        /* sq
1165b 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
1165c 70 50 67 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20  pPg); */.       
1165d 20 70 61 67 65 72 44 72 6f 70 50 61 67 65 28 70   pagerDropPage(p
1165e 50 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  Pg);.        ret
1165f 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
11660 20 20 20 20 7d 0a 23 69 66 64 65 66 20 53 51 4c      }.#ifdef SQL
11661 49 54 45 5f 43 48 45 43 4b 5f 50 41 47 45 53 0a  ITE_CHECK_PAGES.
11662 20 20 20 20 70 50 67 2d 3e 70 61 67 65 48 61 73      pPg->pageHas
11663 68 20 3d 20 70 61 67 65 72 5f 70 61 67 65 68 61  h = pager_pageha
11664 73 68 28 70 50 67 29 3b 0a 23 65 6e 64 69 66 0a  sh(pPg);.#endif.
11665 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
11666 54 68 65 20 72 65 71 75 65 73 74 65 64 20 70 61  The requested pa
11667 67 65 20 69 73 20 69 6e 20 74 68 65 20 70 61 67  ge is in the pag
11668 65 20 63 61 63 68 65 2e 20 2a 2f 0a 20 20 20 20  e cache. */.    
11669 61 73 73 65 72 74 28 73 71 6c 69 74 65 33 50 63  assert(sqlite3Pc
1166a 61 63 68 65 52 65 66 43 6f 75 6e 74 28 70 50 61  acheRefCount(pPa
1166b 67 65 72 2d 3e 70 50 43 61 63 68 65 29 3e 30 20  ger->pPCache)>0 
1166c 7c 7c 20 70 67 6e 6f 3d 3d 31 29 3b 0a 20 20 20  || pgno==1);.   
1166d 20 50 41 47 45 52 5f 49 4e 43 52 28 70 50 61 67   PAGER_INCR(pPag
1166e 65 72 2d 3e 6e 48 69 74 29 3b 0a 20 20 20 20 69  er->nHit);.    i
1166f 66 28 20 21 6e 6f 43 6f 6e 74 65 6e 74 20 29 7b  f( !noContent ){
11670 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
11671 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70 50  r_get_content(pP
11672 67 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  g);.      if( rc
11673 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
11674 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
11675 67 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  g);.        retu
11676 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20  rn rc;.      }. 
11677 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a 70 70 50     }.  }..  *ppP
11678 61 67 65 20 3d 20 70 50 67 3b 0a 20 20 72 65 74  age = pPg;.  ret
11679 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1167a 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
1167b 61 20 70 61 67 65 20 69 66 20 69 74 20 69 73 20  a page if it is 
1167c 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20 69  already in the i
1167d 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 2e 20  n-memory cache. 
1167e 20 44 6f 0a 2a 2a 20 6e 6f 74 20 72 65 61 64 20   Do.** not read 
1167f 74 68 65 20 70 61 67 65 20 66 72 6f 6d 20 64 69  the page from di
11680 73 6b 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  sk.  Return a po
11681 69 6e 74 65 72 20 74 6f 20 74 68 65 20 70 61 67  inter to the pag
11682 65 2c 0a 2a 2a 20 6f 72 20 30 20 69 66 20 74 68  e,.** or 0 if th
11683 65 20 70 61 67 65 20 69 73 20 6e 6f 74 20 69 6e  e page is not in
11684 20 63 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 53 65   cache..**.** Se
11685 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33 50 61  e also sqlite3Pa
11686 67 65 72 47 65 74 28 29 2e 20 20 54 68 65 20 64  gerGet().  The d
11687 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
11688 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  n this routine.*
11689 2a 20 61 6e 64 20 73 71 6c 69 74 65 33 50 61 67  * and sqlite3Pag
1168a 65 72 47 65 74 28 29 20 69 73 20 74 68 61 74 20  erGet() is that 
1168b 5f 67 65 74 28 29 20 77 69 6c 6c 20 67 6f 20 74  _get() will go t
1168c 6f 20 74 68 65 20 64 69 73 6b 20 61 6e 64 20 72  o the disk and r
1168d 65 61 64 0a 2a 2a 20 69 6e 20 74 68 65 20 70 61  ead.** in the pa
1168e 67 65 20 69 66 20 74 68 65 20 70 61 67 65 20 69  ge if the page i
1168f 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 69 6e  s not already in
11690 20 63 61 63 68 65 2e 20 20 54 68 69 73 20 72 6f   cache.  This ro
11691 75 74 69 6e 65 0a 2a 2a 20 72 65 74 75 72 6e 73  utine.** returns
11692 20 4e 55 4c 4c 20 69 66 20 74 68 65 20 70 61 67   NULL if the pag
11693 65 20 69 73 20 6e 6f 74 20 69 6e 20 63 61 63 68  e is not in cach
11694 65 20 6f 72 20 69 66 20 61 20 64 69 73 6b 20 49  e or if a disk I
11695 2f 4f 20 65 72 72 6f 72 20 0a 2a 2a 20 68 61 73  /O error .** has
11696 20 65 76 65 72 20 68 61 70 70 65 6e 65 64 2e 0a   ever happened..
11697 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11698 45 20 44 62 50 61 67 65 20 2a 73 71 6c 69 74 65  E DbPage *sqlite
11699 33 50 61 67 65 72 4c 6f 6f 6b 75 70 28 50 61 67  3PagerLookup(Pag
1169a 65 72 20 2a 70 50 61 67 65 72 2c 20 50 67 6e 6f  er *pPager, Pgno
1169b 20 70 67 6e 6f 29 7b 0a 20 20 50 67 48 64 72 20   pgno){.  PgHdr 
1169c 2a 70 50 67 20 3d 20 30 3b 0a 20 20 61 73 73 65  *pPg = 0;.  asse
1169d 72 74 28 20 70 50 61 67 65 72 21 3d 30 20 29 3b  rt( pPager!=0 );
1169e 0a 20 20 61 73 73 65 72 74 28 20 70 67 6e 6f 21  .  assert( pgno!
1169f 3d 30 20 29 3b 0a 0a 20 20 69 66 28 20 28 70 50  =0 );..  if( (pP
116a0 61 67 65 72 2d 3e 73 74 61 74 65 21 3d 50 41 47  ager->state!=PAG
116a1 45 52 5f 55 4e 4c 4f 43 4b 29 0a 20 20 20 26 26  ER_UNLOCK).   &&
116a2 20 28 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64   (pPager->errCod
116a3 65 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  e==SQLITE_OK || 
116a4 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 3d  pPager->errCode=
116a5 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29 0a 20 20  =SQLITE_FULL).  
116a6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  ){.    sqlite3Pc
116a7 61 63 68 65 46 65 74 63 68 28 70 50 61 67 65 72  acheFetch(pPager
116a8 2d 3e 70 50 43 61 63 68 65 2c 20 70 67 6e 6f 2c  ->pPCache, pgno,
116a9 20 30 2c 20 26 70 50 67 29 3b 0a 20 20 7d 0a 0a   0, &pPg);.  }..
116aa 20 20 72 65 74 75 72 6e 20 70 50 67 3b 0a 7d 0a    return pPg;.}.
116ab 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61  ./*.** Release a
116ac 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   page..**.** If 
116ad 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
116ae 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
116af 70 61 67 65 20 64 72 6f 70 20 74 6f 20 7a 65 72  page drop to zer
116b0 6f 2c 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 70  o, then the.** p
116b1 61 67 65 20 69 73 20 61 64 64 65 64 20 74 6f 20  age is added to 
116b2 74 68 65 20 4c 52 55 20 6c 69 73 74 2e 20 20 57  the LRU list.  W
116b3 68 65 6e 20 61 6c 6c 20 72 65 66 65 72 65 6e 63  hen all referenc
116b4 65 73 20 74 6f 20 61 6c 6c 20 70 61 67 65 73 0a  es to all pages.
116b5 2a 2a 20 61 72 65 20 72 65 6c 65 61 73 65 64 2c  ** are released,
116b6 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f 63 63 75   a rollback occu
116b7 72 73 20 61 6e 64 20 74 68 65 20 6c 6f 63 6b 20  rs and the lock 
116b8 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  on the database 
116b9 69 73 0a 2a 2a 20 72 65 6d 6f 76 65 64 2e 0a 2a  is.** removed..*
116ba 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
116bb 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65   int sqlite3Page
116bc 72 55 6e 72 65 66 28 44 62 50 61 67 65 20 2a 70  rUnref(DbPage *p
116bd 50 67 29 7b 0a 20 20 69 66 28 20 70 50 67 20 29  Pg){.  if( pPg )
116be 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70 50 61  {.    Pager *pPa
116bf 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
116c0 72 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63  r;.    sqlite3Pc
116c1 61 63 68 65 52 65 6c 65 61 73 65 28 70 50 67 29  acheRelease(pPg)
116c2 3b 0a 20 20 20 20 70 61 67 65 72 55 6e 6c 6f 63  ;.    pagerUnloc
116c3 6b 49 66 55 6e 75 73 65 64 28 70 50 61 67 65 72  kIfUnused(pPager
116c4 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
116c5 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
116c6 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6a 6f 75  .** Create a jou
116c7 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20 70 50  rnal file for pP
116c8 61 67 65 72 2e 20 20 54 68 65 72 65 20 73 68 6f  ager.  There sho
116c9 75 6c 64 20 61 6c 72 65 61 64 79 20 62 65 20 61  uld already be a
116ca 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6f 72 20   RESERVED.** or 
116cb 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20 6f  EXCLUSIVE lock o
116cc 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
116cd 69 6c 65 20 77 68 65 6e 20 74 68 69 73 20 72 6f  ile when this ro
116ce 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
116cf 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
116d0 4c 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79  LITE_OK if every
116d1 74 68 69 6e 67 2e 20 20 52 65 74 75 72 6e 20 61  thing.  Return a
116d2 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 61 6e 64  n error code and
116d3 20 72 65 6c 65 61 73 65 20 74 68 65 0a 2a 2a 20   release the.** 
116d4 77 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 61 6e  write lock if an
116d5 79 74 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e  ything goes wron
116d6 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  g..*/.static int
116d7 20 70 61 67 65 72 5f 6f 70 65 6e 5f 6a 6f 75 72   pager_open_jour
116d8 6e 61 6c 28 50 61 67 65 72 20 2a 70 50 61 67 65  nal(Pager *pPage
116d9 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 66  r){.  sqlite3_vf
116da 73 20 2a 70 56 66 73 20 3d 20 70 50 61 67 65 72  s *pVfs = pPager
116db 2d 3e 70 56 66 73 3b 0a 20 20 69 6e 74 20 66 6c  ->pVfs;.  int fl
116dc 61 67 73 20 3d 20 28 53 51 4c 49 54 45 5f 4f 50  ags = (SQLITE_OP
116dd 45 4e 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c  EN_READWRITE|SQL
116de 49 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49  ITE_OPEN_EXCLUSI
116df 56 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 43  VE|SQLITE_OPEN_C
116e0 52 45 41 54 45 29 3b 0a 0a 20 20 69 6e 74 20 72  REATE);..  int r
116e1 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  c;.  assert( pPa
116e2 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50 41 47 45  ger->state>=PAGE
116e3 52 5f 52 45 53 45 52 56 45 44 20 29 3b 0a 20 20  R_RESERVED );.  
116e4 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
116e5 75 73 65 4a 6f 75 72 6e 61 6c 20 29 3b 0a 20 20  useJournal );.  
116e6 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
116e7 70 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b  pInJournal==0 );
116e8 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 50  .  sqlite3PagerP
116e9 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65 72 2c  agecount(pPager,
116ea 20 30 29 3b 0a 20 20 70 50 61 67 65 72 2d 3e 70   0);.  pPager->p
116eb 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 73 71 6c 69  InJournal = sqli
116ec 74 65 33 42 69 74 76 65 63 43 72 65 61 74 65 28  te3BitvecCreate(
116ed 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 29 3b  pPager->dbSize);
116ee 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 70  .  if( pPager->p
116ef 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 7b 0a  InJournal==0 ){.
116f0 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
116f1 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20  NOMEM;.    goto 
116f2 66 61 69 6c 65 64 5f 74 6f 5f 6f 70 65 6e 5f 6a  failed_to_open_j
116f3 6f 75 72 6e 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69  ournal;.  }..  i
116f4 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  f( pPager->journ
116f5 61 6c 4f 70 65 6e 3d 3d 30 20 29 7b 0a 20 20 20  alOpen==0 ){.   
116f6 20 69 66 28 20 70 50 61 67 65 72 2d 3e 74 65 6d   if( pPager->tem
116f7 70 46 69 6c 65 20 29 7b 0a 20 20 20 20 20 20 66  pFile ){.      f
116f8 6c 61 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f  lags |= (SQLITE_
116f9 4f 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f  OPEN_DELETEONCLO
116fa 53 45 7c 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54  SE|SQLITE_OPEN_T
116fb 45 4d 50 5f 4a 4f 55 52 4e 41 4c 29 3b 0a 20 20  EMP_JOURNAL);.  
116fc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 66    }else{.      f
116fd 6c 61 67 73 20 7c 3d 20 28 53 51 4c 49 54 45 5f  lags |= (SQLITE_
116fe 4f 50 45 4e 5f 4d 41 49 4e 5f 4a 4f 55 52 4e 41  OPEN_MAIN_JOURNA
116ff 4c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  L);.    }.    if
11700 28 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  ( pPager->journa
11701 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55  lMode==PAGER_JOU
11702 52 4e 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20  RNALMODE_MEMORY 
11703 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
11704 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 70  MemJournalOpen(p
11705 50 61 67 65 72 2d 3e 6a 66 64 29 3b 0a 20 20 20  Pager->jfd);.   
11706 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
11707 4b 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 23 69  K;.    }else{.#i
11708 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  fdef SQLITE_ENAB
11709 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45 0a  LE_ATOMIC_WRITE.
1170a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1170b 65 33 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20  e3JournalOpen(. 
1170c 20 20 20 20 20 20 20 20 20 70 56 66 73 2c 20 70           pVfs, p
1170d 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
1170e 20 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 66 6c   pPager->jfd, fl
1170f 61 67 73 2c 20 6a 72 6e 6c 42 75 66 66 65 72 53  ags, jrnlBufferS
11710 69 7a 65 28 70 50 61 67 65 72 29 0a 20 20 20 20  ize(pPager).    
11711 20 20 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20    );.#else.     
11712 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
11713 70 65 6e 28 70 56 66 73 2c 20 70 50 61 67 65 72  pen(pVfs, pPager
11714 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c 20 70 50 61 67  ->zJournal, pPag
11715 65 72 2d 3e 6a 66 64 2c 20 66 6c 61 67 73 2c 20  er->jfd, flags, 
11716 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d  0);.#endif.    }
11717 0a 20 20 20 20 61 73 73 65 72 74 28 20 72 63 21  .    assert( rc!
11718 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 50  =SQLITE_OK || pP
11719 61 67 65 72 2d 3e 6a 66 64 2d 3e 70 4d 65 74 68  ager->jfd->pMeth
1171a 6f 64 73 20 29 3b 0a 20 20 20 20 70 50 61 67 65  ods );.    pPage
1171b 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 3d 20  r->journalOff = 
1171c 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73  0;.    pPager->s
1171d 65 74 4d 61 73 74 65 72 20 3d 20 30 3b 0a 20 20  etMaster = 0;.  
1171e 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
1171f 6c 48 64 72 20 3d 20 30 3b 0a 20 20 20 20 69 66  lHdr = 0;.    if
11720 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11721 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  ){.      if( rc=
11722 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b  =SQLITE_NOMEM ){
11723 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
11724 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c 20 70  OsDelete(pVfs, p
11725 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61 6c 2c  Pager->zJournal,
11726 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   0);.      }.   
11727 20 20 20 67 6f 74 6f 20 66 61 69 6c 65 64 5f 74     goto failed_t
11728 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 3b 0a  o_open_journal;.
11729 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61 67      }.  }.  pPag
1172a 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
1172b 3d 20 31 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6a  = 1;.  pPager->j
1172c 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 3d 20  ournalStarted = 
1172d 30 3b 0a 20 20 70 50 61 67 65 72 2d 3e 6e 65 65  0;.  pPager->nee
1172e 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 70 50 61  dSync = 0;.  pPa
1172f 67 65 72 2d 3e 6e 52 65 63 20 3d 20 30 3b 0a 20  ger->nRec = 0;. 
11730 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72 72   if( pPager->err
11731 43 6f 64 65 20 29 7b 0a 20 20 20 20 72 63 20 3d  Code ){.    rc =
11732 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65   pPager->errCode
11733 3b 0a 20 20 20 20 67 6f 74 6f 20 66 61 69 6c 65  ;.    goto faile
11734 64 5f 74 6f 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61  d_to_open_journa
11735 6c 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d  l;.  }.  pPager-
11736 3e 6f 72 69 67 44 62 53 69 7a 65 20 3d 20 70 50  >origDbSize = pP
11737 61 67 65 72 2d 3e 64 62 53 69 7a 65 3b 0a 0a 20  ager->dbSize;.. 
11738 20 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e   rc = writeJourn
11739 61 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 0a  alHdr(pPager);..
1173a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
1173b 6d 74 41 75 74 6f 6f 70 65 6e 20 26 26 20 72 63  mtAutoopen && rc
1173c 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1173d 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1173e 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28 70 50  agerStmtBegin(pP
1173f 61 67 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28  ager);.  }.  if(
11740 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc!=SQLITE_OK &
11741 26 20 72 63 21 3d 53 51 4c 49 54 45 5f 4e 4f 4d  & rc!=SQLITE_NOM
11742 45 4d 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  EM && rc!=SQLITE
11743 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
11744 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65      rc = pager_e
11745 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70  nd_transaction(p
11746 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 69  Pager, 0);.    i
11747 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11748 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
11749 51 4c 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20  QLITE_FULL;.    
1174a 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  }.  }.  return r
1174b 63 3b 0a 0a 66 61 69 6c 65 64 5f 74 6f 5f 6f 70  c;..failed_to_op
1174c 65 6e 5f 6a 6f 75 72 6e 61 6c 3a 0a 20 20 73 71  en_journal:.  sq
1174d 6c 69 74 65 33 42 69 74 76 65 63 44 65 73 74 72  lite3BitvecDestr
1174e 6f 79 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  oy(pPager->pInJo
1174f 75 72 6e 61 6c 29 3b 0a 20 20 70 50 61 67 65 72  urnal);.  pPager
11750 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 3d 20 30  ->pInJournal = 0
11751 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
11752 0a 0a 2f 2a 0a 2a 2a 20 41 63 71 75 69 72 65 20  ../*.** Acquire 
11753 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20  a write-lock on 
11754 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 54  the database.  T
11755 68 65 20 6c 6f 63 6b 20 69 73 20 72 65 6d 6f 76  he lock is remov
11756 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 61  ed when.** the a
11757 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ny of the follow
11758 69 6e 67 20 68 61 70 70 65 6e 3a 0a 2a 2a 0a 2a  ing happen:.**.*
11759 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 61  *   *  sqlite3Pa
1175a 67 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77  gerCommitPhaseTw
1175b 6f 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a 2a  o() is called..*
1175c 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50 61  *   *  sqlite3Pa
1175d 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 69 73  gerRollback() is
1175e 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 20 20 2a 20   called..**   * 
1175f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6c 6f   sqlite3PagerClo
11760 73 65 28 29 20 69 73 20 63 61 6c 6c 65 64 2e 0a  se() is called..
11761 2a 2a 20 20 20 2a 20 20 73 71 6c 69 74 65 33 50  **   *  sqlite3P
11762 61 67 65 72 55 6e 72 65 66 28 29 20 69 73 20 63  agerUnref() is c
11763 61 6c 6c 65 64 20 74 6f 20 6f 6e 20 65 76 65 72  alled to on ever
11764 79 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 70 61  y outstanding pa
11765 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69  ge..**.** The fi
11766 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rst parameter to
11767 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
11768 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e   a pointer to an
11769 79 20 6f 70 65 6e 20 70 61 67 65 20 6f 66 20 74  y open page of t
1176a 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 66  he.** database f
1176b 69 6c 65 2e 20 20 4e 6f 74 68 69 6e 67 20 63 68  ile.  Nothing ch
1176c 61 6e 67 65 73 20 61 62 6f 75 74 20 74 68 65 20  anges about the 
1176d 70 61 67 65 20 2d 20 69 74 20 69 73 20 75 73 65  page - it is use
1176e 64 20 6d 65 72 65 6c 79 20 74 6f 0a 2a 2a 20 61  d merely to.** a
1176f 63 71 75 69 72 65 20 61 20 70 6f 69 6e 74 65 72  cquire a pointer
11770 20 74 6f 20 74 68 65 20 50 61 67 65 72 20 73 74   to the Pager st
11771 72 75 63 74 75 72 65 20 61 6e 64 20 61 73 20 70  ructure and as p
11772 72 6f 6f 66 20 74 68 61 74 20 74 68 65 72 65 20  roof that there 
11773 69 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 61 20  is.** already a 
11774 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  read-lock on the
11775 20 64 61 74 61 62 61 73 65 2e 0a 2a 2a 0a 2a 2a   database..**.**
11776 20 54 68 65 20 73 65 63 6f 6e 64 20 70 61 72 61   The second para
11777 6d 65 74 65 72 20 69 6e 64 69 63 61 74 65 73 20  meter indicates 
11778 68 6f 77 20 6d 75 63 68 20 73 70 61 63 65 20 69  how much space i
11779 6e 20 62 79 74 65 73 20 74 6f 20 72 65 73 65 72  n bytes to reser
1177a 76 65 20 66 6f 72 20 61 0a 2a 2a 20 6d 61 73 74  ve for a.** mast
1177b 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2d  er journal file-
1177c 6e 61 6d 65 20 61 74 20 74 68 65 20 73 74 61 72  name at the star
1177d 74 20 6f 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c  t of the journal
1177e 20 77 68 65 6e 20 69 74 20 69 73 20 63 72 65 61   when it is crea
1177f 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 6f 75  ted..**.** A jou
11780 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 6f 70 65  rnal file is ope
11781 6e 65 64 20 69 66 20 74 68 69 73 20 69 73 20 6e  ned if this is n
11782 6f 74 20 61 20 74 65 6d 70 6f 72 61 72 79 20 66  ot a temporary f
11783 69 6c 65 2e 20 20 46 6f 72 20 74 65 6d 70 6f 72  ile.  For tempor
11784 61 72 79 0a 2a 2a 20 66 69 6c 65 73 2c 20 74 68  ary.** files, th
11785 65 20 6f 70 65 6e 69 6e 67 20 6f 66 20 74 68 65  e opening of the
11786 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73   journal file is
11787 20 64 65 66 65 72 72 65 64 20 75 6e 74 69 6c 20   deferred until 
11788 74 68 65 72 65 20 69 73 20 61 6e 0a 2a 2a 20 61  there is an.** a
11789 63 74 75 61 6c 20 6e 65 65 64 20 74 6f 20 77 72  ctual need to wr
1178a 69 74 65 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e  ite to the journ
1178b 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  al..**.** If the
1178c 20 64 61 74 61 62 61 73 65 20 69 73 20 61 6c 72   database is alr
1178d 65 61 64 79 20 72 65 73 65 72 76 65 64 20 66 6f  eady reserved fo
1178e 72 20 77 72 69 74 69 6e 67 2c 20 74 68 69 73 20  r writing, this 
1178f 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
11790 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65 78 46  op..**.** If exF
11791 6c 61 67 20 69 73 20 74 72 75 65 2c 20 67 6f 20  lag is true, go 
11792 61 68 65 61 64 20 61 6e 64 20 67 65 74 20 61 6e  ahead and get an
11793 20 45 58 43 4c 55 53 49 56 45 20 6c 6f 63 6b 20   EXCLUSIVE lock 
11794 6f 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a 20 69  on the file.** i
11795 6d 6d 65 64 69 61 74 65 6c 79 20 69 6e 73 74 65  mmediately inste
11796 61 64 20 6f 66 20 77 61 69 74 69 6e 67 20 75 6e  ad of waiting un
11797 74 69 6c 20 77 65 20 74 72 79 20 74 6f 20 66 6c  til we try to fl
11798 75 73 68 20 74 68 65 20 63 61 63 68 65 2e 20 20  ush the cache.  
11799 54 68 65 0a 2a 2a 20 65 78 46 6c 61 67 20 69 73  The.** exFlag is
1179a 20 69 67 6e 6f 72 65 64 20 69 66 20 61 20 74 72   ignored if a tr
1179b 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 6c 72  ansaction is alr
1179c 65 61 64 79 20 61 63 74 69 76 65 2e 0a 2a 2f 0a  eady active..*/.
1179d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1179e 6e 74 20 73 71 6c 69 74 65 33 50 61 67 65 72 42  nt sqlite3PagerB
1179f 65 67 69 6e 28 44 62 50 61 67 65 20 2a 70 50 67  egin(DbPage *pPg
117a0 2c 20 69 6e 74 20 65 78 46 6c 61 67 29 7b 0a 20  , int exFlag){. 
117a1 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
117a2 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
117a3 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
117a4 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50  OK;.  assert( pP
117a5 67 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20 20 61  g->nRef>0 );.  a
117a6 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73  ssert( pPager->s
117a7 74 61 74 65 21 3d 50 41 47 45 52 5f 55 4e 4c 4f  tate!=PAGER_UNLO
117a8 43 4b 20 29 3b 0a 20 20 69 66 28 20 70 50 61 67  CK );.  if( pPag
117a9 65 72 2d 3e 73 74 61 74 65 3d 3d 50 41 47 45 52  er->state==PAGER
117aa 5f 53 48 41 52 45 44 20 29 7b 0a 20 20 20 20 61  _SHARED ){.    a
117ab 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
117ac 49 6e 4a 6f 75 72 6e 61 6c 3d 3d 30 20 29 3b 0a  InJournal==0 );.
117ad 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45 4d      assert( !MEM
117ae 44 42 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 73  DB );.    rc = s
117af 71 6c 69 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61  qlite3OsLock(pPa
117b0 67 65 72 2d 3e 66 64 2c 20 52 45 53 45 52 56 45  ger->fd, RESERVE
117b1 44 5f 4c 4f 43 4b 29 3b 0a 20 20 20 20 69 66 28  D_LOCK);.    if(
117b2 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
117b3 7b 0a 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e  {.      pPager->
117b4 73 74 61 74 65 20 3d 20 50 41 47 45 52 5f 52 45  state = PAGER_RE
117b5 53 45 52 56 45 44 3b 0a 20 20 20 20 20 20 69 66  SERVED;.      if
117b6 28 20 65 78 46 6c 61 67 20 29 7b 0a 20 20 20 20  ( exFlag ){.    
117b7 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 77      rc = pager_w
117b8 61 69 74 5f 6f 6e 5f 6c 6f 63 6b 28 70 50 61 67  ait_on_lock(pPag
117b9 65 72 2c 20 45 58 43 4c 55 53 49 56 45 5f 4c 4f  er, EXCLUSIVE_LO
117ba 43 4b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  CK);.      }.   
117bb 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
117bc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
117bd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
117be 20 7d 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64   }.    pPager->d
117bf 69 72 74 79 43 61 63 68 65 20 3d 20 30 3b 0a 20  irtyCache = 0;. 
117c0 20 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22     PAGERTRACE2("
117c1 54 52 41 4e 53 41 43 54 49 4f 4e 20 25 64 5c 6e  TRANSACTION %d\n
117c2 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67 65  ", PAGERID(pPage
117c3 72 29 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  r));.    if( pPa
117c4 67 65 72 2d 3e 75 73 65 4a 6f 75 72 6e 61 6c 20  ger->useJournal 
117c5 26 26 20 21 70 50 61 67 65 72 2d 3e 74 65 6d 70  && !pPager->temp
117c6 46 69 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20  File.           
117c7 26 26 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  && pPager->journ
117c8 61 6c 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f  alMode!=PAGER_JO
117c9 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b  URNALMODE_OFF ){
117ca 0a 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65  .      rc = page
117cb 72 5f 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70  r_open_journal(p
117cc 50 61 67 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20  Pager);.    }.  
117cd 7d 65 6c 73 65 20 69 66 28 20 70 50 61 67 65 72  }else if( pPager
117ce 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26  ->journalOpen &&
117cf 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
117d0 4f 66 66 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a  Off==0 ){.    /*
117d1 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77 68   This happens wh
117d2 65 6e 20 74 68 65 20 70 61 67 65 72 20 77 61 73  en the pager was
117d3 20 69 6e 20 65 78 63 6c 75 73 69 76 65 2d 61 63   in exclusive-ac
117d4 63 65 73 73 20 6d 6f 64 65 20 74 68 65 20 6c 61  cess mode the la
117d5 73 74 0a 20 20 20 20 2a 2a 20 74 69 6d 65 20 61  st.    ** time a
117d6 20 28 72 65 61 64 20 6f 72 20 77 72 69 74 65 29   (read or write)
117d7 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 61 73   transaction was
117d8 20 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f   successfully co
117d9 6e 63 6c 75 64 65 64 0a 20 20 20 20 2a 2a 20 62  ncluded.    ** b
117da 79 20 74 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f  y this connectio
117db 6e 2e 20 49 6e 73 74 65 61 64 20 6f 66 20 64 65  n. Instead of de
117dc 6c 65 74 69 6e 67 20 74 68 65 20 6a 6f 75 72 6e  leting the journ
117dd 61 6c 20 66 69 6c 65 20 69 74 20 77 61 73 20 0a  al file it was .
117de 20 20 20 20 2a 2a 20 6b 65 70 74 20 6f 70 65 6e      ** kept open
117df 20 61 6e 64 20 65 69 74 68 65 72 20 77 61 73 20   and either was 
117e0 74 72 75 6e 63 61 74 65 64 20 74 6f 20 30 20 62  truncated to 0 b
117e1 79 74 65 73 20 6f 72 20 69 74 73 20 68 65 61 64  ytes or its head
117e2 65 72 20 77 61 73 0a 20 20 20 20 2a 2a 20 6f 76  er was.    ** ov
117e3 65 72 77 72 69 74 74 65 6e 20 77 69 74 68 20 7a  erwritten with z
117e4 65 72 6f 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  eros..    */.   
117e5 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
117e6 3e 6e 52 65 63 3d 3d 30 20 29 3b 0a 20 20 20 20  >nRec==0 );.    
117e7 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e  assert( pPager->
117e8 6f 72 69 67 44 62 53 69 7a 65 3d 3d 30 20 29 3b  origDbSize==0 );
117e9 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61  .    assert( pPa
117ea 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 3d  ger->pInJournal=
117eb 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
117ec 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
117ed 70 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20  pPager, 0);.    
117ee 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e  pPager->pInJourn
117ef 61 6c 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76  al = sqlite3Bitv
117f0 65 63 43 72 65 61 74 65 28 20 70 50 61 67 65 72  ecCreate( pPager
117f1 2d 3e 64 62 53 69 7a 65 20 29 3b 0a 20 20 20 20  ->dbSize );.    
117f2 69 66 28 20 21 70 50 61 67 65 72 2d 3e 70 49 6e  if( !pPager->pIn
117f3 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20 20  Journal ){.     
117f4 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
117f5 45 4d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  EM;.    }else{. 
117f6 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6f 72 69       pPager->ori
117f7 67 44 62 53 69 7a 65 20 3d 20 70 50 61 67 65 72  gDbSize = pPager
117f8 2d 3e 64 62 53 69 7a 65 3b 0a 20 20 20 20 20 20  ->dbSize;.      
117f9 72 63 20 3d 20 77 72 69 74 65 4a 6f 75 72 6e 61  rc = writeJourna
117fa 6c 48 64 72 28 70 50 61 67 65 72 29 3b 0a 20 20  lHdr(pPager);.  
117fb 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65 72 74    }.  }.  assert
117fc 28 20 21 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  ( !pPager->journ
117fd 61 6c 4f 70 65 6e 20 7c 7c 20 70 50 61 67 65 72  alOpen || pPager
117fe 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3e 30 20 7c  ->journalOff>0 |
117ff 7c 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  | rc!=SQLITE_OK 
11800 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
11801 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20  }../*.** Make a 
11802 70 61 67 65 20 64 69 72 74 79 2e 20 20 53 65 74  page dirty.  Set
11803 20 69 74 73 20 64 69 72 74 79 20 66 6c 61 67 20   its dirty flag 
11804 61 6e 64 20 61 64 64 20 69 74 20 74 6f 20 74 68  and add it to th
11805 65 20 64 69 72 74 79 0a 2a 2a 20 70 61 67 65 20  e dirty.** page 
11806 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  list..*/.static 
11807 76 6f 69 64 20 6d 61 6b 65 44 69 72 74 79 28 50  void makeDirty(P
11808 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 73 71  gHdr *pPg){.  sq
11809 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65 44  lite3PcacheMakeD
1180a 69 72 74 79 28 70 50 67 29 3b 0a 7d 0a 0a 2f 2a  irty(pPg);.}../*
1180b 0a 2a 2a 20 4d 61 6b 65 20 61 20 70 61 67 65 20  .** Make a page 
1180c 63 6c 65 61 6e 2e 20 20 43 6c 65 61 72 20 69 74  clean.  Clear it
1180d 73 20 64 69 72 74 79 20 62 69 74 20 61 6e 64 20  s dirty bit and 
1180e 72 65 6d 6f 76 65 20 69 74 20 66 72 6f 6d 20 74  remove it from t
1180f 68 65 0a 2a 2a 20 64 69 72 74 79 20 70 61 67 65  he.** dirty page
11810 20 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63   list..*/.static
11811 20 76 6f 69 64 20 6d 61 6b 65 43 6c 65 61 6e 28   void makeClean(
11812 50 67 48 64 72 20 2a 70 50 67 29 7b 0a 20 20 73  PgHdr *pPg){.  s
11813 71 6c 69 74 65 33 50 63 61 63 68 65 4d 61 6b 65  qlite3PcacheMake
11814 43 6c 65 61 6e 28 70 50 67 29 3b 0a 7d 0a 0a 0a  Clean(pPg);.}...
11815 2f 2a 0a 2a 2a 20 4d 61 72 6b 20 61 20 64 61 74  /*.** Mark a dat
11816 61 20 70 61 67 65 20 61 73 20 77 72 69 74 65 61  a page as writea
11817 62 6c 65 2e 20 20 54 68 65 20 70 61 67 65 20 69  ble.  The page i
11818 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74  s written into t
11819 68 65 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2a 20 69  he journal .** i
1181a 66 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72  f it is not ther
1181b 65 20 61 6c 72 65 61 64 79 2e 20 20 54 68 69 73  e already.  This
1181c 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62 65   routine must be
1181d 20 63 61 6c 6c 65 64 20 62 65 66 6f 72 65 20 6d   called before m
1181e 61 6b 69 6e 67 0a 2a 2a 20 63 68 61 6e 67 65 73  aking.** changes
1181f 20 74 6f 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a   to a page..**.*
11820 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  * The first time
11821 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
11822 20 63 61 6c 6c 65 64 2c 20 74 68 65 20 70 61 67   called, the pag
11823 65 72 20 63 72 65 61 74 65 73 20 61 20 6e 65 77  er creates a new
11824 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 20 61 6e 64 20  .** journal and 
11825 61 63 71 75 69 72 65 73 20 61 20 52 45 53 45 52  acquires a RESER
11826 56 45 44 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  VED lock on the 
11827 64 61 74 61 62 61 73 65 2e 20 20 49 66 20 74 68  database.  If th
11828 65 20 52 45 53 45 52 56 45 44 0a 2a 2a 20 6c 6f  e RESERVED.** lo
11829 63 6b 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  ck could not be 
1182a 61 63 71 75 69 72 65 64 2c 20 74 68 69 73 20 72  acquired, this r
1182b 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53  outine returns S
1182c 51 4c 49 54 45 5f 42 55 53 59 2e 20 20 54 68 65  QLITE_BUSY.  The
1182d 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20 72 6f 75 74  .** calling rout
1182e 69 6e 65 20 6d 75 73 74 20 63 68 65 63 6b 20 66  ine must check f
1182f 6f 72 20 74 68 61 74 20 72 65 74 75 72 6e 20 76  or that return v
11830 61 6c 75 65 20 61 6e 64 20 62 65 20 63 61 72 65  alue and be care
11831 66 75 6c 20 6e 6f 74 20 74 6f 0a 2a 2a 20 63 68  ful not to.** ch
11832 61 6e 67 65 20 61 6e 79 20 70 61 67 65 20 64 61  ange any page da
11833 74 61 20 75 6e 74 69 6c 20 74 68 69 73 20 72 6f  ta until this ro
11834 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53 51  utine returns SQ
11835 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a 2a 20 49  LITE_OK..**.** I
11836 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  f the journal fi
11837 6c 65 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20  le could not be 
11838 77 72 69 74 74 65 6e 20 62 65 63 61 75 73 65 20  written because 
11839 74 68 65 20 64 69 73 6b 20 69 73 20 66 75 6c 6c  the disk is full
1183a 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 72  ,.** then this r
1183b 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 53  outine returns S
1183c 51 4c 49 54 45 5f 46 55 4c 4c 20 61 6e 64 20 64  QLITE_FULL and d
1183d 6f 65 73 20 61 6e 20 69 6d 6d 65 64 69 61 74 65  oes an immediate
1183e 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 41 6c   rollback..** Al
1183f 6c 20 73 75 62 73 65 71 75 65 6e 74 20 77 72 69  l subsequent wri
11840 74 65 20 61 74 74 65 6d 70 74 73 20 61 6c 73 6f  te attempts also
11841 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 46   return SQLITE_F
11842 55 4c 4c 20 75 6e 74 69 6c 20 74 68 65 72 65 0a  ULL until there.
11843 2a 2a 20 69 73 20 61 20 63 61 6c 6c 20 74 6f 20  ** is a call to 
11844 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f 6d 6d  sqlite3PagerComm
11845 69 74 28 29 20 6f 72 20 73 71 6c 69 74 65 33 50  it() or sqlite3P
11846 61 67 65 72 52 6f 6c 6c 62 61 63 6b 28 29 20 74  agerRollback() t
11847 6f 0a 2a 2a 20 72 65 73 65 74 2e 0a 2a 2f 0a 73  o.** reset..*/.s
11848 74 61 74 69 63 20 69 6e 74 20 70 61 67 65 72 5f  tatic int pager_
11849 77 72 69 74 65 28 50 67 48 64 72 20 2a 70 50 67  write(PgHdr *pPg
1184a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 44 61 74 61  ){.  void *pData
1184b 20 3d 20 70 50 67 2d 3e 70 44 61 74 61 3b 0a 20   = pPg->pData;. 
1184c 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d   Pager *pPager =
1184d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20   pPg->pPager;.  
1184e 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
1184f 4f 4b 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  OK;..  /* Check 
11850 66 6f 72 20 65 72 72 6f 72 73 0a 20 20 2a 2f 0a  for errors.  */.
11851 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72    if( pPager->er
11852 72 43 6f 64 65 20 29 7b 20 0a 20 20 20 20 72 65  rCode ){ .    re
11853 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 65 72 72  turn pPager->err
11854 43 6f 64 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  Code;.  }.  if( 
11855 70 50 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79  pPager->readOnly
11856 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
11857 51 4c 49 54 45 5f 50 45 52 4d 3b 0a 20 20 7d 0a  QLITE_PERM;.  }.
11858 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61 67  .  assert( !pPag
11859 65 72 2d 3e 73 65 74 4d 61 73 74 65 72 20 29 3b  er->setMaster );
1185a 0a 0a 20 20 43 48 45 43 4b 5f 50 41 47 45 28 70  ..  CHECK_PAGE(p
1185b 50 67 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  Pg);..  /* If th
1185c 69 73 20 70 61 67 65 20 77 61 73 20 70 72 65 76  is page was prev
1185d 69 6f 75 73 6c 79 20 61 63 71 75 69 72 65 64 20  iously acquired 
1185e 77 69 74 68 20 6e 6f 43 6f 6e 74 65 6e 74 3d 3d  with noContent==
1185f 31 2c 20 74 68 61 74 20 6d 65 61 6e 73 0a 20 20  1, that means.  
11860 2a 2a 20 77 65 20 64 69 64 6e 27 74 20 72 65 61  ** we didn't rea
11861 6c 6c 79 20 72 65 61 64 20 69 6e 20 74 68 65 20  lly read in the 
11862 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70  content of the p
11863 61 67 65 2e 20 20 54 68 69 73 20 63 61 6e 20 68  age.  This can h
11864 61 70 70 65 6e 0a 20 20 2a 2a 20 28 66 6f 72 20  appen.  ** (for 
11865 65 78 61 6d 70 6c 65 29 20 77 68 65 6e 20 74 68  example) when th
11866 65 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20  e page is being 
11867 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 66 72 65  moved to the fre
11868 65 6c 69 73 74 2e 20 20 42 75 74 0a 20 20 2a 2a  elist.  But.  **
11869 20 6e 6f 77 20 77 65 20 61 72 65 20 28 70 65 72   now we are (per
1186a 68 61 70 73 29 20 6d 6f 76 69 6e 67 20 74 68 65  haps) moving the
1186b 20 70 61 67 65 20 6f 66 66 20 6f 66 20 74 68 65   page off of the
1186c 20 66 72 65 65 6c 69 73 74 20 66 6f 72 0a 20 20   freelist for.  
1186d 2a 2a 20 72 65 75 73 65 20 61 6e 64 20 77 65 20  ** reuse and we 
1186e 6e 65 65 64 20 74 6f 20 6b 6e 6f 77 20 69 74 73  need to know its
1186f 20 6f 72 69 67 69 6e 61 6c 20 63 6f 6e 74 65 6e   original conten
11870 74 20 73 6f 20 74 68 61 74 20 63 6f 6e 74 65 6e  t so that conten
11871 74 0a 20 20 2a 2a 20 63 61 6e 20 62 65 20 73 74  t.  ** can be st
11872 6f 72 65 64 20 69 6e 20 74 68 65 20 72 6f 6c 6c  ored in the roll
11873 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 20 53  back journal.  S
11874 6f 20 64 6f 20 74 68 65 20 72 65 61 64 20 61 74  o do the read at
11875 20 74 68 69 73 0a 20 20 2a 2a 20 74 69 6d 65 2e   this.  ** time.
11876 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 67  .  */.  rc = pag
11877 65 72 5f 67 65 74 5f 63 6f 6e 74 65 6e 74 28 70  er_get_content(p
11878 50 67 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b  Pg);.  if( rc ){
11879 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
1187a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 72 6b 20 74    }..  /* Mark t
1187b 68 65 20 70 61 67 65 20 61 73 20 64 69 72 74 79  he page as dirty
1187c 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20 68  .  If the page h
1187d 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20  as already been 
1187e 77 72 69 74 74 65 6e 0a 20 20 2a 2a 20 74 6f 20  written.  ** to 
1187f 74 68 65 20 6a 6f 75 72 6e 61 6c 20 74 68 65 6e  the journal then
11880 20 77 65 20 63 61 6e 20 72 65 74 75 72 6e 20 72   we can return r
11881 69 67 68 74 20 61 77 61 79 2e 0a 20 20 2a 2f 0a  ight away..  */.
11882 20 20 6d 61 6b 65 44 69 72 74 79 28 70 50 67 29    makeDirty(pPg)
11883 3b 0a 20 20 69 66 28 20 70 61 67 65 49 6e 4a 6f  ;.  if( pageInJo
11884 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20 28 70  urnal(pPg) && (p
11885 61 67 65 49 6e 53 74 61 74 65 6d 65 6e 74 28 70  ageInStatement(p
11886 50 67 29 20 7c 7c 20 70 50 61 67 65 72 2d 3e 73  Pg) || pPager->s
11887 74 6d 74 49 6e 55 73 65 3d 3d 30 29 20 29 7b 0a  tmtInUse==0) ){.
11888 20 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72 74      pPager->dirt
11889 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 20 20  yCache = 1;.    
1188a 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66 69  pPager->dbModifi
1188b 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b  ed = 1;.  }else{
1188c 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 67  ..    /* If we g
1188d 65 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20  et this far, it 
1188e 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 70  means that the p
1188f 61 67 65 20 6e 65 65 64 73 20 74 6f 20 62 65 0a  age needs to be.
11890 20 20 20 20 2a 2a 20 77 72 69 74 74 65 6e 20 74      ** written t
11891 6f 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  o the transactio
11892 6e 20 6a 6f 75 72 6e 61 6c 20 6f 72 20 74 68 65  n journal or the
11893 20 63 6b 65 63 6b 70 6f 69 6e 74 20 6a 6f 75 72   ckeckpoint jour
11894 6e 61 6c 0a 20 20 20 20 2a 2a 20 6f 72 20 62 6f  nal.    ** or bo
11895 74 68 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  th..    **.    *
11896 2a 20 46 69 72 73 74 20 63 68 65 63 6b 20 74 6f  * First check to
11897 20 73 65 65 20 74 68 61 74 20 74 68 65 20 74 72   see that the tr
11898 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e 61  ansaction journa
11899 6c 20 65 78 69 73 74 73 20 61 6e 64 0a 20 20 20  l exists and.   
1189a 20 2a 2a 20 63 72 65 61 74 65 20 69 74 20 69 66   ** create it if
1189b 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e 0a 20 20   it does not..  
1189c 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
1189d 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
1189e 50 41 47 45 52 5f 55 4e 4c 4f 43 4b 20 29 3b 0a  PAGER_UNLOCK );.
1189f 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
118a0 50 61 67 65 72 42 65 67 69 6e 28 70 50 67 2c 20  PagerBegin(pPg, 
118a1 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  0);.    if( rc!=
118a2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
118a3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
118a4 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
118a5 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d 50  pPager->state>=P
118a6 41 47 45 52 5f 52 45 53 45 52 56 45 44 20 29 3b  AGER_RESERVED );
118a7 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 72  .    if( !pPager
118a8 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 26 26  ->journalOpen &&
118a9 20 70 50 61 67 65 72 2d 3e 75 73 65 4a 6f 75 72   pPager->useJour
118aa 6e 61 6c 0a 20 20 20 20 20 20 20 20 20 20 26 26  nal.          &&
118ab 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
118ac 4d 6f 64 65 21 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode!=PAGER_JOUR
118ad 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 7b 0a 20  NALMODE_OFF ){. 
118ae 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f       rc = pager_
118af 6f 70 65 6e 5f 6a 6f 75 72 6e 61 6c 28 70 50 61  open_journal(pPa
118b0 67 65 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ger);.      if( 
118b1 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
118b2 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d  return rc;.    }
118b3 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 64 69 72  .    pPager->dir
118b4 74 79 43 61 63 68 65 20 3d 20 31 3b 0a 20 20 20  tyCache = 1;.   
118b5 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f 64 69 66   pPager->dbModif
118b6 69 65 64 20 3d 20 31 3b 0a 20 20 0a 20 20 20 20  ied = 1;.  .    
118b7 2f 2a 20 54 68 65 20 74 72 61 6e 73 61 63 74 69  /* The transacti
118b8 6f 6e 20 6a 6f 75 72 6e 61 6c 20 6e 6f 77 20 65  on journal now e
118b9 78 69 73 74 73 20 61 6e 64 20 77 65 20 68 61 76  xists and we hav
118ba 65 20 61 20 52 45 53 45 52 56 45 44 20 6f 72 20  e a RESERVED or 
118bb 61 6e 0a 20 20 20 20 2a 2a 20 45 58 43 4c 55 53  an.    ** EXCLUS
118bc 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20  IVE lock on the 
118bd 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
118be 6c 65 2e 20 20 57 72 69 74 65 20 74 68 65 20 63  le.  Write the c
118bf 75 72 72 65 6e 74 20 70 61 67 65 20 74 6f 0a 20  urrent page to. 
118c0 20 20 20 2a 2a 20 74 68 65 20 74 72 61 6e 73 61     ** the transa
118c1 63 74 69 6f 6e 20 6a 6f 75 72 6e 61 6c 20 69 66  ction journal if
118c2 20 69 74 20 69 73 20 6e 6f 74 20 74 68 65 72 65   it is not there
118c3 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20 2a 2f   already..    */
118c4 0a 20 20 20 20 69 66 28 20 21 70 61 67 65 49 6e  .    if( !pageIn
118c5 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 26 26 20  Journal(pPg) && 
118c6 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
118c7 70 65 6e 20 29 7b 0a 20 20 20 20 20 20 69 66 28  pen ){.      if(
118c8 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d 70 50 61 67   pPg->pgno<=pPag
118c9 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29  er->origDbSize )
118ca 7b 0a 20 20 20 20 20 20 20 20 75 33 32 20 63 6b  {.        u32 ck
118cb 73 75 6d 3b 0a 20 20 20 20 20 20 20 20 63 68 61  sum;.        cha
118cc 72 20 2a 70 44 61 74 61 32 3b 0a 0a 20 20 20 20  r *pData2;..    
118cd 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64      /* We should
118ce 20 6e 65 76 65 72 20 77 72 69 74 65 20 74 6f 20   never write to 
118cf 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
118d0 20 74 68 65 20 70 61 67 65 20 74 68 61 74 0a 20   the page that. 
118d1 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69         ** contai
118d2 6e 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ns the database 
118d3 6c 6f 63 6b 73 2e 20 20 54 68 65 20 66 6f 6c 6c  locks.  The foll
118d4 6f 77 69 6e 67 20 61 73 73 65 72 74 20 76 65 72  owing assert ver
118d5 69 66 69 65 73 0a 20 20 20 20 20 20 20 20 2a 2a  ifies.        **
118d6 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 2e   that we do not.
118d7 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
118d8 72 74 28 20 70 50 67 2d 3e 70 67 6e 6f 21 3d 50  rt( pPg->pgno!=P
118d9 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
118da 67 65 72 29 20 29 3b 0a 20 20 20 20 20 20 20 20  ger) );.        
118db 70 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28  pData2 = CODEC2(
118dc 70 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70  pPager, pData, p
118dd 50 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20  Pg->pgno, 7);.  
118de 20 20 20 20 20 20 63 6b 73 75 6d 20 3d 20 70 61        cksum = pa
118df 67 65 72 5f 63 6b 73 75 6d 28 70 50 61 67 65 72  ger_cksum(pPager
118e0 2c 20 28 75 38 2a 29 70 44 61 74 61 32 29 3b 0a  , (u8*)pData2);.
118e1 20 20 20 20 20 20 20 20 72 63 20 3d 20 77 72 69          rc = wri
118e2 74 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d  te32bits(pPager-
118e3 3e 6a 66 64 2c 20 70 50 61 67 65 72 2d 3e 6a 6f  >jfd, pPager->jo
118e4 75 72 6e 61 6c 4f 66 66 2c 20 70 50 67 2d 3e 70  urnalOff, pPg->p
118e5 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66  gno);.        if
118e6 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
118e7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
118e8 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
118e9 28 70 50 61 67 65 72 2d 3e 6a 66 64 2c 20 70 44  (pPager->jfd, pD
118ea 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70 61  ata2, pPager->pa
118eb 67 65 53 69 7a 65 2c 0a 20 20 20 20 20 20 20 20  geSize,.        
118ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
118ed 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f        pPager->jo
118ee 75 72 6e 61 6c 4f 66 66 20 2b 20 34 29 3b 0a 20  urnalOff + 4);. 
118ef 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
118f0 3e 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 70  >journalOff += p
118f1 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 2b  Pager->pageSize+
118f2 34 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  4;.        }.   
118f3 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
118f4 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
118f5 20 20 20 20 72 63 20 3d 20 77 72 69 74 65 33 32      rc = write32
118f6 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a 66 64  bits(pPager->jfd
118f7 2c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61  , pPager->journa
118f8 6c 4f 66 66 2c 20 63 6b 73 75 6d 29 3b 0a 20 20  lOff, cksum);.  
118f9 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e          pPager->
118fa 6a 6f 75 72 6e 61 6c 4f 66 66 20 2b 3d 20 34 3b  journalOff += 4;
118fb 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
118fc 20 20 20 49 4f 54 52 41 43 45 28 28 22 4a 4f 55     IOTRACE(("JOU
118fd 54 20 25 70 20 25 64 20 25 6c 6c 64 20 25 64 5c  T %p %d %lld %d\
118fe 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d  n", pPager, pPg-
118ff 3e 70 67 6e 6f 2c 20 0a 20 20 20 20 20 20 20 20  >pgno, .        
11900 20 20 20 20 20 20 20 20 20 70 50 61 67 65 72 2d           pPager-
11901 3e 6a 6f 75 72 6e 61 6c 4f 66 66 2c 20 70 50 61  >journalOff, pPa
11902 67 65 72 2d 3e 70 61 67 65 53 69 7a 65 29 29 3b  ger->pageSize));
11903 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 5f 49  .        PAGER_I
11904 4e 43 52 28 73 71 6c 69 74 65 33 5f 70 61 67 65  NCR(sqlite3_page
11905 72 5f 77 72 69 74 65 6a 5f 63 6f 75 6e 74 29 3b  r_writej_count);
11906 0a 20 20 20 20 20 20 20 20 50 41 47 45 52 54 52  .        PAGERTR
11907 41 43 45 35 28 22 4a 4f 55 52 4e 41 4c 20 25 64  ACE5("JOURNAL %d
11908 20 70 61 67 65 20 25 64 20 6e 65 65 64 53 79 6e   page %d needSyn
11909 63 3d 25 64 20 68 61 73 68 28 25 30 38 78 29 5c  c=%d hash(%08x)\
1190a 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
1190b 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
1190c 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 0a 20 20  , pPg->pgno, .  
1190d 20 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67             ((pPg
1190e 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
1190f 45 44 5f 53 59 4e 43 29 3f 31 3a 30 29 2c 20 70  ED_SYNC)?1:0), p
11910 61 67 65 72 5f 70 61 67 65 68 61 73 68 28 70 50  ager_pagehash(pP
11911 67 29 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  g));..        /*
11912 20 41 6e 20 65 72 72 6f 72 20 68 61 73 20 6f 63   An error has oc
11913 63 75 72 65 64 20 77 72 69 74 69 6e 67 20 74 6f  cured writing to
11914 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
11915 65 2e 20 54 68 65 20 0a 20 20 20 20 20 20 20 20  e. The .        
11916 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  ** transaction w
11917 69 6c 6c 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  ill be rolled ba
11918 63 6b 20 62 79 20 74 68 65 20 6c 61 79 65 72 20  ck by the layer 
11919 61 62 6f 76 65 2e 0a 20 20 20 20 20 20 20 20 2a  above..        *
1191a 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63  /.        if( rc
1191b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
1191c 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1191d 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20  rc;.        }.. 
1191e 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e         pPager->n
1191f 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 20 20 61  Rec++;.        a
11920 73 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70  ssert( pPager->p
11921 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30 20 29 3b 0a  InJournal!=0 );.
11922 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
11923 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d  itvecSet(pPager-
11924 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67  >pInJournal, pPg
11925 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20  ->pgno);.       
11926 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e 6e 6f   if( !pPager->no
11927 53 79 6e 63 20 29 7b 0a 20 20 20 20 20 20 20 20  Sync ){.        
11928 20 20 70 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20    pPg->flags |= 
11929 50 47 48 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b  PGHDR_NEED_SYNC;
1192a 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1192b 20 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73     if( pPager->s
1192c 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20  tmtInUse ){.    
1192d 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74        sqlite3Bit
1192e 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70  vecSet(pPager->p
1192f 49 6e 53 74 6d 74 2c 20 70 50 67 2d 3e 70 67 6e  InStmt, pPg->pgn
11930 6f 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  o);.        }.  
11931 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
11932 20 20 20 69 66 28 20 21 70 50 61 67 65 72 2d 3e     if( !pPager->
11933 6a 6f 75 72 6e 61 6c 53 74 61 72 74 65 64 20 26  journalStarted &
11934 26 20 21 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e  & !pPager->noSyn
11935 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  c ){.          p
11936 50 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 50 47 48  Pg->flags |= PGH
11937 44 52 5f 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20  DR_NEED_SYNC;.  
11938 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
11939 50 41 47 45 52 54 52 41 43 45 34 28 22 41 50 50  PAGERTRACE4("APP
1193a 45 4e 44 20 25 64 20 70 61 67 65 20 25 64 20 6e  END %d page %d n
1193b 65 65 64 53 79 6e 63 3d 25 64 5c 6e 22 2c 0a 20  eedSync=%d\n",. 
1193c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
1193d 41 47 45 52 49 44 28 70 50 61 67 65 72 29 2c 20  AGERID(pPager), 
1193e 70 50 67 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20 20  pPg->pgno,.     
1193f 20 20 20 20 20 20 20 20 20 20 28 28 70 50 67 2d            ((pPg-
11940 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45 45  >flags&PGHDR_NEE
11941 44 5f 53 59 4e 43 29 3f 31 3a 30 29 29 3b 0a 20  D_SYNC)?1:0));. 
11942 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
11943 20 70 50 67 2d 3e 66 6c 61 67 73 26 50 47 48 44   pPg->flags&PGHD
11944 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a 20  R_NEED_SYNC ){. 
11945 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 6e         pPager->n
11946 65 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20  eedSync = 1;.   
11947 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
11948 20 20 2f 2a 20 49 66 20 74 68 65 20 73 74 61 74    /* If the stat
11949 65 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73  ement journal is
1194a 20 6f 70 65 6e 20 61 6e 64 20 74 68 65 20 70 61   open and the pa
1194b 67 65 20 69 73 20 6e 6f 74 20 69 6e 20 69 74 2c  ge is not in it,
1194c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 77 72 69  .    ** then wri
1194d 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 70  te the current p
1194e 61 67 65 20 74 6f 20 74 68 65 20 73 74 61 74 65  age to the state
1194f 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 2e 20 20 4e  ment journal.  N
11950 6f 74 65 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ote that.    ** 
11951 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6a 6f  the statement jo
11952 75 72 6e 61 6c 20 66 6f 72 6d 61 74 20 64 69 66  urnal format dif
11953 66 65 72 73 20 66 72 6f 6d 20 74 68 65 20 73 74  fers from the st
11954 61 6e 64 61 72 64 20 6a 6f 75 72 6e 61 6c 20 66  andard journal f
11955 6f 72 6d 61 74 0a 20 20 20 20 2a 2a 20 69 6e 20  ormat.    ** in 
11956 74 68 61 74 20 69 74 20 6f 6d 69 74 73 20 74 68  that it omits th
11957 65 20 63 68 65 63 6b 73 75 6d 73 20 61 6e 64 20  e checksums and 
11958 74 68 65 20 68 65 61 64 65 72 2e 0a 20 20 20 20  the header..    
11959 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  */.    if( pPage
1195a 72 2d 3e 73 74 6d 74 49 6e 55 73 65 20 0a 20 20  r->stmtInUse .  
1195b 20 20 20 26 26 20 21 70 61 67 65 49 6e 53 74 61     && !pageInSta
1195c 74 65 6d 65 6e 74 28 70 50 67 29 20 0a 20 20 20  tement(pPg) .   
1195d 20 20 26 26 20 70 50 67 2d 3e 70 67 6e 6f 3c 3d    && pPg->pgno<=
1195e 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65  pPager->stmtSize
1195f 20 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69   .    ){.      i
11960 36 34 20 6f 66 66 73 65 74 20 3d 20 70 50 61 67  64 offset = pPag
11961 65 72 2d 3e 73 74 6d 74 4e 52 65 63 2a 28 34 2b  er->stmtNRec*(4+
11962 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a 65  pPager->pageSize
11963 29 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 70  );.      char *p
11964 44 61 74 61 32 20 3d 20 43 4f 44 45 43 32 28 70  Data2 = CODEC2(p
11965 50 61 67 65 72 2c 20 70 44 61 74 61 2c 20 70 50  Pager, pData, pP
11966 67 2d 3e 70 67 6e 6f 2c 20 37 29 3b 0a 20 20 20  g->pgno, 7);.   
11967 20 20 20 61 73 73 65 72 74 28 20 70 61 67 65 49     assert( pageI
11968 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29 20 7c 7c  nJournal(pPg) ||
11969 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65   pPg->pgno>pPage
1196a 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 3b  r->origDbSize );
1196b 0a 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74  .      rc = writ
1196c 65 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e  e32bits(pPager->
1196d 73 74 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50  stfd, offset, pP
1196e 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  g->pgno);.      
1196f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
11970 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
11971 3d 20 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65  = sqlite3OsWrite
11972 28 70 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 70  (pPager->stfd, p
11973 44 61 74 61 32 2c 20 70 50 61 67 65 72 2d 3e 70  Data2, pPager->p
11974 61 67 65 53 69 7a 65 2c 20 6f 66 66 73 65 74 2b  ageSize, offset+
11975 34 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  4);.      }.    
11976 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22 53    PAGERTRACE3("S
11977 54 4d 54 2d 4a 4f 55 52 4e 41 4c 20 25 64 20 70  TMT-JOURNAL %d p
11978 61 67 65 20 25 64 5c 6e 22 2c 20 50 41 47 45 52  age %d\n", PAGER
11979 49 44 28 70 50 61 67 65 72 29 2c 20 70 50 67 2d  ID(pPager), pPg-
1197a 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66  >pgno);.      if
1197b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1197c 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
1197d 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
1197e 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74      pPager->stmt
1197f 4e 52 65 63 2b 2b 3b 0a 20 20 20 20 20 20 61 73  NRec++;.      as
11980 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 70 49  sert( pPager->pI
11981 6e 53 74 6d 74 21 3d 30 20 29 3b 0a 20 20 20 20  nStmt!=0 );.    
11982 20 20 73 71 6c 69 74 65 33 42 69 74 76 65 63 53    sqlite3BitvecS
11983 65 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 53 74  et(pPager->pInSt
11984 6d 74 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a  mt, pPg->pgno);.
11985 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
11986 55 70 64 61 74 65 20 74 68 65 20 64 61 74 61 62  Update the datab
11987 61 73 65 20 73 69 7a 65 20 61 6e 64 20 72 65 74  ase size and ret
11988 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  urn..  */.  asse
11989 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
1198a 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e>=PAGER_SHARED 
1198b 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  );.  if( pPager-
1198c 3e 64 62 53 69 7a 65 3c 70 50 67 2d 3e 70 67 6e  >dbSize<pPg->pgn
1198d 6f 20 29 7b 0a 20 20 20 20 70 50 61 67 65 72 2d  o ){.    pPager-
1198e 3e 64 62 53 69 7a 65 20 3d 20 70 50 67 2d 3e 70  >dbSize = pPg->p
1198f 67 6e 6f 3b 0a 20 20 20 20 69 66 28 20 70 50 61  gno;.    if( pPa
11990 67 65 72 2d 3e 64 62 53 69 7a 65 3d 3d 28 50 41  ger->dbSize==(PA
11991 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61 67  GER_MJ_PGNO(pPag
11992 65 72 29 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  er)-1) ){.      
11993 70 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 2b 2b  pPager->dbSize++
11994 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
11995 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
11996 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
11997 69 73 20 75 73 65 64 20 74 6f 20 6d 61 72 6b 20  is used to mark 
11998 61 20 64 61 74 61 2d 70 61 67 65 20 61 73 20 77  a data-page as w
11999 72 69 74 61 62 6c 65 2e 20 49 74 20 75 73 65 73  ritable. It uses
1199a 20 0a 2a 2a 20 70 61 67 65 72 5f 77 72 69 74 65   .** pager_write
1199b 28 29 20 74 6f 20 6f 70 65 6e 20 61 20 6a 6f 75  () to open a jou
1199c 72 6e 61 6c 20 66 69 6c 65 20 28 69 66 20 69 74  rnal file (if it
1199d 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20   is not already 
1199e 6f 70 65 6e 29 0a 2a 2a 20 61 6e 64 20 77 72 69  open).** and wri
1199f 74 65 20 74 68 65 20 70 61 67 65 20 2a 70 44 61  te the page *pDa
119a0 74 61 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61  ta to the journa
119a1 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66  l..**.** The dif
119a2 66 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20  ference between 
119a3 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 6e  this function an
119a4 64 20 70 61 67 65 72 5f 77 72 69 74 65 28 29 20  d pager_write() 
119a5 69 73 20 74 68 61 74 20 74 68 69 73 0a 2a 2a 20  is that this.** 
119a6 66 75 6e 63 74 69 6f 6e 20 61 6c 73 6f 20 64 65  function also de
119a7 61 6c 73 20 77 69 74 68 20 74 68 65 20 73 70 65  als with the spe
119a8 63 69 61 6c 20 63 61 73 65 20 77 68 65 72 65 20  cial case where 
119a9 32 20 6f 72 20 6d 6f 72 65 20 70 61 67 65 73 0a  2 or more pages.
119aa 2a 2a 20 66 69 74 20 6f 6e 20 61 20 73 69 6e 67  ** fit on a sing
119ab 6c 65 20 64 69 73 6b 20 73 65 63 74 6f 72 2e 20  le disk sector. 
119ac 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c  In this case all
119ad 20 63 6f 2d 72 65 73 69 64 65 6e 74 20 70 61 67   co-resident pag
119ae 65 73 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20  es.** must have 
119af 62 65 65 6e 20 77 72 69 74 74 65 6e 20 74 6f 20  been written to 
119b0 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
119b1 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
119b2 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
119b3 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
119b4 50 61 67 65 72 57 72 69 74 65 28 44 62 50 61 67  PagerWrite(DbPag
119b5 65 20 2a 70 44 62 50 61 67 65 29 7b 0a 20 20 69  e *pDbPage){.  i
119b6 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
119b7 4b 3b 0a 0a 20 20 50 67 48 64 72 20 2a 70 50 67  K;..  PgHdr *pPg
119b8 20 3d 20 70 44 62 50 61 67 65 3b 0a 20 20 50 61   = pDbPage;.  Pa
119b9 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 50  ger *pPager = pP
119ba 67 2d 3e 70 50 61 67 65 72 3b 0a 20 20 50 67 6e  g->pPager;.  Pgn
119bb 6f 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72  o nPagePerSector
119bc 20 3d 20 28 70 50 61 67 65 72 2d 3e 73 65 63 74   = (pPager->sect
119bd 6f 72 53 69 7a 65 2f 70 50 61 67 65 72 2d 3e 70  orSize/pPager->p
119be 61 67 65 53 69 7a 65 29 3b 0a 0a 20 20 69 66 28  ageSize);..  if(
119bf 20 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 3e   nPagePerSector>
119c0 31 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e 50  1 ){.    Pgno nP
119c1 61 67 65 43 6f 75 6e 74 3b 20 20 20 20 20 20 20  ageCount;       
119c2 20 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62     /* Total numb
119c3 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 64  er of pages in d
119c4 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f 0a  atabase file */.
119c5 20 20 20 20 50 67 6e 6f 20 70 67 31 3b 20 20 20      Pgno pg1;   
119c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
119c7 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20 74   First page of t
119c8 68 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73  he sector pPg is
119c9 20 6c 6f 63 61 74 65 64 20 6f 6e 2e 20 2a 2f 0a   located on. */.
119ca 20 20 20 20 69 6e 74 20 6e 50 61 67 65 3b 20 20      int nPage;  
119cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
119cc 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   Number of pages
119cd 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 67 31   starting at pg1
119ce 20 74 6f 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20   to journal */. 
119cf 20 20 20 69 6e 74 20 69 69 3b 0a 20 20 20 20 69     int ii;.    i
119d0 6e 74 20 6e 65 65 64 53 79 6e 63 20 3d 20 30 3b  nt needSync = 0;
119d1 0a 0a 20 20 20 20 2f 2a 20 53 65 74 20 74 68 65  ..    /* Set the
119d2 20 64 6f 4e 6f 74 53 79 6e 63 20 66 6c 61 67 20   doNotSync flag 
119d3 74 6f 20 31 2e 20 54 68 69 73 20 69 73 20 62 65  to 1. This is be
119d4 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74 20  cause we cannot 
119d5 61 6c 6c 6f 77 20 61 20 6a 6f 75 72 6e 61 6c 0a  allow a journal.
119d6 20 20 20 20 2a 2a 20 68 65 61 64 65 72 20 74 6f      ** header to
119d7 20 62 65 20 77 72 69 74 74 65 6e 20 62 65 74 77   be written betw
119d8 65 65 6e 20 74 68 65 20 70 61 67 65 73 20 6a 6f  een the pages jo
119d9 75 72 6e 61 6c 65 64 20 62 79 20 74 68 69 73 20  urnaled by this 
119da 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f  function..    */
119db 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 4d 45  .    assert( !ME
119dc 4d 44 42 20 29 3b 0a 20 20 20 20 61 73 73 65 72  MDB );.    asser
119dd 74 28 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74  t( pPager->doNot
119de 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20 20 70  Sync==0 );.    p
119df 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63  Pager->doNotSync
119e0 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 54 68   = 1;..    /* Th
119e1 69 73 20 74 72 69 63 6b 20 61 73 73 75 6d 65 73  is trick assumes
119e2 20 74 68 61 74 20 62 6f 74 68 20 74 68 65 20 70   that both the p
119e3 61 67 65 2d 73 69 7a 65 20 61 6e 64 20 73 65 63  age-size and sec
119e4 74 6f 72 2d 73 69 7a 65 20 61 72 65 0a 20 20 20  tor-size are.   
119e5 20 2a 2a 20 61 6e 20 69 6e 74 65 67 65 72 20 70   ** an integer p
119e6 6f 77 65 72 20 6f 66 20 32 2e 20 49 74 20 73 65  ower of 2. It se
119e7 74 73 20 76 61 72 69 61 62 6c 65 20 70 67 31 20  ts variable pg1 
119e8 74 6f 20 74 68 65 20 69 64 65 6e 74 69 66 69 65  to the identifie
119e9 72 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20  r.    ** of the 
119ea 66 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68  first page of th
119eb 65 20 73 65 63 74 6f 72 20 70 50 67 20 69 73 20  e sector pPg is 
119ec 6c 6f 63 61 74 65 64 20 6f 6e 2e 0a 20 20 20 20  located on..    
119ed 2a 2f 0a 20 20 20 20 70 67 31 20 3d 20 28 28 70  */.    pg1 = ((p
119ee 50 67 2d 3e 70 67 6e 6f 2d 31 29 20 26 20 7e 28  Pg->pgno-1) & ~(
119ef 6e 50 61 67 65 50 65 72 53 65 63 74 6f 72 2d 31  nPagePerSector-1
119f0 29 29 20 2b 20 31 3b 0a 0a 20 20 20 20 73 71 6c  )) + 1;..    sql
119f1 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75  ite3PagerPagecou
119f2 6e 74 28 70 50 61 67 65 72 2c 20 28 69 6e 74 20  nt(pPager, (int 
119f3 2a 29 26 6e 50 61 67 65 43 6f 75 6e 74 29 3b 0a  *)&nPageCount);.
119f4 20 20 20 20 69 66 28 20 70 50 67 2d 3e 70 67 6e      if( pPg->pgn
119f5 6f 3e 6e 50 61 67 65 43 6f 75 6e 74 20 29 7b 0a  o>nPageCount ){.
119f6 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 28 70        nPage = (p
119f7 50 67 2d 3e 70 67 6e 6f 20 2d 20 70 67 31 29 2b  Pg->pgno - pg1)+
119f8 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  1;.    }else if(
119f9 20 28 70 67 31 2b 6e 50 61 67 65 50 65 72 53 65   (pg1+nPagePerSe
119fa 63 74 6f 72 2d 31 29 3e 6e 50 61 67 65 43 6f 75  ctor-1)>nPageCou
119fb 6e 74 20 29 7b 0a 20 20 20 20 20 20 6e 50 61 67  nt ){.      nPag
119fc 65 20 3d 20 6e 50 61 67 65 43 6f 75 6e 74 2b 31  e = nPageCount+1
119fd 2d 70 67 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  -pg1;.    }else{
119fe 0a 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 6e  .      nPage = n
119ff 50 61 67 65 50 65 72 53 65 63 74 6f 72 3b 0a 20  PagePerSector;. 
11a00 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
11a01 6e 50 61 67 65 3e 30 29 3b 0a 20 20 20 20 61 73  nPage>0);.    as
11a02 73 65 72 74 28 70 67 31 3c 3d 70 50 67 2d 3e 70  sert(pg1<=pPg->p
11a03 67 6e 6f 29 3b 0a 20 20 20 20 61 73 73 65 72 74  gno);.    assert
11a04 28 28 70 67 31 2b 6e 50 61 67 65 29 3e 70 50 67  ((pg1+nPage)>pPg
11a05 2d 3e 70 67 6e 6f 29 3b 0a 0a 20 20 20 20 66 6f  ->pgno);..    fo
11a06 72 28 69 69 3d 30 3b 20 69 69 3c 6e 50 61 67 65  r(ii=0; ii<nPage
11a07 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f   && rc==SQLITE_O
11a08 4b 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  K; ii++){.      
11a09 50 67 6e 6f 20 70 67 20 3d 20 70 67 31 2b 69 69  Pgno pg = pg1+ii
11a0a 3b 0a 20 20 20 20 20 20 50 67 48 64 72 20 2a 70  ;.      PgHdr *p
11a0b 50 61 67 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Page;.      if( 
11a0c 70 67 3d 3d 70 50 67 2d 3e 70 67 6e 6f 20 7c 7c  pg==pPg->pgno ||
11a0d 20 21 73 71 6c 69 74 65 33 42 69 74 76 65 63 54   !sqlite3BitvecT
11a0e 65 73 74 28 70 50 61 67 65 72 2d 3e 70 49 6e 4a  est(pPager->pInJ
11a0f 6f 75 72 6e 61 6c 2c 20 70 67 29 20 29 7b 0a 20  ournal, pg) ){. 
11a10 20 20 20 20 20 20 20 69 66 28 20 70 67 21 3d 50         if( pg!=P
11a11 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 70 50 61  AGER_MJ_PGNO(pPa
11a12 67 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ger) ){.        
11a13 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
11a14 67 65 72 47 65 74 28 70 50 61 67 65 72 2c 20 70  gerGet(pPager, p
11a15 67 2c 20 26 70 50 61 67 65 29 3b 0a 20 20 20 20  g, &pPage);.    
11a16 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
11a17 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
11a18 20 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65         rc = page
11a19 72 5f 77 72 69 74 65 28 70 50 61 67 65 29 3b 0a  r_write(pPage);.
11a1a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
11a1b 70 50 61 67 65 2d 3e 66 6c 61 67 73 26 50 47 48  pPage->flags&PGH
11a1c 44 52 5f 4e 45 45 44 5f 53 59 4e 43 20 29 7b 0a  DR_NEED_SYNC ){.
11a1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 65                ne
11a1e 65 64 53 79 6e 63 20 3d 20 31 3b 0a 20 20 20 20  edSync = 1;.    
11a1f 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
11a20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
11a21 65 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a  erUnref(pPage);.
11a22 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
11a23 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
11a24 65 20 69 66 28 20 28 70 50 61 67 65 20 3d 20 70  e if( (pPage = p
11a25 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50 61 67  ager_lookup(pPag
11a26 65 72 2c 20 70 67 29 29 21 3d 30 20 29 7b 0a 20  er, pg))!=0 ){. 
11a27 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
11a28 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e 45  ->flags&PGHDR_NE
11a29 45 44 5f 53 59 4e 43 20 29 7b 0a 20 20 20 20 20  ED_SYNC ){.     
11a2a 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20 3d 20       needSync = 
11a2b 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
11a2c 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
11a2d 72 55 6e 72 65 66 28 70 50 61 67 65 29 3b 0a 20  rUnref(pPage);. 
11a2e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
11a2f 20 20 2f 2a 20 49 66 20 74 68 65 20 50 67 48 64    /* If the PgHd
11a30 72 2e 6e 65 65 64 53 79 6e 63 20 66 6c 61 67 20  r.needSync flag 
11a31 69 73 20 73 65 74 20 66 6f 72 20 61 6e 79 20 6f  is set for any o
11a32 66 20 74 68 65 20 6e 50 61 67 65 20 70 61 67 65  f the nPage page
11a33 73 20 0a 20 20 20 20 2a 2a 20 73 74 61 72 74 69  s .    ** starti
11a34 6e 67 20 61 74 20 70 67 31 2c 20 74 68 65 6e 20  ng at pg1, then 
11a35 69 74 20 6e 65 65 64 73 20 74 6f 20 62 65 20 73  it needs to be s
11a36 65 74 20 66 6f 72 20 61 6c 6c 20 6f 66 20 74 68  et for all of th
11a37 65 6d 2e 20 42 65 63 61 75 73 65 0a 20 20 20 20  em. Because.    
11a38 2a 2a 20 77 72 69 74 69 6e 67 20 74 6f 20 61 6e  ** writing to an
11a39 79 20 6f 66 20 74 68 65 73 65 20 6e 50 61 67 65  y of these nPage
11a3a 20 70 61 67 65 73 20 6d 61 79 20 64 61 6d 61 67   pages may damag
11a3b 65 20 74 68 65 20 6f 74 68 65 72 73 2c 20 74 68  e the others, th
11a3c 65 0a 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c  e.    ** journal
11a3d 20 66 69 6c 65 20 6d 75 73 74 20 63 6f 6e 74 61   file must conta
11a3e 69 6e 20 73 79 6e 63 28 29 65 64 20 63 6f 70 69  in sync()ed copi
11a3f 65 73 20 6f 66 20 61 6c 6c 20 6f 66 20 74 68 65  es of all of the
11a40 6d 0a 20 20 20 20 2a 2a 20 62 65 66 6f 72 65 20  m.    ** before 
11a41 61 6e 79 20 6f 66 20 74 68 65 6d 20 63 61 6e 20  any of them can 
11a42 62 65 20 77 72 69 74 74 65 6e 20 6f 75 74 20 74  be written out t
11a43 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  o the database f
11a44 69 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ile..    */.    
11a45 69 66 28 20 6e 65 65 64 53 79 6e 63 20 29 7b 0a  if( needSync ){.
11a46 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 4d        assert( !M
11a47 45 4d 44 42 20 26 26 20 70 50 61 67 65 72 2d 3e  EMDB && pPager->
11a48 6e 6f 53 79 6e 63 3d 3d 30 20 29 3b 0a 20 20 20  noSync==0 );.   
11a49 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c     for(ii=0; ii<
11a4a 6e 50 61 67 65 20 26 26 20 6e 65 65 64 53 79 6e  nPage && needSyn
11a4b 63 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 20 20  c; ii++){.      
11a4c 20 20 50 67 48 64 72 20 2a 70 50 61 67 65 20 3d    PgHdr *pPage =
11a4d 20 70 61 67 65 72 5f 6c 6f 6f 6b 75 70 28 70 50   pager_lookup(pP
11a4e 61 67 65 72 2c 20 70 67 31 2b 69 69 29 3b 0a 20  ager, pg1+ii);. 
11a4f 20 20 20 20 20 20 20 69 66 28 20 70 50 61 67 65         if( pPage
11a50 20 29 20 70 50 61 67 65 2d 3e 66 6c 61 67 73 20   ) pPage->flags 
11a51 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  |= PGHDR_NEED_SY
11a52 4e 43 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  NC;.        sqli
11a53 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
11a54 61 67 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  age);.      }.  
11a55 20 20 20 20 61 73 73 65 72 74 28 70 50 61 67 65      assert(pPage
11a56 72 2d 3e 6e 65 65 64 53 79 6e 63 29 3b 0a 20 20  r->needSync);.  
11a57 20 20 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28    }..    assert(
11a58 20 70 50 61 67 65 72 2d 3e 64 6f 4e 6f 74 53 79   pPager->doNotSy
11a59 6e 63 3d 3d 31 20 29 3b 0a 20 20 20 20 70 50 61  nc==1 );.    pPa
11a5a 67 65 72 2d 3e 64 6f 4e 6f 74 53 79 6e 63 20 3d  ger->doNotSync =
11a5b 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20   0;.  }else{.   
11a5c 20 72 63 20 3d 20 70 61 67 65 72 5f 77 72 69 74   rc = pager_writ
11a5d 65 28 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  e(pDbPage);.  }.
11a5e 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
11a5f 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55  /*.** Return TRU
11a60 45 20 69 66 20 74 68 65 20 70 61 67 65 20 67 69  E if the page gi
11a61 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67 75 6d  ven in the argum
11a62 65 6e 74 20 77 61 73 20 70 72 65 76 69 6f 75 73  ent was previous
11a63 6c 79 20 70 61 73 73 65 64 0a 2a 2a 20 74 6f 20  ly passed.** to 
11a64 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
11a65 65 28 29 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  e().  In other w
11a66 6f 72 64 73 2c 20 72 65 74 75 72 6e 20 54 52 55  ords, return TRU
11a67 45 20 69 66 20 69 74 20 69 73 20 6f 6b 0a 2a 2a  E if it is ok.**
11a68 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20 63   to change the c
11a69 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61  ontent of the pa
11a6a 67 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e  ge..*/.#ifndef N
11a6b 44 45 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49  DEBUG.SQLITE_PRI
11a6c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
11a6d 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
11a6e 28 44 62 50 61 67 65 20 2a 70 50 67 29 7b 0a 20  (DbPage *pPg){. 
11a6f 20 72 65 74 75 72 6e 20 70 50 67 2d 3e 66 6c 61   return pPg->fla
11a70 67 73 26 50 47 48 44 52 5f 44 49 52 54 59 3b 0a  gs&PGHDR_DIRTY;.
11a71 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
11a72 41 20 63 61 6c 6c 20 74 6f 20 74 68 69 73 20 72  A call to this r
11a73 6f 75 74 69 6e 65 20 74 65 6c 6c 73 20 74 68 65  outine tells the
11a74 20 70 61 67 65 72 20 74 68 61 74 20 69 74 20 69   pager that it i
11a75 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20  s not necessary 
11a76 74 6f 0a 2a 2a 20 77 72 69 74 65 20 74 68 65 20  to.** write the 
11a77 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 6e 20 70  information on p
11a78 61 67 65 20 70 50 67 20 62 61 63 6b 20 74 6f 20  age pPg back to 
11a79 74 68 65 20 64 69 73 6b 2c 20 65 76 65 6e 20 74  the disk, even t
11a7a 68 6f 75 67 68 0a 2a 2a 20 74 68 61 74 20 70 61  hough.** that pa
11a7b 67 65 20 6d 69 67 68 74 20 62 65 20 6d 61 72 6b  ge might be mark
11a7c 65 64 20 61 73 20 64 69 72 74 79 2e 20 20 54 68  ed as dirty.  Th
11a7d 69 73 20 68 61 70 70 65 6e 73 2c 20 66 6f 72 20  is happens, for 
11a7e 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e 0a 2a 2a  example, when.**
11a7f 20 74 68 65 20 70 61 67 65 20 68 61 73 20 62 65   the page has be
11a80 65 6e 20 61 64 64 65 64 20 61 73 20 61 20 6c 65  en added as a le
11a81 61 66 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69  af of the freeli
11a82 73 74 20 61 6e 64 20 73 6f 20 69 74 73 0a 2a 2a  st and so its.**
11a83 20 63 6f 6e 74 65 6e 74 20 6e 6f 20 6c 6f 6e 67   content no long
11a84 65 72 20 6d 61 74 74 65 72 73 2e 0a 2a 2a 0a 2a  er matters..**.*
11a85 2a 20 54 68 65 20 6f 76 65 72 6c 79 69 6e 67 20  * The overlying 
11a86 73 6f 66 74 77 61 72 65 20 6c 61 79 65 72 20 63  software layer c
11a87 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
11a88 65 20 77 68 65 6e 20 61 6c 6c 20 6f 66 20 74 68  e when all of th
11a89 65 20 64 61 74 61 0a 2a 2a 20 6f 6e 20 74 68 65  e data.** on the
11a8a 20 67 69 76 65 6e 20 70 61 67 65 20 69 73 20 75   given page is u
11a8b 6e 75 73 65 64 2e 20 20 54 68 65 20 70 61 67 65  nused.  The page
11a8c 72 20 6d 61 72 6b 73 20 74 68 65 20 70 61 67 65  r marks the page
11a8d 20 61 73 20 63 6c 65 61 6e 20 73 6f 0a 2a 2a 20   as clean so.** 
11a8e 74 68 61 74 20 69 74 20 64 6f 65 73 20 6e 6f 74  that it does not
11a8f 20 67 65 74 20 77 72 69 74 74 65 6e 20 74 6f 20   get written to 
11a90 64 69 73 6b 2e 0a 2a 2a 0a 2a 2a 20 54 65 73 74  disk..**.** Test
11a91 73 20 73 68 6f 77 20 74 68 61 74 20 74 68 69 73  s show that this
11a92 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 74   optimization, t
11a93 6f 67 65 74 68 65 72 20 77 69 74 68 20 74 68 65  ogether with the
11a94 0a 2a 2a 20 73 71 6c 69 74 65 33 50 61 67 65 72  .** sqlite3Pager
11a95 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b 28 29 20 62  DontRollback() b
11a96 65 6c 6f 77 2c 20 6d 6f 72 65 20 74 68 61 6e 20  elow, more than 
11a97 64 6f 75 62 6c 65 20 74 68 65 20 73 70 65 65 64  double the speed
11a98 0a 2a 2a 20 6f 66 20 6c 61 72 67 65 20 49 4e 53  .** of large INS
11a99 45 52 54 20 6f 70 65 72 61 74 69 6f 6e 73 20 61  ERT operations a
11a9a 6e 64 20 71 75 61 64 72 75 70 6c 65 20 74 68 65  nd quadruple the
11a9b 20 73 70 65 65 64 20 6f 66 20 6c 61 72 67 65 20   speed of large 
11a9c 44 45 4c 45 54 45 73 2e 0a 2a 2a 0a 2a 2a 20 57  DELETEs..**.** W
11a9d 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65  hen this routine
11a9e 20 69 73 20 63 61 6c 6c 65 64 2c 20 73 65 74 20   is called, set 
11a9f 74 68 65 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61  the alwaysRollba
11aa0 63 6b 20 66 6c 61 67 20 74 6f 20 74 72 75 65 2e  ck flag to true.
11aa1 0a 2a 2a 20 53 75 62 73 65 71 75 65 6e 74 20 63  .** Subsequent c
11aa2 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 33 50  alls to sqlite3P
11aa3 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61 63 6b  agerDontRollback
11aa4 28 29 20 66 6f 72 20 74 68 65 20 73 61 6d 65 20  () for the same 
11aa5 70 61 67 65 0a 2a 2a 20 77 69 6c 6c 20 74 68 65  page.** will the
11aa6 72 65 61 66 74 65 72 20 62 65 20 69 67 6e 6f 72  reafter be ignor
11aa7 65 64 2e 20 20 54 68 69 73 20 69 73 20 6e 65 63  ed.  This is nec
11aa8 65 73 73 61 72 79 20 74 6f 20 61 76 6f 69 64 20  essary to avoid 
11aa9 61 20 70 72 6f 62 6c 65 6d 0a 2a 2a 20 77 68 65  a problem.** whe
11aaa 72 65 20 61 20 70 61 67 65 20 77 69 74 68 20 64  re a page with d
11aab 61 74 61 20 69 73 20 61 64 64 65 64 20 74 6f 20  ata is added to 
11aac 74 68 65 20 66 72 65 65 6c 69 73 74 20 64 75 72  the freelist dur
11aad 69 6e 67 20 6f 6e 65 20 70 61 72 74 20 6f 66 0a  ing one part of.
11aae 2a 2a 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  ** a transaction
11aaf 20 74 68 65 6e 20 72 65 6d 6f 76 65 64 20 66 72   then removed fr
11ab0 6f 6d 20 74 68 65 20 66 72 65 65 6c 69 73 74 20  om the freelist 
11ab1 64 75 72 69 6e 67 20 61 20 6c 61 74 65 72 20 70  during a later p
11ab2 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20 73 61  art.** of the sa
11ab3 6d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  me transaction a
11ab4 6e 64 20 72 65 75 73 65 64 20 66 6f 72 20 73 6f  nd reused for so
11ab5 6d 65 20 6f 74 68 65 72 20 70 75 72 70 6f 73 65  me other purpose
11ab6 2e 20 20 57 68 65 6e 20 69 74 0a 2a 2a 20 69 73  .  When it.** is
11ab7 20 66 69 72 73 74 20 61 64 64 65 64 20 74 6f 20   first added to 
11ab8 74 68 65 20 66 72 65 65 6c 69 73 74 2c 20 74 68  the freelist, th
11ab9 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61  is routine is ca
11aba 6c 6c 65 64 2e 20 20 57 68 65 6e 20 72 65 75 73  lled.  When reus
11abb 65 64 2c 0a 2a 2a 20 74 68 65 20 73 71 6c 69 74  ed,.** the sqlit
11abc 65 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62  e3PagerDontRollb
11abd 61 63 6b 28 29 20 72 6f 75 74 69 6e 65 20 69 73  ack() routine is
11abe 20 63 61 6c 6c 65 64 2e 20 20 42 75 74 20 62 65   called.  But be
11abf 63 61 75 73 65 20 74 68 65 0a 2a 2a 20 70 61 67  cause the.** pag
11ac0 65 20 63 6f 6e 74 61 69 6e 73 20 63 72 69 74 69  e contains criti
11ac1 63 61 6c 20 64 61 74 61 2c 20 77 65 20 73 74 69  cal data, we sti
11ac2 6c 6c 20 6e 65 65 64 20 74 6f 20 62 65 20 73 75  ll need to be su
11ac3 72 65 20 69 74 20 67 65 74 73 0a 2a 2a 20 72 6f  re it gets.** ro
11ac4 6c 6c 65 64 20 62 61 63 6b 20 69 6e 20 73 70 69  lled back in spi
11ac5 74 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  te of the sqlite
11ac6 33 50 61 67 65 72 44 6f 6e 74 52 6f 6c 6c 62 61  3PagerDontRollba
11ac7 63 6b 28 29 20 63 61 6c 6c 2e 0a 2a 2f 0a 53 51  ck() call..*/.SQ
11ac8 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
11ac9 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
11aca 74 57 72 69 74 65 28 44 62 50 61 67 65 20 2a 70  tWrite(DbPage *p
11acb 44 62 50 61 67 65 29 7b 0a 20 20 50 67 48 64 72  DbPage){.  PgHdr
11acc 20 2a 70 50 67 20 3d 20 70 44 62 50 61 67 65 3b   *pPg = pDbPage;
11acd 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
11ace 20 3d 20 70 50 67 2d 3e 70 50 61 67 65 72 3b 0a   = pPg->pPager;.
11acf 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 69 66 28    int rc;..  if(
11ad0 20 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65   pPg->pgno>pPage
11ad1 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b  r->origDbSize ){
11ad2 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
11ad3 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69 66 28  TE_OK;.  }.  if(
11ad4 20 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73   pPager->pAlways
11ad5 52 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 7b 0a 20  Rollback==0 ){. 
11ad6 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
11ad7 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 20 29 3b  r->pInJournal );
11ad8 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 70 41 6c  .    pPager->pAl
11ad9 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20 3d 20 73  waysRollback = s
11ada 71 6c 69 74 65 33 42 69 74 76 65 63 43 72 65 61  qlite3BitvecCrea
11adb 74 65 28 70 50 61 67 65 72 2d 3e 6f 72 69 67 44  te(pPager->origD
11adc 62 53 69 7a 65 29 3b 0a 20 20 20 20 69 66 28 20  bSize);.    if( 
11add 21 70 50 61 67 65 72 2d 3e 70 41 6c 77 61 79 73  !pPager->pAlways
11ade 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20 20  Rollback ){.    
11adf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11ae0 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d  NOMEM;.    }.  }
11ae1 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
11ae2 69 74 76 65 63 53 65 74 28 70 50 61 67 65 72 2d  itvecSet(pPager-
11ae3 3e 70 41 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b  >pAlwaysRollback
11ae4 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 0a 20  , pPg->pgno);.. 
11ae5 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
11ae6 4f 4b 20 26 26 20 28 70 50 67 2d 3e 66 6c 61 67  OK && (pPg->flag
11ae7 73 26 50 47 48 44 52 5f 44 49 52 54 59 29 20 26  s&PGHDR_DIRTY) &
11ae8 26 20 21 70 50 61 67 65 72 2d 3e 73 74 6d 74 49  & !pPager->stmtI
11ae9 6e 55 73 65 20 29 7b 0a 20 20 20 20 61 73 73 65  nUse ){.    asse
11aea 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74  rt( pPager->stat
11aeb 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45 44 20  e>=PAGER_SHARED 
11aec 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65  );.    if( pPage
11aed 72 2d 3e 64 62 53 69 7a 65 3d 3d 70 50 67 2d 3e  r->dbSize==pPg->
11aee 70 67 6e 6f 20 26 26 20 70 50 61 67 65 72 2d 3e  pgno && pPager->
11aef 6f 72 69 67 44 62 53 69 7a 65 3c 70 50 61 67 65  origDbSize<pPage
11af0 72 2d 3e 64 62 53 69 7a 65 20 29 7b 0a 20 20 20  r->dbSize ){.   
11af1 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 70 61     /* If this pa
11af2 67 65 73 20 69 73 20 74 68 65 20 6c 61 73 74 20  ges is the last 
11af3 70 61 67 65 20 69 6e 20 74 68 65 20 66 69 6c 65  page in the file
11af4 20 61 6e 64 20 74 68 65 20 66 69 6c 65 20 68 61   and the file ha
11af5 73 20 67 72 6f 77 6e 0a 20 20 20 20 20 20 2a 2a  s grown.      **
11af6 20 64 75 72 69 6e 67 20 74 68 65 20 63 75 72 72   during the curr
11af7 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c  ent transaction,
11af8 20 74 68 65 6e 20 64 6f 20 4e 4f 54 20 6d 61 72   then do NOT mar
11af9 6b 20 74 68 65 20 70 61 67 65 20 61 73 20 63 6c  k the page as cl
11afa 65 61 6e 2e 0a 20 20 20 20 20 20 2a 2a 20 57 68  ean..      ** Wh
11afb 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  en the database 
11afc 66 69 6c 65 20 67 72 6f 77 73 2c 20 77 65 20 6d  file grows, we m
11afd 75 73 74 20 6d 61 6b 65 20 73 75 72 65 20 74 68  ust make sure th
11afe 61 74 20 74 68 65 20 6c 61 73 74 20 70 61 67 65  at the last page
11aff 0a 20 20 20 20 20 20 2a 2a 20 67 65 74 73 20 77  .      ** gets w
11b00 72 69 74 74 65 6e 20 61 74 20 6c 65 61 73 74 20  ritten at least 
11b01 6f 6e 63 65 20 73 6f 20 74 68 61 74 20 74 68 65  once so that the
11b02 20 64 69 73 6b 20 66 69 6c 65 20 77 69 6c 6c 20   disk file will 
11b03 62 65 20 74 68 65 20 63 6f 72 72 65 63 74 0a 20  be the correct. 
11b04 20 20 20 20 20 2a 2a 20 73 69 7a 65 2e 20 49 66       ** size. If
11b05 20 79 6f 75 20 64 6f 20 6e 6f 74 20 77 72 69 74   you do not writ
11b06 65 20 74 68 69 73 20 70 61 67 65 20 61 6e 64 20  e this page and 
11b07 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65 20  the size of the 
11b08 66 69 6c 65 0a 20 20 20 20 20 20 2a 2a 20 6f 6e  file.      ** on
11b09 20 74 68 65 20 64 69 73 6b 20 65 6e 64 73 20 75   the disk ends u
11b0a 70 20 62 65 69 6e 67 20 74 6f 6f 20 73 6d 61 6c  p being too smal
11b0b 6c 2c 20 74 68 61 74 20 63 61 6e 20 6c 65 61 64  l, that can lead
11b0c 20 74 6f 20 64 61 74 61 62 61 73 65 0a 20 20 20   to database.   
11b0d 20 20 20 2a 2a 20 63 6f 72 72 75 70 74 69 6f 6e     ** corruption
11b0e 20 64 75 72 69 6e 67 20 74 68 65 20 6e 65 78 74   during the next
11b0f 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20   transaction..  
11b10 20 20 20 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65      */.    }else
11b11 7b 0a 20 20 20 20 20 20 50 41 47 45 52 54 52 41  {.      PAGERTRA
11b12 43 45 33 28 22 44 4f 4e 54 5f 57 52 49 54 45 20  CE3("DONT_WRITE 
11b13 70 61 67 65 20 25 64 20 6f 66 20 25 64 5c 6e 22  page %d of %d\n"
11b14 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47  , pPg->pgno, PAG
11b15 45 52 49 44 28 70 50 61 67 65 72 29 29 3b 0a 20  ERID(pPager));. 
11b16 20 20 20 20 20 49 4f 54 52 41 43 45 28 28 22 43       IOTRACE(("C
11b17 4c 45 41 4e 20 25 70 20 25 64 5c 6e 22 2c 20 70  LEAN %p %d\n", p
11b18 50 61 67 65 72 2c 20 70 50 67 2d 3e 70 67 6e 6f  Pager, pPg->pgno
11b19 29 29 0a 20 20 20 20 20 20 70 50 67 2d 3e 66 6c  )).      pPg->fl
11b1a 61 67 73 20 7c 3d 20 50 47 48 44 52 5f 44 4f 4e  ags |= PGHDR_DON
11b1b 54 5f 57 52 49 54 45 3b 0a 23 69 66 64 65 66 20  T_WRITE;.#ifdef 
11b1c 53 51 4c 49 54 45 5f 43 48 45 43 4b 5f 50 41 47  SQLITE_CHECK_PAG
11b1d 45 53 0a 20 20 20 20 20 20 70 50 67 2d 3e 70 61  ES.      pPg->pa
11b1e 67 65 48 61 73 68 20 3d 20 70 61 67 65 72 5f 70  geHash = pager_p
11b1f 61 67 65 68 61 73 68 28 70 50 67 29 3b 0a 23 65  agehash(pPg);.#e
11b20 6e 64 69 66 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ndif.    }.  }. 
11b21 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11b22 2a 0a 2a 2a 20 41 20 63 61 6c 6c 20 74 6f 20 74  *.** A call to t
11b23 68 69 73 20 72 6f 75 74 69 6e 65 20 74 65 6c 6c  his routine tell
11b24 73 20 74 68 65 20 70 61 67 65 72 20 74 68 61 74  s the pager that
11b25 20 69 66 20 61 20 72 6f 6c 6c 62 61 63 6b 20 6f   if a rollback o
11b26 63 63 75 72 73 2c 0a 2a 2a 20 69 74 20 69 73 20  ccurs,.** it is 
11b27 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20 74 6f  not necessary to
11b28 20 72 65 73 74 6f 72 65 20 74 68 65 20 64 61 74   restore the dat
11b29 61 20 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 70  a on the given p
11b2a 61 67 65 2e 20 20 54 68 69 73 0a 2a 2a 20 6d 65  age.  This.** me
11b2b 61 6e 73 20 74 68 61 74 20 74 68 65 20 70 61 67  ans that the pag
11b2c 65 72 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65  er does not have
11b2d 20 74 6f 20 72 65 63 6f 72 64 20 74 68 65 20 67   to record the g
11b2e 69 76 65 6e 20 70 61 67 65 20 69 6e 20 74 68 65  iven page in the
11b2f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  .** rollback jou
11b30 72 6e 61 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 77  rnal..**.** If w
11b31 65 20 68 61 76 65 20 6e 6f 74 20 79 65 74 20 61  e have not yet a
11b32 63 74 75 61 6c 6c 79 20 72 65 61 64 20 74 68 65  ctually read the
11b33 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 69 73   content of this
11b34 20 70 61 67 65 20 28 69 66 0a 2a 2a 20 74 68 65   page (if.** the
11b35 20 50 67 48 64 72 2e 6e 65 65 64 52 65 61 64 20   PgHdr.needRead 
11b36 66 6c 61 67 20 69 73 20 73 65 74 29 20 74 68 65  flag is set) the
11b37 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  n this routine a
11b38 63 74 73 20 61 73 20 61 20 70 72 6f 6d 69 73 65  cts as a promise
11b39 0a 2a 2a 20 74 68 61 74 20 77 65 20 77 69 6c 6c  .** that we will
11b3a 20 6e 65 76 65 72 20 6e 65 65 64 20 74 6f 20 72   never need to r
11b3b 65 61 64 20 74 68 65 20 70 61 67 65 20 63 6f 6e  ead the page con
11b3c 74 65 6e 74 20 69 6e 20 74 68 65 20 66 75 74 75  tent in the futu
11b3d 72 65 2e 0a 2a 2a 20 73 6f 20 74 68 65 20 6e 65  re..** so the ne
11b3e 65 64 52 65 61 64 20 66 6c 61 67 20 63 61 6e 20  edRead flag can 
11b3f 62 65 20 63 6c 65 61 72 65 64 20 61 74 20 74 68  be cleared at th
11b40 69 73 20 70 6f 69 6e 74 2e 0a 2a 2f 0a 53 51 4c  is point..*/.SQL
11b41 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
11b42 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
11b43 74 52 6f 6c 6c 62 61 63 6b 28 44 62 50 61 67 65  tRollback(DbPage
11b44 20 2a 70 50 67 29 7b 0a 20 20 50 61 67 65 72 20   *pPg){.  Pager 
11b45 2a 70 50 61 67 65 72 20 3d 20 70 50 67 2d 3e 70  *pPager = pPg->p
11b46 50 61 67 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  Pager;..  assert
11b47 28 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e  ( pPager->state>
11b48 3d 50 41 47 45 52 5f 52 45 53 45 52 56 45 44 20  =PAGER_RESERVED 
11b49 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
11b4a 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 69 73 20  journal file is 
11b4b 6e 6f 74 20 6f 70 65 6e 2c 20 6f 72 20 44 6f 6e  not open, or Don
11b4c 74 57 72 69 74 65 28 29 20 68 61 73 20 62 65 65  tWrite() has bee
11b4d 6e 20 63 61 6c 6c 65 64 20 6f 6e 0a 20 20 2a 2a  n called on.  **
11b4e 20 74 68 69 73 20 70 61 67 65 20 28 44 6f 6e 74   this page (Dont
11b4f 57 72 69 74 65 28 29 20 73 65 74 73 20 74 68 65  Write() sets the
11b50 20 61 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 20   alwaysRollback 
11b51 66 6c 61 67 29 2c 20 74 68 65 6e 20 74 68 69 73  flag), then this
11b52 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 69  .  ** function i
11b53 73 20 61 20 6e 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a  s a no-op..  */.
11b54 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 6a 6f    if( pPager->jo
11b55 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20 0a 20 20  urnalOpen==0 .  
11b56 20 7c 7c 20 73 71 6c 69 74 65 33 42 69 74 76 65   || sqlite3Bitve
11b57 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70 41  cTest(pPager->pA
11b58 6c 77 61 79 73 52 6f 6c 6c 62 61 63 6b 2c 20 70  lwaysRollback, p
11b59 50 67 2d 3e 70 67 6e 6f 29 0a 20 20 20 7c 7c 20  Pg->pgno).   || 
11b5a 70 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72  pPg->pgno>pPager
11b5b 2d 3e 6f 72 69 67 44 62 53 69 7a 65 0a 20 20 29  ->origDbSize.  )
11b5c 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
11b5d 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
11b5e 5f 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20  _SECURE_DELETE. 
11b5f 20 69 66 28 20 73 71 6c 69 74 65 33 42 69 74 76   if( sqlite3Bitv
11b60 65 63 54 65 73 74 28 70 50 61 67 65 72 2d 3e 70  ecTest(pPager->p
11b61 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e  InJournal, pPg->
11b62 70 67 6e 6f 29 21 3d 30 0a 20 20 20 7c 7c 20 70  pgno)!=0.   || p
11b63 50 67 2d 3e 70 67 6e 6f 3e 70 50 61 67 65 72 2d  Pg->pgno>pPager-
11b64 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20  >origDbSize ){. 
11b65 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 23     return;.  }.#
11b66 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 53  endif..  /* If S
11b67 45 43 55 52 45 5f 44 45 4c 45 54 45 20 69 73 20  ECURE_DELETE is 
11b68 64 69 73 61 62 6c 65 64 2c 20 74 68 65 6e 20 74  disabled, then t
11b69 68 65 72 65 20 69 73 20 6e 6f 20 77 61 79 20 74  here is no way t
11b6a 68 61 74 20 74 68 69 73 0a 20 20 2a 2a 20 72 6f  hat this.  ** ro
11b6b 75 74 69 6e 65 20 63 61 6e 20 62 65 20 63 61 6c  utine can be cal
11b6c 6c 65 64 20 6f 6e 20 61 20 70 61 67 65 20 66 6f  led on a page fo
11b6d 72 20 77 68 69 63 68 20 73 71 6c 69 74 65 33 50  r which sqlite3P
11b6e 61 67 65 72 44 6f 6e 74 57 72 69 74 65 28 29 0a  agerDontWrite().
11b6f 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 62 65 65    ** has not bee
11b70 6e 20 70 72 65 76 69 6f 75 73 6c 79 20 63 61 6c  n previously cal
11b71 6c 65 64 20 64 75 72 69 6e 67 20 74 68 65 20 73  led during the s
11b72 61 6d 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ame transaction.
11b73 0a 20 20 2a 2a 20 41 6e 64 20 69 66 20 44 6f 6e  .  ** And if Don
11b74 74 57 72 69 74 65 28 29 20 68 61 73 20 70 72 65  tWrite() has pre
11b75 76 69 6f 75 73 6c 79 20 62 65 65 6e 20 63 61 6c  viously been cal
11b76 6c 65 64 2c 20 74 68 65 20 66 6f 6c 6c 6f 77 69  led, the followi
11b77 6e 67 0a 20 20 2a 2a 20 63 6f 6e 64 69 74 69 6f  ng.  ** conditio
11b78 6e 73 20 6d 75 73 74 20 62 65 20 6d 65 74 2e 0a  ns must be met..
11b79 20 20 2a 2a 0a 20 20 2a 2a 20 28 4c 61 74 65 72    **.  ** (Later
11b7a 3a 29 20 20 4e 6f 74 20 74 72 75 65 2e 20 20 49  :)  Not true.  I
11b7b 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
11b7c 73 20 63 6f 72 72 75 70 74 65 64 20 62 79 20 68  s corrupted by h
11b7d 61 76 69 6e 67 20 64 75 70 6c 69 63 61 74 65 0a  aving duplicate.
11b7e 20 20 2a 2a 20 70 61 67 65 73 20 6f 6e 20 74 68    ** pages on th
11b7f 65 20 66 72 65 65 6c 69 73 74 20 28 65 78 3a 20  e freelist (ex: 
11b80 63 6f 72 72 75 70 74 39 2e 74 65 73 74 29 20 74  corrupt9.test) t
11b81 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hen the followin
11b82 67 20 69 73 20 6e 6f 74 0a 20 20 2a 2a 20 6e 65  g is not.  ** ne
11b83 63 65 73 73 61 72 69 6c 79 20 74 72 75 65 3a 0a  cessarily true:.
11b84 20 20 2a 2f 0a 20 20 2f 2a 20 61 73 73 65 72 74    */.  /* assert
11b85 28 20 21 70 50 67 2d 3e 69 6e 4a 6f 75 72 6e 61  ( !pPg->inJourna
11b86 6c 20 26 26 20 28 69 6e 74 29 70 50 67 2d 3e 70  l && (int)pPg->p
11b87 67 6e 6f 20 3c 3d 20 70 50 61 67 65 72 2d 3e 6f  gno <= pPager->o
11b88 72 69 67 44 62 53 69 7a 65 20 29 3b 20 2a 2f 0a  rigDbSize ); */.
11b89 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
11b8a 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 21 3d 30  r->pInJournal!=0
11b8b 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 69 74   );.  sqlite3Bit
11b8c 76 65 63 53 65 74 28 70 50 61 67 65 72 2d 3e 70  vecSet(pPager->p
11b8d 49 6e 4a 6f 75 72 6e 61 6c 2c 20 70 50 67 2d 3e  InJournal, pPg->
11b8e 70 67 6e 6f 29 3b 0a 20 20 70 50 67 2d 3e 66 6c  pgno);.  pPg->fl
11b8f 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f 4e 45  ags &= ~PGHDR_NE
11b90 45 44 5f 52 45 41 44 3b 0a 20 20 69 66 28 20 70  ED_READ;.  if( p
11b91 50 61 67 65 72 2d 3e 73 74 6d 74 49 6e 55 73 65  Pager->stmtInUse
11b92 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
11b93 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65  pPager->stmtSize
11b94 20 3e 3d 20 70 50 61 67 65 72 2d 3e 6f 72 69 67   >= pPager->orig
11b95 44 62 53 69 7a 65 20 29 3b 0a 20 20 20 20 73 71  DbSize );.    sq
11b96 6c 69 74 65 33 42 69 74 76 65 63 53 65 74 28 70  lite3BitvecSet(p
11b97 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 2c 20  Pager->pInStmt, 
11b98 70 50 67 2d 3e 70 67 6e 6f 29 3b 0a 20 20 7d 0a  pPg->pgno);.  }.
11b99 20 20 50 41 47 45 52 54 52 41 43 45 33 28 22 44    PAGERTRACE3("D
11b9a 4f 4e 54 5f 52 4f 4c 4c 42 41 43 4b 20 70 61 67  ONT_ROLLBACK pag
11b9b 65 20 25 64 20 6f 66 20 25 64 5c 6e 22 2c 20 70  e %d of %d\n", p
11b9c 50 67 2d 3e 70 67 6e 6f 2c 20 50 41 47 45 52 49  Pg->pgno, PAGERI
11b9d 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 49 4f  D(pPager));.  IO
11b9e 54 52 41 43 45 28 28 22 47 41 52 42 41 47 45 20  TRACE(("GARBAGE 
11b9f 25 70 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 72  %p %d\n", pPager
11ba0 2c 20 70 50 67 2d 3e 70 67 6e 6f 29 29 0a 7d 0a  , pPg->pgno)).}.
11ba1 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
11ba2 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
11ba3 6f 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  o increment the 
11ba4 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 63 68  database file ch
11ba5 61 6e 67 65 2d 63 6f 75 6e 74 65 72 2c 0a 2a 2a  ange-counter,.**
11ba6 20 73 74 6f 72 65 64 20 61 74 20 62 79 74 65 20   stored at byte 
11ba7 32 34 20 6f 66 20 74 68 65 20 70 61 67 65 72 20  24 of the pager 
11ba8 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  file..*/.static 
11ba9 69 6e 74 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  int pager_incr_c
11baa 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 50 61 67  hangecounter(Pag
11bab 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74 20  er *pPager, int 
11bac 69 73 44 69 72 65 63 74 29 7b 0a 20 20 50 67 48  isDirect){.  PgH
11bad 64 72 20 2a 70 50 67 48 64 72 3b 0a 20 20 75 33  dr *pPgHdr;.  u3
11bae 32 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72  2 change_counter
11baf 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
11bb0 49 54 45 5f 4f 4b 3b 0a 0a 23 69 66 6e 64 65 66  ITE_OK;..#ifndef
11bb1 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 41   SQLITE_ENABLE_A
11bb2 54 4f 4d 49 43 5f 57 52 49 54 45 0a 20 20 61 73  TOMIC_WRITE.  as
11bb3 73 65 72 74 28 20 69 73 44 69 72 65 63 74 3d 3d  sert( isDirect==
11bb4 30 20 29 3b 20 20 2f 2a 20 69 73 44 69 72 65 63  0 );  /* isDirec
11bb5 74 20 69 73 20 6f 6e 6c 79 20 74 72 75 65 20 66  t is only true f
11bb6 6f 72 20 61 74 6f 6d 69 63 20 77 72 69 74 65 73  or atomic writes
11bb7 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 66 28   */.#endif.  if(
11bb8 20 21 70 50 61 67 65 72 2d 3e 63 68 61 6e 67 65   !pPager->change
11bb9 43 6f 75 6e 74 44 6f 6e 65 20 29 7b 0a 20 20 20  CountDone ){.   
11bba 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65 20 31 20   /* Open page 1 
11bbb 6f 66 20 74 68 65 20 66 69 6c 65 20 66 6f 72 20  of the file for 
11bbc 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 20 20 20 20  writing. */.    
11bbd 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
11bbe 72 47 65 74 28 70 50 61 67 65 72 2c 20 31 2c 20  rGet(pPager, 1, 
11bbf 26 70 50 67 48 64 72 29 3b 0a 20 20 20 20 69 66  &pPgHdr);.    if
11bc0 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
11bc1 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
11bc2 20 20 69 66 28 20 21 69 73 44 69 72 65 63 74 20    if( !isDirect 
11bc3 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
11bc4 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
11bc5 70 50 67 48 64 72 29 3b 0a 20 20 20 20 20 20 69  pPgHdr);.      i
11bc6 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
11bc7 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
11bc8 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 50  te3PagerUnref(pP
11bc9 67 48 64 72 29 3b 0a 20 20 20 20 20 20 20 20 72  gHdr);.        r
11bca 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
11bcb 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  }.    }..    /* 
11bcc 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 76 61  Increment the va
11bcd 6c 75 65 20 6a 75 73 74 20 72 65 61 64 20 61 6e  lue just read an
11bce 64 20 77 72 69 74 65 20 69 74 20 62 61 63 6b 20  d write it back 
11bcf 74 6f 20 62 79 74 65 20 32 34 2e 20 2a 2f 0a 20  to byte 24. */. 
11bd0 20 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65     change_counte
11bd1 72 20 3d 20 73 71 6c 69 74 65 33 47 65 74 34 62  r = sqlite3Get4b
11bd2 79 74 65 28 28 75 38 2a 29 70 50 61 67 65 72 2d  yte((u8*)pPager-
11bd3 3e 64 62 46 69 6c 65 56 65 72 73 29 3b 0a 20 20  >dbFileVers);.  
11bd4 20 20 63 68 61 6e 67 65 5f 63 6f 75 6e 74 65 72    change_counter
11bd5 2b 2b 3b 0a 20 20 20 20 70 75 74 33 32 62 69 74  ++;.    put32bit
11bd6 73 28 28 28 63 68 61 72 2a 29 70 50 67 48 64 72  s(((char*)pPgHdr
11bd7 2d 3e 70 44 61 74 61 29 2b 32 34 2c 20 63 68 61  ->pData)+24, cha
11bd8 6e 67 65 5f 63 6f 75 6e 74 65 72 29 3b 0a 0a 23  nge_counter);..#
11bd9 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41  ifdef SQLITE_ENA
11bda 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49 54 45  BLE_ATOMIC_WRITE
11bdb 0a 20 20 20 20 69 66 28 20 69 73 44 69 72 65 63  .    if( isDirec
11bdc 74 20 26 26 20 70 50 61 67 65 72 2d 3e 66 64 2d  t && pPager->fd-
11bdd 3e 70 4d 65 74 68 6f 64 73 20 29 7b 0a 20 20 20  >pMethods ){.   
11bde 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a     const void *z
11bdf 42 75 66 20 3d 20 70 50 67 48 64 72 2d 3e 70 44  Buf = pPgHdr->pD
11be0 61 74 61 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  ata;.      rc = 
11be1 73 71 6c 69 74 65 33 4f 73 57 72 69 74 65 28 70  sqlite3OsWrite(p
11be2 50 61 67 65 72 2d 3e 66 64 2c 20 7a 42 75 66 2c  Pager->fd, zBuf,
11be3 20 70 50 61 67 65 72 2d 3e 70 61 67 65 53 69 7a   pPager->pageSiz
11be4 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e  e, 0);.    }.#en
11be5 64 69 66 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65  dif..    /* Rele
11be6 61 73 65 20 74 68 65 20 70 61 67 65 20 72 65 66  ase the page ref
11be7 65 72 65 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 73  erence. */.    s
11be8 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
11be9 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20 70 50  (pPgHdr);.    pP
11bea 61 67 65 72 2d 3e 63 68 61 6e 67 65 43 6f 75 6e  ager->changeCoun
11beb 74 44 6f 6e 65 20 3d 20 31 3b 0a 20 20 7d 0a 20  tDone = 1;.  }. 
11bec 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
11bed 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 70 61  *.** Sync the pa
11bee 67 65 72 20 66 69 6c 65 20 74 6f 20 64 69 73 6b  ger file to disk
11bef 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11bf0 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50  ATE int sqlite3P
11bf1 61 67 65 72 53 79 6e 63 28 50 61 67 65 72 20 2a  agerSync(Pager *
11bf2 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
11bf3 63 3b 0a 20 20 69 66 28 20 4d 45 4d 44 42 20 29  c;.  if( MEMDB )
11bf4 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54  {.    rc = SQLIT
11bf5 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  E_OK;.  }else{. 
11bf6 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
11bf7 73 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64  sSync(pPager->fd
11bf8 2c 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66  , pPager->sync_f
11bf9 6c 61 67 73 29 3b 0a 20 20 7d 0a 20 20 72 65 74  lags);.  }.  ret
11bfa 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
11bfb 20 53 79 6e 63 20 74 68 65 20 64 61 74 61 62 61   Sync the databa
11bfc 73 65 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  se file for the 
11bfd 70 61 67 65 72 20 70 50 61 67 65 72 2e 20 7a 4d  pager pPager. zM
11bfe 61 73 74 65 72 20 70 6f 69 6e 74 73 20 74 6f 20  aster points to 
11bff 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66 20 61  the name.** of a
11c00 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
11c01 66 69 6c 65 20 74 68 61 74 20 73 68 6f 75 6c 64  file that should
11c02 20 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f   be written into
11c03 20 74 68 65 20 69 6e 64 69 76 69 64 75 61 6c 0a   the individual.
11c04 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e  ** journal file.
11c05 20 7a 4d 61 73 74 65 72 20 6d 61 79 20 62 65 20   zMaster may be 
11c06 4e 55 4c 4c 2c 20 77 68 69 63 68 20 69 73 20 69  NULL, which is i
11c07 6e 74 65 72 70 72 65 74 65 64 20 61 73 20 6e 6f  nterpreted as no
11c08 20 6d 61 73 74 65 72 0a 2a 2a 20 6a 6f 75 72 6e   master.** journ
11c09 61 6c 20 28 61 20 73 69 6e 67 6c 65 20 64 61 74  al (a single dat
11c0a 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69 6f  abase transactio
11c0b 6e 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  n)..**.** This r
11c0c 6f 75 74 69 6e 65 20 65 6e 73 75 72 65 73 20 74  outine ensures t
11c0d 68 61 74 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  hat the journal 
11c0e 69 73 20 73 79 6e 63 65 64 2c 20 61 6c 6c 20 64  is synced, all d
11c0f 69 72 74 79 20 70 61 67 65 73 20 77 72 69 74 74  irty pages writt
11c10 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 64 61 74  en.** to the dat
11c11 61 62 61 73 65 20 66 69 6c 65 20 61 6e 64 20 74  abase file and t
11c12 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11c13 20 73 79 6e 63 65 64 2e 20 54 68 65 20 6f 6e 6c   synced. The onl
11c14 79 20 74 68 69 6e 67 20 74 68 61 74 0a 2a 2a 20  y thing that.** 
11c15 72 65 6d 61 69 6e 73 20 74 6f 20 63 6f 6d 6d 69  remains to commi
11c16 74 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  t the transactio
11c17 6e 20 69 73 20 74 6f 20 64 65 6c 65 74 65 20 74  n is to delete t
11c18 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
11c19 28 6f 72 0a 2a 2a 20 6d 61 73 74 65 72 20 6a 6f  (or.** master jo
11c1a 75 72 6e 61 6c 20 66 69 6c 65 20 69 66 20 73 70  urnal file if sp
11c1b 65 63 69 66 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20  ecified)..**.** 
11c1c 4e 6f 74 65 20 74 68 61 74 20 69 66 20 7a 4d 61  Note that if zMa
11c1d 73 74 65 72 3d 3d 4e 55 4c 4c 2c 20 74 68 69 73  ster==NULL, this
11c1e 20 64 6f 65 73 20 6e 6f 74 20 6f 76 65 72 77 72   does not overwr
11c1f 69 74 65 20 61 20 70 72 65 76 69 6f 75 73 20 76  ite a previous v
11c20 61 6c 75 65 0a 2a 2a 20 70 61 73 73 65 64 20 74  alue.** passed t
11c21 6f 20 61 6e 20 73 71 6c 69 74 65 33 50 61 67 65  o an sqlite3Page
11c22 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65 28  rCommitPhaseOne(
11c23 29 20 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66  ) call..**.** If
11c24 20 70 61 72 61 6d 65 74 65 72 20 6e 54 72 75 6e   parameter nTrun
11c25 63 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  c is non-zero, t
11c26 68 65 6e 20 74 68 65 20 70 61 67 65 72 20 66 69  hen the pager fi
11c27 6c 65 20 69 73 20 74 72 75 6e 63 61 74 65 64 20  le is truncated 
11c28 74 6f 0a 2a 2a 20 6e 54 72 75 6e 63 20 70 61 67  to.** nTrunc pag
11c29 65 73 20 28 74 68 69 73 20 69 73 20 75 73 65 64  es (this is used
11c2a 20 62 79 20 61 75 74 6f 2d 76 61 63 75 75 6d 20   by auto-vacuum 
11c2b 64 61 74 61 62 61 73 65 73 29 2e 0a 2a 2a 0a 2a  databases)..**.*
11c2c 2a 20 49 66 20 74 68 65 20 66 69 6e 61 6c 20 70  * If the final p
11c2d 61 72 61 6d 65 74 65 72 20 2d 20 6e 6f 53 79 6e  arameter - noSyn
11c2e 63 20 2d 20 69 73 20 74 72 75 65 2c 20 74 68 65  c - is true, the
11c2f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  n the database f
11c30 69 6c 65 20 69 74 73 65 6c 66 0a 2a 2a 20 69 73  ile itself.** is
11c31 20 6e 6f 74 20 73 79 6e 63 65 64 2e 20 54 68 65   not synced. The
11c32 20 63 61 6c 6c 65 72 20 6d 75 73 74 20 63 61 6c   caller must cal
11c33 6c 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 79  l sqlite3PagerSy
11c34 6e 63 28 29 20 64 69 72 65 63 74 6c 79 20 74 6f  nc() directly to
11c35 0a 2a 2a 20 73 79 6e 63 20 74 68 65 20 64 61 74  .** sync the dat
11c36 61 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72  abase file befor
11c37 65 20 63 61 6c 6c 69 6e 67 20 43 6f 6d 6d 69 74  e calling Commit
11c38 50 68 61 73 65 54 77 6f 28 29 20 74 6f 20 64 65  PhaseTwo() to de
11c39 6c 65 74 65 20 74 68 65 0a 2a 2a 20 6a 6f 75 72  lete the.** jour
11c3a 6e 61 6c 20 66 69 6c 65 20 69 6e 20 74 68 69 73  nal file in this
11c3b 20 63 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   case..*/.SQLITE
11c3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11c3d 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
11c3e 68 61 73 65 4f 6e 65 28 0a 20 20 50 61 67 65 72  haseOne(.  Pager
11c3f 20 2a 70 50 61 67 65 72 2c 20 0a 20 20 63 6f 6e   *pPager, .  con
11c40 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72  st char *zMaster
11c41 2c 20 0a 20 20 50 67 6e 6f 20 6e 54 72 75 6e 63  , .  Pgno nTrunc
11c42 2c 0a 20 20 69 6e 74 20 6e 6f 53 79 6e 63 0a 29  ,.  int noSync.)
11c43 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  {.  int rc = SQL
11c44 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70  ITE_OK;..  if( p
11c45 50 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29  Pager->errCode )
11c46 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61  {.    return pPa
11c47 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20  ger->errCode;.  
11c48 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 63 68  }..  /* If no ch
11c49 61 6e 67 65 73 20 68 61 76 65 20 62 65 65 6e 20  anges have been 
11c4a 6d 61 64 65 2c 20 77 65 20 63 61 6e 20 6c 65 61  made, we can lea
11c4b 76 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  ve the transacti
11c4c 6f 6e 20 65 61 72 6c 79 2e 0a 20 20 2a 2f 0a 20  on early..  */. 
11c4d 20 69 66 28 20 70 50 61 67 65 72 2d 3e 64 62 4d   if( pPager->dbM
11c4e 6f 64 69 66 69 65 64 3d 3d 30 20 26 26 0a 20 20  odified==0 &&.  
11c4f 20 20 20 20 20 20 28 70 50 61 67 65 72 2d 3e 6a        (pPager->j
11c50 6f 75 72 6e 61 6c 4d 6f 64 65 21 3d 50 41 47 45  ournalMode!=PAGE
11c51 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45  R_JOURNALMODE_DE
11c52 4c 45 54 45 20 7c 7c 0a 20 20 20 20 20 20 20 20  LETE ||.        
11c53 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75 73    pPager->exclus
11c54 69 76 65 4d 6f 64 65 21 3d 30 29 20 29 7b 0a 20  iveMode!=0) ){. 
11c55 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65     assert( pPage
11c56 72 2d 3e 64 69 72 74 79 43 61 63 68 65 3d 3d 30  r->dirtyCache==0
11c57 20 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72   || pPager->jour
11c58 6e 61 6c 4f 70 65 6e 3d 3d 30 20 29 3b 0a 20 20  nalOpen==0 );.  
11c59 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
11c5a 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 50 41 47 45 52  OK;.  }..  PAGER
11c5b 54 52 41 43 45 34 28 22 44 41 54 41 42 41 53 45  TRACE4("DATABASE
11c5c 20 53 59 4e 43 3a 20 46 69 6c 65 3d 25 73 20 7a   SYNC: File=%s z
11c5d 4d 61 73 74 65 72 3d 25 73 20 6e 54 72 75 6e 63  Master=%s nTrunc
11c5e 3d 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20 20 70  =%d\n", .      p
11c5f 50 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65  Pager->zFilename
11c60 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54 72 75 6e  , zMaster, nTrun
11c61 63 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69  c);..  /* If thi
11c62 73 20 69 73 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72  s is an in-memor
11c63 79 20 64 62 2c 20 6f 72 20 6e 6f 20 70 61 67 65  y db, or no page
11c64 73 20 68 61 76 65 20 62 65 65 6e 20 77 72 69 74  s have been writ
11c65 74 65 6e 20 74 6f 2c 20 6f 72 20 74 68 69 73 0a  ten to, or this.
11c66 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61    ** function ha
11c67 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63  s already been c
11c68 61 6c 6c 65 64 2c 20 69 74 20 69 73 20 61 20 6e  alled, it is a n
11c69 6f 2d 6f 70 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  o-op..  */.  if(
11c6a 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 21 3d   pPager->state!=
11c6b 50 41 47 45 52 5f 53 59 4e 43 45 44 20 26 26 20  PAGER_SYNCED && 
11c6c 21 4d 45 4d 44 42 20 26 26 20 70 50 61 67 65 72  !MEMDB && pPager
11c6d 2d 3e 64 69 72 74 79 43 61 63 68 65 20 29 7b 0a  ->dirtyCache ){.
11c6e 20 20 20 20 50 67 48 64 72 20 2a 70 50 67 3b 0a      PgHdr *pPg;.
11c6f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
11c70 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52 49  NABLE_ATOMIC_WRI
11c71 54 45 0a 20 20 20 20 2f 2a 20 54 68 65 20 61 74  TE.    /* The at
11c72 6f 6d 69 63 2d 77 72 69 74 65 20 6f 70 74 69 6d  omic-write optim
11c73 69 7a 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75  ization can be u
11c74 73 65 64 20 69 66 20 61 6c 6c 20 6f 66 20 74 68  sed if all of th
11c75 65 0a 20 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69  e.    ** followi
11c76 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 20 20 20  ng are true:.   
11c77 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20   **.    **    + 
11c78 54 68 65 20 66 69 6c 65 2d 73 79 73 74 65 6d 20  The file-system 
11c79 73 75 70 70 6f 72 74 73 20 74 68 65 20 61 74 6f  supports the ato
11c7a 6d 69 63 2d 77 72 69 74 65 20 70 72 6f 70 65 72  mic-write proper
11c7b 74 79 20 66 6f 72 0a 20 20 20 20 2a 2a 20 20 20  ty for.    **   
11c7c 20 20 20 62 6c 6f 63 6b 73 20 6f 66 20 73 69 7a     blocks of siz
11c7d 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 6e 64  e page-size, and
11c7e 0a 20 20 20 20 2a 2a 20 20 20 20 2b 20 54 68 69  .    **    + Thi
11c7f 73 20 63 6f 6d 6d 69 74 20 69 73 20 6e 6f 74 20  s commit is not 
11c80 70 61 72 74 20 6f 66 20 61 20 6d 75 6c 74 69 2d  part of a multi-
11c81 66 69 6c 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  file transaction
11c82 2c 20 61 6e 64 0a 20 20 20 20 2a 2a 20 20 20 20  , and.    **    
11c83 2b 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 70 61  + Exactly one pa
11c84 67 65 20 68 61 73 20 62 65 65 6e 20 6d 6f 64 69  ge has been modi
11c85 66 69 65 64 20 61 6e 64 20 73 74 6f 72 65 20 69  fied and store i
11c86 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69  n the journal fi
11c87 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  le..    **.    *
11c88 2a 20 49 66 20 74 68 65 20 6f 70 74 69 6d 69 7a  * If the optimiz
11c89 61 74 69 6f 6e 20 63 61 6e 20 62 65 20 75 73 65  ation can be use
11c8a 64 2c 20 74 68 65 6e 20 74 68 65 20 6a 6f 75 72  d, then the jour
11c8b 6e 61 6c 20 66 69 6c 65 20 77 69 6c 6c 20 6e 65  nal file will ne
11c8c 76 65 72 0a 20 20 20 20 2a 2a 20 62 65 20 63 72  ver.    ** be cr
11c8d 65 61 74 65 64 20 66 6f 72 20 74 68 69 73 20 74  eated for this t
11c8e 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 20 20  ransaction..    
11c8f 2a 2f 0a 20 20 20 20 69 6e 74 20 75 73 65 41 74  */.    int useAt
11c90 6f 6d 69 63 57 72 69 74 65 3b 0a 20 20 20 20 70  omicWrite;.    p
11c91 50 67 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63  Pg = sqlite3Pcac
11c92 68 65 44 69 72 74 79 4c 69 73 74 28 70 50 61 67  heDirtyList(pPag
11c93 65 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20  er->pPCache);.  
11c94 20 20 75 73 65 41 74 6f 6d 69 63 57 72 69 74 65    useAtomicWrite
11c95 20 3d 20 28 0a 20 20 20 20 20 20 20 20 21 7a 4d   = (.        !zM
11c96 61 73 74 65 72 20 26 26 20 0a 20 20 20 20 20 20  aster && .      
11c97 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61    pPager->journa
11c98 6c 4f 70 65 6e 20 26 26 0a 20 20 20 20 20 20 20  lOpen &&.       
11c99 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c   pPager->journal
11c9a 4f 66 66 3d 3d 6a 72 6e 6c 42 75 66 66 65 72 53  Off==jrnlBufferS
11c9b 69 7a 65 28 70 50 61 67 65 72 29 20 26 26 20 0a  ize(pPager) && .
11c9c 20 20 20 20 20 20 20 20 6e 54 72 75 6e 63 3d 3d          nTrunc==
11c9d 30 20 26 26 20 0a 20 20 20 20 20 20 20 20 28 70  0 && .        (p
11c9e 50 67 3d 3d 30 20 7c 7c 20 70 50 67 2d 3e 70 44  Pg==0 || pPg->pD
11c9f 69 72 74 79 3d 3d 30 29 0a 20 20 20 20 29 3b 0a  irty==0).    );.
11ca0 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
11ca1 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20  er->journalOpen 
11ca2 7c 7c 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  || pPager->journ
11ca3 61 6c 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f  alMode==PAGER_JO
11ca4 55 52 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 29 3b  URNALMODE_OFF );
11ca5 0a 20 20 20 20 69 66 28 20 75 73 65 41 74 6f 6d  .    if( useAtom
11ca6 69 63 57 72 69 74 65 20 29 7b 0a 20 20 20 20 20  icWrite ){.     
11ca7 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 6e   /* Update the n
11ca8 52 65 63 20 66 69 65 6c 64 20 69 6e 20 74 68 65  Rec field in the
11ca9 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20 2a   journal file. *
11caa 2f 0a 20 20 20 20 20 20 69 6e 74 20 6f 66 66 73  /.      int offs
11cab 65 74 20 3d 20 70 50 61 67 65 72 2d 3e 6a 6f 75  et = pPager->jou
11cac 72 6e 61 6c 48 64 72 20 2b 20 73 69 7a 65 6f 66  rnalHdr + sizeof
11cad 28 61 4a 6f 75 72 6e 61 6c 4d 61 67 69 63 29 3b  (aJournalMagic);
11cae 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50  .      assert(pP
11caf 61 67 65 72 2d 3e 6e 52 65 63 3d 3d 31 29 3b 0a  ager->nRec==1);.
11cb0 20 20 20 20 20 20 72 63 20 3d 20 77 72 69 74 65        rc = write
11cb1 33 32 62 69 74 73 28 70 50 61 67 65 72 2d 3e 6a  32bits(pPager->j
11cb2 66 64 2c 20 6f 66 66 73 65 74 2c 20 70 50 61 67  fd, offset, pPag
11cb3 65 72 2d 3e 6e 52 65 63 29 3b 0a 0a 20 20 20 20  er->nRec);..    
11cb4 20 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20    /* Update the 
11cb5 64 62 20 66 69 6c 65 20 63 68 61 6e 67 65 20 63  db file change c
11cb6 6f 75 6e 74 65 72 2e 20 54 68 65 20 66 6f 6c 6c  ounter. The foll
11cb7 6f 77 69 6e 67 20 63 61 6c 6c 20 77 69 6c 6c 20  owing call will 
11cb8 6d 6f 64 69 66 79 0a 20 20 20 20 20 20 2a 2a 20  modify.      ** 
11cb9 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65  the in-memory re
11cba 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20  presentation of 
11cbb 70 61 67 65 20 31 20 74 6f 20 69 6e 63 6c 75 64  page 1 to includ
11cbc 65 20 74 68 65 20 75 70 64 61 74 65 64 0a 20 20  e the updated.  
11cbd 20 20 20 20 2a 2a 20 63 68 61 6e 67 65 20 63 6f      ** change co
11cbe 75 6e 74 65 72 20 61 6e 64 20 74 68 65 6e 20 77  unter and then w
11cbf 72 69 74 65 20 70 61 67 65 20 31 20 64 69 72 65  rite page 1 dire
11cc0 63 74 6c 79 20 74 6f 20 74 68 65 20 64 61 74 61  ctly to the data
11cc1 62 61 73 65 0a 20 20 20 20 20 20 2a 2a 20 66 69  base.      ** fi
11cc2 6c 65 2e 20 42 65 63 61 75 73 65 20 6f 66 20 74  le. Because of t
11cc3 68 65 20 61 74 6f 6d 69 63 2d 77 72 69 74 65 20  he atomic-write 
11cc4 70 72 6f 70 65 72 74 79 20 6f 66 20 74 68 65 20  property of the 
11cc5 68 6f 73 74 20 66 69 6c 65 2d 73 79 73 74 65 6d  host file-system
11cc6 2c 20 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73  , .      ** this
11cc7 20 69 73 20 73 61 66 65 2e 0a 20 20 20 20 20 20   is safe..      
11cc8 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  */.      if( rc=
11cc9 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
11cca 20 20 20 20 20 20 72 63 20 3d 20 70 61 67 65 72        rc = pager
11ccb 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63 6f 75 6e  _incr_changecoun
11ccc 74 65 72 28 70 50 61 67 65 72 2c 20 31 29 3b 0a  ter(pPager, 1);.
11ccd 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
11cce 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  e{.      rc = sq
11ccf 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43 72 65 61  lite3JournalCrea
11cd0 74 65 28 70 50 61 67 65 72 2d 3e 6a 66 64 29 3b  te(pPager->jfd);
11cd1 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
11cd2 21 75 73 65 41 74 6f 6d 69 63 57 72 69 74 65 20  !useAtomicWrite 
11cd3 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  && rc==SQLITE_OK
11cd4 20 29 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f   ).#endif..    /
11cd5 2a 20 49 66 20 61 20 6d 61 73 74 65 72 20 6a 6f  * If a master jo
11cd6 75 72 6e 61 6c 20 66 69 6c 65 20 6e 61 6d 65 20  urnal file name 
11cd7 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e  has already been
11cd8 20 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 0a   written to the.
11cd9 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c 20 66      ** journal f
11cda 69 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 73 79 6e  ile, then no syn
11cdb 63 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 54  c is required. T
11cdc 68 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e  his happens when
11cdd 20 69 74 20 69 73 0a 20 20 20 20 2a 2a 20 77 72   it is.    ** wr
11cde 69 74 74 65 6e 2c 20 74 68 65 6e 20 74 68 65 20  itten, then the 
11cdf 70 72 6f 63 65 73 73 20 66 61 69 6c 73 20 74 6f  process fails to
11ce0 20 75 70 67 72 61 64 65 20 66 72 6f 6d 20 61 20   upgrade from a 
11ce1 52 45 53 45 52 56 45 44 20 74 6f 20 61 6e 0a 20  RESERVED to an. 
11ce2 20 20 20 2a 2a 20 45 58 43 4c 55 53 49 56 45 20     ** EXCLUSIVE 
11ce3 6c 6f 63 6b 2e 20 54 68 65 20 6e 65 78 74 20 74  lock. The next t
11ce4 69 6d 65 20 74 68 65 20 70 72 6f 63 65 73 73 20  ime the process 
11ce5 74 72 69 65 73 20 74 6f 20 63 6f 6d 6d 69 74 20  tries to commit 
11ce6 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  the.    ** trans
11ce7 61 63 74 69 6f 6e 20 74 68 65 20 6d 2d 6a 20 6e  action the m-j n
11ce8 61 6d 65 20 77 69 6c 6c 20 68 61 76 65 20 61 6c  ame will have al
11ce9 72 65 61 64 79 20 62 65 65 6e 20 77 72 69 74 74  ready been writt
11cea 65 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  en..    */.    i
11ceb 66 28 20 21 70 50 61 67 65 72 2d 3e 73 65 74 4d  f( !pPager->setM
11cec 61 73 74 65 72 20 29 7b 0a 20 20 20 20 20 20 72  aster ){.      r
11ced 63 20 3d 20 70 61 67 65 72 5f 69 6e 63 72 5f 63  c = pager_incr_c
11cee 68 61 6e 67 65 63 6f 75 6e 74 65 72 28 70 50 61  hangecounter(pPa
11cef 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 69  ger, 0);.      i
11cf0 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
11cf1 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69   ) goto sync_exi
11cf2 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  t;.      if( pPa
11cf3 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65  ger->journalMode
11cf4 21 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d  !=PAGER_JOURNALM
11cf5 4f 44 45 5f 4f 46 46 20 29 7b 0a 23 69 66 6e 64  ODE_OFF ){.#ifnd
11cf6 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
11cf7 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
11cf8 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30 20    if( nTrunc!=0 
11cf9 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
11cfa 49 66 20 74 68 69 73 20 74 72 61 6e 73 61 63 74  If this transact
11cfb 69 6f 6e 20 68 61 73 20 6d 61 64 65 20 74 68 65  ion has made the
11cfc 20 64 61 74 61 62 61 73 65 20 73 6d 61 6c 6c 65   database smalle
11cfd 72 2c 20 74 68 65 6e 20 61 6c 6c 20 70 61 67 65  r, then all page
11cfe 73 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62  s.          ** b
11cff 65 69 6e 67 20 64 69 73 63 61 72 64 65 64 20 62  eing discarded b
11d00 79 20 74 68 65 20 74 72 75 6e 63 61 74 69 6f 6e  y the truncation
11d01 20 6d 75 73 74 20 62 65 20 77 72 69 74 74 65 6e   must be written
11d02 20 74 6f 20 74 68 65 20 6a 6f 75 72 6e 61 6c 0a   to the journal.
11d03 20 20 20 20 20 20 20 20 20 20 2a 2a 20 66 69 6c            ** fil
11d04 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
11d05 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69            Pgno i
11d06 3b 0a 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f  ;.          Pgno
11d07 20 69 53 6b 69 70 20 3d 20 50 41 47 45 52 5f 4d   iSkip = PAGER_M
11d08 4a 5f 50 47 4e 4f 28 70 50 61 67 65 72 29 3b 0a  J_PGNO(pPager);.
11d09 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 20 69            for( i
11d0a 3d 6e 54 72 75 6e 63 2b 31 3b 20 69 3c 3d 70 50  =nTrunc+1; i<=pP
11d0b 61 67 65 72 2d 3e 6f 72 69 67 44 62 53 69 7a 65  ager->origDbSize
11d0c 3b 20 69 2b 2b 20 29 7b 0a 20 20 20 20 20 20 20  ; i++ ){.       
11d0d 20 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65       if( !sqlite
11d0e 33 42 69 74 76 65 63 54 65 73 74 28 70 50 61 67  3BitvecTest(pPag
11d0f 65 72 2d 3e 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20  er->pInJournal, 
11d10 69 29 20 26 26 20 69 21 3d 69 53 6b 69 70 20 29  i) && i!=iSkip )
11d11 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
11d12 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
11d13 72 47 65 74 28 70 50 61 67 65 72 2c 20 69 2c 20  rGet(pPager, i, 
11d14 26 70 50 67 29 3b 0a 20 20 20 20 20 20 20 20 20  &pPg);.         
11d15 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
11d16 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79  ITE_OK ) goto sy
11d17 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20 20 20 20  nc_exit;.       
11d18 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
11d19 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
11d1a 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  g);.            
11d1b 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
11d1c 72 65 66 28 70 50 67 29 3b 0a 20 20 20 20 20 20  ref(pPg);.      
11d1d 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d          if( rc!=
11d1e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
11d1f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20 20 20   sync_exit;.    
11d20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
11d21 20 20 20 20 7d 20 0a 20 20 20 20 20 20 20 20 7d      } .        }
11d22 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20  .#endif.        
11d23 72 63 20 3d 20 77 72 69 74 65 4d 61 73 74 65 72  rc = writeMaster
11d24 4a 6f 75 72 6e 61 6c 28 70 50 61 67 65 72 2c 20  Journal(pPager, 
11d25 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20  zMaster);.      
11d26 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
11d27 5f 4f 4b 20 29 20 67 6f 74 6f 20 73 79 6e 63 5f  _OK ) goto sync_
11d28 65 78 69 74 3b 0a 20 20 20 20 20 20 20 20 72 63  exit;.        rc
11d29 20 3d 20 73 79 6e 63 4a 6f 75 72 6e 61 6c 28 70   = syncJournal(p
11d2a 50 61 67 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a  Pager);.      }.
11d2b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
11d2c 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
11d2d 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 0a 23  to sync_exit;..#
11d2e 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
11d2f 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
11d30 20 20 69 66 28 20 6e 54 72 75 6e 63 21 3d 30 20    if( nTrunc!=0 
11d31 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
11d32 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63 61  lite3PagerTrunca
11d33 74 65 28 70 50 61 67 65 72 2c 20 6e 54 72 75 6e  te(pPager, nTrun
11d34 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  c);.      if( rc
11d35 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
11d36 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b 0a 20 20  to sync_exit;.  
11d37 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
11d38 2f 2a 20 57 72 69 74 65 20 61 6c 6c 20 64 69 72  /* Write all dir
11d39 74 79 20 70 61 67 65 73 20 74 6f 20 74 68 65 20  ty pages to the 
11d3a 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a 2f  database file */
11d3b 0a 20 20 20 20 70 50 67 20 3d 20 73 71 6c 69 74  .    pPg = sqlit
11d3c 65 33 50 63 61 63 68 65 44 69 72 74 79 4c 69 73  e3PcacheDirtyLis
11d3d 74 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68  t(pPager->pPCach
11d3e 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 70 61 67  e);.    rc = pag
11d3f 65 72 5f 77 72 69 74 65 5f 70 61 67 65 6c 69 73  er_write_pagelis
11d40 74 28 70 50 67 29 3b 0a 20 20 20 20 69 66 28 20  t(pPg);.    if( 
11d41 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
11d42 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72  .      assert( r
11d43 63 21 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52 5f  c!=SQLITE_IOERR_
11d44 42 4c 4f 43 4b 45 44 20 29 3b 0a 20 20 20 20 20  BLOCKED );.     
11d45 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 20 6d 69   /* The error mi
11d46 67 68 74 20 68 61 76 65 20 6c 65 66 74 20 74 68  ght have left th
11d47 65 20 64 69 72 74 79 20 6c 69 73 74 20 61 6c 6c  e dirty list all
11d48 20 66 6f 75 6c 65 64 20 75 70 20 68 65 72 65 2c   fouled up here,
11d49 0a 20 20 20 20 20 20 2a 2a 20 62 75 74 20 74 68  .      ** but th
11d4a 61 74 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 74  at does not matt
11d4b 65 72 20 62 65 63 61 75 73 65 20 69 66 20 74 68  er because if th
11d4c 65 20 69 66 20 74 68 65 20 64 69 72 74 79 20 6c  e if the dirty l
11d4d 69 73 74 20 64 69 64 0a 20 20 20 20 20 20 2a 2a  ist did.      **
11d4e 20 67 65 74 20 63 6f 72 72 75 70 74 65 64 2c 20   get corrupted, 
11d4f 74 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63  then the transac
11d50 74 69 6f 6e 20 77 69 6c 6c 20 72 6f 6c 6c 20 62  tion will roll b
11d51 61 63 6b 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  ack and.      **
11d52 20 64 69 73 63 61 72 64 20 74 68 65 20 64 69 72   discard the dir
11d53 74 79 20 6c 69 73 74 2e 20 20 54 68 65 72 65 20  ty list.  There 
11d54 69 73 20 61 6e 20 61 73 73 65 72 74 20 69 6e 0a  is an assert in.
11d55 20 20 20 20 20 20 2a 2a 20 70 61 67 65 72 5f 67        ** pager_g
11d56 65 74 5f 61 6c 6c 5f 64 69 72 74 79 5f 70 61 67  et_all_dirty_pag
11d57 65 73 28 29 20 74 68 61 74 20 76 65 72 69 66 69  es() that verifi
11d58 65 73 20 74 68 61 74 20 6e 6f 20 61 74 74 65 6d  es that no attem
11d59 70 74 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 6d  pt.      ** is m
11d5a 61 64 65 20 74 6f 20 75 73 65 20 61 6e 20 69 6e  ade to use an in
11d5b 76 61 6c 69 64 20 64 69 72 74 79 20 6c 69 73 74  valid dirty list
11d5c 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
11d5d 20 67 6f 74 6f 20 73 79 6e 63 5f 65 78 69 74 3b   goto sync_exit;
11d5e 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
11d5f 65 33 50 63 61 63 68 65 43 6c 65 61 6e 41 6c 6c  e3PcacheCleanAll
11d60 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
11d61 29 3b 0a 0a 20 20 20 20 2f 2a 20 53 79 6e 63 20  );..    /* Sync 
11d62 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
11d63 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70  e. */.    if( !p
11d64 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 20 26 26  Pager->noSync &&
11d65 20 21 6e 6f 53 79 6e 63 20 29 7b 0a 20 20 20 20   !noSync ){.    
11d66 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
11d67 53 79 6e 63 28 70 50 61 67 65 72 2d 3e 66 64 2c  Sync(pPager->fd,
11d68 20 70 50 61 67 65 72 2d 3e 73 79 6e 63 5f 66 6c   pPager->sync_fl
11d69 61 67 73 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ags);.    }.    
11d6a 49 4f 54 52 41 43 45 28 28 22 44 42 53 59 4e 43  IOTRACE(("DBSYNC
11d6b 20 25 70 5c 6e 22 2c 20 70 50 61 67 65 72 29 29   %p\n", pPager))
11d6c 0a 0a 20 20 20 20 70 50 61 67 65 72 2d 3e 73 74  ..    pPager->st
11d6d 61 74 65 20 3d 20 50 41 47 45 52 5f 53 59 4e 43  ate = PAGER_SYNC
11d6e 45 44 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  ED;.  }else if( 
11d6f 4d 45 4d 44 42 20 26 26 20 6e 54 72 75 6e 63 21  MEMDB && nTrunc!
11d70 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  =0 ){.    rc = s
11d71 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63  qlite3PagerTrunc
11d72 61 74 65 28 70 50 61 67 65 72 2c 20 6e 54 72 75  ate(pPager, nTru
11d73 6e 63 29 3b 0a 20 20 7d 0a 0a 73 79 6e 63 5f 65  nc);.  }..sync_e
11d74 78 69 74 3a 0a 20 20 69 66 28 20 72 63 3d 3d 53  xit:.  if( rc==S
11d75 51 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43  QLITE_IOERR_BLOC
11d76 4b 45 44 20 29 7b 0a 20 20 20 20 2f 2a 20 70 61  KED ){.    /* pa
11d77 67 65 72 5f 69 6e 63 72 5f 63 68 61 6e 67 65 63  ger_incr_changec
11d78 6f 75 6e 74 65 72 28 29 20 6d 61 79 20 61 74 74  ounter() may att
11d79 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e 20 61  empt to obtain a
11d7a 6e 20 65 78 63 6c 75 73 69 76 65 0a 20 20 20 20  n exclusive.    
11d7b 20 2a 20 6c 6f 63 6b 20 74 6f 20 73 70 69 6c 6c   * lock to spill
11d7c 20 74 68 65 20 63 61 63 68 65 20 61 6e 64 20 72   the cache and r
11d7d 65 74 75 72 6e 20 49 4f 45 52 52 5f 42 4c 4f 43  eturn IOERR_BLOC
11d7e 4b 45 44 2e 20 42 75 74 20 73 69 6e 63 65 20 0a  KED. But since .
11d7f 20 20 20 20 20 2a 20 74 68 65 72 65 20 69 73 20       * there is 
11d80 6e 6f 20 63 68 61 6e 63 65 20 74 68 65 20 63 61  no chance the ca
11d81 63 68 65 20 69 73 20 69 6e 63 6f 6e 73 69 73 74  che is inconsist
11d82 65 6e 74 2c 20 69 74 20 69 73 0a 20 20 20 20 20  ent, it is.     
11d83 2a 20 62 65 74 74 65 72 20 74 6f 20 72 65 74 75  * better to retu
11d84 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 2e 0a  rn SQLITE_BUSY..
11d85 20 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d       */.    rc =
11d86 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20   SQLITE_BUSY;.  
11d87 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
11d88 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20  .../*.** Commit 
11d89 61 6c 6c 20 63 68 61 6e 67 65 73 20 74 6f 20 74  all changes to t
11d8a 68 65 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  he database and 
11d8b 72 65 6c 65 61 73 65 20 74 68 65 20 77 72 69 74  release the writ
11d8c 65 20 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66  e lock..**.** If
11d8d 20 74 68 65 20 63 6f 6d 6d 69 74 20 66 61 69 6c   the commit fail
11d8e 73 20 66 6f 72 20 61 6e 79 20 72 65 61 73 6f 6e  s for any reason
11d8f 2c 20 61 20 72 6f 6c 6c 62 61 63 6b 20 61 74 74  , a rollback att
11d90 65 6d 70 74 20 69 73 20 6d 61 64 65 0a 2a 2a 20  empt is made.** 
11d91 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  and an error cod
11d92 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  e is returned.  
11d93 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 77 6f  If the commit wo
11d94 72 6b 65 64 2c 20 53 51 4c 49 54 45 5f 4f 4b 0a  rked, SQLITE_OK.
11d95 2a 2a 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  ** is returned..
11d96 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
11d97 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
11d98 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  erCommitPhaseTwo
11d99 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b  (Pager *pPager){
11d9a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
11d9b 54 45 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 50  TE_OK;..  if( pP
11d9c 61 67 65 72 2d 3e 65 72 72 43 6f 64 65 20 29 7b  ager->errCode ){
11d9d 0a 20 20 20 20 72 65 74 75 72 6e 20 70 50 61 67  .    return pPag
11d9e 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20 7d  er->errCode;.  }
11d9f 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73  .  if( pPager->s
11da0 74 61 74 65 3c 50 41 47 45 52 5f 52 45 53 45 52  tate<PAGER_RESER
11da1 56 45 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72  VED ){.    retur
11da2 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
11da3 20 20 7d 0a 20 20 69 66 28 20 70 50 61 67 65 72    }.  if( pPager
11da4 2d 3e 64 62 4d 6f 64 69 66 69 65 64 3d 3d 30 20  ->dbModified==0 
11da5 26 26 0a 20 20 20 20 20 20 20 20 28 70 50 61 67  &&.        (pPag
11da6 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 21  er->journalMode!
11da7 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
11da8 44 45 5f 44 45 4c 45 54 45 20 7c 7c 0a 20 20 20  DE_DELETE ||.   
11da9 20 20 20 20 20 20 20 70 50 61 67 65 72 2d 3e 65         pPager->e
11daa 78 63 6c 75 73 69 76 65 4d 6f 64 65 21 3d 30 29  xclusiveMode!=0)
11dab 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
11dac 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63  pPager->dirtyCac
11dad 68 65 3d 3d 30 20 7c 7c 20 70 50 61 67 65 72 2d  he==0 || pPager-
11dae 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 3d 3d 30 20  >journalOpen==0 
11daf 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  );.    return SQ
11db0 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 50  LITE_OK;.  }.  P
11db1 41 47 45 52 54 52 41 43 45 32 28 22 43 4f 4d 4d  AGERTRACE2("COMM
11db2 49 54 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49  IT %d\n", PAGERI
11db3 44 28 70 50 61 67 65 72 29 29 3b 0a 20 20 61 73  D(pPager));.  as
11db4 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
11db5 61 74 65 3d 3d 50 41 47 45 52 5f 53 59 4e 43 45  ate==PAGER_SYNCE
11db6 44 20 7c 7c 20 4d 45 4d 44 42 20 7c 7c 20 21 70  D || MEMDB || !p
11db7 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61 63 68  Pager->dirtyCach
11db8 65 20 29 3b 0a 20 20 72 63 20 3d 20 70 61 67 65  e );.  rc = page
11db9 72 5f 65 6e 64 5f 74 72 61 6e 73 61 63 74 69 6f  r_end_transactio
11dba 6e 28 70 50 61 67 65 72 2c 20 70 50 61 67 65 72  n(pPager, pPager
11dbb 2d 3e 73 65 74 4d 61 73 74 65 72 29 3b 0a 20 20  ->setMaster);.  
11dbc 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72  rc = pager_error
11dbd 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
11dbe 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
11dbf 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 6c 6c  .** Rollback all
11dc0 20 63 68 61 6e 67 65 73 2e 20 20 54 68 65 20 64   changes.  The d
11dc1 61 74 61 62 61 73 65 20 66 61 6c 6c 73 20 62 61  atabase falls ba
11dc2 63 6b 20 74 6f 20 50 41 47 45 52 5f 53 48 41 52  ck to PAGER_SHAR
11dc3 45 44 20 6d 6f 64 65 2e 0a 2a 2a 20 41 6c 6c 20  ED mode..** All 
11dc4 69 6e 2d 6d 65 6d 6f 72 79 20 63 61 63 68 65 20  in-memory cache 
11dc5 70 61 67 65 73 20 72 65 76 65 72 74 20 74 6f 20  pages revert to 
11dc6 74 68 65 69 72 20 6f 72 69 67 69 6e 61 6c 20 64  their original d
11dc7 61 74 61 20 63 6f 6e 74 65 6e 74 73 2e 0a 2a 2a  ata contents..**
11dc8 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 69 73 20   The journal is 
11dc9 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 54  deleted..**.** T
11dca 68 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6e 6e  his routine cann
11dcb 6f 74 20 66 61 69 6c 20 75 6e 6c 65 73 73 20 73  ot fail unless s
11dcc 6f 6d 65 20 6f 74 68 65 72 20 70 72 6f 63 65 73  ome other proces
11dcd 73 20 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 69  s is not followi
11dce 6e 67 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 63  ng.** the correc
11dcf 74 20 6c 6f 63 6b 69 6e 67 20 70 72 6f 74 6f 63  t locking protoc
11dd0 6f 6c 20 6f 72 20 75 6e 6c 65 73 73 20 73 6f 6d  ol or unless som
11dd1 65 20 6f 74 68 65 72 0a 2a 2a 20 70 72 6f 63 65  e other.** proce
11dd2 73 73 20 69 73 20 77 72 69 74 69 6e 67 20 74 72  ss is writing tr
11dd3 61 73 68 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75  ash into the jou
11dd4 72 6e 61 6c 20 66 69 6c 65 20 28 53 51 4c 49 54  rnal file (SQLIT
11dd5 45 5f 43 4f 52 52 55 50 54 29 20 6f 72 0a 2a 2a  E_CORRUPT) or.**
11dd6 20 75 6e 6c 65 73 73 20 61 20 70 72 69 6f 72 20   unless a prior 
11dd7 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 65 64 20  malloc() failed 
11dd8 28 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 2e 20  (SQLITE_NOMEM). 
11dd9 20 41 70 70 72 6f 70 72 69 61 74 65 20 65 72 72   Appropriate err
11dda 6f 72 0a 2a 2a 20 63 6f 64 65 73 20 61 72 65 20  or.** codes are 
11ddb 72 65 74 75 72 6e 65 64 20 66 6f 72 20 61 6c 6c  returned for all
11ddc 20 74 68 65 73 65 20 6f 63 63 61 73 69 6f 6e 73   these occasions
11ddd 2e 20 20 4f 74 68 65 72 77 69 73 65 2c 0a 2a 2a  .  Otherwise,.**
11dde 20 53 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65   SQLITE_OK is re
11ddf 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  turned..*/.SQLIT
11de0 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11de1 6c 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61  lite3PagerRollba
11de2 63 6b 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  ck(Pager *pPager
11de3 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
11de4 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 41 47 45 52  LITE_OK;.  PAGER
11de5 54 52 41 43 45 32 28 22 52 4f 4c 4c 42 41 43 4b  TRACE2("ROLLBACK
11de6 20 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28   %d\n", PAGERID(
11de7 70 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20  pPager));.  if( 
11de8 21 70 50 61 67 65 72 2d 3e 64 69 72 74 79 43 61  !pPager->dirtyCa
11de9 63 68 65 20 7c 7c 20 21 70 50 61 67 65 72 2d 3e  che || !pPager->
11dea 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 7b 0a 20  journalOpen ){. 
11deb 20 20 20 72 63 20 3d 20 70 61 67 65 72 5f 65 6e     rc = pager_en
11dec 64 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50  d_transaction(pP
11ded 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65  ager, pPager->se
11dee 74 4d 61 73 74 65 72 29 3b 0a 20 20 7d 65 6c 73  tMaster);.  }els
11def 65 20 69 66 28 20 70 50 61 67 65 72 2d 3e 65 72  e if( pPager->er
11df0 72 43 6f 64 65 20 26 26 20 70 50 61 67 65 72 2d  rCode && pPager-
11df1 3e 65 72 72 43 6f 64 65 21 3d 53 51 4c 49 54 45  >errCode!=SQLITE
11df2 5f 46 55 4c 4c 20 29 7b 0a 20 20 20 20 69 66 28  _FULL ){.    if(
11df3 20 70 50 61 67 65 72 2d 3e 73 74 61 74 65 3e 3d   pPager->state>=
11df4 50 41 47 45 52 5f 45 58 43 4c 55 53 49 56 45 20  PAGER_EXCLUSIVE 
11df5 29 7b 0a 20 20 20 20 20 20 70 61 67 65 72 5f 70  ){.      pager_p
11df6 6c 61 79 62 61 63 6b 28 70 50 61 67 65 72 2c 20  layback(pPager, 
11df7 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  0);.    }.    rc
11df8 20 3d 20 70 50 61 67 65 72 2d 3e 65 72 72 43 6f   = pPager->errCo
11df9 64 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  de;.  }else{.   
11dfa 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74 61   if( pPager->sta
11dfb 74 65 3d 3d 50 41 47 45 52 5f 52 45 53 45 52 56  te==PAGER_RESERV
11dfc 45 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ED ){.      int 
11dfd 72 63 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  rc2;.      rc = 
11dfe 70 61 67 65 72 5f 70 6c 61 79 62 61 63 6b 28 70  pager_playback(p
11dff 50 61 67 65 72 2c 20 30 29 3b 0a 20 20 20 20 20  Pager, 0);.     
11e00 20 72 63 32 20 3d 20 70 61 67 65 72 5f 65 6e 64   rc2 = pager_end
11e01 5f 74 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  _transaction(pPa
11e02 67 65 72 2c 20 70 50 61 67 65 72 2d 3e 73 65 74  ger, pPager->set
11e03 4d 61 73 74 65 72 29 3b 0a 20 20 20 20 20 20 69  Master);.      i
11e04 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
11e05 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d   ){.        rc =
11e06 20 72 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20   rc2;.      }.  
11e07 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72    }else{.      r
11e08 63 20 3d 20 70 61 67 65 72 5f 70 6c 61 79 62 61  c = pager_playba
11e09 63 6b 28 70 50 61 67 65 72 2c 20 30 29 3b 0a 20  ck(pPager, 0);. 
11e0a 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21 4d     }..    if( !M
11e0b 45 4d 44 42 20 29 7b 0a 20 20 20 20 20 20 70 50  EMDB ){.      pP
11e0c 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69  ager->dbSizeVali
11e0d 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a 20 20  d = 0;.    }..  
11e0e 20 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72    /* If an error
11e0f 20 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 61   occurs during a
11e10 20 52 4f 4c 4c 42 41 43 4b 2c 20 77 65 20 63 61   ROLLBACK, we ca
11e11 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20 74 72 75 73  n no longer trus
11e12 74 20 74 68 65 20 70 61 67 65 72 0a 20 20 20 20  t the pager.    
11e13 2a 2a 20 63 61 63 68 65 2e 20 53 6f 20 63 61 6c  ** cache. So cal
11e14 6c 20 70 61 67 65 72 5f 65 72 72 6f 72 28 29 20  l pager_error() 
11e15 6f 6e 20 74 68 65 20 77 61 79 20 6f 75 74 20 74  on the way out t
11e16 6f 20 6d 61 6b 65 20 61 6e 79 20 65 72 72 6f 72  o make any error
11e17 20 0a 20 20 20 20 2a 2a 20 70 65 72 73 69 73 74   .    ** persist
11e18 65 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ent..    */.    
11e19 72 63 20 3d 20 70 61 67 65 72 5f 65 72 72 6f 72  rc = pager_error
11e1a 28 70 50 61 67 65 72 2c 20 72 63 29 3b 0a 20 20  (pPager, rc);.  
11e1b 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
11e1c 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54  ../*.** Return T
11e1d 52 55 45 20 69 66 20 74 68 65 20 64 61 74 61 62  RUE if the datab
11e1e 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  ase file is open
11e1f 65 64 20 72 65 61 64 2d 6f 6e 6c 79 2e 20 20 52  ed read-only.  R
11e20 65 74 75 72 6e 20 46 41 4c 53 45 0a 2a 2a 20 69  eturn FALSE.** i
11e21 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
11e22 73 20 28 69 6e 20 74 68 65 6f 72 79 29 20 77 72  s (in theory) wr
11e23 69 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54  itable..*/.SQLIT
11e24 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
11e25 6c 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64  lite3PagerIsread
11e26 6f 6e 6c 79 28 50 61 67 65 72 20 2a 70 50 61 67  only(Pager *pPag
11e27 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
11e28 61 67 65 72 2d 3e 72 65 61 64 4f 6e 6c 79 3b 0a  ager->readOnly;.
11e29 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
11e2a 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
11e2b 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68 65 20  ferences to the 
11e2c 70 61 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  pager..*/.SQLITE
11e2d 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
11e2e 69 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e  ite3PagerRefcoun
11e2f 74 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  t(Pager *pPager)
11e30 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
11e31 65 33 50 63 61 63 68 65 52 65 66 43 6f 75 6e 74  e3PcacheRefCount
11e32 28 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65  (pPager->pPCache
11e33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
11e34 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
11e35 20 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74   references to t
11e36 68 65 20 73 70 65 63 69 66 69 65 64 20 70 61 67  he specified pag
11e37 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
11e38 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
11e39 50 61 67 65 72 50 61 67 65 52 65 66 63 6f 75 6e  PagerPageRefcoun
11e3a 74 28 44 62 50 61 67 65 20 2a 70 50 61 67 65 29  t(DbPage *pPage)
11e3b 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74  {.  return sqlit
11e3c 65 33 50 63 61 63 68 65 50 61 67 65 52 65 66 63  e3PcachePageRefc
11e3d 6f 75 6e 74 28 70 50 61 67 65 29 3b 0a 7d 0a 0a  ount(pPage);.}..
11e3e 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
11e3f 53 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  ST./*.** This ro
11e40 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
11e41 72 20 74 65 73 74 69 6e 67 20 61 6e 64 20 61 6e  r testing and an
11e42 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a  alysis only..*/.
11e43 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
11e44 6e 74 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72  nt *sqlite3Pager
11e45 53 74 61 74 73 28 50 61 67 65 72 20 2a 70 50 61  Stats(Pager *pPa
11e46 67 65 72 29 7b 0a 20 20 73 74 61 74 69 63 20 69  ger){.  static i
11e47 6e 74 20 61 5b 31 31 5d 3b 0a 20 20 61 5b 30 5d  nt a[11];.  a[0]
11e48 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65   = sqlite3Pcache
11e49 52 65 66 43 6f 75 6e 74 28 70 50 61 67 65 72 2d  RefCount(pPager-
11e4a 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61 5b 31  >pPCache);.  a[1
11e4b 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68  ] = sqlite3Pcach
11e4c 65 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  ePagecount(pPage
11e4d 72 2d 3e 70 50 43 61 63 68 65 29 3b 0a 20 20 61  r->pPCache);.  a
11e4e 5b 32 5d 20 3d 20 73 71 6c 69 74 65 33 50 63 61  [2] = sqlite3Pca
11e4f 63 68 65 47 65 74 43 61 63 68 65 73 69 7a 65 28  cheGetCachesize(
11e50 70 50 61 67 65 72 2d 3e 70 50 43 61 63 68 65 29  pPager->pPCache)
11e51 3b 0a 20 20 61 5b 33 5d 20 3d 20 70 50 61 67 65  ;.  a[3] = pPage
11e52 72 2d 3e 64 62 53 69 7a 65 56 61 6c 69 64 20 3f  r->dbSizeValid ?
11e53 20 28 69 6e 74 29 20 70 50 61 67 65 72 2d 3e 64   (int) pPager->d
11e54 62 53 69 7a 65 20 3a 20 2d 31 3b 0a 20 20 61 5b  bSize : -1;.  a[
11e55 34 5d 20 3d 20 70 50 61 67 65 72 2d 3e 73 74 61  4] = pPager->sta
11e56 74 65 3b 0a 20 20 61 5b 35 5d 20 3d 20 70 50 61  te;.  a[5] = pPa
11e57 67 65 72 2d 3e 65 72 72 43 6f 64 65 3b 0a 20 20  ger->errCode;.  
11e58 61 5b 36 5d 20 3d 20 70 50 61 67 65 72 2d 3e 6e  a[6] = pPager->n
11e59 48 69 74 3b 0a 20 20 61 5b 37 5d 20 3d 20 70 50  Hit;.  a[7] = pP
11e5a 61 67 65 72 2d 3e 6e 4d 69 73 73 3b 0a 20 20 61  ager->nMiss;.  a
11e5b 5b 38 5d 20 3d 20 30 3b 20 20 2f 2a 20 55 73 65  [8] = 0;  /* Use
11e5c 64 20 74 6f 20 62 65 20 70 50 61 67 65 72 2d 3e  d to be pPager->
11e5d 6e 4f 76 66 6c 20 2a 2f 0a 20 20 61 5b 39 5d 20  nOvfl */.  a[9] 
11e5e 3d 20 70 50 61 67 65 72 2d 3e 6e 52 65 61 64 3b  = pPager->nRead;
11e5f 0a 20 20 61 5b 31 30 5d 20 3d 20 70 50 61 67 65  .  a[10] = pPage
11e60 72 2d 3e 6e 57 72 69 74 65 3b 0a 20 20 72 65 74  r->nWrite;.  ret
11e61 75 72 6e 20 61 3b 0a 7d 0a 53 51 4c 49 54 45 5f  urn a;.}.SQLITE_
11e62 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
11e63 74 65 33 50 61 67 65 72 49 73 4d 65 6d 64 62 28  te3PagerIsMemdb(
11e64 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11e65 20 20 72 65 74 75 72 6e 20 4d 45 4d 44 42 3b 0a    return MEMDB;.
11e66 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
11e67 53 65 74 20 74 68 65 20 73 74 61 74 65 6d 65 6e  Set the statemen
11e68 74 20 72 6f 6c 6c 62 61 63 6b 20 70 6f 69 6e 74  t rollback point
11e69 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
11e6a 74 69 6e 65 20 73 68 6f 75 6c 64 20 62 65 20 63  tine should be c
11e6b 61 6c 6c 65 64 20 77 69 74 68 20 74 68 65 20 74  alled with the t
11e6c 72 61 6e 73 61 63 74 69 6f 6e 20 6a 6f 75 72 6e  ransaction journ
11e6d 61 6c 20 61 6c 72 65 61 64 79 0a 2a 2a 20 6f 70  al already.** op
11e6e 65 6e 2e 20 20 41 20 6e 65 77 20 73 74 61 74 65  en.  A new state
11e6f 6d 65 6e 74 20 6a 6f 75 72 6e 61 6c 20 69 73 20  ment journal is 
11e70 63 72 65 61 74 65 64 20 74 68 61 74 20 63 61 6e  created that can
11e71 20 62 65 20 75 73 65 64 20 74 6f 20 72 6f 6c 6c   be used to roll
11e72 62 61 63 6b 0a 2a 2a 20 63 68 61 6e 67 65 73 20  back.** changes 
11e73 6f 66 20 61 20 73 69 6e 67 6c 65 20 53 51 4c 20  of a single SQL 
11e74 63 6f 6d 6d 61 6e 64 20 77 69 74 68 69 6e 20 61  command within a
11e75 20 6c 61 72 67 65 72 20 74 72 61 6e 73 61 63 74   larger transact
11e76 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ion..*/.static i
11e77 6e 74 20 70 61 67 65 72 53 74 6d 74 42 65 67 69  nt pagerStmtBegi
11e78 6e 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 29  n(Pager *pPager)
11e79 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 61 73  {.  int rc;.  as
11e7a 73 65 72 74 28 20 21 70 50 61 67 65 72 2d 3e 73  sert( !pPager->s
11e7b 74 6d 74 49 6e 55 73 65 20 29 3b 0a 20 20 61 73  tmtInUse );.  as
11e7c 73 65 72 74 28 20 70 50 61 67 65 72 2d 3e 73 74  sert( pPager->st
11e7d 61 74 65 3e 3d 50 41 47 45 52 5f 53 48 41 52 45  ate>=PAGER_SHARE
11e7e 44 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  D );.  assert( p
11e7f 50 61 67 65 72 2d 3e 64 62 53 69 7a 65 56 61 6c  Pager->dbSizeVal
11e80 69 64 20 29 3b 0a 20 20 50 41 47 45 52 54 52 41  id );.  PAGERTRA
11e81 43 45 32 28 22 53 54 4d 54 2d 42 45 47 49 4e 20  CE2("STMT-BEGIN 
11e82 25 64 5c 6e 22 2c 20 50 41 47 45 52 49 44 28 70  %d\n", PAGERID(p
11e83 50 61 67 65 72 29 29 3b 0a 20 20 69 66 28 20 21  Pager));.  if( !
11e84 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e 61 6c 4f  pPager->journalO
11e85 70 65 6e 20 29 7b 0a 20 20 20 20 70 50 61 67 65  pen ){.    pPage
11e86 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65 6e 20  r->stmtAutoopen 
11e87 3d 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  = 1;.    return 
11e88 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20  SQLITE_OK;.  }. 
11e89 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72 2d   assert( pPager-
11e8a 3e 6a 6f 75 72 6e 61 6c 4f 70 65 6e 20 29 3b 0a  >journalOpen );.
11e8b 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 72    assert( pPager
11e8c 2d 3e 70 49 6e 53 74 6d 74 3d 3d 30 20 29 3b 0a  ->pInStmt==0 );.
11e8d 20 20 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d    pPager->pInStm
11e8e 74 20 3d 20 73 71 6c 69 74 65 33 42 69 74 76 65  t = sqlite3Bitve
11e8f 63 43 72 65 61 74 65 28 70 50 61 67 65 72 2d 3e  cCreate(pPager->
11e90 64 62 53 69 7a 65 29 3b 0a 20 20 69 66 28 20 70  dbSize);.  if( p
11e91 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 3d 3d  Pager->pInStmt==
11e92 30 20 29 7b 0a 20 20 20 20 2f 2a 20 73 71 6c 69  0 ){.    /* sqli
11e93 74 65 33 4f 73 4c 6f 63 6b 28 70 50 61 67 65 72  te3OsLock(pPager
11e94 2d 3e 66 64 2c 20 53 48 41 52 45 44 5f 4c 4f 43  ->fd, SHARED_LOC
11e95 4b 29 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  K); */.    retur
11e96 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
11e97 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73 74    }.  pPager->st
11e98 6d 74 4a 53 69 7a 65 20 3d 20 70 50 61 67 65 72  mtJSize = pPager
11e99 2d 3e 6a 6f 75 72 6e 61 6c 4f 66 66 3b 0a 20 20  ->journalOff;.  
11e9a 70 50 61 67 65 72 2d 3e 73 74 6d 74 53 69 7a 65  pPager->stmtSize
11e9b 20 3d 20 70 50 61 67 65 72 2d 3e 64 62 53 69 7a   = pPager->dbSiz
11e9c 65 3b 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d  e;.  pPager->stm
11e9d 74 48 64 72 4f 66 66 20 3d 20 30 3b 0a 20 20 70  tHdrOff = 0;.  p
11e9e 50 61 67 65 72 2d 3e 73 74 6d 74 43 6b 73 75 6d  Pager->stmtCksum
11e9f 20 3d 20 70 50 61 67 65 72 2d 3e 63 6b 73 75 6d   = pPager->cksum
11ea0 49 6e 69 74 3b 0a 20 20 69 66 28 20 21 70 50 61  Init;.  if( !pPa
11ea1 67 65 72 2d 3e 73 74 6d 74 4f 70 65 6e 20 29 7b  ger->stmtOpen ){
11ea2 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 72 2d  .    if( pPager-
11ea3 3e 6a 6f 75 72 6e 61 6c 4d 6f 64 65 3d 3d 50 41  >journalMode==PA
11ea4 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
11ea5 4d 45 4d 4f 52 59 20 29 7b 0a 20 20 20 20 20 20  MEMORY ){.      
11ea6 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61  sqlite3MemJourna
11ea7 6c 4f 70 65 6e 28 70 50 61 67 65 72 2d 3e 73 74  lOpen(pPager->st
11ea8 66 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  fd);.    }else{.
11ea9 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
11eaa 65 33 50 61 67 65 72 4f 70 65 6e 74 65 6d 70 28  e3PagerOpentemp(
11eab 70 50 61 67 65 72 2c 20 70 50 61 67 65 72 2d 3e  pPager, pPager->
11eac 73 74 66 64 2c 20 53 51 4c 49 54 45 5f 4f 50 45  stfd, SQLITE_OPE
11ead 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 29 3b 0a 20  N_SUBJOURNAL);. 
11eae 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20       if( rc ){. 
11eaf 20 20 20 20 20 20 20 67 6f 74 6f 20 73 74 6d 74         goto stmt
11eb0 5f 62 65 67 69 6e 5f 66 61 69 6c 65 64 3b 0a 20  _begin_failed;. 
11eb1 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
11eb2 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 4f 70 65   pPager->stmtOpe
11eb3 6e 20 3d 20 31 3b 0a 20 20 20 20 70 50 61 67 65  n = 1;.    pPage
11eb4 72 2d 3e 73 74 6d 74 4e 52 65 63 20 3d 20 30 3b  r->stmtNRec = 0;
11eb5 0a 20 20 7d 0a 20 20 70 50 61 67 65 72 2d 3e 73  .  }.  pPager->s
11eb6 74 6d 74 49 6e 55 73 65 20 3d 20 31 3b 0a 20 20  tmtInUse = 1;.  
11eb7 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
11eb8 3b 0a 20 0a 73 74 6d 74 5f 62 65 67 69 6e 5f 66  ;. .stmt_begin_f
11eb9 61 69 6c 65 64 3a 0a 20 20 69 66 28 20 70 50 61  ailed:.  if( pPa
11eba 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20 29 7b 0a  ger->pInStmt ){.
11ebb 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76 65      sqlite3Bitve
11ebc 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72 2d  cDestroy(pPager-
11ebd 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 20 20 20 70  >pInStmt);.    p
11ebe 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20 3d  Pager->pInStmt =
11ebf 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
11ec0 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52   rc;.}.SQLITE_PR
11ec1 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
11ec2 33 50 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28  3PagerStmtBegin(
11ec3 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11ec4 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
11ec5 20 70 61 67 65 72 53 74 6d 74 42 65 67 69 6e 28   pagerStmtBegin(
11ec6 70 50 61 67 65 72 29 3b 0a 20 20 72 65 74 75 72  pPager);.  retur
11ec7 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
11ec8 6f 6d 6d 69 74 20 61 20 73 74 61 74 65 6d 65 6e  ommit a statemen
11ec9 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  t..*/.SQLITE_PRI
11eca 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
11ecb 50 61 67 65 72 53 74 6d 74 43 6f 6d 6d 69 74 28  PagerStmtCommit(
11ecc 50 61 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a  Pager *pPager){.
11ecd 20 20 69 66 28 20 70 50 61 67 65 72 2d 3e 73 74    if( pPager->st
11ece 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20 20 20 50  mtInUse ){.    P
11ecf 41 47 45 52 54 52 41 43 45 32 28 22 53 54 4d 54  AGERTRACE2("STMT
11ed0 2d 43 4f 4d 4d 49 54 20 25 64 5c 6e 22 2c 20 50  -COMMIT %d\n", P
11ed1 41 47 45 52 49 44 28 70 50 61 67 65 72 29 29 3b  AGERID(pPager));
11ed2 0a 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76  .    sqlite3Bitv
11ed3 65 63 44 65 73 74 72 6f 79 28 70 50 61 67 65 72  ecDestroy(pPager
11ed4 2d 3e 70 49 6e 53 74 6d 74 29 3b 0a 20 20 20 20  ->pInStmt);.    
11ed5 70 50 61 67 65 72 2d 3e 70 49 6e 53 74 6d 74 20  pPager->pInStmt 
11ed6 3d 20 30 3b 0a 20 20 20 20 70 50 61 67 65 72 2d  = 0;.    pPager-
11ed7 3e 73 74 6d 74 4e 52 65 63 20 3d 20 30 3b 0a 20  >stmtNRec = 0;. 
11ed8 20 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 49     pPager->stmtI
11ed9 6e 55 73 65 20 3d 20 30 3b 0a 20 20 20 20 69 66  nUse = 0;.    if
11eda 28 20 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f  ( sqlite3IsMemJo
11edb 75 72 6e 61 6c 28 70 50 61 67 65 72 2d 3e 73 74  urnal(pPager->st
11edc 66 64 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  fd) ){.      sql
11edd 69 74 65 33 4f 73 54 72 75 6e 63 61 74 65 28 70  ite3OsTruncate(p
11ede 50 61 67 65 72 2d 3e 73 74 66 64 2c 20 30 29 3b  Pager->stfd, 0);
11edf 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 50 61  .    }.  }.  pPa
11ee0 67 65 72 2d 3e 73 74 6d 74 41 75 74 6f 6f 70 65  ger->stmtAutoope
11ee1 6e 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20  n = 0;.  return 
11ee2 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
11ee3 0a 2a 2a 20 52 6f 6c 6c 62 61 63 6b 20 61 20 73  .** Rollback a s
11ee4 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
11ee5 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
11ee6 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74  sqlite3PagerStmt
11ee7 52 6f 6c 6c 62 61 63 6b 28 50 61 67 65 72 20 2a  Rollback(Pager *
11ee8 70 50 61 67 65 72 29 7b 0a 20 20 69 6e 74 20 72  pPager){.  int r
11ee9 63 3b 0a 20 20 69 66 28 20 70 50 61 67 65 72 2d  c;.  if( pPager-
11eea 3e 73 74 6d 74 49 6e 55 73 65 20 29 7b 0a 20 20  >stmtInUse ){.  
11eeb 20 20 50 41 47 45 52 54 52 41 43 45 32 28 22 53    PAGERTRACE2("S
11eec 54 4d 54 2d 52 4f 4c 4c 42 41 43 4b 20 25 64 5c  TMT-ROLLBACK %d\
11eed 6e 22 2c 20 50 41 47 45 52 49 44 28 70 50 61 67  n", PAGERID(pPag
11eee 65 72 29 29 3b 0a 20 20 20 20 72 63 20 3d 20 70  er));.    rc = p
11eef 61 67 65 72 5f 73 74 6d 74 5f 70 6c 61 79 62 61  ager_stmt_playba
11ef0 63 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20  ck(pPager);.    
11ef1 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d 74  sqlite3PagerStmt
11ef2 43 6f 6d 6d 69 74 28 70 50 61 67 65 72 29 3b 0a  Commit(pPager);.
11ef3 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63 20    }else{.    rc 
11ef4 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  = SQLITE_OK;.  }
11ef5 0a 20 20 70 50 61 67 65 72 2d 3e 73 74 6d 74 41  .  pPager->stmtA
11ef6 75 74 6f 6f 70 65 6e 20 3d 20 30 3b 0a 20 20 72  utoopen = 0;.  r
11ef7 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
11ef8 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66 75  ** Return the fu
11ef9 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  ll pathname of t
11efa 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
11efb 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11efc 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
11efd 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
11efe 6e 61 6d 65 28 50 61 67 65 72 20 2a 70 50 61 67  name(Pager *pPag
11eff 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50  er){.  return pP
11f00 61 67 65 72 2d 3e 7a 46 69 6c 65 6e 61 6d 65 3b  ager->zFilename;
11f01 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
11f02 20 74 68 65 20 56 46 53 20 73 74 72 75 63 74 75   the VFS structu
11f03 72 65 20 66 6f 72 20 74 68 65 20 70 61 67 65 72  re for the pager
11f04 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11f05 41 54 45 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ATE const sqlite
11f06 33 5f 76 66 73 20 2a 73 71 6c 69 74 65 33 50 61  3_vfs *sqlite3Pa
11f07 67 65 72 56 66 73 28 50 61 67 65 72 20 2a 70 50  gerVfs(Pager *pP
11f08 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ager){.  return 
11f09 70 50 61 67 65 72 2d 3e 70 56 66 73 3b 0a 7d 0a  pPager->pVfs;.}.
11f0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
11f0b 65 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 66 6f  e file handle fo
11f0c 72 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  r the database f
11f0d 69 6c 65 20 61 73 73 6f 63 69 61 74 65 64 0a 2a  ile associated.*
11f0e 2a 20 77 69 74 68 20 74 68 65 20 70 61 67 65 72  * with the pager
11f0f 2e 20 20 54 68 69 73 20 6d 69 67 68 74 20 72 65  .  This might re
11f10 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 74 68 65  turn NULL if the
11f11 20 66 69 6c 65 20 68 61 73 0a 2a 2a 20 6e 6f 74   file has.** not
11f12 20 79 65 74 20 62 65 65 6e 20 6f 70 65 6e 65 64   yet been opened
11f13 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11f14 41 54 45 20 73 71 6c 69 74 65 33 5f 66 69 6c 65  ATE sqlite3_file
11f15 20 2a 73 71 6c 69 74 65 33 50 61 67 65 72 46 69   *sqlite3PagerFi
11f16 6c 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  le(Pager *pPager
11f17 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
11f18 65 72 2d 3e 66 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  er->fd;.}../*.**
11f19 20 52 65 74 75 72 6e 20 74 68 65 20 64 69 72 65   Return the dire
11f1a 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 61 74  ctory of the dat
11f1b 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2f 0a 53  abase file..*/.S
11f1c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f  QLITE_PRIVATE co
11f1d 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65  nst char *sqlite
11f1e 33 50 61 67 65 72 44 69 72 6e 61 6d 65 28 50 61  3PagerDirname(Pa
11f1f 67 65 72 20 2a 70 50 61 67 65 72 29 7b 0a 20 20  ger *pPager){.  
11f20 72 65 74 75 72 6e 20 70 50 61 67 65 72 2d 3e 7a  return pPager->z
11f21 44 69 72 65 63 74 6f 72 79 3b 0a 7d 0a 0a 2f 2a  Directory;.}../*
11f22 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 66  .** Return the f
11f23 75 6c 6c 20 70 61 74 68 6e 61 6d 65 20 6f 66 20  ull pathname of 
11f24 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  the journal file
11f25 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
11f26 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ATE const char *
11f27 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
11f28 6e 61 6c 6e 61 6d 65 28 50 61 67 65 72 20 2a 70  nalname(Pager *p
11f29 50 61 67 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  Pager){.  return
11f2a 20 70 50 61 67 65 72 2d 3e 7a 4a 6f 75 72 6e 61   pPager->zJourna
11f2b 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  l;.}../*.** Retu
11f2c 72 6e 20 74 72 75 65 20 69 66 20 66 73 79 6e 63  rn true if fsync
11f2d 28 29 20 63 61 6c 6c 73 20 61 72 65 20 64 69 73  () calls are dis
11f2e 61 62 6c 65 64 20 66 6f 72 20 74 68 69 73 20 70  abled for this p
11f2f 61 67 65 72 2e 20 20 52 65 74 75 72 6e 20 46 41  ager.  Return FA
11f30 4c 53 45 0a 2a 2a 20 69 66 20 66 73 79 6e 63 28  LSE.** if fsync(
11f31 29 73 20 61 72 65 20 65 78 65 63 75 74 65 64 20  )s are executed 
11f32 6e 6f 72 6d 61 6c 6c 79 2e 0a 2a 2f 0a 53 51 4c  normally..*/.SQL
11f33 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
11f34 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79  sqlite3PagerNosy
11f35 6e 63 28 50 61 67 65 72 20 2a 70 50 61 67 65 72  nc(Pager *pPager
11f36 29 7b 0a 20 20 72 65 74 75 72 6e 20 70 50 61 67  ){.  return pPag
11f37 65 72 2d 3e 6e 6f 53 79 6e 63 3b 0a 7d 0a 0a 23  er->noSync;.}..#
11f38 69 66 64 65 66 20 53 51 4c 49 54 45 5f 48 41 53  ifdef SQLITE_HAS
11f39 5f 43 4f 44 45 43 0a 2f 2a 0a 2a 2a 20 53 65 74  _CODEC./*.** Set
11f3a 20 74 68 65 20 63 6f 64 65 63 20 66 6f 72 20 74   the codec for t
11f3b 68 69 73 20 70 61 67 65 72 0a 2a 2f 0a 53 51 4c  his pager.*/.SQL
11f3c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
11f3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 65 74   sqlite3PagerSet
11f3e 43 6f 64 65 63 28 0a 20 20 50 61 67 65 72 20 2a  Codec(.  Pager *
11f3f 70 50 61 67 65 72 2c 0a 20 20 76 6f 69 64 20 2a  pPager,.  void *
11f40 28 2a 78 43 6f 64 65 63 29 28 76 6f 69 64 2a 2c  (*xCodec)(void*,
11f41 76 6f 69 64 2a 2c 50 67 6e 6f 2c 69 6e 74 29 2c  void*,Pgno,int),
11f42 0a 20 20 76 6f 69 64 20 2a 70 43 6f 64 65 63 41  .  void *pCodecA
11f43 72 67 0a 29 7b 0a 20 20 70 50 61 67 65 72 2d 3e  rg.){.  pPager->
11f44 78 43 6f 64 65 63 20 3d 20 78 43 6f 64 65 63 3b  xCodec = xCodec;
11f45 0a 20 20 70 50 61 67 65 72 2d 3e 70 43 6f 64 65  .  pPager->pCode
11f46 63 41 72 67 20 3d 20 70 43 6f 64 65 63 41 72 67  cArg = pCodecArg
11f47 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  ;.}.#endif..#ifn
11f48 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
11f49 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a  AUTOVACUUM./*.**
11f4a 20 4d 6f 76 65 20 74 68 65 20 70 61 67 65 20 70   Move the page p
11f4b 50 67 20 74 6f 20 6c 6f 63 61 74 69 6f 6e 20 70  Pg to location p
11f4c 67 6e 6f 20 69 6e 20 74 68 65 20 66 69 6c 65 2e  gno in the file.
11f4d 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 6d 75 73  .**.** There mus
11f4e 74 20 62 65 20 6e 6f 20 72 65 66 65 72 65 6e 63  t be no referenc
11f4f 65 73 20 74 6f 20 74 68 65 20 70 61 67 65 20 70  es to the page p
11f50 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74 65  reviously locate
11f51 64 20 61 74 0a 2a 2a 20 70 67 6e 6f 20 28 77 68  d at.** pgno (wh
11f52 69 63 68 20 77 65 20 63 61 6c 6c 20 70 50 67 4f  ich we call pPgO
11f53 6c 64 29 20 74 68 6f 75 67 68 20 74 68 61 74 20  ld) though that 
11f54 70 61 67 65 20 69 73 20 61 6c 6c 6f 77 65 64 20  page is allowed 
11f55 74 6f 20 62 65 0a 2a 2a 20 69 6e 20 63 61 63 68  to be.** in cach
11f56 65 2e 20 20 49 66 20 74 68 65 20 70 61 67 65 20  e.  If the page 
11f57 70 72 65 76 69 6f 75 73 6c 79 20 6c 6f 63 61 74  previously locat
11f58 65 64 20 61 74 20 70 67 6e 6f 20 69 73 20 6e 6f  ed at pgno is no
11f59 74 20 61 6c 72 65 61 64 79 0a 2a 2a 20 69 6e 20  t already.** in 
11f5a 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
11f5b 72 6e 61 6c 2c 20 69 74 20 69 73 20 6e 6f 74 20  rnal, it is not 
11f5c 70 75 74 20 74 68 65 72 65 20 62 79 20 62 79 20  put there by by 
11f5d 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a  this routine..**
11f5e 0a 2a 2a 20 52 65 66 65 72 65 6e 63 65 73 20 74  .** References t
11f5f 6f 20 74 68 65 20 70 61 67 65 20 70 50 67 20 72  o the page pPg r
11f60 65 6d 61 69 6e 20 76 61 6c 69 64 2e 20 55 70 64  emain valid. Upd
11f61 61 74 69 6e 67 20 61 6e 79 0a 2a 2a 20 6d 65 74  ating any.** met
11f62 61 2d 64 61 74 61 20 61 73 73 6f 63 69 61 74 65  a-data associate
11f63 64 20 77 69 74 68 20 70 50 67 20 28 69 2e 65 2e  d with pPg (i.e.
11f64 20 64 61 74 61 20 73 74 6f 72 65 64 20 69 6e 20   data stored in 
11f65 74 68 65 20 6e 45 78 74 72 61 20 62 79 74 65 73  the nExtra bytes
11f66 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6c  .** allocated al
11f67 6f 6e 67 20 77 69 74 68 20 74 68 65 20 70 61 67  ong with the pag
11f68 65 29 20 69 73 20 74 68 65 20 72 65 73 70 6f 6e  e) is the respon
11f69 73 69 62 69 6c 69 74 79 20 6f 66 20 74 68 65 20  sibility of the 
11f6a 63 61 6c 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 41 20  caller..**.** A 
11f6b 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74  transaction must
11f6c 20 62 65 20 61 63 74 69 76 65 20 77 68 65 6e 20   be active when 
11f6d 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
11f6e 63 61 6c 6c 65 64 2e 20 49 74 20 75 73 65 64 20  called. It used 
11f6f 74 6f 20 62 65 0a 2a 2a 20 72 65 71 75 69 72 65  to be.** require
11f70 64 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65  d that a stateme
11f71 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77  nt transaction w
11f72 61 73 20 6e 6f 74 20 61 63 74 69 76 65 2c 20 62  as not active, b
11f73 75 74 20 74 68 69 73 20 72 65 73 74 72 69 63 74  ut this restrict
11f74 69 6f 6e 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20  ion.** has been 
11f75 72 65 6d 6f 76 65 64 20 28 43 52 45 41 54 45 20  removed (CREATE 
11f76 49 4e 44 45 58 20 6e 65 65 64 73 20 74 6f 20 6d  INDEX needs to m
11f77 6f 76 65 20 61 20 70 61 67 65 20 77 68 65 6e 20  ove a page when 
11f78 61 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 74  a statement.** t
11f79 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
11f7a 74 69 76 65 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  tive)..**.** If 
11f7b 74 68 65 20 66 6f 75 72 74 68 20 61 72 67 75 6d  the fourth argum
11f7c 65 6e 74 2c 20 69 73 43 6f 6d 6d 69 74 2c 20 69  ent, isCommit, i
11f7d 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e  s non-zero, then
11f7e 20 74 68 69 73 20 70 61 67 65 20 69 73 20 62 65   this page is be
11f7f 69 6e 67 0a 2a 2a 20 6d 6f 76 65 64 20 61 73 20  ing.** moved as 
11f80 70 61 72 74 20 6f 66 20 61 20 64 61 74 61 62 61  part of a databa
11f81 73 65 20 72 65 6f 72 67 61 6e 69 7a 61 74 69 6f  se reorganizatio
11f82 6e 20 6a 75 73 74 20 62 65 66 6f 72 65 20 74 68  n just before th
11f83 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 0a 2a  e transaction .*
11f84 2a 20 69 73 20 62 65 69 6e 67 20 63 6f 6d 6d 69  * is being commi
11f85 74 74 65 64 2e 20 49 6e 20 74 68 69 73 20 63 61  tted. In this ca
11f86 73 65 2c 20 69 74 20 69 73 20 67 75 61 72 61 6e  se, it is guaran
11f87 74 65 65 64 20 74 68 61 74 20 74 68 65 20 64 61  teed that the da
11f88 74 61 62 61 73 65 20 70 61 67 65 20 0a 2a 2a 20  tabase page .** 
11f89 70 50 67 20 72 65 66 65 72 73 20 74 6f 20 77 69  pPg refers to wi
11f8a 6c 6c 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65  ll not be writte
11f8b 6e 20 74 6f 20 61 67 61 69 6e 20 77 69 74 68 69  n to again withi
11f8c 6e 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69  n this transacti
11f8d 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
11f8e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
11f8f 33 50 61 67 65 72 4d 6f 76 65 70 61 67 65 28 50  3PagerMovepage(P
11f90 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 44 62  ager *pPager, Db
11f91 50 61 67 65 20 2a 70 50 67 2c 20 50 67 6e 6f 20  Page *pPg, Pgno 
11f92 70 67 6e 6f 2c 20 69 6e 74 20 69 73 43 6f 6d 6d  pgno, int isComm
11f93 69 74 29 7b 0a 20 20 50 67 48 64 72 20 2a 70 50  it){.  PgHdr *pP
11f94 67 4f 6c 64 3b 20 20 2f 2a 20 54 68 65 20 70 61  gOld;  /* The pa
11f95 67 65 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69  ge being overwri
11f96 74 74 65 6e 2e 20 2a 2f 0a 20 20 50 67 6e 6f 20  tten. */.  Pgno 
11f97 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20 3d 20 30  needSyncPgno = 0
11f98 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 67  ;..  assert( pPg
11f99 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 0a 20 20 50  ->nRef>0 );..  P
11f9a 41 47 45 52 54 52 41 43 45 35 28 22 4d 4f 56 45  AGERTRACE5("MOVE
11f9b 20 25 64 20 70 61 67 65 20 25 64 20 28 6e 65 65   %d page %d (nee
11f9c 64 53 79 6e 63 3d 25 64 29 20 6d 6f 76 65 73 20  dSync=%d) moves 
11f9d 74 6f 20 25 64 5c 6e 22 2c 20 0a 20 20 20 20 20  to %d\n", .     
11f9e 20 50 41 47 45 52 49 44 28 70 50 61 67 65 72 29   PAGERID(pPager)
11f9f 2c 20 70 50 67 2d 3e 70 67 6e 6f 2c 20 28 70 50  , pPg->pgno, (pP
11fa0 67 2d 3e 66 6c 61 67 73 26 50 47 48 44 52 5f 4e  g->flags&PGHDR_N
11fa1 45 45 44 5f 53 59 4e 43 29 3f 31 3a 30 2c 20 70  EED_SYNC)?1:0, p
11fa2 67 6e 6f 29 3b 0a 20 20 49 4f 54 52 41 43 45 28  gno);.  IOTRACE(
11fa3 28 22 4d 4f 56 45 20 25 70 20 25 64 20 25 64 5c  ("MOVE %p %d %d\
11fa4 6e 22 2c 20 70 50 61 67 65 72 2c 20 70 50 67 2d  n", pPager, pPg-
11fa5 3e 70 67 6e 6f 2c 20 70 67 6e 6f 29 29 0a 0a 20  >pgno, pgno)).. 
11fa6 20 70 61 67 65 72 5f 67 65 74 5f 63 6f 6e 74 65   pager_get_conte
11fa7 6e 74 28 70 50 67 29 3b 0a 0a 20 20 2f 2a 20 49  nt(pPg);..  /* I
11fa8 66 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65  f the journal ne
11fa9 65 64 73 20 74 6f 20 62 65 20 73 79 6e 63 28 29  eds to be sync()
11faa 65 64 20 62 65 66 6f 72 65 20 70 61 67 65 20 70  ed before page p
11fab 50 67 2d 3e 70 67 6e 6f 20 63 61 6e 0a 20 20 2a  Pg->pgno can.  *
11fac 2a 20 62 65 20 77 72 69 74 74 65 6e 20 74 6f 2c  * be written to,
11fad 20 73 74 6f 72 65 20 70 50 67 2d 3e 70 67 6e 6f   store pPg->pgno
11fae 20 69 6e 20 6c 6f 63 61 6c 20 76 61 72 69 61 62   in local variab
11faf 6c 65 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 2e  le needSyncPgno.
11fb0 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
11fb1 65 20 69 73 43 6f 6d 6d 69 74 20 66 6c 61 67 20  e isCommit flag 
11fb2 69 73 20 73 65 74 2c 20 74 68 65 72 65 20 69 73  is set, there is
11fb3 20 6e 6f 20 6e 65 65 64 20 74 6f 20 72 65 6d 65   no need to reme
11fb4 6d 62 65 72 20 74 68 61 74 0a 20 20 2a 2a 20 74  mber that.  ** t
11fb5 68 65 20 6a 6f 75 72 6e 61 6c 20 6e 65 65 64 73  he journal needs
11fb6 20 74 6f 20 62 65 20 73 79 6e 63 28 29 65 64 20   to be sync()ed 
11fb7 62 65 66 6f 72 65 20 64 61 74 61 62 61 73 65 20  before database 
11fb8 70 61 67 65 20 70 50 67 2d 3e 70 67 6e 6f 20 0a  page pPg->pgno .
11fb9 20 20 2a 2a 20 63 61 6e 20 62 65 20 77 72 69 74    ** can be writ
11fba 74 65 6e 20 74 6f 2e 20 54 68 65 20 63 61 6c 6c  ten to. The call
11fbb 65 72 20 68 61 73 20 61 6c 72 65 61 64 79 20 70  er has already p
11fbc 72 6f 6d 69 73 65 64 20 6e 6f 74 20 74 6f 20 77  romised not to w
11fbd 72 69 74 65 20 74 6f 20 69 74 2e 0a 20 20 2a 2f  rite to it..  */
11fbe 0a 20 20 69 66 28 20 28 70 50 67 2d 3e 66 6c 61  .  if( (pPg->fla
11fbf 67 73 26 50 47 48 44 52 5f 4e 45 45 44 5f 53 59  gs&PGHDR_NEED_SY
11fc0 4e 43 29 20 26 26 20 21 69 73 43 6f 6d 6d 69 74  NC) && !isCommit
11fc1 20 29 7b 0a 20 20 20 20 6e 65 65 64 53 79 6e 63   ){.    needSync
11fc2 50 67 6e 6f 20 3d 20 70 50 67 2d 3e 70 67 6e 6f  Pgno = pPg->pgno
11fc3 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 61  ;.    assert( pa
11fc4 67 65 49 6e 4a 6f 75 72 6e 61 6c 28 70 50 67 29  geInJournal(pPg)
11fc5 20 7c 7c 20 70 67 6e 6f 3e 70 50 61 67 65 72 2d   || pgno>pPager-
11fc6 3e 6f 72 69 67 44 62 53 69 7a 65 20 29 3b 0a 20  >origDbSize );. 
11fc7 20 20 20 61 73 73 65 72 74 28 20 70 50 67 2d 3e     assert( pPg->
11fc8 66 6c 61 67 73 26 50 47 48 44 52 5f 44 49 52 54  flags&PGHDR_DIRT
11fc9 59 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Y );.    assert(
11fca 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e   pPager->needSyn
11fcb 63 20 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  c );.  }..  /* I
11fcc 66 20 74 68 65 20 63 61 63 68 65 20 63 6f 6e 74  f the cache cont
11fcd 61 69 6e 73 20 61 20 70 61 67 65 20 77 69 74 68  ains a page with
11fce 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 70 67 6e   page-number pgn
11fcf 6f 2c 20 72 65 6d 6f 76 65 20 69 74 0a 20 20 2a  o, remove it.  *
11fd0 2a 20 66 72 6f 6d 20 69 74 73 20 68 61 73 68 20  * from its hash 
11fd1 63 68 61 69 6e 2e 20 41 6c 73 6f 2c 20 69 66 20  chain. Also, if 
11fd2 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79  the PgHdr.needSy
11fd3 6e 63 20 77 61 73 20 73 65 74 20 66 6f 72 20 0a  nc was set for .
11fd4 20 20 2a 2a 20 70 61 67 65 20 70 67 6e 6f 20 62    ** page pgno b
11fd5 65 66 6f 72 65 20 74 68 65 20 27 6d 6f 76 65 27  efore the 'move'
11fd6 20 6f 70 65 72 61 74 69 6f 6e 2c 20 69 74 20 6e   operation, it n
11fd7 65 65 64 73 20 74 6f 20 62 65 20 72 65 74 61 69  eeds to be retai
11fd8 6e 65 64 20 0a 20 20 2a 2a 20 66 6f 72 20 74 68  ned .  ** for th
11fd9 65 20 70 61 67 65 20 6d 6f 76 65 64 20 74 68 65  e page moved the
11fda 72 65 2e 0a 20 20 2a 2f 0a 20 20 70 50 67 2d 3e  re..  */.  pPg->
11fdb 66 6c 61 67 73 20 26 3d 20 7e 50 47 48 44 52 5f  flags &= ~PGHDR_
11fdc 4e 45 45 44 5f 53 59 4e 43 3b 0a 20 20 70 50 67  NEED_SYNC;.  pPg
11fdd 4f 6c 64 20 3d 20 70 61 67 65 72 5f 6c 6f 6f 6b  Old = pager_look
11fde 75 70 28 70 50 61 67 65 72 2c 20 70 67 6e 6f 29  up(pPager, pgno)
11fdf 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 67  ;.  assert( !pPg
11fe0 4f 6c 64 20 7c 7c 20 70 50 67 4f 6c 64 2d 3e 6e  Old || pPgOld->n
11fe1 52 65 66 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20  Ref==1 );.  if( 
11fe2 70 50 67 4f 6c 64 20 29 7b 0a 20 20 20 20 70 50  pPgOld ){.    pP
11fe3 67 2d 3e 66 6c 61 67 73 20 7c 3d 20 28 70 50 67  g->flags |= (pPg
11fe4 4f 6c 64 2d 3e 66 6c 61 67 73 26 50 47 48 44 52  Old->flags&PGHDR
11fe5 5f 4e 45 45 44 5f 53 59 4e 43 29 3b 0a 20 20 7d  _NEED_SYNC);.  }
11fe6 0a 0a 20 20 73 71 6c 69 74 65 33 50 63 61 63 68  ..  sqlite3Pcach
11fe7 65 4d 6f 76 65 28 70 50 67 2c 20 70 67 6e 6f 29  eMove(pPg, pgno)
11fe8 3b 0a 20 20 69 66 28 20 70 50 67 4f 6c 64 20 29  ;.  if( pPgOld )
11fe9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 63 61  {.    sqlite3Pca
11fea 63 68 65 44 72 6f 70 28 70 50 67 4f 6c 64 29 3b  cheDrop(pPgOld);
11feb 0a 20 20 7d 0a 0a 20 20 6d 61 6b 65 44 69 72 74  .  }..  makeDirt
11fec 79 28 70 50 67 29 3b 0a 20 20 70 50 61 67 65 72  y(pPg);.  pPager
11fed 2d 3e 64 69 72 74 79 43 61 63 68 65 20 3d 20 31  ->dirtyCache = 1
11fee 3b 0a 20 20 70 50 61 67 65 72 2d 3e 64 62 4d 6f  ;.  pPager->dbMo
11fef 64 69 66 69 65 64 20 3d 20 31 3b 0a 0a 20 20 69  dified = 1;..  i
11ff0 66 28 20 6e 65 65 64 53 79 6e 63 50 67 6e 6f 20  f( needSyncPgno 
11ff1 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 6e 65 65  ){.    /* If nee
11ff2 64 53 79 6e 63 50 67 6e 6f 20 69 73 20 6e 6f 6e  dSyncPgno is non
11ff3 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65 20  -zero, then the 
11ff4 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 6e 65 65  journal file nee
11ff5 64 73 20 74 6f 20 62 65 20 0a 20 20 20 20 2a 2a  ds to be .    **
11ff6 20 73 79 6e 63 28 29 65 64 20 62 65 66 6f 72 65   sync()ed before
11ff7 20 61 6e 79 20 64 61 74 61 20 69 73 20 77 72 69   any data is wri
11ff8 74 74 65 6e 20 74 6f 20 64 61 74 61 62 61 73 65  tten to database
11ff9 20 66 69 6c 65 20 70 61 67 65 20 6e 65 65 64 53   file page needS
11ffa 79 6e 63 50 67 6e 6f 2e 0a 20 20 20 20 2a 2a 20  yncPgno..    ** 
11ffb 43 75 72 72 65 6e 74 6c 79 2c 20 6e 6f 20 73 75  Currently, no su
11ffc 63 68 20 70 61 67 65 20 65 78 69 73 74 73 20 69  ch page exists i
11ffd 6e 20 74 68 65 20 70 61 67 65 2d 63 61 63 68 65  n the page-cache
11ffe 20 61 6e 64 20 74 68 65 20 0a 20 20 20 20 2a 2a   and the .    **
11fff 20 22 69 73 20 6a 6f 75 72 6e 61 6c 65 64 22 20   "is journaled" 
12000 62 69 74 76 65 63 20 66 6c 61 67 20 68 61 73 20  bitvec flag has 
12001 62 65 65 6e 20 73 65 74 2e 20 54 68 69 73 20 6e  been set. This n
12002 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 65 64  eeds to be remed
12003 69 65 64 20 62 79 0a 20 20 20 20 2a 2a 20 6c 6f  ied by.    ** lo
12004 61 64 69 6e 67 20 74 68 65 20 70 61 67 65 20 69  ading the page i
12005 6e 74 6f 20 74 68 65 20 70 61 67 65 72 2d 63 61  nto the pager-ca
12006 63 68 65 20 61 6e 64 20 73 65 74 74 69 6e 67 20  che and setting 
12007 74 68 65 20 50 67 48 64 72 2e 6e 65 65 64 53 79  the PgHdr.needSy
12008 6e 63 20 0a 20 20 20 20 2a 2a 20 66 6c 61 67 2e  nc .    ** flag.
12009 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 49  .    **.    ** I
1200a 66 20 74 68 65 20 61 74 74 65 6d 70 74 20 74 6f  f the attempt to
1200b 20 6c 6f 61 64 20 74 68 65 20 70 61 67 65 20 69   load the page i
1200c 6e 74 6f 20 74 68 65 20 70 61 67 65 2d 63 61 63  nto the page-cac
1200d 68 65 20 66 61 69 6c 73 2c 20 28 64 75 65 0a 20  he fails, (due. 
1200e 20 20 20 2a 2a 20 74 6f 20 61 20 6d 61 6c 6c 6f     ** to a mallo
1200f 63 28 29 20 6f 72 20 49 4f 20 66 61 69 6c 75 72  c() or IO failur
12010 65 29 2c 20 63 6c 65 61 72 20 74 68 65 20 62 69  e), clear the bi
12011 74 20 69 6e 20 74 68 65 20 70 49 6e 4a 6f 75 72  t in the pInJour
12012 6e 61 6c 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72  nal[].    ** arr
12013 61 79 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69  ay. Otherwise, i
12014 66 20 74 68 65 20 70 61 67 65 20 69 73 20 6c 6f  f the page is lo
12015 61 64 65 64 20 61 6e 64 20 77 72 69 74 74 65 6e  aded and written
12016 20 61 67 61 69 6e 20 69 6e 0a 20 20 20 20 2a 2a   again in.    **
12017 20 74 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f   this transactio
12018 6e 2c 20 69 74 20 6d 61 79 20 62 65 20 77 72 69  n, it may be wri
12019 74 74 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61  tten to the data
1201a 62 61 73 65 20 66 69 6c 65 20 62 65 66 6f 72 65  base file before
1201b 0a 20 20 20 20 2a 2a 20 69 74 20 69 73 20 73 79  .    ** it is sy
1201c 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 6a 6f  nced into the jo
1201d 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73  urnal file. This
1201e 20 77 61 79 2c 20 69 74 20 6d 61 79 20 65 6e 64   way, it may end
1201f 20 75 70 20 69 6e 0a 20 20 20 20 2a 2a 20 74 68   up in.    ** th
12020 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 74  e journal file t
12021 77 69 63 65 2c 20 62 75 74 20 74 68 61 74 20 69  wice, but that i
12022 73 20 6e 6f 74 20 61 20 70 72 6f 62 6c 65 6d 2e  s not a problem.
12023 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
12024 68 65 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  he sqlite3PagerG
12025 65 74 28 29 20 63 61 6c 6c 20 6d 61 79 20 63 61  et() call may ca
12026 75 73 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  use the journal 
12027 74 6f 20 73 79 6e 63 2e 20 53 6f 20 6d 61 6b 65  to sync. So make
12028 0a 20 20 20 20 2a 2a 20 73 75 72 65 20 74 68 65  .    ** sure the
12029 20 50 61 67 65 72 2e 6e 65 65 64 53 79 6e 63 20   Pager.needSync 
1202a 66 6c 61 67 20 69 73 20 73 65 74 20 74 6f 6f 2e  flag is set too.
1202b 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20  .    */.    int 
1202c 72 63 3b 0a 20 20 20 20 50 67 48 64 72 20 2a 70  rc;.    PgHdr *p
1202d 50 67 48 64 72 3b 0a 20 20 20 20 61 73 73 65 72  PgHdr;.    asser
1202e 74 28 20 70 50 61 67 65 72 2d 3e 6e 65 65 64 53  t( pPager->needS
1202f 79 6e 63 20 29 3b 0a 20 20 20 20 72 63 20 3d 20  ync );.    rc = 
12030 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
12031 70 50 61 67 65 72 2c 20 6e 65 65 64 53 79 6e 63  pPager, needSync
12032 50 67 6e 6f 2c 20 26 70 50 67 48 64 72 29 3b 0a  Pgno, &pPgHdr);.
12033 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
12034 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69  TE_OK ){.      i
12035 66 28 20 70 50 61 67 65 72 2d 3e 70 49 6e 4a 6f  f( pPager->pInJo
12036 75 72 6e 61 6c 20 26 26 20 6e 65 65 64 53 79 6e  urnal && needSyn
12037 63 50 67 6e 6f 3c 3d 70 50 61 67 65 72 2d 3e 6f  cPgno<=pPager->o
12038 72 69 67 44 62 53 69 7a 65 20 29 7b 0a 20 20 20  rigDbSize ){.   
12039 20 20 20 20 20 73 71 6c 69 74 65 33 42 69 74 76       sqlite3Bitv
1203a 65 63 43 6c 65 61 72 28 70 50 61 67 65 72 2d 3e  ecClear(pPager->
1203b 70 49 6e 4a 6f 75 72 6e 61 6c 2c 20 6e 65 65 64  pInJournal, need
1203c 53 79 6e 63 50 67 6e 6f 29 3b 0a 20 20 20 20 20  SyncPgno);.     
1203d 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20   }.      return 
1203e 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50  rc;.    }.    pP
1203f 61 67 65 72 2d 3e 6e 65 65 64 53 79 6e 63 20 3d  ager->needSync =
12040 20 31 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   1;.    assert( 
12041 70 50 61 67 65 72 2d 3e 6e 6f 53 79 6e 63 3d 3d  pPager->noSync==
12042 30 20 26 26 20 21 4d 45 4d 44 42 20 29 3b 0a 20  0 && !MEMDB );. 
12043 20 20 20 70 50 67 48 64 72 2d 3e 66 6c 61 67 73     pPgHdr->flags
12044 20 7c 3d 20 50 47 48 44 52 5f 4e 45 45 44 5f 53   |= PGHDR_NEED_S
12045 59 4e 43 3b 0a 20 20 20 20 6d 61 6b 65 44 69 72  YNC;.    makeDir
12046 74 79 28 70 50 67 48 64 72 29 3b 0a 20 20 20 20  ty(pPgHdr);.    
12047 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
12048 66 28 70 50 67 48 64 72 29 3b 0a 20 20 7d 0a 0a  f(pPgHdr);.  }..
12049 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1204a 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  OK;.}.#endif../*
1204b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69  .** Return a poi
1204c 6e 74 65 72 20 74 6f 20 74 68 65 20 64 61 74 61  nter to the data
1204d 20 66 6f 72 20 74 68 65 20 73 70 65 63 69 66 69   for the specifi
1204e 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  ed page..*/.SQLI
1204f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12050 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
12051 44 61 74 61 28 44 62 50 61 67 65 20 2a 70 50 67  Data(DbPage *pPg
12052 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 50 67  ){.  assert( pPg
12053 2d 3e 6e 52 65 66 3e 30 20 7c 7c 20 70 50 67 2d  ->nRef>0 || pPg-
12054 3e 70 50 61 67 65 72 2d 3e 6d 65 6d 44 62 20 29  >pPager->memDb )
12055 3b 0a 20 20 72 65 74 75 72 6e 20 70 50 67 2d 3e  ;.  return pPg->
12056 70 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pData;.}../*.** 
12057 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
12058 20 74 6f 20 74 68 65 20 50 61 67 65 72 2e 6e 45   to the Pager.nE
12059 78 74 72 61 20 62 79 74 65 73 20 6f 66 20 22 65  xtra bytes of "e
1205a 78 74 72 61 22 20 73 70 61 63 65 20 0a 2a 2a 20  xtra" space .** 
1205b 61 6c 6c 6f 63 61 74 65 64 20 61 6c 6f 6e 67 20  allocated along 
1205c 77 69 74 68 20 74 68 65 20 73 70 65 63 69 66 69  with the specifi
1205d 65 64 20 70 61 67 65 2e 0a 2a 2f 0a 53 51 4c 49  ed page..*/.SQLI
1205e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1205f 2a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74  *sqlite3PagerGet
12060 45 78 74 72 61 28 44 62 50 61 67 65 20 2a 70 50  Extra(DbPage *pP
12061 67 29 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  g){.  Pager *pPa
12062 67 65 72 20 3d 20 70 50 67 2d 3e 70 50 61 67 65  ger = pPg->pPage
12063 72 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 50 61  r;.  return (pPa
12064 67 65 72 3f 70 50 67 2d 3e 70 45 78 74 72 61 3a  ger?pPg->pExtra:
12065 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 74  0);.}../*.** Get
12066 2f 73 65 74 20 74 68 65 20 6c 6f 63 6b 69 6e 67  /set the locking
12067 2d 6d 6f 64 65 20 66 6f 72 20 74 68 69 73 20 70  -mode for this p
12068 61 67 65 72 2e 20 50 61 72 61 6d 65 74 65 72 20  ager. Parameter 
12069 65 4d 6f 64 65 20 6d 75 73 74 20 62 65 20 6f 6e  eMode must be on
1206a 65 0a 2a 2a 20 6f 66 20 50 41 47 45 52 5f 4c 4f  e.** of PAGER_LO
1206b 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52 59 2c  CKINGMODE_QUERY,
1206c 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
1206d 44 45 5f 4e 4f 52 4d 41 4c 20 6f 72 20 0a 2a 2a  DE_NORMAL or .**
1206e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f   PAGER_LOCKINGMO
1206f 44 45 5f 45 58 43 4c 55 53 49 56 45 2e 20 49 66  DE_EXCLUSIVE. If
12070 20 74 68 65 20 70 61 72 61 6d 65 74 65 72 20 69   the parameter i
12071 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20 74 68  s not _QUERY, th
12072 65 6e 0a 2a 2a 20 74 68 65 20 6c 6f 63 6b 69 6e  en.** the lockin
12073 67 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f  g-mode is set to
12074 20 74 68 65 20 76 61 6c 75 65 20 73 70 65 63 69   the value speci
12075 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  fied..**.** The 
12076 72 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69  returned value i
12077 73 20 65 69 74 68 65 72 20 50 41 47 45 52 5f 4c  s either PAGER_L
12078 4f 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41  OCKINGMODE_NORMA
12079 4c 20 6f 72 0a 2a 2a 20 50 41 47 45 52 5f 4c 4f  L or.** PAGER_LO
1207a 43 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53  CKINGMODE_EXCLUS
1207b 49 56 45 2c 20 69 6e 64 69 63 61 74 69 6e 67 20  IVE, indicating 
1207c 74 68 65 20 63 75 72 72 65 6e 74 20 28 70 6f 73  the current (pos
1207d 73 69 62 6c 79 20 75 70 64 61 74 65 64 29 0a 2a  sibly updated).*
1207e 2a 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 2e 0a  * locking-mode..
1207f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12080 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 67  E int sqlite3Pag
12081 65 72 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 50 61  erLockingMode(Pa
12082 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 6e 74  ger *pPager, int
12083 20 65 4d 6f 64 65 29 7b 0a 20 20 61 73 73 65 72   eMode){.  asser
12084 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  t( eMode==PAGER_
12085 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
12086 59 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  Y.            ||
12087 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f   eMode==PAGER_LO
12088 43 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c  CKINGMODE_NORMAL
12089 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20  .            || 
1208a 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43  eMode==PAGER_LOC
1208b 4b 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49  KINGMODE_EXCLUSI
1208c 56 45 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  VE );.  assert( 
1208d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d 4f 44  PAGER_LOCKINGMOD
1208e 45 5f 51 55 45 52 59 3c 30 20 29 3b 0a 20 20 61  E_QUERY<0 );.  a
1208f 73 73 65 72 74 28 20 50 41 47 45 52 5f 4c 4f 43  ssert( PAGER_LOC
12090 4b 49 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3e  KINGMODE_NORMAL>
12091 3d 30 20 26 26 20 50 41 47 45 52 5f 4c 4f 43 4b  =0 && PAGER_LOCK
12092 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
12093 45 3e 3d 30 20 29 3b 0a 20 20 69 66 28 20 65 4d  E>=0 );.  if( eM
12094 6f 64 65 3e 3d 30 20 26 26 20 21 70 50 61 67 65  ode>=0 && !pPage
12095 72 2d 3e 74 65 6d 70 46 69 6c 65 20 29 7b 0a 20  r->tempFile ){. 
12096 20 20 20 70 50 61 67 65 72 2d 3e 65 78 63 6c 75     pPager->exclu
12097 73 69 76 65 4d 6f 64 65 20 3d 20 65 4d 6f 64 65  siveMode = eMode
12098 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  ;.  }.  return (
12099 69 6e 74 29 70 50 61 67 65 72 2d 3e 65 78 63 6c  int)pPager->excl
1209a 75 73 69 76 65 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a  usiveMode;.}../*
1209b 0a 2a 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20  .** Get/set the 
1209c 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 66 6f 72  journal-mode for
1209d 20 74 68 69 73 20 70 61 67 65 72 2e 20 50 61 72   this pager. Par
1209e 61 6d 65 74 65 72 20 65 4d 6f 64 65 20 6d 75 73  ameter eMode mus
1209f 74 20 62 65 20 6f 6e 65 20 6f 66 3a 0a 2a 2a 0a  t be one of:.**.
120a0 2a 2a 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52  **    PAGER_JOUR
120a1 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 0a 2a 2a  NALMODE_QUERY.**
120a2 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
120a3 4c 4d 4f 44 45 5f 44 45 4c 45 54 45 0a 2a 2a 20  LMODE_DELETE.** 
120a4 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c     PAGER_JOURNAL
120a5 4d 4f 44 45 5f 54 52 55 4e 43 41 54 45 0a 2a 2a  MODE_TRUNCATE.**
120a6 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
120a7 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 2a 2a  LMODE_PERSIST.**
120a8 20 20 20 20 50 41 47 45 52 5f 4a 4f 55 52 4e 41      PAGER_JOURNA
120a9 4c 4d 4f 44 45 5f 4f 46 46 0a 2a 2a 0a 2a 2a 20  LMODE_OFF.**.** 
120aa 49 66 20 74 68 65 20 70 61 72 61 6d 65 74 65 72  If the parameter
120ab 20 69 73 20 6e 6f 74 20 5f 51 55 45 52 59 2c 20   is not _QUERY, 
120ac 74 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  then the journal
120ad 2d 6d 6f 64 65 20 69 73 20 73 65 74 20 74 6f 20  -mode is set to 
120ae 74 68 65 0a 2a 2a 20 76 61 6c 75 65 20 73 70 65  the.** value spe
120af 63 69 66 69 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  cified..**.** Th
120b0 65 20 72 65 74 75 72 6e 65 64 20 69 6e 64 69 63  e returned indic
120b1 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
120b2 28 70 6f 73 73 69 62 6c 79 20 75 70 64 61 74 65  (possibly update
120b3 64 29 0a 2a 2a 20 6a 6f 75 72 6e 61 6c 2d 6d 6f  d).** journal-mo
120b4 64 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  de..*/.SQLITE_PR
120b5 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
120b6 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64  3PagerJournalMod
120b7 65 28 50 61 67 65 72 20 2a 70 50 61 67 65 72 2c  e(Pager *pPager,
120b8 20 69 6e 74 20 65 4d 6f 64 65 29 7b 0a 20 20 69   int eMode){.  i
120b9 66 28 20 21 4d 45 4d 44 42 20 29 7b 0a 20 20 20  f( !MEMDB ){.   
120ba 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d 3d   assert( eMode==
120bb 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
120bc 45 5f 51 55 45 52 59 0a 20 20 20 20 20 20 20 20  E_QUERY.        
120bd 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
120be 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
120bf 45 5f 44 45 4c 45 54 45 0a 20 20 20 20 20 20 20  E_DELETE.       
120c0 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d         || eMode=
120c1 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
120c2 44 45 5f 54 52 55 4e 43 41 54 45 0a 20 20 20 20  DE_TRUNCATE.    
120c3 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d 6f            || eMo
120c4 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41  de==PAGER_JOURNA
120c5 4c 4d 4f 44 45 5f 50 45 52 53 49 53 54 0a 20 20  LMODE_PERSIST.  
120c6 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65              || e
120c7 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52  Mode==PAGER_JOUR
120c8 4e 41 4c 4d 4f 44 45 5f 4f 46 46 20 0a 20 20 20  NALMODE_OFF .   
120c9 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 65 4d             || eM
120ca 6f 64 65 3d 3d 50 41 47 45 52 5f 4a 4f 55 52 4e  ode==PAGER_JOURN
120cb 41 4c 4d 4f 44 45 5f 4d 45 4d 4f 52 59 20 29 3b  ALMODE_MEMORY );
120cc 0a 20 20 20 20 61 73 73 65 72 74 28 20 50 41 47  .    assert( PAG
120cd 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
120ce 55 45 52 59 3c 30 20 29 3b 0a 20 20 20 20 69 66  UERY<0 );.    if
120cf 28 20 65 4d 6f 64 65 3e 3d 30 20 29 7b 0a 20 20  ( eMode>=0 ){.  
120d0 20 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72      pPager->jour
120d1 6e 61 6c 4d 6f 64 65 20 3d 20 65 4d 6f 64 65 3b  nalMode = eMode;
120d2 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
120d3 20 20 61 73 73 65 72 74 28 20 65 4d 6f 64 65 3d    assert( eMode=
120d4 3d 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f  =PAGER_JOURNALMO
120d5 44 45 5f 51 55 45 52 59 20 29 3b 0a 20 20 20 20  DE_QUERY );.    
120d6 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28  }.  }.  return (
120d7 69 6e 74 29 70 50 61 67 65 72 2d 3e 6a 6f 75 72  int)pPager->jour
120d8 6e 61 6c 4d 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  nalMode;.}../*.*
120d9 2a 20 47 65 74 2f 73 65 74 20 74 68 65 20 73 69  * Get/set the si
120da 7a 65 2d 6c 69 6d 69 74 20 75 73 65 64 20 66 6f  ze-limit used fo
120db 72 20 70 65 72 73 69 73 74 65 6e 74 20 6a 6f 75  r persistent jou
120dc 72 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53  rnal files..*/.S
120dd 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36  QLITE_PRIVATE i6
120de 34 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f  4 sqlite3PagerJo
120df 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28 50  urnalSizeLimit(P
120e0 61 67 65 72 20 2a 70 50 61 67 65 72 2c 20 69 36  ager *pPager, i6
120e1 34 20 69 4c 69 6d 69 74 29 7b 0a 20 20 69 66 28  4 iLimit){.  if(
120e2 20 69 4c 69 6d 69 74 3e 3d 2d 31 20 29 7b 0a 20   iLimit>=-1 ){. 
120e3 20 20 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e     pPager->journ
120e4 61 6c 53 69 7a 65 4c 69 6d 69 74 20 3d 20 69 4c  alSizeLimit = iL
120e5 69 6d 69 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75  imit;.  }.  retu
120e6 72 6e 20 70 50 61 67 65 72 2d 3e 6a 6f 75 72 6e  rn pPager->journ
120e7 61 6c 53 69 7a 65 4c 69 6d 69 74 3b 0a 7d 0a 0a  alSizeLimit;.}..
120e8 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
120e9 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 20 2a 2f 0a  _OMIT_DISKIO */.
120ea 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
120eb 20 45 6e 64 20 6f 66 20 70 61 67 65 72 2e 63 20   End of pager.c 
120ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
120ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
120ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
120ef 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
120f0 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 6d 75   Begin file btmu
120f1 74 65 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  tex.c **********
120f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
120f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
120f4 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 41 75 67 75  ./*.** 2007 Augu
120f5 73 74 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20  st 27.**.** The 
120f6 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
120f7 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
120f8 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
120f9 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
120fa 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
120fb 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
120fc 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
120fd 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
120fe 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
120ff 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
12100 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
12101 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
12102 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
12103 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
12104 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
12105 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
12106 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
12107 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12108 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12109 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1210a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1210b 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ****.**.** $Id: 
1210c 62 74 6d 75 74 65 78 2e 63 2c 76 20 31 2e 31 32  btmutex.c,v 1.12
1210d 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 3a 31   2008/11/17 19:1
1210e 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:55 danielk1977
1210f 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69   Exp $.**.** Thi
12110 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
12111 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70  code used to imp
12112 6c 65 6d 65 6e 74 20 6d 75 74 65 78 65 73 20 6f  lement mutexes o
12113 6e 20 42 74 72 65 65 20 6f 62 6a 65 63 74 73 2e  n Btree objects.
12114 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 72 65  .** This code re
12115 61 6c 6c 79 20 62 65 6c 6f 6e 67 73 20 69 6e 20  ally belongs in 
12116 62 74 72 65 65 2e 63 2e 20 20 42 75 74 20 62 74  btree.c.  But bt
12117 72 65 65 2e 63 20 69 73 20 67 65 74 74 69 6e 67  ree.c is getting
12118 20 74 6f 6f 0a 2a 2a 20 62 69 67 20 61 6e 64 20   too.** big and 
12119 77 65 20 77 61 6e 74 20 74 6f 20 62 72 65 61 6b  we want to break
1211a 20 69 74 20 64 6f 77 6e 20 73 6f 6d 65 2e 20 20   it down some.  
1211b 54 68 69 73 20 70 61 63 6b 61 67 65 64 20 73 65  This packaged se
1211c 65 6d 65 64 20 6c 69 6b 65 0a 2a 2a 20 61 20 67  emed like.** a g
1211d 6f 6f 64 20 62 72 65 61 6b 6f 75 74 2e 0a 2a 2f  ood breakout..*/
1211e 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
1211f 20 49 6e 63 6c 75 64 65 20 62 74 72 65 65 49 6e   Include btreeIn
12120 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  t.h in the middl
12121 65 20 6f 66 20 62 74 6d 75 74 65 78 2e 63 20 2a  e of btmutex.c *
12122 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
12123 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
12124 20 42 65 67 69 6e 20 66 69 6c 65 20 62 74 72 65   Begin file btre
12125 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
12126 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12127 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
12128 0a 2f 2a 0a 2a 2a 20 32 30 30 34 20 41 70 72 69  ./*.** 2004 Apri
12129 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75  l 6.**.** The au
1212a 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
1212b 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
1212c 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
1212d 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
1212e 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
1212f 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
12130 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
12131 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
12132 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
12133 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
12134 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
12135 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
12136 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
12137 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
12138 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
12139 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
1213a 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
1213b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1213c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1213d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1213e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1213f 2a 2a 0a 2a 2a 20 24 49 64 3a 20 62 74 72 65 65  **.** $Id: btree
12140 49 6e 74 2e 68 2c 76 20 31 2e 33 36 20 32 30 30  Int.h,v 1.36 200
12141 38 2f 31 31 2f 31 39 20 31 30 3a 32 32 3a 33 33  8/11/19 10:22:33
12142 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
12143 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69   $.**.** This fi
12144 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20  le implements a 
12145 65 78 74 65 72 6e 61 6c 20 28 64 69 73 6b 2d 62  external (disk-b
12146 61 73 65 64 29 20 64 61 74 61 62 61 73 65 20 75  ased) database u
12147 73 69 6e 67 20 42 54 72 65 65 73 2e 0a 2a 2a 20  sing BTrees..** 
12148 46 6f 72 20 61 20 64 65 74 61 69 6c 65 64 20 64  For a detailed d
12149 69 73 63 75 73 73 69 6f 6e 20 6f 66 20 42 54 72  iscussion of BTr
1214a 65 65 73 2c 20 72 65 66 65 72 20 74 6f 0a 2a 2a  ees, refer to.**
1214b 0a 2a 2a 20 20 20 20 20 44 6f 6e 61 6c 64 20 45  .**     Donald E
1214c 2e 20 4b 6e 75 74 68 2c 20 54 48 45 20 41 52 54  . Knuth, THE ART
1214d 20 4f 46 20 43 4f 4d 50 55 54 45 52 20 50 52 4f   OF COMPUTER PRO
1214e 47 52 41 4d 4d 49 4e 47 2c 20 56 6f 6c 75 6d 65  GRAMMING, Volume
1214f 20 33 3a 0a 2a 2a 20 20 20 20 20 22 53 6f 72 74   3:.**     "Sort
12150 69 6e 67 20 41 6e 64 20 53 65 61 72 63 68 69 6e  ing And Searchin
12151 67 22 2c 20 70 61 67 65 73 20 34 37 33 2d 34 38  g", pages 473-48
12152 30 2e 20 41 64 64 69 73 6f 6e 2d 57 65 73 6c 65  0. Addison-Wesle
12153 79 0a 2a 2a 20 20 20 20 20 50 75 62 6c 69 73 68  y.**     Publish
12154 69 6e 67 20 43 6f 6d 70 61 6e 79 2c 20 52 65 61  ing Company, Rea
12155 64 69 6e 67 2c 20 4d 61 73 73 61 63 68 75 73 65  ding, Massachuse
12156 74 74 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 62  tts..**.** The b
12157 61 73 69 63 20 69 64 65 61 20 69 73 20 74 68 61  asic idea is tha
12158 74 20 65 61 63 68 20 70 61 67 65 20 6f 66 20 74  t each page of t
12159 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  he file contains
1215a 20 4e 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 65   N database.** e
1215b 6e 74 72 69 65 73 20 61 6e 64 20 4e 2b 31 20 70  ntries and N+1 p
1215c 6f 69 6e 74 65 72 73 20 74 6f 20 73 75 62 70 61  ointers to subpa
1215d 67 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 2d 2d 2d  ges..**.**   ---
1215e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1215f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12160 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12161 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a  -------------.**
12162 20 20 20 7c 20 20 50 74 72 28 30 29 20 7c 20 4b     |  Ptr(0) | K
12163 65 79 28 30 29 20 7c 20 50 74 72 28 31 29 20 7c  ey(0) | Ptr(1) |
12164 20 4b 65 79 28 31 29 20 7c 20 2e 2e 2e 20 7c 20   Key(1) | ... | 
12165 4b 65 79 28 4e 2d 31 29 20 7c 20 50 74 72 28 4e  Key(N-1) | Ptr(N
12166 29 20 7c 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d  ) |.**   -------
12167 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12168 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
12169 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1216a 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a 2a 2a 20  ---------.**.** 
1216b 41 6c 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 20  All of the keys 
1216c 6f 6e 20 74 68 65 20 70 61 67 65 20 74 68 61 74  on the page that
1216d 20 50 74 72 28 30 29 20 70 6f 69 6e 74 73 20 74   Ptr(0) points t
1216e 6f 20 68 61 76 65 20 76 61 6c 75 65 73 20 6c 65  o have values le
1216f 73 73 0a 2a 2a 20 74 68 61 6e 20 4b 65 79 28 30  ss.** than Key(0
12170 29 2e 20 20 41 6c 6c 20 6f 66 20 74 68 65 20 6b  ).  All of the k
12171 65 79 73 20 6f 6e 20 70 61 67 65 20 50 74 72 28  eys on page Ptr(
12172 31 29 20 61 6e 64 20 69 74 73 20 73 75 62 70 61  1) and its subpa
12173 67 65 73 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75  ges have.** valu
12174 65 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20  es greater than 
12175 4b 65 79 28 30 29 20 61 6e 64 20 6c 65 73 73 20  Key(0) and less 
12176 74 68 61 6e 20 4b 65 79 28 31 29 2e 20 20 41 6c  than Key(1).  Al
12177 6c 20 6f 66 20 74 68 65 20 6b 65 79 73 0a 2a 2a  l of the keys.**
12178 20 6f 6e 20 50 74 72 28 4e 29 20 61 6e 64 20 69   on Ptr(N) and i
12179 74 73 20 73 75 62 70 61 67 65 73 20 68 61 76 65  ts subpages have
1217a 20 76 61 6c 75 65 73 20 67 72 65 61 74 65 72 20   values greater 
1217b 74 68 61 6e 20 4b 65 79 28 4e 2d 31 29 2e 20 20  than Key(N-1).  
1217c 41 6e 64 0a 2a 2a 20 73 6f 20 66 6f 72 74 68 2e  And.** so forth.
1217d 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 69 6e 67 20 61  .**.** Finding a
1217e 20 70 61 72 74 69 63 75 6c 61 72 20 6b 65 79 20   particular key 
1217f 72 65 71 75 69 72 65 73 20 72 65 61 64 69 6e 67  requires reading
12180 20 4f 28 6c 6f 67 28 4d 29 29 20 70 61 67 65 73   O(log(M)) pages
12181 20 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 64 69   from the .** di
12182 73 6b 20 77 68 65 72 65 20 4d 20 69 73 20 74 68  sk where M is th
12183 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72  e number of entr
12184 69 65 73 20 69 6e 20 74 68 65 20 74 72 65 65 2e  ies in the tree.
12185 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 69 73 20 69  .**.** In this i
12186 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 2c 20 61  mplementation, a
12187 20 73 69 6e 67 6c 65 20 66 69 6c 65 20 63 61 6e   single file can
12188 20 68 6f 6c 64 20 6f 6e 65 20 6f 72 20 6d 6f 72   hold one or mor
12189 65 20 73 65 70 61 72 61 74 65 20 0a 2a 2a 20 42  e separate .** B
1218a 54 72 65 65 73 2e 20 20 45 61 63 68 20 42 54 72  Trees.  Each BTr
1218b 65 65 20 69 73 20 69 64 65 6e 74 69 66 69 65 64  ee is identified
1218c 20 62 79 20 74 68 65 20 69 6e 64 65 78 20 6f 66   by the index of
1218d 20 69 74 73 20 72 6f 6f 74 20 70 61 67 65 2e 20   its root page. 
1218e 20 54 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20   The.** key and 
1218f 64 61 74 61 20 66 6f 72 20 61 6e 79 20 65 6e 74  data for any ent
12190 72 79 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20  ry are combined 
12191 74 6f 20 66 6f 72 6d 20 74 68 65 20 22 70 61 79  to form the "pay
12192 6c 6f 61 64 22 2e 20 20 41 0a 2a 2a 20 66 69 78  load".  A.** fix
12193 65 64 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79  ed amount of pay
12194 6c 6f 61 64 20 63 61 6e 20 62 65 20 63 61 72 72  load can be carr
12195 69 65 64 20 64 69 72 65 63 74 6c 79 20 6f 6e 20  ied directly on 
12196 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  the database.** 
12197 70 61 67 65 2e 20 20 49 66 20 74 68 65 20 70 61  page.  If the pa
12198 79 6c 6f 61 64 20 69 73 20 6c 61 72 67 65 72 20  yload is larger 
12199 74 68 61 6e 20 74 68 65 20 70 72 65 73 65 74 20  than the preset 
1219a 61 6d 6f 75 6e 74 20 74 68 65 6e 20 73 75 72 70  amount then surp
1219b 6c 75 73 0a 2a 2a 20 62 79 74 65 73 20 61 72 65  lus.** bytes are
1219c 20 73 74 6f 72 65 64 20 6f 6e 20 6f 76 65 72 66   stored on overf
1219d 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65 20  low pages.  The 
1219e 70 61 79 6c 6f 61 64 20 66 6f 72 20 61 6e 20 65  payload for an e
1219f 6e 74 72 79 0a 2a 2a 20 61 6e 64 20 74 68 65 20  ntry.** and the 
121a0 70 72 65 63 65 64 69 6e 67 20 70 6f 69 6e 74 65  preceding pointe
121a1 72 20 61 72 65 20 63 6f 6d 62 69 6e 65 64 20 74  r are combined t
121a2 6f 20 66 6f 72 6d 20 61 20 22 43 65 6c 6c 22 2e  o form a "Cell".
121a3 20 20 45 61 63 68 20 0a 2a 2a 20 70 61 67 65 20    Each .** page 
121a4 68 61 73 20 61 20 73 6d 61 6c 6c 20 68 65 61 64  has a small head
121a5 65 72 20 77 68 69 63 68 20 63 6f 6e 74 61 69 6e  er which contain
121a6 73 20 74 68 65 20 50 74 72 28 4e 29 20 70 6f 69  s the Ptr(N) poi
121a7 6e 74 65 72 20 61 6e 64 20 6f 74 68 65 72 0a 2a  nter and other.*
121a8 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 75  * information su
121a9 63 68 20 61 73 20 74 68 65 20 73 69 7a 65 20 6f  ch as the size o
121aa 66 20 6b 65 79 20 61 6e 64 20 64 61 74 61 2e 0a  f key and data..
121ab 2a 2a 0a 2a 2a 20 46 4f 52 4d 41 54 20 44 45 54  **.** FORMAT DET
121ac 41 49 4c 53 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  AILS.**.** The f
121ad 69 6c 65 20 69 73 20 64 69 76 69 64 65 64 20 69  ile is divided i
121ae 6e 74 6f 20 70 61 67 65 73 2e 20 20 54 68 65 20  nto pages.  The 
121af 66 69 72 73 74 20 70 61 67 65 20 69 73 20 63 61  first page is ca
121b0 6c 6c 65 64 20 70 61 67 65 20 31 2c 0a 2a 2a 20  lled page 1,.** 
121b1 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 70 61  the second is pa
121b2 67 65 20 32 2c 20 61 6e 64 20 73 6f 20 66 6f 72  ge 2, and so for
121b3 74 68 2e 20 20 41 20 70 61 67 65 20 6e 75 6d 62  th.  A page numb
121b4 65 72 20 6f 66 20 7a 65 72 6f 20 69 6e 64 69 63  er of zero indic
121b5 61 74 65 73 0a 2a 2a 20 22 6e 6f 20 73 75 63 68  ates.** "no such
121b6 20 70 61 67 65 22 2e 20 20 54 68 65 20 70 61 67   page".  The pag
121b7 65 20 73 69 7a 65 20 63 61 6e 20 62 65 20 61 6e  e size can be an
121b8 79 74 68 69 6e 67 20 62 65 74 77 65 65 6e 20 35  ything between 5
121b9 31 32 20 61 6e 64 20 36 35 35 33 36 2e 0a 2a 2a  12 and 65536..**
121ba 20 45 61 63 68 20 70 61 67 65 20 63 61 6e 20 62   Each page can b
121bb 65 20 65 69 74 68 65 72 20 61 20 62 74 72 65 65  e either a btree
121bc 20 70 61 67 65 2c 20 61 20 66 72 65 65 6c 69 73   page, a freelis
121bd 74 20 70 61 67 65 20 6f 72 20 61 6e 20 6f 76 65  t page or an ove
121be 72 66 6c 6f 77 0a 2a 2a 20 70 61 67 65 2e 0a 2a  rflow.** page..*
121bf 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 70  *.** The first p
121c0 61 67 65 20 69 73 20 61 6c 77 61 79 73 20 61 20  age is always a 
121c1 62 74 72 65 65 20 70 61 67 65 2e 20 20 54 68 65  btree page.  The
121c2 20 66 69 72 73 74 20 31 30 30 20 62 79 74 65 73   first 100 bytes
121c3 20 6f 66 20 74 68 65 20 66 69 72 73 74 0a 2a 2a   of the first.**
121c4 20 70 61 67 65 20 63 6f 6e 74 61 69 6e 20 61 20   page contain a 
121c5 73 70 65 63 69 61 6c 20 68 65 61 64 65 72 20 28  special header (
121c6 74 68 65 20 22 66 69 6c 65 20 68 65 61 64 65 72  the "file header
121c7 22 29 20 74 68 61 74 20 64 65 73 63 72 69 62 65  ") that describe
121c8 73 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a 20 54  s the file..** T
121c9 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68 65  he format of the
121ca 20 66 69 6c 65 20 68 65 61 64 65 72 20 69 73 20   file header is 
121cb 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a  as follows:.**.*
121cc 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49 5a  *   OFFSET   SIZ
121cd 45 20 20 20 20 44 45 53 43 52 49 50 54 49 4f 4e  E    DESCRIPTION
121ce 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20 20 20  .**      0      
121cf 31 36 20 20 20 20 20 48 65 61 64 65 72 20 73 74  16     Header st
121d0 72 69 6e 67 3a 20 22 53 51 4c 69 74 65 20 66 6f  ring: "SQLite fo
121d1 72 6d 61 74 20 33 5c 30 30 30 22 0a 2a 2a 20 20  rmat 3\000".**  
121d2 20 20 20 31 36 20 20 20 20 20 20 20 32 20 20 20     16       2   
121d3 20 20 50 61 67 65 20 73 69 7a 65 20 69 6e 20 62    Page size in b
121d4 79 74 65 73 2e 20 20 0a 2a 2a 20 20 20 20 20 31  ytes.  .**     1
121d5 38 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69  8       1     Fi
121d6 6c 65 20 66 6f 72 6d 61 74 20 77 72 69 74 65 20  le format write 
121d7 76 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 31  version.**     1
121d8 39 20 20 20 20 20 20 20 31 20 20 20 20 20 46 69  9       1     Fi
121d9 6c 65 20 66 6f 72 6d 61 74 20 72 65 61 64 20 76  le format read v
121da 65 72 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 32 30  ersion.**     20
121db 20 20 20 20 20 20 20 31 20 20 20 20 20 42 79 74         1     Byt
121dc 65 73 20 6f 66 20 75 6e 75 73 65 64 20 73 70 61  es of unused spa
121dd 63 65 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66  ce at the end of
121de 20 65 61 63 68 20 70 61 67 65 0a 2a 2a 20 20 20   each page.**   
121df 20 20 32 31 20 20 20 20 20 20 20 31 20 20 20 20    21       1    
121e0 20 4d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61   Max embedded pa
121e1 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 0a 2a  yload fraction.*
121e2 2a 20 20 20 20 20 32 32 20 20 20 20 20 20 20 31  *     22       1
121e3 20 20 20 20 20 4d 69 6e 20 65 6d 62 65 64 64 65       Min embedde
121e4 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
121e5 6f 6e 0a 2a 2a 20 20 20 20 20 32 33 20 20 20 20  on.**     23    
121e6 20 20 20 31 20 20 20 20 20 4d 69 6e 20 6c 65 61     1     Min lea
121e7 66 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  f payload fracti
121e8 6f 6e 0a 2a 2a 20 20 20 20 20 32 34 20 20 20 20  on.**     24    
121e9 20 20 20 34 20 20 20 20 20 46 69 6c 65 20 63 68     4     File ch
121ea 61 6e 67 65 20 63 6f 75 6e 74 65 72 0a 2a 2a 20  ange counter.** 
121eb 20 20 20 20 32 38 20 20 20 20 20 20 20 34 20 20      28       4  
121ec 20 20 20 52 65 73 65 72 76 65 64 20 66 6f 72 20     Reserved for 
121ed 66 75 74 75 72 65 20 75 73 65 0a 2a 2a 20 20 20  future use.**   
121ee 20 20 33 32 20 20 20 20 20 20 20 34 20 20 20 20    32       4    
121ef 20 46 69 72 73 74 20 66 72 65 65 6c 69 73 74 20   First freelist 
121f0 70 61 67 65 0a 2a 2a 20 20 20 20 20 33 36 20 20  page.**     36  
121f1 20 20 20 20 20 34 20 20 20 20 20 4e 75 6d 62 65       4     Numbe
121f2 72 20 6f 66 20 66 72 65 65 6c 69 73 74 20 70 61  r of freelist pa
121f3 67 65 73 20 69 6e 20 74 68 65 20 66 69 6c 65 0a  ges in the file.
121f4 2a 2a 20 20 20 20 20 34 30 20 20 20 20 20 20 36  **     40      6
121f5 30 20 20 20 20 20 31 35 20 34 2d 62 79 74 65 20  0     15 4-byte 
121f6 6d 65 74 61 20 76 61 6c 75 65 73 20 70 61 73 73  meta values pass
121f7 65 64 20 74 6f 20 68 69 67 68 65 72 20 6c 61 79  ed to higher lay
121f8 65 72 73 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 6f 66  ers.**.** All of
121f9 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
121fa 75 65 73 20 61 72 65 20 62 69 67 2d 65 6e 64 69  ues are big-endi
121fb 61 6e 20 28 6d 6f 73 74 20 73 69 67 6e 69 66 69  an (most signifi
121fc 63 61 6e 74 20 62 79 74 65 20 66 69 72 73 74 29  cant byte first)
121fd 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6c 65  ..**.** The file
121fe 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
121ff 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 77  is incremented w
12200 68 65 6e 20 74 68 65 20 64 61 74 61 62 61 73 65  hen the database
12201 20 69 73 20 63 68 61 6e 67 65 64 0a 2a 2a 20 54   is changed.** T
12202 68 69 73 20 63 6f 75 6e 74 65 72 20 61 6c 6c 6f  his counter allo
12203 77 73 20 6f 74 68 65 72 20 70 72 6f 63 65 73 73  ws other process
12204 65 73 20 74 6f 20 6b 6e 6f 77 20 77 68 65 6e 20  es to know when 
12205 74 68 65 20 66 69 6c 65 20 68 61 73 20 63 68 61  the file has cha
12206 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74 68 75 73  nged.** and thus
12207 20 77 68 65 6e 20 74 68 65 79 20 6e 65 65 64 20   when they need 
12208 74 6f 20 66 6c 75 73 68 20 74 68 65 69 72 20 63  to flush their c
12209 61 63 68 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ache..**.** The 
1220a 6d 61 78 20 65 6d 62 65 64 64 65 64 20 70 61 79  max embedded pay
1220b 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20 69 73  load fraction is
1220c 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20 74   the amount of t
1220d 68 65 20 74 6f 74 61 6c 20 75 73 61 62 6c 65 0a  he total usable.
1220e 2a 2a 20 73 70 61 63 65 20 69 6e 20 61 20 70 61  ** space in a pa
1220f 67 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 63  ge that can be c
12210 6f 6e 73 75 6d 65 64 20 62 79 20 61 20 73 69 6e  onsumed by a sin
12211 67 6c 65 20 63 65 6c 6c 20 66 6f 72 20 73 74 61  gle cell for sta
12212 6e 64 61 72 64 0a 2a 2a 20 42 2d 74 72 65 65 20  ndard.** B-tree 
12213 28 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 29 20 74  (non-LEAFDATA) t
12214 61 62 6c 65 73 2e 20 20 41 20 76 61 6c 75 65 20  ables.  A value 
12215 6f 66 20 32 35 35 20 6d 65 61 6e 73 20 31 30 30  of 255 means 100
12216 25 2e 20 20 54 68 65 20 64 65 66 61 75 6c 74 0a  %.  The default.
12217 2a 2a 20 69 73 20 74 6f 20 6c 69 6d 69 74 20 74  ** is to limit t
12218 68 65 20 6d 61 78 69 6d 75 6d 20 63 65 6c 6c 20  he maximum cell 
12219 73 69 7a 65 20 73 6f 20 74 68 61 74 20 61 74 20  size so that at 
1221a 6c 65 61 73 74 20 34 20 63 65 6c 6c 73 20 77 69  least 4 cells wi
1221b 6c 6c 20 66 69 74 0a 2a 2a 20 6f 6e 20 6f 6e 65  ll fit.** on one
1221c 20 70 61 67 65 2e 20 20 54 68 75 73 20 74 68 65   page.  Thus the
1221d 20 64 65 66 61 75 6c 74 20 6d 61 78 20 65 6d 62   default max emb
1221e 65 64 64 65 64 20 70 61 79 6c 6f 61 64 20 66 72  edded payload fr
1221f 61 63 74 69 6f 6e 20 69 73 20 36 34 2e 0a 2a 2a  action is 64..**
12220 0a 2a 2a 20 49 66 20 74 68 65 20 70 61 79 6c 6f  .** If the paylo
12221 61 64 20 66 6f 72 20 61 20 63 65 6c 6c 20 69 73  ad for a cell is
12222 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74 68 65   larger than the
12223 20 6d 61 78 20 70 61 79 6c 6f 61 64 2c 20 74 68   max payload, th
12224 65 6e 20 65 78 74 72 61 0a 2a 2a 20 70 61 79 6c  en extra.** payl
12225 6f 61 64 20 69 73 20 73 70 69 6c 6c 65 64 20 74  oad is spilled t
12226 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
12227 2e 20 20 4f 6e 63 65 20 61 6e 20 6f 76 65 72 66  .  Once an overf
12228 6c 6f 77 20 70 61 67 65 20 69 73 20 61 6c 6c 6f  low page is allo
12229 63 61 74 65 64 2c 0a 2a 2a 20 61 73 20 6d 61 6e  cated,.** as man
1222a 79 20 62 79 74 65 73 20 61 73 20 70 6f 73 73 69  y bytes as possi
1222b 62 6c 65 20 61 72 65 20 6d 6f 76 65 64 20 69 6e  ble are moved in
1222c 74 6f 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  to the overflow 
1222d 70 61 67 65 73 20 77 69 74 68 6f 75 74 20 6c 65  pages without le
1222e 74 74 69 6e 67 0a 2a 2a 20 74 68 65 20 63 65 6c  tting.** the cel
1222f 6c 20 73 69 7a 65 20 64 72 6f 70 20 62 65 6c 6f  l size drop belo
12230 77 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64  w the min embedd
12231 65 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74  ed payload fract
12232 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6d  ion..**.** The m
12233 69 6e 20 6c 65 61 66 20 70 61 79 6c 6f 61 64 20  in leaf payload 
12234 66 72 61 63 74 69 6f 6e 20 69 73 20 6c 69 6b 65  fraction is like
12235 20 74 68 65 20 6d 69 6e 20 65 6d 62 65 64 64 65   the min embedde
12236 64 20 70 61 79 6c 6f 61 64 20 66 72 61 63 74 69  d payload fracti
12237 6f 6e 0a 2a 2a 20 65 78 63 65 70 74 20 74 68 61  on.** except tha
12238 74 20 69 74 20 61 70 70 6c 69 65 73 20 74 6f 20  t it applies to 
12239 6c 65 61 66 20 6e 6f 64 65 73 20 69 6e 20 61 20  leaf nodes in a 
1223a 4c 45 41 46 44 41 54 41 20 74 72 65 65 2e 20 20  LEAFDATA tree.  
1223b 54 68 65 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 70  The maximum.** p
1223c 61 79 6c 6f 61 64 20 66 72 61 63 74 69 6f 6e 20  ayload fraction 
1223d 66 6f 72 20 61 20 4c 45 41 46 44 41 54 41 20 74  for a LEAFDATA t
1223e 72 65 65 20 69 73 20 61 6c 77 61 79 73 20 31 30  ree is always 10
1223f 30 25 20 28 6f 72 20 32 35 35 29 20 61 6e 64 20  0% (or 255) and 
12240 69 74 0a 2a 2a 20 6e 6f 74 20 73 70 65 63 69 66  it.** not specif
12241 69 65 64 20 69 6e 20 74 68 65 20 68 65 61 64 65  ied in the heade
12242 72 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 62 74  r..**.** Each bt
12243 72 65 65 20 70 61 67 65 73 20 69 73 20 64 69 76  ree pages is div
12244 69 64 65 64 20 69 6e 74 6f 20 74 68 72 65 65 20  ided into three 
12245 73 65 63 74 69 6f 6e 73 3a 20 20 54 68 65 20 68  sections:  The h
12246 65 61 64 65 72 2c 20 74 68 65 0a 2a 2a 20 63 65  eader, the.** ce
12247 6c 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79  ll pointer array
12248 2c 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  , and the cell c
12249 6f 6e 74 65 6e 74 20 61 72 65 61 2e 20 20 50 61  ontent area.  Pa
1224a 67 65 20 31 20 61 6c 73 6f 20 68 61 73 20 61 20  ge 1 also has a 
1224b 31 30 30 2d 62 79 74 65 0a 2a 2a 20 66 69 6c 65  100-byte.** file
1224c 20 68 65 61 64 65 72 20 74 68 61 74 20 6f 63 63   header that occ
1224d 75 72 73 20 62 65 66 6f 72 65 20 74 68 65 20 70  urs before the p
1224e 61 67 65 20 68 65 61 64 65 72 2e 0a 2a 2a 0a 2a  age header..**.*
1224f 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d  *      |--------
12250 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20 20  --------|.**    
12251 20 20 7c 20 66 69 6c 65 20 68 65 61 64 65 72 20    | file header 
12252 20 20 20 7c 20 20 20 31 30 30 20 62 79 74 65 73     |   100 bytes
12253 2e 20 20 50 61 67 65 20 31 20 6f 6e 6c 79 2e 0a  .  Page 1 only..
12254 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d 2d 2d 2d  **      |-------
12255 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a 20 20 20  ---------|.**   
12256 20 20 20 7c 20 70 61 67 65 20 68 65 61 64 65 72     | page header
12257 20 20 20 20 7c 20 20 20 38 20 62 79 74 65 73 20      |   8 bytes 
12258 66 6f 72 20 6c 65 61 76 65 73 2e 20 20 31 32 20  for leaves.  12 
12259 62 79 74 65 73 20 66 6f 72 20 69 6e 74 65 72 69  bytes for interi
1225a 6f 72 20 6e 6f 64 65 73 0a 2a 2a 20 20 20 20 20  or nodes.**     
1225b 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   |--------------
1225c 2d 2d 7c 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65  --|.**      | ce
1225d 6c 6c 20 70 6f 69 6e 74 65 72 20 20 20 7c 20 20  ll pointer   |  
1225e 20 7c 20 20 32 20 62 79 74 65 73 20 70 65 72 20   |  2 bytes per 
1225f 63 65 6c 6c 2e 20 20 53 6f 72 74 65 64 20 6f 72  cell.  Sorted or
12260 64 65 72 2e 0a 2a 2a 20 20 20 20 20 20 7c 20 61  der..**      | a
12261 72 72 61 79 20 20 20 20 20 20 20 20 20 20 7c 20  rray          | 
12262 20 20 7c 20 20 47 72 6f 77 73 20 64 6f 77 6e 77    |  Grows downw
12263 61 72 64 0a 2a 2a 20 20 20 20 20 20 7c 20 20 20  ard.**      |   
12264 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 20 20               |  
12265 20 76 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d 2d   v.**      |----
12266 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 0a 2a 2a  ------------|.**
12267 20 20 20 20 20 20 7c 20 75 6e 61 6c 6c 6f 63 61        | unalloca
12268 74 65 64 20 20 20 20 7c 0a 2a 2a 20 20 20 20 20  ted    |.**     
12269 20 7c 20 73 70 61 63 65 20 20 20 20 20 20 20 20   | space        
1226a 20 20 7c 0a 2a 2a 20 20 20 20 20 20 7c 2d 2d 2d    |.**      |---
1226b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 7c 20 20  -------------|  
1226c 20 5e 20 20 47 72 6f 77 73 20 75 70 77 61 72 64   ^  Grows upward
1226d 73 0a 2a 2a 20 20 20 20 20 20 7c 20 63 65 6c 6c  s.**      | cell
1226e 20 63 6f 6e 74 65 6e 74 20 20 20 7c 20 20 20 7c   content   |   |
1226f 20 20 41 72 62 69 74 72 61 72 79 20 6f 72 64 65    Arbitrary orde
12270 72 20 69 6e 74 65 72 73 70 65 72 73 65 64 20 77  r interspersed w
12271 69 74 68 20 66 72 65 65 62 6c 6f 63 6b 73 2e 0a  ith freeblocks..
12272 2a 2a 20 20 20 20 20 20 7c 20 61 72 65 61 20 20  **      | area  
12273 20 20 20 20 20 20 20 20 20 7c 20 20 20 7c 20 20           |   |  
12274 61 6e 64 20 66 72 65 65 20 73 70 61 63 65 20 66  and free space f
12275 72 61 67 6d 65 6e 74 73 2e 0a 2a 2a 20 20 20 20  ragments..**    
12276 20 20 7c 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d    |-------------
12277 2d 2d 2d 7c 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70  ---|.**.** The p
12278 61 67 65 20 68 65 61 64 65 72 73 20 6c 6f 6f 6b  age headers look
12279 73 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a  s like this:.**.
1227a 2a 2a 20 20 20 4f 46 46 53 45 54 20 20 20 53 49  **   OFFSET   SI
1227b 5a 45 20 20 20 20 20 44 45 53 43 52 49 50 54 49  ZE     DESCRIPTI
1227c 4f 4e 0a 2a 2a 20 20 20 20 20 20 30 20 20 20 20  ON.**      0    
1227d 20 20 20 31 20 20 20 20 20 20 46 6c 61 67 73 2e     1      Flags.
1227e 20 31 3a 20 69 6e 74 6b 65 79 2c 20 32 3a 20 7a   1: intkey, 2: z
1227f 65 72 6f 64 61 74 61 2c 20 34 3a 20 6c 65 61 66  erodata, 4: leaf
12280 64 61 74 61 2c 20 38 3a 20 6c 65 61 66 0a 2a 2a  data, 8: leaf.**
12281 20 20 20 20 20 20 31 20 20 20 20 20 20 20 32 20        1       2 
12282 20 20 20 20 20 62 79 74 65 20 6f 66 66 73 65 74       byte offset
12283 20 74 6f 20 74 68 65 20 66 69 72 73 74 20 66 72   to the first fr
12284 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20 20 20 20  eeblock.**      
12285 33 20 20 20 20 20 20 20 32 20 20 20 20 20 20 6e  3       2      n
12286 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
12287 6e 20 74 68 69 73 20 70 61 67 65 0a 2a 2a 20 20  n this page.**  
12288 20 20 20 20 35 20 20 20 20 20 20 20 32 20 20 20      5       2   
12289 20 20 20 66 69 72 73 74 20 62 79 74 65 20 6f 66     first byte of
1228a 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
1228b 74 20 61 72 65 61 0a 2a 2a 20 20 20 20 20 20 37  t area.**      7
1228c 20 20 20 20 20 20 20 31 20 20 20 20 20 20 6e 75         1      nu
1228d 6d 62 65 72 20 6f 66 20 66 72 61 67 6d 65 6e 74  mber of fragment
1228e 65 64 20 66 72 65 65 20 62 79 74 65 73 0a 2a 2a  ed free bytes.**
1228f 20 20 20 20 20 20 38 20 20 20 20 20 20 20 34 20        8       4 
12290 20 20 20 20 20 52 69 67 68 74 20 63 68 69 6c 64       Right child
12291 20 28 74 68 65 20 50 74 72 28 4e 29 20 76 61 6c   (the Ptr(N) val
12292 75 65 29 2e 20 20 4f 6d 69 74 74 65 64 20 6f 6e  ue).  Omitted on
12293 20 6c 65 61 76 65 73 2e 0a 2a 2a 0a 2a 2a 20 54   leaves..**.** T
12294 68 65 20 66 6c 61 67 73 20 64 65 66 69 6e 65 20  he flags define 
12295 74 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 74 68  the format of th
12296 69 73 20 62 74 72 65 65 20 70 61 67 65 2e 20 20  is btree page.  
12297 54 68 65 20 6c 65 61 66 20 66 6c 61 67 20 6d 65  The leaf flag me
12298 61 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 69 73  ans that.** this
12299 20 70 61 67 65 20 68 61 73 20 6e 6f 20 63 68 69   page has no chi
1229a 6c 64 72 65 6e 2e 20 20 54 68 65 20 7a 65 72 6f  ldren.  The zero
1229b 64 61 74 61 20 66 6c 61 67 20 6d 65 61 6e 73 20  data flag means 
1229c 74 68 61 74 20 74 68 69 73 20 70 61 67 65 20 63  that this page c
1229d 61 72 72 69 65 73 0a 2a 2a 20 6f 6e 6c 79 20 6b  arries.** only k
1229e 65 79 73 20 61 6e 64 20 6e 6f 20 64 61 74 61 2e  eys and no data.
1229f 20 20 54 68 65 20 69 6e 74 6b 65 79 20 66 6c 61    The intkey fla
122a0 67 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  g means that the
122a1 20 6b 65 79 20 69 73 20 61 20 69 6e 74 65 67 65   key is a intege
122a2 72 0a 2a 2a 20 77 68 69 63 68 20 69 73 20 73 74  r.** which is st
122a3 6f 72 65 64 20 69 6e 20 74 68 65 20 6b 65 79 20  ored in the key 
122a4 73 69 7a 65 20 65 6e 74 72 79 20 6f 66 20 74 68  size entry of th
122a5 65 20 63 65 6c 6c 20 68 65 61 64 65 72 20 72 61  e cell header ra
122a6 74 68 65 72 20 74 68 61 6e 20 69 6e 0a 2a 2a 20  ther than in.** 
122a7 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72 65 61  the payload area
122a8 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 65 6c 6c  ..**.** The cell
122a9 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 20 62   pointer array b
122aa 65 67 69 6e 73 20 6f 6e 20 74 68 65 20 66 69 72  egins on the fir
122ab 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74 68  st byte after th
122ac 65 20 70 61 67 65 20 68 65 61 64 65 72 2e 0a 2a  e page header..*
122ad 2a 20 54 68 65 20 63 65 6c 6c 20 70 6f 69 6e 74  * The cell point
122ae 65 72 20 61 72 72 61 79 20 63 6f 6e 74 61 69 6e  er array contain
122af 73 20 7a 65 72 6f 20 6f 72 20 6d 6f 72 65 20 32  s zero or more 2
122b0 2d 62 79 74 65 20 6e 75 6d 62 65 72 73 20 77 68  -byte numbers wh
122b1 69 63 68 20 61 72 65 0a 2a 2a 20 6f 66 66 73 65  ich are.** offse
122b2 74 73 20 66 72 6f 6d 20 74 68 65 20 62 65 67 69  ts from the begi
122b3 6e 6e 69 6e 67 20 6f 66 20 74 68 65 20 70 61 67  nning of the pag
122b4 65 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63 6f  e to the cell co
122b5 6e 74 65 6e 74 20 69 6e 20 74 68 65 20 63 65 6c  ntent in the cel
122b6 6c 0a 2a 2a 20 63 6f 6e 74 65 6e 74 20 61 72 65  l.** content are
122b7 61 2e 20 20 54 68 65 20 63 65 6c 6c 20 70 6f 69  a.  The cell poi
122b8 6e 74 65 72 73 20 6f 63 63 75 72 20 69 6e 20 73  nters occur in s
122b9 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 20 54 68  orted order.  Th
122ba 65 20 73 79 73 74 65 6d 20 73 74 72 69 76 65 73  e system strives
122bb 0a 2a 2a 20 74 6f 20 6b 65 65 70 20 66 72 65 65  .** to keep free
122bc 20 73 70 61 63 65 20 61 66 74 65 72 20 74 68 65   space after the
122bd 20 6c 61 73 74 20 63 65 6c 6c 20 70 6f 69 6e 74   last cell point
122be 65 72 20 73 6f 20 74 68 61 74 20 6e 65 77 20 63  er so that new c
122bf 65 6c 6c 73 20 63 61 6e 0a 2a 2a 20 62 65 20 65  ells can.** be e
122c0 61 73 69 6c 79 20 61 64 64 65 64 20 77 69 74 68  asily added with
122c1 6f 75 74 20 68 61 76 69 6e 67 20 74 6f 20 64 65  out having to de
122c2 66 72 61 67 6d 65 6e 74 20 74 68 65 20 70 61 67  fragment the pag
122c3 65 2e 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f  e..**.** Cell co
122c4 6e 74 65 6e 74 20 69 73 20 73 74 6f 72 65 64 20  ntent is stored 
122c5 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64 20  at the very end 
122c6 6f 66 20 74 68 65 20 70 61 67 65 20 61 6e 64 20  of the page and 
122c7 67 72 6f 77 73 20 74 6f 77 61 72 64 20 74 68 65  grows toward the
122c8 0a 2a 2a 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  .** beginning of
122c9 20 74 68 65 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a   the page..**.**
122ca 20 55 6e 75 73 65 64 20 73 70 61 63 65 20 77 69   Unused space wi
122cb 74 68 69 6e 20 74 68 65 20 63 65 6c 6c 20 63 6f  thin the cell co
122cc 6e 74 65 6e 74 20 61 72 65 61 20 69 73 20 63 6f  ntent area is co
122cd 6c 6c 65 63 74 65 64 20 69 6e 74 6f 20 61 20 6c  llected into a l
122ce 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 0a 2a 2a  inked list of.**
122cf 20 66 72 65 65 62 6c 6f 63 6b 73 2e 20 20 45 61   freeblocks.  Ea
122d0 63 68 20 66 72 65 65 62 6c 6f 63 6b 20 69 73 20  ch freeblock is 
122d1 61 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73  at least 4 bytes
122d2 20 69 6e 20 73 69 7a 65 2e 20 20 54 68 65 20 62   in size.  The b
122d3 79 74 65 20 6f 66 66 73 65 74 0a 2a 2a 20 74 6f  yte offset.** to
122d4 20 74 68 65 20 66 69 72 73 74 20 66 72 65 65 62   the first freeb
122d5 6c 6f 63 6b 20 69 73 20 67 69 76 65 6e 20 69 6e  lock is given in
122d6 20 74 68 65 20 68 65 61 64 65 72 2e 20 20 46 72   the header.  Fr
122d7 65 65 62 6c 6f 63 6b 73 20 6f 63 63 75 72 20 69  eeblocks occur i
122d8 6e 0a 2a 2a 20 69 6e 63 72 65 61 73 69 6e 67 20  n.** increasing 
122d9 6f 72 64 65 72 2e 20 20 42 65 63 61 75 73 65 20  order.  Because 
122da 61 20 66 72 65 65 62 6c 6f 63 6b 20 6d 75 73 74  a freeblock must
122db 20 62 65 20 61 74 20 6c 65 61 73 74 20 34 20 62   be at least 4 b
122dc 79 74 65 73 20 69 6e 20 73 69 7a 65 2c 0a 2a 2a  ytes in size,.**
122dd 20 61 6e 79 20 67 72 6f 75 70 20 6f 66 20 33 20   any group of 3 
122de 6f 72 20 66 65 77 65 72 20 75 6e 75 73 65 64 20  or fewer unused 
122df 62 79 74 65 73 20 69 6e 20 74 68 65 20 63 65 6c  bytes in the cel
122e0 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 63  l content area c
122e1 61 6e 6e 6f 74 0a 2a 2a 20 65 78 69 73 74 20 6f  annot.** exist o
122e2 6e 20 74 68 65 20 66 72 65 65 62 6c 6f 63 6b 20  n the freeblock 
122e3 63 68 61 69 6e 2e 20 20 41 20 67 72 6f 75 70 20  chain.  A group 
122e4 6f 66 20 33 20 6f 72 20 66 65 77 65 72 20 66 72  of 3 or fewer fr
122e5 65 65 20 62 79 74 65 73 20 69 73 20 63 61 6c 6c  ee bytes is call
122e6 65 64 0a 2a 2a 20 61 20 66 72 61 67 6d 65 6e 74  ed.** a fragment
122e7 2e 20 20 54 68 65 20 74 6f 74 61 6c 20 6e 75 6d  .  The total num
122e8 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
122e9 61 6c 6c 20 66 72 61 67 6d 65 6e 74 73 20 69 73  all fragments is
122ea 20 72 65 63 6f 72 64 65 64 2e 0a 2a 2a 20 69 6e   recorded..** in
122eb 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65 72   the page header
122ec 20 61 74 20 6f 66 66 73 65 74 20 37 2e 0a 2a 2a   at offset 7..**
122ed 0a 2a 2a 20 20 20 20 53 49 5a 45 20 20 20 20 44  .**    SIZE    D
122ee 45 53 43 52 49 50 54 49 4f 4e 0a 2a 2a 20 20 20  ESCRIPTION.**   
122ef 20 20 20 32 20 20 20 20 20 42 79 74 65 20 6f 66     2     Byte of
122f0 66 73 65 74 20 6f 66 20 74 68 65 20 6e 65 78 74  fset of the next
122f1 20 66 72 65 65 62 6c 6f 63 6b 0a 2a 2a 20 20 20   freeblock.**   
122f2 20 20 20 32 20 20 20 20 20 42 79 74 65 73 20 69     2     Bytes i
122f3 6e 20 74 68 69 73 20 66 72 65 65 62 6c 6f 63 6b  n this freeblock
122f4 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 73 20 61 72 65  .**.** Cells are
122f5 20 6f 66 20 76 61 72 69 61 62 6c 65 20 6c 65 6e   of variable len
122f6 67 74 68 2e 20 20 43 65 6c 6c 73 20 61 72 65 20  gth.  Cells are 
122f7 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 63 65  stored in the ce
122f8 6c 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20  ll content area 
122f9 61 74 0a 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66  at.** the end of
122fa 20 74 68 65 20 70 61 67 65 2e 20 20 50 6f 69 6e   the page.  Poin
122fb 74 65 72 73 20 74 6f 20 74 68 65 20 63 65 6c 6c  ters to the cell
122fc 73 20 61 72 65 20 69 6e 20 74 68 65 20 63 65 6c  s are in the cel
122fd 6c 20 70 6f 69 6e 74 65 72 20 61 72 72 61 79 0a  l pointer array.
122fe 2a 2a 20 74 68 61 74 20 69 6d 6d 65 64 69 61 74  ** that immediat
122ff 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74 68 65 20  ely follows the 
12300 70 61 67 65 20 68 65 61 64 65 72 2e 20 20 43 65  page header.  Ce
12301 6c 6c 73 20 69 73 20 6e 6f 74 20 6e 65 63 65 73  lls is not neces
12302 73 61 72 69 6c 79 0a 2a 2a 20 63 6f 6e 74 69 67  sarily.** contig
12303 75 6f 75 73 20 6f 72 20 69 6e 20 6f 72 64 65 72  uous or in order
12304 2c 20 62 75 74 20 63 65 6c 6c 20 70 6f 69 6e 74  , but cell point
12305 65 72 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f  ers are contiguo
12306 75 73 20 61 6e 64 20 69 6e 20 6f 72 64 65 72 2e  us and in order.
12307 0a 2a 2a 0a 2a 2a 20 43 65 6c 6c 20 63 6f 6e 74  .**.** Cell cont
12308 65 6e 74 20 6d 61 6b 65 73 20 75 73 65 20 6f 66  ent makes use of
12309 20 76 61 72 69 61 62 6c 65 20 6c 65 6e 67 74 68   variable length
1230a 20 69 6e 74 65 67 65 72 73 2e 20 20 41 20 76 61   integers.  A va
1230b 72 69 61 62 6c 65 0a 2a 2a 20 6c 65 6e 67 74 68  riable.** length
1230c 20 69 6e 74 65 67 65 72 20 69 73 20 31 20 74 6f   integer is 1 to
1230d 20 39 20 62 79 74 65 73 20 77 68 65 72 65 20 74   9 bytes where t
1230e 68 65 20 6c 6f 77 65 72 20 37 20 62 69 74 73 20  he lower 7 bits 
1230f 6f 66 20 65 61 63 68 20 0a 2a 2a 20 62 79 74 65  of each .** byte
12310 20 61 72 65 20 75 73 65 64 2e 20 20 54 68 65 20   are used.  The 
12311 69 6e 74 65 67 65 72 20 63 6f 6e 73 69 73 74 73  integer consists
12312 20 6f 66 20 61 6c 6c 20 62 79 74 65 73 20 74 68   of all bytes th
12313 61 74 20 68 61 76 65 20 62 69 74 20 38 20 73 65  at have bit 8 se
12314 74 20 61 6e 64 0a 2a 2a 20 74 68 65 20 66 69 72  t and.** the fir
12315 73 74 20 62 79 74 65 20 77 69 74 68 20 62 69 74  st byte with bit
12316 20 38 20 63 6c 65 61 72 2e 20 20 54 68 65 20 6d   8 clear.  The m
12317 6f 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20  ost significant 
12318 62 79 74 65 20 6f 66 20 74 68 65 20 69 6e 74 65  byte of the inte
12319 67 65 72 0a 2a 2a 20 61 70 70 65 61 72 73 20 66  ger.** appears f
1231a 69 72 73 74 2e 20 20 41 20 76 61 72 69 61 62 6c  irst.  A variabl
1231b 65 2d 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72  e-length integer
1231c 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 72 65   may not be more
1231d 20 74 68 61 6e 20 39 20 62 79 74 65 73 20 6c 6f   than 9 bytes lo
1231e 6e 67 2e 0a 2a 2a 20 41 73 20 61 20 73 70 65 63  ng..** As a spec
1231f 69 61 6c 20 63 61 73 65 2c 20 61 6c 6c 20 38 20  ial case, all 8 
12320 62 79 74 65 73 20 6f 66 20 74 68 65 20 39 74 68  bytes of the 9th
12321 20 62 79 74 65 20 61 72 65 20 75 73 65 64 20 61   byte are used a
12322 73 20 64 61 74 61 2e 20 20 54 68 69 73 0a 2a 2a  s data.  This.**
12323 20 61 6c 6c 6f 77 73 20 61 20 36 34 2d 62 69 74   allows a 64-bit
12324 20 69 6e 74 65 67 65 72 20 74 6f 20 62 65 20 65   integer to be e
12325 6e 63 6f 64 65 64 20 69 6e 20 39 20 62 79 74 65  ncoded in 9 byte
12326 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 30 78 30 30  s..**.**    0x00
12327 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12328 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
12329 78 30 30 30 30 30 30 30 30 0a 2a 2a 20 20 20 20  x00000000.**    
1232a 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20 20  0x7f            
1232b 20 20 20 20 20 20 20 20 20 20 62 65 63 6f 6d 65            become
1232c 73 20 20 30 78 30 30 30 30 30 30 37 66 0a 2a 2a  s  0x0000007f.**
1232d 20 20 20 20 30 78 38 31 20 30 78 30 30 20 20 20      0x81 0x00   
1232e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 65                be
1232f 63 6f 6d 65 73 20 20 30 78 30 30 30 30 30 30 38  comes  0x0000008
12330 30 0a 2a 2a 20 20 20 20 30 78 38 32 20 30 78 30  0.**    0x82 0x0
12331 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0               
12332 20 20 62 65 63 6f 6d 65 73 20 20 30 78 30 30 30    becomes  0x000
12333 30 30 31 30 30 0a 2a 2a 20 20 20 20 30 78 38 30  00100.**    0x80
12334 20 30 78 37 66 20 20 20 20 20 20 20 20 20 20 20   0x7f           
12335 20 20 20 20 20 20 62 65 63 6f 6d 65 73 20 20 30        becomes  0
12336 78 30 30 30 30 30 30 37 66 0a 2a 2a 20 20 20 20  x0000007f.**    
12337 30 78 38 61 20 30 78 39 31 20 30 78 64 31 20 30  0x8a 0x91 0xd1 0
12338 78 61 63 20 30 78 37 38 20 20 62 65 63 6f 6d 65  xac 0x78  become
12339 73 20 20 30 78 31 32 33 34 35 36 37 38 0a 2a 2a  s  0x12345678.**
1233a 20 20 20 20 30 78 38 31 20 30 78 38 31 20 30 78      0x81 0x81 0x
1233b 38 31 20 30 78 38 31 20 30 78 30 31 20 20 62 65  81 0x81 0x01  be
1233c 63 6f 6d 65 73 20 20 30 78 31 30 32 30 34 30 38  comes  0x1020408
1233d 31 0a 2a 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65  1.**.** Variable
1233e 20 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 73   length integers
1233f 20 61 72 65 20 75 73 65 64 20 66 6f 72 20 72 6f   are used for ro
12340 77 69 64 73 20 61 6e 64 20 74 6f 20 68 6f 6c 64  wids and to hold
12341 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 0a 2a   the number of.*
12342 2a 20 62 79 74 65 73 20 6f 66 20 6b 65 79 20 61  * bytes of key a
12343 6e 64 20 64 61 74 61 20 69 6e 20 61 20 62 74 72  nd data in a btr
12344 65 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54  ee cell..**.** T
12345 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20  he content of a 
12346 63 65 6c 6c 20 6c 6f 6f 6b 73 20 6c 69 6b 65 20  cell looks like 
12347 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53  this:.**.**    S
12348 49 5a 45 20 20 20 20 44 45 53 43 52 49 50 54 49  IZE    DESCRIPTI
12349 4f 4e 0a 2a 2a 20 20 20 20 20 20 34 20 20 20 20  ON.**      4    
1234a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20   Page number of 
1234b 74 68 65 20 6c 65 66 74 20 63 68 69 6c 64 2e 20  the left child. 
1234c 4f 6d 69 74 74 65 64 20 69 66 20 6c 65 61 66 20  Omitted if leaf 
1234d 66 6c 61 67 20 69 73 20 73 65 74 2e 0a 2a 2a 20  flag is set..** 
1234e 20 20 20 20 76 61 72 20 20 20 20 4e 75 6d 62 65      var    Numbe
1234f 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20 64 61  r of bytes of da
12350 74 61 2e 20 4f 6d 69 74 74 65 64 20 69 66 20 74  ta. Omitted if t
12351 68 65 20 7a 65 72 6f 64 61 74 61 20 66 6c 61 67  he zerodata flag
12352 20 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20   is set..**     
12353 76 61 72 20 20 20 20 4e 75 6d 62 65 72 20 6f 66  var    Number of
12354 20 62 79 74 65 73 20 6f 66 20 6b 65 79 2e 20 4f   bytes of key. O
12355 72 20 74 68 65 20 6b 65 79 20 69 74 73 65 6c 66  r the key itself
12356 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67 20   if intkey flag 
12357 69 73 20 73 65 74 2e 0a 2a 2a 20 20 20 20 20 20  is set..**      
12358 2a 20 20 20 20 20 50 61 79 6c 6f 61 64 0a 2a 2a  *     Payload.**
12359 20 20 20 20 20 20 34 20 20 20 20 20 46 69 72 73        4     Firs
1235a 74 20 70 61 67 65 20 6f 66 20 74 68 65 20 6f 76  t page of the ov
1235b 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 20 4f  erflow chain.  O
1235c 6d 69 74 74 65 64 20 69 66 20 6e 6f 20 6f 76 65  mitted if no ove
1235d 72 66 6c 6f 77 0a 2a 2a 0a 2a 2a 20 4f 76 65 72  rflow.**.** Over
1235e 66 6c 6f 77 20 70 61 67 65 73 20 66 6f 72 6d 20  flow pages form 
1235f 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 2e 20 20  a linked list.  
12360 45 61 63 68 20 70 61 67 65 20 65 78 63 65 70 74  Each page except
12361 20 74 68 65 20 6c 61 73 74 20 69 73 20 63 6f 6d   the last is com
12362 70 6c 65 74 65 6c 79 0a 2a 2a 20 66 69 6c 6c 65  pletely.** fille
12363 64 20 77 69 74 68 20 64 61 74 61 20 28 70 61 67  d with data (pag
12364 65 73 69 7a 65 20 2d 20 34 20 62 79 74 65 73 29  esize - 4 bytes)
12365 2e 20 20 54 68 65 20 6c 61 73 74 20 70 61 67 65  .  The last page
12366 20 63 61 6e 20 68 61 76 65 20 61 73 20 6c 69 74   can have as lit
12367 74 6c 65 0a 2a 2a 20 61 73 20 31 20 62 79 74 65  tle.** as 1 byte
12368 20 6f 66 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20   of data..**.** 
12369 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
1236a 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
1236b 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
1236c 20 6f 66 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f   of next overflo
1236d 77 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  w page.**      *
1236e 20 20 20 20 20 44 61 74 61 0a 2a 2a 0a 2a 2a 20       Data.**.** 
1236f 46 72 65 65 6c 69 73 74 20 70 61 67 65 73 20 63  Freelist pages c
12370 6f 6d 65 20 69 6e 20 74 77 6f 20 73 75 62 74 79  ome in two subty
12371 70 65 73 3a 20 74 72 75 6e 6b 20 70 61 67 65 73  pes: trunk pages
12372 20 61 6e 64 20 6c 65 61 66 20 70 61 67 65 73 2e   and leaf pages.
12373 20 20 54 68 65 0a 2a 2a 20 66 69 6c 65 20 68 65    The.** file he
12374 61 64 65 72 20 70 6f 69 6e 74 73 20 74 6f 20 74  ader points to t
12375 68 65 20 66 69 72 73 74 20 69 6e 20 61 20 6c 69  he first in a li
12376 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 72 75  nked list of tru
12377 6e 6b 20 70 61 67 65 2e 20 20 45 61 63 68 20 74  nk page.  Each t
12378 72 75 6e 6b 0a 2a 2a 20 70 61 67 65 20 70 6f 69  runk.** page poi
12379 6e 74 73 20 74 6f 20 6d 75 6c 74 69 70 6c 65 20  nts to multiple 
1237a 6c 65 61 66 20 70 61 67 65 73 2e 20 20 54 68 65  leaf pages.  The
1237b 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 6c 65   content of a le
1237c 61 66 20 70 61 67 65 20 69 73 0a 2a 2a 20 75 6e  af page is.** un
1237d 73 70 65 63 69 66 69 65 64 2e 20 20 41 20 74 72  specified.  A tr
1237e 75 6e 6b 20 70 61 67 65 20 6c 6f 6f 6b 73 20 6c  unk page looks l
1237f 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20  ike this:.**.** 
12380 20 20 20 53 49 5a 45 20 20 20 20 44 45 53 43 52     SIZE    DESCR
12381 49 50 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 34  IPTION.**      4
12382 20 20 20 20 20 50 61 67 65 20 6e 75 6d 62 65 72       Page number
12383 20 6f 66 20 6e 65 78 74 20 74 72 75 6e 6b 20 70   of next trunk p
12384 61 67 65 0a 2a 2a 20 20 20 20 20 20 34 20 20 20  age.**      4   
12385 20 20 4e 75 6d 62 65 72 20 6f 66 20 6c 65 61 66    Number of leaf
12386 20 70 6f 69 6e 74 65 72 73 20 6f 6e 20 74 68 69   pointers on thi
12387 73 20 70 61 67 65 0a 2a 2a 20 20 20 20 20 20 2a  s page.**      *
12388 20 20 20 20 20 7a 65 72 6f 20 6f 72 20 6d 6f 72       zero or mor
12389 65 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20  e pages numbers 
1238a 6f 66 20 6c 65 61 76 65 73 0a 2a 2f 0a 0a 2f 2a  of leaves.*/../*
1238b 20 52 6f 75 6e 64 20 75 70 20 61 20 6e 75 6d 62   Round up a numb
1238c 65 72 20 74 6f 20 74 68 65 20 6e 65 78 74 20 6c  er to the next l
1238d 61 72 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f  arger multiple o
1238e 66 20 38 2e 20 20 54 68 69 73 20 69 73 20 75 73  f 8.  This is us
1238f 65 64 0a 2a 2a 20 74 6f 20 66 6f 72 63 65 20 38  ed.** to force 8
12390 2d 62 79 74 65 20 61 6c 69 67 6e 6d 65 6e 74 20  -byte alignment 
12391 6f 6e 20 36 34 2d 62 69 74 20 61 72 63 68 69 74  on 64-bit archit
12392 65 63 74 75 72 65 73 2e 0a 2a 2f 0a 23 64 65 66  ectures..*/.#def
12393 69 6e 65 20 52 4f 55 4e 44 38 28 78 29 20 20 20  ine ROUND8(x)   
12394 28 28 78 2b 37 29 26 7e 37 29 0a 0a 0a 2f 2a 20  ((x+7)&~7).../* 
12395 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61  The following va
12396 6c 75 65 20 69 73 20 74 68 65 20 6d 61 78 69 6d  lue is the maxim
12397 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20 61 73 73  um cell size ass
12398 75 6d 69 6e 67 20 61 20 6d 61 78 69 6d 75 6d 20  uming a maximum 
12399 70 61 67 65 0a 2a 2a 20 73 69 7a 65 20 67 69 76  page.** size giv
1239a 65 20 61 62 6f 76 65 2e 0a 2a 2f 0a 23 64 65 66  e above..*/.#def
1239b 69 6e 65 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45  ine MX_CELL_SIZE
1239c 28 70 42 74 29 20 20 28 70 42 74 2d 3e 70 61 67  (pBt)  (pBt->pag
1239d 65 53 69 7a 65 2d 38 29 0a 0a 2f 2a 20 54 68 65  eSize-8)../* The
1239e 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
1239f 6f 66 20 63 65 6c 6c 73 20 6f 6e 20 61 20 73 69  of cells on a si
123a0 6e 67 6c 65 20 70 61 67 65 20 6f 66 20 74 68 65  ngle page of the
123a1 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69 73   database.  This
123a2 0a 2a 2a 20 61 73 73 75 6d 65 73 20 61 20 6d 69  .** assumes a mi
123a3 6e 69 6d 75 6d 20 63 65 6c 6c 20 73 69 7a 65 20  nimum cell size 
123a4 6f 66 20 36 20 62 79 74 65 73 20 20 28 34 20 62  of 6 bytes  (4 b
123a5 79 74 65 73 20 66 6f 72 20 74 68 65 20 63 65 6c  ytes for the cel
123a6 6c 20 69 74 73 65 6c 66 0a 2a 2a 20 70 6c 75 73  l itself.** plus
123a7 20 32 20 62 79 74 65 73 20 66 6f 72 20 74 68 65   2 bytes for the
123a8 20 69 6e 64 65 78 20 74 6f 20 74 68 65 20 63 65   index to the ce
123a9 6c 6c 20 69 6e 20 74 68 65 20 70 61 67 65 20 68  ll in the page h
123aa 65 61 64 65 72 29 2e 20 20 53 75 63 68 0a 2a 2a  eader).  Such.**
123ab 20 73 6d 61 6c 6c 20 63 65 6c 6c 73 20 77 69 6c   small cells wil
123ac 6c 20 62 65 20 72 61 72 65 2c 20 62 75 74 20 74  l be rare, but t
123ad 68 65 79 20 61 72 65 20 70 6f 73 73 69 62 6c 65  hey are possible
123ae 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4d 58 5f  ..*/.#define MX_
123af 43 45 4c 4c 28 70 42 74 29 20 28 28 70 42 74 2d  CELL(pBt) ((pBt-
123b0 3e 70 61 67 65 53 69 7a 65 2d 38 29 2f 36 29 0a  >pageSize-8)/6).
123b1 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c  ./* Forward decl
123b2 61 72 61 74 69 6f 6e 73 20 2a 2f 0a 74 79 70 65  arations */.type
123b3 64 65 66 20 73 74 72 75 63 74 20 4d 65 6d 50 61  def struct MemPa
123b4 67 65 20 4d 65 6d 50 61 67 65 3b 0a 74 79 70 65  ge MemPage;.type
123b5 64 65 66 20 73 74 72 75 63 74 20 42 74 4c 6f 63  def struct BtLoc
123b6 6b 20 42 74 4c 6f 63 6b 3b 0a 0a 2f 2a 0a 2a 2a  k BtLock;../*.**
123b7 20 54 68 69 73 20 69 73 20 61 20 6d 61 67 69 63   This is a magic
123b8 20 73 74 72 69 6e 67 20 74 68 61 74 20 61 70 70   string that app
123b9 65 61 72 73 20 61 74 20 74 68 65 20 62 65 67 69  ears at the begi
123ba 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72 79 0a 2a  nning of every.*
123bb 2a 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  * SQLite databas
123bc 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 69 64  e in order to id
123bd 65 6e 74 69 66 79 20 74 68 65 20 66 69 6c 65 20  entify the file 
123be 61 73 20 61 20 72 65 61 6c 20 64 61 74 61 62 61  as a real databa
123bf 73 65 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 63 61  se..**.** You ca
123c0 6e 20 63 68 61 6e 67 65 20 74 68 69 73 20 76 61  n change this va
123c1 6c 75 65 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74  lue at compile-t
123c2 69 6d 65 20 62 79 20 73 70 65 63 69 66 79 69 6e  ime by specifyin
123c3 67 20 61 0a 2a 2a 20 2d 44 53 51 4c 49 54 45 5f  g a.** -DSQLITE_
123c4 46 49 4c 45 5f 48 45 41 44 45 52 3d 22 2e 2e 2e  FILE_HEADER="...
123c5 22 20 6f 6e 20 74 68 65 20 63 6f 6d 70 69 6c 65  " on the compile
123c6 72 20 63 6f 6d 6d 61 6e 64 2d 6c 69 6e 65 2e 20  r command-line. 
123c7 20 54 68 65 0a 2a 2a 20 68 65 61 64 65 72 20 6d   The.** header m
123c8 75 73 74 20 62 65 20 65 78 61 63 74 6c 79 20 31  ust be exactly 1
123c9 36 20 62 79 74 65 73 20 69 6e 63 6c 75 64 69 6e  6 bytes includin
123ca 67 20 74 68 65 20 7a 65 72 6f 2d 74 65 72 6d 69  g the zero-termi
123cb 6e 61 74 6f 72 20 73 6f 0a 2a 2a 20 74 68 65 20  nator so.** the 
123cc 73 74 72 69 6e 67 20 69 74 73 65 6c 66 20 73 68  string itself sh
123cd 6f 75 6c 64 20 62 65 20 31 35 20 63 68 61 72 61  ould be 15 chara
123ce 63 74 65 72 73 20 6c 6f 6e 67 2e 20 20 49 66 20  cters long.  If 
123cf 79 6f 75 20 63 68 61 6e 67 65 0a 2a 2a 20 74 68  you change.** th
123d0 65 20 68 65 61 64 65 72 2c 20 74 68 65 6e 20 79  e header, then y
123d1 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69 62 72 61  our custom libra
123d2 72 79 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20 61  ry will not be a
123d3 62 6c 65 20 74 6f 20 72 65 61 64 20 0a 2a 2a 20  ble to read .** 
123d4 64 61 74 61 62 61 73 65 73 20 67 65 6e 65 72 61  databases genera
123d5 74 65 64 20 62 79 20 74 68 65 20 73 74 61 6e 64  ted by the stand
123d6 61 72 64 20 74 6f 6f 6c 73 20 61 6e 64 20 74 68  ard tools and th
123d7 65 20 73 74 61 6e 64 61 72 64 20 74 6f 6f 6c 73  e standard tools
123d8 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 62 65 20  .** will not be 
123d9 61 62 6c 65 20 74 6f 20 72 65 61 64 20 64 61 74  able to read dat
123da 61 62 61 73 65 73 20 63 72 65 61 74 65 64 20 62  abases created b
123db 79 20 79 6f 75 72 20 63 75 73 74 6f 6d 20 6c 69  y your custom li
123dc 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 6e 64 65  brary..*/.#ifnde
123dd 66 20 53 51 4c 49 54 45 5f 46 49 4c 45 5f 48 45  f SQLITE_FILE_HE
123de 41 44 45 52 20 2f 2a 20 31 32 33 34 35 36 37 38  ADER /* 12345678
123df 39 20 31 32 33 34 35 36 20 2a 2f 0a 23 20 20 64  9 123456 */.#  d
123e0 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46 49 4c  efine SQLITE_FIL
123e1 45 5f 48 45 41 44 45 52 20 22 53 51 4c 69 74 65  E_HEADER "SQLite
123e2 20 66 6f 72 6d 61 74 20 33 22 0a 23 65 6e 64 69   format 3".#endi
123e3 66 0a 0a 2f 2a 0a 2a 2a 20 50 61 67 65 20 74 79  f../*.** Page ty
123e4 70 65 20 66 6c 61 67 73 2e 20 20 41 6e 20 4f 52  pe flags.  An OR
123e5 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20 6f  ed combination o
123e6 66 20 74 68 65 73 65 20 66 6c 61 67 73 20 61 70  f these flags ap
123e7 70 65 61 72 20 61 73 20 74 68 65 0a 2a 2a 20 66  pear as the.** f
123e8 69 72 73 74 20 62 79 74 65 20 6f 66 20 6f 6e 2d  irst byte of on-
123e9 64 69 73 6b 20 69 6d 61 67 65 20 6f 66 20 65 76  disk image of ev
123ea 65 72 79 20 42 54 72 65 65 20 70 61 67 65 2e 0a  ery BTree page..
123eb 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 46 5f 49  */.#define PTF_I
123ec 4e 54 4b 45 59 20 20 20 20 30 78 30 31 0a 23 64  NTKEY    0x01.#d
123ed 65 66 69 6e 65 20 50 54 46 5f 5a 45 52 4f 44 41  efine PTF_ZERODA
123ee 54 41 20 20 30 78 30 32 0a 23 64 65 66 69 6e 65  TA  0x02.#define
123ef 20 50 54 46 5f 4c 45 41 46 44 41 54 41 20 20 30   PTF_LEAFDATA  0
123f0 78 30 34 0a 23 64 65 66 69 6e 65 20 50 54 46 5f  x04.#define PTF_
123f1 4c 45 41 46 20 20 20 20 20 20 30 78 30 38 0a 0a  LEAF      0x08..
123f2 2f 2a 0a 2a 2a 20 41 73 20 65 61 63 68 20 70 61  /*.** As each pa
123f3 67 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ge of the file i
123f4 73 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20 6d 65  s loaded into me
123f5 6d 6f 72 79 2c 20 61 6e 20 69 6e 73 74 61 6e 63  mory, an instanc
123f6 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
123f7 6e 67 0a 2a 2a 20 73 74 72 75 63 74 75 72 65 20  ng.** structure 
123f8 69 73 20 61 70 70 65 6e 64 65 64 20 61 6e 64 20  is appended and 
123f9 69 6e 69 74 69 61 6c 69 7a 65 64 20 74 6f 20 7a  initialized to z
123fa 65 72 6f 2e 20 20 54 68 69 73 20 73 74 72 75 63  ero.  This struc
123fb 74 75 72 65 20 73 74 6f 72 65 73 0a 2a 2a 20 69  ture stores.** i
123fc 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74  nformation about
123fd 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20 69   the page that i
123fe 73 20 64 65 63 6f 64 65 64 20 66 72 6f 6d 20 74  s decoded from t
123ff 68 65 20 72 61 77 20 66 69 6c 65 20 70 61 67 65  he raw file page
12400 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 50 61 72  ..**.** The pPar
12401 65 6e 74 20 66 69 65 6c 64 20 70 6f 69 6e 74 73  ent field points
12402 20 62 61 63 6b 20 74 6f 20 74 68 65 20 70 61 72   back to the par
12403 65 6e 74 20 70 61 67 65 2e 20 20 54 68 69 73 20  ent page.  This 
12404 61 6c 6c 6f 77 73 20 75 73 20 74 6f 0a 2a 2a 20  allows us to.** 
12405 77 61 6c 6b 20 75 70 20 74 68 65 20 42 54 72 65  walk up the BTre
12406 65 20 66 72 6f 6d 20 61 6e 79 20 6c 65 61 66 20  e from any leaf 
12407 74 6f 20 74 68 65 20 72 6f 6f 74 2e 20 20 43 61  to the root.  Ca
12408 72 65 20 6d 75 73 74 20 62 65 20 74 61 6b 65 6e  re must be taken
12409 20 74 6f 0a 2a 2a 20 75 6e 72 65 66 28 29 20 74   to.** unref() t
1240a 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 70  he parent page p
1240b 6f 69 6e 74 65 72 20 77 68 65 6e 20 74 68 69 73  ointer when this
1240c 20 70 61 67 65 20 69 73 20 6e 6f 20 6c 6f 6e 67   page is no long
1240d 65 72 20 72 65 66 65 72 65 6e 63 65 64 2e 0a 2a  er referenced..*
1240e 2a 20 54 68 65 20 70 61 67 65 44 65 73 74 72 75  * The pageDestru
1240f 63 74 6f 72 28 29 20 72 6f 75 74 69 6e 65 20 68  ctor() routine h
12410 61 6e 64 6c 65 73 20 74 68 61 74 20 63 68 6f 72  andles that chor
12411 65 2e 0a 2a 2a 0a 2a 2a 20 41 63 63 65 73 73 20  e..**.** Access 
12412 74 6f 20 61 6c 6c 20 66 69 65 6c 64 73 20 6f 66  to all fields of
12413 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
12414 69 73 20 63 6f 6e 74 72 6f 6c 6c 65 64 20 62 79  is controlled by
12415 20 74 68 65 20 6d 75 74 65 78 0a 2a 2a 20 73 74   the mutex.** st
12416 6f 72 65 64 20 69 6e 20 4d 65 6d 50 61 67 65 2e  ored in MemPage.
12417 70 42 74 2d 3e 6d 75 74 65 78 2e 0a 2a 2f 0a 73  pBt->mutex..*/.s
12418 74 72 75 63 74 20 4d 65 6d 50 61 67 65 20 7b 0a  truct MemPage {.
12419 20 20 75 38 20 69 73 49 6e 69 74 3b 20 20 20 20    u8 isInit;    
1241a 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69         /* True i
1241b 66 20 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 69  f previously ini
1241c 74 69 61 6c 69 7a 65 64 2e 20 4d 55 53 54 20 42  tialized. MUST B
1241d 45 20 46 49 52 53 54 21 20 2a 2f 0a 20 20 75 38  E FIRST! */.  u8
1241e 20 6e 4f 76 65 72 66 6c 6f 77 3b 20 20 20 20 20   nOverflow;     
1241f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
12420 6f 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 62 6f  overflow cell bo
12421 64 69 65 73 20 69 6e 20 61 43 65 6c 6c 5b 5d 20  dies in aCell[] 
12422 2a 2f 0a 20 20 75 38 20 69 6e 74 4b 65 79 3b 20  */.  u8 intKey; 
12423 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75            /* Tru
12424 65 20 69 66 20 69 6e 74 6b 65 79 20 66 6c 61 67  e if intkey flag
12425 20 69 73 20 73 65 74 20 2a 2f 0a 20 20 75 38 20   is set */.  u8 
12426 6c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20 20  leaf;           
12427 20 20 2f 2a 20 54 72 75 65 20 69 66 20 6c 65 61    /* True if lea
12428 66 20 66 6c 61 67 20 69 73 20 73 65 74 20 2a 2f  f flag is set */
12429 0a 20 20 75 38 20 68 61 73 44 61 74 61 3b 20 20  .  u8 hasData;  
1242a 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1242b 69 66 20 74 68 69 73 20 70 61 67 65 20 73 74 6f  if this page sto
1242c 72 65 73 20 64 61 74 61 20 2a 2f 0a 20 20 75 38  res data */.  u8
1242d 20 68 64 72 4f 66 66 73 65 74 3b 20 20 20 20 20   hdrOffset;     
1242e 20 20 20 2f 2a 20 31 30 30 20 66 6f 72 20 70 61     /* 100 for pa
1242f 67 65 20 31 2e 20 20 30 20 6f 74 68 65 72 77 69  ge 1.  0 otherwi
12430 73 65 20 2a 2f 0a 20 20 75 38 20 63 68 69 6c 64  se */.  u8 child
12431 50 74 72 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20  PtrSize;     /* 
12432 30 20 69 66 20 6c 65 61 66 3d 3d 31 2e 20 20 34  0 if leaf==1.  4
12433 20 69 66 20 6c 65 61 66 3d 3d 30 20 2a 2f 0a 20   if leaf==0 */. 
12434 20 75 31 36 20 6d 61 78 4c 6f 63 61 6c 3b 20 20   u16 maxLocal;  
12435 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
12436 20 42 74 53 68 61 72 65 64 2e 6d 61 78 4c 6f 63   BtShared.maxLoc
12437 61 6c 20 6f 72 20 42 74 53 68 61 72 65 64 2e 6d  al or BtShared.m
12438 61 78 4c 65 61 66 20 2a 2f 0a 20 20 75 31 36 20  axLeaf */.  u16 
12439 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  minLocal;       
1243a 20 2f 2a 20 43 6f 70 79 20 6f 66 20 42 74 53 68   /* Copy of BtSh
1243b 61 72 65 64 2e 6d 69 6e 4c 6f 63 61 6c 20 6f 72  ared.minLocal or
1243c 20 42 74 53 68 61 72 65 64 2e 6d 69 6e 4c 65 61   BtShared.minLea
1243d 66 20 2a 2f 0a 20 20 75 31 36 20 63 65 6c 6c 4f  f */.  u16 cellO
1243e 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20 49  ffset;      /* I
1243f 6e 64 65 78 20 69 6e 20 61 44 61 74 61 20 6f 66  ndex in aData of
12440 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f 69 6e   first cell poin
12441 74 65 72 20 2a 2f 0a 20 20 75 31 36 20 6e 46 72  ter */.  u16 nFr
12442 65 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ee;           /*
12443 20 4e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20   Number of free 
12444 62 79 74 65 73 20 6f 6e 20 74 68 65 20 70 61 67  bytes on the pag
12445 65 20 2a 2f 0a 20 20 75 31 36 20 6e 43 65 6c 6c  e */.  u16 nCell
12446 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
12447 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 6f  umber of cells o
12448 6e 20 74 68 69 73 20 70 61 67 65 2c 20 6c 6f 63  n this page, loc
12449 61 6c 20 61 6e 64 20 6f 76 66 6c 20 2a 2f 0a 20  al and ovfl */. 
1244a 20 75 31 36 20 6d 61 73 6b 50 61 67 65 3b 20 20   u16 maskPage;  
1244b 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20 66 6f        /* Mask fo
1244c 72 20 70 61 67 65 20 6f 66 66 73 65 74 20 2a 2f  r page offset */
1244d 0a 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43  .  struct _OvflC
1244e 65 6c 6c 20 7b 20 20 20 2f 2a 20 43 65 6c 6c 73  ell {   /* Cells
1244f 20 74 68 61 74 20 77 69 6c 6c 20 6e 6f 74 20 66   that will not f
12450 69 74 20 6f 6e 20 61 44 61 74 61 5b 5d 20 2a 2f  it on aData[] */
12451 0a 20 20 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20  .    u8 *pCell; 
12452 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
12453 74 65 72 73 20 74 6f 20 74 68 65 20 62 6f 64 79  ters to the body
12454 20 6f 66 20 74 68 65 20 6f 76 65 72 66 6c 6f 77   of the overflow
12455 20 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 75 31 36   cell */.    u16
12456 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20 20   idx;           
12457 20 2f 2a 20 49 6e 73 65 72 74 20 74 68 69 73 20   /* Insert this 
12458 63 65 6c 6c 20 62 65 66 6f 72 65 20 69 64 78 2d  cell before idx-
12459 74 68 20 6e 6f 6e 2d 6f 76 65 72 66 6c 6f 77 20  th non-overflow 
1245a 63 65 6c 6c 20 2a 2f 0a 20 20 7d 20 61 4f 76 66  cell */.  } aOvf
1245b 6c 5b 35 5d 3b 0a 20 20 42 74 53 68 61 72 65 64  l[5];.  BtShared
1245c 20 2a 70 42 74 3b 20 20 20 20 20 20 20 2f 2a 20   *pBt;       /* 
1245d 50 6f 69 6e 74 65 72 20 74 6f 20 42 74 53 68 61  Pointer to BtSha
1245e 72 65 64 20 74 68 61 74 20 74 68 69 73 20 70 61  red that this pa
1245f 67 65 20 69 73 20 70 61 72 74 20 6f 66 20 2a 2f  ge is part of */
12460 0a 20 20 75 38 20 2a 61 44 61 74 61 3b 20 20 20  .  u8 *aData;   
12461 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
12462 65 72 20 74 6f 20 64 69 73 6b 20 69 6d 61 67 65  er to disk image
12463 20 6f 66 20 74 68 65 20 70 61 67 65 20 64 61 74   of the page dat
12464 61 20 2a 2f 0a 20 20 44 62 50 61 67 65 20 2a 70  a */.  DbPage *p
12465 44 62 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50  DbPage;     /* P
12466 61 67 65 72 20 70 61 67 65 20 68 61 6e 64 6c 65  ager page handle
12467 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b   */.  Pgno pgno;
12468 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
12469 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  ge number for th
1246a 69 73 20 70 61 67 65 20 2a 2f 0a 7d 3b 0a 0a 2f  is page */.};../
1246b 2a 0a 2a 2a 20 54 68 65 20 69 6e 2d 6d 65 6d 6f  *.** The in-memo
1246c 72 79 20 69 6d 61 67 65 20 6f 66 20 61 20 64 69  ry image of a di
1246d 73 6b 20 70 61 67 65 20 68 61 73 20 74 68 65 20  sk page has the 
1246e 61 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d  auxiliary inform
1246f 61 74 69 6f 6e 20 61 70 70 65 6e 64 65 64 0a 2a  ation appended.*
12470 2a 20 74 6f 20 74 68 65 20 65 6e 64 2e 20 20 45  * to the end.  E
12471 58 54 52 41 5f 53 49 5a 45 20 69 73 20 74 68 65  XTRA_SIZE is the
12472 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
12473 20 6f 66 20 73 70 61 63 65 20 6e 65 65 64 65 64   of space needed
12474 20 74 6f 20 68 6f 6c 64 0a 2a 2a 20 74 68 61 74   to hold.** that
12475 20 65 78 74 72 61 20 69 6e 66 6f 72 6d 61 74 69   extra informati
12476 6f 6e 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 45  on..*/.#define E
12477 58 54 52 41 5f 53 49 5a 45 20 73 69 7a 65 6f 66  XTRA_SIZE sizeof
12478 28 4d 65 6d 50 61 67 65 29 0a 0a 2f 2a 20 41 20  (MemPage)../* A 
12479 42 74 72 65 65 20 68 61 6e 64 6c 65 0a 2a 2a 0a  Btree handle.**.
1247a 2a 2a 20 41 20 64 61 74 61 62 61 73 65 20 63 6f  ** A database co
1247b 6e 6e 65 63 74 69 6f 6e 20 63 6f 6e 74 61 69 6e  nnection contain
1247c 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1247d 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 2a 2a  n instance of.**
1247e 20 74 68 69 73 20 6f 62 6a 65 63 74 20 66 6f 72   this object for
1247f 20 65 76 65 72 79 20 64 61 74 61 62 61 73 65 20   every database 
12480 66 69 6c 65 20 74 68 61 74 20 69 74 20 68 61 73  file that it has
12481 20 6f 70 65 6e 2e 20 20 54 68 69 73 20 73 74 72   open.  This str
12482 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 6f 70 61  ucture.** is opa
12483 71 75 65 20 74 6f 20 74 68 65 20 64 61 74 61 62  que to the datab
12484 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20  ase connection. 
12485 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63 6f   The database co
12486 6e 6e 65 63 74 69 6f 6e 20 63 61 6e 6e 6f 74 0a  nnection cannot.
12487 2a 2a 20 73 65 65 20 74 68 65 20 69 6e 74 65 72  ** see the inter
12488 6e 61 6c 73 20 6f 66 20 74 68 69 73 20 73 74 72  nals of this str
12489 75 63 74 75 72 65 20 61 6e 64 20 6f 6e 6c 79 20  ucture and only 
1248a 64 65 61 6c 73 20 77 69 74 68 20 70 6f 69 6e 74  deals with point
1248b 65 72 73 20 74 6f 0a 2a 2a 20 74 68 69 73 20 73  ers to.** this s
1248c 74 72 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20  tructure..**.** 
1248d 46 6f 72 20 73 6f 6d 65 20 64 61 74 61 62 61 73  For some databas
1248e 65 20 66 69 6c 65 73 2c 20 74 68 65 20 73 61 6d  e files, the sam
1248f 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 64 61 74  e underlying dat
12490 61 62 61 73 65 20 63 61 63 68 65 20 6d 69 67 68  abase cache migh
12491 74 20 62 65 20 0a 2a 2a 20 73 68 61 72 65 64 20  t be .** shared 
12492 62 65 74 77 65 65 6e 20 6d 75 6c 74 69 70 6c 65  between multiple
12493 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 49   connections.  I
12494 6e 20 74 68 61 74 20 63 61 73 65 2c 20 65 61 63  n that case, eac
12495 68 20 63 6f 6e 74 65 63 74 69 6f 6e 0a 2a 2a 20  h contection.** 
12496 68 61 73 20 69 74 20 6f 77 6e 20 70 6f 69 6e 74  has it own point
12497 65 72 20 74 6f 20 74 68 69 73 20 6f 62 6a 65 63  er to this objec
12498 74 2e 20 20 42 75 74 20 65 61 63 68 20 69 6e 73  t.  But each ins
12499 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 6f 62  tance of this ob
1249a 6a 65 63 74 0a 2a 2a 20 70 6f 69 6e 74 73 20 74  ject.** points t
1249b 6f 20 74 68 65 20 73 61 6d 65 20 42 74 53 68 61  o the same BtSha
1249c 72 65 64 20 6f 62 6a 65 63 74 2e 20 20 54 68 65  red object.  The
1249d 20 64 61 74 61 62 61 73 65 20 63 61 63 68 65 20   database cache 
1249e 61 6e 64 20 74 68 65 0a 2a 2a 20 73 63 68 65 6d  and the.** schem
1249f 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
124a0 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  h the database f
124a1 69 6c 65 20 61 72 65 20 61 6c 6c 20 63 6f 6e 74  ile are all cont
124a2 61 69 6e 65 64 20 77 69 74 68 69 6e 0a 2a 2a 20  ained within.** 
124a3 74 68 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a  the BtShared obj
124a4 65 63 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20 66  ect..**.** All f
124a5 69 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74  ields in this st
124a6 72 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65  ructure are acce
124a7 73 73 65 64 20 75 6e 64 65 72 20 73 71 6c 69 74  ssed under sqlit
124a8 65 33 2e 6d 75 74 65 78 2e 0a 2a 2a 20 54 68 65  e3.mutex..** The
124a9 20 70 42 74 20 70 6f 69 6e 74 65 72 20 69 74 73   pBt pointer its
124aa 65 6c 66 20 6d 61 79 20 6e 6f 74 20 62 65 20 63  elf may not be c
124ab 68 61 6e 67 65 64 20 77 68 69 6c 65 20 74 68 65  hanged while the
124ac 72 65 20 65 78 69 73 74 73 20 63 75 72 73 6f 72  re exists cursor
124ad 73 20 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65 66  s .** in the ref
124ae 65 72 65 6e 63 65 64 20 42 74 53 68 61 72 65 64  erenced BtShared
124af 20 74 68 61 74 20 70 6f 69 6e 74 20 62 61 63 6b   that point back
124b0 20 74 6f 20 74 68 69 73 20 42 74 72 65 65 20 73   to this Btree s
124b1 69 6e 63 65 20 74 68 6f 73 65 0a 2a 2a 20 63 75  ince those.** cu
124b2 72 73 6f 72 73 20 68 61 76 65 20 74 6f 20 64 6f  rsors have to do
124b3 20 67 6f 20 74 68 72 6f 75 67 68 20 74 68 69 73   go through this
124b4 20 42 74 72 65 65 20 74 6f 20 66 69 6e 64 20 74   Btree to find t
124b5 68 65 69 72 20 42 74 53 68 61 72 65 64 20 61 6e  heir BtShared an
124b6 64 0a 2a 2a 20 74 68 65 79 20 6f 66 74 65 6e 20  d.** they often 
124b7 64 6f 20 73 6f 20 77 69 74 68 6f 75 74 20 68 6f  do so without ho
124b8 6c 64 69 6e 67 20 73 71 6c 69 74 65 33 2e 6d 75  lding sqlite3.mu
124b9 74 65 78 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 42  tex..*/.struct B
124ba 74 72 65 65 20 7b 0a 20 20 73 71 6c 69 74 65 33  tree {.  sqlite3
124bb 20 2a 64 62 3b 20 20 20 20 20 20 20 2f 2a 20 54   *db;       /* T
124bc 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
124bd 65 63 74 69 6f 6e 20 68 6f 6c 64 69 6e 67 20 74  ection holding t
124be 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20 20 42  his btree */.  B
124bf 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20  tShared *pBt;   
124c0 20 20 2f 2a 20 53 68 61 72 61 62 6c 65 20 63 6f    /* Sharable co
124c1 6e 74 65 6e 74 20 6f 66 20 74 68 69 73 20 62 74  ntent of this bt
124c2 72 65 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72  ree */.  u8 inTr
124c3 61 6e 73 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ans;        /* T
124c4 52 41 4e 53 5f 4e 4f 4e 45 2c 20 54 52 41 4e 53  RANS_NONE, TRANS
124c5 5f 52 45 41 44 20 6f 72 20 54 52 41 4e 53 5f 57  _READ or TRANS_W
124c6 52 49 54 45 20 2a 2f 0a 20 20 75 38 20 73 68 61  RITE */.  u8 sha
124c7 72 61 62 6c 65 3b 20 20 20 20 20 20 20 2f 2a 20  rable;       /* 
124c8 54 72 75 65 20 69 66 20 77 65 20 63 61 6e 20 73  True if we can s
124c9 68 61 72 65 20 70 42 74 20 77 69 74 68 20 61 6e  hare pBt with an
124ca 6f 74 68 65 72 20 64 62 20 2a 2f 0a 20 20 75 38  other db */.  u8
124cb 20 6c 6f 63 6b 65 64 3b 20 20 20 20 20 20 20 20   locked;        
124cc 20 2f 2a 20 54 72 75 65 20 69 66 20 64 62 20 63   /* True if db c
124cd 75 72 72 65 6e 74 6c 79 20 68 61 73 20 70 42 74  urrently has pBt
124ce 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69 6e 74   locked */.  int
124cf 20 77 61 6e 74 54 6f 4c 6f 63 6b 3b 20 20 20 20   wantToLock;    
124d0 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65 73  /* Number of nes
124d1 74 65 64 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c  ted calls to sql
124d2 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 29  ite3BtreeEnter()
124d3 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 70 4e 65   */.  Btree *pNe
124d4 78 74 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74  xt;      /* List
124d5 20 6f 66 20 6f 74 68 65 72 20 73 68 61 72 61 62   of other sharab
124d6 6c 65 20 42 74 72 65 65 73 20 66 72 6f 6d 20 74  le Btrees from t
124d7 68 65 20 73 61 6d 65 20 64 62 20 2a 2f 0a 20 20  he same db */.  
124d8 42 74 72 65 65 20 2a 70 50 72 65 76 3b 20 20 20  Btree *pPrev;   
124d9 20 20 20 2f 2a 20 42 61 63 6b 20 70 6f 69 6e 74     /* Back point
124da 65 72 20 6f 66 20 74 68 65 20 73 61 6d 65 20 6c  er of the same l
124db 69 73 74 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  ist */.};../*.**
124dc 20 42 74 72 65 65 2e 69 6e 54 72 61 6e 73 20 6d   Btree.inTrans m
124dd 61 79 20 74 61 6b 65 20 6f 6e 65 20 6f 66 20 74  ay take one of t
124de 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c  he following val
124df 75 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ues..**.** If th
124e0 65 20 73 68 61 72 65 64 2d 64 61 74 61 20 65 78  e shared-data ex
124e1 74 65 6e 73 69 6f 6e 20 69 73 20 65 6e 61 62 6c  tension is enabl
124e2 65 64 2c 20 74 68 65 72 65 20 6d 61 79 20 62 65  ed, there may be
124e3 20 6d 75 6c 74 69 70 6c 65 20 75 73 65 72 73 0a   multiple users.
124e4 2a 2a 20 6f 66 20 74 68 65 20 42 74 72 65 65 20  ** of the Btree 
124e5 73 74 72 75 63 74 75 72 65 2e 20 41 74 20 6d 6f  structure. At mo
124e6 73 74 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20  st one of these 
124e7 6d 61 79 20 6f 70 65 6e 20 61 20 77 72 69 74 65  may open a write
124e8 20 74 72 61 6e 73 61 63 74 69 6f 6e 2c 0a 2a 2a   transaction,.**
124e9 20 62 75 74 20 61 6e 79 20 6e 75 6d 62 65 72 20   but any number 
124ea 6d 61 79 20 68 61 76 65 20 61 63 74 69 76 65 20  may have active 
124eb 72 65 61 64 20 74 72 61 6e 73 61 63 74 69 6f 6e  read transaction
124ec 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 54 52  s..*/.#define TR
124ed 41 4e 53 5f 4e 4f 4e 45 20 20 30 0a 23 64 65 66  ANS_NONE  0.#def
124ee 69 6e 65 20 54 52 41 4e 53 5f 52 45 41 44 20 20  ine TRANS_READ  
124ef 31 0a 23 64 65 66 69 6e 65 20 54 52 41 4e 53 5f  1.#define TRANS_
124f0 57 52 49 54 45 20 32 0a 0a 2f 2a 0a 2a 2a 20 41  WRITE 2../*.** A
124f1 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68  n instance of th
124f2 69 73 20 6f 62 6a 65 63 74 20 72 65 70 72 65 73  is object repres
124f3 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 64 61  ents a single da
124f4 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 20  tabase file..** 
124f5 0a 2a 2a 20 41 20 73 69 6e 67 6c 65 20 64 61 74  .** A single dat
124f6 61 62 61 73 65 20 66 69 6c 65 20 63 61 6e 20 62  abase file can b
124f7 65 20 69 6e 20 75 73 65 20 61 73 20 74 68 65 20  e in use as the 
124f8 73 61 6d 65 20 74 69 6d 65 20 62 79 20 74 77 6f  same time by two
124f9 0a 2a 2a 20 6f 72 20 6d 6f 72 65 20 64 61 74 61  .** or more data
124fa 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73  base connections
124fb 2e 20 20 57 68 65 6e 20 74 77 6f 20 6f 72 20 6d  .  When two or m
124fc 6f 72 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20  ore connections 
124fd 61 72 65 0a 2a 2a 20 73 68 61 72 69 6e 67 20 74  are.** sharing t
124fe 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
124ff 20 66 69 6c 65 2c 20 65 61 63 68 20 63 6f 6e 6e   file, each conn
12500 65 63 74 69 6f 6e 20 68 61 73 20 69 74 20 6f 77  ection has it ow
12501 6e 0a 2a 2a 20 70 72 69 76 61 74 65 20 42 74 72  n.** private Btr
12502 65 65 20 6f 62 6a 65 63 74 20 66 6f 72 20 74 68  ee object for th
12503 65 20 66 69 6c 65 20 61 6e 64 20 65 61 63 68 20  e file and each 
12504 6f 66 20 74 68 6f 73 65 20 42 74 72 65 65 73 20  of those Btrees 
12505 70 6f 69 6e 74 73 0a 2a 2a 20 74 6f 20 74 68 69  points.** to thi
12506 73 20 6f 6e 65 20 42 74 53 68 61 72 65 64 20 6f  s one BtShared o
12507 62 6a 65 63 74 2e 20 20 42 74 53 68 61 72 65 64  bject.  BtShared
12508 2e 6e 52 65 66 20 69 73 20 74 68 65 20 6e 75 6d  .nRef is the num
12509 62 65 72 20 6f 66 0a 2a 2a 20 63 6f 6e 6e 65 63  ber of.** connec
1250a 74 69 6f 6e 73 20 63 75 72 72 65 6e 74 6c 79 20  tions currently 
1250b 73 68 61 72 69 6e 67 20 74 68 69 73 20 64 61 74  sharing this dat
1250c 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a  abase file..**.*
1250d 2a 20 46 69 65 6c 64 73 20 69 6e 20 74 68 69 73  * Fields in this
1250e 20 73 74 72 75 63 74 75 72 65 20 61 72 65 20 61   structure are a
1250f 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68  ccessed under th
12510 65 20 42 74 53 68 61 72 65 64 2e 6d 75 74 65 78  e BtShared.mutex
12511 0a 2a 2a 20 6d 75 74 65 78 2c 20 65 78 63 65 70  .** mutex, excep
12512 74 20 66 6f 72 20 6e 52 65 66 20 61 6e 64 20 70  t for nRef and p
12513 4e 65 78 74 20 77 68 69 63 68 20 61 72 65 20 61  Next which are a
12514 63 63 65 73 73 65 64 20 75 6e 64 65 72 20 74 68  ccessed under th
12515 65 0a 2a 2a 20 67 6c 6f 62 61 6c 20 53 51 4c 49  e.** global SQLI
12516 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f  TE_MUTEX_STATIC_
12517 4d 41 53 54 45 52 20 6d 75 74 65 78 2e 20 20 54  MASTER mutex.  T
12518 68 65 20 70 50 61 67 65 72 20 66 69 65 6c 64 0a  he pPager field.
12519 2a 2a 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f  ** may not be mo
1251a 64 69 66 69 65 64 20 6f 6e 63 65 20 69 74 20 69  dified once it i
1251b 73 20 69 6e 69 74 69 61 6c 6c 79 20 73 65 74 20  s initially set 
1251c 61 73 20 6c 6f 6e 67 20 61 73 20 6e 52 65 66 3e  as long as nRef>
1251d 30 2e 0a 2a 2a 20 54 68 65 20 70 53 63 68 65 6d  0..** The pSchem
1251e 61 20 66 69 65 6c 64 20 6d 61 79 20 62 65 20 73  a field may be s
1251f 65 74 20 6f 6e 63 65 20 75 6e 64 65 72 20 42 74  et once under Bt
12520 53 68 61 72 65 64 2e 6d 75 74 65 78 20 61 6e 64  Shared.mutex and
12521 0a 2a 2a 20 74 68 65 72 65 61 66 74 65 72 20 69  .** thereafter i
12522 73 20 75 6e 63 68 61 6e 67 65 64 20 61 73 20 6c  s unchanged as l
12523 6f 6e 67 20 61 73 20 6e 52 65 66 3e 30 2e 0a 2a  ong as nRef>0..*
12524 2f 0a 73 74 72 75 63 74 20 42 74 53 68 61 72 65  /.struct BtShare
12525 64 20 7b 0a 20 20 50 61 67 65 72 20 2a 70 50 61  d {.  Pager *pPa
12526 67 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  ger;        /* T
12527 68 65 20 70 61 67 65 20 63 61 63 68 65 20 2a 2f  he page cache */
12528 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20  .  sqlite3 *db; 
12529 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
1252a 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
1252b 63 75 72 72 65 6e 74 6c 79 20 75 73 69 6e 67 20  currently using 
1252c 74 68 69 73 20 42 74 72 65 65 20 2a 2f 0a 20 20  this Btree */.  
1252d 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  BtCursor *pCurso
1252e 72 3b 20 20 20 20 2f 2a 20 41 20 6c 69 73 74 20  r;    /* A list 
1252f 6f 66 20 61 6c 6c 20 6f 70 65 6e 20 63 75 72 73  of all open curs
12530 6f 72 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  ors */.  MemPage
12531 20 2a 70 50 61 67 65 31 3b 20 20 20 20 20 20 2f   *pPage1;      /
12532 2a 20 46 69 72 73 74 20 70 61 67 65 20 6f 66 20  * First page of 
12533 74 68 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a  the database */.
12534 20 20 75 38 20 69 6e 53 74 6d 74 3b 20 20 20 20    u8 inStmt;    
12535 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
12536 69 66 20 77 65 20 61 72 65 20 69 6e 20 61 20 73  if we are in a s
12537 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61 6e  tatement subtran
12538 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 75 38 20  saction */.  u8 
12539 72 65 61 64 4f 6e 6c 79 3b 20 20 20 20 20 20 20  readOnly;       
1253a 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
1253b 65 20 75 6e 64 65 72 6c 79 69 6e 67 20 66 69 6c  e underlying fil
1253c 65 20 69 73 20 72 65 61 64 6f 6e 6c 79 20 2a 2f  e is readonly */
1253d 0a 20 20 75 38 20 70 61 67 65 53 69 7a 65 46 69  .  u8 pageSizeFi
1253e 78 65 64 3b 20 20 20 20 20 2f 2a 20 54 72 75 65  xed;     /* True
1253f 20 69 66 20 74 68 65 20 70 61 67 65 20 73 69 7a   if the page siz
12540 65 20 63 61 6e 20 6e 6f 20 6c 6f 6e 67 65 72 20  e can no longer 
12541 62 65 20 63 68 61 6e 67 65 64 20 2a 2f 0a 23 69  be changed */.#i
12542 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
12543 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 75  T_AUTOVACUUM.  u
12544 38 20 61 75 74 6f 56 61 63 75 75 6d 3b 20 20 20  8 autoVacuum;   
12545 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
12546 61 75 74 6f 2d 76 61 63 75 75 6d 20 69 73 20 65  auto-vacuum is e
12547 6e 61 62 6c 65 64 20 2a 2f 0a 20 20 75 38 20 69  nabled */.  u8 i
12548 6e 63 72 56 61 63 75 75 6d 3b 20 20 20 20 20 20  ncrVacuum;      
12549 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 63    /* True if inc
1254a 72 2d 76 61 63 75 75 6d 20 69 73 20 65 6e 61 62  r-vacuum is enab
1254b 6c 65 64 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 54  led */.  Pgno nT
1254c 72 75 6e 63 3b 20 20 20 20 20 20 20 20 20 20 2f  runc;          /
1254d 2a 20 4e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68  * Non-zero if th
1254e 65 20 64 62 20 77 69 6c 6c 20 62 65 20 74 72 75  e db will be tru
1254f 6e 63 61 74 65 64 20 28 69 6e 63 72 20 76 61 63  ncated (incr vac
12550 75 75 6d 29 20 2a 2f 0a 23 65 6e 64 69 66 0a 20  uum) */.#endif. 
12551 20 75 31 36 20 70 61 67 65 53 69 7a 65 3b 20 20   u16 pageSize;  
12552 20 20 20 20 20 20 20 2f 2a 20 54 6f 74 61 6c 20         /* Total 
12553 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
12554 6f 6e 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 75  on a page */.  u
12555 31 36 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20  16 usableSize;  
12556 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
12557 66 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f  f usable bytes o
12558 6e 20 65 61 63 68 20 70 61 67 65 20 2a 2f 0a 20  n each page */. 
12559 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20 20   int maxLocal;  
1255a 20 20 20 20 20 20 20 2f 2a 20 4d 61 78 69 6d 75         /* Maximu
1255b 6d 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20  m local payload 
1255c 69 6e 20 6e 6f 6e 2d 4c 45 41 46 44 41 54 41 20  in non-LEAFDATA 
1255d 74 61 62 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20  tables */.  int 
1255e 6d 69 6e 4c 6f 63 61 6c 3b 20 20 20 20 20 20 20  minLocal;       
1255f 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 6c 6f 63    /* Minimum loc
12560 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e 20 6e 6f  al payload in no
12561 6e 2d 4c 45 41 46 44 41 54 41 20 74 61 62 6c 65  n-LEAFDATA table
12562 73 20 2a 2f 0a 20 20 69 6e 74 20 6d 61 78 4c 65  s */.  int maxLe
12563 61 66 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  af;          /* 
12564 4d 61 78 69 6d 75 6d 20 6c 6f 63 61 6c 20 70 61  Maximum local pa
12565 79 6c 6f 61 64 20 69 6e 20 61 20 4c 45 41 46 44  yload in a LEAFD
12566 41 54 41 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ATA table */.  i
12567 6e 74 20 6d 69 6e 4c 65 61 66 3b 20 20 20 20 20  nt minLeaf;     
12568 20 20 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20       /* Minimum 
12569 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 20 69 6e  local payload in
1256a 20 61 20 4c 45 41 46 44 41 54 41 20 74 61 62 6c   a LEAFDATA tabl
1256b 65 20 2a 2f 0a 20 20 75 38 20 69 6e 54 72 61 6e  e */.  u8 inTran
1256c 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20  saction;     /* 
1256d 54 72 61 6e 73 61 63 74 69 6f 6e 20 73 74 61 74  Transaction stat
1256e 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 72 61 6e  e */.  int nTran
1256f 73 61 63 74 69 6f 6e 3b 20 20 20 20 20 2f 2a 20  saction;     /* 
12570 4e 75 6d 62 65 72 20 6f 66 20 6f 70 65 6e 20 74  Number of open t
12571 72 61 6e 73 61 63 74 69 6f 6e 73 20 28 72 65 61  ransactions (rea
12572 64 20 2b 20 77 72 69 74 65 29 20 2a 2f 0a 20 20  d + write) */.  
12573 76 6f 69 64 20 2a 70 53 63 68 65 6d 61 3b 20 20  void *pSchema;  
12574 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
12575 20 74 6f 20 73 70 61 63 65 20 61 6c 6c 6f 63 61   to space alloca
12576 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 42 74  ted by sqlite3Bt
12577 72 65 65 53 63 68 65 6d 61 28 29 20 2a 2f 0a 20  reeSchema() */. 
12578 20 76 6f 69 64 20 28 2a 78 46 72 65 65 53 63 68   void (*xFreeSch
12579 65 6d 61 29 28 76 6f 69 64 2a 29 3b 20 20 2f 2a  ema)(void*);  /*
1257a 20 44 65 73 74 72 75 63 74 6f 72 20 66 6f 72 20   Destructor for 
1257b 42 74 53 68 61 72 65 64 2e 70 53 63 68 65 6d 61  BtShared.pSchema
1257c 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
1257d 74 65 78 20 2a 6d 75 74 65 78 3b 20 2f 2a 20 4e  tex *mutex; /* N
1257e 6f 6e 2d 72 65 63 75 72 73 69 76 65 20 6d 75 74  on-recursive mut
1257f 65 78 20 72 65 71 75 69 72 65 64 20 74 6f 20 61  ex required to a
12580 63 63 65 73 73 20 74 68 69 73 20 73 74 72 75 63  ccess this struc
12581 74 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  t */.#ifndef SQL
12582 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
12583 43 41 43 48 45 0a 20 20 69 6e 74 20 6e 52 65 66  CACHE.  int nRef
12584 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ;             /*
12585 20 4e 75 6d 62 65 72 20 6f 66 20 72 65 66 65 72   Number of refer
12586 65 6e 63 65 73 20 74 6f 20 74 68 69 73 20 73 74  ences to this st
12587 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 42 74 53  ructure */.  BtS
12588 68 61 72 65 64 20 2a 70 4e 65 78 74 3b 20 20 20  hared *pNext;   
12589 20 20 20 2f 2a 20 4e 65 78 74 20 6f 6e 20 61 20     /* Next on a 
1258a 6c 69 73 74 20 6f 66 20 73 68 61 72 61 62 6c 65  list of sharable
1258b 20 42 74 53 68 61 72 65 64 20 73 74 72 75 63 74   BtShared struct
1258c 73 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b 20 2a 70  s */.  BtLock *p
1258d 4c 6f 63 6b 3b 20 20 20 20 20 20 20 20 2f 2a 20  Lock;        /* 
1258e 4c 69 73 74 20 6f 66 20 6c 6f 63 6b 73 20 68 65  List of locks he
1258f 6c 64 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65  ld on this share
12590 64 2d 62 74 72 65 65 20 73 74 72 75 63 74 20 2a  d-btree struct *
12591 2f 0a 20 20 42 74 72 65 65 20 2a 70 45 78 63 6c  /.  Btree *pExcl
12592 75 73 69 76 65 3b 20 20 20 20 2f 2a 20 42 74 72  usive;    /* Btr
12593 65 65 20 77 69 74 68 20 61 6e 20 45 58 43 4c 55  ee with an EXCLU
12594 53 49 56 45 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  SIVE lock on the
12595 20 77 68 6f 6c 65 20 64 62 20 2a 2f 0a 23 65 6e   whole db */.#en
12596 64 69 66 0a 20 20 75 38 20 2a 70 54 6d 70 53 70  dif.  u8 *pTmpSp
12597 61 63 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  ace;        /* B
12598 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a 65  tShared.pageSize
12599 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
1259a 66 6f 72 20 74 6d 70 20 75 73 65 20 2a 2f 0a 7d  for tmp use */.}
1259b 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74  ;../*.** An inst
1259c 61 6e 63 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c  ance of the foll
1259d 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
1259e 69 73 20 75 73 65 64 20 74 6f 20 68 6f 6c 64 20  is used to hold 
1259f 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 61  information.** a
125a0 62 6f 75 74 20 61 20 63 65 6c 6c 2e 20 20 54 68  bout a cell.  Th
125a1 65 20 70 61 72 73 65 43 65 6c 6c 50 74 72 28 29  e parseCellPtr()
125a2 20 66 75 6e 63 74 69 6f 6e 20 66 69 6c 6c 73 20   function fills 
125a3 69 6e 20 74 68 69 73 20 73 74 72 75 63 74 75 72  in this structur
125a4 65 0a 2a 2a 20 62 61 73 65 64 20 6f 6e 20 69 6e  e.** based on in
125a5 66 6f 72 6d 61 74 69 6f 6e 20 65 78 74 72 61 63  formation extrac
125a6 74 20 66 72 6f 6d 20 74 68 65 20 72 61 77 20 64  t from the raw d
125a7 69 73 6b 20 70 61 67 65 2e 0a 2a 2f 0a 74 79 70  isk page..*/.typ
125a8 65 64 65 66 20 73 74 72 75 63 74 20 43 65 6c 6c  edef struct Cell
125a9 49 6e 66 6f 20 43 65 6c 6c 49 6e 66 6f 3b 0a 73  Info CellInfo;.s
125aa 74 72 75 63 74 20 43 65 6c 6c 49 6e 66 6f 20 7b  truct CellInfo {
125ab 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 20 20 20  .  u8 *pCell;   
125ac 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
125ad 74 68 65 20 73 74 61 72 74 20 6f 66 20 63 65 6c  the start of cel
125ae 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  l content */.  i
125af 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a  64 nKey;      /*
125b0 20 54 68 65 20 6b 65 79 20 66 6f 72 20 49 4e 54   The key for INT
125b1 4b 45 59 20 74 61 62 6c 65 73 2c 20 6f 72 20 6e  KEY tables, or n
125b2 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69  umber of bytes i
125b3 6e 20 6b 65 79 20 2a 2f 0a 20 20 75 33 32 20 6e  n key */.  u32 n
125b4 44 61 74 61 3b 20 20 20 20 20 2f 2a 20 4e 75 6d  Data;     /* Num
125b5 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
125b6 64 61 74 61 20 2a 2f 0a 20 20 75 33 32 20 6e 50  data */.  u32 nP
125b7 61 79 6c 6f 61 64 3b 20 20 2f 2a 20 54 6f 74 61  ayload;  /* Tota
125b8 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 70 61 79 6c  l amount of payl
125b9 6f 61 64 20 2a 2f 0a 20 20 75 31 36 20 6e 48 65  oad */.  u16 nHe
125ba 61 64 65 72 3b 20 20 20 2f 2a 20 53 69 7a 65 20  ader;   /* Size 
125bb 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74  of the cell cont
125bc 65 6e 74 20 68 65 61 64 65 72 20 69 6e 20 62 79  ent header in by
125bd 74 65 73 20 2a 2f 0a 20 20 75 31 36 20 6e 4c 6f  tes */.  u16 nLo
125be 63 61 6c 3b 20 20 20 20 2f 2a 20 41 6d 6f 75 6e  cal;    /* Amoun
125bf 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65 6c  t of payload hel
125c0 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20 75  d locally */.  u
125c1 31 36 20 69 4f 76 65 72 66 6c 6f 77 3b 20 2f 2a  16 iOverflow; /*
125c2 20 4f 66 66 73 65 74 20 74 6f 20 6f 76 65 72 66   Offset to overf
125c3 6c 6f 77 20 70 61 67 65 20 6e 75 6d 62 65 72 2e  low page number.
125c4 20 20 5a 65 72 6f 20 69 66 20 6e 6f 20 6f 76 65    Zero if no ove
125c5 72 66 6c 6f 77 20 2a 2f 0a 20 20 75 31 36 20 6e  rflow */.  u16 n
125c6 53 69 7a 65 3b 20 20 20 20 20 2f 2a 20 53 69 7a  Size;     /* Siz
125c7 65 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  e of the cell co
125c8 6e 74 65 6e 74 20 6f 6e 20 74 68 65 20 6d 61 69  ntent on the mai
125c9 6e 20 62 2d 74 72 65 65 20 70 61 67 65 20 2a 2f  n b-tree page */
125ca 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 4d 61 78 69 6d  .};../*.** Maxim
125cb 75 6d 20 64 65 70 74 68 20 6f 66 20 61 6e 20 53  um depth of an S
125cc 51 4c 69 74 65 20 42 2d 54 72 65 65 20 73 74 72  QLite B-Tree str
125cd 75 63 74 75 72 65 2e 20 41 6e 79 20 42 2d 54 72  ucture. Any B-Tr
125ce 65 65 20 64 65 65 70 65 72 20 74 68 61 6e 0a 2a  ee deeper than.*
125cf 2a 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 64  * this will be d
125d0 65 63 6c 61 72 65 64 20 63 6f 72 72 75 70 74 2e  eclared corrupt.
125d1 20 54 68 69 73 20 76 61 6c 75 65 20 69 73 20 63   This value is c
125d2 61 6c 63 75 6c 61 74 65 64 20 62 61 73 65 64 20  alculated based 
125d3 6f 6e 20 61 0a 2a 2a 20 6d 61 78 69 6d 75 6d 20  on a.** maximum 
125d4 64 61 74 61 62 61 73 65 20 73 69 7a 65 20 6f 66  database size of
125d5 20 32 5e 33 31 20 70 61 67 65 73 20 61 20 6d 69   2^31 pages a mi
125d6 6e 69 6d 75 6d 20 66 61 6e 6f 75 74 20 6f 66 20  nimum fanout of 
125d7 32 20 66 6f 72 20 61 0a 2a 2a 20 72 6f 6f 74 2d  2 for a.** root-
125d8 6e 6f 64 65 20 61 6e 64 20 33 20 66 6f 72 20 61  node and 3 for a
125d9 6c 6c 20 6f 74 68 65 72 20 69 6e 74 65 72 6e 61  ll other interna
125da 6c 20 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 49  l nodes..**.** I
125db 66 20 61 20 74 72 65 65 20 74 68 61 74 20 61 70  f a tree that ap
125dc 70 65 61 72 73 20 74 6f 20 62 65 20 74 61 6c 6c  pears to be tall
125dd 65 72 20 74 68 61 6e 20 74 68 69 73 20 69 73 20  er than this is 
125de 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 69 74 20  encountered, it 
125df 69 73 0a 2a 2a 20 61 73 73 75 6d 65 64 20 74 68  is.** assumed th
125e0 61 74 20 74 68 65 20 64 61 74 61 62 61 73 65 20  at the database 
125e1 69 73 20 63 6f 72 72 75 70 74 2e 0a 2a 2f 0a 23  is corrupt..*/.#
125e2 64 65 66 69 6e 65 20 42 54 43 55 52 53 4f 52 5f  define BTCURSOR_
125e3 4d 41 58 5f 44 45 50 54 48 20 32 30 0a 0a 2f 2a  MAX_DEPTH 20../*
125e4 0a 2a 2a 20 41 20 63 75 72 73 6f 72 20 69 73 20  .** A cursor is 
125e5 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 70  a pointer to a p
125e6 61 72 74 69 63 75 6c 61 72 20 65 6e 74 72 79 20  articular entry 
125e7 77 69 74 68 69 6e 20 61 20 70 61 72 74 69 63 75  within a particu
125e8 6c 61 72 0a 2a 2a 20 62 2d 74 72 65 65 20 77 69  lar.** b-tree wi
125e9 74 68 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  thin a database 
125ea 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
125eb 65 6e 74 72 79 20 69 73 20 69 64 65 6e 74 69 66  entry is identif
125ec 69 65 64 20 62 79 20 69 74 73 20 4d 65 6d 50 61  ied by its MemPa
125ed 67 65 20 61 6e 64 20 74 68 65 20 69 6e 64 65 78  ge and the index
125ee 20 69 6e 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61   in.** MemPage.a
125ef 43 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 65 6e  Cell[] of the en
125f0 74 72 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  try..**.** When 
125f1 61 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  a single databas
125f2 65 20 66 69 6c 65 20 63 61 6e 20 73 68 61 72 65  e file can share
125f3 64 20 62 79 20 74 77 6f 20 6d 6f 72 65 20 64 61  d by two more da
125f4 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
125f5 6e 73 2c 0a 2a 2a 20 62 75 74 20 63 75 72 73 6f  ns,.** but curso
125f6 72 73 20 63 61 6e 6e 6f 74 20 62 65 20 73 68 61  rs cannot be sha
125f7 72 65 64 2e 20 20 45 61 63 68 20 63 75 72 73 6f  red.  Each curso
125f8 72 20 69 73 20 61 73 73 6f 63 69 61 74 65 64 20  r is associated 
125f9 77 69 74 68 20 61 0a 2a 2a 20 70 61 72 74 69 63  with a.** partic
125fa 75 6c 61 72 20 64 61 74 61 62 61 73 65 20 63 6f  ular database co
125fb 6e 6e 65 63 74 69 6f 6e 20 69 64 65 6e 74 69 66  nnection identif
125fc 69 65 64 20 42 74 43 75 72 73 6f 72 2e 70 42 74  ied BtCursor.pBt
125fd 72 65 65 2e 64 62 2e 0a 2a 2a 0a 2a 2a 20 46 69  ree.db..**.** Fi
125fe 65 6c 64 73 20 69 6e 20 74 68 69 73 20 73 74 72  elds in this str
125ff 75 63 74 75 72 65 20 61 72 65 20 61 63 63 65 73  ucture are acces
12600 73 65 64 20 75 6e 64 65 72 20 74 68 65 20 42 74  sed under the Bt
12601 53 68 61 72 65 64 2e 6d 75 74 65 78 0a 2a 2a 20  Shared.mutex.** 
12602 66 6f 75 6e 64 20 61 74 20 73 65 6c 66 2d 3e 70  found at self->p
12603 42 74 2d 3e 6d 75 74 65 78 2e 20 0a 2a 2f 0a 73  Bt->mutex. .*/.s
12604 74 72 75 63 74 20 42 74 43 75 72 73 6f 72 20 7b  truct BtCursor {
12605 0a 20 20 42 74 72 65 65 20 2a 70 42 74 72 65 65  .  Btree *pBtree
12606 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ;            /* 
12607 54 68 65 20 42 74 72 65 65 20 74 6f 20 77 68 69  The Btree to whi
12608 63 68 20 74 68 69 73 20 63 75 72 73 6f 72 20 62  ch this cursor b
12609 65 6c 6f 6e 67 73 20 2a 2f 0a 20 20 42 74 53 68  elongs */.  BtSh
1260a 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 20  ared *pBt;      
1260b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 74 53        /* The BtS
1260c 68 61 72 65 64 20 74 68 69 73 20 63 75 72 73 6f  hared this curso
1260d 72 20 70 6f 69 6e 74 73 20 74 6f 20 2a 2f 0a 20  r points to */. 
1260e 20 42 74 43 75 72 73 6f 72 20 2a 70 4e 65 78 74   BtCursor *pNext
1260f 2c 20 2a 70 50 72 65 76 3b 20 20 2f 2a 20 46 6f  , *pPrev;  /* Fo
12610 72 6d 73 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73  rms a linked lis
12611 74 20 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73  t of all cursors
12612 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65 79   */.  struct Key
12613 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 20  Info *pKeyInfo; 
12614 2f 2a 20 41 72 67 75 6d 65 6e 74 20 70 61 73 73  /* Argument pass
12615 65 64 20 74 6f 20 63 6f 6d 70 61 72 69 73 6f 6e  ed to comparison
12616 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 50   function */.  P
12617 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 3b 20 20 20  gno pgnoRoot;   
12618 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
12619 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 68 69  root page of thi
1261a 73 20 74 72 65 65 20 2a 2f 0a 20 20 43 65 6c 6c  s tree */.  Cell
1261b 49 6e 66 6f 20 69 6e 66 6f 3b 20 20 20 20 20 20  Info info;      
1261c 20 20 20 20 20 20 2f 2a 20 41 20 70 61 72 73 65        /* A parse
1261d 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 77 65 20   of the cell we 
1261e 61 72 65 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  are pointing at 
1261f 2a 2f 0a 20 20 75 38 20 77 72 46 6c 61 67 3b 20  */.  u8 wrFlag; 
12620 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12621 2a 20 54 72 75 65 20 69 66 20 77 72 69 74 61 62  * True if writab
12622 6c 65 20 2a 2f 0a 20 20 75 38 20 61 74 4c 61 73  le */.  u8 atLas
12623 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
12624 20 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e    /* Cursor poin
12625 74 69 6e 67 20 74 6f 20 74 68 65 20 6c 61 73 74  ting to the last
12626 20 65 6e 74 72 79 20 2a 2f 0a 20 20 75 38 20 76   entry */.  u8 v
12627 61 6c 69 64 4e 4b 65 79 3b 20 20 20 20 20 20 20  alidNKey;       
12628 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
12629 20 69 6e 66 6f 2e 6e 4b 65 79 20 69 73 20 76 61   info.nKey is va
1262a 6c 69 64 20 2a 2f 0a 20 20 75 38 20 65 53 74 61  lid */.  u8 eSta
1262b 74 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  te;             
1262c 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68 65     /* One of the
1262d 20 43 55 52 53 4f 52 5f 58 58 58 20 63 6f 6e 73   CURSOR_XXX cons
1262e 74 61 6e 74 73 20 28 73 65 65 20 62 65 6c 6f 77  tants (see below
1262f 29 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 4b 65  ) */.  void *pKe
12630 79 3b 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64  y;      /* Saved
12631 20 6b 65 79 20 74 68 61 74 20 77 61 73 20 63 75   key that was cu
12632 72 73 6f 72 27 73 20 6c 61 73 74 20 6b 6e 6f 77  rsor's last know
12633 6e 20 70 6f 73 69 74 69 6f 6e 20 2a 2f 0a 20 20  n position */.  
12634 69 36 34 20 6e 4b 65 79 3b 20 20 20 20 20 20 20  i64 nKey;       
12635 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 4b 65 79   /* Size of pKey
12636 2c 20 6f 72 20 6c 61 73 74 20 69 6e 74 65 67 65  , or last intege
12637 72 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 73  r key */.  int s
12638 6b 69 70 3b 20 20 20 20 20 20 20 20 2f 2a 20 28  kip;        /* (
12639 73 6b 69 70 3c 30 29 20 2d 3e 20 50 72 65 76 28  skip<0) -> Prev(
1263a 29 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 28 73  ) is a no-op. (s
1263b 6b 69 70 3e 30 29 20 2d 3e 20 4e 65 78 74 28 29  kip>0) -> Next()
1263c 20 69 73 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53   is */.#ifndef S
1263d 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
1263e 4c 4f 42 0a 20 20 75 38 20 69 73 49 6e 63 72 62  LOB.  u8 isIncrb
1263f 6c 6f 62 48 61 6e 64 6c 65 3b 20 20 20 20 20 20  lobHandle;      
12640 2f 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20  /* True if this 
12641 63 75 72 73 6f 72 20 69 73 20 61 6e 20 69 6e 63  cursor is an inc
12642 72 2e 20 69 6f 20 68 61 6e 64 6c 65 20 2a 2f 0a  r. io handle */.
12643 20 20 50 67 6e 6f 20 2a 61 4f 76 65 72 66 6c 6f    Pgno *aOverflo
12644 77 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  w;          /* C
12645 61 63 68 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77  ache of overflow
12646 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 73 20   page locations 
12647 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65  */.#endif.#ifnde
12648 66 20 4e 44 45 42 55 47 0a 20 20 75 38 20 70 61  f NDEBUG.  u8 pa
12649 67 65 73 53 68 75 66 66 6c 65 64 3b 20 20 20 20  gesShuffled;    
1264a 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1264b 42 74 72 65 65 20 70 61 67 65 73 20 61 72 65 20  Btree pages are 
1264c 72 65 61 72 72 61 6e 67 65 64 20 62 79 20 62 61  rearranged by ba
1264d 6c 61 6e 63 65 28 29 2a 2f 0a 23 65 6e 64 69 66  lance()*/.#endif
1264e 0a 20 20 69 31 36 20 69 50 61 67 65 3b 20 20 20  .  i16 iPage;   
1264f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12650 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
12651 78 20 6f 66 20 63 75 72 72 65 6e 74 20 70 61 67  x of current pag
12652 65 20 69 6e 20 61 70 50 61 67 65 20 2a 2f 0a 20  e in apPage */. 
12653 20 4d 65 6d 50 61 67 65 20 2a 61 70 50 61 67 65   MemPage *apPage
12654 5b 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f 44 45  [BTCURSOR_MAX_DE
12655 50 54 48 5d 3b 20 20 2f 2a 20 50 61 67 65 73 20  PTH];  /* Pages 
12656 66 72 6f 6d 20 72 6f 6f 74 20 74 6f 20 63 75 72  from root to cur
12657 72 65 6e 74 20 70 61 67 65 20 2a 2f 0a 20 20 75  rent page */.  u
12658 31 36 20 61 69 49 64 78 5b 42 54 43 55 52 53 4f  16 aiIdx[BTCURSO
12659 52 5f 4d 41 58 5f 44 45 50 54 48 5d 3b 20 20 20  R_MAX_DEPTH];   
1265a 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
1265b 69 6e 64 65 78 20 69 6e 20 61 70 50 61 67 65 5b  index in apPage[
1265c 69 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  i] */.};../*.** 
1265d 50 6f 74 65 6e 74 69 61 6c 20 76 61 6c 75 65 73  Potential values
1265e 20 66 6f 72 20 42 74 43 75 72 73 6f 72 2e 65 53   for BtCursor.eS
1265f 74 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53  tate..**.** CURS
12660 4f 52 5f 56 41 4c 49 44 3a 0a 2a 2a 20 20 20 43  OR_VALID:.**   C
12661 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20  ursor points to 
12662 61 20 76 61 6c 69 64 20 65 6e 74 72 79 2e 20 67  a valid entry. g
12663 65 74 50 61 79 6c 6f 61 64 28 29 20 65 74 63 2e  etPayload() etc.
12664 20 6d 61 79 20 62 65 20 63 61 6c 6c 65 64 2e 0a   may be called..
12665 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 49 4e 56  **.** CURSOR_INV
12666 41 4c 49 44 3a 0a 2a 2a 20 20 20 43 75 72 73 6f  ALID:.**   Curso
12667 72 20 64 6f 65 73 20 6e 6f 74 20 70 6f 69 6e 74  r does not point
12668 20 74 6f 20 61 20 76 61 6c 69 64 20 65 6e 74 72   to a valid entr
12669 79 2e 20 54 68 69 73 20 63 61 6e 20 68 61 70 70  y. This can happ
1266a 65 6e 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29  en (for example)
1266b 20 0a 2a 2a 20 20 20 62 65 63 61 75 73 65 20 74   .**   because t
1266c 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
1266d 79 20 6f 72 20 62 65 63 61 75 73 65 20 42 74 72  y or because Btr
1266e 65 65 43 75 72 73 6f 72 46 69 72 73 74 28 29 20  eeCursorFirst() 
1266f 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 2a 2a 20  has not been.** 
12670 20 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a 2a 2a 20    called..**.** 
12671 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
12672 45 4b 3a 0a 2a 2a 20 20 20 54 68 65 20 74 61 62  EK:.**   The tab
12673 6c 65 20 74 68 61 74 20 74 68 69 73 20 63 75 72  le that this cur
12674 73 6f 72 20 77 61 73 20 6f 70 65 6e 65 64 20 6f  sor was opened o
12675 6e 20 73 74 69 6c 6c 20 65 78 69 73 74 73 2c 20  n still exists, 
12676 62 75 74 20 68 61 73 20 62 65 65 6e 20 0a 2a 2a  but has been .**
12677 20 20 20 6d 6f 64 69 66 69 65 64 20 73 69 6e 63     modified sinc
12678 65 20 74 68 65 20 63 75 72 73 6f 72 20 77 61 73  e the cursor was
12679 20 6c 61 73 74 20 75 73 65 64 2e 20 54 68 65 20   last used. The 
1267a 63 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20  cursor position 
1267b 69 73 20 73 61 76 65 64 0a 2a 2a 20 20 20 69 6e  is saved.**   in
1267c 20 76 61 72 69 61 62 6c 65 73 20 42 74 43 75 72   variables BtCur
1267d 73 6f 72 2e 70 4b 65 79 20 61 6e 64 20 42 74 43  sor.pKey and BtC
1267e 75 72 73 6f 72 2e 6e 4b 65 79 2e 20 57 68 65 6e  ursor.nKey. When
1267f 20 61 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20   a cursor is in 
12680 0a 2a 2a 20 20 20 74 68 69 73 20 73 74 61 74 65  .**   this state
12681 2c 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50  , restoreCursorP
12682 6f 73 69 74 69 6f 6e 28 29 20 63 61 6e 20 62 65  osition() can be
12683 20 63 61 6c 6c 65 64 20 74 6f 20 61 74 74 65 6d   called to attem
12684 70 74 20 74 6f 0a 2a 2a 20 20 20 73 65 65 6b 20  pt to.**   seek 
12685 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
12686 65 20 73 61 76 65 64 20 70 6f 73 69 74 69 6f 6e  e saved position
12687 2e 0a 2a 2a 0a 2a 2a 20 43 55 52 53 4f 52 5f 46  ..**.** CURSOR_F
12688 41 55 4c 54 3a 0a 2a 2a 20 20 20 41 20 75 6e 72  AULT:.**   A unr
12689 65 63 6f 76 65 72 61 62 6c 65 20 65 72 72 6f 72  ecoverable error
1268a 20 28 61 6e 20 49 2f 4f 20 65 72 72 6f 72 20 6f   (an I/O error o
1268b 72 20 61 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 75  r a malloc failu
1268c 72 65 29 20 68 61 73 20 6f 63 63 75 72 72 65 64  re) has occurred
1268d 0a 2a 2a 20 20 20 6f 6e 20 61 20 64 69 66 66 65  .**   on a diffe
1268e 72 65 6e 74 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  rent connection 
1268f 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20  that shares the 
12690 42 74 53 68 61 72 65 64 20 63 61 63 68 65 20 77  BtShared cache w
12691 69 74 68 20 74 68 69 73 0a 2a 2a 20 20 20 63 75  ith this.**   cu
12692 72 73 6f 72 2e 20 20 54 68 65 20 65 72 72 6f 72  rsor.  The error
12693 20 68 61 73 20 6c 65 66 74 20 74 68 65 20 63 61   has left the ca
12694 63 68 65 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  che in an incons
12695 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2a  istent state..**
12696 20 20 20 44 6f 20 6e 6f 74 68 69 6e 67 20 65 6c     Do nothing el
12697 73 65 20 77 69 74 68 20 74 68 69 73 20 63 75 72  se with this cur
12698 73 6f 72 2e 20 20 41 6e 79 20 61 74 74 65 6d 70  sor.  Any attemp
12699 74 20 74 6f 20 75 73 65 20 74 68 65 20 63 75 72  t to use the cur
1269a 73 6f 72 0a 2a 2a 20 20 20 73 68 6f 75 6c 64 20  sor.**   should 
1269b 72 65 74 75 72 6e 20 74 68 65 20 65 72 72 6f 72  return the error
1269c 20 63 6f 64 65 20 73 74 6f 72 65 64 20 69 6e 20   code stored in 
1269d 42 74 43 75 72 73 6f 72 2e 73 6b 69 70 0a 2a 2f  BtCursor.skip.*/
1269e 0a 23 64 65 66 69 6e 65 20 43 55 52 53 4f 52 5f  .#define CURSOR_
1269f 49 4e 56 41 4c 49 44 20 20 20 20 20 20 20 20 20  INVALID         
126a0 20 20 30 0a 23 64 65 66 69 6e 65 20 43 55 52 53    0.#define CURS
126a1 4f 52 5f 56 41 4c 49 44 20 20 20 20 20 20 20 20  OR_VALID        
126a2 20 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 43       1.#define C
126a3 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
126a4 4b 20 20 20 20 20 20 20 32 0a 23 64 65 66 69 6e  K       2.#defin
126a5 65 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 20  e CURSOR_FAULT  
126a6 20 20 20 20 20 20 20 20 20 20 20 33 0a 0a 2f 2a             3../*
126a7 20 54 68 65 20 64 61 74 61 62 61 73 65 20 70 61   The database pa
126a8 67 65 20 74 68 65 20 50 45 4e 44 49 4e 47 5f 42  ge the PENDING_B
126a9 59 54 45 20 6f 63 63 75 70 69 65 73 2e 20 54 68  YTE occupies. Th
126aa 69 73 20 70 61 67 65 20 69 73 20 6e 65 76 65 72  is page is never
126ab 20 75 73 65 64 2e 0a 2a 2a 20 54 4f 44 4f 3a 20   used..** TODO: 
126ac 54 68 69 73 20 6d 61 63 72 6f 20 69 73 20 76 65  This macro is ve
126ad 72 79 20 73 69 6d 69 6c 61 72 79 20 74 6f 20 50  ry similary to P
126ae 41 47 45 52 5f 4d 4a 5f 50 47 4e 4f 28 29 20 69  AGER_MJ_PGNO() i
126af 6e 20 70 61 67 65 72 2e 63 2e 20 54 68 65 79 0a  n pager.c. They.
126b0 2a 2a 20 73 68 6f 75 6c 64 20 70 6f 73 73 69 62  ** should possib
126b1 6c 79 20 62 65 20 63 6f 6e 73 6f 6c 69 64 61 74  ly be consolidat
126b2 65 64 20 28 70 72 65 73 75 6d 61 62 6c 79 20 69  ed (presumably i
126b3 6e 20 70 61 67 65 72 2e 68 29 2e 0a 2a 2a 0a 2a  n pager.h)..**.*
126b4 2a 20 49 66 20 64 69 73 6b 20 49 2f 4f 20 69 73  * If disk I/O is
126b5 20 6f 6d 69 74 74 65 64 20 28 6d 65 61 6e 69 6e   omitted (meanin
126b6 67 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  g that the datab
126b7 61 73 65 20 69 73 20 73 74 6f 72 65 64 20 70 75  ase is stored pu
126b8 72 65 6c 79 0a 2a 2a 20 69 6e 20 6d 65 6d 6f 72  rely.** in memor
126b9 79 29 20 74 68 65 6e 20 74 68 65 72 65 20 69 73  y) then there is
126ba 20 6e 6f 20 70 65 6e 64 69 6e 67 20 62 79 74 65   no pending byte
126bb 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49  ..*/.#ifdef SQLI
126bc 54 45 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 23  TE_OMIT_DISKIO.#
126bd 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e 47 5f   define PENDING_
126be 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 20  BYTE_PAGE(pBt)  
126bf 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
126c0 0a 23 20 64 65 66 69 6e 65 20 50 45 4e 44 49 4e  .# define PENDIN
126c1 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74 29  G_BYTE_PAGE(pBt)
126c2 20 28 28 50 67 6e 6f 29 28 28 50 45 4e 44 49 4e   ((Pgno)((PENDIN
126c3 47 5f 42 59 54 45 2f 28 70 42 74 29 2d 3e 70 61  G_BYTE/(pBt)->pa
126c4 67 65 53 69 7a 65 29 2b 31 29 29 0a 23 65 6e 64  geSize)+1)).#end
126c5 69 66 0a 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 6e 6b  if../*.** A link
126c6 65 64 20 6c 69 73 74 20 6f 66 20 74 68 65 20 66  ed list of the f
126c7 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
126c8 72 65 73 20 69 73 20 73 74 6f 72 65 64 20 61 74  res is stored at
126c9 20 42 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 2e   BtShared.pLock.
126ca 0a 2a 2a 20 4c 6f 63 6b 73 20 61 72 65 20 61 64  .** Locks are ad
126cb 64 65 64 20 28 6f 72 20 75 70 67 72 61 64 65 64  ded (or upgraded
126cc 20 66 72 6f 6d 20 52 45 41 44 5f 4c 4f 43 4b 20   from READ_LOCK 
126cd 74 6f 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 77  to WRITE_LOCK) w
126ce 68 65 6e 20 61 20 63 75 72 73 6f 72 20 0a 2a 2a  hen a cursor .**
126cf 20 69 73 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68   is opened on th
126d0 65 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f  e table with roo
126d1 74 20 70 61 67 65 20 42 74 53 68 61 72 65 64 2e  t page BtShared.
126d2 69 54 61 62 6c 65 2e 20 4c 6f 63 6b 73 20 61 72  iTable. Locks ar
126d3 65 20 72 65 6d 6f 76 65 64 0a 2a 2a 20 66 72 6f  e removed.** fro
126d4 6d 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e  m this list when
126d5 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
126d6 73 20 63 6f 6d 6d 69 74 74 65 64 20 6f 72 20 72  s committed or r
126d7 6f 6c 6c 65 64 20 62 61 63 6b 2c 20 6f 72 20 77  olled back, or w
126d8 68 65 6e 0a 2a 2a 20 61 20 62 74 72 65 65 20 68  hen.** a btree h
126d9 61 6e 64 6c 65 20 69 73 20 63 6c 6f 73 65 64 2e  andle is closed.
126da 0a 2a 2f 0a 73 74 72 75 63 74 20 42 74 4c 6f 63  .*/.struct BtLoc
126db 6b 20 7b 0a 20 20 42 74 72 65 65 20 2a 70 42 74  k {.  Btree *pBt
126dc 72 65 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 42  ree;        /* B
126dd 74 72 65 65 20 68 61 6e 64 6c 65 20 68 6f 6c 64  tree handle hold
126de 69 6e 67 20 74 68 69 73 20 6c 6f 63 6b 20 2a 2f  ing this lock */
126df 0a 20 20 50 67 6e 6f 20 69 54 61 62 6c 65 3b 20  .  Pgno iTable; 
126e0 20 20 20 20 20 20 20 20 20 2f 2a 20 52 6f 6f 74           /* Root
126e1 20 70 61 67 65 20 6f 66 20 74 61 62 6c 65 20 2a   page of table *
126e2 2f 0a 20 20 75 38 20 65 4c 6f 63 6b 3b 20 20 20  /.  u8 eLock;   
126e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41            /* REA
126e4 44 5f 4c 4f 43 4b 20 6f 72 20 57 52 49 54 45 5f  D_LOCK or WRITE_
126e5 4c 4f 43 4b 20 2a 2f 0a 20 20 42 74 4c 6f 63 6b  LOCK */.  BtLock
126e6 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20 20   *pNext;        
126e7 2f 2a 20 4e 65 78 74 20 69 6e 20 42 74 53 68 61  /* Next in BtSha
126e8 72 65 64 2e 70 4c 6f 63 6b 20 6c 69 73 74 20 2a  red.pLock list *
126e9 2f 0a 7d 3b 0a 0a 2f 2a 20 43 61 6e 64 69 64 61  /.};../* Candida
126ea 74 65 20 76 61 6c 75 65 73 20 66 6f 72 20 42 74  te values for Bt
126eb 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 2a 2f 0a 23 64  Lock.eLock */.#d
126ec 65 66 69 6e 65 20 52 45 41 44 5f 4c 4f 43 4b 20  efine READ_LOCK 
126ed 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 57 52      1.#define WR
126ee 49 54 45 5f 4c 4f 43 4b 20 20 20 20 32 0a 0a 2f  ITE_LOCK    2../
126ef 2a 0a 2a 2a 20 54 68 65 73 65 20 6d 61 63 72 6f  *.** These macro
126f0 73 20 64 65 66 69 6e 65 20 74 68 65 20 6c 6f 63  s define the loc
126f1 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 69  ation of the poi
126f2 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66  nter-map entry f
126f3 6f 72 20 61 20 0a 2a 2a 20 64 61 74 61 62 61 73  or a .** databas
126f4 65 20 70 61 67 65 2e 20 54 68 65 20 66 69 72 73  e page. The firs
126f5 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 65 61  t argument to ea
126f6 63 68 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ch is the number
126f7 20 6f 66 20 75 73 61 62 6c 65 0a 2a 2a 20 62 79   of usable.** by
126f8 74 65 73 20 6f 6e 20 65 61 63 68 20 70 61 67 65  tes on each page
126f9 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
126fa 20 28 6f 66 74 65 6e 20 31 30 32 34 29 2e 20 54   (often 1024). T
126fb 68 65 20 73 65 63 6f 6e 64 20 69 73 20 74 68 65  he second is the
126fc 0a 2a 2a 20 70 61 67 65 20 6e 75 6d 62 65 72 20  .** page number 
126fd 74 6f 20 6c 6f 6f 6b 20 75 70 20 69 6e 20 74 68  to look up in th
126fe 65 20 70 6f 69 6e 74 65 72 20 6d 61 70 2e 0a 2a  e pointer map..*
126ff 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f 50 41 47 45  *.** PTRMAP_PAGE
12700 4e 4f 20 72 65 74 75 72 6e 73 20 74 68 65 20 64  NO returns the d
12701 61 74 61 62 61 73 65 20 70 61 67 65 20 6e 75 6d  atabase page num
12702 62 65 72 20 6f 66 20 74 68 65 20 70 6f 69 6e 74  ber of the point
12703 65 72 2d 6d 61 70 0a 2a 2a 20 70 61 67 65 20 74  er-map.** page t
12704 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20 72  hat stores the r
12705 65 71 75 69 72 65 64 20 70 6f 69 6e 74 65 72 2e  equired pointer.
12706 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45   PTRMAP_PTROFFSE
12707 54 20 72 65 74 75 72 6e 73 0a 2a 2a 20 74 68 65  T returns.** the
12708 20 6f 66 66 73 65 74 20 6f 66 20 74 68 65 20 72   offset of the r
12709 65 71 75 65 73 74 65 64 20 6d 61 70 20 65 6e 74  equested map ent
1270a 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ry..**.** If the
1270b 20 70 67 6e 6f 20 61 72 67 75 6d 65 6e 74 20 70   pgno argument p
1270c 61 73 73 65 64 20 74 6f 20 50 54 52 4d 41 50 5f  assed to PTRMAP_
1270d 50 41 47 45 4e 4f 20 69 73 20 61 20 70 6f 69 6e  PAGENO is a poin
1270e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 0a 2a 2a  ter-map page,.**
1270f 20 74 68 65 6e 20 70 67 6e 6f 20 69 73 20 72 65   then pgno is re
12710 74 75 72 6e 65 64 2e 20 53 6f 20 28 70 67 6e 6f  turned. So (pgno
12711 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  ==PTRMAP_PAGENO(
12712 70 67 73 7a 2c 20 70 67 6e 6f 29 29 20 63 61 6e  pgsz, pgno)) can
12713 20 62 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20 74   be.** used to t
12714 65 73 74 20 69 66 20 70 67 6e 6f 20 69 73 20 61  est if pgno is a
12715 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67   pointer-map pag
12716 65 2e 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  e. PTRMAP_ISPAGE
12717 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20 74   implements.** t
12718 68 69 73 20 74 65 73 74 2e 0a 2a 2f 0a 23 64 65  his test..*/.#de
12719 66 69 6e 65 20 50 54 52 4d 41 50 5f 50 41 47 45  fine PTRMAP_PAGE
1271a 4e 4f 28 70 42 74 2c 20 70 67 6e 6f 29 20 70 74  NO(pBt, pgno) pt
1271b 72 6d 61 70 50 61 67 65 6e 6f 28 70 42 74 2c 20  rmapPageno(pBt, 
1271c 70 67 6e 6f 29 0a 23 64 65 66 69 6e 65 20 50 54  pgno).#define PT
1271d 52 4d 41 50 5f 50 54 52 4f 46 46 53 45 54 28 70  RMAP_PTROFFSET(p
1271e 67 70 74 72 6d 61 70 2c 20 70 67 6e 6f 29 20 28  gptrmap, pgno) (
1271f 35 2a 28 70 67 6e 6f 2d 70 67 70 74 72 6d 61 70  5*(pgno-pgptrmap
12720 2d 31 29 29 0a 23 64 65 66 69 6e 65 20 50 54 52  -1)).#define PTR
12721 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
12722 70 67 6e 6f 29 20 28 50 54 52 4d 41 50 5f 50 41  pgno) (PTRMAP_PA
12723 47 45 4e 4f 28 28 70 42 74 29 2c 28 70 67 6e 6f  GENO((pBt),(pgno
12724 29 29 3d 3d 28 70 67 6e 6f 29 29 0a 0a 2f 2a 0a  ))==(pgno))../*.
12725 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65 72 20 6d  ** The pointer m
12726 61 70 20 69 73 20 61 20 6c 6f 6f 6b 75 70 20 74  ap is a lookup t
12727 61 62 6c 65 20 74 68 61 74 20 69 64 65 6e 74 69  able that identi
12728 66 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20  fies the parent 
12729 70 61 67 65 20 66 6f 72 0a 2a 2a 20 65 61 63 68  page for.** each
1272a 20 63 68 69 6c 64 20 70 61 67 65 20 69 6e 20 74   child page in t
1272b 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
1272c 2e 20 20 54 68 65 20 70 61 72 65 6e 74 20 70 61  .  The parent pa
1272d 67 65 20 69 73 20 74 68 65 20 70 61 67 65 20 74  ge is the page t
1272e 68 61 74 0a 2a 2a 20 63 6f 6e 74 61 69 6e 73 20  hat.** contains 
1272f 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
12730 20 63 68 69 6c 64 2e 20 20 45 76 65 72 79 20 70   child.  Every p
12731 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
12732 61 73 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20  ase contains.** 
12733 30 20 6f 72 20 31 20 70 61 72 65 6e 74 20 70 61  0 or 1 parent pa
12734 67 65 73 2e 20 20 28 49 6e 20 74 68 69 73 20 63  ges.  (In this c
12735 6f 6e 74 65 78 74 20 27 64 61 74 61 62 61 73 65  ontext 'database
12736 20 70 61 67 65 27 20 72 65 66 65 72 73 0a 2a 2a   page' refers.**
12737 20 74 6f 20 61 6e 79 20 70 61 67 65 20 74 68 61   to any page tha
12738 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
12739 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
1273a 20 69 74 73 65 6c 66 2e 29 20 20 45 61 63 68 20   itself.)  Each 
1273b 70 6f 69 6e 74 65 72 20 6d 61 70 0a 2a 2a 20 65  pointer map.** e
1273c 6e 74 72 79 20 63 6f 6e 73 69 73 74 73 20 6f 66  ntry consists of
1273d 20 61 20 73 69 6e 67 6c 65 20 62 79 74 65 20 27   a single byte '
1273e 74 79 70 65 27 20 61 6e 64 20 61 20 34 20 62 79  type' and a 4 by
1273f 74 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 6e  te parent page n
12740 75 6d 62 65 72 2e 0a 2a 2a 20 54 68 65 20 50 54  umber..** The PT
12741 52 4d 41 50 5f 58 58 58 20 69 64 65 6e 74 69 66  RMAP_XXX identif
12742 69 65 72 73 20 62 65 6c 6f 77 20 61 72 65 20 74  iers below are t
12743 68 65 20 76 61 6c 69 64 20 74 79 70 65 73 2e 0a  he valid types..
12744 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72 70 6f 73  **.** The purpos
12745 65 20 6f 66 20 74 68 65 20 70 6f 69 6e 74 65 72  e of the pointer
12746 20 6d 61 70 20 69 73 20 74 6f 20 66 61 63 69 6c   map is to facil
12747 69 74 79 20 6d 6f 76 69 6e 67 20 70 61 67 65 73  ity moving pages
12748 20 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 70 6f 73   from one.** pos
12749 69 74 69 6f 6e 20 69 6e 20 74 68 65 20 66 69 6c  ition in the fil
1274a 65 20 74 6f 20 61 6e 6f 74 68 65 72 20 61 73 20  e to another as 
1274b 70 61 72 74 20 6f 66 20 61 75 74 6f 76 61 63 75  part of autovacu
1274c 75 6d 2e 20 20 57 68 65 6e 20 61 20 70 61 67 65  um.  When a page
1274d 0a 2a 2a 20 69 73 20 6d 6f 76 65 64 2c 20 74 68  .** is moved, th
1274e 65 20 70 6f 69 6e 74 65 72 20 69 6e 20 69 74 73  e pointer in its
1274f 20 70 61 72 65 6e 74 20 6d 75 73 74 20 62 65 20   parent must be 
12750 75 70 64 61 74 65 64 20 74 6f 20 70 6f 69 6e 74  updated to point
12751 20 74 6f 20 74 68 65 0a 2a 2a 20 6e 65 77 20 6c   to the.** new l
12752 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20 70 6f  ocation.  The po
12753 69 6e 74 65 72 20 6d 61 70 20 69 73 20 75 73 65  inter map is use
12754 64 20 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20  d to locate the 
12755 70 61 72 65 6e 74 20 70 61 67 65 20 71 75 69 63  parent page quic
12756 6b 6c 79 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41  kly..**.** PTRMA
12757 50 5f 52 4f 4f 54 50 41 47 45 3a 20 54 68 65 20  P_ROOTPAGE: The 
12758 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
12759 20 61 20 72 6f 6f 74 2d 70 61 67 65 2e 20 54 68   a root-page. Th
1275a 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 69 73  e page-number is
1275b 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20 20 20 20   not.**         
1275c 20 20 20 20 20 20 20 20 20 75 73 65 64 20 69 6e           used in
1275d 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a 0a 2a   this case..**.*
1275e 2a 20 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  * PTRMAP_FREEPAG
1275f 45 3a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  E: The database 
12760 70 61 67 65 20 69 73 20 61 6e 20 75 6e 75 73 65  page is an unuse
12761 64 20 28 66 72 65 65 29 20 70 61 67 65 2e 20 54  d (free) page. T
12762 68 65 20 70 61 67 65 2d 6e 75 6d 62 65 72 20 0a  he page-number .
12763 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
12764 20 20 20 20 69 73 20 6e 6f 74 20 75 73 65 64 20      is not used 
12765 69 6e 20 74 68 69 73 20 63 61 73 65 2e 0a 2a 2a  in this case..**
12766 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  .** PTRMAP_OVERF
12767 4c 4f 57 31 3a 20 54 68 65 20 64 61 74 61 62 61  LOW1: The databa
12768 73 65 20 70 61 67 65 20 69 73 20 74 68 65 20 66  se page is the f
12769 69 72 73 74 20 70 61 67 65 20 69 6e 20 61 20 6c  irst page in a l
1276a 69 73 74 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20  ist of .**      
1276b 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 76 65               ove
1276c 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65  rflow pages. The
1276d 20 70 61 67 65 20 6e 75 6d 62 65 72 20 69 64 65   page number ide
1276e 6e 74 69 66 69 65 73 20 74 68 65 20 70 61 67 65  ntifies the page
1276f 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
12770 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 61             conta
12771 69 6e 73 20 74 68 65 20 63 65 6c 6c 20 77 69 74  ins the cell wit
12772 68 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  h a pointer to t
12773 68 69 73 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  his overflow pag
12774 65 2e 0a 2a 2a 0a 2a 2a 20 50 54 52 4d 41 50 5f  e..**.** PTRMAP_
12775 4f 56 45 52 46 4c 4f 57 32 3a 20 54 68 65 20 64  OVERFLOW2: The d
12776 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73 20  atabase page is 
12777 74 68 65 20 73 65 63 6f 6e 64 20 6f 72 20 6c 61  the second or la
12778 74 65 72 20 70 61 67 65 20 69 6e 20 61 20 6c 69  ter page in a li
12779 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20  st of.**        
1277a 20 20 20 20 20 20 20 20 20 20 20 6f 76 65 72 66             overf
1277b 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65 20 70  low pages. The p
1277c 61 67 65 2d 6e 75 6d 62 65 72 20 69 64 65 6e 74  age-number ident
1277d 69 66 69 65 73 20 74 68 65 20 70 72 65 76 69 6f  ifies the previo
1277e 75 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  us.**           
1277f 20 20 20 20 20 20 20 20 70 61 67 65 20 69 6e 20          page in 
12780 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  the overflow pag
12781 65 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 50 54  e list..**.** PT
12782 52 4d 41 50 5f 42 54 52 45 45 3a 20 54 68 65 20  RMAP_BTREE: The 
12783 64 61 74 61 62 61 73 65 20 70 61 67 65 20 69 73  database page is
12784 20 61 20 6e 6f 6e 2d 72 6f 6f 74 20 62 74 72 65   a non-root btre
12785 65 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65  e page. The page
12786 20 6e 75 6d 62 65 72 0a 2a 2a 20 20 20 20 20 20   number.**      
12787 20 20 20 20 20 20 20 20 20 69 64 65 6e 74 69 66           identif
12788 69 65 73 20 74 68 65 20 70 61 72 65 6e 74 20 70  ies the parent p
12789 61 67 65 20 69 6e 20 74 68 65 20 62 74 72 65 65  age in the btree
1278a 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 50 54 52  ..*/.#define PTR
1278b 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20 31 0a 23  MAP_ROOTPAGE 1.#
1278c 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 46 52  define PTRMAP_FR
1278d 45 45 50 41 47 45 20 32 0a 23 64 65 66 69 6e 65  EEPAGE 2.#define
1278e 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
1278f 31 20 33 0a 23 64 65 66 69 6e 65 20 50 54 52 4d  1 3.#define PTRM
12790 41 50 5f 4f 56 45 52 46 4c 4f 57 32 20 34 0a 23  AP_OVERFLOW2 4.#
12791 64 65 66 69 6e 65 20 50 54 52 4d 41 50 5f 42 54  define PTRMAP_BT
12792 52 45 45 20 35 0a 0a 2f 2a 20 41 20 62 75 6e 63  REE 5../* A bunc
12793 68 20 6f 66 20 61 73 73 65 72 74 28 29 20 73 74  h of assert() st
12794 61 74 65 6d 65 6e 74 73 20 74 6f 20 63 68 65 63  atements to chec
12795 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  k the transactio
12796 6e 20 73 74 61 74 65 20 76 61 72 69 61 62 6c 65  n state variable
12797 73 0a 2a 2a 20 6f 66 20 68 61 6e 64 6c 65 20 70  s.** of handle p
12798 20 28 74 79 70 65 20 42 74 72 65 65 2a 29 20 61   (type Btree*) a
12799 72 65 20 69 6e 74 65 72 6e 61 6c 6c 79 20 63 6f  re internally co
1279a 6e 73 69 73 74 65 6e 74 2e 0a 2a 2f 0a 23 64 65  nsistent..*/.#de
1279b 66 69 6e 65 20 62 74 72 65 65 49 6e 74 65 67 72  fine btreeIntegr
1279c 69 74 79 28 70 29 20 5c 0a 20 20 61 73 73 65 72  ity(p) \.  asser
1279d 74 28 20 70 2d 3e 70 42 74 2d 3e 69 6e 54 72 61  t( p->pBt->inTra
1279e 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f  nsaction!=TRANS_
1279f 4e 4f 4e 45 20 7c 7c 20 70 2d 3e 70 42 74 2d 3e  NONE || p->pBt->
127a0 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 30 20  nTransaction==0 
127a1 29 3b 20 5c 0a 20 20 61 73 73 65 72 74 28 20 70  ); \.  assert( p
127a2 2d 3e 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ->pBt->inTransac
127a3 74 69 6f 6e 3e 3d 70 2d 3e 69 6e 54 72 61 6e 73  tion>=p->inTrans
127a4 20 29 3b 20 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 65   ); .../*.** The
127a5 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 6d 61   ISAUTOVACUUM ma
127a6 63 72 6f 20 69 73 20 75 73 65 64 20 77 69 74 68  cro is used with
127a7 69 6e 20 62 61 6c 61 6e 63 65 5f 6e 6f 6e 72 6f  in balance_nonro
127a8 6f 74 28 29 20 74 6f 20 64 65 74 65 72 6d 69 6e  ot() to determin
127a9 65 0a 2a 2a 20 69 66 20 74 68 65 20 64 61 74 61  e.** if the data
127aa 62 61 73 65 20 73 75 70 70 6f 72 74 73 20 61 75  base supports au
127ab 74 6f 2d 76 61 63 75 75 6d 20 6f 72 20 6e 6f 74  to-vacuum or not
127ac 2e 20 42 65 63 61 75 73 65 20 69 74 20 69 73 20  . Because it is 
127ad 75 73 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 61  used.** within a
127ae 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61  n expression tha
127af 74 20 69 73 20 61 6e 20 61 72 67 75 6d 65 6e 74  t is an argument
127b0 20 74 6f 20 61 6e 6f 74 68 65 72 20 6d 61 63 72   to another macr
127b1 6f 20 0a 2a 2a 20 28 73 71 6c 69 74 65 4d 61 6c  o .** (sqliteMal
127b2 6c 6f 63 52 61 77 29 2c 20 69 74 20 69 73 20 6e  locRaw), it is n
127b3 6f 74 20 70 6f 73 73 69 62 6c 65 20 74 6f 20 75  ot possible to u
127b4 73 65 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 63  se conditional c
127b5 6f 6d 70 69 6c 61 74 69 6f 6e 2e 0a 2a 2a 20 53  ompilation..** S
127b6 6f 2c 20 74 68 69 73 20 6d 61 63 72 6f 20 69 73  o, this macro is
127b7 20 64 65 66 69 6e 65 64 20 69 6e 73 74 65 61 64   defined instead
127b8 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
127b9 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
127ba 55 55 4d 0a 23 64 65 66 69 6e 65 20 49 53 41 55  UUM.#define ISAU
127bb 54 4f 56 41 43 55 55 4d 20 28 70 42 74 2d 3e 61  TOVACUUM (pBt->a
127bc 75 74 6f 56 61 63 75 75 6d 29 0a 23 65 6c 73 65  utoVacuum).#else
127bd 0a 23 64 65 66 69 6e 65 20 49 53 41 55 54 4f 56  .#define ISAUTOV
127be 41 43 55 55 4d 20 30 0a 23 65 6e 64 69 66 0a 0a  ACUUM 0.#endif..
127bf 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 73 74 72 75  ./*.** This stru
127c0 63 74 75 72 65 20 69 73 20 70 61 73 73 65 64 20  cture is passed 
127c1 61 72 6f 75 6e 64 20 74 68 72 6f 75 67 68 20 61  around through a
127c2 6c 6c 20 74 68 65 20 73 61 6e 69 74 79 20 63 68  ll the sanity ch
127c3 65 63 6b 69 6e 67 20 72 6f 75 74 69 6e 65 73 0a  ecking routines.
127c4 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6b  ** in order to k
127c5 65 65 70 20 74 72 61 63 6b 20 6f 66 20 73 6f 6d  eep track of som
127c6 65 20 67 6c 6f 62 61 6c 20 73 74 61 74 65 20 69  e global state i
127c7 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 74  nformation..*/.t
127c8 79 70 65 64 65 66 20 73 74 72 75 63 74 20 49 6e  ypedef struct In
127c9 74 65 67 72 69 74 79 43 6b 20 49 6e 74 65 67 72  tegrityCk Integr
127ca 69 74 79 43 6b 3b 0a 73 74 72 75 63 74 20 49 6e  ityCk;.struct In
127cb 74 65 67 72 69 74 79 43 6b 20 7b 0a 20 20 42 74  tegrityCk {.  Bt
127cc 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20 20 20  Shared *pBt;    
127cd 2f 2a 20 54 68 65 20 74 72 65 65 20 62 65 69 6e  /* The tree bein
127ce 67 20 63 68 65 63 6b 65 64 20 6f 75 74 20 2a 2f  g checked out */
127cf 0a 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72  .  Pager *pPager
127d0 3b 20 20 20 20 2f 2a 20 54 68 65 20 61 73 73 6f  ;    /* The asso
127d1 63 69 61 74 65 64 20 70 61 67 65 72 2e 20 20 41  ciated pager.  A
127d2 6c 73 6f 20 61 63 63 65 73 73 69 62 6c 65 20 62  lso accessible b
127d3 79 20 70 42 74 2d 3e 70 50 61 67 65 72 20 2a 2f  y pBt->pPager */
127d4 0a 20 20 50 67 6e 6f 20 6e 50 61 67 65 3b 20 20  .  Pgno nPage;  
127d5 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
127d6 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 64  f pages in the d
127d7 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74  atabase */.  int
127d8 20 2a 61 6e 52 65 66 3b 20 20 20 20 20 20 20 2f   *anRef;       /
127d9 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 69 6d 65  * Number of time
127da 73 20 65 61 63 68 20 70 61 67 65 20 69 73 20 72  s each page is r
127db 65 66 65 72 65 6e 63 65 64 20 2a 2f 0a 20 20 69  eferenced */.  i
127dc 6e 74 20 6d 78 45 72 72 3b 20 20 20 20 20 20 20  nt mxErr;       
127dd 20 2f 2a 20 53 74 6f 70 20 61 63 63 75 6d 75 6c   /* Stop accumul
127de 61 74 69 6e 67 20 65 72 72 6f 72 73 20 77 68 65  ating errors whe
127df 6e 20 74 68 69 73 20 72 65 61 63 68 65 73 20 7a  n this reaches z
127e0 65 72 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72  ero */.  int nEr
127e1 72 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  r;         /* Nu
127e2 6d 62 65 72 20 6f 66 20 6d 65 73 73 61 67 65 73  mber of messages
127e3 20 77 72 69 74 74 65 6e 20 74 6f 20 7a 45 72 72   written to zErr
127e4 4d 73 67 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  Msg so far */.  
127e5 69 6e 74 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  int mallocFailed
127e6 3b 20 2f 2a 20 41 20 6d 65 6d 6f 72 79 20 61 6c  ; /* A memory al
127e7 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f 72 20 68  location error h
127e8 61 73 20 6f 63 63 75 72 72 65 64 20 2a 2f 0a 20  as occurred */. 
127e9 20 53 74 72 41 63 63 75 6d 20 65 72 72 4d 73 67   StrAccum errMsg
127ea 3b 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65  ;  /* Accumulate
127eb 20 74 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61   the error messa
127ec 67 65 20 74 65 78 74 20 68 65 72 65 20 2a 2f 0a  ge text here */.
127ed 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 6f  };../*.** Read o
127ee 72 20 77 72 69 74 65 20 61 20 74 77 6f 2d 20 61  r write a two- a
127ef 6e 64 20 66 6f 75 72 2d 62 79 74 65 20 62 69 67  nd four-byte big
127f0 2d 65 6e 64 69 61 6e 20 69 6e 74 65 67 65 72 20  -endian integer 
127f1 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 64 65 66 69  values..*/.#defi
127f2 6e 65 20 67 65 74 32 62 79 74 65 28 78 29 20 20  ne get2byte(x)  
127f3 20 28 28 78 29 5b 30 5d 3c 3c 38 20 7c 20 28 78   ((x)[0]<<8 | (x
127f4 29 5b 31 5d 29 0a 23 64 65 66 69 6e 65 20 70 75  )[1]).#define pu
127f5 74 32 62 79 74 65 28 70 2c 76 29 20 28 28 70 29  t2byte(p,v) ((p)
127f6 5b 30 5d 20 3d 20 28 76 29 3e 3e 38 2c 20 28 70  [0] = (v)>>8, (p
127f7 29 5b 31 5d 20 3d 20 28 76 29 29 0a 23 64 65 66  )[1] = (v)).#def
127f8 69 6e 65 20 67 65 74 34 62 79 74 65 20 73 71 6c  ine get4byte sql
127f9 69 74 65 33 47 65 74 34 62 79 74 65 0a 23 64 65  ite3Get4byte.#de
127fa 66 69 6e 65 20 70 75 74 34 62 79 74 65 20 73 71  fine put4byte sq
127fb 6c 69 74 65 33 50 75 74 34 62 79 74 65 0a 0a 2f  lite3Put4byte../
127fc 2a 0a 2a 2a 20 49 6e 74 65 72 6e 61 6c 20 72 6f  *.** Internal ro
127fd 75 74 69 6e 65 73 20 74 68 61 74 20 73 68 6f 75  utines that shou
127fe 6c 64 20 62 65 20 61 63 63 65 73 73 65 64 20 62  ld be accessed b
127ff 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  y the btree laye
12800 72 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54  r only..*/.SQLIT
12801 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12802 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
12803 65 28 42 74 53 68 61 72 65 64 2a 2c 20 50 67 6e  e(BtShared*, Pgn
12804 6f 2c 20 4d 65 6d 50 61 67 65 2a 2a 2c 20 69 6e  o, MemPage**, in
12805 74 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  t);.SQLITE_PRIVA
12806 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
12807 72 65 65 49 6e 69 74 50 61 67 65 28 4d 65 6d 50  reeInitPage(MemP
12808 61 67 65 20 2a 70 50 61 67 65 29 3b 0a 53 51 4c  age *pPage);.SQL
12809 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1280a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
1280b 73 65 43 65 6c 6c 50 74 72 28 4d 65 6d 50 61 67  seCellPtr(MemPag
1280c 65 2a 2c 20 75 38 2a 2c 20 43 65 6c 6c 49 6e 66  e*, u8*, CellInf
1280d 6f 2a 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56  o*);.SQLITE_PRIV
1280e 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1280f 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28 4d  BtreeParseCell(M
12810 65 6d 50 61 67 65 2a 2c 20 69 6e 74 2c 20 43 65  emPage*, int, Ce
12811 6c 6c 49 6e 66 6f 2a 29 3b 0a 53 51 4c 49 54 45  llInfo*);.SQLITE
12812 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
12813 69 74 65 33 42 74 72 65 65 52 65 73 74 6f 72 65  ite3BtreeRestore
12814 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 42  CursorPosition(B
12815 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 3b 0a  tCursor *pCur);.
12816 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
12817 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65  oid sqlite3Btree
12818 47 65 74 54 65 6d 70 43 75 72 73 6f 72 28 42 74  GetTempCursor(Bt
12819 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 42 74  Cursor *pCur, Bt
1281a 43 75 72 73 6f 72 20 2a 70 54 65 6d 70 43 75 72  Cursor *pTempCur
1281b 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  );.SQLITE_PRIVAT
1281c 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
1281d 72 65 65 52 65 6c 65 61 73 65 54 65 6d 70 43 75  reeReleaseTempCu
1281e 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20 2a 70  rsor(BtCursor *p
1281f 43 75 72 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Cur);.SQLITE_PRI
12820 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
12821 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65  3BtreeMoveToPare
12822 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  nt(BtCursor *pCu
12823 72 29 3b 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r);../**********
12824 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 62 74 72 65  **** End of btre
12825 65 49 6e 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eInt.h *********
12826 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12827 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12828 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
12829 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20  **** Continuing 
1282a 77 68 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66  where we left of
1282b 66 20 69 6e 20 62 74 6d 75 74 65 78 2e 63 20 2a  f in btmutex.c *
1282c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1282d 2a 2a 2a 2f 0a 23 69 66 20 53 51 4c 49 54 45 5f  ***/.#if SQLITE_
1282e 54 48 52 45 41 44 53 41 46 45 20 26 26 20 21 64  THREADSAFE && !d
1282f 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
12830 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 29  IT_SHARED_CACHE)
12831 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 61  .../*.** Enter a
12832 20 6d 75 74 65 78 20 6f 6e 20 74 68 65 20 67 69   mutex on the gi
12833 76 65 6e 20 42 54 72 65 65 20 6f 62 6a 65 63 74  ven BTree object
12834 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6f  ..**.** If the o
12835 62 6a 65 63 74 20 69 73 20 6e 6f 74 20 73 68 61  bject is not sha
12836 72 61 62 6c 65 2c 20 74 68 65 6e 20 6e 6f 20 6d  rable, then no m
12837 75 74 65 78 20 69 73 20 65 76 65 72 20 72 65 71  utex is ever req
12838 75 69 72 65 64 0a 2a 2a 20 61 6e 64 20 74 68 69  uired.** and thi
12839 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
1283a 6f 2d 6f 70 2e 20 20 54 68 65 20 75 6e 64 65 72  o-op.  The under
1283b 6c 79 69 6e 67 20 6d 75 74 65 78 20 69 73 20 6e  lying mutex is n
1283c 6f 6e 2d 72 65 63 75 72 73 69 76 65 2e 0a 2a 2a  on-recursive..**
1283d 20 42 75 74 20 77 65 20 6b 65 65 70 20 61 20 72   But we keep a r
1283e 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 69  eference count i
1283f 6e 20 42 74 72 65 65 2e 77 61 6e 74 54 6f 4c 6f  n Btree.wantToLo
12840 63 6b 20 73 6f 20 74 68 65 20 62 65 68 61 76 69  ck so the behavi
12841 6f 72 0a 2a 2a 20 6f 66 20 74 68 69 73 20 69 6e  or.** of this in
12842 74 65 72 66 61 63 65 20 69 73 20 72 65 63 75 72  terface is recur
12843 73 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 61  sive..**.** To a
12844 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 73 2c 20  void deadlocks, 
12845 6d 75 6c 74 69 70 6c 65 20 42 74 72 65 65 73 20  multiple Btrees 
12846 61 72 65 20 6c 6f 63 6b 65 64 20 69 6e 20 74 68  are locked in th
12847 65 20 73 61 6d 65 20 6f 72 64 65 72 0a 2a 2a 20  e same order.** 
12848 62 79 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  by all database 
12849 63 6f 6e 6e 65 63 74 69 6f 6e 73 2e 20 20 54 68  connections.  Th
1284a 65 20 70 2d 3e 70 4e 65 78 74 20 69 73 20 61 20  e p->pNext is a 
1284b 6c 69 73 74 20 6f 66 20 6f 74 68 65 72 0a 2a 2a  list of other.**
1284c 20 42 74 72 65 65 73 20 62 65 6c 6f 6e 67 69 6e   Btrees belongin
1284d 67 20 74 6f 20 74 68 65 20 73 61 6d 65 20 64 61  g to the same da
1284e 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1284f 6e 20 61 73 20 74 68 65 20 70 20 42 74 72 65 65  n as the p Btree
12850 0a 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74  .** which need t
12851 6f 20 62 65 20 6c 6f 63 6b 65 64 20 61 66 74 65  o be locked afte
12852 72 20 70 2e 20 20 49 66 20 77 65 20 63 61 6e 6e  r p.  If we cann
12853 6f 74 20 67 65 74 20 61 20 6c 6f 63 6b 20 6f 6e  ot get a lock on
12854 0a 2a 2a 20 70 2c 20 74 68 65 6e 20 66 69 72 73  .** p, then firs
12855 74 20 75 6e 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20  t unlock all of 
12856 74 68 65 20 6f 74 68 65 72 73 20 6f 6e 20 70 2d  the others on p-
12857 3e 70 4e 65 78 74 2c 20 74 68 65 6e 20 77 61 69  >pNext, then wai
12858 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f 63  t.** for the loc
12859 6b 20 74 6f 20 62 65 63 6f 6d 65 20 61 76 61 69  k to become avai
1285a 6c 61 62 6c 65 20 6f 6e 20 70 2c 20 74 68 65 6e  lable on p, then
1285b 20 72 65 6c 6f 63 6b 20 61 6c 6c 20 6f 66 20 74   relock all of t
1285c 68 65 0a 2a 2a 20 73 75 62 73 65 71 75 65 6e 74  he.** subsequent
1285d 20 42 74 72 65 65 73 20 74 68 61 74 20 64 65 73   Btrees that des
1285e 69 72 65 20 61 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53  ire a lock..*/.S
1285f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
12860 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  id sqlite3BtreeE
12861 6e 74 65 72 28 42 74 72 65 65 20 2a 70 29 7b 0a  nter(Btree *p){.
12862 20 20 42 74 72 65 65 20 2a 70 4c 61 74 65 72 3b    Btree *pLater;
12863 0a 0a 20 20 2f 2a 20 53 6f 6d 65 20 62 61 73 69  ..  /* Some basi
12864 63 20 73 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  c sanity checkin
12865 67 20 6f 6e 20 74 68 65 20 42 74 72 65 65 2e 20  g on the Btree. 
12866 20 54 68 65 20 6c 69 73 74 20 6f 66 20 42 74 72   The list of Btr
12867 65 65 73 0a 20 20 2a 2a 20 63 6f 6e 6e 65 63 74  ees.  ** connect
12868 65 64 20 62 79 20 70 4e 65 78 74 20 61 6e 64 20  ed by pNext and 
12869 70 50 72 65 76 20 73 68 6f 75 6c 64 20 62 65 20  pPrev should be 
1286a 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72 20  in sorted order 
1286b 62 79 0a 20 20 2a 2a 20 42 74 72 65 65 2e 70 42  by.  ** Btree.pB
1286c 74 20 76 61 6c 75 65 2e 20 41 6c 6c 20 65 6c 65  t value. All ele
1286d 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 6c 69 73  ments of the lis
1286e 74 20 73 68 6f 75 6c 64 20 62 65 6c 6f 6e 67 20  t should belong 
1286f 74 6f 0a 20 20 2a 2a 20 74 68 65 20 73 61 6d 65  to.  ** the same
12870 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 20 4f 6e 6c   connection. Onl
12871 79 20 73 68 61 72 65 64 20 42 74 72 65 65 73 20  y shared Btrees 
12872 61 72 65 20 6f 6e 20 74 68 65 20 6c 69 73 74 2e  are on the list.
12873 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
12874 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 2d 3e  >pNext==0 || p->
12875 70 4e 65 78 74 2d 3e 70 42 74 3e 70 2d 3e 70 42  pNext->pBt>p->pB
12876 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
12877 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70 2d  ->pPrev==0 || p-
12878 3e 70 50 72 65 76 2d 3e 70 42 74 3c 70 2d 3e 70  >pPrev->pBt<p->p
12879 42 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Bt );.  assert( 
1287a 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70  p->pNext==0 || p
1287b 2d 3e 70 4e 65 78 74 2d 3e 64 62 3d 3d 70 2d 3e  ->pNext->db==p->
1287c 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
1287d 70 2d 3e 70 50 72 65 76 3d 3d 30 20 7c 7c 20 70  p->pPrev==0 || p
1287e 2d 3e 70 50 72 65 76 2d 3e 64 62 3d 3d 70 2d 3e  ->pPrev->db==p->
1287f 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  db );.  assert( 
12880 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 28  p->sharable || (
12881 70 2d 3e 70 4e 65 78 74 3d 3d 30 20 26 26 20 70  p->pNext==0 && p
12882 2d 3e 70 50 72 65 76 3d 3d 30 29 20 29 3b 0a 0a  ->pPrev==0) );..
12883 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 6c    /* Check for l
12884 6f 63 6b 69 6e 67 20 63 6f 6e 73 69 73 74 65 6e  ocking consisten
12885 63 79 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  cy */.  assert( 
12886 21 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d  !p->locked || p-
12887 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b  >wantToLock>0 );
12888 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73 68  .  assert( p->sh
12889 61 72 61 62 6c 65 20 7c 7c 20 70 2d 3e 77 61 6e  arable || p->wan
1288a 74 54 6f 4c 6f 63 6b 3d 3d 30 20 29 3b 0a 0a 20  tToLock==0 );.. 
1288b 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61 6c   /* We should al
1288c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f 63  ready hold a loc
1288d 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  k on the databas
1288e 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
1288f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
12890 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
12891 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
12892 20 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c   if( !p->sharabl
12893 65 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 2d  e ) return;.  p-
12894 3e 77 61 6e 74 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20  >wantToLock++;. 
12895 20 69 66 28 20 70 2d 3e 6c 6f 63 6b 65 64 20 29   if( p->locked )
12896 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a 20 49   return;..  /* I
12897 6e 20 6d 6f 73 74 20 63 61 73 65 73 2c 20 77 65  n most cases, we
12898 20 73 68 6f 75 6c 64 20 62 65 20 61 62 6c 65 20   should be able 
12899 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c  to acquire the l
1289a 6f 63 6b 20 77 65 0a 20 20 2a 2a 20 77 61 6e 74  ock we.  ** want
1289b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67 20   without having 
1289c 74 6f 20 67 6f 20 74 68 72 6f 75 67 68 74 20 74  to go throught t
1289d 68 65 20 61 73 63 65 6e 64 69 6e 67 20 6c 6f 63  he ascending loc
1289e 6b 0a 20 20 2a 2a 20 70 72 6f 63 65 64 75 72 65  k.  ** procedure
1289f 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 2e 20 20   that follows.  
128a0 4a 75 73 74 20 62 65 20 73 75 72 65 20 6e 6f 74  Just be sure not
128a1 20 74 6f 20 62 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a   to block..  */.
128a2 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 6d 75    if( sqlite3_mu
128a3 74 65 78 5f 74 72 79 28 70 2d 3e 70 42 74 2d 3e  tex_try(p->pBt->
128a4 6d 75 74 65 78 29 3d 3d 53 51 4c 49 54 45 5f 4f  mutex)==SQLITE_O
128a5 4b 20 29 7b 0a 20 20 20 20 70 2d 3e 6c 6f 63 6b  K ){.    p->lock
128a6 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
128a7 72 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f  rn;.  }..  /* To
128a8 20 61 76 6f 69 64 20 64 65 61 64 6c 6f 63 6b 2c   avoid deadlock,
128a9 20 66 69 72 73 74 20 72 65 6c 65 61 73 65 20 61   first release a
128aa 6c 6c 20 6c 6f 63 6b 73 20 77 69 74 68 20 61 20  ll locks with a 
128ab 6c 61 72 67 65 72 0a 20 20 2a 2a 20 42 74 53 68  larger.  ** BtSh
128ac 61 72 65 64 20 61 64 64 72 65 73 73 2e 20 20 54  ared address.  T
128ad 68 65 6e 20 61 63 71 75 69 72 65 20 6f 75 72 20  hen acquire our 
128ae 6c 6f 63 6b 2e 20 20 54 68 65 6e 20 72 65 61 63  lock.  Then reac
128af 71 75 69 72 65 0a 20 20 2a 2a 20 74 68 65 20 6f  quire.  ** the o
128b0 74 68 65 72 20 42 74 53 68 61 72 65 64 20 6c 6f  ther BtShared lo
128b1 63 6b 73 20 74 68 61 74 20 77 65 20 75 73 65 64  cks that we used
128b2 20 74 6f 20 68 6f 6c 64 20 69 6e 20 61 73 63 65   to hold in asce
128b3 6e 64 69 6e 67 0a 20 20 2a 2a 20 6f 72 64 65 72  nding.  ** order
128b4 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 4c 61  ..  */.  for(pLa
128b5 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b 20 70 4c  ter=p->pNext; pL
128b6 61 74 65 72 3b 20 70 4c 61 74 65 72 3d 70 4c 61  ater; pLater=pLa
128b7 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
128b8 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d   assert( pLater-
128b9 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20 20  >sharable );.   
128ba 20 61 73 73 65 72 74 28 20 70 4c 61 74 65 72 2d   assert( pLater-
128bb 3e 70 4e 65 78 74 3d 3d 30 20 7c 7c 20 70 4c 61  >pNext==0 || pLa
128bc 74 65 72 2d 3e 70 4e 65 78 74 2d 3e 70 42 74 3e  ter->pNext->pBt>
128bd 70 4c 61 74 65 72 2d 3e 70 42 74 20 29 3b 0a 20  pLater->pBt );. 
128be 20 20 20 61 73 73 65 72 74 28 20 21 70 4c 61 74     assert( !pLat
128bf 65 72 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 4c  er->locked || pL
128c0 61 74 65 72 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b  ater->wantToLock
128c1 3e 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  >0 );.    if( pL
128c2 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a  ater->locked ){.
128c3 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
128c4 74 65 78 5f 6c 65 61 76 65 28 70 4c 61 74 65 72  tex_leave(pLater
128c5 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20  ->pBt->mutex);. 
128c6 20 20 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63       pLater->loc
128c7 6b 65 64 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20  ked = 0;.    }. 
128c8 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74   }.  sqlite3_mut
128c9 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d  ex_enter(p->pBt-
128ca 3e 6d 75 74 65 78 29 3b 0a 20 20 70 2d 3e 6c 6f  >mutex);.  p->lo
128cb 63 6b 65 64 20 3d 20 31 3b 0a 20 20 66 6f 72 28  cked = 1;.  for(
128cc 70 4c 61 74 65 72 3d 70 2d 3e 70 4e 65 78 74 3b  pLater=p->pNext;
128cd 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72 3d   pLater; pLater=
128ce 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a  pLater->pNext){.
128cf 20 20 20 20 69 66 28 20 70 4c 61 74 65 72 2d 3e      if( pLater->
128d0 77 61 6e 74 54 6f 4c 6f 63 6b 20 29 7b 0a 20 20  wantToLock ){.  
128d1 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
128d2 78 5f 65 6e 74 65 72 28 70 4c 61 74 65 72 2d 3e  x_enter(pLater->
128d3 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  pBt->mutex);.   
128d4 20 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65     pLater->locke
128d5 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d  d = 1;.    }.  }
128d6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 69 74 20 74  .}../*.** Exit t
128d7 68 65 20 72 65 63 75 72 73 69 76 65 20 6d 75 74  he recursive mut
128d8 65 78 20 6f 6e 20 61 20 42 74 72 65 65 2e 0a 2a  ex on a Btree..*
128d9 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
128da 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72   void sqlite3Btr
128db 65 65 4c 65 61 76 65 28 42 74 72 65 65 20 2a 70  eeLeave(Btree *p
128dc 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 68 61 72  ){.  if( p->shar
128dd 61 62 6c 65 20 29 7b 0a 20 20 20 20 61 73 73 65  able ){.    asse
128de 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  rt( p->wantToLoc
128df 6b 3e 30 20 29 3b 0a 20 20 20 20 70 2d 3e 77 61  k>0 );.    p->wa
128e0 6e 74 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20  ntToLock--;.    
128e1 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  if( p->wantToLoc
128e2 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73  k==0 ){.      as
128e3 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65 64 20  sert( p->locked 
128e4 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
128e5 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e  _mutex_leave(p->
128e6 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20  pBt->mutex);.   
128e7 20 20 20 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30     p->locked = 0
128e8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23  ;.    }.  }.}..#
128e9 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
128ea 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
128eb 69 66 20 74 68 65 20 42 74 53 68 61 72 65 64 20  if the BtShared 
128ec 6d 75 74 65 78 20 69 73 20 68 65 6c 64 20 6f 6e  mutex is held on
128ed 20 74 68 65 20 62 74 72 65 65 2e 20 20 0a 2a 2a   the btree.  .**
128ee 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
128ef 20 6d 61 6b 65 73 20 6e 6f 20 64 65 74 65 72 6d   makes no determ
128f0 69 6e 61 74 69 6f 6e 20 6f 6e 65 20 77 68 79 20  ination one why 
128f1 6f 72 20 61 6e 6f 74 68 65 72 20 69 66 20 74 68  or another if th
128f2 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  e.** database co
128f3 6e 6e 65 63 74 69 6f 6e 20 6d 75 74 65 78 20 69  nnection mutex i
128f4 73 20 68 65 6c 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  s held..**.** Th
128f5 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 75 73  is routine is us
128f6 65 64 20 6f 6e 6c 79 20 66 72 6f 6d 20 77 69 74  ed only from wit
128f7 68 69 6e 20 61 73 73 65 72 74 28 29 20 73 74 61  hin assert() sta
128f8 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49  tements..*/.SQLI
128f9 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
128fa 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73  qlite3BtreeHolds
128fb 4d 75 74 65 78 28 42 74 72 65 65 20 2a 70 29 7b  Mutex(Btree *p){
128fc 0a 20 20 72 65 74 75 72 6e 20 28 70 2d 3e 73 68  .  return (p->sh
128fd 61 72 61 62 6c 65 3d 3d 30 20 7c 7c 0a 20 20 20  arable==0 ||.   
128fe 20 20 20 20 20 20 20 20 20 20 28 70 2d 3e 6c 6f            (p->lo
128ff 63 6b 65 64 20 26 26 20 70 2d 3e 77 61 6e 74 54  cked && p->wantT
12900 6f 4c 6f 63 6b 20 26 26 20 73 71 6c 69 74 65 33  oLock && sqlite3
12901 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70  _mutex_held(p->p
12902 42 74 2d 3e 6d 75 74 65 78 29 29 29 3b 0a 7d 0a  Bt->mutex)));.}.
12903 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
12904 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
12905 52 42 4c 4f 42 0a 2f 2a 0a 2a 2a 20 45 6e 74 65  RBLOB./*.** Ente
12906 72 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d 75  r and leave a mu
12907 74 65 78 20 6f 6e 20 61 20 42 74 72 65 65 20 67  tex on a Btree g
12908 69 76 65 6e 20 61 20 63 75 72 73 6f 72 20 6f 77  iven a cursor ow
12909 6e 65 64 20 62 79 20 74 68 61 74 0a 2a 2a 20 42  ned by that.** B
1290a 74 72 65 65 2e 20 20 54 68 65 73 65 20 65 6e 74  tree.  These ent
1290b 72 79 20 70 6f 69 6e 74 73 20 61 72 65 20 75 73  ry points are us
1290c 65 64 20 62 79 20 69 6e 63 72 65 6d 65 6e 74 61  ed by incrementa
1290d 6c 20 49 2f 4f 20 61 6e 64 20 63 61 6e 20 62 65  l I/O and can be
1290e 0a 2a 2a 20 6f 6d 69 74 74 65 64 20 69 66 20 74  .** omitted if t
1290f 68 61 74 20 6d 6f 64 75 6c 65 20 69 73 20 6e 6f  hat module is no
12910 74 20 75 73 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  t used..*/.SQLIT
12911 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
12912 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
12913 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72 20  Cursor(BtCursor 
12914 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69 74 65  *pCur){.  sqlite
12915 33 42 74 72 65 65 45 6e 74 65 72 28 70 43 75 72  3BtreeEnter(pCur
12916 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a 53 51 4c  ->pBtree);.}.SQL
12917 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
12918 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
12919 76 65 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f  veCursor(BtCurso
1291a 72 20 2a 70 43 75 72 29 7b 0a 20 20 73 71 6c 69  r *pCur){.  sqli
1291b 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 43  te3BtreeLeave(pC
1291c 75 72 2d 3e 70 42 74 72 65 65 29 3b 0a 7d 0a 23  ur->pBtree);.}.#
1291d 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1291e 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 20 2a 2f  OMIT_INCRBLOB */
1291f 0a 0a 0a 2f 2a 0a 2a 2a 20 45 6e 74 65 72 20 74  .../*.** Enter t
12920 68 65 20 6d 75 74 65 78 20 6f 6e 20 65 76 65 72  he mutex on ever
12921 79 20 42 74 72 65 65 20 61 73 73 6f 63 69 61 74  y Btree associat
12922 65 64 20 77 69 74 68 20 61 20 64 61 74 61 62 61  ed with a databa
12923 73 65 0a 2a 2a 20 63 6f 6e 6e 65 63 74 69 6f 6e  se.** connection
12924 2e 20 20 54 68 69 73 20 69 73 20 6e 65 65 64 65  .  This is neede
12925 64 20 28 66 6f 72 20 65 78 61 6d 70 6c 65 29 20  d (for example) 
12926 70 72 69 6f 72 20 74 6f 20 70 61 72 73 69 6e 67  prior to parsing
12927 0a 2a 2a 20 61 20 73 74 61 74 65 6d 65 6e 74 20  .** a statement 
12928 73 69 6e 63 65 20 77 65 20 77 69 6c 6c 20 62 65  since we will be
12929 20 63 6f 6d 70 61 72 69 6e 67 20 74 61 62 6c 65   comparing table
1292a 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   and column name
1292b 73 0a 2a 2a 20 61 67 61 69 6e 73 74 20 61 6c 6c  s.** against all
1292c 20 73 63 68 65 6d 61 73 20 61 6e 64 20 77 65 20   schemas and we 
1292d 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 6f 73  do not want thos
1292e 65 20 73 63 68 65 6d 61 73 20 62 65 69 6e 67 0a  e schemas being.
1292f 2a 2a 20 72 65 73 65 74 20 6f 75 74 20 66 72 6f  ** reset out fro
12930 6d 20 75 6e 64 65 72 20 75 73 2e 0a 2a 2a 0a 2a  m under us..**.*
12931 2a 20 54 68 65 72 65 20 69 73 20 61 20 63 6f 72  * There is a cor
12932 72 65 73 70 6f 6e 64 69 6e 67 20 6c 65 61 76 65  responding leave
12933 2d 61 6c 6c 20 70 72 6f 63 65 64 75 72 65 73 2e  -all procedures.
12934 0a 2a 2a 0a 2a 2a 20 45 6e 74 65 72 20 74 68 65  .**.** Enter the
12935 20 6d 75 74 65 78 65 73 20 69 6e 20 61 63 63 65   mutexes in acce
12936 6e 64 69 6e 67 20 6f 72 64 65 72 20 62 79 20 42  nding order by B
12937 74 53 68 61 72 65 64 20 70 6f 69 6e 74 65 72 20  tShared pointer 
12938 61 64 64 72 65 73 73 0a 2a 2a 20 74 6f 20 61 76  address.** to av
12939 6f 69 64 20 74 68 65 20 70 6f 73 73 69 62 69 6c  oid the possibil
1293a 69 74 79 20 6f 66 20 64 65 61 64 6c 6f 63 6b 20  ity of deadlock 
1293b 77 68 65 6e 20 74 77 6f 20 74 68 72 65 61 64 73  when two threads
1293c 20 77 69 74 68 0a 2a 2a 20 74 77 6f 20 6f 72 20   with.** two or 
1293d 6d 6f 72 65 20 62 74 72 65 65 73 20 69 6e 20 63  more btrees in c
1293e 6f 6d 6d 6f 6e 20 62 6f 74 68 20 74 72 79 20 74  ommon both try t
1293f 6f 20 6c 6f 63 6b 20 61 6c 6c 20 74 68 65 69 72  o lock all their
12940 20 62 74 72 65 65 73 0a 2a 2a 20 61 74 20 74 68   btrees.** at th
12941 65 20 73 61 6d 65 20 69 6e 73 74 61 6e 74 2e 0a  e same instant..
12942 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
12943 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74  E void sqlite3Bt
12944 72 65 65 45 6e 74 65 72 41 6c 6c 28 73 71 6c 69  reeEnterAll(sqli
12945 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
12946 69 3b 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 2a  i;.  Btree *p, *
12947 70 4c 61 74 65 72 3b 0a 20 20 61 73 73 65 72 74  pLater;.  assert
12948 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
12949 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
1294a 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
1294b 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1294c 20 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69     p = db->aDb[i
1294d 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
1294e 20 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20   && p->sharable 
1294f 29 7b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74  ){.      p->want
12950 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 20 20  ToLock++;.      
12951 69 66 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 29  if( !p->locked )
12952 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  {.        assert
12953 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
12954 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 77 68  =1 );.        wh
12955 69 6c 65 28 20 70 2d 3e 70 50 72 65 76 20 29 20  ile( p->pPrev ) 
12956 70 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a 20 20  p = p->pPrev;.  
12957 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 2d 3e        while( p->
12958 6c 6f 63 6b 65 64 20 26 26 20 70 2d 3e 70 4e 65  locked && p->pNe
12959 78 74 20 29 20 70 20 3d 20 70 2d 3e 70 4e 65 78  xt ) p = p->pNex
1295a 74 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70  t;.        for(p
1295b 4c 61 74 65 72 20 3d 20 70 2d 3e 70 4e 65 78 74  Later = p->pNext
1295c 3b 20 70 4c 61 74 65 72 3b 20 70 4c 61 74 65 72  ; pLater; pLater
1295d 3d 70 4c 61 74 65 72 2d 3e 70 4e 65 78 74 29 7b  =pLater->pNext){
1295e 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
1295f 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64 20 29 7b  Later->locked ){
12960 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
12961 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
12962 28 70 4c 61 74 65 72 2d 3e 70 42 74 2d 3e 6d 75  (pLater->pBt->mu
12963 74 65 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20  tex);.          
12964 20 20 70 4c 61 74 65 72 2d 3e 6c 6f 63 6b 65 64    pLater->locked
12965 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20   = 0;.          
12966 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
12967 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a      while( p ){.
12968 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
12969 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d  3_mutex_enter(p-
1296a 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  >pBt->mutex);.  
1296b 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b 65          p->locke
1296c 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 70  d++;.          p
1296d 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20   = p->pNext;.   
1296e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
1296f 20 20 20 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54     }.  }.}.SQLIT
12970 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
12971 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
12972 41 6c 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 29  All(sqlite3 *db)
12973 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 42 74 72  {.  int i;.  Btr
12974 65 65 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28  ee *p;.  assert(
12975 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
12976 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
12977 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64  ;.  for(i=0; i<d
12978 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
12979 20 20 70 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d    p = db->aDb[i]
1297a 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70 20  .pBt;.    if( p 
1297b 26 26 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29  && p->sharable )
1297c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
1297d 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20  p->wantToLock>0 
1297e 29 3b 0a 20 20 20 20 20 20 70 2d 3e 77 61 6e 74  );.      p->want
1297f 54 6f 4c 6f 63 6b 2d 2d 3b 0a 20 20 20 20 20 20  ToLock--;.      
12980 69 66 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63  if( p->wantToLoc
12981 6b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  k==0 ){.        
12982 61 73 73 65 72 74 28 20 70 2d 3e 6c 6f 63 6b 65  assert( p->locke
12983 64 20 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  d );.        sql
12984 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
12985 28 70 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 3b  (p->pBt->mutex);
12986 0a 20 20 20 20 20 20 20 20 70 2d 3e 6c 6f 63 6b  .        p->lock
12987 65 64 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ed = 0;.      }.
12988 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 23 69 66      }.  }.}..#if
12989 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a 0a 2a  ndef NDEBUG./*.*
1298a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69 66  * Return true if
1298b 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 68 72   the current thr
1298c 65 61 64 20 68 6f 6c 64 73 20 74 68 65 20 64 61  ead holds the da
1298d 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
1298e 6e 0a 2a 2a 20 6d 75 74 65 78 20 61 6e 64 20 61  n.** mutex and a
1298f 6c 6c 20 72 65 71 75 69 72 65 64 20 42 74 53 68  ll required BtSh
12990 61 72 65 64 20 6d 75 74 65 78 65 73 2e 0a 2a 2a  ared mutexes..**
12991 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
12992 20 69 73 20 75 73 65 64 20 69 6e 73 69 64 65 20   is used inside 
12993 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
12994 6e 74 73 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51 4c  nts only..*/.SQL
12995 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
12996 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
12997 73 41 6c 6c 4d 75 74 65 78 65 73 28 73 71 6c 69  sAllMutexes(sqli
12998 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
12999 69 3b 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65  i;.  if( !sqlite
1299a 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
1299b 3e 6d 75 74 65 78 29 20 29 7b 0a 20 20 20 20 72  >mutex) ){.    r
1299c 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 66  eturn 0;.  }.  f
1299d 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
1299e 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72  b; i++){.    Btr
1299f 65 65 20 2a 70 3b 0a 20 20 20 20 70 20 3d 20 64  ee *p;.    p = d
129a0 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20  b->aDb[i].pBt;. 
129a1 20 20 20 69 66 28 20 70 20 26 26 20 70 2d 3e 73     if( p && p->s
129a2 68 61 72 61 62 6c 65 20 26 26 0a 20 20 20 20 20  harable &&.     
129a3 20 20 20 20 28 70 2d 3e 77 61 6e 74 54 6f 4c 6f      (p->wantToLo
129a4 63 6b 3d 3d 30 20 7c 7c 20 21 73 71 6c 69 74 65  ck==0 || !sqlite
129a5 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
129a6 70 42 74 2d 3e 6d 75 74 65 78 29 29 20 29 7b 0a  pBt->mutex)) ){.
129a7 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
129a8 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
129a9 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  rn 1;.}.#endif /
129aa 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f 2a 0a  * NDEBUG */../*.
129ab 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 42 74 72  ** Add a new Btr
129ac 65 65 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20  ee pointer to a 
129ad 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 2e  BtreeMutexArray.
129ae 20 0a 2a 2a 20 69 66 20 74 68 65 20 70 6f 69 6e   .** if the poin
129af 74 65 72 20 63 61 6e 20 70 6f 73 73 69 62 6c 79  ter can possibly
129b0 20 62 65 20 73 68 61 72 65 64 20 77 69 74 68 0a   be shared with.
129b1 2a 2a 20 61 6e 6f 74 68 65 72 20 64 61 74 61 62  ** another datab
129b2 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a  ase connection..
129b3 2a 2a 0a 2a 2a 20 54 68 65 20 70 6f 69 6e 74 65  **.** The pointe
129b4 72 73 20 61 72 65 20 6b 65 70 74 20 69 6e 20 73  rs are kept in s
129b5 6f 72 74 65 64 20 6f 72 64 65 72 20 62 79 20 70  orted order by p
129b6 42 74 72 65 65 2d 3e 70 42 74 2e 20 20 54 68 61  Btree->pBt.  Tha
129b7 74 0a 2a 2a 20 77 61 79 20 77 68 65 6e 20 77 65  t.** way when we
129b8 20 67 6f 20 74 6f 20 65 6e 74 65 72 20 61 6c 6c   go to enter all
129b9 20 74 68 65 20 6d 75 74 65 78 65 73 2c 20 77 65   the mutexes, we
129ba 20 63 61 6e 20 65 6e 74 65 72 20 74 68 65 6d 0a   can enter them.
129bb 2a 2a 20 69 6e 20 6f 72 64 65 72 20 77 69 74 68  ** in order with
129bc 6f 75 74 20 65 76 65 72 79 20 68 61 76 69 6e 67  out every having
129bd 20 74 6f 20 62 61 63 6b 75 70 20 61 6e 64 20 72   to backup and r
129be 65 74 72 79 20 61 6e 64 20 77 69 74 68 6f 75 74  etry and without
129bf 0a 2a 2a 20 77 6f 72 72 79 69 6e 67 20 61 62 6f  .** worrying abo
129c0 75 74 20 64 65 61 64 6c 6f 63 6b 2e 0a 2a 2a 0a  ut deadlock..**.
129c1 2a 2a 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66  ** The number of
129c2 20 73 68 61 72 65 64 20 62 74 72 65 65 73 20 77   shared btrees w
129c3 69 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 73 6d  ill always be sm
129c4 61 6c 6c 20 28 75 73 75 61 6c 6c 79 20 30 20 6f  all (usually 0 o
129c5 72 20 31 29 0a 2a 2a 20 73 6f 20 61 6e 20 69 6e  r 1).** so an in
129c6 73 65 72 74 69 6f 6e 20 73 6f 72 74 20 69 73 20  sertion sort is 
129c7 61 6e 20 61 64 65 71 75 61 74 65 20 61 6c 67 6f  an adequate algo
129c8 72 69 74 68 6d 20 68 65 72 65 2e 0a 2a 2f 0a 53  rithm here..*/.S
129c9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
129ca 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
129cb 75 74 65 78 41 72 72 61 79 49 6e 73 65 72 74 28  utexArrayInsert(
129cc 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20  BtreeMutexArray 
129cd 2a 70 41 72 72 61 79 2c 20 42 74 72 65 65 20 2a  *pArray, Btree *
129ce 70 42 74 72 65 65 29 7b 0a 20 20 69 6e 74 20 69  pBtree){.  int i
129cf 2c 20 6a 3b 0a 20 20 42 74 53 68 61 72 65 64 20  , j;.  BtShared 
129d0 2a 70 42 74 3b 0a 20 20 69 66 28 20 70 42 74 72  *pBt;.  if( pBtr
129d1 65 65 3d 3d 30 20 7c 7c 20 70 42 74 72 65 65 2d  ee==0 || pBtree-
129d2 3e 73 68 61 72 61 62 6c 65 3d 3d 30 20 29 20 72  >sharable==0 ) r
129d3 65 74 75 72 6e 3b 0a 23 69 66 6e 64 65 66 20 4e  eturn;.#ifndef N
129d4 44 45 42 55 47 0a 20 20 7b 0a 20 20 20 20 66 6f  DEBUG.  {.    fo
129d5 72 28 69 3d 30 3b 20 69 3c 70 41 72 72 61 79 2d  r(i=0; i<pArray-
129d6 3e 6e 4d 75 74 65 78 3b 20 69 2b 2b 29 7b 0a 20  >nMutex; i++){. 
129d7 20 20 20 20 20 61 73 73 65 72 74 28 20 70 41 72       assert( pAr
129d8 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d 21 3d  ray->aBtree[i]!=
129d9 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 7d 0a  pBtree );.    }.
129da 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 61 73 73    }.#endif.  ass
129db 65 72 74 28 20 70 41 72 72 61 79 2d 3e 6e 4d 75  ert( pArray->nMu
129dc 74 65 78 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65  tex>=0 );.  asse
129dd 72 74 28 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74  rt( pArray->nMut
129de 65 78 3c 41 72 72 61 79 53 69 7a 65 28 70 41 72  ex<ArraySize(pAr
129df 72 61 79 2d 3e 61 42 74 72 65 65 29 2d 31 20 29  ray->aBtree)-1 )
129e0 3b 0a 20 20 70 42 74 20 3d 20 70 42 74 72 65 65  ;.  pBt = pBtree
129e1 2d 3e 70 42 74 3b 0a 20 20 66 6f 72 28 69 3d 30  ->pBt;.  for(i=0
129e2 3b 20 69 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74  ; i<pArray->nMut
129e3 65 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 61 73  ex; i++){.    as
129e4 73 65 72 74 28 20 70 41 72 72 61 79 2d 3e 61 42  sert( pArray->aB
129e5 74 72 65 65 5b 69 5d 21 3d 70 42 74 72 65 65 20  tree[i]!=pBtree 
129e6 29 3b 0a 20 20 20 20 69 66 28 20 70 41 72 72 61  );.    if( pArra
129e7 79 2d 3e 61 42 74 72 65 65 5b 69 5d 2d 3e 70 42  y->aBtree[i]->pB
129e8 74 3e 70 42 74 20 29 7b 0a 20 20 20 20 20 20 66  t>pBt ){.      f
129e9 6f 72 28 6a 3d 70 41 72 72 61 79 2d 3e 6e 4d 75  or(j=pArray->nMu
129ea 74 65 78 3b 20 6a 3e 69 3b 20 6a 2d 2d 29 7b 0a  tex; j>i; j--){.
129eb 20 20 20 20 20 20 20 20 70 41 72 72 61 79 2d 3e          pArray->
129ec 61 42 74 72 65 65 5b 6a 5d 20 3d 20 70 41 72 72  aBtree[j] = pArr
129ed 61 79 2d 3e 61 42 74 72 65 65 5b 6a 2d 31 5d 3b  ay->aBtree[j-1];
129ee 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
129ef 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69 5d  Array->aBtree[i]
129f0 20 3d 20 70 42 74 72 65 65 3b 0a 20 20 20 20 20   = pBtree;.     
129f1 20 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 2b   pArray->nMutex+
129f2 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  +;.      return;
129f3 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 41 72  .    }.  }.  pAr
129f4 72 61 79 2d 3e 61 42 74 72 65 65 5b 70 41 72 72  ray->aBtree[pArr
129f5 61 79 2d 3e 6e 4d 75 74 65 78 2b 2b 5d 20 3d 20  ay->nMutex++] = 
129f6 70 42 74 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pBtree;.}../*.**
129f7 20 45 6e 74 65 72 20 74 68 65 20 6d 75 74 65 78   Enter the mutex
129f8 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20   of every btree 
129f9 69 6e 20 74 68 65 20 61 72 72 61 79 2e 20 20 54  in the array.  T
129fa 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 0a 2a  his routine is.*
129fb 2a 20 63 61 6c 6c 65 64 20 61 74 20 74 68 65 20  * called at the 
129fc 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 73 71 6c  beginning of sql
129fd 69 74 65 33 56 64 62 65 45 78 65 63 28 29 2e 20  ite3VdbeExec(). 
129fe 20 54 68 65 20 6d 75 74 65 78 65 73 20 61 72 65   The mutexes are
129ff 0a 2a 2a 20 65 78 69 74 65 64 20 61 74 20 74 68  .** exited at th
12a00 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 61 6d  e end of the sam
12a01 65 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  e function..*/.S
12a02 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
12a03 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  id sqlite3BtreeM
12a04 75 74 65 78 41 72 72 61 79 45 6e 74 65 72 28 42  utexArrayEnter(B
12a05 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 20 2a  treeMutexArray *
12a06 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74 20 69  pArray){.  int i
12a07 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
12a08 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b 20 69  Array->nMutex; i
12a09 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a  ++){.    Btree *
12a0a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42 74 72  p = pArray->aBtr
12a0b 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20 53 6f  ee[i];.    /* So
12a0c 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74 79 20  me basic sanity 
12a0d 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20 20 20  checking */.    
12a0e 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c 7c 20  assert( i==0 || 
12a0f 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65 5b 69  pArray->aBtree[i
12a10 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42 74 20  -1]->pBt<p->pBt 
12a11 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21  );.    assert( !
12a12 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 70 2d 3e  p->locked || p->
12a13 77 61 6e 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a  wantToLock>0 );.
12a14 0a 20 20 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c  .    /* We shoul
12a15 64 20 61 6c 72 65 61 64 79 20 68 6f 6c 64 20 61  d already hold a
12a16 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 64 61 74   lock on the dat
12a17 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
12a18 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
12a19 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
12a1a 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
12a1b 20 29 3b 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74   );..    p->want
12a1c 54 6f 4c 6f 63 6b 2b 2b 3b 0a 20 20 20 20 69 66  ToLock++;.    if
12a1d 28 20 21 70 2d 3e 6c 6f 63 6b 65 64 20 26 26 20  ( !p->locked && 
12a1e 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
12a1f 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
12a20 65 78 5f 65 6e 74 65 72 28 70 2d 3e 70 42 74 2d  ex_enter(p->pBt-
12a21 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20 70  >mutex);.      p
12a22 2d 3e 6c 6f 63 6b 65 64 20 3d 20 31 3b 0a 20 20  ->locked = 1;.  
12a23 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
12a24 20 4c 65 61 76 65 20 74 68 65 20 6d 75 74 65 78   Leave the mutex
12a25 20 6f 66 20 65 76 65 72 79 20 62 74 72 65 65 20   of every btree 
12a26 69 6e 20 74 68 65 20 67 72 6f 75 70 2e 0a 2a 2f  in the group..*/
12a27 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12a28 76 6f 69 64 20 73 71 6c 69 74 65 33 42 74 72 65  void sqlite3Btre
12a29 65 4d 75 74 65 78 41 72 72 61 79 4c 65 61 76 65  eMutexArrayLeave
12a2a 28 42 74 72 65 65 4d 75 74 65 78 41 72 72 61 79  (BtreeMutexArray
12a2b 20 2a 70 41 72 72 61 79 29 7b 0a 20 20 69 6e 74   *pArray){.  int
12a2c 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   i;.  for(i=0; i
12a2d 3c 70 41 72 72 61 79 2d 3e 6e 4d 75 74 65 78 3b  <pArray->nMutex;
12a2e 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74 72 65 65   i++){.    Btree
12a2f 20 2a 70 20 3d 20 70 41 72 72 61 79 2d 3e 61 42   *p = pArray->aB
12a30 74 72 65 65 5b 69 5d 3b 0a 20 20 20 20 2f 2a 20  tree[i];.    /* 
12a31 53 6f 6d 65 20 62 61 73 69 63 20 73 61 6e 69 74  Some basic sanit
12a32 79 20 63 68 65 63 6b 69 6e 67 20 2a 2f 0a 20 20  y checking */.  
12a33 20 20 61 73 73 65 72 74 28 20 69 3d 3d 30 20 7c    assert( i==0 |
12a34 7c 20 70 41 72 72 61 79 2d 3e 61 42 74 72 65 65  | pArray->aBtree
12a35 5b 69 2d 31 5d 2d 3e 70 42 74 3c 70 2d 3e 70 42  [i-1]->pBt<p->pB
12a36 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  t );.    assert(
12a37 20 70 2d 3e 6c 6f 63 6b 65 64 20 7c 7c 20 21 70   p->locked || !p
12a38 2d 3e 73 68 61 72 61 62 6c 65 20 29 3b 0a 20 20  ->sharable );.  
12a39 20 20 61 73 73 65 72 74 28 20 70 2d 3e 77 61 6e    assert( p->wan
12a3a 74 54 6f 4c 6f 63 6b 3e 30 20 29 3b 0a 0a 20 20  tToLock>0 );..  
12a3b 20 20 2f 2a 20 57 65 20 73 68 6f 75 6c 64 20 61    /* We should a
12a3c 6c 72 65 61 64 79 20 68 6f 6c 64 20 61 20 6c 6f  lready hold a lo
12a3d 63 6b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61  ck on the databa
12a3e 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
12a3f 0a 20 20 20 20 61 73 73 65 72 74 28 20 73 71 6c  .    assert( sql
12a40 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
12a41 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
12a42 0a 0a 20 20 20 20 70 2d 3e 77 61 6e 74 54 6f 4c  ..    p->wantToL
12a43 6f 63 6b 2d 2d 3b 0a 20 20 20 20 69 66 28 20 70  ock--;.    if( p
12a44 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d 3d 30 20  ->wantToLock==0 
12a45 26 26 20 70 2d 3e 6c 6f 63 6b 65 64 20 29 7b 0a  && p->locked ){.
12a46 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
12a47 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 70 42 74  tex_leave(p->pBt
12a48 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
12a49 70 2d 3e 6c 6f 63 6b 65 64 20 3d 20 30 3b 0a 20  p->locked = 0;. 
12a4a 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 23 65 6e     }.  }.}...#en
12a4b 64 69 66 20 20 2f 2a 20 53 51 4c 49 54 45 5f 54  dif  /* SQLITE_T
12a4c 48 52 45 41 44 53 41 46 45 20 26 26 20 21 53 51  HREADSAFE && !SQ
12a4d 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
12a4e 5f 43 41 43 48 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  _CACHE */../****
12a4f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
12a50 66 20 62 74 6d 75 74 65 78 2e 63 20 2a 2a 2a 2a  f btmutex.c ****
12a51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
12a54 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
12a55 20 66 69 6c 65 20 62 74 72 65 65 2e 63 20 2a 2a   file btree.c **
12a56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a57 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a58 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
12a59 20 32 30 30 34 20 41 70 72 69 6c 20 36 0a 2a 2a   2004 April 6.**
12a5a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
12a5b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
12a5c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
12a5d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
12a5e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
12a5f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
12a60 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
12a61 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
12a62 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
12a63 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
12a64 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
12a65 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
12a66 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
12a67 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
12a68 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
12a69 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
12a6a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
12a6b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
12a6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a6e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
12a6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
12a70 24 49 64 3a 20 62 74 72 65 65 2e 63 2c 76 20 31  $Id: btree.c,v 1
12a71 2e 35 33 39 2e 32 2e 31 20 32 30 30 38 2f 31 31  .539.2.1 2008/11
12a72 2f 32 32 20 31 34 3a 30 37 3a 34 39 20 64 72 68  /22 14:07:49 drh
12a73 20 45 78 70 20 24 0a 2a 2a 0a 2a 2a 20 54 68 69   Exp $.**.** Thi
12a74 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74  s file implement
12a75 73 20 61 20 65 78 74 65 72 6e 61 6c 20 28 64 69  s a external (di
12a76 73 6b 2d 62 61 73 65 64 29 20 64 61 74 61 62 61  sk-based) databa
12a77 73 65 20 75 73 69 6e 67 20 42 54 72 65 65 73 2e  se using BTrees.
12a78 0a 2a 2a 20 53 65 65 20 74 68 65 20 68 65 61 64  .** See the head
12a79 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 22 62  er comment on "b
12a7a 74 72 65 65 49 6e 74 2e 68 22 20 66 6f 72 20 61  treeInt.h" for a
12a7b 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
12a7c 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 63 6c 75 64  ation..** Includ
12a7d 69 6e 67 20 61 20 64 65 73 63 72 69 70 74 69 6f  ing a descriptio
12a7e 6e 20 6f 66 20 66 69 6c 65 20 66 6f 72 6d 61 74  n of file format
12a7f 20 61 6e 64 20 61 6e 20 6f 76 65 72 76 69 65 77   and an overview
12a80 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a   of operation..*
12a81 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68 65 61  /../*.** The hea
12a82 64 65 72 20 73 74 72 69 6e 67 20 74 68 61 74 20  der string that 
12a83 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 62  appears at the b
12a84 65 67 69 6e 6e 69 6e 67 20 6f 66 20 65 76 65 72  eginning of ever
12a85 79 0a 2a 2a 20 53 51 4c 69 74 65 20 64 61 74 61  y.** SQLite data
12a86 62 61 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  base..*/.static 
12a87 63 6f 6e 73 74 20 63 68 61 72 20 7a 4d 61 67 69  const char zMagi
12a88 63 48 65 61 64 65 72 5b 5d 20 3d 20 53 51 4c 49  cHeader[] = SQLI
12a89 54 45 5f 46 49 4c 45 5f 48 45 41 44 45 52 3b 0a  TE_FILE_HEADER;.
12a8a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 69 73 20  ./*.** Set this 
12a8b 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20  global variable 
12a8c 74 6f 20 31 20 74 6f 20 65 6e 61 62 6c 65 20 74  to 1 to enable t
12a8d 72 61 63 69 6e 67 20 75 73 69 6e 67 20 74 68 65  racing using the
12a8e 20 54 52 41 43 45 0a 2a 2a 20 6d 61 63 72 6f 2e   TRACE.** macro.
12a8f 0a 2a 2f 0a 23 69 66 20 30 0a 69 6e 74 20 73 71  .*/.#if 0.int sq
12a90 6c 69 74 65 33 42 74 72 65 65 54 72 61 63 65 3d  lite3BtreeTrace=
12a91 30 3b 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 65  0;  /* True to e
12a92 6e 61 62 6c 65 20 74 72 61 63 69 6e 67 20 2a 2f  nable tracing */
12a93 0a 23 20 64 65 66 69 6e 65 20 54 52 41 43 45 28  .# define TRACE(
12a94 58 29 20 20 69 66 28 73 71 6c 69 74 65 33 42 74  X)  if(sqlite3Bt
12a95 72 65 65 54 72 61 63 65 29 7b 70 72 69 6e 74 66  reeTrace){printf
12a96 20 58 3b 66 66 6c 75 73 68 28 73 74 64 6f 75 74   X;fflush(stdout
12a97 29 3b 7d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69  );}.#else.# defi
12a98 6e 65 20 54 52 41 43 45 28 58 29 0a 23 65 6e 64  ne TRACE(X).#end
12a99 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 74 69  if../*.** Someti
12a9a 6d 65 73 20 77 65 20 6e 65 65 64 20 61 20 73 6d  mes we need a sm
12a9b 61 6c 6c 20 61 6d 6f 75 6e 74 20 6f 66 20 63 6f  all amount of co
12a9c 64 65 20 73 75 63 68 20 61 73 20 61 20 76 61 72  de such as a var
12a9d 69 61 62 6c 65 20 69 6e 69 74 69 61 6c 69 7a 61  iable initializa
12a9e 74 69 6f 6e 0a 2a 2a 20 74 6f 20 73 65 74 75 70  tion.** to setup
12a9f 20 66 6f 72 20 61 20 6c 61 74 65 72 20 61 73 73   for a later ass
12aa0 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e 74 2e  ert() statement.
12aa1 20 20 57 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74    We do not want
12aa2 20 74 68 69 73 20 63 6f 64 65 20 74 6f 0a 2a 2a   this code to.**
12aa3 20 61 70 70 65 61 72 20 77 68 65 6e 20 61 73 73   appear when ass
12aa4 65 72 74 28 29 20 69 73 20 64 69 73 61 62 6c 65  ert() is disable
12aa5 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  d.  The followin
12aa6 67 20 6d 61 63 72 6f 20 69 73 20 74 68 65 72 65  g macro is there
12aa7 66 6f 72 65 0a 2a 2a 20 75 73 65 64 20 74 6f 20  fore.** used to 
12aa8 63 6f 6e 74 61 69 6e 20 74 68 61 74 20 73 65 74  contain that set
12aa9 75 70 20 63 6f 64 65 2e 20 20 54 68 65 20 22 56  up code.  The "V
12aaa 56 41 22 20 61 63 72 6f 6e 79 6d 20 73 74 61 6e  VA" acronym stan
12aab 64 73 20 66 6f 72 0a 2a 2a 20 22 56 65 72 69 66  ds for.** "Verif
12aac 69 63 61 74 69 6f 6e 2c 20 56 61 6c 69 64 61 74  ication, Validat
12aad 69 6f 6e 2c 20 61 6e 64 20 41 63 63 72 65 64 69  ion, and Accredi
12aae 74 61 74 69 6f 6e 22 2e 20 20 49 6e 20 6f 74 68  tation".  In oth
12aaf 65 72 20 77 6f 72 64 73 2c 20 74 68 65 0a 2a 2a  er words, the.**
12ab0 20 63 6f 64 65 20 77 69 74 68 69 6e 20 56 56 41   code within VVA
12ab1 5f 4f 4e 4c 59 28 29 20 77 69 6c 6c 20 6f 6e 6c  _ONLY() will onl
12ab2 79 20 72 75 6e 20 64 75 72 69 6e 67 20 76 65 72  y run during ver
12ab3 69 66 69 63 61 74 69 6f 6e 20 70 72 6f 63 65 73  ification proces
12ab4 73 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ses..*/.#ifndef 
12ab5 4e 44 45 42 55 47 0a 23 20 64 65 66 69 6e 65 20  NDEBUG.# define 
12ab6 56 56 41 5f 4f 4e 4c 59 28 58 29 20 20 58 0a 23  VVA_ONLY(X)  X.#
12ab7 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 56 56  else.# define VV
12ab8 41 5f 4f 4e 4c 59 28 58 29 0a 23 65 6e 64 69 66  A_ONLY(X).#endif
12ab9 0a 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ....#ifndef SQLI
12aba 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
12abb 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41 20 6c 69 73  ACHE./*.** A lis
12abc 74 20 6f 66 20 42 74 53 68 61 72 65 64 20 6f 62  t of BtShared ob
12abd 6a 65 63 74 73 20 74 68 61 74 20 61 72 65 20 65  jects that are e
12abe 6c 69 67 69 62 6c 65 20 66 6f 72 20 70 61 72 74  ligible for part
12abf 69 63 69 70 61 74 69 6f 6e 0a 2a 2a 20 69 6e 20  icipation.** in 
12ac0 73 68 61 72 65 64 20 63 61 63 68 65 2e 20 20 54  shared cache.  T
12ac1 68 69 73 20 76 61 72 69 61 62 6c 65 20 68 61 73  his variable has
12ac2 20 66 69 6c 65 20 73 63 6f 70 65 20 64 75 72 69   file scope duri
12ac3 6e 67 20 6e 6f 72 6d 61 6c 20 62 75 69 6c 64 73  ng normal builds
12ac4 2c 0a 2a 2a 20 62 75 74 20 74 68 65 20 74 65 73  ,.** but the tes
12ac5 74 20 68 61 72 6e 65 73 73 20 6e 65 65 64 73 20  t harness needs 
12ac6 74 6f 20 61 63 63 65 73 73 20 69 74 20 73 6f 20  to access it so 
12ac7 77 65 20 6d 61 6b 65 20 69 74 20 67 6c 6f 62 61  we make it globa
12ac8 6c 20 66 6f 72 20 0a 2a 2a 20 74 65 73 74 20 62  l for .** test b
12ac9 75 69 6c 64 73 2e 0a 2a 2f 0a 23 69 66 64 65 66  uilds..*/.#ifdef
12aca 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51 4c   SQLITE_TEST.SQL
12acb 49 54 45 5f 50 52 49 56 41 54 45 20 42 74 53 68  ITE_PRIVATE BtSh
12acc 61 72 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44  ared *SQLITE_WSD
12acd 20 73 71 6c 69 74 65 33 53 68 61 72 65 64 43 61   sqlite3SharedCa
12ace 63 68 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6c  cheList = 0;.#el
12acf 73 65 0a 73 74 61 74 69 63 20 42 74 53 68 61 72  se.static BtShar
12ad0 65 64 20 2a 53 51 4c 49 54 45 5f 57 53 44 20 73  ed *SQLITE_WSD s
12ad1 71 6c 69 74 65 33 53 68 61 72 65 64 43 61 63 68  qlite3SharedCach
12ad2 65 4c 69 73 74 20 3d 20 30 3b 0a 23 65 6e 64 69  eList = 0;.#endi
12ad3 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  f.#endif /* SQLI
12ad4 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43  TE_OMIT_SHARED_C
12ad5 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  ACHE */..#ifndef
12ad6 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
12ad7 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20  RED_CACHE./*.** 
12ad8 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
12ad9 65 20 74 68 65 20 73 68 61 72 65 64 20 70 61 67  e the shared pag
12ada 65 72 20 61 6e 64 20 73 63 68 65 6d 61 20 66 65  er and schema fe
12adb 61 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  atures..**.** Th
12adc 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 6e  is routine has n
12add 6f 20 65 66 66 65 63 74 20 6f 6e 20 65 78 69 73  o effect on exis
12ade 74 69 6e 67 20 64 61 74 61 62 61 73 65 20 63 6f  ting database co
12adf 6e 6e 65 63 74 69 6f 6e 73 2e 0a 2a 2a 20 54 68  nnections..** Th
12ae0 65 20 73 68 61 72 65 64 20 63 61 63 68 65 20 73  e shared cache s
12ae1 65 74 74 69 6e 67 20 65 66 66 65 63 74 73 20 6f  etting effects o
12ae2 6e 6c 79 20 66 75 74 75 72 65 20 63 61 6c 6c 73  nly future calls
12ae3 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6f   to.** sqlite3_o
12ae4 70 65 6e 28 29 2c 20 73 71 6c 69 74 65 33 5f 6f  pen(), sqlite3_o
12ae5 70 65 6e 31 36 28 29 2c 20 6f 72 20 73 71 6c 69  pen16(), or sqli
12ae6 74 65 33 5f 6f 70 65 6e 5f 76 32 28 29 2e 0a 2a  te3_open_v2()..*
12ae7 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
12ae8 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f   sqlite3_enable_
12ae9 73 68 61 72 65 64 5f 63 61 63 68 65 28 69 6e 74  shared_cache(int
12aea 20 65 6e 61 62 6c 65 29 7b 0a 20 20 73 71 6c 69   enable){.  sqli
12aeb 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
12aec 73 68 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c  sharedCacheEnabl
12aed 65 64 20 3d 20 65 6e 61 62 6c 65 3b 0a 20 20 72  ed = enable;.  r
12aee 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
12aef 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
12af0 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72  * Forward declar
12af1 61 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20  ation.*/.static 
12af2 69 6e 74 20 63 68 65 63 6b 52 65 61 64 4c 6f 63  int checkReadLoc
12af3 6b 73 28 42 74 72 65 65 2a 2c 20 50 67 6e 6f 2c  ks(Btree*, Pgno,
12af4 20 42 74 43 75 72 73 6f 72 2a 2c 20 69 36 34 29   BtCursor*, i64)
12af5 3b 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ;...#ifdef SQLIT
12af6 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
12af7 43 48 45 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68  CHE.  /*.  ** Th
12af8 65 20 66 75 6e 63 74 69 6f 6e 73 20 71 75 65 72  e functions quer
12af9 79 54 61 62 6c 65 4c 6f 63 6b 28 29 2c 20 6c 6f  yTableLock(), lo
12afa 63 6b 54 61 62 6c 65 28 29 20 61 6e 64 20 75 6e  ckTable() and un
12afb 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 29 0a  lockAllTables().
12afc 20 20 2a 2a 20 6d 61 6e 69 70 75 6c 61 74 65 20    ** manipulate 
12afd 65 6e 74 72 69 65 73 20 69 6e 20 74 68 65 20 42  entries in the B
12afe 74 53 68 61 72 65 64 2e 70 4c 6f 63 6b 20 6c 69  tShared.pLock li
12aff 6e 6b 65 64 20 6c 69 73 74 20 75 73 65 64 20 74  nked list used t
12b00 6f 20 73 74 6f 72 65 0a 20 20 2a 2a 20 73 68 61  o store.  ** sha
12b01 72 65 64 2d 63 61 63 68 65 20 74 61 62 6c 65 20  red-cache table 
12b02 6c 65 76 65 6c 20 6c 6f 63 6b 73 2e 20 49 66 20  level locks. If 
12b03 74 68 65 20 6c 69 62 72 61 72 79 20 69 73 20 63  the library is c
12b04 6f 6d 70 69 6c 65 64 20 77 69 74 68 20 74 68 65  ompiled with the
12b05 0a 20 20 2a 2a 20 73 68 61 72 65 64 2d 63 61 63  .  ** shared-cac
12b06 68 65 20 66 65 61 74 75 72 65 20 64 69 73 61 62  he feature disab
12b07 6c 65 64 2c 20 74 68 65 6e 20 74 68 65 72 65 20  led, then there 
12b08 69 73 20 6f 6e 6c 79 20 65 76 65 72 20 6f 6e 65  is only ever one
12b09 20 75 73 65 72 0a 20 20 2a 2a 20 6f 66 20 65 61   user.  ** of ea
12b0a 63 68 20 42 74 53 68 61 72 65 64 20 73 74 72 75  ch BtShared stru
12b0b 63 74 75 72 65 20 61 6e 64 20 73 6f 20 74 68 69  cture and so thi
12b0c 73 20 6c 6f 63 6b 69 6e 67 20 69 73 20 6e 6f 74  s locking is not
12b0d 20 6e 65 63 65 73 73 61 72 79 2e 20 0a 20 20 2a   necessary. .  *
12b0e 2a 20 53 6f 20 64 65 66 69 6e 65 20 74 68 65 20  * So define the 
12b0f 6c 6f 63 6b 20 72 65 6c 61 74 65 64 20 66 75 6e  lock related fun
12b10 63 74 69 6f 6e 73 20 61 73 20 6e 6f 2d 6f 70 73  ctions as no-ops
12b11 2e 0a 20 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65  ..  */.  #define
12b12 20 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28   queryTableLock(
12b13 61 2c 62 2c 63 29 20 53 51 4c 49 54 45 5f 4f 4b  a,b,c) SQLITE_OK
12b14 0a 20 20 23 64 65 66 69 6e 65 20 6c 6f 63 6b 54  .  #define lockT
12b15 61 62 6c 65 28 61 2c 62 2c 63 29 20 53 51 4c 49  able(a,b,c) SQLI
12b16 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65 20  TE_OK.  #define 
12b17 75 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28  unlockAllTables(
12b18 61 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  a).#endif..#ifnd
12b19 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
12b1a 48 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a  HARED_CACHE./*.*
12b1b 2a 20 51 75 65 72 79 20 74 6f 20 73 65 65 20 69  * Query to see i
12b1c 66 20 62 74 72 65 65 20 68 61 6e 64 6c 65 20 70  f btree handle p
12b1d 20 6d 61 79 20 6f 62 74 61 69 6e 20 61 20 6c 6f   may obtain a lo
12b1e 63 6b 20 6f 66 20 74 79 70 65 20 65 4c 6f 63 6b  ck of type eLock
12b1f 20 0a 2a 2a 20 28 52 45 41 44 5f 4c 4f 43 4b 20   .** (READ_LOCK 
12b20 6f 72 20 57 52 49 54 45 5f 4c 4f 43 4b 29 20 6f  or WRITE_LOCK) o
12b21 6e 20 74 68 65 20 74 61 62 6c 65 20 77 69 74 68  n the table with
12b22 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62 2e   root-page iTab.
12b23 20 52 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   Return.** SQLIT
12b24 45 5f 4f 4b 20 69 66 20 74 68 65 20 6c 6f 63 6b  E_OK if the lock
12b25 20 6d 61 79 20 62 65 20 6f 62 74 61 69 6e 65 64   may be obtained
12b26 20 28 62 79 20 63 61 6c 6c 69 6e 67 20 6c 6f 63   (by calling loc
12b27 6b 54 61 62 6c 65 28 29 29 2c 20 6f 72 0a 2a 2a  kTable()), or.**
12b28 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69   SQLITE_LOCKED i
12b29 66 20 6e 6f 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  f not..*/.static
12b2a 20 69 6e 74 20 71 75 65 72 79 54 61 62 6c 65 4c   int queryTableL
12b2b 6f 63 6b 28 42 74 72 65 65 20 2a 70 2c 20 50 67  ock(Btree *p, Pg
12b2c 6e 6f 20 69 54 61 62 2c 20 75 38 20 65 4c 6f 63  no iTab, u8 eLoc
12b2d 6b 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  k){.  BtShared *
12b2e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
12b2f 42 74 4c 6f 63 6b 20 2a 70 49 74 65 72 3b 0a 0a  BtLock *pIter;..
12b30 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
12b31 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78  3BtreeHoldsMutex
12b32 28 70 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  (p) );.  assert(
12b33 20 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c 4f 43   eLock==READ_LOC
12b34 4b 20 7c 7c 20 65 4c 6f 63 6b 3d 3d 57 52 49 54  K || eLock==WRIT
12b35 45 5f 4c 4f 43 4b 20 29 3b 0a 20 20 61 73 73 65  E_LOCK );.  asse
12b36 72 74 28 20 70 2d 3e 64 62 21 3d 30 20 29 3b 0a  rt( p->db!=0 );.
12b37 20 20 0a 20 20 2f 2a 20 54 68 69 73 20 69 73 20    .  /* This is 
12b38 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 73  a no-op if the s
12b39 68 61 72 65 64 2d 63 61 63 68 65 20 69 73 20 6e  hared-cache is n
12b3a 6f 74 20 65 6e 61 62 6c 65 64 20 2a 2f 0a 20 20  ot enabled */.  
12b3b 69 66 28 20 21 70 2d 3e 73 68 61 72 61 62 6c 65  if( !p->sharable
12b3c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
12b3d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
12b3e 20 2f 2a 20 49 66 20 73 6f 6d 65 20 6f 74 68 65   /* If some othe
12b3f 72 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69 73 20  r connection is 
12b40 68 6f 6c 64 69 6e 67 20 61 6e 20 65 78 63 6c 75  holding an exclu
12b41 73 69 76 65 20 6c 6f 63 6b 2c 20 74 68 65 0a 20  sive lock, the. 
12b42 20 2a 2a 20 72 65 71 75 65 73 74 65 64 20 6c 6f   ** requested lo
12b43 63 6b 20 6d 61 79 20 6e 6f 74 20 62 65 20 6f 62  ck may not be ob
12b44 74 61 69 6e 65 64 2e 0a 20 20 2a 2f 0a 20 20 69  tained..  */.  i
12b45 66 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69  f( pBt->pExclusi
12b46 76 65 20 26 26 20 70 42 74 2d 3e 70 45 78 63 6c  ve && pBt->pExcl
12b47 75 73 69 76 65 21 3d 70 20 29 7b 0a 20 20 20 20  usive!=p ){.    
12b48 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
12b49 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  CKED;.  }..  /* 
12b4a 54 68 69 73 20 28 61 6c 6f 6e 67 20 77 69 74 68  This (along with
12b4b 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29 20 69 73   lockTable()) is
12b4c 20 77 68 65 72 65 20 74 68 65 20 52 65 61 64 55   where the ReadU
12b4d 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 20  ncommitted flag 
12b4e 69 73 0a 20 20 2a 2a 20 64 65 61 6c 74 20 77 69  is.  ** dealt wi
12b4f 74 68 2e 20 49 66 20 74 68 65 20 63 61 6c 6c 65  th. If the calle
12b50 72 20 69 73 20 71 75 65 72 79 69 6e 67 20 66 6f  r is querying fo
12b51 72 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 61 6e  r a read-lock an
12b52 64 20 74 68 65 20 66 6c 61 67 20 69 73 0a 20 20  d the flag is.  
12b53 2a 2a 20 73 65 74 2c 20 69 74 20 69 73 20 75 6e  ** set, it is un
12b54 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 67 72  conditionally gr
12b55 61 6e 74 65 64 20 2d 20 65 76 65 6e 20 69 66 20  anted - even if 
12b56 74 68 65 72 65 20 61 72 65 20 77 72 69 74 65 2d  there are write-
12b57 6c 6f 63 6b 73 0a 20 20 2a 2a 20 6f 6e 20 74 68  locks.  ** on th
12b58 65 20 74 61 62 6c 65 2e 20 49 66 20 61 20 77 72  e table. If a wr
12b59 69 74 65 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75  ite-lock is requ
12b5a 65 73 74 65 64 2c 20 74 68 65 20 52 65 61 64 55  ested, the ReadU
12b5b 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67 0a  ncommitted flag.
12b5c 20 20 2a 2a 20 69 73 20 6e 6f 74 20 63 6f 6e 73    ** is not cons
12b5d 69 64 65 72 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  idered..  **.  *
12b5e 2a 20 49 6e 20 66 75 6e 63 74 69 6f 6e 20 6c 6f  * In function lo
12b5f 63 6b 54 61 62 6c 65 28 29 2c 20 69 66 20 61 20  ckTable(), if a 
12b60 72 65 61 64 2d 6c 6f 63 6b 20 69 73 20 64 65 6d  read-lock is dem
12b61 61 6e 64 65 64 20 61 6e 64 20 74 68 65 20 0a 20  anded and the . 
12b62 20 2a 2a 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74   ** ReadUncommit
12b63 74 65 64 20 66 6c 61 67 20 69 73 20 73 65 74 2c  ted flag is set,
12b64 20 6e 6f 20 65 6e 74 72 79 20 69 73 20 61 64 64   no entry is add
12b65 65 64 20 74 6f 20 74 68 65 20 6c 6f 63 6b 73 20  ed to the locks 
12b66 6c 69 73 74 20 0a 20 20 2a 2a 20 28 42 74 53 68  list .  ** (BtSh
12b67 61 72 65 64 2e 70 4c 6f 63 6b 29 2e 0a 20 20 2a  ared.pLock)..  *
12b68 2a 0a 20 20 2a 2a 20 54 6f 20 73 75 6d 6d 61 72  *.  ** To summar
12b69 69 7a 65 3a 20 49 66 20 74 68 65 20 52 65 61 64  ize: If the Read
12b6a 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67  Uncommitted flag
12b6b 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 72 65   is set, then re
12b6c 61 64 20 63 75 72 73 6f 72 73 20 64 6f 0a 20 20  ad cursors do.  
12b6d 2a 2a 20 6e 6f 74 20 63 72 65 61 74 65 20 6f 72  ** not create or
12b6e 20 72 65 73 70 65 63 74 20 74 61 62 6c 65 20 6c   respect table l
12b6f 6f 63 6b 73 2e 20 54 68 65 20 6c 6f 63 6b 69 6e  ocks. The lockin
12b70 67 20 70 72 6f 63 65 64 75 72 65 20 66 6f 72 20  g procedure for 
12b71 61 20 0a 20 20 2a 2a 20 77 72 69 74 65 2d 63 75  a .  ** write-cu
12b72 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20 63 68  rsor does not ch
12b73 61 6e 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ange..  */.  if(
12b74 20 0a 20 20 20 20 30 3d 3d 28 70 2d 3e 64 62 2d   .    0==(p->db-
12b75 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 52 65  >flags&SQLITE_Re
12b76 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29 20 7c  adUncommitted) |
12b77 7c 20 0a 20 20 20 20 65 4c 6f 63 6b 3d 3d 57 52  | .    eLock==WR
12b78 49 54 45 5f 4c 4f 43 4b 20 7c 7c 0a 20 20 20 20  ITE_LOCK ||.    
12b79 69 54 61 62 3d 3d 4d 41 53 54 45 52 5f 52 4f 4f  iTab==MASTER_ROO
12b7a 54 0a 20 20 29 7b 0a 20 20 20 20 66 6f 72 28 70  T.  ){.    for(p
12b7b 49 74 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b  Iter=pBt->pLock;
12b7c 20 70 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49   pIter; pIter=pI
12b7d 74 65 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20  ter->pNext){.   
12b7e 20 20 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42     if( pIter->pB
12b7f 74 72 65 65 21 3d 70 20 26 26 20 70 49 74 65 72  tree!=p && pIter
12b80 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 20 26  ->iTable==iTab &
12b81 26 20 0a 20 20 20 20 20 20 20 20 20 20 28 70 49  & .          (pI
12b82 74 65 72 2d 3e 65 4c 6f 63 6b 21 3d 65 4c 6f 63  ter->eLock!=eLoc
12b83 6b 20 7c 7c 20 65 4c 6f 63 6b 21 3d 52 45 41 44  k || eLock!=READ
12b84 5f 4c 4f 43 4b 29 20 29 7b 0a 20 20 20 20 20 20  _LOCK) ){.      
12b85 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
12b86 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a  LOCKED;.      }.
12b87 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
12b88 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
12b89 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54  #endif /* !SQLIT
12b8a 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41  E_OMIT_SHARED_CA
12b8b 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  CHE */..#ifndef 
12b8c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52  SQLITE_OMIT_SHAR
12b8d 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a 20 41  ED_CACHE./*.** A
12b8e 64 64 20 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  dd a lock on the
12b8f 20 74 61 62 6c 65 20 77 69 74 68 20 72 6f 6f 74   table with root
12b90 2d 70 61 67 65 20 69 54 61 62 6c 65 20 74 6f 20  -page iTable to 
12b91 74 68 65 20 73 68 61 72 65 64 2d 62 74 72 65 65  the shared-btree
12b92 20 75 73 65 64 0a 2a 2a 20 62 79 20 42 74 72 65   used.** by Btre
12b93 65 20 68 61 6e 64 6c 65 20 70 2e 20 50 61 72 61  e handle p. Para
12b94 6d 65 74 65 72 20 65 4c 6f 63 6b 20 6d 75 73 74  meter eLock must
12b95 20 62 65 20 65 69 74 68 65 72 20 52 45 41 44 5f   be either READ_
12b96 4c 4f 43 4b 20 6f 72 20 0a 2a 2a 20 57 52 49 54  LOCK or .** WRIT
12b97 45 5f 4c 4f 43 4b 2e 0a 2a 2a 0a 2a 2a 20 53 51  E_LOCK..**.** SQ
12b98 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72  LITE_OK is retur
12b99 6e 65 64 20 69 66 20 74 68 65 20 6c 6f 63 6b 20  ned if the lock 
12b9a 69 73 20 61 64 64 65 64 20 73 75 63 63 65 73 73  is added success
12b9b 66 75 6c 6c 79 2e 20 53 51 4c 49 54 45 5f 42 55  fully. SQLITE_BU
12b9c 53 59 20 61 6e 64 0a 2a 2a 20 53 51 4c 49 54 45  SY and.** SQLITE
12b9d 5f 4e 4f 4d 45 4d 20 6d 61 79 20 61 6c 73 6f 20  _NOMEM may also 
12b9e 62 65 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a  be returned..*/.
12b9f 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 54  static int lockT
12ba0 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 50  able(Btree *p, P
12ba1 67 6e 6f 20 69 54 61 62 6c 65 2c 20 75 38 20 65  gno iTable, u8 e
12ba2 4c 6f 63 6b 29 7b 0a 20 20 42 74 53 68 61 72 65  Lock){.  BtShare
12ba3 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
12ba4 0a 20 20 42 74 4c 6f 63 6b 20 2a 70 4c 6f 63 6b  .  BtLock *pLock
12ba5 20 3d 20 30 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a   = 0;.  BtLock *
12ba6 70 49 74 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  pIter;..  assert
12ba7 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f  ( sqlite3BtreeHo
12ba8 6c 64 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20  ldsMutex(p) );. 
12ba9 20 61 73 73 65 72 74 28 20 65 4c 6f 63 6b 3d 3d   assert( eLock==
12baa 52 45 41 44 5f 4c 4f 43 4b 20 7c 7c 20 65 4c 6f  READ_LOCK || eLo
12bab 63 6b 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29  ck==WRITE_LOCK )
12bac 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64  ;.  assert( p->d
12bad 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 54 68  b!=0 );..  /* Th
12bae 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66  is is a no-op if
12baf 20 74 68 65 20 73 68 61 72 65 64 2d 63 61 63 68   the shared-cach
12bb0 65 20 69 73 20 6e 6f 74 20 65 6e 61 62 6c 65 64  e is not enabled
12bb1 20 2a 2f 0a 20 20 69 66 28 20 21 70 2d 3e 73 68   */.  if( !p->sh
12bb2 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20 72 65  arable ){.    re
12bb3 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
12bb4 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 53    }..  assert( S
12bb5 51 4c 49 54 45 5f 4f 4b 3d 3d 71 75 65 72 79 54  QLITE_OK==queryT
12bb6 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 54 61 62  ableLock(p, iTab
12bb7 6c 65 2c 20 65 4c 6f 63 6b 29 20 29 3b 0a 0a 20  le, eLock) );.. 
12bb8 20 2f 2a 20 49 66 20 74 68 65 20 72 65 61 64 2d   /* If the read-
12bb9 75 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c 61 67  uncommitted flag
12bba 20 69 73 20 73 65 74 20 61 6e 64 20 61 20 72 65   is set and a re
12bbb 61 64 2d 6c 6f 63 6b 20 69 73 20 72 65 71 75 65  ad-lock is reque
12bbc 73 74 65 64 2c 0a 20 20 2a 2a 20 72 65 74 75 72  sted,.  ** retur
12bbd 6e 20 65 61 72 6c 79 20 77 69 74 68 6f 75 74 20  n early without 
12bbe 61 64 64 69 6e 67 20 61 6e 20 65 6e 74 72 79 20  adding an entry 
12bbf 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 2e  to the BtShared.
12bc0 70 4c 6f 63 6b 20 6c 69 73 74 2e 20 53 65 65 0a  pLock list. See.
12bc1 20 20 2a 2a 20 63 6f 6d 6d 65 6e 74 20 69 6e 20    ** comment in 
12bc2 66 75 6e 63 74 69 6f 6e 20 71 75 65 72 79 54 61  function queryTa
12bc3 62 6c 65 4c 6f 63 6b 28 29 20 66 6f 72 20 6d 6f  bleLock() for mo
12bc4 72 65 20 69 6e 66 6f 20 6f 6e 20 68 61 6e 64 6c  re info on handl
12bc5 69 6e 67 20 0a 20 20 2a 2a 20 74 68 65 20 52 65  ing .  ** the Re
12bc6 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 66 6c  adUncommitted fl
12bc7 61 67 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a  ag..  */.  if( .
12bc8 20 20 20 20 28 70 2d 3e 64 62 2d 3e 66 6c 61 67      (p->db->flag
12bc9 73 26 53 51 4c 49 54 45 5f 52 65 61 64 55 6e 63  s&SQLITE_ReadUnc
12bca 6f 6d 6d 69 74 74 65 64 29 20 26 26 20 0a 20 20  ommitted) && .  
12bcb 20 20 28 65 4c 6f 63 6b 3d 3d 52 45 41 44 5f 4c    (eLock==READ_L
12bcc 4f 43 4b 29 20 26 26 0a 20 20 20 20 69 54 61 62  OCK) &&.    iTab
12bcd 6c 65 21 3d 4d 41 53 54 45 52 5f 52 4f 4f 54 0a  le!=MASTER_ROOT.
12bce 20 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20    ){.    return 
12bcf 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a  SQLITE_OK;.  }..
12bd0 20 20 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63    /* First searc
12bd1 68 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 61  h the list for a
12bd2 6e 20 65 78 69 73 74 69 6e 67 20 6c 6f 63 6b 20  n existing lock 
12bd3 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2e 20 2a  on this table. *
12bd4 2f 0a 20 20 66 6f 72 28 70 49 74 65 72 3d 70 42  /.  for(pIter=pB
12bd5 74 2d 3e 70 4c 6f 63 6b 3b 20 70 49 74 65 72 3b  t->pLock; pIter;
12bd6 20 70 49 74 65 72 3d 70 49 74 65 72 2d 3e 70 4e   pIter=pIter->pN
12bd7 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70 49  ext){.    if( pI
12bd8 74 65 72 2d 3e 69 54 61 62 6c 65 3d 3d 69 54 61  ter->iTable==iTa
12bd9 62 6c 65 20 26 26 20 70 49 74 65 72 2d 3e 70 42  ble && pIter->pB
12bda 74 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20  tree==p ){.     
12bdb 20 70 4c 6f 63 6b 20 3d 20 70 49 74 65 72 3b 0a   pLock = pIter;.
12bdc 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
12bdd 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20   }.  }..  /* If 
12bde 74 68 65 20 61 62 6f 76 65 20 73 65 61 72 63 68  the above search
12bdf 20 64 69 64 20 6e 6f 74 20 66 69 6e 64 20 61 20   did not find a 
12be0 42 74 4c 6f 63 6b 20 73 74 72 75 63 74 20 61 73  BtLock struct as
12be1 73 6f 63 69 61 74 69 6e 67 20 42 74 72 65 65 20  sociating Btree 
12be2 70 0a 20 20 2a 2a 20 77 69 74 68 20 74 61 62 6c  p.  ** with tabl
12be3 65 20 69 54 61 62 6c 65 2c 20 61 6c 6c 6f 63 61  e iTable, alloca
12be4 74 65 20 6f 6e 65 20 61 6e 64 20 6c 69 6e 6b 20  te one and link 
12be5 69 74 20 69 6e 74 6f 20 74 68 65 20 6c 69 73 74  it into the list
12be6 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 70 4c  ..  */.  if( !pL
12be7 6f 63 6b 20 29 7b 0a 20 20 20 20 70 4c 6f 63 6b  ock ){.    pLock
12be8 20 3d 20 28 42 74 4c 6f 63 6b 20 2a 29 73 71 6c   = (BtLock *)sql
12be9 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73  ite3MallocZero(s
12bea 69 7a 65 6f 66 28 42 74 4c 6f 63 6b 29 29 3b 0a  izeof(BtLock));.
12beb 20 20 20 20 69 66 28 20 21 70 4c 6f 63 6b 20 29      if( !pLock )
12bec 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
12bed 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
12bee 20 7d 0a 20 20 20 20 70 4c 6f 63 6b 2d 3e 69 54   }.    pLock->iT
12bef 61 62 6c 65 20 3d 20 69 54 61 62 6c 65 3b 0a 20  able = iTable;. 
12bf0 20 20 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65     pLock->pBtree
12bf1 20 3d 20 70 3b 0a 20 20 20 20 70 4c 6f 63 6b 2d   = p;.    pLock-
12bf2 3e 70 4e 65 78 74 20 3d 20 70 42 74 2d 3e 70 4c  >pNext = pBt->pL
12bf3 6f 63 6b 3b 0a 20 20 20 20 70 42 74 2d 3e 70 4c  ock;.    pBt->pL
12bf4 6f 63 6b 20 3d 20 70 4c 6f 63 6b 3b 0a 20 20 7d  ock = pLock;.  }
12bf5 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 42  ..  /* Set the B
12bf6 74 4c 6f 63 6b 2e 65 4c 6f 63 6b 20 76 61 72 69  tLock.eLock vari
12bf7 61 62 6c 65 20 74 6f 20 74 68 65 20 6d 61 78 69  able to the maxi
12bf8 6d 75 6d 20 6f 66 20 74 68 65 20 63 75 72 72 65  mum of the curre
12bf9 6e 74 20 6c 6f 63 6b 0a 20 20 2a 2a 20 61 6e 64  nt lock.  ** and
12bfa 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 6c   the requested l
12bfb 6f 63 6b 2e 20 54 68 69 73 20 6d 65 61 6e 73 20  ock. This means 
12bfc 69 66 20 61 20 77 72 69 74 65 2d 6c 6f 63 6b 20  if a write-lock 
12bfd 77 61 73 20 61 6c 72 65 61 64 79 20 68 65 6c 64  was already held
12bfe 0a 20 20 2a 2a 20 61 6e 64 20 61 20 72 65 61 64  .  ** and a read
12bff 2d 6c 6f 63 6b 20 72 65 71 75 65 73 74 65 64 2c  -lock requested,
12c00 20 77 65 20 64 6f 6e 27 74 20 69 6e 63 6f 72 72   we don't incorr
12c01 65 63 74 6c 79 20 64 6f 77 6e 67 72 61 64 65 20  ectly downgrade 
12c02 74 68 65 20 6c 6f 63 6b 2e 0a 20 20 2a 2f 0a 20  the lock..  */. 
12c03 20 61 73 73 65 72 74 28 20 57 52 49 54 45 5f 4c   assert( WRITE_L
12c04 4f 43 4b 3e 52 45 41 44 5f 4c 4f 43 4b 20 29 3b  OCK>READ_LOCK );
12c05 0a 20 20 69 66 28 20 65 4c 6f 63 6b 3e 70 4c 6f  .  if( eLock>pLo
12c06 63 6b 2d 3e 65 4c 6f 63 6b 20 29 7b 0a 20 20 20  ck->eLock ){.   
12c07 20 70 4c 6f 63 6b 2d 3e 65 4c 6f 63 6b 20 3d 20   pLock->eLock = 
12c08 65 4c 6f 63 6b 3b 0a 20 20 7d 0a 0a 20 20 72 65  eLock;.  }..  re
12c09 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
12c0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51 4c  }.#endif /* !SQL
12c0b 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
12c0c 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
12c0d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48  f SQLITE_OMIT_SH
12c0e 41 52 45 44 5f 43 41 43 48 45 0a 2f 2a 0a 2a 2a  ARED_CACHE./*.**
12c0f 20 52 65 6c 65 61 73 65 20 61 6c 6c 20 74 68 65   Release all the
12c10 20 74 61 62 6c 65 20 6c 6f 63 6b 73 20 28 6c 6f   table locks (lo
12c11 63 6b 73 20 6f 62 74 61 69 6e 65 64 20 76 69 61  cks obtained via
12c12 20 63 61 6c 6c 73 20 74 6f 20 74 68 65 20 6c 6f   calls to the lo
12c13 63 6b 54 61 62 6c 65 28 29 0a 2a 2a 20 70 72 6f  ckTable().** pro
12c14 63 65 64 75 72 65 29 20 68 65 6c 64 20 62 79 20  cedure) held by 
12c15 42 74 72 65 65 20 68 61 6e 64 6c 65 20 70 2e 0a  Btree handle p..
12c16 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75  */.static void u
12c17 6e 6c 6f 63 6b 41 6c 6c 54 61 62 6c 65 73 28 42  nlockAllTables(B
12c18 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53 68  tree *p){.  BtSh
12c19 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
12c1a 42 74 3b 0a 20 20 42 74 4c 6f 63 6b 20 2a 2a 70  Bt;.  BtLock **p
12c1b 70 49 74 65 72 20 3d 20 26 70 42 74 2d 3e 70 4c  pIter = &pBt->pL
12c1c 6f 63 6b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  ock;..  assert( 
12c1d 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
12c1e 73 4d 75 74 65 78 28 70 29 20 29 3b 0a 20 20 61  sMutex(p) );.  a
12c1f 73 73 65 72 74 28 20 70 2d 3e 73 68 61 72 61 62  ssert( p->sharab
12c20 6c 65 20 7c 7c 20 30 3d 3d 2a 70 70 49 74 65 72  le || 0==*ppIter
12c21 20 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 2a 70   );..  while( *p
12c22 70 49 74 65 72 20 29 7b 0a 20 20 20 20 42 74 4c  pIter ){.    BtL
12c23 6f 63 6b 20 2a 70 4c 6f 63 6b 20 3d 20 2a 70 70  ock *pLock = *pp
12c24 49 74 65 72 3b 0a 20 20 20 20 61 73 73 65 72 74  Iter;.    assert
12c25 28 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69 76  ( pBt->pExclusiv
12c26 65 3d 3d 30 20 7c 7c 20 70 42 74 2d 3e 70 45 78  e==0 || pBt->pEx
12c27 63 6c 75 73 69 76 65 3d 3d 70 4c 6f 63 6b 2d 3e  clusive==pLock->
12c28 70 42 74 72 65 65 20 29 3b 0a 20 20 20 20 69 66  pBtree );.    if
12c29 28 20 70 4c 6f 63 6b 2d 3e 70 42 74 72 65 65 3d  ( pLock->pBtree=
12c2a 3d 70 20 29 7b 0a 20 20 20 20 20 20 2a 70 70 49  =p ){.      *ppI
12c2b 74 65 72 20 3d 20 70 4c 6f 63 6b 2d 3e 70 4e 65  ter = pLock->pNe
12c2c 78 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  xt;.      sqlite
12c2d 33 5f 66 72 65 65 28 70 4c 6f 63 6b 29 3b 0a 20  3_free(pLock);. 
12c2e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
12c2f 70 70 49 74 65 72 20 3d 20 26 70 4c 6f 63 6b 2d  ppIter = &pLock-
12c30 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20 20  >pNext;.    }.  
12c31 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 45  }..  if( pBt->pE
12c32 78 63 6c 75 73 69 76 65 3d 3d 70 20 29 7b 0a 20  xclusive==p ){. 
12c33 20 20 20 70 42 74 2d 3e 70 45 78 63 6c 75 73 69     pBt->pExclusi
12c34 76 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65  ve = 0;.  }.}.#e
12c35 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
12c36 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45  MIT_SHARED_CACHE
12c37 20 2a 2f 0a 0a 73 74 61 74 69 63 20 76 6f 69 64   */..static void
12c38 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65 6d   releasePage(Mem
12c39 50 61 67 65 20 2a 70 50 61 67 65 29 3b 20 20 2f  Page *pPage);  /
12c3a 2a 20 46 6f 72 77 61 72 64 20 72 65 66 65 72 65  * Forward refere
12c3b 6e 63 65 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 56 65  nce */../*.** Ve
12c3c 72 69 66 79 20 74 68 61 74 20 74 68 65 20 63 75  rify that the cu
12c3d 72 73 6f 72 20 68 6f 6c 64 73 20 61 20 6d 75 74  rsor holds a mut
12c3e 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68 61 72  ex on the BtShar
12c3f 65 64 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44  ed.*/.#ifndef ND
12c40 45 42 55 47 0a 73 74 61 74 69 63 20 69 6e 74 20  EBUG.static int 
12c41 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
12c42 28 42 74 43 75 72 73 6f 72 20 2a 70 29 7b 0a 20  (BtCursor *p){. 
12c43 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f   return sqlite3_
12c44 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 70 42  mutex_held(p->pB
12c45 74 2d 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e  t->mutex);.}.#en
12c46 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51  dif...#ifndef SQ
12c47 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
12c48 4f 42 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64  OB./*.** Invalid
12c49 61 74 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77  ate the overflow
12c4a 20 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65   page-list cache
12c4b 20 66 6f 72 20 63 75 72 73 6f 72 20 70 43 75 72   for cursor pCur
12c4c 2c 20 69 66 20 61 6e 79 2e 0a 2a 2f 0a 73 74 61  , if any..*/.sta
12c4d 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c 69 64  tic void invalid
12c4e 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65  ateOverflowCache
12c4f 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29  (BtCursor *pCur)
12c50 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  {.  assert( curs
12c51 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
12c52 72 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  r) );.  sqlite3_
12c53 66 72 65 65 28 70 43 75 72 2d 3e 61 4f 76 65 72  free(pCur->aOver
12c54 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 61  flow);.  pCur->a
12c55 4f 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 7d 0a  Overflow = 0;.}.
12c56 0a 2f 2a 0a 2a 2a 20 49 6e 76 61 6c 69 64 61 74  ./*.** Invalidat
12c57 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 70  e the overflow p
12c58 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 20 66  age-list cache f
12c59 6f 72 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f  or all cursors o
12c5a 70 65 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68 65 20  pened.** on the 
12c5b 73 68 61 72 65 64 20 62 74 72 65 65 20 73 74 72  shared btree str
12c5c 75 63 74 75 72 65 20 70 42 74 2e 0a 2a 2f 0a 73  ucture pBt..*/.s
12c5d 74 61 74 69 63 20 76 6f 69 64 20 69 6e 76 61 6c  tatic void inval
12c5e 69 64 61 74 65 41 6c 6c 4f 76 65 72 66 6c 6f 77  idateAllOverflow
12c5f 43 61 63 68 65 28 42 74 53 68 61 72 65 64 20 2a  Cache(BtShared *
12c60 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  pBt){.  BtCursor
12c61 20 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 73   *p;.  assert( s
12c62 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
12c63 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
12c64 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70 43  .  for(p=pBt->pC
12c65 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e 70  ursor; p; p=p->p
12c66 4e 65 78 74 29 7b 0a 20 20 20 20 69 6e 76 61 6c  Next){.    inval
12c67 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63  idateOverflowCac
12c68 68 65 28 70 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6c  he(p);.  }.}.#el
12c69 73 65 0a 20 20 23 64 65 66 69 6e 65 20 69 6e 76  se.  #define inv
12c6a 61 6c 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43  alidateOverflowC
12c6b 61 63 68 65 28 78 29 0a 20 20 23 64 65 66 69 6e  ache(x).  #defin
12c6c 65 20 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f  e invalidateAllO
12c6d 76 65 72 66 6c 6f 77 43 61 63 68 65 28 78 29 0a  verflowCache(x).
12c6e 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 53 61  #endif../*.** Sa
12c6f 76 65 20 74 68 65 20 63 75 72 72 65 6e 74 20 63  ve the current c
12c70 75 72 73 6f 72 20 70 6f 73 69 74 69 6f 6e 20 69  ursor position i
12c71 6e 20 74 68 65 20 76 61 72 69 61 62 6c 65 73 20  n the variables 
12c72 42 74 43 75 72 73 6f 72 2e 6e 4b 65 79 20 0a 2a  BtCursor.nKey .*
12c73 2a 20 61 6e 64 20 42 74 43 75 72 73 6f 72 2e 70  * and BtCursor.p
12c74 4b 65 79 2e 20 54 68 65 20 63 75 72 73 6f 72 27  Key. The cursor'
12c75 73 20 73 74 61 74 65 20 69 73 20 73 65 74 20 74  s state is set t
12c76 6f 20 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45  o CURSOR_REQUIRE
12c77 53 45 45 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  SEEK..*/.static 
12c78 69 6e 74 20 73 61 76 65 43 75 72 73 6f 72 50 6f  int saveCursorPo
12c79 73 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20  sition(BtCursor 
12c7a 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63  *pCur){.  int rc
12c7b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 43 55 52  ;..  assert( CUR
12c7c 53 4f 52 5f 56 41 4c 49 44 3d 3d 70 43 75 72 2d  SOR_VALID==pCur-
12c7d 3e 65 53 74 61 74 65 20 29 3b 0a 20 20 61 73 73  >eState );.  ass
12c7e 65 72 74 28 20 30 3d 3d 70 43 75 72 2d 3e 70 4b  ert( 0==pCur->pK
12c7f 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
12c80 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
12c81 28 70 43 75 72 29 20 29 3b 0a 0a 20 20 72 63 20  (pCur) );..  rc 
12c82 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  = sqlite3BtreeKe
12c83 79 53 69 7a 65 28 70 43 75 72 2c 20 26 70 43 75  ySize(pCur, &pCu
12c84 72 2d 3e 6e 4b 65 79 29 3b 0a 0a 20 20 2f 2a 20  r->nKey);..  /* 
12c85 49 66 20 74 68 69 73 20 69 73 20 61 6e 20 69 6e  If this is an in
12c86 74 4b 65 79 20 74 61 62 6c 65 2c 20 74 68 65 6e  tKey table, then
12c87 20 74 68 65 20 61 62 6f 76 65 20 63 61 6c 6c 20   the above call 
12c88 74 6f 20 42 74 72 65 65 4b 65 79 53 69 7a 65 28  to BtreeKeySize(
12c89 29 0a 20 20 2a 2a 20 73 74 6f 72 65 73 20 74 68  ).  ** stores th
12c8a 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 69 6e  e integer key in
12c8b 20 70 43 75 72 2d 3e 6e 4b 65 79 2e 20 49 6e 20   pCur->nKey. In 
12c8c 74 68 69 73 20 63 61 73 65 20 74 68 69 73 20 76  this case this v
12c8d 61 6c 75 65 20 69 73 0a 20 20 2a 2a 20 61 6c 6c  alue is.  ** all
12c8e 20 74 68 61 74 20 69 73 20 72 65 71 75 69 72 65   that is require
12c8f 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 69 66  d. Otherwise, if
12c90 20 70 43 75 72 20 69 73 20 6e 6f 74 20 6f 70 65   pCur is not ope
12c91 6e 20 6f 6e 20 61 6e 20 69 6e 74 4b 65 79 0a 20  n on an intKey. 
12c92 20 2a 2a 20 74 61 62 6c 65 2c 20 74 68 65 6e 20   ** table, then 
12c93 6d 61 6c 6c 6f 63 20 73 70 61 63 65 20 66 6f 72  malloc space for
12c94 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20 70   and store the p
12c95 43 75 72 2d 3e 6e 4b 65 79 20 62 79 74 65 73 20  Cur->nKey bytes 
12c96 6f 66 20 6b 65 79 20 0a 20 20 2a 2a 20 64 61 74  of key .  ** dat
12c97 61 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  a..  */.  if( rc
12c98 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 30  ==SQLITE_OK && 0
12c99 3d 3d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  ==pCur->apPage[0
12c9a 5d 2d 3e 69 6e 74 4b 65 79 29 7b 0a 20 20 20 20  ]->intKey){.    
12c9b 76 6f 69 64 20 2a 70 4b 65 79 20 3d 20 73 71 6c  void *pKey = sql
12c9c 69 74 65 33 4d 61 6c 6c 6f 63 28 70 43 75 72 2d  ite3Malloc(pCur-
12c9d 3e 6e 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20  >nKey);.    if( 
12c9e 70 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 63  pKey ){.      rc
12c9f 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b   = sqlite3BtreeK
12ca0 65 79 28 70 43 75 72 2c 20 30 2c 20 70 43 75 72  ey(pCur, 0, pCur
12ca1 2d 3e 6e 4b 65 79 2c 20 70 4b 65 79 29 3b 0a 20  ->nKey, pKey);. 
12ca2 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
12ca3 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
12ca4 20 20 70 43 75 72 2d 3e 70 4b 65 79 20 3d 20 70    pCur->pKey = p
12ca5 4b 65 79 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  Key;.      }else
12ca6 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
12ca7 33 5f 66 72 65 65 28 70 4b 65 79 29 3b 0a 20 20  3_free(pKey);.  
12ca8 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b      }.    }else{
12ca9 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
12caa 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
12cab 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21 70    }.  assert( !p
12cac 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e  Cur->apPage[0]->
12cad 69 6e 74 4b 65 79 20 7c 7c 20 21 70 43 75 72 2d  intKey || !pCur-
12cae 3e 70 4b 65 79 20 29 3b 0a 0a 20 20 69 66 28 20  >pKey );..  if( 
12caf 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
12cb0 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
12cb1 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72  for(i=0; i<=pCur
12cb2 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  ->iPage; i++){. 
12cb3 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
12cb4 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  (pCur->apPage[i]
12cb5 29 3b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61  );.      pCur->a
12cb6 70 50 61 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20  pPage[i] = 0;.  
12cb7 20 20 7d 0a 20 20 20 20 70 43 75 72 2d 3e 69 50    }.    pCur->iP
12cb8 61 67 65 20 3d 20 2d 31 3b 0a 20 20 20 20 70 43  age = -1;.    pC
12cb9 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52  ur->eState = CUR
12cba 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 3b  SOR_REQUIRESEEK;
12cbb 0a 20 20 7d 0a 0a 20 20 69 6e 76 61 6c 69 64 61  .  }..  invalida
12cbc 74 65 4f 76 65 72 66 6c 6f 77 43 61 63 68 65 28  teOverflowCache(
12cbd 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCur);.  return 
12cbe 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 61 76  rc;.}../*.** Sav
12cbf 65 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20  e the positions 
12cc0 6f 66 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65  of all cursors e
12cc1 78 63 65 70 74 20 70 45 78 63 65 70 74 20 6f 70  xcept pExcept op
12cc2 65 6e 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20  en on the table 
12cc3 0a 2a 2a 20 77 69 74 68 20 72 6f 6f 74 2d 70 61  .** with root-pa
12cc4 67 65 20 69 52 6f 6f 74 2e 20 55 73 75 61 6c 6c  ge iRoot. Usuall
12cc5 79 2c 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  y, this is calle
12cc6 64 20 6a 75 73 74 20 62 65 66 6f 72 65 20 63 75  d just before cu
12cc7 72 73 6f 72 0a 2a 2a 20 70 45 78 63 65 70 74 20  rsor.** pExcept 
12cc8 69 73 20 75 73 65 64 20 74 6f 20 6d 6f 64 69 66  is used to modif
12cc9 79 20 74 68 65 20 74 61 62 6c 65 20 28 42 74 72  y the table (Btr
12cca 65 65 44 65 6c 65 74 65 28 29 20 6f 72 20 42 74  eeDelete() or Bt
12ccb 72 65 65 49 6e 73 65 72 74 28 29 29 2e 0a 2a 2f  reeInsert())..*/
12ccc 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 76 65  .static int save
12ccd 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 53 68 61  AllCursors(BtSha
12cce 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f 20 69  red *pBt, Pgno i
12ccf 52 6f 6f 74 2c 20 42 74 43 75 72 73 6f 72 20 2a  Root, BtCursor *
12cd0 70 45 78 63 65 70 74 29 7b 0a 20 20 42 74 43 75  pExcept){.  BtCu
12cd1 72 73 6f 72 20 2a 70 3b 0a 20 20 61 73 73 65 72  rsor *p;.  asser
12cd2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12cd3 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
12cd4 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
12cd5 45 78 63 65 70 74 3d 3d 30 20 7c 7c 20 70 45 78  Except==0 || pEx
12cd6 63 65 70 74 2d 3e 70 42 74 3d 3d 70 42 74 20 29  cept->pBt==pBt )
12cd7 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 2d 3e 70  ;.  for(p=pBt->p
12cd8 43 75 72 73 6f 72 3b 20 70 3b 20 70 3d 70 2d 3e  Cursor; p; p=p->
12cd9 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
12cda 70 21 3d 70 45 78 63 65 70 74 20 26 26 20 28 30  p!=pExcept && (0
12cdb 3d 3d 69 52 6f 6f 74 20 7c 7c 20 70 2d 3e 70 67  ==iRoot || p->pg
12cdc 6e 6f 52 6f 6f 74 3d 3d 69 52 6f 6f 74 29 20 26  noRoot==iRoot) &
12cdd 26 20 0a 20 20 20 20 20 20 20 20 70 2d 3e 65 53  & .        p->eS
12cde 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
12cdf 49 44 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ID ){.      int 
12ce0 72 63 20 3d 20 73 61 76 65 43 75 72 73 6f 72 50  rc = saveCursorP
12ce1 6f 73 69 74 69 6f 6e 28 70 29 3b 0a 20 20 20 20  osition(p);.    
12ce2 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
12ce3 3d 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 72  =rc ){.        r
12ce4 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
12ce5 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  }.    }.  }.  re
12ce6 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
12ce7 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20 74  }../*.** Clear t
12ce8 68 65 20 63 75 72 72 65 6e 74 20 63 75 72 73 6f  he current curso
12ce9 72 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a 53  r position..*/.S
12cea 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
12ceb 69 64 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  id sqlite3BtreeC
12cec 6c 65 61 72 43 75 72 73 6f 72 28 42 74 43 75 72  learCursor(BtCur
12ced 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73  sor *pCur){.  as
12cee 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
12cef 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
12cf0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70    sqlite3_free(p
12cf1 43 75 72 2d 3e 70 4b 65 79 29 3b 0a 20 20 70 43  Cur->pKey);.  pC
12cf2 75 72 2d 3e 70 4b 65 79 20 3d 20 30 3b 0a 20 20  ur->pKey = 0;.  
12cf3 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d 20 43  pCur->eState = C
12cf4 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 7d  URSOR_INVALID;.}
12cf5 0a 0a 2f 2a 0a 2a 2a 20 52 65 73 74 6f 72 65 20  ../*.** Restore 
12cf6 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
12cf7 65 20 70 6f 73 69 74 69 6f 6e 20 69 74 20 77 61  e position it wa
12cf8 73 20 69 6e 20 28 6f 72 20 61 73 20 63 6c 6f 73  s in (or as clos
12cf9 65 20 74 6f 20 61 73 20 70 6f 73 73 69 62 6c 65  e to as possible
12cfa 29 0a 2a 2a 20 77 68 65 6e 20 73 61 76 65 43 75  ).** when saveCu
12cfb 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29 20 77  rsorPosition() w
12cfc 61 73 20 63 61 6c 6c 65 64 2e 20 4e 6f 74 65 20  as called. Note 
12cfd 74 68 61 74 20 74 68 69 73 20 63 61 6c 6c 20 64  that this call d
12cfe 65 6c 65 74 65 73 20 74 68 65 20 0a 2a 2a 20 73  eletes the .** s
12cff 61 76 65 64 20 70 6f 73 69 74 69 6f 6e 20 69 6e  aved position in
12d00 66 6f 20 73 74 6f 72 65 64 20 62 79 20 73 61 76  fo stored by sav
12d01 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
12d02 29 2c 20 73 6f 20 74 68 65 72 65 20 63 61 6e 20  ), so there can 
12d03 62 65 0a 2a 2a 20 61 74 20 6d 6f 73 74 20 6f 6e  be.** at most on
12d04 65 20 65 66 66 65 63 74 69 76 65 20 72 65 73 74  e effective rest
12d05 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
12d06 6e 28 29 20 63 61 6c 6c 20 61 66 74 65 72 20 65  n() call after e
12d07 61 63 68 20 0a 2a 2a 20 73 61 76 65 43 75 72 73  ach .** saveCurs
12d08 6f 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f  orPosition()..*/
12d09 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
12d0a 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
12d0b 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  RestoreCursorPos
12d0c 69 74 69 6f 6e 28 42 74 43 75 72 73 6f 72 20 2a  ition(BtCursor *
12d0d 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pCur){.  int rc;
12d0e 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
12d0f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
12d10 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
12d11 43 75 72 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52  Cur->eState>=CUR
12d12 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20  SOR_REQUIRESEEK 
12d13 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65  );.  if( pCur->e
12d14 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41  State==CURSOR_FA
12d15 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ULT ){.    retur
12d16 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20  n pCur->skip;.  
12d17 7d 0a 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65  }.  pCur->eState
12d18 20 3d 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49   = CURSOR_INVALI
12d19 44 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  D;.  rc = sqlite
12d1a 33 42 74 72 65 65 4d 6f 76 65 74 6f 28 70 43 75  3BtreeMoveto(pCu
12d1b 72 2c 20 70 43 75 72 2d 3e 70 4b 65 79 2c 20 70  r, pCur->pKey, p
12d1c 43 75 72 2d 3e 6e 4b 65 79 2c 20 30 2c 20 26 70  Cur->nKey, 0, &p
12d1d 43 75 72 2d 3e 73 6b 69 70 29 3b 0a 20 20 69 66  Cur->skip);.  if
12d1e 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
12d1f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  ){.    sqlite3_f
12d20 72 65 65 28 70 43 75 72 2d 3e 70 4b 65 79 29 3b  ree(pCur->pKey);
12d21 0a 20 20 20 20 70 43 75 72 2d 3e 70 4b 65 79 20  .    pCur->pKey 
12d22 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74 28  = 0;.    assert(
12d23 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
12d24 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20 70  URSOR_VALID || p
12d25 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
12d26 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 3b 0a 20  SOR_INVALID );. 
12d27 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
12d28 7d 0a 0a 23 64 65 66 69 6e 65 20 72 65 73 74 6f  }..#define resto
12d29 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e  reCursorPosition
12d2a 28 70 29 20 5c 0a 20 20 28 70 2d 3e 65 53 74 61  (p) \.  (p->eSta
12d2b 74 65 3e 3d 43 55 52 53 4f 52 5f 52 45 51 55 49  te>=CURSOR_REQUI
12d2c 52 45 53 45 45 4b 20 3f 20 5c 0a 20 20 20 20 20  RESEEK ? \.     
12d2d 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
12d2e 52 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  RestoreCursorPos
12d2f 69 74 69 6f 6e 28 70 29 20 3a 20 5c 0a 20 20 20  ition(p) : \.   
12d30 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 29        SQLITE_OK)
12d31 0a 0a 2f 2a 0a 2a 2a 20 44 65 74 65 72 6d 69 6e  ../*.** Determin
12d32 65 20 77 68 65 74 68 65 72 20 6f 72 20 6e 6f 74  e whether or not
12d33 20 61 20 63 75 72 73 6f 72 20 68 61 73 20 6d 6f   a cursor has mo
12d34 76 65 64 20 66 72 6f 6d 20 74 68 65 20 70 6f 73  ved from the pos
12d35 69 74 69 6f 6e 20 69 74 0a 2a 2a 20 77 61 73 20  ition it.** was 
12d36 6c 61 73 74 20 70 6c 61 63 65 64 20 61 74 2e 20  last placed at. 
12d37 20 43 75 72 73 6f 72 73 20 63 61 6e 20 6d 6f 76   Cursors can mov
12d38 65 20 77 68 65 6e 20 74 68 65 20 72 6f 77 20 74  e when the row t
12d39 68 65 79 20 61 72 65 20 70 6f 69 6e 74 69 6e 67  hey are pointing
12d3a 0a 2a 2a 20 61 74 20 69 73 20 64 65 6c 65 74 65  .** at is delete
12d3b 64 20 6f 75 74 20 66 72 6f 6d 20 75 6e 64 65 72  d out from under
12d3c 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   them..**.** Thi
12d3d 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
12d3e 73 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20  s an error code 
12d3f 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
12d40 73 20 77 72 6f 6e 67 2e 20 20 54 68 65 0a 2a 2a  s wrong.  The.**
12d41 20 69 6e 74 65 67 65 72 20 2a 70 48 61 73 4d 6f   integer *pHasMo
12d42 76 65 64 20 69 73 20 73 65 74 20 74 6f 20 6f 6e  ved is set to on
12d43 65 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20  e if the cursor 
12d44 68 61 73 20 6d 6f 76 65 64 20 61 6e 64 20 30 20  has moved and 0 
12d45 69 66 20 6e 6f 74 2e 0a 2a 2f 0a 53 51 4c 49 54  if not..*/.SQLIT
12d46 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
12d47 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
12d48 48 61 73 4d 6f 76 65 64 28 42 74 43 75 72 73 6f  HasMoved(BtCurso
12d49 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70 48  r *pCur, int *pH
12d4a 61 73 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20  asMoved){.  int 
12d4b 72 63 3b 0a 0a 20 20 72 63 20 3d 20 72 65 73 74  rc;..  rc = rest
12d4c 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
12d4d 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  n(pCur);.  if( r
12d4e 63 20 29 7b 0a 20 20 20 20 2a 70 48 61 73 4d 6f  c ){.    *pHasMo
12d4f 76 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74  ved = 1;.    ret
12d50 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69 66  urn rc;.  }.  if
12d51 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 21 3d  ( pCur->eState!=
12d52 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 7c 7c 20  CURSOR_VALID || 
12d53 70 43 75 72 2d 3e 73 6b 69 70 21 3d 30 20 29 7b  pCur->skip!=0 ){
12d54 0a 20 20 20 20 2a 70 48 61 73 4d 6f 76 65 64 20  .    *pHasMoved 
12d55 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  = 1;.  }else{.  
12d56 20 20 2a 70 48 61 73 4d 6f 76 65 64 20 3d 20 30    *pHasMoved = 0
12d57 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
12d58 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66  QLITE_OK;.}..#if
12d59 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
12d5a 5f 41 55 54 4f 56 41 43 55 55 4d 0a 2f 2a 0a 2a  _AUTOVACUUM./*.*
12d5b 2a 20 47 69 76 65 6e 20 61 20 70 61 67 65 20 6e  * Given a page n
12d5c 75 6d 62 65 72 20 6f 66 20 61 20 72 65 67 75 6c  umber of a regul
12d5d 61 72 20 64 61 74 61 62 61 73 65 20 70 61 67 65  ar database page
12d5e 2c 20 72 65 74 75 72 6e 20 74 68 65 20 70 61 67  , return the pag
12d5f 65 0a 2a 2a 20 6e 75 6d 62 65 72 20 66 6f 72 20  e.** number for 
12d60 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  the pointer-map 
12d61 70 61 67 65 20 74 68 61 74 20 63 6f 6e 74 61 69  page that contai
12d62 6e 73 20 74 68 65 20 65 6e 74 72 79 20 66 6f 72  ns the entry for
12d63 20 74 68 65 0a 2a 2a 20 69 6e 70 75 74 20 70 61   the.** input pa
12d64 67 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74  ge number..*/.st
12d65 61 74 69 63 20 50 67 6e 6f 20 70 74 72 6d 61 70  atic Pgno ptrmap
12d66 50 61 67 65 6e 6f 28 42 74 53 68 61 72 65 64 20  Pageno(BtShared 
12d67 2a 70 42 74 2c 20 50 67 6e 6f 20 70 67 6e 6f 29  *pBt, Pgno pgno)
12d68 7b 0a 20 20 69 6e 74 20 6e 50 61 67 65 73 50 65  {.  int nPagesPe
12d69 72 4d 61 70 50 61 67 65 3b 0a 20 20 50 67 6e 6f  rMapPage;.  Pgno
12d6a 20 69 50 74 72 4d 61 70 2c 20 72 65 74 3b 0a 20   iPtrMap, ret;. 
12d6b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
12d6c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
12d6d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 6e 50 61  >mutex) );.  nPa
12d6e 67 65 73 50 65 72 4d 61 70 50 61 67 65 20 3d 20  gesPerMapPage = 
12d6f 28 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  (pBt->usableSize
12d70 2f 35 29 2b 31 3b 0a 20 20 69 50 74 72 4d 61 70  /5)+1;.  iPtrMap
12d71 20 3d 20 28 70 67 6e 6f 2d 32 29 2f 6e 50 61 67   = (pgno-2)/nPag
12d72 65 73 50 65 72 4d 61 70 50 61 67 65 3b 0a 20 20  esPerMapPage;.  
12d73 72 65 74 20 3d 20 28 69 50 74 72 4d 61 70 2a 6e  ret = (iPtrMap*n
12d74 50 61 67 65 73 50 65 72 4d 61 70 50 61 67 65 29  PagesPerMapPage)
12d75 20 2b 20 32 3b 20 0a 20 20 69 66 28 20 72 65 74   + 2; .  if( ret
12d76 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  ==PENDING_BYTE_P
12d77 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20  AGE(pBt) ){.    
12d78 72 65 74 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74  ret++;.  }.  ret
12d79 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn ret;.}../*.*
12d7a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e 74 72 79  * Write an entry
12d7b 20 69 6e 74 6f 20 74 68 65 20 70 6f 69 6e 74 65   into the pointe
12d7c 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  r map..**.** Thi
12d7d 73 20 72 6f 75 74 69 6e 65 20 75 70 64 61 74 65  s routine update
12d7e 73 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  s the pointer ma
12d7f 70 20 65 6e 74 72 79 20 66 6f 72 20 70 61 67 65  p entry for page
12d80 20 6e 75 6d 62 65 72 20 27 6b 65 79 27 0a 2a 2a   number 'key'.**
12d81 20 73 6f 20 74 68 61 74 20 69 74 20 6d 61 70 73   so that it maps
12d82 20 74 6f 20 74 79 70 65 20 27 65 54 79 70 65 27   to type 'eType'
12d83 20 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65   and parent page
12d84 20 6e 75 6d 62 65 72 20 27 70 67 6e 6f 27 2e 0a   number 'pgno'..
12d85 2a 2a 20 41 6e 20 65 72 72 6f 72 20 63 6f 64 65  ** An error code
12d86 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20   is returned if 
12d87 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65 73 20 77  something goes w
12d88 72 6f 6e 67 2c 20 6f 74 68 65 72 77 69 73 65 20  rong, otherwise 
12d89 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74  SQLITE_OK..*/.st
12d8a 61 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50  atic int ptrmapP
12d8b 75 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ut(BtShared *pBt
12d8c 2c 20 50 67 6e 6f 20 6b 65 79 2c 20 75 38 20 65  , Pgno key, u8 e
12d8d 54 79 70 65 2c 20 50 67 6e 6f 20 70 61 72 65 6e  Type, Pgno paren
12d8e 74 29 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44  t){.  DbPage *pD
12d8f 62 50 61 67 65 3b 20 20 2f 2a 20 54 68 65 20 70  bPage;  /* The p
12d90 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20  ointer map page 
12d91 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70  */.  u8 *pPtrmap
12d92 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f  ;      /* The po
12d93 69 6e 74 65 72 20 6d 61 70 20 64 61 74 61 20 2a  inter map data *
12d94 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 6d 61 70  /.  Pgno iPtrmap
12d95 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 70 6f 69  ;     /* The poi
12d96 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 6e 75  nter map page nu
12d97 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6f 66  mber */.  int of
12d98 66 73 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 4f  fset;       /* O
12d99 66 66 73 65 74 20 69 6e 20 70 6f 69 6e 74 65 72  ffset in pointer
12d9a 20 6d 61 70 20 70 61 67 65 20 2a 2f 0a 20 20 69   map page */.  i
12d9b 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
12d9c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
12d9d 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29  held(pBt->mutex)
12d9e 20 29 3b 0a 20 20 2f 2a 20 54 68 65 20 6d 61 73   );.  /* The mas
12d9f 74 65 72 2d 6a 6f 75 72 6e 61 6c 20 70 61 67 65  ter-journal page
12da0 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 6e 65 76   number must nev
12da1 65 72 20 62 65 20 75 73 65 64 20 61 73 20 61 20  er be used as a 
12da2 70 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 67 65  pointer map page
12da3 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 30 3d   */.  assert( 0=
12da4 3d 50 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70  =PTRMAP_ISPAGE(p
12da5 42 74 2c 20 50 45 4e 44 49 4e 47 5f 42 59 54 45  Bt, PENDING_BYTE
12da6 5f 50 41 47 45 28 70 42 74 29 29 20 29 3b 0a 0a  _PAGE(pBt)) );..
12da7 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 61    assert( pBt->a
12da8 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20 69  utoVacuum );.  i
12da9 66 28 20 6b 65 79 3d 3d 30 20 29 7b 0a 20 20 20  f( key==0 ){.   
12daa 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
12dab 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d  ORRUPT_BKPT;.  }
12dac 0a 20 20 69 50 74 72 6d 61 70 20 3d 20 50 54 52  .  iPtrmap = PTR
12dad 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c 20  MAP_PAGENO(pBt, 
12dae 6b 65 79 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  key);.  rc = sql
12daf 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74  ite3PagerGet(pBt
12db0 2d 3e 70 50 61 67 65 72 2c 20 69 50 74 72 6d 61  ->pPager, iPtrma
12db1 70 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20  p, &pDbPage);.  
12db2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
12db3 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  K ){.    return 
12db4 72 63 3b 0a 20 20 7d 0a 20 20 6f 66 66 73 65 74  rc;.  }.  offset
12db5 20 3d 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46   = PTRMAP_PTROFF
12db6 53 45 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79  SET(iPtrmap, key
12db7 29 3b 0a 20 20 70 50 74 72 6d 61 70 20 3d 20 28  );.  pPtrmap = (
12db8 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61 67 65  u8 *)sqlite3Page
12db9 72 47 65 74 44 61 74 61 28 70 44 62 50 61 67 65  rGetData(pDbPage
12dba 29 3b 0a 0a 20 20 69 66 28 20 65 54 79 70 65 21  );..  if( eType!
12dbb 3d 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d  =pPtrmap[offset]
12dbc 20 7c 7c 20 67 65 74 34 62 79 74 65 28 26 70 50   || get4byte(&pP
12dbd 74 72 6d 61 70 5b 6f 66 66 73 65 74 2b 31 5d 29  trmap[offset+1])
12dbe 21 3d 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  !=parent ){.    
12dbf 54 52 41 43 45 28 28 22 50 54 52 4d 41 50 5f 55  TRACE(("PTRMAP_U
12dc0 50 44 41 54 45 3a 20 25 64 2d 3e 28 25 64 2c 25  PDATE: %d->(%d,%
12dc1 64 29 5c 6e 22 2c 20 6b 65 79 2c 20 65 54 79 70  d)\n", key, eTyp
12dc2 65 2c 20 70 61 72 65 6e 74 29 29 3b 0a 20 20 20  e, parent));.   
12dc3 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
12dc4 65 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29  erWrite(pDbPage)
12dc5 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
12dc6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
12dc7 20 70 50 74 72 6d 61 70 5b 6f 66 66 73 65 74 5d   pPtrmap[offset]
12dc8 20 3d 20 65 54 79 70 65 3b 0a 20 20 20 20 20 20   = eType;.      
12dc9 70 75 74 34 62 79 74 65 28 26 70 50 74 72 6d 61  put4byte(&pPtrma
12dca 70 5b 6f 66 66 73 65 74 2b 31 5d 2c 20 70 61 72  p[offset+1], par
12dcb 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ent);.    }.  }.
12dcc 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55  .  sqlite3PagerU
12dcd 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20  nref(pDbPage);. 
12dce 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
12dcf 2a 0a 2a 2a 20 52 65 61 64 20 61 6e 20 65 6e 74  *.** Read an ent
12dd0 72 79 20 66 72 6f 6d 20 74 68 65 20 70 6f 69 6e  ry from the poin
12dd1 74 65 72 20 6d 61 70 2e 0a 2a 2a 0a 2a 2a 20 54  ter map..**.** T
12dd2 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 72  his routine retr
12dd3 69 65 76 65 73 20 74 68 65 20 70 6f 69 6e 74 65  ieves the pointe
12dd4 72 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20  r map entry for 
12dd5 70 61 67 65 20 27 6b 65 79 27 2c 20 77 72 69 74  page 'key', writ
12dd6 69 6e 67 0a 2a 2a 20 74 68 65 20 74 79 70 65 20  ing.** the type 
12dd7 61 6e 64 20 70 61 72 65 6e 74 20 70 61 67 65 20  and parent page 
12dd8 6e 75 6d 62 65 72 20 74 6f 20 2a 70 45 54 79 70  number to *pETyp
12dd9 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 72 65 73  e and *pPgno res
12dda 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2a 20 41 6e  pectively..** An
12ddb 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 72   error code is r
12ddc 65 74 75 72 6e 65 64 20 69 66 20 73 6f 6d 65 74  eturned if somet
12ddd 68 69 6e 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c  hing goes wrong,
12dde 20 6f 74 68 65 72 77 69 73 65 20 53 51 4c 49 54   otherwise SQLIT
12ddf 45 5f 4f 4b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  E_OK..*/.static 
12de0 69 6e 74 20 70 74 72 6d 61 70 47 65 74 28 42 74  int ptrmapGet(Bt
12de1 53 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e  Shared *pBt, Pgn
12de2 6f 20 6b 65 79 2c 20 75 38 20 2a 70 45 54 79 70  o key, u8 *pETyp
12de3 65 2c 20 50 67 6e 6f 20 2a 70 50 67 6e 6f 29 7b  e, Pgno *pPgno){
12de4 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50 61  .  DbPage *pDbPa
12de5 67 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 6f 69  ge;   /* The poi
12de6 6e 74 65 72 20 6d 61 70 20 70 61 67 65 20 2a 2f  nter map page */
12de7 0a 20 20 69 6e 74 20 69 50 74 72 6d 61 70 3b 20  .  int iPtrmap; 
12de8 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72        /* Pointer
12de9 20 6d 61 70 20 70 61 67 65 20 69 6e 64 65 78 20   map page index 
12dea 2a 2f 0a 20 20 75 38 20 2a 70 50 74 72 6d 61 70  */.  u8 *pPtrmap
12deb 3b 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74  ;       /* Point
12dec 65 72 20 6d 61 70 20 70 61 67 65 20 64 61 74 61  er map page data
12ded 20 2a 2f 0a 20 20 69 6e 74 20 6f 66 66 73 65 74   */.  int offset
12dee 3b 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73  ;        /* Offs
12def 65 74 20 6f 66 20 65 6e 74 72 79 20 69 6e 20 70  et of entry in p
12df0 6f 69 6e 74 65 72 20 6d 61 70 20 2a 2f 0a 20 20  ointer map */.  
12df1 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
12df2 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12df3 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
12df4 29 20 29 3b 0a 0a 20 20 69 50 74 72 6d 61 70 20  ) );..  iPtrmap 
12df5 3d 20 50 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28  = PTRMAP_PAGENO(
12df6 70 42 74 2c 20 6b 65 79 29 3b 0a 20 20 72 63 20  pBt, key);.  rc 
12df7 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
12df8 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 69  t(pBt->pPager, i
12df9 50 74 72 6d 61 70 2c 20 26 70 44 62 50 61 67 65  Ptrmap, &pDbPage
12dfa 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 30 20 29  );.  if( rc!=0 )
12dfb 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
12dfc 0a 20 20 7d 0a 20 20 70 50 74 72 6d 61 70 20 3d  .  }.  pPtrmap =
12dfd 20 28 75 38 20 2a 29 73 71 6c 69 74 65 33 50 61   (u8 *)sqlite3Pa
12dfe 67 65 72 47 65 74 44 61 74 61 28 70 44 62 50 61  gerGetData(pDbPa
12dff 67 65 29 3b 0a 0a 20 20 6f 66 66 73 65 74 20 3d  ge);..  offset =
12e00 20 50 54 52 4d 41 50 5f 50 54 52 4f 46 46 53 45   PTRMAP_PTROFFSE
12e01 54 28 69 50 74 72 6d 61 70 2c 20 6b 65 79 29 3b  T(iPtrmap, key);
12e02 0a 20 20 61 73 73 65 72 74 28 20 70 45 54 79 70  .  assert( pETyp
12e03 65 21 3d 30 20 29 3b 0a 20 20 2a 70 45 54 79 70  e!=0 );.  *pETyp
12e04 65 20 3d 20 70 50 74 72 6d 61 70 5b 6f 66 66 73  e = pPtrmap[offs
12e05 65 74 5d 3b 0a 20 20 69 66 28 20 70 50 67 6e 6f  et];.  if( pPgno
12e06 20 29 20 2a 70 50 67 6e 6f 20 3d 20 67 65 74 34   ) *pPgno = get4
12e07 62 79 74 65 28 26 70 50 74 72 6d 61 70 5b 6f 66  byte(&pPtrmap[of
12e08 66 73 65 74 2b 31 5d 29 3b 0a 0a 20 20 73 71 6c  fset+1]);..  sql
12e09 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28 70  ite3PagerUnref(p
12e0a 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20 2a  DbPage);.  if( *
12e0b 70 45 54 79 70 65 3c 31 20 7c 7c 20 2a 70 45 54  pEType<1 || *pET
12e0c 79 70 65 3e 35 20 29 20 72 65 74 75 72 6e 20 53  ype>5 ) return S
12e0d 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
12e0e 50 54 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  PT;.  return SQL
12e0f 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6c 73 65  ITE_OK;.}..#else
12e10 20 2f 2a 20 69 66 20 64 65 66 69 6e 65 64 20 53   /* if defined S
12e11 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
12e12 41 43 55 55 4d 20 2a 2f 0a 20 20 23 64 65 66 69  ACUUM */.  #defi
12e13 6e 65 20 70 74 72 6d 61 70 50 75 74 28 77 2c 78  ne ptrmapPut(w,x
12e14 2c 79 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a  ,y,z) SQLITE_OK.
12e15 20 20 23 64 65 66 69 6e 65 20 70 74 72 6d 61 70    #define ptrmap
12e16 47 65 74 28 77 2c 78 2c 79 2c 7a 29 20 53 51 4c  Get(w,x,y,z) SQL
12e17 49 54 45 5f 4f 4b 0a 20 20 23 64 65 66 69 6e 65  ITE_OK.  #define
12e18 20 70 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 79   ptrmapPutOvfl(y
12e19 2c 7a 29 20 53 51 4c 49 54 45 5f 4f 4b 0a 23 65  ,z) SQLITE_OK.#e
12e1a 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  ndif../*.** Give
12e1b 6e 20 61 20 62 74 72 65 65 20 70 61 67 65 20 61  n a btree page a
12e1c 6e 64 20 61 20 63 65 6c 6c 20 69 6e 64 65 78 20  nd a cell index 
12e1d 28 30 20 6d 65 61 6e 73 20 74 68 65 20 66 69 72  (0 means the fir
12e1e 73 74 20 63 65 6c 6c 20 6f 6e 0a 2a 2a 20 74 68  st cell on.** th
12e1f 65 20 70 61 67 65 2c 20 31 20 6d 65 61 6e 73 20  e page, 1 means 
12e20 74 68 65 20 73 65 63 6f 6e 64 20 63 65 6c 6c 2c  the second cell,
12e21 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 29 20 72   and so forth) r
12e22 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 0a  eturn a pointer.
12e23 2a 2a 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 63  ** to the cell c
12e24 6f 6e 74 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68  ontent..**.** Th
12e25 69 73 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73  is routine works
12e26 20 6f 6e 6c 79 20 66 6f 72 20 70 61 67 65 73 20   only for pages 
12e27 74 68 61 74 20 64 6f 20 6e 6f 74 20 63 6f 6e 74  that do not cont
12e28 61 69 6e 20 6f 76 65 72 66 6c 6f 77 20 63 65 6c  ain overflow cel
12e29 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66  ls..*/.#define f
12e2a 69 6e 64 43 65 6c 6c 28 50 2c 49 29 20 5c 0a 20  indCell(P,I) \. 
12e2b 20 28 28 50 29 2d 3e 61 44 61 74 61 20 2b 20 28   ((P)->aData + (
12e2c 28 50 29 2d 3e 6d 61 73 6b 50 61 67 65 20 26 20  (P)->maskPage & 
12e2d 67 65 74 32 62 79 74 65 28 26 28 50 29 2d 3e 61  get2byte(&(P)->a
12e2e 44 61 74 61 5b 28 50 29 2d 3e 63 65 6c 6c 4f 66  Data[(P)->cellOf
12e2f 66 73 65 74 2b 32 2a 28 49 29 5d 29 29 29 0a 0a  fset+2*(I)])))..
12e30 2f 2a 0a 2a 2a 20 54 68 69 73 20 61 20 6d 6f 72  /*.** This a mor
12e31 65 20 63 6f 6d 70 6c 65 78 20 76 65 72 73 69 6f  e complex versio
12e32 6e 20 6f 66 20 66 69 6e 64 43 65 6c 6c 28 29 20  n of findCell() 
12e33 74 68 61 74 20 77 6f 72 6b 73 20 66 6f 72 0a 2a  that works for.*
12e34 2a 20 70 61 67 65 73 20 74 68 61 74 20 64 6f 20  * pages that do 
12e35 63 6f 6e 74 61 69 6e 20 6f 76 65 72 66 6c 6f 77  contain overflow
12e36 20 63 65 6c 6c 73 2e 20 20 53 65 65 20 69 6e 73   cells.  See ins
12e37 65 72 74 0a 2a 2f 0a 73 74 61 74 69 63 20 75 38  ert.*/.static u8
12e38 20 2a 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65   *findOverflowCe
12e39 6c 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  ll(MemPage *pPag
12e3a 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29 7b 0a 20  e, int iCell){. 
12e3b 20 69 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74   int i;.  assert
12e3c 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
12e3d 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
12e3e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 66 6f 72  >mutex) );.  for
12e3f 28 69 3d 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66  (i=pPage->nOverf
12e40 6c 6f 77 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d  low-1; i>=0; i--
12e41 29 7b 0a 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20  ){.    int k;.  
12e42 20 20 73 74 72 75 63 74 20 5f 4f 76 66 6c 43 65    struct _OvflCe
12e43 6c 6c 20 2a 70 4f 76 66 6c 3b 0a 20 20 20 20 70  ll *pOvfl;.    p
12e44 4f 76 66 6c 20 3d 20 26 70 50 61 67 65 2d 3e 61  Ovfl = &pPage->a
12e45 4f 76 66 6c 5b 69 5d 3b 0a 20 20 20 20 6b 20 3d  Ovfl[i];.    k =
12e46 20 70 4f 76 66 6c 2d 3e 69 64 78 3b 0a 20 20 20   pOvfl->idx;.   
12e47 20 69 66 28 20 6b 3c 3d 69 43 65 6c 6c 20 29 7b   if( k<=iCell ){
12e48 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 69 43  .      if( k==iC
12e49 65 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ell ){.        r
12e4a 65 74 75 72 6e 20 70 4f 76 66 6c 2d 3e 70 43 65  eturn pOvfl->pCe
12e4b 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ll;.      }.    
12e4c 20 20 69 43 65 6c 6c 2d 2d 3b 0a 20 20 20 20 7d    iCell--;.    }
12e4d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 66 69  .  }.  return fi
12e4e 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  ndCell(pPage, iC
12e4f 65 6c 6c 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ell);.}../*.** P
12e50 61 72 73 65 20 61 20 63 65 6c 6c 20 63 6f 6e 74  arse a cell cont
12e51 65 6e 74 20 62 6c 6f 63 6b 20 61 6e 64 20 66 69  ent block and fi
12e52 6c 6c 20 69 6e 20 74 68 65 20 43 65 6c 6c 49 6e  ll in the CellIn
12e53 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  fo structure.  T
12e54 68 65 72 65 0a 2a 2a 20 61 72 65 20 74 77 6f 20  here.** are two 
12e55 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 69 73  versions of this
12e56 20 66 75 6e 63 74 69 6f 6e 2e 20 20 73 71 6c 69   function.  sqli
12e57 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65 6c  te3BtreeParseCel
12e58 6c 28 29 20 74 61 6b 65 73 20 61 20 0a 2a 2a 20  l() takes a .** 
12e59 63 65 6c 6c 20 69 6e 64 65 78 20 61 73 20 74 68  cell index as th
12e5a 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e  e second argumen
12e5b 74 20 61 6e 64 20 73 71 6c 69 74 65 33 42 74 72  t and sqlite3Btr
12e5c 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 29  eeParseCellPtr()
12e5d 20 0a 2a 2a 20 74 61 6b 65 73 20 61 20 70 6f 69   .** takes a poi
12e5e 6e 74 65 72 20 74 6f 20 74 68 65 20 62 6f 64 79  nter to the body
12e5f 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 61 73 20   of the cell as 
12e60 69 74 73 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  its second argum
12e61 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 69  ent..**.** Withi
12e62 6e 20 74 68 69 73 20 66 69 6c 65 2c 20 74 68 65  n this file, the
12e63 20 70 61 72 73 65 43 65 6c 6c 28 29 20 6d 61 63   parseCell() mac
12e64 72 6f 20 63 61 6e 20 62 65 20 63 61 6c 6c 65 64  ro can be called
12e65 20 69 6e 73 74 65 61 64 20 6f 66 0a 2a 2a 20 73   instead of.** s
12e66 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65  qlite3BtreeParse
12e67 43 65 6c 6c 50 74 72 28 29 2e 20 55 73 69 6e 67  CellPtr(). Using
12e68 20 73 6f 6d 65 20 63 6f 6d 70 69 6c 65 72 73 2c   some compilers,
12e69 20 74 68 69 73 20 77 69 6c 6c 20 62 65 20 66 61   this will be fa
12e6a 73 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ster..*/.SQLITE_
12e6b 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
12e6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
12e6d 6c 6c 50 74 72 28 0a 20 20 4d 65 6d 50 61 67 65  llPtr(.  MemPage
12e6e 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
12e6f 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
12e70 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
12e71 20 20 75 38 20 2a 70 43 65 6c 6c 2c 20 20 20 20    u8 *pCell,    
12e72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69            /* Poi
12e73 6e 74 65 72 20 74 6f 20 74 68 65 20 63 65 6c 6c  nter to the cell
12e74 20 74 65 78 74 2e 20 2a 2f 0a 20 20 43 65 6c 6c   text. */.  Cell
12e75 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 20 20 20 20  Info *pInfo     
12e76 20 20 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20 74      /* Fill in t
12e77 68 69 73 20 73 74 72 75 63 74 75 72 65 20 2a 2f  his structure */
12e78 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20  .){.  int n;    
12e79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
12e7a 20 4e 75 6d 62 65 72 20 62 79 74 65 73 20 69 6e   Number bytes in
12e7b 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 65   cell content he
12e7c 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20 6e 50  ader */.  u32 nP
12e7d 61 79 6c 6f 61 64 3b 20 20 20 20 20 20 20 20 20  ayload;         
12e7e 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62    /* Number of b
12e7f 79 74 65 73 20 6f 66 20 63 65 6c 6c 20 70 61 79  ytes of cell pay
12e80 6c 6f 61 64 20 2a 2f 0a 0a 20 20 61 73 73 65 72  load */..  asser
12e81 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
12e82 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
12e83 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 70  ->mutex) );..  p
12e84 49 6e 66 6f 2d 3e 70 43 65 6c 6c 20 3d 20 70 43  Info->pCell = pC
12e85 65 6c 6c 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ell;.  assert( p
12e86 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 30 20 7c 7c  Page->leaf==0 ||
12e87 20 70 50 61 67 65 2d 3e 6c 65 61 66 3d 3d 31 20   pPage->leaf==1 
12e88 29 3b 0a 20 20 6e 20 3d 20 70 50 61 67 65 2d 3e  );.  n = pPage->
12e89 63 68 69 6c 64 50 74 72 53 69 7a 65 3b 0a 20 20  childPtrSize;.  
12e8a 61 73 73 65 72 74 28 20 6e 3d 3d 34 2d 34 2a 70  assert( n==4-4*p
12e8b 50 61 67 65 2d 3e 6c 65 61 66 20 29 3b 0a 20 20  Page->leaf );.  
12e8c 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  if( pPage->intKe
12e8d 79 20 29 7b 0a 20 20 20 20 69 66 28 20 70 50 61  y ){.    if( pPa
12e8e 67 65 2d 3e 68 61 73 44 61 74 61 20 29 7b 0a 20  ge->hasData ){. 
12e8f 20 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72       n += getVar
12e90 69 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c  int32(&pCell[n],
12e91 20 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20   nPayload);.    
12e92 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 50 61  }else{.      nPa
12e93 79 6c 6f 61 64 20 3d 20 30 3b 0a 20 20 20 20 7d  yload = 0;.    }
12e94 0a 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72  .    n += getVar
12e95 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20 28  int(&pCell[n], (
12e96 75 36 34 2a 29 26 70 49 6e 66 6f 2d 3e 6e 4b 65  u64*)&pInfo->nKe
12e97 79 29 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e  y);.    pInfo->n
12e98 44 61 74 61 20 3d 20 6e 50 61 79 6c 6f 61 64 3b  Data = nPayload;
12e99 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49  .  }else{.    pI
12e9a 6e 66 6f 2d 3e 6e 44 61 74 61 20 3d 20 30 3b 0a  nfo->nData = 0;.
12e9b 20 20 20 20 6e 20 2b 3d 20 67 65 74 56 61 72 69      n += getVari
12e9c 6e 74 33 32 28 26 70 43 65 6c 6c 5b 6e 5d 2c 20  nt32(&pCell[n], 
12e9d 6e 50 61 79 6c 6f 61 64 29 3b 0a 20 20 20 20 70  nPayload);.    p
12e9e 49 6e 66 6f 2d 3e 6e 4b 65 79 20 3d 20 6e 50 61  Info->nKey = nPa
12e9f 79 6c 6f 61 64 3b 0a 20 20 7d 0a 20 20 70 49 6e  yload;.  }.  pIn
12ea0 66 6f 2d 3e 6e 50 61 79 6c 6f 61 64 20 3d 20 6e  fo->nPayload = n
12ea1 50 61 79 6c 6f 61 64 3b 0a 20 20 70 49 6e 66 6f  Payload;.  pInfo
12ea2 2d 3e 6e 48 65 61 64 65 72 20 3d 20 6e 3b 0a 20  ->nHeader = n;. 
12ea3 20 69 66 28 20 6c 69 6b 65 6c 79 28 6e 50 61 79   if( likely(nPay
12ea4 6c 6f 61 64 3c 3d 70 50 61 67 65 2d 3e 6d 61 78  load<=pPage->max
12ea5 4c 6f 63 61 6c 29 20 29 7b 0a 20 20 20 20 2f 2a  Local) ){.    /*
12ea6 20 54 68 69 73 20 69 73 20 74 68 65 20 28 65 61   This is the (ea
12ea7 73 79 29 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20  sy) common case 
12ea8 77 68 65 72 65 20 74 68 65 20 65 6e 74 69 72 65  where the entire
12ea9 20 70 61 79 6c 6f 61 64 20 66 69 74 73 0a 20 20   payload fits.  
12eaa 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c 6f 63 61    ** on the loca
12eab 6c 20 70 61 67 65 2e 20 20 4e 6f 20 6f 76 65 72  l page.  No over
12eac 66 6c 6f 77 20 69 73 20 72 65 71 75 69 72 65 64  flow is required
12ead 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
12eae 20 6e 53 69 7a 65 3b 20 20 20 20 20 20 20 20 20   nSize;         
12eaf 20 2f 2a 20 54 6f 74 61 6c 20 73 69 7a 65 20 6f   /* Total size o
12eb0 66 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 69  f cell content i
12eb1 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 20 20 6e  n bytes */.    n
12eb2 53 69 7a 65 20 3d 20 6e 50 61 79 6c 6f 61 64 20  Size = nPayload 
12eb3 2b 20 6e 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e  + n;.    pInfo->
12eb4 6e 4c 6f 63 61 6c 20 3d 20 6e 50 61 79 6c 6f 61  nLocal = nPayloa
12eb5 64 3b 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f  d;.    pInfo->iO
12eb6 76 65 72 66 6c 6f 77 20 3d 20 30 3b 0a 20 20 20  verflow = 0;.   
12eb7 20 69 66 28 20 28 6e 53 69 7a 65 20 26 20 7e 33   if( (nSize & ~3
12eb8 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53  )==0 ){.      nS
12eb9 69 7a 65 20 3d 20 34 3b 20 20 20 20 20 20 20 20  ize = 4;        
12eba 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63 65 6c 6c 20  /* Minimum cell 
12ebb 73 69 7a 65 20 69 73 20 34 20 2a 2f 0a 20 20 20  size is 4 */.   
12ebc 20 7d 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 53   }.    pInfo->nS
12ebd 69 7a 65 20 3d 20 6e 53 69 7a 65 3b 0a 20 20 7d  ize = nSize;.  }
12ebe 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 49 66 20  else{.    /* If 
12ebf 74 68 65 20 70 61 79 6c 6f 61 64 20 77 69 6c 6c  the payload will
12ec0 20 6e 6f 74 20 66 69 74 20 63 6f 6d 70 6c 65 74   not fit complet
12ec1 65 6c 79 20 6f 6e 20 74 68 65 20 6c 6f 63 61 6c  ely on the local
12ec2 20 70 61 67 65 2c 20 77 65 20 68 61 76 65 0a 20   page, we have. 
12ec3 20 20 20 2a 2a 20 74 6f 20 64 65 63 69 64 65 20     ** to decide 
12ec4 68 6f 77 20 6d 75 63 68 20 74 6f 20 73 74 6f 72  how much to stor
12ec5 65 20 6c 6f 63 61 6c 6c 79 20 61 6e 64 20 68 6f  e locally and ho
12ec6 77 20 6d 75 63 68 20 74 6f 20 73 70 69 6c 6c 20  w much to spill 
12ec7 6f 6e 74 6f 0a 20 20 20 20 2a 2a 20 6f 76 65 72  onto.    ** over
12ec8 66 6c 6f 77 20 70 61 67 65 73 2e 20 20 54 68 65  flow pages.  The
12ec9 20 73 74 72 61 74 65 67 79 20 69 73 20 74 6f 20   strategy is to 
12eca 6d 69 6e 69 6d 69 7a 65 20 74 68 65 20 61 6d 6f  minimize the amo
12ecb 75 6e 74 20 6f 66 20 75 6e 75 73 65 64 0a 20 20  unt of unused.  
12ecc 20 20 2a 2a 20 73 70 61 63 65 20 6f 6e 20 6f 76    ** space on ov
12ecd 65 72 66 6c 6f 77 20 70 61 67 65 73 20 77 68 69  erflow pages whi
12ece 6c 65 20 6b 65 65 70 69 6e 67 20 74 68 65 20 61  le keeping the a
12ecf 6d 6f 75 6e 74 20 6f 66 20 6c 6f 63 61 6c 20 73  mount of local s
12ed0 74 6f 72 61 67 65 0a 20 20 20 20 2a 2a 20 69 6e  torage.    ** in
12ed1 20 62 65 74 77 65 65 6e 20 6d 69 6e 4c 6f 63 61   between minLoca
12ed2 6c 20 61 6e 64 20 6d 61 78 4c 6f 63 61 6c 2e 0a  l and maxLocal..
12ed3 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 57 61      **.    ** Wa
12ed4 72 6e 69 6e 67 3a 20 20 63 68 61 6e 67 69 6e 67  rning:  changing
12ed5 20 74 68 65 20 77 61 79 20 6f 76 65 72 66 6c 6f   the way overflo
12ed6 77 20 70 61 79 6c 6f 61 64 20 69 73 20 64 69 73  w payload is dis
12ed7 74 72 69 62 75 74 65 64 20 69 6e 20 61 6e 79 0a  tributed in any.
12ed8 20 20 20 20 2a 2a 20 77 61 79 20 77 69 6c 6c 20      ** way will 
12ed9 72 65 73 75 6c 74 20 69 6e 20 61 6e 20 69 6e 63  result in an inc
12eda 6f 6d 70 61 74 69 62 6c 65 20 66 69 6c 65 20 66  ompatible file f
12edb 6f 72 6d 61 74 2e 0a 20 20 20 20 2a 2f 0a 20 20  ormat..    */.  
12edc 20 20 69 6e 74 20 6d 69 6e 4c 6f 63 61 6c 3b 20    int minLocal; 
12edd 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 61 6d 6f 75   /* Minimum amou
12ede 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65  nt of payload he
12edf 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20  ld locally */.  
12ee0 20 20 69 6e 74 20 6d 61 78 4c 6f 63 61 6c 3b 20    int maxLocal; 
12ee1 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 61 6d 6f 75   /* Maximum amou
12ee2 6e 74 20 6f 66 20 70 61 79 6c 6f 61 64 20 68 65  nt of payload he
12ee3 6c 64 20 6c 6f 63 61 6c 6c 79 20 2a 2f 0a 20 20  ld locally */.  
12ee4 20 20 69 6e 74 20 73 75 72 70 6c 75 73 3b 20 20    int surplus;  
12ee5 20 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 79   /* Overflow pay
12ee6 6c 6f 61 64 20 61 76 61 69 6c 61 62 6c 65 20 66  load available f
12ee7 6f 72 20 6c 6f 63 61 6c 20 73 74 6f 72 61 67 65  or local storage
12ee8 20 2a 2f 0a 0a 20 20 20 20 6d 69 6e 4c 6f 63 61   */..    minLoca
12ee9 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 69 6e 4c 6f  l = pPage->minLo
12eea 63 61 6c 3b 0a 20 20 20 20 6d 61 78 4c 6f 63 61  cal;.    maxLoca
12eeb 6c 20 3d 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f  l = pPage->maxLo
12eec 63 61 6c 3b 0a 20 20 20 20 73 75 72 70 6c 75 73  cal;.    surplus
12eed 20 3d 20 6d 69 6e 4c 6f 63 61 6c 20 2b 20 28 6e   = minLocal + (n
12eee 50 61 79 6c 6f 61 64 20 2d 20 6d 69 6e 4c 6f 63  Payload - minLoc
12eef 61 6c 29 25 28 70 50 61 67 65 2d 3e 70 42 74 2d  al)%(pPage->pBt-
12ef0 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d 20 34 29  >usableSize - 4)
12ef1 3b 0a 20 20 20 20 69 66 28 20 73 75 72 70 6c 75  ;.    if( surplu
12ef2 73 20 3c 3d 20 6d 61 78 4c 6f 63 61 6c 20 29 7b  s <= maxLocal ){
12ef3 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 6e 4c  .      pInfo->nL
12ef4 6f 63 61 6c 20 3d 20 73 75 72 70 6c 75 73 3b 0a  ocal = surplus;.
12ef5 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
12ef6 20 70 49 6e 66 6f 2d 3e 6e 4c 6f 63 61 6c 20 3d   pInfo->nLocal =
12ef7 20 6d 69 6e 4c 6f 63 61 6c 3b 0a 20 20 20 20 7d   minLocal;.    }
12ef8 0a 20 20 20 20 70 49 6e 66 6f 2d 3e 69 4f 76 65  .    pInfo->iOve
12ef9 72 66 6c 6f 77 20 3d 20 70 49 6e 66 6f 2d 3e 6e  rflow = pInfo->n
12efa 4c 6f 63 61 6c 20 2b 20 6e 3b 0a 20 20 20 20 70  Local + n;.    p
12efb 49 6e 66 6f 2d 3e 6e 53 69 7a 65 20 3d 20 70 49  Info->nSize = pI
12efc 6e 66 6f 2d 3e 69 4f 76 65 72 66 6c 6f 77 20 2b  nfo->iOverflow +
12efd 20 34 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e   4;.  }.}.#defin
12efe 65 20 70 61 72 73 65 43 65 6c 6c 28 70 50 61 67  e parseCell(pPag
12eff 65 2c 20 69 43 65 6c 6c 2c 20 70 49 6e 66 6f 29  e, iCell, pInfo)
12f00 20 5c 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65   \.  sqlite3Btre
12f01 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 28 70  eParseCellPtr((p
12f02 50 61 67 65 29 2c 20 66 69 6e 64 43 65 6c 6c 28  Page), findCell(
12f03 28 70 50 61 67 65 29 2c 20 28 69 43 65 6c 6c 29  (pPage), (iCell)
12f04 29 2c 20 28 70 49 6e 66 6f 29 29 0a 53 51 4c 49  ), (pInfo)).SQLI
12f05 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
12f06 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72 73  sqlite3BtreePars
12f07 65 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65  eCell(.  MemPage
12f08 20 2a 70 50 61 67 65 2c 20 20 20 20 20 20 20 20   *pPage,        
12f09 20 2f 2a 20 50 61 67 65 20 63 6f 6e 74 61 69 6e   /* Page contain
12f0a 69 6e 67 20 74 68 65 20 63 65 6c 6c 20 2a 2f 0a  ing the cell */.
12f0b 20 20 69 6e 74 20 69 43 65 6c 6c 2c 20 20 20 20    int iCell,    
12f0c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
12f0d 20 63 65 6c 6c 20 69 6e 64 65 78 2e 20 20 46 69   cell index.  Fi
12f0e 72 73 74 20 63 65 6c 6c 20 69 73 20 30 20 2a 2f  rst cell is 0 */
12f0f 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 2a 70 49 6e  .  CellInfo *pIn
12f10 66 6f 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  fo         /* Fi
12f11 6c 6c 20 69 6e 20 74 68 69 73 20 73 74 72 75 63  ll in this struc
12f12 74 75 72 65 20 2a 2f 0a 29 7b 0a 20 20 70 61 72  ture */.){.  par
12f13 73 65 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 43  seCell(pPage, iC
12f14 65 6c 6c 2c 20 70 49 6e 66 6f 29 3b 0a 7d 0a 0a  ell, pInfo);.}..
12f15 2f 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  /*.** Compute th
12f16 65 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f  e total number o
12f17 66 20 62 79 74 65 73 20 74 68 61 74 20 61 20 43  f bytes that a C
12f18 65 6c 6c 20 6e 65 65 64 73 20 69 6e 20 74 68 65  ell needs in the
12f19 20 63 65 6c 6c 0a 2a 2a 20 64 61 74 61 20 61 72   cell.** data ar
12f1a 65 61 20 6f 66 20 74 68 65 20 62 74 72 65 65 2d  ea of the btree-
12f1b 70 61 67 65 2e 20 20 54 68 65 20 72 65 74 75 72  page.  The retur
12f1c 6e 20 6e 75 6d 62 65 72 20 69 6e 63 6c 75 64 65  n number include
12f1d 73 20 74 68 65 20 63 65 6c 6c 0a 2a 2a 20 64 61  s the cell.** da
12f1e 74 61 20 68 65 61 64 65 72 20 61 6e 64 20 74 68  ta header and th
12f1f 65 20 6c 6f 63 61 6c 20 70 61 79 6c 6f 61 64 2c  e local payload,
12f20 20 62 75 74 20 6e 6f 74 20 61 6e 79 20 6f 76 65   but not any ove
12f21 72 66 6c 6f 77 20 70 61 67 65 20 6f 72 0a 2a 2a  rflow page or.**
12f22 20 74 68 65 20 73 70 61 63 65 20 75 73 65 64 20   the space used 
12f23 62 79 20 74 68 65 20 63 65 6c 6c 20 70 6f 69 6e  by the cell poin
12f24 74 65 72 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ter..*/.#ifndef 
12f25 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 75 31  NDEBUG.static u1
12f26 36 20 63 65 6c 6c 53 69 7a 65 28 4d 65 6d 50 61  6 cellSize(MemPa
12f27 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 69  ge *pPage, int i
12f28 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e 66  Cell){.  CellInf
12f29 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69 74 65  o info;.  sqlite
12f2a 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 28  3BtreeParseCell(
12f2b 70 50 61 67 65 2c 20 69 43 65 6c 6c 2c 20 26 69  pPage, iCell, &i
12f2c 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 69  nfo);.  return i
12f2d 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a 23 65 6e  nfo.nSize;.}.#en
12f2e 64 69 66 0a 73 74 61 74 69 63 20 75 31 36 20 63  dif.static u16 c
12f2f 65 6c 6c 53 69 7a 65 50 74 72 28 4d 65 6d 50 61  ellSizePtr(MemPa
12f30 67 65 20 2a 70 50 61 67 65 2c 20 75 38 20 2a 70  ge *pPage, u8 *p
12f31 43 65 6c 6c 29 7b 0a 20 20 43 65 6c 6c 49 6e 66  Cell){.  CellInf
12f32 6f 20 69 6e 66 6f 3b 0a 20 20 73 71 6c 69 74 65  o info;.  sqlite
12f33 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
12f34 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
12f35 20 26 69 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72   &info);.  retur
12f36 6e 20 69 6e 66 6f 2e 6e 53 69 7a 65 3b 0a 7d 0a  n info.nSize;.}.
12f37 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
12f38 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
12f39 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c  /*.** If the cel
12f3a 6c 20 70 43 65 6c 6c 2c 20 70 61 72 74 20 6f 66  l pCell, part of
12f3b 20 70 61 67 65 20 70 50 61 67 65 20 63 6f 6e 74   page pPage cont
12f3c 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a  ains a pointer.*
12f3d 2a 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  * to an overflow
12f3e 20 70 61 67 65 2c 20 69 6e 73 65 72 74 20 61 6e   page, insert an
12f3f 20 65 6e 74 72 79 20 69 6e 74 6f 20 74 68 65 20   entry into the 
12f40 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 2a 2a 20 66  pointer-map.** f
12f41 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
12f42 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  page..*/.static 
12f43 69 6e 74 20 70 74 72 6d 61 70 50 75 74 4f 76 66  int ptrmapPutOvf
12f44 6c 50 74 72 28 4d 65 6d 50 61 67 65 20 2a 70 50  lPtr(MemPage *pP
12f45 61 67 65 2c 20 75 38 20 2a 70 43 65 6c 6c 29 7b  age, u8 *pCell){
12f46 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f  .  CellInfo info
12f47 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 65 6c  ;.  assert( pCel
12f48 6c 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  l!=0 );.  sqlite
12f49 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
12f4a 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
12f4b 20 26 69 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72   &info);.  asser
12f4c 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28  t( (info.nData+(
12f4d 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a  pPage->intKey?0:
12f4e 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66  info.nKey))==inf
12f4f 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20  o.nPayload );.  
12f50 69 66 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b  if( (info.nData+
12f51 28 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30  (pPage->intKey?0
12f52 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29 3e 69 6e 66  :info.nKey))>inf
12f53 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20  o.nLocal ){.    
12f54 50 67 6e 6f 20 6f 76 66 6c 20 3d 20 67 65 74 34  Pgno ovfl = get4
12f55 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e 66 6f  byte(&pCell[info
12f56 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a 20 20  .iOverflow]);.  
12f57 20 20 72 65 74 75 72 6e 20 70 74 72 6d 61 70 50    return ptrmapP
12f58 75 74 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 6f  ut(pPage->pBt, o
12f59 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52  vfl, PTRMAP_OVER
12f5a 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e 70 67  FLOW1, pPage->pg
12f5b 6e 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  no);.  }.  retur
12f5c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 2f  n SQLITE_OK;.}./
12f5d 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 65 6c 6c  *.** If the cell
12f5e 20 77 69 74 68 20 69 6e 64 65 78 20 69 43 65 6c   with index iCel
12f5f 6c 20 6f 6e 20 70 61 67 65 20 70 50 61 67 65 20  l on page pPage 
12f60 63 6f 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74  contains a point
12f61 65 72 0a 2a 2a 20 74 6f 20 61 6e 20 6f 76 65 72  er.** to an over
12f62 66 6c 6f 77 20 70 61 67 65 2c 20 69 6e 73 65 72  flow page, inser
12f63 74 20 61 6e 20 65 6e 74 72 79 20 69 6e 74 6f 20  t an entry into 
12f64 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a  the pointer-map.
12f65 2a 2a 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66  ** for the overf
12f66 6c 6f 77 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  low page..*/.sta
12f67 74 69 63 20 69 6e 74 20 70 74 72 6d 61 70 50 75  tic int ptrmapPu
12f68 74 4f 76 66 6c 28 4d 65 6d 50 61 67 65 20 2a 70  tOvfl(MemPage *p
12f69 50 61 67 65 2c 20 69 6e 74 20 69 43 65 6c 6c 29  Page, int iCell)
12f6a 7b 0a 20 20 75 38 20 2a 70 43 65 6c 6c 3b 0a 20  {.  u8 *pCell;. 
12f6b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
12f6c 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
12f6d 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
12f6e 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64  ;.  pCell = find
12f6f 4f 76 65 72 66 6c 6f 77 43 65 6c 6c 28 70 50 61  OverflowCell(pPa
12f70 67 65 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 72 65  ge, iCell);.  re
12f71 74 75 72 6e 20 70 74 72 6d 61 70 50 75 74 4f 76  turn ptrmapPutOv
12f72 66 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43 65  flPtr(pPage, pCe
12f73 6c 6c 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a  ll);.}.#endif...
12f74 2f 2a 0a 2a 2a 20 44 65 66 72 61 67 6d 65 6e 74  /*.** Defragment
12f75 20 74 68 65 20 70 61 67 65 20 67 69 76 65 6e 2e   the page given.
12f76 20 20 41 6c 6c 20 43 65 6c 6c 73 20 61 72 65 20    All Cells are 
12f77 6d 6f 76 65 64 20 74 6f 20 74 68 65 0a 2a 2a 20  moved to the.** 
12f78 65 6e 64 20 6f 66 20 74 68 65 20 70 61 67 65 20  end of the page 
12f79 61 6e 64 20 61 6c 6c 20 66 72 65 65 20 73 70 61  and all free spa
12f7a 63 65 20 69 73 20 63 6f 6c 6c 65 63 74 65 64 20  ce is collected 
12f7b 69 6e 74 6f 20 6f 6e 65 0a 2a 2a 20 62 69 67 20  into one.** big 
12f7c 46 72 65 65 42 6c 6b 20 74 68 61 74 20 6f 63 63  FreeBlk that occ
12f7d 75 72 73 20 69 6e 20 62 65 74 77 65 65 6e 20 74  urs in between t
12f7e 68 65 20 68 65 61 64 65 72 20 61 6e 64 20 63 65  he header and ce
12f7f 6c 6c 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 61 72  ll.** pointer ar
12f80 72 61 79 20 61 6e 64 20 74 68 65 20 63 65 6c 6c  ray and the cell
12f81 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 2a   content area..*
12f82 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 66  /.static int def
12f83 72 61 67 6d 65 6e 74 50 61 67 65 28 4d 65 6d 50  ragmentPage(MemP
12f84 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20 20 69  age *pPage){.  i
12f85 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20  nt i;           
12f86 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
12f87 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69  p counter */.  i
12f88 6e 74 20 70 63 3b 20 20 20 20 20 20 20 20 20 20  nt pc;          
12f89 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64            /* Add
12f8a 72 65 73 73 20 6f 66 20 61 20 69 2d 74 68 20 63  ress of a i-th c
12f8b 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ell */.  int add
12f8c 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
12f8d 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 6f 66      /* Offset of
12f8e 20 66 69 72 73 74 20 62 79 74 65 20 61 66 74 65   first byte afte
12f8f 72 20 63 65 6c 6c 20 70 6f 69 6e 74 65 72 20 61  r cell pointer a
12f90 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 68 64  rray */.  int hd
12f91 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
12f92 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
12f93 6f 20 74 68 65 20 70 61 67 65 20 68 65 61 64 65  o the page heade
12f94 72 20 2a 2f 0a 20 20 69 6e 74 20 73 69 7a 65 3b  r */.  int size;
12f95 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
12f96 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 61 20 63    /* Size of a c
12f97 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 75 73 61  ell */.  int usa
12f98 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 20 20 20  bleSize;        
12f99 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
12f9a 20 75 73 61 62 6c 65 20 62 79 74 65 73 20 6f 6e   usable bytes on
12f9b 20 61 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74   a page */.  int
12f9c 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20 20 20 20   cellOffset;    
12f9d 20 20 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65          /* Offse
12f9e 74 20 74 6f 20 74 68 65 20 63 65 6c 6c 20 70 6f  t to the cell po
12f9f 69 6e 74 65 72 20 61 72 72 61 79 20 2a 2f 0a 20  inter array */. 
12fa0 20 69 6e 74 20 63 62 72 6b 3b 20 20 20 20 20 20   int cbrk;      
12fa1 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
12fa2 66 66 73 65 74 20 74 6f 20 74 68 65 20 63 65 6c  ffset to the cel
12fa3 6c 20 63 6f 6e 74 65 6e 74 20 61 72 65 61 20 2a  l content area *
12fa4 2f 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20 20  /.  int nCell;  
12fa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
12fa6 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c  * Number of cell
12fa7 73 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f  s on the page */
12fa8 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  .  unsigned char
12fa9 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 2f 2a   *data;       /*
12faa 20 54 68 65 20 70 61 67 65 20 64 61 74 61 20 2a   The page data *
12fab 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
12fac 72 20 2a 74 65 6d 70 3b 20 20 20 20 20 20 20 2f  r *temp;       /
12fad 2a 20 54 65 6d 70 20 61 72 65 61 20 66 6f 72 20  * Temp area for 
12fae 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a  cell content */.
12faf 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
12fb0 65 33 50 61 67 65 72 49 73 77 72 69 74 65 61 62  e3PagerIswriteab
12fb1 6c 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67  le(pPage->pDbPag
12fb2 65 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  e) );.  assert( 
12fb3 70 50 61 67 65 2d 3e 70 42 74 21 3d 30 20 29 3b  pPage->pBt!=0 );
12fb4 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
12fb5 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  ->pBt->usableSiz
12fb6 65 20 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f  e <= SQLITE_MAX_
12fb7 50 41 47 45 5f 53 49 5a 45 20 29 3b 0a 20 20 61  PAGE_SIZE );.  a
12fb8 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
12fb9 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20 20  verflow==0 );.  
12fba 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
12fbb 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65  mutex_held(pPage
12fbc 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  ->pBt->mutex) );
12fbd 0a 20 20 74 65 6d 70 20 3d 20 73 71 6c 69 74 65  .  temp = sqlite
12fbe 33 50 61 67 65 72 54 65 6d 70 53 70 61 63 65 28  3PagerTempSpace(
12fbf 70 50 61 67 65 2d 3e 70 42 74 2d 3e 70 50 61 67  pPage->pBt->pPag
12fc0 65 72 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50  er);.  data = pP
12fc1 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64  age->aData;.  hd
12fc2 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
12fc3 66 73 65 74 3b 0a 20 20 63 65 6c 6c 4f 66 66 73  fset;.  cellOffs
12fc4 65 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c  et = pPage->cell
12fc5 4f 66 66 73 65 74 3b 0a 20 20 6e 43 65 6c 6c 20  Offset;.  nCell 
12fc6 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a  = pPage->nCell;.
12fc7 20 20 61 73 73 65 72 74 28 20 6e 43 65 6c 6c 3d    assert( nCell=
12fc8 3d 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b  =get2byte(&data[
12fc9 68 64 72 2b 33 5d 29 20 29 3b 0a 20 20 75 73 61  hdr+3]) );.  usa
12fca 62 6c 65 53 69 7a 65 20 3d 20 70 50 61 67 65 2d  bleSize = pPage-
12fcb 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
12fcc 3b 0a 20 20 63 62 72 6b 20 3d 20 67 65 74 32 62  ;.  cbrk = get2b
12fcd 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
12fce 29 3b 0a 20 20 6d 65 6d 63 70 79 28 26 74 65 6d  );.  memcpy(&tem
12fcf 70 5b 63 62 72 6b 5d 2c 20 26 64 61 74 61 5b 63  p[cbrk], &data[c
12fd0 62 72 6b 5d 2c 20 75 73 61 62 6c 65 53 69 7a 65  brk], usableSize
12fd1 20 2d 20 63 62 72 6b 29 3b 0a 20 20 63 62 72 6b   - cbrk);.  cbrk
12fd2 20 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20   = usableSize;. 
12fd3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 65 6c   for(i=0; i<nCel
12fd4 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 75 38 20  l; i++){.    u8 
12fd5 2a 70 41 64 64 72 3b 20 20 20 20 20 2f 2a 20 54  *pAddr;     /* T
12fd6 68 65 20 69 2d 74 68 20 63 65 6c 6c 20 70 6f 69  he i-th cell poi
12fd7 6e 74 65 72 20 2a 2f 0a 20 20 20 20 70 41 64 64  nter */.    pAdd
12fd8 72 20 3d 20 26 64 61 74 61 5b 63 65 6c 6c 4f 66  r = &data[cellOf
12fd9 66 73 65 74 20 2b 20 69 2a 32 5d 3b 0a 20 20 20  fset + i*2];.   
12fda 20 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 70   pc = get2byte(p
12fdb 41 64 64 72 29 3b 0a 20 20 20 20 69 66 28 20 70  Addr);.    if( p
12fdc 63 3e 3d 75 73 61 62 6c 65 53 69 7a 65 20 29 7b  c>=usableSize ){
12fdd 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
12fde 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
12fdf 54 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 69 7a  T;.    }.    siz
12fe0 65 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28  e = cellSizePtr(
12fe1 70 50 61 67 65 2c 20 26 74 65 6d 70 5b 70 63 5d  pPage, &temp[pc]
12fe2 29 3b 0a 20 20 20 20 63 62 72 6b 20 2d 3d 20 73  );.    cbrk -= s
12fe3 69 7a 65 3b 0a 20 20 20 20 69 66 28 20 63 62 72  ize;.    if( cbr
12fe4 6b 3c 63 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e  k<cellOffset+2*n
12fe5 43 65 6c 6c 20 7c 7c 20 70 63 2b 73 69 7a 65 3e  Cell || pc+size>
12fe6 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
12fe7 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
12fe8 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a  E_CORRUPT_BKPT;.
12fe9 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74      }.    assert
12fea 28 20 63 62 72 6b 2b 73 69 7a 65 3c 3d 75 73 61  ( cbrk+size<=usa
12feb 62 6c 65 53 69 7a 65 20 26 26 20 63 62 72 6b 3e  bleSize && cbrk>
12fec 3d 30 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79  =0 );.    memcpy
12fed 28 26 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26 74  (&data[cbrk], &t
12fee 65 6d 70 5b 70 63 5d 2c 20 73 69 7a 65 29 3b 0a  emp[pc], size);.
12fef 20 20 20 20 70 75 74 32 62 79 74 65 28 70 41 64      put2byte(pAd
12ff0 64 72 2c 20 63 62 72 6b 29 3b 0a 20 20 7d 0a 20  dr, cbrk);.  }. 
12ff1 20 61 73 73 65 72 74 28 20 63 62 72 6b 3e 3d 63   assert( cbrk>=c
12ff2 65 6c 6c 4f 66 66 73 65 74 2b 32 2a 6e 43 65 6c  ellOffset+2*nCel
12ff3 6c 20 29 3b 0a 20 20 70 75 74 32 62 79 74 65 28  l );.  put2byte(
12ff4 26 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 63 62  &data[hdr+5], cb
12ff5 72 6b 29 3b 0a 20 20 64 61 74 61 5b 68 64 72 2b  rk);.  data[hdr+
12ff6 31 5d 20 3d 20 30 3b 0a 20 20 64 61 74 61 5b 68  1] = 0;.  data[h
12ff7 64 72 2b 32 5d 20 3d 20 30 3b 0a 20 20 64 61 74  dr+2] = 0;.  dat
12ff8 61 5b 68 64 72 2b 37 5d 20 3d 20 30 3b 0a 20 20  a[hdr+7] = 0;.  
12ff9 61 64 64 72 20 3d 20 63 65 6c 6c 4f 66 66 73 65  addr = cellOffse
12ffa 74 2b 32 2a 6e 43 65 6c 6c 3b 0a 20 20 6d 65 6d  t+2*nCell;.  mem
12ffb 73 65 74 28 26 64 61 74 61 5b 61 64 64 72 5d 2c  set(&data[addr],
12ffc 20 30 2c 20 63 62 72 6b 2d 61 64 64 72 29 3b 0a   0, cbrk-addr);.
12ffd 20 20 69 66 28 20 63 62 72 6b 2d 61 64 64 72 21    if( cbrk-addr!
12ffe 3d 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 29 7b  =pPage->nFree ){
12fff 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
13000 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
13001 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
13002 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
13003 2a 20 41 6c 6c 6f 63 61 74 65 20 6e 42 79 74 65  * Allocate nByte
13004 20 62 79 74 65 73 20 6f 66 20 73 70 61 63 65 20   bytes of space 
13005 6f 6e 20 61 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a  on a page..**.**
13006 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64 65   Return the inde
13007 78 20 69 6e 74 6f 20 70 50 61 67 65 2d 3e 61 44  x into pPage->aD
13008 61 74 61 5b 5d 20 6f 66 20 74 68 65 20 66 69 72  ata[] of the fir
13009 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a 20 74 68  st byte of.** th
1300a 65 20 6e 65 77 20 61 6c 6c 6f 63 61 74 69 6f 6e  e new allocation
1300b 2e 20 20 54 68 65 20 63 61 6c 6c 65 72 20 67 75  .  The caller gu
1300c 61 72 61 6e 74 65 65 73 20 74 68 61 74 20 74 68  arantees that th
1300d 65 72 65 20 69 73 20 65 6e 6f 75 67 68 0a 2a 2a  ere is enough.**
1300e 20 73 70 61 63 65 2e 20 20 54 68 69 73 20 72 6f   space.  This ro
1300f 75 74 69 6e 65 20 77 69 6c 6c 20 6e 65 76 65 72  utine will never
13010 20 66 61 69 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   fail..**.** If 
13011 74 68 65 20 70 61 67 65 20 63 6f 6e 74 61 69 6e  the page contain
13012 73 20 6e 42 79 74 65 73 20 6f 66 20 66 72 65 65  s nBytes of free
13013 20 73 70 61 63 65 20 62 75 74 20 64 6f 65 73 20   space but does 
13014 6e 6f 74 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e  not contain.** n
13015 42 79 74 65 73 20 6f 66 20 63 6f 6e 74 69 67 75  Bytes of contigu
13016 6f 75 73 20 66 72 65 65 20 73 70 61 63 65 2c 20  ous free space, 
13017 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
13018 65 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a  e automatically.
13019 2a 2a 20 63 61 6c 6c 73 20 64 65 66 72 61 67 65  ** calls defrage
1301a 6d 65 6e 74 50 61 67 65 28 29 20 74 6f 20 63 6f  mentPage() to co
1301b 6e 73 6f 6c 69 64 61 74 65 20 61 6c 6c 20 66 72  nsolidate all fr
1301c 65 65 20 73 70 61 63 65 20 62 65 66 6f 72 65 20  ee space before 
1301d 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69 6e 67 20 74  .** allocating t
1301e 68 65 20 6e 65 77 20 63 68 75 6e 6b 2e 0a 2a 2f  he new chunk..*/
1301f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c 6c 6f  .static int allo
13020 63 61 74 65 53 70 61 63 65 28 4d 65 6d 50 61 67  cateSpace(MemPag
13021 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 6e 42  e *pPage, int nB
13022 79 74 65 29 7b 0a 20 20 69 6e 74 20 61 64 64 72  yte){.  int addr
13023 2c 20 70 63 2c 20 68 64 72 3b 0a 20 20 69 6e 74  , pc, hdr;.  int
13024 20 73 69 7a 65 3b 0a 20 20 69 6e 74 20 6e 46 72   size;.  int nFr
13025 61 67 3b 0a 20 20 69 6e 74 20 74 6f 70 3b 0a 20  ag;.  int top;. 
13026 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20 20 69 6e   int nCell;.  in
13027 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 0a 20 20  t cellOffset;.  
13028 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 64  unsigned char *d
13029 61 74 61 3b 0a 20 20 0a 20 20 64 61 74 61 20 3d  ata;.  .  data =
1302a 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20   pPage->aData;. 
1302b 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1302c 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
1302d 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1302e 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1302f 61 67 65 2d 3e 70 42 74 20 29 3b 0a 20 20 61 73  age->pBt );.  as
13030 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
13031 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
13032 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
13033 20 61 73 73 65 72 74 28 20 6e 42 79 74 65 3e 3d   assert( nByte>=
13034 30 20 29 3b 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d  0 );  /* Minimum
13035 20 63 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20   cell size is 4 
13036 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  */.  assert( pPa
13037 67 65 2d 3e 6e 46 72 65 65 3e 3d 6e 42 79 74 65  ge->nFree>=nByte
13038 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
13039 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d  age->nOverflow==
1303a 30 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 46  0 );.  pPage->nF
1303b 72 65 65 20 2d 3d 20 6e 42 79 74 65 3b 0a 20 20  ree -= nByte;.  
1303c 68 64 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72  hdr = pPage->hdr
1303d 4f 66 66 73 65 74 3b 0a 0a 20 20 6e 46 72 61 67  Offset;..  nFrag
1303e 20 3d 20 64 61 74 61 5b 68 64 72 2b 37 5d 3b 0a   = data[hdr+7];.
1303f 20 20 69 66 28 20 6e 46 72 61 67 3c 36 30 20 29    if( nFrag<60 )
13040 7b 0a 20 20 20 20 2f 2a 20 53 65 61 72 63 68 20  {.    /* Search 
13041 74 68 65 20 66 72 65 65 6c 69 73 74 20 6c 6f 6f  the freelist loo
13042 6b 69 6e 67 20 66 6f 72 20 61 20 73 6c 6f 74 20  king for a slot 
13043 62 69 67 20 65 6e 6f 75 67 68 20 74 6f 20 73 61  big enough to sa
13044 74 69 73 66 79 20 74 68 65 0a 20 20 20 20 2a 2a  tisfy the.    **
13045 20 73 70 61 63 65 20 72 65 71 75 65 73 74 2e 20   space request. 
13046 2a 2f 0a 20 20 20 20 61 64 64 72 20 3d 20 68 64  */.    addr = hd
13047 72 2b 31 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  r+1;.    while( 
13048 28 70 63 20 3d 20 67 65 74 32 62 79 74 65 28 26  (pc = get2byte(&
13049 64 61 74 61 5b 61 64 64 72 5d 29 29 3e 30 20 29  data[addr]))>0 )
1304a 7b 0a 20 20 20 20 20 20 73 69 7a 65 20 3d 20 67  {.      size = g
1304b 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70 63  et2byte(&data[pc
1304c 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +2]);.      if( 
1304d 73 69 7a 65 3e 3d 6e 42 79 74 65 20 29 7b 0a 20  size>=nByte ){. 
1304e 20 20 20 20 20 20 20 69 66 28 20 73 69 7a 65 3c         if( size<
1304f 6e 42 79 74 65 2b 34 20 29 7b 0a 20 20 20 20 20  nByte+4 ){.     
13050 20 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74       memcpy(&dat
13051 61 5b 61 64 64 72 5d 2c 20 26 64 61 74 61 5b 70  a[addr], &data[p
13052 63 5d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20  c], 2);.        
13053 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d 20    data[hdr+7] = 
13054 6e 46 72 61 67 20 2b 20 73 69 7a 65 20 2d 20 6e  nFrag + size - n
13055 42 79 74 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Byte;.          
13056 72 65 74 75 72 6e 20 70 63 3b 0a 20 20 20 20 20  return pc;.     
13057 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
13058 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
13059 74 61 5b 70 63 2b 32 5d 2c 20 73 69 7a 65 2d 6e  ta[pc+2], size-n
1305a 42 79 74 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Byte);.         
1305b 20 72 65 74 75 72 6e 20 70 63 20 2b 20 73 69 7a   return pc + siz
1305c 65 20 2d 20 6e 42 79 74 65 3b 0a 20 20 20 20 20  e - nByte;.     
1305d 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1305e 20 20 20 61 64 64 72 20 3d 20 70 63 3b 0a 20 20     addr = pc;.  
1305f 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c    }.  }..  /* Al
13060 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20 66 72  locate memory fr
13061 6f 6d 20 74 68 65 20 67 61 70 20 69 6e 20 62 65  om the gap in be
13062 74 77 65 65 6e 20 74 68 65 20 63 65 6c 6c 20 70  tween the cell p
13063 6f 69 6e 74 65 72 20 61 72 72 61 79 0a 20 20 2a  ointer array.  *
13064 2a 20 61 6e 64 20 74 68 65 20 63 65 6c 6c 20 63  * and the cell c
13065 6f 6e 74 65 6e 74 20 61 72 65 61 2e 0a 20 20 2a  ontent area..  *
13066 2f 0a 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79  /.  top = get2by
13067 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
13068 3b 0a 20 20 6e 43 65 6c 6c 20 3d 20 67 65 74 32  ;.  nCell = get2
13069 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 33  byte(&data[hdr+3
1306a 5d 29 3b 0a 20 20 63 65 6c 6c 4f 66 66 73 65 74  ]);.  cellOffset
1306b 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66   = pPage->cellOf
1306c 66 73 65 74 3b 0a 20 20 69 66 28 20 6e 46 72 61  fset;.  if( nFra
1306d 67 3e 3d 36 30 20 7c 7c 20 63 65 6c 6c 4f 66 66  g>=60 || cellOff
1306e 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 20 3e 20  set + 2*nCell > 
1306f 74 6f 70 20 2d 20 6e 42 79 74 65 20 29 7b 0a 20  top - nByte ){. 
13070 20 20 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67     defragmentPag
13071 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 74 6f  e(pPage);.    to
13072 70 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  p = get2byte(&da
13073 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 7d 0a  ta[hdr+5]);.  }.
13074 20 20 74 6f 70 20 2d 3d 20 6e 42 79 74 65 3b 0a    top -= nByte;.
13075 20 20 61 73 73 65 72 74 28 20 63 65 6c 6c 4f 66    assert( cellOf
13076 66 73 65 74 20 2b 20 32 2a 6e 43 65 6c 6c 20 3c  fset + 2*nCell <
13077 3d 20 74 6f 70 20 29 3b 0a 20 20 70 75 74 32 62  = top );.  put2b
13078 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
13079 2c 20 74 6f 70 29 3b 0a 20 20 72 65 74 75 72 6e  , top);.  return
1307a 20 74 6f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   top;.}../*.** R
1307b 65 74 75 72 6e 20 61 20 73 65 63 74 69 6f 6e 20  eturn a section 
1307c 6f 66 20 74 68 65 20 70 50 61 67 65 2d 3e 61 44  of the pPage->aD
1307d 61 74 61 20 74 6f 20 74 68 65 20 66 72 65 65 6c  ata to the freel
1307e 69 73 74 2e 0a 2a 2a 20 54 68 65 20 66 69 72 73  ist..** The firs
1307f 74 20 62 79 74 65 20 6f 66 20 74 68 65 20 6e 65  t byte of the ne
13080 77 20 66 72 65 65 20 62 6c 6f 63 6b 20 69 73 20  w free block is 
13081 70 50 61 67 65 2d 3e 61 44 69 73 6b 5b 73 74 61  pPage->aDisk[sta
13082 72 74 5d 0a 2a 2a 20 61 6e 64 20 74 68 65 20 73  rt].** and the s
13083 69 7a 65 20 6f 66 20 74 68 65 20 62 6c 6f 63 6b  ize of the block
13084 20 69 73 20 22 73 69 7a 65 22 20 62 79 74 65 73   is "size" bytes
13085 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20  ..**.** Most of 
13086 74 68 65 20 65 66 66 6f 72 74 20 68 65 72 65 20  the effort here 
13087 69 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e 20 63  is involved in c
13088 6f 61 6c 65 73 69 6e 67 20 61 64 6a 61 63 65 6e  oalesing adjacen
13089 74 0a 2a 2a 20 66 72 65 65 20 62 6c 6f 63 6b 73  t.** free blocks
1308a 20 69 6e 74 6f 20 61 20 73 69 6e 67 6c 65 20 62   into a single b
1308b 69 67 20 66 72 65 65 20 62 6c 6f 63 6b 2e 0a 2a  ig free block..*
1308c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65  /.static int fre
1308d 65 53 70 61 63 65 28 4d 65 6d 50 61 67 65 20 2a  eSpace(MemPage *
1308e 70 50 61 67 65 2c 20 69 6e 74 20 73 74 61 72 74  pPage, int start
1308f 2c 20 69 6e 74 20 73 69 7a 65 29 7b 0a 20 20 69  , int size){.  i
13090 6e 74 20 61 64 64 72 2c 20 70 62 65 67 69 6e 2c  nt addr, pbegin,
13091 20 68 64 72 3b 0a 20 20 75 6e 73 69 67 6e 65 64   hdr;.  unsigned
13092 20 63 68 61 72 20 2a 64 61 74 61 20 3d 20 70 50   char *data = pP
13093 61 67 65 2d 3e 61 44 61 74 61 3b 0a 0a 20 20 61  age->aData;..  a
13094 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42  ssert( pPage->pB
13095 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t!=0 );.  assert
13096 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73  ( sqlite3PagerIs
13097 77 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d  writeable(pPage-
13098 3e 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61  >pDbPage) );.  a
13099 73 73 65 72 74 28 20 73 74 61 72 74 3e 3d 70 50  ssert( start>=pP
1309a 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36  age->hdrOffset+6
1309b 2b 28 70 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a  +(pPage->leaf?0:
1309c 34 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  4) );.  assert( 
1309d 28 73 74 61 72 74 20 2b 20 73 69 7a 65 29 3c 3d  (start + size)<=
1309e 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62  pPage->pBt->usab
1309f 6c 65 53 69 7a 65 20 29 3b 0a 20 20 61 73 73 65  leSize );.  asse
130a0 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  rt( sqlite3_mute
130a1 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42  x_held(pPage->pB
130a2 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 61  t->mutex) );.  a
130a3 73 73 65 72 74 28 20 73 69 7a 65 3e 3d 30 20 29  ssert( size>=0 )
130a4 3b 20 20 20 2f 2a 20 4d 69 6e 69 6d 75 6d 20 63  ;   /* Minimum c
130a5 65 6c 6c 20 73 69 7a 65 20 69 73 20 34 20 2a 2f  ell size is 4 */
130a6 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
130a7 53 45 43 55 52 45 5f 44 45 4c 45 54 45 0a 20 20  SECURE_DELETE.  
130a8 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 64 65 6c  /* Overwrite del
130a9 65 74 65 64 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  eted information
130aa 20 77 69 74 68 20 7a 65 72 6f 73 20 77 68 65 6e   with zeros when
130ab 20 74 68 65 20 53 45 43 55 52 45 5f 44 45 4c 45   the SECURE_DELE
130ac 54 45 20 0a 20 20 2a 2a 20 6f 70 74 69 6f 6e 20  TE .  ** option 
130ad 69 73 20 65 6e 61 62 6c 65 64 20 61 74 20 63 6f  is enabled at co
130ae 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f 0a 20 20  mpile-time */.  
130af 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 73 74 61  memset(&data[sta
130b0 72 74 5d 2c 20 30 2c 20 73 69 7a 65 29 3b 0a 23  rt], 0, size);.#
130b1 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 41 64 64 20  endif..  /* Add 
130b2 74 68 65 20 73 70 61 63 65 20 62 61 63 6b 20 69  the space back i
130b3 6e 74 6f 20 74 68 65 20 6c 69 6e 6b 65 64 20 6c  nto the linked l
130b4 69 73 74 20 6f 66 20 66 72 65 65 62 6c 6f 63 6b  ist of freeblock
130b5 73 20 2a 2f 0a 20 20 68 64 72 20 3d 20 70 50 61  s */.  hdr = pPa
130b6 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
130b7 20 61 64 64 72 20 3d 20 68 64 72 20 2b 20 31 3b   addr = hdr + 1;
130b8 0a 20 20 77 68 69 6c 65 28 20 28 70 62 65 67 69  .  while( (pbegi
130b9 6e 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  n = get2byte(&da
130ba 74 61 5b 61 64 64 72 5d 29 29 3c 73 74 61 72 74  ta[addr]))<start
130bb 20 26 26 20 70 62 65 67 69 6e 3e 30 20 29 7b 0a   && pbegin>0 ){.
130bc 20 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67      assert( pbeg
130bd 69 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e  in<=pPage->pBt->
130be 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a  usableSize-4 );.
130bf 20 20 20 20 69 66 28 20 70 62 65 67 69 6e 3c 3d      if( pbegin<=
130c0 61 64 64 72 20 29 20 7b 0a 20 20 20 20 20 20 72  addr ) {.      r
130c1 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
130c2 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d  RUPT_BKPT;.    }
130c3 0a 20 20 20 20 61 64 64 72 20 3d 20 70 62 65 67  .    addr = pbeg
130c4 69 6e 3b 0a 20 20 7d 0a 20 20 69 66 20 28 20 70  in;.  }.  if ( p
130c5 62 65 67 69 6e 3e 70 50 61 67 65 2d 3e 70 42 74  begin>pPage->pBt
130c6 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 34 20 29  ->usableSize-4 )
130c7 20 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51   {.    return SQ
130c8 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
130c9 54 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  T;.  }.  assert(
130ca 20 70 62 65 67 69 6e 3e 61 64 64 72 20 7c 7c 20   pbegin>addr || 
130cb 70 62 65 67 69 6e 3d 3d 30 20 29 3b 0a 20 20 70  pbegin==0 );.  p
130cc 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 61 64  ut2byte(&data[ad
130cd 64 72 5d 2c 20 73 74 61 72 74 29 3b 0a 20 20 70  dr], start);.  p
130ce 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 73 74  ut2byte(&data[st
130cf 61 72 74 5d 2c 20 70 62 65 67 69 6e 29 3b 0a 20  art], pbegin);. 
130d0 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b   put2byte(&data[
130d1 73 74 61 72 74 2b 32 5d 2c 20 73 69 7a 65 29 3b  start+2], size);
130d2 0a 20 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  .  pPage->nFree 
130d3 2b 3d 20 73 69 7a 65 3b 0a 0a 20 20 2f 2a 20 43  += size;..  /* C
130d4 6f 61 6c 65 73 63 65 20 61 64 6a 61 63 65 6e 74  oalesce adjacent
130d5 20 66 72 65 65 20 62 6c 6f 63 6b 73 20 2a 2f 0a   free blocks */.
130d6 20 20 61 64 64 72 20 3d 20 70 50 61 67 65 2d 3e    addr = pPage->
130d7 68 64 72 4f 66 66 73 65 74 20 2b 20 31 3b 0a 20  hdrOffset + 1;. 
130d8 20 77 68 69 6c 65 28 20 28 70 62 65 67 69 6e 20   while( (pbegin 
130d9 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
130da 5b 61 64 64 72 5d 29 29 3e 30 20 29 7b 0a 20 20  [addr]))>0 ){.  
130db 20 20 69 6e 74 20 70 6e 65 78 74 2c 20 70 73 69    int pnext, psi
130dc 7a 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ze;.    assert( 
130dd 70 62 65 67 69 6e 3e 61 64 64 72 20 29 3b 0a 20  pbegin>addr );. 
130de 20 20 20 61 73 73 65 72 74 28 20 70 62 65 67 69     assert( pbegi
130df 6e 3c 3d 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75  n<=pPage->pBt->u
130e0 73 61 62 6c 65 53 69 7a 65 2d 34 20 29 3b 0a 20  sableSize-4 );. 
130e1 20 20 20 70 6e 65 78 74 20 3d 20 67 65 74 32 62     pnext = get2b
130e2 79 74 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e  yte(&data[pbegin
130e3 5d 29 3b 0a 20 20 20 20 70 73 69 7a 65 20 3d 20  ]);.    psize = 
130e4 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
130e5 62 65 67 69 6e 2b 32 5d 29 3b 0a 20 20 20 20 69  begin+2]);.    i
130e6 66 28 20 70 62 65 67 69 6e 20 2b 20 70 73 69 7a  f( pbegin + psiz
130e7 65 20 2b 20 33 20 3e 3d 20 70 6e 65 78 74 20 26  e + 3 >= pnext &
130e8 26 20 70 6e 65 78 74 3e 30 20 29 7b 0a 20 20 20  & pnext>0 ){.   
130e9 20 20 20 69 6e 74 20 66 72 61 67 20 3d 20 70 6e     int frag = pn
130ea 65 78 74 20 2d 20 28 70 62 65 67 69 6e 2b 70 73  ext - (pbegin+ps
130eb 69 7a 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ize);.      if( 
130ec 28 66 72 61 67 3c 30 29 20 7c 7c 20 28 66 72 61  (frag<0) || (fra
130ed 67 3e 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  g>data[pPage->hd
130ee 72 4f 66 66 73 65 74 2b 37 5d 29 20 29 7b 0a 20  rOffset+7]) ){. 
130ef 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
130f0 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
130f1 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  T;.      }.     
130f2 20 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72   data[pPage->hdr
130f3 4f 66 66 73 65 74 2b 37 5d 20 2d 3d 20 66 72 61  Offset+7] -= fra
130f4 67 3b 0a 20 20 20 20 20 20 70 75 74 32 62 79 74  g;.      put2byt
130f5 65 28 26 64 61 74 61 5b 70 62 65 67 69 6e 5d 2c  e(&data[pbegin],
130f6 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
130f7 70 6e 65 78 74 5d 29 29 3b 0a 20 20 20 20 20 20  pnext]));.      
130f8 70 75 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  put2byte(&data[p
130f9 62 65 67 69 6e 2b 32 5d 2c 20 70 6e 65 78 74 2b  begin+2], pnext+
130fa 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b 70  get2byte(&data[p
130fb 6e 65 78 74 2b 32 5d 29 2d 70 62 65 67 69 6e 29  next+2])-pbegin)
130fc 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
130fd 20 20 20 61 64 64 72 20 3d 20 70 62 65 67 69 6e     addr = pbegin
130fe 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
130ff 2a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 63 6f  * If the cell co
13100 6e 74 65 6e 74 20 61 72 65 61 20 62 65 67 69 6e  ntent area begin
13101 73 20 77 69 74 68 20 61 20 66 72 65 65 62 6c 6f  s with a freeblo
13102 63 6b 2c 20 72 65 6d 6f 76 65 20 69 74 2e 20 2a  ck, remove it. *
13103 2f 0a 20 20 69 66 28 20 64 61 74 61 5b 68 64 72  /.  if( data[hdr
13104 2b 31 5d 3d 3d 64 61 74 61 5b 68 64 72 2b 35 5d  +1]==data[hdr+5]
13105 20 26 26 20 64 61 74 61 5b 68 64 72 2b 32 5d 3d   && data[hdr+2]=
13106 3d 64 61 74 61 5b 68 64 72 2b 36 5d 20 29 7b 0a  =data[hdr+6] ){.
13107 20 20 20 20 69 6e 74 20 74 6f 70 3b 0a 20 20 20      int top;.   
13108 20 70 62 65 67 69 6e 20 3d 20 67 65 74 32 62 79   pbegin = get2by
13109 74 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29  te(&data[hdr+1])
1310a 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 64 61  ;.    memcpy(&da
1310b 74 61 5b 68 64 72 2b 31 5d 2c 20 26 64 61 74 61  ta[hdr+1], &data
1310c 5b 70 62 65 67 69 6e 5d 2c 20 32 29 3b 0a 20 20  [pbegin], 2);.  
1310d 20 20 74 6f 70 20 3d 20 67 65 74 32 62 79 74 65    top = get2byte
1310e 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 3b 0a  (&data[hdr+5]);.
1310f 20 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61      put2byte(&da
13110 74 61 5b 68 64 72 2b 35 5d 2c 20 74 6f 70 20 2b  ta[hdr+5], top +
13111 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
13112 70 62 65 67 69 6e 2b 32 5d 29 29 3b 0a 20 20 7d  pbegin+2]));.  }
13113 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
13114 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65  _OK;.}../*.** De
13115 63 6f 64 65 20 74 68 65 20 66 6c 61 67 73 20 62  code the flags b
13116 79 74 65 20 28 74 68 65 20 66 69 72 73 74 20 62  yte (the first b
13117 79 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65  yte of the heade
13118 72 29 20 66 6f 72 20 61 20 70 61 67 65 0a 2a 2a  r) for a page.**
13119 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65 20   and initialize 
1311a 66 69 65 6c 64 73 20 6f 66 20 74 68 65 20 4d 65  fields of the Me
1311b 6d 50 61 67 65 20 73 74 72 75 63 74 75 72 65 20  mPage structure 
1311c 61 63 63 6f 72 64 69 6e 67 6c 79 2e 0a 2a 2a 0a  accordingly..**.
1311d 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 66 6f 6c 6c  ** Only the foll
1311e 6f 77 69 6e 67 20 63 6f 6d 62 69 6e 61 74 69 6f  owing combinatio
1311f 6e 73 20 61 72 65 20 73 75 70 70 6f 72 74 65 64  ns are supported
13120 2e 20 20 41 6e 79 74 68 69 6e 67 20 64 69 66 66  .  Anything diff
13121 65 72 65 6e 74 0a 2a 2a 20 69 6e 64 69 63 61 74  erent.** indicat
13122 65 73 20 61 20 63 6f 72 72 75 70 74 20 64 61 74  es a corrupt dat
13123 61 62 61 73 65 20 66 69 6c 65 73 3a 0a 2a 2a 0a  abase files:.**.
13124 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 5a  **         PTF_Z
13125 45 52 4f 44 41 54 41 0a 2a 2a 20 20 20 20 20 20  ERODATA.**      
13126 20 20 20 50 54 46 5f 5a 45 52 4f 44 41 54 41 20     PTF_ZERODATA 
13127 7c 20 50 54 46 5f 4c 45 41 46 0a 2a 2a 20 20 20  | PTF_LEAF.**   
13128 20 20 20 20 20 20 50 54 46 5f 4c 45 41 46 44 41        PTF_LEAFDA
13129 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45 59 0a  TA | PTF_INTKEY.
1312a 2a 2a 20 20 20 20 20 20 20 20 20 50 54 46 5f 4c  **         PTF_L
1312b 45 41 46 44 41 54 41 20 7c 20 50 54 46 5f 49 4e  EAFDATA | PTF_IN
1312c 54 4b 45 59 20 7c 20 50 54 46 5f 4c 45 41 46 0a  TKEY | PTF_LEAF.
1312d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
1312e 63 6f 64 65 46 6c 61 67 73 28 4d 65 6d 50 61 67  codeFlags(MemPag
1312f 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20 66 6c  e *pPage, int fl
13130 61 67 42 79 74 65 29 7b 0a 20 20 42 74 53 68 61  agByte){.  BtSha
13131 72 65 64 20 2a 70 42 74 3b 20 20 20 20 20 2f 2a  red *pBt;     /*
13132 20 41 20 63 6f 70 79 20 6f 66 20 70 50 61 67 65   A copy of pPage
13133 2d 3e 70 42 74 20 2a 2f 0a 0a 20 20 61 73 73 65  ->pBt */..  asse
13134 72 74 28 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  rt( pPage->hdrOf
13135 66 73 65 74 3d 3d 28 70 50 61 67 65 2d 3e 70 67  fset==(pPage->pg
13136 6e 6f 3d 3d 31 20 3f 20 31 30 30 20 3a 20 30 29  no==1 ? 100 : 0)
13137 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
13138 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
13139 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74  (pPage->pBt->mut
1313a 65 78 29 20 29 3b 0a 20 20 70 50 61 67 65 2d 3e  ex) );.  pPage->
1313b 6c 65 61 66 20 3d 20 66 6c 61 67 42 79 74 65 3e  leaf = flagByte>
1313c 3e 33 3b 20 20 61 73 73 65 72 74 28 20 50 54 46  >3;  assert( PTF
1313d 5f 4c 45 41 46 20 3d 3d 20 31 3c 3c 33 20 29 3b  _LEAF == 1<<3 );
1313e 0a 20 20 66 6c 61 67 42 79 74 65 20 26 3d 20 7e  .  flagByte &= ~
1313f 50 54 46 5f 4c 45 41 46 3b 0a 20 20 70 50 61 67  PTF_LEAF;.  pPag
13140 65 2d 3e 63 68 69 6c 64 50 74 72 53 69 7a 65 20  e->childPtrSize 
13141 3d 20 34 2d 34 2a 70 50 61 67 65 2d 3e 6c 65 61  = 4-4*pPage->lea
13142 66 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65  f;.  pBt = pPage
13143 2d 3e 70 42 74 3b 0a 20 20 69 66 28 20 66 6c 61  ->pBt;.  if( fla
13144 67 42 79 74 65 3d 3d 28 50 54 46 5f 4c 45 41 46  gByte==(PTF_LEAF
13145 44 41 54 41 20 7c 20 50 54 46 5f 49 4e 54 4b 45  DATA | PTF_INTKE
13146 59 29 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d  Y) ){.    pPage-
13147 3e 69 6e 74 4b 65 79 20 3d 20 31 3b 0a 20 20 20  >intKey = 1;.   
13148 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20   pPage->hasData 
13149 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66 3b 0a 20  = pPage->leaf;. 
1314a 20 20 20 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63     pPage->maxLoc
1314b 61 6c 20 3d 20 70 42 74 2d 3e 6d 61 78 4c 65 61  al = pBt->maxLea
1314c 66 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69  f;.    pPage->mi
1314d 6e 4c 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69  nLocal = pBt->mi
1314e 6e 4c 65 61 66 3b 0a 20 20 7d 65 6c 73 65 20 69  nLeaf;.  }else i
1314f 66 28 20 66 6c 61 67 42 79 74 65 3d 3d 50 54 46  f( flagByte==PTF
13150 5f 5a 45 52 4f 44 41 54 41 20 29 7b 0a 20 20 20  _ZERODATA ){.   
13151 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 3d   pPage->intKey =
13152 20 30 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e 68   0;.    pPage->h
13153 61 73 44 61 74 61 20 3d 20 30 3b 0a 20 20 20 20  asData = 0;.    
13154 70 50 61 67 65 2d 3e 6d 61 78 4c 6f 63 61 6c 20  pPage->maxLocal 
13155 3d 20 70 42 74 2d 3e 6d 61 78 4c 6f 63 61 6c 3b  = pBt->maxLocal;
13156 0a 20 20 20 20 70 50 61 67 65 2d 3e 6d 69 6e 4c  .    pPage->minL
13157 6f 63 61 6c 20 3d 20 70 42 74 2d 3e 6d 69 6e 4c  ocal = pBt->minL
13158 6f 63 61 6c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ocal;.  }else{. 
13159 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1315a 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
1315b 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49   }.  return SQLI
1315c 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
1315d 49 6e 69 74 69 61 6c 69 7a 65 20 74 68 65 20 61  Initialize the a
1315e 75 78 69 6c 69 61 72 79 20 69 6e 66 6f 72 6d 61  uxiliary informa
1315f 74 69 6f 6e 20 66 6f 72 20 61 20 64 69 73 6b 20  tion for a disk 
13160 62 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 52 65 74  block..**.** Ret
13161 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  urn SQLITE_OK on
13162 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 77 65   success.  If we
13163 20 73 65 65 20 74 68 61 74 20 74 68 65 20 70 61   see that the pa
13164 67 65 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  ge does.** not c
13165 6f 6e 74 61 69 6e 20 61 20 77 65 6c 6c 2d 66 6f  ontain a well-fo
13166 72 6d 65 64 20 64 61 74 61 62 61 73 65 20 70 61  rmed database pa
13167 67 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ge, then return 
13168 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  .** SQLITE_CORRU
13169 50 54 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 61  PT.  Note that a
1316a 20 72 65 74 75 72 6e 20 6f 66 20 53 51 4c 49 54   return of SQLIT
1316b 45 5f 4f 4b 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a  E_OK does not.**
1316c 20 67 75 61 72 61 6e 74 65 65 20 74 68 61 74 20   guarantee that 
1316d 74 68 65 20 70 61 67 65 20 69 73 20 77 65 6c 6c  the page is well
1316e 2d 66 6f 72 6d 65 64 2e 20 20 49 74 20 6f 6e 6c  -formed.  It onl
1316f 79 20 73 68 6f 77 73 20 74 68 61 74 0a 2a 2a 20  y shows that.** 
13170 77 65 20 66 61 69 6c 65 64 20 74 6f 20 64 65 74  we failed to det
13171 65 63 74 20 61 6e 79 20 63 6f 72 72 75 70 74 69  ect any corrupti
13172 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  on..*/.SQLITE_PR
13173 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13174 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 4d  3BtreeInitPage(M
13175 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a  emPage *pPage){.
13176 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65  .  assert( pPage
13177 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 61 73  ->pBt!=0 );.  as
13178 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
13179 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
1317a 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
1317b 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1317c 70 67 6e 6f 3d 3d 73 71 6c 69 74 65 33 50 61 67  pgno==sqlite3Pag
1317d 65 72 50 61 67 65 6e 75 6d 62 65 72 28 70 50 61  erPagenumber(pPa
1317e 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b 0a  ge->pDbPage) );.
1317f 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 20    assert( pPage 
13180 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47  == sqlite3PagerG
13181 65 74 45 78 74 72 61 28 70 50 61 67 65 2d 3e 70  etExtra(pPage->p
13182 44 62 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73  DbPage) );.  ass
13183 65 72 74 28 20 70 50 61 67 65 2d 3e 61 44 61 74  ert( pPage->aDat
13184 61 20 3d 3d 20 73 71 6c 69 74 65 33 50 61 67 65  a == sqlite3Page
13185 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e  rGetData(pPage->
13186 70 44 62 50 61 67 65 29 20 29 3b 0a 0a 20 20 69  pDbPage) );..  i
13187 66 28 20 21 70 50 61 67 65 2d 3e 69 73 49 6e 69  f( !pPage->isIni
13188 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 63 3b  t ){.    int pc;
13189 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
1318a 64 64 72 65 73 73 20 6f 66 20 61 20 66 72 65 65  ddress of a free
1318b 62 6c 6f 63 6b 20 77 69 74 68 69 6e 20 70 50 61  block within pPa
1318c 67 65 2d 3e 61 44 61 74 61 5b 5d 20 2a 2f 0a 20  ge->aData[] */. 
1318d 20 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20 20     int hdr;     
1318e 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
1318f 74 6f 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20  to beginning of 
13190 70 61 67 65 20 68 65 61 64 65 72 20 2a 2f 0a 20  page header */. 
13191 20 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20     u8 *data;    
13192 20 20 20 20 20 20 2f 2a 20 45 71 75 61 6c 20 74        /* Equal t
13193 6f 20 70 50 61 67 65 2d 3e 61 44 61 74 61 20 2a  o pPage->aData *
13194 2f 0a 20 20 20 20 42 74 53 68 61 72 65 64 20 2a  /.    BtShared *
13195 70 42 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 54  pBt;        /* T
13196 68 65 20 6d 61 69 6e 20 62 74 72 65 65 20 73 74  he main btree st
13197 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 20 20 69  ructure */.    i
13198 6e 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 20 20  nt usableSize;  
13199 20 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 75    /* Amount of u
1319a 73 61 62 6c 65 20 73 70 61 63 65 20 6f 6e 20 65  sable space on e
1319b 61 63 68 20 70 61 67 65 20 2a 2f 0a 20 20 20 20  ach page */.    
1319c 69 6e 74 20 63 65 6c 6c 4f 66 66 73 65 74 3b 20  int cellOffset; 
1319d 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f     /* Offset fro
1319e 6d 20 73 74 61 72 74 20 6f 66 20 70 61 67 65 20  m start of page 
1319f 74 6f 20 66 69 72 73 74 20 63 65 6c 6c 20 70 6f  to first cell po
131a0 69 6e 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74  inter */.    int
131a1 20 6e 46 72 65 65 3b 20 20 20 20 20 20 20 20 20   nFree;         
131a2 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 75 6e 75  /* Number of unu
131a3 73 65 64 20 62 79 74 65 73 20 6f 6e 20 74 68 65  sed bytes on the
131a4 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 69 6e 74   page */.    int
131a5 20 74 6f 70 3b 20 20 20 20 20 20 20 20 20 20 20   top;           
131a6 2f 2a 20 46 69 72 73 74 20 62 79 74 65 20 6f 66  /* First byte of
131a7 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
131a8 74 20 61 72 65 61 20 2a 2f 0a 0a 20 20 20 20 70  t area */..    p
131a9 42 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b  Bt = pPage->pBt;
131aa 0a 0a 20 20 20 20 68 64 72 20 3d 20 70 50 61 67  ..    hdr = pPag
131ab 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20 20  e->hdrOffset;.  
131ac 20 20 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e    data = pPage->
131ad 61 44 61 74 61 3b 0a 20 20 20 20 69 66 28 20 64  aData;.    if( d
131ae 65 63 6f 64 65 46 6c 61 67 73 28 70 50 61 67 65  ecodeFlags(pPage
131af 2c 20 64 61 74 61 5b 68 64 72 5d 29 20 29 20 72  , data[hdr]) ) r
131b0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52  eturn SQLITE_COR
131b1 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 61  RUPT_BKPT;.    a
131b2 73 73 65 72 74 28 20 70 42 74 2d 3e 70 61 67 65  ssert( pBt->page
131b3 53 69 7a 65 3e 3d 35 31 32 20 26 26 20 70 42 74  Size>=512 && pBt
131b4 2d 3e 70 61 67 65 53 69 7a 65 3c 3d 33 32 37 36  ->pageSize<=3276
131b5 38 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d 3e  8 );.    pPage->
131b6 6d 61 73 6b 50 61 67 65 20 3d 20 70 42 74 2d 3e  maskPage = pBt->
131b7 70 61 67 65 53 69 7a 65 20 2d 20 31 3b 0a 20 20  pageSize - 1;.  
131b8 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
131b9 6f 77 20 3d 20 30 3b 0a 20 20 20 20 75 73 61 62  ow = 0;.    usab
131ba 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  leSize = pBt->us
131bb 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 70 50  ableSize;.    pP
131bc 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73 65 74 20  age->cellOffset 
131bd 3d 20 63 65 6c 6c 4f 66 66 73 65 74 20 3d 20 68  = cellOffset = h
131be 64 72 20 2b 20 31 32 20 2d 20 34 2a 70 50 61 67  dr + 12 - 4*pPag
131bf 65 2d 3e 6c 65 61 66 3b 0a 20 20 20 20 74 6f 70  e->leaf;.    top
131c0 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74   = get2byte(&dat
131c1 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 20 20 70  a[hdr+5]);.    p
131c2 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 3d 20 67 65  Page->nCell = ge
131c3 74 32 62 79 74 65 28 26 64 61 74 61 5b 68 64 72  t2byte(&data[hdr
131c4 2b 33 5d 29 3b 0a 20 20 20 20 69 66 28 20 70 50  +3]);.    if( pP
131c5 61 67 65 2d 3e 6e 43 65 6c 6c 3e 4d 58 5f 43 45  age->nCell>MX_CE
131c6 4c 4c 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  LL(pBt) ){.     
131c7 20 2f 2a 20 54 6f 20 6d 61 6e 79 20 63 65 6c 6c   /* To many cell
131c8 73 20 66 6f 72 20 61 20 73 69 6e 67 6c 65 20 70  s for a single p
131c9 61 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 6d  age.  The page m
131ca 75 73 74 20 62 65 20 63 6f 72 72 75 70 74 20 2a  ust be corrupt *
131cb 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  /.      return S
131cc 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
131cd 50 54 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  PT;.    }.  .   
131ce 20 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20   /* Compute the 
131cf 74 6f 74 61 6c 20 66 72 65 65 20 73 70 61 63 65  total free space
131d0 20 6f 6e 20 74 68 65 20 70 61 67 65 20 2a 2f 0a   on the page */.
131d1 20 20 20 20 70 63 20 3d 20 67 65 74 32 62 79 74      pc = get2byt
131d2 65 28 26 64 61 74 61 5b 68 64 72 2b 31 5d 29 3b  e(&data[hdr+1]);
131d3 0a 20 20 20 20 6e 46 72 65 65 20 3d 20 64 61 74  .    nFree = dat
131d4 61 5b 68 64 72 2b 37 5d 20 2b 20 74 6f 70 20 2d  a[hdr+7] + top -
131d5 20 28 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32   (cellOffset + 2
131d6 2a 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 29 3b 0a  *pPage->nCell);.
131d7 20 20 20 20 77 68 69 6c 65 28 20 70 63 3e 30 20      while( pc>0 
131d8 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 65 78  ){.      int nex
131d9 74 2c 20 73 69 7a 65 3b 0a 20 20 20 20 20 20 69  t, size;.      i
131da 66 28 20 70 63 3e 75 73 61 62 6c 65 53 69 7a 65  f( pc>usableSize
131db 2d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  -4 ){.        /*
131dc 20 46 72 65 65 20 62 6c 6f 63 6b 20 69 73 20 6f   Free block is o
131dd 66 66 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 20  ff the page */. 
131de 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
131df 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
131e0 54 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  T; .      }.    
131e1 20 20 6e 65 78 74 20 3d 20 67 65 74 32 62 79 74    next = get2byt
131e2 65 28 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20  e(&data[pc]);.  
131e3 20 20 20 20 73 69 7a 65 20 3d 20 67 65 74 32 62      size = get2b
131e4 79 74 65 28 26 64 61 74 61 5b 70 63 2b 32 5d 29  yte(&data[pc+2])
131e5 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 65 78 74  ;.      if( next
131e6 3e 30 20 26 26 20 6e 65 78 74 3c 3d 70 63 2b 73  >0 && next<=pc+s
131e7 69 7a 65 2b 33 20 29 7b 0a 20 20 20 20 20 20 20  ize+3 ){.       
131e8 20 2f 2a 20 46 72 65 65 20 62 6c 6f 63 6b 73 20   /* Free blocks 
131e9 6d 75 73 74 20 62 65 20 69 6e 20 61 63 63 65 6e  must be in accen
131ea 64 69 6e 67 20 6f 72 64 65 72 20 2a 2f 0a 20 20  ding order */.  
131eb 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
131ec 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
131ed 3b 20 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ; .      }.     
131ee 20 6e 46 72 65 65 20 2b 3d 20 73 69 7a 65 3b 0a   nFree += size;.
131ef 20 20 20 20 20 20 70 63 20 3d 20 6e 65 78 74 3b        pc = next;
131f0 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65  .    }.    pPage
131f1 2d 3e 6e 46 72 65 65 20 3d 20 6e 46 72 65 65 3b  ->nFree = nFree;
131f2 0a 20 20 20 20 69 66 28 20 6e 46 72 65 65 3e 3d  .    if( nFree>=
131f3 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
131f4 20 20 20 20 2f 2a 20 46 72 65 65 20 73 70 61 63      /* Free spac
131f5 65 20 63 61 6e 6e 6f 74 20 65 78 63 65 65 64 20  e cannot exceed 
131f6 74 6f 74 61 6c 20 70 61 67 65 20 73 69 7a 65 20  total page size 
131f7 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  */.      return 
131f8 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
131f9 4b 50 54 3b 20 0a 20 20 20 20 7d 0a 0a 23 69 66  KPT; .    }..#if
131fa 20 30 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68   0.  /* Check th
131fb 61 74 20 61 6c 6c 20 74 68 65 20 6f 66 66 73 65  at all the offse
131fc 74 73 20 69 6e 20 74 68 65 20 63 65 6c 6c 20 6f  ts in the cell o
131fd 66 66 73 65 74 20 61 72 72 61 79 20 61 72 65 20  ffset array are 
131fe 77 69 74 68 69 6e 20 72 61 6e 67 65 2e 20 0a 20  within range. . 
131ff 20 2a 2a 20 0a 20 20 2a 2a 20 4f 6d 69 74 74 69   ** .  ** Omitti
13200 6e 67 20 74 68 69 73 20 63 6f 6e 73 69 73 74 65  ng this consiste
13201 6e 63 79 20 63 68 65 63 6b 20 61 6e 64 20 75 73  ncy check and us
13202 69 6e 67 20 74 68 65 20 70 50 61 67 65 2d 3e 6d  ing the pPage->m
13203 61 73 6b 50 61 67 65 20 6d 61 73 6b 0a 20 20 2a  askPage mask.  *
13204 2a 20 74 6f 20 70 72 65 76 65 6e 74 20 6f 76 65  * to prevent ove
13205 72 72 75 6e 6e 69 6e 67 20 74 68 65 20 70 61 67  rrunning the pag
13206 65 20 62 75 66 66 65 72 20 69 6e 20 66 69 6e 64  e buffer in find
13207 43 65 6c 6c 28 29 20 72 65 73 75 6c 74 73 20 69  Cell() results i
13208 6e 20 61 0a 20 20 2a 2a 20 32 2e 35 25 20 70 65  n a.  ** 2.5% pe
13209 72 66 6f 72 6d 61 6e 63 65 20 67 61 69 6e 2e 0a  rformance gain..
1320a 20 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 75 38 20    */.  {.    u8 
1320b 2a 70 4f 66 66 3b 20 20 20 20 20 20 20 20 2f 2a  *pOff;        /*
1320c 20 49 74 65 72 61 74 6f 72 20 75 73 65 64 20 74   Iterator used t
1320d 6f 20 63 68 65 63 6b 20 61 6c 6c 20 63 65 6c 6c  o check all cell
1320e 20 6f 66 66 73 65 74 73 20 61 72 65 20 69 6e 20   offsets are in 
1320f 72 61 6e 67 65 20 2a 2f 0a 20 20 20 20 75 38 20  range */.    u8 
13210 2a 70 45 6e 64 3b 20 20 20 20 20 20 20 20 2f 2a  *pEnd;        /*
13211 20 50 6f 69 6e 74 65 72 20 74 6f 20 65 6e 64 20   Pointer to end 
13212 6f 66 20 63 65 6c 6c 20 6f 66 66 73 65 74 20 61  of cell offset a
13213 72 72 61 79 20 2a 2f 0a 20 20 20 20 75 38 20 6d  rray */.    u8 m
13214 61 73 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  ask;         /* 
13215 4d 61 73 6b 20 6f 66 20 62 69 74 73 20 74 68 61  Mask of bits tha
13216 74 20 6d 75 73 74 20 62 65 20 7a 65 72 6f 20 69  t must be zero i
13217 6e 20 4d 53 42 20 6f 66 20 63 65 6c 6c 20 6f 66  n MSB of cell of
13218 66 73 65 74 73 20 2a 2f 0a 20 20 20 20 6d 61 73  fsets */.    mas
13219 6b 20 3d 20 7e 28 28 28 75 38 29 28 70 42 74 2d  k = ~(((u8)(pBt-
1321a 3e 70 61 67 65 53 69 7a 65 3e 3e 38 29 29 2d 31  >pageSize>>8))-1
1321b 29 3b 0a 20 20 20 20 70 45 6e 64 20 3d 20 26 64  );.    pEnd = &d
1321c 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 20 2b  ata[cellOffset +
1321d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2a 32 5d   pPage->nCell*2]
1321e 3b 0a 20 20 20 20 66 6f 72 28 70 4f 66 66 3d 26  ;.    for(pOff=&
1321f 64 61 74 61 5b 63 65 6c 6c 4f 66 66 73 65 74 5d  data[cellOffset]
13220 3b 20 70 4f 66 66 21 3d 70 45 6e 64 20 26 26 20  ; pOff!=pEnd && 
13221 21 28 28 2a 70 4f 66 66 29 26 6d 61 73 6b 29 3b  !((*pOff)&mask);
13222 20 70 4f 66 66 2b 3d 32 29 3b 0a 20 20 20 20 69   pOff+=2);.    i
13223 66 28 20 70 4f 66 66 21 3d 70 45 6e 64 20 29 7b  f( pOff!=pEnd ){
13224 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  .      return SQ
13225 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
13226 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  T;.    }.  }.#en
13227 64 69 66 0a 0a 20 20 20 20 70 50 61 67 65 2d 3e  dif..    pPage->
13228 69 73 49 6e 69 74 20 3d 20 31 3b 0a 20 20 7d 0a  isInit = 1;.  }.
13229 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1322a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  OK;.}../*.** Set
1322b 20 75 70 20 61 20 72 61 77 20 70 61 67 65 20 73   up a raw page s
1322c 6f 20 74 68 61 74 20 69 74 20 6c 6f 6f 6b 73 20  o that it looks 
1322d 6c 69 6b 65 20 61 20 64 61 74 61 62 61 73 65 20  like a database 
1322e 70 61 67 65 20 68 6f 6c 64 69 6e 67 0a 2a 2a 20  page holding.** 
1322f 6e 6f 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73  no entries..*/.s
13230 74 61 74 69 63 20 76 6f 69 64 20 7a 65 72 6f 50  tatic void zeroP
13231 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50 61  age(MemPage *pPa
13232 67 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a  ge, int flags){.
13233 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
13234 2a 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61  *data = pPage->a
13235 44 61 74 61 3b 0a 20 20 42 74 53 68 61 72 65 64  Data;.  BtShared
13236 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70   *pBt = pPage->p
13237 42 74 3b 0a 20 20 69 6e 74 20 68 64 72 20 3d 20  Bt;.  int hdr = 
13238 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74  pPage->hdrOffset
13239 3b 0a 20 20 69 6e 74 20 66 69 72 73 74 3b 0a 0a  ;.  int first;..
1323a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
1323b 33 50 61 67 65 72 50 61 67 65 6e 75 6d 62 65 72  3PagerPagenumber
1323c 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
1323d 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 20 29 3b  ==pPage->pgno );
1323e 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1323f 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
13240 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20  pPage->pDbPage) 
13241 3d 3d 20 28 76 6f 69 64 2a 29 70 50 61 67 65 20  == (void*)pPage 
13242 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
13243 69 74 65 33 50 61 67 65 72 47 65 74 44 61 74 61  ite3PagerGetData
13244 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29  (pPage->pDbPage)
13245 20 3d 3d 20 64 61 74 61 20 29 3b 0a 20 20 61 73   == data );.  as
13246 73 65 72 74 28 20 73 71 6c 69 74 65 33 50 61 67  sert( sqlite3Pag
13247 65 72 49 73 77 72 69 74 65 61 62 6c 65 28 70 50  erIswriteable(pP
13248 61 67 65 2d 3e 70 44 62 50 61 67 65 29 20 29 3b  age->pDbPage) );
13249 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1324a 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42  e3_mutex_held(pB
1324b 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f  t->mutex) );.  /
1324c 2a 6d 65 6d 73 65 74 28 26 64 61 74 61 5b 68 64  *memset(&data[hd
1324d 72 5d 2c 20 30 2c 20 70 42 74 2d 3e 75 73 61 62  r], 0, pBt->usab
1324e 6c 65 53 69 7a 65 20 2d 20 68 64 72 29 3b 2a 2f  leSize - hdr);*/
1324f 0a 20 20 64 61 74 61 5b 68 64 72 5d 20 3d 20 66  .  data[hdr] = f
13250 6c 61 67 73 3b 0a 20 20 66 69 72 73 74 20 3d 20  lags;.  first = 
13251 68 64 72 20 2b 20 38 20 2b 20 34 2a 28 28 66 6c  hdr + 8 + 4*((fl
13252 61 67 73 26 50 54 46 5f 4c 45 41 46 29 3d 3d 30  ags&PTF_LEAF)==0
13253 29 3b 0a 20 20 6d 65 6d 73 65 74 28 26 64 61 74  );.  memset(&dat
13254 61 5b 68 64 72 2b 31 5d 2c 20 30 2c 20 34 29 3b  a[hdr+1], 0, 4);
13255 0a 20 20 64 61 74 61 5b 68 64 72 2b 37 5d 20 3d  .  data[hdr+7] =
13256 20 30 3b 0a 20 20 70 75 74 32 62 79 74 65 28 26   0;.  put2byte(&
13257 64 61 74 61 5b 68 64 72 2b 35 5d 2c 20 70 42 74  data[hdr+5], pBt
13258 2d 3e 75 73 61 62 6c 65 53 69 7a 65 29 3b 0a 20  ->usableSize);. 
13259 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 20 3d 20   pPage->nFree = 
1325a 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
1325b 2d 20 66 69 72 73 74 3b 0a 20 20 64 65 63 6f 64  - first;.  decod
1325c 65 46 6c 61 67 73 28 70 50 61 67 65 2c 20 66 6c  eFlags(pPage, fl
1325d 61 67 73 29 3b 0a 20 20 70 50 61 67 65 2d 3e 68  ags);.  pPage->h
1325e 64 72 4f 66 66 73 65 74 20 3d 20 68 64 72 3b 0a  drOffset = hdr;.
1325f 20 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66    pPage->cellOff
13260 73 65 74 20 3d 20 66 69 72 73 74 3b 0a 20 20 70  set = first;.  p
13261 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20  Page->nOverflow 
13262 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 0;.  assert( p
13263 42 74 2d 3e 70 61 67 65 53 69 7a 65 3e 3d 35 31  Bt->pageSize>=51
13264 32 20 26 26 20 70 42 74 2d 3e 70 61 67 65 53 69  2 && pBt->pageSi
13265 7a 65 3c 3d 33 32 37 36 38 20 29 3b 0a 20 20 70  ze<=32768 );.  p
13266 50 61 67 65 2d 3e 6d 61 73 6b 50 61 67 65 20 3d  Page->maskPage =
13267 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d   pBt->pageSize -
13268 20 31 3b 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65   1;.  pPage->nCe
13269 6c 6c 20 3d 20 30 3b 0a 20 20 70 50 61 67 65 2d  ll = 0;.  pPage-
1326a 3e 69 73 49 6e 69 74 20 3d 20 31 3b 0a 7d 0a 0a  >isInit = 1;.}..
1326b 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 61  ./*.** Convert a
1326c 20 44 62 50 61 67 65 20 6f 62 74 61 69 6e 65 64   DbPage obtained
1326d 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65 72 20   from the pager 
1326e 69 6e 74 6f 20 61 20 4d 65 6d 50 61 67 65 20 75  into a MemPage u
1326f 73 65 64 20 62 79 0a 2a 2a 20 74 68 65 20 62 74  sed by.** the bt
13270 72 65 65 20 6c 61 79 65 72 2e 0a 2a 2f 0a 73 74  ree layer..*/.st
13271 61 74 69 63 20 4d 65 6d 50 61 67 65 20 2a 62 74  atic MemPage *bt
13272 72 65 65 50 61 67 65 46 72 6f 6d 44 62 50 61 67  reePageFromDbPag
13273 65 28 44 62 50 61 67 65 20 2a 70 44 62 50 61 67  e(DbPage *pDbPag
13274 65 2c 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 42 74  e, Pgno pgno, Bt
13275 53 68 61 72 65 64 20 2a 70 42 74 29 7b 0a 20 20  Shared *pBt){.  
13276 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
13277 20 28 4d 65 6d 50 61 67 65 2a 29 73 71 6c 69 74   (MemPage*)sqlit
13278 65 33 50 61 67 65 72 47 65 74 45 78 74 72 61 28  e3PagerGetExtra(
13279 70 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67  pDbPage);.  pPag
1327a 65 2d 3e 61 44 61 74 61 20 3d 20 73 71 6c 69 74  e->aData = sqlit
1327b 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
1327c 44 62 50 61 67 65 29 3b 0a 20 20 70 50 61 67 65  DbPage);.  pPage
1327d 2d 3e 70 44 62 50 61 67 65 20 3d 20 70 44 62 50  ->pDbPage = pDbP
1327e 61 67 65 3b 0a 20 20 70 50 61 67 65 2d 3e 70 42  age;.  pPage->pB
1327f 74 20 3d 20 70 42 74 3b 0a 20 20 70 50 61 67 65  t = pBt;.  pPage
13280 2d 3e 70 67 6e 6f 20 3d 20 70 67 6e 6f 3b 0a 20  ->pgno = pgno;. 
13281 20 70 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65   pPage->hdrOffse
13282 74 20 3d 20 70 50 61 67 65 2d 3e 70 67 6e 6f 3d  t = pPage->pgno=
13283 3d 31 20 3f 20 31 30 30 20 3a 20 30 3b 0a 20 20  =1 ? 100 : 0;.  
13284 72 65 74 75 72 6e 20 70 50 61 67 65 3b 20 0a 7d  return pPage; .}
13285 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61  ../*.** Get a pa
13286 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ge from the page
13287 72 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20 74  r.  Initialize t
13288 68 65 20 4d 65 6d 50 61 67 65 2e 70 42 74 20 61  he MemPage.pBt a
13289 6e 64 0a 2a 2a 20 4d 65 6d 50 61 67 65 2e 61 44  nd.** MemPage.aD
1328a 61 74 61 20 65 6c 65 6d 65 6e 74 73 20 69 66 20  ata elements if 
1328b 6e 65 65 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  needed..**.** If
1328c 20 74 68 65 20 6e 6f 43 6f 6e 74 65 6e 74 20 66   the noContent f
1328d 6c 61 67 20 69 73 20 73 65 74 2c 20 69 74 20 6d  lag is set, it m
1328e 65 61 6e 73 20 74 68 61 74 20 77 65 20 64 6f 20  eans that we do 
1328f 6e 6f 74 20 63 61 72 65 20 61 62 6f 75 74 0a 2a  not care about.*
13290 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  * the content of
13291 20 74 68 65 20 70 61 67 65 20 61 74 20 74 68 69   the page at thi
13292 73 20 74 69 6d 65 2e 20 20 53 6f 20 64 6f 20 6e  s time.  So do n
13293 6f 74 20 67 6f 20 74 6f 20 74 68 65 20 64 69 73  ot go to the dis
13294 6b 0a 2a 2a 20 74 6f 20 66 65 74 63 68 20 74 68  k.** to fetch th
13295 65 20 63 6f 6e 74 65 6e 74 2e 20 20 4a 75 73 74  e content.  Just
13296 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 63 6f 6e   fill in the con
13297 74 65 6e 74 20 77 69 74 68 20 7a 65 72 6f 73 20  tent with zeros 
13298 66 6f 72 20 6e 6f 77 2e 0a 2a 2a 20 49 66 20 69  for now..** If i
13299 6e 20 74 68 65 20 66 75 74 75 72 65 20 77 65 20  n the future we 
1329a 63 61 6c 6c 20 73 71 6c 69 74 65 33 50 61 67 65  call sqlite3Page
1329b 72 57 72 69 74 65 28 29 20 6f 6e 20 74 68 69 73  rWrite() on this
1329c 20 70 61 67 65 2c 20 74 68 61 74 0a 2a 2a 20 6d   page, that.** m
1329d 65 61 6e 73 20 77 65 20 68 61 76 65 20 73 74 61  eans we have sta
1329e 72 74 65 64 20 74 6f 20 62 65 20 63 6f 6e 63 65  rted to be conce
1329f 72 6e 65 64 20 61 62 6f 75 74 20 63 6f 6e 74 65  rned about conte
132a0 6e 74 20 61 6e 64 20 74 68 65 20 64 69 73 6b 0a  nt and the disk.
132a1 2a 2a 20 72 65 61 64 20 73 68 6f 75 6c 64 20 6f  ** read should o
132a2 63 63 75 72 20 61 74 20 74 68 61 74 20 70 6f 69  ccur at that poi
132a3 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nt..*/.SQLITE_PR
132a4 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
132a5 33 42 74 72 65 65 47 65 74 50 61 67 65 28 0a 20  3BtreeGetPage(. 
132a6 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
132a7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72        /* The btr
132a8 65 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 70 67 6e  ee */.  Pgno pgn
132a9 6f 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  o,           /* 
132aa 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 70 61  Number of the pa
132ab 67 65 20 74 6f 20 66 65 74 63 68 20 2a 2f 0a 20  ge to fetch */. 
132ac 20 4d 65 6d 50 61 67 65 20 2a 2a 70 70 50 61 67   MemPage **ppPag
132ad 65 2c 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20  e,    /* Return 
132ae 74 68 65 20 70 61 67 65 20 69 6e 20 74 68 69 73  the page in this
132af 20 70 61 72 61 6d 65 74 65 72 20 2a 2f 0a 20 20   parameter */.  
132b0 69 6e 74 20 6e 6f 43 6f 6e 74 65 6e 74 20 20 20  int noContent   
132b1 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 6c       /* Do not l
132b2 6f 61 64 20 70 61 67 65 20 63 6f 6e 74 65 6e 74  oad page content
132b3 20 69 66 20 74 72 75 65 20 2a 2f 0a 29 7b 0a 20   if true */.){. 
132b4 20 69 6e 74 20 72 63 3b 0a 20 20 44 62 50 61 67   int rc;.  DbPag
132b5 65 20 2a 70 44 62 50 61 67 65 3b 0a 0a 20 20 61  e *pDbPage;..  a
132b6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
132b7 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d  utex_held(pBt->m
132b8 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d 20  utex) );.  rc = 
132b9 73 71 6c 69 74 65 33 50 61 67 65 72 41 63 71 75  sqlite3PagerAcqu
132ba 69 72 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ire(pBt->pPager,
132bb 20 70 67 6e 6f 2c 20 28 44 62 50 61 67 65 2a 2a   pgno, (DbPage**
132bc 29 26 70 44 62 50 61 67 65 2c 20 6e 6f 43 6f 6e  )&pDbPage, noCon
132bd 74 65 6e 74 29 3b 0a 20 20 69 66 28 20 72 63 20  tent);.  if( rc 
132be 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 2a  ) return rc;.  *
132bf 70 70 50 61 67 65 20 3d 20 62 74 72 65 65 50 61  ppPage = btreePa
132c0 67 65 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62  geFromDbPage(pDb
132c1 50 61 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29  Page, pgno, pBt)
132c2 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
132c3 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
132c4 65 74 75 72 6e 20 74 68 65 20 73 69 7a 65 20 6f  eturn the size o
132c5 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
132c6 69 6c 65 20 69 6e 20 70 61 67 65 73 2e 20 49 66  ile in pages. If
132c7 20 74 68 65 72 65 20 69 73 20 61 6e 79 20 6b 69   there is any ki
132c8 6e 64 20 6f 66 0a 2a 2a 20 65 72 72 6f 72 2c 20  nd of.** error, 
132c9 72 65 74 75 72 6e 20 28 28 75 6e 73 69 67 6e 65  return ((unsigne
132ca 64 20 69 6e 74 29 2d 31 29 2e 0a 2a 2f 0a 73 74  d int)-1)..*/.st
132cb 61 74 69 63 20 50 67 6e 6f 20 70 61 67 65 72 50  atic Pgno pagerP
132cc 61 67 65 63 6f 75 6e 74 28 42 74 53 68 61 72 65  agecount(BtShare
132cd 64 20 2a 70 42 74 29 7b 0a 20 20 69 6e 74 20 6e  d *pBt){.  int n
132ce 50 61 67 65 20 3d 20 2d 31 3b 0a 20 20 69 6e 74  Page = -1;.  int
132cf 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 70   rc;.  assert( p
132d0 42 74 2d 3e 70 50 61 67 65 31 20 29 3b 0a 20 20  Bt->pPage1 );.  
132d1 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
132d2 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 2d 3e  rPagecount(pBt->
132d3 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65 29 3b  pPager, &nPage);
132d4 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53  .  assert( rc==S
132d5 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61 67  QLITE_OK || nPag
132d6 65 3d 3d 2d 31 20 29 3b 0a 20 20 72 65 74 75 72  e==-1 );.  retur
132d7 6e 20 28 50 67 6e 6f 29 6e 50 61 67 65 3b 0a 7d  n (Pgno)nPage;.}
132d8 0a 0a 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 70 61  ../*.** Get a pa
132d9 67 65 20 66 72 6f 6d 20 74 68 65 20 70 61 67 65  ge from the page
132da 72 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 7a 65  r and initialize
132db 20 69 74 2e 20 20 54 68 69 73 20 72 6f 75 74 69   it.  This routi
132dc 6e 65 0a 2a 2a 20 69 73 20 6a 75 73 74 20 61 20  ne.** is just a 
132dd 63 6f 6e 76 65 6e 69 65 6e 63 65 20 77 72 61 70  convenience wrap
132de 70 65 72 20 61 72 6f 75 6e 64 20 73 65 70 61 72  per around separ
132df 61 74 65 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ate calls to.** 
132e0 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 50  sqlite3BtreeGetP
132e1 61 67 65 28 29 20 61 6e 64 20 73 71 6c 69 74 65  age() and sqlite
132e2 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29  3BtreeInitPage()
132e3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
132e4 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28 0a  getAndInitPage(.
132e5 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c    BtShared *pBt,
132e6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
132e7 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 2a   database file *
132e8 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 2c 20 20  /.  Pgno pgno,  
132e9 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
132ea 65 72 20 6f 66 20 74 68 65 20 70 61 67 65 20 74  er of the page t
132eb 6f 20 67 65 74 20 2a 2f 0a 20 20 4d 65 6d 50 61  o get */.  MemPa
132ec 67 65 20 2a 2a 70 70 50 61 67 65 20 20 20 20 20  ge **ppPage     
132ed 2f 2a 20 57 72 69 74 65 20 74 68 65 20 70 61 67  /* Write the pag
132ee 65 20 70 6f 69 6e 74 65 72 20 68 65 72 65 20 2a  e pointer here *
132ef 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
132f0 20 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65   DbPage *pDbPage
132f1 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
132f2 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ge;..  assert( s
132f3 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
132f4 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b  d(pBt->mutex) );
132f5 0a 20 20 69 66 28 20 70 67 6e 6f 3d 3d 30 20 29  .  if( pgno==0 )
132f6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
132f7 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
132f8 3b 20 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20  ; .  }..  /* It 
132f9 69 73 20 6f 66 74 65 6e 20 74 68 65 20 63 61 73  is often the cas
132fa 65 20 74 68 61 74 20 74 68 65 20 70 61 67 65 20  e that the page 
132fb 77 65 20 77 61 6e 74 20 69 73 20 61 6c 72 65 61  we want is alrea
132fc 64 79 20 69 6e 20 63 61 63 68 65 2e 0a 20 20 2a  dy in cache..  *
132fd 2a 20 49 66 20 73 6f 2c 20 67 65 74 20 69 74 20  * If so, get it 
132fe 64 69 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20  directly.  This 
132ff 73 61 76 65 73 20 75 73 20 66 72 6f 6d 20 68 61  saves us from ha
13300 76 69 6e 67 20 74 6f 20 63 61 6c 6c 0a 20 20 2a  ving to call.  *
13301 2a 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74  * pagerPagecount
13302 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
13303 70 67 6e 6f 20 69 73 20 77 69 74 68 69 6e 20 6c  pgno is within l
13304 69 6d 69 74 73 2c 20 77 68 69 63 68 20 72 65 73  imits, which res
13305 75 6c 74 73 0a 20 20 2a 2a 20 69 6e 20 61 20 6d  ults.  ** in a m
13306 65 61 73 75 72 65 61 62 6c 65 20 70 65 72 66 6f  easureable perfo
13307 72 6d 61 6e 63 65 20 69 6d 70 72 6f 76 65 6d 65  rmance improveme
13308 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 44 62 50  nts..  */.  pDbP
13309 61 67 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67  age = sqlite3Pag
1330a 65 72 4c 6f 6f 6b 75 70 28 70 42 74 2d 3e 70 50  erLookup(pBt->pP
1330b 61 67 65 72 2c 20 70 67 6e 6f 29 3b 0a 20 20 69  ager, pgno);.  i
1330c 66 28 20 70 44 62 50 61 67 65 20 29 7b 0a 20 20  f( pDbPage ){.  
1330d 20 20 2f 2a 20 50 61 67 65 20 69 73 20 61 6c 72    /* Page is alr
1330e 65 61 64 79 20 69 6e 20 63 61 63 68 65 20 2a 2f  eady in cache */
1330f 0a 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70  .    *ppPage = p
13310 50 61 67 65 20 3d 20 62 74 72 65 65 50 61 67 65  Page = btreePage
13311 46 72 6f 6d 44 62 50 61 67 65 28 70 44 62 50 61  FromDbPage(pDbPa
13312 67 65 2c 20 70 67 6e 6f 2c 20 70 42 74 29 3b 0a  ge, pgno, pBt);.
13313 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
13314 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OK;.  }else{.   
13315 20 2f 2a 20 50 61 67 65 20 6e 6f 74 20 69 6e 20   /* Page not in 
13316 63 61 63 68 65 2e 20 20 41 63 71 75 69 72 65 20  cache.  Acquire 
13317 69 74 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 70  it. */.    if( p
13318 67 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75  gno>pagerPagecou
13319 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  nt(pBt) ){.     
1331a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
1331b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 20 0a 20 20  ORRUPT_BKPT; .  
1331c 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
1331d 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
1331e 28 70 42 74 2c 20 70 67 6e 6f 2c 20 70 70 50 61  (pBt, pgno, ppPa
1331f 67 65 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20  ge, 0);.    if( 
13320 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
13321 20 20 20 20 70 50 61 67 65 20 3d 20 2a 70 70 50      pPage = *ppP
13322 61 67 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  age;.  }.  if( !
13323 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20 29 7b  pPage->isInit ){
13324 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
13325 33 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70  3BtreeInitPage(p
13326 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 69 66 28  Page);.  }.  if(
13327 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13328 7b 0a 20 20 20 20 72 65 6c 65 61 73 65 50 61 67  {.    releasePag
13329 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 2a 70  e(pPage);.    *p
1332a 70 50 61 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 20  pPage = 0;.  }. 
1332b 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1332c 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20 61 20 4d  *.** Release a M
1332d 65 6d 50 61 67 65 2e 20 20 54 68 69 73 20 73 68  emPage.  This sh
1332e 6f 75 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 6f  ould be called o
1332f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 70 72 69  nce for each pri
13330 6f 72 0a 2a 2a 20 63 61 6c 6c 20 74 6f 20 73 71  or.** call to sq
13331 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
13332 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
13333 64 20 72 65 6c 65 61 73 65 50 61 67 65 28 4d 65  d releasePage(Me
13334 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a 20  mPage *pPage){. 
13335 20 69 66 28 20 70 50 61 67 65 20 29 7b 0a 20 20   if( pPage ){.  
13336 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
13337 3e 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73  >aData );.    as
13338 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 42 74  sert( pPage->pBt
13339 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
1333a 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 45  sqlite3PagerGetE
1333b 78 74 72 61 28 70 50 61 67 65 2d 3e 70 44 62 50  xtra(pPage->pDbP
1333c 61 67 65 29 20 3d 3d 20 28 76 6f 69 64 2a 29 70  age) == (void*)p
1333d 50 61 67 65 20 29 3b 0a 20 20 20 20 61 73 73 65  Page );.    asse
1333e 72 74 28 20 73 71 6c 69 74 65 33 50 61 67 65 72  rt( sqlite3Pager
1333f 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e 70  GetData(pPage->p
13340 44 62 50 61 67 65 29 3d 3d 70 50 61 67 65 2d 3e  DbPage)==pPage->
13341 61 44 61 74 61 20 29 3b 0a 20 20 20 20 61 73 73  aData );.    ass
13342 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
13343 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
13344 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
13345 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
13346 72 65 66 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ref(pPage->pDbPa
13347 67 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ge);.  }.}../*.*
13348 2a 20 44 75 72 69 6e 67 20 61 20 72 6f 6c 6c 62  * During a rollb
13349 61 63 6b 2c 20 77 68 65 6e 20 74 68 65 20 70 61  ack, when the pa
1334a 67 65 72 20 72 65 6c 6f 61 64 73 20 69 6e 66 6f  ger reloads info
1334b 72 6d 61 74 69 6f 6e 20 69 6e 74 6f 20 74 68 65  rmation into the
1334c 20 63 61 63 68 65 0a 2a 2a 20 73 6f 20 74 68 61   cache.** so tha
1334d 74 20 74 68 65 20 63 61 63 68 65 20 69 73 20 72  t the cache is r
1334e 65 73 74 6f 72 65 64 20 74 6f 20 69 74 73 20 6f  estored to its o
1334f 72 69 67 69 6e 61 6c 20 73 74 61 74 65 20 61 74  riginal state at
13350 20 74 68 65 20 73 74 61 72 74 20 6f 66 0a 2a 2a   the start of.**
13351 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
13352 2c 20 66 6f 72 20 65 61 63 68 20 70 61 67 65 20  , for each page 
13353 72 65 73 74 6f 72 65 64 20 74 68 69 73 20 72 6f  restored this ro
13354 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2e  utine is called.
13355 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
13356 69 6e 65 20 6e 65 65 64 73 20 74 6f 20 72 65 73  ine needs to res
13357 65 74 20 74 68 65 20 65 78 74 72 61 20 64 61 74  et the extra dat
13358 61 20 73 65 63 74 69 6f 6e 20 61 74 20 74 68 65  a section at the
13359 20 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 70   end of the.** p
1335a 61 67 65 20 74 6f 20 61 67 72 65 65 20 77 69 74  age to agree wit
1335b 68 20 74 68 65 20 72 65 73 74 6f 72 65 64 20 64  h the restored d
1335c 61 74 61 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ata..*/.static v
1335d 6f 69 64 20 70 61 67 65 52 65 69 6e 69 74 28 44  oid pageReinit(D
1335e 62 50 61 67 65 20 2a 70 44 61 74 61 29 7b 0a 20  bPage *pData){. 
1335f 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b   MemPage *pPage;
13360 0a 20 20 70 50 61 67 65 20 3d 20 28 4d 65 6d 50  .  pPage = (MemP
13361 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50 61 67  age *)sqlite3Pag
13362 65 72 47 65 74 45 78 74 72 61 28 70 44 61 74 61  erGetExtra(pData
13363 29 3b 0a 20 20 69 66 28 20 70 50 61 67 65 2d 3e  );.  if( pPage->
13364 69 73 49 6e 69 74 20 29 7b 0a 20 20 20 20 61 73  isInit ){.    as
13365 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
13366 74 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e  tex_held(pPage->
13367 70 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  pBt->mutex) );. 
13368 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74     pPage->isInit
13369 20 3d 20 30 3b 0a 20 20 20 20 69 66 28 20 73 71   = 0;.    if( sq
1336a 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52 65  lite3PagerPageRe
1336b 66 63 6f 75 6e 74 28 70 44 61 74 61 29 3e 30 20  fcount(pData)>0 
1336c 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1336d 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 70 50  BtreeInitPage(pP
1336e 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
1336f 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  }../*.** Invoke 
13370 74 68 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72  the busy handler
13371 20 66 6f 72 20 61 20 62 74 72 65 65 2e 0a 2a 2f   for a btree..*/
13372 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72 65  .static int btre
13373 65 49 6e 76 6f 6b 65 42 75 73 79 48 61 6e 64 6c  eInvokeBusyHandl
13374 65 72 28 76 6f 69 64 20 2a 70 41 72 67 29 7b 0a  er(void *pArg){.
13375 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
13376 3d 20 28 42 74 53 68 61 72 65 64 2a 29 70 41 72  = (BtShared*)pAr
13377 67 3b 0a 20 20 61 73 73 65 72 74 28 20 70 42 74  g;.  assert( pBt
13378 2d 3e 64 62 20 29 3b 0a 20 20 61 73 73 65 72 74  ->db );.  assert
13379 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
1337a 68 65 6c 64 28 70 42 74 2d 3e 64 62 2d 3e 6d 75  held(pBt->db->mu
1337b 74 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  tex) );.  return
1337c 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65 42 75   sqlite3InvokeBu
1337d 73 79 48 61 6e 64 6c 65 72 28 26 70 42 74 2d 3e  syHandler(&pBt->
1337e 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c 65 72 29  db->busyHandler)
1337f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20  ;.}../*.** Open 
13380 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
13381 0a 2a 2a 20 0a 2a 2a 20 7a 46 69 6c 65 6e 61 6d  .** .** zFilenam
13382 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  e is the name of
13383 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
13384 6c 65 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d  le.  If zFilenam
13385 65 20 69 73 20 4e 55 4c 4c 0a 2a 2a 20 61 20 6e  e is NULL.** a n
13386 65 77 20 64 61 74 61 62 61 73 65 20 77 69 74 68  ew database with
13387 20 61 20 72 61 6e 64 6f 6d 20 6e 61 6d 65 20 69   a random name i
13388 73 20 63 72 65 61 74 65 64 2e 20 20 54 68 69 73  s created.  This
13389 20 72 61 6e 64 6f 6d 6c 79 20 6e 61 6d 65 64 0a   randomly named.
1338a 2a 2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ** database file
1338b 20 77 69 6c 6c 20 62 65 20 64 65 6c 65 74 65 64   will be deleted
1338c 20 77 68 65 6e 20 73 71 6c 69 74 65 33 42 74 72   when sqlite3Btr
1338d 65 65 43 6c 6f 73 65 28 29 20 69 73 20 63 61 6c  eeClose() is cal
1338e 6c 65 64 2e 0a 2a 2a 20 49 66 20 7a 46 69 6c 65  led..** If zFile
1338f 6e 61 6d 65 20 69 73 20 22 3a 6d 65 6d 6f 72 79  name is ":memory
13390 3a 22 20 74 68 65 6e 20 61 6e 20 69 6e 2d 6d 65  :" then an in-me
13391 6d 6f 72 79 20 64 61 74 61 62 61 73 65 20 69 73  mory database is
13392 20 63 72 65 61 74 65 64 0a 2a 2a 20 74 68 61 74   created.** that
13393 20 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   is automaticall
13394 79 20 64 65 73 74 72 6f 79 65 64 20 77 68 65 6e  y destroyed when
13395 20 69 74 20 69 73 20 63 6c 6f 73 65 64 2e 0a 2a   it is closed..*
13396 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
13397 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65   int sqlite3Btre
13398 65 4f 70 65 6e 28 0a 20 20 63 6f 6e 73 74 20 63  eOpen(.  const c
13399 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c 20  har *zFilename, 
1339a 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1339b 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  file containing 
1339c 74 68 65 20 42 54 72 65 65 20 64 61 74 61 62 61  the BTree databa
1339d 73 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  se */.  sqlite3 
1339e 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
1339f 2f 2a 20 41 73 73 6f 63 69 61 74 65 64 20 64 61  /* Associated da
133a0 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 2a 2f  tabase handle */
133a1 0a 20 20 42 74 72 65 65 20 2a 2a 70 70 42 74 72  .  Btree **ppBtr
133a2 65 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 6f  ee,        /* Po
133a3 69 6e 74 65 72 20 74 6f 20 6e 65 77 20 42 74 72  inter to new Btr
133a4 65 65 20 6f 62 6a 65 63 74 20 77 72 69 74 74 65  ee object writte
133a5 6e 20 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20  n here */.  int 
133a6 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20 20  flags,          
133a7 20 20 20 20 2f 2a 20 4f 70 74 69 6f 6e 73 20 2a      /* Options *
133a8 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61 67 73  /.  int vfsFlags
133a9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
133aa 6c 61 67 73 20 70 61 73 73 65 64 20 74 68 72 6f  lags passed thro
133ab 75 67 68 20 74 6f 20 73 71 6c 69 74 65 33 5f 76  ugh to sqlite3_v
133ac 66 73 2e 78 4f 70 65 6e 28 29 20 2a 2f 0a 29 7b  fs.xOpen() */.){
133ad 0a 20 20 73 71 6c 69 74 65 33 5f 76 66 73 20 2a  .  sqlite3_vfs *
133ae 70 56 66 73 3b 20 20 20 20 20 20 2f 2a 20 54 68  pVfs;      /* Th
133af 65 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72  e VFS to use for
133b0 20 74 68 69 73 20 62 74 72 65 65 20 2a 2f 0a 20   this btree */. 
133b1 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
133b2 20 30 3b 20 20 20 20 20 20 2f 2a 20 53 68 61 72   0;      /* Shar
133b3 65 64 20 70 61 72 74 20 6f 66 20 62 74 72 65 65  ed part of btree
133b4 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20   structure */.  
133b5 42 74 72 65 65 20 2a 70 3b 20 20 20 20 20 20 20  Btree *p;       
133b6 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64 6c          /* Handl
133b7 65 20 74 6f 20 72 65 74 75 72 6e 20 2a 2f 0a 20  e to return */. 
133b8 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
133b9 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 52 65 73 65  _OK;.  int nRese
133ba 72 76 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  rve;.  unsigned 
133bb 63 68 61 72 20 7a 44 62 48 65 61 64 65 72 5b 31  char zDbHeader[1
133bc 30 30 5d 3b 0a 0a 20 20 2f 2a 20 53 65 74 20 74  00];..  /* Set t
133bd 68 65 20 76 61 72 69 61 62 6c 65 20 69 73 4d 65  he variable isMe
133be 6d 64 62 20 74 6f 20 74 72 75 65 20 66 6f 72 20  mdb to true for 
133bf 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 64 61 74  an in-memory dat
133c0 61 62 61 73 65 2c 20 6f 72 20 0a 20 20 2a 2a 20  abase, or .  ** 
133c1 66 61 6c 73 65 20 66 6f 72 20 61 20 66 69 6c 65  false for a file
133c2 2d 62 61 73 65 64 20 64 61 74 61 62 61 73 65 2e  -based database.
133c3 20 54 68 69 73 20 73 79 6d 62 6f 6c 20 69 73 20   This symbol is 
133c4 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66  only required if
133c5 0a 20 20 2a 2a 20 65 69 74 68 65 72 20 6f 66 20  .  ** either of 
133c6 74 68 65 20 73 68 61 72 65 64 2d 64 61 74 61 20  the shared-data 
133c7 6f 72 20 61 75 74 6f 76 61 63 75 75 6d 20 66 65  or autovacuum fe
133c8 61 74 75 72 65 73 20 61 72 65 20 63 6f 6d 70 69  atures are compi
133c9 6c 65 64 20 0a 20 20 2a 2a 20 69 6e 74 6f 20 74  led .  ** into t
133ca 68 65 20 6c 69 62 72 61 72 79 2e 0a 20 20 2a 2f  he library..  */
133cb 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
133cc 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
133cd 5f 43 41 43 48 45 29 20 7c 7c 20 21 64 65 66 69  _CACHE) || !defi
133ce 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
133cf 41 55 54 4f 56 41 43 55 55 4d 29 0a 20 20 23 69  AUTOVACUUM).  #i
133d0 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
133d1 5f 4d 45 4d 4f 52 59 44 42 0a 20 20 20 20 63 6f  _MEMORYDB.    co
133d2 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d 64 62 20  nst int isMemdb 
133d3 3d 20 30 3b 0a 20 20 23 65 6c 73 65 0a 20 20 20  = 0;.  #else.   
133d4 20 63 6f 6e 73 74 20 69 6e 74 20 69 73 4d 65 6d   const int isMem
133d5 64 62 20 3d 20 7a 46 69 6c 65 6e 61 6d 65 20 26  db = zFilename &
133d6 26 20 21 73 74 72 63 6d 70 28 7a 46 69 6c 65 6e  & !strcmp(zFilen
133d7 61 6d 65 2c 20 22 3a 6d 65 6d 6f 72 79 3a 22 29  ame, ":memory:")
133d8 3b 0a 20 20 23 65 6e 64 69 66 0a 23 65 6e 64 69  ;.  #endif.#endi
133d9 66 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 21  f..  assert( db!
133da 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
133db 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
133dc 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  ld(db->mutex) );
133dd 0a 0a 20 20 70 56 66 73 20 3d 20 64 62 2d 3e 70  ..  pVfs = db->p
133de 56 66 73 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74  Vfs;.  p = sqlit
133df 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a  e3MallocZero(siz
133e0 65 6f 66 28 42 74 72 65 65 29 29 3b 0a 20 20 69  eof(Btree));.  i
133e1 66 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74  f( !p ){.    ret
133e2 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
133e3 3b 0a 20 20 7d 0a 20 20 70 2d 3e 69 6e 54 72 61  ;.  }.  p->inTra
133e4 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b  ns = TRANS_NONE;
133e5 0a 20 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 0a  .  p->db = db;..
133e6 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
133e7 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
133e8 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69 6e  CACHE) && !defin
133e9 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ed(SQLITE_OMIT_D
133ea 49 53 4b 49 4f 29 0a 20 20 2f 2a 0a 20 20 2a 2a  ISKIO).  /*.  **
133eb 20 49 66 20 74 68 69 73 20 42 74 72 65 65 20 69   If this Btree i
133ec 73 20 61 20 63 61 6e 64 69 64 61 74 65 20 66 6f  s a candidate fo
133ed 72 20 73 68 61 72 65 64 20 63 61 63 68 65 2c 20  r shared cache, 
133ee 74 72 79 20 74 6f 20 66 69 6e 64 20 61 6e 0a 20  try to find an. 
133ef 20 2a 2a 20 65 78 69 73 74 69 6e 67 20 42 74 53   ** existing BtS
133f0 68 61 72 65 64 20 6f 62 6a 65 63 74 20 74 68 61  hared object tha
133f1 74 20 77 65 20 63 61 6e 20 73 68 61 72 65 20 77  t we can share w
133f2 69 74 68 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  ith.  */.  if( i
133f3 73 4d 65 6d 64 62 3d 3d 30 0a 20 20 20 26 26 20  sMemdb==0.   && 
133f4 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c  (db->flags & SQL
133f5 49 54 45 5f 56 74 61 62 29 3d 3d 30 0a 20 20 20  ITE_Vtab)==0.   
133f6 26 26 20 7a 46 69 6c 65 6e 61 6d 65 20 26 26 20  && zFilename && 
133f7 7a 46 69 6c 65 6e 61 6d 65 5b 30 5d 0a 20 20 29  zFilename[0].  )
133f8 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
133f9 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 68  3GlobalConfig.sh
133fa 61 72 65 64 43 61 63 68 65 45 6e 61 62 6c 65 64  aredCacheEnabled
133fb 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 46   ){.      int nF
133fc 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 70 56  ullPathname = pV
133fd 66 73 2d 3e 6d 78 50 61 74 68 6e 61 6d 65 2b 31  fs->mxPathname+1
133fe 3b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46  ;.      char *zF
133ff 75 6c 6c 50 61 74 68 6e 61 6d 65 20 3d 20 73 71  ullPathname = sq
13400 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e 46 75 6c  lite3Malloc(nFul
13401 6c 50 61 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20  lPathname);.    
13402 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
13403 2a 6d 75 74 65 78 53 68 61 72 65 64 3b 0a 20 20  *mutexShared;.  
13404 20 20 20 20 70 2d 3e 73 68 61 72 61 62 6c 65 20      p->sharable 
13405 3d 20 31 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66  = 1;.      db->f
13406 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 53  lags |= SQLITE_S
13407 68 61 72 65 64 43 61 63 68 65 3b 0a 20 20 20 20  haredCache;.    
13408 20 20 69 66 28 20 21 7a 46 75 6c 6c 50 61 74 68    if( !zFullPath
13409 6e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20  name ){.        
1340a 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 29 3b  sqlite3_free(p);
1340b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
1340c 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
1340d 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
1340e 74 65 33 4f 73 46 75 6c 6c 50 61 74 68 6e 61 6d  te3OsFullPathnam
1340f 65 28 70 56 66 73 2c 20 7a 46 69 6c 65 6e 61 6d  e(pVfs, zFilenam
13410 65 2c 20 6e 46 75 6c 6c 50 61 74 68 6e 61 6d 65  e, nFullPathname
13411 2c 20 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 29  , zFullPathname)
13412 3b 0a 20 20 20 20 20 20 6d 75 74 65 78 53 68 61  ;.      mutexSha
13413 72 65 64 20 3d 20 73 71 6c 69 74 65 33 4d 75 74  red = sqlite3Mut
13414 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d  exAlloc(SQLITE_M
13415 55 54 45 58 5f 53 54 41 54 49 43 5f 4d 41 53 54  UTEX_STATIC_MAST
13416 45 52 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ER);.      sqlit
13417 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
13418 75 74 65 78 53 68 61 72 65 64 29 3b 0a 20 20 20  utexShared);.   
13419 20 20 20 66 6f 72 28 70 42 74 3d 47 4c 4f 42 41     for(pBt=GLOBA
1341a 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
1341b 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
1341c 73 74 29 3b 20 70 42 74 3b 20 70 42 74 3d 70 42  st); pBt; pBt=pB
1341d 74 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  t->pNext){.     
1341e 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
1341f 6e 52 65 66 3e 30 20 29 3b 0a 20 20 20 20 20 20  nRef>0 );.      
13420 20 20 69 66 28 20 30 3d 3d 73 74 72 63 6d 70 28    if( 0==strcmp(
13421 7a 46 75 6c 6c 50 61 74 68 6e 61 6d 65 2c 20 73  zFullPathname, s
13422 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65 6e  qlite3PagerFilen
13423 61 6d 65 28 70 42 74 2d 3e 70 50 61 67 65 72 29  ame(pBt->pPager)
13424 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ).              
13425 20 20 20 26 26 20 73 71 6c 69 74 65 33 50 61 67     && sqlite3Pag
13426 65 72 56 66 73 28 70 42 74 2d 3e 70 50 61 67 65  erVfs(pBt->pPage
13427 72 29 3d 3d 70 56 66 73 20 29 7b 0a 20 20 20 20  r)==pVfs ){.    
13428 20 20 20 20 20 20 70 2d 3e 70 42 74 20 3d 20 70        p->pBt = p
13429 42 74 3b 0a 20 20 20 20 20 20 20 20 20 20 70 42  Bt;.          pB
1342a 74 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20 20  t->nRef++;.     
1342b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1342c 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1342d 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1342e 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61  x_leave(mutexSha
1342f 72 65 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  red);.      sqli
13430 74 65 33 5f 66 72 65 65 28 7a 46 75 6c 6c 50 61  te3_free(zFullPa
13431 74 68 6e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23  thname);.    }.#
13432 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42  ifdef SQLITE_DEB
13433 55 47 0a 20 20 20 20 65 6c 73 65 7b 0a 20 20 20  UG.    else{.   
13434 20 20 20 2f 2a 20 49 6e 20 64 65 62 75 67 20 6d     /* In debug m
13435 6f 64 65 2c 20 77 65 20 6d 61 72 6b 20 61 6c 6c  ode, we mark all
13436 20 70 65 72 73 69 73 74 65 6e 74 20 64 61 74 61   persistent data
13437 62 61 73 65 73 20 61 73 20 73 68 61 72 61 62 6c  bases as sharabl
13438 65 0a 20 20 20 20 20 20 2a 2a 20 65 76 65 6e 20  e.      ** even 
13439 77 68 65 6e 20 74 68 65 79 20 61 72 65 20 6e 6f  when they are no
1343a 74 2e 20 20 54 68 69 73 20 65 78 65 72 63 69 73  t.  This exercis
1343b 65 73 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20 63  es the locking c
1343c 6f 64 65 20 61 6e 64 0a 20 20 20 20 20 20 2a 2a  ode and.      **
1343d 20 67 69 76 65 73 20 6d 6f 72 65 20 6f 70 70 6f   gives more oppo
1343e 72 74 75 6e 69 74 79 20 66 6f 72 20 61 73 73 65  rtunity for asse
1343f 72 74 73 28 73 71 6c 69 74 65 33 5f 6d 75 74 65  rts(sqlite3_mute
13440 78 5f 68 65 6c 64 28 29 29 0a 20 20 20 20 20 20  x_held()).      
13441 2a 2a 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f  ** statements to
13442 20 66 69 6e 64 20 6c 6f 63 6b 69 6e 67 20 70 72   find locking pr
13443 6f 62 6c 65 6d 73 2e 0a 20 20 20 20 20 20 2a 2f  oblems..      */
13444 0a 20 20 20 20 20 20 70 2d 3e 73 68 61 72 61 62  .      p->sharab
13445 6c 65 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 23 65  le = 1;.    }.#e
13446 6e 64 69 66 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ndif.  }.#endif.
13447 20 20 69 66 28 20 70 42 74 3d 3d 30 20 29 7b 0a    if( pBt==0 ){.
13448 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 68      /*.    ** Th
13449 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65  e following asse
1344a 72 74 73 20 6d 61 6b 65 20 73 75 72 65 20 74 68  rts make sure th
1344b 61 74 20 73 74 72 75 63 74 75 72 65 73 20 75 73  at structures us
1344c 65 64 20 62 79 20 74 68 65 20 62 74 72 65 65 20  ed by the btree 
1344d 61 72 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 72  are.    ** the r
1344e 69 67 68 74 20 73 69 7a 65 2e 20 20 54 68 69 73  ight size.  This
1344f 20 69 73 20 74 6f 20 67 75 61 72 64 20 61 67 61   is to guard aga
13450 69 6e 73 74 20 73 69 7a 65 20 63 68 61 6e 67 65  inst size change
13451 73 20 74 68 61 74 20 72 65 73 75 6c 74 0a 20 20  s that result.  
13452 20 20 2a 2a 20 77 68 65 6e 20 63 6f 6d 70 69 6c    ** when compil
13453 69 6e 67 20 6f 6e 20 61 20 64 69 66 66 65 72 65  ing on a differe
13454 6e 74 20 61 72 63 68 69 74 65 63 74 75 72 65 2e  nt architecture.
13455 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
13456 72 74 28 20 73 69 7a 65 6f 66 28 69 36 34 29 3d  rt( sizeof(i64)=
13457 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 69 36 34  =8 || sizeof(i64
13458 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65  )==4 );.    asse
13459 72 74 28 20 73 69 7a 65 6f 66 28 75 36 34 29 3d  rt( sizeof(u64)=
1345a 3d 38 20 7c 7c 20 73 69 7a 65 6f 66 28 75 36 34  =8 || sizeof(u64
1345b 29 3d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65  )==4 );.    asse
1345c 72 74 28 20 73 69 7a 65 6f 66 28 75 33 32 29 3d  rt( sizeof(u32)=
1345d 3d 34 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =4 );.    assert
1345e 28 20 73 69 7a 65 6f 66 28 75 31 36 29 3d 3d 32  ( sizeof(u16)==2
1345f 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
13460 73 69 7a 65 6f 66 28 50 67 6e 6f 29 3d 3d 34 20  sizeof(Pgno)==4 
13461 29 3b 0a 20 20 0a 20 20 20 20 70 42 74 20 3d 20  );.  .    pBt = 
13462 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
13463 6f 28 20 73 69 7a 65 6f 66 28 2a 70 42 74 29 20  o( sizeof(*pBt) 
13464 29 3b 0a 20 20 20 20 69 66 28 20 70 42 74 3d 3d  );.    if( pBt==
13465 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
13466 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
13467 20 20 20 20 67 6f 74 6f 20 62 74 72 65 65 5f 6f      goto btree_o
13468 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  pen_out;.    }. 
13469 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1346a 61 67 65 72 4f 70 65 6e 28 70 56 66 73 2c 20 26  agerOpen(pVfs, &
1346b 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 7a 46 69  pBt->pPager, zFi
1346c 6c 65 6e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20  lename,.        
1346d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1346e 20 20 45 58 54 52 41 5f 53 49 5a 45 2c 20 66 6c    EXTRA_SIZE, fl
1346f 61 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a  ags, vfsFlags);.
13470 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
13471 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72  TE_OK ){.      r
13472 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13473 52 65 61 64 46 69 6c 65 68 65 61 64 65 72 28 70  ReadFileheader(p
13474 42 74 2d 3e 70 50 61 67 65 72 2c 73 69 7a 65 6f  Bt->pPager,sizeo
13475 66 28 7a 44 62 48 65 61 64 65 72 29 2c 7a 44 62  f(zDbHeader),zDb
13476 48 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20  Header);.    }. 
13477 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13478 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
13479 74 6f 20 62 74 72 65 65 5f 6f 70 65 6e 5f 6f 75  to btree_open_ou
1347a 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  t;.    }.    sql
1347b 69 74 65 33 50 61 67 65 72 53 65 74 42 75 73 79  ite3PagerSetBusy
1347c 68 61 6e 64 6c 65 72 28 70 42 74 2d 3e 70 50 61  handler(pBt->pPa
1347d 67 65 72 2c 20 62 74 72 65 65 49 6e 76 6f 6b 65  ger, btreeInvoke
1347e 42 75 73 79 48 61 6e 64 6c 65 72 2c 20 70 42 74  BusyHandler, pBt
1347f 29 3b 0a 20 20 20 20 70 2d 3e 70 42 74 20 3d 20  );.    p->pBt = 
13480 70 42 74 3b 0a 20 20 0a 20 20 20 20 73 71 6c 69  pBt;.  .    sqli
13481 74 65 33 50 61 67 65 72 53 65 74 52 65 69 6e 69  te3PagerSetReini
13482 74 65 72 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ter(pBt->pPager,
13483 20 70 61 67 65 52 65 69 6e 69 74 29 3b 0a 20 20   pageReinit);.  
13484 20 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 20 3d    pBt->pCursor =
13485 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e 70 50 61   0;.    pBt->pPa
13486 67 65 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74  ge1 = 0;.    pBt
13487 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 73 71 6c  ->readOnly = sql
13488 69 74 65 33 50 61 67 65 72 49 73 72 65 61 64 6f  ite3PagerIsreado
13489 6e 6c 79 28 70 42 74 2d 3e 70 50 61 67 65 72 29  nly(pBt->pPager)
1348a 3b 0a 20 20 20 20 70 42 74 2d 3e 70 61 67 65 53  ;.    pBt->pageS
1348b 69 7a 65 20 3d 20 67 65 74 32 62 79 74 65 28 26  ize = get2byte(&
1348c 7a 44 62 48 65 61 64 65 72 5b 31 36 5d 29 3b 0a  zDbHeader[16]);.
1348d 20 20 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67      if( pBt->pag
1348e 65 53 69 7a 65 3c 35 31 32 20 7c 7c 20 70 42 74  eSize<512 || pBt
1348f 2d 3e 70 61 67 65 53 69 7a 65 3e 53 51 4c 49 54  ->pageSize>SQLIT
13490 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a 45 0a  E_MAX_PAGE_SIZE.
13491 20 20 20 20 20 20 20 20 20 7c 7c 20 28 28 70 42           || ((pB
13492 74 2d 3e 70 61 67 65 53 69 7a 65 2d 31 29 26 70  t->pageSize-1)&p
13493 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 21 3d 30  Bt->pageSize)!=0
13494 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70   ){.      pBt->p
13495 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20  ageSize = 0;.   
13496 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 53     sqlite3PagerS
13497 65 74 50 61 67 65 73 69 7a 65 28 70 42 74 2d 3e  etPagesize(pBt->
13498 70 50 61 67 65 72 2c 20 26 70 42 74 2d 3e 70 61  pPager, &pBt->pa
13499 67 65 53 69 7a 65 29 3b 0a 23 69 66 6e 64 65 66  geSize);.#ifndef
1349a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1349b 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20 2f 2a  OVACUUM.      /*
1349c 20 49 66 20 74 68 65 20 6d 61 67 69 63 20 6e 61   If the magic na
1349d 6d 65 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 77 69  me ":memory:" wi
1349e 6c 6c 20 63 72 65 61 74 65 20 61 6e 20 69 6e 2d  ll create an in-
1349f 6d 65 6d 6f 72 79 20 64 61 74 61 62 61 73 65 2c  memory database,
134a0 20 74 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 6c   then.      ** l
134a1 65 61 76 65 20 74 68 65 20 61 75 74 6f 56 61 63  eave the autoVac
134a2 75 75 6d 20 6d 6f 64 65 20 61 74 20 30 20 28 64  uum mode at 0 (d
134a3 6f 20 6e 6f 74 20 61 75 74 6f 2d 76 61 63 75 75  o not auto-vacuu
134a4 6d 29 2c 20 65 76 65 6e 20 69 66 0a 20 20 20 20  m), even if.    
134a5 20 20 2a 2a 20 53 51 4c 49 54 45 5f 44 45 46 41    ** SQLITE_DEFA
134a6 55 4c 54 5f 41 55 54 4f 56 41 43 55 55 4d 20 69  ULT_AUTOVACUUM i
134a7 73 20 74 72 75 65 2e 20 4f 6e 20 74 68 65 20 6f  s true. On the o
134a8 74 68 65 72 20 68 61 6e 64 2c 20 69 66 0a 20 20  ther hand, if.  
134a9 20 20 20 20 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d      ** SQLITE_OM
134aa 49 54 5f 4d 45 4d 4f 52 59 44 42 20 68 61 73 20  IT_MEMORYDB has 
134ab 62 65 65 6e 20 64 65 66 69 6e 65 64 2c 20 74 68  been defined, th
134ac 65 6e 20 22 3a 6d 65 6d 6f 72 79 3a 22 20 69 73  en ":memory:" is
134ad 20 6a 75 73 74 20 61 0a 20 20 20 20 20 20 2a 2a   just a.      **
134ae 20 72 65 67 75 6c 61 72 20 66 69 6c 65 2d 6e 61   regular file-na
134af 6d 65 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65  me. In this case
134b0 20 74 68 65 20 61 75 74 6f 2d 76 61 63 75 75 6d   the auto-vacuum
134b1 20 61 70 70 6c 69 65 73 20 61 73 20 70 65 72 20   applies as per 
134b2 6e 6f 72 6d 61 6c 2e 0a 20 20 20 20 20 20 2a 2f  normal..      */
134b3 0a 20 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65  .      if( zFile
134b4 6e 61 6d 65 20 26 26 20 21 69 73 4d 65 6d 64 62  name && !isMemdb
134b5 20 29 7b 0a 20 20 20 20 20 20 20 20 70 42 74 2d   ){.        pBt-
134b6 3e 61 75 74 6f 56 61 63 75 75 6d 20 3d 20 28 53  >autoVacuum = (S
134b7 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55  QLITE_DEFAULT_AU
134b8 54 4f 56 41 43 55 55 4d 20 3f 20 31 20 3a 20 30  TOVACUUM ? 1 : 0
134b9 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d 3e  );.        pBt->
134ba 69 6e 63 72 56 61 63 75 75 6d 20 3d 20 28 53 51  incrVacuum = (SQ
134bb 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f 41 55 54  LITE_DEFAULT_AUT
134bc 4f 56 41 43 55 55 4d 3d 3d 32 20 3f 20 31 20 3a  OVACUUM==2 ? 1 :
134bd 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e   0);.      }.#en
134be 64 69 66 0a 20 20 20 20 20 20 6e 52 65 73 65 72  dif.      nReser
134bf 76 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73  ve = 0;.    }els
134c0 65 7b 0a 20 20 20 20 20 20 6e 52 65 73 65 72 76  e{.      nReserv
134c1 65 20 3d 20 7a 44 62 48 65 61 64 65 72 5b 32 30  e = zDbHeader[20
134c2 5d 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 70 61  ];.      pBt->pa
134c3 67 65 53 69 7a 65 46 69 78 65 64 20 3d 20 31 3b  geSizeFixed = 1;
134c4 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
134c5 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
134c6 20 20 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56        pBt->autoV
134c7 61 63 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74  acuum = (get4byt
134c8 65 28 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20  e(&zDbHeader[36 
134c9 2b 20 34 2a 34 5d 29 3f 31 3a 30 29 3b 0a 20 20  + 4*4])?1:0);.  
134ca 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56 61 63      pBt->incrVac
134cb 75 75 6d 20 3d 20 28 67 65 74 34 62 79 74 65 28  uum = (get4byte(
134cc 26 7a 44 62 48 65 61 64 65 72 5b 33 36 20 2b 20  &zDbHeader[36 + 
134cd 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e 64  7*4])?1:0);.#end
134ce 69 66 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  if.    }.    pBt
134cf 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20 70  ->usableSize = p
134d0 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 2d 20 6e  Bt->pageSize - n
134d1 52 65 73 65 72 76 65 3b 0a 20 20 20 20 61 73 73  Reserve;.    ass
134d2 65 72 74 28 20 28 70 42 74 2d 3e 70 61 67 65 53  ert( (pBt->pageS
134d3 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 20 20  ize & 7)==0 );  
134d4 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e 6d  /* 8-byte alignm
134d5 65 6e 74 20 6f 66 20 70 61 67 65 53 69 7a 65 20  ent of pageSize 
134d6 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61  */.    sqlite3Pa
134d7 67 65 72 53 65 74 50 61 67 65 73 69 7a 65 28 70  gerSetPagesize(p
134d8 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 70 42 74  Bt->pPager, &pBt
134d9 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 20 20 20  ->pageSize);.   
134da 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
134db 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44  LITE_OMIT_SHARED
134dc 5f 43 41 43 48 45 29 20 26 26 20 21 64 65 66 69  _CACHE) && !defi
134dd 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
134de 44 49 53 4b 49 4f 29 0a 20 20 20 20 2f 2a 20 41  DISKIO).    /* A
134df 64 64 20 74 68 65 20 6e 65 77 20 42 74 53 68 61  dd the new BtSha
134e0 72 65 64 20 6f 62 6a 65 63 74 20 74 6f 20 74 68  red object to th
134e1 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 73 68  e linked list sh
134e2 61 72 61 62 6c 65 20 42 74 53 68 61 72 65 64 73  arable BtShareds
134e3 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
134e4 20 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a   p->sharable ){.
134e5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75        sqlite3_mu
134e6 74 65 78 20 2a 6d 75 74 65 78 53 68 61 72 65 64  tex *mutexShared
134e7 3b 0a 20 20 20 20 20 20 70 42 74 2d 3e 6e 52 65  ;.      pBt->nRe
134e8 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 6d 75 74  f = 1;.      mut
134e9 65 78 53 68 61 72 65 64 20 3d 20 73 71 6c 69 74  exShared = sqlit
134ea 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c  e3MutexAlloc(SQL
134eb 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43  ITE_MUTEX_STATIC
134ec 5f 4d 41 53 54 45 52 29 3b 0a 20 20 20 20 20 20  _MASTER);.      
134ed 69 66 28 20 53 51 4c 49 54 45 5f 54 48 52 45 41  if( SQLITE_THREA
134ee 44 53 41 46 45 20 26 26 20 73 71 6c 69 74 65 33  DSAFE && sqlite3
134ef 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f  GlobalConfig.bCo
134f0 72 65 4d 75 74 65 78 20 29 7b 0a 20 20 20 20 20  reMutex ){.     
134f1 20 20 20 70 42 74 2d 3e 6d 75 74 65 78 20 3d 20     pBt->mutex = 
134f2 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
134f3 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 46  c(SQLITE_MUTEX_F
134f4 41 53 54 29 3b 0a 20 20 20 20 20 20 20 20 69 66  AST);.        if
134f5 28 20 70 42 74 2d 3e 6d 75 74 65 78 3d 3d 30 20  ( pBt->mutex==0 
134f6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
134f7 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
134f8 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
134f9 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a  llocFailed = 0;.
134fa 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62            goto b
134fb 74 72 65 65 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20  tree_open_out;. 
134fc 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
134fd 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  .      sqlite3_m
134fe 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
134ff 53 68 61 72 65 64 29 3b 0a 20 20 20 20 20 20 70  Shared);.      p
13500 42 74 2d 3e 70 4e 65 78 74 20 3d 20 47 4c 4f 42  Bt->pNext = GLOB
13501 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
13502 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
13503 69 73 74 29 3b 0a 20 20 20 20 20 20 47 4c 4f 42  ist);.      GLOB
13504 41 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c  AL(BtShared*,sql
13505 69 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c  ite3SharedCacheL
13506 69 73 74 29 20 3d 20 70 42 74 3b 0a 20 20 20 20  ist) = pBt;.    
13507 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
13508 6c 65 61 76 65 28 6d 75 74 65 78 53 68 61 72 65  leave(mutexShare
13509 64 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  d);.    }.#endif
1350a 0a 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e  .  }..#if !defin
1350b 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
1350c 48 41 52 45 44 5f 43 41 43 48 45 29 20 26 26 20  HARED_CACHE) && 
1350d 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1350e 4f 4d 49 54 5f 44 49 53 4b 49 4f 29 0a 20 20 2f  OMIT_DISKIO).  /
1350f 2a 20 49 66 20 74 68 65 20 6e 65 77 20 42 74 72  * If the new Btr
13510 65 65 20 75 73 65 73 20 61 20 73 68 61 72 61 62  ee uses a sharab
13511 6c 65 20 70 42 74 53 68 61 72 65 64 2c 20 74 68  le pBtShared, th
13512 65 6e 20 6c 69 6e 6b 20 74 68 65 20 6e 65 77 0a  en link the new.
13513 20 20 2a 2a 20 42 74 72 65 65 20 69 6e 74 6f 20    ** Btree into 
13514 74 68 65 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20  the list of all 
13515 73 68 61 72 61 62 6c 65 20 42 74 72 65 65 73 20  sharable Btrees 
13516 66 6f 72 20 74 68 65 20 73 61 6d 65 20 63 6f 6e  for the same con
13517 6e 65 63 74 69 6f 6e 2e 0a 20 20 2a 2a 20 54 68  nection..  ** Th
13518 65 20 6c 69 73 74 20 69 73 20 6b 65 70 74 20 69  e list is kept i
13519 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65  n ascending orde
1351a 72 20 62 79 20 70 42 74 20 61 64 64 72 65 73 73  r by pBt address
1351b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
1351c 73 68 61 72 61 62 6c 65 20 29 7b 0a 20 20 20 20  sharable ){.    
1351d 69 6e 74 20 69 3b 0a 20 20 20 20 42 74 72 65 65  int i;.    Btree
1351e 20 2a 70 53 69 62 3b 0a 20 20 20 20 66 6f 72 28   *pSib;.    for(
1351f 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
13520 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
13521 28 70 53 69 62 20 3d 20 64 62 2d 3e 61 44 62 5b  (pSib = db->aDb[
13522 69 5d 2e 70 42 74 29 21 3d 30 20 26 26 20 70 53  i].pBt)!=0 && pS
13523 69 62 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a  ib->sharable ){.
13524 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70          while( p
13525 53 69 62 2d 3e 70 50 72 65 76 20 29 7b 20 70 53  Sib->pPrev ){ pS
13526 69 62 20 3d 20 70 53 69 62 2d 3e 70 50 72 65 76  ib = pSib->pPrev
13527 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20  ; }.        if( 
13528 70 2d 3e 70 42 74 3c 70 53 69 62 2d 3e 70 42 74  p->pBt<pSib->pBt
13529 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d   ){.          p-
1352a 3e 70 4e 65 78 74 20 3d 20 70 53 69 62 3b 0a 20  >pNext = pSib;. 
1352b 20 20 20 20 20 20 20 20 20 70 2d 3e 70 50 72 65           p->pPre
1352c 76 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20  v = 0;.         
1352d 20 70 53 69 62 2d 3e 70 50 72 65 76 20 3d 20 70   pSib->pPrev = p
1352e 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1352f 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c 65  .          while
13530 28 20 70 53 69 62 2d 3e 70 4e 65 78 74 20 26 26  ( pSib->pNext &&
13531 20 70 53 69 62 2d 3e 70 4e 65 78 74 2d 3e 70 42   pSib->pNext->pB
13532 74 3c 70 2d 3e 70 42 74 20 29 7b 0a 20 20 20 20  t<p->pBt ){.    
13533 20 20 20 20 20 20 20 20 70 53 69 62 20 3d 20 70          pSib = p
13534 53 69 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  Sib->pNext;.    
13535 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
13536 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 70 53 69    p->pNext = pSi
13537 62 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  b->pNext;.      
13538 20 20 20 20 70 2d 3e 70 50 72 65 76 20 3d 20 70      p->pPrev = p
13539 53 69 62 3b 0a 20 20 20 20 20 20 20 20 20 20 69  Sib;.          i
1353a 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a 20  f( p->pNext ){. 
1353b 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 70 4e             p->pN
1353c 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70 3b 0a  ext->pPrev = p;.
1353d 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1353e 20 20 20 20 20 20 70 53 69 62 2d 3e 70 4e 65 78        pSib->pNex
1353f 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20 20 7d  t = p;.        }
13540 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
13541 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13542 7d 0a 23 65 6e 64 69 66 0a 20 20 2a 70 70 42 74  }.#endif.  *ppBt
13543 72 65 65 20 3d 20 70 3b 0a 0a 62 74 72 65 65 5f  ree = p;..btree_
13544 6f 70 65 6e 5f 6f 75 74 3a 0a 20 20 69 66 28 20  open_out:.  if( 
13545 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13546 0a 20 20 20 20 69 66 28 20 70 42 74 20 26 26 20  .    if( pBt && 
13547 70 42 74 2d 3e 70 50 61 67 65 72 20 29 7b 0a 20  pBt->pPager ){. 
13548 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65       sqlite3Page
13549 72 43 6c 6f 73 65 28 70 42 74 2d 3e 70 50 61 67  rClose(pBt->pPag
1354a 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  er);.    }.    s
1354b 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29  qlite3_free(pBt)
1354c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  ;.    sqlite3_fr
1354d 65 65 28 70 29 3b 0a 20 20 20 20 2a 70 70 42 74  ee(p);.    *ppBt
1354e 72 65 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72  ree = 0;.  }.  r
1354f 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
13550 2a 2a 20 44 65 63 72 65 6d 65 6e 74 20 74 68 65  ** Decrement the
13551 20 42 74 53 68 61 72 65 64 2e 6e 52 65 66 20 63   BtShared.nRef c
13552 6f 75 6e 74 65 72 2e 20 20 57 68 65 6e 20 69 74  ounter.  When it
13553 20 72 65 61 63 68 65 73 20 7a 65 72 6f 2c 0a 2a   reaches zero,.*
13554 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 42 74 53  * remove the BtS
13555 68 61 72 65 64 20 73 74 72 75 63 74 75 72 65 20  hared structure 
13556 66 72 6f 6d 20 74 68 65 20 73 68 61 72 69 6e 67  from the sharing
13557 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 0a 2a   list.  Return.*
13558 2a 20 74 72 75 65 20 69 66 20 74 68 65 20 42 74  * true if the Bt
13559 53 68 61 72 65 64 2e 6e 52 65 66 20 63 6f 75 6e  Shared.nRef coun
1355a 74 65 72 20 72 65 61 63 68 65 73 20 7a 65 72 6f  ter reaches zero
1355b 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a 20 66   and return.** f
1355c 61 6c 73 65 20 69 66 20 69 74 20 69 73 20 73 74  alse if it is st
1355d 69 6c 6c 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2f  ill positive..*/
1355e 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 6d 6f  .static int remo
1355f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67 4c 69 73  veFromSharingLis
13560 74 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  t(BtShared *pBt)
13561 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  {.#ifndef SQLITE
13562 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
13563 48 45 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  HE.  sqlite3_mut
13564 65 78 20 2a 70 4d 61 73 74 65 72 3b 0a 20 20 42  ex *pMaster;.  B
13565 74 53 68 61 72 65 64 20 2a 70 4c 69 73 74 3b 0a  tShared *pList;.
13566 20 20 69 6e 74 20 72 65 6d 6f 76 65 64 20 3d 20    int removed = 
13567 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  0;..  assert( sq
13568 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6e 6f 74 68  lite3_mutex_noth
13569 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
1356a 29 3b 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73  );.  pMaster = s
1356b 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
1356c 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
1356d 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20  ATIC_MASTER);.  
1356e 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
1356f 74 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20  ter(pMaster);.  
13570 70 42 74 2d 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69  pBt->nRef--;.  i
13571 66 28 20 70 42 74 2d 3e 6e 52 65 66 3c 3d 30 20  f( pBt->nRef<=0 
13572 29 7b 0a 20 20 20 20 69 66 28 20 47 4c 4f 42 41  ){.    if( GLOBA
13573 4c 28 42 74 53 68 61 72 65 64 2a 2c 73 71 6c 69  L(BtShared*,sqli
13574 74 65 33 53 68 61 72 65 64 43 61 63 68 65 4c 69  te3SharedCacheLi
13575 73 74 29 3d 3d 70 42 74 20 29 7b 0a 20 20 20 20  st)==pBt ){.    
13576 20 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65    GLOBAL(BtShare
13577 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
13578 43 61 63 68 65 4c 69 73 74 29 20 3d 20 70 42 74  CacheList) = pBt
13579 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c  ->pNext;.    }el
1357a 73 65 7b 0a 20 20 20 20 20 20 70 4c 69 73 74 20  se{.      pList 
1357b 3d 20 47 4c 4f 42 41 4c 28 42 74 53 68 61 72 65  = GLOBAL(BtShare
1357c 64 2a 2c 73 71 6c 69 74 65 33 53 68 61 72 65 64  d*,sqlite3Shared
1357d 43 61 63 68 65 4c 69 73 74 29 3b 0a 20 20 20 20  CacheList);.    
1357e 20 20 77 68 69 6c 65 28 20 41 4c 57 41 59 53 28    while( ALWAYS(
1357f 70 4c 69 73 74 29 20 26 26 20 70 4c 69 73 74 2d  pList) && pList-
13580 3e 70 4e 65 78 74 21 3d 70 42 74 20 29 7b 0a 20  >pNext!=pBt ){. 
13581 20 20 20 20 20 20 20 70 4c 69 73 74 3d 70 4c 69         pList=pLi
13582 73 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20  st->pNext;.     
13583 20 7d 0a 20 20 20 20 20 20 69 66 28 20 41 4c 57   }.      if( ALW
13584 41 59 53 28 70 4c 69 73 74 29 20 29 7b 0a 20 20  AYS(pList) ){.  
13585 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 70 4e 65        pList->pNe
13586 78 74 20 3d 20 70 42 74 2d 3e 70 4e 65 78 74 3b  xt = pBt->pNext;
13587 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
13588 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 54 48     if( SQLITE_TH
13589 52 45 41 44 53 41 46 45 20 29 7b 0a 20 20 20 20  READSAFE ){.    
1358a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1358b 66 72 65 65 28 70 42 74 2d 3e 6d 75 74 65 78 29  free(pBt->mutex)
1358c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 6d 6f  ;.    }.    remo
1358d 76 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 73  ved = 1;.  }.  s
1358e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
1358f 76 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 72  ve(pMaster);.  r
13590 65 74 75 72 6e 20 72 65 6d 6f 76 65 64 3b 0a 23  eturn removed;.#
13591 65 6c 73 65 0a 20 20 72 65 74 75 72 6e 20 31 3b  else.  return 1;
13592 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a  .#endif.}../*.**
13593 20 4d 61 6b 65 20 73 75 72 65 20 70 42 74 2d 3e   Make sure pBt->
13594 70 54 6d 70 53 70 61 63 65 20 70 6f 69 6e 74 73  pTmpSpace points
13595 20 74 6f 20 61 6e 20 61 6c 6c 6f 63 61 74 69 6f   to an allocatio
13596 6e 20 6f 66 20 0a 2a 2a 20 4d 58 5f 43 45 4c 4c  n of .** MX_CELL
13597 5f 53 49 5a 45 28 70 42 74 29 20 62 79 74 65 73  _SIZE(pBt) bytes
13598 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
13599 20 61 6c 6c 6f 63 61 74 65 54 65 6d 70 53 70 61   allocateTempSpa
1359a 63 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74  ce(BtShared *pBt
1359b 29 7b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 70  ){.  if( !pBt->p
1359c 54 6d 70 53 70 61 63 65 20 29 7b 0a 20 20 20 20  TmpSpace ){.    
1359d 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 20 3d  pBt->pTmpSpace =
1359e 20 73 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c   sqlite3PageMall
1359f 6f 63 28 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  oc( pBt->pageSiz
135a0 65 20 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  e );.  }.}../*.*
135a1 2a 20 46 72 65 65 20 74 68 65 20 70 42 74 2d 3e  * Free the pBt->
135a2 70 54 6d 70 53 70 61 63 65 20 61 6c 6c 6f 63 61  pTmpSpace alloca
135a3 74 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  tion.*/.static v
135a4 6f 69 64 20 66 72 65 65 54 65 6d 70 53 70 61 63  oid freeTempSpac
135a5 65 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  e(BtShared *pBt)
135a6 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 67 65 46  {.  sqlite3PageF
135a7 72 65 65 28 20 70 42 74 2d 3e 70 54 6d 70 53 70  ree( pBt->pTmpSp
135a8 61 63 65 29 3b 0a 20 20 70 42 74 2d 3e 70 54 6d  ace);.  pBt->pTm
135a9 70 53 70 61 63 65 20 3d 20 30 3b 0a 7d 0a 0a 2f  pSpace = 0;.}../
135aa 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 6e 20 6f 70  *.** Close an op
135ab 65 6e 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  en database and 
135ac 69 6e 76 61 6c 69 64 61 74 65 20 61 6c 6c 20 63  invalidate all c
135ad 75 72 73 6f 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ursors..*/.SQLIT
135ae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
135af 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
135b0 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42 74 53  Btree *p){.  BtS
135b1 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
135b2 70 42 74 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  pBt;.  BtCursor 
135b3 2a 70 43 75 72 3b 0a 0a 20 20 2f 2a 20 43 6c 6f  *pCur;..  /* Clo
135b4 73 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 6f  se all cursors o
135b5 70 65 6e 65 64 20 76 69 61 20 74 68 69 73 20 68  pened via this h
135b6 61 6e 64 6c 65 2e 20 20 2a 2f 0a 20 20 61 73 73  andle.  */.  ass
135b7 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
135b8 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d 3e 6d  ex_held(p->db->m
135b9 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
135ba 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
135bb 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e  .  pBt->db = p->
135bc 64 62 3b 0a 20 20 70 43 75 72 20 3d 20 70 42 74  db;.  pCur = pBt
135bd 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 77 68 69  ->pCursor;.  whi
135be 6c 65 28 20 70 43 75 72 20 29 7b 0a 20 20 20 20  le( pCur ){.    
135bf 42 74 43 75 72 73 6f 72 20 2a 70 54 6d 70 20 3d  BtCursor *pTmp =
135c0 20 70 43 75 72 3b 0a 20 20 20 20 70 43 75 72 20   pCur;.    pCur 
135c1 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b 0a 20  = pCur->pNext;. 
135c2 20 20 20 69 66 28 20 70 54 6d 70 2d 3e 70 42 74     if( pTmp->pBt
135c3 72 65 65 3d 3d 70 20 29 7b 0a 20 20 20 20 20 20  ree==p ){.      
135c4 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73  sqlite3BtreeClos
135c5 65 43 75 72 73 6f 72 28 70 54 6d 70 29 3b 0a 20  eCursor(pTmp);. 
135c6 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52     }.  }..  /* R
135c7 6f 6c 6c 62 61 63 6b 20 61 6e 79 20 61 63 74 69  ollback any acti
135c8 76 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ve transaction a
135c9 6e 64 20 66 72 65 65 20 74 68 65 20 68 61 6e 64  nd free the hand
135ca 6c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20  le structure..  
135cb 2a 2a 20 54 68 65 20 63 61 6c 6c 20 74 6f 20 73  ** The call to s
135cc 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
135cd 61 63 6b 28 29 20 64 72 6f 70 73 20 61 6e 79 20  ack() drops any 
135ce 74 61 62 6c 65 2d 6c 6f 63 6b 73 20 68 65 6c 64  table-locks held
135cf 20 62 79 0a 20 20 2a 2a 20 74 68 69 73 20 68 61   by.  ** this ha
135d0 6e 64 6c 65 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c  ndle..  */.  sql
135d1 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61 63  ite3BtreeRollbac
135d2 6b 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  k(p);.  sqlite3B
135d3 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 0a 20  treeLeave(p);.. 
135d4 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
135d5 20 73 74 69 6c 6c 20 6f 74 68 65 72 20 6f 75 74   still other out
135d6 73 74 61 6e 64 69 6e 67 20 72 65 66 65 72 65 6e  standing referen
135d7 63 65 73 20 74 6f 20 74 68 65 20 73 68 61 72 65  ces to the share
135d8 64 2d 62 74 72 65 65 0a 20 20 2a 2a 20 73 74 72  d-btree.  ** str
135d9 75 63 74 75 72 65 2c 20 72 65 74 75 72 6e 20 6e  ucture, return n
135da 6f 77 2e 20 54 68 65 20 72 65 6d 61 69 6e 64 65  ow. The remainde
135db 72 20 6f 66 20 74 68 69 73 20 70 72 6f 63 65 64  r of this proced
135dc 75 72 65 20 63 6c 65 61 6e 73 20 0a 20 20 2a 2a  ure cleans .  **
135dd 20 75 70 20 74 68 65 20 73 68 61 72 65 64 2d 62   up the shared-b
135de 74 72 65 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  tree..  */.  ass
135df 65 72 74 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f  ert( p->wantToLo
135e0 63 6b 3d 3d 30 20 26 26 20 70 2d 3e 6c 6f 63 6b  ck==0 && p->lock
135e1 65 64 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 21  ed==0 );.  if( !
135e2 70 2d 3e 73 68 61 72 61 62 6c 65 20 7c 7c 20 72  p->sharable || r
135e3 65 6d 6f 76 65 46 72 6f 6d 53 68 61 72 69 6e 67  emoveFromSharing
135e4 4c 69 73 74 28 70 42 74 29 20 29 7b 0a 20 20 20  List(pBt) ){.   
135e5 20 2f 2a 20 54 68 65 20 70 42 74 20 69 73 20 6e   /* The pBt is n
135e6 6f 20 6c 6f 6e 67 65 72 20 6f 6e 20 74 68 65 20  o longer on the 
135e7 73 68 61 72 69 6e 67 20 6c 69 73 74 2c 20 73 6f  sharing list, so
135e8 20 77 65 20 63 61 6e 20 61 63 63 65 73 73 0a 20   we can access. 
135e9 20 20 20 2a 2a 20 69 74 20 77 69 74 68 6f 75 74     ** it without
135ea 20 68 61 76 69 6e 67 20 74 6f 20 68 6f 6c 64 20   having to hold 
135eb 74 68 65 20 6d 75 74 65 78 2e 0a 20 20 20 20 2a  the mutex..    *
135ec 2a 0a 20 20 20 20 2a 2a 20 43 6c 65 61 6e 20 6f  *.    ** Clean o
135ed 75 74 20 61 6e 64 20 64 65 6c 65 74 65 20 74 68  ut and delete th
135ee 65 20 42 74 53 68 61 72 65 64 20 6f 62 6a 65 63  e BtShared objec
135ef 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  t..    */.    as
135f0 73 65 72 74 28 20 21 70 42 74 2d 3e 70 43 75 72  sert( !pBt->pCur
135f1 73 6f 72 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  sor );.    sqlit
135f2 65 33 50 61 67 65 72 43 6c 6f 73 65 28 70 42 74  e3PagerClose(pBt
135f3 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20 20 69  ->pPager);.    i
135f4 66 28 20 70 42 74 2d 3e 78 46 72 65 65 53 63 68  f( pBt->xFreeSch
135f5 65 6d 61 20 26 26 20 70 42 74 2d 3e 70 53 63 68  ema && pBt->pSch
135f6 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 70 42 74  ema ){.      pBt
135f7 2d 3e 78 46 72 65 65 53 63 68 65 6d 61 28 70 42  ->xFreeSchema(pB
135f8 74 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  t->pSchema);.   
135f9 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66   }.    sqlite3_f
135fa 72 65 65 28 70 42 74 2d 3e 70 53 63 68 65 6d 61  ree(pBt->pSchema
135fb 29 3b 0a 20 20 20 20 66 72 65 65 54 65 6d 70 53  );.    freeTempS
135fc 70 61 63 65 28 70 42 74 29 3b 0a 20 20 20 20 73  pace(pBt);.    s
135fd 71 6c 69 74 65 33 5f 66 72 65 65 28 70 42 74 29  qlite3_free(pBt)
135fe 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53  ;.  }..#ifndef S
135ff 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45  QLITE_OMIT_SHARE
13600 44 5f 43 41 43 48 45 0a 20 20 61 73 73 65 72 74  D_CACHE.  assert
13601 28 20 70 2d 3e 77 61 6e 74 54 6f 4c 6f 63 6b 3d  ( p->wantToLock=
13602 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
13603 70 2d 3e 6c 6f 63 6b 65 64 3d 3d 30 20 29 3b 0a  p->locked==0 );.
13604 20 20 69 66 28 20 70 2d 3e 70 50 72 65 76 20 29    if( p->pPrev )
13605 20 70 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74   p->pPrev->pNext
13606 20 3d 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 69   = p->pNext;.  i
13607 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 20 70 2d  f( p->pNext ) p-
13608 3e 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20  >pNext->pPrev = 
13609 70 2d 3e 70 50 72 65 76 3b 0a 23 65 6e 64 69 66  p->pPrev;.#endif
1360a 0a 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ..  sqlite3_free
1360b 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  (p);.  return SQ
1360c 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1360d 2a 20 43 68 61 6e 67 65 20 74 68 65 20 6c 69 6d  * Change the lim
1360e 69 74 20 6f 6e 20 74 68 65 20 6e 75 6d 62 65 72  it on the number
1360f 20 6f 66 20 70 61 67 65 73 20 61 6c 6c 6f 77 65   of pages allowe
13610 64 20 69 6e 20 74 68 65 20 63 61 63 68 65 2e 0a  d in the cache..
13611 2a 2a 0a 2a 2a 20 54 68 65 20 6d 61 78 69 6d 75  **.** The maximu
13612 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 63 68  m number of cach
13613 65 20 70 61 67 65 73 20 69 73 20 73 65 74 20 74  e pages is set t
13614 6f 20 74 68 65 20 61 62 73 6f 6c 75 74 65 0a 2a  o the absolute.*
13615 2a 20 76 61 6c 75 65 20 6f 66 20 6d 78 50 61 67  * value of mxPag
13616 65 2e 20 20 49 66 20 6d 78 50 61 67 65 20 69 73  e.  If mxPage is
13617 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65 20 70   negative, the p
13618 61 67 65 72 20 77 69 6c 6c 0a 2a 2a 20 6f 70 65  ager will.** ope
13619 72 61 74 65 20 61 73 79 6e 63 68 72 6f 6e 6f 75  rate asynchronou
1361a 73 6c 79 20 2d 20 69 74 20 77 69 6c 6c 20 6e 6f  sly - it will no
1361b 74 20 73 74 6f 70 20 74 6f 20 64 6f 20 66 73 79  t stop to do fsy
1361c 6e 63 28 29 73 0a 2a 2a 20 74 6f 20 69 6e 73 75  nc()s.** to insu
1361d 72 65 20 64 61 74 61 20 69 73 20 77 72 69 74 74  re data is writt
1361e 65 6e 20 74 6f 20 74 68 65 20 64 69 73 6b 20 73  en to the disk s
1361f 75 72 66 61 63 65 20 62 65 66 6f 72 65 0a 2a 2a  urface before.**
13620 20 63 6f 6e 74 69 6e 75 69 6e 67 2e 20 20 54 72   continuing.  Tr
13621 61 6e 73 61 63 74 69 6f 6e 73 20 73 74 69 6c 6c  ansactions still
13622 20 77 6f 72 6b 20 69 66 20 73 79 6e 63 68 72 6f   work if synchro
13623 6e 6f 75 73 20 69 73 20 6f 66 66 2c 0a 2a 2a 20  nous is off,.** 
13624 61 6e 64 20 74 68 65 20 64 61 74 61 62 61 73 65  and the database
13625 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 72 72 75   cannot be corru
13626 70 74 65 64 20 69 66 20 74 68 69 73 20 70 72 6f  pted if this pro
13627 67 72 61 6d 0a 2a 2a 20 63 72 61 73 68 65 73 2e  gram.** crashes.
13628 20 20 42 75 74 20 69 66 20 74 68 65 20 6f 70 65    But if the ope
13629 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 63 72  rating system cr
1362a 61 73 68 65 73 20 6f 72 20 74 68 65 72 65 20 69  ashes or there i
1362b 73 0a 2a 2a 20 61 6e 20 61 62 72 75 70 74 20 70  s.** an abrupt p
1362c 6f 77 65 72 20 66 61 69 6c 75 72 65 20 77 68 65  ower failure whe
1362d 6e 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 69 73  n synchronous is
1362e 20 6f 66 66 2c 20 74 68 65 20 64 61 74 61 62 61   off, the databa
1362f 73 65 0a 2a 2a 20 63 6f 75 6c 64 20 62 65 20 6c  se.** could be l
13630 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  eft in an incons
13631 69 73 74 65 6e 74 20 61 6e 64 20 75 6e 72 65 63  istent and unrec
13632 6f 76 65 72 61 62 6c 65 20 73 74 61 74 65 2e 0a  overable state..
13633 2a 2a 20 53 79 6e 63 68 72 6f 6e 6f 75 73 20 69  ** Synchronous i
13634 73 20 6f 6e 20 62 79 20 64 65 66 61 75 6c 74 20  s on by default 
13635 73 6f 20 64 61 74 61 62 61 73 65 20 63 6f 72 72  so database corr
13636 75 70 74 69 6f 6e 20 69 73 20 6e 6f 74 0a 2a 2a  uption is not.**
13637 20 6e 6f 72 6d 61 6c 6c 79 20 61 20 77 6f 72 72   normally a worr
13638 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
13639 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1363a 42 74 72 65 65 53 65 74 43 61 63 68 65 53 69 7a  BtreeSetCacheSiz
1363b 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  e(Btree *p, int 
1363c 6d 78 50 61 67 65 29 7b 0a 20 20 42 74 53 68 61  mxPage){.  BtSha
1363d 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1363e 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  t;.  assert( sql
1363f 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
13640 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  p->db->mutex) );
13641 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
13642 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c 69 74  nter(p);.  sqlit
13643 65 33 50 61 67 65 72 53 65 74 43 61 63 68 65 73  e3PagerSetCaches
13644 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
13645 20 6d 78 50 61 67 65 29 3b 0a 20 20 73 71 6c 69   mxPage);.  sqli
13646 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
13647 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
13648 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
13649 68 61 6e 67 65 20 74 68 65 20 77 61 79 20 64 61  hange the way da
1364a 74 61 20 69 73 20 73 79 6e 63 65 64 20 74 6f 20  ta is synced to 
1364b 64 69 73 6b 20 69 6e 20 6f 72 64 65 72 20 74 6f  disk in order to
1364c 20 69 6e 63 72 65 61 73 65 20 6f 72 20 64 65 63   increase or dec
1364d 72 65 61 73 65 0a 2a 2a 20 68 6f 77 20 77 65 6c  rease.** how wel
1364e 6c 20 74 68 65 20 64 61 74 61 62 61 73 65 20 72  l the database r
1364f 65 73 69 73 74 73 20 64 61 6d 61 67 65 20 64 75  esists damage du
13650 65 20 74 6f 20 4f 53 20 63 72 61 73 68 65 73 20  e to OS crashes 
13651 61 6e 64 20 70 6f 77 65 72 0a 2a 2a 20 66 61 69  and power.** fai
13652 6c 75 72 65 73 2e 20 20 4c 65 76 65 6c 20 31 20  lures.  Level 1 
13653 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 61  is the same as a
13654 73 79 6e 63 68 72 6f 6e 6f 75 73 20 28 6e 6f 20  synchronous (no 
13655 73 79 6e 63 73 28 29 20 6f 63 63 75 72 20 61 6e  syncs() occur an
13656 64 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 61 20  d.** there is a 
13657 68 69 67 68 20 70 72 6f 62 61 62 69 6c 69 74 79  high probability
13658 20 6f 66 20 64 61 6d 61 67 65 29 20 20 4c 65 76   of damage)  Lev
13659 65 6c 20 32 20 69 73 20 74 68 65 20 64 65 66 61  el 2 is the defa
1365a 75 6c 74 2e 20 20 54 68 65 72 65 0a 2a 2a 20 69  ult.  There.** i
1365b 73 20 61 20 76 65 72 79 20 6c 6f 77 20 62 75 74  s a very low but
1365c 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62   non-zero probab
1365d 69 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 2e  ility of damage.
1365e 20 20 4c 65 76 65 6c 20 33 20 72 65 64 75 63 65    Level 3 reduce
1365f 73 20 74 68 65 0a 2a 2a 20 70 72 6f 62 61 62 69  s the.** probabi
13660 6c 69 74 79 20 6f 66 20 64 61 6d 61 67 65 20 74  lity of damage t
13661 6f 20 6e 65 61 72 20 7a 65 72 6f 20 62 75 74 20  o near zero but 
13662 77 69 74 68 20 61 20 77 72 69 74 65 20 70 65 72  with a write per
13663 66 6f 72 6d 61 6e 63 65 20 72 65 64 75 63 74 69  formance reducti
13664 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
13665 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52  QLITE_OMIT_PAGER
13666 5f 50 52 41 47 4d 41 53 0a 53 51 4c 49 54 45 5f  _PRAGMAS.SQLITE_
13667 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13668 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
13669 79 4c 65 76 65 6c 28 42 74 72 65 65 20 2a 70 2c  yLevel(Btree *p,
1366a 20 69 6e 74 20 6c 65 76 65 6c 2c 20 69 6e 74 20   int level, int 
1366b 66 75 6c 6c 53 79 6e 63 29 7b 0a 20 20 42 74 53  fullSync){.  BtS
1366c 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e  hared *pBt = p->
1366d 70 42 74 3b 0a 20 20 61 73 73 65 72 74 28 20 73  pBt;.  assert( s
1366e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1366f 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
13670 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
13671 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 73 71 6c  eEnter(p);.  sql
13672 69 74 65 33 50 61 67 65 72 53 65 74 53 61 66 65  ite3PagerSetSafe
13673 74 79 4c 65 76 65 6c 28 70 42 74 2d 3e 70 50 61  tyLevel(pBt->pPa
13674 67 65 72 2c 20 6c 65 76 65 6c 2c 20 66 75 6c 6c  ger, level, full
13675 53 79 6e 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  Sync);.  sqlite3
13676 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20  BtreeLeave(p);. 
13677 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
13678 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a  K;.}.#endif../*.
13679 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
1367a 66 20 74 68 65 20 67 69 76 65 6e 20 62 74 72 65  f the given btre
1367b 65 20 69 73 20 73 65 74 20 74 6f 20 73 61 66 65  e is set to safe
1367c 74 79 20 6c 65 76 65 6c 20 31 2e 20 20 49 6e 20  ty level 1.  In 
1367d 6f 74 68 65 72 0a 2a 2a 20 77 6f 72 64 73 2c 20  other.** words, 
1367e 72 65 74 75 72 6e 20 54 52 55 45 20 69 66 20 6e  return TRUE if n
1367f 6f 20 73 79 6e 63 28 29 20 6f 63 63 75 72 73 20  o sync() occurs 
13680 6f 6e 20 74 68 65 20 64 69 73 6b 20 66 69 6c 65  on the disk file
13681 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
13682 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
13683 42 74 72 65 65 53 79 6e 63 44 69 73 61 62 6c 65  BtreeSyncDisable
13684 64 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 42  d(Btree *p){.  B
13685 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
13686 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20 72 63 3b  ->pBt;.  int rc;
13687 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
13688 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d  e3_mutex_held(p-
13689 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 20 20  >db->mutex) );  
1368a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45  .  sqlite3BtreeE
1368b 6e 74 65 72 28 70 29 3b 0a 20 20 61 73 73 65 72  nter(p);.  asser
1368c 74 28 20 70 42 74 20 26 26 20 70 42 74 2d 3e 70  t( pBt && pBt->p
1368d 50 61 67 65 72 20 29 3b 0a 20 20 72 63 20 3d 20  Pager );.  rc = 
1368e 73 71 6c 69 74 65 33 50 61 67 65 72 4e 6f 73 79  sqlite3PagerNosy
1368f 6e 63 28 70 42 74 2d 3e 70 50 61 67 65 72 29 3b  nc(pBt->pPager);
13690 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
13691 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
13692 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  n rc;.}..#if !de
13693 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
13694 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 29  T_PAGER_PRAGMAS)
13695 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51 4c   || !defined(SQL
13696 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d 29  ITE_OMIT_VACUUM)
13697 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68  ./*.** Change th
13698 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 73 20  e default pages 
13699 73 69 7a 65 20 61 6e 64 20 74 68 65 20 6e 75 6d  size and the num
1369a 62 65 72 20 6f 66 20 72 65 73 65 72 76 65 64 20  ber of reserved 
1369b 62 79 74 65 73 20 70 65 72 20 70 61 67 65 2e 0a  bytes per page..
1369c 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65 20 73  **.** The page s
1369d 69 7a 65 20 6d 75 73 74 20 62 65 20 61 20 70 6f  ize must be a po
1369e 77 65 72 20 6f 66 20 32 20 62 65 74 77 65 65 6e  wer of 2 between
1369f 20 35 31 32 20 61 6e 64 20 36 35 35 33 36 2e 20   512 and 65536. 
136a0 20 49 66 20 74 68 65 20 70 61 67 65 0a 2a 2a 20   If the page.** 
136a1 73 69 7a 65 20 73 75 70 70 6c 69 65 64 20 64 6f  size supplied do
136a2 65 73 20 6e 6f 74 20 6d 65 65 74 20 74 68 69 73  es not meet this
136a3 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 68 65 6e   constraint then
136a4 20 74 68 65 20 70 61 67 65 20 73 69 7a 65 20 69   the page size i
136a5 73 20 6e 6f 74 0a 2a 2a 20 63 68 61 6e 67 65 64  s not.** changed
136a6 2e 0a 2a 2a 0a 2a 2a 20 50 61 67 65 20 73 69 7a  ..**.** Page siz
136a7 65 73 20 61 72 65 20 63 6f 6e 73 74 72 61 69 6e  es are constrain
136a8 65 64 20 74 6f 20 62 65 20 61 20 70 6f 77 65 72  ed to be a power
136a9 20 6f 66 20 74 77 6f 20 73 6f 20 74 68 61 74 20   of two so that 
136aa 74 68 65 20 72 65 67 69 6f 6e 0a 2a 2a 20 6f 66  the region.** of
136ab 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
136ac 6c 65 20 75 73 65 64 20 66 6f 72 20 6c 6f 63 6b  le used for lock
136ad 69 6e 67 20 28 62 65 67 69 6e 6e 69 6e 67 20 61  ing (beginning a
136ae 74 20 50 45 4e 44 49 4e 47 5f 42 59 54 45 2c 0a  t PENDING_BYTE,.
136af 2a 2a 20 74 68 65 20 66 69 72 73 74 20 62 79 74  ** the first byt
136b0 65 20 70 61 73 74 20 74 68 65 20 31 47 42 20 62  e past the 1GB b
136b1 6f 75 6e 64 61 72 79 2c 20 30 78 34 30 30 30 30  oundary, 0x40000
136b2 30 30 30 29 20 6e 65 65 64 73 20 74 6f 20 6f 63  000) needs to oc
136b3 63 75 72 0a 2a 2a 20 61 74 20 74 68 65 20 62 65  cur.** at the be
136b4 67 69 6e 6e 69 6e 67 20 6f 66 20 61 20 70 61 67  ginning of a pag
136b5 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61 72 61  e..**.** If para
136b6 6d 65 74 65 72 20 6e 52 65 73 65 72 76 65 20 69  meter nReserve i
136b7 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72 6f  s less than zero
136b8 2c 20 74 68 65 6e 20 74 68 65 20 6e 75 6d 62 65  , then the numbe
136b9 72 20 6f 66 20 72 65 73 65 72 76 65 64 0a 2a 2a  r of reserved.**
136ba 20 62 79 74 65 73 20 70 65 72 20 70 61 67 65 20   bytes per page 
136bb 69 73 20 6c 65 66 74 20 75 6e 63 68 61 6e 67 65  is left unchange
136bc 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
136bd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
136be 42 74 72 65 65 53 65 74 50 61 67 65 53 69 7a 65  BtreeSetPageSize
136bf 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 70  (Btree *p, int p
136c0 61 67 65 53 69 7a 65 2c 20 69 6e 74 20 6e 52 65  ageSize, int nRe
136c1 73 65 72 76 65 29 7b 0a 20 20 69 6e 74 20 72 63  serve){.  int rc
136c2 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
136c3 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20  BtShared *pBt = 
136c4 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65  p->pBt;.  sqlite
136c5 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a  3BtreeEnter(p);.
136c6 20 20 69 66 28 20 70 42 74 2d 3e 70 61 67 65 53    if( pBt->pageS
136c7 69 7a 65 46 69 78 65 64 20 29 7b 0a 20 20 20 20  izeFixed ){.    
136c8 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
136c9 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  e(p);.    return
136ca 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59   SQLITE_READONLY
136cb 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 52 65 73  ;.  }.  if( nRes
136cc 65 72 76 65 3c 30 20 29 7b 0a 20 20 20 20 6e 52  erve<0 ){.    nR
136cd 65 73 65 72 76 65 20 3d 20 70 42 74 2d 3e 70 61  eserve = pBt->pa
136ce 67 65 53 69 7a 65 20 2d 20 70 42 74 2d 3e 75 73  geSize - pBt->us
136cf 61 62 6c 65 53 69 7a 65 3b 0a 20 20 7d 0a 20 20  ableSize;.  }.  
136d0 69 66 28 20 70 61 67 65 53 69 7a 65 3e 3d 35 31  if( pageSize>=51
136d1 32 20 26 26 20 70 61 67 65 53 69 7a 65 3c 3d 53  2 && pageSize<=S
136d2 51 4c 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53  QLITE_MAX_PAGE_S
136d3 49 5a 45 20 26 26 0a 20 20 20 20 20 20 20 20 28  IZE &&.        (
136d4 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67  (pageSize-1)&pag
136d5 65 53 69 7a 65 29 3d 3d 30 20 29 7b 0a 20 20 20  eSize)==0 ){.   
136d6 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53 69   assert( (pageSi
136d7 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20 20  ze & 7)==0 );.  
136d8 20 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e    assert( !pBt->
136d9 70 50 61 67 65 31 20 26 26 20 21 70 42 74 2d 3e  pPage1 && !pBt->
136da 70 43 75 72 73 6f 72 20 29 3b 0a 20 20 20 20 70  pCursor );.    p
136db 42 74 2d 3e 70 61 67 65 53 69 7a 65 20 3d 20 70  Bt->pageSize = p
136dc 61 67 65 53 69 7a 65 3b 0a 20 20 20 20 66 72 65  ageSize;.    fre
136dd 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
136de 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
136df 33 50 61 67 65 72 53 65 74 50 61 67 65 73 69 7a  3PagerSetPagesiz
136e0 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26  e(pBt->pPager, &
136e1 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a  pBt->pageSize);.
136e2 20 20 7d 0a 20 20 70 42 74 2d 3e 75 73 61 62 6c    }.  pBt->usabl
136e3 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 70 61 67  eSize = pBt->pag
136e4 65 53 69 7a 65 20 2d 20 6e 52 65 73 65 72 76 65  eSize - nReserve
136e5 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
136e6 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75  Leave(p);.  retu
136e7 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
136e8 52 65 74 75 72 6e 20 74 68 65 20 63 75 72 72 65  Return the curre
136e9 6e 74 6c 79 20 64 65 66 69 6e 65 64 20 70 61 67  ntly defined pag
136ea 65 20 73 69 7a 65 0a 2a 2f 0a 53 51 4c 49 54 45  e size.*/.SQLITE
136eb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
136ec 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65  ite3BtreeGetPage
136ed 53 69 7a 65 28 42 74 72 65 65 20 2a 70 29 7b 0a  Size(Btree *p){.
136ee 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74 2d    return p->pBt-
136ef 3e 70 61 67 65 53 69 7a 65 3b 0a 7d 0a 53 51 4c  >pageSize;.}.SQL
136f0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
136f1 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
136f2 65 73 65 72 76 65 28 42 74 72 65 65 20 2a 70 29  eserve(Btree *p)
136f3 7b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c  {.  int n;.  sql
136f4 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
136f5 29 3b 0a 20 20 6e 20 3d 20 70 2d 3e 70 42 74 2d  );.  n = p->pBt-
136f6 3e 70 61 67 65 53 69 7a 65 20 2d 20 70 2d 3e 70  >pageSize - p->p
136f7 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 3b 0a  Bt->usableSize;.
136f8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
136f9 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
136fa 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74   n;.}../*.** Set
136fb 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 70 61 67   the maximum pag
136fc 65 20 63 6f 75 6e 74 20 66 6f 72 20 61 20 64 61  e count for a da
136fd 74 61 62 61 73 65 20 69 66 20 6d 78 50 61 67 65  tabase if mxPage
136fe 20 69 73 20 70 6f 73 69 74 69 76 65 2e 0a 2a 2a   is positive..**
136ff 20 4e 6f 20 63 68 61 6e 67 65 73 20 61 72 65 20   No changes are 
13700 6d 61 64 65 20 69 66 20 6d 78 50 61 67 65 20 69  made if mxPage i
13701 73 20 30 20 6f 72 20 6e 65 67 61 74 69 76 65 2e  s 0 or negative.
13702 0a 2a 2a 20 52 65 67 61 72 64 6c 65 73 73 20 6f  .** Regardless o
13703 66 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d  f the value of m
13704 78 50 61 67 65 2c 20 72 65 74 75 72 6e 20 74 68  xPage, return th
13705 65 20 6d 61 78 69 6d 75 6d 20 70 61 67 65 20 63  e maximum page c
13706 6f 75 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ount..*/.SQLITE_
13707 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
13708 74 65 33 42 74 72 65 65 4d 61 78 50 61 67 65 43  te3BtreeMaxPageC
13709 6f 75 6e 74 28 42 74 72 65 65 20 2a 70 2c 20 69  ount(Btree *p, i
1370a 6e 74 20 6d 78 50 61 67 65 29 7b 0a 20 20 69 6e  nt mxPage){.  in
1370b 74 20 6e 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  t n;.  sqlite3Bt
1370c 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 6e  reeEnter(p);.  n
1370d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d   = sqlite3PagerM
1370e 61 78 50 61 67 65 43 6f 75 6e 74 28 70 2d 3e 70  axPageCount(p->p
1370f 42 74 2d 3e 70 50 61 67 65 72 2c 20 6d 78 50 61  Bt->pPager, mxPa
13710 67 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ge);.  sqlite3Bt
13711 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
13712 65 74 75 72 6e 20 6e 3b 0a 7d 0a 23 65 6e 64 69  eturn n;.}.#endi
13713 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
13714 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
13715 50 52 41 47 4d 41 53 29 20 7c 7c 20 21 64 65 66  PRAGMAS) || !def
13716 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
13717 5f 56 41 43 55 55 4d 29 20 2a 2f 0a 0a 2f 2a 0a  _VACUUM) */../*.
13718 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 27 61  ** Change the 'a
13719 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 72 6f 70  uto-vacuum' prop
1371a 65 72 74 79 20 6f 66 20 74 68 65 20 64 61 74 61  erty of the data
1371b 62 61 73 65 2e 20 49 66 20 74 68 65 20 27 61 75  base. If the 'au
1371c 74 6f 56 61 63 75 75 6d 27 0a 2a 2a 20 70 61 72  toVacuum'.** par
1371d 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e 2d 7a 65  ameter is non-ze
1371e 72 6f 2c 20 74 68 65 6e 20 61 75 74 6f 2d 76 61  ro, then auto-va
1371f 63 75 75 6d 20 6d 6f 64 65 20 69 73 20 65 6e 61  cuum mode is ena
13720 62 6c 65 64 2e 20 49 66 20 7a 65 72 6f 2c 20 69  bled. If zero, i
13721 74 0a 2a 2a 20 69 73 20 64 69 73 61 62 6c 65 64  t.** is disabled
13722 2e 20 54 68 65 20 64 65 66 61 75 6c 74 20 76 61  . The default va
13723 6c 75 65 20 66 6f 72 20 74 68 65 20 61 75 74 6f  lue for the auto
13724 2d 76 61 63 75 75 6d 20 70 72 6f 70 65 72 74 79  -vacuum property
13725 20 69 73 20 0a 2a 2a 20 64 65 74 65 72 6d 69 6e   is .** determin
13726 65 64 20 62 79 20 74 68 65 20 53 51 4c 49 54 45  ed by the SQLITE
13727 5f 44 45 46 41 55 4c 54 5f 41 55 54 4f 56 41 43  _DEFAULT_AUTOVAC
13728 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 53 51  UUM macro..*/.SQ
13729 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1372a 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74   sqlite3BtreeSet
1372b 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72 65 65  AutoVacuum(Btree
1372c 20 2a 70 2c 20 69 6e 74 20 61 75 74 6f 56 61 63   *p, int autoVac
1372d 75 75 6d 29 7b 0a 23 69 66 64 65 66 20 53 51 4c  uum){.#ifdef SQL
1372e 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1372f 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  UUM.  return SQL
13730 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 23 65  ITE_READONLY;.#e
13731 6c 73 65 0a 20 20 42 74 53 68 61 72 65 64 20 2a  lse.  BtShared *
13732 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
13733 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13734 4f 4b 3b 0a 20 20 69 6e 74 20 61 76 20 3d 20 28  OK;.  int av = (
13735 61 75 74 6f 56 61 63 75 75 6d 3f 31 3a 30 29 3b  autoVacuum?1:0);
13736 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
13737 45 6e 74 65 72 28 70 29 3b 0a 20 20 69 66 28 20  Enter(p);.  if( 
13738 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78  pBt->pageSizeFix
13739 65 64 20 26 26 20 61 76 21 3d 70 42 74 2d 3e 61  ed && av!=pBt->a
1373a 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20  utoVacuum ){.   
1373b 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 45 41   rc = SQLITE_REA
1373c 44 4f 4e 4c 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a  DONLY;.  }else{.
1373d 20 20 20 20 70 42 74 2d 3e 61 75 74 6f 56 61 63      pBt->autoVac
1373e 75 75 6d 20 3d 20 61 76 3b 0a 20 20 7d 0a 20 20  uum = av;.  }.  
1373f 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
13740 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
13741 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a  c;.#endif.}../*.
13742 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 76 61  ** Return the va
13743 6c 75 65 20 6f 66 20 74 68 65 20 27 61 75 74 6f  lue of the 'auto
13744 2d 76 61 63 75 75 6d 27 20 70 72 6f 70 65 72 74  -vacuum' propert
13745 79 2e 20 49 66 20 61 75 74 6f 2d 76 61 63 75 75  y. If auto-vacuu
13746 6d 20 69 73 20 0a 2a 2a 20 65 6e 61 62 6c 65 64  m is .** enabled
13747 20 31 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20   1 is returned. 
13748 4f 74 68 65 72 77 69 73 65 20 30 2e 0a 2a 2f 0a  Otherwise 0..*/.
13749 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1374a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 47  nt sqlite3BtreeG
1374b 65 74 41 75 74 6f 56 61 63 75 75 6d 28 42 74 72  etAutoVacuum(Btr
1374c 65 65 20 2a 70 29 7b 0a 23 69 66 64 65 66 20 53  ee *p){.#ifdef S
1374d 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
1374e 41 43 55 55 4d 0a 20 20 72 65 74 75 72 6e 20 42  ACUUM.  return B
1374f 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
13750 4e 4f 4e 45 3b 0a 23 65 6c 73 65 0a 20 20 69 6e  NONE;.#else.  in
13751 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42  t rc;.  sqlite3B
13752 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
13753 72 63 20 3d 20 28 0a 20 20 20 20 28 21 70 2d 3e  rc = (.    (!p->
13754 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
13755 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55  ?BTREE_AUTOVACUU
13756 4d 5f 4e 4f 4e 45 3a 0a 20 20 20 20 28 21 70 2d  M_NONE:.    (!p-
13757 3e 70 42 74 2d 3e 69 6e 63 72 56 61 63 75 75 6d  >pBt->incrVacuum
13758 29 3f 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  )?BTREE_AUTOVACU
13759 55 4d 5f 46 55 4c 4c 3a 0a 20 20 20 20 42 54 52  UM_FULL:.    BTR
1375a 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f 49 4e  EE_AUTOVACUUM_IN
1375b 43 52 0a 20 20 29 3b 0a 20 20 73 71 6c 69 74 65  CR.  );.  sqlite
1375c 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
1375d 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e    return rc;.#en
1375e 64 69 66 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 65  dif.}.../*.** Ge
1375f 74 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  t a reference to
13760 20 70 50 61 67 65 31 20 6f 66 20 74 68 65 20 64   pPage1 of the d
13761 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
13762 68 69 73 20 77 69 6c 6c 0a 2a 2a 20 61 6c 73 6f  his will.** also
13763 20 61 63 71 75 69 72 65 20 61 20 72 65 61 64 6c   acquire a readl
13764 6f 63 6b 20 6f 6e 20 74 68 61 74 20 66 69 6c 65  ock on that file
13765 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f  ..**.** SQLITE_O
13766 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e  K is returned on
13767 20 73 75 63 63 65 73 73 2e 20 20 49 66 20 74 68   success.  If th
13768 65 20 66 69 6c 65 20 69 73 20 6e 6f 74 20 61 0a  e file is not a.
13769 2a 2a 20 77 65 6c 6c 2d 66 6f 72 6d 65 64 20 64  ** well-formed d
1376a 61 74 61 62 61 73 65 20 66 69 6c 65 2c 20 74 68  atabase file, th
1376b 65 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  en SQLITE_CORRUP
1376c 54 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  T is returned..*
1376d 2a 20 53 51 4c 49 54 45 5f 42 55 53 59 20 69 73  * SQLITE_BUSY is
1376e 20 72 65 74 75 72 6e 65 64 20 69 66 20 74 68 65   returned if the
1376f 20 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63   database is loc
13770 6b 65 64 2e 20 20 53 51 4c 49 54 45 5f 4e 4f 4d  ked.  SQLITE_NOM
13771 45 4d 0a 2a 2a 20 69 73 20 72 65 74 75 72 6e 65  EM.** is returne
13772 64 20 69 66 20 77 65 20 72 75 6e 20 6f 75 74 20  d if we run out 
13773 6f 66 20 6d 65 6d 6f 72 79 2e 20 0a 2a 2f 0a 73  of memory. .*/.s
13774 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b 42 74  tatic int lockBt
13775 72 65 65 28 42 74 53 68 61 72 65 64 20 2a 70 42  ree(BtShared *pB
13776 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  t){.  int rc;.  
13777 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
13778 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 0a 20  .  int nPage;.. 
13779 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1377a 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d  _mutex_held(pBt-
1377b 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
1377c 20 70 42 74 2d 3e 70 50 61 67 65 31 20 29 20 72   pBt->pPage1 ) r
1377d 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1377e 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1377f 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c  treeGetPage(pBt,
13780 20 31 2c 20 26 70 50 61 67 65 31 2c 20 30 29 3b   1, &pPage1, 0);
13781 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
13782 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
13783 3b 0a 0a 20 20 2f 2a 20 44 6f 20 73 6f 6d 65 20  ;..  /* Do some 
13784 63 68 65 63 6b 69 6e 67 20 74 6f 20 68 65 6c 70  checking to help
13785 20 69 6e 73 75 72 65 20 74 68 65 20 66 69 6c 65   insure the file
13786 20 77 65 20 6f 70 65 6e 65 64 20 72 65 61 6c 6c   we opened reall
13787 79 20 69 73 0a 20 20 2a 2a 20 61 20 76 61 6c 69  y is.  ** a vali
13788 64 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  d database file.
13789 20 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71   .  */.  rc = sq
1378a 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 63 6f  lite3PagerPageco
1378b 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  unt(pBt->pPager,
1378c 20 26 6e 50 61 67 65 29 3b 0a 20 20 69 66 28 20   &nPage);.  if( 
1378d 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1378e 0a 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f  .    goto page1_
1378f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 7d  init_failed;.  }
13790 65 6c 73 65 20 69 66 28 20 6e 50 61 67 65 3e 30  else if( nPage>0
13791 20 29 7b 0a 20 20 20 20 69 6e 74 20 70 61 67 65   ){.    int page
13792 53 69 7a 65 3b 0a 20 20 20 20 69 6e 74 20 75 73  Size;.    int us
13793 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20 75 38  ableSize;.    u8
13794 20 2a 70 61 67 65 31 20 3d 20 70 50 61 67 65 31   *page1 = pPage1
13795 2d 3e 61 44 61 74 61 3b 0a 20 20 20 20 72 63 20  ->aData;.    rc 
13796 3d 20 53 51 4c 49 54 45 5f 4e 4f 54 41 44 42 3b  = SQLITE_NOTADB;
13797 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d 70 28  .    if( memcmp(
13798 70 61 67 65 31 2c 20 7a 4d 61 67 69 63 48 65 61  page1, zMagicHea
13799 64 65 72 2c 20 31 36 29 21 3d 30 20 29 7b 0a 20  der, 16)!=0 ){. 
1379a 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f       goto page1_
1379b 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20  init_failed;.   
1379c 20 7d 0a 20 20 20 20 69 66 28 20 70 61 67 65 31   }.    if( page1
1379d 5b 31 38 5d 3e 31 20 29 7b 0a 20 20 20 20 20 20  [18]>1 ){.      
1379e 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20  pBt->readOnly = 
1379f 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  1;.    }.    if(
137a0 20 70 61 67 65 31 5b 31 39 5d 3e 31 20 29 7b 0a   page1[19]>1 ){.
137a1 20 20 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31        goto page1
137a2 5f 69 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20  _init_failed;.  
137a3 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20    }..    /* The 
137a4 6d 61 78 69 6d 75 6d 20 65 6d 62 65 64 64 65 64  maximum embedded
137a5 20 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62   fraction must b
137a6 65 20 65 78 61 63 74 6c 79 20 32 35 25 2e 20 20  e exactly 25%.  
137a7 41 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d 0a  And the minimum.
137a8 20 20 20 20 2a 2a 20 65 6d 62 65 64 64 65 64 20      ** embedded 
137a9 66 72 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  fraction must be
137aa 20 31 32 2e 35 25 20 66 6f 72 20 62 6f 74 68 20   12.5% for both 
137ab 6c 65 61 66 2d 64 61 74 61 20 61 6e 64 20 6e 6f  leaf-data and no
137ac 6e 2d 6c 65 61 66 2d 64 61 74 61 2e 0a 20 20 20  n-leaf-data..   
137ad 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   ** The original
137ae 20 64 65 73 69 67 6e 20 61 6c 6c 6f 77 65 64 20   design allowed 
137af 74 68 65 73 65 20 61 6d 6f 75 6e 74 73 20 74 6f  these amounts to
137b0 20 76 61 72 79 2c 20 62 75 74 20 61 73 20 6f 66   vary, but as of
137b1 0a 20 20 20 20 2a 2a 20 76 65 72 73 69 6f 6e 20  .    ** version 
137b2 33 2e 36 2e 30 2c 20 77 65 20 72 65 71 75 69 72  3.6.0, we requir
137b3 65 20 74 68 65 6d 20 74 6f 20 62 65 20 66 69 78  e them to be fix
137b4 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  ed..    */.    i
137b5 66 28 20 6d 65 6d 63 6d 70 28 26 70 61 67 65 31  f( memcmp(&page1
137b6 5b 32 31 5d 2c 20 22 5c 31 30 30 5c 30 34 30 5c  [21], "\100\040\
137b7 30 34 30 22 2c 33 29 21 3d 30 20 29 7b 0a 20 20  040",3)!=0 ){.  
137b8 20 20 20 20 67 6f 74 6f 20 70 61 67 65 31 5f 69      goto page1_i
137b9 6e 69 74 5f 66 61 69 6c 65 64 3b 0a 20 20 20 20  nit_failed;.    
137ba 7d 0a 20 20 20 20 70 61 67 65 53 69 7a 65 20 3d  }.    pageSize =
137bb 20 67 65 74 32 62 79 74 65 28 26 70 61 67 65 31   get2byte(&page1
137bc 5b 31 36 5d 29 3b 0a 20 20 20 20 69 66 28 20 28  [16]);.    if( (
137bd 28 70 61 67 65 53 69 7a 65 2d 31 29 26 70 61 67  (pageSize-1)&pag
137be 65 53 69 7a 65 29 21 3d 30 20 7c 7c 20 70 61 67  eSize)!=0 || pag
137bf 65 53 69 7a 65 3c 35 31 32 20 7c 7c 0a 20 20 20  eSize<512 ||.   
137c0 20 20 20 20 20 28 53 51 4c 49 54 45 5f 4d 41 58       (SQLITE_MAX
137c1 5f 50 41 47 45 5f 53 49 5a 45 3c 33 32 37 36 38  _PAGE_SIZE<32768
137c2 20 26 26 20 70 61 67 65 53 69 7a 65 3e 53 51 4c   && pageSize>SQL
137c3 49 54 45 5f 4d 41 58 5f 50 41 47 45 5f 53 49 5a  ITE_MAX_PAGE_SIZ
137c4 45 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20  E).    ){.      
137c5 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
137c6 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
137c7 20 20 61 73 73 65 72 74 28 20 28 70 61 67 65 53    assert( (pageS
137c8 69 7a 65 20 26 20 37 29 3d 3d 30 20 29 3b 0a 20  ize & 7)==0 );. 
137c9 20 20 20 75 73 61 62 6c 65 53 69 7a 65 20 3d 20     usableSize = 
137ca 70 61 67 65 53 69 7a 65 20 2d 20 70 61 67 65 31  pageSize - page1
137cb 5b 32 30 5d 3b 0a 20 20 20 20 69 66 28 20 70 61  [20];.    if( pa
137cc 67 65 53 69 7a 65 21 3d 70 42 74 2d 3e 70 61 67  geSize!=pBt->pag
137cd 65 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 2f  eSize ){.      /
137ce 2a 20 41 66 74 65 72 20 72 65 61 64 69 6e 67 20  * After reading 
137cf 74 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f  the first page o
137d0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  f the database a
137d1 73 73 75 6d 69 6e 67 20 61 20 70 61 67 65 20 73  ssuming a page s
137d2 69 7a 65 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  ize.      ** of 
137d3 42 74 53 68 61 72 65 64 2e 70 61 67 65 53 69 7a  BtShared.pageSiz
137d4 65 2c 20 77 65 20 68 61 76 65 20 64 69 73 63 6f  e, we have disco
137d5 76 65 72 65 64 20 74 68 61 74 20 74 68 65 20 70  vered that the p
137d6 61 67 65 2d 73 69 7a 65 20 69 73 0a 20 20 20 20  age-size is.    
137d7 20 20 2a 2a 20 61 63 74 75 61 6c 6c 79 20 70 61    ** actually pa
137d8 67 65 53 69 7a 65 2e 20 55 6e 6c 6f 63 6b 20 74  geSize. Unlock t
137d9 68 65 20 64 61 74 61 62 61 73 65 2c 20 6c 65 61  he database, lea
137da 76 65 20 70 42 74 2d 3e 70 50 61 67 65 31 20 61  ve pBt->pPage1 a
137db 74 0a 20 20 20 20 20 20 2a 2a 20 7a 65 72 6f 20  t.      ** zero 
137dc 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54  and return SQLIT
137dd 45 5f 4f 4b 2e 20 54 68 65 20 63 61 6c 6c 65 72  E_OK. The caller
137de 20 77 69 6c 6c 20 63 61 6c 6c 20 74 68 69 73 20   will call this 
137df 66 75 6e 63 74 69 6f 6e 0a 20 20 20 20 20 20 2a  function.      *
137e0 2a 20 61 67 61 69 6e 20 77 69 74 68 20 74 68 65  * again with the
137e1 20 63 6f 72 72 65 63 74 20 70 61 67 65 2d 73 69   correct page-si
137e2 7a 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  ze..      */.   
137e3 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
137e4 50 61 67 65 31 29 3b 0a 20 20 20 20 20 20 70 42  Page1);.      pB
137e5 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 3d 20  t->usableSize = 
137e6 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20 20 20  usableSize;.    
137e7 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
137e8 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  = pageSize;.    
137e9 20 20 66 72 65 65 54 65 6d 70 53 70 61 63 65 28    freeTempSpace(
137ea 70 42 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  pBt);.      sqli
137eb 74 65 33 50 61 67 65 72 53 65 74 50 61 67 65 73  te3PagerSetPages
137ec 69 7a 65 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  ize(pBt->pPager,
137ed 20 26 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29   &pBt->pageSize)
137ee 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  ;.      return S
137ef 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a  QLITE_OK;.    }.
137f0 20 20 20 20 69 66 28 20 75 73 61 62 6c 65 53 69      if( usableSi
137f1 7a 65 3c 35 30 30 20 29 7b 0a 20 20 20 20 20 20  ze<500 ){.      
137f2 67 6f 74 6f 20 70 61 67 65 31 5f 69 6e 69 74 5f  goto page1_init_
137f3 66 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20  failed;.    }.  
137f4 20 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 20    pBt->pageSize 
137f5 3d 20 70 61 67 65 53 69 7a 65 3b 0a 20 20 20 20  = pageSize;.    
137f6 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20  pBt->usableSize 
137f7 3d 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 23 69  = usableSize;.#i
137f8 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
137f9 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
137fa 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
137fb 20 3d 20 28 67 65 74 34 62 79 74 65 28 26 70 61   = (get4byte(&pa
137fc 67 65 31 5b 33 36 20 2b 20 34 2a 34 5d 29 3f 31  ge1[36 + 4*4])?1
137fd 3a 30 29 3b 0a 20 20 20 20 70 42 74 2d 3e 69 6e  :0);.    pBt->in
137fe 63 72 56 61 63 75 75 6d 20 3d 20 28 67 65 74 34  crVacuum = (get4
137ff 62 79 74 65 28 26 70 61 67 65 31 5b 33 36 20 2b  byte(&page1[36 +
13800 20 37 2a 34 5d 29 3f 31 3a 30 29 3b 0a 23 65 6e   7*4])?1:0);.#en
13801 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20 6d 61  dif.  }..  /* ma
13802 78 4c 6f 63 61 6c 20 69 73 20 74 68 65 20 6d 61  xLocal is the ma
13803 78 69 6d 75 6d 20 61 6d 6f 75 6e 74 20 6f 66 20  ximum amount of 
13804 70 61 79 6c 6f 61 64 20 74 6f 20 73 74 6f 72 65  payload to store
13805 20 6c 6f 63 61 6c 6c 79 20 66 6f 72 0a 20 20 2a   locally for.  *
13806 2a 20 61 20 63 65 6c 6c 2e 20 20 4d 61 6b 65 20  * a cell.  Make 
13807 73 75 72 65 20 69 74 20 69 73 20 73 6d 61 6c 6c  sure it is small
13808 20 65 6e 6f 75 67 68 20 73 6f 20 74 68 61 74 20   enough so that 
13809 61 74 20 6c 65 61 73 74 20 6d 69 6e 46 61 6e 6f  at least minFano
1380a 75 74 0a 20 20 2a 2a 20 63 65 6c 6c 73 20 63 61  ut.  ** cells ca
1380b 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 6f 6e  n will fit on on
1380c 65 20 70 61 67 65 2e 20 20 57 65 20 61 73 73 75  e page.  We assu
1380d 6d 65 20 61 20 31 30 2d 62 79 74 65 20 70 61 67  me a 10-byte pag
1380e 65 20 68 65 61 64 65 72 2e 0a 20 20 2a 2a 20 42  e header..  ** B
1380f 65 73 69 64 65 73 20 74 68 65 20 70 61 79 6c 6f  esides the paylo
13810 61 64 2c 20 74 68 65 20 63 65 6c 6c 20 6d 75 73  ad, the cell mus
13811 74 20 73 74 6f 72 65 3a 0a 20 20 2a 2a 20 20 20  t store:.  **   
13812 20 20 32 2d 62 79 74 65 20 70 6f 69 6e 74 65 72    2-byte pointer
13813 20 74 6f 20 74 68 65 20 63 65 6c 6c 0a 20 20 2a   to the cell.  *
13814 2a 20 20 20 20 20 34 2d 62 79 74 65 20 63 68 69  *     4-byte chi
13815 6c 64 20 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20  ld pointer.  ** 
13816 20 20 20 20 39 2d 62 79 74 65 20 6e 4b 65 79 20      9-byte nKey 
13817 76 61 6c 75 65 0a 20 20 2a 2a 20 20 20 20 20 34  value.  **     4
13818 2d 62 79 74 65 20 6e 44 61 74 61 20 76 61 6c 75  -byte nData valu
13819 65 0a 20 20 2a 2a 20 20 20 20 20 34 2d 62 79 74  e.  **     4-byt
1381a 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  e overflow page 
1381b 70 6f 69 6e 74 65 72 0a 20 20 2a 2a 20 53 6f 20  pointer.  ** So 
1381c 61 20 63 65 6c 6c 20 63 6f 6e 73 69 73 74 73 20  a cell consists 
1381d 6f 66 20 61 20 32 2d 62 79 74 65 20 70 6f 69 6e  of a 2-byte poin
1381e 65 72 2c 20 61 20 68 65 61 64 65 72 20 77 68 69  er, a header whi
1381f 63 68 20 69 73 20 61 73 20 6d 75 63 68 20 61 73  ch is as much as
13820 0a 20 20 2a 2a 20 31 37 20 62 79 74 65 73 20 6c  .  ** 17 bytes l
13821 6f 6e 67 2c 20 30 20 74 6f 20 4e 20 62 79 74 65  ong, 0 to N byte
13822 73 20 6f 66 20 70 61 79 6c 6f 61 64 2c 20 61 6e  s of payload, an
13823 64 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 34 20  d an optional 4 
13824 62 79 74 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20  byte overflow.  
13825 2a 2a 20 70 61 67 65 20 70 6f 69 6e 74 65 72 2e  ** page pointer.
13826 0a 20 20 2a 2f 0a 20 20 70 42 74 2d 3e 6d 61 78  .  */.  pBt->max
13827 4c 6f 63 61 6c 20 3d 20 28 70 42 74 2d 3e 75 73  Local = (pBt->us
13828 61 62 6c 65 53 69 7a 65 2d 31 32 29 2a 36 34 2f  ableSize-12)*64/
13829 32 35 35 20 2d 20 32 33 3b 0a 20 20 70 42 74 2d  255 - 23;.  pBt-
1382a 3e 6d 69 6e 4c 6f 63 61 6c 20 3d 20 28 70 42 74  >minLocal = (pBt
1382b 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2d 31 32 29  ->usableSize-12)
1382c 2a 33 32 2f 32 35 35 20 2d 20 32 33 3b 0a 20 20  *32/255 - 23;.  
1382d 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 3d 20 70  pBt->maxLeaf = p
1382e 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 20 2d  Bt->usableSize -
1382f 20 33 35 3b 0a 20 20 70 42 74 2d 3e 6d 69 6e 4c   35;.  pBt->minL
13830 65 61 66 20 3d 20 28 70 42 74 2d 3e 75 73 61 62  eaf = (pBt->usab
13831 6c 65 53 69 7a 65 2d 31 32 29 2a 33 32 2f 32 35  leSize-12)*32/25
13832 35 20 2d 20 32 33 3b 0a 20 20 61 73 73 65 72 74  5 - 23;.  assert
13833 28 20 70 42 74 2d 3e 6d 61 78 4c 65 61 66 20 2b  ( pBt->maxLeaf +
13834 20 32 33 20 3c 3d 20 4d 58 5f 43 45 4c 4c 5f 53   23 <= MX_CELL_S
13835 49 5a 45 28 70 42 74 29 20 29 3b 0a 20 20 70 42  IZE(pBt) );.  pB
13836 74 2d 3e 70 50 61 67 65 31 20 3d 20 70 50 61 67  t->pPage1 = pPag
13837 65 31 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  e1;.  return SQL
13838 49 54 45 5f 4f 4b 3b 0a 0a 70 61 67 65 31 5f 69  ITE_OK;..page1_i
13839 6e 69 74 5f 66 61 69 6c 65 64 3a 0a 20 20 72 65  nit_failed:.  re
1383a 6c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 31  leasePage(pPage1
1383b 29 3b 0a 20 20 70 42 74 2d 3e 70 50 61 67 65 31  );.  pBt->pPage1
1383c 20 3d 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 72   = 0;.  return r
1383d 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  c;.}../*.** This
1383e 20 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73 20 6c   routine works l
1383f 69 6b 65 20 6c 6f 63 6b 42 74 72 65 65 28 29 20  ike lockBtree() 
13840 65 78 63 65 70 74 20 74 68 61 74 20 69 74 20 61  except that it a
13841 6c 73 6f 20 69 6e 76 6f 6b 65 73 20 74 68 65 0a  lso invokes the.
13842 2a 2a 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b  ** busy callback
13843 20 69 66 20 74 68 65 72 65 20 69 73 20 6c 6f 63   if there is loc
13844 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2e 0a 2a 2f  k contention..*/
13845 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 63 6b  .static int lock
13846 42 74 72 65 65 57 69 74 68 52 65 74 72 79 28 42  BtreeWithRetry(B
13847 74 72 65 65 20 2a 70 52 65 66 29 7b 0a 20 20 69  tree *pRef){.  i
13848 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
13849 4b 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71  K;..  assert( sq
1384a 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
1384b 75 74 65 78 28 70 52 65 66 29 20 29 3b 0a 20 20  utex(pRef) );.  
1384c 69 66 28 20 70 52 65 66 2d 3e 69 6e 54 72 61 6e  if( pRef->inTran
1384d 73 3d 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b  s==TRANS_NONE ){
1384e 0a 20 20 20 20 75 38 20 69 6e 54 72 61 6e 73 61  .    u8 inTransa
1384f 63 74 69 6f 6e 20 3d 20 70 52 65 66 2d 3e 70 42  ction = pRef->pB
13850 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
13851 3b 0a 20 20 20 20 62 74 72 65 65 49 6e 74 65 67  ;.    btreeInteg
13852 72 69 74 79 28 70 52 65 66 29 3b 0a 20 20 20 20  rity(pRef);.    
13853 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
13854 65 42 65 67 69 6e 54 72 61 6e 73 28 70 52 65 66  eBeginTrans(pRef
13855 2c 20 30 29 3b 0a 20 20 20 20 70 52 65 66 2d 3e  , 0);.    pRef->
13856 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69  pBt->inTransacti
13857 6f 6e 20 3d 20 69 6e 54 72 61 6e 73 61 63 74 69  on = inTransacti
13858 6f 6e 3b 0a 20 20 20 20 70 52 65 66 2d 3e 69 6e  on;.    pRef->in
13859 54 72 61 6e 73 20 3d 20 54 52 41 4e 53 5f 4e 4f  Trans = TRANS_NO
1385a 4e 45 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  NE;.    if( rc==
1385b 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1385c 20 20 20 70 52 65 66 2d 3e 70 42 74 2d 3e 6e 54     pRef->pBt->nT
1385d 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b 0a 20 20  ransaction--;.  
1385e 20 20 7d 0a 20 20 20 20 62 74 72 65 65 49 6e 74    }.    btreeInt
1385f 65 67 72 69 74 79 28 70 52 65 66 29 3b 0a 20 20  egrity(pRef);.  
13860 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
13861 0a 20 20 20 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20  .       ../*.** 
13862 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
13863 6f 75 74 73 74 61 6e 64 69 6e 67 20 63 75 72 73  outstanding curs
13864 6f 72 73 20 61 6e 64 20 77 65 20 61 72 65 20 6e  ors and we are n
13865 6f 74 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65  ot in the middle
13866 0a 2a 2a 20 6f 66 20 61 20 74 72 61 6e 73 61 63  .** of a transac
13867 74 69 6f 6e 20 62 75 74 20 74 68 65 72 65 20 69  tion but there i
13868 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e  s a read lock on
13869 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 20 74   the database, t
1386a 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  hen.** this rout
1386b 69 6e 65 20 75 6e 72 65 66 73 20 74 68 65 20 66  ine unrefs the f
1386c 69 72 73 74 20 70 61 67 65 20 6f 66 20 74 68 65  irst page of the
1386d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
1386e 68 69 63 68 20 0a 2a 2a 20 68 61 73 20 74 68 65  hich .** has the
1386f 20 65 66 66 65 63 74 20 6f 66 20 72 65 6c 65 61   effect of relea
13870 73 69 6e 67 20 74 68 65 20 72 65 61 64 20 6c 6f  sing the read lo
13871 63 6b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  ck..**.** If the
13872 72 65 20 61 72 65 20 61 6e 79 20 6f 75 74 73 74  re are any outst
13873 61 6e 64 69 6e 67 20 63 75 72 73 6f 72 73 2c 20  anding cursors, 
13874 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
13875 61 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49  a no-op..**.** I
13876 66 20 74 68 65 72 65 20 69 73 20 61 20 74 72 61  f there is a tra
13877 6e 73 61 63 74 69 6f 6e 20 69 6e 20 70 72 6f 67  nsaction in prog
13878 72 65 73 73 2c 20 74 68 69 73 20 72 6f 75 74 69  ress, this routi
13879 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
1387a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 75 6e  /.static void un
1387b 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65  lockBtreeIfUnuse
1387c 64 28 42 74 53 68 61 72 65 64 20 2a 70 42 74 29  d(BtShared *pBt)
1387d 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  {.  assert( sqli
1387e 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
1387f 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
13880 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73  if( pBt->inTrans
13881 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f  action==TRANS_NO
13882 4e 45 20 26 26 20 70 42 74 2d 3e 70 43 75 72 73  NE && pBt->pCurs
13883 6f 72 3d 3d 30 20 26 26 20 70 42 74 2d 3e 70 50  or==0 && pBt->pP
13884 61 67 65 31 21 3d 30 20 29 7b 0a 20 20 20 20 69  age1!=0 ){.    i
13885 66 28 20 73 71 6c 69 74 65 33 50 61 67 65 72 52  f( sqlite3PagerR
13886 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61  efcount(pBt->pPa
13887 67 65 72 29 3e 3d 31 20 29 7b 0a 20 20 20 20 20  ger)>=1 ){.     
13888 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e 70 50   assert( pBt->pP
13889 61 67 65 31 2d 3e 61 44 61 74 61 20 29 3b 0a 23  age1->aData );.#
1388a 69 66 20 30 0a 20 20 20 20 20 20 69 66 28 20 70  if 0.      if( p
1388b 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61 74  Bt->pPage1->aDat
1388c 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  a==0 ){.        
1388d 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
1388e 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20   pBt->pPage1;.  
1388f 20 20 20 20 20 20 70 50 61 67 65 2d 3e 61 44 61        pPage->aDa
13890 74 61 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  ta = sqlite3Page
13891 72 47 65 74 44 61 74 61 28 70 50 61 67 65 2d 3e  rGetData(pPage->
13892 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
13893 20 20 70 50 61 67 65 2d 3e 70 42 74 20 3d 20 70    pPage->pBt = p
13894 42 74 3b 0a 20 20 20 20 20 20 20 20 70 50 61 67  Bt;.        pPag
13895 65 2d 3e 70 67 6e 6f 20 3d 20 31 3b 0a 20 20 20  e->pgno = 1;.   
13896 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
13897 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 42    releasePage(pB
13898 74 2d 3e 70 50 61 67 65 31 29 3b 0a 20 20 20 20  t->pPage1);.    
13899 7d 0a 20 20 20 20 70 42 74 2d 3e 70 50 61 67 65  }.    pBt->pPage
1389a 31 20 3d 20 30 3b 0a 20 20 20 20 70 42 74 2d 3e  1 = 0;.    pBt->
1389b 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 7d 0a  inStmt = 0;.  }.
1389c 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
1389d 61 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 62  a new database b
1389e 79 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74  y initializing t
1389f 68 65 20 66 69 72 73 74 20 70 61 67 65 20 6f 66  he first page of
138a0 20 74 68 65 0a 2a 2a 20 66 69 6c 65 2e 0a 2a 2f   the.** file..*/
138a1 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77 44  .static int newD
138a2 61 74 61 62 61 73 65 28 42 74 53 68 61 72 65 64  atabase(BtShared
138a3 20 2a 70 42 74 29 7b 0a 20 20 4d 65 6d 50 61 67   *pBt){.  MemPag
138a4 65 20 2a 70 50 31 3b 0a 20 20 75 6e 73 69 67 6e  e *pP1;.  unsign
138a5 65 64 20 63 68 61 72 20 2a 64 61 74 61 3b 0a 20  ed char *data;. 
138a6 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6e   int rc;.  int n
138a7 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28  Page;..  assert(
138a8 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
138a9 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78 29 20  eld(pBt->mutex) 
138aa 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
138ab 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28  3PagerPagecount(
138ac 70 42 74 2d 3e 70 50 61 67 65 72 2c 20 26 6e 50  pBt->pPager, &nP
138ad 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  age);.  if( rc!=
138ae 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 6e 50 61  SQLITE_OK || nPa
138af 67 65 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75  ge>0 ){.    retu
138b0 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 50 31  rn rc;.  }.  pP1
138b1 20 3d 20 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a   = pBt->pPage1;.
138b2 20 20 61 73 73 65 72 74 28 20 70 50 31 21 3d 30    assert( pP1!=0
138b3 20 29 3b 0a 20 20 64 61 74 61 20 3d 20 70 50 31   );.  data = pP1
138b4 2d 3e 61 44 61 74 61 3b 0a 20 20 72 63 20 3d 20  ->aData;.  rc = 
138b5 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
138b6 65 28 70 50 31 2d 3e 70 44 62 50 61 67 65 29 3b  e(pP1->pDbPage);
138b7 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
138b8 72 6e 20 72 63 3b 0a 20 20 6d 65 6d 63 70 79 28  rn rc;.  memcpy(
138b9 64 61 74 61 2c 20 7a 4d 61 67 69 63 48 65 61 64  data, zMagicHead
138ba 65 72 2c 20 73 69 7a 65 6f 66 28 7a 4d 61 67 69  er, sizeof(zMagi
138bb 63 48 65 61 64 65 72 29 29 3b 0a 20 20 61 73 73  cHeader));.  ass
138bc 65 72 74 28 20 73 69 7a 65 6f 66 28 7a 4d 61 67  ert( sizeof(zMag
138bd 69 63 48 65 61 64 65 72 29 3d 3d 31 36 20 29 3b  icHeader)==16 );
138be 0a 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74  .  put2byte(&dat
138bf 61 5b 31 36 5d 2c 20 70 42 74 2d 3e 70 61 67 65  a[16], pBt->page
138c0 53 69 7a 65 29 3b 0a 20 20 64 61 74 61 5b 31 38  Size);.  data[18
138c1 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 31 39  ] = 1;.  data[19
138c2 5d 20 3d 20 31 3b 0a 20 20 64 61 74 61 5b 32 30  ] = 1;.  data[20
138c3 5d 20 3d 20 70 42 74 2d 3e 70 61 67 65 53 69 7a  ] = pBt->pageSiz
138c4 65 20 2d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e - pBt->usableS
138c5 69 7a 65 3b 0a 20 20 64 61 74 61 5b 32 31 5d 20  ize;.  data[21] 
138c6 3d 20 36 34 3b 0a 20 20 64 61 74 61 5b 32 32 5d  = 64;.  data[22]
138c7 20 3d 20 33 32 3b 0a 20 20 64 61 74 61 5b 32 33   = 32;.  data[23
138c8 5d 20 3d 20 33 32 3b 0a 20 20 6d 65 6d 73 65 74  ] = 32;.  memset
138c9 28 26 64 61 74 61 5b 32 34 5d 2c 20 30 2c 20 31  (&data[24], 0, 1
138ca 30 30 2d 32 34 29 3b 0a 20 20 7a 65 72 6f 50 61  00-24);.  zeroPa
138cb 67 65 28 70 50 31 2c 20 50 54 46 5f 49 4e 54 4b  ge(pP1, PTF_INTK
138cc 45 59 7c 50 54 46 5f 4c 45 41 46 7c 50 54 46 5f  EY|PTF_LEAF|PTF_
138cd 4c 45 41 46 44 41 54 41 20 29 3b 0a 20 20 70 42  LEAFDATA );.  pB
138ce 74 2d 3e 70 61 67 65 53 69 7a 65 46 69 78 65 64  t->pageSizeFixed
138cf 20 3d 20 31 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 1;.#ifndef SQ
138d0 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
138d1 43 55 55 4d 0a 20 20 61 73 73 65 72 74 28 20 70  CUUM.  assert( p
138d2 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 3d 3d  Bt->autoVacuum==
138d3 31 20 7c 7c 20 70 42 74 2d 3e 61 75 74 6f 56 61  1 || pBt->autoVa
138d4 63 75 75 6d 3d 3d 30 20 29 3b 0a 20 20 61 73 73  cuum==0 );.  ass
138d5 65 72 74 28 20 70 42 74 2d 3e 69 6e 63 72 56 61  ert( pBt->incrVa
138d6 63 75 75 6d 3d 3d 31 20 7c 7c 20 70 42 74 2d 3e  cuum==1 || pBt->
138d7 69 6e 63 72 56 61 63 75 75 6d 3d 3d 30 20 29 3b  incrVacuum==0 );
138d8 0a 20 20 70 75 74 34 62 79 74 65 28 26 64 61 74  .  put4byte(&dat
138d9 61 5b 33 36 20 2b 20 34 2a 34 5d 2c 20 70 42 74  a[36 + 4*4], pBt
138da 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20  ->autoVacuum);. 
138db 20 70 75 74 34 62 79 74 65 28 26 64 61 74 61 5b   put4byte(&data[
138dc 33 36 20 2b 20 37 2a 34 5d 2c 20 70 42 74 2d 3e  36 + 7*4], pBt->
138dd 69 6e 63 72 56 61 63 75 75 6d 29 3b 0a 23 65 6e  incrVacuum);.#en
138de 64 69 66 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  dif.  return SQL
138df 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
138e0 20 41 74 74 65 6d 70 74 20 74 6f 20 73 74 61 72   Attempt to star
138e1 74 20 61 20 6e 65 77 20 74 72 61 6e 73 61 63 74  t a new transact
138e2 69 6f 6e 2e 20 41 20 77 72 69 74 65 2d 74 72 61  ion. A write-tra
138e3 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 73  nsaction.** is s
138e4 74 61 72 74 65 64 20 69 66 20 74 68 65 20 73 65  tarted if the se
138e5 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 20 69 73  cond argument is
138e6 20 6e 6f 6e 7a 65 72 6f 2c 20 6f 74 68 65 72 77   nonzero, otherw
138e7 69 73 65 20 61 20 72 65 61 64 2d 0a 2a 2a 20 74  ise a read-.** t
138e8 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20  ransaction.  If 
138e9 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67 75 6d  the second argum
138ea 65 6e 74 20 69 73 20 32 20 6f 72 20 6d 6f 72 65  ent is 2 or more
138eb 20 61 6e 64 20 65 78 63 6c 75 73 69 76 65 0a 2a   and exclusive.*
138ec 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  * transaction is
138ed 20 73 74 61 72 74 65 64 2c 20 6d 65 61 6e 69 6e   started, meanin
138ee 67 20 74 68 61 74 20 6e 6f 20 6f 74 68 65 72 20  g that no other 
138ef 70 72 6f 63 65 73 73 20 69 73 20 61 6c 6c 6f 77  process is allow
138f0 65 64 0a 2a 2a 20 74 6f 20 61 63 63 65 73 73 20  ed.** to access 
138f1 74 68 65 20 64 61 74 61 62 61 73 65 2e 20 20 41  the database.  A
138f2 20 70 72 65 65 78 69 73 74 69 6e 67 20 74 72 61   preexisting tra
138f3 6e 73 61 63 74 69 6f 6e 20 6d 61 79 20 6e 6f 74  nsaction may not
138f4 20 62 65 0a 2a 2a 20 75 70 67 72 61 64 65 64 20   be.** upgraded 
138f5 74 6f 20 65 78 63 6c 75 73 69 76 65 20 62 79 20  to exclusive by 
138f6 63 61 6c 6c 69 6e 67 20 74 68 69 73 20 72 6f 75  calling this rou
138f7 74 69 6e 65 20 61 20 73 65 63 6f 6e 64 20 74 69  tine a second ti
138f8 6d 65 20 2d 20 74 68 65 0a 2a 2a 20 65 78 63 6c  me - the.** excl
138f9 75 73 69 76 69 74 79 20 66 6c 61 67 20 6f 6e 6c  usivity flag onl
138fa 79 20 77 6f 72 6b 73 20 66 6f 72 20 61 20 6e 65  y works for a ne
138fb 77 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  w transaction..*
138fc 2a 0a 2a 2a 20 41 20 77 72 69 74 65 2d 74 72 61  *.** A write-tra
138fd 6e 73 61 63 74 69 6f 6e 20 6d 75 73 74 20 62 65  nsaction must be
138fe 20 73 74 61 72 74 65 64 20 62 65 66 6f 72 65 20   started before 
138ff 61 74 74 65 6d 70 74 69 6e 67 20 61 6e 79 20 0a  attempting any .
13900 2a 2a 20 63 68 61 6e 67 65 73 20 74 6f 20 74 68  ** changes to th
13901 65 20 64 61 74 61 62 61 73 65 2e 20 20 4e 6f 6e  e database.  Non
13902 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69  e of the followi
13903 6e 67 20 72 6f 75 74 69 6e 65 73 20 0a 2a 2a 20  ng routines .** 
13904 77 69 6c 6c 20 77 6f 72 6b 20 75 6e 6c 65 73 73  will work unless
13905 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69   a transaction i
13906 73 20 73 74 61 72 74 65 64 20 66 69 72 73 74 3a  s started first:
13907 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69  .**.**      sqli
13908 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61  te3BtreeCreateTa
13909 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  ble().**      sq
1390a 6c 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65  lite3BtreeCreate
1390b 49 6e 64 65 78 28 29 0a 2a 2a 20 20 20 20 20 20  Index().**      
1390c 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65 61  sqlite3BtreeClea
1390d 72 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20  rTable().**     
1390e 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 72 6f   sqlite3BtreeDro
1390f 70 54 61 62 6c 65 28 29 0a 2a 2a 20 20 20 20 20  pTable().**     
13910 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73   sqlite3BtreeIns
13911 65 72 74 28 29 0a 2a 2a 20 20 20 20 20 20 73 71  ert().**      sq
13912 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
13913 28 29 0a 2a 2a 20 20 20 20 20 20 73 71 6c 69 74  ().**      sqlit
13914 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65 74  e3BtreeUpdateMet
13915 61 28 29 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  a().**.** If an 
13916 69 6e 69 74 69 61 6c 20 61 74 74 65 6d 70 74 20  initial attempt 
13917 74 6f 20 61 63 71 75 69 72 65 20 74 68 65 20 6c  to acquire the l
13918 6f 63 6b 20 66 61 69 6c 73 20 62 65 63 61 75 73  ock fails becaus
13919 65 20 6f 66 20 6c 6f 63 6b 20 63 6f 6e 74 65 6e  e of lock conten
1391a 74 69 6f 6e 0a 2a 2a 20 61 6e 64 20 74 68 65 20  tion.** and the 
1391b 64 61 74 61 62 61 73 65 20 77 61 73 20 70 72 65  database was pre
1391c 76 69 6f 75 73 6c 79 20 75 6e 6c 6f 63 6b 65 64  viously unlocked
1391d 2c 20 74 68 65 6e 20 69 6e 76 6f 6b 65 20 74 68  , then invoke th
1391e 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 0a 2a  e busy handler.*
1391f 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6f 6e  * if there is on
13920 65 2e 20 20 42 75 74 20 69 66 20 74 68 65 72 65  e.  But if there
13921 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79 20   was previously 
13922 61 20 72 65 61 64 2d 6c 6f 63 6b 2c 20 64 6f 20  a read-lock, do 
13923 6e 6f 74 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74 68  not.** invoke th
13924 65 20 62 75 73 79 20 68 61 6e 64 6c 65 72 20 2d  e busy handler -
13925 20 6a 75 73 74 20 72 65 74 75 72 6e 20 53 51 4c   just return SQL
13926 49 54 45 5f 42 55 53 59 2e 20 20 53 51 4c 49 54  ITE_BUSY.  SQLIT
13927 45 5f 42 55 53 59 20 69 73 20 0a 2a 2a 20 72 65  E_BUSY is .** re
13928 74 75 72 6e 65 64 20 77 68 65 6e 20 74 68 65 72  turned when ther
13929 65 20 69 73 20 61 6c 72 65 61 64 79 20 61 20 72  e is already a r
1392a 65 61 64 2d 6c 6f 63 6b 20 69 6e 20 6f 72 64 65  ead-lock in orde
1392b 72 20 74 6f 20 61 76 6f 69 64 20 61 20 64 65 61  r to avoid a dea
1392c 64 6c 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 53 75 70  dlock..**.** Sup
1392d 70 6f 73 65 20 74 68 65 72 65 20 61 72 65 20 74  pose there are t
1392e 77 6f 20 70 72 6f 63 65 73 73 65 73 20 41 20 61  wo processes A a
1392f 6e 64 20 42 2e 20 20 41 20 68 61 73 20 61 20 72  nd B.  A has a r
13930 65 61 64 20 6c 6f 63 6b 20 61 6e 64 20 42 20 68  ead lock and B h
13931 61 73 0a 2a 2a 20 61 20 72 65 73 65 72 76 65 64  as.** a reserved
13932 20 6c 6f 63 6b 2e 20 20 42 20 74 72 69 65 73 20   lock.  B tries 
13933 74 6f 20 70 72 6f 6d 6f 74 65 20 74 6f 20 65 78  to promote to ex
13934 63 6c 75 73 69 76 65 20 62 75 74 20 69 73 20 62  clusive but is b
13935 6c 6f 63 6b 65 64 20 62 65 63 61 75 73 65 0a 2a  locked because.*
13936 2a 20 6f 66 20 41 27 73 20 72 65 61 64 20 6c 6f  * of A's read lo
13937 63 6b 2e 20 20 41 20 74 72 69 65 73 20 74 6f 20  ck.  A tries to 
13938 70 72 6f 6d 6f 74 65 20 74 6f 20 72 65 73 65 72  promote to reser
13939 76 65 64 20 62 75 74 20 69 73 20 62 6c 6f 63 6b  ved but is block
1393a 65 64 20 62 79 20 42 2e 0a 2a 2a 20 4f 6e 65 20  ed by B..** One 
1393b 6f 72 20 74 68 65 20 6f 74 68 65 72 20 6f 66 20  or the other of 
1393c 74 68 65 20 74 77 6f 20 70 72 6f 63 65 73 73 65  the two processe
1393d 73 20 6d 75 73 74 20 67 69 76 65 20 77 61 79 20  s must give way 
1393e 6f 72 20 74 68 65 72 65 20 63 61 6e 20 62 65 0a  or there can be.
1393f 2a 2a 20 6e 6f 20 70 72 6f 67 72 65 73 73 2e 20  ** no progress. 
13940 20 42 79 20 72 65 74 75 72 6e 69 6e 67 20 53 51   By returning SQ
13941 4c 49 54 45 5f 42 55 53 59 20 61 6e 64 20 6e 6f  LITE_BUSY and no
13942 74 20 69 6e 76 6f 6b 69 6e 67 20 74 68 65 20 62  t invoking the b
13943 75 73 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20  usy callback.** 
13944 77 68 65 6e 20 41 20 61 6c 72 65 61 64 79 20 68  when A already h
13945 61 73 20 61 20 72 65 61 64 20 6c 6f 63 6b 2c 20  as a read lock, 
13946 77 65 20 65 6e 63 6f 75 72 61 67 65 20 41 20 74  we encourage A t
13947 6f 20 67 69 76 65 20 75 70 20 61 6e 64 20 6c 65  o give up and le
13948 74 20 42 0a 2a 2a 20 70 72 6f 63 65 65 64 2e 0a  t B.** proceed..
13949 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1394a 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
1394b 65 65 42 65 67 69 6e 54 72 61 6e 73 28 42 74 72  eeBeginTrans(Btr
1394c 65 65 20 2a 70 2c 20 69 6e 74 20 77 72 66 6c 61  ee *p, int wrfla
1394d 67 29 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  g){.  BtShared *
1394e 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
1394f 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
13950 4f 4b 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  OK;..  sqlite3Bt
13951 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
13952 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
13953 20 20 62 74 72 65 65 49 6e 74 65 67 72 69 74 79    btreeIntegrity
13954 28 70 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68  (p);..  /* If th
13955 65 20 62 74 72 65 65 20 69 73 20 61 6c 72 65 61  e btree is alrea
13956 64 79 20 69 6e 20 61 20 77 72 69 74 65 2d 74 72  dy in a write-tr
13957 61 6e 73 61 63 74 69 6f 6e 2c 20 6f 72 20 69 74  ansaction, or it
13958 0a 20 20 2a 2a 20 69 73 20 61 6c 72 65 61 64 79  .  ** is already
13959 20 69 6e 20 61 20 72 65 61 64 2d 74 72 61 6e 73   in a read-trans
1395a 61 63 74 69 6f 6e 20 61 6e 64 20 61 20 72 65 61  action and a rea
1395b 64 2d 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20  d-transaction.  
1395c 2a 2a 20 69 73 20 72 65 71 75 65 73 74 65 64 2c  ** is requested,
1395d 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70   this is a no-op
1395e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
1395f 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53 5f 57  inTrans==TRANS_W
13960 52 49 54 45 20 7c 7c 20 28 70 2d 3e 69 6e 54 72  RITE || (p->inTr
13961 61 6e 73 3d 3d 54 52 41 4e 53 5f 52 45 41 44 20  ans==TRANS_READ 
13962 26 26 20 21 77 72 66 6c 61 67 29 20 29 7b 0a 20  && !wrflag) ){. 
13963 20 20 20 67 6f 74 6f 20 74 72 61 6e 73 5f 62 65     goto trans_be
13964 67 75 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57  gun;.  }..  /* W
13965 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e  rite transaction
13966 73 20 61 72 65 20 6e 6f 74 20 70 6f 73 73 69 62  s are not possib
13967 6c 65 20 6f 6e 20 61 20 72 65 61 64 2d 6f 6e 6c  le on a read-onl
13968 79 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  y database */.  
13969 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  if( pBt->readOnl
1396a 79 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20  y && wrflag ){. 
1396b 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52     rc = SQLITE_R
1396c 45 41 44 4f 4e 4c 59 3b 0a 20 20 20 20 67 6f 74  EADONLY;.    got
1396d 6f 20 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20  o trans_begun;. 
1396e 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 6f 74   }..  /* If anot
1396f 68 65 72 20 64 61 74 61 62 61 73 65 20 68 61 6e  her database han
13970 64 6c 65 20 68 61 73 20 61 6c 72 65 61 64 79 20  dle has already 
13971 6f 70 65 6e 65 64 20 61 20 77 72 69 74 65 20 74  opened a write t
13972 72 61 6e 73 61 63 74 69 6f 6e 20 0a 20 20 2a 2a  ransaction .  **
13973 20 6f 6e 20 74 68 69 73 20 73 68 61 72 65 64 2d   on this shared-
13974 62 74 72 65 65 20 73 74 72 75 63 74 75 72 65 20  btree structure 
13975 61 6e 64 20 61 20 73 65 63 6f 6e 64 20 77 72 69  and a second wri
13976 74 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  te transaction i
13977 73 0a 20 20 2a 2a 20 72 65 71 75 65 73 74 65 64  s.  ** requested
13978 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  , return SQLITE_
13979 42 55 53 59 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  BUSY..  */.  if(
1397a 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
1397b 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion==TRANS_WRITE
1397c 20 26 26 20 77 72 66 6c 61 67 20 29 7b 0a 20 20   && wrflag ){.  
1397d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55    rc = SQLITE_BU
1397e 53 59 3b 0a 20 20 20 20 67 6f 74 6f 20 74 72 61  SY;.    goto tra
1397f 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 7d 0a 0a 23  ns_begun;.  }..#
13980 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13981 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
13982 20 20 69 66 28 20 77 72 66 6c 61 67 3e 31 20 29    if( wrflag>1 )
13983 7b 0a 20 20 20 20 42 74 4c 6f 63 6b 20 2a 70 49  {.    BtLock *pI
13984 74 65 72 3b 0a 20 20 20 20 66 6f 72 28 70 49 74  ter;.    for(pIt
13985 65 72 3d 70 42 74 2d 3e 70 4c 6f 63 6b 3b 20 70  er=pBt->pLock; p
13986 49 74 65 72 3b 20 70 49 74 65 72 3d 70 49 74 65  Iter; pIter=pIte
13987 72 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20  r->pNext){.     
13988 20 69 66 28 20 70 49 74 65 72 2d 3e 70 42 74 72   if( pIter->pBtr
13989 65 65 21 3d 70 20 29 7b 0a 20 20 20 20 20 20 20  ee!=p ){.       
1398a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
1398b 59 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  Y;.        goto 
1398c 74 72 61 6e 73 5f 62 65 67 75 6e 3b 0a 20 20 20  trans_begun;.   
1398d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
1398e 65 6e 64 69 66 0a 0a 20 20 64 6f 20 7b 0a 20 20  endif..  do {.  
1398f 20 20 69 66 28 20 70 42 74 2d 3e 70 50 61 67 65    if( pBt->pPage
13990 31 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 64 6f  1==0 ){.      do
13991 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 6c  {.        rc = l
13992 6f 63 6b 42 74 72 65 65 28 70 42 74 29 3b 0a 20  ockBtree(pBt);. 
13993 20 20 20 20 20 7d 77 68 69 6c 65 28 20 70 42 74       }while( pBt
13994 2d 3e 70 50 61 67 65 31 3d 3d 30 20 26 26 20 72  ->pPage1==0 && r
13995 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a  c==SQLITE_OK );.
13996 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
13997 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
13998 77 72 66 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  wrflag ){.      
13999 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  if( pBt->readOnl
1399a 79 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  y ){.        rc 
1399b 3d 20 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c  = SQLITE_READONL
1399c 59 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  Y;.      }else{.
1399d 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1399e 69 74 65 33 50 61 67 65 72 42 65 67 69 6e 28 70  ite3PagerBegin(p
1399f 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44 62 50  Bt->pPage1->pDbP
139a0 61 67 65 2c 20 77 72 66 6c 61 67 3e 31 29 3b 0a  age, wrflag>1);.
139a1 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
139a2 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
139a3 20 20 20 20 20 20 20 72 63 20 3d 20 6e 65 77 44         rc = newD
139a4 61 74 61 62 61 73 65 28 70 42 74 29 3b 0a 20 20  atabase(pBt);.  
139a5 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
139a6 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 69 66 28      }.  .    if(
139a7 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
139a8 7b 0a 20 20 20 20 20 20 69 66 28 20 77 72 66 6c  {.      if( wrfl
139a9 61 67 20 29 20 70 42 74 2d 3e 69 6e 53 74 6d 74  ag ) pBt->inStmt
139aa 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
139ab 0a 20 20 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72  .      unlockBtr
139ac 65 65 49 66 55 6e 75 73 65 64 28 70 42 74 29 3b  eeIfUnused(pBt);
139ad 0a 20 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28  .    }.  }while(
139ae 20 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59   rc==SQLITE_BUSY
139af 20 26 26 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73   && pBt->inTrans
139b0 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 4e 4f  action==TRANS_NO
139b1 4e 45 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  NE &&.          
139b2 62 74 72 65 65 49 6e 76 6f 6b 65 42 75 73 79 48  btreeInvokeBusyH
139b3 61 6e 64 6c 65 72 28 70 42 74 29 20 29 3b 0a 0a  andler(pBt) );..
139b4 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
139b5 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 70  _OK ){.    if( p
139b6 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52 41 4e 53  ->inTrans==TRANS
139b7 5f 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 20 20 70  _NONE ){.      p
139b8 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e  Bt->nTransaction
139b9 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d  ++;.    }.    p-
139ba 3e 69 6e 54 72 61 6e 73 20 3d 20 28 77 72 66 6c  >inTrans = (wrfl
139bb 61 67 3f 54 52 41 4e 53 5f 57 52 49 54 45 3a 54  ag?TRANS_WRITE:T
139bc 52 41 4e 53 5f 52 45 41 44 29 3b 0a 20 20 20 20  RANS_READ);.    
139bd 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3e 70  if( p->inTrans>p
139be 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
139bf 6e 20 29 7b 0a 20 20 20 20 20 20 70 42 74 2d 3e  n ){.      pBt->
139c0 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 3d 20  inTransaction = 
139c1 70 2d 3e 69 6e 54 72 61 6e 73 3b 0a 20 20 20 20  p->inTrans;.    
139c2 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
139c3 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
139c4 48 45 0a 20 20 20 20 69 66 28 20 77 72 66 6c 61  HE.    if( wrfla
139c5 67 3e 31 20 29 7b 0a 20 20 20 20 20 20 61 73 73  g>1 ){.      ass
139c6 65 72 74 28 20 21 70 42 74 2d 3e 70 45 78 63 6c  ert( !pBt->pExcl
139c7 75 73 69 76 65 20 29 3b 0a 20 20 20 20 20 20 70  usive );.      p
139c8 42 74 2d 3e 70 45 78 63 6c 75 73 69 76 65 20 3d  Bt->pExclusive =
139c9 20 70 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66   p;.    }.#endif
139ca 0a 20 20 7d 0a 0a 0a 74 72 61 6e 73 5f 62 65 67  .  }...trans_beg
139cb 75 6e 3a 0a 20 20 62 74 72 65 65 49 6e 74 65 67  un:.  btreeInteg
139cc 72 69 74 79 28 70 29 3b 0a 20 20 73 71 6c 69 74  rity(p);.  sqlit
139cd 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
139ce 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
139cf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
139d0 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a  OMIT_AUTOVACUUM.
139d1 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 70  ./*.** Set the p
139d2 6f 69 6e 74 65 72 2d 6d 61 70 20 65 6e 74 72 69  ointer-map entri
139d3 65 73 20 66 6f 72 20 61 6c 6c 20 63 68 69 6c 64  es for all child
139d4 72 65 6e 20 6f 66 20 70 61 67 65 20 70 50 61 67  ren of page pPag
139d5 65 2e 20 41 6c 73 6f 2c 20 69 66 0a 2a 2a 20 70  e. Also, if.** p
139d6 50 61 67 65 20 63 6f 6e 74 61 69 6e 73 20 63 65  Page contains ce
139d7 6c 6c 73 20 74 68 61 74 20 70 6f 69 6e 74 20 74  lls that point t
139d8 6f 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73  o overflow pages
139d9 2c 20 73 65 74 20 74 68 65 20 70 6f 69 6e 74 65  , set the pointe
139da 72 0a 2a 2a 20 6d 61 70 20 65 6e 74 72 69 65 73  r.** map entries
139db 20 66 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f   for the overflo
139dc 77 20 70 61 67 65 73 20 61 73 20 77 65 6c 6c 2e  w pages as well.
139dd 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
139de 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28 4d  etChildPtrmaps(M
139df 65 6d 50 61 67 65 20 2a 70 50 61 67 65 29 7b 0a  emPage *pPage){.
139e0 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20    int i;        
139e1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139e2 20 20 20 20 20 2f 2a 20 43 6f 75 6e 74 65 72 20       /* Counter 
139e3 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  variable */.  in
139e4 74 20 6e 43 65 6c 6c 3b 20 20 20 20 20 20 20 20  t nCell;        
139e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139e6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63 65   /* Number of ce
139e7 6c 6c 73 20 69 6e 20 70 61 67 65 20 70 50 61 67  lls in page pPag
139e8 65 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  e */.  int rc;  
139e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
139ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74            /* Ret
139eb 75 72 6e 20 63 6f 64 65 20 2a 2f 0a 20 20 42 74  urn code */.  Bt
139ec 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50  Shared *pBt = pP
139ed 61 67 65 2d 3e 70 42 74 3b 0a 20 20 69 6e 74 20  age->pBt;.  int 
139ee 69 73 49 6e 69 74 4f 72 69 67 20 3d 20 70 50 61  isInitOrig = pPa
139ef 67 65 2d 3e 69 73 49 6e 69 74 3b 0a 20 20 50 67  ge->isInit;.  Pg
139f0 6e 6f 20 70 67 6e 6f 20 3d 20 70 50 61 67 65 2d  no pgno = pPage-
139f1 3e 70 67 6e 6f 3b 0a 0a 20 20 61 73 73 65 72 74  >pgno;..  assert
139f2 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
139f3 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d  held(pPage->pBt-
139f4 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20  >mutex) );.  rc 
139f5 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e  = sqlite3BtreeIn
139f6 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  itPage(pPage);. 
139f7 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
139f8 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 73  OK ){.    goto s
139f9 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73  et_child_ptrmaps
139fa 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 6e 43 65 6c  _out;.  }.  nCel
139fb 6c 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  l = pPage->nCell
139fc 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  ;..  for(i=0; i<
139fd 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
139fe 20 75 38 20 2a 70 43 65 6c 6c 20 3d 20 66 69 6e   u8 *pCell = fin
139ff 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b  dCell(pPage, i);
13a00 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61  ..    rc = ptrma
13a01 70 50 75 74 4f 76 66 6c 50 74 72 28 70 50 61 67  pPutOvflPtr(pPag
13a02 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20 20 69  e, pCell);.    i
13a03 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
13a04 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 73   ){.      goto s
13a05 65 74 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73  et_child_ptrmaps
13a06 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _out;.    }..   
13a07 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
13a08 66 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20  f ){.      Pgno 
13a09 63 68 69 6c 64 50 67 6e 6f 20 3d 20 67 65 74 34  childPgno = get4
13a0a 62 79 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 20  byte(pCell);.   
13a0b 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
13a0c 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e 6f  t(pBt, childPgno
13a0d 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20  , PTRMAP_BTREE, 
13a0e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  pgno);.      if(
13a0f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
13a10 20 67 6f 74 6f 20 73 65 74 5f 63 68 69 6c 64 5f   goto set_child_
13a11 70 74 72 6d 61 70 73 5f 6f 75 74 3b 0a 20 20 20  ptrmaps_out;.   
13a12 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 70   }.  }..  if( !p
13a13 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  Page->leaf ){.  
13a14 20 20 50 67 6e 6f 20 63 68 69 6c 64 50 67 6e 6f    Pgno childPgno
13a15 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
13a16 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67 65 2d  ge->aData[pPage-
13a17 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a  >hdrOffset+8]);.
13a18 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50      rc = ptrmapP
13a19 75 74 28 70 42 74 2c 20 63 68 69 6c 64 50 67 6e  ut(pBt, childPgn
13a1a 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c  o, PTRMAP_BTREE,
13a1b 20 70 67 6e 6f 29 3b 0a 20 20 7d 0a 0a 73 65 74   pgno);.  }..set
13a1c 5f 63 68 69 6c 64 5f 70 74 72 6d 61 70 73 5f 6f  _child_ptrmaps_o
13a1d 75 74 3a 0a 20 20 70 50 61 67 65 2d 3e 69 73 49  ut:.  pPage->isI
13a1e 6e 69 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67  nit = isInitOrig
13a1f 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
13a20 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 77 68 65 72  ../*.** Somewher
13a21 65 20 6f 6e 20 70 50 61 67 65 2c 20 77 68 69 63  e on pPage, whic
13a22 68 20 69 73 20 67 75 61 72 65 6e 74 65 65 64 20  h is guarenteed 
13a23 74 6f 20 62 65 20 61 20 62 74 72 65 65 20 70 61  to be a btree pa
13a24 67 65 2c 20 6e 6f 74 20 61 6e 20 6f 76 65 72 66  ge, not an overf
13a25 6c 6f 77 0a 2a 2a 20 70 61 67 65 2c 20 69 73 20  low.** page, is 
13a26 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 61 67  a pointer to pag
13a27 65 20 69 46 72 6f 6d 2e 20 4d 6f 64 69 66 79 20  e iFrom. Modify 
13a28 74 68 69 73 20 70 6f 69 6e 74 65 72 20 73 6f 20  this pointer so 
13a29 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 74  that it points t
13a2a 6f 0a 2a 2a 20 69 54 6f 2e 20 50 61 72 61 6d 65  o.** iTo. Parame
13a2b 74 65 72 20 65 54 79 70 65 20 64 65 73 63 72 69  ter eType descri
13a2c 62 65 73 20 74 68 65 20 74 79 70 65 20 6f 66 20  bes the type of 
13a2d 70 6f 69 6e 74 65 72 20 74 6f 20 62 65 20 6d 6f  pointer to be mo
13a2e 64 69 66 69 65 64 2c 20 61 73 20 0a 2a 2a 20 66  dified, as .** f
13a2f 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 50 54  ollows:.**.** PT
13a30 52 4d 41 50 5f 42 54 52 45 45 3a 20 20 20 20 20  RMAP_BTREE:     
13a31 70 50 61 67 65 20 69 73 20 61 20 62 74 72 65 65  pPage is a btree
13a32 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69 6e 74  -page. The point
13a33 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61 20 63  er points at a c
13a34 68 69 6c 64 20 0a 2a 2a 20 20 20 20 20 20 20 20  hild .**        
13a35 20 20 20 20 20 20 20 20 20 20 20 70 61 67 65 20             page 
13a36 6f 66 20 70 50 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  of pPage..**.** 
13a37 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31  PTRMAP_OVERFLOW1
13a38 3a 20 70 50 61 67 65 20 69 73 20 61 20 62 74 72  : pPage is a btr
13a39 65 65 2d 70 61 67 65 2e 20 54 68 65 20 70 6f 69  ee-page. The poi
13a3a 6e 74 65 72 20 70 6f 69 6e 74 73 20 61 74 20 61  nter points at a
13a3b 6e 20 6f 76 65 72 66 6c 6f 77 0a 2a 2a 20 20 20  n overflow.**   
13a3c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13a3d 70 61 67 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  page pointed to 
13a3e 62 79 20 6f 6e 65 20 6f 66 20 74 68 65 20 63 65  by one of the ce
13a3f 6c 6c 73 20 6f 6e 20 70 50 61 67 65 2e 0a 2a 2a  lls on pPage..**
13a40 0a 2a 2a 20 50 54 52 4d 41 50 5f 4f 56 45 52 46  .** PTRMAP_OVERF
13a41 4c 4f 57 32 3a 20 70 50 61 67 65 20 69 73 20 61  LOW2: pPage is a
13a42 6e 20 6f 76 65 72 66 6c 6f 77 2d 70 61 67 65 2e  n overflow-page.
13a43 20 54 68 65 20 70 6f 69 6e 74 65 72 20 70 6f 69   The pointer poi
13a44 6e 74 73 20 61 74 20 74 68 65 20 6e 65 78 74 0a  nts at the next.
13a45 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
13a46 20 20 20 20 20 6f 76 65 72 66 6c 6f 77 20 70 61       overflow pa
13a47 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74 2e 0a  ge in the list..
13a48 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 6f  */.static int mo
13a49 64 69 66 79 50 61 67 65 50 6f 69 6e 74 65 72 28  difyPagePointer(
13a4a 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c 20  MemPage *pPage, 
13a4b 50 67 6e 6f 20 69 46 72 6f 6d 2c 20 50 67 6e 6f  Pgno iFrom, Pgno
13a4c 20 69 54 6f 2c 20 75 38 20 65 54 79 70 65 29 7b   iTo, u8 eType){
13a4d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
13a4e 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
13a4f 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
13a50 20 29 3b 0a 20 20 69 66 28 20 65 54 79 70 65 3d   );.  if( eType=
13a51 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57  =PTRMAP_OVERFLOW
13a52 32 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  2 ){.    /* The 
13a53 70 6f 69 6e 74 65 72 20 69 73 20 61 6c 77 61 79  pointer is alway
13a54 73 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79  s the first 4 by
13a55 74 65 73 20 6f 66 20 74 68 65 20 70 61 67 65 20  tes of the page 
13a56 69 6e 20 74 68 69 73 20 63 61 73 65 2e 20 20 2a  in this case.  *
13a57 2f 0a 20 20 20 20 69 66 28 20 67 65 74 34 62 79  /.    if( get4by
13a58 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74 61 29  te(pPage->aData)
13a59 21 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20  !=iFrom ){.     
13a5a 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43   return SQLITE_C
13a5b 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20  ORRUPT_BKPT;.   
13a5c 20 7d 0a 20 20 20 20 70 75 74 34 62 79 74 65 28   }.    put4byte(
13a5d 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 69 54  pPage->aData, iT
13a5e 6f 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  o);.  }else{.   
13a5f 20 69 6e 74 20 69 73 49 6e 69 74 4f 72 69 67 20   int isInitOrig 
13a60 3d 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 3b  = pPage->isInit;
13a61 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
13a62 69 6e 74 20 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20  int nCell;..    
13a63 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 69 74  sqlite3BtreeInit
13a64 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
13a65 20 6e 43 65 6c 6c 20 3d 20 70 50 61 67 65 2d 3e   nCell = pPage->
13a66 6e 43 65 6c 6c 3b 0a 0a 20 20 20 20 66 6f 72 28  nCell;..    for(
13a67 69 3d 30 3b 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b  i=0; i<nCell; i+
13a68 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70 43  +){.      u8 *pC
13a69 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70  ell = findCell(p
13a6a 50 61 67 65 2c 20 69 29 3b 0a 20 20 20 20 20 20  Page, i);.      
13a6b 69 66 28 20 65 54 79 70 65 3d 3d 50 54 52 4d 41  if( eType==PTRMA
13a6c 50 5f 4f 56 45 52 46 4c 4f 57 31 20 29 7b 0a 20  P_OVERFLOW1 ){. 
13a6d 20 20 20 20 20 20 20 43 65 6c 6c 49 6e 66 6f 20         CellInfo 
13a6e 69 6e 66 6f 3b 0a 20 20 20 20 20 20 20 20 73 71  info;.        sq
13a6f 6c 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43  lite3BtreeParseC
13a70 65 6c 6c 50 74 72 28 70 50 61 67 65 2c 20 70 43  ellPtr(pPage, pC
13a71 65 6c 6c 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20  ell, &info);.   
13a72 20 20 20 20 20 69 66 28 20 69 6e 66 6f 2e 69 4f       if( info.iO
13a73 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
13a74 20 20 20 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d       if( iFrom==
13a75 67 65 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b  get4byte(&pCell[
13a76 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29  info.iOverflow])
13a77 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
13a78 70 75 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b  put4byte(&pCell[
13a79 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 2c  info.iOverflow],
13a7a 20 69 54 6f 29 3b 0a 20 20 20 20 20 20 20 20 20   iTo);.         
13a7b 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
13a7c 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
13a7d 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
13a7e 20 20 20 20 20 69 66 28 20 67 65 74 34 62 79 74       if( get4byt
13a7f 65 28 70 43 65 6c 6c 29 3d 3d 69 46 72 6f 6d 20  e(pCell)==iFrom 
13a80 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 75 74  ){.          put
13a81 34 62 79 74 65 28 70 43 65 6c 6c 2c 20 69 54 6f  4byte(pCell, iTo
13a82 29 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  );.          bre
13a83 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
13a84 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20      }.    }.  . 
13a85 20 20 20 69 66 28 20 69 3d 3d 6e 43 65 6c 6c 20     if( i==nCell 
13a86 29 7b 0a 20 20 20 20 20 20 69 66 28 20 65 54 79  ){.      if( eTy
13a87 70 65 21 3d 50 54 52 4d 41 50 5f 42 54 52 45 45  pe!=PTRMAP_BTREE
13a88 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20 20 67   || .          g
13a89 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
13a8a 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
13a8b 4f 66 66 73 65 74 2b 38 5d 29 21 3d 69 46 72 6f  Offset+8])!=iFro
13a8c 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  m ){.        ret
13a8d 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
13a8e 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20 20 7d  PT_BKPT;.      }
13a8f 0a 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28  .      put4byte(
13a90 26 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50  &pPage->aData[pP
13a91 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38  age->hdrOffset+8
13a92 5d 2c 20 69 54 6f 29 3b 0a 20 20 20 20 7d 0a 0a  ], iTo);.    }..
13a93 20 20 20 20 70 50 61 67 65 2d 3e 69 73 49 6e 69      pPage->isIni
13a94 74 20 3d 20 69 73 49 6e 69 74 4f 72 69 67 3b 0a  t = isInitOrig;.
13a95 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
13a96 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  ITE_OK;.}.../*.*
13a97 2a 20 4d 6f 76 65 20 74 68 65 20 6f 70 65 6e 20  * Move the open 
13a98 64 61 74 61 62 61 73 65 20 70 61 67 65 20 70 44  database page pD
13a99 62 50 61 67 65 20 74 6f 20 6c 6f 63 61 74 69 6f  bPage to locatio
13a9a 6e 20 69 46 72 65 65 50 61 67 65 20 69 6e 20 74  n iFreePage in t
13a9b 68 65 20 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e  he .** database.
13a9c 20 54 68 65 20 70 44 62 50 61 67 65 20 72 65 66   The pDbPage ref
13a9d 65 72 65 6e 63 65 20 72 65 6d 61 69 6e 73 20 76  erence remains v
13a9e 61 6c 69 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  alid..*/.static 
13a9f 69 6e 74 20 72 65 6c 6f 63 61 74 65 50 61 67 65  int relocatePage
13aa0 28 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  (.  BtShared *pB
13aa1 74 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t,           /* 
13aa2 42 74 72 65 65 20 2a 2f 0a 20 20 4d 65 6d 50 61  Btree */.  MemPa
13aa3 67 65 20 2a 70 44 62 50 61 67 65 2c 20 20 20 20  ge *pDbPage,    
13aa4 20 20 20 20 2f 2a 20 4f 70 65 6e 20 70 61 67 65      /* Open page
13aa5 20 74 6f 20 6d 6f 76 65 20 2a 2f 0a 20 20 75 38   to move */.  u8
13aa6 20 65 54 79 70 65 2c 20 20 20 20 20 20 20 20 20   eType,         
13aa7 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
13aa8 72 20 6d 61 70 20 27 74 79 70 65 27 20 65 6e 74  r map 'type' ent
13aa9 72 79 20 66 6f 72 20 70 44 62 50 61 67 65 20 2a  ry for pDbPage *
13aaa 2f 0a 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67  /.  Pgno iPtrPag
13aab 65 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  e,           /* 
13aac 50 6f 69 6e 74 65 72 20 6d 61 70 20 27 70 61 67  Pointer map 'pag
13aad 65 2d 6e 6f 27 20 65 6e 74 72 79 20 66 6f 72 20  e-no' entry for 
13aae 70 44 62 50 61 67 65 20 2a 2f 0a 20 20 50 67 6e  pDbPage */.  Pgn
13aaf 6f 20 69 46 72 65 65 50 61 67 65 2c 20 20 20 20  o iFreePage,    
13ab0 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 63        /* The loc
13ab1 61 74 69 6f 6e 20 74 6f 20 6d 6f 76 65 20 70 44  ation to move pD
13ab2 62 50 61 67 65 20 74 6f 20 2a 2f 0a 20 20 69 6e  bPage to */.  in
13ab3 74 20 69 73 43 6f 6d 6d 69 74 0a 29 7b 0a 20 20  t isCommit.){.  
13ab4 4d 65 6d 50 61 67 65 20 2a 70 50 74 72 50 61 67  MemPage *pPtrPag
13ab5 65 3b 20 20 20 2f 2a 20 54 68 65 20 70 61 67 65  e;   /* The page
13ab6 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 61   that contains a
13ab7 20 70 6f 69 6e 74 65 72 20 74 6f 20 70 44 62 50   pointer to pDbP
13ab8 61 67 65 20 2a 2f 0a 20 20 50 67 6e 6f 20 69 44  age */.  Pgno iD
13ab9 62 50 61 67 65 20 3d 20 70 44 62 50 61 67 65 2d  bPage = pDbPage-
13aba 3e 70 67 6e 6f 3b 0a 20 20 50 61 67 65 72 20 2a  >pgno;.  Pager *
13abb 70 50 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50  pPager = pBt->pP
13abc 61 67 65 72 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  ager;.  int rc;.
13abd 0a 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65  .  assert( eType
13abe 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  ==PTRMAP_OVERFLO
13abf 57 32 20 7c 7c 20 65 54 79 70 65 3d 3d 50 54 52  W2 || eType==PTR
13ac0 4d 41 50 5f 4f 56 45 52 46 4c 4f 57 31 20 7c 7c  MAP_OVERFLOW1 ||
13ac1 20 0a 20 20 20 20 20 20 65 54 79 70 65 3d 3d 50   .      eType==P
13ac2 54 52 4d 41 50 5f 42 54 52 45 45 20 7c 7c 20 65  TRMAP_BTREE || e
13ac3 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f 4f  Type==PTRMAP_ROO
13ac4 54 50 41 47 45 20 29 3b 0a 20 20 61 73 73 65 72  TPAGE );.  asser
13ac5 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
13ac6 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
13ac7 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
13ac8 44 62 50 61 67 65 2d 3e 70 42 74 3d 3d 70 42 74  DbPage->pBt==pBt
13ac9 20 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 76 65 20 70   );..  /* Move p
13aca 61 67 65 20 69 44 62 50 61 67 65 20 66 72 6f 6d  age iDbPage from
13acb 20 69 74 73 20 63 75 72 72 65 6e 74 20 6c 6f 63   its current loc
13acc 61 74 69 6f 6e 20 74 6f 20 70 61 67 65 20 6e 75  ation to page nu
13acd 6d 62 65 72 20 69 46 72 65 65 50 61 67 65 20 2a  mber iFreePage *
13ace 2f 0a 20 20 54 52 41 43 45 28 28 22 41 55 54 4f  /.  TRACE(("AUTO
13acf 56 41 43 55 55 4d 3a 20 4d 6f 76 69 6e 67 20 25  VACUUM: Moving %
13ad0 64 20 74 6f 20 66 72 65 65 20 70 61 67 65 20 25  d to free page %
13ad1 64 20 28 70 74 72 20 70 61 67 65 20 25 64 20 74  d (ptr page %d t
13ad2 79 70 65 20 25 64 29 5c 6e 22 2c 20 0a 20 20 20  ype %d)\n", .   
13ad3 20 20 20 69 44 62 50 61 67 65 2c 20 69 46 72 65     iDbPage, iFre
13ad4 65 50 61 67 65 2c 20 69 50 74 72 50 61 67 65 2c  ePage, iPtrPage,
13ad5 20 65 54 79 70 65 29 29 3b 0a 20 20 72 63 20 3d   eType));.  rc =
13ad6 20 73 71 6c 69 74 65 33 50 61 67 65 72 4d 6f 76   sqlite3PagerMov
13ad7 65 70 61 67 65 28 70 50 61 67 65 72 2c 20 70 44  epage(pPager, pD
13ad8 62 50 61 67 65 2d 3e 70 44 62 50 61 67 65 2c 20  bPage->pDbPage, 
13ad9 69 46 72 65 65 50 61 67 65 2c 20 69 73 43 6f 6d  iFreePage, isCom
13ada 6d 69 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  mit);.  if( rc!=
13adb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13adc 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
13add 20 20 70 44 62 50 61 67 65 2d 3e 70 67 6e 6f 20    pDbPage->pgno 
13ade 3d 20 69 46 72 65 65 50 61 67 65 3b 0a 0a 20 20  = iFreePage;..  
13adf 2f 2a 20 49 66 20 70 44 62 50 61 67 65 20 77 61  /* If pDbPage wa
13ae0 73 20 61 20 62 74 72 65 65 2d 70 61 67 65 2c 20  s a btree-page, 
13ae1 74 68 65 6e 20 69 74 20 6d 61 79 20 68 61 76 65  then it may have
13ae2 20 63 68 69 6c 64 20 70 61 67 65 73 20 61 6e 64   child pages and
13ae3 2f 6f 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 74  /or cells.  ** t
13ae4 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f 76 65  hat point to ove
13ae5 72 66 6c 6f 77 20 70 61 67 65 73 2e 20 54 68 65  rflow pages. The
13ae6 20 70 6f 69 6e 74 65 72 20 6d 61 70 20 65 6e 74   pointer map ent
13ae7 72 69 65 73 20 66 6f 72 20 61 6c 6c 20 74 68 65  ries for all the
13ae8 73 65 0a 20 20 2a 2a 20 70 61 67 65 73 20 6e 65  se.  ** pages ne
13ae9 65 64 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64  ed to be changed
13aea 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 70  ..  **.  ** If p
13aeb 44 62 50 61 67 65 20 69 73 20 61 6e 20 6f 76 65  DbPage is an ove
13aec 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65 6e  rflow page, then
13aed 20 74 68 65 20 66 69 72 73 74 20 34 20 62 79 74   the first 4 byt
13aee 65 73 20 6d 61 79 20 73 74 6f 72 65 20 61 0a 20  es may store a. 
13aef 20 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 61   ** pointer to a
13af0 20 73 75 62 73 65 71 75 65 6e 74 20 6f 76 65 72   subsequent over
13af1 66 6c 6f 77 20 70 61 67 65 2e 20 49 66 20 74 68  flow page. If th
13af2 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
13af3 74 68 65 6e 0a 20 20 2a 2a 20 74 68 65 20 70 6f  then.  ** the po
13af4 69 6e 74 65 72 20 6d 61 70 20 6e 65 65 64 73 20  inter map needs 
13af5 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 66 6f  to be updated fo
13af6 72 20 74 68 65 20 73 75 62 73 65 71 75 65 6e 74  r the subsequent
13af7 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2e 0a   overflow page..
13af8 20 20 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65    */.  if( eType
13af9 3d 3d 50 54 52 4d 41 50 5f 42 54 52 45 45 20 7c  ==PTRMAP_BTREE |
13afa 7c 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f  | eType==PTRMAP_
13afb 52 4f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20  ROOTPAGE ){.    
13afc 72 63 20 3d 20 73 65 74 43 68 69 6c 64 50 74 72  rc = setChildPtr
13afd 6d 61 70 73 28 70 44 62 50 61 67 65 29 3b 0a 20  maps(pDbPage);. 
13afe 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
13aff 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65  E_OK ){.      re
13b00 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20  turn rc;.    }. 
13b01 20 7d 65 6c 73 65 7b 0a 20 20 20 20 50 67 6e 6f   }else{.    Pgno
13b02 20 6e 65 78 74 4f 76 66 6c 20 3d 20 67 65 74 34   nextOvfl = get4
13b03 62 79 74 65 28 70 44 62 50 61 67 65 2d 3e 61 44  byte(pDbPage->aD
13b04 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 6e 65  ata);.    if( ne
13b05 78 74 4f 76 66 6c 21 3d 30 20 29 7b 0a 20 20 20  xtOvfl!=0 ){.   
13b06 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75     rc = ptrmapPu
13b07 74 28 70 42 74 2c 20 6e 65 78 74 4f 76 66 6c 2c  t(pBt, nextOvfl,
13b08 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f 57   PTRMAP_OVERFLOW
13b09 32 2c 20 69 46 72 65 65 50 61 67 65 29 3b 0a 20  2, iFreePage);. 
13b0a 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
13b0b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
13b0c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
13b0d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
13b0e 20 20 2f 2a 20 46 69 78 20 74 68 65 20 64 61 74    /* Fix the dat
13b0f 61 62 61 73 65 20 70 6f 69 6e 74 65 72 20 6f 6e  abase pointer on
13b10 20 70 61 67 65 20 69 50 74 72 50 61 67 65 20 74   page iPtrPage t
13b11 68 61 74 20 70 6f 69 6e 74 65 64 20 61 74 20 69  hat pointed at i
13b12 44 62 50 61 67 65 20 73 6f 0a 20 20 2a 2a 20 74  DbPage so.  ** t
13b13 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20 61 74  hat it points at
13b14 20 69 46 72 65 65 50 61 67 65 2e 20 41 6c 73 6f   iFreePage. Also
13b15 20 66 69 78 20 74 68 65 20 70 6f 69 6e 74 65 72   fix the pointer
13b16 20 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 0a 20   map entry for. 
13b17 20 2a 2a 20 69 50 74 72 50 61 67 65 2e 0a 20 20   ** iPtrPage..  
13b18 2a 2f 0a 20 20 69 66 28 20 65 54 79 70 65 21 3d  */.  if( eType!=
13b19 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 20  PTRMAP_ROOTPAGE 
13b1a 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
13b1b 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
13b1c 70 42 74 2c 20 69 50 74 72 50 61 67 65 2c 20 26  pBt, iPtrPage, &
13b1d 70 50 74 72 50 61 67 65 2c 20 30 29 3b 0a 20 20  pPtrPage, 0);.  
13b1e 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13b1f 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74  _OK ){.      ret
13b20 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
13b21 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
13b22 67 65 72 57 72 69 74 65 28 70 50 74 72 50 61 67  gerWrite(pPtrPag
13b23 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
13b24 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13b25 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 6c 65  OK ){.      rele
13b26 61 73 65 50 61 67 65 28 70 50 74 72 50 61 67 65  asePage(pPtrPage
13b27 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
13b28 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63  rc;.    }.    rc
13b29 20 3d 20 6d 6f 64 69 66 79 50 61 67 65 50 6f 69   = modifyPagePoi
13b2a 6e 74 65 72 28 70 50 74 72 50 61 67 65 2c 20 69  nter(pPtrPage, i
13b2b 44 62 50 61 67 65 2c 20 69 46 72 65 65 50 61 67  DbPage, iFreePag
13b2c 65 2c 20 65 54 79 70 65 29 3b 0a 20 20 20 20 72  e, eType);.    r
13b2d 65 6c 65 61 73 65 50 61 67 65 28 70 50 74 72 50  eleasePage(pPtrP
13b2e 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
13b2f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
13b30 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70       rc = ptrmap
13b31 50 75 74 28 70 42 74 2c 20 69 46 72 65 65 50 61  Put(pBt, iFreePa
13b32 67 65 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50  ge, eType, iPtrP
13b33 61 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  age);.    }.  }.
13b34 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
13b35 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61  /* Forward decla
13b36 72 61 74 69 6f 6e 20 72 65 71 75 69 72 65 64 20  ration required 
13b37 62 79 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65  by incrVacuumSte
13b38 70 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  p(). */.static i
13b39 6e 74 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65  nt allocateBtree
13b3a 50 61 67 65 28 42 74 53 68 61 72 65 64 20 2a 2c  Page(BtShared *,
13b3b 20 4d 65 6d 50 61 67 65 20 2a 2a 2c 20 50 67 6e   MemPage **, Pgn
13b3c 6f 20 2a 2c 20 50 67 6e 6f 2c 20 75 38 29 3b 0a  o *, Pgno, u8);.
13b3d 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61  ./*.** Perform a
13b3e 20 73 69 6e 67 6c 65 20 73 74 65 70 20 6f 66 20   single step of 
13b3f 61 6e 20 69 6e 63 72 65 6d 65 6e 74 61 6c 2d 76  an incremental-v
13b40 61 63 75 75 6d 2e 20 49 66 20 73 75 63 63 65 73  acuum. If succes
13b41 73 66 75 6c 2c 0a 2a 2a 20 72 65 74 75 72 6e 20  sful,.** return 
13b42 53 51 4c 49 54 45 5f 4f 4b 2e 20 49 66 20 74 68  SQLITE_OK. If th
13b43 65 72 65 20 69 73 20 6e 6f 20 77 6f 72 6b 20 74  ere is no work t
13b44 6f 20 64 6f 20 28 61 6e 64 20 74 68 65 72 65 66  o do (and theref
13b45 6f 72 65 20 6e 6f 0a 2a 2a 20 70 6f 69 6e 74 20  ore no.** point 
13b46 69 6e 20 63 61 6c 6c 69 6e 67 20 74 68 69 73 20  in calling this 
13b47 66 75 6e 63 74 69 6f 6e 20 61 67 61 69 6e 29 2c  function again),
13b48 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
13b49 4f 4e 45 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 72 65 20  ONE..**.** More 
13b4a 73 70 65 63 69 66 69 63 6c 79 2c 20 74 68 69 73  specificly, this
13b4b 20 66 75 6e 63 74 69 6f 6e 20 61 74 74 65 6d 70   function attemp
13b4c 74 73 20 74 6f 20 72 65 2d 6f 72 67 61 6e 69 7a  ts to re-organiz
13b4d 65 20 74 68 65 20 0a 2a 2a 20 64 61 74 61 62 61  e the .** databa
13b4e 73 65 20 73 6f 20 74 68 61 74 20 74 68 65 20 6c  se so that the l
13b4f 61 73 74 20 70 61 67 65 20 6f 66 20 74 68 65 20  ast page of the 
13b50 66 69 6c 65 20 63 75 72 72 65 6e 74 6c 79 20 69  file currently i
13b51 6e 20 75 73 65 0a 2a 2a 20 69 73 20 6e 6f 20 6c  n use.** is no l
13b52 6f 6e 67 65 72 20 69 6e 20 75 73 65 2e 0a 2a 2a  onger in use..**
13b53 0a 2a 2a 20 49 66 20 74 68 65 20 6e 46 69 6e 20  .** If the nFin 
13b54 70 61 72 61 6d 65 74 65 72 20 69 73 20 6e 6f 6e  parameter is non
13b55 2d 7a 65 72 6f 2c 20 74 68 65 20 69 6d 70 6c 65  -zero, the imple
13b56 6d 65 6e 74 61 74 69 6f 6e 20 61 73 73 75 6d 65  mentation assume
13b57 73 0a 2a 2a 20 74 68 61 74 20 74 68 65 20 63 61  s.** that the ca
13b58 6c 6c 65 72 20 77 69 6c 6c 20 6b 65 65 70 20 63  ller will keep c
13b59 61 6c 6c 69 6e 67 20 69 6e 63 72 56 61 63 75 75  alling incrVacuu
13b5a 6d 53 74 65 70 28 29 20 75 6e 74 69 6c 0a 2a 2a  mStep() until.**
13b5b 20 69 74 20 72 65 74 75 72 6e 73 20 53 51 4c 49   it returns SQLI
13b5c 54 45 5f 44 4f 4e 45 20 6f 72 20 61 6e 20 65 72  TE_DONE or an er
13b5d 72 6f 72 2c 20 61 6e 64 20 74 68 61 74 20 6e 46  ror, and that nF
13b5e 69 6e 20 69 73 20 74 68 65 0a 2a 2a 20 6e 75 6d  in is the.** num
13b5f 62 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 65  ber of pages the
13b60 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
13b61 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 66 74 65  ill contain afte
13b62 72 20 74 68 69 73 20 0a 2a 2a 20 70 72 6f 63 65  r this .** proce
13b63 73 73 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a  ss is complete..
13b64 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
13b65 63 72 56 61 63 75 75 6d 53 74 65 70 28 42 74 53  crVacuumStep(BtS
13b66 68 61 72 65 64 20 2a 70 42 74 2c 20 50 67 6e 6f  hared *pBt, Pgno
13b67 20 6e 46 69 6e 29 7b 0a 20 20 50 67 6e 6f 20 69   nFin){.  Pgno i
13b68 4c 61 73 74 50 67 3b 20 20 20 20 20 20 20 20 20  LastPg;         
13b69 20 20 20 20 2f 2a 20 4c 61 73 74 20 70 61 67 65      /* Last page
13b6a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
13b6b 20 2a 2f 0a 20 20 50 67 6e 6f 20 6e 46 72 65 65   */.  Pgno nFree
13b6c 4c 69 73 74 3b 20 20 20 20 20 20 20 20 20 20 20  List;           
13b6d 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67  /* Number of pag
13b6e 65 73 20 73 74 69 6c 6c 20 6f 6e 20 74 68 65 20  es still on the 
13b6f 66 72 65 65 2d 6c 69 73 74 20 2a 2f 0a 0a 20 20  free-list */..  
13b70 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
13b71 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
13b72 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 4c 61 73  mutex) );.  iLas
13b73 74 50 67 20 3d 20 70 42 74 2d 3e 6e 54 72 75 6e  tPg = pBt->nTrun
13b74 63 3b 0a 20 20 69 66 28 20 69 4c 61 73 74 50 67  c;.  if( iLastPg
13b75 3d 3d 30 20 29 7b 0a 20 20 20 20 69 4c 61 73 74  ==0 ){.    iLast
13b76 50 67 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  Pg = pagerPageco
13b77 75 6e 74 28 70 42 74 29 3b 0a 20 20 7d 0a 0a 20  unt(pBt);.  }.. 
13b78 20 69 66 28 20 21 50 54 52 4d 41 50 5f 49 53 50   if( !PTRMAP_ISP
13b79 41 47 45 28 70 42 74 2c 20 69 4c 61 73 74 50 67  AGE(pBt, iLastPg
13b7a 29 20 26 26 20 69 4c 61 73 74 50 67 21 3d 50 45  ) && iLastPg!=PE
13b7b 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
13b7c 70 42 74 29 20 29 7b 0a 20 20 20 20 69 6e 74 20  pBt) ){.    int 
13b7d 72 63 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65  rc;.    u8 eType
13b7e 3b 0a 20 20 20 20 50 67 6e 6f 20 69 50 74 72 50  ;.    Pgno iPtrP
13b7f 61 67 65 3b 0a 0a 20 20 20 20 6e 46 72 65 65 4c  age;..    nFreeL
13b80 69 73 74 20 3d 20 67 65 74 34 62 79 74 65 28 26  ist = get4byte(&
13b81 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 61 44 61  pBt->pPage1->aDa
13b82 74 61 5b 33 36 5d 29 3b 0a 20 20 20 20 69 66 28  ta[36]);.    if(
13b83 20 6e 46 72 65 65 4c 69 73 74 3d 3d 30 20 7c 7c   nFreeList==0 ||
13b84 20 6e 46 69 6e 3d 3d 69 4c 61 73 74 50 67 20 29   nFin==iLastPg )
13b85 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 53  {.      return S
13b86 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 20 20  QLITE_DONE;.    
13b87 7d 0a 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d  }..    rc = ptrm
13b88 61 70 47 65 74 28 70 42 74 2c 20 69 4c 61 73 74  apGet(pBt, iLast
13b89 50 67 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74  Pg, &eType, &iPt
13b8a 72 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  rPage);.    if( 
13b8b 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
13b8c 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
13b8d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
13b8e 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52 4f  eType==PTRMAP_RO
13b8f 4f 54 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20  OTPAGE ){.      
13b90 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f  return SQLITE_CO
13b91 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
13b92 7d 0a 0a 20 20 20 20 69 66 28 20 65 54 79 70 65  }..    if( eType
13b93 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  ==PTRMAP_FREEPAG
13b94 45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  E ){.      if( n
13b95 46 69 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  Fin==0 ){.      
13b96 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20    /* Remove the 
13b97 70 61 67 65 20 66 72 6f 6d 20 74 68 65 20 66 69  page from the fi
13b98 6c 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 54  les free-list. T
13b99 68 69 73 20 69 73 20 6e 6f 74 20 72 65 71 75 69  his is not requi
13b9a 72 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  red.        ** i
13b9b 66 20 6e 46 69 6e 20 69 73 20 6e 6f 6e 2d 7a 65  f nFin is non-ze
13b9c 72 6f 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65  ro. In that case
13b9d 2c 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  , the free-list 
13b9e 77 69 6c 6c 20 62 65 0a 20 20 20 20 20 20 20 20  will be.        
13b9f 2a 2a 20 74 72 75 6e 63 61 74 65 64 20 74 6f 20  ** truncated to 
13ba0 7a 65 72 6f 20 61 66 74 65 72 20 74 68 69 73 20  zero after this 
13ba1 66 75 6e 63 74 69 6f 6e 20 72 65 74 75 72 6e 73  function returns
13ba2 2c 20 73 6f 20 69 74 20 64 6f 65 73 6e 27 74 20  , so it doesn't 
13ba3 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 74 74  .        ** matt
13ba4 65 72 20 69 66 20 69 74 20 73 74 69 6c 6c 20 63  er if it still c
13ba5 6f 6e 74 61 69 6e 73 20 73 6f 6d 65 20 67 61 72  ontains some gar
13ba6 62 61 67 65 20 65 6e 74 72 69 65 73 2e 0a 20 20  bage entries..  
13ba7 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
13ba8 20 50 67 6e 6f 20 69 46 72 65 65 50 67 3b 0a 20   Pgno iFreePg;. 
13ba9 20 20 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a         MemPage *
13baa 70 46 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20  pFreePg;.       
13bab 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
13bac 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 46  reePage(pBt, &pF
13bad 72 65 65 50 67 2c 20 26 69 46 72 65 65 50 67 2c  reePg, &iFreePg,
13bae 20 69 4c 61 73 74 50 67 2c 20 31 29 3b 0a 20 20   iLastPg, 1);.  
13baf 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
13bb0 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
13bb1 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
13bb2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
13bb3 20 20 61 73 73 65 72 74 28 20 69 46 72 65 65 50    assert( iFreeP
13bb4 67 3d 3d 69 4c 61 73 74 50 67 20 29 3b 0a 20 20  g==iLastPg );.  
13bb5 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
13bb6 65 28 70 46 72 65 65 50 67 29 3b 0a 20 20 20 20  e(pFreePg);.    
13bb7 20 20 7d 0a 20 20 20 20 7d 20 65 6c 73 65 20 7b    }.    } else {
13bb8 0a 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 65  .      Pgno iFre
13bb9 65 50 67 3b 20 20 20 20 20 20 20 20 20 20 20 20  ePg;            
13bba 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 66 72 65   /* Index of fre
13bbb 65 20 70 61 67 65 20 74 6f 20 6d 6f 76 65 20 70  e page to move p
13bbc 4c 61 73 74 50 67 20 74 6f 20 2a 2f 0a 20 20 20  LastPg to */.   
13bbd 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c 61 73     MemPage *pLas
13bbe 74 50 67 3b 0a 0a 20 20 20 20 20 20 72 63 20 3d  tPg;..      rc =
13bbf 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
13bc0 50 61 67 65 28 70 42 74 2c 20 69 4c 61 73 74 50  Page(pBt, iLastP
13bc1 67 2c 20 26 70 4c 61 73 74 50 67 2c 20 30 29 3b  g, &pLastPg, 0);
13bc2 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53  .      if( rc!=S
13bc3 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13bc4 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
13bc5 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
13bc6 20 49 66 20 6e 46 69 6e 20 69 73 20 7a 65 72 6f   If nFin is zero
13bc7 2c 20 74 68 69 73 20 6c 6f 6f 70 20 72 75 6e 73  , this loop runs
13bc8 20 65 78 61 63 74 6c 79 20 6f 6e 63 65 20 61 6e   exactly once an
13bc9 64 20 70 61 67 65 20 70 4c 61 73 74 50 67 0a 20  d page pLastPg. 
13bca 20 20 20 20 20 2a 2a 20 69 73 20 73 77 61 70 70       ** is swapp
13bcb 65 64 20 77 69 74 68 20 74 68 65 20 66 69 72 73  ed with the firs
13bcc 74 20 66 72 65 65 20 70 61 67 65 20 70 75 6c 6c  t free page pull
13bcd 65 64 20 6f 66 66 20 74 68 65 20 66 72 65 65 20  ed off the free 
13bce 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2a 0a 20  list..      **. 
13bcf 20 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20 6f       ** On the o
13bd0 74 68 65 72 20 68 61 6e 64 2c 20 69 66 20 6e 46  ther hand, if nF
13bd1 69 6e 20 69 73 20 67 72 65 61 74 65 72 20 74 68  in is greater th
13bd2 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6b 65  an zero, then ke
13bd3 65 70 0a 20 20 20 20 20 20 2a 2a 20 6c 6f 6f 70  ep.      ** loop
13bd4 69 6e 67 20 75 6e 74 69 6c 20 61 20 66 72 65 65  ing until a free
13bd5 2d 70 61 67 65 20 6c 6f 63 61 74 65 64 20 77 69  -page located wi
13bd6 74 68 69 6e 20 74 68 65 20 66 69 72 73 74 20 6e  thin the first n
13bd7 46 69 6e 20 70 61 67 65 73 0a 20 20 20 20 20 20  Fin pages.      
13bd8 2a 2a 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69  ** of the file i
13bd9 73 20 66 6f 75 6e 64 2e 0a 20 20 20 20 20 20 2a  s found..      *
13bda 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20 20 20  /.      do {.   
13bdb 20 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 46       MemPage *pF
13bdc 72 65 65 50 67 3b 0a 20 20 20 20 20 20 20 20 72  reePg;.        r
13bdd 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65  c = allocateBtre
13bde 65 50 61 67 65 28 70 42 74 2c 20 26 70 46 72 65  ePage(pBt, &pFre
13bdf 65 50 67 2c 20 26 69 46 72 65 65 50 67 2c 20 30  ePg, &iFreePg, 0
13be0 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
13be1 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
13be2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 6c  ){.          rel
13be3 65 61 73 65 50 61 67 65 28 70 4c 61 73 74 50 67  easePage(pLastPg
13be4 29 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74  );.          ret
13be5 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20  urn rc;.        
13be6 7d 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73  }.        releas
13be7 65 50 61 67 65 28 70 46 72 65 65 50 67 29 3b 0a  ePage(pFreePg);.
13be8 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 6e 46        }while( nF
13be9 69 6e 21 3d 30 20 26 26 20 69 46 72 65 65 50 67  in!=0 && iFreePg
13bea 3e 6e 46 69 6e 20 29 3b 0a 20 20 20 20 20 20 61  >nFin );.      a
13beb 73 73 65 72 74 28 20 69 46 72 65 65 50 67 3c 69  ssert( iFreePg<i
13bec 4c 61 73 74 50 67 20 29 3b 0a 20 20 20 20 20 20  LastPg );.      
13bed 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
13bee 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 4c  te3PagerWrite(pL
13bef 61 73 74 50 67 2d 3e 70 44 62 50 61 67 65 29 3b  astPg->pDbPage);
13bf0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
13bf1 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
13bf2 20 20 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74      rc = relocat
13bf3 65 50 61 67 65 28 70 42 74 2c 20 70 4c 61 73 74  ePage(pBt, pLast
13bf4 50 67 2c 20 65 54 79 70 65 2c 20 69 50 74 72 50  Pg, eType, iPtrP
13bf5 61 67 65 2c 20 69 46 72 65 65 50 67 2c 20 6e 46  age, iFreePg, nF
13bf6 69 6e 21 3d 30 29 3b 0a 20 20 20 20 20 20 7d 0a  in!=0);.      }.
13bf7 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
13bf8 65 28 70 4c 61 73 74 50 67 29 3b 0a 20 20 20 20  e(pLastPg);.    
13bf9 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13bfa 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
13bfb 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
13bfc 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 70  }.    }.  }..  p
13bfd 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 69 4c 61  Bt->nTrunc = iLa
13bfe 73 74 50 67 20 2d 20 31 3b 0a 20 20 77 68 69 6c  stPg - 1;.  whil
13bff 65 28 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3d 3d  e( pBt->nTrunc==
13c00 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
13c01 45 28 70 42 74 29 7c 7c 50 54 52 4d 41 50 5f 49  E(pBt)||PTRMAP_I
13c02 53 50 41 47 45 28 70 42 74 2c 20 70 42 74 2d 3e  SPAGE(pBt, pBt->
13c03 6e 54 72 75 6e 63 29 20 29 7b 0a 20 20 20 20 70  nTrunc) ){.    p
13c04 42 74 2d 3e 6e 54 72 75 6e 63 2d 2d 3b 0a 20 20  Bt->nTrunc--;.  
13c05 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
13c06 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  E_OK;.}../*.** A
13c07 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74 69   write-transacti
13c08 6f 6e 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65  on must be opene
13c09 64 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67  d before calling
13c0a 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a   this function..
13c0b 2a 2a 20 49 74 20 70 65 72 66 6f 72 6d 73 20 61  ** It performs a
13c0c 20 73 69 6e 67 6c 65 20 75 6e 69 74 20 6f 66 20   single unit of 
13c0d 77 6f 72 6b 20 74 6f 77 61 72 64 73 20 61 6e 20  work towards an 
13c0e 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
13c0f 75 6d 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  um..**.** If the
13c10 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63   incremental vac
13c11 75 75 6d 20 69 73 20 66 69 6e 69 73 68 65 64 20  uum is finished 
13c12 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63 74  after this funct
13c13 69 6f 6e 20 68 61 73 20 72 75 6e 2c 0a 2a 2a 20  ion has run,.** 
13c14 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69 73 20 72  SQLITE_DONE is r
13c15 65 74 75 72 6e 65 64 2e 20 49 66 20 69 74 20 69  eturned. If it i
13c16 73 20 6e 6f 74 20 66 69 6e 69 73 68 65 64 2c 20  s not finished, 
13c17 62 75 74 20 6e 6f 20 65 72 72 6f 72 20 6f 63 63  but no error occ
13c18 75 72 65 64 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f  ured,.** SQLITE_
13c19 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  OK is returned. 
13c1a 4f 74 68 65 72 77 69 73 65 20 61 6e 20 53 51 4c  Otherwise an SQL
13c1b 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 20  ite error code. 
13c1c 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
13c1d 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
13c1e 72 65 65 49 6e 63 72 56 61 63 75 75 6d 28 42 74  reeIncrVacuum(Bt
13c1f 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72  ree *p){.  int r
13c20 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  c;.  BtShared *p
13c21 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20  Bt = p->pBt;..  
13c22 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
13c23 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64 62 20  r(p);.  pBt->db 
13c24 3d 20 70 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72  = p->db;.  asser
13c25 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61  t( pBt->inTransa
13c26 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57 52 49  ction==TRANS_WRI
13c27 54 45 20 26 26 20 70 2d 3e 69 6e 54 72 61 6e 73  TE && p->inTrans
13c28 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
13c29 0a 20 20 69 66 28 20 21 70 42 74 2d 3e 61 75 74  .  if( !pBt->aut
13c2a 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 72  oVacuum ){.    r
13c2b 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
13c2c 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
13c2d 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65 72 66  validateAllOverf
13c2e 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b 0a 20  lowCache(pBt);. 
13c2f 20 20 20 72 63 20 3d 20 69 6e 63 72 56 61 63 75     rc = incrVacu
13c30 75 6d 53 74 65 70 28 70 42 74 2c 20 30 29 3b 0a  umStep(pBt, 0);.
13c31 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74 72    }.  sqlite3Btr
13c32 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65  eeLeave(p);.  re
13c33 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
13c34 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
13c35 73 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 20 74  s called prior t
13c36 6f 20 73 71 6c 69 74 65 33 50 61 67 65 72 43 6f  o sqlite3PagerCo
13c37 6d 6d 69 74 20 77 68 65 6e 20 61 20 74 72 61 6e  mmit when a tran
13c38 73 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f  saction.** is co
13c39 6d 6d 69 74 65 64 20 66 6f 72 20 61 6e 20 61 75  mmited for an au
13c3a 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61  to-vacuum databa
13c3b 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 53 51 4c  se..**.** If SQL
13c3c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e  ITE_OK is return
13c3d 65 64 2c 20 74 68 65 6e 20 2a 70 6e 54 72 75 6e  ed, then *pnTrun
13c3e 63 20 69 73 20 73 65 74 20 74 6f 20 74 68 65 20  c is set to the 
13c3f 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 0a  number of pages.
13c40 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ** the database 
13c41 66 69 6c 65 20 73 68 6f 75 6c 64 20 62 65 20 74  file should be t
13c42 72 75 6e 63 61 74 65 64 20 74 6f 20 64 75 72 69  runcated to duri
13c43 6e 67 20 74 68 65 20 63 6f 6d 6d 69 74 20 70 72  ng the commit pr
13c44 6f 63 65 73 73 2e 20 0a 2a 2a 20 69 2e 65 2e 20  ocess. .** i.e. 
13c45 74 68 65 20 64 61 74 61 62 61 73 65 20 68 61 73  the database has
13c46 20 62 65 65 6e 20 72 65 6f 72 67 61 6e 69 7a 65   been reorganize
13c47 64 20 73 6f 20 74 68 61 74 20 6f 6e 6c 79 20 74  d so that only t
13c48 68 65 20 66 69 72 73 74 20 2a 70 6e 54 72 75 6e  he first *pnTrun
13c49 63 0a 2a 2a 20 70 61 67 65 73 20 61 72 65 20 69  c.** pages are i
13c4a 6e 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  n use..*/.static
13c4b 20 69 6e 74 20 61 75 74 6f 56 61 63 75 75 6d 43   int autoVacuumC
13c4c 6f 6d 6d 69 74 28 42 74 53 68 61 72 65 64 20 2a  ommit(BtShared *
13c4d 70 42 74 2c 20 50 67 6e 6f 20 2a 70 6e 54 72 75  pBt, Pgno *pnTru
13c4e 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  nc){.  int rc = 
13c4f 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 61 67  SQLITE_OK;.  Pag
13c50 65 72 20 2a 70 50 61 67 65 72 20 3d 20 70 42 74  er *pPager = pBt
13c51 2d 3e 70 50 61 67 65 72 3b 0a 20 20 56 56 41 5f  ->pPager;.  VVA_
13c52 4f 4e 4c 59 28 20 69 6e 74 20 6e 52 65 66 20 3d  ONLY( int nRef =
13c53 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 65 66   sqlite3PagerRef
13c54 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29 3b  count(pPager) );
13c55 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
13c56 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70  te3_mutex_held(p
13c57 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
13c58 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
13c59 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b  rflowCache(pBt);
13c5a 0a 20 20 61 73 73 65 72 74 28 70 42 74 2d 3e 61  .  assert(pBt->a
13c5b 75 74 6f 56 61 63 75 75 6d 29 3b 0a 20 20 69 66  utoVacuum);.  if
13c5c 28 20 21 70 42 74 2d 3e 69 6e 63 72 56 61 63 75  ( !pBt->incrVacu
13c5d 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f 20 6e  um ){.    Pgno n
13c5e 46 69 6e 20 3d 20 30 3b 0a 0a 20 20 20 20 69 66  Fin = 0;..    if
13c5f 28 20 70 42 74 2d 3e 6e 54 72 75 6e 63 3d 3d 30  ( pBt->nTrunc==0
13c60 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f 20 6e   ){.      Pgno n
13c61 46 72 65 65 3b 0a 20 20 20 20 20 20 50 67 6e 6f  Free;.      Pgno
13c62 20 6e 50 74 72 6d 61 70 3b 0a 20 20 20 20 20 20   nPtrmap;.      
13c63 63 6f 6e 73 74 20 69 6e 74 20 70 67 73 7a 20 3d  const int pgsz =
13c64 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 3b 0a   pBt->pageSize;.
13c65 20 20 20 20 20 20 50 67 6e 6f 20 6e 4f 72 69 67        Pgno nOrig
13c66 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f 75 6e   = pagerPagecoun
13c67 74 28 70 42 74 29 3b 0a 0a 20 20 20 20 20 20 69  t(pBt);..      i
13c68 66 28 20 50 54 52 4d 41 50 5f 49 53 50 41 47 45  f( PTRMAP_ISPAGE
13c69 28 70 42 74 2c 20 6e 4f 72 69 67 29 20 29 7b 0a  (pBt, nOrig) ){.
13c6a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53          return S
13c6b 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
13c6c 50 54 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  PT;.      }.    
13c6d 20 20 69 66 28 20 6e 4f 72 69 67 3d 3d 50 45 4e    if( nOrig==PEN
13c6e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
13c6f 42 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 6e  Bt) ){.        n
13c70 4f 72 69 67 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a  Orig--;.      }.
13c71 20 20 20 20 20 20 6e 46 72 65 65 20 3d 20 67 65        nFree = ge
13c72 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
13c73 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b  ge1->aData[36]);
13c74 0a 20 20 20 20 20 20 6e 50 74 72 6d 61 70 20 3d  .      nPtrmap =
13c75 20 28 6e 46 72 65 65 2d 6e 4f 72 69 67 2b 50 54   (nFree-nOrig+PT
13c76 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74 2c  RMAP_PAGENO(pBt,
13c77 20 6e 4f 72 69 67 29 2b 70 67 73 7a 2f 35 29 2f   nOrig)+pgsz/5)/
13c78 28 70 67 73 7a 2f 35 29 3b 0a 20 20 20 20 20 20  (pgsz/5);.      
13c79 6e 46 69 6e 20 3d 20 6e 4f 72 69 67 20 2d 20 6e  nFin = nOrig - n
13c7a 46 72 65 65 20 2d 20 6e 50 74 72 6d 61 70 3b 0a  Free - nPtrmap;.
13c7b 20 20 20 20 20 20 69 66 28 20 6e 4f 72 69 67 3e        if( nOrig>
13c7c 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
13c7d 45 28 70 42 74 29 20 26 26 20 6e 46 69 6e 3c 3d  E(pBt) && nFin<=
13c7e 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
13c7f 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  E(pBt) ){.      
13c80 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 20 20    nFin--;.      
13c81 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 50  }.      while( P
13c82 54 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74  TRMAP_ISPAGE(pBt
13c83 2c 20 6e 46 69 6e 29 20 7c 7c 20 6e 46 69 6e 3d  , nFin) || nFin=
13c84 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
13c85 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
13c86 20 20 20 6e 46 69 6e 2d 2d 3b 0a 20 20 20 20 20     nFin--;.     
13c87 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 68   }.    }..    wh
13c88 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ile( rc==SQLITE_
13c89 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
13c8a 20 69 6e 63 72 56 61 63 75 75 6d 53 74 65 70 28   incrVacuumStep(
13c8b 70 42 74 2c 20 6e 46 69 6e 29 3b 0a 20 20 20 20  pBt, nFin);.    
13c8c 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  }.    if( rc==SQ
13c8d 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
13c8e 20 20 20 61 73 73 65 72 74 28 6e 46 69 6e 3d 3d     assert(nFin==
13c8f 30 20 7c 7c 20 70 42 74 2d 3e 6e 54 72 75 6e 63  0 || pBt->nTrunc
13c90 3d 3d 30 20 7c 7c 20 6e 46 69 6e 3c 3d 70 42 74  ==0 || nFin<=pBt
13c91 2d 3e 6e 54 72 75 6e 63 29 3b 0a 20 20 20 20 20  ->nTrunc);.     
13c92 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
13c93 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e  .      if( pBt->
13c94 6e 54 72 75 6e 63 20 26 26 20 6e 46 69 6e 20 29  nTrunc && nFin )
13c95 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
13c96 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
13c97 28 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e 70 44  (pBt->pPage1->pD
13c98 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20  bPage);.        
13c99 70 75 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70  put4byte(&pBt->p
13c9a 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d  Page1->aData[32]
13c9b 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 75  , 0);.        pu
13c9c 74 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61  t4byte(&pBt->pPa
13c9d 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 2c 20  ge1->aData[36], 
13c9e 30 29 3b 0a 20 20 20 20 20 20 20 20 70 42 74 2d  0);.        pBt-
13c9f 3e 6e 54 72 75 6e 63 20 3d 20 6e 46 69 6e 3b 0a  >nTrunc = nFin;.
13ca0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
13ca1 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13ca2 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  _OK ){.      sql
13ca3 69 74 65 33 50 61 67 65 72 52 6f 6c 6c 62 61 63  ite3PagerRollbac
13ca4 6b 28 70 50 61 67 65 72 29 3b 0a 20 20 20 20 7d  k(pPager);.    }
13ca5 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
13ca6 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
13ca7 20 2a 70 6e 54 72 75 6e 63 20 3d 20 70 42 74 2d   *pnTrunc = pBt-
13ca8 3e 6e 54 72 75 6e 63 3b 0a 20 20 20 20 70 42 74  >nTrunc;.    pBt
13ca9 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20 20  ->nTrunc = 0;.  
13caa 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 52 65 66  }.  assert( nRef
13cab 3d 3d 73 71 6c 69 74 65 33 50 61 67 65 72 52 65  ==sqlite3PagerRe
13cac 66 63 6f 75 6e 74 28 70 50 61 67 65 72 29 20 29  fcount(pPager) )
13cad 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
13cae 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66 6e 64  ..#endif /* ifnd
13caf 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
13cb0 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a 2f 2a  UTOVACUUM */../*
13cb1 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
13cb2 20 64 6f 65 73 20 74 68 65 20 66 69 72 73 74 20   does the first 
13cb3 70 68 61 73 65 20 6f 66 20 61 20 74 77 6f 2d 70  phase of a two-p
13cb4 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20 20 54 68  hase commit.  Th
13cb5 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 63 61  is routine.** ca
13cb6 75 73 65 73 20 61 20 72 6f 6c 6c 62 61 63 6b 20  uses a rollback 
13cb7 6a 6f 75 72 6e 61 6c 20 74 6f 20 62 65 20 63 72  journal to be cr
13cb8 65 61 74 65 64 20 28 69 66 20 69 74 20 64 6f 65  eated (if it doe
13cb9 73 20 6e 6f 74 20 61 6c 72 65 61 64 79 20 65 78  s not already ex
13cba 69 73 74 29 0a 2a 2a 20 61 6e 64 20 70 6f 70 75  ist).** and popu
13cbb 6c 61 74 65 64 20 77 69 74 68 20 65 6e 6f 75 67  lated with enoug
13cbc 68 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 6f  h information so
13cbd 20 74 68 61 74 20 69 66 20 61 20 70 6f 77 65 72   that if a power
13cbe 20 6c 6f 73 73 20 6f 63 63 75 72 73 0a 2a 2a 20   loss occurs.** 
13cbf 74 68 65 20 64 61 74 61 62 61 73 65 20 63 61 6e  the database can
13cc0 20 62 65 20 72 65 73 74 6f 72 65 64 20 74 6f 20   be restored to 
13cc1 69 74 73 20 6f 72 69 67 69 6e 61 6c 20 73 74 61  its original sta
13cc2 74 65 20 62 79 20 70 6c 61 79 69 6e 67 20 62 61  te by playing ba
13cc3 63 6b 0a 2a 2a 20 74 68 65 20 6a 6f 75 72 6e 61  ck.** the journa
13cc4 6c 2e 20 20 54 68 65 6e 20 74 68 65 20 63 6f 6e  l.  Then the con
13cc5 74 65 6e 74 73 20 6f 66 20 74 68 65 20 6a 6f 75  tents of the jou
13cc6 72 6e 61 6c 20 61 72 65 20 66 6c 75 73 68 65 64  rnal are flushed
13cc7 20 6f 75 74 20 74 6f 0a 2a 2a 20 74 68 65 20 64   out to.** the d
13cc8 69 73 6b 2e 20 20 41 66 74 65 72 20 74 68 65 20  isk.  After the 
13cc9 6a 6f 75 72 6e 61 6c 20 69 73 20 73 61 66 65 6c  journal is safel
13cca 79 20 6f 6e 20 6f 78 69 64 65 2c 20 74 68 65 20  y on oxide, the 
13ccb 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 0a 2a  changes to the.*
13ccc 2a 20 64 61 74 61 62 61 73 65 20 61 72 65 20 77  * database are w
13ccd 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65 20  ritten into the 
13cce 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61 6e  database file an
13ccf 64 20 66 6c 75 73 68 65 64 20 74 6f 20 6f 78 69  d flushed to oxi
13cd0 64 65 2e 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e  de..** At the en
13cd1 64 20 6f 66 20 74 68 69 73 20 63 61 6c 6c 2c 20  d of this call, 
13cd2 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75  the rollback jou
13cd3 72 6e 61 6c 20 73 74 69 6c 6c 20 65 78 69 73 74  rnal still exist
13cd4 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 64 69 73 6b  s on the.** disk
13cd5 20 61 6e 64 20 77 65 20 61 72 65 20 73 74 69 6c   and we are stil
13cd6 6c 20 68 6f 6c 64 69 6e 67 20 61 6c 6c 20 6c 6f  l holding all lo
13cd7 63 6b 73 2c 20 73 6f 20 74 68 65 20 74 72 61 6e  cks, so the tran
13cd8 73 61 63 74 69 6f 6e 20 68 61 73 20 6e 6f 74 0a  saction has not.
13cd9 2a 2a 20 63 6f 6d 6d 69 74 74 65 64 2e 20 20 53  ** committed.  S
13cda 65 65 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  ee sqlite3BtreeC
13cdb 6f 6d 6d 69 74 28 29 20 66 6f 72 20 74 68 65 20  ommit() for the 
13cdc 73 65 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20  second phase of 
13cdd 74 68 65 0a 2a 2a 20 63 6f 6d 6d 69 74 20 70 72  the.** commit pr
13cde 6f 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ocess..**.** Thi
13cdf 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d 6f  s call is a no-o
13ce0 70 20 69 66 20 6e 6f 20 77 72 69 74 65 2d 74 72  p if no write-tr
13ce1 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 63 75 72  ansaction is cur
13ce2 72 65 6e 74 6c 79 20 61 63 74 69 76 65 20 6f 6e  rently active on
13ce3 20 70 42 74 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65   pBt..**.** Othe
13ce4 72 77 69 73 65 2c 20 73 79 6e 63 20 74 68 65 20  rwise, sync the 
13ce5 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 66 6f  database file fo
13ce6 72 20 74 68 65 20 62 74 72 65 65 20 70 42 74 2e  r the btree pBt.
13ce7 20 7a 4d 61 73 74 65 72 20 70 6f 69 6e 74 73 20   zMaster points 
13ce8 74 6f 0a 2a 2a 20 74 68 65 20 6e 61 6d 65 20 6f  to.** the name o
13ce9 66 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  f a master journ
13cea 61 6c 20 66 69 6c 65 20 74 68 61 74 20 73 68 6f  al file that sho
13ceb 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 69  uld be written i
13cec 6e 74 6f 20 74 68 65 0a 2a 2a 20 69 6e 64 69 76  nto the.** indiv
13ced 69 64 75 61 6c 20 6a 6f 75 72 6e 61 6c 20 66 69  idual journal fi
13cee 6c 65 2c 20 6f 72 20 69 73 20 4e 55 4c 4c 2c 20  le, or is NULL, 
13cef 69 6e 64 69 63 61 74 69 6e 67 20 6e 6f 20 6d 61  indicating no ma
13cf0 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  ster journal fil
13cf1 65 20 0a 2a 2a 20 28 73 69 6e 67 6c 65 20 64 61  e .** (single da
13cf2 74 61 62 61 73 65 20 74 72 61 6e 73 61 63 74 69  tabase transacti
13cf3 6f 6e 29 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20  on)..**.** When 
13cf4 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2c 20  this is called, 
13cf5 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e  the master journ
13cf6 61 6c 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  al should alread
13cf7 79 20 68 61 76 65 20 62 65 65 6e 0a 2a 2a 20 63  y have been.** c
13cf8 72 65 61 74 65 64 2c 20 70 6f 70 75 6c 61 74 65  reated, populate
13cf9 64 20 77 69 74 68 20 74 68 69 73 20 6a 6f 75 72  d with this jour
13cfa 6e 61 6c 20 70 6f 69 6e 74 65 72 20 61 6e 64 20  nal pointer and 
13cfb 73 79 6e 63 65 64 20 74 6f 20 64 69 73 6b 2e 0a  synced to disk..
13cfc 2a 2a 0a 2a 2a 20 4f 6e 63 65 20 74 68 69 73 20  **.** Once this 
13cfd 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73 20 72  is routine has r
13cfe 65 74 75 72 6e 65 64 2c 20 74 68 65 20 6f 6e 6c  eturned, the onl
13cff 79 20 74 68 69 6e 67 20 72 65 71 75 69 72 65 64  y thing required
13d00 20 74 6f 20 63 6f 6d 6d 69 74 0a 2a 2a 20 74 68   to commit.** th
13d01 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  e write-transact
13d02 69 6f 6e 20 66 6f 72 20 74 68 69 73 20 64 61 74  ion for this dat
13d03 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 74 6f  abase file is to
13d04 20 64 65 6c 65 74 65 20 74 68 65 20 6a 6f 75 72   delete the jour
13d05 6e 61 6c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  nal..*/.SQLITE_P
13d06 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13d07 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
13d08 73 65 4f 6e 65 28 42 74 72 65 65 20 2a 70 2c 20  seOne(Btree *p, 
13d09 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73  const char *zMas
13d0a 74 65 72 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ter){.  int rc =
13d0b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66   SQLITE_OK;.  if
13d0c 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
13d0d 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
13d0e 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d   BtShared *pBt =
13d0f 20 70 2d 3e 70 42 74 3b 0a 20 20 20 20 50 67 6e   p->pBt;.    Pgn
13d10 6f 20 6e 54 72 75 6e 63 20 3d 20 30 3b 0a 20 20  o nTrunc = 0;.  
13d11 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
13d12 74 65 72 28 70 29 3b 0a 20 20 20 20 70 42 74 2d  ter(p);.    pBt-
13d13 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 23 69 66  >db = p->db;.#if
13d14 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13d15 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
13d16 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
13d17 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  uum ){.      rc 
13d18 3d 20 61 75 74 6f 56 61 63 75 75 6d 43 6f 6d 6d  = autoVacuumComm
13d19 69 74 28 70 42 74 2c 20 26 6e 54 72 75 6e 63 29  it(pBt, &nTrunc)
13d1a 3b 20 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  ; .      if( rc!
13d1b 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
13d1c 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
13d1d 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 20 20  eeLeave(p);.    
13d1e 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
13d1f 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e       }.    }.#en
13d20 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  dif.    rc = sql
13d21 69 74 65 33 50 61 67 65 72 43 6f 6d 6d 69 74 50  ite3PagerCommitP
13d22 68 61 73 65 4f 6e 65 28 70 42 74 2d 3e 70 50 61  haseOne(pBt->pPa
13d23 67 65 72 2c 20 7a 4d 61 73 74 65 72 2c 20 6e 54  ger, zMaster, nT
13d24 72 75 6e 63 2c 20 30 29 3b 0a 20 20 20 20 73 71  runc, 0);.    sq
13d25 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
13d26 70 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  p);.  }.  return
13d27 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f   rc;.}../*.** Co
13d28 6d 6d 69 74 20 74 68 65 20 74 72 61 6e 73 61 63  mmit the transac
13d29 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c 79 20 69  tion currently i
13d2a 6e 20 70 72 6f 67 72 65 73 73 2e 0a 2a 2a 0a 2a  n progress..**.*
13d2b 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69  * This routine i
13d2c 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 73 65  mplements the se
13d2d 63 6f 6e 64 20 70 68 61 73 65 20 6f 66 20 61 20  cond phase of a 
13d2e 32 2d 70 68 61 73 65 20 63 6f 6d 6d 69 74 2e 20  2-phase commit. 
13d2f 20 54 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 42   The.** sqlite3B
13d30 74 72 65 65 53 79 6e 63 28 29 20 72 6f 75 74 69  treeSync() routi
13d31 6e 65 20 64 6f 65 73 20 74 68 65 20 66 69 72 73  ne does the firs
13d32 74 20 70 68 61 73 65 20 61 6e 64 20 73 68 6f 75  t phase and shou
13d33 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 0a 2a 2a  ld be invoked.**
13d34 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69 6e   prior to callin
13d35 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20  g this routine. 
13d36 20 54 68 65 20 73 71 6c 69 74 65 33 42 74 72 65   The sqlite3Btre
13d37 65 53 79 6e 63 28 29 20 72 6f 75 74 69 6e 65 20  eSync() routine 
13d38 64 69 64 0a 2a 2a 20 61 6c 6c 20 74 68 65 20 77  did.** all the w
13d39 6f 72 6b 20 6f 66 20 77 72 69 74 69 6e 67 20 69  ork of writing i
13d3a 6e 66 6f 72 6d 61 74 69 6f 6e 20 6f 75 74 20 74  nformation out t
13d3b 6f 20 64 69 73 6b 20 61 6e 64 20 66 6c 75 73 68  o disk and flush
13d3c 69 6e 67 20 74 68 65 0a 2a 2a 20 63 6f 6e 74 65  ing the.** conte
13d3d 6e 74 73 20 73 6f 20 74 68 61 74 20 74 68 65 79  nts so that they
13d3e 20 61 72 65 20 77 72 69 74 74 65 6e 20 6f 6e 74   are written ont
13d3f 6f 20 74 68 65 20 64 69 73 6b 20 70 6c 61 74 74  o the disk platt
13d40 65 72 2e 20 20 41 6c 6c 20 74 68 69 73 0a 2a 2a  er.  All this.**
13d41 20 72 6f 75 74 69 6e 65 20 68 61 73 20 74 6f 20   routine has to 
13d42 64 6f 20 69 73 20 64 65 6c 65 74 65 20 6f 72 20  do is delete or 
13d43 74 72 75 6e 63 61 74 65 20 74 68 65 20 72 6f 6c  truncate the rol
13d44 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 0a 2a 2a  lback journal.**
13d45 20 28 77 68 69 63 68 20 63 61 75 73 65 73 20 74   (which causes t
13d46 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 74  he transaction t
13d47 6f 20 63 6f 6d 6d 69 74 29 20 61 6e 64 20 64 72  o commit) and dr
13d48 6f 70 20 6c 6f 63 6b 73 2e 0a 2a 2a 0a 2a 2a 20  op locks..**.** 
13d49 54 68 69 73 20 77 69 6c 6c 20 72 65 6c 65 61 73  This will releas
13d4a 65 20 74 68 65 20 77 72 69 74 65 20 6c 6f 63 6b  e the write lock
13d4b 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   on the database
13d4c 20 66 69 6c 65 2e 20 20 49 66 20 74 68 65 72 65   file.  If there
13d4d 0a 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69 76  .** are no activ
13d4e 65 20 63 75 72 73 6f 72 73 2c 20 69 74 20 61 6c  e cursors, it al
13d4f 73 6f 20 72 65 6c 65 61 73 65 73 20 74 68 65 20  so releases the 
13d50 72 65 61 64 20 6c 6f 63 6b 2e 0a 2a 2f 0a 53 51  read lock..*/.SQ
13d51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
13d52 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d   sqlite3BtreeCom
13d53 6d 69 74 50 68 61 73 65 54 77 6f 28 42 74 72 65  mitPhaseTwo(Btre
13d54 65 20 2a 70 29 7b 0a 20 20 42 74 53 68 61 72 65  e *p){.  BtShare
13d55 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b  d *pBt = p->pBt;
13d56 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
13d57 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d  Enter(p);.  pBt-
13d58 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 62  >db = p->db;.  b
13d59 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70 29  treeIntegrity(p)
13d5a 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 68  ;..  /* If the h
13d5b 61 6e 64 6c 65 20 68 61 73 20 61 20 77 72 69 74  andle has a writ
13d5c 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70  e-transaction op
13d5d 65 6e 2c 20 63 6f 6d 6d 69 74 20 74 68 65 20 73  en, commit the s
13d5e 68 61 72 65 64 2d 62 74 72 65 65 73 20 0a 20 20  hared-btrees .  
13d5f 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61  ** transaction a
13d60 6e 64 20 73 65 74 20 74 68 65 20 73 68 61 72 65  nd set the share
13d61 64 20 73 74 61 74 65 20 74 6f 20 54 52 41 4e 53  d state to TRANS
13d62 5f 52 45 41 44 2e 0a 20 20 2a 2f 0a 20 20 69 66  _READ..  */.  if
13d63 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d 54 52  ( p->inTrans==TR
13d64 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20 20  ANS_WRITE ){.   
13d65 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 61 73 73   int rc;.    ass
13d66 65 72 74 28 20 70 42 74 2d 3e 69 6e 54 72 61 6e  ert( pBt->inTran
13d67 73 61 63 74 69 6f 6e 3d 3d 54 52 41 4e 53 5f 57  saction==TRANS_W
13d68 52 49 54 45 20 29 3b 0a 20 20 20 20 61 73 73 65  RITE );.    asse
13d69 72 74 28 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61  rt( pBt->nTransa
13d6a 63 74 69 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 72  ction>0 );.    r
13d6b 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
13d6c 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 70  CommitPhaseTwo(p
13d6d 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 20  Bt->pPager);.   
13d6e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13d6f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  OK ){.      sqli
13d70 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
13d71 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
13d72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 74  c;.    }.    pBt
13d73 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20  ->inTransaction 
13d74 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a 20 20  = TRANS_READ;.  
13d75 20 20 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20    pBt->inStmt = 
13d76 30 3b 0a 20 20 7d 0a 20 20 75 6e 6c 6f 63 6b 41  0;.  }.  unlockA
13d77 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20  llTables(p);..  
13d78 2f 2a 20 49 66 20 74 68 65 20 68 61 6e 64 6c 65  /* If the handle
13d79 20 68 61 73 20 61 6e 79 20 6b 69 6e 64 20 6f 66   has any kind of
13d7a 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
13d7b 6e 2c 20 64 65 63 72 65 6d 65 6e 74 20 74 68 65  n, decrement the
13d7c 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a   transaction.  *
13d7d 2a 20 63 6f 75 6e 74 20 6f 66 20 74 68 65 20 73  * count of the s
13d7e 68 61 72 65 64 20 62 74 72 65 65 2e 20 49 66 20  hared btree. If 
13d7f 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  the transaction 
13d80 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 30 2c  count reaches 0,
13d81 20 73 65 74 0a 20 20 2a 2a 20 74 68 65 20 73 68   set.  ** the sh
13d82 61 72 65 64 20 73 74 61 74 65 20 74 6f 20 54 52  ared state to TR
13d83 41 4e 53 5f 4e 4f 4e 45 2e 20 54 68 65 20 75 6e  ANS_NONE. The un
13d84 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65  lockBtreeIfUnuse
13d85 64 28 29 20 63 61 6c 6c 20 62 65 6c 6f 77 0a 20  d() call below. 
13d86 20 2a 2a 20 77 69 6c 6c 20 75 6e 6c 6f 63 6b 20   ** will unlock 
13d87 74 68 65 20 70 61 67 65 72 2e 0a 20 20 2a 2f 0a  the pager..  */.
13d88 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73    if( p->inTrans
13d89 21 3d 54 52 41 4e 53 5f 4e 4f 4e 45 20 29 7b 0a  !=TRANS_NONE ){.
13d8a 20 20 20 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61      pBt->nTransa
13d8b 63 74 69 6f 6e 2d 2d 3b 0a 20 20 20 20 69 66 28  ction--;.    if(
13d8c 20 30 3d 3d 70 42 74 2d 3e 6e 54 72 61 6e 73 61   0==pBt->nTransa
13d8d 63 74 69 6f 6e 20 29 7b 0a 20 20 20 20 20 20 70  ction ){.      p
13d8e 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
13d8f 6e 20 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a  n = TRANS_NONE;.
13d90 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
13d91 53 65 74 20 74 68 65 20 68 61 6e 64 6c 65 73 20  Set the handles 
13d92 63 75 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74  current transact
13d93 69 6f 6e 20 73 74 61 74 65 20 74 6f 20 54 52 41  ion state to TRA
13d94 4e 53 5f 4e 4f 4e 45 20 61 6e 64 20 75 6e 6c 6f  NS_NONE and unlo
13d95 63 6b 0a 20 20 2a 2a 20 74 68 65 20 70 61 67 65  ck.  ** the page
13d96 72 20 69 66 20 74 68 69 73 20 63 61 6c 6c 20 63  r if this call c
13d97 6c 6f 73 65 64 20 74 68 65 20 6f 6e 6c 79 20 72  losed the only r
13d98 65 61 64 20 6f 72 20 77 72 69 74 65 20 74 72 61  ead or write tra
13d99 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  nsaction..  */. 
13d9a 20 70 2d 3e 69 6e 54 72 61 6e 73 20 3d 20 54 52   p->inTrans = TR
13d9b 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20 75 6e 6c 6f  ANS_NONE;.  unlo
13d9c 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64 28  ckBtreeIfUnused(
13d9d 70 42 74 29 3b 0a 0a 20 20 62 74 72 65 65 49 6e  pBt);..  btreeIn
13d9e 74 65 67 72 69 74 79 28 70 29 3b 0a 20 20 73 71  tegrity(p);.  sq
13d9f 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
13da0 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  p);.  return SQL
13da1 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
13da2 20 44 6f 20 62 6f 74 68 20 70 68 61 73 65 73 20   Do both phases 
13da3 6f 66 20 61 20 63 6f 6d 6d 69 74 2e 0a 2a 2f 0a  of a commit..*/.
13da4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
13da5 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
13da6 6f 6d 6d 69 74 28 42 74 72 65 65 20 2a 70 29 7b  ommit(Btree *p){
13da7 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c  .  int rc;.  sql
13da8 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
13da9 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
13daa 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
13dab 65 4f 6e 65 28 70 2c 20 30 29 3b 0a 20 20 69 66  eOne(p, 0);.  if
13dac 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
13dad 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
13dae 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
13daf 61 73 65 54 77 6f 28 70 29 3b 0a 20 20 7d 0a 20  aseTwo(p);.  }. 
13db0 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
13db1 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ve(p);.  return 
13db2 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e  rc;.}..#ifndef N
13db3 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 52 65 74 75  DEBUG./*.** Retu
13db4 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
13db5 20 77 72 69 74 65 2d 63 75 72 73 6f 72 73 20 6f   write-cursors o
13db6 70 65 6e 20 6f 6e 20 74 68 69 73 20 68 61 6e 64  pen on this hand
13db7 6c 65 2e 20 54 68 69 73 20 69 73 20 66 6f 72 20  le. This is for 
13db8 75 73 65 0a 2a 2a 20 69 6e 20 61 73 73 65 72 74  use.** in assert
13db9 28 29 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20  () expressions, 
13dba 73 6f 20 69 74 20 69 73 20 6f 6e 6c 79 20 63 6f  so it is only co
13dbb 6d 70 69 6c 65 64 20 69 66 20 4e 44 45 42 55 47  mpiled if NDEBUG
13dbc 20 69 73 20 6e 6f 74 0a 2a 2a 20 64 65 66 69 6e   is not.** defin
13dbd 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68  ed..**.** For th
13dbe 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 74 68  e purposes of th
13dbf 69 73 20 72 6f 75 74 69 6e 65 2c 20 61 20 77 72  is routine, a wr
13dc0 69 74 65 2d 63 75 72 73 6f 72 20 69 73 20 61 6e  ite-cursor is an
13dc1 79 20 63 75 72 73 6f 72 20 74 68 61 74 0a 2a 2a  y cursor that.**
13dc2 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 77   is capable of w
13dc3 72 69 74 69 6e 67 20 74 6f 20 74 68 65 20 64 61  riting to the da
13dc4 74 61 62 73 65 2e 20 20 54 68 61 74 20 6d 65 61  tabse.  That mea
13dc5 6e 73 20 74 68 65 20 63 75 72 73 6f 72 20 77 61  ns the cursor wa
13dc6 73 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 6c 79 20  s.** originally 
13dc7 6f 70 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69  opened for writi
13dc8 6e 67 20 61 6e 64 20 74 68 65 20 63 75 72 73 6f  ng and the curso
13dc9 72 20 68 61 73 20 6e 6f 74 20 62 65 20 64 69 73  r has not be dis
13dca 61 62 6c 65 64 0a 2a 2a 20 62 79 20 68 61 76 69  abled.** by havi
13dcb 6e 67 20 69 74 73 20 73 74 61 74 65 20 63 68 61  ng its state cha
13dcc 6e 67 65 64 20 74 6f 20 43 55 52 53 4f 52 5f 46  nged to CURSOR_F
13dcd 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  AULT..*/.static 
13dce 69 6e 74 20 63 6f 75 6e 74 57 72 69 74 65 43 75  int countWriteCu
13dcf 72 73 6f 72 73 28 42 74 53 68 61 72 65 64 20 2a  rsors(BtShared *
13dd0 70 42 74 29 7b 0a 20 20 42 74 43 75 72 73 6f 72  pBt){.  BtCursor
13dd1 20 2a 70 43 75 72 3b 0a 20 20 69 6e 74 20 72 20   *pCur;.  int r 
13dd2 3d 20 30 3b 0a 20 20 66 6f 72 28 70 43 75 72 3d  = 0;.  for(pCur=
13dd3 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70 43  pBt->pCursor; pC
13dd4 75 72 3b 20 70 43 75 72 3d 70 43 75 72 2d 3e 70  ur; pCur=pCur->p
13dd5 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 70  Next){.    if( p
13dd6 43 75 72 2d 3e 77 72 46 6c 61 67 20 26 26 20 70  Cur->wrFlag && p
13dd7 43 75 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52  Cur->eState!=CUR
13dd8 53 4f 52 5f 46 41 55 4c 54 20 29 20 72 2b 2b 3b  SOR_FAULT ) r++;
13dd9 20 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72   .  }.  return r
13dda 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  ;.}.#endif../*.*
13ddb 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73  * This routine s
13ddc 65 74 73 20 74 68 65 20 73 74 61 74 65 20 74 6f  ets the state to
13ddd 20 43 55 52 53 4f 52 5f 46 41 55 4c 54 20 61 6e   CURSOR_FAULT an
13dde 64 20 74 68 65 20 65 72 72 6f 72 0a 2a 2a 20 63  d the error.** c
13ddf 6f 64 65 20 74 6f 20 65 72 72 43 6f 64 65 20 66  ode to errCode f
13de0 6f 72 20 65 76 65 72 79 20 63 75 72 73 6f 72 20  or every cursor 
13de1 6f 6e 20 42 74 53 68 61 72 65 64 20 74 68 61 74  on BtShared that
13de2 20 70 42 74 72 65 65 0a 2a 2a 20 72 65 66 65 72   pBtree.** refer
13de3 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 45 76 65  ences..**.** Eve
13de4 72 79 20 63 75 72 73 6f 72 20 69 73 20 74 72 69  ry cursor is tri
13de5 70 70 65 64 2c 20 69 6e 63 6c 75 64 69 6e 67 20  pped, including 
13de6 63 75 72 73 6f 72 73 20 74 68 61 74 20 62 65 6c  cursors that bel
13de7 6f 6e 67 0a 2a 2a 20 74 6f 20 6f 74 68 65 72 20  ong.** to other 
13de8 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74  database connect
13de9 69 6f 6e 73 20 74 68 61 74 20 68 61 70 70 65 6e  ions that happen
13dea 20 74 6f 20 62 65 20 73 68 61 72 69 6e 67 0a 2a   to be sharing.*
13deb 2a 20 74 68 65 20 63 61 63 68 65 20 77 69 74 68  * the cache with
13dec 20 70 42 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54   pBtree..**.** T
13ded 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 74 73  his routine gets
13dee 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 72   called when a r
13def 6f 6c 6c 62 61 63 6b 20 6f 63 63 75 72 73 2e 0a  ollback occurs..
13df0 2a 2a 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 75  ** All cursors u
13df1 73 69 6e 67 20 74 68 65 20 73 61 6d 65 20 63 61  sing the same ca
13df2 63 68 65 20 6d 75 73 74 20 62 65 20 74 72 69 70  che must be trip
13df3 70 65 64 0a 2a 2a 20 74 6f 20 70 72 65 76 65 6e  ped.** to preven
13df4 74 20 74 68 65 6d 20 66 72 6f 6d 20 74 72 79 69  t them from tryi
13df5 6e 67 20 74 6f 20 75 73 65 20 74 68 65 20 62 74  ng to use the bt
13df6 72 65 65 20 61 66 74 65 72 0a 2a 2a 20 74 68 65  ree after.** the
13df7 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 54 68 65 20   rollback.  The 
13df8 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61 76  rollback may hav
13df9 65 20 64 65 6c 65 74 65 64 20 74 61 62 6c 65 73  e deleted tables
13dfa 0a 2a 2a 20 6f 72 20 6d 6f 76 65 64 20 72 6f 6f  .** or moved roo
13dfb 74 20 70 61 67 65 73 2c 20 73 6f 20 69 74 20 69  t pages, so it i
13dfc 73 20 6e 6f 74 20 73 75 66 66 69 63 69 65 6e 74  s not sufficient
13dfd 20 74 6f 0a 2a 2a 20 73 61 76 65 20 74 68 65 20   to.** save the 
13dfe 73 74 61 74 65 20 6f 66 20 74 68 65 20 63 75 72  state of the cur
13dff 73 6f 72 2e 20 20 54 68 65 20 63 75 72 73 6f 72  sor.  The cursor
13e00 20 6d 75 73 74 20 62 65 0a 2a 2a 20 69 6e 76 61   must be.** inva
13e01 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49  lidated..*/.SQLI
13e02 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
13e03 73 71 6c 69 74 65 33 42 74 72 65 65 54 72 69 70  sqlite3BtreeTrip
13e04 41 6c 6c 43 75 72 73 6f 72 73 28 42 74 72 65 65  AllCursors(Btree
13e05 20 2a 70 42 74 72 65 65 2c 20 69 6e 74 20 65 72   *pBtree, int er
13e06 72 43 6f 64 65 29 7b 0a 20 20 42 74 43 75 72 73  rCode){.  BtCurs
13e07 6f 72 20 2a 70 3b 0a 20 20 73 71 6c 69 74 65 33  or *p;.  sqlite3
13e08 42 74 72 65 65 45 6e 74 65 72 28 70 42 74 72 65  BtreeEnter(pBtre
13e09 65 29 3b 0a 20 20 66 6f 72 28 70 3d 70 42 74 72  e);.  for(p=pBtr
13e0a 65 65 2d 3e 70 42 74 2d 3e 70 43 75 72 73 6f 72  ee->pBt->pCursor
13e0b 3b 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29  ; p; p=p->pNext)
13e0c 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  {.    int i;.   
13e0d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
13e0e 61 72 43 75 72 73 6f 72 28 70 29 3b 0a 20 20 20  arCursor(p);.   
13e0f 20 70 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52   p->eState = CUR
13e10 53 4f 52 5f 46 41 55 4c 54 3b 0a 20 20 20 20 70  SOR_FAULT;.    p
13e11 2d 3e 73 6b 69 70 20 3d 20 65 72 72 43 6f 64 65  ->skip = errCode
13e12 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
13e13 3c 3d 70 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29  <=p->iPage; i++)
13e14 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  {.      releaseP
13e15 61 67 65 28 70 2d 3e 61 70 50 61 67 65 5b 69 5d  age(p->apPage[i]
13e16 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61 70 50 61  );.      p->apPa
13e17 67 65 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 7d  ge[i] = 0;.    }
13e18 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
13e19 72 65 65 4c 65 61 76 65 28 70 42 74 72 65 65 29  reeLeave(pBtree)
13e1a 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62  ;.}../*.** Rollb
13e1b 61 63 6b 20 74 68 65 20 74 72 61 6e 73 61 63 74  ack the transact
13e1c 69 6f 6e 20 69 6e 20 70 72 6f 67 72 65 73 73 2e  ion in progress.
13e1d 20 20 41 6c 6c 20 63 75 72 73 6f 72 73 20 77 69    All cursors wi
13e1e 6c 6c 20 62 65 0a 2a 2a 20 69 6e 76 61 6c 69 64  ll be.** invalid
13e1f 65 64 20 62 79 20 74 68 69 73 20 6f 70 65 72 61  ed by this opera
13e20 74 69 6f 6e 2e 20 20 41 6e 79 20 61 74 74 65 6d  tion.  Any attem
13e21 70 74 20 74 6f 20 75 73 65 20 61 20 63 75 72 73  pt to use a curs
13e22 6f 72 0a 2a 2a 20 74 68 61 74 20 77 61 73 20 6f  or.** that was o
13e23 70 65 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e  pen at the begin
13e24 6e 69 6e 67 20 6f 66 20 74 68 69 73 20 6f 70 65  ning of this ope
13e25 72 61 74 69 6f 6e 20 77 69 6c 6c 20 72 65 73 75  ration will resu
13e26 6c 74 0a 2a 2a 20 69 6e 20 61 6e 20 65 72 72 6f  lt.** in an erro
13e27 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 69  r..**.** This wi
13e28 6c 6c 20 72 65 6c 65 61 73 65 20 74 68 65 20 77  ll release the w
13e29 72 69 74 65 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite lock on the
13e2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20   database file. 
13e2b 20 49 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65   If there.** are
13e2c 20 6e 6f 20 61 63 74 69 76 65 20 63 75 72 73 6f   no active curso
13e2d 72 73 2c 20 69 74 20 61 6c 73 6f 20 72 65 6c 65  rs, it also rele
13e2e 61 73 65 73 20 74 68 65 20 72 65 61 64 20 6c 6f  ases the read lo
13e2f 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ck..*/.SQLITE_PR
13e30 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
13e31 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 42  3BtreeRollback(B
13e32 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  tree *p){.  int 
13e33 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  rc;.  BtShared *
13e34 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20  pBt = p->pBt;.  
13e35 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 31 3b  MemPage *pPage1;
13e36 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
13e37 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d  Enter(p);.  pBt-
13e38 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72  >db = p->db;.  r
13e39 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73 6f  c = saveAllCurso
13e3a 72 73 28 70 42 74 2c 20 30 2c 20 30 29 3b 0a 23  rs(pBt, 0, 0);.#
13e3b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
13e3c 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
13e3d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
13e3e 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  _OK ){.    /* Th
13e3f 69 73 20 69 73 20 61 20 68 6f 72 72 69 62 6c 65  is is a horrible
13e40 20 73 69 74 75 61 74 69 6f 6e 2e 20 41 6e 20 49   situation. An I
13e41 4f 20 6f 72 20 6d 61 6c 6c 6f 63 28 29 20 65 72  O or malloc() er
13e42 72 6f 72 20 6f 63 63 75 72 65 64 20 77 68 69 6c  ror occured whil
13e43 73 74 0a 20 20 20 20 2a 2a 20 74 72 79 69 6e 67  st.    ** trying
13e44 20 74 6f 20 73 61 76 65 20 63 75 72 73 6f 72 20   to save cursor 
13e45 70 6f 73 69 74 69 6f 6e 73 2e 20 49 66 20 74 68  positions. If th
13e46 69 73 20 69 73 20 61 6e 20 61 75 74 6f 6d 61 74  is is an automat
13e47 69 63 20 72 6f 6c 6c 62 61 63 6b 20 28 61 73 0a  ic rollback (as.
13e48 20 20 20 20 2a 2a 20 74 68 65 20 72 65 73 75 6c      ** the resul
13e49 74 20 6f 66 20 61 20 63 6f 6e 73 74 72 61 69 6e  t of a constrain
13e4a 74 2c 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c  t, malloc() fail
13e4b 75 72 65 20 6f 72 20 49 4f 20 65 72 72 6f 72 29  ure or IO error)
13e4c 20 74 68 65 6e 20 0a 20 20 20 20 2a 2a 20 74 68   then .    ** th
13e4d 65 20 63 61 63 68 65 20 6d 61 79 20 62 65 20 69  e cache may be i
13e4e 6e 74 65 72 6e 61 6c 6c 79 20 69 6e 63 6f 6e 73  nternally incons
13e4f 69 73 74 65 6e 74 20 28 6e 6f 74 20 63 6f 6e 74  istent (not cont
13e50 61 69 6e 20 76 61 6c 69 64 20 74 72 65 65 73 29  ain valid trees)
13e51 20 73 6f 0a 20 20 20 20 2a 2a 20 77 65 20 63 61   so.    ** we ca
13e52 6e 6e 6f 74 20 73 69 6d 70 6c 79 20 72 65 74 75  nnot simply retu
13e53 72 6e 20 74 68 65 20 65 72 72 6f 72 20 74 6f 20  rn the error to 
13e54 74 68 65 20 63 61 6c 6c 65 72 2e 20 49 6e 73 74  the caller. Inst
13e55 65 61 64 2c 20 61 62 6f 72 74 20 0a 20 20 20 20  ead, abort .    
13e56 2a 2a 20 61 6c 6c 20 71 75 65 72 69 65 73 20 74  ** all queries t
13e57 68 61 74 20 6d 61 79 20 62 65 20 75 73 69 6e 67  hat may be using
13e58 20 61 6e 79 20 6f 66 20 74 68 65 20 63 75 72 73   any of the curs
13e59 6f 72 73 20 74 68 61 74 20 66 61 69 6c 65 64 20  ors that failed 
13e5a 74 6f 20 73 61 76 65 2e 0a 20 20 20 20 2a 2f 0a  to save..    */.
13e5b 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
13e5c 54 72 69 70 41 6c 6c 43 75 72 73 6f 72 73 28 70  TripAllCursors(p
13e5d 2c 20 72 63 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  , rc);.  }.#endi
13e5e 66 0a 20 20 62 74 72 65 65 49 6e 74 65 67 72 69  f.  btreeIntegri
13e5f 74 79 28 70 29 3b 0a 20 20 75 6e 6c 6f 63 6b 41  ty(p);.  unlockA
13e60 6c 6c 54 61 62 6c 65 73 28 70 29 3b 0a 0a 20 20  llTables(p);..  
13e61 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  if( p->inTrans==
13e62 54 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20  TRANS_WRITE ){. 
13e63 20 20 20 69 6e 74 20 72 63 32 3b 0a 0a 23 69 66     int rc2;..#if
13e64 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
13e65 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20  _AUTOVACUUM.    
13e66 70 42 74 2d 3e 6e 54 72 75 6e 63 20 3d 20 30 3b  pBt->nTrunc = 0;
13e67 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 61 73 73  .#endif..    ass
13e68 65 72 74 28 20 54 52 41 4e 53 5f 57 52 49 54 45  ert( TRANS_WRITE
13e69 3d 3d 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63  ==pBt->inTransac
13e6a 74 69 6f 6e 20 29 3b 0a 20 20 20 20 72 63 32 20  tion );.    rc2 
13e6b 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 52 6f  = sqlite3PagerRo
13e6c 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70 50 61 67  llback(pBt->pPag
13e6d 65 72 29 3b 0a 20 20 20 20 69 66 28 20 72 63 32  er);.    if( rc2
13e6e 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
13e6f 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a 20       rc = rc2;. 
13e70 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65     }..    /* The
13e71 20 72 6f 6c 6c 62 61 63 6b 20 6d 61 79 20 68 61   rollback may ha
13e72 76 65 20 64 65 73 74 72 6f 79 65 64 20 74 68 65  ve destroyed the
13e73 20 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 76   pPage1->aData v
13e74 61 6c 75 65 2e 20 20 53 6f 0a 20 20 20 20 2a 2a  alue.  So.    **
13e75 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 42 74 72   call sqlite3Btr
13e76 65 65 47 65 74 50 61 67 65 28 29 20 6f 6e 20 70  eeGetPage() on p
13e77 61 67 65 20 31 20 61 67 61 69 6e 20 74 6f 20 6d  age 1 again to m
13e78 61 6b 65 0a 20 20 20 20 2a 2a 20 73 75 72 65 20  ake.    ** sure 
13e79 70 50 61 67 65 31 2d 3e 61 44 61 74 61 20 69 73  pPage1->aData is
13e7a 20 73 65 74 20 63 6f 72 72 65 63 74 6c 79 2e 20   set correctly. 
13e7b 2a 2f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  */.    if( sqlit
13e7c 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70  e3BtreeGetPage(p
13e7d 42 74 2c 20 31 2c 20 26 70 50 61 67 65 31 2c 20  Bt, 1, &pPage1, 
13e7e 30 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  0)==SQLITE_OK ){
13e7f 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
13e80 67 65 28 70 50 61 67 65 31 29 3b 0a 20 20 20 20  ge(pPage1);.    
13e81 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 63 6f  }.    assert( co
13e82 75 6e 74 57 72 69 74 65 43 75 72 73 6f 72 73 28  untWriteCursors(
13e83 70 42 74 29 3d 3d 30 20 29 3b 0a 20 20 20 20 70  pBt)==0 );.    p
13e84 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
13e85 6e 20 3d 20 54 52 41 4e 53 5f 52 45 41 44 3b 0a  n = TRANS_READ;.
13e86 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d 3e 69 6e    }..  if( p->in
13e87 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f 4e 4f 4e  Trans!=TRANS_NON
13e88 45 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  E ){.    assert(
13e89 20 70 42 74 2d 3e 6e 54 72 61 6e 73 61 63 74 69   pBt->nTransacti
13e8a 6f 6e 3e 30 20 29 3b 0a 20 20 20 20 70 42 74 2d  on>0 );.    pBt-
13e8b 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 2d 2d 3b  >nTransaction--;
13e8c 0a 20 20 20 20 69 66 28 20 30 3d 3d 70 42 74 2d  .    if( 0==pBt-
13e8d 3e 6e 54 72 61 6e 73 61 63 74 69 6f 6e 20 29 7b  >nTransaction ){
13e8e 0a 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 54 72  .      pBt->inTr
13e8f 61 6e 73 61 63 74 69 6f 6e 20 3d 20 54 52 41 4e  ansaction = TRAN
13e90 53 5f 4e 4f 4e 45 3b 0a 20 20 20 20 7d 0a 20 20  S_NONE;.    }.  
13e91 7d 0a 0a 20 20 70 2d 3e 69 6e 54 72 61 6e 73 20  }..  p->inTrans 
13e92 3d 20 54 52 41 4e 53 5f 4e 4f 4e 45 3b 0a 20 20  = TRANS_NONE;.  
13e93 70 42 74 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b  pBt->inStmt = 0;
13e94 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66  .  unlockBtreeIf
13e95 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 0a 20 20  Unused(pBt);..  
13e96 62 74 72 65 65 49 6e 74 65 67 72 69 74 79 28 70  btreeIntegrity(p
13e97 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
13e98 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
13e99 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
13e9a 20 53 74 61 72 74 20 61 20 73 74 61 74 65 6d 65   Start a stateme
13e9b 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  nt subtransactio
13e9c 6e 2e 20 20 54 68 65 20 73 75 62 74 72 61 6e 73  n.  The subtrans
13e9d 61 63 74 69 6f 6e 20 63 61 6e 0a 2a 2a 20 63 61  action can.** ca
13e9e 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b  n be rolled back
13e9f 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 6f   independently o
13ea0 66 20 74 68 65 20 6d 61 69 6e 20 74 72 61 6e 73  f the main trans
13ea1 61 63 74 69 6f 6e 2e 0a 2a 2a 20 59 6f 75 20 6d  action..** You m
13ea2 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61 6e  ust start a tran
13ea3 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 20 73  saction before s
13ea4 74 61 72 74 69 6e 67 20 61 20 73 75 62 74 72 61  tarting a subtra
13ea5 6e 73 61 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65  nsaction..** The
13ea6 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 20   subtransaction 
13ea7 69 73 20 65 6e 64 65 64 20 61 75 74 6f 6d 61 74  is ended automat
13ea8 69 63 61 6c 6c 79 20 69 66 20 74 68 65 20 6d 61  ically if the ma
13ea9 69 6e 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a  in transaction.*
13eaa 2a 20 63 6f 6d 6d 69 74 73 20 6f 72 20 72 6f 6c  * commits or rol
13eab 6c 73 20 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 4f  ls back..**.** O
13eac 6e 6c 79 20 6f 6e 65 20 73 75 62 74 72 61 6e 73  nly one subtrans
13ead 61 63 74 69 6f 6e 20 6d 61 79 20 62 65 20 61 63  action may be ac
13eae 74 69 76 65 20 61 74 20 61 20 74 69 6d 65 2e 20  tive at a time. 
13eaf 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72 20   It is an error 
13eb0 74 6f 20 74 72 79 0a 2a 2a 20 74 6f 20 73 74 61  to try.** to sta
13eb1 72 74 20 61 20 6e 65 77 20 73 75 62 74 72 61 6e  rt a new subtran
13eb2 73 61 63 74 69 6f 6e 20 69 66 20 61 6e 6f 74 68  saction if anoth
13eb3 65 72 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  er subtransactio
13eb4 6e 20 69 73 20 61 6c 72 65 61 64 79 20 61 63 74  n is already act
13eb5 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 61 74 65  ive..**.** State
13eb6 6d 65 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74  ment subtransact
13eb7 69 6f 6e 73 20 61 72 65 20 75 73 65 64 20 61 72  ions are used ar
13eb8 6f 75 6e 64 20 69 6e 64 69 76 69 64 75 61 6c 20  ound individual 
13eb9 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 0a 2a  SQL statements.*
13eba 2a 20 74 68 61 74 20 61 72 65 20 63 6f 6e 74 61  * that are conta
13ebb 69 6e 65 64 20 77 69 74 68 69 6e 20 61 20 42 45  ined within a BE
13ebc 47 49 4e 2e 2e 2e 43 4f 4d 4d 49 54 20 62 6c 6f  GIN...COMMIT blo
13ebd 63 6b 2e 20 20 49 66 20 61 20 63 6f 6e 73 74 72  ck.  If a constr
13ebe 61 69 6e 74 0a 2a 2a 20 65 72 72 6f 72 20 6f 63  aint.** error oc
13ebf 63 75 72 73 20 77 69 74 68 69 6e 20 74 68 65 20  curs within the 
13ec0 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65 20 65  statement, the e
13ec1 66 66 65 63 74 20 6f 66 20 74 68 61 74 20 6f 6e  ffect of that on
13ec2 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 63  e statement.** c
13ec3 61 6e 20 62 65 20 72 6f 6c 6c 65 64 20 62 61 63  an be rolled bac
13ec4 6b 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e 67  k without having
13ec5 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65   to rollback the
13ec6 20 65 6e 74 69 72 65 20 74 72 61 6e 73 61 63 74   entire transact
13ec7 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ion..*/.SQLITE_P
13ec8 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
13ec9 65 33 42 74 72 65 65 42 65 67 69 6e 53 74 6d 74  e3BtreeBeginStmt
13eca 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e  (Btree *p){.  in
13ecb 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72 65 64  t rc;.  BtShared
13ecc 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a   *pBt = p->pBt;.
13ecd 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
13ece 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64  ter(p);.  pBt->d
13ecf 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
13ed0 20 28 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54 52   (p->inTrans!=TR
13ed1 41 4e 53 5f 57 52 49 54 45 29 20 7c 7c 20 70 42  ANS_WRITE) || pB
13ed2 74 2d 3e 69 6e 53 74 6d 74 20 29 7b 0a 20 20 20  t->inStmt ){.   
13ed3 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f   rc = pBt->readO
13ed4 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41  nly ? SQLITE_REA
13ed5 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45  DONLY : SQLITE_E
13ed6 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  RROR;.  }else{. 
13ed7 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
13ed8 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e 3d 3d 54  inTransaction==T
13ed9 52 41 4e 53 5f 57 52 49 54 45 20 29 3b 0a 20 20  RANS_WRITE );.  
13eda 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64    rc = pBt->read
13edb 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 4f 4b  Only ? SQLITE_OK
13edc 20 3a 20 73 71 6c 69 74 65 33 50 61 67 65 72 53   : sqlite3PagerS
13edd 74 6d 74 42 65 67 69 6e 28 70 42 74 2d 3e 70 50  tmtBegin(pBt->pP
13ede 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d 3e  ager);.    pBt->
13edf 69 6e 53 74 6d 74 20 3d 20 31 3b 0a 20 20 7d 0a  inStmt = 1;.  }.
13ee0 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
13ee1 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e  ave(p);.  return
13ee2 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43   rc;.}.../*.** C
13ee3 6f 6d 6d 69 74 20 74 68 65 20 73 74 61 74 6d 65  ommit the statme
13ee4 6e 74 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  nt subtransactio
13ee5 6e 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 70  n currently in p
13ee6 72 6f 67 72 65 73 73 2e 20 20 49 66 20 6e 6f 0a  rogress.  If no.
13ee7 2a 2a 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f  ** subtransactio
13ee8 6e 20 69 73 20 61 63 74 69 76 65 2c 20 74 68 69  n is active, thi
13ee9 73 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  s is a no-op..*/
13eea 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
13eeb 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
13eec 43 6f 6d 6d 69 74 53 74 6d 74 28 42 74 72 65 65  CommitStmt(Btree
13eed 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a   *p){.  int rc;.
13eee 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
13eef 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71 6c 69  = p->pBt;.  sqli
13ef0 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29  te3BtreeEnter(p)
13ef1 3b 0a 20 20 70 42 74 2d 3e 64 62 20 3d 20 70 2d  ;.  pBt->db = p-
13ef2 3e 64 62 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e  >db;.  if( pBt->
13ef3 69 6e 53 74 6d 74 20 26 26 20 21 70 42 74 2d 3e  inStmt && !pBt->
13ef4 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20 20  readOnly ){.    
13ef5 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
13ef6 72 53 74 6d 74 43 6f 6d 6d 69 74 28 70 42 74 2d  rStmtCommit(pBt-
13ef7 3e 70 50 61 67 65 72 29 3b 0a 20 20 7d 65 6c 73  >pPager);.  }els
13ef8 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  e{.    rc = SQLI
13ef9 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 70 42 74  TE_OK;.  }.  pBt
13efa 2d 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20  ->inStmt = 0;.  
13efb 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
13efc 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  e(p);.  return r
13efd 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c  c;.}../*.** Roll
13efe 62 61 63 6b 20 74 68 65 20 61 63 74 69 76 65 20  back the active 
13eff 73 74 61 74 65 6d 65 6e 74 20 73 75 62 74 72 61  statement subtra
13f00 6e 73 61 63 74 69 6f 6e 2e 20 20 49 66 20 6e 6f  nsaction.  If no
13f01 20 73 75 62 74 72 61 6e 73 61 63 74 69 6f 6e 0a   subtransaction.
13f02 2a 2a 20 69 73 20 61 63 74 69 76 65 20 74 68 69  ** is active thi
13f03 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
13f04 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 41 6c 6c 20  o-op..**.** All 
13f05 63 75 72 73 6f 72 73 20 77 69 6c 6c 20 62 65 20  cursors will be 
13f06 69 6e 76 61 6c 69 64 61 74 65 64 20 62 79 20 74  invalidated by t
13f07 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20  his operation.  
13f08 41 6e 79 20 61 74 74 65 6d 70 74 0a 2a 2a 20 74  Any attempt.** t
13f09 6f 20 75 73 65 20 61 20 63 75 72 73 6f 72 20 74  o use a cursor t
13f0a 68 61 74 20 77 61 73 20 6f 70 65 6e 20 61 74 20  hat was open at 
13f0b 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
13f0c 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 0a   this operation.
13f0d 2a 2a 20 77 69 6c 6c 20 72 65 73 75 6c 74 20 69  ** will result i
13f0e 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53  n an error..*/.S
13f0f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
13f10 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f  t sqlite3BtreeRo
13f11 6c 6c 62 61 63 6b 53 74 6d 74 28 42 74 72 65 65  llbackStmt(Btree
13f12 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d   *p){.  int rc =
13f13 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74   SQLITE_OK;.  Bt
13f14 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d  Shared *pBt = p-
13f15 3e 70 42 74 3b 0a 20 20 73 71 6c 69 74 65 33 42  >pBt;.  sqlite3B
13f16 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
13f17 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
13f18 0a 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 53 74  .  if( pBt->inSt
13f19 6d 74 20 26 26 20 21 70 42 74 2d 3e 72 65 61 64  mt && !pBt->read
13f1a 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 72 63 20 3d  Only ){.    rc =
13f1b 20 73 71 6c 69 74 65 33 50 61 67 65 72 53 74 6d   sqlite3PagerStm
13f1c 74 52 6f 6c 6c 62 61 63 6b 28 70 42 74 2d 3e 70  tRollback(pBt->p
13f1d 50 61 67 65 72 29 3b 0a 20 20 20 20 70 42 74 2d  Pager);.    pBt-
13f1e 3e 69 6e 53 74 6d 74 20 3d 20 30 3b 0a 20 20 7d  >inStmt = 0;.  }
13f1f 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
13f20 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
13f21 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
13f22 72 65 61 74 65 20 61 20 6e 65 77 20 63 75 72 73  reate a new curs
13f23 6f 72 20 66 6f 72 20 74 68 65 20 42 54 72 65 65  or for the BTree
13f24 20 77 68 6f 73 65 20 72 6f 6f 74 20 69 73 20 6f   whose root is o
13f25 6e 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 69 54  n the page.** iT
13f26 61 62 6c 65 2e 20 20 54 68 65 20 61 63 74 20 6f  able.  The act o
13f27 66 20 61 63 71 75 69 72 69 6e 67 20 61 20 63 75  f acquiring a cu
13f28 72 73 6f 72 20 67 65 74 73 20 61 20 72 65 61 64  rsor gets a read
13f29 20 6c 6f 63 6b 20 6f 6e 20 0a 2a 2a 20 74 68 65   lock on .** the
13f2a 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
13f2b 2a 2a 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d  **.** If wrFlag=
13f2c 3d 30 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72  =0, then the cur
13f2d 73 6f 72 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20  sor can only be 
13f2e 75 73 65 64 20 66 6f 72 20 72 65 61 64 69 6e 67  used for reading
13f2f 2e 0a 2a 2a 20 49 66 20 77 72 46 6c 61 67 3d 3d  ..** If wrFlag==
13f30 31 2c 20 74 68 65 6e 20 74 68 65 20 63 75 72 73  1, then the curs
13f31 6f 72 20 63 61 6e 20 62 65 20 75 73 65 64 20 66  or can be used f
13f32 6f 72 20 72 65 61 64 69 6e 67 20 6f 72 20 66 6f  or reading or fo
13f33 72 0a 2a 2a 20 77 72 69 74 69 6e 67 20 69 66 20  r.** writing if 
13f34 6f 74 68 65 72 20 63 6f 6e 64 69 74 69 6f 6e 73  other conditions
13f35 20 66 6f 72 20 77 72 69 74 69 6e 67 20 61 72 65   for writing are
13f36 20 61 6c 73 6f 20 6d 65 74 2e 20 20 54 68 65 73   also met.  Thes
13f37 65 0a 2a 2a 20 61 72 65 20 74 68 65 20 63 6f 6e  e.** are the con
13f38 64 69 74 69 6f 6e 73 20 74 68 61 74 20 6d 75 73  ditions that mus
13f39 74 20 62 65 20 6d 65 74 20 69 6e 20 6f 72 64 65  t be met in orde
13f3a 72 20 66 6f 72 20 77 72 69 74 69 6e 67 20 74 6f  r for writing to
13f3b 0a 2a 2a 20 62 65 20 61 6c 6c 6f 77 65 64 3a 0a  .** be allowed:.
13f3c 2a 2a 0a 2a 2a 20 31 3a 20 20 54 68 65 20 63 75  **.** 1:  The cu
13f3d 72 73 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62  rsor must have b
13f3e 65 65 6e 20 6f 70 65 6e 65 64 20 77 69 74 68 20  een opened with 
13f3f 77 72 46 6c 61 67 3d 3d 31 0a 2a 2a 0a 2a 2a 20  wrFlag==1.**.** 
13f40 32 3a 20 20 4f 74 68 65 72 20 64 61 74 61 62 61  2:  Other databa
13f41 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 74  se connections t
13f42 68 61 74 20 73 68 61 72 65 20 74 68 65 20 73 61  hat share the sa
13f43 6d 65 20 70 61 67 65 72 20 63 61 63 68 65 0a 2a  me pager cache.*
13f44 2a 20 20 20 20 20 62 75 74 20 77 68 69 63 68 20  *     but which 
13f45 61 72 65 20 6e 6f 74 20 69 6e 20 74 68 65 20 52  are not in the R
13f46 45 41 44 5f 55 4e 43 4f 4d 4d 49 54 54 45 44 20  EAD_UNCOMMITTED 
13f47 73 74 61 74 65 20 6d 61 79 20 6e 6f 74 20 68 61  state may not ha
13f48 76 65 0a 2a 2a 20 20 20 20 20 63 75 72 73 6f 72  ve.**     cursor
13f49 73 20 6f 70 65 6e 20 77 69 74 68 20 77 72 46 6c  s open with wrFl
13f4a 61 67 3d 3d 30 20 6f 6e 20 74 68 65 20 73 61 6d  ag==0 on the sam
13f4b 65 20 74 61 62 6c 65 2e 20 20 4f 74 68 65 72 77  e table.  Otherw
13f4c 69 73 65 0a 2a 2a 20 20 20 20 20 74 68 65 20 63  ise.**     the c
13f4d 68 61 6e 67 65 73 20 6d 61 64 65 20 62 79 20 74  hanges made by t
13f4e 68 69 73 20 77 72 69 74 65 20 63 75 72 73 6f 72  his write cursor
13f4f 20 77 6f 75 6c 64 20 62 65 20 76 69 73 69 62 6c   would be visibl
13f50 65 20 74 6f 0a 2a 2a 20 20 20 20 20 74 68 65 20  e to.**     the 
13f51 72 65 61 64 20 63 75 72 73 6f 72 73 20 69 6e 20  read cursors in 
13f52 74 68 65 20 6f 74 68 65 72 20 64 61 74 61 62 61  the other databa
13f53 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e 0a 2a  se connection..*
13f54 2a 0a 2a 2a 20 33 3a 20 20 54 68 65 20 64 61 74  *.** 3:  The dat
13f55 61 62 61 73 65 20 6d 75 73 74 20 62 65 20 77 72  abase must be wr
13f56 69 74 61 62 6c 65 20 28 6e 6f 74 20 6f 6e 20 72  itable (not on r
13f57 65 61 64 2d 6f 6e 6c 79 20 6d 65 64 69 61 29 0a  ead-only media).
13f58 2a 2a 0a 2a 2a 20 34 3a 20 20 54 68 65 72 65 20  **.** 4:  There 
13f59 6d 75 73 74 20 62 65 20 61 6e 20 61 63 74 69 76  must be an activ
13f5a 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 2a  e transaction..*
13f5b 2a 0a 2a 2a 20 4e 6f 20 63 68 65 63 6b 69 6e 67  *.** No checking
13f5c 20 69 73 20 64 6f 6e 65 20 74 6f 20 6d 61 6b 65   is done to make
13f5d 20 73 75 72 65 20 74 68 61 74 20 70 61 67 65 20   sure that page 
13f5e 69 54 61 62 6c 65 20 72 65 61 6c 6c 79 20 69 73  iTable really is
13f5f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 70 61 67   the.** root pag
13f60 65 20 6f 66 20 61 20 62 2d 74 72 65 65 2e 20 20  e of a b-tree.  
13f61 49 66 20 69 74 20 69 73 20 6e 6f 74 2c 20 74 68  If it is not, th
13f62 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 61 63  en the cursor ac
13f63 71 75 69 72 65 64 0a 2a 2a 20 77 69 6c 6c 20 6e  quired.** will n
13f64 6f 74 20 77 6f 72 6b 20 63 6f 72 72 65 63 74 6c  ot work correctl
13f65 79 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 61  y..**.** It is a
13f66 73 73 75 6d 65 64 20 74 68 61 74 20 74 68 65 20  ssumed that the 
13f67 73 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73  sqlite3BtreeCurs
13f68 6f 72 53 69 7a 65 28 29 20 62 79 74 65 73 20 6f  orSize() bytes o
13f69 66 20 6d 65 6d 6f 72 79 20 0a 2a 2a 20 70 6f 69  f memory .** poi
13f6a 6e 74 65 64 20 74 6f 20 62 79 20 70 43 75 72 20  nted to by pCur 
13f6b 68 61 76 65 20 62 65 65 6e 20 7a 65 72 6f 65 64  have been zeroed
13f6c 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e 0a   by the caller..
13f6d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74  */.static int bt
13f6e 72 65 65 43 75 72 73 6f 72 28 0a 20 20 42 74 72  reeCursor(.  Btr
13f6f 65 65 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20  ee *p,          
13f70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13f71 20 20 20 20 2f 2a 20 54 68 65 20 62 74 72 65 65      /* The btree
13f72 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65   */.  int iTable
13f73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
13f74 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13f75 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61 62  Root page of tab
13f76 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20  le to open */.  
13f77 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20 20  int wrFlag,     
13f78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13f79 20 20 20 20 20 20 20 2f 2a 20 31 20 74 6f 20 77         /* 1 to w
13f7a 72 69 74 65 2e 20 30 20 72 65 61 64 2d 6f 6e 6c  rite. 0 read-onl
13f7b 79 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 4b 65  y */.  struct Ke
13f7c 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c  yInfo *pKeyInfo,
13f7d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13f7e 20 46 69 72 73 74 20 61 72 67 20 74 6f 20 63 6f   First arg to co
13f7f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63 74 69 6f  mparison functio
13f80 6e 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f 72 20  n */.  BtCursor 
13f81 2a 70 43 75 72 20 20 20 20 20 20 20 20 20 20 20  *pCur           
13f82 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13f83 20 53 70 61 63 65 20 66 6f 72 20 6e 65 77 20 63   Space for new c
13f84 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ursor */.){.  in
13f85 74 20 72 63 3b 0a 20 20 50 67 6e 6f 20 6e 50 61  t rc;.  Pgno nPa
13f86 67 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ge;.  BtShared *
13f87 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
13f88 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
13f89 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28  BtreeHoldsMutex(
13f8a 70 29 20 29 3b 0a 20 20 69 66 28 20 77 72 46 6c  p) );.  if( wrFl
13f8b 61 67 20 29 7b 0a 20 20 20 20 69 66 28 20 70 42  ag ){.    if( pB
13f8c 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20  t->readOnly ){. 
13f8d 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
13f8e 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20 20  TE_READONLY;.   
13f8f 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63 6b   }.    if( check
13f90 52 65 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54 61  ReadLocks(p, iTa
13f91 62 6c 65 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20  ble, 0, 0) ){.  
13f92 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
13f93 45 5f 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 7d 0a  E_LOCKED;.    }.
13f94 20 20 7d 0a 0a 20 20 69 66 28 20 70 42 74 2d 3e    }..  if( pBt->
13f95 70 50 61 67 65 31 3d 3d 30 20 29 7b 0a 20 20 20  pPage1==0 ){.   
13f96 20 72 63 20 3d 20 6c 6f 63 6b 42 74 72 65 65 57   rc = lockBtreeW
13f97 69 74 68 52 65 74 72 79 28 70 29 3b 0a 20 20 20  ithRetry(p);.   
13f98 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
13f99 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  OK ){.      retu
13f9a 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20  rn rc;.    }.   
13f9b 20 69 66 28 20 70 42 74 2d 3e 72 65 61 64 4f 6e   if( pBt->readOn
13f9c 6c 79 20 26 26 20 77 72 46 6c 61 67 20 29 7b 0a  ly && wrFlag ){.
13f9d 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
13f9e 49 54 45 5f 52 45 41 44 4f 4e 4c 59 3b 0a 20 20  ITE_READONLY;.  
13f9f 20 20 7d 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e    }.  }.  pCur->
13fa0 70 67 6e 6f 52 6f 6f 74 20 3d 20 28 50 67 6e 6f  pgnoRoot = (Pgno
13fa1 29 69 54 61 62 6c 65 3b 0a 20 20 72 63 20 3d 20  )iTable;.  rc = 
13fa2 73 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65  sqlite3PagerPage
13fa3 63 6f 75 6e 74 28 70 42 74 2d 3e 70 50 61 67 65  count(pBt->pPage
13fa4 72 2c 20 28 69 6e 74 20 2a 29 26 6e 50 61 67 65  r, (int *)&nPage
13fa5 29 3b 20 0a 20 20 69 66 28 20 72 63 21 3d 53 51  ); .  if( rc!=SQ
13fa6 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
13fa7 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
13fa8 69 66 28 20 69 54 61 62 6c 65 3d 3d 31 20 26 26  if( iTable==1 &&
13fa9 20 6e 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20   nPage==0 ){.   
13faa 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 4d 50   rc = SQLITE_EMP
13fab 54 59 3b 0a 20 20 20 20 67 6f 74 6f 20 63 72 65  TY;.    goto cre
13fac 61 74 65 5f 63 75 72 73 6f 72 5f 65 78 63 65 70  ate_cursor_excep
13fad 74 69 6f 6e 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  tion;.  }.  rc =
13fae 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
13faf 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  pBt, pCur->pgnoR
13fb0 6f 6f 74 2c 20 26 70 43 75 72 2d 3e 61 70 50 61  oot, &pCur->apPa
13fb1 67 65 5b 30 5d 29 3b 0a 20 20 69 66 28 20 72 63  ge[0]);.  if( rc
13fb2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
13fb3 20 20 20 67 6f 74 6f 20 63 72 65 61 74 65 5f 63     goto create_c
13fb4 75 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3b  ursor_exception;
13fb5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4e 6f 77 20 74  .  }..  /* Now t
13fb6 68 61 74 20 6e 6f 20 6f 74 68 65 72 20 65 72 72  hat no other err
13fb7 6f 72 73 20 63 61 6e 20 6f 63 63 75 72 2c 20 66  ors can occur, f
13fb8 69 6e 69 73 68 20 66 69 6c 6c 69 6e 67 20 69 6e  inish filling in
13fb9 20 74 68 65 20 42 74 43 75 72 73 6f 72 0a 20 20   the BtCursor.  
13fba 2a 2a 20 76 61 72 69 61 62 6c 65 73 2c 20 6c 69  ** variables, li
13fbb 6e 6b 20 74 68 65 20 63 75 72 73 6f 72 20 69 6e  nk the cursor in
13fbc 74 6f 20 74 68 65 20 42 74 53 68 61 72 65 64 20  to the BtShared 
13fbd 6c 69 73 74 20 61 6e 64 20 73 65 74 20 2a 70 70  list and set *pp
13fbe 43 75 72 20 28 74 68 65 0a 20 20 2a 2a 20 6f 75  Cur (the.  ** ou
13fbf 74 70 75 74 20 61 72 67 75 6d 65 6e 74 20 74 6f  tput argument to
13fc0 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 29 2e   this function).
13fc1 0a 20 20 2a 2f 0a 20 20 70 43 75 72 2d 3e 70 4b  .  */.  pCur->pK
13fc2 65 79 49 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66  eyInfo = pKeyInf
13fc3 6f 3b 0a 20 20 70 43 75 72 2d 3e 70 42 74 72 65  o;.  pCur->pBtre
13fc4 65 20 3d 20 70 3b 0a 20 20 70 43 75 72 2d 3e 70  e = p;.  pCur->p
13fc5 42 74 20 3d 20 70 42 74 3b 0a 20 20 70 43 75 72  Bt = pBt;.  pCur
13fc6 2d 3e 77 72 46 6c 61 67 20 3d 20 77 72 46 6c 61  ->wrFlag = wrFla
13fc7 67 3b 0a 20 20 70 43 75 72 2d 3e 70 4e 65 78 74  g;.  pCur->pNext
13fc8 20 3d 20 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b   = pBt->pCursor;
13fc9 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 70 4e 65  .  if( pCur->pNe
13fca 78 74 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e  xt ){.    pCur->
13fcb 70 4e 65 78 74 2d 3e 70 50 72 65 76 20 3d 20 70  pNext->pPrev = p
13fcc 43 75 72 3b 0a 20 20 7d 0a 20 20 70 42 74 2d 3e  Cur;.  }.  pBt->
13fcd 70 43 75 72 73 6f 72 20 3d 20 70 43 75 72 3b 0a  pCursor = pCur;.
13fce 20 20 70 43 75 72 2d 3e 65 53 74 61 74 65 20 3d    pCur->eState =
13fcf 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 3b   CURSOR_INVALID;
13fd0 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
13fd1 45 5f 4f 4b 3b 0a 0a 63 72 65 61 74 65 5f 63 75  E_OK;..create_cu
13fd2 72 73 6f 72 5f 65 78 63 65 70 74 69 6f 6e 3a 0a  rsor_exception:.
13fd3 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
13fd4 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 29 3b 0a  ur->apPage[0]);.
13fd5 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55    unlockBtreeIfU
13fd6 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20 72 65  nused(pBt);.  re
13fd7 74 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54  turn rc;.}.SQLIT
13fd8 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
13fd9 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
13fda 28 0a 20 20 42 74 72 65 65 20 2a 70 2c 20 20 20  (.  Btree *p,   
13fdb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fdd 2f 2a 20 54 68 65 20 62 74 72 65 65 20 2a 2f 0a  /* The btree */.
13fde 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
13fdf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fe0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
13fe1 20 52 6f 6f 74 20 70 61 67 65 20 6f 66 20 74 61   Root page of ta
13fe2 62 6c 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20  ble to open */. 
13fe3 20 69 6e 74 20 77 72 46 6c 61 67 2c 20 20 20 20   int wrFlag,    
13fe4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fe5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
13fe6 31 20 74 6f 20 77 72 69 74 65 2e 20 30 20 72 65  1 to write. 0 re
13fe7 61 64 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 74 72  ad-only */.  str
13fe8 75 63 74 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65  uct KeyInfo *pKe
13fe9 79 49 6e 66 6f 2c 20 20 20 20 20 20 20 20 20 20  yInfo,          
13fea 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73           /* Firs
13feb 74 20 61 72 67 20 74 6f 20 78 43 6f 6d 70 61 72  t arg to xCompar
13fec 65 28 29 20 2a 2f 0a 20 20 42 74 43 75 72 73 6f  e() */.  BtCurso
13fed 72 20 2a 70 43 75 72 20 20 20 20 20 20 20 20 20  r *pCur         
13fee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
13fef 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e 65       /* Write ne
13ff0 77 20 63 75 72 73 6f 72 20 68 65 72 65 20 2a 2f  w cursor here */
13ff1 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  .){.  int rc;.  
13ff2 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65  sqlite3BtreeEnte
13ff3 72 28 70 29 3b 0a 20 20 70 2d 3e 70 42 74 2d 3e  r(p);.  p->pBt->
13ff4 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 72 63  db = p->db;.  rc
13ff5 20 3d 20 62 74 72 65 65 43 75 72 73 6f 72 28 70   = btreeCursor(p
13ff6 2c 20 69 54 61 62 6c 65 2c 20 77 72 46 6c 61 67  , iTable, wrFlag
13ff7 2c 20 70 4b 65 79 49 6e 66 6f 2c 20 70 43 75 72  , pKeyInfo, pCur
13ff8 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
13ff9 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
13ffa 75 72 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45  urn rc;.}.SQLITE
13ffb 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
13ffc 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53  ite3BtreeCursorS
13ffd 69 7a 65 28 29 7b 0a 20 20 72 65 74 75 72 6e 20  ize(){.  return 
13ffe 73 69 7a 65 6f 66 28 42 74 43 75 72 73 6f 72 29  sizeof(BtCursor)
13fff 3b 0a 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  ;.}..../*.** Clo
14000 73 65 20 61 20 63 75 72 73 6f 72 2e 20 20 54 68  se a cursor.  Th
14001 65 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74  e read lock on t
14002 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  he database file
14003 20 69 73 20 72 65 6c 65 61 73 65 64 0a 2a 2a 20   is released.** 
14004 77 68 65 6e 20 74 68 65 20 6c 61 73 74 20 63 75  when the last cu
14005 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a  rsor is closed..
14006 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14007 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
14008 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28 42 74  eeCloseCursor(Bt
14009 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20  Cursor *pCur){. 
1400a 20 42 74 72 65 65 20 2a 70 42 74 72 65 65 20 3d   Btree *pBtree =
1400b 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b 0a 20   pCur->pBtree;. 
1400c 20 69 66 28 20 70 42 74 72 65 65 20 29 7b 0a 20   if( pBtree ){. 
1400d 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 42 74     int i;.    Bt
1400e 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 43  Shared *pBt = pC
1400f 75 72 2d 3e 70 42 74 3b 0a 20 20 20 20 73 71 6c  ur->pBt;.    sql
14010 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70  ite3BtreeEnter(p
14011 42 74 72 65 65 29 3b 0a 20 20 20 20 70 42 74 2d  Btree);.    pBt-
14012 3e 64 62 20 3d 20 70 42 74 72 65 65 2d 3e 64 62  >db = pBtree->db
14013 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
14014 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43  eeClearCursor(pC
14015 75 72 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75  ur);.    if( pCu
14016 72 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20  r->pPrev ){.    
14017 20 20 70 43 75 72 2d 3e 70 50 72 65 76 2d 3e 70    pCur->pPrev->p
14018 4e 65 78 74 20 3d 20 70 43 75 72 2d 3e 70 4e 65  Next = pCur->pNe
14019 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  xt;.    }else{. 
1401a 20 20 20 20 20 70 42 74 2d 3e 70 43 75 72 73 6f       pBt->pCurso
1401b 72 20 3d 20 70 43 75 72 2d 3e 70 4e 65 78 74 3b  r = pCur->pNext;
1401c 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
1401d 43 75 72 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20  Cur->pNext ){.  
1401e 20 20 20 20 70 43 75 72 2d 3e 70 4e 65 78 74 2d      pCur->pNext-
1401f 3e 70 50 72 65 76 20 3d 20 70 43 75 72 2d 3e 70  >pPrev = pCur->p
14020 50 72 65 76 3b 0a 20 20 20 20 7d 0a 20 20 20 20  Prev;.    }.    
14021 66 6f 72 28 69 3d 30 3b 20 69 3c 3d 70 43 75 72  for(i=0; i<=pCur
14022 2d 3e 69 50 61 67 65 3b 20 69 2b 2b 29 7b 0a 20  ->iPage; i++){. 
14023 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
14024 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  (pCur->apPage[i]
14025 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 75 6e 6c  );.    }.    unl
14026 6f 63 6b 42 74 72 65 65 49 66 55 6e 75 73 65 64  ockBtreeIfUnused
14027 28 70 42 74 29 3b 0a 20 20 20 20 69 6e 76 61 6c  (pBt);.    inval
14028 69 64 61 74 65 4f 76 65 72 66 6c 6f 77 43 61 63  idateOverflowCac
14029 68 65 28 70 43 75 72 29 3b 0a 20 20 20 20 2f 2a  he(pCur);.    /*
1402a 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
1402b 75 72 29 3b 20 2a 2f 0a 20 20 20 20 73 71 6c 69  ur); */.    sqli
1402c 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 42  te3BtreeLeave(pB
1402d 74 72 65 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74  tree);.  }.  ret
1402e 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
1402f 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 74  ../*.** Make a t
14030 65 6d 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20  emporary cursor 
14031 62 79 20 66 69 6c 6c 69 6e 67 20 69 6e 20 74 68  by filling in th
14032 65 20 66 69 65 6c 64 73 20 6f 66 20 70 54 65 6d  e fields of pTem
14033 70 43 75 72 2e 0a 2a 2a 20 54 68 65 20 74 65 6d  pCur..** The tem
14034 70 6f 72 61 72 79 20 63 75 72 73 6f 72 20 69 73  porary cursor is
14035 20 6e 6f 74 20 6f 6e 20 74 68 65 20 63 75 72 73   not on the curs
14036 6f 72 20 6c 69 73 74 20 66 6f 72 20 74 68 65 20  or list for the 
14037 42 74 72 65 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Btree..*/.SQLITE
14038 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
14039 6c 69 74 65 33 42 74 72 65 65 47 65 74 54 65 6d  lite3BtreeGetTem
1403a 70 43 75 72 73 6f 72 28 42 74 43 75 72 73 6f 72  pCursor(BtCursor
1403b 20 2a 70 43 75 72 2c 20 42 74 43 75 72 73 6f 72   *pCur, BtCursor
1403c 20 2a 70 54 65 6d 70 43 75 72 29 7b 0a 20 20 69   *pTempCur){.  i
1403d 6e 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20  nt i;.  assert( 
1403e 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
1403f 28 70 43 75 72 29 20 29 3b 0a 20 20 6d 65 6d 63  (pCur) );.  memc
14040 70 79 28 70 54 65 6d 70 43 75 72 2c 20 70 43 75  py(pTempCur, pCu
14041 72 2c 20 73 69 7a 65 6f 66 28 42 74 43 75 72 73  r, sizeof(BtCurs
14042 6f 72 29 29 3b 0a 20 20 70 54 65 6d 70 43 75 72  or));.  pTempCur
14043 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 70  ->pNext = 0;.  p
14044 54 65 6d 70 43 75 72 2d 3e 70 50 72 65 76 20 3d  TempCur->pPrev =
14045 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   0;.  for(i=0; i
14046 3c 3d 70 54 65 6d 70 43 75 72 2d 3e 69 50 61 67  <=pTempCur->iPag
14047 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  e; i++){.    sql
14048 69 74 65 33 50 61 67 65 72 52 65 66 28 70 54 65  ite3PagerRef(pTe
14049 6d 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d  mpCur->apPage[i]
1404a 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a  ->pDbPage);.  }.
1404b 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20  }../*.** Delete 
1404c 61 20 74 65 6d 70 6f 72 61 72 79 20 63 75 72 73  a temporary curs
1404d 6f 72 20 73 75 63 68 20 61 73 20 77 61 73 20 6d  or such as was m
1404e 61 64 65 20 62 79 20 74 68 65 20 43 72 65 61 74  ade by the Creat
1404f 65 54 65 6d 70 6f 72 61 72 79 43 75 72 73 6f 72  eTemporaryCursor
14050 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 61  ().** function a
14051 62 6f 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bove..*/.SQLITE_
14052 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
14053 69 74 65 33 42 74 72 65 65 52 65 6c 65 61 73 65  ite3BtreeRelease
14054 54 65 6d 70 43 75 72 73 6f 72 28 42 74 43 75 72  TempCursor(BtCur
14055 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e  sor *pCur){.  in
14056 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 63  t i;.  assert( c
14057 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28  ursorHoldsMutex(
14058 70 43 75 72 29 20 29 3b 0a 20 20 66 6f 72 28 69  pCur) );.  for(i
14059 3d 30 3b 20 69 3c 3d 70 43 75 72 2d 3e 69 50 61  =0; i<=pCur->iPa
1405a 67 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  ge; i++){.    sq
1405b 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
1405c 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69 5d 2d  pCur->apPage[i]-
1405d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 7d  >pDbPage);.  }.}
1405e 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72  ../*.** Make sur
1405f 65 20 74 68 65 20 42 74 43 75 72 73 6f 72 2a 20  e the BtCursor* 
14060 67 69 76 65 6e 20 69 6e 20 74 68 65 20 61 72 67  given in the arg
14061 75 6d 65 6e 74 20 68 61 73 20 61 20 76 61 6c 69  ument has a vali
14062 64 0a 2a 2a 20 42 74 43 75 72 73 6f 72 2e 69 6e  d.** BtCursor.in
14063 66 6f 20 73 74 72 75 63 74 75 72 65 2e 20 20 49  fo structure.  I
14064 66 20 69 74 20 69 73 20 6e 6f 74 20 61 6c 72 65  f it is not alre
14065 61 64 79 20 76 61 6c 69 64 2c 20 63 61 6c 6c 0a  ady valid, call.
14066 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  ** sqlite3BtreeP
14067 61 72 73 65 43 65 6c 6c 28 29 20 74 6f 20 66 69  arseCell() to fi
14068 6c 6c 20 69 74 20 69 6e 2e 0a 2a 2a 0a 2a 2a 20  ll it in..**.** 
14069 42 74 43 75 72 73 6f 72 2e 69 6e 66 6f 20 69 73  BtCursor.info is
1406a 20 61 20 63 61 63 68 65 20 6f 66 20 74 68 65 20   a cache of the 
1406b 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74  information in t
1406c 68 65 20 63 75 72 72 65 6e 74 20 63 65 6c 6c 2e  he current cell.
1406d 0a 2a 2a 20 55 73 69 6e 67 20 74 68 69 73 20 63  .** Using this c
1406e 61 63 68 65 20 72 65 64 75 63 65 73 20 74 68 65  ache reduces the
1406f 20 6e 75 6d 62 65 72 20 6f 66 20 63 61 6c 6c 73   number of calls
14070 20 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65   to sqlite3Btree
14071 50 61 72 73 65 43 65 6c 6c 28 29 2e 0a 2a 2a 0a  ParseCell()..**.
14072 2a 2a 20 32 30 30 37 2d 30 36 2d 32 35 3a 20 20  ** 2007-06-25:  
14073 54 68 65 72 65 20 69 73 20 61 20 62 75 67 20 69  There is a bug i
14074 6e 20 73 6f 6d 65 20 76 65 72 73 69 6f 6e 73 20  n some versions 
14075 6f 66 20 4d 53 56 43 20 74 68 61 74 20 63 61 75  of MSVC that cau
14076 73 65 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69 6c  se the.** compil
14077 65 72 20 74 6f 20 63 72 61 73 68 20 77 68 65 6e  er to crash when
14078 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 29 20 69   getCellInfo() i
14079 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73  s implemented as
1407a 20 61 20 6d 61 63 72 6f 2e 0a 2a 2a 20 42 75 74   a macro..** But
1407b 20 74 68 65 72 65 20 69 73 20 61 20 6d 65 61 73   there is a meas
1407c 75 72 65 61 62 6c 65 20 73 70 65 65 64 20 61 64  ureable speed ad
1407d 76 61 6e 74 61 67 65 20 74 6f 20 75 73 69 6e 67  vantage to using
1407e 20 74 68 65 20 6d 61 63 72 6f 20 6f 6e 20 67 63   the macro on gc
1407f 63 0a 2a 2a 20 28 77 68 65 6e 20 6c 65 73 73 20  c.** (when less 
14080 63 6f 6d 70 69 6c 65 72 20 6f 70 74 69 6d 69 7a  compiler optimiz
14081 61 74 69 6f 6e 73 20 6c 69 6b 65 20 2d 4f 73 20  ations like -Os 
14082 6f 72 20 2d 4f 30 20 61 72 65 20 75 73 65 64 20  or -O0 are used 
14083 61 6e 64 20 74 68 65 0a 2a 2a 20 63 6f 6d 70 69  and the.** compi
14084 6c 65 72 20 69 73 20 6e 6f 74 20 64 6f 69 6e 67  ler is not doing
14085 20 61 67 72 65 73 73 69 76 65 20 69 6e 6c 69 6e   agressive inlin
14086 69 6e 67 2e 29 20 20 53 6f 20 77 65 20 75 73 65  ing.)  So we use
14087 20 61 20 72 65 61 6c 20 66 75 6e 63 74 69 6f 6e   a real function
14088 0a 2a 2a 20 66 6f 72 20 4d 53 56 43 20 61 6e 64  .** for MSVC and
14089 20 61 20 6d 61 63 72 6f 20 66 6f 72 20 65 76 65   a macro for eve
1408a 72 79 74 68 69 6e 67 20 65 6c 73 65 2e 20 20 54  rything else.  T
1408b 69 63 6b 65 74 20 23 32 34 35 37 2e 0a 2a 2f 0a  icket #2457..*/.
1408c 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
1408d 20 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73   static void ass
1408e 65 72 74 43 65 6c 6c 49 6e 66 6f 28 42 74 43 75  ertCellInfo(BtCu
1408f 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 20  rsor *pCur){.   
14090 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
14091 20 20 20 20 69 6e 74 20 69 50 61 67 65 20 3d 20      int iPage = 
14092 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20 20  pCur->iPage;.   
14093 20 6d 65 6d 73 65 74 28 26 69 6e 66 6f 2c 20 30   memset(&info, 0
14094 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29 3b  , sizeof(info));
14095 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
14096 65 50 61 72 73 65 43 65 6c 6c 28 70 43 75 72 2d  eParseCell(pCur-
14097 3e 61 70 50 61 67 65 5b 69 50 61 67 65 5d 2c 20  >apPage[iPage], 
14098 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 50 61 67  pCur->aiIdx[iPag
14099 65 5d 2c 20 26 69 6e 66 6f 29 3b 0a 20 20 20 20  e], &info);.    
1409a 61 73 73 65 72 74 28 20 6d 65 6d 63 6d 70 28 26  assert( memcmp(&
1409b 69 6e 66 6f 2c 20 26 70 43 75 72 2d 3e 69 6e 66  info, &pCur->inf
1409c 6f 2c 20 73 69 7a 65 6f 66 28 69 6e 66 6f 29 29  o, sizeof(info))
1409d 3d 3d 30 20 29 3b 0a 20 20 7d 0a 23 65 6c 73 65  ==0 );.  }.#else
1409e 0a 20 20 23 64 65 66 69 6e 65 20 61 73 73 65 72  .  #define asser
1409f 74 43 65 6c 6c 49 6e 66 6f 28 78 29 0a 23 65 6e  tCellInfo(x).#en
140a0 64 69 66 0a 23 69 66 64 65 66 20 5f 4d 53 43 5f  dif.#ifdef _MSC_
140a1 56 45 52 0a 20 20 2f 2a 20 55 73 65 20 61 20 72  VER.  /* Use a r
140a2 65 61 6c 20 66 75 6e 63 74 69 6f 6e 20 69 6e 20  eal function in 
140a3 4d 53 56 43 20 74 6f 20 77 6f 72 6b 20 61 72 6f  MSVC to work aro
140a4 75 6e 64 20 62 75 67 73 20 69 6e 20 74 68 61 74  und bugs in that
140a5 20 63 6f 6d 70 69 6c 65 72 2e 20 2a 2f 0a 20 20   compiler. */.  
140a6 73 74 61 74 69 63 20 76 6f 69 64 20 67 65 74 43  static void getC
140a7 65 6c 6c 49 6e 66 6f 28 42 74 43 75 72 73 6f 72  ellInfo(BtCursor
140a8 20 2a 70 43 75 72 29 7b 0a 20 20 20 20 69 66 28   *pCur){.    if(
140a9 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a   pCur->info.nSiz
140aa 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e  e==0 ){.      in
140ab 74 20 69 50 61 67 65 20 3d 20 70 43 75 72 2d 3e  t iPage = pCur->
140ac 69 50 61 67 65 3b 0a 20 20 20 20 20 20 73 71 6c  iPage;.      sql
140ad 69 74 65 33 42 74 72 65 65 50 61 72 73 65 43 65  ite3BtreeParseCe
140ae 6c 6c 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  ll(pCur->apPage[
140af 69 50 61 67 65 5d 2c 70 43 75 72 2d 3e 61 69 49  iPage],pCur->aiI
140b0 64 78 5b 69 50 61 67 65 5d 2c 26 70 43 75 72 2d  dx[iPage],&pCur-
140b1 3e 69 6e 66 6f 29 3b 0a 20 20 20 20 20 20 70 43  >info);.      pC
140b2 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20  ur->validNKey = 
140b3 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  1;.    }else{.  
140b4 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49 6e      assertCellIn
140b5 66 6f 28 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a  fo(pCur);.    }.
140b6 20 20 7d 0a 23 65 6c 73 65 20 2f 2a 20 69 66 20    }.#else /* if 
140b7 6e 6f 74 20 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a  not _MSC_VER */.
140b8 20 20 2f 2a 20 55 73 65 20 61 20 6d 61 63 72 6f    /* Use a macro
140b9 20 69 6e 20 61 6c 6c 20 6f 74 68 65 72 20 63 6f   in all other co
140ba 6d 70 69 6c 65 72 73 20 73 6f 20 74 68 61 74 20  mpilers so that 
140bb 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  the function is 
140bc 69 6e 6c 69 6e 65 64 20 2a 2f 0a 23 64 65 66 69  inlined */.#defi
140bd 6e 65 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70  ne getCellInfo(p
140be 43 75 72 29 20 20 20 20 20 20 20 20 20 20 20 20  Cur)            
140bf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140c1 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 69 66            \.  if
140c2 28 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69  ( pCur->info.nSi
140c3 7a 65 3d 3d 30 20 29 7b 20 20 20 20 20 20 20 20  ze==0 ){        
140c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140c6 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20 20             \.   
140c7 20 69 6e 74 20 69 50 61 67 65 20 3d 20 70 43 75   int iPage = pCu
140c8 72 2d 3e 69 50 61 67 65 3b 20 20 20 20 20 20 20  r->iPage;       
140c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140cb 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
140cc 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61    sqlite3BtreePa
140cd 72 73 65 43 65 6c 6c 28 70 43 75 72 2d 3e 61 70  rseCell(pCur->ap
140ce 50 61 67 65 5b 69 50 61 67 65 5d 2c 70 43 75 72  Page[iPage],pCur
140cf 2d 3e 61 69 49 64 78 5b 69 50 61 67 65 5d 2c 26  ->aiIdx[iPage],&
140d0 70 43 75 72 2d 3e 69 6e 66 6f 29 3b 20 5c 0a 20  pCur->info); \. 
140d1 20 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b     pCur->validNK
140d2 65 79 20 3d 20 31 3b 20 20 20 20 20 20 20 20 20  ey = 1;         
140d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a                \.
140d6 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
140d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 5c                 \
140db 0a 20 20 20 20 61 73 73 65 72 74 43 65 6c 6c 49  .    assertCellI
140dc 6e 66 6f 28 70 43 75 72 29 3b 20 20 20 20 20 20  nfo(pCur);      
140dd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
140e0 5c 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  \.  }.#endif /* 
140e1 5f 4d 53 43 5f 56 45 52 20 2a 2f 0a 0a 2f 2a 0a  _MSC_VER */../*.
140e2 2a 2a 20 53 65 74 20 2a 70 53 69 7a 65 20 74 6f  ** Set *pSize to
140e3 20 74 68 65 20 73 69 7a 65 20 6f 66 20 74 68 65   the size of the
140e4 20 62 75 66 66 65 72 20 6e 65 65 64 65 64 20 74   buffer needed t
140e5 6f 20 68 6f 6c 64 20 74 68 65 20 76 61 6c 75 65  o hold the value
140e6 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79 20 66   of.** the key f
140e7 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 65  or the current e
140e8 6e 74 72 79 2e 20 20 49 66 20 74 68 65 20 63 75  ntry.  If the cu
140e9 72 73 6f 72 20 69 73 20 6e 6f 74 20 70 6f 69 6e  rsor is not poin
140ea 74 69 6e 67 0a 2a 2a 20 74 6f 20 61 20 76 61 6c  ting.** to a val
140eb 69 64 20 65 6e 74 72 79 2c 20 2a 70 53 69 7a 65  id entry, *pSize
140ec 20 69 73 20 73 65 74 20 74 6f 20 30 2e 20 0a 2a   is set to 0. .*
140ed 2a 0a 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65  *.** For a table
140ee 20 77 69 74 68 20 74 68 65 20 49 4e 54 4b 45 59   with the INTKEY
140ef 20 66 6c 61 67 20 73 65 74 2c 20 74 68 69 73 20   flag set, this 
140f0 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20  routine returns 
140f1 74 68 65 20 6b 65 79 0a 2a 2a 20 69 74 73 65 6c  the key.** itsel
140f2 66 2c 20 6e 6f 74 20 74 68 65 20 6e 75 6d 62 65  f, not the numbe
140f3 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68  r of bytes in th
140f4 65 20 6b 65 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  e key..*/.SQLITE
140f5 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
140f6 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
140f7 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c  (BtCursor *pCur,
140f8 20 69 36 34 20 2a 70 53 69 7a 65 29 7b 0a 20 20   i64 *pSize){.  
140f9 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
140fa 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
140fb 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72  tex(pCur) );.  r
140fc 63 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f  c = restoreCurso
140fd 72 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b  rPosition(pCur);
140fe 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
140ff 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 61 73 73 65  E_OK ){.    asse
14100 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
14101 3d 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44  ==CURSOR_INVALID
14102 20 7c 7c 20 70 43 75 72 2d 3e 65 53 74 61 74 65   || pCur->eState
14103 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
14104 3b 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  ;.    if( pCur->
14105 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 49  eState==CURSOR_I
14106 4e 56 41 4c 49 44 20 29 7b 0a 20 20 20 20 20 20  NVALID ){.      
14107 2a 70 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  *pSize = 0;.    
14108 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 67 65 74  }else{.      get
14109 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b 0a  CellInfo(pCur);.
1410a 20 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 70        *pSize = p
1410b 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3b 0a  Cur->info.nKey;.
1410c 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1410d 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1410e 53 65 74 20 2a 70 53 69 7a 65 20 74 6f 20 74 68  Set *pSize to th
1410f 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  e number of byte
14110 73 20 6f 66 20 64 61 74 61 20 69 6e 20 74 68 65  s of data in the
14111 20 65 6e 74 72 79 20 74 68 65 0a 2a 2a 20 63 75   entry the.** cu
14112 72 73 6f 72 20 63 75 72 72 65 6e 74 6c 79 20 70  rsor currently p
14113 6f 69 6e 74 73 20 74 6f 2e 20 20 41 6c 77 61 79  oints to.  Alway
14114 73 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  s return SQLITE_
14115 4f 4b 2e 0a 2a 2a 20 46 61 69 6c 75 72 65 20 69  OK..** Failure i
14116 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 2e 20  s not possible. 
14117 20 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69   If the cursor i
14118 73 20 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 0a  s not currently.
14119 2a 2a 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  ** pointing to a
1411a 6e 20 65 6e 74 72 79 20 28 77 68 69 63 68 20 63  n entry (which c
1411b 61 6e 20 68 61 70 70 65 6e 2c 20 66 6f 72 20 65  an happen, for e
1411c 78 61 6d 70 6c 65 2c 20 69 66 0a 2a 2a 20 74 68  xample, if.** th
1411d 65 20 64 61 74 61 62 61 73 65 20 69 73 20 65 6d  e database is em
1411e 70 74 79 29 20 74 68 65 6e 20 2a 70 53 69 7a 65  pty) then *pSize
1411f 20 69 73 20 73 65 74 20 74 6f 20 30 2e 0a 2a 2f   is set to 0..*/
14120 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
14121 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
14122 44 61 74 61 53 69 7a 65 28 42 74 43 75 72 73 6f  DataSize(BtCurso
14123 72 20 2a 70 43 75 72 2c 20 75 33 32 20 2a 70 53  r *pCur, u32 *pS
14124 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  ize){.  int rc;.
14125 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
14126 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
14127 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
14128 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
14129 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  n(pCur);.  if( r
1412a 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
1412b 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
1412c 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1412d 5f 49 4e 56 41 4c 49 44 20 7c 7c 20 70 43 75 72  _INVALID || pCur
1412e 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
1412f 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 69 66  _VALID );.    if
14130 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
14131 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
14132 7b 0a 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 70  {.      /* Not p
14133 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 76 61 6c  ointing at a val
14134 69 64 20 65 6e 74 72 79 20 2d 20 73 65 74 20 2a  id entry - set *
14135 70 53 69 7a 65 20 74 6f 20 30 2e 20 2a 2f 0a 20  pSize to 0. */. 
14136 20 20 20 20 20 2a 70 53 69 7a 65 20 3d 20 30 3b       *pSize = 0;
14137 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
14138 20 20 67 65 74 43 65 6c 6c 49 6e 66 6f 28 70 43    getCellInfo(pC
14139 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 53 69 7a  ur);.      *pSiz
1413a 65 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  e = pCur->info.n
1413b 44 61 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Data;.    }.  }.
1413c 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
1413d 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
1413e 70 61 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 61  page number of a
1413f 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  n overflow page 
14140 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
14141 28 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 6f 76  (parameter.** ov
14142 66 6c 29 2c 20 74 68 69 73 20 66 75 6e 63 74 69  fl), this functi
14143 6f 6e 20 66 69 6e 64 73 20 74 68 65 20 70 61 67  on finds the pag
14144 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
14145 6e 65 78 74 20 70 61 67 65 20 69 6e 20 74 68 65  next page in the
14146 20 0a 2a 2a 20 6c 69 6e 6b 65 64 20 6c 69 73 74   .** linked list
14147 20 6f 66 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   of overflow pag
14148 65 73 2e 20 49 66 20 70 6f 73 73 69 62 6c 65 2c  es. If possible,
14149 20 69 74 20 75 73 65 73 20 74 68 65 20 61 75 74   it uses the aut
1414a 6f 2d 76 61 63 75 75 6d 0a 2a 2a 20 70 6f 69 6e  o-vacuum.** poin
1414b 74 65 72 2d 6d 61 70 20 64 61 74 61 20 69 6e 73  ter-map data ins
1414c 74 65 61 64 20 6f 66 20 72 65 61 64 69 6e 67 20  tead of reading 
1414d 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 70  the content of p
1414e 61 67 65 20 6f 76 66 6c 20 74 6f 20 64 6f 20 73  age ovfl to do s
1414f 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20  o. .**.** If an 
14150 65 72 72 6f 72 20 6f 63 63 75 72 73 20 61 6e 20  error occurs an 
14151 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63 6f 64  SQLite error cod
14152 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 4f  e is returned. O
14153 74 68 65 72 77 69 73 65 3a 0a 2a 2a 0a 2a 2a 20  therwise:.**.** 
14154 55 6e 6c 65 73 73 20 70 50 67 6e 6f 4e 65 78 74  Unless pPgnoNext
14155 20 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 70 61   is NULL, the pa
14156 67 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65  ge number of the
14157 20 6e 65 78 74 20 6f 76 65 72 66 6c 6f 77 20 0a   next overflow .
14158 2a 2a 20 70 61 67 65 20 69 6e 20 74 68 65 20 6c  ** page in the l
14159 69 6e 6b 65 64 20 6c 69 73 74 20 69 73 20 77 72  inked list is wr
1415a 69 74 74 65 6e 20 74 6f 20 2a 70 50 67 6e 6f 4e  itten to *pPgnoN
1415b 65 78 74 2e 20 49 66 20 70 61 67 65 20 6f 76 66  ext. If page ovf
1415c 6c 0a 2a 2a 20 69 73 20 74 68 65 20 6c 61 73 74  l.** is the last
1415d 20 70 61 67 65 20 69 6e 20 69 74 73 20 6c 69 6e   page in its lin
1415e 6b 65 64 20 6c 69 73 74 2c 20 2a 70 50 67 6e 6f  ked list, *pPgno
1415f 4e 65 78 74 20 69 73 20 73 65 74 20 74 6f 20 7a  Next is set to z
14160 65 72 6f 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 70  ero. .**.** If p
14161 70 50 61 67 65 20 69 73 20 6e 6f 74 20 4e 55 4c  pPage is not NUL
14162 4c 2c 20 2a 70 70 50 61 67 65 20 69 73 20 73 65  L, *ppPage is se
14163 74 20 74 6f 20 74 68 65 20 4d 65 6d 50 61 67 65  t to the MemPage
14164 2a 20 68 61 6e 64 6c 65 0a 2a 2a 20 66 6f 72 20  * handle.** for 
14165 70 61 67 65 20 6f 76 66 6c 2e 20 54 68 65 20 75  page ovfl. The u
14166 6e 64 65 72 6c 79 69 6e 67 20 70 61 67 65 72 20  nderlying pager 
14167 70 61 67 65 20 6d 61 79 20 68 61 76 65 20 62 65  page may have be
14168 65 6e 20 72 65 71 75 65 73 74 65 64 0a 2a 2a 20  en requested.** 
14169 77 69 74 68 20 74 68 65 20 6e 6f 43 6f 6e 74 65  with the noConte
1416a 6e 74 20 66 6c 61 67 20 73 65 74 2c 20 73 6f 20  nt flag set, so 
1416b 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61 63  the page data ac
1416c 63 65 73 73 61 62 6c 65 20 76 69 61 0a 2a 2a 20  cessable via.** 
1416d 74 68 69 73 20 68 61 6e 64 6c 65 20 6d 61 79 20  this handle may 
1416e 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a  not be trusted..
1416f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65  */.static int ge
14170 74 4f 76 65 72 66 6c 6f 77 50 61 67 65 28 0a 20  tOverflowPage(. 
14171 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 2c 20   BtShared *pBt, 
14172 0a 20 20 50 67 6e 6f 20 6f 76 66 6c 2c 20 20 20  .  Pgno ovfl,   
14173 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14174 2f 2a 20 4f 76 65 72 66 6c 6f 77 20 70 61 67 65  /* Overflow page
14175 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a   */.  MemPage **
14176 70 70 50 61 67 65 2c 20 20 20 20 20 20 20 20 20  ppPage,         
14177 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 65 6d 50 61     /* OUT: MemPa
14178 67 65 20 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 50  ge handle */.  P
14179 67 6e 6f 20 2a 70 50 67 6e 6f 4e 65 78 74 20 20  gno *pPgnoNext  
1417a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
1417b 55 54 3a 20 4e 65 78 74 20 6f 76 65 72 66 6c 6f  UT: Next overflo
1417c 77 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  w page number */
1417d 0a 29 7b 0a 20 20 50 67 6e 6f 20 6e 65 78 74 20  .){.  Pgno next 
1417e 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a  = 0;.  int rc;..
1417f 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
14180 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74  3_mutex_held(pBt
14181 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 2f 2a  ->mutex) );.  /*
14182 20 4f 6e 65 20 6f 66 20 74 68 65 73 65 20 6d 75   One of these mu
14183 73 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 20  st not be NULL. 
14184 4f 74 68 65 72 77 69 73 65 2c 20 77 68 79 20 63  Otherwise, why c
14185 61 6c 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f  all this functio
14186 6e 3f 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 70  n? */.  assert(p
14187 70 50 61 67 65 20 7c 7c 20 70 50 67 6e 6f 4e 65  pPage || pPgnoNe
14188 78 74 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 50  xt);..  /* If pP
14189 67 6e 6f 4e 65 78 74 20 69 73 20 4e 55 4c 4c 2c  gnoNext is NULL,
1418a 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
1418b 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c  ion is being cal
1418c 6c 65 64 20 74 6f 20 6f 62 74 61 69 6e 0a 20 20  led to obtain.  
1418d 2a 2a 20 61 20 4d 65 6d 50 61 67 65 2a 20 72 65  ** a MemPage* re
1418e 66 65 72 65 6e 63 65 20 6f 6e 6c 79 2e 20 4e 6f  ference only. No
1418f 20 70 61 67 65 2d 64 61 74 61 20 69 73 20 72 65   page-data is re
14190 71 75 69 72 65 64 20 69 6e 20 74 68 69 73 20 63  quired in this c
14191 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ase..  */.  if( 
14192 21 70 50 67 6e 6f 4e 65 78 74 20 29 7b 0a 20 20  !pPgnoNext ){.  
14193 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
14194 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
14195 2c 20 6f 76 66 6c 2c 20 70 70 50 61 67 65 2c 20  , ovfl, ppPage, 
14196 31 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66  1);.  }..#ifndef
14197 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
14198 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 54 72 79  OVACUUM.  /* Try
14199 20 74 6f 20 66 69 6e 64 20 74 68 65 20 6e 65 78   to find the nex
1419a 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76  t page in the ov
1419b 65 72 66 6c 6f 77 20 6c 69 73 74 20 75 73 69 6e  erflow list usin
1419c 67 20 74 68 65 0a 20 20 2a 2a 20 61 75 74 6f 76  g the.  ** autov
1419d 61 63 75 75 6d 20 70 6f 69 6e 74 65 72 2d 6d 61  acuum pointer-ma
1419e 70 20 70 61 67 65 73 2e 20 47 75 65 73 73 20 74  p pages. Guess t
1419f 68 61 74 20 74 68 65 20 6e 65 78 74 20 70 61 67  hat the next pag
141a0 65 20 69 6e 20 0a 20 20 2a 2a 20 74 68 65 20 6f  e in .  ** the o
141a1 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 69 73 20  verflow list is 
141a2 70 61 67 65 20 6e 75 6d 62 65 72 20 28 6f 76 66  page number (ovf
141a3 6c 2b 31 29 2e 20 49 66 20 74 68 61 74 20 67 75  l+1). If that gu
141a4 65 73 73 20 74 75 72 6e 73 20 0a 20 20 2a 2a 20  ess turns .  ** 
141a5 6f 75 74 20 74 6f 20 62 65 20 77 72 6f 6e 67 2c  out to be wrong,
141a6 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f 20 6c 6f   fall back to lo
141a7 61 64 69 6e 67 20 74 68 65 20 64 61 74 61 20 6f  ading the data o
141a8 66 20 70 61 67 65 20 0a 20 20 2a 2a 20 6e 75 6d  f page .  ** num
141a9 62 65 72 20 6f 76 66 6c 20 74 6f 20 64 65 74 65  ber ovfl to dete
141aa 72 6d 69 6e 65 20 74 68 65 20 6e 65 78 74 20 70  rmine the next p
141ab 61 67 65 20 6e 75 6d 62 65 72 2e 0a 20 20 2a 2f  age number..  */
141ac 0a 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f  .  if( pBt->auto
141ad 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67  Vacuum ){.    Pg
141ae 6e 6f 20 70 67 6e 6f 3b 0a 20 20 20 20 50 67 6e  no pgno;.    Pgn
141af 6f 20 69 47 75 65 73 73 20 3d 20 6f 76 66 6c 2b  o iGuess = ovfl+
141b0 31 3b 0a 20 20 20 20 75 38 20 65 54 79 70 65 3b  1;.    u8 eType;
141b1 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 50 54 52  ..    while( PTR
141b2 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c 20  MAP_ISPAGE(pBt, 
141b3 69 47 75 65 73 73 29 20 7c 7c 20 69 47 75 65 73  iGuess) || iGues
141b4 73 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  s==PENDING_BYTE_
141b5 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20  PAGE(pBt) ){.   
141b6 20 20 20 69 47 75 65 73 73 2b 2b 3b 0a 20 20 20     iGuess++;.   
141b7 20 7d 0a 0a 20 20 20 20 69 66 28 20 69 47 75 65   }..    if( iGue
141b8 73 73 3c 3d 70 61 67 65 72 50 61 67 65 63 6f 75  ss<=pagerPagecou
141b9 6e 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  nt(pBt) ){.     
141ba 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
141bb 70 42 74 2c 20 69 47 75 65 73 73 2c 20 26 65 54  pBt, iGuess, &eT
141bc 79 70 65 2c 20 26 70 67 6e 6f 29 3b 0a 20 20 20  ype, &pgno);.   
141bd 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
141be 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
141bf 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
141c0 20 7d 0a 20 20 20 20 20 20 69 66 28 20 65 54 79   }.      if( eTy
141c1 70 65 3d 3d 50 54 52 4d 41 50 5f 4f 56 45 52 46  pe==PTRMAP_OVERF
141c2 4c 4f 57 32 20 26 26 20 70 67 6e 6f 3d 3d 6f 76  LOW2 && pgno==ov
141c3 66 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 6e 65  fl ){.        ne
141c4 78 74 20 3d 20 69 47 75 65 73 73 3b 0a 20 20 20  xt = iGuess;.   
141c5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 23     }.    }.  }.#
141c6 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 6e 65 78  endif..  if( nex
141c7 74 3d 3d 30 20 7c 7c 20 70 70 50 61 67 65 20 29  t==0 || ppPage )
141c8 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70  {.    MemPage *p
141c9 50 61 67 65 20 3d 20 30 3b 0a 0a 20 20 20 20 72  Page = 0;..    r
141ca 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
141cb 47 65 74 50 61 67 65 28 70 42 74 2c 20 6f 76 66  GetPage(pBt, ovf
141cc 6c 2c 20 26 70 50 61 67 65 2c 20 6e 65 78 74 21  l, &pPage, next!
141cd 3d 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  =0);.    assert(
141ce 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc==SQLITE_OK ||
141cf 20 70 50 61 67 65 3d 3d 30 29 3b 0a 20 20 20 20   pPage==0);.    
141d0 69 66 28 20 6e 65 78 74 3d 3d 30 20 26 26 20 72  if( next==0 && r
141d1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
141d2 20 20 20 20 20 20 6e 65 78 74 20 3d 20 67 65 74        next = get
141d3 34 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61  4byte(pPage->aDa
141d4 74 61 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  ta);.    }..    
141d5 69 66 28 20 70 70 50 61 67 65 20 29 7b 0a 20 20  if( ppPage ){.  
141d6 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20 70 50      *ppPage = pP
141d7 61 67 65 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  age;.    }else{.
141d8 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
141d9 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a  e(pPage);.    }.
141da 20 20 7d 0a 20 20 2a 70 50 67 6e 6f 4e 65 78 74    }.  *pPgnoNext
141db 20 3d 20 6e 65 78 74 3b 0a 0a 20 20 72 65 74 75   = next;..  retu
141dc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
141dd 43 6f 70 79 20 64 61 74 61 20 66 72 6f 6d 20 61  Copy data from a
141de 20 62 75 66 66 65 72 20 74 6f 20 61 20 70 61 67   buffer to a pag
141df 65 2c 20 6f 72 20 66 72 6f 6d 20 61 20 70 61 67  e, or from a pag
141e0 65 20 74 6f 20 61 20 62 75 66 66 65 72 2e 0a 2a  e to a buffer..*
141e1 2a 0a 2a 2a 20 70 50 61 79 6c 6f 61 64 20 69 73  *.** pPayload is
141e2 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 64 61   a pointer to da
141e3 74 61 20 73 74 6f 72 65 64 20 6f 6e 20 64 61 74  ta stored on dat
141e4 61 62 61 73 65 20 70 61 67 65 20 70 44 62 50 61  abase page pDbPa
141e5 67 65 2e 0a 2a 2a 20 49 66 20 61 72 67 75 6d 65  ge..** If argume
141e6 6e 74 20 65 4f 70 20 69 73 20 66 61 6c 73 65 2c  nt eOp is false,
141e7 20 74 68 65 6e 20 6e 42 79 74 65 20 62 79 74 65   then nByte byte
141e8 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 63 6f  s of data are co
141e9 70 69 65 64 0a 2a 2a 20 66 72 6f 6d 20 70 50 61  pied.** from pPa
141ea 79 6c 6f 61 64 20 74 6f 20 74 68 65 20 62 75 66  yload to the buf
141eb 66 65 72 20 70 6f 69 6e 74 65 64 20 61 74 20 62  fer pointed at b
141ec 79 20 70 42 75 66 2e 20 49 66 20 65 4f 70 20 69  y pBuf. If eOp i
141ed 73 20 74 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20  s true,.** then 
141ee 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
141ef 65 28 29 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e  e() is called on
141f0 20 70 44 62 50 61 67 65 20 61 6e 64 20 6e 42 79   pDbPage and nBy
141f1 74 65 20 62 79 74 65 73 0a 2a 2a 20 6f 66 20 64  te bytes.** of d
141f2 61 74 61 20 61 72 65 20 63 6f 70 69 65 64 20 66  ata are copied f
141f3 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72 20 70  rom the buffer p
141f4 42 75 66 20 74 6f 20 70 50 61 79 6c 6f 61 64 2e  Buf to pPayload.
141f5 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b  .**.** SQLITE_OK
141f6 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20   is returned on 
141f7 73 75 63 63 65 73 73 2c 20 6f 74 68 65 72 77 69  success, otherwi
141f8 73 65 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  se an error code
141f9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
141fa 63 6f 70 79 50 61 79 6c 6f 61 64 28 0a 20 20 76  copyPayload(.  v
141fb 6f 69 64 20 2a 70 50 61 79 6c 6f 61 64 2c 20 20  oid *pPayload,  
141fc 20 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e           /* Poin
141fd 74 65 72 20 74 6f 20 70 61 67 65 20 64 61 74 61  ter to page data
141fe 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 42 75 66   */.  void *pBuf
141ff 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
14200 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 62 75  /* Pointer to bu
14201 66 66 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ffer */.  int nB
14202 79 74 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  yte,            
14203 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
14204 20 62 79 74 65 73 20 74 6f 20 63 6f 70 79 20 2a   bytes to copy *
14205 2f 0a 20 20 69 6e 74 20 65 4f 70 2c 20 20 20 20  /.  int eOp,    
14206 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
14207 20 30 20 2d 3e 20 63 6f 70 79 20 66 72 6f 6d 20   0 -> copy from 
14208 70 61 67 65 2c 20 31 20 2d 3e 20 63 6f 70 79 20  page, 1 -> copy 
14209 74 6f 20 70 61 67 65 20 2a 2f 0a 20 20 44 62 50  to page */.  DbP
1420a 61 67 65 20 2a 70 44 62 50 61 67 65 20 20 20 20  age *pDbPage    
1420b 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 63         /* Page c
1420c 6f 6e 74 61 69 6e 69 6e 67 20 70 50 61 79 6c 6f  ontaining pPaylo
1420d 61 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 65  ad */.){.  if( e
1420e 4f 70 20 29 7b 0a 20 20 20 20 2f 2a 20 43 6f 70  Op ){.    /* Cop
1420f 79 20 64 61 74 61 20 66 72 6f 6d 20 62 75 66 66  y data from buff
14210 65 72 20 74 6f 20 70 61 67 65 20 28 61 20 77 72  er to page (a wr
14211 69 74 65 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a  ite operation) *
14212 2f 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73  /.    int rc = s
14213 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
14214 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  (pDbPage);.    i
14215 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
14216 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
14217 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d   rc;.    }.    m
14218 65 6d 63 70 79 28 70 50 61 79 6c 6f 61 64 2c 20  emcpy(pPayload, 
14219 70 42 75 66 2c 20 6e 42 79 74 65 29 3b 0a 20 20  pBuf, nByte);.  
1421a 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43 6f  }else{.    /* Co
1421b 70 79 20 64 61 74 61 20 66 72 6f 6d 20 70 61 67  py data from pag
1421c 65 20 74 6f 20 62 75 66 66 65 72 20 28 61 20 72  e to buffer (a r
1421d 65 61 64 20 6f 70 65 72 61 74 69 6f 6e 29 20 2a  ead operation) *
1421e 2f 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 75  /.    memcpy(pBu
1421f 66 2c 20 70 50 61 79 6c 6f 61 64 2c 20 6e 42 79  f, pPayload, nBy
14220 74 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  te);.  }.  retur
14221 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
14222 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
14223 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20 72  ion is used to r
14224 65 61 64 20 6f 72 20 6f 76 65 72 77 72 69 74 65  ead or overwrite
14225 20 70 61 79 6c 6f 61 64 20 69 6e 66 6f 72 6d 61   payload informa
14226 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 68 65 20  tion.** for the 
14227 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 70  entry that the p
14228 43 75 72 20 63 75 72 73 6f 72 20 69 73 20 70 6f  Cur cursor is po
14229 69 6e 74 69 6e 67 20 74 6f 2e 20 49 66 20 74 68  inting to. If th
1422a 65 20 65 4f 70 0a 2a 2a 20 70 61 72 61 6d 65 74  e eOp.** paramet
1422b 65 72 20 69 73 20 30 2c 20 74 68 69 73 20 69 73  er is 0, this is
1422c 20 61 20 72 65 61 64 20 6f 70 65 72 61 74 69 6f   a read operatio
1422d 6e 20 28 64 61 74 61 20 63 6f 70 69 65 64 20 69  n (data copied i
1422e 6e 74 6f 0a 2a 2a 20 62 75 66 66 65 72 20 70 42  nto.** buffer pB
1422f 75 66 29 2e 20 49 66 20 69 74 20 69 73 20 6e 6f  uf). If it is no
14230 6e 2d 7a 65 72 6f 2c 20 61 20 77 72 69 74 65 20  n-zero, a write 
14231 28 64 61 74 61 20 63 6f 70 69 65 64 20 66 72 6f  (data copied fro
14232 6d 0a 2a 2a 20 62 75 66 66 65 72 20 70 42 75 66  m.** buffer pBuf
14233 29 2e 0a 2a 2a 0a 2a 2a 20 41 20 74 6f 74 61 6c  )..**.** A total
14234 20 6f 66 20 22 61 6d 74 22 20 62 79 74 65 73 20   of "amt" bytes 
14235 61 72 65 20 72 65 61 64 20 6f 72 20 77 72 69 74  are read or writ
14236 74 65 6e 20 62 65 67 69 6e 6e 69 6e 67 20 61 74  ten beginning at
14237 20 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 20 44 61   "offset"..** Da
14238 74 61 20 69 73 20 72 65 61 64 20 74 6f 20 6f 72  ta is read to or
14239 20 66 72 6f 6d 20 74 68 65 20 62 75 66 66 65 72   from the buffer
1423a 20 70 42 75 66 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   pBuf..**.** Thi
1423b 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73 20 6e  s routine does n
1423c 6f 74 20 6d 61 6b 65 20 61 20 64 69 73 74 69 6e  ot make a distin
1423d 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 6b 65  ction between ke
1423e 79 20 61 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 49  y and data..** I
1423f 74 20 6a 75 73 74 20 72 65 61 64 73 20 6f 72 20  t just reads or 
14240 77 72 69 74 65 73 20 62 79 74 65 73 20 66 72 6f  writes bytes fro
14241 6d 20 74 68 65 20 70 61 79 6c 6f 61 64 20 61 72  m the payload ar
14242 65 61 2e 20 20 44 61 74 61 20 6d 69 67 68 74 20  ea.  Data might 
14243 0a 2a 2a 20 61 70 70 65 61 72 20 6f 6e 20 74 68  .** appear on th
14244 65 20 6d 61 69 6e 20 70 61 67 65 20 6f 72 20 62  e main page or b
14245 65 20 73 63 61 74 74 65 72 65 64 20 6f 75 74 20  e scattered out 
14246 6f 6e 20 6d 75 6c 74 69 70 6c 65 20 6f 76 65 72  on multiple over
14247 66 6c 6f 77 20 0a 2a 2a 20 70 61 67 65 73 2e 0a  flow .** pages..
14248 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 42 74 43  **.** If the BtC
14249 75 72 73 6f 72 2e 69 73 49 6e 63 72 62 6c 6f 62  ursor.isIncrblob
1424a 48 61 6e 64 6c 65 20 66 6c 61 67 20 69 73 20 73  Handle flag is s
1424b 65 74 2c 20 61 6e 64 20 74 68 65 20 63 75 72 72  et, and the curr
1424c 65 6e 74 0a 2a 2a 20 63 75 72 73 6f 72 20 65 6e  ent.** cursor en
1424d 74 72 79 20 75 73 65 73 20 6f 6e 65 20 6f 72 20  try uses one or 
1424e 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20 70 61  more overflow pa
1424f 67 65 73 2c 20 74 68 69 73 20 66 75 6e 63 74 69  ges, this functi
14250 6f 6e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 73 20  on.** allocates 
14251 73 70 61 63 65 20 66 6f 72 20 61 6e 64 20 6c 61  space for and la
14252 7a 69 6c 79 20 70 6f 70 6c 75 61 74 65 73 20 74  zily popluates t
14253 68 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65  he overflow page
14254 2d 6c 69 73 74 20 0a 2a 2a 20 63 61 63 68 65 20  -list .** cache 
14255 61 72 72 61 79 20 28 42 74 43 75 72 73 6f 72 2e  array (BtCursor.
14256 61 4f 76 65 72 66 6c 6f 77 29 2e 20 53 75 62 73  aOverflow). Subs
14257 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 75 73 65  equent calls use
14258 20 74 68 69 73 0a 2a 2a 20 63 61 63 68 65 20 74   this.** cache t
14259 6f 20 6d 61 6b 65 20 73 65 65 6b 69 6e 67 20 74  o make seeking t
1425a 6f 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 6f  o the supplied o
1425b 66 66 73 65 74 20 6d 6f 72 65 20 65 66 66 69 63  ffset more effic
1425c 69 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65  ient..**.** Once
1425d 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67   an overflow pag
1425e 65 2d 6c 69 73 74 20 63 61 63 68 65 20 68 61 73  e-list cache has
1425f 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 2c   been allocated,
14260 20 69 74 20 6d 61 79 20 62 65 0a 2a 2a 20 69 6e   it may be.** in
14261 76 61 6c 69 64 61 74 65 64 20 69 66 20 73 6f 6d  validated if som
14262 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72 20 77  e other cursor w
14263 72 69 74 65 73 20 74 6f 20 74 68 65 20 73 61 6d  rites to the sam
14264 65 20 74 61 62 6c 65 2c 20 6f 72 20 69 66 0a 2a  e table, or if.*
14265 2a 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  * the cursor is 
14266 6d 6f 76 65 64 20 74 6f 20 61 20 64 69 66 66 65  moved to a diffe
14267 72 65 6e 74 20 72 6f 77 2e 20 41 64 64 69 74 69  rent row. Additi
14268 6f 6e 61 6c 6c 79 2c 20 69 6e 20 61 75 74 6f 2d  onally, in auto-
14269 76 61 63 75 75 6d 0a 2a 2a 20 6d 6f 64 65 2c 20  vacuum.** mode, 
1426a 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 76  the following ev
1426b 65 6e 74 73 20 6d 61 79 20 69 6e 76 61 6c 69 64  ents may invalid
1426c 61 74 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20  ate an overflow 
1426d 70 61 67 65 2d 6c 69 73 74 20 63 61 63 68 65 2e  page-list cache.
1426e 0a 2a 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6e  .**.**   * An in
1426f 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75 75 6d  cremental vacuum
14270 2c 0a 2a 2a 20 20 20 2a 20 41 20 63 6f 6d 6d 69  ,.**   * A commi
14271 74 20 69 6e 20 61 75 74 6f 5f 76 61 63 75 75 6d  t in auto_vacuum
14272 3d 22 66 75 6c 6c 22 20 6d 6f 64 65 2c 0a 2a 2a  ="full" mode,.**
14273 20 20 20 2a 20 43 72 65 61 74 69 6e 67 20 61 20     * Creating a 
14274 74 61 62 6c 65 20 28 6d 61 79 20 72 65 71 75 69  table (may requi
14275 72 65 20 6d 6f 76 69 6e 67 20 61 6e 20 6f 76 65  re moving an ove
14276 72 66 6c 6f 77 20 70 61 67 65 29 2e 0a 2a 2f 0a  rflow page)..*/.
14277 73 74 61 74 69 63 20 69 6e 74 20 61 63 63 65 73  static int acces
14278 73 50 61 79 6c 6f 61 64 28 0a 20 20 42 74 43 75  sPayload(.  BtCu
14279 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
1427a 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74   /* Cursor point
1427b 69 6e 67 20 74 6f 20 65 6e 74 72 79 20 74 6f 20  ing to entry to 
1427c 72 65 61 64 20 66 72 6f 6d 20 2a 2f 0a 20 20 75  read from */.  u
1427d 33 32 20 6f 66 66 73 65 74 2c 20 20 20 20 20 20  32 offset,      
1427e 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 72 65 61      /* Begin rea
1427f 64 69 6e 67 20 74 68 69 73 20 66 61 72 20 69 6e  ding this far in
14280 74 6f 20 70 61 79 6c 6f 61 64 20 2a 2f 0a 20 20  to payload */.  
14281 75 33 32 20 61 6d 74 2c 20 20 20 20 20 20 20 20  u32 amt,        
14282 20 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68 69       /* Read thi
14283 73 20 6d 61 6e 79 20 62 79 74 65 73 20 2a 2f 0a  s many bytes */.
14284 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
14285 2a 70 42 75 66 2c 20 2f 2a 20 57 72 69 74 65 20  *pBuf, /* Write 
14286 74 68 65 20 62 79 74 65 73 20 69 6e 74 6f 20 74  the bytes into t
14287 68 69 73 20 62 75 66 66 65 72 20 2a 2f 20 0a 20  his buffer */ . 
14288 20 69 6e 74 20 73 6b 69 70 4b 65 79 2c 20 20 20   int skipKey,   
14289 20 20 20 20 20 20 2f 2a 20 6f 66 66 73 65 74 20        /* offset 
1428a 62 65 67 69 6e 73 20 61 74 20 64 61 74 61 20 69  begins at data i
1428b 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20 2a  f this is true *
1428c 2f 0a 20 20 69 6e 74 20 65 4f 70 20 20 20 20 20  /.  int eOp     
1428d 20 20 20 20 20 20 20 20 20 2f 2a 20 7a 65 72 6f           /* zero
1428e 20 74 6f 20 72 65 61 64 2e 20 6e 6f 6e 2d 7a 65   to read. non-ze
1428f 72 6f 20 74 6f 20 77 72 69 74 65 2e 20 2a 2f 0a  ro to write. */.
14290 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  ){.  unsigned ch
14291 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b 0a 20 20  ar *aPayload;.  
14292 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
14293 4f 4b 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a  OK;.  u32 nKey;.
14294 20 20 69 6e 74 20 69 49 64 78 20 3d 20 30 3b 0a    int iIdx = 0;.
14295 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65    MemPage *pPage
14296 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
14297 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 20 2f 2a  pCur->iPage]; /*
14298 20 42 74 72 65 65 20 70 61 67 65 20 6f 66 20 63   Btree page of c
14299 75 72 72 65 6e 74 20 65 6e 74 72 79 20 2a 2f 0a  urrent entry */.
1429a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 20    BtShared *pBt 
1429b 3d 20 70 43 75 72 2d 3e 70 42 74 3b 20 20 20 20  = pCur->pBt;    
1429c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1429d 20 42 74 72 65 65 20 74 68 69 73 20 63 75 72 73   Btree this curs
1429e 6f 72 20 62 65 6c 6f 6e 67 73 20 74 6f 20 2a 2f  or belongs to */
1429f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 67  ..  assert( pPag
142a0 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  e );.  assert( p
142a1 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
142a2 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61  SOR_VALID );.  a
142a3 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 69 49  ssert( pCur->aiI
142a4 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c  dx[pCur->iPage]<
142a5 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a  pPage->nCell );.
142a6 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72    assert( cursor
142a7 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29  HoldsMutex(pCur)
142a8 20 29 3b 0a 0a 20 20 67 65 74 43 65 6c 6c 49 6e   );..  getCellIn
142a9 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79  fo(pCur);.  aPay
142aa 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66  load = pCur->inf
142ab 6f 2e 70 43 65 6c 6c 20 2b 20 70 43 75 72 2d 3e  o.pCell + pCur->
142ac 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20  info.nHeader;.  
142ad 6e 4b 65 79 20 3d 20 28 70 50 61 67 65 2d 3e 69  nKey = (pPage->i
142ae 6e 74 4b 65 79 20 3f 20 30 20 3a 20 70 43 75 72  ntKey ? 0 : pCur
142af 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 29 3b 0a 0a 20  ->info.nKey);.. 
142b0 20 69 66 28 20 73 6b 69 70 4b 65 79 20 29 7b 0a   if( skipKey ){.
142b1 20 20 20 20 6f 66 66 73 65 74 20 2b 3d 20 6e 4b      offset += nK
142b2 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6f 66  ey;.  }.  if( of
142b3 66 73 65 74 2b 61 6d 74 20 3e 20 6e 4b 65 79 2b  fset+amt > nKey+
142b4 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 44 61 74 61  pCur->info.nData
142b5 20 0a 20 20 20 7c 7c 20 26 61 50 61 79 6c 6f 61   .   || &aPayloa
142b6 64 5b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f  d[pCur->info.nLo
142b7 63 61 6c 5d 20 3e 20 26 70 50 61 67 65 2d 3e 61  cal] > &pPage->a
142b8 44 61 74 61 5b 70 42 74 2d 3e 75 73 61 62 6c 65  Data[pBt->usable
142b9 53 69 7a 65 5d 0a 20 20 29 7b 0a 20 20 20 20 2f  Size].  ){.    /
142ba 2a 20 54 72 79 69 6e 67 20 74 6f 20 72 65 61 64  * Trying to read
142bb 20 6f 72 20 77 72 69 74 65 20 70 61 73 74 20 74   or write past t
142bc 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 64 61  he end of the da
142bd 74 61 20 69 73 20 61 6e 20 65 72 72 6f 72 20 2a  ta is an error *
142be 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  /.    return SQL
142bf 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54  ITE_CORRUPT_BKPT
142c0 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63  ;.  }..  /* Chec
142c1 6b 20 69 66 20 64 61 74 61 20 6d 75 73 74 20 62  k if data must b
142c2 65 20 72 65 61 64 2f 77 72 69 74 74 65 6e 20 74  e read/written t
142c3 6f 2f 66 72 6f 6d 20 74 68 65 20 62 74 72 65 65  o/from the btree
142c4 20 70 61 67 65 20 69 74 73 65 6c 66 2e 20 2a 2f   page itself. */
142c5 0a 20 20 69 66 28 20 6f 66 66 73 65 74 3c 70 43  .  if( offset<pC
142c6 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20  ur->info.nLocal 
142c7 29 7b 0a 20 20 20 20 69 6e 74 20 61 20 3d 20 61  ){.    int a = a
142c8 6d 74 3b 0a 20 20 20 20 69 66 28 20 61 2b 6f 66  mt;.    if( a+of
142c9 66 73 65 74 3e 70 43 75 72 2d 3e 69 6e 66 6f 2e  fset>pCur->info.
142ca 6e 4c 6f 63 61 6c 20 29 7b 0a 20 20 20 20 20 20  nLocal ){.      
142cb 61 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  a = pCur->info.n
142cc 4c 6f 63 61 6c 20 2d 20 6f 66 66 73 65 74 3b 0a  Local - offset;.
142cd 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 63      }.    rc = c
142ce 6f 70 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79  opyPayload(&aPay
142cf 6c 6f 61 64 5b 6f 66 66 73 65 74 5d 2c 20 70 42  load[offset], pB
142d0 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 50 61 67  uf, a, eOp, pPag
142d1 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
142d2 20 6f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20   offset = 0;.   
142d3 20 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20   pBuf += a;.    
142d4 61 6d 74 20 2d 3d 20 61 3b 0a 20 20 7d 65 6c 73  amt -= a;.  }els
142d5 65 7b 0a 20 20 20 20 6f 66 66 73 65 74 20 2d 3d  e{.    offset -=
142d6 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63   pCur->info.nLoc
142d7 61 6c 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72  al;.  }..  if( r
142d8 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
142d9 61 6d 74 3e 30 20 29 7b 0a 20 20 20 20 63 6f 6e  amt>0 ){.    con
142da 73 74 20 75 33 32 20 6f 76 66 6c 53 69 7a 65 20  st u32 ovflSize 
142db 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
142dc 65 20 2d 20 34 3b 20 20 2f 2a 20 42 79 74 65 73  e - 4;  /* Bytes
142dd 20 63 6f 6e 74 65 6e 74 20 70 65 72 20 6f 76 66   content per ovf
142de 6c 20 70 61 67 65 20 2a 2f 0a 20 20 20 20 50 67  l page */.    Pg
142df 6e 6f 20 6e 65 78 74 50 61 67 65 3b 0a 0a 20 20  no nextPage;..  
142e0 20 20 6e 65 78 74 50 61 67 65 20 3d 20 67 65 74    nextPage = get
142e1 34 62 79 74 65 28 26 61 50 61 79 6c 6f 61 64 5b  4byte(&aPayload[
142e2 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61  pCur->info.nLoca
142e3 6c 5d 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  l]);..#ifndef SQ
142e4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c  LITE_OMIT_INCRBL
142e5 4f 42 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  OB.    /* If the
142e6 20 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c   isIncrblobHandl
142e7 65 20 66 6c 61 67 20 69 73 20 73 65 74 20 61 6e  e flag is set an
142e8 64 20 74 68 65 20 42 74 43 75 72 73 6f 72 2e 61  d the BtCursor.a
142e9 4f 76 65 72 66 6c 6f 77 5b 5d 0a 20 20 20 20 2a  Overflow[].    *
142ea 2a 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 61  * has not been a
142eb 6c 6c 6f 63 61 74 65 64 2c 20 61 6c 6c 6f 63 61  llocated, alloca
142ec 74 65 20 69 74 20 6e 6f 77 2e 20 54 68 65 20 61  te it now. The a
142ed 72 72 61 79 20 69 73 20 73 69 7a 65 64 20 61 74  rray is sized at
142ee 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6e 74 72  .    ** one entr
142ef 79 20 66 6f 72 20 65 61 63 68 20 6f 76 65 72 66  y for each overf
142f0 6c 6f 77 20 70 61 67 65 20 69 6e 20 74 68 65 20  low page in the 
142f1 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20  overflow chain. 
142f2 54 68 65 0a 20 20 20 20 2a 2a 20 70 61 67 65 20  The.    ** page 
142f3 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 66 69  number of the fi
142f4 72 73 74 20 6f 76 65 72 66 6c 6f 77 20 70 61 67  rst overflow pag
142f5 65 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 61  e is stored in a
142f6 4f 76 65 72 66 6c 6f 77 5b 30 5d 2c 0a 20 20 20  Overflow[0],.   
142f7 20 2a 2a 20 65 74 63 2e 20 41 20 76 61 6c 75 65   ** etc. A value
142f8 20 6f 66 20 30 20 69 6e 20 74 68 65 20 61 4f 76   of 0 in the aOv
142f9 65 72 66 6c 6f 77 5b 5d 20 61 72 72 61 79 20 6d  erflow[] array m
142fa 65 61 6e 73 20 22 6e 6f 74 20 79 65 74 20 6b 6e  eans "not yet kn
142fb 6f 77 6e 22 0a 20 20 20 20 2a 2a 20 28 74 68 65  own".    ** (the
142fc 20 63 61 63 68 65 20 69 73 20 6c 61 7a 69 6c 79   cache is lazily
142fd 20 70 6f 70 75 6c 61 74 65 64 29 2e 0a 20 20 20   populated)..   
142fe 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72   */.    if( pCur
142ff 2d 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64  ->isIncrblobHand
14300 6c 65 20 26 26 20 21 70 43 75 72 2d 3e 61 4f 76  le && !pCur->aOv
14301 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20  erflow ){.      
14302 69 6e 74 20 6e 4f 76 66 6c 20 3d 20 28 70 43 75  int nOvfl = (pCu
14303 72 2d 3e 69 6e 66 6f 2e 6e 50 61 79 6c 6f 61 64  r->info.nPayload
14304 2d 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63  -pCur->info.nLoc
14305 61 6c 2b 6f 76 66 6c 53 69 7a 65 2d 31 29 2f 6f  al+ovflSize-1)/o
14306 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20 20 20 70  vflSize;.      p
14307 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20 3d  Cur->aOverflow =
14308 20 28 50 67 6e 6f 20 2a 29 73 71 6c 69 74 65 33   (Pgno *)sqlite3
14309 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65 6f  MallocZero(sizeo
1430a 66 28 50 67 6e 6f 29 2a 6e 4f 76 66 6c 29 3b 0a  f(Pgno)*nOvfl);.
1430b 20 20 20 20 20 20 69 66 28 20 6e 4f 76 66 6c 20        if( nOvfl 
1430c 26 26 20 21 70 43 75 72 2d 3e 61 4f 76 65 72 66  && !pCur->aOverf
1430d 6c 6f 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72  low ){.        r
1430e 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
1430f 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
14310 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 6f  .    /* If the o
14311 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6c 69 73  verflow page-lis
14312 74 20 63 61 63 68 65 20 68 61 73 20 62 65 65 6e  t cache has been
14313 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 74   allocated and t
14314 68 65 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20  he.    ** entry 
14315 66 6f 72 20 74 68 65 20 66 69 72 73 74 20 72 65  for the first re
14316 71 75 69 72 65 64 20 6f 76 65 72 66 6c 6f 77 20  quired overflow 
14317 70 61 67 65 20 69 73 20 76 61 6c 69 64 2c 20 73  page is valid, s
14318 6b 69 70 0a 20 20 20 20 2a 2a 20 64 69 72 65 63  kip.    ** direc
14319 74 6c 79 20 74 6f 20 69 74 2e 0a 20 20 20 20 2a  tly to it..    *
1431a 2f 0a 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e  /.    if( pCur->
1431b 61 4f 76 65 72 66 6c 6f 77 20 26 26 20 70 43 75  aOverflow && pCu
1431c 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 6f 66 66  r->aOverflow[off
1431d 73 65 74 2f 6f 76 66 6c 53 69 7a 65 5d 20 29 7b  set/ovflSize] ){
1431e 0a 20 20 20 20 20 20 69 49 64 78 20 3d 20 28 6f  .      iIdx = (o
1431f 66 66 73 65 74 2f 6f 76 66 6c 53 69 7a 65 29 3b  ffset/ovflSize);
14320 0a 20 20 20 20 20 20 6e 65 78 74 50 61 67 65 20  .      nextPage 
14321 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f  = pCur->aOverflo
14322 77 5b 69 49 64 78 5d 3b 0a 20 20 20 20 20 20 6f  w[iIdx];.      o
14323 66 66 73 65 74 20 3d 20 28 6f 66 66 73 65 74 25  ffset = (offset%
14324 6f 76 66 6c 53 69 7a 65 29 3b 0a 20 20 20 20 7d  ovflSize);.    }
14325 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 66 6f 72  .#endif..    for
14326 28 20 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  ( ; rc==SQLITE_O
14327 4b 20 26 26 20 61 6d 74 3e 30 20 26 26 20 6e 65  K && amt>0 && ne
14328 78 74 50 61 67 65 3b 20 69 49 64 78 2b 2b 29 7b  xtPage; iIdx++){
14329 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1432a 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42 0a 20  _OMIT_INCRBLOB. 
1432b 20 20 20 20 20 2f 2a 20 49 66 20 72 65 71 75 69       /* If requi
1432c 72 65 64 2c 20 70 6f 70 75 6c 61 74 65 20 74 68  red, populate th
1432d 65 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 2d  e overflow page-
1432e 6c 69 73 74 20 63 61 63 68 65 2e 20 2a 2f 0a 20  list cache. */. 
1432f 20 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 61       if( pCur->a
14330 4f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20  Overflow ){.    
14331 20 20 20 20 61 73 73 65 72 74 28 21 70 43 75 72      assert(!pCur
14332 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69 49 64 78  ->aOverflow[iIdx
14333 5d 20 7c 7c 20 70 43 75 72 2d 3e 61 4f 76 65 72  ] || pCur->aOver
14334 66 6c 6f 77 5b 69 49 64 78 5d 3d 3d 6e 65 78 74  flow[iIdx]==next
14335 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70  Page);.        p
14336 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 5b 69  Cur->aOverflow[i
14337 49 64 78 5d 20 3d 20 6e 65 78 74 50 61 67 65 3b  Idx] = nextPage;
14338 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a  .      }.#endif.
14339 0a 20 20 20 20 20 20 69 66 28 20 6f 66 66 73 65  .      if( offse
1433a 74 3e 3d 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20  t>=ovflSize ){. 
1433b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 6e         /* The on
1433c 6c 79 20 72 65 61 73 6f 6e 20 74 6f 20 72 65 61  ly reason to rea
1433d 64 20 74 68 69 73 20 70 61 67 65 20 69 73 20 74  d this page is t
1433e 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 70 61 67  o obtain the pag
1433f 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d  e.        ** num
14340 62 65 72 20 66 6f 72 20 74 68 65 20 6e 65 78 74  ber for the next
14341 20 70 61 67 65 20 69 6e 20 74 68 65 20 6f 76 65   page in the ove
14342 72 66 6c 6f 77 20 63 68 61 69 6e 2e 20 54 68 65  rflow chain. The
14343 20 70 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a   page.        **
14344 20 64 61 74 61 20 69 73 20 6e 6f 74 20 72 65 71   data is not req
14345 75 69 72 65 64 2e 20 53 6f 20 66 69 72 73 74 20  uired. So first 
14346 74 72 79 20 74 6f 20 6c 6f 6f 6b 75 70 20 74 68  try to lookup th
14347 65 20 6f 76 65 72 66 6c 6f 77 0a 20 20 20 20 20  e overflow.     
14348 20 20 20 2a 2a 20 70 61 67 65 2d 6c 69 73 74 20     ** page-list 
14349 63 61 63 68 65 2c 20 69 66 20 61 6e 79 2c 20 74  cache, if any, t
1434a 68 65 6e 20 66 61 6c 6c 20 62 61 63 6b 20 74 6f  hen fall back to
1434b 20 74 68 65 20 67 65 74 4f 76 65 72 66 6c 6f 77   the getOverflow
1434c 50 61 67 65 28 29 0a 20 20 20 20 20 20 20 20 2a  Page().        *
1434d 2a 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20  * function..    
1434e 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53      */.#ifndef S
1434f 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
14350 4c 4f 42 0a 20 20 20 20 20 20 20 20 69 66 28 20  LOB.        if( 
14351 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c 6f 77 20  pCur->aOverflow 
14352 26 26 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c  && pCur->aOverfl
14353 6f 77 5b 69 49 64 78 2b 31 5d 20 29 7b 0a 20 20  ow[iIdx+1] ){.  
14354 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67 65          nextPage
14355 20 3d 20 70 43 75 72 2d 3e 61 4f 76 65 72 66 6c   = pCur->aOverfl
14356 6f 77 5b 69 49 64 78 2b 31 5d 3b 0a 20 20 20 20  ow[iIdx+1];.    
14357 20 20 20 20 7d 20 65 6c 73 65 20 0a 23 65 6e 64      } else .#end
14358 69 66 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  if.          rc 
14359 3d 20 67 65 74 4f 76 65 72 66 6c 6f 77 50 61 67  = getOverflowPag
1435a 65 28 70 42 74 2c 20 6e 65 78 74 50 61 67 65 2c  e(pBt, nextPage,
1435b 20 30 2c 20 26 6e 65 78 74 50 61 67 65 29 3b 0a   0, &nextPage);.
1435c 20 20 20 20 20 20 20 20 6f 66 66 73 65 74 20 2d          offset -
1435d 3d 20 6f 76 66 6c 53 69 7a 65 3b 0a 20 20 20 20  = ovflSize;.    
1435e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1435f 20 2f 2a 20 4e 65 65 64 20 74 6f 20 72 65 61 64   /* Need to read
14360 20 74 68 69 73 20 70 61 67 65 20 70 72 6f 70 65   this page prope
14361 72 6c 79 2e 20 49 74 20 63 6f 6e 74 61 69 6e 73  rly. It contains
14362 20 73 6f 6d 65 20 6f 66 20 74 68 65 0a 20 20 20   some of the.   
14363 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20 6f 66       ** range of
14364 20 64 61 74 61 20 74 68 61 74 20 69 73 20 62 65   data that is be
14365 69 6e 67 20 72 65 61 64 20 28 65 4f 70 3d 3d 30  ing read (eOp==0
14366 29 20 6f 72 20 77 72 69 74 74 65 6e 20 28 65 4f  ) or written (eO
14367 70 21 3d 30 29 2e 0a 20 20 20 20 20 20 20 20 2a  p!=0)..        *
14368 2f 0a 20 20 20 20 20 20 20 20 44 62 50 61 67 65  /.        DbPage
14369 20 2a 70 44 62 50 61 67 65 3b 0a 20 20 20 20 20   *pDbPage;.     
1436a 20 20 20 69 6e 74 20 61 20 3d 20 61 6d 74 3b 0a     int a = amt;.
1436b 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1436c 69 74 65 33 50 61 67 65 72 47 65 74 28 70 42 74  ite3PagerGet(pBt
1436d 2d 3e 70 50 61 67 65 72 2c 20 6e 65 78 74 50 61  ->pPager, nextPa
1436e 67 65 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20  ge, &pDbPage);. 
1436f 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
14370 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
14371 20 20 20 20 20 20 61 50 61 79 6c 6f 61 64 20 3d        aPayload =
14372 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74   sqlite3PagerGet
14373 44 61 74 61 28 70 44 62 50 61 67 65 29 3b 0a 20  Data(pDbPage);. 
14374 20 20 20 20 20 20 20 20 20 6e 65 78 74 50 61 67           nextPag
14375 65 20 3d 20 67 65 74 34 62 79 74 65 28 61 50 61  e = get4byte(aPa
14376 79 6c 6f 61 64 29 3b 0a 20 20 20 20 20 20 20 20  yload);.        
14377 20 20 69 66 28 20 61 20 2b 20 6f 66 66 73 65 74    if( a + offset
14378 20 3e 20 6f 76 66 6c 53 69 7a 65 20 29 7b 0a 20   > ovflSize ){. 
14379 20 20 20 20 20 20 20 20 20 20 20 61 20 3d 20 6f             a = o
1437a 76 66 6c 53 69 7a 65 20 2d 20 6f 66 66 73 65 74  vflSize - offset
1437b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1437c 20 20 20 20 20 20 20 20 72 63 20 3d 20 63 6f 70          rc = cop
1437d 79 50 61 79 6c 6f 61 64 28 26 61 50 61 79 6c 6f  yPayload(&aPaylo
1437e 61 64 5b 6f 66 66 73 65 74 2b 34 5d 2c 20 70 42  ad[offset+4], pB
1437f 75 66 2c 20 61 2c 20 65 4f 70 2c 20 70 44 62 50  uf, a, eOp, pDbP
14380 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
14381 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
14382 66 28 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20  f(pDbPage);.    
14383 20 20 20 20 20 20 6f 66 66 73 65 74 20 3d 20 30        offset = 0
14384 3b 0a 20 20 20 20 20 20 20 20 20 20 61 6d 74 20  ;.          amt 
14385 2d 3d 20 61 3b 0a 20 20 20 20 20 20 20 20 20 20  -= a;.          
14386 70 42 75 66 20 2b 3d 20 61 3b 0a 20 20 20 20 20  pBuf += a;.     
14387 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
14388 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63   }.  }..  if( rc
14389 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 61  ==SQLITE_OK && a
1438a 6d 74 3e 30 20 29 7b 0a 20 20 20 20 72 65 74 75  mt>0 ){.    retu
1438b 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50  rn SQLITE_CORRUP
1438c 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 65  T_BKPT;.  }.  re
1438d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1438e 2a 20 52 65 61 64 20 70 61 72 74 20 6f 66 20 74  * Read part of t
1438f 68 65 20 6b 65 79 20 61 73 73 6f 63 69 61 74 65  he key associate
14390 64 20 77 69 74 68 20 63 75 72 73 6f 72 20 70 43  d with cursor pC
14391 75 72 2e 20 20 45 78 61 63 74 6c 79 0a 2a 2a 20  ur.  Exactly.** 
14392 22 61 6d 74 22 20 62 79 74 65 73 20 77 69 6c 6c  "amt" bytes will
14393 20 62 65 20 74 72 61 6e 73 66 65 72 65 64 20 69   be transfered i
14394 6e 74 6f 20 70 42 75 66 5b 5d 2e 20 20 54 68 65  nto pBuf[].  The
14395 20 74 72 61 6e 73 66 65 72 0a 2a 2a 20 62 65 67   transfer.** beg
14396 69 6e 73 20 61 74 20 22 6f 66 66 73 65 74 22 2e  ins at "offset".
14397 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
14398 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
14399 73 73 20 6f 72 20 61 6e 20 65 72 72 6f 72 20 63  ss or an error c
1439a 6f 64 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  ode if anything 
1439b 67 6f 65 73 0a 2a 2a 20 77 72 6f 6e 67 2e 20 20  goes.** wrong.  
1439c 41 6e 20 65 72 72 6f 72 20 69 73 20 72 65 74 75  An error is retu
1439d 72 6e 65 64 20 69 66 20 22 6f 66 66 73 65 74 2b  rned if "offset+
1439e 61 6d 74 22 20 69 73 20 6c 61 72 67 65 72 20 74  amt" is larger t
1439f 68 61 6e 0a 2a 2a 20 74 68 65 20 61 76 61 69 6c  han.** the avail
143a0 61 62 6c 65 20 70 61 79 6c 6f 61 64 2e 0a 2a 2f  able payload..*/
143a1 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
143a2 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
143a3 4b 65 79 28 42 74 43 75 72 73 6f 72 20 2a 70 43  Key(BtCursor *pC
143a4 75 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c 20  ur, u32 offset, 
143a5 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a 70  u32 amt, void *p
143a6 42 75 66 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Buf){.  int rc;.
143a7 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
143a8 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
143a9 29 20 29 3b 0a 20 20 72 63 20 3d 20 72 65 73 74  ) );.  rc = rest
143aa 6f 72 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f  oreCursorPositio
143ab 6e 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  n(pCur);.  if( r
143ac 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
143ad 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
143ae 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52  ->eState==CURSOR
143af 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20 20 61 73  _VALID );.    as
143b0 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
143b1 65 3e 3d 30 20 26 26 20 70 43 75 72 2d 3e 61 70  e>=0 && pCur->ap
143b2 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
143b3 5d 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75  ] );.    if( pCu
143b4 72 2d 3e 61 70 50 61 67 65 5b 30 5d 2d 3e 69 6e  r->apPage[0]->in
143b5 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 72 65  tKey ){.      re
143b6 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
143b7 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a  UPT_BKPT;.    }.
143b8 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75 72      assert( pCur
143b9 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
143ba 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70 50 61 67  age]<pCur->apPag
143bb 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d 3e  e[pCur->iPage]->
143bc 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20 72 63 20  nCell );.    rc 
143bd 3d 20 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28  = accessPayload(
143be 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
143bf 74 2c 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61  t, (unsigned cha
143c0 72 2a 29 70 42 75 66 2c 20 30 2c 20 30 29 3b 0a  r*)pBuf, 0, 0);.
143c1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
143c2 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20 70  .}../*.** Read p
143c3 61 72 74 20 6f 66 20 74 68 65 20 64 61 74 61 20  art of the data 
143c4 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
143c5 63 75 72 73 6f 72 20 70 43 75 72 2e 20 20 45 78  cursor pCur.  Ex
143c6 61 63 74 6c 79 0a 2a 2a 20 22 61 6d 74 22 20 62  actly.** "amt" b
143c7 79 74 65 73 20 77 69 6c 6c 20 62 65 20 74 72 61  ytes will be tra
143c8 6e 73 66 65 72 65 64 20 69 6e 74 6f 20 70 42 75  nsfered into pBu
143c9 66 5b 5d 2e 20 20 54 68 65 20 74 72 61 6e 73 66  f[].  The transf
143ca 65 72 0a 2a 2a 20 62 65 67 69 6e 73 20 61 74 20  er.** begins at 
143cb 22 6f 66 66 73 65 74 22 2e 0a 2a 2a 0a 2a 2a 20  "offset"..**.** 
143cc 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
143cd 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 61   on success or a
143ce 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 69 66 20  n error code if 
143cf 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 0a 2a 2a  anything goes.**
143d0 20 77 72 6f 6e 67 2e 20 20 41 6e 20 65 72 72 6f   wrong.  An erro
143d1 72 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  r is returned if
143d2 20 22 6f 66 66 73 65 74 2b 61 6d 74 22 20 69 73   "offset+amt" is
143d3 20 6c 61 72 67 65 72 20 74 68 61 6e 0a 2a 2a 20   larger than.** 
143d4 74 68 65 20 61 76 61 69 6c 61 62 6c 65 20 70 61  the available pa
143d5 79 6c 6f 61 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  yload..*/.SQLITE
143d6 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
143d7 69 74 65 33 42 74 72 65 65 44 61 74 61 28 42 74  ite3BtreeData(Bt
143d8 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75 33  Cursor *pCur, u3
143d9 32 20 6f 66 66 73 65 74 2c 20 75 33 32 20 61 6d  2 offset, u32 am
143da 74 2c 20 76 6f 69 64 20 2a 70 42 75 66 29 7b 0a  t, void *pBuf){.
143db 20 20 69 6e 74 20 72 63 3b 0a 0a 23 69 66 6e 64    int rc;..#ifnd
143dc 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
143dd 4e 43 52 42 4c 4f 42 0a 20 20 69 66 20 28 20 70  NCRBLOB.  if ( p
143de 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
143df 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b 0a 20  SOR_INVALID ){. 
143e0 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
143e1 5f 41 42 4f 52 54 3b 0a 20 20 7d 0a 23 65 6e 64  _ABORT;.  }.#end
143e2 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 63 75  if..  assert( cu
143e3 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
143e4 43 75 72 29 20 29 3b 0a 20 20 72 63 20 3d 20 72  Cur) );.  rc = r
143e5 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
143e6 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 69 66  tion(pCur);.  if
143e7 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
143e8 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
143e9 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
143ea 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 20  SOR_VALID );.   
143eb 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69   assert( pCur->i
143ec 50 61 67 65 3e 3d 30 20 26 26 20 70 43 75 72 2d  Page>=0 && pCur-
143ed 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
143ee 61 67 65 5d 20 29 3b 0a 20 20 20 20 61 73 73 65  age] );.    asse
143ef 72 74 28 20 70 43 75 72 2d 3e 61 69 49 64 78 5b  rt( pCur->aiIdx[
143f0 70 43 75 72 2d 3e 69 50 61 67 65 5d 3c 70 43 75  pCur->iPage]<pCu
143f1 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
143f2 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 20 29 3b  iPage]->nCell );
143f3 0a 20 20 20 20 72 63 20 3d 20 61 63 63 65 73 73  .    rc = access
143f4 50 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 6f 66  Payload(pCur, of
143f5 66 73 65 74 2c 20 61 6d 74 2c 20 70 42 75 66 2c  fset, amt, pBuf,
143f6 20 31 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   1, 0);.  }.  re
143f7 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
143f8 2a 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  * Return a point
143f9 65 72 20 74 6f 20 70 61 79 6c 6f 61 64 20 69 6e  er to payload in
143fa 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74  formation from t
143fb 68 65 20 65 6e 74 72 79 20 74 68 61 74 20 74 68  he entry that th
143fc 65 20 0a 2a 2a 20 70 43 75 72 20 63 75 72 73 6f  e .** pCur curso
143fd 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
143fe 2e 20 20 54 68 65 20 70 6f 69 6e 74 65 72 20 69  .  The pointer i
143ff 73 20 74 6f 20 74 68 65 20 62 65 67 69 6e 6e 69  s to the beginni
14400 6e 67 20 6f 66 0a 2a 2a 20 74 68 65 20 6b 65 79  ng of.** the key
14401 20 69 66 20 73 6b 69 70 4b 65 79 3d 3d 30 20 61   if skipKey==0 a
14402 6e 64 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  nd it points to 
14403 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66  the beginning of
14404 20 64 61 74 61 20 69 66 0a 2a 2a 20 73 6b 69 70   data if.** skip
14405 4b 65 79 3d 3d 31 2e 20 20 54 68 65 20 6e 75 6d  Key==1.  The num
14406 62 65 72 20 6f 66 20 62 79 74 65 73 20 6f 66 20  ber of bytes of 
14407 61 76 61 69 6c 61 62 6c 65 20 6b 65 79 2f 64 61  available key/da
14408 74 61 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a  ta is written.**
14409 20 69 6e 74 6f 20 2a 70 41 6d 74 2e 20 20 49 66   into *pAmt.  If
1440a 20 2a 70 41 6d 74 3d 3d 30 2c 20 74 68 65 6e 20   *pAmt==0, then 
1440b 74 68 65 20 76 61 6c 75 65 20 72 65 74 75 72 6e  the value return
1440c 65 64 20 77 69 6c 6c 20 6e 6f 74 20 62 65 0a 2a  ed will not be.*
1440d 2a 20 61 20 76 61 6c 69 64 20 70 6f 69 6e 74 65  * a valid pointe
1440e 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  r..**.** This ro
1440f 75 74 69 6e 65 20 69 73 20 61 6e 20 6f 70 74 69  utine is an opti
14410 6d 69 7a 61 74 69 6f 6e 2e 20 20 49 74 20 69 73  mization.  It is
14411 20 63 6f 6d 6d 6f 6e 20 66 6f 72 20 74 68 65 20   common for the 
14412 65 6e 74 69 72 65 20 6b 65 79 0a 2a 2a 20 61 6e  entire key.** an
14413 64 20 64 61 74 61 20 74 6f 20 66 69 74 20 6f 6e  d data to fit on
14414 20 74 68 65 20 6c 6f 63 61 6c 20 70 61 67 65 20   the local page 
14415 61 6e 64 20 66 6f 72 20 74 68 65 72 65 20 74 6f  and for there to
14416 20 62 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 0a   be no overflow.
14417 2a 2a 20 70 61 67 65 73 2e 20 20 57 68 65 6e 20  ** pages.  When 
14418 74 68 61 74 20 69 73 20 73 6f 2c 20 74 68 69 73  that is so, this
14419 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20   routine can be 
1441a 75 73 65 64 20 74 6f 20 61 63 63 65 73 73 20 74  used to access t
1441b 68 65 0a 2a 2a 20 6b 65 79 20 61 6e 64 20 64 61  he.** key and da
1441c 74 61 20 77 69 74 68 6f 75 74 20 6d 61 6b 69 6e  ta without makin
1441d 67 20 61 20 63 6f 70 79 2e 20 20 49 66 20 74 68  g a copy.  If th
1441e 65 20 6b 65 79 20 61 6e 64 2f 6f 72 20 64 61 74  e key and/or dat
1441f 61 20 73 70 69 6c 6c 73 0a 2a 2a 20 6f 6e 74 6f  a spills.** onto
14420 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2c   overflow pages,
14421 20 74 68 65 6e 20 61 63 63 65 73 73 50 61 79 6c   then accessPayl
14422 6f 61 64 28 29 20 6d 75 73 74 20 62 65 20 75 73  oad() must be us
14423 65 64 20 74 6f 20 72 65 61 73 73 65 6d 62 6c 79  ed to reassembly
14424 0a 2a 2a 20 74 68 65 20 6b 65 79 2f 64 61 74 61  .** the key/data
14425 20 61 6e 64 20 63 6f 70 79 20 69 74 20 69 6e 74   and copy it int
14426 6f 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64  o a preallocated
14427 20 62 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 54   buffer..**.** T
14428 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74 75 72  he pointer retur
14429 6e 65 64 20 62 79 20 74 68 69 73 20 72 6f 75 74  ned by this rout
1442a 69 6e 65 20 6c 6f 6f 6b 73 20 64 69 72 65 63 74  ine looks direct
1442b 6c 79 20 69 6e 74 6f 20 74 68 65 20 63 61 63 68  ly into the cach
1442c 65 64 0a 2a 2a 20 70 61 67 65 20 6f 66 20 74 68  ed.** page of th
1442d 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
1442e 20 64 61 74 61 20 6d 69 67 68 74 20 63 68 61 6e   data might chan
1442f 67 65 20 6f 72 20 6d 6f 76 65 20 74 68 65 20 6e  ge or move the n
14430 65 78 74 20 74 69 6d 65 0a 2a 2a 20 61 6e 79 20  ext time.** any 
14431 62 74 72 65 65 20 72 6f 75 74 69 6e 65 20 69 73  btree routine is
14432 20 63 61 6c 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74   called..*/.stat
14433 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
14434 64 20 63 68 61 72 20 2a 66 65 74 63 68 50 61 79  d char *fetchPay
14435 6c 6f 61 64 28 0a 20 20 42 74 43 75 72 73 6f 72  load(.  BtCursor
14436 20 2a 70 43 75 72 2c 20 20 20 20 20 20 2f 2a 20   *pCur,      /* 
14437 43 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67 20  Cursor pointing 
14438 74 6f 20 65 6e 74 72 79 20 74 6f 20 72 65 61 64  to entry to read
14439 20 66 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 2a   from */.  int *
1443a 70 41 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20  pAmt,           
1443b 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 75 6d  /* Write the num
1443c 62 65 72 20 6f 66 20 61 76 61 69 6c 61 62 6c 65  ber of available
1443d 20 62 79 74 65 73 20 68 65 72 65 20 2a 2f 0a 20   bytes here */. 
1443e 20 69 6e 74 20 73 6b 69 70 4b 65 79 20 20 20 20   int skipKey    
1443f 20 20 20 20 20 20 2f 2a 20 72 65 61 64 20 62 65        /* read be
14440 67 69 6e 6e 69 6e 67 20 61 74 20 64 61 74 61 20  ginning at data 
14441 69 66 20 74 68 69 73 20 69 73 20 74 72 75 65 20  if this is true 
14442 2a 2f 0a 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  */.){.  unsigned
14443 20 63 68 61 72 20 2a 61 50 61 79 6c 6f 61 64 3b   char *aPayload;
14444 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
14445 65 3b 0a 20 20 75 33 32 20 6e 4b 65 79 3b 0a 20  e;.  u32 nKey;. 
14446 20 75 33 32 20 6e 4c 6f 63 61 6c 3b 0a 0a 20 20   u32 nLocal;..  
14447 61 73 73 65 72 74 28 20 70 43 75 72 21 3d 30 20  assert( pCur!=0 
14448 26 26 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d  && pCur->iPage>=
14449 30 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61 67  0 && pCur->apPag
1444a 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 3b  e[pCur->iPage]);
1444b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1444c 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
1444d 56 41 4c 49 44 20 29 3b 0a 20 20 61 73 73 65 72  VALID );.  asser
1444e 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
1444f 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 70  tex(pCur) );.  p
14450 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
14451 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
14452 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72  ;.  assert( pCur
14453 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50  ->aiIdx[pCur->iP
14454 61 67 65 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c  age]<pPage->nCel
14455 6c 20 29 3b 0a 20 20 67 65 74 43 65 6c 6c 49 6e  l );.  getCellIn
14456 66 6f 28 70 43 75 72 29 3b 0a 20 20 61 50 61 79  fo(pCur);.  aPay
14457 6c 6f 61 64 20 3d 20 70 43 75 72 2d 3e 69 6e 66  load = pCur->inf
14458 6f 2e 70 43 65 6c 6c 3b 0a 20 20 61 50 61 79 6c  o.pCell;.  aPayl
14459 6f 61 64 20 2b 3d 20 70 43 75 72 2d 3e 69 6e 66  oad += pCur->inf
1445a 6f 2e 6e 48 65 61 64 65 72 3b 0a 20 20 69 66 28  o.nHeader;.  if(
1445b 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29   pPage->intKey )
1445c 7b 0a 20 20 20 20 6e 4b 65 79 20 3d 20 30 3b 0a  {.    nKey = 0;.
1445d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 6e 4b 65    }else{.    nKe
1445e 79 20 3d 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  y = pCur->info.n
1445f 4b 65 79 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73  Key;.  }.  if( s
14460 6b 69 70 4b 65 79 20 29 7b 0a 20 20 20 20 61 50  kipKey ){.    aP
14461 61 79 6c 6f 61 64 20 2b 3d 20 6e 4b 65 79 3b 0a  ayload += nKey;.
14462 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75      nLocal = pCu
14463 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 2d  r->info.nLocal -
14464 20 6e 4b 65 79 3b 0a 20 20 7d 65 6c 73 65 7b 0a   nKey;.  }else{.
14465 20 20 20 20 6e 4c 6f 63 61 6c 20 3d 20 70 43 75      nLocal = pCu
14466 72 2d 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a  r->info.nLocal;.
14467 20 20 20 20 69 66 28 20 6e 4c 6f 63 61 6c 3e 6e      if( nLocal>n
14468 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 6e 4c 6f  Key ){.      nLo
14469 63 61 6c 20 3d 20 6e 4b 65 79 3b 0a 20 20 20 20  cal = nKey;.    
1446a 7d 0a 20 20 7d 0a 20 20 2a 70 41 6d 74 20 3d 20  }.  }.  *pAmt = 
1446b 6e 4c 6f 63 61 6c 3b 0a 20 20 72 65 74 75 72 6e  nLocal;.  return
1446c 20 61 50 61 79 6c 6f 61 64 3b 0a 7d 0a 0a 0a 2f   aPayload;.}.../
1446d 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 65 6e 74  *.** For the ent
1446e 72 79 20 74 68 61 74 20 63 75 72 73 6f 72 20 70  ry that cursor p
1446f 43 75 72 20 69 73 20 70 6f 69 6e 74 20 74 6f 2c  Cur is point to,
14470 20 72 65 74 75 72 6e 20 61 73 0a 2a 2a 20 6d 61   return as.** ma
14471 6e 79 20 62 79 74 65 73 20 6f 66 20 74 68 65 20  ny bytes of the 
14472 6b 65 79 20 6f 72 20 64 61 74 61 20 61 73 20 61  key or data as a
14473 72 65 20 61 76 61 69 6c 61 62 6c 65 20 6f 6e 20  re available on 
14474 74 68 65 20 6c 6f 63 61 6c 0a 2a 2a 20 62 2d 74  the local.** b-t
14475 72 65 65 20 70 61 67 65 2e 20 20 57 72 69 74 65  ree page.  Write
14476 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 61   the number of a
14477 76 61 69 6c 61 62 6c 65 20 62 79 74 65 73 20 69  vailable bytes i
14478 6e 74 6f 20 2a 70 41 6d 74 2e 0a 2a 2a 0a 2a 2a  nto *pAmt..**.**
14479 20 54 68 65 20 70 6f 69 6e 74 65 72 20 72 65 74   The pointer ret
1447a 75 72 6e 65 64 20 69 73 20 65 70 68 65 6d 65 72  urned is ephemer
1447b 61 6c 2e 20 20 54 68 65 20 6b 65 79 2f 64 61 74  al.  The key/dat
1447c 61 20 6d 61 79 20 6d 6f 76 65 0a 2a 2a 20 6f 72  a may move.** or
1447d 20 62 65 20 64 65 73 74 72 6f 79 65 64 20 6f 6e   be destroyed on
1447e 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c 20 74   the next call t
1447f 6f 20 61 6e 79 20 42 74 72 65 65 20 72 6f 75 74  o any Btree rout
14480 69 6e 65 2c 0a 2a 2a 20 69 6e 63 6c 75 64 69 6e  ine,.** includin
14481 67 20 63 61 6c 6c 73 20 66 72 6f 6d 20 6f 74 68  g calls from oth
14482 65 72 20 74 68 72 65 61 64 73 20 61 67 61 69 6e  er threads again
14483 73 74 20 74 68 65 20 73 61 6d 65 20 63 61 63 68  st the same cach
14484 65 2e 0a 2a 2a 20 48 65 6e 63 65 2c 20 61 20 6d  e..** Hence, a m
14485 75 74 65 78 20 6f 6e 20 74 68 65 20 42 74 53 68  utex on the BtSh
14486 61 72 65 64 20 73 68 6f 75 6c 64 20 62 65 20 68  ared should be h
14487 65 6c 64 20 70 72 69 6f 72 20 74 6f 20 63 61 6c  eld prior to cal
14488 6c 69 6e 67 0a 2a 2a 20 74 68 69 73 20 72 6f 75  ling.** this rou
14489 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73  tine..**.** Thes
1448a 65 20 72 6f 75 74 69 6e 65 73 20 69 73 20 75 73  e routines is us
1448b 65 64 20 74 6f 20 67 65 74 20 71 75 69 63 6b 20  ed to get quick 
1448c 61 63 63 65 73 73 20 74 6f 20 6b 65 79 20 61 6e  access to key an
1448d 64 20 64 61 74 61 0a 2a 2a 20 69 6e 20 74 68 65  d data.** in the
1448e 20 63 6f 6d 6d 6f 6e 20 63 61 73 65 20 77 68 65   common case whe
1448f 72 65 20 6e 6f 20 6f 76 65 72 66 6c 6f 77 20 70  re no overflow p
14490 61 67 65 73 20 61 72 65 20 75 73 65 64 2e 0a 2a  ages are used..*
14491 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
14492 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
14493 69 74 65 33 42 74 72 65 65 4b 65 79 46 65 74 63  ite3BtreeKeyFetc
14494 68 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  h(BtCursor *pCur
14495 2c 20 69 6e 74 20 2a 70 41 6d 74 29 7b 0a 20 20  , int *pAmt){.  
14496 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
14497 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
14498 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53  ;.  if( pCur->eS
14499 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c  tate==CURSOR_VAL
1449a 49 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ID ){.    return
1449b 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 29 66 65   (const void*)fe
1449c 74 63 68 50 61 79 6c 6f 61 64 28 70 43 75 72 2c  tchPayload(pCur,
1449d 20 70 41 6d 74 2c 20 30 29 3b 0a 20 20 7d 0a 20   pAmt, 0);.  }. 
1449e 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 53 51 4c   return 0;.}.SQL
1449f 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
144a0 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 42  t void *sqlite3B
144a1 74 72 65 65 44 61 74 61 46 65 74 63 68 28 42 74  treeDataFetch(Bt
144a2 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e  Cursor *pCur, in
144a3 74 20 2a 70 41 6d 74 29 7b 0a 20 20 61 73 73 65  t *pAmt){.  asse
144a4 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d  rt( cursorHoldsM
144a5 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20  utex(pCur) );.  
144a6 69 66 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  if( pCur->eState
144a7 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
144a8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 63 6f  {.    return (co
144a9 6e 73 74 20 76 6f 69 64 2a 29 66 65 74 63 68 50  nst void*)fetchP
144aa 61 79 6c 6f 61 64 28 70 43 75 72 2c 20 70 41 6d  ayload(pCur, pAm
144ab 74 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74  t, 1);.  }.  ret
144ac 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a  urn 0;.}.../*.**
144ad 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72   Move the cursor
144ae 20 64 6f 77 6e 20 74 6f 20 61 20 6e 65 77 20 63   down to a new c
144af 68 69 6c 64 20 70 61 67 65 2e 20 20 54 68 65 20  hild page.  The 
144b0 6e 65 77 50 67 6e 6f 20 61 72 67 75 6d 65 6e 74  newPgno argument
144b1 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 67 65 20   is the.** page 
144b2 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 63 68  number of the ch
144b3 69 6c 64 20 70 61 67 65 20 74 6f 20 6d 6f 76 65  ild page to move
144b4 20 74 6f 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69   to..*/.static i
144b5 6e 74 20 6d 6f 76 65 54 6f 43 68 69 6c 64 28 42  nt moveToChild(B
144b6 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 75  tCursor *pCur, u
144b7 33 32 20 6e 65 77 50 67 6e 6f 29 7b 0a 20 20 69  32 newPgno){.  i
144b8 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 20 3d  nt rc;.  int i =
144b9 20 70 43 75 72 2d 3e 69 50 61 67 65 3b 0a 20 20   pCur->iPage;.  
144ba 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 50 61 67  MemPage *pNewPag
144bb 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70  e;.  BtShared *p
144bc 42 74 20 3d 20 70 43 75 72 2d 3e 70 42 74 3b 0a  Bt = pCur->pBt;.
144bd 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73 6f  .  assert( curso
144be 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72  rHoldsMutex(pCur
144bf 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  ) );.  assert( p
144c0 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43 55 52  Cur->eState==CUR
144c1 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a 20 20 61  SOR_VALID );.  a
144c2 73 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61  ssert( pCur->iPa
144c3 67 65 3c 42 54 43 55 52 53 4f 52 5f 4d 41 58 5f  ge<BTCURSOR_MAX_
144c4 44 45 50 54 48 20 29 3b 0a 20 20 69 66 28 20 70  DEPTH );.  if( p
144c5 43 75 72 2d 3e 69 50 61 67 65 3e 3d 28 42 54 43  Cur->iPage>=(BTC
144c6 55 52 53 4f 52 5f 4d 41 58 5f 44 45 50 54 48 2d  URSOR_MAX_DEPTH-
144c7 31 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  1) ){.    return
144c8 20 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f   SQLITE_CORRUPT_
144c9 42 4b 50 54 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  BKPT;.  }.  rc =
144ca 20 67 65 74 41 6e 64 49 6e 69 74 50 61 67 65 28   getAndInitPage(
144cb 70 42 74 2c 20 6e 65 77 50 67 6e 6f 2c 20 26 70  pBt, newPgno, &p
144cc 4e 65 77 50 61 67 65 29 3b 0a 20 20 69 66 28 20  NewPage);.  if( 
144cd 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
144ce 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 69    pCur->apPage[i
144cf 2b 31 5d 20 3d 20 70 4e 65 77 50 61 67 65 3b 0a  +1] = pNewPage;.
144d0 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 69 2b    pCur->aiIdx[i+
144d1 31 5d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e  1] = 0;.  pCur->
144d2 69 50 61 67 65 2b 2b 3b 0a 0a 20 20 70 43 75 72  iPage++;..  pCur
144d3 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30  ->info.nSize = 0
144d4 3b 0a 20 20 70 43 75 72 2d 3e 76 61 6c 69 64 4e  ;.  pCur->validN
144d5 4b 65 79 20 3d 20 30 3b 0a 20 20 69 66 28 20 70  Key = 0;.  if( p
144d6 4e 65 77 50 61 67 65 2d 3e 6e 43 65 6c 6c 3c 31  NewPage->nCell<1
144d7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
144d8 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b  QLITE_CORRUPT_BK
144d9 50 54 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  PT;.  }.  return
144da 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23   SQLITE_OK;.}..#
144db 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
144dc 0a 2a 2a 20 50 61 67 65 20 70 50 61 72 65 6e 74  .** Page pParent
144dd 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20   is an internal 
144de 28 6e 6f 6e 2d 6c 65 61 66 29 20 74 72 65 65 20  (non-leaf) tree 
144df 70 61 67 65 2e 20 54 68 69 73 20 66 75 6e 63 74  page. This funct
144e0 69 6f 6e 20 0a 2a 2a 20 61 73 73 65 72 74 73 20  ion .** asserts 
144e1 74 68 61 74 20 70 61 67 65 20 6e 75 6d 62 65 72  that page number
144e2 20 69 43 68 69 6c 64 20 69 73 20 74 68 65 20 6c   iChild is the l
144e3 65 66 74 2d 63 68 69 6c 64 20 69 66 20 74 68 65  eft-child if the
144e4 20 69 49 64 78 27 74 68 0a 2a 2a 20 63 65 6c 6c   iIdx'th.** cell
144e5 20 69 6e 20 70 61 67 65 20 70 50 61 72 65 6e 74   in page pParent
144e6 2e 20 4f 72 2c 20 69 66 20 69 49 64 78 20 69 73  . Or, if iIdx is
144e7 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74 6f   equal to the to
144e8 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 0a 2a 2a  tal number of.**
144e9 20 63 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e   cells in pParen
144ea 74 2c 20 74 68 61 74 20 70 61 67 65 20 6e 75 6d  t, that page num
144eb 62 65 72 20 69 43 68 69 6c 64 20 69 73 20 74 68  ber iChild is th
144ec 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f 66  e right-child of
144ed 0a 2a 2a 20 74 68 65 20 70 61 67 65 2e 0a 2a 2f  .** the page..*/
144ee 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 73 73  .static void ass
144ef 65 72 74 50 61 72 65 6e 74 49 6e 64 65 78 28 4d  ertParentIndex(M
144f0 65 6d 50 61 67 65 20 2a 70 50 61 72 65 6e 74 2c  emPage *pParent,
144f1 20 69 6e 74 20 69 49 64 78 2c 20 50 67 6e 6f 20   int iIdx, Pgno 
144f2 69 43 68 69 6c 64 29 7b 0a 20 20 61 73 73 65 72  iChild){.  asser
144f3 74 28 20 69 49 64 78 3c 3d 70 50 61 72 65 6e 74  t( iIdx<=pParent
144f4 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 69 66 28  ->nCell );.  if(
144f5 20 69 49 64 78 3d 3d 70 50 61 72 65 6e 74 2d 3e   iIdx==pParent->
144f6 6e 43 65 6c 6c 20 29 7b 0a 20 20 20 20 61 73 73  nCell ){.    ass
144f7 65 72 74 28 20 67 65 74 34 62 79 74 65 28 26 70  ert( get4byte(&p
144f8 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50  Parent->aData[pP
144f9 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74  arent->hdrOffset
144fa 2b 38 5d 29 3d 3d 69 43 68 69 6c 64 20 29 3b 0a  +8])==iChild );.
144fb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
144fc 65 72 74 28 20 67 65 74 34 62 79 74 65 28 66 69  ert( get4byte(fi
144fd 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20  ndCell(pParent, 
144fe 69 49 64 78 29 29 3d 3d 69 43 68 69 6c 64 20 29  iIdx))==iChild )
144ff 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 23 20  ;.  }.}.#else.# 
14500 20 64 65 66 69 6e 65 20 61 73 73 65 72 74 50 61   define assertPa
14501 72 65 6e 74 49 6e 64 65 78 28 78 2c 79 2c 7a 29  rentIndex(x,y,z)
14502 20 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20   .#endif../*.** 
14503 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20  Move the cursor 
14504 75 70 20 74 6f 20 74 68 65 20 70 61 72 65 6e 74  up to the parent
14505 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 70 43 75   page..**.** pCu
14506 72 2d 3e 69 64 78 20 69 73 20 73 65 74 20 74 6f  r->idx is set to
14507 20 74 68 65 20 63 65 6c 6c 20 69 6e 64 65 78 20   the cell index 
14508 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
14509 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74 6f 20  e pointer.** to 
1450a 74 68 65 20 70 61 67 65 20 77 65 20 61 72 65 20  the page we are 
1450b 63 6f 6d 69 6e 67 20 66 72 6f 6d 2e 20 20 49 66  coming from.  If
1450c 20 77 65 20 61 72 65 20 63 6f 6d 69 6e 67 20 66   we are coming f
1450d 72 6f 6d 20 74 68 65 0a 2a 2a 20 72 69 67 68 74  rom the.** right
1450e 2d 6d 6f 73 74 20 63 68 69 6c 64 20 70 61 67 65  -most child page
1450f 20 74 68 65 6e 20 70 43 75 72 2d 3e 69 64 78 20   then pCur->idx 
14510 69 73 20 73 65 74 20 74 6f 20 6f 6e 65 20 6d 6f  is set to one mo
14511 72 65 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 6c  re than.** the l
14512 61 72 67 65 73 74 20 63 65 6c 6c 20 69 6e 64 65  argest cell inde
14513 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  x..*/.SQLITE_PRI
14514 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
14515 33 42 74 72 65 65 4d 6f 76 65 54 6f 50 61 72 65  3BtreeMoveToPare
14516 6e 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75  nt(BtCursor *pCu
14517 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  r){.  assert( cu
14518 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
14519 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Cur) );.  assert
1451a 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
1451b 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29 3b 0a  CURSOR_VALID );.
1451c 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e    assert( pCur->
1451d 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73 73  iPage>0 );.  ass
1451e 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61 67  ert( pCur->apPag
1451f 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 29  e[pCur->iPage] )
14520 3b 0a 20 20 61 73 73 65 72 74 50 61 72 65 6e 74  ;.  assertParent
14521 49 6e 64 65 78 28 0a 20 20 20 20 70 43 75 72 2d  Index(.    pCur-
14522 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50  >apPage[pCur->iP
14523 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43 75  age-1], .    pCu
14524 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69  r->aiIdx[pCur->i
14525 50 61 67 65 2d 31 5d 2c 20 0a 20 20 20 20 70 43  Page-1], .    pC
14526 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d  ur->apPage[pCur-
14527 3e 69 50 61 67 65 5d 2d 3e 70 67 6e 6f 0a 20 20  >iPage]->pgno.  
14528 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  );.  releasePage
14529 28 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43  (pCur->apPage[pC
1452a 75 72 2d 3e 69 50 61 67 65 5d 29 3b 0a 20 20 70  ur->iPage]);.  p
1452b 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20 20  Cur->iPage--;.  
1452c 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1452d 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61   = 0;.  pCur->va
1452e 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 7d 0a 0a  lidNKey = 0;.}..
1452f 2f 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63  /*.** Move the c
14530 75 72 73 6f 72 20 74 6f 20 74 68 65 20 72 6f 6f  ursor to the roo
14531 74 20 70 61 67 65 0a 2a 2f 0a 73 74 61 74 69 63  t page.*/.static
14532 20 69 6e 74 20 6d 6f 76 65 54 6f 52 6f 6f 74 28   int moveToRoot(
14533 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b  BtCursor *pCur){
14534 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 52 6f 6f  .  MemPage *pRoo
14535 74 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  t;.  int rc = SQ
14536 4c 49 54 45 5f 4f 4b 3b 0a 20 20 42 74 72 65 65  LITE_OK;.  Btree
14537 20 2a 70 20 3d 20 70 43 75 72 2d 3e 70 42 74 72   *p = pCur->pBtr
14538 65 65 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  ee;.  BtShared *
14539 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20  pBt = p->pBt;.. 
1453a 20 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48   assert( cursorH
1453b 6f 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20  oldsMutex(pCur) 
1453c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55 52  );.  assert( CUR
1453d 53 4f 52 5f 49 4e 56 41 4c 49 44 20 3c 20 43 55  SOR_INVALID < CU
1453e 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
1453f 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43 55   );.  assert( CU
14540 52 53 4f 52 5f 56 41 4c 49 44 20 20 20 3c 20 43  RSOR_VALID   < C
14541 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45  URSOR_REQUIRESEE
14542 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 43  K );.  assert( C
14543 55 52 53 4f 52 5f 46 41 55 4c 54 20 20 20 3e 20  URSOR_FAULT   > 
14544 43 55 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45  CURSOR_REQUIRESE
14545 45 4b 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72  EK );.  if( pCur
14546 2d 3e 65 53 74 61 74 65 3e 3d 43 55 52 53 4f 52  ->eState>=CURSOR
14547 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 7b 0a  _REQUIRESEEK ){.
14548 20 20 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53      if( pCur->eS
14549 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55  tate==CURSOR_FAU
1454a 4c 54 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  LT ){.      retu
1454b 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20  rn pCur->skip;. 
1454c 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
1454d 42 74 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72  BtreeClearCursor
1454e 28 70 43 75 72 29 3b 0a 20 20 7d 0a 0a 20 20 69  (pCur);.  }..  i
1454f 66 28 20 70 43 75 72 2d 3e 69 50 61 67 65 3e 3d  f( pCur->iPage>=
14550 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  0 ){.    int i;.
14551 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d      for(i=1; i<=
14552 70 43 75 72 2d 3e 69 50 61 67 65 3b 20 69 2b 2b  pCur->iPage; i++
14553 29 7b 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65  ){.      release
14554 50 61 67 65 28 70 43 75 72 2d 3e 61 70 50 61 67  Page(pCur->apPag
14555 65 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  e[i]);.    }.  }
14556 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 0a 20  else{.    if( . 
14557 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d       SQLITE_OK!=
14558 28 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74  (rc = getAndInit
14559 50 61 67 65 28 70 42 74 2c 20 70 43 75 72 2d 3e  Page(pBt, pCur->
1455a 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 43 75 72 2d  pgnoRoot, &pCur-
1455b 3e 61 70 50 61 67 65 5b 30 5d 29 29 0a 20 20 20  >apPage[0])).   
1455c 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d 3e   ){.      pCur->
1455d 65 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f  eState = CURSOR_
1455e 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 72  INVALID;.      r
1455f 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
14560 20 20 7d 0a 0a 20 20 70 52 6f 6f 74 20 3d 20 70    }..  pRoot = p
14561 43 75 72 2d 3e 61 70 50 61 67 65 5b 30 5d 3b 0a  Cur->apPage[0];.
14562 20 20 61 73 73 65 72 74 28 20 70 52 6f 6f 74 2d    assert( pRoot-
14563 3e 70 67 6e 6f 3d 3d 70 43 75 72 2d 3e 70 67 6e  >pgno==pCur->pgn
14564 6f 52 6f 6f 74 20 29 3b 0a 20 20 70 43 75 72 2d  oRoot );.  pCur-
14565 3e 69 50 61 67 65 20 3d 20 30 3b 0a 20 20 70 43  >iPage = 0;.  pC
14566 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20 3d 20 30  ur->aiIdx[0] = 0
14567 3b 0a 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e  ;.  pCur->info.n
14568 53 69 7a 65 20 3d 20 30 3b 0a 20 20 70 43 75 72  Size = 0;.  pCur
14569 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20  ->atLast = 0;.  
1456a 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1456b 3d 20 30 3b 0a 0a 20 20 69 66 28 20 70 52 6f 6f  = 0;..  if( pRoo
1456c 74 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 26 26 20 21  t->nCell==0 && !
1456d 70 52 6f 6f 74 2d 3e 6c 65 61 66 20 29 7b 0a 20  pRoot->leaf ){. 
1456e 20 20 20 50 67 6e 6f 20 73 75 62 70 61 67 65 3b     Pgno subpage;
1456f 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52 6f  .    assert( pRo
14570 6f 74 2d 3e 70 67 6e 6f 3d 3d 31 20 29 3b 0a 20  ot->pgno==1 );. 
14571 20 20 20 73 75 62 70 61 67 65 20 3d 20 67 65 74     subpage = get
14572 34 62 79 74 65 28 26 70 52 6f 6f 74 2d 3e 61 44  4byte(&pRoot->aD
14573 61 74 61 5b 70 52 6f 6f 74 2d 3e 68 64 72 4f 66  ata[pRoot->hdrOf
14574 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 61 73  fset+8]);.    as
14575 73 65 72 74 28 20 73 75 62 70 61 67 65 3e 30 20  sert( subpage>0 
14576 29 3b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74  );.    pCur->eSt
14577 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 56 41 4c  ate = CURSOR_VAL
14578 49 44 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76  ID;.    rc = mov
14579 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 73  eToChild(pCur, s
1457a 75 62 70 61 67 65 29 3b 0a 20 20 7d 65 6c 73 65  ubpage);.  }else
1457b 7b 0a 20 20 20 20 70 43 75 72 2d 3e 65 53 74 61  {.    pCur->eSta
1457c 74 65 20 3d 20 28 28 70 52 6f 6f 74 2d 3e 6e 43  te = ((pRoot->nC
1457d 65 6c 6c 3e 30 29 3f 43 55 52 53 4f 52 5f 56 41  ell>0)?CURSOR_VA
1457e 4c 49 44 3a 43 55 52 53 4f 52 5f 49 4e 56 41 4c  LID:CURSOR_INVAL
1457f 49 44 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ID);.  }.  retur
14580 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  n rc;.}../*.** M
14581 6f 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64  ove the cursor d
14582 6f 77 6e 20 74 6f 20 74 68 65 20 6c 65 66 74 2d  own to the left-
14583 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20  most leaf entry 
14584 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 65  beneath the.** e
14585 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 69 74  ntry to which it
14586 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
14587 69 6e 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68  inting..**.** Th
14588 65 20 6c 65 66 74 2d 6d 6f 73 74 20 6c 65 61 66  e left-most leaf
14589 20 69 73 20 74 68 65 20 6f 6e 65 20 77 69 74 68   is the one with
1458a 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 6b 65   the smallest ke
1458b 79 20 2d 20 74 68 65 20 66 69 72 73 74 0a 2a 2a  y - the first.**
1458c 20 69 6e 20 61 73 63 65 6e 64 69 6e 67 20 6f 72   in ascending or
1458d 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  der..*/.static i
1458e 6e 74 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73  nt moveToLeftmos
1458f 74 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  t(BtCursor *pCur
14590 29 7b 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a  ){.  Pgno pgno;.
14591 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
14592 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20  E_OK;.  MemPage 
14593 2a 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72  *pPage;..  asser
14594 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75  t( cursorHoldsMu
14595 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 61  tex(pCur) );.  a
14596 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
14597 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
14598 44 20 29 3b 0a 20 20 77 68 69 6c 65 28 20 72 63  D );.  while( rc
14599 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21  ==SQLITE_OK && !
1459a 28 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61  (pPage = pCur->a
1459b 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
1459c 65 5d 29 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  e])->leaf ){.   
1459d 20 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61   assert( pCur->a
1459e 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1459f 5d 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  ]<pPage->nCell )
145a0 3b 0a 20 20 20 20 70 67 6e 6f 20 3d 20 67 65 74  ;.    pgno = get
145a1 34 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70  4byte(findCell(p
145a2 50 61 67 65 2c 20 70 43 75 72 2d 3e 61 69 49 64  Page, pCur->aiId
145a3 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29 29  x[pCur->iPage]))
145a4 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54  ;.    rc = moveT
145a5 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 70 67 6e  oChild(pCur, pgn
145a6 6f 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  o);.  }.  return
145a7 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 6f   rc;.}../*.** Mo
145a8 76 65 20 74 68 65 20 63 75 72 73 6f 72 20 64 6f  ve the cursor do
145a9 77 6e 20 74 6f 20 74 68 65 20 72 69 67 68 74 2d  wn to the right-
145aa 6d 6f 73 74 20 6c 65 61 66 20 65 6e 74 72 79 20  most leaf entry 
145ab 62 65 6e 65 61 74 68 20 74 68 65 0a 2a 2a 20 70  beneath the.** p
145ac 61 67 65 20 74 6f 20 77 68 69 63 68 20 69 74 20  age to which it 
145ad 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69  is currently poi
145ae 6e 74 69 6e 67 2e 20 20 4e 6f 74 69 63 65 20 74  nting.  Notice t
145af 68 65 20 64 69 66 66 65 72 65 6e 63 65 0a 2a 2a  he difference.**
145b0 20 62 65 74 77 65 65 6e 20 6d 6f 76 65 54 6f 4c   between moveToL
145b1 65 66 74 6d 6f 73 74 28 29 20 61 6e 64 20 6d 6f  eftmost() and mo
145b2 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 2e  veToRightmost().
145b3 20 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74    moveToLeftmost
145b4 28 29 0a 2a 2a 20 66 69 6e 64 73 20 74 68 65 20  ().** finds the 
145b5 6c 65 66 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20  left-most entry 
145b6 62 65 6e 65 61 74 68 20 74 68 65 20 2a 65 6e 74  beneath the *ent
145b7 72 79 2a 20 77 68 65 72 65 61 73 20 6d 6f 76 65  ry* whereas move
145b8 54 6f 52 69 67 68 74 6d 6f 73 74 28 29 0a 2a 2a  ToRightmost().**
145b9 20 66 69 6e 64 73 20 74 68 65 20 72 69 67 68 74   finds the right
145ba 2d 6d 6f 73 74 20 65 6e 74 72 79 20 62 65 6e 65  -most entry bene
145bb 61 74 68 20 74 68 65 20 2a 70 61 67 65 2a 2e 0a  ath the *page*..
145bc 2a 2a 0a 2a 2a 20 54 68 65 20 72 69 67 68 74 2d  **.** The right-
145bd 6d 6f 73 74 20 65 6e 74 72 79 20 69 73 20 74 68  most entry is th
145be 65 20 6f 6e 65 20 77 69 74 68 20 74 68 65 20 6c  e one with the l
145bf 61 72 67 65 73 74 20 6b 65 79 20 2d 20 74 68 65  argest key - the
145c0 20 6c 61 73 74 0a 2a 2a 20 6b 65 79 20 69 6e 20   last.** key in 
145c1 61 73 63 65 6e 64 69 6e 67 20 6f 72 64 65 72 2e  ascending order.
145c2 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6d  .*/.static int m
145c3 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74 28 42  oveToRightmost(B
145c4 74 43 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a  tCursor *pCur){.
145c5 20 20 50 67 6e 6f 20 70 67 6e 6f 3b 0a 20 20 69    Pgno pgno;.  i
145c6 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
145c7 4b 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  K;.  MemPage *pP
145c8 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  age;..  assert( 
145c9 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78  cursorHoldsMutex
145ca 28 70 43 75 72 29 20 29 3b 0a 20 20 61 73 73 65  (pCur) );.  asse
145cb 72 74 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65  rt( pCur->eState
145cc 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49 44 20 29  ==CURSOR_VALID )
145cd 3b 0a 20 20 77 68 69 6c 65 28 20 72 63 3d 3d 53  ;.  while( rc==S
145ce 51 4c 49 54 45 5f 4f 4b 20 26 26 20 21 28 70 50  QLITE_OK && !(pP
145cf 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61  age = pCur->apPa
145d0 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 29  ge[pCur->iPage])
145d1 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67  ->leaf ){.    pg
145d2 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28 26 70  no = get4byte(&p
145d3 50 61 67 65 2d 3e 61 44 61 74 61 5b 70 50 61 67  Page->aData[pPag
145d4 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  e->hdrOffset+8])
145d5 3b 0a 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64  ;.    pCur->aiId
145d6 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d  x[pCur->iPage] =
145d7 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3b 0a 20   pPage->nCell;. 
145d8 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 43 68     rc = moveToCh
145d9 69 6c 64 28 70 43 75 72 2c 20 70 67 6e 6f 29 3b  ild(pCur, pgno);
145da 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
145db 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
145dc 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72  pCur->aiIdx[pCur
145dd 2d 3e 69 50 61 67 65 5d 20 3d 20 70 50 61 67 65  ->iPage] = pPage
145de 2d 3e 6e 43 65 6c 6c 2d 31 3b 0a 20 20 20 20 70  ->nCell-1;.    p
145df 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20  Cur->info.nSize 
145e0 3d 20 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76  = 0;.    pCur->v
145e1 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20  alidNKey = 0;.  
145e2 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
145e3 0a 0a 2f 2a 20 4d 6f 76 65 20 74 68 65 20 63 75  ../* Move the cu
145e4 72 73 6f 72 20 74 6f 20 74 68 65 20 66 69 72 73  rsor to the firs
145e5 74 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 74  t entry in the t
145e6 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 53 51  able.  Return SQ
145e7 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 6e 20 73 75  LITE_OK.** on su
145e8 63 63 65 73 73 2e 20 20 53 65 74 20 2a 70 52 65  ccess.  Set *pRe
145e9 73 20 74 6f 20 30 20 69 66 20 74 68 65 20 63 75  s to 0 if the cu
145ea 72 73 6f 72 20 61 63 74 75 61 6c 6c 79 20 70 6f  rsor actually po
145eb 69 6e 74 73 20 74 6f 20 73 6f 6d 65 74 68 69 6e  ints to somethin
145ec 67 0a 2a 2a 20 6f 72 20 73 65 74 20 2a 70 52 65  g.** or set *pRe
145ed 73 20 74 6f 20 31 20 69 66 20 74 68 65 20 74 61  s to 1 if the ta
145ee 62 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 2a 2f  ble is empty..*/
145ef 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
145f0 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
145f1 46 69 72 73 74 28 42 74 43 75 72 73 6f 72 20 2a  First(BtCursor *
145f2 70 43 75 72 2c 20 69 6e 74 20 2a 70 52 65 73 29  pCur, int *pRes)
145f3 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  {.  int rc;..  a
145f4 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
145f5 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
145f6 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
145f7 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
145f8 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  ur->pBtree->db->
145f9 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
145fa 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70 43 75 72   moveToRoot(pCur
145fb 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
145fc 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66  ITE_OK ){.    if
145fd 28 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d  ( pCur->eState==
145fe 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29  CURSOR_INVALID )
145ff 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
14600 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75  pCur->apPage[pCu
14601 72 2d 3e 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c  r->iPage]->nCell
14602 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 2a 70 52  ==0 );.      *pR
14603 65 73 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 63  es = 1;.      rc
14604 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
14605 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
14606 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
14607 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
14608 2d 3e 6e 43 65 6c 6c 3e 30 20 29 3b 0a 20 20 20  ->nCell>0 );.   
14609 20 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20     *pRes = 0;.  
1460a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 4c      rc = moveToL
1460b 65 66 74 6d 6f 73 74 28 70 43 75 72 29 3b 0a 20  eftmost(pCur);. 
1460c 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1460d 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4d 6f 76 65  n rc;.}../* Move
1460e 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74   the cursor to t
1460f 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
14610 20 74 68 65 20 74 61 62 6c 65 2e 20 20 52 65 74   the table.  Ret
14611 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 0a 2a 2a  urn SQLITE_OK.**
14612 20 6f 6e 20 73 75 63 63 65 73 73 2e 20 20 53 65   on success.  Se
14613 74 20 2a 70 52 65 73 20 74 6f 20 30 20 69 66 20  t *pRes to 0 if 
14614 74 68 65 20 63 75 72 73 6f 72 20 61 63 74 75 61  the cursor actua
14615 6c 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 73 6f  lly points to so
14616 6d 65 74 68 69 6e 67 0a 2a 2a 20 6f 72 20 73 65  mething.** or se
14617 74 20 2a 70 52 65 73 20 74 6f 20 31 20 69 66 20  t *pRes to 1 if 
14618 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70  the table is emp
14619 74 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ty..*/.SQLITE_PR
1461a 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1461b 33 42 74 72 65 65 4c 61 73 74 28 42 74 43 75 72  3BtreeLast(BtCur
1461c 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a  sor *pCur, int *
1461d 70 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pRes){.  int rc;
1461e 0a 20 0a 20 20 61 73 73 65 72 74 28 20 63 75 72  . .  assert( cur
1461f 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43  sorHoldsMutex(pC
14620 75 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ur) );.  assert(
14621 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
14622 65 6c 64 28 70 43 75 72 2d 3e 70 42 74 72 65 65  eld(pCur->pBtree
14623 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  ->db->mutex) );.
14624 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f    rc = moveToRoo
14625 74 28 70 43 75 72 29 3b 0a 20 20 69 66 28 20 72  t(pCur);.  if( r
14626 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
14627 20 20 20 20 69 66 28 20 43 55 52 53 4f 52 5f 49      if( CURSOR_I
14628 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d 3e 65 53  NVALID==pCur->eS
14629 74 61 74 65 20 29 7b 0a 20 20 20 20 20 20 61 73  tate ){.      as
1462a 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61  sert( pCur->apPa
1462b 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
1462c 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  >nCell==0 );.   
1462d 20 20 20 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20     *pRes = 1;.  
1462e 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61    }else{.      a
1462f 73 73 65 72 74 28 20 70 43 75 72 2d 3e 65 53 74  ssert( pCur->eSt
14630 61 74 65 3d 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate==CURSOR_VALI
14631 44 20 29 3b 0a 20 20 20 20 20 20 2a 70 52 65 73  D );.      *pRes
14632 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
14633 20 6d 6f 76 65 54 6f 52 69 67 68 74 6d 6f 73 74   moveToRightmost
14634 28 70 43 75 72 29 3b 0a 20 20 20 20 20 20 67 65  (pCur);.      ge
14635 74 43 65 6c 6c 49 6e 66 6f 28 70 43 75 72 29 3b  tCellInfo(pCur);
14636 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 74 4c  .      pCur->atL
14637 61 73 74 20 3d 20 72 63 3d 3d 53 51 4c 49 54 45  ast = rc==SQLITE
14638 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  _OK;.    }.  }. 
14639 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1463a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73 6f  * Move the curso
1463b 72 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f 69  r so that it poi
1463c 6e 74 73 20 74 6f 20 61 6e 20 65 6e 74 72 79 20  nts to an entry 
1463d 6e 65 61 72 20 74 68 65 20 6b 65 79 20 0a 2a 2a  near the key .**
1463e 20 73 70 65 63 69 66 69 65 64 20 62 79 20 70 49   specified by pI
1463f 64 78 4b 65 79 20 6f 72 20 69 6e 74 4b 65 79 2e  dxKey or intKey.
14640 20 20 20 52 65 74 75 72 6e 20 61 20 73 75 63 63     Return a succ
14641 65 73 73 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20  ess code..**.** 
14642 46 6f 72 20 49 4e 54 4b 45 59 20 74 61 62 6c 65  For INTKEY table
14643 73 2c 20 74 68 65 20 69 6e 74 4b 65 79 20 70 61  s, the intKey pa
14644 72 61 6d 65 74 65 72 20 69 73 20 75 73 65 64 2e  rameter is used.
14645 20 20 70 49 64 78 4b 65 79 20 0a 2a 2a 20 6d 75    pIdxKey .** mu
14646 73 74 20 62 65 20 4e 55 4c 4c 2e 20 20 46 6f 72  st be NULL.  For
14647 20 69 6e 64 65 78 20 74 61 62 6c 65 73 2c 20 70   index tables, p
14648 49 64 78 4b 65 79 20 69 73 20 75 73 65 64 20 61  IdxKey is used a
14649 6e 64 20 69 6e 74 4b 65 79 0a 2a 2a 20 69 73 20  nd intKey.** is 
1464a 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  ignored..**.** I
1464b 66 20 61 6e 20 65 78 61 63 74 20 6d 61 74 63 68  f an exact match
1464c 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 20 74   is not found, t
1464d 68 65 6e 20 74 68 65 20 63 75 72 73 6f 72 20 69  hen the cursor i
1464e 73 20 61 6c 77 61 79 73 0a 2a 2a 20 6c 65 66 74  s always.** left
1464f 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 6c   pointing at a l
14650 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20 77  eaf page which w
14651 6f 75 6c 64 20 68 6f 6c 64 20 74 68 65 20 65 6e  ould hold the en
14652 74 72 79 20 69 66 20 69 74 0a 2a 2a 20 77 65 72  try if it.** wer
14653 65 20 70 72 65 73 65 6e 74 2e 20 20 54 68 65 20  e present.  The 
14654 63 75 72 73 6f 72 20 6d 69 67 68 74 20 70 6f 69  cursor might poi
14655 6e 74 20 74 6f 20 61 6e 20 65 6e 74 72 79 20 74  nt to an entry t
14656 68 61 74 20 63 6f 6d 65 73 0a 2a 2a 20 62 65 66  hat comes.** bef
14657 6f 72 65 20 6f 72 20 61 66 74 65 72 20 74 68 65  ore or after the
14658 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20   key..**.** The 
14659 72 65 73 75 6c 74 20 6f 66 20 63 6f 6d 70 61 72  result of compar
1465a 69 6e 67 20 74 68 65 20 6b 65 79 20 77 69 74 68  ing the key with
1465b 20 74 68 65 20 65 6e 74 72 79 20 74 6f 20 77 68   the entry to wh
1465c 69 63 68 20 74 68 65 0a 2a 2a 20 63 75 72 73 6f  ich the.** curso
1465d 72 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f 20  r is written to 
1465e 2a 70 52 65 73 20 69 66 20 70 52 65 73 21 3d 4e  *pRes if pRes!=N
1465f 55 4c 4c 2e 20 20 54 68 65 20 6d 65 61 6e 69 6e  ULL.  The meanin
14660 67 20 6f 66 0a 2a 2a 20 74 68 69 73 20 76 61 6c  g of.** this val
14661 75 65 20 69 73 20 61 73 20 66 6f 6c 6c 6f 77 73  ue is as follows
14662 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65  :.**.**     *pRe
14663 73 3c 30 20 20 20 20 20 20 54 68 65 20 63 75 72  s<0      The cur
14664 73 6f 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e  sor is left poin
14665 74 69 6e 67 20 61 74 20 61 6e 20 65 6e 74 72 79  ting at an entry
14666 20 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20   that.**        
14667 20 20 20 20 20 20 20 20 20 20 69 73 20 73 6d 61            is sma
14668 6c 6c 65 72 20 74 68 61 6e 20 70 4b 65 79 20 6f  ller than pKey o
14669 72 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 69  r if the table i
1466a 73 20 65 6d 70 74 79 0a 2a 2a 20 20 20 20 20 20  s empty.**      
1466b 20 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20              and 
1466c 74 68 65 20 63 75 72 73 6f 72 20 69 73 20 74 68  the cursor is th
1466d 65 72 65 66 6f 72 65 20 6c 65 66 74 20 70 6f 69  erefore left poi
1466e 6e 74 20 74 6f 20 6e 6f 74 68 69 6e 67 2e 0a 2a  nt to nothing..*
1466f 2a 0a 2a 2a 20 20 20 20 20 2a 70 52 65 73 3d 3d  *.**     *pRes==
14670 30 20 20 20 20 20 54 68 65 20 63 75 72 73 6f 72  0     The cursor
14671 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
14672 67 20 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68  g at an entry th
14673 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  at.**           
14674 20 20 20 20 20 20 20 65 78 61 63 74 6c 79 20 6d         exactly m
14675 61 74 63 68 65 73 20 70 4b 65 79 2e 0a 2a 2a 0a  atches pKey..**.
14676 2a 2a 20 20 20 20 20 2a 70 52 65 73 3e 30 20 20  **     *pRes>0  
14677 20 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 69      The cursor i
14678 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20  s left pointing 
14679 61 74 20 61 6e 20 65 6e 74 72 79 20 74 68 61 74  at an entry that
1467a 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1467b 20 20 20 20 20 69 73 20 6c 61 72 67 65 72 20 74       is larger t
1467c 68 61 6e 20 70 4b 65 79 2e 0a 2a 2a 0a 2a 2f 0a  han pKey..**.*/.
1467d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1467e 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d  nt sqlite3BtreeM
1467f 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 0a 20  ovetoUnpacked(. 
14680 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
14681 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
14682 20 63 75 72 73 6f 72 20 74 6f 20 62 65 20 6d 6f   cursor to be mo
14683 76 65 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65  ved */.  Unpacke
14684 64 52 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79  dRecord *pIdxKey
14685 2c 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 69 6e  , /* Unpacked in
14686 64 65 78 20 6b 65 79 20 2a 2f 0a 20 20 69 36 34  dex key */.  i64
14687 20 69 6e 74 4b 65 79 2c 20 20 20 20 20 20 20 20   intKey,        
14688 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62        /* The tab
14689 6c 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  le key */.  int 
1468a 62 69 61 73 52 69 67 68 74 2c 20 20 20 20 20 20  biasRight,      
1468b 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c       /* If true,
1468c 20 62 69 61 73 20 74 68 65 20 73 65 61 72 63 68   bias the search
1468d 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64   to the high end
1468e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20   */.  int *pRes 
1468f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
14690 2a 20 57 72 69 74 65 20 73 65 61 72 63 68 20 72  * Write search r
14691 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 29  esults here */.)
14692 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61  {.  int rc;..  a
14693 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c  ssert( cursorHol
14694 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b  dsMutex(pCur) );
14695 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
14696 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
14697 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e  ur->pBtree->db->
14698 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20  mutex) );..  /* 
14699 49 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  If the cursor is
1469a 20 61 6c 72 65 61 64 79 20 70 6f 73 69 74 69 6f   already positio
1469b 6e 65 64 20 61 74 20 74 68 65 20 70 6f 69 6e 74  ned at the point
1469c 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 0a 20   we are trying. 
1469d 20 2a 2a 20 74 6f 20 6d 6f 76 65 20 74 6f 2c 20   ** to move to, 
1469e 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72 6e  then just return
1469f 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 61   without doing a
146a0 6e 79 20 77 6f 72 6b 20 2a 2f 0a 20 20 69 66 28  ny work */.  if(
146a1 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
146a2 55 52 53 4f 52 5f 56 41 4c 49 44 20 26 26 20 70  URSOR_VALID && p
146a3 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 0a  Cur->validNKey .
146a4 20 20 20 26 26 20 70 43 75 72 2d 3e 61 70 50 61     && pCur->apPa
146a5 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20 0a 20  ge[0]->intKey . 
146a6 20 29 7b 0a 20 20 20 20 69 66 28 20 70 43 75 72   ){.    if( pCur
146a7 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 69 6e 74  ->info.nKey==int
146a8 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70 52  Key ){.      *pR
146a9 65 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65  es = 0;.      re
146aa 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
146ab 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 43      }.    if( pC
146ac 75 72 2d 3e 61 74 4c 61 73 74 20 26 26 20 70 43  ur->atLast && pC
146ad 75 72 2d 3e 69 6e 66 6f 2e 6e 4b 65 79 3c 69 6e  ur->info.nKey<in
146ae 74 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 2a 70  tKey ){.      *p
146af 52 65 73 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  Res = -1;.      
146b0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
146b1 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
146b2 63 20 3d 20 6d 6f 76 65 54 6f 52 6f 6f 74 28 70  c = moveToRoot(p
146b3 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29  Cur);.  if( rc )
146b4 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
146b5 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
146b6 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
146b7 2d 3e 69 50 61 67 65 5d 20 29 3b 0a 20 20 61 73  ->iPage] );.  as
146b8 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50 61  sert( pCur->apPa
146b9 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 2d  ge[pCur->iPage]-
146ba 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28  >isInit );.  if(
146bb 20 70 43 75 72 2d 3e 65 53 74 61 74 65 3d 3d 43   pCur->eState==C
146bc 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20 29 7b  URSOR_INVALID ){
146bd 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 2d 31 3b  .    *pRes = -1;
146be 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 75  .    assert( pCu
146bf 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
146c0 69 50 61 67 65 5d 2d 3e 6e 43 65 6c 6c 3d 3d 30  iPage]->nCell==0
146c1 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   );.    return S
146c2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
146c3 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70  assert( pCur->ap
146c4 50 61 67 65 5b 30 5d 2d 3e 69 6e 74 4b 65 79 20  Page[0]->intKey 
146c5 7c 7c 20 70 49 64 78 4b 65 79 20 29 3b 0a 20 20  || pIdxKey );.  
146c6 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 69 6e 74  for(;;){.    int
146c7 20 6c 77 72 2c 20 75 70 72 3b 0a 20 20 20 20 50   lwr, upr;.    P
146c8 67 6e 6f 20 63 68 6c 64 50 67 3b 0a 20 20 20 20  gno chldPg;.    
146c9 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d  MemPage *pPage =
146ca 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43   pCur->apPage[pC
146cb 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20 20  ur->iPage];.    
146cc 69 6e 74 20 63 20 3d 20 2d 31 3b 20 20 2f 2a 20  int c = -1;  /* 
146cd 70 52 65 73 20 72 65 74 75 72 6e 20 69 66 20 74  pRes return if t
146ce 61 62 6c 65 20 69 73 20 65 6d 70 74 79 20 6d 75  able is empty mu
146cf 73 74 20 62 65 20 2d 31 20 2a 2f 0a 20 20 20 20  st be -1 */.    
146d0 6c 77 72 20 3d 20 30 3b 0a 20 20 20 20 75 70 72  lwr = 0;.    upr
146d1 20 3d 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d   = pPage->nCell-
146d2 31 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 67  1;.    if( !pPag
146d3 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 70 49 64  e->intKey && pId
146d4 78 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20  xKey==0 ){.     
146d5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
146d6 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
146d7 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66 69 6e   goto moveto_fin
146d8 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ish;.    }.    i
146d9 66 28 20 62 69 61 73 52 69 67 68 74 20 29 7b 0a  f( biasRight ){.
146da 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49 64        pCur->aiId
146db 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20 3d  x[pCur->iPage] =
146dc 20 75 70 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   upr;.    }else{
146dd 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 61 69 49  .      pCur->aiI
146de 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 20  dx[pCur->iPage] 
146df 3d 20 28 75 70 72 2b 6c 77 72 29 2f 32 3b 0a 20  = (upr+lwr)/2;. 
146e0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6c 77 72     }.    if( lwr
146e1 3c 3d 75 70 72 20 29 20 66 6f 72 28 3b 3b 29 7b  <=upr ) for(;;){
146e2 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 43 65  .      void *pCe
146e3 6c 6c 4b 65 79 3b 0a 20 20 20 20 20 20 69 36 34  llKey;.      i64
146e4 20 6e 43 65 6c 6c 4b 65 79 3b 0a 20 20 20 20 20   nCellKey;.     
146e5 20 69 6e 74 20 69 64 78 20 3d 20 70 43 75 72 2d   int idx = pCur-
146e6 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
146e7 67 65 5d 3b 0a 20 20 20 20 20 20 70 43 75 72 2d  ge];.      pCur-
146e8 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20 30 3b  >info.nSize = 0;
146e9 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 76 61 6c  .      pCur->val
146ea 69 64 4e 4b 65 79 20 3d 20 31 3b 0a 20 20 20 20  idNKey = 1;.    
146eb 20 20 69 66 28 20 70 50 61 67 65 2d 3e 69 6e 74    if( pPage->int
146ec 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 75  Key ){.        u
146ed 38 20 2a 70 43 65 6c 6c 3b 0a 20 20 20 20 20 20  8 *pCell;.      
146ee 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65    pCell = findCe
146ef 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 29 20 2b  ll(pPage, idx) +
146f0 20 70 50 61 67 65 2d 3e 63 68 69 6c 64 50 74 72   pPage->childPtr
146f1 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Size;.        if
146f2 28 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61  ( pPage->hasData
146f3 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 75 33   ){.          u3
146f4 32 20 64 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20  2 dummy;.       
146f5 20 20 20 70 43 65 6c 6c 20 2b 3d 20 67 65 74 56     pCell += getV
146f6 61 72 69 6e 74 33 32 28 70 43 65 6c 6c 2c 20 64  arint32(pCell, d
146f7 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 7d  ummy);.        }
146f8 0a 20 20 20 20 20 20 20 20 67 65 74 56 61 72 69  .        getVari
146f9 6e 74 28 70 43 65 6c 6c 2c 20 28 75 36 34 2a 29  nt(pCell, (u64*)
146fa 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 20 20  &nCellKey);.    
146fb 20 20 20 20 69 66 28 20 6e 43 65 6c 6c 4b 65 79      if( nCellKey
146fc 3d 3d 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20  ==intKey ){.    
146fd 20 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20        c = 0;.   
146fe 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6e       }else if( n
146ff 43 65 6c 6c 4b 65 79 3c 69 6e 74 4b 65 79 20 29  CellKey<intKey )
14700 7b 0a 20 20 20 20 20 20 20 20 20 20 63 20 3d 20  {.          c = 
14701 2d 31 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  -1;.        }els
14702 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  e{.          ass
14703 65 72 74 28 20 6e 43 65 6c 6c 4b 65 79 3e 69 6e  ert( nCellKey>in
14704 74 4b 65 79 20 29 3b 0a 20 20 20 20 20 20 20 20  tKey );.        
14705 20 20 63 20 3d 20 2b 31 3b 0a 20 20 20 20 20 20    c = +1;.      
14706 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b    }.      }else{
14707 0a 20 20 20 20 20 20 20 20 69 6e 74 20 61 76 61  .        int ava
14708 69 6c 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20  ilable;.        
14709 70 43 65 6c 6c 4b 65 79 20 3d 20 28 76 6f 69 64  pCellKey = (void
1470a 20 2a 29 66 65 74 63 68 50 61 79 6c 6f 61 64 28   *)fetchPayload(
1470b 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62 6c 65  pCur, &available
1470c 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 43  , 0);.        nC
1470d 65 6c 6c 4b 65 79 20 3d 20 70 43 75 72 2d 3e 69  ellKey = pCur->i
1470e 6e 66 6f 2e 6e 4b 65 79 3b 0a 20 20 20 20 20 20  nfo.nKey;.      
1470f 20 20 69 66 28 20 61 76 61 69 6c 61 62 6c 65 3e    if( available>
14710 3d 6e 43 65 6c 6c 4b 65 79 20 29 7b 0a 20 20 20  =nCellKey ){.   
14711 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69 74         c = sqlit
14712 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
14713 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20 70 43  are(nCellKey, pC
14714 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 79 29  ellKey, pIdxKey)
14715 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
14716 0a 20 20 20 20 20 20 20 20 20 20 70 43 65 6c 6c  .          pCell
14717 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c  Key = sqlite3Mal
14718 6c 6f 63 28 20 6e 43 65 6c 6c 4b 65 79 20 29 3b  loc( nCellKey );
14719 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
1471a 43 65 6c 6c 4b 65 79 3d 3d 30 20 29 7b 0a 20 20  CellKey==0 ){.  
1471b 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53            rc = S
1471c 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
1471d 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 6f           goto mo
1471e 76 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20  veto_finish;.   
1471f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
14720 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
14721 74 72 65 65 4b 65 79 28 70 43 75 72 2c 20 30 2c  treeKey(pCur, 0,
14722 20 6e 43 65 6c 6c 4b 65 79 2c 20 28 76 6f 69 64   nCellKey, (void
14723 20 2a 29 70 43 65 6c 6c 4b 65 79 29 3b 0a 20 20   *)pCellKey);.  
14724 20 20 20 20 20 20 20 20 63 20 3d 20 73 71 6c 69          c = sqli
14725 74 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d  te3VdbeRecordCom
14726 70 61 72 65 28 6e 43 65 6c 6c 4b 65 79 2c 20 70  pare(nCellKey, p
14727 43 65 6c 6c 4b 65 79 2c 20 70 49 64 78 4b 65 79  CellKey, pIdxKey
14728 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
14729 69 74 65 33 5f 66 72 65 65 28 70 43 65 6c 6c 4b  ite3_free(pCellK
1472a 65 79 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ey);.          i
1472b 66 28 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76  f( rc ) goto mov
1472c 65 74 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 20 20  eto_finish;.    
1472d 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1472e 20 20 20 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a      if( c==0 ){.
1472f 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e          pCur->in
14730 66 6f 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b  fo.nKey = nCellK
14731 65 79 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ey;.        if( 
14732 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 26 26  pPage->intKey &&
14733 20 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b   !pPage->leaf ){
14734 0a 20 20 20 20 20 20 20 20 20 20 6c 77 72 20 3d  .          lwr =
14735 20 69 64 78 3b 0a 20 20 20 20 20 20 20 20 20 20   idx;.          
14736 75 70 72 20 3d 20 6c 77 72 20 2d 20 31 3b 0a 20  upr = lwr - 1;. 
14737 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
14738 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
14739 20 20 20 20 20 20 20 20 20 69 66 28 20 70 52 65           if( pRe
1473a 73 20 29 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20  s ) *pRes = 0;. 
1473b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 53 51           rc = SQ
1473c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20  LITE_OK;.       
1473d 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f 66     goto moveto_f
1473e 69 6e 69 73 68 3b 0a 20 20 20 20 20 20 20 20 7d  inish;.        }
1473f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
14740 66 28 20 63 3c 30 20 29 7b 0a 20 20 20 20 20 20  f( c<0 ){.      
14741 20 20 6c 77 72 20 3d 20 69 64 78 2b 31 3b 0a 20    lwr = idx+1;. 
14742 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
14743 20 20 20 20 75 70 72 20 3d 20 69 64 78 2d 31 3b      upr = idx-1;
14744 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
14745 66 28 20 6c 77 72 3e 75 70 72 20 29 7b 0a 20 20  f( lwr>upr ){.  
14746 20 20 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f        pCur->info
14747 2e 6e 4b 65 79 20 3d 20 6e 43 65 6c 6c 4b 65 79  .nKey = nCellKey
14748 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
14749 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1474a 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
1474b 3e 69 50 61 67 65 5d 20 3d 20 28 6c 77 72 2b 75  >iPage] = (lwr+u
1474c 70 72 29 2f 32 3b 0a 20 20 20 20 7d 0a 20 20 20  pr)/2;.    }.   
1474d 20 61 73 73 65 72 74 28 20 6c 77 72 3d 3d 75 70   assert( lwr==up
1474e 72 2b 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  r+1 );.    asser
1474f 74 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74  t( pPage->isInit
14750 20 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67   );.    if( pPag
14751 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20  e->leaf ){.     
14752 20 63 68 6c 64 50 67 20 3d 20 30 3b 0a 20 20 20   chldPg = 0;.   
14753 20 7d 65 6c 73 65 20 69 66 28 20 6c 77 72 3e 3d   }else if( lwr>=
14754 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  pPage->nCell ){.
14755 20 20 20 20 20 20 63 68 6c 64 50 67 20 3d 20 67        chldPg = g
14756 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e  et4byte(&pPage->
14757 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72  aData[pPage->hdr
14758 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20  Offset+8]);.    
14759 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 68 6c  }else{.      chl
1475a 64 50 67 20 3d 20 67 65 74 34 62 79 74 65 28 66  dPg = get4byte(f
1475b 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 6c  indCell(pPage, l
1475c 77 72 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  wr));.    }.    
1475d 69 66 28 20 63 68 6c 64 50 67 3d 3d 30 20 29 7b  if( chldPg==0 ){
1475e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1475f 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75 72 2d  Cur->aiIdx[pCur-
14760 3e 69 50 61 67 65 5d 3c 70 43 75 72 2d 3e 61 70  >iPage]<pCur->ap
14761 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
14762 5d 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20 20 20  ]->nCell );.    
14763 20 20 69 66 28 20 70 52 65 73 20 29 20 2a 70 52    if( pRes ) *pR
14764 65 73 20 3d 20 63 3b 0a 20 20 20 20 20 20 72 63  es = c;.      rc
14765 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
14766 20 20 20 20 67 6f 74 6f 20 6d 6f 76 65 74 6f 5f      goto moveto_
14767 66 69 6e 69 73 68 3b 0a 20 20 20 20 7d 0a 20 20  finish;.    }.  
14768 20 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43    pCur->aiIdx[pC
14769 75 72 2d 3e 69 50 61 67 65 5d 20 3d 20 6c 77 72  ur->iPage] = lwr
1476a 3b 0a 20 20 20 20 70 43 75 72 2d 3e 69 6e 66 6f  ;.    pCur->info
1476b 2e 6e 53 69 7a 65 20 3d 20 30 3b 0a 20 20 20 20  .nSize = 0;.    
1476c 70 43 75 72 2d 3e 76 61 6c 69 64 4e 4b 65 79 20  pCur->validNKey 
1476d 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 6d 6f  = 0;.    rc = mo
1476e 76 65 54 6f 43 68 69 6c 64 28 70 43 75 72 2c 20  veToChild(pCur, 
1476f 63 68 6c 64 50 67 29 3b 0a 20 20 20 20 69 66 28  chldPg);.    if(
14770 20 72 63 20 29 20 67 6f 74 6f 20 6d 6f 76 65 74   rc ) goto movet
14771 6f 5f 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 6d 6f  o_finish;.  }.mo
14772 76 65 74 6f 5f 66 69 6e 69 73 68 3a 0a 20 20 72  veto_finish:.  r
14773 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a  eturn rc;.}../*.
14774 2a 2a 20 49 6e 20 74 68 69 73 20 76 65 72 73 69  ** In this versi
14775 6f 6e 20 6f 66 20 42 74 72 65 65 4d 6f 76 65 74  on of BtreeMovet
14776 6f 2c 20 70 4b 65 79 20 69 73 20 61 20 70 61 63  o, pKey is a pac
14777 6b 65 64 20 69 6e 64 65 78 20 72 65 63 6f 72 64  ked index record
14778 0a 2a 2a 20 73 75 63 68 20 61 73 20 69 73 20 67  .** such as is g
14779 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65 20  enerated by the 
1477a 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70  OP_MakeRecord op
1477b 63 6f 64 65 2e 20 20 55 6e 70 61 63 6b 20 74 68  code.  Unpack th
1477c 65 0a 2a 2a 20 72 65 63 6f 72 64 20 61 6e 64 20  e.** record and 
1477d 74 68 65 6e 20 63 61 6c 6c 20 42 74 72 65 65 4d  then call BtreeM
1477e 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 29 20  ovetoUnpacked() 
1477f 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 2e 0a  to do the work..
14780 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
14781 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
14782 65 65 4d 6f 76 65 74 6f 28 0a 20 20 42 74 43 75  eeMoveto(.  BtCu
14783 72 73 6f 72 20 2a 70 43 75 72 2c 20 20 20 20 20  rsor *pCur,     
14784 2f 2a 20 43 75 72 73 6f 72 20 6f 70 65 6e 20 6f  /* Cursor open o
14785 6e 20 74 68 65 20 62 74 72 65 65 20 74 6f 20 62  n the btree to b
14786 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a 20 20  e searched */.  
14787 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
14788 2c 20 20 20 2f 2a 20 50 61 63 6b 65 64 20 6b 65  ,   /* Packed ke
14789 79 20 69 66 20 74 68 65 20 62 74 72 65 65 20 69  y if the btree i
1478a 73 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20  s an index */.  
1478b 69 36 34 20 6e 4b 65 79 2c 20 20 20 20 20 20 20  i64 nKey,       
1478c 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 6b      /* Integer k
1478d 65 79 20 66 6f 72 20 74 61 62 6c 65 73 2e 20 20  ey for tables.  
1478e 53 69 7a 65 20 6f 66 20 70 4b 65 79 20 66 6f 72  Size of pKey for
1478f 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e   indices */.  in
14790 74 20 62 69 61 73 2c 20 20 20 20 20 20 20 20 20  t bias,         
14791 20 20 2f 2a 20 42 69 61 73 20 73 65 61 72 63 68    /* Bias search
14792 20 74 6f 20 74 68 65 20 68 69 67 68 20 65 6e 64   to the high end
14793 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 73 20   */.  int *pRes 
14794 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
14795 74 65 20 73 65 61 72 63 68 20 72 65 73 75 6c 74  te search result
14796 73 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69  s here */.){.  i
14797 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 20  nt rc;          
14798 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61            /* Sta
14799 74 75 73 20 63 6f 64 65 20 2a 2f 0a 20 20 55 6e  tus code */.  Un
1479a 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 49  packedRecord *pI
1479b 64 78 4b 65 79 3b 20 20 20 2f 2a 20 55 6e 70 61  dxKey;   /* Unpa
1479c 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 20 2a  cked index key *
1479d 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f  /.  UnpackedReco
1479e 72 64 20 61 53 70 61 63 65 5b 31 36 5d 3b 20 2f  rd aSpace[16]; /
1479f 2a 20 54 65 6d 70 20 73 70 61 63 65 20 66 6f 72  * Temp space for
147a0 20 70 49 64 78 4b 65 79 20 2d 20 74 6f 20 61 76   pIdxKey - to av
147a1 6f 69 64 20 61 20 6d 61 6c 6c 6f 63 20 2a 2f 0a  oid a malloc */.
147a2 0a 20 20 69 66 28 20 70 4b 65 79 20 29 7b 0a 20  .  if( pKey ){. 
147a3 20 20 20 70 49 64 78 4b 65 79 20 3d 20 73 71 6c     pIdxKey = sql
147a4 69 74 65 33 56 64 62 65 52 65 63 6f 72 64 55 6e  ite3VdbeRecordUn
147a5 70 61 63 6b 28 70 43 75 72 2d 3e 70 4b 65 79 49  pack(pCur->pKeyI
147a6 6e 66 6f 2c 20 6e 4b 65 79 2c 20 70 4b 65 79 2c  nfo, nKey, pKey,
147a7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
147a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
147a9 20 20 20 20 20 20 20 61 53 70 61 63 65 2c 20 73         aSpace, s
147aa 69 7a 65 6f 66 28 61 53 70 61 63 65 29 29 3b 0a  izeof(aSpace));.
147ab 20 20 20 20 69 66 28 20 70 49 64 78 4b 65 79 3d      if( pIdxKey=
147ac 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49  =0 ) return SQLI
147ad 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
147ae 65 7b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d  e{.    pIdxKey =
147af 20 30 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73   0;.  }.  rc = s
147b0 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74  qlite3BtreeMovet
147b1 6f 55 6e 70 61 63 6b 65 64 28 70 43 75 72 2c 20  oUnpacked(pCur, 
147b2 70 49 64 78 4b 65 79 2c 20 6e 4b 65 79 2c 20 62  pIdxKey, nKey, b
147b3 69 61 73 2c 20 70 52 65 73 29 3b 0a 20 20 69 66  ias, pRes);.  if
147b4 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20 73 71  ( pKey ){.    sq
147b5 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55  lite3VdbeDeleteU
147b6 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49  npackedRecord(pI
147b7 64 78 4b 65 79 29 3b 0a 20 20 7d 0a 20 20 72 65  dxKey);.  }.  re
147b8 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a  turn rc;.}.../*.
147b9 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
147ba 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
147bb 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20  not pointing at 
147bc 61 6e 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20  an entry of the 
147bd 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 52 55  table..**.** TRU
147be 45 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  E will be return
147bf 65 64 20 61 66 74 65 72 20 61 20 63 61 6c 6c 20  ed after a call 
147c0 74 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e  to sqlite3BtreeN
147c1 65 78 74 28 29 20 6d 6f 76 65 73 0a 2a 2a 20 70  ext() moves.** p
147c2 61 73 74 20 74 68 65 20 6c 61 73 74 20 65 6e 74  ast the last ent
147c3 72 79 20 69 6e 20 74 68 65 20 74 61 62 6c 65 20  ry in the table 
147c4 6f 72 20 73 71 6c 69 74 65 33 42 74 72 65 65 50  or sqlite3BtreeP
147c5 72 65 76 28 29 20 6d 6f 76 65 73 20 70 61 73 74  rev() moves past
147c6 0a 2a 2a 20 74 68 65 20 66 69 72 73 74 20 65 6e  .** the first en
147c7 74 72 79 2e 20 20 54 52 55 45 20 69 73 20 61 6c  try.  TRUE is al
147c8 73 6f 20 72 65 74 75 72 6e 65 64 20 69 66 20 74  so returned if t
147c9 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d 70 74  he table is empt
147ca 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  y..*/.SQLITE_PRI
147cb 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
147cc 42 74 72 65 65 45 6f 66 28 42 74 43 75 72 73 6f  BtreeEof(BtCurso
147cd 72 20 2a 70 43 75 72 29 7b 0a 20 20 2f 2a 20 54  r *pCur){.  /* T
147ce 4f 44 4f 3a 20 57 68 61 74 20 69 66 20 74 68 65  ODO: What if the
147cf 20 63 75 72 73 6f 72 20 69 73 20 69 6e 20 43 55   cursor is in CU
147d0 52 53 4f 52 5f 52 45 51 55 49 52 45 53 45 45 4b  RSOR_REQUIRESEEK
147d1 20 62 75 74 20 61 6c 6c 20 74 61 62 6c 65 20 65   but all table e
147d2 6e 74 72 69 65 73 0a 20 20 2a 2a 20 68 61 76 65  ntries.  ** have
147d3 20 62 65 65 6e 20 64 65 6c 65 74 65 64 3f 20 54   been deleted? T
147d4 68 69 73 20 41 50 49 20 77 69 6c 6c 20 6e 65 65  his API will nee
147d5 64 20 74 6f 20 63 68 61 6e 67 65 20 74 6f 20 72  d to change to r
147d6 65 74 75 72 6e 20 61 6e 20 65 72 72 6f 72 20 63  eturn an error c
147d7 6f 64 65 0a 20 20 2a 2a 20 61 73 20 77 65 6c 6c  ode.  ** as well
147d8 20 61 73 20 74 68 65 20 62 6f 6f 6c 65 61 6e 20   as the boolean 
147d9 72 65 73 75 6c 74 20 76 61 6c 75 65 2e 0a 20 20  result value..  
147da 2a 2f 0a 20 20 72 65 74 75 72 6e 20 28 43 55 52  */.  return (CUR
147db 53 4f 52 5f 56 41 4c 49 44 21 3d 70 43 75 72 2d  SOR_VALID!=pCur-
147dc 3e 65 53 74 61 74 65 29 3b 0a 7d 0a 0a 2f 2a 0a  >eState);.}../*.
147dd 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61  ** Return the da
147de 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
147df 6e 20 68 61 6e 64 6c 65 20 66 6f 72 20 61 20 63  n handle for a c
147e0 75 72 73 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ursor..*/.SQLITE
147e1 5f 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33  _PRIVATE sqlite3
147e2 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 43 75   *sqlite3BtreeCu
147e3 72 73 6f 72 44 62 28 63 6f 6e 73 74 20 42 74 43  rsorDb(const BtC
147e4 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
147e5 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
147e6 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72 2d  mutex_held(pCur-
147e7 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75 74  >pBtree->db->mut
147e8 65 78 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  ex) );.  return 
147e9 70 43 75 72 2d 3e 70 42 74 72 65 65 2d 3e 64 62  pCur->pBtree->db
147ea 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 76 61 6e  ;.}../*.** Advan
147eb 63 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ce the cursor to
147ec 20 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 20   the next entry 
147ed 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
147ee 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66    If.** successf
147ef 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65  ul then set *pRe
147f0 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72  s=0.  If the cur
147f1 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61  sor.** was alrea
147f2 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  dy pointing to t
147f3 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69 6e  he last entry in
147f4 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62 65   the database be
147f5 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75  fore.** this rou
147f6 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64 2c  tine was called,
147f7 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73 3d   then set *pRes=
147f8 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  1..*/.SQLITE_PRI
147f9 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
147fa 42 74 72 65 65 4e 65 78 74 28 42 74 43 75 72 73  BtreeNext(BtCurs
147fb 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20 2a 70  or *pCur, int *p
147fc 52 65 73 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  Res){.  int rc;.
147fd 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65 6d    int idx;.  Mem
147fe 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 0a 20 20  Page *pPage;..  
147ff 61 73 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f  assert( cursorHo
14800 6c 64 73 4d 75 74 65 78 28 70 43 75 72 29 20 29  ldsMutex(pCur) )
14801 3b 0a 20 20 72 63 20 3d 20 72 65 73 74 6f 72 65  ;.  rc = restore
14802 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70  CursorPosition(p
14803 43 75 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  Cur);.  if( rc!=
14804 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
14805 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
14806 20 20 61 73 73 65 72 74 28 20 70 52 65 73 21 3d    assert( pRes!=
14807 30 20 29 3b 0a 20 20 69 66 28 20 43 55 52 53 4f  0 );.  if( CURSO
14808 52 5f 49 4e 56 41 4c 49 44 3d 3d 70 43 75 72 2d  R_INVALID==pCur-
14809 3e 65 53 74 61 74 65 20 29 7b 0a 20 20 20 20 2a  >eState ){.    *
1480a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 72 65  pRes = 1;.    re
1480b 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1480c 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e    }.  if( pCur->
1480d 73 6b 69 70 3e 30 20 29 7b 0a 20 20 20 20 70 43  skip>0 ){.    pC
1480e 75 72 2d 3e 73 6b 69 70 20 3d 20 30 3b 0a 20 20  ur->skip = 0;.  
1480f 20 20 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 20    *pRes = 0;.   
14810 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
14811 4b 3b 0a 20 20 7d 0a 20 20 70 43 75 72 2d 3e 73  K;.  }.  pCur->s
14812 6b 69 70 20 3d 20 30 3b 0a 0a 20 20 70 50 61 67  kip = 0;..  pPag
14813 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
14814 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20  [pCur->iPage];. 
14815 20 69 64 78 20 3d 20 2b 2b 70 43 75 72 2d 3e 61   idx = ++pCur->a
14816 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
14817 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61  ];.  assert( pPa
14818 67 65 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20  ge->isInit );.  
14819 61 73 73 65 72 74 28 20 69 64 78 3c 3d 70 50 61  assert( idx<=pPa
1481a 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 0a 20 20  ge->nCell );..  
1481b 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65  pCur->info.nSize
1481c 20 3d 20 30 3b 0a 20 20 70 43 75 72 2d 3e 76 61   = 0;.  pCur->va
1481d 6c 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 20 20 69  lidNKey = 0;.  i
1481e 66 28 20 69 64 78 3e 3d 70 50 61 67 65 2d 3e 6e  f( idx>=pPage->n
1481f 43 65 6c 6c 20 29 7b 0a 20 20 20 20 69 66 28 20  Cell ){.    if( 
14820 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
14821 20 20 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54        rc = moveT
14822 6f 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74  oChild(pCur, get
14823 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
14824 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
14825 66 73 65 74 2b 38 5d 29 29 3b 0a 20 20 20 20 20  fset+8]));.     
14826 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
14827 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d 20   rc;.      rc = 
14828 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28 70  moveToLeftmost(p
14829 43 75 72 29 3b 0a 20 20 20 20 20 20 2a 70 52 65  Cur);.      *pRe
1482a 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74  s = 0;.      ret
1482b 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20  urn rc;.    }.  
1482c 20 20 64 6f 7b 0a 20 20 20 20 20 20 69 66 28 20    do{.      if( 
1482d 70 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29  pCur->iPage==0 )
1482e 7b 0a 20 20 20 20 20 20 20 20 2a 70 52 65 73 20  {.        *pRes 
1482f 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43 75  = 1;.        pCu
14830 72 2d 3e 65 53 74 61 74 65 20 3d 20 43 55 52 53  r->eState = CURS
14831 4f 52 5f 49 4e 56 41 4c 49 44 3b 0a 20 20 20 20  OR_INVALID;.    
14832 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14833 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  E_OK;.      }.  
14834 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
14835 4d 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75  MoveToParent(pCu
14836 72 29 3b 0a 20 20 20 20 20 20 70 50 61 67 65 20  r);.      pPage 
14837 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 70  = pCur->apPage[p
14838 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 20  Cur->iPage];.   
14839 20 7d 77 68 69 6c 65 28 20 70 43 75 72 2d 3e 61   }while( pCur->a
1483a 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65  iIdx[pCur->iPage
1483b 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20  ]>=pPage->nCell 
1483c 29 3b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20 30  );.    *pRes = 0
1483d 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  ;.    if( pPage-
1483e 3e 69 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 20  >intKey ){.     
1483f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
14840 65 65 4e 65 78 74 28 70 43 75 72 2c 20 70 52 65  eeNext(pCur, pRe
14841 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  s);.    }else{. 
14842 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
14843 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  _OK;.    }.    r
14844 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
14845 2a 70 52 65 73 20 3d 20 30 3b 0a 20 20 69 66 28  *pRes = 0;.  if(
14846 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a   pPage->leaf ){.
14847 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
14848 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 63 20 3d  E_OK;.  }.  rc =
14849 20 6d 6f 76 65 54 6f 4c 65 66 74 6d 6f 73 74 28   moveToLeftmost(
1484a 70 43 75 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  pCur);.  return 
1484b 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53 74  rc;.}.../*.** St
1484c 65 70 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f  ep the cursor to
1484d 20 74 68 65 20 62 61 63 6b 20 74 6f 20 74 68 65   the back to the
1484e 20 70 72 65 76 69 6f 75 73 20 65 6e 74 72 79 20   previous entry 
1484f 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  in the database.
14850 20 20 49 66 0a 2a 2a 20 73 75 63 63 65 73 73 66    If.** successf
14851 75 6c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65  ul then set *pRe
14852 73 3d 30 2e 20 20 49 66 20 74 68 65 20 63 75 72  s=0.  If the cur
14853 73 6f 72 0a 2a 2a 20 77 61 73 20 61 6c 72 65 61  sor.** was alrea
14854 64 79 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  dy pointing to t
14855 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20 69  he first entry i
14856 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 62  n the database b
14857 65 66 6f 72 65 0a 2a 2a 20 74 68 69 73 20 72 6f  efore.** this ro
14858 75 74 69 6e 65 20 77 61 73 20 63 61 6c 6c 65 64  utine was called
14859 2c 20 74 68 65 6e 20 73 65 74 20 2a 70 52 65 73  , then set *pRes
1485a 3d 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  =1..*/.SQLITE_PR
1485b 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1485c 33 42 74 72 65 65 50 72 65 76 69 6f 75 73 28 42  3BtreePrevious(B
1485d 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 69  tCursor *pCur, i
1485e 6e 74 20 2a 70 52 65 73 29 7b 0a 20 20 69 6e 74  nt *pRes){.  int
1485f 20 72 63 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a   rc;.  MemPage *
14860 70 50 61 67 65 3b 0a 0a 20 20 61 73 73 65 72 74  pPage;..  assert
14861 28 20 63 75 72 73 6f 72 48 6f 6c 64 73 4d 75 74  ( cursorHoldsMut
14862 65 78 28 70 43 75 72 29 20 29 3b 0a 20 20 72 63  ex(pCur) );.  rc
14863 20 3d 20 72 65 73 74 6f 72 65 43 75 72 73 6f 72   = restoreCursor
14864 50 6f 73 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a  Position(pCur);.
14865 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
14866 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72  _OK ){.    retur
14867 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 70 43 75 72  n rc;.  }.  pCur
14868 2d 3e 61 74 4c 61 73 74 20 3d 20 30 3b 0a 20 20  ->atLast = 0;.  
14869 69 66 28 20 43 55 52 53 4f 52 5f 49 4e 56 41 4c  if( CURSOR_INVAL
1486a 49 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65  ID==pCur->eState
1486b 20 29 7b 0a 20 20 20 20 2a 70 52 65 73 20 3d 20   ){.    *pRes = 
1486c 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  1;.    return SQ
1486d 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
1486e 66 28 20 70 43 75 72 2d 3e 73 6b 69 70 3c 30 20  f( pCur->skip<0 
1486f 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 73 6b 69  ){.    pCur->ski
14870 70 20 3d 20 30 3b 0a 20 20 20 20 2a 70 52 65 73  p = 0;.    *pRes
14871 20 3d 20 30 3b 0a 20 20 20 20 72 65 74 75 72 6e   = 0;.    return
14872 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a   SQLITE_OK;.  }.
14873 20 20 70 43 75 72 2d 3e 73 6b 69 70 20 3d 20 30    pCur->skip = 0
14874 3b 0a 0a 20 20 70 50 61 67 65 20 3d 20 70 43 75  ;..  pPage = pCu
14875 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72 2d 3e  r->apPage[pCur->
14876 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65 72 74  iPage];.  assert
14877 28 20 70 50 61 67 65 2d 3e 69 73 49 6e 69 74 20  ( pPage->isInit 
14878 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65 2d  );.  if( !pPage-
14879 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 69 6e 74  >leaf ){.    int
1487a 20 69 64 78 20 3d 20 70 43 75 72 2d 3e 61 69 49   idx = pCur->aiI
1487b 64 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b  dx[pCur->iPage];
1487c 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54 6f  .    rc = moveTo
1487d 43 68 69 6c 64 28 70 43 75 72 2c 20 67 65 74 34  Child(pCur, get4
1487e 62 79 74 65 28 66 69 6e 64 43 65 6c 6c 28 70 50  byte(findCell(pP
1487f 61 67 65 2c 20 69 64 78 29 29 29 3b 0a 20 20 20  age, idx)));.   
14880 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
14881 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
14882 7d 0a 20 20 20 20 72 63 20 3d 20 6d 6f 76 65 54  }.    rc = moveT
14883 6f 52 69 67 68 74 6d 6f 73 74 28 70 43 75 72 29  oRightmost(pCur)
14884 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77  ;.  }else{.    w
14885 68 69 6c 65 28 20 70 43 75 72 2d 3e 61 69 49 64  hile( pCur->aiId
14886 78 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3d 3d  x[pCur->iPage]==
14887 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70  0 ){.      if( p
14888 43 75 72 2d 3e 69 50 61 67 65 3d 3d 30 20 29 7b  Cur->iPage==0 ){
14889 0a 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e 65  .        pCur->e
1488a 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
1488b 4e 56 41 4c 49 44 3b 0a 20 20 20 20 20 20 20 20  NVALID;.        
1488c 2a 70 52 65 73 20 3d 20 31 3b 0a 20 20 20 20 20  *pRes = 1;.     
1488d 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1488e 5f 4f 4b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  _OK;.      }.   
1488f 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d     sqlite3BtreeM
14890 6f 76 65 54 6f 50 61 72 65 6e 74 28 70 43 75 72  oveToParent(pCur
14891 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 75  );.    }.    pCu
14892 72 2d 3e 69 6e 66 6f 2e 6e 53 69 7a 65 20 3d 20  r->info.nSize = 
14893 30 3b 0a 20 20 20 20 70 43 75 72 2d 3e 76 61 6c  0;.    pCur->val
14894 69 64 4e 4b 65 79 20 3d 20 30 3b 0a 0a 20 20 20  idNKey = 0;..   
14895 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 70 43 75   pCur->aiIdx[pCu
14896 72 2d 3e 69 50 61 67 65 5d 2d 2d 3b 0a 20 20 20  r->iPage]--;.   
14897 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
14898 70 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67  pPage[pCur->iPag
14899 65 5d 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67  e];.    if( pPag
1489a 65 2d 3e 69 6e 74 4b 65 79 20 26 26 20 21 70 50  e->intKey && !pP
1489b 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  age->leaf ){.   
1489c 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
1489d 74 72 65 65 50 72 65 76 69 6f 75 73 28 70 43 75  treePrevious(pCu
1489e 72 2c 20 70 52 65 73 29 3b 0a 20 20 20 20 7d 65  r, pRes);.    }e
1489f 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  lse{.      rc = 
148a0 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
148a1 0a 20 20 7d 0a 20 20 2a 70 52 65 73 20 3d 20 30  .  }.  *pRes = 0
148a2 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  ;.  return rc;.}
148a3 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
148a4 20 61 20 6e 65 77 20 70 61 67 65 20 66 72 6f 6d   a new page from
148a5 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
148a6 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65  le..**.** The ne
148a7 77 20 70 61 67 65 20 69 73 20 6d 61 72 6b 65 64  w page is marked
148a8 20 61 73 20 64 69 72 74 79 2e 20 20 28 49 6e 20   as dirty.  (In 
148a9 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 73 71 6c  other words, sql
148aa 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 29  ite3PagerWrite()
148ab 0a 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20  .** has already 
148ac 62 65 65 6e 20 63 61 6c 6c 65 64 20 6f 6e 20 74  been called on t
148ad 68 65 20 6e 65 77 20 70 61 67 65 2e 29 20 20 54  he new page.)  T
148ae 68 65 20 6e 65 77 20 70 61 67 65 20 68 61 73 20  he new page has 
148af 61 6c 73 6f 0a 2a 2a 20 62 65 65 6e 20 72 65 66  also.** been ref
148b0 65 72 65 6e 63 65 64 20 61 6e 64 20 74 68 65 20  erenced and the 
148b1 63 61 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20  calling routine 
148b2 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
148b3 6f 72 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 73 71  or calling.** sq
148b4 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66 28  lite3PagerUnref(
148b5 29 20 6f 6e 20 74 68 65 20 6e 65 77 20 70 61 67  ) on the new pag
148b6 65 20 77 68 65 6e 20 69 74 20 69 73 20 64 6f 6e  e when it is don
148b7 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f  e..**.** SQLITE_
148b8 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20 6f  OK is returned o
148b9 6e 20 73 75 63 63 65 73 73 2e 20 20 41 6e 79 20  n success.  Any 
148ba 6f 74 68 65 72 20 72 65 74 75 72 6e 20 76 61 6c  other return val
148bb 75 65 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20  ue indicates.** 
148bc 61 6e 20 65 72 72 6f 72 2e 20 20 2a 70 70 50 61  an error.  *ppPa
148bd 67 65 20 61 6e 64 20 2a 70 50 67 6e 6f 20 61 72  ge and *pPgno ar
148be 65 20 75 6e 64 65 66 69 6e 65 64 20 69 6e 20 74  e undefined in t
148bf 68 65 20 65 76 65 6e 74 20 6f 66 20 61 6e 20 65  he event of an e
148c0 72 72 6f 72 2e 0a 2a 2a 20 44 6f 20 6e 6f 74 20  rror..** Do not 
148c1 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 50 61  invoke sqlite3Pa
148c2 67 65 72 55 6e 72 65 66 28 29 20 6f 6e 20 2a 70  gerUnref() on *p
148c3 70 50 61 67 65 20 69 66 20 61 6e 20 65 72 72 6f  pPage if an erro
148c4 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  r is returned..*
148c5 2a 0a 2a 2a 20 49 66 20 74 68 65 20 22 6e 65 61  *.** If the "nea
148c6 72 62 79 22 20 70 61 72 61 6d 65 74 65 72 20 69  rby" parameter i
148c7 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 61 20  s not 0, then a 
148c8 28 66 65 65 62 6c 65 29 20 65 66 66 6f 72 74 20  (feeble) effort 
148c9 69 73 20 6d 61 64 65 20 74 6f 20 0a 2a 2a 20 6c  is made to .** l
148ca 6f 63 61 74 65 20 61 20 70 61 67 65 20 63 6c 6f  ocate a page clo
148cb 73 65 20 74 6f 20 74 68 65 20 70 61 67 65 20 6e  se to the page n
148cc 75 6d 62 65 72 20 22 6e 65 61 72 62 79 22 2e 20  umber "nearby". 
148cd 20 54 68 69 73 20 63 61 6e 20 62 65 20 75 73 65   This can be use
148ce 64 20 69 6e 20 61 6e 0a 2a 2a 20 61 74 74 65 6d  d in an.** attem
148cf 70 74 20 74 6f 20 6b 65 65 70 20 72 65 6c 61 74  pt to keep relat
148d0 65 64 20 70 61 67 65 73 20 63 6c 6f 73 65 20 74  ed pages close t
148d1 6f 20 65 61 63 68 20 6f 74 68 65 72 20 69 6e 20  o each other in 
148d2 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
148d3 65 2c 0a 2a 2a 20 77 68 69 63 68 20 69 6e 20 74  e,.** which in t
148d4 75 72 6e 20 63 61 6e 20 6d 61 6b 65 20 64 61 74  urn can make dat
148d5 61 62 61 73 65 20 61 63 63 65 73 73 20 66 61 73  abase access fas
148d6 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ter..**.** If th
148d7 65 20 22 65 78 61 63 74 22 20 70 61 72 61 6d 65  e "exact" parame
148d8 74 65 72 20 69 73 20 6e 6f 74 20 30 2c 20 61 6e  ter is not 0, an
148d9 64 20 74 68 65 20 70 61 67 65 2d 6e 75 6d 62 65  d the page-numbe
148da 72 20 6e 65 61 72 62 79 20 65 78 69 73 74 73 20  r nearby exists 
148db 0a 2a 2a 20 61 6e 79 77 68 65 72 65 20 6f 6e 20  .** anywhere on 
148dc 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c 20 74  the free-list, t
148dd 68 65 6e 20 69 74 20 69 73 20 67 75 61 72 65 6e  hen it is guaren
148de 74 65 65 64 20 74 6f 20 62 65 20 72 65 74 75 72  teed to be retur
148df 6e 65 64 2e 20 54 68 69 73 0a 2a 2a 20 69 73 20  ned. This.** is 
148e0 6f 6e 6c 79 20 75 73 65 64 20 62 79 20 61 75 74  only used by aut
148e1 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
148e2 65 73 20 77 68 65 6e 20 61 6c 6c 6f 63 61 74 69  es when allocati
148e3 6e 67 20 61 20 6e 65 77 20 74 61 62 6c 65 2e 0a  ng a new table..
148e4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6c  */.static int al
148e5 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67 65 28  locateBtreePage(
148e6 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
148e7 2c 20 0a 20 20 4d 65 6d 50 61 67 65 20 2a 2a 70  , .  MemPage **p
148e8 70 50 61 67 65 2c 20 0a 20 20 50 67 6e 6f 20 2a  pPage, .  Pgno *
148e9 70 50 67 6e 6f 2c 20 0a 20 20 50 67 6e 6f 20 6e  pPgno, .  Pgno n
148ea 65 61 72 62 79 2c 0a 20 20 75 38 20 65 78 61 63  earby,.  u8 exac
148eb 74 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  t.){.  MemPage *
148ec 70 50 61 67 65 31 3b 0a 20 20 69 6e 74 20 72 63  pPage1;.  int rc
148ed 3b 0a 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 2f  ;.  int n;     /
148ee 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65  * Number of page
148ef 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  s on the freelis
148f0 74 20 2a 2f 0a 20 20 69 6e 74 20 6b 3b 20 20 20  t */.  int k;   
148f1 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6c    /* Number of l
148f2 65 61 76 65 73 20 6f 6e 20 74 68 65 20 74 72 75  eaves on the tru
148f3 6e 6b 20 6f 66 20 74 68 65 20 66 72 65 65 6c 69  nk of the freeli
148f4 73 74 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20  st */.  MemPage 
148f5 2a 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 4d  *pTrunk = 0;.  M
148f6 65 6d 50 61 67 65 20 2a 70 50 72 65 76 54 72 75  emPage *pPrevTru
148f7 6e 6b 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72  nk = 0;..  asser
148f8 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
148f9 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75 74 65 78  _held(pBt->mutex
148fa 29 20 29 3b 0a 20 20 70 50 61 67 65 31 20 3d 20  ) );.  pPage1 = 
148fb 70 42 74 2d 3e 70 50 61 67 65 31 3b 0a 20 20 6e  pBt->pPage1;.  n
148fc 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61   = get4byte(&pPa
148fd 67 65 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 3b  ge1->aData[36]);
148fe 0a 20 20 69 66 28 20 6e 3e 30 20 29 7b 0a 20 20  .  if( n>0 ){.  
148ff 20 20 2f 2a 20 54 68 65 72 65 20 61 72 65 20 70    /* There are p
14900 61 67 65 73 20 6f 6e 20 74 68 65 20 66 72 65 65  ages on the free
14901 6c 69 73 74 2e 20 20 52 65 75 73 65 20 6f 6e 65  list.  Reuse one
14902 20 6f 66 20 74 68 6f 73 65 20 70 61 67 65 73 2e   of those pages.
14903 20 2a 2f 0a 20 20 20 20 50 67 6e 6f 20 69 54 72   */.    Pgno iTr
14904 75 6e 6b 3b 0a 20 20 20 20 75 38 20 73 65 61 72  unk;.    u8 sear
14905 63 68 4c 69 73 74 20 3d 20 30 3b 20 2f 2a 20 49  chList = 0; /* I
14906 66 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20  f the free-list 
14907 6d 75 73 74 20 62 65 20 73 65 61 72 63 68 65 64  must be searched
14908 20 66 6f 72 20 27 6e 65 61 72 62 79 27 20 2a 2f   for 'nearby' */
14909 0a 20 20 20 20 0a 20 20 20 20 2f 2a 20 49 66 20  .    .    /* If 
1490a 74 68 65 20 27 65 78 61 63 74 27 20 70 61 72 61  the 'exact' para
1490b 6d 65 74 65 72 20 77 61 73 20 74 72 75 65 20 61  meter was true a
1490c 6e 64 20 61 20 71 75 65 72 79 20 6f 66 20 74 68  nd a query of th
1490d 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 0a 20 20  e pointer-map.  
1490e 20 20 2a 2a 20 73 68 6f 77 73 20 74 68 61 74 20    ** shows that 
1490f 74 68 65 20 70 61 67 65 20 27 6e 65 61 72 62 79  the page 'nearby
14910 27 20 69 73 20 73 6f 6d 65 77 68 65 72 65 20 6f  ' is somewhere o
14911 6e 20 74 68 65 20 66 72 65 65 2d 6c 69 73 74 2c  n the free-list,
14912 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 65   then.    ** the
14913 20 65 6e 74 69 72 65 2d 6c 69 73 74 20 77 69 6c   entire-list wil
14914 6c 20 62 65 20 73 65 61 72 63 68 65 64 20 66 6f  l be searched fo
14915 72 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 20  r that page..   
14916 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   */.#ifndef SQLI
14917 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
14918 55 4d 0a 20 20 20 20 69 66 28 20 65 78 61 63 74  UM.    if( exact
14919 20 26 26 20 6e 65 61 72 62 79 3c 3d 70 61 67 65   && nearby<=page
1491a 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74 29 20  rPagecount(pBt) 
1491b 29 7b 0a 20 20 20 20 20 20 75 38 20 65 54 79 70  ){.      u8 eTyp
1491c 65 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  e;.      assert(
1491d 20 6e 65 61 72 62 79 3e 30 20 29 3b 0a 20 20 20   nearby>0 );.   
1491e 20 20 20 61 73 73 65 72 74 28 20 70 42 74 2d 3e     assert( pBt->
1491f 61 75 74 6f 56 61 63 75 75 6d 20 29 3b 0a 20 20  autoVacuum );.  
14920 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
14921 65 74 28 70 42 74 2c 20 6e 65 61 72 62 79 2c 20  et(pBt, nearby, 
14922 26 65 54 79 70 65 2c 20 30 29 3b 0a 20 20 20 20  &eType, 0);.    
14923 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
14924 6e 20 72 63 3b 0a 20 20 20 20 20 20 69 66 28 20  n rc;.      if( 
14925 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 46 52  eType==PTRMAP_FR
14926 45 45 50 41 47 45 20 29 7b 0a 20 20 20 20 20 20  EEPAGE ){.      
14927 20 20 73 65 61 72 63 68 4c 69 73 74 20 3d 20 31    searchList = 1
14928 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
14929 2a 70 50 67 6e 6f 20 3d 20 6e 65 61 72 62 79 3b  *pPgno = nearby;
1492a 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  .    }.#endif.. 
1492b 20 20 20 2f 2a 20 44 65 63 72 65 6d 65 6e 74 20     /* Decrement 
1492c 74 68 65 20 66 72 65 65 2d 6c 69 73 74 20 63 6f  the free-list co
1492d 75 6e 74 20 62 79 20 31 2e 20 53 65 74 20 69 54  unt by 1. Set iT
1492e 72 75 6e 6b 20 74 6f 20 74 68 65 20 69 6e 64 65  runk to the inde
1492f 78 20 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20  x of the.    ** 
14930 66 69 72 73 74 20 66 72 65 65 2d 6c 69 73 74 20  first free-list 
14931 74 72 75 6e 6b 20 70 61 67 65 2e 20 69 50 72 65  trunk page. iPre
14932 76 54 72 75 6e 6b 20 69 73 20 69 6e 69 74 69 61  vTrunk is initia
14933 6c 6c 79 20 31 2e 0a 20 20 20 20 2a 2f 0a 20 20  lly 1..    */.  
14934 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
14935 67 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d  gerWrite(pPage1-
14936 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 69  >pDbPage);.    i
14937 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72  f( rc ) return r
14938 63 3b 0a 20 20 20 20 70 75 74 34 62 79 74 65 28  c;.    put4byte(
14939 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
1493a 36 5d 2c 20 6e 2d 31 29 3b 0a 0a 20 20 20 20 2f  6], n-1);..    /
1493b 2a 20 54 68 65 20 63 6f 64 65 20 77 69 74 68 69  * The code withi
1493c 6e 20 74 68 69 73 20 6c 6f 6f 70 20 69 73 20 72  n this loop is r
1493d 75 6e 20 6f 6e 6c 79 20 6f 6e 63 65 20 69 66 20  un only once if 
1493e 74 68 65 20 27 73 65 61 72 63 68 4c 69 73 74 27  the 'searchList'
1493f 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a   variable.    **
14940 20 69 73 20 6e 6f 74 20 74 72 75 65 2e 20 4f 74   is not true. Ot
14941 68 65 72 77 69 73 65 2c 20 69 74 20 72 75 6e 73  herwise, it runs
14942 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74   once for each t
14943 72 75 6e 6b 2d 70 61 67 65 20 6f 6e 20 74 68 65  runk-page on the
14944 0a 20 20 20 20 2a 2a 20 66 72 65 65 2d 6c 69 73  .    ** free-lis
14945 74 20 75 6e 74 69 6c 20 74 68 65 20 70 61 67 65  t until the page
14946 20 27 6e 65 61 72 62 79 27 20 69 73 20 6c 6f 63   'nearby' is loc
14947 61 74 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ated..    */.   
14948 20 64 6f 20 7b 0a 20 20 20 20 20 20 70 50 72 65   do {.      pPre
14949 76 54 72 75 6e 6b 20 3d 20 70 54 72 75 6e 6b 3b  vTrunk = pTrunk;
1494a 0a 20 20 20 20 20 20 69 66 28 20 70 50 72 65 76  .      if( pPrev
1494b 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20 20 20  Trunk ){.       
1494c 20 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79   iTrunk = get4by
1494d 74 65 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e  te(&pPrevTrunk->
1494e 61 44 61 74 61 5b 30 5d 29 3b 0a 20 20 20 20 20  aData[0]);.     
1494f 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
14950 69 54 72 75 6e 6b 20 3d 20 67 65 74 34 62 79 74  iTrunk = get4byt
14951 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
14952 5b 33 32 5d 29 3b 0a 20 20 20 20 20 20 7d 0a 20  [32]);.      }. 
14953 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
14954 33 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42  3BtreeGetPage(pB
14955 74 2c 20 69 54 72 75 6e 6b 2c 20 26 70 54 72 75  t, iTrunk, &pTru
14956 6e 6b 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  nk, 0);.      if
14957 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20  ( rc ){.        
14958 70 54 72 75 6e 6b 20 3d 20 30 3b 0a 20 20 20 20  pTrunk = 0;.    
14959 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c 6c      goto end_all
1495a 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20 20  ocate_page;.    
1495b 20 20 7d 0a 0a 20 20 20 20 20 20 6b 20 3d 20 67    }..      k = g
1495c 65 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b 2d  et4byte(&pTrunk-
1495d 3e 61 44 61 74 61 5b 34 5d 29 3b 0a 20 20 20 20  >aData[4]);.    
1495e 20 20 69 66 28 20 6b 3d 3d 30 20 26 26 20 21 73    if( k==0 && !s
1495f 65 61 72 63 68 4c 69 73 74 20 29 7b 0a 20 20 20  earchList ){.   
14960 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e       /* The trun
14961 6b 20 68 61 73 20 6e 6f 20 6c 65 61 76 65 73 20  k has no leaves 
14962 61 6e 64 20 74 68 65 20 6c 69 73 74 20 69 73 20  and the list is 
14963 6e 6f 74 20 62 65 69 6e 67 20 73 65 61 72 63 68  not being search
14964 65 64 2e 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  ed. .        ** 
14965 53 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 74  So extract the t
14966 72 75 6e 6b 20 70 61 67 65 20 69 74 73 65 6c 66  runk page itself
14967 20 61 6e 64 20 75 73 65 20 69 74 20 61 73 20 74   and use it as t
14968 68 65 20 6e 65 77 6c 79 20 0a 20 20 20 20 20 20  he newly .      
14969 20 20 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 70    ** allocated p
1496a 61 67 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 61  age */.        a
1496b 73 73 65 72 74 28 20 70 50 72 65 76 54 72 75 6e  ssert( pPrevTrun
1496c 6b 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20  k==0 );.        
1496d 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
1496e 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
1496f 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
14970 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
14971 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
14972 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20  locate_page;.   
14973 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 2a       }.        *
14974 70 50 67 6e 6f 20 3d 20 69 54 72 75 6e 6b 3b 0a  pPgno = iTrunk;.
14975 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26          memcpy(&
14976 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 32  pPage1->aData[32
14977 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  ], &pTrunk->aDat
14978 61 5b 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20  a[0], 4);.      
14979 20 20 2a 70 70 50 61 67 65 20 3d 20 70 54 72 75    *ppPage = pTru
1497a 6e 6b 3b 0a 20 20 20 20 20 20 20 20 70 54 72 75  nk;.        pTru
1497b 6e 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  nk = 0;.        
1497c 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
1497d 3a 20 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20  : %d trunk - %d 
1497e 66 72 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c  free pages left\
1497f 6e 22 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29  n", *pPgno, n-1)
14980 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
14981 66 28 20 6b 3e 70 42 74 2d 3e 75 73 61 62 6c 65  f( k>pBt->usable
14982 53 69 7a 65 2f 34 20 2d 20 32 20 29 7b 0a 20 20  Size/4 - 2 ){.  
14983 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 6f        /* Value o
14984 66 20 6b 20 69 73 20 6f 75 74 20 6f 66 20 72 61  f k is out of ra
14985 6e 67 65 2e 20 20 44 61 74 61 62 61 73 65 20 63  nge.  Database c
14986 6f 72 72 75 70 74 69 6f 6e 20 2a 2f 0a 20 20 20  orruption */.   
14987 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
14988 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
14989 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
1498a 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 23  allocate_page;.#
1498b 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1498c 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
1498d 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73 65      }else if( se
1498e 61 72 63 68 4c 69 73 74 20 26 26 20 6e 65 61 72  archList && near
1498f 62 79 3d 3d 69 54 72 75 6e 6b 20 29 7b 0a 20 20  by==iTrunk ){.  
14990 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 69 73        /* The lis
14991 74 20 69 73 20 62 65 69 6e 67 20 73 65 61 72 63  t is being searc
14992 68 65 64 20 61 6e 64 20 74 68 69 73 20 74 72 75  hed and this tru
14993 6e 6b 20 70 61 67 65 20 69 73 20 74 68 65 20 70  nk page is the p
14994 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74  age.        ** t
14995 6f 20 61 6c 6c 6f 63 61 74 65 2c 20 72 65 67 61  o allocate, rega
14996 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
14997 72 20 69 74 20 68 61 73 20 6c 65 61 76 65 73 2e  r it has leaves.
14998 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
14999 20 20 20 20 61 73 73 65 72 74 28 20 2a 70 50 67      assert( *pPg
1499a 6e 6f 3d 3d 69 54 72 75 6e 6b 20 29 3b 0a 20 20  no==iTrunk );.  
1499b 20 20 20 20 20 20 2a 70 70 50 61 67 65 20 3d 20        *ppPage = 
1499c 70 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20  pTrunk;.        
1499d 73 65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a  searchList = 0;.
1499e 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
1499f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
149a0 54 72 75 6e 6b 2d 3e 70 44 62 50 61 67 65 29 3b  Trunk->pDbPage);
149a1 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 20  .        if( rc 
149a2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74  ){.          got
149a3 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
149a4 61 67 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  age;.        }. 
149a5 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 30 20         if( k==0 
149a6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
149a7 20 21 70 50 72 65 76 54 72 75 6e 6b 20 29 7b 0a   !pPrevTrunk ){.
149a8 20 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63              memc
149a9 70 79 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74  py(&pPage1->aDat
149aa 61 5b 33 32 5d 2c 20 26 70 54 72 75 6e 6b 2d 3e  a[32], &pTrunk->
149ab 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a 20 20  aData[0], 4);.  
149ac 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
149ad 20 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70             memcp
149ae 79 28 26 70 50 72 65 76 54 72 75 6e 6b 2d 3e 61  y(&pPrevTrunk->a
149af 44 61 74 61 5b 30 5d 2c 20 26 70 54 72 75 6e 6b  Data[0], &pTrunk
149b0 2d 3e 61 44 61 74 61 5b 30 5d 2c 20 34 29 3b 0a  ->aData[0], 4);.
149b1 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
149b2 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
149b3 20 20 20 20 20 2f 2a 20 54 68 65 20 74 72 75 6e       /* The trun
149b4 6b 20 70 61 67 65 20 69 73 20 72 65 71 75 69 72  k page is requir
149b5 65 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72  ed by the caller
149b6 20 62 75 74 20 69 74 20 63 6f 6e 74 61 69 6e 73   but it contains
149b7 20 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 70   .          ** p
149b8 6f 69 6e 74 65 72 73 20 74 6f 20 66 72 65 65 2d  ointers to free-
149b9 6c 69 73 74 20 6c 65 61 76 65 73 2e 20 54 68 65  list leaves. The
149ba 20 66 69 72 73 74 20 6c 65 61 66 20 62 65 63 6f   first leaf beco
149bb 6d 65 73 20 61 20 74 72 75 6e 6b 0a 20 20 20 20  mes a trunk.    
149bc 20 20 20 20 20 20 2a 2a 20 70 61 67 65 20 69 6e        ** page in
149bd 20 74 68 69 73 20 63 61 73 65 2e 0a 20 20 20 20   this case..    
149be 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
149bf 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77     MemPage *pNew
149c0 54 72 75 6e 6b 3b 0a 20 20 20 20 20 20 20 20 20  Trunk;.         
149c1 20 50 67 6e 6f 20 69 4e 65 77 54 72 75 6e 6b 20   Pgno iNewTrunk 
149c2 3d 20 67 65 74 34 62 79 74 65 28 26 70 54 72 75  = get4byte(&pTru
149c3 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 29 3b 0a 20  nk->aData[8]);. 
149c4 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
149c5 6c 69 74 65 33 42 74 72 65 65 47 65 74 50 61 67  lite3BtreeGetPag
149c6 65 28 70 42 74 2c 20 69 4e 65 77 54 72 75 6e 6b  e(pBt, iNewTrunk
149c7 2c 20 26 70 4e 65 77 54 72 75 6e 6b 2c 20 30 29  , &pNewTrunk, 0)
149c8 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  ;.          if( 
149c9 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
149ca 0a 20 20 20 20 20 20 20 20 20 20 20 20 67 6f 74  .            got
149cb 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f 70  o end_allocate_p
149cc 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  age;.          }
149cd 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20  .          rc = 
149ce 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
149cf 65 28 70 4e 65 77 54 72 75 6e 6b 2d 3e 70 44 62  e(pNewTrunk->pDb
149d0 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Page);.         
149d1 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
149d2 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
149d3 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 4e    releasePage(pN
149d4 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20 20 20  ewTrunk);.      
149d5 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61        goto end_a
149d6 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20  llocate_page;.  
149d7 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
149d8 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65 77      memcpy(&pNew
149d9 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c  Trunk->aData[0],
149da 20 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b   &pTrunk->aData[
149db 30 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  0], 4);.        
149dc 20 20 70 75 74 34 62 79 74 65 28 26 70 4e 65 77    put4byte(&pNew
149dd 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d 2c  Trunk->aData[4],
149de 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20   k-1);.         
149df 20 6d 65 6d 63 70 79 28 26 70 4e 65 77 54 72 75   memcpy(&pNewTru
149e0 6e 6b 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 26 70  nk->aData[8], &p
149e1 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 31 32 5d  Trunk->aData[12]
149e2 2c 20 28 6b 2d 31 29 2a 34 29 3b 0a 20 20 20 20  , (k-1)*4);.    
149e3 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
149e4 65 28 70 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20  e(pNewTrunk);.  
149e5 20 20 20 20 20 20 20 20 69 66 28 20 21 70 50 72          if( !pPr
149e6 65 76 54 72 75 6e 6b 20 29 7b 0a 20 20 20 20 20  evTrunk ){.     
149e7 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
149e8 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33  &pPage1->aData[3
149e9 32 5d 2c 20 69 4e 65 77 54 72 75 6e 6b 29 3b 0a  2], iNewTrunk);.
149ea 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b            }else{
149eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 63 20  .            rc 
149ec 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
149ed 69 74 65 28 70 50 72 65 76 54 72 75 6e 6b 2d 3e  ite(pPrevTrunk->
149ee 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
149ef 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
149f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f                go
149f1 74 6f 20 65 6e 64 5f 61 6c 6c 6f 63 61 74 65 5f  to end_allocate_
149f2 70 61 67 65 3b 0a 20 20 20 20 20 20 20 20 20 20  page;.          
149f3 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
149f4 70 75 74 34 62 79 74 65 28 26 70 50 72 65 76 54  put4byte(&pPrevT
149f5 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 30 5d 2c 20  runk->aData[0], 
149f6 69 4e 65 77 54 72 75 6e 6b 29 3b 0a 20 20 20 20  iNewTrunk);.    
149f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
149f8 7d 0a 20 20 20 20 20 20 20 20 70 54 72 75 6e 6b  }.        pTrunk
149f9 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 54 52   = 0;.        TR
149fa 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45 3a 20  ACE(("ALLOCATE: 
149fb 25 64 20 74 72 75 6e 6b 20 2d 20 25 64 20 66 72  %d trunk - %d fr
149fc 65 65 20 70 61 67 65 73 20 6c 65 66 74 5c 6e 22  ee pages left\n"
149fd 2c 20 2a 70 50 67 6e 6f 2c 20 6e 2d 31 29 29 3b  , *pPgno, n-1));
149fe 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7d 65  .#endif.      }e
149ff 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  lse{.        /* 
14a00 45 78 74 72 61 63 74 20 61 20 6c 65 61 66 20 66  Extract a leaf f
14a01 72 6f 6d 20 74 68 65 20 74 72 75 6e 6b 20 2a 2f  rom the trunk */
14a02 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 6c 6f  .        int clo
14a03 73 65 73 74 3b 0a 20 20 20 20 20 20 20 20 50 67  sest;.        Pg
14a04 6e 6f 20 69 50 61 67 65 3b 0a 20 20 20 20 20 20  no iPage;.      
14a05 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
14a06 2a 61 44 61 74 61 20 3d 20 70 54 72 75 6e 6b 2d  *aData = pTrunk-
14a07 3e 61 44 61 74 61 3b 0a 20 20 20 20 20 20 20 20  >aData;.        
14a08 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
14a09 72 57 72 69 74 65 28 70 54 72 75 6e 6b 2d 3e 70  rWrite(pTrunk->p
14a0a 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  DbPage);.       
14a0b 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
14a0c 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 61 6c       goto end_al
14a0d 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20 20 20  locate_page;.   
14a0e 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
14a0f 66 28 20 6e 65 61 72 62 79 3e 30 20 29 7b 0a 20  f( nearby>0 ){. 
14a10 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 2c 20           int i, 
14a11 64 69 73 74 3b 0a 20 20 20 20 20 20 20 20 20 20  dist;.          
14a12 63 6c 6f 73 65 73 74 20 3d 20 30 3b 0a 20 20 20  closest = 0;.   
14a13 20 20 20 20 20 20 20 64 69 73 74 20 3d 20 67 65         dist = ge
14a14 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 38 5d  t4byte(&aData[8]
14a15 29 20 2d 20 6e 65 61 72 62 79 3b 0a 20 20 20 20  ) - nearby;.    
14a16 20 20 20 20 20 20 69 66 28 20 64 69 73 74 3c 30        if( dist<0
14a17 20 29 20 64 69 73 74 20 3d 20 2d 64 69 73 74 3b   ) dist = -dist;
14a18 0a 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69  .          for(i
14a19 3d 31 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20  =1; i<k; i++){. 
14a1a 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 64             int d
14a1b 32 20 3d 20 67 65 74 34 62 79 74 65 28 26 61 44  2 = get4byte(&aD
14a1c 61 74 61 5b 38 2b 69 2a 34 5d 29 20 2d 20 6e 65  ata[8+i*4]) - ne
14a1d 61 72 62 79 3b 0a 20 20 20 20 20 20 20 20 20 20  arby;.          
14a1e 20 20 69 66 28 20 64 32 3c 30 20 29 20 64 32 20    if( d2<0 ) d2 
14a1f 3d 20 2d 64 32 3b 0a 20 20 20 20 20 20 20 20 20  = -d2;.         
14a20 20 20 20 69 66 28 20 64 32 3c 64 69 73 74 20 29     if( d2<dist )
14a21 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
14a22 63 6c 6f 73 65 73 74 20 3d 20 69 3b 0a 20 20 20  closest = i;.   
14a23 20 20 20 20 20 20 20 20 20 20 20 64 69 73 74 20             dist 
14a24 3d 20 64 32 3b 0a 20 20 20 20 20 20 20 20 20 20  = d2;.          
14a25 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a    }.          }.
14a26 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
14a27 20 20 20 20 20 20 20 20 20 63 6c 6f 73 65 73 74           closest
14a28 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
14a29 0a 20 20 20 20 20 20 20 20 69 50 61 67 65 20 3d  .        iPage =
14a2a 20 67 65 74 34 62 79 74 65 28 26 61 44 61 74 61   get4byte(&aData
14a2b 5b 38 2b 63 6c 6f 73 65 73 74 2a 34 5d 29 3b 0a  [8+closest*4]);.
14a2c 20 20 20 20 20 20 20 20 69 66 28 20 21 73 65 61          if( !sea
14a2d 72 63 68 4c 69 73 74 20 7c 7c 20 69 50 61 67 65  rchList || iPage
14a2e 3d 3d 6e 65 61 72 62 79 20 29 7b 0a 20 20 20 20  ==nearby ){.    
14a2f 20 20 20 20 20 20 50 67 6e 6f 20 6e 50 61 67 65        Pgno nPage
14a30 3b 0a 20 20 20 20 20 20 20 20 20 20 2a 70 50 67  ;.          *pPg
14a31 6e 6f 20 3d 20 69 50 61 67 65 3b 0a 20 20 20 20  no = iPage;.    
14a32 20 20 20 20 20 20 6e 50 61 67 65 20 3d 20 70 61        nPage = pa
14a33 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70 42 74  gerPagecount(pBt
14a34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
14a35 20 2a 70 50 67 6e 6f 3e 6e 50 61 67 65 20 29 7b   *pPgno>nPage ){
14a36 0a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  .            /* 
14a37 46 72 65 65 20 70 61 67 65 20 6f 66 66 20 74 68  Free page off th
14a38 65 20 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c  e end of the fil
14a39 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20  e */.           
14a3a 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f 52   rc = SQLITE_COR
14a3b 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 20  RUPT_BKPT;.     
14a3c 20 20 20 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f         goto end_
14a3d 61 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3b 0a 20  allocate_page;. 
14a3e 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
14a3f 20 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c       TRACE(("ALL
14a40 4f 43 41 54 45 3a 20 25 64 20 77 61 73 20 6c 65  OCATE: %d was le
14a41 61 66 20 25 64 20 6f 66 20 25 64 20 6f 6e 20 74  af %d of %d on t
14a42 72 75 6e 6b 20 25 64 22 0a 20 20 20 20 20 20 20  runk %d".       
14a43 20 20 20 20 20 20 20 20 20 20 22 3a 20 25 64 20            ": %d 
14a44 6d 6f 72 65 20 66 72 65 65 20 70 61 67 65 73 5c  more free pages\
14a45 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
14a46 20 20 20 20 20 2a 70 50 67 6e 6f 2c 20 63 6c 6f       *pPgno, clo
14a47 73 65 73 74 2b 31 2c 20 6b 2c 20 70 54 72 75 6e  sest+1, k, pTrun
14a48 6b 2d 3e 70 67 6e 6f 2c 20 6e 2d 31 29 29 3b 0a  k->pgno, n-1));.
14a49 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 6c            if( cl
14a4a 6f 73 65 73 74 3c 6b 2d 31 20 29 7b 0a 20 20 20  osest<k-1 ){.   
14a4b 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70 79 28           memcpy(
14a4c 26 61 44 61 74 61 5b 38 2b 63 6c 6f 73 65 73 74  &aData[8+closest
14a4d 2a 34 5d 2c 20 26 61 44 61 74 61 5b 34 2b 6b 2a  *4], &aData[4+k*
14a4e 34 5d 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  4], 4);.        
14a4f 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 70 75    }.          pu
14a50 74 34 62 79 74 65 28 26 61 44 61 74 61 5b 34 5d  t4byte(&aData[4]
14a51 2c 20 6b 2d 31 29 3b 0a 20 20 20 20 20 20 20 20  , k-1);.        
14a52 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
14a53 72 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20  reeGetPage(pBt, 
14a54 2a 70 50 67 6e 6f 2c 20 70 70 50 61 67 65 2c 20  *pPgno, ppPage, 
14a55 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  1);.          if
14a56 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
14a57 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  ){.            s
14a58 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e 74 52  qlite3PagerDontR
14a59 6f 6c 6c 62 61 63 6b 28 28 2a 70 70 50 61 67 65  ollback((*ppPage
14a5a 29 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  )->pDbPage);.   
14a5b 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71           rc = sq
14a5c 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28  lite3PagerWrite(
14a5d 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50 61  (*ppPage)->pDbPa
14a5e 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ge);.           
14a5f 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
14a60 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
14a61 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
14a62 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  *ppPage);.      
14a63 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
14a64 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 73 65    }.          se
14a65 61 72 63 68 4c 69 73 74 20 3d 20 30 3b 0a 20 20  archList = 0;.  
14a66 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
14a67 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67        releasePag
14a68 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a 20  e(pPrevTrunk);. 
14a69 20 20 20 20 20 70 50 72 65 76 54 72 75 6e 6b 20       pPrevTrunk 
14a6a 3d 20 30 3b 0a 20 20 20 20 7d 77 68 69 6c 65 28  = 0;.    }while(
14a6b 20 73 65 61 72 63 68 4c 69 73 74 20 29 3b 0a 20   searchList );. 
14a6c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54   }else{.    /* T
14a6d 68 65 72 65 20 61 72 65 20 6e 6f 20 70 61 67 65  here are no page
14a6e 73 20 6f 6e 20 74 68 65 20 66 72 65 65 6c 69 73  s on the freelis
14a6f 74 2c 20 73 6f 20 63 72 65 61 74 65 20 61 20 6e  t, so create a n
14a70 65 77 20 70 61 67 65 20 61 74 20 74 68 65 0a 20  ew page at the. 
14a71 20 20 20 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65     ** end of the
14a72 20 66 69 6c 65 20 2a 2f 0a 20 20 20 20 69 6e 74   file */.    int
14a73 20 6e 50 61 67 65 20 3d 20 70 61 67 65 72 50 61   nPage = pagerPa
14a74 67 65 63 6f 75 6e 74 28 70 42 74 29 3b 0a 20 20  gecount(pBt);.  
14a75 20 20 2a 70 50 67 6e 6f 20 3d 20 6e 50 61 67 65    *pPgno = nPage
14a76 20 2b 20 31 3b 0a 0a 23 69 66 6e 64 65 66 20 53   + 1;..#ifndef S
14a77 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
14a78 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70 42  ACUUM.    if( pB
14a79 74 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a 20 20 20  t->nTrunc ){.   
14a7a 20 20 20 2f 2a 20 41 6e 20 69 6e 63 72 2d 76 61     /* An incr-va
14a7b 63 75 75 6d 20 68 61 73 20 61 6c 72 65 61 64 79  cuum has already
14a7c 20 72 75 6e 20 77 69 74 68 69 6e 20 74 68 69 73   run within this
14a7d 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 53 6f   transaction. So
14a7e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 70 61   the.      ** pa
14a7f 67 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 69  ge to allocate i
14a80 73 20 6e 6f 74 20 66 72 6f 6d 20 74 68 65 20 70  s not from the p
14a81 68 79 73 69 63 61 6c 20 65 6e 64 20 6f 66 20 74  hysical end of t
14a82 68 65 20 66 69 6c 65 2c 20 62 75 74 0a 20 20 20  he file, but.   
14a83 20 20 20 2a 2a 20 61 74 20 70 42 74 2d 3e 6e 54     ** at pBt->nT
14a84 72 75 6e 63 2e 20 0a 20 20 20 20 20 20 2a 2f 0a  runc. .      */.
14a85 20 20 20 20 20 20 2a 70 50 67 6e 6f 20 3d 20 70        *pPgno = p
14a86 42 74 2d 3e 6e 54 72 75 6e 63 2b 31 3b 0a 20 20  Bt->nTrunc+1;.  
14a87 20 20 20 20 69 66 28 20 2a 70 50 67 6e 6f 3d 3d      if( *pPgno==
14a88 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
14a89 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20 20  E(pBt) ){.      
14a8a 20 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b 0a 20 20    (*pPgno)++;.  
14a8b 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
14a8c 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63  if( pBt->autoVac
14a8d 75 75 6d 20 26 26 20 50 54 52 4d 41 50 5f 49 53  uum && PTRMAP_IS
14a8e 50 41 47 45 28 70 42 74 2c 20 2a 70 50 67 6e 6f  PAGE(pBt, *pPgno
14a8f 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66  ) ){.      /* If
14a90 20 2a 70 50 67 6e 6f 20 72 65 66 65 72 73 20 74   *pPgno refers t
14a91 6f 20 61 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20  o a pointer-map 
14a92 70 61 67 65 2c 20 61 6c 6c 6f 63 61 74 65 20 74  page, allocate t
14a93 77 6f 20 6e 65 77 20 70 61 67 65 73 0a 20 20 20  wo new pages.   
14a94 20 20 20 2a 2a 20 61 74 20 74 68 65 20 65 6e 64     ** at the end
14a95 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e 73   of the file ins
14a96 74 65 61 64 20 6f 66 20 6f 6e 65 2e 20 54 68 65  tead of one. The
14a97 20 66 69 72 73 74 20 61 6c 6c 6f 63 61 74 65 64   first allocated
14a98 20 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 62   page.      ** b
14a99 65 63 6f 6d 65 73 20 61 20 6e 65 77 20 70 6f 69  ecomes a new poi
14a9a 6e 74 65 72 2d 6d 61 70 20 70 61 67 65 2c 20 74  nter-map page, t
14a9b 68 65 20 73 65 63 6f 6e 64 20 69 73 20 75 73 65  he second is use
14a9c 64 20 62 79 20 74 68 65 20 63 61 6c 6c 65 72 2e  d by the caller.
14a9d 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
14a9e 54 52 41 43 45 28 28 22 41 4c 4c 4f 43 41 54 45  TRACE(("ALLOCATE
14a9f 3a 20 25 64 20 66 72 6f 6d 20 65 6e 64 20 6f 66  : %d from end of
14aa0 20 66 69 6c 65 20 28 70 6f 69 6e 74 65 72 2d 6d   file (pointer-m
14aa1 61 70 20 70 61 67 65 29 5c 6e 22 2c 20 2a 70 50  ap page)\n", *pP
14aa2 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 61 73 73  gno));.      ass
14aa3 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45 4e  ert( *pPgno!=PEN
14aa4 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
14aa5 42 74 29 20 29 3b 0a 20 20 20 20 20 20 28 2a 70  Bt) );.      (*p
14aa6 50 67 6e 6f 29 2b 2b 3b 0a 20 20 20 20 20 20 69  Pgno)++;.      i
14aa7 66 28 20 2a 70 50 67 6e 6f 3d 3d 50 45 4e 44 49  f( *pPgno==PENDI
14aa8 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70 42 74  NG_BYTE_PAGE(pBt
14aa9 29 20 29 7b 20 28 2a 70 50 67 6e 6f 29 2b 2b 3b  ) ){ (*pPgno)++;
14aaa 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
14aab 20 70 42 74 2d 3e 6e 54 72 75 6e 63 20 29 7b 0a   pBt->nTrunc ){.
14aac 20 20 20 20 20 20 70 42 74 2d 3e 6e 54 72 75 6e        pBt->nTrun
14aad 63 20 3d 20 2a 70 50 67 6e 6f 3b 0a 20 20 20 20  c = *pPgno;.    
14aae 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 61 73  }.#endif..    as
14aaf 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d 50 45  sert( *pPgno!=PE
14ab0 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28  NDING_BYTE_PAGE(
14ab1 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63 20 3d  pBt) );.    rc =
14ab2 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
14ab3 50 61 67 65 28 70 42 74 2c 20 2a 70 50 67 6e 6f  Page(pBt, *pPgno
14ab4 2c 20 70 70 50 61 67 65 2c 20 30 29 3b 0a 20 20  , ppPage, 0);.  
14ab5 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72    if( rc ) retur
14ab6 6e 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73  n rc;.    rc = s
14ab7 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74 65  qlite3PagerWrite
14ab8 28 28 2a 70 70 50 61 67 65 29 2d 3e 70 44 62 50  ((*ppPage)->pDbP
14ab9 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63  age);.    if( rc
14aba 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
14abb 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
14abc 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20 20 7d  (*ppPage);.    }
14abd 0a 20 20 20 20 54 52 41 43 45 28 28 22 41 4c 4c  .    TRACE(("ALL
14abe 4f 43 41 54 45 3a 20 25 64 20 66 72 6f 6d 20 65  OCATE: %d from e
14abf 6e 64 20 6f 66 20 66 69 6c 65 5c 6e 22 2c 20 2a  nd of file\n", *
14ac0 70 50 67 6e 6f 29 29 3b 0a 20 20 7d 0a 0a 20 20  pPgno));.  }..  
14ac1 61 73 73 65 72 74 28 20 2a 70 50 67 6e 6f 21 3d  assert( *pPgno!=
14ac2 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41 47  PENDING_BYTE_PAG
14ac3 45 28 70 42 74 29 20 29 3b 0a 0a 65 6e 64 5f 61  E(pBt) );..end_a
14ac4 6c 6c 6f 63 61 74 65 5f 70 61 67 65 3a 0a 20 20  llocate_page:.  
14ac5 72 65 6c 65 61 73 65 50 61 67 65 28 70 54 72 75  releasePage(pTru
14ac6 6e 6b 29 3b 0a 20 20 72 65 6c 65 61 73 65 50 61  nk);.  releasePa
14ac7 67 65 28 70 50 72 65 76 54 72 75 6e 6b 29 3b 0a  ge(pPrevTrunk);.
14ac8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
14ac9 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 73  _OK ){.    if( s
14aca 71 6c 69 74 65 33 50 61 67 65 72 50 61 67 65 52  qlite3PagerPageR
14acb 65 66 63 6f 75 6e 74 28 28 2a 70 70 50 61 67 65  efcount((*ppPage
14acc 29 2d 3e 70 44 62 50 61 67 65 29 3e 31 20 29 7b  )->pDbPage)>1 ){
14acd 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50 61  .      releasePa
14ace 67 65 28 2a 70 70 50 61 67 65 29 3b 0a 20 20 20  ge(*ppPage);.   
14acf 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
14ad0 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20  _CORRUPT_BKPT;. 
14ad1 20 20 20 7d 0a 20 20 20 20 28 2a 70 70 50 61 67     }.    (*ppPag
14ad2 65 29 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a  e)->isInit = 0;.
14ad3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
14ad4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
14ad5 70 61 67 65 20 6f 66 20 74 68 65 20 64 61 74 61  page of the data
14ad6 62 61 73 65 20 66 69 6c 65 20 74 6f 20 74 68 65  base file to the
14ad7 20 66 72 65 65 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a   freelist..**.**
14ad8 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72   sqlite3PagerUnr
14ad9 65 66 28 29 20 69 73 20 4e 4f 54 20 63 61 6c 6c  ef() is NOT call
14ada 65 64 20 66 6f 72 20 70 50 61 67 65 2e 0a 2a 2f  ed for pPage..*/
14adb 0a 73 74 61 74 69 63 20 69 6e 74 20 66 72 65 65  .static int free
14adc 50 61 67 65 28 4d 65 6d 50 61 67 65 20 2a 70 50  Page(MemPage *pP
14add 61 67 65 29 7b 0a 20 20 42 74 53 68 61 72 65 64  age){.  BtShared
14ade 20 2a 70 42 74 20 3d 20 70 50 61 67 65 2d 3e 70   *pBt = pPage->p
14adf 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
14ae0 50 61 67 65 31 20 3d 20 70 42 74 2d 3e 70 50 61  Page1 = pBt->pPa
14ae1 67 65 31 3b 0a 20 20 69 6e 74 20 72 63 2c 20 6e  ge1;.  int rc, n
14ae2 2c 20 6b 3b 0a 0a 20 20 2f 2a 20 50 72 65 70 61  , k;..  /* Prepa
14ae3 72 65 20 74 68 65 20 70 61 67 65 20 66 6f 72 20  re the page for 
14ae4 66 72 65 65 69 6e 67 20 2a 2f 0a 20 20 61 73 73  freeing */.  ass
14ae5 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
14ae6 65 78 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70  ex_held(pPage->p
14ae7 42 74 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  Bt->mutex) );.  
14ae8 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70  assert( pPage->p
14ae9 67 6e 6f 3e 31 20 29 3b 0a 20 20 70 50 61 67 65  gno>1 );.  pPage
14aea 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 0a 20  ->isInit = 0;.. 
14aeb 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68   /* Increment th
14aec 65 20 66 72 65 65 20 70 61 67 65 20 63 6f 75 6e  e free page coun
14aed 74 20 6f 6e 20 70 50 61 67 65 31 20 2a 2f 0a 20  t on pPage1 */. 
14aee 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
14aef 65 72 57 72 69 74 65 28 70 50 61 67 65 31 2d 3e  erWrite(pPage1->
14af0 70 44 62 50 61 67 65 29 3b 0a 20 20 69 66 28 20  pDbPage);.  if( 
14af1 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
14af2 20 20 6e 20 3d 20 67 65 74 34 62 79 74 65 28 26    n = get4byte(&
14af3 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
14af4 5d 29 3b 0a 20 20 70 75 74 34 62 79 74 65 28 26  ]);.  put4byte(&
14af5 70 50 61 67 65 31 2d 3e 61 44 61 74 61 5b 33 36  pPage1->aData[36
14af6 5d 2c 20 6e 2b 31 29 3b 0a 0a 23 69 66 64 65 66  ], n+1);..#ifdef
14af7 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44   SQLITE_SECURE_D
14af8 45 4c 45 54 45 0a 20 20 2f 2a 20 49 66 20 74 68  ELETE.  /* If th
14af9 65 20 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f  e SQLITE_SECURE_
14afa 44 45 4c 45 54 45 20 63 6f 6d 70 69 6c 65 2d 74  DELETE compile-t
14afb 69 6d 65 20 6f 70 74 69 6f 6e 20 69 73 20 65 6e  ime option is en
14afc 61 62 6c 65 64 2c 20 74 68 65 6e 0a 20 20 2a 2a  abled, then.  **
14afd 20 61 6c 77 61 79 73 20 66 75 6c 6c 79 20 6f 76   always fully ov
14afe 65 72 77 72 69 74 65 20 64 65 6c 65 74 65 64 20  erwrite deleted 
14aff 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 69 74 68  information with
14b00 20 7a 65 72 6f 73 2e 0a 20 20 2a 2f 0a 20 20 72   zeros..  */.  r
14b01 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
14b02 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
14b03 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72 63 20  Page);.  if( rc 
14b04 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 6d  ) return rc;.  m
14b05 65 6d 73 65 74 28 70 50 61 67 65 2d 3e 61 44 61  emset(pPage->aDa
14b06 74 61 2c 20 30 2c 20 70 50 61 67 65 2d 3e 70 42  ta, 0, pPage->pB
14b07 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b 0a 23 65  t->pageSize);.#e
14b08 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 74 68  ndif..  /* If th
14b09 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70 6f  e database suppo
14b0a 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 2c  rts auto-vacuum,
14b0b 20 77 72 69 74 65 20 61 6e 20 65 6e 74 72 79 20   write an entry 
14b0c 69 6e 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d  in the pointer-m
14b0d 61 70 0a 20 20 2a 2a 20 74 6f 20 69 6e 64 69 63  ap.  ** to indic
14b0e 61 74 65 20 74 68 61 74 20 74 68 65 20 70 61 67  ate that the pag
14b0f 65 20 69 73 20 66 72 65 65 2e 0a 20 20 2a 2f 0a  e is free..  */.
14b10 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
14b11 55 4d 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 70  UM ){.    rc = p
14b12 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 70 50  trmapPut(pBt, pP
14b13 61 67 65 2d 3e 70 67 6e 6f 2c 20 50 54 52 4d 41  age->pgno, PTRMA
14b14 50 5f 46 52 45 45 50 41 47 45 2c 20 30 29 3b 0a  P_FREEPAGE, 0);.
14b15 20 20 20 20 69 66 28 20 72 63 20 29 20 72 65 74      if( rc ) ret
14b16 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 69  urn rc;.  }..  i
14b17 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  f( n==0 ){.    /
14b18 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 66 69  * This is the fi
14b19 72 73 74 20 66 72 65 65 20 70 61 67 65 20 2a 2f  rst free page */
14b1a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
14b1b 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61 67  3PagerWrite(pPag
14b1c 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  e->pDbPage);.   
14b1d 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
14b1e 20 72 63 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28   rc;.    memset(
14b1f 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20 30 2c  pPage->aData, 0,
14b20 20 38 29 3b 0a 20 20 20 20 70 75 74 34 62 79 74   8);.    put4byt
14b21 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61 74 61  e(&pPage1->aData
14b22 5b 33 32 5d 2c 20 70 50 61 67 65 2d 3e 70 67 6e  [32], pPage->pgn
14b23 6f 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22  o);.    TRACE(("
14b24 46 52 45 45 2d 50 41 47 45 3a 20 25 64 20 66 69  FREE-PAGE: %d fi
14b25 72 73 74 5c 6e 22 2c 20 70 50 61 67 65 2d 3e 70  rst\n", pPage->p
14b26 67 6e 6f 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  gno));.  }else{.
14b27 20 20 20 20 2f 2a 20 4f 74 68 65 72 20 66 72 65      /* Other fre
14b28 65 20 70 61 67 65 73 20 61 6c 72 65 61 64 79 20  e pages already 
14b29 65 78 69 73 74 2e 20 20 52 65 74 72 69 76 65 20  exist.  Retrive 
14b2a 74 68 65 20 66 69 72 73 74 20 74 72 75 6e 6b 20  the first trunk 
14b2b 70 61 67 65 0a 20 20 20 20 2a 2a 20 6f 66 20 74  page.    ** of t
14b2c 68 65 20 66 72 65 65 6c 69 73 74 20 61 6e 64 20  he freelist and 
14b2d 66 69 6e 64 20 6f 75 74 20 68 6f 77 20 6d 61 6e  find out how man
14b2e 79 20 6c 65 61 76 65 73 20 69 74 20 68 61 73 2e  y leaves it has.
14b2f 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67 65 20   */.    MemPage 
14b30 2a 70 54 72 75 6e 6b 3b 0a 20 20 20 20 72 63 20  *pTrunk;.    rc 
14b31 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
14b32 74 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62  tPage(pBt, get4b
14b33 79 74 65 28 26 70 50 61 67 65 31 2d 3e 61 44 61  yte(&pPage1->aDa
14b34 74 61 5b 33 32 5d 29 2c 20 26 70 54 72 75 6e 6b  ta[32]), &pTrunk
14b35 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , 0);.    if( rc
14b36 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
14b37 20 20 6b 20 3d 20 67 65 74 34 62 79 74 65 28 26    k = get4byte(&
14b38 70 54 72 75 6e 6b 2d 3e 61 44 61 74 61 5b 34 5d  pTrunk->aData[4]
14b39 29 3b 0a 20 20 20 20 69 66 28 20 6b 3e 3d 70 42  );.    if( k>=pB
14b3a 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 20  t->usableSize/4 
14b3b 2d 20 38 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  - 8 ){.      /* 
14b3c 54 68 65 20 74 72 75 6e 6b 20 69 73 20 66 75 6c  The trunk is ful
14b3d 6c 2e 20 20 54 75 72 6e 20 74 68 65 20 70 61 67  l.  Turn the pag
14b3e 65 20 62 65 69 6e 67 20 66 72 65 65 64 20 69 6e  e being freed in
14b3f 74 6f 20 61 20 6e 65 77 0a 20 20 20 20 20 20 2a  to a new.      *
14b40 2a 20 74 72 75 6e 6b 20 70 61 67 65 20 77 69 74  * trunk page wit
14b41 68 20 6e 6f 20 6c 65 61 76 65 73 2e 0a 20 20 20  h no leaves..   
14b42 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e     **.      ** N
14b43 6f 74 65 20 74 68 61 74 20 74 68 65 20 74 72 75  ote that the tru
14b44 6e 6b 20 70 61 67 65 20 69 73 20 6e 6f 74 20 72  nk page is not r
14b45 65 61 6c 6c 79 20 66 75 6c 6c 20 75 6e 74 69 6c  eally full until
14b46 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a 20 20 20   it contains.   
14b47 20 20 20 2a 2a 20 75 73 61 62 6c 65 53 69 7a 65     ** usableSize
14b48 2f 34 20 2d 20 32 20 65 6e 74 72 69 65 73 2c 20  /4 - 2 entries, 
14b49 6e 6f 74 20 75 73 61 62 6c 65 53 69 7a 65 2f 34  not usableSize/4
14b4a 20 2d 20 38 20 65 6e 74 72 69 65 73 20 61 73 20   - 8 entries as 
14b4b 77 65 20 68 61 76 65 0a 20 20 20 20 20 20 2a 2a  we have.      **
14b4c 20 63 6f 64 65 64 2e 20 20 42 75 74 20 64 75 65   coded.  But due
14b4d 20 74 6f 20 61 20 63 6f 64 69 6e 67 20 65 72 72   to a coding err
14b4e 6f 72 20 69 6e 20 76 65 72 73 69 6f 6e 73 20 6f  or in versions o
14b4f 66 20 53 51 4c 69 74 65 20 70 72 69 6f 72 20 74  f SQLite prior t
14b50 6f 0a 20 20 20 20 20 20 2a 2a 20 33 2e 36 2e 30  o.      ** 3.6.0
14b51 2c 20 64 61 74 61 62 61 73 65 73 20 77 69 74 68  , databases with
14b52 20 66 72 65 65 6c 69 73 74 20 74 72 75 6e 6b 20   freelist trunk 
14b53 70 61 67 65 73 20 68 6f 6c 64 69 6e 67 20 6d 6f  pages holding mo
14b54 72 65 20 74 68 61 6e 0a 20 20 20 20 20 20 2a 2a  re than.      **
14b55 20 75 73 61 62 6c 65 53 69 7a 65 2f 34 20 2d 20   usableSize/4 - 
14b56 38 20 65 6e 74 72 69 65 73 20 77 69 6c 6c 20 62  8 entries will b
14b57 65 20 72 65 70 6f 72 74 65 64 20 61 73 20 63 6f  e reported as co
14b58 72 72 75 70 74 2e 20 20 49 6e 20 6f 72 64 65 72  rrupt.  In order
14b59 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 6d 61 69  .      ** to mai
14b5a 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64 73 20  ntain backwards 
14b5b 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 77 69  compatibility wi
14b5c 74 68 20 6f 6c 64 65 72 20 76 65 72 73 69 6f 6e  th older version
14b5d 73 20 6f 66 20 53 51 4c 69 74 65 2c 0a 20 20 20  s of SQLite,.   
14b5e 20 20 20 2a 2a 20 77 65 20 77 69 6c 6c 20 63 6f     ** we will co
14b5f 6e 74 61 69 6e 20 74 6f 20 72 65 73 74 72 69 63  ntain to restric
14b60 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  t the number of 
14b61 65 6e 74 72 69 65 73 20 74 6f 20 75 73 61 62 6c  entries to usabl
14b62 65 53 69 7a 65 2f 34 20 2d 20 38 0a 20 20 20 20  eSize/4 - 8.    
14b63 20 20 2a 2a 20 66 6f 72 20 6e 6f 77 2e 20 20 41    ** for now.  A
14b64 74 20 73 6f 6d 65 20 70 6f 69 6e 74 20 69 6e 20  t some point in 
14b65 74 68 65 20 66 75 74 75 72 65 20 28 6f 6e 63 65  the future (once
14b66 20 65 76 65 72 79 6f 6e 65 20 68 61 73 20 75 70   everyone has up
14b67 67 72 61 64 65 64 0a 20 20 20 20 20 20 2a 2a 20  graded.      ** 
14b68 74 6f 20 33 2e 36 2e 30 20 6f 72 20 6c 61 74 65  to 3.6.0 or late
14b69 72 29 20 77 65 20 73 68 6f 75 6c 64 20 63 6f 6e  r) we should con
14b6a 73 69 64 65 72 20 66 69 78 69 6e 67 20 74 68 65  sider fixing the
14b6b 20 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 61 62 6f   conditional abo
14b6c 76 65 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 72  ve.      ** to r
14b6d 65 61 64 20 22 75 73 61 62 6c 65 53 69 7a 65 2f  ead "usableSize/
14b6e 34 2d 32 22 20 69 6e 73 74 65 61 64 20 6f 66 20  4-2" instead of 
14b6f 22 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 38 22  "usableSize/4-8"
14b70 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
14b71 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
14b72 65 72 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  erWrite(pPage->p
14b73 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20 69  DbPage);.      i
14b74 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
14b75 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75 74 34   ){.        put4
14b76 62 79 74 65 28 70 50 61 67 65 2d 3e 61 44 61 74  byte(pPage->aDat
14b77 61 2c 20 70 54 72 75 6e 6b 2d 3e 70 67 6e 6f 29  a, pTrunk->pgno)
14b78 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
14b79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61 74 61  te(&pPage->aData
14b7a 5b 34 5d 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  [4], 0);.       
14b7b 20 70 75 74 34 62 79 74 65 28 26 70 50 61 67 65   put4byte(&pPage
14b7c 31 2d 3e 61 44 61 74 61 5b 33 32 5d 2c 20 70 50  1->aData[32], pP
14b7d 61 67 65 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20  age->pgno);.    
14b7e 20 20 20 20 54 52 41 43 45 28 28 22 46 52 45 45      TRACE(("FREE
14b7f 2d 50 41 47 45 3a 20 25 64 20 6e 65 77 20 74 72  -PAGE: %d new tr
14b80 75 6e 6b 20 70 61 67 65 20 72 65 70 6c 61 63 69  unk page replaci
14b81 6e 67 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  ng %d\n",.      
14b82 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65 2d            pPage-
14b83 3e 70 67 6e 6f 2c 20 70 54 72 75 6e 6b 2d 3e 70  >pgno, pTrunk->p
14b84 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20  gno));.      }. 
14b85 20 20 20 7d 65 6c 73 65 20 69 66 28 20 6b 3c 30     }else if( k<0
14b86 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   ){.      rc = S
14b87 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 20  QLITE_CORRUPT;. 
14b88 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
14b89 2f 2a 20 41 64 64 20 74 68 65 20 6e 65 77 6c 79  /* Add the newly
14b8a 20 66 72 65 65 64 20 70 61 67 65 20 61 73 20 61   freed page as a
14b8b 20 6c 65 61 66 20 6f 6e 20 74 68 65 20 63 75 72   leaf on the cur
14b8c 72 65 6e 74 20 74 72 75 6e 6b 20 2a 2f 0a 20 20  rent trunk */.  
14b8d 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
14b8e 50 61 67 65 72 57 72 69 74 65 28 70 54 72 75 6e  PagerWrite(pTrun
14b8f 6b 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20 20  k->pDbPage);.   
14b90 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
14b91 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
14b92 70 75 74 34 62 79 74 65 28 26 70 54 72 75 6e 6b  put4byte(&pTrunk
14b93 2d 3e 61 44 61 74 61 5b 34 5d 2c 20 6b 2b 31 29  ->aData[4], k+1)
14b94 3b 0a 20 20 20 20 20 20 20 20 70 75 74 34 62 79  ;.        put4by
14b95 74 65 28 26 70 54 72 75 6e 6b 2d 3e 61 44 61 74  te(&pTrunk->aDat
14b96 61 5b 38 2b 6b 2a 34 5d 2c 20 70 50 61 67 65 2d  a[8+k*4], pPage-
14b97 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64 65 66 20  >pgno);.#ifndef 
14b98 53 51 4c 49 54 45 5f 53 45 43 55 52 45 5f 44 45  SQLITE_SECURE_DE
14b99 4c 45 54 45 0a 20 20 20 20 20 20 20 20 72 63 20  LETE.        rc 
14b9a 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f  = sqlite3PagerDo
14b9b 6e 74 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70  ntWrite(pPage->p
14b9c 44 62 50 61 67 65 29 3b 0a 23 65 6e 64 69 66 0a  DbPage);.#endif.
14b9d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 54 52        }.      TR
14b9e 41 43 45 28 28 22 46 52 45 45 2d 50 41 47 45 3a  ACE(("FREE-PAGE:
14b9f 20 25 64 20 6c 65 61 66 20 6f 6e 20 74 72 75 6e   %d leaf on trun
14ba0 6b 20 70 61 67 65 20 25 64 5c 6e 22 2c 70 50 61  k page %d\n",pPa
14ba1 67 65 2d 3e 70 67 6e 6f 2c 70 54 72 75 6e 6b 2d  ge->pgno,pTrunk-
14ba2 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 7d 0a 20  >pgno));.    }. 
14ba3 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
14ba4 54 72 75 6e 6b 29 3b 0a 20 20 7d 0a 20 20 72 65  Trunk);.  }.  re
14ba5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
14ba6 2a 20 46 72 65 65 20 61 6e 79 20 6f 76 65 72 66  * Free any overf
14ba7 6c 6f 77 20 70 61 67 65 73 20 61 73 73 6f 63 69  low pages associ
14ba8 61 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69  ated with the gi
14ba9 76 65 6e 20 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  ven Cell..*/.sta
14baa 74 69 63 20 69 6e 74 20 63 6c 65 61 72 43 65 6c  tic int clearCel
14bab 6c 28 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65  l(MemPage *pPage
14bac 2c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  , unsigned char 
14bad 2a 70 43 65 6c 6c 29 7b 0a 20 20 42 74 53 68 61  *pCell){.  BtSha
14bae 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61 67 65  red *pBt = pPage
14baf 2d 3e 70 42 74 3b 0a 20 20 43 65 6c 6c 49 6e 66  ->pBt;.  CellInf
14bb0 6f 20 69 6e 66 6f 3b 0a 20 20 50 67 6e 6f 20 6f  o info;.  Pgno o
14bb1 76 66 6c 50 67 6e 6f 3b 0a 20 20 69 6e 74 20 72  vflPgno;.  int r
14bb2 63 3b 0a 20 20 69 6e 74 20 6e 4f 76 66 6c 3b 0a  c;.  int nOvfl;.
14bb3 20 20 69 6e 74 20 6f 76 66 6c 50 61 67 65 53 69    int ovflPageSi
14bb4 7a 65 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  ze;..  assert( s
14bb5 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14bb6 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
14bb7 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65  tex) );.  sqlite
14bb8 33 42 74 72 65 65 50 61 72 73 65 43 65 6c 6c 50  3BtreeParseCellP
14bb9 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c  tr(pPage, pCell,
14bba 20 26 69 6e 66 6f 29 3b 0a 20 20 69 66 28 20 69   &info);.  if( i
14bbb 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 3d 3d 30  nfo.iOverflow==0
14bbc 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
14bbd 51 4c 49 54 45 5f 4f 4b 3b 20 20 2f 2a 20 4e 6f  QLITE_OK;  /* No
14bbe 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e   overflow pages.
14bbf 20 52 65 74 75 72 6e 20 77 69 74 68 6f 75 74 20   Return without 
14bc0 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20 2a  doing anything *
14bc1 2f 0a 20 20 7d 0a 20 20 6f 76 66 6c 50 67 6e 6f  /.  }.  ovflPgno
14bc2 20 3d 20 67 65 74 34 62 79 74 65 28 26 70 43 65   = get4byte(&pCe
14bc3 6c 6c 5b 69 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f  ll[info.iOverflo
14bc4 77 5d 29 3b 0a 20 20 6f 76 66 6c 50 61 67 65 53  w]);.  ovflPageS
14bc5 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c  ize = pBt->usabl
14bc6 65 53 69 7a 65 20 2d 20 34 3b 0a 20 20 6e 4f 76  eSize - 4;.  nOv
14bc7 66 6c 20 3d 20 28 69 6e 66 6f 2e 6e 50 61 79 6c  fl = (info.nPayl
14bc8 6f 61 64 20 2d 20 69 6e 66 6f 2e 6e 4c 6f 63 61  oad - info.nLoca
14bc9 6c 20 2b 20 6f 76 66 6c 50 61 67 65 53 69 7a 65  l + ovflPageSize
14bca 20 2d 20 31 29 2f 6f 76 66 6c 50 61 67 65 53 69   - 1)/ovflPageSi
14bcb 7a 65 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 76  ze;.  assert( ov
14bcc 66 6c 50 67 6e 6f 3d 3d 30 20 7c 7c 20 6e 4f 76  flPgno==0 || nOv
14bcd 66 6c 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  fl>0 );.  while(
14bce 20 6e 4f 76 66 6c 2d 2d 20 29 7b 0a 20 20 20 20   nOvfl-- ){.    
14bcf 4d 65 6d 50 61 67 65 20 2a 70 4f 76 66 6c 3b 0a  MemPage *pOvfl;.
14bd0 20 20 20 20 69 66 28 20 6f 76 66 6c 50 67 6e 6f      if( ovflPgno
14bd1 3d 3d 30 20 7c 7c 20 6f 76 66 6c 50 67 6e 6f 3e  ==0 || ovflPgno>
14bd2 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
14bd3 42 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  Bt) ){.      ret
14bd4 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52 55  urn SQLITE_CORRU
14bd5 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20 7d 0a 0a  PT_BKPT;.    }..
14bd6 20 20 20 20 72 63 20 3d 20 67 65 74 4f 76 65 72      rc = getOver
14bd7 66 6c 6f 77 50 61 67 65 28 70 42 74 2c 20 6f 76  flowPage(pBt, ov
14bd8 66 6c 50 67 6e 6f 2c 20 26 70 4f 76 66 6c 2c 20  flPgno, &pOvfl, 
14bd9 28 6e 4f 76 66 6c 3d 3d 30 29 3f 30 3a 26 6f 76  (nOvfl==0)?0:&ov
14bda 66 6c 50 67 6e 6f 29 3b 0a 20 20 20 20 69 66 28  flPgno);.    if(
14bdb 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b   rc ) return rc;
14bdc 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61  .    rc = freePa
14bdd 67 65 28 70 4f 76 66 6c 29 3b 0a 20 20 20 20 73  ge(pOvfl);.    s
14bde 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65 66  qlite3PagerUnref
14bdf 28 70 4f 76 66 6c 2d 3e 70 44 62 50 61 67 65 29  (pOvfl->pDbPage)
14be0 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29 20 72  ;.    if( rc ) r
14be1 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20  eturn rc;.  }.  
14be2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
14be3 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74  ;.}../*.** Creat
14be4 65 20 74 68 65 20 62 79 74 65 20 73 65 71 75 65  e the byte seque
14be5 6e 63 65 20 75 73 65 64 20 74 6f 20 72 65 70 72  nce used to repr
14be6 65 73 65 6e 74 20 61 20 63 65 6c 6c 20 6f 6e 20  esent a cell on 
14be7 70 61 67 65 20 70 50 61 67 65 0a 2a 2a 20 61 6e  page pPage.** an
14be8 64 20 77 72 69 74 65 20 74 68 61 74 20 62 79 74  d write that byt
14be9 65 20 73 65 71 75 65 6e 63 65 20 69 6e 74 6f 20  e sequence into 
14bea 70 43 65 6c 6c 5b 5d 2e 20 20 4f 76 65 72 66 6c  pCell[].  Overfl
14beb 6f 77 20 70 61 67 65 73 20 61 72 65 0a 2a 2a 20  ow pages are.** 
14bec 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20 66 69  allocated and fi
14bed 6c 6c 65 64 20 69 6e 20 61 73 20 6e 65 63 65 73  lled in as neces
14bee 73 61 72 79 2e 20 20 54 68 65 20 63 61 6c 6c 69  sary.  The calli
14bef 6e 67 20 70 72 6f 63 65 64 75 72 65 0a 2a 2a 20  ng procedure.** 
14bf0 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
14bf1 6f 72 20 6d 61 6b 69 6e 67 20 73 75 72 65 20 73  or making sure s
14bf2 75 66 66 69 63 69 65 6e 74 20 73 70 61 63 65 20  ufficient space 
14bf3 68 61 73 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74  has been allocat
14bf4 65 64 0a 2a 2a 20 66 6f 72 20 70 43 65 6c 6c 5b  ed.** for pCell[
14bf5 5d 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68  ]..**.** Note th
14bf6 61 74 20 70 43 65 6c 6c 20 64 6f 65 73 20 6e 6f  at pCell does no
14bf7 74 20 6e 65 63 65 73 73 61 72 79 20 6e 65 65 64  t necessary need
14bf8 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 65   to point to the
14bf9 20 70 50 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a   pPage->aData.**
14bfa 20 61 72 65 61 2e 20 20 70 43 65 6c 6c 20 6d 69   area.  pCell mi
14bfb 67 68 74 20 70 6f 69 6e 74 20 74 6f 20 73 6f 6d  ght point to som
14bfc 65 20 74 65 6d 70 6f 72 61 72 79 20 73 74 6f 72  e temporary stor
14bfd 61 67 65 2e 20 20 54 68 65 20 63 65 6c 6c 20 77  age.  The cell w
14bfe 69 6c 6c 0a 2a 2a 20 62 65 20 63 6f 6e 73 74 72  ill.** be constr
14bff 75 63 74 65 64 20 69 6e 20 74 68 69 73 20 74 65  ucted in this te
14c00 6d 70 6f 72 61 72 79 20 61 72 65 61 20 74 68 65  mporary area the
14c01 6e 20 63 6f 70 69 65 64 20 69 6e 74 6f 20 70 50  n copied into pP
14c02 61 67 65 2d 3e 61 44 61 74 61 0a 2a 2a 20 6c 61  age->aData.** la
14c03 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ter..*/.static i
14c04 6e 74 20 66 69 6c 6c 49 6e 43 65 6c 6c 28 0a 20  nt fillInCell(. 
14c05 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 2c   MemPage *pPage,
14c06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c07 2f 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74  /* The page that
14c08 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 63 65   contains the ce
14c09 6c 6c 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  ll */.  unsigned
14c0a 20 63 68 61 72 20 2a 70 43 65 6c 6c 2c 20 20 20   char *pCell,   
14c0b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6d 70 6c 65         /* Comple
14c0c 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20 63  te text of the c
14c0d 65 6c 6c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ell */.  const v
14c0e 6f 69 64 20 2a 70 4b 65 79 2c 20 69 36 34 20 6e  oid *pKey, i64 n
14c0f 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68 65 20 6b  Key,    /* The k
14c10 65 79 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  ey */.  const vo
14c11 69 64 20 2a 70 44 61 74 61 2c 69 6e 74 20 6e 44  id *pData,int nD
14c12 61 74 61 2c 20 20 20 2f 2a 20 54 68 65 20 64 61  ata,   /* The da
14c13 74 61 20 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72  ta */.  int nZer
14c14 6f 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  o,              
14c15 20 20 20 20 20 20 20 2f 2a 20 45 78 74 72 61 20         /* Extra 
14c16 7a 65 72 6f 20 62 79 74 65 73 20 74 6f 20 61 70  zero bytes to ap
14c17 70 65 6e 64 20 74 6f 20 70 44 61 74 61 20 2a 2f  pend to pData */
14c18 0a 20 20 69 6e 74 20 2a 70 6e 53 69 7a 65 20 20  .  int *pnSize  
14c19 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14c1a 20 20 2f 2a 20 57 72 69 74 65 20 63 65 6c 6c 20    /* Write cell 
14c1b 73 69 7a 65 20 68 65 72 65 20 2a 2f 0a 29 7b 0a  size here */.){.
14c1c 20 20 69 6e 74 20 6e 50 61 79 6c 6f 61 64 3b 0a    int nPayload;.
14c1d 20 20 63 6f 6e 73 74 20 75 38 20 2a 70 53 72 63    const u8 *pSrc
14c1e 3b 0a 20 20 69 6e 74 20 6e 53 72 63 2c 20 6e 2c  ;.  int nSrc, n,
14c1f 20 72 63 3b 0a 20 20 69 6e 74 20 73 70 61 63 65   rc;.  int space
14c20 4c 65 66 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20  Left;.  MemPage 
14c21 2a 70 4f 76 66 6c 20 3d 20 30 3b 0a 20 20 4d 65  *pOvfl = 0;.  Me
14c22 6d 50 61 67 65 20 2a 70 54 6f 52 65 6c 65 61 73  mPage *pToReleas
14c23 65 20 3d 20 30 3b 0a 20 20 75 6e 73 69 67 6e 65  e = 0;.  unsigne
14c24 64 20 63 68 61 72 20 2a 70 50 72 69 6f 72 3b 0a  d char *pPrior;.
14c25 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
14c26 2a 70 50 61 79 6c 6f 61 64 3b 0a 20 20 42 74 53  *pPayload;.  BtS
14c27 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70 50 61  hared *pBt = pPa
14c28 67 65 2d 3e 70 42 74 3b 0a 20 20 50 67 6e 6f 20  ge->pBt;.  Pgno 
14c29 70 67 6e 6f 4f 76 66 6c 20 3d 20 30 3b 0a 20 20  pgnoOvfl = 0;.  
14c2a 69 6e 74 20 6e 48 65 61 64 65 72 3b 0a 20 20 43  int nHeader;.  C
14c2b 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 0a 20  ellInfo info;.. 
14c2c 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14c2d 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
14c2e 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
14c2f 3b 0a 0a 20 20 2f 2a 20 46 69 6c 6c 20 69 6e 20  ;..  /* Fill in 
14c30 74 68 65 20 68 65 61 64 65 72 2e 20 2a 2f 0a 20  the header. */. 
14c31 20 6e 48 65 61 64 65 72 20 3d 20 30 3b 0a 20 20   nHeader = 0;.  
14c32 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61 66  if( !pPage->leaf
14c33 20 29 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20   ){.    nHeader 
14c34 2b 3d 20 34 3b 0a 20 20 7d 0a 20 20 69 66 28 20  += 4;.  }.  if( 
14c35 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 20 29  pPage->hasData )
14c36 7b 0a 20 20 20 20 6e 48 65 61 64 65 72 20 2b 3d  {.    nHeader +=
14c37 20 70 75 74 56 61 72 69 6e 74 28 26 70 43 65 6c   putVarint(&pCel
14c38 6c 5b 6e 48 65 61 64 65 72 5d 2c 20 6e 44 61 74  l[nHeader], nDat
14c39 61 2b 6e 5a 65 72 6f 29 3b 0a 20 20 7d 65 6c 73  a+nZero);.  }els
14c3a 65 7b 0a 20 20 20 20 6e 44 61 74 61 20 3d 20 6e  e{.    nData = n
14c3b 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Zero = 0;.  }.  
14c3c 6e 48 65 61 64 65 72 20 2b 3d 20 70 75 74 56 61  nHeader += putVa
14c3d 72 69 6e 74 28 26 70 43 65 6c 6c 5b 6e 48 65 61  rint(&pCell[nHea
14c3e 64 65 72 5d 2c 20 2a 28 75 36 34 2a 29 26 6e 4b  der], *(u64*)&nK
14c3f 65 79 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74  ey);.  sqlite3Bt
14c40 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
14c41 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69  pPage, pCell, &i
14c42 6e 66 6f 29 3b 0a 20 20 61 73 73 65 72 74 28 20  nfo);.  assert( 
14c43 69 6e 66 6f 2e 6e 48 65 61 64 65 72 3d 3d 6e 48  info.nHeader==nH
14c44 65 61 64 65 72 20 29 3b 0a 20 20 61 73 73 65 72  eader );.  asser
14c45 74 28 20 69 6e 66 6f 2e 6e 4b 65 79 3d 3d 6e 4b  t( info.nKey==nK
14c46 65 79 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ey );.  assert( 
14c47 69 6e 66 6f 2e 6e 44 61 74 61 3d 3d 28 75 33 32  info.nData==(u32
14c48 29 28 6e 44 61 74 61 2b 6e 5a 65 72 6f 29 20 29  )(nData+nZero) )
14c49 3b 0a 20 20 0a 20 20 2f 2a 20 46 69 6c 6c 20 69  ;.  .  /* Fill i
14c4a 6e 20 74 68 65 20 70 61 79 6c 6f 61 64 20 2a 2f  n the payload */
14c4b 0a 20 20 6e 50 61 79 6c 6f 61 64 20 3d 20 6e 44  .  nPayload = nD
14c4c 61 74 61 20 2b 20 6e 5a 65 72 6f 3b 0a 20 20 69  ata + nZero;.  i
14c4d 66 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  f( pPage->intKey
14c4e 20 29 7b 0a 20 20 20 20 70 53 72 63 20 3d 20 70   ){.    pSrc = p
14c4f 44 61 74 61 3b 0a 20 20 20 20 6e 53 72 63 20 3d  Data;.    nSrc =
14c50 20 6e 44 61 74 61 3b 0a 20 20 20 20 6e 44 61 74   nData;.    nDat
14c51 61 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a  a = 0;.  }else{.
14c52 20 20 20 20 6e 50 61 79 6c 6f 61 64 20 2b 3d 20      nPayload += 
14c53 6e 4b 65 79 3b 0a 20 20 20 20 70 53 72 63 20 3d  nKey;.    pSrc =
14c54 20 70 4b 65 79 3b 0a 20 20 20 20 6e 53 72 63 20   pKey;.    nSrc 
14c55 3d 20 6e 4b 65 79 3b 0a 20 20 7d 0a 20 20 2a 70  = nKey;.  }.  *p
14c56 6e 53 69 7a 65 20 3d 20 69 6e 66 6f 2e 6e 53 69  nSize = info.nSi
14c57 7a 65 3b 0a 20 20 73 70 61 63 65 4c 65 66 74 20  ze;.  spaceLeft 
14c58 3d 20 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 3b 0a 20  = info.nLocal;. 
14c59 20 70 50 61 79 6c 6f 61 64 20 3d 20 26 70 43 65   pPayload = &pCe
14c5a 6c 6c 5b 6e 48 65 61 64 65 72 5d 3b 0a 20 20 70  ll[nHeader];.  p
14c5b 50 72 69 6f 72 20 3d 20 26 70 43 65 6c 6c 5b 69  Prior = &pCell[i
14c5c 6e 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 3b 0a  nfo.iOverflow];.
14c5d 0a 20 20 77 68 69 6c 65 28 20 6e 50 61 79 6c 6f  .  while( nPaylo
14c5e 61 64 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20  ad>0 ){.    if( 
14c5f 73 70 61 63 65 4c 65 66 74 3d 3d 30 20 29 7b 0a  spaceLeft==0 ){.
14c60 20 20 20 20 20 20 69 6e 74 20 69 73 45 78 61 63        int isExac
14c61 74 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53  t = 0;.#ifndef S
14c62 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56  QLITE_OMIT_AUTOV
14c63 41 43 55 55 4d 0a 20 20 20 20 20 20 50 67 6e 6f  ACUUM.      Pgno
14c64 20 70 67 6e 6f 50 74 72 6d 61 70 20 3d 20 70 67   pgnoPtrmap = pg
14c65 6e 6f 4f 76 66 6c 3b 20 2f 2a 20 4f 76 65 72 66  noOvfl; /* Overf
14c66 6c 6f 77 20 70 61 67 65 20 70 6f 69 6e 74 65 72  low page pointer
14c67 2d 6d 61 70 20 65 6e 74 72 79 20 70 61 67 65 20  -map entry page 
14c68 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 42 74  */.      if( pBt
14c69 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
14c6a 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20 20 20          do{.    
14c6b 20 20 20 20 20 20 70 67 6e 6f 4f 76 66 6c 2b 2b        pgnoOvfl++
14c6c 3b 0a 20 20 20 20 20 20 20 20 7d 20 77 68 69 6c  ;.        } whil
14c6d 65 28 20 0a 20 20 20 20 20 20 20 20 20 20 50 54  e( .          PT
14c6e 52 4d 41 50 5f 49 53 50 41 47 45 28 70 42 74 2c  RMAP_ISPAGE(pBt,
14c6f 20 70 67 6e 6f 4f 76 66 6c 29 20 7c 7c 20 70 67   pgnoOvfl) || pg
14c70 6e 6f 4f 76 66 6c 3d 3d 50 45 4e 44 49 4e 47 5f  noOvfl==PENDING_
14c71 42 59 54 45 5f 50 41 47 45 28 70 42 74 29 20 0a  BYTE_PAGE(pBt) .
14c72 20 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20          );.     
14c73 20 20 20 69 66 28 20 70 67 6e 6f 4f 76 66 6c 3e     if( pgnoOvfl>
14c74 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f  1 ){.          /
14c75 2a 20 69 73 45 78 61 63 74 20 3d 20 31 3b 20 2a  * isExact = 1; *
14c76 2f 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  /.        }.    
14c77 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20    }.#endif.     
14c78 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74 65 42 74   rc = allocateBt
14c79 72 65 65 50 61 67 65 28 70 42 74 2c 20 26 70 4f  reePage(pBt, &pO
14c7a 76 66 6c 2c 20 26 70 67 6e 6f 4f 76 66 6c 2c 20  vfl, &pgnoOvfl, 
14c7b 70 67 6e 6f 4f 76 66 6c 2c 20 69 73 45 78 61 63  pgnoOvfl, isExac
14c7c 74 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  t);.#ifndef SQLI
14c7d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
14c7e 55 4d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74  UM.      /* If t
14c7f 68 65 20 64 61 74 61 62 61 73 65 20 73 75 70 70  he database supp
14c80 6f 72 74 73 20 61 75 74 6f 2d 76 61 63 75 75 6d  orts auto-vacuum
14c81 2c 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e 64  , and the second
14c82 20 6f 72 20 73 75 62 73 65 71 75 65 6e 74 0a 20   or subsequent. 
14c83 20 20 20 20 20 2a 2a 20 6f 76 65 72 66 6c 6f 77       ** overflow
14c84 20 70 61 67 65 20 69 73 20 62 65 69 6e 67 20 61   page is being a
14c85 6c 6c 6f 63 61 74 65 64 2c 20 61 64 64 20 61 6e  llocated, add an
14c86 20 65 6e 74 72 79 20 74 6f 20 74 68 65 20 70 6f   entry to the po
14c87 69 6e 74 65 72 2d 6d 61 70 0a 20 20 20 20 20 20  inter-map.      
14c88 2a 2a 20 66 6f 72 20 74 68 61 74 20 70 61 67 65  ** for that page
14c89 20 6e 6f 77 2e 20 0a 20 20 20 20 20 20 2a 2a 0a   now. .      **.
14c8a 20 20 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73        ** If this
14c8b 20 69 73 20 74 68 65 20 66 69 72 73 74 20 6f 76   is the first ov
14c8c 65 72 66 6c 6f 77 20 70 61 67 65 2c 20 74 68 65  erflow page, the
14c8d 6e 20 77 72 69 74 65 20 61 20 70 61 72 74 69 61  n write a partia
14c8e 6c 20 65 6e 74 72 79 20 0a 20 20 20 20 20 20 2a  l entry .      *
14c8f 2a 20 74 6f 20 74 68 65 20 70 6f 69 6e 74 65 72  * to the pointer
14c90 2d 6d 61 70 2e 20 49 66 20 77 65 20 77 72 69 74  -map. If we writ
14c91 65 20 6e 6f 74 68 69 6e 67 20 74 6f 20 74 68 69  e nothing to thi
14c92 73 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 73 6c  s pointer-map sl
14c93 6f 74 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  ot,.      ** the
14c94 6e 20 74 68 65 20 6f 70 74 69 6d 69 73 74 69 63  n the optimistic
14c95 20 6f 76 65 72 66 6c 6f 77 20 63 68 61 69 6e 20   overflow chain 
14c96 70 72 6f 63 65 73 73 69 6e 67 20 69 6e 20 63 6c  processing in cl
14c97 65 61 72 43 65 6c 6c 28 29 0a 20 20 20 20 20 20  earCell().      
14c98 2a 2a 20 6d 61 79 20 6d 69 73 69 6e 74 65 72 70  ** may misinterp
14c99 72 65 74 20 74 68 65 20 75 6e 69 6e 69 74 69 61  ret the uninitia
14c9a 6c 69 73 65 64 20 76 61 6c 75 65 73 20 61 6e 64  lised values and
14c9b 20 64 65 6c 65 74 65 20 74 68 65 0a 20 20 20 20   delete the.    
14c9c 20 20 2a 2a 20 77 72 6f 6e 67 20 70 61 67 65 73    ** wrong pages
14c9d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61   from the databa
14c9e 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  se..      */.   
14c9f 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f     if( pBt->auto
14ca0 56 61 63 75 75 6d 20 26 26 20 72 63 3d 3d 53 51  Vacuum && rc==SQ
14ca1 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
14ca2 20 20 20 75 38 20 65 54 79 70 65 20 3d 20 28 70     u8 eType = (p
14ca3 67 6e 6f 50 74 72 6d 61 70 3f 50 54 52 4d 41 50  gnoPtrmap?PTRMAP
14ca4 5f 4f 56 45 52 46 4c 4f 57 32 3a 50 54 52 4d 41  _OVERFLOW2:PTRMA
14ca5 50 5f 4f 56 45 52 46 4c 4f 57 31 29 3b 0a 20 20  P_OVERFLOW1);.  
14ca6 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
14ca7 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 4f 76  pPut(pBt, pgnoOv
14ca8 66 6c 2c 20 65 54 79 70 65 2c 20 70 67 6e 6f 50  fl, eType, pgnoP
14ca9 74 72 6d 61 70 29 3b 0a 20 20 20 20 20 20 20 20  trmap);.        
14caa 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
14cab 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
14cac 70 4f 76 66 6c 29 3b 0a 20 20 20 20 20 20 20 20  pOvfl);.        
14cad 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66  }.      }.#endif
14cae 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
14caf 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
14cb0 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73 65 29  Page(pToRelease)
14cb1 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
14cb2 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   rc;.      }.   
14cb3 20 20 20 70 75 74 34 62 79 74 65 28 70 50 72 69     put4byte(pPri
14cb4 6f 72 2c 20 70 67 6e 6f 4f 76 66 6c 29 3b 0a 20  or, pgnoOvfl);. 
14cb5 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
14cb6 28 70 54 6f 52 65 6c 65 61 73 65 29 3b 0a 20 20  (pToRelease);.  
14cb7 20 20 20 20 70 54 6f 52 65 6c 65 61 73 65 20 3d      pToRelease =
14cb8 20 70 4f 76 66 6c 3b 0a 20 20 20 20 20 20 70 50   pOvfl;.      pP
14cb9 72 69 6f 72 20 3d 20 70 4f 76 66 6c 2d 3e 61 44  rior = pOvfl->aD
14cba 61 74 61 3b 0a 20 20 20 20 20 20 70 75 74 34 62  ata;.      put4b
14cbb 79 74 65 28 70 50 72 69 6f 72 2c 20 30 29 3b 0a  yte(pPrior, 0);.
14cbc 20 20 20 20 20 20 70 50 61 79 6c 6f 61 64 20 3d        pPayload =
14cbd 20 26 70 4f 76 66 6c 2d 3e 61 44 61 74 61 5b 34   &pOvfl->aData[4
14cbe 5d 3b 0a 20 20 20 20 20 20 73 70 61 63 65 4c 65  ];.      spaceLe
14cbf 66 74 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65  ft = pBt->usable
14cc0 53 69 7a 65 20 2d 20 34 3b 0a 20 20 20 20 7d 0a  Size - 4;.    }.
14cc1 20 20 20 20 6e 20 3d 20 6e 50 61 79 6c 6f 61 64      n = nPayload
14cc2 3b 0a 20 20 20 20 69 66 28 20 6e 3e 73 70 61 63  ;.    if( n>spac
14cc3 65 4c 65 66 74 20 29 20 6e 20 3d 20 73 70 61 63  eLeft ) n = spac
14cc4 65 4c 65 66 74 3b 0a 20 20 20 20 69 66 28 20 6e  eLeft;.    if( n
14cc5 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 69  Src>0 ){.      i
14cc6 66 28 20 6e 3e 6e 53 72 63 20 29 20 6e 20 3d 20  f( n>nSrc ) n = 
14cc7 6e 53 72 63 3b 0a 20 20 20 20 20 20 61 73 73 65  nSrc;.      asse
14cc8 72 74 28 20 70 53 72 63 20 29 3b 0a 20 20 20 20  rt( pSrc );.    
14cc9 20 20 6d 65 6d 63 70 79 28 70 50 61 79 6c 6f 61    memcpy(pPayloa
14cca 64 2c 20 70 53 72 63 2c 20 6e 29 3b 0a 20 20 20  d, pSrc, n);.   
14ccb 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65   }else{.      me
14ccc 6d 73 65 74 28 70 50 61 79 6c 6f 61 64 2c 20 30  mset(pPayload, 0
14ccd 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  , n);.    }.    
14cce 6e 50 61 79 6c 6f 61 64 20 2d 3d 20 6e 3b 0a 20  nPayload -= n;. 
14ccf 20 20 20 70 50 61 79 6c 6f 61 64 20 2b 3d 20 6e     pPayload += n
14cd0 3b 0a 20 20 20 20 70 53 72 63 20 2b 3d 20 6e 3b  ;.    pSrc += n;
14cd1 0a 20 20 20 20 6e 53 72 63 20 2d 3d 20 6e 3b 0a  .    nSrc -= n;.
14cd2 20 20 20 20 73 70 61 63 65 4c 65 66 74 20 2d 3d      spaceLeft -=
14cd3 20 6e 3b 0a 20 20 20 20 69 66 28 20 6e 53 72 63   n;.    if( nSrc
14cd4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 53 72  ==0 ){.      nSr
14cd5 63 20 3d 20 6e 44 61 74 61 3b 0a 20 20 20 20 20  c = nData;.     
14cd6 20 70 53 72 63 20 3d 20 70 44 61 74 61 3b 0a 20   pSrc = pData;. 
14cd7 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 6c 65 61     }.  }.  relea
14cd8 73 65 50 61 67 65 28 70 54 6f 52 65 6c 65 61 73  sePage(pToReleas
14cd9 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  e);.  return SQL
14cda 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
14cdb 20 52 65 6d 6f 76 65 20 74 68 65 20 69 2d 74 68   Remove the i-th
14cdc 20 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65   cell from pPage
14cdd 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
14cde 65 66 66 65 63 74 73 20 70 50 61 67 65 20 6f 6e  effects pPage on
14cdf 6c 79 2e 0a 2a 2a 20 54 68 65 20 63 65 6c 6c 20  ly..** The cell 
14ce0 63 6f 6e 74 65 6e 74 20 69 73 20 6e 6f 74 20 66  content is not f
14ce1 72 65 65 64 20 6f 72 20 64 65 61 6c 6c 6f 63 61  reed or dealloca
14ce2 74 65 64 2e 20 20 49 74 20 69 73 20 61 73 73 75  ted.  It is assu
14ce3 6d 65 64 20 74 68 61 74 0a 2a 2a 20 74 68 65 20  med that.** the 
14ce4 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 68 61 73  cell content has
14ce5 20 62 65 65 6e 20 63 6f 70 69 65 64 20 73 6f 6d   been copied som
14ce6 65 70 6c 61 63 65 20 65 6c 73 65 2e 20 20 54 68  eplace else.  Th
14ce7 69 73 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 0a  is routine just.
14ce8 2a 2a 20 72 65 6d 6f 76 65 73 20 74 68 65 20 72  ** removes the r
14ce9 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
14cea 63 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e  cell from pPage.
14ceb 0a 2a 2a 0a 2a 2a 20 22 73 7a 22 20 6d 75 73 74  .**.** "sz" must
14cec 20 62 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   be the number o
14ced 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20 63  f bytes in the c
14cee 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ell..*/.static i
14cef 6e 74 20 64 72 6f 70 43 65 6c 6c 28 4d 65 6d 50  nt dropCell(MemP
14cf0 61 67 65 20 2a 70 50 61 67 65 2c 20 69 6e 74 20  age *pPage, int 
14cf1 69 64 78 2c 20 69 6e 74 20 73 7a 29 7b 0a 20 20  idx, int sz){.  
14cf2 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
14cf3 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
14cf4 2a 2f 0a 20 20 69 6e 74 20 70 63 3b 20 20 20 20  */.  int pc;    
14cf5 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74       /* Offset t
14cf6 6f 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20 6f  o cell content o
14cf7 66 20 63 65 6c 6c 20 62 65 69 6e 67 20 64 65 6c  f cell being del
14cf8 65 74 65 64 20 2a 2f 0a 20 20 75 38 20 2a 64 61  eted */.  u8 *da
14cf9 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 70 50 61  ta;       /* pPa
14cfa 67 65 2d 3e 61 44 61 74 61 20 2a 2f 0a 20 20 75  ge->aData */.  u
14cfb 38 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 2f  8 *ptr;        /
14cfc 2a 20 55 73 65 64 20 74 6f 20 6d 6f 76 65 20 62  * Used to move b
14cfd 79 74 65 73 20 61 72 6f 75 6e 64 20 77 69 74 68  ytes around with
14cfe 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69  in data[] */.  i
14cff 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20 20 2f  nt rc;         /
14d00 2a 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64  * The return cod
14d01 65 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  e */..  assert( 
14d02 69 64 78 3e 3d 30 20 26 26 20 69 64 78 3c 70 50  idx>=0 && idx<pP
14d03 61 67 65 2d 3e 6e 43 65 6c 6c 20 29 3b 0a 20 20  age->nCell );.  
14d04 61 73 73 65 72 74 28 20 73 7a 3d 3d 63 65 6c 6c  assert( sz==cell
14d05 53 69 7a 65 28 70 50 61 67 65 2c 20 69 64 78 29  Size(pPage, idx)
14d06 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
14d07 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
14d08 65 61 62 6c 65 28 70 50 61 67 65 2d 3e 70 44 62  eable(pPage->pDb
14d09 50 61 67 65 29 20 29 3b 0a 20 20 61 73 73 65 72  Page) );.  asser
14d0a 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
14d0b 5f 68 65 6c 64 28 70 50 61 67 65 2d 3e 70 42 74  _held(pPage->pBt
14d0c 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 64 61  ->mutex) );.  da
14d0d 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74  ta = pPage->aDat
14d0e 61 3b 0a 20 20 70 74 72 20 3d 20 26 64 61 74 61  a;.  ptr = &data
14d0f 5b 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73  [pPage->cellOffs
14d10 65 74 20 2b 20 32 2a 69 64 78 5d 3b 0a 20 20 70  et + 2*idx];.  p
14d11 63 20 3d 20 67 65 74 32 62 79 74 65 28 70 74 72  c = get2byte(ptr
14d12 29 3b 0a 20 20 69 66 20 28 20 28 70 63 3c 70 50  );.  if ( (pc<pP
14d13 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 2b 36  age->hdrOffset+6
14d14 2b 28 70 50 61 67 65 2d 3e 6c 65 61 66 3f 30 3a  +(pPage->leaf?0:
14d15 34 29 29 20 7c 7c 20 28 70 63 2b 73 7a 3e 70 50  4)) || (pc+sz>pP
14d16 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
14d17 53 69 7a 65 29 20 29 20 7b 0a 20 20 20 20 72 65  Size) ) {.    re
14d18 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
14d19 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 20 20  UPT_BKPT;.  }.  
14d1a 72 63 20 3d 20 66 72 65 65 53 70 61 63 65 28 70  rc = freeSpace(p
14d1b 50 61 67 65 2c 20 70 63 2c 20 73 7a 29 3b 0a 20  Page, pc, sz);. 
14d1c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
14d1d 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  OK ){.    return
14d1e 20 72 63 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69   rc;.  }.  for(i
14d1f 3d 69 64 78 2b 31 3b 20 69 3c 70 50 61 67 65 2d  =idx+1; i<pPage-
14d20 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 2c 20 70 74 72  >nCell; i++, ptr
14d21 2b 3d 32 29 7b 0a 20 20 20 20 70 74 72 5b 30 5d  +=2){.    ptr[0]
14d22 20 3d 20 70 74 72 5b 32 5d 3b 0a 20 20 20 20 70   = ptr[2];.    p
14d23 74 72 5b 31 5d 20 3d 20 70 74 72 5b 33 5d 3b 0a  tr[1] = ptr[3];.
14d24 20 20 7d 0a 20 20 70 50 61 67 65 2d 3e 6e 43 65    }.  pPage->nCe
14d25 6c 6c 2d 2d 3b 0a 20 20 70 75 74 32 62 79 74 65  ll--;.  put2byte
14d26 28 26 64 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  (&data[pPage->hd
14d27 72 4f 66 66 73 65 74 2b 33 5d 2c 20 70 50 61 67  rOffset+3], pPag
14d28 65 2d 3e 6e 43 65 6c 6c 29 3b 0a 20 20 70 50 61  e->nCell);.  pPa
14d29 67 65 2d 3e 6e 46 72 65 65 20 2b 3d 20 32 3b 0a  ge->nFree += 2;.
14d2a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14d2b 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73  OK;.}../*.** Ins
14d2c 65 72 74 20 61 20 6e 65 77 20 63 65 6c 6c 20 6f  ert a new cell o
14d2d 6e 20 70 50 61 67 65 20 61 74 20 63 65 6c 6c 20  n pPage at cell 
14d2e 69 6e 64 65 78 20 22 69 22 2e 20 20 70 43 65 6c  index "i".  pCel
14d2f 6c 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a  l points to the.
14d30 2a 2a 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68  ** content of th
14d31 65 20 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66  e cell..**.** If
14d32 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
14d33 74 20 77 69 6c 6c 20 66 69 74 20 6f 6e 20 74 68  t will fit on th
14d34 65 20 70 61 67 65 2c 20 74 68 65 6e 20 70 75 74  e page, then put
14d35 20 69 74 20 74 68 65 72 65 2e 20 20 49 66 20 69   it there.  If i
14d36 74 0a 2a 2a 20 77 69 6c 6c 20 6e 6f 74 20 66 69  t.** will not fi
14d37 74 2c 20 74 68 65 6e 20 6d 61 6b 65 20 61 20 63  t, then make a c
14d38 6f 70 79 20 6f 66 20 74 68 65 20 63 65 6c 6c 20  opy of the cell 
14d39 63 6f 6e 74 65 6e 74 20 69 6e 74 6f 20 70 54 65  content into pTe
14d3a 6d 70 20 69 66 0a 2a 2a 20 70 54 65 6d 70 20 69  mp if.** pTemp i
14d3b 73 20 6e 6f 74 20 6e 75 6c 6c 2e 20 20 52 65 67  s not null.  Reg
14d3c 61 72 64 6c 65 73 73 20 6f 66 20 70 54 65 6d 70  ardless of pTemp
14d3d 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  , allocate a new
14d3e 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 50 61   entry.** in pPa
14d3f 67 65 2d 3e 61 4f 76 66 6c 5b 5d 20 61 6e 64 20  ge->aOvfl[] and 
14d40 6d 61 6b 65 20 69 74 20 70 6f 69 6e 74 20 74 6f  make it point to
14d41 20 74 68 65 20 63 65 6c 6c 20 63 6f 6e 74 65 6e   the cell conten
14d42 74 20 28 65 69 74 68 65 72 0a 2a 2a 20 69 6e 20  t (either.** in 
14d43 70 54 65 6d 70 20 6f 72 20 74 68 65 20 6f 72 69  pTemp or the ori
14d44 67 69 6e 61 6c 20 70 43 65 6c 6c 29 20 61 6e 64  ginal pCell) and
14d45 20 61 6c 73 6f 20 72 65 63 6f 72 64 20 69 74 73   also record its
14d46 20 69 6e 64 65 78 2e 20 0a 2a 2a 20 41 6c 6c 6f   index. .** Allo
14d47 63 61 74 69 6e 67 20 61 20 6e 65 77 20 65 6e 74  cating a new ent
14d48 72 79 20 69 6e 20 70 50 61 67 65 2d 3e 61 43 65  ry in pPage->aCe
14d49 6c 6c 5b 5d 20 69 6d 70 6c 69 65 73 20 74 68 61  ll[] implies tha
14d4a 74 20 0a 2a 2a 20 70 50 61 67 65 2d 3e 6e 4f 76  t .** pPage->nOv
14d4b 65 72 66 6c 6f 77 20 69 73 20 69 6e 63 72 65 6d  erflow is increm
14d4c 65 6e 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ented..**.** If 
14d4d 6e 53 6b 69 70 20 69 73 20 6e 6f 6e 2d 7a 65 72  nSkip is non-zer
14d4e 6f 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 63  o, then do not c
14d4f 6f 70 79 20 74 68 65 20 66 69 72 73 74 20 6e 53  opy the first nS
14d50 6b 69 70 20 62 79 74 65 73 20 6f 66 20 74 68 65  kip bytes of the
14d51 0a 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20 63 61  .** cell. The ca
14d52 6c 6c 65 72 20 77 69 6c 6c 20 6f 76 65 72 77 72  ller will overwr
14d53 69 74 65 20 74 68 65 6d 20 61 66 74 65 72 20 74  ite them after t
14d54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  his function ret
14d55 75 72 6e 73 2e 20 49 66 0a 2a 2a 20 6e 53 6b 69  urns. If.** nSki
14d56 70 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  p is non-zero, t
14d57 68 65 6e 20 70 43 65 6c 6c 20 6d 61 79 20 6e 6f  hen pCell may no
14d58 74 20 70 6f 69 6e 74 20 74 6f 20 61 6e 20 69 6e  t point to an in
14d59 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 6c 6f 63  valid memory loc
14d5a 61 74 69 6f 6e 20 0a 2a 2a 20 28 62 75 74 20 70  ation .** (but p
14d5b 43 65 6c 6c 2b 6e 53 6b 69 70 20 69 73 20 61 6c  Cell+nSkip is al
14d5c 77 61 79 73 20 76 61 6c 69 64 29 2e 0a 2a 2f 0a  ways valid)..*/.
14d5d 73 74 61 74 69 63 20 69 6e 74 20 69 6e 73 65 72  static int inser
14d5e 74 43 65 6c 6c 28 0a 20 20 4d 65 6d 50 61 67 65  tCell(.  MemPage
14d5f 20 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 50 61   *pPage,   /* Pa
14d60 67 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  ge into which we
14d61 20 61 72 65 20 63 6f 70 79 69 6e 67 20 2a 2f 0a   are copying */.
14d62 20 20 69 6e 74 20 69 2c 20 20 20 20 20 20 20 20    int i,        
14d63 20 20 20 20 2f 2a 20 4e 65 77 20 63 65 6c 6c 20      /* New cell 
14d64 62 65 63 6f 6d 65 73 20 74 68 65 20 69 2d 74 68  becomes the i-th
14d65 20 63 65 6c 6c 20 6f 66 20 74 68 65 20 70 61 67   cell of the pag
14d66 65 20 2a 2f 0a 20 20 75 38 20 2a 70 43 65 6c 6c  e */.  u8 *pCell
14d67 2c 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74  ,        /* Cont
14d68 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77 20 63  ent of the new c
14d69 65 6c 6c 20 2a 2f 0a 20 20 69 6e 74 20 73 7a 2c  ell */.  int sz,
14d6a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 79             /* By
14d6b 74 65 73 20 6f 66 20 63 6f 6e 74 65 6e 74 20 69  tes of content i
14d6c 6e 20 70 43 65 6c 6c 20 2a 2f 0a 20 20 75 38 20  n pCell */.  u8 
14d6d 2a 70 54 65 6d 70 2c 20 20 20 20 20 20 20 20 2f  *pTemp,        /
14d6e 2a 20 54 65 6d 70 20 73 74 6f 72 61 67 65 20 73  * Temp storage s
14d6f 70 61 63 65 20 66 6f 72 20 70 43 65 6c 6c 2c 20  pace for pCell, 
14d70 69 66 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 75  if needed */.  u
14d71 38 20 6e 53 6b 69 70 20 20 20 20 20 20 20 20 20  8 nSkip         
14d72 20 2f 2a 20 44 6f 20 6e 6f 74 20 77 72 69 74 65   /* Do not write
14d73 20 74 68 65 20 66 69 72 73 74 20 6e 53 6b 69 70   the first nSkip
14d74 20 62 79 74 65 73 20 6f 66 20 74 68 65 20 63 65   bytes of the ce
14d75 6c 6c 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  ll */.){.  int i
14d76 64 78 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  dx;          /* 
14d77 57 68 65 72 65 20 74 6f 20 77 72 69 74 65 20 6e  Where to write n
14d78 65 77 20 63 65 6c 6c 20 63 6f 6e 74 65 6e 74 20  ew cell content 
14d79 69 6e 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69  in data[] */.  i
14d7a 6e 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20  nt j;           
14d7b 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72   /* Loop counter
14d7c 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 70 3b 20 20   */.  int top;  
14d7d 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
14d7e 20 62 79 74 65 20 6f 66 20 63 6f 6e 74 65 6e 74   byte of content
14d7f 20 66 6f 72 20 61 6e 79 20 63 65 6c 6c 20 69 6e   for any cell in
14d80 20 64 61 74 61 5b 5d 20 2a 2f 0a 20 20 69 6e 74   data[] */.  int
14d81 20 65 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f   end;          /
14d82 2a 20 46 69 72 73 74 20 62 79 74 65 20 70 61 73  * First byte pas
14d83 74 20 74 68 65 20 6c 61 73 74 20 63 65 6c 6c 20  t the last cell 
14d84 70 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b  pointer in data[
14d85 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 73 3b 20  ] */.  int ins; 
14d86 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
14d87 78 20 69 6e 20 64 61 74 61 5b 5d 20 77 68 65 72  x in data[] wher
14d88 65 20 6e 65 77 20 63 65 6c 6c 20 70 6f 69 6e 74  e new cell point
14d89 65 72 20 69 73 20 69 6e 73 65 72 74 65 64 20 2a  er is inserted *
14d8a 2f 0a 20 20 69 6e 74 20 68 64 72 3b 20 20 20 20  /.  int hdr;    
14d8b 20 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20        /* Offset 
14d8c 69 6e 74 6f 20 64 61 74 61 5b 5d 20 6f 66 20 74  into data[] of t
14d8d 68 65 20 70 61 67 65 20 68 65 61 64 65 72 20 2a  he page header *
14d8e 2f 0a 20 20 69 6e 74 20 63 65 6c 6c 4f 66 66 73  /.  int cellOffs
14d8f 65 74 3b 20 20 20 2f 2a 20 41 64 64 72 65 73 73  et;   /* Address
14d90 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c 20 70   of first cell p
14d91 6f 69 6e 74 65 72 20 69 6e 20 64 61 74 61 5b 5d  ointer in data[]
14d92 20 2a 2f 0a 20 20 75 38 20 2a 64 61 74 61 3b 20   */.  u8 *data; 
14d93 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
14d94 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 77 68  ontent of the wh
14d95 6f 6c 65 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  ole page */.  u8
14d96 20 2a 70 74 72 3b 20 20 20 20 20 20 20 20 20 20   *ptr;          
14d97 2f 2a 20 55 73 65 64 20 66 6f 72 20 6d 6f 76 69  /* Used for movi
14d98 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61  ng information a
14d99 72 6f 75 6e 64 20 69 6e 20 64 61 74 61 5b 5d 20  round in data[] 
14d9a 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  */..  assert( i>
14d9b 3d 30 20 26 26 20 69 3c 3d 70 50 61 67 65 2d 3e  =0 && i<=pPage->
14d9c 6e 43 65 6c 6c 2b 70 50 61 67 65 2d 3e 6e 4f 76  nCell+pPage->nOv
14d9d 65 72 66 6c 6f 77 20 29 3b 0a 20 20 61 73 73 65  erflow );.  asse
14d9e 72 74 28 20 73 7a 3d 3d 63 65 6c 6c 53 69 7a 65  rt( sz==cellSize
14d9f 50 74 72 28 70 50 61 67 65 2c 20 70 43 65 6c 6c  Ptr(pPage, pCell
14da0 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73  ) );.  assert( s
14da1 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
14da2 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d 75  d(pPage->pBt->mu
14da3 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 50  tex) );.  if( pP
14da4 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 7c  age->nOverflow |
14da5 7c 20 73 7a 2b 32 3e 70 50 61 67 65 2d 3e 6e 46  | sz+2>pPage->nF
14da6 72 65 65 20 29 7b 0a 20 20 20 20 69 66 28 20 70  ree ){.    if( p
14da7 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 6d 65  Temp ){.      me
14da8 6d 63 70 79 28 70 54 65 6d 70 2b 6e 53 6b 69 70  mcpy(pTemp+nSkip
14da9 2c 20 70 43 65 6c 6c 2b 6e 53 6b 69 70 2c 20 73  , pCell+nSkip, s
14daa 7a 2d 6e 53 6b 69 70 29 3b 0a 20 20 20 20 20 20  z-nSkip);.      
14dab 70 43 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20  pCell = pTemp;. 
14dac 20 20 20 7d 0a 20 20 20 20 6a 20 3d 20 70 50 61     }.    j = pPa
14dad 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 2b 2b 3b  ge->nOverflow++;
14dae 0a 20 20 20 20 61 73 73 65 72 74 28 20 6a 3c 28  .    assert( j<(
14daf 69 6e 74 29 28 73 69 7a 65 6f 66 28 70 50 61 67  int)(sizeof(pPag
14db0 65 2d 3e 61 4f 76 66 6c 29 2f 73 69 7a 65 6f 66  e->aOvfl)/sizeof
14db1 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d  (pPage->aOvfl[0]
14db2 29 29 20 29 3b 0a 20 20 20 20 70 50 61 67 65 2d  )) );.    pPage-
14db3 3e 61 4f 76 66 6c 5b 6a 5d 2e 70 43 65 6c 6c 20  >aOvfl[j].pCell 
14db4 3d 20 70 43 65 6c 6c 3b 0a 20 20 20 20 70 50 61  = pCell;.    pPa
14db5 67 65 2d 3e 61 4f 76 66 6c 5b 6a 5d 2e 69 64 78  ge->aOvfl[j].idx
14db6 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 67 65 2d   = i;.    pPage-
14db7 3e 6e 46 72 65 65 20 3d 20 30 3b 0a 20 20 7d 65  >nFree = 0;.  }e
14db8 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  lse{.    int rc 
14db9 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
14dba 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61  ite(pPage->pDbPa
14dbb 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  ge);.    if( rc!
14dbc 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
14dbd 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
14dbe 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
14dbf 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
14dc0 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
14dc1 70 44 62 50 61 67 65 29 20 29 3b 0a 20 20 20 20  pDbPage) );.    
14dc2 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
14dc3 61 74 61 3b 0a 20 20 20 20 68 64 72 20 3d 20 70  ata;.    hdr = p
14dc4 50 61 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b  Page->hdrOffset;
14dc5 0a 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32 62  .    top = get2b
14dc6 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d  yte(&data[hdr+5]
14dc7 29 3b 0a 20 20 20 20 63 65 6c 6c 4f 66 66 73 65  );.    cellOffse
14dc8 74 20 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f  t = pPage->cellO
14dc9 66 66 73 65 74 3b 0a 20 20 20 20 65 6e 64 20 3d  ffset;.    end =
14dca 20 63 65 6c 6c 4f 66 66 73 65 74 20 2b 20 32 2a   cellOffset + 2*
14dcb 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 2b 20 32  pPage->nCell + 2
14dcc 3b 0a 20 20 20 20 69 6e 73 20 3d 20 63 65 6c 6c  ;.    ins = cell
14dcd 4f 66 66 73 65 74 20 2b 20 32 2a 69 3b 0a 20 20  Offset + 2*i;.  
14dce 20 20 69 66 28 20 65 6e 64 20 3e 20 74 6f 70 20    if( end > top 
14dcf 2d 20 73 7a 20 29 7b 0a 20 20 20 20 20 20 72 63  - sz ){.      rc
14dd0 20 3d 20 64 65 66 72 61 67 6d 65 6e 74 50 61 67   = defragmentPag
14dd1 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
14dd2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
14dd3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  K ){.        ret
14dd4 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a  urn rc;.      }.
14dd5 20 20 20 20 20 20 74 6f 70 20 3d 20 67 65 74 32        top = get2
14dd6 62 79 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35  byte(&data[hdr+5
14dd7 5d 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  ]);.      assert
14dd8 28 20 65 6e 64 20 2b 20 73 7a 20 3c 3d 20 74 6f  ( end + sz <= to
14dd9 70 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  p );.    }.    i
14dda 64 78 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61  dx = allocateSpa
14ddb 63 65 28 70 50 61 67 65 2c 20 73 7a 29 3b 0a 20  ce(pPage, sz);. 
14ddc 20 20 20 61 73 73 65 72 74 28 20 69 64 78 3e 30     assert( idx>0
14ddd 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
14dde 65 6e 64 20 3c 3d 20 67 65 74 32 62 79 74 65 28  end <= get2byte(
14ddf 26 64 61 74 61 5b 68 64 72 2b 35 5d 29 20 29 3b  &data[hdr+5]) );
14de0 0a 20 20 20 20 69 66 20 28 69 64 78 2b 73 7a 20  .    if (idx+sz 
14de1 3e 20 70 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73  > pPage->pBt->us
14de2 61 62 6c 65 53 69 7a 65 29 20 7b 0a 20 20 20 20  ableSize) {.    
14de3 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
14de4 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
14de5 20 20 7d 0a 20 20 20 20 70 50 61 67 65 2d 3e 6e    }.    pPage->n
14de6 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 70 50 61 67  Cell++;.    pPag
14de7 65 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 3b 0a 20  e->nFree -= 2;. 
14de8 20 20 20 6d 65 6d 63 70 79 28 26 64 61 74 61 5b     memcpy(&data[
14de9 69 64 78 2b 6e 53 6b 69 70 5d 2c 20 70 43 65 6c  idx+nSkip], pCel
14dea 6c 2b 6e 53 6b 69 70 2c 20 73 7a 2d 6e 53 6b 69  l+nSkip, sz-nSki
14deb 70 29 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 65 6e  p);.    for(j=en
14dec 64 2d 32 2c 20 70 74 72 3d 26 64 61 74 61 5b 6a  d-2, ptr=&data[j
14ded 5d 3b 20 6a 3e 69 6e 73 3b 20 6a 2d 3d 32 2c 20  ]; j>ins; j-=2, 
14dee 70 74 72 2d 3d 32 29 7b 0a 20 20 20 20 20 20 70  ptr-=2){.      p
14def 74 72 5b 30 5d 20 3d 20 70 74 72 5b 2d 32 5d 3b  tr[0] = ptr[-2];
14df0 0a 20 20 20 20 20 20 70 74 72 5b 31 5d 20 3d 20  .      ptr[1] = 
14df1 70 74 72 5b 2d 31 5d 3b 0a 20 20 20 20 7d 0a 20  ptr[-1];.    }. 
14df2 20 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74     put2byte(&dat
14df3 61 5b 69 6e 73 5d 2c 20 69 64 78 29 3b 0a 20 20  a[ins], idx);.  
14df4 20 20 70 75 74 32 62 79 74 65 28 26 64 61 74 61    put2byte(&data
14df5 5b 68 64 72 2b 33 5d 2c 20 70 50 61 67 65 2d 3e  [hdr+3], pPage->
14df6 6e 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20  nCell);.#ifndef 
14df7 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
14df8 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20 70  VACUUM.    if( p
14df9 50 61 67 65 2d 3e 70 42 74 2d 3e 61 75 74 6f 56  Page->pBt->autoV
14dfa 61 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 2f  acuum ){.      /
14dfb 2a 20 54 68 65 20 63 65 6c 6c 20 6d 61 79 20 63  * The cell may c
14dfc 6f 6e 74 61 69 6e 20 61 20 70 6f 69 6e 74 65 72  ontain a pointer
14dfd 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20   to an overflow 
14dfe 70 61 67 65 2e 20 49 66 20 73 6f 2c 20 77 72 69  page. If so, wri
14dff 74 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  te.      ** the 
14e00 65 6e 74 72 79 20 66 6f 72 20 74 68 65 20 6f 76  entry for the ov
14e01 65 72 66 6c 6f 77 20 70 61 67 65 20 69 6e 74 6f  erflow page into
14e02 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
14e03 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
14e04 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a   CellInfo info;.
14e05 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
14e06 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70  eeParseCellPtr(p
14e07 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69 6e  Page, pCell, &in
14e08 66 6f 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  fo);.      asser
14e09 74 28 20 28 69 6e 66 6f 2e 6e 44 61 74 61 2b 28  t( (info.nData+(
14e0a 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79 3f 30 3a  pPage->intKey?0:
14e0b 69 6e 66 6f 2e 6e 4b 65 79 29 29 3d 3d 69 6e 66  info.nKey))==inf
14e0c 6f 2e 6e 50 61 79 6c 6f 61 64 20 29 3b 0a 20 20  o.nPayload );.  
14e0d 20 20 20 20 69 66 28 20 28 69 6e 66 6f 2e 6e 44      if( (info.nD
14e0e 61 74 61 2b 28 70 50 61 67 65 2d 3e 69 6e 74 4b  ata+(pPage->intK
14e0f 65 79 3f 30 3a 69 6e 66 6f 2e 6e 4b 65 79 29 29  ey?0:info.nKey))
14e10 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a  >info.nLocal ){.
14e11 20 20 20 20 20 20 20 20 50 67 6e 6f 20 70 67 6e          Pgno pgn
14e12 6f 4f 76 66 6c 20 3d 20 67 65 74 34 62 79 74 65  oOvfl = get4byte
14e13 28 26 70 43 65 6c 6c 5b 69 6e 66 6f 2e 69 4f 76  (&pCell[info.iOv
14e14 65 72 66 6c 6f 77 5d 29 3b 0a 20 20 20 20 20 20  erflow]);.      
14e15 20 20 72 63 20 3d 20 70 74 72 6d 61 70 50 75 74    rc = ptrmapPut
14e16 28 70 50 61 67 65 2d 3e 70 42 74 2c 20 70 67 6e  (pPage->pBt, pgn
14e17 6f 4f 76 66 6c 2c 20 50 54 52 4d 41 50 5f 4f 56  oOvfl, PTRMAP_OV
14e18 45 52 46 4c 4f 57 31 2c 20 70 50 61 67 65 2d 3e  ERFLOW1, pPage->
14e19 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 20 20 69  pgno);.        i
14e1a 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
14e1b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
14e1c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64      }.    }.#end
14e1d 69 66 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  if.  }..  return
14e1e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
14e1f 2a 0a 2a 2a 20 41 64 64 20 61 20 6c 69 73 74 20  *.** Add a list 
14e20 6f 66 20 63 65 6c 6c 73 20 74 6f 20 61 20 70 61  of cells to a pa
14e21 67 65 2e 20 20 54 68 65 20 70 61 67 65 20 73 68  ge.  The page sh
14e22 6f 75 6c 64 20 62 65 20 69 6e 69 74 69 61 6c 6c  ould be initiall
14e23 79 20 65 6d 70 74 79 2e 0a 2a 2a 20 54 68 65 20  y empty..** The 
14e24 63 65 6c 6c 73 20 61 72 65 20 67 75 61 72 61 6e  cells are guaran
14e25 74 65 65 64 20 74 6f 20 66 69 74 20 6f 6e 20 74  teed to fit on t
14e26 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61 74  he page..*/.stat
14e27 69 63 20 76 6f 69 64 20 61 73 73 65 6d 62 6c 65  ic void assemble
14e28 50 61 67 65 28 0a 20 20 4d 65 6d 50 61 67 65 20  Page(.  MemPage 
14e29 2a 70 50 61 67 65 2c 20 20 20 2f 2a 20 54 68 65  *pPage,   /* The
14e2a 20 70 61 67 65 20 74 6f 20 62 65 20 61 73 73 65   page to be asse
14e2b 6d 62 6c 69 65 64 20 2a 2f 0a 20 20 69 6e 74 20  mblied */.  int 
14e2c 6e 43 65 6c 6c 2c 20 20 20 20 20 20 20 20 2f 2a  nCell,        /*
14e2d 20 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63   The number of c
14e2e 65 6c 6c 73 20 74 6f 20 61 64 64 20 74 6f 20 74  ells to add to t
14e2f 68 69 73 20 70 61 67 65 20 2a 2f 0a 20 20 75 38  his page */.  u8
14e30 20 2a 2a 61 70 43 65 6c 6c 2c 20 20 20 20 20 20   **apCell,      
14e31 2f 2a 20 50 6f 69 6e 74 65 72 73 20 74 6f 20 63  /* Pointers to c
14e32 65 6c 6c 20 62 6f 64 69 65 73 20 2a 2f 0a 20 20  ell bodies */.  
14e33 75 31 36 20 2a 61 53 69 7a 65 20 20 20 20 20 20  u16 *aSize      
14e34 20 20 2f 2a 20 53 69 7a 65 73 20 6f 66 20 74 68    /* Sizes of th
14e35 65 20 63 65 6c 6c 73 20 2a 2f 0a 29 7b 0a 20 20  e cells */.){.  
14e36 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
14e37 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
14e38 72 20 2a 2f 0a 20 20 69 6e 74 20 74 6f 74 61 6c  r */.  int total
14e39 53 69 7a 65 3b 20 20 20 20 2f 2a 20 54 6f 74 61  Size;    /* Tota
14e3a 6c 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65  l size of all ce
14e3b 6c 6c 73 20 2a 2f 0a 20 20 69 6e 74 20 68 64 72  lls */.  int hdr
14e3c 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  ;          /* In
14e3d 64 65 78 20 6f 66 20 70 61 67 65 20 68 65 61 64  dex of page head
14e3e 65 72 20 2a 2f 0a 20 20 69 6e 74 20 63 65 6c 6c  er */.  int cell
14e3f 70 74 72 3b 20 20 20 20 20 20 2f 2a 20 41 64 64  ptr;      /* Add
14e40 72 65 73 73 20 6f 66 20 6e 65 78 74 20 63 65 6c  ress of next cel
14e41 6c 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 69  l pointer */.  i
14e42 6e 74 20 63 65 6c 6c 62 6f 64 79 3b 20 20 20 20  nt cellbody;    
14e43 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 6e   /* Address of n
14e44 65 78 74 20 63 65 6c 6c 20 62 6f 64 79 20 2a 2f  ext cell body */
14e45 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20  .  u8 *data;    
14e46 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 6f 72       /* Data for
14e47 20 74 68 65 20 70 61 67 65 20 2a 2f 0a 0a 20 20   the page */..  
14e48 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e  assert( pPage->n
14e49 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 20  Overflow==0 );. 
14e4a 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
14e4b 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61 67  _mutex_held(pPag
14e4c 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20 29  e->pBt->mutex) )
14e4d 3b 0a 20 20 74 6f 74 61 6c 53 69 7a 65 20 3d 20  ;.  totalSize = 
14e4e 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  0;.  for(i=0; i<
14e4f 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
14e50 20 74 6f 74 61 6c 53 69 7a 65 20 2b 3d 20 61 53   totalSize += aS
14e51 69 7a 65 5b 69 5d 3b 0a 20 20 7d 0a 20 20 61 73  ize[i];.  }.  as
14e52 73 65 72 74 28 20 74 6f 74 61 6c 53 69 7a 65 2b  sert( totalSize+
14e53 32 2a 6e 43 65 6c 6c 3c 3d 70 50 61 67 65 2d 3e  2*nCell<=pPage->
14e54 6e 46 72 65 65 20 29 3b 0a 20 20 61 73 73 65 72  nFree );.  asser
14e55 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 3d  t( pPage->nCell=
14e56 3d 30 20 29 3b 0a 20 20 63 65 6c 6c 70 74 72 20  =0 );.  cellptr 
14e57 3d 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66  = pPage->cellOff
14e58 73 65 74 3b 0a 20 20 64 61 74 61 20 3d 20 70 50  set;.  data = pP
14e59 61 67 65 2d 3e 61 44 61 74 61 3b 0a 20 20 68 64  age->aData;.  hd
14e5a 72 20 3d 20 70 50 61 67 65 2d 3e 68 64 72 4f 66  r = pPage->hdrOf
14e5b 66 73 65 74 3b 0a 20 20 70 75 74 32 62 79 74 65  fset;.  put2byte
14e5c 28 26 64 61 74 61 5b 68 64 72 2b 33 5d 2c 20 6e  (&data[hdr+3], n
14e5d 43 65 6c 6c 29 3b 0a 20 20 69 66 28 20 6e 43 65  Cell);.  if( nCe
14e5e 6c 6c 20 29 7b 0a 20 20 20 20 63 65 6c 6c 62 6f  ll ){.    cellbo
14e5f 64 79 20 3d 20 61 6c 6c 6f 63 61 74 65 53 70 61  dy = allocateSpa
14e60 63 65 28 70 50 61 67 65 2c 20 74 6f 74 61 6c 53  ce(pPage, totalS
14e61 69 7a 65 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ize);.    assert
14e62 28 20 63 65 6c 6c 62 6f 64 79 3e 30 20 29 3b 0a  ( cellbody>0 );.
14e63 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61 67      assert( pPag
14e64 65 2d 3e 6e 46 72 65 65 20 3e 3d 20 32 2a 6e 43  e->nFree >= 2*nC
14e65 65 6c 6c 20 29 3b 0a 20 20 20 20 70 50 61 67 65  ell );.    pPage
14e66 2d 3e 6e 46 72 65 65 20 2d 3d 20 32 2a 6e 43 65  ->nFree -= 2*nCe
14e67 6c 6c 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  ll;.    for(i=0;
14e68 20 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a   i<nCell; i++){.
14e69 20 20 20 20 20 20 70 75 74 32 62 79 74 65 28 26        put2byte(&
14e6a 64 61 74 61 5b 63 65 6c 6c 70 74 72 5d 2c 20 63  data[cellptr], c
14e6b 65 6c 6c 62 6f 64 79 29 3b 0a 20 20 20 20 20 20  ellbody);.      
14e6c 6d 65 6d 63 70 79 28 26 64 61 74 61 5b 63 65 6c  memcpy(&data[cel
14e6d 6c 62 6f 64 79 5d 2c 20 61 70 43 65 6c 6c 5b 69  lbody], apCell[i
14e6e 5d 2c 20 61 53 69 7a 65 5b 69 5d 29 3b 0a 20 20  ], aSize[i]);.  
14e6f 20 20 20 20 63 65 6c 6c 70 74 72 20 2b 3d 20 32      cellptr += 2
14e70 3b 0a 20 20 20 20 20 20 63 65 6c 6c 62 6f 64 79  ;.      cellbody
14e71 20 2b 3d 20 61 53 69 7a 65 5b 69 5d 3b 0a 20 20   += aSize[i];.  
14e72 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20    }.    assert( 
14e73 63 65 6c 6c 62 6f 64 79 3d 3d 70 50 61 67 65 2d  cellbody==pPage-
14e74 3e 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65  >pBt->usableSize
14e75 20 29 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65 2d   );.  }.  pPage-
14e76 3e 6e 43 65 6c 6c 20 3d 20 6e 43 65 6c 6c 3b 0a  >nCell = nCell;.
14e77 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  }../*.** The fol
14e78 6c 6f 77 69 6e 67 20 70 61 72 61 6d 65 74 65 72  lowing parameter
14e79 73 20 64 65 74 65 72 6d 69 6e 65 20 68 6f 77 20  s determine how 
14e7a 6d 61 6e 79 20 61 64 6a 61 63 65 6e 74 20 70 61  many adjacent pa
14e7b 67 65 73 20 67 65 74 20 69 6e 76 6f 6c 76 65 64  ges get involved
14e7c 0a 2a 2a 20 69 6e 20 61 20 62 61 6c 61 6e 63 69  .** in a balanci
14e7d 6e 67 20 6f 70 65 72 61 74 69 6f 6e 2e 20 20 4e  ng operation.  N
14e7e 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20  N is the number 
14e7f 6f 66 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20  of neighbors on 
14e80 65 69 74 68 65 72 20 73 69 64 65 0a 2a 2a 20 6f  either side.** o
14e81 66 20 74 68 65 20 70 61 67 65 20 74 68 61 74 20  f the page that 
14e82 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20 74  participate in t
14e83 68 65 20 62 61 6c 61 6e 63 69 6e 67 20 6f 70 65  he balancing ope
14e84 72 61 74 69 6f 6e 2e 20 20 4e 42 20 69 73 20 74  ration.  NB is t
14e85 68 65 0a 2a 2a 20 74 6f 74 61 6c 20 6e 75 6d 62  he.** total numb
14e86 65 72 20 6f 66 20 70 61 67 65 73 20 74 68 61 74  er of pages that
14e87 20 70 61 72 74 69 63 69 70 61 74 65 2c 20 69 6e   participate, in
14e88 63 6c 75 64 69 6e 67 20 74 68 65 20 74 61 72 67  cluding the targ
14e89 65 74 20 70 61 67 65 20 61 6e 64 0a 2a 2a 20 4e  et page and.** N
14e8a 4e 20 6e 65 69 67 68 62 6f 72 73 20 6f 6e 20 65  N neighbors on e
14e8b 69 74 68 65 72 20 73 69 64 65 2e 0a 2a 2a 0a 2a  ither side..**.*
14e8c 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d 20 76 61  * The minimum va
14e8d 6c 75 65 20 6f 66 20 4e 4e 20 69 73 20 31 20 28  lue of NN is 1 (
14e8e 6f 66 20 63 6f 75 72 73 65 29 2e 20 20 49 6e 63  of course).  Inc
14e8f 72 65 61 73 69 6e 67 20 4e 4e 20 61 62 6f 76 65  reasing NN above
14e90 20 31 0a 2a 2a 20 28 74 6f 20 32 20 6f 72 20 33   1.** (to 2 or 3
14e91 29 20 67 69 76 65 73 20 61 20 6d 6f 64 65 73 74  ) gives a modest
14e92 20 69 6d 70 72 6f 76 65 6d 65 6e 74 20 69 6e 20   improvement in 
14e93 53 45 4c 45 43 54 20 61 6e 64 20 44 45 4c 45 54  SELECT and DELET
14e94 45 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a 2a 2a  E performance.**
14e95 20 69 6e 20 65 78 63 68 61 6e 67 65 20 66 6f 72   in exchange for
14e96 20 61 20 6c 61 72 67 65 72 20 64 65 67 72 61 64   a larger degrad
14e97 61 74 69 6f 6e 20 69 6e 20 49 4e 53 45 52 54 20  ation in INSERT 
14e98 61 6e 64 20 55 50 44 41 54 45 20 70 65 72 66 6f  and UPDATE perfo
14e99 72 6d 61 6e 63 65 2e 0a 2a 2a 20 54 68 65 20 76  rmance..** The v
14e9a 61 6c 75 65 20 6f 66 20 4e 4e 20 61 70 70 65 61  alue of NN appea
14e9b 72 73 20 74 6f 20 67 69 76 65 20 74 68 65 20 62  rs to give the b
14e9c 65 73 74 20 72 65 73 75 6c 74 73 20 6f 76 65 72  est results over
14e9d 61 6c 6c 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20  all..*/.#define 
14e9e 4e 4e 20 31 20 20 20 20 20 20 20 20 20 20 20 20  NN 1            
14e9f 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6e 65   /* Number of ne
14ea0 69 67 68 62 6f 72 73 20 6f 6e 20 65 69 74 68 65  ighbors on eithe
14ea1 72 20 73 69 64 65 20 6f 66 20 70 50 61 67 65 20  r side of pPage 
14ea2 2a 2f 0a 23 64 65 66 69 6e 65 20 4e 42 20 28 4e  */.#define NB (N
14ea3 4e 2a 32 2b 31 29 20 20 20 20 20 20 2f 2a 20 54  N*2+1)      /* T
14ea4 6f 74 61 6c 20 70 61 67 65 73 20 69 6e 76 6f 6c  otal pages invol
14ea5 76 65 64 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  ved in the balan
14ea6 63 65 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72  ce */../* Forwar
14ea7 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73  d reference */.s
14ea8 74 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63  tatic int balanc
14ea9 65 28 42 74 43 75 72 73 6f 72 2a 2c 20 69 6e 74  e(BtCursor*, int
14eaa 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
14eab 54 45 5f 4f 4d 49 54 5f 51 55 49 43 4b 42 41 4c  TE_OMIT_QUICKBAL
14eac 41 4e 43 45 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ANCE./*.** This 
14ead 76 65 72 73 69 6f 6e 20 6f 66 20 62 61 6c 61 6e  version of balan
14eae 63 65 28 29 20 68 61 6e 64 6c 65 73 20 74 68 65  ce() handles the
14eaf 20 63 6f 6d 6d 6f 6e 20 73 70 65 63 69 61 6c 20   common special 
14eb0 63 61 73 65 20 77 68 65 72 65 0a 2a 2a 20 61 20  case where.** a 
14eb1 6e 65 77 20 65 6e 74 72 79 20 69 73 20 62 65 69  new entry is bei
14eb2 6e 67 20 69 6e 73 65 72 74 65 64 20 6f 6e 20 74  ng inserted on t
14eb3 68 65 20 65 78 74 72 65 6d 65 20 72 69 67 68 74  he extreme right
14eb4 2d 65 6e 64 20 6f 66 20 74 68 65 0a 2a 2a 20 74  -end of the.** t
14eb5 72 65 65 2c 20 69 6e 20 6f 74 68 65 72 20 77 6f  ree, in other wo
14eb6 72 64 73 2c 20 77 68 65 6e 20 74 68 65 20 6e 65  rds, when the ne
14eb7 77 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 63  w entry will bec
14eb8 6f 6d 65 20 74 68 65 20 6c 61 72 67 65 73 74 0a  ome the largest.
14eb9 2a 2a 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ** entry in the 
14eba 74 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 73 74  tree..**.** Inst
14ebb 65 61 64 20 6f 66 20 74 72 79 69 6e 67 20 62 61  ead of trying ba
14ebc 6c 61 6e 63 65 20 74 68 65 20 33 20 72 69 67 68  lance the 3 righ
14ebd 74 2d 6d 6f 73 74 20 6c 65 61 66 20 70 61 67 65  t-most leaf page
14ebe 73 2c 20 6a 75 73 74 20 61 64 64 0a 2a 2a 20 61  s, just add.** a
14ebf 20 6e 65 77 20 70 61 67 65 20 74 6f 20 74 68 65   new page to the
14ec0 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65   right-hand side
14ec1 20 61 6e 64 20 70 75 74 20 74 68 65 20 6f 6e 65   and put the one
14ec2 20 6e 65 77 20 65 6e 74 72 79 20 69 6e 0a 2a 2a   new entry in.**
14ec3 20 74 68 61 74 20 70 61 67 65 2e 20 20 54 68 69   that page.  Thi
14ec4 73 20 6c 65 61 76 65 73 20 74 68 65 20 72 69 67  s leaves the rig
14ec5 68 74 20 73 69 64 65 20 6f 66 20 74 68 65 20 74  ht side of the t
14ec6 72 65 65 20 73 6f 6d 65 77 68 61 74 0a 2a 2a 20  ree somewhat.** 
14ec7 75 6e 62 61 6c 61 6e 63 65 64 2e 20 20 42 75 74  unbalanced.  But
14ec8 20 6f 64 64 73 20 61 72 65 20 74 68 61 74 20 77   odds are that w
14ec9 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74  e will be insert
14eca 69 6e 67 20 6e 65 77 20 65 6e 74 72 69 65 73 0a  ing new entries.
14ecb 2a 2a 20 61 74 20 74 68 65 20 65 6e 64 20 73 6f  ** at the end so
14ecc 6f 6e 20 61 66 74 65 72 77 61 72 64 73 20 73 6f  on afterwards so
14ecd 20 74 68 65 20 6e 65 61 72 6c 79 20 65 6d 70 74   the nearly empt
14ece 79 20 70 61 67 65 20 77 69 6c 6c 20 71 75 69 63  y page will quic
14ecf 6b 6c 79 0a 2a 2a 20 66 69 6c 6c 20 75 70 2e 20  kly.** fill up. 
14ed0 20 4f 6e 20 61 76 65 72 61 67 65 2e 0a 2a 2a 0a   On average..**.
14ed1 2a 2a 20 70 50 61 67 65 20 69 73 20 74 68 65 20  ** pPage is the 
14ed2 6c 65 61 66 20 70 61 67 65 20 77 68 69 63 68 20  leaf page which 
14ed3 69 73 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  is the right-mos
14ed4 74 20 70 61 67 65 20 69 6e 20 74 68 65 20 74 72  t page in the tr
14ed5 65 65 2e 0a 2a 2a 20 70 50 61 72 65 6e 74 20 69  ee..** pParent i
14ed6 73 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20 70  s its parent.  p
14ed7 50 61 67 65 20 6d 75 73 74 20 68 61 76 65 20 61  Page must have a
14ed8 20 73 69 6e 67 6c 65 20 6f 76 65 72 66 6c 6f 77   single overflow
14ed9 20 65 6e 74 72 79 0a 2a 2a 20 77 68 69 63 68 20   entry.** which 
14eda 69 73 20 61 6c 73 6f 20 74 68 65 20 72 69 67 68  is also the righ
14edb 74 2d 6d 6f 73 74 20 65 6e 74 72 79 20 6f 6e 20  t-most entry on 
14edc 74 68 65 20 70 61 67 65 2e 0a 2a 2f 0a 73 74 61  the page..*/.sta
14edd 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65 5f  tic int balance_
14ede 71 75 69 63 6b 28 42 74 43 75 72 73 6f 72 20 2a  quick(BtCursor *
14edf 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72 63 3b  pCur){.  int rc;
14ee0 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77  .  MemPage *pNew
14ee1 20 3d 20 30 3b 0a 20 20 50 67 6e 6f 20 70 67 6e   = 0;.  Pgno pgn
14ee2 6f 4e 65 77 3b 0a 20 20 75 38 20 2a 70 43 65 6c  oNew;.  u8 *pCel
14ee3 6c 3b 0a 20 20 75 31 36 20 73 7a 43 65 6c 6c 3b  l;.  u16 szCell;
14ee4 0a 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f  .  CellInfo info
14ee5 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61  ;.  MemPage *pPa
14ee6 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
14ee7 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
14ee8 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 72 65    MemPage *pPare
14ee9 6e 74 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  nt = pCur->apPag
14eea 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d  e[pCur->iPage-1]
14eeb 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
14eec 74 20 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a  t = pPage->pBt;.
14eed 20 20 69 6e 74 20 70 61 72 65 6e 74 49 64 78 20    int parentIdx 
14eee 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  = pParent->nCell
14eef 3b 20 20 20 2f 2a 20 70 50 61 72 65 6e 74 20 6e  ;   /* pParent n
14ef0 65 77 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  ew divider cell 
14ef1 69 6e 64 65 78 20 2a 2f 0a 20 20 69 6e 74 20 70  index */.  int p
14ef2 61 72 65 6e 74 53 69 7a 65 3b 20 20 20 20 20 20  arentSize;      
14ef3 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
14ef4 53 69 7a 65 20 6f 66 20 6e 65 77 20 64 69 76 69  Size of new divi
14ef5 64 65 72 20 63 65 6c 6c 20 2a 2f 0a 20 20 75 38  der cell */.  u8
14ef6 20 70 61 72 65 6e 74 43 65 6c 6c 5b 36 34 5d 3b   parentCell[64];
14ef7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
14ef8 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 74 68 65  /* Space for the
14ef9 20 6e 65 77 20 64 69 76 69 64 65 72 20 63 65 6c   new divider cel
14efa 6c 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20  l */..  assert( 
14efb 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
14efc 6c 64 28 70 50 61 67 65 2d 3e 70 42 74 2d 3e 6d  ld(pPage->pBt->m
14efd 75 74 65 78 29 20 29 3b 0a 0a 20 20 2f 2a 20 41  utex) );..  /* A
14efe 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 70 61  llocate a new pa
14eff 67 65 2e 20 49 6e 73 65 72 74 20 74 68 65 20 6f  ge. Insert the o
14f00 76 65 72 66 6c 6f 77 20 63 65 6c 6c 20 66 72 6f  verflow cell fro
14f01 6d 20 70 50 61 67 65 0a 20 20 2a 2a 20 69 6e 74  m pPage.  ** int
14f02 6f 20 69 74 2e 20 54 68 65 6e 20 72 65 6d 6f 76  o it. Then remov
14f03 65 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20 63  e the overflow c
14f04 65 6c 6c 20 66 72 6f 6d 20 70 50 61 67 65 2e 0a  ell from pPage..
14f05 20 20 2a 2f 0a 20 20 72 63 20 3d 20 61 6c 6c 6f    */.  rc = allo
14f06 63 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42  cateBtreePage(pB
14f07 74 2c 20 26 70 4e 65 77 2c 20 26 70 67 6e 6f 4e  t, &pNew, &pgnoN
14f08 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28  ew, 0, 0);.  if(
14f09 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
14f0a 7b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 70 50  {.    pCell = pP
14f0b 61 67 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 70 43  age->aOvfl[0].pC
14f0c 65 6c 6c 3b 0a 20 20 20 20 73 7a 43 65 6c 6c 20  ell;.    szCell 
14f0d 3d 20 63 65 6c 6c 53 69 7a 65 50 74 72 28 70 50  = cellSizePtr(pP
14f0e 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  age, pCell);.   
14f0f 20 7a 65 72 6f 50 61 67 65 28 70 4e 65 77 2c 20   zeroPage(pNew, 
14f10 70 50 61 67 65 2d 3e 61 44 61 74 61 5b 30 5d 29  pPage->aData[0])
14f11 3b 0a 20 20 20 20 61 73 73 65 6d 62 6c 65 50 61  ;.    assemblePa
14f12 67 65 28 70 4e 65 77 2c 20 31 2c 20 26 70 43 65  ge(pNew, 1, &pCe
14f13 6c 6c 2c 20 26 73 7a 43 65 6c 6c 29 3b 0a 20 20  ll, &szCell);.  
14f14 20 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c    pPage->nOverfl
14f15 6f 77 20 3d 20 30 3b 0a 20 20 0a 20 20 20 20 2f  ow = 0;.  .    /
14f16 2a 20 70 50 61 67 65 20 69 73 20 63 75 72 72 65  * pPage is curre
14f17 6e 74 6c 79 20 74 68 65 20 72 69 67 68 74 2d 63  ntly the right-c
14f18 68 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 2e  hild of pParent.
14f19 20 43 68 61 6e 67 65 20 74 68 69 73 0a 20 20 20   Change this.   
14f1a 20 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65 20   ** so that the 
14f1b 72 69 67 68 74 2d 63 68 69 6c 64 20 69 73 20 74  right-child is t
14f1c 68 65 20 6e 65 77 20 70 61 67 65 20 61 6c 6c 6f  he new page allo
14f1d 63 61 74 65 64 20 61 62 6f 76 65 20 61 6e 64 0a  cated above and.
14f1e 20 20 20 20 2a 2a 20 70 50 61 67 65 20 69 73 20      ** pPage is 
14f1f 74 68 65 20 6e 65 78 74 2d 74 6f 2d 72 69 67 68  the next-to-righ
14f20 74 20 63 68 69 6c 64 2e 20 0a 20 20 20 20 2a 2a  t child. .    **
14f21 0a 20 20 20 20 2a 2a 20 49 67 6e 6f 72 65 20 74  .    ** Ignore t
14f22 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
14f23 6f 66 20 74 68 65 20 63 61 6c 6c 20 74 6f 20 66  of the call to f
14f24 69 6c 6c 49 6e 43 65 6c 6c 28 29 2e 20 66 69 6c  illInCell(). fil
14f25 6c 49 6e 43 65 6c 6c 28 29 0a 20 20 20 20 2a 2a  lInCell().    **
14f26 20 6d 61 79 20 6f 6e 6c 79 20 72 65 74 75 72 6e   may only return
14f27 20 6f 74 68 65 72 20 74 68 61 6e 20 53 51 4c 49   other than SQLI
14f28 54 45 5f 4f 4b 20 69 66 20 69 74 20 69 73 20 72  TE_OK if it is r
14f29 65 71 75 69 72 65 64 20 74 6f 20 61 6c 6c 6f 63  equired to alloc
14f2a 61 74 65 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 6f  ate.    ** one o
14f2b 72 20 6d 6f 72 65 20 6f 76 65 72 66 6c 6f 77 20  r more overflow 
14f2c 70 61 67 65 73 2e 20 53 69 6e 63 65 20 61 6e 20  pages. Since an 
14f2d 69 6e 74 65 72 6e 61 6c 20 74 61 62 6c 65 20 42  internal table B
14f2e 2d 54 72 65 65 20 63 65 6c 6c 20 0a 20 20 20 20  -Tree cell .    
14f2f 2a 2a 20 6d 61 79 20 6e 65 76 65 72 20 73 70 69  ** may never spi
14f30 6c 6c 20 6f 76 65 72 20 6f 6e 74 6f 20 61 6e 20  ll over onto an 
14f31 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20 28 69  overflow page (i
14f32 74 20 69 73 20 61 20 6d 61 78 69 6d 75 6d 20 6f  t is a maximum o
14f33 66 20 0a 20 20 20 20 2a 2a 20 31 33 20 62 79 74  f .    ** 13 byt
14f34 65 73 20 69 6e 20 73 69 7a 65 29 2c 20 69 74 20  es in size), it 
14f35 69 73 20 6e 6f 74 20 6e 65 63 63 65 73 73 61 72  is not neccessar
14f36 79 20 74 6f 20 63 68 65 63 6b 20 74 68 65 20 72  y to check the r
14f37 65 74 75 72 6e 20 63 6f 64 65 2e 0a 20 20 20 20  eturn code..    
14f38 2a 2a 0a 20 20 20 20 2a 2a 20 53 69 6d 69 6c 61  **.    ** Simila
14f39 72 6c 79 2c 20 74 68 65 20 69 6e 73 65 72 74 43  rly, the insertC
14f3a 65 6c 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 63  ell() function c
14f3b 61 6e 6e 6f 74 20 66 61 69 6c 20 69 66 20 74 68  annot fail if th
14f3c 65 20 70 61 67 65 0a 20 20 20 20 2a 2a 20 62 65  e page.    ** be
14f3d 69 6e 67 20 69 6e 73 65 72 74 65 64 20 69 6e 74  ing inserted int
14f3e 6f 20 69 73 20 61 6c 72 65 61 64 79 20 77 72 69  o is already wri
14f3f 74 61 62 6c 65 20 61 6e 64 20 74 68 65 20 63 65  table and the ce
14f40 6c 6c 20 64 6f 65 73 20 6e 6f 74 20 0a 20 20 20  ll does not .   
14f41 20 2a 2a 20 63 6f 6e 74 61 69 6e 20 61 6e 20 6f   ** contain an o
14f42 76 65 72 66 6c 6f 77 20 70 6f 69 6e 74 65 72 2e  verflow pointer.
14f43 20 53 6f 20 69 67 6e 6f 72 65 20 74 68 69 73 20   So ignore this 
14f44 72 65 74 75 72 6e 20 63 6f 64 65 20 74 6f 6f 2e  return code too.
14f45 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
14f46 72 74 28 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  rt( pPage->nCell
14f47 3e 30 20 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20  >0 );.    pCell 
14f48 3d 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65  = findCell(pPage
14f49 2c 20 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 2d 31  , pPage->nCell-1
14f4a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
14f4b 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
14f4c 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69  pPage, pCell, &i
14f4d 6e 66 6f 29 3b 0a 20 20 20 20 66 69 6c 6c 49 6e  nfo);.    fillIn
14f4e 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61  Cell(pParent, pa
14f4f 72 65 6e 74 43 65 6c 6c 2c 20 30 2c 20 69 6e 66  rentCell, 0, inf
14f50 6f 2e 6e 4b 65 79 2c 20 30 2c 20 30 2c 20 30 2c  o.nKey, 0, 0, 0,
14f51 20 26 70 61 72 65 6e 74 53 69 7a 65 29 3b 0a 20   &parentSize);. 
14f52 20 20 20 61 73 73 65 72 74 28 20 70 61 72 65 6e     assert( paren
14f53 74 53 69 7a 65 3c 36 34 20 29 3b 0a 20 20 20 20  tSize<64 );.    
14f54 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 50  assert( sqlite3P
14f55 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65 28  agerIswriteable(
14f56 70 50 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65  pParent->pDbPage
14f57 29 20 29 3b 0a 20 20 20 20 69 6e 73 65 72 74 43  ) );.    insertC
14f58 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 70 61 72  ell(pParent, par
14f59 65 6e 74 49 64 78 2c 20 70 61 72 65 6e 74 43 65  entIdx, parentCe
14f5a 6c 6c 2c 20 70 61 72 65 6e 74 53 69 7a 65 2c 20  ll, parentSize, 
14f5b 30 2c 20 34 29 3b 0a 20 20 20 20 70 75 74 34 62  0, 4);.    put4b
14f5c 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77  yte(findOverflow
14f5d 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 70 61 72  Cell(pParent,par
14f5e 65 6e 74 49 64 78 29 2c 20 70 50 61 67 65 2d 3e  entIdx), pPage->
14f5f 70 67 6e 6f 29 3b 0a 20 20 20 20 70 75 74 34 62  pgno);.    put4b
14f60 79 74 65 28 26 70 50 61 72 65 6e 74 2d 3e 61 44  yte(&pParent->aD
14f61 61 74 61 5b 70 50 61 72 65 6e 74 2d 3e 68 64 72  ata[pParent->hdr
14f62 4f 66 66 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 4e  Offset+8], pgnoN
14f63 65 77 29 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 49  ew);.  .    /* I
14f64 66 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74  f this is an aut
14f65 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
14f66 65 2c 20 75 70 64 61 74 65 20 74 68 65 20 70 6f  e, update the po
14f67 69 6e 74 65 72 20 6d 61 70 0a 20 20 20 20 2a 2a  inter map.    **
14f68 20 77 69 74 68 20 65 6e 74 72 69 65 73 20 66 6f   with entries fo
14f69 72 20 74 68 65 20 6e 65 77 20 70 61 67 65 2c 20  r the new page, 
14f6a 61 6e 64 20 61 6e 79 20 70 6f 69 6e 74 65 72 20  and any pointer 
14f6b 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 2a 2a  from the .    **
14f6c 20 63 65 6c 6c 20 6f 6e 20 74 68 65 20 70 61 67   cell on the pag
14f6d 65 20 74 6f 20 61 6e 20 6f 76 65 72 66 6c 6f 77  e to an overflow
14f6e 20 70 61 67 65 2e 0a 20 20 20 20 2a 2f 0a 20 20   page..    */.  
14f6f 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
14f70 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  UM ){.      rc =
14f71 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20   ptrmapPut(pBt, 
14f72 70 67 6e 6f 4e 65 77 2c 20 50 54 52 4d 41 50 5f  pgnoNew, PTRMAP_
14f73 42 54 52 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e  BTREE, pParent->
14f74 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  pgno);.      if(
14f75 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
14f76 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 70  {.        rc = p
14f77 74 72 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65  trmapPutOvfl(pNe
14f78 77 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20  w, 0);.      }. 
14f79 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 52 65 6c     }..    /* Rel
14f7a 65 61 73 65 20 74 68 65 20 72 65 66 65 72 65 6e  ease the referen
14f7b 63 65 20 74 6f 20 74 68 65 20 6e 65 77 20 70 61  ce to the new pa
14f7c 67 65 2e 20 2a 2f 0a 20 20 20 20 72 65 6c 65 61  ge. */.    relea
14f7d 73 65 50 61 67 65 28 70 4e 65 77 29 3b 0a 20 20  sePage(pNew);.  
14f7e 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73 20  }..  /* At this 
14f7f 70 6f 69 6e 74 20 74 68 65 20 70 50 61 67 65 2d  point the pPage-
14f80 3e 6e 46 72 65 65 20 76 61 72 69 61 62 6c 65 20  >nFree variable 
14f81 69 73 20 6e 6f 74 20 73 65 74 20 63 6f 72 72 65  is not set corre
14f82 63 74 6c 79 20 77 69 74 68 0a 20 20 2a 2a 20 72  ctly with.  ** r
14f83 65 73 70 65 63 74 20 74 6f 20 74 68 65 20 63 6f  espect to the co
14f84 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 70 61 67  ntent of the pag
14f85 65 20 28 62 65 63 61 75 73 65 20 69 74 20 77 61  e (because it wa
14f86 73 20 73 65 74 20 74 6f 20 30 20 62 79 20 0a 20  s set to 0 by . 
14f87 20 2a 2a 20 69 6e 73 65 72 74 43 65 6c 6c 29 2e   ** insertCell).
14f88 20 53 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33   So call sqlite3
14f89 42 74 72 65 65 49 6e 69 74 50 61 67 65 28 29 20  BtreeInitPage() 
14f8a 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 69 74 20  to make sure it 
14f8b 69 73 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 2e  is.  ** correct.
14f8c 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
14f8d 68 61 73 20 74 6f 20 62 65 20 64 6f 6e 65 20 65  has to be done e
14f8e 76 65 6e 20 69 66 20 61 6e 20 65 72 72 6f 72 20  ven if an error 
14f8f 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e 65 64  will be returned
14f90 2e 20 4e 6f 72 6d 61 6c 6c 79 2c 20 69 66 0a 20  . Normally, if. 
14f91 20 2a 2a 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   ** an error occ
14f92 75 72 73 20 64 75 72 69 6e 67 20 74 72 65 65 20  urs during tree 
14f93 62 61 6c 61 6e 63 69 6e 67 2c 20 74 68 65 20 63  balancing, the c
14f94 6f 6e 74 65 6e 74 73 20 6f 66 20 4d 65 6d 50 61  ontents of MemPa
14f95 67 65 20 61 72 65 0a 20 20 2a 2a 20 6e 6f 74 20  ge are.  ** not 
14f96 69 6d 70 6f 72 74 61 6e 74 2c 20 61 73 20 74 68  important, as th
14f97 65 79 20 77 69 6c 6c 20 62 65 20 72 65 63 61 6c  ey will be recal
14f98 63 75 6c 61 74 65 64 20 77 68 65 6e 20 74 68 65  culated when the
14f99 20 70 61 67 65 20 69 73 20 72 6f 6c 6c 65 64 0a   page is rolled.
14f9a 20 20 2a 2a 20 62 61 63 6b 2e 20 42 75 74 20 68    ** back. But h
14f9b 65 72 65 2c 20 69 6e 20 62 61 6c 61 6e 63 65 5f  ere, in balance_
14f9c 71 75 69 63 6b 28 29 2c 20 69 74 20 69 73 20 70  quick(), it is p
14f9d 6f 73 73 69 62 6c 65 20 74 68 61 74 20 70 50 61  ossible that pPa
14f9e 67 65 20 68 61 73 20 0a 20 20 2a 2a 20 6e 6f 74  ge has .  ** not
14f9f 20 79 65 74 20 62 65 65 6e 20 6d 61 72 6b 65 64   yet been marked
14fa0 20 64 69 72 74 79 20 6f 72 20 77 72 69 74 74 65   dirty or writte
14fa1 6e 20 69 6e 74 6f 20 74 68 65 20 6a 6f 75 72 6e  n into the journ
14fa2 61 6c 20 66 69 6c 65 2e 20 54 68 65 72 65 66 6f  al file. Therefo
14fa3 72 65 0a 20 20 2a 2a 20 69 74 20 77 69 6c 6c 20  re.  ** it will 
14fa4 6e 6f 74 20 62 65 20 72 6f 6c 6c 65 64 20 62 61  not be rolled ba
14fa5 63 6b 20 61 6e 64 20 73 6f 20 69 74 20 69 73 20  ck and so it is 
14fa6 69 6d 70 6f 72 74 61 6e 74 20 74 6f 20 6d 61 6b  important to mak
14fa7 65 20 73 75 72 65 20 74 68 61 74 0a 20 20 2a 2a  e sure that.  **
14fa8 20 74 68 65 20 70 61 67 65 20 64 61 74 61 20 61   the page data a
14fa9 6e 64 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 4d  nd contents of M
14faa 65 6d 50 61 67 65 20 61 72 65 20 63 6f 6e 73 69  emPage are consi
14fab 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 50  stent..  */.  pP
14fac 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b  age->isInit = 0;
14fad 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 49  .  sqlite3BtreeI
14fae 6e 69 74 50 61 67 65 28 70 50 61 67 65 29 3b 0a  nitPage(pPage);.
14faf 0a 20 20 2f 2a 20 49 66 20 65 76 65 72 79 74 68  .  /* If everyth
14fb0 69 6e 67 20 65 6c 73 65 20 73 75 63 63 65 65 64  ing else succeed
14fb1 65 64 2c 20 62 61 6c 61 6e 63 65 20 74 68 65 20  ed, balance the 
14fb2 70 61 72 65 6e 74 20 70 61 67 65 2c 20 69 6e 20  parent page, in 
14fb3 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 64  .  ** case the d
14fb4 69 76 69 64 65 72 20 63 65 6c 6c 20 69 6e 73 65  ivider cell inse
14fb5 72 74 65 64 20 63 61 75 73 65 64 20 69 74 20 74  rted caused it t
14fb6 6f 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c  o become overful
14fb7 6c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63  l..  */.  if( rc
14fb8 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
14fb9 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70     releasePage(p
14fba 50 61 67 65 29 3b 0a 20 20 20 20 70 43 75 72 2d  Page);.    pCur-
14fbb 3e 69 50 61 67 65 2d 2d 3b 0a 20 20 20 20 72 63  >iPage--;.    rc
14fbc 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72 2c   = balance(pCur,
14fbd 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72   0);.  }.  retur
14fbe 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  n rc;.}.#endif /
14fbf 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55  * SQLITE_OMIT_QU
14fc0 49 43 4b 42 41 4c 41 4e 43 45 20 2a 2f 0a 0a 2f  ICKBALANCE */../
14fc1 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
14fc2 65 20 72 65 64 69 73 74 72 69 62 75 74 65 73 20  e redistributes 
14fc3 43 65 6c 6c 73 20 6f 6e 20 70 50 61 67 65 20 61  Cells on pPage a
14fc4 6e 64 20 75 70 20 74 6f 20 4e 4e 2a 32 20 73 69  nd up to NN*2 si
14fc5 62 6c 69 6e 67 73 0a 2a 2a 20 6f 66 20 70 50 61  blings.** of pPa
14fc6 67 65 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 70  ge so that all p
14fc7 61 67 65 73 20 68 61 76 65 20 61 62 6f 75 74 20  ages have about 
14fc8 74 68 65 20 73 61 6d 65 20 61 6d 6f 75 6e 74 20  the same amount 
14fc9 6f 66 20 66 72 65 65 20 73 70 61 63 65 2e 0a 2a  of free space..*
14fca 2a 20 55 73 75 61 6c 6c 79 20 4e 4e 20 73 69 62  * Usually NN sib
14fcb 6c 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 20  lings on either 
14fcc 73 69 64 65 20 6f 66 20 70 50 61 67 65 20 69 73  side of pPage is
14fcd 20 75 73 65 64 20 69 6e 20 74 68 65 20 62 61 6c   used in the bal
14fce 61 6e 63 69 6e 67 2c 0a 2a 2a 20 74 68 6f 75 67  ancing,.** thoug
14fcf 68 20 6d 6f 72 65 20 73 69 62 6c 69 6e 67 73 20  h more siblings 
14fd0 6d 69 67 68 74 20 63 6f 6d 65 20 66 72 6f 6d 20  might come from 
14fd1 6f 6e 65 20 73 69 64 65 20 69 66 20 70 50 61 67  one side if pPag
14fd2 65 20 69 73 20 74 68 65 20 66 69 72 73 74 0a 2a  e is the first.*
14fd3 2a 20 6f 72 20 6c 61 73 74 20 63 68 69 6c 64 20  * or last child 
14fd4 6f 66 20 69 74 73 20 70 61 72 65 6e 74 2e 20 20  of its parent.  
14fd5 49 66 20 70 50 61 67 65 20 68 61 73 20 66 65 77  If pPage has few
14fd6 65 72 20 74 68 61 6e 20 32 2a 4e 4e 20 73 69 62  er than 2*NN sib
14fd7 6c 69 6e 67 73 0a 2a 2a 20 28 73 6f 6d 65 74 68  lings.** (someth
14fd8 69 6e 67 20 77 68 69 63 68 20 63 61 6e 20 6f 6e  ing which can on
14fd9 6c 79 20 68 61 70 70 65 6e 20 69 66 20 70 50 61  ly happen if pPa
14fda 67 65 20 69 73 20 74 68 65 20 72 6f 6f 74 20 70  ge is the root p
14fdb 61 67 65 20 6f 72 20 61 20 0a 2a 2a 20 63 68 69  age or a .** chi
14fdc 6c 64 20 6f 66 20 72 6f 6f 74 29 20 74 68 65 6e  ld of root) then
14fdd 20 61 6c 6c 20 61 76 61 69 6c 61 62 6c 65 20 73   all available s
14fde 69 62 6c 69 6e 67 73 20 70 61 72 74 69 63 69 70  iblings particip
14fdf 61 74 65 20 69 6e 20 74 68 65 20 62 61 6c 61 6e  ate in the balan
14fe0 63 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  cing..**.** The 
14fe1 6e 75 6d 62 65 72 20 6f 66 20 73 69 62 6c 69 6e  number of siblin
14fe2 67 73 20 6f 66 20 70 50 61 67 65 20 6d 69 67 68  gs of pPage migh
14fe3 74 20 62 65 20 69 6e 63 72 65 61 73 65 64 20 6f  t be increased o
14fe4 72 20 64 65 63 72 65 61 73 65 64 20 62 79 20 6f  r decreased by o
14fe5 6e 65 20 6f 72 0a 2a 2a 20 74 77 6f 20 69 6e 20  ne or.** two in 
14fe6 61 6e 20 65 66 66 6f 72 74 20 74 6f 20 6b 65 65  an effort to kee
14fe7 70 20 70 61 67 65 73 20 6e 65 61 72 6c 79 20 66  p pages nearly f
14fe8 75 6c 6c 20 62 75 74 20 6e 6f 74 20 6f 76 65 72  ull but not over
14fe9 20 66 75 6c 6c 2e 20 54 68 65 20 72 6f 6f 74 20   full. The root 
14fea 70 61 67 65 0a 2a 2a 20 69 73 20 73 70 65 63 69  page.** is speci
14feb 61 6c 20 61 6e 64 20 69 73 20 61 6c 6c 6f 77 65  al and is allowe
14fec 64 20 74 6f 20 62 65 20 6e 65 61 72 6c 79 20 65  d to be nearly e
14fed 6d 70 74 79 2e 20 49 66 20 70 50 61 67 65 20 69  mpty. If pPage i
14fee 73 20 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70  s .** the root p
14fef 61 67 65 2c 20 74 68 65 6e 20 74 68 65 20 64 65  age, then the de
14ff0 70 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20  pth of the tree 
14ff1 6d 69 67 68 74 20 62 65 20 69 6e 63 72 65 61 73  might be increas
14ff2 65 64 0a 2a 2a 20 6f 72 20 64 65 63 72 65 61 73  ed.** or decreas
14ff3 65 64 20 62 79 20 6f 6e 65 2c 20 61 73 20 6e 65  ed by one, as ne
14ff4 63 65 73 73 61 72 79 2c 20 74 6f 20 6b 65 65 70  cessary, to keep
14ff5 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 66   the root page f
14ff6 72 6f 6d 20 62 65 69 6e 67 0a 2a 2a 20 6f 76 65  rom being.** ove
14ff7 72 66 75 6c 6c 20 6f 72 20 63 6f 6d 70 6c 65 74  rfull or complet
14ff8 65 6c 79 20 65 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a  ely empty..**.**
14ff9 20 4e 6f 74 65 20 74 68 61 74 20 77 68 65 6e 20   Note that when 
14ffa 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
14ffb 63 61 6c 6c 65 64 2c 20 73 6f 6d 65 20 6f 66 20  called, some of 
14ffc 74 68 65 20 43 65 6c 6c 73 20 6f 6e 20 70 50 61  the Cells on pPa
14ffd 67 65 0a 2a 2a 20 6d 69 67 68 74 20 6e 6f 74 20  ge.** might not 
14ffe 61 63 74 75 61 6c 6c 79 20 62 65 20 73 74 6f 72  actually be stor
14fff 65 64 20 69 6e 20 70 50 61 67 65 2d 3e 61 44 61  ed in pPage->aDa
15000 74 61 5b 5d 2e 20 20 54 68 69 73 20 63 61 6e 20  ta[].  This can 
15001 68 61 70 70 65 6e 0a 2a 2a 20 69 66 20 74 68 65  happen.** if the
15002 20 70 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c   page is overful
15003 6c 2e 20 20 50 61 72 74 20 6f 66 20 74 68 65 20  l.  Part of the 
15004 6a 6f 62 20 6f 66 20 74 68 69 73 20 72 6f 75 74  job of this rout
15005 69 6e 65 20 69 73 20 74 6f 0a 2a 2a 20 6d 61 6b  ine is to.** mak
15006 65 20 73 75 72 65 20 61 6c 6c 20 43 65 6c 6c 73  e sure all Cells
15007 20 66 6f 72 20 70 50 61 67 65 20 6f 6e 63 65 20   for pPage once 
15008 61 67 61 69 6e 20 66 69 74 20 69 6e 20 70 50 61  again fit in pPa
15009 67 65 2d 3e 61 44 61 74 61 5b 5d 2e 0a 2a 2a 0a  ge->aData[]..**.
1500a 2a 2a 20 49 6e 20 74 68 65 20 63 6f 75 72 73 65  ** In the course
1500b 20 6f 66 20 62 61 6c 61 6e 63 69 6e 67 20 74 68   of balancing th
1500c 65 20 73 69 62 6c 69 6e 67 73 20 6f 66 20 70 50  e siblings of pP
1500d 61 67 65 2c 20 74 68 65 20 70 61 72 65 6e 74 20  age, the parent 
1500e 6f 66 20 70 50 61 67 65 0a 2a 2a 20 6d 69 67 68  of pPage.** migh
1500f 74 20 62 65 63 6f 6d 65 20 6f 76 65 72 66 75 6c  t become overful
15010 6c 20 6f 72 20 75 6e 64 65 72 66 75 6c 6c 2e 20  l or underfull. 
15011 20 49 66 20 74 68 61 74 20 68 61 70 70 65 6e 73   If that happens
15012 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
15013 69 6e 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64  ine.** is called
15014 20 72 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20   recursively on 
15015 74 68 65 20 70 61 72 65 6e 74 2e 0a 2a 2a 0a 2a  the parent..**.*
15016 2a 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e  * If this routin
15017 65 20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20  e fails for any 
15018 72 65 61 73 6f 6e 2c 20 69 74 20 6d 69 67 68 74  reason, it might
15019 20 6c 65 61 76 65 20 74 68 65 20 64 61 74 61 62   leave the datab
1501a 61 73 65 0a 2a 2a 20 69 6e 20 61 20 63 6f 72 72  ase.** in a corr
1501b 75 70 74 65 64 20 73 74 61 74 65 2e 20 20 53 6f  upted state.  So
1501c 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
1501d 20 66 61 69 6c 73 2c 20 74 68 65 20 64 61 74 61   fails, the data
1501e 62 61 73 65 20 73 68 6f 75 6c 64 0a 2a 2a 20 62  base should.** b
1501f 65 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 0a 2a  e rolled back..*
15020 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 61 6c  /.static int bal
15021 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 42 74 43  ance_nonroot(BtC
15022 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
15023 4d 65 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 20  MemPage *pPage; 
15024 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15025 54 68 65 20 6f 76 65 72 20 6f 72 20 75 6e 64 65  The over or unde
15026 72 66 75 6c 6c 20 70 61 67 65 20 74 6f 20 62 61  rfull page to ba
15027 6c 61 6e 63 65 20 2a 2f 0a 20 20 4d 65 6d 50 61  lance */.  MemPa
15028 67 65 20 2a 70 50 61 72 65 6e 74 3b 20 20 20 20  ge *pParent;    
15029 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70          /* The p
1502a 61 72 65 6e 74 20 6f 66 20 70 50 61 67 65 20 2a  arent of pPage *
1502b 2f 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  /.  BtShared *pB
1502c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1502d 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 64 61   /* The whole da
1502e 74 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  tabase */.  int 
1502f 6e 43 65 6c 6c 20 3d 20 30 3b 20 20 20 20 20 20  nCell = 0;      
15030 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
15031 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61  er of cells in a
15032 70 43 65 6c 6c 5b 5d 20 2a 2f 0a 20 20 69 6e 74  pCell[] */.  int
15033 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 30 3b 20   nMaxCells = 0; 
15034 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6c 6c            /* All
15035 6f 63 61 74 65 64 20 73 69 7a 65 20 6f 66 20 61  ocated size of a
15036 70 43 65 6c 6c 2c 20 73 7a 43 65 6c 6c 2c 20 61  pCell, szCell, a
15037 46 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  From. */.  int n
15038 4f 6c 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  Old;            
15039 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1503a 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70  r of pages in ap
1503b 4f 6c 64 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  Old[] */.  int n
1503c 4e 65 77 3b 20 20 20 20 20 20 20 20 20 20 20 20  New;            
1503d 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
1503e 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20 61 70  r of pages in ap
1503f 4e 65 77 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e  New[] */.  int n
15040 44 69 76 3b 20 20 20 20 20 20 20 20 20 20 20 20  Div;            
15041 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
15042 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e 20 61 70  r of cells in ap
15043 44 69 76 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  Div[] */.  int i
15044 2c 20 6a 2c 20 6b 3b 20 20 20 20 20 20 20 20 20  , j, k;         
15045 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
15046 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e  counters */.  in
15047 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
15048 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
15049 64 65 78 20 6f 66 20 70 50 61 67 65 20 69 6e 20  dex of pPage in 
1504a 70 50 61 72 65 6e 74 2d 3e 61 43 65 6c 6c 5b 5d  pParent->aCell[]
1504b 20 2a 2f 0a 20 20 69 6e 74 20 6e 78 44 69 76 3b   */.  int nxDiv;
1504c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1504d 20 20 20 2f 2a 20 4e 65 78 74 20 64 69 76 69 64     /* Next divid
1504e 65 72 20 73 6c 6f 74 20 69 6e 20 70 50 61 72 65  er slot in pPare
1504f 6e 74 2d 3e 61 43 65 6c 6c 5b 5d 20 2a 2f 0a 20  nt->aCell[] */. 
15050 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20 20   int rc;        
15051 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15052 20 54 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65   The return code
15053 20 2a 2f 0a 20 20 69 6e 74 20 6c 65 61 66 43 6f   */.  int leafCo
15054 72 72 65 63 74 69 6f 6e 3b 20 20 20 20 20 20 20  rrection;       
15055 20 20 20 2f 2a 20 34 20 69 66 20 70 50 61 67 65     /* 4 if pPage
15056 20 69 73 20 61 20 6c 65 61 66 2e 20 20 30 20 69   is a leaf.  0 i
15057 66 20 6e 6f 74 20 2a 2f 0a 20 20 69 6e 74 20 6c  f not */.  int l
15058 65 61 66 44 61 74 61 3b 20 20 20 20 20 20 20 20  eafData;        
15059 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
1505a 69 66 20 70 50 61 67 65 20 69 73 20 61 20 6c 65  if pPage is a le
1505b 61 66 20 6f 66 20 61 20 4c 45 41 46 44 41 54 41  af of a LEAFDATA
1505c 20 74 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75   tree */.  int u
1505d 73 61 62 6c 65 53 70 61 63 65 3b 20 20 20 20 20  sableSpace;     
1505e 20 20 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73          /* Bytes
1505f 20 69 6e 20 70 50 61 67 65 20 62 65 79 6f 6e 64   in pPage beyond
15060 20 74 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20   the header */. 
15061 20 69 6e 74 20 70 61 67 65 46 6c 61 67 73 3b 20   int pageFlags; 
15062 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
15063 20 56 61 6c 75 65 20 6f 66 20 70 50 61 67 65 2d   Value of pPage-
15064 3e 61 44 61 74 61 5b 30 5d 20 2a 2f 0a 20 20 69  >aData[0] */.  i
15065 6e 74 20 73 75 62 74 6f 74 61 6c 3b 20 20 20 20  nt subtotal;    
15066 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
15067 75 62 74 6f 74 61 6c 20 6f 66 20 62 79 74 65 73  ubtotal of bytes
15068 20 69 6e 20 63 65 6c 6c 73 20 6f 6e 20 6f 6e 65   in cells on one
15069 20 70 61 67 65 20 2a 2f 0a 20 20 69 6e 74 20 69   page */.  int i
1506a 53 70 61 63 65 31 20 3d 20 30 3b 20 20 20 20 20  Space1 = 0;     
1506b 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
1506c 20 75 6e 75 73 65 64 20 62 79 74 65 20 6f 66 20   unused byte of 
1506d 61 53 70 61 63 65 31 5b 5d 20 2a 2f 0a 20 20 69  aSpace1[] */.  i
1506e 6e 74 20 69 53 70 61 63 65 32 20 3d 20 30 3b 20  nt iSpace2 = 0; 
1506f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46              /* F
15070 69 72 73 74 20 75 6e 75 73 65 64 20 62 79 74 65  irst unused byte
15071 20 6f 66 20 61 53 70 61 63 65 32 5b 5d 20 2a 2f   of aSpace2[] */
15072 0a 20 20 69 6e 74 20 73 7a 53 63 72 61 74 63 68  .  int szScratch
15073 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
15074 2f 2a 20 53 69 7a 65 20 6f 66 20 73 63 72 61 74  /* Size of scrat
15075 63 68 20 6d 65 6d 6f 72 79 20 72 65 71 75 65 73  ch memory reques
15076 74 65 64 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65  ted */.  MemPage
15077 20 2a 61 70 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20   *apOld[NB];    
15078 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65 20 61        /* pPage a
15079 6e 64 20 75 70 20 74 6f 20 74 77 6f 20 73 69 62  nd up to two sib
1507a 6c 69 6e 67 73 20 2a 2f 0a 20 20 50 67 6e 6f 20  lings */.  Pgno 
1507b 70 67 6e 6f 4f 6c 64 5b 4e 42 5d 3b 20 20 20 20  pgnoOld[NB];    
1507c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20          /* Page 
1507d 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63 68  numbers for each
1507e 20 70 61 67 65 20 69 6e 20 61 70 4f 6c 64 5b 5d   page in apOld[]
1507f 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a 61   */.  MemPage *a
15080 70 43 6f 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20  pCopy[NB];      
15081 20 20 20 2f 2a 20 50 72 69 76 61 74 65 20 63 6f     /* Private co
15082 70 69 65 73 20 6f 66 20 61 70 4f 6c 64 5b 5d 20  pies of apOld[] 
15083 70 61 67 65 73 20 2a 2f 0a 20 20 4d 65 6d 50 61  pages */.  MemPa
15084 67 65 20 2a 61 70 4e 65 77 5b 4e 42 2b 32 5d 3b  ge *apNew[NB+2];
15085 20 20 20 20 20 20 20 20 2f 2a 20 70 50 61 67 65          /* pPage
15086 20 61 6e 64 20 75 70 20 74 6f 20 4e 42 20 73 69   and up to NB si
15087 62 6c 69 6e 67 73 20 61 66 74 65 72 20 62 61 6c  blings after bal
15088 61 6e 63 69 6e 67 20 2a 2f 0a 20 20 50 67 6e 6f  ancing */.  Pgno
15089 20 70 67 6e 6f 4e 65 77 5b 4e 42 2b 32 5d 3b 20   pgnoNew[NB+2]; 
1508a 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65           /* Page
1508b 20 6e 75 6d 62 65 72 73 20 66 6f 72 20 65 61 63   numbers for eac
1508c 68 20 70 61 67 65 20 69 6e 20 61 70 4e 65 77 5b  h page in apNew[
1508d 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 70 44 69 76  ] */.  u8 *apDiv
1508e 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20 20 20  [NB];           
1508f 20 20 20 20 2f 2a 20 44 69 76 69 64 65 72 20 63      /* Divider c
15090 65 6c 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20  ells in pParent 
15091 2a 2f 0a 20 20 69 6e 74 20 63 6e 74 4e 65 77 5b  */.  int cntNew[
15092 4e 42 2b 32 5d 3b 20 20 20 20 20 20 20 20 20 20  NB+2];          
15093 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 61 43    /* Index in aC
15094 65 6c 6c 5b 5d 20 6f 66 20 63 65 6c 6c 20 61 66  ell[] of cell af
15095 74 65 72 20 69 2d 74 68 20 70 61 67 65 20 2a 2f  ter i-th page */
15096 0a 20 20 69 6e 74 20 73 7a 4e 65 77 5b 4e 42 2b  .  int szNew[NB+
15097 32 5d 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  2];             
15098 2f 2a 20 43 6f 6d 62 69 6e 65 64 20 73 69 7a 65  /* Combined size
15099 20 6f 66 20 63 65 6c 6c 73 20 70 6c 61 63 65 20   of cells place 
1509a 6f 6e 20 69 2d 74 68 20 70 61 67 65 20 2a 2f 0a  on i-th page */.
1509b 20 20 75 38 20 2a 2a 61 70 43 65 6c 6c 20 3d 20    u8 **apCell = 
1509c 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
1509d 2a 20 41 6c 6c 20 63 65 6c 6c 73 20 62 65 67 69  * All cells begi
1509e 6e 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20  n balanced */.  
1509f 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20  u16 *szCell;    
150a0 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
150a1 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c  Local size of al
150a2 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70 43 65 6c  l cells in apCel
150a3 6c 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61 43 6f  l[] */.  u8 *aCo
150a4 70 79 5b 4e 42 5d 3b 20 20 20 20 20 20 20 20 20  py[NB];         
150a5 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 68 6f 6c  /* Space for hol
150a6 64 69 6e 67 20 64 61 74 61 20 6f 66 20 61 70 43  ding data of apC
150a7 6f 70 79 5b 5d 20 2a 2f 0a 20 20 75 38 20 2a 61  opy[] */.  u8 *a
150a8 53 70 61 63 65 31 3b 20 20 20 20 20 20 20 20 20  Space1;         
150a9 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20 63    /* Space for c
150aa 6f 70 69 65 73 20 6f 66 20 64 69 76 69 64 65 72  opies of divider
150ab 73 20 63 65 6c 6c 73 20 62 65 66 6f 72 65 20 62  s cells before b
150ac 61 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a  alance */.  u8 *
150ad 61 53 70 61 63 65 32 20 3d 20 30 3b 20 20 20 20  aSpace2 = 0;    
150ae 20 20 20 2f 2a 20 53 70 61 63 65 20 66 6f 72 20     /* Space for 
150af 6f 76 65 72 66 6c 6f 77 20 64 69 76 69 64 65 72  overflow divider
150b0 73 20 63 65 6c 6c 73 20 61 66 74 65 72 20 62 61  s cells after ba
150b1 6c 61 6e 63 65 20 2a 2f 0a 20 20 75 38 20 2a 61  lance */.  u8 *a
150b2 46 72 6f 6d 20 3d 20 30 3b 0a 0a 20 20 70 50 61  From = 0;..  pPa
150b3 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67  ge = pCur->apPag
150b4 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a  e[pCur->iPage];.
150b5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
150b6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
150b7 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
150b8 29 3b 0a 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70  );.  VVA_ONLY( p
150b9 43 75 72 2d 3e 70 61 67 65 73 53 68 75 66 66 6c  Cur->pagesShuffl
150ba 65 64 20 3d 20 31 20 29 3b 0a 0a 20 20 2f 2a 20  ed = 1 );..  /* 
150bb 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 70  .  ** Find the p
150bc 61 72 65 6e 74 20 70 61 67 65 2e 0a 20 20 2a 2f  arent page..  */
150bd 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
150be 3e 69 50 61 67 65 3e 30 20 29 3b 0a 20 20 61 73  >iPage>0 );.  as
150bf 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
150c0 6e 69 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nit );.  assert(
150c1 20 73 71 6c 69 74 65 33 50 61 67 65 72 49 73 77   sqlite3PagerIsw
150c2 72 69 74 65 61 62 6c 65 28 70 50 61 67 65 2d 3e  riteable(pPage->
150c3 70 44 62 50 61 67 65 29 20 7c 7c 20 70 50 61 67  pDbPage) || pPag
150c4 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 31 20  e->nOverflow==1 
150c5 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67 65  );.  pBt = pPage
150c6 2d 3e 70 42 74 3b 0a 20 20 70 50 61 72 65 6e 74  ->pBt;.  pParent
150c7 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b   = pCur->apPage[
150c8 70 43 75 72 2d 3e 69 50 61 67 65 2d 31 5d 3b 0a  pCur->iPage-1];.
150c9 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
150ca 74 20 29 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  t );.  if( SQLIT
150cb 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
150cc 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
150cd 61 72 65 6e 74 2d 3e 70 44 62 50 61 67 65 29 29  arent->pDbPage))
150ce 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
150cf 63 3b 0a 20 20 7d 0a 0a 20 20 54 52 41 43 45 28  c;.  }..  TRACE(
150d0 28 22 42 41 4c 41 4e 43 45 3a 20 62 65 67 69 6e  ("BALANCE: begin
150d1 20 70 61 67 65 20 25 64 20 63 68 69 6c 64 20 6f   page %d child o
150d2 66 20 25 64 5c 6e 22 2c 20 70 50 61 67 65 2d 3e  f %d\n", pPage->
150d3 70 67 6e 6f 2c 20 70 50 61 72 65 6e 74 2d 3e 70  pgno, pParent->p
150d4 67 6e 6f 29 29 3b 0a 0a 23 69 66 6e 64 65 66 20  gno));..#ifndef 
150d5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 51 55 49 43  SQLITE_OMIT_QUIC
150d6 4b 42 41 4c 41 4e 43 45 0a 20 20 2f 2a 0a 20 20  KBALANCE.  /*.  
150d7 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 63 61 73  ** A special cas
150d8 65 3a 20 20 49 66 20 61 20 6e 65 77 20 65 6e 74  e:  If a new ent
150d9 72 79 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e  ry has just been
150da 20 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 61   inserted into a
150db 0a 20 20 2a 2a 20 74 61 62 6c 65 20 28 74 68 61  .  ** table (tha
150dc 74 20 69 73 2c 20 61 20 62 74 72 65 65 20 77 69  t is, a btree wi
150dd 74 68 20 69 6e 74 65 67 65 72 20 6b 65 79 73 20  th integer keys 
150de 61 6e 64 20 61 6c 6c 20 64 61 74 61 20 61 74 20  and all data at 
150df 74 68 65 20 6c 65 61 76 65 73 29 0a 20 20 2a 2a  the leaves).  **
150e0 20 61 6e 64 20 74 68 65 20 6e 65 77 20 65 6e 74   and the new ent
150e1 72 79 20 69 73 20 74 68 65 20 72 69 67 68 74 2d  ry is the right-
150e2 6d 6f 73 74 20 65 6e 74 72 79 20 69 6e 20 74 68  most entry in th
150e3 65 20 74 72 65 65 20 28 69 74 20 68 61 73 20 74  e tree (it has t
150e4 68 65 0a 20 20 2a 2a 20 6c 61 72 67 65 73 74 20  he.  ** largest 
150e5 6b 65 79 29 20 74 68 65 6e 20 75 73 65 20 74 68  key) then use th
150e6 65 20 73 70 65 63 69 61 6c 20 62 61 6c 61 6e 63  e special balanc
150e7 65 5f 71 75 69 63 6b 28 29 20 72 6f 75 74 69 6e  e_quick() routin
150e8 65 20 66 6f 72 0a 20 20 2a 2a 20 62 61 6c 61 6e  e for.  ** balan
150e9 63 69 6e 67 2e 20 20 62 61 6c 61 6e 63 65 5f 71  cing.  balance_q
150ea 75 69 63 6b 28 29 20 69 73 20 6d 75 63 68 20 66  uick() is much f
150eb 61 73 74 65 72 20 61 6e 64 20 72 65 73 75 6c 74  aster and result
150ec 73 20 69 6e 20 61 20 74 69 67 68 74 65 72 0a 20  s in a tighter. 
150ed 20 2a 2a 20 70 61 63 6b 69 6e 67 20 6f 66 20 64   ** packing of d
150ee 61 74 61 20 69 6e 20 74 68 65 20 63 6f 6d 6d 6f  ata in the commo
150ef 6e 20 63 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  n case..  */.  i
150f0 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 26  f( pPage->leaf &
150f1 26 0a 20 20 20 20 20 20 70 50 61 67 65 2d 3e 69  &.      pPage->i
150f2 6e 74 4b 65 79 20 26 26 0a 20 20 20 20 20 20 70  ntKey &&.      p
150f3 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d  Page->nOverflow=
150f4 3d 31 20 26 26 0a 20 20 20 20 20 20 70 50 61 67  =1 &&.      pPag
150f5 65 2d 3e 61 4f 76 66 6c 5b 30 5d 2e 69 64 78 3d  e->aOvfl[0].idx=
150f6 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26  =pPage->nCell &&
150f7 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e  .      pParent->
150f8 70 67 6e 6f 21 3d 31 20 26 26 0a 20 20 20 20 20  pgno!=1 &&.     
150f9 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72 65   get4byte(&pPare
150fa 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65 6e  nt->aData[pParen
150fb 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d 29  t->hdrOffset+8])
150fc 3d 3d 70 50 61 67 65 2d 3e 70 67 6e 6f 0a 20 20  ==pPage->pgno.  
150fd 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  ){.    assert( p
150fe 50 61 67 65 2d 3e 69 6e 74 4b 65 79 20 29 3b 0a  Page->intKey );.
150ff 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 54 4f      /*.    ** TO
15100 44 4f 3a 20 43 68 65 63 6b 20 74 68 65 20 73 69  DO: Check the si
15101 62 6c 69 6e 67 73 20 74 6f 20 74 68 65 20 6c 65  blings to the le
15102 66 74 20 6f 66 20 70 50 61 67 65 2e 20 49 74 20  ft of pPage. It 
15103 6d 61 79 20 62 65 20 74 68 61 74 0a 20 20 20 20  may be that.    
15104 2a 2a 20 74 68 65 79 20 61 72 65 20 6e 6f 74 20  ** they are not 
15105 66 75 6c 6c 20 61 6e 64 20 6e 6f 20 6e 65 77 20  full and no new 
15106 70 61 67 65 20 69 73 20 72 65 71 75 69 72 65 64  page is required
15107 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74  ..    */.    ret
15108 75 72 6e 20 62 61 6c 61 6e 63 65 5f 71 75 69 63  urn balance_quic
15109 6b 28 70 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e  k(pCur);.  }.#en
1510a 64 69 66 0a 0a 20 20 69 66 28 20 53 51 4c 49 54  dif..  if( SQLIT
1510b 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 71 6c 69  E_OK!=(rc = sqli
1510c 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50  te3PagerWrite(pP
1510d 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 20 29  age->pDbPage)) )
1510e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
1510f 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
15110 46 69 6e 64 20 74 68 65 20 63 65 6c 6c 20 69 6e  Find the cell in
15111 20 74 68 65 20 70 61 72 65 6e 74 20 70 61 67 65   the parent page
15112 20 77 68 6f 73 65 20 6c 65 66 74 20 63 68 69 6c   whose left chil
15113 64 20 70 6f 69 6e 74 73 20 62 61 63 6b 0a 20 20  d points back.  
15114 2a 2a 20 74 6f 20 70 50 61 67 65 2e 20 20 54 68  ** to pPage.  Th
15115 65 20 22 69 64 78 22 20 76 61 72 69 61 62 6c 65  e "idx" variable
15116 20 69 73 20 74 68 65 20 69 6e 64 65 78 20 6f 66   is the index of
15117 20 74 68 61 74 20 63 65 6c 6c 2e 20 20 49 66 20   that cell.  If 
15118 70 50 61 67 65 0a 20 20 2a 2a 20 69 73 20 74 68  pPage.  ** is th
15119 65 20 72 69 67 68 74 6d 6f 73 74 20 63 68 69 6c  e rightmost chil
1511a 64 20 6f 66 20 70 50 61 72 65 6e 74 20 74 68 65  d of pParent the
1511b 6e 20 73 65 74 20 69 64 78 20 74 6f 20 70 50 61  n set idx to pPa
1511c 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 0a 20 20 2a  rent->nCell .  *
1511d 2f 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e  /.  idx = pCur->
1511e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1511f 65 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 50 61  e-1];.  assertPa
15120 72 65 6e 74 49 6e 64 65 78 28 70 50 61 72 65 6e  rentIndex(pParen
15121 74 2c 20 69 64 78 2c 20 70 50 61 67 65 2d 3e 70  t, idx, pPage->p
15122 67 6e 6f 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  gno);..  /*.  **
15123 20 49 6e 69 74 69 61 6c 69 7a 65 20 76 61 72 69   Initialize vari
15124 61 62 6c 65 73 20 73 6f 20 74 68 61 74 20 69 74  ables so that it
15125 20 77 69 6c 6c 20 62 65 20 73 61 66 65 20 74 6f   will be safe to
15126 20 6a 75 6d 70 0a 20 20 2a 2a 20 64 69 72 65 63   jump.  ** direc
15127 74 6c 79 20 74 6f 20 62 61 6c 61 6e 63 65 5f 63  tly to balance_c
15128 6c 65 61 6e 75 70 20 61 74 20 61 6e 79 20 6d 6f  leanup at any mo
15129 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 6e 4f 6c  ment..  */.  nOl
1512a 64 20 3d 20 6e 4e 65 77 20 3d 20 30 3b 0a 0a 20  d = nNew = 0;.. 
1512b 20 2f 2a 0a 20 20 2a 2a 20 46 69 6e 64 20 73 69   /*.  ** Find si
1512c 62 6c 69 6e 67 20 70 61 67 65 73 20 74 6f 20 70  bling pages to p
1512d 50 61 67 65 20 61 6e 64 20 74 68 65 20 63 65 6c  Page and the cel
1512e 6c 73 20 69 6e 20 70 50 61 72 65 6e 74 20 74 68  ls in pParent th
1512f 61 74 20 64 69 76 69 64 65 0a 20 20 2a 2a 20 74  at divide.  ** t
15130 68 65 20 73 69 62 6c 69 6e 67 73 2e 20 20 41 6e  he siblings.  An
15131 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65   attempt is made
15132 20 74 6f 20 66 69 6e 64 20 4e 4e 20 73 69 62 6c   to find NN sibl
15133 69 6e 67 73 20 6f 6e 20 65 69 74 68 65 72 0a 20  ings on either. 
15134 20 2a 2a 20 73 69 64 65 20 6f 66 20 70 50 61 67   ** side of pPag
15135 65 2e 20 20 4d 6f 72 65 20 73 69 62 6c 69 6e 67  e.  More sibling
15136 73 20 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d  s are taken from
15137 20 6f 6e 65 20 73 69 64 65 2c 20 68 6f 77 65 76   one side, howev
15138 65 72 2c 20 69 66 0a 20 20 2a 2a 20 70 50 61 67  er, if.  ** pPag
15139 65 20 74 68 65 72 65 20 61 72 65 20 66 65 77 65  e there are fewe
1513a 72 20 74 68 61 6e 20 4e 4e 20 73 69 62 6c 69 6e  r than NN siblin
1513b 67 73 20 6f 6e 20 74 68 65 20 6f 74 68 65 72 20  gs on the other 
1513c 73 69 64 65 2e 20 20 49 66 20 70 50 61 72 65 6e  side.  If pParen
1513d 74 0a 20 20 2a 2a 20 68 61 73 20 4e 42 20 6f 72  t.  ** has NB or
1513e 20 66 65 77 65 72 20 63 68 69 6c 64 72 65 6e 20   fewer children 
1513f 74 68 65 6e 20 61 6c 6c 20 63 68 69 6c 64 72 65  then all childre
15140 6e 20 6f 66 20 70 50 61 72 65 6e 74 20 61 72 65  n of pParent are
15141 20 74 61 6b 65 6e 2e 0a 20 20 2a 2f 0a 20 20 6e   taken..  */.  n
15142 78 44 69 76 20 3d 20 69 64 78 20 2d 20 4e 4e 3b  xDiv = idx - NN;
15143 0a 20 20 69 66 28 20 6e 78 44 69 76 20 2b 20 4e  .  if( nxDiv + N
15144 42 20 3e 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65  B > pParent->nCe
15145 6c 6c 20 29 7b 0a 20 20 20 20 6e 78 44 69 76 20  ll ){.    nxDiv 
15146 3d 20 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c  = pParent->nCell
15147 20 2d 20 4e 42 20 2b 20 31 3b 0a 20 20 7d 0a 20   - NB + 1;.  }. 
15148 20 69 66 28 20 6e 78 44 69 76 3c 30 20 29 7b 0a   if( nxDiv<0 ){.
15149 20 20 20 20 6e 78 44 69 76 20 3d 20 30 3b 0a 20      nxDiv = 0;. 
1514a 20 7d 0a 20 20 6e 44 69 76 20 3d 20 30 3b 0a 20   }.  nDiv = 0;. 
1514b 20 66 6f 72 28 69 3d 30 2c 20 6b 3d 6e 78 44 69   for(i=0, k=nxDi
1514c 76 3b 20 69 3c 4e 42 3b 20 69 2b 2b 2c 20 6b 2b  v; i<NB; i++, k+
1514d 2b 29 7b 0a 20 20 20 20 69 66 28 20 6b 3c 70 50  +){.    if( k<pP
1514e 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a  arent->nCell ){.
1514f 20 20 20 20 20 20 61 70 44 69 76 5b 69 5d 20 3d        apDiv[i] =
15150 20 66 69 6e 64 43 65 6c 6c 28 70 50 61 72 65 6e   findCell(pParen
15151 74 2c 20 6b 29 3b 0a 20 20 20 20 20 20 6e 44 69  t, k);.      nDi
15152 76 2b 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72  v++;.      asser
15153 74 28 20 21 70 50 61 72 65 6e 74 2d 3e 6c 65 61  t( !pParent->lea
15154 66 20 29 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4f  f );.      pgnoO
15155 6c 64 5b 69 5d 20 3d 20 67 65 74 34 62 79 74 65  ld[i] = get4byte
15156 28 61 70 44 69 76 5b 69 5d 29 3b 0a 20 20 20 20  (apDiv[i]);.    
15157 7d 65 6c 73 65 20 69 66 28 20 6b 3d 3d 70 50 61  }else if( k==pPa
15158 72 65 6e 74 2d 3e 6e 43 65 6c 6c 20 29 7b 0a 20  rent->nCell ){. 
15159 20 20 20 20 20 70 67 6e 6f 4f 6c 64 5b 69 5d 20       pgnoOld[i] 
1515a 3d 20 67 65 74 34 62 79 74 65 28 26 70 50 61 72  = get4byte(&pPar
1515b 65 6e 74 2d 3e 61 44 61 74 61 5b 70 50 61 72 65  ent->aData[pPare
1515c 6e 74 2d 3e 68 64 72 4f 66 66 73 65 74 2b 38 5d  nt->hdrOffset+8]
1515d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
1515e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1515f 0a 20 20 20 20 72 63 20 3d 20 67 65 74 41 6e 64  .    rc = getAnd
15160 49 6e 69 74 50 61 67 65 28 70 42 74 2c 20 70 67  InitPage(pBt, pg
15161 6e 6f 4f 6c 64 5b 69 5d 2c 20 26 61 70 4f 6c 64  noOld[i], &apOld
15162 5b 69 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63  [i]);.    if( rc
15163 20 29 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f   ) goto balance_
15164 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 2f 2a 20  cleanup;.    /* 
15165 61 70 4f 6c 64 5b 69 5d 2d 3e 69 64 78 50 61 72  apOld[i]->idxPar
15166 65 6e 74 20 3d 20 6b 3b 20 2a 2f 0a 20 20 20 20  ent = k; */.    
15167 61 70 43 6f 70 79 5b 69 5d 20 3d 20 30 3b 0a 20  apCopy[i] = 0;. 
15168 20 20 20 61 73 73 65 72 74 28 20 69 3d 3d 6e 4f     assert( i==nO
15169 6c 64 20 29 3b 0a 20 20 20 20 6e 4f 6c 64 2b 2b  ld );.    nOld++
1516a 3b 0a 20 20 20 20 6e 4d 61 78 43 65 6c 6c 73 20  ;.    nMaxCells 
1516b 2b 3d 20 31 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e  += 1+apOld[i]->n
1516c 43 65 6c 6c 2b 61 70 4f 6c 64 5b 69 5d 2d 3e 6e  Cell+apOld[i]->n
1516d 4f 76 65 72 66 6c 6f 77 3b 0a 20 20 7d 0a 0a 20  Overflow;.  }.. 
1516e 20 2f 2a 20 4d 61 6b 65 20 6e 4d 61 78 43 65 6c   /* Make nMaxCel
1516f 6c 73 20 61 20 6d 75 6c 74 69 70 6c 65 20 6f 66  ls a multiple of
15170 20 34 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 70   4 in order to p
15171 72 65 73 65 72 76 65 20 38 2d 62 79 74 65 0a 20  reserve 8-byte. 
15172 20 2a 2a 20 61 6c 69 67 6e 6d 65 6e 74 20 2a 2f   ** alignment */
15173 0a 20 20 6e 4d 61 78 43 65 6c 6c 73 20 3d 20 28  .  nMaxCells = (
15174 6e 4d 61 78 43 65 6c 6c 73 20 2b 20 33 29 26 7e  nMaxCells + 3)&~
15175 33 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c  3;..  /*.  ** Al
15176 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f 72  locate space for
15177 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72   memory structur
15178 65 73 0a 20 20 2a 2f 0a 20 20 73 7a 53 63 72 61  es.  */.  szScra
15179 74 63 68 20 3d 0a 20 20 20 20 20 20 20 6e 4d 61  tch =.       nMa
1517a 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 38  xCells*sizeof(u8
1517b 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *)              
1517c 20 20 20 20 20 20 20 20 20 2f 2a 20 61 70 43 65           /* apCe
1517d 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 6e 4d 61  ll */.     + nMa
1517e 78 43 65 6c 6c 73 2a 73 69 7a 65 6f 66 28 75 31  xCells*sizeof(u1
1517f 36 29 20 20 20 20 20 20 20 20 20 20 20 20 20 20  6)              
15180 20 20 20 20 20 20 20 20 20 2f 2a 20 73 7a 43 65           /* szCe
15181 6c 6c 20 2a 2f 0a 20 20 20 20 20 2b 20 28 52 4f  ll */.     + (RO
15182 55 4e 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50  UND8(sizeof(MemP
15183 61 67 65 29 29 2b 70 42 74 2d 3e 70 61 67 65 53  age))+pBt->pageS
15184 69 7a 65 29 2a 4e 42 20 20 2f 2a 20 61 43 6f 70  ize)*NB  /* aCop
15185 79 20 2a 2f 0a 20 20 20 20 20 2b 20 70 42 74 2d  y */.     + pBt-
15186 3e 70 61 67 65 53 69 7a 65 20 20 20 20 20 20 20  >pageSize       
15187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15188 20 20 20 20 20 20 20 20 2f 2a 20 61 53 70 61 63          /* aSpac
15189 65 31 20 2a 2f 0a 20 20 20 20 20 2b 20 28 49 53  e1 */.     + (IS
1518a 41 55 54 4f 56 41 43 55 55 4d 20 3f 20 6e 4d 61  AUTOVACUUM ? nMa
1518b 78 43 65 6c 6c 73 20 3a 20 30 29 3b 20 20 20 20  xCells : 0);    
1518c 20 20 20 20 20 20 20 20 20 2f 2a 20 61 46 72 6f           /* aFro
1518d 6d 20 2a 2f 0a 20 20 61 70 43 65 6c 6c 20 3d 20  m */.  apCell = 
1518e 73 71 6c 69 74 65 33 53 63 72 61 74 63 68 4d 61  sqlite3ScratchMa
1518f 6c 6c 6f 63 28 20 73 7a 53 63 72 61 74 63 68 20  lloc( szScratch 
15190 29 3b 20 0a 20 20 69 66 28 20 61 70 43 65 6c 6c  ); .  if( apCell
15191 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  ==0 ){.    rc = 
15192 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
15193 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
15194 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 73 7a  leanup;.  }.  sz
15195 43 65 6c 6c 20 3d 20 28 75 31 36 2a 29 26 61 70  Cell = (u16*)&ap
15196 43 65 6c 6c 5b 6e 4d 61 78 43 65 6c 6c 73 5d 3b  Cell[nMaxCells];
15197 0a 20 20 61 43 6f 70 79 5b 30 5d 20 3d 20 28 75  .  aCopy[0] = (u
15198 38 2a 29 26 73 7a 43 65 6c 6c 5b 6e 4d 61 78 43  8*)&szCell[nMaxC
15199 65 6c 6c 73 5d 3b 0a 20 20 61 73 73 65 72 74 28  ells];.  assert(
1519a 20 28 28 61 43 6f 70 79 5b 30 5d 20 2d 20 28 75   ((aCopy[0] - (u
1519b 38 2a 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d  8*)apCell) & 7)=
1519c 3d 30 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20  =0 ); /* 8-byte 
1519d 61 6c 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72  alignment requir
1519e 65 64 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 31 3b  ed */.  for(i=1;
1519f 20 69 3c 4e 42 3b 20 69 2b 2b 29 7b 0a 20 20 20   i<NB; i++){.   
151a0 20 61 43 6f 70 79 5b 69 5d 20 3d 20 26 61 43 6f   aCopy[i] = &aCo
151a1 70 79 5b 69 2d 31 5d 5b 70 42 74 2d 3e 70 61 67  py[i-1][pBt->pag
151a2 65 53 69 7a 65 2b 52 4f 55 4e 44 38 28 73 69 7a  eSize+ROUND8(siz
151a3 65 6f 66 28 4d 65 6d 50 61 67 65 29 29 5d 3b 0a  eof(MemPage))];.
151a4 20 20 20 20 61 73 73 65 72 74 28 20 28 28 61 43      assert( ((aC
151a5 6f 70 79 5b 69 5d 20 2d 20 28 75 38 2a 29 61 70  opy[i] - (u8*)ap
151a6 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30 20 29 3b  Cell) & 7)==0 );
151a7 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c 69 67 6e   /* 8-byte align
151a8 6d 65 6e 74 20 72 65 71 75 69 72 65 64 20 2a 2f  ment required */
151a9 0a 20 20 7d 0a 20 20 61 53 70 61 63 65 31 20 3d  .  }.  aSpace1 =
151aa 20 26 61 43 6f 70 79 5b 4e 42 2d 31 5d 5b 70 42   &aCopy[NB-1][pB
151ab 74 2d 3e 70 61 67 65 53 69 7a 65 2b 52 4f 55 4e  t->pageSize+ROUN
151ac 44 38 28 73 69 7a 65 6f 66 28 4d 65 6d 50 61 67  D8(sizeof(MemPag
151ad 65 29 29 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  e))];.  assert( 
151ae 28 28 61 53 70 61 63 65 31 20 2d 20 28 75 38 2a  ((aSpace1 - (u8*
151af 29 61 70 43 65 6c 6c 29 20 26 20 37 29 3d 3d 30  )apCell) & 7)==0
151b0 20 29 3b 20 2f 2a 20 38 2d 62 79 74 65 20 61 6c   ); /* 8-byte al
151b1 69 67 6e 6d 65 6e 74 20 72 65 71 75 69 72 65 64  ignment required
151b2 20 2a 2f 0a 20 20 69 66 28 20 49 53 41 55 54 4f   */.  if( ISAUTO
151b3 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 61 46  VACUUM ){.    aF
151b4 72 6f 6d 20 3d 20 26 61 53 70 61 63 65 31 5b 70  rom = &aSpace1[p
151b5 42 74 2d 3e 70 61 67 65 53 69 7a 65 5d 3b 0a 20  Bt->pageSize];. 
151b6 20 7d 0a 20 20 61 53 70 61 63 65 32 20 3d 20 73   }.  aSpace2 = s
151b7 71 6c 69 74 65 33 50 61 67 65 4d 61 6c 6c 6f 63  qlite3PageMalloc
151b8 28 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b  (pBt->pageSize);
151b9 0a 20 20 69 66 28 20 61 53 70 61 63 65 32 3d 3d  .  if( aSpace2==
151ba 30 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  0 ){.    rc = SQ
151bb 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20  LITE_NOMEM;.    
151bc 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65  goto balance_cle
151bd 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 0a 20 20 2f  anup;.  }.  .  /
151be 2a 0a 20 20 2a 2a 20 4d 61 6b 65 20 63 6f 70 69  *.  ** Make copi
151bf 65 73 20 6f 66 20 74 68 65 20 63 6f 6e 74 65 6e  es of the conten
151c0 74 20 6f 66 20 70 50 61 67 65 20 61 6e 64 20 69  t of pPage and i
151c1 74 73 20 73 69 62 6c 69 6e 67 73 20 69 6e 74 6f  ts siblings into
151c2 20 61 4f 6c 64 5b 5d 2e 0a 20 20 2a 2a 20 54 68   aOld[]..  ** Th
151c3 65 20 72 65 73 74 20 6f 66 20 74 68 69 73 20 66  e rest of this f
151c4 75 6e 63 74 69 6f 6e 20 77 69 6c 6c 20 75 73 65  unction will use
151c5 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65 20 63   data from the c
151c6 6f 70 69 65 73 20 72 61 74 68 65 72 0a 20 20 2a  opies rather.  *
151c7 2a 20 74 68 61 74 20 74 68 65 20 6f 72 69 67 69  * that the origi
151c8 6e 61 6c 20 70 61 67 65 73 20 73 69 6e 63 65 20  nal pages since 
151c9 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 70 61 67  the original pag
151ca 65 73 20 77 69 6c 6c 20 62 65 20 69 6e 20 74 68  es will be in th
151cb 65 0a 20 20 2a 2a 20 70 72 6f 63 65 73 73 20 6f  e.  ** process o
151cc 66 20 62 65 69 6e 67 20 6f 76 65 72 77 72 69 74  f being overwrit
151cd 74 65 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  ten..  */.  for(
151ce 69 3d 30 3b 20 69 3c 6e 4f 6c 64 3b 20 69 2b 2b  i=0; i<nOld; i++
151cf 29 7b 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a  ){.    MemPage *
151d0 70 20 3d 20 61 70 43 6f 70 79 5b 69 5d 20 3d 20  p = apCopy[i] = 
151d1 28 4d 65 6d 50 61 67 65 2a 29 61 43 6f 70 79 5b  (MemPage*)aCopy[
151d2 69 5d 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  i];.    memcpy(p
151d3 2c 20 61 70 4f 6c 64 5b 69 5d 2c 20 73 69 7a 65  , apOld[i], size
151d4 6f 66 28 4d 65 6d 50 61 67 65 29 29 3b 0a 20 20  of(MemPage));.  
151d5 20 20 70 2d 3e 61 44 61 74 61 20 3d 20 28 76 6f    p->aData = (vo
151d6 69 64 2a 29 26 70 5b 31 5d 3b 0a 20 20 20 20 6d  id*)&p[1];.    m
151d7 65 6d 63 70 79 28 70 2d 3e 61 44 61 74 61 2c 20  emcpy(p->aData, 
151d8 61 70 4f 6c 64 5b 69 5d 2d 3e 61 44 61 74 61 2c  apOld[i]->aData,
151d9 20 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 29 3b   pBt->pageSize);
151da 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
151db 4c 6f 61 64 20 70 6f 69 6e 74 65 72 73 20 74 6f  Load pointers to
151dc 20 61 6c 6c 20 63 65 6c 6c 73 20 6f 6e 20 73 69   all cells on si
151dd 62 6c 69 6e 67 20 70 61 67 65 73 20 61 6e 64 20  bling pages and 
151de 74 68 65 20 64 69 76 69 64 65 72 20 63 65 6c 6c  the divider cell
151df 73 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20  s.  ** into the 
151e0 6c 6f 63 61 6c 20 61 70 43 65 6c 6c 5b 5d 20 61  local apCell[] a
151e1 72 72 61 79 2e 20 20 4d 61 6b 65 20 63 6f 70 69  rray.  Make copi
151e2 65 73 20 6f 66 20 74 68 65 20 64 69 76 69 64 65  es of the divide
151e3 72 20 63 65 6c 6c 73 0a 20 20 2a 2a 20 69 6e 74  r cells.  ** int
151e4 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e 65 64  o space obtained
151e5 20 66 6f 72 6d 20 61 53 70 61 63 65 31 5b 5d 20   form aSpace1[] 
151e6 61 6e 64 20 72 65 6d 6f 76 65 20 74 68 65 20 74  and remove the t
151e7 68 65 20 64 69 76 69 64 65 72 20 43 65 6c 6c 73  he divider Cells
151e8 0a 20 20 2a 2a 20 66 72 6f 6d 20 70 50 61 72 65  .  ** from pPare
151e9 6e 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  nt..  **.  ** If
151ea 20 74 68 65 20 73 69 62 6c 69 6e 67 73 20 61 72   the siblings ar
151eb 65 20 6f 6e 20 6c 65 61 66 20 70 61 67 65 73 2c  e on leaf pages,
151ec 20 74 68 65 6e 20 74 68 65 20 63 68 69 6c 64 20   then the child 
151ed 70 6f 69 6e 74 65 72 73 20 6f 66 20 74 68 65 0a  pointers of the.
151ee 20 20 2a 2a 20 64 69 76 69 64 65 72 20 63 65 6c    ** divider cel
151ef 6c 73 20 61 72 65 20 73 74 72 69 70 70 65 64 20  ls are stripped 
151f0 66 72 6f 6d 20 74 68 65 20 63 65 6c 6c 73 20 62  from the cells b
151f1 65 66 6f 72 65 20 74 68 65 79 20 61 72 65 20 63  efore they are c
151f2 6f 70 69 65 64 0a 20 20 2a 2a 20 69 6e 74 6f 20  opied.  ** into 
151f3 61 53 70 61 63 65 31 5b 5d 2e 20 20 49 6e 20 74  aSpace1[].  In t
151f4 68 69 73 20 77 61 79 2c 20 61 6c 6c 20 63 65 6c  his way, all cel
151f5 6c 73 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61  ls in apCell[] a
151f6 72 65 20 77 69 74 68 6f 75 74 0a 20 20 2a 2a 20  re without.  ** 
151f7 63 68 69 6c 64 20 70 6f 69 6e 74 65 72 73 2e 20  child pointers. 
151f8 20 49 66 20 73 69 62 6c 69 6e 67 73 20 61 72 65   If siblings are
151f9 20 6e 6f 74 20 6c 65 61 76 65 73 2c 20 74 68 65   not leaves, the
151fa 6e 20 61 6c 6c 20 63 65 6c 6c 20 69 6e 0a 20 20  n all cell in.  
151fb 2a 2a 20 61 70 43 65 6c 6c 5b 5d 20 69 6e 63 6c  ** apCell[] incl
151fc 75 64 65 20 63 68 69 6c 64 20 70 6f 69 6e 74 65  ude child pointe
151fd 72 73 2e 20 20 45 69 74 68 65 72 20 77 61 79 2c  rs.  Either way,
151fe 20 61 6c 6c 20 63 65 6c 6c 73 20 69 6e 20 61 70   all cells in ap
151ff 43 65 6c 6c 5b 5d 0a 20 20 2a 2a 20 61 72 65 20  Cell[].  ** are 
15200 61 6c 69 6b 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  alike..  **.  **
15201 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3a   leafCorrection:
15202 20 20 34 20 69 66 20 70 50 61 67 65 20 69 73 20    4 if pPage is 
15203 61 20 6c 65 61 66 2e 20 20 30 20 69 66 20 70 50  a leaf.  0 if pP
15204 61 67 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61  age is not a lea
15205 66 2e 0a 20 20 2a 2a 20 20 20 20 20 20 20 6c 65  f..  **       le
15206 61 66 44 61 74 61 3a 20 20 31 20 69 66 20 70 50  afData:  1 if pP
15207 61 67 65 20 68 6f 6c 64 73 20 6b 65 79 2b 64 61  age holds key+da
15208 74 61 20 61 6e 64 20 70 50 61 72 65 6e 74 20 68  ta and pParent h
15209 6f 6c 64 73 20 6f 6e 6c 79 20 6b 65 79 73 2e 0a  olds only keys..
1520a 20 20 2a 2f 0a 20 20 6e 43 65 6c 6c 20 3d 20 30    */.  nCell = 0
1520b 3b 0a 20 20 6c 65 61 66 43 6f 72 72 65 63 74 69  ;.  leafCorrecti
1520c 6f 6e 20 3d 20 70 50 61 67 65 2d 3e 6c 65 61 66  on = pPage->leaf
1520d 2a 34 3b 0a 20 20 6c 65 61 66 44 61 74 61 20 3d  *4;.  leafData =
1520e 20 70 50 61 67 65 2d 3e 68 61 73 44 61 74 61 3b   pPage->hasData;
1520f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f  .  for(i=0; i<nO
15210 6c 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65  ld; i++){.    Me
15211 6d 50 61 67 65 20 2a 70 4f 6c 64 20 3d 20 61 70  mPage *pOld = ap
15212 43 6f 70 79 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Copy[i];.    int
15213 20 6c 69 6d 69 74 20 3d 20 70 4f 6c 64 2d 3e 6e   limit = pOld->n
15214 43 65 6c 6c 2b 70 4f 6c 64 2d 3e 6e 4f 76 65 72  Cell+pOld->nOver
15215 66 6c 6f 77 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  flow;.    for(j=
15216 30 3b 20 6a 3c 6c 69 6d 69 74 3b 20 6a 2b 2b 29  0; j<limit; j++)
15217 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
15218 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c 6c 73 20  nCell<nMaxCells 
15219 29 3b 0a 20 20 20 20 20 20 61 70 43 65 6c 6c 5b  );.      apCell[
1521a 6e 43 65 6c 6c 5d 20 3d 20 66 69 6e 64 4f 76 65  nCell] = findOve
1521b 72 66 6c 6f 77 43 65 6c 6c 28 70 4f 6c 64 2c 20  rflowCell(pOld, 
1521c 6a 29 3b 0a 20 20 20 20 20 20 73 7a 43 65 6c 6c  j);.      szCell
1521d 5b 6e 43 65 6c 6c 5d 20 3d 20 63 65 6c 6c 53 69  [nCell] = cellSi
1521e 7a 65 50 74 72 28 70 4f 6c 64 2c 20 61 70 43 65  zePtr(pOld, apCe
1521f 6c 6c 5b 6e 43 65 6c 6c 5d 29 3b 0a 20 20 20 20  ll[nCell]);.    
15220 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55    if( ISAUTOVACU
15221 55 4d 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  UM ){.        in
15222 74 20 61 3b 0a 20 20 20 20 20 20 20 20 61 46 72  t a;.        aFr
15223 6f 6d 5b 6e 43 65 6c 6c 5d 20 3d 20 69 3b 0a 20  om[nCell] = i;. 
15224 20 20 20 20 20 20 20 66 6f 72 28 61 3d 30 3b 20         for(a=0; 
15225 61 3c 70 4f 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f  a<pOld->nOverflo
15226 77 3b 20 61 2b 2b 29 7b 0a 20 20 20 20 20 20 20  w; a++){.       
15227 20 20 20 69 66 28 20 70 4f 6c 64 2d 3e 61 4f 76     if( pOld->aOv
15228 66 6c 5b 61 5d 2e 70 43 65 6c 6c 3d 3d 61 70 43  fl[a].pCell==apC
15229 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 29 7b 0a 20 20  ell[nCell] ){.  
1522a 20 20 20 20 20 20 20 20 20 20 61 46 72 6f 6d 5b            aFrom[
1522b 6e 43 65 6c 6c 5d 20 3d 20 30 78 46 46 3b 0a 20  nCell] = 0xFF;. 
1522c 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b             break
1522d 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
1522e 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1522f 20 20 20 20 20 20 6e 43 65 6c 6c 2b 2b 3b 0a 20        nCell++;. 
15230 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 3c 6e     }.    if( i<n
15231 4f 6c 64 2d 31 20 29 7b 0a 20 20 20 20 20 20 75  Old-1 ){.      u
15232 31 36 20 73 7a 20 3d 20 63 65 6c 6c 53 69 7a 65  16 sz = cellSize
15233 50 74 72 28 70 50 61 72 65 6e 74 2c 20 61 70 44  Ptr(pParent, apD
15234 69 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20 69 66  iv[i]);.      if
15235 28 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20  ( leafData ){.  
15236 20 20 20 20 20 20 2f 2a 20 57 69 74 68 20 74 68        /* With th
15237 65 20 4c 45 41 46 44 41 54 41 20 66 6c 61 67 2c  e LEAFDATA flag,
15238 20 70 50 61 72 65 6e 74 20 63 65 6c 6c 73 20 68   pParent cells h
15239 6f 6c 64 20 6f 6e 6c 79 20 49 4e 54 4b 45 59 73  old only INTKEYs
1523a 20 74 68 61 74 0a 20 20 20 20 20 20 20 20 2a 2a   that.        **
1523b 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73 20   are duplicates 
1523c 6f 66 20 6b 65 79 73 20 6f 6e 20 74 68 65 20 63  of keys on the c
1523d 68 69 6c 64 20 70 61 67 65 73 2e 20 20 57 65 20  hild pages.  We 
1523e 6e 65 65 64 20 74 6f 20 72 65 6d 6f 76 65 0a 20  need to remove. 
1523f 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20 64 69         ** the di
15240 76 69 64 65 72 20 63 65 6c 6c 73 20 66 72 6f 6d  vider cells from
15241 20 70 50 61 72 65 6e 74 2c 20 62 75 74 20 74 68   pParent, but th
15242 65 20 64 69 76 69 64 65 72 73 20 63 65 6c 6c 73  e dividers cells
15243 20 61 72 65 20 6e 6f 74 0a 20 20 20 20 20 20 20   are not.       
15244 20 2a 2a 20 61 64 64 65 64 20 74 6f 20 61 70 43   ** added to apC
15245 65 6c 6c 5b 5d 20 62 65 63 61 75 73 65 20 74 68  ell[] because th
15246 65 79 20 61 72 65 20 64 75 70 6c 69 63 61 74 65  ey are duplicate
15247 73 20 6f 66 20 63 68 69 6c 64 20 63 65 6c 6c 73  s of child cells
15248 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
15249 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70 50       dropCell(pP
1524a 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c 20 73 7a  arent, nxDiv, sz
1524b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1524c 20 20 20 20 20 20 20 20 75 38 20 2a 70 54 65 6d          u8 *pTem
1524d 70 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  p;.        asser
1524e 74 28 20 6e 43 65 6c 6c 3c 6e 4d 61 78 43 65 6c  t( nCell<nMaxCel
1524f 6c 73 20 29 3b 0a 20 20 20 20 20 20 20 20 73 7a  ls );.        sz
15250 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 73 7a  Cell[nCell] = sz
15251 3b 0a 20 20 20 20 20 20 20 20 70 54 65 6d 70 20  ;.        pTemp 
15252 3d 20 26 61 53 70 61 63 65 31 5b 69 53 70 61 63  = &aSpace1[iSpac
15253 65 31 5d 3b 0a 20 20 20 20 20 20 20 20 69 53 70  e1];.        iSp
15254 61 63 65 31 20 2b 3d 20 73 7a 3b 0a 20 20 20 20  ace1 += sz;.    
15255 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d      assert( sz<=
15256 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20  pBt->pageSize/4 
15257 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
15258 74 28 20 69 53 70 61 63 65 31 3c 3d 70 42 74 2d  t( iSpace1<=pBt-
15259 3e 70 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20  >pageSize );.   
1525a 20 20 20 20 20 6d 65 6d 63 70 79 28 70 54 65 6d       memcpy(pTem
1525b 70 2c 20 61 70 44 69 76 5b 69 5d 2c 20 73 7a 29  p, apDiv[i], sz)
1525c 3b 0a 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c  ;.        apCell
1525d 5b 6e 43 65 6c 6c 5d 20 3d 20 70 54 65 6d 70 2b  [nCell] = pTemp+
1525e 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3b 0a  leafCorrection;.
1525f 20 20 20 20 20 20 20 20 69 66 28 20 49 53 41 55          if( ISAU
15260 54 4f 56 41 43 55 55 4d 20 29 7b 0a 20 20 20 20  TOVACUUM ){.    
15261 20 20 20 20 20 20 61 46 72 6f 6d 5b 6e 43 65 6c        aFrom[nCel
15262 6c 5d 20 3d 20 30 78 46 46 3b 0a 20 20 20 20 20  l] = 0xFF;.     
15263 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 72 6f     }.        dro
15264 70 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e  pCell(pParent, n
15265 78 44 69 76 2c 20 73 7a 29 3b 0a 20 20 20 20 20  xDiv, sz);.     
15266 20 20 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d     szCell[nCell]
15267 20 2d 3d 20 6c 65 61 66 43 6f 72 72 65 63 74 69   -= leafCorrecti
15268 6f 6e 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  on;.        asse
15269 72 74 28 20 67 65 74 34 62 79 74 65 28 70 54 65  rt( get4byte(pTe
1526a 6d 70 29 3d 3d 70 67 6e 6f 4f 6c 64 5b 69 5d 20  mp)==pgnoOld[i] 
1526b 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  );.        if( !
1526c 70 4f 6c 64 2d 3e 6c 65 61 66 20 29 7b 0a 20 20  pOld->leaf ){.  
1526d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1526e 6c 65 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d  leafCorrection==
1526f 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 2f  0 );.          /
15270 2a 20 54 68 65 20 72 69 67 68 74 20 70 6f 69 6e  * The right poin
15271 74 65 72 20 6f 66 20 74 68 65 20 63 68 69 6c 64  ter of the child
15272 20 70 61 67 65 20 70 4f 6c 64 20 62 65 63 6f 6d   page pOld becom
15273 65 73 20 74 68 65 20 6c 65 66 74 0a 20 20 20 20  es the left.    
15274 20 20 20 20 20 20 2a 2a 20 70 6f 69 6e 74 65 72        ** pointer
15275 20 6f 66 20 74 68 65 20 64 69 76 69 64 65 72 20   of the divider 
15276 63 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20  cell */.        
15277 20 20 6d 65 6d 63 70 79 28 61 70 43 65 6c 6c 5b    memcpy(apCell[
15278 6e 43 65 6c 6c 5d 2c 20 26 70 4f 6c 64 2d 3e 61  nCell], &pOld->a
15279 44 61 74 61 5b 70 4f 6c 64 2d 3e 68 64 72 4f 66  Data[pOld->hdrOf
1527a 66 73 65 74 2b 38 5d 2c 20 34 29 3b 0a 20 20 20  fset+8], 4);.   
1527b 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1527c 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6c 65        assert( le
1527d 61 66 43 6f 72 72 65 63 74 69 6f 6e 3d 3d 34 20  afCorrection==4 
1527e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  );.          if(
1527f 20 73 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 3c 34   szCell[nCell]<4
15280 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
15281 2f 2a 20 44 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20  /* Do not allow 
15282 61 6e 79 20 63 65 6c 6c 73 20 73 6d 61 6c 6c 65  any cells smalle
15283 72 20 74 68 61 6e 20 34 20 62 79 74 65 73 2e 20  r than 4 bytes. 
15284 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  */.            s
15285 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d 20 3d 20 34  zCell[nCell] = 4
15286 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
15287 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
15288 6e 43 65 6c 6c 2b 2b 3b 0a 20 20 20 20 20 20 7d  nCell++;.      }
15289 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
1528a 0a 20 20 2a 2a 20 46 69 67 75 72 65 20 6f 75 74  .  ** Figure out
1528b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 70   the number of p
1528c 61 67 65 73 20 6e 65 65 64 65 64 20 74 6f 20 68  ages needed to h
1528d 6f 6c 64 20 61 6c 6c 20 6e 43 65 6c 6c 20 63 65  old all nCell ce
1528e 6c 6c 73 2e 0a 20 20 2a 2a 20 53 74 6f 72 65 20  lls..  ** Store 
1528f 74 68 69 73 20 6e 75 6d 62 65 72 20 69 6e 20 22  this number in "
15290 6b 22 2e 20 20 41 6c 73 6f 20 63 6f 6d 70 75 74  k".  Also comput
15291 65 20 73 7a 4e 65 77 5b 5d 20 77 68 69 63 68 20  e szNew[] which 
15292 69 73 20 74 68 65 20 74 6f 74 61 6c 0a 20 20 2a  is the total.  *
15293 2a 20 73 69 7a 65 20 6f 66 20 61 6c 6c 20 63 65  * size of all ce
15294 6c 6c 73 20 6f 6e 20 74 68 65 20 69 2d 74 68 20  lls on the i-th 
15295 70 61 67 65 20 61 6e 64 20 63 6e 74 4e 65 77 5b  page and cntNew[
15296 5d 20 77 68 69 63 68 20 69 73 20 74 68 65 20 69  ] which is the i
15297 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20 61 70 43  ndex.  ** in apC
15298 65 6c 6c 5b 5d 20 6f 66 20 74 68 65 20 63 65 6c  ell[] of the cel
15299 6c 20 74 68 61 74 20 64 69 76 69 64 65 73 20 70  l that divides p
1529a 61 67 65 20 69 20 66 72 6f 6d 20 70 61 67 65 20  age i from page 
1529b 69 2b 31 2e 20 20 0a 20 20 2a 2a 20 63 6e 74 4e  i+1.  .  ** cntN
1529c 65 77 5b 6b 5d 20 73 68 6f 75 6c 64 20 65 71 75  ew[k] should equ
1529d 61 6c 20 6e 43 65 6c 6c 2e 0a 20 20 2a 2a 0a 20  al nCell..  **. 
1529e 20 2a 2a 20 56 61 6c 75 65 73 20 63 6f 6d 70 75   ** Values compu
1529f 74 65 64 20 62 79 20 74 68 69 73 20 62 6c 6f 63  ted by this bloc
152a0 6b 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  k:.  **.  **    
152a1 20 20 20 20 20 20 20 6b 3a 20 54 68 65 20 74 6f         k: The to
152a2 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 73 69  tal number of si
152a3 62 6c 69 6e 67 20 70 61 67 65 73 0a 20 20 2a 2a  bling pages.  **
152a4 20 20 20 20 73 7a 4e 65 77 5b 69 5d 3a 20 53 70      szNew[i]: Sp
152a5 61 63 65 64 20 75 73 65 64 20 6f 6e 20 74 68 65  aced used on the
152a6 20 69 2d 74 68 20 73 69 62 6c 69 6e 67 20 70 61   i-th sibling pa
152a7 67 65 2e 0a 20 20 2a 2a 20 20 20 63 6e 74 4e 65  ge..  **   cntNe
152a8 77 5b 69 5d 3a 20 49 6e 64 65 78 20 69 6e 20 61  w[i]: Index in a
152a9 70 43 65 6c 6c 5b 5d 20 61 6e 64 20 73 7a 43 65  pCell[] and szCe
152aa 6c 6c 5b 5d 20 66 6f 72 20 74 68 65 20 66 69 72  ll[] for the fir
152ab 73 74 20 63 65 6c 6c 20 74 6f 0a 20 20 2a 2a 20  st cell to.  ** 
152ac 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65               the
152ad 20 72 69 67 68 74 20 6f 66 20 74 68 65 20 69 2d   right of the i-
152ae 74 68 20 73 69 62 6c 69 6e 67 20 70 61 67 65 2e  th sibling page.
152af 0a 20 20 2a 2a 20 75 73 61 62 6c 65 53 70 61 63  .  ** usableSpac
152b0 65 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  e: Number of byt
152b1 65 73 20 6f 66 20 73 70 61 63 65 20 61 76 61 69  es of space avai
152b2 6c 61 62 6c 65 20 6f 6e 20 65 61 63 68 20 73 69  lable on each si
152b3 62 6c 69 6e 67 2e 0a 20 20 2a 2a 20 0a 20 20 2a  bling..  ** .  *
152b4 2f 0a 20 20 75 73 61 62 6c 65 53 70 61 63 65 20  /.  usableSpace 
152b5 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  = pBt->usableSiz
152b6 65 20 2d 20 31 32 20 2b 20 6c 65 61 66 43 6f 72  e - 12 + leafCor
152b7 72 65 63 74 69 6f 6e 3b 0a 20 20 66 6f 72 28 73  rection;.  for(s
152b8 75 62 74 6f 74 61 6c 3d 6b 3d 69 3d 30 3b 20 69  ubtotal=k=i=0; i
152b9 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20  <nCell; i++){.  
152ba 20 20 61 73 73 65 72 74 28 20 69 3c 6e 4d 61 78    assert( i<nMax
152bb 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 73 75 62  Cells );.    sub
152bc 74 6f 74 61 6c 20 2b 3d 20 73 7a 43 65 6c 6c 5b  total += szCell[
152bd 69 5d 20 2b 20 32 3b 0a 20 20 20 20 69 66 28 20  i] + 2;.    if( 
152be 73 75 62 74 6f 74 61 6c 20 3e 20 75 73 61 62 6c  subtotal > usabl
152bf 65 53 70 61 63 65 20 29 7b 0a 20 20 20 20 20 20  eSpace ){.      
152c0 73 7a 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f  szNew[k] = subto
152c1 74 61 6c 20 2d 20 73 7a 43 65 6c 6c 5b 69 5d 3b  tal - szCell[i];
152c2 0a 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 6b 5d  .      cntNew[k]
152c3 20 3d 20 69 3b 0a 20 20 20 20 20 20 69 66 28 20   = i;.      if( 
152c4 6c 65 61 66 44 61 74 61 20 29 7b 20 69 2d 2d 3b  leafData ){ i--;
152c5 20 7d 0a 20 20 20 20 20 20 73 75 62 74 6f 74 61   }.      subtota
152c6 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 6b 2b 2b  l = 0;.      k++
152c7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 7a  ;.    }.  }.  sz
152c8 4e 65 77 5b 6b 5d 20 3d 20 73 75 62 74 6f 74 61  New[k] = subtota
152c9 6c 3b 0a 20 20 63 6e 74 4e 65 77 5b 6b 5d 20 3d  l;.  cntNew[k] =
152ca 20 6e 43 65 6c 6c 3b 0a 20 20 6b 2b 2b 3b 0a 0a   nCell;.  k++;..
152cb 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65 20 70 61    /*.  ** The pa
152cc 63 6b 69 6e 67 20 63 6f 6d 70 75 74 65 64 20 62  cking computed b
152cd 79 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 62  y the previous b
152ce 6c 6f 63 6b 20 69 73 20 62 69 61 73 65 64 20 74  lock is biased t
152cf 6f 77 61 72 64 20 74 68 65 20 73 69 62 6c 69 6e  oward the siblin
152d0 67 73 0a 20 20 2a 2a 20 6f 6e 20 74 68 65 20 6c  gs.  ** on the l
152d1 65 66 74 20 73 69 64 65 2e 20 20 54 68 65 20 6c  eft side.  The l
152d2 65 66 74 20 73 69 62 6c 69 6e 67 73 20 61 72 65  eft siblings are
152d3 20 61 6c 77 61 79 73 20 6e 65 61 72 6c 79 20 66   always nearly f
152d4 75 6c 6c 2c 20 77 68 69 6c 65 20 74 68 65 0a 20  ull, while the. 
152d5 20 2a 2a 20 72 69 67 68 74 2d 6d 6f 73 74 20 73   ** right-most s
152d6 69 62 6c 69 6e 67 20 6d 69 67 68 74 20 62 65 20  ibling might be 
152d7 6e 65 61 72 6c 79 20 65 6d 70 74 79 2e 20 20 54  nearly empty.  T
152d8 68 69 73 20 62 6c 6f 63 6b 20 6f 66 20 63 6f 64  his block of cod
152d9 65 20 61 74 74 65 6d 70 74 73 0a 20 20 2a 2a 20  e attempts.  ** 
152da 74 6f 20 61 64 6a 75 73 74 20 74 68 65 20 70 61  to adjust the pa
152db 63 6b 69 6e 67 20 6f 66 20 73 69 62 6c 69 6e 67  cking of sibling
152dc 73 20 74 6f 20 67 65 74 20 61 20 62 65 74 74 65  s to get a bette
152dd 72 20 62 61 6c 61 6e 63 65 2e 0a 20 20 2a 2a 0a  r balance..  **.
152de 20 20 2a 2a 20 54 68 69 73 20 61 64 6a 75 73 74    ** This adjust
152df 6d 65 6e 74 20 69 73 20 6d 6f 72 65 20 74 68 61  ment is more tha
152e0 6e 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  n an optimizatio
152e1 6e 2e 20 20 54 68 65 20 70 61 63 6b 69 6e 67 20  n.  The packing 
152e2 61 62 6f 76 65 20 6d 69 67 68 74 0a 20 20 2a 2a  above might.  **
152e3 20 62 65 20 73 6f 20 6f 75 74 20 6f 66 20 62 61   be so out of ba
152e4 6c 61 6e 63 65 20 61 73 20 74 6f 20 62 65 20 69  lance as to be i
152e5 6c 6c 65 67 61 6c 2e 20 20 46 6f 72 20 65 78 61  llegal.  For exa
152e6 6d 70 6c 65 2c 20 74 68 65 20 72 69 67 68 74 2d  mple, the right-
152e7 6d 6f 73 74 0a 20 20 2a 2a 20 73 69 62 6c 69 6e  most.  ** siblin
152e8 67 20 6d 69 67 68 74 20 62 65 20 63 6f 6d 70 6c  g might be compl
152e9 65 74 65 6c 79 20 65 6d 70 74 79 2e 20 20 54 68  etely empty.  Th
152ea 69 73 20 61 64 6a 75 73 74 6d 65 6e 74 20 69 73  is adjustment is
152eb 20 6e 6f 74 20 6f 70 74 69 6f 6e 61 6c 2e 0a 20   not optional.. 
152ec 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 6b 2d 31 3b   */.  for(i=k-1;
152ed 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20   i>0; i--){.    
152ee 69 6e 74 20 73 7a 52 69 67 68 74 20 3d 20 73 7a  int szRight = sz
152ef 4e 65 77 5b 69 5d 3b 20 20 2f 2a 20 53 69 7a 65  New[i];  /* Size
152f0 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74   of sibling on t
152f1 68 65 20 72 69 67 68 74 20 2a 2f 0a 20 20 20 20  he right */.    
152f2 69 6e 74 20 73 7a 4c 65 66 74 20 3d 20 73 7a 4e  int szLeft = szN
152f3 65 77 5b 69 2d 31 5d 3b 20 2f 2a 20 53 69 7a 65  ew[i-1]; /* Size
152f4 20 6f 66 20 73 69 62 6c 69 6e 67 20 6f 6e 20 74   of sibling on t
152f5 68 65 20 6c 65 66 74 20 2a 2f 0a 20 20 20 20 69  he left */.    i
152f6 6e 74 20 72 3b 20 20 20 20 20 20 20 20 20 20 20  nt r;           
152f7 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 72     /* Index of r
152f8 69 67 68 74 2d 6d 6f 73 74 20 63 65 6c 6c 20 69  ight-most cell i
152f9 6e 20 6c 65 66 74 20 73 69 62 6c 69 6e 67 20 2a  n left sibling *
152fa 2f 0a 20 20 20 20 69 6e 74 20 64 3b 20 20 20 20  /.    int d;    
152fb 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64            /* Ind
152fc 65 78 20 6f 66 20 66 69 72 73 74 20 63 65 6c 6c  ex of first cell
152fd 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f 66 20   to the left of 
152fe 72 69 67 68 74 20 73 69 62 6c 69 6e 67 20 2a 2f  right sibling */
152ff 0a 0a 20 20 20 20 72 20 3d 20 63 6e 74 4e 65 77  ..    r = cntNew
15300 5b 69 2d 31 5d 20 2d 20 31 3b 0a 20 20 20 20 64  [i-1] - 1;.    d
15301 20 3d 20 72 20 2b 20 31 20 2d 20 6c 65 61 66 44   = r + 1 - leafD
15302 61 74 61 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ata;.    assert(
15303 20 64 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b 0a   d<nMaxCells );.
15304 20 20 20 20 61 73 73 65 72 74 28 20 72 3c 6e 4d      assert( r<nM
15305 61 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 77  axCells );.    w
15306 68 69 6c 65 28 20 73 7a 52 69 67 68 74 3d 3d 30  hile( szRight==0
15307 20 7c 7c 20 73 7a 52 69 67 68 74 2b 73 7a 43 65   || szRight+szCe
15308 6c 6c 5b 64 5d 2b 32 3c 3d 73 7a 4c 65 66 74 2d  ll[d]+2<=szLeft-
15309 28 73 7a 43 65 6c 6c 5b 72 5d 2b 32 29 20 29 7b  (szCell[r]+2) ){
1530a 0a 20 20 20 20 20 20 73 7a 52 69 67 68 74 20 2b  .      szRight +
1530b 3d 20 73 7a 43 65 6c 6c 5b 64 5d 20 2b 20 32 3b  = szCell[d] + 2;
1530c 0a 20 20 20 20 20 20 73 7a 4c 65 66 74 20 2d 3d  .      szLeft -=
1530d 20 73 7a 43 65 6c 6c 5b 72 5d 20 2b 20 32 3b 0a   szCell[r] + 2;.
1530e 20 20 20 20 20 20 63 6e 74 4e 65 77 5b 69 2d 31        cntNew[i-1
1530f 5d 2d 2d 3b 0a 20 20 20 20 20 20 72 20 3d 20 63  ]--;.      r = c
15310 6e 74 4e 65 77 5b 69 2d 31 5d 20 2d 20 31 3b 0a  ntNew[i-1] - 1;.
15311 20 20 20 20 20 20 64 20 3d 20 72 20 2b 20 31 20        d = r + 1 
15312 2d 20 6c 65 61 66 44 61 74 61 3b 0a 20 20 20 20  - leafData;.    
15313 7d 0a 20 20 20 20 73 7a 4e 65 77 5b 69 5d 20 3d  }.    szNew[i] =
15314 20 73 7a 52 69 67 68 74 3b 0a 20 20 20 20 73 7a   szRight;.    sz
15315 4e 65 77 5b 69 2d 31 5d 20 3d 20 73 7a 4c 65 66  New[i-1] = szLef
15316 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 69 74  t;.  }..  /* Eit
15317 68 65 72 20 77 65 20 66 6f 75 6e 64 20 6f 6e 65  her we found one
15318 20 6f 72 20 6d 6f 72 65 20 63 65 6c 6c 73 20 28   or more cells (
15319 63 6e 74 6e 65 77 5b 30 5d 29 3e 30 29 20 6f 72  cntnew[0])>0) or
1531a 20 77 65 20 61 72 65 20 74 68 65 0a 20 20 2a 2a   we are the.  **
1531b 20 61 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20   a virtual root 
1531c 70 61 67 65 2e 20 20 41 20 76 69 72 74 75 61 6c  page.  A virtual
1531d 20 72 6f 6f 74 20 70 61 67 65 20 69 73 20 77 68   root page is wh
1531e 65 6e 20 74 68 65 20 72 65 61 6c 20 72 6f 6f 74  en the real root
1531f 0a 20 20 2a 2a 20 70 61 67 65 20 69 73 20 70 61  .  ** page is pa
15320 67 65 20 31 20 61 6e 64 20 77 65 20 61 72 65 20  ge 1 and we are 
15321 74 68 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 6f  the only child o
15322 66 20 74 68 61 74 20 70 61 67 65 2e 0a 20 20 2a  f that page..  *
15323 2f 0a 20 20 61 73 73 65 72 74 28 20 63 6e 74 4e  /.  assert( cntN
15324 65 77 5b 30 5d 3e 30 20 7c 7c 20 28 70 50 61 72  ew[0]>0 || (pPar
15325 65 6e 74 2d 3e 70 67 6e 6f 3d 3d 31 20 26 26 20  ent->pgno==1 && 
15326 70 50 61 72 65 6e 74 2d 3e 6e 43 65 6c 6c 3d 3d  pParent->nCell==
15327 30 29 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  0) );..  /*.  **
15328 20 41 6c 6c 6f 63 61 74 65 20 6b 20 6e 65 77 20   Allocate k new 
15329 70 61 67 65 73 2e 20 20 52 65 75 73 65 20 6f 6c  pages.  Reuse ol
1532a 64 20 70 61 67 65 73 20 77 68 65 72 65 20 70 6f  d pages where po
1532b 73 73 69 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 61  ssible..  */.  a
1532c 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 70 67  ssert( pPage->pg
1532d 6e 6f 3e 31 20 29 3b 0a 20 20 70 61 67 65 46 6c  no>1 );.  pageFl
1532e 61 67 73 20 3d 20 70 50 61 67 65 2d 3e 61 44 61  ags = pPage->aDa
1532f 74 61 5b 30 5d 3b 0a 20 20 66 6f 72 28 69 3d 30  ta[0];.  for(i=0
15330 3b 20 69 3c 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20  ; i<k; i++){.   
15331 20 4d 65 6d 50 61 67 65 20 2a 70 4e 65 77 3b 0a   MemPage *pNew;.
15332 20 20 20 20 69 66 28 20 69 3c 6e 4f 6c 64 20 29      if( i<nOld )
15333 7b 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 61  {.      pNew = a
15334 70 4e 65 77 5b 69 5d 20 3d 20 61 70 4f 6c 64 5b  pNew[i] = apOld[
15335 69 5d 3b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65  i];.      pgnoNe
15336 77 5b 69 5d 20 3d 20 70 67 6e 6f 4f 6c 64 5b 69  w[i] = pgnoOld[i
15337 5d 3b 0a 20 20 20 20 20 20 61 70 4f 6c 64 5b 69  ];.      apOld[i
15338 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20  ] = 0;.      rc 
15339 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57 72  = sqlite3PagerWr
1533a 69 74 65 28 70 4e 65 77 2d 3e 70 44 62 50 61 67  ite(pNew->pDbPag
1533b 65 29 3b 0a 20 20 20 20 20 20 6e 4e 65 77 2b 2b  e);.      nNew++
1533c 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29  ;.      if( rc )
1533d 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
1533e 65 61 6e 75 70 3b 0a 20 20 20 20 7d 65 6c 73 65  eanup;.    }else
1533f 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
15340 69 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 63 20  i>0 );.      rc 
15341 3d 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50  = allocateBtreeP
15342 61 67 65 28 70 42 74 2c 20 26 70 4e 65 77 2c 20  age(pBt, &pNew, 
15343 26 70 67 6e 6f 4e 65 77 5b 69 5d 2c 20 70 67 6e  &pgnoNew[i], pgn
15344 6f 4e 65 77 5b 69 2d 31 5d 2c 20 30 29 3b 0a 20  oNew[i-1], 0);. 
15345 20 20 20 20 20 69 66 28 20 72 63 20 29 20 67 6f       if( rc ) go
15346 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c 65 61 6e  to balance_clean
15347 75 70 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b  up;.      apNew[
15348 69 5d 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 20  i] = pNew;.     
15349 20 6e 4e 65 77 2b 2b 3b 0a 20 20 20 20 7d 0a 20   nNew++;.    }. 
1534a 20 7d 0a 0a 20 20 2f 2a 20 46 72 65 65 20 61 6e   }..  /* Free an
1534b 79 20 6f 6c 64 20 70 61 67 65 73 20 74 68 61 74  y old pages that
1534c 20 77 65 72 65 20 6e 6f 74 20 72 65 75 73 65 64   were not reused
1534d 20 61 73 20 6e 65 77 20 70 61 67 65 73 2e 0a 20   as new pages.. 
1534e 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 3c 6e   */.  while( i<n
1534f 4f 6c 64 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Old ){.    rc = 
15350 66 72 65 65 50 61 67 65 28 61 70 4f 6c 64 5b 69  freePage(apOld[i
15351 5d 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20 29  ]);.    if( rc )
15352 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
15353 65 61 6e 75 70 3b 0a 20 20 20 20 72 65 6c 65 61  eanup;.    relea
15354 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d 29  sePage(apOld[i])
15355 3b 0a 20 20 20 20 61 70 4f 6c 64 5b 69 5d 20 3d  ;.    apOld[i] =
15356 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a 20 20 7d   0;.    i++;.  }
15357 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 50 75 74 20  ..  /*.  ** Put 
15358 74 68 65 20 6e 65 77 20 70 61 67 65 73 20 69 6e  the new pages in
15359 20 61 63 63 65 6e 64 69 6e 67 20 6f 72 64 65 72   accending order
1535a 2e 20 20 54 68 69 73 20 68 65 6c 70 73 20 74 6f  .  This helps to
1535b 0a 20 20 2a 2a 20 6b 65 65 70 20 65 6e 74 72 69  .  ** keep entri
1535c 65 73 20 69 6e 20 74 68 65 20 64 69 73 6b 20 66  es in the disk f
1535d 69 6c 65 20 69 6e 20 6f 72 64 65 72 20 73 6f 20  ile in order so 
1535e 74 68 61 74 20 61 20 73 63 61 6e 0a 20 20 2a 2a  that a scan.  **
1535f 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 73   of the table is
15360 20 61 20 6c 69 6e 65 61 72 20 73 63 61 6e 20 74   a linear scan t
15361 68 72 6f 75 67 68 20 74 68 65 20 66 69 6c 65 2e  hrough the file.
15362 20 20 54 68 61 74 0a 20 20 2a 2a 20 69 6e 20 74    That.  ** in t
15363 75 72 6e 20 68 65 6c 70 73 20 74 68 65 20 6f 70  urn helps the op
15364 65 72 61 74 69 6e 67 20 73 79 73 74 65 6d 20 74  erating system t
15365 6f 20 64 65 6c 69 76 65 72 20 70 61 67 65 73 0a  o deliver pages.
15366 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 64 69    ** from the di
15367 73 6b 20 6d 6f 72 65 20 72 61 70 69 64 6c 79 2e  sk more rapidly.
15368 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 6e 20 4f 28  .  **.  ** An O(
15369 6e 5e 32 29 20 69 6e 73 65 72 74 69 6f 6e 20 73  n^2) insertion s
1536a 6f 72 74 20 61 6c 67 6f 72 69 74 68 6d 20 69 73  ort algorithm is
1536b 20 75 73 65 64 2c 20 62 75 74 20 73 69 6e 63 65   used, but since
1536c 0a 20 20 2a 2a 20 6e 20 69 73 20 6e 65 76 65 72  .  ** n is never
1536d 20 6d 6f 72 65 20 74 68 61 6e 20 4e 42 20 28 61   more than NB (a
1536e 20 73 6d 61 6c 6c 20 63 6f 6e 73 74 61 6e 74 29   small constant)
1536f 2c 20 74 68 61 74 20 73 68 6f 75 6c 64 0a 20 20  , that should.  
15370 2a 2a 20 6e 6f 74 20 62 65 20 61 20 70 72 6f 62  ** not be a prob
15371 6c 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57  lem..  **.  ** W
15372 68 65 6e 20 4e 42 3d 3d 33 2c 20 74 68 69 73 20  hen NB==3, this 
15373 6f 6e 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  one optimization
15374 20 6d 61 6b 65 73 20 74 68 65 20 64 61 74 61 62   makes the datab
15375 61 73 65 0a 20 20 2a 2a 20 61 62 6f 75 74 20 32  ase.  ** about 2
15376 35 25 20 66 61 73 74 65 72 20 66 6f 72 20 6c 61  5% faster for la
15377 72 67 65 20 69 6e 73 65 72 74 69 6f 6e 73 20 61  rge insertions a
15378 6e 64 20 64 65 6c 65 74 69 6f 6e 73 2e 0a 20 20  nd deletions..  
15379 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
1537a 6b 2d 31 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  k-1; i++){.    i
1537b 6e 74 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65  nt minV = pgnoNe
1537c 77 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 6d 69  w[i];.    int mi
1537d 6e 49 20 3d 20 69 3b 0a 20 20 20 20 66 6f 72 28  nI = i;.    for(
1537e 6a 3d 69 2b 31 3b 20 6a 3c 6b 3b 20 6a 2b 2b 29  j=i+1; j<k; j++)
1537f 7b 0a 20 20 20 20 20 20 69 66 28 20 70 67 6e 6f  {.      if( pgno
15380 4e 65 77 5b 6a 5d 3c 28 75 6e 73 69 67 6e 65 64  New[j]<(unsigned
15381 29 6d 69 6e 56 20 29 7b 0a 20 20 20 20 20 20 20  )minV ){.       
15382 20 6d 69 6e 49 20 3d 20 6a 3b 0a 20 20 20 20 20   minI = j;.     
15383 20 20 20 6d 69 6e 56 20 3d 20 70 67 6e 6f 4e 65     minV = pgnoNe
15384 77 5b 6a 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  w[j];.      }.  
15385 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 69 6e 49    }.    if( minI
15386 3e 69 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  >i ){.      int 
15387 74 3b 0a 20 20 20 20 20 20 4d 65 6d 50 61 67 65  t;.      MemPage
15388 20 2a 70 54 3b 0a 20 20 20 20 20 20 74 20 3d 20   *pT;.      t = 
15389 70 67 6e 6f 4e 65 77 5b 69 5d 3b 0a 20 20 20 20  pgnoNew[i];.    
1538a 20 20 70 54 20 3d 20 61 70 4e 65 77 5b 69 5d 3b    pT = apNew[i];
1538b 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 69  .      pgnoNew[i
1538c 5d 20 3d 20 70 67 6e 6f 4e 65 77 5b 6d 69 6e 49  ] = pgnoNew[minI
1538d 5d 3b 0a 20 20 20 20 20 20 61 70 4e 65 77 5b 69  ];.      apNew[i
1538e 5d 20 3d 20 61 70 4e 65 77 5b 6d 69 6e 49 5d 3b  ] = apNew[minI];
1538f 0a 20 20 20 20 20 20 70 67 6e 6f 4e 65 77 5b 6d  .      pgnoNew[m
15390 69 6e 49 5d 20 3d 20 74 3b 0a 20 20 20 20 20 20  inI] = t;.      
15391 61 70 4e 65 77 5b 6d 69 6e 49 5d 20 3d 20 70 54  apNew[minI] = pT
15392 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 54 52  ;.    }.  }.  TR
15393 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 6f  ACE(("BALANCE: o
15394 6c 64 3a 20 25 64 20 25 64 20 25 64 20 20 6e 65  ld: %d %d %d  ne
15395 77 3a 20 25 64 28 25 64 29 20 25 64 28 25 64 29  w: %d(%d) %d(%d)
15396 20 25 64 28 25 64 29 20 25 64 28 25 64 29 20 25   %d(%d) %d(%d) %
15397 64 28 25 64 29 5c 6e 22 2c 0a 20 20 20 20 70 67  d(%d)\n",.    pg
15398 6e 6f 4f 6c 64 5b 30 5d 2c 20 0a 20 20 20 20 6e  noOld[0], .    n
15399 4f 6c 64 3e 3d 32 20 3f 20 70 67 6e 6f 4f 6c 64  Old>=2 ? pgnoOld
1539a 5b 31 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4f 6c  [1] : 0,.    nOl
1539b 64 3e 3d 33 20 3f 20 70 67 6e 6f 4f 6c 64 5b 32  d>=3 ? pgnoOld[2
1539c 5d 20 3a 20 30 2c 0a 20 20 20 20 70 67 6e 6f 4e  ] : 0,.    pgnoN
1539d 65 77 5b 30 5d 2c 20 73 7a 4e 65 77 5b 30 5d 2c  ew[0], szNew[0],
1539e 0a 20 20 20 20 6e 4e 65 77 3e 3d 32 20 3f 20 70  .    nNew>=2 ? p
1539f 67 6e 6f 4e 65 77 5b 31 5d 20 3a 20 30 2c 20 6e  gnoNew[1] : 0, n
153a0 4e 65 77 3e 3d 32 20 3f 20 73 7a 4e 65 77 5b 31  New>=2 ? szNew[1
153a1 5d 20 3a 20 30 2c 0a 20 20 20 20 6e 4e 65 77 3e  ] : 0,.    nNew>
153a2 3d 33 20 3f 20 70 67 6e 6f 4e 65 77 5b 32 5d 20  =3 ? pgnoNew[2] 
153a3 3a 20 30 2c 20 6e 4e 65 77 3e 3d 33 20 3f 20 73  : 0, nNew>=3 ? s
153a4 7a 4e 65 77 5b 32 5d 20 3a 20 30 2c 0a 20 20 20  zNew[2] : 0,.   
153a5 20 6e 4e 65 77 3e 3d 34 20 3f 20 70 67 6e 6f 4e   nNew>=4 ? pgnoN
153a6 65 77 5b 33 5d 20 3a 20 30 2c 20 6e 4e 65 77 3e  ew[3] : 0, nNew>
153a7 3d 34 20 3f 20 73 7a 4e 65 77 5b 33 5d 20 3a 20  =4 ? szNew[3] : 
153a8 30 2c 0a 20 20 20 20 6e 4e 65 77 3e 3d 35 20 3f  0,.    nNew>=5 ?
153a9 20 70 67 6e 6f 4e 65 77 5b 34 5d 20 3a 20 30 2c   pgnoNew[4] : 0,
153aa 20 6e 4e 65 77 3e 3d 35 20 3f 20 73 7a 4e 65 77   nNew>=5 ? szNew
153ab 5b 34 5d 20 3a 20 30 29 29 3b 0a 0a 20 20 2f 2a  [4] : 0));..  /*
153ac 0a 20 20 2a 2a 20 45 76 65 6e 6c 79 20 64 69 73  .  ** Evenly dis
153ad 74 72 69 62 75 74 65 20 74 68 65 20 64 61 74 61  tribute the data
153ae 20 69 6e 20 61 70 43 65 6c 6c 5b 5d 20 61 63 72   in apCell[] acr
153af 6f 73 73 20 74 68 65 20 6e 65 77 20 70 61 67 65  oss the new page
153b0 73 2e 0a 20 20 2a 2a 20 49 6e 73 65 72 74 20 64  s..  ** Insert d
153b1 69 76 69 64 65 72 20 63 65 6c 6c 73 20 69 6e 74  ivider cells int
153b2 6f 20 70 50 61 72 65 6e 74 20 61 73 20 6e 65 63  o pParent as nec
153b3 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a 20 20 6a  essary..  */.  j
153b4 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69 3d 30 3b   = 0;.  for(i=0;
153b5 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a 20   i<nNew; i++){. 
153b6 20 20 20 2f 2a 20 41 73 73 65 6d 62 6c 65 20 74     /* Assemble t
153b7 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70  he new sibling p
153b8 61 67 65 2e 20 2a 2f 0a 20 20 20 20 4d 65 6d 50  age. */.    MemP
153b9 61 67 65 20 2a 70 4e 65 77 20 3d 20 61 70 4e 65  age *pNew = apNe
153ba 77 5b 69 5d 3b 0a 20 20 20 20 61 73 73 65 72 74  w[i];.    assert
153bb 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20 29 3b  ( j<nMaxCells );
153bc 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65  .    assert( pNe
153bd 77 2d 3e 70 67 6e 6f 3d 3d 70 67 6e 6f 4e 65 77  w->pgno==pgnoNew
153be 5b 69 5d 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50  [i] );.    zeroP
153bf 61 67 65 28 70 4e 65 77 2c 20 70 61 67 65 46 6c  age(pNew, pageFl
153c0 61 67 73 29 3b 0a 20 20 20 20 61 73 73 65 6d 62  ags);.    assemb
153c1 6c 65 50 61 67 65 28 70 4e 65 77 2c 20 63 6e 74  lePage(pNew, cnt
153c2 4e 65 77 5b 69 5d 2d 6a 2c 20 26 61 70 43 65 6c  New[i]-j, &apCel
153c3 6c 5b 6a 5d 2c 20 26 73 7a 43 65 6c 6c 5b 6a 5d  l[j], &szCell[j]
153c4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
153c5 4e 65 77 2d 3e 6e 43 65 6c 6c 3e 30 20 7c 7c 20  New->nCell>0 || 
153c6 28 6e 4e 65 77 3d 3d 31 20 26 26 20 63 6e 74 4e  (nNew==1 && cntN
153c7 65 77 5b 30 5d 3d 3d 30 29 20 29 3b 0a 20 20 20  ew[0]==0) );.   
153c8 20 61 73 73 65 72 74 28 20 70 4e 65 77 2d 3e 6e   assert( pNew->n
153c9 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 0a  Overflow==0 );..
153ca 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69      /* If this i
153cb 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
153cc 20 64 61 74 61 62 61 73 65 2c 20 75 70 64 61 74   database, updat
153cd 65 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61  e the pointer ma
153ce 70 20 65 6e 74 72 69 65 73 0a 20 20 20 20 2a 2a  p entries.    **
153cf 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74   that point to t
153d0 68 65 20 73 69 62 6c 69 6e 67 73 20 74 68 61 74  he siblings that
153d1 20 77 65 72 65 20 72 65 61 72 72 61 6e 67 65 64   were rearranged
153d2 2e 20 54 68 65 73 65 20 63 61 6e 20 62 65 3a 20  . These can be: 
153d3 6c 65 66 74 0a 20 20 20 20 2a 2a 20 63 68 69 6c  left.    ** chil
153d4 64 72 65 6e 20 6f 66 20 63 65 6c 6c 73 2c 20 74  dren of cells, t
153d5 68 65 20 72 69 67 68 74 2d 63 68 69 6c 64 20 6f  he right-child o
153d6 66 20 74 68 65 20 70 61 67 65 2c 20 6f 72 20 6f  f the page, or o
153d7 76 65 72 66 6c 6f 77 20 70 61 67 65 73 0a 20 20  verflow pages.  
153d8 20 20 2a 2a 20 70 6f 69 6e 74 65 64 20 74 6f 20    ** pointed to 
153d9 62 79 20 63 65 6c 6c 73 2e 0a 20 20 20 20 2a 2f  by cells..    */
153da 0a 20 20 20 20 69 66 28 20 49 53 41 55 54 4f 56  .    if( ISAUTOV
153db 41 43 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 66  ACUUM ){.      f
153dc 6f 72 28 6b 3d 6a 3b 20 6b 3c 63 6e 74 4e 65 77  or(k=j; k<cntNew
153dd 5b 69 5d 3b 20 6b 2b 2b 29 7b 0a 20 20 20 20 20  [i]; k++){.     
153de 20 20 20 61 73 73 65 72 74 28 20 6b 3c 6e 4d 61     assert( k<nMa
153df 78 43 65 6c 6c 73 20 29 3b 0a 20 20 20 20 20 20  xCells );.      
153e0 20 20 69 66 28 20 61 46 72 6f 6d 5b 6b 5d 3d 3d    if( aFrom[k]==
153e1 30 78 46 46 20 7c 7c 20 61 70 43 6f 70 79 5b 61  0xFF || apCopy[a
153e2 46 72 6f 6d 5b 6b 5d 5d 2d 3e 70 67 6e 6f 21 3d  From[k]]->pgno!=
153e3 70 4e 65 77 2d 3e 70 67 6e 6f 20 29 7b 0a 20 20  pNew->pgno ){.  
153e4 20 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72          rc = ptr
153e5 6d 61 70 50 75 74 4f 76 66 6c 28 70 4e 65 77 2c  mapPutOvfl(pNew,
153e6 20 6b 2d 6a 29 3b 0a 20 20 20 20 20 20 20 20 20   k-j);.         
153e7 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
153e8 4f 4b 20 26 26 20 6c 65 61 66 43 6f 72 72 65 63  OK && leafCorrec
153e9 74 69 6f 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  tion==0 ){.     
153ea 20 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d         rc = ptrm
153eb 61 70 50 75 74 28 70 42 74 2c 20 67 65 74 34 62  apPut(pBt, get4b
153ec 79 74 65 28 61 70 43 65 6c 6c 5b 6b 5d 29 2c 20  yte(apCell[k]), 
153ed 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e  PTRMAP_BTREE, pN
153ee 65 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20  ew->pgno);.     
153ef 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
153f0 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
153f1 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  OK ){.          
153f2 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63    goto balance_c
153f3 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20  leanup;.        
153f4 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
153f5 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
153f6 20 6a 20 3d 20 63 6e 74 4e 65 77 5b 69 5d 3b 0a   j = cntNew[i];.
153f7 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73  .    /* If the s
153f8 69 62 6c 69 6e 67 20 70 61 67 65 20 61 73 73 65  ibling page asse
153f9 6d 62 6c 65 64 20 61 62 6f 76 65 20 77 61 73 20  mbled above was 
153fa 6e 6f 74 20 74 68 65 20 72 69 67 68 74 2d 6d 6f  not the right-mo
153fb 73 74 20 73 69 62 6c 69 6e 67 2c 0a 20 20 20 20  st sibling,.    
153fc 2a 2a 20 69 6e 73 65 72 74 20 61 20 64 69 76 69  ** insert a divi
153fd 64 65 72 20 63 65 6c 6c 20 69 6e 74 6f 20 74 68  der cell into th
153fe 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 0a 20  e parent page.. 
153ff 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 69 3c     */.    if( i<
15400 6e 4e 65 77 2d 31 20 26 26 20 6a 3c 6e 43 65 6c  nNew-1 && j<nCel
15401 6c 20 29 7b 0a 20 20 20 20 20 20 75 38 20 2a 70  l ){.      u8 *p
15402 43 65 6c 6c 3b 0a 20 20 20 20 20 20 75 38 20 2a  Cell;.      u8 *
15403 70 54 65 6d 70 3b 0a 20 20 20 20 20 20 69 6e 74  pTemp;.      int
15404 20 73 7a 3b 0a 0a 20 20 20 20 20 20 61 73 73 65   sz;..      asse
15405 72 74 28 20 6a 3c 6e 4d 61 78 43 65 6c 6c 73 20  rt( j<nMaxCells 
15406 29 3b 0a 20 20 20 20 20 20 70 43 65 6c 6c 20 3d  );.      pCell =
15407 20 61 70 43 65 6c 6c 5b 6a 5d 3b 0a 20 20 20 20   apCell[j];.    
15408 20 20 73 7a 20 3d 20 73 7a 43 65 6c 6c 5b 6a 5d    sz = szCell[j]
15409 20 2b 20 6c 65 61 66 43 6f 72 72 65 63 74 69 6f   + leafCorrectio
1540a 6e 3b 0a 20 20 20 20 20 20 70 54 65 6d 70 20 3d  n;.      pTemp =
1540b 20 26 61 53 70 61 63 65 32 5b 69 53 70 61 63 65   &aSpace2[iSpace
1540c 32 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70  2];.      if( !p
1540d 4e 65 77 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20  New->leaf ){.   
1540e 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 4e 65       memcpy(&pNe
1540f 77 2d 3e 61 44 61 74 61 5b 38 5d 2c 20 70 43 65  w->aData[8], pCe
15410 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 20 20 20 20  ll, 4);.        
15411 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55 4d  if( ISAUTOVACUUM
15412 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 61   .         && (a
15413 46 72 6f 6d 5b 6a 5d 3d 3d 30 78 46 46 20 7c 7c  From[j]==0xFF ||
15414 20 61 70 43 6f 70 79 5b 61 46 72 6f 6d 5b 6a 5d   apCopy[aFrom[j]
15415 5d 2d 3e 70 67 6e 6f 21 3d 70 4e 65 77 2d 3e 70  ]->pgno!=pNew->p
15416 67 6e 6f 29 0a 20 20 20 20 20 20 20 20 29 7b 0a  gno).        ){.
15417 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20 70            rc = p
15418 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67 65  trmapPut(pBt, ge
15419 74 34 62 79 74 65 28 70 43 65 6c 6c 29 2c 20 50  t4byte(pCell), P
1541a 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 4e 65  TRMAP_BTREE, pNe
1541b 77 2d 3e 70 67 6e 6f 29 3b 0a 20 20 20 20 20 20  w->pgno);.      
1541c 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
1541d 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1541e 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61 6e 63       goto balanc
1541f 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 20  e_cleanup;.     
15420 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
15421 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
15422 20 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20 20   leafData ){.   
15423 20 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 74       /* If the t
15424 72 65 65 20 69 73 20 61 20 6c 65 61 66 2d 64 61  ree is a leaf-da
15425 74 61 20 74 72 65 65 2c 20 61 6e 64 20 74 68 65  ta tree, and the
15426 20 73 69 62 6c 69 6e 67 73 20 61 72 65 20 6c 65   siblings are le
15427 61 76 65 73 2c 20 0a 20 20 20 20 20 20 20 20 2a  aves, .        *
15428 2a 20 74 68 65 6e 20 74 68 65 72 65 20 69 73 20  * then there is 
15429 6e 6f 20 64 69 76 69 64 65 72 20 63 65 6c 6c 20  no divider cell 
1542a 69 6e 20 61 70 43 65 6c 6c 5b 5d 2e 20 49 6e 73  in apCell[]. Ins
1542b 74 65 61 64 2c 20 74 68 65 20 64 69 76 69 64 65  tead, the divide
1542c 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 65  r .        ** ce
1542d 6c 6c 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 74  ll consists of t
1542e 68 65 20 69 6e 74 65 67 65 72 20 6b 65 79 20 66  he integer key f
1542f 6f 72 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73  or the right-mos
15430 74 20 63 65 6c 6c 20 6f 66 20 0a 20 20 20 20 20  t cell of .     
15431 20 20 20 2a 2a 20 74 68 65 20 73 69 62 6c 69 6e     ** the siblin
15432 67 2d 70 61 67 65 20 61 73 73 65 6d 62 6c 65 64  g-page assembled
15433 20 61 62 6f 76 65 20 6f 6e 6c 79 2e 0a 20 20 20   above only..   
15434 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
15435 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f 3b 0a 20  CellInfo info;. 
15436 20 20 20 20 20 20 20 6a 2d 2d 3b 0a 20 20 20 20         j--;.    
15437 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15438 50 61 72 73 65 43 65 6c 6c 50 74 72 28 70 4e 65  ParseCellPtr(pNe
15439 77 2c 20 61 70 43 65 6c 6c 5b 6a 5d 2c 20 26 69  w, apCell[j], &i
1543a 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 70 43  nfo);.        pC
1543b 65 6c 6c 20 3d 20 70 54 65 6d 70 3b 0a 20 20 20  ell = pTemp;.   
1543c 20 20 20 20 20 66 69 6c 6c 49 6e 43 65 6c 6c 28       fillInCell(
1543d 70 50 61 72 65 6e 74 2c 20 70 43 65 6c 6c 2c 20  pParent, pCell, 
1543e 30 2c 20 69 6e 66 6f 2e 6e 4b 65 79 2c 20 30 2c  0, info.nKey, 0,
1543f 20 30 2c 20 30 2c 20 26 73 7a 29 3b 0a 20 20 20   0, 0, &sz);.   
15440 20 20 20 20 20 70 54 65 6d 70 20 3d 20 30 3b 0a       pTemp = 0;.
15441 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
15442 20 20 20 20 20 70 43 65 6c 6c 20 2d 3d 20 34 3b       pCell -= 4;
15443 0a 20 20 20 20 20 20 20 20 2f 2a 20 4f 62 73 63  .        /* Obsc
15444 75 72 65 20 63 61 73 65 20 66 6f 72 20 6e 6f 6e  ure case for non
15445 2d 6c 65 61 66 2d 64 61 74 61 20 74 72 65 65 73  -leaf-data trees
15446 3a 20 49 66 20 74 68 65 20 63 65 6c 6c 20 61 74  : If the cell at
15447 20 70 43 65 6c 6c 20 77 61 73 0a 20 20 20 20 20   pCell was.     
15448 20 20 20 2a 2a 20 70 72 65 76 69 6f 75 73 6c 79     ** previously
15449 20 73 74 6f 72 65 64 20 6f 6e 20 61 20 6c 65 61   stored on a lea
1544a 66 20 6e 6f 64 65 2c 20 61 6e 64 20 69 74 73 20  f node, and its 
1544b 72 65 70 6f 72 74 65 64 20 73 69 7a 65 20 77 61  reported size wa
1544c 73 20 34 0a 20 20 20 20 20 20 20 20 2a 2a 20 62  s 4.        ** b
1544d 79 74 65 73 2c 20 74 68 65 6e 20 69 74 20 6d 61  ytes, then it ma
1544e 79 20 61 63 74 75 61 6c 6c 79 20 62 65 20 73 6d  y actually be sm
1544f 61 6c 6c 65 72 20 74 68 61 6e 20 74 68 69 73 20  aller than this 
15450 0a 20 20 20 20 20 20 20 20 2a 2a 20 28 73 65 65  .        ** (see
15451 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 72   sqlite3BtreePar
15452 73 65 43 65 6c 6c 50 74 72 28 29 2c 20 34 20 62  seCellPtr(), 4 b
15453 79 74 65 73 20 69 73 20 74 68 65 20 6d 69 6e 69  ytes is the mini
15454 6d 75 6d 20 73 69 7a 65 20 6f 66 0a 20 20 20 20  mum size of.    
15455 20 20 20 20 2a 2a 20 61 6e 79 20 63 65 6c 6c 29      ** any cell)
15456 2e 20 42 75 74 20 69 74 20 69 73 20 69 6d 70 6f  . But it is impo
15457 72 74 61 6e 74 20 74 6f 20 70 61 73 73 20 74 68  rtant to pass th
15458 65 20 63 6f 72 72 65 63 74 20 73 69 7a 65 20 74  e correct size t
15459 6f 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e  o .        ** in
1545a 73 65 72 74 43 65 6c 6c 28 29 2c 20 73 6f 20 72  sertCell(), so r
1545b 65 70 61 72 73 65 20 74 68 65 20 63 65 6c 6c 20  eparse the cell 
1545c 6e 6f 77 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a  now..        **.
1545d 20 20 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65 20          ** Note 
1545e 74 68 61 74 20 74 68 69 73 20 63 61 6e 20 6e 65  that this can ne
1545f 76 65 72 20 68 61 70 70 65 6e 20 69 6e 20 61 6e  ver happen in an
15460 20 53 51 4c 69 74 65 20 64 61 74 61 20 66 69 6c   SQLite data fil
15461 65 2c 20 61 73 20 61 6c 6c 0a 20 20 20 20 20 20  e, as all.      
15462 20 20 2a 2a 20 63 65 6c 6c 73 20 61 72 65 20 61    ** cells are a
15463 74 20 6c 65 61 73 74 20 34 20 62 79 74 65 73 2e  t least 4 bytes.
15464 20 49 74 20 6f 6e 6c 79 20 68 61 70 70 65 6e 73   It only happens
15465 20 69 6e 20 62 2d 74 72 65 65 73 20 75 73 65 64   in b-trees used
15466 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 6f 20 65  .        ** to e
15467 76 61 6c 75 61 74 65 20 22 49 4e 20 28 53 45 4c  valuate "IN (SEL
15468 45 43 54 20 2e 2e 2e 29 22 20 61 6e 64 20 73 69  ECT ...)" and si
15469 6d 69 6c 61 72 20 63 6c 61 75 73 65 73 2e 0a 20  milar clauses.. 
1546a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
1546b 20 20 69 66 28 20 73 7a 43 65 6c 6c 5b 6a 5d 3d    if( szCell[j]=
1546c 3d 34 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =4 ){.          
1546d 61 73 73 65 72 74 28 6c 65 61 66 43 6f 72 72 65  assert(leafCorre
1546e 63 74 69 6f 6e 3d 3d 34 29 3b 0a 20 20 20 20 20  ction==4);.     
1546f 20 20 20 20 20 73 7a 20 3d 20 63 65 6c 6c 53 69       sz = cellSi
15470 7a 65 50 74 72 28 70 50 61 72 65 6e 74 2c 20 70  zePtr(pParent, p
15471 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 7d  Cell);.        }
15472 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
15473 53 70 61 63 65 32 20 2b 3d 20 73 7a 3b 0a 20 20  Space2 += sz;.  
15474 20 20 20 20 61 73 73 65 72 74 28 20 73 7a 3c 3d      assert( sz<=
15475 70 42 74 2d 3e 70 61 67 65 53 69 7a 65 2f 34 20  pBt->pageSize/4 
15476 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
15477 20 69 53 70 61 63 65 32 3c 3d 70 42 74 2d 3e 70   iSpace2<=pBt->p
15478 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 20 20 20  ageSize );.     
15479 20 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c   rc = insertCell
1547a 28 70 50 61 72 65 6e 74 2c 20 6e 78 44 69 76 2c  (pParent, nxDiv,
1547b 20 70 43 65 6c 6c 2c 20 73 7a 2c 20 70 54 65 6d   pCell, sz, pTem
1547c 70 2c 20 34 29 3b 0a 20 20 20 20 20 20 69 66 28  p, 4);.      if(
1547d 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1547e 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
1547f 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 70 75 74  eanup;.      put
15480 34 62 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c  4byte(findOverfl
15481 6f 77 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 6e  owCell(pParent,n
15482 78 44 69 76 29 2c 20 70 4e 65 77 2d 3e 70 67 6e  xDiv), pNew->pgn
15483 6f 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 66  o);..      /* If
15484 20 74 68 69 73 20 69 73 20 61 6e 20 61 75 74 6f   this is an auto
15485 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73 65  -vacuum database
15486 2c 20 61 6e 64 20 6e 6f 74 20 61 20 6c 65 61 66  , and not a leaf
15487 2d 64 61 74 61 20 74 72 65 65 2c 0a 20 20 20 20  -data tree,.    
15488 20 20 2a 2a 20 74 68 65 6e 20 75 70 64 61 74 65    ** then update
15489 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6d 61 70   the pointer map
1548a 20 77 69 74 68 20 61 6e 20 65 6e 74 72 79 20 66   with an entry f
1548b 6f 72 20 74 68 65 20 6f 76 65 72 66 6c 6f 77 20  or the overflow 
1548c 70 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 74 68  page.      ** th
1548d 61 74 20 74 68 65 20 63 65 6c 6c 20 6a 75 73 74  at the cell just
1548e 20 69 6e 73 65 72 74 65 64 20 70 6f 69 6e 74 73   inserted points
1548f 20 74 6f 20 28 69 66 20 61 6e 79 29 2e 0a 20 20   to (if any)..  
15490 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
15491 20 49 53 41 55 54 4f 56 41 43 55 55 4d 20 26 26   ISAUTOVACUUM &&
15492 20 21 6c 65 61 66 44 61 74 61 20 29 7b 0a 20 20   !leafData ){.  
15493 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
15494 70 50 75 74 4f 76 66 6c 28 70 50 61 72 65 6e 74  pPutOvfl(pParent
15495 2c 20 6e 78 44 69 76 29 3b 0a 20 20 20 20 20 20  , nxDiv);.      
15496 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
15497 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
15498 20 67 6f 74 6f 20 62 61 6c 61 6e 63 65 5f 63 6c   goto balance_cl
15499 65 61 6e 75 70 3b 0a 20 20 20 20 20 20 20 20 7d  eanup;.        }
1549a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6a  .      }.      j
1549b 2b 2b 3b 0a 20 20 20 20 20 20 6e 78 44 69 76 2b  ++;.      nxDiv+
1549c 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a  +;.    }..    /*
1549d 20 53 65 74 20 74 68 65 20 70 6f 69 6e 74 65 72   Set the pointer
1549e 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f 72 20 74  -map entry for t
1549f 68 65 20 6e 65 77 20 73 69 62 6c 69 6e 67 20 70  he new sibling p
154a0 61 67 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  age. */.    if( 
154a1 49 53 41 55 54 4f 56 41 43 55 55 4d 20 29 7b 0a  ISAUTOVACUUM ){.
154a2 20 20 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61        rc = ptrma
154a3 70 50 75 74 28 70 42 74 2c 20 70 4e 65 77 2d 3e  pPut(pBt, pNew->
154a4 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  pgno, PTRMAP_BTR
154a5 45 45 2c 20 70 50 61 72 65 6e 74 2d 3e 70 67 6e  EE, pParent->pgn
154a6 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  o);.      if( rc
154a7 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
154a8 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c 61         goto bala
154a9 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  nce_cleanup;.   
154aa 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20     }.    }.  }. 
154ab 20 61 73 73 65 72 74 28 20 6a 3d 3d 6e 43 65 6c   assert( j==nCel
154ac 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  l );.  assert( n
154ad 4f 6c 64 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  Old>0 );.  asser
154ae 74 28 20 6e 4e 65 77 3e 30 20 29 3b 0a 20 20 69  t( nNew>0 );.  i
154af 66 28 20 28 70 61 67 65 46 6c 61 67 73 20 26 20  f( (pageFlags & 
154b0 50 54 46 5f 4c 45 41 46 29 3d 3d 30 20 29 7b 0a  PTF_LEAF)==0 ){.
154b1 20 20 20 20 75 38 20 2a 7a 43 68 69 6c 64 20 3d      u8 *zChild =
154b2 20 26 61 70 43 6f 70 79 5b 6e 4f 6c 64 2d 31 5d   &apCopy[nOld-1]
154b3 2d 3e 61 44 61 74 61 5b 38 5d 3b 0a 20 20 20 20  ->aData[8];.    
154b4 6d 65 6d 63 70 79 28 26 61 70 4e 65 77 5b 6e 4e  memcpy(&apNew[nN
154b5 65 77 2d 31 5d 2d 3e 61 44 61 74 61 5b 38 5d 2c  ew-1]->aData[8],
154b6 20 7a 43 68 69 6c 64 2c 20 34 29 3b 0a 20 20 20   zChild, 4);.   
154b7 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
154b8 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  M ){.      rc = 
154b9 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c 20 67  ptrmapPut(pBt, g
154ba 65 74 34 62 79 74 65 28 7a 43 68 69 6c 64 29 2c  et4byte(zChild),
154bb 20 50 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 61   PTRMAP_BTREE, a
154bc 70 4e 65 77 5b 6e 4e 65 77 2d 31 5d 2d 3e 70 67  pNew[nNew-1]->pg
154bd 6e 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  no);.      if( r
154be 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
154bf 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 61 6c          goto bal
154c0 61 6e 63 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  ance_cleanup;.  
154c1 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
154c2 20 20 69 66 28 20 6e 78 44 69 76 3d 3d 70 50 61    if( nxDiv==pPa
154c3 72 65 6e 74 2d 3e 6e 43 65 6c 6c 2b 70 50 61 72  rent->nCell+pPar
154c4 65 6e 74 2d 3e 6e 4f 76 65 72 66 6c 6f 77 20 29  ent->nOverflow )
154c5 7b 0a 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d  {.    /* Right-m
154c6 6f 73 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74  ost sibling is t
154c7 68 65 20 72 69 67 68 74 2d 6d 6f 73 74 20 63 68  he right-most ch
154c8 69 6c 64 20 6f 66 20 70 50 61 72 65 6e 74 20 2a  ild of pParent *
154c9 2f 0a 20 20 20 20 70 75 74 34 62 79 74 65 28 26  /.    put4byte(&
154ca 70 50 61 72 65 6e 74 2d 3e 61 44 61 74 61 5b 70  pParent->aData[p
154cb 50 61 72 65 6e 74 2d 3e 68 64 72 4f 66 66 73 65  Parent->hdrOffse
154cc 74 2b 38 5d 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e  t+8], pgnoNew[nN
154cd 65 77 2d 31 5d 29 3b 0a 20 20 7d 65 6c 73 65 7b  ew-1]);.  }else{
154ce 0a 20 20 20 20 2f 2a 20 52 69 67 68 74 2d 6d 6f  .    /* Right-mo
154cf 73 74 20 73 69 62 6c 69 6e 67 20 69 73 20 74 68  st sibling is th
154d0 65 20 6c 65 66 74 20 63 68 69 6c 64 20 6f 66 20  e left child of 
154d1 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20  the first entry 
154d2 69 6e 20 70 50 61 72 65 6e 74 0a 20 20 20 20 2a  in pParent.    *
154d3 2a 20 70 61 73 74 20 74 68 65 20 72 69 67 68 74  * past the right
154d4 2d 6d 6f 73 74 20 64 69 76 69 64 65 72 20 65 6e  -most divider en
154d5 74 72 79 20 2a 2f 0a 20 20 20 20 70 75 74 34 62  try */.    put4b
154d6 79 74 65 28 66 69 6e 64 4f 76 65 72 66 6c 6f 77  yte(findOverflow
154d7 43 65 6c 6c 28 70 50 61 72 65 6e 74 2c 20 6e 78  Cell(pParent, nx
154d8 44 69 76 29 2c 20 70 67 6e 6f 4e 65 77 5b 6e 4e  Div), pgnoNew[nN
154d9 65 77 2d 31 5d 29 3b 0a 20 20 7d 0a 0a 20 20 2f  ew-1]);.  }..  /
154da 2a 0a 20 20 2a 2a 20 42 61 6c 61 6e 63 65 20 74  *.  ** Balance t
154db 68 65 20 70 61 72 65 6e 74 20 70 61 67 65 2e 20  he parent page. 
154dc 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 63   Note that the c
154dd 75 72 72 65 6e 74 20 70 61 67 65 20 28 70 50 61  urrent page (pPa
154de 67 65 29 20 6d 69 67 68 74 0a 20 20 2a 2a 20 68  ge) might.  ** h
154df 61 76 65 20 62 65 65 6e 20 61 64 64 65 64 20 74  ave been added t
154e0 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74 20 73  o the freelist s
154e1 6f 20 69 74 20 6d 69 67 68 74 20 6e 6f 20 6c 6f  o it might no lo
154e2 6e 67 65 72 20 62 65 20 69 6e 69 74 69 61 6c 69  nger be initiali
154e3 7a 65 64 2e 0a 20 20 2a 2a 20 42 75 74 20 74 68  zed..  ** But th
154e4 65 20 70 61 72 65 6e 74 20 70 61 67 65 20 77 69  e parent page wi
154e5 6c 6c 20 61 6c 77 61 79 73 20 62 65 20 69 6e 69  ll always be ini
154e6 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20  tialized..  */. 
154e7 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e 74   assert( pParent
154e8 2d 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 73 71  ->isInit );.  sq
154e9 6c 69 74 65 33 53 63 72 61 74 63 68 46 72 65 65  lite3ScratchFree
154ea 28 61 70 43 65 6c 6c 29 3b 0a 20 20 61 70 43 65  (apCell);.  apCe
154eb 6c 6c 20 3d 20 30 3b 0a 20 20 72 65 6c 65 61 73  ll = 0;.  releas
154ec 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
154ed 70 43 75 72 2d 3e 69 50 61 67 65 2d 2d 3b 0a 20  pCur->iPage--;. 
154ee 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43   rc = balance(pC
154ef 75 72 2c 20 30 29 3b 0a 20 20 0a 20 20 2f 2a 0a  ur, 0);.  .  /*.
154f0 20 20 2a 2a 20 43 6c 65 61 6e 75 70 20 62 65 66    ** Cleanup bef
154f1 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e 0a 20  ore returning.. 
154f2 20 2a 2f 0a 62 61 6c 61 6e 63 65 5f 63 6c 65 61   */.balance_clea
154f3 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 50 61  nup:.  sqlite3Pa
154f4 67 65 46 72 65 65 28 61 53 70 61 63 65 32 29 3b  geFree(aSpace2);
154f5 0a 20 20 73 71 6c 69 74 65 33 53 63 72 61 74 63  .  sqlite3Scratc
154f6 68 46 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20  hFree(apCell);. 
154f7 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 6c 64   for(i=0; i<nOld
154f8 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 72 65 6c 65  ; i++){.    rele
154f9 61 73 65 50 61 67 65 28 61 70 4f 6c 64 5b 69 5d  asePage(apOld[i]
154fa 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  );.  }.  for(i=0
154fb 3b 20 69 3c 6e 4e 65 77 3b 20 69 2b 2b 29 7b 0a  ; i<nNew; i++){.
154fc 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65 28      releasePage(
154fd 61 70 4e 65 77 5b 69 5d 29 3b 0a 20 20 7d 0a 0a  apNew[i]);.  }..
154fe 20 20 2f 2a 20 72 65 6c 65 61 73 65 50 61 67 65    /* releasePage
154ff 28 70 50 61 72 65 6e 74 29 3b 20 2a 2f 0a 20 20  (pParent); */.  
15500 54 52 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a  TRACE(("BALANCE:
15501 20 66 69 6e 69 73 68 65 64 20 77 69 74 68 20 25   finished with %
15502 64 3a 20 6f 6c 64 3d 25 64 20 6e 65 77 3d 25 64  d: old=%d new=%d
15503 20 63 65 6c 6c 73 3d 25 64 5c 6e 22 2c 0a 20 20   cells=%d\n",.  
15504 20 20 20 20 20 20 20 20 70 50 61 67 65 2d 3e 70          pPage->p
15505 67 6e 6f 2c 20 6e 4f 6c 64 2c 20 6e 4e 65 77 2c  gno, nOld, nNew,
15506 20 6e 43 65 6c 6c 29 29 3b 0a 0a 20 20 72 65 74   nCell));..  ret
15507 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
15508 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
15509 20 63 61 6c 6c 65 64 20 66 6f 72 20 74 68 65 20   called for the 
1550a 72 6f 6f 74 20 70 61 67 65 20 6f 66 20 61 20 62  root page of a b
1550b 74 72 65 65 20 77 68 65 6e 20 74 68 65 20 72 6f  tree when the ro
1550c 6f 74 0a 2a 2a 20 70 61 67 65 20 63 6f 6e 74 61  ot.** page conta
1550d 69 6e 73 20 6e 6f 20 63 65 6c 6c 73 2e 20 20 54  ins no cells.  T
1550e 68 69 73 20 69 73 20 61 6e 20 6f 70 70 6f 72 74  his is an opport
1550f 75 6e 69 74 79 20 74 6f 20 6d 61 6b 65 20 74 68  unity to make th
15510 65 20 74 72 65 65 0a 2a 2a 20 73 68 61 6c 6c 6f  e tree.** shallo
15511 77 65 72 20 62 79 20 6f 6e 65 20 6c 65 76 65 6c  wer by one level
15512 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
15513 62 61 6c 61 6e 63 65 5f 73 68 61 6c 6c 6f 77 65  balance_shallowe
15514 72 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  r(BtCursor *pCur
15515 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50  ){.  MemPage *pP
15516 61 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  age;            
15517 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67 65 20 6f    /* Root page o
15518 66 20 42 2d 54 72 65 65 20 2a 2f 0a 20 20 4d 65  f B-Tree */.  Me
15519 6d 50 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20  mPage *pChild;  
1551a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1551b 65 20 6f 6e 6c 79 20 63 68 69 6c 64 20 70 61 67  e only child pag
1551c 65 20 6f 66 20 70 50 61 67 65 20 2a 2f 0a 20 20  e of pPage */.  
1551d 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64 3b 20  Pgno pgnoChild; 
1551e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1551f 50 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  Page number for 
15520 70 43 68 69 6c 64 20 2a 2f 0a 20 20 69 6e 74 20  pChild */.  int 
15521 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
15522 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65 74 75           /* Retu
15523 72 6e 20 63 6f 64 65 20 66 72 6f 6d 20 73 75 62  rn code from sub
15524 70 72 6f 63 65 64 75 72 65 73 20 2a 2f 0a 20 20  procedures */.  
15525 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b 20 20  BtShared *pBt;  
15526 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15527 2f 2a 20 54 68 65 20 6d 61 69 6e 20 42 54 72 65  /* The main BTre
15528 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20  e structure */. 
15529 20 69 6e 74 20 6d 78 43 65 6c 6c 50 65 72 50 61   int mxCellPerPa
1552a 67 65 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ge;           /*
1552b 20 4d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   Maximum number 
1552c 6f 66 20 63 65 6c 6c 73 20 70 65 72 20 70 61 67  of cells per pag
1552d 65 20 2a 2f 0a 20 20 75 38 20 2a 2a 61 70 43 65  e */.  u8 **apCe
1552e 6c 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ll;             
1552f 20 20 20 20 2f 2a 20 41 6c 6c 20 63 65 6c 6c 73      /* All cells
15530 20 66 72 6f 6d 20 70 61 67 65 73 20 62 65 69 6e   from pages bein
15531 67 20 62 61 6c 61 6e 63 65 64 20 2a 2f 0a 20 20  g balanced */.  
15532 75 31 36 20 2a 73 7a 43 65 6c 6c 3b 20 20 20 20  u16 *szCell;    
15533 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
15534 4c 6f 63 61 6c 20 73 69 7a 65 20 6f 66 20 61 6c  Local size of al
15535 6c 20 63 65 6c 6c 73 20 2a 2f 0a 0a 20 20 61 73  l cells */..  as
15536 73 65 72 74 28 20 70 43 75 72 2d 3e 69 50 61 67  sert( pCur->iPag
15537 65 3d 3d 30 20 29 3b 0a 20 20 70 50 61 67 65 20  e==0 );.  pPage 
15538 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b 30  = pCur->apPage[0
15539 5d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 50  ];..  assert( pP
1553a 61 67 65 2d 3e 6e 43 65 6c 6c 3d 3d 30 20 29 3b  age->nCell==0 );
1553b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1553c 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50  e3_mutex_held(pP
1553d 61 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29  age->pBt->mutex)
1553e 20 29 3b 0a 20 20 70 42 74 20 3d 20 70 50 61 67   );.  pBt = pPag
1553f 65 2d 3e 70 42 74 3b 0a 20 20 6d 78 43 65 6c 6c  e->pBt;.  mxCell
15540 50 65 72 50 61 67 65 20 3d 20 4d 58 5f 43 45 4c  PerPage = MX_CEL
15541 4c 28 70 42 74 29 3b 0a 20 20 61 70 43 65 6c 6c  L(pBt);.  apCell
15542 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63   = sqlite3Malloc
15543 28 20 6d 78 43 65 6c 6c 50 65 72 50 61 67 65 2a  ( mxCellPerPage*
15544 28 73 69 7a 65 6f 66 28 75 38 2a 29 2b 73 69 7a  (sizeof(u8*)+siz
15545 65 6f 66 28 75 31 36 29 29 20 29 3b 0a 20 20 69  eof(u16)) );.  i
15546 66 28 20 61 70 43 65 6c 6c 3d 3d 30 20 29 20 72  f( apCell==0 ) r
15547 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
15548 45 4d 3b 0a 20 20 73 7a 43 65 6c 6c 20 3d 20 28  EM;.  szCell = (
15549 75 31 36 2a 29 26 61 70 43 65 6c 6c 5b 6d 78 43  u16*)&apCell[mxC
1554a 65 6c 6c 50 65 72 50 61 67 65 5d 3b 0a 20 20 69  ellPerPage];.  i
1554b 66 28 20 70 50 61 67 65 2d 3e 6c 65 61 66 20 29  f( pPage->leaf )
1554c 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61 62  {.    /* The tab
1554d 6c 65 20 69 73 20 63 6f 6d 70 6c 65 74 65 6c 79  le is completely
1554e 20 65 6d 70 74 79 20 2a 2f 0a 20 20 20 20 54 52   empty */.    TR
1554f 41 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 65  ACE(("BALANCE: e
15550 6d 70 74 79 20 74 61 62 6c 65 20 25 64 5c 6e 22  mpty table %d\n"
15551 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b  , pPage->pgno));
15552 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
15553 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65 20 69   The root page i
15554 73 20 65 6d 70 74 79 20 62 75 74 20 68 61 73 20  s empty but has 
15555 6f 6e 65 20 63 68 69 6c 64 2e 20 20 54 72 61 6e  one child.  Tran
15556 73 66 65 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  sfer the.    ** 
15557 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d  information from
15558 20 74 68 61 74 20 6f 6e 65 20 63 68 69 6c 64 20   that one child 
15559 69 6e 74 6f 20 74 68 65 20 72 6f 6f 74 20 70 61  into the root pa
1555a 67 65 20 69 66 20 69 74 20 0a 20 20 20 20 2a 2a  ge if it .    **
1555b 20 77 69 6c 6c 20 66 69 74 2e 20 20 54 68 69 73   will fit.  This
1555c 20 72 65 64 75 63 65 73 20 74 68 65 20 64 65 70   reduces the dep
1555d 74 68 20 6f 66 20 74 68 65 20 74 72 65 65 20 62  th of the tree b
1555e 79 20 6f 6e 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  y one..    **.  
1555f 20 20 2a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74    ** If the root
15560 20 70 61 67 65 20 69 73 20 70 61 67 65 20 31 2c   page is page 1,
15561 20 69 74 20 68 61 73 20 6c 65 73 73 20 73 70 61   it has less spa
15562 63 65 20 61 76 61 69 6c 61 62 6c 65 20 74 68 61  ce available tha
15563 6e 0a 20 20 20 20 2a 2a 20 69 74 73 20 63 68 69  n.    ** its chi
15564 6c 64 20 28 64 75 65 20 74 6f 20 74 68 65 20 31  ld (due to the 1
15565 30 30 20 62 79 74 65 20 68 65 61 64 65 72 20 74  00 byte header t
15566 68 61 74 20 6f 63 63 75 72 73 20 61 74 20 74 68  hat occurs at th
15567 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20 20 20  e beginning.    
15568 2a 2a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  ** of the databa
15569 73 65 20 66 6c 65 29 2c 20 73 6f 20 69 74 20 6d  se fle), so it m
1556a 69 67 68 74 20 6e 6f 74 20 62 65 20 61 62 6c 65  ight not be able
1556b 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 6f 66 20   to hold all of 
1556c 74 68 65 20 0a 20 20 20 20 2a 2a 20 69 6e 66 6f  the .    ** info
1556d 72 6d 61 74 69 6f 6e 20 63 75 72 72 65 6e 74 6c  rmation currentl
1556e 79 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  y contained in t
1556f 68 65 20 63 68 69 6c 64 2e 20 20 49 66 20 74 68  he child.  If th
15570 69 73 20 69 73 20 74 68 65 20 0a 20 20 20 20 2a  is is the .    *
15571 2a 20 63 61 73 65 2c 20 74 68 65 6e 20 64 6f 20  * case, then do 
15572 6e 6f 74 20 64 6f 20 74 68 65 20 74 72 61 6e 73  not do the trans
15573 66 65 72 2e 20 20 4c 65 61 76 65 20 70 61 67 65  fer.  Leave page
15574 20 31 20 65 6d 70 74 79 20 65 78 63 65 70 74 0a   1 empty except.
15575 20 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 72      ** for the r
15576 69 67 68 74 2d 70 6f 69 6e 74 65 72 20 74 6f 20  ight-pointer to 
15577 74 68 65 20 63 68 69 6c 64 20 70 61 67 65 2e 20  the child page. 
15578 20 54 68 65 20 63 68 69 6c 64 20 70 61 67 65 20   The child page 
15579 62 65 63 6f 6d 65 73 0a 20 20 20 20 2a 2a 20 74  becomes.    ** t
1557a 68 65 20 76 69 72 74 75 61 6c 20 72 6f 6f 74 20  he virtual root 
1557b 6f 66 20 74 68 65 20 74 72 65 65 2e 0a 20 20 20  of the tree..   
1557c 20 2a 2f 0a 20 20 20 20 56 56 41 5f 4f 4e 4c 59   */.    VVA_ONLY
1557d 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53 68 75  ( pCur->pagesShu
1557e 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20 20 20  ffled = 1 );.   
1557f 20 70 67 6e 6f 43 68 69 6c 64 20 3d 20 67 65 74   pgnoChild = get
15580 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44  4byte(&pPage->aD
15581 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66  ata[pPage->hdrOf
15582 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20 20 61 73  fset+8]);.    as
15583 73 65 72 74 28 20 70 67 6e 6f 43 68 69 6c 64 3e  sert( pgnoChild>
15584 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  0 );.    assert(
15585 20 70 67 6e 6f 43 68 69 6c 64 3c 3d 70 61 67 65   pgnoChild<=page
15586 72 50 61 67 65 63 6f 75 6e 74 28 70 50 61 67 65  rPagecount(pPage
15587 2d 3e 70 42 74 29 20 29 3b 0a 20 20 20 20 72 63  ->pBt) );.    rc
15588 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
15589 65 74 50 61 67 65 28 70 50 61 67 65 2d 3e 70 42  etPage(pPage->pB
1558a 74 2c 20 70 67 6e 6f 43 68 69 6c 64 2c 20 26 70  t, pgnoChild, &p
1558b 43 68 69 6c 64 2c 20 30 29 3b 0a 20 20 20 20 69  Child, 0);.    i
1558c 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
1558d 5f 73 68 61 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65  _shallow_balance
1558e 3b 0a 20 20 20 20 69 66 28 20 70 50 61 67 65 2d  ;.    if( pPage-
1558f 3e 70 67 6e 6f 3d 3d 31 20 29 7b 0a 20 20 20 20  >pgno==1 ){.    
15590 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
15591 72 65 65 49 6e 69 74 50 61 67 65 28 70 43 68 69  reeInitPage(pChi
15592 6c 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ld);.      if( r
15593 63 20 29 20 67 6f 74 6f 20 65 6e 64 5f 73 68 61  c ) goto end_sha
15594 6c 6c 6f 77 5f 62 61 6c 61 6e 63 65 3b 0a 20 20  llow_balance;.  
15595 20 20 20 20 61 73 73 65 72 74 28 20 70 43 68 69      assert( pChi
15596 6c 64 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30  ld->nOverflow==0
15597 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43   );.      if( pC
15598 68 69 6c 64 2d 3e 6e 46 72 65 65 3e 3d 31 30 30  hild->nFree>=100
15599 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
1559a 68 65 20 63 68 69 6c 64 20 69 6e 66 6f 72 6d 61  he child informa
1559b 74 69 6f 6e 20 77 69 6c 6c 20 66 69 74 20 6f 6e  tion will fit on
1559c 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65 2c 20   the root page, 
1559d 73 6f 20 64 6f 20 74 68 65 0a 20 20 20 20 20 20  so do the.      
1559e 20 20 2a 2a 20 63 6f 70 79 20 2a 2f 0a 20 20 20    ** copy */.   
1559f 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20       int i;.    
155a0 20 20 20 20 7a 65 72 6f 50 61 67 65 28 70 50 61      zeroPage(pPa
155a1 67 65 2c 20 70 43 68 69 6c 64 2d 3e 61 44 61 74  ge, pChild->aDat
155a2 61 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20 66  a[0]);.        f
155a3 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 69 6c 64  or(i=0; i<pChild
155a4 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  ->nCell; i++){. 
155a5 20 20 20 20 20 20 20 20 20 61 70 43 65 6c 6c 5b           apCell[
155a6 69 5d 20 3d 20 66 69 6e 64 43 65 6c 6c 28 70 43  i] = findCell(pC
155a7 68 69 6c 64 2c 69 29 3b 0a 20 20 20 20 20 20 20  hild,i);.       
155a8 20 20 20 73 7a 43 65 6c 6c 5b 69 5d 20 3d 20 63     szCell[i] = c
155a9 65 6c 6c 53 69 7a 65 50 74 72 28 70 43 68 69 6c  ellSizePtr(pChil
155aa 64 2c 20 61 70 43 65 6c 6c 5b 69 5d 29 3b 0a 20  d, apCell[i]);. 
155ab 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
155ac 20 61 73 73 65 6d 62 6c 65 50 61 67 65 28 70 50   assemblePage(pP
155ad 61 67 65 2c 20 70 43 68 69 6c 64 2d 3e 6e 43 65  age, pChild->nCe
155ae 6c 6c 2c 20 61 70 43 65 6c 6c 2c 20 73 7a 43 65  ll, apCell, szCe
155af 6c 6c 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ll);.        /* 
155b0 43 6f 70 79 20 74 68 65 20 72 69 67 68 74 2d 70  Copy the right-p
155b1 6f 69 6e 74 65 72 20 6f 66 20 74 68 65 20 63 68  ointer of the ch
155b2 69 6c 64 20 74 6f 20 74 68 65 20 70 61 72 65 6e  ild to the paren
155b3 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 75  t. */.        pu
155b4 74 34 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61  t4byte(&pPage->a
155b5 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f  Data[pPage->hdrO
155b6 66 66 73 65 74 2b 38 5d 2c 20 0a 20 20 20 20 20  ffset+8], .     
155b7 20 20 20 20 20 20 20 67 65 74 34 62 79 74 65 28         get4byte(
155b8 26 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 5b 70  &pChild->aData[p
155b9 43 68 69 6c 64 2d 3e 68 64 72 4f 66 66 73 65 74  Child->hdrOffset
155ba 2b 38 5d 29 29 3b 0a 20 20 20 20 20 20 20 20 66  +8]));.        f
155bb 72 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b  reePage(pChild);
155bc 0a 20 20 20 20 20 20 20 20 54 52 41 43 45 28 28  .        TRACE((
155bd 22 42 41 4c 41 4e 43 45 3a 20 63 68 69 6c 64 20  "BALANCE: child 
155be 25 64 20 74 72 61 6e 73 66 65 72 20 74 6f 20 70  %d transfer to p
155bf 61 67 65 20 31 5c 6e 22 2c 20 70 43 68 69 6c 64  age 1\n", pChild
155c0 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20  ->pgno));.      
155c1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f  }else{.        /
155c2 2a 20 54 68 65 20 63 68 69 6c 64 20 68 61 73 20  * The child has 
155c3 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  more information
155c4 20 74 68 61 74 20 77 69 6c 6c 20 66 69 74 20 6f   that will fit o
155c5 6e 20 74 68 65 20 72 6f 6f 74 2e 0a 20 20 20 20  n the root..    
155c6 20 20 20 20 2a 2a 20 54 68 65 20 74 72 65 65 20      ** The tree 
155c7 69 73 20 61 6c 72 65 61 64 79 20 62 61 6c 61 6e  is already balan
155c8 63 65 64 2e 20 20 44 6f 20 6e 6f 74 68 69 6e 67  ced.  Do nothing
155c9 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 54 52 41  . */.        TRA
155ca 43 45 28 28 22 42 41 4c 41 4e 43 45 3a 20 63 68  CE(("BALANCE: ch
155cb 69 6c 64 20 25 64 20 77 69 6c 6c 20 6e 6f 74 20  ild %d will not 
155cc 66 69 74 20 6f 6e 20 70 61 67 65 20 31 5c 6e 22  fit on page 1\n"
155cd 2c 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29  , pChild->pgno))
155ce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
155cf 6c 73 65 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  lse{.      memcp
155d0 79 28 70 50 61 67 65 2d 3e 61 44 61 74 61 2c 20  y(pPage->aData, 
155d1 70 43 68 69 6c 64 2d 3e 61 44 61 74 61 2c 20 70  pChild->aData, p
155d2 50 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c  Page->pBt->usabl
155d3 65 53 69 7a 65 29 3b 0a 20 20 20 20 20 20 70 50  eSize);.      pP
155d4 61 67 65 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b  age->isInit = 0;
155d5 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
155d6 74 65 33 42 74 72 65 65 49 6e 69 74 50 61 67 65  te3BtreeInitPage
155d7 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 61  (pPage);.      a
155d8 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54  ssert( rc==SQLIT
155d9 45 5f 4f 4b 20 29 3b 0a 20 20 20 20 20 20 66 72  E_OK );.      fr
155da 65 65 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a  eePage(pChild);.
155db 20 20 20 20 20 20 54 52 41 43 45 28 28 22 42 41        TRACE(("BA
155dc 4c 41 4e 43 45 3a 20 74 72 61 6e 73 66 65 72 20  LANCE: transfer 
155dd 63 68 69 6c 64 20 25 64 20 69 6e 74 6f 20 72 6f  child %d into ro
155de 6f 74 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20 20  ot %d\n",.      
155df 20 20 20 20 20 20 20 20 70 43 68 69 6c 64 2d 3e          pChild->
155e0 70 67 6e 6f 2c 20 70 50 61 67 65 2d 3e 70 67 6e  pgno, pPage->pgn
155e1 6f 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61  o));.    }.    a
155e2 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6e 4f  ssert( pPage->nO
155e3 76 65 72 66 6c 6f 77 3d 3d 30 20 29 3b 0a 23 69  verflow==0 );.#i
155e4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
155e5 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 20  T_AUTOVACUUM.   
155e6 20 69 66 28 20 49 53 41 55 54 4f 56 41 43 55 55   if( ISAUTOVACUU
155e7 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  M ){.      rc = 
155e8 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70 73 28  setChildPtrmaps(
155e9 70 50 61 67 65 29 3b 0a 20 20 20 20 7d 0a 23 65  pPage);.    }.#e
155ea 6e 64 69 66 0a 20 20 20 20 72 65 6c 65 61 73 65  ndif.    release
155eb 50 61 67 65 28 70 43 68 69 6c 64 29 3b 0a 20 20  Page(pChild);.  
155ec 7d 0a 65 6e 64 5f 73 68 61 6c 6c 6f 77 5f 62 61  }.end_shallow_ba
155ed 6c 61 6e 63 65 3a 0a 20 20 73 71 6c 69 74 65 33  lance:.  sqlite3
155ee 5f 66 72 65 65 28 61 70 43 65 6c 6c 29 3b 0a 20  _free(apCell);. 
155ef 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a   return rc;.}...
155f0 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70  /*.** The root p
155f1 61 67 65 20 69 73 20 6f 76 65 72 66 75 6c 6c 0a  age is overfull.
155f2 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20  **.** When this 
155f3 68 61 70 70 65 6e 73 2c 20 43 72 65 61 74 65 20  happens, Create 
155f4 61 20 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65  a new child page
155f5 20 61 6e 64 20 63 6f 70 79 20 74 68 65 0a 2a 2a   and copy the.**
155f6 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65   contents of the
155f7 20 72 6f 6f 74 20 69 6e 74 6f 20 74 68 65 20 63   root into the c
155f8 68 69 6c 64 2e 20 20 54 68 65 6e 20 6d 61 6b 65  hild.  Then make
155f9 20 74 68 65 20 72 6f 6f 74 0a 2a 2a 20 70 61 67   the root.** pag
155fa 65 20 61 6e 20 65 6d 70 74 79 20 70 61 67 65 20  e an empty page 
155fb 77 69 74 68 20 72 69 67 68 74 43 68 69 6c 64 20  with rightChild 
155fc 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
155fd 6e 65 77 0a 2a 2a 20 63 68 69 6c 64 2e 20 20 20  new.** child.   
155fe 46 69 6e 61 6c 6c 79 2c 20 63 61 6c 6c 20 62 61  Finally, call ba
155ff 6c 61 6e 63 65 5f 69 6e 74 65 72 6e 61 6c 28 29  lance_internal()
15600 20 6f 6e 20 74 68 65 20 6e 65 77 20 63 68 69 6c   on the new chil
15601 64 0a 2a 2a 20 74 6f 20 63 61 75 73 65 20 69 74  d.** to cause it
15602 20 74 6f 20 73 70 6c 69 74 2e 0a 2a 2f 0a 73 74   to split..*/.st
15603 61 74 69 63 20 69 6e 74 20 62 61 6c 61 6e 63 65  atic int balance
15604 5f 64 65 65 70 65 72 28 42 74 43 75 72 73 6f 72  _deeper(BtCursor
15605 20 2a 70 43 75 72 29 7b 0a 20 20 69 6e 74 20 72   *pCur){.  int r
15606 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c;             /
15607 2a 20 52 65 74 75 72 6e 20 76 61 6c 75 65 20 66  * Return value f
15608 72 6f 6d 20 73 75 62 70 72 6f 63 65 64 75 72 65  rom subprocedure
15609 73 20 2a 2f 0a 20 20 4d 65 6d 50 61 67 65 20 2a  s */.  MemPage *
1560a 70 50 61 67 65 3b 20 20 20 20 20 2f 2a 20 50 6f  pPage;     /* Po
1560b 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 6f 6f  inter to the roo
1560c 74 20 70 61 67 65 20 2a 2f 0a 20 20 4d 65 6d 50  t page */.  MemP
1560d 61 67 65 20 2a 70 43 68 69 6c 64 3b 20 20 20 20  age *pChild;    
1560e 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 61 20  /* Pointer to a 
1560f 6e 65 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a  new child page *
15610 2f 0a 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69  /.  Pgno pgnoChi
15611 6c 64 3b 20 20 20 20 20 2f 2a 20 50 61 67 65 20  ld;     /* Page 
15612 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e 65  number of the ne
15613 77 20 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a  w child page */.
15614 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74 3b    BtShared *pBt;
15615 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
15616 42 54 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 75  BTree */.  int u
15617 73 61 62 6c 65 53 69 7a 65 3b 20 20 20 20 20 2f  sableSize;     /
15618 2a 20 54 6f 74 61 6c 20 75 73 61 62 6c 65 20 73  * Total usable s
15619 69 7a 65 20 6f 66 20 61 20 70 61 67 65 20 2a 2f  ize of a page */
1561a 0a 20 20 75 38 20 2a 64 61 74 61 3b 20 20 20 20  .  u8 *data;    
1561b 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 6e         /* Conten
1561c 74 20 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20  t of the parent 
1561d 70 61 67 65 20 2a 2f 0a 20 20 75 38 20 2a 63 64  page */.  u8 *cd
1561e 61 74 61 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ata;          /*
1561f 20 43 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 20   Content of the 
15620 63 68 69 6c 64 20 70 61 67 65 20 2a 2f 0a 20 20  child page */.  
15621 69 6e 74 20 68 64 72 3b 20 20 20 20 20 20 20 20  int hdr;        
15622 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 74 6f      /* Offset to
15623 20 70 61 67 65 20 68 65 61 64 65 72 20 69 6e 20   page header in 
15624 70 61 72 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20  parent */.  int 
15625 63 62 72 6b 3b 20 20 20 20 20 20 20 20 20 20 20  cbrk;           
15626 2f 2a 20 4f 66 66 73 65 74 20 74 6f 20 63 6f 6e  /* Offset to con
15627 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20 63 65  tent of first ce
15628 6c 6c 20 69 6e 20 70 61 72 65 6e 74 20 2a 2f 0a  ll in parent */.
15629 0a 20 20 61 73 73 65 72 74 28 20 70 43 75 72 2d  .  assert( pCur-
1562a 3e 69 50 61 67 65 3d 3d 30 20 29 3b 0a 20 20 61  >iPage==0 );.  a
1562b 73 73 65 72 74 28 20 70 43 75 72 2d 3e 61 70 50  ssert( pCur->apP
1562c 61 67 65 5b 30 5d 2d 3e 6e 4f 76 65 72 66 6c 6f  age[0]->nOverflo
1562d 77 3e 30 20 29 3b 0a 0a 20 20 56 56 41 5f 4f 4e  w>0 );..  VVA_ON
1562e 4c 59 28 20 70 43 75 72 2d 3e 70 61 67 65 73 53  LY( pCur->pagesS
1562f 68 75 66 66 6c 65 64 20 3d 20 31 20 29 3b 0a 20  huffled = 1 );. 
15630 20 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61   pPage = pCur->a
15631 70 50 61 67 65 5b 30 5d 3b 0a 20 20 70 42 74 20  pPage[0];.  pBt 
15632 3d 20 70 50 61 67 65 2d 3e 70 42 74 3b 0a 20 20  = pPage->pBt;.  
15633 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
15634 6d 75 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e  mutex_held(pBt->
15635 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 63 20 3d  mutex) );.  rc =
15636 20 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61   allocateBtreePa
15637 67 65 28 70 42 74 2c 20 26 70 43 68 69 6c 64 2c  ge(pBt, &pChild,
15638 20 26 70 67 6e 6f 43 68 69 6c 64 2c 20 70 50 61   &pgnoChild, pPa
15639 67 65 2d 3e 70 67 6e 6f 2c 20 30 29 3b 0a 20 20  ge->pgno, 0);.  
1563a 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
1563b 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  rc;.  assert( sq
1563c 6c 69 74 65 33 50 61 67 65 72 49 73 77 72 69 74  lite3PagerIswrit
1563d 65 61 62 6c 65 28 70 43 68 69 6c 64 2d 3e 70 44  eable(pChild->pD
1563e 62 50 61 67 65 29 20 29 3b 0a 20 20 75 73 61 62  bPage) );.  usab
1563f 6c 65 53 69 7a 65 20 3d 20 70 42 74 2d 3e 75 73  leSize = pBt->us
15640 61 62 6c 65 53 69 7a 65 3b 0a 20 20 64 61 74 61  ableSize;.  data
15641 20 3d 20 70 50 61 67 65 2d 3e 61 44 61 74 61 3b   = pPage->aData;
15642 0a 20 20 68 64 72 20 3d 20 70 50 61 67 65 2d 3e  .  hdr = pPage->
15643 68 64 72 4f 66 66 73 65 74 3b 0a 20 20 63 62 72  hdrOffset;.  cbr
15644 6b 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  k = get2byte(&da
15645 74 61 5b 68 64 72 2b 35 5d 29 3b 0a 20 20 63 64  ta[hdr+5]);.  cd
15646 61 74 61 20 3d 20 70 43 68 69 6c 64 2d 3e 61 44  ata = pChild->aD
15647 61 74 61 3b 0a 20 20 6d 65 6d 63 70 79 28 63 64  ata;.  memcpy(cd
15648 61 74 61 2c 20 26 64 61 74 61 5b 68 64 72 5d 2c  ata, &data[hdr],
15649 20 70 50 61 67 65 2d 3e 63 65 6c 6c 4f 66 66 73   pPage->cellOffs
1564a 65 74 2b 32 2a 70 50 61 67 65 2d 3e 6e 43 65 6c  et+2*pPage->nCel
1564b 6c 2d 68 64 72 29 3b 0a 20 20 6d 65 6d 63 70 79  l-hdr);.  memcpy
1564c 28 26 63 64 61 74 61 5b 63 62 72 6b 5d 2c 20 26  (&cdata[cbrk], &
1564d 64 61 74 61 5b 63 62 72 6b 5d 2c 20 75 73 61 62  data[cbrk], usab
1564e 6c 65 53 69 7a 65 2d 63 62 72 6b 29 3b 0a 0a 20  leSize-cbrk);.. 
1564f 20 61 73 73 65 72 74 28 20 70 43 68 69 6c 64 2d   assert( pChild-
15650 3e 69 73 49 6e 69 74 3d 3d 30 20 29 3b 0a 20 20  >isInit==0 );.  
15651 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
15652 65 49 6e 69 74 50 61 67 65 28 70 43 68 69 6c 64  eInitPage(pChild
15653 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
15654 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
15655 74 20 6e 43 6f 70 79 20 3d 20 70 50 61 67 65 2d  t nCopy = pPage-
15656 3e 6e 4f 76 65 72 66 6c 6f 77 2a 73 69 7a 65 6f  >nOverflow*sizeo
15657 66 28 70 50 61 67 65 2d 3e 61 4f 76 66 6c 5b 30  f(pPage->aOvfl[0
15658 5d 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70  ]);.    memcpy(p
15659 43 68 69 6c 64 2d 3e 61 4f 76 66 6c 2c 20 70 50  Child->aOvfl, pP
1565a 61 67 65 2d 3e 61 4f 76 66 6c 2c 20 6e 43 6f 70  age->aOvfl, nCop
1565b 79 29 3b 0a 20 20 20 20 70 43 68 69 6c 64 2d 3e  y);.    pChild->
1565c 6e 4f 76 65 72 66 6c 6f 77 20 3d 20 70 50 61 67  nOverflow = pPag
1565d 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3b 0a 20 20  e->nOverflow;.  
1565e 20 20 69 66 28 20 70 43 68 69 6c 64 2d 3e 6e 4f    if( pChild->nO
1565f 76 65 72 66 6c 6f 77 20 29 7b 0a 20 20 20 20 20  verflow ){.     
15660 20 70 43 68 69 6c 64 2d 3e 6e 46 72 65 65 20 3d   pChild->nFree =
15661 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73   0;.    }.    as
15662 73 65 72 74 28 20 70 43 68 69 6c 64 2d 3e 6e 43  sert( pChild->nC
15663 65 6c 6c 3d 3d 70 50 61 67 65 2d 3e 6e 43 65 6c  ell==pPage->nCel
15664 6c 20 29 3b 0a 20 20 20 20 7a 65 72 6f 50 61 67  l );.    zeroPag
15665 65 28 70 50 61 67 65 2c 20 70 43 68 69 6c 64 2d  e(pPage, pChild-
15666 3e 61 44 61 74 61 5b 30 5d 20 26 20 7e 50 54 46  >aData[0] & ~PTF
15667 5f 4c 45 41 46 29 3b 0a 20 20 20 20 70 75 74 34  _LEAF);.    put4
15668 62 79 74 65 28 26 70 50 61 67 65 2d 3e 61 44 61  byte(&pPage->aDa
15669 74 61 5b 70 50 61 67 65 2d 3e 68 64 72 4f 66 66  ta[pPage->hdrOff
1566a 73 65 74 2b 38 5d 2c 20 70 67 6e 6f 43 68 69 6c  set+8], pgnoChil
1566b 64 29 3b 0a 20 20 20 20 54 52 41 43 45 28 28 22  d);.    TRACE(("
1566c 42 41 4c 41 4e 43 45 3a 20 63 6f 70 79 20 72 6f  BALANCE: copy ro
1566d 6f 74 20 25 64 20 69 6e 74 6f 20 25 64 5c 6e 22  ot %d into %d\n"
1566e 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 2c 20 70  , pPage->pgno, p
1566f 43 68 69 6c 64 2d 3e 70 67 6e 6f 29 29 3b 0a 20  Child->pgno));. 
15670 20 20 20 69 66 28 20 49 53 41 55 54 4f 56 41 43     if( ISAUTOVAC
15671 55 55 4d 20 29 7b 0a 20 20 20 20 20 20 72 63 20  UUM ){.      rc 
15672 3d 20 70 74 72 6d 61 70 50 75 74 28 70 42 74 2c  = ptrmapPut(pBt,
15673 20 70 43 68 69 6c 64 2d 3e 70 67 6e 6f 2c 20 50   pChild->pgno, P
15674 54 52 4d 41 50 5f 42 54 52 45 45 2c 20 70 50 61  TRMAP_BTREE, pPa
15675 67 65 2d 3e 70 67 6e 6f 29 3b 0a 23 69 66 6e 64  ge->pgno);.#ifnd
15676 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
15677 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 20 20  UTOVACUUM.      
15678 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
15679 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20  K ){.        rc 
1567a 3d 20 73 65 74 43 68 69 6c 64 50 74 72 6d 61 70  = setChildPtrmap
1567b 73 28 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 20  s(pChild);.     
1567c 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a   }.#endif.    }.
1567d 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53    }..  if( rc==S
1567e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1567f 70 43 75 72 2d 3e 69 50 61 67 65 2b 2b 3b 0a 20  pCur->iPage++;. 
15680 20 20 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b     pCur->apPage[
15681 31 5d 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 20  1] = pChild;.   
15682 20 70 43 75 72 2d 3e 61 69 49 64 78 5b 30 5d 20   pCur->aiIdx[0] 
15683 3d 20 30 3b 0a 20 20 20 20 72 63 20 3d 20 62 61  = 0;.    rc = ba
15684 6c 61 6e 63 65 5f 6e 6f 6e 72 6f 6f 74 28 70 43  lance_nonroot(pC
15685 75 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ur);.  }else{.  
15686 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 43    releasePage(pC
15687 68 69 6c 64 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  hild);.  }..  re
15688 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15689 2a 20 54 68 65 20 70 61 67 65 20 74 68 61 74 20  * The page that 
1568a 70 43 75 72 20 63 75 72 72 65 6e 74 6c 79 20 70  pCur currently p
1568b 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 6a 75 73  oints to has jus
1568c 74 20 62 65 65 6e 20 6d 6f 64 69 66 69 65 64 20  t been modified 
1568d 69 6e 0a 2a 2a 20 73 6f 6d 65 20 77 61 79 2e 20  in.** some way. 
1568e 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 66 69  This function fi
1568f 67 75 72 65 73 20 6f 75 74 20 69 66 20 74 68 69  gures out if thi
15690 73 20 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 6d  s modification m
15691 65 61 6e 73 20 74 68 65 0a 2a 2a 20 74 72 65 65  eans the.** tree
15692 20 6e 65 65 64 73 20 74 6f 20 62 65 20 62 61 6c   needs to be bal
15693 61 6e 63 65 64 2c 20 61 6e 64 20 69 66 20 73 6f  anced, and if so
15694 20 63 61 6c 6c 73 20 74 68 65 20 61 70 70 72 6f   calls the appro
15695 70 72 69 61 74 65 20 62 61 6c 61 6e 63 69 6e 67  priate balancing
15696 20 0a 2a 2a 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a   .** routine..**
15697 20 0a 2a 2a 20 50 61 72 61 6d 65 74 65 72 20 69   .** Parameter i
15698 73 49 6e 73 65 72 74 20 69 73 20 74 72 75 65 20  sInsert is true 
15699 69 66 20 61 20 6e 65 77 20 63 65 6c 6c 20 77 61  if a new cell wa
1569a 73 20 6a 75 73 74 20 69 6e 73 65 72 74 65 64 20  s just inserted 
1569b 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 70 61 67 65  into the.** page
1569c 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
1569d 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  wise..*/.static 
1569e 69 6e 74 20 62 61 6c 61 6e 63 65 28 42 74 43 75  int balance(BtCu
1569f 72 73 6f 72 20 2a 70 43 75 72 2c 20 69 6e 74 20  rsor *pCur, int 
156a0 69 73 49 6e 73 65 72 74 29 7b 0a 20 20 69 6e 74  isInsert){.  int
156a1 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
156a2 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70 50 61 67  .  MemPage *pPag
156a3 65 20 3d 20 70 43 75 72 2d 3e 61 70 50 61 67 65  e = pCur->apPage
156a4 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 0a  [pCur->iPage];..
156a5 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
156a6 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 50 61  3_mutex_held(pPa
156a7 67 65 2d 3e 70 42 74 2d 3e 6d 75 74 65 78 29 20  ge->pBt->mutex) 
156a8 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 69  );.  if( pCur->i
156a9 50 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 72  Page==0 ){.    r
156aa 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
156ab 57 72 69 74 65 28 70 50 61 67 65 2d 3e 70 44 62  Write(pPage->pDb
156ac 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Page);.    if( r
156ad 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
156ae 70 50 61 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77  pPage->nOverflow
156af 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  >0 ){.      rc =
156b0 20 62 61 6c 61 6e 63 65 5f 64 65 65 70 65 72 28   balance_deeper(
156b1 70 43 75 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  pCur);.    }.   
156b2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
156b3 4f 4b 20 26 26 20 70 50 61 67 65 2d 3e 6e 43 65  OK && pPage->nCe
156b4 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 72  ll==0 ){.      r
156b5 63 20 3d 20 62 61 6c 61 6e 63 65 5f 73 68 61 6c  c = balance_shal
156b6 6c 6f 77 65 72 28 70 43 75 72 29 3b 0a 20 20 20  lower(pCur);.   
156b7 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
156b8 69 66 28 20 70 50 61 67 65 2d 3e 6e 4f 76 65 72  if( pPage->nOver
156b9 66 6c 6f 77 3e 30 20 7c 7c 20 0a 20 20 20 20 20  flow>0 || .     
156ba 20 20 20 28 21 69 73 49 6e 73 65 72 74 20 26 26     (!isInsert &&
156bb 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3e 70 50   pPage->nFree>pP
156bc 61 67 65 2d 3e 70 42 74 2d 3e 75 73 61 62 6c 65  age->pBt->usable
156bd 53 69 7a 65 2a 32 2f 33 29 20 29 7b 0a 20 20 20  Size*2/3) ){.   
156be 20 20 20 72 63 20 3d 20 62 61 6c 61 6e 63 65 5f     rc = balance_
156bf 6e 6f 6e 72 6f 6f 74 28 70 43 75 72 29 3b 0a 20  nonroot(pCur);. 
156c0 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
156c1 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
156c2 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68 65 63  his routine chec
156c3 6b 73 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 74  ks all cursors t
156c4 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 74 61 62  hat point to tab
156c5 6c 65 20 70 67 6e 6f 52 6f 6f 74 2e 0a 2a 2a 20  le pgnoRoot..** 
156c6 49 66 20 61 6e 79 20 6f 66 20 74 68 6f 73 65 20  If any of those 
156c7 63 75 72 73 6f 72 73 20 77 65 72 65 20 6f 70 65  cursors were ope
156c8 6e 65 64 20 77 69 74 68 20 77 72 46 6c 61 67 3d  ned with wrFlag=
156c9 3d 30 20 69 6e 20 61 20 64 69 66 66 65 72 65 6e  =0 in a differen
156ca 74 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 63 6f  t.** database co
156cb 6e 6e 65 63 74 69 6f 6e 20 28 61 20 64 61 74 61  nnection (a data
156cc 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20  base connection 
156cd 74 68 61 74 20 73 68 61 72 65 73 20 74 68 65 20  that shares the 
156ce 70 61 67 65 72 0a 2a 2a 20 63 61 63 68 65 20 77  pager.** cache w
156cf 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ith the current 
156d0 63 6f 6e 6e 65 63 74 69 6f 6e 29 20 61 6e 64 20  connection) and 
156d1 74 68 61 74 20 6f 74 68 65 72 20 63 6f 6e 6e 65  that other conne
156d2 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 6e 6f 74  ction .** is not
156d3 20 69 6e 20 74 68 65 20 52 65 61 64 55 6e 63 6f   in the ReadUnco
156d4 6d 6d 6d 69 74 74 65 64 20 73 74 61 74 65 2c 20  mmmitted state, 
156d5 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
156d6 65 20 72 65 74 75 72 6e 73 20 0a 2a 2a 20 53 51  e returns .** SQ
156d7 4c 49 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2a 0a  LITE_LOCKED..**.
156d8 2a 2a 20 41 73 20 77 65 6c 6c 20 61 73 20 63 75  ** As well as cu
156d9 72 73 6f 72 73 20 77 69 74 68 20 77 72 46 6c 61  rsors with wrFla
156da 67 3d 3d 30 2c 20 63 75 72 73 6f 72 73 20 77 69  g==0, cursors wi
156db 74 68 20 77 72 46 6c 61 67 3d 3d 31 20 61 6e 64  th wrFlag==1 and
156dc 20 0a 2a 2a 20 69 73 49 6e 63 72 62 6c 6f 62 48   .** isIncrblobH
156dd 61 6e 64 6c 65 3d 3d 31 20 61 72 65 20 61 6c 73  andle==1 are als
156de 6f 20 63 6f 6e 73 69 64 65 72 65 64 20 27 72 65  o considered 're
156df 61 64 27 20 63 75 72 73 6f 72 73 2e 20 49 6e 63  ad' cursors. Inc
156e0 72 65 6d 65 6e 74 61 6c 20 0a 2a 2a 20 62 6c 6f  remental .** blo
156e1 62 20 63 75 72 73 6f 72 73 20 61 72 65 20 75 73  b cursors are us
156e2 65 64 20 66 6f 72 20 62 6f 74 68 20 72 65 61 64  ed for both read
156e3 69 6e 67 20 61 6e 64 20 77 72 69 74 69 6e 67 2e  ing and writing.
156e4 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 70 67 6e 6f  .**.** When pgno
156e5 52 6f 6f 74 20 69 73 20 74 68 65 20 72 6f 6f 74  Root is the root
156e6 20 70 61 67 65 20 6f 66 20 61 6e 20 69 6e 74 6b   page of an intk
156e7 65 79 20 74 61 62 6c 65 2c 20 74 68 69 73 20 66  ey table, this f
156e8 75 6e 63 74 69 6f 6e 20 69 73 20 61 6c 73 6f 0a  unction is also.
156e9 2a 2a 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  ** responsible f
156ea 6f 72 20 69 6e 76 61 6c 69 64 61 74 69 6e 67 20  or invalidating 
156eb 69 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62  incremental blob
156ec 20 63 75 72 73 6f 72 73 20 77 68 65 6e 20 74 68   cursors when th
156ed 65 20 74 61 62 6c 65 20 72 6f 77 0a 2a 2a 20 6f  e table row.** o
156ee 6e 20 77 68 69 63 68 20 74 68 65 79 20 61 72 65  n which they are
156ef 20 6f 70 65 6e 65 64 20 69 73 20 64 65 6c 65 74   opened is delet
156f0 65 64 20 6f 72 20 6d 6f 64 69 66 69 65 64 2e 20  ed or modified. 
156f1 43 75 72 73 6f 72 73 20 61 72 65 20 69 6e 76 61  Cursors are inva
156f2 6c 69 64 61 74 65 64 0a 2a 2a 20 61 63 63 6f 72  lidated.** accor
156f3 64 69 6e 67 20 74 6f 20 74 68 65 20 66 6f 6c 6c  ding to the foll
156f4 6f 77 69 6e 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a  owing rules:.**.
156f5 2a 2a 20 20 20 31 29 20 57 68 65 6e 20 42 74 72  **   1) When Btr
156f6 65 65 43 6c 65 61 72 54 61 62 6c 65 28 29 20 69  eeClearTable() i
156f7 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70  s called to comp
156f8 6c 65 74 65 6c 79 20 64 65 6c 65 74 65 20 74 68  letely delete th
156f9 65 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 20 20  e contents.**   
156fa 20 20 20 6f 66 20 61 20 42 2d 54 72 65 65 20 74     of a B-Tree t
156fb 61 62 6c 65 2c 20 70 45 78 63 6c 75 64 65 20 69  able, pExclude i
156fc 73 20 73 65 74 20 74 6f 20 7a 65 72 6f 20 61 6e  s set to zero an
156fd 64 20 70 61 72 61 6d 65 74 65 72 20 69 52 6f 77  d parameter iRow
156fe 20 69 73 20 0a 2a 2a 20 20 20 20 20 20 73 65 74   is .**      set
156ff 20 74 6f 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 49 6e   to non-zero. In
15700 20 74 68 69 73 20 63 61 73 65 20 61 6c 6c 20 69   this case all i
15701 6e 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20  ncremental blob 
15702 63 75 72 73 6f 72 73 20 6f 70 65 6e 0a 2a 2a 20  cursors open.** 
15703 20 20 20 20 20 6f 6e 20 74 68 65 20 74 61 62 6c       on the tabl
15704 65 20 72 6f 6f 74 65 64 20 61 74 20 70 67 6e 6f  e rooted at pgno
15705 52 6f 6f 74 20 61 72 65 20 69 6e 76 61 6c 69 64  Root are invalid
15706 61 74 65 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 32 29  ated..**.**   2)
15707 20 57 68 65 6e 20 42 74 72 65 65 49 6e 73 65 72   When BtreeInser
15708 74 28 29 2c 20 42 74 72 65 65 44 65 6c 65 74 65  t(), BtreeDelete
15709 28 29 20 6f 72 20 42 74 72 65 65 50 75 74 44 61  () or BtreePutDa
1570a 74 61 28 29 20 69 73 20 63 61 6c 6c 65 64 20 74  ta() is called t
1570b 6f 20 0a 2a 2a 20 20 20 20 20 20 6d 6f 64 69 66  o .**      modif
1570c 79 20 61 20 74 61 62 6c 65 20 72 6f 77 20 76 69  y a table row vi
1570d 61 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  a an SQL stateme
1570e 6e 74 2c 20 70 45 78 63 6c 75 64 65 20 69 73 20  nt, pExclude is 
1570f 73 65 74 20 74 6f 20 74 68 65 20 0a 2a 2a 20 20  set to the .**  
15710 20 20 20 20 77 72 69 74 65 20 63 75 72 73 6f 72      write cursor
15711 20 75 73 65 64 20 74 6f 20 64 6f 20 74 68 65 20   used to do the 
15712 6d 6f 64 69 66 69 63 61 74 69 6f 6e 20 61 6e 64  modification and
15713 20 70 61 72 61 6d 65 74 65 72 20 69 52 6f 77 20   parameter iRow 
15714 69 73 20 73 65 74 0a 2a 2a 20 20 20 20 20 20 74  is set.**      t
15715 6f 20 74 68 65 20 69 6e 74 65 67 65 72 20 72 6f  o the integer ro
15716 77 20 69 64 20 6f 66 20 74 68 65 20 42 2d 54 72  w id of the B-Tr
15717 65 65 20 65 6e 74 72 79 20 62 65 69 6e 67 20 6d  ee entry being m
15718 6f 64 69 66 69 65 64 2e 20 55 6e 6c 65 73 73 0a  odified. Unless.
15719 2a 2a 20 20 20 20 20 20 70 45 78 63 6c 75 64 65  **      pExclude
1571a 20 69 73 20 69 74 73 65 6c 66 20 61 6e 20 69 6e   is itself an in
1571b 63 72 65 6d 65 6e 74 61 6c 20 62 6c 6f 62 20 63  cremental blob c
1571c 75 72 73 6f 72 2c 20 74 68 65 6e 20 61 6c 6c 20  ursor, then all 
1571d 69 6e 63 72 65 6d 65 6e 74 61 6c 0a 2a 2a 20 20  incremental.**  
1571e 20 20 20 20 62 6c 6f 62 20 63 75 72 73 6f 72 73      blob cursors
1571f 20 6f 70 65 6e 20 6f 6e 20 72 6f 77 20 69 52 6f   open on row iRo
15720 77 20 6f 66 20 74 68 65 20 42 2d 54 72 65 65 20  w of the B-Tree 
15721 61 72 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e  are invalidated.
15722 0a 2a 2a 0a 2a 2a 20 20 20 33 29 20 49 66 20 62  .**.**   3) If b
15723 6f 74 68 20 70 45 78 63 6c 75 64 65 20 61 6e 64  oth pExclude and
15724 20 69 52 6f 77 20 61 72 65 20 73 65 74 20 74 6f   iRow are set to
15725 20 7a 65 72 6f 2c 20 6e 6f 20 69 6e 63 72 65 6d   zero, no increm
15726 65 6e 74 61 6c 20 62 6c 6f 62 20 0a 2a 2a 20 20  ental blob .**  
15727 20 20 20 20 63 75 72 73 6f 72 73 20 61 72 65 20      cursors are 
15728 69 6e 76 61 6c 69 64 61 74 65 64 2e 0a 2a 2f 0a  invalidated..*/.
15729 73 74 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b  static int check
1572a 52 65 61 64 4c 6f 63 6b 73 28 0a 20 20 42 74 72  ReadLocks(.  Btr
1572b 65 65 20 2a 70 42 74 72 65 65 2c 20 0a 20 20 50  ee *pBtree, .  P
1572c 67 6e 6f 20 70 67 6e 6f 52 6f 6f 74 2c 20 0a 20  gno pgnoRoot, . 
1572d 20 42 74 43 75 72 73 6f 72 20 2a 70 45 78 63 6c   BtCursor *pExcl
1572e 75 64 65 2c 0a 20 20 69 36 34 20 69 52 6f 77 0a  ude,.  i64 iRow.
1572f 29 7b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70  ){.  BtCursor *p
15730 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
15731 74 20 3d 20 70 42 74 72 65 65 2d 3e 70 42 74 3b  t = pBtree->pBt;
15732 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
15733 20 70 42 74 72 65 65 2d 3e 64 62 3b 0a 20 20 61   pBtree->db;.  a
15734 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
15735 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 42  reeHoldsMutex(pB
15736 74 72 65 65 29 20 29 3b 0a 20 20 66 6f 72 28 70  tree) );.  for(p
15737 3d 70 42 74 2d 3e 70 43 75 72 73 6f 72 3b 20 70  =pBt->pCursor; p
15738 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20  ; p=p->pNext){. 
15739 20 20 20 69 66 28 20 70 3d 3d 70 45 78 63 6c 75     if( p==pExclu
1573a 64 65 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  de ) continue;. 
1573b 20 20 20 69 66 28 20 70 2d 3e 70 67 6e 6f 52 6f     if( p->pgnoRo
1573c 6f 74 21 3d 70 67 6e 6f 52 6f 6f 74 20 29 20 63  ot!=pgnoRoot ) c
1573d 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66  ontinue;.#ifndef
1573e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1573f 52 42 4c 4f 42 0a 20 20 20 20 69 66 28 20 70 2d  RBLOB.    if( p-
15740 3e 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c  >isIncrblobHandl
15741 65 20 26 26 20 28 20 0a 20 20 20 20 20 20 20 20  e && ( .        
15742 20 28 21 70 45 78 63 6c 75 64 65 20 26 26 20 69   (!pExclude && i
15743 52 6f 77 29 0a 20 20 20 20 20 20 7c 7c 20 28 70  Row).      || (p
15744 45 78 63 6c 75 64 65 20 26 26 20 21 70 45 78 63  Exclude && !pExc
15745 6c 75 64 65 2d 3e 69 73 49 6e 63 72 62 6c 6f 62  lude->isIncrblob
15746 48 61 6e 64 6c 65 20 26 26 20 70 2d 3e 69 6e 66  Handle && p->inf
15747 6f 2e 6e 4b 65 79 3d 3d 69 52 6f 77 29 0a 20 20  o.nKey==iRow).  
15748 20 20 29 29 7b 0a 20 20 20 20 20 20 70 2d 3e 65    )){.      p->e
15749 53 74 61 74 65 20 3d 20 43 55 52 53 4f 52 5f 49  State = CURSOR_I
1574a 4e 56 41 4c 49 44 3b 0a 20 20 20 20 7d 0a 23 65  NVALID;.    }.#e
1574b 6e 64 69 66 0a 20 20 20 20 69 66 28 20 70 2d 3e  ndif.    if( p->
1574c 65 53 74 61 74 65 21 3d 43 55 52 53 4f 52 5f 56  eState!=CURSOR_V
1574d 41 4c 49 44 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ALID ) continue;
1574e 0a 20 20 20 20 69 66 28 20 70 2d 3e 77 72 46 6c  .    if( p->wrFl
1574f 61 67 3d 3d 30 20 0a 23 69 66 6e 64 65 66 20 53  ag==0 .#ifndef S
15750 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42  QLITE_OMIT_INCRB
15751 4c 4f 42 0a 20 20 20 20 20 7c 7c 20 70 2d 3e 69  LOB.     || p->i
15752 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 0a  sIncrblobHandle.
15753 23 65 6e 64 69 66 0a 20 20 20 20 29 7b 0a 20 20  #endif.    ){.  
15754 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 4f      sqlite3 *dbO
15755 74 68 65 72 20 3d 20 70 2d 3e 70 42 74 72 65 65  ther = p->pBtree
15756 2d 3e 64 62 3b 0a 20 20 20 20 20 20 69 66 28 20  ->db;.      if( 
15757 64 62 4f 74 68 65 72 3d 3d 30 20 7c 7c 0a 20 20  dbOther==0 ||.  
15758 20 20 20 20 20 20 20 28 64 62 4f 74 68 65 72 21         (dbOther!
15759 3d 64 62 20 26 26 20 28 64 62 4f 74 68 65 72 2d  =db && (dbOther-
1575a 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f  >flags & SQLITE_
1575b 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 29  ReadUncommitted)
1575c 3d 3d 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20  ==0) ){.        
1575d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
1575e 43 4b 45 44 3b 0a 20 20 20 20 20 20 7d 0a 20 20  CKED;.      }.  
1575f 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
15760 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
15761 2a 0a 2a 2a 20 49 6e 73 65 72 74 20 61 20 6e 65  *.** Insert a ne
15762 77 20 72 65 63 6f 72 64 20 69 6e 74 6f 20 74 68  w record into th
15763 65 20 42 54 72 65 65 2e 20 20 54 68 65 20 6b 65  e BTree.  The ke
15764 79 20 69 73 20 67 69 76 65 6e 20 62 79 20 28 70  y is given by (p
15765 4b 65 79 2c 6e 4b 65 79 29 0a 2a 2a 20 61 6e 64  Key,nKey).** and
15766 20 74 68 65 20 64 61 74 61 20 69 73 20 67 69 76   the data is giv
15767 65 6e 20 62 79 20 28 70 44 61 74 61 2c 6e 44 61  en by (pData,nDa
15768 74 61 29 2e 20 20 54 68 65 20 63 75 72 73 6f 72  ta).  The cursor
15769 20 69 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f   is used only to
1576a 0a 2a 2a 20 64 65 66 69 6e 65 20 77 68 61 74 20  .** define what 
1576b 74 61 62 6c 65 20 74 68 65 20 72 65 63 6f 72 64  table the record
1576c 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 73 65 72   should be inser
1576d 74 65 64 20 69 6e 74 6f 2e 20 20 54 68 65 20 63  ted into.  The c
1576e 75 72 73 6f 72 0a 2a 2a 20 69 73 20 6c 65 66 74  ursor.** is left
1576f 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 61 20 72   pointing at a r
15770 61 6e 64 6f 6d 20 6c 6f 63 61 74 69 6f 6e 2e 0a  andom location..
15771 2a 2a 0a 2a 2a 20 46 6f 72 20 61 6e 20 49 4e 54  **.** For an INT
15772 4b 45 59 20 74 61 62 6c 65 2c 20 6f 6e 6c 79 20  KEY table, only 
15773 74 68 65 20 6e 4b 65 79 20 76 61 6c 75 65 20 6f  the nKey value o
15774 66 20 74 68 65 20 6b 65 79 20 69 73 20 75 73 65  f the key is use
15775 64 2e 20 20 70 4b 65 79 20 69 73 0a 2a 2a 20 69  d.  pKey is.** i
15776 67 6e 6f 72 65 64 2e 20 20 46 6f 72 20 61 20 5a  gnored.  For a Z
15777 45 52 4f 44 41 54 41 20 74 61 62 6c 65 2c 20 74  ERODATA table, t
15778 68 65 20 70 44 61 74 61 20 61 6e 64 20 6e 44 61  he pData and nDa
15779 74 61 20 61 72 65 20 62 6f 74 68 20 69 67 6e 6f  ta are both igno
1577a 72 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  red..*/.SQLITE_P
1577b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1577c 65 33 42 74 72 65 65 49 6e 73 65 72 74 28 0a 20  e3BtreeInsert(. 
1577d 20 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c   BtCursor *pCur,
1577e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1577f 2f 2a 20 49 6e 73 65 72 74 20 64 61 74 61 20 69  /* Insert data i
15780 6e 74 6f 20 74 68 65 20 74 61 62 6c 65 20 6f 66  nto the table of
15781 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a   this cursor */.
15782 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
15783 65 79 2c 20 69 36 34 20 6e 4b 65 79 2c 20 20 20  ey, i64 nKey,   
15784 20 2f 2a 20 54 68 65 20 6b 65 79 20 6f 66 20 74   /* The key of t
15785 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20 2a 2f  he new record */
15786 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70  .  const void *p
15787 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
15788 20 20 2f 2a 20 54 68 65 20 64 61 74 61 20 6f 66    /* The data of
15789 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
1578a 2a 2f 0a 20 20 69 6e 74 20 6e 5a 65 72 6f 2c 20  */.  int nZero, 
1578b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1578c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
1578d 20 65 78 74 72 61 20 30 20 62 79 74 65 73 20 74   extra 0 bytes t
1578e 6f 20 61 70 70 65 6e 64 20 74 6f 20 64 61 74 61  o append to data
1578f 20 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64   */.  int append
15790 42 69 61 73 20 20 20 20 20 20 20 20 20 20 20 20  Bias            
15791 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
15792 74 68 69 73 20 69 73 20 6c 69 6b 65 6c 79 20 61  this is likely a
15793 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 29 7b 0a 20  n append */.){. 
15794 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 6c   int rc;.  int l
15795 6f 63 3b 0a 20 20 69 6e 74 20 73 7a 4e 65 77 3b  oc;.  int szNew;
15796 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20 4d 65  .  int idx;.  Me
15797 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  mPage *pPage;.  
15798 42 74 72 65 65 20 2a 70 20 3d 20 70 43 75 72 2d  Btree *p = pCur-
15799 3e 70 42 74 72 65 65 3b 0a 20 20 42 74 53 68 61  >pBtree;.  BtSha
1579a 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42  red *pBt = p->pB
1579b 74 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  t;.  unsigned ch
1579c 61 72 20 2a 6f 6c 64 43 65 6c 6c 3b 0a 20 20 75  ar *oldCell;.  u
1579d 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 6e 65  nsigned char *ne
1579e 77 43 65 6c 6c 20 3d 20 30 3b 0a 0a 20 20 61 73  wCell = 0;..  as
1579f 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
157a0 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
157a1 20 20 69 66 28 20 70 42 74 2d 3e 69 6e 54 72 61    if( pBt->inTra
157a2 6e 73 61 63 74 69 6f 6e 21 3d 54 52 41 4e 53 5f  nsaction!=TRANS_
157a3 57 52 49 54 45 20 29 7b 0a 20 20 20 20 2f 2a 20  WRITE ){.    /* 
157a4 4d 75 73 74 20 73 74 61 72 74 20 61 20 74 72 61  Must start a tra
157a5 6e 73 61 63 74 69 6f 6e 20 62 65 66 6f 72 65 20  nsaction before 
157a6 64 6f 69 6e 67 20 61 6e 20 69 6e 73 65 72 74 20  doing an insert 
157a7 2a 2f 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d  */.    rc = pBt-
157a8 3e 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49  >readOnly ? SQLI
157a9 54 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51  TE_READONLY : SQ
157aa 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
157ab 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20  return rc;.  }. 
157ac 20 61 73 73 65 72 74 28 20 21 70 42 74 2d 3e 72   assert( !pBt->r
157ad 65 61 64 4f 6e 6c 79 20 29 3b 0a 20 20 69 66 28  eadOnly );.  if(
157ae 20 21 70 43 75 72 2d 3e 77 72 46 6c 61 67 20 29   !pCur->wrFlag )
157af 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
157b0 49 54 45 5f 50 45 52 4d 3b 20 20 20 2f 2a 20 43  ITE_PERM;   /* C
157b1 75 72 73 6f 72 20 6e 6f 74 20 6f 70 65 6e 20 66  ursor not open f
157b2 6f 72 20 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20  or writing */.  
157b3 7d 0a 20 20 69 66 28 20 63 68 65 63 6b 52 65 61  }.  if( checkRea
157b4 64 4c 6f 63 6b 73 28 70 43 75 72 2d 3e 70 42 74  dLocks(pCur->pBt
157b5 72 65 65 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  ree, pCur->pgnoR
157b6 6f 6f 74 2c 20 70 43 75 72 2c 20 6e 4b 65 79 29  oot, pCur, nKey)
157b7 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
157b8 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a  QLITE_LOCKED; /*
157b9 20 54 68 65 20 74 61 62 6c 65 20 70 43 75 72 20   The table pCur 
157ba 70 6f 69 6e 74 73 20 74 6f 20 68 61 73 20 61 20  points to has a 
157bb 72 65 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d  read lock */.  }
157bc 0a 20 20 69 66 28 20 70 43 75 72 2d 3e 65 53 74  .  if( pCur->eSt
157bd 61 74 65 3d 3d 43 55 52 53 4f 52 5f 46 41 55 4c  ate==CURSOR_FAUL
157be 54 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  T ){.    return 
157bf 70 43 75 72 2d 3e 73 6b 69 70 3b 0a 20 20 7d 0a  pCur->skip;.  }.
157c0 0a 20 20 2f 2a 20 53 61 76 65 20 74 68 65 20 70  .  /* Save the p
157c1 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e 79 20  ositions of any 
157c2 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20 6f 70  other cursors op
157c3 65 6e 20 6f 6e 20 74 68 69 73 20 74 61 62 6c 65  en on this table
157c4 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42 74 72   */.  sqlite3Btr
157c5 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70 43  eeClearCursor(pC
157c6 75 72 29 3b 0a 20 20 69 66 28 20 0a 20 20 20 20  ur);.  if( .    
157c7 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
157c8 20 73 61 76 65 41 6c 6c 43 75 72 73 6f 72 73 28   saveAllCursors(
157c9 70 42 74 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52  pBt, pCur->pgnoR
157ca 6f 6f 74 2c 20 70 43 75 72 29 29 20 7c 7c 0a 20  oot, pCur)) ||. 
157cb 20 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72     SQLITE_OK!=(r
157cc 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
157cd 4d 6f 76 65 74 6f 28 70 43 75 72 2c 20 70 4b 65  Moveto(pCur, pKe
157ce 79 2c 20 6e 4b 65 79 2c 20 61 70 70 65 6e 64 42  y, nKey, appendB
157cf 69 61 73 2c 20 26 6c 6f 63 29 29 0a 20 20 29 7b  ias, &loc)).  ){
157d0 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
157d1 20 20 7d 0a 0a 20 20 70 50 61 67 65 20 3d 20 70    }..  pPage = p
157d2 43 75 72 2d 3e 61 70 50 61 67 65 5b 70 43 75 72  Cur->apPage[pCur
157d3 2d 3e 69 50 61 67 65 5d 3b 0a 20 20 61 73 73 65  ->iPage];.  asse
157d4 72 74 28 20 70 50 61 67 65 2d 3e 69 6e 74 4b 65  rt( pPage->intKe
157d5 79 20 7c 7c 20 6e 4b 65 79 3e 3d 30 20 29 3b 0a  y || nKey>=0 );.
157d6 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
157d7 3e 6c 65 61 66 20 7c 7c 20 21 70 50 61 67 65 2d  >leaf || !pPage-
157d8 3e 69 6e 74 4b 65 79 20 29 3b 0a 20 20 54 52 41  >intKey );.  TRA
157d9 43 45 28 28 22 49 4e 53 45 52 54 3a 20 74 61 62  CE(("INSERT: tab
157da 6c 65 3d 25 64 20 6e 6b 65 79 3d 25 6c 6c 64 20  le=%d nkey=%lld 
157db 6e 64 61 74 61 3d 25 64 20 70 61 67 65 3d 25 64  ndata=%d page=%d
157dc 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20   %s\n",.        
157dd 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74    pCur->pgnoRoot
157de 2c 20 6e 4b 65 79 2c 20 6e 44 61 74 61 2c 20 70  , nKey, nData, p
157df 50 61 67 65 2d 3e 70 67 6e 6f 2c 0a 20 20 20 20  Page->pgno,.    
157e0 20 20 20 20 20 20 6c 6f 63 3d 3d 30 20 3f 20 22        loc==0 ? "
157e1 6f 76 65 72 77 72 69 74 65 22 20 3a 20 22 6e 65  overwrite" : "ne
157e2 77 20 65 6e 74 72 79 22 29 29 3b 0a 20 20 61 73  w entry"));.  as
157e3 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 73 49  sert( pPage->isI
157e4 6e 69 74 20 29 3b 0a 20 20 61 6c 6c 6f 63 61 74  nit );.  allocat
157e5 65 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b  eTempSpace(pBt);
157e6 0a 20 20 6e 65 77 43 65 6c 6c 20 3d 20 70 42 74  .  newCell = pBt
157e7 2d 3e 70 54 6d 70 53 70 61 63 65 3b 0a 20 20 69  ->pTmpSpace;.  i
157e8 66 28 20 6e 65 77 43 65 6c 6c 3d 3d 30 20 29 20  f( newCell==0 ) 
157e9 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
157ea 4d 45 4d 3b 0a 20 20 72 63 20 3d 20 66 69 6c 6c  MEM;.  rc = fill
157eb 49 6e 43 65 6c 6c 28 70 50 61 67 65 2c 20 6e 65  InCell(pPage, ne
157ec 77 43 65 6c 6c 2c 20 70 4b 65 79 2c 20 6e 4b 65  wCell, pKey, nKe
157ed 79 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c  y, pData, nData,
157ee 20 6e 5a 65 72 6f 2c 20 26 73 7a 4e 65 77 29 3b   nZero, &szNew);
157ef 0a 20 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f  .  if( rc ) goto
157f0 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a 20 20 61   end_insert;.  a
157f1 73 73 65 72 74 28 20 73 7a 4e 65 77 3d 3d 63 65  ssert( szNew==ce
157f2 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c  llSizePtr(pPage,
157f3 20 6e 65 77 43 65 6c 6c 29 20 29 3b 0a 20 20 61   newCell) );.  a
157f4 73 73 65 72 74 28 20 73 7a 4e 65 77 3c 3d 4d 58  ssert( szNew<=MX
157f5 5f 43 45 4c 4c 5f 53 49 5a 45 28 70 42 74 29 20  _CELL_SIZE(pBt) 
157f6 29 3b 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d  );.  idx = pCur-
157f7 3e 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61  >aiIdx[pCur->iPa
157f8 67 65 5d 3b 0a 20 20 69 66 28 20 6c 6f 63 3d 3d  ge];.  if( loc==
157f9 30 20 26 26 20 43 55 52 53 4f 52 5f 56 41 4c 49  0 && CURSOR_VALI
157fa 44 3d 3d 70 43 75 72 2d 3e 65 53 74 61 74 65 20  D==pCur->eState 
157fb 29 7b 0a 20 20 20 20 75 31 36 20 73 7a 4f 6c 64  ){.    u16 szOld
157fc 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 64  ;.    assert( id
157fd 78 3c 70 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 29  x<pPage->nCell )
157fe 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
157ff 65 33 50 61 67 65 72 57 72 69 74 65 28 70 50 61  e3PagerWrite(pPa
15800 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ge->pDbPage);.  
15801 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
15802 20 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72    goto end_inser
15803 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6f 6c 64  t;.    }.    old
15804 43 65 6c 6c 20 3d 20 66 69 6e 64 43 65 6c 6c 28  Cell = findCell(
15805 70 50 61 67 65 2c 20 69 64 78 29 3b 0a 20 20 20  pPage, idx);.   
15806 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
15807 66 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  f ){.      memcp
15808 79 28 6e 65 77 43 65 6c 6c 2c 20 6f 6c 64 43 65  y(newCell, oldCe
15809 6c 6c 2c 20 34 29 3b 0a 20 20 20 20 7d 0a 20 20  ll, 4);.    }.  
1580a 20 20 73 7a 4f 6c 64 20 3d 20 63 65 6c 6c 53 69    szOld = cellSi
1580b 7a 65 50 74 72 28 70 50 61 67 65 2c 20 6f 6c 64  zePtr(pPage, old
1580c 43 65 6c 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20  Cell);.    rc = 
1580d 63 6c 65 61 72 43 65 6c 6c 28 70 50 61 67 65 2c  clearCell(pPage,
1580e 20 6f 6c 64 43 65 6c 6c 29 3b 0a 20 20 20 20 69   oldCell);.    i
1580f 66 28 20 72 63 20 29 20 67 6f 74 6f 20 65 6e 64  f( rc ) goto end
15810 5f 69 6e 73 65 72 74 3b 0a 20 20 20 20 72 63 20  _insert;.    rc 
15811 3d 20 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65  = dropCell(pPage
15812 2c 20 69 64 78 2c 20 73 7a 4f 6c 64 29 3b 0a 20  , idx, szOld);. 
15813 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
15814 45 5f 4f 4b 20 29 20 7b 0a 20 20 20 20 20 20 67  E_OK ) {.      g
15815 6f 74 6f 20 65 6e 64 5f 69 6e 73 65 72 74 3b 0a  oto end_insert;.
15816 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
15817 28 20 6c 6f 63 3c 30 20 26 26 20 70 50 61 67 65  ( loc<0 && pPage
15818 2d 3e 6e 43 65 6c 6c 3e 30 20 29 7b 0a 20 20 20  ->nCell>0 ){.   
15819 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e   assert( pPage->
1581a 6c 65 61 66 20 29 3b 0a 20 20 20 20 69 64 78 20  leaf );.    idx 
1581b 3d 20 2b 2b 70 43 75 72 2d 3e 61 69 49 64 78 5b  = ++pCur->aiIdx[
1581c 70 43 75 72 2d 3e 69 50 61 67 65 5d 3b 0a 20 20  pCur->iPage];.  
1581d 20 20 70 43 75 72 2d 3e 69 6e 66 6f 2e 6e 53 69    pCur->info.nSi
1581e 7a 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 75 72  ze = 0;.    pCur
1581f 2d 3e 76 61 6c 69 64 4e 4b 65 79 20 3d 20 30 3b  ->validNKey = 0;
15820 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73  .  }else{.    as
15821 73 65 72 74 28 20 70 50 61 67 65 2d 3e 6c 65 61  sert( pPage->lea
15822 66 20 29 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20  f );.  }.  rc = 
15823 69 6e 73 65 72 74 43 65 6c 6c 28 70 50 61 67 65  insertCell(pPage
15824 2c 20 69 64 78 2c 20 6e 65 77 43 65 6c 6c 2c 20  , idx, newCell, 
15825 73 7a 4e 65 77 2c 20 30 2c 20 30 29 3b 0a 20 20  szNew, 0, 0);.  
15826 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15827 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 69 6e 73  K ) goto end_ins
15828 65 72 74 3b 0a 20 20 72 63 20 3d 20 62 61 6c 61  ert;.  rc = bala
15829 6e 63 65 28 70 43 75 72 2c 20 31 29 3b 0a 20 20  nce(pCur, 1);.  
1582a 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1582b 4b 20 29 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52  K ){.    moveToR
1582c 6f 6f 74 28 70 43 75 72 29 3b 0a 20 20 7d 0a 65  oot(pCur);.  }.e
1582d 6e 64 5f 69 6e 73 65 72 74 3a 0a 20 20 72 65 74  nd_insert:.  ret
1582e 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
1582f 20 44 65 6c 65 74 65 20 74 68 65 20 65 6e 74 72   Delete the entr
15830 79 20 74 68 61 74 20 74 68 65 20 63 75 72 73 6f  y that the curso
15831 72 20 69 73 20 70 6f 69 6e 74 69 6e 67 20 74 6f  r is pointing to
15832 2e 20 20 54 68 65 20 63 75 72 73 6f 72 0a 2a 2a  .  The cursor.**
15833 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69 6e   is left pointin
15834 67 20 61 74 20 61 20 61 72 62 69 74 72 61 72 79  g at a arbitrary
15835 20 6c 6f 63 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51   location..*/.SQ
15836 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15837 20 73 71 6c 69 74 65 33 42 74 72 65 65 44 65 6c   sqlite3BtreeDel
15838 65 74 65 28 42 74 43 75 72 73 6f 72 20 2a 70 43  ete(BtCursor *pC
15839 75 72 29 7b 0a 20 20 4d 65 6d 50 61 67 65 20 2a  ur){.  MemPage *
1583a 70 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70  pPage = pCur->ap
1583b 50 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65  Page[pCur->iPage
1583c 5d 3b 0a 20 20 69 6e 74 20 69 64 78 3b 0a 20 20  ];.  int idx;.  
1583d 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70  unsigned char *p
1583e 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Cell;.  int rc;.
1583f 20 20 50 67 6e 6f 20 70 67 6e 6f 43 68 69 6c 64    Pgno pgnoChild
15840 20 3d 20 30 3b 0a 20 20 42 74 72 65 65 20 2a 70   = 0;.  Btree *p
15841 20 3d 20 70 43 75 72 2d 3e 70 42 74 72 65 65 3b   = pCur->pBtree;
15842 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42 74  .  BtShared *pBt
15843 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61 73   = p->pBt;..  as
15844 73 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64  sert( cursorHold
15845 73 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a  sMutex(pCur) );.
15846 20 20 61 73 73 65 72 74 28 20 70 50 61 67 65 2d    assert( pPage-
15847 3e 69 73 49 6e 69 74 20 29 3b 0a 20 20 69 66 28  >isInit );.  if(
15848 20 70 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74   pBt->inTransact
15849 69 6f 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54 45  ion!=TRANS_WRITE
1584a 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20   ){.    /* Must 
1584b 73 74 61 72 74 20 61 20 74 72 61 6e 73 61 63 74  start a transact
1584c 69 6f 6e 20 62 65 66 6f 72 65 20 64 6f 69 6e 67  ion before doing
1584d 20 61 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20 20   a delete */.   
1584e 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f   rc = pBt->readO
1584f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41  nly ? SQLITE_REA
15850 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45  DONLY : SQLITE_E
15851 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e  RROR;.    return
15852 20 72 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72   rc;.  }.  asser
15853 74 28 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  t( !pBt->readOnl
15854 79 20 29 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  y );.  if( pCur-
15855 3e 65 53 74 61 74 65 3d 3d 43 55 52 53 4f 52 5f  >eState==CURSOR_
15856 46 41 55 4c 54 20 29 7b 0a 20 20 20 20 72 65 74  FAULT ){.    ret
15857 75 72 6e 20 70 43 75 72 2d 3e 73 6b 69 70 3b 0a  urn pCur->skip;.
15858 20 20 7d 0a 20 20 69 66 28 20 70 43 75 72 2d 3e    }.  if( pCur->
15859 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1585a 65 5d 3e 3d 70 50 61 67 65 2d 3e 6e 43 65 6c 6c  e]>=pPage->nCell
1585b 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
1585c 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a  QLITE_ERROR;  /*
1585d 20 54 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e   The cursor is n
1585e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 61  ot pointing to a
1585f 6e 79 74 68 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20  nything */.  }. 
15860 20 69 66 28 20 21 70 43 75 72 2d 3e 77 72 46 6c   if( !pCur->wrFl
15861 61 67 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ag ){.    return
15862 20 53 51 4c 49 54 45 5f 50 45 52 4d 3b 20 20 20   SQLITE_PERM;   
15863 2f 2a 20 44 69 64 20 6e 6f 74 20 6f 70 65 6e 20  /* Did not open 
15864 74 68 69 73 20 63 75 72 73 6f 72 20 66 6f 72 20  this cursor for 
15865 77 72 69 74 69 6e 67 20 2a 2f 0a 20 20 7d 0a 20  writing */.  }. 
15866 20 69 66 28 20 63 68 65 63 6b 52 65 61 64 4c 6f   if( checkReadLo
15867 63 6b 73 28 70 43 75 72 2d 3e 70 42 74 72 65 65  cks(pCur->pBtree
15868 2c 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74  , pCur->pgnoRoot
15869 2c 20 70 43 75 72 2c 20 70 43 75 72 2d 3e 69 6e  , pCur, pCur->in
1586a 66 6f 2e 6e 4b 65 79 29 20 29 7b 0a 20 20 20 20  fo.nKey) ){.    
1586b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f  return SQLITE_LO
1586c 43 4b 45 44 3b 20 2f 2a 20 54 68 65 20 74 61 62  CKED; /* The tab
1586d 6c 65 20 70 43 75 72 20 70 6f 69 6e 74 73 20 74  le pCur points t
1586e 6f 20 68 61 73 20 61 20 72 65 61 64 20 6c 6f 63  o has a read loc
1586f 6b 20 2a 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52  k */.  }..  /* R
15870 65 73 74 6f 72 65 20 74 68 65 20 63 75 72 72 65  estore the curre
15871 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74 69  nt cursor positi
15872 6f 6e 20 28 61 20 6e 6f 2d 6f 70 20 69 66 20 74  on (a no-op if t
15873 68 65 20 63 75 72 73 6f 72 20 69 73 20 6e 6f 74  he cursor is not
15874 20 69 6e 20 0a 20 20 2a 2a 20 43 55 52 53 4f 52   in .  ** CURSOR
15875 5f 52 45 51 55 49 52 45 53 45 45 4b 20 73 74 61  _REQUIRESEEK sta
15876 74 65 29 20 61 6e 64 20 73 61 76 65 20 74 68 65  te) and save the
15877 20 70 6f 73 69 74 69 6f 6e 73 20 6f 66 20 61 6e   positions of an
15878 79 20 6f 74 68 65 72 20 63 75 72 73 6f 72 73 20  y other cursors 
15879 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f 6e 20 74 68  .  ** open on th
1587a 65 20 73 61 6d 65 20 74 61 62 6c 65 2e 20 54 68  e same table. Th
1587b 65 6e 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 50  en call sqlite3P
1587c 61 67 65 72 57 72 69 74 65 28 29 20 6f 6e 20 74  agerWrite() on t
1587d 68 65 20 70 61 67 65 0a 20 20 2a 2a 20 74 68 61  he page.  ** tha
1587e 74 20 74 68 65 20 65 6e 74 72 79 20 77 69 6c 6c  t the entry will
1587f 20 62 65 20 64 65 6c 65 74 65 64 20 66 72 6f 6d   be deleted from
15880 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 0a 20 20  ..  */.  if( .  
15881 20 20 28 72 63 20 3d 20 72 65 73 74 6f 72 65 43    (rc = restoreC
15882 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43  ursorPosition(pC
15883 75 72 29 29 21 3d 30 20 7c 7c 0a 20 20 20 20 28  ur))!=0 ||.    (
15884 72 63 20 3d 20 73 61 76 65 41 6c 6c 43 75 72 73  rc = saveAllCurs
15885 6f 72 73 28 70 42 74 2c 20 70 43 75 72 2d 3e 70  ors(pBt, pCur->p
15886 67 6e 6f 52 6f 6f 74 2c 20 70 43 75 72 29 29 21  gnoRoot, pCur))!
15887 3d 30 20 7c 7c 0a 20 20 20 20 28 72 63 20 3d 20  =0 ||.    (rc = 
15888 73 71 6c 69 74 65 33 50 61 67 65 72 57 72 69 74  sqlite3PagerWrit
15889 65 28 70 50 61 67 65 2d 3e 70 44 62 50 61 67 65  e(pPage->pDbPage
1588a 29 29 21 3d 30 0a 20 20 29 7b 0a 20 20 20 20 72  ))!=0.  ){.    r
1588b 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
1588c 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 63   /* Locate the c
1588d 65 6c 6c 20 77 69 74 68 69 6e 20 69 74 73 20 70  ell within its p
1588e 61 67 65 20 61 6e 64 20 6c 65 61 76 65 20 70 43  age and leave pC
1588f 65 6c 6c 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20  ell pointing to 
15890 74 68 65 0a 20 20 2a 2a 20 64 61 74 61 2e 20 54  the.  ** data. T
15891 68 65 20 63 6c 65 61 72 43 65 6c 6c 28 29 20 63  he clearCell() c
15892 61 6c 6c 20 66 72 65 65 73 20 61 6e 79 20 6f 76  all frees any ov
15893 65 72 66 6c 6f 77 20 70 61 67 65 73 20 61 73 73  erflow pages ass
15894 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65  ociated with the
15895 0a 20 20 2a 2a 20 63 65 6c 6c 2e 20 54 68 65 20  .  ** cell. The 
15896 63 65 6c 6c 20 69 74 73 65 6c 66 20 69 73 20 73  cell itself is s
15897 74 69 6c 6c 20 69 6e 74 61 63 74 2e 0a 20 20 2a  till intact..  *
15898 2f 0a 20 20 69 64 78 20 3d 20 70 43 75 72 2d 3e  /.  idx = pCur->
15899 61 69 49 64 78 5b 70 43 75 72 2d 3e 69 50 61 67  aiIdx[pCur->iPag
1589a 65 5d 3b 0a 20 20 70 43 65 6c 6c 20 3d 20 66 69  e];.  pCell = fi
1589b 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 64  ndCell(pPage, id
1589c 78 29 3b 0a 20 20 69 66 28 20 21 70 50 61 67 65  x);.  if( !pPage
1589d 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 70 67  ->leaf ){.    pg
1589e 6e 6f 43 68 69 6c 64 20 3d 20 67 65 74 34 62 79  noChild = get4by
1589f 74 65 28 70 43 65 6c 6c 29 3b 0a 20 20 7d 0a 20  te(pCell);.  }. 
158a0 20 72 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28   rc = clearCell(
158a1 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20  pPage, pCell);. 
158a2 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
158a3 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20  eturn rc;.  }.. 
158a4 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
158a5 66 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20  f ){.    /*.    
158a6 2a 2a 20 54 68 65 20 65 6e 74 72 79 20 77 65 20  ** The entry we 
158a7 61 72 65 20 61 62 6f 75 74 20 74 6f 20 64 65 6c  are about to del
158a8 65 74 65 20 69 73 20 6e 6f 74 20 61 20 6c 65 61  ete is not a lea
158a9 66 20 73 6f 20 69 66 20 77 65 20 64 6f 20 6e 6f  f so if we do no
158aa 74 0a 20 20 20 20 2a 2a 20 64 6f 20 73 6f 6d 65  t.    ** do some
158ab 74 68 69 6e 67 20 77 65 20 77 69 6c 6c 20 6c 65  thing we will le
158ac 61 76 65 20 61 20 68 6f 6c 65 20 6f 6e 20 61 6e  ave a hole on an
158ad 20 69 6e 74 65 72 6e 61 6c 20 70 61 67 65 2e 0a   internal page..
158ae 20 20 20 20 2a 2a 20 57 65 20 68 61 76 65 20 74      ** We have t
158af 6f 20 66 69 6c 6c 20 74 68 65 20 68 6f 6c 65 20  o fill the hole 
158b0 62 79 20 6d 6f 76 69 6e 67 20 69 6e 20 61 20 63  by moving in a c
158b1 65 6c 6c 20 66 72 6f 6d 20 61 20 6c 65 61 66 2e  ell from a leaf.
158b2 20 20 54 68 65 0a 20 20 20 20 2a 2a 20 6e 65 78    The.    ** nex
158b3 74 20 43 65 6c 6c 20 61 66 74 65 72 20 74 68 65  t Cell after the
158b4 20 6f 6e 65 20 74 6f 20 62 65 20 64 65 6c 65 74   one to be delet
158b5 65 64 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  ed is guaranteed
158b6 20 74 6f 20 65 78 69 73 74 20 61 6e 64 0a 20 20   to exist and.  
158b7 20 20 2a 2a 20 74 6f 20 62 65 20 61 20 6c 65 61    ** to be a lea
158b8 66 20 73 6f 20 77 65 20 63 61 6e 20 75 73 65 20  f so we can use 
158b9 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 42  it..    */.    B
158ba 74 43 75 72 73 6f 72 20 6c 65 61 66 43 75 72 3b  tCursor leafCur;
158bb 0a 20 20 20 20 4d 65 6d 50 61 67 65 20 2a 70 4c  .    MemPage *pL
158bc 65 61 66 50 61 67 65 3b 0a 0a 20 20 20 20 75 6e  eafPage;..    un
158bd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 70 4e 65  signed char *pNe
158be 78 74 3b 0a 20 20 20 20 69 6e 74 20 6e 6f 74 55  xt;.    int notU
158bf 73 65 64 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65  sed;.    unsigne
158c0 64 20 63 68 61 72 20 2a 74 65 6d 70 43 65 6c 6c  d char *tempCell
158c1 20 3d 20 30 3b 0a 20 20 20 20 61 73 73 65 72 74   = 0;.    assert
158c2 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65 79  ( !pPage->intKey
158c3 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   );.    sqlite3B
158c4 74 72 65 65 47 65 74 54 65 6d 70 43 75 72 73 6f  treeGetTempCurso
158c5 72 28 70 43 75 72 2c 20 26 6c 65 61 66 43 75 72  r(pCur, &leafCur
158c6 29 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  );.    rc = sqli
158c7 74 65 33 42 74 72 65 65 4e 65 78 74 28 26 6c 65  te3BtreeNext(&le
158c8 61 66 43 75 72 2c 20 26 6e 6f 74 55 73 65 64 29  afCur, &notUsed)
158c9 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
158ca 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
158cb 20 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72   assert( leafCur
158cc 2e 61 69 49 64 78 5b 6c 65 61 66 43 75 72 2e 69  .aiIdx[leafCur.i
158cd 50 61 67 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20  Page]==0 );.    
158ce 20 20 70 4c 65 61 66 50 61 67 65 20 3d 20 6c 65    pLeafPage = le
158cf 61 66 43 75 72 2e 61 70 50 61 67 65 5b 6c 65 61  afCur.apPage[lea
158d0 66 43 75 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20  fCur.iPage];.   
158d1 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
158d2 61 67 65 72 57 72 69 74 65 28 70 4c 65 61 66 50  agerWrite(pLeafP
158d3 61 67 65 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20  age->pDbPage);. 
158d4 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
158d5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
158d6 20 20 20 20 69 6e 74 20 6c 65 61 66 43 75 72 73      int leafCurs
158d7 6f 72 49 6e 76 61 6c 69 64 20 3d 20 30 3b 0a 20  orInvalid = 0;. 
158d8 20 20 20 20 20 75 31 36 20 73 7a 4e 65 78 74 3b       u16 szNext;
158d9 0a 20 20 20 20 20 20 54 52 41 43 45 28 28 22 44  .      TRACE(("D
158da 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d 25 64 20  ELETE: table=%d 
158db 64 65 6c 65 74 65 20 69 6e 74 65 72 6e 61 6c 20  delete internal 
158dc 66 72 6f 6d 20 25 64 20 72 65 70 6c 61 63 65 20  from %d replace 
158dd 66 72 6f 6d 20 6c 65 61 66 20 25 64 5c 6e 22 2c  from leaf %d\n",
158de 0a 20 20 20 20 20 20 20 20 20 70 43 75 72 2d 3e  .         pCur->
158df 70 67 6e 6f 52 6f 6f 74 2c 20 70 50 61 67 65 2d  pgnoRoot, pPage-
158e0 3e 70 67 6e 6f 2c 20 70 4c 65 61 66 50 61 67 65  >pgno, pLeafPage
158e1 2d 3e 70 67 6e 6f 29 29 3b 0a 20 20 20 20 20 20  ->pgno));.      
158e2 64 72 6f 70 43 65 6c 6c 28 70 50 61 67 65 2c 20  dropCell(pPage, 
158e3 69 64 78 2c 20 63 65 6c 6c 53 69 7a 65 50 74 72  idx, cellSizePtr
158e4 28 70 50 61 67 65 2c 20 70 43 65 6c 6c 29 29 3b  (pPage, pCell));
158e5 0a 20 20 20 20 20 20 70 4e 65 78 74 20 3d 20 66  .      pNext = f
158e6 69 6e 64 43 65 6c 6c 28 70 4c 65 61 66 50 61 67  indCell(pLeafPag
158e7 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 7a 4e  e, 0);.      szN
158e8 65 78 74 20 3d 20 63 65 6c 6c 53 69 7a 65 50 74  ext = cellSizePt
158e9 72 28 70 4c 65 61 66 50 61 67 65 2c 20 70 4e 65  r(pLeafPage, pNe
158ea 78 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  xt);.      asser
158eb 74 28 20 4d 58 5f 43 45 4c 4c 5f 53 49 5a 45 28  t( MX_CELL_SIZE(
158ec 70 42 74 29 3e 3d 73 7a 4e 65 78 74 2b 34 20 29  pBt)>=szNext+4 )
158ed 3b 0a 20 20 20 20 20 20 61 6c 6c 6f 63 61 74 65  ;.      allocate
158ee 54 65 6d 70 53 70 61 63 65 28 70 42 74 29 3b 0a  TempSpace(pBt);.
158ef 20 20 20 20 20 20 74 65 6d 70 43 65 6c 6c 20 3d        tempCell =
158f0 20 70 42 74 2d 3e 70 54 6d 70 53 70 61 63 65 3b   pBt->pTmpSpace;
158f1 0a 20 20 20 20 20 20 69 66 28 20 74 65 6d 70 43  .      if( tempC
158f2 65 6c 6c 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ell==0 ){.      
158f3 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
158f4 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
158f5 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
158f6 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
158f7 72 63 20 3d 20 69 6e 73 65 72 74 43 65 6c 6c 28  rc = insertCell(
158f8 70 50 61 67 65 2c 20 69 64 78 2c 20 70 4e 65 78  pPage, idx, pNex
158f9 74 2d 34 2c 20 73 7a 4e 65 78 74 2b 34 2c 20 74  t-4, szNext+4, t
158fa 65 6d 70 43 65 6c 6c 2c 20 30 29 3b 0a 20 20 20  empCell, 0);.   
158fb 20 20 20 7d 0a 0a 0a 20 20 20 20 20 20 2f 2a 20     }...      /* 
158fc 54 68 65 20 22 69 66 22 20 73 74 61 74 65 6d 65  The "if" stateme
158fd 6e 74 20 69 6e 20 74 68 65 20 6e 65 78 74 20 63  nt in the next c
158fe 6f 64 65 20 62 6c 6f 63 6b 20 69 73 20 63 72 69  ode block is cri
158ff 74 69 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20  tical.  The.    
15900 20 20 2a 2a 20 73 6c 69 67 68 74 65 73 74 20 65    ** slightest e
15901 72 72 6f 72 20 69 6e 20 74 68 61 74 20 73 74 61  rror in that sta
15902 74 65 6d 65 6e 74 20 77 6f 75 6c 64 20 61 6c 6c  tement would all
15903 6f 77 20 53 51 4c 69 74 65 20 74 6f 20 6f 70 65  ow SQLite to ope
15904 72 61 74 65 0a 20 20 20 20 20 20 2a 2a 20 63 6f  rate.      ** co
15905 72 72 65 63 74 6c 79 20 6d 6f 73 74 20 6f 66 20  rrectly most of 
15906 74 68 65 20 74 69 6d 65 20 62 75 74 20 70 72 6f  the time but pro
15907 64 75 63 65 20 76 65 72 79 20 72 61 72 65 20 66  duce very rare f
15908 61 69 6c 75 72 65 73 2e 20 20 54 6f 0a 20 20 20  ailures.  To.   
15909 20 20 20 2a 2a 20 67 75 61 72 64 20 61 67 61 69     ** guard agai
1590a 6e 73 74 20 74 68 69 73 2c 20 74 68 65 20 66 6f  nst this, the fo
1590b 6c 6c 6f 77 69 6e 67 20 6d 61 63 72 6f 73 20 68  llowing macros h
1590c 65 6c 70 20 74 6f 20 76 65 72 69 66 79 20 74 68  elp to verify th
1590d 61 74 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  at.      ** the 
1590e 22 69 66 22 20 73 74 61 74 65 6d 65 6e 74 20 69  "if" statement i
1590f 73 20 77 65 6c 6c 20 74 65 73 74 65 64 2e 0a 20  s well tested.. 
15910 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74 65       */.      te
15911 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e  stcase( pPage->n
15912 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70  Overflow==0 && p
15913 50 61 67 65 2d 3e 6e 46 72 65 65 3c 70 42 74 2d  Page->nFree<pBt-
15914 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20  >usableSize*2/3 
15915 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
15916 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d 3e    && pLeafPage->
15917 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20 3e  nFree+2+szNext >
15918 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65   pBt->usableSize
15919 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74 65  *2/3 );.      te
1591a 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e  stcase( pPage->n
1591b 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20 70  Overflow==0 && p
1591c 50 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42 74  Page->nFree==pBt
1591d 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33  ->usableSize*2/3
1591e 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
1591f 20 20 20 26 26 20 70 4c 65 61 66 50 61 67 65 2d     && pLeafPage-
15920 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74 20  >nFree+2+szNext 
15921 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  > pBt->usableSiz
15922 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20 20 74  e*2/3 );.      t
15923 65 73 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e  estcase( pPage->
15924 6e 4f 76 65 72 66 6c 6f 77 3d 3d 30 20 26 26 20  nOverflow==0 && 
15925 70 50 61 67 65 2d 3e 6e 46 72 65 65 3d 3d 70 42  pPage->nFree==pB
15926 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f  t->usableSize*2/
15927 33 2b 31 20 0a 20 20 20 20 20 20 20 20 20 20 20  3+1 .           
15928 20 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61        && pLeafPa
15929 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65  ge->nFree+2+szNe
1592a 78 74 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65  xt > pBt->usable
1592b 53 69 7a 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20  Size*2/3 );.    
1592c 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 67    testcase( pPag
1592d 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 26  e->nOverflow>0 &
1592e 26 20 70 50 61 67 65 2d 3e 6e 46 72 65 65 3c 3d  & pPage->nFree<=
1592f 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2a  pBt->usableSize*
15930 32 2f 33 0a 20 20 20 20 20 20 20 20 20 20 20 20  2/3.            
15931 20 20 20 20 20 26 26 20 70 4c 65 61 66 50 61 67       && pLeafPag
15932 65 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78  e->nFree+2+szNex
15933 74 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  t > pBt->usableS
15934 69 7a 65 2a 32 2f 33 20 29 3b 0a 20 20 20 20 20  ize*2/3 );.     
15935 20 74 65 73 74 63 61 73 65 28 20 28 70 50 61 67   testcase( (pPag
15936 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20 7c  e->nOverflow>0 |
15937 7c 20 28 70 50 61 67 65 2d 3e 6e 46 72 65 65 20  | (pPage->nFree 
15938 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69 7a  > pBt->usableSiz
15939 65 2a 32 2f 33 29 29 0a 20 20 20 20 20 20 20 20  e*2/3)).        
1593a 20 20 20 20 20 20 20 20 20 26 26 20 70 4c 65 61           && pLea
1593b 66 50 61 67 65 2d 3e 6e 46 72 65 65 2b 32 2b 73  fPage->nFree+2+s
1593c 7a 4e 65 78 74 20 3d 3d 20 70 42 74 2d 3e 75 73  zNext == pBt->us
1593d 61 62 6c 65 53 69 7a 65 2a 32 2f 33 20 29 3b 0a  ableSize*2/3 );.
1593e 0a 0a 20 20 20 20 20 20 69 66 28 20 28 70 50 61  ..      if( (pPa
1593f 67 65 2d 3e 6e 4f 76 65 72 66 6c 6f 77 3e 30 20  ge->nOverflow>0 
15940 7c 7c 20 28 70 50 61 67 65 2d 3e 6e 46 72 65 65  || (pPage->nFree
15941 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   > pBt->usableSi
15942 7a 65 2a 32 2f 33 29 29 20 26 26 0a 20 20 20 20  ze*2/3)) &&.    
15943 20 20 20 20 20 20 28 70 4c 65 61 66 50 61 67 65        (pLeafPage
15944 2d 3e 6e 46 72 65 65 2b 32 2b 73 7a 4e 65 78 74  ->nFree+2+szNext
15945 20 3e 20 70 42 74 2d 3e 75 73 61 62 6c 65 53 69   > pBt->usableSi
15946 7a 65 2a 32 2f 33 29 0a 20 20 20 20 20 20 29 7b  ze*2/3).      ){
15947 0a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73  .        /* This
15948 20 62 72 61 6e 63 68 20 69 73 20 74 61 6b 65 6e   branch is taken
15949 20 69 66 20 74 68 65 20 69 6e 74 65 72 6e 61 6c   if the internal
1594a 20 6e 6f 64 65 20 69 73 20 6e 6f 77 20 65 69 74   node is now eit
1594b 68 65 72 20 6f 76 65 72 66 6c 6f 77 69 6e 67 0a  her overflowing.
1594c 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 75 6e          ** or un
1594d 64 65 72 66 75 6c 6c 20 61 6e 64 20 74 68 65 20  derfull and the 
1594e 6c 65 61 66 20 6e 6f 64 65 20 77 69 6c 6c 20 62  leaf node will b
1594f 65 20 75 6e 64 65 72 66 75 6c 6c 20 61 66 74 65  e underfull afte
15950 72 20 74 68 65 20 6a 75 73 74 20 63 65 6c 6c 20  r the just cell 
15951 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69  .        ** copi
15952 65 64 20 74 6f 20 74 68 65 20 69 6e 74 65 72 6e  ed to the intern
15953 61 6c 20 6e 6f 64 65 20 69 73 20 64 65 6c 65 74  al node is delet
15954 65 64 20 66 72 6f 6d 20 69 74 2e 20 54 68 69 73  ed from it. This
15955 20 69 73 20 61 20 73 70 65 63 69 61 6c 0a 20 20   is a special.  
15956 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20 62 65        ** case be
15957 63 61 75 73 65 20 74 68 65 20 63 61 6c 6c 20 74  cause the call t
15958 6f 20 62 61 6c 61 6e 63 65 28 29 20 74 6f 20 63  o balance() to c
15959 6f 72 72 65 63 74 20 74 68 65 20 69 6e 74 65 72  orrect the inter
1595a 6e 61 6c 20 6e 6f 64 65 0a 20 20 20 20 20 20 20  nal node.       
1595b 20 2a 2a 20 6d 61 79 20 63 68 61 6e 67 65 20 74   ** may change t
1595c 68 65 20 74 72 65 65 20 73 74 72 75 63 74 75 72  he tree structur
1595d 65 20 61 6e 64 20 69 6e 76 61 6c 69 64 61 74 65  e and invalidate
1595e 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
1595f 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68 65 20  .        ** the 
15960 6c 65 61 66 43 75 72 2e 61 70 50 61 67 65 5b 5d  leafCur.apPage[]
15961 20 61 6e 64 20 6c 65 61 66 43 75 72 2e 61 69 49   and leafCur.aiI
15962 64 78 5b 5d 20 61 72 72 61 79 73 2c 20 77 68 69  dx[] arrays, whi
15963 63 68 20 77 69 6c 6c 20 62 65 0a 20 20 20 20 20  ch will be.     
15964 20 20 20 2a 2a 20 75 73 65 64 20 62 79 20 74 68     ** used by th
15965 65 20 62 61 6c 61 6e 63 65 28 29 20 72 65 71 75  e balance() requ
15966 69 72 65 64 20 74 6f 20 63 6f 72 72 65 63 74 20  ired to correct 
15967 74 68 65 20 75 6e 64 65 72 66 75 6c 6c 20 6c 65  the underfull le
15968 61 66 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 6f  af.        ** no
15969 64 65 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  de..        **. 
1596a 20 20 20 20 20 20 20 2a 2a 20 54 68 65 20 66 6f         ** The fo
1596b 72 6d 75 6c 61 20 75 73 65 64 20 69 6e 20 74 68  rmula used in th
1596c 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61 62 6f  e expression abo
1596d 76 65 20 61 72 65 20 62 61 73 65 64 20 6f 6e 20  ve are based on 
1596e 66 61 63 65 74 73 20 6f 66 0a 20 20 20 20 20 20  facets of.      
1596f 20 20 2a 2a 20 74 68 65 20 53 51 4c 69 74 65 20    ** the SQLite 
15970 66 69 6c 65 2d 66 6f 72 6d 61 74 20 74 68 61 74  file-format that
15971 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20 6f   do not change o
15972 76 65 72 20 74 69 6d 65 2e 0a 20 20 20 20 20 20  ver time..      
15973 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 74 65 73    */.        tes
15974 74 63 61 73 65 28 20 70 50 61 67 65 2d 3e 6e 46  tcase( pPage->nF
15975 72 65 65 3d 3d 70 42 74 2d 3e 75 73 61 62 6c 65  ree==pBt->usable
15976 53 69 7a 65 2a 32 2f 33 2b 31 20 29 3b 0a 20 20  Size*2/3+1 );.  
15977 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
15978 70 4c 65 61 66 50 61 67 65 2d 3e 6e 46 72 65 65  pLeafPage->nFree
15979 2b 32 2b 73 7a 4e 65 78 74 3d 3d 70 42 74 2d 3e  +2+szNext==pBt->
1597a 75 73 61 62 6c 65 53 69 7a 65 2a 32 2f 33 2b 31  usableSize*2/3+1
1597b 20 29 3b 0a 20 20 20 20 20 20 20 20 6c 65 61 66   );.        leaf
1597c 43 75 72 73 6f 72 49 6e 76 61 6c 69 64 20 3d 20  CursorInvalid = 
1597d 31 3b 0a 20 20 20 20 20 20 7d 20 20 20 20 20 20  1;.      }      
1597e 20 20 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63    ..      if( rc
1597f 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
15980 20 20 20 20 20 20 20 70 75 74 34 62 79 74 65 28         put4byte(
15981 66 69 6e 64 4f 76 65 72 66 6c 6f 77 43 65 6c 6c  findOverflowCell
15982 28 70 50 61 67 65 2c 20 69 64 78 29 2c 20 70 67  (pPage, idx), pg
15983 6e 6f 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20  noChild);.      
15984 20 20 56 56 41 5f 4f 4e 4c 59 28 20 70 43 75 72    VVA_ONLY( pCur
15985 2d 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20  ->pagesShuffled 
15986 3d 20 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72  = 0 );.        r
15987 63 20 3d 20 62 61 6c 61 6e 63 65 28 70 43 75 72  c = balance(pCur
15988 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  , 0);.      }.. 
15989 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
1598a 49 54 45 5f 4f 4b 20 26 26 20 6c 65 61 66 43 75  ITE_OK && leafCu
1598b 72 73 6f 72 49 6e 76 61 6c 69 64 20 29 7b 0a 20  rsorInvalid ){. 
1598c 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65         /* The le
1598d 61 66 2d 6e 6f 64 65 20 69 73 20 6e 6f 77 20 75  af-node is now u
1598e 6e 64 65 72 66 75 6c 6c 20 61 6e 64 20 73 6f 20  nderfull and so 
1598f 74 68 65 20 74 72 65 65 20 6e 65 65 64 73 20 74  the tree needs t
15990 6f 20 62 65 20 0a 20 20 20 20 20 20 20 20 2a 2a  o be .        **
15991 20 72 65 62 61 6c 61 6e 63 65 64 2e 20 48 6f 77   rebalanced. How
15992 65 76 65 72 2c 20 74 68 65 20 62 61 6c 61 6e 63  ever, the balanc
15993 65 28 29 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e  e() operation on
15994 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 0a 20 20   the internal.  
15995 20 20 20 20 20 20 2a 2a 20 6e 6f 64 65 20 61 62        ** node ab
15996 6f 76 65 20 6d 61 79 20 68 61 76 65 20 6d 6f 64  ove may have mod
15997 69 66 69 65 64 20 74 68 65 20 73 74 72 75 63 74  ified the struct
15998 75 72 65 20 6f 66 20 74 68 65 20 42 2d 54 72 65  ure of the B-Tre
15999 65 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a 2a  e and.        **
1599a 20 73 6f 20 74 68 65 20 63 75 72 72 65 6e 74 20   so the current 
1599b 63 6f 6e 74 65 6e 74 73 20 6f 66 20 6c 65 61 66  contents of leaf
1599c 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64  Cur.apPage[] and
1599d 20 6c 65 61 66 43 75 72 2e 61 69 49 64 78 5b 5d   leafCur.aiIdx[]
1599e 0a 20 20 20 20 20 20 20 20 2a 2a 20 6d 61 79 20  .        ** may 
1599f 6e 6f 74 20 62 65 20 74 72 75 73 74 65 64 2e 0a  not be trusted..
159a0 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20          **.     
159a1 20 20 20 2a 2a 20 49 74 20 69 73 20 6e 6f 74 20     ** It is not 
159a2 70 6f 73 73 69 62 6c 65 20 74 6f 20 63 6f 70 79  possible to copy
159a3 20 74 68 65 20 61 6e 63 65 73 74 72 79 20 66 72   the ancestry fr
159a4 6f 6d 20 70 43 75 72 2c 20 61 73 20 74 68 65 20  om pCur, as the 
159a5 73 61 6d 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  same.        ** 
159a6 62 61 6c 61 6e 63 65 28 29 20 63 61 6c 6c 20 68  balance() call h
159a7 61 73 20 69 6e 76 61 6c 69 64 61 74 65 64 20 74  as invalidated t
159a8 68 65 20 70 43 75 72 2d 3e 61 70 50 61 67 65 5b  he pCur->apPage[
159a9 5d 20 61 6e 64 20 61 69 49 64 78 5b 5d 0a 20 20  ] and aiIdx[].  
159aa 20 20 20 20 20 20 2a 2a 20 61 72 72 61 79 73 2e        ** arrays.
159ab 20 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20   .        **.   
159ac 20 20 20 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c       ** The call
159ad 20 74 6f 20 73 61 76 65 43 75 72 73 6f 72 50 6f   to saveCursorPo
159ae 73 69 74 69 6f 6e 28 29 20 62 65 6c 6f 77 20 69  sition() below i
159af 6e 74 65 72 6e 61 6c 6c 79 20 73 61 76 65 73 20  nternally saves 
159b0 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
159b1 6b 65 79 20 74 68 61 74 20 6c 65 61 66 43 75 72  key that leafCur
159b2 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 70 6f   is currently po
159b3 69 6e 74 69 6e 67 20 74 6f 2e 20 43 75 72 72 65  inting to. Curre
159b4 6e 74 6c 79 2c 20 74 68 65 72 65 0a 20 20 20 20  ntly, there.    
159b5 20 20 20 20 2a 2a 20 61 72 65 20 74 77 6f 20 63      ** are two c
159b6 6f 70 69 65 73 20 6f 66 20 74 68 61 74 20 6b 65  opies of that ke
159b7 79 20 69 6e 20 74 68 65 20 74 72 65 65 20 2d 20  y in the tree - 
159b8 6f 6e 65 20 68 65 72 65 20 6f 6e 20 74 68 65 20  one here on the 
159b9 6c 65 61 66 0a 20 20 20 20 20 20 20 20 2a 2a 20  leaf.        ** 
159ba 70 61 67 65 20 61 6e 64 20 6f 6e 65 20 6f 6e 20  page and one on 
159bb 73 6f 6d 65 20 69 6e 74 65 72 6e 61 6c 20 6e 6f  some internal no
159bc 64 65 20 69 6e 20 74 68 65 20 74 72 65 65 2e 20  de in the tree. 
159bd 54 68 65 20 63 6f 70 79 20 6f 6e 0a 20 20 20 20  The copy on.    
159be 20 20 20 20 2a 2a 20 74 68 65 20 6c 65 61 66 20      ** the leaf 
159bf 6e 6f 64 65 20 69 73 20 61 6c 77 61 79 73 20 74  node is always t
159c0 68 65 20 6e 65 78 74 20 6b 65 79 20 69 6e 20 74  he next key in t
159c1 72 65 65 2d 6f 72 64 65 72 20 61 66 74 65 72 20  ree-order after 
159c2 74 68 65 20 0a 20 20 20 20 20 20 20 20 2a 2a 20  the .        ** 
159c3 63 6f 70 79 20 6f 6e 20 74 68 65 20 69 6e 74 65  copy on the inte
159c4 72 6e 61 6c 20 6e 6f 64 65 2e 20 53 6f 2c 20 74  rnal node. So, t
159c5 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
159c6 65 33 42 74 72 65 65 4e 65 78 74 28 29 0a 20 20  e3BtreeNext().  
159c7 20 20 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 72        ** calls r
159c8 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73 69  estoreCursorPosi
159c9 74 69 6f 6e 28 29 20 74 6f 20 70 6f 69 6e 74 20  tion() to point 
159ca 74 68 65 20 63 75 72 73 6f 72 20 74 6f 20 74 68  the cursor to th
159cb 65 20 63 6f 70 79 0a 20 20 20 20 20 20 20 20 2a  e copy.        *
159cc 2a 20 73 74 6f 72 65 64 20 6f 6e 20 74 68 65 20  * stored on the 
159cd 69 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 2c 20 74  internal node, t
159ce 68 65 6e 20 61 64 76 61 6e 63 65 73 20 74 6f 20  hen advances to 
159cf 74 68 65 20 6e 65 78 74 20 65 6e 74 72 79 2c 0a  the next entry,.
159d0 20 20 20 20 20 20 20 20 2a 2a 20 77 68 69 63 68          ** which
159d1 20 68 61 70 70 65 6e 73 20 74 6f 20 62 65 20 74   happens to be t
159d2 68 65 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b  he copy of the k
159d3 65 79 20 6f 6e 20 74 68 65 20 69 6e 74 65 72 6e  ey on the intern
159d4 61 6c 20 6e 6f 64 65 2e 0a 20 20 20 20 20 20 20  al node..       
159d5 20 2a 2a 20 4e 65 74 20 65 66 66 65 63 74 3a 20   ** Net effect: 
159d6 6c 65 61 66 43 75 72 20 69 73 20 70 6f 69 6e 74  leafCur is point
159d7 69 6e 67 20 62 61 63 6b 20 74 6f 20 74 68 65 20  ing back to the 
159d8 64 75 70 6c 69 63 61 74 65 20 63 65 6c 6c 0a 20  duplicate cell. 
159d9 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 6e         ** that n
159da 65 65 64 73 20 74 6f 20 62 65 20 72 65 6d 6f 76  eeds to be remov
159db 65 64 2c 20 61 6e 64 20 74 68 65 20 6c 65 61 66  ed, and the leaf
159dc 43 75 72 2e 61 70 50 61 67 65 5b 5d 20 61 6e 64  Cur.apPage[] and
159dd 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 65 61 66  .        ** leaf
159de 43 75 72 2e 61 69 49 64 78 5b 5d 20 61 72 72 61  Cur.aiIdx[] arra
159df 79 73 20 61 72 65 20 63 6f 72 72 65 63 74 2e 0a  ys are correct..
159e0 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
159e1 20 20 20 56 56 41 5f 4f 4e 4c 59 28 20 50 67 6e     VVA_ONLY( Pgn
159e2 6f 20 6c 65 61 66 50 67 6e 6f 20 3d 20 70 4c 65  o leafPgno = pLe
159e3 61 66 50 61 67 65 2d 3e 70 67 6e 6f 20 29 3b 0a  afPage->pgno );.
159e4 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 61 76          rc = sav
159e5 65 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28  eCursorPosition(
159e6 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 20 20 20  &leafCur);.     
159e7 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
159e8 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
159e9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
159ea 72 65 65 4e 65 78 74 28 26 6c 65 61 66 43 75 72  reeNext(&leafCur
159eb 2c 20 26 6e 6f 74 55 73 65 64 29 3b 0a 20 20 20  , &notUsed);.   
159ec 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70       }.        p
159ed 4c 65 61 66 50 61 67 65 20 3d 20 6c 65 61 66 43  LeafPage = leafC
159ee 75 72 2e 61 70 50 61 67 65 5b 6c 65 61 66 43 75  ur.apPage[leafCu
159ef 72 2e 69 50 61 67 65 5d 3b 0a 20 20 20 20 20 20  r.iPage];.      
159f0 20 20 61 73 73 65 72 74 28 20 70 4c 65 61 66 50    assert( pLeafP
159f1 61 67 65 2d 3e 70 67 6e 6f 3d 3d 6c 65 61 66 50  age->pgno==leafP
159f2 67 6e 6f 20 29 3b 0a 20 20 20 20 20 20 20 20 61  gno );.        a
159f3 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 2e 61  ssert( leafCur.a
159f4 69 49 64 78 5b 6c 65 61 66 43 75 72 2e 69 50 61  iIdx[leafCur.iPa
159f5 67 65 5d 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ge]==0 );.      
159f6 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  }..      if( rc=
159f7 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
159f8 20 20 20 20 20 20 64 72 6f 70 43 65 6c 6c 28 70        dropCell(p
159f9 4c 65 61 66 50 61 67 65 2c 20 30 2c 20 73 7a 4e  LeafPage, 0, szN
159fa 65 78 74 29 3b 0a 20 20 20 20 20 20 20 20 56 56  ext);.        VV
159fb 41 5f 4f 4e 4c 59 28 20 6c 65 61 66 43 75 72 2e  A_ONLY( leafCur.
159fc 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 3d 20  pagesShuffled = 
159fd 30 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20  0 );.        rc 
159fe 3d 20 62 61 6c 61 6e 63 65 28 26 6c 65 61 66 43  = balance(&leafC
159ff 75 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  ur, 0);.        
15a00 61 73 73 65 72 74 28 20 6c 65 61 66 43 75 72 73  assert( leafCurs
15a01 6f 72 49 6e 76 61 6c 69 64 20 7c 7c 20 21 6c 65  orInvalid || !le
15a02 61 66 43 75 72 2e 70 61 67 65 73 53 68 75 66 66  afCur.pagesShuff
15a03 6c 65 64 0a 20 20 20 20 20 20 20 20 20 20 20 20  led.            
15a04 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15a05 20 20 20 20 20 20 20 7c 7c 20 21 70 43 75 72 2d         || !pCur-
15a06 3e 70 61 67 65 73 53 68 75 66 66 6c 65 64 20 29  >pagesShuffled )
15a07 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
15a08 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
15a09 52 65 6c 65 61 73 65 54 65 6d 70 43 75 72 73 6f  ReleaseTempCurso
15a0a 72 28 26 6c 65 61 66 43 75 72 29 3b 0a 20 20 7d  r(&leafCur);.  }
15a0b 65 6c 73 65 7b 0a 20 20 20 20 54 52 41 43 45 28  else{.    TRACE(
15a0c 28 22 44 45 4c 45 54 45 3a 20 74 61 62 6c 65 3d  ("DELETE: table=
15a0d 25 64 20 64 65 6c 65 74 65 20 66 72 6f 6d 20 6c  %d delete from l
15a0e 65 61 66 20 25 64 5c 6e 22 2c 0a 20 20 20 20 20  eaf %d\n",.     
15a0f 20 20 70 43 75 72 2d 3e 70 67 6e 6f 52 6f 6f 74    pCur->pgnoRoot
15a10 2c 20 70 50 61 67 65 2d 3e 70 67 6e 6f 29 29 3b  , pPage->pgno));
15a11 0a 20 20 20 20 72 63 20 3d 20 64 72 6f 70 43 65  .    rc = dropCe
15a12 6c 6c 28 70 50 61 67 65 2c 20 69 64 78 2c 20 63  ll(pPage, idx, c
15a13 65 6c 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65  ellSizePtr(pPage
15a14 2c 20 70 43 65 6c 6c 29 29 3b 0a 20 20 20 20 69  , pCell));.    i
15a15 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
15a16 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 62   ){.      rc = b
15a17 61 6c 61 6e 63 65 28 70 43 75 72 2c 20 30 29 3b  alance(pCur, 0);
15a18 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28  .    }.  }.  if(
15a19 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
15a1a 7b 0a 20 20 20 20 6d 6f 76 65 54 6f 52 6f 6f 74  {.    moveToRoot
15a1b 28 70 43 75 72 29 3b 0a 20 20 7d 0a 20 20 72 65  (pCur);.  }.  re
15a1c 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
15a1d 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 42  * Create a new B
15a1e 54 72 65 65 20 74 61 62 6c 65 2e 20 20 57 72 69  Tree table.  Wri
15a1f 74 65 20 69 6e 74 6f 20 2a 70 69 54 61 62 6c 65  te into *piTable
15a20 20 74 68 65 20 70 61 67 65 0a 2a 2a 20 6e 75 6d   the page.** num
15a21 62 65 72 20 66 6f 72 20 74 68 65 20 72 6f 6f 74  ber for the root
15a22 20 70 61 67 65 20 6f 66 20 74 68 65 20 6e 65 77   page of the new
15a23 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68   table..**.** Th
15a24 65 20 74 79 70 65 20 6f 66 20 74 79 70 65 20 69  e type of type i
15a25 73 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  s determined by 
15a26 74 68 65 20 66 6c 61 67 73 20 70 61 72 61 6d 65  the flags parame
15a27 74 65 72 2e 20 20 4f 6e 6c 79 20 74 68 65 0a 2a  ter.  Only the.*
15a28 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 6c 75  * following valu
15a29 65 73 20 6f 66 20 66 6c 61 67 73 20 61 72 65 20  es of flags are 
15a2a 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 75 73 65  currently in use
15a2b 2e 20 20 4f 74 68 65 72 20 76 61 6c 75 65 73 20  .  Other values 
15a2c 66 6f 72 0a 2a 2a 20 66 6c 61 67 73 20 6d 69 67  for.** flags mig
15a2d 68 74 20 6e 6f 74 20 77 6f 72 6b 3a 0a 2a 2a 0a  ht not work:.**.
15a2e 2a 2a 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54  **     BTREE_INT
15a2f 4b 45 59 7c 42 54 52 45 45 5f 4c 45 41 46 44 41  KEY|BTREE_LEAFDA
15a30 54 41 20 20 20 20 20 55 73 65 64 20 66 6f 72 20  TA     Used for 
15a31 53 51 4c 20 74 61 62 6c 65 73 20 77 69 74 68 20  SQL tables with 
15a32 72 6f 77 69 64 20 6b 65 79 73 0a 2a 2a 20 20 20  rowid keys.**   
15a33 20 20 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41    BTREE_ZERODATA
15a34 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15a35 20 20 55 73 65 64 20 66 6f 72 20 53 51 4c 20 69    Used for SQL i
15a36 6e 64 69 63 65 73 0a 2a 2f 0a 73 74 61 74 69 63  ndices.*/.static
15a37 20 69 6e 74 20 62 74 72 65 65 43 72 65 61 74 65   int btreeCreate
15a38 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20  Table(Btree *p, 
15a39 69 6e 74 20 2a 70 69 54 61 62 6c 65 2c 20 69 6e  int *piTable, in
15a3a 74 20 66 6c 61 67 73 29 7b 0a 20 20 42 74 53 68  t flags){.  BtSh
15a3b 61 72 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70  ared *pBt = p->p
15a3c 42 74 3b 0a 20 20 4d 65 6d 50 61 67 65 20 2a 70  Bt;.  MemPage *p
15a3d 52 6f 6f 74 3b 0a 20 20 50 67 6e 6f 20 70 67 6e  Root;.  Pgno pgn
15a3e 6f 52 6f 6f 74 3b 0a 20 20 69 6e 74 20 72 63 3b  oRoot;.  int rc;
15a3f 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
15a40 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74  te3BtreeHoldsMut
15a41 65 78 28 70 29 20 29 3b 0a 20 20 69 66 28 20 70  ex(p) );.  if( p
15a42 42 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f  Bt->inTransactio
15a43 6e 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  n!=TRANS_WRITE )
15a44 7b 0a 20 20 20 20 2f 2a 20 4d 75 73 74 20 73 74  {.    /* Must st
15a45 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  art a transactio
15a46 6e 20 66 69 72 73 74 20 2a 2f 0a 20 20 20 20 72  n first */.    r
15a47 63 20 3d 20 70 42 74 2d 3e 72 65 61 64 4f 6e 6c  c = pBt->readOnl
15a48 79 20 3f 20 53 51 4c 49 54 45 5f 52 45 41 44 4f  y ? SQLITE_READO
15a49 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f 45 52 52  NLY : SQLITE_ERR
15a4a 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  OR;.    return r
15a4b 63 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  c;.  }.  assert(
15a4c 20 21 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20   !pBt->readOnly 
15a4d 29 3b 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  );..#ifdef SQLIT
15a4e 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
15a4f 4d 0a 20 20 72 63 20 3d 20 61 6c 6c 6f 63 61 74  M.  rc = allocat
15a50 65 42 74 72 65 65 50 61 67 65 28 70 42 74 2c 20  eBtreePage(pBt, 
15a51 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52 6f 6f  &pRoot, &pgnoRoo
15a52 74 2c 20 31 2c 20 30 29 3b 0a 20 20 69 66 28 20  t, 1, 0);.  if( 
15a53 72 63 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  rc ){.    return
15a54 20 72 63 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20   rc;.  }.#else. 
15a55 20 69 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61   if( pBt->autoVa
15a56 63 75 75 6d 20 29 7b 0a 20 20 20 20 50 67 6e 6f  cuum ){.    Pgno
15a57 20 70 67 6e 6f 4d 6f 76 65 3b 20 20 20 20 20 20   pgnoMove;      
15a58 2f 2a 20 4d 6f 76 65 20 61 20 70 61 67 65 20 68  /* Move a page h
15a59 65 72 65 20 74 6f 20 6d 61 6b 65 20 72 6f 6f 6d  ere to make room
15a5a 20 66 6f 72 20 74 68 65 20 72 6f 6f 74 2d 70 61   for the root-pa
15a5b 67 65 20 2a 2f 0a 20 20 20 20 4d 65 6d 50 61 67  ge */.    MemPag
15a5c 65 20 2a 70 50 61 67 65 4d 6f 76 65 3b 20 2f 2a  e *pPageMove; /*
15a5d 20 54 68 65 20 70 61 67 65 20 74 6f 20 6d 6f 76   The page to mov
15a5e 65 20 74 6f 2e 20 2a 2f 0a 0a 20 20 20 20 2f 2a  e to. */..    /*
15a5f 20 43 72 65 61 74 69 6e 67 20 61 20 6e 65 77 20   Creating a new 
15a60 74 61 62 6c 65 20 6d 61 79 20 70 72 6f 62 61 62  table may probab
15a61 6c 79 20 72 65 71 75 69 72 65 20 6d 6f 76 69 6e  ly require movin
15a62 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 64 61  g an existing da
15a63 74 61 62 61 73 65 0a 20 20 20 20 2a 2a 20 74 6f  tabase.    ** to
15a64 20 6d 61 6b 65 20 72 6f 6f 6d 20 66 6f 72 20 74   make room for t
15a65 68 65 20 6e 65 77 20 74 61 62 6c 65 73 20 72 6f  he new tables ro
15a66 6f 74 20 70 61 67 65 2e 20 49 6e 20 63 61 73 65  ot page. In case
15a67 20 74 68 69 73 20 70 61 67 65 20 74 75 72 6e 73   this page turns
15a68 0a 20 20 20 20 2a 2a 20 6f 75 74 20 74 6f 20 62  .    ** out to b
15a69 65 20 61 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61  e an overflow pa
15a6a 67 65 2c 20 64 65 6c 65 74 65 20 61 6c 6c 20 6f  ge, delete all o
15a6b 76 65 72 66 6c 6f 77 20 70 61 67 65 2d 6d 61 70  verflow page-map
15a6c 20 63 61 63 68 65 73 0a 20 20 20 20 2a 2a 20 68   caches.    ** h
15a6d 65 6c 64 20 62 79 20 6f 70 65 6e 20 63 75 72 73  eld by open curs
15a6e 6f 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ors..    */.    
15a6f 69 6e 76 61 6c 69 64 61 74 65 41 6c 6c 4f 76 65  invalidateAllOve
15a70 72 66 6c 6f 77 43 61 63 68 65 28 70 42 74 29 3b  rflowCache(pBt);
15a71 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 74 68  ..    /* Read th
15a72 65 20 76 61 6c 75 65 20 6f 66 20 6d 65 74 61 5b  e value of meta[
15a73 33 5d 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  3] from the data
15a74 62 61 73 65 20 74 6f 20 64 65 74 65 72 6d 69 6e  base to determin
15a75 65 20 77 68 65 72 65 20 74 68 65 0a 20 20 20 20  e where the.    
15a76 2a 2a 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 20  ** root page of 
15a77 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 73 68  the new table sh
15a78 6f 75 6c 64 20 67 6f 2e 20 6d 65 74 61 5b 33 5d  ould go. meta[3]
15a79 20 69 73 20 74 68 65 20 6c 61 72 67 65 73 74 20   is the largest 
15a7a 72 6f 6f 74 2d 70 61 67 65 0a 20 20 20 20 2a 2a  root-page.    **
15a7b 20 63 72 65 61 74 65 64 20 73 6f 20 66 61 72 2c   created so far,
15a7c 20 73 6f 20 74 68 65 20 6e 65 77 20 72 6f 6f 74   so the new root
15a7d 2d 70 61 67 65 20 69 73 20 28 6d 65 74 61 5b 33  -page is (meta[3
15a7e 5d 2b 31 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ]+1)..    */.   
15a7f 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
15a80 65 65 47 65 74 4d 65 74 61 28 70 2c 20 34 2c 20  eeGetMeta(p, 4, 
15a81 26 70 67 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20  &pgnoRoot);.    
15a82 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15a83 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
15a84 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n rc;.    }.    
15a85 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 0a 20 20 20  pgnoRoot++;..   
15a86 20 2f 2a 20 54 68 65 20 6e 65 77 20 72 6f 6f 74   /* The new root
15a87 2d 70 61 67 65 20 6d 61 79 20 6e 6f 74 20 62 65  -page may not be
15a88 20 61 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 61 20   allocated on a 
15a89 70 6f 69 6e 74 65 72 2d 6d 61 70 20 70 61 67 65  pointer-map page
15a8a 2c 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  , or the.    ** 
15a8b 50 45 4e 44 49 4e 47 5f 42 59 54 45 20 70 61 67  PENDING_BYTE pag
15a8c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 77 68  e..    */.    wh
15a8d 69 6c 65 28 20 70 67 6e 6f 52 6f 6f 74 3d 3d 50  ile( pgnoRoot==P
15a8e 54 52 4d 41 50 5f 50 41 47 45 4e 4f 28 70 42 74  TRMAP_PAGENO(pBt
15a8f 2c 20 70 67 6e 6f 52 6f 6f 74 29 20 7c 7c 0a 20  , pgnoRoot) ||. 
15a90 20 20 20 20 20 20 20 70 67 6e 6f 52 6f 6f 74 3d         pgnoRoot=
15a91 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50 41  =PENDING_BYTE_PA
15a92 47 45 28 70 42 74 29 20 29 7b 0a 20 20 20 20 20  GE(pBt) ){.     
15a93 20 70 67 6e 6f 52 6f 6f 74 2b 2b 3b 0a 20 20 20   pgnoRoot++;.   
15a94 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70   }.    assert( p
15a95 67 6e 6f 52 6f 6f 74 3e 3d 33 20 29 3b 0a 0a 20  gnoRoot>=3 );.. 
15a96 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61     /* Allocate a
15a97 20 70 61 67 65 2e 20 54 68 65 20 70 61 67 65 20   page. The page 
15a98 74 68 61 74 20 63 75 72 72 65 6e 74 6c 79 20 72  that currently r
15a99 65 73 69 64 65 73 20 61 74 20 70 67 6e 6f 52 6f  esides at pgnoRo
15a9a 6f 74 20 77 69 6c 6c 0a 20 20 20 20 2a 2a 20 62  ot will.    ** b
15a9b 65 20 6d 6f 76 65 64 20 74 6f 20 74 68 65 20 61  e moved to the a
15a9c 6c 6c 6f 63 61 74 65 64 20 70 61 67 65 20 28 75  llocated page (u
15a9d 6e 6c 65 73 73 20 74 68 65 20 61 6c 6c 6f 63 61  nless the alloca
15a9e 74 65 64 20 70 61 67 65 20 68 61 70 70 65 6e 73  ted page happens
15a9f 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 73 69 64  .    ** to resid
15aa0 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 29 2e 0a  e at pgnoRoot)..
15aa1 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20      */.    rc = 
15aa2 61 6c 6c 6f 63 61 74 65 42 74 72 65 65 50 61 67  allocateBtreePag
15aa3 65 28 70 42 74 2c 20 26 70 50 61 67 65 4d 6f 76  e(pBt, &pPageMov
15aa4 65 2c 20 26 70 67 6e 6f 4d 6f 76 65 2c 20 70 67  e, &pgnoMove, pg
15aa5 6e 6f 52 6f 6f 74 2c 20 31 29 3b 0a 20 20 20 20  noRoot, 1);.    
15aa6 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15aa7 4b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  K ){.      retur
15aa8 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  n rc;.    }..   
15aa9 20 69 66 28 20 70 67 6e 6f 4d 6f 76 65 21 3d 70   if( pgnoMove!=p
15aaa 67 6e 6f 52 6f 6f 74 20 29 7b 0a 20 20 20 20 20  gnoRoot ){.     
15aab 20 2f 2a 20 70 67 6e 6f 52 6f 6f 74 20 69 73 20   /* pgnoRoot is 
15aac 74 68 65 20 70 61 67 65 20 74 68 61 74 20 77 69  the page that wi
15aad 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ll be used for t
15aae 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f 66 0a  he root-page of.
15aaf 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 77        ** the new
15ab0 20 74 61 62 6c 65 20 28 61 73 73 75 6d 69 6e 67   table (assuming
15ab1 20 61 6e 20 65 72 72 6f 72 20 64 69 64 20 6e 6f   an error did no
15ab2 74 20 6f 63 63 75 72 29 2e 20 42 75 74 20 77 65  t occur). But we
15ab3 20 77 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 61   were.      ** a
15ab4 6c 6c 6f 63 61 74 65 64 20 70 67 6e 6f 4d 6f 76  llocated pgnoMov
15ab5 65 2e 20 49 66 20 72 65 71 75 69 72 65 64 20 28  e. If required (
15ab6 69 2e 65 2e 20 69 66 20 69 74 20 77 61 73 20 6e  i.e. if it was n
15ab7 6f 74 20 61 6c 6c 6f 63 61 74 65 64 0a 20 20 20  ot allocated.   
15ab8 20 20 20 2a 2a 20 62 79 20 65 78 74 65 6e 64 69     ** by extendi
15ab9 6e 67 20 74 68 65 20 66 69 6c 65 29 2c 20 74 68  ng the file), th
15aba 65 20 63 75 72 72 65 6e 74 20 70 61 67 65 20 61  e current page a
15abb 74 20 70 6f 73 69 74 69 6f 6e 20 70 67 6e 6f 4d  t position pgnoM
15abc 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 69 73 20  ove.      ** is 
15abd 61 6c 72 65 61 64 79 20 6a 6f 75 72 6e 61 6c 65  already journale
15abe 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  d..      */.    
15abf 20 20 75 38 20 65 54 79 70 65 3b 0a 20 20 20 20    u8 eType;.    
15ac0 20 20 50 67 6e 6f 20 69 50 74 72 50 61 67 65 3b    Pgno iPtrPage;
15ac1 0a 0a 20 20 20 20 20 20 72 65 6c 65 61 73 65 50  ..      releaseP
15ac2 61 67 65 28 70 50 61 67 65 4d 6f 76 65 29 3b 0a  age(pPageMove);.
15ac3 0a 20 20 20 20 20 20 2f 2a 20 4d 6f 76 65 20 74  .      /* Move t
15ac4 68 65 20 70 61 67 65 20 63 75 72 72 65 6e 74 6c  he page currentl
15ac5 79 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 74 6f  y at pgnoRoot to
15ac6 20 70 67 6e 6f 4d 6f 76 65 2e 20 2a 2f 0a 20 20   pgnoMove. */.  
15ac7 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15ac8 42 74 72 65 65 47 65 74 50 61 67 65 28 70 42 74  BtreeGetPage(pBt
15ac9 2c 20 70 67 6e 6f 52 6f 6f 74 2c 20 26 70 52 6f  , pgnoRoot, &pRo
15aca 6f 74 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  ot, 0);.      if
15acb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
15acc 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  ){.        retur
15acd 6e 20 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20  n rc;.      }.  
15ace 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61 70 47      rc = ptrmapG
15acf 65 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f 6f 74  et(pBt, pgnoRoot
15ad0 2c 20 26 65 54 79 70 65 2c 20 26 69 50 74 72 50  , &eType, &iPtrP
15ad1 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
15ad2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
15ad3 20 65 54 79 70 65 3d 3d 50 54 52 4d 41 50 5f 52   eType==PTRMAP_R
15ad4 4f 4f 54 50 41 47 45 20 7c 7c 20 65 54 79 70 65  OOTPAGE || eType
15ad5 3d 3d 50 54 52 4d 41 50 5f 46 52 45 45 50 41 47  ==PTRMAP_FREEPAG
15ad6 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  E ){.        rel
15ad7 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
15ad8 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
15ad9 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
15ada 20 20 61 73 73 65 72 74 28 20 65 54 79 70 65 21    assert( eType!
15adb 3d 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45  =PTRMAP_ROOTPAGE
15adc 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
15add 28 20 65 54 79 70 65 21 3d 50 54 52 4d 41 50 5f  ( eType!=PTRMAP_
15ade 46 52 45 45 50 41 47 45 20 29 3b 0a 20 20 20 20  FREEPAGE );.    
15adf 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
15ae0 67 65 72 57 72 69 74 65 28 70 52 6f 6f 74 2d 3e  gerWrite(pRoot->
15ae1 70 44 62 50 61 67 65 29 3b 0a 20 20 20 20 20 20  pDbPage);.      
15ae2 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
15ae3 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 6c  K ){.        rel
15ae4 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
15ae5 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
15ae6 72 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  rc;.      }.    
15ae7 20 20 72 63 20 3d 20 72 65 6c 6f 63 61 74 65 50    rc = relocateP
15ae8 61 67 65 28 70 42 74 2c 20 70 52 6f 6f 74 2c 20  age(pBt, pRoot, 
15ae9 65 54 79 70 65 2c 20 69 50 74 72 50 61 67 65 2c  eType, iPtrPage,
15aea 20 70 67 6e 6f 4d 6f 76 65 2c 20 30 29 3b 0a 20   pgnoMove, 0);. 
15aeb 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
15aec 28 70 52 6f 6f 74 29 3b 0a 0a 20 20 20 20 20 20  (pRoot);..      
15aed 2f 2a 20 4f 62 74 61 69 6e 20 74 68 65 20 70 61  /* Obtain the pa
15aee 67 65 20 61 74 20 70 67 6e 6f 52 6f 6f 74 20 2a  ge at pgnoRoot *
15aef 2f 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  /.      if( rc!=
15af0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15af1 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
15af2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
15af3 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
15af4 65 74 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f  etPage(pBt, pgno
15af5 52 6f 6f 74 2c 20 26 70 52 6f 6f 74 2c 20 30 29  Root, &pRoot, 0)
15af6 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
15af7 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15af8 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
15af9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
15afa 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 57   = sqlite3PagerW
15afb 72 69 74 65 28 70 52 6f 6f 74 2d 3e 70 44 62 50  rite(pRoot->pDbP
15afc 61 67 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  age);.      if( 
15afd 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15afe 0a 20 20 20 20 20 20 20 20 72 65 6c 65 61 73 65  .        release
15aff 50 61 67 65 28 70 52 6f 6f 74 29 3b 0a 20 20 20  Page(pRoot);.   
15b00 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
15b01 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
15b02 65 7b 0a 20 20 20 20 20 20 70 52 6f 6f 74 20 3d  e{.      pRoot =
15b03 20 70 50 61 67 65 4d 6f 76 65 3b 0a 20 20 20 20   pPageMove;.    
15b04 7d 20 0a 0a 20 20 20 20 2f 2a 20 55 70 64 61 74  } ..    /* Updat
15b05 65 20 74 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61  e the pointer-ma
15b06 70 20 61 6e 64 20 6d 65 74 61 2d 64 61 74 61 20  p and meta-data 
15b07 77 69 74 68 20 74 68 65 20 6e 65 77 20 72 6f 6f  with the new roo
15b08 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 2e 20 2a  t-page number. *
15b09 2f 0a 20 20 20 20 72 63 20 3d 20 70 74 72 6d 61  /.    rc = ptrma
15b0a 70 50 75 74 28 70 42 74 2c 20 70 67 6e 6f 52 6f  pPut(pBt, pgnoRo
15b0b 6f 74 2c 20 50 54 52 4d 41 50 5f 52 4f 4f 54 50  ot, PTRMAP_ROOTP
15b0c 41 47 45 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  AGE, 0);.    if(
15b0d 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
15b0e 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
15b0f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
15b10 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d  ;.    }.    rc =
15b11 20 73 71 6c 69 74 65 33 42 74 72 65 65 55 70 64   sqlite3BtreeUpd
15b12 61 74 65 4d 65 74 61 28 70 2c 20 34 2c 20 70 67  ateMeta(p, 4, pg
15b13 6e 6f 52 6f 6f 74 29 3b 0a 20 20 20 20 69 66 28  noRoot);.    if(
15b14 20 72 63 20 29 7b 0a 20 20 20 20 20 20 72 65 6c   rc ){.      rel
15b15 65 61 73 65 50 61 67 65 28 70 52 6f 6f 74 29 3b  easePage(pRoot);
15b16 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63  .      return rc
15b17 3b 0a 20 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65  ;.    }..  }else
15b18 7b 0a 20 20 20 20 72 63 20 3d 20 61 6c 6c 6f 63  {.    rc = alloc
15b19 61 74 65 42 74 72 65 65 50 61 67 65 28 70 42 74  ateBtreePage(pBt
15b1a 2c 20 26 70 52 6f 6f 74 2c 20 26 70 67 6e 6f 52  , &pRoot, &pgnoR
15b1b 6f 6f 74 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20  oot, 1, 0);.    
15b1c 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e 20  if( rc ) return 
15b1d 72 63 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  rc;.  }.#endif. 
15b1e 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
15b1f 50 61 67 65 72 49 73 77 72 69 74 65 61 62 6c 65  PagerIswriteable
15b20 28 70 52 6f 6f 74 2d 3e 70 44 62 50 61 67 65 29  (pRoot->pDbPage)
15b21 20 29 3b 0a 20 20 7a 65 72 6f 50 61 67 65 28 70   );.  zeroPage(p
15b22 52 6f 6f 74 2c 20 66 6c 61 67 73 20 7c 20 50 54  Root, flags | PT
15b23 46 5f 4c 45 41 46 29 3b 0a 20 20 73 71 6c 69 74  F_LEAF);.  sqlit
15b24 65 33 50 61 67 65 72 55 6e 72 65 66 28 70 52 6f  e3PagerUnref(pRo
15b25 6f 74 2d 3e 70 44 62 50 61 67 65 29 3b 0a 20 20  ot->pDbPage);.  
15b26 2a 70 69 54 61 62 6c 65 20 3d 20 28 69 6e 74 29  *piTable = (int)
15b27 70 67 6e 6f 52 6f 6f 74 3b 0a 20 20 72 65 74 75  pgnoRoot;.  retu
15b28 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
15b29 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
15b2a 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65 43  nt sqlite3BtreeC
15b2b 72 65 61 74 65 54 61 62 6c 65 28 42 74 72 65 65  reateTable(Btree
15b2c 20 2a 70 2c 20 69 6e 74 20 2a 70 69 54 61 62 6c   *p, int *piTabl
15b2d 65 2c 20 69 6e 74 20 66 6c 61 67 73 29 7b 0a 20  e, int flags){. 
15b2e 20 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74   int rc;.  sqlit
15b2f 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 29 3b  e3BtreeEnter(p);
15b30 0a 20 20 70 2d 3e 70 42 74 2d 3e 64 62 20 3d 20  .  p->pBt->db = 
15b31 70 2d 3e 64 62 3b 0a 20 20 72 63 20 3d 20 62 74  p->db;.  rc = bt
15b32 72 65 65 43 72 65 61 74 65 54 61 62 6c 65 28 70  reeCreateTable(p
15b33 2c 20 70 69 54 61 62 6c 65 2c 20 66 6c 61 67 73  , piTable, flags
15b34 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15b35 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
15b36 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
15b37 20 45 72 61 73 65 20 74 68 65 20 67 69 76 65 6e   Erase the given
15b38 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 61   database page a
15b39 6e 64 20 61 6c 6c 20 69 74 73 20 63 68 69 6c 64  nd all its child
15b3a 72 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20  ren.  Return.** 
15b3b 74 68 65 20 70 61 67 65 20 74 6f 20 74 68 65 20  the page to the 
15b3c 66 72 65 65 6c 69 73 74 2e 0a 2a 2f 0a 73 74 61  freelist..*/.sta
15b3d 74 69 63 20 69 6e 74 20 63 6c 65 61 72 44 61 74  tic int clearDat
15b3e 61 62 61 73 65 50 61 67 65 28 0a 20 20 42 74 53  abasePage(.  BtS
15b3f 68 61 72 65 64 20 2a 70 42 74 2c 20 20 20 20 20  hared *pBt,     
15b40 20 20 20 20 20 20 2f 2a 20 54 68 65 20 42 54 72        /* The BTr
15b41 65 65 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73  ee that contains
15b42 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
15b43 50 67 6e 6f 20 70 67 6e 6f 2c 20 20 20 20 20 20  Pgno pgno,      
15b44 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e 75        /* Page nu
15b45 6d 62 65 72 20 74 6f 20 63 6c 65 61 72 20 2a 2f  mber to clear */
15b46 0a 20 20 69 6e 74 20 66 72 65 65 50 61 67 65 46  .  int freePageF
15b47 6c 61 67 2c 20 20 20 20 20 2f 2a 20 44 65 61 6c  lag,     /* Deal
15b48 6c 6f 63 61 74 65 20 70 61 67 65 20 69 66 20 74  locate page if t
15b49 72 75 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  rue */.  int *pn
15b4a 43 68 61 6e 67 65 0a 29 7b 0a 20 20 4d 65 6d 50  Change.){.  MemP
15b4b 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30 3b 0a  age *pPage = 0;.
15b4c 20 20 69 6e 74 20 72 63 3b 0a 20 20 75 6e 73 69    int rc;.  unsi
15b4d 67 6e 65 64 20 63 68 61 72 20 2a 70 43 65 6c 6c  gned char *pCell
15b4e 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
15b4f 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
15b50 74 65 78 5f 68 65 6c 64 28 70 42 74 2d 3e 6d 75  tex_held(pBt->mu
15b51 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 67  tex) );.  if( pg
15b52 6e 6f 3e 70 61 67 65 72 50 61 67 65 63 6f 75 6e  no>pagerPagecoun
15b53 74 28 70 42 74 29 20 29 7b 0a 20 20 20 20 72 65  t(pBt) ){.    re
15b54 74 75 72 6e 20 53 51 4c 49 54 45 5f 43 4f 52 52  turn SQLITE_CORR
15b55 55 50 54 5f 42 4b 50 54 3b 0a 20 20 7d 0a 0a 20  UPT_BKPT;.  }.. 
15b56 20 72 63 20 3d 20 67 65 74 41 6e 64 49 6e 69 74   rc = getAndInit
15b57 50 61 67 65 28 70 42 74 2c 20 70 67 6e 6f 2c 20  Page(pBt, pgno, 
15b58 26 70 50 61 67 65 29 3b 0a 20 20 69 66 28 20 72  &pPage);.  if( r
15b59 63 20 29 20 67 6f 74 6f 20 63 6c 65 61 72 64 61  c ) goto clearda
15b5a 74 61 62 61 73 65 70 61 67 65 5f 6f 75 74 3b 0a  tabasepage_out;.
15b5b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61    for(i=0; i<pPa
15b5c 67 65 2d 3e 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b  ge->nCell; i++){
15b5d 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20 66 69 6e  .    pCell = fin
15b5e 64 43 65 6c 6c 28 70 50 61 67 65 2c 20 69 29 3b  dCell(pPage, i);
15b5f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65 2d  .    if( !pPage-
15b60 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20 72  >leaf ){.      r
15b61 63 20 3d 20 63 6c 65 61 72 44 61 74 61 62 61 73  c = clearDatabas
15b62 65 50 61 67 65 28 70 42 74 2c 20 67 65 74 34 62  ePage(pBt, get4b
15b63 79 74 65 28 70 43 65 6c 6c 29 2c 20 31 2c 20 70  yte(pCell), 1, p
15b64 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 20 20  nChange);.      
15b65 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c  if( rc ) goto cl
15b66 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65 5f  eardatabasepage_
15b67 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72  out;.    }.    r
15b68 63 20 3d 20 63 6c 65 61 72 43 65 6c 6c 28 70 50  c = clearCell(pP
15b69 61 67 65 2c 20 70 43 65 6c 6c 29 3b 0a 20 20 20  age, pCell);.   
15b6a 20 69 66 28 20 72 63 20 29 20 67 6f 74 6f 20 63   if( rc ) goto c
15b6b 6c 65 61 72 64 61 74 61 62 61 73 65 70 61 67 65  leardatabasepage
15b6c 5f 6f 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  _out;.  }.  if( 
15b6d 21 70 50 61 67 65 2d 3e 6c 65 61 66 20 29 7b 0a  !pPage->leaf ){.
15b6e 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44 61      rc = clearDa
15b6f 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c 20  tabasePage(pBt, 
15b70 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
15b71 3e 61 44 61 74 61 5b 38 5d 29 2c 20 31 2c 20 70  >aData[8]), 1, p
15b72 6e 43 68 61 6e 67 65 29 3b 0a 20 20 20 20 69 66  nChange);.    if
15b73 28 20 72 63 20 29 20 67 6f 74 6f 20 63 6c 65 61  ( rc ) goto clea
15b74 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
15b75 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  t;.  }else if( p
15b76 6e 43 68 61 6e 67 65 20 29 7b 0a 20 20 20 20 61  nChange ){.    a
15b77 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 6e  ssert( pPage->in
15b78 74 4b 65 79 20 29 3b 0a 20 20 20 20 2a 70 6e 43  tKey );.    *pnC
15b79 68 61 6e 67 65 20 2b 3d 20 70 50 61 67 65 2d 3e  hange += pPage->
15b7a 6e 43 65 6c 6c 3b 0a 20 20 7d 0a 20 20 69 66 28  nCell;.  }.  if(
15b7b 20 66 72 65 65 50 61 67 65 46 6c 61 67 20 29 7b   freePageFlag ){
15b7c 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61  .    rc = freePa
15b7d 67 65 28 70 50 61 67 65 29 3b 0a 20 20 7d 65 6c  ge(pPage);.  }el
15b7e 73 65 20 69 66 28 20 28 72 63 20 3d 20 73 71 6c  se if( (rc = sql
15b7f 69 74 65 33 50 61 67 65 72 57 72 69 74 65 28 70  ite3PagerWrite(p
15b80 50 61 67 65 2d 3e 70 44 62 50 61 67 65 29 29 3d  Page->pDbPage))=
15b81 3d 30 20 29 7b 0a 20 20 20 20 7a 65 72 6f 50 61  =0 ){.    zeroPa
15b82 67 65 28 70 50 61 67 65 2c 20 70 50 61 67 65 2d  ge(pPage, pPage-
15b83 3e 61 44 61 74 61 5b 30 5d 20 7c 20 50 54 46 5f  >aData[0] | PTF_
15b84 4c 45 41 46 29 3b 0a 20 20 7d 0a 0a 63 6c 65 61  LEAF);.  }..clea
15b85 72 64 61 74 61 62 61 73 65 70 61 67 65 5f 6f 75  rdatabasepage_ou
15b86 74 3a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  t:.  releasePage
15b87 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72  (pPage);.  retur
15b88 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  n rc;.}../*.** D
15b89 65 6c 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d  elete all inform
15b8a 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 73 69 6e  ation from a sin
15b8b 67 6c 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  gle table in the
15b8c 20 64 61 74 61 62 61 73 65 2e 20 20 69 54 61 62   database.  iTab
15b8d 6c 65 20 69 73 0a 2a 2a 20 74 68 65 20 70 61 67  le is.** the pag
15b8e 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  e number of the 
15b8f 72 6f 6f 74 20 6f 66 20 74 68 65 20 74 61 62 6c  root of the tabl
15b90 65 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72  e.  After this r
15b91 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 2c 0a  outine returns,.
15b92 2a 2a 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  ** the root page
15b93 20 69 73 20 65 6d 70 74 79 2c 20 62 75 74 20 73   is empty, but s
15b94 74 69 6c 6c 20 65 78 69 73 74 73 2e 0a 2a 2a 0a  till exists..**.
15b95 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
15b96 77 69 6c 6c 20 66 61 69 6c 20 77 69 74 68 20 53  will fail with S
15b97 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 20 69 66 20  QLITE_LOCKED if 
15b98 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 6f 70  there are any op
15b99 65 6e 0a 2a 2a 20 72 65 61 64 20 63 75 72 73 6f  en.** read curso
15b9a 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e  rs on the table.
15b9b 20 20 4f 70 65 6e 20 77 72 69 74 65 20 63 75 72    Open write cur
15b9c 73 6f 72 73 20 61 72 65 20 6d 6f 76 65 64 20 74  sors are moved t
15b9d 6f 20 74 68 65 0a 2a 2a 20 72 6f 6f 74 20 6f 66  o the.** root of
15b9e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a   the table..**.*
15b9f 2a 20 49 66 20 70 6e 43 68 61 6e 67 65 20 69 73  * If pnChange is
15ba0 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20   not NULL, then 
15ba1 74 61 62 6c 65 20 69 54 61 62 6c 65 20 6d 75 73  table iTable mus
15ba2 74 20 62 65 20 61 6e 20 69 6e 74 6b 65 79 20 74  t be an intkey t
15ba3 61 62 6c 65 2e 20 54 68 65 0a 2a 2a 20 69 6e 74  able. The.** int
15ba4 65 67 65 72 20 76 61 6c 75 65 20 70 6f 69 6e 74  eger value point
15ba5 65 64 20 74 6f 20 62 79 20 70 6e 43 68 61 6e 67  ed to by pnChang
15ba6 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  e is incremented
15ba7 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
15ba8 66 0a 2a 2a 20 65 6e 74 72 69 65 73 20 69 6e 20  f.** entries in 
15ba9 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51  the table..*/.SQ
15baa 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
15bab 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 65   sqlite3BtreeCle
15bac 61 72 54 61 62 6c 65 28 42 74 72 65 65 20 2a 70  arTable(Btree *p
15bad 2c 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 69 6e  , int iTable, in
15bae 74 20 2a 70 6e 43 68 61 6e 67 65 29 7b 0a 20 20  t *pnChange){.  
15baf 69 6e 74 20 72 63 3b 0a 20 20 42 74 53 68 61 72  int rc;.  BtShar
15bb0 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
15bb1 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
15bb2 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d  Enter(p);.  pBt-
15bb3 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69  >db = p->db;.  i
15bb4 66 28 20 70 2d 3e 69 6e 54 72 61 6e 73 21 3d 54  f( p->inTrans!=T
15bb5 52 41 4e 53 5f 57 52 49 54 45 20 29 7b 0a 20 20  RANS_WRITE ){.  
15bb6 20 20 72 63 20 3d 20 70 42 74 2d 3e 72 65 61 64    rc = pBt->read
15bb7 4f 6e 6c 79 20 3f 20 53 51 4c 49 54 45 5f 52 45  Only ? SQLITE_RE
15bb8 41 44 4f 4e 4c 59 20 3a 20 53 51 4c 49 54 45 5f  ADONLY : SQLITE_
15bb9 45 52 52 4f 52 3b 0a 20 20 7d 65 6c 73 65 20 69  ERROR;.  }else i
15bba 66 28 20 28 72 63 20 3d 20 63 68 65 63 6b 52 65  f( (rc = checkRe
15bbb 61 64 4c 6f 63 6b 73 28 70 2c 20 69 54 61 62 6c  adLocks(p, iTabl
15bbc 65 2c 20 30 2c 20 31 29 29 21 3d 53 51 4c 49 54  e, 0, 1))!=SQLIT
15bbd 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 6e  E_OK ){.    /* n
15bbe 6f 74 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a  othing to do */.
15bbf 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49    }else if( SQLI
15bc0 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73 61 76  TE_OK!=(rc = sav
15bc1 65 41 6c 6c 43 75 72 73 6f 72 73 28 70 42 74 2c  eAllCursors(pBt,
15bc2 20 69 54 61 62 6c 65 2c 20 30 29 29 20 29 7b 0a   iTable, 0)) ){.
15bc3 20 20 20 20 2f 2a 20 6e 6f 74 68 69 6e 67 20 74      /* nothing t
15bc4 6f 20 64 6f 20 2a 2f 0a 20 20 7d 65 6c 73 65 7b  o do */.  }else{
15bc5 0a 20 20 20 20 72 63 20 3d 20 63 6c 65 61 72 44  .    rc = clearD
15bc6 61 74 61 62 61 73 65 50 61 67 65 28 70 42 74 2c  atabasePage(pBt,
15bc7 20 28 50 67 6e 6f 29 69 54 61 62 6c 65 2c 20 30   (Pgno)iTable, 0
15bc8 2c 20 70 6e 43 68 61 6e 67 65 29 3b 0a 20 20 7d  , pnChange);.  }
15bc9 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c  .  sqlite3BtreeL
15bca 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74 75 72  eave(p);.  retur
15bcb 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  n rc;.}../*.** E
15bcc 72 61 73 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61  rase all informa
15bcd 74 69 6f 6e 20 69 6e 20 61 20 74 61 62 6c 65 20  tion in a table 
15bce 61 6e 64 20 61 64 64 20 74 68 65 20 72 6f 6f 74  and add the root
15bcf 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
15bd0 0a 2a 2a 20 74 68 65 20 66 72 65 65 6c 69 73 74  .** the freelist
15bd1 2e 20 20 45 78 63 65 70 74 2c 20 74 68 65 20 72  .  Except, the r
15bd2 6f 6f 74 20 6f 66 20 74 68 65 20 70 72 69 6e 63  oot of the princ
15bd3 69 70 6c 65 20 74 61 62 6c 65 20 28 74 68 65 20  iple table (the 
15bd4 6f 6e 65 20 6f 6e 0a 2a 2a 20 70 61 67 65 20 31  one on.** page 1
15bd5 29 20 69 73 20 6e 65 76 65 72 20 61 64 64 65 64  ) is never added
15bd6 20 74 6f 20 74 68 65 20 66 72 65 65 6c 69 73 74   to the freelist
15bd7 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
15bd8 74 69 6e 65 20 77 69 6c 6c 20 66 61 69 6c 20 77  tine will fail w
15bd9 69 74 68 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  ith SQLITE_LOCKE
15bda 44 20 69 66 20 74 68 65 72 65 20 61 72 65 20 61  D if there are a
15bdb 6e 79 20 6f 70 65 6e 0a 2a 2a 20 63 75 72 73 6f  ny open.** curso
15bdc 72 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 2e  rs on the table.
15bdd 0a 2a 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41  .**.** If AUTOVA
15bde 43 55 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20  CUUM is enabled 
15bdf 61 6e 64 20 74 68 65 20 70 61 67 65 20 61 74 20  and the page at 
15be0 69 54 61 62 6c 65 20 69 73 20 6e 6f 74 20 74 68  iTable is not th
15be1 65 20 6c 61 73 74 0a 2a 2a 20 72 6f 6f 74 20 70  e last.** root p
15be2 61 67 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  age in the datab
15be3 61 73 65 20 66 69 6c 65 2c 20 74 68 65 6e 20 74  ase file, then t
15be4 68 65 20 6c 61 73 74 20 72 6f 6f 74 20 70 61 67  he last root pag
15be5 65 20 0a 2a 2a 20 69 6e 20 74 68 65 20 64 61 74  e .** in the dat
15be6 61 62 61 73 65 20 66 69 6c 65 20 69 73 20 6d 6f  abase file is mo
15be7 76 65 64 20 69 6e 74 6f 20 74 68 65 20 73 6c 6f  ved into the slo
15be8 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63 75 70  t formerly occup
15be9 69 65 64 20 62 79 0a 2a 2a 20 69 54 61 62 6c 65  ied by.** iTable
15bea 20 61 6e 64 20 74 68 61 74 20 6c 61 73 74 20 73   and that last s
15beb 6c 6f 74 20 66 6f 72 6d 65 72 6c 79 20 6f 63 63  lot formerly occ
15bec 75 70 69 65 64 20 62 79 20 74 68 65 20 6c 61 73  upied by the las
15bed 74 20 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69  t root page.** i
15bee 73 20 61 64 64 65 64 20 74 6f 20 74 68 65 20 66  s added to the f
15bef 72 65 65 6c 69 73 74 20 69 6e 73 74 65 61 64 20  reelist instead 
15bf0 6f 66 20 69 54 61 62 6c 65 2e 20 20 49 6e 20 74  of iTable.  In t
15bf1 68 69 73 20 73 61 79 2c 20 61 6c 6c 0a 2a 2a 20  his say, all.** 
15bf2 72 6f 6f 74 20 70 61 67 65 73 20 61 72 65 20 6b  root pages are k
15bf3 65 70 74 20 61 74 20 74 68 65 20 62 65 67 69 6e  ept at the begin
15bf4 6e 69 6e 67 20 6f 66 20 74 68 65 20 64 61 74 61  ning of the data
15bf5 62 61 73 65 20 66 69 6c 65 2c 20 77 68 69 63 68  base file, which
15bf6 0a 2a 2a 20 69 73 20 6e 65 63 65 73 73 61 72 79  .** is necessary
15bf7 20 66 6f 72 20 41 55 54 4f 56 41 43 55 55 4d 20   for AUTOVACUUM 
15bf8 74 6f 20 77 6f 72 6b 20 72 69 67 68 74 2e 20 20  to work right.  
15bf9 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65 74 20  *piMoved is set 
15bfa 74 6f 20 74 68 65 20 0a 2a 2a 20 70 61 67 65 20  to the .** page 
15bfb 6e 75 6d 62 65 72 20 74 68 61 74 20 75 73 65 64  number that used
15bfc 20 74 6f 20 62 65 20 74 68 65 20 6c 61 73 74 20   to be the last 
15bfd 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 74 68 65  root page in the
15bfe 20 66 69 6c 65 20 62 65 66 6f 72 65 0a 2a 2a 20   file before.** 
15bff 74 68 65 20 6d 6f 76 65 2e 20 20 49 66 20 6e 6f  the move.  If no
15c00 20 70 61 67 65 20 67 65 74 73 20 6d 6f 76 65 64   page gets moved
15c01 2c 20 2a 70 69 4d 6f 76 65 64 20 69 73 20 73 65  , *piMoved is se
15c02 74 20 74 6f 20 30 2e 0a 2a 2a 20 54 68 65 20 6c  t to 0..** The l
15c03 61 73 74 20 72 6f 6f 74 20 70 61 67 65 20 69 73  ast root page is
15c04 20 72 65 63 6f 72 64 65 64 20 69 6e 20 6d 65 74   recorded in met
15c05 61 5b 33 5d 20 61 6e 64 20 74 68 65 20 76 61 6c  a[3] and the val
15c06 75 65 20 6f 66 0a 2a 2a 20 6d 65 74 61 5b 33 5d  ue of.** meta[3]
15c07 20 69 73 20 75 70 64 61 74 65 64 20 62 79 20 74   is updated by t
15c08 68 69 73 20 70 72 6f 63 65 64 75 72 65 2e 0a 2a  his procedure..*
15c09 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72  /.static int btr
15c0a 65 65 44 72 6f 70 54 61 62 6c 65 28 42 74 72 65  eeDropTable(Btre
15c0b 65 20 2a 70 2c 20 50 67 6e 6f 20 69 54 61 62 6c  e *p, Pgno iTabl
15c0c 65 2c 20 69 6e 74 20 2a 70 69 4d 6f 76 65 64 29  e, int *piMoved)
15c0d 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65  {.  int rc;.  Me
15c0e 6d 50 61 67 65 20 2a 70 50 61 67 65 20 3d 20 30  mPage *pPage = 0
15c0f 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  ;.  BtShared *pB
15c10 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a 20 20 61  t = p->pBt;..  a
15c11 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
15c12 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 70 29  reeHoldsMutex(p)
15c13 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54   );.  if( p->inT
15c14 72 61 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54  rans!=TRANS_WRIT
15c15 45 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  E ){.    return 
15c16 70 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3f 20  pBt->readOnly ? 
15c17 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
15c18 3a 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  : SQLITE_ERROR;.
15c19 20 20 7d 0a 0a 20 20 2f 2a 20 49 74 20 69 73 20    }..  /* It is 
15c1a 69 6c 6c 65 67 61 6c 20 74 6f 20 64 72 6f 70 20  illegal to drop 
15c1b 61 20 74 61 62 6c 65 20 69 66 20 61 6e 79 20 63  a table if any c
15c1c 75 72 73 6f 72 73 20 61 72 65 20 6f 70 65 6e 20  ursors are open 
15c1d 6f 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  on the.  ** data
15c1e 62 61 73 65 2e 20 54 68 69 73 20 69 73 20 62 65  base. This is be
15c1f 63 61 75 73 65 20 69 6e 20 61 75 74 6f 2d 76 61  cause in auto-va
15c20 63 75 75 6d 20 6d 6f 64 65 20 74 68 65 20 62 61  cuum mode the ba
15c21 63 6b 65 6e 64 20 6d 61 79 0a 20 20 2a 2a 20 6e  ckend may.  ** n
15c22 65 65 64 20 74 6f 20 6d 6f 76 65 20 61 6e 6f 74  eed to move anot
15c23 68 65 72 20 72 6f 6f 74 2d 70 61 67 65 20 74 6f  her root-page to
15c24 20 66 69 6c 6c 20 61 20 67 61 70 20 6c 65 66 74   fill a gap left
15c25 20 62 79 20 74 68 65 20 64 65 6c 65 74 65 64 0a   by the deleted.
15c26 20 20 2a 2a 20 72 6f 6f 74 20 70 61 67 65 2e 20    ** root page. 
15c27 49 66 20 61 6e 20 6f 70 65 6e 20 63 75 72 73 6f  If an open curso
15c28 72 20 77 61 73 20 75 73 69 6e 67 20 74 68 69 73  r was using this
15c29 20 70 61 67 65 20 61 20 70 72 6f 62 6c 65 6d 20   page a problem 
15c2a 77 6f 75 6c 64 20 0a 20 20 2a 2a 20 6f 63 63 75  would .  ** occu
15c2b 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 42  r..  */.  if( pB
15c2c 74 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a 20 20  t->pCursor ){.  
15c2d 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
15c2e 4c 4f 43 4b 45 44 3b 0a 20 20 7d 0a 0a 20 20 72  LOCKED;.  }..  r
15c2f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
15c30 47 65 74 50 61 67 65 28 70 42 74 2c 20 28 50 67  GetPage(pBt, (Pg
15c31 6e 6f 29 69 54 61 62 6c 65 2c 20 26 70 50 61 67  no)iTable, &pPag
15c32 65 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 20  e, 0);.  if( rc 
15c33 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 72  ) return rc;.  r
15c34 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
15c35 43 6c 65 61 72 54 61 62 6c 65 28 70 2c 20 69 54  ClearTable(p, iT
15c36 61 62 6c 65 2c 20 30 29 3b 0a 20 20 69 66 28 20  able, 0);.  if( 
15c37 72 63 20 29 7b 0a 20 20 20 20 72 65 6c 65 61 73  rc ){.    releas
15c38 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20  ePage(pPage);.  
15c39 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
15c3a 0a 0a 20 20 2a 70 69 4d 6f 76 65 64 20 3d 20 30  ..  *piMoved = 0
15c3b 3b 0a 0a 20 20 69 66 28 20 69 54 61 62 6c 65 3e  ;..  if( iTable>
15c3c 31 20 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49  1 ){.#ifdef SQLI
15c3d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15c3e 55 4d 0a 20 20 20 20 72 63 20 3d 20 66 72 65 65  UM.    rc = free
15c3f 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20 20 20  Page(pPage);.   
15c40 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61   releasePage(pPa
15c41 67 65 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 69  ge);.#else.    i
15c42 66 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75  f( pBt->autoVacu
15c43 75 6d 20 29 7b 0a 20 20 20 20 20 20 50 67 6e 6f  um ){.      Pgno
15c44 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a 20 20   maxRootPgno;.  
15c45 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15c46 42 74 72 65 65 47 65 74 4d 65 74 61 28 70 2c 20  BtreeGetMeta(p, 
15c47 34 2c 20 26 6d 61 78 52 6f 6f 74 50 67 6e 6f 29  4, &maxRootPgno)
15c48 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
15c49 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
15c4a 20 20 20 20 20 72 65 6c 65 61 73 65 50 61 67 65       releasePage
15c4b 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20  (pPage);.       
15c4c 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20   return rc;.    
15c4d 20 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 69    }..      if( i
15c4e 54 61 62 6c 65 3d 3d 6d 61 78 52 6f 6f 74 50 67  Table==maxRootPg
15c4f 6e 6f 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  no ){.        /*
15c50 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   If the table be
15c51 69 6e 67 20 64 72 6f 70 70 65 64 20 69 73 20 74  ing dropped is t
15c52 68 65 20 74 61 62 6c 65 20 77 69 74 68 20 74 68  he table with th
15c53 65 20 6c 61 72 67 65 73 74 20 72 6f 6f 74 2d 70  e largest root-p
15c54 61 67 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e  age.        ** n
15c55 75 6d 62 65 72 20 69 6e 20 74 68 65 20 64 61 74  umber in the dat
15c56 61 62 61 73 65 2c 20 70 75 74 20 74 68 65 20 72  abase, put the r
15c57 6f 6f 74 20 70 61 67 65 20 6f 6e 20 74 68 65 20  oot page on the 
15c58 66 72 65 65 20 6c 69 73 74 2e 20 0a 20 20 20 20  free list. .    
15c59 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
15c5a 63 20 3d 20 66 72 65 65 50 61 67 65 28 70 50 61  c = freePage(pPa
15c5b 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 6c  ge);.        rel
15c5c 65 61 73 65 50 61 67 65 28 70 50 61 67 65 29 3b  easePage(pPage);
15c5d 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
15c5e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
15c5f 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
15c60 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  c;.        }.   
15c61 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
15c62 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62    /* The table b
15c63 65 69 6e 67 20 64 72 6f 70 70 65 64 20 64 6f 65  eing dropped doe
15c64 73 20 6e 6f 74 20 68 61 76 65 20 74 68 65 20 6c  s not have the l
15c65 61 72 67 65 73 74 20 72 6f 6f 74 2d 70 61 67 65  argest root-page
15c66 0a 20 20 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62  .        ** numb
15c67 65 72 20 69 6e 20 74 68 65 20 64 61 74 61 62 61  er in the databa
15c68 73 65 2e 20 53 6f 20 6d 6f 76 65 20 74 68 65 20  se. So move the 
15c69 70 61 67 65 20 74 68 61 74 20 64 6f 65 73 20 69  page that does i
15c6a 6e 74 6f 20 74 68 65 20 0a 20 20 20 20 20 20 20  nto the .       
15c6b 20 2a 2a 20 67 61 70 20 6c 65 66 74 20 62 79 20   ** gap left by 
15c6c 74 68 65 20 64 65 6c 65 74 65 64 20 72 6f 6f 74  the deleted root
15c6d 2d 70 61 67 65 2e 0a 20 20 20 20 20 20 20 20 2a  -page..        *
15c6e 2f 0a 20 20 20 20 20 20 20 20 4d 65 6d 50 61 67  /.        MemPag
15c6f 65 20 2a 70 4d 6f 76 65 3b 0a 20 20 20 20 20 20  e *pMove;.      
15c70 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
15c71 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 72 63  age);.        rc
15c72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47   = sqlite3BtreeG
15c73 65 74 50 61 67 65 28 70 42 74 2c 20 6d 61 78 52  etPage(pBt, maxR
15c74 6f 6f 74 50 67 6e 6f 2c 20 26 70 4d 6f 76 65 2c  ootPgno, &pMove,
15c75 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28   0);.        if(
15c76 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
15c77 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  {.          retu
15c78 72 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d  rn rc;.        }
15c79 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72 65  .        rc = re
15c7a 6c 6f 63 61 74 65 50 61 67 65 28 70 42 74 2c 20  locatePage(pBt, 
15c7b 70 4d 6f 76 65 2c 20 50 54 52 4d 41 50 5f 52 4f  pMove, PTRMAP_RO
15c7c 4f 54 50 41 47 45 2c 20 30 2c 20 69 54 61 62 6c  OTPAGE, 0, iTabl
15c7d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 72  e, 0);.        r
15c7e 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76 65  eleasePage(pMove
15c7f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 72  );.        if( r
15c80 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
15c81 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
15c82 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   rc;.        }. 
15c83 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
15c84 74 65 33 42 74 72 65 65 47 65 74 50 61 67 65 28  te3BtreeGetPage(
15c85 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  pBt, maxRootPgno
15c86 2c 20 26 70 4d 6f 76 65 2c 20 30 29 3b 0a 20 20  , &pMove, 0);.  
15c87 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
15c88 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15c89 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a       return rc;.
15c8a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
15c8b 20 20 72 63 20 3d 20 66 72 65 65 50 61 67 65 28    rc = freePage(
15c8c 70 4d 6f 76 65 29 3b 0a 20 20 20 20 20 20 20 20  pMove);.        
15c8d 72 65 6c 65 61 73 65 50 61 67 65 28 70 4d 6f 76  releasePage(pMov
15c8e 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  e);.        if( 
15c8f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
15c90 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
15c91 6e 20 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a  n rc;.        }.
15c92 20 20 20 20 20 20 20 20 2a 70 69 4d 6f 76 65 64          *piMoved
15c93 20 3d 20 6d 61 78 52 6f 6f 74 50 67 6e 6f 3b 0a   = maxRootPgno;.
15c94 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
15c95 2a 20 53 65 74 20 74 68 65 20 6e 65 77 20 27 6d  * Set the new 'm
15c96 61 78 2d 72 6f 6f 74 2d 70 61 67 65 27 20 76 61  ax-root-page' va
15c97 6c 75 65 20 69 6e 20 74 68 65 20 64 61 74 61 62  lue in the datab
15c98 61 73 65 20 68 65 61 64 65 72 2e 20 54 68 69 73  ase header. This
15c99 0a 20 20 20 20 20 20 2a 2a 20 69 73 20 74 68 65  .      ** is the
15c9a 20 6f 6c 64 20 76 61 6c 75 65 20 6c 65 73 73 20   old value less 
15c9b 6f 6e 65 2c 20 6c 65 73 73 20 6f 6e 65 20 6d 6f  one, less one mo
15c9c 72 65 20 69 66 20 74 68 61 74 20 68 61 70 70 65  re if that happe
15c9d 6e 73 20 74 6f 0a 20 20 20 20 20 20 2a 2a 20 62  ns to.      ** b
15c9e 65 20 61 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75  e a root-page nu
15c9f 6d 62 65 72 2c 20 6c 65 73 73 20 6f 6e 65 20 61  mber, less one a
15ca0 67 61 69 6e 20 69 66 20 74 68 61 74 20 69 73 20  gain if that is 
15ca1 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 50 45 4e  the.      ** PEN
15ca2 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 2e 0a  DING_BYTE_PAGE..
15ca3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 6d        */.      m
15ca4 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20 20  axRootPgno--;.  
15ca5 20 20 20 20 69 66 28 20 6d 61 78 52 6f 6f 74 50      if( maxRootP
15ca6 67 6e 6f 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54  gno==PENDING_BYT
15ca7 45 5f 50 41 47 45 28 70 42 74 29 20 29 7b 0a 20  E_PAGE(pBt) ){. 
15ca8 20 20 20 20 20 20 20 6d 61 78 52 6f 6f 74 50 67         maxRootPg
15ca9 6e 6f 2d 2d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  no--;.      }.  
15caa 20 20 20 20 69 66 28 20 6d 61 78 52 6f 6f 74 50      if( maxRootP
15cab 67 6e 6f 3d 3d 50 54 52 4d 41 50 5f 50 41 47 45  gno==PTRMAP_PAGE
15cac 4e 4f 28 70 42 74 2c 20 6d 61 78 52 6f 6f 74 50  NO(pBt, maxRootP
15cad 67 6e 6f 29 20 29 7b 0a 20 20 20 20 20 20 20 20  gno) ){.        
15cae 6d 61 78 52 6f 6f 74 50 67 6e 6f 2d 2d 3b 0a 20  maxRootPgno--;. 
15caf 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
15cb0 65 72 74 28 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  ert( maxRootPgno
15cb1 21 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f 50  !=PENDING_BYTE_P
15cb2 41 47 45 28 70 42 74 29 20 29 3b 0a 0a 20 20 20  AGE(pBt) );..   
15cb3 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
15cb4 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70  treeUpdateMeta(p
15cb5 2c 20 34 2c 20 6d 61 78 52 6f 6f 74 50 67 6e 6f  , 4, maxRootPgno
15cb6 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
15cb7 20 20 20 20 72 63 20 3d 20 66 72 65 65 50 61 67      rc = freePag
15cb8 65 28 70 50 61 67 65 29 3b 0a 20 20 20 20 20 20  e(pPage);.      
15cb9 72 65 6c 65 61 73 65 50 61 67 65 28 70 50 61 67  releasePage(pPag
15cba 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  e);.    }.#endif
15cbb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a  .  }else{.    /*
15cbc 20 49 66 20 73 71 6c 69 74 65 33 42 74 72 65 65   If sqlite3Btree
15cbd 44 72 6f 70 54 61 62 6c 65 20 77 61 73 20 63 61  DropTable was ca
15cbe 6c 6c 65 64 20 6f 6e 20 70 61 67 65 20 31 2e 20  lled on page 1. 
15cbf 2a 2f 0a 20 20 20 20 7a 65 72 6f 50 61 67 65 28  */.    zeroPage(
15cc0 70 50 61 67 65 2c 20 50 54 46 5f 49 4e 54 4b 45  pPage, PTF_INTKE
15cc1 59 7c 50 54 46 5f 4c 45 41 46 20 29 3b 0a 20 20  Y|PTF_LEAF );.  
15cc2 20 20 72 65 6c 65 61 73 65 50 61 67 65 28 70 50    releasePage(pP
15cc3 61 67 65 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75  age);.  }.  retu
15cc4 72 6e 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54  rn rc;  .}.SQLIT
15cc5 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15cc6 6c 69 74 65 33 42 74 72 65 65 44 72 6f 70 54 61  lite3BtreeDropTa
15cc7 62 6c 65 28 42 74 72 65 65 20 2a 70 2c 20 69 6e  ble(Btree *p, in
15cc8 74 20 69 54 61 62 6c 65 2c 20 69 6e 74 20 2a 70  t iTable, int *p
15cc9 69 4d 6f 76 65 64 29 7b 0a 20 20 69 6e 74 20 72  iMoved){.  int r
15cca 63 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  c;.  sqlite3Btre
15ccb 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70 2d 3e  eEnter(p);.  p->
15ccc 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
15ccd 0a 20 20 72 63 20 3d 20 62 74 72 65 65 44 72 6f  .  rc = btreeDro
15cce 70 54 61 62 6c 65 28 70 2c 20 69 54 61 62 6c 65  pTable(p, iTable
15ccf 2c 20 70 69 4d 6f 76 65 64 29 3b 0a 20 20 73 71  , piMoved);.  sq
15cd0 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
15cd1 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
15cd2 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65 61 64 20  .}.../*.** Read 
15cd3 74 68 65 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61  the meta-informa
15cd4 74 69 6f 6e 20 6f 75 74 20 6f 66 20 61 20 64 61  tion out of a da
15cd5 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 4d 65  tabase file.  Me
15cd6 74 61 5b 30 5d 0a 2a 2a 20 69 73 20 74 68 65 20  ta[0].** is the 
15cd7 6e 75 6d 62 65 72 20 6f 66 20 66 72 65 65 20 70  number of free p
15cd8 61 67 65 73 20 63 75 72 72 65 6e 74 6c 79 20 69  ages currently i
15cd9 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  n the database. 
15cda 20 4d 65 74 61 5b 31 5d 0a 2a 2a 20 74 68 72 6f   Meta[1].** thro
15cdb 75 67 68 20 6d 65 74 61 5b 31 35 5d 20 61 72 65  ugh meta[15] are
15cdc 20 61 76 61 69 6c 61 62 6c 65 20 66 6f 72 20 75   available for u
15cdd 73 65 20 62 79 20 68 69 67 68 65 72 20 6c 61 79  se by higher lay
15cde 65 72 73 2e 20 20 4d 65 74 61 5b 30 5d 0a 2a 2a  ers.  Meta[0].**
15cdf 20 69 73 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 74   is read-only, t
15ce0 68 65 20 6f 74 68 65 72 73 20 61 72 65 20 72 65  he others are re
15ce1 61 64 2f 77 72 69 74 65 2e 0a 2a 2a 20 0a 2a 2a  ad/write..** .**
15ce2 20 54 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65   The schema laye
15ce3 72 20 6e 75 6d 62 65 72 73 20 6d 65 74 61 20 76  r numbers meta v
15ce4 61 6c 75 65 73 20 64 69 66 66 65 72 65 6e 74 6c  alues differentl
15ce5 79 2e 20 20 41 74 20 74 68 65 20 73 63 68 65 6d  y.  At the schem
15ce6 61 0a 2a 2a 20 6c 61 79 65 72 20 28 61 6e 64 20  a.** layer (and 
15ce7 74 68 65 20 53 65 74 43 6f 6f 6b 69 65 20 61 6e  the SetCookie an
15ce8 64 20 52 65 61 64 43 6f 6f 6b 69 65 20 6f 70 63  d ReadCookie opc
15ce9 6f 64 65 73 29 20 74 68 65 20 6e 75 6d 62 65 72  odes) the number
15cea 20 6f 66 0a 2a 2a 20 66 72 65 65 20 70 61 67 65   of.** free page
15ceb 73 20 69 73 20 6e 6f 74 20 76 69 73 69 62 6c 65  s is not visible
15cec 2e 20 20 53 6f 20 43 6f 6f 6b 69 65 5b 30 5d 20  .  So Cookie[0] 
15ced 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20 4d  is the same as M
15cee 65 74 61 5b 31 5d 2e 0a 2a 2f 0a 53 51 4c 49 54  eta[1]..*/.SQLIT
15cef 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
15cf0 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74  lite3BtreeGetMet
15cf1 61 28 42 74 72 65 65 20 2a 70 2c 20 69 6e 74 20  a(Btree *p, int 
15cf2 69 64 78 2c 20 75 33 32 20 2a 70 4d 65 74 61 29  idx, u32 *pMeta)
15cf3 7b 0a 20 20 44 62 50 61 67 65 20 2a 70 44 62 50  {.  DbPage *pDbP
15cf4 61 67 65 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  age;.  int rc;. 
15cf5 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
15cf6 70 50 31 3b 0a 20 20 42 74 53 68 61 72 65 64 20  pP1;.  BtShared 
15cf7 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 0a  *pBt = p->pBt;..
15cf8 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
15cf9 74 65 72 28 70 29 3b 0a 20 20 70 42 74 2d 3e 64  ter(p);.  pBt->d
15cfa 62 20 3d 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a  b = p->db;..  /*
15cfb 20 52 65 61 64 69 6e 67 20 61 20 6d 65 74 61 2d   Reading a meta-
15cfc 64 61 74 61 20 76 61 6c 75 65 20 72 65 71 75 69  data value requi
15cfd 72 65 73 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20  res a read-lock 
15cfe 6f 6e 20 70 61 67 65 20 31 20 28 61 6e 64 20 68  on page 1 (and h
15cff 65 6e 63 65 0a 20 20 2a 2a 20 74 68 65 20 73 71  ence.  ** the sq
15d00 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c  lite_master tabl
15d01 65 2e 20 57 65 20 67 72 61 62 20 74 68 69 73 20  e. We grab this 
15d02 6c 6f 63 6b 20 72 65 67 61 72 64 6c 65 73 73 20  lock regardless 
15d03 6f 66 20 77 68 65 74 68 65 72 20 6f 72 0a 20 20  of whether or.  
15d04 2a 2a 20 6e 6f 74 20 74 68 65 20 53 51 4c 49 54  ** not the SQLIT
15d05 45 5f 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  E_ReadUncommitte
15d06 64 20 66 6c 61 67 20 69 73 20 73 65 74 20 28 74  d flag is set (t
15d07 68 65 20 74 61 62 6c 65 20 72 6f 6f 74 65 64 20  he table rooted 
15d08 61 74 20 70 61 67 65 0a 20 20 2a 2a 20 31 20 69  at page.  ** 1 i
15d09 73 20 74 72 65 61 74 65 64 20 61 73 20 61 20 73  s treated as a s
15d0a 70 65 63 69 61 6c 20 63 61 73 65 20 62 79 20 71  pecial case by q
15d0b 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 29 20  ueryTableLock() 
15d0c 61 6e 64 20 6c 6f 63 6b 54 61 62 6c 65 28 29 29  and lockTable())
15d0d 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 71 75  ..  */.  rc = qu
15d0e 65 72 79 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20  eryTableLock(p, 
15d0f 31 2c 20 52 45 41 44 5f 4c 4f 43 4b 29 3b 0a 20  1, READ_LOCK);. 
15d10 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
15d11 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  OK ){.    sqlite
15d12 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
15d13 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
15d14 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 69 64   }..  assert( id
15d15 78 3e 3d 30 20 26 26 20 69 64 78 3c 3d 31 35 20  x>=0 && idx<=15 
15d16 29 3b 0a 20 20 69 66 28 20 70 42 74 2d 3e 70 50  );.  if( pBt->pP
15d17 61 67 65 31 20 29 7b 0a 20 20 20 20 2f 2a 20 54  age1 ){.    /* T
15d18 68 65 20 62 2d 74 72 65 65 20 69 73 20 61 6c 72  he b-tree is alr
15d19 65 61 64 79 20 68 6f 6c 64 69 6e 67 20 61 20 72  eady holding a r
15d1a 65 66 65 72 65 6e 63 65 20 74 6f 20 70 61 67 65  eference to page
15d1b 20 31 20 6f 66 20 74 68 65 20 64 61 74 61 62 61   1 of the databa
15d1c 73 65 0a 20 20 20 20 2a 2a 20 66 69 6c 65 2e 20  se.    ** file. 
15d1d 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
15d1e 20 72 65 71 75 69 72 65 64 20 6d 65 74 61 2d 64   required meta-d
15d1f 61 74 61 20 76 61 6c 75 65 20 63 61 6e 20 62 65  ata value can be
15d20 20 72 65 61 64 20 64 69 72 65 63 74 6c 79 0a 20   read directly. 
15d21 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20 70     ** from the p
15d22 61 67 65 20 64 61 74 61 20 6f 66 20 74 68 69 73  age data of this
15d23 20 72 65 66 65 72 65 6e 63 65 2e 20 54 68 69 73   reference. This
15d24 20 69 73 20 73 6c 69 67 68 74 6c 79 20 66 61 73   is slightly fas
15d25 74 65 72 20 74 68 61 6e 0a 20 20 20 20 2a 2a 20  ter than.    ** 
15d26 72 65 71 75 65 73 74 69 6e 67 20 61 20 6e 65 77  requesting a new
15d27 20 72 65 66 65 72 65 6e 63 65 20 66 72 6f 6d 20   reference from 
15d28 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 2e  the pager layer.
15d29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 50 31 20  .    */.    pP1 
15d2a 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
15d2b 20 2a 29 70 42 74 2d 3e 70 50 61 67 65 31 2d 3e   *)pBt->pPage1->
15d2c 61 44 61 74 61 3b 0a 20 20 7d 65 6c 73 65 7b 0a  aData;.  }else{.
15d2d 20 20 20 20 2f 2a 20 54 68 65 20 62 2d 74 72 65      /* The b-tre
15d2e 65 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20  e does not have 
15d2f 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 70  a reference to p
15d30 61 67 65 20 31 20 6f 66 20 74 68 65 20 64 61 74  age 1 of the dat
15d31 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20  abase file..    
15d32 2a 2a 20 4f 62 74 61 69 6e 20 6f 6e 65 20 66 72  ** Obtain one fr
15d33 6f 6d 20 74 68 65 20 70 61 67 65 72 20 6c 61 79  om the pager lay
15d34 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72  er..    */.    r
15d35 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  c = sqlite3Pager
15d36 47 65 74 28 70 42 74 2d 3e 70 50 61 67 65 72 2c  Get(pBt->pPager,
15d37 20 31 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20   1, &pDbPage);. 
15d38 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20     if( rc ){.   
15d39 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
15d3a 65 61 76 65 28 70 29 3b 0a 20 20 20 20 20 20 72  eave(p);.      r
15d3b 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a  eturn rc;.    }.
15d3c 20 20 20 20 70 50 31 20 3d 20 28 75 6e 73 69 67      pP1 = (unsig
15d3d 6e 65 64 20 63 68 61 72 20 2a 29 73 71 6c 69 74  ned char *)sqlit
15d3e 65 33 50 61 67 65 72 47 65 74 44 61 74 61 28 70  e3PagerGetData(p
15d3f 44 62 50 61 67 65 29 3b 0a 20 20 7d 0a 20 20 2a  DbPage);.  }.  *
15d40 70 4d 65 74 61 20 3d 20 67 65 74 34 62 79 74 65  pMeta = get4byte
15d41 28 26 70 50 31 5b 33 36 20 2b 20 69 64 78 2a 34  (&pP1[36 + idx*4
15d42 5d 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65  ]);..  /* If the
15d43 20 62 2d 74 72 65 65 20 69 73 20 6e 6f 74 20 68   b-tree is not h
15d44 6f 6c 64 69 6e 67 20 61 20 72 65 66 65 72 65 6e  olding a referen
15d45 63 65 20 74 6f 20 70 61 67 65 20 31 2c 20 74 68  ce to page 1, th
15d46 65 6e 20 6f 6e 65 20 77 61 73 20 0a 20 20 2a 2a  en one was .  **
15d47 20 72 65 71 75 65 73 74 65 64 20 66 72 6f 6d 20   requested from 
15d48 74 68 65 20 70 61 67 65 72 20 6c 61 79 65 72 20  the pager layer 
15d49 69 6e 20 74 68 65 20 61 62 6f 76 65 20 62 6c 6f  in the above blo
15d4a 63 6b 2e 20 52 65 6c 65 61 73 65 20 69 74 20 6e  ck. Release it n
15d4b 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
15d4c 70 42 74 2d 3e 70 50 61 67 65 31 20 29 7b 0a 20  pBt->pPage1 ){. 
15d4d 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
15d4e 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20  nref(pDbPage);. 
15d4f 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 75 74 6f   }..  /* If auto
15d50 76 61 63 75 75 6d 65 64 20 69 73 20 64 69 73 61  vacuumed is disa
15d51 62 6c 65 64 20 69 6e 20 74 68 69 73 20 62 75 69  bled in this bui
15d52 6c 64 20 62 75 74 20 77 65 20 61 72 65 20 74 72  ld but we are tr
15d53 79 69 6e 67 20 74 6f 20 0a 20 20 2a 2a 20 61 63  ying to .  ** ac
15d54 63 65 73 73 20 61 6e 20 61 75 74 6f 76 61 63 75  cess an autovacu
15d55 75 6d 65 64 20 64 61 74 61 62 61 73 65 2c 20 74  umed database, t
15d56 68 65 6e 20 6d 61 6b 65 20 74 68 65 20 64 61 74  hen make the dat
15d57 61 62 61 73 65 20 72 65 61 64 6f 6e 6c 79 2e 20  abase readonly. 
15d58 0a 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  .  */.#ifdef SQL
15d59 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
15d5a 55 55 4d 0a 20 20 69 66 28 20 69 64 78 3d 3d 34  UUM.  if( idx==4
15d5b 20 26 26 20 2a 70 4d 65 74 61 3e 30 20 29 20 70   && *pMeta>0 ) p
15d5c 42 74 2d 3e 72 65 61 64 4f 6e 6c 79 20 3d 20 31  Bt->readOnly = 1
15d5d 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 47  ;.#endif..  /* G
15d5e 72 61 62 20 74 68 65 20 72 65 61 64 2d 6c 6f 63  rab the read-loc
15d5f 6b 20 6f 6e 20 70 61 67 65 20 31 2e 20 2a 2f 0a  k on page 1. */.
15d60 20 20 72 63 20 3d 20 6c 6f 63 6b 54 61 62 6c 65    rc = lockTable
15d61 28 70 2c 20 31 2c 20 52 45 41 44 5f 4c 4f 43 4b  (p, 1, READ_LOCK
15d62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65  );.  sqlite3Btre
15d63 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72 65 74  eLeave(p);.  ret
15d64 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
15d65 20 57 72 69 74 65 20 6d 65 74 61 2d 69 6e 66 6f   Write meta-info
15d66 72 6d 61 74 69 6f 6e 20 62 61 63 6b 20 69 6e 74  rmation back int
15d67 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 20  o the database. 
15d68 20 4d 65 74 61 5b 30 5d 20 69 73 0a 2a 2a 20 72   Meta[0] is.** r
15d69 65 61 64 2d 6f 6e 6c 79 20 61 6e 64 20 6d 61 79  ead-only and may
15d6a 20 6e 6f 74 20 62 65 20 77 72 69 74 74 65 6e 2e   not be written.
15d6b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
15d6c 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74  TE int sqlite3Bt
15d6d 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 42 74  reeUpdateMeta(Bt
15d6e 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 64 78 2c  ree *p, int idx,
15d6f 20 75 33 32 20 69 4d 65 74 61 29 7b 0a 20 20 42   u32 iMeta){.  B
15d70 74 53 68 61 72 65 64 20 2a 70 42 74 20 3d 20 70  tShared *pBt = p
15d71 2d 3e 70 42 74 3b 0a 20 20 75 6e 73 69 67 6e 65  ->pBt;.  unsigne
15d72 64 20 63 68 61 72 20 2a 70 50 31 3b 0a 20 20 69  d char *pP1;.  i
15d73 6e 74 20 72 63 3b 0a 20 20 61 73 73 65 72 74 28  nt rc;.  assert(
15d74 20 69 64 78 3e 3d 31 20 26 26 20 69 64 78 3c 3d   idx>=1 && idx<=
15d75 31 35 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  15 );.  sqlite3B
15d76 74 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20  treeEnter(p);.  
15d77 70 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b  pBt->db = p->db;
15d78 0a 20 20 69 66 28 20 70 2d 3e 69 6e 54 72 61 6e  .  if( p->inTran
15d79 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s!=TRANS_WRITE )
15d7a 7b 0a 20 20 20 20 72 63 20 3d 20 70 42 74 2d 3e  {.    rc = pBt->
15d7b 72 65 61 64 4f 6e 6c 79 20 3f 20 53 51 4c 49 54  readOnly ? SQLIT
15d7c 45 5f 52 45 41 44 4f 4e 4c 59 20 3a 20 53 51 4c  E_READONLY : SQL
15d7d 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c  ITE_ERROR;.  }el
15d7e 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
15d7f 70 42 74 2d 3e 70 50 61 67 65 31 21 3d 30 20 29  pBt->pPage1!=0 )
15d80 3b 0a 20 20 20 20 70 50 31 20 3d 20 70 42 74 2d  ;.    pP1 = pBt-
15d81 3e 70 50 61 67 65 31 2d 3e 61 44 61 74 61 3b 0a  >pPage1->aData;.
15d82 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
15d83 50 61 67 65 72 57 72 69 74 65 28 70 42 74 2d 3e  PagerWrite(pBt->
15d84 70 50 61 67 65 31 2d 3e 70 44 62 50 61 67 65 29  pPage1->pDbPage)
15d85 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
15d86 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
15d87 20 70 75 74 34 62 79 74 65 28 26 70 50 31 5b 33   put4byte(&pP1[3
15d88 36 20 2b 20 69 64 78 2a 34 5d 2c 20 69 4d 65 74  6 + idx*4], iMet
15d89 61 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  a);.#ifndef SQLI
15d8a 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15d8b 55 4d 0a 20 20 20 20 20 20 69 66 28 20 69 64 78  UM.      if( idx
15d8c 3d 3d 37 20 29 7b 0a 20 20 20 20 20 20 20 20 61  ==7 ){.        a
15d8d 73 73 65 72 74 28 20 70 42 74 2d 3e 61 75 74 6f  ssert( pBt->auto
15d8e 56 61 63 75 75 6d 20 7c 7c 20 69 4d 65 74 61 3d  Vacuum || iMeta=
15d8f 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =0 );.        as
15d90 73 65 72 74 28 20 69 4d 65 74 61 3d 3d 30 20 7c  sert( iMeta==0 |
15d91 7c 20 69 4d 65 74 61 3d 3d 31 20 29 3b 0a 20 20  | iMeta==1 );.  
15d92 20 20 20 20 20 20 70 42 74 2d 3e 69 6e 63 72 56        pBt->incrV
15d93 61 63 75 75 6d 20 3d 20 69 4d 65 74 61 3b 0a 20  acuum = iMeta;. 
15d94 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
15d95 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65    }.  }.  sqlite
15d96 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
15d97 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
15d98 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
15d99 20 66 6c 61 67 20 62 79 74 65 20 61 74 20 74 68   flag byte at th
15d9a 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74  e beginning of t
15d9b 68 65 20 70 61 67 65 20 74 68 61 74 20 74 68 65  he page that the
15d9c 20 63 75 72 73 6f 72 0a 2a 2a 20 69 73 20 63 75   cursor.** is cu
15d9d 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67  rrently pointing
15d9e 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50   to..*/.SQLITE_P
15d9f 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
15da0 65 33 42 74 72 65 65 46 6c 61 67 73 28 42 74 43  e3BtreeFlags(BtC
15da1 75 72 73 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20  ursor *pCur){.  
15da2 2f 2a 20 54 4f 44 4f 3a 20 57 68 61 74 20 61 62  /* TODO: What ab
15da3 6f 75 74 20 43 55 52 53 4f 52 5f 52 45 51 55 49  out CURSOR_REQUI
15da4 52 45 53 45 45 4b 20 73 74 61 74 65 3f 20 50 72  RESEEK state? Pr
15da5 6f 62 61 62 6c 79 20 6e 65 65 64 20 74 6f 20 63  obably need to c
15da6 61 6c 6c 0a 20 20 2a 2a 20 72 65 73 74 6f 72 65  all.  ** restore
15da7 43 75 72 73 6f 72 50 6f 73 69 74 69 6f 6e 28 29  CursorPosition()
15da8 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 4d 65   here..  */.  Me
15da9 6d 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20  mPage *pPage;.  
15daa 72 65 73 74 6f 72 65 43 75 72 73 6f 72 50 6f 73  restoreCursorPos
15dab 69 74 69 6f 6e 28 70 43 75 72 29 3b 0a 20 20 70  ition(pCur);.  p
15dac 50 61 67 65 20 3d 20 70 43 75 72 2d 3e 61 70 50  Page = pCur->apP
15dad 61 67 65 5b 70 43 75 72 2d 3e 69 50 61 67 65 5d  age[pCur->iPage]
15dae 3b 0a 20 20 61 73 73 65 72 74 28 20 63 75 72 73  ;.  assert( curs
15daf 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70 43 75  orHoldsMutex(pCu
15db0 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  r) );.  assert( 
15db1 70 50 61 67 65 2d 3e 70 42 74 3d 3d 70 43 75 72  pPage->pBt==pCur
15db2 2d 3e 70 42 74 20 29 3b 0a 20 20 72 65 74 75 72  ->pBt );.  retur
15db3 6e 20 70 50 61 67 65 20 3f 20 70 50 61 67 65 2d  n pPage ? pPage-
15db4 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
15db5 72 4f 66 66 73 65 74 5d 20 3a 20 30 3b 0a 7d 0a  rOffset] : 0;.}.
15db6 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
15db7 68 65 20 70 61 67 65 72 20 61 73 73 6f 63 69 61  he pager associa
15db8 74 65 64 20 77 69 74 68 20 61 20 42 54 72 65 65  ted with a BTree
15db9 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
15dba 69 73 20 75 73 65 64 20 66 6f 72 0a 2a 2a 20 74  is used for.** t
15dbb 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75 67  esting and debug
15dbc 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a 53 51  ging only..*/.SQ
15dbd 4c 49 54 45 5f 50 52 49 56 41 54 45 20 50 61 67  LITE_PRIVATE Pag
15dbe 65 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65  er *sqlite3Btree
15dbf 50 61 67 65 72 28 42 74 72 65 65 20 2a 70 29 7b  Pager(Btree *p){
15dc0 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e 70 42 74  .  return p->pBt
15dc1 2d 3e 70 50 61 67 65 72 3b 0a 7d 0a 0a 23 69 66  ->pPager;.}..#if
15dc2 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
15dc3 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b  _INTEGRITY_CHECK
15dc4 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20  ./*.** Append a 
15dc5 6d 65 73 73 61 67 65 20 74 6f 20 74 68 65 20 65  message to the e
15dc6 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
15dc7 69 6e 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ing..*/.static v
15dc8 6f 69 64 20 63 68 65 63 6b 41 70 70 65 6e 64 4d  oid checkAppendM
15dc9 73 67 28 0a 20 20 49 6e 74 65 67 72 69 74 79 43  sg(.  IntegrityC
15dca 6b 20 2a 70 43 68 65 63 6b 2c 0a 20 20 63 68 61  k *pCheck,.  cha
15dcb 72 20 2a 7a 4d 73 67 31 2c 0a 20 20 63 6f 6e 73  r *zMsg1,.  cons
15dcc 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c  t char *zFormat,
15dcd 0a 20 20 2e 2e 2e 0a 29 7b 0a 20 20 76 61 5f 6c  .  ....){.  va_l
15dce 69 73 74 20 61 70 3b 0a 20 20 69 66 28 20 21 70  ist ap;.  if( !p
15dcf 43 68 65 63 6b 2d 3e 6d 78 45 72 72 20 29 20 72  Check->mxErr ) r
15dd0 65 74 75 72 6e 3b 0a 20 20 70 43 68 65 63 6b 2d  eturn;.  pCheck-
15dd1 3e 6d 78 45 72 72 2d 2d 3b 0a 20 20 70 43 68 65  >mxErr--;.  pChe
15dd2 63 6b 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 76 61  ck->nErr++;.  va
15dd3 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
15dd4 61 74 29 3b 0a 20 20 69 66 28 20 70 43 68 65 63  at);.  if( pChec
15dd5 6b 2d 3e 65 72 72 4d 73 67 2e 6e 43 68 61 72 20  k->errMsg.nChar 
15dd6 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 74  ){.    sqlite3St
15dd7 72 41 63 63 75 6d 41 70 70 65 6e 64 28 26 70 43  rAccumAppend(&pC
15dd8 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2c 20 22 5c  heck->errMsg, "\
15dd9 6e 22 2c 20 31 29 3b 0a 20 20 7d 0a 20 20 69 66  n", 1);.  }.  if
15dda 28 20 7a 4d 73 67 31 20 29 7b 0a 20 20 20 20 73  ( zMsg1 ){.    s
15ddb 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41 70  qlite3StrAccumAp
15ddc 70 65 6e 64 28 26 70 43 68 65 63 6b 2d 3e 65 72  pend(&pCheck->er
15ddd 72 4d 73 67 2c 20 7a 4d 73 67 31 2c 20 2d 31 29  rMsg, zMsg1, -1)
15dde 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
15ddf 58 50 72 69 6e 74 66 28 26 70 43 68 65 63 6b 2d  XPrintf(&pCheck-
15de0 3e 65 72 72 4d 73 67 2c 20 31 2c 20 7a 46 6f 72  >errMsg, 1, zFor
15de1 6d 61 74 2c 20 61 70 29 3b 0a 20 20 76 61 5f 65  mat, ap);.  va_e
15de2 6e 64 28 61 70 29 3b 0a 20 20 69 66 28 20 70 43  nd(ap);.  if( pC
15de3 68 65 63 6b 2d 3e 65 72 72 4d 73 67 2e 6d 61 6c  heck->errMsg.mal
15de4 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
15de5 20 70 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46   pCheck->mallocF
15de6 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 7d  ailed = 1;.  }.}
15de7 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
15de8 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59  E_OMIT_INTEGRITY
15de9 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64  _CHECK */..#ifnd
15dea 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
15deb 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 0a 2f  NTEGRITY_CHECK./
15dec 2a 0a 2a 2a 20 41 64 64 20 31 20 74 6f 20 74 68  *.** Add 1 to th
15ded 65 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e  e reference coun
15dee 74 20 66 6f 72 20 70 61 67 65 20 69 50 61 67 65  t for page iPage
15def 2e 20 20 49 66 20 74 68 69 73 20 69 73 20 74 68  .  If this is th
15df0 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 72 65 66 65  e second.** refe
15df1 72 65 6e 63 65 20 74 6f 20 74 68 65 20 70 61 67  rence to the pag
15df2 65 2c 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20  e, add an error 
15df3 6d 65 73 73 61 67 65 20 74 6f 20 70 43 68 65 63  message to pChec
15df4 6b 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a 2a 20 52  k->zErrMsg..** R
15df5 65 74 75 72 6e 20 31 20 69 66 20 74 68 65 72 65  eturn 1 if there
15df6 20 61 72 65 20 32 20 6f 72 65 20 6d 6f 72 65 20   are 2 ore more 
15df7 72 65 66 65 72 65 6e 63 65 73 20 74 6f 20 74 68  references to th
15df8 65 20 70 61 67 65 20 61 6e 64 20 30 20 69 66 0a  e page and 0 if.
15df9 2a 2a 20 69 66 20 74 68 69 73 20 69 73 20 74 68  ** if this is th
15dfa 65 20 66 69 72 73 74 20 72 65 66 65 72 65 6e 63  e first referenc
15dfb 65 20 74 6f 20 74 68 65 20 70 61 67 65 2e 0a 2a  e to the page..*
15dfc 2a 0a 2a 2a 20 41 6c 73 6f 20 63 68 65 63 6b 20  *.** Also check 
15dfd 74 68 61 74 20 74 68 65 20 70 61 67 65 20 6e 75  that the page nu
15dfe 6d 62 65 72 20 69 73 20 69 6e 20 62 6f 75 6e 64  mber is in bound
15dff 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  s..*/.static int
15e00 20 63 68 65 63 6b 52 65 66 28 49 6e 74 65 67 72   checkRef(Integr
15e01 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20 50  ityCk *pCheck, P
15e02 67 6e 6f 20 69 50 61 67 65 2c 20 63 68 61 72 20  gno iPage, char 
15e03 2a 7a 43 6f 6e 74 65 78 74 29 7b 0a 20 20 69 66  *zContext){.  if
15e04 28 20 69 50 61 67 65 3d 3d 30 20 29 20 72 65 74  ( iPage==0 ) ret
15e05 75 72 6e 20 31 3b 0a 20 20 69 66 28 20 69 50 61  urn 1;.  if( iPa
15e06 67 65 3e 70 43 68 65 63 6b 2d 3e 6e 50 61 67 65  ge>pCheck->nPage
15e07 20 29 7b 0a 20 20 20 20 63 68 65 63 6b 41 70 70   ){.    checkApp
15e08 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 7a  endMsg(pCheck, z
15e09 43 6f 6e 74 65 78 74 2c 20 22 69 6e 76 61 6c 69  Context, "invali
15e0a 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 25 64  d page number %d
15e0b 22 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 72  ", iPage);.    r
15e0c 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69  eturn 1;.  }.  i
15e0d 66 28 20 70 43 68 65 63 6b 2d 3e 61 6e 52 65 66  f( pCheck->anRef
15e0e 5b 69 50 61 67 65 5d 3d 3d 31 20 29 7b 0a 20 20  [iPage]==1 ){.  
15e0f 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
15e10 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
15e11 74 2c 20 22 32 6e 64 20 72 65 66 65 72 65 6e 63  t, "2nd referenc
15e12 65 20 74 6f 20 70 61 67 65 20 25 64 22 2c 20 69  e to page %d", i
15e13 50 61 67 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Page);.    retur
15e14 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
15e15 6e 20 20 28 70 43 68 65 63 6b 2d 3e 61 6e 52 65  n  (pCheck->anRe
15e16 66 5b 69 50 61 67 65 5d 2b 2b 29 3e 31 3b 0a 7d  f[iPage]++)>1;.}
15e17 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
15e18 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
15e19 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 61  ./*.** Check tha
15e1a 74 20 74 68 65 20 65 6e 74 72 79 20 69 6e 20 74  t the entry in t
15e1b 68 65 20 70 6f 69 6e 74 65 72 2d 6d 61 70 20 66  he pointer-map f
15e1c 6f 72 20 70 61 67 65 20 69 43 68 69 6c 64 20 6d  or page iChild m
15e1d 61 70 73 20 74 6f 20 0a 2a 2a 20 70 61 67 65 20  aps to .** page 
15e1e 69 50 61 72 65 6e 74 2c 20 70 6f 69 6e 74 65 72  iParent, pointer
15e1f 20 74 79 70 65 20 70 74 72 54 79 70 65 2e 20 49   type ptrType. I
15e20 66 20 6e 6f 74 2c 20 61 70 70 65 6e 64 20 61 6e  f not, append an
15e21 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 2a   error message.*
15e22 2a 20 74 6f 20 70 43 68 65 63 6b 2e 0a 2a 2f 0a  * to pCheck..*/.
15e23 73 74 61 74 69 63 20 76 6f 69 64 20 63 68 65 63  static void chec
15e24 6b 50 74 72 6d 61 70 28 0a 20 20 49 6e 74 65 67  kPtrmap(.  Integ
15e25 72 69 74 79 43 6b 20 2a 70 43 68 65 63 6b 2c 20  rityCk *pCheck, 
15e26 20 20 2f 2a 20 49 6e 74 65 67 72 69 74 79 20 63    /* Integrity c
15e27 68 65 63 6b 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  heck context */.
15e28 20 20 50 67 6e 6f 20 69 43 68 69 6c 64 2c 20 20    Pgno iChild,  
15e29 20 20 20 20 20 20 20 20 20 2f 2a 20 43 68 69 6c           /* Chil
15e2a 64 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  d page number */
15e2b 0a 20 20 75 38 20 65 54 79 70 65 2c 20 20 20 20  .  u8 eType,    
15e2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
15e2d 65 63 74 65 64 20 70 6f 69 6e 74 65 72 20 6d 61  ected pointer ma
15e2e 70 20 74 79 70 65 20 2a 2f 0a 20 20 50 67 6e 6f  p type */.  Pgno
15e2f 20 69 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20   iParent,       
15e30 20 20 20 2f 2a 20 45 78 70 65 63 74 65 64 20 70     /* Expected p
15e31 6f 69 6e 74 65 72 20 6d 61 70 20 70 61 72 65 6e  ointer map paren
15e32 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20 2a 2f  t page number */
15e33 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78  .  char *zContex
15e34 74 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e  t         /* Con
15e35 74 65 78 74 20 64 65 73 63 72 69 70 74 69 6f 6e  text description
15e36 20 28 75 73 65 64 20 66 6f 72 20 65 72 72 6f 72   (used for error
15e37 20 6d 73 67 29 20 2a 2f 0a 29 7b 0a 20 20 69 6e   msg) */.){.  in
15e38 74 20 72 63 3b 0a 20 20 75 38 20 65 50 74 72 6d  t rc;.  u8 ePtrm
15e39 61 70 54 79 70 65 3b 0a 20 20 50 67 6e 6f 20 69  apType;.  Pgno i
15e3a 50 74 72 6d 61 70 50 61 72 65 6e 74 3b 0a 0a 20  PtrmapParent;.. 
15e3b 20 72 63 20 3d 20 70 74 72 6d 61 70 47 65 74 28   rc = ptrmapGet(
15e3c 70 43 68 65 63 6b 2d 3e 70 42 74 2c 20 69 43 68  pCheck->pBt, iCh
15e3d 69 6c 64 2c 20 26 65 50 74 72 6d 61 70 54 79 70  ild, &ePtrmapTyp
15e3e 65 2c 20 26 69 50 74 72 6d 61 70 50 61 72 65 6e  e, &iPtrmapParen
15e3f 74 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  t);.  if( rc!=SQ
15e40 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 63  LITE_OK ){.    c
15e41 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43  heckAppendMsg(pC
15e42 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20  heck, zContext, 
15e43 22 46 61 69 6c 65 64 20 74 6f 20 72 65 61 64 20  "Failed to read 
15e44 70 74 72 6d 61 70 20 6b 65 79 3d 25 64 22 2c 20  ptrmap key=%d", 
15e45 69 43 68 69 6c 64 29 3b 0a 20 20 20 20 72 65 74  iChild);.    ret
15e46 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  urn;.  }..  if( 
15e47 65 50 74 72 6d 61 70 54 79 70 65 21 3d 65 54 79  ePtrmapType!=eTy
15e48 70 65 20 7c 7c 20 69 50 74 72 6d 61 70 50 61 72  pe || iPtrmapPar
15e49 65 6e 74 21 3d 69 50 61 72 65 6e 74 20 29 7b 0a  ent!=iParent ){.
15e4a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d      checkAppendM
15e4b 73 67 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74  sg(pCheck, zCont
15e4c 65 78 74 2c 20 0a 20 20 20 20 20 20 22 42 61 64  ext, .      "Bad
15e4d 20 70 74 72 20 6d 61 70 20 65 6e 74 72 79 20 6b   ptr map entry k
15e4e 65 79 3d 25 64 20 65 78 70 65 63 74 65 64 3d 28  ey=%d expected=(
15e4f 25 64 2c 25 64 29 20 67 6f 74 3d 28 25 64 2c 25  %d,%d) got=(%d,%
15e50 64 29 22 2c 20 0a 20 20 20 20 20 20 69 43 68 69  d)", .      iChi
15e51 6c 64 2c 20 65 54 79 70 65 2c 20 69 50 61 72 65  ld, eType, iPare
15e52 6e 74 2c 20 65 50 74 72 6d 61 70 54 79 70 65 2c  nt, ePtrmapType,
15e53 20 69 50 74 72 6d 61 70 50 61 72 65 6e 74 29 3b   iPtrmapParent);
15e54 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  .  }.}.#endif../
15e55 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 69  *.** Check the i
15e56 6e 74 65 67 72 69 74 79 20 6f 66 20 74 68 65 20  ntegrity of the 
15e57 66 72 65 65 6c 69 73 74 20 6f 72 20 6f 66 20 61  freelist or of a
15e58 6e 20 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 20  n overflow page 
15e59 6c 69 73 74 2e 0a 2a 2a 20 56 65 72 69 66 79 20  list..** Verify 
15e5a 74 68 61 74 20 74 68 65 20 6e 75 6d 62 65 72 20  that the number 
15e5b 6f 66 20 70 61 67 65 73 20 6f 6e 20 74 68 65 20  of pages on the 
15e5c 6c 69 73 74 20 69 73 20 4e 2e 0a 2a 2f 0a 73 74  list is N..*/.st
15e5d 61 74 69 63 20 76 6f 69 64 20 63 68 65 63 6b 4c  atic void checkL
15e5e 69 73 74 28 0a 20 20 49 6e 74 65 67 72 69 74 79  ist(.  Integrity
15e5f 43 6b 20 2a 70 43 68 65 63 6b 2c 20 20 2f 2a 20  Ck *pCheck,  /* 
15e60 49 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 69  Integrity checki
15e61 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
15e62 69 6e 74 20 69 73 46 72 65 65 4c 69 73 74 2c 20  int isFreeList, 
15e63 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
15e64 72 20 61 20 66 72 65 65 6c 69 73 74 2e 20 20 46  r a freelist.  F
15e65 61 6c 73 65 20 66 6f 72 20 6f 76 65 72 66 6c 6f  alse for overflo
15e66 77 20 70 61 67 65 20 6c 69 73 74 20 2a 2f 0a 20  w page list */. 
15e67 20 69 6e 74 20 69 50 61 67 65 2c 20 20 20 20 20   int iPage,     
15e68 20 20 20 20 20 20 20 2f 2a 20 50 61 67 65 20 6e         /* Page n
15e69 75 6d 62 65 72 20 66 6f 72 20 66 69 72 73 74 20  umber for first 
15e6a 70 61 67 65 20 69 6e 20 74 68 65 20 6c 69 73 74  page in the list
15e6b 20 2a 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20   */.  int N,    
15e6c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45              /* E
15e6d 78 70 65 63 74 65 64 20 6e 75 6d 62 65 72 20 6f  xpected number o
15e6e 66 20 70 61 67 65 73 20 69 6e 20 74 68 65 20 6c  f pages in the l
15e6f 69 73 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ist */.  char *z
15e70 43 6f 6e 74 65 78 74 20 20 20 20 20 20 20 20 2f  Context        /
15e71 2a 20 43 6f 6e 74 65 78 74 20 66 6f 72 20 65 72  * Context for er
15e72 72 6f 72 20 6d 65 73 73 61 67 65 73 20 2a 2f 0a  ror messages */.
15e73 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e  ){.  int i;.  in
15e74 74 20 65 78 70 65 63 74 65 64 20 3d 20 4e 3b 0a  t expected = N;.
15e75 20 20 69 6e 74 20 69 46 69 72 73 74 20 3d 20 69    int iFirst = i
15e76 50 61 67 65 3b 0a 20 20 77 68 69 6c 65 28 20 4e  Page;.  while( N
15e77 2d 2d 20 3e 20 30 20 26 26 20 70 43 68 65 63 6b  -- > 0 && pCheck
15e78 2d 3e 6d 78 45 72 72 20 29 7b 0a 20 20 20 20 44  ->mxErr ){.    D
15e79 62 50 61 67 65 20 2a 70 4f 76 66 6c 50 61 67 65  bPage *pOvflPage
15e7a 3b 0a 20 20 20 20 75 6e 73 69 67 6e 65 64 20 63  ;.    unsigned c
15e7b 68 61 72 20 2a 70 4f 76 66 6c 44 61 74 61 3b 0a  har *pOvflData;.
15e7c 20 20 20 20 69 66 28 20 69 50 61 67 65 3c 31 20      if( iPage<1 
15e7d 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41 70  ){.      checkAp
15e7e 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
15e7f 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20 20 20 20  zContext,.      
15e80 20 20 20 22 25 64 20 6f 66 20 25 64 20 70 61 67     "%d of %d pag
15e81 65 73 20 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20  es missing from 
15e82 6f 76 65 72 66 6c 6f 77 20 6c 69 73 74 20 73 74  overflow list st
15e83 61 72 74 69 6e 67 20 61 74 20 25 64 22 2c 0a 20  arting at %d",. 
15e84 20 20 20 20 20 20 20 20 20 4e 2b 31 2c 20 65 78           N+1, ex
15e85 70 65 63 74 65 64 2c 20 69 46 69 72 73 74 29 3b  pected, iFirst);
15e86 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
15e87 20 20 7d 0a 20 20 20 20 69 66 28 20 63 68 65 63    }.    if( chec
15e88 6b 52 65 66 28 70 43 68 65 63 6b 2c 20 69 50 61  kRef(pCheck, iPa
15e89 67 65 2c 20 7a 43 6f 6e 74 65 78 74 29 20 29 20  ge, zContext) ) 
15e8a 62 72 65 61 6b 3b 0a 20 20 20 20 69 66 28 20 73  break;.    if( s
15e8b 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28 70  qlite3PagerGet(p
15e8c 43 68 65 63 6b 2d 3e 70 50 61 67 65 72 2c 20 28  Check->pPager, (
15e8d 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 4f 76  Pgno)iPage, &pOv
15e8e 66 6c 50 61 67 65 29 20 29 7b 0a 20 20 20 20 20  flPage) ){.     
15e8f 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28   checkAppendMsg(
15e90 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78 74  pCheck, zContext
15e91 2c 20 22 66 61 69 6c 65 64 20 74 6f 20 67 65 74  , "failed to get
15e92 20 70 61 67 65 20 25 64 22 2c 20 69 50 61 67 65   page %d", iPage
15e93 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  );.      break;.
15e94 20 20 20 20 7d 0a 20 20 20 20 70 4f 76 66 6c 44      }.    pOvflD
15e95 61 74 61 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  ata = (unsigned 
15e96 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 50 61  char *)sqlite3Pa
15e97 67 65 72 47 65 74 44 61 74 61 28 70 4f 76 66 6c  gerGetData(pOvfl
15e98 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20 69  Page);.    if( i
15e99 73 46 72 65 65 4c 69 73 74 20 29 7b 0a 20 20 20  sFreeList ){.   
15e9a 20 20 20 69 6e 74 20 6e 20 3d 20 67 65 74 34 62     int n = get4b
15e9b 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 34  yte(&pOvflData[4
15e9c 5d 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ]);.#ifndef SQLI
15e9d 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55  TE_OMIT_AUTOVACU
15e9e 55 4d 0a 20 20 20 20 20 20 69 66 28 20 70 43 68  UM.      if( pCh
15e9f 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74 6f 56 61  eck->pBt->autoVa
15ea0 63 75 75 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  cuum ){.        
15ea1 63 68 65 63 6b 50 74 72 6d 61 70 28 70 43 68 65  checkPtrmap(pChe
15ea2 63 6b 2c 20 69 50 61 67 65 2c 20 50 54 52 4d 41  ck, iPage, PTRMA
15ea3 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a  P_FREEPAGE, 0, z
15ea4 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
15ea5 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69  }.#endif.      i
15ea6 66 28 20 6e 3e 70 43 68 65 63 6b 2d 3e 70 42 74  f( n>pCheck->pBt
15ea7 2d 3e 75 73 61 62 6c 65 53 69 7a 65 2f 34 2d 32  ->usableSize/4-2
15ea8 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
15ea9 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65 63  kAppendMsg(pChec
15eaa 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20 20 20  k, zContext,.   
15eab 20 20 20 20 20 20 20 20 22 66 72 65 65 6c 69 73          "freelis
15eac 74 20 6c 65 61 66 20 63 6f 75 6e 74 20 74 6f 6f  t leaf count too
15ead 20 62 69 67 20 6f 6e 20 70 61 67 65 20 25 64 22   big on page %d"
15eae 2c 20 69 50 61 67 65 29 3b 0a 20 20 20 20 20 20  , iPage);.      
15eaf 20 20 4e 2d 2d 3b 0a 20 20 20 20 20 20 7d 65 6c    N--;.      }el
15eb0 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28  se{.        for(
15eb1 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 29 7b 0a  i=0; i<n; i++){.
15eb2 20 20 20 20 20 20 20 20 20 20 50 67 6e 6f 20 69            Pgno i
15eb3 46 72 65 65 50 61 67 65 20 3d 20 67 65 74 34 62  FreePage = get4b
15eb4 79 74 65 28 26 70 4f 76 66 6c 44 61 74 61 5b 38  yte(&pOvflData[8
15eb5 2b 69 2a 34 5d 29 3b 0a 23 69 66 6e 64 65 66 20  +i*4]);.#ifndef 
15eb6 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
15eb7 56 41 43 55 55 4d 0a 20 20 20 20 20 20 20 20 20  VACUUM.         
15eb8 20 69 66 28 20 70 43 68 65 63 6b 2d 3e 70 42 74   if( pCheck->pBt
15eb9 2d 3e 61 75 74 6f 56 61 63 75 75 6d 20 29 7b 0a  ->autoVacuum ){.
15eba 20 20 20 20 20 20 20 20 20 20 20 20 63 68 65 63              chec
15ebb 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
15ebc 69 46 72 65 65 50 61 67 65 2c 20 50 54 52 4d 41  iFreePage, PTRMA
15ebd 50 5f 46 52 45 45 50 41 47 45 2c 20 30 2c 20 7a  P_FREEPAGE, 0, z
15ebe 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20 20  Context);.      
15ebf 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20      }.#endif.   
15ec0 20 20 20 20 20 20 20 63 68 65 63 6b 52 65 66 28         checkRef(
15ec1 70 43 68 65 63 6b 2c 20 69 46 72 65 65 50 61 67  pCheck, iFreePag
15ec2 65 2c 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20  e, zContext);.  
15ec3 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
15ec4 4e 20 2d 3d 20 6e 3b 0a 20 20 20 20 20 20 7d 0a  N -= n;.      }.
15ec5 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
15ec6 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
15ec7 43 55 55 4d 0a 20 20 20 20 65 6c 73 65 7b 0a 20  CUUM.    else{. 
15ec8 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
15ec9 64 61 74 61 62 61 73 65 20 73 75 70 70 6f 72 74  database support
15eca 73 20 61 75 74 6f 2d 76 61 63 75 75 6d 20 61 6e  s auto-vacuum an
15ecb 64 20 69 50 61 67 65 20 69 73 20 6e 6f 74 20 74  d iPage is not t
15ecc 68 65 20 6c 61 73 74 0a 20 20 20 20 20 20 2a 2a  he last.      **
15ecd 20 70 61 67 65 20 69 6e 20 74 68 69 73 20 6f 76   page in this ov
15ece 65 72 66 6c 6f 77 20 6c 69 73 74 2c 20 63 68 65  erflow list, che
15ecf 63 6b 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e  ck that the poin
15ed0 74 65 72 2d 6d 61 70 20 65 6e 74 72 79 20 66 6f  ter-map entry fo
15ed1 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 66  r.      ** the f
15ed2 6f 6c 6c 6f 77 69 6e 67 20 70 61 67 65 20 6d 61  ollowing page ma
15ed3 74 63 68 65 73 20 69 50 61 67 65 2e 0a 20 20 20  tches iPage..   
15ed4 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20     */.      if( 
15ed5 70 43 68 65 63 6b 2d 3e 70 42 74 2d 3e 61 75 74  pCheck->pBt->aut
15ed6 6f 56 61 63 75 75 6d 20 26 26 20 4e 3e 30 20 29  oVacuum && N>0 )
15ed7 7b 0a 20 20 20 20 20 20 20 20 69 20 3d 20 67 65  {.        i = ge
15ed8 74 34 62 79 74 65 28 70 4f 76 66 6c 44 61 74 61  t4byte(pOvflData
15ed9 29 3b 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b  );.        check
15eda 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 69  Ptrmap(pCheck, i
15edb 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
15edc 57 32 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  W2, iPage, zCont
15edd 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ext);.      }.  
15ede 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 69    }.#endif.    i
15edf 50 61 67 65 20 3d 20 67 65 74 34 62 79 74 65 28  Page = get4byte(
15ee0 70 4f 76 66 6c 44 61 74 61 29 3b 0a 20 20 20 20  pOvflData);.    
15ee1 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e 72 65  sqlite3PagerUnre
15ee2 66 28 70 4f 76 66 6c 50 61 67 65 29 3b 0a 20 20  f(pOvflPage);.  
15ee3 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  }.}.#endif /* SQ
15ee4 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52  LITE_OMIT_INTEGR
15ee5 49 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69  ITY_CHECK */..#i
15ee6 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
15ee7 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
15ee8 4b 0a 2f 2a 0a 2a 2a 20 44 6f 20 76 61 72 69 6f  K./*.** Do vario
15ee9 75 73 20 73 61 6e 69 74 79 20 63 68 65 63 6b 73  us sanity checks
15eea 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 70 61 67   on a single pag
15eeb 65 20 6f 66 20 61 20 74 72 65 65 2e 20 20 52 65  e of a tree.  Re
15eec 74 75 72 6e 0a 2a 2a 20 74 68 65 20 74 72 65 65  turn.** the tree
15eed 20 64 65 70 74 68 2e 20 20 52 6f 6f 74 20 70 61   depth.  Root pa
15eee 67 65 73 20 72 65 74 75 72 6e 20 30 2e 20 20 50  ges return 0.  P
15eef 61 72 65 6e 74 73 20 6f 66 20 72 6f 6f 74 20 70  arents of root p
15ef0 61 67 65 73 0a 2a 2a 20 72 65 74 75 72 6e 20 31  ages.** return 1
15ef1 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68 2e 0a  , and so forth..
15ef2 2a 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 68 65  ** .** These che
15ef3 63 6b 73 20 61 72 65 20 64 6f 6e 65 3a 0a 2a 2a  cks are done:.**
15ef4 0a 2a 2a 20 20 20 20 20 20 31 2e 20 20 4d 61 6b  .**      1.  Mak
15ef5 65 20 73 75 72 65 20 74 68 61 74 20 63 65 6c 6c  e sure that cell
15ef6 73 20 61 6e 64 20 66 72 65 65 62 6c 6f 63 6b 73  s and freeblocks
15ef7 20 64 6f 20 6e 6f 74 20 6f 76 65 72 6c 61 70 0a   do not overlap.
15ef8 2a 2a 20 20 20 20 20 20 20 20 20 20 62 75 74 20  **          but 
15ef9 63 6f 6d 62 69 6e 65 20 74 6f 20 63 6f 6d 70 6c  combine to compl
15efa 65 74 65 6c 79 20 63 6f 76 65 72 20 74 68 65 20  etely cover the 
15efb 70 61 67 65 2e 0a 2a 2a 20 20 4e 4f 20 20 32 2e  page..**  NO  2.
15efc 20 20 4d 61 6b 65 20 73 75 72 65 20 63 65 6c 6c    Make sure cell
15efd 20 6b 65 79 73 20 61 72 65 20 69 6e 20 6f 72 64   keys are in ord
15efe 65 72 2e 0a 2a 2a 20 20 4e 4f 20 20 33 2e 20 20  er..**  NO  3.  
15eff 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20 6b 65 79  Make sure no key
15f00 20 69 73 20 6c 65 73 73 20 74 68 61 6e 20 6f 72   is less than or
15f01 20 65 71 75 61 6c 20 74 6f 20 7a 4c 6f 77 65 72   equal to zLower
15f02 42 6f 75 6e 64 2e 0a 2a 2a 20 20 4e 4f 20 20 34  Bound..**  NO  4
15f03 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 6e 6f 20  .  Make sure no 
15f04 6b 65 79 20 69 73 20 67 72 65 61 74 65 72 20 74  key is greater t
15f05 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20  han or equal to 
15f06 7a 55 70 70 65 72 42 6f 75 6e 64 2e 0a 2a 2a 20  zUpperBound..** 
15f07 20 20 20 20 20 35 2e 20 20 43 68 65 63 6b 20 74       5.  Check t
15f08 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20  he integrity of 
15f09 6f 76 65 72 66 6c 6f 77 20 70 61 67 65 73 2e 0a  overflow pages..
15f0a 2a 2a 20 20 20 20 20 20 36 2e 20 20 52 65 63 75  **      6.  Recu
15f0b 72 73 69 76 65 6c 79 20 63 61 6c 6c 20 63 68 65  rsively call che
15f0c 63 6b 54 72 65 65 50 61 67 65 20 6f 6e 20 61 6c  ckTreePage on al
15f0d 6c 20 63 68 69 6c 64 72 65 6e 2e 0a 2a 2a 20 20  l children..**  
15f0e 20 20 20 20 37 2e 20 20 56 65 72 69 66 79 20 74      7.  Verify t
15f0f 68 61 74 20 74 68 65 20 64 65 70 74 68 20 6f 66  hat the depth of
15f10 20 61 6c 6c 20 63 68 69 6c 64 72 65 6e 20 69 73   all children is
15f11 20 74 68 65 20 73 61 6d 65 2e 0a 2a 2a 20 20 20   the same..**   
15f12 20 20 20 38 2e 20 20 4d 61 6b 65 20 73 75 72 65     8.  Make sure
15f13 20 74 68 69 73 20 70 61 67 65 20 69 73 20 61 74   this page is at
15f14 20 6c 65 61 73 74 20 33 33 25 20 66 75 6c 6c 20   least 33% full 
15f15 6f 72 20 65 6c 73 65 20 69 74 20 69 73 0a 2a 2a  or else it is.**
15f16 20 20 20 20 20 20 20 20 20 20 74 68 65 20 72 6f            the ro
15f17 6f 74 20 6f 66 20 74 68 65 20 74 72 65 65 2e 0a  ot of the tree..
15f18 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 68  */.static int ch
15f19 65 63 6b 54 72 65 65 50 61 67 65 28 0a 20 20 49  eckTreePage(.  I
15f1a 6e 74 65 67 72 69 74 79 43 6b 20 2a 70 43 68 65  ntegrityCk *pChe
15f1b 63 6b 2c 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20  ck,  /* Context 
15f1c 66 6f 72 20 74 68 65 20 73 61 6e 69 74 79 20 63  for the sanity c
15f1d 68 65 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 50  heck */.  int iP
15f1e 61 67 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  age,            
15f1f 2f 2a 20 50 61 67 65 20 6e 75 6d 62 65 72 20 6f  /* Page number o
15f20 66 20 74 68 65 20 70 61 67 65 20 74 6f 20 63 68  f the page to ch
15f21 65 63 6b 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  eck */.  char *z
15f22 50 61 72 65 6e 74 43 6f 6e 74 65 78 74 20 20 2f  ParentContext  /
15f23 2a 20 50 61 72 65 6e 74 20 63 6f 6e 74 65 78 74  * Parent context
15f24 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 50 61 67 65   */.){.  MemPage
15f25 20 2a 70 50 61 67 65 3b 0a 20 20 69 6e 74 20 69   *pPage;.  int i
15f26 2c 20 72 63 2c 20 64 65 70 74 68 2c 20 64 32 2c  , rc, depth, d2,
15f27 20 70 67 6e 6f 2c 20 63 6e 74 3b 0a 20 20 69 6e   pgno, cnt;.  in
15f28 74 20 68 64 72 2c 20 63 65 6c 6c 53 74 61 72 74  t hdr, cellStart
15f29 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 0a 20  ;.  int nCell;. 
15f2a 20 75 38 20 2a 64 61 74 61 3b 0a 20 20 42 74 53   u8 *data;.  BtS
15f2b 68 61 72 65 64 20 2a 70 42 74 3b 0a 20 20 69 6e  hared *pBt;.  in
15f2c 74 20 75 73 61 62 6c 65 53 69 7a 65 3b 0a 20 20  t usableSize;.  
15f2d 63 68 61 72 20 7a 43 6f 6e 74 65 78 74 5b 31 30  char zContext[10
15f2e 30 5d 3b 0a 20 20 63 68 61 72 20 2a 68 69 74 20  0];.  char *hit 
15f2f 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f  = 0;..  sqlite3_
15f30 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
15f31 7a 43 6f 6e 74 65 78 74 29 2c 20 7a 43 6f 6e 74  zContext), zCont
15f32 65 78 74 2c 20 22 50 61 67 65 20 25 64 3a 20 22  ext, "Page %d: "
15f33 2c 20 69 50 61 67 65 29 3b 0a 0a 20 20 2f 2a 20  , iPage);..  /* 
15f34 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20 70  Check that the p
15f35 61 67 65 20 65 78 69 73 74 73 0a 20 20 2a 2f 0a  age exists.  */.
15f36 20 20 70 42 74 20 3d 20 70 43 68 65 63 6b 2d 3e    pBt = pCheck->
15f37 70 42 74 3b 0a 20 20 75 73 61 62 6c 65 53 69 7a  pBt;.  usableSiz
15f38 65 20 3d 20 70 42 74 2d 3e 75 73 61 62 6c 65 53  e = pBt->usableS
15f39 69 7a 65 3b 0a 20 20 69 66 28 20 69 50 61 67 65  ize;.  if( iPage
15f3a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
15f3b 20 20 69 66 28 20 63 68 65 63 6b 52 65 66 28 70    if( checkRef(p
15f3c 43 68 65 63 6b 2c 20 69 50 61 67 65 2c 20 7a 50  Check, iPage, zP
15f3d 61 72 65 6e 74 43 6f 6e 74 65 78 74 29 20 29 20  arentContext) ) 
15f3e 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20  return 0;.  if( 
15f3f 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72  (rc = sqlite3Btr
15f40 65 65 47 65 74 50 61 67 65 28 70 42 74 2c 20 28  eeGetPage(pBt, (
15f41 50 67 6e 6f 29 69 50 61 67 65 2c 20 26 70 50 61  Pgno)iPage, &pPa
15f42 67 65 2c 20 30 29 29 21 3d 30 20 29 7b 0a 20 20  ge, 0))!=0 ){.  
15f43 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73 67    checkAppendMsg
15f44 28 70 43 68 65 63 6b 2c 20 7a 43 6f 6e 74 65 78  (pCheck, zContex
15f45 74 2c 0a 20 20 20 20 20 20 20 22 75 6e 61 62 6c  t,.       "unabl
15f46 65 20 74 6f 20 67 65 74 20 74 68 65 20 70 61 67  e to get the pag
15f47 65 2e 20 65 72 72 6f 72 20 63 6f 64 65 3d 25 64  e. error code=%d
15f48 22 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 74 75  ", rc);.    retu
15f49 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20  rn 0;.  }.  if( 
15f4a 28 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72  (rc = sqlite3Btr
15f4b 65 65 49 6e 69 74 50 61 67 65 28 70 50 61 67 65  eeInitPage(pPage
15f4c 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 63 68 65  ))!=0 ){.    che
15f4d 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
15f4e 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  ck, zContext, . 
15f4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15f50 20 20 22 73 71 6c 69 74 65 33 42 74 72 65 65 49    "sqlite3BtreeI
15f51 6e 69 74 50 61 67 65 28 29 20 72 65 74 75 72 6e  nitPage() return
15f52 73 20 65 72 72 6f 72 20 63 6f 64 65 20 25 64 22  s error code %d"
15f53 2c 20 72 63 29 3b 0a 20 20 20 20 72 65 6c 65 61  , rc);.    relea
15f54 73 65 50 61 67 65 28 70 50 61 67 65 29 3b 0a 20  sePage(pPage);. 
15f55 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
15f56 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 6f 75 74  ..  /* Check out
15f57 20 61 6c 6c 20 74 68 65 20 63 65 6c 6c 73 2e 0a   all the cells..
15f58 20 20 2a 2f 0a 20 20 64 65 70 74 68 20 3d 20 30    */.  depth = 0
15f59 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
15f5a 50 61 67 65 2d 3e 6e 43 65 6c 6c 20 26 26 20 70  Page->nCell && p
15f5b 43 68 65 63 6b 2d 3e 6d 78 45 72 72 3b 20 69 2b  Check->mxErr; i+
15f5c 2b 29 7b 0a 20 20 20 20 75 38 20 2a 70 43 65 6c  +){.    u8 *pCel
15f5d 6c 3b 0a 20 20 20 20 75 33 32 20 73 7a 3b 0a 20  l;.    u32 sz;. 
15f5e 20 20 20 43 65 6c 6c 49 6e 66 6f 20 69 6e 66 6f     CellInfo info
15f5f 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  ;..    /* Check 
15f60 70 61 79 6c 6f 61 64 20 6f 76 65 72 66 6c 6f 77  payload overflow
15f61 20 70 61 67 65 73 0a 20 20 20 20 2a 2f 0a 20 20   pages.    */.  
15f62 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
15f63 74 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65  tf(sizeof(zConte
15f64 78 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 0a 20  xt), zContext,. 
15f65 20 20 20 20 20 20 20 20 20 20 20 20 22 4f 6e 20              "On 
15f66 74 72 65 65 20 70 61 67 65 20 25 64 20 63 65 6c  tree page %d cel
15f67 6c 20 25 64 3a 20 22 2c 20 69 50 61 67 65 2c 20  l %d: ", iPage, 
15f68 69 29 3b 0a 20 20 20 20 70 43 65 6c 6c 20 3d 20  i);.    pCell = 
15f69 66 69 6e 64 43 65 6c 6c 28 70 50 61 67 65 2c 69  findCell(pPage,i
15f6a 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  );.    sqlite3Bt
15f6b 72 65 65 50 61 72 73 65 43 65 6c 6c 50 74 72 28  reeParseCellPtr(
15f6c 70 50 61 67 65 2c 20 70 43 65 6c 6c 2c 20 26 69  pPage, pCell, &i
15f6d 6e 66 6f 29 3b 0a 20 20 20 20 73 7a 20 3d 20 69  nfo);.    sz = i
15f6e 6e 66 6f 2e 6e 44 61 74 61 3b 0a 20 20 20 20 69  nfo.nData;.    i
15f6f 66 28 20 21 70 50 61 67 65 2d 3e 69 6e 74 4b 65  f( !pPage->intKe
15f70 79 20 29 20 73 7a 20 2b 3d 20 69 6e 66 6f 2e 6e  y ) sz += info.n
15f71 4b 65 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Key;.    assert(
15f72 20 73 7a 3d 3d 69 6e 66 6f 2e 6e 50 61 79 6c 6f   sz==info.nPaylo
15f73 61 64 20 29 3b 0a 20 20 20 20 69 66 28 20 73 7a  ad );.    if( sz
15f74 3e 69 6e 66 6f 2e 6e 4c 6f 63 61 6c 20 29 7b 0a  >info.nLocal ){.
15f75 20 20 20 20 20 20 69 6e 74 20 6e 50 61 67 65 20        int nPage 
15f76 3d 20 28 73 7a 20 2d 20 69 6e 66 6f 2e 6e 4c 6f  = (sz - info.nLo
15f77 63 61 6c 20 2b 20 75 73 61 62 6c 65 53 69 7a 65  cal + usableSize
15f78 20 2d 20 35 29 2f 28 75 73 61 62 6c 65 53 69 7a   - 5)/(usableSiz
15f79 65 20 2d 20 34 29 3b 0a 20 20 20 20 20 20 50 67  e - 4);.      Pg
15f7a 6e 6f 20 70 67 6e 6f 4f 76 66 6c 20 3d 20 67 65  no pgnoOvfl = ge
15f7b 74 34 62 79 74 65 28 26 70 43 65 6c 6c 5b 69 6e  t4byte(&pCell[in
15f7c 66 6f 2e 69 4f 76 65 72 66 6c 6f 77 5d 29 3b 0a  fo.iOverflow]);.
15f7d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
15f7e 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
15f7f 20 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61 75       if( pBt->au
15f80 74 6f 56 61 63 75 75 6d 20 29 7b 0a 20 20 20 20  toVacuum ){.    
15f81 20 20 20 20 63 68 65 63 6b 50 74 72 6d 61 70 28      checkPtrmap(
15f82 70 43 68 65 63 6b 2c 20 70 67 6e 6f 4f 76 66 6c  pCheck, pgnoOvfl
15f83 2c 20 50 54 52 4d 41 50 5f 4f 56 45 52 46 4c 4f  , PTRMAP_OVERFLO
15f84 57 31 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  W1, iPage, zCont
15f85 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
15f86 6e 64 69 66 0a 20 20 20 20 20 20 63 68 65 63 6b  ndif.      check
15f87 4c 69 73 74 28 70 43 68 65 63 6b 2c 20 30 2c 20  List(pCheck, 0, 
15f88 70 67 6e 6f 4f 76 66 6c 2c 20 6e 50 61 67 65 2c  pgnoOvfl, nPage,
15f89 20 7a 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20   zContext);.    
15f8a 7d 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  }..    /* Check 
15f8b 73 61 6e 69 74 79 20 6f 66 20 6c 65 66 74 20 63  sanity of left c
15f8c 68 69 6c 64 20 70 61 67 65 2e 0a 20 20 20 20 2a  hild page..    *
15f8d 2f 0a 20 20 20 20 69 66 28 20 21 70 50 61 67 65  /.    if( !pPage
15f8e 2d 3e 6c 65 61 66 20 29 7b 0a 20 20 20 20 20 20  ->leaf ){.      
15f8f 70 67 6e 6f 20 3d 20 67 65 74 34 62 79 74 65 28  pgno = get4byte(
15f90 70 43 65 6c 6c 29 3b 0a 23 69 66 6e 64 65 66 20  pCell);.#ifndef 
15f91 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
15f92 56 41 43 55 55 4d 0a 20 20 20 20 20 20 69 66 28  VACUUM.      if(
15f93 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d   pBt->autoVacuum
15f94 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65 63   ){.        chec
15f95 6b 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20  kPtrmap(pCheck, 
15f96 70 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52  pgno, PTRMAP_BTR
15f97 45 45 2c 20 69 50 61 67 65 2c 20 7a 43 6f 6e 74  EE, iPage, zCont
15f98 65 78 74 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65  ext);.      }.#e
15f99 6e 64 69 66 0a 20 20 20 20 20 20 64 32 20 3d 20  ndif.      d2 = 
15f9a 63 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43  checkTreePage(pC
15f9b 68 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e  heck, pgno, zCon
15f9c 74 65 78 74 29 3b 0a 20 20 20 20 20 20 69 66 28  text);.      if(
15f9d 20 69 3e 30 20 26 26 20 64 32 21 3d 64 65 70 74   i>0 && d2!=dept
15f9e 68 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  h ){.        che
15f9f 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
15fa0 63 6b 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 22 43  ck, zContext, "C
15fa1 68 69 6c 64 20 70 61 67 65 20 64 65 70 74 68 20  hild page depth 
15fa2 64 69 66 66 65 72 73 22 29 3b 0a 20 20 20 20 20  differs");.     
15fa3 20 7d 0a 20 20 20 20 20 20 64 65 70 74 68 20 3d   }.      depth =
15fa4 20 64 32 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20   d2;.    }.  }. 
15fa5 20 69 66 28 20 21 70 50 61 67 65 2d 3e 6c 65 61   if( !pPage->lea
15fa6 66 20 29 7b 0a 20 20 20 20 70 67 6e 6f 20 3d 20  f ){.    pgno = 
15fa7 67 65 74 34 62 79 74 65 28 26 70 50 61 67 65 2d  get4byte(&pPage-
15fa8 3e 61 44 61 74 61 5b 70 50 61 67 65 2d 3e 68 64  >aData[pPage->hd
15fa9 72 4f 66 66 73 65 74 2b 38 5d 29 3b 0a 20 20 20  rOffset+8]);.   
15faa 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
15fab 66 28 73 69 7a 65 6f 66 28 7a 43 6f 6e 74 65 78  f(sizeof(zContex
15fac 74 29 2c 20 7a 43 6f 6e 74 65 78 74 2c 20 0a 20  t), zContext, . 
15fad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
15fae 20 20 20 20 22 4f 6e 20 70 61 67 65 20 25 64 20      "On page %d 
15faf 61 74 20 72 69 67 68 74 20 63 68 69 6c 64 3a 20  at right child: 
15fb0 22 2c 20 69 50 61 67 65 29 3b 0a 23 69 66 6e 64  ", iPage);.#ifnd
15fb1 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
15fb2 55 54 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66  UTOVACUUM.    if
15fb3 28 20 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75  ( pBt->autoVacuu
15fb4 6d 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b  m ){.      check
15fb5 50 74 72 6d 61 70 28 70 43 68 65 63 6b 2c 20 70  Ptrmap(pCheck, p
15fb6 67 6e 6f 2c 20 50 54 52 4d 41 50 5f 42 54 52 45  gno, PTRMAP_BTRE
15fb7 45 2c 20 69 50 61 67 65 2c 20 30 29 3b 0a 20 20  E, iPage, 0);.  
15fb8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
15fb9 68 65 63 6b 54 72 65 65 50 61 67 65 28 70 43 68  heckTreePage(pCh
15fba 65 63 6b 2c 20 70 67 6e 6f 2c 20 7a 43 6f 6e 74  eck, pgno, zCont
15fbb 65 78 74 29 3b 0a 20 20 7d 0a 20 0a 20 20 2f 2a  ext);.  }. .  /*
15fbc 20 43 68 65 63 6b 20 66 6f 72 20 63 6f 6d 70 6c   Check for compl
15fbd 65 74 65 20 63 6f 76 65 72 61 67 65 20 6f 66 20  ete coverage of 
15fbe 74 68 65 20 70 61 67 65 0a 20 20 2a 2f 0a 20 20  the page.  */.  
15fbf 64 61 74 61 20 3d 20 70 50 61 67 65 2d 3e 61 44  data = pPage->aD
15fc0 61 74 61 3b 0a 20 20 68 64 72 20 3d 20 70 50 61  ata;.  hdr = pPa
15fc1 67 65 2d 3e 68 64 72 4f 66 66 73 65 74 3b 0a 20  ge->hdrOffset;. 
15fc2 20 68 69 74 20 3d 20 73 71 6c 69 74 65 33 50 61   hit = sqlite3Pa
15fc3 67 65 4d 61 6c 6c 6f 63 28 20 70 42 74 2d 3e 70  geMalloc( pBt->p
15fc4 61 67 65 53 69 7a 65 20 29 3b 0a 20 20 69 66 28  ageSize );.  if(
15fc5 20 68 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70   hit==0 ){.    p
15fc6 43 68 65 63 6b 2d 3e 6d 61 6c 6c 6f 63 46 61 69  Check->mallocFai
15fc7 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65  led = 1;.  }else
15fc8 7b 0a 20 20 20 20 75 31 36 20 63 6f 6e 74 65 6e  {.    u16 conten
15fc9 74 4f 66 66 73 65 74 20 3d 20 67 65 74 32 62 79  tOffset = get2by
15fca 74 65 28 26 64 61 74 61 5b 68 64 72 2b 35 5d 29  te(&data[hdr+5])
15fcb 3b 0a 20 20 20 20 69 66 20 28 63 6f 6e 74 65 6e  ;.    if (conten
15fcc 74 4f 66 66 73 65 74 20 3e 20 75 73 61 62 6c 65  tOffset > usable
15fcd 53 69 7a 65 29 20 7b 0a 20 20 20 20 20 20 63 68  Size) {.      ch
15fce 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68  eckAppendMsg(pCh
15fcf 65 63 6b 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  eck, 0, .       
15fd0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 43                "C
15fd1 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65 63 74  orruption detect
15fd2 65 64 20 69 6e 20 68 65 61 64 65 72 20 6f 6e 20  ed in header on 
15fd3 70 61 67 65 20 25 64 22 2c 69 50 61 67 65 2c 30  page %d",iPage,0
15fd4 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 63 68  );.      goto ch
15fd5 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 3b 0a  eck_page_abort;.
15fd6 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73 65 74      }.    memset
15fd7 28 68 69 74 2b 63 6f 6e 74 65 6e 74 4f 66 66 73  (hit+contentOffs
15fd8 65 74 2c 20 30 2c 20 75 73 61 62 6c 65 53 69 7a  et, 0, usableSiz
15fd9 65 2d 63 6f 6e 74 65 6e 74 4f 66 66 73 65 74 29  e-contentOffset)
15fda 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28 68 69 74  ;.    memset(hit
15fdb 2c 20 31 2c 20 63 6f 6e 74 65 6e 74 4f 66 66 73  , 1, contentOffs
15fdc 65 74 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  et);.    nCell =
15fdd 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61 5b   get2byte(&data[
15fde 68 64 72 2b 33 5d 29 3b 0a 20 20 20 20 63 65 6c  hdr+3]);.    cel
15fdf 6c 53 74 61 72 74 20 3d 20 68 64 72 20 2b 20 31  lStart = hdr + 1
15fe0 32 20 2d 20 34 2a 70 50 61 67 65 2d 3e 6c 65 61  2 - 4*pPage->lea
15fe1 66 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  f;.    for(i=0; 
15fe2 69 3c 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20  i<nCell; i++){. 
15fe3 20 20 20 20 20 69 6e 74 20 70 63 20 3d 20 67 65       int pc = ge
15fe4 74 32 62 79 74 65 28 26 64 61 74 61 5b 63 65 6c  t2byte(&data[cel
15fe5 6c 53 74 61 72 74 2b 69 2a 32 5d 29 3b 0a 20 20  lStart+i*2]);.  
15fe6 20 20 20 20 75 31 36 20 73 69 7a 65 20 3d 20 31      u16 size = 1
15fe7 30 32 34 3b 0a 20 20 20 20 20 20 69 6e 74 20 6a  024;.      int j
15fe8 3b 0a 20 20 20 20 20 20 69 66 28 20 70 63 3c 3d  ;.      if( pc<=
15fe9 75 73 61 62 6c 65 53 69 7a 65 20 29 7b 0a 20 20  usableSize ){.  
15fea 20 20 20 20 20 20 73 69 7a 65 20 3d 20 63 65 6c        size = cel
15feb 6c 53 69 7a 65 50 74 72 28 70 50 61 67 65 2c 20  lSizePtr(pPage, 
15fec 26 64 61 74 61 5b 70 63 5d 29 3b 0a 20 20 20 20  &data[pc]);.    
15fed 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 28 70    }.      if( (p
15fee 63 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c  c+size-1)>=usabl
15fef 65 53 69 7a 65 20 7c 7c 20 70 63 3c 30 20 29 7b  eSize || pc<0 ){
15ff0 0a 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70  .        checkAp
15ff1 70 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20  pendMsg(pCheck, 
15ff2 30 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  0, .            
15ff3 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65  "Corruption dete
15ff4 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20  cted in cell %d 
15ff5 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50  on page %d",i,iP
15ff6 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65  age,0);.      }e
15ff7 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72  lse{.        for
15ff8 28 6a 3d 70 63 2b 73 69 7a 65 2d 31 3b 20 6a 3e  (j=pc+size-1; j>
15ff9 3d 70 63 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d  =pc; j--) hit[j]
15ffa 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ++;.      }.    
15ffb 7d 0a 20 20 20 20 66 6f 72 28 63 6e 74 3d 30 2c  }.    for(cnt=0,
15ffc 20 69 3d 67 65 74 32 62 79 74 65 28 26 64 61 74   i=get2byte(&dat
15ffd 61 5b 68 64 72 2b 31 5d 29 3b 20 69 3e 30 20 26  a[hdr+1]); i>0 &
15ffe 26 20 69 3c 75 73 61 62 6c 65 53 69 7a 65 20 26  & i<usableSize &
15fff 26 20 63 6e 74 3c 31 30 30 30 30 3b 20 0a 20 20  & cnt<10000; .  
16000 20 20 20 20 20 20 20 20 20 63 6e 74 2b 2b 29 7b           cnt++){
16001 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65 20  .      int size 
16002 3d 20 67 65 74 32 62 79 74 65 28 26 64 61 74 61  = get2byte(&data
16003 5b 69 2b 32 5d 29 3b 0a 20 20 20 20 20 20 69 6e  [i+2]);.      in
16004 74 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 28  t j;.      if( (
16005 69 2b 73 69 7a 65 2d 31 29 3e 3d 75 73 61 62 6c  i+size-1)>=usabl
16006 65 53 69 7a 65 20 7c 7c 20 69 3c 30 20 29 7b 0a  eSize || i<0 ){.
16007 20 20 20 20 20 20 20 20 63 68 65 63 6b 41 70 70          checkApp
16008 65 6e 64 4d 73 67 28 70 43 68 65 63 6b 2c 20 30  endMsg(pCheck, 0
16009 2c 20 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ,  .            
1600a 22 43 6f 72 72 75 70 74 69 6f 6e 20 64 65 74 65  "Corruption dete
1600b 63 74 65 64 20 69 6e 20 63 65 6c 6c 20 25 64 20  cted in cell %d 
1600c 6f 6e 20 70 61 67 65 20 25 64 22 2c 69 2c 69 50  on page %d",i,iP
1600d 61 67 65 2c 30 29 3b 0a 20 20 20 20 20 20 7d 65  age,0);.      }e
1600e 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 66 6f 72  lse{.        for
1600f 28 6a 3d 69 2b 73 69 7a 65 2d 31 3b 20 6a 3e 3d  (j=i+size-1; j>=
16010 69 3b 20 6a 2d 2d 29 20 68 69 74 5b 6a 5d 2b 2b  i; j--) hit[j]++
16011 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
16012 69 20 3d 20 67 65 74 32 62 79 74 65 28 26 64 61  i = get2byte(&da
16013 74 61 5b 69 5d 29 3b 0a 20 20 20 20 7d 0a 20 20  ta[i]);.    }.  
16014 20 20 66 6f 72 28 69 3d 63 6e 74 3d 30 3b 20 69    for(i=cnt=0; i
16015 3c 75 73 61 62 6c 65 53 69 7a 65 3b 20 69 2b 2b  <usableSize; i++
16016 29 7b 0a 20 20 20 20 20 20 69 66 28 20 68 69 74  ){.      if( hit
16017 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  [i]==0 ){.      
16018 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d    cnt++;.      }
16019 65 6c 73 65 20 69 66 28 20 68 69 74 5b 69 5d 3e  else if( hit[i]>
1601a 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 65  1 ){.        che
1601b 63 6b 41 70 70 65 6e 64 4d 73 67 28 70 43 68 65  ckAppendMsg(pChe
1601c 63 6b 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20  ck, 0,.         
1601d 20 22 4d 75 6c 74 69 70 6c 65 20 75 73 65 73 20   "Multiple uses 
1601e 66 6f 72 20 62 79 74 65 20 25 64 20 6f 66 20 70  for byte %d of p
1601f 61 67 65 20 25 64 22 2c 20 69 2c 20 69 50 61 67  age %d", i, iPag
16020 65 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  e);.        brea
16021 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  k;.      }.    }
16022 0a 20 20 20 20 69 66 28 20 63 6e 74 21 3d 64 61  .    if( cnt!=da
16023 74 61 5b 68 64 72 2b 37 5d 20 29 7b 0a 20 20 20  ta[hdr+7] ){.   
16024 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64 4d 73     checkAppendMs
16025 67 28 70 43 68 65 63 6b 2c 20 30 2c 20 0a 20 20  g(pCheck, 0, .  
16026 20 20 20 20 20 20 20 20 22 46 72 61 67 6d 65 6e          "Fragmen
16027 74 65 64 20 73 70 61 63 65 20 69 73 20 25 64 20  ted space is %d 
16028 62 79 74 65 20 72 65 70 6f 72 74 65 64 20 61 73  byte reported as
16029 20 25 64 20 6f 6e 20 70 61 67 65 20 25 64 22 2c   %d on page %d",
1602a 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 2c 20  .          cnt, 
1602b 64 61 74 61 5b 68 64 72 2b 37 5d 2c 20 69 50 61  data[hdr+7], iPa
1602c 67 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 63  ge);.    }.  }.c
1602d 68 65 63 6b 5f 70 61 67 65 5f 61 62 6f 72 74 3a  heck_page_abort:
1602e 0a 20 20 69 66 20 28 68 69 74 29 20 73 71 6c 69  .  if (hit) sqli
1602f 74 65 33 50 61 67 65 46 72 65 65 28 68 69 74 29  te3PageFree(hit)
16030 3b 0a 0a 20 20 72 65 6c 65 61 73 65 50 61 67 65  ;..  releasePage
16031 28 70 50 61 67 65 29 3b 0a 20 20 72 65 74 75 72  (pPage);.  retur
16032 6e 20 64 65 70 74 68 2b 31 3b 0a 7d 0a 23 65 6e  n depth+1;.}.#en
16033 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
16034 49 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45  IT_INTEGRITY_CHE
16035 43 4b 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  CK */..#ifndef S
16036 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47  QLITE_OMIT_INTEG
16037 52 49 54 59 5f 43 48 45 43 4b 0a 2f 2a 0a 2a 2a  RITY_CHECK./*.**
16038 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f   This routine do
16039 65 73 20 61 20 63 6f 6d 70 6c 65 74 65 20 63 68  es a complete ch
1603a 65 63 6b 20 6f 66 20 74 68 65 20 67 69 76 65 6e  eck of the given
1603b 20 42 54 72 65 65 20 66 69 6c 65 2e 20 20 61 52   BTree file.  aR
1603c 6f 6f 74 5b 5d 20 69 73 0a 2a 2a 20 61 6e 20 61  oot[] is.** an a
1603d 72 72 61 79 20 6f 66 20 70 61 67 65 73 20 6e 75  rray of pages nu
1603e 6d 62 65 72 73 20 77 65 72 65 20 65 61 63 68 20  mbers were each 
1603f 70 61 67 65 20 6e 75 6d 62 65 72 20 69 73 20 74  page number is t
16040 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6f 66 0a  he root page of.
16041 2a 2a 20 61 20 74 61 62 6c 65 2e 20 20 6e 52 6f  ** a table.  nRo
16042 6f 74 20 69 73 20 74 68 65 20 6e 75 6d 62 65 72  ot is the number
16043 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
16044 52 6f 6f 74 2e 0a 2a 2a 0a 2a 2a 20 57 72 69 74  Root..**.** Writ
16045 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
16046 65 72 72 6f 72 20 73 65 65 6e 20 69 6e 20 2a 70  error seen in *p
16047 6e 45 72 72 2e 20 20 45 78 63 65 70 74 20 66 6f  nErr.  Except fo
16048 72 20 73 6f 6d 65 20 6d 65 6d 6f 72 79 0a 2a 2a  r some memory.**
16049 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
1604a 72 73 2c 20 20 6e 6e 20 65 72 72 6f 72 20 6d 65  rs,  nn error me
1604b 73 73 61 67 65 20 69 73 20 68 65 6c 64 20 69 6e  ssage is held in
1604c 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
1604d 20 66 72 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 20   from.** malloc 
1604e 69 73 20 72 65 74 75 72 6e 65 64 20 69 66 20 2a  is returned if *
1604f 70 6e 45 72 72 20 69 73 20 6e 6f 6e 2d 7a 65 72  pnErr is non-zer
16050 6f 2e 20 20 49 66 20 2a 70 6e 45 72 72 3d 3d 30  o.  If *pnErr==0
16051 20 74 68 65 6e 20 4e 55 4c 4c 20 69 73 0a 2a 2a   then NULL is.**
16052 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51   returned..*/.SQ
16053 4c 49 54 45 5f 50 52 49 56 41 54 45 20 63 68 61  LITE_PRIVATE cha
16054 72 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 49  r *sqlite3BtreeI
16055 6e 74 65 67 72 69 74 79 43 68 65 63 6b 28 0a 20  ntegrityCheck(. 
16056 20 42 74 72 65 65 20 2a 70 2c 20 20 20 20 20 2f   Btree *p,     /
16057 2a 20 54 68 65 20 62 74 72 65 65 20 74 6f 20 62  * The btree to b
16058 65 20 63 68 65 63 6b 65 64 20 2a 2f 0a 20 20 69  e checked */.  i
16059 6e 74 20 2a 61 52 6f 6f 74 2c 20 20 20 2f 2a 20  nt *aRoot,   /* 
1605a 41 6e 20 61 72 72 61 79 20 6f 66 20 72 6f 6f 74  An array of root
1605b 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73 20 66   pages numbers f
1605c 6f 72 20 69 6e 64 69 76 69 64 75 61 6c 20 74 72  or individual tr
1605d 65 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 6f  ees */.  int nRo
1605e 6f 74 2c 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72  ot,    /* Number
1605f 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
16060 52 6f 6f 74 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20  Root[] */.  int 
16061 6d 78 45 72 72 2c 20 20 20 20 2f 2a 20 53 74 6f  mxErr,    /* Sto
16062 70 20 72 65 70 6f 72 74 69 6e 67 20 65 72 72 6f  p reporting erro
16063 72 73 20 61 66 74 65 72 20 74 68 69 73 20 6d 61  rs after this ma
16064 6e 79 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 45  ny */.  int *pnE
16065 72 72 20 20 20 20 2f 2a 20 57 72 69 74 65 20 6e  rr    /* Write n
16066 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72 73 20  umber of errors 
16067 73 65 65 6e 20 74 6f 20 74 68 69 73 20 76 61 72  seen to this var
16068 69 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 50 67  iable */.){.  Pg
16069 6e 6f 20 69 3b 0a 20 20 69 6e 74 20 6e 52 65 66  no i;.  int nRef
1606a 3b 0a 20 20 49 6e 74 65 67 72 69 74 79 43 6b 20  ;.  IntegrityCk 
1606b 73 43 68 65 63 6b 3b 0a 20 20 42 74 53 68 61 72  sCheck;.  BtShar
1606c 65 64 20 2a 70 42 74 20 3d 20 70 2d 3e 70 42 74  ed *pBt = p->pBt
1606d 3b 0a 20 20 63 68 61 72 20 7a 45 72 72 5b 31 30  ;.  char zErr[10
1606e 30 5d 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74  0];..  sqlite3Bt
1606f 72 65 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 70  reeEnter(p);.  p
16070 42 74 2d 3e 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  Bt->db = p->db;.
16071 20 20 6e 52 65 66 20 3d 20 73 71 6c 69 74 65 33    nRef = sqlite3
16072 50 61 67 65 72 52 65 66 63 6f 75 6e 74 28 70 42  PagerRefcount(pB
16073 74 2d 3e 70 50 61 67 65 72 29 3b 0a 20 20 69 66  t->pPager);.  if
16074 28 20 6c 6f 63 6b 42 74 72 65 65 57 69 74 68 52  ( lockBtreeWithR
16075 65 74 72 79 28 70 29 21 3d 53 51 4c 49 54 45 5f  etry(p)!=SQLITE_
16076 4f 4b 20 29 7b 0a 20 20 20 20 2a 70 6e 45 72 72  OK ){.    *pnErr
16077 20 3d 20 31 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 1;.    sqlite
16078 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b 0a  3BtreeLeave(p);.
16079 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
1607a 65 33 44 62 53 74 72 44 75 70 28 30 2c 20 22 63  e3DbStrDup(0, "c
1607b 61 6e 6e 6f 74 20 61 63 71 75 69 72 65 20 61 20  annot acquire a 
1607c 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74 68 65  read lock on the
1607d 20 64 61 74 61 62 61 73 65 22 29 3b 0a 20 20 7d   database");.  }
1607e 0a 20 20 73 43 68 65 63 6b 2e 70 42 74 20 3d 20  .  sCheck.pBt = 
1607f 70 42 74 3b 0a 20 20 73 43 68 65 63 6b 2e 70 50  pBt;.  sCheck.pP
16080 61 67 65 72 20 3d 20 70 42 74 2d 3e 70 50 61 67  ager = pBt->pPag
16081 65 72 3b 0a 20 20 73 43 68 65 63 6b 2e 6e 50 61  er;.  sCheck.nPa
16082 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ge = pagerPageco
16083 75 6e 74 28 73 43 68 65 63 6b 2e 70 42 74 29 3b  unt(sCheck.pBt);
16084 0a 20 20 73 43 68 65 63 6b 2e 6d 78 45 72 72 20  .  sCheck.mxErr 
16085 3d 20 6d 78 45 72 72 3b 0a 20 20 73 43 68 65 63  = mxErr;.  sChec
16086 6b 2e 6e 45 72 72 20 3d 20 30 3b 0a 20 20 73 43  k.nErr = 0;.  sC
16087 68 65 63 6b 2e 6d 61 6c 6c 6f 63 46 61 69 6c 65  heck.mallocFaile
16088 64 20 3d 20 30 3b 0a 20 20 2a 70 6e 45 72 72 20  d = 0;.  *pnErr 
16089 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  = 0;.#ifndef SQL
1608a 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43  ITE_OMIT_AUTOVAC
1608b 55 55 4d 0a 20 20 69 66 28 20 70 42 74 2d 3e 6e  UUM.  if( pBt->n
1608c 54 72 75 6e 63 21 3d 30 20 29 7b 0a 20 20 20 20  Trunc!=0 ){.    
1608d 73 43 68 65 63 6b 2e 6e 50 61 67 65 20 3d 20 70  sCheck.nPage = p
1608e 42 74 2d 3e 6e 54 72 75 6e 63 3b 0a 20 20 7d 0a  Bt->nTrunc;.  }.
1608f 23 65 6e 64 69 66 0a 20 20 69 66 28 20 73 43 68  #endif.  if( sCh
16090 65 63 6b 2e 6e 50 61 67 65 3d 3d 30 20 29 7b 0a  eck.nPage==0 ){.
16091 20 20 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49      unlockBtreeI
16092 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
16093 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
16094 61 76 65 28 70 29 3b 0a 20 20 20 20 72 65 74 75  ave(p);.    retu
16095 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 43 68 65  rn 0;.  }.  sChe
16096 63 6b 2e 61 6e 52 65 66 20 3d 20 73 71 6c 69 74  ck.anRef = sqlit
16097 65 33 4d 61 6c 6c 6f 63 28 20 28 73 43 68 65 63  e3Malloc( (sChec
16098 6b 2e 6e 50 61 67 65 2b 31 29 2a 73 69 7a 65 6f  k.nPage+1)*sizeo
16099 66 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 5b 30  f(sCheck.anRef[0
1609a 5d 29 20 29 3b 0a 20 20 69 66 28 20 21 73 43 68  ]) );.  if( !sCh
1609b 65 63 6b 2e 61 6e 52 65 66 20 29 7b 0a 20 20 20  eck.anRef ){.   
1609c 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49 66 55 6e   unlockBtreeIfUn
1609d 75 73 65 64 28 70 42 74 29 3b 0a 20 20 20 20 2a  used(pBt);.    *
1609e 70 6e 45 72 72 20 3d 20 31 3b 0a 20 20 20 20 73  pnErr = 1;.    s
1609f 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65  qlite3BtreeLeave
160a0 28 70 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  (p);.    return 
160a1 30 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  0;.  }.  for(i=0
160a2 3b 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50 61 67  ; i<=sCheck.nPag
160a3 65 3b 20 69 2b 2b 29 7b 20 73 43 68 65 63 6b 2e  e; i++){ sCheck.
160a4 61 6e 52 65 66 5b 69 5d 20 3d 20 30 3b 20 7d 0a  anRef[i] = 0; }.
160a5 20 20 69 20 3d 20 50 45 4e 44 49 4e 47 5f 42 59    i = PENDING_BY
160a6 54 45 5f 50 41 47 45 28 70 42 74 29 3b 0a 20 20  TE_PAGE(pBt);.  
160a7 69 66 28 20 69 3c 3d 73 43 68 65 63 6b 2e 6e 50  if( i<=sCheck.nP
160a8 61 67 65 20 29 7b 0a 20 20 20 20 73 43 68 65 63  age ){.    sChec
160a9 6b 2e 61 6e 52 65 66 5b 69 5d 20 3d 20 31 3b 0a  k.anRef[i] = 1;.
160aa 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74 72    }.  sqlite3Str
160ab 41 63 63 75 6d 49 6e 69 74 28 26 73 43 68 65 63  AccumInit(&sChec
160ac 6b 2e 65 72 72 4d 73 67 2c 20 7a 45 72 72 2c 20  k.errMsg, zErr, 
160ad 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 32 30  sizeof(zErr), 20
160ae 30 30 30 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63  000);..  /* Chec
160af 6b 20 74 68 65 20 69 6e 74 65 67 72 69 74 79 20  k the integrity 
160b0 6f 66 20 74 68 65 20 66 72 65 65 6c 69 73 74 0a  of the freelist.
160b1 20 20 2a 2f 0a 20 20 63 68 65 63 6b 4c 69 73 74    */.  checkList
160b2 28 26 73 43 68 65 63 6b 2c 20 31 2c 20 67 65 74  (&sCheck, 1, get
160b3 34 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67  4byte(&pBt->pPag
160b4 65 31 2d 3e 61 44 61 74 61 5b 33 32 5d 29 2c 0a  e1->aData[32]),.
160b5 20 20 20 20 20 20 20 20 20 20 20 20 67 65 74 34              get4
160b6 62 79 74 65 28 26 70 42 74 2d 3e 70 50 61 67 65  byte(&pBt->pPage
160b7 31 2d 3e 61 44 61 74 61 5b 33 36 5d 29 2c 20 22  1->aData[36]), "
160b8 4d 61 69 6e 20 66 72 65 65 6c 69 73 74 3a 20 22  Main freelist: "
160b9 29 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 61  );..  /* Check a
160ba 6c 6c 20 74 68 65 20 74 61 62 6c 65 73 2e 0a 20  ll the tables.. 
160bb 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 28   */.  for(i=0; (
160bc 69 6e 74 29 69 3c 6e 52 6f 6f 74 20 26 26 20 73  int)i<nRoot && s
160bd 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b  Check.mxErr; i++
160be 29 7b 0a 20 20 20 20 69 66 28 20 61 52 6f 6f 74  ){.    if( aRoot
160bf 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75  [i]==0 ) continu
160c0 65 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  e;.#ifndef SQLIT
160c1 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55  E_OMIT_AUTOVACUU
160c2 4d 0a 20 20 20 20 69 66 28 20 70 42 74 2d 3e 61  M.    if( pBt->a
160c3 75 74 6f 56 61 63 75 75 6d 20 26 26 20 61 52 6f  utoVacuum && aRo
160c4 6f 74 5b 69 5d 3e 31 20 29 7b 0a 20 20 20 20 20  ot[i]>1 ){.     
160c5 20 63 68 65 63 6b 50 74 72 6d 61 70 28 26 73 43   checkPtrmap(&sC
160c6 68 65 63 6b 2c 20 61 52 6f 6f 74 5b 69 5d 2c 20  heck, aRoot[i], 
160c7 50 54 52 4d 41 50 5f 52 4f 4f 54 50 41 47 45 2c  PTRMAP_ROOTPAGE,
160c8 20 30 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 23 65   0, 0);.    }.#e
160c9 6e 64 69 66 0a 20 20 20 20 63 68 65 63 6b 54 72  ndif.    checkTr
160ca 65 65 50 61 67 65 28 26 73 43 68 65 63 6b 2c 20  eePage(&sCheck, 
160cb 61 52 6f 6f 74 5b 69 5d 2c 20 22 4c 69 73 74 20  aRoot[i], "List 
160cc 6f 66 20 74 72 65 65 20 72 6f 6f 74 73 3a 20 22  of tree roots: "
160cd 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b  );.  }..  /* Mak
160ce 65 20 73 75 72 65 20 65 76 65 72 79 20 70 61 67  e sure every pag
160cf 65 20 69 6e 20 74 68 65 20 66 69 6c 65 20 69 73  e in the file is
160d0 20 72 65 66 65 72 65 6e 63 65 64 0a 20 20 2a 2f   referenced.  */
160d1 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 73  .  for(i=1; i<=s
160d2 43 68 65 63 6b 2e 6e 50 61 67 65 20 26 26 20 73  Check.nPage && s
160d3 43 68 65 63 6b 2e 6d 78 45 72 72 3b 20 69 2b 2b  Check.mxErr; i++
160d4 29 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ){.#ifdef SQLITE
160d5 5f 4f 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d  _OMIT_AUTOVACUUM
160d6 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e  .    if( sCheck.
160d7 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 29 7b 0a 20  anRef[i]==0 ){. 
160d8 20 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64       checkAppend
160d9 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20  Msg(&sCheck, 0, 
160da 22 50 61 67 65 20 25 64 20 69 73 20 6e 65 76 65  "Page %d is neve
160db 72 20 75 73 65 64 22 2c 20 69 29 3b 0a 20 20 20  r used", i);.   
160dc 20 7d 0a 23 65 6c 73 65 0a 20 20 20 20 2f 2a 20   }.#else.    /* 
160dd 49 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20  If the database 
160de 73 75 70 70 6f 72 74 73 20 61 75 74 6f 2d 76 61  supports auto-va
160df 63 75 75 6d 2c 20 6d 61 6b 65 20 73 75 72 65 20  cuum, make sure 
160e0 6e 6f 20 74 61 62 6c 65 73 20 63 6f 6e 74 61 69  no tables contai
160e1 6e 0a 20 20 20 20 2a 2a 20 72 65 66 65 72 65 6e  n.    ** referen
160e2 63 65 73 20 74 6f 20 70 6f 69 6e 74 65 72 2d 6d  ces to pointer-m
160e3 61 70 20 70 61 67 65 73 2e 0a 20 20 20 20 2a 2f  ap pages..    */
160e4 0a 20 20 20 20 69 66 28 20 73 43 68 65 63 6b 2e  .    if( sCheck.
160e5 61 6e 52 65 66 5b 69 5d 3d 3d 30 20 26 26 20 0a  anRef[i]==0 && .
160e6 20 20 20 20 20 20 20 28 50 54 52 4d 41 50 5f 50         (PTRMAP_P
160e7 41 47 45 4e 4f 28 70 42 74 2c 20 69 29 21 3d 69  AGENO(pBt, i)!=i
160e8 20 7c 7c 20 21 70 42 74 2d 3e 61 75 74 6f 56 61   || !pBt->autoVa
160e9 63 75 75 6d 29 20 29 7b 0a 20 20 20 20 20 20 63  cuum) ){.      c
160ea 68 65 63 6b 41 70 70 65 6e 64 4d 73 67 28 26 73  heckAppendMsg(&s
160eb 43 68 65 63 6b 2c 20 30 2c 20 22 50 61 67 65 20  Check, 0, "Page 
160ec 25 64 20 69 73 20 6e 65 76 65 72 20 75 73 65 64  %d is never used
160ed 22 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ", i);.    }.   
160ee 20 69 66 28 20 73 43 68 65 63 6b 2e 61 6e 52 65   if( sCheck.anRe
160ef 66 5b 69 5d 21 3d 30 20 26 26 20 0a 20 20 20 20  f[i]!=0 && .    
160f0 20 20 20 28 50 54 52 4d 41 50 5f 50 41 47 45 4e     (PTRMAP_PAGEN
160f1 4f 28 70 42 74 2c 20 69 29 3d 3d 69 20 26 26 20  O(pBt, i)==i && 
160f2 70 42 74 2d 3e 61 75 74 6f 56 61 63 75 75 6d 29  pBt->autoVacuum)
160f3 20 29 7b 0a 20 20 20 20 20 20 63 68 65 63 6b 41   ){.      checkA
160f4 70 70 65 6e 64 4d 73 67 28 26 73 43 68 65 63 6b  ppendMsg(&sCheck
160f5 2c 20 30 2c 20 22 50 6f 69 6e 74 65 72 20 6d 61  , 0, "Pointer ma
160f6 70 20 70 61 67 65 20 25 64 20 69 73 20 72 65 66  p page %d is ref
160f7 65 72 65 6e 63 65 64 22 2c 20 69 29 3b 0a 20 20  erenced", i);.  
160f8 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a    }.#endif.  }..
160f9 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
160fa 68 69 73 20 61 6e 61 6c 79 73 69 73 20 64 69 64  his analysis did
160fb 20 6e 6f 74 20 6c 65 61 76 65 20 61 6e 79 20 75   not leave any u
160fc 6e 72 65 66 28 29 20 70 61 67 65 73 0a 20 20 2a  nref() pages.  *
160fd 2f 0a 20 20 75 6e 6c 6f 63 6b 42 74 72 65 65 49  /.  unlockBtreeI
160fe 66 55 6e 75 73 65 64 28 70 42 74 29 3b 0a 20 20  fUnused(pBt);.  
160ff 69 66 28 20 6e 52 65 66 20 21 3d 20 73 71 6c 69  if( nRef != sqli
16100 74 65 33 50 61 67 65 72 52 65 66 63 6f 75 6e 74  te3PagerRefcount
16101 28 70 42 74 2d 3e 70 50 61 67 65 72 29 20 29 7b  (pBt->pPager) ){
16102 0a 20 20 20 20 63 68 65 63 6b 41 70 70 65 6e 64  .    checkAppend
16103 4d 73 67 28 26 73 43 68 65 63 6b 2c 20 30 2c 20  Msg(&sCheck, 0, 
16104 0a 20 20 20 20 20 20 22 4f 75 74 73 74 61 6e 64  .      "Outstand
16105 69 6e 67 20 70 61 67 65 20 63 6f 75 6e 74 20 67  ing page count g
16106 6f 65 73 20 66 72 6f 6d 20 25 64 20 74 6f 20 25  oes from %d to %
16107 64 20 64 75 72 69 6e 67 20 74 68 69 73 20 61 6e  d during this an
16108 61 6c 79 73 69 73 22 2c 0a 20 20 20 20 20 20 6e  alysis",.      n
16109 52 65 66 2c 20 73 71 6c 69 74 65 33 50 61 67 65  Ref, sqlite3Page
1610a 72 52 65 66 63 6f 75 6e 74 28 70 42 74 2d 3e 70  rRefcount(pBt->p
1610b 50 61 67 65 72 29 0a 20 20 20 20 29 3b 0a 20 20  Pager).    );.  
1610c 7d 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 20 20 75  }..  /* Clean  u
1610d 70 20 61 6e 64 20 72 65 70 6f 72 74 20 65 72 72  p and report err
1610e 6f 72 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  ors..  */.  sqli
1610f 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29  te3BtreeLeave(p)
16110 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
16111 28 73 43 68 65 63 6b 2e 61 6e 52 65 66 29 3b 0a  (sCheck.anRef);.
16112 20 20 69 66 28 20 73 43 68 65 63 6b 2e 6d 61 6c    if( sCheck.mal
16113 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
16114 20 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d   sqlite3StrAccum
16115 52 65 73 65 74 28 26 73 43 68 65 63 6b 2e 65 72  Reset(&sCheck.er
16116 72 4d 73 67 29 3b 0a 20 20 20 20 2a 70 6e 45 72  rMsg);.    *pnEr
16117 72 20 3d 20 73 43 68 65 63 6b 2e 6e 45 72 72 2b  r = sCheck.nErr+
16118 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  1;.    return 0;
16119 0a 20 20 7d 0a 20 20 2a 70 6e 45 72 72 20 3d 20  .  }.  *pnErr = 
1611a 73 43 68 65 63 6b 2e 6e 45 72 72 3b 0a 20 20 69  sCheck.nErr;.  i
1611b 66 28 20 73 43 68 65 63 6b 2e 6e 45 72 72 3d 3d  f( sCheck.nErr==
1611c 30 20 29 20 73 71 6c 69 74 65 33 53 74 72 41 63  0 ) sqlite3StrAc
1611d 63 75 6d 52 65 73 65 74 28 26 73 43 68 65 63 6b  cumReset(&sCheck
1611e 2e 65 72 72 4d 73 67 29 3b 0a 20 20 72 65 74 75  .errMsg);.  retu
1611f 72 6e 20 73 71 6c 69 74 65 33 53 74 72 41 63 63  rn sqlite3StrAcc
16120 75 6d 46 69 6e 69 73 68 28 26 73 43 68 65 63 6b  umFinish(&sCheck
16121 2e 65 72 72 4d 73 67 29 3b 0a 7d 0a 23 65 6e 64  .errMsg);.}.#end
16122 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
16123 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
16124 4b 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  K */../*.** Retu
16125 72 6e 20 74 68 65 20 66 75 6c 6c 20 70 61 74 68  rn the full path
16126 6e 61 6d 65 20 6f 66 20 74 68 65 20 75 6e 64 65  name of the unde
16127 72 6c 79 69 6e 67 20 64 61 74 61 62 61 73 65 20  rlying database 
16128 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  file..**.** The 
16129 70 61 67 65 72 20 66 69 6c 65 6e 61 6d 65 20 69  pager filename i
1612a 73 20 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c  s invariant as l
1612b 6f 6e 67 20 61 73 20 74 68 65 20 70 61 67 65 72  ong as the pager
1612c 20 69 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69   is.** open so i
1612d 74 20 69 73 20 73 61 66 65 20 74 6f 20 61 63 63  t is safe to acc
1612e 65 73 73 20 77 69 74 68 6f 75 74 20 74 68 65 20  ess without the 
1612f 42 74 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a  BtShared mutex..
16130 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16131 45 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71  E const char *sq
16132 6c 69 74 65 33 42 74 72 65 65 47 65 74 46 69 6c  lite3BtreeGetFil
16133 65 6e 61 6d 65 28 42 74 72 65 65 20 2a 70 29 7b  ename(Btree *p){
16134 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 42  .  assert( p->pB
16135 74 2d 3e 70 50 61 67 65 72 21 3d 30 20 29 3b 0a  t->pPager!=0 );.
16136 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
16137 50 61 67 65 72 46 69 6c 65 6e 61 6d 65 28 70 2d  PagerFilename(p-
16138 3e 70 42 74 2d 3e 70 50 61 67 65 72 29 3b 0a 7d  >pBt->pPager);.}
16139 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1613a 68 65 20 70 61 74 68 6e 61 6d 65 20 6f 66 20 74  he pathname of t
1613b 68 65 20 64 69 72 65 63 74 6f 72 79 20 74 68 61  he directory tha
1613c 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 64  t contains the d
1613d 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a  atabase file..**
1613e 0a 2a 2a 20 54 68 65 20 70 61 67 65 72 20 64 69  .** The pager di
1613f 72 65 63 74 6f 72 79 20 6e 61 6d 65 20 69 73 20  rectory name is 
16140 69 6e 76 61 72 69 61 6e 74 20 61 73 20 6c 6f 6e  invariant as lon
16141 67 20 61 73 20 74 68 65 20 70 61 67 65 72 20 69  g as the pager i
16142 73 0a 2a 2a 20 6f 70 65 6e 20 73 6f 20 69 74 20  s.** open so it 
16143 69 73 20 73 61 66 65 20 74 6f 20 61 63 63 65 73  is safe to acces
16144 73 20 77 69 74 68 6f 75 74 20 74 68 65 20 42 74  s without the Bt
16145 53 68 61 72 65 64 20 6d 75 74 65 78 2e 0a 2a 2f  Shared mutex..*/
16146 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16147 63 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69  const char *sqli
16148 74 65 33 42 74 72 65 65 47 65 74 44 69 72 6e 61  te3BtreeGetDirna
16149 6d 65 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  me(Btree *p){.  
1614a 61 73 73 65 72 74 28 20 70 2d 3e 70 42 74 2d 3e  assert( p->pBt->
1614b 70 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 72  pPager!=0 );.  r
1614c 65 74 75 72 6e 20 73 71 6c 69 74 65 33 50 61 67  eturn sqlite3Pag
1614d 65 72 44 69 72 6e 61 6d 65 28 70 2d 3e 70 42 74  erDirname(p->pBt
1614e 2d 3e 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 2f 2a  ->pPager);.}../*
1614f 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70  .** Return the p
16150 61 74 68 6e 61 6d 65 20 6f 66 20 74 68 65 20 6a  athname of the j
16151 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 6f 72 20  ournal file for 
16152 74 68 69 73 20 64 61 74 61 62 61 73 65 2e 20 54  this database. T
16153 68 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c  he return.** val
16154 75 65 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69  ue of this routi
16155 6e 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 72  ne is the same r
16156 65 67 61 72 64 6c 65 73 73 20 6f 66 20 77 68 65  egardless of whe
16157 74 68 65 72 20 74 68 65 20 6a 6f 75 72 6e 61 6c  ther the journal
16158 20 66 69 6c 65 0a 2a 2a 20 68 61 73 20 62 65 65   file.** has bee
16159 6e 20 63 72 65 61 74 65 64 20 6f 72 20 6e 6f 74  n created or not
1615a 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 67 65  ..**.** The page
1615b 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 6e 61  r journal filena
1615c 6d 65 20 69 73 20 69 6e 76 61 72 69 61 6e 74 20  me is invariant 
1615d 61 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 70  as long as the p
1615e 61 67 65 72 20 69 73 0a 2a 2a 20 6f 70 65 6e 20  ager is.** open 
1615f 73 6f 20 69 74 20 69 73 20 73 61 66 65 20 74 6f  so it is safe to
16160 20 61 63 63 65 73 73 20 77 69 74 68 6f 75 74 20   access without 
16161 74 68 65 20 42 74 53 68 61 72 65 64 20 6d 75 74  the BtShared mut
16162 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
16163 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
16164 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 47 65   *sqlite3BtreeGe
16165 74 4a 6f 75 72 6e 61 6c 6e 61 6d 65 28 42 74 72  tJournalname(Btr
16166 65 65 20 2a 70 29 7b 0a 20 20 61 73 73 65 72 74  ee *p){.  assert
16167 28 20 70 2d 3e 70 42 74 2d 3e 70 50 61 67 65 72  ( p->pBt->pPager
16168 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  !=0 );.  return 
16169 73 71 6c 69 74 65 33 50 61 67 65 72 4a 6f 75 72  sqlite3PagerJour
1616a 6e 61 6c 6e 61 6d 65 28 70 2d 3e 70 42 74 2d 3e  nalname(p->pBt->
1616b 70 50 61 67 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e  pPager);.}..#ifn
1616c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1616d 56 41 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 43 6f 70  VACUUM./*.** Cop
1616e 79 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 63  y the complete c
1616f 6f 6e 74 65 6e 74 20 6f 66 20 70 42 74 46 72 6f  ontent of pBtFro
16170 6d 20 69 6e 74 6f 20 70 42 74 54 6f 2e 20 20 41  m into pBtTo.  A
16171 20 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2a 20   transaction.** 
16172 6d 75 73 74 20 62 65 20 61 63 74 69 76 65 20 66  must be active f
16173 6f 72 20 62 6f 74 68 20 66 69 6c 65 73 2e 0a 2a  or both files..*
16174 2a 0a 2a 2a 20 54 68 65 20 73 69 7a 65 20 6f 66  *.** The size of
16175 20 66 69 6c 65 20 70 54 6f 20 6d 61 79 20 62 65   file pTo may be
16176 20 72 65 64 75 63 65 64 20 62 79 20 74 68 69 73   reduced by this
16177 20 6f 70 65 72 61 74 69 6f 6e 2e 0a 2a 2a 20 49   operation..** I
16178 66 20 61 6e 79 74 68 69 6e 67 20 67 6f 65 73 20  f anything goes 
16179 77 72 6f 6e 67 2c 20 74 68 65 20 74 72 61 6e 73  wrong, the trans
1617a 61 63 74 69 6f 6e 20 6f 6e 20 70 54 6f 20 69 73  action on pTo is
1617b 20 72 6f 6c 6c 65 64 20 62 61 63 6b 2e 20 0a 2a   rolled back. .*
1617c 2a 0a 2a 2a 20 49 66 20 73 75 63 63 65 73 73 66  *.** If successf
1617d 75 6c 2c 20 43 6f 6d 6d 69 74 50 68 61 73 65 4f  ul, CommitPhaseO
1617e 6e 65 28 29 20 6d 61 79 20 62 65 20 63 61 6c 6c  ne() may be call
1617f 65 64 20 6f 6e 20 70 54 6f 20 62 65 66 6f 72 65  ed on pTo before
16180 20 72 65 74 75 72 6e 69 6e 67 2e 20 0a 2a 2a 20   returning. .** 
16181 54 68 65 20 63 61 6c 6c 65 72 20 73 68 6f 75 6c  The caller shoul
16182 64 20 66 69 6e 69 73 68 20 63 6f 6d 6d 69 74 74  d finish committ
16183 69 6e 67 20 74 68 65 20 74 72 61 6e 73 61 63 74  ing the transact
16184 69 6f 6e 20 6f 6e 20 70 54 6f 20 62 79 20 63 61  ion on pTo by ca
16185 6c 6c 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33  lling.** sqlite3
16186 42 74 72 65 65 43 6f 6d 6d 69 74 28 29 2e 0a 2a  BtreeCommit()..*
16187 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 74 72  /.static int btr
16188 65 65 43 6f 70 79 46 69 6c 65 28 42 74 72 65 65  eeCopyFile(Btree
16189 20 2a 70 54 6f 2c 20 42 74 72 65 65 20 2a 70 46   *pTo, Btree *pF
1618a 72 6f 6d 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  rom){.  int rc =
1618b 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 50 67   SQLITE_OK;.  Pg
1618c 6e 6f 20 69 3b 0a 0a 20 20 50 67 6e 6f 20 6e 46  no i;..  Pgno nF
1618d 72 6f 6d 50 61 67 65 3b 20 20 20 20 20 2f 2a 20  romPage;     /* 
1618e 4e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20  Number of pages 
1618f 69 6e 20 70 46 72 6f 6d 20 2a 2f 0a 20 20 50 67  in pFrom */.  Pg
16190 6e 6f 20 6e 54 6f 50 61 67 65 3b 20 20 20 20 20  no nToPage;     
16191 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 70    /* Number of p
16192 61 67 65 73 20 69 6e 20 70 54 6f 20 2a 2f 0a 20  ages in pTo */. 
16193 20 50 67 6e 6f 20 6e 4e 65 77 50 61 67 65 3b 20   Pgno nNewPage; 
16194 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
16195 66 20 70 61 67 65 73 20 69 6e 20 70 54 6f 20 61  f pages in pTo a
16196 66 74 65 72 20 74 68 65 20 63 6f 70 79 20 2a 2f  fter the copy */
16197 0a 0a 20 20 50 67 6e 6f 20 69 53 6b 69 70 3b 20  ..  Pgno iSkip; 
16198 20 20 20 20 20 20 20 20 2f 2a 20 50 65 6e 64 69          /* Pendi
16199 6e 67 20 62 79 74 65 20 70 61 67 65 20 69 6e 20  ng byte page in 
1619a 70 54 6f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 6f  pTo */.  int nTo
1619b 50 61 67 65 53 69 7a 65 3b 20 20 20 20 2f 2a 20  PageSize;    /* 
1619c 50 61 67 65 20 73 69 7a 65 20 6f 66 20 70 54 6f  Page size of pTo
1619d 20 69 6e 20 62 79 74 65 73 20 2a 2f 0a 20 20 69   in bytes */.  i
1619e 6e 74 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65  nt nFromPageSize
1619f 3b 20 20 2f 2a 20 50 61 67 65 20 73 69 7a 65 20  ;  /* Page size 
161a0 6f 66 20 70 46 72 6f 6d 20 69 6e 20 62 79 74 65  of pFrom in byte
161a1 73 20 2a 2f 0a 0a 20 20 42 74 53 68 61 72 65 64  s */..  BtShared
161a2 20 2a 70 42 74 54 6f 20 3d 20 70 54 6f 2d 3e 70   *pBtTo = pTo->p
161a3 42 74 3b 0a 20 20 42 74 53 68 61 72 65 64 20 2a  Bt;.  BtShared *
161a4 70 42 74 46 72 6f 6d 20 3d 20 70 46 72 6f 6d 2d  pBtFrom = pFrom-
161a5 3e 70 42 74 3b 0a 20 20 70 42 74 54 6f 2d 3e 64  >pBt;.  pBtTo->d
161a6 62 20 3d 20 70 54 6f 2d 3e 64 62 3b 0a 20 20 70  b = pTo->db;.  p
161a7 42 74 46 72 6f 6d 2d 3e 64 62 20 3d 20 70 46 72  BtFrom->db = pFr
161a8 6f 6d 2d 3e 64 62 3b 0a 0a 20 20 6e 54 6f 50 61  om->db;..  nToPa
161a9 67 65 53 69 7a 65 20 3d 20 70 42 74 54 6f 2d 3e  geSize = pBtTo->
161aa 70 61 67 65 53 69 7a 65 3b 0a 20 20 6e 46 72 6f  pageSize;.  nFro
161ab 6d 50 61 67 65 53 69 7a 65 20 3d 20 70 42 74 46  mPageSize = pBtF
161ac 72 6f 6d 2d 3e 70 61 67 65 53 69 7a 65 3b 0a 0a  rom->pageSize;..
161ad 20 20 69 66 28 20 70 54 6f 2d 3e 69 6e 54 72 61    if( pTo->inTra
161ae 6e 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20  ns!=TRANS_WRITE 
161af 7c 7c 20 70 46 72 6f 6d 2d 3e 69 6e 54 72 61 6e  || pFrom->inTran
161b0 73 21 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29  s!=TRANS_WRITE )
161b1 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
161b2 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
161b3 20 69 66 28 20 70 42 74 54 6f 2d 3e 70 43 75 72   if( pBtTo->pCur
161b4 73 6f 72 20 29 7b 0a 20 20 20 20 72 65 74 75 72  sor ){.    retur
161b5 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
161b6 20 7d 0a 0a 20 20 6e 54 6f 50 61 67 65 20 3d 20   }..  nToPage = 
161b7 70 61 67 65 72 50 61 67 65 63 6f 75 6e 74 28 70  pagerPagecount(p
161b8 42 74 54 6f 29 3b 0a 20 20 6e 46 72 6f 6d 50 61  BtTo);.  nFromPa
161b9 67 65 20 3d 20 70 61 67 65 72 50 61 67 65 63 6f  ge = pagerPageco
161ba 75 6e 74 28 70 42 74 46 72 6f 6d 29 3b 0a 20 20  unt(pBtFrom);.  
161bb 69 53 6b 69 70 20 3d 20 50 45 4e 44 49 4e 47 5f  iSkip = PENDING_
161bc 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f 29  BYTE_PAGE(pBtTo)
161bd 3b 0a 0a 20 20 2f 2a 20 56 61 72 69 61 62 6c 65  ;..  /* Variable
161be 20 6e 4e 65 77 50 61 67 65 20 69 73 20 74 68 65   nNewPage is the
161bf 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
161c0 20 72 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f   required to sto
161c1 72 65 20 74 68 65 0a 20 20 2a 2a 20 63 6f 6e 74  re the.  ** cont
161c2 65 6e 74 73 20 6f 66 20 70 46 72 6f 6d 20 75 73  ents of pFrom us
161c3 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
161c4 70 61 67 65 2d 73 69 7a 65 20 6f 66 20 70 54 6f  page-size of pTo
161c5 2e 0a 20 20 2a 2f 0a 20 20 6e 4e 65 77 50 61 67  ..  */.  nNewPag
161c6 65 20 3d 20 28 28 69 36 34 29 6e 46 72 6f 6d 50  e = ((i64)nFromP
161c7 61 67 65 20 2a 20 28 69 36 34 29 6e 46 72 6f 6d  age * (i64)nFrom
161c8 50 61 67 65 53 69 7a 65 20 2b 20 28 69 36 34 29  PageSize + (i64)
161c9 6e 54 6f 50 61 67 65 53 69 7a 65 20 2d 20 31 29  nToPageSize - 1)
161ca 20 2f 20 0a 20 20 20 20 20 20 28 69 36 34 29 6e   / .      (i64)n
161cb 54 6f 50 61 67 65 53 69 7a 65 3b 0a 0a 20 20 66  ToPageSize;..  f
161cc 6f 72 28 69 3d 31 3b 20 72 63 3d 3d 53 51 4c 49  or(i=1; rc==SQLI
161cd 54 45 5f 4f 4b 20 26 26 20 28 69 3c 3d 6e 54 6f  TE_OK && (i<=nTo
161ce 50 61 67 65 20 7c 7c 20 69 3c 3d 6e 4e 65 77 50  Page || i<=nNewP
161cf 61 67 65 29 3b 20 69 2b 2b 29 7b 0a 0a 20 20 20  age); i++){..   
161d0 20 2f 2a 20 4a 6f 75 72 6e 61 6c 20 74 68 65 20   /* Journal the 
161d1 6f 72 69 67 69 6e 61 6c 20 70 61 67 65 2e 0a 20  original page.. 
161d2 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 69 53 6b     **.    ** iSk
161d3 69 70 20 69 73 20 74 68 65 20 70 61 67 65 20 6e  ip is the page n
161d4 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6c 6f 63  umber of the loc
161d5 6b 69 6e 67 20 70 61 67 65 20 28 50 45 4e 44 49  king page (PENDI
161d6 4e 47 5f 42 59 54 45 5f 50 41 47 45 29 0a 20 20  NG_BYTE_PAGE).  
161d7 20 20 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65    ** in database
161d8 20 2a 70 54 6f 20 28 62 65 66 6f 72 65 20 74 68   *pTo (before th
161d9 65 20 63 6f 70 79 29 2e 20 54 68 69 73 20 70 61  e copy). This pa
161da 67 65 20 69 73 20 6e 65 76 65 72 20 77 72 69 74  ge is never writ
161db 74 65 6e 20 0a 20 20 20 20 2a 2a 20 69 6e 74 6f  ten .    ** into
161dc 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c   the journal fil
161dd 65 2e 20 55 6e 6c 65 73 73 20 69 3d 3d 69 53 6b  e. Unless i==iSk
161de 69 70 20 6f 72 20 74 68 65 20 70 61 67 65 20 77  ip or the page w
161df 61 73 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 70 72  as not.    ** pr
161e0 65 73 65 6e 74 20 69 6e 20 70 54 6f 20 62 65 66  esent in pTo bef
161e1 6f 72 65 20 74 68 65 20 63 6f 70 79 20 6f 70 65  ore the copy ope
161e2 72 61 74 69 6f 6e 2c 20 6a 6f 75 72 6e 61 6c 20  ration, journal 
161e3 70 61 67 65 20 69 20 66 72 6f 6d 20 70 54 6f 2e  page i from pTo.
161e4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
161e5 69 21 3d 69 53 6b 69 70 20 26 26 20 69 3c 3d 6e  i!=iSkip && i<=n
161e6 54 6f 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20  ToPage ){.      
161e7 44 62 50 61 67 65 20 2a 70 44 62 50 61 67 65 20  DbPage *pDbPage 
161e8 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  = 0;.      rc = 
161e9 73 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 28  sqlite3PagerGet(
161ea 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20 69  pBtTo->pPager, i
161eb 2c 20 26 70 44 62 50 61 67 65 29 3b 0a 20 20 20  , &pDbPage);.   
161ec 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
161ed 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20  E_OK ){.        
161ee 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  rc = sqlite3Page
161ef 72 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b  rWrite(pDbPage);
161f0 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d  .        if( rc=
161f1 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3e  =SQLITE_OK && i>
161f2 6e 46 72 6f 6d 50 61 67 65 20 29 7b 0a 20 20 20  nFromPage ){.   
161f3 20 20 20 20 20 20 20 2f 2a 20 59 65 61 68 2e 20         /* Yeah. 
161f4 20 49 74 20 73 65 65 6d 73 20 77 69 65 72 64 20   It seems wierd 
161f5 74 6f 20 63 61 6c 6c 20 44 6f 6e 74 57 72 69 74  to call DontWrit
161f6 65 28 29 20 72 69 67 68 74 20 61 66 74 65 72 20  e() right after 
161f7 57 72 69 74 65 28 29 2e 20 42 75 74 0a 20 20 20  Write(). But.   
161f8 20 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 69         ** that i
161f9 73 20 62 65 63 61 75 73 65 20 74 68 65 20 6e 61  s because the na
161fa 6d 65 73 20 6f 66 20 74 68 6f 73 65 20 70 72 6f  mes of those pro
161fb 63 65 64 75 72 65 73 20 64 6f 20 6e 6f 74 20 65  cedures do not e
161fc 78 61 63 74 6c 79 20 0a 20 20 20 20 20 20 20 20  xactly .        
161fd 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74 20 77    ** represent w
161fe 68 61 74 20 74 68 65 79 20 64 6f 2e 20 20 57 72  hat they do.  Wr
161ff 69 74 65 28 29 20 72 65 61 6c 6c 79 20 6d 65 61  ite() really mea
16200 6e 73 20 22 70 75 74 20 74 68 69 73 20 70 61 67  ns "put this pag
16201 65 20 69 6e 20 74 68 65 0a 20 20 20 20 20 20 20  e in the.       
16202 20 20 20 2a 2a 20 72 6f 6c 6c 62 61 63 6b 20 6a     ** rollback j
16203 6f 75 72 6e 61 6c 20 61 6e 64 20 6d 61 72 6b 20  ournal and mark 
16204 69 74 20 61 73 20 64 69 72 74 79 20 73 6f 20 74  it as dirty so t
16205 68 61 74 20 69 74 20 77 69 6c 6c 20 62 65 20 77  hat it will be w
16206 72 69 74 74 65 6e 0a 20 20 20 20 20 20 20 20 20  ritten.         
16207 20 2a 2a 20 74 6f 20 74 68 65 20 64 61 74 61 62   ** to the datab
16208 61 73 65 20 66 69 6c 65 20 6c 61 74 65 72 2e 22  ase file later."
16209 20 20 44 6f 6e 74 57 72 69 74 65 28 29 20 75 6e    DontWrite() un
1620a 64 6f 65 73 20 74 68 65 20 73 65 63 6f 6e 64 20  does the second 
1620b 70 61 72 74 20 6f 66 0a 20 20 20 20 20 20 20 20  part of.        
1620c 20 20 2a 2a 20 74 68 61 74 20 61 6e 64 20 70 72    ** that and pr
1620d 65 76 65 6e 74 73 20 74 68 65 20 70 61 67 65 20  events the page 
1620e 66 72 6f 6d 20 62 65 69 6e 67 20 77 72 69 74 74  from being writt
1620f 65 6e 20 74 6f 20 74 68 65 20 64 61 74 61 62 61  en to the databa
16210 73 65 2e 20 54 68 65 0a 20 20 20 20 20 20 20 20  se. The.        
16211 20 20 2a 2a 20 70 61 67 65 20 69 73 20 73 74 69    ** page is sti
16212 6c 6c 20 6f 6e 20 74 68 65 20 72 6f 6c 6c 62 61  ll on the rollba
16213 63 6b 20 6a 6f 75 72 6e 61 6c 2c 20 74 68 6f 75  ck journal, thou
16214 67 68 2e 20 20 41 6e 64 20 74 68 61 74 20 69 73  gh.  And that is
16215 20 74 68 65 20 0a 20 20 20 20 20 20 20 20 20 20   the .          
16216 2a 2a 20 77 68 6f 6c 65 20 70 6f 69 6e 74 20 6f  ** whole point o
16217 66 20 74 68 69 73 20 62 6c 6f 63 6b 3a 20 74 6f  f this block: to
16218 20 70 75 74 20 70 61 67 65 73 20 6f 6e 20 74 68   put pages on th
16219 65 20 72 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e  e rollback journ
1621a 61 6c 2e 20 0a 20 20 20 20 20 20 20 20 20 20 2a  al. .          *
1621b 2f 0a 20 20 20 20 20 20 20 20 20 20 72 63 20 3d  /.          rc =
1621c 20 73 71 6c 69 74 65 33 50 61 67 65 72 44 6f 6e   sqlite3PagerDon
1621d 74 57 72 69 74 65 28 70 44 62 50 61 67 65 29 3b  tWrite(pDbPage);
1621e 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1621f 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55     sqlite3PagerU
16220 6e 72 65 66 28 70 44 62 50 61 67 65 29 3b 0a 20  nref(pDbPage);. 
16221 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
16222 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65 20 74    /* Overwrite t
16223 68 65 20 64 61 74 61 20 69 6e 20 70 61 67 65 20  he data in page 
16224 69 20 6f 66 20 74 68 65 20 74 61 72 67 65 74 20  i of the target 
16225 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20 20 20  database */.    
16226 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
16227 4b 20 26 26 20 69 21 3d 69 53 6b 69 70 20 26 26  K && i!=iSkip &&
16228 20 69 3c 3d 6e 4e 65 77 50 61 67 65 20 29 7b 0a   i<=nNewPage ){.
16229 0a 20 20 20 20 20 20 44 62 50 61 67 65 20 2a 70  .      DbPage *p
1622a 54 6f 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20  ToPage = 0;.    
1622b 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20    sqlite3_int64 
1622c 69 4f 66 66 3b 0a 0a 20 20 20 20 20 20 72 63 20  iOff;..      rc 
1622d 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 47 65  = sqlite3PagerGe
1622e 74 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c  t(pBtTo->pPager,
1622f 20 69 2c 20 26 70 54 6f 50 61 67 65 29 3b 0a 20   i, &pToPage);. 
16230 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
16231 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16232 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16233 67 65 72 57 72 69 74 65 28 70 54 6f 50 61 67 65  gerWrite(pToPage
16234 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
16235 20 20 66 6f 72 28 0a 20 20 20 20 20 20 20 20 69    for(.        i
16236 4f 66 66 3d 28 69 2d 31 29 2a 6e 54 6f 50 61 67  Off=(i-1)*nToPag
16237 65 53 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20  eSize; .        
16238 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
16239 20 69 4f 66 66 3c 69 2a 6e 54 6f 50 61 67 65 53   iOff<i*nToPageS
1623a 69 7a 65 3b 20 0a 20 20 20 20 20 20 20 20 69 4f  ize; .        iO
1623b 66 66 20 2b 3d 20 6e 46 72 6f 6d 50 61 67 65 53  ff += nFromPageS
1623c 69 7a 65 0a 20 20 20 20 20 20 29 7b 0a 20 20 20  ize.      ){.   
1623d 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 46 72       DbPage *pFr
1623e 6f 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20  omPage = 0;.    
1623f 20 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d 20 3d      Pgno iFrom =
16240 20 28 69 4f 66 66 2f 6e 46 72 6f 6d 50 61 67 65   (iOff/nFromPage
16241 53 69 7a 65 29 2b 31 3b 0a 0a 20 20 20 20 20 20  Size)+1;..      
16242 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 50 45 4e    if( iFrom==PEN
16243 44 49 4e 47 5f 42 59 54 45 5f 50 41 47 45 28 70  DING_BYTE_PAGE(p
16244 42 74 46 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20  BtFrom) ){.     
16245 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
16246 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
16247 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61    rc = sqlite3Pa
16248 67 65 72 47 65 74 28 70 42 74 46 72 6f 6d 2d 3e  gerGet(pBtFrom->
16249 70 50 61 67 65 72 2c 20 69 46 72 6f 6d 2c 20 26  pPager, iFrom, &
1624a 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20 20  pFromPage);.    
1624b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1624c 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
1624d 20 20 20 63 68 61 72 20 2a 7a 54 6f 20 3d 20 73     char *zTo = s
1624e 71 6c 69 74 65 33 50 61 67 65 72 47 65 74 44 61  qlite3PagerGetDa
1624f 74 61 28 70 54 6f 50 61 67 65 29 3b 0a 20 20 20  ta(pToPage);.   
16250 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46 72         char *zFr
16251 6f 6d 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65  om = sqlite3Page
16252 72 47 65 74 44 61 74 61 28 70 46 72 6f 6d 50 61  rGetData(pFromPa
16253 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ge);.          i
16254 6e 74 20 6e 43 6f 70 79 3b 0a 0a 20 20 20 20 20  nt nCopy;..     
16255 20 20 20 20 20 69 66 28 20 6e 46 72 6f 6d 50 61       if( nFromPa
16256 67 65 53 69 7a 65 3e 3d 6e 54 6f 50 61 67 65 53  geSize>=nToPageS
16257 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ize ){.         
16258 20 20 20 7a 46 72 6f 6d 20 2b 3d 20 28 28 69 2d     zFrom += ((i-
16259 31 29 2a 6e 54 6f 50 61 67 65 53 69 7a 65 20 2d  1)*nToPageSize -
1625a 20 28 28 69 46 72 6f 6d 2d 31 29 2a 6e 46 72 6f   ((iFrom-1)*nFro
1625b 6d 50 61 67 65 53 69 7a 65 29 29 3b 0a 20 20 20  mPageSize));.   
1625c 20 20 20 20 20 20 20 20 20 6e 43 6f 70 79 20 3d           nCopy =
1625d 20 6e 54 6f 50 61 67 65 53 69 7a 65 3b 0a 20 20   nToPageSize;.  
1625e 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
1625f 20 20 20 20 20 20 20 20 20 20 20 7a 54 6f 20 2b             zTo +
16260 3d 20 28 28 28 69 46 72 6f 6d 2d 31 29 2a 6e 46  = (((iFrom-1)*nF
16261 72 6f 6d 50 61 67 65 53 69 7a 65 29 20 2d 20 28  romPageSize) - (
16262 69 2d 31 29 2a 6e 54 6f 50 61 67 65 53 69 7a 65  i-1)*nToPageSize
16263 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e  );.            n
16264 43 6f 70 79 20 3d 20 6e 46 72 6f 6d 50 61 67 65  Copy = nFromPage
16265 53 69 7a 65 3b 0a 20 20 20 20 20 20 20 20 20 20  Size;.          
16266 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  }..          mem
16267 63 70 79 28 7a 54 6f 2c 20 7a 46 72 6f 6d 2c 20  cpy(zTo, zFrom, 
16268 6e 43 6f 70 79 29 3b 0a 20 20 20 20 20 20 20 20  nCopy);.        
16269 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 55 6e    sqlite3PagerUn
1626a 72 65 66 28 70 46 72 6f 6d 50 61 67 65 29 3b 0a  ref(pFromPage);.
1626b 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1626c 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 70 54 6f  }..      if( pTo
1626d 50 61 67 65 20 29 7b 0a 20 20 20 20 20 20 20 20  Page ){.        
1626e 4d 65 6d 50 61 67 65 20 2a 70 20 3d 20 28 4d 65  MemPage *p = (Me
1626f 6d 50 61 67 65 20 2a 29 73 71 6c 69 74 65 33 50  mPage *)sqlite3P
16270 61 67 65 72 47 65 74 45 78 74 72 61 28 70 54 6f  agerGetExtra(pTo
16271 50 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 70  Page);.        p
16272 2d 3e 69 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20  ->isInit = 0;.  
16273 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
16274 65 72 55 6e 72 65 66 28 70 54 6f 50 61 67 65 29  erUnref(pToPage)
16275 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
16276 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69    }..  /* If thi
16277 6e 67 73 20 68 61 76 65 20 77 6f 72 6b 65 64 20  ngs have worked 
16278 73 6f 20 66 61 72 2c 20 74 68 65 20 64 61 74 61  so far, the data
16279 62 61 73 65 20 66 69 6c 65 20 6d 61 79 20 6e 65  base file may ne
1627a 65 64 20 74 6f 20 62 65 20 0a 20 20 2a 2a 20 74  ed to be .  ** t
1627b 72 75 6e 63 61 74 65 64 2e 20 54 68 65 20 63 6f  runcated. The co
1627c 6d 70 6c 65 78 20 70 61 72 74 20 69 73 20 74 68  mplex part is th
1627d 61 74 20 69 74 20 6d 61 79 20 6e 65 65 64 20 74  at it may need t
1627e 6f 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74  o be truncated t
1627f 6f 0a 20 20 2a 2a 20 61 20 73 69 7a 65 20 74 68  o.  ** a size th
16280 61 74 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74  at is not an int
16281 65 67 65 72 20 6d 75 6c 74 69 70 6c 65 20 6f 66  eger multiple of
16282 20 6e 54 6f 50 61 67 65 53 69 7a 65 20 2d 20 74   nToPageSize - t
16283 68 65 20 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20  he current.  ** 
16284 70 61 67 65 20 73 69 7a 65 20 75 73 65 64 20 62  page size used b
16285 79 20 74 68 65 20 70 61 67 65 72 20 61 73 73 6f  y the pager asso
16286 63 69 61 74 65 64 20 77 69 74 68 20 42 2d 54 72  ciated with B-Tr
16287 65 65 20 70 54 6f 2e 0a 20 20 2a 2a 0a 20 20 2a  ee pTo..  **.  *
16288 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 73  * For example, s
16289 61 79 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65  ay the page-size
1628a 20 6f 66 20 70 54 6f 20 69 73 20 32 30 34 38 20   of pTo is 2048 
1628b 62 79 74 65 73 20 61 6e 64 20 74 68 65 20 6f 72  bytes and the or
1628c 69 67 69 6e 61 6c 20 0a 20 20 2a 2a 20 6e 75 6d  iginal .  ** num
1628d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 73 20  ber of pages is 
1628e 35 20 28 31 30 20 4b 42 20 66 69 6c 65 29 2e 20  5 (10 KB file). 
1628f 49 66 20 70 46 72 6f 6d 20 68 61 73 20 61 20 70  If pFrom has a p
16290 61 67 65 20 73 69 7a 65 20 6f 66 20 31 30 32 34  age size of 1024
16291 20 0a 20 20 2a 2a 20 62 79 74 65 73 20 61 6e 64   .  ** bytes and
16292 20 39 20 70 61 67 65 73 2c 20 74 68 65 6e 20 74   9 pages, then t
16293 68 65 20 66 69 6c 65 20 6e 65 65 64 73 20 74 6f  he file needs to
16294 20 62 65 20 74 72 75 6e 63 61 74 65 64 20 74 6f   be truncated to
16295 20 39 4b 42 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   9KB..  */.  if(
16296 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
16297 7b 0a 20 20 20 20 69 66 28 20 6e 46 72 6f 6d 50  {.    if( nFromP
16298 61 67 65 53 69 7a 65 21 3d 6e 54 6f 50 61 67 65  ageSize!=nToPage
16299 53 69 7a 65 20 29 7b 0a 20 20 20 20 20 20 73 71  Size ){.      sq
1629a 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 46 69 6c  lite3_file *pFil
1629b 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
1629c 46 69 6c 65 28 70 42 74 54 6f 2d 3e 70 50 61 67  File(pBtTo->pPag
1629d 65 72 29 3b 0a 20 20 20 20 20 20 69 36 34 20 69  er);.      i64 i
1629e 53 69 7a 65 20 3d 20 28 69 36 34 29 6e 46 72 6f  Size = (i64)nFro
1629f 6d 50 61 67 65 53 69 7a 65 20 2a 20 28 69 36 34  mPageSize * (i64
162a0 29 6e 46 72 6f 6d 50 61 67 65 3b 0a 20 20 20 20  )nFromPage;.    
162a1 20 20 69 36 34 20 69 4e 6f 77 20 3d 20 28 69 36    i64 iNow = (i6
162a2 34 29 28 28 6e 54 6f 50 61 67 65 3e 6e 4e 65 77  4)((nToPage>nNew
162a3 50 61 67 65 29 3f 6e 54 6f 50 61 67 65 3a 6e 4e  Page)?nToPage:nN
162a4 65 77 50 61 67 65 29 20 2a 20 28 69 36 34 29 6e  ewPage) * (i64)n
162a5 54 6f 50 61 67 65 53 69 7a 65 3b 20 0a 20 20 20  ToPageSize; .   
162a6 20 20 20 69 36 34 20 69 50 65 6e 64 69 6e 67 20     i64 iPending 
162a7 3d 20 28 28 69 36 34 29 50 45 4e 44 49 4e 47 5f  = ((i64)PENDING_
162a8 42 59 54 45 5f 50 41 47 45 28 70 42 74 54 6f 29  BYTE_PAGE(pBtTo)
162a9 2d 31 29 20 2a 28 69 36 34 29 6e 54 6f 50 61 67  -1) *(i64)nToPag
162aa 65 53 69 7a 65 3b 0a 20 20 0a 20 20 20 20 20 20  eSize;.  .      
162ab 61 73 73 65 72 74 28 20 69 53 69 7a 65 3c 3d 69  assert( iSize<=i
162ac 4e 6f 77 20 29 3b 0a 20 20 0a 20 20 20 20 20 20  Now );.  .      
162ad 2f 2a 20 43 6f 6d 6d 69 74 20 70 68 61 73 65 20  /* Commit phase 
162ae 6f 6e 65 20 73 79 6e 63 73 20 74 68 65 20 6a 6f  one syncs the jo
162af 75 72 6e 61 6c 20 66 69 6c 65 20 61 73 73 6f 63  urnal file assoc
162b0 69 61 74 65 64 20 77 69 74 68 20 70 54 6f 20 0a  iated with pTo .
162b1 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69 6e        ** contain
162b2 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ing the original
162b3 20 64 61 74 61 2e 20 49 74 20 64 6f 65 73 20 6e   data. It does n
162b4 6f 74 20 73 79 6e 63 20 74 68 65 20 64 61 74 61  ot sync the data
162b5 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20  base file.      
162b6 2a 2a 20 69 74 73 65 6c 66 2e 20 41 66 74 65 72  ** itself. After
162b7 20 64 6f 69 6e 67 20 74 68 69 73 20 69 74 20 69   doing this it i
162b8 73 20 73 61 66 65 20 74 6f 20 75 73 65 20 4f 73  s safe to use Os
162b9 54 72 75 6e 63 61 74 65 28 29 20 61 6e 64 20 6f  Truncate() and o
162ba 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20 66 69  ther.      ** fi
162bb 6c 65 20 41 50 49 73 20 6f 6e 20 74 68 65 20 64  le APIs on the d
162bc 61 74 61 62 61 73 65 20 66 69 6c 65 20 64 69 72  atabase file dir
162bd 65 63 74 6c 79 2e 0a 20 20 20 20 20 20 2a 2f 0a  ectly..      */.
162be 20 20 20 20 20 20 70 42 74 54 6f 2d 3e 64 62 20        pBtTo->db 
162bf 3d 20 70 54 6f 2d 3e 64 62 3b 0a 20 20 20 20 20  = pTo->db;.     
162c0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 61 67   rc = sqlite3Pag
162c1 65 72 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e 65  erCommitPhaseOne
162c2 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 2c 20  (pBtTo->pPager, 
162c3 30 2c 20 30 2c 20 31 29 3b 0a 20 20 20 20 20 20  0, 0, 1);.      
162c4 69 66 28 20 69 53 69 7a 65 3c 69 4e 6f 77 20 26  if( iSize<iNow &
162c5 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  & rc==SQLITE_OK 
162c6 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
162c7 73 71 6c 69 74 65 33 4f 73 54 72 75 6e 63 61 74  sqlite3OsTruncat
162c8 65 28 70 46 69 6c 65 2c 20 69 53 69 7a 65 29 3b  e(pFile, iSize);
162c9 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  .      }.  .    
162ca 20 20 2f 2a 20 54 68 65 20 6c 6f 6f 70 20 74 68    /* The loop th
162cb 61 74 20 63 6f 70 69 65 64 20 64 61 74 61 20 66  at copied data f
162cc 72 6f 6d 20 64 61 74 61 62 61 73 65 20 70 46 72  rom database pFr
162cd 6f 6d 20 74 6f 20 70 54 6f 20 64 69 64 20 6e 6f  om to pTo did no
162ce 74 0a 20 20 20 20 20 20 2a 2a 20 70 6f 70 75 6c  t.      ** popul
162cf 61 74 65 20 74 68 65 20 6c 6f 63 6b 69 6e 67 20  ate the locking 
162d0 70 61 67 65 20 6f 66 20 64 61 74 61 62 61 73 65  page of database
162d1 20 70 54 6f 2e 20 49 66 20 74 68 65 20 70 61 67   pTo. If the pag
162d2 65 2d 73 69 7a 65 20 6f 66 0a 20 20 20 20 20 20  e-size of.      
162d3 2a 2a 20 70 46 72 6f 6d 20 69 73 20 73 6d 61 6c  ** pFrom is smal
162d4 6c 65 72 20 74 68 61 6e 20 74 68 61 74 20 6f 66  ler than that of
162d5 20 70 54 6f 2c 20 74 68 69 73 20 6d 65 61 6e 73   pTo, this means
162d6 20 73 6f 6d 65 20 64 61 74 61 20 77 69 6c 6c 0a   some data will.
162d7 20 20 20 20 20 20 2a 2a 20 6e 6f 74 20 68 61 76        ** not hav
162d8 65 20 62 65 65 6e 20 63 6f 70 69 65 64 2e 20 0a  e been copied. .
162d9 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
162da 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70  * This block cop
162db 69 65 73 20 74 68 65 20 6d 69 73 73 69 6e 67 20  ies the missing 
162dc 64 61 74 61 20 66 72 6f 6d 20 64 61 74 61 62 61  data from databa
162dd 73 65 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 20  se pFrom to pTo 
162de 0a 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20  .      ** using 
162df 66 69 6c 65 20 41 50 49 73 2e 20 54 68 69 73 20  file APIs. This 
162e0 69 73 20 73 61 66 65 20 62 65 63 61 75 73 65 20  is safe because 
162e1 61 74 20 74 68 69 73 20 70 6f 69 6e 74 20 77 65  at this point we
162e2 20 6b 6e 6f 77 20 74 68 61 74 0a 20 20 20 20 20   know that.     
162e3 20 2a 2a 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f   ** all of the o
162e4 72 69 67 69 6e 61 6c 20 64 61 74 61 20 66 72 6f  riginal data fro
162e5 6d 20 70 54 6f 20 68 61 73 20 62 65 65 6e 20 73  m pTo has been s
162e6 79 6e 63 65 64 20 69 6e 74 6f 20 74 68 65 20 0a  ynced into the .
162e7 20 20 20 20 20 20 2a 2a 20 6a 6f 75 72 6e 61 6c        ** journal
162e8 20 66 69 6c 65 2e 20 41 74 20 74 68 69 73 20 70   file. At this p
162e9 6f 69 6e 74 20 69 74 20 77 6f 75 6c 64 20 62 65  oint it would be
162ea 20 73 61 66 65 20 74 6f 20 64 6f 20 61 6e 79 74   safe to do anyt
162eb 68 69 6e 67 20 61 74 0a 20 20 20 20 20 20 2a 2a  hing at.      **
162ec 20 61 6c 6c 20 74 6f 20 74 68 65 20 64 61 74 61   all to the data
162ed 62 61 73 65 20 66 69 6c 65 20 65 78 63 65 70 74  base file except
162ee 20 74 72 75 6e 63 61 74 65 20 69 74 20 74 6f 20   truncate it to 
162ef 7a 65 72 6f 20 62 79 74 65 73 2e 0a 20 20 20 20  zero bytes..    
162f0 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 72    */.      if( r
162f1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
162f2 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 3c 6e 54  nFromPageSize<nT
162f3 6f 50 61 67 65 53 69 7a 65 20 26 26 20 69 53 69  oPageSize && iSi
162f4 7a 65 3e 69 50 65 6e 64 69 6e 67 29 7b 0a 20 20  ze>iPending){.  
162f5 20 20 20 20 20 20 69 36 34 20 69 4f 66 66 3b 0a        i64 iOff;.
162f6 20 20 20 20 20 20 20 20 66 6f 72 28 0a 20 20 20          for(.   
162f7 20 20 20 20 20 20 20 69 4f 66 66 3d 69 50 65 6e         iOff=iPen
162f8 64 69 6e 67 3b 20 0a 20 20 20 20 20 20 20 20 20  ding; .         
162f9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
162fa 26 20 69 4f 66 66 3c 28 69 50 65 6e 64 69 6e 67  & iOff<(iPending
162fb 2b 6e 54 6f 50 61 67 65 53 69 7a 65 29 3b 20 0a  +nToPageSize); .
162fc 20 20 20 20 20 20 20 20 20 20 69 4f 66 66 20 2b            iOff +
162fd 3d 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a 65 0a  = nFromPageSize.
162fe 20 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20          ){.     
162ff 20 20 20 20 20 44 62 50 61 67 65 20 2a 70 46 72       DbPage *pFr
16300 6f 6d 50 61 67 65 20 3d 20 30 3b 0a 20 20 20 20  omPage = 0;.    
16301 20 20 20 20 20 20 50 67 6e 6f 20 69 46 72 6f 6d        Pgno iFrom
16302 20 3d 20 28 69 4f 66 66 2f 6e 46 72 6f 6d 50 61   = (iOff/nFromPa
16303 67 65 53 69 7a 65 29 2b 31 3b 0a 20 20 0a 20 20  geSize)+1;.  .  
16304 20 20 20 20 20 20 20 20 69 66 28 20 69 46 72 6f          if( iFro
16305 6d 3d 3d 50 45 4e 44 49 4e 47 5f 42 59 54 45 5f  m==PENDING_BYTE_
16306 50 41 47 45 28 70 42 74 46 72 6f 6d 29 20 7c 7c  PAGE(pBtFrom) ||
16307 20 69 46 72 6f 6d 3e 6e 46 72 6f 6d 50 61 67 65   iFrom>nFromPage
16308 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
16309 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
1630a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 20 20 20      }.  .       
1630b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50     rc = sqlite3P
1630c 61 67 65 72 47 65 74 28 70 42 74 46 72 6f 6d 2d  agerGet(pBtFrom-
1630d 3e 70 50 61 67 65 72 2c 20 69 46 72 6f 6d 2c 20  >pPager, iFrom, 
1630e 26 70 46 72 6f 6d 50 61 67 65 29 3b 0a 20 20 20  &pFromPage);.   
1630f 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
16310 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
16311 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 46          char *zF
16312 72 6f 6d 20 3d 20 73 71 6c 69 74 65 33 50 61 67  rom = sqlite3Pag
16313 65 72 47 65 74 44 61 74 61 28 70 46 72 6f 6d 50  erGetData(pFromP
16314 61 67 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  age);.          
16315 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73    rc = sqlite3Os
16316 57 72 69 74 65 28 70 46 69 6c 65 2c 20 7a 46 72  Write(pFile, zFr
16317 6f 6d 2c 20 6e 46 72 6f 6d 50 61 67 65 53 69 7a  om, nFromPageSiz
16318 65 2c 20 69 4f 66 66 29 3b 0a 20 20 20 20 20 20  e, iOff);.      
16319 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61 67        sqlite3Pag
1631a 65 72 55 6e 72 65 66 28 70 46 72 6f 6d 50 61 67  erUnref(pFromPag
1631b 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  e);.          }.
1631c 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1631d 7d 0a 20 20 0a 20 20 20 20 20 20 2f 2a 20 53 79  }.  .      /* Sy
1631e 6e 63 20 74 68 65 20 64 61 74 61 62 61 73 65 20  nc the database 
1631f 66 69 6c 65 20 2a 2f 0a 20 20 20 20 20 20 69 66  file */.      if
16320 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
16321 29 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  ){.        rc = 
16322 73 71 6c 69 74 65 33 50 61 67 65 72 53 79 6e 63  sqlite3PagerSync
16323 28 70 42 74 54 6f 2d 3e 70 50 61 67 65 72 29 3b  (pBtTo->pPager);
16324 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c  .      }.    }el
16325 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
16326 71 6c 69 74 65 33 50 61 67 65 72 54 72 75 6e 63  qlite3PagerTrunc
16327 61 74 65 28 70 42 74 54 6f 2d 3e 70 50 61 67 65  ate(pBtTo->pPage
16328 72 2c 20 6e 4e 65 77 50 61 67 65 29 3b 0a 20 20  r, nNewPage);.  
16329 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d    }.    if( rc==
1632a 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
1632b 20 20 20 70 42 74 54 6f 2d 3e 70 61 67 65 53 69     pBtTo->pageSi
1632c 7a 65 46 69 78 65 64 20 3d 20 30 3b 0a 20 20 20  zeFixed = 0;.   
1632d 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63   }.  }..  if( rc
1632e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42   ){.    sqlite3B
1632f 74 72 65 65 52 6f 6c 6c 62 61 63 6b 28 70 54 6f  treeRollback(pTo
16330 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
16331 20 72 63 3b 20 20 0a 7d 0a 53 51 4c 49 54 45 5f   rc;  .}.SQLITE_
16332 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16333 74 65 33 42 74 72 65 65 43 6f 70 79 46 69 6c 65  te3BtreeCopyFile
16334 28 42 74 72 65 65 20 2a 70 54 6f 2c 20 42 74 72  (Btree *pTo, Btr
16335 65 65 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 6e  ee *pFrom){.  in
16336 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 42  t rc;.  sqlite3B
16337 74 72 65 65 45 6e 74 65 72 28 70 54 6f 29 3b 0a  treeEnter(pTo);.
16338 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45 6e    sqlite3BtreeEn
16339 74 65 72 28 70 46 72 6f 6d 29 3b 0a 20 20 72 63  ter(pFrom);.  rc
1633a 20 3d 20 62 74 72 65 65 43 6f 70 79 46 69 6c 65   = btreeCopyFile
1633b 28 70 54 6f 2c 20 70 46 72 6f 6d 29 3b 0a 20 20  (pTo, pFrom);.  
1633c 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1633d 65 28 70 46 72 6f 6d 29 3b 0a 20 20 73 71 6c 69  e(pFrom);.  sqli
1633e 74 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 54  te3BtreeLeave(pT
1633f 6f 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  o);.  return rc;
16340 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  .}..#endif /* SQ
16341 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55 55 4d  LITE_OMIT_VACUUM
16342 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72   */../*.** Retur
16343 6e 20 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20  n non-zero if a 
16344 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
16345 63 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ctive..*/.SQLITE
16346 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16347 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
16348 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a 20 20  ns(Btree *p){.  
16349 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c 7c 20  assert( p==0 || 
1634a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
1634b 6c 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ld(p->db->mutex)
1634c 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 20   );.  return (p 
1634d 26 26 20 28 70 2d 3e 69 6e 54 72 61 6e 73 3d 3d  && (p->inTrans==
1634e 54 52 41 4e 53 5f 57 52 49 54 45 29 29 3b 0a 7d  TRANS_WRITE));.}
1634f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 6e  ../*.** Return n
16350 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 73 74 61  on-zero if a sta
16351 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63 74 69  tement transacti
16352 6f 6e 20 69 73 20 61 63 74 69 76 65 2e 0a 2a 2f  on is active..*/
16353 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16354 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72 65 65  int sqlite3Btree
16355 49 73 49 6e 53 74 6d 74 28 42 74 72 65 65 20 2a  IsInStmt(Btree *
16356 70 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  p){.  assert( sq
16357 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 4d  lite3BtreeHoldsM
16358 75 74 65 78 28 70 29 20 29 3b 0a 20 20 72 65 74  utex(p) );.  ret
16359 75 72 6e 20 28 70 2d 3e 70 42 74 20 26 26 20 70  urn (p->pBt && p
1635a 2d 3e 70 42 74 2d 3e 69 6e 53 74 6d 74 29 3b 0a  ->pBt->inStmt);.
1635b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
1635c 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 61 20 72 65  non-zero if a re
1635d 61 64 20 28 6f 72 20 77 72 69 74 65 29 20 74 72  ad (or write) tr
1635e 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
1635f 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ive..*/.SQLITE_P
16360 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16361 65 33 42 74 72 65 65 49 73 49 6e 52 65 61 64 54  e3BtreeIsInReadT
16362 72 61 6e 73 28 42 74 72 65 65 20 2a 70 29 7b 0a  rans(Btree *p){.
16363 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
16364 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e  3_mutex_held(p->
16365 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
16366 72 65 74 75 72 6e 20 28 70 20 26 26 20 28 70 2d  return (p && (p-
16367 3e 69 6e 54 72 61 6e 73 21 3d 54 52 41 4e 53 5f  >inTrans!=TRANS_
16368 4e 4f 4e 45 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  NONE));.}../*.**
16369 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   This function r
1636a 65 74 75 72 6e 73 20 61 20 70 6f 69 6e 74 65 72  eturns a pointer
1636b 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20 6d 65   to a blob of me
1636c 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20  mory associated 
1636d 77 69 74 68 0a 2a 2a 20 61 20 73 69 6e 67 6c 65  with.** a single
1636e 20 73 68 61 72 65 64 2d 62 74 72 65 65 2e 20 54   shared-btree. T
1636f 68 65 20 6d 65 6d 6f 72 79 20 69 73 20 75 73 65  he memory is use
16370 64 20 62 79 20 63 6c 69 65 6e 74 20 63 6f 64 65  d by client code
16371 20 66 6f 72 20 69 74 73 20 6f 77 6e 0a 2a 2a 20   for its own.** 
16372 70 75 72 70 6f 73 65 73 20 28 66 6f 72 20 65 78  purposes (for ex
16373 61 6d 70 6c 65 2c 20 74 6f 20 73 74 6f 72 65 20  ample, to store 
16374 61 20 68 69 67 68 2d 6c 65 76 65 6c 20 73 63 68  a high-level sch
16375 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20 77  ema associated w
16376 69 74 68 20 0a 2a 2a 20 74 68 65 20 73 68 61 72  ith .** the shar
16377 65 64 2d 62 74 72 65 65 29 2e 20 54 68 65 20 62  ed-btree). The b
16378 74 72 65 65 20 6c 61 79 65 72 20 6d 61 6e 61 67  tree layer manag
16379 65 73 20 72 65 66 65 72 65 6e 63 65 20 63 6f 75  es reference cou
1637a 6e 74 69 6e 67 20 69 73 73 75 65 73 2e 0a 2a 2a  nting issues..**
1637b 0a 2a 2a 20 54 68 65 20 66 69 72 73 74 20 74 69  .** The first ti
1637c 6d 65 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65  me this is calle
1637d 64 20 6f 6e 20 61 20 73 68 61 72 65 64 2d 62 74  d on a shared-bt
1637e 72 65 65 2c 20 6e 42 79 74 65 73 20 62 79 74 65  ree, nBytes byte
1637f 73 20 6f 66 20 6d 65 6d 6f 72 79 0a 2a 2a 20 61  s of memory.** a
16380 72 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 7a 65  re allocated, ze
16381 72 6f 65 64 2c 20 61 6e 64 20 72 65 74 75 72 6e  roed, and return
16382 65 64 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72  ed to the caller
16383 2e 20 46 6f 72 20 65 61 63 68 20 73 75 62 73 65  . For each subse
16384 71 75 65 6e 74 20 0a 2a 2a 20 63 61 6c 6c 20 74  quent .** call t
16385 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65  he nBytes parame
16386 74 65 72 20 69 73 20 69 67 6e 6f 72 65 64 20 61  ter is ignored a
16387 6e 64 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  nd a pointer to 
16388 74 68 65 20 73 61 6d 65 20 62 6c 6f 62 0a 2a 2a  the same blob.**
16389 20 6f 66 20 6d 65 6d 6f 72 79 20 72 65 74 75 72   of memory retur
1638a 6e 65 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ned. .**.** If t
1638b 68 65 20 6e 42 79 74 65 73 20 70 61 72 61 6d 65  he nBytes parame
1638c 74 65 72 20 69 73 20 30 20 61 6e 64 20 74 68 65  ter is 0 and the
1638d 20 62 6c 6f 62 20 6f 66 20 6d 65 6d 6f 72 79 20   blob of memory 
1638e 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65 65 6e  has not yet been
1638f 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 61  .** allocated, a
16390 20 6e 75 6c 6c 20 70 6f 69 6e 74 65 72 20 69 73   null pointer is
16391 20 72 65 74 75 72 6e 65 64 2e 20 49 66 20 74 68   returned. If th
16392 65 20 62 6c 6f 62 20 68 61 73 20 61 6c 72 65 61  e blob has alrea
16393 64 79 20 62 65 65 6e 0a 2a 2a 20 61 6c 6c 6f 63  dy been.** alloc
16394 61 74 65 64 2c 20 69 74 20 69 73 20 72 65 74 75  ated, it is retu
16395 72 6e 65 64 20 61 73 20 6e 6f 72 6d 61 6c 2e 0a  rned as normal..
16396 2a 2a 0a 2a 2a 20 4a 75 73 74 20 62 65 66 6f 72  **.** Just befor
16397 65 20 74 68 65 20 73 68 61 72 65 64 2d 62 74 72  e the shared-btr
16398 65 65 20 69 73 20 63 6c 6f 73 65 64 2c 20 74 68  ee is closed, th
16399 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73 73 65  e function passe
1639a 64 20 61 73 20 74 68 65 20 0a 2a 2a 20 78 46 72  d as the .** xFr
1639b 65 65 20 61 72 67 75 6d 65 6e 74 20 77 68 65 6e  ee argument when
1639c 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f   the memory allo
1639d 63 61 74 69 6f 6e 20 77 61 73 20 6d 61 64 65 20  cation was made 
1639e 69 73 20 69 6e 76 6f 6b 65 64 20 6f 6e 20 74 68  is invoked on th
1639f 65 20 0a 2a 2a 20 62 6c 6f 62 20 6f 66 20 61 6c  e .** blob of al
163a0 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79 2e 20  located memory. 
163a1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 73 68  This function sh
163a2 6f 75 6c 64 20 6e 6f 74 20 63 61 6c 6c 20 73 71  ould not call sq
163a3 6c 69 74 65 33 5f 66 72 65 65 28 29 0a 2a 2a 20  lite3_free().** 
163a4 6f 6e 20 74 68 65 20 6d 65 6d 6f 72 79 2c 20 74  on the memory, t
163a5 68 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 64  he btree layer d
163a6 6f 65 73 20 74 68 61 74 2e 0a 2a 2f 0a 53 51 4c  oes that..*/.SQL
163a7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
163a8 20 2a 73 71 6c 69 74 65 33 42 74 72 65 65 53 63   *sqlite3BtreeSc
163a9 68 65 6d 61 28 42 74 72 65 65 20 2a 70 2c 20 69  hema(Btree *p, i
163aa 6e 74 20 6e 42 79 74 65 73 2c 20 76 6f 69 64 28  nt nBytes, void(
163ab 2a 78 46 72 65 65 29 28 76 6f 69 64 20 2a 29 29  *xFree)(void *))
163ac 7b 0a 20 20 42 74 53 68 61 72 65 64 20 2a 70 42  {.  BtShared *pB
163ad 74 20 3d 20 70 2d 3e 70 42 74 3b 0a 20 20 73 71  t = p->pBt;.  sq
163ae 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
163af 70 29 3b 0a 20 20 69 66 28 20 21 70 42 74 2d 3e  p);.  if( !pBt->
163b0 70 53 63 68 65 6d 61 20 26 26 20 6e 42 79 74 65  pSchema && nByte
163b1 73 20 29 7b 0a 20 20 20 20 70 42 74 2d 3e 70 53  s ){.    pBt->pS
163b2 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33 4d  chema = sqlite3M
163b3 61 6c 6c 6f 63 5a 65 72 6f 28 6e 42 79 74 65 73  allocZero(nBytes
163b4 29 3b 0a 20 20 20 20 70 42 74 2d 3e 78 46 72 65  );.    pBt->xFre
163b5 65 53 63 68 65 6d 61 20 3d 20 78 46 72 65 65 3b  eSchema = xFree;
163b6 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 42 74  .  }.  sqlite3Bt
163b7 72 65 65 4c 65 61 76 65 28 70 29 3b 0a 20 20 72  reeLeave(p);.  r
163b8 65 74 75 72 6e 20 70 42 74 2d 3e 70 53 63 68 65  eturn pBt->pSche
163b9 6d 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ma;.}../*.** Ret
163ba 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 6f 74  urn true if anot
163bb 68 65 72 20 75 73 65 72 20 6f 66 20 74 68 65 20  her user of the 
163bc 73 61 6d 65 20 73 68 61 72 65 64 20 62 74 72 65  same shared btre
163bd 65 20 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e  e as the argumen
163be 74 0a 2a 2a 20 68 61 6e 64 6c 65 20 68 6f 6c 64  t.** handle hold
163bf 73 20 61 6e 20 65 78 63 6c 75 73 69 76 65 20 6c  s an exclusive l
163c0 6f 63 6b 20 6f 6e 20 74 68 65 20 73 71 6c 69 74  ock on the sqlit
163c1 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a  e_master table..
163c2 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
163c3 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
163c4 65 65 53 63 68 65 6d 61 4c 6f 63 6b 65 64 28 42  eeSchemaLocked(B
163c5 74 72 65 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  tree *p){.  int 
163c6 72 63 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71  rc;.  assert( sq
163c7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
163c8 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
163c9 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
163ca 45 6e 74 65 72 28 70 29 3b 0a 20 20 72 63 20 3d  Enter(p);.  rc =
163cb 20 28 71 75 65 72 79 54 61 62 6c 65 4c 6f 63 6b   (queryTableLock
163cc 28 70 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c  (p, MASTER_ROOT,
163cd 20 52 45 41 44 5f 4c 4f 43 4b 29 21 3d 53 51 4c   READ_LOCK)!=SQL
163ce 49 54 45 5f 4f 4b 29 3b 0a 20 20 73 71 6c 69 74  ITE_OK);.  sqlit
163cf 65 33 42 74 72 65 65 4c 65 61 76 65 28 70 29 3b  e3BtreeLeave(p);
163d0 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
163d1 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
163d2 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
163d3 48 45 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20  HE./*.** Obtain 
163d4 61 20 6c 6f 63 6b 20 6f 6e 20 74 68 65 20 74 61  a lock on the ta
163d5 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  ble whose root p
163d6 61 67 65 20 69 73 20 69 54 61 62 2e 20 20 54 68  age is iTab.  Th
163d7 65 0a 2a 2a 20 6c 6f 63 6b 20 69 73 20 61 20 77  e.** lock is a w
163d8 72 69 74 65 20 6c 6f 63 6b 20 69 66 20 69 73 57  rite lock if isW
163d9 72 69 74 65 6c 6f 63 6b 20 69 73 20 74 72 75 65  ritelock is true
163da 20 6f 72 20 61 20 72 65 61 64 20 6c 6f 63 6b 0a   or a read lock.
163db 2a 2a 20 69 66 20 69 74 20 69 73 20 66 61 6c 73  ** if it is fals
163dc 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
163dd 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
163de 42 74 72 65 65 4c 6f 63 6b 54 61 62 6c 65 28 42  BtreeLockTable(B
163df 74 72 65 65 20 2a 70 2c 20 69 6e 74 20 69 54 61  tree *p, int iTa
163e0 62 2c 20 75 38 20 69 73 57 72 69 74 65 4c 6f 63  b, u8 isWriteLoc
163e1 6b 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  k){.  int rc = S
163e2 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20  QLITE_OK;.  if( 
163e3 70 2d 3e 73 68 61 72 61 62 6c 65 20 29 7b 0a 20  p->sharable ){. 
163e4 20 20 20 75 38 20 6c 6f 63 6b 54 79 70 65 20 3d     u8 lockType =
163e5 20 52 45 41 44 5f 4c 4f 43 4b 20 2b 20 69 73 57   READ_LOCK + isW
163e6 72 69 74 65 4c 6f 63 6b 3b 0a 20 20 20 20 61 73  riteLock;.    as
163e7 73 65 72 74 28 20 52 45 41 44 5f 4c 4f 43 4b 2b  sert( READ_LOCK+
163e8 31 3d 3d 57 52 49 54 45 5f 4c 4f 43 4b 20 29 3b  1==WRITE_LOCK );
163e9 0a 20 20 20 20 61 73 73 65 72 74 28 20 69 73 57  .    assert( isW
163ea 72 69 74 65 4c 6f 63 6b 3d 3d 30 20 7c 7c 20 69  riteLock==0 || i
163eb 73 57 72 69 74 65 4c 6f 63 6b 3d 3d 31 20 29 3b  sWriteLock==1 );
163ec 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
163ed 65 45 6e 74 65 72 28 70 29 3b 0a 20 20 20 20 72  eEnter(p);.    r
163ee 63 20 3d 20 71 75 65 72 79 54 61 62 6c 65 4c 6f  c = queryTableLo
163ef 63 6b 28 70 2c 20 69 54 61 62 2c 20 6c 6f 63 6b  ck(p, iTab, lock
163f0 54 79 70 65 29 3b 0a 20 20 20 20 69 66 28 20 72  Type);.    if( r
163f1 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
163f2 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 63 6b 54        rc = lockT
163f3 61 62 6c 65 28 70 2c 20 69 54 61 62 2c 20 6c 6f  able(p, iTab, lo
163f4 63 6b 54 79 70 65 29 3b 0a 20 20 20 20 7d 0a 20  ckType);.    }. 
163f5 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c     sqlite3BtreeL
163f6 65 61 76 65 28 70 29 3b 0a 20 20 7d 0a 20 20 72  eave(p);.  }.  r
163f7 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
163f8 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
163f9 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
163fa 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20  ./*.** Argument 
163fb 70 43 73 72 20 6d 75 73 74 20 62 65 20 61 20 63  pCsr must be a c
163fc 75 72 73 6f 72 20 6f 70 65 6e 65 64 20 66 6f 72  ursor opened for
163fd 20 77 72 69 74 69 6e 67 20 6f 6e 20 61 6e 20 0a   writing on an .
163fe 2a 2a 20 49 4e 54 4b 45 59 20 74 61 62 6c 65 20  ** INTKEY table 
163ff 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 69  currently pointi
16400 6e 67 20 61 74 20 61 20 76 61 6c 69 64 20 74 61  ng at a valid ta
16401 62 6c 65 20 65 6e 74 72 79 2e 20 0a 2a 2a 20 54  ble entry. .** T
16402 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 6f 64  his function mod
16403 69 66 69 65 73 20 74 68 65 20 64 61 74 61 20 73  ifies the data s
16404 74 6f 72 65 64 20 61 73 20 70 61 72 74 20 6f 66  tored as part of
16405 20 74 68 61 74 20 65 6e 74 72 79 2e 0a 2a 2a 20   that entry..** 
16406 4f 6e 6c 79 20 74 68 65 20 64 61 74 61 20 63 6f  Only the data co
16407 6e 74 65 6e 74 20 6d 61 79 20 6f 6e 6c 79 20 62  ntent may only b
16408 65 20 6d 6f 64 69 66 69 65 64 2c 20 69 74 20 69  e modified, it i
16409 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65 0a 2a  s not possible.*
1640a 2a 20 74 6f 20 63 68 61 6e 67 65 20 74 68 65 20  * to change the 
1640b 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 64 61  length of the da
1640c 74 61 20 73 74 6f 72 65 64 2e 0a 2a 2f 0a 53 51  ta stored..*/.SQ
1640d 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1640e 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
1640f 44 61 74 61 28 42 74 43 75 72 73 6f 72 20 2a 70  Data(BtCursor *p
16410 43 73 72 2c 20 75 33 32 20 6f 66 66 73 65 74 2c  Csr, u32 offset,
16411 20 75 33 32 20 61 6d 74 2c 20 76 6f 69 64 20 2a   u32 amt, void *
16412 7a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 63 75  z){.  assert( cu
16413 72 73 6f 72 48 6f 6c 64 73 4d 75 74 65 78 28 70  rsorHoldsMutex(p
16414 43 73 72 29 20 29 3b 0a 20 20 61 73 73 65 72 74  Csr) );.  assert
16415 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
16416 68 65 6c 64 28 70 43 73 72 2d 3e 70 42 74 72 65  held(pCsr->pBtre
16417 65 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  e->db->mutex) );
16418 0a 20 20 61 73 73 65 72 74 28 70 43 73 72 2d 3e  .  assert(pCsr->
16419 69 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65  isIncrblobHandle
1641a 29 3b 0a 0a 20 20 72 65 73 74 6f 72 65 43 75 72  );..  restoreCur
1641b 73 6f 72 50 6f 73 69 74 69 6f 6e 28 70 43 73 72  sorPosition(pCsr
1641c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 73  );.  assert( pCs
1641d 72 2d 3e 65 53 74 61 74 65 21 3d 43 55 52 53 4f  r->eState!=CURSO
1641e 52 5f 52 45 51 55 49 52 45 53 45 45 4b 20 29 3b  R_REQUIRESEEK );
1641f 0a 20 20 69 66 28 20 70 43 73 72 2d 3e 65 53 74  .  if( pCsr->eSt
16420 61 74 65 21 3d 43 55 52 53 4f 52 5f 56 41 4c 49  ate!=CURSOR_VALI
16421 44 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  D ){.    return 
16422 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20 20  SQLITE_ABORT;.  
16423 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 73 6f  }..  /* Check so
16424 6d 65 20 70 72 65 63 6f 6e 64 69 74 69 6f 6e 73  me preconditions
16425 3a 20 0a 20 20 2a 2a 20 20 20 28 61 29 20 74 68  : .  **   (a) th
16426 65 20 63 75 72 73 6f 72 20 69 73 20 6f 70 65 6e  e cursor is open
16427 20 66 6f 72 20 77 72 69 74 69 6e 67 2c 0a 20 20   for writing,.  
16428 2a 2a 20 20 20 28 62 29 20 74 68 65 72 65 20 69  **   (b) there i
16429 73 20 6e 6f 20 72 65 61 64 2d 6c 6f 63 6b 20 6f  s no read-lock o
1642a 6e 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  n the table bein
1642b 67 20 6d 6f 64 69 66 69 65 64 20 61 6e 64 0a 20  g modified and. 
1642c 20 2a 2a 20 20 20 28 63 29 20 74 68 65 20 63 75   **   (c) the cu
1642d 72 73 6f 72 20 70 6f 69 6e 74 73 20 61 74 20 61  rsor points at a
1642e 20 76 61 6c 69 64 20 72 6f 77 20 6f 66 20 61 6e   valid row of an
1642f 20 69 6e 74 4b 65 79 20 74 61 62 6c 65 2e 0a 20   intKey table.. 
16430 20 2a 2f 0a 20 20 69 66 28 20 21 70 43 73 72 2d   */.  if( !pCsr-
16431 3e 77 72 46 6c 61 67 20 29 7b 0a 20 20 20 20 72  >wrFlag ){.    r
16432 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 52 45 41  eturn SQLITE_REA
16433 44 4f 4e 4c 59 3b 0a 20 20 7d 0a 20 20 61 73 73  DONLY;.  }.  ass
16434 65 72 74 28 20 21 70 43 73 72 2d 3e 70 42 74 2d  ert( !pCsr->pBt-
16435 3e 72 65 61 64 4f 6e 6c 79 20 0a 20 20 20 20 20  >readOnly .     
16436 20 20 20 20 20 26 26 20 70 43 73 72 2d 3e 70 42       && pCsr->pB
16437 74 2d 3e 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  t->inTransaction
16438 3d 3d 54 52 41 4e 53 5f 57 52 49 54 45 20 29 3b  ==TRANS_WRITE );
16439 0a 20 20 69 66 28 20 63 68 65 63 6b 52 65 61 64  .  if( checkRead
1643a 4c 6f 63 6b 73 28 70 43 73 72 2d 3e 70 42 74 72  Locks(pCsr->pBtr
1643b 65 65 2c 20 70 43 73 72 2d 3e 70 67 6e 6f 52 6f  ee, pCsr->pgnoRo
1643c 6f 74 2c 20 70 43 73 72 2c 20 30 29 20 29 7b 0a  ot, pCsr, 0) ){.
1643d 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
1643e 45 5f 4c 4f 43 4b 45 44 3b 20 2f 2a 20 54 68 65  E_LOCKED; /* The
1643f 20 74 61 62 6c 65 20 70 43 75 72 20 70 6f 69 6e   table pCur poin
16440 74 73 20 74 6f 20 68 61 73 20 61 20 72 65 61 64  ts to has a read
16441 20 6c 6f 63 6b 20 2a 2f 0a 20 20 7d 0a 20 20 69   lock */.  }.  i
16442 66 28 20 70 43 73 72 2d 3e 65 53 74 61 74 65 3d  f( pCsr->eState=
16443 3d 43 55 52 53 4f 52 5f 49 4e 56 41 4c 49 44 20  =CURSOR_INVALID 
16444 7c 7c 20 21 70 43 73 72 2d 3e 61 70 50 61 67 65  || !pCsr->apPage
16445 5b 70 43 73 72 2d 3e 69 50 61 67 65 5d 2d 3e 69  [pCsr->iPage]->i
16446 6e 74 4b 65 79 20 29 7b 0a 20 20 20 20 72 65 74  ntKey ){.    ret
16447 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
16448 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20  ;.  }..  return 
16449 61 63 63 65 73 73 50 61 79 6c 6f 61 64 28 70 43  accessPayload(pC
1644a 73 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c  sr, offset, amt,
1644b 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20   (unsigned char 
1644c 2a 29 7a 2c 20 30 2c 20 31 29 3b 0a 7d 0a 0a 2f  *)z, 0, 1);.}../
1644d 2a 20 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67  * .** Set a flag
1644e 20 6f 6e 20 74 68 69 73 20 63 75 72 73 6f 72 20   on this cursor 
1644f 74 6f 20 63 61 63 68 65 20 74 68 65 20 6c 6f 63  to cache the loc
16450 61 74 69 6f 6e 73 20 6f 66 20 70 61 67 65 73 20  ations of pages 
16451 66 72 6f 6d 20 74 68 65 20 0a 2a 2a 20 6f 76 65  from the .** ove
16452 72 66 6c 6f 77 20 6c 69 73 74 20 66 6f 72 20 74  rflow list for t
16453 68 65 20 63 75 72 72 65 6e 74 20 72 6f 77 2e 20  he current row. 
16454 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
16455 63 75 72 73 6f 72 73 20 6f 70 65 6e 65 64 0a 2a  cursors opened.*
16456 2a 20 66 6f 72 20 69 6e 63 72 65 6d 65 6e 74 61  * for incrementa
16457 6c 20 62 6c 6f 62 20 49 4f 20 6f 6e 6c 79 2e 0a  l blob IO only..
16458 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  **.** This funct
16459 69 6f 6e 20 73 65 74 73 20 61 20 66 6c 61 67 20  ion sets a flag 
1645a 6f 6e 6c 79 2e 20 54 68 65 20 61 63 74 75 61 6c  only. The actual
1645b 20 70 61 67 65 20 6c 6f 63 61 74 69 6f 6e 20 63   page location c
1645c 61 63 68 65 0a 2a 2a 20 28 73 74 6f 72 65 64 20  ache.** (stored 
1645d 69 6e 20 42 74 43 75 72 73 6f 72 2e 61 4f 76 65  in BtCursor.aOve
1645e 72 66 6c 6f 77 5b 5d 29 20 69 73 20 61 6c 6c 6f  rflow[]) is allo
1645f 63 61 74 65 64 20 61 6e 64 20 75 73 65 64 20 62  cated and used b
16460 79 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 61 63  y function.** ac
16461 63 65 73 73 50 61 79 6c 6f 61 64 28 29 20 28 74  cessPayload() (t
16462 68 65 20 77 6f 72 6b 65 72 20 66 75 6e 63 74 69  he worker functi
16463 6f 6e 20 66 6f 72 20 73 71 6c 69 74 65 33 42 74  on for sqlite3Bt
16464 72 65 65 44 61 74 61 28 29 20 61 6e 64 0a 2a 2a  reeData() and.**
16465 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74   sqlite3BtreePut
16466 44 61 74 61 28 29 29 2e 0a 2a 2f 0a 53 51 4c 49  Data())..*/.SQLI
16467 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
16468 73 71 6c 69 74 65 33 42 74 72 65 65 43 61 63 68  sqlite3BtreeCach
16469 65 4f 76 65 72 66 6c 6f 77 28 42 74 43 75 72 73  eOverflow(BtCurs
1646a 6f 72 20 2a 70 43 75 72 29 7b 0a 20 20 61 73 73  or *pCur){.  ass
1646b 65 72 74 28 20 63 75 72 73 6f 72 48 6f 6c 64 73  ert( cursorHolds
1646c 4d 75 74 65 78 28 70 43 75 72 29 20 29 3b 0a 20  Mutex(pCur) );. 
1646d 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
1646e 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 75 72  _mutex_held(pCur
1646f 2d 3e 70 42 74 72 65 65 2d 3e 64 62 2d 3e 6d 75  ->pBtree->db->mu
16470 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74  tex) );.  assert
16471 28 21 70 43 75 72 2d 3e 69 73 49 6e 63 72 62 6c  (!pCur->isIncrbl
16472 6f 62 48 61 6e 64 6c 65 29 3b 0a 20 20 61 73 73  obHandle);.  ass
16473 65 72 74 28 21 70 43 75 72 2d 3e 61 4f 76 65 72  ert(!pCur->aOver
16474 66 6c 6f 77 29 3b 0a 20 20 70 43 75 72 2d 3e 69  flow);.  pCur->i
16475 73 49 6e 63 72 62 6c 6f 62 48 61 6e 64 6c 65 20  sIncrblobHandle 
16476 3d 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  = 1;.}.#endif../
16477 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
16478 6e 64 20 6f 66 20 62 74 72 65 65 2e 63 20 2a 2a  nd of btree.c **
16479 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1647a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1647b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1647c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
1647d 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 66 69  egin file vdbefi
1647e 66 6f 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  fo.c ***********
1647f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16480 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
16481 2a 0a 2a 2a 20 32 30 30 35 20 4a 75 6e 65 20 31  *.** 2005 June 1
16482 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
16483 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
16484 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
16485 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
16486 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
16487 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
16488 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
16489 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1648a 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1648b 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1648c 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1648d 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1648e 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1648f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
16490 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
16491 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
16492 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
16493 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
16494 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16495 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16496 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16497 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16498 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d  .** This file im
16499 70 6c 65 6d 65 6e 74 73 20 61 20 46 49 46 4f 20  plements a FIFO 
1649a 71 75 65 75 65 20 6f 66 20 72 6f 77 69 64 73 20  queue of rowids 
1649b 75 73 65 64 20 66 6f 72 20 70 72 6f 63 65 73 73  used for process
1649c 69 6e 67 0a 2a 2a 20 55 50 44 41 54 45 20 61 6e  ing.** UPDATE an
1649d 64 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  d DELETE stateme
1649e 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  nts..**.** $Id: 
1649f 76 64 62 65 66 69 66 6f 2e 63 2c 76 20 31 2e 39  vdbefifo.c,v 1.9
164a0 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39 3a 31   2008/11/17 19:1
164a1 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 37 37  8:55 danielk1977
164a2 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a   Exp $.*/../*.**
164a3 20 43 6f 6e 73 74 61 6e 74 73 20 46 49 46 4f 53   Constants FIFOS
164a4 49 5a 45 5f 46 49 52 53 54 20 61 6e 64 20 46 49  IZE_FIRST and FI
164a5 46 4f 53 49 5a 45 5f 4d 41 58 20 61 72 65 20 74  FOSIZE_MAX are t
164a6 68 65 20 69 6e 69 74 69 61 6c 0a 2a 2a 20 6e 75  he initial.** nu
164a7 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
164a8 69 6e 20 61 20 66 69 66 6f 20 70 61 67 65 20 61  in a fifo page a
164a9 6e 64 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 6e  nd the maximum n
164aa 75 6d 62 65 72 20 6f 66 0a 2a 2a 20 65 6e 74 72  umber of.** entr
164ab 69 65 73 20 69 6e 20 61 20 66 69 66 6f 20 70 61  ies in a fifo pa
164ac 67 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 46  ge..*/.#define F
164ad 49 46 4f 53 49 5a 45 5f 46 49 52 53 54 20 28 28  IFOSIZE_FIRST ((
164ae 28 31 32 38 2d 73 69 7a 65 6f 66 28 46 69 66 6f  (128-sizeof(Fifo
164af 50 61 67 65 29 29 2f 38 29 2b 31 29 0a 23 69 66  Page))/8)+1).#if
164b0 64 65 66 20 53 51 4c 49 54 45 5f 4d 41 4c 4c 4f  def SQLITE_MALLO
164b1 43 5f 53 4f 46 54 5f 4c 49 4d 49 54 0a 23 20 64  C_SOFT_LIMIT.# d
164b2 65 66 69 6e 65 20 46 49 46 4f 53 49 5a 45 5f 4d  efine FIFOSIZE_M
164b3 41 58 20 20 20 28 69 6e 74 29 28 28 28 53 51 4c  AX   (int)(((SQL
164b4 49 54 45 5f 4d 41 4c 4c 4f 43 5f 53 4f 46 54 5f  ITE_MALLOC_SOFT_
164b5 4c 49 4d 49 54 2d 73 69 7a 65 6f 66 28 46 69 66  LIMIT-sizeof(Fif
164b6 6f 50 61 67 65 29 29 2f 38 29 2b 31 29 0a 23 65  oPage))/8)+1).#e
164b7 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 46 49 46  lse.# define FIF
164b8 4f 53 49 5a 45 5f 4d 41 58 20 20 20 28 69 6e 74  OSIZE_MAX   (int
164b9 29 28 28 28 32 36 32 31 34 34 2d 73 69 7a 65 6f  )(((262144-sizeo
164ba 66 28 46 69 66 6f 50 61 67 65 29 29 2f 38 29 2b  f(FifoPage))/8)+
164bb 31 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  1).#endif../*.**
164bc 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20   Allocate a new 
164bd 46 69 66 6f 50 61 67 65 20 61 6e 64 20 72 65 74  FifoPage and ret
164be 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
164bf 20 69 74 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c   it.  Return NUL
164c0 4c 20 69 66 0a 2a 2a 20 77 65 20 72 75 6e 20 6f  L if.** we run o
164c1 75 74 20 6f 66 20 6d 65 6d 6f 72 79 2e 20 20 4c  ut of memory.  L
164c2 65 61 76 65 20 73 70 61 63 65 20 6f 6e 20 74 68  eave space on th
164c3 65 20 70 61 67 65 20 66 6f 72 20 6e 45 6e 74 72  e page for nEntr
164c4 79 20 65 6e 74 72 69 65 73 2e 0a 2a 2f 0a 73 74  y entries..*/.st
164c5 61 74 69 63 20 46 69 66 6f 50 61 67 65 20 2a 61  atic FifoPage *a
164c6 6c 6c 6f 63 61 74 65 46 69 66 6f 50 61 67 65 28  llocateFifoPage(
164c7 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
164c8 20 6e 45 6e 74 72 79 29 7b 0a 20 20 46 69 66 6f   nEntry){.  Fifo
164c9 50 61 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 69  Page *pPage;.  i
164ca 66 28 20 6e 45 6e 74 72 79 3e 46 49 46 4f 53 49  f( nEntry>FIFOSI
164cb 5a 45 5f 4d 41 58 20 29 7b 0a 20 20 20 20 6e 45  ZE_MAX ){.    nE
164cc 6e 74 72 79 20 3d 20 46 49 46 4f 53 49 5a 45 5f  ntry = FIFOSIZE_
164cd 4d 41 58 3b 0a 20 20 7d 0a 20 20 70 50 61 67 65  MAX;.  }.  pPage
164ce 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
164cf 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
164d0 28 46 69 66 6f 50 61 67 65 29 20 2b 20 73 69 7a  (FifoPage) + siz
164d1 65 6f 66 28 69 36 34 29 2a 28 6e 45 6e 74 72 79  eof(i64)*(nEntry
164d2 2d 31 29 20 29 3b 0a 20 20 69 66 28 20 70 50 61  -1) );.  if( pPa
164d3 67 65 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d  ge ){.    pPage-
164d4 3e 6e 53 6c 6f 74 20 3d 20 6e 45 6e 74 72 79 3b  >nSlot = nEntry;
164d5 0a 20 20 20 20 70 50 61 67 65 2d 3e 69 57 72 69  .    pPage->iWri
164d6 74 65 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 67  te = 0;.    pPag
164d7 65 2d 3e 69 52 65 61 64 20 3d 20 30 3b 0a 20 20  e->iRead = 0;.  
164d8 20 20 70 50 61 67 65 2d 3e 70 4e 65 78 74 20 3d    pPage->pNext =
164d9 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
164da 20 70 50 61 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   pPage;.}../*.**
164db 20 49 6e 69 74 69 61 6c 69 7a 65 20 61 20 46 69   Initialize a Fi
164dc 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  fo structure..*/
164dd 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
164de 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
164df 46 69 66 6f 49 6e 69 74 28 46 69 66 6f 20 2a 70  FifoInit(Fifo *p
164e0 46 69 66 6f 2c 20 73 71 6c 69 74 65 33 20 2a 64  Fifo, sqlite3 *d
164e1 62 29 7b 0a 20 20 6d 65 6d 73 65 74 28 70 46 69  b){.  memset(pFi
164e2 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70  fo, 0, sizeof(*p
164e3 46 69 66 6f 29 29 3b 0a 20 20 70 46 69 66 6f 2d  Fifo));.  pFifo-
164e4 3e 64 62 20 3d 20 64 62 3b 0a 7d 0a 0a 2f 2a 0a  >db = db;.}../*.
164e5 2a 2a 20 50 75 73 68 20 61 20 73 69 6e 67 6c 65  ** Push a single
164e6 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 20   64-bit integer 
164e7 76 61 6c 75 65 20 69 6e 74 6f 20 74 68 65 20 46  value into the F
164e8 69 66 6f 2e 20 20 52 65 74 75 72 6e 20 53 51 4c  ifo.  Return SQL
164e9 49 54 45 5f 4f 4b 0a 2a 2a 20 6e 6f 72 6d 61 6c  ITE_OK.** normal
164ea 6c 79 2e 20 20 20 53 51 4c 49 54 45 5f 4e 4f 4d  ly.   SQLITE_NOM
164eb 45 4d 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  EM is returned i
164ec 66 20 77 65 20 61 72 65 20 75 6e 61 62 6c 65 20  f we are unable 
164ed 74 6f 20 61 6c 6c 6f 63 61 74 65 0a 2a 2a 20 6d  to allocate.** m
164ee 65 6d 6f 72 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  emory..*/.SQLITE
164ef 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
164f0 69 74 65 33 56 64 62 65 46 69 66 6f 50 75 73 68  ite3VdbeFifoPush
164f1 28 46 69 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36  (Fifo *pFifo, i6
164f2 34 20 76 61 6c 29 7b 0a 20 20 46 69 66 6f 50 61  4 val){.  FifoPa
164f3 67 65 20 2a 70 50 61 67 65 3b 0a 20 20 70 50 61  ge *pPage;.  pPa
164f4 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 4c 61 73  ge = pFifo->pLas
164f5 74 3b 0a 20 20 69 66 28 20 70 50 61 67 65 3d 3d  t;.  if( pPage==
164f6 30 20 29 7b 0a 20 20 20 20 70 50 61 67 65 20 3d  0 ){.    pPage =
164f7 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20   pFifo->pLast = 
164f8 70 46 69 66 6f 2d 3e 70 46 69 72 73 74 20 3d 0a  pFifo->pFirst =.
164f9 20 20 20 20 20 20 20 20 20 61 6c 6c 6f 63 61 74           allocat
164fa 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 6f 2d  eFifoPage(pFifo-
164fb 3e 64 62 2c 20 46 49 46 4f 53 49 5a 45 5f 46 49  >db, FIFOSIZE_FI
164fc 52 53 54 29 3b 0a 20 20 20 20 69 66 28 20 70 50  RST);.    if( pP
164fd 61 67 65 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  age==0 ){.      
164fe 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
164ff 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  MEM;.    }.  }el
16500 73 65 20 69 66 28 20 70 50 61 67 65 2d 3e 69 57  se if( pPage->iW
16501 72 69 74 65 3e 3d 70 50 61 67 65 2d 3e 6e 53 6c  rite>=pPage->nSl
16502 6f 74 20 29 7b 0a 20 20 20 20 70 50 61 67 65 2d  ot ){.    pPage-
16503 3e 70 4e 65 78 74 20 3d 20 61 6c 6c 6f 63 61 74  >pNext = allocat
16504 65 46 69 66 6f 50 61 67 65 28 70 46 69 66 6f 2d  eFifoPage(pFifo-
16505 3e 64 62 2c 20 70 46 69 66 6f 2d 3e 6e 45 6e 74  >db, pFifo->nEnt
16506 72 79 29 3b 0a 20 20 20 20 69 66 28 20 70 50 61  ry);.    if( pPa
16507 67 65 2d 3e 70 4e 65 78 74 3d 3d 30 20 29 7b 0a  ge->pNext==0 ){.
16508 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
16509 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d  ITE_NOMEM;.    }
1650a 0a 20 20 20 20 70 50 61 67 65 20 3d 20 70 46 69  .    pPage = pFi
1650b 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 70 50 61 67  fo->pLast = pPag
1650c 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  e->pNext;.  }.  
1650d 70 50 61 67 65 2d 3e 61 53 6c 6f 74 5b 70 50 61  pPage->aSlot[pPa
1650e 67 65 2d 3e 69 57 72 69 74 65 2b 2b 5d 20 3d 20  ge->iWrite++] = 
1650f 76 61 6c 3b 0a 20 20 70 46 69 66 6f 2d 3e 6e 45  val;.  pFifo->nE
16510 6e 74 72 79 2b 2b 3b 0a 20 20 72 65 74 75 72 6e  ntry++;.  return
16511 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
16512 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 61 20 73  *.** Extract a s
16513 69 6e 67 6c 65 20 36 34 2d 62 69 74 20 69 6e 74  ingle 64-bit int
16514 65 67 65 72 20 76 61 6c 75 65 20 66 72 6f 6d 20  eger value from 
16515 74 68 65 20 46 69 66 6f 2e 20 20 54 68 65 20 69  the Fifo.  The i
16516 6e 74 65 67 65 72 0a 2a 2a 20 65 78 74 72 61 63  nteger.** extrac
16517 74 65 64 20 69 73 20 74 68 65 20 6f 6e 65 20 6c  ted is the one l
16518 65 61 73 74 20 72 65 63 65 6e 74 6c 79 20 69 6e  east recently in
16519 73 65 72 74 65 64 2e 20 20 49 66 20 74 68 65 20  serted.  If the 
1651a 46 69 66 6f 20 69 73 20 65 6d 70 74 79 0a 2a 2a  Fifo is empty.**
1651b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44   return SQLITE_D
1651c 4f 4e 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ONE..*/.SQLITE_P
1651d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1651e 65 33 56 64 62 65 46 69 66 6f 50 6f 70 28 46 69  e3VdbeFifoPop(Fi
1651f 66 6f 20 2a 70 46 69 66 6f 2c 20 69 36 34 20 2a  fo *pFifo, i64 *
16520 70 56 61 6c 29 7b 0a 20 20 46 69 66 6f 50 61 67  pVal){.  FifoPag
16521 65 20 2a 70 50 61 67 65 3b 0a 20 20 69 66 28 20  e *pPage;.  if( 
16522 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d 3d 30  pFifo->nEntry==0
16523 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
16524 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20 20 7d 0a  QLITE_DONE;.  }.
16525 20 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d    assert( pFifo-
16526 3e 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20 70  >nEntry>0 );.  p
16527 50 61 67 65 20 3d 20 70 46 69 66 6f 2d 3e 70 46  Page = pFifo->pF
16528 69 72 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20  irst;.  assert( 
16529 70 50 61 67 65 21 3d 30 20 29 3b 0a 20 20 61 73  pPage!=0 );.  as
1652a 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 57 72  sert( pPage->iWr
1652b 69 74 65 3e 70 50 61 67 65 2d 3e 69 52 65 61 64  ite>pPage->iRead
1652c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
1652d 61 67 65 2d 3e 69 57 72 69 74 65 3c 3d 70 50 61  age->iWrite<=pPa
1652e 67 65 2d 3e 6e 53 6c 6f 74 20 29 3b 0a 20 20 61  ge->nSlot );.  a
1652f 73 73 65 72 74 28 20 70 50 61 67 65 2d 3e 69 52  ssert( pPage->iR
16530 65 61 64 3c 70 50 61 67 65 2d 3e 6e 53 6c 6f 74  ead<pPage->nSlot
16531 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50   );.  assert( pP
16532 61 67 65 2d 3e 69 52 65 61 64 3e 3d 30 20 29 3b  age->iRead>=0 );
16533 0a 20 20 2a 70 56 61 6c 20 3d 20 70 50 61 67 65  .  *pVal = pPage
16534 2d 3e 61 53 6c 6f 74 5b 70 50 61 67 65 2d 3e 69  ->aSlot[pPage->i
16535 52 65 61 64 2b 2b 5d 3b 0a 20 20 70 46 69 66 6f  Read++];.  pFifo
16536 2d 3e 6e 45 6e 74 72 79 2d 2d 3b 0a 20 20 69 66  ->nEntry--;.  if
16537 28 20 70 50 61 67 65 2d 3e 69 52 65 61 64 3e 3d  ( pPage->iRead>=
16538 70 50 61 67 65 2d 3e 69 57 72 69 74 65 20 29 7b  pPage->iWrite ){
16539 0a 20 20 20 20 70 46 69 66 6f 2d 3e 70 46 69 72  .    pFifo->pFir
1653a 73 74 20 3d 20 70 50 61 67 65 2d 3e 70 4e 65 78  st = pPage->pNex
1653b 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  t;.    sqlite3Db
1653c 46 72 65 65 28 70 46 69 66 6f 2d 3e 64 62 2c 20  Free(pFifo->db, 
1653d 70 50 61 67 65 29 3b 0a 20 20 20 20 69 66 28 20  pPage);.    if( 
1653e 70 46 69 66 6f 2d 3e 6e 45 6e 74 72 79 3d 3d 30  pFifo->nEntry==0
1653f 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
16540 28 20 70 46 69 66 6f 2d 3e 70 4c 61 73 74 3d 3d  ( pFifo->pLast==
16541 70 50 61 67 65 20 29 3b 0a 20 20 20 20 20 20 70  pPage );.      p
16542 46 69 66 6f 2d 3e 70 4c 61 73 74 20 3d 20 30 3b  Fifo->pLast = 0;
16543 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
16544 20 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d    assert( pFifo-
16545 3e 70 46 69 72 73 74 21 3d 30 20 29 3b 0a 20 20  >pFirst!=0 );.  
16546 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
16547 20 61 73 73 65 72 74 28 20 70 46 69 66 6f 2d 3e   assert( pFifo->
16548 6e 45 6e 74 72 79 3e 30 20 29 3b 0a 20 20 7d 0a  nEntry>0 );.  }.
16549 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
1654a 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c  OK;.}../*.** Del
1654b 65 74 65 20 61 6c 6c 20 69 6e 66 6f 72 6d 61 74  ete all informat
1654c 69 6f 6e 20 66 72 6f 6d 20 61 20 46 69 66 6f 20  ion from a Fifo 
1654d 6f 62 6a 65 63 74 2e 20 20 20 46 72 65 65 20 61  object.   Free a
1654e 6c 6c 20 6d 65 6d 6f 72 79 20 68 65 6c 64 0a 2a  ll memory held.*
1654f 2a 20 62 79 20 74 68 65 20 46 69 66 6f 2e 0a 2a  * by the Fifo..*
16550 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16551 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
16552 65 46 69 66 6f 43 6c 65 61 72 28 46 69 66 6f 20  eFifoClear(Fifo 
16553 2a 70 46 69 66 6f 29 7b 0a 20 20 46 69 66 6f 50  *pFifo){.  FifoP
16554 61 67 65 20 2a 70 50 61 67 65 2c 20 2a 70 4e 65  age *pPage, *pNe
16555 78 74 50 61 67 65 3b 0a 20 20 66 6f 72 28 70 50  xtPage;.  for(pP
16556 61 67 65 3d 70 46 69 66 6f 2d 3e 70 46 69 72 73  age=pFifo->pFirs
16557 74 3b 20 70 50 61 67 65 3b 20 70 50 61 67 65 3d  t; pPage; pPage=
16558 70 4e 65 78 74 50 61 67 65 29 7b 0a 20 20 20 20  pNextPage){.    
16559 70 4e 65 78 74 50 61 67 65 20 3d 20 70 50 61 67  pNextPage = pPag
1655a 65 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 73 71  e->pNext;.    sq
1655b 6c 69 74 65 33 44 62 46 72 65 65 28 70 46 69 66  lite3DbFree(pFif
1655c 6f 2d 3e 64 62 2c 20 70 50 61 67 65 29 3b 0a 20  o->db, pPage);. 
1655d 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65   }.  sqlite3Vdbe
1655e 46 69 66 6f 49 6e 69 74 28 70 46 69 66 6f 2c 20  FifoInit(pFifo, 
1655f 70 46 69 66 6f 2d 3e 64 62 29 3b 0a 7d 0a 0a 2f  pFifo->db);.}../
16560 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
16561 6e 64 20 6f 66 20 76 64 62 65 66 69 66 6f 2e 63  nd of vdbefifo.c
16562 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
16563 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16564 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
16565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
16566 65 67 69 6e 20 66 69 6c 65 20 76 64 62 65 6d 65  egin file vdbeme
16567 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  m.c ************
16568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
1656a 2a 0a 2a 2a 20 32 30 30 34 20 4d 61 79 20 32 36  *.** 2004 May 26
1656b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1656c 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1656d 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1656e 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1656f 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
16570 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
16571 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
16572 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
16573 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
16574 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
16575 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
16576 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
16577 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
16578 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
16579 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1657a 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1657b 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1657c 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1657d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1657e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1657f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
16581 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
16582 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
16583 65 20 74 6f 20 6d 61 6e 69 70 75 6c 61 74 65 20  e to manipulate 
16584 22 4d 65 6d 22 20 73 74 72 75 63 74 75 72 65 2e  "Mem" structure.
16585 20 20 41 20 22 4d 65 6d 22 0a 2a 2a 20 73 74 6f    A "Mem".** sto
16586 72 65 73 20 61 20 73 69 6e 67 6c 65 20 76 61 6c  res a single val
16587 75 65 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20  ue in the VDBE. 
16588 20 4d 65 6d 20 69 73 20 61 6e 20 6f 70 61 71 75   Mem is an opaqu
16589 65 20 73 74 72 75 63 74 75 72 65 20 76 69 73 69  e structure visi
1658a 62 6c 65 0a 2a 2a 20 6f 6e 6c 79 20 77 69 74 68  ble.** only with
1658b 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20 49 6e  in the VDBE.  In
1658c 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65 73  terface routines
1658d 20 72 65 66 65 72 20 74 6f 20 61 20 4d 65 6d 20   refer to a Mem 
1658e 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 6e 61 6d  using the.** nam
1658f 65 20 73 71 6c 69 74 65 5f 76 61 6c 75 65 0a 2a  e sqlite_value.*
16590 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65 6d 65  *.** $Id: vdbeme
16591 6d 2e 63 2c 76 20 31 2e 31 32 36 20 32 30 30 38  m.c,v 1.126 2008
16592 2f 31 31 2f 31 31 20 30 30 3a 32 31 3a 33 30 20  /11/11 00:21:30 
16593 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
16594 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
16595 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
16596 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c  b() on the suppl
16597 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20  ied value (type 
16598 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65  Mem*).** P if re
16599 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69  quired..*/.#defi
1659a 6e 65 20 65 78 70 61 6e 64 42 6c 6f 62 28 50 29  ne expandBlob(P)
1659b 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45   (((P)->flags&ME
1659c 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56  M_Zero)?sqlite3V
1659d 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
1659e 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 49 66  (P):0)../*.** If
1659f 20 70 4d 65 6d 20 69 73 20 61 6e 20 6f 62 6a 65   pMem is an obje
165a0 63 74 20 77 69 74 68 20 61 20 76 61 6c 69 64 20  ct with a valid 
165a1 73 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74  string represent
165a2 61 74 69 6f 6e 2c 20 74 68 69 73 20 72 6f 75 74  ation, this rout
165a3 69 6e 65 0a 2a 2a 20 65 6e 73 75 72 65 73 20 74  ine.** ensures t
165a4 68 65 20 69 6e 74 65 72 6e 61 6c 20 65 6e 63 6f  he internal enco
165a5 64 69 6e 67 20 66 6f 72 20 74 68 65 20 73 74 72  ding for the str
165a6 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61 74 69  ing representati
165a7 6f 6e 20 69 73 0a 2a 2a 20 27 64 65 73 69 72 65  on is.** 'desire
165a8 64 45 6e 63 27 2c 20 6f 6e 65 20 6f 66 20 53 51  dEnc', one of SQ
165a9 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
165aa 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53 51 4c  E_UTF16LE or SQL
165ab 49 54 45 5f 55 54 46 31 36 42 45 2e 0a 2a 2a 0a  ITE_UTF16BE..**.
165ac 2a 2a 20 49 66 20 70 4d 65 6d 20 69 73 20 6e 6f  ** If pMem is no
165ad 74 20 61 20 73 74 72 69 6e 67 20 6f 62 6a 65 63  t a string objec
165ae 74 2c 20 6f 72 20 74 68 65 20 65 6e 63 6f 64 69  t, or the encodi
165af 6e 67 20 6f 66 20 74 68 65 20 73 74 72 69 6e 67  ng of the string
165b0 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 61 74 69  .** representati
165b1 6f 6e 20 69 73 20 61 6c 72 65 61 64 79 20 73 74  on is already st
165b2 6f 72 65 64 20 75 73 69 6e 67 20 74 68 65 20 72  ored using the r
165b3 65 71 75 65 73 74 65 64 20 65 6e 63 6f 64 69 6e  equested encodin
165b4 67 2c 20 74 68 65 6e 20 74 68 69 73 0a 2a 2a 20  g, then this.** 
165b5 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d  routine is a no-
165b6 6f 70 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45  op..**.** SQLITE
165b7 5f 4f 4b 20 69 73 20 72 65 74 75 72 6e 65 64 20  _OK is returned 
165b8 69 66 20 74 68 65 20 63 6f 6e 76 65 72 73 69 6f  if the conversio
165b9 6e 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 20  n is successful 
165ba 28 6f 72 20 6e 6f 74 20 72 65 71 75 69 72 65 64  (or not required
165bb 29 2e 0a 2a 2a 20 53 51 4c 49 54 45 5f 4e 4f 4d  )..** SQLITE_NOM
165bc 45 4d 20 6d 61 79 20 62 65 20 72 65 74 75 72 6e  EM may be return
165bd 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29  ed if a malloc()
165be 20 66 61 69 6c 73 20 64 75 72 69 6e 67 20 63 6f   fails during co
165bf 6e 76 65 72 73 69 6f 6e 0a 2a 2a 20 62 65 74 77  nversion.** betw
165c0 65 65 6e 20 66 6f 72 6d 61 74 73 2e 0a 2a 2f 0a  een formats..*/.
165c1 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
165c2 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  nt sqlite3VdbeCh
165c3 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 4d 65 6d  angeEncoding(Mem
165c4 20 2a 70 4d 65 6d 2c 20 69 6e 74 20 64 65 73 69   *pMem, int desi
165c5 72 65 64 45 6e 63 29 7b 0a 20 20 69 6e 74 20 72  redEnc){.  int r
165c6 63 3b 0a 20 20 69 66 28 20 21 28 70 4d 65 6d 2d  c;.  if( !(pMem-
165c7 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 20  >flags&MEM_Str) 
165c8 7c 7c 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65  || pMem->enc==de
165c9 73 69 72 65 64 45 6e 63 20 29 7b 0a 20 20 20 20  siredEnc ){.    
165ca 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
165cb 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
165cc 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
165cd 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
165ce 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
165cf 78 29 20 29 3b 0a 23 69 66 64 65 66 20 53 51 4c  x) );.#ifdef SQL
165d0 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20  ITE_OMIT_UTF16. 
165d1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
165d2 52 52 4f 52 3b 0a 23 65 6c 73 65 0a 0a 20 20 2f  RROR;.#else..  /
165d3 2a 20 4d 65 6d 54 72 61 6e 73 6c 61 74 65 28 29  * MemTranslate()
165d4 20 6d 61 79 20 72 65 74 75 72 6e 20 53 51 4c 49   may return SQLI
165d5 54 45 5f 4f 4b 20 6f 72 20 53 51 4c 49 54 45 5f  TE_OK or SQLITE_
165d6 4e 4f 4d 45 4d 2e 20 49 66 20 4e 4f 4d 45 4d 20  NOMEM. If NOMEM 
165d7 69 73 20 72 65 74 75 72 6e 65 64 2c 0a 20 20 2a  is returned,.  *
165d8 2a 20 74 68 65 6e 20 74 68 65 20 65 6e 63 6f 64  * then the encod
165d9 69 6e 67 20 6f 66 20 74 68 65 20 76 61 6c 75 65  ing of the value
165da 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 63 68   may not have ch
165db 61 6e 67 65 64 2e 0a 20 20 2a 2f 0a 20 20 72 63  anged..  */.  rc
165dc 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65   = sqlite3VdbeMe
165dd 6d 54 72 61 6e 73 6c 61 74 65 28 70 4d 65 6d 2c  mTranslate(pMem,
165de 20 64 65 73 69 72 65 64 45 6e 63 29 3b 0a 20 20   desiredEnc);.  
165df 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
165e0 45 5f 4f 4b 20 20 20 20 7c 7c 20 72 63 3d 3d 53  E_OK    || rc==S
165e1 51 4c 49 54 45 5f 4e 4f 4d 45 4d 29 3b 0a 20 20  QLITE_NOMEM);.  
165e2 61 73 73 65 72 74 28 72 63 3d 3d 53 51 4c 49 54  assert(rc==SQLIT
165e3 45 5f 4f 4b 20 20 20 20 7c 7c 20 70 4d 65 6d 2d  E_OK    || pMem-
165e4 3e 65 6e 63 21 3d 64 65 73 69 72 65 64 45 6e 63  >enc!=desiredEnc
165e5 29 3b 0a 20 20 61 73 73 65 72 74 28 72 63 3d 3d  );.  assert(rc==
165e6 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
165e7 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 64 65 73 69 72  pMem->enc==desir
165e8 65 64 45 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e  edEnc);.  return
165e9 20 72 63 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f   rc;.#endif.}../
165ea 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 70  *.** Make sure p
165eb 4d 65 6d 2d 3e 7a 20 70 6f 69 6e 74 73 20 74 6f  Mem->z points to
165ec 20 61 20 77 72 69 74 61 62 6c 65 20 61 6c 6c 6f   a writable allo
165ed 63 61 74 69 6f 6e 20 6f 66 20 61 74 20 6c 65 61  cation of at lea
165ee 73 74 20 0a 2a 2a 20 6e 20 62 79 74 65 73 2e 0a  st .** n bytes..
165ef 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d  **.** If the mem
165f0 6f 72 79 20 63 65 6c 6c 20 63 75 72 72 65 6e 74  ory cell current
165f1 6c 79 20 63 6f 6e 74 61 69 6e 73 20 73 74 72 69  ly contains stri
165f2 6e 67 20 6f 72 20 62 6c 6f 62 20 64 61 74 61 0a  ng or blob data.
165f3 2a 2a 20 61 6e 64 20 74 68 65 20 74 68 69 72 64  ** and the third
165f4 20 61 72 67 75 6d 65 6e 74 20 70 61 73 73 65 64   argument passed
165f5 20 74 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f   to this functio
165f6 6e 20 69 73 20 74 72 75 65 2c 20 74 68 65 20 0a  n is true, the .
165f7 2a 2a 20 63 75 72 72 65 6e 74 20 63 6f 6e 74 65  ** current conte
165f8 6e 74 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 69  nt of the cell i
165f9 73 20 70 72 65 73 65 72 76 65 64 2e 20 4f 74 68  s preserved. Oth
165fa 65 72 77 69 73 65 2c 20 69 74 20 6d 61 79 0a 2a  erwise, it may.*
165fb 2a 20 62 65 20 64 69 73 63 61 72 64 65 64 2e 20  * be discarded. 
165fc 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e   .**.** This fun
165fd 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65 20 4d  ction sets the M
165fe 45 4d 5f 44 79 6e 20 66 6c 61 67 20 61 6e 64 20  EM_Dyn flag and 
165ff 63 6c 65 61 72 73 20 61 6e 79 20 78 44 65 6c 20  clears any xDel 
16600 63 61 6c 6c 62 61 63 6b 2e 0a 2a 2a 20 49 74 20  callback..** It 
16601 61 6c 73 6f 20 63 6c 65 61 72 73 20 4d 45 4d 5f  also clears MEM_
16602 45 70 68 65 6d 20 61 6e 64 20 4d 45 4d 5f 53 74  Ephem and MEM_St
16603 61 74 69 63 2e 20 49 66 20 74 68 65 20 70 72 65  atic. If the pre
16604 73 65 72 76 65 20 66 6c 61 67 20 69 73 20 0a 2a  serve flag is .*
16605 2a 20 6e 6f 74 20 73 65 74 2c 20 4d 65 6d 2e 6e  * not set, Mem.n
16606 20 69 73 20 7a 65 72 6f 65 64 2e 0a 2a 2f 0a 53   is zeroed..*/.S
16607 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16608 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
16609 47 72 6f 77 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Grow(Mem *pMem, 
1660a 69 6e 74 20 6e 2c 20 69 6e 74 20 70 72 65 73 65  int n, int prese
1660b 72 76 65 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rve){.  assert( 
1660c 31 20 3e 3d 0a 20 20 20 20 28 28 70 4d 65 6d 2d  1 >=.    ((pMem-
1660d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20 70 4d 65 6d  >zMalloc && pMem
1660e 2d 3e 7a 4d 61 6c 6c 6f 63 3d 3d 70 4d 65 6d 2d  ->zMalloc==pMem-
1660f 3e 7a 29 20 3f 20 31 20 3a 20 30 29 20 2b 0a 20  >z) ? 1 : 0) +. 
16610 20 20 20 28 28 28 70 4d 65 6d 2d 3e 66 6c 61 67     (((pMem->flag
16611 73 26 4d 45 4d 5f 44 79 6e 29 26 26 70 4d 65 6d  s&MEM_Dyn)&&pMem
16612 2d 3e 78 44 65 6c 29 20 3f 20 31 20 3a 20 30 29  ->xDel) ? 1 : 0)
16613 20 2b 20 0a 20 20 20 20 28 28 70 4d 65 6d 2d 3e   + .    ((pMem->
16614 66 6c 61 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29  flags&MEM_Ephem)
16615 20 3f 20 31 20 3a 20 30 29 20 2b 20 0a 20 20 20   ? 1 : 0) + .   
16616 20 28 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d   ((pMem->flags&M
16617 45 4d 5f 53 74 61 74 69 63 29 20 3f 20 31 20 3a  EM_Static) ? 1 :
16618 20 30 29 0a 20 20 29 3b 0a 0a 20 20 69 66 28 20   0).  );..  if( 
16619 6e 3c 33 32 20 29 20 6e 20 3d 20 33 32 3b 0a 20  n<32 ) n = 32;. 
1661a 20 69 66 28 20 73 71 6c 69 74 65 33 44 62 4d 61   if( sqlite3DbMa
1661b 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d 3e 64  llocSize(pMem->d
1661c 62 2c 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63  b, pMem->zMalloc
1661d 29 3c 6e 20 29 7b 0a 20 20 20 20 69 66 28 20 70  )<n ){.    if( p
1661e 72 65 73 65 72 76 65 20 26 26 20 70 4d 65 6d 2d  reserve && pMem-
1661f 3e 7a 3d 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  >z==pMem->zMallo
16620 63 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d 2d  c ){.      pMem-
16621 3e 7a 20 3d 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  >z = pMem->zMall
16622 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65  oc = sqlite3DbRe
16623 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 4d 65 6d  allocOrFree(pMem
16624 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 6e  ->db, pMem->z, n
16625 29 3b 0a 20 20 20 20 20 20 70 72 65 73 65 72 76  );.      preserv
16626 65 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65  e = 0;.    }else
16627 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
16628 62 46 72 65 65 28 70 4d 65 6d 2d 3e 64 62 2c 20  bFree(pMem->db, 
16629 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a  pMem->zMalloc);.
1662a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c        pMem->zMal
1662b 6c 6f 63 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  loc = sqlite3DbM
1662c 61 6c 6c 6f 63 52 61 77 28 70 4d 65 6d 2d 3e 64  allocRaw(pMem->d
1662d 62 2c 20 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  b, n);.    }.  }
1662e 0a 0a 20 20 69 66 28 20 70 72 65 73 65 72 76 65  ..  if( preserve
1662f 20 26 26 20 70 4d 65 6d 2d 3e 7a 20 26 26 20 70   && pMem->z && p
16630 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 26 26 20  Mem->zMalloc && 
16631 70 4d 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a  pMem->z!=pMem->z
16632 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 6d 65  Malloc ){.    me
16633 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c  mcpy(pMem->zMall
16634 6f 63 2c 20 70 4d 65 6d 2d 3e 7a 2c 20 70 4d 65  oc, pMem->z, pMe
16635 6d 2d 3e 6e 29 3b 0a 20 20 7d 0a 20 20 69 66 28  m->n);.  }.  if(
16636 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d   pMem->flags&MEM
16637 5f 44 79 6e 20 26 26 20 70 4d 65 6d 2d 3e 78 44  _Dyn && pMem->xD
16638 65 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  el ){.    pMem->
16639 78 44 65 6c 28 28 76 6f 69 64 20 2a 29 28 70 4d  xDel((void *)(pM
1663a 65 6d 2d 3e 7a 29 29 3b 0a 20 20 7d 0a 0a 20 20  em->z));.  }..  
1663b 70 4d 65 6d 2d 3e 7a 20 3d 20 70 4d 65 6d 2d 3e  pMem->z = pMem->
1663c 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 66 28 20 70  zMalloc;.  if( p
1663d 4d 65 6d 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  Mem->z==0 ){.   
1663e 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d   pMem->flags = M
1663f 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 65 6c 73 65  EM_Null;.  }else
16640 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  {.    pMem->flag
16641 73 20 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d  s &= ~(MEM_Ephem
16642 7c 4d 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20  |MEM_Static);.  
16643 7d 0a 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d  }.  pMem->xDel =
16644 20 30 3b 0a 20 20 72 65 74 75 72 6e 20 28 70 4d   0;.  return (pM
16645 65 6d 2d 3e 7a 20 3f 20 53 51 4c 49 54 45 5f 4f  em->z ? SQLITE_O
16646 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  K : SQLITE_NOMEM
16647 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65  );.}../*.** Make
16648 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 20 6f   the given Mem o
16649 62 6a 65 63 74 20 4d 45 4d 5f 44 79 6e 2e 20 20  bject MEM_Dyn.  
1664a 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20  In other words, 
1664b 6d 61 6b 65 20 69 74 20 73 6f 0a 2a 2a 20 74 68  make it so.** th
1664c 61 74 20 61 6e 79 20 54 45 58 54 20 6f 72 20 42  at any TEXT or B
1664d 4c 4f 42 20 63 6f 6e 74 65 6e 74 20 69 73 20 73  LOB content is s
1664e 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72 79 20  tored in memory 
1664f 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a 2a  obtained from.**
16650 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 49 6e 20 74   malloc().  In t
16651 68 69 73 20 77 61 79 2c 20 77 65 20 6b 6e 6f 77  his way, we know
16652 20 74 68 61 74 20 74 68 65 20 6d 65 6d 6f 72 79   that the memory
16653 20 69 73 20 73 61 66 65 20 74 6f 20 62 65 0a 2a   is safe to be.*
16654 2a 20 6f 76 65 72 77 72 69 74 74 65 6e 20 6f 72  * overwritten or
16655 20 61 6c 74 65 72 65 64 2e 0a 2a 2a 0a 2a 2a 20   altered..**.** 
16656 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
16657 20 6f 6e 20 73 75 63 63 65 73 73 20 6f 72 20 53   on success or S
16658 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 69 66 20 6d  QLITE_NOMEM if m
16659 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a  alloc fails..*/.
1665a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1665b 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
1665c 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c 65 28 4d  mMakeWriteable(M
1665d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74  em *pMem){.  int
1665e 20 66 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4d   f;.  assert( pM
1665f 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71 6c  em->db==0 || sql
16660 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
16661 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  pMem->db->mutex)
16662 20 29 3b 0a 20 20 65 78 70 61 6e 64 42 6c 6f 62   );.  expandBlob
16663 28 70 4d 65 6d 29 3b 0a 20 20 66 20 3d 20 70 4d  (pMem);.  f = pM
16664 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20 69 66 28  em->flags;.  if(
16665 20 28 66 26 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d   (f&(MEM_Str|MEM
16666 5f 42 6c 6f 62 29 29 20 26 26 20 70 4d 65 6d 2d  _Blob)) && pMem-
16667 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f  >z!=pMem->zMallo
16668 63 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  c ){.    if( sql
16669 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
1666a 70 4d 65 6d 2c 20 70 4d 65 6d 2d 3e 6e 20 2b 20  pMem, pMem->n + 
1666b 32 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72  2, 1) ){.      r
1666c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
1666d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  EM;.    }.    pM
1666e 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 20 3d  em->z[pMem->n] =
1666f 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b   0;.    pMem->z[
16670 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b 0a  pMem->n+1] = 0;.
16671 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
16672 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d  |= MEM_Term;.  }
16673 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
16674 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  E_OK;.}../*.** I
16675 66 20 74 68 65 20 67 69 76 65 6e 20 4d 65 6d 2a  f the given Mem*
16676 20 68 61 73 20 61 20 7a 65 72 6f 2d 66 69 6c 6c   has a zero-fill
16677 65 64 20 74 61 69 6c 2c 20 74 75 72 6e 20 69 74  ed tail, turn it
16678 20 69 6e 74 6f 20 61 6e 20 6f 72 64 69 6e 61 72   into an ordinar
16679 79 0a 2a 2a 20 62 6c 6f 62 20 73 74 6f 72 65 64  y.** blob stored
1667a 20 69 6e 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   in dynamically 
1667b 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 2e  allocated space.
1667c 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
1667d 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f 42  TE_OMIT_INCRBLOB
1667e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1667f 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
16680 65 6d 45 78 70 61 6e 64 42 6c 6f 62 28 4d 65 6d  emExpandBlob(Mem
16681 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 66 28 20 70   *pMem){.  if( p
16682 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
16683 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20 69 6e 74  _Zero ){.    int
16684 20 6e 42 79 74 65 3b 0a 20 20 20 20 61 73 73 65   nByte;.    asse
16685 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26  rt( pMem->flags&
16686 4d 45 4d 5f 42 6c 6f 62 20 29 3b 0a 20 20 20 20  MEM_Blob );.    
16687 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
16688 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
16689 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
1668a 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
1668b 20 20 20 2f 2a 20 53 65 74 20 6e 42 79 74 65 20     /* Set nByte 
1668c 74 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  to the number of
1668d 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64 20   bytes required 
1668e 74 6f 20 73 74 6f 72 65 20 74 68 65 20 65 78 70  to store the exp
1668f 61 6e 64 65 64 20 62 6c 6f 62 2e 20 2a 2f 0a 20  anded blob. */. 
16690 20 20 20 6e 42 79 74 65 20 3d 20 70 4d 65 6d 2d     nByte = pMem-
16691 3e 6e 20 2b 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a  >n + pMem->u.i;.
16692 20 20 20 20 69 66 28 20 6e 42 79 74 65 3c 3d 30      if( nByte<=0
16693 20 29 7b 0a 20 20 20 20 20 20 6e 42 79 74 65 20   ){.      nByte 
16694 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  = 1;.    }.    i
16695 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
16696 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e 42 79 74  mGrow(pMem, nByt
16697 65 2c 20 31 29 20 29 7b 0a 20 20 20 20 20 20 72  e, 1) ){.      r
16698 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
16699 45 4d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6d  EM;.    }..    m
1669a 65 6d 73 65 74 28 26 70 4d 65 6d 2d 3e 7a 5b 70  emset(&pMem->z[p
1669b 4d 65 6d 2d 3e 6e 5d 2c 20 30 2c 20 70 4d 65 6d  Mem->n], 0, pMem
1669c 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 70 4d 65 6d  ->u.i);.    pMem
1669d 2d 3e 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69  ->n += pMem->u.i
1669e 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
1669f 73 20 26 3d 20 7e 28 4d 45 4d 5f 5a 65 72 6f 7c  s &= ~(MEM_Zero|
166a0 4d 45 4d 5f 54 65 72 6d 29 3b 0a 20 20 7d 0a 20  MEM_Term);.  }. 
166a1 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
166a2 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a  K;.}.#endif.../*
166a3 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68  .** Make sure th
166a4 65 20 67 69 76 65 6e 20 4d 65 6d 20 69 73 20 5c  e given Mem is \
166a5 75 30 30 30 30 20 74 65 72 6d 69 6e 61 74 65 64  u0000 terminated
166a6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
166a7 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56  ATE int sqlite3V
166a8 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69 6e 61  dbeMemNulTermina
166a9 74 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20  te(Mem *pMem){. 
166aa 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
166ab 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
166ac 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
166ad 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
166ae 20 69 66 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67   if( (pMem->flag
166af 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 21 3d 30  s & MEM_Term)!=0
166b0 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73   || (pMem->flags
166b1 20 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29   & MEM_Str)==0 )
166b2 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
166b3 49 54 45 5f 4f 4b 3b 20 20 20 2f 2a 20 4e 6f 74  ITE_OK;   /* Not
166b4 68 69 6e 67 20 74 6f 20 64 6f 20 2a 2f 0a 20 20  hing to do */.  
166b5 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  }.  if( sqlite3V
166b6 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c  dbeMemGrow(pMem,
166b7 20 70 4d 65 6d 2d 3e 6e 2b 32 2c 20 31 29 20 29   pMem->n+2, 1) )
166b8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
166b9 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20  ITE_NOMEM;.  }. 
166ba 20 70 4d 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e   pMem->z[pMem->n
166bb 5d 20 3d 20 30 3b 0a 20 20 70 4d 65 6d 2d 3e 7a  ] = 0;.  pMem->z
166bc 5b 70 4d 65 6d 2d 3e 6e 2b 31 5d 20 3d 20 30 3b  [pMem->n+1] = 0;
166bd 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 7c  .  pMem->flags |
166be 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 72 65  = MEM_Term;.  re
166bf 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
166c0 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 4d 45 4d  }../*.** Add MEM
166c1 5f 53 74 72 20 74 6f 20 74 68 65 20 73 65 74 20  _Str to the set 
166c2 6f 66 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  of representatio
166c3 6e 73 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e  ns for the given
166c4 20 4d 65 6d 2e 20 20 4e 75 6d 62 65 72 73 0a 2a   Mem.  Numbers.*
166c5 2a 20 61 72 65 20 63 6f 6e 76 65 72 74 65 64 20  * are converted 
166c6 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f 73 6e  using sqlite3_sn
166c7 70 72 69 6e 74 66 28 29 2e 20 20 43 6f 6e 76 65  printf().  Conve
166c8 72 74 69 6e 67 20 61 20 42 4c 4f 42 20 74 6f 20  rting a BLOB to 
166c9 61 20 73 74 72 69 6e 67 0a 2a 2a 20 69 73 20 61  a string.** is a
166ca 20 6e 6f 2d 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78   no-op..**.** Ex
166cb 69 73 74 69 6e 67 20 72 65 70 72 65 73 65 6e 74  isting represent
166cc 61 74 69 6f 6e 73 20 4d 45 4d 5f 49 6e 74 20 61  ations MEM_Int a
166cd 6e 64 20 4d 45 4d 5f 52 65 61 6c 20 61 72 65 20  nd MEM_Real are 
166ce 2a 6e 6f 74 2a 20 69 6e 76 61 6c 69 64 61 74 65  *not* invalidate
166cf 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 4d 45 4d 5f 4e  d..**.** A MEM_N
166d0 75 6c 6c 20 76 61 6c 75 65 20 77 69 6c 6c 20 6e  ull value will n
166d1 65 76 65 72 20 62 65 20 70 61 73 73 65 64 20 74  ever be passed t
166d2 6f 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e  o this function.
166d3 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
166d4 73 0a 2a 2a 20 75 73 65 64 20 66 6f 72 20 63 6f  s.** used for co
166d5 6e 76 65 72 74 69 6e 67 20 76 61 6c 75 65 73 20  nverting values 
166d6 74 6f 20 74 65 78 74 20 66 6f 72 20 72 65 74 75  to text for retu
166d7 72 6e 69 6e 67 20 74 6f 20 74 68 65 20 75 73 65  rning to the use
166d8 72 20 28 69 2e 65 2e 20 76 69 61 0a 2a 2a 20 73  r (i.e. via.** s
166d9 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
166da 74 28 29 29 2c 20 6f 72 20 66 6f 72 20 65 6e 73  t()), or for ens
166db 75 72 69 6e 67 20 74 68 61 74 20 76 61 6c 75 65  uring that value
166dc 73 20 74 6f 20 62 65 20 75 73 65 64 20 61 73 20  s to be used as 
166dd 62 74 72 65 65 0a 2a 2a 20 6b 65 79 73 20 61 72  btree.** keys ar
166de 65 20 73 74 72 69 6e 67 73 2e 20 49 6e 20 74 68  e strings. In th
166df 65 20 66 6f 72 6d 65 72 20 63 61 73 65 20 61 20  e former case a 
166e0 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 20 69 73 20  NULL pointer is 
166e1 72 65 74 75 72 6e 65 64 20 74 68 65 0a 2a 2a 20  returned the.** 
166e2 75 73 65 72 20 61 6e 64 20 74 68 65 20 6c 61 74  user and the lat
166e3 65 72 20 69 73 20 61 6e 20 69 6e 74 65 72 6e 61  er is an interna
166e4 6c 20 70 72 6f 67 72 61 6d 6d 69 6e 67 20 65 72  l programming er
166e5 72 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ror..*/.SQLITE_P
166e6 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
166e7 65 33 56 64 62 65 4d 65 6d 53 74 72 69 6e 67 69  e3VdbeMemStringi
166e8 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69 6e  fy(Mem *pMem, in
166e9 74 20 65 6e 63 29 7b 0a 20 20 69 6e 74 20 72 63  t enc){.  int rc
166ea 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
166eb 69 6e 74 20 66 67 20 3d 20 70 4d 65 6d 2d 3e 66  int fg = pMem->f
166ec 6c 61 67 73 3b 0a 20 20 63 6f 6e 73 74 20 69 6e  lags;.  const in
166ed 74 20 6e 42 79 74 65 20 3d 20 33 32 3b 0a 0a 20  t nByte = 32;.. 
166ee 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64   assert( pMem->d
166ef 62 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f  b==0 || sqlite3_
166f0 6d 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d  mutex_held(pMem-
166f1 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  >db->mutex) );. 
166f2 20 61 73 73 65 72 74 28 20 21 28 66 67 26 4d 45   assert( !(fg&ME
166f3 4d 5f 5a 65 72 6f 29 20 29 3b 0a 20 20 61 73 73  M_Zero) );.  ass
166f4 65 72 74 28 20 21 28 66 67 26 28 4d 45 4d 5f 53  ert( !(fg&(MEM_S
166f5 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 20 29 3b  tr|MEM_Blob)) );
166f6 0a 20 20 61 73 73 65 72 74 28 20 66 67 26 28 4d  .  assert( fg&(M
166f7 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29  EM_Int|MEM_Real)
166f8 20 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74   );..  if( sqlit
166f9 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
166fa 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 20 29 7b  em, nByte, 0) ){
166fb 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
166fc 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
166fd 20 2f 2a 20 46 6f 72 20 61 20 52 65 61 6c 20 6f   /* For a Real o
166fe 72 20 49 6e 74 65 67 65 72 2c 20 75 73 65 20 73  r Integer, use s
166ff 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 29  qlite3_mprintf()
16700 20 74 6f 20 70 72 6f 64 75 63 65 20 74 68 65 20   to produce the 
16701 55 54 46 2d 38 0a 20 20 2a 2a 20 73 74 72 69 6e  UTF-8.  ** strin
16702 67 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e  g representation
16703 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20 54   of the value. T
16704 68 65 6e 2c 20 69 66 20 74 68 65 20 72 65 71 75  hen, if the requ
16705 69 72 65 64 20 65 6e 63 6f 64 69 6e 67 0a 20 20  ired encoding.  
16706 2a 2a 20 69 73 20 55 54 46 2d 31 36 6c 65 20 6f  ** is UTF-16le o
16707 72 20 55 54 46 2d 31 36 62 65 20 64 6f 20 61 20  r UTF-16be do a 
16708 74 72 61 6e 73 6c 61 74 69 6f 6e 2e 0a 20 20 2a  translation..  *
16709 2a 20 0a 20 20 2a 2a 20 46 49 58 20 4d 45 3a 20  * .  ** FIX ME: 
1670a 49 74 20 77 6f 75 6c 64 20 62 65 20 62 65 74 74  It would be bett
1670b 65 72 20 69 66 20 73 71 6c 69 74 65 33 5f 73 6e  er if sqlite3_sn
1670c 70 72 69 6e 74 66 28 29 20 63 6f 75 6c 64 20 64  printf() could d
1670d 6f 20 55 54 46 2d 31 36 2e 0a 20 20 2a 2f 0a 20  o UTF-16..  */. 
1670e 20 69 66 28 20 66 67 20 26 20 4d 45 4d 5f 49 6e   if( fg & MEM_In
1670f 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  t ){.    sqlite3
16710 5f 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 2c  _snprintf(nByte,
16711 20 70 4d 65 6d 2d 3e 7a 2c 20 22 25 6c 6c 64 22   pMem->z, "%lld"
16712 2c 20 70 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20  , pMem->u.i);.  
16713 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
16714 74 28 20 66 67 20 26 20 4d 45 4d 5f 52 65 61 6c  t( fg & MEM_Real
16715 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   );.    sqlite3_
16716 73 6e 70 72 69 6e 74 66 28 6e 42 79 74 65 2c 20  snprintf(nByte, 
16717 70 4d 65 6d 2d 3e 7a 2c 20 22 25 21 2e 31 35 67  pMem->z, "%!.15g
16718 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20 20 7d  ", pMem->r);.  }
16719 0a 20 20 70 4d 65 6d 2d 3e 6e 20 3d 20 73 74 72  .  pMem->n = str
1671a 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20  len(pMem->z);.  
1671b 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
1671c 54 45 5f 55 54 46 38 3b 0a 20 20 70 4d 65 6d 2d  TE_UTF8;.  pMem-
1671d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 53 74  >flags |= MEM_St
1671e 72 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 73 71  r|MEM_Term;.  sq
1671f 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45  lite3VdbeChangeE
16720 6e 63 6f 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e  ncoding(pMem, en
16721 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  c);.  return rc;
16722 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 6d 6f 72 79  .}../*.** Memory
16723 20 63 65 6c 6c 20 70 4d 65 6d 20 63 6f 6e 74 61   cell pMem conta
16724 69 6e 73 20 74 68 65 20 63 6f 6e 74 65 78 74 20  ins the context 
16725 6f 66 20 61 6e 20 61 67 67 72 65 67 61 74 65 20  of an aggregate 
16726 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69  function..** Thi
16727 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73 20  s routine calls 
16728 74 68 65 20 66 69 6e 61 6c 69 7a 65 20 6d 65 74  the finalize met
16729 68 6f 64 20 66 6f 72 20 74 68 61 74 20 66 75 6e  hod for that fun
1672a 63 74 69 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 72  ction.  The.** r
1672b 65 73 75 6c 74 20 6f 66 20 74 68 65 20 61 67 67  esult of the agg
1672c 72 65 67 61 74 65 20 69 73 20 73 74 6f 72 65 64  regate is stored
1672d 20 62 61 63 6b 20 69 6e 74 6f 20 70 4d 65 6d 2e   back into pMem.
1672e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
1672f 4c 49 54 45 5f 45 52 52 4f 52 20 69 66 20 74 68  LITE_ERROR if th
16730 65 20 66 69 6e 61 6c 69 7a 65 72 20 72 65 70 6f  e finalizer repo
16731 72 74 73 20 61 6e 20 65 72 72 6f 72 2e 20 20 53  rts an error.  S
16732 51 4c 49 54 45 5f 4f 4b 0a 2a 2a 20 6f 74 68 65  QLITE_OK.** othe
16733 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rwise..*/.SQLITE
16734 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
16735 69 74 65 33 56 64 62 65 4d 65 6d 46 69 6e 61 6c  ite3VdbeMemFinal
16736 69 7a 65 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 46  ize(Mem *pMem, F
16737 75 6e 63 44 65 66 20 2a 70 46 75 6e 63 29 7b 0a  uncDef *pFunc){.
16738 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
16739 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 70 46 75 6e  E_OK;.  if( pFun
1673a 63 20 26 26 20 70 46 75 6e 63 2d 3e 78 46 69 6e  c && pFunc->xFin
1673b 61 6c 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c  alize ){.    sql
1673c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 63 74 78  ite3_context ctx
1673d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ;.    assert( (p
1673e 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1673f 5f 4e 75 6c 6c 29 21 3d 30 20 7c 7c 20 70 46 75  _Null)!=0 || pFu
16740 6e 63 3d 3d 70 4d 65 6d 2d 3e 75 2e 70 44 65 66  nc==pMem->u.pDef
16741 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
16742 70 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73  pMem->db==0 || s
16743 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
16744 64 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65  d(pMem->db->mute
16745 78 29 20 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  x) );.    memset
16746 28 26 63 74 78 2c 20 30 2c 20 73 69 7a 65 6f 66  (&ctx, 0, sizeof
16747 28 63 74 78 29 29 3b 0a 20 20 20 20 63 74 78 2e  (ctx));.    ctx.
16748 73 2e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75  s.flags = MEM_Nu
16749 6c 6c 3b 0a 20 20 20 20 63 74 78 2e 73 2e 64 62  ll;.    ctx.s.db
1674a 20 3d 20 70 4d 65 6d 2d 3e 64 62 3b 0a 20 20 20   = pMem->db;.   
1674b 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d 65 6d   ctx.pMem = pMem
1674c 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20  ;.    ctx.pFunc 
1674d 3d 20 70 46 75 6e 63 3b 0a 20 20 20 20 70 46 75  = pFunc;.    pFu
1674e 6e 63 2d 3e 78 46 69 6e 61 6c 69 7a 65 28 26 63  nc->xFinalize(&c
1674f 74 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  tx);.    assert(
16750 20 30 3d 3d 28 70 4d 65 6d 2d 3e 66 6c 61 67 73   0==(pMem->flags
16751 26 4d 45 4d 5f 44 79 6e 29 20 26 26 20 21 70 4d  &MEM_Dyn) && !pM
16752 65 6d 2d 3e 78 44 65 6c 20 29 3b 0a 20 20 20 20  em->xDel );.    
16753 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 4d  sqlite3DbFree(pM
16754 65 6d 2d 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 4d  em->db, pMem->zM
16755 61 6c 6c 6f 63 29 3b 0a 20 20 20 20 2a 70 4d 65  alloc);.    *pMe
16756 6d 20 3d 20 63 74 78 2e 73 3b 0a 20 20 20 20 72  m = ctx.s;.    r
16757 63 20 3d 20 28 63 74 78 2e 69 73 45 72 72 6f 72  c = (ctx.isError
16758 3f 53 51 4c 49 54 45 5f 45 52 52 4f 52 3a 53 51  ?SQLITE_ERROR:SQ
16759 4c 49 54 45 5f 4f 4b 29 3b 0a 20 20 7d 0a 20 20  LITE_OK);.  }.  
1675a 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1675b 0a 2a 2a 20 49 66 20 74 68 65 20 6d 65 6d 6f 72  .** If the memor
1675c 79 20 63 65 6c 6c 20 63 6f 6e 74 61 69 6e 73 20  y cell contains 
1675d 61 20 73 74 72 69 6e 67 20 76 61 6c 75 65 20 74  a string value t
1675e 68 61 74 20 6d 75 73 74 20 62 65 20 66 72 65 65  hat must be free
1675f 64 20 62 79 0a 2a 2a 20 69 6e 76 6f 6b 69 6e 67  d by.** invoking
16760 20 61 6e 20 65 78 74 65 72 6e 61 6c 20 63 61 6c   an external cal
16761 6c 62 61 63 6b 2c 20 66 72 65 65 20 69 74 20 6e  lback, free it n
16762 6f 77 2e 20 43 61 6c 6c 69 6e 67 20 74 68 69 73  ow. Calling this
16763 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 64 6f 65   function.** doe
16764 73 20 6e 6f 74 20 66 72 65 65 20 61 6e 79 20 4d  s not free any M
16765 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 62 75 66 66 65  em.zMalloc buffe
16766 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  r..*/.SQLITE_PRI
16767 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
16768 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
16769 78 74 65 72 6e 61 6c 28 4d 65 6d 20 2a 70 29 7b  xternal(Mem *p){
1676a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62  .  assert( p->db
1676b 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
1676c 75 74 65 78 5f 68 65 6c 64 28 70 2d 3e 64 62 2d  utex_held(p->db-
1676d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
1676e 20 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 41 67   p->flags&MEM_Ag
1676f 67 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  g ){.    sqlite3
16770 56 64 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28  VdbeMemFinalize(
16771 70 2c 20 70 2d 3e 75 2e 70 44 65 66 29 3b 0a 20  p, p->u.pDef);. 
16772 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 66     assert( (p->f
16773 6c 61 67 73 20 26 20 4d 45 4d 5f 41 67 67 29 3d  lags & MEM_Agg)=
16774 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  =0 );.    sqlite
16775 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
16776 70 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  p);.  }else if( 
16777 70 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 44 79 6e  p->flags&MEM_Dyn
16778 20 26 26 20 70 2d 3e 78 44 65 6c 20 29 7b 0a 20   && p->xDel ){. 
16779 20 20 20 70 2d 3e 78 44 65 6c 28 28 76 6f 69 64     p->xDel((void
1677a 20 2a 29 70 2d 3e 7a 29 3b 0a 20 20 20 20 70 2d   *)p->z);.    p-
1677b 3e 78 44 65 6c 20 3d 20 30 3b 0a 20 20 7d 0a 7d  >xDel = 0;.  }.}
1677c 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65 20  ../*.** Release 
1677d 61 6e 79 20 6d 65 6d 6f 72 79 20 68 65 6c 64 20  any memory held 
1677e 62 79 20 74 68 65 20 4d 65 6d 2e 20 54 68 69 73  by the Mem. This
1677f 20 6d 61 79 20 6c 65 61 76 65 20 74 68 65 20 4d   may leave the M
16780 65 6d 20 69 6e 20 61 6e 0a 2a 2a 20 69 6e 63 6f  em in an.** inco
16781 6e 73 69 73 74 65 6e 74 20 73 74 61 74 65 2c 20  nsistent state, 
16782 66 6f 72 20 65 78 61 6d 70 6c 65 20 77 69 74 68  for example with
16783 20 28 4d 65 6d 2e 7a 3d 3d 30 29 20 61 6e 64 0a   (Mem.z==0) and.
16784 2a 2a 20 28 4d 65 6d 2e 74 79 70 65 3d 3d 53 51  ** (Mem.type==SQ
16785 4c 49 54 45 5f 54 45 58 54 29 2e 0a 2a 2f 0a 53  LITE_TEXT)..*/.S
16786 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
16787 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  id sqlite3VdbeMe
16788 6d 52 65 6c 65 61 73 65 28 4d 65 6d 20 2a 70 29  mRelease(Mem *p)
16789 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  {.  sqlite3VdbeM
1678a 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1678b 6c 28 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  l(p);.  sqlite3D
1678c 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e  bFree(p->db, p->
1678d 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 70 2d 3e 7a  zMalloc);.  p->z
1678e 20 3d 20 30 3b 0a 20 20 70 2d 3e 7a 4d 61 6c 6c   = 0;.  p->zMall
1678f 6f 63 20 3d 20 30 3b 0a 20 20 70 2d 3e 78 44 65  oc = 0;.  p->xDe
16790 6c 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  l = 0;.}../*.** 
16791 43 6f 6e 76 65 72 74 20 61 20 36 34 2d 62 69 74  Convert a 64-bit
16792 20 49 45 45 45 20 64 6f 75 62 6c 65 20 69 6e 74   IEEE double int
16793 6f 20 61 20 36 34 2d 62 69 74 20 73 69 67 6e 65  o a 64-bit signe
16794 64 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49 66  d integer..** If
16795 20 74 68 65 20 64 6f 75 62 6c 65 20 69 73 20 74   the double is t
16796 6f 6f 20 6c 61 72 67 65 2c 20 72 65 74 75 72 6e  oo large, return
16797 20 30 78 38 30 30 30 30 30 30 30 30 30 30 30 30   0x8000000000000
16798 30 30 30 2e 0a 2a 2a 0a 2a 2a 20 4d 6f 73 74 20  000..**.** Most 
16799 73 79 73 74 65 6d 73 20 61 70 70 65 61 72 20 74  systems appear t
1679a 6f 20 64 6f 20 74 68 69 73 20 73 69 6d 70 6c 79  o do this simply
1679b 20 62 79 20 61 73 73 69 67 6e 69 6e 67 0a 2a 2a   by assigning.**
1679c 20 76 61 72 69 61 62 6c 65 73 20 61 6e 64 20 77   variables and w
1679d 69 74 68 6f 75 74 20 74 68 65 20 65 78 74 72 61  ithout the extra
1679e 20 72 61 6e 67 65 20 74 65 73 74 73 2e 20 20 42   range tests.  B
1679f 75 74 0a 2a 2a 20 74 68 65 72 65 20 61 72 65 20  ut.** there are 
167a0 72 65 70 6f 72 74 73 20 74 68 61 74 20 77 69 6e  reports that win
167a1 64 6f 77 73 20 74 68 72 6f 77 73 20 61 6e 20 65  dows throws an e
167a2 78 70 65 63 74 69 6f 6e 0a 2a 2a 20 69 66 20 74  xpection.** if t
167a3 68 65 20 66 6c 6f 61 74 69 6e 67 20 70 6f 69 6e  he floating poin
167a4 74 20 76 61 6c 75 65 20 69 73 20 6f 75 74 20 6f  t value is out o
167a5 66 20 72 61 6e 67 65 2e 20 28 53 65 65 20 74 69  f range. (See ti
167a6 63 6b 65 74 20 23 32 38 38 30 2e 29 0a 2a 2a 20  cket #2880.).** 
167a7 42 65 63 61 75 73 65 20 77 65 20 64 6f 20 6e 6f  Because we do no
167a8 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 75 6e 64  t completely und
167a9 65 72 73 74 61 6e 64 20 74 68 65 20 70 72 6f 62  erstand the prob
167aa 6c 65 6d 2c 20 77 65 20 77 69 6c 6c 0a 2a 2a 20  lem, we will.** 
167ab 74 61 6b 65 20 74 68 65 20 63 6f 6e 73 65 72 76  take the conserv
167ac 61 74 69 76 65 20 61 70 70 72 6f 61 63 68 20 61  ative approach a
167ad 6e 64 20 61 6c 77 61 79 73 20 64 6f 20 72 61 6e  nd always do ran
167ae 67 65 20 74 65 73 74 73 0a 2a 2a 20 62 65 66 6f  ge tests.** befo
167af 72 65 20 61 74 74 65 6d 70 74 69 6e 67 20 74 68  re attempting th
167b0 65 20 63 6f 6e 76 65 72 73 69 6f 6e 2e 0a 2a 2f  e conversion..*/
167b1 0a 73 74 61 74 69 63 20 69 36 34 20 64 6f 75 62  .static i64 doub
167b2 6c 65 54 6f 49 6e 74 36 34 28 64 6f 75 62 6c 65  leToInt64(double
167b3 20 72 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 4d   r){.  /*.  ** M
167b4 61 6e 79 20 63 6f 6d 70 69 6c 65 72 73 20 77 65  any compilers we
167b5 20 65 6e 63 6f 75 6e 74 65 72 20 64 6f 20 6e 6f   encounter do no
167b6 74 20 64 65 66 69 6e 65 20 63 6f 6e 73 74 61 6e  t define constan
167b7 74 73 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a 20  ts for the.  ** 
167b8 6d 69 6e 69 6d 75 6d 20 61 6e 64 20 6d 61 78 69  minimum and maxi
167b9 6d 75 6d 20 36 34 2d 62 69 74 20 69 6e 74 65 67  mum 64-bit integ
167ba 65 72 73 2c 20 6f 72 20 74 68 65 79 20 64 65 66  ers, or they def
167bb 69 6e 65 20 74 68 65 6d 0a 20 20 2a 2a 20 69 6e  ine them.  ** in
167bc 63 6f 6e 73 69 73 74 65 6e 74 6c 79 2e 20 20 41  consistently.  A
167bd 6e 64 20 6d 61 6e 79 20 64 6f 20 6e 6f 74 20 75  nd many do not u
167be 6e 64 65 72 73 74 61 6e 64 20 74 68 65 20 22 4c  nderstand the "L
167bf 4c 22 20 6e 6f 74 61 74 69 6f 6e 2e 0a 20 20 2a  L" notation..  *
167c0 2a 20 53 6f 20 77 65 20 64 65 66 69 6e 65 20 6f  * So we define o
167c1 75 72 20 6f 77 6e 20 73 74 61 74 69 63 20 63 6f  ur own static co
167c2 6e 73 74 61 6e 74 73 20 68 65 72 65 20 75 73 69  nstants here usi
167c3 6e 67 20 6e 6f 74 68 69 6e 67 0a 20 20 2a 2a 20  ng nothing.  ** 
167c4 6c 61 72 67 65 72 20 74 68 61 6e 20 61 20 33 32  larger than a 32
167c5 2d 62 69 74 20 69 6e 74 65 67 65 72 20 63 6f 6e  -bit integer con
167c6 73 74 61 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 74  stant..  */.  st
167c7 61 74 69 63 20 63 6f 6e 73 74 20 69 36 34 20 6d  atic const i64 m
167c8 61 78 49 6e 74 20 3d 20 4c 41 52 47 45 53 54 5f  axInt = LARGEST_
167c9 49 4e 54 36 34 3b 0a 20 20 73 74 61 74 69 63 20  INT64;.  static 
167ca 63 6f 6e 73 74 20 69 36 34 20 6d 69 6e 49 6e 74  const i64 minInt
167cb 20 3d 20 53 4d 41 4c 4c 45 53 54 5f 49 4e 54 36   = SMALLEST_INT6
167cc 34 3b 0a 0a 20 20 69 66 28 20 72 3c 28 64 6f 75  4;..  if( r<(dou
167cd 62 6c 65 29 6d 69 6e 49 6e 74 20 29 7b 0a 20 20  ble)minInt ){.  
167ce 20 20 72 65 74 75 72 6e 20 6d 69 6e 49 6e 74 3b    return minInt;
167cf 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 3e 28  .  }else if( r>(
167d0 64 6f 75 62 6c 65 29 6d 61 78 49 6e 74 20 29 7b  double)maxInt ){
167d1 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 69 6e 49  .    return minI
167d2 6e 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  nt;.  }else{.   
167d3 20 72 65 74 75 72 6e 20 28 69 36 34 29 72 3b 0a   return (i64)r;.
167d4 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74    }.}../*.** Ret
167d5 75 72 6e 20 73 6f 6d 65 20 6b 69 6e 64 20 6f 66  urn some kind of
167d6 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 77   integer value w
167d7 68 69 63 68 20 69 73 20 74 68 65 20 62 65 73 74  hich is the best
167d8 20 77 65 20 63 61 6e 20 64 6f 0a 2a 2a 20 61 74   we can do.** at
167d9 20 72 65 70 72 65 73 65 6e 74 69 6e 67 20 74 68   representing th
167da 65 20 76 61 6c 75 65 20 74 68 61 74 20 2a 70 4d  e value that *pM
167db 65 6d 20 64 65 73 63 72 69 62 65 73 20 61 73 20  em describes as 
167dc 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20 49  an integer..** I
167dd 66 20 70 4d 65 6d 20 69 73 20 61 6e 20 69 6e 74  f pMem is an int
167de 65 67 65 72 2c 20 74 68 65 6e 20 74 68 65 20 76  eger, then the v
167df 61 6c 75 65 20 69 73 20 65 78 61 63 74 2e 20 20  alue is exact.  
167e0 49 66 20 70 4d 65 6d 20 69 73 0a 2a 2a 20 61 20  If pMem is.** a 
167e1 66 6c 6f 61 74 69 6e 67 2d 70 6f 69 6e 74 20 74  floating-point t
167e2 68 65 6e 20 74 68 65 20 76 61 6c 75 65 20 72 65  hen the value re
167e3 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 69 6e  turned is the in
167e4 74 65 67 65 72 20 70 61 72 74 2e 0a 2a 2a 20 49  teger part..** I
167e5 66 20 70 4d 65 6d 20 69 73 20 61 20 73 74 72 69  f pMem is a stri
167e6 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e  ng or blob, then
167e7 20 77 65 20 6d 61 6b 65 20 61 6e 20 61 74 74 65   we make an atte
167e8 6d 70 74 20 74 6f 20 63 6f 6e 76 65 72 74 0a 2a  mpt to convert.*
167e9 2a 20 69 74 20 69 6e 74 6f 20 61 20 69 6e 74 65  * it into a inte
167ea 67 65 72 20 61 6e 64 20 72 65 74 75 72 6e 20 74  ger and return t
167eb 68 61 74 2e 20 20 49 66 20 70 4d 65 6d 20 69 73  hat.  If pMem is
167ec 20 4e 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e   NULL, return 0.
167ed 0a 2a 2a 0a 2a 2a 20 49 66 20 70 4d 65 6d 20 69  .**.** If pMem i
167ee 73 20 61 20 73 74 72 69 6e 67 2c 20 69 74 73 20  s a string, its 
167ef 65 6e 63 6f 64 69 6e 67 20 6d 69 67 68 74 20 62  encoding might b
167f0 65 20 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51  e changed..*/.SQ
167f1 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 36 34  LITE_PRIVATE i64
167f2 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
167f3 61 6c 75 65 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b  alue(Mem *pMem){
167f4 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20  .  int flags;.  
167f5 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
167f6 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
167f7 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
167f8 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
167f9 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c  flags = pMem->fl
167fa 61 67 73 3b 0a 20 20 69 66 28 20 66 6c 61 67 73  ags;.  if( flags
167fb 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
167fc 20 20 72 65 74 75 72 6e 20 70 4d 65 6d 2d 3e 75    return pMem->u
167fd 2e 69 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  .i;.  }else if( 
167fe 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61 6c  flags & MEM_Real
167ff 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 64   ){.    return d
16800 6f 75 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65  oubleToInt64(pMe
16801 6d 2d 3e 72 29 3b 0a 20 20 7d 65 6c 73 65 20 69  m->r);.  }else i
16802 66 28 20 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  f( flags & (MEM_
16803 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b  Str|MEM_Blob) ){
16804 0a 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a  .    i64 value;.
16805 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20      pMem->flags 
16806 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20 20 20  |= MEM_Str;.    
16807 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 43  if( sqlite3VdbeC
16808 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 4d  hangeEncoding(pM
16809 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29  em, SQLITE_UTF8)
1680a 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c 69 74  .       || sqlit
1680b 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d  e3VdbeMemNulTerm
1680c 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b 0a 20  inate(pMem) ){. 
1680d 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
1680e 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72 74 28     }.    assert(
1680f 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a 20 20 20 20   pMem->z );.    
16810 73 71 6c 69 74 65 33 41 74 6f 69 36 34 28 70 4d  sqlite3Atoi64(pM
16811 65 6d 2d 3e 7a 2c 20 26 76 61 6c 75 65 29 3b 0a  em->z, &value);.
16812 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 75 65      return value
16813 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
16814 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a  eturn 0;.  }.}..
16815 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
16816 20 62 65 73 74 20 72 65 70 72 65 73 65 6e 74 61   best representa
16817 74 69 6f 6e 20 6f 66 20 70 4d 65 6d 20 74 68 61  tion of pMem tha
16818 74 20 77 65 20 63 61 6e 20 67 65 74 20 69 6e 74  t we can get int
16819 6f 20 61 0a 2a 2a 20 64 6f 75 62 6c 65 2e 20 20  o a.** double.  
1681a 49 66 20 70 4d 65 6d 20 69 73 20 61 6c 72 65 61  If pMem is alrea
1681b 64 79 20 61 20 64 6f 75 62 6c 65 20 6f 72 20 61  dy a double or a
1681c 6e 20 69 6e 74 65 67 65 72 2c 20 72 65 74 75 72  n integer, retur
1681d 6e 20 69 74 73 0a 2a 2a 20 76 61 6c 75 65 2e 20  n its.** value. 
1681e 20 49 66 20 69 74 20 69 73 20 61 20 73 74 72 69   If it is a stri
1681f 6e 67 20 6f 72 20 62 6c 6f 62 2c 20 74 72 79 20  ng or blob, try 
16820 74 6f 20 63 6f 6e 76 65 72 74 20 69 74 20 74 6f  to convert it to
16821 20 61 20 64 6f 75 62 6c 65 2e 0a 2a 2a 20 49 66   a double..** If
16822 20 69 74 20 69 73 20 61 20 4e 55 4c 4c 2c 20 72   it is a NULL, r
16823 65 74 75 72 6e 20 30 2e 30 2e 0a 2a 2f 0a 53 51  eturn 0.0..*/.SQ
16824 4c 49 54 45 5f 50 52 49 56 41 54 45 20 64 6f 75  LITE_PRIVATE dou
16825 62 6c 65 20 73 71 6c 69 74 65 33 56 64 62 65 52  ble sqlite3VdbeR
16826 65 61 6c 56 61 6c 75 65 28 4d 65 6d 20 2a 70 4d  ealValue(Mem *pM
16827 65 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70  em){.  assert( p
16828 4d 65 6d 2d 3e 64 62 3d 3d 30 20 7c 7c 20 73 71  Mem->db==0 || sq
16829 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
1682a 28 70 4d 65 6d 2d 3e 64 62 2d 3e 6d 75 74 65 78  (pMem->db->mutex
1682b 29 20 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d  ) );.  if( pMem-
1682c 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
1682d 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
1682e 70 4d 65 6d 2d 3e 72 3b 0a 20 20 7d 65 6c 73 65  pMem->r;.  }else
1682f 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   if( pMem->flags
16830 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20   & MEM_Int ){.  
16831 20 20 72 65 74 75 72 6e 20 28 64 6f 75 62 6c 65    return (double
16832 29 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20 7d 65  )pMem->u.i;.  }e
16833 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c  lse if( pMem->fl
16834 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
16835 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20  EM_Blob) ){.    
16836 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 30 2e 30  double val = 0.0
16837 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67  ;.    pMem->flag
16838 73 20 7c 3d 20 4d 45 4d 5f 53 74 72 3b 0a 20 20  s |= MEM_Str;.  
16839 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1683a 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28  eChangeEncoding(
1683b 70 4d 65 6d 2c 20 53 51 4c 49 54 45 5f 55 54 46  pMem, SQLITE_UTF
1683c 38 29 0a 20 20 20 20 20 20 20 7c 7c 20 73 71 6c  8).       || sql
1683d 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
1683e 72 6d 69 6e 61 74 65 28 70 4d 65 6d 29 20 29 7b  rminate(pMem) ){
1683f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 2e  .      return 0.
16840 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73  0;.    }.    ass
16841 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 20 29 3b 0a  ert( pMem->z );.
16842 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28      sqlite3AtoF(
16843 70 4d 65 6d 2d 3e 7a 2c 20 26 76 61 6c 29 3b 0a  pMem->z, &val);.
16844 20 20 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a      return val;.
16845 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74    }else{.    ret
16846 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 7d 0a 0a  urn 0.0;.  }.}..
16847 2f 2a 0a 2a 2a 20 54 68 65 20 4d 45 4d 20 73 74  /*.** The MEM st
16848 72 75 63 74 75 72 65 20 69 73 20 61 6c 72 65 61  ructure is alrea
16849 64 79 20 61 20 4d 45 4d 5f 52 65 61 6c 2e 20 20  dy a MEM_Real.  
1684a 54 72 79 20 74 6f 20 61 6c 73 6f 20 6d 61 6b 65  Try to also make
1684b 20 69 74 20 61 0a 2a 2a 20 4d 45 4d 5f 49 6e 74   it a.** MEM_Int
1684c 20 69 66 20 77 65 20 63 61 6e 2e 0a 2a 2f 0a 53   if we can..*/.S
1684d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1684e 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e  id sqlite3VdbeIn
1684f 74 65 67 65 72 41 66 66 69 6e 69 74 79 28 4d 65  tegerAffinity(Me
16850 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73 65  m *pMem){.  asse
16851 72 74 28 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20  rt( pMem->flags 
16852 26 20 4d 45 4d 5f 52 65 61 6c 20 29 3b 0a 20 20  & MEM_Real );.  
16853 61 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62  assert( pMem->db
16854 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d  ==0 || sqlite3_m
16855 75 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e  utex_held(pMem->
16856 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20  db->mutex) );.. 
16857 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 64 6f 75   pMem->u.i = dou
16858 62 6c 65 54 6f 49 6e 74 36 34 28 70 4d 65 6d 2d  bleToInt64(pMem-
16859 3e 72 29 3b 0a 20 20 69 66 28 20 70 4d 65 6d 2d  >r);.  if( pMem-
1685a 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 70 4d 65 6d  >r==(double)pMem
1685b 2d 3e 75 2e 69 20 29 7b 0a 20 20 20 20 70 4d 65  ->u.i ){.    pMe
1685c 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f  m->flags |= MEM_
1685d 49 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 73 74 61 74  Int;.  }.}..stat
1685e 69 63 20 76 6f 69 64 20 73 65 74 54 79 70 65 46  ic void setTypeF
1685f 6c 61 67 28 4d 65 6d 20 2a 70 4d 65 6d 2c 20 69  lag(Mem *pMem, i
16860 6e 74 20 66 29 7b 0a 20 20 4d 65 6d 53 65 74 54  nt f){.  MemSetT
16861 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 66 29  ypeFlag(pMem, f)
16862 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76 65  ;.}../*.** Conve
16863 72 74 20 70 4d 65 6d 20 74 6f 20 74 79 70 65 20  rt pMem to type 
16864 69 6e 74 65 67 65 72 2e 20 20 49 6e 76 61 6c 69  integer.  Invali
16865 64 61 74 65 20 61 6e 79 20 70 72 69 6f 72 20 72  date any prior r
16866 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 73 2e 0a  epresentations..
16867 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16868 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
16869 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79 28 4d  eMemIntegerify(M
1686a 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61 73 73  em *pMem){.  ass
1686b 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
1686c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
1686d 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
1686e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 4d 65  >mutex) );.  pMe
1686f 6d 2d 3e 75 2e 69 20 3d 20 73 71 6c 69 74 65 33  m->u.i = sqlite3
16870 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70 4d 65  VdbeIntValue(pMe
16871 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 6c 61  m);.  setTypeFla
16872 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 49 6e 74 29  g(pMem, MEM_Int)
16873 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
16874 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  E_OK;.}../*.** C
16875 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f 20 74  onvert pMem so t
16876 68 61 74 20 69 74 20 69 73 20 6f 66 20 74 79 70  hat it is of typ
16877 65 20 4d 45 4d 5f 52 65 61 6c 2e 0a 2a 2a 20 49  e MEM_Real..** I
16878 6e 76 61 6c 69 64 61 74 65 20 61 6e 79 20 70 72  nvalidate any pr
16879 69 6f 72 20 72 65 70 72 65 73 65 6e 74 61 74 69  ior representati
1687a 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ons..*/.SQLITE_P
1687b 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1687c 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69 66 79  e3VdbeMemRealify
1687d 28 4d 65 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 61  (Mem *pMem){.  a
1687e 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
1687f 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
16880 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
16881 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70  b->mutex) );.  p
16882 4d 65 6d 2d 3e 72 20 3d 20 73 71 6c 69 74 65 33  Mem->r = sqlite3
16883 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28 70 4d  VdbeRealValue(pM
16884 65 6d 29 3b 0a 20 20 73 65 74 54 79 70 65 46 6c  em);.  setTypeFl
16885 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f 52 65 61  ag(pMem, MEM_Rea
16886 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  l);.  return SQL
16887 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
16888 20 43 6f 6e 76 65 72 74 20 70 4d 65 6d 20 73 6f   Convert pMem so
16889 20 74 68 61 74 20 69 74 20 68 61 73 20 74 79 70   that it has typ
1688a 65 73 20 4d 45 4d 5f 52 65 61 6c 20 6f 72 20 4d  es MEM_Real or M
1688b 45 4d 5f 49 6e 74 20 6f 72 20 62 6f 74 68 2e 0a  EM_Int or both..
1688c 2a 2a 20 49 6e 76 61 6c 69 64 61 74 65 20 61 6e  ** Invalidate an
1688d 79 20 70 72 69 6f 72 20 72 65 70 72 65 73 65 6e  y prior represen
1688e 74 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c 49  tations..*/.SQLI
1688f 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
16890 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6d  qlite3VdbeMemNum
16891 65 72 69 66 79 28 4d 65 6d 20 2a 70 4d 65 6d 29  erify(Mem *pMem)
16892 7b 0a 20 20 64 6f 75 62 6c 65 20 72 31 2c 20 72  {.  double r1, r
16893 32 3b 0a 20 20 69 36 34 20 69 3b 0a 20 20 61 73  2;.  i64 i;.  as
16894 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
16895 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45  gs & (MEM_Int|ME
16896 4d 5f 52 65 61 6c 7c 4d 45 4d 5f 4e 75 6c 6c 29  M_Real|MEM_Null)
16897 29 3d 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  )==0 );.  assert
16898 28 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26  ( (pMem->flags &
16899 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53   (MEM_Blob|MEM_S
1689a 74 72 29 29 21 3d 30 20 29 3b 0a 20 20 61 73 73  tr))!=0 );.  ass
1689b 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d 3d 30  ert( pMem->db==0
1689c 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65   || sqlite3_mute
1689d 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64 62 2d  x_held(pMem->db-
1689e 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 72 31 20  >mutex) );.  r1 
1689f 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  = sqlite3VdbeRea
168a0 6c 56 61 6c 75 65 28 70 4d 65 6d 29 3b 0a 20 20  lValue(pMem);.  
168a1 69 20 3d 20 64 6f 75 62 6c 65 54 6f 49 6e 74 36  i = doubleToInt6
168a2 34 28 72 31 29 3b 0a 20 20 72 32 20 3d 20 28 64  4(r1);.  r2 = (d
168a3 6f 75 62 6c 65 29 69 3b 0a 20 20 69 66 28 20 72  ouble)i;.  if( r
168a4 31 3d 3d 72 32 20 29 7b 0a 20 20 20 20 73 71 6c  1==r2 ){.    sql
168a5 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
168a6 65 72 69 66 79 28 70 4d 65 6d 29 3b 0a 20 20 7d  erify(pMem);.  }
168a7 65 6c 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e  else{.    pMem->
168a8 72 20 3d 20 72 31 3b 0a 20 20 20 20 73 65 74 54  r = r1;.    setT
168a9 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45  ypeFlag(pMem, ME
168aa 4d 5f 52 65 61 6c 29 3b 0a 20 20 7d 0a 20 20 72  M_Real);.  }.  r
168ab 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
168ac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65  .}../*.** Delete
168ad 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20 76 61   any previous va
168ae 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68 65 20  lue and set the 
168af 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69 6e 20  value stored in 
168b0 2a 70 4d 65 6d 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  *pMem to NULL..*
168b1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
168b2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
168b3 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 4d 65 6d 20  eMemSetNull(Mem 
168b4 2a 70 4d 65 6d 29 7b 0a 20 20 73 65 74 54 79 70  *pMem){.  setTyp
168b5 65 46 6c 61 67 28 70 4d 65 6d 2c 20 4d 45 4d 5f  eFlag(pMem, MEM_
168b6 4e 75 6c 6c 29 3b 0a 20 20 70 4d 65 6d 2d 3e 74  Null);.  pMem->t
168b7 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55 4c  ype = SQLITE_NUL
168b8 4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 6c 65  L;.}../*.** Dele
168b9 74 65 20 61 6e 79 20 70 72 65 76 69 6f 75 73 20  te any previous 
168ba 76 61 6c 75 65 20 61 6e 64 20 73 65 74 20 74 68  value and set th
168bb 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 61 20  e value to be a 
168bc 42 4c 4f 42 20 6f 66 20 6c 65 6e 67 74 68 0a 2a  BLOB of length.*
168bd 2a 20 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61  * n containing a
168be 6c 6c 20 7a 65 72 6f 73 2e 0a 2a 2f 0a 53 51 4c  ll zeros..*/.SQL
168bf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
168c0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
168c1 65 74 5a 65 72 6f 42 6c 6f 62 28 4d 65 6d 20 2a  etZeroBlob(Mem *
168c2 70 4d 65 6d 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  pMem, int n){.  
168c3 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
168c4 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 73  lease(pMem);.  s
168c5 65 74 54 79 70 65 46 6c 61 67 28 70 4d 65 6d 2c  etTypeFlag(pMem,
168c6 20 4d 45 4d 5f 42 6c 6f 62 29 3b 0a 20 20 70 4d   MEM_Blob);.  pM
168c7 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
168c8 42 6c 6f 62 7c 4d 45 4d 5f 5a 65 72 6f 3b 0a 20  Blob|MEM_Zero;. 
168c9 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
168ca 4c 49 54 45 5f 42 4c 4f 42 3b 0a 20 20 70 4d 65  LITE_BLOB;.  pMe
168cb 6d 2d 3e 6e 20 3d 20 30 3b 0a 20 20 69 66 28 20  m->n = 0;.  if( 
168cc 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 70  n<0 ) n = 0;.  p
168cd 4d 65 6d 2d 3e 75 2e 69 20 3d 20 6e 3b 0a 20 20  Mem->u.i = n;.  
168ce 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pMem->enc = SQLI
168cf 54 45 5f 55 54 46 38 3b 0a 7d 0a 0a 2f 2a 0a 2a  TE_UTF8;.}../*.*
168d0 2a 20 44 65 6c 65 74 65 20 61 6e 79 20 70 72 65  * Delete any pre
168d1 76 69 6f 75 73 20 76 61 6c 75 65 20 61 6e 64 20  vious value and 
168d2 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 73 74  set the value st
168d3 6f 72 65 64 20 69 6e 20 2a 70 4d 65 6d 20 74 6f  ored in *pMem to
168d4 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e 69 66 65 73   val,.** manifes
168d5 74 20 74 79 70 65 20 49 4e 54 45 47 45 52 2e 0a  t type INTEGER..
168d6 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
168d7 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
168d8 62 65 4d 65 6d 53 65 74 49 6e 74 36 34 28 4d 65  beMemSetInt64(Me
168d9 6d 20 2a 70 4d 65 6d 2c 20 69 36 34 20 76 61 6c  m *pMem, i64 val
168da 29 7b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  ){.  sqlite3Vdbe
168db 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
168dc 3b 0a 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20  ;.  pMem->u.i = 
168dd 76 61 6c 3b 0a 20 20 70 4d 65 6d 2d 3e 66 6c 61  val;.  pMem->fla
168de 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20  gs = MEM_Int;.  
168df 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
168e0 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 7d 0a 0a  ITE_INTEGER;.}..
168e1 2f 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 79  /*.** Delete any
168e2 20 70 72 65 76 69 6f 75 73 20 76 61 6c 75 65 20   previous value 
168e3 61 6e 64 20 73 65 74 20 74 68 65 20 76 61 6c 75  and set the valu
168e4 65 20 73 74 6f 72 65 64 20 69 6e 20 2a 70 4d 65  e stored in *pMe
168e5 6d 20 74 6f 20 76 61 6c 2c 0a 2a 2a 20 6d 61 6e  m to val,.** man
168e6 69 66 65 73 74 20 74 79 70 65 20 52 45 41 4c 2e  ifest type REAL.
168e7 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
168e8 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
168e9 64 62 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28  dbeMemSetDouble(
168ea 4d 65 6d 20 2a 70 4d 65 6d 2c 20 64 6f 75 62 6c  Mem *pMem, doubl
168eb 65 20 76 61 6c 29 7b 0a 20 20 69 66 28 20 73 71  e val){.  if( sq
168ec 6c 69 74 65 33 49 73 4e 61 4e 28 76 61 6c 29 20  lite3IsNaN(val) 
168ed 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
168ee 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 4d 65  beMemSetNull(pMe
168ef 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  m);.  }else{.   
168f0 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
168f1 65 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20  elease(pMem);.  
168f2 20 20 70 4d 65 6d 2d 3e 72 20 3d 20 76 61 6c 3b    pMem->r = val;
168f3 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73  .    pMem->flags
168f4 20 3d 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 20   = MEM_Real;.   
168f5 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
168f6 4c 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a  LITE_FLOAT;.  }.
168f7 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
168f8 74 72 75 65 20 69 66 20 74 68 65 20 4d 65 6d 20  true if the Mem 
168f9 6f 62 6a 65 63 74 20 63 6f 6e 74 61 69 6e 73 20  object contains 
168fa 61 20 54 45 58 54 20 6f 72 20 42 4c 4f 42 20 74  a TEXT or BLOB t
168fb 68 61 74 20 69 73 0a 2a 2a 20 74 6f 6f 20 6c 61  hat is.** too la
168fc 72 67 65 20 2d 20 77 68 6f 73 65 20 73 69 7a 65  rge - whose size
168fd 20 65 78 63 65 65 64 73 20 53 51 4c 49 54 45 5f   exceeds SQLITE_
168fe 4d 41 58 5f 4c 45 4e 47 54 48 2e 0a 2a 2f 0a 53  MAX_LENGTH..*/.S
168ff 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16900 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  t sqlite3VdbeMem
16901 54 6f 6f 42 69 67 28 4d 65 6d 20 2a 70 29 7b 0a  TooBig(Mem *p){.
16902 20 20 61 73 73 65 72 74 28 20 70 2d 3e 64 62 21    assert( p->db!
16903 3d 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 66  =0 );.  if( p->f
16904 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c  lags & (MEM_Str|
16905 4d 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20  MEM_Blob) ){.   
16906 20 69 6e 74 20 6e 20 3d 20 70 2d 3e 6e 3b 0a 20   int n = p->n;. 
16907 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20     if( p->flags 
16908 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
16909 20 20 20 20 6e 20 2b 3d 20 70 2d 3e 75 2e 69 3b      n += p->u.i;
1690a 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
1690b 6e 20 6e 3e 70 2d 3e 64 62 2d 3e 61 4c 69 6d 69  n n>p->db->aLimi
1690c 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1690d 45 4e 47 54 48 5d 3b 0a 20 20 7d 0a 20 20 72 65  ENGTH];.  }.  re
1690e 74 75 72 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a  turn 0; .}../*.*
1690f 2a 20 53 69 7a 65 20 6f 66 20 73 74 72 75 63 74  * Size of struct
16910 20 4d 65 6d 20 6e 6f 74 20 69 6e 63 6c 75 64 69   Mem not includi
16911 6e 67 20 74 68 65 20 4d 65 6d 2e 7a 4d 61 6c 6c  ng the Mem.zMall
16912 6f 63 20 6d 65 6d 62 65 72 2e 0a 2a 2f 0a 23 64  oc member..*/.#d
16913 65 66 69 6e 65 20 4d 45 4d 43 45 4c 4c 53 49 5a  efine MEMCELLSIZ
16914 45 20 28 73 69 7a 65 5f 74 29 28 26 28 28 28 4d  E (size_t)(&(((M
16915 65 6d 20 2a 29 30 29 2d 3e 7a 4d 61 6c 6c 6f 63  em *)0)->zMalloc
16916 29 29 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61  ))../*.** Make a
16917 6e 20 73 68 61 6c 6c 6f 77 20 63 6f 70 79 20 6f  n shallow copy o
16918 66 20 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f  f pFrom into pTo
16919 2e 20 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74  .  Prior content
1691a 73 20 6f 66 0a 2a 2a 20 70 54 6f 20 61 72 65 20  s of.** pTo are 
1691b 66 72 65 65 64 2e 20 20 54 68 65 20 70 46 72 6f  freed.  The pFro
1691c 6d 2d 3e 7a 20 66 69 65 6c 64 20 69 73 20 6e 6f  m->z field is no
1691d 74 20 64 75 70 6c 69 63 61 74 65 64 2e 20 20 49  t duplicated.  I
1691e 66 0a 2a 2a 20 70 46 72 6f 6d 2d 3e 7a 20 69 73  f.** pFrom->z is
1691f 20 75 73 65 64 2c 20 74 68 65 6e 20 70 54 6f 2d   used, then pTo-
16920 3e 7a 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65  >z points to the
16921 20 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 70   same thing as p
16922 46 72 6f 6d 2d 3e 7a 0a 2a 2a 20 61 6e 64 20 66  From->z.** and f
16923 6c 61 67 73 20 67 65 74 73 20 73 72 63 54 79 70  lags gets srcTyp
16924 65 20 28 65 69 74 68 65 72 20 4d 45 4d 5f 45 70  e (either MEM_Ep
16925 68 65 6d 20 6f 72 20 4d 45 4d 5f 53 74 61 74 69  hem or MEM_Stati
16926 63 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  c)..*/.SQLITE_PR
16927 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
16928 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
16929 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20 63  Copy(Mem *pTo, c
1692a 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d 2c  onst Mem *pFrom,
1692b 20 69 6e 74 20 73 72 63 54 79 70 65 29 7b 0a 20   int srcType){. 
1692c 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52   sqlite3VdbeMemR
1692d 65 6c 65 61 73 65 45 78 74 65 72 6e 61 6c 28 70  eleaseExternal(p
1692e 54 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54  To);.  memcpy(pT
1692f 6f 2c 20 70 46 72 6f 6d 2c 20 4d 45 4d 43 45 4c  o, pFrom, MEMCEL
16930 4c 53 49 5a 45 29 3b 0a 20 20 70 54 6f 2d 3e 78  LSIZE);.  pTo->x
16931 44 65 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 28  Del = 0;.  if( (
16932 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  pFrom->flags&MEM
16933 5f 44 79 6e 29 21 3d 30 20 7c 7c 20 70 46 72 6f  _Dyn)!=0 || pFro
16934 6d 2d 3e 7a 3d 3d 70 46 72 6f 6d 2d 3e 7a 4d 61  m->z==pFrom->zMa
16935 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 70 54 6f 2d  lloc ){.    pTo-
16936 3e 66 6c 61 67 73 20 26 3d 20 7e 28 4d 45 4d 5f  >flags &= ~(MEM_
16937 44 79 6e 7c 4d 45 4d 5f 53 74 61 74 69 63 7c 4d  Dyn|MEM_Static|M
16938 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 61  EM_Ephem);.    a
16939 73 73 65 72 74 28 20 73 72 63 54 79 70 65 3d 3d  ssert( srcType==
1693a 4d 45 4d 5f 45 70 68 65 6d 20 7c 7c 20 73 72 63  MEM_Ephem || src
1693b 54 79 70 65 3d 3d 4d 45 4d 5f 53 74 61 74 69 63  Type==MEM_Static
1693c 20 29 3b 0a 20 20 20 20 70 54 6f 2d 3e 66 6c 61   );.    pTo->fla
1693d 67 73 20 7c 3d 20 73 72 63 54 79 70 65 3b 0a 20  gs |= srcType;. 
1693e 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65   }.}../*.** Make
1693f 20 61 20 66 75 6c 6c 20 63 6f 70 79 20 6f 66 20   a full copy of 
16940 70 46 72 6f 6d 20 69 6e 74 6f 20 70 54 6f 2e 20  pFrom into pTo. 
16941 20 50 72 69 6f 72 20 63 6f 6e 74 65 6e 74 73 20   Prior contents 
16942 6f 66 20 70 54 6f 20 61 72 65 0a 2a 2a 20 66 72  of pTo are.** fr
16943 65 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 63  eed before the c
16944 6f 70 79 20 69 73 20 6d 61 64 65 2e 0a 2a 2f 0a  opy is made..*/.
16945 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
16946 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  nt sqlite3VdbeMe
16947 6d 43 6f 70 79 28 4d 65 6d 20 2a 70 54 6f 2c 20  mCopy(Mem *pTo, 
16948 63 6f 6e 73 74 20 4d 65 6d 20 2a 70 46 72 6f 6d  const Mem *pFrom
16949 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1694a 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 73 71 6c 69  LITE_OK;..  sqli
1694b 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
1694c 65 45 78 74 65 72 6e 61 6c 28 70 54 6f 29 3b 0a  eExternal(pTo);.
1694d 20 20 6d 65 6d 63 70 79 28 70 54 6f 2c 20 70 46    memcpy(pTo, pF
1694e 72 6f 6d 2c 20 4d 45 4d 43 45 4c 4c 53 49 5a 45  rom, MEMCELLSIZE
1694f 29 3b 0a 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20  );.  pTo->flags 
16950 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 0a 20 20  &= ~MEM_Dyn;..  
16951 69 66 28 20 70 54 6f 2d 3e 66 6c 61 67 73 26 28  if( pTo->flags&(
16952 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62  MEM_Str|MEM_Blob
16953 29 20 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d  ) ){.    if( 0==
16954 28 70 46 72 6f 6d 2d 3e 66 6c 61 67 73 26 4d 45  (pFrom->flags&ME
16955 4d 5f 53 74 61 74 69 63 29 20 29 7b 0a 20 20 20  M_Static) ){.   
16956 20 20 20 70 54 6f 2d 3e 66 6c 61 67 73 20 7c 3d     pTo->flags |=
16957 20 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20   MEM_Ephem;.    
16958 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
16959 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62  beMemMakeWriteab
1695a 6c 65 28 70 54 6f 29 3b 0a 20 20 20 20 7d 0a 20  le(pTo);.    }. 
1695b 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
1695c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 66  .}../*.** Transf
1695d 65 72 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  er the contents 
1695e 6f 66 20 70 46 72 6f 6d 20 74 6f 20 70 54 6f 2e  of pFrom to pTo.
1695f 20 41 6e 79 20 65 78 69 73 74 69 6e 67 20 76 61   Any existing va
16960 6c 75 65 20 69 6e 20 70 54 6f 20 69 73 0a 2a 2a  lue in pTo is.**
16961 20 66 72 65 65 64 2e 20 49 66 20 70 46 72 6f 6d   freed. If pFrom
16962 20 63 6f 6e 74 61 69 6e 73 20 65 70 68 65 6d 65   contains epheme
16963 72 61 6c 20 64 61 74 61 2c 20 61 20 63 6f 70 79  ral data, a copy
16964 20 69 73 20 6d 61 64 65 2e 0a 2a 2a 0a 2a 2a 20   is made..**.** 
16965 70 46 72 6f 6d 20 63 6f 6e 74 61 69 6e 73 20 61  pFrom contains a
16966 6e 20 53 51 4c 20 4e 55 4c 4c 20 77 68 65 6e 20  n SQL NULL when 
16967 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  this routine ret
16968 75 72 6e 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  urns..*/.SQLITE_
16969 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1696a 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28  ite3VdbeMemMove(
1696b 4d 65 6d 20 2a 70 54 6f 2c 20 4d 65 6d 20 2a 70  Mem *pTo, Mem *p
1696c 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  From){.  assert(
1696d 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c   pFrom->db==0 ||
1696e 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1696f 65 6c 64 28 70 46 72 6f 6d 2d 3e 64 62 2d 3e 6d  eld(pFrom->db->m
16970 75 74 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72  utex) );.  asser
16971 74 28 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c  t( pTo->db==0 ||
16972 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
16973 65 6c 64 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74  eld(pTo->db->mut
16974 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
16975 20 70 46 72 6f 6d 2d 3e 64 62 3d 3d 30 20 7c 7c   pFrom->db==0 ||
16976 20 70 54 6f 2d 3e 64 62 3d 3d 30 20 7c 7c 20 70   pTo->db==0 || p
16977 46 72 6f 6d 2d 3e 64 62 3d 3d 70 54 6f 2d 3e 64  From->db==pTo->d
16978 62 20 29 3b 0a 0a 20 20 73 71 6c 69 74 65 33 56  b );..  sqlite3V
16979 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 54  dbeMemRelease(pT
1697a 6f 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 54 6f  o);.  memcpy(pTo
1697b 2c 20 70 46 72 6f 6d 2c 20 73 69 7a 65 6f 66 28  , pFrom, sizeof(
1697c 4d 65 6d 29 29 3b 0a 20 20 70 46 72 6f 6d 2d 3e  Mem));.  pFrom->
1697d 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
1697e 3b 0a 20 20 70 46 72 6f 6d 2d 3e 78 44 65 6c 20  ;.  pFrom->xDel 
1697f 3d 20 30 3b 0a 20 20 70 46 72 6f 6d 2d 3e 7a 4d  = 0;.  pFrom->zM
16980 61 6c 6c 6f 63 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a  alloc = 0;.}../*
16981 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76  .** Change the v
16982 61 6c 75 65 20 6f 66 20 61 20 4d 65 6d 20 74 6f  alue of a Mem to
16983 20 62 65 20 61 20 73 74 72 69 6e 67 20 6f 72 20   be a string or 
16984 61 20 42 4c 4f 42 2e 0a 2a 2a 0a 2a 2a 20 54 68  a BLOB..**.** Th
16985 65 20 6d 65 6d 6f 72 79 20 6d 61 6e 61 67 65 6d  e memory managem
16986 65 6e 74 20 73 74 72 61 74 65 67 79 20 64 65 70  ent strategy dep
16987 65 6e 64 73 20 6f 6e 20 74 68 65 20 76 61 6c 75  ends on the valu
16988 65 20 6f 66 20 74 68 65 20 78 44 65 6c 0a 2a 2a  e of the xDel.**
16989 20 70 61 72 61 6d 65 74 65 72 2e 20 49 66 20 74   parameter. If t
1698a 68 65 20 76 61 6c 75 65 20 70 61 73 73 65 64 20  he value passed 
1698b 69 73 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49  is SQLITE_TRANSI
1698c 45 4e 54 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a  ENT, then the .*
1698d 2a 20 73 74 72 69 6e 67 20 69 73 20 63 6f 70 69  * string is copi
1698e 65 64 20 69 6e 74 6f 20 61 20 28 70 6f 73 73 69  ed into a (possi
1698f 62 6c 79 20 65 78 69 73 74 69 6e 67 29 20 62 75  bly existing) bu
16990 66 66 65 72 20 6d 61 6e 61 67 65 64 20 62 79 20  ffer managed by 
16991 74 68 65 20 0a 2a 2a 20 4d 65 6d 20 73 74 72 75  the .** Mem stru
16992 63 74 75 72 65 2e 20 4f 74 68 65 72 77 69 73 65  cture. Otherwise
16993 2c 20 61 6e 79 20 65 78 69 73 74 69 6e 67 20 62  , any existing b
16994 75 66 66 65 72 20 69 73 20 66 72 65 65 64 20 61  uffer is freed a
16995 6e 64 20 74 68 65 0a 2a 2a 20 70 6f 69 6e 74 65  nd the.** pointe
16996 72 20 63 6f 70 69 65 64 2e 0a 2a 2f 0a 53 51 4c  r copied..*/.SQL
16997 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16998 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
16999 74 53 74 72 28 0a 20 20 4d 65 6d 20 2a 70 4d 65  tStr(.  Mem *pMe
1699a 6d 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  m,          /* M
1699b 65 6d 6f 72 79 20 63 65 6c 6c 20 74 6f 20 73 65  emory cell to se
1699c 74 20 74 6f 20 73 74 72 69 6e 67 20 76 61 6c 75  t to string valu
1699d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
1699e 72 20 2a 7a 2c 20 20 20 20 20 20 2f 2a 20 53 74  r *z,      /* St
1699f 72 69 6e 67 20 70 6f 69 6e 74 65 72 20 2a 2f 0a  ring pointer */.
169a0 20 20 69 6e 74 20 6e 2c 20 20 20 20 20 20 20 20    int n,        
169a1 20 20 20 20 20 20 2f 2a 20 42 79 74 65 73 20 69        /* Bytes i
169a2 6e 20 73 74 72 69 6e 67 2c 20 6f 72 20 6e 65 67  n string, or neg
169a3 61 74 69 76 65 20 2a 2f 0a 20 20 75 38 20 65 6e  ative */.  u8 en
169a4 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  c,             /
169a5 2a 20 45 6e 63 6f 64 69 6e 67 20 6f 66 20 7a 2e  * Encoding of z.
169a6 20 20 30 20 66 6f 72 20 42 4c 4f 42 73 20 2a 2f    0 for BLOBs */
169a7 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28  .  void (*xDel)(
169a8 76 6f 69 64 2a 29 20 2f 2a 20 44 65 73 74 72 75  void*) /* Destru
169a9 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ctor function */
169aa 0a 29 7b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20  .){.  int nByte 
169ab 3d 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e 65 77  = n;      /* New
169ac 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65 6d 2d   value for pMem-
169ad 3e 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 4c 69 6d  >n */.  int iLim
169ae 69 74 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4d  it;         /* M
169af 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20 73  aximum allowed s
169b0 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 73 69  tring or blob si
169b1 7a 65 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67  ze */.  int flag
169b2 73 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e  s = 0;      /* N
169b3 65 77 20 76 61 6c 75 65 20 66 6f 72 20 70 4d 65  ew value for pMe
169b4 6d 2d 3e 66 6c 61 67 73 20 2a 2f 0a 0a 20 20 61  m->flags */..  a
169b5 73 73 65 72 74 28 20 70 4d 65 6d 2d 3e 64 62 3d  ssert( pMem->db=
169b6 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 5f 6d 75  =0 || sqlite3_mu
169b7 74 65 78 5f 68 65 6c 64 28 70 4d 65 6d 2d 3e 64  tex_held(pMem->d
169b8 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
169b9 2f 2a 20 49 66 20 7a 20 69 73 20 61 20 4e 55 4c  /* If z is a NUL
169ba 4c 20 70 6f 69 6e 74 65 72 2c 20 73 65 74 20 70  L pointer, set p
169bb 4d 65 6d 20 74 6f 20 63 6f 6e 74 61 69 6e 20 61  Mem to contain a
169bc 6e 20 53 51 4c 20 4e 55 4c 4c 2e 20 2a 2f 0a 20  n SQL NULL. */. 
169bd 20 69 66 28 20 21 7a 20 29 7b 0a 20 20 20 20 73   if( !z ){.    s
169be 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
169bf 4e 75 6c 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20  Null(pMem);.    
169c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
169c1 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 4d 65  ;.  }..  if( pMe
169c2 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20 69 4c 69  m->db ){.    iLi
169c3 6d 69 74 20 3d 20 70 4d 65 6d 2d 3e 64 62 2d 3e  mit = pMem->db->
169c4 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
169c5 4d 49 54 5f 4c 45 4e 47 54 48 5d 3b 0a 20 20 7d  MIT_LENGTH];.  }
169c6 65 6c 73 65 7b 0a 20 20 20 20 69 4c 69 6d 69 74  else{.    iLimit
169c7 20 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45   = SQLITE_MAX_LE
169c8 4e 47 54 48 3b 0a 20 20 7d 0a 20 20 66 6c 61 67  NGTH;.  }.  flag
169c9 73 20 3d 20 28 65 6e 63 3d 3d 30 3f 4d 45 4d 5f  s = (enc==0?MEM_
169ca 42 6c 6f 62 3a 4d 45 4d 5f 53 74 72 29 3b 0a 20  Blob:MEM_Str);. 
169cb 20 69 66 28 20 6e 42 79 74 65 3c 30 20 29 7b 0a   if( nByte<0 ){.
169cc 20 20 20 20 61 73 73 65 72 74 28 20 65 6e 63 21      assert( enc!
169cd 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 65 6e  =0 );.    if( en
169ce 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 29  c==SQLITE_UTF8 )
169cf 7b 0a 20 20 20 20 20 20 66 6f 72 28 6e 42 79 74  {.      for(nByt
169d0 65 3d 30 3b 20 6e 42 79 74 65 3c 3d 69 4c 69 6d  e=0; nByte<=iLim
169d1 69 74 20 26 26 20 7a 5b 6e 42 79 74 65 5d 3b 20  it && z[nByte]; 
169d2 6e 42 79 74 65 2b 2b 29 7b 7d 0a 20 20 20 20 7d  nByte++){}.    }
169d3 65 6c 73 65 7b 0a 20 20 20 20 20 20 66 6f 72 28  else{.      for(
169d4 6e 42 79 74 65 3d 30 3b 20 6e 42 79 74 65 3c 3d  nByte=0; nByte<=
169d5 69 4c 69 6d 69 74 20 26 26 20 28 7a 5b 6e 42 79  iLimit && (z[nBy
169d6 74 65 5d 20 7c 20 7a 5b 6e 42 79 74 65 2b 31 5d  te] | z[nByte+1]
169d7 29 3b 20 6e 42 79 74 65 2b 3d 32 29 7b 7d 0a 20  ); nByte+=2){}. 
169d8 20 20 20 7d 0a 20 20 20 20 66 6c 61 67 73 20 7c     }.    flags |
169d9 3d 20 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 7d 0a  = MEM_Term;.  }.
169da 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  .  /* The follow
169db 69 6e 67 20 62 6c 6f 63 6b 20 73 65 74 73 20 74  ing block sets t
169dc 68 65 20 6e 65 77 20 76 61 6c 75 65 73 20 6f 66  he new values of
169dd 20 4d 65 6d 2e 7a 20 61 6e 64 20 4d 65 6d 2e 78   Mem.z and Mem.x
169de 44 65 6c 2e 20 49 74 0a 20 20 2a 2a 20 61 6c 73  Del. It.  ** als
169df 6f 20 73 65 74 73 20 61 20 66 6c 61 67 20 69 6e  o sets a flag in
169e0 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20   local variable 
169e1 22 66 6c 61 67 73 22 20 74 6f 20 69 6e 64 69 63  "flags" to indic
169e2 61 74 65 20 74 68 65 20 6d 65 6d 6f 72 79 0a 20  ate the memory. 
169e3 20 2a 2a 20 6d 61 6e 61 67 65 6d 65 6e 74 20 28   ** management (
169e4 6f 6e 65 20 6f 66 20 4d 45 4d 5f 44 79 6e 20 6f  one of MEM_Dyn o
169e5 72 20 4d 45 4d 5f 53 74 61 74 69 63 29 2e 0a 20  r MEM_Static).. 
169e6 20 2a 2f 0a 20 20 69 66 28 20 78 44 65 6c 3d 3d   */.  if( xDel==
169e7 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54  SQLITE_TRANSIENT
169e8 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c   ){.    int nAll
169e9 6f 63 20 3d 20 6e 42 79 74 65 3b 0a 20 20 20 20  oc = nByte;.    
169ea 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 54 65  if( flags&MEM_Te
169eb 72 6d 20 29 7b 0a 20 20 20 20 20 20 6e 41 6c 6c  rm ){.      nAll
169ec 6f 63 20 2b 3d 20 28 65 6e 63 3d 3d 53 51 4c 49  oc += (enc==SQLI
169ed 54 45 5f 55 54 46 38 3f 31 3a 32 29 3b 0a 20 20  TE_UTF8?1:2);.  
169ee 20 20 7d 0a 20 20 20 20 69 66 28 20 6e 42 79 74    }.    if( nByt
169ef 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  e>iLimit ){.    
169f0 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
169f1 54 4f 4f 42 49 47 3b 0a 20 20 20 20 7d 0a 20 20  TOOBIG;.    }.  
169f2 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
169f3 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
169f4 41 6c 6c 6f 63 2c 20 30 29 20 29 7b 0a 20 20 20  Alloc, 0) ){.   
169f5 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
169f6 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
169f7 20 20 6d 65 6d 63 70 79 28 70 4d 65 6d 2d 3e 7a    memcpy(pMem->z
169f8 2c 20 7a 2c 20 6e 41 6c 6c 6f 63 29 3b 0a 20 20  , z, nAlloc);.  
169f9 7d 65 6c 73 65 20 69 66 28 20 78 44 65 6c 3d 3d  }else if( xDel==
169fa 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29  SQLITE_DYNAMIC )
169fb 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
169fc 65 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d  eMemRelease(pMem
169fd 29 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61  );.    pMem->zMa
169fe 6c 6c 6f 63 20 3d 20 70 4d 65 6d 2d 3e 7a 20 3d  lloc = pMem->z =
169ff 20 28 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20 20   (char *)z;.    
16a00 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30 3b 0a  pMem->xDel = 0;.
16a01 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
16a02 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
16a03 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20 70 4d  se(pMem);.    pM
16a04 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20 2a 29  em->z = (char *)
16a05 7a 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 78 44 65  z;.    pMem->xDe
16a06 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 66 6c  l = xDel;.    fl
16a07 61 67 73 20 7c 3d 20 28 28 78 44 65 6c 3d 3d 53  ags |= ((xDel==S
16a08 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3f 4d 45  QLITE_STATIC)?ME
16a09 4d 5f 53 74 61 74 69 63 3a 4d 45 4d 5f 44 79 6e  M_Static:MEM_Dyn
16a0a 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 6e 42 79  );.  }.  if( nBy
16a0b 74 65 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20  te>iLimit ){.   
16a0c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 54   return SQLITE_T
16a0d 4f 4f 42 49 47 3b 0a 20 20 7d 0a 0a 20 20 70 4d  OOBIG;.  }..  pM
16a0e 65 6d 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b 0a 20  em->n = nByte;. 
16a0f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 66   pMem->flags = f
16a10 6c 61 67 73 3b 0a 20 20 70 4d 65 6d 2d 3e 65 6e  lags;.  pMem->en
16a11 63 20 3d 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51  c = (enc==0 ? SQ
16a12 4c 49 54 45 5f 55 54 46 38 20 3a 20 65 6e 63 29  LITE_UTF8 : enc)
16a13 3b 0a 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d  ;.  pMem->type =
16a14 20 28 65 6e 63 3d 3d 30 20 3f 20 53 51 4c 49 54   (enc==0 ? SQLIT
16a15 45 5f 42 4c 4f 42 20 3a 20 53 51 4c 49 54 45 5f  E_BLOB : SQLITE_
16a16 54 45 58 54 29 3b 0a 0a 23 69 66 6e 64 65 66 20  TEXT);..#ifndef 
16a17 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
16a18 36 0a 20 20 69 66 28 20 70 4d 65 6d 2d 3e 65 6e  6.  if( pMem->en
16a19 63 21 3d 53 51 4c 49 54 45 5f 55 54 46 38 20 26  c!=SQLITE_UTF8 &
16a1a 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  & sqlite3VdbeMem
16a1b 48 61 6e 64 6c 65 42 6f 6d 28 70 4d 65 6d 29 20  HandleBom(pMem) 
16a1c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
16a1d 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
16a1e 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75 72 6e  #endif..  return
16a1f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
16a20 2a 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65  *.** Compare the
16a21 20 76 61 6c 75 65 73 20 63 6f 6e 74 61 69 6e 65   values containe
16a22 64 20 62 79 20 74 68 65 20 74 77 6f 20 6d 65 6d  d by the two mem
16a23 6f 72 79 20 63 65 6c 6c 73 2c 20 72 65 74 75 72  ory cells, retur
16a24 6e 69 6e 67 0a 2a 2a 20 6e 65 67 61 74 69 76 65  ning.** negative
16a25 2c 20 7a 65 72 6f 20 6f 72 20 70 6f 73 69 74 69  , zero or positi
16a26 76 65 20 69 66 20 70 4d 65 6d 31 20 69 73 20 6c  ve if pMem1 is l
16a27 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61 6c 20  ess than, equal 
16a28 74 6f 2c 20 6f 72 20 67 72 65 61 74 65 72 0a 2a  to, or greater.*
16a29 2a 20 74 68 61 6e 20 70 4d 65 6d 32 2e 20 53 6f  * than pMem2. So
16a2a 72 74 69 6e 67 20 6f 72 64 65 72 20 69 73 20 4e  rting order is N
16a2b 55 4c 4c 27 73 20 66 69 72 73 74 2c 20 66 6f 6c  ULL's first, fol
16a2c 6c 6f 77 65 64 20 62 79 20 6e 75 6d 62 65 72 73  lowed by numbers
16a2d 20 28 69 6e 74 65 67 65 72 73 0a 2a 2a 20 61 6e   (integers.** an
16a2e 64 20 72 65 61 6c 73 29 20 73 6f 72 74 65 64 20  d reals) sorted 
16a2f 6e 75 6d 65 72 69 63 61 6c 6c 79 2c 20 66 6f 6c  numerically, fol
16a30 6c 6f 77 65 64 20 62 79 20 74 65 78 74 20 6f 72  lowed by text or
16a31 64 65 72 65 64 20 62 79 20 74 68 65 20 63 6f 6c  dered by the col
16a32 6c 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e  lating.** sequen
16a33 63 65 20 70 43 6f 6c 6c 20 61 6e 64 20 66 69 6e  ce pColl and fin
16a34 61 6c 6c 79 20 62 6c 6f 62 27 73 20 6f 72 64 65  ally blob's orde
16a35 72 65 64 20 62 79 20 6d 65 6d 63 6d 70 28 29 2e  red by memcmp().
16a36 0a 2a 2a 0a 2a 2a 20 54 77 6f 20 4e 55 4c 4c 20  .**.** Two NULL 
16a37 76 61 6c 75 65 73 20 61 72 65 20 63 6f 6e 73 69  values are consi
16a38 64 65 72 65 64 20 65 71 75 61 6c 20 62 79 20 74  dered equal by t
16a39 68 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f  his function..*/
16a3a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16a3b 69 6e 74 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f  int sqlite3MemCo
16a3c 6d 70 61 72 65 28 63 6f 6e 73 74 20 4d 65 6d 20  mpare(const Mem 
16a3d 2a 70 4d 65 6d 31 2c 20 63 6f 6e 73 74 20 4d 65  *pMem1, const Me
16a3e 6d 20 2a 70 4d 65 6d 32 2c 20 63 6f 6e 73 74 20  m *pMem2, const 
16a3f 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b  CollSeq *pColl){
16a40 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74  .  int rc;.  int
16a41 20 66 31 2c 20 66 32 3b 0a 20 20 69 6e 74 20 63   f1, f2;.  int c
16a42 6f 6d 62 69 6e 65 64 5f 66 6c 61 67 73 3b 0a 0a  ombined_flags;..
16a43 20 20 2f 2a 20 49 6e 74 65 72 63 68 61 6e 67 65    /* Interchange
16a44 20 70 4d 65 6d 31 20 61 6e 64 20 70 4d 65 6d 32   pMem1 and pMem2
16a45 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e   if the collatin
16a46 67 20 73 65 71 75 65 6e 63 65 20 73 70 65 63 69  g sequence speci
16a47 66 69 65 73 0a 20 20 2a 2a 20 44 45 53 43 20 6f  fies.  ** DESC o
16a48 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 66 31 20  rder..  */.  f1 
16a49 3d 20 70 4d 65 6d 31 2d 3e 66 6c 61 67 73 3b 0a  = pMem1->flags;.
16a4a 20 20 66 32 20 3d 20 70 4d 65 6d 32 2d 3e 66 6c    f2 = pMem2->fl
16a4b 61 67 73 3b 0a 20 20 63 6f 6d 62 69 6e 65 64 5f  ags;.  combined_
16a4c 66 6c 61 67 73 20 3d 20 66 31 7c 66 32 3b 0a 20  flags = f1|f2;. 
16a4d 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c  .  /* If one val
16a4e 75 65 20 69 73 20 4e 55 4c 4c 2c 20 69 74 20 69  ue is NULL, it i
16a4f 73 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20  s less than the 
16a50 6f 74 68 65 72 2e 20 49 66 20 62 6f 74 68 20 76  other. If both v
16a51 61 6c 75 65 73 0a 20 20 2a 2a 20 61 72 65 20 4e  alues.  ** are N
16a52 55 4c 4c 2c 20 72 65 74 75 72 6e 20 30 2e 0a 20  ULL, return 0.. 
16a53 20 2a 2f 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e   */.  if( combin
16a54 65 64 5f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c  ed_flags&MEM_Nul
16a55 6c 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  l ){.    return 
16a56 28 66 32 26 4d 45 4d 5f 4e 75 6c 6c 29 20 2d 20  (f2&MEM_Null) - 
16a57 28 66 31 26 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 20  (f1&MEM_Null);. 
16a58 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20   }..  /* If one 
16a59 76 61 6c 75 65 20 69 73 20 61 20 6e 75 6d 62 65  value is a numbe
16a5a 72 20 61 6e 64 20 74 68 65 20 6f 74 68 65 72 20  r and the other 
16a5b 69 73 20 6e 6f 74 2c 20 74 68 65 20 6e 75 6d 62  is not, the numb
16a5c 65 72 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a  er is less..  **
16a5d 20 49 66 20 62 6f 74 68 20 61 72 65 20 6e 75 6d   If both are num
16a5e 62 65 72 73 2c 20 63 6f 6d 70 61 72 65 20 61 73  bers, compare as
16a5f 20 72 65 61 6c 73 20 69 66 20 6f 6e 65 20 69 73   reals if one is
16a60 20 61 20 72 65 61 6c 2c 20 6f 72 20 61 73 20 69   a real, or as i
16a61 6e 74 65 67 65 72 73 0a 20 20 2a 2a 20 69 66 20  ntegers.  ** if 
16a62 62 6f 74 68 20 76 61 6c 75 65 73 20 61 72 65 20  both values are 
16a63 69 6e 74 65 67 65 72 73 2e 0a 20 20 2a 2f 0a 20  integers..  */. 
16a64 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f 66 6c   if( combined_fl
16a65 61 67 73 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d  ags&(MEM_Int|MEM
16a66 5f 52 65 61 6c 29 20 29 7b 0a 20 20 20 20 69 66  _Real) ){.    if
16a67 28 20 21 28 66 31 26 28 4d 45 4d 5f 49 6e 74 7c  ( !(f1&(MEM_Int|
16a68 4d 45 4d 5f 52 65 61 6c 29 29 20 29 7b 0a 20 20  MEM_Real)) ){.  
16a69 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
16a6a 20 20 7d 0a 20 20 20 20 69 66 28 20 21 28 66 32    }.    if( !(f2
16a6b 26 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  &(MEM_Int|MEM_Re
16a6c 61 6c 29 29 20 29 7b 0a 20 20 20 20 20 20 72 65  al)) ){.      re
16a6d 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20  turn -1;.    }. 
16a6e 20 20 20 69 66 28 20 28 66 31 20 26 20 66 32 20     if( (f1 & f2 
16a6f 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 30 20 29 7b  & MEM_Int)==0 ){
16a70 0a 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 31  .      double r1
16a71 2c 20 72 32 3b 0a 20 20 20 20 20 20 69 66 28 20  , r2;.      if( 
16a72 28 66 31 26 4d 45 4d 5f 52 65 61 6c 29 3d 3d 30  (f1&MEM_Real)==0
16a73 20 29 7b 0a 20 20 20 20 20 20 20 20 72 31 20 3d   ){.        r1 =
16a74 20 70 4d 65 6d 31 2d 3e 75 2e 69 3b 0a 20 20 20   pMem1->u.i;.   
16a75 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
16a76 20 20 72 31 20 3d 20 70 4d 65 6d 31 2d 3e 72 3b    r1 = pMem1->r;
16a77 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
16a78 66 28 20 28 66 32 26 4d 45 4d 5f 52 65 61 6c 29  f( (f2&MEM_Real)
16a79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
16a7a 32 20 3d 20 70 4d 65 6d 32 2d 3e 75 2e 69 3b 0a  2 = pMem2->u.i;.
16a7b 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
16a7c 20 20 20 20 20 72 32 20 3d 20 70 4d 65 6d 32 2d       r2 = pMem2-
16a7d 3e 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  >r;.      }.    
16a7e 20 20 69 66 28 20 72 31 3c 72 32 20 29 20 72 65    if( r1<r2 ) re
16a7f 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69  turn -1;.      i
16a80 66 28 20 72 31 3e 72 32 20 29 20 72 65 74 75 72  f( r1>r2 ) retur
16a81 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  n 1;.      retur
16a82 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  n 0;.    }else{.
16a83 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66 31        assert( f1
16a84 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 20 20  &MEM_Int );.    
16a85 20 20 61 73 73 65 72 74 28 20 66 32 26 4d 45 4d    assert( f2&MEM
16a86 5f 49 6e 74 20 29 3b 0a 20 20 20 20 20 20 69 66  _Int );.      if
16a87 28 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3c 20 70  ( pMem1->u.i < p
16a88 4d 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75  Mem2->u.i ) retu
16a89 72 6e 20 2d 31 3b 0a 20 20 20 20 20 20 69 66 28  rn -1;.      if(
16a8a 20 70 4d 65 6d 31 2d 3e 75 2e 69 20 3e 20 70 4d   pMem1->u.i > pM
16a8b 65 6d 32 2d 3e 75 2e 69 20 29 20 72 65 74 75 72  em2->u.i ) retur
16a8c 6e 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72  n 1;.      retur
16a8d 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  n 0;.    }.  }..
16a8e 20 20 2f 2a 20 49 66 20 6f 6e 65 20 76 61 6c 75    /* If one valu
16a8f 65 20 69 73 20 61 20 73 74 72 69 6e 67 20 61 6e  e is a string an
16a90 64 20 74 68 65 20 6f 74 68 65 72 20 69 73 20 61  d the other is a
16a91 20 62 6c 6f 62 2c 20 74 68 65 20 73 74 72 69 6e   blob, the strin
16a92 67 20 69 73 20 6c 65 73 73 2e 0a 20 20 2a 2a 20  g is less..  ** 
16a93 49 66 20 62 6f 74 68 20 61 72 65 20 73 74 72 69  If both are stri
16a94 6e 67 73 2c 20 63 6f 6d 70 61 72 65 20 75 73 69  ngs, compare usi
16a95 6e 67 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67  ng the collating
16a96 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a 2f   functions..  */
16a97 0a 20 20 69 66 28 20 63 6f 6d 62 69 6e 65 64 5f  .  if( combined_
16a98 66 6c 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b  flags&MEM_Str ){
16a99 0a 20 20 20 20 69 66 28 20 28 66 31 20 26 20 4d  .    if( (f1 & M
16a9a 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b 0a 20 20  EM_Str)==0 ){.  
16a9b 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
16a9c 20 20 7d 0a 20 20 20 20 69 66 28 20 28 66 32 20    }.    if( (f2 
16a9d 26 20 4d 45 4d 5f 53 74 72 29 3d 3d 30 20 29 7b  & MEM_Str)==0 ){
16a9e 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31  .      return -1
16a9f 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 61 73 73  ;.    }..    ass
16aa0 65 72 74 28 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d  ert( pMem1->enc=
16aa1 3d 70 4d 65 6d 32 2d 3e 65 6e 63 20 29 3b 0a 20  =pMem2->enc );. 
16aa2 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 31     assert( pMem1
16aa3 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54  ->enc==SQLITE_UT
16aa4 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 20  F8 || .         
16aa5 20 20 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53     pMem1->enc==S
16aa6 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c  QLITE_UTF16LE ||
16aa7 20 70 4d 65 6d 31 2d 3e 65 6e 63 3d 3d 53 51 4c   pMem1->enc==SQL
16aa8 49 54 45 5f 55 54 46 31 36 42 45 20 29 3b 0a 0a  ITE_UTF16BE );..
16aa9 20 20 20 20 2f 2a 20 54 68 65 20 63 6f 6c 6c 61      /* The colla
16aaa 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6d 75  tion sequence mu
16aab 73 74 20 62 65 20 64 65 66 69 6e 65 64 20 61 74  st be defined at
16aac 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 65 76 65   this point, eve
16aad 6e 20 69 66 0a 20 20 20 20 2a 2a 20 74 68 65 20  n if.    ** the 
16aae 75 73 65 72 20 64 65 6c 65 74 65 73 20 74 68 65  user deletes the
16aaf 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
16ab0 6e 63 65 20 61 66 74 65 72 20 74 68 65 20 76 64  nce after the vd
16ab1 62 65 20 70 72 6f 67 72 61 6d 20 69 73 0a 20 20  be program is.  
16ab2 20 20 2a 2a 20 63 6f 6d 70 69 6c 65 64 20 28 74    ** compiled (t
16ab3 68 69 73 20 77 61 73 20 6e 6f 74 20 61 6c 77 61  his was not alwa
16ab4 79 73 20 74 68 65 20 63 61 73 65 29 2e 0a 20 20  ys the case)..  
16ab5 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28    */.    assert(
16ab6 20 21 70 43 6f 6c 6c 20 7c 7c 20 70 43 6f 6c 6c   !pColl || pColl
16ab7 2d 3e 78 43 6d 70 20 29 3b 0a 0a 20 20 20 20 69  ->xCmp );..    i
16ab8 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
16ab9 20 20 69 66 28 20 70 4d 65 6d 31 2d 3e 65 6e 63    if( pMem1->enc
16aba 3d 3d 70 43 6f 6c 6c 2d 3e 65 6e 63 20 29 7b 0a  ==pColl->enc ){.
16abb 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73          /* The s
16abc 74 72 69 6e 67 73 20 61 72 65 20 61 6c 72 65 61  trings are alrea
16abd 64 79 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63  dy in the correc
16abe 74 20 65 6e 63 6f 64 69 6e 67 2e 20 20 43 61 6c  t encoding.  Cal
16abf 6c 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a  l the.        **
16ac0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66 75 6e 63   comparison func
16ac1 74 69 6f 6e 20 64 69 72 65 63 74 6c 79 20 2a 2f  tion directly */
16ac2 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20  .        return 
16ac3 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28 70 43 6f 6c  pColl->xCmp(pCol
16ac4 6c 2d 3e 70 55 73 65 72 2c 70 4d 65 6d 31 2d 3e  l->pUser,pMem1->
16ac5 6e 2c 70 4d 65 6d 31 2d 3e 7a 2c 70 4d 65 6d 32  n,pMem1->z,pMem2
16ac6 2d 3e 6e 2c 70 4d 65 6d 32 2d 3e 7a 29 3b 0a 20  ->n,pMem2->z);. 
16ac7 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
16ac8 20 20 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a      const void *
16ac9 76 31 2c 20 2a 76 32 3b 0a 20 20 20 20 20 20 20  v1, *v2;.       
16aca 20 69 6e 74 20 6e 31 2c 20 6e 32 3b 0a 20 20 20   int n1, n2;.   
16acb 20 20 20 20 20 4d 65 6d 20 63 31 3b 0a 20 20 20       Mem c1;.   
16acc 20 20 20 20 20 4d 65 6d 20 63 32 3b 0a 20 20 20       Mem c2;.   
16acd 20 20 20 20 20 6d 65 6d 73 65 74 28 26 63 31 2c       memset(&c1,
16ace 20 30 2c 20 73 69 7a 65 6f 66 28 63 31 29 29 3b   0, sizeof(c1));
16acf 0a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28  .        memset(
16ad0 26 63 32 2c 20 30 2c 20 73 69 7a 65 6f 66 28 63  &c2, 0, sizeof(c
16ad1 32 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  2));.        sql
16ad2 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
16ad3 6f 77 43 6f 70 79 28 26 63 31 2c 20 70 4d 65 6d  owCopy(&c1, pMem
16ad4 31 2c 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20  1, MEM_Ephem);. 
16ad5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
16ad6 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70 79  beMemShallowCopy
16ad7 28 26 63 32 2c 20 70 4d 65 6d 32 2c 20 4d 45 4d  (&c2, pMem2, MEM
16ad8 5f 45 70 68 65 6d 29 3b 0a 20 20 20 20 20 20 20  _Ephem);.       
16ad9 20 76 31 20 3d 20 73 71 6c 69 74 65 33 56 61 6c   v1 = sqlite3Val
16ada 75 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f  ueText((sqlite3_
16adb 76 61 6c 75 65 2a 29 26 63 31 2c 20 70 43 6f 6c  value*)&c1, pCol
16adc 6c 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20  l->enc);.       
16add 20 6e 31 20 3d 20 76 31 3d 3d 30 20 3f 20 30 20   n1 = v1==0 ? 0 
16ade 3a 20 63 31 2e 6e 3b 0a 20 20 20 20 20 20 20 20  : c1.n;.        
16adf 76 32 20 3d 20 73 71 6c 69 74 65 33 56 61 6c 75  v2 = sqlite3Valu
16ae0 65 54 65 78 74 28 28 73 71 6c 69 74 65 33 5f 76  eText((sqlite3_v
16ae1 61 6c 75 65 2a 29 26 63 32 2c 20 70 43 6f 6c 6c  alue*)&c2, pColl
16ae2 2d 3e 65 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  ->enc);.        
16ae3 6e 32 20 3d 20 76 32 3d 3d 30 20 3f 20 30 20 3a  n2 = v2==0 ? 0 :
16ae4 20 63 32 2e 6e 3b 0a 20 20 20 20 20 20 20 20 72   c2.n;.        r
16ae5 63 20 3d 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 28  c = pColl->xCmp(
16ae6 70 43 6f 6c 6c 2d 3e 70 55 73 65 72 2c 20 6e 31  pColl->pUser, n1
16ae7 2c 20 76 31 2c 20 6e 32 2c 20 76 32 29 3b 0a 20  , v1, n2, v2);. 
16ae8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
16ae9 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 63 31  beMemRelease(&c1
16aea 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
16aeb 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65  e3VdbeMemRelease
16aec 28 26 63 32 29 3b 0a 20 20 20 20 20 20 20 20 72  (&c2);.        r
16aed 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
16aee 7d 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20 49  }.    }.    /* I
16aef 66 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  f a NULL pointer
16af0 20 77 61 73 20 70 61 73 73 65 64 20 61 73 20 74   was passed as t
16af1 68 65 20 63 6f 6c 6c 61 74 65 20 66 75 6e 63 74  he collate funct
16af2 69 6f 6e 2c 20 66 61 6c 6c 20 74 68 72 6f 75 67  ion, fall throug
16af3 68 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20  h.    ** to the 
16af4 62 6c 6f 62 20 63 61 73 65 20 61 6e 64 20 75 73  blob case and us
16af5 65 20 6d 65 6d 63 6d 70 28 29 2e 20 20 2a 2f 0a  e memcmp().  */.
16af6 20 20 7d 0a 20 0a 20 20 2f 2a 20 42 6f 74 68 20    }. .  /* Both 
16af7 76 61 6c 75 65 73 20 6d 75 73 74 20 62 65 20 62  values must be b
16af8 6c 6f 62 73 2e 20 20 43 6f 6d 70 61 72 65 20 75  lobs.  Compare u
16af9 73 69 6e 67 20 6d 65 6d 63 6d 70 28 29 2e 20 20  sing memcmp().  
16afa 2a 2f 0a 20 20 72 63 20 3d 20 6d 65 6d 63 6d 70  */.  rc = memcmp
16afb 28 70 4d 65 6d 31 2d 3e 7a 2c 20 70 4d 65 6d 32  (pMem1->z, pMem2
16afc 2d 3e 7a 2c 20 28 70 4d 65 6d 31 2d 3e 6e 3e 70  ->z, (pMem1->n>p
16afd 4d 65 6d 32 2d 3e 6e 29 3f 70 4d 65 6d 32 2d 3e  Mem2->n)?pMem2->
16afe 6e 3a 70 4d 65 6d 31 2d 3e 6e 29 3b 0a 20 20 69  n:pMem1->n);.  i
16aff 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20 20  f( rc==0 ){.    
16b00 72 63 20 3d 20 70 4d 65 6d 31 2d 3e 6e 20 2d 20  rc = pMem1->n - 
16b01 70 4d 65 6d 32 2d 3e 6e 3b 0a 20 20 7d 0a 20 20  pMem2->n;.  }.  
16b02 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
16b03 0a 2a 2a 20 4d 6f 76 65 20 64 61 74 61 20 6f 75  .** Move data ou
16b04 74 20 6f 66 20 61 20 62 74 72 65 65 20 6b 65 79  t of a btree key
16b05 20 6f 72 20 64 61 74 61 20 66 69 65 6c 64 20 61   or data field a
16b06 6e 64 20 69 6e 74 6f 20 61 20 4d 65 6d 20 73 74  nd into a Mem st
16b07 72 75 63 74 75 72 65 2e 0a 2a 2a 20 54 68 65 20  ructure..** The 
16b08 64 61 74 61 20 6f 72 20 6b 65 79 20 69 73 20 74  data or key is t
16b09 61 6b 65 6e 20 66 72 6f 6d 20 74 68 65 20 65 6e  aken from the en
16b0a 74 72 79 20 74 68 61 74 20 70 43 75 72 20 69 73  try that pCur is
16b0b 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
16b0c 69 6e 67 0a 2a 2a 20 74 6f 2e 20 20 6f 66 66 73  ing.** to.  offs
16b0d 65 74 20 61 6e 64 20 61 6d 74 20 64 65 74 65 72  et and amt deter
16b0e 6d 69 6e 65 20 77 68 61 74 20 70 6f 72 74 69 6f  mine what portio
16b0f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 20 6f 72  n of the data or
16b10 20 6b 65 79 20 74 6f 20 72 65 74 72 69 65 76 65   key to retrieve
16b11 2e 0a 2a 2a 20 6b 65 79 20 69 73 20 74 72 75 65  ..** key is true
16b12 20 74 6f 20 67 65 74 20 74 68 65 20 6b 65 79 20   to get the key 
16b13 6f 72 20 66 61 6c 73 65 20 74 6f 20 67 65 74 20  or false to get 
16b14 64 61 74 61 2e 20 20 54 68 65 20 72 65 73 75 6c  data.  The resul
16b15 74 20 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20  t is written.** 
16b16 69 6e 74 6f 20 74 68 65 20 70 4d 65 6d 20 65 6c  into the pMem el
16b17 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ement..**.** The
16b18 20 70 4d 65 6d 20 73 74 72 75 63 74 75 72 65 20   pMem structure 
16b19 69 73 20 61 73 73 75 6d 65 64 20 74 6f 20 62 65  is assumed to be
16b1a 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20   uninitialized. 
16b1b 20 41 6e 79 20 70 72 69 6f 72 20 63 6f 6e 74 65   Any prior conte
16b1c 6e 74 0a 2a 2a 20 69 73 20 6f 76 65 72 77 72 69  nt.** is overwri
16b1d 74 74 65 6e 20 77 69 74 68 6f 75 74 20 62 65 69  tten without bei
16b1e 6e 67 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a 2a 20  ng freed..**.** 
16b1f 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  If this routine 
16b20 66 61 69 6c 73 20 66 6f 72 20 61 6e 79 20 72 65  fails for any re
16b21 61 73 6f 6e 20 28 6d 61 6c 6c 6f 63 20 72 65 74  ason (malloc ret
16b22 75 72 6e 73 20 4e 55 4c 4c 20 6f 72 20 75 6e 61  urns NULL or una
16b23 62 6c 65 0a 2a 2a 20 74 6f 20 72 65 61 64 20 66  ble.** to read f
16b24 72 6f 6d 20 74 68 65 20 64 69 73 6b 29 20 74 68  rom the disk) th
16b25 65 6e 20 74 68 65 20 70 4d 65 6d 20 69 73 20 6c  en the pMem is l
16b26 65 66 74 20 69 6e 20 61 6e 20 69 6e 63 6f 6e 73  eft in an incons
16b27 69 73 74 65 6e 74 20 73 74 61 74 65 2e 0a 2a 2f  istent state..*/
16b28 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16b29 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 4d  int sqlite3VdbeM
16b2a 65 6d 46 72 6f 6d 42 74 72 65 65 28 0a 20 20 42  emFromBtree(.  B
16b2b 74 43 75 72 73 6f 72 20 2a 70 43 75 72 2c 20 20  tCursor *pCur,  
16b2c 20 2f 2a 20 43 75 72 73 6f 72 20 70 6f 69 6e 74   /* Cursor point
16b2d 69 6e 67 20 61 74 20 72 65 63 6f 72 64 20 74 6f  ing at record to
16b2e 20 72 65 74 72 69 65 76 65 2e 20 2a 2f 0a 20 20   retrieve. */.  
16b2f 69 6e 74 20 6f 66 66 73 65 74 2c 20 20 20 20 20  int offset,     
16b30 20 20 2f 2a 20 4f 66 66 73 65 74 20 66 72 6f 6d    /* Offset from
16b31 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 64 61   the start of da
16b32 74 61 20 74 6f 20 72 65 74 75 72 6e 20 62 79 74  ta to return byt
16b33 65 73 20 66 72 6f 6d 2e 20 2a 2f 0a 20 20 69 6e  es from. */.  in
16b34 74 20 61 6d 74 2c 20 20 20 20 20 20 20 20 20 20  t amt,          
16b35 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
16b36 65 73 20 74 6f 20 72 65 74 75 72 6e 2e 20 2a 2f  es to return. */
16b37 0a 20 20 69 6e 74 20 6b 65 79 2c 20 20 20 20 20  .  int key,     
16b38 20 20 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c       /* If true,
16b39 20 72 65 74 72 69 65 76 65 20 66 72 6f 6d 20 74   retrieve from t
16b3a 68 65 20 62 74 72 65 65 20 6b 65 79 2c 20 6e 6f  he btree key, no
16b3b 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 4d 65 6d  t data. */.  Mem
16b3c 20 2a 70 4d 65 6d 20 20 20 20 20 20 20 20 20 2f   *pMem         /
16b3d 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e 20 64 61  * OUT: Return da
16b3e 74 61 20 69 6e 20 74 68 69 73 20 4d 65 6d 20 73  ta in this Mem s
16b3f 74 72 75 63 74 75 72 65 2e 20 2a 2f 0a 29 7b 0a  tructure. */.){.
16b40 20 20 63 68 61 72 20 2a 7a 44 61 74 61 3b 20 20    char *zData;  
16b41 20 20 20 20 20 2f 2a 20 44 61 74 61 20 66 72 6f       /* Data fro
16b42 6d 20 74 68 65 20 62 74 72 65 65 20 6c 61 79 65  m the btree laye
16b43 72 20 2a 2f 0a 20 20 69 6e 74 20 61 76 61 69 6c  r */.  int avail
16b44 61 62 6c 65 20 3d 20 30 3b 20 2f 2a 20 4e 75 6d  able = 0; /* Num
16b45 62 65 72 20 6f 66 20 62 79 74 65 73 20 61 76 61  ber of bytes ava
16b46 69 6c 61 62 6c 65 20 6f 6e 20 74 68 65 20 6c 6f  ilable on the lo
16b47 63 61 6c 20 62 74 72 65 65 20 70 61 67 65 20 2a  cal btree page *
16b48 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
16b49 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61         /* Databa
16b4a 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f  se connection */
16b4b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
16b4c 54 45 5f 4f 4b 3b 0a 0a 20 20 64 62 20 3d 20 73  TE_OK;..  db = s
16b4d 71 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f  qlite3BtreeCurso
16b4e 72 44 62 28 70 43 75 72 29 3b 0a 20 20 61 73 73  rDb(pCur);.  ass
16b4f 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
16b50 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
16b51 78 29 20 29 3b 0a 20 20 69 66 28 20 6b 65 79 20  x) );.  if( key 
16b52 29 7b 0a 20 20 20 20 7a 44 61 74 61 20 3d 20 28  ){.    zData = (
16b53 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 42 74  char *)sqlite3Bt
16b54 72 65 65 4b 65 79 46 65 74 63 68 28 70 43 75 72  reeKeyFetch(pCur
16b55 2c 20 26 61 76 61 69 6c 61 62 6c 65 29 3b 0a 20  , &available);. 
16b56 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 44 61 74   }else{.    zDat
16b57 61 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69  a = (char *)sqli
16b58 74 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63  te3BtreeDataFetc
16b59 68 28 70 43 75 72 2c 20 26 61 76 61 69 6c 61 62  h(pCur, &availab
16b5a 6c 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  le);.  }.  asser
16b5b 74 28 20 7a 44 61 74 61 21 3d 30 20 29 3b 0a 0a  t( zData!=0 );..
16b5c 20 20 69 66 28 20 6f 66 66 73 65 74 2b 61 6d 74    if( offset+amt
16b5d 3c 3d 61 76 61 69 6c 61 62 6c 65 20 26 26 20 28  <=available && (
16b5e 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d  (pMem->flags&MEM
16b5f 5f 44 79 6e 29 3d 3d 30 20 7c 7c 20 70 4d 65 6d  _Dyn)==0 || pMem
16b60 2d 3e 78 44 65 6c 29 20 29 7b 0a 20 20 20 20 73  ->xDel) ){.    s
16b61 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
16b62 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20 20  ease(pMem);.    
16b63 70 4d 65 6d 2d 3e 7a 20 3d 20 26 7a 44 61 74 61  pMem->z = &zData
16b64 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 70 4d  [offset];.    pM
16b65 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
16b66 42 6c 6f 62 7c 4d 45 4d 5f 45 70 68 65 6d 3b 0a  Blob|MEM_Ephem;.
16b67 20 20 7d 65 6c 73 65 20 69 66 28 20 53 51 4c 49    }else if( SQLI
16b68 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c  TE_OK==(rc = sql
16b69 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28  ite3VdbeMemGrow(
16b6a 70 4d 65 6d 2c 20 61 6d 74 2b 32 2c 20 30 29 29  pMem, amt+2, 0))
16b6b 20 29 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 66 6c   ){.    pMem->fl
16b6c 61 67 73 20 3d 20 4d 45 4d 5f 42 6c 6f 62 7c 4d  ags = MEM_Blob|M
16b6d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 54 65 72 6d 3b  EM_Dyn|MEM_Term;
16b6e 0a 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d  .    pMem->enc =
16b6f 20 30 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79   0;.    pMem->ty
16b70 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  pe = SQLITE_BLOB
16b71 3b 0a 20 20 20 20 69 66 28 20 6b 65 79 20 29 7b  ;.    if( key ){
16b72 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
16b73 74 65 33 42 74 72 65 65 4b 65 79 28 70 43 75 72  te3BtreeKey(pCur
16b74 2c 20 6f 66 66 73 65 74 2c 20 61 6d 74 2c 20 70  , offset, amt, p
16b75 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c  Mem->z);.    }el
16b76 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73  se{.      rc = s
16b77 71 6c 69 74 65 33 42 74 72 65 65 44 61 74 61 28  qlite3BtreeData(
16b78 70 43 75 72 2c 20 6f 66 66 73 65 74 2c 20 61 6d  pCur, offset, am
16b79 74 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20  t, pMem->z);.   
16b7a 20 7d 0a 20 20 20 20 70 4d 65 6d 2d 3e 7a 5b 61   }.    pMem->z[a
16b7b 6d 74 5d 20 3d 20 30 3b 0a 20 20 20 20 70 4d 65  mt] = 0;.    pMe
16b7c 6d 2d 3e 7a 5b 61 6d 74 2b 31 5d 20 3d 20 30 3b  m->z[amt+1] = 0;
16b7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
16b7e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
16b7f 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
16b80 6c 65 61 73 65 28 70 4d 65 6d 29 3b 0a 20 20 20  lease(pMem);.   
16b81 20 7d 0a 20 20 7d 0a 20 20 70 4d 65 6d 2d 3e 6e   }.  }.  pMem->n
16b82 20 3d 20 61 6d 74 3b 0a 0a 20 20 72 65 74 75 72   = amt;..  retur
16b83 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 30 0a 2f  n rc;.}..#if 0./
16b84 2a 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 76 61 72  *.** Perform var
16b85 69 6f 75 73 20 63 68 65 63 6b 73 20 6f 6e 20 74  ious checks on t
16b86 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70  he memory cell p
16b87 4d 65 6d 2e 20 41 6e 20 61 73 73 65 72 74 28 29  Mem. An assert()
16b88 20 77 69 6c 6c 0a 2a 2a 20 66 61 69 6c 20 69 66   will.** fail if
16b89 20 70 4d 65 6d 20 69 73 20 69 6e 74 65 72 6e 61   pMem is interna
16b8a 6c 6c 79 20 69 6e 63 6f 6e 73 69 73 74 65 6e 74  lly inconsistent
16b8b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
16b8c 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
16b8d 56 64 62 65 4d 65 6d 53 61 6e 69 74 79 28 4d 65  VdbeMemSanity(Me
16b8e 6d 20 2a 70 4d 65 6d 29 7b 0a 20 20 69 6e 74 20  m *pMem){.  int 
16b8f 66 6c 61 67 73 20 3d 20 70 4d 65 6d 2d 3e 66 6c  flags = pMem->fl
16b90 61 67 73 3b 0a 20 20 61 73 73 65 72 74 28 20 66  ags;.  assert( f
16b91 6c 61 67 73 21 3d 30 20 29 3b 20 20 2f 2a 20 4d  lags!=0 );  /* M
16b92 75 73 74 20 64 65 66 69 6e 65 20 73 6f 6d 65 20  ust define some 
16b93 74 79 70 65 20 2a 2f 0a 20 20 69 66 28 20 66 6c  type */.  if( fl
16b94 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 72 7c 4d  ags & (MEM_Str|M
16b95 45 4d 5f 42 6c 6f 62 29 20 29 7b 0a 20 20 20 20  EM_Blob) ){.    
16b96 69 6e 74 20 78 20 3d 20 66 6c 61 67 73 20 26 20  int x = flags & 
16b97 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
16b98 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 7c 4d 45  Dyn|MEM_Ephem|ME
16b99 4d 5f 53 68 6f 72 74 29 3b 0a 20 20 20 20 61 73  M_Short);.    as
16b9a 73 65 72 74 28 20 78 21 3d 30 20 29 3b 20 20 20  sert( x!=0 );   
16b9b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 72 69           /* Stri
16b9c 6e 67 73 20 6d 75 73 74 20 64 65 66 69 6e 65 20  ngs must define 
16b9d 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70 65  a string subtype
16b9e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
16b9f 28 78 20 26 20 28 78 2d 31 29 29 3d 3d 30 20 29  (x & (x-1))==0 )
16ba0 3b 20 20 2f 2a 20 4f 6e 6c 79 20 6f 6e 65 20 73  ;  /* Only one s
16ba1 74 72 69 6e 67 20 73 75 62 74 79 70 65 20 63 61  tring subtype ca
16ba2 6e 20 62 65 20 64 65 66 69 6e 65 64 20 2a 2f 0a  n be defined */.
16ba3 20 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d      assert( pMem
16ba4 2d 3e 7a 21 3d 30 20 29 3b 20 20 20 20 20 20 2f  ->z!=0 );      /
16ba5 2a 20 53 74 72 69 6e 67 73 20 6d 75 73 74 20 68  * Strings must h
16ba6 61 76 65 20 61 20 76 61 6c 75 65 20 2a 2f 0a 20  ave a value */. 
16ba7 20 20 20 2f 2a 20 4d 65 6d 2e 7a 20 70 6f 69 6e     /* Mem.z poin
16ba8 74 73 20 74 6f 20 4d 65 6d 2e 7a 53 68 6f 72 74  ts to Mem.zShort
16ba9 20 69 66 66 20 74 68 65 20 73 75 62 74 79 70 65   iff the subtype
16baa 20 69 73 20 4d 45 4d 5f 53 68 6f 72 74 20 2a 2f   is MEM_Short */
16bab 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 78 20  .    assert( (x 
16bac 26 20 4d 45 4d 5f 53 68 6f 72 74 29 3d 3d 30 20  & MEM_Short)==0 
16bad 7c 7c 20 70 4d 65 6d 2d 3e 7a 3d 3d 70 4d 65 6d  || pMem->z==pMem
16bae 2d 3e 7a 53 68 6f 72 74 20 29 3b 0a 20 20 20 20  ->zShort );.    
16baf 61 73 73 65 72 74 28 20 28 78 20 26 20 4d 45 4d  assert( (x & MEM
16bb0 5f 53 68 6f 72 74 29 21 3d 30 20 7c 7c 20 70 4d  _Short)!=0 || pM
16bb1 65 6d 2d 3e 7a 21 3d 70 4d 65 6d 2d 3e 7a 53 68  em->z!=pMem->zSh
16bb2 6f 72 74 20 29 3b 0a 20 20 20 20 2f 2a 20 4e 6f  ort );.    /* No
16bb3 20 64 65 73 74 72 75 63 74 6f 72 20 75 6e 6c 65   destructor unle
16bb4 73 73 20 74 68 65 72 65 20 69 73 20 4d 45 4d 5f  ss there is MEM_
16bb5 44 79 6e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72  Dyn */.    asser
16bb6 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30  t( pMem->xDel==0
16bb7 20 7c 7c 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73   || (pMem->flags
16bb8 20 26 20 4d 45 4d 5f 44 79 6e 29 21 3d 30 20 29   & MEM_Dyn)!=0 )
16bb9 3b 0a 0a 20 20 20 20 69 66 28 20 28 66 6c 61 67  ;..    if( (flag
16bba 73 20 26 20 4d 45 4d 5f 53 74 72 29 20 29 7b 0a  s & MEM_Str) ){.
16bbb 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d        assert( pM
16bbc 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  em->enc==SQLITE_
16bbd 55 54 46 38 20 7c 7c 20 0a 20 20 20 20 20 20 20  UTF8 || .       
16bbe 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63         pMem->enc
16bbf 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  ==SQLITE_UTF16BE
16bc0 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
16bc1 20 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c    pMem->enc==SQL
16bc2 49 54 45 5f 55 54 46 31 36 4c 45 20 0a 20 20 20  ITE_UTF16LE .   
16bc3 20 20 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 49     );.      /* I
16bc4 66 20 74 68 65 20 73 74 72 69 6e 67 20 69 73 20  f the string is 
16bc5 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 61 6e  UTF-8 encoded an
16bc6 64 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  d nul terminated
16bc7 2c 20 74 68 65 6e 20 70 4d 65 6d 2d 3e 6e 0a 20  , then pMem->n. 
16bc8 20 20 20 20 20 2a 2a 20 6d 75 73 74 20 62 65 20       ** must be 
16bc9 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
16bca 65 20 73 74 72 69 6e 67 2e 20 20 28 4c 61 74 65  e string.  (Late
16bcb 72 3a 29 20 20 49 66 20 74 68 65 20 64 61 74 61  r:)  If the data
16bcc 62 61 73 65 20 66 69 6c 65 0a 20 20 20 20 20 20  base file.      
16bcd 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 72 72  ** has been corr
16bce 75 70 74 65 64 2c 20 27 5c 30 30 30 27 20 63 68  upted, '\000' ch
16bcf 61 72 61 63 74 65 72 73 20 6d 69 67 68 74 20 68  aracters might h
16bd0 61 76 65 20 62 65 65 6e 20 69 6e 73 65 72 74 65  ave been inserte
16bd1 64 0a 20 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20  d.      ** into 
16bd2 74 68 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68  the middle of th
16bd3 65 20 73 74 72 69 6e 67 2e 20 20 49 6e 20 74 68  e string.  In th
16bd4 61 74 20 63 61 73 65 2c 20 74 68 65 20 73 74 72  at case, the str
16bd5 6c 65 6e 28 29 20 6d 69 67 68 74 0a 20 20 20 20  len() might.    
16bd6 20 20 2a 2a 20 62 65 20 6c 65 73 73 2e 0a 20 20    ** be less..  
16bd7 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
16bd8 20 70 4d 65 6d 2d 3e 65 6e 63 3d 3d 53 51 4c 49   pMem->enc==SQLI
16bd9 54 45 5f 55 54 46 38 20 26 26 20 28 66 6c 61 67  TE_UTF8 && (flag
16bda 73 20 26 20 4d 45 4d 5f 54 65 72 6d 29 20 29 7b  s & MEM_Term) ){
16bdb 20 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74   .        assert
16bdc 28 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a  ( strlen(pMem->z
16bdd 29 3c 3d 70 4d 65 6d 2d 3e 6e 20 29 3b 0a 20 20  )<=pMem->n );.  
16bde 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4d        assert( pM
16bdf 65 6d 2d 3e 7a 5b 70 4d 65 6d 2d 3e 6e 5d 3d 3d  em->z[pMem->n]==
16be0 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  0 );.      }.   
16be1 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
16be2 2f 2a 20 43 61 6e 6e 6f 74 20 64 65 66 69 6e 65  /* Cannot define
16be3 20 61 20 73 74 72 69 6e 67 20 73 75 62 74 79 70   a string subtyp
16be4 65 20 66 6f 72 20 6e 6f 6e 2d 73 74 72 69 6e 67  e for non-string
16be5 20 6f 62 6a 65 63 74 73 20 2a 2f 0a 20 20 20 20   objects */.    
16be6 61 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66  assert( (pMem->f
16be7 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53 74 61 74  lags & (MEM_Stat
16be8 69 63 7c 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 45  ic|MEM_Dyn|MEM_E
16be9 70 68 65 6d 7c 4d 45 4d 5f 53 68 6f 72 74 29 29  phem|MEM_Short))
16bea 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==0 );.    asser
16beb 74 28 20 70 4d 65 6d 2d 3e 78 44 65 6c 3d 3d 30  t( pMem->xDel==0
16bec 20 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 4d 45 4d   );.  }.  /* MEM
16bed 5f 4e 75 6c 6c 20 65 78 63 6c 75 64 65 73 20 61  _Null excludes a
16bee 6c 6c 20 6f 74 68 65 72 20 74 79 70 65 73 20 2a  ll other types *
16bef 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 4d 65  /.  assert( (pMe
16bf0 6d 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74  m->flags&(MEM_St
16bf1 72 7c 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 52 65  r|MEM_Int|MEM_Re
16bf2 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30  al|MEM_Blob))==0
16bf3 0a 20 20 20 20 20 20 20 20 20 20 7c 7c 20 28 70  .          || (p
16bf4 4d 65 6d 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 4e  Mem->flags&MEM_N
16bf5 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 2f 2a 20  ull)==0 );.  /* 
16bf6 49 66 20 74 68 65 20 4d 45 4d 20 69 73 20 62 6f  If the MEM is bo
16bf7 74 68 20 72 65 61 6c 20 61 6e 64 20 69 6e 74 65  th real and inte
16bf8 67 65 72 2c 20 74 68 65 20 76 61 6c 75 65 73 20  ger, the values 
16bf9 61 72 65 20 65 71 75 61 6c 20 2a 2f 0a 20 20 61  are equal */.  a
16bfa 73 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c  ssert( (pMem->fl
16bfb 61 67 73 20 26 20 28 4d 45 4d 5f 49 6e 74 7c 4d  ags & (MEM_Int|M
16bfc 45 4d 5f 52 65 61 6c 29 29 21 3d 28 4d 45 4d 5f  EM_Real))!=(MEM_
16bfd 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 20 0a 20  Int|MEM_Real) . 
16bfe 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4d 65 6d           || pMem
16bff 2d 3e 72 3d 3d 70 4d 65 6d 2d 3e 75 2e 69 20 29  ->r==pMem->u.i )
16c00 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 54  ;.}.#endif../* T
16c01 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  his function is 
16c02 6f 6e 6c 79 20 61 76 61 69 6c 61 62 6c 65 20 69  only available i
16c03 6e 74 65 72 6e 61 6c 6c 79 2c 20 69 74 20 69 73  nternally, it is
16c04 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65   not part of the
16c05 0a 2a 2a 20 65 78 74 65 72 6e 61 6c 20 41 50 49  .** external API
16c06 2e 20 49 74 20 77 6f 72 6b 73 20 69 6e 20 61 20  . It works in a 
16c07 73 69 6d 69 6c 61 72 20 77 61 79 20 74 6f 20 73  similar way to s
16c08 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
16c09 74 28 29 2c 0a 2a 2a 20 65 78 63 65 70 74 20 74  t(),.** except t
16c0a 68 65 20 64 61 74 61 20 72 65 74 75 72 6e 65 64  he data returned
16c0b 20 69 73 20 69 6e 20 74 68 65 20 65 6e 63 6f 64   is in the encod
16c0c 69 6e 67 20 73 70 65 63 69 66 69 65 64 20 62 79  ing specified by
16c0d 20 74 68 65 20 73 65 63 6f 6e 64 0a 2a 2a 20 70   the second.** p
16c0e 61 72 61 6d 65 74 65 72 2c 20 77 68 69 63 68 20  arameter, which 
16c0f 6d 75 73 74 20 62 65 20 6f 6e 65 20 6f 66 20 53  must be one of S
16c10 51 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53  QLITE_UTF16BE, S
16c11 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72  QLITE_UTF16LE or
16c12 0a 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 38 2e  .** SQLITE_UTF8.
16c13 0a 2a 2a 0a 2a 2a 20 28 32 30 30 36 2d 30 32 2d  .**.** (2006-02-
16c14 31 36 3a 29 20 20 54 68 65 20 65 6e 63 20 76 61  16:)  The enc va
16c15 6c 75 65 20 63 61 6e 20 62 65 20 6f 72 2d 65 64  lue can be or-ed
16c16 20 77 69 74 68 20 53 51 4c 49 54 45 5f 55 54 46   with SQLITE_UTF
16c17 31 36 5f 41 4c 49 47 4e 45 44 2e 0a 2a 2a 20 49  16_ALIGNED..** I
16c18 66 20 74 68 61 74 20 69 73 20 74 68 65 20 63 61  f that is the ca
16c19 73 65 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73  se, then the res
16c1a 75 6c 74 20 6d 75 73 74 20 62 65 20 61 6c 69 67  ult must be alig
16c1b 6e 65 64 20 6f 6e 20 61 6e 20 65 76 65 6e 20 62  ned on an even b
16c1c 79 74 65 0a 2a 2a 20 62 6f 75 6e 64 61 72 79 2e  yte.** boundary.
16c1d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
16c1e 54 45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73  TE const void *s
16c1f 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28  qlite3ValueText(
16c20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 20 70  sqlite3_value* p
16c21 56 61 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20  Val, u8 enc){.  
16c22 69 66 28 20 21 70 56 61 6c 20 29 20 72 65 74 75  if( !pVal ) retu
16c23 72 6e 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28  rn 0;..  assert(
16c24 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 20 7c 7c 20   pVal->db==0 || 
16c25 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
16c26 6c 64 28 70 56 61 6c 2d 3e 64 62 2d 3e 6d 75 74  ld(pVal->db->mut
16c27 65 78 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ex) );.  assert(
16c28 20 28 65 6e 63 26 33 29 3d 3d 28 65 6e 63 26 7e   (enc&3)==(enc&~
16c29 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
16c2a 47 4e 45 44 29 20 29 3b 0a 0a 20 20 69 66 28 20  GNED) );..  if( 
16c2b 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  pVal->flags&MEM_
16c2c 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  Null ){.    retu
16c2d 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 61 73 73 65  rn 0;.  }.  asse
16c2e 72 74 28 20 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33  rt( (MEM_Blob>>3
16c2f 29 20 3d 3d 20 4d 45 4d 5f 53 74 72 20 29 3b 0a  ) == MEM_Str );.
16c30 20 20 70 56 61 6c 2d 3e 66 6c 61 67 73 20 7c 3d    pVal->flags |=
16c31 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20   (pVal->flags & 
16c32 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20 20  MEM_Blob)>>3;.  
16c33 65 78 70 61 6e 64 42 6c 6f 62 28 70 56 61 6c 29  expandBlob(pVal)
16c34 3b 0a 20 20 69 66 28 20 70 56 61 6c 2d 3e 66 6c  ;.  if( pVal->fl
16c35 61 67 73 26 4d 45 4d 5f 53 74 72 20 29 7b 0a 20  ags&MEM_Str ){. 
16c36 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
16c37 61 6e 67 65 45 6e 63 6f 64 69 6e 67 28 70 56 61  angeEncoding(pVa
16c38 6c 2c 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45  l, enc & ~SQLITE
16c39 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b  _UTF16_ALIGNED);
16c3a 0a 20 20 20 20 69 66 28 20 28 65 6e 63 20 26 20  .    if( (enc & 
16c3b 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41 4c 49  SQLITE_UTF16_ALI
16c3c 47 4e 45 44 29 21 3d 30 20 26 26 20 31 3d 3d 28  GNED)!=0 && 1==(
16c3d 31 26 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f  1&SQLITE_PTR_TO_
16c3e 49 4e 54 28 70 56 61 6c 2d 3e 7a 29 29 20 29 7b  INT(pVal->z)) ){
16c3f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
16c40 70 56 61 6c 2d 3e 66 6c 61 67 73 20 26 20 28 4d  pVal->flags & (M
16c41 45 4d 5f 45 70 68 65 6d 7c 4d 45 4d 5f 53 74 61  EM_Ephem|MEM_Sta
16c42 74 69 63 29 29 21 3d 30 20 29 3b 0a 20 20 20 20  tic))!=0 );.    
16c43 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
16c44 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65 61 62 6c  eMemMakeWriteabl
16c45 65 28 70 56 61 6c 29 21 3d 53 51 4c 49 54 45 5f  e(pVal)!=SQLITE_
16c46 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  OK ){.        re
16c47 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
16c48 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
16c49 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65 72 6d 69  3VdbeMemNulTermi
16c4a 6e 61 74 65 28 70 56 61 6c 29 3b 0a 20 20 7d 65  nate(pVal);.  }e
16c4b 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
16c4c 20 28 70 56 61 6c 2d 3e 66 6c 61 67 73 26 4d 45   (pVal->flags&ME
16c4d 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 3b 0a 20 20  M_Blob)==0 );.  
16c4e 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
16c4f 53 74 72 69 6e 67 69 66 79 28 70 56 61 6c 2c 20  Stringify(pVal, 
16c50 65 6e 63 29 3b 0a 20 20 20 20 61 73 73 65 72 74  enc);.    assert
16c51 28 20 30 3d 3d 28 31 26 28 69 6e 74 29 70 56 61  ( 0==(1&(int)pVa
16c52 6c 2d 3e 7a 29 20 29 3b 0a 20 20 7d 0a 20 20 61  l->z) );.  }.  a
16c53 73 73 65 72 74 28 70 56 61 6c 2d 3e 65 6e 63 3d  ssert(pVal->enc=
16c54 3d 28 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f  =(enc & ~SQLITE_
16c55 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 20 7c  UTF16_ALIGNED) |
16c56 7c 20 70 56 61 6c 2d 3e 64 62 3d 3d 30 0a 20 20  | pVal->db==0.  
16c57 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70              || p
16c58 56 61 6c 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  Val->db->mallocF
16c59 61 69 6c 65 64 20 29 3b 0a 20 20 69 66 28 20 70  ailed );.  if( p
16c5a 56 61 6c 2d 3e 65 6e 63 3d 3d 28 65 6e 63 20 26  Val->enc==(enc &
16c5b 20 7e 53 51 4c 49 54 45 5f 55 54 46 31 36 5f 41   ~SQLITE_UTF16_A
16c5c 4c 49 47 4e 45 44 29 20 29 7b 0a 20 20 20 20 72  LIGNED) ){.    r
16c5d 65 74 75 72 6e 20 70 56 61 6c 2d 3e 7a 3b 0a 20  eturn pVal->z;. 
16c5e 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
16c5f 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 0;.  }.}../*.
16c60 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20  ** Create a new 
16c61 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62  sqlite3_value ob
16c62 6a 65 63 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ject..*/.SQLITE_
16c63 50 52 49 56 41 54 45 20 73 71 6c 69 74 65 33 5f  PRIVATE sqlite3_
16c64 76 61 6c 75 65 20 2a 73 71 6c 69 74 65 33 56 61  value *sqlite3Va
16c65 6c 75 65 4e 65 77 28 73 71 6c 69 74 65 33 20 2a  lueNew(sqlite3 *
16c66 64 62 29 7b 0a 20 20 4d 65 6d 20 2a 70 20 3d 20  db){.  Mem *p = 
16c67 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
16c68 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
16c69 70 29 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a  p));.  if( p ){.
16c6a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d      p->flags = M
16c6b 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 2d 3e  EM_Null;.    p->
16c6c 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 4e 55  type = SQLITE_NU
16c6d 4c 4c 3b 0a 20 20 20 20 70 2d 3e 64 62 20 3d 20  LL;.    p->db = 
16c6e 64 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  db;.  }.  return
16c6f 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65   p;.}../*.** Cre
16c70 61 74 65 20 61 20 6e 65 77 20 73 71 6c 69 74 65  ate a new sqlite
16c71 33 5f 76 61 6c 75 65 20 6f 62 6a 65 63 74 2c 20  3_value object, 
16c72 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 76  containing the v
16c73 61 6c 75 65 20 6f 66 20 70 45 78 70 72 2e 0a 2a  alue of pExpr..*
16c74 2a 0a 2a 2a 20 54 68 69 73 20 6f 6e 6c 79 20 77  *.** This only w
16c75 6f 72 6b 73 20 66 6f 72 20 76 65 72 79 20 73 69  orks for very si
16c76 6d 70 6c 65 20 65 78 70 72 65 73 73 69 6f 6e 73  mple expressions
16c77 20 74 68 61 74 20 63 6f 6e 73 69 73 74 20 6f 66   that consist of
16c78 20 6f 6e 65 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a   one constant.**
16c79 20 74 6f 6b 65 6e 20 28 69 2e 65 2e 20 22 35 22   token (i.e. "5"
16c7a 2c 20 22 35 2e 31 22 2c 20 22 27 61 20 73 74 72  , "5.1", "'a str
16c7b 69 6e 67 27 22 29 2e 20 49 66 20 74 68 65 20 65  ing'"). If the e
16c7c 78 70 72 65 73 73 69 6f 6e 20 63 61 6e 0a 2a 2a  xpression can.**
16c7d 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 64 69   be converted di
16c7e 72 65 63 74 6c 79 20 69 6e 74 6f 20 61 20 76 61  rectly into a va
16c7f 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 76 61  lue, then the va
16c80 6c 75 65 20 69 73 20 61 6c 6c 6f 63 61 74 65 64  lue is allocated
16c81 20 61 6e 64 0a 2a 2a 20 61 20 70 6f 69 6e 74 65   and.** a pointe
16c82 72 20 77 72 69 74 74 65 6e 20 74 6f 20 2a 70 70  r written to *pp
16c83 56 61 6c 2e 20 54 68 65 20 63 61 6c 6c 65 72 20  Val. The caller 
16c84 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
16c85 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 0a  or deallocating.
16c86 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 62 79 20  ** the value by 
16c87 70 61 73 73 69 6e 67 20 69 74 20 74 6f 20 73 71  passing it to sq
16c88 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 29  lite3ValueFree()
16c89 20 6c 61 74 65 72 20 6f 6e 2e 20 49 66 20 74 68   later on. If th
16c8a 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
16c8b 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  cannot be conver
16c8c 74 65 64 20 74 6f 20 61 20 76 61 6c 75 65 2c 20  ted to a value, 
16c8d 74 68 65 6e 20 2a 70 70 56 61 6c 20 69 73 20 73  then *ppVal is s
16c8e 65 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a 2f 0a 53  et to NULL..*/.S
16c8f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16c90 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72  t sqlite3ValueFr
16c91 6f 6d 45 78 70 72 28 0a 20 20 73 71 6c 69 74 65  omExpr(.  sqlite
16c92 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
16c93 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
16c94 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  ase connection *
16c95 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
16c96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16c97 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
16c98 74 6f 20 65 76 61 6c 75 61 74 65 20 2a 2f 0a 20  to evaluate */. 
16c99 20 75 38 20 65 6e 63 2c 20 20 20 20 20 20 20 20   u8 enc,        
16c9a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e             /* En
16c9b 63 6f 64 69 6e 67 20 74 6f 20 75 73 65 20 2a 2f  coding to use */
16c9c 0a 20 20 75 38 20 61 66 66 69 6e 69 74 79 2c 20  .  u8 affinity, 
16c9d 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
16c9e 41 66 66 69 6e 69 74 79 20 74 6f 20 75 73 65 20  Affinity to use 
16c9f 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
16ca0 75 65 20 2a 2a 70 70 56 61 6c 20 20 20 20 20 2f  ue **ppVal     /
16ca1 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65 77 20  * Write the new 
16ca2 76 61 6c 75 65 20 68 65 72 65 20 2a 2f 0a 29 7b  value here */.){
16ca3 0a 20 20 69 6e 74 20 6f 70 3b 0a 20 20 63 68 61  .  int op;.  cha
16ca4 72 20 2a 7a 56 61 6c 20 3d 20 30 3b 0a 20 20 73  r *zVal = 0;.  s
16ca5 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
16ca6 61 6c 20 3d 20 30 3b 0a 0a 20 20 69 66 28 20 21  al = 0;..  if( !
16ca7 70 45 78 70 72 20 29 7b 0a 20 20 20 20 2a 70 70  pExpr ){.    *pp
16ca8 56 61 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  Val = 0;.    ret
16ca9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
16caa 20 7d 0a 20 20 6f 70 20 3d 20 70 45 78 70 72 2d   }.  op = pExpr-
16cab 3e 6f 70 3b 0a 0a 20 20 69 66 28 20 6f 70 3d 3d  >op;..  if( op==
16cac 54 4b 5f 53 54 52 49 4e 47 20 7c 7c 20 6f 70 3d  TK_STRING || op=
16cad 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 6f 70 3d  =TK_FLOAT || op=
16cae 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  =TK_INTEGER ){. 
16caf 20 20 20 7a 56 61 6c 20 3d 20 73 71 6c 69 74 65     zVal = sqlite
16cb0 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
16cb1 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
16cb2 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b  en.z, pExpr->tok
16cb3 65 6e 2e 6e 29 3b 0a 20 20 20 20 70 56 61 6c 20  en.n);.    pVal 
16cb4 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 4e 65  = sqlite3ValueNe
16cb5 77 28 64 62 29 3b 0a 20 20 20 20 69 66 28 20 21  w(db);.    if( !
16cb6 7a 56 61 6c 20 7c 7c 20 21 70 56 61 6c 20 29 20  zVal || !pVal ) 
16cb7 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20  goto no_mem;.   
16cb8 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 28   sqlite3Dequote(
16cb9 7a 56 61 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74  zVal);.    sqlit
16cba 65 33 56 61 6c 75 65 53 65 74 53 74 72 28 70 56  e3ValueSetStr(pV
16cbb 61 6c 2c 20 2d 31 2c 20 7a 56 61 6c 2c 20 53 51  al, -1, zVal, SQ
16cbc 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
16cbd 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  E_DYNAMIC);.    
16cbe 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 49 4e 54 45  if( (op==TK_INTE
16cbf 47 45 52 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 46 4c  GER || op==TK_FL
16cc0 4f 41 54 20 29 20 26 26 20 61 66 66 69 6e 69 74  OAT ) && affinit
16cc1 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f  y==SQLITE_AFF_NO
16cc2 4e 45 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  NE ){.      sqli
16cc3 74 65 33 56 61 6c 75 65 41 70 70 6c 79 41 66 66  te3ValueApplyAff
16cc4 69 6e 69 74 79 28 70 56 61 6c 2c 20 53 51 4c 49  inity(pVal, SQLI
16cc5 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 2c 20  TE_AFF_NUMERIC, 
16cc6 65 6e 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  enc);.    }else{
16cc7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 61  .      sqlite3Va
16cc8 6c 75 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79  lueApplyAffinity
16cc9 28 70 56 61 6c 2c 20 61 66 66 69 6e 69 74 79 2c  (pVal, affinity,
16cca 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20 20 7d   enc);.    }.  }
16ccb 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  else if( op==TK_
16ccc 55 4d 49 4e 55 53 20 29 20 7b 0a 20 20 20 20 69  UMINUS ) {.    i
16ccd 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73 71  f( SQLITE_OK==sq
16cce 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
16ccf 70 72 28 64 62 2c 70 45 78 70 72 2d 3e 70 4c 65  pr(db,pExpr->pLe
16cd0 66 74 2c 65 6e 63 2c 61 66 66 69 6e 69 74 79 2c  ft,enc,affinity,
16cd1 26 70 56 61 6c 29 20 29 7b 0a 20 20 20 20 20 20  &pVal) ){.      
16cd2 70 56 61 6c 2d 3e 75 2e 69 20 3d 20 2d 31 20 2a  pVal->u.i = -1 *
16cd3 20 70 56 61 6c 2d 3e 75 2e 69 3b 0a 20 20 20 20   pVal->u.i;.    
16cd4 20 20 70 56 61 6c 2d 3e 72 20 3d 20 2d 31 2e 30    pVal->r = -1.0
16cd5 20 2a 20 70 56 61 6c 2d 3e 72 3b 0a 20 20 20 20   * pVal->r;.    
16cd6 7d 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  }.  }.#ifndef SQ
16cd7 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
16cd8 49 54 45 52 41 4c 0a 20 20 65 6c 73 65 20 69 66  ITERAL.  else if
16cd9 28 20 6f 70 3d 3d 54 4b 5f 42 4c 4f 42 20 29 7b  ( op==TK_BLOB ){
16cda 0a 20 20 20 20 69 6e 74 20 6e 56 61 6c 3b 0a 20  .    int nVal;. 
16cdb 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
16cdc 2d 3e 74 6f 6b 65 6e 2e 6e 3e 3d 33 20 29 3b 0a  ->token.n>=3 );.
16cdd 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
16cde 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27  r->token.z[0]=='
16cdf 78 27 20 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b  x' || pExpr->tok
16ce0 65 6e 2e 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a  en.z[0]=='X' );.
16ce1 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70      assert( pExp
16ce2 72 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27  r->token.z[1]=='
16ce3 5c 27 27 20 29 3b 0a 20 20 20 20 61 73 73 65 72  \'' );.    asser
16ce4 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  t( pExpr->token.
16ce5 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e  z[pExpr->token.n
16ce6 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20 20  -1]=='\'' );.   
16ce7 20 70 56 61 6c 20 3d 20 73 71 6c 69 74 65 33 56   pVal = sqlite3V
16ce8 61 6c 75 65 4e 65 77 28 64 62 29 3b 0a 20 20 20  alueNew(db);.   
16ce9 20 69 66 28 20 21 70 56 61 6c 20 29 20 67 6f 74   if( !pVal ) got
16cea 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 6e 56  o no_mem;.    nV
16ceb 61 6c 20 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65  al = pExpr->toke
16cec 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20 7a 56 61  n.n - 3;.    zVa
16ced 6c 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70 72  l = (char*)pExpr
16cee 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b 0a 20  ->token.z + 2;. 
16cef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
16cf0 6d 53 65 74 53 74 72 28 70 56 61 6c 2c 20 73 71  mSetStr(pVal, sq
16cf1 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28 64  lite3HexToBlob(d
16cf2 62 2c 20 7a 56 61 6c 2c 20 6e 56 61 6c 29 2c 20  b, zVal, nVal), 
16cf3 6e 56 61 6c 2f 32 2c 0a 20 20 20 20 20 20 20 20  nVal/2,.        
16cf4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16cf5 20 30 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d   0, SQLITE_DYNAM
16cf6 49 43 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  IC);.  }.#endif.
16cf7 0a 20 20 2a 70 70 56 61 6c 20 3d 20 70 56 61 6c  .  *ppVal = pVal
16cf8 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
16cf9 45 5f 4f 4b 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 0a 20  E_OK;..no_mem:. 
16cfa 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
16cfb 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65 33  d = 1;.  sqlite3
16cfc 44 62 46 72 65 65 28 64 62 2c 20 7a 56 61 6c 29  DbFree(db, zVal)
16cfd 3b 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65  ;.  sqlite3Value
16cfe 46 72 65 65 28 70 56 61 6c 29 3b 0a 20 20 2a 70  Free(pVal);.  *p
16cff 70 56 61 6c 20 3d 20 30 3b 0a 20 20 72 65 74 75  pVal = 0;.  retu
16d00 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
16d01 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
16d02 20 74 68 65 20 73 74 72 69 6e 67 20 76 61 6c 75   the string valu
16d03 65 20 6f 66 20 61 6e 20 73 71 6c 69 74 65 33 5f  e of an sqlite3_
16d04 76 61 6c 75 65 20 6f 62 6a 65 63 74 0a 2a 2f 0a  value object.*/.
16d05 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
16d06 6f 69 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65  oid sqlite3Value
16d07 53 65 74 53 74 72 28 0a 20 20 73 71 6c 69 74 65  SetStr(.  sqlite
16d08 33 5f 76 61 6c 75 65 20 2a 76 2c 20 20 20 20 20  3_value *v,     
16d09 2f 2a 20 56 61 6c 75 65 20 74 6f 20 62 65 20 73  /* Value to be s
16d0a 65 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 2c 20 20  et */.  int n,  
16d0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
16d0c 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69 6e   Length of strin
16d0d 67 20 7a 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  g z */.  const v
16d0e 6f 69 64 20 2a 7a 2c 20 20 20 20 20 20 20 20 2f  oid *z,        /
16d0f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20 6e 65  * Text of the ne
16d10 77 20 73 74 72 69 6e 67 20 2a 2f 0a 20 20 75 38  w string */.  u8
16d11 20 65 6e 63 2c 20 20 20 20 20 20 20 20 20 20 20   enc,           
16d12 20 20 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67 20      /* Encoding 
16d13 74 6f 20 75 73 65 20 2a 2f 0a 20 20 76 6f 69 64  to use */.  void
16d14 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 20   (*xDel)(void*) 
16d15 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
16d16 66 6f 72 20 74 68 65 20 73 74 72 69 6e 67 20 2a  for the string *
16d17 2f 0a 29 7b 0a 20 20 69 66 28 20 76 20 29 20 73  /.){.  if( v ) s
16d18 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
16d19 53 74 72 28 28 4d 65 6d 20 2a 29 76 2c 20 7a 2c  Str((Mem *)v, z,
16d1a 20 6e 2c 20 65 6e 63 2c 20 78 44 65 6c 29 3b 0a   n, enc, xDel);.
16d1b 7d 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20 61 6e  }../*.** Free an
16d1c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f   sqlite3_value o
16d1d 62 6a 65 63 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bject.*/.SQLITE_
16d1e 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
16d1f 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 73 71  ite3ValueFree(sq
16d20 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 76 29 7b  lite3_value *v){
16d21 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74 75  .  if( !v ) retu
16d22 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  rn;.  sqlite3Vdb
16d23 65 4d 65 6d 52 65 6c 65 61 73 65 28 28 4d 65 6d  eMemRelease((Mem
16d24 20 2a 29 76 29 3b 0a 20 20 73 71 6c 69 74 65 33   *)v);.  sqlite3
16d25 44 62 46 72 65 65 28 28 28 4d 65 6d 2a 29 76 29  DbFree(((Mem*)v)
16d26 2d 3e 64 62 2c 20 76 29 3b 0a 7d 0a 0a 2f 2a 0a  ->db, v);.}../*.
16d27 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75  ** Return the nu
16d28 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e  mber of bytes in
16d29 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 61 6c   the sqlite3_val
16d2a 75 65 20 6f 62 6a 65 63 74 20 61 73 73 75 6d 69  ue object assumi
16d2b 6e 67 0a 2a 2a 20 74 68 61 74 20 69 74 20 75 73  ng.** that it us
16d2c 65 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  es the encoding 
16d2d 22 65 6e 63 22 0a 2a 2f 0a 53 51 4c 49 54 45 5f  "enc".*/.SQLITE_
16d2e 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
16d2f 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 73 71  te3ValueBytes(sq
16d30 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
16d31 6c 2c 20 75 38 20 65 6e 63 29 7b 0a 20 20 4d 65  l, u8 enc){.  Me
16d32 6d 20 2a 70 20 3d 20 28 4d 65 6d 2a 29 70 56 61  m *p = (Mem*)pVa
16d33 6c 3b 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61  l;.  if( (p->fla
16d34 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 29 21 3d  gs & MEM_Blob)!=
16d35 30 20 7c 7c 20 73 71 6c 69 74 65 33 56 61 6c 75  0 || sqlite3Valu
16d36 65 54 65 78 74 28 70 56 61 6c 2c 20 65 6e 63 29  eText(pVal, enc)
16d37 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 66   ){.    if( p->f
16d38 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20  lags & MEM_Zero 
16d39 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
16d3a 70 2d 3e 6e 2b 70 2d 3e 75 2e 69 3b 0a 20 20 20  p->n+p->u.i;.   
16d3b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 65   }else{.      re
16d3c 74 75 72 6e 20 70 2d 3e 6e 3b 0a 20 20 20 20 7d  turn p->n;.    }
16d3d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
16d3e 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
16d3f 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62 65 6d  *** End of vdbem
16d40 65 6d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  em.c ***********
16d41 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d42 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d43 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
16d44 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 76  *** Begin file v
16d45 64 62 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a  dbeaux.c *******
16d46 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d47 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d48 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 53  **/./*.** 2003 S
16d49 65 70 74 65 6d 62 65 72 20 36 0a 2a 2a 0a 2a 2a  eptember 6.**.**
16d4a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
16d4b 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
16d4c 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
16d4d 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
16d4e 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
16d4f 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
16d50 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
16d51 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
16d52 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
16d53 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
16d54 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
16d55 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
16d56 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
16d57 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
16d58 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
16d59 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
16d5a 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
16d5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
16d5f 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
16d60 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
16d61 63 6f 64 65 20 75 73 65 64 20 66 6f 72 20 63 72  code used for cr
16d62 65 61 74 69 6e 67 2c 20 64 65 73 74 72 6f 79 69  eating, destroyi
16d63 6e 67 2c 20 61 6e 64 20 70 6f 70 75 6c 61 74 69  ng, and populati
16d64 6e 67 0a 2a 2a 20 61 20 56 44 42 45 20 28 6f 72  ng.** a VDBE (or
16d65 20 61 6e 20 22 73 71 6c 69 74 65 33 5f 73 74 6d   an "sqlite3_stm
16d66 74 22 20 61 73 20 69 74 20 69 73 20 6b 6e 6f 77  t" as it is know
16d67 6e 20 74 6f 20 74 68 65 20 6f 75 74 73 69 64 65  n to the outside
16d68 20 77 6f 72 6c 64 2e 29 20 20 50 72 69 6f 72 0a   world.)  Prior.
16d69 2a 2a 20 74 6f 20 76 65 72 73 69 6f 6e 20 32 2e  ** to version 2.
16d6a 38 2e 37 2c 20 61 6c 6c 20 74 68 69 73 20 63 6f  8.7, all this co
16d6b 64 65 20 77 61 73 20 63 6f 6d 62 69 6e 65 64 20  de was combined 
16d6c 69 6e 74 6f 20 74 68 65 20 76 64 62 65 2e 63 20  into the vdbe.c 
16d6d 73 6f 75 72 63 65 20 66 69 6c 65 2e 0a 2a 2a 20  source file..** 
16d6e 42 75 74 20 74 68 61 74 20 66 69 6c 65 20 77 61  But that file wa
16d6f 73 20 67 65 74 74 69 6e 67 20 74 6f 6f 20 62 69  s getting too bi
16d70 67 20 73 6f 20 74 68 69 73 20 73 75 62 72 6f 75  g so this subrou
16d71 74 69 6e 65 73 20 77 65 72 65 20 73 70 6c 69 74  tines were split
16d72 20 6f 75 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a   out..**.** $Id:
16d73 20 76 64 62 65 61 75 78 2e 63 2c 76 20 31 2e 34   vdbeaux.c,v 1.4
16d74 32 30 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39  20 2008/11/17 19
16d75 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39  :18:55 danielk19
16d76 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 0a 2f  77 Exp $.*/..../
16d77 2a 0a 2a 2a 20 57 68 65 6e 20 64 65 62 75 67 67  *.** When debugg
16d78 69 6e 67 20 74 68 65 20 63 6f 64 65 20 67 65 6e  ing the code gen
16d79 65 72 61 74 6f 72 20 69 6e 20 61 20 73 79 6d 62  erator in a symb
16d7a 6f 6c 69 63 20 64 65 62 75 67 67 65 72 2c 20 6f  olic debugger, o
16d7b 6e 65 20 63 61 6e 0a 2a 2a 20 73 65 74 20 74 68  ne can.** set th
16d7c 65 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  e sqlite3VdbeAdd
16d7d 6f 70 54 72 61 63 65 20 74 6f 20 31 20 61 6e 64  opTrace to 1 and
16d7e 20 61 6c 6c 20 6f 70 63 6f 64 65 73 20 77 69 6c   all opcodes wil
16d7f 6c 20 62 65 20 70 72 69 6e 74 65 64 0a 2a 2a 20  l be printed.** 
16d80 61 73 20 74 68 65 79 20 61 72 65 20 61 64 64 65  as they are adde
16d81 64 20 74 6f 20 74 68 65 20 69 6e 73 74 72 75 63  d to the instruc
16d82 74 69 6f 6e 20 73 74 72 65 61 6d 2e 0a 2a 2f 0a  tion stream..*/.
16d83 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
16d84 42 55 47 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  BUG.SQLITE_PRIVA
16d85 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
16d86 62 65 41 64 64 6f 70 54 72 61 63 65 20 3d 20 30  beAddopTrace = 0
16d87 3b 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  ;.#endif.../*.**
16d88 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 76 69   Create a new vi
16d89 72 74 75 61 6c 20 64 61 74 61 62 61 73 65 20 65  rtual database e
16d8a 6e 67 69 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ngine..*/.SQLITE
16d8b 5f 50 52 49 56 41 54 45 20 56 64 62 65 20 2a 73  _PRIVATE Vdbe *s
16d8c 71 6c 69 74 65 33 56 64 62 65 43 72 65 61 74 65  qlite3VdbeCreate
16d8d 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
16d8e 20 56 64 62 65 20 2a 70 3b 0a 20 20 70 20 3d 20   Vdbe *p;.  p = 
16d8f 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
16d90 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 56  ero(db, sizeof(V
16d91 64 62 65 29 20 29 3b 0a 20 20 69 66 28 20 70 3d  dbe) );.  if( p=
16d92 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
16d93 20 70 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 69   p->db = db;.  i
16d94 66 28 20 64 62 2d 3e 70 56 64 62 65 20 29 7b 0a  f( db->pVdbe ){.
16d95 20 20 20 20 64 62 2d 3e 70 56 64 62 65 2d 3e 70      db->pVdbe->p
16d96 50 72 65 76 20 3d 20 70 3b 0a 20 20 7d 0a 20 20  Prev = p;.  }.  
16d97 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d 3e 70  p->pNext = db->p
16d98 56 64 62 65 3b 0a 20 20 70 2d 3e 70 50 72 65 76  Vdbe;.  p->pPrev
16d99 20 3d 20 30 3b 0a 20 20 64 62 2d 3e 70 56 64 62   = 0;.  db->pVdb
16d9a 65 20 3d 20 70 3b 0a 20 20 70 2d 3e 6d 61 67 69  e = p;.  p->magi
16d9b 63 20 3d 20 56 44 42 45 5f 4d 41 47 49 43 5f 49  c = VDBE_MAGIC_I
16d9c 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b  NIT;.  return p;
16d9d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 65 6d 62  .}../*.** Rememb
16d9e 65 72 20 74 68 65 20 53 51 4c 20 73 74 72 69 6e  er the SQL strin
16d9f 67 20 66 6f 72 20 61 20 70 72 65 70 61 72 65 64  g for a prepared
16da0 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53   statement..*/.S
16da1 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
16da2 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  id sqlite3VdbeSe
16da3 74 53 71 6c 28 56 64 62 65 20 2a 70 2c 20 63 6f  tSql(Vdbe *p, co
16da4 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74  nst char *z, int
16da5 20 6e 29 7b 0a 20 20 69 66 28 20 70 3d 3d 30 20   n){.  if( p==0 
16da6 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65  ) return;.  asse
16da7 72 74 28 20 70 2d 3e 7a 53 71 6c 3d 3d 30 20 29  rt( p->zSql==0 )
16da8 3b 0a 20 20 70 2d 3e 7a 53 71 6c 20 3d 20 73 71  ;.  p->zSql = sq
16da9 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 70  lite3DbStrNDup(p
16daa 2d 3e 64 62 2c 20 7a 2c 20 6e 29 3b 0a 7d 0a 0a  ->db, z, n);.}..
16dab 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
16dac 20 53 51 4c 20 61 73 73 6f 63 69 61 74 65 64 20   SQL associated 
16dad 77 69 74 68 20 61 20 70 72 65 70 61 72 65 64 20  with a prepared 
16dae 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53 51 4c  statement.*/.SQL
16daf 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
16db0 61 72 20 2a 73 71 6c 69 74 65 33 5f 73 71 6c 28  ar *sqlite3_sql(
16db1 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
16db2 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 28  tmt){.  return (
16db3 28 56 64 62 65 20 2a 29 70 53 74 6d 74 29 2d 3e  (Vdbe *)pStmt)->
16db4 7a 53 71 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  zSql;.}../*.** S
16db5 77 61 70 20 61 6c 6c 20 63 6f 6e 74 65 6e 74 20  wap all content 
16db6 62 65 74 77 65 65 6e 20 74 77 6f 20 56 44 42 45  between two VDBE
16db7 20 73 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a   structures..*/.
16db8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
16db9 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53  oid sqlite3VdbeS
16dba 77 61 70 28 56 64 62 65 20 2a 70 41 2c 20 56 64  wap(Vdbe *pA, Vd
16dbb 62 65 20 2a 70 42 29 7b 0a 20 20 56 64 62 65 20  be *pB){.  Vdbe 
16dbc 74 6d 70 2c 20 2a 70 54 6d 70 3b 0a 20 20 63 68  tmp, *pTmp;.  ch
16dbd 61 72 20 2a 7a 54 6d 70 3b 0a 20 20 69 6e 74 20  ar *zTmp;.  int 
16dbe 6e 54 6d 70 3b 0a 20 20 74 6d 70 20 3d 20 2a 70  nTmp;.  tmp = *p
16dbf 41 3b 0a 20 20 2a 70 41 20 3d 20 2a 70 42 3b 0a  A;.  *pA = *pB;.
16dc0 20 20 2a 70 42 20 3d 20 74 6d 70 3b 0a 20 20 70    *pB = tmp;.  p
16dc1 54 6d 70 20 3d 20 70 41 2d 3e 70 4e 65 78 74 3b  Tmp = pA->pNext;
16dc2 0a 20 20 70 41 2d 3e 70 4e 65 78 74 20 3d 20 70  .  pA->pNext = p
16dc3 42 2d 3e 70 4e 65 78 74 3b 0a 20 20 70 42 2d 3e  B->pNext;.  pB->
16dc4 70 4e 65 78 74 20 3d 20 70 54 6d 70 3b 0a 20 20  pNext = pTmp;.  
16dc5 70 54 6d 70 20 3d 20 70 41 2d 3e 70 50 72 65 76  pTmp = pA->pPrev
16dc6 3b 0a 20 20 70 41 2d 3e 70 50 72 65 76 20 3d 20  ;.  pA->pPrev = 
16dc7 70 42 2d 3e 70 50 72 65 76 3b 0a 20 20 70 42 2d  pB->pPrev;.  pB-
16dc8 3e 70 50 72 65 76 20 3d 20 70 54 6d 70 3b 0a 20  >pPrev = pTmp;. 
16dc9 20 7a 54 6d 70 20 3d 20 70 41 2d 3e 7a 53 71 6c   zTmp = pA->zSql
16dca 3b 0a 20 20 70 41 2d 3e 7a 53 71 6c 20 3d 20 70  ;.  pA->zSql = p
16dcb 42 2d 3e 7a 53 71 6c 3b 0a 20 20 70 42 2d 3e 7a  B->zSql;.  pB->z
16dcc 53 71 6c 20 3d 20 7a 54 6d 70 3b 0a 20 20 6e 54  Sql = zTmp;.  nT
16dcd 6d 70 20 3d 20 70 41 2d 3e 6e 53 71 6c 3b 0a 20  mp = pA->nSql;. 
16dce 20 70 41 2d 3e 6e 53 71 6c 20 3d 20 70 42 2d 3e   pA->nSql = pB->
16dcf 6e 53 71 6c 3b 0a 20 20 70 42 2d 3e 6e 53 71 6c  nSql;.  pB->nSql
16dd0 20 3d 20 6e 54 6d 70 3b 0a 7d 0a 0a 23 69 66 64   = nTmp;.}..#ifd
16dd1 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
16dd2 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 72 61 63 69  /*.** Turn traci
16dd3 6e 67 20 6f 6e 20 6f 72 20 6f 66 66 0a 2a 2f 0a  ng on or off.*/.
16dd4 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
16dd5 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 54  oid sqlite3VdbeT
16dd6 72 61 63 65 28 56 64 62 65 20 2a 70 2c 20 46 49  race(Vdbe *p, FI
16dd7 4c 45 20 2a 74 72 61 63 65 29 7b 0a 20 20 70 2d  LE *trace){.  p-
16dd8 3e 74 72 61 63 65 20 3d 20 74 72 61 63 65 3b 0a  >trace = trace;.
16dd9 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
16dda 52 65 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e  Resize the Vdbe.
16ddb 61 4f 70 20 61 72 72 61 79 20 73 6f 20 74 68 61  aOp array so tha
16ddc 74 20 69 74 20 69 73 20 61 74 20 6c 65 61 73 74  t it is at least
16ddd 20 6f 6e 65 20 6f 70 20 6c 61 72 67 65 72 20 74   one op larger t
16dde 68 61 6e 20 0a 2a 2a 20 69 74 20 77 61 73 2e 0a  han .** it was..
16ddf 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 6f 75 74 2d  **.** If an out-
16de0 6f 66 2d 6d 65 6d 6f 72 79 20 65 72 72 6f 72 20  of-memory error 
16de1 6f 63 63 75 72 73 20 77 68 69 6c 65 20 72 65 73  occurs while res
16de2 69 7a 69 6e 67 20 74 68 65 20 61 72 72 61 79 2c  izing the array,
16de3 20 72 65 74 75 72 6e 0a 2a 2a 20 53 51 4c 49 54   return.** SQLIT
16de4 45 5f 4e 4f 4d 45 4d 2e 20 49 6e 20 74 68 69 73  E_NOMEM. In this
16de5 20 63 61 73 65 20 56 64 62 65 2e 61 4f 70 20 61   case Vdbe.aOp a
16de6 6e 64 20 56 64 62 65 2e 6e 4f 70 41 6c 6c 6f 63  nd Vdbe.nOpAlloc
16de7 20 72 65 6d 61 69 6e 20 0a 2a 2a 20 75 6e 63 68   remain .** unch
16de8 61 6e 67 65 64 20 28 74 68 69 73 20 69 73 20 73  anged (this is s
16de9 6f 20 74 68 61 74 20 61 6e 79 20 6f 70 63 6f 64  o that any opcod
16dea 65 73 20 61 6c 72 65 61 64 79 20 61 6c 6c 6f 63  es already alloc
16deb 61 74 65 64 20 63 61 6e 20 62 65 20 0a 2a 2a 20  ated can be .** 
16dec 63 6f 72 72 65 63 74 6c 79 20 64 65 61 6c 6c 6f  correctly deallo
16ded 63 61 74 65 64 20 61 6c 6f 6e 67 20 77 69 74 68  cated along with
16dee 20 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65   the rest of the
16def 20 56 64 62 65 29 2e 0a 2a 2f 0a 73 74 61 74 69   Vdbe)..*/.stati
16df0 63 20 69 6e 74 20 67 72 6f 77 4f 70 41 72 72 61  c int growOpArra
16df1 79 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 56 64  y(Vdbe *p){.  Vd
16df2 62 65 4f 70 20 2a 70 4e 65 77 3b 0a 20 20 69 6e  beOp *pNew;.  in
16df3 74 20 6e 4e 65 77 20 3d 20 28 70 2d 3e 6e 4f 70  t nNew = (p->nOp
16df4 41 6c 6c 6f 63 20 3f 20 70 2d 3e 6e 4f 70 41 6c  Alloc ? p->nOpAl
16df5 6c 6f 63 2a 32 20 3a 20 28 69 6e 74 29 28 31 30  loc*2 : (int)(10
16df6 32 34 2f 73 69 7a 65 6f 66 28 4f 70 29 29 29 3b  24/sizeof(Op)));
16df7 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
16df8 33 44 62 52 65 61 6c 6c 6f 63 28 70 2d 3e 64 62  3DbRealloc(p->db
16df9 2c 20 70 2d 3e 61 4f 70 2c 20 6e 4e 65 77 2a 73  , p->aOp, nNew*s
16dfa 69 7a 65 6f 66 28 4f 70 29 29 3b 0a 20 20 69 66  izeof(Op));.  if
16dfb 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 70 2d  ( pNew ){.    p-
16dfc 3e 6e 4f 70 41 6c 6c 6f 63 20 3d 20 6e 4e 65 77  >nOpAlloc = nNew
16dfd 3b 0a 20 20 20 20 70 2d 3e 61 4f 70 20 3d 20 70  ;.    p->aOp = p
16dfe 4e 65 77 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  New;.  }.  retur
16dff 6e 20 28 70 4e 65 77 20 3f 20 53 51 4c 49 54 45  n (pNew ? SQLITE
16e00 5f 4f 4b 20 3a 20 53 51 4c 49 54 45 5f 4e 4f 4d  _OK : SQLITE_NOM
16e01 45 4d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64  EM);.}../*.** Ad
16e02 64 20 61 20 6e 65 77 20 69 6e 73 74 72 75 63 74  d a new instruct
16e03 69 6f 6e 20 74 6f 20 74 68 65 20 6c 69 73 74 20  ion to the list 
16e04 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 20  of instructions 
16e05 63 75 72 72 65 6e 74 20 69 6e 20 74 68 65 0a 2a  current in the.*
16e06 2a 20 56 44 42 45 2e 20 20 52 65 74 75 72 6e 20  * VDBE.  Return 
16e07 74 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74  the address of t
16e08 68 65 20 6e 65 77 20 69 6e 73 74 72 75 63 74 69  he new instructi
16e09 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61 6d 65  on..**.** Parame
16e0a 74 65 72 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 70  ters:.**.**    p
16e0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 50                 P
16e0c 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 56 44  ointer to the VD
16e0d 42 45 0a 2a 2a 0a 2a 2a 20 20 20 20 6f 70 20 20  BE.**.**    op  
16e0e 20 20 20 20 20 20 20 20 20 20 20 20 54 68 65 20              The 
16e0f 6f 70 63 6f 64 65 20 66 6f 72 20 74 68 69 73 20  opcode for this 
16e10 69 6e 73 74 72 75 63 74 69 6f 6e 0a 2a 2a 0a 2a  instruction.**.*
16e11 2a 20 20 20 20 70 31 2c 20 70 32 2c 20 70 33 20  *    p1, p2, p3 
16e12 20 20 20 20 20 4f 70 65 72 61 6e 64 73 0a 2a 2a       Operands.**
16e13 0a 2a 2a 20 55 73 65 20 74 68 65 20 73 71 6c 69  .** Use the sqli
16e14 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
16e15 62 65 6c 28 29 20 66 75 6e 63 74 69 6f 6e 20 74  bel() function t
16e16 6f 20 66 69 78 20 61 6e 20 61 64 64 72 65 73 73  o fix an address
16e17 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 71 6c 69   and.** the sqli
16e18 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
16e19 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 63 68  ) function to ch
16e1a 61 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f  ange the value o
16e1b 66 20 74 68 65 20 50 34 0a 2a 2a 20 6f 70 65 72  f the P4.** oper
16e1c 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  and..*/.SQLITE_P
16e1d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16e1e 65 33 56 64 62 65 41 64 64 4f 70 33 28 56 64 62  e3VdbeAddOp3(Vdb
16e1f 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
16e20 74 20 70 31 2c 20 69 6e 74 20 70 32 2c 20 69 6e  t p1, int p2, in
16e21 74 20 70 33 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  t p3){.  int i;.
16e22 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 0a    VdbeOp *pOp;..
16e23 20 20 69 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20 20    i = p->nOp;.  
16e24 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63  assert( p->magic
16e25 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49  ==VDBE_MAGIC_INI
16e26 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f  T );.  if( p->nO
16e27 70 41 6c 6c 6f 63 3c 3d 69 20 29 7b 0a 20 20 20  pAlloc<=i ){.   
16e28 20 69 66 28 20 67 72 6f 77 4f 70 41 72 72 61 79   if( growOpArray
16e29 28 70 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  (p) ){.      ret
16e2a 75 72 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  urn 0;.    }.  }
16e2b 0a 20 20 70 2d 3e 6e 4f 70 2b 2b 3b 0a 20 20 70  .  p->nOp++;.  p
16e2c 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b  Op = &p->aOp[i];
16e2d 0a 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d  .  pOp->opcode =
16e2e 20 6f 70 3b 0a 20 20 70 4f 70 2d 3e 70 35 20 3d   op;.  pOp->p5 =
16e2f 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 31 20 3d 20   0;.  pOp->p1 = 
16e30 70 31 3b 0a 20 20 70 4f 70 2d 3e 70 32 20 3d 20  p1;.  pOp->p2 = 
16e31 70 32 3b 0a 20 20 70 4f 70 2d 3e 70 33 20 3d 20  p2;.  pOp->p3 = 
16e32 70 33 3b 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20  p3;.  pOp->p4.p 
16e33 3d 20 30 3b 0a 20 20 70 4f 70 2d 3e 70 34 74 79  = 0;.  pOp->p4ty
16e34 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b  pe = P4_NOTUSED;
16e35 0a 20 20 70 2d 3e 65 78 70 69 72 65 64 20 3d 20  .  p->expired = 
16e36 30 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  0;.#ifdef SQLITE
16e37 5f 44 45 42 55 47 0a 20 20 70 4f 70 2d 3e 7a 43  _DEBUG.  pOp->zC
16e38 6f 6d 6d 65 6e 74 20 3d 20 30 3b 0a 20 20 69 66  omment = 0;.  if
16e39 28 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  ( sqlite3VdbeAdd
16e3a 6f 70 54 72 61 63 65 20 29 20 73 71 6c 69 74 65  opTrace ) sqlite
16e3b 33 56 64 62 65 50 72 69 6e 74 4f 70 28 30 2c 20  3VdbePrintOp(0, 
16e3c 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a  i, &p->aOp[i]);.
16e3d 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 56 44  #endif.#ifdef VD
16e3e 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 70 4f 70  BE_PROFILE.  pOp
16e3f 2d 3e 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20  ->cycles = 0;.  
16e40 70 4f 70 2d 3e 63 6e 74 20 3d 20 30 3b 0a 23 65  pOp->cnt = 0;.#e
16e41 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 69 3b  ndif.  return i;
16e42 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
16e43 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
16e44 65 41 64 64 4f 70 30 28 56 64 62 65 20 2a 70 2c  eAddOp0(Vdbe *p,
16e45 20 69 6e 74 20 6f 70 29 7b 0a 20 20 72 65 74 75   int op){.  retu
16e46 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  rn sqlite3VdbeAd
16e47 64 4f 70 33 28 70 2c 20 6f 70 2c 20 30 2c 20 30  dOp3(p, op, 0, 0
16e48 2c 20 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50  , 0);.}.SQLITE_P
16e49 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
16e4a 65 33 56 64 62 65 41 64 64 4f 70 31 28 56 64 62  e3VdbeAddOp1(Vdb
16e4b 65 20 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e  e *p, int op, in
16e4c 74 20 70 31 29 7b 0a 20 20 72 65 74 75 72 6e 20  t p1){.  return 
16e4d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
16e4e 33 28 70 2c 20 6f 70 2c 20 70 31 2c 20 30 2c 20  3(p, op, p1, 0, 
16e4f 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49  0);.}.SQLITE_PRI
16e50 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
16e51 56 64 62 65 41 64 64 4f 70 32 28 56 64 62 65 20  VdbeAddOp2(Vdbe 
16e52 2a 70 2c 20 69 6e 74 20 6f 70 2c 20 69 6e 74 20  *p, int op, int 
16e53 70 31 2c 20 69 6e 74 20 70 32 29 7b 0a 20 20 72  p1, int p2){.  r
16e54 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62  eturn sqlite3Vdb
16e55 65 41 64 64 4f 70 33 28 70 2c 20 6f 70 2c 20 70  eAddOp3(p, op, p
16e56 31 2c 20 70 32 2c 20 30 29 3b 0a 7d 0a 0a 0a 2f  1, p2, 0);.}.../
16e57 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 6f 70 63 6f  *.** Add an opco
16e58 64 65 20 74 68 61 74 20 69 6e 63 6c 75 64 65 73  de that includes
16e59 20 74 68 65 20 70 34 20 76 61 6c 75 65 20 61 73   the p4 value as
16e5a 20 61 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53   a pointer..*/.S
16e5b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
16e5c 74 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  t sqlite3VdbeAdd
16e5d 4f 70 34 28 0a 20 20 56 64 62 65 20 2a 70 2c 20  Op4(.  Vdbe *p, 
16e5e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64             /* Ad
16e5f 64 20 74 68 65 20 6f 70 63 6f 64 65 20 74 6f 20  d the opcode to 
16e60 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 69 6e 74  this VM */.  int
16e61 20 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   op,            
16e62 20 2f 2a 20 54 68 65 20 6e 65 77 20 6f 70 63 6f   /* The new opco
16e63 64 65 20 2a 2f 0a 20 20 69 6e 74 20 70 31 2c 20  de */.  int p1, 
16e64 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
16e65 68 65 20 50 31 20 6f 70 65 72 61 6e 64 20 2a 2f  he P1 operand */
16e66 0a 20 20 69 6e 74 20 70 32 2c 20 20 20 20 20 20  .  int p2,      
16e67 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 50 32         /* The P2
16e68 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69 6e   operand */.  in
16e69 74 20 70 33 2c 20 20 20 20 20 20 20 20 20 20 20  t p3,           
16e6a 20 20 2f 2a 20 54 68 65 20 50 33 20 6f 70 65 72    /* The P3 oper
16e6b 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  and */.  const c
16e6c 68 61 72 20 2a 7a 50 34 2c 20 20 20 20 2f 2a 20  har *zP4,    /* 
16e6d 54 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20 2a  The P4 operand *
16e6e 2f 0a 20 20 69 6e 74 20 70 34 74 79 70 65 20 20  /.  int p4type  
16e6f 20 20 20 20 20 20 20 20 2f 2a 20 50 34 20 6f 70          /* P4 op
16e70 65 72 61 6e 64 20 74 79 70 65 20 2a 2f 0a 29 7b  erand type */.){
16e71 0a 20 20 69 6e 74 20 61 64 64 72 20 3d 20 73 71  .  int addr = sq
16e72 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
16e73 70 2c 20 6f 70 2c 20 70 31 2c 20 70 32 2c 20 70  p, op, p1, p2, p
16e74 33 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  3);.  sqlite3Vdb
16e75 65 43 68 61 6e 67 65 50 34 28 70 2c 20 61 64 64  eChangeP4(p, add
16e76 72 2c 20 7a 50 34 2c 20 70 34 74 79 70 65 29 3b  r, zP4, p4type);
16e77 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72 3b 0a  .  return addr;.
16e78 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20  }../*.** Create 
16e79 61 20 6e 65 77 20 73 79 6d 62 6f 6c 69 63 20 6c  a new symbolic l
16e7a 61 62 65 6c 20 66 6f 72 20 61 6e 20 69 6e 73 74  abel for an inst
16e7b 72 75 63 74 69 6f 6e 20 74 68 61 74 20 68 61 73  ruction that has
16e7c 20 79 65 74 20 74 6f 20 62 65 0a 2a 2a 20 63 6f   yet to be.** co
16e7d 64 65 64 2e 20 20 54 68 65 20 73 79 6d 62 6f 6c  ded.  The symbol
16e7e 69 63 20 6c 61 62 65 6c 20 69 73 20 72 65 61 6c  ic label is real
16e7f 6c 79 20 6a 75 73 74 20 61 20 6e 65 67 61 74 69  ly just a negati
16e80 76 65 20 6e 75 6d 62 65 72 2e 20 20 54 68 65 0a  ve number.  The.
16e81 2a 2a 20 6c 61 62 65 6c 20 63 61 6e 20 62 65 20  ** label can be 
16e82 75 73 65 64 20 61 73 20 74 68 65 20 50 32 20 76  used as the P2 v
16e83 61 6c 75 65 20 6f 66 20 61 6e 20 6f 70 65 72 61  alue of an opera
16e84 74 69 6f 6e 2e 20 20 4c 61 74 65 72 2c 20 77 68  tion.  Later, wh
16e85 65 6e 0a 2a 2a 20 74 68 65 20 6c 61 62 65 6c 20  en.** the label 
16e86 69 73 20 72 65 73 6f 6c 76 65 64 20 74 6f 20 61  is resolved to a
16e87 20 73 70 65 63 69 66 69 63 20 61 64 64 72 65 73   specific addres
16e88 73 2c 20 74 68 65 20 56 44 42 45 20 77 69 6c 6c  s, the VDBE will
16e89 20 73 63 61 6e 0a 2a 2a 20 74 68 72 6f 75 67 68   scan.** through
16e8a 20 69 74 73 20 6f 70 65 72 61 74 69 6f 6e 20 6c   its operation l
16e8b 69 73 74 20 61 6e 64 20 63 68 61 6e 67 65 20 61  ist and change a
16e8c 6c 6c 20 76 61 6c 75 65 73 20 6f 66 20 50 32 20  ll values of P2 
16e8d 77 68 69 63 68 20 6d 61 74 63 68 0a 2a 2a 20 74  which match.** t
16e8e 68 65 20 6c 61 62 65 6c 20 69 6e 74 6f 20 74 68  he label into th
16e8f 65 20 72 65 73 6f 6c 76 65 64 20 61 64 64 72 65  e resolved addre
16e90 73 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44  ss..**.** The VD
16e91 42 45 20 6b 6e 6f 77 73 20 74 68 61 74 20 61 20  BE knows that a 
16e92 50 32 20 76 61 6c 75 65 20 69 73 20 61 20 6c 61  P2 value is a la
16e93 62 65 6c 20 62 65 63 61 75 73 65 20 6c 61 62 65  bel because labe
16e94 6c 73 20 61 72 65 0a 2a 2a 20 61 6c 77 61 79 73  ls are.** always
16e95 20 6e 65 67 61 74 69 76 65 20 61 6e 64 20 50 32   negative and P2
16e96 20 76 61 6c 75 65 73 20 61 72 65 20 73 75 70 70   values are supp
16e97 6f 73 65 20 74 6f 20 62 65 20 6e 6f 6e 2d 6e 65  ose to be non-ne
16e98 67 61 74 69 76 65 2e 0a 2a 2a 20 48 65 6e 63 65  gative..** Hence
16e99 2c 20 61 20 6e 65 67 61 74 69 76 65 20 50 32 20  , a negative P2 
16e9a 76 61 6c 75 65 20 69 73 20 61 20 6c 61 62 65 6c  value is a label
16e9b 20 74 68 61 74 20 68 61 73 20 79 65 74 20 74 6f   that has yet to
16e9c 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2a   be resolved..**
16e9d 0a 2a 2a 20 5a 65 72 6f 20 69 73 20 72 65 74 75  .** Zero is retu
16e9e 72 6e 65 64 20 69 66 20 61 20 6d 61 6c 6c 6f 63  rned if a malloc
16e9f 28 29 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c  () fails..*/.SQL
16ea0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16ea1 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
16ea2 61 62 65 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20  abel(Vdbe *p){. 
16ea3 20 69 6e 74 20 69 3b 0a 20 20 69 20 3d 20 70 2d   int i;.  i = p-
16ea4 3e 6e 4c 61 62 65 6c 2b 2b 3b 0a 20 20 61 73 73  >nLabel++;.  ass
16ea5 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
16ea6 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
16ea7 3b 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4c  ;.  if( i>=p->nL
16ea8 61 62 65 6c 41 6c 6c 6f 63 20 29 7b 0a 20 20 20  abelAlloc ){.   
16ea9 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63 20   p->nLabelAlloc 
16eaa 3d 20 70 2d 3e 6e 4c 61 62 65 6c 41 6c 6c 6f 63  = p->nLabelAlloc
16eab 2a 32 20 2b 20 31 30 3b 0a 20 20 20 20 70 2d 3e  *2 + 10;.    p->
16eac 61 4c 61 62 65 6c 20 3d 20 73 71 6c 69 74 65 33  aLabel = sqlite3
16ead 44 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28  DbReallocOrFree(
16eae 70 2d 3e 64 62 2c 20 70 2d 3e 61 4c 61 62 65 6c  p->db, p->aLabel
16eaf 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
16eb0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
16eb1 20 20 20 20 20 20 70 2d 3e 6e 4c 61 62 65 6c 41        p->nLabelA
16eb2 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d 3e 61  lloc*sizeof(p->a
16eb3 4c 61 62 65 6c 5b 30 5d 29 29 3b 0a 20 20 7d 0a  Label[0]));.  }.
16eb4 20 20 69 66 28 20 70 2d 3e 61 4c 61 62 65 6c 20    if( p->aLabel 
16eb5 29 7b 0a 20 20 20 20 70 2d 3e 61 4c 61 62 65 6c  ){.    p->aLabel
16eb6 5b 69 5d 20 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20  [i] = -1;.  }.  
16eb7 72 65 74 75 72 6e 20 2d 31 2d 69 3b 0a 7d 0a 0a  return -1-i;.}..
16eb8 2f 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6c 61  /*.** Resolve la
16eb9 62 65 6c 20 22 78 22 20 74 6f 20 62 65 20 74 68  bel "x" to be th
16eba 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
16ebb 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
16ebc 6e 20 74 6f 0a 2a 2a 20 62 65 20 69 6e 73 65 72  n to.** be inser
16ebd 74 65 64 2e 20 20 54 68 65 20 70 61 72 61 6d 65  ted.  The parame
16ebe 74 65 72 20 22 78 22 20 6d 75 73 74 20 68 61 76  ter "x" must hav
16ebf 65 20 62 65 65 6e 20 6f 62 74 61 69 6e 65 64 20  e been obtained 
16ec0 66 72 6f 6d 0a 2a 2a 20 61 20 70 72 69 6f 72 20  from.** a prior 
16ec1 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33 56  call to sqlite3V
16ec2 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 29 2e 0a  dbeMakeLabel()..
16ec3 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
16ec4 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
16ec5 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 56  beResolveLabel(V
16ec6 64 62 65 20 2a 70 2c 20 69 6e 74 20 78 29 7b 0a  dbe *p, int x){.
16ec7 20 20 69 6e 74 20 6a 20 3d 20 2d 31 2d 78 3b 0a    int j = -1-x;.
16ec8 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
16ec9 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49  ic==VDBE_MAGIC_I
16eca 4e 49 54 20 29 3b 0a 20 20 61 73 73 65 72 74 28  NIT );.  assert(
16ecb 20 6a 3e 3d 30 20 26 26 20 6a 3c 70 2d 3e 6e 4c   j>=0 && j<p->nL
16ecc 61 62 65 6c 20 29 3b 0a 20 20 69 66 28 20 70 2d  abel );.  if( p-
16ecd 3e 61 4c 61 62 65 6c 20 29 7b 0a 20 20 20 20 70  >aLabel ){.    p
16ece 2d 3e 61 4c 61 62 65 6c 5b 6a 5d 20 3d 20 70 2d  ->aLabel[j] = p-
16ecf 3e 6e 4f 70 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  >nOp;.  }.}../*.
16ed0 2a 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68 20  ** Loop through 
16ed1 74 68 65 20 70 72 6f 67 72 61 6d 20 6c 6f 6f 6b  the program look
16ed2 69 6e 67 20 66 6f 72 20 50 32 20 76 61 6c 75 65  ing for P2 value
16ed3 73 20 74 68 61 74 20 61 72 65 20 6e 65 67 61 74  s that are negat
16ed4 69 76 65 0a 2a 2a 20 6f 6e 20 6a 75 6d 70 20 69  ive.** on jump i
16ed5 6e 73 74 72 75 63 74 69 6f 6e 73 2e 20 20 45 61  nstructions.  Ea
16ed6 63 68 20 73 75 63 68 20 76 61 6c 75 65 20 69 73  ch such value is
16ed7 20 61 20 6c 61 62 65 6c 2e 20 20 52 65 73 6f 6c   a label.  Resol
16ed8 76 65 20 74 68 65 0a 2a 2a 20 6c 61 62 65 6c 20  ve the.** label 
16ed9 62 79 20 73 65 74 74 69 6e 67 20 74 68 65 20 50  by setting the P
16eda 32 20 76 61 6c 75 65 20 74 6f 20 69 74 73 20 63  2 value to its c
16edb 6f 72 72 65 63 74 20 6e 6f 6e 2d 7a 65 72 6f 20  orrect non-zero 
16edc 76 61 6c 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  value..**.** Thi
16edd 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
16ede 6c 65 64 20 6f 6e 63 65 20 61 66 74 65 72 20 61  led once after a
16edf 6c 6c 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20  ll opcodes have 
16ee0 62 65 65 6e 20 69 6e 73 65 72 74 65 64 2e 0a 2a  been inserted..*
16ee1 2a 0a 2a 2a 20 56 61 72 69 61 62 6c 65 20 2a 70  *.** Variable *p
16ee2 4d 61 78 46 75 6e 63 41 72 67 73 20 69 73 20 73  MaxFuncArgs is s
16ee3 65 74 20 74 6f 20 74 68 65 20 6d 61 78 69 6d 75  et to the maximu
16ee4 6d 20 76 61 6c 75 65 20 6f 66 20 61 6e 79 20 50  m value of any P
16ee5 32 20 61 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 74  2 argument .** t
16ee6 6f 20 61 6e 20 4f 50 5f 46 75 6e 63 74 69 6f 6e  o an OP_Function
16ee7 2c 20 4f 50 5f 41 67 67 53 74 65 70 20 6f 72 20  , OP_AggStep or 
16ee8 4f 50 5f 56 46 69 6c 74 65 72 20 6f 70 63 6f 64  OP_VFilter opcod
16ee9 65 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  e. This is used 
16eea 62 79 20 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64  by .** sqlite3Vd
16eeb 62 65 4d 61 6b 65 52 65 61 64 79 28 29 20 74 6f  beMakeReady() to
16eec 20 73 69 7a 65 20 74 68 65 20 56 64 62 65 2e 61   size the Vdbe.a
16eed 70 41 72 67 5b 5d 20 61 72 72 61 79 2e 0a 2a 2a  pArg[] array..**
16eee 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
16eef 20 61 6c 73 6f 20 64 6f 65 73 20 74 68 65 20 66   also does the f
16ef0 6f 6c 6c 6f 77 69 6e 67 20 6f 70 74 69 6d 69 7a  ollowing optimiz
16ef1 61 74 69 6f 6e 3a 20 20 49 74 20 73 63 61 6e 73  ation:  It scans
16ef2 20 66 6f 72 0a 2a 2a 20 69 6e 73 74 72 75 63 74   for.** instruct
16ef3 69 6f 6e 73 20 74 68 61 74 20 6d 69 67 68 74 20  ions that might 
16ef4 63 61 75 73 65 20 61 20 73 74 61 74 65 6d 65 6e  cause a statemen
16ef5 74 20 72 6f 6c 6c 62 61 63 6b 2e 20 20 53 75 63  t rollback.  Suc
16ef6 68 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 0a 2a  h instructions.*
16ef7 2a 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 2a  * are:.**.**   *
16ef8 20 20 4f 50 5f 48 61 6c 74 20 77 69 74 68 20 50    OP_Halt with P
16ef9 31 3d 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41  1=SQLITE_CONSTRA
16efa 49 4e 54 20 61 6e 64 20 50 32 3d 4f 45 5f 41 62  INT and P2=OE_Ab
16efb 6f 72 74 2e 0a 2a 2a 20 20 20 2a 20 20 4f 50 5f  ort..**   *  OP_
16efc 44 65 73 74 72 6f 79 0a 2a 2a 20 20 20 2a 20 20  Destroy.**   *  
16efd 4f 50 5f 56 55 70 64 61 74 65 0a 2a 2a 20 20 20  OP_VUpdate.**   
16efe 2a 20 20 4f 50 5f 56 52 65 6e 61 6d 65 0a 2a 2a  *  OP_VRename.**
16eff 0a 2a 2a 20 49 66 20 6e 6f 20 73 75 63 68 20 69  .** If no such i
16f00 6e 73 74 72 75 63 74 69 6f 6e 20 69 73 20 66 6f  nstruction is fo
16f01 75 6e 64 2c 20 74 68 65 6e 20 65 76 65 72 79 20  und, then every 
16f02 53 74 61 74 65 6d 65 6e 74 20 69 6e 73 74 72 75  Statement instru
16f03 63 74 69 6f 6e 20 0a 2a 2a 20 69 73 20 63 68 61  ction .** is cha
16f04 6e 67 65 64 20 74 6f 20 61 20 4e 6f 6f 70 2e 20  nged to a Noop. 
16f05 20 49 6e 20 74 68 69 73 20 77 61 79 2c 20 77 65   In this way, we
16f06 20 61 76 6f 69 64 20 63 72 65 61 74 69 6e 67 20   avoid creating 
16f07 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20 0a 2a  the statement .*
16f08 2a 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 75  * journal file u
16f09 6e 6e 65 63 65 73 73 61 72 69 6c 79 2e 0a 2a 2f  nnecessarily..*/
16f0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 73  .static void res
16f0b 6f 6c 76 65 50 32 56 61 6c 75 65 73 28 56 64 62  olveP2Values(Vdb
16f0c 65 20 2a 70 2c 20 69 6e 74 20 2a 70 4d 61 78 46  e *p, int *pMaxF
16f0d 75 6e 63 41 72 67 73 29 7b 0a 20 20 69 6e 74 20  uncArgs){.  int 
16f0e 69 3b 0a 20 20 69 6e 74 20 6e 4d 61 78 41 72 67  i;.  int nMaxArg
16f0f 73 20 3d 20 30 3b 0a 20 20 4f 70 20 2a 70 4f 70  s = 0;.  Op *pOp
16f10 3b 0a 20 20 69 6e 74 20 2a 61 4c 61 62 65 6c 20  ;.  int *aLabel 
16f11 3d 20 70 2d 3e 61 4c 61 62 65 6c 3b 0a 20 20 69  = p->aLabel;.  i
16f12 6e 74 20 64 6f 65 73 53 74 61 74 65 6d 65 6e 74  nt doesStatement
16f13 52 6f 6c 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20  Rollback = 0;.  
16f14 69 6e 74 20 68 61 73 53 74 61 74 65 6d 65 6e 74  int hasStatement
16f15 42 65 67 69 6e 20 3d 20 30 3b 0a 20 20 70 2d 3e  Begin = 0;.  p->
16f16 72 65 61 64 4f 6e 6c 79 20 3d 20 31 3b 0a 20 20  readOnly = 1;.  
16f17 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f 75 72 6e  p->usesStmtJourn
16f18 61 6c 20 3d 20 30 3b 0a 20 20 66 6f 72 28 70 4f  al = 0;.  for(pO
16f19 70 3d 70 2d 3e 61 4f 70 2c 20 69 3d 70 2d 3e 6e  p=p->aOp, i=p->n
16f1a 4f 70 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c  Op-1; i>=0; i--,
16f1b 20 70 4f 70 2b 2b 29 7b 0a 20 20 20 20 75 38 20   pOp++){.    u8 
16f1c 6f 70 63 6f 64 65 20 3d 20 70 4f 70 2d 3e 6f 70  opcode = pOp->op
16f1d 63 6f 64 65 3b 0a 0a 20 20 20 20 69 66 28 20 6f  code;..    if( o
16f1e 70 63 6f 64 65 3d 3d 4f 50 5f 46 75 6e 63 74 69  pcode==OP_Functi
16f1f 6f 6e 20 7c 7c 20 6f 70 63 6f 64 65 3d 3d 4f 50  on || opcode==OP
16f20 5f 41 67 67 53 74 65 70 20 29 7b 0a 20 20 20 20  _AggStep ){.    
16f21 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 3e 6e 4d    if( pOp->p5>nM
16f22 61 78 41 72 67 73 20 29 20 6e 4d 61 78 41 72 67  axArgs ) nMaxArg
16f23 73 20 3d 20 70 4f 70 2d 3e 70 35 3b 0a 23 69 66  s = pOp->p5;.#if
16f24 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
16f25 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
16f26 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 63 6f    }else if( opco
16f27 64 65 3d 3d 4f 50 5f 56 55 70 64 61 74 65 20 29  de==OP_VUpdate )
16f28 7b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  {.      if( pOp-
16f29 3e 70 32 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e  >p2>nMaxArgs ) n
16f2a 4d 61 78 41 72 67 73 20 3d 20 70 4f 70 2d 3e 70  MaxArgs = pOp->p
16f2b 32 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 0a  2;.#endif.    }.
16f2c 20 20 20 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d      if( opcode==
16f2d 4f 50 5f 48 61 6c 74 20 29 7b 0a 20 20 20 20 20  OP_Halt ){.     
16f2e 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d 53 51   if( pOp->p1==SQ
16f2f 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 20  LITE_CONSTRAINT 
16f30 26 26 20 70 4f 70 2d 3e 70 32 3d 3d 4f 45 5f 41  && pOp->p2==OE_A
16f31 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20  bort ){.        
16f32 64 6f 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c  doesStatementRol
16f33 6c 62 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 20  lback = 1;.     
16f34 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
16f35 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 53 74 61 74   opcode==OP_Stat
16f36 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 68  ement ){.      h
16f37 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69 6e  asStatementBegin
16f38 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 2d 3e 75   = 1;.      p->u
16f39 73 65 73 53 74 6d 74 4a 6f 75 72 6e 61 6c 20 3d  sesStmtJournal =
16f3a 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   1;.    }else if
16f3b 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 44 65 73  ( opcode==OP_Des
16f3c 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 64 6f  troy ){.      do
16f3d 65 73 53 74 61 74 65 6d 65 6e 74 52 6f 6c 6c 62  esStatementRollb
16f3e 61 63 6b 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c  ack = 1;.    }el
16f3f 73 65 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f  se if( opcode==O
16f40 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 20 26 26  P_Transaction &&
16f41 20 70 4f 70 2d 3e 70 32 21 3d 30 20 29 7b 0a 20   pOp->p2!=0 ){. 
16f42 20 20 20 20 20 70 2d 3e 72 65 61 64 4f 6e 6c 79       p->readOnly
16f43 20 3d 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51   = 0;.#ifndef SQ
16f44 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
16f45 4c 54 41 42 4c 45 0a 20 20 20 20 7d 65 6c 73 65  LTABLE.    }else
16f46 20 69 66 28 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f   if( opcode==OP_
16f47 56 55 70 64 61 74 65 20 7c 7c 20 6f 70 63 6f 64  VUpdate || opcod
16f48 65 3d 3d 4f 50 5f 56 52 65 6e 61 6d 65 20 29 7b  e==OP_VRename ){
16f49 0a 20 20 20 20 20 20 64 6f 65 73 53 74 61 74 65  .      doesState
16f4a 6d 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 3d 20 31  mentRollback = 1
16f4b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
16f4c 6f 70 63 6f 64 65 3d 3d 4f 50 5f 56 46 69 6c 74  opcode==OP_VFilt
16f4d 65 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  er ){.      int 
16f4e 6e 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  n;.      assert(
16f4f 20 70 2d 3e 6e 4f 70 20 2d 20 69 20 3e 3d 20 33   p->nOp - i >= 3
16f50 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
16f51 28 20 70 4f 70 5b 2d 31 5d 2e 6f 70 63 6f 64 65  ( pOp[-1].opcode
16f52 3d 3d 4f 50 5f 49 6e 74 65 67 65 72 20 29 3b 0a  ==OP_Integer );.
16f53 20 20 20 20 20 20 6e 20 3d 20 70 4f 70 5b 2d 31        n = pOp[-1
16f54 5d 2e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20  ].p1;.      if( 
16f55 6e 3e 6e 4d 61 78 41 72 67 73 20 29 20 6e 4d 61  n>nMaxArgs ) nMa
16f56 78 41 72 67 73 20 3d 20 6e 3b 0a 23 65 6e 64 69  xArgs = n;.#endi
16f57 66 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28  f.    }..    if(
16f58 20 73 71 6c 69 74 65 33 56 64 62 65 4f 70 63 6f   sqlite3VdbeOpco
16f59 64 65 48 61 73 50 72 6f 70 65 72 74 79 28 6f 70  deHasProperty(op
16f5a 63 6f 64 65 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50  code, OPFLG_JUMP
16f5b 29 20 26 26 20 70 4f 70 2d 3e 70 32 3c 30 20 29  ) && pOp->p2<0 )
16f5c 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
16f5d 2d 31 2d 70 4f 70 2d 3e 70 32 3c 70 2d 3e 6e 4c  -1-pOp->p2<p->nL
16f5e 61 62 65 6c 20 29 3b 0a 20 20 20 20 20 20 70 4f  abel );.      pO
16f5f 70 2d 3e 70 32 20 3d 20 61 4c 61 62 65 6c 5b 2d  p->p2 = aLabel[-
16f60 31 2d 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20  1-pOp->p2];.    
16f61 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44  }.  }.  sqlite3D
16f62 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 70 2d 3e  bFree(p->db, p->
16f63 61 4c 61 62 65 6c 29 3b 0a 20 20 70 2d 3e 61 4c  aLabel);.  p->aL
16f64 61 62 65 6c 20 3d 20 30 3b 0a 0a 20 20 2a 70 4d  abel = 0;..  *pM
16f65 61 78 46 75 6e 63 41 72 67 73 20 3d 20 6e 4d 61  axFuncArgs = nMa
16f66 78 41 72 67 73 3b 0a 0a 20 20 2f 2a 20 49 66 20  xArgs;..  /* If 
16f67 77 65 20 6e 65 76 65 72 20 72 6f 6c 6c 62 61 63  we never rollbac
16f68 6b 20 61 20 73 74 61 74 65 6d 65 6e 74 20 74 72  k a statement tr
16f69 61 6e 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20  ansaction, then 
16f6a 73 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 74  statement.  ** t
16f6b 72 61 6e 73 61 63 74 69 6f 6e 73 20 61 72 65 20  ransactions are 
16f6c 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20 53 6f 20  not needed.  So 
16f6d 63 68 61 6e 67 65 20 65 76 65 72 79 20 4f 50 5f  change every OP_
16f6e 53 74 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 6f  Statement.  ** o
16f6f 70 63 6f 64 65 20 69 6e 74 6f 20 61 6e 20 4f 50  pcode into an OP
16f70 5f 4e 6f 6f 70 2e 20 20 54 68 69 73 20 61 76 6f  _Noop.  This avo
16f71 69 64 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c  id a call to sql
16f72 69 74 65 33 4f 73 4f 70 65 6e 45 78 63 6c 75 73  ite3OsOpenExclus
16f73 69 76 65 28 29 0a 20 20 2a 2a 20 77 68 69 63 68  ive().  ** which
16f74 20 63 61 6e 20 62 65 20 65 78 70 65 6e 73 69 76   can be expensiv
16f75 65 20 6f 6e 20 73 6f 6d 65 20 70 6c 61 74 66 6f  e on some platfo
16f76 72 6d 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  rms..  */.  if( 
16f77 68 61 73 53 74 61 74 65 6d 65 6e 74 42 65 67 69  hasStatementBegi
16f78 6e 20 26 26 20 21 64 6f 65 73 53 74 61 74 65 6d  n && !doesStatem
16f79 65 6e 74 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20  entRollback ){. 
16f7a 20 20 20 70 2d 3e 75 73 65 73 53 74 6d 74 4a 6f     p->usesStmtJo
16f7b 75 72 6e 61 6c 20 3d 20 30 3b 0a 20 20 20 20 66  urnal = 0;.    f
16f7c 6f 72 28 70 4f 70 3d 70 2d 3e 61 4f 70 2c 20 69  or(pOp=p->aOp, i
16f7d 3d 70 2d 3e 6e 4f 70 2d 31 3b 20 69 3e 3d 30 3b  =p->nOp-1; i>=0;
16f7e 20 69 2d 2d 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20   i--, pOp++){.  
16f7f 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70 63      if( pOp->opc
16f80 6f 64 65 3d 3d 4f 50 5f 53 74 61 74 65 6d 65 6e  ode==OP_Statemen
16f81 74 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4f 70  t ){.        pOp
16f82 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 6f  ->opcode = OP_No
16f83 6f 70 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  op;.      }.    
16f84 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  }.  }.}../*.** R
16f85 65 74 75 72 6e 20 74 68 65 20 61 64 64 72 65 73  eturn the addres
16f86 73 20 6f 66 20 74 68 65 20 6e 65 78 74 20 69 6e  s of the next in
16f87 73 74 72 75 63 74 69 6f 6e 20 74 6f 20 62 65 20  struction to be 
16f88 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 53 51 4c  inserted..*/.SQL
16f89 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
16f8a 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
16f8b 6e 74 41 64 64 72 28 56 64 62 65 20 2a 70 29 7b  ntAddr(Vdbe *p){
16f8c 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61  .  assert( p->ma
16f8d 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic==VDBE_MAGIC_
16f8e 49 4e 49 54 20 29 3b 0a 20 20 72 65 74 75 72 6e  INIT );.  return
16f8f 20 70 2d 3e 6e 4f 70 3b 0a 7d 0a 0a 2f 2a 0a 2a   p->nOp;.}../*.*
16f90 2a 20 41 64 64 20 61 20 77 68 6f 6c 65 20 6c 69  * Add a whole li
16f91 73 74 20 6f 66 20 6f 70 65 72 61 74 69 6f 6e 73  st of operations
16f92 20 74 6f 20 74 68 65 20 6f 70 65 72 61 74 69 6f   to the operatio
16f93 6e 20 73 74 61 63 6b 2e 20 20 52 65 74 75 72 6e  n stack.  Return
16f94 20 74 68 65 0a 2a 2a 20 61 64 64 72 65 73 73 20   the.** address 
16f95 6f 66 20 74 68 65 20 66 69 72 73 74 20 6f 70 65  of the first ope
16f96 72 61 74 69 6f 6e 20 61 64 64 65 64 2e 0a 2a 2f  ration added..*/
16f97 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
16f98 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 41  int sqlite3VdbeA
16f99 64 64 4f 70 4c 69 73 74 28 56 64 62 65 20 2a 70  ddOpList(Vdbe *p
16f9a 2c 20 69 6e 74 20 6e 4f 70 2c 20 56 64 62 65 4f  , int nOp, VdbeO
16f9b 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 61 4f 70  pList const *aOp
16f9c 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  ){.  int addr;. 
16f9d 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69   assert( p->magi
16f9e 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
16f9f 49 54 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e  IT );.  if( p->n
16fa0 4f 70 20 2b 20 6e 4f 70 20 3e 20 70 2d 3e 6e 4f  Op + nOp > p->nO
16fa1 70 41 6c 6c 6f 63 20 26 26 20 67 72 6f 77 4f 70  pAlloc && growOp
16fa2 41 72 72 61 79 28 70 29 20 29 7b 0a 20 20 20 20  Array(p) ){.    
16fa3 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
16fa4 61 64 64 72 20 3d 20 70 2d 3e 6e 4f 70 3b 0a 20  addr = p->nOp;. 
16fa5 20 69 66 28 20 6e 4f 70 3e 30 20 29 7b 0a 20 20   if( nOp>0 ){.  
16fa6 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 56 64 62    int i;.    Vdb
16fa7 65 4f 70 4c 69 73 74 20 63 6f 6e 73 74 20 2a 70  eOpList const *p
16fa8 49 6e 20 3d 20 61 4f 70 3b 0a 20 20 20 20 66 6f  In = aOp;.    fo
16fa9 72 28 69 3d 30 3b 20 69 3c 6e 4f 70 3b 20 69 2b  r(i=0; i<nOp; i+
16faa 2b 2c 20 70 49 6e 2b 2b 29 7b 0a 20 20 20 20 20  +, pIn++){.     
16fab 20 69 6e 74 20 70 32 20 3d 20 70 49 6e 2d 3e 70   int p2 = pIn->p
16fac 32 3b 0a 20 20 20 20 20 20 56 64 62 65 4f 70 20  2;.      VdbeOp 
16fad 2a 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4f 70 5b  *pOut = &p->aOp[
16fae 69 2b 61 64 64 72 5d 3b 0a 20 20 20 20 20 20 70  i+addr];.      p
16faf 4f 75 74 2d 3e 6f 70 63 6f 64 65 20 3d 20 70 49  Out->opcode = pI
16fb0 6e 2d 3e 6f 70 63 6f 64 65 3b 0a 20 20 20 20 20  n->opcode;.     
16fb1 20 70 4f 75 74 2d 3e 70 31 20 3d 20 70 49 6e 2d   pOut->p1 = pIn-
16fb2 3e 70 31 3b 0a 20 20 20 20 20 20 69 66 28 20 70  >p1;.      if( p
16fb3 32 3c 30 20 26 26 20 73 71 6c 69 74 65 33 56 64  2<0 && sqlite3Vd
16fb4 62 65 4f 70 63 6f 64 65 48 61 73 50 72 6f 70 65  beOpcodeHasPrope
16fb5 72 74 79 28 70 4f 75 74 2d 3e 6f 70 63 6f 64 65  rty(pOut->opcode
16fb6 2c 20 4f 50 46 4c 47 5f 4a 55 4d 50 29 20 29 7b  , OPFLG_JUMP) ){
16fb7 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e 70  .        pOut->p
16fb8 32 20 3d 20 61 64 64 72 20 2b 20 41 44 44 52 28  2 = addr + ADDR(
16fb9 70 32 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  p2);.      }else
16fba 7b 0a 20 20 20 20 20 20 20 20 70 4f 75 74 2d 3e  {.        pOut->
16fbb 70 32 20 3d 20 70 32 3b 0a 20 20 20 20 20 20 7d  p2 = p2;.      }
16fbc 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 33 20  .      pOut->p3 
16fbd 3d 20 70 49 6e 2d 3e 70 33 3b 0a 20 20 20 20 20  = pIn->p3;.     
16fbe 20 70 4f 75 74 2d 3e 70 34 74 79 70 65 20 3d 20   pOut->p4type = 
16fbf 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20 20 20 20  P4_NOTUSED;.    
16fc0 20 20 70 4f 75 74 2d 3e 70 34 2e 70 20 3d 20 30    pOut->p4.p = 0
16fc1 3b 0a 20 20 20 20 20 20 70 4f 75 74 2d 3e 70 35  ;.      pOut->p5
16fc2 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 53 51 4c   = 0;.#ifdef SQL
16fc3 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20  ITE_DEBUG.      
16fc4 70 4f 75 74 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3d  pOut->zComment =
16fc5 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71   0;.      if( sq
16fc6 6c 69 74 65 33 56 64 62 65 41 64 64 6f 70 54 72  lite3VdbeAddopTr
16fc7 61 63 65 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ace ){.        s
16fc8 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74 4f  qlite3VdbePrintO
16fc9 70 28 30 2c 20 69 2b 61 64 64 72 2c 20 26 70 2d  p(0, i+addr, &p-
16fca 3e 61 4f 70 5b 69 2b 61 64 64 72 5d 29 3b 0a 20  >aOp[i+addr]);. 
16fcb 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20       }.#endif.  
16fcc 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 4f 70 20 2b    }.    p->nOp +
16fcd 3d 20 6e 4f 70 3b 0a 20 20 7d 0a 20 20 72 65 74  = nOp;.  }.  ret
16fce 75 72 6e 20 61 64 64 72 3b 0a 7d 0a 0a 2f 2a 0a  urn addr;.}../*.
16fcf 2a 2a 20 43 68 61 6e 67 65 20 74 68 65 20 76 61  ** Change the va
16fd0 6c 75 65 20 6f 66 20 74 68 65 20 50 31 20 6f 70  lue of the P1 op
16fd1 65 72 61 6e 64 20 66 6f 72 20 61 20 73 70 65 63  erand for a spec
16fd2 69 66 69 63 20 69 6e 73 74 72 75 63 74 69 6f 6e  ific instruction
16fd3 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  ..** This routin
16fd4 65 20 69 73 20 75 73 65 66 75 6c 20 77 68 65 6e  e is useful when
16fd5 20 61 20 6c 61 72 67 65 20 70 72 6f 67 72 61 6d   a large program
16fd6 20 69 73 20 6c 6f 61 64 65 64 20 66 72 6f 6d 20   is loaded from 
16fd7 61 0a 2a 2a 20 73 74 61 74 69 63 20 61 72 72 61  a.** static arra
16fd8 79 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 56  y using sqlite3V
16fd9 64 62 65 41 64 64 4f 70 4c 69 73 74 20 62 75 74  dbeAddOpList but
16fda 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65   we want to make
16fdb 20 61 0a 2a 2a 20 66 65 77 20 6d 69 6e 6f 72 20   a.** few minor 
16fdc 63 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 70  changes to the p
16fdd 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49 54  rogram..*/.SQLIT
16fde 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
16fdf 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
16fe0 50 31 28 56 64 62 65 20 2a 70 2c 20 69 6e 74 20  P1(Vdbe *p, int 
16fe1 61 64 64 72 2c 20 69 6e 74 20 76 61 6c 29 7b 0a  addr, int val){.
16fe2 20 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 7c    assert( p==0 |
16fe3 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d 56 44 42 45  | p->magic==VDBE
16fe4 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29 3b 0a 20  _MAGIC_INIT );. 
16fe5 20 69 66 28 20 70 20 26 26 20 61 64 64 72 3e 3d   if( p && addr>=
16fe6 30 20 26 26 20 70 2d 3e 6e 4f 70 3e 61 64 64 72  0 && p->nOp>addr
16fe7 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a 20 20   && p->aOp ){.  
16fe8 20 20 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 2e 70    p->aOp[addr].p
16fe9 31 20 3d 20 76 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a  1 = val;.  }.}..
16fea 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20 74 68 65  /*.** Change the
16feb 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 50 32   value of the P2
16fec 20 6f 70 65 72 61 6e 64 20 66 6f 72 20 61 20 73   operand for a s
16fed 70 65 63 69 66 69 63 20 69 6e 73 74 72 75 63 74  pecific instruct
16fee 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ion..** This rou
16fef 74 69 6e 65 20 69 73 20 75 73 65 66 75 6c 20 66  tine is useful f
16ff0 6f 72 20 73 65 74 74 69 6e 67 20 61 20 6a 75 6d  or setting a jum
16ff1 70 20 64 65 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a  p destination..*
16ff2 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
16ff3 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
16ff4 65 43 68 61 6e 67 65 50 32 28 56 64 62 65 20 2a  eChangeP2(Vdbe *
16ff5 70 2c 20 69 6e 74 20 61 64 64 72 2c 20 69 6e 74  p, int addr, int
16ff6 20 76 61 6c 29 7b 0a 20 20 61 73 73 65 72 74 28   val){.  assert(
16ff7 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69   p==0 || p->magi
16ff8 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e  c==VDBE_MAGIC_IN
16ff9 49 54 20 29 3b 0a 20 20 69 66 28 20 70 20 26 26  IT );.  if( p &&
16ffa 20 61 64 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e   addr>=0 && p->n
16ffb 4f 70 3e 61 64 64 72 20 26 26 20 70 2d 3e 61 4f  Op>addr && p->aO
16ffc 70 20 29 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b  p ){.    p->aOp[
16ffd 61 64 64 72 5d 2e 70 32 20 3d 20 76 61 6c 3b 0a  addr].p2 = val;.
16ffe 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61    }.}../*.** Cha
16fff 6e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  nge the value of
17000 20 74 68 65 20 50 33 20 6f 70 65 72 61 6e 64 20   the P3 operand 
17001 66 6f 72 20 61 20 73 70 65 63 69 66 69 63 20 69  for a specific i
17002 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  nstruction..*/.S
17003 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17004 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
17005 61 6e 67 65 50 33 28 56 64 62 65 20 2a 70 2c 20  angeP3(Vdbe *p, 
17006 69 6e 74 20 61 64 64 72 2c 20 69 6e 74 20 76 61  int addr, int va
17007 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d  l){.  assert( p=
17008 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d  =0 || p->magic==
17009 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
1700a 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 61 64  );.  if( p && ad
1700b 64 72 3e 3d 30 20 26 26 20 70 2d 3e 6e 4f 70 3e  dr>=0 && p->nOp>
1700c 61 64 64 72 20 26 26 20 70 2d 3e 61 4f 70 20 29  addr && p->aOp )
1700d 7b 0a 20 20 20 20 70 2d 3e 61 4f 70 5b 61 64 64  {.    p->aOp[add
1700e 72 5d 2e 70 33 20 3d 20 76 61 6c 3b 0a 20 20 7d  r].p3 = val;.  }
1700f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65  .}../*.** Change
17010 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68   the value of th
17011 65 20 50 35 20 6f 70 65 72 61 6e 64 20 66 6f 72  e P5 operand for
17012 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
17013 6c 79 0a 2a 2a 20 61 64 64 65 64 20 6f 70 65 72  ly.** added oper
17014 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
17015 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
17016 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
17017 35 28 56 64 62 65 20 2a 70 2c 20 75 38 20 76 61  5(Vdbe *p, u8 va
17018 6c 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 3d  l){.  assert( p=
17019 3d 30 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d  =0 || p->magic==
1701a 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
1701b 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 70 2d  );.  if( p && p-
1701c 3e 61 4f 70 20 29 7b 0a 20 20 20 20 61 73 73 65  >aOp ){.    asse
1701d 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b 0a  rt( p->nOp>0 );.
1701e 20 20 20 20 70 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f      p->aOp[p->nO
1701f 70 2d 31 5d 2e 70 35 20 3d 20 76 61 6c 3b 0a 20  p-1].p5 = val;. 
17020 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e   }.}../*.** Chan
17021 67 65 20 74 68 65 20 50 32 20 6f 70 65 72 61 6e  ge the P2 operan
17022 64 20 6f 66 20 69 6e 73 74 72 75 63 74 69 6f 6e  d of instruction
17023 20 61 64 64 72 20 73 6f 20 74 68 61 74 20 69 74   addr so that it
17024 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 74 68   points to.** th
17025 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68 65  e address of the
17026 20 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f   next instructio
17027 6e 20 74 6f 20 62 65 20 63 6f 64 65 64 2e 0a 2a  n to be coded..*
17028 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
17029 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62   void sqlite3Vdb
1702a 65 4a 75 6d 70 48 65 72 65 28 56 64 62 65 20 2a  eJumpHere(Vdbe *
1702b 70 2c 20 69 6e 74 20 61 64 64 72 29 7b 0a 20 20  p, int addr){.  
1702c 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1702d 65 50 32 28 70 2c 20 61 64 64 72 2c 20 70 2d 3e  eP2(p, addr, p->
1702e 6e 4f 70 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  nOp);.}.../*.** 
1702f 49 66 20 74 68 65 20 69 6e 70 75 74 20 46 75 6e  If the input Fun
17030 63 44 65 66 20 73 74 72 75 63 74 75 72 65 20 69  cDef structure i
17031 73 20 65 70 68 65 6d 65 72 61 6c 2c 20 74 68 65  s ephemeral, the
17032 6e 20 66 72 65 65 20 69 74 2e 20 20 49 66 0a 2a  n free it.  If.*
17033 2a 20 74 68 65 20 46 75 6e 63 44 65 66 20 69 73  * the FuncDef is
17034 20 6e 6f 74 20 65 70 68 65 72 6d 61 6c 2c 20 74   not ephermal, t
17035 68 65 6e 20 64 6f 20 6e 6f 74 68 69 6e 67 2e 0a  hen do nothing..
17036 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
17037 72 65 65 45 70 68 65 6d 65 72 61 6c 46 75 6e 63  reeEphemeralFunc
17038 74 69 6f 6e 28 73 71 6c 69 74 65 33 20 2a 64 62  tion(sqlite3 *db
17039 2c 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66 29  , FuncDef *pDef)
1703a 7b 0a 20 20 69 66 28 20 70 44 65 66 20 26 26 20  {.  if( pDef && 
1703b 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26 20 53  (pDef->flags & S
1703c 51 4c 49 54 45 5f 46 55 4e 43 5f 45 50 48 45 4d  QLITE_FUNC_EPHEM
1703d 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  )!=0 ){.    sqli
1703e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 44  te3DbFree(db, pD
1703f 65 66 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ef);.  }.}../*.*
17040 2a 20 44 65 6c 65 74 65 20 61 20 50 34 20 76 61  * Delete a P4 va
17041 6c 75 65 20 69 66 20 6e 65 63 65 73 73 61 72 79  lue if necessary
17042 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
17043 20 66 72 65 65 50 34 28 73 71 6c 69 74 65 33 20   freeP4(sqlite3 
17044 2a 64 62 2c 20 69 6e 74 20 70 34 74 79 70 65 2c  *db, int p4type,
17045 20 76 6f 69 64 20 2a 70 34 29 7b 0a 20 20 69 66   void *p4){.  if
17046 28 20 70 34 20 29 7b 0a 20 20 20 20 73 77 69 74  ( p4 ){.    swit
17047 63 68 28 20 70 34 74 79 70 65 20 29 7b 0a 20 20  ch( p4type ){.  
17048 20 20 20 20 63 61 73 65 20 50 34 5f 52 45 41 4c      case P4_REAL
17049 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50 34 5f  :.      case P4_
1704a 49 4e 54 36 34 3a 0a 20 20 20 20 20 20 63 61 73  INT64:.      cas
1704b 65 20 50 34 5f 4d 50 52 49 4e 54 46 3a 0a 20 20  e P4_MPRINTF:.  
1704c 20 20 20 20 63 61 73 65 20 50 34 5f 44 59 4e 41      case P4_DYNA
1704d 4d 49 43 3a 0a 20 20 20 20 20 20 63 61 73 65 20  MIC:.      case 
1704e 50 34 5f 4b 45 59 49 4e 46 4f 3a 0a 20 20 20 20  P4_KEYINFO:.    
1704f 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52 52    case P4_INTARR
17050 41 59 3a 0a 20 20 20 20 20 20 63 61 73 65 20 50  AY:.      case P
17051 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46  4_KEYINFO_HANDOF
17052 46 3a 20 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  F: {.        sql
17053 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
17054 34 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  4);.        brea
17055 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
17056 20 63 61 73 65 20 50 34 5f 56 44 42 45 46 55 4e   case P4_VDBEFUN
17057 43 3a 20 7b 0a 20 20 20 20 20 20 20 20 56 64 62  C: {.        Vdb
17058 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75 6e 63  eFunc *pVdbeFunc
17059 20 3d 20 28 56 64 62 65 46 75 6e 63 20 2a 29 70   = (VdbeFunc *)p
1705a 34 3b 0a 20 20 20 20 20 20 20 20 66 72 65 65 45  4;.        freeE
1705b 70 68 65 6d 65 72 61 6c 46 75 6e 63 74 69 6f 6e  phemeralFunction
1705c 28 64 62 2c 20 70 56 64 62 65 46 75 6e 63 2d 3e  (db, pVdbeFunc->
1705d 70 46 75 6e 63 29 3b 0a 20 20 20 20 20 20 20 20  pFunc);.        
1705e 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
1705f 65 41 75 78 44 61 74 61 28 70 56 64 62 65 46 75  eAuxData(pVdbeFu
17060 6e 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  nc, 0);.        
17061 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
17062 2c 20 70 56 64 62 65 46 75 6e 63 29 3b 0a 20 20  , pVdbeFunc);.  
17063 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
17064 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20     }.      case 
17065 50 34 5f 46 55 4e 43 44 45 46 3a 20 7b 0a 20 20  P4_FUNCDEF: {.  
17066 20 20 20 20 20 20 66 72 65 65 45 70 68 65 6d 65        freeEpheme
17067 72 61 6c 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20  ralFunction(db, 
17068 28 46 75 6e 63 44 65 66 2a 29 70 34 29 3b 0a 20  (FuncDef*)p4);. 
17069 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1706a 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
1706b 20 50 34 5f 4d 45 4d 3a 20 7b 0a 20 20 20 20 20   P4_MEM: {.     
1706c 20 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46     sqlite3ValueF
1706d 72 65 65 28 28 73 71 6c 69 74 65 33 5f 76 61 6c  ree((sqlite3_val
1706e 75 65 2a 29 70 34 29 3b 0a 20 20 20 20 20 20 20  ue*)p4);.       
1706f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
17070 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a      }.  }.}.../*
17071 0a 2a 2a 20 43 68 61 6e 67 65 20 4e 20 6f 70 63  .** Change N opc
17072 6f 64 65 73 20 73 74 61 72 74 69 6e 67 20 61 74  odes starting at
17073 20 61 64 64 72 20 74 6f 20 4e 6f 2d 6f 70 73 2e   addr to No-ops.
17074 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
17075 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56  TE void sqlite3V
17076 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
17077 56 64 62 65 20 2a 70 2c 20 69 6e 74 20 61 64 64  Vdbe *p, int add
17078 72 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 69 66 28  r, int N){.  if(
17079 20 70 20 26 26 20 70 2d 3e 61 4f 70 20 29 7b 0a   p && p->aOp ){.
1707a 20 20 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 20      VdbeOp *pOp 
1707b 3d 20 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d 3b  = &p->aOp[addr];
1707c 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62  .    sqlite3 *db
1707d 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 20 20 77 68   = p->db;.    wh
1707e 69 6c 65 28 20 4e 2d 2d 20 29 7b 0a 20 20 20 20  ile( N-- ){.    
1707f 20 20 66 72 65 65 50 34 28 64 62 2c 20 70 4f 70    freeP4(db, pOp
17080 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d 3e 70  ->p4type, pOp->p
17081 34 2e 70 29 3b 0a 20 20 20 20 20 20 6d 65 6d 73  4.p);.      mems
17082 65 74 28 70 4f 70 2c 20 30 2c 20 73 69 7a 65 6f  et(pOp, 0, sizeo
17083 66 28 70 4f 70 5b 30 5d 29 29 3b 0a 20 20 20 20  f(pOp[0]));.    
17084 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d 20    pOp->opcode = 
17085 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 20 20 70  OP_Noop;.      p
17086 4f 70 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  Op++;.    }.  }.
17087 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 6e 67 65 20  }../*.** Change 
17088 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
17089 20 50 34 20 6f 70 65 72 61 6e 64 20 66 6f 72 20   P4 operand for 
1708a 61 20 73 70 65 63 69 66 69 63 20 69 6e 73 74 72  a specific instr
1708b 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20  uction..** This 
1708c 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 66 75  routine is usefu
1708d 6c 20 77 68 65 6e 20 61 20 6c 61 72 67 65 20 70  l when a large p
1708e 72 6f 67 72 61 6d 20 69 73 20 6c 6f 61 64 65 64  rogram is loaded
1708f 20 66 72 6f 6d 20 61 0a 2a 2a 20 73 74 61 74 69   from a.** stati
17090 63 20 61 72 72 61 79 20 75 73 69 6e 67 20 73 71  c array using sq
17091 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
17092 73 74 20 62 75 74 20 77 65 20 77 61 6e 74 20 74  st but we want t
17093 6f 20 6d 61 6b 65 20 61 0a 2a 2a 20 66 65 77 20  o make a.** few 
17094 6d 69 6e 6f 72 20 63 68 61 6e 67 65 73 20 74 6f  minor changes to
17095 20 74 68 65 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a   the program..**
17096 0a 2a 2a 20 49 66 20 6e 3e 3d 30 20 74 68 65 6e  .** If n>=0 then
17097 20 74 68 65 20 50 34 20 6f 70 65 72 61 6e 64 20   the P4 operand 
17098 69 73 20 64 79 6e 61 6d 69 63 2c 20 6d 65 61 6e  is dynamic, mean
17099 69 6e 67 20 74 68 61 74 20 61 20 63 6f 70 79 20  ing that a copy 
1709a 6f 66 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67  of.** the string
1709b 20 69 73 20 6d 61 64 65 20 69 6e 74 6f 20 6d 65   is made into me
1709c 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
1709d 6f 6d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  om sqlite3_mallo
1709e 63 28 29 2e 0a 2a 2a 20 41 20 76 61 6c 75 65 20  c()..** A value 
1709f 6f 66 20 6e 3d 3d 30 20 6d 65 61 6e 73 20 63 6f  of n==0 means co
170a0 70 79 20 62 79 74 65 73 20 6f 66 20 7a 50 34 20  py bytes of zP4 
170a1 75 70 20 74 6f 20 61 6e 64 20 69 6e 63 6c 75 64  up to and includ
170a2 69 6e 67 20 74 68 65 0a 2a 2a 20 66 69 72 73 74  ing the.** first
170a3 20 6e 75 6c 6c 20 62 79 74 65 2e 20 20 49 66 20   null byte.  If 
170a4 6e 3e 30 20 74 68 65 6e 20 63 6f 70 79 20 6e 2b  n>0 then copy n+
170a5 31 20 62 79 74 65 73 20 6f 66 20 7a 50 34 2e 0a  1 bytes of zP4..
170a6 2a 2a 0a 2a 2a 20 49 66 20 6e 3d 3d 50 34 5f 4b  **.** If n==P4_K
170a7 45 59 49 4e 46 4f 20 69 74 20 6d 65 61 6e 73 20  EYINFO it means 
170a8 74 68 61 74 20 7a 50 34 20 69 73 20 61 20 70 6f  that zP4 is a po
170a9 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e  inter to a KeyIn
170aa 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a  fo structure..**
170ab 20 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20   A copy is made 
170ac 6f 66 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73  of the KeyInfo s
170ad 74 72 75 63 74 75 72 65 20 69 6e 74 6f 20 6d 65  tructure into me
170ae 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
170af 6f 6d 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6d 61  om.** sqlite3_ma
170b0 6c 6c 6f 63 2c 20 74 6f 20 62 65 20 66 72 65 65  lloc, to be free
170b1 64 20 77 68 65 6e 20 74 68 65 20 56 64 62 65 20  d when the Vdbe 
170b2 69 73 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a  is finalized..**
170b3 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48   n==P4_KEYINFO_H
170b4 41 4e 44 4f 46 46 20 69 6e 64 69 63 61 74 65 73  ANDOFF indicates
170b5 20 74 68 61 74 20 7a 50 34 20 70 6f 69 6e 74 73   that zP4 points
170b6 20 74 6f 20 61 20 4b 65 79 49 6e 66 6f 20 73 74   to a KeyInfo st
170b7 72 75 63 74 75 72 65 0a 2a 2a 20 73 74 6f 72 65  ructure.** store
170b8 64 20 69 6e 20 6d 65 6d 6f 72 79 20 74 68 61 74  d in memory that
170b9 20 74 68 65 20 63 61 6c 6c 65 72 20 68 61 73 20   the caller has 
170ba 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73 71  obtained from sq
170bb 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 2e 20 54 68  lite3_malloc. Th
170bc 65 20 0a 2a 2a 20 63 61 6c 6c 65 72 20 73 68 6f  e .** caller sho
170bd 75 6c 64 20 6e 6f 74 20 66 72 65 65 20 74 68 65  uld not free the
170be 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 74 20   allocation, it 
170bf 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20 77 68  will be freed wh
170c0 65 6e 20 74 68 65 20 56 64 62 65 20 69 73 0a 2a  en the Vdbe is.*
170c1 2a 20 66 69 6e 61 6c 69 7a 65 64 2e 0a 2a 2a 20  * finalized..** 
170c2 0a 2a 2a 20 4f 74 68 65 72 20 76 61 6c 75 65 73  .** Other values
170c3 20 6f 66 20 6e 20 28 50 34 5f 53 54 41 54 49 43   of n (P4_STATIC
170c4 2c 20 50 34 5f 43 4f 4c 4c 53 45 51 20 65 74 63  , P4_COLLSEQ etc
170c5 2e 29 20 69 6e 64 69 63 61 74 65 20 74 68 61 74  .) indicate that
170c6 20 7a 50 34 20 70 6f 69 6e 74 73 0a 2a 2a 20 74   zP4 points.** t
170c7 6f 20 61 20 73 74 72 69 6e 67 20 6f 72 20 73 74  o a string or st
170c8 72 75 63 74 75 72 65 20 74 68 61 74 20 69 73 20  ructure that is 
170c9 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 65 78  guaranteed to ex
170ca 69 73 74 20 66 6f 72 20 74 68 65 20 6c 69 66 65  ist for the life
170cb 74 69 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 56  time of.** the V
170cc 64 62 65 2e 20 49 6e 20 74 68 65 73 65 20 63 61  dbe. In these ca
170cd 73 65 73 20 77 65 20 63 61 6e 20 6a 75 73 74 20  ses we can just 
170ce 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72  copy the pointer
170cf 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 64 64 72 3c  ..**.** If addr<
170d0 30 20 74 68 65 6e 20 63 68 61 6e 67 65 20 50 34  0 then change P4
170d1 20 6f 6e 20 74 68 65 20 6d 6f 73 74 20 72 65 63   on the most rec
170d2 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20 69  ently inserted i
170d3 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  nstruction..*/.S
170d4 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
170d5 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  id sqlite3VdbeCh
170d6 61 6e 67 65 50 34 28 56 64 62 65 20 2a 70 2c 20  angeP4(Vdbe *p, 
170d7 69 6e 74 20 61 64 64 72 2c 20 63 6f 6e 73 74 20  int addr, const 
170d8 63 68 61 72 20 2a 7a 50 34 2c 20 69 6e 74 20 6e  char *zP4, int n
170d9 29 7b 0a 20 20 4f 70 20 2a 70 4f 70 3b 0a 20 20  ){.  Op *pOp;.  
170da 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 61  sqlite3 *db;.  a
170db 73 73 65 72 74 28 20 70 21 3d 30 20 29 3b 0a 20  ssert( p!=0 );. 
170dc 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 61   db = p->db;.  a
170dd 73 73 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d  ssert( p->magic=
170de 3d 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54  =VDBE_MAGIC_INIT
170df 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70   );.  if( p->aOp
170e0 3d 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  ==0 || db->mallo
170e1 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 69  cFailed ){.    i
170e2 66 20 28 6e 20 21 3d 20 50 34 5f 4b 45 59 49 4e  f (n != P4_KEYIN
170e3 46 4f 29 20 7b 0a 20 20 20 20 20 20 66 72 65 65  FO) {.      free
170e4 50 34 28 64 62 2c 20 6e 2c 20 28 76 6f 69 64 2a  P4(db, n, (void*
170e5 29 2a 28 63 68 61 72 2a 2a 29 26 7a 50 34 29 3b  )*(char**)&zP4);
170e6 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
170e7 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
170e8 20 61 64 64 72 3c 70 2d 3e 6e 4f 70 20 29 3b 0a   addr<p->nOp );.
170e9 20 20 69 66 28 20 61 64 64 72 3c 30 20 29 7b 0a    if( addr<0 ){.
170ea 20 20 20 20 61 64 64 72 20 3d 20 70 2d 3e 6e 4f      addr = p->nO
170eb 70 20 2d 20 31 3b 0a 20 20 20 20 69 66 28 20 61  p - 1;.    if( a
170ec 64 64 72 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a  ddr<0 ) return;.
170ed 20 20 7d 0a 20 20 70 4f 70 20 3d 20 26 70 2d 3e    }.  pOp = &p->
170ee 61 4f 70 5b 61 64 64 72 5d 3b 0a 20 20 66 72 65  aOp[addr];.  fre
170ef 65 50 34 28 64 62 2c 20 70 4f 70 2d 3e 70 34 74  eP4(db, pOp->p4t
170f0 79 70 65 2c 20 70 4f 70 2d 3e 70 34 2e 70 29 3b  ype, pOp->p4.p);
170f1 0a 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20 30  .  pOp->p4.p = 0
170f2 3b 0a 20 20 69 66 28 20 6e 3d 3d 50 34 5f 49 4e  ;.  if( n==P4_IN
170f3 54 33 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 6f  T32 ){.    /* No
170f4 74 65 3a 20 74 68 69 73 20 63 61 73 74 20 69 73  te: this cast is
170f5 20 73 61 66 65 2c 20 62 65 63 61 75 73 65 20 74   safe, because t
170f6 68 65 20 6f 72 69 67 69 6e 20 64 61 74 61 20 70  he origin data p
170f7 6f 69 6e 74 20 77 61 73 20 61 6e 20 69 6e 74 0a  oint was an int.
170f8 20 20 20 20 2a 2a 20 74 68 61 74 20 77 61 73 20      ** that was 
170f9 63 61 73 74 20 74 6f 20 61 20 28 63 6f 6e 73 74  cast to a (const
170fa 20 63 68 61 72 20 2a 29 2e 20 2a 2f 0a 20 20 20   char *). */.   
170fb 20 70 4f 70 2d 3e 70 34 2e 69 20 3d 20 53 51 4c   pOp->p4.i = SQL
170fc 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54 28 7a  ITE_PTR_TO_INT(z
170fd 50 34 29 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34  P4);.    pOp->p4
170fe 74 79 70 65 20 3d 20 6e 3b 0a 20 20 7d 65 6c 73  type = n;.  }els
170ff 65 20 69 66 28 20 7a 50 34 3d 3d 30 20 29 7b 0a  e if( zP4==0 ){.
17100 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 20 3d 20      pOp->p4.p = 
17101 30 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  0;.    pOp->p4ty
17102 70 65 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b  pe = P4_NOTUSED;
17103 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 3d 3d  .  }else if( n==
17104 50 34 5f 4b 45 59 49 4e 46 4f 20 29 7b 0a 20 20  P4_KEYINFO ){.  
17105 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49    KeyInfo *pKeyI
17106 6e 66 6f 3b 0a 20 20 20 20 69 6e 74 20 6e 46 69  nfo;.    int nFi
17107 65 6c 64 2c 20 6e 42 79 74 65 3b 0a 0a 20 20 20  eld, nByte;..   
17108 20 6e 46 69 65 6c 64 20 3d 20 28 28 4b 65 79 49   nField = ((KeyI
17109 6e 66 6f 2a 29 7a 50 34 29 2d 3e 6e 46 69 65 6c  nfo*)zP4)->nFiel
1710a 64 3b 0a 20 20 20 20 6e 42 79 74 65 20 3d 20 73  d;.    nByte = s
1710b 69 7a 65 6f 66 28 2a 70 4b 65 79 49 6e 66 6f 29  izeof(*pKeyInfo)
1710c 20 2b 20 28 6e 46 69 65 6c 64 2d 31 29 2a 73 69   + (nField-1)*si
1710d 7a 65 6f 66 28 70 4b 65 79 49 6e 66 6f 2d 3e 61  zeof(pKeyInfo->a
1710e 43 6f 6c 6c 5b 30 5d 29 20 2b 20 6e 46 69 65 6c  Coll[0]) + nFiel
1710f 64 3b 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20  d;.    pKeyInfo 
17110 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28  = sqlite3Malloc(
17111 20 6e 42 79 74 65 20 29 3b 0a 20 20 20 20 70 4f   nByte );.    pO
17112 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 20 3d  p->p4.pKeyInfo =
17113 20 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 69   pKeyInfo;.    i
17114 66 28 20 70 4b 65 79 49 6e 66 6f 20 29 7b 0a 20  f( pKeyInfo ){. 
17115 20 20 20 20 20 75 38 20 2a 61 53 6f 72 74 4f 72       u8 *aSortOr
17116 64 65 72 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70  der;.      memcp
17117 79 28 70 4b 65 79 49 6e 66 6f 2c 20 7a 50 34 2c  y(pKeyInfo, zP4,
17118 20 6e 42 79 74 65 29 3b 0a 20 20 20 20 20 20 61   nByte);.      a
17119 53 6f 72 74 4f 72 64 65 72 20 3d 20 70 4b 65 79  SortOrder = pKey
1711a 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1711b 3b 0a 20 20 20 20 20 20 69 66 28 20 61 53 6f 72  ;.      if( aSor
1711c 74 4f 72 64 65 72 20 29 7b 0a 20 20 20 20 20 20  tOrder ){.      
1711d 20 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72    pKeyInfo->aSor
1711e 74 4f 72 64 65 72 20 3d 20 28 75 6e 73 69 67 6e  tOrder = (unsign
1711f 65 64 20 63 68 61 72 2a 29 26 70 4b 65 79 49 6e  ed char*)&pKeyIn
17120 66 6f 2d 3e 61 43 6f 6c 6c 5b 6e 46 69 65 6c 64  fo->aColl[nField
17121 5d 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  ];.        memcp
17122 79 28 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72  y(pKeyInfo->aSor
17123 74 4f 72 64 65 72 2c 20 61 53 6f 72 74 4f 72 64  tOrder, aSortOrd
17124 65 72 2c 20 6e 46 69 65 6c 64 29 3b 0a 20 20 20  er, nField);.   
17125 20 20 20 7d 0a 20 20 20 20 20 20 70 4f 70 2d 3e     }.      pOp->
17126 70 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49  p4type = P4_KEYI
17127 4e 46 4f 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  NFO;.    }else{.
17128 20 20 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c        p->db->mal
17129 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
1712a 20 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70 65       pOp->p4type
1712b 20 3d 20 50 34 5f 4e 4f 54 55 53 45 44 3b 0a 20   = P4_NOTUSED;. 
1712c 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
1712d 20 6e 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 5f 48   n==P4_KEYINFO_H
1712e 41 4e 44 4f 46 46 20 29 7b 0a 20 20 20 20 70 4f  ANDOFF ){.    pO
1712f 70 2d 3e 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a  p->p4.p = (void*
17130 29 7a 50 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70  )zP4;.    pOp->p
17131 34 74 79 70 65 20 3d 20 50 34 5f 4b 45 59 49 4e  4type = P4_KEYIN
17132 46 4f 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  FO;.  }else if( 
17133 6e 3c 30 20 29 7b 0a 20 20 20 20 70 4f 70 2d 3e  n<0 ){.    pOp->
17134 70 34 2e 70 20 3d 20 28 76 6f 69 64 2a 29 7a 50  p4.p = (void*)zP
17135 34 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79  4;.    pOp->p4ty
17136 70 65 20 3d 20 6e 3b 0a 20 20 7d 65 6c 73 65 7b  pe = n;.  }else{
17137 0a 20 20 20 20 69 66 28 20 6e 3d 3d 30 20 29 20  .    if( n==0 ) 
17138 6e 20 3d 20 73 74 72 6c 65 6e 28 7a 50 34 29 3b  n = strlen(zP4);
17139 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 7a 20 3d  .    pOp->p4.z =
1713a 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75   sqlite3DbStrNDu
1713b 70 28 70 2d 3e 64 62 2c 20 7a 50 34 2c 20 6e 29  p(p->db, zP4, n)
1713c 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 74 79 70  ;.    pOp->p4typ
1713d 65 20 3d 20 50 34 5f 44 59 4e 41 4d 49 43 3b 0a  e = P4_DYNAMIC;.
1713e 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e    }.}..#ifndef N
1713f 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 43 68 61 6e  DEBUG./*.** Chan
17140 67 65 20 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f  ge the comment o
17141 6e 20 74 68 65 20 74 68 65 20 6d 6f 73 74 20 72  n the the most r
17142 65 63 65 6e 74 6c 79 20 63 6f 64 65 64 20 69 6e  ecently coded in
17143 73 74 72 75 63 74 69 6f 6e 2e 20 20 4f 72 0a 2a  struction.  Or.*
17144 2a 20 69 6e 73 65 72 74 20 61 20 4e 6f 2d 6f 70  * insert a No-op
17145 20 61 6e 64 20 61 64 64 20 74 68 65 20 63 6f 6d   and add the com
17146 6d 65 6e 74 20 74 6f 20 74 68 61 74 20 6e 65 77  ment to that new
17147 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 54   instruction.  T
17148 68 69 73 0a 2a 2a 20 6d 61 6b 65 73 20 74 68 65  his.** makes the
17149 20 63 6f 64 65 20 65 61 73 69 65 72 20 74 6f 20   code easier to 
1714a 72 65 61 64 20 64 75 72 69 6e 67 20 64 65 62 75  read during debu
1714b 67 67 69 6e 67 2e 20 20 4e 6f 6e 65 20 6f 66 20  gging.  None of 
1714c 74 68 69 73 20 68 61 70 70 65 6e 73 0a 2a 2a 20  this happens.** 
1714d 69 6e 20 61 20 70 72 6f 64 75 63 74 69 6f 6e 20  in a production 
1714e 62 75 69 6c 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  build..*/.SQLITE
1714f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
17150 6c 69 74 65 33 56 64 62 65 43 6f 6d 6d 65 6e 74  lite3VdbeComment
17151 28 56 64 62 65 20 2a 70 2c 20 63 6f 6e 73 74 20  (Vdbe *p, const 
17152 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e  char *zFormat, .
17153 2e 2e 29 7b 0a 20 20 76 61 5f 6c 69 73 74 20 61  ..){.  va_list a
17154 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  p;.  assert( p->
17155 6e 4f 70 3e 30 20 7c 7c 20 70 2d 3e 61 4f 70 3d  nOp>0 || p->aOp=
17156 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
17157 70 2d 3e 61 4f 70 3d 3d 30 20 7c 7c 20 70 2d 3e  p->aOp==0 || p->
17158 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e 7a 43  aOp[p->nOp-1].zC
17159 6f 6d 6d 65 6e 74 3d 3d 30 20 7c 7c 20 70 2d 3e  omment==0 || p->
1715a 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1715b 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6e 4f 70   );.  if( p->nOp
1715c 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a 70   ){.    char **p
1715d 7a 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 2d 3e 6e  z = &p->aOp[p->n
1715e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74 3b 0a  Op-1].zComment;.
1715f 20 20 20 20 76 61 5f 73 74 61 72 74 28 61 70 2c      va_start(ap,
17160 20 7a 46 6f 72 6d 61 74 29 3b 0a 20 20 20 20 73   zFormat);.    s
17161 71 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e  qlite3DbFree(p->
17162 64 62 2c 20 2a 70 7a 29 3b 0a 20 20 20 20 2a 70  db, *pz);.    *p
17163 7a 20 3d 20 73 71 6c 69 74 65 33 56 4d 50 72 69  z = sqlite3VMPri
17164 6e 74 66 28 70 2d 3e 64 62 2c 20 7a 46 6f 72 6d  ntf(p->db, zForm
17165 61 74 2c 20 61 70 29 3b 0a 20 20 20 20 76 61 5f  at, ap);.    va_
17166 65 6e 64 28 61 70 29 3b 0a 20 20 7d 0a 7d 0a 53  end(ap);.  }.}.S
17167 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17168 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4e 6f  id sqlite3VdbeNo
17169 6f 70 43 6f 6d 6d 65 6e 74 28 56 64 62 65 20 2a  opComment(Vdbe *
1716a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  p, const char *z
1716b 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b 0a 20 20  Format, ...){.  
1716c 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 73 71  va_list ap;.  sq
1716d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30 28  lite3VdbeAddOp0(
1716e 70 2c 20 4f 50 5f 4e 6f 6f 70 29 3b 0a 20 20 61  p, OP_Noop);.  a
1716f 73 73 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20  ssert( p->nOp>0 
17170 7c 7c 20 70 2d 3e 61 4f 70 3d 3d 30 20 29 3b 0a  || p->aOp==0 );.
17171 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 4f 70    assert( p->aOp
17172 3d 3d 30 20 7c 7c 20 70 2d 3e 61 4f 70 5b 70 2d  ==0 || p->aOp[p-
17173 3e 6e 4f 70 2d 31 5d 2e 7a 43 6f 6d 6d 65 6e 74  >nOp-1].zComment
17174 3d 3d 30 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d 61  ==0 || p->db->ma
17175 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
17176 69 66 28 20 70 2d 3e 6e 4f 70 20 29 7b 0a 20 20  if( p->nOp ){.  
17177 20 20 63 68 61 72 20 2a 2a 70 7a 20 3d 20 26 70    char **pz = &p
17178 2d 3e 61 4f 70 5b 70 2d 3e 6e 4f 70 2d 31 5d 2e  ->aOp[p->nOp-1].
17179 7a 43 6f 6d 6d 65 6e 74 3b 0a 20 20 20 20 76 61  zComment;.    va
1717a 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
1717b 61 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  at);.    sqlite3
1717c 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20 2a 70  DbFree(p->db, *p
1717d 7a 29 3b 0a 20 20 20 20 2a 70 7a 20 3d 20 73 71  z);.    *pz = sq
1717e 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 70 2d  lite3VMPrintf(p-
1717f 3e 64 62 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70  >db, zFormat, ap
17180 29 3b 0a 20 20 20 20 76 61 5f 65 6e 64 28 61 70  );.    va_end(ap
17181 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20  );.  }.}.#endif 
17182 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 2f   /* NDEBUG */../
17183 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
17184 6f 70 63 6f 64 65 20 66 6f 72 20 61 20 67 69 76  opcode for a giv
17185 65 6e 20 61 64 64 72 65 73 73 2e 0a 2a 2f 0a 53  en address..*/.S
17186 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 56 64  QLITE_PRIVATE Vd
17187 62 65 4f 70 20 2a 73 71 6c 69 74 65 33 56 64 62  beOp *sqlite3Vdb
17188 65 47 65 74 4f 70 28 56 64 62 65 20 2a 70 2c 20  eGetOp(Vdbe *p, 
17189 69 6e 74 20 61 64 64 72 29 7b 0a 20 20 61 73 73  int addr){.  ass
1718a 65 72 74 28 20 70 2d 3e 6d 61 67 69 63 3d 3d 56  ert( p->magic==V
1718b 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 29  DBE_MAGIC_INIT )
1718c 3b 0a 20 20 61 73 73 65 72 74 28 20 28 61 64 64  ;.  assert( (add
1718d 72 3e 3d 30 20 26 26 20 61 64 64 72 3c 70 2d 3e  r>=0 && addr<p->
1718e 6e 4f 70 29 20 7c 7c 20 70 2d 3e 64 62 2d 3e 6d  nOp) || p->db->m
1718f 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20  allocFailed );. 
17190 20 72 65 74 75 72 6e 20 28 28 61 64 64 72 3e 3d   return ((addr>=
17191 30 20 26 26 20 61 64 64 72 3c 70 2d 3e 6e 4f 70  0 && addr<p->nOp
17192 29 3f 28 26 70 2d 3e 61 4f 70 5b 61 64 64 72 5d  )?(&p->aOp[addr]
17193 29 3a 30 29 3b 0a 7d 0a 0a 23 69 66 20 21 64 65  ):0);.}..#if !de
17194 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
17195 54 5f 45 58 50 4c 41 49 4e 29 20 7c 7c 20 21 64  T_EXPLAIN) || !d
17196 65 66 69 6e 65 64 28 4e 44 45 42 55 47 29 20 5c  efined(NDEBUG) \
17197 0a 20 20 20 20 20 7c 7c 20 64 65 66 69 6e 65 64  .     || defined
17198 28 56 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c  (VDBE_PROFILE) |
17199 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
1719a 5f 44 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 43 6f  _DEBUG)./*.** Co
1719b 6d 70 75 74 65 20 61 20 73 74 72 69 6e 67 20 74  mpute a string t
1719c 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74 68  hat describes th
1719d 65 20 50 34 20 70 61 72 61 6d 65 74 65 72 20 66  e P4 parameter f
1719e 6f 72 20 61 6e 20 6f 70 63 6f 64 65 2e 0a 2a 2a  or an opcode..**
1719f 20 55 73 65 20 7a 54 65 6d 70 20 66 6f 72 20 61   Use zTemp for a
171a0 6e 79 20 72 65 71 75 69 72 65 64 20 74 65 6d 70  ny required temp
171a1 6f 72 61 72 79 20 62 75 66 66 65 72 20 73 70 61  orary buffer spa
171a2 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68  ce..*/.static ch
171a3 61 72 20 2a 64 69 73 70 6c 61 79 50 34 28 4f 70  ar *displayP4(Op
171a4 20 2a 70 4f 70 2c 20 63 68 61 72 20 2a 7a 54 65   *pOp, char *zTe
171a5 6d 70 2c 20 69 6e 74 20 6e 54 65 6d 70 29 7b 0a  mp, int nTemp){.
171a6 20 20 63 68 61 72 20 2a 7a 50 34 20 3d 20 7a 54    char *zP4 = zT
171a7 65 6d 70 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  emp;.  assert( n
171a8 54 65 6d 70 3e 3d 32 30 20 29 3b 0a 20 20 73 77  Temp>=20 );.  sw
171a9 69 74 63 68 28 20 70 4f 70 2d 3e 70 34 74 79 70  itch( pOp->p4typ
171aa 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 50 34  e ){.    case P4
171ab 5f 4b 45 59 49 4e 46 4f 5f 53 54 41 54 49 43 3a  _KEYINFO_STATIC:
171ac 0a 20 20 20 20 63 61 73 65 20 50 34 5f 4b 45 59  .    case P4_KEY
171ad 49 4e 46 4f 3a 20 7b 0a 20 20 20 20 20 20 69 6e  INFO: {.      in
171ae 74 20 69 2c 20 6a 3b 0a 20 20 20 20 20 20 4b 65  t i, j;.      Ke
171af 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20  yInfo *pKeyInfo 
171b0 3d 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e  = pOp->p4.pKeyIn
171b1 66 6f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  fo;.      sqlite
171b2 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70  3_snprintf(nTemp
171b3 2c 20 7a 54 65 6d 70 2c 20 22 6b 65 79 69 6e 66  , zTemp, "keyinf
171b4 6f 28 25 64 22 2c 20 70 4b 65 79 49 6e 66 6f 2d  o(%d", pKeyInfo-
171b5 3e 6e 46 69 65 6c 64 29 3b 0a 20 20 20 20 20 20  >nField);.      
171b6 69 20 3d 20 73 74 72 6c 65 6e 28 7a 54 65 6d 70  i = strlen(zTemp
171b7 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30  );.      for(j=0
171b8 3b 20 6a 3c 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46  ; j<pKeyInfo->nF
171b9 69 65 6c 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20  ield; j++){.    
171ba 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f      CollSeq *pCo
171bb 6c 6c 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 61  ll = pKeyInfo->a
171bc 43 6f 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20  Coll[j];.       
171bd 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
171be 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20          int n = 
171bf 73 74 72 6c 65 6e 28 70 43 6f 6c 6c 2d 3e 7a 4e  strlen(pColl->zN
171c0 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ame);.          
171c1 69 66 28 20 69 2b 6e 3e 6e 54 65 6d 70 2d 36 20  if( i+n>nTemp-6 
171c2 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6d  ){.            m
171c3 65 6d 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c  emcpy(&zTemp[i],
171c4 22 2c 2e 2e 2e 22 2c 34 29 3b 0a 20 20 20 20 20  ",...",4);.     
171c5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
171c6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
171c7 20 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d      zTemp[i++] =
171c8 20 27 2c 27 3b 0a 20 20 20 20 20 20 20 20 20 20   ',';.          
171c9 69 66 28 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53  if( pKeyInfo->aS
171ca 6f 72 74 4f 72 64 65 72 20 26 26 20 70 4b 65 79  ortOrder && pKey
171cb 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
171cc 5b 6a 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 20  [j] ){.         
171cd 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20     zTemp[i++] = 
171ce 27 2d 27 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  '-';.          }
171cf 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d 63 70  .          memcp
171d0 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 20 70 43 6f  y(&zTemp[i], pCo
171d1 6c 6c 2d 3e 7a 4e 61 6d 65 2c 6e 2b 31 29 3b 0a  ll->zName,n+1);.
171d2 20 20 20 20 20 20 20 20 20 20 69 20 2b 3d 20 6e            i += n
171d3 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
171d4 69 66 28 20 69 2b 34 3c 6e 54 65 6d 70 2d 36 20  if( i+4<nTemp-6 
171d5 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6d 65 6d  ){.          mem
171d6 63 70 79 28 26 7a 54 65 6d 70 5b 69 5d 2c 22 2c  cpy(&zTemp[i],",
171d7 6e 69 6c 22 2c 34 29 3b 0a 20 20 20 20 20 20 20  nil",4);.       
171d8 20 20 20 69 20 2b 3d 20 34 3b 0a 20 20 20 20 20     i += 4;.     
171d9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
171da 20 20 20 7a 54 65 6d 70 5b 69 2b 2b 5d 20 3d 20     zTemp[i++] = 
171db 27 29 27 3b 0a 20 20 20 20 20 20 7a 54 65 6d 70  ')';.      zTemp
171dc 5b 69 5d 20 3d 20 30 3b 0a 20 20 20 20 20 20 61  [i] = 0;.      a
171dd 73 73 65 72 74 28 20 69 3c 6e 54 65 6d 70 20 29  ssert( i<nTemp )
171de 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
171df 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34     }.    case P4
171e0 5f 43 4f 4c 4c 53 45 51 3a 20 7b 0a 20 20 20 20  _COLLSEQ: {.    
171e1 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c    CollSeq *pColl
171e2 20 3d 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c   = pOp->p4.pColl
171e3 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
171e4 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
171e5 7a 54 65 6d 70 2c 20 22 63 6f 6c 6c 73 65 71 28  zTemp, "collseq(
171e6 25 2e 32 30 73 29 22 2c 20 70 43 6f 6c 6c 2d 3e  %.20s)", pColl->
171e7 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72  zName);.      br
171e8 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
171e9 61 73 65 20 50 34 5f 46 55 4e 43 44 45 46 3a 20  ase P4_FUNCDEF: 
171ea 7b 0a 20 20 20 20 20 20 46 75 6e 63 44 65 66 20  {.      FuncDef 
171eb 2a 70 44 65 66 20 3d 20 70 4f 70 2d 3e 70 34 2e  *pDef = pOp->p4.
171ec 70 46 75 6e 63 3b 0a 20 20 20 20 20 20 73 71 6c  pFunc;.      sql
171ed 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
171ee 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 73 28  emp, zTemp, "%s(
171ef 25 64 29 22 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d  %d)", pDef->zNam
171f0 65 2c 20 70 44 65 66 2d 3e 6e 41 72 67 29 3b 0a  e, pDef->nArg);.
171f1 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
171f2 20 7d 0a 20 20 20 20 63 61 73 65 20 50 34 5f 49   }.    case P4_I
171f3 4e 54 36 34 3a 20 7b 0a 20 20 20 20 20 20 73 71  NT64: {.      sq
171f4 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e  lite3_snprintf(n
171f5 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 6c  Temp, zTemp, "%l
171f6 6c 64 22 2c 20 2a 70 4f 70 2d 3e 70 34 2e 70 49  ld", *pOp->p4.pI
171f7 36 34 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  64);.      break
171f8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
171f9 20 50 34 5f 49 4e 54 33 32 3a 20 7b 0a 20 20 20   P4_INT32: {.   
171fa 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
171fb 6e 74 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70  ntf(nTemp, zTemp
171fc 2c 20 22 25 64 22 2c 20 70 4f 70 2d 3e 70 34 2e  , "%d", pOp->p4.
171fd 69 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  i);.      break;
171fe 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
171ff 50 34 5f 52 45 41 4c 3a 20 7b 0a 20 20 20 20 20  P4_REAL: {.     
17200 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
17201 66 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20  f(nTemp, zTemp, 
17202 22 25 2e 31 36 67 22 2c 20 2a 70 4f 70 2d 3e 70  "%.16g", *pOp->p
17203 34 2e 70 52 65 61 6c 29 3b 0a 20 20 20 20 20 20  4.pReal);.      
17204 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
17205 20 63 61 73 65 20 50 34 5f 4d 45 4d 3a 20 7b 0a   case P4_MEM: {.
17206 20 20 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d 20        Mem *pMem 
17207 3d 20 70 4f 70 2d 3e 70 34 2e 70 4d 65 6d 3b 0a  = pOp->p4.pMem;.
17208 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 70        assert( (p
17209 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  Mem->flags & MEM
1720a 5f 4e 75 6c 6c 29 3d 3d 30 20 29 3b 0a 20 20 20  _Null)==0 );.   
1720b 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61     if( pMem->fla
1720c 67 73 20 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a  gs & MEM_Str ){.
1720d 20 20 20 20 20 20 20 20 7a 50 34 20 3d 20 70 4d          zP4 = pM
1720e 65 6d 2d 3e 7a 3b 0a 20 20 20 20 20 20 7d 65 6c  em->z;.      }el
1720f 73 65 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61  se if( pMem->fla
17210 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29 7b 0a  gs & MEM_Int ){.
17211 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
17212 73 6e 70 72 69 6e 74 66 28 6e 54 65 6d 70 2c 20  snprintf(nTemp, 
17213 7a 54 65 6d 70 2c 20 22 25 6c 6c 64 22 2c 20 70  zTemp, "%lld", p
17214 4d 65 6d 2d 3e 75 2e 69 29 3b 0a 20 20 20 20 20  Mem->u.i);.     
17215 20 7d 65 6c 73 65 20 69 66 28 20 70 4d 65 6d 2d   }else if( pMem-
17216 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  >flags & MEM_Rea
17217 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  l ){.        sql
17218 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
17219 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 25 2e 31  emp, zTemp, "%.1
1721a 36 67 22 2c 20 70 4d 65 6d 2d 3e 72 29 3b 0a 20  6g", pMem->r);. 
1721b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
1721c 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
1721d 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1721e 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 63  RTUALTABLE.    c
1721f 61 73 65 20 50 34 5f 56 54 41 42 3a 20 7b 0a 20  ase P4_VTAB: {. 
17220 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61       sqlite3_vta
17221 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e  b *pVtab = pOp->
17222 70 34 2e 70 56 74 61 62 3b 0a 20 20 20 20 20 20  p4.pVtab;.      
17223 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
17224 28 6e 54 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22  (nTemp, zTemp, "
17225 76 74 61 62 3a 25 70 3a 25 70 22 2c 20 70 56 74  vtab:%p:%p", pVt
17226 61 62 2c 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75  ab, pVtab->pModu
17227 6c 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  le);.      break
17228 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
17229 20 20 20 63 61 73 65 20 50 34 5f 49 4e 54 41 52     case P4_INTAR
1722a 52 41 59 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  RAY: {.      sql
1722b 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 6e 54  ite3_snprintf(nT
1722c 65 6d 70 2c 20 7a 54 65 6d 70 2c 20 22 69 6e 74  emp, zTemp, "int
1722d 61 72 72 61 79 22 29 3b 0a 20 20 20 20 20 20 62  array");.      b
1722e 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1722f 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
17230 20 7a 50 34 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a   zP4 = pOp->p4.z
17231 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 50 34 3d  ;.      if( zP4=
17232 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 50  =0 ){.        zP
17233 34 20 3d 20 7a 54 65 6d 70 3b 0a 20 20 20 20 20  4 = zTemp;.     
17234 20 20 20 7a 54 65 6d 70 5b 30 5d 20 3d 20 30 3b     zTemp[0] = 0;
17235 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
17236 20 7d 0a 20 20 61 73 73 65 72 74 28 20 7a 50 34   }.  assert( zP4
17237 21 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  !=0 );.  return 
17238 7a 50 34 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f  zP4;.}.#endif../
17239 2a 0a 2a 2a 20 44 65 63 6c 61 72 65 20 74 6f 20  *.** Declare to 
1723a 74 68 65 20 56 64 62 65 20 74 68 61 74 20 74 68  the Vdbe that th
1723b 65 20 42 54 72 65 65 20 6f 62 6a 65 63 74 20 61  e BTree object a
1723c 74 20 64 62 2d 3e 61 44 62 5b 69 5d 20 69 73 20  t db->aDb[i] is 
1723d 75 73 65 64 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49  used..**.*/.SQLI
1723e 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1723f 73 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42  sqlite3VdbeUsesB
17240 74 72 65 65 28 56 64 62 65 20 2a 70 2c 20 69 6e  tree(Vdbe *p, in
17241 74 20 69 29 7b 0a 20 20 69 6e 74 20 6d 61 73 6b  t i){.  int mask
17242 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ;.  assert( i>=0
17243 20 26 26 20 69 3c 70 2d 3e 64 62 2d 3e 6e 44 62   && i<p->db->nDb
17244 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3c   );.  assert( i<
17245 28 69 6e 74 29 73 69 7a 65 6f 66 28 70 2d 3e 62  (int)sizeof(p->b
17246 74 72 65 65 4d 61 73 6b 29 2a 38 20 29 3b 0a 20  treeMask)*8 );. 
17247 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 3b 0a 20 20   mask = 1<<i;.  
17248 69 66 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  if( (p->btreeMas
17249 6b 20 26 20 6d 61 73 6b 29 3d 3d 30 20 29 7b 0a  k & mask)==0 ){.
1724a 20 20 20 20 70 2d 3e 62 74 72 65 65 4d 61 73 6b      p->btreeMask
1724b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20 73 71   |= mask;.    sq
1724c 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65 78 41  lite3BtreeMutexA
1724d 72 72 61 79 49 6e 73 65 72 74 28 26 70 2d 3e 61  rrayInsert(&p->a
1724e 4d 75 74 65 78 2c 20 70 2d 3e 64 62 2d 3e 61 44  Mutex, p->db->aD
1724f 62 5b 69 5d 2e 70 42 74 29 3b 0a 20 20 7d 0a 7d  b[i].pBt);.  }.}
17250 0a 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 56  ...#if defined(V
17251 44 42 45 5f 50 52 4f 46 49 4c 45 29 20 7c 7c 20  DBE_PROFILE) || 
17252 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 44  defined(SQLITE_D
17253 45 42 55 47 29 0a 2f 2a 0a 2a 2a 20 50 72 69 6e  EBUG)./*.** Prin
17254 74 20 61 20 73 69 6e 67 6c 65 20 6f 70 63 6f 64  t a single opcod
17255 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  e.  This routine
17256 20 69 73 20 75 73 65 64 20 66 6f 72 20 64 65 62   is used for deb
17257 75 67 67 69 6e 67 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ugging only..*/.
17258 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
17259 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 50  oid sqlite3VdbeP
1725a 72 69 6e 74 4f 70 28 46 49 4c 45 20 2a 70 4f 75  rintOp(FILE *pOu
1725b 74 2c 20 69 6e 74 20 70 63 2c 20 4f 70 20 2a 70  t, int pc, Op *p
1725c 4f 70 29 7b 0a 20 20 63 68 61 72 20 2a 7a 50 34  Op){.  char *zP4
1725d 3b 0a 20 20 63 68 61 72 20 7a 50 74 72 5b 35 30  ;.  char zPtr[50
1725e 5d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ];.  static cons
1725f 74 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 31  t char *zFormat1
17260 20 3d 20 22 25 34 64 20 25 2d 31 33 73 20 25 34   = "%4d %-13s %4
17261 64 20 25 34 64 20 25 34 64 20 25 2d 34 73 20 25  d %4d %4d %-4s %
17262 2e 32 58 20 25 73 5c 6e 22 3b 0a 20 20 69 66 28  .2X %s\n";.  if(
17263 20 70 4f 75 74 3d 3d 30 20 29 20 70 4f 75 74 20   pOut==0 ) pOut 
17264 3d 20 73 74 64 6f 75 74 3b 0a 20 20 7a 50 34 20  = stdout;.  zP4 
17265 3d 20 64 69 73 70 6c 61 79 50 34 28 70 4f 70 2c  = displayP4(pOp,
17266 20 7a 50 74 72 2c 20 73 69 7a 65 6f 66 28 7a 50   zPtr, sizeof(zP
17267 74 72 29 29 3b 0a 20 20 66 70 72 69 6e 74 66 28  tr));.  fprintf(
17268 70 4f 75 74 2c 20 7a 46 6f 72 6d 61 74 31 2c 20  pOut, zFormat1, 
17269 70 63 2c 20 0a 20 20 20 20 20 20 73 71 6c 69 74  pc, .      sqlit
1726a 65 33 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70  e3OpcodeName(pOp
1726b 2d 3e 6f 70 63 6f 64 65 29 2c 20 70 4f 70 2d 3e  ->opcode), pOp->
1726c 70 31 2c 20 70 4f 70 2d 3e 70 32 2c 20 70 4f 70  p1, pOp->p2, pOp
1726d 2d 3e 70 33 2c 20 7a 50 34 2c 20 70 4f 70 2d 3e  ->p3, zP4, pOp->
1726e 70 35 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  p5,.#ifdef SQLIT
1726f 45 5f 44 45 42 55 47 0a 20 20 20 20 20 20 70 4f  E_DEBUG.      pO
17270 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3f 20 70 4f  p->zComment ? pO
17271 70 2d 3e 7a 43 6f 6d 6d 65 6e 74 20 3a 20 22 22  p->zComment : ""
17272 0a 23 65 6c 73 65 0a 20 20 20 20 20 20 22 22 0a  .#else.      "".
17273 23 65 6e 64 69 66 0a 20 20 29 3b 0a 20 20 66 66  #endif.  );.  ff
17274 6c 75 73 68 28 70 4f 75 74 29 3b 0a 7d 0a 23 65  lush(pOut);.}.#e
17275 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65  ndif../*.** Rele
17276 61 73 65 20 61 6e 20 61 72 72 61 79 20 6f 66 20  ase an array of 
17277 4e 20 4d 65 6d 20 65 6c 65 6d 65 6e 74 73 0a 2a  N Mem elements.*
17278 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65  /.static void re
17279 6c 65 61 73 65 4d 65 6d 41 72 72 61 79 28 4d 65  leaseMemArray(Me
1727a 6d 20 2a 70 2c 20 69 6e 74 20 4e 29 7b 0a 20 20  m *p, int N){.  
1727b 69 66 28 20 70 20 26 26 20 4e 20 29 7b 0a 20 20  if( p && N ){.  
1727c 20 20 4d 65 6d 20 2a 70 45 6e 64 3b 0a 20 20 20    Mem *pEnd;.   
1727d 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1727e 2d 3e 64 62 3b 0a 20 20 20 20 69 6e 74 20 6d 61  ->db;.    int ma
1727f 6c 6c 6f 63 5f 66 61 69 6c 65 64 20 3d 20 64 62  lloc_failed = db
17280 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 3b 0a  ->mallocFailed;.
17281 20 20 20 20 66 6f 72 28 70 45 6e 64 3d 26 70 5b      for(pEnd=&p[
17282 4e 5d 3b 20 70 3c 70 45 6e 64 3b 20 70 2b 2b 29  N]; p<pEnd; p++)
17283 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
17284 28 26 70 5b 31 5d 29 3d 3d 70 45 6e 64 20 7c 7c  (&p[1])==pEnd ||
17285 20 70 5b 30 5d 2e 64 62 3d 3d 70 5b 31 5d 2e 64   p[0].db==p[1].d
17286 62 20 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 54  b );..      /* T
17287 68 69 73 20 62 6c 6f 63 6b 20 69 73 20 72 65 61  his block is rea
17288 6c 6c 79 20 61 6e 20 69 6e 6c 69 6e 65 64 20 76  lly an inlined v
17289 65 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65  ersion of sqlite
1728a 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
1728b 29 0a 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20  ).      ** that 
1728c 74 61 6b 65 73 20 61 64 76 61 6e 74 61 67 65 20  takes advantage 
1728d 6f 66 20 74 68 65 20 66 61 63 74 20 74 68 61 74  of the fact that
1728e 20 74 68 65 20 6d 65 6d 6f 72 79 20 63 65 6c 6c   the memory cell
1728f 20 76 61 6c 75 65 20 69 73 20 0a 20 20 20 20 20   value is .     
17290 20 2a 2a 20 62 65 69 6e 67 20 73 65 74 20 74 6f   ** being set to
17291 20 4e 55 4c 4c 20 61 66 74 65 72 20 72 65 6c 65   NULL after rele
17292 61 73 69 6e 67 20 61 6e 79 20 64 79 6e 61 6d 69  asing any dynami
17293 63 20 72 65 73 6f 75 72 63 65 73 2e 0a 20 20 20  c resources..   
17294 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
17295 68 65 20 6a 75 73 74 69 66 69 63 61 74 69 6f 6e  he justification
17296 20 66 6f 72 20 64 75 70 6c 69 63 61 74 69 6e 67   for duplicating
17297 20 63 6f 64 65 20 69 73 20 74 68 61 74 20 61 63   code is that ac
17298 63 6f 72 64 69 6e 67 20 74 6f 20 0a 20 20 20 20  cording to .    
17299 20 20 2a 2a 20 63 61 6c 6c 67 72 69 6e 64 2c 20    ** callgrind, 
1729a 74 68 69 73 20 63 61 75 73 65 73 20 61 20 63 65  this causes a ce
1729b 72 74 61 69 6e 20 74 65 73 74 20 63 61 73 65 20  rtain test case 
1729c 74 6f 20 68 69 74 20 74 68 65 20 43 50 55 20 34  to hit the CPU 4
1729d 2e 37 20 0a 20 20 20 20 20 20 2a 2a 20 70 65 72  .7 .      ** per
1729e 63 65 6e 74 20 6c 65 73 73 20 28 78 38 36 20 6c  cent less (x86 l
1729f 69 6e 75 78 2c 20 67 63 63 20 76 65 72 73 69 6f  inux, gcc versio
172a0 6e 20 34 2e 31 2e 32 2c 20 2d 4f 36 29 20 74 68  n 4.1.2, -O6) th
172a1 61 6e 20 69 66 20 0a 20 20 20 20 20 20 2a 2a 20  an if .      ** 
172a2 73 71 6c 69 74 65 33 4d 65 6d 52 65 6c 65 61 73  sqlite3MemReleas
172a3 65 28 29 20 77 65 72 65 20 63 61 6c 6c 65 64 20  e() were called 
172a4 66 72 6f 6d 20 68 65 72 65 2e 20 57 69 74 68 20  from here. With 
172a5 2d 4f 32 2c 20 74 68 69 73 20 6a 75 6d 70 73 0a  -O2, this jumps.
172a6 20 20 20 20 20 20 2a 2a 20 74 6f 20 36 2e 36 20        ** to 6.6 
172a7 70 65 72 63 65 6e 74 2e 20 54 68 65 20 74 65 73  percent. The tes
172a8 74 20 63 61 73 65 20 69 73 20 69 6e 73 65 72 74  t case is insert
172a9 69 6e 67 20 31 30 30 30 20 72 6f 77 73 20 69 6e  ing 1000 rows in
172aa 74 6f 20 61 20 74 61 62 6c 65 20 0a 20 20 20 20  to a table .    
172ab 20 20 2a 2a 20 77 69 74 68 20 6e 6f 20 69 6e 64    ** with no ind
172ac 65 78 65 73 20 75 73 69 6e 67 20 61 20 73 69 6e  exes using a sin
172ad 67 6c 65 20 70 72 65 70 61 72 65 64 20 49 4e 53  gle prepared INS
172ae 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 62  ERT statement, b
172af 69 6e 64 28 29 20 0a 20 20 20 20 20 20 2a 2a 20  ind() .      ** 
172b0 61 6e 64 20 72 65 73 65 74 28 29 2e 20 49 6e 73  and reset(). Ins
172b1 65 72 74 73 20 61 72 65 20 67 72 6f 75 70 65 64  erts are grouped
172b2 20 69 6e 74 6f 20 61 20 74 72 61 6e 73 61 63 74   into a transact
172b3 69 6f 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ion..      */.  
172b4 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73      if( p->flags
172b5 26 28 4d 45 4d 5f 41 67 67 7c 4d 45 4d 5f 44 79  &(MEM_Agg|MEM_Dy
172b6 6e 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  n) ){.        sq
172b7 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
172b8 61 73 65 28 70 29 3b 0a 20 20 20 20 20 20 7d 65  ase(p);.      }e
172b9 6c 73 65 20 69 66 28 20 70 2d 3e 7a 4d 61 6c 6c  lse if( p->zMall
172ba 6f 63 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  oc ){.        sq
172bb 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
172bc 70 2d 3e 7a 4d 61 6c 6c 6f 63 29 3b 0a 20 20 20  p->zMalloc);.   
172bd 20 20 20 20 20 70 2d 3e 7a 4d 61 6c 6c 6f 63 20       p->zMalloc 
172be 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  = 0;.      }..  
172bf 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 3d 20 4d      p->flags = M
172c0 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 7d 0a 20  EM_Null;.    }. 
172c1 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69     db->mallocFai
172c2 6c 65 64 20 3d 20 6d 61 6c 6c 6f 63 5f 66 61 69  led = malloc_fai
172c3 6c 65 64 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 64  led;.  }.}..#ifd
172c4 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
172c5 5f 4d 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45  _MEMORY_MANAGEME
172c6 4e 54 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  NT.SQLITE_PRIVAT
172c7 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62  E int sqlite3Vdb
172c8 65 52 65 6c 65 61 73 65 42 75 66 66 65 72 73 28  eReleaseBuffers(
172c9 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e 74 20  Vdbe *p){.  int 
172ca 69 69 3b 0a 20 20 69 6e 74 20 6e 46 72 65 65 20  ii;.  int nFree 
172cb 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 73  = 0;.  assert( s
172cc 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
172cd 64 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20  d(p->db->mutex) 
172ce 29 3b 0a 20 20 66 6f 72 28 69 69 3d 31 3b 20 69  );.  for(ii=1; i
172cf 69 3c 3d 70 2d 3e 6e 4d 65 6d 3b 20 69 69 2b 2b  i<=p->nMem; ii++
172d0 29 7b 0a 20 20 20 20 4d 65 6d 20 2a 70 4d 65 6d  ){.    Mem *pMem
172d1 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 69 69 5d 3b   = &p->aMem[ii];
172d2 0a 20 20 20 20 69 66 28 20 70 4d 65 6d 2d 3e 7a  .    if( pMem->z
172d3 20 26 26 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 26   && pMem->flags&
172d4 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20 20 20 20  MEM_Dyn ){.     
172d5 20 61 73 73 65 72 74 28 20 21 70 4d 65 6d 2d 3e   assert( !pMem->
172d6 78 44 65 6c 20 29 3b 0a 20 20 20 20 20 20 6e 46  xDel );.      nF
172d7 72 65 65 20 2b 3d 20 73 71 6c 69 74 65 33 44 62  ree += sqlite3Db
172d8 4d 61 6c 6c 6f 63 53 69 7a 65 28 70 4d 65 6d 2d  MallocSize(pMem-
172d9 3e 64 62 2c 20 70 4d 65 6d 2d 3e 7a 29 3b 0a 20  >db, pMem->z);. 
172da 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
172db 4d 65 6d 52 65 6c 65 61 73 65 28 70 4d 65 6d 29  MemRelease(pMem)
172dc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
172dd 74 75 72 6e 20 6e 46 72 65 65 3b 0a 7d 0a 23 65  turn nFree;.}.#e
172de 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
172df 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49  LITE_OMIT_EXPLAI
172e0 4e 0a 2f 2a 0a 2a 2a 20 47 69 76 65 20 61 20 6c  N./*.** Give a l
172e1 69 73 74 69 6e 67 20 6f 66 20 74 68 65 20 70 72  isting of the pr
172e2 6f 67 72 61 6d 20 69 6e 20 74 68 65 20 76 69 72  ogram in the vir
172e3 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 0a 2a 2a  tual machine..**
172e4 0a 2a 2a 20 54 68 65 20 69 6e 74 65 72 66 61 63  .** The interfac
172e5 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
172e6 20 73 71 6c 69 74 65 33 56 64 62 65 45 78 65 63   sqlite3VdbeExec
172e7 28 29 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64  ().  But instead
172e8 20 6f 66 0a 2a 2a 20 72 75 6e 6e 69 6e 67 20 74   of.** running t
172e9 68 65 20 63 6f 64 65 2c 20 69 74 20 69 6e 76 6f  he code, it invo
172ea 6b 65 73 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  kes the callback
172eb 20 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 69   once for each i
172ec 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 20 54  nstruction..** T
172ed 68 69 73 20 66 65 61 74 75 72 65 20 69 73 20 75  his feature is u
172ee 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  sed to implement
172ef 20 22 45 58 50 4c 41 49 4e 22 2e 0a 2a 2a 0a 2a   "EXPLAIN"..**.*
172f0 2a 20 57 68 65 6e 20 70 2d 3e 65 78 70 6c 61 69  * When p->explai
172f1 6e 3d 3d 31 2c 20 65 61 63 68 20 69 6e 73 74 72  n==1, each instr
172f2 75 63 74 69 6f 6e 20 69 73 20 6c 69 73 74 65 64  uction is listed
172f3 2e 20 20 57 68 65 6e 0a 2a 2a 20 70 2d 3e 65 78  .  When.** p->ex
172f4 70 6c 61 69 6e 3d 3d 32 2c 20 6f 6e 6c 79 20 4f  plain==2, only O
172f5 50 5f 45 78 70 6c 61 69 6e 20 69 6e 73 74 72 75  P_Explain instru
172f6 63 74 69 6f 6e 73 20 61 72 65 20 6c 69 73 74 65  ctions are liste
172f7 64 20 61 6e 64 20 74 68 65 73 65 0a 2a 2a 20 61  d and these.** a
172f8 72 65 20 73 68 6f 77 6e 20 69 6e 20 61 20 64 69  re shown in a di
172f9 66 66 65 72 65 6e 74 20 66 6f 72 6d 61 74 2e 20  fferent format. 
172fa 20 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 69   p->explain==2 i
172fb 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
172fc 65 6e 74 0a 2a 2a 20 45 58 50 4c 41 49 4e 20 51  ent.** EXPLAIN Q
172fd 55 45 52 59 20 50 4c 41 4e 2e 0a 2a 2f 0a 53 51  UERY PLAN..*/.SQ
172fe 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
172ff 20 73 71 6c 69 74 65 33 56 64 62 65 4c 69 73 74   sqlite3VdbeList
17300 28 0a 20 20 56 64 62 65 20 2a 70 20 20 20 20 20  (.  Vdbe *p     
17301 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17302 20 54 68 65 20 56 44 42 45 20 2a 2f 0a 29 7b 0a   The VDBE */.){.
17303 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
17304 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b 0a  p->db;.  int i;.
17305 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
17306 45 5f 4f 4b 3b 0a 20 20 4d 65 6d 20 2a 70 4d 65  E_OK;.  Mem *pMe
17307 6d 20 3d 20 70 2d 3e 70 52 65 73 75 6c 74 53 65  m = p->pResultSe
17308 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d 3b  t = &p->aMem[1];
17309 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65  ..  assert( p->e
1730a 78 70 6c 61 69 6e 20 29 3b 0a 20 20 69 66 28 20  xplain );.  if( 
1730b 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d  p->magic!=VDBE_M
1730c 41 47 49 43 5f 52 55 4e 20 29 20 72 65 74 75 72  AGIC_RUN ) retur
1730d 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
1730e 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6d  .  assert( db->m
1730f 61 67 69 63 3d 3d 53 51 4c 49 54 45 5f 4d 41 47  agic==SQLITE_MAG
17310 49 43 5f 42 55 53 59 20 29 3b 0a 20 20 61 73 73  IC_BUSY );.  ass
17311 65 72 74 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49  ert( p->rc==SQLI
17312 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d  TE_OK || p->rc==
17313 53 51 4c 49 54 45 5f 42 55 53 59 20 29 3b 0a 0a  SQLITE_BUSY );..
17314 20 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68    /* Even though
17315 20 74 68 69 73 20 6f 70 63 6f 64 65 20 64 6f 65   this opcode doe
17316 73 20 6e 6f 74 20 75 73 65 20 64 79 6e 61 6d 69  s not use dynami
17317 63 20 73 74 72 69 6e 67 73 20 66 6f 72 0a 20 20  c strings for.  
17318 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2c 20 72  ** the result, r
17319 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 6d 61  esult columns ma
1731a 79 20 62 65 63 6f 6d 65 20 64 79 6e 61 6d 69 63  y become dynamic
1731b 20 69 66 20 74 68 65 20 75 73 65 72 20 63 61 6c   if the user cal
1731c 6c 73 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  ls.  ** sqlite3_
1731d 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28 29 2c  column_text16(),
1731e 20 63 61 75 73 69 6e 67 20 61 20 74 72 61 6e 73   causing a trans
1731f 6c 61 74 69 6f 6e 20 74 6f 20 55 54 46 2d 31 36  lation to UTF-16
17320 20 65 6e 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a   encoding..  */.
17321 20 20 72 65 6c 65 61 73 65 4d 65 6d 41 72 72 61    releaseMemArra
17322 79 28 70 4d 65 6d 2c 20 70 2d 3e 6e 4d 65 6d 29  y(pMem, p->nMem)
17323 3b 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 69 20 3d  ;..  do{.    i =
17324 20 70 2d 3e 70 63 2b 2b 3b 0a 20 20 7d 77 68 69   p->pc++;.  }whi
17325 6c 65 28 20 69 3c 70 2d 3e 6e 4f 70 20 26 26 20  le( i<p->nOp && 
17326 70 2d 3e 65 78 70 6c 61 69 6e 3d 3d 32 20 26 26  p->explain==2 &&
17327 20 70 2d 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64   p->aOp[i].opcod
17328 65 21 3d 4f 50 5f 45 78 70 6c 61 69 6e 20 29 3b  e!=OP_Explain );
17329 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 6e 4f 70  .  if( i>=p->nOp
1732a 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20   ){.    p->rc = 
1732b 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 72  SQLITE_OK;.    r
1732c 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
1732d 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64 62 2d  .  }else if( db-
1732e 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70 74 65  >u1.isInterrupte
1732f 64 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  d ){.    p->rc =
17330 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
17331 54 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  T;.    rc = SQLI
17332 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 73 71  TE_ERROR;.    sq
17333 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
17334 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
17335 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45 72 72  "%s", sqlite3Err
17336 53 74 72 28 70 2d 3e 72 63 29 29 3b 0a 20 20 7d  Str(p->rc));.  }
17337 65 6c 73 65 7b 0a 20 20 20 20 63 68 61 72 20 2a  else{.    char *
17338 7a 3b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20 3d  z;.    Op *pOp =
17339 20 26 70 2d 3e 61 4f 70 5b 69 5d 3b 0a 20 20 20   &p->aOp[i];.   
1733a 20 69 66 28 20 70 2d 3e 65 78 70 6c 61 69 6e 3d   if( p->explain=
1733b 3d 31 20 29 7b 0a 20 20 20 20 20 20 70 4d 65 6d  =1 ){.      pMem
1733c 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
1733d 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  t;.      pMem->t
1733e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
1733f 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d  EGER;.      pMem
17340 2d 3e 75 2e 69 20 3d 20 69 3b 20 20 20 20 20 20  ->u.i = i;      
17341 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17342 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 72 6f            /* Pro
17343 67 72 61 6d 20 63 6f 75 6e 74 65 72 20 2a 2f 0a  gram counter */.
17344 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20        pMem++;.  
17345 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61  .      pMem->fla
17346 67 73 20 3d 20 4d 45 4d 5f 53 74 61 74 69 63 7c  gs = MEM_Static|
17347 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54 65 72 6d  MEM_Str|MEM_Term
17348 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 7a 20  ;.      pMem->z 
17349 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65 33  = (char*)sqlite3
1734a 4f 70 63 6f 64 65 4e 61 6d 65 28 70 4f 70 2d 3e  OpcodeName(pOp->
1734b 6f 70 63 6f 64 65 29 3b 20 20 2f 2a 20 4f 70 63  opcode);  /* Opc
1734c 6f 64 65 20 2a 2f 0a 20 20 20 20 20 20 61 73 73  ode */.      ass
1734d 65 72 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20  ert( pMem->z!=0 
1734e 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e  );.      pMem->n
1734f 20 3d 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e   = strlen(pMem->
17350 7a 29 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  z);.      pMem->
17351 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54 45  type = SQLITE_TE
17352 58 54 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  XT;.      pMem->
17353 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46  enc = SQLITE_UTF
17354 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2b 2b 3b  8;.      pMem++;
17355 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 4d 65 6d  .    }..    pMem
17356 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e  ->flags = MEM_In
17357 74 3b 0a 20 20 20 20 70 4d 65 6d 2d 3e 75 2e 69  t;.    pMem->u.i
17358 20 3d 20 70 4f 70 2d 3e 70 31 3b 20 20 20 20 20   = pOp->p1;     
17359 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1735a 20 20 20 20 20 2f 2a 20 50 31 20 2a 2f 0a 20 20       /* P1 */.  
1735b 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
1735c 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3b 0a 20  QLITE_INTEGER;. 
1735d 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a 20 20 20 20     pMem++;..    
1735e 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
1735f 4d 5f 49 6e 74 3b 0a 20 20 20 20 70 4d 65 6d 2d  M_Int;.    pMem-
17360 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 32 3b 20  >u.i = pOp->p2; 
17361 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17362 20 20 20 20 20 20 20 20 20 2f 2a 20 50 32 20 2a           /* P2 *
17363 2f 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65  /.    pMem->type
17364 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45   = SQLITE_INTEGE
17365 52 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 0a  R;.    pMem++;..
17366 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70 6c 61      if( p->expla
17367 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 70  in==1 ){.      p
17368 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
17369 5f 49 6e 74 3b 0a 20 20 20 20 20 20 70 4d 65 6d  _Int;.      pMem
1736a 2d 3e 75 2e 69 20 3d 20 70 4f 70 2d 3e 70 33 3b  ->u.i = pOp->p3;
1736b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1736c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 33 20            /* P3 
1736d 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 74  */.      pMem->t
1736e 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 49 4e 54  ype = SQLITE_INT
1736f 45 47 45 52 3b 0a 20 20 20 20 20 20 70 4d 65 6d  EGER;.      pMem
17370 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69  ++;.    }..    i
17371 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  f( sqlite3VdbeMe
17372 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 33 32 2c 20  mGrow(pMem, 32, 
17373 30 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20  0) ){           
17374 20 2f 2a 20 50 34 20 2a 2f 0a 20 20 20 20 20 20   /* P4 */.      
17375 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  p->db->mallocFai
17376 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 72  led = 1;.      r
17377 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
17378 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d  EM;.    }.    pM
17379 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  em->flags = MEM_
1737a 44 79 6e 7c 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f  Dyn|MEM_Str|MEM_
1737b 54 65 72 6d 3b 0a 20 20 20 20 7a 20 3d 20 64 69  Term;.    z = di
1737c 73 70 6c 61 79 50 34 28 70 4f 70 2c 20 70 4d 65  splayP4(pOp, pMe
1737d 6d 2d 3e 7a 2c 20 33 32 29 3b 0a 20 20 20 20 69  m->z, 32);.    i
1737e 66 28 20 7a 21 3d 70 4d 65 6d 2d 3e 7a 20 29 7b  f( z!=pMem->z ){
1737f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
17380 62 65 4d 65 6d 53 65 74 53 74 72 28 70 4d 65 6d  beMemSetStr(pMem
17381 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  , z, -1, SQLITE_
17382 55 54 46 38 2c 20 30 29 3b 0a 20 20 20 20 7d 65  UTF8, 0);.    }e
17383 6c 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72  lse{.      asser
17384 74 28 20 70 4d 65 6d 2d 3e 7a 21 3d 30 20 29 3b  t( pMem->z!=0 );
17385 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 6e 20 3d  .      pMem->n =
17386 20 73 74 72 6c 65 6e 28 70 4d 65 6d 2d 3e 7a 29   strlen(pMem->z)
17387 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 65 6e  ;.      pMem->en
17388 63 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b  c = SQLITE_UTF8;
17389 0a 20 20 20 20 7d 0a 20 20 20 20 70 4d 65 6d 2d  .    }.    pMem-
1738a 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f 54  >type = SQLITE_T
1738b 45 58 54 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b  EXT;.    pMem++;
1738c 0a 0a 20 20 20 20 69 66 28 20 70 2d 3e 65 78 70  ..    if( p->exp
1738d 6c 61 69 6e 3d 3d 31 20 29 7b 0a 20 20 20 20 20  lain==1 ){.     
1738e 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1738f 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 34 2c  MemGrow(pMem, 4,
17390 20 30 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70   0) ){.        p
17391 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
17392 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20  ed = 1;.        
17393 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
17394 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  MEM;.      }.   
17395 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
17396 20 4d 45 4d 5f 44 79 6e 7c 4d 45 4d 5f 53 74 72   MEM_Dyn|MEM_Str
17397 7c 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 20  |MEM_Term;.     
17398 20 70 4d 65 6d 2d 3e 6e 20 3d 20 32 3b 0a 20 20   pMem->n = 2;.  
17399 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1739a 69 6e 74 66 28 33 2c 20 70 4d 65 6d 2d 3e 7a 2c  intf(3, pMem->z,
1739b 20 22 25 2e 32 78 22 2c 20 70 4f 70 2d 3e 70 35   "%.2x", pOp->p5
1739c 29 3b 20 20 20 2f 2a 20 50 35 20 2a 2f 0a 20 20  );   /* P5 */.  
1739d 20 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d      pMem->type =
1739e 20 53 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20   SQLITE_TEXT;.  
1739f 20 20 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20      pMem->enc = 
173a0 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a 20 20 20  SQLITE_UTF8;.   
173a1 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20 0a 23 69     pMem++;.  .#i
173a2 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
173a3 47 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d  G.      if( pOp-
173a4 3e 7a 43 6f 6d 6d 65 6e 74 20 29 7b 0a 20 20 20  >zComment ){.   
173a5 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
173a6 20 3d 20 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 54   = MEM_Str|MEM_T
173a7 65 72 6d 3b 0a 20 20 20 20 20 20 20 20 70 4d 65  erm;.        pMe
173a8 6d 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 7a 43 6f 6d  m->z = pOp->zCom
173a9 6d 65 6e 74 3b 0a 20 20 20 20 20 20 20 20 70 4d  ment;.        pM
173aa 65 6d 2d 3e 6e 20 3d 20 73 74 72 6c 65 6e 28 70  em->n = strlen(p
173ab 4d 65 6d 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 20  Mem->z);.       
173ac 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 53 51 4c   pMem->enc = SQL
173ad 49 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 20 20  ITE_UTF8;.      
173ae 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53    pMem->type = S
173af 51 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 20 20  QLITE_TEXT;.    
173b0 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
173b1 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70       {.        p
173b2 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
173b3 5f 4e 75 6c 6c 3b 20 20 20 20 20 20 20 20 20 20  _Null;          
173b4 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
173b5 43 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 20 20 20  Comment */.     
173b6 20 20 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20     pMem->type = 
173b7 53 51 4c 49 54 45 5f 4e 55 4c 4c 3b 0a 20 20 20  SQLITE_NULL;.   
173b8 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20     }.    }..    
173b9 70 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20  p->nResColumn = 
173ba 38 20 2d 20 35 2a 28 70 2d 3e 65 78 70 6c 61 69  8 - 5*(p->explai
173bb 6e 2d 31 29 3b 0a 20 20 20 20 70 2d 3e 72 63 20  n-1);.    p->rc 
173bc 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20  = SQLITE_OK;.   
173bd 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57   rc = SQLITE_ROW
173be 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
173bf 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  c;.}.#endif /* S
173c0 51 4c 49 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41  QLITE_OMIT_EXPLA
173c1 49 4e 20 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51  IN */..#ifdef SQ
173c2 4c 49 54 45 5f 44 45 42 55 47 0a 2f 2a 0a 2a 2a  LITE_DEBUG./*.**
173c3 20 50 72 69 6e 74 20 74 68 65 20 53 51 4c 20 74   Print the SQL t
173c4 68 61 74 20 77 61 73 20 75 73 65 64 20 74 6f 20  hat was used to 
173c5 67 65 6e 65 72 61 74 65 20 61 20 56 44 42 45 20  generate a VDBE 
173c6 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 53 51 4c 49  program..*/.SQLI
173c7 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
173c8 73 71 6c 69 74 65 33 56 64 62 65 50 72 69 6e 74  sqlite3VdbePrint
173c9 53 71 6c 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  Sql(Vdbe *p){.  
173ca 69 6e 74 20 6e 4f 70 20 3d 20 70 2d 3e 6e 4f 70  int nOp = p->nOp
173cb 3b 0a 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b  ;.  VdbeOp *pOp;
173cc 0a 20 20 69 66 28 20 6e 4f 70 3c 31 20 29 20 72  .  if( nOp<1 ) r
173cd 65 74 75 72 6e 3b 0a 20 20 70 4f 70 20 3d 20 26  eturn;.  pOp = &
173ce 70 2d 3e 61 4f 70 5b 30 5d 3b 0a 20 20 69 66 28  p->aOp[0];.  if(
173cf 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
173d0 5f 54 72 61 63 65 20 26 26 20 70 4f 70 2d 3e 70  _Trace && pOp->p
173d1 34 2e 7a 21 3d 30 20 29 7b 0a 20 20 20 20 63 6f  4.z!=0 ){.    co
173d2 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 4f  nst char *z = pO
173d3 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 77 68 69  p->p4.z;.    whi
173d4 6c 65 28 20 69 73 73 70 61 63 65 28 2a 28 75 38  le( isspace(*(u8
173d5 2a 29 7a 29 20 29 20 7a 2b 2b 3b 0a 20 20 20 20  *)z) ) z++;.    
173d6 70 72 69 6e 74 66 28 22 53 51 4c 3a 20 5b 25 73  printf("SQL: [%s
173d7 5d 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d 0a 7d 0a  ]\n", z);.  }.}.
173d8 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66  #endif..#if !def
173d9 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
173da 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69 6e  _TRACE) && defin
173db 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ed(SQLITE_ENABLE
173dc 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a 20  _IOTRACE)./*.** 
173dd 50 72 69 6e 74 20 61 6e 20 49 4f 54 52 41 43 45  Print an IOTRACE
173de 20 6d 65 73 73 61 67 65 20 73 68 6f 77 69 6e 67   message showing
173df 20 53 51 4c 20 63 6f 6e 74 65 6e 74 2e 0a 2a 2f   SQL content..*/
173e0 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
173e1 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65  void sqlite3Vdbe
173e2 49 4f 54 72 61 63 65 53 71 6c 28 56 64 62 65 20  IOTraceSql(Vdbe 
173e3 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 4f 70 20 3d  *p){.  int nOp =
173e4 20 70 2d 3e 6e 4f 70 3b 0a 20 20 56 64 62 65 4f   p->nOp;.  VdbeO
173e5 70 20 2a 70 4f 70 3b 0a 20 20 69 66 28 20 73 71  p *pOp;.  if( sq
173e6 6c 69 74 65 33 49 6f 54 72 61 63 65 3d 3d 30 20  lite3IoTrace==0 
173e7 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
173e8 6e 4f 70 3c 31 20 29 20 72 65 74 75 72 6e 3b 0a  nOp<1 ) return;.
173e9 20 20 70 4f 70 20 3d 20 26 70 2d 3e 61 4f 70 5b    pOp = &p->aOp[
173ea 30 5d 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f  0];.  if( pOp->o
173eb 70 63 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20  pcode==OP_Trace 
173ec 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 21 3d 30 20  && pOp->p4.z!=0 
173ed 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 3b  ){.    int i, j;
173ee 0a 20 20 20 20 63 68 61 72 20 7a 5b 31 30 30 30  .    char z[1000
173ef 5d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73  ];.    sqlite3_s
173f0 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
173f1 29 2c 20 7a 2c 20 22 25 73 22 2c 20 70 4f 70 2d  ), z, "%s", pOp-
173f2 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 66 6f 72 28  >p4.z);.    for(
173f3 69 3d 30 3b 20 69 73 73 70 61 63 65 28 28 75 6e  i=0; isspace((un
173f4 73 69 67 6e 65 64 20 63 68 61 72 29 7a 5b 69 5d  signed char)z[i]
173f5 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 66 6f  ); i++){}.    fo
173f6 72 28 6a 3d 30 3b 20 7a 5b 69 5d 3b 20 69 2b 2b  r(j=0; z[i]; i++
173f7 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73 73  ){.      if( iss
173f8 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63  pace((unsigned c
173f9 68 61 72 29 7a 5b 69 5d 29 20 29 7b 0a 20 20 20  har)z[i]) ){.   
173fa 20 20 20 20 20 69 66 28 20 7a 5b 69 2d 31 5d 21       if( z[i-1]!
173fb 3d 27 20 27 20 29 7b 0a 20 20 20 20 20 20 20 20  =' ' ){.        
173fc 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 20 27 3b 0a    z[j++] = ' ';.
173fd 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
173fe 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a  }else{.        z
173ff 5b 6a 2b 2b 5d 20 3d 20 7a 5b 69 5d 3b 0a 20 20  [j++] = z[i];.  
17400 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
17401 7a 5b 6a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71  z[j] = 0;.    sq
17402 6c 69 74 65 33 49 6f 54 72 61 63 65 28 22 53 51  lite3IoTrace("SQ
17403 4c 20 25 73 5c 6e 22 2c 20 7a 29 3b 0a 20 20 7d  L %s\n", z);.  }
17404 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 53 51  .}.#endif /* !SQ
17405 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 20  LITE_OMIT_TRACE 
17406 26 26 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  && SQLITE_ENABLE
17407 5f 49 4f 54 52 41 43 45 20 2a 2f 0a 0a 0a 2f 2a  _IOTRACE */.../*
17408 0a 2a 2a 20 50 72 65 70 61 72 65 20 61 20 76 69  .** Prepare a vi
17409 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 66 6f  rtual machine fo
1740a 72 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 54 68  r execution.  Th
1740b 69 73 20 69 6e 76 6f 6c 76 65 73 20 74 68 69 6e  is involves thin
1740c 67 73 20 73 75 63 68 0a 2a 2a 20 61 73 20 61 6c  gs such.** as al
1740d 6c 6f 63 61 74 69 6e 67 20 73 74 61 63 6b 20 73  locating stack s
1740e 70 61 63 65 20 61 6e 64 20 69 6e 69 74 69 61 6c  pace and initial
1740f 69 7a 69 6e 67 20 74 68 65 20 70 72 6f 67 72 61  izing the progra
17410 6d 20 63 6f 75 6e 74 65 72 2e 0a 2a 2a 20 41 66  m counter..** Af
17411 74 65 72 20 74 68 65 20 56 44 42 45 20 68 61 73  ter the VDBE has
17412 20 62 65 20 70 72 65 70 70 65 64 2c 20 69 74 20   be prepped, it 
17413 63 61 6e 20 62 65 20 65 78 65 63 75 74 65 64 20  can be executed 
17414 62 79 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 0a 2a  by one or more.*
17415 2a 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74  * calls to sqlit
17416 65 33 56 64 62 65 45 78 65 63 28 29 2e 20 20 0a  e3VdbeExec().  .
17417 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  **.** This is th
17418 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f  e only way to mo
17419 76 65 20 61 20 56 44 42 45 20 66 72 6f 6d 20 56  ve a VDBE from V
1741a 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20 74  DBE_MAGIC_INIT t
1741b 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41 47 49 43 5f  o.** VDBE_MAGIC_
1741c 52 55 4e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  RUN..*/.SQLITE_P
1741d 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1741e 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
1741f 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20 20  (.  Vdbe *p,    
17420 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17421 20 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a     /* The VDBE *
17422 2f 0a 20 20 69 6e 74 20 6e 56 61 72 2c 20 20 20  /.  int nVar,   
17423 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17424 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
17425 27 3f 27 20 73 65 65 20 69 6e 20 74 68 65 20 53  '?' see in the S
17426 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  QL statement */.
17427 20 20 69 6e 74 20 6e 4d 65 6d 2c 20 20 20 20 20    int nMem,     
17428 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17429 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6d 65   /* Number of me
1742a 6d 6f 72 79 20 63 65 6c 6c 73 20 74 6f 20 61 6c  mory cells to al
1742b 6c 6f 63 61 74 65 20 2a 2f 0a 20 20 69 6e 74 20  locate */.  int 
1742c 6e 43 75 72 73 6f 72 2c 20 20 20 20 20 20 20 20  nCursor,        
1742d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
1742e 6d 62 65 72 20 6f 66 20 63 75 72 73 6f 72 73 20  mber of cursors 
1742f 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 20  to allocate */. 
17430 20 69 6e 74 20 69 73 45 78 70 6c 61 69 6e 20 20   int isExplain  
17431 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17432 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 45  /* True if the E
17433 58 50 4c 41 49 4e 20 6b 65 79 77 6f 72 64 73 20  XPLAIN keywords 
17434 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a 29 7b  is present */.){
17435 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 73 71 6c 69  .  int n;.  sqli
17436 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b  te3 *db = p->db;
17437 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d 30  ..  assert( p!=0
17438 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d   );.  assert( p-
17439 3e 6d 61 67 69 63 3d 3d 56 44 42 45 5f 4d 41 47  >magic==VDBE_MAG
1743a 49 43 5f 49 4e 49 54 20 29 3b 0a 0a 20 20 2f 2a  IC_INIT );..  /*
1743b 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65   There should be
1743c 20 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 6f 70   at least one op
1743d 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  code..  */.  ass
1743e 65 72 74 28 20 70 2d 3e 6e 4f 70 3e 30 20 29 3b  ert( p->nOp>0 );
1743f 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6d  ..  /* Set the m
17440 61 67 69 63 20 74 6f 20 56 44 42 45 5f 4d 41 47  agic to VDBE_MAG
17441 49 43 5f 52 55 4e 20 73 6f 6f 6e 65 72 20 72 61  IC_RUN sooner ra
17442 74 68 65 72 20 74 68 61 6e 20 6c 61 74 65 72 2e  ther than later.
17443 20 2a 2f 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d   */.  p->magic =
17444 20 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 3b   VDBE_MAGIC_RUN;
17445 0a 0a 20 20 2f 2a 20 46 6f 72 20 65 61 63 68 20  ..  /* For each 
17446 63 75 72 73 6f 72 20 72 65 71 75 69 72 65 64 2c  cursor required,
17447 20 61 6c 73 6f 20 61 6c 6c 6f 63 61 74 65 20 61   also allocate a
17448 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 2e 20 4d 65   memory cell. Me
17449 6d 6f 72 79 0a 20 20 2a 2a 20 63 65 6c 6c 73 20  mory.  ** cells 
1744a 28 6e 4d 65 6d 2b 31 2d 6e 43 75 72 73 6f 72 29  (nMem+1-nCursor)
1744b 2e 2e 6e 4d 65 6d 2c 20 69 6e 63 6c 75 73 69 76  ..nMem, inclusiv
1744c 65 2c 20 77 69 6c 6c 20 6e 65 76 65 72 20 62 65  e, will never be
1744d 20 75 73 65 64 20 62 79 0a 20 20 2a 2a 20 74 68   used by.  ** th
1744e 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 2e 20  e vdbe program. 
1744f 49 6e 73 74 65 61 64 20 74 68 65 79 20 61 72 65  Instead they are
17450 20 75 73 65 64 20 74 6f 20 61 6c 6c 6f 63 61 74   used to allocat
17451 65 20 73 70 61 63 65 20 66 6f 72 0a 20 20 2a 2a  e space for.  **
17452 20 56 64 62 65 43 75 72 73 6f 72 2f 42 74 43 75   VdbeCursor/BtCu
17453 72 73 6f 72 20 73 74 72 75 63 74 75 72 65 73 2e  rsor structures.
17454 20 54 68 65 20 62 6c 6f 62 20 6f 66 20 6d 65 6d   The blob of mem
17455 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64 20 77  ory associated w
17456 69 74 68 20 0a 20 20 2a 2a 20 63 75 72 73 6f 72  ith .  ** cursor
17457 20 30 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20   0 is stored in 
17458 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 6e 4d 65 6d  memory cell nMem
17459 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 28 6e  . Memory cell (n
1745a 4d 65 6d 2d 31 29 0a 20 20 2a 2a 20 73 74 6f 72  Mem-1).  ** stor
1745b 65 73 20 74 68 65 20 62 6c 6f 62 20 6f 66 20 6d  es the blob of m
1745c 65 6d 6f 72 79 20 61 73 73 6f 63 69 61 74 65 64  emory associated
1745d 20 77 69 74 68 20 63 75 72 73 6f 72 20 31 2c 20   with cursor 1, 
1745e 65 74 63 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 53  etc..  **.  ** S
1745f 65 65 20 61 6c 73 6f 3a 20 61 6c 6c 6f 63 61 74  ee also: allocat
17460 65 43 75 72 73 6f 72 28 29 2e 0a 20 20 2a 2f 0a  eCursor()..  */.
17461 20 20 6e 4d 65 6d 20 2b 3d 20 6e 43 75 72 73 6f    nMem += nCurso
17462 72 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 6c  r;..  /*.  ** Al
17463 6c 6f 63 61 74 69 6f 6e 20 73 70 61 63 65 20 66  location space f
17464 6f 72 20 72 65 67 69 73 74 65 72 73 2e 0a 20 20  or registers..  
17465 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 61 4d 65 6d  */.  if( p->aMem
17466 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e  ==0 ){.    int n
17467 41 72 67 3b 20 20 20 20 20 20 20 2f 2a 20 4d 61  Arg;       /* Ma
17468 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20  ximum number of 
17469 61 72 67 73 20 70 61 73 73 65 64 20 74 6f 20 61  args passed to a
1746a 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2e 20   user function. 
1746b 2a 2f 0a 20 20 20 20 72 65 73 6f 6c 76 65 50 32  */.    resolveP2
1746c 56 61 6c 75 65 73 28 70 2c 20 26 6e 41 72 67 29  Values(p, &nArg)
1746d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 56  ;.    assert( nV
1746e 61 72 3e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28  ar>=0 );.    if(
1746f 20 69 73 45 78 70 6c 61 69 6e 20 26 26 20 6e 4d   isExplain && nM
17470 65 6d 3c 31 30 20 29 7b 0a 20 20 20 20 20 20 6e  em<10 ){.      n
17471 4d 65 6d 20 3d 20 31 30 3b 0a 20 20 20 20 7d 0a  Mem = 10;.    }.
17472 20 20 20 20 70 2d 3e 61 4d 65 6d 20 3d 20 73 71      p->aMem = sq
17473 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
17474 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 6e 4d  o(db,.        nM
17475 65 6d 2a 73 69 7a 65 6f 66 28 4d 65 6d 29 20 20  em*sizeof(Mem)  
17476 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
17477 61 4d 65 6d 20 2a 2f 0a 20 20 20 20 20 20 2b 20  aMem */.      + 
17478 6e 56 61 72 2a 73 69 7a 65 6f 66 28 4d 65 6d 29  nVar*sizeof(Mem)
17479 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1747a 2a 20 61 56 61 72 20 2a 2f 0a 20 20 20 20 20 20  * aVar */.      
1747b 2b 20 6e 41 72 67 2a 73 69 7a 65 6f 66 28 4d 65  + nArg*sizeof(Me
1747c 6d 2a 29 20 20 20 20 20 20 20 20 20 20 20 20 20  m*)             
1747d 20 2f 2a 20 61 70 41 72 67 20 2a 2f 0a 20 20 20   /* apArg */.   
1747e 20 20 20 2b 20 6e 56 61 72 2a 73 69 7a 65 6f 66     + nVar*sizeof
1747f 28 63 68 61 72 2a 29 20 20 20 20 20 20 20 20 20  (char*)         
17480 20 20 20 20 2f 2a 20 61 7a 56 61 72 20 2a 2f 0a      /* azVar */.
17481 20 20 20 20 20 20 2b 20 6e 43 75 72 73 6f 72 2a        + nCursor*
17482 73 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f  sizeof(VdbeCurso
17483 72 2a 29 2b 31 20 20 2f 2a 20 61 70 43 73 72 20  r*)+1  /* apCsr 
17484 2a 2f 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  */.    );.    if
17485 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  ( !db->mallocFai
17486 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  led ){.      p->
17487 61 4d 65 6d 2d 2d 3b 20 20 20 20 20 20 20 20 20  aMem--;         
17488 20 20 20 20 2f 2a 20 61 4d 65 6d 5b 5d 20 67 6f      /* aMem[] go
17489 65 73 20 66 72 6f 6d 20 31 2e 2e 6e 4d 65 6d 20  es from 1..nMem 
1748a 2a 2f 0a 20 20 20 20 20 20 70 2d 3e 6e 4d 65 6d  */.      p->nMem
1748b 20 3d 20 6e 4d 65 6d 3b 20 20 20 20 20 20 20 20   = nMem;        
1748c 2f 2a 20 20 20 20 20 20 20 6e 6f 74 20 66 72 6f  /*       not fro
1748d 6d 20 30 2e 2e 6e 4d 65 6d 2d 31 20 2a 2f 0a 20  m 0..nMem-1 */. 
1748e 20 20 20 20 20 70 2d 3e 61 56 61 72 20 3d 20 26       p->aVar = &
1748f 70 2d 3e 61 4d 65 6d 5b 6e 4d 65 6d 2b 31 5d 3b  p->aMem[nMem+1];
17490 0a 20 20 20 20 20 20 70 2d 3e 6e 56 61 72 20 3d  .      p->nVar =
17491 20 6e 56 61 72 3b 0a 20 20 20 20 20 20 70 2d 3e   nVar;.      p->
17492 6f 6b 56 61 72 20 3d 20 30 3b 0a 20 20 20 20 20  okVar = 0;.     
17493 20 70 2d 3e 61 70 41 72 67 20 3d 20 28 4d 65 6d   p->apArg = (Mem
17494 2a 2a 29 26 70 2d 3e 61 56 61 72 5b 6e 56 61 72  **)&p->aVar[nVar
17495 5d 3b 0a 20 20 20 20 20 20 70 2d 3e 61 7a 56 61  ];.      p->azVa
17496 72 20 3d 20 28 63 68 61 72 2a 2a 29 26 70 2d 3e  r = (char**)&p->
17497 61 70 41 72 67 5b 6e 41 72 67 5d 3b 0a 20 20 20  apArg[nArg];.   
17498 20 20 20 70 2d 3e 61 70 43 73 72 20 3d 20 28 56     p->apCsr = (V
17499 64 62 65 43 75 72 73 6f 72 2a 2a 29 26 70 2d 3e  dbeCursor**)&p->
1749a 61 7a 56 61 72 5b 6e 56 61 72 5d 3b 0a 20 20 20  azVar[nVar];.   
1749b 20 20 20 70 2d 3e 6e 43 75 72 73 6f 72 20 3d 20     p->nCursor = 
1749c 6e 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20 66  nCursor;.      f
1749d 6f 72 28 6e 3d 30 3b 20 6e 3c 6e 56 61 72 3b 20  or(n=0; n<nVar; 
1749e 6e 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 2d  n++){.        p-
1749f 3e 61 56 61 72 5b 6e 5d 2e 66 6c 61 67 73 20 3d  >aVar[n].flags =
174a0 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20   MEM_Null;.     
174a1 20 20 20 70 2d 3e 61 56 61 72 5b 6e 5d 2e 64 62     p->aVar[n].db
174a2 20 3d 20 64 62 3b 0a 20 20 20 20 20 20 7d 0a 20   = db;.      }. 
174a3 20 20 20 20 20 66 6f 72 28 6e 3d 31 3b 20 6e 3c       for(n=1; n<
174a4 3d 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a 20 20 20  =nMem; n++){.   
174a5 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 6e 5d 2e       p->aMem[n].
174a6 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c  flags = MEM_Null
174a7 3b 0a 20 20 20 20 20 20 20 20 70 2d 3e 61 4d 65  ;.        p->aMe
174a8 6d 5b 6e 5d 2e 64 62 20 3d 20 64 62 3b 0a 20 20  m[n].db = db;.  
174a9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
174aa 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
174ab 42 55 47 0a 20 20 66 6f 72 28 6e 3d 31 3b 20 6e  BUG.  for(n=1; n
174ac 3c 70 2d 3e 6e 4d 65 6d 3b 20 6e 2b 2b 29 7b 0a  <p->nMem; n++){.
174ad 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61      assert( p->a
174ae 4d 65 6d 5b 6e 5d 2e 64 62 3d 3d 64 62 20 29 3b  Mem[n].db==db );
174af 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 70  .  }.#endif..  p
174b0 2d 3e 70 63 20 3d 20 2d 31 3b 0a 20 20 70 2d 3e  ->pc = -1;.  p->
174b1 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
174b2 20 20 70 2d 3e 75 6e 69 71 75 65 43 6e 74 20 3d    p->uniqueCnt =
174b3 20 30 3b 0a 20 20 70 2d 3e 65 72 72 6f 72 41 63   0;.  p->errorAc
174b4 74 69 6f 6e 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  tion = OE_Abort;
174b5 0a 20 20 70 2d 3e 65 78 70 6c 61 69 6e 20 7c 3d  .  p->explain |=
174b6 20 69 73 45 78 70 6c 61 69 6e 3b 0a 20 20 70 2d   isExplain;.  p-
174b7 3e 6d 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41  >magic = VDBE_MA
174b8 47 49 43 5f 52 55 4e 3b 0a 20 20 70 2d 3e 6e 43  GIC_RUN;.  p->nC
174b9 68 61 6e 67 65 20 3d 20 30 3b 0a 20 20 70 2d 3e  hange = 0;.  p->
174ba 63 61 63 68 65 43 74 72 20 3d 20 31 3b 0a 20 20  cacheCtr = 1;.  
174bb 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
174bc 6f 72 6d 61 74 20 3d 20 32 35 35 3b 0a 20 20 70  ormat = 255;.  p
174bd 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d 65 6e  ->openedStatemen
174be 74 20 3d 20 30 3b 0a 23 69 66 64 65 66 20 56 44  t = 0;.#ifdef VD
174bf 42 45 5f 50 52 4f 46 49 4c 45 0a 20 20 7b 0a 20  BE_PROFILE.  {. 
174c0 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f     int i;.    fo
174c1 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
174c2 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 2d 3e   i++){.      p->
174c3 61 4f 70 5b 69 5d 2e 63 6e 74 20 3d 20 30 3b 0a  aOp[i].cnt = 0;.
174c4 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d 2e        p->aOp[i].
174c5 63 79 63 6c 65 73 20 3d 20 30 3b 0a 20 20 20 20  cycles = 0;.    
174c6 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a  }.  }.#endif.}..
174c7 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 56 44  /*.** Close a VD
174c8 42 45 20 63 75 72 73 6f 72 20 61 6e 64 20 72 65  BE cursor and re
174c9 6c 65 61 73 65 20 61 6c 6c 20 74 68 65 20 72 65  lease all the re
174ca 73 6f 75 72 63 65 73 20 74 68 61 74 20 63 75 72  sources that cur
174cb 73 6f 72 20 0a 2a 2a 20 68 61 70 70 65 6e 73 20  sor .** happens 
174cc 74 6f 20 68 6f 6c 64 2e 0a 2a 2f 0a 53 51 4c 49  to hold..*/.SQLI
174cd 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
174ce 73 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43  sqlite3VdbeFreeC
174cf 75 72 73 6f 72 28 56 64 62 65 20 2a 70 2c 20 56  ursor(Vdbe *p, V
174d0 64 62 65 43 75 72 73 6f 72 20 2a 70 43 78 29 7b  dbeCursor *pCx){
174d1 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 7b  .  if( pCx==0 ){
174d2 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
174d3 0a 20 20 69 66 28 20 70 43 78 2d 3e 70 42 74 20  .  if( pCx->pBt 
174d4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
174d5 72 65 65 43 6c 6f 73 65 28 70 43 78 2d 3e 70 42  reeClose(pCx->pB
174d6 74 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65 20 70  t);.    /* The p
174d7 43 78 2d 3e 70 43 75 72 73 6f 72 20 77 69 6c 6c  Cx->pCursor will
174d8 20 62 65 20 63 6c 6f 73 65 20 61 75 74 6f 6d 61   be close automa
174d9 74 69 63 61 6c 6c 79 2c 20 69 66 20 69 74 20 65  tically, if it e
174da 78 69 73 74 73 2c 20 62 79 0a 20 20 20 20 2a 2a  xists, by.    **
174db 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65 2e   the call above.
174dc 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
174dd 70 43 78 2d 3e 70 43 75 72 73 6f 72 20 29 7b 0a  pCx->pCursor ){.
174de 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
174df 43 6c 6f 73 65 43 75 72 73 6f 72 28 70 43 78 2d  CloseCursor(pCx-
174e0 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 23  >pCursor);.  }.#
174e1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
174e2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
174e3 20 20 69 66 28 20 70 43 78 2d 3e 70 56 74 61 62    if( pCx->pVtab
174e4 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 73 71  Cursor ){.    sq
174e5 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
174e6 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 20 3d  r *pVtabCursor =
174e7 20 70 43 78 2d 3e 70 56 74 61 62 43 75 72 73 6f   pCx->pVtabCurso
174e8 72 3b 0a 20 20 20 20 63 6f 6e 73 74 20 73 71 6c  r;.    const sql
174e9 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
174ea 64 75 6c 65 20 3d 20 70 43 78 2d 3e 70 4d 6f 64  dule = pCx->pMod
174eb 75 6c 65 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  ule;.    p->inVt
174ec 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a 20 20  abMethod = 1;.  
174ed 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53    (void)sqlite3S
174ee 61 66 65 74 79 4f 66 66 28 70 2d 3e 64 62 29 3b  afetyOff(p->db);
174ef 0a 20 20 20 20 70 4d 6f 64 75 6c 65 2d 3e 78 43  .    pModule->xC
174f0 6c 6f 73 65 28 70 56 74 61 62 43 75 72 73 6f 72  lose(pVtabCursor
174f1 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c  );.    (void)sql
174f2 69 74 65 33 53 61 66 65 74 79 4f 6e 28 70 2d 3e  ite3SafetyOn(p->
174f3 64 62 29 3b 0a 20 20 20 20 70 2d 3e 69 6e 56 74  db);.    p->inVt
174f4 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20  abMethod = 0;.  
174f5 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21  }.#endif.  if( !
174f6 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f  pCx->ephemPseudo
174f7 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73 71 6c  Table ){.    sql
174f8 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62  ite3DbFree(p->db
174f9 2c 20 70 43 78 2d 3e 70 44 61 74 61 29 3b 0a 20  , pCx->pData);. 
174fa 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73   }.}../*.** Clos
174fb 65 20 61 6c 6c 20 63 75 72 73 6f 72 73 20 65 78  e all cursors ex
174fc 63 65 70 74 20 66 6f 72 20 56 54 61 62 20 63 75  cept for VTab cu
174fd 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 63  rsors that are c
174fe 75 72 72 65 6e 74 6c 79 0a 2a 2a 20 69 6e 20 75  urrently.** in u
174ff 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  se..*/.static vo
17500 69 64 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f  id closeAllCurso
17501 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74  rsExceptActiveVt
17502 61 62 73 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  abs(Vdbe *p){.  
17503 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 2d 3e  int i;.  if( p->
17504 61 70 43 73 72 3d 3d 30 20 29 20 72 65 74 75 72  apCsr==0 ) retur
17505 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
17506 70 2d 3e 6e 43 75 72 73 6f 72 3b 20 69 2b 2b 29  p->nCursor; i++)
17507 7b 0a 20 20 20 20 56 64 62 65 43 75 72 73 6f 72  {.    VdbeCursor
17508 20 2a 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b   *pC = p->apCsr[
17509 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 43 20 26  i];.    if( pC &
1750a 26 20 28 21 70 2d 3e 69 6e 56 74 61 62 4d 65 74  & (!p->inVtabMet
1750b 68 6f 64 20 7c 7c 20 21 70 43 2d 3e 70 56 74 61  hod || !pC->pVta
1750c 62 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  bCursor) ){.    
1750d 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 72 65    sqlite3VdbeFre
1750e 65 43 75 72 73 6f 72 28 70 2c 20 70 43 29 3b 0a  eCursor(p, pC);.
1750f 20 20 20 20 20 20 70 2d 3e 61 70 43 73 72 5b 69        p->apCsr[i
17510 5d 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d  ] = 0;.    }.  }
17511 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 6e 20  .}../*.** Clean 
17512 75 70 20 74 68 65 20 56 4d 20 61 66 74 65 72 20  up the VM after 
17513 65 78 65 63 75 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a  execution..**.**
17514 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 77 69   This routine wi
17515 6c 6c 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79  ll automatically
17516 20 63 6c 6f 73 65 20 61 6e 79 20 63 75 72 73 6f   close any curso
17517 72 73 2c 20 6c 69 73 74 73 2c 20 61 6e 64 2f 6f  rs, lists, and/o
17518 72 0a 2a 2a 20 73 6f 72 74 65 72 73 20 74 68 61  r.** sorters tha
17519 74 20 77 65 72 65 20 6c 65 66 74 20 6f 70 65 6e  t were left open
1751a 2e 20 20 49 74 20 61 6c 73 6f 20 64 65 6c 65 74  .  It also delet
1751b 65 73 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66  es the values of
1751c 0a 2a 2a 20 76 61 72 69 61 62 6c 65 73 20 69 6e  .** variables in
1751d 20 74 68 65 20 61 56 61 72 5b 5d 20 61 72 72 61   the aVar[] arra
1751e 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  y..*/.static voi
1751f 64 20 43 6c 65 61 6e 75 70 28 56 64 62 65 20 2a  d Cleanup(Vdbe *
17520 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  p){.  int i;.  s
17521 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d 3e  qlite3 *db = p->
17522 64 62 3b 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75  db;.  closeAllCu
17523 72 73 6f 72 73 45 78 63 65 70 74 41 63 74 69 76  rsorsExceptActiv
17524 65 56 74 61 62 73 28 70 29 3b 0a 20 20 66 6f 72  eVtabs(p);.  for
17525 28 69 3d 31 3b 20 69 3c 3d 70 2d 3e 6e 4d 65 6d  (i=1; i<=p->nMem
17526 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 4d 65 6d 53  ; i++){.    MemS
17527 65 74 54 79 70 65 46 6c 61 67 28 26 70 2d 3e 61  etTypeFlag(&p->a
17528 4d 65 6d 5b 69 5d 2c 20 4d 45 4d 5f 4e 75 6c 6c  Mem[i], MEM_Null
17529 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65  );.  }.  release
1752a 4d 65 6d 41 72 72 61 79 28 26 70 2d 3e 61 4d 65  MemArray(&p->aMe
1752b 6d 5b 31 5d 2c 20 70 2d 3e 6e 4d 65 6d 29 3b 0a  m[1], p->nMem);.
1752c 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69 66    sqlite3VdbeFif
1752d 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46 69 66 6f  oClear(&p->sFifo
1752e 29 3b 0a 20 20 69 66 28 20 70 2d 3e 63 6f 6e 74  );.  if( p->cont
1752f 65 78 74 53 74 61 63 6b 20 29 7b 0a 20 20 20 20  extStack ){.    
17530 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 63 6f  for(i=0; i<p->co
17531 6e 74 65 78 74 53 74 61 63 6b 54 6f 70 3b 20 69  ntextStackTop; i
17532 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
17533 65 33 56 64 62 65 46 69 66 6f 43 6c 65 61 72 28  e3VdbeFifoClear(
17534 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b  &p->contextStack
17535 5b 69 5d 2e 73 46 69 66 6f 29 3b 0a 20 20 20 20  [i].sFifo);.    
17536 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
17537 72 65 65 28 64 62 2c 20 70 2d 3e 63 6f 6e 74 65  ree(db, p->conte
17538 78 74 53 74 61 63 6b 29 3b 0a 20 20 7d 0a 20 20  xtStack);.  }.  
17539 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 20  p->contextStack 
1753a 3d 20 30 3b 0a 20 20 70 2d 3e 63 6f 6e 74 65 78  = 0;.  p->contex
1753b 74 53 74 61 63 6b 44 65 70 74 68 20 3d 20 30 3b  tStackDepth = 0;
1753c 0a 20 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61  .  p->contextSta
1753d 63 6b 54 6f 70 20 3d 20 30 3b 0a 20 20 73 71 6c  ckTop = 0;.  sql
1753e 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1753f 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70 2d  ->zErrMsg);.  p-
17540 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20  >zErrMsg = 0;.  
17541 70 2d 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20  p->pResultSet = 
17542 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20  0;.}../*.** Set 
17543 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 65  the number of re
17544 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20 74 68 61  sult columns tha
17545 74 20 77 69 6c 6c 20 62 65 20 72 65 74 75 72 6e  t will be return
17546 65 64 20 62 79 20 74 68 69 73 20 53 51 4c 0a 2a  ed by this SQL.*
17547 2a 20 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 69  * statement. Thi
17548 73 20 69 73 20 6e 6f 77 20 73 65 74 20 61 74 20  s is now set at 
17549 63 6f 6d 70 69 6c 65 20 74 69 6d 65 2c 20 72 61  compile time, ra
1754a 74 68 65 72 20 74 68 61 6e 20 64 75 72 69 6e 67  ther than during
1754b 0a 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 6f 66  .** execution of
1754c 20 74 68 65 20 76 64 62 65 20 70 72 6f 67 72 61   the vdbe progra
1754d 6d 20 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65  m so that sqlite
1754e 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 29  3_column_count()
1754f 20 63 61 6e 0a 2a 2a 20 62 65 20 63 61 6c 6c 65   can.** be calle
17550 64 20 6f 6e 20 61 6e 20 53 51 4c 20 73 74 61 74  d on an SQL stat
17551 65 6d 65 6e 74 20 62 65 66 6f 72 65 20 73 71 6c  ement before sql
17552 69 74 65 33 5f 73 74 65 70 28 29 2e 0a 2a 2f 0a  ite3_step()..*/.
17553 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
17554 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 53  oid sqlite3VdbeS
17555 65 74 4e 75 6d 43 6f 6c 73 28 56 64 62 65 20 2a  etNumCols(Vdbe *
17556 70 2c 20 69 6e 74 20 6e 52 65 73 43 6f 6c 75 6d  p, int nResColum
17557 6e 29 7b 0a 20 20 4d 65 6d 20 2a 70 43 6f 6c 4e  n){.  Mem *pColN
17558 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  ame;.  int n;.  
17559 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 2d  sqlite3 *db = p-
1755a 3e 64 62 3b 0a 0a 20 20 72 65 6c 65 61 73 65 4d  >db;..  releaseM
1755b 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c 4e  emArray(p->aColN
1755c 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c 75  ame, p->nResColu
1755d 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a 20  mn*COLNAME_N);. 
1755e 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1755f 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29 3b  b, p->aColName);
17560 0a 20 20 6e 20 3d 20 6e 52 65 73 43 6f 6c 75 6d  .  n = nResColum
17561 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 3b 0a 20 20 70  n*COLNAME_N;.  p
17562 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 20 3d 20 6e  ->nResColumn = n
17563 52 65 73 43 6f 6c 75 6d 6e 3b 0a 20 20 70 2d 3e  ResColumn;.  p->
17564 61 43 6f 6c 4e 61 6d 65 20 3d 20 70 43 6f 6c 4e  aColName = pColN
17565 61 6d 65 20 3d 20 28 4d 65 6d 2a 29 73 71 6c 69  ame = (Mem*)sqli
17566 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
17567 64 62 2c 20 73 69 7a 65 6f 66 28 4d 65 6d 29 2a  db, sizeof(Mem)*
17568 6e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 43  n );.  if( p->aC
17569 6f 6c 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74 75  olName==0 ) retu
1756a 72 6e 3b 0a 20 20 77 68 69 6c 65 28 20 6e 2d 2d  rn;.  while( n--
1756b 20 3e 20 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c   > 0 ){.    pCol
1756c 4e 61 6d 65 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  Name->flags = ME
1756d 4d 5f 4e 75 6c 6c 3b 0a 20 20 20 20 70 43 6f 6c  M_Null;.    pCol
1756e 4e 61 6d 65 2d 3e 64 62 20 3d 20 70 2d 3e 64 62  Name->db = p->db
1756f 3b 0a 20 20 20 20 70 43 6f 6c 4e 61 6d 65 2b 2b  ;.    pColName++
17570 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  ;.  }.}../*.** S
17571 65 74 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  et the name of t
17572 68 65 20 69 64 78 27 74 68 20 63 6f 6c 75 6d 6e  he idx'th column
17573 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64 20   to be returned 
17574 62 79 20 74 68 65 20 53 51 4c 20 73 74 61 74 65  by the SQL state
17575 6d 65 6e 74 2e 0a 2a 2a 20 7a 4e 61 6d 65 20 6d  ment..** zName m
17576 75 73 74 20 62 65 20 61 20 70 6f 69 6e 74 65 72  ust be a pointer
17577 20 74 6f 20 61 20 6e 75 6c 20 74 65 72 6d 69 6e   to a nul termin
17578 61 74 65 64 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a  ated string..**.
17579 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 6d 75 73  ** This call mus
1757a 74 20 62 65 20 6d 61 64 65 20 61 66 74 65 72 20  t be made after 
1757b 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  a call to sqlite
1757c 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
1757d 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 6e  )..**.** The fin
1757e 61 6c 20 70 61 72 61 6d 65 74 65 72 2c 20 78 44  al parameter, xD
1757f 65 6c 2c 20 6d 75 73 74 20 62 65 20 6f 6e 65 20  el, must be one 
17580 6f 66 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  of SQLITE_DYNAMI
17581 43 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  C, SQLITE_STATIC
17582 0a 2a 2a 20 6f 72 20 53 51 4c 49 54 45 5f 54 52  .** or SQLITE_TR
17583 41 4e 53 49 45 4e 54 2e 20 49 66 20 69 74 20 69  ANSIENT. If it i
17584 73 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  s SQLITE_DYNAMIC
17585 2c 20 74 68 65 6e 20 74 68 65 20 62 75 66 66 65  , then the buffe
17586 72 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20  r pointed.** to 
17587 62 79 20 7a 4e 61 6d 65 20 77 69 6c 6c 20 62 65  by zName will be
17588 20 66 72 65 65 64 20 62 79 20 73 71 6c 69 74 65   freed by sqlite
17589 33 44 62 46 72 65 65 28 29 20 77 68 65 6e 20 74  3DbFree() when t
1758a 68 65 20 76 64 62 65 20 69 73 20 64 65 73 74 72  he vdbe is destr
1758b 6f 79 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  oyed..*/.SQLITE_
1758c 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1758d 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
1758e 65 28 0a 20 20 56 64 62 65 20 2a 70 2c 20 20 20  e(.  Vdbe *p,   
1758f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17590 20 20 20 20 20 20 2f 2a 20 56 64 62 65 20 62 65        /* Vdbe be
17591 69 6e 67 20 63 6f 6e 66 69 67 75 72 65 64 20 2a  ing configured *
17592 2f 0a 20 20 69 6e 74 20 69 64 78 2c 20 20 20 20  /.  int idx,    
17593 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17594 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
17595 20 63 6f 6c 75 6d 6e 20 7a 4e 61 6d 65 20 61 70   column zName ap
17596 70 6c 69 65 73 20 74 6f 20 2a 2f 0a 20 20 69 6e  plies to */.  in
17597 74 20 76 61 72 2c 20 20 20 20 20 20 20 20 20 20  t var,          
17598 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
17599 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 43 4f 4c  * One of the COL
1759a 4e 41 4d 45 5f 2a 20 63 6f 6e 73 74 61 6e 74 73  NAME_* constants
1759b 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
1759c 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20   *zName,        
1759d 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65         /* Pointe
1759e 72 20 74 6f 20 62 75 66 66 65 72 20 63 6f 6e 74  r to buffer cont
1759f 61 69 6e 69 6e 67 20 6e 61 6d 65 20 2a 2f 0a 20  aining name */. 
175a0 20 76 6f 69 64 20 28 2a 78 44 65 6c 29 28 76 6f   void (*xDel)(vo
175a1 69 64 2a 29 20 20 20 20 20 20 20 20 20 20 20 20  id*)            
175a2 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 6d 61 6e 61    /* Memory mana
175a3 67 65 6d 65 6e 74 20 73 74 72 61 74 65 67 79 20  gement strategy 
175a4 66 6f 72 20 7a 4e 61 6d 65 20 2a 2f 0a 29 7b 0a  for zName */.){.
175a5 20 20 69 6e 74 20 72 63 3b 0a 20 20 4d 65 6d 20    int rc;.  Mem 
175a6 2a 70 43 6f 6c 4e 61 6d 65 3b 0a 20 20 61 73 73  *pColName;.  ass
175a7 65 72 74 28 20 69 64 78 3c 70 2d 3e 6e 52 65 73  ert( idx<p->nRes
175a8 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 61 73 73 65  Column );.  asse
175a9 72 74 28 20 76 61 72 3c 43 4f 4c 4e 41 4d 45 5f  rt( var<COLNAME_
175aa 4e 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 64 62  N );.  if( p->db
175ab 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
175ac 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 7a  {.    assert( !z
175ad 4e 61 6d 65 20 7c 7c 20 78 44 65 6c 21 3d 53 51  Name || xDel!=SQ
175ae 4c 49 54 45 5f 44 59 4e 41 4d 49 43 20 29 3b 0a  LITE_DYNAMIC );.
175af 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
175b0 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 61  E_NOMEM;.  }.  a
175b1 73 73 65 72 74 28 20 70 2d 3e 61 43 6f 6c 4e 61  ssert( p->aColNa
175b2 6d 65 21 3d 30 20 29 3b 0a 20 20 70 43 6f 6c 4e  me!=0 );.  pColN
175b3 61 6d 65 20 3d 20 26 28 70 2d 3e 61 43 6f 6c 4e  ame = &(p->aColN
175b4 61 6d 65 5b 69 64 78 2b 76 61 72 2a 70 2d 3e 6e  ame[idx+var*p->n
175b5 52 65 73 43 6f 6c 75 6d 6e 5d 29 3b 0a 20 20 72  ResColumn]);.  r
175b6 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  c = sqlite3VdbeM
175b7 65 6d 53 65 74 53 74 72 28 70 43 6f 6c 4e 61 6d  emSetStr(pColNam
175b8 65 2c 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51  e, zName, -1, SQ
175b9 4c 49 54 45 5f 55 54 46 38 2c 20 78 44 65 6c 29  LITE_UTF8, xDel)
175ba 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 21 3d  ;.  assert( rc!=
175bb 30 20 7c 7c 20 21 7a 4e 61 6d 65 20 7c 7c 20 28  0 || !zName || (
175bc 70 43 6f 6c 4e 61 6d 65 2d 3e 66 6c 61 67 73 26  pColName->flags&
175bd 4d 45 4d 5f 54 65 72 6d 29 21 3d 30 20 29 3b 0a  MEM_Term)!=0 );.
175be 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
175bf 2f 2a 0a 2a 2a 20 41 20 72 65 61 64 20 6f 72 20  /*.** A read or 
175c0 77 72 69 74 65 20 74 72 61 6e 73 61 63 74 69 6f  write transactio
175c1 6e 20 6d 61 79 20 6f 72 20 6d 61 79 20 6e 6f 74  n may or may not
175c2 20 62 65 20 61 63 74 69 76 65 20 6f 6e 20 64 61   be active on da
175c3 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
175c4 20 64 62 2e 20 49 66 20 61 20 74 72 61 6e 73 61   db. If a transa
175c5 63 74 69 6f 6e 20 69 73 20 61 63 74 69 76 65 2c  ction is active,
175c6 20 63 6f 6d 6d 69 74 20 69 74 2e 20 49 66 20 74   commit it. If t
175c7 68 65 72 65 20 69 73 20 61 0a 2a 2a 20 77 72 69  here is a.** wri
175c8 74 65 2d 74 72 61 6e 73 61 63 74 69 6f 6e 20 73  te-transaction s
175c9 70 61 6e 6e 69 6e 67 20 6d 6f 72 65 20 74 68 61  panning more tha
175ca 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  n one database f
175cb 69 6c 65 2c 20 74 68 69 73 20 72 6f 75 74 69 6e  ile, this routin
175cc 65 0a 2a 2a 20 74 61 6b 65 73 20 63 61 72 65 20  e.** takes care 
175cd 6f 66 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  of the master jo
175ce 75 72 6e 61 6c 20 74 72 69 63 6b 65 72 79 2e 0a  urnal trickery..
175cf 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 76 64  */.static int vd
175d0 62 65 43 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33  beCommit(sqlite3
175d1 20 2a 64 62 2c 20 56 64 62 65 20 2a 70 29 7b 0a   *db, Vdbe *p){.
175d2 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e    int i;.  int n
175d3 54 72 61 6e 73 20 3d 20 30 3b 20 20 2f 2a 20 4e  Trans = 0;  /* N
175d4 75 6d 62 65 72 20 6f 66 20 64 61 74 61 62 61 73  umber of databas
175d5 65 73 20 77 69 74 68 20 61 6e 20 61 63 74 69 76  es with an activ
175d6 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  e write-transact
175d7 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 72 63 20  ion */.  int rc 
175d8 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  = SQLITE_OK;.  i
175d9 6e 74 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 3d  nt needXcommit =
175da 20 30 3b 0a 0a 20 20 2f 2a 20 42 65 66 6f 72 65   0;..  /* Before
175db 20 64 6f 69 6e 67 20 61 6e 79 74 68 69 6e 67 20   doing anything 
175dc 65 6c 73 65 2c 20 63 61 6c 6c 20 74 68 65 20 78  else, call the x
175dd 53 79 6e 63 28 29 20 63 61 6c 6c 62 61 63 6b 20  Sync() callback 
175de 66 6f 72 20 61 6e 79 0a 20 20 2a 2a 20 76 69 72  for any.  ** vir
175df 74 75 61 6c 20 6d 6f 64 75 6c 65 20 74 61 62 6c  tual module tabl
175e0 65 73 20 77 72 69 74 74 65 6e 20 69 6e 20 74 68  es written in th
175e1 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20  is transaction. 
175e2 54 68 69 73 20 68 61 73 20 74 6f 0a 20 20 2a 2a  This has to.  **
175e3 20 62 65 20 64 6f 6e 65 20 62 65 66 6f 72 65 20   be done before 
175e4 64 65 74 65 72 6d 69 6e 69 6e 67 20 77 68 65 74  determining whet
175e5 68 65 72 20 61 20 6d 61 73 74 65 72 20 6a 6f 75  her a master jou
175e6 72 6e 61 6c 20 66 69 6c 65 20 69 73 20 0a 20 20  rnal file is .  
175e7 2a 2a 20 72 65 71 75 69 72 65 64 2c 20 61 73 20  ** required, as 
175e8 61 6e 20 78 53 79 6e 63 28 29 20 63 61 6c 6c 62  an xSync() callb
175e9 61 63 6b 20 6d 61 79 20 61 64 64 20 61 6e 20 61  ack may add an a
175ea 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
175eb 0a 20 20 2a 2a 20 74 6f 20 74 68 65 20 74 72 61  .  ** to the tra
175ec 6e 73 61 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20  nsaction..  */. 
175ed 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61   rc = sqlite3Vta
175ee 62 53 79 6e 63 28 64 62 2c 20 26 70 2d 3e 7a 45  bSync(db, &p->zE
175ef 72 72 4d 73 67 29 3b 0a 20 20 69 66 28 20 72 63  rrMsg);.  if( rc
175f0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
175f1 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
175f2 7d 0a 0a 20 20 2f 2a 20 54 68 69 73 20 6c 6f 6f  }..  /* This loo
175f3 70 20 64 65 74 65 72 6d 69 6e 65 73 20 28 61 29  p determines (a)
175f4 20 69 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 68   if the commit h
175f5 6f 6f 6b 20 73 68 6f 75 6c 64 20 62 65 20 69 6e  ook should be in
175f6 76 6f 6b 65 64 20 61 6e 64 0a 20 20 2a 2a 20 28  voked and.  ** (
175f7 62 29 20 68 6f 77 20 6d 61 6e 79 20 64 61 74 61  b) how many data
175f8 62 61 73 65 20 66 69 6c 65 73 20 68 61 76 65 20  base files have 
175f9 6f 70 65 6e 20 77 72 69 74 65 20 74 72 61 6e 73  open write trans
175fa 61 63 74 69 6f 6e 73 2c 20 6e 6f 74 20 0a 20 20  actions, not .  
175fb 2a 2a 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65  ** including the
175fc 20 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 20   temp database. 
175fd 28 62 29 20 69 73 20 69 6d 70 6f 72 74 61 6e 74  (b) is important
175fe 20 62 65 63 61 75 73 65 20 69 66 20 6d 6f 72 65   because if more
175ff 20 74 68 61 6e 20 0a 20 20 2a 2a 20 6f 6e 65 20   than .  ** one 
17600 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 68 61  database file ha
17601 73 20 61 6e 20 6f 70 65 6e 20 77 72 69 74 65 20  s an open write 
17602 74 72 61 6e 73 61 63 74 69 6f 6e 2c 20 61 20 6d  transaction, a m
17603 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 0a 20 20  aster journal.  
17604 2a 2a 20 66 69 6c 65 20 69 73 20 72 65 71 75 69  ** file is requi
17605 72 65 64 20 66 6f 72 20 61 6e 20 61 74 6f 6d 69  red for an atomi
17606 63 20 63 6f 6d 6d 69 74 2e 0a 20 20 2a 2f 20 0a  c commit..  */ .
17607 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d    for(i=0; i<db-
17608 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20  >nDb; i++){ .   
17609 20 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62   Btree *pBt = db
1760a 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20  ->aDb[i].pBt;.  
1760b 20 20 69 66 28 20 73 71 6c 69 74 65 33 42 74 72    if( sqlite3Btr
1760c 65 65 49 73 49 6e 54 72 61 6e 73 28 70 42 74 29  eeIsInTrans(pBt)
1760d 20 29 7b 0a 20 20 20 20 20 20 6e 65 65 64 58 63   ){.      needXc
1760e 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  ommit = 1;.     
1760f 20 69 66 28 20 69 21 3d 31 20 29 20 6e 54 72 61   if( i!=1 ) nTra
17610 6e 73 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ns++;.    }.  }.
17611 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61  .  /* If there a
17612 72 65 20 61 6e 79 20 77 72 69 74 65 2d 74 72 61  re any write-tra
17613 6e 73 61 63 74 69 6f 6e 73 20 61 74 20 61 6c 6c  nsactions at all
17614 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 6f 6d  , invoke the com
17615 6d 69 74 20 68 6f 6f 6b 20 2a 2f 0a 20 20 69 66  mit hook */.  if
17616 28 20 6e 65 65 64 58 63 6f 6d 6d 69 74 20 26 26  ( needXcommit &&
17617 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c   db->xCommitCall
17618 62 61 63 6b 20 29 7b 0a 20 20 20 20 28 76 6f 69  back ){.    (voi
17619 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
1761a 66 66 28 64 62 29 3b 0a 20 20 20 20 72 63 20 3d  ff(db);.    rc =
1761b 20 64 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c   db->xCommitCall
1761c 62 61 63 6b 28 64 62 2d 3e 70 43 6f 6d 6d 69 74  back(db->pCommit
1761d 41 72 67 29 3b 0a 20 20 20 20 28 76 6f 69 64 29  Arg);.    (void)
1761e 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1761f 64 62 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  db);.    if( rc 
17620 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
17621 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
17622 54 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  T;.    }.  }..  
17623 2f 2a 20 54 68 65 20 73 69 6d 70 6c 65 20 63 61  /* The simple ca
17624 73 65 20 2d 20 6e 6f 20 6d 6f 72 65 20 74 68 61  se - no more tha
17625 6e 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 66  n one database f
17626 69 6c 65 20 28 6e 6f 74 20 63 6f 75 6e 74 69 6e  ile (not countin
17627 67 20 74 68 65 0a 20 20 2a 2a 20 54 45 4d 50 20  g the.  ** TEMP 
17628 64 61 74 61 62 61 73 65 29 20 68 61 73 20 61 20  database) has a 
17629 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 63 74 69  transaction acti
1762a 76 65 2e 20 20 20 54 68 65 72 65 20 69 73 20 6e  ve.   There is n
1762b 6f 20 6e 65 65 64 20 66 6f 72 20 74 68 65 0a 20  o need for the. 
1762c 20 2a 2a 20 6d 61 73 74 65 72 2d 6a 6f 75 72 6e   ** master-journ
1762d 61 6c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66  al..  **.  ** If
1762e 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
1762f 65 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72 65  e of sqlite3Btre
17630 65 47 65 74 46 69 6c 65 6e 61 6d 65 28 29 20 69  eGetFilename() i
17631 73 20 61 20 7a 65 72 6f 20 6c 65 6e 67 74 68 0a  s a zero length.
17632 20 20 2a 2a 20 73 74 72 69 6e 67 2c 20 69 74 20    ** string, it 
17633 6d 65 61 6e 73 20 74 68 65 20 6d 61 69 6e 20 64  means the main d
17634 61 74 61 62 61 73 65 20 69 73 20 3a 6d 65 6d 6f  atabase is :memo
17635 72 79 3a 20 6f 72 20 61 20 74 65 6d 70 20 66 69  ry: or a temp fi
17636 6c 65 2e 20 20 49 6e 20 0a 20 20 2a 2a 20 74 68  le.  In .  ** th
17637 61 74 20 63 61 73 65 20 77 65 20 64 6f 20 6e 6f  at case we do no
17638 74 20 73 75 70 70 6f 72 74 20 61 74 6f 6d 69 63  t support atomic
17639 20 6d 75 6c 74 69 2d 66 69 6c 65 20 63 6f 6d 6d   multi-file comm
1763a 69 74 73 2c 20 73 6f 20 75 73 65 20 74 68 65 20  its, so use the 
1763b 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20 63 61 73  .  ** simple cas
1763c 65 20 74 68 65 6e 20 74 6f 6f 2e 0a 20 20 2a 2f  e then too..  */
1763d 0a 20 20 69 66 28 20 30 3d 3d 73 74 72 6c 65 6e  .  if( 0==strlen
1763e 28 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74  (sqlite3BtreeGet
1763f 46 69 6c 65 6e 61 6d 65 28 64 62 2d 3e 61 44 62  Filename(db->aDb
17640 5b 30 5d 2e 70 42 74 29 29 20 7c 7c 20 6e 54 72  [0].pBt)) || nTr
17641 61 6e 73 3c 3d 31 20 29 7b 0a 20 20 20 20 66 6f  ans<=1 ){.    fo
17642 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
17643 45 5f 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44  E_OK && i<db->nD
17644 62 3b 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20  b; i++){ .      
17645 42 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d  Btree *pBt = db-
17646 3e 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20  >aDb[i].pBt;.   
17647 20 20 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20     if( pBt ){.  
17648 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
17649 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61  e3BtreeCommitPha
1764a 73 65 4f 6e 65 28 70 42 74 2c 20 30 29 3b 0a 20  seOne(pBt, 0);. 
1764b 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
1764c 20 20 2f 2a 20 44 6f 20 74 68 65 20 63 6f 6d 6d    /* Do the comm
1764d 69 74 20 6f 6e 6c 79 20 69 66 20 61 6c 6c 20 64  it only if all d
1764e 61 74 61 62 61 73 65 73 20 73 75 63 63 65 73 73  atabases success
1764f 66 75 6c 6c 79 20 63 6f 6d 70 6c 65 74 65 20 70  fully complete p
17650 68 61 73 65 20 31 2e 20 0a 20 20 20 20 2a 2a 20  hase 1. .    ** 
17651 49 66 20 6f 6e 65 20 6f 66 20 74 68 65 20 42 74  If one of the Bt
17652 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 4f 6e  reeCommitPhaseOn
17653 65 28 29 20 63 61 6c 6c 73 20 66 61 69 6c 73 2c  e() calls fails,
17654 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
17655 61 6e 0a 20 20 20 20 2a 2a 20 49 4f 20 65 72 72  an.    ** IO err
17656 6f 72 20 77 68 69 6c 65 20 64 65 6c 65 74 69 6e  or while deletin
17657 67 20 6f 72 20 74 72 75 6e 63 61 74 69 6e 67 20  g or truncating 
17658 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 2e 20  a journal file. 
17659 49 74 20 69 73 20 75 6e 6c 69 6b 65 6c 79 2c 0a  It is unlikely,.
1765a 20 20 20 20 2a 2a 20 62 75 74 20 63 6f 75 6c 64      ** but could
1765b 20 68 61 70 70 65 6e 2e 20 49 6e 20 74 68 69 73   happen. In this
1765c 20 63 61 73 65 20 61 62 61 6e 64 6f 6e 20 70 72   case abandon pr
1765d 6f 63 65 73 73 69 6e 67 20 61 6e 64 20 72 65 74  ocessing and ret
1765e 75 72 6e 20 74 68 65 20 65 72 72 6f 72 2e 0a 20  urn the error.. 
1765f 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d     */.    for(i=
17660 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  0; rc==SQLITE_OK
17661 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69   && i<db->nDb; i
17662 2b 2b 29 7b 0a 20 20 20 20 20 20 42 74 72 65 65  ++){.      Btree
17663 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b   *pBt = db->aDb[
17664 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
17665 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 20  ( pBt ){.       
17666 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
17667 65 65 43 6f 6d 6d 69 74 50 68 61 73 65 54 77 6f  eeCommitPhaseTwo
17668 28 70 42 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20  (pBt);.      }. 
17669 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 3d     }.    if( rc=
1766a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1766b 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 43      sqlite3VtabC
1766c 6f 6d 6d 69 74 28 64 62 29 3b 0a 20 20 20 20 7d  ommit(db);.    }
1766d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 63  .  }..  /* The c
1766e 6f 6d 70 6c 65 78 20 63 61 73 65 20 2d 20 54 68  omplex case - Th
1766f 65 72 65 20 69 73 20 61 20 6d 75 6c 74 69 2d 66  ere is a multi-f
17670 69 6c 65 20 77 72 69 74 65 2d 74 72 61 6e 73 61  ile write-transa
17671 63 74 69 6f 6e 20 61 63 74 69 76 65 2e 0a 20 20  ction active..  
17672 2a 2a 20 54 68 69 73 20 72 65 71 75 69 72 65 73  ** This requires
17673 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
17674 6c 20 66 69 6c 65 20 74 6f 20 65 6e 73 75 72 65  l file to ensure
17675 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
17676 20 69 73 0a 20 20 2a 2a 20 63 6f 6d 6d 69 74 74   is.  ** committ
17677 65 64 20 61 74 6f 6d 69 63 6c 79 2e 0a 20 20 2a  ed atomicly..  *
17678 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
17679 5f 4f 4d 49 54 5f 44 49 53 4b 49 4f 0a 20 20 65  _OMIT_DISKIO.  e
1767a 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1767b 5f 76 66 73 20 2a 70 56 66 73 20 3d 20 64 62 2d  _vfs *pVfs = db-
1767c 3e 70 56 66 73 3b 0a 20 20 20 20 69 6e 74 20 6e  >pVfs;.    int n
1767d 65 65 64 53 79 6e 63 20 3d 20 30 3b 0a 20 20 20  eedSync = 0;.   
1767e 20 63 68 61 72 20 2a 7a 4d 61 73 74 65 72 20 3d   char *zMaster =
1767f 20 30 3b 20 20 20 2f 2a 20 46 69 6c 65 2d 6e 61   0;   /* File-na
17680 6d 65 20 66 6f 72 20 74 68 65 20 6d 61 73 74 65  me for the maste
17681 72 20 6a 6f 75 72 6e 61 6c 20 2a 2f 0a 20 20 20  r journal */.   
17682 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61   char const *zMa
17683 69 6e 46 69 6c 65 20 3d 20 73 71 6c 69 74 65 33  inFile = sqlite3
17684 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61 6d 65  BtreeGetFilename
17685 28 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 29  (db->aDb[0].pBt)
17686 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69  ;.    sqlite3_fi
17687 6c 65 20 2a 70 4d 61 73 74 65 72 20 3d 20 30 3b  le *pMaster = 0;
17688 0a 20 20 20 20 69 36 34 20 6f 66 66 73 65 74 20  .    i64 offset 
17689 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20 72 65 73  = 0;.    int res
1768a 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 6c 65 63 74  ;..    /* Select
1768b 20 61 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61   a master journa
1768c 6c 20 66 69 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20  l file name */. 
1768d 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 75 33     do {.      u3
1768e 32 20 72 61 6e 64 6f 6d 3b 0a 20 20 20 20 20 20  2 random;.      
1768f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
17690 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  , zMaster);.    
17691 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d    sqlite3_random
17692 6e 65 73 73 28 73 69 7a 65 6f 66 28 72 61 6e 64  ness(sizeof(rand
17693 6f 6d 29 2c 20 26 72 61 6e 64 6f 6d 29 3b 0a 20  om), &random);. 
17694 20 20 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 73       zMaster = s
17695 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
17696 2c 20 22 25 73 2d 6d 6a 25 30 38 58 22 2c 20 7a  , "%s-mj%08X", z
17697 4d 61 69 6e 46 69 6c 65 2c 20 72 61 6e 64 6f 6d  MainFile, random
17698 26 30 78 37 66 66 66 66 66 66 66 29 3b 0a 20 20  &0x7fffffff);.  
17699 20 20 20 20 69 66 28 20 21 7a 4d 61 73 74 65 72      if( !zMaster
1769a 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
1769b 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
1769c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72  .      }.      r
1769d 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63  c = sqlite3OsAcc
1769e 65 73 73 28 70 56 66 73 2c 20 7a 4d 61 73 74 65  ess(pVfs, zMaste
1769f 72 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53 53  r, SQLITE_ACCESS
176a0 5f 45 58 49 53 54 53 2c 20 26 72 65 73 29 3b 0a  _EXISTS, &res);.
176a1 20 20 20 20 7d 77 68 69 6c 65 28 20 72 63 3d 3d      }while( rc==
176a2 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65 73  SQLITE_OK && res
176a3 20 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   );.    if( rc==
176a4 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
176a5 20 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20 6d     /* Open the m
176a6 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 2e 20 2a  aster journal. *
176a7 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
176a8 69 74 65 33 4f 73 4f 70 65 6e 4d 61 6c 6c 6f 63  ite3OsOpenMalloc
176a9 28 70 56 66 73 2c 20 7a 4d 61 73 74 65 72 2c 20  (pVfs, zMaster, 
176aa 26 70 4d 61 73 74 65 72 2c 20 0a 20 20 20 20 20  &pMaster, .     
176ab 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
176ac 5f 52 45 41 44 57 52 49 54 45 7c 53 51 4c 49 54  _READWRITE|SQLIT
176ad 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 7c 0a 20  E_OPEN_CREATE|. 
176ae 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
176af 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 7c 53  OPEN_EXCLUSIVE|S
176b0 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 53 54 45  QLITE_OPEN_MASTE
176b1 52 5f 4a 4f 55 52 4e 41 4c 2c 20 30 0a 20 20 20  R_JOURNAL, 0.   
176b2 20 20 20 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20     );.    }.    
176b3 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
176b4 4b 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  K ){.      sqlit
176b5 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 61  e3DbFree(db, zMa
176b6 73 74 65 72 29 3b 0a 20 20 20 20 20 20 72 65 74  ster);.      ret
176b7 75 72 6e 20 72 63 3b 0a 20 20 20 20 7d 0a 20 0a  urn rc;.    }. .
176b8 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
176b9 20 6e 61 6d 65 20 6f 66 20 65 61 63 68 20 64 61   name of each da
176ba 74 61 62 61 73 65 20 66 69 6c 65 20 69 6e 20 74  tabase file in t
176bb 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69  he transaction i
176bc 6e 74 6f 20 74 68 65 20 6e 65 77 0a 20 20 20 20  nto the new.    
176bd 2a 2a 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  ** master journa
176be 6c 20 66 69 6c 65 2e 20 49 66 20 61 6e 20 65 72  l file. If an er
176bf 72 6f 72 20 6f 63 63 75 72 73 20 61 74 20 74 68  ror occurs at th
176c0 69 73 20 70 6f 69 6e 74 20 63 6c 6f 73 65 0a 20  is point close. 
176c1 20 20 20 2a 2a 20 61 6e 64 20 64 65 6c 65 74 65     ** and delete
176c2 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
176c3 6e 61 6c 20 66 69 6c 65 2e 20 41 6c 6c 20 74 68  nal file. All th
176c4 65 20 69 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75  e individual jou
176c5 72 6e 61 6c 20 66 69 6c 65 73 0a 20 20 20 20 2a  rnal files.    *
176c6 2a 20 73 74 69 6c 6c 20 68 61 76 65 20 27 6e 75  * still have 'nu
176c7 6c 6c 27 20 61 73 20 74 68 65 20 6d 61 73 74 65  ll' as the maste
176c8 72 20 6a 6f 75 72 6e 61 6c 20 70 6f 69 6e 74 65  r journal pointe
176c9 72 2c 20 73 6f 20 74 68 65 79 20 77 69 6c 6c 20  r, so they will 
176ca 72 6f 6c 6c 0a 20 20 20 20 2a 2a 20 62 61 63 6b  roll.    ** back
176cb 20 69 6e 64 65 70 65 6e 64 65 6e 74 6c 79 20 69   independently i
176cc 66 20 61 20 66 61 69 6c 75 72 65 20 6f 63 63 75  f a failure occu
176cd 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66  rs..    */.    f
176ce 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44  or(i=0; i<db->nD
176cf 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 42  b; i++){.      B
176d0 74 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e  tree *pBt = db->
176d1 61 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20  aDb[i].pBt;.    
176d2 20 20 69 66 28 20 69 3d 3d 31 20 29 20 63 6f 6e    if( i==1 ) con
176d3 74 69 6e 75 65 3b 20 20 20 2f 2a 20 49 67 6e 6f  tinue;   /* Igno
176d4 72 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61  re the TEMP data
176d5 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 69 66  base */.      if
176d6 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73  ( sqlite3BtreeIs
176d7 49 6e 54 72 61 6e 73 28 70 42 74 29 20 29 7b 0a  InTrans(pBt) ){.
176d8 20 20 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e          char con
176d9 73 74 20 2a 7a 46 69 6c 65 20 3d 20 73 71 6c 69  st *zFile = sqli
176da 74 65 33 42 74 72 65 65 47 65 74 4a 6f 75 72 6e  te3BtreeGetJourn
176db 61 6c 6e 61 6d 65 28 70 42 74 29 3b 0a 20 20 20  alname(pBt);.   
176dc 20 20 20 20 20 69 66 28 20 7a 46 69 6c 65 5b 30       if( zFile[0
176dd 5d 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b  ]==0 ) continue;
176de 20 20 2f 2a 20 49 67 6e 6f 72 65 20 3a 6d 65 6d    /* Ignore :mem
176df 6f 72 79 3a 20 64 61 74 61 62 61 73 65 73 20 2a  ory: databases *
176e0 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 21 6e  /.        if( !n
176e1 65 65 64 53 79 6e 63 20 26 26 20 21 73 71 6c 69  eedSync && !sqli
176e2 74 65 33 42 74 72 65 65 53 79 6e 63 44 69 73 61  te3BtreeSyncDisa
176e3 62 6c 65 64 28 70 42 74 29 20 29 7b 0a 20 20 20  bled(pBt) ){.   
176e4 20 20 20 20 20 20 20 6e 65 65 64 53 79 6e 63 20         needSync 
176e5 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  = 1;.        }. 
176e6 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
176e7 74 65 33 4f 73 57 72 69 74 65 28 70 4d 61 73 74  te3OsWrite(pMast
176e8 65 72 2c 20 7a 46 69 6c 65 2c 20 73 74 72 6c 65  er, zFile, strle
176e9 6e 28 7a 46 69 6c 65 29 2b 31 2c 20 6f 66 66 73  n(zFile)+1, offs
176ea 65 74 29 3b 0a 20 20 20 20 20 20 20 20 6f 66 66  et);.        off
176eb 73 65 74 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 46  set += strlen(zF
176ec 69 6c 65 29 2b 31 3b 0a 20 20 20 20 20 20 20 20  ile)+1;.        
176ed 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
176ee 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  K ){.          s
176ef 71 6c 69 74 65 33 4f 73 43 6c 6f 73 65 46 72 65  qlite3OsCloseFre
176f0 65 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 20 20  e(pMaster);.    
176f1 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44        sqlite3OsD
176f2 65 6c 65 74 65 28 70 56 66 73 2c 20 7a 4d 61 73  elete(pVfs, zMas
176f3 74 65 72 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ter, 0);.       
176f4 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
176f5 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
176f6 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
176f7 72 63 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  rc;.        }.  
176f8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20      }.    }..   
176f9 20 2f 2a 20 53 79 6e 63 20 74 68 65 20 6d 61 73   /* Sync the mas
176fa 74 65 72 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65  ter journal file
176fb 2e 20 49 66 20 74 68 65 20 49 4f 43 41 50 5f 53  . If the IOCAP_S
176fc 45 51 55 45 4e 54 49 41 4c 20 64 65 76 69 63 65  EQUENTIAL device
176fd 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 73 20  .    ** flag is 
176fe 73 65 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20  set this is not 
176ff 72 65 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f  required..    */
17700 0a 20 20 20 20 7a 4d 61 69 6e 46 69 6c 65 20 3d  .    zMainFile =
17701 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74   sqlite3BtreeGet
17702 44 69 72 6e 61 6d 65 28 64 62 2d 3e 61 44 62 5b  Dirname(db->aDb[
17703 30 5d 2e 70 42 74 29 3b 0a 20 20 20 20 69 66 28  0].pBt);.    if(
17704 20 28 6e 65 65 64 53 79 6e 63 20 0a 20 20 20 20   (needSync .    
17705 20 26 26 20 28 30 3d 3d 28 73 71 6c 69 74 65 33   && (0==(sqlite3
17706 4f 73 44 65 76 69 63 65 43 68 61 72 61 63 74 65  OsDeviceCharacte
17707 72 69 73 74 69 63 73 28 70 4d 61 73 74 65 72 29  ristics(pMaster)
17708 26 53 51 4c 49 54 45 5f 49 4f 43 41 50 5f 53 45  &SQLITE_IOCAP_SE
17709 51 55 45 4e 54 49 41 4c 29 29 0a 20 20 20 20 20  QUENTIAL)).     
1770a 26 26 20 28 72 63 3d 73 71 6c 69 74 65 33 4f 73  && (rc=sqlite3Os
1770b 53 79 6e 63 28 70 4d 61 73 74 65 72 2c 20 53 51  Sync(pMaster, SQ
1770c 4c 49 54 45 5f 53 59 4e 43 5f 4e 4f 52 4d 41 4c  LITE_SYNC_NORMAL
1770d 29 29 21 3d 53 51 4c 49 54 45 5f 4f 4b 29 20 29  ))!=SQLITE_OK) )
1770e 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f  {.      sqlite3O
1770f 73 43 6c 6f 73 65 46 72 65 65 28 70 4d 61 73 74  sCloseFree(pMast
17710 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  er);.      sqlit
17711 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73 2c  e3OsDelete(pVfs,
17712 20 7a 4d 61 73 74 65 72 2c 20 30 29 3b 0a 20 20   zMaster, 0);.  
17713 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
17714 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a  e(db, zMaster);.
17715 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
17716 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
17717 79 6e 63 20 61 6c 6c 20 74 68 65 20 64 62 20 66  ync all the db f
17718 69 6c 65 73 20 69 6e 76 6f 6c 76 65 64 20 69 6e  iles involved in
17719 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1771a 2e 20 54 68 65 20 73 61 6d 65 20 63 61 6c 6c 0a  . The same call.
1771b 20 20 20 20 2a 2a 20 73 65 74 73 20 74 68 65 20      ** sets the 
1771c 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20 70  master journal p
1771d 6f 69 6e 74 65 72 20 69 6e 20 65 61 63 68 20 69  ointer in each i
1771e 6e 64 69 76 69 64 75 61 6c 20 6a 6f 75 72 6e 61  ndividual journa
1771f 6c 2e 20 49 66 0a 20 20 20 20 2a 2a 20 61 6e 20  l. If.    ** an 
17720 65 72 72 6f 72 20 6f 63 63 75 72 73 20 68 65 72  error occurs her
17721 65 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74 65  e, do not delete
17722 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72   the master jour
17723 6e 61 6c 20 66 69 6c 65 2e 0a 20 20 20 20 2a 2a  nal file..    **
17724 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 65 20 65  .    ** If the e
17725 72 72 6f 72 20 6f 63 63 75 72 73 20 64 75 72 69  rror occurs duri
17726 6e 67 20 74 68 65 20 66 69 72 73 74 20 63 61 6c  ng the first cal
17727 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
17728 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68  te3BtreeCommitPh
17729 61 73 65 4f 6e 65 28 29 2c 20 74 68 65 6e 20 74  aseOne(), then t
1772a 68 65 72 65 20 69 73 20 61 20 63 68 61 6e 63 65  here is a chance
1772b 20 74 68 61 74 20 74 68 65 0a 20 20 20 20 2a 2a   that the.    **
1772c 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61 6c 20   master journal 
1772d 66 69 6c 65 20 77 69 6c 6c 20 62 65 20 6f 72 70  file will be orp
1772e 68 61 6e 65 64 2e 20 42 75 74 20 77 65 20 63 61  haned. But we ca
1772f 6e 6e 6f 74 20 64 65 6c 65 74 65 20 69 74 2c 0a  nnot delete it,.
17730 20 20 20 20 2a 2a 20 69 6e 20 63 61 73 65 20 74      ** in case t
17731 68 65 20 6d 61 73 74 65 72 20 6a 6f 75 72 6e 61  he master journa
17732 6c 20 66 69 6c 65 20 6e 61 6d 65 20 77 61 73 20  l file name was 
17733 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 74 68 65  written into the
17734 20 6a 6f 75 72 6e 61 6c 0a 20 20 20 20 2a 2a 20   journal.    ** 
17735 66 69 6c 65 20 62 65 66 6f 72 65 20 74 68 65 20  file before the 
17736 66 61 69 6c 75 72 65 20 6f 63 63 75 72 65 64 2e  failure occured.
17737 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
17738 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f  i=0; rc==SQLITE_
17739 4f 4b 20 26 26 20 69 3c 64 62 2d 3e 6e 44 62 3b  OK && i<db->nDb;
1773a 20 69 2b 2b 29 7b 20 0a 20 20 20 20 20 20 42 74   i++){ .      Bt
1773b 72 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61  ree *pBt = db->a
1773c 44 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20  Db[i].pBt;.     
1773d 20 69 66 28 20 70 42 74 20 29 7b 0a 20 20 20 20   if( pBt ){.    
1773e 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1773f 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73 65  BtreeCommitPhase
17740 4f 6e 65 28 70 42 74 2c 20 7a 4d 61 73 74 65 72  One(pBt, zMaster
17741 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
17742 0a 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 6c  .    sqlite3OsCl
17743 6f 73 65 46 72 65 65 28 70 4d 61 73 74 65 72 29  oseFree(pMaster)
17744 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
17745 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
17746 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
17747 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20 20 20  b, zMaster);.   
17748 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
17749 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65    }..    /* Dele
1774a 74 65 20 74 68 65 20 6d 61 73 74 65 72 20 6a 6f  te the master jo
1774b 75 72 6e 61 6c 20 66 69 6c 65 2e 20 54 68 69 73  urnal file. This
1774c 20 63 6f 6d 6d 69 74 73 20 74 68 65 20 74 72 61   commits the tra
1774d 6e 73 61 63 74 69 6f 6e 2e 20 41 66 74 65 72 0a  nsaction. After.
1774e 20 20 20 20 2a 2a 20 64 6f 69 6e 67 20 74 68 69      ** doing thi
1774f 73 20 74 68 65 20 64 69 72 65 63 74 6f 72 79 20  s the directory 
17750 69 73 20 73 79 6e 63 65 64 20 61 67 61 69 6e 20  is synced again 
17751 62 65 66 6f 72 65 20 61 6e 79 20 69 6e 64 69 76  before any indiv
17752 69 64 75 61 6c 0a 20 20 20 20 2a 2a 20 74 72 61  idual.    ** tra
17753 6e 73 61 63 74 69 6f 6e 20 66 69 6c 65 73 20 61  nsaction files a
17754 72 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20  re deleted..    
17755 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  */.    rc = sqli
17756 74 65 33 4f 73 44 65 6c 65 74 65 28 70 56 66 73  te3OsDelete(pVfs
17757 2c 20 7a 4d 61 73 74 65 72 2c 20 31 29 3b 0a 20  , zMaster, 1);. 
17758 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
17759 28 64 62 2c 20 7a 4d 61 73 74 65 72 29 3b 0a 20  (db, zMaster);. 
1775a 20 20 20 7a 4d 61 73 74 65 72 20 3d 20 30 3b 0a     zMaster = 0;.
1775b 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20      if( rc ){.  
1775c 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
1775d 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c     }..    /* All
1775e 20 66 69 6c 65 73 20 61 6e 64 20 64 69 72 65 63   files and direc
1775f 74 6f 72 69 65 73 20 68 61 76 65 20 61 6c 72 65  tories have alre
17760 61 64 79 20 62 65 65 6e 20 73 79 6e 63 65 64 2c  ady been synced,
17761 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   so the followin
17762 67 0a 20 20 20 20 2a 2a 20 63 61 6c 6c 73 20 74  g.    ** calls t
17763 6f 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f  o sqlite3BtreeCo
17764 6d 6d 69 74 50 68 61 73 65 54 77 6f 28 29 20 61  mmitPhaseTwo() a
17765 72 65 20 6f 6e 6c 79 20 63 6c 6f 73 69 6e 67 20  re only closing 
17766 66 69 6c 65 73 20 61 6e 64 0a 20 20 20 20 2a 2a  files and.    **
17767 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 74 72 75   deleting or tru
17768 6e 63 61 74 69 6e 67 20 6a 6f 75 72 6e 61 6c 73  ncating journals
17769 2e 20 49 66 20 73 6f 6d 65 74 68 69 6e 67 20 67  . If something g
1776a 6f 65 73 20 77 72 6f 6e 67 20 77 68 69 6c 65 0a  oes wrong while.
1776b 20 20 20 20 2a 2a 20 74 68 69 73 20 69 73 20 68      ** this is h
1776c 61 70 70 65 6e 69 6e 67 20 77 65 20 64 6f 6e 27  appening we don'
1776d 74 20 72 65 61 6c 6c 79 20 63 61 72 65 2e 20 54  t really care. T
1776e 68 65 20 69 6e 74 65 67 72 69 74 79 20 6f 66 20  he integrity of 
1776f 74 68 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73  the.    ** trans
17770 61 63 74 69 6f 6e 20 69 73 20 61 6c 72 65 61 64  action is alread
17771 79 20 67 75 61 72 61 6e 74 65 65 64 2c 20 62 75  y guaranteed, bu
17772 74 20 73 6f 6d 65 20 73 74 72 61 79 20 27 63 6f  t some stray 'co
17773 6c 64 27 20 6a 6f 75 72 6e 61 6c 73 0a 20 20 20  ld' journals.   
17774 20 2a 2a 20 6d 61 79 20 62 65 20 6c 79 69 6e 67   ** may be lying
17775 20 61 72 6f 75 6e 64 2e 20 52 65 74 75 72 6e 69   around. Returni
17776 6e 67 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65  ng an error code
17777 20 77 6f 6e 27 74 20 68 65 6c 70 20 6d 61 74 74   won't help matt
17778 65 72 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ers..    */.    
17779 64 69 73 61 62 6c 65 5f 73 69 6d 75 6c 61 74 65  disable_simulate
1777a 64 5f 69 6f 5f 65 72 72 6f 72 73 28 29 3b 0a 20  d_io_errors();. 
1777b 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 42     sqlite3BeginB
1777c 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20  enignMalloc();. 
1777d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62     for(i=0; i<db
1777e 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a 20 20  ->nDb; i++){ .  
1777f 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
17780 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
17781 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
17782 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
17783 33 42 74 72 65 65 43 6f 6d 6d 69 74 50 68 61 73  3BtreeCommitPhas
17784 65 54 77 6f 28 70 42 74 29 3b 0a 20 20 20 20 20  eTwo(pBt);.     
17785 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
17786 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
17787 6c 6f 63 28 29 3b 0a 20 20 20 20 65 6e 61 62 6c  loc();.    enabl
17788 65 5f 73 69 6d 75 6c 61 74 65 64 5f 69 6f 5f 65  e_simulated_io_e
17789 72 72 6f 72 73 28 29 3b 0a 0a 20 20 20 20 73 71  rrors();..    sq
1778a 6c 69 74 65 33 56 74 61 62 43 6f 6d 6d 69 74 28  lite3VtabCommit(
1778b 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  db);.  }.#endif.
1778c 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1778d 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ./* .** This rou
1778e 74 69 6e 65 20 63 68 65 63 6b 73 20 74 68 61 74  tine checks that
1778f 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 63 74   the sqlite3.act
17790 69 76 65 56 64 62 65 43 6e 74 20 63 6f 75 6e 74  iveVdbeCnt count
17791 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 6d 61 74   variable.** mat
17792 63 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 20  ches the number 
17793 6f 66 20 76 64 62 65 27 73 20 69 6e 20 74 68 65  of vdbe's in the
17794 20 6c 69 73 74 20 73 71 6c 69 74 65 33 2e 70 56   list sqlite3.pV
17795 64 62 65 20 74 68 61 74 20 61 72 65 0a 2a 2a 20  dbe that are.** 
17796 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69 76 65  currently active
17797 2e 20 41 6e 20 61 73 73 65 72 74 69 6f 6e 20 66  . An assertion f
17798 61 69 6c 73 20 69 66 20 74 68 65 20 74 77 6f 20  ails if the two 
17799 63 6f 75 6e 74 73 20 64 6f 20 6e 6f 74 20 6d 61  counts do not ma
1779a 74 63 68 2e 0a 2a 2a 20 54 68 69 73 20 69 73 20  tch..** This is 
1779b 61 6e 20 69 6e 74 65 72 6e 61 6c 20 73 65 6c 66  an internal self
1779c 2d 63 68 65 63 6b 20 6f 6e 6c 79 20 2d 20 69 74  -check only - it
1779d 20 69 73 20 6e 6f 74 20 61 6e 20 65 73 73 65 6e   is not an essen
1779e 74 69 61 6c 20 70 72 6f 63 65 73 73 69 6e 67 0a  tial processing.
1779f 2a 2a 20 73 74 65 70 2e 0a 2a 2a 0a 2a 2a 20 54  ** step..**.** T
177a0 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f 70 20 69  his is a no-op i
177a1 66 20 4e 44 45 42 55 47 20 69 73 20 64 65 66 69  f NDEBUG is defi
177a2 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ned..*/.#ifndef 
177a3 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20 76 6f  NDEBUG.static vo
177a4 69 64 20 63 68 65 63 6b 41 63 74 69 76 65 56 64  id checkActiveVd
177a5 62 65 43 6e 74 28 73 71 6c 69 74 65 33 20 2a 64  beCnt(sqlite3 *d
177a6 62 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20  b){.  Vdbe *p;. 
177a7 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
177a8 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 30 3b 0a  int nWrite = 0;.
177a9 20 20 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b    p = db->pVdbe;
177aa 0a 20 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20  .  while( p ){. 
177ab 20 20 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 3d     if( p->magic=
177ac 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20  =VDBE_MAGIC_RUN 
177ad 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  && p->pc>=0 ){. 
177ae 20 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20       cnt++;.    
177af 20 20 69 66 28 20 70 2d 3e 72 65 61 64 4f 6e 6c    if( p->readOnl
177b0 79 3d 3d 30 20 29 20 6e 57 72 69 74 65 2b 2b 3b  y==0 ) nWrite++;
177b1 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20 70  .    }.    p = p
177b2 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 61  ->pNext;.  }.  a
177b3 73 73 65 72 74 28 20 63 6e 74 3d 3d 64 62 2d 3e  ssert( cnt==db->
177b4 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 3b  activeVdbeCnt );
177b5 0a 20 20 61 73 73 65 72 74 28 20 6e 57 72 69 74  .  assert( nWrit
177b6 65 3d 3d 64 62 2d 3e 77 72 69 74 65 56 64 62 65  e==db->writeVdbe
177b7 43 6e 74 20 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  Cnt );.}.#else.#
177b8 64 65 66 69 6e 65 20 63 68 65 63 6b 41 63 74 69  define checkActi
177b9 76 65 56 64 62 65 43 6e 74 28 78 29 0a 23 65 6e  veVdbeCnt(x).#en
177ba 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20 65  dif../*.** For e
177bb 76 65 72 79 20 42 74 72 65 65 20 74 68 61 74 20  very Btree that 
177bc 69 6e 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  in database conn
177bd 65 63 74 69 6f 6e 20 64 62 20 77 68 69 63 68 20  ection db which 
177be 0a 2a 2a 20 68 61 73 20 62 65 65 6e 20 6d 6f 64  .** has been mod
177bf 69 66 69 65 64 2c 20 22 74 72 69 70 22 20 6f 72  ified, "trip" or
177c0 20 69 6e 76 61 6c 69 64 61 74 65 20 65 61 63 68   invalidate each
177c1 20 63 75 72 73 6f 72 20 69 6e 0a 2a 2a 20 74 68   cursor in.** th
177c2 61 74 20 42 74 72 65 65 20 6d 69 67 68 74 20 68  at Btree might h
177c3 61 76 65 20 62 65 65 6e 20 6d 6f 64 69 66 69 65  ave been modifie
177c4 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 63 75  d so that the cu
177c5 72 73 6f 72 0a 2a 2a 20 63 61 6e 20 6e 65 76 65  rsor.** can neve
177c6 72 20 62 65 20 75 73 65 64 20 61 67 61 69 6e 2e  r be used again.
177c7 20 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 77    This happens w
177c8 68 65 6e 20 61 20 72 6f 6c 6c 62 61 63 6b 0a 2a  hen a rollback.*
177c9 2a 2a 20 6f 63 63 75 72 73 2e 20 20 57 65 20 68  ** occurs.  We h
177ca 61 76 65 20 74 6f 20 74 72 69 70 20 61 6c 6c 20  ave to trip all 
177cb 74 68 65 20 6f 74 68 65 72 20 63 75 72 73 6f 72  the other cursor
177cc 73 2c 20 65 76 65 6e 0a 2a 2a 20 63 75 72 73 6f  s, even.** curso
177cd 72 20 66 72 6f 6d 20 6f 74 68 65 72 20 56 4d 73  r from other VMs
177ce 20 69 6e 20 64 69 66 66 65 72 65 6e 74 20 64 61   in different da
177cf 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
177d0 6e 73 2c 0a 2a 2a 20 73 6f 20 74 68 61 74 20 6e  ns,.** so that n
177d1 6f 6e 65 20 6f 66 20 74 68 65 6d 20 74 72 79 20  one of them try 
177d2 74 6f 20 75 73 65 20 74 68 65 20 64 61 74 61 20  to use the data 
177d3 61 74 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a  at which they.**
177d4 20 77 65 72 65 20 70 6f 69 6e 74 69 6e 67 20 61   were pointing a
177d5 6e 64 20 77 68 69 63 68 20 6e 6f 77 20 6d 61 79  nd which now may
177d6 20 68 61 76 65 20 62 65 65 6e 20 63 68 61 6e 67   have been chang
177d7 65 64 20 64 75 65 0a 2a 2a 20 74 6f 20 74 68 65  ed due.** to the
177d8 20 72 6f 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a   rollback..**.**
177d9 20 52 65 6d 65 6d 62 65 72 20 74 68 61 74 20 61   Remember that a
177da 20 72 6f 6c 6c 62 61 63 6b 20 63 61 6e 20 64 65   rollback can de
177db 6c 65 74 65 20 74 61 62 6c 65 73 20 63 6f 6d 70  lete tables comp
177dc 6c 65 74 65 20 61 6e 64 0a 2a 2a 20 72 65 6f 72  lete and.** reor
177dd 64 65 72 20 72 6f 6f 74 70 61 67 65 73 2e 20 20  der rootpages.  
177de 53 6f 20 69 74 20 69 73 20 6e 6f 74 20 73 75 66  So it is not suf
177df 66 69 63 69 65 6e 74 20 6a 75 73 74 20 74 6f 20  ficient just to 
177e0 73 61 76 65 0a 2a 2a 20 74 68 65 20 73 74 61 74  save.** the stat
177e1 65 20 6f 66 20 74 68 65 20 63 75 72 73 6f 72 2e  e of the cursor.
177e2 20 20 57 65 20 68 61 76 65 20 74 6f 20 69 6e 76    We have to inv
177e3 61 6c 69 64 61 74 65 20 74 68 65 20 63 75 72 73  alidate the curs
177e4 6f 72 0a 2a 2a 20 73 6f 20 74 68 61 74 20 69 74  or.** so that it
177e5 20 69 73 20 6e 65 76 65 72 20 75 73 65 64 20 61   is never used a
177e6 67 61 69 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  gain..*/.static 
177e7 76 6f 69 64 20 69 6e 76 61 6c 69 64 61 74 65 43  void invalidateC
177e8 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69 65 64  ursorsOnModified
177e9 42 74 72 65 65 73 28 73 71 6c 69 74 65 33 20 2a  Btrees(sqlite3 *
177ea 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  db){.  int i;.  
177eb 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
177ec 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 42 74  Db; i++){.    Bt
177ed 72 65 65 20 2a 70 20 3d 20 64 62 2d 3e 61 44 62  ree *p = db->aDb
177ee 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28  [i].pBt;.    if(
177ef 20 70 20 26 26 20 73 71 6c 69 74 65 33 42 74 72   p && sqlite3Btr
177f0 65 65 49 73 49 6e 54 72 61 6e 73 28 70 29 20 29  eeIsInTrans(p) )
177f1 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42  {.      sqlite3B
177f2 74 72 65 65 54 72 69 70 41 6c 6c 43 75 72 73 6f  treeTripAllCurso
177f3 72 73 28 70 2c 20 53 51 4c 49 54 45 5f 41 42 4f  rs(p, SQLITE_ABO
177f4 52 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  RT);.    }.  }.}
177f5 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
177f6 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
177f7 68 65 20 77 68 65 6e 20 61 20 56 44 42 45 20 74  he when a VDBE t
177f8 72 69 65 73 20 74 6f 20 68 61 6c 74 2e 20 20 49  ries to halt.  I
177f9 66 20 74 68 65 20 56 44 42 45 0a 2a 2a 20 68 61  f the VDBE.** ha
177fa 73 20 6d 61 64 65 20 63 68 61 6e 67 65 73 20 61  s made changes a
177fb 6e 64 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d  nd is in autocom
177fc 6d 69 74 20 6d 6f 64 65 2c 20 74 68 65 6e 20 63  mit mode, then c
177fd 6f 6d 6d 69 74 20 74 68 6f 73 65 0a 2a 2a 20 63  ommit those.** c
177fe 68 61 6e 67 65 73 2e 20 20 49 66 20 61 20 72 6f  hanges.  If a ro
177ff 6c 6c 62 61 63 6b 20 69 73 20 6e 65 65 64 65 64  llback is needed
17800 2c 20 74 68 65 6e 20 64 6f 20 74 68 65 20 72 6f  , then do the ro
17801 6c 6c 62 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  llback..**.** Th
17802 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20 74 68  is routine is th
17803 65 20 6f 6e 6c 79 20 77 61 79 20 74 6f 20 6d 6f  e only way to mo
17804 76 65 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ve the state of 
17805 61 20 56 4d 20 66 72 6f 6d 0a 2a 2a 20 53 51 4c  a VM from.** SQL
17806 49 54 45 5f 4d 41 47 49 43 5f 52 55 4e 20 74 6f  ITE_MAGIC_RUN to
17807 20 53 51 4c 49 54 45 5f 4d 41 47 49 43 5f 48 41   SQLITE_MAGIC_HA
17808 4c 54 2e 20 20 49 74 20 69 73 20 68 61 72 6d 6c  LT.  It is harml
17809 65 73 73 20 74 6f 0a 2a 2a 20 63 61 6c 6c 20 74  ess to.** call t
1780a 68 69 73 20 6f 6e 20 61 20 56 4d 20 74 68 61 74  his on a VM that
1780b 20 69 73 20 69 6e 20 74 68 65 20 53 51 4c 49 54   is in the SQLIT
1780c 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 73 74 61  E_MAGIC_HALT sta
1780d 74 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  te..**.** Return
1780e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 2e 20   an error code. 
1780f 20 49 66 20 74 68 65 20 63 6f 6d 6d 69 74 20 63   If the commit c
17810 6f 75 6c 64 20 6e 6f 74 20 63 6f 6d 70 6c 65 74  ould not complet
17811 65 20 62 65 63 61 75 73 65 20 6f 66 0a 2a 2a 20  e because of.** 
17812 6c 6f 63 6b 20 63 6f 6e 74 65 6e 74 69 6f 6e 2c  lock contention,
17813 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 42   return SQLITE_B
17814 55 53 59 2e 20 20 49 66 20 53 51 4c 49 54 45 5f  USY.  If SQLITE_
17815 42 55 53 59 20 69 73 20 72 65 74 75 72 6e 65 64  BUSY is returned
17816 2c 20 69 74 0a 2a 2a 20 6d 65 61 6e 73 20 74 68  , it.** means th
17817 65 20 63 6c 6f 73 65 20 64 69 64 20 6e 6f 74 20  e close did not 
17818 68 61 70 70 65 6e 20 61 6e 64 20 6e 65 65 64 73  happen and needs
17819 20 74 6f 20 62 65 20 72 65 70 65 61 74 65 64 2e   to be repeated.
1781a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1781b 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64  TE int sqlite3Vd
1781c 62 65 48 61 6c 74 28 56 64 62 65 20 2a 70 29 7b  beHalt(Vdbe *p){
1781d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1781e 20 70 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 3b   p->db;.  int i;
1781f 0a 20 20 69 6e 74 20 28 2a 78 46 75 6e 63 29 28  .  int (*xFunc)(
17820 42 74 72 65 65 20 2a 70 42 74 29 20 3d 20 30 3b  Btree *pBt) = 0;
17821 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f    /* Function to
17822 20 63 61 6c 6c 20 6f 6e 20 65 61 63 68 20 62 74   call on each bt
17823 72 65 65 20 62 61 63 6b 65 6e 64 20 2a 2f 0a 20  ree backend */. 
17824 20 69 6e 74 20 69 73 53 70 65 63 69 61 6c 45 72   int isSpecialEr
17825 72 6f 72 3b 20 20 20 20 20 20 20 20 20 20 20 20  ror;            
17826 2f 2a 20 53 65 74 20 74 6f 20 74 72 75 65 20 69  /* Set to true i
17827 66 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 6f  f SQLITE_NOMEM o
17828 72 20 49 4f 45 52 52 20 2a 2f 0a 0a 20 20 2f 2a  r IOERR */..  /*
17829 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 63   This function c
1782a 6f 6e 74 61 69 6e 73 20 74 68 65 20 6c 6f 67 69  ontains the logi
1782b 63 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65  c that determine
1782c 73 20 69 66 20 61 20 73 74 61 74 65 6d 65 6e 74  s if a statement
1782d 20 6f 72 0a 20 20 2a 2a 20 74 72 61 6e 73 61 63   or.  ** transac
1782e 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20 63 6f 6d  tion will be com
1782f 6d 69 74 74 65 64 20 6f 72 20 72 6f 6c 6c 65 64  mitted or rolled
17830 20 62 61 63 6b 20 61 73 20 61 20 72 65 73 75 6c   back as a resul
17831 74 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 65 78  t of the.  ** ex
17832 65 63 75 74 69 6f 6e 20 6f 66 20 74 68 69 73 20  ecution of this 
17833 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e  virtual machine.
17834 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 61   .  **.  ** If a
17835 6e 79 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ny of the follow
17836 69 6e 67 20 65 72 72 6f 72 73 20 6f 63 63 75 72  ing errors occur
17837 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  :.  **.  **     
17838 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 0a 20 20 2a  SQLITE_NOMEM.  *
17839 2a 20 20 20 20 20 53 51 4c 49 54 45 5f 49 4f 45  *     SQLITE_IOE
1783a 52 52 0a 20 20 2a 2a 20 20 20 20 20 53 51 4c 49  RR.  **     SQLI
1783b 54 45 5f 46 55 4c 4c 0a 20 20 2a 2a 20 20 20 20  TE_FULL.  **    
1783c 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
1783d 54 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 6e  T.  **.  ** Then
1783e 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 63 61   the internal ca
1783f 63 68 65 20 6d 69 67 68 74 20 68 61 76 65 20 62  che might have b
17840 65 65 6e 20 6c 65 66 74 20 69 6e 20 61 6e 20 69  een left in an i
17841 6e 63 6f 6e 73 69 73 74 65 6e 74 0a 20 20 2a 2a  nconsistent.  **
17842 20 73 74 61 74 65 2e 20 20 57 65 20 6e 65 65 64   state.  We need
17843 20 74 6f 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65   to rollback the
17844 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
17845 61 63 74 69 6f 6e 2c 20 69 66 20 74 68 65 72 65  action, if there
17846 20 69 73 0a 20 20 2a 2a 20 6f 6e 65 2c 20 6f 72   is.  ** one, or
17847 20 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 72   the complete tr
17848 61 6e 73 61 63 74 69 6f 6e 20 69 66 20 74 68 65  ansaction if the
17849 72 65 20 69 73 20 6e 6f 20 73 74 61 74 65 6d 65  re is no stateme
1784a 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a  nt transaction..
1784b 20 20 2a 2f 0a 0a 20 20 69 66 28 20 70 2d 3e 64    */..  if( p->d
1784c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1784d 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d 20 53  ){.    p->rc = S
1784e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
1784f 0a 20 20 63 6c 6f 73 65 41 6c 6c 43 75 72 73 6f  .  closeAllCurso
17850 72 73 45 78 63 65 70 74 41 63 74 69 76 65 56 74  rsExceptActiveVt
17851 61 62 73 28 70 29 3b 0a 20 20 69 66 28 20 70 2d  abs(p);.  if( p-
17852 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f 4d 41 47  >magic!=VDBE_MAG
17853 49 43 5f 52 55 4e 20 29 7b 0a 20 20 20 20 72 65  IC_RUN ){.    re
17854 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
17855 20 20 7d 0a 20 20 63 68 65 63 6b 41 63 74 69 76    }.  checkActiv
17856 65 56 64 62 65 43 6e 74 28 64 62 29 3b 0a 0a 20  eVdbeCnt(db);.. 
17857 20 2f 2a 20 4e 6f 20 63 6f 6d 6d 69 74 20 6f 72   /* No commit or
17858 20 72 6f 6c 6c 62 61 63 6b 20 6e 65 65 64 65 64   rollback needed
17859 20 69 66 20 74 68 65 20 70 72 6f 67 72 61 6d 20   if the program 
1785a 6e 65 76 65 72 20 73 74 61 72 74 65 64 20 2a 2f  never started */
1785b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30 20  .  if( p->pc>=0 
1785c 29 7b 0a 20 20 20 20 69 6e 74 20 6d 72 63 3b 20  ){.    int mrc; 
1785d 20 20 2f 2a 20 50 72 69 6d 61 72 79 20 65 72 72    /* Primary err
1785e 6f 72 20 63 6f 64 65 20 66 72 6f 6d 20 70 2d 3e  or code from p->
1785f 72 63 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 4c 6f  rc */..    /* Lo
17860 63 6b 20 61 6c 6c 20 62 74 72 65 65 73 20 75 73  ck all btrees us
17861 65 64 20 62 79 20 74 68 65 20 73 74 61 74 65 6d  ed by the statem
17862 65 6e 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74  ent */.    sqlit
17863 65 33 42 74 72 65 65 4d 75 74 65 78 41 72 72 61  e3BtreeMutexArra
17864 79 45 6e 74 65 72 28 26 70 2d 3e 61 4d 75 74 65  yEnter(&p->aMute
17865 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63  x);..    /* Chec
17866 6b 20 66 6f 72 20 6f 6e 65 20 6f 66 20 74 68 65  k for one of the
17867 20 73 70 65 63 69 61 6c 20 65 72 72 6f 72 73 20   special errors 
17868 2a 2f 0a 20 20 20 20 6d 72 63 20 3d 20 70 2d 3e  */.    mrc = p->
17869 72 63 20 26 20 30 78 66 66 3b 0a 20 20 20 20 69  rc & 0xff;.    i
1786a 73 53 70 65 63 69 61 6c 45 72 72 6f 72 20 3d 20  sSpecialError = 
1786b 6d 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45  mrc==SQLITE_NOME
1786c 4d 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c 49 54 45  M || mrc==SQLITE
1786d 5f 49 4f 45 52 52 0a 20 20 20 20 20 20 20 20 20  _IOERR.         
1786e 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 6d              || m
1786f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4e 54 45 52  rc==SQLITE_INTER
17870 52 55 50 54 20 7c 7c 20 6d 72 63 3d 3d 53 51 4c  RUPT || mrc==SQL
17871 49 54 45 5f 46 55 4c 4c 3b 0a 20 20 20 20 69 66  ITE_FULL;.    if
17872 28 20 69 73 53 70 65 63 69 61 6c 45 72 72 6f 72  ( isSpecialError
17873 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20   ){.      /* If 
17874 74 68 65 20 71 75 65 72 79 20 77 61 73 20 72 65  the query was re
17875 61 64 2d 6f 6e 6c 79 2c 20 77 65 20 6e 65 65 64  ad-only, we need
17876 20 64 6f 20 6e 6f 20 72 6f 6c 6c 62 61 63 6b 20   do no rollback 
17877 61 74 20 61 6c 6c 2e 20 4f 74 68 65 72 77 69 73  at all. Otherwis
17878 65 2c 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 63  e,.      ** proc
17879 65 65 64 20 77 69 74 68 20 74 68 65 20 73 70 65  eed with the spe
1787a 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a 20  cial handling.. 
1787b 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66       */.      if
1787c 28 20 21 70 2d 3e 72 65 61 64 4f 6e 6c 79 20 7c  ( !p->readOnly |
1787d 7c 20 6d 72 63 21 3d 53 51 4c 49 54 45 5f 49 4e  | mrc!=SQLITE_IN
1787e 54 45 52 52 55 50 54 20 29 7b 0a 20 20 20 20 20  TERRUPT ){.     
1787f 20 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51     if( p->rc==SQ
17880 4c 49 54 45 5f 49 4f 45 52 52 5f 42 4c 4f 43 4b  LITE_IOERR_BLOCK
17881 45 44 20 26 26 20 70 2d 3e 75 73 65 73 53 74 6d  ED && p->usesStm
17882 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20 20  tJournal ){.    
17883 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73 71        xFunc = sq
17884 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62 61  lite3BtreeRollba
17885 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20 20  ckStmt;.        
17886 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45    p->rc = SQLITE
17887 5f 42 55 53 59 3b 0a 20 20 20 20 20 20 20 20 7d  _BUSY;.        }
17888 65 6c 73 65 20 69 66 28 20 28 6d 72 63 3d 3d 53  else if( (mrc==S
17889 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 6d  QLITE_NOMEM || m
1788a 72 63 3d 3d 53 51 4c 49 54 45 5f 46 55 4c 4c 29  rc==SQLITE_FULL)
1788b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1788c 20 20 20 20 26 26 20 70 2d 3e 75 73 65 73 53 74      && p->usesSt
1788d 6d 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  mtJournal ){.   
1788e 20 20 20 20 20 20 20 78 46 75 6e 63 20 3d 20 73         xFunc = s
1788f 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c 62  qlite3BtreeRollb
17890 61 63 6b 53 74 6d 74 3b 0a 20 20 20 20 20 20 20  ackStmt;.       
17891 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
17892 20 20 2f 2a 20 57 65 20 61 72 65 20 66 6f 72 63    /* We are forc
17893 65 64 20 74 6f 20 72 6f 6c 6c 20 62 61 63 6b 20  ed to roll back 
17894 74 68 65 20 61 63 74 69 76 65 20 74 72 61 6e 73  the active trans
17895 61 63 74 69 6f 6e 2e 20 42 65 66 6f 72 65 20 64  action. Before d
17896 6f 69 6e 67 0a 20 20 20 20 20 20 20 20 20 20 2a  oing.          *
17897 2a 20 73 6f 2c 20 61 62 6f 72 74 20 61 6e 79 20  * so, abort any 
17898 6f 74 68 65 72 20 73 74 61 74 65 6d 65 6e 74 73  other statements
17899 20 74 68 69 73 20 68 61 6e 64 6c 65 20 63 75 72   this handle cur
1789a 72 65 6e 74 6c 79 20 68 61 73 20 61 63 74 69 76  rently has activ
1789b 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
1789c 20 20 20 20 20 20 20 20 20 20 69 6e 76 61 6c 69            invali
1789d 64 61 74 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64  dateCursorsOnMod
1789e 69 66 69 65 64 42 74 72 65 65 73 28 64 62 29 3b  ifiedBtrees(db);
1789f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
178a0 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c 28 64 62  e3RollbackAll(db
178a1 29 3b 0a 20 20 20 20 20 20 20 20 20 20 64 62 2d  );.          db-
178a2 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b  >autoCommit = 1;
178a3 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
178a4 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20   }.    }.  .    
178a5 2f 2a 20 49 66 20 74 68 65 20 61 75 74 6f 2d 63  /* If the auto-c
178a6 6f 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 73 65  ommit flag is se
178a7 74 20 61 6e 64 20 74 68 69 73 20 69 73 20 74 68  t and this is th
178a8 65 20 6f 6e 6c 79 20 61 63 74 69 76 65 20 76 64  e only active vd
178a9 62 65 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20  be, then.    ** 
178aa 77 65 20 64 6f 20 65 69 74 68 65 72 20 61 20 63  we do either a c
178ab 6f 6d 6d 69 74 20 6f 72 20 72 6f 6c 6c 62 61 63  ommit or rollbac
178ac 6b 20 6f 66 20 74 68 65 20 63 75 72 72 65 6e 74  k of the current
178ad 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 0a 20   transaction. . 
178ae 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74     **.    ** Not
178af 65 3a 20 54 68 69 73 20 62 6c 6f 63 6b 20 61 6c  e: This block al
178b0 73 6f 20 72 75 6e 73 20 69 66 20 6f 6e 65 20 6f  so runs if one o
178b1 66 20 74 68 65 20 73 70 65 63 69 61 6c 20 65 72  f the special er
178b2 72 6f 72 73 20 68 61 6e 64 6c 65 64 20 0a 20 20  rors handled .  
178b3 20 20 2a 2a 20 61 62 6f 76 65 20 68 61 73 20 6f    ** above has o
178b4 63 63 75 72 72 65 64 2e 20 0a 20 20 20 20 2a 2f  ccurred. .    */
178b5 0a 20 20 20 20 69 66 28 20 21 73 71 6c 69 74 65  .    if( !sqlite
178b6 33 56 74 61 62 49 6e 53 79 6e 63 28 64 62 29 20  3VtabInSync(db) 
178b7 0a 20 20 20 20 20 26 26 20 64 62 2d 3e 61 75 74  .     && db->aut
178b8 6f 43 6f 6d 6d 69 74 20 0a 20 20 20 20 20 26 26  oCommit .     &&
178b9 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e   db->writeVdbeCn
178ba 74 3d 3d 28 70 2d 3e 72 65 61 64 4f 6e 6c 79 3d  t==(p->readOnly=
178bb 3d 30 29 20 0a 20 20 20 20 29 7b 0a 20 20 20 20  =0) .    ){.    
178bc 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c    if( p->rc==SQL
178bd 49 54 45 5f 4f 4b 20 7c 7c 20 28 70 2d 3e 65 72  ITE_OK || (p->er
178be 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61  rorAction==OE_Fa
178bf 69 6c 20 26 26 20 21 69 73 53 70 65 63 69 61 6c  il && !isSpecial
178c0 45 72 72 6f 72 29 20 29 7b 0a 20 20 20 20 20 20  Error) ){.      
178c1 20 20 2f 2a 20 54 68 65 20 61 75 74 6f 2d 63 6f    /* The auto-co
178c2 6d 6d 69 74 20 66 6c 61 67 20 69 73 20 74 72 75  mmit flag is tru
178c3 65 2c 20 61 6e 64 20 74 68 65 20 76 64 62 65 20  e, and the vdbe 
178c4 70 72 6f 67 72 61 6d 20 77 61 73 20 0a 20 20 20  program was .   
178c5 20 20 20 20 20 2a 2a 20 73 75 63 63 65 73 73 66       ** successf
178c6 75 6c 20 6f 72 20 68 69 74 20 61 6e 20 27 4f 52  ul or hit an 'OR
178c7 20 46 41 49 4c 27 20 63 6f 6e 73 74 72 61 69 6e   FAIL' constrain
178c8 74 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 61 20  t. This means a 
178c9 63 6f 6d 6d 69 74 20 0a 20 20 20 20 20 20 20 20  commit .        
178ca 2a 2a 20 69 73 20 72 65 71 75 69 72 65 64 2e 0a  ** is required..
178cb 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
178cc 20 20 20 69 6e 74 20 72 63 20 3d 20 76 64 62 65     int rc = vdbe
178cd 43 6f 6d 6d 69 74 28 64 62 2c 20 70 29 3b 0a 20  Commit(db, p);. 
178ce 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
178cf 51 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20  QLITE_BUSY ){.  
178d0 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
178d1 74 72 65 65 4d 75 74 65 78 41 72 72 61 79 4c 65  treeMutexArrayLe
178d2 61 76 65 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b  ave(&p->aMutex);
178d3 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
178d4 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20  n SQLITE_BUSY;. 
178d5 20 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28         }else if(
178d6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
178d7 7b 0a 20 20 20 20 20 20 20 20 20 20 70 2d 3e 72  {.          p->r
178d8 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 20 20  c = rc;.        
178d9 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61 63    sqlite3Rollbac
178da 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20 20  kAll(db);.      
178db 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
178dc 20 20 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74     sqlite3Commit
178dd 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28  InternalChanges(
178de 64 62 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  db);.        }. 
178df 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
178e0 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62      sqlite3Rollb
178e1 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  ackAll(db);.    
178e2 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66    }.    }else if
178e3 28 20 21 78 46 75 6e 63 20 29 7b 0a 20 20 20 20  ( !xFunc ){.    
178e4 20 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c    if( p->rc==SQL
178e5 49 54 45 5f 4f 4b 20 7c 7c 20 70 2d 3e 65 72 72  ITE_OK || p->err
178e6 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 46 61 69  orAction==OE_Fai
178e7 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  l ){.        if(
178e8 20 70 2d 3e 6f 70 65 6e 65 64 53 74 61 74 65 6d   p->openedStatem
178e9 65 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ent ){.         
178ea 20 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33   xFunc = sqlite3
178eb 42 74 72 65 65 43 6f 6d 6d 69 74 53 74 6d 74 3b  BtreeCommitStmt;
178ec 0a 20 20 20 20 20 20 20 20 7d 20 0a 20 20 20 20  .        } .    
178ed 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 65    }else if( p->e
178ee 72 72 6f 72 41 63 74 69 6f 6e 3d 3d 4f 45 5f 41  rrorAction==OE_A
178ef 62 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20  bort ){.        
178f0 78 46 75 6e 63 20 3d 20 73 71 6c 69 74 65 33 42  xFunc = sqlite3B
178f1 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74  treeRollbackStmt
178f2 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
178f3 20 20 20 20 20 20 20 69 6e 76 61 6c 69 64 61 74         invalidat
178f4 65 43 75 72 73 6f 72 73 4f 6e 4d 6f 64 69 66 69  eCursorsOnModifi
178f5 65 64 42 74 72 65 65 73 28 64 62 29 3b 0a 20 20  edBtrees(db);.  
178f6 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6c        sqlite3Rol
178f7 6c 62 61 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20  lbackAll(db);.  
178f8 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
178f9 6d 6d 69 74 20 3d 20 31 3b 0a 20 20 20 20 20 20  mmit = 1;.      
178fa 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  }.    }.  .    /
178fb 2a 20 49 66 20 78 46 75 6e 63 20 69 73 20 6e 6f  * If xFunc is no
178fc 74 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 69 74 20  t NULL, then it 
178fd 69 73 20 6f 6e 65 20 6f 66 20 73 71 6c 69 74 65  is one of sqlite
178fe 33 42 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74  3BtreeRollbackSt
178ff 6d 74 20 6f 72 0a 20 20 20 20 2a 2a 20 73 71 6c  mt or.    ** sql
17900 69 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53  ite3BtreeCommitS
17901 74 6d 74 2e 20 43 61 6c 6c 20 69 74 20 6f 6e 63  tmt. Call it onc
17902 65 20 6f 6e 20 65 61 63 68 20 62 61 63 6b 65 6e  e on each backen
17903 64 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f  d. If an error o
17904 63 63 75 72 73 0a 20 20 20 20 2a 2a 20 61 6e 64  ccurs.    ** and
17905 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f 64 65   the return code
17906 20 69 73 20 73 74 69 6c 6c 20 53 51 4c 49 54 45   is still SQLITE
17907 5f 4f 4b 2c 20 73 65 74 20 74 68 65 20 72 65 74  _OK, set the ret
17908 75 72 6e 20 63 6f 64 65 20 74 6f 20 74 68 65 20  urn code to the 
17909 6e 65 77 0a 20 20 20 20 2a 2a 20 65 72 72 6f 72  new.    ** error
1790a 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a 2f 0a 20   value..    */. 
1790b 20 20 20 61 73 73 65 72 74 28 21 78 46 75 6e 63     assert(!xFunc
1790c 20 7c 7c 0a 20 20 20 20 20 20 78 46 75 6e 63 3d   ||.      xFunc=
1790d 3d 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 6d  =sqlite3BtreeCom
1790e 6d 69 74 53 74 6d 74 20 7c 7c 0a 20 20 20 20 20  mitStmt ||.     
1790f 20 78 46 75 6e 63 3d 3d 73 71 6c 69 74 65 33 42   xFunc==sqlite3B
17910 74 72 65 65 52 6f 6c 6c 62 61 63 6b 53 74 6d 74  treeRollbackStmt
17911 0a 20 20 20 20 29 3b 0a 20 20 20 20 66 6f 72 28  .    );.    for(
17912 69 3d 30 3b 20 78 46 75 6e 63 20 26 26 20 69 3c  i=0; xFunc && i<
17913 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 20 0a  db->nDb; i++){ .
17914 20 20 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20        int rc;.  
17915 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20 3d      Btree *pBt =
17916 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42 74 3b   db->aDb[i].pBt;
17917 0a 20 20 20 20 20 20 69 66 28 20 70 42 74 20 29  .      if( pBt )
17918 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 78  {.        rc = x
17919 46 75 6e 63 28 70 42 74 29 3b 0a 20 20 20 20 20  Func(pBt);.     
1791a 20 20 20 69 66 28 20 72 63 20 26 26 20 28 70 2d     if( rc && (p-
1791b 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c  >rc==SQLITE_OK |
1791c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  | p->rc==SQLITE_
1791d 43 4f 4e 53 54 52 41 49 4e 54 29 20 29 7b 0a 20  CONSTRAINT) ){. 
1791e 20 20 20 20 20 20 20 20 20 70 2d 3e 72 63 20 3d           p->rc =
1791f 20 72 63 3b 0a 20 20 20 20 20 20 20 20 20 20 73   rc;.          s
17920 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
17921 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
17922 20 20 20 20 20 20 20 20 70 2d 3e 7a 45 72 72 4d          p->zErrM
17923 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  sg = 0;.        
17924 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
17925 20 20 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69    .    /* If thi
17926 73 20 77 61 73 20 61 6e 20 49 4e 53 45 52 54 2c  s was an INSERT,
17927 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c 45 54   UPDATE or DELET
17928 45 20 61 6e 64 20 74 68 65 20 73 74 61 74 65 6d  E and the statem
17929 65 6e 74 20 77 61 73 20 63 6f 6d 6d 69 74 74 65  ent was committe
1792a 64 2c 20 0a 20 20 20 20 2a 2a 20 73 65 74 20 74  d, .    ** set t
1792b 68 65 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65  he change counte
1792c 72 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  r. .    */.    i
1792d 66 28 20 70 2d 3e 63 68 61 6e 67 65 43 6e 74 4f  f( p->changeCntO
1792e 6e 20 26 26 20 70 2d 3e 70 63 3e 3d 30 20 29 7b  n && p->pc>=0 ){
1792f 0a 20 20 20 20 20 20 69 66 28 20 21 78 46 75 6e  .      if( !xFun
17930 63 20 7c 7c 20 78 46 75 6e 63 3d 3d 73 71 6c 69  c || xFunc==sqli
17931 74 65 33 42 74 72 65 65 43 6f 6d 6d 69 74 53 74  te3BtreeCommitSt
17932 6d 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  mt ){.        sq
17933 6c 69 74 65 33 56 64 62 65 53 65 74 43 68 61 6e  lite3VdbeSetChan
17934 67 65 73 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e  ges(db, p->nChan
17935 67 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  ge);.      }else
17936 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
17937 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73 28  3VdbeSetChanges(
17938 64 62 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  db, 0);.      }.
17939 20 20 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65        p->nChange
1793a 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20   = 0;.    }.  . 
1793b 20 20 20 2f 2a 20 52 6f 6c 6c 62 61 63 6b 20 6f     /* Rollback o
1793c 72 20 63 6f 6d 6d 69 74 20 61 6e 79 20 73 63 68  r commit any sch
1793d 65 6d 61 20 63 68 61 6e 67 65 73 20 74 68 61 74  ema changes that
1793e 20 6f 63 63 75 72 72 65 64 2e 20 2a 2f 0a 20 20   occurred. */.  
1793f 20 20 69 66 28 20 70 2d 3e 72 63 21 3d 53 51 4c    if( p->rc!=SQL
17940 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d 3e 66 6c  ITE_OK && db->fl
17941 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74 65 72  ags&SQLITE_Inter
17942 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20 20 20  nChanges ){.    
17943 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
17944 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
17945 20 30 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e 66   0);.      db->f
17946 6c 61 67 73 20 3d 20 28 64 62 2d 3e 66 6c 61 67  lags = (db->flag
17947 73 20 7c 20 53 51 4c 49 54 45 5f 49 6e 74 65 72  s | SQLITE_Inter
17948 6e 43 68 61 6e 67 65 73 29 3b 0a 20 20 20 20 7d  nChanges);.    }
17949 0a 0a 20 20 20 20 2f 2a 20 52 65 6c 65 61 73 65  ..    /* Release
1794a 20 74 68 65 20 6c 6f 63 6b 73 20 2a 2f 0a 20 20   the locks */.  
1794b 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75    sqlite3BtreeMu
1794c 74 65 78 41 72 72 61 79 4c 65 61 76 65 28 26 70  texArrayLeave(&p
1794d 2d 3e 61 4d 75 74 65 78 29 3b 0a 20 20 7d 0a 0a  ->aMutex);.  }..
1794e 20 20 2f 2a 20 57 65 20 68 61 76 65 20 73 75 63    /* We have suc
1794f 63 65 73 73 66 75 6c 6c 79 20 68 61 6c 74 65 64  cessfully halted
17950 20 61 6e 64 20 63 6c 6f 73 65 64 20 74 68 65 20   and closed the 
17951 56 4d 2e 20 20 52 65 63 6f 72 64 20 74 68 69 73  VM.  Record this
17952 20 66 61 63 74 2e 20 2a 2f 0a 20 20 69 66 28 20   fact. */.  if( 
17953 70 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20  p->pc>=0 ){.    
17954 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
17955 74 2d 2d 3b 0a 20 20 20 20 69 66 28 20 21 70 2d  t--;.    if( !p-
17956 3e 72 65 61 64 4f 6e 6c 79 20 29 7b 0a 20 20 20  >readOnly ){.   
17957 20 20 20 64 62 2d 3e 77 72 69 74 65 56 64 62 65     db->writeVdbe
17958 43 6e 74 2d 2d 3b 0a 20 20 20 20 7d 0a 20 20 20  Cnt--;.    }.   
17959 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 63 74   assert( db->act
1795a 69 76 65 56 64 62 65 43 6e 74 3e 3d 64 62 2d 3e  iveVdbeCnt>=db->
1795b 77 72 69 74 65 56 64 62 65 43 6e 74 20 29 3b 0a  writeVdbeCnt );.
1795c 20 20 7d 0a 20 20 70 2d 3e 6d 61 67 69 63 20 3d    }.  p->magic =
1795d 20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54   VDBE_MAGIC_HALT
1795e 3b 0a 20 20 63 68 65 63 6b 41 63 74 69 76 65 56  ;.  checkActiveV
1795f 64 62 65 43 6e 74 28 64 62 29 3b 0a 20 20 69 66  dbeCnt(db);.  if
17960 28 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  ( p->db->mallocF
17961 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 70 2d 3e  ailed ){.    p->
17962 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
17963 4d 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  M;.  }..  return
17964 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a   SQLITE_OK;.}...
17965 2f 2a 0a 2a 2a 20 45 61 63 68 20 56 44 42 45 20  /*.** Each VDBE 
17966 68 6f 6c 64 73 20 74 68 65 20 72 65 73 75 6c 74  holds the result
17967 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63   of the most rec
17968 65 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70  ent sqlite3_step
17969 28 29 20 63 61 6c 6c 0a 2a 2a 20 69 6e 20 70 2d  () call.** in p-
1796a 3e 72 63 2e 20 20 54 68 69 73 20 72 6f 75 74 69  >rc.  This routi
1796b 6e 65 20 73 65 74 73 20 74 68 61 74 20 72 65 73  ne sets that res
1796c 75 6c 74 20 62 61 63 6b 20 74 6f 20 53 51 4c 49  ult back to SQLI
1796d 54 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45  TE_OK..*/.SQLITE
1796e 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1796f 6c 69 74 65 33 56 64 62 65 52 65 73 65 74 53 74  lite3VdbeResetSt
17970 65 70 52 65 73 75 6c 74 28 56 64 62 65 20 2a 70  epResult(Vdbe *p
17971 29 7b 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  ){.  p->rc = SQL
17972 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
17973 20 43 6c 65 61 6e 20 75 70 20 61 20 56 44 42 45   Clean up a VDBE
17974 20 61 66 74 65 72 20 65 78 65 63 75 74 69 6f 6e   after execution
17975 20 62 75 74 20 64 6f 20 6e 6f 74 20 64 65 6c 65   but do not dele
17976 74 65 20 74 68 65 20 56 44 42 45 20 6a 75 73 74  te the VDBE just
17977 20 79 65 74 2e 0a 2a 2a 20 57 72 69 74 65 20 61   yet..** Write a
17978 6e 79 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ny error message
17979 73 20 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67  s into *pzErrMsg
1797a 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 72 65  .  Return the re
1797b 73 75 6c 74 20 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a  sult code..**.**
1797c 20 41 66 74 65 72 20 74 68 69 73 20 72 6f 75 74   After this rout
1797d 69 6e 65 20 69 73 20 72 75 6e 2c 20 74 68 65 20  ine is run, the 
1797e 56 44 42 45 20 73 68 6f 75 6c 64 20 62 65 20 72  VDBE should be r
1797f 65 61 64 79 20 74 6f 20 62 65 20 65 78 65 63 75  eady to be execu
17980 74 65 64 0a 2a 2a 20 61 67 61 69 6e 2e 0a 2a 2a  ted.** again..**
17981 0a 2a 2a 20 54 6f 20 6c 6f 6f 6b 20 61 74 20 69  .** To look at i
17982 74 20 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 74  t another way, t
17983 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 73 65  his routine rese
17984 74 73 20 74 68 65 20 73 74 61 74 65 20 6f 66 20  ts the state of 
17985 74 68 65 0a 2a 2a 20 76 69 72 74 75 61 6c 20 6d  the.** virtual m
17986 61 63 68 69 6e 65 20 66 72 6f 6d 20 56 44 42 45  achine from VDBE
17987 5f 4d 41 47 49 43 5f 52 55 4e 20 6f 72 20 56 44  _MAGIC_RUN or VD
17988 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20 62 61  BE_MAGIC_HALT ba
17989 63 6b 20 74 6f 0a 2a 2a 20 56 44 42 45 5f 4d 41  ck to.** VDBE_MA
1798a 47 49 43 5f 49 4e 49 54 2e 0a 2a 2f 0a 53 51 4c  GIC_INIT..*/.SQL
1798b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1798c 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 65 74  sqlite3VdbeReset
1798d 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
1798e 69 74 65 33 20 2a 64 62 3b 0a 20 20 64 62 20 3d  ite3 *db;.  db =
1798f 20 70 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 49 66   p->db;..  /* If
17990 20 74 68 65 20 56 4d 20 64 69 64 20 6e 6f 74 20   the VM did not 
17991 72 75 6e 20 74 6f 20 63 6f 6d 70 6c 65 74 69 6f  run to completio
17992 6e 20 6f 72 20 69 66 20 69 74 20 65 6e 63 6f 75  n or if it encou
17993 6e 74 65 72 65 64 20 61 6e 0a 20 20 2a 2a 20 65  ntered an.  ** e
17994 72 72 6f 72 2c 20 74 68 65 6e 20 69 74 20 6d 69  rror, then it mi
17995 67 68 74 20 6e 6f 74 20 68 61 76 65 20 62 65 65  ght not have bee
17996 6e 20 68 61 6c 74 65 64 20 70 72 6f 70 65 72 6c  n halted properl
17997 79 2e 20 20 53 6f 20 68 61 6c 74 0a 20 20 2a 2a  y.  So halt.  **
17998 20 69 74 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20   it now..  */.  
17999 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
1799a 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 73 71 6c  etyOn(db);.  sql
1799b 69 74 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b  ite3VdbeHalt(p);
1799c 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
1799d 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 0a  SafetyOff(db);..
1799e 20 20 2f 2a 20 49 66 20 74 68 65 20 56 44 42 45    /* If the VDBE
1799f 20 68 61 73 20 62 65 20 72 75 6e 20 65 76 65 6e   has be run even
179a0 20 70 61 72 74 69 61 6c 6c 79 2c 20 74 68 65 6e   partially, then
179a1 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 65 72   transfer the er
179a2 72 6f 72 20 63 6f 64 65 0a 20 20 2a 2a 20 61 6e  ror code.  ** an
179a3 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  d error message 
179a4 66 72 6f 6d 20 74 68 65 20 56 44 42 45 20 69 6e  from the VDBE in
179a5 74 6f 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  to the main data
179a6 62 61 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  base structure. 
179a7 20 42 75 74 0a 20 20 2a 2a 20 69 66 20 74 68 65   But.  ** if the
179a8 20 56 44 42 45 20 68 61 73 20 6a 75 73 74 20 62   VDBE has just b
179a9 65 65 6e 20 73 65 74 20 74 6f 20 72 75 6e 20 62  een set to run b
179aa 75 74 20 68 61 73 20 6e 6f 74 20 61 63 74 75 61  ut has not actua
179ab 6c 6c 79 20 65 78 65 63 75 74 65 64 20 61 6e 79  lly executed any
179ac 0a 20 20 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f  .  ** instructio
179ad 6e 73 20 79 65 74 2c 20 6c 65 61 76 65 20 74 68  ns yet, leave th
179ae 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
179af 65 72 72 6f 72 20 69 6e 66 6f 72 6d 61 74 69 6f  error informatio
179b0 6e 20 75 6e 63 68 61 6e 67 65 64 2e 0a 20 20 2a  n unchanged..  *
179b1 2f 0a 20 20 69 66 28 20 70 2d 3e 70 63 3e 3d 30  /.  if( p->pc>=0
179b2 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a   ){.    if( p->z
179b3 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20  ErrMsg ){.      
179b4 73 71 6c 69 74 65 33 42 65 67 69 6e 42 65 6e 69  sqlite3BeginBeni
179b5 67 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20  gnMalloc();.    
179b6 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
179b7 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 2d 31  tStr(db->pErr,-1
179b8 2c 70 2d 3e 7a 45 72 72 4d 73 67 2c 53 51 4c 49  ,p->zErrMsg,SQLI
179b9 54 45 5f 55 54 46 38 2c 53 51 4c 49 54 45 5f 54  TE_UTF8,SQLITE_T
179ba 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 20  RANSIENT);.     
179bb 20 73 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67   sqlite3EndBenig
179bc 6e 4d 61 6c 6c 6f 63 28 29 3b 0a 20 20 20 20 20  nMalloc();.     
179bd 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 70   db->errCode = p
179be 2d 3e 72 63 3b 0a 20 20 20 20 20 20 73 71 6c 69  ->rc;.      sqli
179bf 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d  te3DbFree(db, p-
179c0 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 20  >zErrMsg);.     
179c1 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b   p->zErrMsg = 0;
179c2 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
179c3 2d 3e 72 63 20 29 7b 0a 20 20 20 20 20 20 73 71  ->rc ){.      sq
179c4 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 70  lite3Error(db, p
179c5 2d 3e 72 63 2c 20 30 29 3b 0a 20 20 20 20 7d 65  ->rc, 0);.    }e
179c6 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
179c7 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
179c8 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 20 20 7d  TE_OK, 0);.    }
179c9 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e  .  }else if( p->
179ca 72 63 20 26 26 20 70 2d 3e 65 78 70 69 72 65 64  rc && p->expired
179cb 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 65   ){.    /* The e
179cc 78 70 69 72 65 64 20 66 6c 61 67 20 77 61 73 20  xpired flag was 
179cd 73 65 74 20 6f 6e 20 74 68 65 20 56 44 42 45 20  set on the VDBE 
179ce 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73 74  before the first
179cf 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f 20   call.    ** to 
179d0 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20  sqlite3_step(). 
179d1 46 6f 72 20 63 6f 6e 73 69 73 74 65 6e 63 79 20  For consistency 
179d2 28 73 69 6e 63 65 20 73 71 6c 69 74 65 33 5f 73  (since sqlite3_s
179d3 74 65 70 28 29 20 77 61 73 0a 20 20 20 20 2a 2a  tep() was.    **
179d4 20 63 61 6c 6c 65 64 29 2c 20 73 65 74 20 74 68   called), set th
179d5 65 20 64 61 74 61 62 61 73 65 20 65 72 72 6f 72  e database error
179d6 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 61 73   in this case as
179d7 20 77 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20   well..    */.  
179d8 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
179d9 62 2c 20 70 2d 3e 72 63 2c 20 30 29 3b 0a 20 20  b, p->rc, 0);.  
179da 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
179db 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d  tStr(db->pErr, -
179dc 31 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 53  1, p->zErrMsg, S
179dd 51 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49  QLITE_UTF8, SQLI
179de 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
179df 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
179e0 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
179e1 3b 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67  ;.    p->zErrMsg
179e2 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
179e3 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d 6f  Reclaim all memo
179e4 72 79 20 75 73 65 64 20 62 79 20 74 68 65 20 56  ry used by the V
179e5 44 42 45 0a 20 20 2a 2f 0a 20 20 43 6c 65 61 6e  DBE.  */.  Clean
179e6 75 70 28 70 29 3b 0a 0a 20 20 2f 2a 20 53 61 76  up(p);..  /* Sav
179e7 65 20 70 72 6f 66 69 6c 69 6e 67 20 69 6e 66 6f  e profiling info
179e8 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 69  rmation from thi
179e9 73 20 56 44 42 45 20 72 75 6e 2e 0a 20 20 2a 2f  s VDBE run..  */
179ea 0a 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f  .#ifdef VDBE_PRO
179eb 46 49 4c 45 0a 20 20 7b 0a 20 20 20 20 46 49 4c  FILE.  {.    FIL
179ec 45 20 2a 6f 75 74 20 3d 20 66 6f 70 65 6e 28 22  E *out = fopen("
179ed 76 64 62 65 5f 70 72 6f 66 69 6c 65 2e 6f 75 74  vdbe_profile.out
179ee 22 2c 20 22 61 22 29 3b 0a 20 20 20 20 69 66 28  ", "a");.    if(
179ef 20 6f 75 74 20 29 7b 0a 20 20 20 20 20 20 69 6e   out ){.      in
179f0 74 20 69 3b 0a 20 20 20 20 20 20 66 70 72 69 6e  t i;.      fprin
179f1 74 66 28 6f 75 74 2c 20 22 2d 2d 2d 2d 20 22 29  tf(out, "---- ")
179f2 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
179f3 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
179f4 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66  .        fprintf
179f5 28 6f 75 74 2c 20 22 25 30 32 78 22 2c 20 70 2d  (out, "%02x", p-
179f6 3e 61 4f 70 5b 69 5d 2e 6f 70 63 6f 64 65 29 3b  >aOp[i].opcode);
179f7 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66  .      }.      f
179f8 70 72 69 6e 74 66 28 6f 75 74 2c 20 22 5c 6e 22  printf(out, "\n"
179f9 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  );.      for(i=0
179fa 3b 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29  ; i<p->nOp; i++)
179fb 7b 0a 20 20 20 20 20 20 20 20 66 70 72 69 6e 74  {.        fprint
179fc 66 28 6f 75 74 2c 20 22 25 36 64 20 25 31 30 6c  f(out, "%6d %10l
179fd 6c 64 20 25 38 6c 6c 64 20 22 2c 0a 20 20 20 20  ld %8lld ",.    
179fe 20 20 20 20 20 20 20 70 2d 3e 61 4f 70 5b 69 5d         p->aOp[i]
179ff 2e 63 6e 74 2c 0a 20 20 20 20 20 20 20 20 20 20  .cnt,.          
17a00 20 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65   p->aOp[i].cycle
17a01 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 2d  s,.           p-
17a02 3e 61 4f 70 5b 69 5d 2e 63 6e 74 3e 30 20 3f 20  >aOp[i].cnt>0 ? 
17a03 70 2d 3e 61 4f 70 5b 69 5d 2e 63 79 63 6c 65 73  p->aOp[i].cycles
17a04 2f 70 2d 3e 61 4f 70 5b 69 5d 2e 63 6e 74 20 3a  /p->aOp[i].cnt :
17a05 20 30 0a 20 20 20 20 20 20 20 20 29 3b 0a 20 20   0.        );.  
17a06 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
17a07 65 50 72 69 6e 74 4f 70 28 6f 75 74 2c 20 69 2c  ePrintOp(out, i,
17a08 20 26 70 2d 3e 61 4f 70 5b 69 5d 29 3b 0a 20 20   &p->aOp[i]);.  
17a09 20 20 20 20 7d 0a 20 20 20 20 20 20 66 63 6c 6f      }.      fclo
17a0a 73 65 28 6f 75 74 29 3b 0a 20 20 20 20 7d 0a 20  se(out);.    }. 
17a0b 20 7d 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 6d   }.#endif.  p->m
17a0c 61 67 69 63 20 3d 20 56 44 42 45 5f 4d 41 47 49  agic = VDBE_MAGI
17a0d 43 5f 49 4e 49 54 3b 0a 20 20 72 65 74 75 72 6e  C_INIT;.  return
17a0e 20 70 2d 3e 72 63 20 26 20 64 62 2d 3e 65 72 72   p->rc & db->err
17a0f 4d 61 73 6b 3b 0a 7d 0a 20 0a 2f 2a 0a 2a 2a 20  Mask;.}. ./*.** 
17a10 43 6c 65 61 6e 20 75 70 20 61 6e 64 20 64 65 6c  Clean up and del
17a11 65 74 65 20 61 20 56 44 42 45 20 61 66 74 65 72  ete a VDBE after
17a12 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 52 65 74   execution.  Ret
17a13 75 72 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 77  urn an integer w
17a14 68 69 63 68 20 69 73 0a 2a 2a 20 74 68 65 20 72  hich is.** the r
17a15 65 73 75 6c 74 20 63 6f 64 65 2e 20 20 57 72 69  esult code.  Wri
17a16 74 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73  te any error mes
17a17 73 61 67 65 20 74 65 78 74 20 69 6e 74 6f 20 2a  sage text into *
17a18 70 7a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c  pzErrMsg..*/.SQL
17a19 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
17a1a 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
17a1b 69 7a 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  ize(Vdbe *p){.  
17a1c 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
17a1d 4f 4b 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61 67  OK;.  if( p->mag
17a1e 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
17a1f 55 4e 20 7c 7c 20 70 2d 3e 6d 61 67 69 63 3d 3d  UN || p->magic==
17a20 56 44 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 20  VDBE_MAGIC_HALT 
17a21 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
17a22 74 65 33 56 64 62 65 52 65 73 65 74 28 70 29 3b  te3VdbeReset(p);
17a23 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 72 63  .    assert( (rc
17a24 20 26 20 70 2d 3e 64 62 2d 3e 65 72 72 4d 61 73   & p->db->errMas
17a25 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 7d 65 6c 73  k)==rc );.  }els
17a26 65 20 69 66 28 20 70 2d 3e 6d 61 67 69 63 21 3d  e if( p->magic!=
17a27 56 44 42 45 5f 4d 41 47 49 43 5f 49 4e 49 54 20  VDBE_MAGIC_INIT 
17a28 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
17a29 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
17a2a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65  .  sqlite3VdbeDe
17a2b 6c 65 74 65 28 70 29 3b 0a 20 20 72 65 74 75 72  lete(p);.  retur
17a2c 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
17a2d 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75 63 74  all the destruct
17a2e 6f 72 20 66 6f 72 20 65 61 63 68 20 61 75 78 64  or for each auxd
17a2f 61 74 61 20 65 6e 74 72 79 20 69 6e 20 70 56 64  ata entry in pVd
17a30 62 65 46 75 6e 63 20 66 6f 72 20 77 68 69 63 68  beFunc for which
17a31 0a 2a 2a 20 74 68 65 20 63 6f 72 72 65 73 70 6f  .** the correspo
17a32 6e 64 69 6e 67 20 62 69 74 20 69 6e 20 6d 61 73  nding bit in mas
17a33 6b 20 69 73 20 63 6c 65 61 72 2e 20 20 41 75 78  k is clear.  Aux
17a34 64 61 74 61 20 65 6e 74 72 69 65 73 20 62 65 79  data entries bey
17a35 6f 6e 64 20 33 31 0a 2a 2a 20 61 72 65 20 61 6c  ond 31.** are al
17a36 77 61 79 73 20 64 65 73 74 72 6f 79 65 64 2e 20  ways destroyed. 
17a37 20 54 6f 20 64 65 73 74 72 6f 79 20 61 6c 6c 20   To destroy all 
17a38 61 75 78 64 61 74 61 20 65 6e 74 72 69 65 73 2c  auxdata entries,
17a39 20 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 72 6f   call this.** ro
17a3a 75 74 69 6e 65 20 77 69 74 68 20 6d 61 73 6b 3d  utine with mask=
17a3b 3d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  =0..*/.SQLITE_PR
17a3c 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
17a3d 65 33 56 64 62 65 44 65 6c 65 74 65 41 75 78 44  e3VdbeDeleteAuxD
17a3e 61 74 61 28 56 64 62 65 46 75 6e 63 20 2a 70 56  ata(VdbeFunc *pV
17a3f 64 62 65 46 75 6e 63 2c 20 69 6e 74 20 6d 61 73  dbeFunc, int mas
17a40 6b 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  k){.  int i;.  f
17a41 6f 72 28 69 3d 30 3b 20 69 3c 70 56 64 62 65 46  or(i=0; i<pVdbeF
17a42 75 6e 63 2d 3e 6e 41 75 78 3b 20 69 2b 2b 29 7b  unc->nAux; i++){
17a43 0a 20 20 20 20 73 74 72 75 63 74 20 41 75 78 44  .    struct AuxD
17a44 61 74 61 20 2a 70 41 75 78 20 3d 20 26 70 56 64  ata *pAux = &pVd
17a45 62 65 46 75 6e 63 2d 3e 61 70 41 75 78 5b 69 5d  beFunc->apAux[i]
17a46 3b 0a 20 20 20 20 69 66 28 20 28 69 3e 33 31 20  ;.    if( (i>31 
17a47 7c 7c 20 21 28 6d 61 73 6b 26 28 31 3c 3c 69 29  || !(mask&(1<<i)
17a48 29 29 20 26 26 20 70 41 75 78 2d 3e 70 41 75 78  )) && pAux->pAux
17a49 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 41   ){.      if( pA
17a4a 75 78 2d 3e 78 44 65 6c 65 74 65 20 29 7b 0a 20  ux->xDelete ){. 
17a4b 20 20 20 20 20 20 20 70 41 75 78 2d 3e 78 44 65         pAux->xDe
17a4c 6c 65 74 65 28 70 41 75 78 2d 3e 70 41 75 78 29  lete(pAux->pAux)
17a4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17a4e 70 41 75 78 2d 3e 70 41 75 78 20 3d 20 30 3b 0a  pAux->pAux = 0;.
17a4f 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
17a50 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65 6e 74  ** Delete an ent
17a51 69 72 65 20 56 44 42 45 2e 0a 2a 2f 0a 53 51 4c  ire VDBE..*/.SQL
17a52 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17a53 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65   sqlite3VdbeDele
17a54 74 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69  te(Vdbe *p){.  i
17a55 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65 33 20  nt i;.  sqlite3 
17a56 2a 64 62 3b 0a 0a 20 20 69 66 28 20 70 3d 3d 30  *db;..  if( p==0
17a57 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
17a58 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  = p->db;.  if( p
17a59 2d 3e 70 50 72 65 76 20 29 7b 0a 20 20 20 20 70  ->pPrev ){.    p
17a5a 2d 3e 70 50 72 65 76 2d 3e 70 4e 65 78 74 20 3d  ->pPrev->pNext =
17a5b 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 65 6c   p->pNext;.  }el
17a5c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
17a5d 64 62 2d 3e 70 56 64 62 65 3d 3d 70 20 29 3b 0a  db->pVdbe==p );.
17a5e 20 20 20 20 64 62 2d 3e 70 56 64 62 65 20 3d 20      db->pVdbe = 
17a5f 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20  p->pNext;.  }.  
17a60 69 66 28 20 70 2d 3e 70 4e 65 78 74 20 29 7b 0a  if( p->pNext ){.
17a61 20 20 20 20 70 2d 3e 70 4e 65 78 74 2d 3e 70 50      p->pNext->pP
17a62 72 65 76 20 3d 20 70 2d 3e 70 50 72 65 76 3b 0a  rev = p->pPrev;.
17a63 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 61 4f 70    }.  if( p->aOp
17a64 20 29 7b 0a 20 20 20 20 4f 70 20 2a 70 4f 70 20   ){.    Op *pOp 
17a65 3d 20 70 2d 3e 61 4f 70 3b 0a 20 20 20 20 66 6f  = p->aOp;.    fo
17a66 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4f 70 3b  r(i=0; i<p->nOp;
17a67 20 69 2b 2b 2c 20 70 4f 70 2b 2b 29 7b 0a 20 20   i++, pOp++){.  
17a68 20 20 20 20 66 72 65 65 50 34 28 64 62 2c 20 70      freeP4(db, p
17a69 4f 70 2d 3e 70 34 74 79 70 65 2c 20 70 4f 70 2d  Op->p4type, pOp-
17a6a 3e 70 34 2e 70 29 3b 0a 23 69 66 64 65 66 20 53  >p4.p);.#ifdef S
17a6b 51 4c 49 54 45 5f 44 45 42 55 47 0a 20 20 20 20  QLITE_DEBUG.    
17a6c 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
17a6d 64 62 2c 20 70 4f 70 2d 3e 7a 43 6f 6d 6d 65 6e  db, pOp->zCommen
17a6e 74 29 3b 0a 23 65 6e 64 69 66 20 20 20 20 20 0a  t);.#endif     .
17a6f 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
17a70 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
17a71 4f 70 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61  Op);.  }.  relea
17a72 73 65 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 56  seMemArray(p->aV
17a73 61 72 2c 20 70 2d 3e 6e 56 61 72 29 3b 0a 20 20  ar, p->nVar);.  
17a74 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
17a75 2c 20 70 2d 3e 61 4c 61 62 65 6c 29 3b 0a 20 20  , p->aLabel);.  
17a76 69 66 28 20 70 2d 3e 61 4d 65 6d 20 29 7b 0a 20  if( p->aMem ){. 
17a77 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
17a78 28 64 62 2c 20 26 70 2d 3e 61 4d 65 6d 5b 31 5d  (db, &p->aMem[1]
17a79 29 3b 0a 20 20 7d 0a 20 20 72 65 6c 65 61 73 65  );.  }.  release
17a7a 4d 65 6d 41 72 72 61 79 28 70 2d 3e 61 43 6f 6c  MemArray(p->aCol
17a7b 4e 61 6d 65 2c 20 70 2d 3e 6e 52 65 73 43 6f 6c  Name, p->nResCol
17a7c 75 6d 6e 2a 43 4f 4c 4e 41 4d 45 5f 4e 29 3b 0a  umn*COLNAME_N);.
17a7d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
17a7e 64 62 2c 20 70 2d 3e 61 43 6f 6c 4e 61 6d 65 29  db, p->aColName)
17a7f 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
17a80 65 28 64 62 2c 20 70 2d 3e 7a 53 71 6c 29 3b 0a  e(db, p->zSql);.
17a81 20 20 70 2d 3e 6d 61 67 69 63 20 3d 20 56 44 42    p->magic = VDB
17a82 45 5f 4d 41 47 49 43 5f 44 45 41 44 3b 0a 20 20  E_MAGIC_DEAD;.  
17a83 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
17a84 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  , p);.}../*.** I
17a85 66 20 61 20 4d 6f 76 65 54 6f 20 6f 70 65 72 61  f a MoveTo opera
17a86 74 69 6f 6e 20 69 73 20 70 65 6e 64 69 6e 67 20  tion is pending 
17a87 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 63 75 72  on the given cur
17a88 73 6f 72 2c 20 74 68 65 6e 20 64 6f 20 74 68 61  sor, then do tha
17a89 74 0a 2a 2a 20 4d 6f 76 65 54 6f 20 6e 6f 77 2e  t.** MoveTo now.
17a8a 20 20 52 65 74 75 72 6e 20 61 6e 20 65 72 72 6f    Return an erro
17a8b 72 20 63 6f 64 65 2e 20 20 49 66 20 6e 6f 20 4d  r code.  If no M
17a8c 6f 76 65 54 6f 20 69 73 20 70 65 6e 64 69 6e 67  oveTo is pending
17a8d 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e  , this.** routin
17a8e 65 20 64 6f 65 73 20 6e 6f 74 68 69 6e 67 20 61  e does nothing a
17a8f 6e 64 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  nd returns SQLIT
17a90 45 5f 4f 4b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  E_OK..*/.SQLITE_
17a91 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17a92 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
17a93 65 74 6f 28 56 64 62 65 43 75 72 73 6f 72 20 2a  eto(VdbeCursor *
17a94 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 64 65 66  p){.  if( p->def
17a95 65 72 72 65 64 4d 6f 76 65 74 6f 20 29 7b 0a 20  erredMoveto ){. 
17a96 20 20 20 69 6e 74 20 72 65 73 2c 20 72 63 3b 0a     int res, rc;.
17a97 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
17a98 53 54 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e  ST.    extern in
17a99 74 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68  t sqlite3_search
17a9a 5f 63 6f 75 6e 74 3b 0a 23 65 6e 64 69 66 0a 20  _count;.#endif. 
17a9b 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 69 73     assert( p->is
17a9c 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 72 63 20  Table );.    rc 
17a9d 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f  = sqlite3BtreeMo
17a9e 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 2d 3e  vetoUnpacked(p->
17a9f 70 43 75 72 73 6f 72 2c 20 30 2c 20 70 2d 3e 6d  pCursor, 0, p->m
17aa0 6f 76 65 74 6f 54 61 72 67 65 74 2c 20 30 2c 20  ovetoTarget, 0, 
17aa1 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20 72  &res);.    if( r
17aa2 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  c ) return rc;. 
17aa3 20 20 20 70 2d 3e 6c 61 73 74 52 6f 77 69 64 20     p->lastRowid 
17aa4 3d 20 6b 65 79 54 6f 49 6e 74 28 70 2d 3e 6d 6f  = keyToInt(p->mo
17aa5 76 65 74 6f 54 61 72 67 65 74 29 3b 0a 20 20 20  vetoTarget);.   
17aa6 20 70 2d 3e 72 6f 77 69 64 49 73 56 61 6c 69 64   p->rowidIsValid
17aa7 20 3d 20 72 65 73 3d 3d 30 3b 0a 20 20 20 20 69   = res==0;.    i
17aa8 66 28 20 72 65 73 3c 30 20 29 7b 0a 20 20 20 20  f( res<0 ){.    
17aa9 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
17aaa 72 65 65 4e 65 78 74 28 70 2d 3e 70 43 75 72 73  reeNext(p->pCurs
17aab 6f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20  or, &res);.     
17aac 20 69 66 28 20 72 63 20 29 20 72 65 74 75 72 6e   if( rc ) return
17aad 20 72 63 3b 0a 20 20 20 20 7d 0a 23 69 66 64 65   rc;.    }.#ifde
17aae 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20  f SQLITE_TEST.  
17aaf 20 20 73 71 6c 69 74 65 33 5f 73 65 61 72 63 68    sqlite3_search
17ab0 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
17ab1 0a 20 20 20 20 70 2d 3e 64 65 66 65 72 72 65 64  .    p->deferred
17ab2 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20  Moveto = 0;.    
17ab3 70 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d  p->cacheStatus =
17ab4 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20   CACHE_STALE;.  
17ab5 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 70 43 75  }else if( p->pCu
17ab6 72 73 6f 72 20 29 7b 0a 20 20 20 20 69 6e 74 20  rsor ){.    int 
17ab7 68 61 73 4d 6f 76 65 64 3b 0a 20 20 20 20 69 6e  hasMoved;.    in
17ab8 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74  t rc = sqlite3Bt
17ab9 72 65 65 43 75 72 73 6f 72 48 61 73 4d 6f 76 65  reeCursorHasMove
17aba 64 28 70 2d 3e 70 43 75 72 73 6f 72 2c 20 26 68  d(p->pCursor, &h
17abb 61 73 4d 6f 76 65 64 29 3b 0a 20 20 20 20 69 66  asMoved);.    if
17abc 28 20 72 63 20 29 20 72 65 74 75 72 6e 20 72 63  ( rc ) return rc
17abd 3b 0a 20 20 20 20 69 66 28 20 68 61 73 4d 6f 76  ;.    if( hasMov
17abe 65 64 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 63  ed ){.      p->c
17abf 61 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43  acheStatus = CAC
17ac0 48 45 5f 53 54 41 4c 45 3b 0a 20 20 20 20 20 20  HE_STALE;.      
17ac1 70 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a  p->nullRow = 1;.
17ac2 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
17ac3 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
17ac4 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
17ac5 77 69 6e 67 20 66 75 6e 63 74 69 6f 6e 73 3a 0a  wing functions:.
17ac6 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 56 64 62  **.** sqlite3Vdb
17ac7 65 53 65 72 69 61 6c 54 79 70 65 28 29 0a 2a 2a  eSerialType().**
17ac8 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
17ac9 61 6c 54 79 70 65 4c 65 6e 28 29 0a 2a 2a 20 73  alTypeLen().** s
17aca 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
17acb 4c 65 6e 28 29 0a 2a 2a 20 73 71 6c 69 74 65 33  Len().** sqlite3
17acc 56 64 62 65 53 65 72 69 61 6c 50 75 74 28 29 0a  VdbeSerialPut().
17acd 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65 53 65  ** sqlite3VdbeSe
17ace 72 69 61 6c 47 65 74 28 29 0a 2a 2a 0a 2a 2a 20  rialGet().**.** 
17acf 65 6e 63 61 70 73 75 6c 61 74 65 20 74 68 65 20  encapsulate the 
17ad0 63 6f 64 65 20 74 68 61 74 20 73 65 72 69 61 6c  code that serial
17ad1 69 7a 65 73 20 76 61 6c 75 65 73 20 66 6f 72 20  izes values for 
17ad2 73 74 6f 72 61 67 65 20 69 6e 20 53 51 4c 69 74  storage in SQLit
17ad3 65 0a 2a 2a 20 64 61 74 61 20 61 6e 64 20 69 6e  e.** data and in
17ad4 64 65 78 20 72 65 63 6f 72 64 73 2e 20 45 61 63  dex records. Eac
17ad5 68 20 73 65 72 69 61 6c 69 7a 65 64 20 76 61 6c  h serialized val
17ad6 75 65 20 63 6f 6e 73 69 73 74 73 20 6f 66 20 61  ue consists of a
17ad7 0a 2a 2a 20 27 73 65 72 69 61 6c 2d 74 79 70 65  .** 'serial-type
17ad8 27 20 61 6e 64 20 61 20 62 6c 6f 62 20 6f 66 20  ' and a blob of 
17ad9 64 61 74 61 2e 20 54 68 65 20 73 65 72 69 61 6c  data. The serial
17ada 20 74 79 70 65 20 69 73 20 61 6e 20 38 2d 62 79   type is an 8-by
17adb 74 65 20 75 6e 73 69 67 6e 65 64 0a 2a 2a 20 69  te unsigned.** i
17adc 6e 74 65 67 65 72 2c 20 73 74 6f 72 65 64 20 61  nteger, stored a
17add 73 20 61 20 76 61 72 69 6e 74 2e 0a 2a 2a 0a 2a  s a varint..**.*
17ade 2a 20 49 6e 20 61 6e 20 53 51 4c 69 74 65 20 69  * In an SQLite i
17adf 6e 64 65 78 20 72 65 63 6f 72 64 2c 20 74 68 65  ndex record, the
17ae0 20 73 65 72 69 61 6c 20 74 79 70 65 20 69 73 20   serial type is 
17ae1 73 74 6f 72 65 64 20 64 69 72 65 63 74 6c 79 20  stored directly 
17ae2 62 65 66 6f 72 65 0a 2a 2a 20 74 68 65 20 62 6c  before.** the bl
17ae3 6f 62 20 6f 66 20 64 61 74 61 20 74 68 61 74 20  ob of data that 
17ae4 69 74 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  it corresponds t
17ae5 6f 2e 20 49 6e 20 61 20 74 61 62 6c 65 20 72 65  o. In a table re
17ae6 63 6f 72 64 2c 20 61 6c 6c 20 73 65 72 69 61 6c  cord, all serial
17ae7 0a 2a 2a 20 74 79 70 65 73 20 61 72 65 20 73 74  .** types are st
17ae8 6f 72 65 64 20 61 74 20 74 68 65 20 73 74 61 72  ored at the star
17ae9 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64 2c  t of the record,
17aea 20 61 6e 64 20 74 68 65 20 62 6c 6f 62 73 20 6f   and the blobs o
17aeb 66 20 64 61 74 61 20 61 74 0a 2a 2a 20 74 68 65  f data at.** the
17aec 20 65 6e 64 2e 20 48 65 6e 63 65 20 74 68 65 73   end. Hence thes
17aed 65 20 66 75 6e 63 74 69 6f 6e 73 20 61 6c 6c 6f  e functions allo
17aee 77 20 74 68 65 20 63 61 6c 6c 65 72 20 74 6f 20  w the caller to 
17aef 68 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 73 65  handle the.** se
17af0 72 69 61 6c 2d 74 79 70 65 20 61 6e 64 20 64 61  rial-type and da
17af1 74 61 20 62 6c 6f 62 20 73 65 70 65 72 61 74 65  ta blob seperate
17af2 6c 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ly..**.** The fo
17af3 6c 6c 6f 77 69 6e 67 20 74 61 62 6c 65 20 64 65  llowing table de
17af4 73 63 72 69 62 65 73 20 74 68 65 20 76 61 72 69  scribes the vari
17af5 6f 75 73 20 73 74 6f 72 61 67 65 20 63 6c 61 73  ous storage clas
17af6 73 65 73 20 66 6f 72 20 64 61 74 61 3a 0a 2a 2a  ses for data:.**
17af7 0a 2a 2a 20 20 20 73 65 72 69 61 6c 20 74 79 70  .**   serial typ
17af8 65 20 20 20 20 20 20 20 20 62 79 74 65 73 20 6f  e        bytes o
17af9 66 20 64 61 74 61 20 20 20 20 20 20 74 79 70 65  f data      type
17afa 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
17afb 2d 2d 2d 2d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d  ----     -------
17afc 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d  --------    ----
17afd 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20  -----------.**  
17afe 20 20 20 20 30 20 20 20 20 20 20 20 20 20 20 20      0           
17aff 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20            0     
17b00 20 20 20 20 20 20 20 4e 55 4c 4c 0a 2a 2a 20 20         NULL.**  
17b01 20 20 20 20 31 20 20 20 20 20 20 20 20 20 20 20      1           
17b02 20 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20            1     
17b03 20 20 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e         signed in
17b04 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20 32 20  teger.**      2 
17b05 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b06 20 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20      2           
17b07 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a   signed integer.
17b08 2a 2a 20 20 20 20 20 20 33 20 20 20 20 20 20 20  **      3       
17b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 33 20                3 
17b0a 20 20 20 20 20 20 20 20 20 20 20 73 69 67 6e 65             signe
17b0b 64 20 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20  d integer.**    
17b0c 20 20 34 20 20 20 20 20 20 20 20 20 20 20 20 20    4             
17b0d 20 20 20 20 20 20 20 20 34 20 20 20 20 20 20 20          4       
17b0e 20 20 20 20 20 73 69 67 6e 65 64 20 69 6e 74 65       signed inte
17b0f 67 65 72 0a 2a 2a 20 20 20 20 20 20 35 20 20 20  ger.**      5   
17b10 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b11 20 20 36 20 20 20 20 20 20 20 20 20 20 20 20 73    6            s
17b12 69 67 6e 65 64 20 69 6e 74 65 67 65 72 0a 2a 2a  igned integer.**
17b13 20 20 20 20 20 20 36 20 20 20 20 20 20 20 20 20        6         
17b14 20 20 20 20 20 20 20 20 20 20 20 20 38 20 20 20              8   
17b15 20 20 20 20 20 20 20 20 20 73 69 67 6e 65 64 20           signed 
17b16 69 6e 74 65 67 65 72 0a 2a 2a 20 20 20 20 20 20  integer.**      
17b17 37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  7               
17b18 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
17b19 20 20 20 49 45 45 45 20 66 6c 6f 61 74 0a 2a 2a     IEEE float.**
17b1a 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
17b1b 20 20 20 20 20 20 20 20 20 20 20 20 30 20 20 20              0   
17b1c 20 20 20 20 20 20 20 20 20 49 6e 74 65 67 65 72           Integer
17b1d 20 63 6f 6e 73 74 61 6e 74 20 30 0a 2a 2a 20 20   constant 0.**  
17b1e 20 20 20 20 39 20 20 20 20 20 20 20 20 20 20 20      9           
17b1f 20 20 20 20 20 20 20 20 20 20 30 20 20 20 20 20            0     
17b20 20 20 20 20 20 20 20 49 6e 74 65 67 65 72 20 63         Integer c
17b21 6f 6e 73 74 61 6e 74 20 31 0a 2a 2a 20 20 20 20  onstant 1.**    
17b22 20 31 30 2c 31 31 20 20 20 20 20 20 20 20 20 20   10,11          
17b23 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17b24 20 20 20 20 20 72 65 73 65 72 76 65 64 20 66 6f       reserved fo
17b25 72 20 65 78 70 61 6e 73 69 6f 6e 0a 2a 2a 20 20  r expansion.**  
17b26 20 20 4e 3e 3d 31 32 20 61 6e 64 20 65 76 65 6e    N>=12 and even
17b27 20 20 20 20 20 20 20 28 4e 2d 31 32 29 2f 32 20         (N-12)/2 
17b28 20 20 20 20 20 20 20 42 4c 4f 42 0a 2a 2a 20 20         BLOB.**  
17b29 20 20 4e 3e 3d 31 33 20 61 6e 64 20 6f 64 64 20    N>=13 and odd 
17b2a 20 20 20 20 20 20 20 28 4e 2d 31 33 29 2f 32 20         (N-13)/2 
17b2b 20 20 20 20 20 20 20 74 65 78 74 0a 2a 2a 0a 2a         text.**.*
17b2c 2a 20 54 68 65 20 38 20 61 6e 64 20 39 20 74 79  * The 8 and 9 ty
17b2d 70 65 73 20 77 65 72 65 20 61 64 64 65 64 20 69  pes were added i
17b2e 6e 20 33 2e 33 2e 30 2c 20 66 69 6c 65 20 66 6f  n 3.3.0, file fo
17b2f 72 6d 61 74 20 34 2e 20 20 50 72 69 6f 72 20 76  rmat 4.  Prior v
17b30 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20 53 51  ersions.** of SQ
17b31 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20 75 6e  Lite will not un
17b32 64 65 72 73 74 61 6e 64 20 74 68 6f 73 65 20 73  derstand those s
17b33 65 72 69 61 6c 20 74 79 70 65 73 2e 0a 2a 2f 0a  erial types..*/.
17b34 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
17b35 65 20 73 65 72 69 61 6c 2d 74 79 70 65 20 66 6f  e serial-type fo
17b36 72 20 74 68 65 20 76 61 6c 75 65 20 73 74 6f 72  r the value stor
17b37 65 64 20 69 6e 20 70 4d 65 6d 2e 0a 2a 2f 0a 53  ed in pMem..*/.S
17b38 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 75 33  QLITE_PRIVATE u3
17b39 32 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72  2 sqlite3VdbeSer
17b3a 69 61 6c 54 79 70 65 28 4d 65 6d 20 2a 70 4d 65  ialType(Mem *pMe
17b3b 6d 2c 20 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d  m, int file_form
17b3c 61 74 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73  at){.  int flags
17b3d 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a   = pMem->flags;.
17b3e 20 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28 20    int n;..  if( 
17b3f 66 6c 61 67 73 26 4d 45 4d 5f 4e 75 6c 6c 20 29  flags&MEM_Null )
17b40 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
17b41 20 20 7d 0a 20 20 69 66 28 20 66 6c 61 67 73 26    }.  if( flags&
17b42 4d 45 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 2f  MEM_Int ){.    /
17b43 2a 20 46 69 67 75 72 65 20 6f 75 74 20 77 68 65  * Figure out whe
17b44 74 68 65 72 20 74 6f 20 75 73 65 20 31 2c 20 32  ther to use 1, 2
17b45 2c 20 34 2c 20 36 20 6f 72 20 38 20 62 79 74 65  , 4, 6 or 8 byte
17b46 73 2e 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e 65  s. */.#   define
17b47 20 4d 41 58 5f 36 42 59 54 45 20 28 28 28 28 69   MAX_6BYTE ((((i
17b48 36 34 29 30 78 30 30 30 30 38 30 30 30 29 3c 3c  64)0x00008000)<<
17b49 33 32 29 2d 31 29 0a 20 20 20 20 69 36 34 20 69  32)-1).    i64 i
17b4a 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a 20 20   = pMem->u.i;.  
17b4b 20 20 75 36 34 20 75 3b 0a 20 20 20 20 69 66 28    u64 u;.    if(
17b4c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20   file_format>=4 
17b4d 26 26 20 28 69 26 31 29 3d 3d 69 20 29 7b 0a 20  && (i&1)==i ){. 
17b4e 20 20 20 20 20 72 65 74 75 72 6e 20 38 2b 69 3b       return 8+i;
17b4f 0a 20 20 20 20 7d 0a 20 20 20 20 75 20 3d 20 69  .    }.    u = i
17b50 3c 30 20 3f 20 2d 69 20 3a 20 69 3b 0a 20 20 20  <0 ? -i : i;.   
17b51 20 69 66 28 20 75 3c 3d 31 32 37 20 29 20 72 65   if( u<=127 ) re
17b52 74 75 72 6e 20 31 3b 0a 20 20 20 20 69 66 28 20  turn 1;.    if( 
17b53 75 3c 3d 33 32 37 36 37 20 29 20 72 65 74 75 72  u<=32767 ) retur
17b54 6e 20 32 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d  n 2;.    if( u<=
17b55 38 33 38 38 36 30 37 20 29 20 72 65 74 75 72 6e  8388607 ) return
17b56 20 33 3b 0a 20 20 20 20 69 66 28 20 75 3c 3d 32   3;.    if( u<=2
17b57 31 34 37 34 38 33 36 34 37 20 29 20 72 65 74 75  147483647 ) retu
17b58 72 6e 20 34 3b 0a 20 20 20 20 69 66 28 20 75 3c  rn 4;.    if( u<
17b59 3d 4d 41 58 5f 36 42 59 54 45 20 29 20 72 65 74  =MAX_6BYTE ) ret
17b5a 75 72 6e 20 35 3b 0a 20 20 20 20 72 65 74 75 72  urn 5;.    retur
17b5b 6e 20 36 3b 0a 20 20 7d 0a 20 20 69 66 28 20 66  n 6;.  }.  if( f
17b5c 6c 61 67 73 26 4d 45 4d 5f 52 65 61 6c 20 29 7b  lags&MEM_Real ){
17b5d 0a 20 20 20 20 72 65 74 75 72 6e 20 37 3b 0a 20  .    return 7;. 
17b5e 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 4d 65   }.  assert( pMe
17b5f 6d 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  m->db->mallocFai
17b60 6c 65 64 20 7c 7c 20 66 6c 61 67 73 26 28 4d 45  led || flags&(ME
17b61 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 20  M_Str|MEM_Blob) 
17b62 29 3b 0a 20 20 6e 20 3d 20 70 4d 65 6d 2d 3e 6e  );.  n = pMem->n
17b63 3b 0a 20 20 69 66 28 20 66 6c 61 67 73 20 26 20  ;.  if( flags & 
17b64 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20 20 20  MEM_Zero ){.    
17b65 6e 20 2b 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a  n += pMem->u.i;.
17b66 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 6e 3e    }.  assert( n>
17b67 3d 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 28  =0 );.  return (
17b68 28 6e 2a 32 29 20 2b 20 31 32 20 2b 20 28 28 66  (n*2) + 12 + ((f
17b69 6c 61 67 73 26 4d 45 4d 5f 53 74 72 29 21 3d 30  lags&MEM_Str)!=0
17b6a 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  ));.}../*.** Ret
17b6b 75 72 6e 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  urn the length o
17b6c 66 20 74 68 65 20 64 61 74 61 20 63 6f 72 72 65  f the data corre
17b6d 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65 20  sponding to the 
17b6e 73 75 70 70 6c 69 65 64 20 73 65 72 69 61 6c 2d  supplied serial-
17b6f 74 79 70 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  type..*/.SQLITE_
17b70 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
17b71 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
17b72 65 4c 65 6e 28 75 33 32 20 73 65 72 69 61 6c 5f  eLen(u32 serial_
17b73 74 79 70 65 29 7b 0a 20 20 69 66 28 20 73 65 72  type){.  if( ser
17b74 69 61 6c 5f 74 79 70 65 3e 3d 31 32 20 29 7b 0a  ial_type>=12 ){.
17b75 20 20 20 20 72 65 74 75 72 6e 20 28 73 65 72 69      return (seri
17b76 61 6c 5f 74 79 70 65 2d 31 32 29 2f 32 3b 0a 20  al_type-12)/2;. 
17b77 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74   }else{.    stat
17b78 69 63 20 63 6f 6e 73 74 20 75 38 20 61 53 69 7a  ic const u8 aSiz
17b79 65 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20 32 2c  e[] = { 0, 1, 2,
17b7a 20 33 2c 20 34 2c 20 36 2c 20 38 2c 20 38 2c 20   3, 4, 6, 8, 8, 
17b7b 30 2c 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20  0, 0, 0, 0 };.  
17b7c 20 20 72 65 74 75 72 6e 20 61 53 69 7a 65 5b 73    return aSize[s
17b7d 65 72 69 61 6c 5f 74 79 70 65 5d 3b 0a 20 20 7d  erial_type];.  }
17b7e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 77 65 20  .}../*.** If we 
17b7f 61 72 65 20 6f 6e 20 61 6e 20 61 72 63 68 69 74  are on an archit
17b80 65 63 74 75 72 65 20 77 69 74 68 20 6d 69 78 65  ecture with mixe
17b81 64 2d 65 6e 64 69 61 6e 20 66 6c 6f 61 74 69 6e  d-endian floatin
17b82 67 20 0a 2a 2a 20 70 6f 69 6e 74 73 20 28 65 78  g .** points (ex
17b83 3a 20 41 52 4d 37 29 20 74 68 65 6e 20 73 77 61  : ARM7) then swa
17b84 70 20 74 68 65 20 6c 6f 77 65 72 20 34 20 62 79  p the lower 4 by
17b85 74 65 73 20 77 69 74 68 20 74 68 65 20 0a 2a 2a  tes with the .**
17b86 20 75 70 70 65 72 20 34 20 62 79 74 65 73 2e 20   upper 4 bytes. 
17b87 20 52 65 74 75 72 6e 20 74 68 65 20 72 65 73 75   Return the resu
17b88 6c 74 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 6d 6f  lt..**.** For mo
17b89 73 74 20 61 72 63 68 69 74 65 63 74 75 72 65 73  st architectures
17b8a 2c 20 74 68 69 73 20 69 73 20 61 20 6e 6f 2d 6f  , this is a no-o
17b8b 70 2e 0a 2a 2a 0a 2a 2a 20 28 6c 61 74 65 72 29  p..**.** (later)
17b8c 3a 20 20 49 74 20 69 73 20 72 65 70 6f 72 74 65  :  It is reporte
17b8d 64 20 74 6f 20 6d 65 20 74 68 61 74 20 74 68 65  d to me that the
17b8e 20 6d 69 78 65 64 2d 65 6e 64 69 61 6e 20 70 72   mixed-endian pr
17b8f 6f 62 6c 65 6d 0a 2a 2a 20 6f 6e 20 41 52 4d 37  oblem.** on ARM7
17b90 20 69 73 20 61 6e 20 69 73 73 75 65 20 77 69 74   is an issue wit
17b91 68 20 47 43 43 2c 20 6e 6f 74 20 77 69 74 68 20  h GCC, not with 
17b92 74 68 65 20 41 52 4d 37 20 63 68 69 70 2e 20 20  the ARM7 chip.  
17b93 49 74 20 73 65 65 6d 73 0a 2a 2a 20 74 68 61 74  It seems.** that
17b94 20 65 61 72 6c 79 20 76 65 72 73 69 6f 6e 73 20   early versions 
17b95 6f 66 20 47 43 43 20 73 74 6f 72 65 64 20 74 68  of GCC stored th
17b96 65 20 74 77 6f 20 77 6f 72 64 73 20 6f 66 20 61  e two words of a
17b97 20 36 34 2d 62 69 74 0a 2a 2a 20 66 6c 6f 61 74   64-bit.** float
17b98 20 69 6e 20 74 68 65 20 77 72 6f 6e 67 20 6f 72   in the wrong or
17b99 64 65 72 2e 20 20 41 6e 64 20 74 68 61 74 20 65  der.  And that e
17b9a 72 72 6f 72 20 68 61 73 20 62 65 65 6e 20 70 72  rror has been pr
17b9b 6f 70 61 67 61 74 65 64 0a 2a 2a 20 65 76 65 72  opagated.** ever
17b9c 20 73 69 6e 63 65 2e 20 20 54 68 65 20 62 6c 61   since.  The bla
17b9d 6d 65 20 69 73 20 6e 6f 74 20 6e 65 63 65 73 73  me is not necess
17b9e 61 72 69 6c 79 20 77 69 74 68 20 47 43 43 2c 20  arily with GCC, 
17b9f 74 68 6f 75 67 68 2e 0a 2a 2a 20 47 43 43 20 6d  though..** GCC m
17ba0 69 67 68 74 20 68 61 76 65 20 6a 75 73 74 20 63  ight have just c
17ba1 6f 70 79 69 6e 67 20 74 68 65 20 70 72 6f 62 6c  opying the probl
17ba2 65 6d 20 66 72 6f 6d 20 61 20 70 72 69 6f 72 20  em from a prior 
17ba3 63 6f 6d 70 69 6c 65 72 2e 0a 2a 2a 20 49 20 61  compiler..** I a
17ba4 6d 20 61 6c 73 6f 20 74 6f 6c 64 20 74 68 61 74  m also told that
17ba5 20 6e 65 77 65 72 20 76 65 72 73 69 6f 6e 73 20   newer versions 
17ba6 6f 66 20 47 43 43 20 74 68 61 74 20 66 6f 6c 6c  of GCC that foll
17ba7 6f 77 20 61 20 64 69 66 66 65 72 65 6e 74 0a 2a  ow a different.*
17ba8 2a 20 41 42 49 20 67 65 74 20 74 68 65 20 62 79  * ABI get the by
17ba9 74 65 20 6f 72 64 65 72 20 72 69 67 68 74 2e 0a  te order right..
17baa 2a 2a 0a 2a 2a 20 44 65 76 65 6c 6f 70 65 72 73  **.** Developers
17bab 20 75 73 69 6e 67 20 53 51 4c 69 74 65 20 6f 6e   using SQLite on
17bac 20 61 6e 20 41 52 4d 37 20 73 68 6f 75 6c 64 20   an ARM7 should 
17bad 63 6f 6d 70 69 6c 65 20 61 6e 64 20 72 75 6e 20  compile and run 
17bae 74 68 65 69 72 0a 2a 2a 20 61 70 70 6c 69 63 61  their.** applica
17baf 74 69 6f 6e 20 75 73 69 6e 67 20 2d 44 53 51 4c  tion using -DSQL
17bb0 49 54 45 5f 44 45 42 55 47 3d 31 20 61 74 20 6c  ITE_DEBUG=1 at l
17bb1 65 61 73 74 20 6f 6e 63 65 2e 20 20 57 69 74 68  east once.  With
17bb2 20 44 45 42 55 47 0a 2a 2a 20 65 6e 61 62 6c 65   DEBUG.** enable
17bb3 64 2c 20 73 6f 6d 65 20 61 73 73 65 72 74 73 20  d, some asserts 
17bb4 62 65 6c 6f 77 20 77 69 6c 6c 20 65 6e 73 75 72  below will ensur
17bb5 65 20 74 68 61 74 20 74 68 65 20 62 79 74 65 20  e that the byte 
17bb6 6f 72 64 65 72 20 6f 66 0a 2a 2a 20 66 6c 6f 61  order of.** floa
17bb7 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
17bb8 73 20 69 73 20 63 6f 72 72 65 63 74 2e 0a 2a 2a  s is correct..**
17bb9 0a 2a 2a 20 28 32 30 30 37 2d 30 38 2d 33 30 29  .** (2007-08-30)
17bba 20 20 46 72 61 6e 6b 20 76 61 6e 20 56 75 67 74    Frank van Vugt
17bbb 20 68 61 73 20 73 74 75 64 69 65 64 20 74 68 69   has studied thi
17bbc 73 20 70 72 6f 62 6c 65 6d 20 63 6c 6f 73 65 6c  s problem closel
17bbd 79 0a 2a 2a 20 61 6e 64 20 68 61 73 20 73 65 6e  y.** and has sen
17bbe 64 20 68 69 73 20 66 69 6e 64 69 6e 67 73 20 74  d his findings t
17bbf 6f 20 74 68 65 20 53 51 4c 69 74 65 20 64 65 76  o the SQLite dev
17bc0 65 6c 6f 70 65 72 73 2e 20 20 46 72 61 6e 6b 0a  elopers.  Frank.
17bc1 2a 2a 20 77 72 69 74 65 73 20 74 68 61 74 20 73  ** writes that s
17bc2 6f 6d 65 20 4c 69 6e 75 78 20 6b 65 72 6e 65 6c  ome Linux kernel
17bc3 73 20 6f 66 66 65 72 20 66 6c 6f 61 74 69 6e 67  s offer floating
17bc4 20 70 6f 69 6e 74 20 68 61 72 64 77 61 72 65 0a   point hardware.
17bc5 2a 2a 20 65 6d 75 6c 61 74 69 6f 6e 20 74 68 61  ** emulation tha
17bc6 74 20 75 73 65 73 20 6f 6e 6c 79 20 33 32 2d 62  t uses only 32-b
17bc7 69 74 20 6d 61 6e 74 69 73 73 61 73 20 69 6e 73  it mantissas ins
17bc8 74 65 61 64 20 6f 66 20 61 20 66 75 6c 6c 20 0a  tead of a full .
17bc9 2a 2a 20 34 38 2d 62 69 74 73 20 61 73 20 72 65  ** 48-bits as re
17bca 71 75 69 72 65 64 20 62 79 20 74 68 65 20 49 45  quired by the IE
17bcb 45 45 20 73 74 61 6e 64 61 72 64 2e 20 20 28 54  EE standard.  (T
17bcc 68 69 73 20 69 73 20 74 68 65 0a 2a 2a 20 43 4f  his is the.** CO
17bcd 4e 46 49 47 5f 46 50 45 5f 46 41 53 54 46 50 45  NFIG_FPE_FASTFPE
17bce 20 6f 70 74 69 6f 6e 2e 29 20 20 4f 6e 20 73 75   option.)  On su
17bcf 63 68 20 73 79 73 74 65 6d 73 2c 20 66 6c 6f 61  ch systems, floa
17bd0 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a 20 62 79  ting point.** by
17bd1 74 65 20 73 77 61 70 70 69 6e 67 20 62 65 63 6f  te swapping beco
17bd2 6d 65 73 20 76 65 72 79 20 63 6f 6d 70 6c 69 63  mes very complic
17bd3 61 74 65 64 2e 20 20 54 6f 20 61 76 6f 69 64 20  ated.  To avoid 
17bd4 70 72 6f 62 6c 65 6d 73 2c 0a 2a 2a 20 74 68 65  problems,.** the
17bd5 20 6e 65 63 65 73 73 61 72 79 20 62 79 74 65 20   necessary byte 
17bd6 73 77 61 70 70 69 6e 67 20 69 73 20 63 61 72 72  swapping is carr
17bd7 69 65 64 20 6f 75 74 20 75 73 69 6e 67 20 61 20  ied out using a 
17bd8 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72 0a 2a  64-bit integer.*
17bd9 2a 20 72 61 74 68 65 72 20 74 68 61 6e 20 61 20  * rather than a 
17bda 36 34 2d 62 69 74 20 66 6c 6f 61 74 2e 20 20 46  64-bit float.  F
17bdb 72 61 6e 6b 20 61 73 73 75 72 65 73 20 75 73 20  rank assures us 
17bdc 74 68 61 74 20 74 68 65 20 63 6f 64 65 20 68 65  that the code he
17bdd 72 65 0a 2a 2a 20 77 6f 72 6b 73 20 66 6f 72 20  re.** works for 
17bde 68 69 6d 2e 20 20 57 65 2c 20 74 68 65 20 64 65  him.  We, the de
17bdf 76 65 6c 6f 70 65 72 73 2c 20 68 61 76 65 20 6e  velopers, have n
17be0 6f 20 77 61 79 20 74 6f 20 69 6e 64 65 70 65 6e  o way to indepen
17be1 64 65 6e 74 6c 79 0a 2a 2a 20 76 65 72 69 66 79  dently.** verify
17be2 20 74 68 69 73 2c 20 62 75 74 20 46 72 61 6e 6b   this, but Frank
17be3 20 73 65 65 6d 73 20 74 6f 20 6b 6e 6f 77 20 77   seems to know w
17be4 68 61 74 20 68 65 20 69 73 20 74 61 6c 6b 69 6e  hat he is talkin
17be5 67 20 61 62 6f 75 74 0a 2a 2a 20 73 6f 20 77 65  g about.** so we
17be6 20 74 72 75 73 74 20 68 69 6d 2e 0a 2a 2f 0a 23   trust him..*/.#
17be7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4d 49 58  ifdef SQLITE_MIX
17be8 45 44 5f 45 4e 44 49 41 4e 5f 36 34 42 49 54 5f  ED_ENDIAN_64BIT_
17be9 46 4c 4f 41 54 0a 73 74 61 74 69 63 20 75 36 34  FLOAT.static u64
17bea 20 66 6c 6f 61 74 53 77 61 70 28 75 36 34 20 69   floatSwap(u64 i
17beb 6e 29 7b 0a 20 20 75 6e 69 6f 6e 20 7b 0a 20 20  n){.  union {.  
17bec 20 20 75 36 34 20 72 3b 0a 20 20 20 20 75 33 32    u64 r;.    u32
17bed 20 69 5b 32 5d 3b 0a 20 20 7d 20 75 3b 0a 20 20   i[2];.  } u;.  
17bee 75 33 32 20 74 3b 0a 0a 20 20 75 2e 72 20 3d 20  u32 t;..  u.r = 
17bef 69 6e 3b 0a 20 20 74 20 3d 20 75 2e 69 5b 30 5d  in;.  t = u.i[0]
17bf0 3b 0a 20 20 75 2e 69 5b 30 5d 20 3d 20 75 2e 69  ;.  u.i[0] = u.i
17bf1 5b 31 5d 3b 0a 20 20 75 2e 69 5b 31 5d 20 3d 20  [1];.  u.i[1] = 
17bf2 74 3b 0a 20 20 72 65 74 75 72 6e 20 75 2e 72 3b  t;.  return u.r;
17bf3 0a 7d 0a 23 20 64 65 66 69 6e 65 20 73 77 61 70  .}.# define swap
17bf4 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61 74  MixedEndianFloat
17bf5 28 58 29 20 20 58 20 3d 20 66 6c 6f 61 74 53 77  (X)  X = floatSw
17bf6 61 70 28 58 29 0a 23 65 6c 73 65 0a 23 20 64 65  ap(X).#else.# de
17bf7 66 69 6e 65 20 73 77 61 70 4d 69 78 65 64 45 6e  fine swapMixedEn
17bf8 64 69 61 6e 46 6c 6f 61 74 28 58 29 0a 23 65 6e  dianFloat(X).#en
17bf9 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  dif../*.** Write
17bfa 20 74 68 65 20 73 65 72 69 61 6c 69 7a 65 64 20   the serialized 
17bfb 64 61 74 61 20 62 6c 6f 62 20 66 6f 72 20 74 68  data blob for th
17bfc 65 20 76 61 6c 75 65 20 73 74 6f 72 65 64 20 69  e value stored i
17bfd 6e 20 70 4d 65 6d 20 69 6e 74 6f 20 0a 2a 2a 20  n pMem into .** 
17bfe 62 75 66 2e 20 49 74 20 69 73 20 61 73 73 75 6d  buf. It is assum
17bff 65 64 20 74 68 61 74 20 74 68 65 20 63 61 6c 6c  ed that the call
17c00 65 72 20 68 61 73 20 61 6c 6c 6f 63 61 74 65 64  er has allocated
17c01 20 73 75 66 66 69 63 69 65 6e 74 20 73 70 61 63   sufficient spac
17c02 65 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  e..** Return the
17c03 20 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   number of bytes
17c04 20 77 72 69 74 74 65 6e 2e 0a 2a 2a 0a 2a 2a 20   written..**.** 
17c05 6e 42 75 66 20 69 73 20 74 68 65 20 61 6d 6f 75  nBuf is the amou
17c06 6e 74 20 6f 66 20 73 70 61 63 65 20 6c 65 66 74  nt of space left
17c07 20 69 6e 20 62 75 66 5b 5d 2e 20 20 6e 42 75 66   in buf[].  nBuf
17c08 20 6d 75 73 74 20 61 6c 77 61 79 73 20 62 65 0a   must always be.
17c09 2a 2a 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20  ** large enough 
17c0a 74 6f 20 68 6f 6c 64 20 74 68 65 20 65 6e 74 69  to hold the enti
17c0b 72 65 20 66 69 65 6c 64 2e 20 20 45 78 63 65 70  re field.  Excep
17c0c 74 2c 20 69 66 20 74 68 65 20 66 69 65 6c 64 20  t, if the field 
17c0d 69 73 0a 2a 2a 20 61 20 62 6c 6f 62 20 77 69 74  is.** a blob wit
17c0e 68 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20  h a zero-filled 
17c0f 74 61 69 6c 2c 20 74 68 65 6e 20 62 75 66 5b 5d  tail, then buf[]
17c10 20 6d 69 67 68 74 20 62 65 20 6a 75 73 74 20 74   might be just t
17c11 68 65 20 72 69 67 68 74 0a 2a 2a 20 73 69 7a 65  he right.** size
17c12 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74 68   to hold everyth
17c13 69 6e 67 20 65 78 63 65 70 74 20 66 6f 72 20 74  ing except for t
17c14 68 65 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74  he zero-filled t
17c15 61 69 6c 2e 20 20 49 66 20 62 75 66 5b 5d 0a 2a  ail.  If buf[].*
17c16 2a 20 69 73 20 6f 6e 6c 79 20 62 69 67 20 65 6e  * is only big en
17c17 6f 75 67 68 20 74 6f 20 68 6f 6c 64 20 74 68 65  ough to hold the
17c18 20 6e 6f 6e 2d 7a 65 72 6f 20 70 72 65 66 69 78   non-zero prefix
17c19 2c 20 74 68 65 6e 20 6f 6e 6c 79 20 77 72 69 74  , then only writ
17c1a 65 20 74 68 61 74 0a 2a 2a 20 70 72 65 66 69 78  e that.** prefix
17c1b 20 69 6e 74 6f 20 62 75 66 5b 5d 2e 20 20 42 75   into buf[].  Bu
17c1c 74 20 69 66 20 62 75 66 5b 5d 20 69 73 20 6c 61  t if buf[] is la
17c1d 72 67 65 20 65 6e 6f 75 67 68 20 74 6f 20 68 6f  rge enough to ho
17c1e 6c 64 20 62 6f 74 68 20 74 68 65 0a 2a 2a 20 70  ld both the.** p
17c1f 72 65 66 69 78 20 61 6e 64 20 74 68 65 20 74 61  refix and the ta
17c20 69 6c 20 74 68 65 6e 20 77 72 69 74 65 20 74 68  il then write th
17c21 65 20 70 72 65 66 69 78 20 61 6e 64 20 73 65 74  e prefix and set
17c22 20 74 68 65 20 74 61 69 6c 20 74 6f 20 61 6c 6c   the tail to all
17c23 0a 2a 2a 20 7a 65 72 6f 73 2e 0a 2a 2a 0a 2a 2a  .** zeros..**.**
17c24 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
17c25 65 72 20 6f 66 20 62 79 74 65 73 20 61 63 74 75  er of bytes actu
17c26 61 6c 6c 79 20 77 72 69 74 74 65 6e 20 69 6e 74  ally written int
17c27 6f 20 62 75 66 5b 5d 2e 20 20 54 68 65 20 6e 75  o buf[].  The nu
17c28 6d 62 65 72 0a 2a 2a 20 6f 66 20 62 79 74 65 73  mber.** of bytes
17c29 20 69 6e 20 74 68 65 20 7a 65 72 6f 2d 66 69 6c   in the zero-fil
17c2a 6c 65 64 20 74 61 69 6c 20 69 73 20 69 6e 63 6c  led tail is incl
17c2b 75 64 65 64 20 69 6e 20 74 68 65 20 72 65 74 75  uded in the retu
17c2c 72 6e 20 76 61 6c 75 65 20 6f 6e 6c 79 0a 2a 2a  rn value only.**
17c2d 20 69 66 20 74 68 6f 73 65 20 62 79 74 65 73 20   if those bytes 
17c2e 77 65 72 65 20 7a 65 72 6f 65 64 20 69 6e 20 62  were zeroed in b
17c2f 75 66 5b 5d 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45  uf[]..*/ .SQLITE
17c30 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
17c31 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 50 75  ite3VdbeSerialPu
17c32 74 28 75 38 20 2a 62 75 66 2c 20 69 6e 74 20 6e  t(u8 *buf, int n
17c33 42 75 66 2c 20 4d 65 6d 20 2a 70 4d 65 6d 2c 20  Buf, Mem *pMem, 
17c34 69 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29  int file_format)
17c35 7b 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74  {.  u32 serial_t
17c36 79 70 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ype = sqlite3Vdb
17c37 65 53 65 72 69 61 6c 54 79 70 65 28 70 4d 65 6d  eSerialType(pMem
17c38 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a  , file_format);.
17c39 20 20 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 2f 2a    int len;..  /*
17c3a 20 49 6e 74 65 67 65 72 20 61 6e 64 20 52 65 61   Integer and Rea
17c3b 6c 20 2a 2f 0a 20 20 69 66 28 20 73 65 72 69 61  l */.  if( seria
17c3c 6c 5f 74 79 70 65 3c 3d 37 20 26 26 20 73 65 72  l_type<=7 && ser
17c3d 69 61 6c 5f 74 79 70 65 3e 30 20 29 7b 0a 20 20  ial_type>0 ){.  
17c3e 20 20 75 36 34 20 76 3b 0a 20 20 20 20 69 6e 74    u64 v;.    int
17c3f 20 69 3b 0a 20 20 20 20 69 66 28 20 73 65 72 69   i;.    if( seri
17c40 61 6c 5f 74 79 70 65 3d 3d 37 20 29 7b 0a 20 20  al_type==7 ){.  
17c41 20 20 20 20 61 73 73 65 72 74 28 20 73 69 7a 65      assert( size
17c42 6f 66 28 76 29 3d 3d 73 69 7a 65 6f 66 28 70 4d  of(v)==sizeof(pM
17c43 65 6d 2d 3e 72 29 20 29 3b 0a 20 20 20 20 20 20  em->r) );.      
17c44 6d 65 6d 63 70 79 28 26 76 2c 20 26 70 4d 65 6d  memcpy(&v, &pMem
17c45 2d 3e 72 2c 20 73 69 7a 65 6f 66 28 76 29 29 3b  ->r, sizeof(v));
17c46 0a 20 20 20 20 20 20 73 77 61 70 4d 69 78 65 64  .      swapMixed
17c47 45 6e 64 69 61 6e 46 6c 6f 61 74 28 76 29 3b 0a  EndianFloat(v);.
17c48 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
17c49 20 76 20 3d 20 70 4d 65 6d 2d 3e 75 2e 69 3b 0a   v = pMem->u.i;.
17c4a 20 20 20 20 7d 0a 20 20 20 20 6c 65 6e 20 3d 20      }.    len = 
17c4b 69 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  i = sqlite3VdbeS
17c4c 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73 65 72  erialTypeLen(ser
17c4d 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 61  ial_type);.    a
17c4e 73 73 65 72 74 28 20 6c 65 6e 3c 3d 6e 42 75 66  ssert( len<=nBuf
17c4f 20 29 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 69   );.    while( i
17c50 2d 2d 20 29 7b 0a 20 20 20 20 20 20 62 75 66 5b  -- ){.      buf[
17c51 69 5d 20 3d 20 28 76 26 30 78 46 46 29 3b 0a 20  i] = (v&0xFF);. 
17c52 20 20 20 20 20 76 20 3e 3e 3d 20 38 3b 0a 20 20       v >>= 8;.  
17c53 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6c    }.    return l
17c54 65 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  en;.  }..  /* St
17c55 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 2a 2f 0a  ring or blob */.
17c56 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79 70    if( serial_typ
17c57 65 3e 3d 31 32 20 29 7b 0a 20 20 20 20 61 73 73  e>=12 ){.    ass
17c58 65 72 74 28 20 70 4d 65 6d 2d 3e 6e 20 2b 20 28  ert( pMem->n + (
17c59 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20 4d  (pMem->flags & M
17c5a 45 4d 5f 5a 65 72 6f 29 3f 70 4d 65 6d 2d 3e 75  EM_Zero)?pMem->u
17c5b 2e 69 3a 30 29 0a 20 20 20 20 20 20 20 20 20 20  .i:0).          
17c5c 20 20 20 3d 3d 20 73 71 6c 69 74 65 33 56 64 62     == sqlite3Vdb
17c5d 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 73  eSerialTypeLen(s
17c5e 65 72 69 61 6c 5f 74 79 70 65 29 20 29 3b 0a 20  erial_type) );. 
17c5f 20 20 20 61 73 73 65 72 74 28 20 70 4d 65 6d 2d     assert( pMem-
17c60 3e 6e 3c 3d 6e 42 75 66 20 29 3b 0a 20 20 20 20  >n<=nBuf );.    
17c61 6c 65 6e 20 3d 20 70 4d 65 6d 2d 3e 6e 3b 0a 20  len = pMem->n;. 
17c62 20 20 20 6d 65 6d 63 70 79 28 62 75 66 2c 20 70     memcpy(buf, p
17c63 4d 65 6d 2d 3e 7a 2c 20 6c 65 6e 29 3b 0a 20 20  Mem->z, len);.  
17c64 20 20 69 66 28 20 70 4d 65 6d 2d 3e 66 6c 61 67    if( pMem->flag
17c65 73 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a  s & MEM_Zero ){.
17c66 20 20 20 20 20 20 6c 65 6e 20 2b 3d 20 70 4d 65        len += pMe
17c67 6d 2d 3e 75 2e 69 3b 0a 20 20 20 20 20 20 69 66  m->u.i;.      if
17c68 28 20 6c 65 6e 3e 6e 42 75 66 20 29 7b 0a 20 20  ( len>nBuf ){.  
17c69 20 20 20 20 20 20 6c 65 6e 20 3d 20 6e 42 75 66        len = nBuf
17c6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
17c6b 6d 65 6d 73 65 74 28 26 62 75 66 5b 70 4d 65 6d  memset(&buf[pMem
17c6c 2d 3e 6e 5d 2c 20 30 2c 20 6c 65 6e 2d 70 4d 65  ->n], 0, len-pMe
17c6d 6d 2d 3e 6e 29 3b 0a 20 20 20 20 7d 0a 20 20 20  m->n);.    }.   
17c6e 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20 7d   return len;.  }
17c6f 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6f 72 20 63  ..  /* NULL or c
17c70 6f 6e 73 74 61 6e 74 73 20 30 20 6f 72 20 31 20  onstants 0 or 1 
17c71 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
17c72 0a 0a 2f 2a 0a 2a 2a 20 44 65 73 65 72 69 61 6c  ../*.** Deserial
17c73 69 7a 65 20 74 68 65 20 64 61 74 61 20 62 6c 6f  ize the data blo
17c74 62 20 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20  b pointed to by 
17c75 62 75 66 20 61 73 20 73 65 72 69 61 6c 20 74 79  buf as serial ty
17c76 70 65 20 73 65 72 69 61 6c 5f 74 79 70 65 0a 2a  pe serial_type.*
17c77 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65 20  * and store the 
17c78 72 65 73 75 6c 74 20 69 6e 20 70 4d 65 6d 2e 20  result in pMem. 
17c79 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
17c7a 65 72 20 6f 66 20 62 79 74 65 73 20 72 65 61 64  er of bytes read
17c7b 2e 0a 2a 2f 20 0a 53 51 4c 49 54 45 5f 50 52 49  ..*/ .SQLITE_PRI
17c7c 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
17c7d 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 0a 20  VdbeSerialGet(. 
17c7e 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
17c7f 63 68 61 72 20 2a 62 75 66 2c 20 20 20 20 20 2f  char *buf,     /
17c80 2a 20 42 75 66 66 65 72 20 74 6f 20 64 65 73 65  * Buffer to dese
17c81 72 69 61 6c 69 7a 65 20 66 72 6f 6d 20 2a 2f 0a  rialize from */.
17c82 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79 70    u32 serial_typ
17c83 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
17c84 2f 2a 20 53 65 72 69 61 6c 20 74 79 70 65 20 74  /* Serial type t
17c85 6f 20 64 65 73 65 72 69 61 6c 69 7a 65 20 2a 2f  o deserialize */
17c86 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20 20 20 20  .  Mem *pMem    
17c87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17c88 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
17c89 74 6f 20 77 72 69 74 65 20 76 61 6c 75 65 20 69  to write value i
17c8a 6e 74 6f 20 2a 2f 0a 29 7b 0a 20 20 73 77 69 74  nto */.){.  swit
17c8b 63 68 28 20 73 65 72 69 61 6c 5f 74 79 70 65 20  ch( serial_type 
17c8c 29 7b 0a 20 20 20 20 63 61 73 65 20 31 30 3a 20  ){.    case 10: 
17c8d 20 20 2f 2a 20 52 65 73 65 72 76 65 64 20 66 6f    /* Reserved fo
17c8e 72 20 66 75 74 75 72 65 20 75 73 65 20 2a 2f 0a  r future use */.
17c8f 20 20 20 20 63 61 73 65 20 31 31 3a 20 20 20 2f      case 11:   /
17c90 2a 20 52 65 73 65 72 76 65 64 20 66 6f 72 20 66  * Reserved for f
17c91 75 74 75 72 65 20 75 73 65 20 2a 2f 0a 20 20 20  uture use */.   
17c92 20 63 61 73 65 20 30 3a 20 7b 20 20 2f 2a 20 4e   case 0: {  /* N
17c93 55 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65  ULL */.      pMe
17c94 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 4e  m->flags = MEM_N
17c95 75 6c 6c 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  ull;.      break
17c96 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
17c97 20 31 3a 20 7b 20 2f 2a 20 31 2d 62 79 74 65 20   1: { /* 1-byte 
17c98 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a  signed integer *
17c99 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75 2e  /.      pMem->u.
17c9a 69 20 3d 20 28 73 69 67 6e 65 64 20 63 68 61 72  i = (signed char
17c9b 29 62 75 66 5b 30 5d 3b 0a 20 20 20 20 20 20 70  )buf[0];.      p
17c9c 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
17c9d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
17c9e 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
17c9f 63 61 73 65 20 32 3a 20 7b 20 2f 2a 20 32 2d 62  case 2: { /* 2-b
17ca0 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
17ca1 65 72 20 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d  er */.      pMem
17ca2 2d 3e 75 2e 69 20 3d 20 28 28 28 73 69 67 6e 65  ->u.i = (((signe
17ca3 64 20 63 68 61 72 29 62 75 66 5b 30 5d 29 3c 3c  d char)buf[0])<<
17ca4 38 29 20 7c 20 62 75 66 5b 31 5d 3b 0a 20 20 20  8) | buf[1];.   
17ca5 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d     pMem->flags =
17ca6 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20   MEM_Int;.      
17ca7 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 7d 0a  return 2;.    }.
17ca8 20 20 20 20 63 61 73 65 20 33 3a 20 7b 20 2f 2a      case 3: { /*
17ca9 20 33 2d 62 79 74 65 20 73 69 67 6e 65 64 20 69   3-byte signed i
17caa 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20 20 20 20  nteger */.      
17cab 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28 28 28 73  pMem->u.i = (((s
17cac 69 67 6e 65 64 20 63 68 61 72 29 62 75 66 5b 30  igned char)buf[0
17cad 5d 29 3c 3c 31 36 29 20 7c 20 28 62 75 66 5b 31  ])<<16) | (buf[1
17cae 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 32 5d 3b 0a  ]<<8) | buf[2];.
17caf 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67        pMem->flag
17cb0 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a 20 20 20  s = MEM_Int;.   
17cb1 20 20 20 72 65 74 75 72 6e 20 33 3b 0a 20 20 20     return 3;.   
17cb2 20 7d 0a 20 20 20 20 63 61 73 65 20 34 3a 20 7b   }.    case 4: {
17cb3 20 2f 2a 20 34 2d 62 79 74 65 20 73 69 67 6e 65   /* 4-byte signe
17cb4 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20 20 20  d integer */.   
17cb5 20 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 28     pMem->u.i = (
17cb6 62 75 66 5b 30 5d 3c 3c 32 34 29 20 7c 20 28 62  buf[0]<<24) | (b
17cb7 75 66 5b 31 5d 3c 3c 31 36 29 20 7c 20 28 62 75  uf[1]<<16) | (bu
17cb8 66 5b 32 5d 3c 3c 38 29 20 7c 20 62 75 66 5b 33  f[2]<<8) | buf[3
17cb9 5d 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66  ];.      pMem->f
17cba 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b 0a  lags = MEM_Int;.
17cbb 20 20 20 20 20 20 72 65 74 75 72 6e 20 34 3b 0a        return 4;.
17cbc 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 35      }.    case 5
17cbd 3a 20 7b 20 2f 2a 20 36 2d 62 79 74 65 20 73 69  : { /* 6-byte si
17cbe 67 6e 65 64 20 69 6e 74 65 67 65 72 20 2a 2f 0a  gned integer */.
17cbf 20 20 20 20 20 20 75 36 34 20 78 20 3d 20 28 28        u64 x = ((
17cc0 28 73 69 67 6e 65 64 20 63 68 61 72 29 62 75 66  (signed char)buf
17cc1 5b 30 5d 29 3c 3c 38 29 20 7c 20 62 75 66 5b 31  [0])<<8) | buf[1
17cc2 5d 3b 0a 20 20 20 20 20 20 75 33 32 20 79 20 3d  ];.      u32 y =
17cc3 20 28 62 75 66 5b 32 5d 3c 3c 32 34 29 20 7c 20   (buf[2]<<24) | 
17cc4 28 62 75 66 5b 33 5d 3c 3c 31 36 29 20 7c 20 28  (buf[3]<<16) | (
17cc5 62 75 66 5b 34 5d 3c 3c 38 29 20 7c 20 62 75 66  buf[4]<<8) | buf
17cc6 5b 35 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20 28  [5];.      x = (
17cc7 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20 20  x<<32) | y;.    
17cc8 20 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28    pMem->u.i = *(
17cc9 69 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 70  i64*)&x;.      p
17cca 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
17ccb 5f 49 6e 74 3b 0a 20 20 20 20 20 20 72 65 74 75  _Int;.      retu
17ccc 72 6e 20 36 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 6;.    }.    
17ccd 63 61 73 65 20 36 3a 20 20 20 2f 2a 20 38 2d 62  case 6:   /* 8-b
17cce 79 74 65 20 73 69 67 6e 65 64 20 69 6e 74 65 67  yte signed integ
17ccf 65 72 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 37  er */.    case 7
17cd0 3a 20 7b 20 2f 2a 20 49 45 45 45 20 66 6c 6f 61  : { /* IEEE floa
17cd1 74 69 6e 67 20 70 6f 69 6e 74 20 2a 2f 0a 20 20  ting point */.  
17cd2 20 20 20 20 75 36 34 20 78 3b 0a 20 20 20 20 20      u64 x;.     
17cd3 20 75 33 32 20 79 3b 0a 23 69 66 20 21 64 65 66   u32 y;.#if !def
17cd4 69 6e 65 64 28 4e 44 45 42 55 47 29 20 26 26 20  ined(NDEBUG) && 
17cd5 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
17cd6 4f 4d 49 54 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f  OMIT_FLOATING_PO
17cd7 49 4e 54 29 0a 20 20 20 20 20 20 2f 2a 20 56 65  INT).      /* Ve
17cd8 72 69 66 79 20 74 68 61 74 20 69 6e 74 65 67 65  rify that intege
17cd9 72 73 20 61 6e 64 20 66 6c 6f 61 74 69 6e 67 20  rs and floating 
17cda 70 6f 69 6e 74 20 76 61 6c 75 65 73 20 75 73 65  point values use
17cdb 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20 20 20   the same.      
17cdc 2a 2a 20 62 79 74 65 20 6f 72 64 65 72 2e 20 20  ** byte order.  
17cdd 4f 72 2c 20 74 68 61 74 20 69 66 20 53 51 4c 49  Or, that if SQLI
17cde 54 45 5f 4d 49 58 45 44 5f 45 4e 44 49 41 4e 5f  TE_MIXED_ENDIAN_
17cdf 36 34 42 49 54 5f 46 4c 4f 41 54 20 69 73 0a 20  64BIT_FLOAT is. 
17ce0 20 20 20 20 20 2a 2a 20 64 65 66 69 6e 65 64 20       ** defined 
17ce1 74 68 61 74 20 36 34 2d 62 69 74 20 66 6c 6f 61  that 64-bit floa
17ce2 74 69 6e 67 20 70 6f 69 6e 74 20 76 61 6c 75 65  ting point value
17ce3 73 20 72 65 61 6c 6c 79 20 61 72 65 20 6d 69 78  s really are mix
17ce4 65 64 0a 20 20 20 20 20 20 2a 2a 20 65 6e 64 69  ed.      ** endi
17ce5 61 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  an..      */.   
17ce6 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
17ce7 75 36 34 20 74 31 20 3d 20 28 28 75 36 34 29 30  u64 t1 = ((u64)0
17ce8 78 33 66 66 30 30 30 30 30 29 3c 3c 33 32 3b 0a  x3ff00000)<<32;.
17ce9 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
17cea 73 74 20 64 6f 75 62 6c 65 20 72 31 20 3d 20 31  st double r1 = 1
17ceb 2e 30 3b 0a 20 20 20 20 20 20 75 36 34 20 74 32  .0;.      u64 t2
17cec 20 3d 20 74 31 3b 0a 20 20 20 20 20 20 73 77 61   = t1;.      swa
17ced 70 4d 69 78 65 64 45 6e 64 69 61 6e 46 6c 6f 61  pMixedEndianFloa
17cee 74 28 74 32 29 3b 0a 20 20 20 20 20 20 61 73 73  t(t2);.      ass
17cef 65 72 74 28 20 73 69 7a 65 6f 66 28 72 31 29 3d  ert( sizeof(r1)=
17cf0 3d 73 69 7a 65 6f 66 28 74 32 29 20 26 26 20 6d  =sizeof(t2) && m
17cf1 65 6d 63 6d 70 28 26 72 31 2c 20 26 74 32 2c 20  emcmp(&r1, &t2, 
17cf2 73 69 7a 65 6f 66 28 72 31 29 29 3d 3d 30 20 29  sizeof(r1))==0 )
17cf3 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 20 20  ;.#endif..      
17cf4 78 20 3d 20 28 62 75 66 5b 30 5d 3c 3c 32 34 29  x = (buf[0]<<24)
17cf5 20 7c 20 28 62 75 66 5b 31 5d 3c 3c 31 36 29 20   | (buf[1]<<16) 
17cf6 7c 20 28 62 75 66 5b 32 5d 3c 3c 38 29 20 7c 20  | (buf[2]<<8) | 
17cf7 62 75 66 5b 33 5d 3b 0a 20 20 20 20 20 20 79 20  buf[3];.      y 
17cf8 3d 20 28 62 75 66 5b 34 5d 3c 3c 32 34 29 20 7c  = (buf[4]<<24) |
17cf9 20 28 62 75 66 5b 35 5d 3c 3c 31 36 29 20 7c 20   (buf[5]<<16) | 
17cfa 28 62 75 66 5b 36 5d 3c 3c 38 29 20 7c 20 62 75  (buf[6]<<8) | bu
17cfb 66 5b 37 5d 3b 0a 20 20 20 20 20 20 78 20 3d 20  f[7];.      x = 
17cfc 28 78 3c 3c 33 32 29 20 7c 20 79 3b 0a 20 20 20  (x<<32) | y;.   
17cfd 20 20 20 69 66 28 20 73 65 72 69 61 6c 5f 74 79     if( serial_ty
17cfe 70 65 3d 3d 36 20 29 7b 0a 20 20 20 20 20 20 20  pe==6 ){.       
17cff 20 70 4d 65 6d 2d 3e 75 2e 69 20 3d 20 2a 28 69   pMem->u.i = *(i
17d00 36 34 2a 29 26 78 3b 0a 20 20 20 20 20 20 20 20  64*)&x;.        
17d01 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
17d02 4d 5f 49 6e 74 3b 0a 20 20 20 20 20 20 7d 65 6c  M_Int;.      }el
17d03 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65  se{.        asse
17d04 72 74 28 20 73 69 7a 65 6f 66 28 78 29 3d 3d 38  rt( sizeof(x)==8
17d05 20 26 26 20 73 69 7a 65 6f 66 28 70 4d 65 6d 2d   && sizeof(pMem-
17d06 3e 72 29 3d 3d 38 20 29 3b 0a 20 20 20 20 20 20  >r)==8 );.      
17d07 20 20 73 77 61 70 4d 69 78 65 64 45 6e 64 69 61    swapMixedEndia
17d08 6e 46 6c 6f 61 74 28 78 29 3b 0a 20 20 20 20 20  nFloat(x);.     
17d09 20 20 20 6d 65 6d 63 70 79 28 26 70 4d 65 6d 2d     memcpy(&pMem-
17d0a 3e 72 2c 20 26 78 2c 20 73 69 7a 65 6f 66 28 78  >r, &x, sizeof(x
17d0b 29 29 3b 0a 20 20 20 20 20 20 20 20 70 4d 65 6d  ));.        pMem
17d0c 2d 3e 66 6c 61 67 73 20 3d 20 73 71 6c 69 74 65  ->flags = sqlite
17d0d 33 49 73 4e 61 4e 28 70 4d 65 6d 2d 3e 72 29 20  3IsNaN(pMem->r) 
17d0e 3f 20 4d 45 4d 5f 4e 75 6c 6c 20 3a 20 4d 45 4d  ? MEM_Null : MEM
17d0f 5f 52 65 61 6c 3b 0a 20 20 20 20 20 20 7d 0a 20  _Real;.      }. 
17d10 20 20 20 20 20 72 65 74 75 72 6e 20 38 3b 0a 20       return 8;. 
17d11 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 38 3a     }.    case 8:
17d12 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 30      /* Integer 0
17d13 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 39 3a 20   */.    case 9: 
17d14 7b 20 20 2f 2a 20 49 6e 74 65 67 65 72 20 31 20  {  /* Integer 1 
17d15 2a 2f 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e 75  */.      pMem->u
17d16 2e 69 20 3d 20 73 65 72 69 61 6c 5f 74 79 70 65  .i = serial_type
17d17 2d 38 3b 0a 20 20 20 20 20 20 70 4d 65 6d 2d 3e  -8;.      pMem->
17d18 66 6c 61 67 73 20 3d 20 4d 45 4d 5f 49 6e 74 3b  flags = MEM_Int;
17d19 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
17d1a 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61 75  .    }.    defau
17d1b 6c 74 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20  lt: {.      int 
17d1c 6c 65 6e 20 3d 20 28 73 65 72 69 61 6c 5f 74 79  len = (serial_ty
17d1d 70 65 2d 31 32 29 2f 32 3b 0a 20 20 20 20 20 20  pe-12)/2;.      
17d1e 70 4d 65 6d 2d 3e 7a 20 3d 20 28 63 68 61 72 20  pMem->z = (char 
17d1f 2a 29 62 75 66 3b 0a 20 20 20 20 20 20 70 4d 65  *)buf;.      pMe
17d20 6d 2d 3e 6e 20 3d 20 6c 65 6e 3b 0a 20 20 20 20  m->n = len;.    
17d21 20 20 70 4d 65 6d 2d 3e 78 44 65 6c 20 3d 20 30    pMem->xDel = 0
17d22 3b 0a 20 20 20 20 20 20 69 66 28 20 73 65 72 69  ;.      if( seri
17d23 61 6c 5f 74 79 70 65 26 30 78 30 31 20 29 7b 0a  al_type&0x01 ){.
17d24 20 20 20 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c          pMem->fl
17d25 61 67 73 20 3d 20 4d 45 4d 5f 53 74 72 20 7c 20  ags = MEM_Str | 
17d26 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
17d27 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
17d28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pMem->flags = ME
17d29 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f 45 70 68  M_Blob | MEM_Eph
17d2a 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
17d2b 20 20 72 65 74 75 72 6e 20 6c 65 6e 3b 0a 20 20    return len;.  
17d2c 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
17d2d 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 47 69   0;.}.../*.** Gi
17d2e 76 65 6e 20 74 68 65 20 6e 4b 65 79 2d 62 79 74  ven the nKey-byt
17d2f 65 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20 61 20  e encoding of a 
17d30 72 65 63 6f 72 64 20 69 6e 20 70 4b 65 79 5b 5d  record in pKey[]
17d31 2c 20 70 61 72 73 65 20 74 68 65 0a 2a 2a 20 72  , parse the.** r
17d32 65 63 6f 72 64 20 69 6e 74 6f 20 61 20 55 6e 70  ecord into a Unp
17d33 61 63 6b 65 64 52 65 63 6f 72 64 20 73 74 72 75  ackedRecord stru
17d34 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 61  cture.  Return a
17d35 20 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74   pointer to.** t
17d36 68 61 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a  hat structure..*
17d37 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69 6e 67  *.** The calling
17d38 20 66 75 6e 63 74 69 6f 6e 20 6d 69 67 68 74 20   function might 
17d39 70 72 6f 76 69 64 65 20 73 7a 53 70 61 63 65 20  provide szSpace 
17d3a 62 79 74 65 73 20 6f 66 20 6d 65 6d 6f 72 79 0a  bytes of memory.
17d3b 2a 2a 20 73 70 61 63 65 20 61 74 20 70 53 70 61  ** space at pSpa
17d3c 63 65 2e 20 20 54 68 69 73 20 73 70 61 63 65 20  ce.  This space 
17d3d 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 68  can be used to h
17d3e 6f 6c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64  old the returned
17d3f 0a 2a 2a 20 56 44 62 65 50 61 72 73 65 64 52 65  .** VDbeParsedRe
17d40 63 6f 72 64 20 73 74 72 75 63 74 75 72 65 20 69  cord structure i
17d41 66 20 69 74 20 69 73 20 6c 61 72 67 65 20 65 6e  f it is large en
17d42 6f 75 67 68 2e 20 20 49 66 20 69 74 20 69 73 0a  ough.  If it is.
17d43 2a 2a 20 6e 6f 74 20 62 69 67 20 65 6e 6f 75 67  ** not big enoug
17d44 68 2c 20 73 70 61 63 65 20 69 73 20 6f 62 74 61  h, space is obta
17d45 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
17d46 33 5f 6d 61 6c 6c 6f 63 28 29 2e 0a 2a 2a 0a 2a  3_malloc()..**.*
17d47 2a 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73  * The returned s
17d48 74 72 75 63 74 75 72 65 20 73 68 6f 75 6c 64 20  tructure should 
17d49 62 65 20 63 6c 6f 73 65 64 20 62 79 20 61 20 63  be closed by a c
17d4a 61 6c 6c 20 74 6f 0a 2a 2a 20 73 71 6c 69 74 65  all to.** sqlite
17d4b 33 56 64 62 65 44 65 6c 65 74 65 55 6e 70 61 63  3VdbeDeleteUnpac
17d4c 6b 65 64 52 65 63 6f 72 64 28 29 2e 0a 2a 2f 20  kedRecord()..*/ 
17d4d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
17d4e 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a  UnpackedRecord *
17d4f 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f 72  sqlite3VdbeRecor
17d50 64 55 6e 70 61 63 6b 28 0a 20 20 4b 65 79 49 6e  dUnpack(.  KeyIn
17d51 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 2c 20 20 20  fo *pKeyInfo,   
17d52 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
17d53 20 61 62 6f 75 74 20 74 68 65 20 72 65 63 6f 72   about the recor
17d54 64 20 66 6f 72 6d 61 74 20 2a 2f 0a 20 20 69 6e  d format */.  in
17d55 74 20 6e 4b 65 79 2c 20 20 20 20 20 20 20 20 20  t nKey,         
17d56 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
17d57 74 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f 72  the binary recor
17d58 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69  d */.  const voi
17d59 64 20 2a 70 4b 65 79 2c 20 20 20 20 20 20 2f 2a  d *pKey,      /*
17d5a 20 54 68 65 20 62 69 6e 61 72 79 20 72 65 63 6f   The binary reco
17d5b 72 64 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64  rd */.  Unpacked
17d5c 52 65 63 6f 72 64 20 2a 70 53 70 61 63 65 2c 2f  Record *pSpace,/
17d5d 2a 20 53 70 61 63 65 20 61 76 61 69 6c 61 62 6c  * Space availabl
17d5e 65 20 74 6f 20 68 6f 6c 64 20 72 65 73 75 6c 74  e to hold result
17d5f 69 6e 67 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20  ing object */.  
17d60 69 6e 74 20 73 7a 53 70 61 63 65 20 20 20 20 20  int szSpace     
17d61 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
17d62 66 20 70 53 70 61 63 65 5b 5d 20 69 6e 20 62 79  f pSpace[] in by
17d63 74 65 73 20 2a 2f 0a 29 7b 0a 20 20 63 6f 6e 73  tes */.){.  cons
17d64 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
17d65 2a 61 4b 65 79 20 3d 20 28 63 6f 6e 73 74 20 75  *aKey = (const u
17d66 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
17d67 4b 65 79 3b 0a 20 20 55 6e 70 61 63 6b 65 64 52  Key;.  UnpackedR
17d68 65 63 6f 72 64 20 2a 70 3b 0a 20 20 69 6e 74 20  ecord *p;.  int 
17d69 6e 42 79 74 65 2c 20 64 3b 0a 20 20 75 33 32 20  nByte, d;.  u32 
17d6a 69 64 78 3b 0a 20 20 75 31 36 20 75 3b 20 20 20  idx;.  u16 u;   
17d6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
17d6c 20 55 6e 73 69 67 6e 65 64 20 6c 6f 6f 70 20 63   Unsigned loop c
17d6d 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 75 33 32 20  ounter */.  u32 
17d6e 73 7a 48 64 72 3b 0a 20 20 4d 65 6d 20 2a 70 4d  szHdr;.  Mem *pM
17d6f 65 6d 3b 0a 20 20 0a 20 20 61 73 73 65 72 74 28  em;.  .  assert(
17d70 20 73 69 7a 65 6f 66 28 4d 65 6d 29 3e 73 69 7a   sizeof(Mem)>siz
17d71 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 6e 42 79  eof(*p) );.  nBy
17d72 74 65 20 3d 20 73 69 7a 65 6f 66 28 4d 65 6d 29  te = sizeof(Mem)
17d73 2a 28 70 4b 65 79 49 6e 66 6f 2d 3e 6e 46 69 65  *(pKeyInfo->nFie
17d74 6c 64 2b 32 29 3b 0a 20 20 69 66 28 20 6e 42 79  ld+2);.  if( nBy
17d75 74 65 3e 73 7a 53 70 61 63 65 20 29 7b 0a 20 20  te>szSpace ){.  
17d76 20 20 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d    p = sqlite3DbM
17d77 61 6c 6c 6f 63 52 61 77 28 70 4b 65 79 49 6e 66  allocRaw(pKeyInf
17d78 6f 2d 3e 64 62 2c 20 6e 42 79 74 65 29 3b 0a 20  o->db, nByte);. 
17d79 20 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65     if( p==0 ) re
17d7a 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 2d 3e 66  turn 0;.    p->f
17d7b 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
17d7c 4e 45 45 44 5f 46 52 45 45 20 7c 20 55 4e 50 41  NEED_FREE | UNPA
17d7d 43 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f  CKED_NEED_DESTRO
17d7e 59 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  Y;.  }else{.    
17d7f 70 20 3d 20 70 53 70 61 63 65 3b 0a 20 20 20 20  p = pSpace;.    
17d80 70 2d 3e 66 6c 61 67 73 20 3d 20 55 4e 50 41 43  p->flags = UNPAC
17d81 4b 45 44 5f 4e 45 45 44 5f 44 45 53 54 52 4f 59  KED_NEED_DESTROY
17d82 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4b 65 79 49  ;.  }.  p->pKeyI
17d83 6e 66 6f 20 3d 20 70 4b 65 79 49 6e 66 6f 3b 0a  nfo = pKeyInfo;.
17d84 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 70 4b    p->nField = pK
17d85 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 2b  eyInfo->nField +
17d86 20 31 3b 0a 20 20 70 2d 3e 61 4d 65 6d 20 3d 20   1;.  p->aMem = 
17d87 70 4d 65 6d 20 3d 20 26 28 28 4d 65 6d 2a 29 70  pMem = &((Mem*)p
17d88 29 5b 31 5d 3b 0a 20 20 69 64 78 20 3d 20 67 65  )[1];.  idx = ge
17d89 74 56 61 72 69 6e 74 33 32 28 61 4b 65 79 2c 20  tVarint32(aKey, 
17d8a 73 7a 48 64 72 29 3b 0a 20 20 64 20 3d 20 73 7a  szHdr);.  d = sz
17d8b 48 64 72 3b 0a 20 20 75 20 3d 20 30 3b 0a 20 20  Hdr;.  u = 0;.  
17d8c 77 68 69 6c 65 28 20 69 64 78 3c 73 7a 48 64 72  while( idx<szHdr
17d8d 20 26 26 20 75 3c 70 2d 3e 6e 46 69 65 6c 64 20   && u<p->nField 
17d8e 29 7b 0a 20 20 20 20 75 33 32 20 73 65 72 69 61  ){.    u32 seria
17d8f 6c 5f 74 79 70 65 3b 0a 0a 20 20 20 20 69 64 78  l_type;..    idx
17d90 20 2b 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28   += getVarint32(
17d91 26 61 4b 65 79 5b 69 64 78 5d 2c 20 73 65 72 69  &aKey[idx], seri
17d92 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20 69 66  al_type);.    if
17d93 28 20 64 3e 3d 6e 4b 65 79 20 26 26 20 73 71 6c  ( d>=nKey && sql
17d94 69 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79  ite3VdbeSerialTy
17d95 70 65 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70  peLen(serial_typ
17d96 65 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20  e)>0 ) break;.  
17d97 20 20 70 4d 65 6d 2d 3e 65 6e 63 20 3d 20 70 4b    pMem->enc = pK
17d98 65 79 49 6e 66 6f 2d 3e 65 6e 63 3b 0a 20 20 20  eyInfo->enc;.   
17d99 20 70 4d 65 6d 2d 3e 64 62 20 3d 20 70 4b 65 79   pMem->db = pKey
17d9a 49 6e 66 6f 2d 3e 64 62 3b 0a 20 20 20 20 70 4d  Info->db;.    pM
17d9b 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 30 3b 0a 20  em->flags = 0;. 
17d9c 20 20 20 70 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63     pMem->zMalloc
17d9d 20 3d 20 30 3b 0a 20 20 20 20 64 20 2b 3d 20 73   = 0;.    d += s
17d9e 71 6c 69 74 65 33 56 64 62 65 53 65 72 69 61 6c  qlite3VdbeSerial
17d9f 47 65 74 28 26 61 4b 65 79 5b 64 5d 2c 20 73 65  Get(&aKey[d], se
17da0 72 69 61 6c 5f 74 79 70 65 2c 20 70 4d 65 6d 29  rial_type, pMem)
17da1 3b 0a 20 20 20 20 70 4d 65 6d 2b 2b 3b 0a 20 20  ;.    pMem++;.  
17da2 20 20 75 2b 2b 3b 0a 20 20 7d 0a 20 20 61 73 73    u++;.  }.  ass
17da3 65 72 74 28 20 75 3c 3d 70 4b 65 79 49 6e 66 6f  ert( u<=pKeyInfo
17da4 2d 3e 6e 46 69 65 6c 64 20 2b 20 31 20 29 3b 0a  ->nField + 1 );.
17da5 20 20 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 75 3b    p->nField = u;
17da6 0a 20 20 72 65 74 75 72 6e 20 28 76 6f 69 64 2a  .  return (void*
17da7 29 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  )p;.}../*.** Thi
17da8 73 20 72 6f 75 74 69 6e 65 20 64 65 73 74 72 6f  s routine destro
17da9 79 73 20 61 20 55 6e 70 61 63 6b 65 64 52 65 63  ys a UnpackedRec
17daa 6f 72 64 20 6f 62 6a 65 63 74 0a 2a 2f 0a 53 51  ord object.*/.SQ
17dab 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
17dac 64 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c  d sqlite3VdbeDel
17dad 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  eteUnpackedRecor
17dae 64 28 55 6e 70 61 63 6b 65 64 52 65 63 6f 72 64  d(UnpackedRecord
17daf 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
17db0 0a 20 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  .    if( p->flag
17db1 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45  s & UNPACKED_NEE
17db2 44 5f 44 45 53 54 52 4f 59 20 29 7b 0a 20 20 20  D_DESTROY ){.   
17db3 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
17db4 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 20 20 20  Mem *pMem;.     
17db5 20 66 6f 72 28 69 3d 30 2c 20 70 4d 65 6d 3d 70   for(i=0, pMem=p
17db6 2d 3e 61 4d 65 6d 3b 20 69 3c 70 2d 3e 6e 46 69  ->aMem; i<p->nFi
17db7 65 6c 64 3b 20 69 2b 2b 2c 20 70 4d 65 6d 2b 2b  eld; i++, pMem++
17db8 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
17db9 4d 65 6d 2d 3e 7a 4d 61 6c 6c 6f 63 20 29 7b 0a  Mem->zMalloc ){.
17dba 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
17dbb 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
17dbc 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20 20 7d  pMem);.        }
17dbd 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
17dbe 20 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20     if( p->flags 
17dbf 26 20 55 4e 50 41 43 4b 45 44 5f 4e 45 45 44 5f  & UNPACKED_NEED_
17dc0 46 52 45 45 20 29 7b 0a 20 20 20 20 20 20 73 71  FREE ){.      sq
17dc1 6c 69 74 65 33 44 62 46 72 65 65 28 70 2d 3e 70  lite3DbFree(p->p
17dc2 4b 65 79 49 6e 66 6f 2d 3e 64 62 2c 20 70 29 3b  KeyInfo->db, p);
17dc3 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
17dc4 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
17dc5 6e 20 63 6f 6d 70 61 72 65 73 20 74 68 65 20 74  n compares the t
17dc6 77 6f 20 74 61 62 6c 65 20 72 6f 77 73 20 6f 72  wo table rows or
17dc7 20 69 6e 64 65 78 20 72 65 63 6f 72 64 73 0a 2a   index records.*
17dc8 2a 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7b  * specified by {
17dc9 6e 4b 65 79 31 2c 20 70 4b 65 79 31 7d 20 61 6e  nKey1, pKey1} an
17dca 64 20 70 50 4b 65 79 32 2e 20 20 49 74 20 72 65  d pPKey2.  It re
17dcb 74 75 72 6e 73 20 61 20 6e 65 67 61 74 69 76 65  turns a negative
17dcc 2c 20 7a 65 72 6f 0a 2a 2a 20 6f 72 20 70 6f 73  , zero.** or pos
17dcd 69 74 69 76 65 20 69 6e 74 65 67 65 72 20 69 66  itive integer if
17dce 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74 68   key1 is less th
17dcf 61 6e 2c 20 65 71 75 61 6c 20 74 6f 20 6f 72 20  an, equal to or 
17dd0 0a 2a 2a 20 67 72 65 61 74 65 72 20 74 68 61 6e  .** greater than
17dd1 20 6b 65 79 32 2e 20 20 54 68 65 20 7b 6e 4b 65   key2.  The {nKe
17dd2 79 31 2c 20 70 4b 65 79 31 7d 20 6b 65 79 20 6d  y1, pKey1} key m
17dd3 75 73 74 20 62 65 20 61 20 62 6c 6f 62 0a 2a 2a  ust be a blob.**
17dd4 20 63 72 65 61 74 65 64 20 62 79 20 74 68 20 4f   created by th O
17dd5 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 6f 70 63  P_MakeRecord opc
17dd6 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45 2e  ode of the VDBE.
17dd7 20 20 54 68 65 20 70 50 4b 65 79 32 0a 2a 2a 20    The pPKey2.** 
17dd8 6b 65 79 20 6d 75 73 74 20 62 65 20 61 20 70 61  key must be a pa
17dd9 72 73 65 64 20 6b 65 79 20 73 75 63 68 20 61 73  rsed key such as
17dda 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 0a 2a   obtained from.*
17ddb 2a 20 73 71 6c 69 74 65 33 56 64 62 65 50 61 72  * sqlite3VdbePar
17ddc 73 65 52 65 63 6f 72 64 2e 0a 2a 2a 0a 2a 2a 20  seRecord..**.** 
17ddd 4b 65 79 31 20 61 6e 64 20 4b 65 79 32 20 64 6f  Key1 and Key2 do
17dde 20 6e 6f 74 20 68 61 76 65 20 74 6f 20 63 6f 6e   not have to con
17ddf 74 61 69 6e 20 74 68 65 20 73 61 6d 65 20 6e 75  tain the same nu
17de0 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73 2e 0a  mber of fields..
17de1 2a 2a 20 54 68 65 20 6b 65 79 20 77 69 74 68 20  ** The key with 
17de2 66 65 77 65 72 20 66 69 65 6c 64 73 20 69 73 20  fewer fields is 
17de3 75 73 75 61 6c 6c 79 20 63 6f 6d 70 61 72 65 73  usually compares
17de4 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 0a   less than the .
17de5 2a 2a 20 6c 6f 6e 67 65 72 20 6b 65 79 2e 20 20  ** longer key.  
17de6 48 6f 77 65 76 65 72 20 69 66 20 74 68 65 20 55  However if the U
17de7 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 20  NPACKED_INCRKEY 
17de8 66 6c 61 67 73 20 69 6e 20 70 50 4b 65 79 32 20  flags in pPKey2 
17de9 69 73 20 73 65 74 0a 2a 2a 20 61 6e 64 20 74 68  is set.** and th
17dea 65 20 63 6f 6d 6d 6f 6e 20 70 72 65 66 69 78 65  e common prefixe
17deb 73 20 61 72 65 20 65 71 75 61 6c 2c 20 74 68 65  s are equal, the
17dec 6e 20 6b 65 79 31 20 69 73 20 6c 65 73 73 20 74  n key1 is less t
17ded 68 61 6e 20 6b 65 79 32 2e 0a 2a 2a 20 4f 72 20  han key2..** Or 
17dee 69 66 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f  if the UNPACKED_
17def 4d 41 54 43 48 5f 50 52 45 46 49 58 20 66 6c 61  MATCH_PREFIX fla
17df0 67 20 69 73 20 73 65 74 20 61 6e 64 20 74 68 65  g is set and the
17df1 20 70 72 65 66 69 78 65 73 20 61 72 65 0a 2a 2a   prefixes are.**
17df2 20 65 71 75 61 6c 2c 20 74 68 65 6e 20 74 68 65   equal, then the
17df3 20 6b 65 79 73 20 61 72 65 20 63 6f 6e 73 69 64   keys are consid
17df4 65 72 65 64 20 74 6f 20 62 65 20 65 71 75 61 6c  ered to be equal
17df5 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72 74   and.** the part
17df6 73 20 62 65 79 6f 6e 64 20 74 68 65 20 63 6f 6d  s beyond the com
17df7 6d 6f 6e 20 70 72 65 66 69 78 20 61 72 65 20 69  mon prefix are i
17df8 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66  gnored..**.** If
17df9 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 47   the UNPACKED_IG
17dfa 4e 4f 52 45 5f 52 4f 57 49 44 20 66 6c 61 67 20  NORE_ROWID flag 
17dfb 69 73 20 73 65 74 2c 20 74 68 65 6e 20 74 68 65  is set, then the
17dfc 20 6c 61 73 74 20 62 79 74 65 20 6f 66 0a 2a 2a   last byte of.**
17dfd 20 74 68 65 20 68 65 61 64 65 72 20 6f 66 20 70   the header of p
17dfe 4b 65 79 31 20 69 73 20 69 67 6e 6f 72 65 64 2e  Key1 is ignored.
17dff 20 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20    It is assumed 
17e00 74 68 61 74 20 70 4b 65 79 31 20 69 73 0a 2a 2a  that pKey1 is.**
17e01 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 2c 20 61   an index key, a
17e02 6e 64 20 74 68 75 73 20 65 6e 64 73 20 77 69 74  nd thus ends wit
17e03 68 20 61 20 72 6f 77 69 64 20 76 61 6c 75 65 2e  h a rowid value.
17e04 20 20 54 68 65 20 6c 61 73 74 20 62 79 74 65 0a    The last byte.
17e05 2a 2a 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  ** of the header
17e06 20 77 69 6c 6c 20 74 68 65 72 65 66 6f 72 65 20   will therefore 
17e07 62 65 20 74 68 65 20 73 65 72 69 61 6c 20 74 79  be the serial ty
17e08 70 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 3a  pe of the rowid:
17e09 0a 2a 2a 20 6f 6e 65 20 6f 66 20 31 2c 20 32 2c  .** one of 1, 2,
17e0a 20 33 2c 20 34 2c 20 35 2c 20 36 2c 20 38 2c 20   3, 4, 5, 6, 8, 
17e0b 6f 72 20 39 20 2d 20 74 68 65 20 69 6e 74 65 67  or 9 - the integ
17e0c 65 72 20 73 65 72 69 61 6c 20 74 79 70 65 73 2e  er serial types.
17e0d 0a 2a 2a 20 54 68 65 20 73 65 72 69 61 6c 20 74  .** The serial t
17e0e 79 70 65 20 6f 66 20 74 68 65 20 66 69 6e 61 6c  ype of the final
17e0f 20 72 6f 77 69 64 20 77 69 6c 6c 20 61 6c 77 61   rowid will alwa
17e10 79 73 20 62 65 20 61 20 73 69 6e 67 6c 65 20 62  ys be a single b
17e11 79 74 65 2e 0a 2a 2a 20 42 79 20 69 67 6e 6f 72  yte..** By ignor
17e12 69 6e 67 20 74 68 69 73 20 6c 61 73 74 20 62 79  ing this last by
17e13 74 65 20 6f 66 20 74 68 65 20 68 65 61 64 65 72  te of the header
17e14 2c 20 77 65 20 66 6f 72 63 65 20 74 68 65 20 63  , we force the c
17e15 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 74 6f 20  omparison.** to 
17e16 69 67 6e 6f 72 65 20 74 68 65 20 72 6f 77 69 64  ignore the rowid
17e17 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 6b   at the end of k
17e18 65 79 31 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ey1..*/.SQLITE_P
17e19 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
17e1a 65 33 56 64 62 65 52 65 63 6f 72 64 43 6f 6d 70  e3VdbeRecordComp
17e1b 61 72 65 28 0a 20 20 69 6e 74 20 6e 4b 65 79 31  are(.  int nKey1
17e1c 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
17e1d 65 79 31 2c 20 2f 2a 20 4c 65 66 74 20 6b 65 79  ey1, /* Left key
17e1e 20 2a 2f 0a 20 20 55 6e 70 61 63 6b 65 64 52 65   */.  UnpackedRe
17e1f 63 6f 72 64 20 2a 70 50 4b 65 79 32 20 20 20 20  cord *pPKey2    
17e20 20 20 20 20 2f 2a 20 52 69 67 68 74 20 6b 65 79      /* Right key
17e21 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 64 31 3b   */.){.  int d1;
17e22 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
17e23 66 66 73 65 74 20 69 6e 74 6f 20 61 4b 65 79 5b  ffset into aKey[
17e24 5d 20 6f 66 20 6e 65 78 74 20 64 61 74 61 20 65  ] of next data e
17e25 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 75 33 32 20  lement */.  u32 
17e26 69 64 78 31 3b 20 20 20 20 20 20 20 20 20 20 2f  idx1;          /
17e27 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f 20 61 4b  * Offset into aK
17e28 65 79 5b 5d 20 6f 66 20 6e 65 78 74 20 68 65 61  ey[] of next hea
17e29 64 65 72 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  der element */. 
17e2a 20 75 33 32 20 73 7a 48 64 72 31 3b 20 20 20 20   u32 szHdr1;    
17e2b 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
17e2c 20 62 79 74 65 73 20 69 6e 20 68 65 61 64 65 72   bytes in header
17e2d 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 30 3b   */.  int i = 0;
17e2e 0a 20 20 69 6e 74 20 6e 46 69 65 6c 64 3b 0a 20  .  int nField;. 
17e2f 20 69 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63   int rc = 0;.  c
17e30 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
17e31 61 72 20 2a 61 4b 65 79 31 20 3d 20 28 63 6f 6e  ar *aKey1 = (con
17e32 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
17e33 20 2a 29 70 4b 65 79 31 3b 0a 20 20 4b 65 79 49   *)pKey1;.  KeyI
17e34 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
17e35 20 4d 65 6d 20 6d 65 6d 31 3b 0a 0a 20 20 70 4b   Mem mem1;..  pK
17e36 65 79 49 6e 66 6f 20 3d 20 70 50 4b 65 79 32 2d  eyInfo = pPKey2-
17e37 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 6d 65 6d  >pKeyInfo;.  mem
17e38 31 2e 65 6e 63 20 3d 20 70 4b 65 79 49 6e 66 6f  1.enc = pKeyInfo
17e39 2d 3e 65 6e 63 3b 0a 20 20 6d 65 6d 31 2e 64 62  ->enc;.  mem1.db
17e3a 20 3d 20 70 4b 65 79 49 6e 66 6f 2d 3e 64 62 3b   = pKeyInfo->db;
17e3b 0a 20 20 6d 65 6d 31 2e 66 6c 61 67 73 20 3d 20  .  mem1.flags = 
17e3c 30 3b 0a 20 20 6d 65 6d 31 2e 7a 4d 61 6c 6c 6f  0;.  mem1.zMallo
17e3d 63 20 3d 20 30 3b 0a 20 20 0a 20 20 69 64 78 31  c = 0;.  .  idx1
17e3e 20 3d 20 67 65 74 56 61 72 69 6e 74 33 32 28 61   = getVarint32(a
17e3f 4b 65 79 31 2c 20 73 7a 48 64 72 31 29 3b 0a 20  Key1, szHdr1);. 
17e40 20 64 31 20 3d 20 73 7a 48 64 72 31 3b 0a 20 20   d1 = szHdr1;.  
17e41 69 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67  if( pPKey2->flag
17e42 73 20 26 20 55 4e 50 41 43 4b 45 44 5f 49 47 4e  s & UNPACKED_IGN
17e43 4f 52 45 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20  ORE_ROWID ){.   
17e44 20 73 7a 48 64 72 31 2d 2d 3b 0a 20 20 7d 0a 20   szHdr1--;.  }. 
17e45 20 6e 46 69 65 6c 64 20 3d 20 70 4b 65 79 49 6e   nField = pKeyIn
17e46 66 6f 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20 77 68  fo->nField;.  wh
17e47 69 6c 65 28 20 69 64 78 31 3c 73 7a 48 64 72 31  ile( idx1<szHdr1
17e48 20 26 26 20 69 3c 70 50 4b 65 79 32 2d 3e 6e 46   && i<pPKey2->nF
17e49 69 65 6c 64 20 29 7b 0a 20 20 20 20 75 33 32 20  ield ){.    u32 
17e4a 73 65 72 69 61 6c 5f 74 79 70 65 31 3b 0a 0a 20  serial_type1;.. 
17e4b 20 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73     /* Read the s
17e4c 65 72 69 61 6c 20 74 79 70 65 73 20 66 6f 72 20  erial types for 
17e4d 74 68 65 20 6e 65 78 74 20 65 6c 65 6d 65 6e 74  the next element
17e4e 20 69 6e 20 65 61 63 68 20 6b 65 79 2e 20 2a 2f   in each key. */
17e4f 0a 20 20 20 20 69 64 78 31 20 2b 3d 20 67 65 74  .    idx1 += get
17e50 56 61 72 69 6e 74 33 32 28 20 61 4b 65 79 31 2b  Varint32( aKey1+
17e51 69 64 78 31 2c 20 73 65 72 69 61 6c 5f 74 79 70  idx1, serial_typ
17e52 65 31 20 29 3b 0a 20 20 20 20 69 66 28 20 64 31  e1 );.    if( d1
17e53 3e 3d 6e 4b 65 79 31 20 26 26 20 73 71 6c 69 74  >=nKey1 && sqlit
17e54 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70 65  e3VdbeSerialType
17e55 4c 65 6e 28 73 65 72 69 61 6c 5f 74 79 70 65 31  Len(serial_type1
17e56 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 0a 20 20  )>0 ) break;..  
17e57 20 20 2f 2a 20 45 78 74 72 61 63 74 20 74 68 65    /* Extract the
17e58 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 63 6f   values to be co
17e59 6d 70 61 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  mpared..    */. 
17e5a 20 20 20 64 31 20 2b 3d 20 73 71 6c 69 74 65 33     d1 += sqlite3
17e5b 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 26 61  VdbeSerialGet(&a
17e5c 4b 65 79 31 5b 64 31 5d 2c 20 73 65 72 69 61 6c  Key1[d1], serial
17e5d 5f 74 79 70 65 31 2c 20 26 6d 65 6d 31 29 3b 0a  _type1, &mem1);.
17e5e 0a 20 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 63  .    /* Do the c
17e5f 6f 6d 70 61 72 69 73 6f 6e 0a 20 20 20 20 2a 2f  omparison.    */
17e60 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
17e61 33 4d 65 6d 43 6f 6d 70 61 72 65 28 26 6d 65 6d  3MemCompare(&mem
17e62 31 2c 20 26 70 50 4b 65 79 32 2d 3e 61 4d 65 6d  1, &pPKey2->aMem
17e63 5b 69 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20  [i],.           
17e64 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17e65 69 3c 6e 46 69 65 6c 64 20 3f 20 70 4b 65 79 49  i<nField ? pKeyI
17e66 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 20 3a 20  nfo->aColl[i] : 
17e67 30 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  0);.    if( rc!=
17e68 30 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 ){.      break
17e69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 2b 2b 3b  ;.    }.    i++;
17e6a 0a 20 20 7d 0a 20 20 69 66 28 20 6d 65 6d 31 2e  .  }.  if( mem1.
17e6b 7a 4d 61 6c 6c 6f 63 20 29 20 73 71 6c 69 74 65  zMalloc ) sqlite
17e6c 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 28  3VdbeMemRelease(
17e6d 26 6d 65 6d 31 29 3b 0a 0a 20 20 69 66 28 20 72  &mem1);..  if( r
17e6e 63 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 72  c==0 ){.    /* r
17e6f 63 3d 3d 30 20 68 65 72 65 20 6d 65 61 6e 73 20  c==0 here means 
17e70 74 68 61 74 20 6f 6e 65 20 6f 66 20 74 68 65 20  that one of the 
17e71 6b 65 79 73 20 72 61 6e 20 6f 75 74 20 6f 66 20  keys ran out of 
17e72 66 69 65 6c 64 73 20 61 6e 64 0a 20 20 20 20 2a  fields and.    *
17e73 2a 20 61 6c 6c 20 74 68 65 20 66 69 65 6c 64 73  * all the fields
17e74 20 75 70 20 74 6f 20 74 68 61 74 20 70 6f 69 6e   up to that poin
17e75 74 20 77 65 72 65 20 65 71 75 61 6c 2e 20 49 66  t were equal. If
17e76 20 74 68 65 20 55 4e 50 41 43 4b 45 44 5f 49 4e   the UNPACKED_IN
17e77 43 52 4b 45 59 0a 20 20 20 20 2a 2a 20 66 6c 61  CRKEY.    ** fla
17e78 67 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 62  g is set, then b
17e79 72 65 61 6b 20 74 68 65 20 74 69 65 20 62 79 20  reak the tie by 
17e7a 74 72 65 61 74 69 6e 67 20 6b 65 79 32 20 61 73  treating key2 as
17e7b 20 6c 61 72 67 65 72 2e 0a 20 20 20 20 2a 2a 20   larger..    ** 
17e7c 49 66 20 74 68 65 20 55 50 41 43 4b 45 44 5f 50  If the UPACKED_P
17e7d 52 45 46 49 58 5f 4d 41 54 43 48 20 66 6c 61 67  REFIX_MATCH flag
17e7e 20 69 73 20 73 65 74 2c 20 74 68 65 6e 20 6b 65   is set, then ke
17e7f 79 73 20 77 69 74 68 20 63 6f 6d 6d 6f 6e 20 70  ys with common p
17e80 72 65 66 69 78 65 73 0a 20 20 20 20 2a 2a 20 61  refixes.    ** a
17e81 72 65 20 63 6f 6e 73 69 64 65 72 65 64 20 74 6f  re considered to
17e82 20 62 65 20 65 71 75 61 6c 2e 20 20 4f 74 68 65   be equal.  Othe
17e83 72 77 69 73 65 2c 20 74 68 65 20 6c 6f 6e 67 65  rwise, the longe
17e84 72 20 6b 65 79 20 69 73 20 74 68 65 20 0a 20 20  r key is the .  
17e85 20 20 2a 2a 20 6c 61 72 67 65 72 2e 20 20 41 73    ** larger.  As
17e86 20 69 74 20 68 61 70 70 65 6e 73 2c 20 74 68 65   it happens, the
17e87 20 70 50 4b 65 79 32 20 77 69 6c 6c 20 61 6c 77   pPKey2 will alw
17e88 61 79 73 20 62 65 20 74 68 65 20 6c 6f 6e 67 65  ays be the longe
17e89 72 0a 20 20 20 20 2a 2a 20 69 66 20 74 68 65 72  r.    ** if ther
17e8a 65 20 69 73 20 61 20 64 69 66 66 65 72 65 6e 63  e is a differenc
17e8b 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  e..    */.    if
17e8c 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73 20  ( pPKey2->flags 
17e8d 26 20 55 4e 50 41 43 4b 45 44 5f 49 4e 43 52 4b  & UNPACKED_INCRK
17e8e 45 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  EY ){.      rc =
17e8f 20 2d 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69   -1;.    }else i
17e90 66 28 20 70 50 4b 65 79 32 2d 3e 66 6c 61 67 73  f( pPKey2->flags
17e91 20 26 20 55 4e 50 41 43 4b 45 44 5f 50 52 45 46   & UNPACKED_PREF
17e92 49 58 5f 4d 41 54 43 48 20 29 7b 0a 20 20 20 20  IX_MATCH ){.    
17e93 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 3d 3d 30    /* Leave rc==0
17e94 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   */.    }else if
17e95 28 20 69 64 78 31 3c 73 7a 48 64 72 31 20 29 7b  ( idx1<szHdr1 ){
17e96 0a 20 20 20 20 20 20 72 63 20 3d 20 31 3b 0a 20  .      rc = 1;. 
17e97 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66 28     }.  }else if(
17e98 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74   pKeyInfo->aSort
17e99 4f 72 64 65 72 20 26 26 20 69 3c 70 4b 65 79 49  Order && i<pKeyI
17e9a 6e 66 6f 2d 3e 6e 46 69 65 6c 64 0a 20 20 20 20  nfo->nField.    
17e9b 20 20 20 20 20 20 20 20 20 20 20 26 26 20 70 4b             && pK
17e9c 65 79 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64  eyInfo->aSortOrd
17e9d 65 72 5b 69 5d 20 29 7b 0a 20 20 20 20 72 63 20  er[i] ){.    rc 
17e9e 3d 20 2d 72 63 3b 0a 20 20 7d 0a 0a 20 20 72 65  = -rc;.  }..  re
17e9f 74 75 72 6e 20 72 63 3b 0a 7d 0a 20 0a 0a 2f 2a  turn rc;.}. ../*
17ea0 0a 2a 2a 20 70 43 75 72 20 70 6f 69 6e 74 73 20  .** pCur points 
17ea1 61 74 20 61 6e 20 69 6e 64 65 78 20 65 6e 74 72  at an index entr
17ea2 79 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20  y created using 
17ea3 74 68 65 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72  the OP_MakeRecor
17ea4 64 20 6f 70 63 6f 64 65 2e 0a 2a 2a 20 52 65 61  d opcode..** Rea
17ea5 64 20 74 68 65 20 72 6f 77 69 64 20 28 74 68 65  d the rowid (the
17ea6 20 6c 61 73 74 20 66 69 65 6c 64 20 69 6e 20 74   last field in t
17ea7 68 65 20 72 65 63 6f 72 64 29 20 61 6e 64 20 73  he record) and s
17ea8 74 6f 72 65 20 69 74 20 69 6e 20 2a 72 6f 77 69  tore it in *rowi
17ea9 64 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c  d..** Return SQL
17eaa 49 54 45 5f 4f 4b 20 69 66 20 65 76 65 72 79 74  ITE_OK if everyt
17eab 68 69 6e 67 20 77 6f 72 6b 73 2c 20 6f 72 20 61  hing works, or a
17eac 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f 74 68  n error code oth
17ead 65 72 77 69 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54  erwise..*/.SQLIT
17eae 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
17eaf 6c 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69  lite3VdbeIdxRowi
17eb0 64 28 42 74 43 75 72 73 6f 72 20 2a 70 43 75 72  d(BtCursor *pCur
17eb1 2c 20 69 36 34 20 2a 72 6f 77 69 64 29 7b 0a 20  , i64 *rowid){. 
17eb2 20 69 36 34 20 6e 43 65 6c 6c 4b 65 79 20 3d 20   i64 nCellKey = 
17eb3 30 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 75  0;.  int rc;.  u
17eb4 33 32 20 73 7a 48 64 72 3b 20 20 20 20 20 20 20  32 szHdr;       
17eb5 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
17eb6 68 65 61 64 65 72 20 2a 2f 0a 20 20 75 33 32 20  header */.  u32 
17eb7 74 79 70 65 52 6f 77 69 64 3b 20 20 20 20 2f 2a  typeRowid;    /*
17eb8 20 53 65 72 69 61 6c 20 74 79 70 65 20 6f 66 20   Serial type of 
17eb9 74 68 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 75  the rowid */.  u
17eba 33 32 20 6c 65 6e 52 6f 77 69 64 3b 20 20 20 20  32 lenRowid;    
17ebb 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20   /* Size of the 
17ebc 72 6f 77 69 64 20 2a 2f 0a 20 20 4d 65 6d 20 6d  rowid */.  Mem m
17ebd 2c 20 76 3b 0a 0a 20 20 73 71 6c 69 74 65 33 42  , v;..  sqlite3B
17ebe 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 75 72  treeKeySize(pCur
17ebf 2c 20 26 6e 43 65 6c 6c 4b 65 79 29 3b 0a 20 20  , &nCellKey);.  
17ec0 69 66 28 20 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20  if( nCellKey<=0 
17ec1 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
17ec2 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50  LITE_CORRUPT_BKP
17ec3 54 3b 0a 20 20 7d 0a 20 20 6d 2e 66 6c 61 67 73  T;.  }.  m.flags
17ec4 20 3d 20 30 3b 0a 20 20 6d 2e 64 62 20 3d 20 30   = 0;.  m.db = 0
17ec5 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20  ;.  m.zMalloc = 
17ec6 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
17ec7 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
17ec8 65 28 70 43 75 72 2c 20 30 2c 20 6e 43 65 6c 6c  e(pCur, 0, nCell
17ec9 4b 65 79 2c 20 31 2c 20 26 6d 29 3b 0a 20 20 69  Key, 1, &m);.  i
17eca 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72 65 74  f( rc ){.    ret
17ecb 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 28 76  urn rc;.  }.  (v
17ecc 6f 69 64 29 67 65 74 56 61 72 69 6e 74 33 32 28  oid)getVarint32(
17ecd 28 75 38 2a 29 6d 2e 7a 2c 20 73 7a 48 64 72 29  (u8*)m.z, szHdr)
17ece 3b 0a 20 20 28 76 6f 69 64 29 67 65 74 56 61 72  ;.  (void)getVar
17ecf 69 6e 74 33 32 28 28 75 38 2a 29 26 6d 2e 7a 5b  int32((u8*)&m.z[
17ed0 73 7a 48 64 72 2d 31 5d 2c 20 74 79 70 65 52 6f  szHdr-1], typeRo
17ed1 77 69 64 29 3b 0a 20 20 6c 65 6e 52 6f 77 69 64  wid);.  lenRowid
17ed2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65   = sqlite3VdbeSe
17ed3 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79 70 65  rialTypeLen(type
17ed4 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65  Rowid);.  sqlite
17ed5 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28  3VdbeSerialGet((
17ed6 75 38 2a 29 26 6d 2e 7a 5b 6d 2e 6e 2d 6c 65 6e  u8*)&m.z[m.n-len
17ed7 52 6f 77 69 64 5d 2c 20 74 79 70 65 52 6f 77 69  Rowid], typeRowi
17ed8 64 2c 20 26 76 29 3b 0a 20 20 2a 72 6f 77 69 64  d, &v);.  *rowid
17ed9 20 3d 20 76 2e 75 2e 69 3b 0a 20 20 73 71 6c 69   = v.u.i;.  sqli
17eda 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
17edb 65 28 26 6d 29 3b 0a 20 20 72 65 74 75 72 6e 20  e(&m);.  return 
17edc 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
17edd 0a 2a 2a 20 43 6f 6d 70 61 72 65 20 74 68 65 20  .** Compare the 
17ede 6b 65 79 20 6f 66 20 74 68 65 20 69 6e 64 65 78  key of the index
17edf 20 65 6e 74 72 79 20 74 68 61 74 20 63 75 72 73   entry that curs
17ee0 6f 72 20 70 43 20 69 73 20 70 6f 69 6e 74 20 74  or pC is point t
17ee1 6f 20 61 67 61 69 6e 73 74 0a 2a 2a 20 74 68 65  o against.** the
17ee2 20 6b 65 79 20 73 74 72 69 6e 67 20 69 6e 20 70   key string in p
17ee3 4b 65 79 20 28 6f 66 20 6c 65 6e 67 74 68 20 6e  Key (of length n
17ee4 4b 65 79 29 2e 20 20 57 72 69 74 65 20 69 6e 74  Key).  Write int
17ee5 6f 20 2a 70 52 65 73 20 61 20 6e 75 6d 62 65 72  o *pRes a number
17ee6 0a 2a 2a 20 74 68 61 74 20 69 73 20 6e 65 67 61  .** that is nega
17ee7 74 69 76 65 2c 20 7a 65 72 6f 2c 20 6f 72 20 70  tive, zero, or p
17ee8 6f 73 69 74 69 76 65 20 69 66 20 70 43 20 69 73  ositive if pC is
17ee9 20 6c 65 73 73 20 74 68 61 6e 2c 20 65 71 75 61   less than, equa
17eea 6c 20 74 6f 2c 0a 2a 2a 20 6f 72 20 67 72 65 61  l to,.** or grea
17eeb 74 65 72 20 74 68 61 6e 20 70 4b 65 79 2e 20 20  ter than pKey.  
17eec 52 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  Return SQLITE_OK
17eed 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a   on success..**.
17eee 2a 2a 20 70 4b 65 79 20 69 73 20 65 69 74 68 65  ** pKey is eithe
17eef 72 20 63 72 65 61 74 65 64 20 77 69 74 68 6f 75  r created withou
17ef0 74 20 61 20 72 6f 77 69 64 20 6f 72 20 69 73 20  t a rowid or is 
17ef1 74 72 75 6e 63 61 74 65 64 20 73 6f 20 74 68 61  truncated so tha
17ef2 74 20 69 74 0a 2a 2a 20 6f 6d 69 74 73 20 74 68  t it.** omits th
17ef3 65 20 72 6f 77 69 64 20 61 74 20 74 68 65 20 65  e rowid at the e
17ef4 6e 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 61  nd.  The rowid a
17ef5 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
17ef6 20 69 6e 64 65 78 20 65 6e 74 72 79 0a 2a 2a 20   index entry.** 
17ef7 69 73 20 69 67 6e 6f 72 65 64 20 61 73 20 77 65  is ignored as we
17ef8 6c 6c 2e 20 20 48 65 6e 63 65 2c 20 74 68 69 73  ll.  Hence, this
17ef9 20 72 6f 75 74 69 6e 65 20 6f 6e 6c 79 20 63 6f   routine only co
17efa 6d 70 61 72 65 73 20 74 68 65 20 70 72 65 66 69  mpares the prefi
17efb 78 65 73 20 0a 2a 2a 20 6f 66 20 74 68 65 20 6b  xes .** of the k
17efc 65 79 73 20 70 72 69 6f 72 20 74 6f 20 74 68 65  eys prior to the
17efd 20 66 69 6e 61 6c 20 72 6f 77 69 64 2c 20 6e 6f   final rowid, no
17efe 74 20 74 68 65 20 65 6e 74 69 72 65 20 6b 65 79  t the entire key
17eff 2e 0a 2a 2a 0a 2a 2a 20 70 55 6e 70 61 63 6b 65  ..**.** pUnpacke
17f00 64 20 6d 61 79 20 62 65 20 61 6e 20 75 6e 70 61  d may be an unpa
17f01 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20  cked version of 
17f02 70 4b 65 79 2c 6e 4b 65 79 2e 20 20 49 66 20 70  pKey,nKey.  If p
17f03 55 6e 70 61 63 6b 65 64 20 69 73 0a 2a 2a 20 73  Unpacked is.** s
17f04 75 70 70 6c 69 65 64 20 69 74 20 69 73 20 75 73  upplied it is us
17f05 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 70  ed in place of p
17f06 4b 65 79 2c 6e 4b 65 79 2e 0a 2a 2f 0a 53 51 4c  Key,nKey..*/.SQL
17f07 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
17f08 73 71 6c 69 74 65 33 56 64 62 65 49 64 78 4b 65  sqlite3VdbeIdxKe
17f09 79 43 6f 6d 70 61 72 65 28 0a 20 20 56 64 62 65  yCompare(.  Vdbe
17f0a 43 75 72 73 6f 72 20 2a 70 43 2c 20 20 20 20 20  Cursor *pC,     
17f0b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 63          /* The c
17f0c 75 72 73 6f 72 20 74 6f 20 63 6f 6d 70 61 72 65  ursor to compare
17f0d 20 61 67 61 69 6e 73 74 20 2a 2f 0a 20 20 55 6e   against */.  Un
17f0e 70 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 70 55  packedRecord *pU
17f0f 6e 70 61 63 6b 65 64 2c 20 20 2f 2a 20 55 6e 70  npacked,  /* Unp
17f10 61 63 6b 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  acked version of
17f11 20 70 4b 65 79 20 61 6e 64 20 6e 4b 65 79 20 2a   pKey and nKey *
17f12 2f 0a 20 20 69 6e 74 20 2a 72 65 73 20 20 20 20  /.  int *res    
17f13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
17f14 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f 6d  /* Write the com
17f15 70 61 72 69 73 6f 6e 20 72 65 73 75 6c 74 20 68  parison result h
17f16 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 36 34 20  ere */.){.  i64 
17f17 6e 43 65 6c 6c 4b 65 79 20 3d 20 30 3b 0a 20 20  nCellKey = 0;.  
17f18 69 6e 74 20 72 63 3b 0a 20 20 42 74 43 75 72 73  int rc;.  BtCurs
17f19 6f 72 20 2a 70 43 75 72 20 3d 20 70 43 2d 3e 70  or *pCur = pC->p
17f1a 43 75 72 73 6f 72 3b 0a 20 20 4d 65 6d 20 6d 3b  Cursor;.  Mem m;
17f1b 0a 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ..  sqlite3Btree
17f1c 4b 65 79 53 69 7a 65 28 70 43 75 72 2c 20 26 6e  KeySize(pCur, &n
17f1d 43 65 6c 6c 4b 65 79 29 3b 0a 20 20 69 66 28 20  CellKey);.  if( 
17f1e 6e 43 65 6c 6c 4b 65 79 3c 3d 30 20 29 7b 0a 20  nCellKey<=0 ){. 
17f1f 20 20 20 2a 72 65 73 20 3d 20 30 3b 0a 20 20 20     *res = 0;.   
17f20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
17f21 4b 3b 0a 20 20 7d 0a 20 20 6d 2e 64 62 20 3d 20  K;.  }.  m.db = 
17f22 30 3b 0a 20 20 6d 2e 66 6c 61 67 73 20 3d 20 30  0;.  m.flags = 0
17f23 3b 0a 20 20 6d 2e 7a 4d 61 6c 6c 6f 63 20 3d 20  ;.  m.zMalloc = 
17f24 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  0;.  rc = sqlite
17f25 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
17f26 65 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 30  e(pC->pCursor, 0
17f27 2c 20 6e 43 65 6c 6c 4b 65 79 2c 20 31 2c 20 26  , nCellKey, 1, &
17f28 6d 29 3b 0a 20 20 69 66 28 20 72 63 20 29 7b 0a  m);.  if( rc ){.
17f29 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
17f2a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 55 6e   }.  assert( pUn
17f2b 70 61 63 6b 65 64 2d 3e 66 6c 61 67 73 20 26 20  packed->flags & 
17f2c 55 4e 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f  UNPACKED_IGNORE_
17f2d 52 4f 57 49 44 20 29 3b 0a 20 20 2a 72 65 73 20  ROWID );.  *res 
17f2e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
17f2f 6f 72 64 43 6f 6d 70 61 72 65 28 6d 2e 6e 2c 20  ordCompare(m.n, 
17f30 6d 2e 7a 2c 20 70 55 6e 70 61 63 6b 65 64 29 3b  m.z, pUnpacked);
17f31 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
17f32 6d 52 65 6c 65 61 73 65 28 26 6d 29 3b 0a 20 20  mRelease(&m);.  
17f33 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
17f34 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
17f35 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65  routine sets the
17f36 20 76 61 6c 75 65 20 74 6f 20 62 65 20 72 65 74   value to be ret
17f37 75 72 6e 65 64 20 62 79 20 73 75 62 73 65 71 75  urned by subsequ
17f38 65 6e 74 20 63 61 6c 6c 73 20 74 6f 0a 2a 2a 20  ent calls to.** 
17f39 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73 28  sqlite3_changes(
17f3a 29 20 6f 6e 20 74 68 65 20 64 61 74 61 62 61 73  ) on the databas
17f3b 65 20 68 61 6e 64 6c 65 20 27 64 62 27 2e 20 0a  e handle 'db'. .
17f3c 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17f3d 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 64  E void sqlite3Vd
17f3e 62 65 53 65 74 43 68 61 6e 67 65 73 28 73 71 6c  beSetChanges(sql
17f3f 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6e 43  ite3 *db, int nC
17f40 68 61 6e 67 65 29 7b 0a 20 20 61 73 73 65 72 74  hange){.  assert
17f41 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
17f42 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
17f43 29 3b 0a 20 20 64 62 2d 3e 6e 43 68 61 6e 67 65  );.  db->nChange
17f44 20 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62   = nChange;.  db
17f45 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 2b  ->nTotalChange +
17f46 3d 20 6e 43 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a  = nChange;.}../*
17f47 0a 2a 2a 20 53 65 74 20 61 20 66 6c 61 67 20 69  .** Set a flag i
17f48 6e 20 74 68 65 20 76 64 62 65 20 74 6f 20 75 70  n the vdbe to up
17f49 64 61 74 65 20 74 68 65 20 63 68 61 6e 67 65 20  date the change 
17f4a 63 6f 75 6e 74 65 72 20 77 68 65 6e 20 69 74 20  counter when it 
17f4b 69 73 20 66 69 6e 61 6c 69 73 65 64 0a 2a 2a 20  is finalised.** 
17f4c 6f 72 20 72 65 73 65 74 2e 0a 2a 2f 0a 53 51 4c  or reset..*/.SQL
17f4d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
17f4e 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
17f4f 74 43 68 61 6e 67 65 73 28 56 64 62 65 20 2a 76  tChanges(Vdbe *v
17f50 29 7b 0a 20 20 76 2d 3e 63 68 61 6e 67 65 43 6e  ){.  v->changeCn
17f51 74 4f 6e 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a  tOn = 1;.}../*.*
17f52 2a 20 4d 61 72 6b 20 65 76 65 72 79 20 70 72 65  * Mark every pre
17f53 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pared statement 
17f54 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20  associated with 
17f55 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  a database conne
17f56 63 74 69 6f 6e 0a 2a 2a 20 61 73 20 65 78 70 69  ction.** as expi
17f57 72 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78  red..**.** An ex
17f58 70 69 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20  pired statement 
17f59 6d 65 61 6e 73 20 74 68 61 74 20 72 65 63 6f 6d  means that recom
17f5a 70 69 6c 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  pilation of the 
17f5b 73 74 61 74 65 6d 65 6e 74 20 69 73 0a 2a 2a 20  statement is.** 
17f5c 72 65 63 6f 6d 6d 65 6e 64 2e 20 20 53 74 61 74  recommend.  Stat
17f5d 65 6d 65 6e 74 73 20 65 78 70 69 72 65 20 77 68  ements expire wh
17f5e 65 6e 20 74 68 69 6e 67 73 20 68 61 70 70 65 6e  en things happen
17f5f 20 74 68 61 74 20 6d 61 6b 65 20 74 68 65 69 72   that make their
17f60 0a 2a 2a 20 70 72 6f 67 72 61 6d 73 20 6f 62 73  .** programs obs
17f61 6f 6c 65 74 65 2e 20 20 52 65 6d 6f 76 69 6e 67  olete.  Removing
17f62 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 66 75   user-defined fu
17f63 6e 63 74 69 6f 6e 73 20 6f 72 20 63 6f 6c 6c 61  nctions or colla
17f64 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63 65  ting.** sequence
17f65 73 2c 20 6f 72 20 63 68 61 6e 67 69 6e 67 20 61  s, or changing a
17f66 6e 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20  n authorization 
17f67 66 75 6e 63 74 69 6f 6e 20 61 72 65 20 74 68 65  function are the
17f68 20 74 79 70 65 73 20 6f 66 0a 2a 2a 20 74 68 69   types of.** thi
17f69 6e 67 73 20 74 68 61 74 20 6d 61 6b 65 20 70 72  ngs that make pr
17f6a 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74  epared statement
17f6b 73 20 6f 62 73 6f 6c 65 74 65 2e 0a 2a 2f 0a 53  s obsolete..*/.S
17f6c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
17f6d 69 64 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  id sqlite3Expire
17f6e 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
17f6f 74 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  ts(sqlite3 *db){
17f70 0a 20 20 56 64 62 65 20 2a 70 3b 0a 20 20 66 6f  .  Vdbe *p;.  fo
17f71 72 28 70 20 3d 20 64 62 2d 3e 70 56 64 62 65 3b  r(p = db->pVdbe;
17f72 20 70 3b 20 70 3d 70 2d 3e 70 4e 65 78 74 29 7b   p; p=p->pNext){
17f73 0a 20 20 20 20 70 2d 3e 65 78 70 69 72 65 64 20  .    p->expired 
17f74 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  = 1;.  }.}../*.*
17f75 2a 20 52 65 74 75 72 6e 20 74 68 65 20 64 61 74  * Return the dat
17f76 61 62 61 73 65 20 61 73 73 6f 63 69 61 74 65 64  abase associated
17f77 20 77 69 74 68 20 74 68 65 20 56 64 62 65 2e 0a   with the Vdbe..
17f78 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
17f79 45 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74  E sqlite3 *sqlit
17f7a 65 33 56 64 62 65 44 62 28 56 64 62 65 20 2a 76  e3VdbeDb(Vdbe *v
17f7b 29 7b 0a 20 20 72 65 74 75 72 6e 20 76 2d 3e 64  ){.  return v->d
17f7c 62 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  b;.}../*********
17f7d 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 76 64 62  ***** End of vdb
17f7e 65 61 75 78 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eaux.c *********
17f7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f81 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
17f82 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
17f83 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a   vdbeapi.c *****
17f84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f86 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 34  ****/./*.** 2004
17f87 20 4d 61 79 20 32 36 0a 2a 2a 0a 2a 2a 20 54 68   May 26.**.** Th
17f88 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
17f89 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
17f8a 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
17f8b 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
17f8c 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
17f8d 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
17f8e 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
17f8f 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
17f90 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
17f91 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
17f92 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
17f93 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
17f94 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
17f95 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
17f96 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
17f97 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
17f98 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
17f99 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f9a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f9b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
17f9d 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  ******.**.** Thi
17f9e 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
17f9f 63 6f 64 65 20 75 73 65 20 74 6f 20 69 6d 70 6c  code use to impl
17fa0 65 6d 65 6e 74 20 41 50 49 73 20 74 68 61 74 20  ement APIs that 
17fa1 61 72 65 20 70 61 72 74 20 6f 66 20 74 68 65 0a  are part of the.
17fa2 2a 2a 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20 24  ** VDBE..**.** $
17fa3 49 64 3a 20 76 64 62 65 61 70 69 2e 63 2c 76 20  Id: vdbeapi.c,v 
17fa4 31 2e 31 34 39 20 32 30 30 38 2f 31 31 2f 31 39  1.149 2008/11/19
17fa5 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69 65 6c   09:05:27 daniel
17fa6 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
17fa7 23 69 66 20 30 20 26 26 20 64 65 66 69 6e 65 64  #if 0 && defined
17fa8 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  (SQLITE_ENABLE_M
17fa9 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
17faa 29 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  )./*.** The foll
17fab 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 20  owing structure 
17fac 63 6f 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72  contains pointer
17fad 73 20 74 6f 20 74 68 65 20 65 6e 64 20 70 6f 69  s to the end poi
17fae 6e 74 73 20 6f 66 20 61 0a 2a 2a 20 64 6f 75 62  nts of a.** doub
17faf 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f  ly-linked list o
17fb0 66 20 61 6c 6c 20 63 6f 6d 70 69 6c 65 64 20 53  f all compiled S
17fb1 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 74 68  QL statements th
17fb2 61 74 20 6d 61 79 20 62 65 20 68 6f 6c 64 69 6e  at may be holdin
17fb3 67 0a 2a 2a 20 62 75 66 66 65 72 73 20 65 6c 69  g.** buffers eli
17fb4 67 69 62 6c 65 20 66 6f 72 20 72 65 6c 65 61 73  gible for releas
17fb5 65 20 77 68 65 6e 20 74 68 65 20 73 71 6c 69 74  e when the sqlit
17fb6 65 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72  e3_release_memor
17fb7 79 28 29 20 69 6e 74 65 72 66 61 63 65 20 69 73  y() interface is
17fb8 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 20 41 63 63  .** invoked. Acc
17fb9 65 73 73 20 74 6f 20 74 68 69 73 20 6c 69 73 74  ess to this list
17fba 20 69 73 20 70 72 6f 74 65 63 74 65 64 20 62 79   is protected by
17fbb 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45   the SQLITE_MUTE
17fbc 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 0a 2a 2a  X_STATIC_LRU2.**
17fbd 20 6d 75 74 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 74   mutex..**.** St
17fbe 61 74 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64  atements are add
17fbf 65 64 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66  ed to the end of
17fc0 20 74 68 69 73 20 6c 69 73 74 20 77 68 65 6e 20   this list when 
17fc1 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 29 20  sqlite3_reset() 
17fc2 69 73 0a 2a 2a 20 63 61 6c 6c 65 64 2e 20 54 68  is.** called. Th
17fc3 65 79 20 61 72 65 20 72 65 6d 6f 76 65 64 20 65  ey are removed e
17fc4 69 74 68 65 72 20 77 68 65 6e 20 73 71 6c 69 74  ither when sqlit
17fc5 65 33 5f 73 74 65 70 28 29 20 6f 72 20 73 71 6c  e3_step() or sql
17fc6 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 29 0a  ite3_finalize().
17fc7 2a 2a 20 69 73 20 63 61 6c 6c 65 64 2e 20 57 68  ** is called. Wh
17fc8 65 6e 20 73 74 61 74 65 6d 65 6e 74 73 20 61 72  en statements ar
17fc9 65 20 61 64 64 65 64 20 74 6f 20 74 68 69 73 20  e added to this 
17fca 6c 69 73 74 2c 20 74 68 65 20 61 73 73 6f 63 69  list, the associ
17fcb 61 74 65 64 20 0a 2a 2a 20 72 65 67 69 73 74 65  ated .** registe
17fcc 72 20 61 72 72 61 79 20 28 70 2d 3e 61 4d 65 6d  r array (p->aMem
17fcd 5b 31 2e 2e 70 2d 3e 6e 4d 65 6d 5d 29 20 6d 61  [1..p->nMem]) ma
17fce 79 20 63 6f 6e 74 61 69 6e 20 64 79 6e 61 6d 69  y contain dynami
17fcf 63 20 62 75 66 66 65 72 73 20 74 68 61 74 0a 2a  c buffers that.*
17fd0 2a 20 63 61 6e 20 62 65 20 66 72 65 65 64 20 75  * can be freed u
17fd1 73 69 6e 67 20 73 71 6c 69 74 65 33 56 64 62 65  sing sqlite3Vdbe
17fd2 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 29 2e  ReleaseMemory().
17fd3 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 73 74 61 74  .**.** When stat
17fd4 65 6d 65 6e 74 73 20 61 72 65 20 61 64 64 65 64  ements are added
17fd5 20 6f 72 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d   or removed from
17fd6 20 74 68 69 73 20 6c 69 73 74 2c 20 74 68 65 20   this list, the 
17fd7 6d 75 74 65 78 0a 2a 2a 20 61 73 73 6f 63 69 61  mutex.** associa
17fd8 74 65 64 20 77 69 74 68 20 74 68 65 20 56 64 62  ted with the Vdb
17fd9 65 20 62 65 69 6e 67 20 61 64 64 65 64 20 6f 72  e being added or
17fda 20 72 65 6d 6f 76 65 64 20 28 56 64 62 65 2e 64   removed (Vdbe.d
17fdb 62 2d 3e 6d 75 74 65 78 29 20 69 73 0a 2a 2a 20  b->mutex) is.** 
17fdc 61 6c 72 65 61 64 79 20 68 65 6c 64 2e 20 54 68  already held. Th
17fdd 65 20 4c 52 55 32 20 6d 75 74 65 78 20 69 73 20  e LRU2 mutex is 
17fde 74 68 65 6e 20 6f 62 74 61 69 6e 65 64 2c 20 62  then obtained, b
17fdf 6c 6f 63 6b 69 6e 67 20 69 66 20 6e 65 63 65 73  locking if neces
17fe0 73 61 72 79 2c 0a 2a 2a 20 74 68 65 20 6c 69 6e  sary,.** the lin
17fe1 6b 65 64 2d 6c 69 73 74 20 70 6f 69 6e 74 65 72  ked-list pointer
17fe2 73 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 61 6e  s manipulated an
17fe3 64 20 74 68 65 20 4c 52 55 32 20 6d 75 74 65 78  d the LRU2 mutex
17fe4 20 72 65 6c 69 6e 71 75 69 73 68 65 64 2e 0a 2a   relinquished..*
17fe5 2f 0a 73 74 72 75 63 74 20 53 74 61 74 65 6d 65  /.struct Stateme
17fe6 6e 74 4c 72 75 4c 69 73 74 20 7b 0a 20 20 56 64  ntLruList {.  Vd
17fe7 62 65 20 2a 70 46 69 72 73 74 3b 0a 20 20 56 64  be *pFirst;.  Vd
17fe8 62 65 20 2a 70 4c 61 73 74 3b 0a 7d 3b 0a 73 74  be *pLast;.};.st
17fe9 61 74 69 63 20 73 74 72 75 63 74 20 53 74 61 74  atic struct Stat
17fea 65 6d 65 6e 74 4c 72 75 4c 69 73 74 20 73 71 6c  ementLruList sql
17feb 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
17fec 73 3b 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  s;../*.** Check 
17fed 74 68 61 74 20 74 68 65 20 6c 69 73 74 20 6c 6f  that the list lo
17fee 6f 6b 73 20 74 6f 20 62 65 20 69 6e 74 65 72 6e  oks to be intern
17fef 61 6c 6c 79 20 63 6f 6e 73 69 73 74 65 6e 74 2e  ally consistent.
17ff0 20 54 68 69 73 20 69 73 20 75 73 65 64 0a 2a 2a   This is used.**
17ff1 20 61 73 20 70 61 72 74 20 6f 66 20 61 6e 20 61   as part of an a
17ff2 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65 6e  ssert() statemen
17ff3 74 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a  t as follows:.**
17ff4 0a 2a 2a 20 20 20 61 73 73 65 72 74 28 20 73 74  .**   assert( st
17ff5 6d 74 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a  mtLruCheck() );.
17ff6 2a 2f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  */.#ifndef NDEBU
17ff7 47 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 6d  G.static int stm
17ff8 74 4c 72 75 43 68 65 63 6b 28 29 7b 0a 20 20 56  tLruCheck(){.  V
17ff9 64 62 65 20 2a 70 3b 0a 20 20 66 6f 72 28 70 3d  dbe *p;.  for(p=
17ffa 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
17ffb 65 6e 74 73 2e 70 46 69 72 73 74 3b 20 70 3b 20  ents.pFirst; p; 
17ffc 70 3d 70 2d 3e 70 4c 72 75 4e 65 78 74 29 7b 0a  p=p->pLruNext){.
17ffd 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 70 4c      assert(p->pL
17ffe 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d 73 71 6c  ruNext || p==sql
17fff 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
18000 73 2e 70 4c 61 73 74 29 3b 0a 20 20 20 20 61 73  s.pLast);.    as
18001 73 65 72 74 28 21 70 2d 3e 70 4c 72 75 4e 65 78  sert(!p->pLruNex
18002 74 20 7c 7c 20 70 2d 3e 70 4c 72 75 4e 65 78 74  t || p->pLruNext
18003 2d 3e 70 4c 72 75 50 72 65 76 3d 3d 70 29 3b 0a  ->pLruPrev==p);.
18004 20 20 20 20 61 73 73 65 72 74 28 70 2d 3e 70 4c      assert(p->pL
18005 72 75 50 72 65 76 20 7c 7c 20 70 3d 3d 73 71 6c  ruPrev || p==sql
18006 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
18007 73 2e 70 46 69 72 73 74 29 3b 0a 20 20 20 20 61  s.pFirst);.    a
18008 73 73 65 72 74 28 21 70 2d 3e 70 4c 72 75 50 72  ssert(!p->pLruPr
18009 65 76 20 7c 7c 20 70 2d 3e 70 4c 72 75 50 72 65  ev || p->pLruPre
1800a 76 2d 3e 70 4c 72 75 4e 65 78 74 3d 3d 70 29 3b  v->pLruNext==p);
1800b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
1800c 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a  .}.#endif../*.**
1800d 20 41 64 64 20 76 64 62 65 20 70 20 74 6f 20 74   Add vdbe p to t
1800e 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 73 74  he end of the st
1800f 61 74 65 6d 65 6e 74 20 6c 72 75 20 6c 69 73 74  atement lru list
18010 2e 20 49 74 20 69 73 20 61 73 73 75 6d 65 64 20  . It is assumed 
18011 74 68 61 74 0a 2a 2a 20 70 20 69 73 20 6e 6f 74  that.** p is not
18012 20 61 6c 72 65 61 64 79 20 70 61 72 74 20 6f 66   already part of
18013 20 74 68 65 20 6c 69 73 74 20 77 68 65 6e 20 74   the list when t
18014 68 69 73 20 69 73 20 63 61 6c 6c 65 64 2e 20 54  his is called. T
18015 68 65 20 6c 72 75 20 6c 69 73 74 0a 2a 2a 20 69  he lru list.** i
18016 73 20 70 72 6f 74 65 63 74 65 64 20 62 79 20 74  s protected by t
18017 68 65 20 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  he SQLITE_MUTEX_
18018 53 54 41 54 49 43 5f 4c 52 55 20 6d 75 74 65 78  STATIC_LRU mutex
18019 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1801a 20 73 74 6d 74 4c 72 75 41 64 64 28 56 64 62 65   stmtLruAdd(Vdbe
1801b 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f   *p){.  sqlite3_
1801c 6d 75 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69  mutex_enter(sqli
1801d 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51  te3MutexAlloc(SQ
1801e 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41 54 49  LITE_MUTEX_STATI
1801f 43 5f 4c 52 55 32 29 29 3b 0a 0a 20 20 69 66 28  C_LRU2));..  if(
18020 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20   p->pLruPrev || 
18021 70 2d 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 73  p->pLruNext || s
18022 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
18023 6e 74 73 2e 70 46 69 72 73 74 3d 3d 70 20 29 7b  nts.pFirst==p ){
18024 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  .    sqlite3_mut
18025 65 78 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33  ex_leave(sqlite3
18026 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
18027 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c  E_MUTEX_STATIC_L
18028 52 55 32 29 29 3b 0a 20 20 20 20 72 65 74 75 72  RU2));.    retur
18029 6e 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72 74  n;.  }..  assert
1802a 28 20 73 74 6d 74 4c 72 75 43 68 65 63 6b 28 29  ( stmtLruCheck()
1802b 20 29 3b 0a 0a 20 20 69 66 28 20 21 73 71 6c 69   );..  if( !sqli
1802c 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73  te3LruStatements
1802d 2e 70 46 69 72 73 74 20 29 7b 0a 20 20 20 20 61  .pFirst ){.    a
1802e 73 73 65 72 74 28 20 21 73 71 6c 69 74 65 33 4c  ssert( !sqlite3L
1802f 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61  ruStatements.pLa
18030 73 74 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  st );.    sqlite
18031 33 4c 72 75 53 74 61 74 65 6d 65 6e 74 73 2e 70  3LruStatements.p
18032 46 69 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 73  First = p;.    s
18033 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
18034 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20  nts.pLast = p;. 
18035 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65   }else{.    asse
18036 72 74 28 20 21 73 71 6c 69 74 65 33 4c 72 75 53  rt( !sqlite3LruS
18037 74 61 74 65 6d 65 6e 74 73 2e 70 4c 61 73 74 2d  tatements.pLast-
18038 3e 70 4c 72 75 4e 65 78 74 20 29 3b 0a 20 20 20  >pLruNext );.   
18039 20 70 2d 3e 70 4c 72 75 50 72 65 76 20 3d 20 73   p->pLruPrev = s
1803a 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
1803b 6e 74 73 2e 70 4c 61 73 74 3b 0a 20 20 20 20 73  nts.pLast;.    s
1803c 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65  qlite3LruStateme
1803d 6e 74 73 2e 70 4c 61 73 74 2d 3e 70 4c 72 75 4e  nts.pLast->pLruN
1803e 65 78 74 20 3d 20 70 3b 0a 20 20 20 20 73 71 6c  ext = p;.    sql
1803f 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e 74  ite3LruStatement
18040 73 2e 70 4c 61 73 74 20 3d 20 70 3b 0a 20 20 7d  s.pLast = p;.  }
18041 0a 0a 20 20 61 73 73 65 72 74 28 20 73 74 6d 74  ..  assert( stmt
18042 4c 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 0a 20  LruCheck() );.. 
18043 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
18044 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75 74 65  eave(sqlite3Mute
18045 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f 4d 55  xAlloc(SQLITE_MU
18046 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 29  TEX_STATIC_LRU2)
18047 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75  );.}../*.** Assu
18048 6d 69 6e 67 20 74 68 65 20 53 51 4c 49 54 45 5f  ming the SQLITE_
18049 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
1804a 32 20 6d 75 74 65 78 74 20 69 73 20 61 6c 72 65  2 mutext is alre
1804b 61 64 79 20 68 65 6c 64 2c 20 72 65 6d 6f 76 65  ady held, remove
1804c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 70 20  .** statement p 
1804d 66 72 6f 6d 20 74 68 65 20 6c 65 61 73 74 2d 72  from the least-r
1804e 65 63 65 6e 74 6c 79 2d 75 73 65 64 20 73 74 61  ecently-used sta
1804f 74 65 6d 65 6e 74 20 6c 69 73 74 2e 20 49 66 20  tement list. If 
18050 74 68 65 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  the .** statemen
18051 74 20 69 73 20 6e 6f 74 20 63 75 72 72 65 6e 74  t is not current
18052 6c 79 20 70 61 72 74 20 6f 66 20 74 68 65 20 6c  ly part of the l
18053 69 73 74 2c 20 74 68 69 73 20 63 61 6c 6c 20 69  ist, this call i
18054 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f 0a 73 74  s a no-op..*/.st
18055 61 74 69 63 20 76 6f 69 64 20 73 74 6d 74 4c 72  atic void stmtLr
18056 75 52 65 6d 6f 76 65 4e 6f 6d 75 74 65 78 28 56  uRemoveNomutex(V
18057 64 62 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70  dbe *p){.  if( p
18058 2d 3e 70 4c 72 75 50 72 65 76 20 7c 7c 20 70 2d  ->pLruPrev || p-
18059 3e 70 4c 72 75 4e 65 78 74 20 7c 7c 20 70 3d 3d  >pLruNext || p==
1805a 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
1805b 65 6e 74 73 2e 70 46 69 72 73 74 20 29 7b 0a 20  ents.pFirst ){. 
1805c 20 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c     assert( stmtL
1805d 72 75 43 68 65 63 6b 28 29 20 29 3b 0a 20 20 20  ruCheck() );.   
1805e 20 69 66 28 20 70 2d 3e 70 4c 72 75 4e 65 78 74   if( p->pLruNext
1805f 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 72   ){.      p->pLr
18060 75 4e 65 78 74 2d 3e 70 4c 72 75 50 72 65 76 20  uNext->pLruPrev 
18061 3d 20 70 2d 3e 70 4c 72 75 50 72 65 76 3b 0a 20  = p->pLruPrev;. 
18062 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
18063 73 71 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d  sqlite3LruStatem
18064 65 6e 74 73 2e 70 4c 61 73 74 20 3d 20 70 2d 3e  ents.pLast = p->
18065 70 4c 72 75 50 72 65 76 3b 0a 20 20 20 20 7d 0a  pLruPrev;.    }.
18066 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 72 75 50      if( p->pLruP
18067 72 65 76 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  rev ){.      p->
18068 70 4c 72 75 50 72 65 76 2d 3e 70 4c 72 75 4e 65  pLruPrev->pLruNe
18069 78 74 20 3d 20 70 2d 3e 70 4c 72 75 4e 65 78 74  xt = p->pLruNext
1806a 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
1806b 20 20 20 73 71 6c 69 74 65 33 4c 72 75 53 74 61     sqlite3LruSta
1806c 74 65 6d 65 6e 74 73 2e 70 46 69 72 73 74 20 3d  tements.pFirst =
1806d 20 70 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 20 20   p->pLruNext;.  
1806e 20 20 7d 0a 20 20 20 20 70 2d 3e 70 4c 72 75 4e    }.    p->pLruN
1806f 65 78 74 20 3d 20 30 3b 0a 20 20 20 20 70 2d 3e  ext = 0;.    p->
18070 70 4c 72 75 50 72 65 76 20 3d 20 30 3b 0a 20 20  pLruPrev = 0;.  
18071 20 20 61 73 73 65 72 74 28 20 73 74 6d 74 4c 72    assert( stmtLr
18072 75 43 68 65 63 6b 28 29 20 29 3b 0a 20 20 7d 0a  uCheck() );.  }.
18073 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 75 6d 69 6e  }../*.** Assumin
18074 67 20 74 68 65 20 53 51 4c 49 54 45 5f 4d 55 54  g the SQLITE_MUT
18075 45 58 5f 53 54 41 54 49 43 5f 4c 52 55 32 20 6d  EX_STATIC_LRU2 m
18076 75 74 65 78 74 20 69 73 20 6e 6f 74 20 68 65 6c  utext is not hel
18077 64 2c 20 72 65 6d 6f 76 65 0a 2a 2a 20 73 74 61  d, remove.** sta
18078 74 65 6d 65 6e 74 20 70 20 66 72 6f 6d 20 74 68  tement p from th
18079 65 20 6c 65 61 73 74 2d 72 65 63 65 6e 74 6c 79  e least-recently
1807a 2d 75 73 65 64 20 73 74 61 74 65 6d 65 6e 74 20  -used statement 
1807b 6c 69 73 74 2e 20 49 66 20 74 68 65 20 0a 2a 2a  list. If the .**
1807c 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 6e 6f   statement is no
1807d 74 20 63 75 72 72 65 6e 74 6c 79 20 70 61 72 74  t currently part
1807e 20 6f 66 20 74 68 65 20 6c 69 73 74 2c 20 74 68   of the list, th
1807f 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
18080 6f 70 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  op..*/.static vo
18081 69 64 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65  id stmtLruRemove
18082 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 71 6c  (Vdbe *p){.  sql
18083 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
18084 28 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c  (sqlite3MutexAll
18085 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
18086 53 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 20  STATIC_LRU2));. 
18087 20 73 74 6d 74 4c 72 75 52 65 6d 6f 76 65 4e 6f   stmtLruRemoveNo
18088 6d 75 74 65 78 28 70 29 3b 0a 20 20 73 71 6c 69  mutex(p);.  sqli
18089 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1808a 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f  sqlite3MutexAllo
1808b 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53  c(SQLITE_MUTEX_S
1808c 54 41 54 49 43 5f 4c 52 55 32 29 29 3b 0a 7d 0a  TATIC_LRU2));.}.
1808d 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74 6f 20 72 65  ./*.** Try to re
1808e 6c 65 61 73 65 20 6e 20 62 79 74 65 73 20 6f 66  lease n bytes of
1808f 20 6d 65 6d 6f 72 79 20 62 79 20 66 72 65 65 69   memory by freei
18090 6e 67 20 62 75 66 66 65 72 73 20 61 73 73 6f 63  ng buffers assoc
18091 69 61 74 65 64 20 0a 2a 2a 20 77 69 74 68 20 74  iated .** with t
18092 68 65 20 6d 65 6d 6f 72 79 20 72 65 67 69 73 74  he memory regist
18093 65 72 73 20 6f 66 20 63 75 72 72 65 6e 74 6c 79  ers of currently
18094 20 75 6e 75 73 65 64 20 76 64 62 65 73 2e 0a 2a   unused vdbes..*
18095 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
18096 20 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65   int sqlite3Vdbe
18097 52 65 6c 65 61 73 65 4d 65 6d 6f 72 79 28 69 6e  ReleaseMemory(in
18098 74 20 6e 29 7b 0a 20 20 56 64 62 65 20 2a 70 3b  t n){.  Vdbe *p;
18099 0a 20 20 56 64 62 65 20 2a 70 4e 65 78 74 3b 0a  .  Vdbe *pNext;.
1809a 20 20 69 6e 74 20 6e 46 72 65 65 20 3d 20 30 3b    int nFree = 0;
1809b 0a 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ..  sqlite3_mute
1809c 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65 33 4d  x_enter(sqlite3M
1809d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45  utexAlloc(SQLITE
1809e 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52  _MUTEX_STATIC_LR
1809f 55 32 29 29 3b 0a 20 20 66 6f 72 28 70 3d 73 71  U2));.  for(p=sq
180a0 6c 69 74 65 33 4c 72 75 53 74 61 74 65 6d 65 6e  lite3LruStatemen
180a1 74 73 2e 70 46 69 72 73 74 3b 20 70 20 26 26 20  ts.pFirst; p && 
180a2 6e 46 72 65 65 3c 6e 3b 20 70 3d 70 4e 65 78 74  nFree<n; p=pNext
180a3 29 7b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 70  ){.    pNext = p
180a4 2d 3e 70 4c 72 75 4e 65 78 74 3b 0a 0a 20 20 20  ->pLruNext;..   
180a5 20 2f 2a 20 46 6f 72 20 65 61 63 68 20 73 74 61   /* For each sta
180a6 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 69 6e  tement handle in
180a7 20 74 68 65 20 6c 72 75 20 6c 69 73 74 2c 20 61   the lru list, a
180a8 74 74 65 6d 70 74 20 74 6f 20 6f 62 74 61 69 6e  ttempt to obtain
180a9 20 74 68 65 0a 20 20 20 20 2a 2a 20 61 73 73 6f   the.    ** asso
180aa 63 69 61 74 65 64 20 64 61 74 61 62 61 73 65 20  ciated database 
180ab 6d 75 74 65 78 2e 20 49 66 20 69 74 20 63 61 6e  mutex. If it can
180ac 6e 6f 74 20 62 65 20 6f 62 74 61 69 6e 65 64 2c  not be obtained,
180ad 20 63 6f 6e 74 69 6e 75 65 0a 20 20 20 20 2a 2a   continue.    **
180ae 20 74 6f 20 74 68 65 20 6e 65 78 74 20 73 74 61   to the next sta
180af 74 65 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 20 49  tement handle. I
180b0 74 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c  t is not possibl
180b1 65 20 74 6f 20 62 6c 6f 63 6b 20 6f 6e 0a 20 20  e to block on.  
180b2 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61 73    ** the databas
180b3 65 20 6d 75 74 65 78 20 2d 20 74 68 61 74 20 63  e mutex - that c
180b4 6f 75 6c 64 20 63 61 75 73 65 20 64 65 61 64 6c  ould cause deadl
180b5 6f 63 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ock..    */.    
180b6 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 73  if( SQLITE_OK==s
180b7 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 74 72 79  qlite3_mutex_try
180b8 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 20 29  (p->db->mutex) )
180b9 7b 0a 20 20 20 20 20 20 6e 46 72 65 65 20 2b 3d  {.      nFree +=
180ba 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 6c 65   sqlite3VdbeRele
180bb 61 73 65 42 75 66 66 65 72 73 28 70 29 3b 0a 20  aseBuffers(p);. 
180bc 20 20 20 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f       stmtLruRemo
180bd 76 65 4e 6f 6d 75 74 65 78 28 70 29 3b 0a 20 20  veNomutex(p);.  
180be 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
180bf 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d  x_leave(p->db->m
180c0 75 74 65 78 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  utex);.    }.  }
180c1 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
180c2 5f 6c 65 61 76 65 28 73 71 6c 69 74 65 33 4d 75  _leave(sqlite3Mu
180c3 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
180c4 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4c 52 55  MUTEX_STATIC_LRU
180c5 32 29 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 6e  2));..  return n
180c6 46 72 65 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  Free;.}../*.** C
180c7 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72 65  all sqlite3Repre
180c8 70 61 72 65 28 29 20 6f 6e 20 74 68 65 20 73 74  pare() on the st
180c9 61 74 65 6d 65 6e 74 2e 20 52 65 6d 6f 76 65 20  atement. Remove 
180ca 69 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20 6c  it from the.** l
180cb 72 75 20 6c 69 73 74 20 62 65 66 6f 72 65 20 64  ru list before d
180cc 6f 69 6e 67 20 73 6f 2c 20 61 73 20 52 65 70 72  oing so, as Repr
180cd 65 70 61 72 65 28 29 20 77 69 6c 6c 20 66 72 65  epare() will fre
180ce 65 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 6d 65 6d  e all the.** mem
180cf 6f 72 79 20 72 65 67 69 73 74 65 72 20 62 75 66  ory register buf
180d0 66 65 72 73 20 61 6e 79 77 61 79 2e 0a 2a 2f 0a  fers anyway..*/.
180d1 69 6e 74 20 76 64 62 65 52 65 70 72 65 70 61 72  int vdbeReprepar
180d2 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 73 74  e(Vdbe *p){.  st
180d3 6d 74 4c 72 75 52 65 6d 6f 76 65 28 70 29 3b 0a  mtLruRemove(p);.
180d4 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
180d5 52 65 70 72 65 70 61 72 65 28 70 29 3b 0a 7d 0a  Reprepare(p);.}.
180d6 0a 23 65 6c 73 65 20 20 20 20 20 20 20 2f 2a 20  .#else       /* 
180d7 21 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d  !SQLITE_ENABLE_M
180d8 45 4d 4f 52 59 5f 4d 41 4e 41 47 45 4d 45 4e 54  EMORY_MANAGEMENT
180d9 20 2a 2f 0a 20 20 23 64 65 66 69 6e 65 20 73 74   */.  #define st
180da 6d 74 4c 72 75 52 65 6d 6f 76 65 28 78 29 0a 20  mtLruRemove(x). 
180db 20 23 64 65 66 69 6e 65 20 73 74 6d 74 4c 72 75   #define stmtLru
180dc 41 64 64 28 78 29 0a 20 20 23 64 65 66 69 6e 65  Add(x).  #define
180dd 20 76 64 62 65 52 65 70 72 65 70 61 72 65 28 78   vdbeReprepare(x
180de 29 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61  ) sqlite3Reprepa
180df 72 65 28 78 29 0a 23 65 6e 64 69 66 0a 0a 0a 23  re(x).#endif...#
180e0 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
180e1 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 2f 2a  IT_DEPRECATED./*
180e2 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20  .** Return TRUE 
180e3 28 6e 6f 6e 2d 7a 65 72 6f 29 20 6f 66 20 74 68  (non-zero) of th
180e4 65 20 73 74 61 74 65 6d 65 6e 74 20 73 75 70 70  e statement supp
180e5 6c 69 65 64 20 61 73 20 61 6e 20 61 72 67 75 6d  lied as an argum
180e6 65 6e 74 20 6e 65 65 64 73 0a 2a 2a 20 74 6f 20  ent needs.** to 
180e7 62 65 20 72 65 63 6f 6d 70 69 6c 65 64 2e 20 20  be recompiled.  
180e8 41 20 73 74 61 74 65 6d 65 6e 74 20 6e 65 65 64  A statement need
180e9 73 20 74 6f 20 62 65 20 72 65 63 6f 6d 70 69 6c  s to be recompil
180ea 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a  ed whenever the.
180eb 2a 2a 20 65 78 65 63 75 74 69 6f 6e 20 65 6e 76  ** execution env
180ec 69 72 6f 6e 6d 65 6e 74 20 63 68 61 6e 67 65 73  ironment changes
180ed 20 69 6e 20 61 20 77 61 79 20 74 68 61 74 20 77   in a way that w
180ee 6f 75 6c 64 20 61 6c 74 65 72 20 74 68 65 20 70  ould alter the p
180ef 72 6f 67 72 61 6d 0a 2a 2a 20 74 68 61 74 20 73  rogram.** that s
180f0 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
180f1 20 67 65 6e 65 72 61 74 65 73 2e 20 20 46 6f 72   generates.  For
180f2 20 65 78 61 6d 70 6c 65 2c 20 69 66 20 6e 65 77   example, if new
180f3 20 66 75 6e 63 74 69 6f 6e 73 20 6f 72 0a 2a 2a   functions or.**
180f4 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
180f5 6e 63 65 73 20 61 72 65 20 72 65 67 69 73 74 65  nces are registe
180f6 72 65 64 20 6f 72 20 69 66 20 61 6e 20 61 75 74  red or if an aut
180f7 68 6f 72 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e  horizer function
180f8 20 69 73 0a 2a 2a 20 61 64 64 65 64 20 6f 72 20   is.** added or 
180f9 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
180fa 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
180fb 65 33 5f 65 78 70 69 72 65 64 28 73 71 6c 69 74  e3_expired(sqlit
180fc 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b  e3_stmt *pStmt){
180fd 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64  .  Vdbe *p = (Vd
180fe 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74  be*)pStmt;.  ret
180ff 75 72 6e 20 70 3d 3d 30 20 7c 7c 20 70 2d 3e 65  urn p==0 || p->e
18100 78 70 69 72 65 64 3b 0a 7d 0a 23 65 6e 64 69 66  xpired;.}.#endif
18101 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  ../*.** The foll
18102 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 20 64 65  owing routine de
18103 73 74 72 6f 79 73 20 61 20 76 69 72 74 75 61 6c  stroys a virtual
18104 20 6d 61 63 68 69 6e 65 20 74 68 61 74 20 69 73   machine that is
18105 20 63 72 65 61 74 65 64 20 62 79 0a 2a 2a 20 74   created by.** t
18106 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 69  he sqlite3_compi
18107 6c 65 28 29 20 72 6f 75 74 69 6e 65 2e 20 54 68  le() routine. Th
18108 65 20 69 6e 74 65 67 65 72 20 72 65 74 75 72 6e  e integer return
18109 65 64 20 69 73 20 61 6e 20 53 51 4c 49 54 45 5f  ed is an SQLITE_
1810a 0a 2a 2a 20 73 75 63 63 65 73 73 2f 66 61 69 6c  .** success/fail
1810b 75 72 65 20 63 6f 64 65 20 74 68 61 74 20 64 65  ure code that de
1810c 73 63 72 69 62 65 73 20 74 68 65 20 72 65 73 75  scribes the resu
1810d 6c 74 20 6f 66 20 65 78 65 63 75 74 69 6e 67 20  lt of executing 
1810e 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 6d  the virtual.** m
1810f 61 63 68 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  achine..**.** Th
18110 69 73 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20  is routine sets 
18111 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 61  the error code a
18112 6e 64 20 73 74 72 69 6e 67 20 72 65 74 75 72 6e  nd string return
18113 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69 74 65 33  ed by.** sqlite3
18114 5f 65 72 72 63 6f 64 65 28 29 2c 20 73 71 6c 69  _errcode(), sqli
18115 74 65 33 5f 65 72 72 6d 73 67 28 29 20 61 6e 64  te3_errmsg() and
18116 20 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 31   sqlite3_errmsg1
18117 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  6()..*/.SQLITE_A
18118 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 66  PI int sqlite3_f
18119 69 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f  inalize(sqlite3_
1811a 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
1811b 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53  int rc;.  if( pS
1811c 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  tmt==0 ){.    rc
1811d 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
1811e 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20  }else{.    Vdbe 
1811f 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *v = (Vdbe*)pStm
18120 74 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 48  t;.#if SQLITE_TH
18121 52 45 41 44 53 41 46 45 0a 20 20 20 20 73 71 6c  READSAFE.    sql
18122 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65  ite3_mutex *mute
18123 78 20 3d 20 76 2d 3e 64 62 2d 3e 6d 75 74 65 78  x = v->db->mutex
18124 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c  ;.#endif.    sql
18125 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
18126 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 74 6d  (mutex);.    stm
18127 74 4c 72 75 52 65 6d 6f 76 65 28 76 29 3b 0a 20  tLruRemove(v);. 
18128 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
18129 64 62 65 46 69 6e 61 6c 69 7a 65 28 76 29 3b 0a  dbeFinalize(v);.
1812a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
1812b 78 5f 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a  x_leave(mutex);.
1812c 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
1812d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 65 72 6d 69 6e  .}../*.** Termin
1812e 61 74 65 20 74 68 65 20 63 75 72 72 65 6e 74 20  ate the current 
1812f 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 61 6e 20  execution of an 
18130 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 61 6e  SQL statement an
18131 64 20 72 65 73 65 74 20 69 74 0a 2a 2a 20 62 61  d reset it.** ba
18132 63 6b 20 74 6f 20 69 74 73 20 73 74 61 72 74 69  ck to its starti
18133 6e 67 20 73 74 61 74 65 20 73 6f 20 74 68 61 74  ng state so that
18134 20 69 74 20 63 61 6e 20 62 65 20 72 65 75 73 65   it can be reuse
18135 64 2e 20 41 20 73 75 63 63 65 73 73 20 63 6f 64  d. A success cod
18136 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 70 72  e from.** the pr
18137 69 6f 72 20 65 78 65 63 75 74 69 6f 6e 20 69 73  ior execution is
18138 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a   returned..**.**
18139 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
1813a 74 73 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  ts the error cod
1813b 65 20 61 6e 64 20 73 74 72 69 6e 67 20 72 65 74  e and string ret
1813c 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73 71 6c 69  urned by.** sqli
1813d 74 65 33 5f 65 72 72 63 6f 64 65 28 29 2c 20 73  te3_errcode(), s
1813e 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 29 20  qlite3_errmsg() 
1813f 61 6e 64 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  and sqlite3_errm
18140 73 67 31 36 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54  sg16()..*/.SQLIT
18141 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
18142 33 5f 72 65 73 65 74 28 73 71 6c 69 74 65 33 5f  3_reset(sqlite3_
18143 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
18144 69 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 70 53  int rc;.  if( pS
18145 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 63  tmt==0 ){.    rc
18146 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
18147 7d 65 6c 73 65 7b 0a 20 20 20 20 56 64 62 65 20  }else{.    Vdbe 
18148 2a 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d  *v = (Vdbe*)pStm
18149 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  t;.    sqlite3_m
1814a 75 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62  utex_enter(v->db
1814b 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63  ->mutex);.    rc
1814c 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65   = sqlite3VdbeRe
1814d 73 65 74 28 76 29 3b 0a 20 20 20 20 73 74 6d 74  set(v);.    stmt
1814e 4c 72 75 41 64 64 28 76 29 3b 0a 20 20 20 20 73  LruAdd(v);.    s
1814f 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 52 65  qlite3VdbeMakeRe
18150 61 64 79 28 76 2c 20 2d 31 2c 20 30 2c 20 30 2c  ady(v, -1, 0, 0,
18151 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
18152 20 28 72 63 20 26 20 28 76 2d 3e 64 62 2d 3e 65   (rc & (v->db->e
18153 72 72 4d 61 73 6b 29 29 3d 3d 72 63 20 29 3b 0a  rrMask))==rc );.
18154 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
18155 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d 3e 6d  x_leave(v->db->m
18156 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74  utex);.  }.  ret
18157 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
18158 20 53 65 74 20 61 6c 6c 20 74 68 65 20 70 61 72   Set all the par
18159 61 6d 65 74 65 72 73 20 69 6e 20 74 68 65 20 63  ameters in the c
1815a 6f 6d 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74  ompiled SQL stat
1815b 65 6d 65 6e 74 20 74 6f 20 4e 55 4c 4c 2e 0a 2a  ement to NULL..*
1815c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1815d 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62   sqlite3_clear_b
1815e 69 6e 64 69 6e 67 73 28 73 71 6c 69 74 65 33 5f  indings(sqlite3_
1815f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a 20 20  stmt *pStmt){.  
18160 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20  int i;.  int rc 
18161 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 56  = SQLITE_OK;.  V
18162 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
18163 70 53 74 6d 74 3b 0a 23 69 66 20 53 51 4c 49 54  pStmt;.#if SQLIT
18164 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20 73  E_THREADSAFE.  s
18165 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 6d 75  qlite3_mutex *mu
18166 74 65 78 20 3d 20 28 28 56 64 62 65 2a 29 70 53  tex = ((Vdbe*)pS
18167 74 6d 74 29 2d 3e 64 62 2d 3e 6d 75 74 65 78 3b  tmt)->db->mutex;
18168 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65  .#endif.  sqlite
18169 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d 75  3_mutex_enter(mu
1816a 74 65 78 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  tex);.  for(i=0;
1816b 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29   i<p->nVar; i++)
1816c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1816d 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 70 2d 3e  eMemRelease(&p->
1816e 61 56 61 72 5b 69 5d 29 3b 0a 20 20 20 20 70 2d  aVar[i]);.    p-
1816f 3e 61 56 61 72 5b 69 5d 2e 66 6c 61 67 73 20 3d  >aVar[i].flags =
18170 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 7d 0a 20   MEM_Null;.  }. 
18171 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
18172 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 72  eave(mutex);.  r
18173 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a  eturn rc;.}.../*
18174 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73 71 6c 69  *********** sqli
18176 74 65 33 5f 76 61 6c 75 65 5f 20 20 2a 2a 2a 2a  te3_value_  ****
18177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18178 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
18179 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
1817a 74 69 6e 65 73 20 65 78 74 72 61 63 74 20 69 6e  tines extract in
1817b 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61  formation from a
1817c 20 4d 65 6d 20 6f 72 20 73 71 6c 69 74 65 33 5f   Mem or sqlite3_
1817d 76 61 6c 75 65 0a 2a 2a 20 73 74 72 75 63 74 75  value.** structu
1817e 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  re..*/.SQLITE_AP
1817f 49 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71  I const void *sq
18180 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
18181 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
18182 70 56 61 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 20  pVal){.  Mem *p 
18183 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20  = (Mem*)pVal;.  
18184 69 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 28  if( p->flags & (
18185 4d 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72  MEM_Blob|MEM_Str
18186 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ) ){.    sqlite3
18187 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
18188 62 28 70 29 3b 0a 20 20 20 20 70 2d 3e 66 6c 61  b(p);.    p->fla
18189 67 73 20 26 3d 20 7e 4d 45 4d 5f 53 74 72 3b 0a  gs &= ~MEM_Str;.
1818a 20 20 20 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20      p->flags |= 
1818b 4d 45 4d 5f 42 6c 6f 62 3b 0a 20 20 20 20 72 65  MEM_Blob;.    re
1818c 74 75 72 6e 20 70 2d 3e 7a 3b 0a 20 20 7d 65 6c  turn p->z;.  }el
1818d 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73  se{.    return s
1818e 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
1818f 74 28 70 56 61 6c 29 3b 0a 20 20 7d 0a 7d 0a 53  t(pVal);.  }.}.S
18190 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
18191 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
18192 73 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  s(sqlite3_value 
18193 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e  *pVal){.  return
18194 20 73 71 6c 69 74 65 33 56 61 6c 75 65 42 79 74   sqlite3ValueByt
18195 65 73 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  es(pVal, SQLITE_
18196 55 54 46 38 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  UTF8);.}.SQLITE_
18197 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
18198 76 61 6c 75 65 5f 62 79 74 65 73 31 36 28 73 71  value_bytes16(sq
18199 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
1819a 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  l){.  return sql
1819b 69 74 65 33 56 61 6c 75 65 42 79 74 65 73 28 70  ite3ValueBytes(p
1819c 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Val, SQLITE_UTF1
1819d 36 4e 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49  6NATIVE);.}.SQLI
1819e 54 45 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71  TE_API double sq
1819f 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
181a0 6c 65 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  le(sqlite3_value
181a1 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74 75 72   *pVal){.  retur
181a2 6e 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61  n sqlite3VdbeRea
181a3 6c 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56 61  lValue((Mem*)pVa
181a4 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
181a5 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c   int sqlite3_val
181a6 75 65 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f 76  ue_int(sqlite3_v
181a7 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72  alue *pVal){.  r
181a8 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62  eturn sqlite3Vdb
181a9 65 49 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29  eIntValue((Mem*)
181aa 70 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f  pVal);.}.SQLITE_
181ab 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  API sqlite_int64
181ac 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
181ad 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 76 61 6c  nt64(sqlite3_val
181ae 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20 72 65 74  ue *pVal){.  ret
181af 75 72 6e 20 73 71 6c 69 74 65 33 56 64 62 65 49  urn sqlite3VdbeI
181b0 6e 74 56 61 6c 75 65 28 28 4d 65 6d 2a 29 70 56  ntValue((Mem*)pV
181b1 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50  al);.}.SQLITE_AP
181b2 49 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64  I const unsigned
181b3 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 76   char *sqlite3_v
181b4 61 6c 75 65 5f 74 65 78 74 28 73 71 6c 69 74 65  alue_text(sqlite
181b5 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a  3_value *pVal){.
181b6 20 20 72 65 74 75 72 6e 20 28 63 6f 6e 73 74 20    return (const 
181b7 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
181b8 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74  sqlite3ValueText
181b9 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54  (pVal, SQLITE_UT
181ba 46 38 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53  F8);.}.#ifndef S
181bb 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
181bc 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
181bd 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
181be 76 61 6c 75 65 5f 74 65 78 74 31 36 28 73 71 6c  value_text16(sql
181bf 69 74 65 33 5f 76 61 6c 75 65 2a 20 70 56 61 6c  ite3_value* pVal
181c0 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  ){.  return sqli
181c1 74 65 33 56 61 6c 75 65 54 65 78 74 28 70 56 61  te3ValueText(pVa
181c2 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e  l, SQLITE_UTF16N
181c3 41 54 49 56 45 29 3b 0a 7d 0a 53 51 4c 49 54 45  ATIVE);.}.SQLITE
181c4 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
181c5 2a 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  *sqlite3_value_t
181c6 65 78 74 31 36 62 65 28 73 71 6c 69 74 65 33 5f  ext16be(sqlite3_
181c7 76 61 6c 75 65 20 2a 70 56 61 6c 29 7b 0a 20 20  value *pVal){.  
181c8 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 56 61  return sqlite3Va
181c9 6c 75 65 54 65 78 74 28 70 56 61 6c 2c 20 53 51  lueText(pVal, SQ
181ca 4c 49 54 45 5f 55 54 46 31 36 42 45 29 3b 0a 7d  LITE_UTF16BE);.}
181cb 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73  .SQLITE_API cons
181cc 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f  t void *sqlite3_
181cd 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 28 73  value_text16le(s
181ce 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
181cf 61 6c 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  al){.  return sq
181d0 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
181d1 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31  Val, SQLITE_UTF1
181d2 36 4c 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f  6LE);.}.#endif /
181d3 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
181d4 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  F16 */.SQLITE_AP
181d5 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 76 61  I int sqlite3_va
181d6 6c 75 65 5f 74 79 70 65 28 73 71 6c 69 74 65 33  lue_type(sqlite3
181d7 5f 76 61 6c 75 65 2a 20 70 56 61 6c 29 7b 0a 20  _value* pVal){. 
181d8 20 72 65 74 75 72 6e 20 70 56 61 6c 2d 3e 74 79   return pVal->ty
181d9 70 65 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  pe;.}../********
181da 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181db 2a 2a 2a 2a 20 73 71 6c 69 74 65 33 5f 72 65 73  **** sqlite3_res
181dc 75 6c 74 5f 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ult_  **********
181dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
181de 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c  *****.** The fol
181df 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65 73 20  lowing routines 
181e0 61 72 65 20 75 73 65 64 20 62 79 20 75 73 65 72  are used by user
181e1 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
181e2 6e 73 20 74 6f 20 73 70 65 63 69 66 79 0a 2a 2a  ns to specify.**
181e3 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 72 65   the function re
181e4 73 75 6c 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  sult..*/.SQLITE_
181e5 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
181e6 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 0a 20 20  _result_blob(.  
181e7 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
181e8 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20  *pCtx, .  const 
181e9 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20  void *z, .  int 
181ea 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65  n, .  void (*xDe
181eb 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20  l)(void *).){.  
181ec 61 73 73 65 72 74 28 20 6e 3e 3d 30 20 29 3b 0a  assert( n>=0 );.
181ed 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
181ee 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
181ef 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
181f0 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
181f1 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
181f2 3e 73 2c 20 7a 2c 20 6e 2c 20 30 2c 20 78 44 65  >s, z, n, 0, xDe
181f3 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  l);.}.SQLITE_API
181f4 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
181f5 73 75 6c 74 5f 64 6f 75 62 6c 65 28 73 71 6c 69  sult_double(sqli
181f6 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
181f7 78 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c 29 7b  x, double rVal){
181f8 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
181f9 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
181fa 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
181fb 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
181fc 65 4d 65 6d 53 65 74 44 6f 75 62 6c 65 28 26 70  eMemSetDouble(&p
181fd 43 74 78 2d 3e 73 2c 20 72 56 61 6c 29 3b 0a 7d  Ctx->s, rVal);.}
181fe 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64  .SQLITE_API void
181ff 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
18200 65 72 72 6f 72 28 73 71 6c 69 74 65 33 5f 63 6f  error(sqlite3_co
18201 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 63 6f 6e  ntext *pCtx, con
18202 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
18203 6e 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  n){.  assert( sq
18204 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
18205 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
18206 65 78 29 20 29 3b 0a 20 20 70 43 74 78 2d 3e 69  ex) );.  pCtx->i
18207 73 45 72 72 6f 72 20 3d 20 53 51 4c 49 54 45 5f  sError = SQLITE_
18208 45 52 52 4f 52 3b 0a 20 20 73 71 6c 69 74 65 33  ERROR;.  sqlite3
18209 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 26 70  VdbeMemSetStr(&p
1820a 43 74 78 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51  Ctx->s, z, n, SQ
1820b 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
1820c 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 7d 0a  E_TRANSIENT);.}.
1820d 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1820e 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1820f 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
18210 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36  3_result_error16
18211 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
18212 20 2a 70 43 74 78 2c 20 63 6f 6e 73 74 20 76 6f   *pCtx, const vo
18213 69 64 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20  id *z, int n){. 
18214 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33   assert( sqlite3
18215 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78  _mutex_held(pCtx
18216 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29  ->s.db->mutex) )
18217 3b 0a 20 20 70 43 74 78 2d 3e 69 73 45 72 72 6f  ;.  pCtx->isErro
18218 72 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  r = SQLITE_ERROR
18219 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  ;.  sqlite3VdbeM
1821a 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e  emSetStr(&pCtx->
1821b 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f  s, z, n, SQLITE_
1821c 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53 51 4c  UTF16NATIVE, SQL
1821d 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
1821e 7d 0a 23 65 6e 64 69 66 0a 53 51 4c 49 54 45 5f  }.#endif.SQLITE_
1821f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33  API void sqlite3
18220 5f 72 65 73 75 6c 74 5f 69 6e 74 28 73 71 6c 69  _result_int(sqli
18221 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
18222 78 2c 20 69 6e 74 20 69 56 61 6c 29 7b 0a 20 20  x, int iVal){.  
18223 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
18224 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d  mutex_held(pCtx-
18225 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b  >s.db->mutex) );
18226 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
18227 6d 53 65 74 49 6e 74 36 34 28 26 70 43 74 78 2d  mSetInt64(&pCtx-
18228 3e 73 2c 20 28 69 36 34 29 69 56 61 6c 29 3b 0a  >s, (i64)iVal);.
18229 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  }.SQLITE_API voi
1822a 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  d sqlite3_result
1822b 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 63  _int64(sqlite3_c
1822c 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69 36  ontext *pCtx, i6
1822d 34 20 69 56 61 6c 29 7b 0a 20 20 61 73 73 65 72  4 iVal){.  asser
1822e 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
1822f 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
18230 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71  ->mutex) );.  sq
18231 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49  lite3VdbeMemSetI
18232 6e 74 36 34 28 26 70 43 74 78 2d 3e 73 2c 20 69  nt64(&pCtx->s, i
18233 56 61 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Val);.}.SQLITE_A
18234 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
18235 72 65 73 75 6c 74 5f 6e 75 6c 6c 28 73 71 6c 69  result_null(sqli
18236 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74  te3_context *pCt
18237 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71  x){.  assert( sq
18238 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
18239 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74  (pCtx->s.db->mut
1823a 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  ex) );.  sqlite3
1823b 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c 28 26  VdbeMemSetNull(&
1823c 70 43 74 78 2d 3e 73 29 3b 0a 7d 0a 53 51 4c 49  pCtx->s);.}.SQLI
1823d 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69  TE_API void sqli
1823e 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
1823f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
18240 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e  xt *pCtx, .  con
18241 73 74 20 63 68 61 72 20 2a 7a 2c 20 0a 20 20 69  st char *z, .  i
18242 6e 74 20 6e 2c 0a 20 20 76 6f 69 64 20 28 2a 78  nt n,.  void (*x
18243 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a  Del)(void *).){.
18244 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
18245 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43 74  3_mutex_held(pCt
18246 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20  x->s.db->mutex) 
18247 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
18248 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78 2d  MemSetStr(&pCtx-
18249 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45  >s, z, n, SQLITE
1824a 5f 55 54 46 38 2c 20 78 44 65 6c 29 3b 0a 7d 0a  _UTF8, xDel);.}.
1824b 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1824c 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
1824d 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
1824e 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 28  3_result_text16(
1824f 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
18250 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e  xt *pCtx, .  con
18251 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  st void *z, .  i
18252 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a  nt n, .  void (*
18253 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b  xDel)(void *).){
18254 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
18255 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
18256 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
18257 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
18258 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
18259 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
1825a 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 78  E_UTF16NATIVE, x
1825b 44 65 6c 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  Del);.}.SQLITE_A
1825c 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f  PI void sqlite3_
1825d 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62 65 28  result_text16be(
1825e 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1825f 78 74 20 2a 70 43 74 78 2c 20 0a 20 20 63 6f 6e  xt *pCtx, .  con
18260 73 74 20 76 6f 69 64 20 2a 7a 2c 20 0a 20 20 69  st void *z, .  i
18261 6e 74 20 6e 2c 20 0a 20 20 76 6f 69 64 20 28 2a  nt n, .  void (*
18262 78 44 65 6c 29 28 76 6f 69 64 20 2a 29 0a 29 7b  xDel)(void *).){
18263 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
18264 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 70 43  e3_mutex_held(pC
18265 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  tx->s.db->mutex)
18266 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62   );.  sqlite3Vdb
18267 65 4d 65 6d 53 65 74 53 74 72 28 26 70 43 74 78  eMemSetStr(&pCtx
18268 2d 3e 73 2c 20 7a 2c 20 6e 2c 20 53 51 4c 49 54  ->s, z, n, SQLIT
18269 45 5f 55 54 46 31 36 42 45 2c 20 78 44 65 6c 29  E_UTF16BE, xDel)
1826a 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  ;.}.SQLITE_API v
1826b 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75  oid sqlite3_resu
1826c 6c 74 5f 74 65 78 74 31 36 6c 65 28 0a 20 20 73  lt_text16le(.  s
1826d 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
1826e 70 43 74 78 2c 20 0a 20 20 63 6f 6e 73 74 20 76  pCtx, .  const v
1826f 6f 69 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e  oid *z, .  int n
18270 2c 20 0a 20 20 76 6f 69 64 20 28 2a 78 44 65 6c  , .  void (*xDel
18271 29 28 76 6f 69 64 20 2a 29 0a 29 7b 0a 20 20 61  )(void *).){.  a
18272 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
18273 75 74 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e  utex_held(pCtx->
18274 73 2e 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a  s.db->mutex) );.
18275 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
18276 53 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c  SetStr(&pCtx->s,
18277 20 7a 2c 20 6e 2c 20 53 51 4c 49 54 45 5f 55 54   z, n, SQLITE_UT
18278 46 31 36 4c 45 2c 20 78 44 65 6c 29 3b 0a 7d 0a  F16LE, xDel);.}.
18279 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1827a 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 53  _OMIT_UTF16 */.S
1827b 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73  QLITE_API void s
1827c 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61  qlite3_result_va
1827d 6c 75 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  lue(sqlite3_cont
1827e 65 78 74 20 2a 70 43 74 78 2c 20 73 71 6c 69 74  ext *pCtx, sqlit
1827f 65 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65  e3_value *pValue
18280 29 7b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  ){.  assert( sql
18281 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28  ite3_mutex_held(
18282 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65  pCtx->s.db->mute
18283 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  x) );.  sqlite3V
18284 64 62 65 4d 65 6d 43 6f 70 79 28 26 70 43 74 78  dbeMemCopy(&pCtx
18285 2d 3e 73 2c 20 70 56 61 6c 75 65 29 3b 0a 7d 0a  ->s, pValue);.}.
18286 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20  SQLITE_API void 
18287 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 7a  sqlite3_result_z
18288 65 72 6f 62 6c 6f 62 28 73 71 6c 69 74 65 33 5f  eroblob(sqlite3_
18289 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20 69  context *pCtx, i
1828a 6e 74 20 6e 29 7b 0a 20 20 61 73 73 65 72 74 28  nt n){.  assert(
1828b 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
1828c 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e  eld(pCtx->s.db->
1828d 6d 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69  mutex) );.  sqli
1828e 74 65 33 56 64 62 65 4d 65 6d 53 65 74 5a 65 72  te3VdbeMemSetZer
1828f 6f 42 6c 6f 62 28 26 70 43 74 78 2d 3e 73 2c 20  oBlob(&pCtx->s, 
18290 6e 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  n);.}.SQLITE_API
18291 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72 65   void sqlite3_re
18292 73 75 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 28  sult_error_code(
18293 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
18294 2a 70 43 74 78 2c 20 69 6e 74 20 65 72 72 43 6f  *pCtx, int errCo
18295 64 65 29 7b 0a 20 20 70 43 74 78 2d 3e 69 73 45  de){.  pCtx->isE
18296 72 72 6f 72 20 3d 20 65 72 72 43 6f 64 65 3b 0a  rror = errCode;.
18297 7d 0a 0a 2f 2a 20 46 6f 72 63 65 20 61 6e 20 53  }../* Force an S
18298 51 4c 49 54 45 5f 54 4f 4f 42 49 47 20 65 72 72  QLITE_TOOBIG err
18299 6f 72 2e 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50  or. */.SQLITE_AP
1829a 49 20 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 72  I void sqlite3_r
1829b 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62  esult_error_toob
1829c 69 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  ig(sqlite3_conte
1829d 78 74 20 2a 70 43 74 78 29 7b 0a 20 20 61 73 73  xt *pCtx){.  ass
1829e 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
1829f 65 78 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e  ex_held(pCtx->s.
182a0 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20  db->mutex) );.  
182a1 70 43 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20  pCtx->isError = 
182a2 53 51 4c 49 54 45 5f 54 4f 4f 42 49 47 3b 0a 20  SQLITE_TOOBIG;. 
182a3 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
182a4 65 74 53 74 72 28 26 70 43 74 78 2d 3e 73 2c 20  etStr(&pCtx->s, 
182a5 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20  "string or blob 
182a6 74 6f 6f 20 62 69 67 22 2c 20 2d 31 2c 20 0a 20  too big", -1, . 
182a7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
182a8 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46        SQLITE_UTF
182a9 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  8, SQLITE_STATIC
182aa 29 3b 0a 7d 0a 0a 2f 2a 20 41 6e 20 53 51 4c 49  );.}../* An SQLI
182ab 54 45 5f 4e 4f 4d 45 4d 20 65 72 72 6f 72 2e 20  TE_NOMEM error. 
182ac 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
182ad 69 64 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  id sqlite3_resul
182ae 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 28 73 71  t_error_nomem(sq
182af 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
182b0 43 74 78 29 7b 0a 20 20 61 73 73 65 72 74 28 20  Ctx){.  assert( 
182b1 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65  sqlite3_mutex_he
182b2 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d  ld(pCtx->s.db->m
182b3 75 74 65 78 29 20 29 3b 0a 20 20 73 71 6c 69 74  utex) );.  sqlit
182b4 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75 6c 6c  e3VdbeMemSetNull
182b5 28 26 70 43 74 78 2d 3e 73 29 3b 0a 20 20 70 43  (&pCtx->s);.  pC
182b6 74 78 2d 3e 69 73 45 72 72 6f 72 20 3d 20 53 51  tx->isError = SQ
182b7 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 70 43  LITE_NOMEM;.  pC
182b8 74 78 2d 3e 73 2e 64 62 2d 3e 6d 61 6c 6c 6f 63  tx->s.db->malloc
182b9 46 61 69 6c 65 64 20 3d 20 31 3b 0a 7d 0a 0a 2f  Failed = 1;.}../
182ba 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 74 68 65  *.** Execute the
182bb 20 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74   statement pStmt
182bc 2c 20 65 69 74 68 65 72 20 75 6e 74 69 6c 20 61  , either until a
182bd 20 72 6f 77 20 6f 66 20 64 61 74 61 20 69 73 20   row of data is 
182be 72 65 61 64 79 2c 20 74 68 65 0a 2a 2a 20 73 74  ready, the.** st
182bf 61 74 65 6d 65 6e 74 20 69 73 20 63 6f 6d 70 6c  atement is compl
182c0 65 74 65 6c 79 20 65 78 65 63 75 74 65 64 20 6f  etely executed o
182c1 72 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  r an error occur
182c2 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  s..**.** This ro
182c3 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
182c4 20 74 68 65 20 62 75 6c 6b 20 6f 66 20 74 68 65   the bulk of the
182c5 20 6c 6f 67 69 63 20 62 65 68 69 6e 64 20 74 68   logic behind th
182c6 65 20 73 71 6c 69 74 65 5f 73 74 65 70 28 29 0a  e sqlite_step().
182c7 2a 2a 20 41 50 49 2e 20 20 54 68 65 20 6f 6e 6c  ** API.  The onl
182c8 79 20 74 68 69 6e 67 20 6f 6d 69 74 74 65 64 20  y thing omitted 
182c9 69 73 20 74 68 65 20 61 75 74 6f 6d 61 74 69 63  is the automatic
182ca 20 72 65 63 6f 6d 70 69 6c 65 20 69 66 20 61 20   recompile if a 
182cb 0a 2a 2a 20 73 63 68 65 6d 61 20 63 68 61 6e 67  .** schema chang
182cc 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 2e 20  e has occurred. 
182cd 20 54 68 61 74 20 64 65 74 61 69 6c 20 69 73 20   That detail is 
182ce 68 61 6e 64 6c 65 64 20 62 79 20 74 68 65 0a 2a  handled by the.*
182cf 2a 20 6f 75 74 65 72 20 73 71 6c 69 74 65 33 5f  * outer sqlite3_
182d0 73 74 65 70 28 29 20 77 72 61 70 70 65 72 20 70  step() wrapper p
182d1 72 6f 63 65 64 75 72 65 2e 0a 2a 2f 0a 73 74 61  rocedure..*/.sta
182d2 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 53  tic int sqlite3S
182d3 74 65 70 28 56 64 62 65 20 2a 70 29 7b 0a 20 20  tep(Vdbe *p){.  
182d4 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69  sqlite3 *db;.  i
182d5 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74  nt rc;..  assert
182d6 28 70 29 3b 0a 20 20 69 66 28 20 70 2d 3e 6d 61  (p);.  if( p->ma
182d7 67 69 63 21 3d 56 44 42 45 5f 4d 41 47 49 43 5f  gic!=VDBE_MAGIC_
182d8 52 55 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  RUN ){.    retur
182d9 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
182da 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73 73 65 72  .  }..  /* Asser
182db 74 20 74 68 61 74 20 6d 61 6c 6c 6f 63 28 29 20  t that malloc() 
182dc 68 61 73 20 6e 6f 74 20 66 61 69 6c 65 64 20 2a  has not failed *
182dd 2f 0a 20 20 64 62 20 3d 20 70 2d 3e 64 62 3b 0a  /.  db = p->db;.
182de 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
182df 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65  Failed ){.    re
182e0 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
182e1 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 2d  M;.  }..  if( p-
182e2 3e 70 63 3c 3d 30 20 26 26 20 70 2d 3e 65 78 70  >pc<=0 && p->exp
182e3 69 72 65 64 20 29 7b 0a 20 20 20 20 69 66 28 20  ired ){.    if( 
182e4 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  p->rc==SQLITE_OK
182e5 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e 72 63 20   ){.      p->rc 
182e6 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 3b  = SQLITE_SCHEMA;
182e7 0a 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20  .    }.    rc = 
182e8 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
182e9 20 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 73 74    goto end_of_st
182ea 65 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 71  ep;.  }.  if( sq
182eb 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62  lite3SafetyOn(db
182ec 29 20 29 7b 0a 20 20 20 20 70 2d 3e 72 63 20 3d  ) ){.    p->rc =
182ed 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
182ee 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
182ef 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
182f0 69 66 28 20 70 2d 3e 70 63 3c 30 20 29 7b 0a 20  if( p->pc<0 ){. 
182f1 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
182f2 72 65 20 6e 6f 20 6f 74 68 65 72 20 73 74 61 74  re no other stat
182f3 65 6d 65 6e 74 73 20 63 75 72 72 65 6e 74 6c 79  ements currently
182f4 20 72 75 6e 6e 69 6e 67 2c 20 74 68 65 6e 0a 20   running, then. 
182f5 20 20 20 2a 2a 20 72 65 73 65 74 20 74 68 65 20     ** reset the 
182f6 69 6e 74 65 72 72 75 70 74 20 66 6c 61 67 2e 20  interrupt flag. 
182f7 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20 61   This prevents a
182f8 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
182f9 5f 69 6e 74 65 72 72 75 70 74 0a 20 20 20 20 2a  _interrupt.    *
182fa 2a 20 66 72 6f 6d 20 69 6e 74 65 72 72 75 70 74  * from interrupt
182fb 69 6e 67 20 61 20 73 74 61 74 65 6d 65 6e 74 20  ing a statement 
182fc 74 68 61 74 20 68 61 73 20 6e 6f 74 20 79 65 74  that has not yet
182fd 20 73 74 61 72 74 65 64 2e 0a 20 20 20 20 2a 2f   started..    */
182fe 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 61 63 74  .    if( db->act
182ff 69 76 65 56 64 62 65 43 6e 74 3d 3d 30 20 29 7b  iveVdbeCnt==0 ){
18300 0a 20 20 20 20 20 20 64 62 2d 3e 75 31 2e 69 73  .      db->u1.is
18301 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30 3b  Interrupted = 0;
18302 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  .    }..#ifndef 
18303 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
18304 45 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50  E.    if( db->xP
18305 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69  rofile && !db->i
18306 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
18307 20 20 64 6f 75 62 6c 65 20 72 4e 6f 77 3b 0a 20    double rNow;. 
18308 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73 43 75       sqlite3OsCu
18309 72 72 65 6e 74 54 69 6d 65 28 64 62 2d 3e 70 56  rrentTime(db->pV
1830a 66 73 2c 20 26 72 4e 6f 77 29 3b 0a 20 20 20 20  fs, &rNow);.    
1830b 20 20 70 2d 3e 73 74 61 72 74 54 69 6d 65 20 3d    p->startTime =
1830c 20 28 72 4e 6f 77 20 2d 20 28 69 6e 74 29 72 4e   (rNow - (int)rN
1830d 6f 77 29 2a 33 36 30 30 2e 30 2a 32 34 2e 30 2a  ow)*3600.0*24.0*
1830e 31 30 30 30 30 30 30 30 30 30 2e 30 3b 0a 20 20  1000000000.0;.  
1830f 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
18310 64 62 2d 3e 61 63 74 69 76 65 56 64 62 65 43 6e  db->activeVdbeCn
18311 74 2b 2b 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e  t++;.    if( p->
18312 72 65 61 64 4f 6e 6c 79 3d 3d 30 20 29 20 64 62  readOnly==0 ) db
18313 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74 2b 2b  ->writeVdbeCnt++
18314 3b 0a 20 20 20 20 70 2d 3e 70 63 20 3d 20 30 3b  ;.    p->pc = 0;
18315 0a 20 20 20 20 73 74 6d 74 4c 72 75 52 65 6d 6f  .    stmtLruRemo
18316 76 65 28 70 29 3b 0a 20 20 7d 0a 23 69 66 6e 64  ve(p);.  }.#ifnd
18317 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
18318 58 50 4c 41 49 4e 0a 20 20 69 66 28 20 70 2d 3e  XPLAIN.  if( p->
18319 65 78 70 6c 61 69 6e 20 29 7b 0a 20 20 20 20 72  explain ){.    r
1831a 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4c  c = sqlite3VdbeL
1831b 69 73 74 28 70 29 3b 0a 20 20 7d 65 6c 73 65 0a  ist(p);.  }else.
1831c 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1831d 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f  _OMIT_EXPLAIN */
1831e 0a 20 20 7b 0a 20 20 20 20 72 63 20 3d 20 73 71  .  {.    rc = sq
1831f 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 70 29  lite3VdbeExec(p)
18320 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 71 6c  ;.  }..  if( sql
18321 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62  ite3SafetyOff(db
18322 29 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  ) ){.    rc = SQ
18323 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
18324 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
18325 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 2f 2a  _OMIT_TRACE.  /*
18326 20 49 6e 76 6f 6b 65 20 74 68 65 20 70 72 6f 66   Invoke the prof
18327 69 6c 65 20 63 61 6c 6c 62 61 63 6b 20 69 66 20  ile callback if 
18328 74 68 65 72 65 20 69 73 20 6f 6e 65 0a 20 20 2a  there is one.  *
18329 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  /.  if( rc!=SQLI
1832a 54 45 5f 52 4f 57 20 26 26 20 64 62 2d 3e 78 50  TE_ROW && db->xP
1832b 72 6f 66 69 6c 65 20 26 26 20 21 64 62 2d 3e 69  rofile && !db->i
1832c 6e 69 74 2e 62 75 73 79 20 26 26 20 70 2d 3e 6e  nit.busy && p->n
1832d 4f 70 3e 30 0a 20 20 20 20 20 20 20 20 20 20 20  Op>0.           
1832e 26 26 20 70 2d 3e 61 4f 70 5b 30 5d 2e 6f 70 63  && p->aOp[0].opc
1832f 6f 64 65 3d 3d 4f 50 5f 54 72 61 63 65 20 26 26  ode==OP_Trace &&
18330 20 70 2d 3e 61 4f 70 5b 30 5d 2e 70 34 2e 7a 21   p->aOp[0].p4.z!
18331 3d 30 20 29 7b 0a 20 20 20 20 64 6f 75 62 6c 65  =0 ){.    double
18332 20 72 4e 6f 77 3b 0a 20 20 20 20 75 36 34 20 65   rNow;.    u64 e
18333 6c 61 70 73 65 54 69 6d 65 3b 0a 0a 20 20 20 20  lapseTime;..    
18334 73 71 6c 69 74 65 33 4f 73 43 75 72 72 65 6e 74  sqlite3OsCurrent
18335 54 69 6d 65 28 64 62 2d 3e 70 56 66 73 2c 20 26  Time(db->pVfs, &
18336 72 4e 6f 77 29 3b 0a 20 20 20 20 65 6c 61 70 73  rNow);.    elaps
18337 65 54 69 6d 65 20 3d 20 28 72 4e 6f 77 20 2d 20  eTime = (rNow - 
18338 28 69 6e 74 29 72 4e 6f 77 29 2a 33 36 30 30 2e  (int)rNow)*3600.
18339 30 2a 32 34 2e 30 2a 31 30 30 30 30 30 30 30 30  0*24.0*100000000
1833a 30 2e 30 20 2d 20 70 2d 3e 73 74 61 72 74 54 69  0.0 - p->startTi
1833b 6d 65 3b 0a 20 20 20 20 64 62 2d 3e 78 50 72 6f  me;.    db->xPro
1833c 66 69 6c 65 28 64 62 2d 3e 70 50 72 6f 66 69 6c  file(db->pProfil
1833d 65 41 72 67 2c 20 70 2d 3e 61 4f 70 5b 30 5d 2e  eArg, p->aOp[0].
1833e 70 34 2e 7a 2c 20 65 6c 61 70 73 65 54 69 6d 65  p4.z, elapseTime
1833f 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
18340 20 64 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72   db->errCode = r
18341 63 3b 0a 20 20 2f 2a 73 71 6c 69 74 65 33 45 72  c;.  /*sqlite3Er
18342 72 6f 72 28 70 2d 3e 64 62 2c 20 72 63 2c 20 30  ror(p->db, rc, 0
18343 29 3b 2a 2f 0a 20 20 70 2d 3e 72 63 20 3d 20 73  );*/.  p->rc = s
18344 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 70 2d  qlite3ApiExit(p-
18345 3e 64 62 2c 20 70 2d 3e 72 63 29 3b 0a 65 6e 64  >db, p->rc);.end
18346 5f 6f 66 5f 73 74 65 70 3a 0a 20 20 61 73 73 65  _of_step:.  asse
18347 72 74 28 20 28 72 63 26 30 78 66 66 29 3d 3d 72  rt( (rc&0xff)==r
18348 63 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 7a 53  c );.  if( p->zS
18349 71 6c 20 26 26 20 28 72 63 26 30 78 66 66 29 3c  ql && (rc&0xff)<
1834a 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
1834b 20 20 2f 2a 20 54 68 69 73 20 62 65 68 61 76 69    /* This behavi
1834c 6f 72 20 6f 63 63 75 72 73 20 69 66 20 73 71 6c  or occurs if sql
1834d 69 74 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28  ite3_prepare_v2(
1834e 29 20 77 61 73 20 75 73 65 64 20 74 6f 20 62 75  ) was used to bu
1834f 69 6c 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 70  ild.    ** the p
18350 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65 6e  repared statemen
18351 74 2e 20 20 52 65 74 75 72 6e 20 65 72 72 6f 72  t.  Return error
18352 20 63 6f 64 65 73 20 64 69 72 65 63 74 6c 79 20   codes directly 
18353 2a 2f 0a 20 20 20 20 70 2d 3e 64 62 2d 3e 65 72  */.    p->db->er
18354 72 43 6f 64 65 20 3d 20 70 2d 3e 72 63 3b 0a 20  rCode = p->rc;. 
18355 20 20 20 2f 2a 20 73 71 6c 69 74 65 33 45 72 72     /* sqlite3Err
18356 6f 72 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63 2c  or(p->db, p->rc,
18357 20 30 29 3b 20 2a 2f 0a 20 20 20 20 72 65 74 75   0); */.    retu
18358 72 6e 20 70 2d 3e 72 63 3b 0a 20 20 7d 65 6c 73  rn p->rc;.  }els
18359 65 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69  e{.    /* This i
1835a 73 20 66 6f 72 20 6c 65 67 61 63 79 20 73 71 6c  s for legacy sql
1835b 69 74 65 33 5f 70 72 65 70 61 72 65 28 29 20 62  ite3_prepare() b
1835c 75 69 6c 64 73 20 61 6e 64 20 77 68 65 6e 20 74  uilds and when t
1835d 68 65 20 63 6f 64 65 0a 20 20 20 20 2a 2a 20 69  he code.    ** i
1835e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 6f 72 20  s SQLITE_ROW or 
1835f 53 51 4c 49 54 45 5f 44 4f 4e 45 20 2a 2f 0a 20  SQLITE_DONE */. 
18360 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
18361 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  }.}../*.** This 
18362 69 73 20 74 68 65 20 74 6f 70 2d 6c 65 76 65 6c  is the top-level
18363 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
18364 6f 66 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  of sqlite3_step(
18365 29 2e 20 20 43 61 6c 6c 0a 2a 2a 20 73 71 6c 69  ).  Call.** sqli
18366 74 65 33 53 74 65 70 28 29 20 74 6f 20 64 6f 20  te3Step() to do 
18367 6d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b  most of the work
18368 2e 20 20 49 66 20 61 20 73 63 68 65 6d 61 20 65  .  If a schema e
18369 72 72 6f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20  rror occurs,.** 
1836a 63 61 6c 6c 20 73 71 6c 69 74 65 33 52 65 70 72  call sqlite3Repr
1836b 65 70 61 72 65 28 29 20 61 6e 64 20 74 72 79 20  epare() and try 
1836c 61 67 61 69 6e 2e 0a 2a 2f 0a 23 69 66 64 65 66  again..*/.#ifdef
1836d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52   SQLITE_OMIT_PAR
1836e 53 45 52 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  SER.SQLITE_API i
1836f 6e 74 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  nt sqlite3_step(
18370 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
18371 74 6d 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  tmt){.  int rc =
18372 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
18373 20 20 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20    if( pStmt ){. 
18374 20 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 20 20     Vdbe *v;.    
18375 76 20 3d 20 28 56 64 62 65 2a 29 70 53 74 6d 74  v = (Vdbe*)pStmt
18376 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
18377 74 65 78 5f 65 6e 74 65 72 28 76 2d 3e 64 62 2d  tex_enter(v->db-
18378 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72 63 20  >mutex);.    rc 
18379 3d 20 73 71 6c 69 74 65 33 53 74 65 70 28 76 29  = sqlite3Step(v)
1837a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75  ;.    sqlite3_mu
1837b 74 65 78 5f 6c 65 61 76 65 28 76 2d 3e 64 62 2d  tex_leave(v->db-
1837c 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
1837d 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6c 73  eturn rc;.}.#els
1837e 65 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  e.SQLITE_API int
1837f 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 71   sqlite3_step(sq
18380 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
18381 74 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  t){.  int rc = S
18382 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20  QLITE_MISUSE;.  
18383 69 66 28 20 70 53 74 6d 74 20 29 7b 0a 20 20 20  if( pStmt ){.   
18384 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 20 20   int cnt = 0;.  
18385 20 20 56 64 62 65 20 2a 76 20 3d 20 28 56 64 62    Vdbe *v = (Vdb
18386 65 2a 29 70 53 74 6d 74 3b 0a 20 20 20 20 73 71  e*)pStmt;.    sq
18387 6c 69 74 65 33 20 2a 64 62 20 3d 20 76 2d 3e 64  lite3 *db = v->d
18388 62 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  b;.    sqlite3_m
18389 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d  utex_enter(db->m
1838a 75 74 65 78 29 3b 0a 20 20 20 20 77 68 69 6c 65  utex);.    while
1838b 28 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33 53  ( (rc = sqlite3S
1838c 74 65 70 28 76 29 29 3d 3d 53 51 4c 49 54 45 5f  tep(v))==SQLITE_
1838d 53 43 48 45 4d 41 0a 20 20 20 20 20 20 20 20 20  SCHEMA.         
1838e 20 20 26 26 20 63 6e 74 2b 2b 20 3c 20 35 0a 20    && cnt++ < 5. 
1838f 20 20 20 20 20 20 20 20 20 20 26 26 20 76 64 62            && vdb
18390 65 52 65 70 72 65 70 61 72 65 28 76 29 20 29 7b  eReprepare(v) ){
18391 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
18392 65 73 65 74 28 70 53 74 6d 74 29 3b 0a 20 20 20  eset(pStmt);.   
18393 20 20 20 76 2d 3e 65 78 70 69 72 65 64 20 3d 20     v->expired = 
18394 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  0;.    }.    if(
18395 20 72 63 3d 3d 53 51 4c 49 54 45 5f 53 43 48 45   rc==SQLITE_SCHE
18396 4d 41 20 26 26 20 76 2d 3e 7a 53 71 6c 20 26 26  MA && v->zSql &&
18397 20 64 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20   db->pErr ){.   
18398 20 20 20 2f 2a 20 54 68 69 73 20 63 61 73 65 20     /* This case 
18399 6f 63 63 75 72 73 20 61 66 74 65 72 20 66 61 69  occurs after fai
1839a 6c 69 6e 67 20 74 6f 20 72 65 63 6f 6d 70 69 6c  ling to recompil
1839b 65 20 61 6e 20 73 71 6c 20 73 74 61 74 65 6d 65  e an sql stateme
1839c 6e 74 2e 20 0a 20 20 20 20 20 20 2a 2a 20 54 68  nt. .      ** Th
1839d 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
1839e 66 72 6f 6d 20 74 68 65 20 53 51 4c 20 63 6f 6d  from the SQL com
1839f 70 69 6c 65 72 20 68 61 73 20 61 6c 72 65 61 64  piler has alread
183a0 79 20 62 65 65 6e 20 6c 6f 61 64 65 64 20 0a 20  y been loaded . 
183a1 20 20 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65       ** into the
183a2 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
183a3 2e 20 54 68 69 73 20 62 6c 6f 63 6b 20 63 6f 70  . This block cop
183a4 69 65 73 20 74 68 65 20 65 72 72 6f 72 20 6d 65  ies the error me
183a5 73 73 61 67 65 20 0a 20 20 20 20 20 20 2a 2a 20  ssage .      ** 
183a6 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
183a7 65 20 68 61 6e 64 6c 65 20 69 6e 74 6f 20 74 68  e handle into th
183a8 65 20 73 74 61 74 65 6d 65 6e 74 20 61 6e 64 20  e statement and 
183a9 73 65 74 73 20 74 68 65 20 73 74 61 74 65 6d 65  sets the stateme
183aa 6e 74 0a 20 20 20 20 20 20 2a 2a 20 70 72 6f 67  nt.      ** prog
183ab 72 61 6d 20 63 6f 75 6e 74 65 72 20 74 6f 20 30  ram counter to 0
183ac 20 74 6f 20 65 6e 73 75 72 65 20 74 68 61 74 20   to ensure that 
183ad 77 68 65 6e 20 74 68 65 20 73 74 61 74 65 6d 65  when the stateme
183ae 6e 74 20 69 73 20 0a 20 20 20 20 20 20 2a 2a 20  nt is .      ** 
183af 66 69 6e 61 6c 69 7a 65 64 20 6f 72 20 72 65 73  finalized or res
183b0 65 74 20 74 68 65 20 70 61 72 73 65 72 20 65 72  et the parser er
183b1 72 6f 72 20 6d 65 73 73 61 67 65 20 69 73 20 61  ror message is a
183b2 76 61 69 6c 61 62 6c 65 20 76 69 61 0a 20 20 20  vailable via.   
183b3 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 65 72     ** sqlite3_er
183b4 72 6d 73 67 28 29 20 61 6e 64 20 73 71 6c 69 74  rmsg() and sqlit
183b5 65 33 5f 65 72 72 63 6f 64 65 28 29 2e 0a 20 20  e3_errcode()..  
183b6 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e      */.      con
183b7 73 74 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20  st char *zErr = 
183b8 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
183b9 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
183ba 28 64 62 2d 3e 70 45 72 72 29 3b 20 0a 20 20 20  (db->pErr); .   
183bb 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
183bc 28 64 62 2c 20 76 2d 3e 7a 45 72 72 4d 73 67 29  (db, v->zErrMsg)
183bd 3b 0a 20 20 20 20 20 20 69 66 28 20 21 64 62 2d  ;.      if( !db-
183be 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
183bf 0a 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72  .        v->zErr
183c0 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53  Msg = sqlite3DbS
183c1 74 72 44 75 70 28 64 62 2c 20 7a 45 72 72 29 3b  trDup(db, zErr);
183c2 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a  .      } else {.
183c3 20 20 20 20 20 20 20 20 76 2d 3e 7a 45 72 72 4d          v->zErrM
183c4 73 67 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  sg = 0;.        
183c5 76 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e  v->rc = SQLITE_N
183c6 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OMEM;.      }.  
183c7 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c    }.    rc = sql
183c8 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
183c9 72 63 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  rc);.    sqlite3
183ca 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
183cb 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 20 20 72  >mutex);.  }.  r
183cc 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64  eturn rc;.}.#end
183cd 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 74 72 61 63  if../*.** Extrac
183ce 74 20 74 68 65 20 75 73 65 72 20 64 61 74 61 20  t the user data 
183cf 66 72 6f 6d 20 61 20 73 71 6c 69 74 65 33 5f 63  from a sqlite3_c
183d0 6f 6e 74 65 78 74 20 73 74 72 75 63 74 75 72 65  ontext structure
183d1 20 61 6e 64 20 72 65 74 75 72 6e 20 61 0a 2a 2a   and return a.**
183d2 20 70 6f 69 6e 74 65 72 20 74 6f 20 69 74 2e 0a   pointer to it..
183d3 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f  */.SQLITE_API vo
183d4 69 64 20 2a 73 71 6c 69 74 65 33 5f 75 73 65 72  id *sqlite3_user
183d5 5f 64 61 74 61 28 73 71 6c 69 74 65 33 5f 63 6f  _data(sqlite3_co
183d6 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 73  ntext *p){.  ass
183d7 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75  ert( p && p->pFu
183d8 6e 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  nc );.  return p
183d9 2d 3e 70 46 75 6e 63 2d 3e 70 55 73 65 72 44 61  ->pFunc->pUserDa
183da 74 61 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  ta;.}../*.** Ext
183db 72 61 63 74 20 74 68 65 20 75 73 65 72 20 64 61  ract the user da
183dc 74 61 20 66 72 6f 6d 20 61 20 73 71 6c 69 74 65  ta from a sqlite
183dd 33 5f 63 6f 6e 74 65 78 74 20 73 74 72 75 63 74  3_context struct
183de 75 72 65 20 61 6e 64 20 72 65 74 75 72 6e 20 61  ure and return a
183df 0a 2a 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 69  .** pointer to i
183e0 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
183e1 20 73 71 6c 69 74 65 33 20 2a 73 71 6c 69 74 65   sqlite3 *sqlite
183e2 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
183e3 64 6c 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  dle(sqlite3_cont
183e4 65 78 74 20 2a 70 29 7b 0a 20 20 61 73 73 65 72  ext *p){.  asser
183e5 74 28 20 70 20 26 26 20 70 2d 3e 70 46 75 6e 63  t( p && p->pFunc
183e6 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d 3e   );.  return p->
183e7 73 2e 64 62 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  s.db;.}../*.** T
183e8 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
183e9 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  the implementati
183ea 6f 6e 20 6f 66 20 61 6e 20 53 51 4c 20 66 75 6e  on of an SQL fun
183eb 63 74 69 6f 6e 20 74 68 61 74 20 61 6c 77 61 79  ction that alway
183ec 73 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68 20  s.** fails with 
183ed 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
183ee 20 73 74 61 74 69 6e 67 20 74 68 61 74 20 74 68   stating that th
183ef 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  e function is us
183f0 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 77 72 6f  ed in the.** wro
183f1 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 54 68 65  ng context.  The
183f2 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
183f3 64 5f 66 75 6e 63 74 69 6f 6e 28 29 20 41 50 49  d_function() API
183f4 20 6d 69 67 68 74 20 63 6f 6e 73 74 72 75 63 74   might construct
183f5 0a 2a 2a 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e  .** SQL function
183f6 20 74 68 61 74 20 75 73 65 20 74 68 69 73 20 72   that use this r
183f7 6f 75 74 69 6e 65 20 73 6f 20 74 68 61 74 20 74  outine so that t
183f8 68 65 20 66 75 6e 63 74 69 6f 6e 73 20 77 69 6c  he functions wil
183f9 6c 20 65 78 69 73 74 0a 2a 2a 20 66 6f 72 20 6e  l exist.** for n
183fa 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20 62  ame resolution b
183fb 75 74 20 61 72 65 20 61 63 74 75 61 6c 6c 79 20  ut are actually 
183fc 6f 76 65 72 6c 6f 61 64 65 64 20 62 79 20 74 68  overloaded by th
183fd 65 20 78 46 69 6e 64 46 75 6e 63 74 69 6f 6e 0a  e xFindFunction.
183fe 2a 2a 20 6d 65 74 68 6f 64 20 6f 66 20 76 69 72  ** method of vir
183ff 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a 2a 2f 0a  tual tables..*/.
18400 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
18401 6f 69 64 20 73 71 6c 69 74 65 33 49 6e 76 61 6c  oid sqlite3Inval
18402 69 64 46 75 6e 63 74 69 6f 6e 28 0a 20 20 73 71  idFunction(.  sq
18403 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
18404 6f 6e 74 65 78 74 2c 20 20 2f 2a 20 54 68 65 20  ontext,  /* The 
18405 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 69 6e 67  function calling
18406 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e   context */.  in
18407 74 20 4e 6f 74 55 73 65 64 2c 20 20 20 20 20 20  t NotUsed,      
18408 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
18409 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20  er of arguments 
1840a 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
1840b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  */.  sqlite3_val
1840c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 20 20 20  ue **NotUsed2   
1840d 2f 2a 20 56 61 6c 75 65 20 6f 66 20 65 61 63 68  /* Value of each
1840e 20 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 29 7b 0a   argument */.){.
1840f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
18410 61 6d 65 20 3d 20 63 6f 6e 74 65 78 74 2d 3e 70  ame = context->p
18411 46 75 6e 63 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 63  Func->zName;.  c
18412 68 61 72 20 2a 7a 45 72 72 3b 0a 20 20 55 4e 55  har *zErr;.  UNU
18413 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28 4e  SED_PARAMETER2(N
18414 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64 32  otUsed, NotUsed2
18415 29 3b 0a 20 20 7a 45 72 72 20 3d 20 73 71 6c 69  );.  zErr = sqli
18416 74 65 33 4d 50 72 69 6e 74 66 28 30 2c 0a 20 20  te3MPrintf(0,.  
18417 20 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 75      "unable to u
18418 73 65 20 66 75 6e 63 74 69 6f 6e 20 25 73 20 69  se function %s i
18419 6e 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  n the requested 
1841a 63 6f 6e 74 65 78 74 22 2c 20 7a 4e 61 6d 65 29  context", zName)
1841b 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
1841c 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
1841d 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 73  , zErr, -1);.  s
1841e 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72 72  qlite3_free(zErr
1841f 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f  );.}../*.** Allo
18420 63 61 74 65 20 6f 72 20 72 65 74 75 72 6e 20 74  cate or return t
18421 68 65 20 61 67 67 72 65 67 61 74 65 20 63 6f 6e  he aggregate con
18422 74 65 78 74 20 66 6f 72 20 61 20 75 73 65 72 20  text for a user 
18423 66 75 6e 63 74 69 6f 6e 2e 20 20 41 20 6e 65 77  function.  A new
18424 0a 2a 2a 20 63 6f 6e 74 65 78 74 20 69 73 20 61  .** context is a
18425 6c 6c 6f 63 61 74 65 64 20 6f 6e 20 74 68 65 20  llocated on the 
18426 66 69 72 73 74 20 63 61 6c 6c 2e 20 20 53 75 62  first call.  Sub
18427 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 72 65  sequent calls re
18428 74 75 72 6e 20 74 68 65 0a 2a 2a 20 73 61 6d 65  turn the.** same
18429 20 63 6f 6e 74 65 78 74 20 74 68 61 74 20 77 61   context that wa
1842a 73 20 72 65 74 75 72 6e 65 64 20 6f 6e 20 70 72  s returned on pr
1842b 69 6f 72 20 63 61 6c 6c 73 2e 0a 2a 2f 0a 53 51  ior calls..*/.SQ
1842c 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a 73  LITE_API void *s
1842d 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
1842e 5f 63 6f 6e 74 65 78 74 28 73 71 6c 69 74 65 33  _context(sqlite3
1842f 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74  _context *p, int
18430 20 6e 42 79 74 65 29 7b 0a 20 20 4d 65 6d 20 2a   nByte){.  Mem *
18431 70 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20  pMem;.  assert( 
18432 70 20 26 26 20 70 2d 3e 70 46 75 6e 63 20 26 26  p && p->pFunc &&
18433 20 70 2d 3e 70 46 75 6e 63 2d 3e 78 53 74 65 70   p->pFunc->xStep
18434 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
18435 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
18436 28 70 2d 3e 73 2e 64 62 2d 3e 6d 75 74 65 78 29  (p->s.db->mutex)
18437 20 29 3b 0a 20 20 70 4d 65 6d 20 3d 20 70 2d 3e   );.  pMem = p->
18438 70 4d 65 6d 3b 0a 20 20 69 66 28 20 28 70 4d 65  pMem;.  if( (pMe
18439 6d 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 41  m->flags & MEM_A
1843a 67 67 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  gg)==0 ){.    if
1843b 28 20 6e 42 79 74 65 3d 3d 30 20 29 7b 0a 20 20  ( nByte==0 ){.  
1843c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
1843d 65 6d 52 65 6c 65 61 73 65 45 78 74 65 72 6e 61  emReleaseExterna
1843e 6c 28 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 70  l(pMem);.      p
1843f 4d 65 6d 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Mem->flags = MEM
18440 5f 4e 75 6c 6c 3b 0a 20 20 20 20 20 20 70 4d 65  _Null;.      pMe
18441 6d 2d 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 7d 65  m->z = 0;.    }e
18442 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
18443 65 33 56 64 62 65 4d 65 6d 47 72 6f 77 28 70 4d  e3VdbeMemGrow(pM
18444 65 6d 2c 20 6e 42 79 74 65 2c 20 30 29 3b 0a 20  em, nByte, 0);. 
18445 20 20 20 20 20 70 4d 65 6d 2d 3e 66 6c 61 67 73       pMem->flags
18446 20 3d 20 4d 45 4d 5f 41 67 67 3b 0a 20 20 20 20   = MEM_Agg;.    
18447 20 20 70 4d 65 6d 2d 3e 75 2e 70 44 65 66 20 3d    pMem->u.pDef =
18448 20 70 2d 3e 70 46 75 6e 63 3b 0a 20 20 20 20 20   p->pFunc;.     
18449 20 69 66 28 20 70 4d 65 6d 2d 3e 7a 20 29 7b 0a   if( pMem->z ){.
1844a 20 20 20 20 20 20 20 20 6d 65 6d 73 65 74 28 70          memset(p
1844b 4d 65 6d 2d 3e 7a 2c 20 30 2c 20 6e 42 79 74 65  Mem->z, 0, nByte
1844c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1844d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 28 76  .  }.  return (v
1844e 6f 69 64 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 7d 0a  oid*)pMem->z;.}.
1844f 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18450 65 20 61 75 78 69 6c 61 72 79 20 64 61 74 61 20  e auxilary data 
18451 70 6f 69 6e 74 65 72 2c 20 69 66 20 61 6e 79 2c  pointer, if any,
18452 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74 68   for the iArg'th
18453 20 61 72 67 75 6d 65 6e 74 20 74 6f 0a 2a 2a 20   argument to.** 
18454 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69 6f  the user-functio
18455 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43 74  n defined by pCt
18456 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  x..*/.SQLITE_API
18457 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 67   void *sqlite3_g
18458 65 74 5f 61 75 78 64 61 74 61 28 73 71 6c 69 74  et_auxdata(sqlit
18459 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
1845a 2c 20 69 6e 74 20 69 41 72 67 29 7b 0a 20 20 56  , int iArg){.  V
1845b 64 62 65 46 75 6e 63 20 2a 70 56 64 62 65 46 75  dbeFunc *pVdbeFu
1845c 6e 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 73  nc;..  assert( s
1845d 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c  qlite3_mutex_hel
1845e 64 28 70 43 74 78 2d 3e 73 2e 64 62 2d 3e 6d 75  d(pCtx->s.db->mu
1845f 74 65 78 29 20 29 3b 0a 20 20 70 56 64 62 65 46  tex) );.  pVdbeF
18460 75 6e 63 20 3d 20 70 43 74 78 2d 3e 70 56 64 62  unc = pCtx->pVdb
18461 65 46 75 6e 63 3b 0a 20 20 69 66 28 20 21 70 56  eFunc;.  if( !pV
18462 64 62 65 46 75 6e 63 20 7c 7c 20 69 41 72 67 3e  dbeFunc || iArg>
18463 3d 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78  =pVdbeFunc->nAux
18464 20 7c 7c 20 69 41 72 67 3c 30 20 29 7b 0a 20 20   || iArg<0 ){.  
18465 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a    return 0;.  }.
18466 20 20 72 65 74 75 72 6e 20 70 56 64 62 65 46 75    return pVdbeFu
18467 6e 63 2d 3e 61 70 41 75 78 5b 69 41 72 67 5d 2e  nc->apAux[iArg].
18468 70 41 75 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53  pAux;.}../*.** S
18469 65 74 20 74 68 65 20 61 75 78 69 6c 61 72 79 20  et the auxilary 
1846a 64 61 74 61 20 70 6f 69 6e 74 65 72 20 61 6e 64  data pointer and
1846b 20 64 65 6c 65 74 65 20 66 75 6e 63 74 69 6f 6e   delete function
1846c 2c 20 66 6f 72 20 74 68 65 20 69 41 72 67 27 74  , for the iArg't
1846d 68 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 74 6f  h.** argument to
1846e 20 74 68 65 20 75 73 65 72 2d 66 75 6e 63 74 69   the user-functi
1846f 6f 6e 20 64 65 66 69 6e 65 64 20 62 79 20 70 43  on defined by pC
18470 74 78 2e 20 41 6e 79 20 70 72 65 76 69 6f 75 73  tx. Any previous
18471 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 64 65 6c   value is.** del
18472 65 74 65 64 20 62 79 20 63 61 6c 6c 69 6e 67 20  eted by calling 
18473 74 68 65 20 64 65 6c 65 74 65 20 66 75 6e 63 74  the delete funct
18474 69 6f 6e 20 73 70 65 63 69 66 69 65 64 20 77 68  ion specified wh
18475 65 6e 20 69 74 20 77 61 73 20 73 65 74 2e 0a 2a  en it was set..*
18476 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
18477 64 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75  d sqlite3_set_au
18478 78 64 61 74 61 28 0a 20 20 73 71 6c 69 74 65 33  xdata(.  sqlite3
18479 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78 2c 20  _context *pCtx, 
1847a 0a 20 20 69 6e 74 20 69 41 72 67 2c 20 0a 20 20  .  int iArg, .  
1847b 76 6f 69 64 20 2a 70 41 75 78 2c 20 0a 20 20 76  void *pAux, .  v
1847c 6f 69 64 20 28 2a 78 44 65 6c 65 74 65 29 28 76  oid (*xDelete)(v
1847d 6f 69 64 2a 29 0a 29 7b 0a 20 20 73 74 72 75 63  oid*).){.  struc
1847e 74 20 41 75 78 44 61 74 61 20 2a 70 41 75 78 44  t AuxData *pAuxD
1847f 61 74 61 3b 0a 20 20 56 64 62 65 46 75 6e 63 20  ata;.  VdbeFunc 
18480 2a 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66  *pVdbeFunc;.  if
18481 28 20 69 41 72 67 3c 30 20 29 20 67 6f 74 6f 20  ( iArg<0 ) goto 
18482 66 61 69 6c 65 64 3b 0a 0a 20 20 61 73 73 65 72  failed;..  asser
18483 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
18484 5f 68 65 6c 64 28 70 43 74 78 2d 3e 73 2e 64 62  _held(pCtx->s.db
18485 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 70 56  ->mutex) );.  pV
18486 64 62 65 46 75 6e 63 20 3d 20 70 43 74 78 2d 3e  dbeFunc = pCtx->
18487 70 56 64 62 65 46 75 6e 63 3b 0a 20 20 69 66 28  pVdbeFunc;.  if(
18488 20 21 70 56 64 62 65 46 75 6e 63 20 7c 7c 20 70   !pVdbeFunc || p
18489 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 3c 3d  VdbeFunc->nAux<=
1848a 69 41 72 67 20 29 7b 0a 20 20 20 20 69 6e 74 20  iArg ){.    int 
1848b 6e 41 75 78 20 3d 20 28 70 56 64 62 65 46 75 6e  nAux = (pVdbeFun
1848c 63 20 3f 20 70 56 64 62 65 46 75 6e 63 2d 3e 6e  c ? pVdbeFunc->n
1848d 41 75 78 20 3a 20 30 29 3b 0a 20 20 20 20 69 6e  Aux : 0);.    in
1848e 74 20 6e 4d 61 6c 6c 6f 63 20 3d 20 73 69 7a 65  t nMalloc = size
1848f 6f 66 28 56 64 62 65 46 75 6e 63 29 20 2b 20 73  of(VdbeFunc) + s
18490 69 7a 65 6f 66 28 73 74 72 75 63 74 20 41 75 78  izeof(struct Aux
18491 44 61 74 61 29 2a 69 41 72 67 3b 0a 20 20 20 20  Data)*iArg;.    
18492 70 56 64 62 65 46 75 6e 63 20 3d 20 73 71 6c 69  pVdbeFunc = sqli
18493 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 70 43 74  te3DbRealloc(pCt
18494 78 2d 3e 73 2e 64 62 2c 20 70 56 64 62 65 46 75  x->s.db, pVdbeFu
18495 6e 63 2c 20 6e 4d 61 6c 6c 6f 63 29 3b 0a 20 20  nc, nMalloc);.  
18496 20 20 69 66 28 20 21 70 56 64 62 65 46 75 6e 63    if( !pVdbeFunc
18497 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 66   ){.      goto f
18498 61 69 6c 65 64 3b 0a 20 20 20 20 7d 0a 20 20 20  ailed;.    }.   
18499 20 70 43 74 78 2d 3e 70 56 64 62 65 46 75 6e 63   pCtx->pVdbeFunc
1849a 20 3d 20 70 56 64 62 65 46 75 6e 63 3b 0a 20 20   = pVdbeFunc;.  
1849b 20 20 6d 65 6d 73 65 74 28 26 70 56 64 62 65 46    memset(&pVdbeF
1849c 75 6e 63 2d 3e 61 70 41 75 78 5b 6e 41 75 78 5d  unc->apAux[nAux]
1849d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 74 72 75  , 0, sizeof(stru
1849e 63 74 20 41 75 78 44 61 74 61 29 2a 28 69 41 72  ct AuxData)*(iAr
1849f 67 2b 31 2d 6e 41 75 78 29 29 3b 0a 20 20 20 20  g+1-nAux));.    
184a0 70 56 64 62 65 46 75 6e 63 2d 3e 6e 41 75 78 20  pVdbeFunc->nAux 
184a1 3d 20 69 41 72 67 2b 31 3b 0a 20 20 20 20 70 56  = iArg+1;.    pV
184a2 64 62 65 46 75 6e 63 2d 3e 70 46 75 6e 63 20 3d  dbeFunc->pFunc =
184a3 20 70 43 74 78 2d 3e 70 46 75 6e 63 3b 0a 20 20   pCtx->pFunc;.  
184a4 7d 0a 0a 20 20 70 41 75 78 44 61 74 61 20 3d 20  }..  pAuxData = 
184a5 26 70 56 64 62 65 46 75 6e 63 2d 3e 61 70 41 75  &pVdbeFunc->apAu
184a6 78 5b 69 41 72 67 5d 3b 0a 20 20 69 66 28 20 70  x[iArg];.  if( p
184a7 41 75 78 44 61 74 61 2d 3e 70 41 75 78 20 26 26  AuxData->pAux &&
184a8 20 70 41 75 78 44 61 74 61 2d 3e 78 44 65 6c 65   pAuxData->xDele
184a9 74 65 20 29 7b 0a 20 20 20 20 70 41 75 78 44 61  te ){.    pAuxDa
184aa 74 61 2d 3e 78 44 65 6c 65 74 65 28 70 41 75 78  ta->xDelete(pAux
184ab 44 61 74 61 2d 3e 70 41 75 78 29 3b 0a 20 20 7d  Data->pAux);.  }
184ac 0a 20 20 70 41 75 78 44 61 74 61 2d 3e 70 41 75  .  pAuxData->pAu
184ad 78 20 3d 20 70 41 75 78 3b 0a 20 20 70 41 75 78  x = pAux;.  pAux
184ae 44 61 74 61 2d 3e 78 44 65 6c 65 74 65 20 3d 20  Data->xDelete = 
184af 78 44 65 6c 65 74 65 3b 0a 20 20 72 65 74 75 72  xDelete;.  retur
184b0 6e 3b 0a 0a 66 61 69 6c 65 64 3a 0a 20 20 69 66  n;..failed:.  if
184b1 28 20 78 44 65 6c 65 74 65 20 29 7b 0a 20 20 20  ( xDelete ){.   
184b2 20 78 44 65 6c 65 74 65 28 70 41 75 78 29 3b 0a   xDelete(pAux);.
184b3 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53    }.}..#ifndef S
184b4 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
184b5 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 52 65 74 75  CATED./*.** Retu
184b6 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  rn the number of
184b7 20 74 69 6d 65 73 20 74 68 65 20 53 74 65 70 20   times the Step 
184b8 66 75 6e 63 74 69 6f 6e 20 6f 66 20 61 20 61 67  function of a ag
184b9 67 72 65 67 61 74 65 20 68 61 73 20 62 65 65 6e  gregate has been
184ba 20 0a 2a 2a 20 63 61 6c 6c 65 64 2e 0a 2a 2a 0a   .** called..**.
184bb 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
184bc 20 69 73 20 64 65 70 72 65 63 61 74 65 64 2e 20   is deprecated. 
184bd 20 44 6f 20 6e 6f 74 20 75 73 65 20 69 74 20 66   Do not use it f
184be 6f 72 20 6e 65 77 20 63 6f 64 65 2e 20 20 49 74  or new code.  It
184bf 20 69 73 0a 2a 2a 20 70 72 6f 76 69 64 65 20 6f   is.** provide o
184c0 6e 6c 79 20 74 6f 20 61 76 6f 69 64 20 62 72 65  nly to avoid bre
184c1 61 6b 69 6e 67 20 6c 65 67 61 63 79 20 63 6f 64  aking legacy cod
184c2 65 2e 20 20 4e 65 77 20 61 67 67 72 65 67 61 74  e.  New aggregat
184c3 65 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 6d  e function.** im
184c4 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 73 20 73 68  plementations sh
184c5 6f 75 6c 64 20 6b 65 65 70 20 74 68 65 69 72 20  ould keep their 
184c6 6f 77 6e 20 63 6f 75 6e 74 73 20 77 69 74 68 69  own counts withi
184c7 6e 20 74 68 65 69 72 20 61 67 67 72 65 67 61 74  n their aggregat
184c8 65 0a 2a 2a 20 63 6f 6e 74 65 78 74 2e 0a 2a 2f  e.** context..*/
184c9 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
184ca 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
184cb 65 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65 33 5f  e_count(sqlite3_
184cc 63 6f 6e 74 65 78 74 20 2a 70 29 7b 0a 20 20 61  context *p){.  a
184cd 73 73 65 72 74 28 20 70 20 26 26 20 70 2d 3e 70  ssert( p && p->p
184ce 46 75 6e 63 20 26 26 20 70 2d 3e 70 46 75 6e 63  Func && p->pFunc
184cf 2d 3e 78 53 74 65 70 20 29 3b 0a 20 20 72 65 74  ->xStep );.  ret
184d0 75 72 6e 20 70 2d 3e 70 4d 65 6d 2d 3e 6e 3b 0a  urn p->pMem->n;.
184d1 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
184d2 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
184d3 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
184d4 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 66  the result set f
184d5 6f 72 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74  or the statement
184d6 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54   pStmt..*/.SQLIT
184d7 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
184d8 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 73  3_column_count(s
184d9 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
184da 6d 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d  mt){.  Vdbe *pVm
184db 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74   = (Vdbe *)pStmt
184dc 3b 0a 20 20 72 65 74 75 72 6e 20 70 56 6d 20 3f  ;.  return pVm ?
184dd 20 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e   pVm->nResColumn
184de 20 3a 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52   : 0;.}../*.** R
184df 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
184e0 20 6f 66 20 76 61 6c 75 65 73 20 61 76 61 69 6c   of values avail
184e1 61 62 6c 65 20 66 72 6f 6d 20 74 68 65 20 63 75  able from the cu
184e2 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65  rrent row of the
184e3 0a 2a 2a 20 63 75 72 72 65 6e 74 6c 79 20 65 78  .** currently ex
184e4 65 63 75 74 69 6e 67 20 73 74 61 74 65 6d 65 6e  ecuting statemen
184e5 74 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49  t pStmt..*/.SQLI
184e6 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
184e7 65 33 5f 64 61 74 61 5f 63 6f 75 6e 74 28 73 71  e3_data_count(sq
184e8 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
184e9 74 29 7b 0a 20 20 56 64 62 65 20 2a 70 56 6d 20  t){.  Vdbe *pVm 
184ea 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d 74 3b  = (Vdbe *)pStmt;
184eb 0a 20 20 69 66 28 20 70 56 6d 3d 3d 30 20 7c 7c  .  if( pVm==0 ||
184ec 20 70 56 6d 2d 3e 70 52 65 73 75 6c 74 53 65 74   pVm->pResultSet
184ed 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
184ee 20 20 72 65 74 75 72 6e 20 70 56 6d 2d 3e 6e 52    return pVm->nR
184ef 65 73 43 6f 6c 75 6d 6e 3b 0a 7d 0a 0a 0a 2f 2a  esColumn;.}.../*
184f0 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65  .** Check to see
184f1 20 69 66 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 20   if column iCol 
184f2 6f 66 20 74 68 65 20 67 69 76 65 6e 20 73 74 61  of the given sta
184f3 74 65 6d 65 6e 74 20 69 73 20 76 61 6c 69 64 2e  tement is valid.
184f4 20 20 49 66 0a 2a 2a 20 69 74 20 69 73 2c 20 72    If.** it is, r
184f5 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
184f6 74 6f 20 74 68 65 20 4d 65 6d 20 66 6f 72 20 74  to the Mem for t
184f7 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 61 74  he value of that
184f8 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 20 49 66 20 69   column..** If i
184f9 43 6f 6c 20 69 73 20 6e 6f 74 20 76 61 6c 69 64  Col is not valid
184fa 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74  , return a point
184fb 65 72 20 74 6f 20 61 20 4d 65 6d 20 77 68 69 63  er to a Mem whic
184fc 68 20 68 61 73 20 61 20 76 61 6c 75 65 0a 2a 2a  h has a value.**
184fd 20 6f 66 20 4e 55 4c 4c 2e 0a 2a 2f 0a 73 74 61   of NULL..*/.sta
184fe 74 69 63 20 4d 65 6d 20 2a 63 6f 6c 75 6d 6e 4d  tic Mem *columnM
184ff 65 6d 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  em(sqlite3_stmt 
18500 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a  *pStmt, int i){.
18501 20 20 56 64 62 65 20 2a 70 56 6d 3b 0a 20 20 69    Vdbe *pVm;.  i
18502 6e 74 20 76 61 6c 73 3b 0a 20 20 4d 65 6d 20 2a  nt vals;.  Mem *
18503 70 4f 75 74 3b 0a 0a 20 20 70 56 6d 20 3d 20 28  pOut;..  pVm = (
18504 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
18505 69 66 28 20 70 56 6d 20 26 26 20 70 56 6d 2d 3e  if( pVm && pVm->
18506 70 52 65 73 75 6c 74 53 65 74 21 3d 30 20 26 26  pResultSet!=0 &&
18507 20 69 3c 70 56 6d 2d 3e 6e 52 65 73 43 6f 6c 75   i<pVm->nResColu
18508 6d 6e 20 26 26 20 69 3e 3d 30 20 29 7b 0a 20 20  mn && i>=0 ){.  
18509 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
1850a 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62 2d 3e 6d  enter(pVm->db->m
1850b 75 74 65 78 29 3b 0a 20 20 20 20 76 61 6c 73 20  utex);.    vals 
1850c 3d 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63  = sqlite3_data_c
1850d 6f 75 6e 74 28 70 53 74 6d 74 29 3b 0a 20 20 20  ount(pStmt);.   
1850e 20 70 4f 75 74 20 3d 20 26 70 56 6d 2d 3e 70 52   pOut = &pVm->pR
1850f 65 73 75 6c 74 53 65 74 5b 69 5d 3b 0a 20 20 7d  esultSet[i];.  }
18510 65 6c 73 65 7b 0a 20 20 20 20 73 74 61 74 69 63  else{.    static
18511 20 63 6f 6e 73 74 20 4d 65 6d 20 6e 75 6c 6c 4d   const Mem nullM
18512 65 6d 20 3d 20 7b 7b 30 7d 2c 20 30 2e 30 2c 20  em = {{0}, 0.0, 
18513 30 2c 20 22 22 2c 20 30 2c 20 4d 45 4d 5f 4e 75  0, "", 0, MEM_Nu
18514 6c 6c 2c 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 2c  ll, SQLITE_NULL,
18515 20 30 2c 20 30 2c 20 30 20 7d 3b 0a 20 20 20 20   0, 0, 0 };.    
18516 69 66 28 20 70 56 6d 2d 3e 64 62 20 29 7b 0a 20  if( pVm->db ){. 
18517 20 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74       sqlite3_mut
18518 65 78 5f 65 6e 74 65 72 28 70 56 6d 2d 3e 64 62  ex_enter(pVm->db
18519 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20 20  ->mutex);.      
1851a 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70 56 6d  sqlite3Error(pVm
1851b 2d 3e 64 62 2c 20 53 51 4c 49 54 45 5f 52 41 4e  ->db, SQLITE_RAN
1851c 47 45 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  GE, 0);.    }.  
1851d 20 20 70 4f 75 74 20 3d 20 28 4d 65 6d 2a 29 26    pOut = (Mem*)&
1851e 6e 75 6c 6c 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72  nullMem;.  }.  r
1851f 65 74 75 72 6e 20 70 4f 75 74 3b 0a 7d 0a 0a 2f  eturn pOut;.}../
18520 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
18521 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74  on is called aft
18522 65 72 20 69 6e 76 6f 6b 69 6e 67 20 61 6e 20 73  er invoking an s
18523 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 58 58 58  qlite3_value_XXX
18524 20 66 75 6e 63 74 69 6f 6e 20 6f 6e 20 61 20 0a   function on a .
18525 2a 2a 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65 20  ** column value 
18526 28 69 2e 65 2e 20 61 20 76 61 6c 75 65 20 72 65  (i.e. a value re
18527 74 75 72 6e 65 64 20 62 79 20 65 76 61 6c 75 61  turned by evalua
18528 74 69 6e 67 20 61 6e 20 53 51 4c 20 65 78 70 72  ting an SQL expr
18529 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 0a 2a 2a  ession in the.**
1852a 20 73 65 6c 65 63 74 20 6c 69 73 74 20 6f 66 20   select list of 
1852b 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
1852c 6e 74 29 20 74 68 61 74 20 6d 61 79 20 63 61 75  nt) that may cau
1852d 73 65 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  se a malloc() fa
1852e 69 6c 75 72 65 2e 20 49 66 20 0a 2a 2a 20 6d 61  ilure. If .** ma
1852f 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65  lloc() has faile
18530 64 2c 20 74 68 65 20 74 68 72 65 61 64 73 20 6d  d, the threads m
18531 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67  allocFailed flag
18532 20 69 73 20 63 6c 65 61 72 65 64 20 61 6e 64 20   is cleared and 
18533 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 63 6f  the result.** co
18534 64 65 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 20  de of statement 
18535 70 53 74 6d 74 20 73 65 74 20 74 6f 20 53 51 4c  pStmt set to SQL
18536 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2a 0a 2a 2a  ITE_NOMEM..**.**
18537 20 53 70 65 63 69 66 69 63 61 6c 6c 79 2c 20 74   Specifically, t
18538 68 69 73 20 69 73 20 63 61 6c 6c 65 64 20 66 72  his is called fr
18539 6f 6d 20 77 69 74 68 69 6e 3a 0a 2a 2a 0a 2a 2a  om within:.**.**
1853a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c       sqlite3_col
1853b 75 6d 6e 5f 69 6e 74 28 29 0a 2a 2a 20 20 20 20  umn_int().**    
1853c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
1853d 69 6e 74 36 34 28 29 0a 2a 2a 20 20 20 20 20 73  int64().**     s
1853e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
1853f 78 74 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69  xt().**     sqli
18540 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
18541 36 28 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74  6().**     sqlit
18542 65 33 5f 63 6f 6c 75 6d 6e 5f 72 65 61 6c 28 29  e3_column_real()
18543 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f  .**     sqlite3_
18544 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 29 0a 2a  column_bytes().*
18545 2a 20 20 20 20 20 73 71 6c 69 74 65 33 5f 63 6f  *     sqlite3_co
18546 6c 75 6d 6e 5f 62 79 74 65 73 31 36 28 29 0a 2a  lumn_bytes16().*
18547 2a 0a 2a 2a 20 42 75 74 20 6e 6f 74 20 66 6f 72  *.** But not for
18548 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f   sqlite3_column_
18549 62 6c 6f 62 28 29 2c 20 77 68 69 63 68 20 6e 65  blob(), which ne
1854a 76 65 72 20 63 61 6c 6c 73 20 6d 61 6c 6c 6f 63  ver calls malloc
1854b 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
1854c 69 64 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46  id columnMallocF
1854d 61 69 6c 75 72 65 28 73 71 6c 69 74 65 33 5f 73  ailure(sqlite3_s
1854e 74 6d 74 20 2a 70 53 74 6d 74 29 0a 7b 0a 20 20  tmt *pStmt).{.  
1854f 2f 2a 20 49 66 20 6d 61 6c 6c 6f 63 28 29 20 66  /* If malloc() f
18550 61 69 6c 65 64 20 64 75 72 69 6e 67 20 61 6e 20  ailed during an 
18551 65 6e 63 6f 64 69 6e 67 20 63 6f 6e 76 65 72 73  encoding convers
18552 69 6f 6e 20 77 69 74 68 69 6e 20 61 6e 0a 20 20  ion within an.  
18553 2a 2a 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d  ** sqlite3_colum
18554 6e 5f 58 58 58 20 41 50 49 2c 20 74 68 65 6e 20  n_XXX API, then 
18555 73 65 74 20 74 68 65 20 72 65 74 75 72 6e 20 63  set the return c
18556 6f 64 65 20 6f 66 20 74 68 65 20 73 74 61 74 65  ode of the state
18557 6d 65 6e 74 20 74 6f 0a 20 20 2a 2a 20 53 51 4c  ment to.  ** SQL
18558 49 54 45 5f 4e 4f 4d 45 4d 2e 20 54 68 65 20 6e  ITE_NOMEM. The n
18559 65 78 74 20 63 61 6c 6c 20 74 6f 20 5f 73 74 65  ext call to _ste
1855a 70 28 29 20 28 69 66 20 61 6e 79 29 20 77 69 6c  p() (if any) wil
1855b 6c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  l return SQLITE_
1855c 45 52 52 4f 52 0a 20 20 2a 2a 20 61 6e 64 20 5f  ERROR.  ** and _
1855d 66 69 6e 61 6c 69 7a 65 28 29 20 77 69 6c 6c 20  finalize() will 
1855e 72 65 74 75 72 6e 20 4e 4f 4d 45 4d 2e 0a 20 20  return NOMEM..  
1855f 2a 2f 0a 20 20 56 64 62 65 20 2a 70 20 3d 20 28  */.  Vdbe *p = (
18560 56 64 62 65 20 2a 29 70 53 74 6d 74 3b 0a 20 20  Vdbe *)pStmt;.  
18561 69 66 28 20 70 20 29 7b 0a 20 20 20 20 70 2d 3e  if( p ){.    p->
18562 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
18563 78 69 74 28 70 2d 3e 64 62 2c 20 70 2d 3e 72 63  xit(p->db, p->rc
18564 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
18565 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64 62  utex_leave(p->db
18566 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d 0a 7d 0a  ->mutex);.  }.}.
18567 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
18568 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 73  ************** s
18569 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 20 20  qlite3_column_  
1856a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1856b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1856c 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
1856d 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73   routines are us
1856e 65 64 20 74 6f 20 61 63 63 65 73 73 20 65 6c 65  ed to access ele
1856f 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 63 75 72  ments of the cur
18570 72 65 6e 74 20 72 6f 77 0a 2a 2a 20 69 6e 20 74  rent row.** in t
18571 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 0a 2a  he result set..*
18572 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
18573 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33  st void *sqlite3
18574 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 71 6c  _column_blob(sql
18575 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
18576 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63 6f 6e 73  , int i){.  cons
18577 74 20 76 6f 69 64 20 2a 76 61 6c 3b 0a 20 20 76  t void *val;.  v
18578 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  al = sqlite3_val
18579 75 65 5f 62 6c 6f 62 28 20 63 6f 6c 75 6d 6e 4d  ue_blob( columnM
1857a 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20  em(pStmt,i) );. 
1857b 20 2f 2a 20 45 76 65 6e 20 74 68 6f 75 67 68 20   /* Even though 
1857c 74 68 65 72 65 20 69 73 20 6e 6f 20 65 6e 63 6f  there is no enco
1857d 64 69 6e 67 20 63 6f 6e 76 65 72 73 69 6f 6e 2c  ding conversion,
1857e 20 76 61 6c 75 65 5f 62 6c 6f 62 28 29 20 6d 69   value_blob() mi
1857f 67 68 74 0a 20 20 2a 2a 20 6e 65 65 64 20 74 6f  ght.  ** need to
18580 20 63 61 6c 6c 20 6d 61 6c 6c 6f 63 28 29 20 74   call malloc() t
18581 6f 20 65 78 70 61 6e 64 20 74 68 65 20 72 65 73  o expand the res
18582 75 6c 74 20 6f 66 20 61 20 7a 65 72 6f 62 6c 6f  ult of a zeroblo
18583 62 28 29 20 0a 20 20 2a 2a 20 65 78 70 72 65 73  b() .  ** expres
18584 73 69 6f 6e 2e 20 0a 20 20 2a 2f 0a 20 20 63 6f  sion. .  */.  co
18585 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
18586 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
18587 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
18588 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
18589 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 71  _column_bytes(sq
1858a 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
1858b 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74  t, int i){.  int
1858c 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76   val = sqlite3_v
1858d 61 6c 75 65 5f 62 79 74 65 73 28 20 63 6f 6c 75  alue_bytes( colu
1858e 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
1858f 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
18590 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
18591 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a 7d 0a    return val;.}.
18592 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
18593 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
18594 74 65 73 31 36 28 73 71 6c 69 74 65 33 5f 73 74  tes16(sqlite3_st
18595 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
18596 29 7b 0a 20 20 69 6e 74 20 76 61 6c 20 3d 20 73  ){.  int val = s
18597 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
18598 65 73 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  es16( columnMem(
18599 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
1859a 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
1859b 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
1859c 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
1859d 5f 41 50 49 20 64 6f 75 62 6c 65 20 73 71 6c 69  _API double sqli
1859e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c  te3_column_doubl
1859f 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e(sqlite3_stmt *
185a0 70 53 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20  pStmt, int i){. 
185a1 20 64 6f 75 62 6c 65 20 76 61 6c 20 3d 20 73 71   double val = sq
185a2 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
185a3 6c 65 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  le( columnMem(pS
185a4 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
185a5 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
185a6 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
185a7 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41   val;.}.SQLITE_A
185a8 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
185a9 6f 6c 75 6d 6e 5f 69 6e 74 28 73 71 6c 69 74 65  olumn_int(sqlite
185aa 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
185ab 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20 76 61 6c  nt i){.  int val
185ac 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
185ad 5f 69 6e 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28  _int( columnMem(
185ae 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f  pStmt,i) );.  co
185af 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72  lumnMallocFailur
185b0 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75  e(pStmt);.  retu
185b1 72 6e 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45  rn val;.}.SQLITE
185b2 5f 41 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36  _API sqlite_int6
185b3 34 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  4 sqlite3_column
185b4 5f 69 6e 74 36 34 28 73 71 6c 69 74 65 33 5f 73  _int64(sqlite3_s
185b5 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
185b6 69 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  i){.  sqlite_int
185b7 36 34 20 76 61 6c 20 3d 20 73 71 6c 69 74 65 33  64 val = sqlite3
185b8 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 20 63 6f  _value_int64( co
185b9 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29  lumnMem(pStmt,i)
185ba 20 29 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c   );.  columnMall
185bb 6f 63 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29  ocFailure(pStmt)
185bc 3b 0a 20 20 72 65 74 75 72 6e 20 76 61 6c 3b 0a  ;.  return val;.
185bd 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  }.SQLITE_API con
185be 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
185bf 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
185c0 5f 74 65 78 74 28 73 71 6c 69 74 65 33 5f 73 74  _text(sqlite3_st
185c1 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
185c2 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  ){.  const unsig
185c3 6e 65 64 20 63 68 61 72 20 2a 76 61 6c 20 3d 20  ned char *val = 
185c4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
185c5 78 74 28 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53  xt( columnMem(pS
185c6 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63 6f 6c 75  tmt,i) );.  colu
185c7 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28  mnMallocFailure(
185c8 70 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e  pStmt);.  return
185c9 20 76 61 6c 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41   val;.}.SQLITE_A
185ca 50 49 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  PI sqlite3_value
185cb 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
185cc 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f 73  _value(sqlite3_s
185cd 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
185ce 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 4f 75 74 20  i){.  Mem *pOut 
185cf 3d 20 63 6f 6c 75 6d 6e 4d 65 6d 28 70 53 74 6d  = columnMem(pStm
185d0 74 2c 20 69 29 3b 0a 20 20 69 66 28 20 70 4f 75  t, i);.  if( pOu
185d1 74 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74 61  t->flags&MEM_Sta
185d2 74 69 63 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d  tic ){.    pOut-
185d3 3e 66 6c 61 67 73 20 26 3d 20 7e 4d 45 4d 5f 53  >flags &= ~MEM_S
185d4 74 61 74 69 63 3b 0a 20 20 20 20 70 4f 75 74 2d  tatic;.    pOut-
185d5 3e 66 6c 61 67 73 20 7c 3d 20 4d 45 4d 5f 45 70  >flags |= MEM_Ep
185d6 68 65 6d 3b 0a 20 20 7d 0a 20 20 63 6f 6c 75 6d  hem;.  }.  colum
185d7 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75 72 65 28 70  nMallocFailure(p
185d8 53 74 6d 74 29 3b 0a 20 20 72 65 74 75 72 6e 20  Stmt);.  return 
185d9 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  (sqlite3_value *
185da 29 70 4f 75 74 3b 0a 7d 0a 23 69 66 6e 64 65 66  )pOut;.}.#ifndef
185db 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
185dc 31 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f  16.SQLITE_API co
185dd 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  nst void *sqlite
185de 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31 36 28  3_column_text16(
185df 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
185e0 74 6d 74 2c 20 69 6e 74 20 69 29 7b 0a 20 20 63  tmt, int i){.  c
185e1 6f 6e 73 74 20 76 6f 69 64 20 2a 76 61 6c 20 3d  onst void *val =
185e2 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
185e3 65 78 74 31 36 28 20 63 6f 6c 75 6d 6e 4d 65 6d  ext16( columnMem
185e4 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a 20 20 63  (pStmt,i) );.  c
185e5 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63 46 61 69 6c 75  olumnMallocFailu
185e6 72 65 28 70 53 74 6d 74 29 3b 0a 20 20 72 65 74  re(pStmt);.  ret
185e7 75 72 6e 20 76 61 6c 3b 0a 7d 0a 23 65 6e 64 69  urn val;.}.#endi
185e8 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
185e9 5f 55 54 46 31 36 20 2a 2f 0a 53 51 4c 49 54 45  _UTF16 */.SQLITE
185ea 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
185eb 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 71 6c  _column_type(sql
185ec 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74  ite3_stmt *pStmt
185ed 2c 20 69 6e 74 20 69 29 7b 0a 20 20 69 6e 74 20  , int i){.  int 
185ee 69 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 5f  iType = sqlite3_
185ef 76 61 6c 75 65 5f 74 79 70 65 28 20 63 6f 6c 75  value_type( colu
185f0 6d 6e 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29  mnMem(pStmt,i) )
185f1 3b 0a 20 20 63 6f 6c 75 6d 6e 4d 61 6c 6c 6f 63  ;.  columnMalloc
185f2 46 61 69 6c 75 72 65 28 70 53 74 6d 74 29 3b 0a  Failure(pStmt);.
185f3 20 20 72 65 74 75 72 6e 20 69 54 79 70 65 3b 0a    return iType;.
185f4 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  }../* The follow
185f5 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 69 73 20  ing function is 
185f6 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 61 6e 64  experimental and
185f7 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68 61 6e   subject to chan
185f8 67 65 20 6f 72 0a 2a 2a 20 72 65 6d 6f 76 61 6c  ge or.** removal
185f9 20 2a 2f 0a 2f 2a 69 6e 74 20 73 71 6c 69 74 65   */./*int sqlite
185fa 33 5f 63 6f 6c 75 6d 6e 5f 6e 75 6d 65 72 69 63  3_column_numeric
185fb 5f 74 79 70 65 28 73 71 6c 69 74 65 33 5f 73 74  _type(sqlite3_st
185fc 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
185fd 29 7b 0a 2a 2a 20 20 72 65 74 75 72 6e 20 73 71  ){.**  return sq
185fe 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
185ff 72 69 63 5f 74 79 70 65 28 20 63 6f 6c 75 6d 6e  ric_type( column
18600 4d 65 6d 28 70 53 74 6d 74 2c 69 29 20 29 3b 0a  Mem(pStmt,i) );.
18601 2a 2a 7d 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f  **}.*/../*.** Co
18602 6e 76 65 72 74 20 74 68 65 20 4e 2d 74 68 20 65  nvert the N-th e
18603 6c 65 6d 65 6e 74 20 6f 66 20 70 53 74 6d 74 2d  lement of pStmt-
18604 3e 70 43 6f 6c 4e 61 6d 65 5b 5d 20 69 6e 74 6f  >pColName[] into
18605 20 61 20 73 74 72 69 6e 67 20 75 73 69 6e 67 0a   a string using.
18606 2a 2a 20 78 46 75 6e 63 28 29 20 74 68 65 6e 20  ** xFunc() then 
18607 72 65 74 75 72 6e 20 74 68 61 74 20 73 74 72 69  return that stri
18608 6e 67 2e 20 20 49 66 20 4e 20 69 73 20 6f 75 74  ng.  If N is out
18609 20 6f 66 20 72 61 6e 67 65 2c 20 72 65 74 75 72   of range, retur
1860a 6e 20 30 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  n 0..**.** There
1860b 20 61 72 65 20 75 70 20 74 6f 20 35 20 6e 61 6d   are up to 5 nam
1860c 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75  es for each colu
1860d 6d 6e 2e 20 20 75 73 65 54 79 70 65 20 64 65 74  mn.  useType det
1860e 65 72 6d 69 6e 65 73 20 77 68 69 63 68 0a 2a 2a  ermines which.**
1860f 20 6e 61 6d 65 20 69 73 20 72 65 74 75 72 6e 65   name is returne
18610 64 2e 20 20 48 65 72 65 20 61 72 65 20 74 68 65  d.  Here are the
18611 20 6e 61 6d 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20   names:.**.**   
18612 20 30 20 20 20 20 20 20 54 68 65 20 63 6f 6c 75   0      The colu
18613 6d 6e 20 6e 61 6d 65 20 61 73 20 69 74 20 73 68  mn name as it sh
18614 6f 75 6c 64 20 62 65 20 64 69 73 70 6c 61 79 65  ould be displaye
18615 64 20 66 6f 72 20 6f 75 74 70 75 74 0a 2a 2a 20  d for output.** 
18616 20 20 20 31 20 20 20 20 20 20 54 68 65 20 64 61     1      The da
18617 74 61 74 79 70 65 20 6e 61 6d 65 20 66 6f 72 20  tatype name for 
18618 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 20 20  the column.**   
18619 20 32 20 20 20 20 20 20 54 68 65 20 6e 61 6d 65   2      The name
1861a 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1861b 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e   that the column
1861c 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a   derives from.**
1861d 20 20 20 20 33 20 20 20 20 20 20 54 68 65 20 6e      3      The n
1861e 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1861f 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75 6d 6e   that the column
18620 20 64 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a   derives from.**
18621 20 20 20 20 34 20 20 20 20 20 20 54 68 65 20 6e      4      The n
18622 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
18623 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74 68 65   column that the
18624 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 64   result column d
18625 65 72 69 76 65 73 20 66 72 6f 6d 0a 2a 2a 0a 2a  erives from.**.*
18626 2a 20 49 66 20 74 68 65 20 72 65 73 75 6c 74 20  * If the result 
18627 69 73 20 6e 6f 74 20 61 20 73 69 6d 70 6c 65 20  is not a simple 
18628 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
18629 20 28 69 66 20 69 74 20 69 73 20 61 6e 20 65 78   (if it is an ex
1862a 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 6f 72 20 61  pression.** or a
1862b 20 63 6f 6e 73 74 61 6e 74 29 20 74 68 65 6e 20   constant) then 
1862c 75 73 65 54 79 70 65 73 20 32 2c 20 33 2c 20 61  useTypes 2, 3, a
1862d 6e 64 20 34 20 72 65 74 75 72 6e 20 4e 55 4c 4c  nd 4 return NULL
1862e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
1862f 74 20 76 6f 69 64 20 2a 63 6f 6c 75 6d 6e 4e 61  t void *columnNa
18630 6d 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  me(.  sqlite3_st
18631 6d 74 20 2a 70 53 74 6d 74 2c 0a 20 20 69 6e 74  mt *pStmt,.  int
18632 20 4e 2c 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   N,.  const void
18633 20 2a 28 2a 78 46 75 6e 63 29 28 4d 65 6d 2a 29   *(*xFunc)(Mem*)
18634 2c 0a 20 20 69 6e 74 20 75 73 65 54 79 70 65 0a  ,.  int useType.
18635 29 7b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  ){.  const void 
18636 2a 72 65 74 20 3d 20 30 3b 0a 20 20 56 64 62 65  *ret = 0;.  Vdbe
18637 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
18638 74 6d 74 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20  tmt;.  int n;.  
18639 0a 0a 20 20 69 66 28 20 70 21 3d 30 20 29 7b 0a  ..  if( p!=0 ){.
1863a 20 20 20 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f      n = sqlite3_
1863b 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74  column_count(pSt
1863c 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 4e 3c 6e  mt);.    if( N<n
1863d 20 26 26 20 4e 3e 3d 30 20 29 7b 0a 20 20 20 20   && N>=0 ){.    
1863e 20 20 4e 20 2b 3d 20 75 73 65 54 79 70 65 2a 6e    N += useType*n
1863f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
18640 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
18641 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 20  b->mutex);.     
18642 20 72 65 74 20 3d 20 78 46 75 6e 63 28 26 70 2d   ret = xFunc(&p-
18643 3e 61 43 6f 6c 4e 61 6d 65 5b 4e 5d 29 3b 0a 0a  >aColName[N]);..
18644 20 20 20 20 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f        /* A mallo
18645 63 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65  c may have faile
18646 64 20 69 6e 73 69 64 65 20 6f 66 20 74 68 65 20  d inside of the 
18647 78 46 75 6e 63 28 29 20 63 61 6c 6c 2e 20 49 66  xFunc() call. If
18648 20 74 68 69 73 0a 20 20 20 20 20 20 2a 2a 20 69   this.      ** i
18649 73 20 74 68 65 20 63 61 73 65 2c 20 63 6c 65 61  s the case, clea
1864a 72 20 74 68 65 20 6d 61 6c 6c 6f 63 46 61 69 6c  r the mallocFail
1864b 65 64 20 66 6c 61 67 20 61 6e 64 20 72 65 74 75  ed flag and retu
1864c 72 6e 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a  rn NULL..      *
1864d 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 64  /.      if( p->d
1864e 62 20 26 26 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c  b && p->db->mall
1864f 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
18650 20 20 20 20 70 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f      p->db->mallo
18651 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20 20 20  cFailed = 0;.   
18652 20 20 20 20 20 72 65 74 20 3d 20 30 3b 0a 20 20       ret = 0;.  
18653 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
18654 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
18655 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  p->db->mutex);. 
18656 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
18657 6e 20 72 65 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n ret;.}../*.** 
18658 52 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20  Return the name 
18659 6f 66 20 74 68 65 20 4e 74 68 20 63 6f 6c 75 6d  of the Nth colum
1865a 6e 20 6f 66 20 74 68 65 20 72 65 73 75 6c 74 20  n of the result 
1865b 73 65 74 20 72 65 74 75 72 6e 65 64 20 62 79 20  set returned by 
1865c 53 51 4c 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  SQL.** statement
1865d 20 70 53 74 6d 74 2e 0a 2a 2f 0a 53 51 4c 49 54   pStmt..*/.SQLIT
1865e 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
1865f 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
18660 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
18661 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
18662 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
18663 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
18664 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
18665 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
18666 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
18667 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 29 3b  , COLNAME_NAME);
18668 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
18669 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
1866a 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f  ITE_API const vo
1866b 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75  id *sqlite3_colu
1866c 6d 6e 5f 6e 61 6d 65 31 36 28 73 71 6c 69 74 65  mn_name16(sqlite
1866d 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69  3_stmt *pStmt, i
1866e 6e 74 20 4e 29 7b 0a 20 20 72 65 74 75 72 6e 20  nt N){.  return 
1866f 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a 20 20 20 20  columnName(.    
18670 20 20 70 53 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e    pStmt, N, (con
18671 73 74 20 76 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a  st void*(*)(Mem*
18672 29 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ))sqlite3_value_
18673 74 65 78 74 31 36 2c 20 43 4f 4c 4e 41 4d 45 5f  text16, COLNAME_
18674 4e 41 4d 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a  NAME);.}.#endif.
18675 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 61 69 6e  ./*.** Constrain
18676 74 3a 20 20 49 66 20 79 6f 75 20 68 61 76 65 20  t:  If you have 
18677 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45  ENABLE_COLUMN_ME
18678 54 41 44 41 54 41 20 74 68 65 6e 20 79 6f 75 20  TADATA then you 
18679 6d 75 73 74 0a 2a 2a 20 6e 6f 74 20 64 65 66 69  must.** not defi
1867a 6e 65 20 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45  ne OMIT_DECLTYPE
1867b 2e 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64  ..*/.#if defined
1867c 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43  (SQLITE_OMIT_DEC
1867d 4c 54 59 50 45 29 20 26 26 20 64 65 66 69 6e 65  LTYPE) && define
1867e 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f  d(SQLITE_ENABLE_
1867f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 29  COLUMN_METADATA)
18680 0a 23 20 65 72 72 6f 72 20 22 4d 75 73 74 20 6e  .# error "Must n
18681 6f 74 20 64 65 66 69 6e 65 20 62 6f 74 68 20 53  ot define both S
18682 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c 54  QLITE_OMIT_DECLT
18683 59 50 45 20 5c 0a 20 20 20 20 20 20 20 20 20 61  YPE \.         a
18684 6e 64 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  nd SQLITE_ENABLE
18685 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41  _COLUMN_METADATA
18686 22 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65  ".#endif..#ifnde
18687 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45  f SQLITE_OMIT_DE
18688 43 4c 54 59 50 45 0a 2f 2a 0a 2a 2a 20 52 65 74  CLTYPE./*.** Ret
18689 75 72 6e 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64  urn the column d
1868a 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
1868b 28 69 66 20 61 70 70 6c 69 63 61 62 6c 65 29 20  (if applicable) 
1868c 6f 66 20 74 68 65 20 27 69 27 74 68 20 63 6f 6c  of the 'i'th col
1868d 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 72 65  umn.** of the re
1868e 73 75 6c 74 20 73 65 74 20 6f 66 20 53 51 4c 20  sult set of SQL 
1868f 73 74 61 74 65 6d 65 6e 74 20 70 53 74 6d 74 2e  statement pStmt.
18690 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
18691 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
18692 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79  e3_column_declty
18693 70 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  pe(sqlite3_stmt 
18694 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
18695 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
18696 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
18697 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
18698 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
18699 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43  e3_value_text, C
1869a 4f 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29  OLNAME_DECLTYPE)
1869b 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  ;.}.#ifndef SQLI
1869c 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51  TE_OMIT_UTF16.SQ
1869d 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 76  LITE_API const v
1869e 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c  oid *sqlite3_col
1869f 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 28 73  umn_decltype16(s
186a0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
186a1 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
186a2 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
186a3 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
186a4 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
186a5 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
186a6 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c  alue_text16, COL
186a7 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 29 3b 0a  NAME_DECLTYPE);.
186a8 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
186a9 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
186aa 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
186ab 45 5f 4f 4d 49 54 5f 44 45 43 4c 54 59 50 45 20  E_OMIT_DECLTYPE 
186ac 2a 2f 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  */..#ifdef SQLIT
186ad 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f  E_ENABLE_COLUMN_
186ae 4d 45 54 41 44 41 54 41 0a 2f 2a 0a 2a 2a 20 52  METADATA./*.** R
186af 65 74 75 72 6e 20 74 68 65 20 6e 61 6d 65 20 6f  eturn the name o
186b0 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  f the database f
186b1 72 6f 6d 20 77 68 69 63 68 20 61 20 72 65 73 75  rom which a resu
186b2 6c 74 20 63 6f 6c 75 6d 6e 20 64 65 72 69 76 65  lt column derive
186b3 73 2e 0a 2a 2a 20 4e 55 4c 4c 20 69 73 20 72 65  s..** NULL is re
186b4 74 75 72 6e 65 64 20 69 66 20 74 68 65 20 72 65  turned if the re
186b5 73 75 6c 74 20 63 6f 6c 75 6d 6e 20 69 73 20 61  sult column is a
186b6 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6f 72 20  n expression or 
186b7 63 6f 6e 73 74 61 6e 74 20 6f 72 0a 2a 2a 20 61  constant or.** a
186b8 6e 79 74 68 69 6e 67 20 65 6c 73 65 20 77 68 69  nything else whi
186b9 63 68 20 69 73 20 6e 6f 74 20 61 6e 20 75 6e 61  ch is not an una
186ba 62 69 67 75 6f 75 73 20 72 65 66 65 72 65 6e 63  biguous referenc
186bb 65 20 74 6f 20 61 20 64 61 74 61 62 61 73 65 20  e to a database 
186bc 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  column..*/.SQLIT
186bd 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68 61 72  E_API const char
186be 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
186bf 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 28 73  _database_name(s
186c0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
186c1 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65  mt, int N){.  re
186c2 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28  turn columnName(
186c3 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c  .      pStmt, N,
186c4 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29   (const void*(*)
186c5 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76  (Mem*))sqlite3_v
186c6 61 6c 75 65 5f 74 65 78 74 2c 20 43 4f 4c 4e 41  alue_text, COLNA
186c7 4d 45 5f 44 41 54 41 42 41 53 45 29 3b 0a 7d 0a  ME_DATABASE);.}.
186c8 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
186c9 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54 45  MIT_UTF16.SQLITE
186ca 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64 20  _API const void 
186cb 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  *sqlite3_column_
186cc 64 61 74 61 62 61 73 65 5f 6e 61 6d 65 31 36 28  database_name16(
186cd 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53  sqlite3_stmt *pS
186ce 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72  tmt, int N){.  r
186cf 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65  eturn columnName
186d0 28 0a 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e  (.      pStmt, N
186d1 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a  , (const void*(*
186d2 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f  )(Mem*))sqlite3_
186d3 76 61 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f  value_text16, CO
186d4 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45 29 3b  LNAME_DATABASE);
186d5 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
186d6 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a  ITE_OMIT_UTF16 *
186d7 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  /../*.** Return 
186d8 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
186d9 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69 63 68  table from which
186da 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e   a result column
186db 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e 55 4c   derives..** NUL
186dc 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20 69 66  L is returned if
186dd 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f 6c 75   the result colu
186de 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65 73 73  mn is an express
186df 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ion or constant 
186e0 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67 20 65  or.** anything e
186e1 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e 6f 74  lse which is not
186e2 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73 20 72   an unabiguous r
186e3 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20 64 61  eference to a da
186e4 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e 0a 2a  tabase column..*
186e5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e  /.SQLITE_API con
186e6 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33  st char *sqlite3
186e7 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61  _column_table_na
186e8 6d 65 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  me(sqlite3_stmt 
186e9 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
186ea 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
186eb 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
186ec 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
186ed 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
186ee 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 2c 20 43  e3_value_text, C
186ef 4f 4c 4e 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d  OLNAME_TABLE);.}
186f0 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
186f1 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c 49 54  OMIT_UTF16.SQLIT
186f2 45 5f 41 50 49 20 63 6f 6e 73 74 20 76 6f 69 64  E_API const void
186f3 20 2a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e   *sqlite3_column
186f4 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 28 73 71  _table_name16(sq
186f5 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
186f6 74 2c 20 69 6e 74 20 4e 29 7b 0a 20 20 72 65 74  t, int N){.  ret
186f7 75 72 6e 20 63 6f 6c 75 6d 6e 4e 61 6d 65 28 0a  urn columnName(.
186f8 20 20 20 20 20 20 70 53 74 6d 74 2c 20 4e 2c 20        pStmt, N, 
186f9 28 63 6f 6e 73 74 20 76 6f 69 64 2a 28 2a 29 28  (const void*(*)(
186fa 4d 65 6d 2a 29 29 73 71 6c 69 74 65 33 5f 76 61  Mem*))sqlite3_va
186fb 6c 75 65 5f 74 65 78 74 31 36 2c 20 43 4f 4c 4e  lue_text16, COLN
186fc 41 4d 45 5f 54 41 42 4c 45 29 3b 0a 7d 0a 23 65  AME_TABLE);.}.#e
186fd 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
186fe 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
186ff 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e  .** Return the n
18700 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
18701 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 77 68 69   column from whi
18702 63 68 20 61 20 72 65 73 75 6c 74 20 63 6f 6c 75  ch a result colu
18703 6d 6e 20 64 65 72 69 76 65 73 2e 0a 2a 2a 20 4e  mn derives..** N
18704 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64 20  ULL is returned 
18705 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 63 6f  if the result co
18706 6c 75 6d 6e 20 69 73 20 61 6e 20 65 78 70 72 65  lumn is an expre
18707 73 73 69 6f 6e 20 6f 72 20 63 6f 6e 73 74 61 6e  ssion or constan
18708 74 20 6f 72 0a 2a 2a 20 61 6e 79 74 68 69 6e 67  t or.** anything
18709 20 65 6c 73 65 20 77 68 69 63 68 20 69 73 20 6e   else which is n
1870a 6f 74 20 61 6e 20 75 6e 61 62 69 67 75 6f 75 73  ot an unabiguous
1870b 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 20   reference to a 
1870c 64 61 74 61 62 61 73 65 20 63 6f 6c 75 6d 6e 2e  database column.
1870d 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
1870e 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
1870f 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
18710 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73 74  _name(sqlite3_st
18711 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e  mt *pStmt, int N
18712 29 7b 0a 20 20 72 65 74 75 72 6e 20 63 6f 6c 75  ){.  return colu
18713 6d 6e 4e 61 6d 65 28 0a 20 20 20 20 20 20 70 53  mnName(.      pS
18714 74 6d 74 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76  tmt, N, (const v
18715 6f 69 64 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71  oid*(*)(Mem*))sq
18716 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
18717 2c 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e  , COLNAME_COLUMN
18718 29 3b 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  );.}.#ifndef SQL
18719 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53  ITE_OMIT_UTF16.S
1871a 51 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20  QLITE_API const 
1871b 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 63 6f  void *sqlite3_co
1871c 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
1871d 31 36 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  16(sqlite3_stmt 
1871e 2a 70 53 74 6d 74 2c 20 69 6e 74 20 4e 29 7b 0a  *pStmt, int N){.
1871f 20 20 72 65 74 75 72 6e 20 63 6f 6c 75 6d 6e 4e    return columnN
18720 61 6d 65 28 0a 20 20 20 20 20 20 70 53 74 6d 74  ame(.      pStmt
18721 2c 20 4e 2c 20 28 63 6f 6e 73 74 20 76 6f 69 64  , N, (const void
18722 2a 28 2a 29 28 4d 65 6d 2a 29 29 73 71 6c 69 74  *(*)(Mem*))sqlit
18723 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 2c  e3_value_text16,
18724 20 43 4f 4c 4e 41 4d 45 5f 43 4f 4c 55 4d 4e 29   COLNAME_COLUMN)
18725 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
18726 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20  LITE_OMIT_UTF16 
18727 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  */.#endif /* SQL
18728 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f 4c 55 4d  ITE_ENABLE_COLUM
18729 4e 5f 4d 45 54 41 44 41 54 41 20 2a 2f 0a 0a 0a  N_METADATA */...
1872a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
1872b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1872c 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 20 20   sqlite3_bind_  
1872d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1872e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 0a  ***********.** .
1872f 2a 2a 20 52 6f 75 74 69 6e 65 73 20 75 73 65 64  ** Routines used
18730 20 74 6f 20 61 74 74 61 63 68 20 76 61 6c 75 65   to attach value
18731 73 20 74 6f 20 77 69 6c 64 63 61 72 64 73 20 69  s to wildcards i
18732 6e 20 61 20 63 6f 6d 70 69 6c 65 64 20 53 51 4c  n a compiled SQL
18733 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 2f   statement..*/./
18734 2a 0a 2a 2a 20 55 6e 62 69 6e 64 20 74 68 65 20  *.** Unbind the 
18735 76 61 6c 75 65 20 62 6f 75 6e 64 20 74 6f 20 76  value bound to v
18736 61 72 69 61 62 6c 65 20 69 20 69 6e 20 76 69 72  ariable i in vir
18737 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 70 2e 20  tual machine p. 
18738 54 68 69 73 20 69 73 20 74 68 65 20 0a 2a 2a 20  This is the .** 
18739 74 68 65 20 73 61 6d 65 20 61 73 20 62 69 6e 64  the same as bind
1873a 69 6e 67 20 61 20 4e 55 4c 4c 20 76 61 6c 75 65  ing a NULL value
1873b 20 74 6f 20 74 68 65 20 63 6f 6c 75 6d 6e 2e 20   to the column. 
1873c 49 66 20 74 68 65 20 22 69 22 20 70 61 72 61 6d  If the "i" param
1873d 65 74 65 72 20 69 73 0a 2a 2a 20 6f 75 74 20 6f  eter is.** out o
1873e 66 20 72 61 6e 67 65 2c 20 74 68 65 6e 20 53 51  f range, then SQ
1873f 4c 49 54 45 5f 52 41 4e 47 45 20 69 73 20 72 65  LITE_RANGE is re
18740 74 75 72 6e 65 64 2e 20 4f 74 68 65 77 69 73 65  turned. Othewise
18741 20 53 51 4c 49 54 45 5f 4f 4b 2e 0a 2a 2a 0a 2a   SQLITE_OK..**.*
18742 2a 20 41 20 73 75 63 63 65 73 73 66 75 6c 20 65  * A successful e
18743 76 61 6c 75 61 74 69 6f 6e 20 6f 66 20 74 68 69  valuation of thi
18744 73 20 72 6f 75 74 69 6e 65 20 61 63 71 75 69 72  s routine acquir
18745 65 73 20 74 68 65 20 6d 75 74 65 78 20 6f 6e 20  es the mutex on 
18746 70 2e 0a 2a 2a 20 74 68 65 20 6d 75 74 65 78 20  p..** the mutex 
18747 69 73 20 72 65 6c 65 61 73 65 64 20 69 66 20 61  is released if a
18748 6e 79 20 6b 69 6e 64 20 6f 66 20 65 72 72 6f 72  ny kind of error
18749 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a 20 54   occurs..**.** T
1874a 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 73 74  he error code st
1874b 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61 73 65  ored in database
1874c 20 70 2d 3e 64 62 20 69 73 20 6f 76 65 72 77 72   p->db is overwr
1874d 69 74 74 65 6e 20 77 69 74 68 20 74 68 65 20 72  itten with the r
1874e 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75 65 20 69  eturn.** value i
1874f 6e 20 61 6e 79 20 63 61 73 65 2e 0a 2a 2f 0a 73  n any case..*/.s
18750 74 61 74 69 63 20 69 6e 74 20 76 64 62 65 55 6e  tatic int vdbeUn
18751 62 69 6e 64 28 56 64 62 65 20 2a 70 2c 20 69 6e  bind(Vdbe *p, in
18752 74 20 69 29 7b 0a 20 20 4d 65 6d 20 2a 70 56 61  t i){.  Mem *pVa
18753 72 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20  r;.  if( p==0 ) 
18754 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
18755 53 55 53 45 3b 0a 20 20 73 71 6c 69 74 65 33 5f  SUSE;.  sqlite3_
18756 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 2d 3e 64  mutex_enter(p->d
18757 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28  b->mutex);.  if(
18758 20 70 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f   p->magic!=VDBE_
18759 4d 41 47 49 43 5f 52 55 4e 20 7c 7c 20 70 2d 3e  MAGIC_RUN || p->
1875a 70 63 3e 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  pc>=0 ){.    sql
1875b 69 74 65 33 45 72 72 6f 72 28 70 2d 3e 64 62 2c  ite3Error(p->db,
1875c 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 2c 20   SQLITE_MISUSE, 
1875d 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  0);.    sqlite3_
1875e 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d 3e 64  mutex_leave(p->d
1875f 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 20 20 72  b->mutex);.    r
18760 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
18761 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20 69  USE;.  }.  if( i
18762 3c 31 20 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20  <1 || i>p->nVar 
18763 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
18764 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c 49 54  ror(p->db, SQLIT
18765 45 5f 52 41 4e 47 45 2c 20 30 29 3b 0a 20 20 20  E_RANGE, 0);.   
18766 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
18767 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65  eave(p->db->mute
18768 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
18769 51 4c 49 54 45 5f 52 41 4e 47 45 3b 0a 20 20 7d  QLITE_RANGE;.  }
1876a 0a 20 20 69 2d 2d 3b 0a 20 20 70 56 61 72 20 3d  .  i--;.  pVar =
1876b 20 26 70 2d 3e 61 56 61 72 5b 69 5d 3b 0a 20 20   &p->aVar[i];.  
1876c 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65  sqlite3VdbeMemRe
1876d 6c 65 61 73 65 28 70 56 61 72 29 3b 0a 20 20 70  lease(pVar);.  p
1876e 56 61 72 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Var->flags = MEM
1876f 5f 4e 75 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33  _Null;.  sqlite3
18770 45 72 72 6f 72 28 70 2d 3e 64 62 2c 20 53 51 4c  Error(p->db, SQL
18771 49 54 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 72 65  ITE_OK, 0);.  re
18772 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
18773 7d 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64 20 61 20  }../*.** Bind a 
18774 74 65 78 74 20 6f 72 20 42 4c 4f 42 20 76 61 6c  text or BLOB val
18775 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ue..*/.static in
18776 74 20 62 69 6e 64 54 65 78 74 28 0a 20 20 73 71  t bindText(.  sq
18777 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d  lite3_stmt *pStm
18778 74 2c 20 20 20 2f 2a 20 54 68 65 20 73 74 61 74  t,   /* The stat
18779 65 6d 65 6e 74 20 74 6f 20 62 69 6e 64 20 61 67  ement to bind ag
1877a 61 69 6e 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ainst */.  int i
1877b 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1877c 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1877d 65 20 70 61 72 61 6d 65 74 65 72 20 74 6f 20 62  e parameter to b
1877e 69 6e 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ind */.  const v
1877f 6f 69 64 20 2a 7a 44 61 74 61 2c 20 20 20 20 20  oid *zData,     
18780 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68  /* Pointer to th
18781 65 20 64 61 74 61 20 74 6f 20 62 65 20 62 6f 75  e data to be bou
18782 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 44 61 74  nd */.  int nDat
18783 61 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  a,             /
18784 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
18785 73 20 6f 66 20 64 61 74 61 20 74 6f 20 62 65 20  s of data to be 
18786 62 6f 75 6e 64 20 2a 2f 0a 20 20 76 6f 69 64 20  bound */.  void 
18787 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 2c 20  (*xDel)(void*), 
18788 20 20 2f 2a 20 44 65 73 74 72 75 63 74 6f 72 20    /* Destructor 
18789 66 6f 72 20 74 68 65 20 64 61 74 61 20 2a 2f 0a  for the data */.
1878a 20 20 69 6e 74 20 65 6e 63 6f 64 69 6e 67 20 20    int encoding  
1878b 20 20 20 20 20 20 20 20 20 2f 2a 20 45 6e 63 6f           /* Enco
1878c 64 69 6e 67 20 66 6f 72 20 74 68 65 20 64 61 74  ding for the dat
1878d 61 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  a */.){.  Vdbe *
1878e 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53 74 6d  p = (Vdbe *)pStm
1878f 74 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a  t;.  Mem *pVar;.
18790 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 72 63 20    int rc;..  rc 
18791 3d 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20  = vdbeUnbind(p, 
18792 69 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  i);.  if( rc==SQ
18793 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69  LITE_OK ){.    i
18794 66 28 20 7a 44 61 74 61 21 3d 30 20 29 7b 0a 20  f( zData!=0 ){. 
18795 20 20 20 20 20 70 56 61 72 20 3d 20 26 70 2d 3e       pVar = &p->
18796 61 56 61 72 5b 69 2d 31 5d 3b 0a 20 20 20 20 20  aVar[i-1];.     
18797 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62   rc = sqlite3Vdb
18798 65 4d 65 6d 53 65 74 53 74 72 28 70 56 61 72 2c  eMemSetStr(pVar,
18799 20 7a 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 65   zData, nData, e
1879a 6e 63 6f 64 69 6e 67 2c 20 78 44 65 6c 29 3b 0a  ncoding, xDel);.
1879b 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
1879c 4c 49 54 45 5f 4f 4b 20 26 26 20 65 6e 63 6f 64  LITE_OK && encod
1879d 69 6e 67 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ing!=0 ){.      
1879e 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1879f 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
187a0 28 70 56 61 72 2c 20 45 4e 43 28 70 2d 3e 64 62  (pVar, ENC(p->db
187a1 29 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ));.      }.    
187a2 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 70    sqlite3Error(p
187a3 2d 3e 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20  ->db, rc, 0);.  
187a4 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
187a5 41 70 69 45 78 69 74 28 70 2d 3e 64 62 2c 20 72  ApiExit(p->db, r
187a6 63 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  c);.    }.    sq
187a7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
187a8 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  e(p->db->mutex);
187a9 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
187aa 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 42 69 6e 64  ;.}.../*.** Bind
187ab 20 61 20 62 6c 6f 62 20 76 61 6c 75 65 20 74 6f   a blob value to
187ac 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   an SQL statemen
187ad 74 20 76 61 72 69 61 62 6c 65 2e 0a 2a 2f 0a 53  t variable..*/.S
187ae 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
187af 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28  lite3_bind_blob(
187b0 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
187b1 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69  *pStmt, .  int i
187b2 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
187b3 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e  *zData, .  int n
187b4 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a  Data, .  void (*
187b5 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
187b6 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78    return bindTex
187b7 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74  t(pStmt, i, zDat
187b8 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20  a, nData, xDel, 
187b9 30 29 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49  0);.}.SQLITE_API
187ba 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
187bb 64 5f 64 6f 75 62 6c 65 28 73 71 6c 69 74 65 33  d_double(sqlite3
187bc 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e  _stmt *pStmt, in
187bd 74 20 69 2c 20 64 6f 75 62 6c 65 20 72 56 61 6c  t i, double rVal
187be 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  ue){.  int rc;. 
187bf 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65   Vdbe *p = (Vdbe
187c0 20 2a 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d   *)pStmt;.  rc =
187c1 20 76 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69   vdbeUnbind(p, i
187c2 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
187c3 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
187c4 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 44  lite3VdbeMemSetD
187c5 6f 75 62 6c 65 28 26 70 2d 3e 61 56 61 72 5b 69  ouble(&p->aVar[i
187c6 2d 31 5d 2c 20 72 56 61 6c 75 65 29 3b 0a 20 20  -1], rValue);.  
187c7 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
187c8 6c 65 61 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74  leave(p->db->mut
187c9 65 78 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ex);.  }.  retur
187ca 6e 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41  n rc;.}.SQLITE_A
187cb 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62  PI int sqlite3_b
187cc 69 6e 64 5f 69 6e 74 28 73 71 6c 69 74 65 33 5f  ind_int(sqlite3_
187cd 73 74 6d 74 20 2a 70 2c 20 69 6e 74 20 69 2c 20  stmt *p, int i, 
187ce 69 6e 74 20 69 56 61 6c 75 65 29 7b 0a 20 20 72  int iValue){.  r
187cf 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 62 69  eturn sqlite3_bi
187d0 6e 64 5f 69 6e 74 36 34 28 70 2c 20 69 2c 20 28  nd_int64(p, i, (
187d1 69 36 34 29 69 56 61 6c 75 65 29 3b 0a 7d 0a 53  i64)iValue);.}.S
187d2 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
187d3 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
187d4 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
187d5 53 74 6d 74 2c 20 69 6e 74 20 69 2c 20 73 71 6c  Stmt, int i, sql
187d6 69 74 65 5f 69 6e 74 36 34 20 69 56 61 6c 75 65  ite_int64 iValue
187d7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
187d8 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a  dbe *p = (Vdbe *
187d9 29 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76  )pStmt;.  rc = v
187da 64 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b  dbeUnbind(p, i);
187db 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
187dc 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69  E_OK ){.    sqli
187dd 74 65 33 56 64 62 65 4d 65 6d 53 65 74 49 6e 74  te3VdbeMemSetInt
187de 36 34 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d  64(&p->aVar[i-1]
187df 2c 20 69 56 61 6c 75 65 29 3b 0a 20 20 20 20 73  , iValue);.    s
187e0 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
187e1 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
187e2 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
187e3 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20  c;.}.SQLITE_API 
187e4 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  int sqlite3_bind
187e5 5f 6e 75 6c 6c 28 73 71 6c 69 74 65 33 5f 73 74  _null(sqlite3_st
187e6 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20 69  mt *pStmt, int i
187e7 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56  ){.  int rc;.  V
187e8 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a 29  dbe *p = (Vdbe*)
187e9 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64  pStmt;.  rc = vd
187ea 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
187eb 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
187ec 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  _OK ){.    sqlit
187ed 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70  e3_mutex_leave(p
187ee 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
187ef 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
187f0 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
187f1 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
187f2 74 28 20 0a 20 20 73 71 6c 69 74 65 33 5f 73 74  t( .  sqlite3_st
187f3 6d 74 20 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e  mt *pStmt, .  in
187f4 74 20 69 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68  t i, .  const ch
187f5 61 72 20 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e  ar *zData, .  in
187f6 74 20 6e 44 61 74 61 2c 20 0a 20 20 76 6f 69 64  t nData, .  void
187f7 20 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a   (*xDel)(void*).
187f8 29 7b 0a 20 20 72 65 74 75 72 6e 20 62 69 6e 64  ){.  return bind
187f9 54 65 78 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a  Text(pStmt, i, z
187fa 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 78 44 65  Data, nData, xDe
187fb 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 29 3b  l, SQLITE_UTF8);
187fc 0a 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  .}.#ifndef SQLIT
187fd 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 53 51 4c  E_OMIT_UTF16.SQL
187fe 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
187ff 74 65 33 5f 62 69 6e 64 5f 74 65 78 74 31 36 28  te3_bind_text16(
18800 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
18801 2a 70 53 74 6d 74 2c 20 0a 20 20 69 6e 74 20 69  *pStmt, .  int i
18802 2c 20 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  , .  const void 
18803 2a 7a 44 61 74 61 2c 20 0a 20 20 69 6e 74 20 6e  *zData, .  int n
18804 44 61 74 61 2c 20 0a 20 20 76 6f 69 64 20 28 2a  Data, .  void (*
18805 78 44 65 6c 29 28 76 6f 69 64 2a 29 0a 29 7b 0a  xDel)(void*).){.
18806 20 20 72 65 74 75 72 6e 20 62 69 6e 64 54 65 78    return bindTex
18807 74 28 70 53 74 6d 74 2c 20 69 2c 20 7a 44 61 74  t(pStmt, i, zDat
18808 61 2c 20 6e 44 61 74 61 2c 20 78 44 65 6c 2c 20  a, nData, xDel, 
18809 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49  SQLITE_UTF16NATI
1880a 56 45 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  VE);.}.#endif /*
1880b 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46   SQLITE_OMIT_UTF
1880c 31 36 20 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  16 */.SQLITE_API
1880d 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69 6e   int sqlite3_bin
1880e 64 5f 76 61 6c 75 65 28 73 71 6c 69 74 65 33 5f  d_value(sqlite3_
1880f 73 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74  stmt *pStmt, int
18810 20 69 2c 20 63 6f 6e 73 74 20 73 71 6c 69 74 65   i, const sqlite
18811 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 75 65 29  3_value *pValue)
18812 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64  {.  int rc;.  Vd
18813 62 65 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29  be *p = (Vdbe *)
18814 70 53 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64  pStmt;.  rc = vd
18815 62 65 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a  beUnbind(p, i);.
18816 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
18817 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  _OK ){.    rc = 
18818 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f  sqlite3VdbeMemCo
18819 70 79 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d  py(&p->aVar[i-1]
1881a 2c 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 69  , pValue);.    i
1881b 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1881c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73   ){.      rc = s
1881d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1881e 45 6e 63 6f 64 69 6e 67 28 26 70 2d 3e 61 56 61  Encoding(&p->aVa
1881f 72 5b 69 2d 31 5d 2c 20 45 4e 43 28 70 2d 3e 64  r[i-1], ENC(p->d
18820 62 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  b));.    }.    s
18821 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
18822 76 65 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  ve(p->db->mutex)
18823 3b 0a 20 20 7d 0a 20 20 72 63 20 3d 20 73 71 6c  ;.  }.  rc = sql
18824 69 74 65 33 41 70 69 45 78 69 74 28 70 2d 3e 64  ite3ApiExit(p->d
18825 62 2c 20 72 63 29 3b 0a 20 20 72 65 74 75 72 6e  b, rc);.  return
18826 20 72 63 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50   rc;.}.SQLITE_AP
18827 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 69  I int sqlite3_bi
18828 6e 64 5f 7a 65 72 6f 62 6c 6f 62 28 73 71 6c 69  nd_zeroblob(sqli
18829 74 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 2c  te3_stmt *pStmt,
1882a 20 69 6e 74 20 69 2c 20 69 6e 74 20 6e 29 7b 0a   int i, int n){.
1882b 20 20 69 6e 74 20 72 63 3b 0a 20 20 56 64 62 65    int rc;.  Vdbe
1882c 20 2a 70 20 3d 20 28 56 64 62 65 20 2a 29 70 53   *p = (Vdbe *)pS
1882d 74 6d 74 3b 0a 20 20 72 63 20 3d 20 76 64 62 65  tmt;.  rc = vdbe
1882e 55 6e 62 69 6e 64 28 70 2c 20 69 29 3b 0a 20 20  Unbind(p, i);.  
1882f 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
18830 4b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  K ){.    sqlite3
18831 56 64 62 65 4d 65 6d 53 65 74 5a 65 72 6f 42 6c  VdbeMemSetZeroBl
18832 6f 62 28 26 70 2d 3e 61 56 61 72 5b 69 2d 31 5d  ob(&p->aVar[i-1]
18833 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , n);.    sqlite
18834 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
18835 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
18836 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
18837 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
18838 65 20 6e 75 6d 62 65 72 20 6f 66 20 77 69 6c 64  e number of wild
18839 63 61 72 64 73 20 74 68 61 74 20 63 61 6e 20 62  cards that can b
1883a 65 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 6f  e potentially bo
1883b 75 6e 64 20 74 6f 2e 0a 2a 2a 20 54 68 69 73 20  und to..** This 
1883c 72 6f 75 74 69 6e 65 20 69 73 20 61 64 64 65 64  routine is added
1883d 20 74 6f 20 73 75 70 70 6f 72 74 20 44 42 44 3a   to support DBD:
1883e 3a 53 51 4c 69 74 65 2e 20 20 0a 2a 2f 0a 53 51  :SQLite.  .*/.SQ
1883f 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
18840 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65  ite3_bind_parame
18841 74 65 72 5f 63 6f 75 6e 74 28 73 71 6c 69 74 65  ter_count(sqlite
18842 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
18843 20 20 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62    Vdbe *p = (Vdb
18844 65 2a 29 70 53 74 6d 74 3b 0a 20 20 72 65 74 75  e*)pStmt;.  retu
18845 72 6e 20 70 20 3f 20 70 2d 3e 6e 56 61 72 20 3a  rn p ? p->nVar :
18846 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65   0;.}../*.** Cre
18847 61 74 65 20 61 20 6d 61 70 70 69 6e 67 20 66 72  ate a mapping fr
18848 6f 6d 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  om variable numb
18849 65 72 73 20 74 6f 20 76 61 72 69 61 62 6c 65 20  ers to variable 
1884a 6e 61 6d 65 73 0a 2a 2a 20 69 6e 20 74 68 65 20  names.** in the 
1884b 56 64 62 65 2e 61 7a 56 61 72 5b 5d 20 61 72 72  Vdbe.azVar[] arr
1884c 61 79 2c 20 69 66 20 73 75 63 68 20 61 20 6d 61  ay, if such a ma
1884d 70 70 69 6e 67 20 64 6f 65 73 20 6e 6f 74 20 61  pping does not a
1884e 6c 72 65 61 64 79 0a 2a 2a 20 65 78 69 73 74 2e  lready.** exist.
1884f 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
18850 63 72 65 61 74 65 56 61 72 4d 61 70 28 56 64 62  createVarMap(Vdb
18851 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 70 2d  e *p){.  if( !p-
18852 3e 6f 6b 56 61 72 20 29 7b 0a 20 20 20 20 73 71  >okVar ){.    sq
18853 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65  lite3_mutex_ente
18854 72 28 70 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b  r(p->db->mutex);
18855 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 6f 6b 56  .    if( !p->okV
18856 61 72 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  ar ){.      int 
18857 6a 3b 0a 20 20 20 20 20 20 4f 70 20 2a 70 4f 70  j;.      Op *pOp
18858 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 2c  ;.      for(j=0,
18859 20 70 4f 70 3d 70 2d 3e 61 4f 70 3b 20 6a 3c 70   pOp=p->aOp; j<p
1885a 2d 3e 6e 4f 70 3b 20 6a 2b 2b 2c 20 70 4f 70 2b  ->nOp; j++, pOp+
1885b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
1885c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
1885d 56 61 72 69 61 62 6c 65 20 29 7b 0a 20 20 20 20  Variable ){.    
1885e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
1885f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d 3e  p->p1>0 && pOp->
18860 70 31 3c 3d 70 2d 3e 6e 56 61 72 20 29 3b 0a 20  p1<=p->nVar );. 
18861 20 20 20 20 20 20 20 20 20 70 2d 3e 61 7a 56 61           p->azVa
18862 72 5b 70 4f 70 2d 3e 70 31 2d 31 5d 20 3d 20 70  r[pOp->p1-1] = p
18863 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 20 20  Op->p4.z;.      
18864 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
18865 20 20 70 2d 3e 6f 6b 56 61 72 20 3d 20 31 3b 0a    p->okVar = 1;.
18866 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
18867 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 2d  3_mutex_leave(p-
18868 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 7d  >db->mutex);.  }
18869 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
1886a 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 77   the name of a w
1886b 69 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65  ildcard paramete
1886c 72 2e 20 20 52 65 74 75 72 6e 20 4e 55 4c 4c 20  r.  Return NULL 
1886d 69 66 20 74 68 65 20 69 6e 64 65 78 0a 2a 2a 20  if the index.** 
1886e 69 73 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 20  is out of range 
1886f 6f 72 20 69 66 20 74 68 65 20 77 69 6c 64 63 61  or if the wildca
18870 72 64 20 69 73 20 75 6e 6e 61 6d 65 64 2e 0a 2a  rd is unnamed..*
18871 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  *.** The result 
18872 69 73 20 61 6c 77 61 79 73 20 55 54 46 2d 38 2e  is always UTF-8.
18873 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63  .*/.SQLITE_API c
18874 6f 6e 73 74 20 63 68 61 72 20 2a 73 71 6c 69 74  onst char *sqlit
18875 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74 65  e3_bind_paramete
18876 72 5f 6e 61 6d 65 28 73 71 6c 69 74 65 33 5f 73  r_name(sqlite3_s
18877 74 6d 74 20 2a 70 53 74 6d 74 2c 20 69 6e 74 20  tmt *pStmt, int 
18878 69 29 7b 0a 20 20 56 64 62 65 20 2a 70 20 3d 20  i){.  Vdbe *p = 
18879 28 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20  (Vdbe*)pStmt;.  
1887a 69 66 28 20 70 3d 3d 30 20 7c 7c 20 69 3c 31 20  if( p==0 || i<1 
1887b 7c 7c 20 69 3e 70 2d 3e 6e 56 61 72 20 29 7b 0a  || i>p->nVar ){.
1887c 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20      return 0;.  
1887d 7d 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70  }.  createVarMap
1887e 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 2d  (p);.  return p-
1887f 3e 61 7a 56 61 72 5b 69 2d 31 5d 3b 0a 7d 0a 0a  >azVar[i-1];.}..
18880 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 77 69  /*.** Given a wi
18881 6c 64 63 61 72 64 20 70 61 72 61 6d 65 74 65 72  ldcard parameter
18882 20 6e 61 6d 65 2c 20 72 65 74 75 72 6e 20 74 68   name, return th
18883 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 76  e index of the v
18884 61 72 69 61 62 6c 65 0a 2a 2a 20 77 69 74 68 20  ariable.** with 
18885 74 68 61 74 20 6e 61 6d 65 2e 20 20 49 66 20 74  that name.  If t
18886 68 65 72 65 20 69 73 20 6e 6f 20 76 61 72 69 61  here is no varia
18887 62 6c 65 20 77 69 74 68 20 74 68 65 20 67 69 76  ble with the giv
18888 65 6e 20 6e 61 6d 65 2c 0a 2a 2a 20 72 65 74 75  en name,.** retu
18889 72 6e 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rn 0..*/.SQLITE_
1888a 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
1888b 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 69  bind_parameter_i
1888c 6e 64 65 78 28 73 71 6c 69 74 65 33 5f 73 74 6d  ndex(sqlite3_stm
1888d 74 20 2a 70 53 74 6d 74 2c 20 63 6f 6e 73 74 20  t *pStmt, const 
1888e 63 68 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20  char *zName){.  
1888f 56 64 62 65 20 2a 70 20 3d 20 28 56 64 62 65 2a  Vdbe *p = (Vdbe*
18890 29 70 53 74 6d 74 3b 0a 20 20 69 6e 74 20 69 3b  )pStmt;.  int i;
18891 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20  .  if( p==0 ){. 
18892 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
18893 0a 20 20 63 72 65 61 74 65 56 61 72 4d 61 70 28  .  createVarMap(
18894 70 29 3b 20 0a 20 20 69 66 28 20 7a 4e 61 6d 65  p); .  if( zName
18895 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
18896 20 69 3c 70 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29   i<p->nVar; i++)
18897 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68  {.      const ch
18898 61 72 20 2a 7a 20 3d 20 70 2d 3e 61 7a 56 61 72  ar *z = p->azVar
18899 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28 20 7a  [i];.      if( z
1889a 20 26 26 20 73 74 72 63 6d 70 28 7a 2c 7a 4e 61   && strcmp(z,zNa
1889b 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  me)==0 ){.      
1889c 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20 20    return i+1;.  
1889d 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1889e 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f    return 0;.}../
1889f 2a 0a 2a 2a 20 54 72 61 6e 73 66 65 72 20 61 6c  *.** Transfer al
188a0 6c 20 62 69 6e 64 69 6e 67 73 20 66 72 6f 6d 20  l bindings from 
188a1 74 68 65 20 66 69 72 73 74 20 73 74 61 74 65 6d  the first statem
188a2 65 6e 74 20 6f 76 65 72 20 74 6f 20 74 68 65 20  ent over to the 
188a3 73 65 63 6f 6e 64 2e 0a 2a 2a 20 49 66 20 74 68  second..** If th
188a4 65 20 74 77 6f 20 73 74 61 74 65 6d 65 6e 74 73  e two statements
188a5 20 63 6f 6e 74 61 69 6e 20 61 20 64 69 66 66 65   contain a diffe
188a6 72 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 62  rent number of b
188a7 69 6e 64 69 6e 67 73 2c 20 74 68 65 6e 0a 2a 2a  indings, then.**
188a8 20 61 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52   an SQLITE_ERROR
188a9 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
188aa 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
188ab 69 6e 74 20 73 71 6c 69 74 65 33 54 72 61 6e 73  int sqlite3Trans
188ac 66 65 72 42 69 6e 64 69 6e 67 73 28 73 71 6c 69  ferBindings(sqli
188ad 74 65 33 5f 73 74 6d 74 20 2a 70 46 72 6f 6d 53  te3_stmt *pFromS
188ae 74 6d 74 2c 20 73 71 6c 69 74 65 33 5f 73 74 6d  tmt, sqlite3_stm
188af 74 20 2a 70 54 6f 53 74 6d 74 29 7b 0a 20 20 56  t *pToStmt){.  V
188b0 64 62 65 20 2a 70 46 72 6f 6d 20 3d 20 28 56 64  dbe *pFrom = (Vd
188b1 62 65 2a 29 70 46 72 6f 6d 53 74 6d 74 3b 0a 20  be*)pFromStmt;. 
188b2 20 56 64 62 65 20 2a 70 54 6f 20 3d 20 28 56 64   Vdbe *pTo = (Vd
188b3 62 65 2a 29 70 54 6f 53 74 6d 74 3b 0a 20 20 69  be*)pToStmt;.  i
188b4 6e 74 20 69 2c 20 72 63 20 3d 20 53 51 4c 49 54  nt i, rc = SQLIT
188b5 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 28 70 46 72  E_OK;.  if( (pFr
188b6 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f  om->magic!=VDBE_
188b7 4d 41 47 49 43 5f 52 55 4e 20 26 26 20 70 46 72  MAGIC_RUN && pFr
188b8 6f 6d 2d 3e 6d 61 67 69 63 21 3d 56 44 42 45 5f  om->magic!=VDBE_
188b9 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20 20 20 20  MAGIC_HALT).    
188ba 7c 7c 20 28 70 54 6f 2d 3e 6d 61 67 69 63 21 3d  || (pTo->magic!=
188bb 56 44 42 45 5f 4d 41 47 49 43 5f 52 55 4e 20 26  VDBE_MAGIC_RUN &
188bc 26 20 70 54 6f 2d 3e 6d 61 67 69 63 21 3d 56 44  & pTo->magic!=VD
188bd 42 45 5f 4d 41 47 49 43 5f 48 41 4c 54 29 0a 20  BE_MAGIC_HALT). 
188be 20 20 20 7c 7c 20 70 54 6f 2d 3e 64 62 21 3d 70     || pTo->db!=p
188bf 46 72 6f 6d 2d 3e 64 62 20 29 7b 0a 20 20 20 20  From->db ){.    
188c0 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
188c1 53 55 53 45 3b 0a 20 20 7d 0a 20 20 69 66 28 20  SUSE;.  }.  if( 
188c2 70 46 72 6f 6d 2d 3e 6e 56 61 72 21 3d 70 54 6f  pFrom->nVar!=pTo
188c3 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 72 65  ->nVar ){.    re
188c4 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
188c5 52 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  R;.  }.  sqlite3
188c6 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 54 6f  _mutex_enter(pTo
188c7 2d 3e 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  ->db->mutex);.  
188c8 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c  for(i=0; rc==SQL
188c9 49 54 45 5f 4f 4b 20 26 26 20 69 3c 70 46 72 6f  ITE_OK && i<pFro
188ca 6d 2d 3e 6e 56 61 72 3b 20 69 2b 2b 29 7b 0a 20  m->nVar; i++){. 
188cb 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
188cc 6d 4d 6f 76 65 28 26 70 54 6f 2d 3e 61 56 61 72  mMove(&pTo->aVar
188cd 5b 69 5d 2c 20 26 70 46 72 6f 6d 2d 3e 61 56 61  [i], &pFrom->aVa
188ce 72 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  r[i]);.  }.  sql
188cf 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
188d0 28 70 54 6f 2d 3e 64 62 2d 3e 6d 75 74 65 78 29  (pTo->db->mutex)
188d1 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
188d2 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 72 63 3d  SQLITE_OK || rc=
188d3 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b  =SQLITE_NOMEM );
188d4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
188d5 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
188d6 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a  OMIT_DEPRECATED.
188d7 2f 2a 0a 2a 2a 20 44 65 70 72 65 63 61 74 65 64  /*.** Deprecated
188d8 20 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66   external interf
188d9 61 63 65 2e 20 20 49 6e 74 65 72 6e 61 6c 2f 63  ace.  Internal/c
188da 6f 72 65 20 53 51 4c 69 74 65 20 63 6f 64 65 0a  ore SQLite code.
188db 2a 2a 20 73 68 6f 75 6c 64 20 63 61 6c 6c 20 73  ** should call s
188dc 71 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69  qlite3TransferBi
188dd 6e 64 69 6e 67 73 2e 0a 2a 2f 0a 53 51 4c 49 54  ndings..*/.SQLIT
188de 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
188df 33 5f 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  3_transfer_bindi
188e0 6e 67 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ngs(sqlite3_stmt
188e1 20 2a 70 46 72 6f 6d 53 74 6d 74 2c 20 73 71 6c   *pFromStmt, sql
188e2 69 74 65 33 5f 73 74 6d 74 20 2a 70 54 6f 53 74  ite3_stmt *pToSt
188e3 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 71  mt){.  return sq
188e4 6c 69 74 65 33 54 72 61 6e 73 66 65 72 42 69 6e  lite3TransferBin
188e5 64 69 6e 67 73 28 70 46 72 6f 6d 53 74 6d 74 2c  dings(pFromStmt,
188e6 20 70 54 6f 53 74 6d 74 29 3b 0a 7d 0a 23 65 6e   pToStmt);.}.#en
188e7 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  dif../*.** Retur
188e8 6e 20 74 68 65 20 73 71 6c 69 74 65 33 2a 20 64  n the sqlite3* d
188e9 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74  atabase handle t
188ea 6f 20 77 68 69 63 68 20 74 68 65 20 70 72 65 70  o which the prep
188eb 61 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 67  ared statement g
188ec 69 76 65 6e 0a 2a 2a 20 69 6e 20 74 68 65 20 61  iven.** in the a
188ed 72 67 75 6d 65 6e 74 20 62 65 6c 6f 6e 67 73 2e  rgument belongs.
188ee 20 20 54 68 69 73 20 69 73 20 74 68 65 20 73 61    This is the sa
188ef 6d 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  me database hand
188f0 6c 65 20 74 68 61 74 20 77 61 73 0a 2a 2a 20 74  le that was.** t
188f1 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e  he first argumen
188f2 74 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  t to the sqlite3
188f3 5f 70 72 65 70 61 72 65 28 29 20 74 68 61 74 20  _prepare() that 
188f4 77 61 73 20 75 73 65 64 20 74 6f 20 63 72 65 61  was used to crea
188f5 74 65 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d  te.** the statem
188f6 65 6e 74 20 69 6e 20 74 68 65 20 66 69 72 73 74  ent in the first
188f7 20 70 6c 61 63 65 2e 0a 2a 2f 0a 53 51 4c 49 54   place..*/.SQLIT
188f8 45 5f 41 50 49 20 73 71 6c 69 74 65 33 20 2a 73  E_API sqlite3 *s
188f9 71 6c 69 74 65 33 5f 64 62 5f 68 61 6e 64 6c 65  qlite3_db_handle
188fa 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70  (sqlite3_stmt *p
188fb 53 74 6d 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  Stmt){.  return 
188fc 70 53 74 6d 74 20 3f 20 28 28 56 64 62 65 2a 29  pStmt ? ((Vdbe*)
188fd 70 53 74 6d 74 29 2d 3e 64 62 20 3a 20 30 3b 0a  pStmt)->db : 0;.
188fe 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
188ff 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
18900 20 6e 65 78 74 20 70 72 65 70 61 72 65 64 20 73   next prepared s
18901 74 61 74 65 6d 65 6e 74 20 61 66 74 65 72 20 70  tatement after p
18902 53 74 6d 74 20 61 73 73 6f 63 69 61 74 65 64 0a  Stmt associated.
18903 2a 2a 20 77 69 74 68 20 64 61 74 61 62 61 73 65  ** with database
18904 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 70 44 62 2e   connection pDb.
18905 20 20 49 66 20 70 53 74 6d 74 20 69 73 20 4e 55    If pStmt is NU
18906 4c 4c 2c 20 72 65 74 75 72 6e 20 74 68 65 20 66  LL, return the f
18907 69 72 73 74 0a 2a 2a 20 70 72 65 70 61 72 65 64  irst.** prepared
18908 20 73 74 61 74 65 6d 65 6e 74 20 66 6f 72 20 74   statement for t
18909 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
1890a 65 63 74 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20  ection.  Return 
1890b 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 0a 2a 2a  NULL if there.**
1890c 20 61 72 65 20 6e 6f 20 6d 6f 72 65 2e 0a 2a 2f   are no more..*/
1890d 0a 53 51 4c 49 54 45 5f 41 50 49 20 73 71 6c 69  .SQLITE_API sqli
1890e 74 65 33 5f 73 74 6d 74 20 2a 73 71 6c 69 74 65  te3_stmt *sqlite
1890f 33 5f 6e 65 78 74 5f 73 74 6d 74 28 73 71 6c 69  3_next_stmt(sqli
18910 74 65 33 20 2a 70 44 62 2c 20 73 71 6c 69 74 65  te3 *pDb, sqlite
18911 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 29 7b 0a  3_stmt *pStmt){.
18912 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
18913 70 4e 65 78 74 3b 0a 20 20 73 71 6c 69 74 65 33  pNext;.  sqlite3
18914 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 70 44 62  _mutex_enter(pDb
18915 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
18916 70 53 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20  pStmt==0 ){.    
18917 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33  pNext = (sqlite3
18918 5f 73 74 6d 74 2a 29 70 44 62 2d 3e 70 56 64 62  _stmt*)pDb->pVdb
18919 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
1891a 70 4e 65 78 74 20 3d 20 28 73 71 6c 69 74 65 33  pNext = (sqlite3
1891b 5f 73 74 6d 74 2a 29 28 28 56 64 62 65 2a 29 70  _stmt*)((Vdbe*)p
1891c 53 74 6d 74 29 2d 3e 70 4e 65 78 74 3b 0a 20 20  Stmt)->pNext;.  
1891d 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
1891e 78 5f 6c 65 61 76 65 28 70 44 62 2d 3e 6d 75 74  x_leave(pDb->mut
1891f 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4e  ex);.  return pN
18920 65 78 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  ext;.}../*.** Re
18921 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65 20 6f  turn the value o
18922 66 20 61 20 73 74 61 74 75 73 20 63 6f 75 6e 74  f a status count
18923 65 72 20 66 6f 72 20 61 20 70 72 65 70 61 72 65  er for a prepare
18924 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2f 0a 53  d statement.*/.S
18925 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
18926 6c 69 74 65 33 5f 73 74 6d 74 5f 73 74 61 74 75  lite3_stmt_statu
18927 73 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  s(sqlite3_stmt *
18928 70 53 74 6d 74 2c 20 69 6e 74 20 6f 70 2c 20 69  pStmt, int op, i
18929 6e 74 20 72 65 73 65 74 46 6c 61 67 29 7b 0a 20  nt resetFlag){. 
1892a 20 56 64 62 65 20 2a 70 56 64 62 65 20 3d 20 28   Vdbe *pVdbe = (
1892b 56 64 62 65 2a 29 70 53 74 6d 74 3b 0a 20 20 69  Vdbe*)pStmt;.  i
1892c 6e 74 20 76 20 3d 20 70 56 64 62 65 2d 3e 61 43  nt v = pVdbe->aC
1892d 6f 75 6e 74 65 72 5b 6f 70 2d 31 5d 3b 0a 20 20  ounter[op-1];.  
1892e 69 66 28 20 72 65 73 65 74 46 6c 61 67 20 29 20  if( resetFlag ) 
1892f 70 56 64 62 65 2d 3e 61 43 6f 75 6e 74 65 72 5b  pVdbe->aCounter[
18930 6f 70 2d 31 5d 20 3d 20 30 3b 0a 20 20 72 65 74  op-1] = 0;.  ret
18931 75 72 6e 20 76 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  urn v;.}../*****
18932 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
18933 20 76 64 62 65 61 70 69 2e 63 20 2a 2a 2a 2a 2a   vdbeapi.c *****
18934 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18935 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18936 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
18937 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
18938 66 69 6c 65 20 76 64 62 65 2e 63 20 2a 2a 2a 2a  file vdbe.c ****
18939 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1893a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1893b 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1893c 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
1893d 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
1893e 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1893f 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
18940 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
18941 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
18942 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
18943 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
18944 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
18945 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
18946 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
18947 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
18948 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
18949 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1894a 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1894b 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1894c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1894d 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1894e 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1894f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18950 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18951 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18952 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18953 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
18954 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  this file implem
18955 65 6e 74 73 20 65 78 65 63 75 74 69 6f 6e 20 6d  ents execution m
18956 65 74 68 6f 64 20 6f 66 20 74 68 65 20 0a 2a 2a  ethod of the .**
18957 20 56 69 72 74 75 61 6c 20 44 61 74 61 62 61 73   Virtual Databas
18958 65 20 45 6e 67 69 6e 65 20 28 56 44 42 45 29 2e  e Engine (VDBE).
18959 20 20 41 20 73 65 70 61 72 61 74 65 20 66 69 6c    A separate fil
1895a 65 20 28 22 76 64 62 65 61 75 78 2e 63 22 29 0a  e ("vdbeaux.c").
1895b 2a 2a 20 68 61 6e 64 6c 65 73 20 68 6f 75 73 65  ** handles house
1895c 6b 65 65 70 69 6e 67 20 64 65 74 61 69 6c 73 20  keeping details 
1895d 73 75 63 68 20 61 73 20 63 72 65 61 74 69 6e 67  such as creating
1895e 20 61 6e 64 20 64 65 6c 65 74 69 6e 67 0a 2a 2a   and deleting.**
1895f 20 56 44 42 45 20 69 6e 73 74 61 6e 63 65 73 2e   VDBE instances.
18960 20 20 54 68 69 73 20 66 69 6c 65 20 69 73 20 73    This file is s
18961 6f 6c 65 6c 79 20 69 6e 74 65 72 65 73 74 65 64  olely interested
18962 20 69 6e 20 65 78 65 63 75 74 69 6e 67 0a 2a 2a   in executing.**
18963 20 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61   the VDBE progra
18964 6d 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  m..**.** In the 
18965 65 78 74 65 72 6e 61 6c 20 69 6e 74 65 72 66 61  external interfa
18966 63 65 2c 20 61 6e 20 22 73 71 6c 69 74 65 33 5f  ce, an "sqlite3_
18967 73 74 6d 74 2a 22 20 69 73 20 61 6e 20 6f 70 61  stmt*" is an opa
18968 71 75 65 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 74  que pointer.** t
18969 6f 20 61 20 56 44 42 45 2e 0a 2a 2a 0a 2a 2a 20  o a VDBE..**.** 
1896a 54 68 65 20 53 51 4c 20 70 61 72 73 65 72 20 67  The SQL parser g
1896b 65 6e 65 72 61 74 65 73 20 61 20 70 72 6f 67 72  enerates a progr
1896c 61 6d 20 77 68 69 63 68 20 69 73 20 74 68 65 6e  am which is then
1896d 20 65 78 65 63 75 74 65 64 20 62 79 0a 2a 2a 20   executed by.** 
1896e 74 68 65 20 56 44 42 45 20 74 6f 20 64 6f 20 74  the VDBE to do t
1896f 68 65 20 77 6f 72 6b 20 6f 66 20 74 68 65 20 53  he work of the S
18970 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 56  QL statement.  V
18971 44 42 45 20 70 72 6f 67 72 61 6d 73 20 61 72 65  DBE programs are
18972 20 0a 2a 2a 20 73 69 6d 69 6c 61 72 20 69 6e 20   .** similar in 
18973 66 6f 72 6d 20 74 6f 20 61 73 73 65 6d 62 6c 79  form to assembly
18974 20 6c 61 6e 67 75 61 67 65 2e 20 20 54 68 65 20   language.  The 
18975 70 72 6f 67 72 61 6d 20 63 6f 6e 73 69 73 74 73  program consists
18976 20 6f 66 0a 2a 2a 20 61 20 6c 69 6e 65 61 72 20   of.** a linear 
18977 73 65 71 75 65 6e 63 65 20 6f 66 20 6f 70 65 72  sequence of oper
18978 61 74 69 6f 6e 73 2e 20 20 45 61 63 68 20 6f 70  ations.  Each op
18979 65 72 61 74 69 6f 6e 20 68 61 73 20 61 6e 20 6f  eration has an o
1897a 70 63 6f 64 65 20 0a 2a 2a 20 61 6e 64 20 35 20  pcode .** and 5 
1897b 6f 70 65 72 61 6e 64 73 2e 20 20 4f 70 65 72 61  operands.  Opera
1897c 6e 64 73 20 50 31 2c 20 50 32 2c 20 61 6e 64 20  nds P1, P2, and 
1897d 50 33 20 61 72 65 20 69 6e 74 65 67 65 72 73 2e  P3 are integers.
1897e 20 20 4f 70 65 72 61 6e 64 20 50 34 20 0a 2a 2a    Operand P4 .**
1897f 20 69 73 20 61 20 6e 75 6c 6c 2d 74 65 72 6d 69   is a null-termi
18980 6e 61 74 65 64 20 73 74 72 69 6e 67 2e 20 20 4f  nated string.  O
18981 70 65 72 61 6e 64 20 50 35 20 69 73 20 61 6e 20  perand P5 is an 
18982 75 6e 73 69 67 6e 65 64 20 63 68 61 72 61 63 74  unsigned charact
18983 65 72 2e 0a 2a 2a 20 46 65 77 20 6f 70 63 6f 64  er..** Few opcod
18984 65 73 20 75 73 65 20 61 6c 6c 20 35 20 6f 70 65  es use all 5 ope
18985 72 61 6e 64 73 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6d  rands..**.** Com
18986 70 75 74 61 74 69 6f 6e 20 72 65 73 75 6c 74 73  putation results
18987 20 61 72 65 20 73 74 6f 72 65 64 20 6f 6e 20 61   are stored on a
18988 20 73 65 74 20 6f 66 20 72 65 67 69 73 74 65 72   set of register
18989 73 20 6e 75 6d 62 65 72 65 64 20 62 65 67 69 6e  s numbered begin
1898a 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 31 20 61  ning.** with 1 a
1898b 6e 64 20 67 6f 69 6e 67 20 75 70 20 74 6f 20 56  nd going up to V
1898c 64 62 65 2e 6e 4d 65 6d 2e 20 20 45 61 63 68 20  dbe.nMem.  Each 
1898d 72 65 67 69 73 74 65 72 20 63 61 6e 20 73 74 6f  register can sto
1898e 72 65 0a 2a 2a 20 65 69 74 68 65 72 20 61 6e 20  re.** either an 
1898f 69 6e 74 65 67 65 72 2c 20 61 20 6e 75 6c 6c 2d  integer, a null-
18990 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
18991 67 2c 20 61 20 66 6c 6f 61 74 69 6e 67 20 70 6f  g, a floating po
18992 69 6e 74 0a 2a 2a 20 6e 75 6d 62 65 72 2c 20 6f  int.** number, o
18993 72 20 74 68 65 20 53 51 4c 20 22 4e 55 4c 4c 22  r the SQL "NULL"
18994 20 76 61 6c 75 65 2e 20 20 41 6e 20 69 6d 70 6c   value.  An impl
18995 69 63 69 74 20 63 6f 6e 76 65 72 73 69 6f 6e 20  icit conversion 
18996 66 72 6f 6d 20 6f 6e 65 0a 2a 2a 20 74 79 70 65  from one.** type
18997 20 74 6f 20 74 68 65 20 6f 74 68 65 72 20 6f 63   to the other oc
18998 63 75 72 73 20 61 73 20 6e 65 63 65 73 73 61 72  curs as necessar
18999 79 2e 0a 2a 2a 20 0a 2a 2a 20 4d 6f 73 74 20 6f  y..** .** Most o
1899a 66 20 74 68 65 20 63 6f 64 65 20 69 6e 20 74 68  f the code in th
1899b 69 73 20 66 69 6c 65 20 69 73 20 74 61 6b 65 6e  is file is taken
1899c 20 75 70 20 62 79 20 74 68 65 20 73 71 6c 69 74   up by the sqlit
1899d 65 33 56 64 62 65 45 78 65 63 28 29 0a 2a 2a 20  e3VdbeExec().** 
1899e 66 75 6e 63 74 69 6f 6e 20 77 68 69 63 68 20 64  function which d
1899f 6f 65 73 20 74 68 65 20 77 6f 72 6b 20 6f 66 20  oes the work of 
189a0 69 6e 74 65 72 70 72 65 74 69 6e 67 20 61 20 56  interpreting a V
189a1 44 42 45 20 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20  DBE program..** 
189a2 42 75 74 20 6f 74 68 65 72 20 72 6f 75 74 69 6e  But other routin
189a3 65 73 20 61 72 65 20 61 6c 73 6f 20 70 72 6f 76  es are also prov
189a4 69 64 65 64 20 74 6f 20 68 65 6c 70 20 69 6e 20  ided to help in 
189a5 62 75 69 6c 64 69 6e 67 20 75 70 0a 2a 2a 20 61  building up.** a
189a6 20 70 72 6f 67 72 61 6d 20 69 6e 73 74 72 75 63   program instruc
189a7 74 69 6f 6e 20 62 79 20 69 6e 73 74 72 75 63 74  tion by instruct
189a8 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 56 61 72 69 6f  ion..**.** Vario
189a9 75 73 20 73 63 72 69 70 74 73 20 73 63 61 6e 20  us scripts scan 
189aa 74 68 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65  this source file
189ab 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e   in order to gen
189ac 65 72 61 74 65 20 48 54 4d 4c 0a 2a 2a 20 64 6f  erate HTML.** do
189ad 63 75 6d 65 6e 74 61 74 69 6f 6e 2c 20 68 65 61  cumentation, hea
189ae 64 65 72 73 20 66 69 6c 65 73 2c 20 6f 72 20 6f  ders files, or o
189af 74 68 65 72 20 64 65 72 69 76 65 64 20 66 69 6c  ther derived fil
189b0 65 73 2e 20 20 54 68 65 20 66 6f 72 6d 61 74 74  es.  The formatt
189b1 69 6e 67 0a 2a 2a 20 6f 66 20 74 68 65 20 63 6f  ing.** of the co
189b2 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20  de in this file 
189b3 69 73 2c 20 74 68 65 72 65 66 6f 72 65 2c 20 69  is, therefore, i
189b4 6d 70 6f 72 74 61 6e 74 2e 20 20 53 65 65 20 6f  mportant.  See o
189b5 74 68 65 72 20 63 6f 6d 6d 65 6e 74 73 0a 2a 2a  ther comments.**
189b6 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f   in this file fo
189b7 72 20 64 65 74 61 69 6c 73 2e 20 20 49 66 20 69  r details.  If i
189b8 6e 20 64 6f 75 62 74 2c 20 64 6f 20 6e 6f 74 20  n doubt, do not 
189b9 64 65 76 69 61 74 65 20 66 72 6f 6d 20 65 78 69  deviate from exi
189ba 73 74 69 6e 67 0a 2a 2a 20 63 6f 6d 6d 65 6e 74  sting.** comment
189bb 69 6e 67 20 61 6e 64 20 69 6e 64 65 6e 74 61 74  ing and indentat
189bc 69 6f 6e 20 70 72 61 63 74 69 63 65 73 20 77 68  ion practices wh
189bd 65 6e 20 63 68 61 6e 67 69 6e 67 20 6f 72 20 61  en changing or a
189be 64 64 69 6e 67 20 63 6f 64 65 2e 0a 2a 2a 0a 2a  dding code..**.*
189bf 2a 20 24 49 64 3a 20 76 64 62 65 2e 63 2c 76 20  * $Id: vdbe.c,v 
189c0 31 2e 37 38 38 20 32 30 30 38 2f 31 31 2f 31 37  1.788 2008/11/17
189c1 20 31 35 3a 33 31 3a 34 38 20 64 61 6e 69 65 6c   15:31:48 daniel
189c2 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
189c3 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
189c4 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ing global varia
189c5 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
189c6 65 64 20 65 76 65 72 79 20 74 69 6d 65 20 61 20  ed every time a 
189c7 63 75 72 73 6f 72 0a 2a 2a 20 6d 6f 76 65 73 2c  cursor.** moves,
189c8 20 65 69 74 68 65 72 20 62 79 20 74 68 65 20 4f   either by the O
189c9 50 5f 4d 6f 76 65 58 58 2c 20 4f 50 5f 4e 65 78  P_MoveXX, OP_Nex
189ca 74 2c 20 6f 72 20 4f 50 5f 50 72 65 76 20 6f 70  t, or OP_Prev op
189cb 63 6f 64 65 73 2e 20 20 54 68 65 20 74 65 73 74  codes.  The test
189cc 0a 2a 2a 20 70 72 6f 63 65 64 75 72 65 73 20 75  .** procedures u
189cd 73 65 20 74 68 69 73 20 69 6e 66 6f 72 6d 61 74  se this informat
189ce 69 6f 6e 20 74 6f 20 6d 61 6b 65 20 73 75 72 65  ion to make sure
189cf 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 61 72   that indices ar
189d0 65 0a 2a 2a 20 77 6f 72 6b 69 6e 67 20 63 6f 72  e.** working cor
189d1 72 65 63 74 6c 79 2e 20 20 54 68 69 73 20 76 61  rectly.  This va
189d2 72 69 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75  riable has no fu
189d3 6e 63 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  nction other tha
189d4 6e 20 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72  n to.** help ver
189d5 69 66 79 20 74 68 65 20 63 6f 72 72 65 63 74 20  ify the correct 
189d6 6f 70 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65  operation of the
189d7 20 6c 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66   library..*/.#if
189d8 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
189d9 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
189da 71 6c 69 74 65 33 5f 73 65 61 72 63 68 5f 63 6f  qlite3_search_co
189db 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66 0a  unt = 0;.#endif.
189dc 0a 2f 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73  ./*.** When this
189dd 20 67 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65   global variable
189de 20 69 73 20 70 6f 73 69 74 69 76 65 2c 20 69 74   is positive, it
189df 20 67 65 74 73 20 64 65 63 72 65 6d 65 6e 74 65   gets decremente
189e0 64 20 6f 6e 63 65 20 62 65 66 6f 72 65 0a 2a 2a  d once before.**
189e1 20 65 61 63 68 20 69 6e 73 74 72 75 63 74 69 6f   each instructio
189e2 6e 20 69 6e 20 74 68 65 20 56 44 42 45 2e 20 20  n in the VDBE.  
189e3 57 68 65 6e 20 72 65 61 63 68 65 73 20 7a 65 72  When reaches zer
189e4 6f 2c 20 74 68 65 20 75 31 2e 69 73 49 6e 74 65  o, the u1.isInte
189e5 72 72 75 70 74 65 64 0a 2a 2a 20 66 69 65 6c 64  rrupted.** field
189e6 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 20   of the sqlite3 
189e7 73 74 72 75 63 74 75 72 65 20 69 73 20 73 65 74  structure is set
189e8 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 73 69 6d   in order to sim
189e9 75 6c 61 74 65 20 61 6e 64 20 69 6e 74 65 72 72  ulate and interr
189ea 75 70 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  upt..**.** This 
189eb 66 61 63 69 6c 69 74 79 20 69 73 20 75 73 65 64  facility is used
189ec 20 66 6f 72 20 74 65 73 74 69 6e 67 20 70 75 72   for testing pur
189ed 70 6f 73 65 73 20 6f 6e 6c 79 2e 20 20 49 74 20  poses only.  It 
189ee 64 6f 65 73 20 6e 6f 74 20 66 75 6e 63 74 69 6f  does not functio
189ef 6e 0a 2a 2a 20 69 6e 20 61 6e 20 6f 72 64 69 6e  n.** in an ordin
189f0 61 72 79 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69  ary build..*/.#i
189f1 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
189f2 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
189f3 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70  sqlite3_interrup
189f4 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e  t_count = 0;.#en
189f5 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e  dif../*.** The n
189f6 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69 61  ext global varia
189f7 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e 74  ble is increment
189f8 65 64 20 65 61 63 68 20 74 79 70 65 20 74 68 65  ed each type the
189f9 20 4f 50 5f 53 6f 72 74 20 6f 70 63 6f 64 65 0a   OP_Sort opcode.
189fa 2a 2a 20 69 73 20 65 78 65 63 75 74 65 64 2e 20  ** is executed. 
189fb 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65 64   The test proced
189fc 75 72 65 73 20 75 73 65 20 74 68 69 73 20 69 6e  ures use this in
189fd 66 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 6d 61 6b  formation to mak
189fe 65 20 73 75 72 65 20 74 68 61 74 0a 2a 2a 20 73  e sure that.** s
189ff 6f 72 74 69 6e 67 20 69 73 20 6f 63 63 75 72 72  orting is occurr
18a00 69 6e 67 20 6f 72 20 6e 6f 74 20 6f 63 63 75 72  ing or not occur
18a01 72 69 6e 67 20 61 74 20 61 70 70 72 6f 70 72 69  ring at appropri
18a02 61 74 65 20 74 69 6d 65 73 2e 20 20 20 54 68 69  ate times.   Thi
18a03 73 20 76 61 72 69 61 62 6c 65 0a 2a 2a 20 68 61  s variable.** ha
18a04 73 20 6e 6f 20 66 75 6e 63 74 69 6f 6e 20 6f 74  s no function ot
18a05 68 65 72 20 74 68 61 6e 20 74 6f 20 68 65 6c 70  her than to help
18a06 20 76 65 72 69 66 79 20 74 68 65 20 63 6f 72 72   verify the corr
18a07 65 63 74 20 6f 70 65 72 61 74 69 6f 6e 20 6f 66  ect operation of
18a08 20 74 68 65 0a 2a 2a 20 6c 69 62 72 61 72 79 2e   the.** library.
18a09 0a 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54  .*/.#ifdef SQLIT
18a0a 45 5f 54 45 53 54 0a 53 51 4c 49 54 45 5f 41 50  E_TEST.SQLITE_AP
18a0b 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 6f  I int sqlite3_so
18a0c 72 74 5f 63 6f 75 6e 74 20 3d 20 30 3b 0a 23 65  rt_count = 0;.#e
18a0d 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
18a0e 6e 65 78 74 20 67 6c 6f 62 61 6c 20 76 61 72 69  next global vari
18a0f 61 62 6c 65 20 72 65 63 6f 72 64 73 20 74 68 65  able records the
18a10 20 73 69 7a 65 20 6f 66 20 74 68 65 20 6c 61 72   size of the lar
18a11 67 65 73 74 20 4d 45 4d 5f 42 6c 6f 62 0a 2a 2a  gest MEM_Blob.**
18a12 20 6f 72 20 4d 45 4d 5f 53 74 72 20 74 68 61 74   or MEM_Str that
18a13 20 68 61 73 20 62 65 65 6e 20 75 73 65 64 20 62   has been used b
18a14 79 20 61 20 56 44 42 45 20 6f 70 63 6f 64 65 2e  y a VDBE opcode.
18a15 20 20 54 68 65 20 74 65 73 74 20 70 72 6f 63 65    The test proce
18a16 64 75 72 65 73 0a 2a 2a 20 75 73 65 20 74 68 69  dures.** use thi
18a17 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 74 6f  s information to
18a18 20 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 20   make sure that 
18a19 74 68 65 20 7a 65 72 6f 2d 62 6c 6f 62 20 66 75  the zero-blob fu
18a1a 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 2a 2a 20 69  nctionality.** i
18a1b 73 20 77 6f 72 6b 69 6e 67 20 63 6f 72 72 65 63  s working correc
18a1c 74 6c 79 2e 20 20 20 54 68 69 73 20 76 61 72 69  tly.   This vari
18a1d 61 62 6c 65 20 68 61 73 20 6e 6f 20 66 75 6e 63  able has no func
18a1e 74 69 6f 6e 20 6f 74 68 65 72 20 74 68 61 6e 20  tion other than 
18a1f 74 6f 0a 2a 2a 20 68 65 6c 70 20 76 65 72 69 66  to.** help verif
18a20 79 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f 70  y the correct op
18a21 65 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  eration of the l
18a22 69 62 72 61 72 79 2e 0a 2a 2f 0a 23 69 66 64 65  ibrary..*/.#ifde
18a23 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 53 51  f SQLITE_TEST.SQ
18a24 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
18a25 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a  ite3_max_blobsiz
18a26 65 20 3d 20 30 3b 0a 73 74 61 74 69 63 20 76 6f  e = 0;.static vo
18a27 69 64 20 75 70 64 61 74 65 4d 61 78 42 6c 6f 62  id updateMaxBlob
18a28 73 69 7a 65 28 4d 65 6d 20 2a 70 29 7b 0a 20 20  size(Mem *p){.  
18a29 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26 20  if( (p->flags & 
18a2a 28 4d 45 4d 5f 53 74 72 7c 4d 45 4d 5f 42 6c 6f  (MEM_Str|MEM_Blo
18a2b 62 29 29 21 3d 30 20 26 26 20 70 2d 3e 6e 3e 73  b))!=0 && p->n>s
18a2c 71 6c 69 74 65 33 5f 6d 61 78 5f 62 6c 6f 62 73  qlite3_max_blobs
18a2d 69 7a 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ize ){.    sqlit
18a2e 65 33 5f 6d 61 78 5f 62 6c 6f 62 73 69 7a 65 20  e3_max_blobsize 
18a2f 3d 20 70 2d 3e 6e 3b 0a 20 20 7d 0a 7d 0a 23 65  = p->n;.  }.}.#e
18a30 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65 73 74  ndif../*.** Test
18a31 20 61 20 72 65 67 69 73 74 65 72 20 74 6f 20 73   a register to s
18a32 65 65 20 69 66 20 69 74 20 65 78 63 65 65 64 73  ee if it exceeds
18a33 20 74 68 65 20 63 75 72 72 65 6e 74 20 6d 61 78   the current max
18a34 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e 0a  imum blob size..
18a35 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 72  ** If it does, r
18a36 65 63 6f 72 64 20 74 68 65 20 6e 65 77 20 6d 61  ecord the new ma
18a37 78 69 6d 75 6d 20 62 6c 6f 62 20 73 69 7a 65 2e  ximum blob size.
18a38 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .*/.#if defined(
18a39 53 51 4c 49 54 45 5f 54 45 53 54 29 20 26 26 20  SQLITE_TEST) && 
18a3a 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
18a3b 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53  OMIT_BUILTIN_TES
18a3c 54 29 0a 23 20 64 65 66 69 6e 65 20 55 50 44 41  T).# define UPDA
18a3d 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28  TE_MAX_BLOBSIZE(
18a3e 50 29 20 20 75 70 64 61 74 65 4d 61 78 42 6c 6f  P)  updateMaxBlo
18a3f 62 73 69 7a 65 28 50 29 0a 23 65 6c 73 65 0a 23  bsize(P).#else.#
18a40 20 64 65 66 69 6e 65 20 55 50 44 41 54 45 5f 4d   define UPDATE_M
18a41 41 58 5f 42 4c 4f 42 53 49 5a 45 28 50 29 0a 23  AX_BLOBSIZE(P).#
18a42 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  endif../*.** Con
18a43 76 65 72 74 20 74 68 65 20 67 69 76 65 6e 20 72  vert the given r
18a44 65 67 69 73 74 65 72 20 69 6e 74 6f 20 61 20 73  egister into a s
18a45 74 72 69 6e 67 20 69 66 20 69 74 20 69 73 6e 27  tring if it isn'
18a46 74 20 6f 6e 65 0a 2a 2a 20 61 6c 72 65 61 64 79  t one.** already
18a47 2e 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72  . Return non-zer
18a48 6f 20 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20  o if a malloc() 
18a49 66 61 69 6c 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e  fails..*/.#defin
18a4a 65 20 53 74 72 69 6e 67 69 66 79 28 50 2c 20 65  e Stringify(P, e
18a4b 6e 63 29 20 5c 0a 20 20 20 69 66 28 28 28 50 29  nc) \.   if(((P)
18a4c 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 53 74 72  ->flags&(MEM_Str
18a4d 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30 20 26  |MEM_Blob))==0 &
18a4e 26 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d  & sqlite3VdbeMem
18a4f 53 74 72 69 6e 67 69 66 79 28 50 2c 65 6e 63 29  Stringify(P,enc)
18a50 29 20 5c 0a 20 20 20 20 20 7b 20 67 6f 74 6f 20  ) \.     { goto 
18a51 6e 6f 5f 6d 65 6d 3b 20 7d 0a 0a 2f 2a 0a 2a 2a  no_mem; }../*.**
18a52 20 41 6e 20 65 70 68 65 6d 65 72 61 6c 20 73 74   An ephemeral st
18a53 72 69 6e 67 20 76 61 6c 75 65 20 28 73 69 67 6e  ring value (sign
18a54 69 66 69 65 64 20 62 79 20 74 68 65 20 4d 45 4d  ified by the MEM
18a55 5f 45 70 68 65 6d 20 66 6c 61 67 29 20 63 6f 6e  _Ephem flag) con
18a56 74 61 69 6e 73 0a 2a 2a 20 61 20 70 6f 69 6e 74  tains.** a point
18a57 65 72 20 74 6f 20 61 20 64 79 6e 61 6d 69 63 61  er to a dynamica
18a58 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 73 74  lly allocated st
18a59 72 69 6e 67 20 77 68 65 72 65 20 73 6f 6d 65 20  ring where some 
18a5a 6f 74 68 65 72 20 65 6e 74 69 74 79 0a 2a 2a 20  other entity.** 
18a5b 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66  is responsible f
18a5c 6f 72 20 64 65 61 6c 6c 6f 63 61 74 69 6e 67 20  or deallocating 
18a5d 74 68 61 74 20 73 74 72 69 6e 67 2e 20 20 42 65  that string.  Be
18a5e 63 61 75 73 65 20 74 68 65 20 72 65 67 69 73 74  cause the regist
18a5f 65 72 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 63  er.** does not c
18a60 6f 6e 74 72 6f 6c 20 74 68 65 20 73 74 72 69 6e  ontrol the strin
18a61 67 2c 20 69 74 20 6d 69 67 68 74 20 62 65 20 64  g, it might be d
18a62 65 6c 65 74 65 64 20 77 69 74 68 6f 75 74 20 74  eleted without t
18a63 68 65 20 72 65 67 69 73 74 65 72 0a 2a 2a 20 6b  he register.** k
18a64 6e 6f 77 69 6e 67 20 69 74 2e 0a 2a 2a 0a 2a 2a  nowing it..**.**
18a65 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 6f   This routine co
18a66 6e 76 65 72 74 73 20 61 6e 20 65 70 68 65 6d 65  nverts an epheme
18a67 72 61 6c 20 73 74 72 69 6e 67 20 69 6e 74 6f 20  ral string into 
18a68 61 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c  a dynamically al
18a69 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 74 72 69 6e  located.** strin
18a6a 67 20 74 68 61 74 20 74 68 65 20 72 65 67 69 73  g that the regis
18a6b 74 65 72 20 69 74 73 65 6c 66 20 63 6f 6e 74 72  ter itself contr
18a6c 6f 6c 73 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  ols.  In other w
18a6d 6f 72 64 73 2c 20 69 74 0a 2a 2a 20 63 6f 6e 76  ords, it.** conv
18a6e 65 72 74 73 20 61 6e 20 4d 45 4d 5f 45 70 68 65  erts an MEM_Ephe
18a6f 6d 20 73 74 72 69 6e 67 20 69 6e 74 6f 20 61 6e  m string into an
18a70 20 4d 45 4d 5f 44 79 6e 20 73 74 72 69 6e 67 2e   MEM_Dyn string.
18a71 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 44 65 65 70  .*/.#define Deep
18a72 68 65 6d 65 72 61 6c 69 7a 65 28 50 29 20 5c 0a  hemeralize(P) \.
18a73 20 20 20 69 66 28 20 28 28 50 29 2d 3e 66 6c 61     if( ((P)->fla
18a74 67 73 26 4d 45 4d 5f 45 70 68 65 6d 29 21 3d 30  gs&MEM_Ephem)!=0
18a75 20 5c 0a 20 20 20 20 20 20 20 26 26 20 73 71 6c   \.       && sql
18a76 69 74 65 33 56 64 62 65 4d 65 6d 4d 61 6b 65 57  ite3VdbeMemMakeW
18a77 72 69 74 65 61 62 6c 65 28 50 29 20 29 7b 20 67  riteable(P) ){ g
18a78 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 7d 0a 0a 2f 2a  oto no_mem;}../*
18a79 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65 33  .** Call sqlite3
18a7a 56 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f  VdbeMemExpandBlo
18a7b 62 28 29 20 6f 6e 20 74 68 65 20 73 75 70 70 6c  b() on the suppl
18a7c 69 65 64 20 76 61 6c 75 65 20 28 74 79 70 65 20  ied value (type 
18a7d 4d 65 6d 2a 29 0a 2a 2a 20 50 20 69 66 20 72 65  Mem*).** P if re
18a7e 71 75 69 72 65 64 2e 0a 2a 2f 0a 23 64 65 66 69  quired..*/.#defi
18a7f 6e 65 20 45 78 70 61 6e 64 42 6c 6f 62 28 50 29  ne ExpandBlob(P)
18a80 20 28 28 28 50 29 2d 3e 66 6c 61 67 73 26 4d 45   (((P)->flags&ME
18a81 4d 5f 5a 65 72 6f 29 3f 73 71 6c 69 74 65 33 56  M_Zero)?sqlite3V
18a82 64 62 65 4d 65 6d 45 78 70 61 6e 64 42 6c 6f 62  dbeMemExpandBlob
18a83 28 50 29 3a 30 29 0a 0a 2f 2a 0a 2a 2a 20 41 72  (P):0)../*.** Ar
18a84 67 75 6d 65 6e 74 20 70 4d 65 6d 20 70 6f 69 6e  gument pMem poin
18a85 74 73 20 61 74 20 61 20 72 65 67 69 73 74 65 72  ts at a register
18a86 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20 70 61   that will be pa
18a87 73 73 65 64 20 74 6f 20 61 0a 2a 2a 20 75 73 65  ssed to a.** use
18a88 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
18a89 6f 6e 20 6f 72 20 72 65 74 75 72 6e 65 64 20 74  on or returned t
18a8a 6f 20 74 68 65 20 75 73 65 72 20 61 73 20 74 68  o the user as th
18a8b 65 20 72 65 73 75 6c 74 20 6f 66 20 61 20 71 75  e result of a qu
18a8c 65 72 79 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f  ery..** The seco
18a8d 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 27 64 62  nd argument, 'db
18a8e 5f 65 6e 63 27 20 69 73 20 74 68 65 20 74 65 78  _enc' is the tex
18a8f 74 20 65 6e 63 6f 64 69 6e 67 20 75 73 65 64 20  t encoding used 
18a90 62 79 20 74 68 65 20 76 64 62 65 20 66 6f 72 0a  by the vdbe for.
18a91 2a 2a 20 72 65 67 69 73 74 65 72 20 76 61 72 69  ** register vari
18a92 61 62 6c 65 73 2e 20 20 54 68 69 73 20 72 6f 75  ables.  This rou
18a93 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 4d  tine sets the pM
18a94 65 6d 2d 3e 65 6e 63 20 61 6e 64 20 70 4d 65 6d  em->enc and pMem
18a95 2d 3e 74 79 70 65 0a 2a 2a 20 76 61 72 69 61 62  ->type.** variab
18a96 6c 65 73 20 75 73 65 64 20 62 79 20 74 68 65 20  les used by the 
18a97 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 2a 28  sqlite3_value_*(
18a98 29 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2f 0a 23  ) routines..*/.#
18a99 64 65 66 69 6e 65 20 73 74 6f 72 65 54 79 70 65  define storeType
18a9a 49 6e 66 6f 28 41 2c 42 29 20 5f 73 74 6f 72 65  Info(A,B) _store
18a9b 54 79 70 65 49 6e 66 6f 28 41 29 0a 73 74 61 74  TypeInfo(A).stat
18a9c 69 63 20 76 6f 69 64 20 5f 73 74 6f 72 65 54 79  ic void _storeTy
18a9d 70 65 49 6e 66 6f 28 4d 65 6d 20 2a 70 4d 65 6d  peInfo(Mem *pMem
18a9e 29 7b 0a 20 20 69 6e 74 20 66 6c 61 67 73 20 3d  ){.  int flags =
18a9f 20 70 4d 65 6d 2d 3e 66 6c 61 67 73 3b 0a 20 20   pMem->flags;.  
18aa0 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d 5f  if( flags & MEM_
18aa1 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 70 4d 65 6d  Null ){.    pMem
18aa2 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
18aa3 4e 55 4c 4c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65  NULL;.  }.  else
18aa4 20 69 66 28 20 66 6c 61 67 73 20 26 20 4d 45 4d   if( flags & MEM
18aa5 5f 49 6e 74 20 29 7b 0a 20 20 20 20 70 4d 65 6d  _Int ){.    pMem
18aa6 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54 45 5f  ->type = SQLITE_
18aa7 49 4e 54 45 47 45 52 3b 0a 20 20 7d 0a 20 20 65  INTEGER;.  }.  e
18aa8 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20 26 20  lse if( flags & 
18aa9 4d 45 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20  MEM_Real ){.    
18aaa 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51 4c  pMem->type = SQL
18aab 49 54 45 5f 46 4c 4f 41 54 3b 0a 20 20 7d 0a 20  ITE_FLOAT;.  }. 
18aac 20 65 6c 73 65 20 69 66 28 20 66 6c 61 67 73 20   else if( flags 
18aad 26 20 4d 45 4d 5f 53 74 72 20 29 7b 0a 20 20 20  & MEM_Str ){.   
18aae 20 70 4d 65 6d 2d 3e 74 79 70 65 20 3d 20 53 51   pMem->type = SQ
18aaf 4c 49 54 45 5f 54 45 58 54 3b 0a 20 20 7d 65 6c  LITE_TEXT;.  }el
18ab0 73 65 7b 0a 20 20 20 20 70 4d 65 6d 2d 3e 74 79  se{.    pMem->ty
18ab1 70 65 20 3d 20 53 51 4c 49 54 45 5f 42 4c 4f 42  pe = SQLITE_BLOB
18ab2 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50  ;.  }.}../*.** P
18ab3 72 6f 70 65 72 74 69 65 73 20 6f 66 20 6f 70 63  roperties of opc
18ab4 6f 64 65 73 2e 20 20 54 68 65 20 4f 50 46 4c 47  odes.  The OPFLG
18ab5 5f 49 4e 49 54 49 41 4c 49 5a 45 52 20 6d 61 63  _INITIALIZER mac
18ab6 72 6f 20 69 73 0a 2a 2a 20 63 72 65 61 74 65 64  ro is.** created
18ab7 20 62 79 20 6d 6b 6f 70 63 6f 64 65 68 2e 61 77   by mkopcodeh.aw
18ab8 6b 20 64 75 72 69 6e 67 20 63 6f 6d 70 69 6c 61  k during compila
18ab9 74 69 6f 6e 2e 20 20 44 61 74 61 20 69 73 20 6f  tion.  Data is o
18aba 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72 6f 6d 20  btained.** from 
18abb 74 68 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 6c  the comments fol
18abc 6c 6f 77 69 6e 67 20 74 68 65 20 22 63 61 73 65  lowing the "case
18abd 20 4f 50 5f 78 78 78 78 3a 22 20 73 74 61 74 65   OP_xxxx:" state
18abe 6d 65 6e 74 73 20 69 6e 0a 2a 2a 20 74 68 69 73  ments in.** this
18abf 20 66 69 6c 65 2e 20 20 0a 2a 2f 0a 73 74 61 74   file.  .*/.stat
18ac0 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
18ac1 64 20 63 68 61 72 20 6f 70 63 6f 64 65 50 72 6f  d char opcodePro
18ac2 70 65 72 74 79 5b 5d 20 3d 20 4f 50 46 4c 47 5f  perty[] = OPFLG_
18ac3 49 4e 49 54 49 41 4c 49 5a 45 52 3b 0a 0a 2f 2a  INITIALIZER;../*
18ac4 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
18ac5 69 66 20 61 6e 20 6f 70 63 6f 64 65 20 68 61 73  if an opcode has
18ac6 20 61 6e 79 20 6f 66 20 74 68 65 20 4f 50 46 4c   any of the OPFL
18ac7 47 5f 78 78 78 20 70 72 6f 70 65 72 74 69 65 73  G_xxx properties
18ac8 0a 2a 2a 20 73 70 65 63 69 66 69 65 64 20 62 79  .** specified by
18ac9 20 6d 61 73 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45   mask..*/.SQLITE
18aca 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
18acb 69 74 65 33 56 64 62 65 4f 70 63 6f 64 65 48 61  ite3VdbeOpcodeHa
18acc 73 50 72 6f 70 65 72 74 79 28 69 6e 74 20 6f 70  sProperty(int op
18acd 63 6f 64 65 2c 20 69 6e 74 20 6d 61 73 6b 29 7b  code, int mask){
18ace 0a 20 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64  .  assert( opcod
18acf 65 3e 30 20 26 26 20 6f 70 63 6f 64 65 3c 28 69  e>0 && opcode<(i
18ad0 6e 74 29 73 69 7a 65 6f 66 28 6f 70 63 6f 64 65  nt)sizeof(opcode
18ad1 50 72 6f 70 65 72 74 79 29 20 29 3b 0a 20 20 72  Property) );.  r
18ad2 65 74 75 72 6e 20 28 6f 70 63 6f 64 65 50 72 6f  eturn (opcodePro
18ad3 70 65 72 74 79 5b 6f 70 63 6f 64 65 5d 26 6d 61  perty[opcode]&ma
18ad4 73 6b 29 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  sk)!=0;.}../*.**
18ad5 20 41 6c 6c 6f 63 61 74 65 20 56 64 62 65 43 75   Allocate VdbeCu
18ad6 72 73 6f 72 20 6e 75 6d 62 65 72 20 69 43 75 72  rsor number iCur
18ad7 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e  .  Return a poin
18ad8 74 65 72 20 74 6f 20 69 74 2e 20 20 52 65 74 75  ter to it.  Retu
18ad9 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 77 65  rn NULL.** if we
18ada 20 72 75 6e 20 6f 75 74 20 6f 66 20 6d 65 6d 6f   run out of memo
18adb 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 56 64  ry..*/.static Vd
18adc 62 65 43 75 72 73 6f 72 20 2a 61 6c 6c 6f 63 61  beCursor *alloca
18add 74 65 43 75 72 73 6f 72 28 0a 20 20 56 64 62 65  teCursor(.  Vdbe
18ade 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
18adf 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
18ae0 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 69 6e   machine */.  in
18ae1 74 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20  t iCur,         
18ae2 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
18ae3 74 68 65 20 6e 65 77 20 56 64 62 65 43 75 72 73  the new VdbeCurs
18ae4 6f 72 20 2a 2f 0a 20 20 4f 70 20 2a 70 4f 70 2c  or */.  Op *pOp,
18ae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
18ae6 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20   */.  int iDb,  
18ae7 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 2a              /* *
18ae8 2f 0a 20 20 69 6e 74 20 69 73 42 74 72 65 65 43  /.  int isBtreeC
18ae9 75 72 73 6f 72 20 20 20 20 20 2f 2a 20 2a 2f 0a  ursor     /* */.
18aea 29 7b 0a 20 20 2f 2a 20 46 69 6e 64 20 74 68 65  ){.  /* Find the
18aeb 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61   memory cell tha
18aec 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20 74  t will be used t
18aed 6f 20 73 74 6f 72 65 20 74 68 65 20 62 6c 6f 62  o store the blob
18aee 20 6f 66 20 6d 65 6d 6f 72 79 0a 20 20 2a 2a 20   of memory.  ** 
18aef 72 65 71 75 69 72 65 64 20 66 6f 72 20 74 68 69  required for thi
18af0 73 20 56 64 62 65 43 75 72 73 6f 72 20 73 74 72  s VdbeCursor str
18af1 75 63 74 75 72 65 2e 20 49 74 20 69 73 20 63 6f  ucture. It is co
18af2 6e 76 65 6e 69 65 6e 74 20 74 6f 20 75 73 65 20  nvenient to use 
18af3 61 20 0a 20 20 2a 2a 20 76 64 62 65 20 6d 65 6d  a .  ** vdbe mem
18af4 6f 72 79 20 63 65 6c 6c 20 74 6f 20 6d 61 6e 61  ory cell to mana
18af5 67 65 20 74 68 65 20 6d 65 6d 6f 72 79 20 61 6c  ge the memory al
18af6 6c 6f 63 61 74 69 6f 6e 20 72 65 71 75 69 72 65  location require
18af7 64 20 66 6f 72 20 61 0a 20 20 2a 2a 20 56 64 62  d for a.  ** Vdb
18af8 65 43 75 72 73 6f 72 20 73 74 72 75 63 74 75 72  eCursor structur
18af9 65 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77  e for the follow
18afa 69 6e 67 20 72 65 61 73 6f 6e 73 3a 0a 20 20 2a  ing reasons:.  *
18afb 2a 0a 20 20 2a 2a 20 20 20 2a 20 53 6f 6d 65 74  *.  **   * Somet
18afc 69 6d 65 73 20 63 75 72 73 6f 72 20 6e 75 6d 62  imes cursor numb
18afd 65 72 73 20 61 72 65 20 75 73 65 64 20 66 6f 72  ers are used for
18afe 20 61 20 63 6f 75 70 6c 65 20 6f 66 20 64 69 66   a couple of dif
18aff 66 65 72 65 6e 74 0a 20 20 2a 2a 20 20 20 20 20  ferent.  **     
18b00 70 75 72 70 6f 73 65 73 20 69 6e 20 61 20 76 64  purposes in a vd
18b01 62 65 20 70 72 6f 67 72 61 6d 2e 20 54 68 65 20  be program. The 
18b02 64 69 66 66 65 72 65 6e 74 20 75 73 65 73 20 6d  different uses m
18b03 69 67 68 74 20 72 65 71 75 69 72 65 0a 20 20 2a  ight require.  *
18b04 2a 20 20 20 20 20 64 69 66 66 65 72 65 6e 74 20  *     different 
18b05 73 69 7a 65 64 20 61 6c 6c 6f 63 61 74 69 6f 6e  sized allocation
18b06 73 2e 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20  s. Memory cells 
18b07 70 72 6f 76 69 64 65 20 67 72 6f 77 61 62 6c 65  provide growable
18b08 0a 20 20 2a 2a 20 20 20 20 20 61 6c 6c 6f 63 61  .  **     alloca
18b09 74 69 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  tions..  **.  **
18b0a 20 20 20 2a 20 57 68 65 6e 20 75 73 69 6e 67 20     * When using 
18b0b 45 4e 41 42 4c 45 5f 4d 45 4d 4f 52 59 5f 4d 41  ENABLE_MEMORY_MA
18b0c 4e 41 47 45 4d 45 4e 54 2c 20 6d 65 6d 6f 72 79  NAGEMENT, memory
18b0d 20 63 65 6c 6c 20 62 75 66 66 65 72 73 20 63 61   cell buffers ca
18b0e 6e 0a 20 20 2a 2a 20 20 20 20 20 62 65 20 66 72  n.  **     be fr
18b0f 65 65 64 20 6c 61 7a 69 6c 79 20 76 69 61 20 74  eed lazily via t
18b10 68 65 20 73 71 6c 69 74 65 33 5f 72 65 6c 65 61  he sqlite3_relea
18b11 73 65 5f 6d 65 6d 6f 72 79 28 29 20 41 50 49 2e  se_memory() API.
18b12 20 54 68 69 73 0a 20 20 2a 2a 20 20 20 20 20 6d   This.  **     m
18b13 69 6e 69 6d 69 7a 65 73 20 74 68 65 20 6e 75 6d  inimizes the num
18b14 62 65 72 20 6f 66 20 6d 61 6c 6c 6f 63 20 63 61  ber of malloc ca
18b15 6c 6c 73 20 6d 61 64 65 20 62 79 20 74 68 65 20  lls made by the 
18b16 73 79 73 74 65 6d 2e 0a 20 20 2a 2a 0a 20 20 2a  system..  **.  *
18b17 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 66  * Memory cells f
18b18 6f 72 20 63 75 72 73 6f 72 73 20 61 72 65 20 61  or cursors are a
18b19 6c 6c 6f 63 61 74 65 64 20 61 74 20 74 68 65 20  llocated at the 
18b1a 74 6f 70 20 6f 66 20 74 68 65 20 61 64 64 72 65  top of the addre
18b1b 73 73 0a 20 20 2a 2a 20 73 70 61 63 65 2e 20 4d  ss.  ** space. M
18b1c 65 6d 6f 72 79 20 63 65 6c 6c 20 28 70 2d 3e 6e  emory cell (p->n
18b1d 4d 65 6d 29 20 63 6f 72 72 65 73 70 6f 6e 64 73  Mem) corresponds
18b1e 20 74 6f 20 63 75 72 73 6f 72 20 30 2e 20 53 70   to cursor 0. Sp
18b1f 61 63 65 20 66 6f 72 0a 20 20 2a 2a 20 63 75 72  ace for.  ** cur
18b20 73 6f 72 20 31 20 69 73 20 6d 61 6e 61 67 65 64  sor 1 is managed
18b21 20 62 79 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   by memory cell 
18b22 28 70 2d 3e 6e 4d 65 6d 2d 31 29 2c 20 65 74 63  (p->nMem-1), etc
18b23 2e 0a 20 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4d  ..  */.  Mem *pM
18b24 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 2d  em = &p->aMem[p-
18b25 3e 6e 4d 65 6d 2d 69 43 75 72 5d 3b 0a 0a 20 20  >nMem-iCur];..  
18b26 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 56 64 62  int nByte;.  Vdb
18b27 65 43 75 72 73 6f 72 20 2a 70 43 78 20 3d 20 30  eCursor *pCx = 0
18b28 3b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 70  ;.  /* If the op
18b29 63 6f 64 65 20 6f 66 20 70 4f 70 20 69 73 20 4f  code of pOp is O
18b2a 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c  P_SetNumColumns,
18b2b 20 74 68 65 6e 20 70 4f 70 2d 3e 70 32 20 63 6f   then pOp->p2 co
18b2c 6e 74 61 69 6e 73 0a 20 20 2a 2a 20 74 68 65 20  ntains.  ** the 
18b2d 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64 73  number of fields
18b2e 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 73 20   in the records 
18b2f 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65  contained in the
18b30 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a 20 69   table or.  ** i
18b31 6e 64 65 78 20 62 65 69 6e 67 20 6f 70 65 6e 65  ndex being opene
18b32 64 2e 20 55 73 65 20 74 68 69 73 20 74 6f 20 72  d. Use this to r
18b33 65 73 65 72 76 65 20 73 70 61 63 65 20 66 6f 72  eserve space for
18b34 20 74 68 65 20 0a 20 20 2a 2a 20 56 64 62 65 43   the .  ** VdbeC
18b35 75 72 73 6f 72 2e 61 54 79 70 65 5b 5d 20 61 72  ursor.aType[] ar
18b36 72 61 79 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20  ray..  */.  int 
18b37 6e 46 69 65 6c 64 20 3d 20 30 3b 0a 20 20 69 66  nField = 0;.  if
18b38 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
18b39 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 20  P_SetNumColumns 
18b3a 7c 7c 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  || pOp->opcode==
18b3b 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
18b3c 20 29 7b 0a 20 20 20 20 6e 46 69 65 6c 64 20 3d   ){.    nField =
18b3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 7d 0a 20 20   pOp->p2;.  }.  
18b3e 6e 42 79 74 65 20 3d 20 0a 20 20 20 20 20 20 73  nByte = .      s
18b3f 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
18b40 29 20 2b 20 0a 20 20 20 20 20 20 28 69 73 42 74  ) + .      (isBt
18b41 72 65 65 43 75 72 73 6f 72 3f 73 71 6c 69 74 65  reeCursor?sqlite
18b42 33 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65  3BtreeCursorSize
18b43 28 29 3a 30 29 20 2b 20 0a 20 20 20 20 20 20 32  ():0) + .      2
18b44 2a 6e 46 69 65 6c 64 2a 73 69 7a 65 6f 66 28 75  *nField*sizeof(u
18b45 33 32 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  32);..  assert( 
18b46 69 43 75 72 3c 70 2d 3e 6e 43 75 72 73 6f 72 20  iCur<p->nCursor 
18b47 29 3b 0a 20 20 69 66 28 20 70 2d 3e 61 70 43 73  );.  if( p->apCs
18b48 72 5b 69 43 75 72 5d 20 29 7b 0a 20 20 20 20 73  r[iCur] ){.    s
18b49 71 6c 69 74 65 33 56 64 62 65 46 72 65 65 43 75  qlite3VdbeFreeCu
18b4a 72 73 6f 72 28 70 2c 20 70 2d 3e 61 70 43 73 72  rsor(p, p->apCsr
18b4b 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 70 2d 3e  [iCur]);.    p->
18b4c 61 70 43 73 72 5b 69 43 75 72 5d 20 3d 20 30 3b  apCsr[iCur] = 0;
18b4d 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54  .  }.  if( SQLIT
18b4e 45 5f 4f 4b 3d 3d 73 71 6c 69 74 65 33 56 64 62  E_OK==sqlite3Vdb
18b4f 65 4d 65 6d 47 72 6f 77 28 70 4d 65 6d 2c 20 6e  eMemGrow(pMem, n
18b50 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Byte, 0) ){.    
18b51 70 2d 3e 61 70 43 73 72 5b 69 43 75 72 5d 20 3d  p->apCsr[iCur] =
18b52 20 70 43 78 20 3d 20 28 56 64 62 65 43 75 72 73   pCx = (VdbeCurs
18b53 6f 72 2a 29 70 4d 65 6d 2d 3e 7a 3b 0a 20 20 20  or*)pMem->z;.   
18b54 20 6d 65 6d 73 65 74 28 70 4d 65 6d 2d 3e 7a 2c   memset(pMem->z,
18b55 20 30 2c 20 6e 42 79 74 65 29 3b 0a 20 20 20 20   0, nByte);.    
18b56 70 43 78 2d 3e 69 44 62 20 3d 20 69 44 62 3b 0a  pCx->iDb = iDb;.
18b57 20 20 20 20 70 43 78 2d 3e 6e 46 69 65 6c 64 20      pCx->nField 
18b58 3d 20 6e 46 69 65 6c 64 3b 0a 20 20 20 20 69 66  = nField;.    if
18b59 28 20 6e 46 69 65 6c 64 20 29 7b 0a 20 20 20 20  ( nField ){.    
18b5a 20 20 70 43 78 2d 3e 61 54 79 70 65 20 3d 20 28    pCx->aType = (
18b5b 75 33 32 20 2a 29 26 70 4d 65 6d 2d 3e 7a 5b 73  u32 *)&pMem->z[s
18b5c 69 7a 65 6f 66 28 56 64 62 65 43 75 72 73 6f 72  izeof(VdbeCursor
18b5d 29 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  )];.    }.    if
18b5e 28 20 69 73 42 74 72 65 65 43 75 72 73 6f 72 20  ( isBtreeCursor 
18b5f 29 7b 0a 20 20 20 20 20 20 70 43 78 2d 3e 70 43  ){.      pCx->pC
18b60 75 72 73 6f 72 20 3d 20 28 42 74 43 75 72 73 6f  ursor = (BtCurso
18b61 72 2a 29 0a 20 20 20 20 20 20 20 20 20 20 26 70  r*).          &p
18b62 4d 65 6d 2d 3e 7a 5b 73 69 7a 65 6f 66 28 56 64  Mem->z[sizeof(Vd
18b63 62 65 43 75 72 73 6f 72 29 2b 32 2a 6e 46 69 65  beCursor)+2*nFie
18b64 6c 64 2a 73 69 7a 65 6f 66 28 75 33 32 29 5d 3b  ld*sizeof(u32)];
18b65 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
18b66 75 72 6e 20 70 43 78 3b 0a 7d 0a 0a 2f 2a 0a 2a  urn pCx;.}../*.*
18b67 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72 74  * Try to convert
18b68 20 61 20 76 61 6c 75 65 20 69 6e 74 6f 20 61 20   a value into a 
18b69 6e 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e  numeric represen
18b6a 74 61 74 69 6f 6e 20 69 66 20 77 65 20 63 61 6e  tation if we can
18b6b 0a 2a 2a 20 64 6f 20 73 6f 20 77 69 74 68 6f 75  .** do so withou
18b6c 74 20 6c 6f 73 73 20 6f 66 20 69 6e 66 6f 72 6d  t loss of inform
18b6d 61 74 69 6f 6e 2e 20 20 49 6e 20 6f 74 68 65 72  ation.  In other
18b6e 20 77 6f 72 64 73 2c 20 69 66 20 74 68 65 20 73   words, if the s
18b6f 74 72 69 6e 67 0a 2a 2a 20 6c 6f 6f 6b 73 20 6c  tring.** looks l
18b70 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 63 6f  ike a number, co
18b71 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 61 20  nvert it into a 
18b72 6e 75 6d 62 65 72 2e 20 20 49 66 20 69 74 20 64  number.  If it d
18b73 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6c 6f 6f 6b 20  oes not.** look 
18b74 6c 69 6b 65 20 61 20 6e 75 6d 62 65 72 2c 20 6c  like a number, l
18b75 65 61 76 65 20 69 74 20 61 6c 6f 6e 65 2e 0a 2a  eave it alone..*
18b76 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70  /.static void ap
18b77 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69 6e 69  plyNumericAffini
18b78 74 79 28 4d 65 6d 20 2a 70 52 65 63 29 7b 0a 20  ty(Mem *pRec){. 
18b79 20 69 66 28 20 28 70 52 65 63 2d 3e 66 6c 61 67   if( (pRec->flag
18b7a 73 20 26 20 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45  s & (MEM_Real|ME
18b7b 4d 5f 49 6e 74 29 29 3d 3d 30 20 29 7b 0a 20 20  M_Int))==0 ){.  
18b7c 20 20 69 6e 74 20 72 65 61 6c 6e 75 6d 3b 0a 20    int realnum;. 
18b7d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
18b7e 6d 4e 75 6c 54 65 72 6d 69 6e 61 74 65 28 70 52  mNulTerminate(pR
18b7f 65 63 29 3b 0a 20 20 20 20 69 66 28 20 28 70 52  ec);.    if( (pR
18b80 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f 53 74  ec->flags&MEM_St
18b81 72 29 0a 20 20 20 20 20 20 20 20 20 26 26 20 73  r).         && s
18b82 71 6c 69 74 65 33 49 73 4e 75 6d 62 65 72 28 70  qlite3IsNumber(p
18b83 52 65 63 2d 3e 7a 2c 20 26 72 65 61 6c 6e 75 6d  Rec->z, &realnum
18b84 2c 20 70 52 65 63 2d 3e 65 6e 63 29 20 29 7b 0a  , pRec->enc) ){.
18b85 20 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b        i64 value;
18b86 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
18b87 62 65 43 68 61 6e 67 65 45 6e 63 6f 64 69 6e 67  beChangeEncoding
18b88 28 70 52 65 63 2c 20 53 51 4c 49 54 45 5f 55 54  (pRec, SQLITE_UT
18b89 46 38 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  F8);.      if( !
18b8a 72 65 61 6c 6e 75 6d 20 26 26 20 73 71 6c 69 74  realnum && sqlit
18b8b 65 33 41 74 6f 69 36 34 28 70 52 65 63 2d 3e 7a  e3Atoi64(pRec->z
18b8c 2c 20 26 76 61 6c 75 65 29 20 29 7b 0a 20 20 20  , &value) ){.   
18b8d 20 20 20 20 20 70 52 65 63 2d 3e 75 2e 69 20 3d       pRec->u.i =
18b8e 20 76 61 6c 75 65 3b 0a 20 20 20 20 20 20 20 20   value;.        
18b8f 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
18b90 52 65 63 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Rec, MEM_Int);. 
18b91 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
18b92 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d      sqlite3VdbeM
18b93 65 6d 52 65 61 6c 69 66 79 28 70 52 65 63 29 3b  emRealify(pRec);
18b94 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
18b95 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f 63   }.}../*.** Proc
18b96 65 73 73 69 6e 67 20 69 73 20 64 65 74 65 72 6d  essing is determ
18b97 69 6e 65 20 62 79 20 74 68 65 20 61 66 66 69 6e  ine by the affin
18b98 69 74 79 20 70 61 72 61 6d 65 74 65 72 3a 0a 2a  ity parameter:.*
18b99 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f  *.** SQLITE_AFF_
18b9a 49 4e 54 45 47 45 52 3a 0a 2a 2a 20 53 51 4c 49  INTEGER:.** SQLI
18b9b 54 45 5f 41 46 46 5f 52 45 41 4c 3a 0a 2a 2a 20  TE_AFF_REAL:.** 
18b9c 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
18b9d 49 43 3a 0a 2a 2a 20 20 20 20 54 72 79 20 74 6f  IC:.**    Try to
18b9e 20 63 6f 6e 76 65 72 74 20 70 52 65 63 20 74 6f   convert pRec to
18b9f 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 70 72   an integer repr
18ba0 65 73 65 6e 74 61 74 69 6f 6e 20 6f 72 20 61 20  esentation or a 
18ba1 0a 2a 2a 20 20 20 20 66 6c 6f 61 74 69 6e 67 2d  .**    floating-
18ba2 70 6f 69 6e 74 20 72 65 70 72 65 73 65 6e 74 61  point representa
18ba3 74 69 6f 6e 20 69 66 20 61 6e 20 69 6e 74 65 67  tion if an integ
18ba4 65 72 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f  er representatio
18ba5 6e 0a 2a 2a 20 20 20 20 69 73 20 6e 6f 74 20 70  n.**    is not p
18ba6 6f 73 73 69 62 6c 65 2e 20 20 4e 6f 74 65 20 74  ossible.  Note t
18ba7 68 61 74 20 74 68 65 20 69 6e 74 65 67 65 72 20  hat the integer 
18ba8 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 69  representation i
18ba9 73 0a 2a 2a 20 20 20 20 61 6c 77 61 79 73 20 70  s.**    always p
18baa 72 65 66 65 72 72 65 64 2c 20 65 76 65 6e 20 69  referred, even i
18bab 66 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 69  f the affinity i
18bac 73 20 52 45 41 4c 2c 20 62 65 63 61 75 73 65 0a  s REAL, because.
18bad 2a 2a 20 20 20 20 61 6e 20 69 6e 74 65 67 65 72  **    an integer
18bae 20 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20   representation 
18baf 69 73 20 6d 6f 72 65 20 73 70 61 63 65 20 65 66  is more space ef
18bb0 66 69 63 69 65 6e 74 20 6f 6e 20 64 69 73 6b 2e  ficient on disk.
18bb1 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46  .**.** SQLITE_AF
18bb2 46 5f 54 45 58 54 3a 0a 2a 2a 20 20 20 20 43 6f  F_TEXT:.**    Co
18bb3 6e 76 65 72 74 20 70 52 65 63 20 74 6f 20 61 20  nvert pRec to a 
18bb4 74 65 78 74 20 72 65 70 72 65 73 65 6e 74 61 74  text representat
18bb5 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 49 54  ion..**.** SQLIT
18bb6 45 5f 41 46 46 5f 4e 4f 4e 45 3a 0a 2a 2a 20 20  E_AFF_NONE:.**  
18bb7 20 20 4e 6f 2d 6f 70 2e 20 20 70 52 65 63 20 69    No-op.  pRec i
18bb8 73 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a  s unchanged..*/.
18bb9 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 6c  static void appl
18bba 79 41 66 66 69 6e 69 74 79 28 0a 20 20 4d 65 6d  yAffinity(.  Mem
18bbb 20 2a 70 52 65 63 2c 20 20 20 20 20 20 20 20 20   *pRec,         
18bbc 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 74 6f   /* The value to
18bbd 20 61 70 70 6c 79 20 61 66 66 69 6e 69 74 79 20   apply affinity 
18bbe 74 6f 20 2a 2f 0a 20 20 63 68 61 72 20 61 66 66  to */.  char aff
18bbf 69 6e 69 74 79 2c 20 20 20 20 20 20 2f 2a 20 54  inity,      /* T
18bc0 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f 20 62  he affinity to b
18bc1 65 20 61 70 70 6c 69 65 64 20 2a 2f 0a 20 20 75  e applied */.  u
18bc2 38 20 65 6e 63 20 20 20 20 20 20 20 20 20 20 20  8 enc           
18bc3 20 20 20 2f 2a 20 55 73 65 20 74 68 69 73 20 74     /* Use this t
18bc4 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a  ext encoding */.
18bc5 29 7b 0a 20 20 69 66 28 20 61 66 66 69 6e 69 74  ){.  if( affinit
18bc6 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 54 45  y==SQLITE_AFF_TE
18bc7 58 54 20 29 7b 0a 20 20 20 20 2f 2a 20 4f 6e 6c  XT ){.    /* Onl
18bc8 79 20 61 74 74 65 6d 70 74 20 74 68 65 20 63 6f  y attempt the co
18bc9 6e 76 65 72 73 69 6f 6e 20 74 6f 20 54 45 58 54  nversion to TEXT
18bca 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e 20   if there is an 
18bcb 69 6e 74 65 67 65 72 20 6f 72 20 72 65 61 6c 0a  integer or real.
18bcc 20 20 20 20 2a 2a 20 72 65 70 72 65 73 65 6e 74      ** represent
18bcd 61 74 69 6f 6e 20 28 62 6c 6f 62 20 61 6e 64 20  ation (blob and 
18bce 4e 55 4c 4c 20 64 6f 20 6e 6f 74 20 67 65 74 20  NULL do not get 
18bcf 63 6f 6e 76 65 72 74 65 64 29 20 62 75 74 20 6e  converted) but n
18bd0 6f 20 73 74 72 69 6e 67 0a 20 20 20 20 2a 2a 20  o string.    ** 
18bd1 72 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 2e 0a  representation..
18bd2 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 30      */.    if( 0
18bd3 3d 3d 28 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d  ==(pRec->flags&M
18bd4 45 4d 5f 53 74 72 29 20 26 26 20 28 70 52 65 63  EM_Str) && (pRec
18bd5 2d 3e 66 6c 61 67 73 26 28 4d 45 4d 5f 52 65 61  ->flags&(MEM_Rea
18bd6 6c 7c 4d 45 4d 5f 49 6e 74 29 29 20 29 7b 0a 20  l|MEM_Int)) ){. 
18bd7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
18bd8 4d 65 6d 53 74 72 69 6e 67 69 66 79 28 70 52 65  MemStringify(pRe
18bd9 63 2c 20 65 6e 63 29 3b 0a 20 20 20 20 7d 0a 20  c, enc);.    }. 
18bda 20 20 20 70 52 65 63 2d 3e 66 6c 61 67 73 20 26     pRec->flags &
18bdb 3d 20 7e 28 4d 45 4d 5f 52 65 61 6c 7c 4d 45 4d  = ~(MEM_Real|MEM
18bdc 5f 49 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69  _Int);.  }else i
18bdd 66 28 20 61 66 66 69 6e 69 74 79 21 3d 53 51 4c  f( affinity!=SQL
18bde 49 54 45 5f 41 46 46 5f 4e 4f 4e 45 20 29 7b 0a  ITE_AFF_NONE ){.
18bdf 20 20 20 20 61 73 73 65 72 74 28 20 61 66 66 69      assert( affi
18be0 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
18be1 5f 49 4e 54 45 47 45 52 20 7c 7c 20 61 66 66 69  _INTEGER || affi
18be2 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
18be3 5f 52 45 41 4c 0a 20 20 20 20 20 20 20 20 20 20  _REAL.          
18be4 20 20 20 7c 7c 20 61 66 66 69 6e 69 74 79 3d 3d     || affinity==
18be5 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52  SQLITE_AFF_NUMER
18be6 49 43 20 29 3b 0a 20 20 20 20 61 70 70 6c 79 4e  IC );.    applyN
18be7 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 70  umericAffinity(p
18be8 52 65 63 29 3b 0a 20 20 20 20 69 66 28 20 70 52  Rec);.    if( pR
18be9 65 63 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  ec->flags & MEM_
18bea 52 65 61 6c 20 29 7b 0a 20 20 20 20 20 20 73 71  Real ){.      sq
18beb 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65 72  lite3VdbeInteger
18bec 41 66 66 69 6e 69 74 79 28 70 52 65 63 29 3b 0a  Affinity(pRec);.
18bed 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
18bee 2a 2a 20 54 72 79 20 74 6f 20 63 6f 6e 76 65 72  ** Try to conver
18bef 74 20 74 68 65 20 74 79 70 65 20 6f 66 20 61 20  t the type of a 
18bf0 66 75 6e 63 74 69 6f 6e 20 61 72 67 75 6d 65 6e  function argumen
18bf1 74 20 6f 72 20 61 20 72 65 73 75 6c 74 20 63 6f  t or a result co
18bf2 6c 75 6d 6e 0a 2a 2a 20 69 6e 74 6f 20 61 20 6e  lumn.** into a n
18bf3 75 6d 65 72 69 63 20 72 65 70 72 65 73 65 6e 74  umeric represent
18bf4 61 74 69 6f 6e 2e 20 20 55 73 65 20 65 69 74 68  ation.  Use eith
18bf5 65 72 20 49 4e 54 45 47 45 52 20 6f 72 20 52 45  er INTEGER or RE
18bf6 41 4c 20 77 68 69 63 68 65 76 65 72 0a 2a 2a 20  AL whichever.** 
18bf7 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 20  is appropriate. 
18bf8 20 42 75 74 20 6f 6e 6c 79 20 64 6f 20 74 68 65   But only do the
18bf9 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 20 69   conversion if i
18bfa 74 20 69 73 20 70 6f 73 73 69 62 6c 65 20 77 69  t is possible wi
18bfb 74 68 6f 75 74 0a 2a 2a 20 6c 6f 73 73 20 6f 66  thout.** loss of
18bfc 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 6e 64   information and
18bfd 20 72 65 74 75 72 6e 20 74 68 65 20 72 65 76 69   return the revi
18bfe 73 65 64 20 74 79 70 65 20 6f 66 20 74 68 65 20  sed type of the 
18bff 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  argument..**.** 
18c00 54 68 69 73 20 69 73 20 61 6e 20 45 58 50 45 52  This is an EXPER
18c01 49 4d 45 4e 54 41 4c 20 61 70 69 20 61 6e 64 20  IMENTAL api and 
18c02 69 73 20 73 75 62 6a 65 63 74 20 74 6f 20 63 68  is subject to ch
18c03 61 6e 67 65 20 6f 72 20 72 65 6d 6f 76 61 6c 2e  ange or removal.
18c04 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
18c05 6e 74 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  nt sqlite3_value
18c06 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 28 73 71  _numeric_type(sq
18c07 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56 61  lite3_value *pVa
18c08 6c 29 7b 0a 20 20 4d 65 6d 20 2a 70 4d 65 6d 20  l){.  Mem *pMem 
18c09 3d 20 28 4d 65 6d 2a 29 70 56 61 6c 3b 0a 20 20  = (Mem*)pVal;.  
18c0a 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66 66 69  applyNumericAffi
18c0b 6e 69 74 79 28 70 4d 65 6d 29 3b 0a 20 20 73 74  nity(pMem);.  st
18c0c 6f 72 65 54 79 70 65 49 6e 66 6f 28 70 4d 65 6d  oreTypeInfo(pMem
18c0d 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  , 0);.  return p
18c0e 4d 65 6d 2d 3e 74 79 70 65 3b 0a 7d 0a 0a 2f 2a  Mem->type;.}../*
18c0f 0a 2a 2a 20 45 78 70 6f 72 74 65 64 20 76 65 72  .** Exported ver
18c10 73 69 6f 6e 20 6f 66 20 61 70 70 6c 79 41 66 66  sion of applyAff
18c11 69 6e 69 74 79 28 29 2e 20 54 68 69 73 20 6f 6e  inity(). This on
18c12 65 20 77 6f 72 6b 73 20 6f 6e 20 73 71 6c 69 74  e works on sqlit
18c13 65 33 5f 76 61 6c 75 65 2a 2c 20 0a 2a 2a 20 6e  e3_value*, .** n
18c14 6f 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  ot the internal 
18c15 4d 65 6d 2a 20 74 79 70 65 2e 0a 2a 2f 0a 53 51  Mem* type..*/.SQ
18c16 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
18c17 64 20 73 71 6c 69 74 65 33 56 61 6c 75 65 41 70  d sqlite3ValueAp
18c18 70 6c 79 41 66 66 69 6e 69 74 79 28 0a 20 20 73  plyAffinity(.  s
18c19 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
18c1a 61 6c 2c 20 0a 20 20 75 38 20 61 66 66 69 6e 69  al, .  u8 affini
18c1b 74 79 2c 20 0a 20 20 75 38 20 65 6e 63 0a 29 7b  ty, .  u8 enc.){
18c1c 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79  .  applyAffinity
18c1d 28 28 4d 65 6d 20 2a 29 70 56 61 6c 2c 20 61 66  ((Mem *)pVal, af
18c1e 66 69 6e 69 74 79 2c 20 65 6e 63 29 3b 0a 7d 0a  finity, enc);.}.
18c1f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
18c20 45 42 55 47 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  EBUG./*.** Write
18c21 20 61 20 6e 69 63 65 20 73 74 72 69 6e 67 20 72   a nice string r
18c22 65 70 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66  epresentation of
18c23 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
18c24 20 63 65 6c 6c 20 70 4d 65 6d 0a 2a 2a 20 69 6e   cell pMem.** in
18c25 74 6f 20 62 75 66 66 65 72 20 7a 42 75 66 2c 20  to buffer zBuf, 
18c26 6c 65 6e 67 74 68 20 6e 42 75 66 2e 0a 2a 2f 0a  length nBuf..*/.
18c27 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
18c28 6f 69 64 20 73 71 6c 69 74 65 33 56 64 62 65 4d  oid sqlite3VdbeM
18c29 65 6d 50 72 65 74 74 79 50 72 69 6e 74 28 4d 65  emPrettyPrint(Me
18c2a 6d 20 2a 70 4d 65 6d 2c 20 63 68 61 72 20 2a 7a  m *pMem, char *z
18c2b 42 75 66 29 7b 0a 20 20 63 68 61 72 20 2a 7a 43  Buf){.  char *zC
18c2c 73 72 20 3d 20 7a 42 75 66 3b 0a 20 20 69 6e 74  sr = zBuf;.  int
18c2d 20 66 20 3d 20 70 4d 65 6d 2d 3e 66 6c 61 67 73   f = pMem->flags
18c2e 3b 0a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73  ;..  static cons
18c2f 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 65 6e  t char *const en
18c30 63 6e 61 6d 65 73 5b 5d 20 3d 20 7b 22 28 58 29  cnames[] = {"(X)
18c31 22 2c 20 22 28 38 29 22 2c 20 22 28 31 36 4c 45  ", "(8)", "(16LE
18c32 29 22 2c 20 22 28 31 36 42 45 29 22 7d 3b 0a 0a  )", "(16BE)"};..
18c33 20 20 69 66 28 20 66 26 4d 45 4d 5f 42 6c 6f 62    if( f&MEM_Blob
18c34 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20   ){.    int i;. 
18c35 20 20 20 63 68 61 72 20 63 3b 0a 20 20 20 20 69     char c;.    i
18c36 66 28 20 66 20 26 20 4d 45 4d 5f 44 79 6e 20 29  f( f & MEM_Dyn )
18c37 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 7a 27 3b  {.      c = 'z';
18c38 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
18c39 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69 63 7c  f & (MEM_Static|
18c3a 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29  MEM_Ephem))==0 )
18c3b 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
18c3c 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63 20 29  f & MEM_Static )
18c3d 7b 0a 20 20 20 20 20 20 63 20 3d 20 27 74 27 3b  {.      c = 't';
18c3e 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28  .      assert( (
18c3f 66 20 26 20 28 4d 45 4d 5f 44 79 6e 7c 4d 45 4d  f & (MEM_Dyn|MEM
18c40 5f 45 70 68 65 6d 29 29 3d 3d 30 20 29 3b 0a 20  _Ephem))==0 );. 
18c41 20 20 20 7d 65 6c 73 65 20 69 66 28 20 66 20 26     }else if( f &
18c42 20 4d 45 4d 5f 45 70 68 65 6d 20 29 7b 0a 20 20   MEM_Ephem ){.  
18c43 20 20 20 20 63 20 3d 20 27 65 27 3b 0a 20 20 20      c = 'e';.   
18c44 20 20 20 61 73 73 65 72 74 28 20 28 66 20 26 20     assert( (f & 
18c45 28 4d 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f  (MEM_Static|MEM_
18c46 44 79 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20  Dyn))==0 );.    
18c47 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 20 3d  }else{.      c =
18c48 20 27 73 27 3b 0a 20 20 20 20 7d 0a 0a 20 20 20   's';.    }..   
18c49 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
18c4a 66 28 31 30 30 2c 20 7a 43 73 72 2c 20 22 25 63  f(100, zCsr, "%c
18c4b 22 2c 20 63 29 3b 0a 20 20 20 20 7a 43 73 72 20  ", c);.    zCsr 
18c4c 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72 29 3b  += strlen(zCsr);
18c4d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70  .    sqlite3_snp
18c4e 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72 2c  rintf(100, zCsr,
18c4f 20 22 25 64 5b 22 2c 20 70 4d 65 6d 2d 3e 6e 29   "%d[", pMem->n)
18c50 3b 0a 20 20 20 20 7a 43 73 72 20 2b 3d 20 73 74  ;.    zCsr += st
18c51 72 6c 65 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20  rlen(zCsr);.    
18c52 66 6f 72 28 69 3d 30 3b 20 69 3c 31 36 20 26 26  for(i=0; i<16 &&
18c53 20 69 3c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29   i<pMem->n; i++)
18c54 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
18c55 73 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43  snprintf(100, zC
18c56 73 72 2c 20 22 25 30 32 58 22 2c 20 28 28 69 6e  sr, "%02X", ((in
18c57 74 29 70 4d 65 6d 2d 3e 7a 5b 69 5d 20 26 20 30  t)pMem->z[i] & 0
18c58 78 46 46 29 29 3b 0a 20 20 20 20 20 20 7a 43 73  xFF));.      zCs
18c59 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72  r += strlen(zCsr
18c5a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f 72  );.    }.    for
18c5b 28 69 3d 30 3b 20 69 3c 31 36 20 26 26 20 69 3c  (i=0; i<16 && i<
18c5c 70 4d 65 6d 2d 3e 6e 3b 20 69 2b 2b 29 7b 0a 20  pMem->n; i++){. 
18c5d 20 20 20 20 20 63 68 61 72 20 7a 20 3d 20 70 4d       char z = pM
18c5e 65 6d 2d 3e 7a 5b 69 5d 3b 0a 20 20 20 20 20 20  em->z[i];.      
18c5f 69 66 28 20 7a 3c 33 32 20 7c 7c 20 7a 3e 31 32  if( z<32 || z>12
18c60 36 20 29 20 2a 7a 43 73 72 2b 2b 20 3d 20 27 2e  6 ) *zCsr++ = '.
18c61 27 3b 0a 20 20 20 20 20 20 65 6c 73 65 20 2a 7a  ';.      else *z
18c62 43 73 72 2b 2b 20 3d 20 7a 3b 0a 20 20 20 20 7d  Csr++ = z;.    }
18c63 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e  ..    sqlite3_sn
18c64 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73 72  printf(100, zCsr
18c65 2c 20 22 5d 25 73 22 2c 20 65 6e 63 6e 61 6d 65  , "]%s", encname
18c66 73 5b 70 4d 65 6d 2d 3e 65 6e 63 5d 29 3b 0a 20  s[pMem->enc]);. 
18c67 20 20 20 7a 43 73 72 20 2b 3d 20 73 74 72 6c 65     zCsr += strle
18c68 6e 28 7a 43 73 72 29 3b 0a 20 20 20 20 69 66 28  n(zCsr);.    if(
18c69 20 66 20 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b   f & MEM_Zero ){
18c6a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
18c6b 6e 70 72 69 6e 74 66 28 31 30 30 2c 20 7a 43 73  nprintf(100, zCs
18c6c 72 2c 22 2b 25 6c 6c 64 7a 22 2c 70 4d 65 6d 2d  r,"+%lldz",pMem-
18c6d 3e 75 2e 69 29 3b 0a 20 20 20 20 20 20 7a 43 73  >u.i);.      zCs
18c6e 72 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 43 73 72  r += strlen(zCsr
18c6f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 43  );.    }.    *zC
18c70 73 72 20 3d 20 27 5c 30 27 3b 0a 20 20 7d 65 6c  sr = '\0';.  }el
18c71 73 65 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 53  se if( f & MEM_S
18c72 74 72 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 2c  tr ){.    int j,
18c73 20 6b 3b 0a 20 20 20 20 7a 42 75 66 5b 30 5d 20   k;.    zBuf[0] 
18c74 3d 20 27 20 27 3b 0a 20 20 20 20 69 66 28 20 66  = ' ';.    if( f
18c75 20 26 20 4d 45 4d 5f 44 79 6e 20 29 7b 0a 20 20   & MEM_Dyn ){.  
18c76 20 20 20 20 7a 42 75 66 5b 31 5d 20 3d 20 27 7a      zBuf[1] = 'z
18c77 27 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ';.      assert(
18c78 20 28 66 20 26 20 28 4d 45 4d 5f 53 74 61 74 69   (f & (MEM_Stati
18c79 63 7c 4d 45 4d 5f 45 70 68 65 6d 29 29 3d 3d 30  c|MEM_Ephem))==0
18c7a 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66   );.    }else if
18c7b 28 20 66 20 26 20 4d 45 4d 5f 53 74 61 74 69 63  ( f & MEM_Static
18c7c 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b 31   ){.      zBuf[1
18c7d 5d 20 3d 20 27 74 27 3b 0a 20 20 20 20 20 20 61  ] = 't';.      a
18c7e 73 73 65 72 74 28 20 28 66 20 26 20 28 4d 45 4d  ssert( (f & (MEM
18c7f 5f 44 79 6e 7c 4d 45 4d 5f 45 70 68 65 6d 29 29  _Dyn|MEM_Ephem))
18c80 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65 6c 73 65  ==0 );.    }else
18c81 20 69 66 28 20 66 20 26 20 4d 45 4d 5f 45 70 68   if( f & MEM_Eph
18c82 65 6d 20 29 7b 0a 20 20 20 20 20 20 7a 42 75 66  em ){.      zBuf
18c83 5b 31 5d 20 3d 20 27 65 27 3b 0a 20 20 20 20 20  [1] = 'e';.     
18c84 20 61 73 73 65 72 74 28 20 28 66 20 26 20 28 4d   assert( (f & (M
18c85 45 4d 5f 53 74 61 74 69 63 7c 4d 45 4d 5f 44 79  EM_Static|MEM_Dy
18c86 6e 29 29 3d 3d 30 20 29 3b 0a 20 20 20 20 7d 65  n))==0 );.    }e
18c87 6c 73 65 7b 0a 20 20 20 20 20 20 7a 42 75 66 5b  lse{.      zBuf[
18c88 31 5d 20 3d 20 27 73 27 3b 0a 20 20 20 20 7d 0a  1] = 's';.    }.
18c89 20 20 20 20 6b 20 3d 20 32 3b 0a 20 20 20 20 73      k = 2;.    s
18c8a 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
18c8b 31 30 30 2c 20 26 7a 42 75 66 5b 6b 5d 2c 20 22  100, &zBuf[k], "
18c8c 25 64 22 2c 20 70 4d 65 6d 2d 3e 6e 29 3b 0a 20  %d", pMem->n);. 
18c8d 20 20 20 6b 20 2b 3d 20 73 74 72 6c 65 6e 28 26     k += strlen(&
18c8e 7a 42 75 66 5b 6b 5d 29 3b 0a 20 20 20 20 7a 42  zBuf[k]);.    zB
18c8f 75 66 5b 6b 2b 2b 5d 20 3d 20 27 5b 27 3b 0a 20  uf[k++] = '[';. 
18c90 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 31 35     for(j=0; j<15
18c91 20 26 26 20 6a 3c 70 4d 65 6d 2d 3e 6e 3b 20 6a   && j<pMem->n; j
18c92 2b 2b 29 7b 0a 20 20 20 20 20 20 75 38 20 63 20  ++){.      u8 c 
18c93 3d 20 70 4d 65 6d 2d 3e 7a 5b 6a 5d 3b 0a 20 20  = pMem->z[j];.  
18c94 20 20 20 20 69 66 28 20 63 3e 3d 30 78 32 30 20      if( c>=0x20 
18c95 26 26 20 63 3c 30 78 37 66 20 29 7b 0a 20 20 20  && c<0x7f ){.   
18c96 20 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d       zBuf[k++] =
18c97 20 63 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   c;.      }else{
18c98 0a 20 20 20 20 20 20 20 20 7a 42 75 66 5b 6b 2b  .        zBuf[k+
18c99 2b 5d 20 3d 20 27 2e 27 3b 0a 20 20 20 20 20 20  +] = '.';.      
18c9a 7d 0a 20 20 20 20 7d 0a 20 20 20 20 7a 42 75 66  }.    }.    zBuf
18c9b 5b 6b 2b 2b 5d 20 3d 20 27 5d 27 3b 0a 20 20 20  [k++] = ']';.   
18c9c 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
18c9d 66 28 31 30 30 2c 26 7a 42 75 66 5b 6b 5d 2c 20  f(100,&zBuf[k], 
18c9e 65 6e 63 6e 61 6d 65 73 5b 70 4d 65 6d 2d 3e 65  encnames[pMem->e
18c9f 6e 63 5d 29 3b 0a 20 20 20 20 6b 20 2b 3d 20 73  nc]);.    k += s
18ca0 74 72 6c 65 6e 28 26 7a 42 75 66 5b 6b 5d 29 3b  trlen(&zBuf[k]);
18ca1 0a 20 20 20 20 7a 42 75 66 5b 6b 2b 2b 5d 20 3d  .    zBuf[k++] =
18ca2 20 30 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66   0;.  }.}.#endif
18ca3 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
18ca4 44 45 42 55 47 0a 2f 2a 0a 2a 2a 20 50 72 69 6e  DEBUG./*.** Prin
18ca5 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 61  t the value of a
18ca6 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74 72   register for tr
18ca7 61 63 69 6e 67 20 70 75 72 70 6f 73 65 73 3a 0a  acing purposes:.
18ca8 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6d  */.static void m
18ca9 65 6d 54 72 61 63 65 50 72 69 6e 74 28 46 49 4c  emTracePrint(FIL
18caa 45 20 2a 6f 75 74 2c 20 4d 65 6d 20 2a 70 29 7b  E *out, Mem *p){
18cab 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67 73 20  .  if( p->flags 
18cac 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20  & MEM_Null ){.  
18cad 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
18cae 20 4e 55 4c 4c 22 29 3b 0a 20 20 7d 65 6c 73 65   NULL");.  }else
18caf 20 69 66 28 20 28 70 2d 3e 66 6c 61 67 73 20 26   if( (p->flags &
18cb0 20 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f 53 74   (MEM_Int|MEM_St
18cb1 72 29 29 3d 3d 28 4d 45 4d 5f 49 6e 74 7c 4d 45  r))==(MEM_Int|ME
18cb2 4d 5f 53 74 72 29 20 29 7b 0a 20 20 20 20 66 70  M_Str) ){.    fp
18cb3 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 73 69 3a  rintf(out, " si:
18cb4 25 6c 6c 64 22 2c 20 70 2d 3e 75 2e 69 29 3b 0a  %lld", p->u.i);.
18cb5 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d 3e 66    }else if( p->f
18cb6 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20 29  lags & MEM_Int )
18cb7 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75  {.    fprintf(ou
18cb8 74 2c 20 22 20 69 3a 25 6c 6c 64 22 2c 20 70 2d  t, " i:%lld", p-
18cb9 3e 75 2e 69 29 3b 0a 20 20 7d 65 6c 73 65 20 69  >u.i);.  }else i
18cba 66 28 20 70 2d 3e 66 6c 61 67 73 20 26 20 4d 45  f( p->flags & ME
18cbb 4d 5f 52 65 61 6c 20 29 7b 0a 20 20 20 20 66 70  M_Real ){.    fp
18cbc 72 69 6e 74 66 28 6f 75 74 2c 20 22 20 72 3a 25  rintf(out, " r:%
18cbd 67 22 2c 20 70 2d 3e 72 29 3b 0a 20 20 7d 65 6c  g", p->r);.  }el
18cbe 73 65 7b 0a 20 20 20 20 63 68 61 72 20 7a 42 75  se{.    char zBu
18cbf 66 5b 32 30 30 5d 3b 0a 20 20 20 20 73 71 6c 69  f[200];.    sqli
18cc0 74 65 33 56 64 62 65 4d 65 6d 50 72 65 74 74 79  te3VdbeMemPretty
18cc1 50 72 69 6e 74 28 70 2c 20 7a 42 75 66 29 3b 0a  Print(p, zBuf);.
18cc2 20 20 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c      fprintf(out,
18cc3 20 22 20 22 29 3b 0a 20 20 20 20 66 70 72 69 6e   " ");.    fprin
18cc4 74 66 28 6f 75 74 2c 20 22 25 73 22 2c 20 7a 42  tf(out, "%s", zB
18cc5 75 66 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  uf);.  }.}.stati
18cc6 63 20 76 6f 69 64 20 72 65 67 69 73 74 65 72 54  c void registerT
18cc7 72 61 63 65 28 46 49 4c 45 20 2a 6f 75 74 2c 20  race(FILE *out, 
18cc8 69 6e 74 20 69 52 65 67 2c 20 4d 65 6d 20 2a 70  int iReg, Mem *p
18cc9 29 7b 0a 20 20 66 70 72 69 6e 74 66 28 6f 75 74  ){.  fprintf(out
18cca 2c 20 22 52 45 47 5b 25 64 5d 20 3d 20 22 2c 20  , "REG[%d] = ", 
18ccb 69 52 65 67 29 3b 0a 20 20 6d 65 6d 54 72 61 63  iReg);.  memTrac
18ccc 65 50 72 69 6e 74 28 6f 75 74 2c 20 70 29 3b 0a  ePrint(out, p);.
18ccd 20 20 66 70 72 69 6e 74 66 28 6f 75 74 2c 20 22    fprintf(out, "
18cce 5c 6e 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a  \n");.}.#endif..
18ccf 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
18cd0 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 52 45  BUG.#  define RE
18cd1 47 49 53 54 45 52 5f 54 52 41 43 45 28 52 2c 4d  GISTER_TRACE(R,M
18cd2 29 20 69 66 28 70 2d 3e 74 72 61 63 65 29 72 65  ) if(p->trace)re
18cd3 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74  gisterTrace(p->t
18cd4 72 61 63 65 2c 52 2c 4d 29 0a 23 65 6c 73 65 0a  race,R,M).#else.
18cd5 23 20 20 64 65 66 69 6e 65 20 52 45 47 49 53 54  #  define REGIST
18cd6 45 52 5f 54 52 41 43 45 28 52 2c 4d 29 0a 23 65  ER_TRACE(R,M).#e
18cd7 6e 64 69 66 0a 0a 0a 23 69 66 64 65 66 20 56 44  ndif...#ifdef VD
18cd8 42 45 5f 50 52 4f 46 49 4c 45 0a 0a 2f 2a 20 0a  BE_PROFILE../* .
18cd9 2a 2a 20 68 77 74 69 6d 65 2e 68 20 63 6f 6e 74  ** hwtime.h cont
18cda 61 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 73 65  ains inline asse
18cdb 6d 62 6c 65 72 20 63 6f 64 65 20 66 6f 72 20 69  mbler code for i
18cdc 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 0a 2a 2a 20  mplementing .** 
18cdd 68 69 67 68 2d 70 65 72 66 6f 72 6d 61 6e 63 65  high-performance
18cde 20 74 69 6d 69 6e 67 20 72 6f 75 74 69 6e 65 73   timing routines
18cdf 2e 0a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..*/./**********
18ce0 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 68 77 74  **** Include hwt
18ce1 69 6d 65 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ime.h in the mid
18ce2 64 6c 65 20 6f 66 20 76 64 62 65 2e 63 20 2a 2a  dle of vdbe.c **
18ce3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ce4 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
18ce5 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
18ce6 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a 2a 2a  hwtime.h *******
18ce7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ce8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ce9 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 38 20  ***/./*.** 2008 
18cea 4d 61 79 20 32 37 0a 2a 2a 0a 2a 2a 20 54 68 65  May 27.**.** The
18ceb 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
18cec 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
18ced 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
18cee 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
18cef 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
18cf0 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
18cf1 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
18cf2 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
18cf3 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
18cf4 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
18cf5 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
18cf6 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
18cf7 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
18cf8 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
18cf9 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
18cfa 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
18cfb 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
18cfc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18cfd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18cfe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18cff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18d00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
18d01 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
18d02 69 6e 73 20 69 6e 6c 69 6e 65 20 61 73 6d 20 63  ins inline asm c
18d03 6f 64 65 20 66 6f 72 20 72 65 74 72 69 65 76 69  ode for retrievi
18d04 6e 67 20 22 68 69 67 68 2d 70 65 72 66 6f 72 6d  ng "high-perform
18d05 61 6e 63 65 22 0a 2a 2a 20 63 6f 75 6e 74 65 72  ance".** counter
18d06 73 20 66 6f 72 20 78 38 36 20 63 6c 61 73 73 20  s for x86 class 
18d07 43 50 55 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a  CPUs..**.** $Id:
18d08 20 68 77 74 69 6d 65 2e 68 2c 76 20 31 2e 33 20   hwtime.h,v 1.3 
18d09 32 30 30 38 2f 30 38 2f 30 31 20 31 34 3a 33 33  2008/08/01 14:33
18d0a 3a 31 35 20 73 68 61 6e 65 20 45 78 70 20 24 0a  :15 shane Exp $.
18d0b 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 48 57 54 49  */.#ifndef _HWTI
18d0c 4d 45 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 48  ME_H_.#define _H
18d0d 57 54 49 4d 45 5f 48 5f 0a 0a 2f 2a 0a 2a 2a 20  WTIME_H_../*.** 
18d0e 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f  The following ro
18d0f 75 74 69 6e 65 20 6f 6e 6c 79 20 77 6f 72 6b 73  utine only works
18d10 20 6f 6e 20 70 65 6e 74 69 75 6d 2d 63 6c 61 73   on pentium-clas
18d11 73 20 28 6f 72 20 6e 65 77 65 72 29 20 70 72 6f  s (or newer) pro
18d12 63 65 73 73 6f 72 73 2e 0a 2a 2a 20 49 74 20 75  cessors..** It u
18d13 73 65 73 20 74 68 65 20 52 44 54 53 43 20 6f 70  ses the RDTSC op
18d14 63 6f 64 65 20 74 6f 20 72 65 61 64 20 74 68 65  code to read the
18d15 20 63 79 63 6c 65 20 63 6f 75 6e 74 20 76 61 6c   cycle count val
18d16 75 65 20 6f 75 74 20 6f 66 20 74 68 65 0a 2a 2a  ue out of the.**
18d17 20 70 72 6f 63 65 73 73 6f 72 20 61 6e 64 20 72   processor and r
18d18 65 74 75 72 6e 73 20 74 68 61 74 20 76 61 6c 75  eturns that valu
18d19 65 2e 20 20 54 68 69 73 20 63 61 6e 20 62 65 20  e.  This can be 
18d1a 75 73 65 64 20 66 6f 72 20 68 69 67 68 2d 72 65  used for high-re
18d1b 73 0a 2a 2a 20 70 72 6f 66 69 6c 69 6e 67 2e 0a  s.** profiling..
18d1c 2a 2f 0a 23 69 66 20 28 64 65 66 69 6e 65 64 28  */.#if (defined(
18d1d 5f 5f 47 4e 55 43 5f 5f 29 20 7c 7c 20 64 65 66  __GNUC__) || def
18d1e 69 6e 65 64 28 5f 4d 53 43 5f 56 45 52 29 29 20  ined(_MSC_VER)) 
18d1f 26 26 20 5c 0a 20 20 20 20 20 20 28 64 65 66 69  && \.      (defi
18d20 6e 65 64 28 69 33 38 36 29 20 7c 7c 20 64 65 66  ned(i386) || def
18d21 69 6e 65 64 28 5f 5f 69 33 38 36 5f 5f 29 20 7c  ined(__i386__) |
18d22 7c 20 64 65 66 69 6e 65 64 28 5f 4d 5f 49 58 38  | defined(_M_IX8
18d23 36 29 29 0a 0a 20 20 23 69 66 20 64 65 66 69 6e  6))..  #if defin
18d24 65 64 28 5f 5f 47 4e 55 43 5f 5f 29 0a 0a 20 20  ed(__GNUC__)..  
18d25 5f 5f 69 6e 6c 69 6e 65 5f 5f 20 73 71 6c 69 74  __inline__ sqlit
18d26 65 5f 75 69 6e 74 36 34 20 73 71 6c 69 74 65 33  e_uint64 sqlite3
18d27 48 77 74 69 6d 65 28 76 6f 69 64 29 7b 0a 20 20  Hwtime(void){.  
18d28 20 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20     unsigned int 
18d29 6c 6f 2c 20 68 69 3b 0a 20 20 20 20 20 5f 5f 61  lo, hi;.     __a
18d2a 73 6d 5f 5f 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f  sm__ __volatile_
18d2b 5f 20 28 22 72 64 74 73 63 22 20 3a 20 22 3d 61  _ ("rdtsc" : "=a
18d2c 22 20 28 6c 6f 29 2c 20 22 3d 64 22 20 28 68 69  " (lo), "=d" (hi
18d2d 29 29 3b 0a 20 20 20 20 20 72 65 74 75 72 6e 20  ));.     return 
18d2e 28 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 29 68  (sqlite_uint64)h
18d2f 69 20 3c 3c 20 33 32 20 7c 20 6c 6f 3b 0a 20 20  i << 32 | lo;.  
18d30 7d 0a 0a 20 20 23 65 6c 69 66 20 64 65 66 69 6e  }..  #elif defin
18d31 65 64 28 5f 4d 53 43 5f 56 45 52 29 0a 0a 20 20  ed(_MSC_VER)..  
18d32 5f 5f 64 65 63 6c 73 70 65 63 28 6e 61 6b 65 64  __declspec(naked
18d33 29 20 5f 5f 69 6e 6c 69 6e 65 20 73 71 6c 69 74  ) __inline sqlit
18d34 65 5f 75 69 6e 74 36 34 20 5f 5f 63 64 65 63 6c  e_uint64 __cdecl
18d35 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76   sqlite3Hwtime(v
18d36 6f 69 64 29 7b 0a 20 20 20 20 20 5f 5f 61 73 6d  oid){.     __asm
18d37 20 7b 0a 20 20 20 20 20 20 20 20 72 64 74 73 63   {.        rdtsc
18d38 0a 20 20 20 20 20 20 20 20 72 65 74 20 20 20 20  .        ret    
18d39 20 20 20 3b 20 72 65 74 75 72 6e 20 76 61 6c 75     ; return valu
18d3a 65 20 61 74 20 45 44 58 3a 45 41 58 0a 20 20 20  e at EDX:EAX.   
18d3b 20 20 7d 0a 20 20 7d 0a 0a 20 20 23 65 6e 64 69    }.  }..  #endi
18d3c 66 0a 0a 23 65 6c 69 66 20 28 64 65 66 69 6e 65  f..#elif (define
18d3d 64 28 5f 5f 47 4e 55 43 5f 5f 29 20 26 26 20 64  d(__GNUC__) && d
18d3e 65 66 69 6e 65 64 28 5f 5f 78 38 36 5f 36 34 5f  efined(__x86_64_
18d3f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69 6e 65 5f  _))..  __inline_
18d40 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74 36 34 20  _ sqlite_uint64 
18d41 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 76 6f  sqlite3Hwtime(vo
18d42 69 64 29 7b 0a 20 20 20 20 20 20 75 6e 73 69 67  id){.      unsig
18d43 6e 65 64 20 6c 6f 6e 67 20 76 61 6c 3b 0a 20 20  ned long val;.  
18d44 20 20 20 20 5f 5f 61 73 6d 5f 5f 20 5f 5f 76 6f      __asm__ __vo
18d45 6c 61 74 69 6c 65 5f 5f 20 28 22 72 64 74 73 63  latile__ ("rdtsc
18d46 22 20 3a 20 22 3d 41 22 20 28 76 61 6c 29 29 3b  " : "=A" (val));
18d47 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 76 61  .      return va
18d48 6c 3b 0a 20 20 7d 0a 20 0a 23 65 6c 69 66 20 28  l;.  }. .#elif (
18d49 64 65 66 69 6e 65 64 28 5f 5f 47 4e 55 43 5f 5f  defined(__GNUC__
18d4a 29 20 26 26 20 64 65 66 69 6e 65 64 28 5f 5f 70  ) && defined(__p
18d4b 70 63 5f 5f 29 29 0a 0a 20 20 5f 5f 69 6e 6c 69  pc__))..  __inli
18d4c 6e 65 5f 5f 20 73 71 6c 69 74 65 5f 75 69 6e 74  ne__ sqlite_uint
18d4d 36 34 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65  64 sqlite3Hwtime
18d4e 28 76 6f 69 64 29 7b 0a 20 20 20 20 20 20 75 6e  (void){.      un
18d4f 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6c 6f 6e 67  signed long long
18d50 20 72 65 74 76 61 6c 3b 0a 20 20 20 20 20 20 75   retval;.      u
18d51 6e 73 69 67 6e 65 64 20 6c 6f 6e 67 20 6a 75 6e  nsigned long jun
18d52 6b 3b 0a 20 20 20 20 20 20 5f 5f 61 73 6d 5f 5f  k;.      __asm__
18d53 20 5f 5f 76 6f 6c 61 74 69 6c 65 5f 5f 20 28 22   __volatile__ ("
18d54 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 31 3a  \n\.          1:
18d55 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25 31        mftbu   %1
18d56 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20 20  \n\.            
18d57 20 20 20 20 20 20 6d 66 74 62 20 20 20 20 25 4c        mftb    %L
18d58 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  0\n\.           
18d59 20 20 20 20 20 20 20 6d 66 74 62 75 20 20 20 25         mftbu   %
18d5a 30 5c 6e 5c 0a 20 20 20 20 20 20 20 20 20 20 20  0\n\.           
18d5b 20 20 20 20 20 20 20 63 6d 70 77 20 20 20 20 25         cmpw    %
18d5c 30 2c 25 31 5c 6e 5c 0a 20 20 20 20 20 20 20 20  0,%1\n\.        
18d5d 20 20 20 20 20 20 20 20 20 20 62 6e 65 20 20 20            bne   
18d5e 20 20 31 62 22 0a 20 20 20 20 20 20 20 20 20 20    1b".          
18d5f 20 20 20 20 20 20 20 20 3a 20 22 3d 72 22 20 28          : "=r" (
18d60 72 65 74 76 61 6c 29 2c 20 22 3d 72 22 20 28 6a  retval), "=r" (j
18d61 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 72 65 74  unk));.      ret
18d62 75 72 6e 20 72 65 74 76 61 6c 3b 0a 20 20 7d 0a  urn retval;.  }.
18d63 0a 23 65 6c 73 65 0a 0a 20 20 23 65 72 72 6f 72  .#else..  #error
18d64 20 4e 65 65 64 20 69 6d 70 6c 65 6d 65 6e 74 61   Need implementa
18d65 74 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33 48  tion of sqlite3H
18d66 77 74 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72  wtime() for your
18d67 20 70 6c 61 74 66 6f 72 6d 2e 0a 0a 20 20 2f 2a   platform...  /*
18d68 0a 20 20 2a 2a 20 54 6f 20 63 6f 6d 70 69 6c 65  .  ** To compile
18d69 20 77 69 74 68 6f 75 74 20 69 6d 70 6c 65 6d 65   without impleme
18d6a 6e 74 69 6e 67 20 73 71 6c 69 74 65 33 48 77 74  nting sqlite3Hwt
18d6b 69 6d 65 28 29 20 66 6f 72 20 79 6f 75 72 20 70  ime() for your p
18d6c 6c 61 74 66 6f 72 6d 2c 0a 20 20 2a 2a 20 79 6f  latform,.  ** yo
18d6d 75 20 63 61 6e 20 72 65 6d 6f 76 65 20 74 68 65  u can remove the
18d6e 20 61 62 6f 76 65 20 23 65 72 72 6f 72 20 61 6e   above #error an
18d6f 64 20 75 73 65 20 74 68 65 20 66 6f 6c 6c 6f 77  d use the follow
18d70 69 6e 67 0a 20 20 2a 2a 20 73 74 75 62 20 66 75  ing.  ** stub fu
18d71 6e 63 74 69 6f 6e 2e 20 20 59 6f 75 20 77 69 6c  nction.  You wil
18d72 6c 20 6c 6f 73 65 20 74 69 6d 69 6e 67 20 73 75  l lose timing su
18d73 70 70 6f 72 74 20 66 6f 72 20 6d 61 6e 79 0a 20  pport for many. 
18d74 20 2a 2a 20 6f 66 20 74 68 65 20 64 65 62 75 67   ** of the debug
18d75 67 69 6e 67 20 61 6e 64 20 74 65 73 74 69 6e 67  ging and testing
18d76 20 75 74 69 6c 69 74 69 65 73 2c 20 62 75 74 20   utilities, but 
18d77 69 74 20 73 68 6f 75 6c 64 20 61 74 0a 20 20 2a  it should at.  *
18d78 2a 20 6c 65 61 73 74 20 63 6f 6d 70 69 6c 65 20  * least compile 
18d79 61 6e 64 20 72 75 6e 2e 0a 20 20 2a 2f 0a 53 51  and run..  */.SQ
18d7a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 20 20 73  LITE_PRIVATE   s
18d7b 71 6c 69 74 65 5f 75 69 6e 74 36 34 20 73 71 6c  qlite_uint64 sql
18d7c 69 74 65 33 48 77 74 69 6d 65 28 76 6f 69 64 29  ite3Hwtime(void)
18d7d 7b 20 72 65 74 75 72 6e 20 28 28 73 71 6c 69 74  { return ((sqlit
18d7e 65 5f 75 69 6e 74 36 34 29 30 29 3b 20 7d 0a 0a  e_uint64)0); }..
18d7f 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f  #endif..#endif /
18d80 2a 20 21 64 65 66 69 6e 65 64 28 5f 48 57 54 49  * !defined(_HWTI
18d81 4d 45 5f 48 5f 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a  ME_H_) */../****
18d82 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
18d83 66 20 68 77 74 69 6d 65 2e 68 20 2a 2a 2a 2a 2a  f hwtime.h *****
18d84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18d85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18d86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
18d87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69  ********** Conti
18d88 6e 75 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c  nuing where we l
18d89 65 66 74 20 6f 66 66 20 69 6e 20 76 64 62 65 2e  eft off in vdbe.
18d8a 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
18d8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 65 6e 64  *********/..#end
18d8c 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 43 48  if../*.** The CH
18d8d 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50  ECK_FOR_INTERRUP
18d8e 54 20 6d 61 63 72 6f 20 64 65 66 69 6e 65 64 20  T macro defined 
18d8f 68 65 72 65 20 6c 6f 6f 6b 73 20 74 6f 20 73 65  here looks to se
18d90 65 20 69 66 20 74 68 65 0a 2a 2a 20 73 71 6c 69  e if the.** sqli
18d91 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 29 20  te3_interrupt() 
18d92 72 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e  routine has been
18d93 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69 74 20   called.  If it 
18d94 68 61 73 20 62 65 65 6e 2c 20 74 68 65 6e 0a 2a  has been, then.*
18d95 2a 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 66 20  * processing of 
18d96 74 68 65 20 56 44 42 45 20 70 72 6f 67 72 61 6d  the VDBE program
18d97 20 69 73 20 69 6e 74 65 72 72 75 70 74 65 64 2e   is interrupted.
18d98 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 61 63 72  .**.** This macr
18d99 6f 20 61 64 64 65 64 20 74 6f 20 65 76 65 72 79  o added to every
18d9a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
18d9b 74 20 64 6f 65 73 20 61 20 6a 75 6d 70 20 69 6e  t does a jump in
18d9c 20 6f 72 64 65 72 20 74 6f 0a 2a 2a 20 69 6d 70   order to.** imp
18d9d 6c 65 6d 65 6e 74 20 61 20 6c 6f 6f 70 2e 20 20  lement a loop.  
18d9e 54 68 69 73 20 74 65 73 74 20 75 73 65 64 20 74  This test used t
18d9f 6f 20 62 65 20 6f 6e 20 65 76 65 72 79 20 73 69  o be on every si
18da0 6e 67 6c 65 20 69 6e 73 74 72 75 63 74 69 6f 6e  ngle instruction
18da1 2c 0a 2a 2a 20 62 75 74 20 74 68 61 74 20 6d 65  ,.** but that me
18da2 61 6e 74 20 77 65 20 6d 6f 72 65 20 74 65 73 74  ant we more test
18da3 69 6e 67 20 74 68 61 74 20 77 65 20 6e 65 65 64  ing that we need
18da4 65 64 2e 20 20 42 79 20 6f 6e 6c 79 20 74 65 73  ed.  By only tes
18da5 74 69 6e 67 20 74 68 65 0a 2a 2a 20 66 6c 61 67  ting the.** flag
18da6 20 6f 6e 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   on jump instruc
18da7 74 69 6f 6e 73 2c 20 77 65 20 67 65 74 20 61 20  tions, we get a 
18da8 28 73 6d 61 6c 6c 29 20 73 70 65 65 64 20 69 6d  (small) speed im
18da9 70 72 6f 76 65 6d 65 6e 74 2e 0a 2a 2f 0a 23 64  provement..*/.#d
18daa 65 66 69 6e 65 20 43 48 45 43 4b 5f 46 4f 52 5f  efine CHECK_FOR_
18dab 49 4e 54 45 52 52 55 50 54 20 5c 0a 20 20 20 69  INTERRUPT \.   i
18dac 66 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65  f( db->u1.isInte
18dad 72 72 75 70 74 65 64 20 29 20 67 6f 74 6f 20 61  rrupted ) goto a
18dae 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e 74 65  bort_due_to_inte
18daf 72 72 75 70 74 3b 0a 0a 23 69 66 64 65 66 20 53  rrupt;..#ifdef S
18db0 51 4c 49 54 45 5f 44 45 42 55 47 0a 73 74 61 74  QLITE_DEBUG.stat
18db1 69 63 20 69 6e 74 20 66 69 6c 65 45 78 69 73 74  ic int fileExist
18db2 73 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  s(sqlite3 *db, c
18db3 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c 65  onst char *zFile
18db4 29 7b 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30  ){.  int res = 0
18db5 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
18db6 49 54 45 5f 4f 4b 3b 0a 23 69 66 64 65 66 20 53  ITE_OK;.#ifdef S
18db7 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 2f 2a 20  QLITE_TEST.  /* 
18db8 49 66 20 77 65 20 61 72 65 20 63 75 72 72 65 6e  If we are curren
18db9 74 6c 79 20 74 65 73 74 69 6e 67 20 49 4f 20 65  tly testing IO e
18dba 72 72 6f 72 73 2c 20 74 68 65 6e 20 64 6f 20 6e  rrors, then do n
18dbb 6f 74 20 63 61 6c 6c 20 4f 73 41 63 63 65 73 73  ot call OsAccess
18dbc 28 29 20 74 6f 0a 20 20 2a 2a 20 74 65 73 74 20  () to.  ** test 
18dbd 66 6f 72 20 74 68 65 20 70 72 65 73 65 6e 63 65  for the presence
18dbe 20 6f 66 20 7a 46 69 6c 65 2e 20 54 68 69 73 20   of zFile. This 
18dbf 69 73 20 62 65 63 61 75 73 65 20 61 6e 79 20 49  is because any I
18dc0 4f 20 65 72 72 6f 72 20 74 68 61 74 0a 20 20 2a  O error that.  *
18dc1 2a 20 6f 63 63 75 72 73 20 68 65 72 65 20 77 69  * occurs here wi
18dc2 6c 6c 20 6e 6f 74 20 62 65 20 72 65 70 6f 72 74  ll not be report
18dc3 65 64 2c 20 63 61 75 73 69 6e 67 20 74 68 65 20  ed, causing the 
18dc4 74 65 73 74 20 74 6f 20 66 61 69 6c 2e 0a 20 20  test to fail..  
18dc5 2a 2f 0a 20 20 65 78 74 65 72 6e 20 69 6e 74 20  */.  extern int 
18dc6 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
18dc7 5f 70 65 6e 64 69 6e 67 3b 0a 20 20 69 66 28 20  _pending;.  if( 
18dc8 73 71 6c 69 74 65 33 5f 69 6f 5f 65 72 72 6f 72  sqlite3_io_error
18dc9 5f 70 65 6e 64 69 6e 67 3c 3d 30 20 29 0a 23 65  _pending<=0 ).#e
18dca 6e 64 69 66 0a 20 20 20 20 72 63 20 3d 20 73 71  ndif.    rc = sq
18dcb 6c 69 74 65 33 4f 73 41 63 63 65 73 73 28 64 62  lite3OsAccess(db
18dcc 2d 3e 70 56 66 73 2c 20 7a 46 69 6c 65 2c 20 53  ->pVfs, zFile, S
18dcd 51 4c 49 54 45 5f 41 43 43 45 53 53 5f 45 58 49  QLITE_ACCESS_EXI
18dce 53 54 53 2c 20 26 72 65 73 29 3b 0a 20 20 72 65  STS, &res);.  re
18dcf 74 75 72 6e 20 28 72 65 73 20 26 26 20 72 63 3d  turn (res && rc=
18dd0 3d 53 51 4c 49 54 45 5f 4f 4b 29 3b 0a 7d 0a 23  =SQLITE_OK);.}.#
18dd1 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 45 78 65  endif../*.** Exe
18dd2 63 75 74 65 20 61 73 20 6d 75 63 68 20 6f 66 20  cute as much of 
18dd3 61 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 61  a VDBE program a
18dd4 73 20 77 65 20 63 61 6e 20 74 68 65 6e 20 72 65  s we can then re
18dd5 74 75 72 6e 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69  turn..**.** sqli
18dd6 74 65 33 56 64 62 65 4d 61 6b 65 52 65 61 64 79  te3VdbeMakeReady
18dd7 28 29 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65  () must be calle
18dd8 64 20 62 65 66 6f 72 65 20 74 68 69 73 20 72 6f  d before this ro
18dd9 75 74 69 6e 65 20 69 6e 20 6f 72 64 65 72 20 74  utine in order t
18dda 6f 0a 2a 2a 20 63 6c 6f 73 65 20 74 68 65 20 70  o.** close the p
18ddb 72 6f 67 72 61 6d 20 77 69 74 68 20 61 20 66 69  rogram with a fi
18ddc 6e 61 6c 20 4f 50 5f 48 61 6c 74 20 61 6e 64 20  nal OP_Halt and 
18ddd 74 6f 20 73 65 74 20 75 70 20 74 68 65 20 63 61  to set up the ca
18dde 6c 6c 62 61 63 6b 73 0a 2a 2a 20 61 6e 64 20 74  llbacks.** and t
18ddf 68 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  he error message
18de0 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20   pointer..**.** 
18de1 57 68 65 6e 65 76 65 72 20 61 20 72 6f 77 20 6f  Whenever a row o
18de2 72 20 72 65 73 75 6c 74 20 64 61 74 61 20 69 73  r result data is
18de3 20 61 76 61 69 6c 61 62 6c 65 2c 20 74 68 69 73   available, this
18de4 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 65 69   routine will ei
18de5 74 68 65 72 0a 2a 2a 20 69 6e 76 6f 6b 65 20 74  ther.** invoke t
18de6 68 65 20 72 65 73 75 6c 74 20 63 61 6c 6c 62 61  he result callba
18de7 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73 20  ck (if there is 
18de8 6f 6e 65 29 20 6f 72 20 72 65 74 75 72 6e 20 77  one) or return w
18de9 69 74 68 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f  ith.** SQLITE_RO
18dea 57 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 61  W..**.** If an a
18deb 74 74 65 6d 70 74 20 69 73 20 6d 61 64 65 20 74  ttempt is made t
18dec 6f 20 6f 70 65 6e 20 61 20 6c 6f 63 6b 65 64 20  o open a locked 
18ded 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 74  database, then t
18dee 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 77  his routine.** w
18def 69 6c 6c 20 65 69 74 68 65 72 20 69 6e 76 6f 6b  ill either invok
18df0 65 20 74 68 65 20 62 75 73 79 20 63 61 6c 6c 62  e the busy callb
18df1 61 63 6b 20 28 69 66 20 74 68 65 72 65 20 69 73  ack (if there is
18df2 20 6f 6e 65 29 20 6f 72 20 69 74 20 77 69 6c 6c   one) or it will
18df3 0a 2a 2a 20 72 65 74 75 72 6e 20 53 51 4c 49 54  .** return SQLIT
18df4 45 5f 42 55 53 59 2e 0a 2a 2a 0a 2a 2a 20 49 66  E_BUSY..**.** If
18df5 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73   an error occurs
18df6 2c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61  , an error messa
18df7 67 65 20 69 73 20 77 72 69 74 74 65 6e 20 74 6f  ge is written to
18df8 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
18df9 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  .** from sqlite3
18dfa 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 70 2d  _malloc() and p-
18dfb 3e 7a 45 72 72 4d 73 67 20 69 73 20 6d 61 64 65  >zErrMsg is made
18dfc 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74 68 61   to point to tha
18dfd 74 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 20 54 68 65  t memory..** The
18dfe 20 65 72 72 6f 72 20 63 6f 64 65 20 69 73 20 73   error code is s
18dff 74 6f 72 65 64 20 69 6e 20 70 2d 3e 72 63 20 61  tored in p->rc a
18e00 6e 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  nd this routine 
18e01 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 45  returns SQLITE_E
18e02 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  RROR..**.** If t
18e03 68 65 20 63 61 6c 6c 62 61 63 6b 20 65 76 65 72  he callback ever
18e04 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
18e05 6f 2c 20 74 68 65 6e 20 74 68 65 20 70 72 6f 67  o, then the prog
18e06 72 61 6d 20 65 78 69 74 73 0a 2a 2a 20 69 6d 6d  ram exits.** imm
18e07 65 64 69 61 74 65 6c 79 2e 20 20 54 68 65 72 65  ediately.  There
18e08 20 77 69 6c 6c 20 62 65 20 6e 6f 20 65 72 72 6f   will be no erro
18e09 72 20 6d 65 73 73 61 67 65 20 62 75 74 20 74 68  r message but th
18e0a 65 20 70 2d 3e 72 63 20 66 69 65 6c 64 20 69 73  e p->rc field is
18e0b 0a 2a 2a 20 73 65 74 20 74 6f 20 53 51 4c 49 54  .** set to SQLIT
18e0c 45 5f 41 42 4f 52 54 20 61 6e 64 20 74 68 69 73  E_ABORT and this
18e0d 20 72 6f 75 74 69 6e 65 20 77 69 6c 6c 20 72 65   routine will re
18e0e 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
18e0f 52 2e 0a 2a 2a 0a 2a 2a 20 41 20 6d 65 6d 6f 72  R..**.** A memor
18e10 79 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72  y allocation err
18e11 6f 72 20 63 61 75 73 65 73 20 70 2d 3e 72 63 20  or causes p->rc 
18e12 74 6f 20 62 65 20 73 65 74 20 74 6f 20 53 51 4c  to be set to SQL
18e13 49 54 45 5f 4e 4f 4d 45 4d 20 61 6e 64 20 74 68  ITE_NOMEM and th
18e14 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 74 6f  is.** routine to
18e15 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
18e16 52 52 4f 52 2e 0a 2a 2a 0a 2a 2a 20 4f 74 68 65  RROR..**.** Othe
18e17 72 20 66 61 74 61 6c 20 65 72 72 6f 72 73 20 72  r fatal errors r
18e18 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
18e19 4f 52 2e 0a 2a 2a 0a 2a 2a 20 41 66 74 65 72 20  OR..**.** After 
18e1a 74 68 69 73 20 72 6f 75 74 69 6e 65 20 68 61 73  this routine has
18e1b 20 66 69 6e 69 73 68 65 64 2c 20 73 71 6c 69 74   finished, sqlit
18e1c 65 33 56 64 62 65 46 69 6e 61 6c 69 7a 65 28 29  e3VdbeFinalize()
18e1d 20 73 68 6f 75 6c 64 20 62 65 0a 2a 2a 20 75 73   should be.** us
18e1e 65 64 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74  ed to clean up t
18e1f 68 65 20 6d 65 73 73 20 74 68 61 74 20 77 61 73  he mess that was
18e20 20 6c 65 66 74 20 62 65 68 69 6e 64 2e 0a 2a 2f   left behind..*/
18e21 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
18e22 69 6e 74 20 73 71 6c 69 74 65 33 56 64 62 65 45  int sqlite3VdbeE
18e23 78 65 63 28 0a 20 20 56 64 62 65 20 2a 70 20 20  xec(.  Vdbe *p  
18e24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e25 20 20 2f 2a 20 54 68 65 20 56 44 42 45 20 2a 2f    /* The VDBE */
18e26 0a 29 7b 0a 20 20 69 6e 74 20 70 63 3b 20 20 20  .){.  int pc;   
18e27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e28 20 2f 2a 20 54 68 65 20 70 72 6f 67 72 61 6d 20   /* The program 
18e29 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 4f 70 20  counter */.  Op 
18e2a 2a 70 4f 70 3b 20 20 20 20 20 20 20 20 20 20 20  *pOp;           
18e2b 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
18e2c 6e 74 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a  nt operation */.
18e2d 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
18e2e 45 5f 4f 4b 3b 20 20 20 20 20 20 20 20 2f 2a 20  E_OK;        /* 
18e2f 56 61 6c 75 65 20 74 6f 20 72 65 74 75 72 6e 20  Value to return 
18e30 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
18e31 20 3d 20 70 2d 3e 64 62 3b 20 20 20 20 20 20 20   = p->db;       
18e32 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
18e33 2a 2f 0a 20 20 75 38 20 65 6e 63 6f 64 69 6e 67  */.  u8 encoding
18e34 20 3d 20 45 4e 43 28 64 62 29 3b 20 20 20 20 20   = ENC(db);     
18e35 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
18e36 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 4d 65  encoding */.  Me
18e37 6d 20 2a 70 49 6e 31 2c 20 2a 70 49 6e 32 2c 20  m *pIn1, *pIn2, 
18e38 2a 70 49 6e 33 3b 20 20 20 2f 2a 20 49 6e 70 75  *pIn3;   /* Inpu
18e39 74 20 6f 70 65 72 61 6e 64 73 20 2a 2f 0a 20 20  t operands */.  
18e3a 4d 65 6d 20 2a 70 4f 75 74 3b 20 20 20 20 20 20  Mem *pOut;      
18e3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 75             /* Ou
18e3c 74 70 75 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a  tput operand */.
18e3d 20 20 75 38 20 6f 70 50 72 6f 70 65 72 74 79 3b    u8 opProperty;
18e3e 0a 20 20 69 6e 74 20 69 43 6f 6d 70 61 72 65 20  .  int iCompare 
18e3f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
18e40 20 52 65 73 75 6c 74 20 6f 66 20 6c 61 73 74 20   Result of last 
18e41 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70 65 72 61  OP_Compare opera
18e42 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 61  tion */.  int *a
18e43 50 65 72 6d 75 74 65 20 3d 20 30 3b 20 20 20 20  Permute = 0;    
18e44 20 20 20 20 20 2f 2a 20 50 65 72 6d 75 61 74 69       /* Permuati
18e45 6f 6e 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 66 6f  on of columns fo
18e46 72 20 4f 50 5f 43 6f 6d 70 61 72 65 20 2a 2f 0a  r OP_Compare */.
18e47 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
18e48 49 4c 45 0a 20 20 75 36 34 20 73 74 61 72 74 3b  ILE.  u64 start;
18e49 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
18e4a 20 2f 2a 20 43 50 55 20 63 6c 6f 63 6b 20 63 6f   /* CPU clock co
18e4b 75 6e 74 20 61 74 20 73 74 61 72 74 20 6f 66 20  unt at start of 
18e4c 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20  opcode */.  int 
18e4d 6f 72 69 67 50 63 3b 20 20 20 20 20 20 20 20 20  origPc;         
18e4e 20 20 20 20 20 20 20 2f 2a 20 50 72 6f 67 72 61         /* Progra
18e4f 6d 20 63 6f 75 6e 74 65 72 20 61 74 20 73 74 61  m counter at sta
18e50 72 74 20 6f 66 20 6f 70 63 6f 64 65 20 2a 2f 0a  rt of opcode */.
18e51 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 53  #endif.#ifndef S
18e52 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 4f 47 52  QLITE_OMIT_PROGR
18e53 45 53 53 5f 43 41 4c 4c 42 41 43 4b 0a 20 20 69  ESS_CALLBACK.  i
18e54 6e 74 20 6e 50 72 6f 67 72 65 73 73 4f 70 73 20  nt nProgressOps 
18e55 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 70 63  = 0;      /* Opc
18e56 6f 64 65 73 20 65 78 65 63 75 74 65 64 20 73 69  odes executed si
18e57 6e 63 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  nce progress cal
18e58 6c 62 61 63 6b 2e 20 2a 2f 0a 23 65 6e 64 69 66  lback. */.#endif
18e59 0a 20 20 55 6e 70 61 63 6b 65 64 52 65 63 6f 72  .  UnpackedRecor
18e5a 64 20 61 54 65 6d 70 52 65 63 5b 31 36 5d 3b 20  d aTempRec[16]; 
18e5b 2f 2a 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64  /* Space to hold
18e5c 20 61 20 74 72 61 6e 73 69 65 6e 74 20 55 6e 70   a transient Unp
18e5d 61 63 6b 65 64 52 65 63 6f 72 64 20 2a 2f 0a 0a  ackedRecord */..
18e5e 20 20 61 73 73 65 72 74 28 20 70 2d 3e 6d 61 67    assert( p->mag
18e5f 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
18e60 55 4e 20 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65  UN );  /* sqlite
18e61 33 5f 73 74 65 70 28 29 20 76 65 72 69 66 69 65  3_step() verifie
18e62 73 20 74 68 69 73 20 2a 2f 0a 20 20 61 73 73 65  s this */.  asse
18e63 72 74 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53  rt( db->magic==S
18e64 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
18e65 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72   );.  sqlite3Btr
18e66 65 65 4d 75 74 65 78 41 72 72 61 79 45 6e 74 65  eeMutexArrayEnte
18e67 72 28 26 70 2d 3e 61 4d 75 74 65 78 29 3b 0a 20  r(&p->aMutex);. 
18e68 20 69 66 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49   if( p->rc==SQLI
18e69 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20  TE_NOMEM ){.    
18e6a 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e 73 20  /* This happens 
18e6b 69 66 20 61 20 6d 61 6c 6c 6f 63 28 29 20 69 6e  if a malloc() in
18e6c 73 69 64 65 20 61 20 63 61 6c 6c 20 74 6f 20 73  side a call to s
18e6d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
18e6e 78 74 28 29 20 6f 72 0a 20 20 20 20 2a 2a 20 73  xt() or.    ** s
18e6f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65  qlite3_column_te
18e70 78 74 31 36 28 29 20 66 61 69 6c 65 64 2e 20 20  xt16() failed.  
18e71 2a 2f 0a 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d  */.    goto no_m
18e72 65 6d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  em;.  }.  assert
18e73 28 20 70 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f  ( p->rc==SQLITE_
18e74 4f 4b 20 7c 7c 20 70 2d 3e 72 63 3d 3d 53 51 4c  OK || p->rc==SQL
18e75 49 54 45 5f 42 55 53 59 20 29 3b 0a 20 20 70 2d  ITE_BUSY );.  p-
18e76 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  >rc = SQLITE_OK;
18e77 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 65 78  .  assert( p->ex
18e78 70 6c 61 69 6e 3d 3d 30 20 29 3b 0a 20 20 70 2d  plain==0 );.  p-
18e79 3e 70 52 65 73 75 6c 74 53 65 74 20 3d 20 30 3b  >pResultSet = 0;
18e7a 0a 20 20 64 62 2d 3e 62 75 73 79 48 61 6e 64 6c  .  db->busyHandl
18e7b 65 72 2e 6e 42 75 73 79 20 3d 20 30 3b 0a 20 20  er.nBusy = 0;.  
18e7c 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52  CHECK_FOR_INTERR
18e7d 55 50 54 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  UPT;.  sqlite3Vd
18e7e 62 65 49 4f 54 72 61 63 65 53 71 6c 28 70 29 3b  beIOTraceSql(p);
18e7f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
18e80 45 42 55 47 0a 20 20 73 71 6c 69 74 65 33 42 65  EBUG.  sqlite3Be
18e81 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
18e82 29 3b 0a 20 20 69 66 28 20 70 2d 3e 70 63 3d 3d  );.  if( p->pc==
18e83 30 20 0a 20 20 20 26 26 20 28 28 70 2d 3e 64 62  0 .   && ((p->db
18e84 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
18e85 5f 56 64 62 65 4c 69 73 74 69 6e 67 29 20 7c 7c  _VdbeListing) ||
18e86 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c 20   fileExists(db, 
18e87 22 76 64 62 65 5f 65 78 70 6c 61 69 6e 22 29 29  "vdbe_explain"))
18e88 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b  .  ){.    int i;
18e89 0a 20 20 20 20 70 72 69 6e 74 66 28 22 56 44 42  .    printf("VDB
18e8a 45 20 50 72 6f 67 72 61 6d 20 4c 69 73 74 69 6e  E Program Listin
18e8b 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 73 71 6c 69  g:\n");.    sqli
18e8c 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
18e8d 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  p);.    for(i=0;
18e8e 20 69 3c 70 2d 3e 6e 4f 70 3b 20 69 2b 2b 29 7b   i<p->nOp; i++){
18e8f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
18e90 62 65 50 72 69 6e 74 4f 70 28 73 74 64 6f 75 74  bePrintOp(stdout
18e91 2c 20 69 2c 20 26 70 2d 3e 61 4f 70 5b 69 5d 29  , i, &p->aOp[i])
18e92 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
18e93 28 20 66 69 6c 65 45 78 69 73 74 73 28 64 62 2c  ( fileExists(db,
18e94 20 22 76 64 62 65 5f 74 72 61 63 65 22 29 20 29   "vdbe_trace") )
18e95 7b 0a 20 20 20 20 70 2d 3e 74 72 61 63 65 20 3d  {.    p->trace =
18e96 20 73 74 64 6f 75 74 3b 0a 20 20 7d 0a 20 20 73   stdout;.  }.  s
18e97 71 6c 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d  qlite3EndBenignM
18e98 61 6c 6c 6f 63 28 29 3b 0a 23 65 6e 64 69 66 0a  alloc();.#endif.
18e99 20 20 66 6f 72 28 70 63 3d 70 2d 3e 70 63 3b 20    for(pc=p->pc; 
18e9a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3b 20 70  rc==SQLITE_OK; p
18e9b 63 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74  c++){.    assert
18e9c 28 20 70 63 3e 3d 30 20 26 26 20 70 63 3c 70 2d  ( pc>=0 && pc<p-
18e9d 3e 6e 4f 70 20 29 3b 0a 20 20 20 20 69 66 28 20  >nOp );.    if( 
18e9e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
18e9f 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
18ea0 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
18ea1 49 4c 45 0a 20 20 20 20 6f 72 69 67 50 63 20 3d  ILE.    origPc =
18ea2 20 70 63 3b 0a 20 20 20 20 73 74 61 72 74 20 3d   pc;.    start =
18ea3 20 73 71 6c 69 74 65 33 48 77 74 69 6d 65 28 29   sqlite3Hwtime()
18ea4 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 4f 70  ;.#endif.    pOp
18ea5 20 3d 20 26 70 2d 3e 61 4f 70 5b 70 63 5d 3b 0a   = &p->aOp[pc];.
18ea6 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 61 6c 6c  .    /* Only all
18ea7 6f 77 20 74 72 61 63 69 6e 67 20 69 66 20 53 51  ow tracing if SQ
18ea8 4c 49 54 45 5f 44 45 42 55 47 20 69 73 20 64 65  LITE_DEBUG is de
18ea9 66 69 6e 65 64 2e 0a 20 20 20 20 2a 2f 0a 23 69  fined..    */.#i
18eaa 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55  fdef SQLITE_DEBU
18eab 47 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61  G.    if( p->tra
18eac 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
18ead 70 63 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  pc==0 ){.       
18eae 20 70 72 69 6e 74 66 28 22 56 44 42 45 20 45 78   printf("VDBE Ex
18eaf 65 63 75 74 69 6f 6e 20 54 72 61 63 65 3a 5c 6e  ecution Trace:\n
18eb0 22 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ");.        sqli
18eb1 74 65 33 56 64 62 65 50 72 69 6e 74 53 71 6c 28  te3VdbePrintSql(
18eb2 70 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  p);.      }.    
18eb3 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
18eb4 6e 74 4f 70 28 70 2d 3e 74 72 61 63 65 2c 20 70  ntOp(p->trace, p
18eb5 63 2c 20 70 4f 70 29 3b 0a 20 20 20 20 7d 0a 20  c, pOp);.    }. 
18eb6 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65 3d     if( p->trace=
18eb7 3d 30 20 26 26 20 70 63 3d 3d 30 20 29 7b 0a 20  =0 && pc==0 ){. 
18eb8 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69       sqlite3Begi
18eb9 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28 29 3b  nBenignMalloc();
18eba 0a 20 20 20 20 20 20 69 66 28 20 66 69 6c 65 45  .      if( fileE
18ebb 78 69 73 74 73 28 64 62 2c 20 22 76 64 62 65 5f  xists(db, "vdbe_
18ebc 73 71 6c 74 72 61 63 65 22 29 20 29 7b 0a 20 20  sqltrace") ){.  
18ebd 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
18ebe 65 50 72 69 6e 74 53 71 6c 28 70 29 3b 0a 20 20  ePrintSql(p);.  
18ebf 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
18ec0 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c  te3EndBenignMall
18ec1 6f 63 28 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  oc();.    }.#end
18ec2 69 66 0a 20 20 20 20 20 20 0a 0a 20 20 20 20 2f  if.      ..    /
18ec3 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
18ec4 66 20 77 65 20 6e 65 65 64 20 74 6f 20 73 69 6d  f we need to sim
18ec5 75 6c 61 74 65 20 61 6e 20 69 6e 74 65 72 72 75  ulate an interru
18ec6 70 74 2e 20 20 54 68 69 73 20 6f 6e 6c 79 20 68  pt.  This only h
18ec7 61 70 70 65 6e 73 0a 20 20 20 20 2a 2a 20 69 66  appens.    ** if
18ec8 20 77 65 20 68 61 76 65 20 61 20 73 70 65 63 69   we have a speci
18ec9 61 6c 20 74 65 73 74 20 62 75 69 6c 64 2e 0a 20  al test build.. 
18eca 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c     */.#ifdef SQL
18ecb 49 54 45 5f 54 45 53 54 0a 20 20 20 20 69 66 28  ITE_TEST.    if(
18ecc 20 73 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75   sqlite3_interru
18ecd 70 74 5f 63 6f 75 6e 74 3e 30 20 29 7b 0a 20 20  pt_count>0 ){.  
18ece 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 74 65      sqlite3_inte
18ecf 72 72 75 70 74 5f 63 6f 75 6e 74 2d 2d 3b 0a 20  rrupt_count--;. 
18ed0 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
18ed1 5f 69 6e 74 65 72 72 75 70 74 5f 63 6f 75 6e 74  _interrupt_count
18ed2 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
18ed3 71 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74  qlite3_interrupt
18ed4 28 64 62 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  (db);.      }.  
18ed5 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e    }.#endif..#ifn
18ed6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
18ed7 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
18ed8 4b 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68  K.    /* Call th
18ed9 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c 6c 62  e progress callb
18eda 61 63 6b 20 69 66 20 69 74 20 69 73 20 63 6f 6e  ack if it is con
18edb 66 69 67 75 72 65 64 20 61 6e 64 20 74 68 65 20  figured and the 
18edc 72 65 71 75 69 72 65 64 20 6e 75 6d 62 65 72 0a  required number.
18edd 20 20 20 20 2a 2a 20 6f 66 20 56 44 42 45 20 6f      ** of VDBE o
18ede 70 73 20 68 61 76 65 20 62 65 65 6e 20 65 78 65  ps have been exe
18edf 63 75 74 65 64 20 28 65 69 74 68 65 72 20 73 69  cuted (either si
18ee0 6e 63 65 20 74 68 69 73 20 69 6e 76 6f 63 61 74  nce this invocat
18ee1 69 6f 6e 20 6f 66 0a 20 20 20 20 2a 2a 20 73 71  ion of.    ** sq
18ee2 6c 69 74 65 33 56 64 62 65 45 78 65 63 28 29 20  lite3VdbeExec() 
18ee3 6f 72 20 73 69 6e 63 65 20 6c 61 73 74 20 74 69  or since last ti
18ee4 6d 65 20 74 68 65 20 70 72 6f 67 72 65 73 73 20  me the progress 
18ee5 63 61 6c 6c 62 61 63 6b 20 77 61 73 20 63 61 6c  callback was cal
18ee6 6c 65 64 29 2e 0a 20 20 20 20 2a 2a 20 49 66 20  led)..    ** If 
18ee7 74 68 65 20 70 72 6f 67 72 65 73 73 20 63 61 6c  the progress cal
18ee8 6c 62 61 63 6b 20 72 65 74 75 72 6e 73 20 6e 6f  lback returns no
18ee9 6e 2d 7a 65 72 6f 2c 20 65 78 69 74 20 74 68 65  n-zero, exit the
18eea 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e 65   virtual machine
18eeb 20 77 69 74 68 0a 20 20 20 20 2a 2a 20 61 20 72   with.    ** a r
18eec 65 74 75 72 6e 20 63 6f 64 65 20 53 51 4c 49 54  eturn code SQLIT
18eed 45 5f 41 42 4f 52 54 2e 0a 20 20 20 20 2a 2f 0a  E_ABORT..    */.
18eee 20 20 20 20 69 66 28 20 64 62 2d 3e 78 50 72 6f      if( db->xPro
18eef 67 72 65 73 73 20 29 7b 0a 20 20 20 20 20 20 69  gress ){.      i
18ef0 66 28 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73  f( db->nProgress
18ef1 4f 70 73 3d 3d 6e 50 72 6f 67 72 65 73 73 4f 70  Ops==nProgressOp
18ef2 73 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  s ){.        int
18ef3 20 70 72 63 3b 0a 20 20 20 20 20 20 20 20 69 66   prc;.        if
18ef4 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
18ef5 66 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  ff(db) ) goto ab
18ef6 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
18ef7 65 3b 0a 20 20 20 20 20 20 20 20 70 72 63 20 3d  e;.        prc =
18ef8 64 62 2d 3e 78 50 72 6f 67 72 65 73 73 28 64 62  db->xProgress(db
18ef9 2d 3e 70 50 72 6f 67 72 65 73 73 41 72 67 29 3b  ->pProgressArg);
18efa 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c  .        if( sql
18efb 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29  ite3SafetyOn(db)
18efc 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
18efd 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
18efe 20 20 20 20 20 69 66 28 20 70 72 63 21 3d 30 20       if( prc!=0 
18eff 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63 20  ){.          rc 
18f00 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
18f01 50 54 3b 0a 20 20 20 20 20 20 20 20 20 20 67 6f  PT;.          go
18f02 74 6f 20 76 64 62 65 5f 65 72 72 6f 72 5f 68 61  to vdbe_error_ha
18f03 6c 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  lt;.        }.  
18f04 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
18f05 70 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  ps = 0;.      }.
18f06 20 20 20 20 20 20 6e 50 72 6f 67 72 65 73 73 4f        nProgressO
18f07 70 73 2b 2b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  ps++;.    }.#end
18f08 69 66 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 63 6f  if..    /* Do co
18f09 6d 6d 6f 6e 20 73 65 74 75 70 20 70 72 6f 63 65  mmon setup proce
18f0a 73 73 69 6e 67 20 66 6f 72 20 61 6e 79 20 6f 70  ssing for any op
18f0b 63 6f 64 65 20 74 68 61 74 20 69 73 20 6d 61 72  code that is mar
18f0c 6b 65 64 0a 20 20 20 20 2a 2a 20 77 69 74 68 20  ked.    ** with 
18f0d 74 68 65 20 22 6f 75 74 32 2d 70 72 65 72 65 6c  the "out2-prerel
18f0e 65 61 73 65 22 20 74 61 67 2e 20 20 53 75 63 68  ease" tag.  Such
18f0f 20 6f 70 63 6f 64 65 73 20 68 61 76 65 20 61 20   opcodes have a 
18f10 73 69 6e 67 6c 65 0a 20 20 20 20 2a 2a 20 6f 75  single.    ** ou
18f11 74 70 75 74 20 77 68 69 63 68 20 69 73 20 73 70  tput which is sp
18f12 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20 50  ecified by the P
18f13 32 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  2 parameter.  Th
18f14 65 20 50 32 20 72 65 67 69 73 74 65 72 0a 20 20  e P2 register.  
18f15 20 20 2a 2a 20 69 73 20 69 6e 69 74 69 61 6c 69    ** is initiali
18f16 7a 65 64 20 74 6f 20 61 20 4e 55 4c 4c 2e 0a 20  zed to a NULL.. 
18f17 20 20 20 2a 2f 0a 20 20 20 20 6f 70 50 72 6f 70     */.    opProp
18f18 65 72 74 79 20 3d 20 6f 70 63 6f 64 65 50 72 6f  erty = opcodePro
18f19 70 65 72 74 79 5b 70 4f 70 2d 3e 6f 70 63 6f 64  perty[pOp->opcod
18f1a 65 5d 3b 0a 20 20 20 20 69 66 28 20 28 6f 70 50  e];.    if( (opP
18f1b 72 6f 70 65 72 74 79 20 26 20 4f 50 46 4c 47 5f  roperty & OPFLG_
18f1c 4f 55 54 32 5f 50 52 45 52 45 4c 45 41 53 45 29  OUT2_PRERELEASE)
18f1d 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73  !=0 ){.      ass
18f1e 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29  ert( pOp->p2>0 )
18f1f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
18f20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p2<=p->nMem
18f21 20 29 3b 0a 20 20 20 20 20 20 70 4f 75 74 20 3d   );.      pOut =
18f22 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
18f23 32 5d 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  2];.      sqlite
18f24 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73 65 45  3VdbeMemReleaseE
18f25 78 74 65 72 6e 61 6c 28 70 4f 75 74 29 3b 0a 20  xternal(pOut);. 
18f26 20 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73       pOut->flags
18f27 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20 20 20   = MEM_Null;.   
18f28 20 7d 65 6c 73 65 0a 20 0a 20 20 20 20 2f 2a 20   }else. .    /* 
18f29 44 6f 20 63 6f 6d 6d 6f 6e 20 73 65 74 75 70 20  Do common setup 
18f2a 66 6f 72 20 6f 70 63 6f 64 65 73 20 6d 61 72 6b  for opcodes mark
18f2b 65 64 20 77 69 74 68 20 6f 6e 65 20 6f 66 20 74  ed with one of t
18f2c 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20 20  he following.   
18f2d 20 2a 2a 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 73   ** combinations
18f2e 20 6f 66 20 70 72 6f 70 65 72 74 69 65 73 2e 0a   of properties..
18f2f 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20      **.    **   
18f30 20 20 20 20 20 20 20 20 69 6e 31 0a 20 20 20 20          in1.    
18f31 2a 2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 31  **           in1
18f32 20 69 6e 32 0a 20 20 20 20 2a 2a 20 20 20 20 20   in2.    **     
18f33 20 20 20 20 20 20 69 6e 31 20 69 6e 32 20 6f 75        in1 in2 ou
18f34 74 33 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  t3.    **       
18f35 20 20 20 20 69 6e 31 20 69 6e 33 0a 20 20 20 20      in1 in3.    
18f36 2a 2a 0a 20 20 20 20 2a 2a 20 56 61 72 69 61 62  **.    ** Variab
18f37 6c 65 73 20 70 49 6e 31 2c 20 70 49 6e 32 2c 20  les pIn1, pIn2, 
18f38 61 6e 64 20 70 49 6e 33 20 61 72 65 20 6d 61 64  and pIn3 are mad
18f39 65 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 61 70  e to point to ap
18f3a 70 72 6f 70 72 69 61 74 65 0a 20 20 20 20 2a 2a  propriate.    **
18f3b 20 72 65 67 69 73 74 65 72 73 20 66 6f 72 20 69   registers for i
18f3c 6e 70 75 74 73 2e 20 20 56 61 72 69 61 62 6c 65  nputs.  Variable
18f3d 20 70 4f 75 74 20 70 6f 69 6e 74 73 20 74 6f 20   pOut points to 
18f3e 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69 73  the output regis
18f3f 74 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ter..    */.    
18f40 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20  if( (opProperty 
18f41 26 20 4f 50 46 4c 47 5f 49 4e 31 29 21 3d 30 20  & OPFLG_IN1)!=0 
18f42 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
18f43 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20   pOp->p1>0 );.  
18f44 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d      assert( pOp-
18f45 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
18f46 20 20 20 20 20 20 70 49 6e 31 20 3d 20 26 70 2d        pIn1 = &p-
18f47 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
18f48 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f 54        REGISTER_T
18f49 52 41 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 49  RACE(pOp->p1, pI
18f4a 6e 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  n1);.      if( (
18f4b 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
18f4c 4c 47 5f 49 4e 32 29 21 3d 30 20 29 7b 0a 20 20  LG_IN2)!=0 ){.  
18f4d 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
18f4e 70 2d 3e 70 32 3e 30 20 29 3b 0a 20 20 20 20 20  p->p2>0 );.     
18f4f 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e     assert( pOp->
18f50 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
18f51 20 20 20 20 20 20 20 70 49 6e 32 20 3d 20 26 70         pIn2 = &p
18f52 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
18f53 0a 20 20 20 20 20 20 20 20 52 45 47 49 53 54 45  .        REGISTE
18f54 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c  R_TRACE(pOp->p2,
18f55 20 70 49 6e 32 29 3b 0a 20 20 20 20 20 20 20 20   pIn2);.        
18f56 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79 20  if( (opProperty 
18f57 26 20 4f 50 46 4c 47 5f 4f 55 54 33 29 21 3d 30  & OPFLG_OUT3)!=0
18f58 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
18f59 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
18f5a 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73  );.          ass
18f5b 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ert( pOp->p3<=p-
18f5c 3e 6e 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20  >nMem );.       
18f5d 20 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d     pOut = &p->aM
18f5e 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20  em[pOp->p3];.   
18f5f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
18f60 73 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72  se if( (opProper
18f61 74 79 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21  ty & OPFLG_IN3)!
18f62 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73  =0 ){.        as
18f63 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
18f64 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  );.        asser
18f65 74 28 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e  t( pOp->p3<=p->n
18f66 4d 65 6d 20 29 3b 0a 20 20 20 20 20 20 20 20 70  Mem );.        p
18f67 49 6e 33 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  In3 = &p->aMem[p
18f68 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 20 20 20  Op->p3];.       
18f69 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28   REGISTER_TRACE(
18f6a 70 4f 70 2d 3e 70 33 2c 20 70 49 6e 33 29 3b 0a  pOp->p3, pIn3);.
18f6b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
18f6c 65 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74  e if( (opPropert
18f6d 79 20 26 20 4f 50 46 4c 47 5f 49 4e 32 29 21 3d  y & OPFLG_IN2)!=
18f6e 30 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72  0 ){.      asser
18f6f 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b 0a  t( pOp->p2>0 );.
18f70 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f        assert( pO
18f71 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
18f72 3b 0a 20 20 20 20 20 20 70 49 6e 32 20 3d 20 26  ;.      pIn2 = &
18f73 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
18f74 3b 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52  ;.      REGISTER
18f75 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20  _TRACE(pOp->p2, 
18f76 70 49 6e 32 29 3b 0a 20 20 20 20 7d 65 6c 73 65  pIn2);.    }else
18f77 20 69 66 28 20 28 6f 70 50 72 6f 70 65 72 74 79   if( (opProperty
18f78 20 26 20 4f 50 46 4c 47 5f 49 4e 33 29 21 3d 30   & OPFLG_IN3)!=0
18f79 20 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74   ){.      assert
18f7a 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 3b 0a 20  ( pOp->p3>0 );. 
18f7b 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4f 70       assert( pOp
18f7c 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
18f7d 0a 20 20 20 20 20 20 70 49 6e 33 20 3d 20 26 70  .      pIn3 = &p
18f7e 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
18f7f 0a 20 20 20 20 20 20 52 45 47 49 53 54 45 52 5f  .      REGISTER_
18f80 54 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70  TRACE(pOp->p3, p
18f81 49 6e 33 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  In3);.    }..   
18f82 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70   switch( pOp->op
18f83 63 6f 64 65 20 29 7b 0a 0a 2f 2a 2a 2a 2a 2a 2a  code ){../******
18f84 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f85 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f86 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f87 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18f88 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 57 68 61 74 20  *******.** What 
18f89 66 6f 6c 6c 6f 77 73 20 69 73 20 61 20 6d 61 73  follows is a mas
18f8a 73 69 76 65 20 73 77 69 74 63 68 20 73 74 61 74  sive switch stat
18f8b 65 6d 65 6e 74 20 77 68 65 72 65 20 65 61 63 68  ement where each
18f8c 20 63 61 73 65 20 69 6d 70 6c 65 6d 65 6e 74 73   case implements
18f8d 20 61 0a 2a 2a 20 73 65 70 61 72 61 74 65 20 69   a.** separate i
18f8e 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20 74 68  nstruction in th
18f8f 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69 6e  e virtual machin
18f90 65 2e 20 20 49 66 20 77 65 20 66 6f 6c 6c 6f 77  e.  If we follow
18f91 20 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 69 6e   the usual.** in
18f92 64 65 6e 74 61 74 69 6f 6e 20 63 6f 6e 76 65 6e  dentation conven
18f93 74 69 6f 6e 73 2c 20 65 61 63 68 20 63 61 73 65  tions, each case
18f94 20 73 68 6f 75 6c 64 20 62 65 20 69 6e 64 65 6e   should be inden
18f95 74 65 64 20 62 79 20 36 20 73 70 61 63 65 73 2e  ted by 6 spaces.
18f96 20 20 42 75 74 0a 2a 2a 20 74 68 61 74 20 69 73    But.** that is
18f97 20 61 20 6c 6f 74 20 6f 66 20 77 61 73 74 65 64   a lot of wasted
18f98 20 73 70 61 63 65 20 6f 6e 20 74 68 65 20 6c 65   space on the le
18f99 66 74 20 6d 61 72 67 69 6e 2e 20 20 53 6f 20 74  ft margin.  So t
18f9a 68 65 20 63 6f 64 65 20 77 69 74 68 69 6e 0a 2a  he code within.*
18f9b 2a 20 74 68 65 20 73 77 69 74 63 68 20 73 74 61  * the switch sta
18f9c 74 65 6d 65 6e 74 20 77 69 6c 6c 20 62 72 65 61  tement will brea
18f9d 6b 20 77 69 74 68 20 63 6f 6e 76 65 6e 74 69 6f  k with conventio
18f9e 6e 20 61 6e 64 20 62 65 20 66 6c 75 73 68 2d 6c  n and be flush-l
18f9f 65 66 74 2e 20 41 6e 6f 74 68 65 72 0a 2a 2a 20  eft. Another.** 
18fa0 62 69 67 20 63 6f 6d 6d 65 6e 74 20 28 73 69 6d  big comment (sim
18fa1 69 6c 61 72 20 74 6f 20 74 68 69 73 20 6f 6e 65  ilar to this one
18fa2 29 20 77 69 6c 6c 20 6d 61 72 6b 20 74 68 65 20  ) will mark the 
18fa3 70 6f 69 6e 74 20 69 6e 20 74 68 65 20 63 6f 64  point in the cod
18fa4 65 20 77 68 65 72 65 0a 2a 2a 20 77 65 20 74 72  e where.** we tr
18fa5 61 6e 73 69 74 69 6f 6e 20 62 61 63 6b 20 74 6f  ansition back to
18fa6 20 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74   normal indentat
18fa7 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  ion..**.** The f
18fa8 6f 72 6d 61 74 74 69 6e 67 20 6f 66 20 65 61 63  ormatting of eac
18fa9 68 20 63 61 73 65 20 69 73 20 69 6d 70 6f 72 74  h case is import
18faa 61 6e 74 2e 20 20 54 68 65 20 6d 61 6b 65 66 69  ant.  The makefi
18fab 6c 65 20 66 6f 72 20 53 51 4c 69 74 65 0a 2a 2a  le for SQLite.**
18fac 20 67 65 6e 65 72 61 74 65 73 20 74 77 6f 20 43   generates two C
18fad 20 66 69 6c 65 73 20 22 6f 70 63 6f 64 65 73 2e   files "opcodes.
18fae 68 22 20 61 6e 64 20 22 6f 70 63 6f 64 65 73 2e  h" and "opcodes.
18faf 63 22 20 62 79 20 73 63 61 6e 6e 69 6e 67 20 74  c" by scanning t
18fb0 68 69 73 0a 2a 2a 20 66 69 6c 65 20 6c 6f 6f 6b  his.** file look
18fb1 69 6e 67 20 66 6f 72 20 6c 69 6e 65 73 20 74 68  ing for lines th
18fb2 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 22 63  at begin with "c
18fb3 61 73 65 20 4f 50 5f 22 2e 20 20 54 68 65 20 6f  ase OP_".  The o
18fb4 70 63 6f 64 65 73 2e 68 20 66 69 6c 65 73 0a 2a  pcodes.h files.*
18fb5 2a 20 77 69 6c 6c 20 62 65 20 66 69 6c 6c 65 64  * will be filled
18fb6 20 77 69 74 68 20 23 64 65 66 69 6e 65 73 20 74   with #defines t
18fb7 68 61 74 20 67 69 76 65 20 75 6e 69 71 75 65 20  hat give unique 
18fb8 69 6e 74 65 67 65 72 20 76 61 6c 75 65 73 20 74  integer values t
18fb9 6f 20 65 61 63 68 0a 2a 2a 20 6f 70 63 6f 64 65  o each.** opcode
18fba 20 61 6e 64 20 74 68 65 20 6f 70 63 6f 64 65 73   and the opcodes
18fbb 2e 63 20 66 69 6c 65 20 69 73 20 66 69 6c 6c 65  .c file is fille
18fbc 64 20 77 69 74 68 20 61 6e 20 61 72 72 61 79 20  d with an array 
18fbd 6f 66 20 73 74 72 69 6e 67 73 20 77 68 65 72 65  of strings where
18fbe 0a 2a 2a 20 65 61 63 68 20 73 74 72 69 6e 67 20  .** each string 
18fbf 69 73 20 74 68 65 20 73 79 6d 62 6f 6c 69 63 20  is the symbolic 
18fc0 6e 61 6d 65 20 66 6f 72 20 74 68 65 20 63 6f 72  name for the cor
18fc1 72 65 73 70 6f 6e 64 69 6e 67 20 6f 70 63 6f 64  responding opcod
18fc2 65 2e 20 20 49 66 20 74 68 65 0a 2a 2a 20 63 61  e.  If the.** ca
18fc3 73 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  se statement is 
18fc4 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 20 63 6f  followed by a co
18fc5 6d 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f 72  mment of the for
18fc6 6d 20 22 2f 23 20 73 61 6d 65 20 61 73 20 2e 2e  m "/# same as ..
18fc7 2e 20 23 2f 22 0a 2a 2a 20 74 68 61 74 20 63 6f  . #/".** that co
18fc8 6d 6d 65 6e 74 20 69 73 20 75 73 65 64 20 74 6f  mment is used to
18fc9 20 64 65 74 65 72 6d 69 6e 65 20 74 68 65 20 70   determine the p
18fca 61 72 74 69 63 75 6c 61 72 20 76 61 6c 75 65 20  articular value 
18fcb 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 2e 0a 2a  of the opcode..*
18fcc 2a 0a 2a 2a 20 4f 74 68 65 72 20 6b 65 79 77 6f  *.** Other keywo
18fcd 72 64 73 20 69 6e 20 74 68 65 20 63 6f 6d 6d 65  rds in the comme
18fce 6e 74 20 74 68 61 74 20 66 6f 6c 6c 6f 77 73 20  nt that follows 
18fcf 65 61 63 68 20 63 61 73 65 20 61 72 65 20 75 73  each case are us
18fd0 65 64 20 74 6f 0a 2a 2a 20 63 6f 6e 73 74 72 75  ed to.** constru
18fd1 63 74 20 74 68 65 20 4f 50 46 4c 47 5f 49 4e 49  ct the OPFLG_INI
18fd2 54 49 41 4c 49 5a 45 52 20 76 61 6c 75 65 20 74  TIALIZER value t
18fd3 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73 20  hat initializes 
18fd4 6f 70 63 6f 64 65 50 72 6f 70 65 72 74 79 5b 5d  opcodeProperty[]
18fd5 2e 0a 2a 2a 20 4b 65 79 77 6f 72 64 73 20 69 6e  ..** Keywords in
18fd6 63 6c 75 64 65 3a 20 69 6e 31 2c 20 69 6e 32 2c  clude: in1, in2,
18fd7 20 69 6e 33 2c 20 6f 75 74 32 5f 70 72 65 72 65   in3, out2_prere
18fd8 6c 65 61 73 65 2c 20 6f 75 74 32 2c 20 6f 75 74  lease, out2, out
18fd9 33 2e 20 20 53 65 65 0a 2a 2a 20 74 68 65 20 6d  3.  See.** the m
18fda 6b 6f 70 63 6f 64 65 68 2e 61 77 6b 20 73 63 72  kopcodeh.awk scr
18fdb 69 70 74 20 66 6f 72 20 61 64 64 69 74 69 6f 6e  ipt for addition
18fdc 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a  al information..
18fdd 2a 2a 0a 2a 2a 20 44 6f 63 75 6d 65 6e 74 61 74  **.** Documentat
18fde 69 6f 6e 20 61 62 6f 75 74 20 56 44 42 45 20 6f  ion about VDBE o
18fdf 70 63 6f 64 65 73 20 69 73 20 67 65 6e 65 72 61  pcodes is genera
18fe0 74 65 64 20 62 79 20 73 63 61 6e 6e 69 6e 67 20  ted by scanning 
18fe1 74 68 69 73 20 66 69 6c 65 0a 2a 2a 20 66 6f 72  this file.** for
18fe2 20 6c 69 6e 65 73 20 6f 66 20 74 68 61 74 20 63   lines of that c
18fe3 6f 6e 74 61 69 6e 20 22 4f 70 63 6f 64 65 3a 22  ontain "Opcode:"
18fe4 2e 20 20 54 68 61 74 20 6c 69 6e 65 20 61 6e 64  .  That line and
18fe5 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e 74 0a   all subsequent.
18fe6 2a 2a 20 63 6f 6d 6d 65 6e 74 20 6c 69 6e 65 73  ** comment lines
18fe7 20 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 65   are used in the
18fe8 20 67 65 6e 65 72 61 74 69 6f 6e 20 6f 66 20 74   generation of t
18fe9 68 65 20 6f 70 63 6f 64 65 2e 68 74 6d 6c 20 64  he opcode.html d
18fea 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20  ocumentation.** 
18feb 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 53 55 4d 4d  file..**.** SUMM
18fec 41 52 59 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 46  ARY:.**.**     F
18fed 6f 72 6d 61 74 74 69 6e 67 20 69 73 20 69 6d 70  ormatting is imp
18fee 6f 72 74 61 6e 74 20 74 6f 20 73 63 72 69 70 74  ortant to script
18fef 73 20 74 68 61 74 20 73 63 61 6e 20 74 68 69 73  s that scan this
18ff0 20 66 69 6c 65 2e 0a 2a 2a 20 20 20 20 20 44 6f   file..**     Do
18ff1 20 6e 6f 74 20 64 65 76 69 61 74 65 20 66 72 6f   not deviate fro
18ff2 6d 20 74 68 65 20 66 6f 72 6d 61 74 74 69 6e 67  m the formatting
18ff3 20 73 74 79 6c 65 20 63 75 72 72 65 6e 74 6c 79   style currently
18ff4 20 69 6e 20 75 73 65 2e 0a 2a 2a 0a 2a 2a 2a 2a   in use..**.****
18ff5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ff6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ff7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ff8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
18ff9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 2f 2a 20 4f  *********/../* O
18ffa 70 63 6f 64 65 3a 20 20 47 6f 74 6f 20 2a 20 50  pcode:  Goto * P
18ffb 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 6e  2 * * *.**.** An
18ffc 20 75 6e 63 6f 6e 64 69 74 69 6f 6e 61 6c 20 6a   unconditional j
18ffd 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73 20 50  ump to address P
18ffe 32 2e 0a 2a 2a 20 54 68 65 20 6e 65 78 74 20 69  2..** The next i
18fff 6e 73 74 72 75 63 74 69 6f 6e 20 65 78 65 63 75  nstruction execu
19000 74 65 64 20 77 69 6c 6c 20 62 65 20 0a 2a 2a 20  ted will be .** 
19001 74 68 65 20 6f 6e 65 20 61 74 20 69 6e 64 65 78  the one at index
19002 20 50 32 20 66 72 6f 6d 20 74 68 65 20 62 65 67   P2 from the beg
19003 69 6e 6e 69 6e 67 20 6f 66 0a 2a 2a 20 74 68 65  inning of.** the
19004 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 63 61 73   program..*/.cas
19005 65 20 4f 50 5f 47 6f 74 6f 3a 20 7b 20 20 20 20  e OP_Goto: {    
19006 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
19007 20 2a 2f 0a 20 20 43 48 45 43 4b 5f 46 4f 52 5f   */.  CHECK_FOR_
19008 49 4e 54 45 52 52 55 50 54 3b 0a 20 20 70 63 20  INTERRUPT;.  pc 
19009 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1900a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1900b 63 6f 64 65 3a 20 20 47 6f 73 75 62 20 50 31 20  code:  Gosub P1 
1900c 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
1900d 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
1900e 20 61 64 64 72 65 73 73 20 6f 6e 74 6f 20 72 65   address onto re
1900f 67 69 73 74 65 72 20 50 31 0a 2a 2a 20 61 6e 64  gister P1.** and
19010 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 61 64   then jump to ad
19011 64 72 65 73 73 20 50 32 2e 0a 2a 2f 0a 63 61 73  dress P2..*/.cas
19012 65 20 4f 50 5f 47 6f 73 75 62 3a 20 7b 20 20 20  e OP_Gosub: {   
19013 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70           /* jump
19014 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f   */.  assert( pO
19015 70 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73  p->p1>0 );.  ass
19016 65 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d  ert( pOp->p1<=p-
19017 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20  >nMem );.  pIn1 
19018 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
19019 70 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28  p1];.  assert( (
1901a 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1901b 4d 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70  M_Dyn)==0 );.  p
1901c 49 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  In1->flags = MEM
1901d 5f 49 6e 74 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e  _Int;.  pIn1->u.
1901e 69 20 3d 20 70 63 3b 0a 20 20 52 45 47 49 53 54  i = pc;.  REGIST
1901f 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 31  ER_TRACE(pOp->p1
19020 2c 20 70 49 6e 31 29 3b 0a 20 20 70 63 20 3d 20  , pIn1);.  pc = 
19021 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 62  pOp->p2 - 1;.  b
19022 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
19023 64 65 3a 20 20 52 65 74 75 72 6e 20 50 31 20 2a  de:  Return P1 *
19024 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d   * * *.**.** Jum
19025 70 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69 6e  p to the next in
19026 73 74 72 75 63 74 69 6f 6e 20 61 66 74 65 72 20  struction after 
19027 74 68 65 20 61 64 64 72 65 73 73 20 69 6e 20 72  the address in r
19028 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2f 0a 63  egister P1..*/.c
19029 61 73 65 20 4f 50 5f 52 65 74 75 72 6e 3a 20 7b  ase OP_Return: {
1902a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
1902b 31 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70  1 */.  assert( p
1902c 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1902d 5f 49 6e 74 20 29 3b 0a 20 20 70 63 20 3d 20 70  _Int );.  pc = p
1902e 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 62 72 65 61  In1->u.i;.  brea
1902f 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
19030 20 20 59 69 65 6c 64 20 50 31 20 2a 20 2a 20 2a    Yield P1 * * *
19031 20 2a 0a 2a 2a 0a 2a 2a 20 53 77 61 70 20 74 68   *.**.** Swap th
19032 65 20 70 72 6f 67 72 61 6d 20 63 6f 75 6e 74 65  e program counte
19033 72 20 77 69 74 68 20 74 68 65 20 76 61 6c 75 65  r with the value
19034 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 2e   in register P1.
19035 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 59 69 65 6c  .*/.case OP_Yiel
19036 64 3a 20 7b 0a 20 20 69 6e 74 20 70 63 44 65 73  d: {.  int pcDes
19037 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  t;.  assert( pOp
19038 2d 3e 70 31 3e 30 20 29 3b 0a 20 20 61 73 73 65  ->p1>0 );.  asse
19039 72 74 28 20 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e  rt( pOp->p1<=p->
1903a 6e 4d 65 6d 20 29 3b 0a 20 20 70 49 6e 31 20 3d  nMem );.  pIn1 =
1903b 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1903c 31 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70  1];.  assert( (p
1903d 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
1903e 5f 44 79 6e 29 3d 3d 30 20 29 3b 0a 20 20 70 49  _Dyn)==0 );.  pI
1903f 6e 31 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  n1->flags = MEM_
19040 49 6e 74 3b 0a 20 20 70 63 44 65 73 74 20 3d 20  Int;.  pcDest = 
19041 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 70 49 6e  pIn1->u.i;.  pIn
19042 31 2d 3e 75 2e 69 20 3d 20 70 63 3b 0a 20 20 52  1->u.i = pc;.  R
19043 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f  EGISTER_TRACE(pO
19044 70 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20  p->p1, pIn1);.  
19045 70 63 20 3d 20 70 63 44 65 73 74 3b 0a 20 20 62  pc = pcDest;.  b
19046 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a 20 4f 70 63  reak;.}.../* Opc
19047 6f 64 65 3a 20 20 48 61 6c 74 20 50 31 20 50 32  ode:  Halt P1 P2
19048 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 45 78   * P4 *.**.** Ex
19049 69 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 2e 20  it immediately. 
1904a 20 41 6c 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72   All open cursor
1904b 73 2c 20 46 69 66 6f 73 2c 20 65 74 63 20 61 72  s, Fifos, etc ar
1904c 65 20 63 6c 6f 73 65 64 0a 2a 2a 20 61 75 74 6f  e closed.** auto
1904d 6d 61 74 69 63 61 6c 6c 79 2e 0a 2a 2a 0a 2a 2a  matically..**.**
1904e 20 50 31 20 69 73 20 74 68 65 20 72 65 73 75 6c   P1 is the resul
1904f 74 20 63 6f 64 65 20 72 65 74 75 72 6e 65 64 20  t code returned 
19050 62 79 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28  by sqlite3_exec(
19051 29 2c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  ), sqlite3_reset
19052 28 29 2c 0a 2a 2a 20 6f 72 20 73 71 6c 69 74 65  (),.** or sqlite
19053 33 5f 66 69 6e 61 6c 69 7a 65 28 29 2e 20 20 46  3_finalize().  F
19054 6f 72 20 61 20 6e 6f 72 6d 61 6c 20 68 61 6c 74  or a normal halt
19055 2c 20 74 68 69 73 20 73 68 6f 75 6c 64 20 62 65  , this should be
19056 20 53 51 4c 49 54 45 5f 4f 4b 20 28 30 29 2e 0a   SQLITE_OK (0)..
19057 2a 2a 20 46 6f 72 20 65 72 72 6f 72 73 2c 20 69  ** For errors, i
19058 74 20 63 61 6e 20 62 65 20 73 6f 6d 65 20 6f 74  t can be some ot
19059 68 65 72 20 76 61 6c 75 65 2e 20 20 49 66 20 50  her value.  If P
1905a 31 21 3d 30 20 74 68 65 6e 20 50 32 20 77 69 6c  1!=0 then P2 wil
1905b 6c 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20 77  l determine.** w
1905c 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 6f  hether or not to
1905d 20 72 6f 6c 6c 62 61 63 6b 20 74 68 65 20 63 75   rollback the cu
1905e 72 72 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f  rrent transactio
1905f 6e 2e 20 20 44 6f 20 6e 6f 74 20 72 6f 6c 6c 62  n.  Do not rollb
19060 61 63 6b 0a 2a 2a 20 69 66 20 50 32 3d 3d 4f 45  ack.** if P2==OE
19061 5f 46 61 69 6c 2e 20 44 6f 20 74 68 65 20 72 6f  _Fail. Do the ro
19062 6c 6c 62 61 63 6b 20 69 66 20 50 32 3d 3d 4f 45  llback if P2==OE
19063 5f 52 6f 6c 6c 62 61 63 6b 2e 20 20 49 66 20 50  _Rollback.  If P
19064 32 3d 3d 4f 45 5f 41 62 6f 72 74 2c 0a 2a 2a 20  2==OE_Abort,.** 
19065 74 68 65 6e 20 62 61 63 6b 20 6f 75 74 20 61 6c  then back out al
19066 6c 20 63 68 61 6e 67 65 73 20 74 68 61 74 20 68  l changes that h
19067 61 76 65 20 6f 63 63 75 72 72 65 64 20 64 75 72  ave occurred dur
19068 69 6e 67 20 74 68 69 73 20 65 78 65 63 75 74 69  ing this executi
19069 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 56 44 42  on of the.** VDB
1906a 45 2c 20 62 75 74 20 64 6f 20 6e 6f 74 20 72 6f  E, but do not ro
1906b 6c 6c 62 61 63 6b 20 74 68 65 20 74 72 61 6e 73  llback the trans
1906c 61 63 74 69 6f 6e 2e 20 0a 2a 2a 0a 2a 2a 20 49  action. .**.** I
1906d 66 20 50 34 20 69 73 20 6e 6f 74 20 6e 75 6c 6c  f P4 is not null
1906e 20 74 68 65 6e 20 69 74 20 69 73 20 61 6e 20 65   then it is an e
1906f 72 72 6f 72 20 6d 65 73 73 61 67 65 20 73 74 72  rror message str
19070 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  ing..**.** There
19071 20 69 73 20 61 6e 20 69 6d 70 6c 69 65 64 20 22   is an implied "
19072 48 61 6c 74 20 30 20 30 20 30 22 20 69 6e 73 74  Halt 0 0 0" inst
19073 72 75 63 74 69 6f 6e 20 69 6e 73 65 72 74 65 64  ruction inserted
19074 20 61 74 20 74 68 65 20 76 65 72 79 20 65 6e 64   at the very end
19075 20 6f 66 0a 2a 2a 20 65 76 65 72 79 20 70 72 6f   of.** every pro
19076 67 72 61 6d 2e 20 20 53 6f 20 61 20 6a 75 6d 70  gram.  So a jump
19077 20 70 61 73 74 20 74 68 65 20 6c 61 73 74 20 69   past the last i
19078 6e 73 74 72 75 63 74 69 6f 6e 20 6f 66 20 74 68  nstruction of th
19079 65 20 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 73 20  e program.** is 
1907a 74 68 65 20 73 61 6d 65 20 61 73 20 65 78 65 63  the same as exec
1907b 75 74 69 6e 67 20 48 61 6c 74 2e 0a 2a 2f 0a 63  uting Halt..*/.c
1907c 61 73 65 20 4f 50 5f 48 61 6c 74 3a 20 7b 0a 20  ase OP_Halt: {. 
1907d 20 70 2d 3e 72 63 20 3d 20 70 4f 70 2d 3e 70 31   p->rc = pOp->p1
1907e 3b 0a 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b 0a  ;.  p->pc = pc;.
1907f 20 20 70 2d 3e 65 72 72 6f 72 41 63 74 69 6f 6e    p->errorAction
19080 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 66   = pOp->p2;.  if
19081 28 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20  ( pOp->p4.z ){. 
19082 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
19083 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c  ing(&p->zErrMsg,
19084 20 64 62 2c 20 22 25 73 22 2c 20 70 4f 70 2d 3e   db, "%s", pOp->
19085 70 34 2e 7a 29 3b 0a 20 20 7d 0a 20 20 72 63 20  p4.z);.  }.  rc 
19086 3d 20 73 71 6c 69 74 65 33 56 64 62 65 48 61 6c  = sqlite3VdbeHal
19087 74 28 70 29 3b 0a 20 20 61 73 73 65 72 74 28 20  t(p);.  assert( 
19088 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
19089 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  || rc==SQLITE_OK
1908a 20 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   );.  if( rc==SQ
1908b 4c 49 54 45 5f 42 55 53 59 20 29 7b 0a 20 20 20  LITE_BUSY ){.   
1908c 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51   p->rc = rc = SQ
1908d 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c  LITE_BUSY;.  }el
1908e 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 70 2d 3e  se{.    rc = p->
1908f 72 63 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f  rc ? SQLITE_ERRO
19090 52 20 3a 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  R : SQLITE_DONE;
19091 0a 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65  .  }.  goto vdbe
19092 5f 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f  _return;.}../* O
19093 70 63 6f 64 65 3a 20 49 6e 74 65 67 65 72 20 50  pcode: Integer P
19094 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
19095 20 54 68 65 20 33 32 2d 62 69 74 20 69 6e 74 65   The 32-bit inte
19096 67 65 72 20 76 61 6c 75 65 20 50 31 20 69 73 20  ger value P1 is 
19097 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 72 65 67  written into reg
19098 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73  ister P2..*/.cas
19099 65 20 4f 50 5f 49 6e 74 65 67 65 72 3a 20 7b 20  e OP_Integer: { 
1909a 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
1909b 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
1909c 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45  pOut->flags = ME
1909d 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e 75  M_Int;.  pOut->u
1909e 2e 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20  .i = pOp->p1;.  
1909f 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
190a0 6f 64 65 3a 20 49 6e 74 36 34 20 2a 20 50 32 20  ode: Int64 * P2 
190a1 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20  * P4 *.**.** P4 
190a2 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
190a3 61 20 36 34 2d 62 69 74 20 69 6e 74 65 67 65 72  a 64-bit integer
190a4 20 76 61 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65   value..** Write
190a5 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 74 6f   that value into
190a6 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f   register P2..*/
190a7 0a 63 61 73 65 20 4f 50 5f 49 6e 74 36 34 3a 20  .case OP_Int64: 
190a8 7b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6f  {           /* o
190a9 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a  ut2-prerelease *
190aa 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  /.  assert( pOp-
190ab 3e 70 34 2e 70 49 36 34 21 3d 30 20 29 3b 0a 20  >p4.pI64!=0 );. 
190ac 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d   pOut->flags = M
190ad 45 4d 5f 49 6e 74 3b 0a 20 20 70 4f 75 74 2d 3e  EM_Int;.  pOut->
190ae 75 2e 69 20 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70  u.i = *pOp->p4.p
190af 49 36 34 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  I64;.  break;.}.
190b0 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 6c  ./* Opcode: Real
190b1 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a   * P2 * P4 *.**.
190b2 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
190b3 65 72 20 74 6f 20 61 20 36 34 2d 62 69 74 20 66  er to a 64-bit f
190b4 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 20 76 61  loating point va
190b5 6c 75 65 2e 0a 2a 2a 20 57 72 69 74 65 20 74 68  lue..** Write th
190b6 61 74 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65  at value into re
190b7 67 69 73 74 65 72 20 50 32 2e 0a 2a 2f 0a 63 61  gister P2..*/.ca
190b8 73 65 20 4f 50 5f 52 65 61 6c 3a 20 7b 20 20 20  se OP_Real: {   
190b9 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65           /* same
190ba 20 61 73 20 54 4b 5f 46 4c 4f 41 54 2c 20 6f 75   as TK_FLOAT, ou
190bb 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
190bc 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d  .  pOut->flags =
190bd 20 4d 45 4d 5f 52 65 61 6c 3b 0a 20 20 61 73 73   MEM_Real;.  ass
190be 65 72 74 28 20 21 73 71 6c 69 74 65 33 49 73 4e  ert( !sqlite3IsN
190bf 61 4e 28 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61  aN(*pOp->p4.pRea
190c0 6c 29 20 29 3b 0a 20 20 70 4f 75 74 2d 3e 72 20  l) );.  pOut->r 
190c1 3d 20 2a 70 4f 70 2d 3e 70 34 2e 70 52 65 61 6c  = *pOp->p4.pReal
190c2 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
190c3 20 4f 70 63 6f 64 65 3a 20 53 74 72 69 6e 67 38   Opcode: String8
190c4 20 2a 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a   * P2 * P4 *.**.
190c5 2a 2a 20 50 34 20 70 6f 69 6e 74 73 20 74 6f 20  ** P4 points to 
190c6 61 20 6e 75 6c 20 74 65 72 6d 69 6e 61 74 65 64  a nul terminated
190c7 20 55 54 46 2d 38 20 73 74 72 69 6e 67 2e 20 54   UTF-8 string. T
190c8 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20 74 72  his opcode is tr
190c9 61 6e 73 66 6f 72 6d 65 64 20 0a 2a 2a 20 69 6e  ansformed .** in
190ca 74 6f 20 61 6e 20 4f 50 5f 53 74 72 69 6e 67 20  to an OP_String 
190cb 62 65 66 6f 72 65 20 69 74 20 69 73 20 65 78 65  before it is exe
190cc 63 75 74 65 64 20 66 6f 72 20 74 68 65 20 66 69  cuted for the fi
190cd 72 73 74 20 74 69 6d 65 2e 0a 2a 2f 0a 63 61 73  rst time..*/.cas
190ce 65 20 4f 50 5f 53 74 72 69 6e 67 38 3a 20 7b 20  e OP_String8: { 
190cf 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20          /* same 
190d0 61 73 20 54 4b 5f 53 54 52 49 4e 47 2c 20 6f 75  as TK_STRING, ou
190d1 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20 2a 2f  t2-prerelease */
190d2 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e  .  assert( pOp->
190d3 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70 4f 70  p4.z!=0 );.  pOp
190d4 2d 3e 6f 70 63 6f 64 65 20 3d 20 4f 50 5f 53 74  ->opcode = OP_St
190d5 72 69 6e 67 3b 0a 20 20 70 4f 70 2d 3e 70 31 20  ring;.  pOp->p1 
190d6 3d 20 73 74 72 6c 65 6e 28 70 4f 70 2d 3e 70 34  = strlen(pOp->p4
190d7 2e 7a 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  .z);..#ifndef SQ
190d8 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a  LITE_OMIT_UTF16.
190d9 20 20 69 66 28 20 65 6e 63 6f 64 69 6e 67 21 3d    if( encoding!=
190da 53 51 4c 49 54 45 5f 55 54 46 38 20 29 7b 0a 20  SQLITE_UTF8 ){. 
190db 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
190dc 6d 53 65 74 53 74 72 28 70 4f 75 74 2c 20 70 4f  mSetStr(pOut, pO
190dd 70 2d 3e 70 34 2e 7a 2c 20 2d 31 2c 20 53 51 4c  p->p4.z, -1, SQL
190de 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45  ITE_UTF8, SQLITE
190df 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 69 66  _STATIC);.    if
190e0 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c  ( SQLITE_OK!=sql
190e1 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e  ite3VdbeChangeEn
190e2 63 6f 64 69 6e 67 28 70 4f 75 74 2c 20 65 6e 63  coding(pOut, enc
190e3 6f 64 69 6e 67 29 20 29 20 67 6f 74 6f 20 6e 6f  oding) ) goto no
190e4 5f 6d 65 6d 3b 0a 20 20 20 20 69 66 28 20 53 51  _mem;.    if( SQ
190e5 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
190e6 56 64 62 65 4d 65 6d 4d 61 6b 65 57 72 69 74 65  VdbeMemMakeWrite
190e7 61 62 6c 65 28 70 4f 75 74 29 20 29 20 67 6f 74  able(pOut) ) got
190e8 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 20 20 70 4f  o no_mem;.    pO
190e9 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b  ut->zMalloc = 0;
190ea 0a 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73  .    pOut->flags
190eb 20 7c 3d 20 4d 45 4d 5f 53 74 61 74 69 63 3b 0a   |= MEM_Static;.
190ec 20 20 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20      pOut->flags 
190ed 26 3d 20 7e 4d 45 4d 5f 44 79 6e 3b 0a 20 20 20  &= ~MEM_Dyn;.   
190ee 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79 70 65   if( pOp->p4type
190ef 3d 3d 50 34 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  ==P4_DYNAMIC ){.
190f0 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
190f1 72 65 65 28 64 62 2c 20 70 4f 70 2d 3e 70 34 2e  ree(db, pOp->p4.
190f2 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4f  z);.    }.    pO
190f3 70 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 44  p->p4type = P4_D
190f4 59 4e 41 4d 49 43 3b 0a 20 20 20 20 70 4f 70 2d  YNAMIC;.    pOp-
190f5 3e 70 34 2e 7a 20 3d 20 70 4f 75 74 2d 3e 7a 3b  >p4.z = pOut->z;
190f6 0a 20 20 20 20 70 4f 70 2d 3e 70 31 20 3d 20 70  .    pOp->p1 = p
190f7 4f 75 74 2d 3e 6e 3b 0a 20 20 20 20 69 66 28 20  Out->n;.    if( 
190f8 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d  pOp->p1>db->aLim
190f9 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
190fa 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20  LENGTH] ){.     
190fb 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
190fc 20 20 20 7d 0a 20 20 20 20 55 50 44 41 54 45 5f     }.    UPDATE_
190fd 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f 75  MAX_BLOBSIZE(pOu
190fe 74 29 3b 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20  t);.    break;. 
190ff 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
19100 70 4f 70 2d 3e 70 31 3e 64 62 2d 3e 61 4c 69 6d  pOp->p1>db->aLim
19101 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
19102 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67  LENGTH] ){.    g
19103 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
19104 0a 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75  .  /* Fall throu
19105 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63  gh to the next c
19106 61 73 65 2c 20 4f 50 5f 53 74 72 69 6e 67 20 2a  ase, OP_String *
19107 2f 0a 7d 0a 20 20 0a 2f 2a 20 4f 70 63 6f 64 65  /.}.  ./* Opcode
19108 3a 20 53 74 72 69 6e 67 20 50 31 20 50 32 20 2a  : String P1 P2 *
19109 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20   P4 *.**.** The 
1910a 73 74 72 69 6e 67 20 76 61 6c 75 65 20 50 34 20  string value P4 
1910b 6f 66 20 6c 65 6e 67 74 68 20 50 31 20 28 62 79  of length P1 (by
1910c 74 65 73 29 20 69 73 20 73 74 6f 72 65 64 20 69  tes) is stored i
1910d 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  n register P2..*
1910e 2f 0a 63 61 73 65 20 4f 50 5f 53 74 72 69 6e 67  /.case OP_String
1910f 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  : {          /* 
19110 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
19111 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  */.  assert( pOp
19112 2d 3e 70 34 2e 7a 21 3d 30 20 29 3b 0a 20 20 70  ->p4.z!=0 );.  p
19113 4f 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d  Out->flags = MEM
19114 5f 53 74 72 7c 4d 45 4d 5f 53 74 61 74 69 63 7c  _Str|MEM_Static|
19115 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 70 4f 75 74  MEM_Term;.  pOut
19116 2d 3e 7a 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  ->z = pOp->p4.z;
19117 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 70 4f 70  .  pOut->n = pOp
19118 2d 3e 70 31 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e  ->p1;.  pOut->en
19119 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20  c = encoding;.  
1911a 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53  UPDATE_MAX_BLOBS
1911b 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65  IZE(pOut);.  bre
1911c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1911d 3a 20 4e 75 6c 6c 20 2a 20 50 32 20 2a 20 2a 20  : Null * P2 * * 
1911e 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 61 20  *.**.** Write a 
1911f 4e 55 4c 4c 20 69 6e 74 6f 20 72 65 67 69 73 74  NULL into regist
19120 65 72 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f  er P2..*/.case O
19121 50 5f 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20  P_Null: {       
19122 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
19123 65 6c 65 61 73 65 20 2a 2f 0a 20 20 62 72 65 61  elease */.  brea
19124 6b 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20 53  k;.}...#ifndef S
19125 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f  QLITE_OMIT_BLOB_
19126 4c 49 54 45 52 41 4c 0a 2f 2a 20 4f 70 63 6f 64  LITERAL./* Opcod
19127 65 3a 20 42 6c 6f 62 20 50 31 20 50 32 20 2a 20  e: Blob P1 P2 * 
19128 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 70 6f 69 6e  P4.**.** P4 poin
19129 74 73 20 74 6f 20 61 20 62 6c 6f 62 20 6f 66 20  ts to a blob of 
1912a 64 61 74 61 20 50 31 20 62 79 74 65 73 20 6c 6f  data P1 bytes lo
1912b 6e 67 2e 20 20 53 74 6f 72 65 20 74 68 69 73 0a  ng.  Store this.
1912c 2a 2a 20 62 6c 6f 62 20 69 6e 20 72 65 67 69 73  ** blob in regis
1912d 74 65 72 20 50 32 2e 20 54 68 69 73 20 69 6e 73  ter P2. This ins
1912e 74 72 75 63 74 69 6f 6e 20 69 73 20 6e 6f 74 20  truction is not 
1912f 63 6f 64 65 64 20 64 69 72 65 63 74 6c 79 0a 2a  coded directly.*
19130 2a 20 62 79 20 74 68 65 20 63 6f 6d 70 69 6c 65  * by the compile
19131 72 2e 20 49 6e 73 74 65 61 64 2c 20 74 68 65 20  r. Instead, the 
19132 63 6f 6d 70 69 6c 65 72 20 6c 61 79 65 72 20 73  compiler layer s
19133 70 65 63 69 66 69 65 73 0a 2a 2a 20 61 6e 20 4f  pecifies.** an O
19134 50 5f 48 65 78 42 6c 6f 62 20 6f 70 63 6f 64 65  P_HexBlob opcode
19135 2c 20 77 69 74 68 20 74 68 65 20 68 65 78 20 73  , with the hex s
19136 74 72 69 6e 67 20 72 65 70 72 65 73 65 6e 74 61  tring representa
19137 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 62  tion of.** the b
19138 6c 6f 62 20 61 73 20 50 34 2e 20 54 68 69 73 20  lob as P4. This 
19139 6f 70 63 6f 64 65 20 69 73 20 74 72 61 6e 73 66  opcode is transf
1913a 6f 72 6d 65 64 20 74 6f 20 61 6e 20 4f 50 5f 42  ormed to an OP_B
1913b 6c 6f 62 0a 2a 2a 20 74 68 65 20 66 69 72 73 74  lob.** the first
1913c 20 74 69 6d 65 20 69 74 20 69 73 20 65 78 65 63   time it is exec
1913d 75 74 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  uted..*/.case OP
1913e 5f 42 6c 6f 62 3a 20 7b 20 20 20 20 20 20 20 20  _Blob: {        
1913f 20 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d          /* out2-
19140 70 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20  prerelease */.  
19141 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31 20  assert( pOp->p1 
19142 3c 3d 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45  <= SQLITE_MAX_LE
19143 4e 47 54 48 20 29 3b 0a 20 20 73 71 6c 69 74 65  NGTH );.  sqlite
19144 33 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70  3VdbeMemSetStr(p
19145 4f 75 74 2c 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20  Out, pOp->p4.z, 
19146 70 4f 70 2d 3e 70 31 2c 20 30 2c 20 30 29 3b 0a  pOp->p1, 0, 0);.
19147 20 20 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 65 6e    pOut->enc = en
19148 63 6f 64 69 6e 67 3b 0a 20 20 55 50 44 41 54 45  coding;.  UPDATE
19149 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4f  _MAX_BLOBSIZE(pO
1914a 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ut);.  break;.}.
1914b 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1914c 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c 49 54 45 52  _OMIT_BLOB_LITER
1914d 41 4c 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64 65  AL */../* Opcode
1914e 3a 20 56 61 72 69 61 62 6c 65 20 50 31 20 50 32  : Variable P1 P2
1914f 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65   * * *.**.** The
19150 20 76 61 6c 75 65 20 6f 66 20 76 61 72 69 61 62   value of variab
19151 6c 65 20 50 31 20 69 73 20 77 72 69 74 74 65 6e  le P1 is written
19152 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50   into register P
19153 32 2e 20 41 20 76 61 72 69 61 62 6c 65 20 69 73  2. A variable is
19154 0a 2a 2a 20 61 6e 20 75 6e 6b 6e 6f 77 6e 20 69  .** an unknown i
19155 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53  n the original S
19156 51 4c 20 73 74 72 69 6e 67 20 61 73 20 68 61 6e  QL string as han
19157 64 65 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 63  ded to sqlite3_c
19158 6f 6d 70 69 6c 65 28 29 2e 0a 2a 2a 20 41 6e 79  ompile()..** Any
19159 20 6f 63 63 75 72 72 65 6e 63 65 20 6f 66 20 74   occurrence of t
1915a 68 65 20 27 3f 27 20 63 68 61 72 61 63 74 65 72  he '?' character
1915b 20 69 6e 20 74 68 65 20 6f 72 69 67 69 6e 61 6c   in the original
1915c 20 53 51 4c 20 69 73 20 63 6f 6e 73 69 64 65 72   SQL is consider
1915d 65 64 0a 2a 2a 20 61 20 76 61 72 69 61 62 6c 65  ed.** a variable
1915e 2e 20 20 56 61 72 69 61 62 6c 65 73 20 69 6e 20  .  Variables in 
1915f 74 68 65 20 53 51 4c 20 73 74 72 69 6e 67 20 61  the SQL string a
19160 72 65 20 6e 75 6d 62 65 72 20 66 72 6f 6d 20 6c  re number from l
19161 65 66 74 20 74 6f 0a 2a 2a 20 72 69 67 68 74 20  eft to.** right 
19162 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 31  beginning with 1
19163 2e 20 20 54 68 65 20 76 61 6c 75 65 73 20 6f 66  .  The values of
19164 20 76 61 72 69 61 62 6c 65 73 20 61 72 65 20 73   variables are s
19165 65 74 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20  et using the.** 
19166 73 71 6c 69 74 65 33 5f 62 69 6e 64 28 29 20 41  sqlite3_bind() A
19167 50 49 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56  PI..*/.case OP_V
19168 61 72 69 61 62 6c 65 3a 20 7b 20 20 20 20 20 20  ariable: {      
19169 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65       /* out2-pre
1916a 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74  release */.  int
1916b 20 6a 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31   j = pOp->p1 - 1
1916c 3b 0a 20 20 4d 65 6d 20 2a 70 56 61 72 3b 0a 20  ;.  Mem *pVar;. 
1916d 20 61 73 73 65 72 74 28 20 6a 3e 3d 30 20 26 26   assert( j>=0 &&
1916e 20 6a 3c 70 2d 3e 6e 56 61 72 20 29 3b 0a 0a 20   j<p->nVar );.. 
1916f 20 70 56 61 72 20 3d 20 26 70 2d 3e 61 56 61 72   pVar = &p->aVar
19170 5b 6a 5d 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  [j];.  if( sqlit
19171 65 33 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28  e3VdbeMemTooBig(
19172 70 56 61 72 29 20 29 7b 0a 20 20 20 20 67 6f 74  pVar) ){.    got
19173 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20  o too_big;.  }. 
19174 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53   sqlite3VdbeMemS
19175 68 61 6c 6c 6f 77 43 6f 70 79 28 70 4f 75 74 2c  hallowCopy(pOut,
19176 20 26 70 2d 3e 61 56 61 72 5b 6a 5d 2c 20 4d 45   &p->aVar[j], ME
19177 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 55 50 44  M_Static);.  UPD
19178 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
19179 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
1917a 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4d  .}../* Opcode: M
1917b 6f 76 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a  ove P1 P2 P3 * *
1917c 0a 2a 2a 0a 2a 2a 20 4d 6f 76 65 20 74 68 65 20  .**.** Move the 
1917d 76 61 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74  values in regist
1917e 65 72 20 50 31 2e 2e 50 31 2b 50 33 2d 31 20 6f  er P1..P1+P3-1 o
1917f 76 65 72 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69  ver into.** regi
19180 73 74 65 72 73 20 50 32 2e 2e 50 32 2b 50 33 2d  sters P2..P2+P3-
19181 31 2e 20 20 52 65 67 69 73 74 65 72 73 20 50 31  1.  Registers P1
19182 2e 2e 50 31 2b 50 31 2d 31 20 61 72 65 0a 2a 2a  ..P1+P1-1 are.**
19183 20 6c 65 66 74 20 68 6f 6c 64 69 6e 67 20 61 20   left holding a 
19184 4e 55 4c 4c 2e 20 20 49 74 20 69 73 20 61 6e 20  NULL.  It is an 
19185 65 72 72 6f 72 20 66 6f 72 20 72 65 67 69 73 74  error for regist
19186 65 72 20 72 61 6e 67 65 73 0a 2a 2a 20 50 31 2e  er ranges.** P1.
19187 2e 50 31 2b 50 33 2d 31 20 61 6e 64 20 50 32 2e  .P1+P3-1 and P2.
19188 2e 50 32 2b 50 33 2d 31 20 74 6f 20 6f 76 65 72  .P2+P3-1 to over
19189 6c 61 70 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  lap..*/.case OP_
1918a 4d 6f 76 65 3a 20 7b 0a 20 20 63 68 61 72 20 2a  Move: {.  char *
1918b 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20 6e  zMalloc;.  int n
1918c 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 69 6e   = pOp->p3;.  in
1918d 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  t p1 = pOp->p1;.
1918e 20 20 69 6e 74 20 70 32 20 3d 20 70 4f 70 2d 3e    int p2 = pOp->
1918f 70 32 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  p2;.  assert( n>
19190 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
19191 31 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  1>0 );.  assert(
19192 20 70 31 2b 6e 3c 70 2d 3e 6e 4d 65 6d 20 29 3b   p1+n<p->nMem );
19193 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d  .  pIn1 = &p->aM
19194 65 6d 5b 70 31 5d 3b 0a 20 20 61 73 73 65 72 74  em[p1];.  assert
19195 28 20 70 32 3e 30 20 29 3b 0a 20 20 61 73 73 65  ( p2>0 );.  asse
19196 72 74 28 20 70 32 2b 6e 3c 70 2d 3e 6e 4d 65 6d  rt( p2+n<p->nMem
19197 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d   );.  pOut = &p-
19198 3e 61 4d 65 6d 5b 70 32 5d 3b 0a 20 20 61 73 73  >aMem[p2];.  ass
19199 65 72 74 28 20 70 31 2b 6e 3c 3d 70 32 20 7c 7c  ert( p1+n<=p2 ||
1919a 20 70 32 2b 6e 3c 3d 70 31 20 29 3b 0a 20 20 77   p2+n<=p1 );.  w
1919b 68 69 6c 65 28 20 6e 2d 2d 20 29 7b 0a 20 20 20  hile( n-- ){.   
1919c 20 7a 4d 61 6c 6c 6f 63 20 3d 20 70 4f 75 74 2d   zMalloc = pOut-
1919d 3e 7a 4d 61 6c 6c 6f 63 3b 0a 20 20 20 20 70 4f  >zMalloc;.    pO
1919e 75 74 2d 3e 7a 4d 61 6c 6c 6f 63 20 3d 20 30 3b  ut->zMalloc = 0;
1919f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
191a0 4d 65 6d 4d 6f 76 65 28 70 4f 75 74 2c 20 70 49  MemMove(pOut, pI
191a1 6e 31 29 3b 0a 20 20 20 20 70 49 6e 31 2d 3e 7a  n1);.    pIn1->z
191a2 4d 61 6c 6c 6f 63 20 3d 20 7a 4d 61 6c 6c 6f 63  Malloc = zMalloc
191a3 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54  ;.    REGISTER_T
191a4 52 41 43 45 28 70 32 2b 2b 2c 20 70 4f 75 74 29  RACE(p2++, pOut)
191a5 3b 0a 20 20 20 20 70 49 6e 31 2b 2b 3b 0a 20 20  ;.    pIn1++;.  
191a6 20 20 70 4f 75 74 2b 2b 3b 0a 20 20 7d 0a 20 20    pOut++;.  }.  
191a7 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
191a8 6f 64 65 3a 20 43 6f 70 79 20 50 31 20 50 32 20  ode: Copy P1 P2 
191a9 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4d 61 6b 65  * * *.**.** Make
191aa 20 61 20 63 6f 70 79 20 6f 66 20 72 65 67 69 73   a copy of regis
191ab 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67 69  ter P1 into regi
191ac 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54  ster P2..**.** T
191ad 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
191ae 6d 61 6b 65 73 20 61 20 64 65 65 70 20 63 6f 70  makes a deep cop
191af 79 20 6f 66 20 74 68 65 20 76 61 6c 75 65 2e 20  y of the value. 
191b0 20 41 20 64 75 70 6c 69 63 61 74 65 0a 2a 2a 20   A duplicate.** 
191b1 69 73 20 6d 61 64 65 20 6f 66 20 61 6e 79 20 73  is made of any s
191b2 74 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 63 6f  tring or blob co
191b3 6e 73 74 61 6e 74 2e 20 20 53 65 65 20 61 6c 73  nstant.  See als
191b4 6f 20 4f 50 5f 53 43 6f 70 79 2e 0a 2a 2f 0a 63  o OP_SCopy..*/.c
191b5 61 73 65 20 4f 50 5f 43 6f 70 79 3a 20 7b 0a 20  ase OP_Copy: {. 
191b6 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
191b7 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
191b8 70 4f 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d  pOp->p1<=p->nMem
191b9 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d   );.  pIn1 = &p-
191ba 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
191bb 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
191bc 32 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  2>0 );.  assert(
191bd 20 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65   pOp->p2<=p->nMe
191be 6d 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70  m );.  pOut = &p
191bf 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b  ->aMem[pOp->p2];
191c0 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74 21  .  assert( pOut!
191c1 3d 70 49 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74  =pIn1 );.  sqlit
191c2 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77  e3VdbeMemShallow
191c3 43 6f 70 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c  Copy(pOut, pIn1,
191c4 20 4d 45 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 44   MEM_Ephem);.  D
191c5 65 65 70 68 65 6d 65 72 61 6c 69 7a 65 28 70 4f  eephemeralize(pO
191c6 75 74 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f  ut);.  REGISTER_
191c7 54 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70  TRACE(pOp->p2, p
191c8 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Out);.  break;.}
191c9 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 43 6f  ../* Opcode: SCo
191ca 70 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a  py P1 P2 * * *.*
191cb 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 73 68 61 6c  *.** Make a shal
191cc 6c 6f 77 20 63 6f 70 79 20 6f 66 20 72 65 67 69  low copy of regi
191cd 73 74 65 72 20 50 31 20 69 6e 74 6f 20 72 65 67  ster P1 into reg
191ce 69 73 74 65 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20  ister P2..**.** 
191cf 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
191d0 20 6d 61 6b 65 73 20 61 20 73 68 61 6c 6c 6f 77   makes a shallow
191d1 20 63 6f 70 79 20 6f 66 20 74 68 65 20 76 61 6c   copy of the val
191d2 75 65 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75  ue.  If the valu
191d3 65 0a 2a 2a 20 69 73 20 61 20 73 74 72 69 6e 67  e.** is a string
191d4 20 6f 72 20 62 6c 6f 62 2c 20 74 68 65 6e 20 74   or blob, then t
191d5 68 65 20 63 6f 70 79 20 69 73 20 6f 6e 6c 79 20  he copy is only 
191d6 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
191d7 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20 61 6e 64  .** original and
191d8 20 68 65 6e 63 65 20 69 66 20 74 68 65 20 6f 72   hence if the or
191d9 69 67 69 6e 61 6c 20 63 68 61 6e 67 65 73 20 73  iginal changes s
191da 6f 20 77 69 6c 6c 20 74 68 65 20 63 6f 70 79 2e  o will the copy.
191db 0a 2a 2a 20 57 6f 72 73 65 2c 20 69 66 20 74 68  .** Worse, if th
191dc 65 20 6f 72 69 67 69 6e 61 6c 20 69 73 20 64 65  e original is de
191dd 61 6c 6c 6f 63 61 74 65 64 2c 20 74 68 65 20 63  allocated, the c
191de 6f 70 79 20 62 65 63 6f 6d 65 73 20 69 6e 76 61  opy becomes inva
191df 6c 69 64 2e 0a 2a 2a 20 54 68 75 73 20 74 68 65  lid..** Thus the
191e0 20 70 72 6f 67 72 61 6d 20 6d 75 73 74 20 67 75   program must gu
191e1 61 72 61 6e 74 65 65 20 74 68 61 74 20 74 68 65  arantee that the
191e2 20 6f 72 69 67 69 6e 61 6c 20 77 69 6c 6c 20 6e   original will n
191e3 6f 74 20 63 68 61 6e 67 65 0a 2a 2a 20 64 75 72  ot change.** dur
191e4 69 6e 67 20 74 68 65 20 6c 69 66 65 74 69 6d 65  ing the lifetime
191e5 20 6f 66 20 74 68 65 20 63 6f 70 79 2e 20 20 55   of the copy.  U
191e6 73 65 20 4f 50 5f 43 6f 70 79 20 74 6f 20 6d 61  se OP_Copy to ma
191e7 6b 65 20 61 20 63 6f 6d 70 6c 65 74 65 0a 2a 2a  ke a complete.**
191e8 20 63 6f 70 79 2e 0a 2a 2f 0a 63 61 73 65 20 4f   copy..*/.case O
191e9 50 5f 53 43 6f 70 79 3a 20 7b 0a 20 20 61 73 73  P_SCopy: {.  ass
191ea 65 72 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29  ert( pOp->p1>0 )
191eb 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
191ec 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
191ed 20 20 70 49 6e 31 20 3d 20 26 70 2d 3e 61 4d 65    pIn1 = &p->aMe
191ee 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 52 45  m[pOp->p1];.  RE
191ef 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
191f0 2d 3e 70 31 2c 20 70 49 6e 31 29 3b 0a 20 20 61  ->p1, pIn1);.  a
191f1 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30  ssert( pOp->p2>0
191f2 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f   );.  assert( pO
191f3 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p2<=p->nMem )
191f4 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
191f5 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20  Mem[pOp->p2];.  
191f6 61 73 73 65 72 74 28 20 70 4f 75 74 21 3d 70 49  assert( pOut!=pI
191f7 6e 31 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  n1 );.  sqlite3V
191f8 64 62 65 4d 65 6d 53 68 61 6c 6c 6f 77 43 6f 70  dbeMemShallowCop
191f9 79 28 70 4f 75 74 2c 20 70 49 6e 31 2c 20 4d 45  y(pOut, pIn1, ME
191fa 4d 5f 45 70 68 65 6d 29 3b 0a 20 20 52 45 47 49  M_Ephem);.  REGI
191fb 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
191fc 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 62 72 65  p2, pOut);.  bre
191fd 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
191fe 3a 20 52 65 73 75 6c 74 52 6f 77 20 50 31 20 50  : ResultRow P1 P
191ff 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68  2 * * *.**.** Th
19200 65 20 72 65 67 69 73 74 65 72 73 20 50 31 20 74  e registers P1 t
19201 68 72 6f 75 67 68 20 50 31 2b 50 32 2d 31 20 63  hrough P1+P2-1 c
19202 6f 6e 74 61 69 6e 20 61 20 73 69 6e 67 6c 65 20  ontain a single 
19203 72 6f 77 20 6f 66 0a 2a 2a 20 72 65 73 75 6c 74  row of.** result
19204 73 2e 20 54 68 69 73 20 6f 70 63 6f 64 65 20 63  s. This opcode c
19205 61 75 73 65 73 20 74 68 65 20 73 71 6c 69 74 65  auses the sqlite
19206 33 5f 73 74 65 70 28 29 20 63 61 6c 6c 20 74 6f  3_step() call to
19207 20 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 77 69   terminate.** wi
19208 74 68 20 61 6e 20 53 51 4c 49 54 45 5f 52 4f 57  th an SQLITE_ROW
19209 20 72 65 74 75 72 6e 20 63 6f 64 65 20 61 6e 64   return code and
1920a 20 69 74 20 73 65 74 73 20 75 70 20 74 68 65 20   it sets up the 
1920b 73 71 6c 69 74 65 33 5f 73 74 6d 74 0a 2a 2a 20  sqlite3_stmt.** 
1920c 73 74 72 75 63 74 75 72 65 20 74 6f 20 70 72 6f  structure to pro
1920d 76 69 64 65 20 61 63 63 65 73 73 20 74 6f 20 74  vide access to t
1920e 68 65 20 74 6f 70 20 50 31 20 76 61 6c 75 65 73  he top P1 values
1920f 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a 2a   as the result.*
19210 2a 20 72 6f 77 2e 0a 2a 2f 0a 63 61 73 65 20 4f  * row..*/.case O
19211 50 5f 52 65 73 75 6c 74 52 6f 77 3a 20 7b 0a 20  P_ResultRow: {. 
19212 20 4d 65 6d 20 2a 70 4d 65 6d 3b 0a 20 20 69 6e   Mem *pMem;.  in
19213 74 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t i;.  assert( p
19214 2d 3e 6e 52 65 73 43 6f 6c 75 6d 6e 3d 3d 70 4f  ->nResColumn==pO
19215 70 2d 3e 70 32 20 29 3b 0a 20 20 61 73 73 65 72  p->p2 );.  asser
19216 74 28 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a  t( pOp->p1>0 );.
19217 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
19218 31 2b 70 4f 70 2d 3e 70 32 3c 3d 70 2d 3e 6e 4d  1+pOp->p2<=p->nM
19219 65 6d 20 29 3b 0a 0a 20 20 2f 2a 20 49 6e 76 61  em );..  /* Inva
1921a 6c 69 64 61 74 65 20 61 6c 6c 20 65 70 68 65 6d  lidate all ephem
1921b 65 72 61 6c 20 63 75 72 73 6f 72 20 72 6f 77 20  eral cursor row 
1921c 63 61 63 68 65 73 20 2a 2f 0a 20 20 70 2d 3e 63  caches */.  p->c
1921d 61 63 68 65 43 74 72 20 3d 20 28 70 2d 3e 63 61  acheCtr = (p->ca
1921e 63 68 65 43 74 72 20 2b 20 32 29 7c 31 3b 0a 0a  cheCtr + 2)|1;..
1921f 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74    /* Make sure t
19220 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
19221 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 61 72  e current row ar
19222 65 20 5c 30 30 30 20 74 65 72 6d 69 6e 61 74 65  e \000 terminate
19223 64 0a 20 20 2a 2a 20 61 6e 64 20 68 61 76 65 20  d.  ** and have 
19224 61 6e 20 61 73 73 69 67 6e 65 64 20 74 79 70 65  an assigned type
19225 2e 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 61  .  The results a
19226 72 65 20 64 65 2d 65 70 68 65 6d 65 72 61 6c 69  re de-ephemerali
19227 7a 65 64 20 61 73 0a 20 20 2a 2a 20 61 73 20 73  zed as.  ** as s
19228 69 64 65 20 65 66 66 65 63 74 2e 0a 20 20 2a 2f  ide effect..  */
19229 0a 20 20 70 4d 65 6d 20 3d 20 70 2d 3e 70 52 65  .  pMem = p->pRe
1922a 73 75 6c 74 53 65 74 20 3d 20 26 70 2d 3e 61 4d  sultSet = &p->aM
1922b 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 66  em[pOp->p1];.  f
1922c 6f 72 28 69 3d 30 3b 20 69 3c 70 4f 70 2d 3e 70  or(i=0; i<pOp->p
1922d 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  2; i++){.    sql
1922e 69 74 65 33 56 64 62 65 4d 65 6d 4e 75 6c 54 65  ite3VdbeMemNulTe
1922f 72 6d 69 6e 61 74 65 28 26 70 4d 65 6d 5b 69 5d  rminate(&pMem[i]
19230 29 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65  );.    storeType
19231 49 6e 66 6f 28 26 70 4d 65 6d 5b 69 5d 2c 20 65  Info(&pMem[i], e
19232 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45  ncoding);.    RE
19233 47 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70  GISTER_TRACE(pOp
19234 2d 3e 70 31 2b 69 2c 20 26 70 4d 65 6d 5b 69 5d  ->p1+i, &pMem[i]
19235 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  );.  }.  if( db-
19236 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20  >mallocFailed ) 
19237 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 0a 20 20  goto no_mem;..  
19238 2f 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  /* Return SQLITE
19239 5f 52 4f 57 0a 20 20 2a 2f 0a 20 20 70 2d 3e 6e  _ROW.  */.  p->n
1923a 43 61 6c 6c 62 61 63 6b 2b 2b 3b 0a 20 20 70 2d  Callback++;.  p-
1923b 3e 70 63 20 3d 20 70 63 20 2b 20 31 3b 0a 20 20  >pc = pc + 1;.  
1923c 72 63 20 3d 20 53 51 4c 49 54 45 5f 52 4f 57 3b  rc = SQLITE_ROW;
1923d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74  .  goto vdbe_ret
1923e 75 72 6e 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  urn;.}../* Opcod
1923f 65 3a 20 43 6f 6e 63 61 74 20 50 31 20 50 32 20  e: Concat P1 P2 
19240 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41 64 64  P3 * *.**.** Add
19241 20 74 68 65 20 74 65 78 74 20 69 6e 20 72 65 67   the text in reg
19242 69 73 74 65 72 20 50 31 20 6f 6e 74 6f 20 74 68  ister P1 onto th
19243 65 20 65 6e 64 20 6f 66 20 74 68 65 20 74 65 78  e end of the tex
19244 74 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72  t in.** register
19245 20 50 32 20 61 6e 64 20 73 74 6f 72 65 20 74 68   P2 and store th
19246 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
19247 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65  ster P3..** If e
19248 69 74 68 65 72 20 74 68 65 20 50 31 20 6f 72 20  ither the P1 or 
19249 50 32 20 74 65 78 74 20 61 72 65 20 4e 55 4c 4c  P2 text are NULL
1924a 20 74 68 65 6e 20 73 74 6f 72 65 20 4e 55 4c 4c   then store NULL
1924b 20 69 6e 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 20 20   in P3..**.**   
1924c 50 33 20 3d 20 50 32 20 7c 7c 20 50 31 0a 2a 2a  P3 = P2 || P1.**
1924d 0a 2a 2a 20 49 74 20 69 73 20 69 6c 6c 65 67 61  .** It is illega
1924e 6c 20 66 6f 72 20 50 31 20 61 6e 64 20 50 33 20  l for P1 and P3 
1924f 74 6f 20 62 65 20 74 68 65 20 73 61 6d 65 20 72  to be the same r
19250 65 67 69 73 74 65 72 2e 20 53 6f 6d 65 74 69 6d  egister. Sometim
19251 65 73 2c 0a 2a 2a 20 69 66 20 50 33 20 69 73 20  es,.** if P3 is 
19252 74 68 65 20 73 61 6d 65 20 72 65 67 69 73 74 65  the same registe
19253 72 20 61 73 20 50 32 2c 20 74 68 65 20 69 6d 70  r as P2, the imp
19254 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 69 73 20 61  lementation is a
19255 62 6c 65 0a 2a 2a 20 74 6f 20 61 76 6f 69 64 20  ble.** to avoid 
19256 61 20 6d 65 6d 63 70 79 28 29 2e 0a 2a 2f 0a 63  a memcpy()..*/.c
19257 61 73 65 20 4f 50 5f 43 6f 6e 63 61 74 3a 20 7b  ase OP_Concat: {
19258 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
19259 6d 65 20 61 73 20 54 4b 5f 43 4f 4e 43 41 54 2c  me as TK_CONCAT,
1925a 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20   in1, in2, out3 
1925b 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74 65 3b 0a  */.  i64 nByte;.
1925c 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 31 21  .  assert( pIn1!
1925d 3d 70 4f 75 74 20 29 3b 0a 20 20 69 66 28 20 28  =pOut );.  if( (
1925e 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20 70 49  pIn1->flags | pI
1925f 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d 45 4d  n2->flags) & MEM
19260 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73 71 6c  _Null ){.    sql
19261 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74 4e 75  ite3VdbeMemSetNu
19262 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20 62 72  ll(pOut);.    br
19263 65 61 6b 3b 0a 20 20 7d 0a 20 20 45 78 70 61 6e  eak;.  }.  Expan
19264 64 42 6c 6f 62 28 70 49 6e 31 29 3b 0a 20 20 53  dBlob(pIn1);.  S
19265 74 72 69 6e 67 69 66 79 28 70 49 6e 31 2c 20 65  tringify(pIn1, e
19266 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 45 78 70 61  ncoding);.  Expa
19267 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b 0a 20 20  ndBlob(pIn2);.  
19268 53 74 72 69 6e 67 69 66 79 28 70 49 6e 32 2c 20  Stringify(pIn2, 
19269 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 6e 42 79  encoding);.  nBy
1926a 74 65 20 3d 20 70 49 6e 31 2d 3e 6e 20 2b 20 70  te = pIn1->n + p
1926b 49 6e 32 2d 3e 6e 3b 0a 20 20 69 66 28 20 6e 42  In2->n;.  if( nB
1926c 79 74 65 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  yte>db->aLimit[S
1926d 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47  QLITE_LIMIT_LENG
1926e 54 48 5d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  TH] ){.    goto 
1926f 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 4d  too_big;.  }.  M
19270 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f  emSetTypeFlag(pO
19271 75 74 2c 20 4d 45 4d 5f 53 74 72 29 3b 0a 20 20  ut, MEM_Str);.  
19272 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
19273 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e 42 79  emGrow(pOut, nBy
19274 74 65 2b 32 2c 20 70 4f 75 74 3d 3d 70 49 6e 32  te+2, pOut==pIn2
19275 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e 6f  ) ){.    goto no
19276 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 69 66 28 20  _mem;.  }.  if( 
19277 70 4f 75 74 21 3d 70 49 6e 32 20 29 7b 0a 20 20  pOut!=pIn2 ){.  
19278 20 20 6d 65 6d 63 70 79 28 70 4f 75 74 2d 3e 7a    memcpy(pOut->z
19279 2c 20 70 49 6e 32 2d 3e 7a 2c 20 70 49 6e 32 2d  , pIn2->z, pIn2-
1927a 3e 6e 29 3b 0a 20 20 7d 0a 20 20 6d 65 6d 63 70  >n);.  }.  memcp
1927b 79 28 26 70 4f 75 74 2d 3e 7a 5b 70 49 6e 32 2d  y(&pOut->z[pIn2-
1927c 3e 6e 5d 2c 20 70 49 6e 31 2d 3e 7a 2c 20 70 49  >n], pIn1->z, pI
1927d 6e 31 2d 3e 6e 29 3b 0a 20 20 70 4f 75 74 2d 3e  n1->n);.  pOut->
1927e 7a 5b 6e 42 79 74 65 5d 20 3d 20 30 3b 0a 20 20  z[nByte] = 0;.  
1927f 70 4f 75 74 2d 3e 7a 5b 6e 42 79 74 65 2b 31 5d  pOut->z[nByte+1]
19280 20 3d 20 30 3b 0a 20 20 70 4f 75 74 2d 3e 66 6c   = 0;.  pOut->fl
19281 61 67 73 20 7c 3d 20 4d 45 4d 5f 54 65 72 6d 3b  ags |= MEM_Term;
19282 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d 20 6e 42 79  .  pOut->n = nBy
19283 74 65 3b 0a 20 20 70 4f 75 74 2d 3e 65 6e 63 20  te;.  pOut->enc 
19284 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20 20 55 50  = encoding;.  UP
19285 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
19286 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
19287 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
19288 41 64 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a  Add P1 P2 P3 * *
19289 0a 2a 2a 0a 2a 2a 20 41 64 64 20 74 68 65 20 76  .**.** Add the v
1928a 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72  alue in register
1928b 20 50 31 20 74 6f 20 74 68 65 20 76 61 6c 75 65   P1 to the value
1928c 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 0a   in register P2.
1928d 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  ** and store the
1928e 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73   result in regis
1928f 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69  ter P3..** If ei
19290 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55  ther input is NU
19291 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  LL, the result i
19292 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70  s NULL..*/./* Op
19293 63 6f 64 65 3a 20 4d 75 6c 74 69 70 6c 79 20 50  code: Multiply P
19294 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
19295 2a 0a 2a 2a 20 4d 75 6c 74 69 70 6c 79 20 74 68  *.** Multiply th
19296 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
19297 74 65 72 20 50 31 20 62 79 20 74 68 65 20 76 61  ter P1 by the va
19298 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
19299 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20  P2.** and store 
1929a 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
1929b 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66  gister P3..** If
1929c 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73   either input is
1929d 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
1929e 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a  t is NULL..*/./*
1929f 20 4f 70 63 6f 64 65 3a 20 53 75 62 74 72 61 63   Opcode: Subtrac
192a0 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  t P1 P2 P3 * *.*
192a1 2a 0a 2a 2a 20 53 75 62 74 72 61 63 74 20 74 68  *.** Subtract th
192a2 65 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73  e value in regis
192a3 74 65 72 20 50 31 20 66 72 6f 6d 20 74 68 65 20  ter P1 from the 
192a4 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
192a5 72 20 50 32 0a 2a 2a 20 61 6e 64 20 73 74 6f 72  r P2.** and stor
192a6 65 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20  e the result in 
192a7 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20  register P3..** 
192a8 49 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20  If either input 
192a9 69 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73  is NULL, the res
192aa 75 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  ult is NULL..*/.
192ab 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 69 76 69 64  /* Opcode: Divid
192ac 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  e P1 P2 P3 * *.*
192ad 2a 0a 2a 2a 20 44 69 76 69 64 65 20 74 68 65 20  *.** Divide the 
192ae 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
192af 72 20 50 31 20 62 79 20 74 68 65 20 76 61 6c 75  r P1 by the valu
192b0 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32  e in register P2
192b1 0a 2a 2a 20 61 6e 64 20 73 74 6f 72 65 20 74 68  .** and store th
192b2 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65 67 69  e result in regi
192b3 73 74 65 72 20 50 33 2e 20 20 49 66 20 74 68 65  ster P3.  If the
192b4 20 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74   value in regist
192b5 65 72 20 50 32 0a 2a 2a 20 69 73 20 7a 65 72 6f  er P2.** is zero
192b6 2c 20 74 68 65 6e 20 74 68 65 20 72 65 73 75 6c  , then the resul
192b7 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 20 49 66  t is NULL..** If
192b8 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73   either input is
192b9 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
192ba 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a  t is NULL..*/./*
192bb 20 4f 70 63 6f 64 65 3a 20 52 65 6d 61 69 6e 64   Opcode: Remaind
192bc 65 72 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a  er P1 P2 P3 * *.
192bd 2a 2a 0a 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68  **.** Compute th
192be 65 20 72 65 6d 61 69 6e 64 65 72 20 61 66 74 65  e remainder afte
192bf 72 20 69 6e 74 65 67 65 72 20 64 69 76 69 73 69  r integer divisi
192c0 6f 6e 20 6f 66 20 74 68 65 20 76 61 6c 75 65 20  on of the value 
192c1 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50  in.** register P
192c2 31 20 62 79 20 74 68 65 20 76 61 6c 75 65 20 69  1 by the value i
192c3 6e 20 72 65 67 69 73 74 65 72 20 50 32 20 61 6e  n register P2 an
192c4 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  d store the resu
192c5 6c 74 20 69 6e 20 50 33 2e 20 0a 2a 2a 20 49 66  lt in P3. .** If
192c6 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72 65   the value in re
192c7 67 69 73 74 65 72 20 50 32 20 69 73 20 7a 65 72  gister P2 is zer
192c8 6f 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  o the result is 
192c9 4e 55 4c 4c 2e 0a 2a 2a 20 49 66 20 65 69 74 68  NULL..** If eith
192ca 65 72 20 6f 70 65 72 61 6e 64 20 69 73 20 4e 55  er operand is NU
192cb 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69  LL, the result i
192cc 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20  s NULL..*/.case 
192cd 4f 50 5f 41 64 64 3a 20 20 20 20 20 20 20 20 20  OP_Add:         
192ce 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
192cf 65 20 61 73 20 54 4b 5f 50 4c 55 53 2c 20 69 6e  e as TK_PLUS, in
192d0 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a  1, in2, out3 */.
192d1 63 61 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74  case OP_Subtract
192d2 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  :              /
192d3 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4d 49 4e  * same as TK_MIN
192d4 55 53 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  US, in1, in2, ou
192d5 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4d 75  t3 */.case OP_Mu
192d6 6c 74 69 70 6c 79 3a 20 20 20 20 20 20 20 20 20  ltiply:         
192d7 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
192d8 54 4b 5f 53 54 41 52 2c 20 69 6e 31 2c 20 69 6e  TK_STAR, in1, in
192d9 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61 73 65 20  2, out3 */.case 
192da 4f 50 5f 44 69 76 69 64 65 3a 20 20 20 20 20 20  OP_Divide:      
192db 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61 6d            /* sam
192dc 65 20 61 73 20 54 4b 5f 53 4c 41 53 48 2c 20 69  e as TK_SLASH, i
192dd 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
192de 0a 63 61 73 65 20 4f 50 5f 52 65 6d 61 69 6e 64  .case OP_Remaind
192df 65 72 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  er: {           
192e0 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 52 45  /* same as TK_RE
192e1 4d 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75 74  M, in1, in2, out
192e2 33 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73  3 */.  int flags
192e3 3b 0a 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63  ;.  applyNumeric
192e4 41 66 66 69 6e 69 74 79 28 70 49 6e 31 29 3b 0a  Affinity(pIn1);.
192e5 20 20 61 70 70 6c 79 4e 75 6d 65 72 69 63 41 66    applyNumericAf
192e6 66 69 6e 69 74 79 28 70 49 6e 32 29 3b 0a 20 20  finity(pIn2);.  
192e7 66 6c 61 67 73 20 3d 20 70 49 6e 31 2d 3e 66 6c  flags = pIn1->fl
192e8 61 67 73 20 7c 20 70 49 6e 32 2d 3e 66 6c 61 67  ags | pIn2->flag
192e9 73 3b 0a 20 20 69 66 28 20 28 66 6c 61 67 73 20  s;.  if( (flags 
192ea 26 20 4d 45 4d 5f 4e 75 6c 6c 29 21 3d 30 20 29  & MEM_Null)!=0 )
192eb 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63   goto arithmetic
192ec 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b  _result_is_null;
192ed 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c  .  if( (pIn1->fl
192ee 61 67 73 20 26 20 70 49 6e 32 2d 3e 66 6c 61 67  ags & pIn2->flag
192ef 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d 3d 4d 45  s & MEM_Int)==ME
192f0 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 69 36 34  M_Int ){.    i64
192f1 20 61 2c 20 62 3b 0a 20 20 20 20 61 20 3d 20 70   a, b;.    a = p
192f2 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 20 20 62 20  In1->u.i;.    b 
192f3 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a 20 20 20  = pIn2->u.i;.   
192f4 20 73 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70   switch( pOp->op
192f5 63 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 63 61  code ){.      ca
192f6 73 65 20 4f 50 5f 41 64 64 3a 20 20 20 20 20 20  se OP_Add:      
192f7 20 20 20 62 20 2b 3d 20 61 3b 20 20 20 20 20 20     b += a;      
192f8 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
192f9 73 65 20 4f 50 5f 53 75 62 74 72 61 63 74 3a 20  se OP_Subtract: 
192fa 20 20 20 62 20 2d 3d 20 61 3b 20 20 20 20 20 20     b -= a;      
192fb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
192fc 73 65 20 4f 50 5f 4d 75 6c 74 69 70 6c 79 3a 20  se OP_Multiply: 
192fd 20 20 20 62 20 2a 3d 20 61 3b 20 20 20 20 20 20     b *= a;      
192fe 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
192ff 73 65 20 4f 50 5f 44 69 76 69 64 65 3a 20 7b 0a  se OP_Divide: {.
19300 20 20 20 20 20 20 20 20 69 66 28 20 61 3d 3d 30          if( a==0
19301 20 29 20 67 6f 74 6f 20 61 72 69 74 68 6d 65 74   ) goto arithmet
19302 69 63 5f 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c  ic_result_is_nul
19303 6c 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 44 69  l;.        /* Di
19304 76 69 64 69 6e 67 20 74 68 65 20 6c 61 72 67 65  viding the large
19305 73 74 20 70 6f 73 73 69 62 6c 65 20 6e 65 67 61  st possible nega
19306 74 69 76 65 20 36 34 2d 62 69 74 20 69 6e 74 65  tive 64-bit inte
19307 67 65 72 20 28 31 3c 3c 36 33 29 20 62 79 20 0a  ger (1<<63) by .
19308 20 20 20 20 20 20 20 20 2a 2a 20 2d 31 20 72 65          ** -1 re
19309 74 75 72 6e 73 20 61 6e 20 69 6e 74 65 67 65 72  turns an integer
1930a 20 74 6f 6f 20 6c 61 72 67 65 20 74 6f 20 73 74   too large to st
1930b 6f 72 65 20 69 6e 20 61 20 36 34 2d 62 69 74 20  ore in a 64-bit 
1930c 64 61 74 61 2d 74 79 70 65 2e 20 4f 6e 0a 20 20  data-type. On.  
1930d 20 20 20 20 20 20 2a 2a 20 73 6f 6d 65 20 61 72        ** some ar
1930e 63 68 69 74 65 63 74 75 72 65 73 2c 20 74 68 65  chitectures, the
1930f 20 76 61 6c 75 65 20 6f 76 65 72 66 6c 6f 77 73   value overflows
19310 20 74 6f 20 28 31 3c 3c 36 33 29 2e 20 4f 6e 20   to (1<<63). On 
19311 6f 74 68 65 72 73 2c 0a 20 20 20 20 20 20 20 20  others,.        
19312 2a 2a 20 61 20 53 49 47 46 50 45 20 69 73 20 69  ** a SIGFPE is i
19313 73 73 75 65 64 2e 20 54 68 65 20 66 6f 6c 6c 6f  ssued. The follo
19314 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 6e  wing statement n
19315 6f 72 6d 61 6c 69 7a 65 73 20 74 68 69 73 0a 20  ormalizes this. 
19316 20 20 20 20 20 20 20 2a 2a 20 62 65 68 61 76 69         ** behavi
19317 6f 72 20 73 6f 20 74 68 61 74 20 61 6c 6c 20 61  or so that all a
19318 72 63 68 69 74 65 63 74 75 72 65 73 20 62 65 68  rchitectures beh
19319 61 76 65 20 61 73 20 69 66 20 69 6e 74 65 67 65  ave as if intege
1931a 72 20 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 76  r .        ** ov
1931b 65 72 66 6c 6f 77 20 6f 63 63 75 72 72 65 64 2e  erflow occurred.
1931c 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
1931d 20 20 20 20 69 66 28 20 61 3d 3d 2d 31 20 26 26      if( a==-1 &&
1931e 20 62 3d 3d 53 4d 41 4c 4c 45 53 54 5f 49 4e 54   b==SMALLEST_INT
1931f 36 34 20 29 20 61 20 3d 20 31 3b 0a 20 20 20 20  64 ) a = 1;.    
19320 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20 20 20 20      b /= a;.    
19321 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
19322 20 7d 0a 20 20 20 20 20 20 64 65 66 61 75 6c 74   }.      default
19323 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  : {.        if( 
19324 61 3d 3d 30 20 29 20 67 6f 74 6f 20 61 72 69 74  a==0 ) goto arit
19325 68 6d 65 74 69 63 5f 72 65 73 75 6c 74 5f 69 73  hmetic_result_is
19326 5f 6e 75 6c 6c 3b 0a 20 20 20 20 20 20 20 20 69  _null;.        i
19327 66 28 20 61 3d 3d 2d 31 20 29 20 61 20 3d 20 31  f( a==-1 ) a = 1
19328 3b 0a 20 20 20 20 20 20 20 20 62 20 25 3d 20 61  ;.        b %= a
19329 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
1932a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1932b 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 62     pOut->u.i = b
1932c 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65  ;.    MemSetType
1932d 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49  Flag(pOut, MEM_I
1932e 6e 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  nt);.  }else{.  
1932f 20 20 64 6f 75 62 6c 65 20 61 2c 20 62 3b 0a 20    double a, b;. 
19330 20 20 20 61 20 3d 20 73 71 6c 69 74 65 33 56 64     a = sqlite3Vd
19331 62 65 52 65 61 6c 56 61 6c 75 65 28 70 49 6e 31  beRealValue(pIn1
19332 29 3b 0a 20 20 20 20 62 20 3d 20 73 71 6c 69 74  );.    b = sqlit
19333 65 33 56 64 62 65 52 65 61 6c 56 61 6c 75 65 28  e3VdbeRealValue(
19334 70 49 6e 32 29 3b 0a 20 20 20 20 73 77 69 74 63  pIn2);.    switc
19335 68 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 29  h( pOp->opcode )
19336 7b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  {.      case OP_
19337 41 64 64 3a 20 20 20 20 20 20 20 20 20 62 20 2b  Add:         b +
19338 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b  = a;       break
19339 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  ;.      case OP_
1933a 53 75 62 74 72 61 63 74 3a 20 20 20 20 62 20 2d  Subtract:    b -
1933b 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b  = a;       break
1933c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  ;.      case OP_
1933d 4d 75 6c 74 69 70 6c 79 3a 20 20 20 20 62 20 2a  Multiply:    b *
1933e 3d 20 61 3b 20 20 20 20 20 20 20 62 72 65 61 6b  = a;       break
1933f 3b 0a 20 20 20 20 20 20 63 61 73 65 20 4f 50 5f  ;.      case OP_
19340 44 69 76 69 64 65 3a 20 7b 0a 20 20 20 20 20 20  Divide: {.      
19341 20 20 69 66 28 20 61 3d 3d 30 2e 30 20 29 20 67    if( a==0.0 ) g
19342 6f 74 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72  oto arithmetic_r
19343 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20  esult_is_null;. 
19344 20 20 20 20 20 20 20 62 20 2f 3d 20 61 3b 0a 20         b /= a;. 
19345 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
19346 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65 66 61      }.      defa
19347 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 69  ult: {.        i
19348 36 34 20 69 61 20 3d 20 28 69 36 34 29 61 3b 0a  64 ia = (i64)a;.
19349 20 20 20 20 20 20 20 20 69 36 34 20 69 62 20 3d          i64 ib =
1934a 20 28 69 36 34 29 62 3b 0a 20 20 20 20 20 20 20   (i64)b;.       
1934b 20 69 66 28 20 69 61 3d 3d 30 20 29 20 67 6f 74   if( ia==0 ) got
1934c 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  o arithmetic_res
1934d 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20  ult_is_null;.   
1934e 20 20 20 20 20 69 66 28 20 69 61 3d 3d 2d 31 20       if( ia==-1 
1934f 29 20 69 61 20 3d 20 31 3b 0a 20 20 20 20 20 20  ) ia = 1;.      
19350 20 20 62 20 3d 20 69 62 20 25 20 69 61 3b 0a 20    b = ib % ia;. 
19351 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
19352 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
19353 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61 4e  if( sqlite3IsNaN
19354 28 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  (b) ){.      got
19355 6f 20 61 72 69 74 68 6d 65 74 69 63 5f 72 65 73  o arithmetic_res
19356 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3b 0a 20 20 20  ult_is_null;.   
19357 20 7d 0a 20 20 20 20 70 4f 75 74 2d 3e 72 20 3d   }.    pOut->r =
19358 20 62 3b 0a 20 20 20 20 4d 65 6d 53 65 74 54 79   b;.    MemSetTy
19359 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d  peFlag(pOut, MEM
1935a 5f 52 65 61 6c 29 3b 0a 20 20 20 20 69 66 28 20  _Real);.    if( 
1935b 28 66 6c 61 67 73 20 26 20 4d 45 4d 5f 52 65 61  (flags & MEM_Rea
1935c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  l)==0 ){.      s
1935d 71 6c 69 74 65 33 56 64 62 65 49 6e 74 65 67 65  qlite3VdbeIntege
1935e 72 41 66 66 69 6e 69 74 79 28 70 4f 75 74 29 3b  rAffinity(pOut);
1935f 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72 65  .    }.  }.  bre
19360 61 6b 3b 0a 0a 61 72 69 74 68 6d 65 74 69 63 5f  ak;..arithmetic_
19361 72 65 73 75 6c 74 5f 69 73 5f 6e 75 6c 6c 3a 0a  result_is_null:.
19362 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d    sqlite3VdbeMem
19363 53 65 74 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20  SetNull(pOut);. 
19364 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
19365 63 6f 64 65 3a 20 43 6f 6c 6c 53 65 71 20 2a 20  code: CollSeq * 
19366 2a 20 50 34 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73  * P4.**.** P4 is
19367 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 20   a pointer to a 
19368 43 6f 6c 6c 53 65 71 20 73 74 72 75 63 74 2e 20  CollSeq struct. 
19369 49 66 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  If the next call
1936a 20 74 6f 20 61 20 75 73 65 72 20 66 75 6e 63 74   to a user funct
1936b 69 6f 6e 0a 2a 2a 20 6f 72 20 61 67 67 72 65 67  ion.** or aggreg
1936c 61 74 65 20 63 61 6c 6c 73 20 73 71 6c 69 74 65  ate calls sqlite
1936d 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28  3GetFuncCollSeq(
1936e 29 2c 20 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f  ), this collatio
1936f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 6c 6c 0a  n sequence will.
19370 2a 2a 20 62 65 20 72 65 74 75 72 6e 65 64 2e 20  ** be returned. 
19371 54 68 69 73 20 69 73 20 75 73 65 64 20 62 79 20  This is used by 
19372 74 68 65 20 62 75 69 6c 74 2d 69 6e 20 6d 69 6e  the built-in min
19373 28 29 2c 20 6d 61 78 28 29 20 61 6e 64 20 6e 75  (), max() and nu
19374 6c 6c 69 66 28 29 0a 2a 2a 20 66 75 6e 63 74 69  llif().** functi
19375 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69  ons..**.** The i
19376 6e 74 65 72 66 61 63 65 20 75 73 65 64 20 62 79  nterface used by
19377 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
19378 69 6f 6e 20 6f 66 20 74 68 65 20 61 66 6f 72 65  ion of the afore
19379 6d 65 6e 74 69 6f 6e 65 64 20 66 75 6e 63 74 69  mentioned functi
1937a 6f 6e 73 0a 2a 2a 20 74 6f 20 72 65 74 72 69 65  ons.** to retrie
1937b 76 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  ve the collation
1937c 20 73 65 71 75 65 6e 63 65 20 73 65 74 20 62 79   sequence set by
1937d 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73 20   this opcode is 
1937e 6e 6f 74 20 61 76 61 69 6c 61 62 6c 65 0a 2a 2a  not available.**
1937f 20 70 75 62 6c 69 63 6c 79 2c 20 6f 6e 6c 79 20   publicly, only 
19380 74 6f 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e  to user function
19381 73 20 64 65 66 69 6e 65 64 20 69 6e 20 66 75 6e  s defined in fun
19382 63 2e 63 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  c.c..*/.case OP_
19383 43 6f 6c 6c 53 65 71 3a 20 7b 0a 20 20 61 73 73  CollSeq: {.  ass
19384 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
19385 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a  ==P4_COLLSEQ );.
19386 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
19387 70 63 6f 64 65 3a 20 46 75 6e 63 74 69 6f 6e 20  pcode: Function 
19388 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
19389 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 61 20 75 73  *.** Invoke a us
1938a 65 72 20 66 75 6e 63 74 69 6f 6e 20 28 50 34 20  er function (P4 
1938b 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
1938c 61 20 46 75 6e 63 74 69 6f 6e 20 73 74 72 75 63  a Function struc
1938d 74 75 72 65 20 74 68 61 74 0a 2a 2a 20 64 65 66  ture that.** def
1938e 69 6e 65 73 20 74 68 65 20 66 75 6e 63 74 69 6f  ines the functio
1938f 6e 29 20 77 69 74 68 20 50 35 20 61 72 67 75 6d  n) with P5 argum
19390 65 6e 74 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20  ents taken from 
19391 72 65 67 69 73 74 65 72 20 50 32 20 61 6e 64 0a  register P2 and.
19392 2a 2a 20 73 75 63 63 65 73 73 6f 72 73 2e 20 20  ** successors.  
19393 54 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68  The result of th
19394 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 73 74  e function is st
19395 6f 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72  ored in register
19396 20 50 33 2e 0a 2a 2a 20 52 65 67 69 73 74 65 72   P3..** Register
19397 20 50 33 20 6d 75 73 74 20 6e 6f 74 20 62 65 20   P3 must not be 
19398 6f 6e 65 20 6f 66 20 74 68 65 20 66 75 6e 63 74  one of the funct
19399 69 6f 6e 20 69 6e 70 75 74 73 2e 0a 2a 2a 0a 2a  ion inputs..**.*
1939a 2a 20 50 31 20 69 73 20 61 20 33 32 2d 62 69 74  * P1 is a 32-bit
1939b 20 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74   bitmask indicat
1939c 69 6e 67 20 77 68 65 74 68 65 72 20 6f 72 20 6e  ing whether or n
1939d 6f 74 20 65 61 63 68 20 61 72 67 75 6d 65 6e 74  ot each argument
1939e 20 74 6f 20 74 68 65 20 0a 2a 2a 20 66 75 6e 63   to the .** func
1939f 74 69 6f 6e 20 77 61 73 20 64 65 74 65 72 6d 69  tion was determi
193a0 6e 65 64 20 74 6f 20 62 65 20 63 6f 6e 73 74 61  ned to be consta
193a1 6e 74 20 61 74 20 63 6f 6d 70 69 6c 65 20 74 69  nt at compile ti
193a2 6d 65 2e 20 49 66 20 74 68 65 20 66 69 72 73 74  me. If the first
193a3 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 77 61 73  .** argument was
193a4 20 63 6f 6e 73 74 61 6e 74 20 74 68 65 6e 20 62   constant then b
193a5 69 74 20 30 20 6f 66 20 50 31 20 69 73 20 73 65  it 0 of P1 is se
193a6 74 2e 20 54 68 69 73 20 69 73 20 75 73 65 64 20  t. This is used 
193a7 74 6f 20 64 65 74 65 72 6d 69 6e 65 0a 2a 2a 20  to determine.** 
193a8 77 68 65 74 68 65 72 20 6d 65 74 61 20 64 61 74  whether meta dat
193a9 61 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  a associated wit
193aa 68 20 61 20 75 73 65 72 20 66 75 6e 63 74 69 6f  h a user functio
193ab 6e 20 61 72 67 75 6d 65 6e 74 20 75 73 69 6e 67  n argument using
193ac 20 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f   the.** sqlite3_
193ad 73 65 74 5f 61 75 78 64 61 74 61 28 29 20 41 50  set_auxdata() AP
193ae 49 20 6d 61 79 20 62 65 20 73 61 66 65 6c 79 20  I may be safely 
193af 72 65 74 61 69 6e 65 64 20 75 6e 74 69 6c 20 74  retained until t
193b0 68 65 20 6e 65 78 74 0a 2a 2a 20 69 6e 76 6f 63  he next.** invoc
193b1 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6f 70  ation of this op
193b2 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20  code..**.** See 
193b3 61 6c 73 6f 3a 20 41 67 67 53 74 65 70 20 61 6e  also: AggStep an
193b4 64 20 41 67 67 46 69 6e 61 6c 0a 2a 2f 0a 63 61  d AggFinal.*/.ca
193b5 73 65 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 3a 20  se OP_Function: 
193b6 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d 65 6d  {.  int i;.  Mem
193b7 20 2a 70 41 72 67 3b 0a 20 20 73 71 6c 69 74 65   *pArg;.  sqlite
193b8 33 5f 63 6f 6e 74 65 78 74 20 63 74 78 3b 0a 20  3_context ctx;. 
193b9 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
193ba 2a 61 70 56 61 6c 3b 0a 20 20 69 6e 74 20 6e 20  *apVal;.  int n 
193bb 3d 20 70 4f 70 2d 3e 70 35 3b 0a 0a 20 20 61 70  = pOp->p5;..  ap
193bc 56 61 6c 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Val = p->apArg;.
193bd 20 20 61 73 73 65 72 74 28 20 61 70 56 61 6c 20    assert( apVal 
193be 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 0a 20 20 61 73  || n==0 );..  as
193bf 73 65 72 74 28 20 6e 3d 3d 30 20 7c 7c 20 28 70  sert( n==0 || (p
193c0 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d  Op->p2>0 && pOp-
193c1 3e 70 32 2b 6e 3c 3d 70 2d 3e 6e 4d 65 6d 29 20  >p2+n<=p->nMem) 
193c2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  );.  assert( pOp
193c3 2d 3e 70 33 3c 70 4f 70 2d 3e 70 32 20 7c 7c 20  ->p3<pOp->p2 || 
193c4 70 4f 70 2d 3e 70 33 3e 3d 70 4f 70 2d 3e 70 32  pOp->p3>=pOp->p2
193c5 2b 6e 20 29 3b 0a 20 20 70 41 72 67 20 3d 20 26  +n );.  pArg = &
193c6 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d  p->aMem[pOp->p2]
193c7 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e  ;.  for(i=0; i<n
193c8 3b 20 69 2b 2b 2c 20 70 41 72 67 2b 2b 29 7b 0a  ; i++, pArg++){.
193c9 20 20 20 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70      apVal[i] = p
193ca 41 72 67 3b 0a 20 20 20 20 73 74 6f 72 65 54 79  Arg;.    storeTy
193cb 70 65 49 6e 66 6f 28 70 41 72 67 2c 20 65 6e 63  peInfo(pArg, enc
193cc 6f 64 69 6e 67 29 3b 0a 20 20 20 20 52 45 47 49  oding);.    REGI
193cd 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
193ce 70 32 2c 20 70 41 72 67 29 3b 0a 20 20 7d 0a 0a  p2, pArg);.  }..
193cf 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
193d0 34 74 79 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45  4type==P4_FUNCDE
193d1 46 20 7c 7c 20 70 4f 70 2d 3e 70 34 74 79 70 65  F || pOp->p4type
193d2 3d 3d 50 34 5f 56 44 42 45 46 55 4e 43 20 29 3b  ==P4_VDBEFUNC );
193d3 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 34 74 79  .  if( pOp->p4ty
193d4 70 65 3d 3d 50 34 5f 46 55 4e 43 44 45 46 20 29  pe==P4_FUNCDEF )
193d5 7b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63 20  {.    ctx.pFunc 
193d6 3d 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e 63 3b  = pOp->p4.pFunc;
193d7 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75  .    ctx.pVdbeFu
193d8 6e 63 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b  nc = 0;.  }else{
193d9 0a 20 20 20 20 63 74 78 2e 70 56 64 62 65 46 75  .    ctx.pVdbeFu
193da 6e 63 20 3d 20 28 56 64 62 65 46 75 6e 63 2a 29  nc = (VdbeFunc*)
193db 70 4f 70 2d 3e 70 34 2e 70 56 64 62 65 46 75 6e  pOp->p4.pVdbeFun
193dc 63 3b 0a 20 20 20 20 63 74 78 2e 70 46 75 6e 63  c;.    ctx.pFunc
193dd 20 3d 20 63 74 78 2e 70 56 64 62 65 46 75 6e 63   = ctx.pVdbeFunc
193de 2d 3e 70 46 75 6e 63 3b 0a 20 20 7d 0a 0a 20 20  ->pFunc;.  }..  
193df 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e  assert( pOp->p3>
193e0 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d  0 && pOp->p3<=p-
193e1 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4f 75 74 20  >nMem );.  pOut 
193e2 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
193e3 70 33 5d 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61  p3];.  ctx.s.fla
193e4 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
193e5 20 63 74 78 2e 73 2e 64 62 20 3d 20 64 62 3b 0a   ctx.s.db = db;.
193e6 20 20 63 74 78 2e 73 2e 78 44 65 6c 20 3d 20 30    ctx.s.xDel = 0
193e7 3b 0a 20 20 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f  ;.  ctx.s.zMallo
193e8 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68 65  c = 0;..  /* The
193e9 20 6f 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79   output cell may
193ea 20 61 6c 72 65 61 64 79 20 68 61 76 65 20 61 20   already have a 
193eb 62 75 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64  buffer allocated
193ec 2e 20 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20  . Move.  ** the 
193ed 70 6f 69 6e 74 65 72 20 74 6f 20 63 74 78 2e 73  pointer to ctx.s
193ee 20 73 6f 20 69 6e 20 63 61 73 65 20 74 68 65 20   so in case the 
193ef 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 63 61  user-function ca
193f0 6e 20 75 73 65 0a 20 20 2a 2a 20 74 68 65 20 61  n use.  ** the a
193f1 6c 72 65 61 64 79 20 61 6c 6c 6f 63 61 74 65 64  lready allocated
193f2 20 62 75 66 66 65 72 20 69 6e 73 74 65 61 64 20   buffer instead 
193f3 6f 66 20 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20  of allocating a 
193f4 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20 20  new one..  */.  
193f5 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f  sqlite3VdbeMemMo
193f6 76 65 28 26 63 74 78 2e 73 2c 20 70 4f 75 74 29  ve(&ctx.s, pOut)
193f7 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
193f8 61 67 28 26 63 74 78 2e 73 2c 20 4d 45 4d 5f 4e  ag(&ctx.s, MEM_N
193f9 75 6c 6c 29 3b 0a 0a 20 20 63 74 78 2e 69 73 45  ull);..  ctx.isE
193fa 72 72 6f 72 20 3d 20 30 3b 0a 20 20 69 66 28 20  rror = 0;.  if( 
193fb 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  ctx.pFunc->flags
193fc 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
193fd 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61  EEDCOLL ){.    a
193fe 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f  ssert( pOp>p->aO
193ff 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  p );.    assert(
19400 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d   pOp[-1].p4type=
19401 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20  =P4_COLLSEQ );. 
19402 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d     assert( pOp[-
19403 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f  1].opcode==OP_Co
19404 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78  llSeq );.    ctx
19405 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d  .pColl = pOp[-1]
19406 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  .p4.pColl;.  }. 
19407 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
19408 74 79 4f 66 66 28 64 62 29 20 29 20 67 6f 74 6f  tyOff(db) ) goto
19409 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69   abort_due_to_mi
1940a 73 75 73 65 3b 0a 20 20 28 2a 63 74 78 2e 70 46  suse;.  (*ctx.pF
1940b 75 6e 63 2d 3e 78 46 75 6e 63 29 28 26 63 74 78  unc->xFunc)(&ctx
1940c 2c 20 6e 2c 20 61 70 56 61 6c 29 3b 0a 20 20 69  , n, apVal);.  i
1940d 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1940e 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 73 71  On(db) ){.    sq
1940f 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19410 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20  ase(&ctx.s);.   
19411 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
19412 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20  to_misuse;.  }. 
19413 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
19414 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 2f 2a 20  ailed ){.    /* 
19415 45 76 65 6e 20 74 68 6f 75 67 68 20 61 20 6d 61  Even though a ma
19416 6c 6c 6f 63 28 29 20 68 61 73 20 66 61 69 6c 65  lloc() has faile
19417 64 2c 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  d, the implement
19418 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20  ation of the.   
19419 20 2a 2a 20 75 73 65 72 20 66 75 6e 63 74 69 6f   ** user functio
1941a 6e 20 6d 61 79 20 68 61 76 65 20 63 61 6c 6c 65  n may have calle
1941b 64 20 61 6e 20 73 71 6c 69 74 65 33 5f 72 65 73  d an sqlite3_res
1941c 75 6c 74 5f 58 58 58 28 29 20 66 75 6e 63 74 69  ult_XXX() functi
1941d 6f 6e 0a 20 20 20 20 2a 2a 20 74 6f 20 72 65 74  on.    ** to ret
1941e 75 72 6e 20 61 20 76 61 6c 75 65 2e 20 54 68 65  urn a value. The
1941f 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20   following call 
19420 72 65 6c 65 61 73 65 73 20 61 6e 79 20 72 65 73  releases any res
19421 6f 75 72 63 65 73 0a 20 20 20 20 2a 2a 20 61 73  ources.    ** as
19422 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 73 75  sociated with su
19423 63 68 20 61 20 76 61 6c 75 65 2e 0a 20 20 20 20  ch a value..    
19424 2a 2a 0a 20 20 20 20 2a 2a 20 4e 6f 74 65 3a 20  **.    ** Note: 
19425 4d 61 79 62 65 20 4d 65 6d 52 65 6c 65 61 73 65  Maybe MemRelease
19426 28 29 20 73 68 6f 75 6c 64 20 62 65 20 63 61 6c  () should be cal
19427 6c 65 64 20 69 66 20 73 71 6c 69 74 65 33 53 61  led if sqlite3Sa
19428 66 65 74 79 4f 6e 28 29 0a 20 20 20 20 2a 2a 20  fetyOn().    ** 
19429 66 61 69 6c 73 20 61 6c 73 6f 20 28 74 68 65 20  fails also (the 
1942a 69 66 28 2e 2e 2e 29 20 73 74 61 74 65 6d 65 6e  if(...) statemen
1942b 74 20 61 62 6f 76 65 29 2e 20 42 75 74 20 69 66  t above). But if
1942c 20 70 65 6f 70 6c 65 20 61 72 65 0a 20 20 20 20   people are.    
1942d 2a 2a 20 6d 69 73 75 73 69 6e 67 20 73 71 6c 69  ** misusing sqli
1942e 74 65 2c 20 74 68 65 79 20 68 61 76 65 20 62 69  te, they have bi
1942f 67 67 65 72 20 70 72 6f 62 6c 65 6d 73 20 74 68  gger problems th
19430 61 6e 20 61 20 6c 65 61 6b 65 64 20 76 61 6c 75  an a leaked valu
19431 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  e..    */.    sq
19432 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65  lite3VdbeMemRele
19433 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20 20  ase(&ctx.s);.   
19434 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
19435 7d 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79 20 61  }..  /* If any a
19436 75 78 69 6c 69 61 72 79 20 64 61 74 61 20 66 75  uxiliary data fu
19437 6e 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65  nctions have bee
19438 6e 20 63 61 6c 6c 65 64 20 62 79 20 74 68 69 73  n called by this
19439 20 75 73 65 72 20 66 75 6e 63 74 69 6f 6e 2c 0a   user function,.
1943a 20 20 2a 2a 20 69 6d 6d 65 64 69 61 74 65 6c 79    ** immediately
1943b 20 63 61 6c 6c 20 74 68 65 20 64 65 73 74 72 75   call the destru
1943c 63 74 6f 72 20 66 6f 72 20 61 6e 79 20 6e 6f 6e  ctor for any non
1943d 2d 73 74 61 74 69 63 20 76 61 6c 75 65 73 2e 0a  -static values..
1943e 20 20 2a 2f 0a 20 20 69 66 28 20 63 74 78 2e 70    */.  if( ctx.p
1943f 56 64 62 65 46 75 6e 63 20 29 7b 0a 20 20 20 20  VdbeFunc ){.    
19440 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c 65 74  sqlite3VdbeDelet
19441 65 41 75 78 44 61 74 61 28 63 74 78 2e 70 56 64  eAuxData(ctx.pVd
19442 62 65 46 75 6e 63 2c 20 70 4f 70 2d 3e 70 31 29  beFunc, pOp->p1)
19443 3b 0a 20 20 20 20 70 4f 70 2d 3e 70 34 2e 70 56  ;.    pOp->p4.pV
19444 64 62 65 46 75 6e 63 20 3d 20 63 74 78 2e 70 56  dbeFunc = ctx.pV
19445 64 62 65 46 75 6e 63 3b 0a 20 20 20 20 70 4f 70  dbeFunc;.    pOp
19446 2d 3e 70 34 74 79 70 65 20 3d 20 50 34 5f 56 44  ->p4type = P4_VD
19447 42 45 46 55 4e 43 3b 0a 20 20 7d 0a 0a 20 20 2f  BEFUNC;.  }..  /
19448 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * If the functio
19449 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 65 72  n returned an er
1944a 72 6f 72 2c 20 74 68 72 6f 77 20 61 6e 20 65 78  ror, throw an ex
1944b 63 65 70 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28  ception */.  if(
1944c 20 63 74 78 2e 69 73 45 72 72 6f 72 20 29 7b 0a   ctx.isError ){.
1944d 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1944e 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1944f 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
19450 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 26  te3_value_text(&
19451 63 74 78 2e 73 29 29 3b 0a 20 20 20 20 72 63 20  ctx.s));.    rc 
19452 3d 20 63 74 78 2e 69 73 45 72 72 6f 72 3b 0a 20  = ctx.isError;. 
19453 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68   }..  /* Copy th
19454 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
19455 66 75 6e 63 74 69 6f 6e 20 69 6e 74 6f 20 72 65  function into re
19456 67 69 73 74 65 72 20 50 33 20 2a 2f 0a 20 20 73  gister P3 */.  s
19457 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
19458 45 6e 63 6f 64 69 6e 67 28 26 63 74 78 2e 73 2c  Encoding(&ctx.s,
19459 20 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 73 71   encoding);.  sq
1945a 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65  lite3VdbeMemMove
1945b 28 70 4f 75 74 2c 20 26 63 74 78 2e 73 29 3b 0a  (pOut, &ctx.s);.
1945c 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
1945d 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4f 75 74 29  eMemTooBig(pOut)
1945e 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f   ){.    goto too
1945f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 52 45 47 49  _big;.  }.  REGI
19460 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
19461 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50 44  p3, pOut);.  UPD
19462 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
19463 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b 3b  (pOut);.  break;
19464 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 42  .}../* Opcode: B
19465 69 74 41 6e 64 20 50 31 20 50 32 20 50 33 20 2a  itAnd P1 P2 P3 *
19466 20 2a 0a 2a 2a 0a 2a 2a 20 54 61 6b 65 20 74 68   *.**.** Take th
19467 65 20 62 69 74 2d 77 69 73 65 20 41 4e 44 20 6f  e bit-wise AND o
19468 66 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e 20  f the values in 
19469 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64 20  register P1 and 
1946a 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f 72 65 20  P2 and.** store 
1946b 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72 65  the result in re
1946c 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49 66  gister P3..** If
1946d 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69 73   either input is
1946e 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75 6c   NULL, the resul
1946f 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a  t is NULL..*/./*
19470 20 4f 70 63 6f 64 65 3a 20 42 69 74 4f 72 20 50   Opcode: BitOr P
19471 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
19472 2a 20 54 61 6b 65 20 74 68 65 20 62 69 74 2d 77  * Take the bit-w
19473 69 73 65 20 4f 52 20 6f 66 20 74 68 65 20 76 61  ise OR of the va
19474 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72  lues in register
19475 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a 2a   P1 and P2 and.*
19476 2a 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75  * store the resu
19477 6c 74 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  lt in register P
19478 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20  3..** If either 
19479 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c 2c 20 74  input is NULL, t
1947a 68 65 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c  he result is NUL
1947b 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  L..*/./* Opcode:
1947c 20 53 68 69 66 74 4c 65 66 74 20 50 31 20 50 32   ShiftLeft P1 P2
1947d 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 68   P3 * *.**.** Sh
1947e 69 66 74 20 74 68 65 20 69 6e 74 65 67 65 72 20  ift the integer 
1947f 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
19480 72 20 50 32 20 74 6f 20 74 68 65 20 6c 65 66 74  r P2 to the left
19481 20 62 79 20 74 68 65 0a 2a 2a 20 6e 75 6d 62 65   by the.** numbe
19482 72 20 6f 66 20 62 69 74 73 20 73 70 65 63 69 66  r of bits specif
19483 69 65 64 20 62 79 20 74 68 65 20 69 6e 74 65 67  ied by the integ
19484 65 72 20 69 6e 20 72 65 67 69 73 65 72 20 50 31  er in regiser P1
19485 2e 0a 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 72  ..** Store the r
19486 65 73 75 6c 74 20 69 6e 20 72 65 67 69 73 74 65  esult in registe
19487 72 20 50 33 2e 0a 2a 2a 20 49 66 20 65 69 74 68  r P3..** If eith
19488 65 72 20 69 6e 70 75 74 20 69 73 20 4e 55 4c 4c  er input is NULL
19489 2c 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  , the result is 
1948a 4e 55 4c 4c 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f  NULL..*/./* Opco
1948b 64 65 3a 20 53 68 69 66 74 52 69 67 68 74 20 50  de: ShiftRight P
1948c 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1948d 2a 20 53 68 69 66 74 20 74 68 65 20 69 6e 74 65  * Shift the inte
1948e 67 65 72 20 76 61 6c 75 65 20 69 6e 20 72 65 67  ger value in reg
1948f 69 73 74 65 72 20 50 32 20 74 6f 20 74 68 65 20  ister P2 to the 
19490 72 69 67 68 74 20 62 79 20 74 68 65 0a 2a 2a 20  right by the.** 
19491 6e 75 6d 62 65 72 20 6f 66 20 62 69 74 73 20 73  number of bits s
19492 70 65 63 69 66 69 65 64 20 62 79 20 74 68 65 20  pecified by the 
19493 69 6e 74 65 67 65 72 20 69 6e 20 72 65 67 69 73  integer in regis
19494 74 65 72 20 50 31 2e 0a 2a 2a 20 53 74 6f 72 65  ter P1..** Store
19495 20 74 68 65 20 72 65 73 75 6c 74 20 69 6e 20 72   the result in r
19496 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a 20 49  egister P3..** I
19497 66 20 65 69 74 68 65 72 20 69 6e 70 75 74 20 69  f either input i
19498 73 20 4e 55 4c 4c 2c 20 74 68 65 20 72 65 73 75  s NULL, the resu
19499 6c 74 20 69 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a 63  lt is NULL..*/.c
1949a 61 73 65 20 4f 50 5f 42 69 74 41 6e 64 3a 20 20  ase OP_BitAnd:  
1949b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1949c 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 42 49 54  * same as TK_BIT
1949d 41 4e 44 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f  AND, in1, in2, o
1949e 75 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 42  ut3 */.case OP_B
1949f 69 74 4f 72 3a 20 20 20 20 20 20 20 20 20 20 20  itOr:           
194a0 20 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61         /* same a
194a1 73 20 54 4b 5f 42 49 54 4f 52 2c 20 69 6e 31 2c  s TK_BITOR, in1,
194a2 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 63 61   in2, out3 */.ca
194a3 73 65 20 4f 50 5f 53 68 69 66 74 4c 65 66 74 3a  se OP_ShiftLeft:
194a4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
194a5 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 53 48 49   same as TK_LSHI
194a6 46 54 2c 20 69 6e 31 2c 20 69 6e 32 2c 20 6f 75  FT, in1, in2, ou
194a7 74 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 68  t3 */.case OP_Sh
194a8 69 66 74 52 69 67 68 74 3a 20 7b 20 20 20 20 20  iftRight: {     
194a9 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
194aa 20 54 4b 5f 52 53 48 49 46 54 2c 20 69 6e 31 2c   TK_RSHIFT, in1,
194ab 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20 20   in2, out3 */.  
194ac 69 36 34 20 61 2c 20 62 3b 0a 0a 20 20 69 66 28  i64 a, b;..  if(
194ad 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73 20 7c 20   (pIn1->flags | 
194ae 70 49 6e 32 2d 3e 66 6c 61 67 73 29 20 26 20 4d  pIn2->flags) & M
194af 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20 20 73  EM_Null ){.    s
194b0 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65 74  qlite3VdbeMemSet
194b1 4e 75 6c 6c 28 70 4f 75 74 29 3b 0a 20 20 20 20  Null(pOut);.    
194b2 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 61 20 3d  break;.  }.  a =
194b3 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74 56   sqlite3VdbeIntV
194b4 61 6c 75 65 28 70 49 6e 32 29 3b 0a 20 20 62 20  alue(pIn2);.  b 
194b5 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 6e 74  = sqlite3VdbeInt
194b6 56 61 6c 75 65 28 70 49 6e 31 29 3b 0a 20 20 73  Value(pIn1);.  s
194b7 77 69 74 63 68 28 20 70 4f 70 2d 3e 6f 70 63 6f  witch( pOp->opco
194b8 64 65 20 29 7b 0a 20 20 20 20 63 61 73 65 20 4f  de ){.    case O
194b9 50 5f 42 69 74 41 6e 64 3a 20 20 20 20 20 20 61  P_BitAnd:      a
194ba 20 26 3d 20 62 3b 20 20 20 20 20 62 72 65 61 6b   &= b;     break
194bb 3b 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 42 69  ;.    case OP_Bi
194bc 74 4f 72 3a 20 20 20 20 20 20 20 61 20 7c 3d 20  tOr:       a |= 
194bd 62 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  b;     break;.  
194be 20 20 63 61 73 65 20 4f 50 5f 53 68 69 66 74 4c    case OP_ShiftL
194bf 65 66 74 3a 20 20 20 61 20 3c 3c 3d 20 62 3b 20  eft:   a <<= b; 
194c0 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
194c1 66 61 75 6c 74 3a 20 20 61 73 73 65 72 74 28 20  fault:  assert( 
194c2 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
194c3 53 68 69 66 74 52 69 67 68 74 20 29 3b 0a 20 20  ShiftRight );.  
194c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
194c5 20 20 20 20 20 20 20 61 20 3e 3e 3d 20 62 3b 20         a >>= b; 
194c6 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
194c7 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 61 3b 0a 20  pOut->u.i = a;. 
194c8 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
194c9 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
194ca 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
194cb 70 63 6f 64 65 3a 20 41 64 64 49 6d 6d 20 20 50  pcode: AddImm  P
194cc 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a  1 P2 * * *.** .*
194cd 2a 20 41 64 64 20 74 68 65 20 63 6f 6e 73 74 61  * Add the consta
194ce 6e 74 20 50 32 20 74 6f 20 74 68 65 20 76 61 6c  nt P2 to the val
194cf 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
194d0 31 2e 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  1..** The result
194d1 20 69 73 20 61 6c 77 61 79 73 20 61 6e 20 69 6e   is always an in
194d2 74 65 67 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20  teger..**.** To 
194d3 66 6f 72 63 65 20 61 6e 79 20 72 65 67 69 73 74  force any regist
194d4 65 72 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65  er to be an inte
194d5 67 65 72 2c 20 6a 75 73 74 20 61 64 64 20 30 2e  ger, just add 0.
194d6 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 64 64 49  .*/.case OP_AddI
194d7 6d 6d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  mm: {           
194d8 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 73 71 6c   /* in1 */.  sql
194d9 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67  ite3VdbeMemInteg
194da 65 72 69 66 79 28 70 49 6e 31 29 3b 0a 20 20 70  erify(pIn1);.  p
194db 49 6e 31 2d 3e 75 2e 69 20 2b 3d 20 70 4f 70 2d  In1->u.i += pOp-
194dc 3e 70 32 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  >p2;.  break;.}.
194dd 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 72 63  ./* Opcode: Forc
194de 65 49 6e 74 20 50 31 20 50 32 20 50 33 20 2a 20  eInt P1 P2 P3 * 
194df 2a 0a 2a 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20  *.**.** Convert 
194e0 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
194e1 72 20 50 31 20 69 6e 74 6f 20 61 6e 20 69 6e 74  r P1 into an int
194e2 65 67 65 72 2e 20 20 49 66 20 74 68 65 20 76 61  eger.  If the va
194e3 6c 75 65 20 0a 2a 2a 20 69 6e 20 50 31 20 69 73  lue .** in P1 is
194e4 20 6e 6f 74 20 6e 75 6d 65 72 69 63 20 28 6d 65   not numeric (me
194e5 61 6e 69 6e 67 20 74 68 61 74 20 69 73 20 69 73  aning that is is
194e6 20 61 20 4e 55 4c 4c 20 6f 72 20 61 20 73 74 72   a NULL or a str
194e7 69 6e 67 20 74 68 61 74 0a 2a 2a 20 64 6f 65 73  ing that.** does
194e8 20 6e 6f 74 20 6c 6f 6f 6b 20 6c 69 6b 65 20 61   not look like a
194e9 6e 20 69 6e 74 65 67 65 72 20 6f 72 20 66 6c 6f  n integer or flo
194ea 61 74 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62  ating point numb
194eb 65 72 29 20 74 68 65 6e 0a 2a 2a 20 6a 75 6d 70  er) then.** jump
194ec 20 74 6f 20 50 32 2e 20 20 49 66 20 74 68 65 20   to P2.  If the 
194ed 76 61 6c 75 65 20 69 6e 20 50 31 20 69 73 20 6e  value in P1 is n
194ee 75 6d 65 72 69 63 20 74 68 65 6e 0a 2a 2a 20 63  umeric then.** c
194ef 6f 6e 76 65 72 74 20 69 74 20 69 6e 74 6f 20 74  onvert it into t
194f0 68 65 20 6c 65 61 73 74 20 69 6e 74 65 67 65 72  he least integer
194f1 20 74 68 61 74 20 69 73 20 67 72 65 61 74 65 72   that is greater
194f2 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
194f3 6f 20 69 74 73 0a 2a 2a 20 63 75 72 72 65 6e 74  o its.** current
194f4 20 76 61 6c 75 65 20 69 66 20 50 33 3d 3d 30 2c   value if P3==0,
194f5 20 6f 72 20 74 6f 20 74 68 65 20 6c 65 61 73 74   or to the least
194f6 20 69 6e 74 65 67 65 72 20 74 68 61 74 20 69 73   integer that is
194f7 20 73 74 72 69 63 74 6c 79 0a 2a 2a 20 67 72 65   strictly.** gre
194f8 61 74 65 72 20 74 68 61 6e 20 69 74 73 20 63 75  ater than its cu
194f9 72 72 65 6e 74 20 76 61 6c 75 65 20 69 66 20 50  rrent value if P
194fa 33 3d 3d 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  3==1..*/.case OP
194fb 5f 46 6f 72 63 65 49 6e 74 3a 20 7b 20 20 20 20  _ForceInt: {    
194fc 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 2c          /* jump,
194fd 20 69 6e 31 20 2a 2f 0a 20 20 69 36 34 20 76 3b   in1 */.  i64 v;
194fe 0a 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79  .  applyAffinity
194ff 28 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46  (pIn1, SQLITE_AF
19500 46 5f 4e 55 4d 45 52 49 43 2c 20 65 6e 63 6f 64  F_NUMERIC, encod
19501 69 6e 67 29 3b 0a 20 20 69 66 28 20 28 70 49 6e  ing);.  if( (pIn
19502 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f  1->flags & (MEM_
19503 49 6e 74 7c 4d 45 4d 5f 52 65 61 6c 29 29 3d 3d  Int|MEM_Real))==
19504 30 20 29 7b 0a 20 20 20 20 70 63 20 3d 20 70 4f  0 ){.    pc = pO
19505 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 62  p->p2 - 1;.    b
19506 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  reak;.  }.  if( 
19507 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
19508 4d 5f 49 6e 74 20 29 7b 0a 20 20 20 20 76 20 3d  M_Int ){.    v =
19509 20 70 49 6e 31 2d 3e 75 2e 69 20 2b 20 28 70 4f   pIn1->u.i + (pO
1950a 70 2d 3e 70 33 21 3d 30 29 3b 0a 20 20 7d 65 6c  p->p3!=0);.  }el
1950b 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20  se{.    assert( 
1950c 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
1950d 4d 5f 52 65 61 6c 20 29 3b 0a 20 20 20 20 76 20  M_Real );.    v 
1950e 3d 20 28 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  = (sqlite3_int64
1950f 29 70 49 6e 31 2d 3e 72 3b 0a 20 20 20 20 69 66  )pIn1->r;.    if
19510 28 20 70 49 6e 31 2d 3e 72 3e 28 64 6f 75 62 6c  ( pIn1->r>(doubl
19511 65 29 76 20 29 20 76 2b 2b 3b 0a 20 20 20 20 69  e)v ) v++;.    i
19512 66 28 20 70 4f 70 2d 3e 70 33 20 26 26 20 70 49  f( pOp->p3 && pI
19513 6e 31 2d 3e 72 3d 3d 28 64 6f 75 62 6c 65 29 76  n1->r==(double)v
19514 20 29 20 76 2b 2b 3b 0a 20 20 7d 0a 20 20 70 49   ) v++;.  }.  pI
19515 6e 31 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 4d  n1->u.i = v;.  M
19516 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 49  emSetTypeFlag(pI
19517 6e 31 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20  n1, MEM_Int);.  
19518 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
19519 6f 64 65 3a 20 4d 75 73 74 42 65 49 6e 74 20 50  ode: MustBeInt P
1951a 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 20 0a 2a  1 P2 * * *.** .*
1951b 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75  * Force the valu
1951c 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1951d 20 74 6f 20 62 65 20 61 6e 20 69 6e 74 65 67 65   to be an intege
1951e 72 2e 20 20 49 66 20 74 68 65 20 76 61 6c 75 65  r.  If the value
1951f 0a 2a 2a 20 69 6e 20 50 31 20 69 73 20 6e 6f 74  .** in P1 is not
19520 20 61 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 20   an integer and 
19521 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 76 65 72  cannot be conver
19522 74 65 64 20 69 6e 74 6f 20 61 6e 20 69 6e 74 65  ted into an inte
19523 67 65 72 0a 2a 2a 20 77 69 74 68 6f 75 74 20 64  ger.** without d
19524 61 74 61 20 6c 6f 73 73 2c 20 74 68 65 6e 20 6a  ata loss, then j
19525 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
19526 74 6f 20 50 32 2c 20 6f 72 20 69 66 20 50 32 3d  to P2, or if P2=
19527 3d 30 0a 2a 2a 20 72 61 69 73 65 20 61 6e 20 53  =0.** raise an S
19528 51 4c 49 54 45 5f 4d 49 53 4d 41 54 43 48 20 65  QLITE_MISMATCH e
19529 78 63 65 70 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73  xception..*/.cas
1952a 65 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 3a 20  e OP_MustBeInt: 
1952b 7b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  {            /* 
1952c 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61  jump, in1 */.  a
1952d 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e  pplyAffinity(pIn
1952e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 55  1, SQLITE_AFF_NU
1952f 4d 45 52 49 43 2c 20 65 6e 63 6f 64 69 6e 67 29  MERIC, encoding)
19530 3b 0a 20 20 69 66 28 20 28 70 49 6e 31 2d 3e 66  ;.  if( (pIn1->f
19531 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 29 3d  lags & MEM_Int)=
19532 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f  =0 ){.    if( pO
19533 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20 20  p->p2==0 ){.    
19534 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49    rc = SQLITE_MI
19535 53 4d 41 54 43 48 3b 0a 20 20 20 20 20 20 67 6f  SMATCH;.      go
19536 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f  to abort_due_to_
19537 65 72 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65  error;.    }else
19538 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70  {.      pc = pOp
19539 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a  ->p2 - 1;.    }.
1953a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 4d 65 6d    }else{.    Mem
1953b 53 65 74 54 79 70 65 46 6c 61 67 28 70 49 6e 31  SetTypeFlag(pIn1
1953c 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a  , MEM_Int);.  }.
1953d 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
1953e 70 63 6f 64 65 3a 20 52 65 61 6c 41 66 66 69 6e  pcode: RealAffin
1953f 69 74 79 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a  ity P1 * * * *.*
19540 2a 0a 2a 2a 20 49 66 20 72 65 67 69 73 74 65 72  *.** If register
19541 20 50 31 20 68 6f 6c 64 73 20 61 6e 20 69 6e 74   P1 holds an int
19542 65 67 65 72 20 63 6f 6e 76 65 72 74 20 69 74 20  eger convert it 
19543 74 6f 20 61 20 72 65 61 6c 20 76 61 6c 75 65 2e  to a real value.
19544 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  .**.** This opco
19545 64 65 20 69 73 20 75 73 65 64 20 77 68 65 6e 20  de is used when 
19546 65 78 74 72 61 63 74 69 6e 67 20 69 6e 66 6f 72  extracting infor
19547 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 61 20 63 6f  mation from a co
19548 6c 75 6d 6e 20 74 68 61 74 0a 2a 2a 20 68 61 73  lumn that.** has
19549 20 52 45 41 4c 20 61 66 66 69 6e 69 74 79 2e 20   REAL affinity. 
1954a 20 53 75 63 68 20 63 6f 6c 75 6d 6e 20 76 61 6c   Such column val
1954b 75 65 73 20 6d 61 79 20 73 74 69 6c 6c 20 62 65  ues may still be
1954c 20 73 74 6f 72 65 64 20 61 73 0a 2a 2a 20 69 6e   stored as.** in
1954d 74 65 67 65 72 73 2c 20 66 6f 72 20 73 70 61 63  tegers, for spac
1954e 65 20 65 66 66 69 63 69 65 6e 63 79 2c 20 62 75  e efficiency, bu
1954f 74 20 61 66 74 65 72 20 65 78 74 72 61 63 74 69  t after extracti
19550 6f 6e 20 77 65 20 77 61 6e 74 20 74 68 65 6d 0a  on we want them.
19551 2a 2a 20 74 6f 20 68 61 76 65 20 6f 6e 6c 79 20  ** to have only 
19552 61 20 72 65 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f  a real value..*/
19553 0a 63 61 73 65 20 4f 50 5f 52 65 61 6c 41 66 66  .case OP_RealAff
19554 69 6e 69 74 79 3a 20 7b 20 20 20 20 20 20 20 20  inity: {        
19555 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e 31            /* in1
19556 20 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   */.  if( pIn1->
19557 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e 74 20  flags & MEM_Int 
19558 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
19559 62 65 4d 65 6d 52 65 61 6c 69 66 79 28 70 49 6e  beMemRealify(pIn
1955a 31 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  1);.  }.  break;
1955b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
1955c 54 45 5f 4f 4d 49 54 5f 43 41 53 54 0a 2f 2a 20  TE_OMIT_CAST./* 
1955d 4f 70 63 6f 64 65 3a 20 54 6f 54 65 78 74 20 50  Opcode: ToText P
1955e 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1955f 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20  Force the value 
19560 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74  in register P1 t
19561 6f 20 62 65 20 74 65 78 74 2e 0a 2a 2a 20 49 66  o be text..** If
19562 20 74 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75   the value is nu
19563 6d 65 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69  meric, convert i
19564 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 75 73  t to a string us
19565 69 6e 67 20 74 68 65 0a 2a 2a 20 65 71 75 69 76  ing the.** equiv
19566 61 6c 65 6e 74 20 6f 66 20 70 72 69 6e 74 66 28  alent of printf(
19567 29 2e 20 20 42 6c 6f 62 20 76 61 6c 75 65 73 20  ).  Blob values 
19568 61 72 65 20 75 6e 63 68 61 6e 67 65 64 20 61 6e  are unchanged an
19569 64 0a 2a 2a 20 61 72 65 20 61 66 74 65 72 77 61  d.** are afterwa
1956a 72 64 73 20 73 69 6d 70 6c 79 20 69 6e 74 65 72  rds simply inter
1956b 70 72 65 74 65 64 20 61 73 20 74 65 78 74 2e 0a  preted as text..
1956c 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 76 61 6c  **.** A NULL val
1956d 75 65 20 69 73 20 6e 6f 74 20 63 68 61 6e 67 65  ue is not change
1956e 64 20 62 79 20 74 68 69 73 20 72 6f 75 74 69 6e  d by this routin
1956f 65 2e 20 20 49 74 20 72 65 6d 61 69 6e 73 20 4e  e.  It remains N
19570 55 4c 4c 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ULL..*/.case OP_
19571 54 6f 54 65 78 74 3a 20 7b 20 20 20 20 20 20 20  ToText: {       
19572 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
19573 6d 65 20 61 73 20 54 4b 5f 54 4f 5f 54 45 58 54  me as TK_TO_TEXT
19574 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 70  , in1 */.  if( p
19575 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
19576 5f 4e 75 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20  _Null ) break;. 
19577 20 61 73 73 65 72 74 28 20 4d 45 4d 5f 53 74 72   assert( MEM_Str
19578 3d 3d 28 4d 45 4d 5f 42 6c 6f 62 3e 3e 33 29 20  ==(MEM_Blob>>3) 
19579 29 3b 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73  );.  pIn1->flags
1957a 20 7c 3d 20 28 70 49 6e 31 2d 3e 66 6c 61 67 73   |= (pIn1->flags
1957b 26 4d 45 4d 5f 42 6c 6f 62 29 3e 3e 33 3b 0a 20  &MEM_Blob)>>3;. 
1957c 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
1957d 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  In1, SQLITE_AFF_
1957e 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29 3b  TEXT, encoding);
1957f 0a 20 20 72 63 20 3d 20 45 78 70 61 6e 64 42 6c  .  rc = ExpandBl
19580 6f 62 28 70 49 6e 31 29 3b 0a 20 20 61 73 73 65  ob(pIn1);.  asse
19581 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20  rt( pIn1->flags 
19582 26 20 4d 45 4d 5f 53 74 72 20 7c 7c 20 64 62 2d  & MEM_Str || db-
19583 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
19584 0a 20 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  .  pIn1->flags &
19585 3d 20 7e 28 4d 45 4d 5f 49 6e 74 7c 4d 45 4d 5f  = ~(MEM_Int|MEM_
19586 52 65 61 6c 7c 4d 45 4d 5f 42 6c 6f 62 29 3b 0a  Real|MEM_Blob);.
19587 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
19588 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20 62  BSIZE(pIn1);.  b
19589 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1958a 64 65 3a 20 54 6f 42 6c 6f 62 20 50 31 20 2a 20  de: ToBlob P1 * 
1958b 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 6f 72 63  * * *.**.** Forc
1958c 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20 72  e the value in r
1958d 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 62 65  egister P1 to be
1958e 20 61 20 42 4c 4f 42 2e 0a 2a 2a 20 49 66 20 74   a BLOB..** If t
1958f 68 65 20 76 61 6c 75 65 20 69 73 20 6e 75 6d 65  he value is nume
19590 72 69 63 2c 20 63 6f 6e 76 65 72 74 20 69 74 20  ric, convert it 
19591 74 6f 20 61 20 73 74 72 69 6e 67 20 66 69 72 73  to a string firs
19592 74 2e 0a 2a 2a 20 53 74 72 69 6e 67 73 20 61 72  t..** Strings ar
19593 65 20 73 69 6d 70 6c 79 20 72 65 69 6e 74 65 72  e simply reinter
19594 70 72 65 74 65 64 20 61 73 20 62 6c 6f 62 73 20  preted as blobs 
19595 77 69 74 68 20 6e 6f 20 63 68 61 6e 67 65 0a 2a  with no change.*
19596 2a 20 74 6f 20 74 68 65 20 75 6e 64 65 72 6c 79  * to the underly
19597 69 6e 67 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20  ing data..**.** 
19598 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20  A NULL value is 
19599 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20 74  not changed by t
1959a 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74  his routine.  It
1959b 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a   remains NULL..*
1959c 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 42 6c 6f 62  /.case OP_ToBlob
1959d 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20 20  : {             
1959e 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1959f 54 4b 5f 54 4f 5f 42 4c 4f 42 2c 20 69 6e 31 20  TK_TO_BLOB, in1 
195a0 2a 2f 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66  */.  if( pIn1->f
195a1 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20  lags & MEM_Null 
195a2 29 20 62 72 65 61 6b 3b 0a 20 20 69 66 28 20 28  ) break;.  if( (
195a3 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45  pIn1->flags & ME
195a4 4d 5f 42 6c 6f 62 29 3d 3d 30 20 29 7b 0a 20 20  M_Blob)==0 ){.  
195a5 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
195a6 70 49 6e 31 2c 20 53 51 4c 49 54 45 5f 41 46 46  pIn1, SQLITE_AFF
195a7 5f 54 45 58 54 2c 20 65 6e 63 6f 64 69 6e 67 29  _TEXT, encoding)
195a8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 49  ;.    assert( pI
195a9 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f  n1->flags & MEM_
195aa 53 74 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Str || db->mallo
195ab 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 7d 0a 20  cFailed );.  }. 
195ac 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
195ad 70 49 6e 31 2c 20 4d 45 4d 5f 42 6c 6f 62 29 3b  pIn1, MEM_Blob);
195ae 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
195af 4f 42 53 49 5a 45 28 70 49 6e 31 29 3b 0a 20 20  OBSIZE(pIn1);.  
195b0 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
195b1 6f 64 65 3a 20 54 6f 4e 75 6d 65 72 69 63 20 50  ode: ToNumeric P
195b2 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
195b3 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65 20  Force the value 
195b4 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 74  in register P1 t
195b5 6f 20 62 65 20 6e 75 6d 65 72 69 63 20 28 65 69  o be numeric (ei
195b6 74 68 65 72 20 61 6e 0a 2a 2a 20 69 6e 74 65 67  ther an.** integ
195b7 65 72 20 6f 72 20 61 20 66 6c 6f 61 74 69 6e 67  er or a floating
195b8 2d 70 6f 69 6e 74 20 6e 75 6d 62 65 72 2e 29 0a  -point number.).
195b9 2a 2a 20 49 66 20 74 68 65 20 76 61 6c 75 65 20  ** If the value 
195ba 69 73 20 74 65 78 74 20 6f 72 20 62 6c 6f 62 2c  is text or blob,
195bb 20 74 72 79 20 74 6f 20 63 6f 6e 76 65 72 74 20   try to convert 
195bc 69 74 20 74 6f 20 61 6e 20 75 73 69 6e 67 20 74  it to an using t
195bd 68 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74  he.** equivalent
195be 20 6f 66 20 61 74 6f 69 28 29 20 6f 72 20 61 74   of atoi() or at
195bf 6f 66 28 29 20 61 6e 64 20 73 74 6f 72 65 20 30  of() and store 0
195c0 20 69 66 20 6e 6f 20 73 75 63 68 20 63 6f 6e 76   if no such conv
195c1 65 72 73 69 6f 6e 20 0a 2a 2a 20 69 73 20 70 6f  ersion .** is po
195c2 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  ssible..**.** A 
195c3 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
195c4 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
195c5 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
195c6 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
195c7 63 61 73 65 20 4f 50 5f 54 6f 4e 75 6d 65 72 69  case OP_ToNumeri
195c8 63 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  c: {            
195c9 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
195ca 20 54 4b 5f 54 4f 5f 4e 55 4d 45 52 49 43 2c 20   TK_TO_NUMERIC, 
195cb 69 6e 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49  in1 */.  if( (pI
195cc 6e 31 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d  n1->flags & (MEM
195cd 5f 4e 75 6c 6c 7c 4d 45 4d 5f 49 6e 74 7c 4d 45  _Null|MEM_Int|ME
195ce 4d 5f 52 65 61 6c 29 29 3d 3d 30 20 29 7b 0a 20  M_Real))==0 ){. 
195cf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65     sqlite3VdbeMe
195d0 6d 4e 75 6d 65 72 69 66 79 28 70 49 6e 31 29 3b  mNumerify(pIn1);
195d1 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
195d2 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
195d3 5f 4f 4d 49 54 5f 43 41 53 54 20 2a 2f 0a 0a 2f  _OMIT_CAST */../
195d4 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 49 6e 74 20  * Opcode: ToInt 
195d5 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  P1 * * * *.**.**
195d6 20 46 6f 72 63 65 20 74 68 65 20 76 61 6c 75 65   Force the value
195d7 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
195d8 62 65 20 61 6e 20 69 6e 74 65 67 65 72 2e 20 20  be an integer.  
195d9 49 66 0a 2a 2a 20 54 68 65 20 76 61 6c 75 65 20  If.** The value 
195da 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61 20 72  is currently a r
195db 65 61 6c 20 6e 75 6d 62 65 72 2c 20 64 72 6f 70  eal number, drop
195dc 20 69 74 73 20 66 72 61 63 74 69 6f 6e 61 6c 20   its fractional 
195dd 70 61 72 74 2e 0a 2a 2a 20 49 66 20 74 68 65 20  part..** If the 
195de 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f 72  value is text or
195df 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63 6f   blob, try to co
195e0 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20 69  nvert it to an i
195e1 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68 65  nteger using the
195e2 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 6f  .** equivalent o
195e3 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74 6f  f atoi() and sto
195e4 72 65 20 30 20 69 66 20 6e 6f 20 73 75 63 68 20  re 0 if no such 
195e5 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73 20 70 6f  conversion is po
195e6 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 20  ssible..**.** A 
195e7 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73 20 6e 6f  NULL value is no
195e8 74 20 63 68 61 6e 67 65 64 20 62 79 20 74 68 69  t changed by thi
195e9 73 20 72 6f 75 74 69 6e 65 2e 20 20 49 74 20 72  s routine.  It r
195ea 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a 2a 2f 0a  emains NULL..*/.
195eb 63 61 73 65 20 4f 50 5f 54 6f 49 6e 74 3a 20 7b  case OP_ToInt: {
195ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
195ed 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
195ee 54 4f 5f 49 4e 54 2c 20 69 6e 31 20 2a 2f 0a 20  TO_INT, in1 */. 
195ef 20 69 66 28 20 28 70 49 6e 31 2d 3e 66 6c 61 67   if( (pIn1->flag
195f0 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 29 3d 3d 30  s & MEM_Null)==0
195f1 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
195f2 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
195f3 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20 62 72  (pIn1);.  }.  br
195f4 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  eak;.}..#ifndef 
195f5 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54  SQLITE_OMIT_CAST
195f6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 54 6f 52 65  ./* Opcode: ToRe
195f7 61 6c 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  al P1 * * * *.**
195f8 0a 2a 2a 20 46 6f 72 63 65 20 74 68 65 20 76 61  .** Force the va
195f9 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
195fa 50 31 20 74 6f 20 62 65 20 61 20 66 6c 6f 61 74  P1 to be a float
195fb 69 6e 67 20 70 6f 69 6e 74 20 6e 75 6d 62 65 72  ing point number
195fc 2e 0a 2a 2a 20 49 66 20 54 68 65 20 76 61 6c 75  ..** If The valu
195fd 65 20 69 73 20 63 75 72 72 65 6e 74 6c 79 20 61  e is currently a
195fe 6e 20 69 6e 74 65 67 65 72 2c 20 63 6f 6e 76 65  n integer, conve
195ff 72 74 20 69 74 2e 0a 2a 2a 20 49 66 20 74 68 65  rt it..** If the
19600 20 76 61 6c 75 65 20 69 73 20 74 65 78 74 20 6f   value is text o
19601 72 20 62 6c 6f 62 2c 20 74 72 79 20 74 6f 20 63  r blob, try to c
19602 6f 6e 76 65 72 74 20 69 74 20 74 6f 20 61 6e 20  onvert it to an 
19603 69 6e 74 65 67 65 72 20 75 73 69 6e 67 20 74 68  integer using th
19604 65 0a 2a 2a 20 65 71 75 69 76 61 6c 65 6e 74 20  e.** equivalent 
19605 6f 66 20 61 74 6f 69 28 29 20 61 6e 64 20 73 74  of atoi() and st
19606 6f 72 65 20 30 2e 30 20 69 66 20 6e 6f 20 73 75  ore 0.0 if no su
19607 63 68 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 73  ch conversion is
19608 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2a 0a 2a 2a   possible..**.**
19609 20 41 20 4e 55 4c 4c 20 76 61 6c 75 65 20 69 73   A NULL value is
1960a 20 6e 6f 74 20 63 68 61 6e 67 65 64 20 62 79 20   not changed by 
1960b 74 68 69 73 20 72 6f 75 74 69 6e 65 2e 20 20 49  this routine.  I
1960c 74 20 72 65 6d 61 69 6e 73 20 4e 55 4c 4c 2e 0a  t remains NULL..
1960d 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 6f 52 65 61  */.case OP_ToRea
1960e 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20 20  l: {            
1960f 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
19610 20 54 4b 5f 54 4f 5f 52 45 41 4c 2c 20 69 6e 31   TK_TO_REAL, in1
19611 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31 2d   */.  if( (pIn1-
19612 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c  >flags & MEM_Nul
19613 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  l)==0 ){.    sql
19614 69 74 65 33 56 64 62 65 4d 65 6d 52 65 61 6c 69  ite3VdbeMemReali
19615 66 79 28 70 49 6e 31 29 3b 0a 20 20 7d 0a 20 20  fy(pIn1);.  }.  
19616 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20  break;.}.#endif 
19617 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43  /* SQLITE_OMIT_C
19618 41 53 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  AST */../* Opcod
19619 65 3a 20 4c 74 20 50 31 20 50 32 20 50 33 20 50  e: Lt P1 P2 P3 P
1961a 34 20 50 35 0a 2a 2a 0a 2a 2a 20 43 6f 6d 70 61  4 P5.**.** Compa
1961b 72 65 20 74 68 65 20 76 61 6c 75 65 73 20 69 6e  re the values in
1961c 20 72 65 67 69 73 74 65 72 20 50 31 20 61 6e 64   register P1 and
1961d 20 50 33 2e 20 20 49 66 20 72 65 67 28 50 33 29   P3.  If reg(P3)
1961e 3c 72 65 67 28 50 31 29 20 74 68 65 6e 0a 2a 2a  <reg(P1) then.**
1961f 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 65 73 73   jump to address
19620 20 50 32 2e 20 20 0a 2a 2a 0a 2a 2a 20 49 66 20   P2.  .**.** If 
19621 74 68 65 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  the SQLITE_JUMPI
19622 46 4e 55 4c 4c 20 62 69 74 20 6f 66 20 50 35 20  FNULL bit of P5 
19623 69 73 20 73 65 74 20 61 6e 64 20 65 69 74 68 65  is set and eithe
19624 72 20 72 65 67 28 50 31 29 20 6f 72 0a 2a 2a 20  r reg(P1) or.** 
19625 72 65 67 28 50 33 29 20 69 73 20 4e 55 4c 4c 20  reg(P3) is NULL 
19626 74 68 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75  then take the ju
19627 6d 70 2e 20 20 49 66 20 74 68 65 20 53 51 4c 49  mp.  If the SQLI
19628 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c 20 0a 2a  TE_JUMPIFNULL .*
19629 2a 20 62 69 74 20 69 73 20 63 6c 65 61 72 20 74  * bit is clear t
1962a 68 65 6e 20 66 61 6c 6c 20 74 68 72 75 20 69 66  hen fall thru if
1962b 20 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20   either operand 
1962c 69 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 54  is NULL..**.** T
1962d 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41  he SQLITE_AFF_MA
1962e 53 4b 20 70 6f 72 74 69 6f 6e 20 6f 66 20 50 35  SK portion of P5
1962f 20 6d 75 73 74 20 62 65 20 61 6e 20 61 66 66 69   must be an affi
19630 6e 69 74 79 20 63 68 61 72 61 63 74 65 72 20 2d  nity character -
19631 0a 2a 2a 20 53 51 4c 49 54 45 5f 41 46 46 5f 54  .** SQLITE_AFF_T
19632 45 58 54 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  EXT, SQLITE_AFF_
19633 49 4e 54 45 47 45 52 2c 20 61 6e 64 20 73 6f 20  INTEGER, and so 
19634 66 6f 72 74 68 2e 20 41 6e 20 61 74 74 65 6d 70  forth. An attemp
19635 74 20 69 73 20 6d 61 64 65 20 0a 2a 2a 20 74 6f  t is made .** to
19636 20 63 6f 65 72 63 65 20 62 6f 74 68 20 69 6e 70   coerce both inp
19637 75 74 73 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  uts according to
19638 20 74 68 69 73 20 61 66 66 69 6e 69 74 79 20 62   this affinity b
19639 65 66 6f 72 65 20 74 68 65 0a 2a 2a 20 63 6f 6d  efore the.** com
1963a 70 61 72 69 73 6f 6e 20 69 73 20 6d 61 64 65 2e  parison is made.
1963b 20 49 66 20 74 68 65 20 53 51 4c 49 54 45 5f 41   If the SQLITE_A
1963c 46 46 5f 4d 41 53 4b 20 69 73 20 30 78 30 30 2c  FF_MASK is 0x00,
1963d 20 74 68 65 6e 20 6e 75 6d 65 72 69 63 0a 2a 2a   then numeric.**
1963e 20 61 66 66 69 6e 69 74 79 20 69 73 20 75 73 65   affinity is use
1963f 64 2e 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  d. Note that the
19640 20 61 66 66 69 6e 69 74 79 20 63 6f 6e 76 65 72   affinity conver
19641 73 69 6f 6e 73 20 61 72 65 20 73 74 6f 72 65 64  sions are stored
19642 0a 2a 2a 20 62 61 63 6b 20 69 6e 74 6f 20 74 68  .** back into th
19643 65 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72  e input register
19644 73 20 50 31 20 61 6e 64 20 50 33 2e 20 20 53 6f  s P1 and P3.  So
19645 20 74 68 69 73 20 6f 70 63 6f 64 65 20 63 61 6e   this opcode can
19646 20 63 61 75 73 65 0a 2a 2a 20 70 65 72 73 69 73   cause.** persis
19647 74 65 6e 74 20 63 68 61 6e 67 65 73 20 74 6f 20  tent changes to 
19648 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
19649 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 63 65 20   P3..**.** Once 
1964a 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20  any conversions 
1964b 68 61 76 65 20 74 61 6b 65 6e 20 70 6c 61 63 65  have taken place
1964c 2c 20 61 6e 64 20 6e 65 69 74 68 65 72 20 76 61  , and neither va
1964d 6c 75 65 20 69 73 20 4e 55 4c 4c 2c 20 0a 2a 2a  lue is NULL, .**
1964e 20 74 68 65 20 76 61 6c 75 65 73 20 61 72 65 20   the values are 
1964f 63 6f 6d 70 61 72 65 64 2e 20 49 66 20 62 6f 74  compared. If bot
19650 68 20 76 61 6c 75 65 73 20 61 72 65 20 62 6c 6f  h values are blo
19651 62 73 20 74 68 65 6e 20 6d 65 6d 63 6d 70 28 29  bs then memcmp()
19652 20 69 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20 64   is.** used to d
19653 65 74 65 72 6d 69 6e 65 20 74 68 65 20 72 65 73  etermine the res
19654 75 6c 74 73 20 6f 66 20 74 68 65 20 63 6f 6d 70  ults of the comp
19655 61 72 69 73 6f 6e 2e 20 20 49 66 20 62 6f 74 68  arison.  If both
19656 20 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 74   values.** are t
19657 65 78 74 2c 20 74 68 65 6e 20 74 68 65 20 61 70  ext, then the ap
19658 70 72 6f 70 72 69 61 74 65 20 63 6f 6c 6c 61 74  propriate collat
19659 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 73 70 65  ing function spe
1965a 63 69 66 69 65 64 20 69 6e 0a 2a 2a 20 50 34 20  cified in.** P4 
1965b 69 73 20 20 75 73 65 64 20 74 6f 20 64 6f 20 74  is  used to do t
1965c 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 20  he comparison.  
1965d 49 66 20 50 34 20 69 73 20 6e 6f 74 20 73 70 65  If P4 is not spe
1965e 63 69 66 69 65 64 20 74 68 65 6e 0a 2a 2a 20 6d  cified then.** m
1965f 65 6d 63 6d 70 28 29 20 69 73 20 75 73 65 64 20  emcmp() is used 
19660 74 6f 20 63 6f 6d 70 61 72 65 20 74 65 78 74 20  to compare text 
19661 73 74 72 69 6e 67 2e 20 20 49 66 20 62 6f 74 68  string.  If both
19662 20 76 61 6c 75 65 73 20 61 72 65 0a 2a 2a 20 6e   values are.** n
19663 75 6d 65 72 69 63 2c 20 74 68 65 6e 20 61 20 6e  umeric, then a n
19664 75 6d 65 72 69 63 20 63 6f 6d 70 61 72 69 73 6f  umeric compariso
19665 6e 20 69 73 20 75 73 65 64 2e 20 49 66 20 74 68  n is used. If th
19666 65 20 74 77 6f 20 76 61 6c 75 65 73 0a 2a 2a 20  e two values.** 
19667 61 72 65 20 6f 66 20 64 69 66 66 65 72 65 6e 74  are of different
19668 20 74 79 70 65 73 2c 20 74 68 65 6e 20 6e 75 6d   types, then num
19669 62 65 72 73 20 61 72 65 20 63 6f 6e 73 69 64 65  bers are conside
1966a 72 65 64 20 6c 65 73 73 20 74 68 61 6e 0a 2a 2a  red less than.**
1966b 20 73 74 72 69 6e 67 73 20 61 6e 64 20 73 74 72   strings and str
1966c 69 6e 67 73 20 61 72 65 20 63 6f 6e 73 69 64 65  ings are conside
1966d 72 65 64 20 6c 65 73 73 20 74 68 61 6e 20 62 6c  red less than bl
1966e 6f 62 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  obs..**.** If th
1966f 65 20 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32  e SQLITE_STOREP2
19670 20 62 69 74 20 6f 66 20 50 35 20 69 73 20 73 65   bit of P5 is se
19671 74 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a  t, then do not j
19672 75 6d 70 2e 20 20 49 6e 73 74 65 61 64 2c 0a 2a  ump.  Instead,.*
19673 2a 20 73 74 6f 72 65 20 61 20 62 6f 6f 6c 65 61  * store a boolea
19674 6e 20 72 65 73 75 6c 74 20 28 65 69 74 68 65 72  n result (either
19675 20 30 2c 20 6f 72 20 31 2c 20 6f 72 20 4e 55 4c   0, or 1, or NUL
19676 4c 29 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  L) in register P
19677 32 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  2..*/./* Opcode:
19678 20 4e 65 20 50 31 20 50 32 20 50 33 20 50 34 20   Ne P1 P2 P3 P4 
19679 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f  P5.**.** This wo
1967a 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68  rks just like th
1967b 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65  e Lt opcode exce
1967c 70 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70  pt that the jump
1967d 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20   is taken if.** 
1967e 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69 6e 20  the operands in 
1967f 72 65 67 69 73 74 65 72 73 20 50 31 20 61 6e 64  registers P1 and
19680 20 50 33 20 61 72 65 20 6e 6f 74 20 65 71 75 61   P3 are not equa
19681 6c 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f  l.  See the Lt o
19682 70 63 6f 64 65 20 66 6f 72 0a 2a 2a 20 61 64 64  pcode for.** add
19683 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74  itional informat
19684 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  ion..*/./* Opcod
19685 65 3a 20 45 71 20 50 31 20 50 32 20 50 33 20 50  e: Eq P1 P2 P3 P
19686 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  4 P5.**.** This 
19687 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20  works just like 
19688 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 65 78  the Lt opcode ex
19689 63 65 70 74 20 74 68 61 74 20 74 68 65 20 6a 75  cept that the ju
1968a 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a  mp is taken if.*
1968b 2a 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 69  * the operands i
1968c 6e 20 72 65 67 69 73 74 65 72 73 20 50 31 20 61  n registers P1 a
1968d 6e 64 20 50 33 20 61 72 65 20 65 71 75 61 6c 2e  nd P3 are equal.
1968e 0a 2a 2a 20 53 65 65 20 74 68 65 20 4c 74 20 6f  .** See the Lt o
1968f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69  pcode for additi
19690 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  onal information
19691 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
19692 4c 65 20 50 31 20 50 32 20 50 33 20 50 34 20 50  Le P1 P2 P3 P4 P
19693 35 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72  5.**.** This wor
19694 6b 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65  ks just like the
19695 20 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70   Lt opcode excep
19696 74 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20  t that the jump 
19697 69 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74  is taken if.** t
19698 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65  he content of re
19699 67 69 73 74 65 72 20 50 33 20 69 73 20 6c 65 73  gister P3 is les
1969a 73 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20  s than or equal 
1969b 74 6f 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f  to the content o
1969c 66 0a 2a 2a 20 72 65 67 69 73 74 65 72 20 50 31  f.** register P1
1969d 2e 20 20 53 65 65 20 74 68 65 20 4c 74 20 6f 70  .  See the Lt op
1969e 63 6f 64 65 20 66 6f 72 20 61 64 64 69 74 69 6f  code for additio
1969f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
196a0 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 47  .*/./* Opcode: G
196a1 74 20 50 31 20 50 32 20 50 33 20 50 34 20 50 35  t P1 P2 P3 P4 P5
196a2 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 77 6f 72 6b  .**.** This work
196a3 73 20 6a 75 73 74 20 6c 69 6b 65 20 74 68 65 20  s just like the 
196a4 4c 74 20 6f 70 63 6f 64 65 20 65 78 63 65 70 74  Lt opcode except
196a5 20 74 68 61 74 20 74 68 65 20 6a 75 6d 70 20 69   that the jump i
196a6 73 20 74 61 6b 65 6e 20 69 66 0a 2a 2a 20 74 68  s taken if.** th
196a7 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67  e content of reg
196a8 69 73 74 65 72 20 50 33 20 69 73 20 67 72 65 61  ister P3 is grea
196a9 74 65 72 20 74 68 61 6e 20 74 68 65 20 63 6f 6e  ter than the con
196aa 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67 69 73  tent of.** regis
196ab 74 65 72 20 50 31 2e 20 20 53 65 65 20 74 68 65  ter P1.  See the
196ac 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72 20 61   Lt opcode for a
196ad 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
196ae 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63  ation..*/./* Opc
196af 6f 64 65 3a 20 47 65 20 50 31 20 50 32 20 50 33  ode: Ge P1 P2 P3
196b0 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 54 68 69   P4 P5.**.** Thi
196b1 73 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b  s works just lik
196b2 65 20 74 68 65 20 4c 74 20 6f 70 63 6f 64 65 20  e the Lt opcode 
196b3 65 78 63 65 70 74 20 74 68 61 74 20 74 68 65 20  except that the 
196b4 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20 69 66  jump is taken if
196b5 0a 2a 2a 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  .** the content 
196b6 6f 66 20 72 65 67 69 73 74 65 72 20 50 33 20 69  of register P3 i
196b7 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 6f  s greater than o
196b8 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 63  r equal to the c
196b9 6f 6e 74 65 6e 74 20 6f 66 0a 2a 2a 20 72 65 67  ontent of.** reg
196ba 69 73 74 65 72 20 50 31 2e 20 20 53 65 65 20 74  ister P1.  See t
196bb 68 65 20 4c 74 20 6f 70 63 6f 64 65 20 66 6f 72  he Lt opcode for
196bc 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
196bd 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65  rmation..*/.case
196be 20 4f 50 5f 45 71 3a 20 20 20 20 20 20 20 20 20   OP_Eq:         
196bf 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
196c0 20 54 4b 5f 45 51 2c 20 6a 75 6d 70 2c 20 69 6e   TK_EQ, jump, in
196c1 31 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  1, in3 */.case O
196c2 50 5f 4e 65 3a 20 20 20 20 20 20 20 20 20 20 20  P_Ne:           
196c3 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
196c4 4b 5f 4e 45 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c  K_NE, jump, in1,
196c5 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f   in3 */.case OP_
196c6 4c 74 3a 20 20 20 20 20 20 20 20 20 20 20 20 20  Lt:             
196c7 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f    /* same as TK_
196c8 4c 54 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69  LT, jump, in1, i
196c9 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 4c 65  n3 */.case OP_Le
196ca 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  :               
196cb 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4c 45  /* same as TK_LE
196cc 2c 20 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33  , jump, in1, in3
196cd 20 2a 2f 0a 63 61 73 65 20 4f 50 5f 47 74 3a 20   */.case OP_Gt: 
196ce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
196cf 20 73 61 6d 65 20 61 73 20 54 4b 5f 47 54 2c 20   same as TK_GT, 
196d0 6a 75 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a  jump, in1, in3 *
196d1 2f 0a 63 61 73 65 20 4f 50 5f 47 65 3a 20 7b 20  /.case OP_Ge: { 
196d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
196d3 61 6d 65 20 61 73 20 54 4b 5f 47 45 2c 20 6a 75  ame as TK_GE, ju
196d4 6d 70 2c 20 69 6e 31 2c 20 69 6e 33 20 2a 2f 0a  mp, in1, in3 */.
196d5 20 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 69    int flags;.  i
196d6 6e 74 20 72 65 73 3b 0a 20 20 63 68 61 72 20 61  nt res;.  char a
196d7 66 66 69 6e 69 74 79 3b 0a 0a 20 20 66 6c 61 67  ffinity;..  flag
196d8 73 20 3d 20 70 49 6e 31 2d 3e 66 6c 61 67 73 7c  s = pIn1->flags|
196d9 70 49 6e 33 2d 3e 66 6c 61 67 73 3b 0a 0a 20 20  pIn3->flags;..  
196da 69 66 28 20 66 6c 61 67 73 26 4d 45 4d 5f 4e 75  if( flags&MEM_Nu
196db 6c 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  ll ){.    /* If 
196dc 65 69 74 68 65 72 20 6f 70 65 72 61 6e 64 20 69  either operand i
196dd 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65 20  s NULL then the 
196de 72 65 73 75 6c 74 20 69 73 20 61 6c 77 61 79 73  result is always
196df 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a 20 54 68   NULL..    ** Th
196e0 65 20 6a 75 6d 70 20 69 73 20 74 61 6b 65 6e 20  e jump is taken 
196e1 69 66 20 74 68 65 20 53 51 4c 49 54 45 5f 4a 55  if the SQLITE_JU
196e2 4d 50 49 46 4e 55 4c 4c 20 62 69 74 20 69 73 20  MPIFNULL bit is 
196e3 73 65 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  set..    */.    
196e4 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 53 51  if( pOp->p5 & SQ
196e5 4c 49 54 45 5f 53 54 4f 52 45 50 32 20 29 7b 0a  LITE_STOREP2 ){.
196e6 20 20 20 20 20 20 70 4f 75 74 20 3d 20 26 70 2d        pOut = &p-
196e7 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
196e8 20 20 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65        MemSetType
196e9 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 4e  Flag(pOut, MEM_N
196ea 75 6c 6c 29 3b 0a 20 20 20 20 20 20 52 45 47 49  ull);.      REGI
196eb 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e  STER_TRACE(pOp->
196ec 70 32 2c 20 70 4f 75 74 29 3b 0a 20 20 20 20 7d  p2, pOut);.    }
196ed 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 35  else if( pOp->p5
196ee 20 26 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46   & SQLITE_JUMPIF
196ef 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 70 63  NULL ){.      pc
196f0 20 3d 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20   = pOp->p2-1;.  
196f1 20 20 7d 0a 20 20 20 20 62 72 65 61 6b 3b 0a 20    }.    break;. 
196f2 20 7d 0a 0a 20 20 61 66 66 69 6e 69 74 79 20 3d   }..  affinity =
196f3 20 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54   pOp->p5 & SQLIT
196f4 45 5f 41 46 46 5f 4d 41 53 4b 3b 0a 20 20 69 66  E_AFF_MASK;.  if
196f5 28 20 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20  ( affinity ){.  
196f6 20 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28    applyAffinity(
196f7 70 49 6e 31 2c 20 61 66 66 69 6e 69 74 79 2c 20  pIn1, affinity, 
196f8 65 6e 63 6f 64 69 6e 67 29 3b 0a 20 20 20 20 61  encoding);.    a
196f9 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70 49 6e  pplyAffinity(pIn
196fa 33 2c 20 61 66 66 69 6e 69 74 79 2c 20 65 6e 63  3, affinity, enc
196fb 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 0a 20 20 61  oding);.  }..  a
196fc 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79  ssert( pOp->p4ty
196fd 70 65 3d 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 7c  pe==P4_COLLSEQ |
196fe 7c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c 6c 3d  | pOp->p4.pColl=
196ff 3d 30 20 29 3b 0a 20 20 45 78 70 61 6e 64 42 6c  =0 );.  ExpandBl
19700 6f 62 28 70 49 6e 31 29 3b 0a 20 20 45 78 70 61  ob(pIn1);.  Expa
19701 6e 64 42 6c 6f 62 28 70 49 6e 33 29 3b 0a 20 20  ndBlob(pIn3);.  
19702 72 65 73 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d  res = sqlite3Mem
19703 43 6f 6d 70 61 72 65 28 70 49 6e 33 2c 20 70 49  Compare(pIn3, pI
19704 6e 31 2c 20 70 4f 70 2d 3e 70 34 2e 70 43 6f 6c  n1, pOp->p4.pCol
19705 6c 29 3b 0a 20 20 73 77 69 74 63 68 28 20 70 4f  l);.  switch( pO
19706 70 2d 3e 6f 70 63 6f 64 65 20 29 7b 0a 20 20 20  p->opcode ){.   
19707 20 63 61 73 65 20 4f 50 5f 45 71 3a 20 20 20 20   case OP_Eq:    
19708 72 65 73 20 3d 20 72 65 73 3d 3d 30 3b 20 20 20  res = res==0;   
19709 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
1970a 65 20 4f 50 5f 4e 65 3a 20 20 20 20 72 65 73 20  e OP_Ne:    res 
1970b 3d 20 72 65 73 21 3d 30 3b 20 20 20 20 20 62 72  = res!=0;     br
1970c 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 4f 50  eak;.    case OP
1970d 5f 4c 74 3a 20 20 20 20 72 65 73 20 3d 20 72 65  _Lt:    res = re
1970e 73 3c 30 3b 20 20 20 20 20 20 62 72 65 61 6b 3b  s<0;      break;
1970f 0a 20 20 20 20 63 61 73 65 20 4f 50 5f 4c 65 3a  .    case OP_Le:
19710 20 20 20 20 72 65 73 20 3d 20 72 65 73 3c 3d 30      res = res<=0
19711 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20  ;     break;.   
19712 20 63 61 73 65 20 4f 50 5f 47 74 3a 20 20 20 20   case OP_Gt:    
19713 72 65 73 20 3d 20 72 65 73 3e 30 3b 20 20 20 20  res = res>0;    
19714 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65 66    break;.    def
19715 61 75 6c 74 3a 20 20 20 20 20 20 20 72 65 73 20  ault:       res 
19716 3d 20 72 65 73 3e 3d 30 3b 20 20 20 20 20 62 72  = res>=0;     br
19717 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20  eak;.  }..  if( 
19718 70 4f 70 2d 3e 70 35 20 26 20 53 51 4c 49 54 45  pOp->p5 & SQLITE
19719 5f 53 54 4f 52 45 50 32 20 29 7b 0a 20 20 20 20  _STOREP2 ){.    
1971a 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  pOut = &p->aMem[
1971b 70 4f 70 2d 3e 70 32 5d 3b 0a 20 20 20 20 4d 65  pOp->p2];.    Me
1971c 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70 4f 75  mSetTypeFlag(pOu
1971d 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 20  t, MEM_Int);.   
1971e 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 72 65 73   pOut->u.i = res
1971f 3b 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54  ;.    REGISTER_T
19720 52 41 43 45 28 70 4f 70 2d 3e 70 32 2c 20 70 4f  RACE(pOp->p2, pO
19721 75 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ut);.  }else if(
19722 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20 3d   res ){.    pc =
19723 20 70 4f 70 2d 3e 70 32 2d 31 3b 0a 20 20 7d 0a   pOp->p2-1;.  }.
19724 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
19725 70 63 6f 64 65 3a 20 50 65 72 6d 75 74 61 74 69  pcode: Permutati
19726 6f 6e 20 2a 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a  on * * * P4 *.**
19727 0a 2a 2a 20 53 65 74 20 74 68 65 20 70 65 72 6d  .** Set the perm
19728 75 61 74 69 6f 6e 20 75 73 65 64 20 62 79 20 74  uation used by t
19729 68 65 20 4f 50 5f 43 6f 6d 70 61 72 65 20 6f 70  he OP_Compare op
1972a 65 72 61 74 6f 72 20 74 6f 20 62 65 20 74 68 65  erator to be the
1972b 20 61 72 72 61 79 0a 2a 2a 20 6f 66 20 69 6e 74   array.** of int
1972c 65 67 65 72 73 20 69 6e 20 50 34 2e 0a 2a 2a 0a  egers in P4..**.
1972d 2a 2a 20 54 68 65 20 70 65 72 6d 75 74 61 74 69  ** The permutati
1972e 6f 6e 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64  on is only valid
1972f 20 75 6e 74 69 6c 20 74 68 65 20 6e 65 78 74 20   until the next 
19730 4f 50 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20  OP_Permutation, 
19731 4f 50 5f 43 6f 6d 70 61 72 65 2c 0a 2a 2a 20 4f  OP_Compare,.** O
19732 50 5f 48 61 6c 74 2c 20 6f 72 20 4f 50 5f 52 65  P_Halt, or OP_Re
19733 73 75 6c 74 52 6f 77 2e 20 20 54 79 70 69 63 61  sultRow.  Typica
19734 6c 6c 79 20 74 68 65 20 4f 50 5f 50 65 72 6d 75  lly the OP_Permu
19735 74 61 74 69 6f 6e 20 73 68 6f 75 6c 64 20 6f 63  tation should oc
19736 63 75 72 0a 2a 2a 20 69 6d 6d 65 64 69 61 74 65  cur.** immediate
19737 6c 79 20 70 72 69 6f 72 20 74 6f 20 74 68 65 20  ly prior to the 
19738 4f 50 5f 43 6f 6d 70 61 72 65 2e 0a 2a 2f 0a 63  OP_Compare..*/.c
19739 61 73 65 20 4f 50 5f 50 65 72 6d 75 74 61 74 69  ase OP_Permutati
1973a 6f 6e 3a 20 7b 0a 20 20 61 73 73 65 72 74 28 20  on: {.  assert( 
1973b 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
1973c 49 4e 54 41 52 52 41 59 20 29 3b 0a 20 20 61 73  INTARRAY );.  as
1973d 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 61 69  sert( pOp->p4.ai
1973e 20 29 3b 0a 20 20 61 50 65 72 6d 75 74 65 20 3d   );.  aPermute =
1973f 20 70 4f 70 2d 3e 70 34 2e 61 69 3b 0a 20 20 62   pOp->p4.ai;.  b
19740 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
19741 64 65 3a 20 43 6f 6d 70 61 72 65 20 50 31 20 50  de: Compare P1 P
19742 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20  2 P3 P4 *.**.** 
19743 43 6f 6d 70 61 72 65 20 74 6f 20 76 65 63 74 6f  Compare to vecto
19744 72 73 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  rs of registers 
19745 69 6e 20 72 65 67 28 50 31 29 2e 2e 72 65 67 28  in reg(P1)..reg(
19746 50 31 2b 50 33 2d 31 29 20 28 61 6c 6c 20 74 68  P1+P3-1) (all th
19747 69 73 0a 2a 2a 20 6f 6e 65 20 22 41 22 29 20 61  is.** one "A") a
19748 6e 64 20 69 6e 20 72 65 67 28 50 32 29 2e 2e 72  nd in reg(P2)..r
19749 65 67 28 50 32 2b 50 33 2d 31 29 20 28 22 42 22  eg(P2+P3-1) ("B"
1974a 29 2e 20 20 53 61 76 65 20 74 68 65 20 72 65 73  ).  Save the res
1974b 75 6c 74 20 6f 66 0a 2a 2a 20 74 68 65 20 63 6f  ult of.** the co
1974c 6d 70 61 72 69 73 6f 6e 20 66 6f 72 20 75 73 65  mparison for use
1974d 20 62 79 20 74 68 65 20 6e 65 78 74 20 4f 50 5f   by the next OP_
1974e 4a 75 6d 70 20 69 6e 73 74 72 75 63 74 2e 0a 2a  Jump instruct..*
1974f 2a 0a 2a 2a 20 50 34 20 69 73 20 61 20 4b 65 79  *.** P4 is a Key
19750 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74  Info structure t
19751 68 61 74 20 64 65 66 69 6e 65 73 20 63 6f 6c 6c  hat defines coll
19752 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
19753 61 6e 64 20 73 6f 72 74 0a 2a 2a 20 6f 72 64 65  and sort.** orde
19754 72 73 20 66 6f 72 20 74 68 65 20 63 6f 6d 70 61  rs for the compa
19755 72 69 73 6f 6e 2e 20 20 54 68 65 20 70 65 72 6d  rison.  The perm
19756 75 74 61 74 69 6f 6e 20 61 70 70 6c 69 65 73 20  utation applies 
19757 74 6f 20 72 65 67 69 73 74 65 72 73 0a 2a 2a 20  to registers.** 
19758 6f 6e 6c 79 2e 20 20 54 68 65 20 4b 65 79 49 6e  only.  The KeyIn
19759 66 6f 20 65 6c 65 6d 65 6e 74 73 20 61 72 65 20  fo elements are 
1975a 75 73 65 64 20 73 65 71 75 65 6e 74 69 61 6c 6c  used sequentiall
1975b 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6d  y..**.** The com
1975c 70 61 72 69 73 6f 6e 20 69 73 20 61 20 73 6f 72  parison is a sor
1975d 74 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 73 6f  t comparison, so
1975e 20 4e 55 4c 4c 73 20 63 6f 6d 70 61 72 65 20 65   NULLs compare e
1975f 71 75 61 6c 2c 0a 2a 2a 20 4e 55 4c 4c 73 20 61  qual,.** NULLs a
19760 72 65 20 6c 65 73 73 20 74 68 61 6e 20 6e 75 6d  re less than num
19761 62 65 72 73 2c 20 6e 75 6d 62 65 72 73 20 61 72  bers, numbers ar
19762 65 20 6c 65 73 73 20 74 68 61 6e 20 73 74 72 69  e less than stri
19763 6e 67 73 2c 0a 2a 2a 20 61 6e 64 20 73 74 72 69  ngs,.** and stri
19764 6e 67 73 20 61 72 65 20 6c 65 73 73 20 74 68 61  ngs are less tha
19765 6e 20 62 6c 6f 62 73 2e 0a 2a 2f 0a 63 61 73 65  n blobs..*/.case
19766 20 4f 50 5f 43 6f 6d 70 61 72 65 3a 20 7b 0a 20   OP_Compare: {. 
19767 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e 70 33   int n = pOp->p3
19768 3b 0a 20 20 69 6e 74 20 69 2c 20 70 31 2c 20 70  ;.  int i, p1, p
19769 32 3b 0a 20 20 63 6f 6e 73 74 20 4b 65 79 49 6e  2;.  const KeyIn
1976a 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 20 3d 20 70  fo *pKeyInfo = p
1976b 4f 70 2d 3e 70 34 2e 70 4b 65 79 49 6e 66 6f 3b  Op->p4.pKeyInfo;
1976c 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
1976d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ;.  assert( pKey
1976e 49 6e 66 6f 21 3d 30 20 29 3b 0a 20 20 70 31 20  Info!=0 );.  p1 
1976f 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
19770 65 72 74 28 20 70 31 3e 30 20 26 26 20 70 31 2b  ert( p1>0 && p1+
19771 6e 2d 31 3c 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  n-1<p->nMem );. 
19772 20 70 32 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20   p2 = pOp->p2;. 
19773 20 61 73 73 65 72 74 28 20 70 32 3e 30 20 26 26   assert( p2>0 &&
19774 20 70 32 2b 6e 2d 31 3c 70 2d 3e 6e 4d 65 6d 20   p2+n-1<p->nMem 
19775 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
19776 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  n; i++){.    int
19777 20 69 64 78 20 3d 20 61 50 65 72 6d 75 74 65 20   idx = aPermute 
19778 3f 20 61 50 65 72 6d 75 74 65 5b 69 5d 20 3a 20  ? aPermute[i] : 
19779 69 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  i;.    CollSeq *
1977a 70 43 6f 6c 6c 3b 20 20 20 20 2f 2a 20 43 6f 6c  pColl;    /* Col
1977b 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
1977c 74 6f 20 75 73 65 20 6f 6e 20 74 68 69 73 20 74  to use on this t
1977d 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 62  erm */.    int b
1977e 52 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  Rev;          /*
1977f 20 54 72 75 65 20 66 6f 72 20 44 45 53 43 45 4e   True for DESCEN
19780 44 49 4e 47 20 73 6f 72 74 20 6f 72 64 65 72 20  DING sort order 
19781 2a 2f 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f  */.    REGISTER_
19782 54 52 41 43 45 28 70 31 2b 69 64 78 2c 20 26 70  TRACE(p1+idx, &p
19783 2d 3e 61 4d 65 6d 5b 70 31 2b 69 64 78 5d 29 3b  ->aMem[p1+idx]);
19784 0a 20 20 20 20 52 45 47 49 53 54 45 52 5f 54 52  .    REGISTER_TR
19785 41 43 45 28 70 32 2b 69 64 78 2c 20 26 70 2d 3e  ACE(p2+idx, &p->
19786 61 4d 65 6d 5b 70 32 2b 69 64 78 5d 29 3b 0a 20  aMem[p2+idx]);. 
19787 20 20 20 61 73 73 65 72 74 28 20 69 3c 70 4b 65     assert( i<pKe
19788 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 29 3b  yInfo->nField );
19789 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4b 65  .    pColl = pKe
1978a 79 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 5b 69 5d 3b  yInfo->aColl[i];
1978b 0a 20 20 20 20 62 52 65 76 20 3d 20 70 4b 65 79  .    bRev = pKey
1978c 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Info->aSortOrder
1978d 5b 69 5d 3b 0a 20 20 20 20 69 43 6f 6d 70 61 72  [i];.    iCompar
1978e 65 20 3d 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f  e = sqlite3MemCo
1978f 6d 70 61 72 65 28 26 70 2d 3e 61 4d 65 6d 5b 70  mpare(&p->aMem[p
19790 31 2b 69 64 78 5d 2c 20 26 70 2d 3e 61 4d 65 6d  1+idx], &p->aMem
19791 5b 70 32 2b 69 64 78 5d 2c 20 70 43 6f 6c 6c 29  [p2+idx], pColl)
19792 3b 0a 20 20 20 20 69 66 28 20 69 43 6f 6d 70 61  ;.    if( iCompa
19793 72 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  re ){.      if( 
19794 62 52 65 76 20 29 20 69 43 6f 6d 70 61 72 65 20  bRev ) iCompare 
19795 3d 20 2d 69 43 6f 6d 70 61 72 65 3b 0a 20 20 20  = -iCompare;.   
19796 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
19797 20 20 7d 0a 20 20 61 50 65 72 6d 75 74 65 20 3d    }.  aPermute =
19798 20 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a   0;.  break;.}..
19799 2f 2a 20 4f 70 63 6f 64 65 3a 20 4a 75 6d 70 20  /* Opcode: Jump 
1979a 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a  P1 P2 P3 * *.**.
1979b 2a 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20 69  ** Jump to the i
1979c 6e 73 74 72 75 63 74 69 6f 6e 20 61 74 20 61 64  nstruction at ad
1979d 64 72 65 73 73 20 50 31 2c 20 50 32 2c 20 6f 72  dress P1, P2, or
1979e 20 50 33 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e   P3 depending on
1979f 20 77 68 65 74 68 65 72 0a 2a 2a 20 69 6e 20 74   whether.** in t
197a0 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 4f  he most recent O
197a1 50 5f 43 6f 6d 70 61 72 65 20 69 6e 73 74 72 75  P_Compare instru
197a2 63 74 69 6f 6e 20 74 68 65 20 50 31 20 76 65 63  ction the P1 vec
197a3 74 6f 72 20 77 61 73 20 6c 65 73 73 20 74 68 61  tor was less tha
197a4 6e 0a 2a 2a 20 65 71 75 61 6c 20 74 6f 2c 20 6f  n.** equal to, o
197a5 72 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  r greater than t
197a6 68 65 20 50 32 20 76 65 63 74 6f 72 2c 20 72 65  he P2 vector, re
197a7 73 70 65 63 74 69 76 65 6c 79 2e 0a 2a 2f 0a 63  spectively..*/.c
197a8 61 73 65 20 4f 50 5f 4a 75 6d 70 3a 20 7b 20 20  ase OP_Jump: {  
197a9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
197aa 6d 70 20 2a 2f 0a 20 20 69 66 28 20 69 43 6f 6d  mp */.  if( iCom
197ab 70 61 72 65 3c 30 20 29 7b 0a 20 20 20 20 70 63  pare<0 ){.    pc
197ac 20 3d 20 70 4f 70 2d 3e 70 31 20 2d 20 31 3b 0a   = pOp->p1 - 1;.
197ad 20 20 7d 65 6c 73 65 20 69 66 28 20 69 43 6f 6d    }else if( iCom
197ae 70 61 72 65 3d 3d 30 20 29 7b 0a 20 20 20 20 70  pare==0 ){.    p
197af 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
197b0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 63  .  }else{.    pc
197b1 20 3d 20 70 4f 70 2d 3e 70 33 20 2d 20 31 3b 0a   = pOp->p3 - 1;.
197b2 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
197b3 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 6e 64 20 50  /* Opcode: And P
197b4 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
197b5 2a 20 54 61 6b 65 20 74 68 65 20 6c 6f 67 69 63  * Take the logic
197b6 61 6c 20 41 4e 44 20 6f 66 20 74 68 65 20 76 61  al AND of the va
197b7 6c 75 65 73 20 69 6e 20 72 65 67 69 73 74 65 72  lues in register
197b8 73 20 50 31 20 61 6e 64 20 50 32 20 61 6e 64 0a  s P1 and P2 and.
197b9 2a 2a 20 77 72 69 74 65 20 74 68 65 20 72 65 73  ** write the res
197ba 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74 65  ult into registe
197bb 72 20 50 33 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 65  r P3..**.** If e
197bc 69 74 68 65 72 20 50 31 20 6f 72 20 50 32 20 69  ither P1 or P2 i
197bd 73 20 30 20 28 66 61 6c 73 65 29 20 74 68 65 6e  s 0 (false) then
197be 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20 30   the result is 0
197bf 20 65 76 65 6e 20 69 66 0a 2a 2a 20 74 68 65 20   even if.** the 
197c0 6f 74 68 65 72 20 69 6e 70 75 74 20 69 73 20 4e  other input is N
197c1 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c 20 61 6e 64  ULL.  A NULL and
197c2 20 74 72 75 65 20 6f 72 20 74 77 6f 20 4e 55 4c   true or two NUL
197c3 4c 73 20 67 69 76 65 0a 2a 2a 20 61 20 4e 55 4c  Ls give.** a NUL
197c4 4c 20 6f 75 74 70 75 74 2e 0a 2a 2f 0a 2f 2a 20  L output..*/./* 
197c5 4f 70 63 6f 64 65 3a 20 4f 72 20 50 31 20 50 32  Opcode: Or P1 P2
197c6 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 61   P3 * *.**.** Ta
197c7 6b 65 20 74 68 65 20 6c 6f 67 69 63 61 6c 20 4f  ke the logical O
197c8 52 20 6f 66 20 74 68 65 20 76 61 6c 75 65 73 20  R of the values 
197c9 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 61  in register P1 a
197ca 6e 64 20 50 32 20 61 6e 64 0a 2a 2a 20 73 74 6f  nd P2 and.** sto
197cb 72 65 20 74 68 65 20 61 6e 73 77 65 72 20 69 6e  re the answer in
197cc 20 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2a   register P3..**
197cd 0a 2a 2a 20 49 66 20 65 69 74 68 65 72 20 50 31  .** If either P1
197ce 20 6f 72 20 50 32 20 69 73 20 6e 6f 6e 7a 65 72   or P2 is nonzer
197cf 6f 20 28 74 72 75 65 29 20 74 68 65 6e 20 74 68  o (true) then th
197d0 65 20 72 65 73 75 6c 74 20 69 73 20 31 20 28 74  e result is 1 (t
197d1 72 75 65 29 0a 2a 2a 20 65 76 65 6e 20 69 66 20  rue).** even if 
197d2 74 68 65 20 6f 74 68 65 72 20 69 6e 70 75 74 20  the other input 
197d3 69 73 20 4e 55 4c 4c 2e 20 20 41 20 4e 55 4c 4c  is NULL.  A NULL
197d4 20 61 6e 64 20 66 61 6c 73 65 20 6f 72 20 74 77   and false or tw
197d5 6f 20 4e 55 4c 4c 73 0a 2a 2a 20 67 69 76 65 20  o NULLs.** give 
197d6 61 20 4e 55 4c 4c 20 6f 75 74 70 75 74 2e 0a 2a  a NULL output..*
197d7 2f 0a 63 61 73 65 20 4f 50 5f 41 6e 64 3a 20 20  /.case OP_And:  
197d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
197d9 61 6d 65 20 61 73 20 54 4b 5f 41 4e 44 2c 20 69  ame as TK_AND, i
197da 6e 31 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f  n1, in2, out3 */
197db 0a 63 61 73 65 20 4f 50 5f 4f 72 3a 20 7b 20 20  .case OP_Or: {  
197dc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73 61             /* sa
197dd 6d 65 20 61 73 20 54 4b 5f 4f 52 2c 20 69 6e 31  me as TK_OR, in1
197de 2c 20 69 6e 32 2c 20 6f 75 74 33 20 2a 2f 0a 20  , in2, out3 */. 
197df 20 69 6e 74 20 76 31 2c 20 76 32 3b 20 20 20 20   int v1, v2;    
197e0 2f 2a 20 30 3d 3d 46 41 4c 53 45 2c 20 31 3d 3d  /* 0==FALSE, 1==
197e1 54 52 55 45 2c 20 32 3d 3d 55 4e 4b 4e 4f 57 4e  TRUE, 2==UNKNOWN
197e2 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20 69   or NULL */..  i
197e3 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 20 26  f( pIn1->flags &
197e4 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
197e5 20 76 31 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65   v1 = 2;.  }else
197e6 7b 0a 20 20 20 20 76 31 20 3d 20 73 71 6c 69 74  {.    v1 = sqlit
197e7 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
197e8 49 6e 31 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69  In1)!=0;.  }.  i
197e9 66 28 20 70 49 6e 32 2d 3e 66 6c 61 67 73 20 26  f( pIn2->flags &
197ea 20 4d 45 4d 5f 4e 75 6c 6c 20 29 7b 0a 20 20 20   MEM_Null ){.   
197eb 20 76 32 20 3d 20 32 3b 0a 20 20 7d 65 6c 73 65   v2 = 2;.  }else
197ec 7b 0a 20 20 20 20 76 32 20 3d 20 73 71 6c 69 74  {.    v2 = sqlit
197ed 65 33 56 64 62 65 49 6e 74 56 61 6c 75 65 28 70  e3VdbeIntValue(p
197ee 49 6e 32 29 21 3d 30 3b 0a 20 20 7d 0a 20 20 69  In2)!=0;.  }.  i
197ef 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
197f0 4f 50 5f 41 6e 64 20 29 7b 0a 20 20 20 20 73 74  OP_And ){.    st
197f1 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67  atic const unsig
197f2 6e 65 64 20 63 68 61 72 20 61 6e 64 5f 6c 6f 67  ned char and_log
197f3 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 30 2c 20 30  ic[] = { 0, 0, 0
197f4 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 32 2c  , 0, 1, 2, 0, 2,
197f5 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 20 61   2 };.    v1 = a
197f6 6e 64 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b 76 32  nd_logic[v1*3+v2
197f7 5d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  ];.  }else{.    
197f8 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73  static const uns
197f9 69 67 6e 65 64 20 63 68 61 72 20 6f 72 5f 6c 6f  igned char or_lo
197fa 67 69 63 5b 5d 20 3d 20 7b 20 30 2c 20 31 2c 20  gic[] = { 0, 1, 
197fb 32 2c 20 31 2c 20 31 2c 20 31 2c 20 32 2c 20 31  2, 1, 1, 1, 2, 1
197fc 2c 20 32 20 7d 3b 0a 20 20 20 20 76 31 20 3d 20  , 2 };.    v1 = 
197fd 6f 72 5f 6c 6f 67 69 63 5b 76 31 2a 33 2b 76 32  or_logic[v1*3+v2
197fe 5d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 76 31 3d  ];.  }.  if( v1=
197ff 3d 32 20 29 7b 0a 20 20 20 20 4d 65 6d 53 65 74  =2 ){.    MemSet
19800 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d  TypeFlag(pOut, M
19801 45 4d 5f 4e 75 6c 6c 29 3b 0a 20 20 7d 65 6c 73  EM_Null);.  }els
19802 65 7b 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69  e{.    pOut->u.i
19803 20 3d 20 76 31 3b 0a 20 20 20 20 4d 65 6d 53 65   = v1;.    MemSe
19804 74 54 79 70 65 46 6c 61 67 28 70 4f 75 74 2c 20  tTypeFlag(pOut, 
19805 4d 45 4d 5f 49 6e 74 29 3b 0a 20 20 7d 0a 20 20  MEM_Int);.  }.  
19806 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
19807 6f 64 65 3a 20 4e 6f 74 20 50 31 20 2a 20 2a 20  ode: Not P1 * * 
19808 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70  * *.**.** Interp
19809 72 65 74 20 74 68 65 20 76 61 6c 75 65 20 69 6e  ret the value in
1980a 20 72 65 67 69 73 74 65 72 20 50 31 20 61 73 20   register P1 as 
1980b 61 20 62 6f 6f 6c 65 61 6e 20 76 61 6c 75 65 2e  a boolean value.
1980c 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20    Replace it.** 
1980d 77 69 74 68 20 69 74 73 20 63 6f 6d 70 6c 65 6d  with its complem
1980e 65 6e 74 2e 20 20 49 66 20 74 68 65 20 76 61 6c  ent.  If the val
1980f 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
19810 31 20 69 73 20 4e 55 4c 4c 20 69 74 73 20 76 61  1 is NULL its va
19811 6c 75 65 0a 2a 2a 20 69 73 20 75 6e 63 68 61 6e  lue.** is unchan
19812 67 65 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ged..*/.case OP_
19813 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20 20  Not: {          
19814 20 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73        /* same as
19815 20 54 4b 5f 4e 4f 54 2c 20 69 6e 31 20 2a 2f 0a   TK_NOT, in1 */.
19816 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61 67    if( pIn1->flag
19817 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20 62  s & MEM_Null ) b
19818 72 65 61 6b 3b 20 20 2f 2a 20 44 6f 20 6e 6f 74  reak;  /* Do not
19819 68 69 6e 67 20 74 6f 20 4e 55 4c 4c 73 20 2a 2f  hing to NULLs */
1981a 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1981b 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e 31  mIntegerify(pIn1
1981c 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20 3d  );.  pIn1->u.i =
1981d 20 21 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20 61   !pIn1->u.i;.  a
1981e 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61  ssert( pIn1->fla
1981f 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20  gs&MEM_Int );.  
19820 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
19821 6f 64 65 3a 20 42 69 74 4e 6f 74 20 50 31 20 2a  ode: BitNot P1 *
19822 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 6e 74   * * *.**.** Int
19823 65 72 70 72 65 74 20 74 68 65 20 63 6f 6e 74 65  erpret the conte
19824 6e 74 20 6f 66 20 72 65 67 69 73 74 65 72 20 50  nt of register P
19825 31 20 61 73 20 61 6e 20 69 6e 74 65 67 65 72 2e  1 as an integer.
19826 20 20 52 65 70 6c 61 63 65 20 69 74 0a 2a 2a 20    Replace it.** 
19827 77 69 74 68 20 69 74 73 20 6f 6e 65 73 2d 63 6f  with its ones-co
19828 6d 70 6c 65 6d 65 6e 74 2e 20 20 49 66 20 74 68  mplement.  If th
19829 65 20 76 61 6c 75 65 20 69 73 20 6f 72 69 67 69  e value is origi
1982a 6e 61 6c 6c 79 20 4e 55 4c 4c 2c 20 6c 65 61 76  nally NULL, leav
1982b 65 0a 2a 2a 20 69 74 20 75 6e 63 68 61 6e 67 65  e.** it unchange
1982c 64 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 42 69  d..*/.case OP_Bi
1982d 74 4e 6f 74 3a 20 7b 20 20 20 20 20 20 20 20 20  tNot: {         
1982e 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20 54      /* same as T
1982f 4b 5f 42 49 54 4e 4f 54 2c 20 69 6e 31 20 2a 2f  K_BITNOT, in1 */
19830 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e 66 6c 61  .  if( pIn1->fla
19831 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c 20 29 20  gs & MEM_Null ) 
19832 62 72 65 61 6b 3b 20 20 2f 2a 20 44 6f 20 6e 6f  break;  /* Do no
19833 74 68 69 6e 67 20 74 6f 20 4e 55 4c 4c 73 20 2a  thing to NULLs *
19834 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d  /.  sqlite3VdbeM
19835 65 6d 49 6e 74 65 67 65 72 69 66 79 28 70 49 6e  emIntegerify(pIn
19836 31 29 3b 0a 20 20 70 49 6e 31 2d 3e 75 2e 69 20  1);.  pIn1->u.i 
19837 3d 20 7e 70 49 6e 31 2d 3e 75 2e 69 3b 0a 20 20  = ~pIn1->u.i;.  
19838 61 73 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c  assert( pIn1->fl
19839 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  ags&MEM_Int );. 
1983a 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70   break;.}../* Op
1983b 63 6f 64 65 3a 20 49 66 20 50 31 20 50 32 20 50  code: If P1 P2 P
1983c 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70  3 * *.**.** Jump
1983d 20 74 6f 20 50 32 20 69 66 20 74 68 65 20 76 61   to P2 if the va
1983e 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1983f 50 31 20 69 73 20 74 72 75 65 2e 20 20 54 68 65  P1 is true.  The
19840 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 69 73 20   value is.** is 
19841 63 6f 6e 73 69 64 65 72 65 64 20 74 72 75 65 20  considered true 
19842 69 66 20 69 74 20 69 73 20 6e 75 6d 65 72 69 63  if it is numeric
19843 20 61 6e 64 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20   and non-zero.  
19844 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
19845 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68  in P1 is NULL th
19846 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  en take the jump
19847 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a   if P3 is true..
19848 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 66  */./* Opcode: If
19849 4e 6f 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a  Not P1 P2 P3 * *
1984a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f 20 50  .**.** Jump to P
1984b 32 20 69 66 20 74 68 65 20 76 61 6c 75 65 20 69  2 if the value i
1984c 6e 20 72 65 67 69 73 74 65 72 20 50 31 20 69 73  n register P1 is
1984d 20 46 61 6c 73 65 2e 20 20 54 68 65 20 76 61 6c   False.  The val
1984e 75 65 20 69 73 0a 2a 2a 20 69 73 20 63 6f 6e 73  ue is.** is cons
1984f 69 64 65 72 65 64 20 74 72 75 65 20 69 66 20 69  idered true if i
19850 74 20 68 61 73 20 61 20 6e 75 6d 65 72 69 63 20  t has a numeric 
19851 76 61 6c 75 65 20 6f 66 20 7a 65 72 6f 2e 20 20  value of zero.  
19852 49 66 20 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20  If the value.** 
19853 69 6e 20 50 31 20 69 73 20 4e 55 4c 4c 20 74 68  in P1 is NULL th
19854 65 6e 20 74 61 6b 65 20 74 68 65 20 6a 75 6d 70  en take the jump
19855 20 69 66 20 50 33 20 69 73 20 74 72 75 65 2e 0a   if P3 is true..
19856 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 3a 20 20  */.case OP_If:  
19857 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
19858 2a 20 6a 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 63  * jump, in1 */.c
19859 61 73 65 20 4f 50 5f 49 66 4e 6f 74 3a 20 7b 20  ase OP_IfNot: { 
1985a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75             /* ju
1985b 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74  mp, in1 */.  int
1985c 20 63 3b 0a 20 20 69 66 28 20 70 49 6e 31 2d 3e   c;.  if( pIn1->
1985d 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75 6c 6c  flags & MEM_Null
1985e 20 29 7b 0a 20 20 20 20 63 20 3d 20 70 4f 70 2d   ){.    c = pOp-
1985f 3e 70 33 3b 0a 20 20 7d 65 6c 73 65 7b 0a 23 69  >p3;.  }else{.#i
19860 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
19861 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
19862 20 20 20 20 63 20 3d 20 73 71 6c 69 74 65 33 56      c = sqlite3V
19863 64 62 65 49 6e 74 56 61 6c 75 65 28 70 49 6e 31  dbeIntValue(pIn1
19864 29 3b 0a 23 65 6c 73 65 0a 20 20 20 20 63 20 3d  );.#else.    c =
19865 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 61 6c   sqlite3VdbeReal
19866 56 61 6c 75 65 28 70 49 6e 31 29 21 3d 30 2e 30  Value(pIn1)!=0.0
19867 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28  ;.#endif.    if(
19868 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50   pOp->opcode==OP
19869 5f 49 66 4e 6f 74 20 29 20 63 20 3d 20 21 63 3b  _IfNot ) c = !c;
1986a 0a 20 20 7d 0a 20 20 69 66 28 20 63 20 29 7b 0a  .  }.  if( c ){.
1986b 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32      pc = pOp->p2
1986c 2d 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  -1;.  }.  break;
1986d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49  .}../* Opcode: I
1986e 73 4e 75 6c 6c 20 50 31 20 50 32 20 50 33 20 2a  sNull P1 P2 P3 *
1986f 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74 6f   *.**.** Jump to
19870 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75 65   P2 if the value
19871 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31 20   in register P1 
19872 69 73 20 4e 55 4c 4c 2e 20 20 49 66 20 50 33 20  is NULL.  If P3 
19873 69 73 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68  is greater.** th
19874 61 6e 20 7a 65 72 6f 2c 20 74 68 65 6e 20 63 68  an zero, then ch
19875 65 63 6b 20 61 6c 6c 20 76 61 6c 75 65 73 20 72  eck all values r
19876 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b 31  eg(P1), reg(P1+1
19877 29 2c 20 0a 2a 2a 20 72 65 67 28 50 31 2b 32 29  ), .** reg(P1+2)
19878 2c 20 2e 2e 2e 2c 20 72 65 67 28 50 31 2b 50 33  , ..., reg(P1+P3
19879 2d 31 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  -1)..*/.case OP_
1987a 49 73 4e 75 6c 6c 3a 20 7b 20 20 20 20 20 20 20  IsNull: {       
1987b 20 20 20 20 20 2f 2a 20 73 61 6d 65 20 61 73 20       /* same as 
1987c 54 4b 5f 49 53 4e 55 4c 4c 2c 20 6a 75 6d 70 2c  TK_ISNULL, jump,
1987d 20 69 6e 31 20 2a 2f 0a 20 20 69 6e 74 20 6e 20   in1 */.  int n 
1987e 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20 61 73 73  = pOp->p3;.  ass
1987f 65 72 74 28 20 70 4f 70 2d 3e 70 33 3d 3d 30 20  ert( pOp->p3==0 
19880 7c 7c 20 70 4f 70 2d 3e 70 31 3e 30 20 29 3b 0a  || pOp->p1>0 );.
19881 20 20 64 6f 7b 0a 20 20 20 20 69 66 28 20 28 70    do{.    if( (p
19882 49 6e 31 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In1->flags & MEM
19883 5f 4e 75 6c 6c 29 21 3d 30 20 29 7b 0a 20 20 20  _Null)!=0 ){.   
19884 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
19885 2d 20 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  - 1;.      break
19886 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 6e 31  ;.    }.    pIn1
19887 2b 2b 3b 0a 20 20 7d 77 68 69 6c 65 28 20 2d 2d  ++;.  }while( --
19888 6e 20 3e 20 30 20 29 3b 0a 20 20 62 72 65 61 6b  n > 0 );.  break
19889 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
1988a 4e 6f 74 4e 75 6c 6c 20 50 31 20 50 32 20 2a 20  NotNull P1 P2 * 
1988b 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4a 75 6d 70 20 74  * *.**.** Jump t
1988c 6f 20 50 32 20 69 66 20 74 68 65 20 76 61 6c 75  o P2 if the valu
1988d 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 31  e in register P1
1988e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20 0a   is not NULL.  .
1988f 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 4e 75  */.case OP_NotNu
19890 6c 6c 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ll: {           
19891 20 2f 2a 20 73 61 6d 65 20 61 73 20 54 4b 5f 4e   /* same as TK_N
19892 4f 54 4e 55 4c 4c 2c 20 6a 75 6d 70 2c 20 69 6e  OTNULL, jump, in
19893 31 20 2a 2f 0a 20 20 69 66 28 20 28 70 49 6e 31  1 */.  if( (pIn1
19894 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
19895 6c 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 70 63  ll)==0 ){.    pc
19896 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
19897 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
19898 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74 4e 75  /* Opcode: SetNu
19899 6d 43 6f 6c 75 6d 6e 73 20 2a 20 50 32 20 2a 20  mColumns * P2 * 
1989a 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f  * *.**.** This o
1989b 70 63 6f 64 65 20 73 65 74 73 20 74 68 65 20 6e  pcode sets the n
1989c 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
1989d 20 66 6f 72 20 74 68 65 20 63 75 72 73 6f 72 20   for the cursor 
1989e 6f 70 65 6e 65 64 20 62 79 20 74 68 65 0a 2a 2a  opened by the.**
1989f 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 73 74 72   following instr
198a0 75 63 74 69 6f 6e 20 74 6f 20 50 32 2e 0a 2a 2a  uction to P2..**
198a1 0a 2a 2a 20 41 6e 20 4f 50 5f 53 65 74 4e 75 6d  .** An OP_SetNum
198a2 43 6f 6c 75 6d 6e 73 20 69 73 20 6f 6e 6c 79 20  Columns is only 
198a3 75 73 65 66 75 6c 20 69 66 20 69 74 20 6f 63 63  useful if it occ
198a4 75 72 73 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  urs immediately 
198a5 62 65 66 6f 72 65 20 0a 2a 2a 20 6f 6e 65 20 6f  before .** one o
198a6 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
198a7 6f 70 63 6f 64 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  opcodes:.**.**  
198a8 20 20 20 4f 70 65 6e 52 65 61 64 0a 2a 2a 20 20     OpenRead.**  
198a9 20 20 20 4f 70 65 6e 57 72 69 74 65 0a 2a 2a 20     OpenWrite.** 
198aa 20 20 20 20 4f 70 65 6e 50 73 65 75 64 6f 0a 2a      OpenPseudo.*
198ab 2a 0a 2a 2a 20 49 66 20 74 68 65 20 4f 50 5f 43  *.** If the OP_C
198ac 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 69 73 20  olumn opcode is 
198ad 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 6f  to be executed o
198ae 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68 65 6e  n a cursor, then
198af 0a 2a 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 20  .** this opcode 
198b0 6d 75 73 74 20 62 65 20 70 72 65 73 65 6e 74 20  must be present 
198b1 69 6d 6d 65 64 69 61 74 65 6c 79 20 62 65 66 6f  immediately befo
198b2 72 65 20 74 68 65 20 6f 70 63 6f 64 65 20 74 68  re the opcode th
198b3 61 74 0a 2a 2a 20 6f 70 65 6e 73 20 74 68 65 20  at.** opens the 
198b4 63 75 72 73 6f 72 2e 0a 2a 2f 0a 63 61 73 65 20  cursor..*/.case 
198b5 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
198b6 3a 20 7b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  : {.  break;.}..
198b7 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6c 75 6d  /* Opcode: Colum
198b8 6e 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a  n P1 P2 P3 P4 *.
198b9 2a 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74 20  **.** Interpret 
198ba 74 68 65 20 64 61 74 61 20 74 68 61 74 20 63 75  the data that cu
198bb 72 73 6f 72 20 50 31 20 70 6f 69 6e 74 73 20 74  rsor P1 points t
198bc 6f 20 61 73 20 61 20 73 74 72 75 63 74 75 72 65  o as a structure
198bd 20 62 75 69 6c 74 20 75 73 69 6e 67 0a 2a 2a 20   built using.** 
198be 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64 20 69  the MakeRecord i
198bf 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 28 53 65  nstruction.  (Se
198c0 65 20 74 68 65 20 4d 61 6b 65 52 65 63 6f 72 64  e the MakeRecord
198c1 20 6f 70 63 6f 64 65 20 66 6f 72 20 61 64 64 69   opcode for addi
198c2 74 69 6f 6e 61 6c 0a 2a 2a 20 69 6e 66 6f 72 6d  tional.** inform
198c3 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65 20  ation about the 
198c4 66 6f 72 6d 61 74 20 6f 66 20 74 68 65 20 64 61  format of the da
198c5 74 61 2e 29 20 20 45 78 74 72 61 63 74 20 74 68  ta.)  Extract th
198c6 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d 6e 0a 2a  e P2-th column.*
198c7 2a 20 66 72 6f 6d 20 74 68 69 73 20 72 65 63 6f  * from this reco
198c8 72 64 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  rd.  If there ar
198c9 65 20 6c 65 73 73 20 74 68 61 74 20 28 50 32 2b  e less that (P2+
198ca 31 29 20 0a 2a 2a 20 76 61 6c 75 65 73 20 69 6e  1) .** values in
198cb 20 74 68 65 20 72 65 63 6f 72 64 2c 20 65 78 74   the record, ext
198cc 72 61 63 74 20 61 20 4e 55 4c 4c 2e 0a 2a 2a 0a  ract a NULL..**.
198cd 2a 2a 20 54 68 65 20 76 61 6c 75 65 20 65 78 74  ** The value ext
198ce 72 61 63 74 65 64 20 69 73 20 73 74 6f 72 65 64  racted is stored
198cf 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2e   in register P3.
198d0 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 6f  .**.** If the co
198d1 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 73 20 66 65  lumn contains fe
198d2 77 65 72 20 74 68 61 6e 20 50 32 20 66 69 65 6c  wer than P2 fiel
198d3 64 73 2c 20 74 68 65 6e 20 65 78 74 72 61 63 74  ds, then extract
198d4 20 61 20 4e 55 4c 4c 2e 20 20 4f 72 2c 0a 2a 2a   a NULL.  Or,.**
198d5 20 69 66 20 74 68 65 20 50 34 20 61 72 67 75 6d   if the P4 argum
198d6 65 6e 74 20 69 73 20 61 20 50 34 5f 4d 45 4d 20  ent is a P4_MEM 
198d7 75 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  use the value of
198d8 20 74 68 65 20 50 34 20 61 72 67 75 6d 65 6e 74   the P4 argument
198d9 20 61 73 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c   as.** the resul
198da 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f  t..*/.case OP_Co
198db 6c 75 6d 6e 3a 20 7b 0a 20 20 69 6e 74 20 70 61  lumn: {.  int pa
198dc 79 6c 6f 61 64 53 69 7a 65 3b 20 20 20 2f 2a 20  yloadSize;   /* 
198dd 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  Number of bytes 
198de 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 2a 2f  in the record */
198df 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70 2d  .  int p1 = pOp-
198e0 3e 70 31 3b 20 20 2f 2a 20 50 31 20 76 61 6c 75  >p1;  /* P1 valu
198e1 65 20 6f 66 20 74 68 65 20 6f 70 63 6f 64 65 20  e of the opcode 
198e2 2a 2f 0a 20 20 69 6e 74 20 70 32 20 3d 20 70 4f  */.  int p2 = pO
198e3 70 2d 3e 70 32 3b 20 20 2f 2a 20 63 6f 6c 75 6d  p->p2;  /* colum
198e4 6e 20 6e 75 6d 62 65 72 20 74 6f 20 72 65 74 72  n number to retr
198e5 69 65 76 65 20 2a 2f 0a 20 20 56 64 62 65 43 75  ieve */.  VdbeCu
198e6 72 73 6f 72 20 2a 70 43 20 3d 20 30 3b 2f 2a 20  rsor *pC = 0;/* 
198e7 54 68 65 20 56 44 42 45 20 63 75 72 73 6f 72 20  The VDBE cursor 
198e8 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 52 65 63 3b  */.  char *zRec;
198e9 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
198ea 65 72 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 72  er to complete r
198eb 65 63 6f 72 64 2d 64 61 74 61 20 2a 2f 0a 20 20  ecord-data */.  
198ec 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b  BtCursor *pCrsr;
198ed 20 20 20 2f 2a 20 54 68 65 20 42 54 72 65 65 20     /* The BTree 
198ee 63 75 72 73 6f 72 20 2a 2f 0a 20 20 75 33 32 20  cursor */.  u32 
198ef 2a 61 54 79 70 65 3b 20 20 20 20 20 20 20 20 2f  *aType;        /
198f0 2a 20 61 54 79 70 65 5b 69 5d 20 68 6f 6c 64 73  * aType[i] holds
198f1 20 74 68 65 20 6e 75 6d 65 72 69 63 20 74 79 70   the numeric typ
198f2 65 20 6f 66 20 74 68 65 20 69 2d 74 68 20 63 6f  e of the i-th co
198f3 6c 75 6d 6e 20 2a 2f 0a 20 20 75 33 32 20 2a 61  lumn */.  u32 *a
198f4 4f 66 66 73 65 74 3b 20 20 20 20 20 20 2f 2a 20  Offset;      /* 
198f5 61 4f 66 66 73 65 74 5b 69 5d 20 69 73 20 6f 66  aOffset[i] is of
198f6 66 73 65 74 20 74 6f 20 73 74 61 72 74 20 6f 66  fset to start of
198f7 20 64 61 74 61 20 66 6f 72 20 69 2d 74 68 20 63   data for i-th c
198f8 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e  olumn */.  int n
198f9 46 69 65 6c 64 3b 20 20 20 20 20 20 20 20 2f 2a  Field;        /*
198fa 20 6e 75 6d 62 65 72 20 6f 66 20 66 69 65 6c 64   number of field
198fb 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20  s in the record 
198fc 2a 2f 0a 20 20 69 6e 74 20 6c 65 6e 3b 20 20 20  */.  int len;   
198fd 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6c          /* The l
198fe 65 6e 67 74 68 20 6f 66 20 74 68 65 20 73 65 72  ength of the ser
198ff 69 61 6c 69 7a 65 64 20 64 61 74 61 20 66 6f 72  ialized data for
19900 20 74 68 65 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20   the column */. 
19901 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
19902 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
19903 74 65 72 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ter */.  char *z
19904 44 61 74 61 3b 20 20 20 20 20 20 20 2f 2a 20 50  Data;       /* P
19905 61 72 74 20 6f 66 20 74 68 65 20 72 65 63 6f 72  art of the recor
19906 64 20 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20  d being decoded 
19907 2a 2f 0a 20 20 4d 65 6d 20 2a 70 44 65 73 74 3b  */.  Mem *pDest;
19908 20 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65          /* Where
19909 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65 78   to write the ex
1990a 74 72 61 63 74 65 64 20 76 61 6c 75 65 20 2a 2f  tracted value */
1990b 0a 20 20 4d 65 6d 20 73 4d 65 6d 3b 20 20 20 20  .  Mem sMem;    
1990c 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f        /* For sto
1990d 72 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 20  ring the record 
1990e 62 65 69 6e 67 20 64 65 63 6f 64 65 64 20 2a 2f  being decoded */
1990f 0a 0a 20 20 73 4d 65 6d 2e 66 6c 61 67 73 20 3d  ..  sMem.flags =
19910 20 30 3b 0a 20 20 73 4d 65 6d 2e 64 62 20 3d 20   0;.  sMem.db = 
19911 30 3b 0a 20 20 73 4d 65 6d 2e 7a 4d 61 6c 6c 6f  0;.  sMem.zMallo
19912 63 20 3d 20 30 3b 0a 20 20 61 73 73 65 72 74 28  c = 0;.  assert(
19913 20 70 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29   p1<p->nCursor )
19914 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
19915 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33  >p3>0 && pOp->p3
19916 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70  <=p->nMem );.  p
19917 44 65 73 74 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b  Dest = &p->aMem[
19918 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 4d 65 6d 53  pOp->p3];.  MemS
19919 65 74 54 79 70 65 46 6c 61 67 28 70 44 65 73 74  etTypeFlag(pDest
1991a 2c 20 4d 45 4d 5f 4e 75 6c 6c 29 3b 0a 0a 20 20  , MEM_Null);..  
1991b 2f 2a 20 54 68 69 73 20 62 6c 6f 63 6b 20 73 65  /* This block se
1991c 74 73 20 74 68 65 20 76 61 72 69 61 62 6c 65 20  ts the variable 
1991d 70 61 79 6c 6f 61 64 53 69 7a 65 20 74 6f 20 62  payloadSize to b
1991e 65 20 74 68 65 20 74 6f 74 61 6c 20 6e 75 6d 62  e the total numb
1991f 65 72 20 6f 66 0a 20 20 2a 2a 20 62 79 74 65 73  er of.  ** bytes
19920 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64 2e 0a   in the record..
19921 20 20 2a 2a 0a 20 20 2a 2a 20 7a 52 65 63 20 69    **.  ** zRec i
19922 73 20 73 65 74 20 74 6f 20 62 65 20 74 68 65 20  s set to be the 
19923 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66  complete text of
19924 20 74 68 65 20 72 65 63 6f 72 64 20 69 66 20 69   the record if i
19925 74 20 69 73 20 61 76 61 69 6c 61 62 6c 65 2e 0a  t is available..
19926 20 20 2a 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    ** The complet
19927 65 20 72 65 63 6f 72 64 20 74 65 78 74 20 69 73  e record text is
19928 20 61 6c 77 61 79 73 20 61 76 61 69 6c 61 62 6c   always availabl
19929 65 20 66 6f 72 20 70 73 65 75 64 6f 2d 74 61 62  e for pseudo-tab
1992a 6c 65 73 0a 20 20 2a 2a 20 49 66 20 74 68 65 20  les.  ** If the 
1992b 72 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64  record is stored
1992c 20 69 6e 20 61 20 63 75 72 73 6f 72 2c 20 74 68   in a cursor, th
1992d 65 20 63 6f 6d 70 6c 65 74 65 20 72 65 63 6f 72  e complete recor
1992e 64 20 74 65 78 74 0a 20 20 2a 2a 20 6d 69 67 68  d text.  ** migh
1992f 74 20 62 65 20 61 76 61 69 6c 61 62 6c 65 20 69  t be available i
19930 6e 20 74 68 65 20 20 70 43 2d 3e 61 52 6f 77 20  n the  pC->aRow 
19931 63 61 63 68 65 2e 20 20 4f 72 20 69 74 20 6d 69  cache.  Or it mi
19932 67 68 74 20 6e 6f 74 20 62 65 2e 0a 20 20 2a 2a  ght not be..  **
19933 20 49 66 20 74 68 65 20 64 61 74 61 20 69 73 20   If the data is 
19934 75 6e 61 76 61 69 6c 61 62 6c 65 2c 20 20 7a 52  unavailable,  zR
19935 65 63 20 69 73 20 73 65 74 20 74 6f 20 4e 55 4c  ec is set to NUL
19936 4c 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20  L..  **.  ** We 
19937 61 6c 73 6f 20 63 6f 6d 70 75 74 65 20 74 68 65  also compute the
19938 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
19939 6e 73 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ns in the record
1993a 2e 20 20 46 6f 72 20 63 75 72 73 6f 72 73 2c 0a  .  For cursors,.
1993b 20 20 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20    ** the number 
1993c 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 73 20 73 74  of columns is st
1993d 6f 72 65 64 20 69 6e 20 74 68 65 20 56 64 62 65  ored in the Vdbe
1993e 43 75 72 73 6f 72 2e 6e 46 69 65 6c 64 20 65 6c  Cursor.nField el
1993f 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 70 43  ement..  */.  pC
19940 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 31 5d 3b   = p->apCsr[p1];
19941 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30  .  assert( pC!=0
19942 20 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   );.#ifndef SQLI
19943 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
19944 41 42 4c 45 0a 20 20 61 73 73 65 72 74 28 20 70  ABLE.  assert( p
19945 43 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 3d 3d  C->pVtabCursor==
19946 30 20 29 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66  0 );.#endif.  if
19947 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30  ( pC->pCursor!=0
19948 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 72   ){.    /* The r
19949 65 63 6f 72 64 20 69 73 20 73 74 6f 72 65 64 20  ecord is stored 
1994a 69 6e 20 61 20 42 2d 54 72 65 65 20 2a 2f 0a 20  in a B-Tree */. 
1994b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56     rc = sqlite3V
1994c 64 62 65 43 75 72 73 6f 72 4d 6f 76 65 74 6f 28  dbeCursorMoveto(
1994d 70 43 29 3b 0a 20 20 20 20 69 66 28 20 72 63 20  pC);.    if( rc 
1994e 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1994f 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7a  _to_error;.    z
19950 52 65 63 20 3d 20 30 3b 0a 20 20 20 20 70 43 72  Rec = 0;.    pCr
19951 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73 6f 72  sr = pC->pCursor
19952 3b 0a 20 20 20 20 69 66 28 20 70 43 2d 3e 6e 75  ;.    if( pC->nu
19953 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 70  llRow ){.      p
19954 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 30 3b 0a  ayloadSize = 0;.
19955 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 43      }else if( pC
19956 2d 3e 63 61 63 68 65 53 74 61 74 75 73 3d 3d 70  ->cacheStatus==p
19957 2d 3e 63 61 63 68 65 43 74 72 20 29 7b 0a 20 20  ->cacheCtr ){.  
19958 20 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20      payloadSize 
19959 3d 20 70 43 2d 3e 70 61 79 6c 6f 61 64 53 69 7a  = pC->payloadSiz
1995a 65 3b 0a 20 20 20 20 20 20 7a 52 65 63 20 3d 20  e;.      zRec = 
1995b 28 63 68 61 72 2a 29 70 43 2d 3e 61 52 6f 77 3b  (char*)pC->aRow;
1995c 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
1995d 43 2d 3e 69 73 49 6e 64 65 78 20 29 7b 0a 20 20  C->isIndex ){.  
1995e 20 20 20 20 69 36 34 20 70 61 79 6c 6f 61 64 53      i64 payloadS
1995f 69 7a 65 36 34 3b 0a 20 20 20 20 20 20 73 71 6c  ize64;.      sql
19960 69 74 65 33 42 74 72 65 65 4b 65 79 53 69 7a 65  ite3BtreeKeySize
19961 28 70 43 72 73 72 2c 20 26 70 61 79 6c 6f 61 64  (pCrsr, &payload
19962 53 69 7a 65 36 34 29 3b 0a 20 20 20 20 20 20 70  Size64);.      p
19963 61 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 79  ayloadSize = pay
19964 6c 6f 61 64 53 69 7a 65 36 34 3b 0a 20 20 20 20  loadSize64;.    
19965 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
19966 69 74 65 33 42 74 72 65 65 44 61 74 61 53 69 7a  ite3BtreeDataSiz
19967 65 28 70 43 72 73 72 2c 20 28 75 33 32 20 2a 29  e(pCrsr, (u32 *)
19968 26 70 61 79 6c 6f 61 64 53 69 7a 65 29 3b 0a 20  &payloadSize);. 
19969 20 20 20 7d 0a 20 20 20 20 6e 46 69 65 6c 64 20     }.    nField 
1996a 3d 20 70 43 2d 3e 6e 46 69 65 6c 64 3b 0a 20 20  = pC->nField;.  
1996b 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
1996c 74 28 20 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  t( pC->pseudoTab
1996d 6c 65 20 29 3b 0a 20 20 20 20 2f 2a 20 54 68 65  le );.    /* The
1996e 20 72 65 63 6f 72 64 20 69 73 20 74 68 65 20 73   record is the s
1996f 6f 6c 65 20 65 6e 74 72 79 20 6f 66 20 61 20 70  ole entry of a p
19970 73 65 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20  seudo-table */. 
19971 20 20 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 3d     payloadSize =
19972 20 70 43 2d 3e 6e 44 61 74 61 3b 0a 20 20 20 20   pC->nData;.    
19973 7a 52 65 63 20 3d 20 70 43 2d 3e 70 44 61 74 61  zRec = pC->pData
19974 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53  ;.    pC->cacheS
19975 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
19976 41 4c 45 3b 0a 20 20 20 20 61 73 73 65 72 74 28  ALE;.    assert(
19977 20 70 61 79 6c 6f 61 64 53 69 7a 65 3d 3d 30 20   payloadSize==0 
19978 7c 7c 20 7a 52 65 63 21 3d 30 20 29 3b 0a 20 20  || zRec!=0 );.  
19979 20 20 6e 46 69 65 6c 64 20 3d 20 70 43 2d 3e 6e    nField = pC->n
1997a 46 69 65 6c 64 3b 0a 20 20 20 20 70 43 72 73 72  Field;.    pCrsr
1997b 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   = 0;.  }..  /* 
1997c 49 66 20 70 61 79 6c 6f 61 64 53 69 7a 65 20 69  If payloadSize i
1997d 73 20 30 2c 20 74 68 65 6e 20 6a 75 73 74 20 73  s 0, then just s
1997e 74 6f 72 65 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20  tore a NULL */. 
1997f 20 69 66 28 20 70 61 79 6c 6f 61 64 53 69 7a 65   if( payloadSize
19980 3d 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72  ==0 ){.    asser
19981 74 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 26  t( pDest->flags&
19982 4d 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20  MEM_Null );.    
19983 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  goto op_column_o
19984 75 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 61  ut;.  }.  if( pa
19985 79 6c 6f 61 64 53 69 7a 65 3e 64 62 2d 3e 61 4c  yloadSize>db->aL
19986 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
19987 54 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20  T_LENGTH] ){.   
19988 20 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20   goto too_big;. 
19989 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 32   }..  assert( p2
1998a 3c 6e 46 69 65 6c 64 20 29 3b 0a 0a 20 20 2f 2a  <nField );..  /*
1998b 20 52 65 61 64 20 61 6e 64 20 70 61 72 73 65 20   Read and parse 
1998c 74 68 65 20 74 61 62 6c 65 20 68 65 61 64 65 72  the table header
1998d 2e 20 20 53 74 6f 72 65 20 74 68 65 20 72 65 73  .  Store the res
1998e 75 6c 74 73 20 6f 66 20 74 68 65 20 70 61 72 73  ults of the pars
1998f 65 0a 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20  e.  ** into the 
19990 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 63 61  record header ca
19991 63 68 65 20 66 69 65 6c 64 73 20 6f 66 20 74 68  che fields of th
19992 65 20 63 75 72 73 6f 72 2e 0a 20 20 2a 2f 0a 20  e cursor..  */. 
19993 20 61 54 79 70 65 20 3d 20 70 43 2d 3e 61 54 79   aType = pC->aTy
19994 70 65 3b 0a 20 20 69 66 28 20 70 43 2d 3e 63 61  pe;.  if( pC->ca
19995 63 68 65 53 74 61 74 75 73 3d 3d 70 2d 3e 63 61  cheStatus==p->ca
19996 63 68 65 43 74 72 20 29 7b 0a 20 20 20 20 61 4f  cheCtr ){.    aO
19997 66 66 73 65 74 20 3d 20 70 43 2d 3e 61 4f 66 66  ffset = pC->aOff
19998 73 65 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  set;.  }else{.  
19999 20 20 75 38 20 2a 7a 49 64 78 3b 20 20 20 20 20    u8 *zIdx;     
1999a 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e 74 6f     /* Index into
1999b 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20 20 75   header */.    u
1999c 38 20 2a 7a 45 6e 64 48 64 72 3b 20 20 20 20 20  8 *zEndHdr;     
1999d 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69  /* Pointer to fi
1999e 72 73 74 20 62 79 74 65 20 61 66 74 65 72 20 74  rst byte after t
1999f 68 65 20 68 65 61 64 65 72 20 2a 2f 0a 20 20 20  he header */.   
199a0 20 69 6e 74 20 6f 66 66 73 65 74 3b 20 20 20 20   int offset;    
199a1 20 20 2f 2a 20 4f 66 66 73 65 74 20 69 6e 74 6f    /* Offset into
199a2 20 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 20   the data */.   
199a3 20 69 6e 74 20 73 7a 48 64 72 53 7a 3b 20 20 20   int szHdrSz;   
199a4 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 74 68 65    /* Size of the
199a5 20 68 65 61 64 65 72 20 73 69 7a 65 20 66 69 65   header size fie
199a6 6c 64 20 61 74 20 73 74 61 72 74 20 6f 66 20 72  ld at start of r
199a7 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74  ecord */.    int
199a8 20 61 76 61 69 6c 3b 20 20 20 20 20 20 20 2f 2a   avail;       /*
199a9 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73   Number of bytes
199aa 20 6f 66 20 61 76 61 69 6c 61 62 6c 65 20 64 61   of available da
199ab 74 61 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65 72  ta */..    asser
199ac 74 28 61 54 79 70 65 29 3b 0a 20 20 20 20 70 43  t(aType);.    pC
199ad 2d 3e 61 4f 66 66 73 65 74 20 3d 20 61 4f 66 66  ->aOffset = aOff
199ae 73 65 74 20 3d 20 26 61 54 79 70 65 5b 6e 46 69  set = &aType[nFi
199af 65 6c 64 5d 3b 0a 20 20 20 20 70 43 2d 3e 70 61  eld];.    pC->pa
199b0 79 6c 6f 61 64 53 69 7a 65 20 3d 20 70 61 79 6c  yloadSize = payl
199b1 6f 61 64 53 69 7a 65 3b 0a 20 20 20 20 70 43 2d  oadSize;.    pC-
199b2 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 70  >cacheStatus = p
199b3 2d 3e 63 61 63 68 65 43 74 72 3b 0a 0a 20 20 20  ->cacheCtr;..   
199b4 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68   /* Figure out h
199b5 6f 77 20 6d 61 6e 79 20 62 79 74 65 73 20 61 72  ow many bytes ar
199b6 65 20 69 6e 20 74 68 65 20 68 65 61 64 65 72 20  e in the header 
199b7 2a 2f 0a 20 20 20 20 69 66 28 20 7a 52 65 63 20  */.    if( zRec 
199b8 29 7b 0a 20 20 20 20 20 20 7a 44 61 74 61 20 3d  ){.      zData =
199b9 20 7a 52 65 63 3b 0a 20 20 20 20 7d 65 6c 73 65   zRec;.    }else
199ba 7b 0a 20 20 20 20 20 20 69 66 28 20 70 43 2d 3e  {.      if( pC->
199bb 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20  isIndex ){.     
199bc 20 20 20 7a 44 61 74 61 20 3d 20 28 63 68 61 72     zData = (char
199bd 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65  *)sqlite3BtreeKe
199be 79 46 65 74 63 68 28 70 43 72 73 72 2c 20 26 61  yFetch(pCrsr, &a
199bf 76 61 69 6c 29 3b 0a 20 20 20 20 20 20 7d 65 6c  vail);.      }el
199c0 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 44 61 74  se{.        zDat
199c1 61 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74  a = (char*)sqlit
199c2 65 33 42 74 72 65 65 44 61 74 61 46 65 74 63 68  e3BtreeDataFetch
199c3 28 70 43 72 73 72 2c 20 26 61 76 61 69 6c 29 3b  (pCrsr, &avail);
199c4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 2f  .      }.      /
199c5 2a 20 49 66 20 4b 65 79 46 65 74 63 68 28 29 2f  * If KeyFetch()/
199c6 44 61 74 61 46 65 74 63 68 28 29 20 6d 61 6e 61  DataFetch() mana
199c7 67 65 64 20 74 6f 20 67 65 74 20 74 68 65 20 65  ged to get the e
199c8 6e 74 69 72 65 20 70 61 79 6c 6f 61 64 2c 0a 20  ntire payload,. 
199c9 20 20 20 20 20 2a 2a 20 73 61 76 65 20 74 68 65       ** save the
199ca 20 70 61 79 6c 6f 61 64 20 69 6e 20 74 68 65 20   payload in the 
199cb 70 43 2d 3e 61 52 6f 77 20 63 61 63 68 65 2e 20  pC->aRow cache. 
199cc 20 54 68 61 74 20 77 69 6c 6c 20 73 61 76 65 20   That will save 
199cd 75 73 20 66 72 6f 6d 0a 20 20 20 20 20 20 2a 2a  us from.      **
199ce 20 68 61 76 69 6e 67 20 74 6f 20 6d 61 6b 65 20   having to make 
199cf 61 64 64 69 74 69 6f 6e 61 6c 20 63 61 6c 6c 73  additional calls
199d0 20 74 6f 20 66 65 74 63 68 20 74 68 65 20 63 6f   to fetch the co
199d1 6e 74 65 6e 74 20 70 6f 72 74 69 6f 6e 20 6f 66  ntent portion of
199d2 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 72 65  .      ** the re
199d3 63 6f 72 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  cord..      */. 
199d4 20 20 20 20 20 69 66 28 20 61 76 61 69 6c 3e 3d       if( avail>=
199d5 70 61 79 6c 6f 61 64 53 69 7a 65 20 29 7b 0a 20  payloadSize ){. 
199d6 20 20 20 20 20 20 20 7a 52 65 63 20 3d 20 7a 44         zRec = zD
199d7 61 74 61 3b 0a 20 20 20 20 20 20 20 20 70 43 2d  ata;.        pC-
199d8 3e 61 52 6f 77 20 3d 20 28 75 38 2a 29 7a 44 61  >aRow = (u8*)zDa
199d9 74 61 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ta;.      }else{
199da 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 61 52 6f  .        pC->aRo
199db 77 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  w = 0;.      }. 
199dc 20 20 20 7d 0a 20 20 20 20 2f 2a 20 54 68 65 20     }.    /* The 
199dd 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73 65 72 74  following assert
199de 20 69 73 20 74 72 75 65 20 69 6e 20 61 6c 6c 20   is true in all 
199df 63 61 73 65 73 20 61 63 63 65 70 74 20 77 68 65  cases accept whe
199e0 6e 0a 20 20 20 20 2a 2a 20 74 68 65 20 64 61 74  n.    ** the dat
199e1 61 62 61 73 65 20 66 69 6c 65 20 68 61 73 20 62  abase file has b
199e2 65 65 6e 20 63 6f 72 72 75 70 74 65 64 20 65 78  een corrupted ex
199e3 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 20 20 2a 2a  ternally..    **
199e4 20 20 20 20 61 73 73 65 72 74 28 20 7a 52 65 63      assert( zRec
199e5 21 3d 30 20 7c 7c 20 61 76 61 69 6c 3e 3d 70 61  !=0 || avail>=pa
199e6 79 6c 6f 61 64 53 69 7a 65 20 7c 7c 20 61 76 61  yloadSize || ava
199e7 69 6c 3e 3d 39 20 29 3b 20 2a 2f 0a 20 20 20 20  il>=9 ); */.    
199e8 73 7a 48 64 72 53 7a 20 3d 20 67 65 74 56 61 72  szHdrSz = getVar
199e9 69 6e 74 33 32 28 28 75 38 2a 29 7a 44 61 74 61  int32((u8*)zData
199ea 2c 20 6f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20  , offset);..    
199eb 2f 2a 20 54 68 65 20 4b 65 79 46 65 74 63 68 28  /* The KeyFetch(
199ec 29 20 6f 72 20 44 61 74 61 46 65 74 63 68 28 29  ) or DataFetch()
199ed 20 61 62 6f 76 65 20 61 72 65 20 66 61 73 74 20   above are fast 
199ee 61 6e 64 20 77 69 6c 6c 20 67 65 74 20 74 68 65  and will get the
199ef 20 65 6e 74 69 72 65 0a 20 20 20 20 2a 2a 20 72   entire.    ** r
199f0 65 63 6f 72 64 20 68 65 61 64 65 72 20 69 6e 20  ecord header in 
199f1 6d 6f 73 74 20 63 61 73 65 73 2e 20 20 42 75 74  most cases.  But
199f2 20 74 68 65 79 20 77 69 6c 6c 20 66 61 69 6c 20   they will fail 
199f3 74 6f 20 67 65 74 20 74 68 65 20 63 6f 6d 70 6c  to get the compl
199f4 65 74 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72  ete.    ** recor
199f5 64 20 68 65 61 64 65 72 20 69 66 20 74 68 65 20  d header if the 
199f6 72 65 63 6f 72 64 20 68 65 61 64 65 72 20 64 6f  record header do
199f7 65 73 20 6e 6f 74 20 66 69 74 20 6f 6e 20 61 20  es not fit on a 
199f8 73 69 6e 67 6c 65 20 70 61 67 65 0a 20 20 20 20  single page.    
199f9 2a 2a 20 69 6e 20 74 68 65 20 42 2d 54 72 65 65  ** in the B-Tree
199fa 2e 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 70  .  When that hap
199fb 70 65 6e 73 2c 20 75 73 65 20 73 71 6c 69 74 65  pens, use sqlite
199fc 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65  3VdbeMemFromBtre
199fd 65 28 29 20 74 6f 0a 20 20 20 20 2a 2a 20 61 63  e() to.    ** ac
199fe 71 75 69 72 65 20 74 68 65 20 63 6f 6d 70 6c 65  quire the comple
199ff 74 65 20 68 65 61 64 65 72 20 74 65 78 74 2e 0a  te header text..
19a00 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21      */.    if( !
19a01 7a 52 65 63 20 26 26 20 61 76 61 69 6c 3c 6f 66  zRec && avail<of
19a02 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 73 4d  fset ){.      sM
19a03 65 6d 2e 66 6c 61 67 73 20 3d 20 30 3b 0a 20 20  em.flags = 0;.  
19a04 20 20 20 20 73 4d 65 6d 2e 64 62 20 3d 20 30 3b      sMem.db = 0;
19a05 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
19a06 74 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74  te3VdbeMemFromBt
19a07 72 65 65 28 70 43 72 73 72 2c 20 30 2c 20 6f 66  ree(pCrsr, 0, of
19a08 66 73 65 74 2c 20 70 43 2d 3e 69 73 49 6e 64 65  fset, pC->isInde
19a09 78 2c 20 26 73 4d 65 6d 29 3b 0a 20 20 20 20 20  x, &sMem);.     
19a0a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
19a0b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  OK ){.        go
19a0c 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f 75 74  to op_column_out
19a0d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
19a0e 7a 44 61 74 61 20 3d 20 73 4d 65 6d 2e 7a 3b 0a  zData = sMem.z;.
19a0f 20 20 20 20 7d 0a 20 20 20 20 7a 45 6e 64 48 64      }.    zEndHd
19a10 72 20 3d 20 28 75 38 20 2a 29 26 7a 44 61 74 61  r = (u8 *)&zData
19a11 5b 6f 66 66 73 65 74 5d 3b 0a 20 20 20 20 7a 49  [offset];.    zI
19a12 64 78 20 3d 20 28 75 38 20 2a 29 26 7a 44 61 74  dx = (u8 *)&zDat
19a13 61 5b 73 7a 48 64 72 53 7a 5d 3b 0a 0a 20 20 20  a[szHdrSz];..   
19a14 20 2f 2a 20 53 63 61 6e 20 74 68 65 20 68 65 61   /* Scan the hea
19a15 64 65 72 20 61 6e 64 20 75 73 65 20 69 74 20 74  der and use it t
19a16 6f 20 66 69 6c 6c 20 69 6e 20 74 68 65 20 61 54  o fill in the aT
19a17 79 70 65 5b 5d 20 61 6e 64 20 61 4f 66 66 73 65  ype[] and aOffse
19a18 74 5b 5d 0a 20 20 20 20 2a 2a 20 61 72 72 61 79  t[].    ** array
19a19 73 2e 20 20 61 54 79 70 65 5b 69 5d 20 77 69 6c  s.  aType[i] wil
19a1a 6c 20 63 6f 6e 74 61 69 6e 20 74 68 65 20 74 79  l contain the ty
19a1b 70 65 20 69 6e 74 65 67 65 72 20 66 6f 72 20 74  pe integer for t
19a1c 68 65 20 69 2d 74 68 0a 20 20 20 20 2a 2a 20 63  he i-th.    ** c
19a1d 6f 6c 75 6d 6e 20 61 6e 64 20 61 4f 66 66 73 65  olumn and aOffse
19a1e 74 5b 69 5d 20 77 69 6c 6c 20 63 6f 6e 74 61 69  t[i] will contai
19a1f 6e 20 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f  n the offset fro
19a20 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a  m the beginning.
19a21 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
19a22 63 6f 72 64 20 74 6f 20 74 68 65 20 73 74 61 72  cord to the star
19a23 74 20 6f 66 20 74 68 65 20 64 61 74 61 20 66 6f  t of the data fo
19a24 72 20 74 68 65 20 69 2d 74 68 20 63 6f 6c 75 6d  r the i-th colum
19a25 6e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72  n.    */.    for
19a26 28 69 3d 30 3b 20 69 3c 6e 46 69 65 6c 64 3b 20  (i=0; i<nField; 
19a27 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
19a28 7a 49 64 78 3c 7a 45 6e 64 48 64 72 20 29 7b 0a  zIdx<zEndHdr ){.
19a29 20 20 20 20 20 20 20 20 61 4f 66 66 73 65 74 5b          aOffset[
19a2a 69 5d 20 3d 20 6f 66 66 73 65 74 3b 0a 20 20 20  i] = offset;.   
19a2b 20 20 20 20 20 7a 49 64 78 20 2b 3d 20 67 65 74       zIdx += get
19a2c 56 61 72 69 6e 74 33 32 28 7a 49 64 78 2c 20 61  Varint32(zIdx, a
19a2d 54 79 70 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20  Type[i]);.      
19a2e 20 20 6f 66 66 73 65 74 20 2b 3d 20 73 71 6c 69    offset += sqli
19a2f 74 65 33 56 64 62 65 53 65 72 69 61 6c 54 79 70  te3VdbeSerialTyp
19a30 65 4c 65 6e 28 61 54 79 70 65 5b 69 5d 29 3b 0a  eLen(aType[i]);.
19a31 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
19a32 20 20 20 20 20 2f 2a 20 49 66 20 69 20 69 73 20       /* If i is 
19a33 6c 65 73 73 20 74 68 61 74 20 6e 46 69 65 6c 64  less that nField
19a34 2c 20 74 68 65 6e 20 74 68 65 72 65 20 61 72 65  , then there are
19a35 20 6c 65 73 73 20 66 69 65 6c 64 73 20 69 6e 20   less fields in 
19a36 74 68 69 73 0a 20 20 20 20 20 20 20 20 2a 2a 20  this.        ** 
19a37 72 65 63 6f 72 64 20 74 68 61 6e 20 53 65 74 4e  record than SetN
19a38 75 6d 43 6f 6c 75 6d 6e 73 20 69 6e 64 69 63 61  umColumns indica
19a39 74 65 64 20 74 68 65 72 65 20 61 72 65 20 63 6f  ted there are co
19a3a 6c 75 6d 6e 73 20 69 6e 20 74 68 65 0a 20 20 20  lumns in the.   
19a3b 20 20 20 20 20 2a 2a 20 74 61 62 6c 65 2e 20 53       ** table. S
19a3c 65 74 20 74 68 65 20 6f 66 66 73 65 74 20 66 6f  et the offset fo
19a3d 72 20 61 6e 79 20 65 78 74 72 61 20 63 6f 6c 75  r any extra colu
19a3e 6d 6e 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 20  mns not present 
19a3f 69 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  in.        ** th
19a40 65 20 72 65 63 6f 72 64 20 74 6f 20 30 2e 20 54  e record to 0. T
19a41 68 69 73 20 74 65 6c 6c 73 20 63 6f 64 65 20 62  his tells code b
19a42 65 6c 6f 77 20 74 6f 20 73 74 6f 72 65 20 61 20  elow to store a 
19a43 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 2a 2a 20  NULL.        ** 
19a44 69 6e 73 74 65 61 64 20 6f 66 20 64 65 73 65 72  instead of deser
19a45 69 61 6c 69 7a 69 6e 67 20 61 20 76 61 6c 75 65  ializing a value
19a46 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64   from the record
19a47 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
19a48 20 20 20 20 20 61 4f 66 66 73 65 74 5b 69 5d 20       aOffset[i] 
19a49 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 0;.      }.   
19a4a 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64   }.    sqlite3Vd
19a4b 62 65 4d 65 6d 52 65 6c 65 61 73 65 28 26 73 4d  beMemRelease(&sM
19a4c 65 6d 29 3b 0a 20 20 20 20 73 4d 65 6d 2e 66 6c  em);.    sMem.fl
19a4d 61 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a  ags = MEM_Null;.
19a4e 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 68 61  .    /* If we ha
19a4f 76 65 20 72 65 61 64 20 6d 6f 72 65 20 68 65 61  ve read more hea
19a50 64 65 72 20 64 61 74 61 20 74 68 61 6e 20 77 61  der data than wa
19a51 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  s contained in t
19a52 68 65 20 68 65 61 64 65 72 2c 0a 20 20 20 20 2a  he header,.    *
19a53 2a 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64 20  * or if the end 
19a54 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65 6c  of the last fiel
19a55 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 65 20  d appears to be 
19a56 70 61 73 74 20 74 68 65 20 65 6e 64 20 6f 66 20  past the end of 
19a57 74 68 65 0a 20 20 20 20 2a 2a 20 72 65 63 6f 72  the.    ** recor
19a58 64 2c 20 6f 72 20 69 66 20 74 68 65 20 65 6e 64  d, or if the end
19a59 20 6f 66 20 74 68 65 20 6c 61 73 74 20 66 69 65   of the last fie
19a5a 6c 64 20 61 70 70 65 61 72 73 20 74 6f 20 62 65  ld appears to be
19a5b 20 62 65 66 6f 72 65 20 74 68 65 20 65 6e 64 0a   before the end.
19a5c 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 72 65      ** of the re
19a5d 63 6f 72 64 20 28 77 68 65 6e 20 61 6c 6c 20 66  cord (when all f
19a5e 69 65 6c 64 73 20 70 72 65 73 65 6e 74 29 2c 20  ields present), 
19a5f 74 68 65 6e 20 77 65 20 6d 75 73 74 20 62 65 20  then we must be 
19a60 64 65 61 6c 69 6e 67 20 0a 20 20 20 20 2a 2a 20  dealing .    ** 
19a61 77 69 74 68 20 61 20 63 6f 72 72 75 70 74 20 64  with a corrupt d
19a62 61 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a  atabase..    */.
19a63 20 20 20 20 69 66 28 20 7a 49 64 78 3e 7a 45 6e      if( zIdx>zEn
19a64 64 48 64 72 20 7c 7c 20 6f 66 66 73 65 74 3e 70  dHdr || offset>p
19a65 61 79 6c 6f 61 64 53 69 7a 65 20 0a 20 20 20 20  ayloadSize .    
19a66 20 7c 7c 20 28 7a 49 64 78 3d 3d 7a 45 6e 64 48   || (zIdx==zEndH
19a67 64 72 20 26 26 20 6f 66 66 73 65 74 21 3d 70 61  dr && offset!=pa
19a68 79 6c 6f 61 64 53 69 7a 65 29 20 29 7b 0a 20 20  yloadSize) ){.  
19a69 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
19a6a 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20  CORRUPT_BKPT;.  
19a6b 20 20 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75      goto op_colu
19a6c 6d 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20  mn_out;.    }.  
19a6d 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20  }..  /* Get the 
19a6e 63 6f 6c 75 6d 6e 20 69 6e 66 6f 72 6d 61 74 69  column informati
19a6f 6f 6e 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70  on. If aOffset[p
19a70 32 5d 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20  2] is non-zero, 
19a71 74 68 65 6e 20 0a 20 20 2a 2a 20 64 65 73 65 72  then .  ** deser
19a72 69 61 6c 69 7a 65 20 74 68 65 20 76 61 6c 75 65  ialize the value
19a73 20 66 72 6f 6d 20 74 68 65 20 72 65 63 6f 72 64   from the record
19a74 2e 20 49 66 20 61 4f 66 66 73 65 74 5b 70 32 5d  . If aOffset[p2]
19a75 20 69 73 20 7a 65 72 6f 2c 0a 20 20 2a 2a 20 74   is zero,.  ** t
19a76 68 65 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f  hen there are no
19a77 74 20 65 6e 6f 75 67 68 20 66 69 65 6c 64 73 20  t enough fields 
19a78 69 6e 20 74 68 65 20 72 65 63 6f 72 64 20 74 6f  in the record to
19a79 20 73 61 74 69 73 66 79 20 74 68 65 0a 20 20 2a   satisfy the.  *
19a7a 2a 20 72 65 71 75 65 73 74 2e 20 20 49 6e 20 74  * request.  In t
19a7b 68 69 73 20 63 61 73 65 2c 20 73 65 74 20 74 68  his case, set th
19a7c 65 20 76 61 6c 75 65 20 4e 55 4c 4c 20 6f 72 20  e value NULL or 
19a7d 74 6f 20 50 34 20 69 66 20 50 34 20 69 73 0a 20  to P4 if P4 is. 
19a7e 20 2a 2a 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   ** a pointer to
19a7f 20 61 20 4d 65 6d 20 6f 62 6a 65 63 74 2e 0a 20   a Mem object.. 
19a80 20 2a 2f 0a 20 20 69 66 28 20 61 4f 66 66 73 65   */.  if( aOffse
19a81 74 5b 70 32 5d 20 29 7b 0a 20 20 20 20 61 73 73  t[p2] ){.    ass
19a82 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
19a83 4f 4b 20 29 3b 0a 20 20 20 20 69 66 28 20 7a 52  OK );.    if( zR
19a84 65 63 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ec ){.      sqli
19a85 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61 73  te3VdbeMemReleas
19a86 65 45 78 74 65 72 6e 61 6c 28 70 44 65 73 74 29  eExternal(pDest)
19a87 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
19a88 64 62 65 53 65 72 69 61 6c 47 65 74 28 28 75 38  dbeSerialGet((u8
19a89 20 2a 29 26 7a 52 65 63 5b 61 4f 66 66 73 65 74   *)&zRec[aOffset
19a8a 5b 70 32 5d 5d 2c 20 61 54 79 70 65 5b 70 32 5d  [p2]], aType[p2]
19a8b 2c 20 70 44 65 73 74 29 3b 0a 20 20 20 20 7d 65  , pDest);.    }e
19a8c 6c 73 65 7b 0a 20 20 20 20 20 20 6c 65 6e 20 3d  lse{.      len =
19a8d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 72 69   sqlite3VdbeSeri
19a8e 61 6c 54 79 70 65 4c 65 6e 28 61 54 79 70 65 5b  alTypeLen(aType[
19a8f 70 32 5d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  p2]);.      sqli
19a90 74 65 33 56 64 62 65 4d 65 6d 4d 6f 76 65 28 26  te3VdbeMemMove(&
19a91 73 4d 65 6d 2c 20 70 44 65 73 74 29 3b 0a 20 20  sMem, pDest);.  
19a92 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
19a93 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72 65 65  VdbeMemFromBtree
19a94 28 70 43 72 73 72 2c 20 61 4f 66 66 73 65 74 5b  (pCrsr, aOffset[
19a95 70 32 5d 2c 20 6c 65 6e 2c 20 70 43 2d 3e 69 73  p2], len, pC->is
19a96 49 6e 64 65 78 2c 20 26 73 4d 65 6d 29 3b 0a 20  Index, &sMem);. 
19a97 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
19a98 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
19a99 20 20 67 6f 74 6f 20 6f 70 5f 63 6f 6c 75 6d 6e    goto op_column
19a9a 5f 6f 75 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20  _out;.      }.  
19a9b 20 20 20 20 7a 44 61 74 61 20 3d 20 73 4d 65 6d      zData = sMem
19a9c 2e 7a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  .z;.      sqlite
19a9d 33 56 64 62 65 53 65 72 69 61 6c 47 65 74 28 28  3VdbeSerialGet((
19a9e 75 38 2a 29 7a 44 61 74 61 2c 20 61 54 79 70 65  u8*)zData, aType
19a9f 5b 70 32 5d 2c 20 70 44 65 73 74 29 3b 0a 20 20  [p2], pDest);.  
19aa0 20 20 7d 0a 20 20 20 20 70 44 65 73 74 2d 3e 65    }.    pDest->e
19aa1 6e 63 20 3d 20 65 6e 63 6f 64 69 6e 67 3b 0a 20  nc = encoding;. 
19aa2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20   }else{.    if( 
19aa3 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50 34 5f  pOp->p4type==P4_
19aa4 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  MEM ){.      sql
19aa5 69 74 65 33 56 64 62 65 4d 65 6d 53 68 61 6c 6c  ite3VdbeMemShall
19aa6 6f 77 43 6f 70 79 28 70 44 65 73 74 2c 20 70 4f  owCopy(pDest, pO
19aa7 70 2d 3e 70 34 2e 70 4d 65 6d 2c 20 4d 45 4d 5f  p->p4.pMem, MEM_
19aa8 53 74 61 74 69 63 29 3b 0a 20 20 20 20 7d 65 6c  Static);.    }el
19aa9 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
19aaa 28 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 26 4d  ( pDest->flags&M
19aab 45 4d 5f 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 7d  EM_Null );.    }
19aac 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65  .  }..  /* If we
19aad 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 61 6c 6c   dynamically all
19aae 6f 63 61 74 65 64 20 73 70 61 63 65 20 74 6f 20  ocated space to 
19aaf 68 6f 6c 64 20 74 68 65 20 64 61 74 61 20 28 69  hold the data (i
19ab0 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74  n the.  ** sqlit
19ab1 65 33 56 64 62 65 4d 65 6d 46 72 6f 6d 42 74 72  e3VdbeMemFromBtr
19ab2 65 65 28 29 20 63 61 6c 6c 20 61 62 6f 76 65 29  ee() call above)
19ab3 20 74 68 65 6e 20 74 72 61 6e 73 66 65 72 20 63   then transfer c
19ab4 6f 6e 74 72 6f 6c 20 6f 66 20 74 68 61 74 0a 20  ontrol of that. 
19ab5 20 2a 2a 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20   ** dynamically 
19ab6 61 6c 6c 6f 63 61 74 65 64 20 73 70 61 63 65 20  allocated space 
19ab7 6f 76 65 72 20 74 6f 20 74 68 65 20 70 44 65 73  over to the pDes
19ab8 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 2a  t structure..  *
19ab9 2a 20 54 68 69 73 20 70 72 65 76 65 6e 74 73 20  * This prevents 
19aba 61 20 6d 65 6d 6f 72 79 20 63 6f 70 79 2e 0a 20  a memory copy.. 
19abb 20 2a 2f 0a 20 20 69 66 28 20 73 4d 65 6d 2e 7a   */.  if( sMem.z
19abc 4d 61 6c 6c 6f 63 20 29 7b 0a 20 20 20 20 61 73  Malloc ){.    as
19abd 73 65 72 74 28 20 73 4d 65 6d 2e 7a 3d 3d 73 4d  sert( sMem.z==sM
19abe 65 6d 2e 7a 4d 61 6c 6c 6f 63 20 29 3b 0a 20 20  em.zMalloc );.  
19abf 20 20 61 73 73 65 72 74 28 20 21 28 70 44 65 73    assert( !(pDes
19ac0 74 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 44  t->flags & MEM_D
19ac1 79 6e 29 20 29 3b 0a 20 20 20 20 61 73 73 65 72  yn) );.    asser
19ac2 74 28 20 21 28 70 44 65 73 74 2d 3e 66 6c 61 67  t( !(pDest->flag
19ac3 73 20 26 20 28 4d 45 4d 5f 42 6c 6f 62 7c 4d 45  s & (MEM_Blob|ME
19ac4 4d 5f 53 74 72 29 29 20 7c 7c 20 70 44 65 73 74  M_Str)) || pDest
19ac5 2d 3e 7a 3d 3d 73 4d 65 6d 2e 7a 20 29 3b 0a 20  ->z==sMem.z );. 
19ac6 20 20 20 70 44 65 73 74 2d 3e 66 6c 61 67 73 20     pDest->flags 
19ac7 26 3d 20 7e 28 4d 45 4d 5f 45 70 68 65 6d 7c 4d  &= ~(MEM_Ephem|M
19ac8 45 4d 5f 53 74 61 74 69 63 29 3b 0a 20 20 20 20  EM_Static);.    
19ac9 70 44 65 73 74 2d 3e 66 6c 61 67 73 20 7c 3d 20  pDest->flags |= 
19aca 4d 45 4d 5f 54 65 72 6d 3b 0a 20 20 20 20 70 44  MEM_Term;.    pD
19acb 65 73 74 2d 3e 7a 20 3d 20 73 4d 65 6d 2e 7a 3b  est->z = sMem.z;
19acc 0a 20 20 20 20 70 44 65 73 74 2d 3e 7a 4d 61 6c  .    pDest->zMal
19acd 6c 6f 63 20 3d 20 73 4d 65 6d 2e 7a 4d 61 6c 6c  loc = sMem.zMall
19ace 6f 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  oc;.  }..  rc = 
19acf 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 4d 61  sqlite3VdbeMemMa
19ad0 6b 65 57 72 69 74 65 61 62 6c 65 28 70 44 65 73  keWriteable(pDes
19ad1 74 29 3b 0a 0a 6f 70 5f 63 6f 6c 75 6d 6e 5f 6f  t);..op_column_o
19ad2 75 74 3a 0a 20 20 55 50 44 41 54 45 5f 4d 41 58  ut:.  UPDATE_MAX
19ad3 5f 42 4c 4f 42 53 49 5a 45 28 70 44 65 73 74 29  _BLOBSIZE(pDest)
19ad4 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
19ad5 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44 65 73  CE(pOp->p3, pDes
19ad6 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  t);.  break;.}..
19ad7 2f 2a 20 4f 70 63 6f 64 65 3a 20 41 66 66 69 6e  /* Opcode: Affin
19ad8 69 74 79 20 50 31 20 50 32 20 2a 20 50 34 20 2a  ity P1 P2 * P4 *
19ad9 0a 2a 2a 0a 2a 2a 20 41 70 70 6c 79 20 61 66 66  .**.** Apply aff
19ada 69 6e 69 74 69 65 73 20 74 6f 20 61 20 72 61 6e  inities to a ran
19adb 67 65 20 6f 66 20 50 32 20 72 65 67 69 73 74 65  ge of P2 registe
19adc 72 73 20 73 74 61 72 74 69 6e 67 20 77 69 74 68  rs starting with
19add 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69 73   P1..**.** P4 is
19ade 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 69   a string that i
19adf 73 20 50 32 20 63 68 61 72 61 63 74 65 72 73 20  s P2 characters 
19ae0 6c 6f 6e 67 2e 20 54 68 65 20 6e 74 68 20 63 68  long. The nth ch
19ae1 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 0a 2a  aracter of the.*
19ae2 2a 20 73 74 72 69 6e 67 20 69 6e 64 69 63 61 74  * string indicat
19ae3 65 73 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 66  es the column af
19ae4 66 69 6e 69 74 79 20 74 68 61 74 20 73 68 6f 75  finity that shou
19ae5 6c 64 20 62 65 20 75 73 65 64 20 66 6f 72 20 74  ld be used for t
19ae6 68 65 20 6e 74 68 0a 2a 2a 20 6d 65 6d 6f 72 79  he nth.** memory
19ae7 20 63 65 6c 6c 20 69 6e 20 74 68 65 20 72 61 6e   cell in the ran
19ae8 67 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41  ge..*/.case OP_A
19ae9 66 66 69 6e 69 74 79 3a 20 7b 0a 20 20 63 68 61  ffinity: {.  cha
19aea 72 20 2a 7a 41 66 66 69 6e 69 74 79 20 3d 20 70  r *zAffinity = p
19aeb 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 4d 65 6d 20  Op->p4.z;.  Mem 
19aec 2a 70 44 61 74 61 30 20 3d 20 26 70 2d 3e 61 4d  *pData0 = &p->aM
19aed 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 4d  em[pOp->p1];.  M
19aee 65 6d 20 2a 70 4c 61 73 74 20 3d 20 26 70 44 61  em *pLast = &pDa
19aef 74 61 30 5b 70 4f 70 2d 3e 70 32 2d 31 5d 3b 0a  ta0[pOp->p2-1];.
19af0 20 20 4d 65 6d 20 2a 70 52 65 63 3b 0a 0a 20 20    Mem *pRec;..  
19af1 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b  for(pRec=pData0;
19af2 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52   pRec<=pLast; pR
19af3 65 63 2b 2b 29 7b 0a 20 20 20 20 45 78 70 61 6e  ec++){.    Expan
19af4 64 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20  dBlob(pRec);.   
19af5 20 61 70 70 6c 79 41 66 66 69 6e 69 74 79 28 70   applyAffinity(p
19af6 52 65 63 2c 20 7a 41 66 66 69 6e 69 74 79 5b 70  Rec, zAffinity[p
19af7 52 65 63 2d 70 44 61 74 61 30 5d 2c 20 65 6e 63  Rec-pData0], enc
19af8 6f 64 69 6e 67 29 3b 0a 20 20 7d 0a 20 20 62 72  oding);.  }.  br
19af9 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
19afa 65 3a 20 4d 61 6b 65 52 65 63 6f 72 64 20 50 31  e: MakeRecord P1
19afb 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
19afc 2a 20 43 6f 6e 76 65 72 74 20 50 32 20 72 65 67  * Convert P2 reg
19afd 69 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67  isters beginning
19afe 20 77 69 74 68 20 50 31 20 69 6e 74 6f 20 61 20   with P1 into a 
19aff 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20  single entry.** 
19b00 73 75 69 74 61 62 6c 65 20 66 6f 72 20 75 73 65  suitable for use
19b01 20 61 73 20 61 20 64 61 74 61 20 72 65 63 6f 72   as a data recor
19b02 64 20 69 6e 20 61 20 64 61 74 61 62 61 73 65 20  d in a database 
19b03 74 61 62 6c 65 20 6f 72 20 61 73 20 61 20 6b 65  table or as a ke
19b04 79 0a 2a 2a 20 69 6e 20 61 6e 20 69 6e 64 65 78  y.** in an index
19b05 2e 20 20 54 68 65 20 64 65 74 61 69 6c 73 20 6f  .  The details o
19b06 66 20 74 68 65 20 66 6f 72 6d 61 74 20 61 72 65  f the format are
19b07 20 69 72 72 65 6c 65 76 61 6e 74 20 61 73 20 6c   irrelevant as l
19b08 6f 6e 67 20 61 73 0a 2a 2a 20 74 68 65 20 4f 50  ong as.** the OP
19b09 5f 43 6f 6c 75 6d 6e 20 6f 70 63 6f 64 65 20 63  _Column opcode c
19b0a 61 6e 20 64 65 63 6f 64 65 20 74 68 65 20 72 65  an decode the re
19b0b 63 6f 72 64 20 6c 61 74 65 72 2e 0a 2a 2a 20 52  cord later..** R
19b0c 65 66 65 72 20 74 6f 20 73 6f 75 72 63 65 20 63  efer to source c
19b0d 6f 64 65 20 63 6f 6d 6d 65 6e 74 73 20 66 6f 72  ode comments for
19b0e 20 74 68 65 20 64 65 74 61 69 6c 73 20 6f 66 20   the details of 
19b0f 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a 20 66 6f  the record.** fo
19b10 72 6d 61 74 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 6d  rmat..**.** P4 m
19b11 61 79 20 62 65 20 61 20 73 74 72 69 6e 67 20 74  ay be a string t
19b12 68 61 74 20 69 73 20 50 32 20 63 68 61 72 61 63  hat is P2 charac
19b13 74 65 72 73 20 6c 6f 6e 67 2e 20 20 54 68 65 20  ters long.  The 
19b14 6e 74 68 20 63 68 61 72 61 63 74 65 72 20 6f 66  nth character of
19b15 20 74 68 65 0a 2a 2a 20 73 74 72 69 6e 67 20 69   the.** string i
19b16 6e 64 69 63 61 74 65 73 20 74 68 65 20 63 6f 6c  ndicates the col
19b17 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 74 68 61  umn affinity tha
19b18 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73 65 64  t should be used
19b19 20 66 6f 72 20 74 68 65 20 6e 74 68 0a 2a 2a 20   for the nth.** 
19b1a 66 69 65 6c 64 20 6f 66 20 74 68 65 20 69 6e 64  field of the ind
19b1b 65 78 20 6b 65 79 2e 0a 2a 2a 0a 2a 2a 20 54 68  ex key..**.** Th
19b1c 65 20 6d 61 70 70 69 6e 67 20 66 72 6f 6d 20 63  e mapping from c
19b1d 68 61 72 61 63 74 65 72 20 74 6f 20 61 66 66 69  haracter to affi
19b1e 6e 69 74 79 20 69 73 20 67 69 76 65 6e 20 62 79  nity is given by
19b1f 20 74 68 65 20 53 51 4c 49 54 45 5f 41 46 46 5f   the SQLITE_AFF_
19b20 0a 2a 2a 20 6d 61 63 72 6f 73 20 64 65 66 69 6e  .** macros defin
19b21 65 64 20 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e  ed in sqliteInt.
19b22 68 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 34 20 69  h..**.** If P4 i
19b23 73 20 4e 55 4c 4c 20 74 68 65 6e 20 61 6c 6c 20  s NULL then all 
19b24 69 6e 64 65 78 20 66 69 65 6c 64 73 20 68 61 76  index fields hav
19b25 65 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 4e  e the affinity N
19b26 4f 4e 45 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  ONE..*/.case OP_
19b27 4d 61 6b 65 52 65 63 6f 72 64 3a 20 7b 0a 20 20  MakeRecord: {.  
19b28 2f 2a 20 41 73 73 75 6d 69 6e 67 20 74 68 65 20  /* Assuming the 
19b29 72 65 63 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  record contains 
19b2a 4e 20 66 69 65 6c 64 73 2c 20 74 68 65 20 72 65  N fields, the re
19b2b 63 6f 72 64 20 66 6f 72 6d 61 74 20 6c 6f 6f 6b  cord format look
19b2c 73 0a 20 20 2a 2a 20 6c 69 6b 65 20 74 68 69 73  s.  ** like this
19b2d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2d 2d 2d 2d  :.  **.  ** ----
19b2e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b2f 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b30 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b31 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b32 2d 2d 2d 2d 0a 20 20 2a 2a 20 7c 20 68 64 72 2d  ----.  ** | hdr-
19b33 73 69 7a 65 20 7c 20 74 79 70 65 20 30 20 7c 20  size | type 0 | 
19b34 74 79 70 65 20 31 20 7c 20 2e 2e 2e 20 7c 20 74  type 1 | ... | t
19b35 79 70 65 20 4e 2d 31 20 7c 20 64 61 74 61 30 20  ype N-1 | data0 
19b36 7c 20 2e 2e 2e 20 7c 20 64 61 74 61 20 4e 2d 31  | ... | data N-1
19b37 20 7c 20 0a 20 20 2a 2a 20 2d 2d 2d 2d 2d 2d 2d   | .  ** -------
19b38 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b39 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b3a 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b3b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
19b3c 2d 0a 20 20 2a 2a 0a 20 20 2a 2a 20 44 61 74 61  -.  **.  ** Data
19b3d 28 30 29 20 69 73 20 74 61 6b 65 6e 20 66 72 6f  (0) is taken fro
19b3e 6d 20 72 65 67 69 73 74 65 72 20 50 31 2e 20 20  m register P1.  
19b3f 44 61 74 61 28 31 29 20 63 6f 6d 65 73 20 66 72  Data(1) comes fr
19b40 6f 6d 20 72 65 67 69 73 74 65 72 20 50 31 2b 31  om register P1+1
19b41 0a 20 20 2a 2a 20 61 6e 64 20 73 6f 20 66 72 6f  .  ** and so fro
19b42 74 68 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 61  th..  **.  ** Ea
19b43 63 68 20 74 79 70 65 20 66 69 65 6c 64 20 69 73  ch type field is
19b44 20 61 20 76 61 72 69 6e 74 20 72 65 70 72 65 73   a varint repres
19b45 65 6e 74 69 6e 67 20 74 68 65 20 73 65 72 69 61  enting the seria
19b46 6c 20 74 79 70 65 20 6f 66 20 74 68 65 20 0a 20  l type of the . 
19b47 20 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e   ** correspondin
19b48 67 20 64 61 74 61 20 65 6c 65 6d 65 6e 74 20 28  g data element (
19b49 73 65 65 20 73 71 6c 69 74 65 33 56 64 62 65 53  see sqlite3VdbeS
19b4a 65 72 69 61 6c 54 79 70 65 28 29 29 2e 20 54 68  erialType()). Th
19b4b 65 0a 20 20 2a 2a 20 68 64 72 2d 73 69 7a 65 20  e.  ** hdr-size 
19b4c 66 69 65 6c 64 20 69 73 20 61 6c 73 6f 20 61 20  field is also a 
19b4d 76 61 72 69 6e 74 20 77 68 69 63 68 20 69 73 20  varint which is 
19b4e 74 68 65 20 6f 66 66 73 65 74 20 66 72 6f 6d 20  the offset from 
19b4f 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 0a 20 20  the beginning.  
19b50 2a 2a 20 6f 66 20 74 68 65 20 72 65 63 6f 72 64  ** of the record
19b51 20 74 6f 20 64 61 74 61 30 2e 0a 20 20 2a 2f 0a   to data0..  */.
19b52 20 20 75 38 20 2a 7a 4e 65 77 52 65 63 6f 72 64    u8 *zNewRecord
19b53 3b 20 20 20 20 20 20 20 20 2f 2a 20 41 20 62 75  ;        /* A bu
19b54 66 66 65 72 20 74 6f 20 68 6f 6c 64 20 74 68 65  ffer to hold the
19b55 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 6e 65   data for the ne
19b56 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 4d 65  w record */.  Me
19b57 6d 20 2a 70 52 65 63 3b 20 20 20 20 20 20 20 20  m *pRec;        
19b58 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65 77 20       /* The new 
19b59 72 65 63 6f 72 64 20 2a 2f 0a 20 20 75 36 34 20  record */.  u64 
19b5a 6e 44 61 74 61 20 3d 20 30 3b 20 20 20 20 20 20  nData = 0;      
19b5b 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
19b5c 62 79 74 65 73 20 6f 66 20 64 61 74 61 20 73 70  bytes of data sp
19b5d 61 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 48 64  ace */.  int nHd
19b5e 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
19b5f 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
19b60 65 73 20 6f 66 20 68 65 61 64 65 72 20 73 70 61  es of header spa
19b61 63 65 20 2a 2f 0a 20 20 69 36 34 20 6e 42 79 74  ce */.  i64 nByt
19b62 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f  e = 0;         /
19b63 2a 20 44 61 74 61 20 73 70 61 63 65 20 72 65 71  * Data space req
19b64 75 69 72 65 64 20 66 6f 72 20 74 68 69 73 20 72  uired for this r
19b65 65 63 6f 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e  ecord */.  int n
19b66 5a 65 72 6f 20 3d 20 30 3b 20 20 20 20 20 20 20  Zero = 0;       
19b67 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 7a    /* Number of z
19b68 65 72 6f 20 62 79 74 65 73 20 61 74 20 74 68 65  ero bytes at the
19b69 20 65 6e 64 20 6f 66 20 74 68 65 20 72 65 63 6f   end of the reco
19b6a 72 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 56 61 72  rd */.  int nVar
19b6b 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f  int;           /
19b6c 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74 65  * Number of byte
19b6d 73 20 69 6e 20 61 20 76 61 72 69 6e 74 20 2a 2f  s in a varint */
19b6e 0a 20 20 75 33 32 20 73 65 72 69 61 6c 5f 74 79  .  u32 serial_ty
19b6f 70 65 3b 20 20 20 20 20 20 20 2f 2a 20 54 79 70  pe;       /* Typ
19b70 65 20 66 69 65 6c 64 20 2a 2f 0a 20 20 4d 65 6d  e field */.  Mem
19b71 20 2a 70 44 61 74 61 30 3b 20 20 20 20 20 20 20   *pData0;       
19b72 20 20 20 20 2f 2a 20 46 69 72 73 74 20 66 69 65      /* First fie
19b73 6c 64 20 74 6f 20 62 65 20 63 6f 6d 62 69 6e 65  ld to be combine
19b74 64 20 69 6e 74 6f 20 74 68 65 20 72 65 63 6f 72  d into the recor
19b75 64 20 2a 2f 0a 20 20 4d 65 6d 20 2a 70 4c 61 73  d */.  Mem *pLas
19b76 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
19b77 20 4c 61 73 74 20 66 69 65 6c 64 20 6f 66 20 74   Last field of t
19b78 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
19b79 6e 74 20 6e 46 69 65 6c 64 3b 20 20 20 20 20 20  nt nField;      
19b7a 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
19b7b 6f 66 20 66 69 65 6c 64 73 20 69 6e 20 74 68 65  of fields in the
19b7c 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 63 68 61   record */.  cha
19b7d 72 20 2a 7a 41 66 66 69 6e 69 74 79 3b 20 20 20  r *zAffinity;   
19b7e 20 20 20 20 2f 2a 20 54 68 65 20 61 66 66 69 6e      /* The affin
19b7f 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72 20 74  ity string for t
19b80 68 65 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  he record */.  i
19b81 6e 74 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3b 20  nt file_format; 
19b82 20 20 20 20 20 20 2f 2a 20 46 69 6c 65 20 66 6f        /* File fo
19b83 72 6d 61 74 20 74 6f 20 75 73 65 20 66 6f 72 20  rmat to use for 
19b84 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e  encoding */.  in
19b85 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
19b86 20 20 20 20 20 2f 2a 20 53 70 61 63 65 20 75 73       /* Space us
19b87 65 64 20 69 6e 20 7a 4e 65 77 52 65 63 6f 72 64  ed in zNewRecord
19b88 5b 5d 20 2a 2f 0a 0a 20 20 6e 46 69 65 6c 64 20  [] */..  nField 
19b89 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 7a 41 66  = pOp->p1;.  zAf
19b8a 66 69 6e 69 74 79 20 3d 20 70 4f 70 2d 3e 70 34  finity = pOp->p4
19b8b 2e 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 46  .z;.  assert( nF
19b8c 69 65 6c 64 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ield>0 && pOp->p
19b8d 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70 32 2b 6e  2>0 && pOp->p2+n
19b8e 46 69 65 6c 64 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  Field<=p->nMem )
19b8f 3b 0a 20 20 70 44 61 74 61 30 20 3d 20 26 70 2d  ;.  pData0 = &p-
19b90 3e 61 4d 65 6d 5b 6e 46 69 65 6c 64 5d 3b 0a 20  >aMem[nField];. 
19b91 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70   nField = pOp->p
19b92 32 3b 0a 20 20 70 4c 61 73 74 20 3d 20 26 70 44  2;.  pLast = &pD
19b93 61 74 61 30 5b 6e 46 69 65 6c 64 2d 31 5d 3b 0a  ata0[nField-1];.
19b94 20 20 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20    file_format = 
19b95 70 2d 3e 6d 69 6e 57 72 69 74 65 46 69 6c 65 46  p->minWriteFileF
19b96 6f 72 6d 61 74 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f  ormat;..  /* Loo
19b97 70 20 74 68 72 6f 75 67 68 20 74 68 65 20 65 6c  p through the el
19b98 65 6d 65 6e 74 73 20 74 68 61 74 20 77 69 6c 6c  ements that will
19b99 20 6d 61 6b 65 20 75 70 20 74 68 65 20 72 65 63   make up the rec
19b9a 6f 72 64 20 74 6f 20 66 69 67 75 72 65 0a 20 20  ord to figure.  
19b9b 2a 2a 20 6f 75 74 20 68 6f 77 20 6d 75 63 68 20  ** out how much 
19b9c 73 70 61 63 65 20 69 73 20 72 65 71 75 69 72 65  space is require
19b9d 64 20 66 6f 72 20 74 68 65 20 6e 65 77 20 72 65  d for the new re
19b9e 63 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  cord..  */.  for
19b9f 28 70 52 65 63 3d 70 44 61 74 61 30 3b 20 70 52  (pRec=pData0; pR
19ba0 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52 65 63 2b  ec<=pLast; pRec+
19ba1 2b 29 7b 0a 20 20 20 20 69 6e 74 20 6c 65 6e 3b  +){.    int len;
19ba2 0a 20 20 20 20 69 66 28 20 7a 41 66 66 69 6e 69  .    if( zAffini
19ba3 74 79 20 29 7b 0a 20 20 20 20 20 20 61 70 70 6c  ty ){.      appl
19ba4 79 41 66 66 69 6e 69 74 79 28 70 52 65 63 2c 20  yAffinity(pRec, 
19ba5 7a 41 66 66 69 6e 69 74 79 5b 70 52 65 63 2d 70  zAffinity[pRec-p
19ba6 44 61 74 61 30 5d 2c 20 65 6e 63 6f 64 69 6e 67  Data0], encoding
19ba7 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28  );.    }.    if(
19ba8 20 70 52 65 63 2d 3e 66 6c 61 67 73 26 4d 45 4d   pRec->flags&MEM
19ba9 5f 5a 65 72 6f 20 26 26 20 70 52 65 63 2d 3e 6e  _Zero && pRec->n
19baa 3e 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  >0 ){.      sqli
19bab 74 65 33 56 64 62 65 4d 65 6d 45 78 70 61 6e 64  te3VdbeMemExpand
19bac 42 6c 6f 62 28 70 52 65 63 29 3b 0a 20 20 20 20  Blob(pRec);.    
19bad 7d 0a 20 20 20 20 73 65 72 69 61 6c 5f 74 79 70  }.    serial_typ
19bae 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 53  e = sqlite3VdbeS
19baf 65 72 69 61 6c 54 79 70 65 28 70 52 65 63 2c 20  erialType(pRec, 
19bb0 66 69 6c 65 5f 66 6f 72 6d 61 74 29 3b 0a 20 20  file_format);.  
19bb1 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 56    len = sqlite3V
19bb2 64 62 65 53 65 72 69 61 6c 54 79 70 65 4c 65 6e  dbeSerialTypeLen
19bb3 28 73 65 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20  (serial_type);. 
19bb4 20 20 20 6e 44 61 74 61 20 2b 3d 20 6c 65 6e 3b     nData += len;
19bb5 0a 20 20 20 20 6e 48 64 72 20 2b 3d 20 73 71 6c  .    nHdr += sql
19bb6 69 74 65 33 56 61 72 69 6e 74 4c 65 6e 28 73 65  ite3VarintLen(se
19bb7 72 69 61 6c 5f 74 79 70 65 29 3b 0a 20 20 20 20  rial_type);.    
19bb8 69 66 28 20 70 52 65 63 2d 3e 66 6c 61 67 73 20  if( pRec->flags 
19bb9 26 20 4d 45 4d 5f 5a 65 72 6f 20 29 7b 0a 20 20  & MEM_Zero ){.  
19bba 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 70 75 72 65      /* Only pure
19bbb 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 42 4c 4f   zero-filled BLO
19bbc 42 73 20 63 61 6e 20 62 65 20 69 6e 70 75 74 20  Bs can be input 
19bbd 74 6f 20 74 68 69 73 20 4f 70 63 6f 64 65 2e 0a  to this Opcode..
19bbe 20 20 20 20 20 20 2a 2a 20 57 65 20 64 6f 20 6e        ** We do n
19bbf 6f 74 20 61 6c 6c 6f 77 20 62 6c 6f 62 73 20 77  ot allow blobs w
19bc0 69 74 68 20 61 20 70 72 65 66 69 78 20 61 6e 64  ith a prefix and
19bc1 20 61 20 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 74   a zero-filled t
19bc2 61 69 6c 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 5a  ail. */.      nZ
19bc3 65 72 6f 20 2b 3d 20 70 52 65 63 2d 3e 75 2e 69  ero += pRec->u.i
19bc4 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
19bc5 6c 65 6e 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65  len ){.      nZe
19bc6 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ro = 0;.    }.  
19bc7 7d 0a 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20  }..  /* Add the 
19bc8 69 6e 69 74 69 61 6c 20 68 65 61 64 65 72 20 76  initial header v
19bc9 61 72 69 6e 74 20 61 6e 64 20 74 6f 74 61 6c 20  arint and total 
19bca 74 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20 6e 48  the size */.  nH
19bcb 64 72 20 2b 3d 20 6e 56 61 72 69 6e 74 20 3d 20  dr += nVarint = 
19bcc 73 71 6c 69 74 65 33 56 61 72 69 6e 74 4c 65 6e  sqlite3VarintLen
19bcd 28 6e 48 64 72 29 3b 0a 20 20 69 66 28 20 6e 56  (nHdr);.  if( nV
19bce 61 72 69 6e 74 3c 73 71 6c 69 74 65 33 56 61 72  arint<sqlite3Var
19bcf 69 6e 74 4c 65 6e 28 6e 48 64 72 29 20 29 7b 0a  intLen(nHdr) ){.
19bd0 20 20 20 20 6e 48 64 72 2b 2b 3b 0a 20 20 7d 0a      nHdr++;.  }.
19bd1 20 20 6e 42 79 74 65 20 3d 20 6e 48 64 72 2b 6e    nByte = nHdr+n
19bd2 44 61 74 61 2d 6e 5a 65 72 6f 3b 0a 20 20 69 66  Data-nZero;.  if
19bd3 28 20 6e 42 79 74 65 3e 64 62 2d 3e 61 4c 69 6d  ( nByte>db->aLim
19bd4 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
19bd5 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 67  LENGTH] ){.    g
19bd6 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d  oto too_big;.  }
19bd7 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65  ..  /* Make sure
19bd8 20 74 68 65 20 6f 75 74 70 75 74 20 72 65 67 69   the output regi
19bd9 73 74 65 72 20 68 61 73 20 61 20 62 75 66 66 65  ster has a buffe
19bda 72 20 6c 61 72 67 65 20 65 6e 6f 75 67 68 20 74  r large enough t
19bdb 6f 20 73 74 6f 72 65 20 0a 20 20 2a 2a 20 74 68  o store .  ** th
19bdc 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 20 54 68  e new record. Th
19bdd 65 20 6f 75 74 70 75 74 20 72 65 67 69 73 74 65  e output registe
19bde 72 20 28 70 4f 70 2d 3e 70 33 29 20 69 73 20 6e  r (pOp->p3) is n
19bdf 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 0a 20 20  ot allowed to.  
19be0 2a 2a 20 62 65 20 6f 6e 65 20 6f 66 20 74 68 65  ** be one of the
19be1 20 69 6e 70 75 74 20 72 65 67 69 73 74 65 72 73   input registers
19be2 20 28 62 65 63 61 75 73 65 20 74 68 65 20 66 6f   (because the fo
19be3 6c 6c 6f 77 69 6e 67 20 63 61 6c 6c 20 74 6f 0a  llowing call to.
19be4 20 20 2a 2a 20 73 71 6c 69 74 65 33 56 64 62 65    ** sqlite3Vdbe
19be5 4d 65 6d 47 72 6f 77 28 29 20 63 6f 75 6c 64 20  MemGrow() could 
19be6 63 6c 6f 62 62 65 72 20 74 68 65 20 76 61 6c 75  clobber the valu
19be7 65 20 62 65 66 6f 72 65 20 69 74 20 69 73 20 75  e before it is u
19be8 73 65 64 29 2e 0a 20 20 2a 2f 0a 20 20 61 73 73  sed)..  */.  ass
19be9 65 72 74 28 20 70 4f 70 2d 3e 70 33 3c 70 4f 70  ert( pOp->p3<pOp
19bea 2d 3e 70 31 20 7c 7c 20 70 4f 70 2d 3e 70 33 3e  ->p1 || pOp->p3>
19beb 3d 70 4f 70 2d 3e 70 31 2b 70 4f 70 2d 3e 70 32  =pOp->p1+pOp->p2
19bec 20 29 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d   );.  pOut = &p-
19bed 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a  >aMem[pOp->p3];.
19bee 20 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62    if( sqlite3Vdb
19bef 65 4d 65 6d 47 72 6f 77 28 70 4f 75 74 2c 20 6e  eMemGrow(pOut, n
19bf0 42 79 74 65 2c 20 30 29 20 29 7b 0a 20 20 20 20  Byte, 0) ){.    
19bf1 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d  goto no_mem;.  }
19bf2 0a 20 20 7a 4e 65 77 52 65 63 6f 72 64 20 3d 20  .  zNewRecord = 
19bf3 28 75 38 20 2a 29 70 4f 75 74 2d 3e 7a 3b 0a 0a  (u8 *)pOut->z;..
19bf4 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 72    /* Write the r
19bf5 65 63 6f 72 64 20 2a 2f 0a 20 20 69 20 3d 20 70  ecord */.  i = p
19bf6 75 74 56 61 72 69 6e 74 33 32 28 7a 4e 65 77 52  utVarint32(zNewR
19bf7 65 63 6f 72 64 2c 20 6e 48 64 72 29 3b 0a 20 20  ecord, nHdr);.  
19bf8 66 6f 72 28 70 52 65 63 3d 70 44 61 74 61 30 3b  for(pRec=pData0;
19bf9 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b 20 70 52   pRec<=pLast; pR
19bfa 65 63 2b 2b 29 7b 0a 20 20 20 20 73 65 72 69 61  ec++){.    seria
19bfb 6c 5f 74 79 70 65 20 3d 20 73 71 6c 69 74 65 33  l_type = sqlite3
19bfc 56 64 62 65 53 65 72 69 61 6c 54 79 70 65 28 70  VdbeSerialType(p
19bfd 52 65 63 2c 20 66 69 6c 65 5f 66 6f 72 6d 61 74  Rec, file_format
19bfe 29 3b 0a 20 20 20 20 69 20 2b 3d 20 70 75 74 56  );.    i += putV
19bff 61 72 69 6e 74 33 32 28 26 7a 4e 65 77 52 65 63  arint32(&zNewRec
19c00 6f 72 64 5b 69 5d 2c 20 73 65 72 69 61 6c 5f 74  ord[i], serial_t
19c01 79 70 65 29 3b 20 20 20 20 20 20 2f 2a 20 73 65  ype);      /* se
19c02 72 69 61 6c 20 74 79 70 65 20 2a 2f 0a 20 20 7d  rial type */.  }
19c03 0a 20 20 66 6f 72 28 70 52 65 63 3d 70 44 61 74  .  for(pRec=pDat
19c04 61 30 3b 20 70 52 65 63 3c 3d 70 4c 61 73 74 3b  a0; pRec<=pLast;
19c05 20 70 52 65 63 2b 2b 29 7b 20 20 2f 2a 20 73 65   pRec++){  /* se
19c06 72 69 61 6c 20 64 61 74 61 20 2a 2f 0a 20 20 20  rial data */.   
19c07 20 69 20 2b 3d 20 73 71 6c 69 74 65 33 56 64 62   i += sqlite3Vdb
19c08 65 53 65 72 69 61 6c 50 75 74 28 26 7a 4e 65 77  eSerialPut(&zNew
19c09 52 65 63 6f 72 64 5b 69 5d 2c 20 6e 42 79 74 65  Record[i], nByte
19c0a 2d 69 2c 20 70 52 65 63 2c 20 66 69 6c 65 5f 66  -i, pRec, file_f
19c0b 6f 72 6d 61 74 29 3b 0a 20 20 7d 0a 20 20 61 73  ormat);.  }.  as
19c0c 73 65 72 74 28 20 69 3d 3d 6e 42 79 74 65 20 29  sert( i==nByte )
19c0d 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  ;..  assert( pOp
19c0e 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p3>0 && pOp->p
19c0f 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20  3<=p->nMem );.  
19c10 70 4f 75 74 2d 3e 6e 20 3d 20 6e 42 79 74 65 3b  pOut->n = nByte;
19c11 0a 20 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 3d  .  pOut->flags =
19c12 20 4d 45 4d 5f 42 6c 6f 62 20 7c 20 4d 45 4d 5f   MEM_Blob | MEM_
19c13 44 79 6e 3b 0a 20 20 70 4f 75 74 2d 3e 78 44 65  Dyn;.  pOut->xDe
19c14 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 5a 65  l = 0;.  if( nZe
19c15 72 6f 20 29 7b 0a 20 20 20 20 70 4f 75 74 2d 3e  ro ){.    pOut->
19c16 75 2e 69 20 3d 20 6e 5a 65 72 6f 3b 0a 20 20 20  u.i = nZero;.   
19c17 20 70 4f 75 74 2d 3e 66 6c 61 67 73 20 7c 3d 20   pOut->flags |= 
19c18 4d 45 4d 5f 5a 65 72 6f 3b 0a 20 20 7d 0a 20 20  MEM_Zero;.  }.  
19c19 70 4f 75 74 2d 3e 65 6e 63 20 3d 20 53 51 4c 49  pOut->enc = SQLI
19c1a 54 45 5f 55 54 46 38 3b 20 20 2f 2a 20 49 6e 20  TE_UTF8;  /* In 
19c1b 63 61 73 65 20 74 68 65 20 62 6c 6f 62 20 69 73  case the blob is
19c1c 20 65 76 65 72 20 63 6f 6e 76 65 72 74 65 64 20   ever converted 
19c1d 74 6f 20 74 65 78 74 20 2a 2f 0a 20 20 52 45 47  to text */.  REG
19c1e 49 53 54 45 52 5f 54 52 41 43 45 28 70 4f 70 2d  ISTER_TRACE(pOp-
19c1f 3e 70 33 2c 20 70 4f 75 74 29 3b 0a 20 20 55 50  >p3, pOut);.  UP
19c20 44 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a  DATE_MAX_BLOBSIZ
19c21 45 28 70 4f 75 74 29 3b 0a 20 20 62 72 65 61 6b  E(pOut);.  break
19c22 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ;.}../* Opcode: 
19c23 53 74 61 74 65 6d 65 6e 74 20 50 31 20 2a 20 2a  Statement P1 * *
19c24 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 42 65 67 69 6e   * *.**.** Begin
19c25 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 73   an individual s
19c26 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73 61 63  tatement transac
19c27 74 69 6f 6e 20 77 68 69 63 68 20 69 73 20 70 61  tion which is pa
19c28 72 74 20 6f 66 20 61 20 6c 61 72 67 65 72 0a 2a  rt of a larger.*
19c29 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20 20  * transaction.  
19c2a 54 68 69 73 20 69 73 20 6e 65 65 64 65 64 20 73  This is needed s
19c2b 6f 20 74 68 61 74 20 74 68 65 20 73 74 61 74 65  o that the state
19c2c 6d 65 6e 74 0a 2a 2a 20 63 61 6e 20 62 65 20 72  ment.** can be r
19c2d 6f 6c 6c 65 64 20 62 61 63 6b 20 61 66 74 65 72  olled back after
19c2e 20 61 6e 20 65 72 72 6f 72 20 77 69 74 68 6f 75   an error withou
19c2f 74 20 68 61 76 69 6e 67 20 74 6f 20 72 6f 6c 6c  t having to roll
19c30 20 62 61 63 6b 20 74 68 65 0a 2a 2a 20 65 6e 74   back the.** ent
19c31 69 72 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e  ire transaction.
19c32 20 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74 20    The statement 
19c33 74 72 61 6e 73 61 63 74 69 6f 6e 20 77 69 6c 6c  transaction will
19c34 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a   automatically.*
19c35 2a 20 63 6f 6d 6d 69 74 20 77 68 65 6e 20 74 68  * commit when th
19c36 65 20 56 44 42 45 20 68 61 6c 74 73 2e 0a 2a 2a  e VDBE halts..**
19c37 0a 2a 2a 20 49 66 20 74 68 65 20 64 61 74 61 62  .** If the datab
19c38 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 69  ase connection i
19c39 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e 20 61  s currently in a
19c3a 75 74 6f 63 6f 6d 6d 69 74 20 6d 6f 64 65 20 28  utocommit mode (
19c3b 74 68 61 74 20 0a 2a 2a 20 69 73 20 74 6f 20 73  that .** is to s
19c3c 61 79 2c 20 69 66 20 69 74 20 69 73 20 69 6e 20  ay, if it is in 
19c3d 62 65 74 77 65 65 6e 20 42 45 47 49 4e 20 61 6e  between BEGIN an
19c3e 64 20 43 4f 4d 4d 49 54 29 0a 2a 2a 20 61 6e 64  d COMMIT).** and
19c3f 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f   if there are no
19c40 20 6f 74 68 65 72 20 61 63 74 69 76 65 20 73 74   other active st
19c41 61 74 65 6d 65 6e 74 73 20 6f 6e 20 74 68 65 20  atements on the 
19c42 73 61 6d 65 20 64 61 74 61 62 61 73 65 0a 2a 2a  same database.**
19c43 20 63 6f 6e 6e 65 63 74 69 6f 6e 2c 20 74 68 65   connection, the
19c44 6e 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  n this operation
19c45 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20 20 4e 6f   is a no-op.  No
19c46 20 73 74 61 74 65 6d 65 6e 74 20 74 72 61 6e 73   statement trans
19c47 61 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 6e 65 65  action.** is nee
19c48 64 65 64 20 73 69 6e 63 65 20 61 6e 79 20 65 72  ded since any er
19c49 72 6f 72 20 63 61 6e 20 75 73 65 20 74 68 65 20  ror can use the 
19c4a 6e 6f 72 6d 61 6c 20 52 4f 4c 4c 42 41 43 4b 20  normal ROLLBACK 
19c4b 70 72 6f 63 65 73 73 20 74 6f 0a 2a 2a 20 75 6e  process to.** un
19c4c 64 6f 20 63 68 61 6e 67 65 73 2e 0a 2a 2a 0a 2a  do changes..**.*
19c4d 2a 20 49 66 20 61 20 73 74 61 74 65 6d 65 6e 74  * If a statement
19c4e 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20   transaction is 
19c4f 73 74 61 72 74 65 64 2c 20 74 68 65 6e 20 61 20  started, then a 
19c50 73 74 61 74 65 6d 65 6e 74 20 6a 6f 75 72 6e 61  statement journa
19c51 6c 20 66 69 6c 65 0a 2a 2a 20 77 69 6c 6c 20 62  l file.** will b
19c52 65 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  e allocated and 
19c53 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 2a 2a 0a  initialized..**.
19c54 2a 2a 20 54 68 65 20 73 74 61 74 65 6d 65 6e 74  ** The statement
19c55 20 69 73 20 62 65 67 75 6e 20 6f 6e 20 74 68 65   is begun on the
19c56 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 77   database file w
19c57 69 74 68 20 69 6e 64 65 78 20 50 31 2e 20 20 54  ith index P1.  T
19c58 68 65 20 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62  he main.** datab
19c59 61 73 65 20 66 69 6c 65 20 68 61 73 20 61 6e 20  ase file has an 
19c5a 69 6e 64 65 78 20 6f 66 20 30 20 61 6e 64 20 74  index of 0 and t
19c5b 68 65 20 66 69 6c 65 20 75 73 65 64 20 66 6f 72  he file used for
19c5c 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
19c5d 73 0a 2a 2a 20 68 61 73 20 61 6e 20 69 6e 64 65  s.** has an inde
19c5e 78 20 6f 66 20 31 2e 0a 2a 2f 0a 63 61 73 65 20  x of 1..*/.case 
19c5f 4f 50 5f 53 74 61 74 65 6d 65 6e 74 3a 20 7b 0a  OP_Statement: {.
19c60 20 20 69 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f    if( db->autoCo
19c61 6d 6d 69 74 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61  mmit==0 || db->a
19c62 63 74 69 76 65 56 64 62 65 43 6e 74 3e 31 20 29  ctiveVdbeCnt>1 )
19c63 7b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 70 4f  {.    int i = pO
19c64 70 2d 3e 70 31 3b 0a 20 20 20 20 42 74 72 65 65  p->p1;.    Btree
19c65 20 2a 70 42 74 3b 0a 20 20 20 20 61 73 73 65 72   *pBt;.    asser
19c66 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 64 62 2d  t( i>=0 && i<db-
19c67 3e 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65  >nDb );.    asse
19c68 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70  rt( db->aDb[i].p
19c69 42 74 21 3d 30 20 29 3b 0a 20 20 20 20 70 42 74  Bt!=0 );.    pBt
19c6a 20 3d 20 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42   = db->aDb[i].pB
19c6b 74 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 73  t;.    assert( s
19c6c 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54  qlite3BtreeIsInT
19c6d 72 61 6e 73 28 70 42 74 29 20 29 3b 0a 20 20 20  rans(pBt) );.   
19c6e 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72   assert( (p->btr
19c6f 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 29 29  eeMask & (1<<i))
19c70 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 21  !=0 );.    if( !
19c71 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e  sqlite3BtreeIsIn
19c72 53 74 6d 74 28 70 42 74 29 20 29 7b 0a 20 20 20  Stmt(pBt) ){.   
19c73 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19c74 74 72 65 65 42 65 67 69 6e 53 74 6d 74 28 70 42  treeBeginStmt(pB
19c75 74 29 3b 0a 20 20 20 20 20 20 70 2d 3e 6f 70 65  t);.      p->ope
19c76 6e 65 64 53 74 61 74 65 6d 65 6e 74 20 3d 20 31  nedStatement = 1
19c77 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 62 72  ;.    }.  }.  br
19c78 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64  eak;.}../* Opcod
19c79 65 3a 20 41 75 74 6f 43 6f 6d 6d 69 74 20 50 31  e: AutoCommit P1
19c7a 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20   P2 * * *.**.** 
19c7b 53 65 74 20 74 68 65 20 64 61 74 61 62 61 73 65  Set the database
19c7c 20 61 75 74 6f 2d 63 6f 6d 6d 69 74 20 66 6c 61   auto-commit fla
19c7d 67 20 74 6f 20 50 31 20 28 31 20 6f 72 20 30 29  g to P1 (1 or 0)
19c7e 2e 20 49 66 20 50 32 20 69 73 20 74 72 75 65 2c  . If P2 is true,
19c7f 20 72 6f 6c 6c 0a 2a 2a 20 62 61 63 6b 20 61 6e   roll.** back an
19c80 79 20 63 75 72 72 65 6e 74 6c 79 20 61 63 74 69  y currently acti
19c81 76 65 20 62 74 72 65 65 20 74 72 61 6e 73 61 63  ve btree transac
19c82 74 69 6f 6e 73 2e 20 49 66 20 74 68 65 72 65 20  tions. If there 
19c83 61 72 65 20 61 6e 79 20 61 63 74 69 76 65 0a 2a  are any active.*
19c84 2a 20 56 4d 73 20 28 61 70 61 72 74 20 66 72 6f  * VMs (apart fro
19c85 6d 20 74 68 69 73 20 6f 6e 65 29 2c 20 74 68 65  m this one), the
19c86 6e 20 74 68 65 20 43 4f 4d 4d 49 54 20 6f 72 20  n the COMMIT or 
19c87 52 4f 4c 4c 42 41 43 4b 20 73 74 61 74 65 6d 65  ROLLBACK stateme
19c88 6e 74 20 66 61 69 6c 73 2e 0a 2a 2a 0a 2a 2a 20  nt fails..**.** 
19c89 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
19c8a 20 63 61 75 73 65 73 20 74 68 65 20 56 4d 20 74   causes the VM t
19c8b 6f 20 68 61 6c 74 2e 0a 2a 2f 0a 63 61 73 65 20  o halt..*/.case 
19c8c 4f 50 5f 41 75 74 6f 43 6f 6d 6d 69 74 3a 20 7b  OP_AutoCommit: {
19c8d 0a 20 20 69 6e 74 20 64 65 73 69 72 65 64 41 75  .  int desiredAu
19c8e 74 6f 43 6f 6d 6d 69 74 20 3d 20 70 4f 70 2d 3e  toCommit = pOp->
19c8f 70 31 3b 0a 20 20 69 6e 74 20 72 6f 6c 6c 62 61  p1;.  int rollba
19c90 63 6b 20 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20  ck = pOp->p2;.  
19c91 69 6e 74 20 74 75 72 6e 4f 6e 41 43 20 3d 20 64  int turnOnAC = d
19c92 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
19c93 20 26 26 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   && !db->autoCom
19c94 6d 69 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  mit;..  assert( 
19c95 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
19c96 74 3d 3d 31 20 7c 7c 20 64 65 73 69 72 65 64 41  t==1 || desiredA
19c97 75 74 6f 43 6f 6d 6d 69 74 3d 3d 30 20 29 3b 0a  utoCommit==0 );.
19c98 20 20 61 73 73 65 72 74 28 20 64 65 73 69 72 65    assert( desire
19c99 64 41 75 74 6f 43 6f 6d 6d 69 74 3d 3d 31 20 7c  dAutoCommit==1 |
19c9a 7c 20 72 6f 6c 6c 62 61 63 6b 3d 3d 30 20 29 3b  | rollback==0 );
19c9b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e  ..  assert( db->
19c9c 61 63 74 69 76 65 56 64 62 65 43 6e 74 3e 30 20  activeVdbeCnt>0 
19c9d 29 3b 20 20 2f 2a 20 41 74 20 6c 65 61 73 74 20  );  /* At least 
19c9e 74 68 69 73 20 6f 6e 65 20 56 4d 20 69 73 20 61  this one VM is a
19c9f 63 74 69 76 65 20 2a 2f 0a 0a 20 20 69 66 28 20  ctive */..  if( 
19ca0 74 75 72 6e 4f 6e 41 43 20 26 26 20 72 6f 6c 6c  turnOnAC && roll
19ca1 62 61 63 6b 20 26 26 20 64 62 2d 3e 61 63 74 69  back && db->acti
19ca2 76 65 56 64 62 65 43 6e 74 3e 31 20 29 7b 0a 20  veVdbeCnt>1 ){. 
19ca3 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e     /* If this in
19ca4 73 74 72 75 63 74 69 6f 6e 20 69 6d 70 6c 65 6d  struction implem
19ca5 65 6e 74 73 20 61 20 52 4f 4c 4c 42 41 43 4b 20  ents a ROLLBACK 
19ca6 61 6e 64 20 6f 74 68 65 72 20 56 4d 73 20 61 72  and other VMs ar
19ca7 65 0a 20 20 20 20 2a 2a 20 73 74 69 6c 6c 20 72  e.    ** still r
19ca8 75 6e 6e 69 6e 67 2c 20 61 6e 64 20 61 20 74 72  unning, and a tr
19ca9 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63 74  ansaction is act
19caa 69 76 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  ive, return an e
19cab 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67 0a  rror indicating.
19cac 20 20 20 20 2a 2a 20 74 68 61 74 20 74 68 65 20      ** that the 
19cad 6f 74 68 65 72 20 56 4d 73 20 6d 75 73 74 20 63  other VMs must c
19cae 6f 6d 70 6c 65 74 65 20 66 69 72 73 74 2e 20 0a  omplete first. .
19caf 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74      */.    sqlit
19cb0 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e  e3SetString(&p->
19cb1 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 63 61  zErrMsg, db, "ca
19cb2 6e 6e 6f 74 20 72 6f 6c 6c 62 61 63 6b 20 74 72  nnot rollback tr
19cb3 61 6e 73 61 63 74 69 6f 6e 20 2d 20 22 0a 20 20  ansaction - ".  
19cb4 20 20 20 20 20 20 22 53 51 4c 20 73 74 61 74 65        "SQL state
19cb5 6d 65 6e 74 73 20 69 6e 20 70 72 6f 67 72 65 73  ments in progres
19cb6 73 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51  s");.    rc = SQ
19cb7 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 65 6c  LITE_BUSY;.  }el
19cb8 73 65 20 69 66 28 20 74 75 72 6e 4f 6e 41 43 20  se if( turnOnAC 
19cb9 26 26 20 21 72 6f 6c 6c 62 61 63 6b 20 26 26 20  && !rollback && 
19cba 64 62 2d 3e 77 72 69 74 65 56 64 62 65 43 6e 74  db->writeVdbeCnt
19cbb 3e 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20  >1 ){.    /* If 
19cbc 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  this instruction
19cbd 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 43 4f   implements a CO
19cbe 4d 4d 49 54 20 61 6e 64 20 6f 74 68 65 72 20 56  MMIT and other V
19cbf 4d 73 20 61 72 65 20 77 72 69 74 69 6e 67 0a 20  Ms are writing. 
19cc0 20 20 20 2a 2a 20 72 65 74 75 72 6e 20 61 6e 20     ** return an 
19cc1 65 72 72 6f 72 20 69 6e 64 69 63 61 74 69 6e 67  error indicating
19cc2 20 74 68 61 74 20 74 68 65 20 6f 74 68 65 72 20   that the other 
19cc3 56 4d 73 20 6d 75 73 74 20 63 6f 6d 70 6c 65 74  VMs must complet
19cc4 65 20 66 69 72 73 74 2e 20 0a 20 20 20 20 2a 2f  e first. .    */
19cc5 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53  .    sqlite3SetS
19cc6 74 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73  tring(&p->zErrMs
19cc7 67 2c 20 64 62 2c 20 22 63 61 6e 6e 6f 74 20 63  g, db, "cannot c
19cc8 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74 69 6f  ommit transactio
19cc9 6e 20 2d 20 22 0a 20 20 20 20 20 20 20 20 22 53  n - ".        "S
19cca 51 4c 20 73 74 61 74 65 6d 65 6e 74 73 20 69 6e  QL statements in
19ccb 20 70 72 6f 67 72 65 73 73 22 29 3b 0a 20 20 20   progress");.   
19ccc 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53   rc = SQLITE_BUS
19ccd 59 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 64  Y;.  }else if( d
19cce 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69 74  esiredAutoCommit
19ccf 21 3d 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74  !=db->autoCommit
19cd0 20 29 7b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d   ){.    if( pOp-
19cd1 3e 70 32 20 29 7b 0a 20 20 20 20 20 20 61 73 73  >p2 ){.      ass
19cd2 65 72 74 28 20 64 65 73 69 72 65 64 41 75 74 6f  ert( desiredAuto
19cd3 43 6f 6d 6d 69 74 3d 3d 31 20 29 3b 0a 20 20 20  Commit==1 );.   
19cd4 20 20 20 73 71 6c 69 74 65 33 52 6f 6c 6c 62 61     sqlite3Rollba
19cd5 63 6b 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 20  ckAll(db);.     
19cd6 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20   db->autoCommit 
19cd7 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  = 1;.    }else{.
19cd8 20 20 20 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f        db->autoCo
19cd9 6d 6d 69 74 20 3d 20 64 65 73 69 72 65 64 41 75  mmit = desiredAu
19cda 74 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20  toCommit;.      
19cdb 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 48  if( sqlite3VdbeH
19cdc 61 6c 74 28 70 29 3d 3d 53 51 4c 49 54 45 5f 42  alt(p)==SQLITE_B
19cdd 55 53 59 20 29 7b 0a 20 20 20 20 20 20 20 20 70  USY ){.        p
19cde 2d 3e 70 63 20 3d 20 70 63 3b 0a 20 20 20 20 20  ->pc = pc;.     
19cdf 20 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69     db->autoCommi
19ce0 74 20 3d 20 31 2d 64 65 73 69 72 65 64 41 75 74  t = 1-desiredAut
19ce1 6f 43 6f 6d 6d 69 74 3b 0a 20 20 20 20 20 20 20  oCommit;.       
19ce2 20 70 2d 3e 72 63 20 3d 20 72 63 20 3d 20 53 51   p->rc = rc = SQ
19ce3 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 20 20 20  LITE_BUSY;.     
19ce4 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72 65 74     goto vdbe_ret
19ce5 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
19ce6 20 7d 0a 20 20 20 20 69 66 28 20 70 2d 3e 72 63   }.    if( p->rc
19ce7 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
19ce8 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
19ce9 5f 44 4f 4e 45 3b 0a 20 20 20 20 7d 65 6c 73 65  _DONE;.    }else
19cea 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c  {.      rc = SQL
19ceb 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d  ITE_ERROR;.    }
19cec 0a 20 20 20 20 67 6f 74 6f 20 76 64 62 65 5f 72  .    goto vdbe_r
19ced 65 74 75 72 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a  eturn;.  }else{.
19cee 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
19cef 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
19cf0 2c 20 64 62 2c 0a 20 20 20 20 20 20 20 20 28 21  , db,.        (!
19cf1 64 65 73 69 72 65 64 41 75 74 6f 43 6f 6d 6d 69  desiredAutoCommi
19cf2 74 29 3f 22 63 61 6e 6e 6f 74 20 73 74 61 72 74  t)?"cannot start
19cf3 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 77   a transaction w
19cf4 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61 63 74  ithin a transact
19cf5 69 6f 6e 22 3a 28 0a 20 20 20 20 20 20 20 20 28  ion":(.        (
19cf6 72 6f 6c 6c 62 61 63 6b 29 3f 22 63 61 6e 6e 6f  rollback)?"canno
19cf7 74 20 72 6f 6c 6c 62 61 63 6b 20 2d 20 6e 6f 20  t rollback - no 
19cf8 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61  transaction is a
19cf9 63 74 69 76 65 22 3a 0a 20 20 20 20 20 20 20 20  ctive":.        
19cfa 20 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e             "cann
19cfb 6f 74 20 63 6f 6d 6d 69 74 20 2d 20 6e 6f 20 74  ot commit - no t
19cfc 72 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 61 63  ransaction is ac
19cfd 74 69 76 65 22 29 29 3b 0a 20 20 20 20 20 20 20  tive"));.       
19cfe 20 20 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49    .    rc = SQLI
19cff 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20 20  TE_ERROR;.  }.  
19d00 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
19d01 6f 64 65 3a 20 54 72 61 6e 73 61 63 74 69 6f 6e  ode: Transaction
19d02 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
19d03 2a 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73  ** Begin a trans
19d04 61 63 74 69 6f 6e 2e 20 20 54 68 65 20 74 72 61  action.  The tra
19d05 6e 73 61 63 74 69 6f 6e 20 65 6e 64 73 20 77 68  nsaction ends wh
19d06 65 6e 20 61 20 43 6f 6d 6d 69 74 20 6f 72 20 52  en a Commit or R
19d07 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 6f 70 63 6f 64  ollback.** opcod
19d08 65 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  e is encountered
19d09 2e 20 20 44 65 70 65 6e 64 69 6e 67 20 6f 6e 20  .  Depending on 
19d0a 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54 20  the ON CONFLICT 
19d0b 73 65 74 74 69 6e 67 2c 20 74 68 65 0a 2a 2a 20  setting, the.** 
19d0c 74 72 61 6e 73 61 63 74 69 6f 6e 20 6d 69 67 68  transaction migh
19d0d 74 20 61 6c 73 6f 20 62 65 20 72 6f 6c 6c 65 64  t also be rolled
19d0e 20 62 61 63 6b 20 69 66 20 61 6e 20 65 72 72 6f   back if an erro
19d0f 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
19d10 2e 0a 2a 2a 0a 2a 2a 20 50 31 20 69 73 20 74 68  ..**.** P1 is th
19d11 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  e index of the d
19d12 61 74 61 62 61 73 65 20 66 69 6c 65 20 6f 6e 20  atabase file on 
19d13 77 68 69 63 68 20 74 68 65 20 74 72 61 6e 73 61  which the transa
19d14 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 73 74 61 72  ction is.** star
19d15 74 65 64 2e 20 20 49 6e 64 65 78 20 30 20 69 73  ted.  Index 0 is
19d16 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
19d17 73 65 20 66 69 6c 65 20 61 6e 64 20 69 6e 64 65  se file and inde
19d18 78 20 31 20 69 73 20 74 68 65 0a 2a 2a 20 66 69  x 1 is the.** fi
19d19 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65 6d 70  le used for temp
19d1a 6f 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 49  orary tables.  I
19d1b 6e 64 69 63 65 73 20 6f 66 20 32 20 6f 72 20 6d  ndices of 2 or m
19d1c 6f 72 65 20 61 72 65 20 75 73 65 64 20 66 6f 72  ore are used for
19d1d 0a 2a 2a 20 61 74 74 61 63 68 65 64 20 64 61 74  .** attached dat
19d1e 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66  abases..**.** If
19d1f 20 50 32 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c   P2 is non-zero,
19d20 20 74 68 65 6e 20 61 20 77 72 69 74 65 2d 74 72   then a write-tr
19d21 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 73 74 61  ansaction is sta
19d22 72 74 65 64 2e 20 20 41 20 52 45 53 45 52 56 45  rted.  A RESERVE
19d23 44 20 6c 6f 63 6b 20 69 73 0a 2a 2a 20 6f 62 74  D lock is.** obt
19d24 61 69 6e 65 64 20 6f 6e 20 74 68 65 20 64 61 74  ained on the dat
19d25 61 62 61 73 65 20 66 69 6c 65 20 77 68 65 6e 20  abase file when 
19d26 61 20 77 72 69 74 65 2d 74 72 61 6e 73 61 63 74  a write-transact
19d27 69 6f 6e 20 69 73 20 73 74 61 72 74 65 64 2e 20  ion is started. 
19d28 20 4e 6f 0a 2a 2a 20 6f 74 68 65 72 20 70 72 6f   No.** other pro
19d29 63 65 73 73 20 63 61 6e 20 73 74 61 72 74 20 61  cess can start a
19d2a 6e 6f 74 68 65 72 20 77 72 69 74 65 20 74 72 61  nother write tra
19d2b 6e 73 61 63 74 69 6f 6e 20 77 68 69 6c 65 20 74  nsaction while t
19d2c 68 69 73 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  his transaction 
19d2d 69 73 0a 2a 2a 20 75 6e 64 65 72 77 61 79 2e 20  is.** underway. 
19d2e 20 53 74 61 72 74 69 6e 67 20 61 20 77 72 69 74   Starting a writ
19d2f 65 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 61 6c  e transaction al
19d30 73 6f 20 63 72 65 61 74 65 73 20 61 20 72 6f 6c  so creates a rol
19d31 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 20 41  lback journal. A
19d32 0a 2a 2a 20 77 72 69 74 65 20 74 72 61 6e 73 61  .** write transa
19d33 63 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74  ction must be st
19d34 61 72 74 65 64 20 62 65 66 6f 72 65 20 61 6e 79  arted before any
19d35 20 63 68 61 6e 67 65 73 20 63 61 6e 20 62 65 20   changes can be 
19d36 6d 61 64 65 20 74 6f 20 74 68 65 0a 2a 2a 20 64  made to the.** d
19d37 61 74 61 62 61 73 65 2e 20 20 49 66 20 50 32 20  atabase.  If P2 
19d38 69 73 20 32 20 6f 72 20 67 72 65 61 74 65 72 20  is 2 or greater 
19d39 74 68 65 6e 20 61 6e 20 45 58 43 4c 55 53 49 56  then an EXCLUSIV
19d3a 45 20 6c 6f 63 6b 20 69 73 20 61 6c 73 6f 20 6f  E lock is also o
19d3b 62 74 61 69 6e 65 64 0a 2a 2a 20 6f 6e 20 74 68  btained.** on th
19d3c 65 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  e file..**.** If
19d3d 20 50 32 20 69 73 20 7a 65 72 6f 2c 20 74 68 65   P2 is zero, the
19d3e 6e 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 69 73  n a read-lock is
19d3f 20 6f 62 74 61 69 6e 65 64 20 6f 6e 20 74 68 65   obtained on the
19d40 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
19d41 2a 2f 0a 63 61 73 65 20 4f 50 5f 54 72 61 6e 73  */.case OP_Trans
19d42 61 63 74 69 6f 6e 3a 20 7b 0a 20 20 69 6e 74 20  action: {.  int 
19d43 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 42  i = pOp->p1;.  B
19d44 74 72 65 65 20 2a 70 42 74 3b 0a 0a 20 20 61 73  tree *pBt;..  as
19d45 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
19d46 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73  db->nDb );.  ass
19d47 65 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61  ert( (p->btreeMa
19d48 73 6b 20 26 20 28 31 3c 3c 69 29 29 21 3d 30 20  sk & (1<<i))!=0 
19d49 29 3b 0a 20 20 70 42 74 20 3d 20 64 62 2d 3e 61  );.  pBt = db->a
19d4a 44 62 5b 69 5d 2e 70 42 74 3b 0a 0a 20 20 69 66  Db[i].pBt;..  if
19d4b 28 20 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20  ( pBt ){.    rc 
19d4c 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 42 65  = sqlite3BtreeBe
19d4d 67 69 6e 54 72 61 6e 73 28 70 42 74 2c 20 70 4f  ginTrans(pBt, pO
19d4e 70 2d 3e 70 32 29 3b 0a 20 20 20 20 69 66 28 20  p->p2);.    if( 
19d4f 72 63 3d 3d 53 51 4c 49 54 45 5f 42 55 53 59 20  rc==SQLITE_BUSY 
19d50 29 7b 0a 20 20 20 20 20 20 70 2d 3e 70 63 20 3d  ){.      p->pc =
19d51 20 70 63 3b 0a 20 20 20 20 20 20 70 2d 3e 72 63   pc;.      p->rc
19d52 20 3d 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 42   = rc = SQLITE_B
19d53 55 53 59 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  USY;.      goto 
19d54 76 64 62 65 5f 72 65 74 75 72 6e 3b 0a 20 20 20  vdbe_return;.   
19d55 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
19d56 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 63 21 3d  QLITE_OK && rc!=
19d57 53 51 4c 49 54 45 5f 52 45 41 44 4f 4e 4c 59 20  SQLITE_READONLY 
19d58 2f 2a 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  /* && rc!=SQLITE
19d59 5f 42 55 53 59 20 2a 2f 20 29 7b 0a 20 20 20 20  _BUSY */ ){.    
19d5a 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65    goto abort_due
19d5b 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d  _to_error;.    }
19d5c 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
19d5d 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 65 61 64  ./* Opcode: Read
19d5e 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
19d5f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61 64 20 63  * *.**.** Read c
19d60 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 33 20  ookie number P3 
19d61 66 72 6f 6d 20 64 61 74 61 62 61 73 65 20 50 31  from database P1
19d62 20 61 6e 64 20 77 72 69 74 65 20 69 74 20 69 6e   and write it in
19d63 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
19d64 2a 2a 20 50 33 3d 3d 30 20 69 73 20 74 68 65 20  ** P3==0 is the 
19d65 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20  schema version. 
19d66 20 50 33 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P3==1 is the da
19d67 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a  tabase format..*
19d68 2a 20 50 33 3d 3d 32 20 69 73 20 74 68 65 20 72  * P3==2 is the r
19d69 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72  ecommended pager
19d6a 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64   cache size, and
19d6b 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d   so forth.  P1==
19d6c 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e  0 is.** the main
19d6d 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
19d6e 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
19d6f 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
19d70 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74  ed to store.** t
19d71 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
19d72 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20 69 73 20  .**.** If P1 is 
19d73 6e 65 67 61 74 69 76 65 2c 20 74 68 65 6e 20 74  negative, then t
19d74 68 69 73 20 69 73 20 61 20 72 65 71 75 65 73 74  his is a request
19d75 20 74 6f 20 72 65 61 64 20 74 68 65 20 73 69 7a   to read the siz
19d76 65 20 6f 66 20 61 0a 2a 2a 20 64 61 74 61 62 61  e of a.** databa
19d77 73 65 73 20 66 72 65 65 2d 6c 69 73 74 2e 20 50  ses free-list. P
19d78 33 20 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f  3 must be set to
19d79 20 31 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e   1 in this case.
19d7a 20 54 68 65 20 61 63 74 75 61 6c 0a 2a 2a 20 64   The actual.** d
19d7b 61 74 61 62 61 73 65 20 61 63 63 65 73 73 65 64  atabase accessed
19d7c 20 69 73 20 28 28 50 31 2b 31 29 2a 2d 31 29 2e   is ((P1+1)*-1).
19d7d 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 61 20   For example, a 
19d7e 50 31 20 70 61 72 61 6d 65 74 65 72 20 6f 66 20  P1 parameter of 
19d7f 2d 31 0a 2a 2a 20 63 6f 72 72 65 73 70 6f 6e 64  -1.** correspond
19d80 73 20 74 6f 20 64 61 74 61 62 61 73 65 20 30 20  s to database 0 
19d81 28 22 6d 61 69 6e 22 29 2c 20 61 20 50 31 20 6f  ("main"), a P1 o
19d82 66 20 2d 32 20 69 73 20 64 61 74 61 62 61 73 65  f -2 is database
19d83 20 31 20 28 22 74 65 6d 70 22 29 2e 0a 2a 2a 0a   1 ("temp")..**.
19d84 2a 2a 20 54 68 65 72 65 20 6d 75 73 74 20 62 65  ** There must be
19d85 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20   a read-lock on 
19d86 74 68 65 20 64 61 74 61 62 61 73 65 20 28 65 69  the database (ei
19d87 74 68 65 72 20 61 20 74 72 61 6e 73 61 63 74 69  ther a transacti
19d88 6f 6e 0a 2a 2a 20 6d 75 73 74 20 62 65 20 73 74  on.** must be st
19d89 61 72 74 65 64 20 6f 72 20 74 68 65 72 65 20 6d  arted or there m
19d8a 75 73 74 20 62 65 20 61 6e 20 6f 70 65 6e 20 63  ust be an open c
19d8b 75 72 73 6f 72 29 20 62 65 66 6f 72 65 0a 2a 2a  ursor) before.**
19d8c 20 65 78 65 63 75 74 69 6e 67 20 74 68 69 73 20   executing this 
19d8d 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a  instruction..*/.
19d8e 63 61 73 65 20 4f 50 5f 52 65 61 64 43 6f 6f 6b  case OP_ReadCook
19d8f 69 65 3a 20 7b 20 20 20 20 20 20 20 20 20 20 20  ie: {           
19d90 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72      /* out2-prer
19d91 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20  elease */.  int 
19d92 69 4d 65 74 61 3b 0a 20 20 69 6e 74 20 69 44 62  iMeta;.  int iDb
19d93 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 6e   = pOp->p1;.  in
19d94 74 20 69 43 6f 6f 6b 69 65 20 3d 20 70 4f 70 2d  t iCookie = pOp-
19d95 3e 70 33 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  >p3;..  assert( 
19d96 70 4f 70 2d 3e 70 33 3c 53 51 4c 49 54 45 5f 4e  pOp->p3<SQLITE_N
19d97 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20  _BTREE_META );. 
19d98 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20   if( iDb<0 ){.  
19d99 20 20 69 44 62 20 3d 20 28 2d 31 2a 28 69 44 62    iDb = (-1*(iDb
19d9a 2b 31 29 29 3b 0a 20 20 20 20 69 43 6f 6f 6b 69  +1));.    iCooki
19d9b 65 20 2a 3d 20 2d 31 3b 0a 20 20 7d 0a 20 20 61  e *= -1;.  }.  a
19d9c 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
19d9d 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
19d9e 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61 44    assert( db->aD
19d9f 62 5b 69 44 62 5d 2e 70 42 74 21 3d 30 20 29 3b  b[iDb].pBt!=0 );
19da0 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
19da1 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69  treeMask & (1<<i
19da2 44 62 29 29 21 3d 30 20 29 3b 0a 20 20 2f 2a 20  Db))!=0 );.  /* 
19da3 54 68 65 20 69 6e 64 65 78 69 6e 67 20 6f 66 20  The indexing of 
19da4 6d 65 74 61 20 76 61 6c 75 65 73 20 61 74 20 74  meta values at t
19da5 68 65 20 73 63 68 65 6d 61 20 6c 61 79 65 72 20  he schema layer 
19da6 69 73 20 6f 66 66 20 62 79 20 6f 6e 65 20 66 72  is off by one fr
19da7 6f 6d 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  om.  ** the inde
19da8 78 69 6e 67 20 69 6e 20 74 68 65 20 62 74 72 65  xing in the btre
19da9 65 20 6c 61 79 65 72 2e 20 20 54 68 65 20 62 74  e layer.  The bt
19daa 72 65 65 20 63 6f 6e 73 69 64 65 72 73 20 6d 65  ree considers me
19dab 74 61 5b 30 5d 20 74 6f 0a 20 20 2a 2a 20 62 65  ta[0] to.  ** be
19dac 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 66   the number of f
19dad 72 65 65 20 70 61 67 65 73 20 69 6e 20 74 68 65  ree pages in the
19dae 20 64 61 74 61 62 61 73 65 20 28 61 20 72 65 61   database (a rea
19daf 64 2d 6f 6e 6c 79 20 76 61 6c 75 65 29 0a 20 20  d-only value).  
19db0 2a 2a 20 61 6e 64 20 6d 65 74 61 5b 31 5d 20 74  ** and meta[1] t
19db1 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d 61 20  o be the schema 
19db2 63 6f 6f 6b 69 65 2e 20 20 54 68 65 20 73 63 68  cookie.  The sch
19db3 65 6d 61 20 6c 61 79 65 72 20 63 6f 6e 73 69 64  ema layer consid
19db4 65 72 73 0a 20 20 2a 2a 20 6d 65 74 61 5b 31 5d  ers.  ** meta[1]
19db5 20 74 6f 20 62 65 20 74 68 65 20 73 63 68 65 6d   to be the schem
19db6 61 20 63 6f 6f 6b 69 65 2e 20 20 53 6f 20 77 65  a cookie.  So we
19db7 20 68 61 76 65 20 74 6f 20 73 68 69 66 74 20 74   have to shift t
19db8 68 65 20 69 6e 64 65 78 0a 20 20 2a 2a 20 62 79  he index.  ** by
19db9 20 6f 6e 65 20 69 6e 20 74 68 65 20 66 6f 6c 6c   one in the foll
19dba 6f 77 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 2e  owing statement.
19dbb 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  .  */.  rc = sql
19dbc 69 74 65 33 42 74 72 65 65 47 65 74 4d 65 74 61  ite3BtreeGetMeta
19dbd 28 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42  (db->aDb[iDb].pB
19dbe 74 2c 20 31 20 2b 20 69 43 6f 6f 6b 69 65 2c 20  t, 1 + iCookie, 
19dbf 28 75 33 32 20 2a 29 26 69 4d 65 74 61 29 3b 0a  (u32 *)&iMeta);.
19dc0 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69 4d    pOut->u.i = iM
19dc1 65 74 61 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70  eta;.  MemSetTyp
19dc2 65 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f  eFlag(pOut, MEM_
19dc3 49 6e 74 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  Int);.  break;.}
19dc4 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 65 74  ../* Opcode: Set
19dc5 43 6f 6f 6b 69 65 20 50 31 20 50 32 20 50 33 20  Cookie P1 P2 P3 
19dc6 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20  * *.**.** Write 
19dc7 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
19dc8 65 67 69 73 74 65 72 20 50 33 20 28 69 6e 74 65  egister P3 (inte
19dc9 72 70 72 65 74 65 64 20 61 73 20 61 6e 20 69 6e  rpreted as an in
19dca 74 65 67 65 72 29 0a 2a 2a 20 69 6e 74 6f 20 63  teger).** into c
19dcb 6f 6f 6b 69 65 20 6e 75 6d 62 65 72 20 50 32 20  ookie number P2 
19dcc 6f 66 20 64 61 74 61 62 61 73 65 20 50 31 2e 0a  of database P1..
19dcd 2a 2a 20 50 32 3d 3d 30 20 69 73 20 74 68 65 20  ** P2==0 is the 
19dce 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e 2e 20  schema version. 
19dcf 20 50 32 3d 3d 31 20 69 73 20 74 68 65 20 64 61   P2==1 is the da
19dd0 74 61 62 61 73 65 20 66 6f 72 6d 61 74 2e 0a 2a  tabase format..*
19dd1 2a 20 50 32 3d 3d 32 20 69 73 20 74 68 65 20 72  * P2==2 is the r
19dd2 65 63 6f 6d 6d 65 6e 64 65 64 20 70 61 67 65 72  ecommended pager
19dd3 20 63 61 63 68 65 20 73 69 7a 65 2c 20 61 6e 64   cache size, and
19dd4 20 73 6f 20 66 6f 72 74 68 2e 20 20 50 31 3d 3d   so forth.  P1==
19dd5 30 20 69 73 0a 2a 2a 20 74 68 65 20 6d 61 69 6e  0 is.** the main
19dd6 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 61   database file a
19dd7 6e 64 20 50 31 3d 3d 31 20 69 73 20 74 68 65 20  nd P1==1 is the 
19dd8 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 75 73  database file us
19dd9 65 64 20 74 6f 20 73 74 6f 72 65 0a 2a 2a 20 74  ed to store.** t
19dda 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
19ddb 0a 2a 2a 0a 2a 2a 20 41 20 74 72 61 6e 73 61 63  .**.** A transac
19ddc 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 73 74 61  tion must be sta
19ddd 72 74 65 64 20 62 65 66 6f 72 65 20 65 78 65 63  rted before exec
19dde 75 74 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64  uting this opcod
19ddf 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65  e..*/.case OP_Se
19de0 74 43 6f 6f 6b 69 65 3a 20 7b 20 20 20 20 20 20  tCookie: {      
19de1 20 2f 2a 20 69 6e 33 20 2a 2f 0a 20 20 44 62 20   /* in3 */.  Db 
19de2 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28 20  *pDb;.  assert( 
19de3 70 4f 70 2d 3e 70 32 3c 53 51 4c 49 54 45 5f 4e  pOp->p2<SQLITE_N
19de4 5f 42 54 52 45 45 5f 4d 45 54 41 20 29 3b 0a 20  _BTREE_META );. 
19de5 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 31   assert( pOp->p1
19de6 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70 31 3c 64  >=0 && pOp->p1<d
19de7 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65  b->nDb );.  asse
19de8 72 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73  rt( (p->btreeMas
19de9 6b 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 31 29  k & (1<<pOp->p1)
19dea 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20  )!=0 );.  pDb = 
19deb 26 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e 70 31  &db->aDb[pOp->p1
19dec 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 70 44 62  ];.  assert( pDb
19ded 2d 3e 70 42 74 21 3d 30 20 29 3b 0a 20 20 73 71  ->pBt!=0 );.  sq
19dee 6c 69 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65  lite3VdbeMemInte
19def 67 65 72 69 66 79 28 70 49 6e 33 29 3b 0a 20 20  gerify(pIn3);.  
19df0 2f 2a 20 53 65 65 20 6e 6f 74 65 20 61 62 6f 75  /* See note abou
19df1 74 20 69 6e 64 65 78 20 73 68 69 66 74 69 6e 67  t index shifting
19df2 20 6f 6e 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69   on OP_ReadCooki
19df3 65 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c 69  e */.  rc = sqli
19df4 74 65 33 42 74 72 65 65 55 70 64 61 74 65 4d 65  te3BtreeUpdateMe
19df5 74 61 28 70 44 62 2d 3e 70 42 74 2c 20 31 2b 70  ta(pDb->pBt, 1+p
19df6 4f 70 2d 3e 70 32 2c 20 28 69 6e 74 29 70 49 6e  Op->p2, (int)pIn
19df7 33 2d 3e 75 2e 69 29 3b 0a 20 20 69 66 28 20 70  3->u.i);.  if( p
19df8 4f 70 2d 3e 70 32 3d 3d 30 20 29 7b 0a 20 20 20  Op->p2==0 ){.   
19df9 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 73 63 68   /* When the sch
19dfa 65 6d 61 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67  ema cookie chang
19dfb 65 73 2c 20 72 65 63 6f 72 64 20 74 68 65 20 6e  es, record the n
19dfc 65 77 20 63 6f 6f 6b 69 65 20 69 6e 74 65 72 6e  ew cookie intern
19dfd 61 6c 6c 79 20 2a 2f 0a 20 20 20 20 70 44 62 2d  ally */.    pDb-
19dfe 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  >pSchema->schema
19dff 5f 63 6f 6f 6b 69 65 20 3d 20 70 49 6e 33 2d 3e  _cookie = pIn3->
19e00 75 2e 69 3b 0a 20 20 20 20 64 62 2d 3e 66 6c 61  u.i;.    db->fla
19e01 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49 6e 74  gs |= SQLITE_Int
19e02 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20 7d 65  ernChanges;.  }e
19e03 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e 70 32 3d  lse if( pOp->p2=
19e04 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 52 65 63  =1 ){.    /* Rec
19e05 6f 72 64 20 63 68 61 6e 67 65 73 20 69 6e 20 74  ord changes in t
19e06 68 65 20 66 69 6c 65 20 66 6f 72 6d 61 74 20 2a  he file format *
19e07 2f 0a 20 20 20 20 70 44 62 2d 3e 70 53 63 68 65  /.    pDb->pSche
19e08 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72 6d 61 74 20  ma->file_format 
19e09 3d 20 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 7d  = pIn3->u.i;.  }
19e0a 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70 31 3d 3d  .  if( pOp->p1==
19e0b 31 20 29 7b 0a 20 20 20 20 2f 2a 20 49 6e 76 61  1 ){.    /* Inva
19e0c 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 70 61  lidate all prepa
19e0d 72 65 64 20 73 74 61 74 65 6d 65 6e 74 73 20 77  red statements w
19e0e 68 65 6e 65 76 65 72 20 74 68 65 20 54 45 4d 50  henever the TEMP
19e0f 20 64 61 74 61 62 61 73 65 0a 20 20 20 20 2a 2a   database.    **
19e10 20 73 63 68 65 6d 61 20 69 73 20 63 68 61 6e 67   schema is chang
19e11 65 64 2e 20 20 54 69 63 6b 65 74 20 23 31 36 34  ed.  Ticket #164
19e12 34 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33  4 */.    sqlite3
19e13 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53 74  ExpirePreparedSt
19e14 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20  atements(db);.  
19e15 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
19e16 20 4f 70 63 6f 64 65 3a 20 56 65 72 69 66 79 43   Opcode: VerifyC
19e17 6f 6f 6b 69 65 20 50 31 20 50 32 20 2a 0a 2a 2a  ookie P1 P2 *.**
19e18 0a 2a 2a 20 43 68 65 63 6b 20 74 68 65 20 76 61  .** Check the va
19e19 6c 75 65 20 6f 66 20 67 6c 6f 62 61 6c 20 64 61  lue of global da
19e1a 74 61 62 61 73 65 20 70 61 72 61 6d 65 74 65 72  tabase parameter
19e1b 20 6e 75 6d 62 65 72 20 30 20 28 74 68 65 0a 2a   number 0 (the.*
19e1c 2a 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f 6e  * schema version
19e1d 29 20 61 6e 64 20 6d 61 6b 65 20 73 75 72 65 20  ) and make sure 
19e1e 69 74 20 69 73 20 65 71 75 61 6c 20 74 6f 20 50  it is equal to P
19e1f 32 2e 20 20 0a 2a 2a 20 50 31 20 69 73 20 74 68  2.  .** P1 is th
19e20 65 20 64 61 74 61 62 61 73 65 20 6e 75 6d 62 65  e database numbe
19e21 72 20 77 68 69 63 68 20 69 73 20 30 20 66 6f 72  r which is 0 for
19e22 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
19e23 73 65 20 66 69 6c 65 0a 2a 2a 20 61 6e 64 20 31  se file.** and 1
19e24 20 66 6f 72 20 74 68 65 20 66 69 6c 65 20 68 6f   for the file ho
19e25 6c 64 69 6e 67 20 74 65 6d 70 6f 72 61 72 79 20  lding temporary 
19e26 74 61 62 6c 65 73 20 61 6e 64 20 73 6f 6d 65 20  tables and some 
19e27 68 69 67 68 65 72 20 6e 75 6d 62 65 72 0a 2a 2a  higher number.**
19e28 20 66 6f 72 20 61 75 78 69 6c 69 61 72 79 20 64   for auxiliary d
19e29 61 74 61 62 61 73 65 73 2e 0a 2a 2a 0a 2a 2a 20  atabases..**.** 
19e2a 54 68 65 20 63 6f 6f 6b 69 65 20 63 68 61 6e 67  The cookie chang
19e2b 65 73 20 69 74 73 20 76 61 6c 75 65 20 77 68 65  es its value whe
19e2c 6e 65 76 65 72 20 74 68 65 20 64 61 74 61 62 61  never the databa
19e2d 73 65 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  se schema change
19e2e 73 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 65 72 61  s..** This opera
19e2f 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74 6f 20  tion is used to 
19e30 64 65 74 65 63 74 20 77 68 65 6e 20 74 68 61 74  detect when that
19e31 20 74 68 65 20 63 6f 6f 6b 69 65 20 68 61 73 20   the cookie has 
19e32 63 68 61 6e 67 65 64 0a 2a 2a 20 61 6e 64 20 74  changed.** and t
19e33 68 61 74 20 74 68 65 20 63 75 72 72 65 6e 74 20  hat the current 
19e34 70 72 6f 63 65 73 73 20 6e 65 65 64 73 20 74 6f  process needs to
19e35 20 72 65 72 65 61 64 20 74 68 65 20 73 63 68 65   reread the sche
19e36 6d 61 2e 0a 2a 2a 0a 2a 2a 20 45 69 74 68 65 72  ma..**.** Either
19e37 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6e   a transaction n
19e38 65 65 64 73 20 74 6f 20 68 61 76 65 20 62 65 65  eeds to have bee
19e39 6e 20 73 74 61 72 74 65 64 20 6f 72 20 61 6e 20  n started or an 
19e3a 4f 50 5f 4f 70 65 6e 20 6e 65 65 64 73 0a 2a 2a  OP_Open needs.**
19e3b 20 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20   to be executed 
19e3c 28 74 6f 20 65 73 74 61 62 6c 69 73 68 20 61 20  (to establish a 
19e3d 72 65 61 64 20 6c 6f 63 6b 29 20 62 65 66 6f 72  read lock) befor
19e3e 65 20 74 68 69 73 20 6f 70 63 6f 64 65 20 69 73  e this opcode is
19e3f 0a 2a 2a 20 69 6e 76 6f 6b 65 64 2e 0a 2a 2f 0a  .** invoked..*/.
19e40 63 61 73 65 20 4f 50 5f 56 65 72 69 66 79 43 6f  case OP_VerifyCo
19e41 6f 6b 69 65 3a 20 7b 0a 20 20 69 6e 74 20 69 4d  okie: {.  int iM
19e42 65 74 61 3b 0a 20 20 42 74 72 65 65 20 2a 70 42  eta;.  Btree *pB
19e43 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  t;.  assert( pOp
19e44 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e  ->p1>=0 && pOp->
19e45 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  p1<db->nDb );.  
19e46 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
19e47 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f 70 2d  eMask & (1<<pOp-
19e48 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 70 42  >p1))!=0 );.  pB
19e49 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d  t = db->aDb[pOp-
19e4a 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 69 66 28 20  >p1].pBt;.  if( 
19e4b 70 42 74 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  pBt ){.    rc = 
19e4c 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 4d  sqlite3BtreeGetM
19e4d 65 74 61 28 70 42 74 2c 20 31 2c 20 28 75 33 32  eta(pBt, 1, (u32
19e4e 20 2a 29 26 69 4d 65 74 61 29 3b 0a 20 20 7d 65   *)&iMeta);.  }e
19e4f 6c 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 53 51  lse{.    rc = SQ
19e50 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 69 4d 65  LITE_OK;.    iMe
19e51 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  ta = 0;.  }.  if
19e52 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
19e53 26 26 20 69 4d 65 74 61 21 3d 70 4f 70 2d 3e 70  && iMeta!=pOp->p
19e54 32 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  2 ){.    sqlite3
19e55 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45  DbFree(db, p->zE
19e56 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
19e57 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
19e58 44 62 53 74 72 44 75 70 28 64 62 2c 20 22 64 61  DbStrDup(db, "da
19e59 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 68 61  tabase schema ha
19e5a 73 20 63 68 61 6e 67 65 64 22 29 3b 0a 20 20 20  s changed");.   
19e5b 20 2f 2a 20 49 66 20 74 68 65 20 73 63 68 65 6d   /* If the schem
19e5c 61 2d 63 6f 6f 6b 69 65 20 66 72 6f 6d 20 74 68  a-cookie from th
19e5d 65 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 20  e database file 
19e5e 6d 61 74 63 68 65 73 20 74 68 65 20 63 6f 6f 6b  matches the cook
19e5f 69 65 20 0a 20 20 20 20 2a 2a 20 73 74 6f 72 65  ie .    ** store
19e60 64 20 77 69 74 68 20 74 68 65 20 69 6e 2d 6d 65  d with the in-me
19e61 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74  mory representat
19e62 69 6f 6e 20 6f 66 20 74 68 65 20 73 63 68 65 6d  ion of the schem
19e63 61 2c 20 64 6f 0a 20 20 20 20 2a 2a 20 6e 6f 74  a, do.    ** not
19e64 20 72 65 6c 6f 61 64 20 74 68 65 20 73 63 68 65   reload the sche
19e65 6d 61 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  ma from the data
19e66 62 61 73 65 20 66 69 6c 65 2e 0a 20 20 20 20 2a  base file..    *
19e67 2a 0a 20 20 20 20 2a 2a 20 49 66 20 76 69 72 74  *.    ** If virt
19e68 75 61 6c 2d 74 61 62 6c 65 73 20 61 72 65 20 69  ual-tables are i
19e69 6e 20 75 73 65 2c 20 74 68 69 73 20 69 73 20 6e  n use, this is n
19e6a 6f 74 20 6a 75 73 74 20 61 6e 20 6f 70 74 69 6d  ot just an optim
19e6b 69 7a 61 74 69 6f 6e 2e 0a 20 20 20 20 2a 2a 20  ization..    ** 
19e6c 4f 66 74 65 6e 2c 20 76 2d 74 61 62 6c 65 73 20  Often, v-tables 
19e6d 73 74 6f 72 65 20 74 68 65 69 72 20 64 61 74 61  store their data
19e6e 20 69 6e 20 6f 74 68 65 72 20 53 51 4c 69 74 65   in other SQLite
19e6f 20 74 61 62 6c 65 73 2c 20 77 68 69 63 68 0a 20   tables, which. 
19e70 20 20 20 2a 2a 20 61 72 65 20 71 75 65 72 69 65     ** are querie
19e71 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 78 4e  d from within xN
19e72 65 78 74 28 29 20 61 6e 64 20 6f 74 68 65 72 20  ext() and other 
19e73 76 2d 74 61 62 6c 65 20 6d 65 74 68 6f 64 73 20  v-table methods 
19e74 75 73 69 6e 67 0a 20 20 20 20 2a 2a 20 70 72 65  using.    ** pre
19e75 70 61 72 65 64 20 71 75 65 72 69 65 73 2e 20 49  pared queries. I
19e76 66 20 73 75 63 68 20 61 20 71 75 65 72 79 20 69  f such a query i
19e77 73 20 6f 75 74 2d 6f 66 2d 64 61 74 65 2c 20 77  s out-of-date, w
19e78 65 20 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 6f  e do not want to
19e79 0a 20 20 20 20 2a 2a 20 64 69 73 63 61 72 64 20  .    ** discard 
19e7a 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
19e7b 65 6d 61 2c 20 61 73 20 74 68 65 20 75 73 65 72  ema, as the user
19e7c 20 63 6f 64 65 20 69 6d 70 6c 65 6d 65 6e 74 69   code implementi
19e7d 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 76 2d  ng the.    ** v-
19e7e 74 61 62 6c 65 20 77 6f 75 6c 64 20 68 61 76 65  table would have
19e7f 20 74 6f 20 62 65 20 72 65 61 64 79 20 66 6f 72   to be ready for
19e80 20 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61   the sqlite3_vta
19e81 62 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  b structure itse
19e82 6c 66 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  lf.    ** to be 
19e83 69 6e 76 61 6c 69 64 61 74 65 64 20 77 68 65 6e  invalidated when
19e84 65 76 65 72 20 73 71 6c 69 74 65 33 5f 73 74 65  ever sqlite3_ste
19e85 70 28 29 20 69 73 20 63 61 6c 6c 65 64 20 66 72  p() is called fr
19e86 6f 6d 20 77 69 74 68 69 6e 20 0a 20 20 20 20 2a  om within .    *
19e87 2a 20 61 20 76 2d 74 61 62 6c 65 20 6d 65 74 68  * a v-table meth
19e88 6f 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  od..    */.    i
19e89 66 28 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e  f( db->aDb[pOp->
19e8a 70 31 5d 2e 70 53 63 68 65 6d 61 2d 3e 73 63 68  p1].pSchema->sch
19e8b 65 6d 61 5f 63 6f 6f 6b 69 65 21 3d 69 4d 65 74  ema_cookie!=iMet
19e8c 61 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  a ){.      sqlit
19e8d 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
19e8e 63 68 65 6d 61 28 64 62 2c 20 70 4f 70 2d 3e 70  chema(db, pOp->p
19e8f 31 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73  1);.    }..    s
19e90 71 6c 69 74 65 33 45 78 70 69 72 65 50 72 65 70  qlite3ExpirePrep
19e91 61 72 65 64 53 74 61 74 65 6d 65 6e 74 73 28 64  aredStatements(d
19e92 62 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  b);.    rc = SQL
19e93 49 54 45 5f 53 43 48 45 4d 41 3b 0a 20 20 7d 0a  ITE_SCHEMA;.  }.
19e94 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
19e95 70 63 6f 64 65 3a 20 4f 70 65 6e 52 65 61 64 20  pcode: OpenRead 
19e96 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
19e97 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 72 65 61 64  *.** Open a read
19e98 2d 6f 6e 6c 79 20 63 75 72 73 6f 72 20 66 6f 72  -only cursor for
19e99 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 61   the database ta
19e9a 62 6c 65 20 77 68 6f 73 65 20 72 6f 6f 74 20 70  ble whose root p
19e9b 61 67 65 20 69 73 0a 2a 2a 20 50 32 20 69 6e 20  age is.** P2 in 
19e9c 61 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e  a database file.
19e9d 20 20 54 68 65 20 64 61 74 61 62 61 73 65 20 66    The database f
19e9e 69 6c 65 20 69 73 20 64 65 74 65 72 6d 69 6e 65  ile is determine
19e9f 64 20 62 79 20 50 33 2e 20 0a 2a 2a 20 50 33 3d  d by P3. .** P3=
19ea0 3d 30 20 6d 65 61 6e 73 20 74 68 65 20 6d 61 69  =0 means the mai
19ea1 6e 20 64 61 74 61 62 61 73 65 2c 20 50 33 3d 3d  n database, P3==
19ea2 31 20 6d 65 61 6e 73 20 74 68 65 20 64 61 74 61  1 means the data
19ea3 62 61 73 65 20 75 73 65 64 20 66 6f 72 20 0a 2a  base used for .*
19ea4 2a 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  * temporary tabl
19ea5 65 73 2c 20 61 6e 64 20 50 33 3e 31 20 6d 65 61  es, and P3>1 mea
19ea6 6e 73 20 75 73 65 64 20 74 68 65 20 63 6f 72 72  ns used the corr
19ea7 65 73 70 6f 6e 64 69 6e 67 20 61 74 74 61 63 68  esponding attach
19ea8 65 64 0a 2a 2a 20 64 61 74 61 62 61 73 65 2e 20  ed.** database. 
19ea9 20 47 69 76 65 20 74 68 65 20 6e 65 77 20 63 75   Give the new cu
19eaa 72 73 6f 72 20 61 6e 20 69 64 65 6e 74 69 66 69  rsor an identifi
19eab 65 72 20 6f 66 20 50 31 2e 20 20 54 68 65 20 50  er of P1.  The P
19eac 31 0a 2a 2a 20 76 61 6c 75 65 73 20 6e 65 65 64  1.** values need
19ead 20 6e 6f 74 20 62 65 20 63 6f 6e 74 69 67 75 6f   not be contiguo
19eae 75 73 20 62 75 74 20 61 6c 6c 20 50 31 20 76 61  us but all P1 va
19eaf 6c 75 65 73 20 73 68 6f 75 6c 64 20 62 65 20 73  lues should be s
19eb0 6d 61 6c 6c 20 69 6e 74 65 67 65 72 73 2e 0a 2a  mall integers..*
19eb1 2a 20 49 74 20 69 73 20 61 6e 20 65 72 72 6f 72  * It is an error
19eb2 20 66 6f 72 20 50 31 20 74 6f 20 62 65 20 6e 65   for P1 to be ne
19eb3 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  gative..**.** If
19eb4 20 50 35 21 3d 30 20 74 68 65 6e 20 75 73 65 20   P5!=0 then use 
19eb5 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20 72  the content of r
19eb6 65 67 69 73 74 65 72 20 50 32 20 61 73 20 74 68  egister P2 as th
19eb7 65 20 72 6f 6f 74 20 70 61 67 65 2c 20 6e 6f 74  e root page, not
19eb8 0a 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66  .** the value of
19eb9 20 50 32 20 69 74 73 65 6c 66 2e 0a 2a 2a 0a 2a   P2 itself..**.*
19eba 2a 20 54 68 65 72 65 20 77 69 6c 6c 20 62 65 20  * There will be 
19ebb 61 20 72 65 61 64 20 6c 6f 63 6b 20 6f 6e 20 74  a read lock on t
19ebc 68 65 20 64 61 74 61 62 61 73 65 20 77 68 65 6e  he database when
19ebd 65 76 65 72 20 74 68 65 72 65 20 69 73 20 61 6e  ever there is an
19ebe 0a 2a 2a 20 6f 70 65 6e 20 63 75 72 73 6f 72 2e  .** open cursor.
19ebf 20 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73    If the databas
19ec0 65 20 77 61 73 20 75 6e 6c 6f 63 6b 65 64 20 70  e was unlocked p
19ec1 72 69 6f 72 20 74 6f 20 74 68 69 73 20 69 6e 73  rior to this ins
19ec2 74 72 75 63 74 69 6f 6e 0a 2a 2a 20 74 68 65 6e  truction.** then
19ec3 20 61 20 72 65 61 64 20 6c 6f 63 6b 20 69 73 20   a read lock is 
19ec4 61 63 71 75 69 72 65 64 20 61 73 20 70 61 72 74  acquired as part
19ec5 20 6f 66 20 74 68 69 73 20 69 6e 73 74 72 75 63   of this instruc
19ec6 74 69 6f 6e 2e 20 20 41 20 72 65 61 64 0a 2a 2a  tion.  A read.**
19ec7 20 6c 6f 63 6b 20 61 6c 6c 6f 77 73 20 6f 74 68   lock allows oth
19ec8 65 72 20 70 72 6f 63 65 73 73 65 73 20 74 6f 20  er processes to 
19ec9 72 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73  read the databas
19eca 65 20 62 75 74 20 70 72 6f 68 69 62 69 74 73 0a  e but prohibits.
19ecb 2a 2a 20 61 6e 79 20 6f 74 68 65 72 20 70 72 6f  ** any other pro
19ecc 63 65 73 73 20 66 72 6f 6d 20 6d 6f 64 69 66 79  cess from modify
19ecd 69 6e 67 20 74 68 65 20 64 61 74 61 62 61 73 65  ing the database
19ece 2e 20 20 54 68 65 20 72 65 61 64 20 6c 6f 63 6b  .  The read lock
19ecf 20 69 73 0a 2a 2a 20 72 65 6c 65 61 73 65 64 20   is.** released 
19ed0 77 68 65 6e 20 61 6c 6c 20 63 75 72 73 6f 72 73  when all cursors
19ed1 20 61 72 65 20 63 6c 6f 73 65 64 2e 20 20 49 66   are closed.  If
19ed2 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
19ed3 6e 20 61 74 74 65 6d 70 74 73 0a 2a 2a 20 74 6f  n attempts.** to
19ed4 20 67 65 74 20 61 20 72 65 61 64 20 6c 6f 63 6b   get a read lock
19ed5 20 62 75 74 20 66 61 69 6c 73 2c 20 74 68 65 20   but fails, the 
19ed6 73 63 72 69 70 74 20 74 65 72 6d 69 6e 61 74 65  script terminate
19ed7 73 20 77 69 74 68 20 61 6e 0a 2a 2a 20 53 51 4c  s with an.** SQL
19ed8 49 54 45 5f 42 55 53 59 20 65 72 72 6f 72 20 63  ITE_BUSY error c
19ed9 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50  ode..**.** The P
19eda 34 20 76 61 6c 75 65 20 69 73 20 61 20 70 6f 69  4 value is a poi
19edb 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49 6e 66  nter to a KeyInf
19edc 6f 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  o structure that
19edd 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a 2a 20   defines the.** 
19ede 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f 6c 6c  content and coll
19edf 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 6f  ating sequence o
19ee0 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34 20 69  f indices.  P4 i
19ee1 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72 73 6f  s NULL for curso
19ee2 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 6e  rs.** that are n
19ee3 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 69  ot pointing to i
19ee4 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65  ndices..**.** Se
19ee5 65 20 61 6c 73 6f 20 4f 70 65 6e 57 72 69 74 65  e also OpenWrite
19ee6 2e 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20  ..*/./* Opcode: 
19ee7 4f 70 65 6e 57 72 69 74 65 20 50 31 20 50 32 20  OpenWrite P1 P2 
19ee8 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20 4f  P3 P4 P5.**.** O
19ee9 70 65 6e 20 61 20 72 65 61 64 2f 77 72 69 74 65  pen a read/write
19eea 20 63 75 72 73 6f 72 20 6e 61 6d 65 64 20 50 31   cursor named P1
19eeb 20 6f 6e 20 74 68 65 20 74 61 62 6c 65 20 6f 72   on the table or
19eec 20 69 6e 64 65 78 20 77 68 6f 73 65 20 72 6f 6f   index whose roo
19eed 74 0a 2a 2a 20 70 61 67 65 20 69 73 20 50 32 2e  t.** page is P2.
19eee 20 20 4f 72 20 69 66 20 50 35 21 3d 30 20 75 73    Or if P5!=0 us
19eef 65 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66  e the content of
19ef0 20 72 65 67 69 73 74 65 72 20 50 32 20 74 6f 20   register P2 to 
19ef1 66 69 6e 64 20 74 68 65 0a 2a 2a 20 72 6f 6f 74  find the.** root
19ef2 20 70 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65   page..**.** The
19ef3 20 50 34 20 76 61 6c 75 65 20 69 73 20 61 20 70   P4 value is a p
19ef4 6f 69 6e 74 65 72 20 74 6f 20 61 20 4b 65 79 49  ointer to a KeyI
19ef5 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
19ef6 61 74 20 64 65 66 69 6e 65 73 20 74 68 65 0a 2a  at defines the.*
19ef7 2a 20 63 6f 6e 74 65 6e 74 20 61 6e 64 20 63 6f  * content and co
19ef8 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
19ef9 20 6f 66 20 69 6e 64 69 63 65 73 2e 20 20 50 34   of indices.  P4
19efa 20 69 73 20 4e 55 4c 4c 20 66 6f 72 20 63 75 72   is NULL for cur
19efb 73 6f 72 73 0a 2a 2a 20 74 68 61 74 20 61 72 65  sors.** that are
19efc 20 6e 6f 74 20 70 6f 69 6e 74 69 6e 67 20 74 6f   not pointing to
19efd 20 69 6e 64 69 63 65 73 2e 0a 2a 2a 0a 2a 2a 20   indices..**.** 
19efe 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
19eff 20 77 6f 72 6b 73 20 6a 75 73 74 20 6c 69 6b 65   works just like
19f00 20 4f 70 65 6e 52 65 61 64 20 65 78 63 65 70 74   OpenRead except
19f01 20 74 68 61 74 20 69 74 20 6f 70 65 6e 73 20 74   that it opens t
19f02 68 65 20 63 75 72 73 6f 72 0a 2a 2a 20 69 6e 20  he cursor.** in 
19f03 72 65 61 64 2f 77 72 69 74 65 20 6d 6f 64 65 2e  read/write mode.
19f04 20 20 46 6f 72 20 61 20 67 69 76 65 6e 20 74 61    For a given ta
19f05 62 6c 65 2c 20 74 68 65 72 65 20 63 61 6e 20 62  ble, there can b
19f06 65 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 72 65  e one or more re
19f07 61 64 2d 6f 6e 6c 79 0a 2a 2a 20 63 75 72 73 6f  ad-only.** curso
19f08 72 73 20 6f 72 20 61 20 73 69 6e 67 6c 65 20 72  rs or a single r
19f09 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72  ead/write cursor
19f0a 20 62 75 74 20 6e 6f 74 20 62 6f 74 68 2e 0a 2a   but not both..*
19f0b 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 20 4f 70  *.** See also Op
19f0c 65 6e 52 65 61 64 2e 0a 2a 2f 0a 63 61 73 65 20  enRead..*/.case 
19f0d 4f 50 5f 4f 70 65 6e 52 65 61 64 3a 0a 63 61 73  OP_OpenRead:.cas
19f0e 65 20 4f 50 5f 4f 70 65 6e 57 72 69 74 65 3a 20  e OP_OpenWrite: 
19f0f 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  {.  int i = pOp-
19f10 3e 70 31 3b 0a 20 20 69 6e 74 20 70 32 20 3d 20  >p1;.  int p2 = 
19f11 70 4f 70 2d 3e 70 32 3b 0a 20 20 69 6e 74 20 69  pOp->p2;.  int i
19f12 44 62 20 3d 20 70 4f 70 2d 3e 70 33 3b 0a 20 20  Db = pOp->p3;.  
19f13 69 6e 74 20 77 72 46 6c 61 67 3b 0a 20 20 42 74  int wrFlag;.  Bt
19f14 72 65 65 20 2a 70 58 3b 0a 20 20 56 64 62 65 43  ree *pX;.  VdbeC
19f15 75 72 73 6f 72 20 2a 70 43 75 72 3b 0a 20 20 44  ursor *pCur;.  D
19f16 62 20 2a 70 44 62 3b 0a 20 20 0a 20 20 61 73 73  b *pDb;.  .  ass
19f17 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
19f18 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
19f19 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74 72 65  assert( (p->btre
19f1a 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44 62 29  eMask & (1<<iDb)
19f1b 29 21 3d 30 20 29 3b 0a 20 20 70 44 62 20 3d 20  )!=0 );.  pDb = 
19f1c 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20  &db->aDb[iDb];. 
19f1d 20 70 58 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a   pX = pDb->pBt;.
19f1e 20 20 61 73 73 65 72 74 28 20 70 58 21 3d 30 20    assert( pX!=0 
19f1f 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f 70  );.  if( pOp->op
19f20 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57 72 69  code==OP_OpenWri
19f21 74 65 20 29 7b 0a 20 20 20 20 77 72 46 6c 61 67  te ){.    wrFlag
19f22 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20 70 44   = 1;.    if( pD
19f23 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  b->pSchema->file
19f24 5f 66 6f 72 6d 61 74 20 3c 20 70 2d 3e 6d 69 6e  _format < p->min
19f25 57 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20  WriteFileFormat 
19f26 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6d 69 6e 57  ){.      p->minW
19f27 72 69 74 65 46 69 6c 65 46 6f 72 6d 61 74 20 3d  riteFileFormat =
19f28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
19f29 69 6c 65 5f 66 6f 72 6d 61 74 3b 0a 20 20 20 20  ile_format;.    
19f2a 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77  }.  }else{.    w
19f2b 72 46 6c 61 67 20 3d 20 30 3b 0a 20 20 7d 0a 20  rFlag = 0;.  }. 
19f2c 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 29 7b 0a   if( pOp->p5 ){.
19f2d 20 20 20 20 61 73 73 65 72 74 28 20 70 32 3e 30      assert( p2>0
19f2e 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
19f2f 70 32 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20  p2<=p->nMem );. 
19f30 20 20 20 70 49 6e 32 20 3d 20 26 70 2d 3e 61 4d     pIn2 = &p->aM
19f31 65 6d 5b 70 32 5d 3b 0a 20 20 20 20 73 71 6c 69  em[p2];.    sqli
19f32 74 65 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65  te3VdbeMemIntege
19f33 72 69 66 79 28 70 49 6e 32 29 3b 0a 20 20 20 20  rify(pIn2);.    
19f34 70 32 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b 0a  p2 = pIn2->u.i;.
19f35 20 20 20 20 69 66 28 20 70 32 3c 32 20 29 20 7b      if( p2<2 ) {
19f36 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
19f37 54 45 5f 43 4f 52 52 55 50 54 5f 42 4b 50 54 3b  TE_CORRUPT_BKPT;
19f38 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
19f39 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
19f3a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 73 73 65      }.  }.  asse
19f3b 72 74 28 20 69 3e 3d 30 20 29 3b 0a 20 20 70 43  rt( i>=0 );.  pC
19f3c 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65 43 75 72  ur = allocateCur
19f3d 73 6f 72 28 70 2c 20 69 2c 20 26 70 4f 70 5b 2d  sor(p, i, &pOp[-
19f3e 31 5d 2c 20 69 44 62 2c 20 31 29 3b 0a 20 20 69  1], iDb, 1);.  i
19f3f 66 28 20 70 43 75 72 3d 3d 30 20 29 20 67 6f 74  f( pCur==0 ) got
19f40 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70 43 75 72  o no_mem;.  pCur
19f41 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b 0a 20  ->nullRow = 1;. 
19f42 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
19f43 65 65 43 75 72 73 6f 72 28 70 58 2c 20 70 32 2c  eeCursor(pX, p2,
19f44 20 77 72 46 6c 61 67 2c 20 70 4f 70 2d 3e 70 34   wrFlag, pOp->p4
19f45 2e 70 2c 20 70 43 75 72 2d 3e 70 43 75 72 73 6f  .p, pCur->pCurso
19f46 72 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 70  r);.  if( pOp->p
19f47 34 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46  4type==P4_KEYINF
19f48 4f 20 29 7b 0a 20 20 20 20 70 43 75 72 2d 3e 70  O ){.    pCur->p
19f49 4b 65 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70  KeyInfo = pOp->p
19f4a 34 2e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20  4.pKeyInfo;.    
19f4b 70 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 2d 3e  pCur->pKeyInfo->
19f4c 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62 29  enc = ENC(p->db)
19f4d 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70  ;.  }else{.    p
19f4e 43 75 72 2d 3e 70 4b 65 79 49 6e 66 6f 20 3d 20  Cur->pKeyInfo = 
19f4f 30 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28  0;.  }.  switch(
19f50 20 72 63 20 29 7b 0a 20 20 20 20 63 61 73 65 20   rc ){.    case 
19f51 53 51 4c 49 54 45 5f 42 55 53 59 3a 20 7b 0a 20  SQLITE_BUSY: {. 
19f52 20 20 20 20 20 70 2d 3e 70 63 20 3d 20 70 63 3b       p->pc = pc;
19f53 0a 20 20 20 20 20 20 70 2d 3e 72 63 20 3d 20 72  .      p->rc = r
19f54 63 20 3d 20 53 51 4c 49 54 45 5f 42 55 53 59 3b  c = SQLITE_BUSY;
19f55 0a 20 20 20 20 20 20 67 6f 74 6f 20 76 64 62 65  .      goto vdbe
19f56 5f 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20  _return;.    }. 
19f57 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4f     case SQLITE_O
19f58 4b 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 66  K: {.      int f
19f59 6c 61 67 73 20 3d 20 73 71 6c 69 74 65 33 42 74  lags = sqlite3Bt
19f5a 72 65 65 46 6c 61 67 73 28 70 43 75 72 2d 3e 70  reeFlags(pCur->p
19f5b 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 2f  Cursor);.      /
19f5c 2a 20 53 61 6e 69 74 79 20 63 68 65 63 6b 69 6e  * Sanity checkin
19f5d 67 2e 20 20 4f 6e 6c 79 20 74 68 65 20 6c 6f 77  g.  Only the low
19f5e 65 72 20 66 6f 75 72 20 62 69 74 73 20 6f 66 20  er four bits of 
19f5f 74 68 65 20 66 6c 61 67 73 20 62 79 74 65 20 73  the flags byte s
19f60 68 6f 75 6c 64 0a 20 20 20 20 20 20 2a 2a 20 62  hould.      ** b
19f61 65 20 75 73 65 64 2e 20 20 42 69 74 20 33 20 28  e used.  Bit 3 (
19f62 6d 61 73 6b 20 30 78 30 38 29 20 69 73 20 75 6e  mask 0x08) is un
19f63 70 72 65 64 69 63 74 61 62 6c 65 2e 20 20 54 68  predictable.  Th
19f64 65 20 6c 6f 77 65 72 20 33 20 62 69 74 73 0a 20  e lower 3 bits. 
19f65 20 20 20 20 20 2a 2a 20 28 6d 61 73 6b 20 30 78       ** (mask 0x
19f66 30 37 29 20 73 68 6f 75 6c 64 20 62 65 20 65 69  07) should be ei
19f67 74 68 65 72 20 35 20 28 69 6e 74 6b 65 79 2b 6c  ther 5 (intkey+l
19f68 65 61 66 64 61 74 61 20 66 6f 72 20 74 61 62 6c  eafdata for tabl
19f69 65 73 29 20 6f 72 0a 20 20 20 20 20 20 2a 2a 20  es) or.      ** 
19f6a 32 20 28 7a 65 72 6f 64 61 74 61 20 66 6f 72 20  2 (zerodata for 
19f6b 69 6e 64 69 63 65 73 29 2e 20 20 49 66 20 74 68  indices).  If th
19f6c 65 73 65 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61  ese conditions a
19f6d 72 65 20 6e 6f 74 20 6d 65 74 20 69 74 20 63 61  re not met it ca
19f6e 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 6e 6c 79 20  n.      ** only 
19f6f 6d 65 61 6e 20 74 68 61 74 20 77 65 20 61 72 65  mean that we are
19f70 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 61 20   dealing with a 
19f71 63 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65  corrupt database
19f72 20 66 69 6c 65 0a 20 20 20 20 20 20 2a 2f 0a 20   file.      */. 
19f73 20 20 20 20 20 69 66 28 20 28 66 6c 61 67 73 20       if( (flags 
19f74 26 20 30 78 66 30 29 21 3d 30 20 7c 7c 20 28 28  & 0xf0)!=0 || ((
19f75 66 6c 61 67 73 20 26 20 30 78 30 37 29 21 3d 35  flags & 0x07)!=5
19f76 20 26 26 20 28 66 6c 61 67 73 20 26 20 30 78 30   && (flags & 0x0
19f77 37 29 21 3d 32 29 20 29 7b 0a 20 20 20 20 20 20  7)!=2) ){.      
19f78 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
19f79 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
19f7a 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
19f7b 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
19f7c 20 20 20 7d 0a 20 20 20 20 20 20 70 43 75 72 2d     }.      pCur-
19f7d 3e 69 73 54 61 62 6c 65 20 3d 20 28 66 6c 61 67  >isTable = (flag
19f7e 73 20 26 20 42 54 52 45 45 5f 49 4e 54 4b 45 59  s & BTREE_INTKEY
19f7f 29 21 3d 30 3b 0a 20 20 20 20 20 20 70 43 75 72  )!=0;.      pCur
19f80 2d 3e 69 73 49 6e 64 65 78 20 3d 20 28 66 6c 61  ->isIndex = (fla
19f81 67 73 20 26 20 42 54 52 45 45 5f 5a 45 52 4f 44  gs & BTREE_ZEROD
19f82 41 54 41 29 21 3d 30 3b 0a 20 20 20 20 20 20 2f  ATA)!=0;.      /
19f83 2a 20 49 66 20 50 34 3d 3d 30 20 69 74 20 6d 65  * If P4==0 it me
19f84 61 6e 73 20 77 65 20 61 72 65 20 65 78 70 65 63  ans we are expec
19f85 74 65 64 20 74 6f 20 6f 70 65 6e 20 61 20 74 61  ted to open a ta
19f86 62 6c 65 2e 20 20 49 66 20 50 34 21 3d 30 20 74  ble.  If P4!=0 t
19f87 68 65 6e 0a 20 20 20 20 20 20 2a 2a 20 77 65 20  hen.      ** we 
19f88 65 78 70 65 63 74 20 74 6f 20 62 65 20 6f 70 65  expect to be ope
19f89 6e 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 20 20  ning an index.  
19f8a 49 66 20 74 68 69 73 20 69 73 20 6e 6f 74 20 77  If this is not w
19f8b 68 61 74 20 68 61 70 70 65 6e 65 64 2c 0a 20 20  hat happened,.  
19f8c 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20      ** then the 
19f8d 64 61 74 61 62 61 73 65 20 69 73 20 63 6f 72 72  database is corr
19f8e 75 70 74 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  upt.      */.   
19f8f 20 20 20 69 66 28 20 28 70 43 75 72 2d 3e 69 73     if( (pCur->is
19f90 54 61 62 6c 65 20 26 26 20 70 4f 70 2d 3e 70 34  Table && pOp->p4
19f91 74 79 70 65 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f  type==P4_KEYINFO
19f92 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 43 75  ).       || (pCu
19f93 72 2d 3e 69 73 49 6e 64 65 78 20 26 26 20 70 4f  r->isIndex && pO
19f94 70 2d 3e 70 34 74 79 70 65 21 3d 50 34 5f 4b 45  p->p4type!=P4_KE
19f95 59 49 4e 46 4f 29 20 29 7b 0a 20 20 20 20 20 20  YINFO) ){.      
19f96 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 43 4f    rc = SQLITE_CO
19f97 52 52 55 50 54 5f 42 4b 50 54 3b 0a 20 20 20 20  RRUPT_BKPT;.    
19f98 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64      goto abort_d
19f99 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
19f9a 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b     }.      break
19f9b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
19f9c 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 3a 20 7b   SQLITE_EMPTY: {
19f9d 0a 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 54  .      pCur->isT
19f9e 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 34 74 79  able = pOp->p4ty
19f9f 70 65 21 3d 50 34 5f 4b 45 59 49 4e 46 4f 3b 0a  pe!=P4_KEYINFO;.
19fa0 20 20 20 20 20 20 70 43 75 72 2d 3e 69 73 49 6e        pCur->isIn
19fa1 64 65 78 20 3d 20 21 70 43 75 72 2d 3e 69 73 54  dex = !pCur->isT
19fa2 61 62 6c 65 3b 0a 20 20 20 20 20 20 70 43 75 72  able;.      pCur
19fa3 2d 3e 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20  ->pCursor = 0;. 
19fa4 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
19fa5 5f 4f 4b 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  _OK;.      break
19fa6 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66 61  ;.    }.    defa
19fa7 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 67 6f 74  ult: {.      got
19fa8 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65  o abort_due_to_e
19fa9 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rror;.    }.  }.
19faa 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f    break;.}../* O
19fab 70 63 6f 64 65 3a 20 4f 70 65 6e 45 70 68 65 6d  pcode: OpenEphem
19fac 65 72 61 6c 20 50 31 20 50 32 20 2a 20 50 34 20  eral P1 P2 * P4 
19fad 2a 0a 2a 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e  *.**.** Open a n
19fae 65 77 20 63 75 72 73 6f 72 20 50 31 20 74 6f 20  ew cursor P1 to 
19faf 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62 6c  a transient tabl
19fb0 65 2e 0a 2a 2a 20 54 68 65 20 63 75 72 73 6f 72  e..** The cursor
19fb1 20 69 73 20 61 6c 77 61 79 73 20 6f 70 65 6e 65   is always opene
19fb2 64 20 72 65 61 64 2f 77 72 69 74 65 20 65 76 65  d read/write eve
19fb3 6e 20 69 66 20 0a 2a 2a 20 74 68 65 20 6d 61 69  n if .** the mai
19fb4 6e 20 64 61 74 61 62 61 73 65 20 69 73 20 72 65  n database is re
19fb5 61 64 2d 6f 6e 6c 79 2e 20 20 54 68 65 20 74 72  ad-only.  The tr
19fb6 61 6e 73 69 65 6e 74 20 6f 72 20 76 69 72 74 75  ansient or virtu
19fb7 61 6c 0a 2a 2a 20 74 61 62 6c 65 20 69 73 20 64  al.** table is d
19fb8 65 6c 65 74 65 64 20 61 75 74 6f 6d 61 74 69 63  eleted automatic
19fb9 61 6c 6c 79 20 77 68 65 6e 20 74 68 65 20 63 75  ally when the cu
19fba 72 73 6f 72 20 69 73 20 63 6c 6f 73 65 64 2e 0a  rsor is closed..
19fbb 2a 2a 0a 2a 2a 20 50 32 20 69 73 20 74 68 65 20  **.** P2 is the 
19fbc 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
19fbd 73 20 69 6e 20 74 68 65 20 76 69 72 74 75 61 6c  s in the virtual
19fbe 20 74 61 62 6c 65 2e 0a 2a 2a 20 54 68 65 20 63   table..** The c
19fbf 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 74 6f 20  ursor points to 
19fc0 61 20 42 54 72 65 65 20 74 61 62 6c 65 20 69 66  a BTree table if
19fc1 20 50 34 3d 3d 30 20 61 6e 64 20 74 6f 20 61 20   P4==0 and to a 
19fc2 42 54 72 65 65 20 69 6e 64 65 78 0a 2a 2a 20 69  BTree index.** i
19fc3 66 20 50 34 20 69 73 20 6e 6f 74 20 30 2e 20 20  f P4 is not 0.  
19fc4 49 66 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c  If P4 is not NUL
19fc5 4c 2c 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20  L, it points to 
19fc6 61 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  a KeyInfo struct
19fc7 75 72 65 0a 2a 2a 20 74 68 61 74 20 64 65 66 69  ure.** that defi
19fc8 6e 65 73 20 74 68 65 20 66 6f 72 6d 61 74 20 6f  nes the format o
19fc9 66 20 6b 65 79 73 20 69 6e 20 74 68 65 20 69 6e  f keys in the in
19fca 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  dex..**.** This 
19fcb 6f 70 63 6f 64 65 20 77 61 73 20 6f 6e 63 65 20  opcode was once 
19fcc 63 61 6c 6c 65 64 20 4f 70 65 6e 54 65 6d 70 2e  called OpenTemp.
19fcd 20 20 42 75 74 20 74 68 61 74 20 63 72 65 61 74    But that creat
19fce 65 64 0a 2a 2a 20 63 6f 6e 66 75 73 69 6f 6e 20  ed.** confusion 
19fcf 62 65 63 61 75 73 65 20 74 68 65 20 74 65 72 6d  because the term
19fd0 20 22 74 65 6d 70 20 74 61 62 6c 65 22 2c 20 6d   "temp table", m
19fd1 69 67 68 74 20 72 65 66 65 72 20 65 69 74 68 65  ight refer eithe
19fd2 72 0a 2a 2a 20 74 6f 20 61 20 54 45 4d 50 20 74  r.** to a TEMP t
19fd3 61 62 6c 65 20 61 74 20 74 68 65 20 53 51 4c 20  able at the SQL 
19fd4 6c 65 76 65 6c 2c 20 6f 72 20 74 6f 20 61 20 74  level, or to a t
19fd5 61 62 6c 65 20 6f 70 65 6e 65 64 20 62 79 0a 2a  able opened by.*
19fd6 2a 20 74 68 69 73 20 6f 70 63 6f 64 65 2e 20 20  * this opcode.  
19fd7 54 68 65 6e 20 74 68 69 73 20 6f 70 63 6f 64 65  Then this opcode
19fd8 20 77 61 73 20 63 61 6c 6c 20 4f 70 65 6e 56 69   was call OpenVi
19fd9 72 74 75 61 6c 2e 20 20 42 75 74 0a 2a 2a 20 74  rtual.  But.** t
19fda 68 61 74 20 63 72 65 61 74 65 64 20 63 6f 6e 66  hat created conf
19fdb 75 73 69 6f 6e 20 77 69 74 68 20 74 68 65 20 77  usion with the w
19fdc 68 6f 6c 65 20 76 69 72 74 75 61 6c 2d 74 61 62  hole virtual-tab
19fdd 6c 65 20 69 64 65 61 2e 0a 2a 2f 0a 63 61 73 65  le idea..*/.case
19fde 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
19fdf 6c 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70  l: {.  int i = p
19fe0 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75  Op->p1;.  VdbeCu
19fe1 72 73 6f 72 20 2a 70 43 78 3b 0a 20 20 73 74 61  rsor *pCx;.  sta
19fe2 74 69 63 20 63 6f 6e 73 74 20 69 6e 74 20 6f 70  tic const int op
19fe3 65 6e 46 6c 61 67 73 20 3d 20 0a 20 20 20 20 20  enFlags = .     
19fe4 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41   SQLITE_OPEN_REA
19fe5 44 57 52 49 54 45 20 7c 0a 20 20 20 20 20 20 53  DWRITE |.      S
19fe6 51 4c 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54  QLITE_OPEN_CREAT
19fe7 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
19fe8 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56 45 20  _OPEN_EXCLUSIVE 
19fe9 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f  |.      SQLITE_O
19fea 50 45 4e 5f 44 45 4c 45 54 45 4f 4e 43 4c 4f 53  PEN_DELETEONCLOS
19feb 45 20 7c 0a 20 20 20 20 20 20 53 51 4c 49 54 45  E |.      SQLITE
19fec 5f 4f 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f  _OPEN_TRANSIENT_
19fed 44 42 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  DB;..  assert( i
19fee 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20 61  >=0 );.  pCx = a
19fef 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70 2c  llocateCursor(p,
19ff0 20 69 2c 20 70 4f 70 2c 20 2d 31 2c 20 31 29 3b   i, pOp, -1, 1);
19ff1 0a 20 20 69 66 28 20 70 43 78 3d 3d 30 20 29 20  .  if( pCx==0 ) 
19ff2 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 70  goto no_mem;.  p
19ff3 43 78 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 31 3b  Cx->nullRow = 1;
19ff4 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
19ff5 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
19ff6 30 2c 20 31 2c 20 53 51 4c 49 54 45 5f 44 45 46  0, 1, SQLITE_DEF
19ff7 41 55 4c 54 5f 54 45 4d 50 5f 43 41 43 48 45 5f  AULT_TEMP_CACHE_
19ff8 53 49 5a 45 2c 20 6f 70 65 6e 46 6c 61 67 73 2c  SIZE, openFlags,
19ff9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
19ffa 20 20 20 20 20 20 20 20 20 20 20 20 26 70 43 78              &pCx
19ffb 2d 3e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  ->pBt);.  if( rc
19ffc 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
19ffd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
19ffe 74 72 65 65 42 65 67 69 6e 54 72 61 6e 73 28 70  treeBeginTrans(p
19fff 43 78 2d 3e 70 42 74 2c 20 31 29 3b 0a 20 20 7d  Cx->pBt, 1);.  }
1a000 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1a001 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 2f 2a 20 49  E_OK ){.    /* I
1a002 66 20 61 20 74 72 61 6e 73 69 65 6e 74 20 69 6e  f a transient in
1a003 64 65 78 20 69 73 20 72 65 71 75 69 72 65 64 2c  dex is required,
1a004 20 63 72 65 61 74 65 20 69 74 20 62 79 20 63 61   create it by ca
1a005 6c 6c 69 6e 67 0a 20 20 20 20 2a 2a 20 73 71 6c  lling.    ** sql
1a006 69 74 65 33 42 74 72 65 65 43 72 65 61 74 65 54  ite3BtreeCreateT
1a007 61 62 6c 65 28 29 20 77 69 74 68 20 74 68 65 20  able() with the 
1a008 42 54 52 45 45 5f 5a 45 52 4f 44 41 54 41 20 66  BTREE_ZERODATA f
1a009 6c 61 67 20 62 65 66 6f 72 65 0a 20 20 20 20 2a  lag before.    *
1a00a 2a 20 6f 70 65 6e 69 6e 67 20 69 74 2e 20 49 66  * opening it. If
1a00b 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62   a transient tab
1a00c 6c 65 20 69 73 20 72 65 71 75 69 72 65 64 2c 20  le is required, 
1a00d 6a 75 73 74 20 75 73 65 20 74 68 65 0a 20 20 20  just use the.   
1a00e 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   ** automaticall
1a00f 79 20 63 72 65 61 74 65 64 20 74 61 62 6c 65 20  y created table 
1a010 77 69 74 68 20 72 6f 6f 74 2d 70 61 67 65 20 31  with root-page 1
1a011 20 28 61 6e 20 49 4e 54 4b 45 59 20 74 61 62 6c   (an INTKEY tabl
1a012 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  e)..    */.    i
1a013 66 28 20 70 4f 70 2d 3e 70 34 2e 70 4b 65 79 49  f( pOp->p4.pKeyI
1a014 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  nfo ){.      int
1a015 20 70 67 6e 6f 3b 0a 20 20 20 20 20 20 61 73 73   pgno;.      ass
1a016 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1a017 3d 3d 50 34 5f 4b 45 59 49 4e 46 4f 20 29 3b 0a  ==P4_KEYINFO );.
1a018 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
1a019 65 33 42 74 72 65 65 43 72 65 61 74 65 54 61 62  e3BtreeCreateTab
1a01a 6c 65 28 70 43 78 2d 3e 70 42 74 2c 20 26 70 67  le(pCx->pBt, &pg
1a01b 6e 6f 2c 20 42 54 52 45 45 5f 5a 45 52 4f 44 41  no, BTREE_ZERODA
1a01c 54 41 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20  TA); .      if( 
1a01d 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
1a01e 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1a01f 20 70 67 6e 6f 3d 3d 4d 41 53 54 45 52 5f 52 4f   pgno==MASTER_RO
1a020 4f 54 2b 31 20 29 3b 0a 20 20 20 20 20 20 20 20  OT+1 );.        
1a021 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1a022 65 43 75 72 73 6f 72 28 70 43 78 2d 3e 70 42 74  eCursor(pCx->pBt
1a023 2c 20 70 67 6e 6f 2c 20 31 2c 20 0a 20 20 20 20  , pgno, 1, .    
1a024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a025 20 20 20 20 20 20 20 20 20 20 20 20 28 4b 65 79              (Key
1a026 49 6e 66 6f 2a 29 70 4f 70 2d 3e 70 34 2e 7a 2c  Info*)pOp->p4.z,
1a027 20 70 43 78 2d 3e 70 43 75 72 73 6f 72 29 3b 0a   pCx->pCursor);.
1a028 20 20 20 20 20 20 20 20 70 43 78 2d 3e 70 4b 65          pCx->pKe
1a029 79 49 6e 66 6f 20 3d 20 70 4f 70 2d 3e 70 34 2e  yInfo = pOp->p4.
1a02a 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pKeyInfo;.      
1a02b 20 20 70 43 78 2d 3e 70 4b 65 79 49 6e 66 6f 2d    pCx->pKeyInfo-
1a02c 3e 65 6e 63 20 3d 20 45 4e 43 28 70 2d 3e 64 62  >enc = ENC(p->db
1a02d 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1a02e 20 70 43 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20   pCx->isTable = 
1a02f 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  0;.    }else{.  
1a030 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1a031 42 74 72 65 65 43 75 72 73 6f 72 28 70 43 78 2d  BtreeCursor(pCx-
1a032 3e 70 42 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f  >pBt, MASTER_ROO
1a033 54 2c 20 31 2c 20 30 2c 20 70 43 78 2d 3e 70 43  T, 1, 0, pCx->pC
1a034 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 70 43  ursor);.      pC
1a035 78 2d 3e 69 73 54 61 62 6c 65 20 3d 20 31 3b 0a  x->isTable = 1;.
1a036 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 43 78 2d      }.  }.  pCx-
1a037 3e 69 73 49 6e 64 65 78 20 3d 20 21 70 43 78 2d  >isIndex = !pCx-
1a038 3e 69 73 54 61 62 6c 65 3b 0a 20 20 62 72 65 61  >isTable;.  brea
1a039 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a03a 20 4f 70 65 6e 50 73 65 75 64 6f 20 50 31 20 50   OpenPseudo P1 P
1a03b 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 4f 70  2 * * *.**.** Op
1a03c 65 6e 20 61 20 6e 65 77 20 63 75 72 73 6f 72 20  en a new cursor 
1a03d 74 68 61 74 20 70 6f 69 6e 74 73 20 74 6f 20 61  that points to a
1a03e 20 66 61 6b 65 20 74 61 62 6c 65 20 74 68 61 74   fake table that
1a03f 20 63 6f 6e 74 61 69 6e 73 20 61 20 73 69 6e 67   contains a sing
1a040 6c 65 0a 2a 2a 20 72 6f 77 20 6f 66 20 64 61 74  le.** row of dat
1a041 61 2e 20 20 41 6e 79 20 61 74 74 65 6d 70 74 20  a.  Any attempt 
1a042 74 6f 20 77 72 69 74 65 20 61 20 73 65 63 6f 6e  to write a secon
1a043 64 20 72 6f 77 20 6f 66 20 64 61 74 61 20 63 61  d row of data ca
1a044 75 73 65 73 20 74 68 65 0a 2a 2a 20 66 69 72 73  uses the.** firs
1a045 74 20 72 6f 77 20 74 6f 20 62 65 20 64 65 6c 65  t row to be dele
1a046 74 65 64 2e 20 20 41 6c 6c 20 64 61 74 61 20 69  ted.  All data i
1a047 73 20 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74  s deleted when t
1a048 68 65 20 63 75 72 73 6f 72 20 69 73 0a 2a 2a 20  he cursor is.** 
1a049 63 6c 6f 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 41 20  closed..**.** A 
1a04a 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 63 72 65  pseudo-table cre
1a04b 61 74 65 64 20 62 79 20 74 68 69 73 20 6f 70 63  ated by this opc
1a04c 6f 64 65 20 69 73 20 75 73 65 66 75 6c 20 66 6f  ode is useful fo
1a04d 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 0a 2a 2a  r holding the.**
1a04e 20 4e 45 57 20 6f 72 20 4f 4c 44 20 74 61 62 6c   NEW or OLD tabl
1a04f 65 73 20 69 6e 20 61 20 74 72 69 67 67 65 72 2e  es in a trigger.
1a050 20 20 41 6c 73 6f 20 75 73 65 64 20 74 6f 20 68    Also used to h
1a051 6f 6c 64 20 74 68 65 20 61 20 73 69 6e 67 6c 65  old the a single
1a052 0a 2a 2a 20 72 6f 77 20 6f 75 74 70 75 74 20 66  .** row output f
1a053 72 6f 6d 20 74 68 65 20 73 6f 72 74 65 72 20 73  rom the sorter s
1a054 6f 20 74 68 61 74 20 74 68 65 20 72 6f 77 20 63  o that the row c
1a055 61 6e 20 62 65 20 64 65 63 6f 6d 70 6f 73 65 64  an be decomposed
1a056 20 69 6e 74 6f 0a 2a 2a 20 69 6e 64 69 76 69 64   into.** individ
1a057 75 61 6c 20 63 6f 6c 75 6d 6e 73 20 75 73 69 6e  ual columns usin
1a058 67 20 74 68 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20  g the OP_Column 
1a059 6f 70 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 57 68  opcode..**.** Wh
1a05a 65 6e 20 4f 50 5f 49 6e 73 65 72 74 20 69 73 20  en OP_Insert is 
1a05b 65 78 65 63 75 74 65 64 20 74 6f 20 69 6e 73 65  executed to inse
1a05c 72 74 20 61 20 72 6f 77 20 69 6e 20 74 6f 20 74  rt a row in to t
1a05d 68 65 20 70 73 65 75 64 6f 20 74 61 62 6c 65 2c  he pseudo table,
1a05e 0a 2a 2a 20 74 68 65 20 70 73 65 75 64 6f 2d 74  .** the pseudo-t
1a05f 61 62 6c 65 20 63 75 72 73 6f 72 20 6d 61 79 20  able cursor may 
1a060 6f 72 20 6d 61 79 20 6e 6f 74 20 6d 61 6b 65 20  or may not make 
1a061 69 74 27 73 20 6f 77 6e 20 63 6f 70 79 20 6f 66  it's own copy of
1a062 20 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c   the.** original
1a063 20 72 6f 77 20 64 61 74 61 2e 20 49 66 20 50 32   row data. If P2
1a064 20 69 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20   is 0, then the 
1a065 70 73 65 75 64 6f 2d 74 61 62 6c 65 20 77 69 6c  pseudo-table wil
1a066 6c 20 63 6f 70 79 20 74 68 65 0a 2a 2a 20 6f 72  l copy the.** or
1a067 69 67 69 6e 61 6c 20 72 6f 77 20 64 61 74 61 2e  iginal row data.
1a068 20 4f 74 68 65 72 77 69 73 65 2c 20 61 20 70 6f   Otherwise, a po
1a069 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6f 72 69  inter to the ori
1a06a 67 69 6e 61 6c 20 6d 65 6d 6f 72 79 20 63 65 6c  ginal memory cel
1a06b 6c 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64 2e 20  l.** is stored. 
1a06c 49 6e 20 74 68 69 73 20 63 61 73 65 2c 20 74 68  In this case, th
1a06d 65 20 76 64 62 65 20 70 72 6f 67 72 61 6d 20 6d  e vdbe program m
1a06e 75 73 74 20 65 6e 73 75 72 65 20 74 68 61 74 20  ust ensure that 
1a06f 74 68 65 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63  the .** memory c
1a070 65 6c 6c 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ell containing t
1a071 68 65 20 72 6f 77 20 64 61 74 61 20 69 73 20 6e  he row data is n
1a072 6f 74 20 6f 76 65 72 77 72 69 74 74 65 6e 20 75  ot overwritten u
1a073 6e 74 69 6c 20 74 68 65 0a 2a 2a 20 70 73 65 75  ntil the.** pseu
1a074 64 6f 20 74 61 62 6c 65 20 69 73 20 63 6c 6f 73  do table is clos
1a075 65 64 20 28 6f 72 20 61 20 6e 65 77 20 72 6f 77  ed (or a new row
1a076 20 69 73 20 69 6e 73 65 72 74 65 64 20 69 6e 74   is inserted int
1a077 6f 20 69 74 29 2e 0a 2a 2f 0a 63 61 73 65 20 4f  o it)..*/.case O
1a078 50 5f 4f 70 65 6e 50 73 65 75 64 6f 3a 20 7b 0a  P_OpenPseudo: {.
1a079 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1a07a 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1a07b 2a 70 43 78 3b 0a 20 20 61 73 73 65 72 74 28 20  *pCx;.  assert( 
1a07c 69 3e 3d 30 20 29 3b 0a 20 20 70 43 78 20 3d 20  i>=0 );.  pCx = 
1a07d 61 6c 6c 6f 63 61 74 65 43 75 72 73 6f 72 28 70  allocateCursor(p
1a07e 2c 20 69 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 2d  , i, &pOp[-1], -
1a07f 31 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 78  1, 0);.  if( pCx
1a080 3d 3d 30 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65  ==0 ) goto no_me
1a081 6d 3b 0a 20 20 70 43 78 2d 3e 6e 75 6c 6c 52 6f  m;.  pCx->nullRo
1a082 77 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 70 73  w = 1;.  pCx->ps
1a083 65 75 64 6f 54 61 62 6c 65 20 3d 20 31 3b 0a 20  eudoTable = 1;. 
1a084 20 70 43 78 2d 3e 65 70 68 65 6d 50 73 65 75 64   pCx->ephemPseud
1a085 6f 54 61 62 6c 65 20 3d 20 70 4f 70 2d 3e 70 32  oTable = pOp->p2
1a086 3b 0a 20 20 70 43 78 2d 3e 69 73 54 61 62 6c 65  ;.  pCx->isTable
1a087 20 3d 20 31 3b 0a 20 20 70 43 78 2d 3e 69 73 49   = 1;.  pCx->isI
1a088 6e 64 65 78 20 3d 20 30 3b 0a 20 20 62 72 65 61  ndex = 0;.  brea
1a089 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a  k;.}../* Opcode:
1a08a 20 43 6c 6f 73 65 20 50 31 20 2a 20 2a 20 2a 20   Close P1 * * * 
1a08b 2a 0a 2a 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20  *.**.** Close a 
1a08c 63 75 72 73 6f 72 20 70 72 65 76 69 6f 75 73 6c  cursor previousl
1a08d 79 20 6f 70 65 6e 65 64 20 61 73 20 50 31 2e 20  y opened as P1. 
1a08e 20 49 66 20 50 31 20 69 73 20 6e 6f 74 0a 2a 2a   If P1 is not.**
1a08f 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 2c   currently open,
1a090 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1a091 6e 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  n is a no-op..*/
1a092 0a 63 61 73 65 20 4f 50 5f 43 6c 6f 73 65 3a 20  .case OP_Close: 
1a093 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d  {.  int i = pOp-
1a094 3e 70 31 3b 0a 20 20 61 73 73 65 72 74 28 20 69  >p1;.  assert( i
1a095 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72  >=0 && i<p->nCur
1a096 73 6f 72 20 29 3b 0a 20 20 73 71 6c 69 74 65 33  sor );.  sqlite3
1a097 56 64 62 65 46 72 65 65 43 75 72 73 6f 72 28 70  VdbeFreeCursor(p
1a098 2c 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29 3b 0a  , p->apCsr[i]);.
1a099 20 20 70 2d 3e 61 70 43 73 72 5b 69 5d 20 3d 20    p->apCsr[i] = 
1a09a 30 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  0;.  break;.}../
1a09b 2a 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 47 65  * Opcode: MoveGe
1a09c 20 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a   P1 P2 P3 P4 *.*
1a09d 2a 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50  *.** If cursor P
1a09e 31 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53  1 refers to an S
1a09f 51 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65  QL table (B-Tree
1a0a0 20 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67   that uses integ
1a0a1 65 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73  er keys), .** us
1a0a2 65 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61  e the integer va
1a0a3 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20  lue in register 
1a0a4 50 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20  P3 as a key. If 
1a0a5 63 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73  cursor P1 refers
1a0a6 20 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69   .** to an SQL i
1a0a7 6e 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73  ndex, then P3 is
1a0a8 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e   the first in an
1a0a9 20 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67   array of P4 reg
1a0aa 69 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20  isters .** that 
1a0ab 61 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75  are used as an u
1a0ac 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65  npacked index ke
1a0ad 79 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69  y. .**.** Reposi
1a0ae 74 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73  tion cursor P1 s
1a0af 6f 20 74 68 61 74 20 20 69 74 20 70 6f 69 6e 74  o that  it point
1a0b0 73 20 74 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73  s to the smalles
1a0b1 74 20 65 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a  t entry that .**
1a0b2 20 69 73 20 67 72 65 61 74 65 72 20 74 68 61 6e   is greater than
1a0b3 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1a0b4 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20 74   key value. If t
1a0b5 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63 6f  here are no reco
1a0b6 72 64 73 20 0a 2a 2a 20 67 72 65 61 74 65 72 20  rds .** greater 
1a0b7 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
1a0b8 20 74 68 65 20 6b 65 79 20 61 6e 64 20 50 32 20   the key and P2 
1a0b9 69 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65  is not zero, the
1a0ba 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a  n jump to P2..**
1a0bb 0a 2a 2a 20 41 20 73 70 65 63 69 61 6c 20 66 65  .** A special fe
1a0bc 61 74 75 72 65 20 6f 66 20 74 68 69 73 20 6f 70  ature of this op
1a0bd 63 6f 64 65 20 28 61 6e 64 20 64 69 66 66 65 72  code (and differ
1a0be 65 6e 74 20 66 72 6f 6d 20 74 68 65 0a 2a 2a 20  ent from the.** 
1a0bf 72 65 6c 61 74 65 64 20 4f 50 5f 4d 6f 76 65 47  related OP_MoveG
1a0c0 74 2c 20 4f 50 5f 4d 6f 76 65 4c 74 2c 20 61 6e  t, OP_MoveLt, an
1a0c1 64 20 4f 50 5f 4d 6f 76 65 4c 65 29 20 69 73 20  d OP_MoveLe) is 
1a0c2 74 68 61 74 20 69 66 20 50 32 20 69 73 0a 2a 2a  that if P2 is.**
1a0c3 20 7a 65 72 6f 20 61 6e 64 20 50 31 20 69 73 20   zero and P1 is 
1a0c4 61 6e 20 53 51 4c 20 74 61 62 6c 65 20 28 61 20  an SQL table (a 
1a0c5 62 2d 74 72 65 65 20 77 69 74 68 20 69 6e 74 65  b-tree with inte
1a0c6 67 65 72 20 6b 65 79 73 29 20 74 68 65 6e 0a 2a  ger keys) then.*
1a0c7 2a 20 74 68 65 20 73 65 65 6b 20 69 73 20 64 65  * the seek is de
1a0c8 66 65 72 72 65 64 20 75 6e 74 69 6c 20 69 74 20  ferred until it 
1a0c9 69 73 20 61 63 74 75 61 6c 6c 79 20 6e 65 65 64  is actually need
1a0ca 65 64 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65  ed.  It might be
1a0cb 0a 2a 2a 20 74 68 65 20 63 61 73 65 20 74 68 61  .** the case tha
1a0cc 74 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  t the cursor is 
1a0cd 6e 65 76 65 72 20 61 63 63 65 73 73 65 64 2e 20  never accessed. 
1a0ce 20 42 79 20 64 65 66 65 72 72 69 6e 67 20 74 68   By deferring th
1a0cf 65 0a 2a 2a 20 73 65 65 6b 2c 20 77 65 20 61 76  e.** seek, we av
1a0d0 6f 69 64 20 75 6e 6e 65 63 65 73 73 61 72 79 20  oid unnecessary 
1a0d1 73 65 65 6b 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  seeks..**.** See
1a0d2 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1a0d3 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74  tFound, Distinct
1a0d4 2c 20 4d 6f 76 65 4c 74 2c 20 4d 6f 76 65 47 74  , MoveLt, MoveGt
1a0d5 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f  , MoveLe.*/./* O
1a0d6 70 63 6f 64 65 3a 20 4d 6f 76 65 47 74 20 50 31  pcode: MoveGt P1
1a0d7 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a   P2 P3 P4 *.**.*
1a0d8 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31 20 72  * If cursor P1 r
1a0d9 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51 4c 20  efers to an SQL 
1a0da 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20 74 68  table (B-Tree th
1a0db 61 74 20 75 73 65 73 20 69 6e 74 65 67 65 72 20  at uses integer 
1a0dc 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74  keys), .** use t
1a0dd 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c 75 65  he integer value
1a0de 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 20   in register P3 
1a0df 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63 75 72  as a key. If cur
1a0e0 73 6f 72 20 50 31 20 72 65 66 65 72 73 20 0a 2a  sor P1 refers .*
1a0e1 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65  * to an SQL inde
1a0e2 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20 74 68  x, then P3 is th
1a0e3 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20 61 72  e first in an ar
1a0e4 72 61 79 20 6f 66 20 50 34 20 72 65 67 69 73 74  ray of P4 regist
1a0e5 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61 72 65  ers .** that are
1a0e6 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e 70 61   used as an unpa
1a0e7 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79 2e 20  cked index key. 
1a0e8 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f  .**.** Repositio
1a0e9 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f 20 74  n cursor P1 so t
1a0ea 68 61 74 20 20 69 74 20 70 6f 69 6e 74 73 20 74  hat  it points t
1a0eb 6f 20 74 68 65 20 73 6d 61 6c 6c 65 73 74 20 65  o the smallest e
1a0ec 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73  ntry that .** is
1a0ed 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1a0ee 65 20 6b 65 79 20 76 61 6c 75 65 2e 20 49 66 20  e key value. If 
1a0ef 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63  there are no rec
1a0f0 6f 72 64 73 20 67 72 65 61 74 65 72 20 74 68 61  ords greater tha
1a0f1 6e 20 0a 2a 2a 20 74 68 65 20 6b 65 79 20 61 6e  n .** the key an
1a0f2 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65 72 6f  d P2 is not zero
1a0f3 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50  , then jump to P
1a0f4 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  2..**.** See als
1a0f5 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46 6f 75  o: Found, NotFou
1a0f6 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20 4d 6f  nd, Distinct, Mo
1a0f7 76 65 4c 74 2c 20 4d 6f 76 65 47 65 2c 20 4d 6f  veLt, MoveGe, Mo
1a0f8 76 65 4c 65 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64  veLe.*/./* Opcod
1a0f9 65 3a 20 4d 6f 76 65 4c 74 20 50 31 20 50 32 20  e: MoveLt P1 P2 
1a0fa 50 33 20 50 34 20 2a 20 0a 2a 2a 0a 2a 2a 20 49  P3 P4 * .**.** I
1a0fb 66 20 63 75 72 73 6f 72 20 50 31 20 72 65 66 65  f cursor P1 refe
1a0fc 72 73 20 74 6f 20 61 6e 20 53 51 4c 20 74 61 62  rs to an SQL tab
1a0fd 6c 65 20 28 42 2d 54 72 65 65 20 74 68 61 74 20  le (B-Tree that 
1a0fe 75 73 65 73 20 69 6e 74 65 67 65 72 20 6b 65 79  uses integer key
1a0ff 73 29 2c 20 0a 2a 2a 20 75 73 65 20 74 68 65 20  s), .** use the 
1a100 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 69 6e  integer value in
1a101 20 72 65 67 69 73 74 65 72 20 50 33 20 61 73 20   register P3 as 
1a102 61 20 6b 65 79 2e 20 49 66 20 63 75 72 73 6f 72  a key. If cursor
1a103 20 50 31 20 72 65 66 65 72 73 20 0a 2a 2a 20 74   P1 refers .** t
1a104 6f 20 61 6e 20 53 51 4c 20 69 6e 64 65 78 2c 20  o an SQL index, 
1a105 74 68 65 6e 20 50 33 20 69 73 20 74 68 65 20 66  then P3 is the f
1a106 69 72 73 74 20 69 6e 20 61 6e 20 61 72 72 61 79  irst in an array
1a107 20 6f 66 20 50 34 20 72 65 67 69 73 74 65 72 73   of P4 registers
1a108 20 0a 2a 2a 20 74 68 61 74 20 61 72 65 20 75 73   .** that are us
1a109 65 64 20 61 73 20 61 6e 20 75 6e 70 61 63 6b 65  ed as an unpacke
1a10a 64 20 69 6e 64 65 78 20 6b 65 79 2e 20 0a 2a 2a  d index key. .**
1a10b 0a 2a 2a 20 52 65 70 6f 73 69 74 69 6f 6e 20 63  .** Reposition c
1a10c 75 72 73 6f 72 20 50 31 20 73 6f 20 74 68 61 74  ursor P1 so that
1a10d 20 20 69 74 20 70 6f 69 6e 74 73 20 74 6f 20 74    it points to t
1a10e 68 65 20 6c 61 72 67 65 73 74 20 65 6e 74 72 79  he largest entry
1a10f 20 74 68 61 74 20 0a 2a 2a 20 69 73 20 6c 65 73   that .** is les
1a110 73 20 74 68 61 6e 20 74 68 65 20 6b 65 79 20 76  s than the key v
1a111 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
1a112 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 6c 65  re no records le
1a113 73 73 20 74 68 61 6e 20 0a 2a 2a 20 74 68 65 20  ss than .** the 
1a114 6b 65 79 20 61 6e 64 20 50 32 20 69 73 20 6e 6f  key and P2 is no
1a115 74 20 7a 65 72 6f 2c 20 74 68 65 6e 20 6a 75 6d  t zero, then jum
1a116 70 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53  p to P2..**.** S
1a117 65 65 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20  ee also: Found, 
1a118 4e 6f 74 46 6f 75 6e 64 2c 20 44 69 73 74 69 6e  NotFound, Distin
1a119 63 74 2c 20 4d 6f 76 65 47 74 2c 20 4d 6f 76 65  ct, MoveGt, Move
1a11a 47 65 2c 20 4d 6f 76 65 4c 65 0a 2a 2f 0a 2f 2a  Ge, MoveLe.*/./*
1a11b 20 4f 70 63 6f 64 65 3a 20 4d 6f 76 65 4c 65 20   Opcode: MoveLe 
1a11c 50 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a  P1 P2 P3 P4 *.**
1a11d 0a 2a 2a 20 49 66 20 63 75 72 73 6f 72 20 50 31  .** If cursor P1
1a11e 20 72 65 66 65 72 73 20 74 6f 20 61 6e 20 53 51   refers to an SQ
1a11f 4c 20 74 61 62 6c 65 20 28 42 2d 54 72 65 65 20  L table (B-Tree 
1a120 74 68 61 74 20 75 73 65 73 20 69 6e 74 65 67 65  that uses intege
1a121 72 20 6b 65 79 73 29 2c 20 0a 2a 2a 20 75 73 65  r keys), .** use
1a122 20 74 68 65 20 69 6e 74 65 67 65 72 20 76 61 6c   the integer val
1a123 75 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  ue in register P
1a124 33 20 61 73 20 61 20 6b 65 79 2e 20 49 66 20 63  3 as a key. If c
1a125 75 72 73 6f 72 20 50 31 20 72 65 66 65 72 73 20  ursor P1 refers 
1a126 0a 2a 2a 20 74 6f 20 61 6e 20 53 51 4c 20 69 6e  .** to an SQL in
1a127 64 65 78 2c 20 74 68 65 6e 20 50 33 20 69 73 20  dex, then P3 is 
1a128 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 6e 20  the first in an 
1a129 61 72 72 61 79 20 6f 66 20 50 34 20 72 65 67 69  array of P4 regi
1a12a 73 74 65 72 73 20 0a 2a 2a 20 74 68 61 74 20 61  sters .** that a
1a12b 72 65 20 75 73 65 64 20 61 73 20 61 6e 20 75 6e  re used as an un
1a12c 70 61 63 6b 65 64 20 69 6e 64 65 78 20 6b 65 79  packed index key
1a12d 2e 20 0a 2a 2a 0a 2a 2a 20 52 65 70 6f 73 69 74  . .**.** Reposit
1a12e 69 6f 6e 20 63 75 72 73 6f 72 20 50 31 20 73 6f  ion cursor P1 so
1a12f 20 74 68 61 74 20 69 74 20 70 6f 69 6e 74 73 20   that it points 
1a130 74 6f 20 74 68 65 20 6c 61 72 67 65 73 74 20 65  to the largest e
1a131 6e 74 72 79 20 74 68 61 74 20 0a 2a 2a 20 69 73  ntry that .** is
1a132 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65 71   less than or eq
1a133 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20 76  ual to the key v
1a134 61 6c 75 65 2e 20 49 66 20 74 68 65 72 65 20 61  alue. If there a
1a135 72 65 20 6e 6f 20 72 65 63 6f 72 64 73 20 0a 2a  re no records .*
1a136 2a 20 6c 65 73 73 20 74 68 61 6e 20 6f 72 20 65  * less than or e
1a137 71 75 61 6c 20 74 6f 20 74 68 65 20 6b 65 79 20  qual to the key 
1a138 61 6e 64 20 50 32 20 69 73 20 6e 6f 74 20 7a 65  and P2 is not ze
1a139 72 6f 2c 20 74 68 65 6e 20 6a 75 6d 70 20 74 6f  ro, then jump to
1a13a 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61   P2..**.** See a
1a13b 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 46  lso: Found, NotF
1a13c 6f 75 6e 64 2c 20 44 69 73 74 69 6e 63 74 2c 20  ound, Distinct, 
1a13d 4d 6f 76 65 47 74 2c 20 4d 6f 76 65 47 65 2c 20  MoveGt, MoveGe, 
1a13e 4d 6f 76 65 4c 74 0a 2a 2f 0a 63 61 73 65 20 4f  MoveLt.*/.case O
1a13f 50 5f 4d 6f 76 65 4c 74 3a 20 20 20 20 20 20 20  P_MoveLt:       
1a140 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1a141 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 4c 65  /.case OP_MoveLe
1a142 3a 20 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  :         /* jum
1a143 70 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f  p, in3 */.case O
1a144 50 5f 4d 6f 76 65 47 65 3a 20 20 20 20 20 20 20  P_MoveGe:       
1a145 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a    /* jump, in3 *
1a146 2f 0a 63 61 73 65 20 4f 50 5f 4d 6f 76 65 47 74  /.case OP_MoveGt
1a147 3a 20 7b 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d  : {       /* jum
1a148 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20  p, in3 */.  int 
1a149 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56  i = pOp->p1;.  V
1a14a 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a  dbeCursor *pC;..
1a14b 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1a14c 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1a14d 3b 0a 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73  ;.  pC = p->apCs
1a14e 72 5b 69 5d 3b 0a 20 20 61 73 73 65 72 74 28 20  r[i];.  assert( 
1a14f 70 43 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  pC!=0 );.  if( p
1a150 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 7b  C->pCursor!=0 ){
1a151 0a 20 20 20 20 69 6e 74 20 72 65 73 2c 20 6f 63  .    int res, oc
1a152 3b 0a 20 20 20 20 6f 63 20 3d 20 70 4f 70 2d 3e  ;.    oc = pOp->
1a153 6f 70 63 6f 64 65 3b 0a 20 20 20 20 70 43 2d 3e  opcode;.    pC->
1a154 6e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20  nullRow = 0;.   
1a155 20 69 66 28 20 70 43 2d 3e 69 73 54 61 62 6c 65   if( pC->isTable
1a156 20 29 7b 0a 20 20 20 20 20 20 69 36 34 20 69 4b   ){.      i64 iK
1a157 65 79 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  ey = sqlite3Vdbe
1a158 49 6e 74 56 61 6c 75 65 28 70 49 6e 33 29 3b 0a  IntValue(pIn3);.
1a159 20 20 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70        if( pOp->p
1a15a 32 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  2==0 ){.        
1a15b 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63  assert( pOp->opc
1a15c 6f 64 65 3d 3d 4f 50 5f 4d 6f 76 65 47 65 20 29  ode==OP_MoveGe )
1a15d 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 6d 6f  ;.        pC->mo
1a15e 76 65 74 6f 54 61 72 67 65 74 20 3d 20 69 4b 65  vetoTarget = iKe
1a15f 79 3b 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 72  y;.        pC->r
1a160 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1a161 0a 20 20 20 20 20 20 20 20 70 43 2d 3e 64 65 66  .        pC->def
1a162 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 31 3b  erredMoveto = 1;
1a163 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
1a164 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
1a165 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
1a166 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28 70 43  ovetoUnpacked(pC
1a167 2d 3e 70 43 75 72 73 6f 72 2c 20 30 2c 20 28 75  ->pCursor, 0, (u
1a168 36 34 29 69 4b 65 79 2c 20 30 2c 20 26 72 65 73  64)iKey, 0, &res
1a169 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21  );.      if( rc!
1a16a 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1a16b 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74        goto abort
1a16c 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1a16d 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 43 2d       }.      pC-
1a16e 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 69 4b 65  >lastRowid = iKe
1a16f 79 3b 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f 77  y;.      pC->row
1a170 69 64 49 73 56 61 6c 69 64 20 3d 20 72 65 73 3d  idIsValid = res=
1a171 3d 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  =0;.    }else{. 
1a172 20 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65 63       UnpackedRec
1a173 6f 72 64 20 72 3b 0a 20 20 20 20 20 20 69 6e 74  ord r;.      int
1a174 20 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70   nField = pOp->p
1a175 34 2e 69 3b 0a 20 20 20 20 20 20 61 73 73 65 72  4.i;.      asser
1a176 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d  t( pOp->p4type==
1a177 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20  P4_INT32 );.    
1a178 20 20 61 73 73 65 72 74 28 20 6e 46 69 65 6c 64    assert( nField
1a179 3e 30 20 29 3b 0a 20 20 20 20 20 20 72 2e 70 4b  >0 );.      r.pK
1a17a 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e 70 4b 65  eyInfo = pC->pKe
1a17b 79 49 6e 66 6f 3b 0a 20 20 20 20 20 20 72 2e 6e  yInfo;.      r.n
1a17c 46 69 65 6c 64 20 3d 20 6e 46 69 65 6c 64 3b 0a  Field = nField;.
1a17d 20 20 20 20 20 20 69 66 28 20 6f 63 3d 3d 4f 50        if( oc==OP
1a17e 5f 4d 6f 76 65 47 74 20 7c 7c 20 6f 63 3d 3d 4f  _MoveGt || oc==O
1a17f 50 5f 4d 6f 76 65 4c 65 20 29 7b 0a 20 20 20 20  P_MoveLe ){.    
1a180 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e      r.flags = UN
1a181 50 41 43 4b 45 44 5f 49 4e 43 52 4b 45 59 3b 0a  PACKED_INCRKEY;.
1a182 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1a183 20 20 20 20 20 72 2e 66 6c 61 67 73 20 3d 20 30       r.flags = 0
1a184 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1a185 72 2e 61 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65  r.aMem = &p->aMe
1a186 6d 5b 70 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20  m[pOp->p3];.    
1a187 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
1a188 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65  reeMovetoUnpacke
1a189 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20 26  d(pC->pCursor, &
1a18a 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a  r, 0, 0, &res);.
1a18b 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
1a18c 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
1a18d 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75     goto abort_du
1a18e 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20 20  e_to_error;.    
1a18f 20 20 7d 0a 20 20 20 20 20 20 70 43 2d 3e 72 6f    }.      pC->ro
1a190 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a  widIsValid = 0;.
1a191 20 20 20 20 7d 0a 20 20 20 20 70 43 2d 3e 64 65      }.    pC->de
1a192 66 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30  ferredMoveto = 0
1a193 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53  ;.    pC->cacheS
1a194 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54  tatus = CACHE_ST
1a195 41 4c 45 3b 0a 23 69 66 64 65 66 20 53 51 4c 49  ALE;.#ifdef SQLI
1a196 54 45 5f 54 45 53 54 0a 20 20 20 20 73 71 6c 69  TE_TEST.    sqli
1a197 74 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74  te3_search_count
1a198 2b 2b 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 69  ++;.#endif.    i
1a199 66 28 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47 65  f( oc==OP_MoveGe
1a19a 20 7c 7c 20 6f 63 3d 3d 4f 50 5f 4d 6f 76 65 47   || oc==OP_MoveG
1a19b 74 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72  t ){.      if( r
1a19c 65 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  es<0 ){.        
1a19d 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1a19e 65 4e 65 78 74 28 70 43 2d 3e 70 43 75 72 73 6f  eNext(pC->pCurso
1a19f 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20  r, &res);.      
1a1a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1a1a1 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  _OK ) goto abort
1a1a2 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20  _due_to_error;. 
1a1a3 20 20 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64         pC->rowid
1a1a4 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1a1a5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a1a6 20 20 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 20    res = 0;.     
1a1a7 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1a1a8 20 20 20 20 61 73 73 65 72 74 28 20 6f 63 3d 3d      assert( oc==
1a1a9 4f 50 5f 4d 6f 76 65 4c 74 20 7c 7c 20 6f 63 3d  OP_MoveLt || oc=
1a1aa 3d 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a 20 20  =OP_MoveLe );.  
1a1ab 20 20 20 20 69 66 28 20 72 65 73 3e 3d 30 20 29      if( res>=0 )
1a1ac 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
1a1ad 71 6c 69 74 65 33 42 74 72 65 65 50 72 65 76 69  qlite3BtreePrevi
1a1ae 6f 75 73 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c  ous(pC->pCursor,
1a1af 20 26 72 65 73 29 3b 0a 20 20 20 20 20 20 20 20   &res);.        
1a1b0 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
1a1b1 4b 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  K ) goto abort_d
1a1b2 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 20  ue_to_error;.   
1a1b3 20 20 20 20 20 70 43 2d 3e 72 6f 77 69 64 49 73       pC->rowidIs
1a1b4 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20 20 20  Valid = 0;.     
1a1b5 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
1a1b6 2f 2a 20 72 65 73 20 6d 69 67 68 74 20 62 65 20  /* res might be 
1a1b7 6e 65 67 61 74 69 76 65 20 62 65 63 61 75 73 65  negative because
1a1b8 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 65 6d   the table is em
1a1b9 70 74 79 2e 20 20 43 68 65 63 6b 20 74 6f 0a 20  pty.  Check to. 
1a1ba 20 20 20 20 20 20 20 2a 2a 20 73 65 65 20 69 66         ** see if
1a1bb 20 74 68 69 73 20 69 73 20 74 68 65 20 63 61 73   this is the cas
1a1bc 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
1a1bd 20 20 20 20 20 20 72 65 73 20 3d 20 73 71 6c 69        res = sqli
1a1be 74 65 33 42 74 72 65 65 45 6f 66 28 70 43 2d 3e  te3BtreeEof(pC->
1a1bf 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20  pCursor);.      
1a1c0 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
1a1c1 72 74 28 20 70 4f 70 2d 3e 70 32 3e 30 20 29 3b  rt( pOp->p2>0 );
1a1c2 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a  .    if( res ){.
1a1c3 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1a1c4 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  p2 - 1;.    }.  
1a1c5 7d 65 6c 73 65 20 69 66 28 20 21 70 43 2d 3e 70  }else if( !pC->p
1a1c6 73 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20  seudoTable ){.  
1a1c7 20 20 2f 2a 20 54 68 69 73 20 68 61 70 70 65 6e    /* This happen
1a1c8 73 20 77 68 65 6e 20 61 74 74 65 6d 70 74 69 6e  s when attemptin
1a1c9 67 20 74 6f 20 6f 70 65 6e 20 74 68 65 20 73 71  g to open the sq
1a1ca 6c 69 74 65 33 5f 6d 61 73 74 65 72 20 74 61 62  lite3_master tab
1a1cb 6c 65 0a 20 20 20 20 2a 2a 20 66 6f 72 20 72 65  le.    ** for re
1a1cc 61 64 20 61 63 63 65 73 73 20 72 65 74 75 72 6e  ad access return
1a1cd 73 20 53 51 4c 49 54 45 5f 45 4d 50 54 59 2e 20  s SQLITE_EMPTY. 
1a1ce 49 6e 20 74 68 69 73 20 63 61 73 65 20 61 6c 77  In this case alw
1a1cf 61 79 73 0a 20 20 20 20 2a 2a 20 74 61 6b 65 20  ays.    ** take 
1a1d0 74 68 65 20 6a 75 6d 70 20 28 73 69 6e 63 65 20  the jump (since 
1a1d1 74 68 65 72 65 20 61 72 65 20 6e 6f 20 72 65 63  there are no rec
1a1d2 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61 62 6c  ords in the tabl
1a1d3 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70  e)..    */.    p
1a1d4 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1a1d5 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1a1d6 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 6f 75 6e  ./* Opcode: Foun
1a1d7 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1a1d8 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 33  *.** Register P3
1a1d9 20 68 6f 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f   holds a blob co
1a1da 6e 73 74 72 75 63 74 65 64 20 62 79 20 4d 61 6b  nstructed by Mak
1a1db 65 52 65 63 6f 72 64 2e 20 20 50 31 20 69 73 20  eRecord.  P1 is 
1a1dc 61 6e 20 69 6e 64 65 78 2e 0a 2a 2a 20 49 66 20  an index..** If 
1a1dd 61 6e 20 65 6e 74 72 79 20 74 68 61 74 20 6d 61  an entry that ma
1a1de 74 63 68 65 73 20 74 68 65 20 76 61 6c 75 65 20  tches the value 
1a1df 69 6e 20 72 65 67 69 73 74 65 72 20 70 33 20 65  in register p3 e
1a1e0 78 69 73 74 73 20 69 6e 20 50 31 20 74 68 65 6e  xists in P1 then
1a1e1 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e 20  .** jump to P2. 
1a1e2 20 49 66 20 74 68 65 20 50 33 20 76 61 6c 75 65   If the P3 value
1a1e3 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
1a1e4 61 6e 79 20 65 6e 74 72 79 20 69 6e 20 50 31 0a  any entry in P1.
1a1e5 2a 2a 20 74 68 65 6e 20 66 61 6c 6c 20 74 68 72  ** then fall thr
1a1e6 75 2e 20 20 54 68 65 20 50 31 20 63 75 72 73 6f  u.  The P1 curso
1a1e7 72 20 69 73 20 6c 65 66 74 20 70 6f 69 6e 74 69  r is left pointi
1a1e8 6e 67 20 61 74 20 74 68 65 20 6d 61 74 63 68 69  ng at the matchi
1a1e9 6e 67 20 65 6e 74 72 79 0a 2a 2a 20 69 66 20 69  ng entry.** if i
1a1ea 74 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a 2a 20  t exists..**.** 
1a1eb 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1a1ec 20 69 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c   is used to impl
1a1ed 65 6d 65 6e 74 20 74 68 65 20 49 4e 20 6f 70 65  ement the IN ope
1a1ee 72 61 74 6f 72 20 77 68 65 72 65 20 74 68 65 0a  rator where the.
1a1ef 2a 2a 20 6c 65 66 74 2d 68 61 6e 64 20 73 69 64  ** left-hand sid
1a1f0 65 20 69 73 20 61 20 53 45 4c 45 43 54 20 73 74  e is a SELECT st
1a1f1 61 74 65 6d 65 6e 74 2e 20 20 50 31 20 6d 61 79  atement.  P1 may
1a1f2 20 62 65 20 61 20 74 72 75 65 20 69 6e 64 65 78   be a true index
1a1f3 2c 20 6f 72 20 69 74 0a 2a 2a 20 6d 61 79 20 62  , or it.** may b
1a1f4 65 20 61 20 74 65 6d 70 6f 72 61 72 79 20 69 6e  e a temporary in
1a1f5 64 65 78 20 74 68 61 74 20 68 6f 6c 64 73 20 74  dex that holds t
1a1f6 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
1a1f7 65 20 53 45 4c 45 43 54 0a 2a 2a 20 73 74 61 74  e SELECT.** stat
1a1f8 65 6d 65 6e 74 2e 20 20 20 54 68 69 73 20 69 6e  ement.   This in
1a1f9 73 74 72 75 63 74 69 6f 6e 20 69 73 20 61 6c 73  struction is als
1a1fa 6f 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  o used to implem
1a1fb 65 6e 74 20 74 68 65 0a 2a 2a 20 44 49 53 54 49  ent the.** DISTI
1a1fc 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 6e 20 53  NCT keyword in S
1a1fd 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73  ELECT statements
1a1fe 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73  ..**.** This ins
1a1ff 74 72 75 63 74 69 6f 6e 20 63 68 65 63 6b 73 20  truction checks 
1a200 69 66 20 69 6e 64 65 78 20 50 31 20 63 6f 6e 74  if index P1 cont
1a201 61 69 6e 73 20 61 20 72 65 63 6f 72 64 20 66 6f  ains a record fo
1a202 72 20 77 68 69 63 68 20 0a 2a 2a 20 74 68 65 20  r which .** the 
1a203 66 69 72 73 74 20 4e 20 73 65 72 69 61 6c 69 7a  first N serializ
1a204 65 64 20 76 61 6c 75 65 73 20 65 78 61 63 74 6c  ed values exactl
1a205 79 20 6d 61 74 63 68 20 74 68 65 20 4e 20 73 65  y match the N se
1a206 72 69 61 6c 69 7a 65 64 20 76 61 6c 75 65 73 0a  rialized values.
1a207 2a 2a 20 69 6e 20 74 68 65 20 72 65 63 6f 72 64  ** in the record
1a208 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 33 2c   in register P3,
1a209 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65 20   where N is the 
1a20a 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20  total number of 
1a20b 76 61 6c 75 65 73 20 69 6e 0a 2a 2a 20 74 68 65  values in.** the
1a20c 20 50 33 20 72 65 63 6f 72 64 20 28 74 68 65 20   P3 record (the 
1a20d 50 33 20 72 65 63 6f 72 64 20 69 73 20 61 20 70  P3 record is a p
1a20e 72 65 66 69 78 20 6f 66 20 74 68 65 20 50 31 20  refix of the P1 
1a20f 72 65 63 6f 72 64 29 2e 20 0a 2a 2a 0a 2a 2a 20  record). .**.** 
1a210 53 65 65 20 61 6c 73 6f 3a 20 4e 6f 74 46 6f 75  See also: NotFou
1a211 6e 64 2c 20 49 73 55 6e 69 71 75 65 2c 20 4e 6f  nd, IsUnique, No
1a212 74 45 78 69 73 74 73 0a 2a 2f 0a 2f 2a 20 4f 70  tExists.*/./* Op
1a213 63 6f 64 65 3a 20 4e 6f 74 46 6f 75 6e 64 20 50  code: NotFound P
1a214 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a  1 P2 P3 * *.**.*
1a215 2a 20 52 65 67 69 73 74 65 72 20 50 33 20 68 6f  * Register P3 ho
1a216 6c 64 73 20 61 20 62 6c 6f 62 20 63 6f 6e 73 74  lds a blob const
1a217 72 75 63 74 65 64 20 62 79 20 4d 61 6b 65 52 65  ructed by MakeRe
1a218 63 6f 72 64 2e 20 20 50 31 20 69 73 0a 2a 2a 20  cord.  P1 is.** 
1a219 61 6e 20 69 6e 64 65 78 2e 20 20 49 66 20 6e 6f  an index.  If no
1a21a 20 65 6e 74 72 79 20 65 78 69 73 74 73 20 69 6e   entry exists in
1a21b 20 50 31 20 74 68 61 74 20 6d 61 74 63 68 65 73   P1 that matches
1a21c 20 74 68 65 20 62 6c 6f 62 20 74 68 65 6e 20 6a   the blob then j
1a21d 75 6d 70 0a 2a 2a 20 74 6f 20 50 32 2e 20 20 49  ump.** to P2.  I
1a21e 66 20 61 6e 20 65 6e 74 72 79 20 64 6f 65 73 20  f an entry does 
1a21f 65 78 69 73 74 69 6e 67 2c 20 66 61 6c 6c 20 74  existing, fall t
1a220 68 72 6f 75 67 68 2e 20 20 54 68 65 20 63 75 72  hrough.  The cur
1a221 73 6f 72 20 69 73 20 6c 65 66 74 0a 2a 2a 20 70  sor is left.** p
1a222 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 65  ointing to the e
1a223 6e 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65  ntry that matche
1a224 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73  s..**.** See als
1a225 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69  o: Found, NotExi
1a226 73 74 73 2c 20 49 73 55 6e 69 71 75 65 0a 2a 2f  sts, IsUnique.*/
1a227 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 46 6f 75 6e  .case OP_NotFoun
1a228 64 3a 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  d:       /* jump
1a229 2c 20 69 6e 33 20 2a 2f 0a 63 61 73 65 20 4f 50  , in3 */.case OP
1a22a 5f 46 6f 75 6e 64 3a 20 7b 20 20 20 20 20 20 20  _Found: {       
1a22b 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f   /* jump, in3 */
1a22c 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e  .  int i = pOp->
1a22d 70 31 3b 0a 20 20 69 6e 74 20 61 6c 72 65 61 64  p1;.  int alread
1a22e 79 45 78 69 73 74 73 20 3d 20 30 3b 0a 20 20 56  yExists = 0;.  V
1a22f 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20  dbeCursor *pC;. 
1a230 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26   assert( i>=0 &&
1a231 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b   i<p->nCursor );
1a232 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70  .  assert( p->ap
1a233 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69  Csr[i]!=0 );.  i
1a234 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43 73  f( (pC = p->apCs
1a235 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21 3d  r[i])->pCursor!=
1a236 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 73  0 ){.    int res
1a237 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52 65  ;.    UnpackedRe
1a238 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 0a 0a  cord *pIdxKey;..
1a239 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1a23a 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  isTable==0 );.  
1a23b 20 20 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e    assert( pIn3->
1a23c 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62  flags & MEM_Blob
1a23d 20 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20   );.    pIdxKey 
1a23e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63  = sqlite3VdbeRec
1a23f 6f 72 64 55 6e 70 61 63 6b 28 70 43 2d 3e 70 4b  ordUnpack(pC->pK
1a240 65 79 49 6e 66 6f 2c 20 70 49 6e 33 2d 3e 6e 2c  eyInfo, pIn3->n,
1a241 20 70 49 6e 33 2d 3e 7a 2c 0a 20 20 20 20 20 20   pIn3->z,.      
1a242 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a243 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a244 61 54 65 6d 70 52 65 63 2c 20 73 69 7a 65 6f 66  aTempRec, sizeof
1a245 28 61 54 65 6d 70 52 65 63 29 29 3b 0a 20 20 20  (aTempRec));.   
1a246 20 69 66 28 20 70 49 64 78 4b 65 79 3d 3d 30 20   if( pIdxKey==0 
1a247 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
1a248 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
1a249 69 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d  if( pOp->opcode=
1a24a 3d 4f 50 5f 46 6f 75 6e 64 20 29 7b 0a 20 20 20  =OP_Found ){.   
1a24b 20 20 20 70 49 64 78 4b 65 79 2d 3e 66 6c 61 67     pIdxKey->flag
1a24c 73 20 7c 3d 20 55 4e 50 41 43 4b 45 44 5f 50 52  s |= UNPACKED_PR
1a24d 45 46 49 58 5f 4d 41 54 43 48 3b 0a 20 20 20 20  EFIX_MATCH;.    
1a24e 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
1a24f 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70  e3BtreeMovetoUnp
1a250 61 63 6b 65 64 28 70 43 2d 3e 70 43 75 72 73 6f  acked(pC->pCurso
1a251 72 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 30  r, pIdxKey, 0, 0
1a252 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 73 71 6c  , &res);.    sql
1a253 69 74 65 33 56 64 62 65 44 65 6c 65 74 65 55 6e  ite3VdbeDeleteUn
1a254 70 61 63 6b 65 64 52 65 63 6f 72 64 28 70 49 64  packedRecord(pId
1a255 78 4b 65 79 29 3b 0a 20 20 20 20 69 66 28 20 72  xKey);.    if( r
1a256 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
1a257 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a258 20 7d 0a 20 20 20 20 61 6c 72 65 61 64 79 45 78   }.    alreadyEx
1a259 69 73 74 73 20 3d 20 28 72 65 73 3d 3d 30 29 3b  ists = (res==0);
1a25a 0a 20 20 20 20 70 43 2d 3e 64 65 66 65 72 72 65  .    pC->deferre
1a25b 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20 20 20  dMoveto = 0;.   
1a25c 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
1a25d 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1a25e 20 20 7d 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f    }.  if( pOp->o
1a25f 70 63 6f 64 65 3d 3d 4f 50 5f 46 6f 75 6e 64 20  pcode==OP_Found 
1a260 29 7b 0a 20 20 20 20 69 66 28 20 61 6c 72 65 61  ){.    if( alrea
1a261 64 79 45 78 69 73 74 73 20 29 20 70 63 20 3d 20  dyExists ) pc = 
1a262 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 7d  pOp->p2 - 1;.  }
1a263 65 6c 73 65 7b 0a 20 20 20 20 69 66 28 20 21 61  else{.    if( !a
1a264 6c 72 65 61 64 79 45 78 69 73 74 73 20 29 20 70  lreadyExists ) p
1a265 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1a266 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1a267 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 73 55 6e  ./* Opcode: IsUn
1a268 69 71 75 65 20 50 31 20 50 32 20 50 33 20 50 34  ique P1 P2 P3 P4
1a269 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 33 20   *.**.** The P3 
1a26a 72 65 67 69 73 74 65 72 20 63 6f 6e 74 61 69 6e  register contain
1a26b 73 20 61 6e 20 69 6e 74 65 67 65 72 20 72 65 63  s an integer rec
1a26c 6f 72 64 20 6e 75 6d 62 65 72 2e 20 20 43 61 6c  ord number.  Cal
1a26d 6c 20 74 68 69 73 0a 2a 2a 20 72 65 63 6f 72 64  l this.** record
1a26e 20 6e 75 6d 62 65 72 20 52 2e 20 20 54 68 65 20   number R.  The 
1a26f 50 34 20 72 65 67 69 73 74 65 72 20 63 6f 6e 74  P4 register cont
1a270 61 69 6e 73 20 61 6e 20 69 6e 64 65 78 20 6b 65  ains an index ke
1a271 79 20 63 72 65 61 74 65 64 0a 2a 2a 20 75 73 69  y created.** usi
1a272 6e 67 20 4d 61 6b 65 52 65 63 6f 72 64 2e 20 20  ng MakeRecord.  
1a273 43 61 6c 6c 20 69 74 20 4b 2e 0a 2a 2a 0a 2a 2a  Call it K..**.**
1a274 20 50 31 20 69 73 20 61 6e 20 69 6e 64 65 78 2e   P1 is an index.
1a275 20 20 53 6f 20 69 74 20 68 61 73 20 6e 6f 20 64    So it has no d
1a276 61 74 61 20 61 6e 64 20 69 74 73 20 6b 65 79 20  ata and its key 
1a277 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 0a 2a 2a  consists of a.**
1a278 20 72 65 63 6f 72 64 20 67 65 6e 65 72 61 74 65   record generate
1a279 64 20 62 79 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  d by OP_MakeReco
1a27a 72 64 20 77 68 65 72 65 20 74 68 65 20 6c 61 73  rd where the las
1a27b 74 20 66 69 65 6c 64 20 69 73 20 74 68 65 20 0a  t field is the .
1a27c 2a 2a 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  ** rowid of the 
1a27d 65 6e 74 72 79 20 74 68 61 74 20 74 68 65 20 69  entry that the i
1a27e 6e 64 65 78 20 72 65 66 65 72 73 20 74 6f 2e 0a  ndex refers to..
1a27f 2a 2a 20 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  ** .** This inst
1a280 72 75 63 74 69 6f 6e 20 61 73 6b 73 20 69 66 20  ruction asks if 
1a281 74 68 65 72 65 20 69 73 20 61 6e 20 65 6e 74 72  there is an entr
1a282 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 74 68  y in P1 where th
1a283 65 0a 2a 2a 20 66 69 65 6c 64 73 20 6d 61 74 63  e.** fields matc
1a284 68 65 73 20 4b 20 62 75 74 20 74 68 65 20 72 6f  hes K but the ro
1a285 77 69 64 20 69 73 20 64 69 66 66 65 72 65 6e 74  wid is different
1a286 20 66 72 6f 6d 20 52 2e 0a 2a 2a 20 49 66 20 74   from R..** If t
1a287 68 65 72 65 20 69 73 20 6e 6f 20 73 75 63 68 20  here is no such 
1a288 65 6e 74 72 79 2c 20 74 68 65 6e 20 74 68 65 72  entry, then ther
1a289 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74  e is an immediat
1a28a 65 0a 2a 2a 20 6a 75 6d 70 20 74 6f 20 50 32 2e  e.** jump to P2.
1a28b 20 20 49 66 20 61 6e 79 20 65 6e 74 72 79 20 64    If any entry d
1a28c 6f 65 73 20 65 78 69 73 74 20 77 68 65 72 65 20  oes exist where 
1a28d 74 68 65 20 69 6e 64 65 78 20 73 74 72 69 6e 67  the index string
1a28e 0a 2a 2a 20 6d 61 74 63 68 65 73 20 4b 20 62 75  .** matches K bu
1a28f 74 20 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d  t the record num
1a290 62 65 72 20 69 73 20 6e 6f 74 20 52 2c 20 74 68  ber is not R, th
1a291 65 6e 20 74 68 65 20 72 65 63 6f 72 64 0a 2a 2a  en the record.**
1a292 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61 74   number for that
1a293 20 65 6e 74 72 79 20 69 73 20 77 72 69 74 74 65   entry is writte
1a294 6e 20 69 6e 74 6f 20 50 33 20 61 6e 64 20 63 6f  n into P3 and co
1a295 6e 74 72 6f 6c 0a 2a 2a 20 66 61 6c 6c 73 20 74  ntrol.** falls t
1a296 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 6e 65  hrough to the ne
1a297 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a  xt instruction..
1a298 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20  **.** See also: 
1a299 4e 6f 74 46 6f 75 6e 64 2c 20 4e 6f 74 45 78 69  NotFound, NotExi
1a29a 73 74 73 2c 20 46 6f 75 6e 64 0a 2a 2f 0a 63 61  sts, Found.*/.ca
1a29b 73 65 20 4f 50 5f 49 73 55 6e 69 71 75 65 3a 20  se OP_IsUnique: 
1a29c 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70  {        /* jump
1a29d 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74 20 69  , in3 */.  int i
1a29e 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64   = pOp->p1;.  Vd
1a29f 62 65 43 75 72 73 6f 72 20 2a 70 43 78 3b 0a 20  beCursor *pCx;. 
1a2a0 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72 73 72   BtCursor *pCrsr
1a2a1 3b 0a 20 20 4d 65 6d 20 2a 70 4b 3b 0a 20 20 69  ;.  Mem *pK;.  i
1a2a2 36 34 20 52 3b 0a 0a 20 20 2f 2a 20 50 6f 70 20  64 R;..  /* Pop 
1a2a3 74 68 65 20 76 61 6c 75 65 20 52 20 6f 66 66 20  the value R off 
1a2a4 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
1a2a5 74 61 63 6b 0a 20 20 2a 2f 0a 20 20 61 73 73 65  tack.  */.  asse
1a2a6 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d  rt( pOp->p4type=
1a2a7 3d 50 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 61  =P4_INT32 );.  a
1a2a8 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e 69  ssert( pOp->p4.i
1a2a9 3e 30 20 26 26 20 70 4f 70 2d 3e 70 34 2e 69 3c  >0 && pOp->p4.i<
1a2aa 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 4b  =p->nMem );.  pK
1a2ab 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d   = &p->aMem[pOp-
1a2ac 3e 70 34 2e 69 5d 3b 0a 20 20 73 71 6c 69 74 65  >p4.i];.  sqlite
1a2ad 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
1a2ae 66 79 28 70 49 6e 33 29 3b 0a 20 20 52 20 3d 20  fy(pIn3);.  R = 
1a2af 70 49 6e 33 2d 3e 75 2e 69 3b 0a 20 20 61 73 73  pIn3->u.i;.  ass
1a2b0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1a2b1 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70  ->nCursor );.  p
1a2b2 43 78 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  Cx = p->apCsr[i]
1a2b3 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 78 21  ;.  assert( pCx!
1a2b4 3d 30 20 29 3b 0a 20 20 70 43 72 73 72 20 3d 20  =0 );.  pCrsr = 
1a2b5 70 43 78 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20  pCx->pCursor;.  
1a2b6 69 66 28 20 70 43 72 73 72 21 3d 30 20 29 7b 0a  if( pCrsr!=0 ){.
1a2b7 20 20 20 20 69 6e 74 20 72 65 73 3b 0a 20 20 20      int res;.   
1a2b8 20 69 36 34 20 76 3b 20 20 20 20 20 20 20 20 20   i64 v;         
1a2b9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
1a2ba 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
1a2bb 20 74 68 61 74 20 6d 61 74 63 68 65 73 20 4b 20   that matches K 
1a2bc 2a 2f 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52  */.    UnpackedR
1a2bd 65 63 6f 72 64 20 2a 70 49 64 78 4b 65 79 3b 20  ecord *pIdxKey; 
1a2be 20 20 2f 2a 20 55 6e 70 61 63 6b 65 64 20 76 65    /* Unpacked ve
1a2bf 72 73 69 6f 6e 20 6f 66 20 50 34 20 2a 2f 0a 0a  rsion of P4 */..
1a2c0 20 20 20 20 2f 2a 20 4d 61 6b 65 20 73 75 72 65      /* Make sure
1a2c1 20 4b 20 69 73 20 61 20 73 74 72 69 6e 67 20 61   K is a string a
1a2c2 6e 64 20 6d 61 6b 65 20 7a 4b 65 79 20 70 6f 69  nd make zKey poi
1a2c3 6e 74 20 74 6f 20 4b 0a 20 20 20 20 2a 2f 0a 20  nt to K.    */. 
1a2c4 20 20 20 61 73 73 65 72 74 28 20 70 4b 2d 3e 66     assert( pK->f
1a2c5 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c 6f 62 20  lags & MEM_Blob 
1a2c6 29 3b 0a 20 20 20 20 70 49 64 78 4b 65 79 20 3d  );.    pIdxKey =
1a2c7 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 63 6f   sqlite3VdbeReco
1a2c8 72 64 55 6e 70 61 63 6b 28 70 43 78 2d 3e 70 4b  rdUnpack(pCx->pK
1a2c9 65 79 49 6e 66 6f 2c 20 70 4b 2d 3e 6e 2c 20 70  eyInfo, pK->n, p
1a2ca 4b 2d 3e 7a 2c 0a 20 20 20 20 20 20 20 20 20 20  K->z,.          
1a2cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a2cc 20 20 20 20 20 20 20 20 20 20 20 20 61 54 65 6d              aTem
1a2cd 70 52 65 63 2c 20 73 69 7a 65 6f 66 28 61 54 65  pRec, sizeof(aTe
1a2ce 6d 70 52 65 63 29 29 3b 0a 20 20 20 20 69 66 28  mpRec));.    if(
1a2cf 20 70 49 64 78 4b 65 79 3d 3d 30 20 29 7b 0a 20   pIdxKey==0 ){. 
1a2d0 20 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d       goto no_mem
1a2d1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 49 64 78  ;.    }.    pIdx
1a2d2 4b 65 79 2d 3e 66 6c 61 67 73 20 7c 3d 20 55 4e  Key->flags |= UN
1a2d3 50 41 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f  PACKED_IGNORE_RO
1a2d4 57 49 44 3b 0a 0a 20 20 20 20 2f 2a 20 53 65 61  WID;..    /* Sea
1a2d5 72 63 68 20 66 6f 72 20 61 6e 20 65 6e 74 72 79  rch for an entry
1a2d6 20 69 6e 20 50 31 20 77 68 65 72 65 20 61 6c 6c   in P1 where all
1a2d7 20 62 75 74 20 74 68 65 20 6c 61 73 74 20 72 6f   but the last ro
1a2d8 77 69 64 20 6d 61 74 63 68 20 4b 0a 20 20 20 20  wid match K.    
1a2d9 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 6e  ** If there is n
1a2da 6f 20 73 75 63 68 20 65 6e 74 72 79 2c 20 6a 75  o such entry, ju
1a2db 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20 74  mp immediately t
1a2dc 6f 20 50 32 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  o P2..    */.   
1a2dd 20 61 73 73 65 72 74 28 20 70 43 78 2d 3e 64 65   assert( pCx->de
1a2de 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20  ferredMoveto==0 
1a2df 29 3b 0a 20 20 20 20 70 43 78 2d 3e 63 61 63 68  );.    pCx->cach
1a2e0 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1a2e1 53 54 41 4c 45 3b 0a 20 20 20 20 72 63 20 3d 20  STALE;.    rc = 
1a2e2 73 71 6c 69 74 65 33 42 74 72 65 65 4d 6f 76 65  sqlite3BtreeMove
1a2e3 74 6f 55 6e 70 61 63 6b 65 64 28 70 43 72 73 72  toUnpacked(pCrsr
1a2e4 2c 20 70 49 64 78 4b 65 79 2c 20 30 2c 20 30 2c  , pIdxKey, 0, 0,
1a2e5 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66 28 20   &res);.    if( 
1a2e6 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc!=SQLITE_OK ){
1a2e7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1a2e8 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64  beDeleteUnpacked
1a2e9 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b  Record(pIdxKey);
1a2ea 0a 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72  .      goto abor
1a2eb 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1a2ec 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 65      }.    if( re
1a2ed 73 3c 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  s<0 ){.      rc 
1a2ee 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4e 65  = sqlite3BtreeNe
1a2ef 78 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b  xt(pCrsr, &res);
1a2f0 0a 20 20 20 20 20 20 69 66 28 20 72 65 73 20 29  .      if( res )
1a2f1 7b 0a 20 20 20 20 20 20 20 20 70 63 20 3d 20 70  {.        pc = p
1a2f2 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20  Op->p2 - 1;.    
1a2f3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 44      sqlite3VdbeD
1a2f4 65 6c 65 74 65 55 6e 70 61 63 6b 65 64 52 65 63  eleteUnpackedRec
1a2f5 6f 72 64 28 70 49 64 78 4b 65 79 29 3b 0a 20 20  ord(pIdxKey);.  
1a2f6 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1a2f7 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 72     }.    }.    r
1a2f8 63 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49  c = sqlite3VdbeI
1a2f9 64 78 4b 65 79 43 6f 6d 70 61 72 65 28 70 43 78  dxKeyCompare(pCx
1a2fa 2c 20 70 49 64 78 4b 65 79 2c 20 26 72 65 73 29  , pIdxKey, &res)
1a2fb 3b 20 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ; .    sqlite3Vd
1a2fc 62 65 44 65 6c 65 74 65 55 6e 70 61 63 6b 65 64  beDeleteUnpacked
1a2fd 52 65 63 6f 72 64 28 70 49 64 78 4b 65 79 29 3b  Record(pIdxKey);
1a2fe 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
1a2ff 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 61 62  ITE_OK ) goto ab
1a300 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1a301 3b 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 20  ;.    if( res>0 
1a302 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1a303 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1a304 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20   break;.    }.. 
1a305 20 20 20 2f 2a 20 41 74 20 74 68 69 73 20 70 6f     /* At this po
1a306 69 6e 74 2c 20 70 43 72 73 72 20 69 73 20 70 6f  int, pCrsr is po
1a307 69 6e 74 69 6e 67 20 74 6f 20 61 6e 20 65 6e 74  inting to an ent
1a308 72 79 20 69 6e 20 50 31 20 77 68 65 72 65 20 61  ry in P1 where a
1a309 6c 6c 20 62 75 74 0a 20 20 20 20 2a 2a 20 74 68  ll but.    ** th
1a30a 65 20 66 69 6e 61 6c 20 65 6e 74 72 79 20 28 74  e final entry (t
1a30b 68 65 20 72 6f 77 69 64 29 20 6d 61 74 63 68 65  he rowid) matche
1a30c 73 20 4b 2e 20 20 43 68 65 63 6b 20 74 6f 20 73  s K.  Check to s
1a30d 65 65 20 69 66 20 74 68 65 0a 20 20 20 20 2a 2a  ee if the.    **
1a30e 20 66 69 6e 61 6c 20 72 6f 77 69 64 20 63 6f 6c   final rowid col
1a30f 75 6d 6e 20 69 73 20 64 69 66 66 65 72 65 6e 74  umn is different
1a310 20 66 72 6f 6d 20 52 2e 20 20 49 66 20 69 74 20   from R.  If it 
1a311 65 71 75 61 6c 73 20 52 20 74 68 65 6e 20 6a 75  equals R then ju
1a312 6d 70 0a 20 20 20 20 2a 2a 20 69 6d 6d 65 64 69  mp.    ** immedi
1a313 61 74 65 6c 79 20 74 6f 20 50 32 2e 0a 20 20 20  ately to P2..   
1a314 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c   */.    rc = sql
1a315 69 74 65 33 56 64 62 65 49 64 78 52 6f 77 69 64  ite3VdbeIdxRowid
1a316 28 70 43 72 73 72 2c 20 26 76 29 3b 0a 20 20 20  (pCrsr, &v);.   
1a317 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1a318 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  OK ){.      goto
1a319 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1a31a 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ror;.    }.    i
1a31b 66 28 20 76 3d 3d 52 20 29 7b 0a 20 20 20 20 20  f( v==R ){.     
1a31c 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1a31d 31 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  1;.      break;.
1a31e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68      }..    /* Th
1a31f 65 20 66 69 6e 61 6c 20 76 61 72 69 6e 74 20 6f  e final varint o
1a320 66 20 74 68 65 20 6b 65 79 20 69 73 20 64 69 66  f the key is dif
1a321 66 65 72 65 6e 74 20 66 72 6f 6d 20 52 2e 20 20  ferent from R.  
1a322 53 74 6f 72 65 20 69 74 20 62 61 63 6b 0a 20 20  Store it back.  
1a323 20 20 2a 2a 20 69 6e 74 6f 20 72 65 67 69 73 74    ** into regist
1a324 65 72 20 52 33 2e 20 20 28 54 68 65 20 72 65 63  er R3.  (The rec
1a325 6f 72 64 20 6e 75 6d 62 65 72 20 6f 66 20 61 6e  ord number of an
1a326 20 65 6e 74 72 79 20 74 68 61 74 20 76 69 6f 6c   entry that viol
1a327 61 74 65 73 0a 20 20 20 20 2a 2a 20 61 20 55 4e  ates.    ** a UN
1a328 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74 2e  IQUE constraint.
1a329 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e  ).    */.    pIn
1a32a 33 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20 20  3->u.i = v;.    
1a32b 61 73 73 65 72 74 28 20 70 49 6e 33 2d 3e 66 6c  assert( pIn3->fl
1a32c 61 67 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20  ags&MEM_Int );. 
1a32d 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1a32e 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f 74 45 78 69  * Opcode: NotExi
1a32f 73 74 73 20 50 31 20 50 32 20 50 33 20 2a 20 2a  sts P1 P2 P3 * *
1a330 0a 2a 2a 0a 2a 2a 20 55 73 65 20 74 68 65 20 63  .**.** Use the c
1a331 6f 6e 74 65 6e 74 20 6f 66 20 72 65 67 69 73 74  ontent of regist
1a332 65 72 20 50 33 20 61 73 20 61 20 69 6e 74 65 67  er P3 as a integ
1a333 65 72 20 6b 65 79 2e 20 20 49 66 20 61 20 72 65  er key.  If a re
1a334 63 6f 72 64 20 0a 2a 2a 20 77 69 74 68 20 74 68  cord .** with th
1a335 61 74 20 6b 65 79 20 64 6f 65 73 20 6e 6f 74 20  at key does not 
1a336 65 78 69 73 74 20 69 6e 20 74 61 62 6c 65 20 6f  exist in table o
1a337 66 20 50 31 2c 20 74 68 65 6e 20 6a 75 6d 70 20  f P1, then jump 
1a338 74 6f 20 50 32 2e 20 0a 2a 2a 20 49 66 20 74 68  to P2. .** If th
1a339 65 20 72 65 63 6f 72 64 20 64 6f 65 73 20 65 78  e record does ex
1a33a 69 73 74 2c 20 74 68 65 6e 20 66 61 6c 6c 20 74  ist, then fall t
1a33b 68 72 75 2e 20 20 54 68 65 20 63 75 72 73 6f 72  hru.  The cursor
1a33c 20 69 73 20 6c 65 66 74 20 0a 2a 2a 20 70 6f 69   is left .** poi
1a33d 6e 74 69 6e 67 20 74 6f 20 74 68 65 20 72 65 63  nting to the rec
1a33e 6f 72 64 20 69 66 20 69 74 20 65 78 69 73 74 73  ord if it exists
1a33f 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 66 66  ..**.** The diff
1a340 65 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74  erence between t
1a341 68 69 73 20 6f 70 65 72 61 74 69 6f 6e 20 61 6e  his operation an
1a342 64 20 4e 6f 74 46 6f 75 6e 64 20 69 73 20 74 68  d NotFound is th
1a343 61 74 20 74 68 69 73 0a 2a 2a 20 6f 70 65 72 61  at this.** opera
1a344 74 69 6f 6e 20 61 73 73 75 6d 65 73 20 74 68 65  tion assumes the
1a345 20 6b 65 79 20 69 73 20 61 6e 20 69 6e 74 65 67   key is an integ
1a346 65 72 20 61 6e 64 20 74 68 61 74 20 50 31 20 69  er and that P1 i
1a347 73 20 61 20 74 61 62 6c 65 20 77 68 65 72 65 61  s a table wherea
1a348 73 0a 2a 2a 20 4e 6f 74 46 6f 75 6e 64 20 61 73  s.** NotFound as
1a349 73 75 6d 65 73 20 6b 65 79 20 69 73 20 61 20 62  sumes key is a b
1a34a 6c 6f 62 20 63 6f 6e 73 74 72 75 63 74 65 64 20  lob constructed 
1a34b 66 72 6f 6d 20 4d 61 6b 65 52 65 63 6f 72 64 20  from MakeRecord 
1a34c 61 6e 64 0a 2a 2a 20 50 31 20 69 73 20 61 6e 20  and.** P1 is an 
1a34d 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  index..**.** See
1a34e 20 61 6c 73 6f 3a 20 46 6f 75 6e 64 2c 20 4e 6f   also: Found, No
1a34f 74 46 6f 75 6e 64 2c 20 49 73 55 6e 69 71 75 65  tFound, IsUnique
1a350 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 6f 74 45  .*/.case OP_NotE
1a351 78 69 73 74 73 3a 20 7b 20 20 20 20 20 20 20 20  xists: {        
1a352 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20 2a 2f 0a  /* jump, in3 */.
1a353 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1a354 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1a355 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1a356 2a 70 43 72 73 72 3b 0a 20 20 61 73 73 65 72 74  *pCrsr;.  assert
1a357 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1a358 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73 65  Cursor );.  asse
1a359 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 21  rt( p->apCsr[i]!
1a35a 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72  =0 );.  if( (pCr
1a35b 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70  sr = (pC = p->ap
1a35c 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72  Csr[i])->pCursor
1a35d 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20  )!=0 ){.    int 
1a35e 72 65 73 3b 0a 20 20 20 20 75 36 34 20 69 4b 65  res;.    u64 iKe
1a35f 79 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  y;.    assert( p
1a360 49 6e 33 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d  In3->flags & MEM
1a361 5f 49 6e 74 20 29 3b 0a 20 20 20 20 61 73 73 65  _Int );.    asse
1a362 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d 2d  rt( p->apCsr[i]-
1a363 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  >isTable );.    
1a364 69 4b 65 79 20 3d 20 69 6e 74 54 6f 4b 65 79 28  iKey = intToKey(
1a365 70 49 6e 33 2d 3e 75 2e 69 29 3b 0a 20 20 20 20  pIn3->u.i);.    
1a366 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1a367 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b 65 64 28  eMovetoUnpacked(
1a368 70 43 72 73 72 2c 20 30 2c 20 69 4b 65 79 2c 20  pCrsr, 0, iKey, 
1a369 30 2c 26 72 65 73 29 3b 0a 20 20 20 20 70 43 2d  0,&res);.    pC-
1a36a 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 49 6e  >lastRowid = pIn
1a36b 33 2d 3e 75 2e 69 3b 0a 20 20 20 20 70 43 2d 3e  3->u.i;.    pC->
1a36c 72 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 72  rowidIsValid = r
1a36d 65 73 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e 6e  es==0;.    pC->n
1a36e 75 6c 6c 52 6f 77 20 3d 20 30 3b 0a 20 20 20 20  ullRow = 0;.    
1a36f 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20  pC->cacheStatus 
1a370 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20  = CACHE_STALE;. 
1a371 20 20 20 2f 2a 20 72 65 73 20 6d 69 67 68 74 20     /* res might 
1a372 62 65 20 75 6e 69 6e 69 74 69 61 6c 69 7a 65 64  be uninitialized
1a373 20 69 66 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f   if rc!=SQLITE_O
1a374 4b 2e 20 20 42 75 74 20 69 66 20 72 63 21 3d 53  K.  But if rc!=S
1a375 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 20 2a 2a 20  QLITE_OK.    ** 
1a376 70 72 6f 63 65 73 73 69 6e 67 20 69 73 20 61 62  processing is ab
1a377 6f 75 74 20 74 6f 20 61 62 6f 72 74 20 73 6f 20  out to abort so 
1a378 77 65 20 72 65 61 6c 6c 79 20 64 6f 20 6e 6f 74  we really do not
1a379 20 63 61 72 65 20 77 68 65 74 68 65 72 20 6f 72   care whether or
1a37a 20 6e 6f 74 0a 20 20 20 20 2a 2a 20 74 68 65 20   not.    ** the 
1a37b 66 6f 6c 6c 6f 77 69 6e 67 20 6a 75 6d 70 20 69  following jump i
1a37c 73 20 74 61 6b 65 6e 2e 20 20 28 49 6e 20 6f 74  s taken.  (In ot
1a37d 68 65 72 20 77 6f 72 64 73 2c 20 64 6f 20 6e 6f  her words, do no
1a37e 74 20 73 74 72 65 73 73 20 6f 76 65 72 0a 20 20  t stress over.  
1a37f 20 20 2a 2a 20 74 68 65 20 65 72 72 6f 72 20 74    ** the error t
1a380 68 61 74 20 76 61 6c 67 72 69 6e 64 20 73 6f 6d  hat valgrind som
1a381 65 74 69 6d 65 73 20 73 68 6f 77 73 20 6f 6e 20  etimes shows on 
1a382 74 68 65 20 6e 65 78 74 20 73 74 61 74 65 6d 65  the next stateme
1a383 6e 74 20 77 68 65 6e 0a 20 20 20 20 2a 2a 20 72  nt when.    ** r
1a384 75 6e 6e 69 6e 67 20 69 6f 65 72 72 2e 74 65 73  unning ioerr.tes
1a385 74 20 61 6e 64 20 73 69 6d 69 6c 61 72 20 66 61  t and similar fa
1a386 69 6c 75 72 65 2d 72 65 63 6f 76 65 72 79 20 74  ilure-recovery t
1a387 65 73 74 20 73 63 72 69 70 74 73 2e 29 20 2a 2f  est scripts.) */
1a388 0a 20 20 20 20 69 66 28 20 72 65 73 21 3d 30 20  .    if( res!=0 
1a389 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1a38a 70 2d 3e 70 32 20 2d 20 31 3b 0a 20 20 20 20 20  p->p2 - 1;.     
1a38b 20 61 73 73 65 72 74 28 20 70 43 2d 3e 72 6f 77   assert( pC->row
1a38c 69 64 49 73 56 61 6c 69 64 3d 3d 30 20 29 3b 0a  idIsValid==0 );.
1a38d 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69 66      }.  }else if
1a38e 28 20 21 70 43 2d 3e 70 73 65 75 64 6f 54 61 62  ( !pC->pseudoTab
1a38f 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69  le ){.    /* Thi
1a390 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20 61  s happens when a
1a391 6e 20 61 74 74 65 6d 70 74 20 74 6f 20 6f 70 65  n attempt to ope
1a392 6e 20 61 20 72 65 61 64 20 63 75 72 73 6f 72 20  n a read cursor 
1a393 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a 20 73  on the .    ** s
1a394 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1a395 6c 65 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54  le returns SQLIT
1a396 45 5f 45 4d 50 54 59 2e 0a 20 20 20 20 2a 2f 0a  E_EMPTY..    */.
1a397 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1a398 69 73 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 70  isTable );.    p
1a399 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b  c = pOp->p2 - 1;
1a39a 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1a39b 3e 72 6f 77 69 64 49 73 56 61 6c 69 64 3d 3d 30  >rowidIsValid==0
1a39c 20 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b   );.  }.  break;
1a39d 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53  .}../* Opcode: S
1a39e 65 71 75 65 6e 63 65 20 50 31 20 50 32 20 2a 20  equence P1 P2 * 
1a39f 2a 20 2a 0a 2a 2a 0a 2a 2a 20 46 69 6e 64 20 74  * *.**.** Find t
1a3a0 68 65 20 6e 65 78 74 20 61 76 61 69 6c 61 62 6c  he next availabl
1a3a1 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
1a3a2 72 20 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e  r for cursor P1.
1a3a3 0a 2a 2a 20 57 72 69 74 65 20 74 68 65 20 73 65  .** Write the se
1a3a4 71 75 65 6e 63 65 20 6e 75 6d 62 65 72 20 69 6e  quence number in
1a3a5 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a  to register P2..
1a3a6 2a 2a 20 54 68 65 20 73 65 71 75 65 6e 63 65 20  ** The sequence 
1a3a7 6e 75 6d 62 65 72 20 6f 6e 20 74 68 65 20 63 75  number on the cu
1a3a8 72 73 6f 72 20 69 73 20 69 6e 63 72 65 6d 65 6e  rsor is incremen
1a3a9 74 65 64 20 61 66 74 65 72 20 74 68 69 73 0a 2a  ted after this.*
1a3aa 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20  * instruction.  
1a3ab 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 53 65 71 75  .*/.case OP_Sequ
1a3ac 65 6e 63 65 3a 20 7b 20 20 20 20 20 20 20 20 20  ence: {         
1a3ad 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1a3ae 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20  ease */.  int i 
1a3af 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61 73 73  = pOp->p1;.  ass
1a3b0 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70  ert( i>=0 && i<p
1a3b1 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  ->nCursor );.  a
1a3b2 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b  ssert( p->apCsr[
1a3b3 69 5d 21 3d 30 20 29 3b 0a 20 20 70 4f 75 74 2d  i]!=0 );.  pOut-
1a3b4 3e 75 2e 69 20 3d 20 70 2d 3e 61 70 43 73 72 5b  >u.i = p->apCsr[
1a3b5 69 5d 2d 3e 73 65 71 43 6f 75 6e 74 2b 2b 3b 0a  i]->seqCount++;.
1a3b6 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1a3b7 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1a3b8 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 2f 2a  .  break;.}.../*
1a3b9 20 4f 70 63 6f 64 65 3a 20 4e 65 77 52 6f 77 69   Opcode: NewRowi
1a3ba 64 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  d P1 P2 P3 * *.*
1a3bb 2a 0a 2a 2a 20 47 65 74 20 61 20 6e 65 77 20 69  *.** Get a new i
1a3bc 6e 74 65 67 65 72 20 72 65 63 6f 72 64 20 6e 75  nteger record nu
1a3bd 6d 62 65 72 20 28 61 2e 6b 2e 61 20 22 72 6f 77  mber (a.k.a "row
1a3be 69 64 22 29 20 75 73 65 64 20 61 73 20 74 68 65  id") used as the
1a3bf 20 6b 65 79 20 74 6f 20 61 20 74 61 62 6c 65 2e   key to a table.
1a3c0 0a 2a 2a 20 54 68 65 20 72 65 63 6f 72 64 20 6e  .** The record n
1a3c1 75 6d 62 65 72 20 69 73 20 6e 6f 74 20 70 72 65  umber is not pre
1a3c2 76 69 6f 75 73 6c 79 20 75 73 65 64 20 61 73 20  viously used as 
1a3c3 61 20 6b 65 79 20 69 6e 20 74 68 65 20 64 61 74  a key in the dat
1a3c4 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 74  abase.** table t
1a3c5 68 61 74 20 63 75 72 73 6f 72 20 50 31 20 70 6f  hat cursor P1 po
1a3c6 69 6e 74 73 20 74 6f 2e 20 20 54 68 65 20 6e 65  ints to.  The ne
1a3c7 77 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  w record number 
1a3c8 69 73 20 77 72 69 74 74 65 6e 0a 2a 2a 20 77 72  is written.** wr
1a3c9 69 74 74 65 6e 20 74 6f 20 72 65 67 69 73 74 65  itten to registe
1a3ca 72 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  r P2..**.** If P
1a3cb 33 3e 30 20 74 68 65 6e 20 50 33 20 69 73 20 61  3>0 then P3 is a
1a3cc 20 72 65 67 69 73 74 65 72 20 74 68 61 74 20 68   register that h
1a3cd 6f 6c 64 73 20 74 68 65 20 6c 61 72 67 65 73 74  olds the largest
1a3ce 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a 2a 20 67   previously.** g
1a3cf 65 6e 65 72 61 74 65 64 20 72 65 63 6f 72 64 20  enerated record 
1a3d0 6e 75 6d 62 65 72 2e 20 20 4e 6f 20 6e 65 77 20  number.  No new 
1a3d1 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 73 20 61  record numbers a
1a3d2 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62 65  re allowed to be
1a3d3 20 6c 65 73 73 0a 2a 2a 20 74 68 61 6e 20 74 68   less.** than th
1a3d4 69 73 20 76 61 6c 75 65 2e 20 20 57 68 65 6e 20  is value.  When 
1a3d5 74 68 69 73 20 76 61 6c 75 65 20 72 65 61 63 68  this value reach
1a3d6 65 73 20 69 74 73 20 6d 61 78 69 6d 75 6d 2c 20  es its maximum, 
1a3d7 61 20 53 51 4c 49 54 45 5f 46 55 4c 4c 0a 2a 2a  a SQLITE_FULL.**
1a3d8 20 65 72 72 6f 72 20 69 73 20 67 65 6e 65 72 61   error is genera
1a3d9 74 65 64 2e 20 20 54 68 65 20 50 33 20 72 65 67  ted.  The P3 reg
1a3da 69 73 74 65 72 20 69 73 20 75 70 64 61 74 65 64  ister is updated
1a3db 20 77 69 74 68 20 74 68 65 20 67 65 6e 65 72 61   with the genera
1a3dc 74 65 64 0a 2a 2a 20 72 65 63 6f 72 64 20 6e 75  ted.** record nu
1a3dd 6d 62 65 72 2e 20 20 54 68 69 73 20 50 33 20 6d  mber.  This P3 m
1a3de 65 63 68 61 6e 69 73 6d 20 69 73 20 75 73 65 64  echanism is used
1a3df 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65   to help impleme
1a3e0 6e 74 20 74 68 65 0a 2a 2a 20 41 55 54 4f 49 4e  nt the.** AUTOIN
1a3e1 43 52 45 4d 45 4e 54 20 66 65 61 74 75 72 65 2e  CREMENT feature.
1a3e2 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 4e 65 77 52  .*/.case OP_NewR
1a3e3 6f 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20  owid: {         
1a3e4 20 20 2f 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c    /* out2-prerel
1a3e5 65 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69 20  ease */.  int i 
1a3e6 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34  = pOp->p1;.  i64
1a3e7 20 76 20 3d 20 30 3b 0a 20 20 56 64 62 65 43 75   v = 0;.  VdbeCu
1a3e8 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1a3e9 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1a3ea 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73  >nCursor );.  as
1a3eb 73 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69  sert( p->apCsr[i
1a3ec 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70  ]!=0 );.  if( (p
1a3ed 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 29  C = p->apCsr[i])
1a3ee 2d 3e 70 43 75 72 73 6f 72 3d 3d 30 20 29 7b 0a  ->pCursor==0 ){.
1a3ef 20 20 20 20 2f 2a 20 54 68 65 20 7a 65 72 6f 20      /* The zero 
1a3f0 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 61  initialization a
1a3f1 62 6f 76 65 20 69 73 20 61 6c 6c 20 74 68 61 74  bove is all that
1a3f2 20 69 73 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20   is needed */.  
1a3f3 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 54 68  }else{.    /* Th
1a3f4 65 20 6e 65 78 74 20 72 6f 77 69 64 20 6f 72 20  e next rowid or 
1a3f5 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 28 64  record number (d
1a3f6 69 66 66 65 72 65 6e 74 20 74 65 72 6d 73 20 66  ifferent terms f
1a3f7 6f 72 20 74 68 65 20 73 61 6d 65 0a 20 20 20 20  or the same.    
1a3f8 2a 2a 20 74 68 69 6e 67 29 20 69 73 20 6f 62 74  ** thing) is obt
1a3f9 61 69 6e 65 64 20 69 6e 20 61 20 74 77 6f 2d 73  ained in a two-s
1a3fa 74 65 70 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 20  tep algorithm.. 
1a3fb 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 69 72     **.    ** Fir
1a3fc 73 74 20 77 65 20 61 74 74 65 6d 70 74 20 74 6f  st we attempt to
1a3fd 20 66 69 6e 64 20 74 68 65 20 6c 61 72 67 65 73   find the larges
1a3fe 74 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64  t existing rowid
1a3ff 20 61 6e 64 20 61 64 64 20 6f 6e 65 0a 20 20 20   and add one.   
1a400 20 2a 2a 20 74 6f 20 74 68 61 74 2e 20 20 42 75   ** to that.  Bu
1a401 74 20 69 66 20 74 68 65 20 6c 61 72 67 65 73 74  t if the largest
1a402 20 65 78 69 73 74 69 6e 67 20 72 6f 77 69 64 20   existing rowid 
1a403 69 73 20 61 6c 72 65 61 64 79 20 74 68 65 20 6d  is already the m
1a404 61 78 69 6d 75 6d 0a 20 20 20 20 2a 2a 20 70 6f  aximum.    ** po
1a405 73 69 74 69 76 65 20 69 6e 74 65 67 65 72 2c 20  sitive integer, 
1a406 77 65 20 68 61 76 65 20 74 6f 20 66 61 6c 6c 20  we have to fall 
1a407 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20 73  through to the s
1a408 65 63 6f 6e 64 0a 20 20 20 20 2a 2a 20 70 72 6f  econd.    ** pro
1a409 62 61 62 69 6c 69 73 74 69 63 20 61 6c 67 6f 72  babilistic algor
1a40a 69 74 68 6d 0a 20 20 20 20 2a 2a 0a 20 20 20 20  ithm.    **.    
1a40b 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 61 6c  ** The second al
1a40c 67 6f 72 69 74 68 6d 20 69 73 20 74 6f 20 73 65  gorithm is to se
1a40d 6c 65 63 74 20 61 20 72 6f 77 69 64 20 61 74 20  lect a rowid at 
1a40e 72 61 6e 64 6f 6d 20 61 6e 64 20 73 65 65 20 69  random and see i
1a40f 66 0a 20 20 20 20 2a 2a 20 69 74 20 61 6c 72 65  f.    ** it alre
1a410 61 64 79 20 65 78 69 73 74 73 20 69 6e 20 74 68  ady exists in th
1a411 65 20 74 61 62 6c 65 2e 20 20 49 66 20 69 74 20  e table.  If it 
1a412 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c 20  does not exist, 
1a413 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 73  we have.    ** s
1a414 75 63 63 65 65 64 65 64 2e 20 20 49 66 20 74 68  ucceeded.  If th
1a415 65 20 72 61 6e 64 6f 6d 20 72 6f 77 69 64 20 64  e random rowid d
1a416 6f 65 73 20 65 78 69 73 74 2c 20 77 65 20 73 65  oes exist, we se
1a417 6c 65 63 74 20 61 20 6e 65 77 20 6f 6e 65 0a 20  lect a new one. 
1a418 20 20 20 2a 2a 20 61 6e 64 20 74 72 79 20 61 67     ** and try ag
1a419 61 69 6e 2c 20 75 70 20 74 6f 20 31 30 30 30 20  ain, up to 1000 
1a41a 74 69 6d 65 73 2e 0a 20 20 20 20 2a 2a 0a 20 20  times..    **.  
1a41b 20 20 2a 2a 20 46 6f 72 20 61 20 74 61 62 6c 65    ** For a table
1a41c 20 77 69 74 68 20 6c 65 73 73 20 74 68 61 6e 20   with less than 
1a41d 32 20 62 69 6c 6c 69 6f 6e 20 65 6e 74 72 69 65  2 billion entrie
1a41e 73 2c 20 74 68 65 20 70 72 6f 62 61 62 69 6c 69  s, the probabili
1a41f 74 79 0a 20 20 20 20 2a 2a 20 6f 66 20 6e 6f 74  ty.    ** of not
1a420 20 66 69 6e 64 69 6e 67 20 61 20 75 6e 75 73 65   finding a unuse
1a421 64 20 72 6f 77 69 64 20 69 73 20 61 62 6f 75 74  d rowid is about
1a422 20 31 2e 30 65 2d 33 30 30 2e 20 20 54 68 69 73   1.0e-300.  This
1a423 20 69 73 20 61 20 0a 20 20 20 20 2a 2a 20 6e 6f   is a .    ** no
1a424 6e 2d 7a 65 72 6f 20 70 72 6f 62 61 62 69 6c 69  n-zero probabili
1a425 74 79 2c 20 62 75 74 20 69 74 20 69 73 20 73 74  ty, but it is st
1a426 69 6c 6c 20 76 61 6e 69 73 68 69 6e 67 6c 79 20  ill vanishingly 
1a427 73 6d 61 6c 6c 20 61 6e 64 20 73 68 6f 75 6c 64  small and should
1a428 0a 20 20 20 20 2a 2a 20 6e 65 76 65 72 20 63 61  .    ** never ca
1a429 75 73 65 20 61 20 70 72 6f 62 6c 65 6d 2e 20 20  use a problem.  
1a42a 59 6f 75 20 61 72 65 20 6d 75 63 68 2c 20 6d 75  You are much, mu
1a42b 63 68 20 6d 6f 72 65 20 6c 69 6b 65 6c 79 20 74  ch more likely t
1a42c 6f 20 68 61 76 65 20 61 0a 20 20 20 20 2a 2a 20  o have a.    ** 
1a42d 68 61 72 64 77 61 72 65 20 66 61 69 6c 75 72 65  hardware failure
1a42e 20 74 68 61 6e 20 66 6f 72 20 74 68 69 73 20 61   than for this a
1a42f 6c 67 6f 72 69 74 68 6d 20 74 6f 20 66 61 69 6c  lgorithm to fail
1a430 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  ..    **.    ** 
1a431 54 68 65 20 61 6e 61 6c 79 73 69 73 20 69 6e 20  The analysis in 
1a432 74 68 65 20 70 72 65 76 69 6f 75 73 20 70 61 72  the previous par
1a433 61 67 72 61 70 68 20 61 73 73 75 6d 65 73 20 74  agraph assumes t
1a434 68 61 74 20 79 6f 75 20 68 61 76 65 20 61 20 67  hat you have a g
1a435 6f 6f 64 0a 20 20 20 20 2a 2a 20 73 6f 75 72 63  ood.    ** sourc
1a436 65 20 6f 66 20 72 61 6e 64 6f 6d 20 6e 75 6d 62  e of random numb
1a437 65 72 73 2e 20 20 49 73 20 61 20 6c 69 62 72 61  ers.  Is a libra
1a438 72 79 20 66 75 6e 63 74 69 6f 6e 20 6c 69 6b 65  ry function like
1a439 20 6c 72 61 6e 64 34 38 28 29 0a 20 20 20 20 2a   lrand48().    *
1a43a 2a 20 67 6f 6f 64 20 65 6e 6f 75 67 68 3f 20 20  * good enough?  
1a43b 4d 61 79 62 65 2e 20 4d 61 79 62 65 20 6e 6f 74  Maybe. Maybe not
1a43c 2e 20 49 74 27 73 20 68 61 72 64 20 74 6f 20 6b  . It's hard to k
1a43d 6e 6f 77 20 77 68 65 74 68 65 72 20 74 68 65 72  now whether ther
1a43e 65 0a 20 20 20 20 2a 2a 20 6d 69 67 68 74 20 62  e.    ** might b
1a43f 65 20 73 75 62 74 6c 65 20 62 75 67 73 20 69 73  e subtle bugs is
1a440 20 73 6f 6d 65 20 69 6d 70 6c 65 6d 65 6e 74 61   some implementa
1a441 74 69 6f 6e 73 20 6f 66 20 6c 72 61 6e 64 34 38  tions of lrand48
1a442 28 29 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 63  () that.    ** c
1a443 6f 75 6c 64 20 63 61 75 73 65 20 70 72 6f 62 6c  ould cause probl
1a444 65 6d 73 2e 20 54 6f 20 61 76 6f 69 64 20 75 6e  ems. To avoid un
1a445 63 65 72 74 61 69 6e 74 79 2c 20 53 51 4c 69 74  certainty, SQLit
1a446 65 20 75 73 65 73 20 69 74 73 20 6f 77 6e 20 0a  e uses its own .
1a447 20 20 20 20 2a 2a 20 72 61 6e 64 6f 6d 20 6e 75      ** random nu
1a448 6d 62 65 72 20 67 65 6e 65 72 61 74 6f 72 20 62  mber generator b
1a449 61 73 65 64 20 6f 6e 20 74 68 65 20 52 43 34 20  ased on the RC4 
1a44a 61 6c 67 6f 72 69 74 68 6d 2e 0a 20 20 20 20 2a  algorithm..    *
1a44b 2a 0a 20 20 20 20 2a 2a 20 54 6f 20 70 72 6f 6d  *.    ** To prom
1a44c 6f 74 65 20 6c 6f 63 61 6c 69 74 79 20 6f 66 20  ote locality of 
1a44d 72 65 66 65 72 65 6e 63 65 20 66 6f 72 20 72 65  reference for re
1a44e 70 65 74 69 74 69 76 65 20 69 6e 73 65 72 74 73  petitive inserts
1a44f 2c 20 74 68 65 0a 20 20 20 20 2a 2a 20 66 69 72  , the.    ** fir
1a450 73 74 20 66 65 77 20 61 74 74 65 6d 70 74 73 20  st few attempts 
1a451 61 74 20 63 68 6f 6f 73 69 6e 67 20 61 20 72 61  at choosing a ra
1a452 6e 64 6f 6d 20 72 6f 77 69 64 20 70 69 63 6b 20  ndom rowid pick 
1a453 76 61 6c 75 65 73 20 6a 75 73 74 20 61 20 6c 69  values just a li
1a454 74 74 6c 65 0a 20 20 20 20 2a 2a 20 6c 61 72 67  ttle.    ** larg
1a455 65 72 20 74 68 61 6e 20 74 68 65 20 70 72 65 76  er than the prev
1a456 69 6f 75 73 20 72 6f 77 69 64 2e 20 20 54 68 69  ious rowid.  Thi
1a457 73 20 68 61 73 20 62 65 65 6e 20 73 68 6f 77 6e  s has been shown
1a458 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 6c 79 0a   experimentally.
1a459 20 20 20 20 2a 2a 20 74 6f 20 64 6f 75 62 6c 65      ** to double
1a45a 20 74 68 65 20 73 70 65 65 64 20 6f 66 20 74 68   the speed of th
1a45b 65 20 43 4f 50 59 20 6f 70 65 72 61 74 69 6f 6e  e COPY operation
1a45c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  ..    */.    int
1a45d 20 72 65 73 2c 20 72 78 3d 53 51 4c 49 54 45 5f   res, rx=SQLITE_
1a45e 4f 4b 2c 20 63 6e 74 3b 0a 20 20 20 20 69 36 34  OK, cnt;.    i64
1a45f 20 78 3b 0a 20 20 20 20 63 6e 74 20 3d 20 30 3b   x;.    cnt = 0;
1a460 0a 20 20 20 20 69 66 28 20 28 73 71 6c 69 74 65  .    if( (sqlite
1a461 33 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e  3BtreeFlags(pC->
1a462 70 43 75 72 73 6f 72 29 26 28 42 54 52 45 45 5f  pCursor)&(BTREE_
1a463 49 4e 54 4b 45 59 7c 42 54 52 45 45 5f 5a 45 52  INTKEY|BTREE_ZER
1a464 4f 44 41 54 41 29 29 20 21 3d 0a 20 20 20 20 20  ODATA)) !=.     
1a465 20 20 20 20 20 42 54 52 45 45 5f 49 4e 54 4b 45       BTREE_INTKE
1a466 59 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  Y ){.      rc = 
1a467 53 51 4c 49 54 45 5f 43 4f 52 52 55 50 54 5f 42  SQLITE_CORRUPT_B
1a468 4b 50 54 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20  KPT;.      goto 
1a469 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72  abort_due_to_err
1a46a 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  or;.    }.    as
1a46b 73 65 72 74 28 20 28 73 71 6c 69 74 65 33 42 74  sert( (sqlite3Bt
1a46c 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70 43 75  reeFlags(pC->pCu
1a46d 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f 49 4e  rsor) & BTREE_IN
1a46e 54 4b 45 59 29 21 3d 30 20 29 3b 0a 20 20 20 20  TKEY)!=0 );.    
1a46f 61 73 73 65 72 74 28 20 28 73 71 6c 69 74 65 33  assert( (sqlite3
1a470 42 74 72 65 65 46 6c 61 67 73 28 70 43 2d 3e 70  BtreeFlags(pC->p
1a471 43 75 72 73 6f 72 29 20 26 20 42 54 52 45 45 5f  Cursor) & BTREE_
1a472 5a 45 52 4f 44 41 54 41 29 3d 3d 30 20 29 3b 0a  ZERODATA)==0 );.
1a473 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 33  .#ifdef SQLITE_3
1a474 32 42 49 54 5f 52 4f 57 49 44 0a 23 20 20 20 64  2BIT_ROWID.#   d
1a475 65 66 69 6e 65 20 4d 41 58 5f 52 4f 57 49 44 20  efine MAX_ROWID 
1a476 30 78 37 66 66 66 66 66 66 66 0a 23 65 6c 73 65  0x7fffffff.#else
1a477 0a 20 20 20 20 2f 2a 20 53 6f 6d 65 20 63 6f 6d  .    /* Some com
1a478 70 69 6c 65 72 73 20 63 6f 6d 70 6c 61 69 6e 20  pilers complain 
1a479 61 62 6f 75 74 20 63 6f 6e 73 74 61 6e 74 73 20  about constants 
1a47a 6f 66 20 74 68 65 20 66 6f 72 6d 20 30 78 37 66  of the form 0x7f
1a47b 66 66 66 66 66 66 66 66 66 66 66 66 66 66 2e 0a  ffffffffffffff..
1a47c 20 20 20 20 2a 2a 20 4f 74 68 65 72 73 20 63 6f      ** Others co
1a47d 6d 70 6c 61 69 6e 20 61 62 6f 75 74 20 30 78 37  mplain about 0x7
1a47e 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66 66  ffffffffffffffff
1a47f 4c 4c 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  LL.  The followi
1a480 6e 67 20 6d 61 63 72 6f 20 73 65 65 6d 73 0a 20  ng macro seems. 
1a481 20 20 20 2a 2a 20 74 6f 20 70 72 6f 76 69 64 65     ** to provide
1a482 20 74 68 65 20 63 6f 6e 73 74 61 6e 74 20 77 68   the constant wh
1a483 69 6c 65 20 6d 61 6b 69 6e 67 20 61 6c 6c 20 63  ile making all c
1a484 6f 6d 70 69 6c 65 72 73 20 68 61 70 70 79 2e 0a  ompilers happy..
1a485 20 20 20 20 2a 2f 0a 23 20 20 20 64 65 66 69 6e      */.#   defin
1a486 65 20 4d 41 58 5f 52 4f 57 49 44 20 20 28 69 36  e MAX_ROWID  (i6
1a487 34 29 28 20 28 28 28 75 36 34 29 30 78 37 66 66  4)( (((u64)0x7ff
1a488 66 66 66 66 66 29 3c 3c 33 32 29 20 7c 20 28 75  fffff)<<32) | (u
1a489 36 34 29 30 78 66 66 66 66 66 66 66 66 20 29 0a  64)0xffffffff ).
1a48a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28 20  #endif..    if( 
1a48b 21 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d 52 6f  !pC->useRandomRo
1a48c 77 69 64 20 29 7b 0a 20 20 20 20 20 20 69 66 28  wid ){.      if(
1a48d 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61   pC->nextRowidVa
1a48e 6c 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 76  lid ){.        v
1a48f 20 3d 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64   = pC->nextRowid
1a490 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
1a491 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69         rc = sqli
1a492 74 65 33 42 74 72 65 65 4c 61 73 74 28 70 43 2d  te3BtreeLast(pC-
1a493 3e 70 43 75 72 73 6f 72 2c 20 26 72 65 73 29 3b  >pCursor, &res);
1a494 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
1a495 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1a496 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61 62 6f          goto abo
1a497 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b  rt_due_to_error;
1a498 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1a499 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a 20 20     if( res ){.  
1a49a 20 20 20 20 20 20 20 20 76 20 3d 20 31 3b 0a 20          v = 1;. 
1a49b 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1a49c 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
1a49d 74 72 65 65 4b 65 79 53 69 7a 65 28 70 43 2d 3e  treeKeySize(pC->
1a49e 70 43 75 72 73 6f 72 2c 20 26 76 29 3b 0a 20 20  pCursor, &v);.  
1a49f 20 20 20 20 20 20 20 20 76 20 3d 20 6b 65 79 54          v = keyT
1a4a0 6f 49 6e 74 28 76 29 3b 0a 20 20 20 20 20 20 20  oInt(v);.       
1a4a1 20 20 20 69 66 28 20 76 3d 3d 4d 41 58 5f 52 4f     if( v==MAX_RO
1a4a2 57 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 20  WID ){.         
1a4a3 20 20 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d     pC->useRandom
1a4a4 52 6f 77 69 64 20 3d 20 31 3b 0a 20 20 20 20 20  Rowid = 1;.     
1a4a5 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1a4a6 20 20 20 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20          v++;.   
1a4a7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1a4a8 20 7d 0a 20 20 20 20 20 20 7d 0a 0a 23 69 66 6e   }.      }..#ifn
1a4a9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1a4aa 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20  AUTOINCREMENT.  
1a4ab 20 20 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20      if( pOp->p3 
1a4ac 29 7b 0a 20 20 20 20 20 20 20 20 4d 65 6d 20 2a  ){.        Mem *
1a4ad 70 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 61 73  pMem;.        as
1a4ae 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30 20  sert( pOp->p3>0 
1a4af 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e 6e  && pOp->p3<=p->n
1a4b0 4d 65 6d 20 29 3b 20 2f 2a 20 50 33 20 69 73 20  Mem ); /* P3 is 
1a4b1 61 20 76 61 6c 69 64 20 6d 65 6d 6f 72 79 20 63  a valid memory c
1a4b2 65 6c 6c 20 2a 2f 0a 20 20 20 20 20 20 20 20 70  ell */.        p
1a4b3 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1a4b4 4f 70 2d 3e 70 33 5d 3b 0a 09 52 45 47 49 53 54  Op->p3];..REGIST
1a4b5 45 52 5f 54 52 41 43 45 28 70 4f 70 2d 3e 70 33  ER_TRACE(pOp->p3
1a4b6 2c 20 70 4d 65 6d 29 3b 0a 20 20 20 20 20 20 20  , pMem);.       
1a4b7 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 49   sqlite3VdbeMemI
1a4b8 6e 74 65 67 65 72 69 66 79 28 70 4d 65 6d 29 3b  ntegerify(pMem);
1a4b9 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1a4ba 20 28 70 4d 65 6d 2d 3e 66 6c 61 67 73 20 26 20   (pMem->flags & 
1a4bb 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 20 20  MEM_Int)!=0 );  
1a4bc 2f 2a 20 6d 65 6d 28 50 33 29 20 68 6f 6c 64 73  /* mem(P3) holds
1a4bd 20 61 6e 20 69 6e 74 65 67 65 72 20 2a 2f 0a 20   an integer */. 
1a4be 20 20 20 20 20 20 20 69 66 28 20 70 4d 65 6d 2d         if( pMem-
1a4bf 3e 75 2e 69 3d 3d 4d 41 58 5f 52 4f 57 49 44 20  >u.i==MAX_ROWID 
1a4c0 7c 7c 20 70 43 2d 3e 75 73 65 52 61 6e 64 6f 6d  || pC->useRandom
1a4c1 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20 20  Rowid ){.       
1a4c2 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46     rc = SQLITE_F
1a4c3 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 20 20 67  ULL;.          g
1a4c4 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1a4c5 5f 65 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  _error;.        
1a4c6 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 76 3c  }.        if( v<
1a4c7 70 4d 65 6d 2d 3e 75 2e 69 2b 31 20 29 7b 0a 20  pMem->u.i+1 ){. 
1a4c8 20 20 20 20 20 20 20 20 20 76 20 3d 20 70 4d 65           v = pMe
1a4c9 6d 2d 3e 75 2e 69 20 2b 20 31 3b 0a 20 20 20 20  m->u.i + 1;.    
1a4ca 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d      }.        pM
1a4cb 65 6d 2d 3e 75 2e 69 20 3d 20 76 3b 0a 20 20 20  em->u.i = v;.   
1a4cc 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20     }.#endif..   
1a4cd 20 20 20 69 66 28 20 76 3c 4d 41 58 5f 52 4f 57     if( v<MAX_ROW
1a4ce 49 44 20 29 7b 0a 20 20 20 20 20 20 20 20 70 43  ID ){.        pC
1a4cf 2d 3e 6e 65 78 74 52 6f 77 69 64 56 61 6c 69 64  ->nextRowidValid
1a4d0 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 70 43   = 1;.        pC
1a4d1 2d 3e 6e 65 78 74 52 6f 77 69 64 20 3d 20 76 2b  ->nextRowid = v+
1a4d2 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  1;.      }else{.
1a4d3 20 20 20 20 20 20 20 20 70 43 2d 3e 6e 65 78 74          pC->next
1a4d4 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a  RowidValid = 0;.
1a4d5 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1a4d6 20 20 69 66 28 20 70 43 2d 3e 75 73 65 52 61 6e    if( pC->useRan
1a4d7 64 6f 6d 52 6f 77 69 64 20 29 7b 0a 20 20 20 20  domRowid ){.    
1a4d8 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1a4d9 33 3d 3d 30 20 29 3b 20 20 2f 2a 20 53 51 4c 49  3==0 );  /* SQLI
1a4da 54 45 5f 46 55 4c 4c 20 6d 75 73 74 20 68 61 76  TE_FULL must hav
1a4db 65 20 6f 63 63 75 72 72 65 64 20 70 72 69 6f 72  e occurred prior
1a4dc 20 74 6f 20 74 68 69 73 20 2a 2f 0a 20 20 20 20   to this */.    
1a4dd 20 20 76 20 3d 20 64 62 2d 3e 70 72 69 6f 72 4e    v = db->priorN
1a4de 65 77 52 6f 77 69 64 3b 0a 20 20 20 20 20 20 63  ewRowid;.      c
1a4df 6e 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 6f  nt = 0;.      do
1a4e0 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6e  {.        if( cn
1a4e1 74 3d 3d 30 20 26 26 20 28 76 26 30 78 66 66 66  t==0 && (v&0xfff
1a4e2 66 66 66 29 3d 3d 76 20 29 7b 0a 20 20 20 20 20  fff)==v ){.     
1a4e3 20 20 20 20 20 76 2b 2b 3b 0a 20 20 20 20 20 20       v++;.      
1a4e4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1a4e5 20 20 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f     sqlite3_rando
1a4e6 6d 6e 65 73 73 28 73 69 7a 65 6f 66 28 76 29 2c  mness(sizeof(v),
1a4e7 20 26 76 29 3b 0a 20 20 20 20 20 20 20 20 20 20   &v);.          
1a4e8 69 66 28 20 63 6e 74 3c 35 20 29 20 76 20 26 3d  if( cnt<5 ) v &=
1a4e9 20 30 78 66 66 66 66 66 66 3b 0a 20 20 20 20 20   0xffffff;.     
1a4ea 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
1a4eb 20 76 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65   v==0 ) continue
1a4ec 3b 0a 20 20 20 20 20 20 20 20 78 20 3d 20 69 6e  ;.        x = in
1a4ed 74 54 6f 4b 65 79 28 76 29 3b 0a 20 20 20 20 20  tToKey(v);.     
1a4ee 20 20 20 72 78 20 3d 20 73 71 6c 69 74 65 33 42     rx = sqlite3B
1a4ef 74 72 65 65 4d 6f 76 65 74 6f 55 6e 70 61 63 6b  treeMovetoUnpack
1a4f0 65 64 28 70 43 2d 3e 70 43 75 72 73 6f 72 2c 20  ed(pC->pCursor, 
1a4f1 30 2c 20 28 75 36 34 29 78 2c 20 30 2c 20 26 72  0, (u64)x, 0, &r
1a4f2 65 73 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74  es);.        cnt
1a4f3 2b 2b 3b 0a 20 20 20 20 20 20 7d 77 68 69 6c 65  ++;.      }while
1a4f4 28 20 63 6e 74 3c 31 30 30 20 26 26 20 72 78 3d  ( cnt<100 && rx=
1a4f5 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72 65  =SQLITE_OK && re
1a4f6 73 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 64 62  s==0 );.      db
1a4f7 2d 3e 70 72 69 6f 72 4e 65 77 52 6f 77 69 64 20  ->priorNewRowid 
1a4f8 3d 20 76 3b 0a 20 20 20 20 20 20 69 66 28 20 72  = v;.      if( r
1a4f9 78 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  x==SQLITE_OK && 
1a4fa 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  res==0 ){.      
1a4fb 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 46 55    rc = SQLITE_FU
1a4fc 4c 4c 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  LL;.        goto
1a4fd 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72   abort_due_to_er
1a4fe 72 6f 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ror;.      }.   
1a4ff 20 7d 0a 20 20 20 20 70 43 2d 3e 72 6f 77 69 64   }.    pC->rowid
1a500 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20 20  IsValid = 0;.   
1a501 20 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76   pC->deferredMov
1a502 65 74 6f 20 3d 20 30 3b 0a 20 20 20 20 70 43 2d  eto = 0;.    pC-
1a503 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20 43  >cacheStatus = C
1a504 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 7d 0a  ACHE_STALE;.  }.
1a505 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1a506 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1a507 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76  .  pOut->u.i = v
1a508 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  ;.  break;.}../*
1a509 20 4f 70 63 6f 64 65 3a 20 49 6e 73 65 72 74 20   Opcode: Insert 
1a50a 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1a50b 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20 65 6e  *.** Write an en
1a50c 74 72 79 20 69 6e 74 6f 20 74 68 65 20 74 61 62  try into the tab
1a50d 6c 65 20 6f 66 20 63 75 72 73 6f 72 20 50 31 2e  le of cursor P1.
1a50e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20 69 73    A new entry is
1a50f 0a 2a 2a 20 63 72 65 61 74 65 64 20 69 66 20 69  .** created if i
1a510 74 20 64 6f 65 73 6e 27 74 20 61 6c 72 65 61 64  t doesn't alread
1a511 79 20 65 78 69 73 74 20 6f 72 20 74 68 65 20 64  y exist or the d
1a512 61 74 61 20 66 6f 72 20 61 6e 20 65 78 69 73 74  ata for an exist
1a513 69 6e 67 0a 2a 2a 20 65 6e 74 72 79 20 69 73 20  ing.** entry is 
1a514 6f 76 65 72 77 72 69 74 74 65 6e 2e 20 20 54 68  overwritten.  Th
1a515 65 20 64 61 74 61 20 69 73 20 74 68 65 20 76 61  e data is the va
1a516 6c 75 65 20 73 74 6f 72 65 64 20 72 65 67 69 73  lue stored regis
1a517 74 65 72 0a 2a 2a 20 6e 75 6d 62 65 72 20 50 32  ter.** number P2
1a518 2e 20 54 68 65 20 6b 65 79 20 69 73 20 73 74 6f  . The key is sto
1a519 72 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 20  red in register 
1a51a 50 33 2e 20 54 68 65 20 6b 65 79 20 6d 75 73 74  P3. The key must
1a51b 0a 2a 2a 20 62 65 20 61 6e 20 69 6e 74 65 67 65  .** be an intege
1a51c 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  r..**.** If the 
1a51d 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 66  OPFLAG_NCHANGE f
1a51e 6c 61 67 20 6f 66 20 50 35 20 69 73 20 73 65 74  lag of P5 is set
1a51f 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 20 63  , then the row c
1a520 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73 0a 2a  hange count is.*
1a521 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 28 6f  * incremented (o
1a522 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e 20 20  therwise not).  
1a523 49 66 20 74 68 65 20 4f 50 46 4c 41 47 5f 4c 41  If the OPFLAG_LA
1a524 53 54 52 4f 57 49 44 20 66 6c 61 67 20 6f 66 20  STROWID flag of 
1a525 50 35 20 69 73 20 73 65 74 2c 0a 2a 2a 20 74 68  P5 is set,.** th
1a526 65 6e 20 72 6f 77 69 64 20 69 73 20 73 74 6f 72  en rowid is stor
1a527 65 64 20 66 6f 72 20 73 75 62 73 65 71 75 65 6e  ed for subsequen
1a528 74 20 72 65 74 75 72 6e 20 62 79 20 74 68 65 0a  t return by the.
1a529 2a 2a 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  ** sqlite3_last_
1a52a 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 66  insert_rowid() f
1a52b 75 6e 63 74 69 6f 6e 20 28 6f 74 68 65 72 77 69  unction (otherwi
1a52c 73 65 20 69 74 20 69 73 20 75 6e 6d 6f 64 69 66  se it is unmodif
1a52d 69 65 64 29 2e 0a 2a 2a 0a 2a 2a 20 50 61 72 61  ied)..**.** Para
1a52e 6d 65 74 65 72 20 50 34 20 6d 61 79 20 70 6f 69  meter P4 may poi
1a52f 6e 74 20 74 6f 20 61 20 73 74 72 69 6e 67 20 63  nt to a string c
1a530 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74 61  ontaining the ta
1a531 62 6c 65 2d 6e 61 6d 65 2c 20 6f 72 0a 2a 2a 20  ble-name, or.** 
1a532 6d 61 79 20 62 65 20 4e 55 4c 4c 2e 20 49 66 20  may be NULL. If 
1a533 69 74 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20  it is not NULL, 
1a534 74 68 65 6e 20 74 68 65 20 75 70 64 61 74 65 2d  then the update-
1a535 68 6f 6f 6b 20 0a 2a 2a 20 28 73 71 6c 69 74 65  hook .** (sqlite
1a536 33 2e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63  3.xUpdateCallbac
1a537 6b 29 20 69 73 20 69 6e 76 6f 6b 65 64 20 66 6f  k) is invoked fo
1a538 6c 6c 6f 77 69 6e 67 20 61 20 73 75 63 63 65 73  llowing a succes
1a539 73 66 75 6c 20 69 6e 73 65 72 74 2e 0a 2a 2a 0a  sful insert..**.
1a53a 2a 2a 20 28 57 41 52 4e 49 4e 47 2f 54 4f 44 4f  ** (WARNING/TODO
1a53b 3a 20 49 66 20 50 31 20 69 73 20 61 20 70 73 65  : If P1 is a pse
1a53c 75 64 6f 2d 63 75 72 73 6f 72 20 61 6e 64 20 50  udo-cursor and P
1a53d 32 20 69 73 20 64 79 6e 61 6d 69 63 61 6c 6c 79  2 is dynamically
1a53e 0a 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 2c 20 74  .** allocated, t
1a53f 68 65 6e 20 6f 77 6e 65 72 73 68 69 70 20 6f 66  hen ownership of
1a540 20 50 32 20 69 73 20 74 72 61 6e 73 66 65 72 72   P2 is transferr
1a541 65 64 20 74 6f 20 74 68 65 20 70 73 65 75 64 6f  ed to the pseudo
1a542 2d 63 75 72 73 6f 72 0a 2a 2a 20 61 6e 64 20 72  -cursor.** and r
1a543 65 67 69 73 74 65 72 20 50 32 20 62 65 63 6f 6d  egister P2 becom
1a544 65 73 20 65 70 68 65 6d 65 72 61 6c 2e 20 20 49  es ephemeral.  I
1a545 66 20 74 68 65 20 63 75 72 73 6f 72 20 69 73 20  f the cursor is 
1a546 63 68 61 6e 67 65 64 2c 20 74 68 65 0a 2a 2a 20  changed, the.** 
1a547 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74 65  value of registe
1a548 72 20 50 32 20 77 69 6c 6c 20 74 68 65 6e 20 63  r P2 will then c
1a549 68 61 6e 67 65 2e 20 20 4d 61 6b 65 20 73 75 72  hange.  Make sur
1a54a 65 20 74 68 69 73 20 64 6f 65 73 20 6e 6f 74 0a  e this does not.
1a54b 2a 2a 20 63 61 75 73 65 20 61 6e 79 20 70 72 6f  ** cause any pro
1a54c 62 6c 65 6d 73 2e 29 0a 2a 2a 0a 2a 2a 20 54 68  blems.).**.** Th
1a54d 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 6f  is instruction o
1a54e 6e 6c 79 20 77 6f 72 6b 73 20 6f 6e 20 74 61 62  nly works on tab
1a54f 6c 65 73 2e 20 20 54 68 65 20 65 71 75 69 76 61  les.  The equiva
1a550 6c 65 6e 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  lent instruction
1a551 0a 2a 2a 20 66 6f 72 20 69 6e 64 69 63 65 73 20  .** for indices 
1a552 69 73 20 4f 50 5f 49 64 78 49 6e 73 65 72 74 2e  is OP_IdxInsert.
1a553 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 73 65  .*/.case OP_Inse
1a554 72 74 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70 44 61  rt: {.  Mem *pDa
1a555 74 61 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  ta = &p->aMem[pO
1a556 70 2d 3e 70 32 5d 3b 0a 20 20 4d 65 6d 20 2a 70  p->p2];.  Mem *p
1a557 4b 65 79 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Key = &p->aMem[p
1a558 4f 70 2d 3e 70 33 5d 3b 0a 0a 20 20 69 36 34 20  Op->p3];..  i64 
1a559 69 4b 65 79 3b 20 20 20 2f 2a 20 54 68 65 20 69  iKey;   /* The i
1a55a 6e 74 65 67 65 72 20 52 4f 57 49 44 20 6f 72 20  nteger ROWID or 
1a55b 6b 65 79 20 66 6f 72 20 74 68 65 20 72 65 63 6f  key for the reco
1a55c 72 64 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65  rd to be inserte
1a55d 64 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70  d */.  int i = p
1a55e 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75  Op->p1;.  VdbeCu
1a55f 72 73 6f 72 20 2a 70 43 3b 0a 20 20 61 73 73 65  rsor *pC;.  asse
1a560 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1a561 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43  >nCursor );.  pC
1a562 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
1a563 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20    assert( pC!=0 
1a564 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d  );.  assert( pC-
1a565 3e 70 43 75 72 73 6f 72 21 3d 30 20 7c 7c 20 70  >pCursor!=0 || p
1a566 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29  C->pseudoTable )
1a567 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4b 65 79  ;.  assert( pKey
1a568 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 49 6e  ->flags & MEM_In
1a569 74 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  t );.  assert( p
1a56a 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1a56b 52 45 47 49 53 54 45 52 5f 54 52 41 43 45 28 70  REGISTER_TRACE(p
1a56c 4f 70 2d 3e 70 32 2c 20 70 44 61 74 61 29 3b 0a  Op->p2, pData);.
1a56d 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43 45    REGISTER_TRACE
1a56e 28 70 4f 70 2d 3e 70 33 2c 20 70 4b 65 79 29 3b  (pOp->p3, pKey);
1a56f 0a 0a 20 20 69 4b 65 79 20 3d 20 69 6e 74 54 6f  ..  iKey = intTo
1a570 4b 65 79 28 70 4b 65 79 2d 3e 75 2e 69 29 3b 0a  Key(pKey->u.i);.
1a571 20 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20    if( pOp->p5 & 
1a572 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29  OPFLAG_NCHANGE )
1a573 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1a574 20 69 66 28 20 70 4f 70 2d 3e 70 35 20 26 20 4f   if( pOp->p5 & O
1a575 50 46 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 20  PFLAG_LASTROWID 
1a576 29 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20  ) db->lastRowid 
1a577 3d 20 70 4b 65 79 2d 3e 75 2e 69 3b 0a 20 20 69  = pKey->u.i;.  i
1a578 66 28 20 70 43 2d 3e 6e 65 78 74 52 6f 77 69 64  f( pC->nextRowid
1a579 56 61 6c 69 64 20 26 26 20 70 4b 65 79 2d 3e 75  Valid && pKey->u
1a57a 2e 69 3e 3d 70 43 2d 3e 6e 65 78 74 52 6f 77 69  .i>=pC->nextRowi
1a57b 64 20 29 7b 0a 20 20 20 20 70 43 2d 3e 6e 65 78  d ){.    pC->nex
1a57c 74 52 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b  tRowidValid = 0;
1a57d 0a 20 20 7d 0a 20 20 69 66 28 20 70 44 61 74 61  .  }.  if( pData
1a57e 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 4e 75  ->flags & MEM_Nu
1a57f 6c 6c 20 29 7b 0a 20 20 20 20 70 44 61 74 61 2d  ll ){.    pData-
1a580 3e 7a 20 3d 20 30 3b 0a 20 20 20 20 70 44 61 74  >z = 0;.    pDat
1a581 61 2d 3e 6e 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  a->n = 0;.  }els
1a582 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1a583 44 61 74 61 2d 3e 66 6c 61 67 73 20 26 20 28 4d  Data->flags & (M
1a584 45 4d 5f 42 6c 6f 62 7c 4d 45 4d 5f 53 74 72 29  EM_Blob|MEM_Str)
1a585 20 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 43   );.  }.  if( pC
1a586 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 20 29 7b  ->pseudoTable ){
1a587 0a 20 20 20 20 69 66 28 20 21 70 43 2d 3e 65 70  .    if( !pC->ep
1a588 68 65 6d 50 73 65 75 64 6f 54 61 62 6c 65 20 29  hemPseudoTable )
1a589 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  {.      sqlite3D
1a58a 62 46 72 65 65 28 64 62 2c 20 70 43 2d 3e 70 44  bFree(db, pC->pD
1a58b 61 74 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ata);.    }.    
1a58c 70 43 2d 3e 69 4b 65 79 20 3d 20 69 4b 65 79 3b  pC->iKey = iKey;
1a58d 0a 20 20 20 20 70 43 2d 3e 6e 44 61 74 61 20 3d  .    pC->nData =
1a58e 20 70 44 61 74 61 2d 3e 6e 3b 0a 20 20 20 20 69   pData->n;.    i
1a58f 66 28 20 70 44 61 74 61 2d 3e 7a 3d 3d 70 44 61  f( pData->z==pDa
1a590 74 61 2d 3e 7a 4d 61 6c 6c 6f 63 20 7c 7c 20 70  ta->zMalloc || p
1a591 43 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61  C->ephemPseudoTa
1a592 62 6c 65 20 29 7b 0a 20 20 20 20 20 20 70 43 2d  ble ){.      pC-
1a593 3e 70 44 61 74 61 20 3d 20 70 44 61 74 61 2d 3e  >pData = pData->
1a594 7a 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  z;.      if( !pC
1a595 2d 3e 65 70 68 65 6d 50 73 65 75 64 6f 54 61 62  ->ephemPseudoTab
1a596 6c 65 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  le ){.        pD
1a597 61 74 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 4d  ata->flags &= ~M
1a598 45 4d 5f 44 79 6e 3b 0a 20 20 20 20 20 20 20 20  EM_Dyn;.        
1a599 70 44 61 74 61 2d 3e 66 6c 61 67 73 20 7c 3d 20  pData->flags |= 
1a59a 4d 45 4d 5f 45 70 68 65 6d 3b 0a 20 20 20 20 20  MEM_Ephem;.     
1a59b 20 20 20 70 44 61 74 61 2d 3e 7a 4d 61 6c 6c 6f     pData->zMallo
1a59c 63 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20  c = 0;.      }. 
1a59d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1a59e 70 43 2d 3e 70 44 61 74 61 20 3d 20 73 71 6c 69  pC->pData = sqli
1a59f 74 65 33 4d 61 6c 6c 6f 63 28 20 70 43 2d 3e 6e  te3Malloc( pC->n
1a5a0 44 61 74 61 2b 32 20 29 3b 0a 20 20 20 20 20 20  Data+2 );.      
1a5a1 69 66 28 20 21 70 43 2d 3e 70 44 61 74 61 20 29  if( !pC->pData )
1a5a2 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20   goto no_mem;.  
1a5a3 20 20 20 20 6d 65 6d 63 70 79 28 70 43 2d 3e 70      memcpy(pC->p
1a5a4 44 61 74 61 2c 20 70 44 61 74 61 2d 3e 7a 2c 20  Data, pData->z, 
1a5a5 70 43 2d 3e 6e 44 61 74 61 29 3b 0a 20 20 20 20  pC->nData);.    
1a5a6 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e    pC->pData[pC->
1a5a7 6e 44 61 74 61 5d 20 3d 20 30 3b 0a 20 20 20 20  nData] = 0;.    
1a5a8 20 20 70 43 2d 3e 70 44 61 74 61 5b 70 43 2d 3e    pC->pData[pC->
1a5a9 6e 44 61 74 61 2b 31 5d 20 3d 20 30 3b 0a 20 20  nData+1] = 0;.  
1a5aa 20 20 7d 0a 20 20 20 20 70 43 2d 3e 6e 75 6c 6c    }.    pC->null
1a5ab 52 6f 77 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  Row = 0;.  }else
1a5ac 7b 0a 20 20 20 20 69 6e 74 20 6e 5a 65 72 6f 3b  {.    int nZero;
1a5ad 0a 20 20 20 20 69 66 28 20 70 44 61 74 61 2d 3e  .    if( pData->
1a5ae 66 6c 61 67 73 20 26 20 4d 45 4d 5f 5a 65 72 6f  flags & MEM_Zero
1a5af 20 29 7b 0a 20 20 20 20 20 20 6e 5a 65 72 6f 20   ){.      nZero 
1a5b0 3d 20 70 44 61 74 61 2d 3e 75 2e 69 3b 0a 20 20  = pData->u.i;.  
1a5b1 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
1a5b2 5a 65 72 6f 20 3d 20 30 3b 0a 20 20 20 20 7d 0a  Zero = 0;.    }.
1a5b3 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
1a5b4 42 74 72 65 65 49 6e 73 65 72 74 28 70 43 2d 3e  BtreeInsert(pC->
1a5b5 70 43 75 72 73 6f 72 2c 20 30 2c 20 69 4b 65 79  pCursor, 0, iKey
1a5b6 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
1a5b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44                pD
1a5b8 61 74 61 2d 3e 7a 2c 20 70 44 61 74 61 2d 3e 6e  ata->z, pData->n
1a5b9 2c 20 6e 5a 65 72 6f 2c 0a 20 20 20 20 20 20 20  , nZero,.       
1a5ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a5bb 20 20 20 20 20 70 4f 70 2d 3e 70 35 20 26 20 4f       pOp->p5 & O
1a5bc 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20  PFLAG_APPEND);. 
1a5bd 20 7d 0a 20 20 0a 20 20 70 43 2d 3e 72 6f 77 69   }.  .  pC->rowi
1a5be 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1a5bf 70 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65  pC->deferredMove
1a5c0 74 6f 20 3d 20 30 3b 0a 20 20 70 43 2d 3e 63 61  to = 0;.  pC->ca
1a5c1 63 68 65 53 74 61 74 75 73 20 3d 20 43 41 43 48  cheStatus = CACH
1a5c2 45 5f 53 54 41 4c 45 3b 0a 0a 20 20 2f 2a 20 49  E_STALE;..  /* I
1a5c3 6e 76 6f 6b 65 20 74 68 65 20 75 70 64 61 74 65  nvoke the update
1a5c4 2d 68 6f 6f 6b 20 69 66 20 72 65 71 75 69 72 65  -hook if require
1a5c5 64 2e 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  d. */.  if( rc==
1a5c6 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 64 62 2d  SQLITE_OK && db-
1a5c7 3e 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b  >xUpdateCallback
1a5c8 20 26 26 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b   && pOp->p4.z ){
1a5c9 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1a5ca 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 70  *zDb = db->aDb[p
1a5cb 43 2d 3e 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20  C->iDb].zName;. 
1a5cc 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
1a5cd 54 62 6c 20 3d 20 70 4f 70 2d 3e 70 34 2e 7a 3b  Tbl = pOp->p4.z;
1a5ce 0a 20 20 20 20 69 6e 74 20 6f 70 20 3d 20 28 28  .    int op = ((
1a5cf 70 4f 70 2d 3e 70 35 20 26 20 4f 50 46 4c 41 47  pOp->p5 & OPFLAG
1a5d0 5f 49 53 55 50 44 41 54 45 29 20 3f 20 53 51 4c  _ISUPDATE) ? SQL
1a5d1 49 54 45 5f 55 50 44 41 54 45 20 3a 20 53 51 4c  ITE_UPDATE : SQL
1a5d2 49 54 45 5f 49 4e 53 45 52 54 29 3b 0a 20 20 20  ITE_INSERT);.   
1a5d3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
1a5d4 61 62 6c 65 20 29 3b 0a 20 20 20 20 64 62 2d 3e  able );.    db->
1a5d5 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28  xUpdateCallback(
1a5d6 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20  db->pUpdateArg, 
1a5d7 6f 70 2c 20 7a 44 62 2c 20 7a 54 62 6c 2c 20 69  op, zDb, zTbl, i
1a5d8 4b 65 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74  Key);.    assert
1a5d9 28 20 70 43 2d 3e 69 44 62 3e 3d 30 20 29 3b 0a  ( pC->iDb>=0 );.
1a5da 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1a5db 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 6c 65 74  /* Opcode: Delet
1a5dc 65 20 50 31 20 50 32 20 2a 20 50 34 20 2a 0a 2a  e P1 P2 * P4 *.*
1a5dd 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 74 68 65 20  *.** Delete the 
1a5de 72 65 63 6f 72 64 20 61 74 20 77 68 69 63 68 20  record at which 
1a5df 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 69 73  the P1 cursor is
1a5e0 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74   currently point
1a5e1 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63  ing..**.** The c
1a5e2 75 72 73 6f 72 20 77 69 6c 6c 20 62 65 20 6c 65  ursor will be le
1a5e3 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 65  ft pointing at e
1a5e4 69 74 68 65 72 20 74 68 65 20 6e 65 78 74 20 6f  ither the next o
1a5e5 72 20 74 68 65 20 70 72 65 76 69 6f 75 73 0a 2a  r the previous.*
1a5e6 2a 20 72 65 63 6f 72 64 20 69 6e 20 74 68 65 20  * record in the 
1a5e7 74 61 62 6c 65 2e 20 49 66 20 69 74 20 69 73 20  table. If it is 
1a5e8 6c 65 66 74 20 70 6f 69 6e 74 69 6e 67 20 61 74  left pointing at
1a5e9 20 74 68 65 20 6e 65 78 74 20 72 65 63 6f 72 64   the next record
1a5ea 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65 20 6e 65  , then.** the ne
1a5eb 78 74 20 4e 65 78 74 20 69 6e 73 74 72 75 63 74  xt Next instruct
1a5ec 69 6f 6e 20 77 69 6c 6c 20 62 65 20 61 20 6e 6f  ion will be a no
1a5ed 2d 6f 70 2e 20 20 48 65 6e 63 65 20 69 74 20 69  -op.  Hence it i
1a5ee 73 20 4f 4b 20 74 6f 20 64 65 6c 65 74 65 0a 2a  s OK to delete.*
1a5ef 2a 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20  * a record from 
1a5f0 77 69 74 68 69 6e 20 61 6e 20 4e 65 78 74 20 6c  within an Next l
1a5f1 6f 6f 70 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  oop..**.** If th
1a5f2 65 20 4f 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45  e OPFLAG_NCHANGE
1a5f3 20 66 6c 61 67 20 6f 66 20 50 32 20 69 73 20 73   flag of P2 is s
1a5f4 65 74 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77  et, then the row
1a5f5 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 20 69 73   change count is
1a5f6 0a 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 65 64 20  .** incremented 
1a5f7 28 6f 74 68 65 72 77 69 73 65 20 6e 6f 74 29 2e  (otherwise not).
1a5f8 0a 2a 2a 0a 2a 2a 20 50 31 20 6d 75 73 74 20 6e  .**.** P1 must n
1a5f9 6f 74 20 62 65 20 70 73 65 75 64 6f 2d 74 61 62  ot be pseudo-tab
1a5fa 6c 65 2e 20 20 49 74 20 68 61 73 20 74 6f 20 62  le.  It has to b
1a5fb 65 20 61 20 72 65 61 6c 20 74 61 62 6c 65 20 77  e a real table w
1a5fc 69 74 68 0a 2a 2a 20 6d 75 6c 74 69 70 6c 65 20  ith.** multiple 
1a5fd 72 6f 77 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50  rows..**.** If P
1a5fe 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74  4 is not NULL, t
1a5ff 68 65 6e 20 69 74 20 69 73 20 74 68 65 20 6e 61  hen it is the na
1a600 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1a601 74 68 61 74 20 50 31 20 69 73 0a 2a 2a 20 70 6f  that P1 is.** po
1a602 69 6e 74 69 6e 67 20 74 6f 2e 20 20 54 68 65 20  inting to.  The 
1a603 75 70 64 61 74 65 20 68 6f 6f 6b 20 77 69 6c 6c  update hook will
1a604 20 62 65 20 69 6e 76 6f 6b 65 64 2c 20 69 66 20   be invoked, if 
1a605 69 74 20 65 78 69 73 74 73 2e 0a 2a 2a 20 49 66  it exists..** If
1a606 20 50 34 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20   P4 is not NULL 
1a607 74 68 65 6e 20 74 68 65 20 50 31 20 63 75 72 73  then the P1 curs
1a608 6f 72 20 6d 75 73 74 20 68 61 76 65 20 62 65 65  or must have bee
1a609 6e 20 70 6f 73 69 74 69 6f 6e 65 64 0a 2a 2a 20  n positioned.** 
1a60a 75 73 69 6e 67 20 4f 50 5f 4e 6f 74 46 6f 75 6e  using OP_NotFoun
1a60b 64 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f 6b  d prior to invok
1a60c 69 6e 67 20 74 68 69 73 20 6f 70 63 6f 64 65 2e  ing this opcode.
1a60d 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 44 65 6c 65  .*/.case OP_Dele
1a60e 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20  te: {.  int i = 
1a60f 70 4f 70 2d 3e 70 31 3b 0a 20 20 69 36 34 20 69  pOp->p1;.  i64 i
1a610 4b 65 79 3b 0a 20 20 56 64 62 65 43 75 72 73 6f  Key;.  VdbeCurso
1a611 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65 72 74  r *pC;..  assert
1a612 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e  ( i>=0 && i<p->n
1a613 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d  Cursor );.  pC =
1a614 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20   p->apCsr[i];.  
1a615 61 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b  assert( pC!=0 );
1a616 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 70  .  assert( pC->p
1a617 43 75 72 73 6f 72 21 3d 30 20 29 3b 20 20 2f 2a  Cursor!=0 );  /*
1a618 20 4f 6e 6c 79 20 76 61 6c 69 64 20 66 6f 72 20   Only valid for 
1a619 72 65 61 6c 20 74 61 62 6c 65 73 2c 20 6e 6f 20  real tables, no 
1a61a 70 73 65 75 64 6f 74 61 62 6c 65 73 20 2a 2f 0a  pseudotables */.
1a61b 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 75 70 64  .  /* If the upd
1a61c 61 74 65 2d 68 6f 6f 6b 20 77 69 6c 6c 20 62 65  ate-hook will be
1a61d 20 69 6e 76 6f 6b 65 64 2c 20 73 65 74 20 69 4b   invoked, set iK
1a61e 65 79 20 74 6f 20 74 68 65 20 72 6f 77 69 64 20  ey to the rowid 
1a61f 6f 66 20 74 68 65 0a 20 20 2a 2a 20 72 6f 77 20  of the.  ** row 
1a620 62 65 69 6e 67 20 64 65 6c 65 74 65 64 2e 0a 20  being deleted.. 
1a621 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 78 55   */.  if( db->xU
1a622 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 20 26 26  pdateCallback &&
1a623 20 70 4f 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20   pOp->p4.z ){.  
1a624 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73    assert( pC->is
1a625 54 61 62 6c 65 20 29 3b 0a 20 20 20 20 61 73 73  Table );.    ass
1a626 65 72 74 28 20 70 43 2d 3e 72 6f 77 69 64 49 73  ert( pC->rowidIs
1a627 56 61 6c 69 64 20 29 3b 20 20 2f 2a 20 6c 61 73  Valid );  /* las
1a628 74 52 6f 77 69 64 20 73 65 74 20 62 79 20 70 72  tRowid set by pr
1a629 65 76 69 6f 75 73 20 4f 50 5f 4e 6f 74 46 6f 75  evious OP_NotFou
1a62a 6e 64 20 2a 2f 0a 20 20 20 20 69 4b 65 79 20 3d  nd */.    iKey =
1a62b 20 70 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a   pC->lastRowid;.
1a62c 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69    }..  rc = sqli
1a62d 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
1a62e 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72  eto(pC);.  if( r
1a62f 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  c ) goto abort_d
1a630 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 72  ue_to_error;.  r
1a631 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1a632 44 65 6c 65 74 65 28 70 43 2d 3e 70 43 75 72 73  Delete(pC->pCurs
1a633 6f 72 29 3b 0a 20 20 70 43 2d 3e 6e 65 78 74 52  or);.  pC->nextR
1a634 6f 77 69 64 56 61 6c 69 64 20 3d 20 30 3b 0a 20  owidValid = 0;. 
1a635 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
1a636 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1a637 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65  .  /* Invoke the
1a638 20 75 70 64 61 74 65 2d 68 6f 6f 6b 20 69 66 20   update-hook if 
1a639 72 65 71 75 69 72 65 64 2e 20 2a 2f 0a 20 20 69  required. */.  i
1a63a 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1a63b 20 26 26 20 64 62 2d 3e 78 55 70 64 61 74 65 43   && db->xUpdateC
1a63c 61 6c 6c 62 61 63 6b 20 26 26 20 70 4f 70 2d 3e  allback && pOp->
1a63d 70 34 2e 7a 20 29 7b 0a 20 20 20 20 63 6f 6e 73  p4.z ){.    cons
1a63e 74 20 63 68 61 72 20 2a 7a 44 62 20 3d 20 64 62  t char *zDb = db
1a63f 2d 3e 61 44 62 5b 70 43 2d 3e 69 44 62 5d 2e 7a  ->aDb[pC->iDb].z
1a640 4e 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20  Name;.    const 
1a641 63 68 61 72 20 2a 7a 54 62 6c 20 3d 20 70 4f 70  char *zTbl = pOp
1a642 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 64 62 2d 3e  ->p4.z;.    db->
1a643 78 55 70 64 61 74 65 43 61 6c 6c 62 61 63 6b 28  xUpdateCallback(
1a644 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67 2c 20  db->pUpdateArg, 
1a645 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a  SQLITE_DELETE, z
1a646 44 62 2c 20 7a 54 62 6c 2c 20 69 4b 65 79 29 3b  Db, zTbl, iKey);
1a647 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d  .    assert( pC-
1a648 3e 69 44 62 3e 3d 30 20 29 3b 0a 20 20 7d 0a 20  >iDb>=0 );.  }. 
1a649 20 69 66 28 20 70 4f 70 2d 3e 70 32 20 26 20 4f   if( pOp->p2 & O
1a64a 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 20 29 20  PFLAG_NCHANGE ) 
1a64b 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20 20  p->nChange++;.  
1a64c 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1a64d 6f 64 65 3a 20 52 65 73 65 74 43 6f 75 6e 74 20  ode: ResetCount 
1a64e 50 31 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54 68 69  P1 * *.**.** Thi
1a64f 73 20 6f 70 63 6f 64 65 20 72 65 73 65 74 73 20  s opcode resets 
1a650 74 68 65 20 56 4d 73 20 69 6e 74 65 72 6e 61 6c  the VMs internal
1a651 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1a652 74 6f 20 30 2e 20 49 66 20 50 31 20 69 73 20 74  to 0. If P1 is t
1a653 72 75 65 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  rue,.** then the
1a654 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 63 68   value of the ch
1a655 61 6e 67 65 20 63 6f 75 6e 74 65 72 20 69 73 20  ange counter is 
1a656 63 6f 70 69 65 64 20 74 6f 20 74 68 65 20 64 61  copied to the da
1a657 74 61 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a  tabase handle.**
1a658 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 65 72 20   change counter 
1a659 28 72 65 74 75 72 6e 65 64 20 62 79 20 73 75 62  (returned by sub
1a65a 73 65 71 75 65 6e 74 20 63 61 6c 6c 73 20 74 6f  sequent calls to
1a65b 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65 73   sqlite3_changes
1a65c 28 29 29 0a 2a 2a 20 62 65 66 6f 72 65 20 69 74  ()).** before it
1a65d 20 69 73 20 72 65 73 65 74 2e 20 54 68 69 73 20   is reset. This 
1a65e 69 73 20 75 73 65 64 20 62 79 20 74 72 69 67 67  is used by trigg
1a65f 65 72 20 70 72 6f 67 72 61 6d 73 2e 0a 2a 2f 0a  er programs..*/.
1a660 63 61 73 65 20 4f 50 5f 52 65 73 65 74 43 6f 75  case OP_ResetCou
1a661 6e 74 3a 20 7b 0a 20 20 69 66 28 20 70 4f 70 2d  nt: {.  if( pOp-
1a662 3e 70 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  >p1 ){.    sqlit
1a663 65 33 56 64 62 65 53 65 74 43 68 61 6e 67 65 73  e3VdbeSetChanges
1a664 28 64 62 2c 20 70 2d 3e 6e 43 68 61 6e 67 65 29  (db, p->nChange)
1a665 3b 0a 20 20 7d 0a 20 20 70 2d 3e 6e 43 68 61 6e  ;.  }.  p->nChan
1a666 67 65 20 3d 20 30 3b 0a 20 20 62 72 65 61 6b 3b  ge = 0;.  break;
1a667 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 52  .}../* Opcode: R
1a668 6f 77 44 61 74 61 20 50 31 20 50 32 20 2a 20 2a  owData P1 P2 * *
1a669 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69 74 65 20 69   *.**.** Write i
1a66a 6e 74 6f 20 72 65 67 69 73 74 65 72 20 50 32 20  nto register P2 
1a66b 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 72 6f 77  the complete row
1a66c 20 64 61 74 61 20 66 6f 72 20 63 75 72 73 6f 72   data for cursor
1a66d 20 50 31 2e 0a 2a 2a 20 54 68 65 72 65 20 69 73   P1..** There is
1a66e 20 6e 6f 20 69 6e 74 65 72 70 72 65 74 61 74 69   no interpretati
1a66f 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 2e 20  on of the data. 
1a670 20 0a 2a 2a 20 49 74 20 69 73 20 6a 75 73 74 20   .** It is just 
1a671 63 6f 70 69 65 64 20 6f 6e 74 6f 20 74 68 65 20  copied onto the 
1a672 50 32 20 72 65 67 69 73 74 65 72 20 65 78 61 63  P2 register exac
1a673 74 6c 79 20 61 73 20 0a 2a 2a 20 69 74 20 69 73  tly as .** it is
1a674 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20 64 61   found in the da
1a675 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1a676 2a 2a 20 49 66 20 74 68 65 20 50 31 20 63 75 72  ** If the P1 cur
1a677 73 6f 72 20 6d 75 73 74 20 62 65 20 70 6f 69 6e  sor must be poin
1a678 74 69 6e 67 20 74 6f 20 61 20 76 61 6c 69 64 20  ting to a valid 
1a679 72 6f 77 20 28 6e 6f 74 20 61 20 4e 55 4c 4c 20  row (not a NULL 
1a67a 72 6f 77 29 0a 2a 2a 20 6f 66 20 61 20 72 65 61  row).** of a rea
1a67b 6c 20 74 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70  l table, not a p
1a67c 73 65 75 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2f 0a  seudo-table..*/.
1a67d 2f 2a 20 4f 70 63 6f 64 65 3a 20 52 6f 77 4b 65  /* Opcode: RowKe
1a67e 79 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  y P1 P2 * * *.**
1a67f 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20 72  .** Write into r
1a680 65 67 69 73 74 65 72 20 50 32 20 74 68 65 20 63  egister P2 the c
1a681 6f 6d 70 6c 65 74 65 20 72 6f 77 20 6b 65 79 20  omplete row key 
1a682 66 6f 72 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a  for cursor P1..*
1a683 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 20 69 6e  * There is no in
1a684 74 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20  terpretation of 
1a685 74 68 65 20 64 61 74 61 2e 20 20 0a 2a 2a 20 54  the data.  .** T
1a686 68 65 20 6b 65 79 20 69 73 20 63 6f 70 69 65 64  he key is copied
1a687 20 6f 6e 74 6f 20 74 68 65 20 50 33 20 72 65 67   onto the P3 reg
1a688 69 73 74 65 72 20 65 78 61 63 74 6c 79 20 61 73  ister exactly as
1a689 20 0a 2a 2a 20 69 74 20 69 73 20 66 6f 75 6e 64   .** it is found
1a68a 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1a68b 20 66 69 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   file..**.** If 
1a68c 74 68 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75  the P1 cursor mu
1a68d 73 74 20 62 65 20 70 6f 69 6e 74 69 6e 67 20 74  st be pointing t
1a68e 6f 20 61 20 76 61 6c 69 64 20 72 6f 77 20 28 6e  o a valid row (n
1a68f 6f 74 20 61 20 4e 55 4c 4c 20 72 6f 77 29 0a 2a  ot a NULL row).*
1a690 2a 20 6f 66 20 61 20 72 65 61 6c 20 74 61 62 6c  * of a real tabl
1a691 65 2c 20 6e 6f 74 20 61 20 70 73 65 75 64 6f 2d  e, not a pseudo-
1a692 74 61 62 6c 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f  table..*/.case O
1a693 50 5f 52 6f 77 4b 65 79 3a 0a 63 61 73 65 20 4f  P_RowKey:.case O
1a694 50 5f 52 6f 77 44 61 74 61 3a 20 7b 0a 20 20 69  P_RowData: {.  i
1a695 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
1a696 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1a697 3b 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43  ;.  BtCursor *pC
1a698 72 73 72 3b 0a 20 20 75 33 32 20 6e 3b 0a 0a 20  rsr;.  u32 n;.. 
1a699 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61 4d 65 6d   pOut = &p->aMem
1a69a 5b 70 4f 70 2d 3e 70 32 5d 3b 0a 0a 20 20 2f 2a  [pOp->p2];..  /*
1a69b 20 4e 6f 74 65 20 74 68 61 74 20 52 6f 77 4b 65   Note that RowKe
1a69c 79 20 61 6e 64 20 52 6f 77 44 61 74 61 20 61 72  y and RowData ar
1a69d 65 20 72 65 61 6c 6c 79 20 65 78 61 63 74 6c 79  e really exactly
1a69e 20 74 68 65 20 73 61 6d 65 20 69 6e 73 74 72 75   the same instru
1a69f 63 74 69 6f 6e 20 2a 2f 0a 20 20 61 73 73 65 72  ction */.  asser
1a6a0 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
1a6a1 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20  nCursor );.  pC 
1a6a2 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20  = p->apCsr[i];. 
1a6a3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 69 73 54   assert( pC->isT
1a6a4 61 62 6c 65 20 7c 7c 20 70 4f 70 2d 3e 6f 70 63  able || pOp->opc
1a6a5 6f 64 65 3d 3d 4f 50 5f 52 6f 77 4b 65 79 20 29  ode==OP_RowKey )
1a6a6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e  ;.  assert( pC->
1a6a7 69 73 49 6e 64 65 78 20 7c 7c 20 70 4f 70 2d 3e  isIndex || pOp->
1a6a8 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 44 61  opcode==OP_RowDa
1a6a9 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  ta );.  assert( 
1a6aa 70 43 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  pC!=0 );.  asser
1a6ab 74 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 3d 3d  t( pC->nullRow==
1a6ac 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1a6ad 43 2d 3e 70 73 65 75 64 6f 54 61 62 6c 65 3d 3d  C->pseudoTable==
1a6ae 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  0 );.  assert( p
1a6af 43 2d 3e 70 43 75 72 73 6f 72 21 3d 30 20 29 3b  C->pCursor!=0 );
1a6b0 0a 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70  .  pCrsr = pC->p
1a6b1 43 75 72 73 6f 72 3b 0a 20 20 72 63 20 3d 20 73  Cursor;.  rc = s
1a6b2 71 6c 69 74 65 33 56 64 62 65 43 75 72 73 6f 72  qlite3VdbeCursor
1a6b3 4d 6f 76 65 74 6f 28 70 43 29 3b 0a 20 20 69 66  Moveto(pC);.  if
1a6b4 28 20 72 63 20 29 20 67 6f 74 6f 20 61 62 6f 72  ( rc ) goto abor
1a6b5 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a  t_due_to_error;.
1a6b6 20 20 69 66 28 20 70 43 2d 3e 69 73 49 6e 64 65    if( pC->isInde
1a6b7 78 20 29 7b 0a 20 20 20 20 69 36 34 20 6e 36 34  x ){.    i64 n64
1a6b8 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 70  ;.    assert( !p
1a6b9 43 2d 3e 69 73 54 61 62 6c 65 20 29 3b 0a 20 20  C->isTable );.  
1a6ba 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65    sqlite3BtreeKe
1a6bb 79 53 69 7a 65 28 70 43 72 73 72 2c 20 26 6e 36  ySize(pCrsr, &n6
1a6bc 34 29 3b 0a 20 20 20 20 69 66 28 20 6e 36 34 3e  4);.    if( n64>
1a6bd 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1a6be 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d 20  E_LIMIT_LENGTH] 
1a6bf 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 6f  ){.      goto to
1a6c0 6f 5f 62 69 67 3b 0a 20 20 20 20 7d 0a 20 20 20  o_big;.    }.   
1a6c1 20 6e 20 3d 20 6e 36 34 3b 0a 20 20 7d 65 6c 73   n = n64;.  }els
1a6c2 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  e{.    sqlite3Bt
1a6c3 72 65 65 44 61 74 61 53 69 7a 65 28 70 43 72 73  reeDataSize(pCrs
1a6c4 72 2c 20 26 6e 29 3b 0a 20 20 20 20 69 66 28 20  r, &n);.    if( 
1a6c5 28 69 6e 74 29 6e 3e 64 62 2d 3e 61 4c 69 6d 69  (int)n>db->aLimi
1a6c6 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c  t[SQLITE_LIMIT_L
1a6c7 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20 20 20  ENGTH] ){.      
1a6c8 67 6f 74 6f 20 74 6f 6f 5f 62 69 67 3b 0a 20 20  goto too_big;.  
1a6c9 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 73 71    }.  }.  if( sq
1a6ca 6c 69 74 65 33 56 64 62 65 4d 65 6d 47 72 6f 77  lite3VdbeMemGrow
1a6cb 28 70 4f 75 74 2c 20 6e 2c 20 30 29 20 29 7b 0a  (pOut, n, 0) ){.
1a6cc 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1a6cd 0a 20 20 7d 0a 20 20 70 4f 75 74 2d 3e 6e 20 3d  .  }.  pOut->n =
1a6ce 20 6e 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65   n;.  MemSetType
1a6cf 46 6c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 42  Flag(pOut, MEM_B
1a6d0 6c 6f 62 29 3b 0a 20 20 69 66 28 20 70 43 2d 3e  lob);.  if( pC->
1a6d1 69 73 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 72  isIndex ){.    r
1a6d2 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1a6d3 4b 65 79 28 70 43 72 73 72 2c 20 30 2c 20 6e 2c  Key(pCrsr, 0, n,
1a6d4 20 70 4f 75 74 2d 3e 7a 29 3b 0a 20 20 7d 65 6c   pOut->z);.  }el
1a6d5 73 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  se{.    rc = sql
1a6d6 69 74 65 33 42 74 72 65 65 44 61 74 61 28 70 43  ite3BtreeData(pC
1a6d7 72 73 72 2c 20 30 2c 20 6e 2c 20 70 4f 75 74 2d  rsr, 0, n, pOut-
1a6d8 3e 7a 29 3b 0a 20 20 7d 0a 20 20 70 4f 75 74 2d  >z);.  }.  pOut-
1a6d9 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54  >enc = SQLITE_UT
1a6da 46 38 3b 20 20 2f 2a 20 49 6e 20 63 61 73 65 20  F8;  /* In case 
1a6db 74 68 65 20 62 6c 6f 62 20 69 73 20 65 76 65 72  the blob is ever
1a6dc 20 63 61 73 74 20 74 6f 20 74 65 78 74 20 2a 2f   cast to text */
1a6dd 0a 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c  .  UPDATE_MAX_BL
1a6de 4f 42 53 49 5a 45 28 70 4f 75 74 29 3b 0a 20 20  OBSIZE(pOut);.  
1a6df 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1a6e0 6f 64 65 3a 20 52 6f 77 69 64 20 50 31 20 50 32  ode: Rowid P1 P2
1a6e1 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74 6f   * * *.**.** Sto
1a6e2 72 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 50  re in register P
1a6e3 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77 68 69  2 an integer whi
1a6e4 63 68 20 69 73 20 74 68 65 20 6b 65 79 20 6f 66  ch is the key of
1a6e5 20 74 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79   the table entry
1a6e6 20 74 68 61 74 0a 2a 2a 20 50 31 20 69 73 20 63   that.** P1 is c
1a6e7 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20 74  urrently point t
1a6e8 6f 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 52 6f  o..*/.case OP_Ro
1a6e9 77 69 64 3a 20 7b 20 20 20 20 20 20 20 20 20 20  wid: {          
1a6ea 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1a6eb 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69  rerelease */.  i
1a6ec 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a  nt i = pOp->p1;.
1a6ed 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1a6ee 3b 0a 20 20 69 36 34 20 76 3b 0a 0a 20 20 61 73  ;.  i64 v;..  as
1a6ef 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c  sert( i>=0 && i<
1a6f0 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20  p->nCursor );.  
1a6f1 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d  pC = p->apCsr[i]
1a6f2 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43 21 3d  ;.  assert( pC!=
1a6f3 30 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  0 );.  rc = sqli
1a6f4 74 65 33 56 64 62 65 43 75 72 73 6f 72 4d 6f 76  te3VdbeCursorMov
1a6f5 65 74 6f 28 70 43 29 3b 0a 20 20 69 66 28 20 72  eto(pC);.  if( r
1a6f6 63 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64  c ) goto abort_d
1a6f7 75 65 5f 74 6f 5f 65 72 72 6f 72 3b 0a 20 20 69  ue_to_error;.  i
1a6f8 66 28 20 70 43 2d 3e 72 6f 77 69 64 49 73 56 61  f( pC->rowidIsVa
1a6f9 6c 69 64 20 29 7b 0a 20 20 20 20 76 20 3d 20 70  lid ){.    v = p
1a6fa 43 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20  C->lastRowid;.  
1a6fb 7d 65 6c 73 65 20 69 66 28 20 70 43 2d 3e 70 73  }else if( pC->ps
1a6fc 65 75 64 6f 54 61 62 6c 65 20 29 7b 0a 20 20 20  eudoTable ){.   
1a6fd 20 76 20 3d 20 6b 65 79 54 6f 49 6e 74 28 70 43   v = keyToInt(pC
1a6fe 2d 3e 69 4b 65 79 29 3b 0a 20 20 7d 65 6c 73 65  ->iKey);.  }else
1a6ff 20 69 66 28 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77   if( pC->nullRow
1a700 20 29 7b 0a 20 20 20 20 2f 2a 20 4c 65 61 76 65   ){.    /* Leave
1a701 20 74 68 65 20 72 6f 77 69 64 20 73 65 74 20 74   the rowid set t
1a702 6f 20 61 20 4e 55 4c 4c 20 2a 2f 0a 20 20 20 20  o a NULL */.    
1a703 62 72 65 61 6b 3b 0a 20 20 7d 65 6c 73 65 7b 0a  break;.  }else{.
1a704 20 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e      assert( pC->
1a705 70 43 75 72 73 6f 72 21 3d 30 20 29 3b 0a 20 20  pCursor!=0 );.  
1a706 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4b 65    sqlite3BtreeKe
1a707 79 53 69 7a 65 28 70 43 2d 3e 70 43 75 72 73 6f  ySize(pC->pCurso
1a708 72 2c 20 26 76 29 3b 0a 20 20 20 20 76 20 3d 20  r, &v);.    v = 
1a709 6b 65 79 54 6f 49 6e 74 28 76 29 3b 0a 20 20 7d  keyToInt(v);.  }
1a70a 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 76  .  pOut->u.i = v
1a70b 3b 0a 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c  ;.  MemSetTypeFl
1a70c 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
1a70d 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1a70e 2a 20 4f 70 63 6f 64 65 3a 20 4e 75 6c 6c 52 6f  * Opcode: NullRo
1a70f 77 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  w P1 * * * *.**.
1a710 2a 2a 20 4d 6f 76 65 20 74 68 65 20 63 75 72 73  ** Move the curs
1a711 6f 72 20 50 31 20 74 6f 20 61 20 6e 75 6c 6c 20  or P1 to a null 
1a712 72 6f 77 2e 20 20 41 6e 79 20 4f 50 5f 43 6f 6c  row.  Any OP_Col
1a713 75 6d 6e 20 6f 70 65 72 61 74 69 6f 6e 73 0a 2a  umn operations.*
1a714 2a 20 74 68 61 74 20 6f 63 63 75 72 20 77 68 69  * that occur whi
1a715 6c 65 20 74 68 65 20 63 75 72 73 6f 72 20 69 73  le the cursor is
1a716 20 6f 6e 20 74 68 65 20 6e 75 6c 6c 20 72 6f 77   on the null row
1a717 20 77 69 6c 6c 20 61 6c 77 61 79 73 0a 2a 2a 20   will always.** 
1a718 77 72 69 74 65 20 61 20 4e 55 4c 4c 2e 0a 2a 2f  write a NULL..*/
1a719 0a 63 61 73 65 20 4f 50 5f 4e 75 6c 6c 52 6f 77  .case OP_NullRow
1a71a 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f  : {.  int i = pO
1a71b 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72  p->p1;.  VdbeCur
1a71c 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61 73 73 65  sor *pC;..  asse
1a71d 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d  rt( i>=0 && i<p-
1a71e 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 70 43  >nCursor );.  pC
1a71f 20 3d 20 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a   = p->apCsr[i];.
1a720 20 20 61 73 73 65 72 74 28 20 70 43 21 3d 30 20    assert( pC!=0 
1a721 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77  );.  pC->nullRow
1a722 20 3d 20 31 3b 0a 20 20 70 43 2d 3e 72 6f 77 69   = 1;.  pC->rowi
1a723 64 49 73 56 61 6c 69 64 20 3d 20 30 3b 0a 20 20  dIsValid = 0;.  
1a724 69 66 28 20 70 43 2d 3e 70 43 75 72 73 6f 72 20  if( pC->pCursor 
1a725 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  ){.    sqlite3Bt
1a726 72 65 65 43 6c 65 61 72 43 75 72 73 6f 72 28 70  reeClearCursor(p
1a727 43 2d 3e 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d  C->pCursor);.  }
1a728 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1a729 4f 70 63 6f 64 65 3a 20 4c 61 73 74 20 50 31 20  Opcode: Last P1 
1a72a 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
1a72b 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74  he next use of t
1a72c 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75  he Rowid or Colu
1a72d 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72  mn or Next instr
1a72e 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a  uction for P1 .*
1a72f 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20  * will refer to 
1a730 74 68 65 20 6c 61 73 74 20 65 6e 74 72 79 20 69  the last entry i
1a731 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  n the database t
1a732 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a 2a  able or index..*
1a733 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 6f  * If the table o
1a734 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74 79  r index is empty
1a735 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e 20   and P2>0, then 
1a736 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1a737 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50 32   to P2..** If P2
1a738 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65 20   is 0 or if the 
1a739 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 69  table or index i
1a73a 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61 6c  s not empty, fal
1a73b 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f 20  l through.** to 
1a73c 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e  the following in
1a73d 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61  struction..*/.ca
1a73e 73 65 20 4f 50 5f 4c 61 73 74 3a 20 7b 20 20 20  se OP_Last: {   
1a73f 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1a740 20 20 69 6e 74 20 69 20 3d 20 70 4f 70 2d 3e 70    int i = pOp->p
1a741 31 3b 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20  1;.  VdbeCursor 
1a742 2a 70 43 3b 0a 20 20 42 74 43 75 72 73 6f 72 20  *pC;.  BtCursor 
1a743 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74 20 72 65  *pCrsr;.  int re
1a744 73 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e  s;..  assert( i>
1a745 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1a746 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20 70 2d 3e  or );.  pC = p->
1a747 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61 73 73 65  apCsr[i];.  asse
1a748 72 74 28 20 70 43 21 3d 30 20 29 3b 0a 20 20 70  rt( pC!=0 );.  p
1a749 43 72 73 72 20 3d 20 70 43 2d 3e 70 43 75 72 73  Crsr = pC->pCurs
1a74a 6f 72 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  or;.  assert( pC
1a74b 72 73 72 21 3d 30 20 29 3b 0a 20 20 72 63 20 3d  rsr!=0 );.  rc =
1a74c 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 61 73   sqlite3BtreeLas
1a74d 74 28 70 43 72 73 72 2c 20 26 72 65 73 29 3b 0a  t(pCrsr, &res);.
1a74e 20 20 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20    pC->nullRow = 
1a74f 72 65 73 3b 0a 20 20 70 43 2d 3e 64 65 66 65 72  res;.  pC->defer
1a750 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b 0a 20  redMoveto = 0;. 
1a751 20 70 43 2d 3e 63 61 63 68 65 53 74 61 74 75 73   pC->cacheStatus
1a752 20 3d 20 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a   = CACHE_STALE;.
1a753 20 20 69 66 28 20 72 65 73 20 26 26 20 70 4f 70    if( res && pOp
1a754 2d 3e 70 32 3e 30 20 29 7b 0a 20 20 20 20 70 63  ->p2>0 ){.    pc
1a755 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1a756 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a    }.  break;.}..
1a757 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 53 6f 72 74  ./* Opcode: Sort
1a758 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1a759 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 64  ** This opcode d
1a75a 6f 65 73 20 65 78 61 63 74 6c 79 20 74 68 65 20  oes exactly the 
1a75b 73 61 6d 65 20 74 68 69 6e 67 20 61 73 20 4f 50  same thing as OP
1a75c 5f 52 65 77 69 6e 64 20 65 78 63 65 70 74 20 74  _Rewind except t
1a75d 68 61 74 0a 2a 2a 20 69 74 20 69 6e 63 72 65 6d  hat.** it increm
1a75e 65 6e 74 73 20 61 6e 20 75 6e 64 6f 63 75 6d 65  ents an undocume
1a75f 6e 74 65 64 20 67 6c 6f 62 61 6c 20 76 61 72 69  nted global vari
1a760 61 62 6c 65 20 75 73 65 64 20 66 6f 72 20 74 65  able used for te
1a761 73 74 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 53 6f 72  sting..**.** Sor
1a762 74 69 6e 67 20 69 73 20 61 63 63 6f 6d 70 6c 69  ting is accompli
1a763 73 68 65 64 20 62 79 20 77 72 69 74 69 6e 67 20  shed by writing 
1a764 72 65 63 6f 72 64 73 20 69 6e 74 6f 20 61 20 73  records into a s
1a765 6f 72 74 69 6e 67 20 69 6e 64 65 78 2c 0a 2a 2a  orting index,.**
1a766 20 74 68 65 6e 20 72 65 77 69 6e 64 69 6e 67 20   then rewinding 
1a767 74 68 61 74 20 69 6e 64 65 78 20 61 6e 64 20 70  that index and p
1a768 6c 61 79 69 6e 67 20 69 74 20 62 61 63 6b 20 66  laying it back f
1a769 72 6f 6d 20 62 65 67 69 6e 6e 69 6e 67 20 74 6f  rom beginning to
1a76a 0a 2a 2a 20 65 6e 64 2e 20 20 57 65 20 75 73 65  .** end.  We use
1a76b 20 74 68 65 20 4f 50 5f 53 6f 72 74 20 6f 70 63   the OP_Sort opc
1a76c 6f 64 65 20 69 6e 73 74 65 61 64 20 6f 66 20 4f  ode instead of O
1a76d 50 5f 52 65 77 69 6e 64 20 74 6f 20 64 6f 20 74  P_Rewind to do t
1a76e 68 65 0a 2a 2a 20 72 65 77 69 6e 64 69 6e 67 20  he.** rewinding 
1a76f 73 6f 20 74 68 61 74 20 74 68 65 20 67 6c 6f 62  so that the glob
1a770 61 6c 20 76 61 72 69 61 62 6c 65 20 77 69 6c 6c  al variable will
1a771 20 62 65 20 69 6e 63 72 65 6d 65 6e 74 65 64 20   be incremented 
1a772 61 6e 64 0a 2a 2a 20 72 65 67 72 65 73 73 69 6f  and.** regressio
1a773 6e 20 74 65 73 74 73 20 63 61 6e 20 64 65 74 65  n tests can dete
1a774 72 6d 69 6e 65 20 77 68 65 74 68 65 72 20 6f 72  rmine whether or
1a775 20 6e 6f 74 20 74 68 65 20 6f 70 74 69 6d 69 7a   not the optimiz
1a776 65 72 20 69 73 0a 2a 2a 20 63 6f 72 72 65 63 74  er is.** correct
1a777 6c 79 20 6f 70 74 69 6d 69 7a 69 6e 67 20 6f 75  ly optimizing ou
1a778 74 20 73 6f 72 74 73 2e 0a 2a 2f 0a 63 61 73 65  t sorts..*/.case
1a779 20 4f 50 5f 53 6f 72 74 3a 20 7b 20 20 20 20 20   OP_Sort: {     
1a77a 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a 23 69     /* jump */.#i
1a77b 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
1a77c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6f 72 74 5f  .  sqlite3_sort_
1a77d 63 6f 75 6e 74 2b 2b 3b 0a 20 20 73 71 6c 69 74  count++;.  sqlit
1a77e 65 33 5f 73 65 61 72 63 68 5f 63 6f 75 6e 74 2d  e3_search_count-
1a77f 2d 3b 0a 23 65 6e 64 69 66 0a 20 20 70 2d 3e 61  -;.#endif.  p->a
1a780 43 6f 75 6e 74 65 72 5b 53 51 4c 49 54 45 5f 53  Counter[SQLITE_S
1a781 54 4d 54 53 54 41 54 55 53 5f 53 4f 52 54 2d 31  TMTSTATUS_SORT-1
1a782 5d 2b 2b 3b 0a 20 20 2f 2a 20 46 61 6c 6c 20 74  ]++;.  /* Fall t
1a783 68 72 6f 75 67 68 20 69 6e 74 6f 20 4f 50 5f 52  hrough into OP_R
1a784 65 77 69 6e 64 20 2a 2f 0a 7d 0a 2f 2a 20 4f 70  ewind */.}./* Op
1a785 63 6f 64 65 3a 20 52 65 77 69 6e 64 20 50 31 20  code: Rewind P1 
1a786 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 54  P2 * * *.**.** T
1a787 68 65 20 6e 65 78 74 20 75 73 65 20 6f 66 20 74  he next use of t
1a788 68 65 20 52 6f 77 69 64 20 6f 72 20 43 6f 6c 75  he Rowid or Colu
1a789 6d 6e 20 6f 72 20 4e 65 78 74 20 69 6e 73 74 72  mn or Next instr
1a78a 75 63 74 69 6f 6e 20 66 6f 72 20 50 31 20 0a 2a  uction for P1 .*
1a78b 2a 20 77 69 6c 6c 20 72 65 66 65 72 20 74 6f 20  * will refer to 
1a78c 74 68 65 20 66 69 72 73 74 20 65 6e 74 72 79 20  the first entry 
1a78d 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
1a78e 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 0a  table or index..
1a78f 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20  ** If the table 
1a790 6f 72 20 69 6e 64 65 78 20 69 73 20 65 6d 70 74  or index is empt
1a791 79 20 61 6e 64 20 50 32 3e 30 2c 20 74 68 65 6e  y and P2>0, then
1a792 20 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c   jump immediatel
1a793 79 20 74 6f 20 50 32 2e 0a 2a 2a 20 49 66 20 50  y to P2..** If P
1a794 32 20 69 73 20 30 20 6f 72 20 69 66 20 74 68 65  2 is 0 or if the
1a795 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20   table or index 
1a796 69 73 20 6e 6f 74 20 65 6d 70 74 79 2c 20 66 61  is not empty, fa
1a797 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  ll through.** to
1a798 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1a799 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63  nstruction..*/.c
1a79a 61 73 65 20 4f 50 5f 52 65 77 69 6e 64 3a 20 7b  ase OP_Rewind: {
1a79b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20          /* jump 
1a79c 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f 70  */.  int i = pOp
1a79d 2d 3e 70 31 3b 0a 20 20 56 64 62 65 43 75 72 73  ->p1;.  VdbeCurs
1a79e 6f 72 20 2a 70 43 3b 0a 20 20 42 74 43 75 72 73  or *pC;.  BtCurs
1a79f 6f 72 20 2a 70 43 72 73 72 3b 0a 20 20 69 6e 74  or *pCrsr;.  int
1a7a0 20 72 65 73 3b 0a 0a 20 20 61 73 73 65 72 74 28   res;..  assert(
1a7a1 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43   i>=0 && i<p->nC
1a7a2 75 72 73 6f 72 20 29 3b 0a 20 20 70 43 20 3d 20  ursor );.  pC = 
1a7a3 70 2d 3e 61 70 43 73 72 5b 69 5d 3b 0a 20 20 61  p->apCsr[i];.  a
1a7a4 73 73 65 72 74 28 20 70 43 21 3d 30 20 29 3b 0a  ssert( pC!=0 );.
1a7a5 20 20 69 66 28 20 28 70 43 72 73 72 20 3d 20 70    if( (pCrsr = p
1a7a6 43 2d 3e 70 43 75 72 73 6f 72 29 21 3d 30 20 29  C->pCursor)!=0 )
1a7a7 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  {.    rc = sqlit
1a7a8 65 33 42 74 72 65 65 46 69 72 73 74 28 70 43 72  e3BtreeFirst(pCr
1a7a9 73 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 70  sr, &res);.    p
1a7aa 43 2d 3e 61 74 46 69 72 73 74 20 3d 20 72 65 73  C->atFirst = res
1a7ab 3d 3d 30 3b 0a 20 20 20 20 70 43 2d 3e 64 65 66  ==0;.    pC->def
1a7ac 65 72 72 65 64 4d 6f 76 65 74 6f 20 3d 20 30 3b  erredMoveto = 0;
1a7ad 0a 20 20 20 20 70 43 2d 3e 63 61 63 68 65 53 74  .    pC->cacheSt
1a7ae 61 74 75 73 20 3d 20 43 41 43 48 45 5f 53 54 41  atus = CACHE_STA
1a7af 4c 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  LE;.  }else{.   
1a7b0 20 72 65 73 20 3d 20 31 3b 0a 20 20 7d 0a 20 20   res = 1;.  }.  
1a7b1 70 43 2d 3e 6e 75 6c 6c 52 6f 77 20 3d 20 72 65  pC->nullRow = re
1a7b2 73 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70  s;.  assert( pOp
1a7b3 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70 2d 3e 70  ->p2>0 && pOp->p
1a7b4 32 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 20 20 69 66  2<p->nOp );.  if
1a7b5 28 20 72 65 73 20 29 7b 0a 20 20 20 20 70 63 20  ( res ){.    pc 
1a7b6 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a 20  = pOp->p2 - 1;. 
1a7b7 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1a7b8 2a 20 4f 70 63 6f 64 65 3a 20 4e 65 78 74 20 50  * Opcode: Next P
1a7b9 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a  1 P2 * * *.**.**
1a7ba 20 41 64 76 61 6e 63 65 20 63 75 72 73 6f 72 20   Advance cursor 
1a7bb 50 31 20 73 6f 20 74 68 61 74 20 69 74 20 70 6f  P1 so that it po
1a7bc 69 6e 74 73 20 74 6f 20 74 68 65 20 6e 65 78 74  ints to the next
1a7bd 20 6b 65 79 2f 64 61 74 61 20 70 61 69 72 20 69   key/data pair i
1a7be 6e 20 69 74 73 0a 2a 2a 20 74 61 62 6c 65 20 6f  n its.** table o
1a7bf 72 20 69 6e 64 65 78 2e 20 20 49 66 20 74 68 65  r index.  If the
1a7c0 72 65 20 61 72 65 20 6e 6f 20 6d 6f 72 65 20 6b  re are no more k
1a7c1 65 79 2f 76 61 6c 75 65 20 70 61 69 72 73 20 74  ey/value pairs t
1a7c2 68 65 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  hen fall through
1a7c3 0a 2a 2a 20 74 6f 20 74 68 65 20 66 6f 6c 6c 6f  .** to the follo
1a7c4 77 69 6e 67 20 69 6e 73 74 72 75 63 74 69 6f 6e  wing instruction
1a7c5 2e 20 20 42 75 74 20 69 66 20 74 68 65 20 63 75  .  But if the cu
1a7c6 72 73 6f 72 20 61 64 76 61 6e 63 65 20 77 61 73  rsor advance was
1a7c7 20 73 75 63 63 65 73 73 66 75 6c 2c 0a 2a 2a 20   successful,.** 
1a7c8 6a 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79  jump immediately
1a7c9 20 74 6f 20 50 32 2e 0a 2a 2a 0a 2a 2a 20 54 68   to P2..**.** Th
1a7ca 65 20 50 31 20 63 75 72 73 6f 72 20 6d 75 73 74  e P1 cursor must
1a7cb 20 62 65 20 66 6f 72 20 61 20 72 65 61 6c 20 74   be for a real t
1a7cc 61 62 6c 65 2c 20 6e 6f 74 20 61 20 70 73 65 75  able, not a pseu
1a7cd 64 6f 2d 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20  do-table..**.** 
1a7ce 53 65 65 20 61 6c 73 6f 3a 20 50 72 65 76 0a 2a  See also: Prev.*
1a7cf 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 72 65  /./* Opcode: Pre
1a7d0 76 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a  v P1 P2 * * *.**
1a7d1 0a 2a 2a 20 42 61 63 6b 20 75 70 20 63 75 72 73  .** Back up curs
1a7d2 6f 72 20 50 31 20 73 6f 20 74 68 61 74 20 69 74  or P1 so that it
1a7d3 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20 70   points to the p
1a7d4 72 65 76 69 6f 75 73 20 6b 65 79 2f 64 61 74 61  revious key/data
1a7d5 20 70 61 69 72 20 69 6e 20 69 74 73 0a 2a 2a 20   pair in its.** 
1a7d6 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
1a7d7 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
1a7d8 70 72 65 76 69 6f 75 73 20 6b 65 79 2f 76 61 6c  previous key/val
1a7d9 75 65 20 70 61 69 72 73 20 74 68 65 6e 20 66 61  ue pairs then fa
1a7da 6c 6c 20 74 68 72 6f 75 67 68 0a 2a 2a 20 74 6f  ll through.** to
1a7db 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69   the following i
1a7dc 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 42 75 74  nstruction.  But
1a7dd 20 69 66 20 74 68 65 20 63 75 72 73 6f 72 20 62   if the cursor b
1a7de 61 63 6b 75 70 20 77 61 73 20 73 75 63 63 65 73  ackup was succes
1a7df 73 66 75 6c 2c 0a 2a 2a 20 6a 75 6d 70 20 69 6d  sful,.** jump im
1a7e0 6d 65 64 69 61 74 65 6c 79 20 74 6f 20 50 32 2e  mediately to P2.
1a7e1 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 31 20 63 75  .**.** The P1 cu
1a7e2 72 73 6f 72 20 6d 75 73 74 20 62 65 20 66 6f 72  rsor must be for
1a7e3 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c 20 6e   a real table, n
1a7e4 6f 74 20 61 20 70 73 65 75 64 6f 2d 74 61 62 6c  ot a pseudo-tabl
1a7e5 65 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 72  e..*/.case OP_Pr
1a7e6 65 76 3a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ev:          /* 
1a7e7 6a 75 6d 70 20 2a 2f 0a 63 61 73 65 20 4f 50 5f  jump */.case OP_
1a7e8 4e 65 78 74 3a 20 7b 20 20 20 20 20 20 20 20 2f  Next: {        /
1a7e9 2a 20 6a 75 6d 70 20 2a 2f 0a 20 20 56 64 62 65  * jump */.  Vdbe
1a7ea 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1a7eb 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1a7ec 20 69 6e 74 20 72 65 73 3b 0a 0a 20 20 43 48 45   int res;..  CHE
1a7ed 43 4b 5f 46 4f 52 5f 49 4e 54 45 52 52 55 50 54  CK_FOR_INTERRUPT
1a7ee 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d  ;.  assert( pOp-
1a7ef 3e 70 31 3e 3d 30 20 26 26 20 70 4f 70 2d 3e 70  >p1>=0 && pOp->p
1a7f0 31 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a  1<p->nCursor );.
1a7f1 20 20 70 43 20 3d 20 70 2d 3e 61 70 43 73 72 5b    pC = p->apCsr[
1a7f2 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 69 66 28 20  pOp->p1];.  if( 
1a7f3 70 43 3d 3d 30 20 29 7b 0a 20 20 20 20 62 72 65  pC==0 ){.    bre
1a7f4 61 6b 3b 20 20 2f 2a 20 53 65 65 20 74 69 63 6b  ak;  /* See tick
1a7f5 65 74 20 23 32 32 37 33 20 2a 2f 0a 20 20 7d 0a  et #2273 */.  }.
1a7f6 20 20 70 43 72 73 72 20 3d 20 70 43 2d 3e 70 43    pCrsr = pC->pC
1a7f7 75 72 73 6f 72 3b 0a 20 20 61 73 73 65 72 74 28  ursor;.  assert(
1a7f8 20 70 43 72 73 72 20 29 3b 0a 20 20 72 65 73 20   pCrsr );.  res 
1a7f9 3d 20 31 3b 0a 20 20 61 73 73 65 72 74 28 20 70  = 1;.  assert( p
1a7fa 43 2d 3e 64 65 66 65 72 72 65 64 4d 6f 76 65 74  C->deferredMovet
1a7fb 6f 3d 3d 30 20 29 3b 0a 20 20 72 63 20 3d 20 70  o==0 );.  rc = p
1a7fc 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4e  Op->opcode==OP_N
1a7fd 65 78 74 20 3f 20 73 71 6c 69 74 65 33 42 74 72  ext ? sqlite3Btr
1a7fe 65 65 4e 65 78 74 28 70 43 72 73 72 2c 20 26 72  eeNext(pCrsr, &r
1a7ff 65 73 29 20 3a 0a 20 20 20 20 20 20 20 20 20 20  es) :.          
1a800 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1a801 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65      sqlite3Btree
1a802 50 72 65 76 69 6f 75 73 28 70 43 72 73 72 2c 20  Previous(pCrsr, 
1a803 26 72 65 73 29 3b 0a 20 20 70 43 2d 3e 6e 75 6c  &res);.  pC->nul
1a804 6c 52 6f 77 20 3d 20 72 65 73 3b 0a 20 20 70 43  lRow = res;.  pC
1a805 2d 3e 63 61 63 68 65 53 74 61 74 75 73 20 3d 20  ->cacheStatus = 
1a806 43 41 43 48 45 5f 53 54 41 4c 45 3b 0a 20 20 69  CACHE_STALE;.  i
1a807 66 28 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20  f( res==0 ){.   
1a808 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20   pc = pOp->p2 - 
1a809 31 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  1;.    if( pOp->
1a80a 70 35 20 29 20 70 2d 3e 61 43 6f 75 6e 74 65 72  p5 ) p->aCounter
1a80b 5b 70 4f 70 2d 3e 70 35 2d 31 5d 2b 2b 3b 0a 23  [pOp->p5-1]++;.#
1a80c 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53  ifdef SQLITE_TES
1a80d 54 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 73 65  T.    sqlite3_se
1a80e 61 72 63 68 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65  arch_count++;.#e
1a80f 6e 64 69 66 0a 20 20 7d 0a 20 20 70 43 2d 3e 72  ndif.  }.  pC->r
1a810 6f 77 69 64 49 73 56 61 6c 69 64 20 3d 20 30 3b  owidIsValid = 0;
1a811 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1a812 4f 70 63 6f 64 65 3a 20 49 64 78 49 6e 73 65 72  Opcode: IdxInser
1a813 74 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  t P1 P2 P3 * *.*
1a814 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 50 32  *.** Register P2
1a815 20 68 6f 6c 64 73 20 61 20 53 51 4c 20 69 6e 64   holds a SQL ind
1a816 65 78 20 6b 65 79 20 6d 61 64 65 20 75 73 69 6e  ex key made usin
1a817 67 20 74 68 65 0a 2a 2a 20 4d 61 6b 65 49 64 78  g the.** MakeIdx
1a818 52 65 63 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  Rec instructions
1a819 2e 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 77  .  This opcode w
1a81a 72 69 74 65 73 20 74 68 61 74 20 6b 65 79 0a 2a  rites that key.*
1a81b 2a 20 69 6e 74 6f 20 74 68 65 20 69 6e 64 65 78  * into the index
1a81c 20 50 31 2e 20 20 44 61 74 61 20 66 6f 72 20 74   P1.  Data for t
1a81d 68 65 20 65 6e 74 72 79 20 69 73 20 6e 69 6c 2e  he entry is nil.
1a81e 0a 2a 2a 0a 2a 2a 20 50 33 20 69 73 20 61 20 66  .**.** P3 is a f
1a81f 6c 61 67 20 74 68 61 74 20 70 72 6f 76 69 64 65  lag that provide
1a820 73 20 61 20 68 69 6e 74 20 74 6f 20 74 68 65 20  s a hint to the 
1a821 62 2d 74 72 65 65 20 6c 61 79 65 72 20 74 68 61  b-tree layer tha
1a822 74 20 74 68 69 73 0a 2a 2a 20 69 6e 73 65 72 74  t this.** insert
1a823 20 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65   is likely to be
1a824 20 61 6e 20 61 70 70 65 6e 64 2e 0a 2a 2a 0a 2a   an append..**.*
1a825 2a 20 54 68 69 73 20 69 6e 73 74 72 75 63 74 69  * This instructi
1a826 6f 6e 20 6f 6e 6c 79 20 77 6f 72 6b 73 20 66 6f  on only works fo
1a827 72 20 69 6e 64 69 63 65 73 2e 20 20 54 68 65 20  r indices.  The 
1a828 65 71 75 69 76 61 6c 65 6e 74 20 69 6e 73 74 72  equivalent instr
1a829 75 63 74 69 6f 6e 0a 2a 2a 20 66 6f 72 20 74 61  uction.** for ta
1a82a 62 6c 65 73 20 69 73 20 4f 50 5f 49 6e 73 65 72  bles is OP_Inser
1a82b 74 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64  t..*/.case OP_Id
1a82c 78 49 6e 73 65 72 74 3a 20 7b 20 20 20 20 20 20  xInsert: {      
1a82d 20 20 2f 2a 20 69 6e 32 20 2a 2f 0a 20 20 69 6e    /* in2 */.  in
1a82e 74 20 69 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20  t i = pOp->p1;. 
1a82f 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b   VdbeCursor *pC;
1a830 0a 20 20 42 74 43 75 72 73 6f 72 20 2a 70 43 72  .  BtCursor *pCr
1a831 73 72 3b 0a 20 20 61 73 73 65 72 74 28 20 69 3e  sr;.  assert( i>
1a832 3d 30 20 26 26 20 69 3c 70 2d 3e 6e 43 75 72 73  =0 && i<p->nCurs
1a833 6f 72 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  or );.  assert( 
1a834 70 2d 3e 61 70 43 73 72 5b 69 5d 21 3d 30 20 29  p->apCsr[i]!=0 )
1a835 3b 0a 20 20 61 73 73 65 72 74 28 20 70 49 6e 32  ;.  assert( pIn2
1a836 2d 3e 66 6c 61 67 73 20 26 20 4d 45 4d 5f 42 6c  ->flags & MEM_Bl
1a837 6f 62 20 29 3b 0a 20 20 69 66 28 20 28 70 43 72  ob );.  if( (pCr
1a838 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61 70  sr = (pC = p->ap
1a839 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72  Csr[i])->pCursor
1a83a 29 21 3d 30 20 29 7b 0a 20 20 20 20 61 73 73 65  )!=0 ){.    asse
1a83b 72 74 28 20 70 43 2d 3e 69 73 54 61 62 6c 65 3d  rt( pC->isTable=
1a83c 3d 30 20 29 3b 0a 20 20 20 20 72 63 20 3d 20 45  =0 );.    rc = E
1a83d 78 70 61 6e 64 42 6c 6f 62 28 70 49 6e 32 29 3b  xpandBlob(pIn2);
1a83e 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1a83f 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1a840 69 6e 74 20 6e 4b 65 79 20 3d 20 70 49 6e 32 2d  int nKey = pIn2-
1a841 3e 6e 3b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  >n;.      const 
1a842 63 68 61 72 20 2a 7a 4b 65 79 20 3d 20 70 49 6e  char *zKey = pIn
1a843 32 2d 3e 7a 3b 0a 20 20 20 20 20 20 72 63 20 3d  2->z;.      rc =
1a844 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 6e 73   sqlite3BtreeIns
1a845 65 72 74 28 70 43 72 73 72 2c 20 7a 4b 65 79 2c  ert(pCrsr, zKey,
1a846 20 6e 4b 65 79 2c 20 22 22 2c 20 30 2c 20 30 2c   nKey, "", 0, 0,
1a847 20 70 4f 70 2d 3e 70 33 29 3b 0a 20 20 20 20 20   pOp->p3);.     
1a848 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66   assert( pC->def
1a849 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1a84a 3b 0a 20 20 20 20 20 20 70 43 2d 3e 63 61 63 68  ;.      pC->cach
1a84b 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1a84c 53 54 41 4c 45 3b 0a 20 20 20 20 7d 0a 20 20 7d  STALE;.    }.  }
1a84d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20  .  break;.}../* 
1a84e 4f 70 63 6f 64 65 3a 20 49 64 78 44 65 6c 65 74  Opcode: IdxDelet
1a84f 65 20 50 31 20 50 32 20 50 33 20 2a 20 2a 0a 2a  e P1 P2 P3 * *.*
1a850 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 6e 74  *.** The content
1a851 20 6f 66 20 50 33 20 72 65 67 69 73 74 65 72 73   of P3 registers
1a852 20 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67   starting at reg
1a853 69 73 74 65 72 20 50 32 20 66 6f 72 6d 0a 2a 2a  ister P2 form.**
1a854 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69 6e 64   an unpacked ind
1a855 65 78 20 6b 65 79 2e 20 54 68 69 73 20 6f 70 63  ex key. This opc
1a856 6f 64 65 20 72 65 6d 6f 76 65 73 20 74 68 61 74  ode removes that
1a857 20 65 6e 74 72 79 20 66 72 6f 6d 20 74 68 65 20   entry from the 
1a858 0a 2a 2a 20 69 6e 64 65 78 20 6f 70 65 6e 65 64  .** index opened
1a859 20 62 79 20 63 75 72 73 6f 72 20 50 31 2e 0a 2a   by cursor P1..*
1a85a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 44 65 6c  /.case OP_IdxDel
1a85b 65 74 65 3a 20 7b 0a 20 20 69 6e 74 20 69 20 3d  ete: {.  int i =
1a85c 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56 64 62 65   pOp->p1;.  Vdbe
1a85d 43 75 72 73 6f 72 20 2a 70 43 3b 0a 20 20 42 74  Cursor *pC;.  Bt
1a85e 43 75 72 73 6f 72 20 2a 70 43 72 73 72 3b 0a 20  Cursor *pCrsr;. 
1a85f 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1a860 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
1a861 70 4f 70 2d 3e 70 32 3e 30 20 26 26 20 70 4f 70  pOp->p2>0 && pOp
1a862 2d 3e 70 32 2b 70 4f 70 2d 3e 70 33 3c 3d 70 2d  ->p2+pOp->p3<=p-
1a863 3e 6e 4d 65 6d 20 29 3b 0a 20 20 61 73 73 65 72  >nMem );.  asser
1a864 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 70 2d 3e  t( i>=0 && i<p->
1a865 6e 43 75 72 73 6f 72 20 29 3b 0a 20 20 61 73 73  nCursor );.  ass
1a866 65 72 74 28 20 70 2d 3e 61 70 43 73 72 5b 69 5d  ert( p->apCsr[i]
1a867 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 28 70 43  !=0 );.  if( (pC
1a868 72 73 72 20 3d 20 28 70 43 20 3d 20 70 2d 3e 61  rsr = (pC = p->a
1a869 70 43 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f  pCsr[i])->pCurso
1a86a 72 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  r)!=0 ){.    int
1a86b 20 72 65 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b   res;.    Unpack
1a86c 65 64 52 65 63 6f 72 64 20 72 3b 0a 20 20 20 20  edRecord r;.    
1a86d 72 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d  r.pKeyInfo = pC-
1a86e 3e 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72  >pKeyInfo;.    r
1a86f 2e 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70  .nField = pOp->p
1a870 33 3b 0a 20 20 20 20 72 2e 66 6c 61 67 73 20 3d  3;.    r.flags =
1a871 20 30 3b 0a 20 20 20 20 72 2e 61 4d 65 6d 20 3d   0;.    r.aMem =
1a872 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1a873 32 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c  2];.    rc = sql
1a874 69 74 65 33 42 74 72 65 65 4d 6f 76 65 74 6f 55  ite3BtreeMovetoU
1a875 6e 70 61 63 6b 65 64 28 70 43 72 73 72 2c 20 26  npacked(pCrsr, &
1a876 72 2c 20 30 2c 20 30 2c 20 26 72 65 73 29 3b 0a  r, 0, 0, &res);.
1a877 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
1a878 54 45 5f 4f 4b 20 26 26 20 72 65 73 3d 3d 30 20  TE_OK && res==0 
1a879 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71  ){.      rc = sq
1a87a 6c 69 74 65 33 42 74 72 65 65 44 65 6c 65 74 65  lite3BtreeDelete
1a87b 28 70 43 72 73 72 29 3b 0a 20 20 20 20 7d 0a 20  (pCrsr);.    }. 
1a87c 20 20 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64     assert( pC->d
1a87d 65 66 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30  eferredMoveto==0
1a87e 20 29 3b 0a 20 20 20 20 70 43 2d 3e 63 61 63 68   );.    pC->cach
1a87f 65 53 74 61 74 75 73 20 3d 20 43 41 43 48 45 5f  eStatus = CACHE_
1a880 53 54 41 4c 45 3b 0a 20 20 7d 0a 20 20 62 72 65  STALE;.  }.  bre
1a881 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1a882 3a 20 49 64 78 52 6f 77 69 64 20 50 31 20 50 32  : IdxRowid P1 P2
1a883 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57 72 69   * * *.**.** Wri
1a884 74 65 20 69 6e 74 6f 20 72 65 67 69 73 74 65 72  te into register
1a885 20 50 32 20 61 6e 20 69 6e 74 65 67 65 72 20 77   P2 an integer w
1a886 68 69 63 68 20 69 73 20 74 68 65 20 6c 61 73 74  hich is the last
1a887 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20 72 65   entry in the re
1a888 63 6f 72 64 20 61 74 0a 2a 2a 20 74 68 65 20 65  cord at.** the e
1a889 6e 64 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20  nd of the index 
1a88a 6b 65 79 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  key pointed to b
1a88b 79 20 63 75 72 73 6f 72 20 50 31 2e 20 20 54 68  y cursor P1.  Th
1a88c 69 73 20 69 6e 74 65 67 65 72 20 73 68 6f 75 6c  is integer shoul
1a88d 64 20 62 65 0a 2a 2a 20 74 68 65 20 72 6f 77 69  d be.** the rowi
1a88e 64 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 65  d of the table e
1a88f 6e 74 72 79 20 74 6f 20 77 68 69 63 68 20 74 68  ntry to which th
1a890 69 73 20 69 6e 64 65 78 20 65 6e 74 72 79 20 70  is index entry p
1a891 6f 69 6e 74 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65  oints..**.** See
1a892 20 61 6c 73 6f 3a 20 52 6f 77 69 64 2c 20 4d 61   also: Rowid, Ma
1a893 6b 65 49 64 78 52 65 63 2e 0a 2a 2f 0a 63 61 73  keIdxRec..*/.cas
1a894 65 20 4f 50 5f 49 64 78 52 6f 77 69 64 3a 20 7b  e OP_IdxRowid: {
1a895 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1a896 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65   out2-prerelease
1a897 20 2a 2f 0a 20 20 69 6e 74 20 69 20 3d 20 70 4f   */.  int i = pO
1a898 70 2d 3e 70 31 3b 0a 20 20 42 74 43 75 72 73 6f  p->p1;.  BtCurso
1a899 72 20 2a 70 43 72 73 72 3b 0a 20 20 56 64 62 65  r *pCrsr;.  Vdbe
1a89a 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a 20 20 61  Cursor *pC;..  a
1a89b 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69  ssert( i>=0 && i
1a89c 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29 3b 0a 20  <p->nCursor );. 
1a89d 20 61 73 73 65 72 74 28 20 70 2d 3e 61 70 43 73   assert( p->apCs
1a89e 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28  r[i]!=0 );.  if(
1a89f 20 28 70 43 72 73 72 20 3d 20 28 70 43 20 3d 20   (pCrsr = (pC = 
1a8a0 70 2d 3e 61 70 43 73 72 5b 69 5d 29 2d 3e 70 43  p->apCsr[i])->pC
1a8a1 75 72 73 6f 72 29 21 3d 30 20 29 7b 0a 20 20 20  ursor)!=0 ){.   
1a8a2 20 69 36 34 20 72 6f 77 69 64 3b 0a 0a 20 20 20   i64 rowid;..   
1a8a3 20 61 73 73 65 72 74 28 20 70 43 2d 3e 64 65 66   assert( pC->def
1a8a4 65 72 72 65 64 4d 6f 76 65 74 6f 3d 3d 30 20 29  erredMoveto==0 )
1a8a5 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 43  ;.    assert( pC
1a8a6 2d 3e 69 73 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  ->isTable==0 );.
1a8a7 20 20 20 20 69 66 28 20 21 70 43 2d 3e 6e 75 6c      if( !pC->nul
1a8a8 6c 52 6f 77 20 29 7b 0a 20 20 20 20 20 20 72 63  lRow ){.      rc
1a8a9 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64   = sqlite3VdbeId
1a8aa 78 52 6f 77 69 64 28 70 43 72 73 72 2c 20 26 72  xRowid(pCrsr, &r
1a8ab 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 69 66 28  owid);.      if(
1a8ac 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
1a8ad 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 61  {.        goto a
1a8ae 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f  bort_due_to_erro
1a8af 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  r;.      }.     
1a8b0 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28   MemSetTypeFlag(
1a8b1 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a  pOut, MEM_Int);.
1a8b2 20 20 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20        pOut->u.i 
1a8b3 3d 20 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20  = rowid;.    }. 
1a8b4 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f   }.  break;.}../
1a8b5 2a 20 4f 70 63 6f 64 65 3a 20 49 64 78 47 45 20  * Opcode: IdxGE 
1a8b6 50 31 20 50 32 20 50 33 20 50 34 20 50 35 0a 2a  P1 P2 P3 P4 P5.*
1a8b7 2a 0a 2a 2a 20 54 68 65 20 50 34 20 72 65 67 69  *.** The P4 regi
1a8b8 73 74 65 72 20 76 61 6c 75 65 73 20 62 65 67 69  ster values begi
1a8b9 6e 6e 69 6e 67 20 77 69 74 68 20 50 33 20 66 6f  nning with P3 fo
1a8ba 72 6d 20 61 6e 20 75 6e 70 61 63 6b 65 64 20 69  rm an unpacked i
1a8bb 6e 64 65 78 20 0a 2a 2a 20 6b 65 79 20 74 68 61  ndex .** key tha
1a8bc 74 20 6f 6d 69 74 73 20 74 68 65 20 52 4f 57 49  t omits the ROWI
1a8bd 44 2e 20 20 43 6f 6d 70 61 72 65 20 74 68 69 73  D.  Compare this
1a8be 20 6b 65 79 20 76 61 6c 75 65 20 61 67 61 69 6e   key value again
1a8bf 73 74 20 74 68 65 20 69 6e 64 65 78 20 0a 2a 2a  st the index .**
1a8c0 20 74 68 61 74 20 50 31 20 69 73 20 63 75 72 72   that P1 is curr
1a8c1 65 6e 74 6c 79 20 70 6f 69 6e 74 69 6e 67 20 74  ently pointing t
1a8c2 6f 2c 20 69 67 6e 6f 72 69 6e 67 20 74 68 65 20  o, ignoring the 
1a8c3 52 4f 57 49 44 20 6f 6e 20 74 68 65 20 50 31 20  ROWID on the P1 
1a8c4 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  index..**.** If 
1a8c5 74 68 65 20 50 31 20 69 6e 64 65 78 20 65 6e 74  the P1 index ent
1a8c6 72 79 20 69 73 20 67 72 65 61 74 65 72 20 74 68  ry is greater th
1a8c7 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74  an or equal to t
1a8c8 68 65 20 6b 65 79 20 76 61 6c 75 65 0a 2a 2a 20  he key value.** 
1a8c9 74 68 65 6e 20 6a 75 6d 70 20 74 6f 20 50 32 2e  then jump to P2.
1a8ca 20 20 4f 74 68 65 72 77 69 73 65 20 66 61 6c 6c    Otherwise fall
1a8cb 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65 20   through to the 
1a8cc 6e 65 78 74 20 69 6e 73 74 72 75 63 74 69 6f 6e  next instruction
1a8cd 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69 73  ..**.** If P5 is
1a8ce 20 6e 6f 6e 2d 7a 65 72 6f 20 74 68 65 6e 20 74   non-zero then t
1a8cf 68 65 20 6b 65 79 20 76 61 6c 75 65 20 69 73 20  he key value is 
1a8d0 69 6e 63 72 65 61 73 65 64 20 62 79 20 61 6e 20  increased by an 
1a8d1 65 70 73 69 6c 6f 6e 20 0a 2a 2a 20 70 72 69 6f  epsilon .** prio
1a8d2 72 20 74 6f 20 74 68 65 20 63 6f 6d 70 61 72 69  r to the compari
1a8d3 73 6f 6e 2e 20 20 54 68 69 73 20 6d 61 6b 65 20  son.  This make 
1a8d4 74 68 65 20 6f 70 63 6f 64 65 20 77 6f 72 6b 20  the opcode work 
1a8d5 6c 69 6b 65 20 49 64 78 47 54 20 65 78 63 65 70  like IdxGT excep
1a8d6 74 0a 2a 2a 20 74 68 61 74 20 69 66 20 74 68 65  t.** that if the
1a8d7 20 6b 65 79 20 66 72 6f 6d 20 72 65 67 69 73 74   key from regist
1a8d8 65 72 20 50 33 20 69 73 20 61 20 70 72 65 66 69  er P3 is a prefi
1a8d9 78 20 6f 66 20 74 68 65 20 6b 65 79 20 69 6e 20  x of the key in 
1a8da 74 68 65 20 63 75 72 73 6f 72 2c 0a 2a 2a 20 74  the cursor,.** t
1a8db 68 65 20 72 65 73 75 6c 74 20 69 73 20 66 61 6c  he result is fal
1a8dc 73 65 20 77 68 65 72 65 61 73 20 69 74 20 77 6f  se whereas it wo
1a8dd 75 6c 64 20 62 65 20 74 72 75 65 20 77 69 74 68  uld be true with
1a8de 20 49 64 78 47 54 2e 0a 2a 2f 0a 2f 2a 20 4f 70   IdxGT..*/./* Op
1a8df 63 6f 64 65 3a 20 49 64 78 4c 54 20 50 31 20 50  code: IdxLT P1 P
1a8e0 32 20 50 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20  2 P3 * P5.**.** 
1a8e1 54 68 65 20 50 34 20 72 65 67 69 73 74 65 72 20  The P4 register 
1a8e2 76 61 6c 75 65 73 20 62 65 67 69 6e 6e 69 6e 67  values beginning
1a8e3 20 77 69 74 68 20 50 33 20 66 6f 72 6d 20 61 6e   with P3 form an
1a8e4 20 75 6e 70 61 63 6b 65 64 20 69 6e 64 65 78 20   unpacked index 
1a8e5 0a 2a 2a 20 6b 65 79 20 74 68 61 74 20 6f 6d 69  .** key that omi
1a8e6 74 73 20 74 68 65 20 52 4f 57 49 44 2e 20 20 43  ts the ROWID.  C
1a8e7 6f 6d 70 61 72 65 20 74 68 69 73 20 6b 65 79 20  ompare this key 
1a8e8 76 61 6c 75 65 20 61 67 61 69 6e 73 74 20 74 68  value against th
1a8e9 65 20 69 6e 64 65 78 20 0a 2a 2a 20 74 68 61 74  e index .** that
1a8ea 20 50 31 20 69 73 20 63 75 72 72 65 6e 74 6c 79   P1 is currently
1a8eb 20 70 6f 69 6e 74 69 6e 67 20 74 6f 2c 20 69 67   pointing to, ig
1a8ec 6e 6f 72 69 6e 67 20 74 68 65 20 52 4f 57 49 44  noring the ROWID
1a8ed 20 6f 6e 20 74 68 65 20 50 31 20 69 6e 64 65 78   on the P1 index
1a8ee 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 50  ..**.** If the P
1a8ef 31 20 69 6e 64 65 78 20 65 6e 74 72 79 20 69 73  1 index entry is
1a8f0 20 6c 65 73 73 20 74 68 61 6e 20 74 68 65 20 6b   less than the k
1a8f1 65 79 20 76 61 6c 75 65 20 74 68 65 6e 20 6a 75  ey value then ju
1a8f2 6d 70 20 74 6f 20 50 32 2e 0a 2a 2a 20 4f 74 68  mp to P2..** Oth
1a8f3 65 72 77 69 73 65 20 66 61 6c 6c 20 74 68 72 6f  erwise fall thro
1a8f4 75 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20  ugh to the next 
1a8f5 69 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2a 0a  instruction..**.
1a8f6 2a 2a 20 49 66 20 50 35 20 69 73 20 6e 6f 6e 2d  ** If P5 is non-
1a8f7 7a 65 72 6f 20 74 68 65 6e 20 74 68 65 20 6b 65  zero then the ke
1a8f8 79 20 76 61 6c 75 65 20 69 73 20 69 6e 63 72 65  y value is incre
1a8f9 61 73 65 64 20 62 79 20 61 6e 20 65 70 73 69 6c  ased by an epsil
1a8fa 6f 6e 20 70 72 69 6f 72 20 0a 2a 2a 20 74 6f 20  on prior .** to 
1a8fb 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20  the comparison. 
1a8fc 20 54 68 69 73 20 6d 61 6b 65 73 20 74 68 65 20   This makes the 
1a8fd 6f 70 63 6f 64 65 20 77 6f 72 6b 20 6c 69 6b 65  opcode work like
1a8fe 20 49 64 78 4c 45 2e 0a 2a 2f 0a 63 61 73 65 20   IdxLE..*/.case 
1a8ff 4f 50 5f 49 64 78 4c 54 3a 20 20 20 20 20 20 20  OP_IdxLT:       
1a900 20 20 20 2f 2a 20 6a 75 6d 70 2c 20 69 6e 33 20     /* jump, in3 
1a901 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 64 78 47 45  */.case OP_IdxGE
1a902 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1a903 6d 70 2c 20 69 6e 33 20 2a 2f 0a 20 20 69 6e 74  mp, in3 */.  int
1a904 20 69 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 56   i= pOp->p1;.  V
1a905 64 62 65 43 75 72 73 6f 72 20 2a 70 43 3b 0a 0a  dbeCursor *pC;..
1a906 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26    assert( i>=0 &
1a907 26 20 69 3c 70 2d 3e 6e 43 75 72 73 6f 72 20 29  & i<p->nCursor )
1a908 3b 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61  ;.  assert( p->a
1a909 70 43 73 72 5b 69 5d 21 3d 30 20 29 3b 0a 20 20  pCsr[i]!=0 );.  
1a90a 69 66 28 20 28 70 43 20 3d 20 70 2d 3e 61 70 43  if( (pC = p->apC
1a90b 73 72 5b 69 5d 29 2d 3e 70 43 75 72 73 6f 72 21  sr[i])->pCursor!
1a90c 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65  =0 ){.    int re
1a90d 73 3b 0a 20 20 20 20 55 6e 70 61 63 6b 65 64 52  s;.    UnpackedR
1a90e 65 63 6f 72 64 20 72 3b 0a 20 20 20 20 61 73 73  ecord r;.    ass
1a90f 65 72 74 28 20 70 43 2d 3e 64 65 66 65 72 72 65  ert( pC->deferre
1a910 64 4d 6f 76 65 74 6f 3d 3d 30 20 29 3b 0a 20 20  dMoveto==0 );.  
1a911 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
1a912 35 3d 3d 30 20 7c 7c 20 70 4f 70 2d 3e 70 35 3d  5==0 || pOp->p5=
1a913 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =1 );.    assert
1a914 28 20 70 4f 70 2d 3e 70 34 74 79 70 65 3d 3d 50  ( pOp->p4type==P
1a915 34 5f 49 4e 54 33 32 20 29 3b 0a 20 20 20 20 72  4_INT32 );.    r
1a916 2e 70 4b 65 79 49 6e 66 6f 20 3d 20 70 43 2d 3e  .pKeyInfo = pC->
1a917 70 4b 65 79 49 6e 66 6f 3b 0a 20 20 20 20 72 2e  pKeyInfo;.    r.
1a918 6e 46 69 65 6c 64 20 3d 20 70 4f 70 2d 3e 70 34  nField = pOp->p4
1a919 2e 69 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  .i;.    if( pOp-
1a91a 3e 70 35 20 29 7b 0a 20 20 20 20 20 20 72 2e 66  >p5 ){.      r.f
1a91b 6c 61 67 73 20 3d 20 55 4e 50 41 43 4b 45 44 5f  lags = UNPACKED_
1a91c 49 4e 43 52 4b 45 59 20 7c 20 55 4e 50 41 43 4b  INCRKEY | UNPACK
1a91d 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49 44 3b  ED_IGNORE_ROWID;
1a91e 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
1a91f 20 20 72 2e 66 6c 61 67 73 20 3d 20 55 4e 50 41    r.flags = UNPA
1a920 43 4b 45 44 5f 49 47 4e 4f 52 45 5f 52 4f 57 49  CKED_IGNORE_ROWI
1a921 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 2e 61  D;.    }.    r.a
1a922 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70  Mem = &p->aMem[p
1a923 4f 70 2d 3e 70 33 5d 3b 0a 20 20 20 20 72 63 20  Op->p3];.    rc 
1a924 3d 20 73 71 6c 69 74 65 33 56 64 62 65 49 64 78  = sqlite3VdbeIdx
1a925 4b 65 79 43 6f 6d 70 61 72 65 28 70 43 2c 20 26  KeyCompare(pC, &
1a926 72 2c 20 26 72 65 73 29 3b 0a 20 20 20 20 69 66  r, &res);.    if
1a927 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  ( pOp->opcode==O
1a928 50 5f 49 64 78 4c 54 20 29 7b 0a 20 20 20 20 20  P_IdxLT ){.     
1a929 20 72 65 73 20 3d 20 2d 72 65 73 3b 0a 20 20 20   res = -res;.   
1a92a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 61 73   }else{.      as
1a92b 73 65 72 74 28 20 70 4f 70 2d 3e 6f 70 63 6f 64  sert( pOp->opcod
1a92c 65 3d 3d 4f 50 5f 49 64 78 47 45 20 29 3b 0a 20  e==OP_IdxGE );. 
1a92d 20 20 20 20 20 72 65 73 2b 2b 3b 0a 20 20 20 20       res++;.    
1a92e 7d 0a 20 20 20 20 69 66 28 20 72 65 73 3e 30 20  }.    if( res>0 
1a92f 29 7b 0a 20 20 20 20 20 20 70 63 20 3d 20 70 4f  ){.      pc = pO
1a930 70 2d 3e 70 32 20 2d 20 31 20 3b 0a 20 20 20 20  p->p2 - 1 ;.    
1a931 7d 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d  }.  }.  break;.}
1a932 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 65 73  ../* Opcode: Des
1a933 74 72 6f 79 20 50 31 20 50 32 20 50 33 20 2a 20  troy P1 P2 P3 * 
1a934 2a 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61  *.**.** Delete a
1a935 6e 20 65 6e 74 69 72 65 20 64 61 74 61 62 61 73  n entire databas
1a936 65 20 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78  e table or index
1a937 20 77 68 6f 73 65 20 72 6f 6f 74 20 70 61 67 65   whose root page
1a938 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
1a939 0a 2a 2a 20 66 69 6c 65 20 69 73 20 67 69 76 65  .** file is give
1a93a 6e 20 62 79 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54  n by P1..**.** T
1a93b 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20 64  he table being d
1a93c 65 73 74 72 6f 79 65 64 20 69 73 20 69 6e 20 74  estroyed is in t
1a93d 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1a93e 20 66 69 6c 65 20 69 66 20 50 33 3d 3d 30 2e 20   file if P3==0. 
1a93f 20 49 66 0a 2a 2a 20 50 33 3d 3d 31 20 74 68 65   If.** P3==1 the
1a940 6e 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 62  n the table to b
1a941 65 20 63 6c 65 61 72 20 69 73 20 69 6e 20 74 68  e clear is in th
1a942 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61  e auxiliary data
1a943 62 61 73 65 20 66 69 6c 65 0a 2a 2a 20 74 68 61  base file.** tha
1a944 74 20 69 73 20 75 73 65 64 20 74 6f 20 73 74 6f  t is used to sto
1a945 72 65 20 74 61 62 6c 65 73 20 63 72 65 61 74 65  re tables create
1a946 20 75 73 69 6e 67 20 43 52 45 41 54 45 20 54 45   using CREATE TE
1a947 4d 50 4f 52 41 52 59 20 54 41 42 4c 45 2e 0a 2a  MPORARY TABLE..*
1a948 2a 0a 2a 2a 20 49 66 20 41 55 54 4f 56 41 43 55  *.** If AUTOVACU
1a949 55 4d 20 69 73 20 65 6e 61 62 6c 65 64 20 74 68  UM is enabled th
1a94a 65 6e 20 69 74 20 69 73 20 70 6f 73 73 69 62 6c  en it is possibl
1a94b 65 20 74 68 61 74 20 61 6e 6f 74 68 65 72 20 72  e that another r
1a94c 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 6d 69 67 68  oot page.** migh
1a94d 74 20 62 65 20 6d 6f 76 65 64 20 69 6e 74 6f 20  t be moved into 
1a94e 74 68 65 20 6e 65 77 6c 79 20 64 65 6c 65 74 65  the newly delete
1a94f 64 20 72 6f 6f 74 20 70 61 67 65 20 69 6e 20 6f  d root page in o
1a950 72 64 65 72 20 74 6f 20 6b 65 65 70 20 61 6c 6c  rder to keep all
1a951 0a 2a 2a 20 72 6f 6f 74 20 70 61 67 65 73 20 63  .** root pages c
1a952 6f 6e 74 69 67 75 6f 75 73 20 61 74 20 74 68 65  ontiguous at the
1a953 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
1a954 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 65  e database.  The
1a955 20 66 6f 72 6d 65 72 0a 2a 2a 20 76 61 6c 75 65   former.** value
1a956 20 6f 66 20 74 68 65 20 72 6f 6f 74 20 70 61 67   of the root pag
1a957 65 20 74 68 61 74 20 6d 6f 76 65 64 20 2d 20 69  e that moved - i
1a958 74 73 20 76 61 6c 75 65 20 62 65 66 6f 72 65 20  ts value before 
1a959 74 68 65 20 6d 6f 76 65 20 6f 63 63 75 72 72 65  the move occurre
1a95a 64 20 2d 0a 2a 2a 20 69 73 20 73 74 6f 72 65 64  d -.** is stored
1a95b 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1a95c 20 20 49 66 20 6e 6f 20 70 61 67 65 20 0a 2a 2a    If no page .**
1a95d 20 6d 6f 76 65 6d 65 6e 74 20 77 61 73 20 72 65   movement was re
1a95e 71 75 69 72 65 64 20 28 62 65 63 61 75 73 65 20  quired (because 
1a95f 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
1a960 64 72 6f 70 70 65 64 20 77 61 73 20 61 6c 72 65  dropped was alre
1a961 61 64 79 20 0a 2a 2a 20 74 68 65 20 6c 61 73 74  ady .** the last
1a962 20 6f 6e 65 20 69 6e 20 74 68 65 20 64 61 74 61   one in the data
1a963 62 61 73 65 29 20 74 68 65 6e 20 61 20 7a 65 72  base) then a zer
1a964 6f 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 72  o is stored in r
1a965 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a 20 49  egister P2..** I
1a966 66 20 41 55 54 4f 56 41 43 55 55 4d 20 69 73 20  f AUTOVACUUM is 
1a967 64 69 73 61 62 6c 65 64 20 74 68 65 6e 20 61 20  disabled then a 
1a968 7a 65 72 6f 20 69 73 20 73 74 6f 72 65 64 20 69  zero is stored i
1a969 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a  n register P2..*
1a96a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a 20 43  *.** See also: C
1a96b 6c 65 61 72 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  lear.*/.case OP_
1a96c 44 65 73 74 72 6f 79 3a 20 7b 20 20 20 20 20 2f  Destroy: {     /
1a96d 2a 20 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73  * out2-prereleas
1a96e 65 20 2a 2f 0a 20 20 69 6e 74 20 69 4d 6f 76 65  e */.  int iMove
1a96f 64 3b 0a 20 20 69 6e 74 20 69 43 6e 74 3b 0a 23  d;.  int iCnt;.#
1a970 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1a971 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1a972 20 20 56 64 62 65 20 2a 70 56 64 62 65 3b 0a 20    Vdbe *pVdbe;. 
1a973 20 69 43 6e 74 20 3d 20 30 3b 0a 20 20 66 6f 72   iCnt = 0;.  for
1a974 28 70 56 64 62 65 3d 64 62 2d 3e 70 56 64 62 65  (pVdbe=db->pVdbe
1a975 3b 20 70 56 64 62 65 3b 20 70 56 64 62 65 3d 70  ; pVdbe; pVdbe=p
1a976 56 64 62 65 2d 3e 70 4e 65 78 74 29 7b 0a 20 20  Vdbe->pNext){.  
1a977 20 20 69 66 28 20 70 56 64 62 65 2d 3e 6d 61 67    if( pVdbe->mag
1a978 69 63 3d 3d 56 44 42 45 5f 4d 41 47 49 43 5f 52  ic==VDBE_MAGIC_R
1a979 55 4e 20 26 26 20 70 56 64 62 65 2d 3e 69 6e 56  UN && pVdbe->inV
1a97a 74 61 62 4d 65 74 68 6f 64 3c 32 20 26 26 20 70  tabMethod<2 && p
1a97b 56 64 62 65 2d 3e 70 63 3e 3d 30 20 29 7b 0a 20  Vdbe->pc>=0 ){. 
1a97c 20 20 20 20 20 69 43 6e 74 2b 2b 3b 0a 20 20 20       iCnt++;.   
1a97d 20 7d 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 69   }.  }.#else.  i
1a97e 43 6e 74 20 3d 20 64 62 2d 3e 61 63 74 69 76 65  Cnt = db->active
1a97f 56 64 62 65 43 6e 74 3b 0a 23 65 6e 64 69 66 0a  VdbeCnt;.#endif.
1a980 20 20 69 66 28 20 69 43 6e 74 3e 31 20 29 7b 0a    if( iCnt>1 ){.
1a981 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1a982 4c 4f 43 4b 45 44 3b 0a 20 20 20 20 70 2d 3e 65  LOCKED;.    p->e
1a983 72 72 6f 72 41 63 74 69 6f 6e 20 3d 20 4f 45 5f  rrorAction = OE_
1a984 41 62 6f 72 74 3b 0a 20 20 7d 65 6c 73 65 7b 0a  Abort;.  }else{.
1a985 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 70 4f      int iDb = pO
1a986 70 2d 3e 70 33 3b 0a 20 20 20 20 61 73 73 65 72  p->p3;.    asser
1a987 74 28 20 69 43 6e 74 3d 3d 31 20 29 3b 0a 20 20  t( iCnt==1 );.  
1a988 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1a989 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 69 44  reeMask & (1<<iD
1a98a 62 29 29 21 3d 30 20 29 3b 0a 20 20 20 20 72 63  b))!=0 );.    rc
1a98b 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 44   = sqlite3BtreeD
1a98c 72 6f 70 54 61 62 6c 65 28 64 62 2d 3e 61 44 62  ropTable(db->aDb
1a98d 5b 69 44 62 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e  [iDb].pBt, pOp->
1a98e 70 31 2c 20 26 69 4d 6f 76 65 64 29 3b 0a 20 20  p1, &iMoved);.  
1a98f 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1a990 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1a991 0a 20 20 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d  .    pOut->u.i =
1a992 20 69 4d 6f 76 65 64 3b 0a 23 69 66 6e 64 65 66   iMoved;.#ifndef
1a993 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1a994 4f 56 41 43 55 55 4d 0a 20 20 20 20 69 66 28 20  OVACUUM.    if( 
1a995 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
1a996 20 69 4d 6f 76 65 64 21 3d 30 20 29 7b 0a 20 20   iMoved!=0 ){.  
1a997 20 20 20 20 73 71 6c 69 74 65 33 52 6f 6f 74 50      sqlite3RootP
1a998 61 67 65 4d 6f 76 65 64 28 26 64 62 2d 3e 61 44  ageMoved(&db->aD
1a999 62 5b 69 44 62 5d 2c 20 69 4d 6f 76 65 64 2c 20  b[iDb], iMoved, 
1a99a 70 4f 70 2d 3e 70 31 29 3b 0a 20 20 20 20 7d 0a  pOp->p1);.    }.
1a99b 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 62 72 65  #endif.  }.  bre
1a99c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1a99d 3a 20 43 6c 65 61 72 20 50 31 20 50 32 20 50 33  : Clear P1 P2 P3
1a99e 0a 2a 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6c  .**.** Delete al
1a99f 6c 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68  l contents of th
1a9a0 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  e database table
1a9a1 20 6f 72 20 69 6e 64 65 78 20 77 68 6f 73 65 20   or index whose 
1a9a2 72 6f 6f 74 20 70 61 67 65 0a 2a 2a 20 69 6e 20  root page.** in 
1a9a3 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69 6c  the database fil
1a9a4 65 20 69 73 20 67 69 76 65 6e 20 62 79 20 50 31  e is given by P1
1a9a5 2e 20 20 42 75 74 2c 20 75 6e 6c 69 6b 65 20 44  .  But, unlike D
1a9a6 65 73 74 72 6f 79 2c 20 64 6f 20 6e 6f 74 0a 2a  estroy, do not.*
1a9a7 2a 20 72 65 6d 6f 76 65 20 74 68 65 20 74 61 62  * remove the tab
1a9a8 6c 65 20 6f 72 20 69 6e 64 65 78 20 66 72 6f 6d  le or index from
1a9a9 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
1a9aa 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 61  le..**.** The ta
1a9ab 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 20  ble being clear 
1a9ac 69 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20 64  is in the main d
1a9ad 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20  atabase file if 
1a9ae 50 32 3d 3d 30 2e 20 20 49 66 0a 2a 2a 20 50 32  P2==0.  If.** P2
1a9af 3d 3d 31 20 74 68 65 6e 20 74 68 65 20 74 61 62  ==1 then the tab
1a9b0 6c 65 20 74 6f 20 62 65 20 63 6c 65 61 72 20 69  le to be clear i
1a9b1 73 20 69 6e 20 74 68 65 20 61 75 78 69 6c 69 61  s in the auxilia
1a9b2 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c 65  ry database file
1a9b3 0a 2a 2a 20 74 68 61 74 20 69 73 20 75 73 65 64  .** that is used
1a9b4 20 74 6f 20 73 74 6f 72 65 20 74 61 62 6c 65 73   to store tables
1a9b5 20 63 72 65 61 74 65 20 75 73 69 6e 67 20 43 52   create using CR
1a9b6 45 41 54 45 20 54 45 4d 50 4f 52 41 52 59 20 54  EATE TEMPORARY T
1a9b7 41 42 4c 45 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  ABLE..**.** If t
1a9b8 68 65 20 50 33 20 76 61 6c 75 65 20 69 73 20 6e  he P3 value is n
1a9b9 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68  on-zero, then th
1a9ba 65 20 74 61 62 6c 65 20 72 65 66 65 72 65 64 20  e table refered 
1a9bb 74 6f 20 6d 75 73 74 20 62 65 20 61 6e 0a 2a 2a  to must be an.**
1a9bc 20 69 6e 74 6b 65 79 20 74 61 62 6c 65 20 28 61   intkey table (a
1a9bd 6e 20 53 51 4c 20 74 61 62 6c 65 2c 20 6e 6f 74  n SQL table, not
1a9be 20 61 6e 20 69 6e 64 65 78 29 2e 20 49 6e 20 74   an index). In t
1a9bf 68 69 73 20 63 61 73 65 20 74 68 65 20 72 6f 77  his case the row
1a9c0 20 63 68 61 6e 67 65 20 0a 2a 2a 20 63 6f 75 6e   change .** coun
1a9c1 74 20 69 73 20 69 6e 63 72 65 6d 65 6e 74 65 64  t is incremented
1a9c2 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
1a9c3 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61  f rows in the ta
1a9c4 62 6c 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65  ble being cleare
1a9c5 64 2e 20 0a 2a 2a 20 49 66 20 50 33 20 69 73 20  d. .** If P3 is 
1a9c6 67 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72  greater than zer
1a9c7 6f 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  o, then the valu
1a9c8 65 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 69  e stored in regi
1a9c9 73 74 65 72 20 50 33 20 69 73 0a 2a 2a 20 61 6c  ster P3 is.** al
1a9ca 73 6f 20 69 6e 63 72 65 6d 65 6e 74 65 64 20 62  so incremented b
1a9cb 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  y the number of 
1a9cc 72 6f 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c  rows in the tabl
1a9cd 65 20 62 65 69 6e 67 20 63 6c 65 61 72 65 64 2e  e being cleared.
1a9ce 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61 6c 73 6f 3a  .**.** See also:
1a9cf 20 44 65 73 74 72 6f 79 0a 2a 2f 0a 63 61 73 65   Destroy.*/.case
1a9d0 20 4f 50 5f 43 6c 65 61 72 3a 20 7b 0a 20 20 69   OP_Clear: {.  i
1a9d1 6e 74 20 6e 43 68 61 6e 67 65 20 3d 20 30 3b 0a  nt nChange = 0;.
1a9d2 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62 74    assert( (p->bt
1a9d3 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70 4f  reeMask & (1<<pO
1a9d4 70 2d 3e 70 32 29 29 21 3d 30 20 29 3b 0a 20 20  p->p2))!=0 );.  
1a9d5 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65  rc = sqlite3Btre
1a9d6 65 43 6c 65 61 72 54 61 62 6c 65 28 0a 20 20 20  eClearTable(.   
1a9d7 20 20 20 64 62 2d 3e 61 44 62 5b 70 4f 70 2d 3e     db->aDb[pOp->
1a9d8 70 32 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 31  p2].pBt, pOp->p1
1a9d9 2c 20 28 70 4f 70 2d 3e 70 33 20 3f 20 26 6e 43  , (pOp->p3 ? &nC
1a9da 68 61 6e 67 65 20 3a 20 30 29 0a 20 20 29 3b 0a  hange : 0).  );.
1a9db 20 20 69 66 28 20 70 4f 70 2d 3e 70 33 20 29 7b    if( pOp->p3 ){
1a9dc 0a 20 20 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20  .    p->nChange 
1a9dd 2b 3d 20 6e 43 68 61 6e 67 65 3b 0a 20 20 20 20  += nChange;.    
1a9de 69 66 28 20 70 4f 70 2d 3e 70 33 3e 30 20 29 7b  if( pOp->p3>0 ){
1a9df 0a 20 20 20 20 20 20 70 2d 3e 61 4d 65 6d 5b 70  .      p->aMem[p
1a9e0 4f 70 2d 3e 70 33 5d 2e 75 2e 69 20 2b 3d 20 6e  Op->p3].u.i += n
1a9e1 43 68 61 6e 67 65 3b 0a 20 20 20 20 7d 0a 20 20  Change;.    }.  
1a9e2 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a  }.  break;.}../*
1a9e3 20 4f 70 63 6f 64 65 3a 20 43 72 65 61 74 65 54   Opcode: CreateT
1a9e4 61 62 6c 65 20 50 31 20 50 32 20 2a 20 2a 20 2a  able P1 P2 * * *
1a9e5 0a 2a 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65 20  .**.** Allocate 
1a9e6 61 20 6e 65 77 20 74 61 62 6c 65 20 69 6e 20 74  a new table in t
1a9e7 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
1a9e8 20 66 69 6c 65 20 69 66 20 50 31 3d 3d 30 20 6f   file if P1==0 o
1a9e9 72 20 69 6e 20 74 68 65 0a 2a 2a 20 61 75 78 69  r in the.** auxi
1a9ea 6c 69 61 72 79 20 64 61 74 61 62 61 73 65 20 66  liary database f
1a9eb 69 6c 65 20 69 66 20 50 31 3d 3d 31 20 6f 72 20  ile if P1==1 or 
1a9ec 69 6e 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  in an attached d
1a9ed 61 74 61 62 61 73 65 20 69 66 0a 2a 2a 20 50 31  atabase if.** P1
1a9ee 3e 31 2e 20 20 57 72 69 74 65 20 74 68 65 20 72  >1.  Write the r
1a9ef 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
1a9f0 6f 66 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65  of the new table
1a9f1 20 69 6e 74 6f 0a 2a 2a 20 72 65 67 69 73 74 65   into.** registe
1a9f2 72 20 50 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  r P2.**.** The d
1a9f3 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
1a9f4 6e 20 61 20 74 61 62 6c 65 20 61 6e 64 20 61 6e  n a table and an
1a9f5 20 69 6e 64 65 78 20 69 73 20 74 68 69 73 3a 20   index is this: 
1a9f6 20 41 20 74 61 62 6c 65 20 6d 75 73 74 0a 2a 2a   A table must.**
1a9f7 20 68 61 76 65 20 61 20 34 2d 62 79 74 65 20 69   have a 4-byte i
1a9f8 6e 74 65 67 65 72 20 6b 65 79 20 61 6e 64 20 63  nteger key and c
1a9f9 61 6e 20 68 61 76 65 20 61 72 62 69 74 72 61 72  an have arbitrar
1a9fa 79 20 64 61 74 61 2e 20 20 41 6e 20 69 6e 64 65  y data.  An inde
1a9fb 78 0a 2a 2a 20 68 61 73 20 61 6e 20 61 72 62 69  x.** has an arbi
1a9fc 74 72 61 72 79 20 6b 65 79 20 62 75 74 20 6e 6f  trary key but no
1a9fd 20 64 61 74 61 2e 0a 2a 2a 0a 2a 2a 20 53 65 65   data..**.** See
1a9fe 20 61 6c 73 6f 3a 20 43 72 65 61 74 65 49 6e 64   also: CreateInd
1a9ff 65 78 0a 2a 2f 0a 2f 2a 20 4f 70 63 6f 64 65 3a  ex.*/./* Opcode:
1aa00 20 43 72 65 61 74 65 49 6e 64 65 78 20 50 31 20   CreateIndex P1 
1aa01 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1aa02 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77 20 69 6e  llocate a new in
1aa03 64 65 78 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  dex in the main 
1aa04 64 61 74 61 62 61 73 65 20 66 69 6c 65 20 69 66  database file if
1aa05 20 50 31 3d 3d 30 20 6f 72 20 69 6e 20 74 68 65   P1==0 or in the
1aa06 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20 64 61  .** auxiliary da
1aa07 74 61 62 61 73 65 20 66 69 6c 65 20 69 66 20 50  tabase file if P
1aa08 31 3d 3d 31 20 6f 72 20 69 6e 20 61 6e 20 61 74  1==1 or in an at
1aa09 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 20  tached database 
1aa0a 69 66 0a 2a 2a 20 50 31 3e 31 2e 20 20 57 72 69  if.** P1>1.  Wri
1aa0b 74 65 20 74 68 65 20 72 6f 6f 74 20 70 61 67 65  te the root page
1aa0c 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 6e   number of the n
1aa0d 65 77 20 74 61 62 6c 65 20 69 6e 74 6f 0a 2a 2a  ew table into.**
1aa0e 20 72 65 67 69 73 74 65 72 20 50 32 2e 0a 2a 2a   register P2..**
1aa0f 0a 2a 2a 20 53 65 65 20 64 6f 63 75 6d 65 6e 74  .** See document
1aa10 61 74 69 6f 6e 20 6f 6e 20 4f 50 5f 43 72 65 61  ation on OP_Crea
1aa11 74 65 54 61 62 6c 65 20 66 6f 72 20 61 64 64 69  teTable for addi
1aa12 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69  tional informati
1aa13 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43  on..*/.case OP_C
1aa14 72 65 61 74 65 49 6e 64 65 78 3a 20 20 20 20 20  reateIndex:     
1aa15 20 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70         /* out2-p
1aa16 72 65 72 65 6c 65 61 73 65 20 2a 2f 0a 63 61 73  rerelease */.cas
1aa17 65 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  e OP_CreateTable
1aa18 3a 20 7b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  : {          /* 
1aa19 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1aa1a 2a 2f 0a 20 20 69 6e 74 20 70 67 6e 6f 3b 0a 20  */.  int pgno;. 
1aa1b 20 69 6e 74 20 66 6c 61 67 73 3b 0a 20 20 44 62   int flags;.  Db
1aa1c 20 2a 70 44 62 3b 0a 20 20 61 73 73 65 72 74 28   *pDb;.  assert(
1aa1d 20 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70   pOp->p1>=0 && p
1aa1e 4f 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29  Op->p1<db->nDb )
1aa1f 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e  ;.  assert( (p->
1aa20 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c  btreeMask & (1<<
1aa21 70 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a  pOp->p1))!=0 );.
1aa22 20 20 70 44 62 20 3d 20 26 64 62 2d 3e 61 44 62    pDb = &db->aDb
1aa23 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73  [pOp->p1];.  ass
1aa24 65 72 74 28 20 70 44 62 2d 3e 70 42 74 21 3d 30  ert( pDb->pBt!=0
1aa25 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 2d 3e 6f   );.  if( pOp->o
1aa26 70 63 6f 64 65 3d 3d 4f 50 5f 43 72 65 61 74 65  pcode==OP_Create
1aa27 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 2f 2a 20  Table ){.    /* 
1aa28 66 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 49 4e  flags = BTREE_IN
1aa29 54 4b 45 59 3b 20 2a 2f 0a 20 20 20 20 66 6c 61  TKEY; */.    fla
1aa2a 67 73 20 3d 20 42 54 52 45 45 5f 4c 45 41 46 44  gs = BTREE_LEAFD
1aa2b 41 54 41 7c 42 54 52 45 45 5f 49 4e 54 4b 45 59  ATA|BTREE_INTKEY
1aa2c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66  ;.  }else{.    f
1aa2d 6c 61 67 73 20 3d 20 42 54 52 45 45 5f 5a 45 52  lags = BTREE_ZER
1aa2e 4f 44 41 54 41 3b 0a 20 20 7d 0a 20 20 72 63 20  ODATA;.  }.  rc 
1aa2f 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 72  = sqlite3BtreeCr
1aa30 65 61 74 65 54 61 62 6c 65 28 70 44 62 2d 3e 70  eateTable(pDb->p
1aa31 42 74 2c 20 26 70 67 6e 6f 2c 20 66 6c 61 67 73  Bt, &pgno, flags
1aa32 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1aa33 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f  ITE_OK ){.    pO
1aa34 75 74 2d 3e 75 2e 69 20 3d 20 70 67 6e 6f 3b 0a  ut->u.i = pgno;.
1aa35 20 20 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c      MemSetTypeFl
1aa36 61 67 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74  ag(pOut, MEM_Int
1aa37 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a  );.  }.  break;.
1aa38 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 50 61  }../* Opcode: Pa
1aa39 72 73 65 53 63 68 65 6d 61 20 50 31 20 50 32 20  rseSchema P1 P2 
1aa3a 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 61  * P4 *.**.** Rea
1aa3b 64 20 61 6e 64 20 70 61 72 73 65 20 61 6c 6c 20  d and parse all 
1aa3c 65 6e 74 72 69 65 73 20 66 72 6f 6d 20 74 68 65  entries from the
1aa3d 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74   SQLITE_MASTER t
1aa3e 61 62 6c 65 20 6f 66 20 64 61 74 61 62 61 73 65  able of database
1aa3f 20 50 31 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63   P1.** that matc
1aa40 68 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  h the WHERE clau
1aa41 73 65 20 50 34 2e 20 20 50 32 20 69 73 20 74 68  se P4.  P2 is th
1aa42 65 20 22 66 6f 72 63 65 22 20 66 6c 61 67 2e 20  e "force" flag. 
1aa43 20 20 41 6c 77 61 79 73 20 64 6f 0a 2a 2a 20 74    Always do.** t
1aa44 68 65 20 70 61 72 73 69 6e 67 20 69 66 20 50 32  he parsing if P2
1aa45 20 69 73 20 74 72 75 65 2e 20 20 49 66 20 50 32   is true.  If P2
1aa46 20 69 73 20 66 61 6c 73 65 2c 20 74 68 65 6e 20   is false, then 
1aa47 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
1aa48 61 0a 2a 2a 20 6e 6f 2d 6f 70 20 69 66 20 74 68  a.** no-op if th
1aa49 65 20 73 63 68 65 6d 61 20 69 73 20 6e 6f 74 20  e schema is not 
1aa4a 63 75 72 72 65 6e 74 6c 79 20 6c 6f 61 64 65 64  currently loaded
1aa4b 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
1aa4c 73 2c 20 69 66 20 50 32 0a 2a 2a 20 69 73 20 66  s, if P2.** is f
1aa4d 61 6c 73 65 2c 20 74 68 65 20 53 51 4c 49 54 45  alse, the SQLITE
1aa4e 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 69 73  _MASTER table is
1aa4f 20 6f 6e 6c 79 20 70 61 72 73 65 64 20 69 66 20   only parsed if 
1aa50 74 68 65 20 72 65 73 74 20 6f 66 20 74 68 65 0a  the rest of the.
1aa51 2a 2a 20 73 63 68 65 6d 61 20 69 73 20 61 6c 72  ** schema is alr
1aa52 65 61 64 79 20 6c 6f 61 64 65 64 20 69 6e 74 6f  eady loaded into
1aa53 20 74 68 65 20 73 79 6d 62 6f 6c 20 74 61 62 6c   the symbol tabl
1aa54 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6f 70  e..**.** This op
1aa55 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65  code invokes the
1aa56 20 70 61 72 73 65 72 20 74 6f 20 63 72 65 61 74   parser to creat
1aa57 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 20  e a new virtual 
1aa58 6d 61 63 68 69 6e 65 2c 0a 2a 2a 20 74 68 65 6e  machine,.** then
1aa59 20 72 75 6e 73 20 74 68 65 20 6e 65 77 20 76 69   runs the new vi
1aa5a 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 2e 20 20  rtual machine.  
1aa5b 49 74 20 69 73 20 74 68 75 73 20 61 20 72 65 2d  It is thus a re-
1aa5c 65 6e 74 72 61 6e 74 20 6f 70 63 6f 64 65 2e 0a  entrant opcode..
1aa5d 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61 72 73 65  */.case OP_Parse
1aa5e 53 63 68 65 6d 61 3a 20 7b 0a 20 20 63 68 61 72  Schema: {.  char
1aa5f 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 69 44   *zSql;.  int iD
1aa60 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 63  b = pOp->p1;.  c
1aa61 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 61 73 74  onst char *zMast
1aa62 65 72 3b 0a 20 20 49 6e 69 74 44 61 74 61 20 69  er;.  InitData i
1aa63 6e 69 74 44 61 74 61 3b 0a 0a 20 20 61 73 73 65  nitData;..  asse
1aa64 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
1aa65 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 69  b<db->nDb );.  i
1aa66 66 28 20 21 70 4f 70 2d 3e 70 32 20 26 26 20 21  f( !pOp->p2 && !
1aa67 44 62 48 61 73 50 72 6f 70 65 72 74 79 28 64 62  DbHasProperty(db
1aa68 2c 20 69 44 62 2c 20 44 42 5f 53 63 68 65 6d 61  , iDb, DB_Schema
1aa69 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20 62  Loaded) ){.    b
1aa6a 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 7a 4d 61 73  reak;.  }.  zMas
1aa6b 74 65 72 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  ter = SCHEMA_TAB
1aa6c 4c 45 28 69 44 62 29 3b 0a 20 20 69 6e 69 74 44  LE(iDb);.  initD
1aa6d 61 74 61 2e 64 62 20 3d 20 64 62 3b 0a 20 20 69  ata.db = db;.  i
1aa6e 6e 69 74 44 61 74 61 2e 69 44 62 20 3d 20 70 4f  nitData.iDb = pO
1aa6f 70 2d 3e 70 31 3b 0a 20 20 69 6e 69 74 44 61 74  p->p1;.  initDat
1aa70 61 2e 70 7a 45 72 72 4d 73 67 20 3d 20 26 70 2d  a.pzErrMsg = &p-
1aa71 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 7a 53 71 6c  >zErrMsg;.  zSql
1aa72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1aa73 66 28 64 62 2c 0a 20 20 20 20 20 22 53 45 4c 45  f(db,.     "SELE
1aa74 43 54 20 6e 61 6d 65 2c 20 72 6f 6f 74 70 61 67  CT name, rootpag
1aa75 65 2c 20 73 71 6c 20 46 52 4f 4d 20 27 25 71 27  e, sql FROM '%q'
1aa76 2e 25 73 20 57 48 45 52 45 20 25 73 22 2c 0a 20  .%s WHERE %s",. 
1aa77 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d      db->aDb[iDb]
1aa78 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65 72 2c  .zName, zMaster,
1aa79 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 69   pOp->p4.z);.  i
1aa7a 66 28 20 7a 53 71 6c 3d 3d 30 20 29 20 67 6f 74  f( zSql==0 ) got
1aa7b 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 28 76 6f 69  o no_mem;.  (voi
1aa7c 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
1aa7d 66 66 28 64 62 29 3b 0a 20 20 61 73 73 65 72 74  ff(db);.  assert
1aa7e 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d  ( db->init.busy=
1aa7f 3d 30 20 29 3b 0a 20 20 64 62 2d 3e 69 6e 69 74  =0 );.  db->init
1aa80 2e 62 75 73 79 20 3d 20 31 3b 0a 20 20 69 6e 69  .busy = 1;.  ini
1aa81 74 44 61 74 61 2e 72 63 20 3d 20 53 51 4c 49 54  tData.rc = SQLIT
1aa82 45 5f 4f 4b 3b 0a 20 20 61 73 73 65 72 74 28 20  E_OK;.  assert( 
1aa83 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
1aa84 64 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  d );.  rc = sqli
1aa85 74 65 33 5f 65 78 65 63 28 64 62 2c 20 7a 53 71  te3_exec(db, zSq
1aa86 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61  l, sqlite3InitCa
1aa87 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74 44 61 74  llback, &initDat
1aa88 61 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63 3d  a, 0);.  if( rc=
1aa89 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20  =SQLITE_OK ) rc 
1aa8a 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20  = initData.rc;. 
1aa8b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1aa8c 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 64 62 2d 3e  b, zSql);.  db->
1aa8d 69 6e 69 74 2e 62 75 73 79 20 3d 20 30 3b 0a 20  init.busy = 0;. 
1aa8e 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61   (void)sqlite3Sa
1aa8f 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 69 66  fetyOn(db);.  if
1aa90 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d  ( rc==SQLITE_NOM
1aa91 45 4d 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6e  EM ){.    goto n
1aa92 6f 5f 6d 65 6d 3b 0a 20 20 7d 0a 20 20 62 72 65  o_mem;.  }.  bre
1aa93 61 6b 3b 20 20 0a 7d 0a 0a 23 69 66 20 21 64 65  ak;  .}..#if !de
1aa94 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1aa95 54 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64  T_ANALYZE) && !d
1aa96 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1aa97 49 54 5f 50 41 52 53 45 52 29 0a 2f 2a 20 4f 70  IT_PARSER)./* Op
1aa98 63 6f 64 65 3a 20 4c 6f 61 64 41 6e 61 6c 79 73  code: LoadAnalys
1aa99 69 73 20 50 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a  is P1 * * * *.**
1aa9a 0a 2a 2a 20 52 65 61 64 20 74 68 65 20 73 71 6c  .** Read the sql
1aa9b 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20  ite_stat1 table 
1aa9c 66 6f 72 20 64 61 74 61 62 61 73 65 20 50 31 20  for database P1 
1aa9d 61 6e 64 20 6c 6f 61 64 20 74 68 65 20 63 6f 6e  and load the con
1aa9e 74 65 6e 74 0a 2a 2a 20 6f 66 20 74 68 61 74 20  tent.** of that 
1aa9f 74 61 62 6c 65 20 69 6e 74 6f 20 74 68 65 20 69  table into the i
1aaa0 6e 74 65 72 6e 61 6c 20 69 6e 64 65 78 20 68 61  nternal index ha
1aaa1 73 68 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  sh table.  This 
1aaa2 77 69 6c 6c 20 63 61 75 73 65 0a 2a 2a 20 74 68  will cause.** th
1aaa3 65 20 61 6e 61 6c 79 73 69 73 20 74 6f 20 62 65  e analysis to be
1aaa4 20 75 73 65 64 20 77 68 65 6e 20 70 72 65 70 61   used when prepa
1aaa5 72 69 6e 67 20 61 6c 6c 20 73 75 62 73 65 71 75  ring all subsequ
1aaa6 65 6e 74 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a  ent queries..*/.
1aaa7 63 61 73 65 20 4f 50 5f 4c 6f 61 64 41 6e 61 6c  case OP_LoadAnal
1aaa8 79 73 69 73 3a 20 7b 0a 20 20 69 6e 74 20 69 44  ysis: {.  int iD
1aaa9 62 20 3d 20 70 4f 70 2d 3e 70 31 3b 0a 20 20 61  b = pOp->p1;.  a
1aaaa 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26  ssert( iDb>=0 &&
1aaab 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a   iDb<db->nDb );.
1aaac 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 41 6e    rc = sqlite3An
1aaad 61 6c 79 73 69 73 4c 6f 61 64 28 64 62 2c 20 69  alysisLoad(db, i
1aaae 44 62 29 3b 0a 20 20 62 72 65 61 6b 3b 20 20 0a  Db);.  break;  .
1aaaf 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  }.#endif /* !def
1aab0 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1aab1 5f 41 4e 41 4c 59 5a 45 29 20 26 26 20 21 64 65  _ANALYZE) && !de
1aab2 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1aab3 54 5f 50 41 52 53 45 52 29 20 20 2a 2f 0a 0a 2f  T_PARSER)  */../
1aab4 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 54 61  * Opcode: DropTa
1aab5 62 6c 65 20 50 31 20 2a 20 2a 20 50 34 20 2a 0a  ble P1 * * P4 *.
1aab6 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68 65  **.** Remove the
1aab7 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d 65   internal (in-me
1aab8 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75 63  mory) data struc
1aab9 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63 72  tures that descr
1aaba 69 62 65 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  ibe.** the table
1aabb 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61 74   named P4 in dat
1aabc 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73 20  abase P1.  This 
1aabd 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72 20  is called after 
1aabe 61 20 74 61 62 6c 65 0a 2a 2a 20 69 73 20 64 72  a table.** is dr
1aabf 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72 20 74  opped in order t
1aac0 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74 65 72  o keep the inter
1aac1 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74 69  nal representati
1aac2 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73 63 68  on of the.** sch
1aac3 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74 20 77  ema consistent w
1aac4 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e 20 64  ith what is on d
1aac5 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f  isk..*/.case OP_
1aac6 44 72 6f 70 54 61 62 6c 65 3a 20 7b 0a 20 20 73  DropTable: {.  s
1aac7 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44  qlite3UnlinkAndD
1aac8 65 6c 65 74 65 54 61 62 6c 65 28 64 62 2c 20 70  eleteTable(db, p
1aac9 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e  Op->p1, pOp->p4.
1aaca 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a  z);.  break;.}..
1aacb 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f 70 49  /* Opcode: DropI
1aacc 6e 64 65 78 20 50 31 20 2a 20 2a 20 50 34 20 2a  ndex P1 * * P4 *
1aacd 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  .**.** Remove th
1aace 65 20 69 6e 74 65 72 6e 61 6c 20 28 69 6e 2d 6d  e internal (in-m
1aacf 65 6d 6f 72 79 29 20 64 61 74 61 20 73 74 72 75  emory) data stru
1aad0 63 74 75 72 65 73 20 74 68 61 74 20 64 65 73 63  ctures that desc
1aad1 72 69 62 65 0a 2a 2a 20 74 68 65 20 69 6e 64 65  ribe.** the inde
1aad2 78 20 6e 61 6d 65 64 20 50 34 20 69 6e 20 64 61  x named P4 in da
1aad3 74 61 62 61 73 65 20 50 31 2e 20 20 54 68 69 73  tabase P1.  This
1aad4 20 69 73 20 63 61 6c 6c 65 64 20 61 66 74 65 72   is called after
1aad5 20 61 6e 20 69 6e 64 65 78 0a 2a 2a 20 69 73 20   an index.** is 
1aad6 64 72 6f 70 70 65 64 20 69 6e 20 6f 72 64 65 72  dropped in order
1aad7 20 74 6f 20 6b 65 65 70 20 74 68 65 20 69 6e 74   to keep the int
1aad8 65 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61  ernal representa
1aad9 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 73  tion of the.** s
1aada 63 68 65 6d 61 20 63 6f 6e 73 69 73 74 65 6e 74  chema consistent
1aadb 20 77 69 74 68 20 77 68 61 74 20 69 73 20 6f 6e   with what is on
1aadc 20 64 69 73 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f   disk..*/.case O
1aadd 50 5f 44 72 6f 70 49 6e 64 65 78 3a 20 7b 0a 20  P_DropIndex: {. 
1aade 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41 6e   sqlite3UnlinkAn
1aadf 64 44 65 6c 65 74 65 49 6e 64 65 78 28 64 62 2c  dDeleteIndex(db,
1aae0 20 70 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70   pOp->p1, pOp->p
1aae1 34 2e 7a 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d  4.z);.  break;.}
1aae2 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 44 72 6f  ../* Opcode: Dro
1aae3 70 54 72 69 67 67 65 72 20 50 31 20 2a 20 2a 20  pTrigger P1 * * 
1aae4 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 52 65 6d 6f 76  P4 *.**.** Remov
1aae5 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 28  e the internal (
1aae6 69 6e 2d 6d 65 6d 6f 72 79 29 20 64 61 74 61 20  in-memory) data 
1aae7 73 74 72 75 63 74 75 72 65 73 20 74 68 61 74 20  structures that 
1aae8 64 65 73 63 72 69 62 65 0a 2a 2a 20 74 68 65 20  describe.** the 
1aae9 74 72 69 67 67 65 72 20 6e 61 6d 65 64 20 50 34  trigger named P4
1aaea 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 2e   in database P1.
1aaeb 20 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64    This is called
1aaec 20 61 66 74 65 72 20 61 20 74 72 69 67 67 65 72   after a trigger
1aaed 0a 2a 2a 20 69 73 20 64 72 6f 70 70 65 64 20 69  .** is dropped i
1aaee 6e 20 6f 72 64 65 72 20 74 6f 20 6b 65 65 70 20  n order to keep 
1aaef 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 72 65 70  the internal rep
1aaf0 72 65 73 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  resentation of t
1aaf1 68 65 0a 2a 2a 20 73 63 68 65 6d 61 20 63 6f 6e  he.** schema con
1aaf2 73 69 73 74 65 6e 74 20 77 69 74 68 20 77 68 61  sistent with wha
1aaf3 74 20 69 73 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2f  t is on disk..*/
1aaf4 0a 63 61 73 65 20 4f 50 5f 44 72 6f 70 54 72 69  .case OP_DropTri
1aaf5 67 67 65 72 3a 20 7b 0a 20 20 73 71 6c 69 74 65  gger: {.  sqlite
1aaf6 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74 65  3UnlinkAndDelete
1aaf7 54 72 69 67 67 65 72 28 64 62 2c 20 70 4f 70 2d  Trigger(db, pOp-
1aaf8 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b  >p1, pOp->p4.z);
1aaf9 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69  .  break;.}...#i
1aafa 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1aafb 54 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  T_INTEGRITY_CHEC
1aafc 4b 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 49 6e 74  K./* Opcode: Int
1aafd 65 67 72 69 74 79 43 6b 20 50 31 20 50 32 20 50  egrityCk P1 P2 P
1aafe 33 20 2a 20 50 35 0a 2a 2a 0a 2a 2a 20 44 6f 20  3 * P5.**.** Do 
1aaff 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 74  an analysis of t
1ab00 68 65 20 63 75 72 72 65 6e 74 6c 79 20 6f 70 65  he currently ope
1ab01 6e 20 64 61 74 61 62 61 73 65 2e 20 20 53 74 6f  n database.  Sto
1ab02 72 65 20 69 6e 0a 2a 2a 20 72 65 67 69 73 74 65  re in.** registe
1ab03 72 20 50 31 20 74 68 65 20 74 65 78 74 20 6f 66  r P1 the text of
1ab04 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1ab05 65 20 64 65 73 63 72 69 62 69 6e 67 20 61 6e 79  e describing any
1ab06 20 70 72 6f 62 6c 65 6d 73 2e 0a 2a 2a 20 49 66   problems..** If
1ab07 20 6e 6f 20 70 72 6f 62 6c 65 6d 73 20 61 72 65   no problems are
1ab08 20 66 6f 75 6e 64 2c 20 73 74 6f 72 65 20 61 20   found, store a 
1ab09 4e 55 4c 4c 20 69 6e 20 72 65 67 69 73 74 65 72  NULL in register
1ab0a 20 50 31 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72   P1..**.** The r
1ab0b 65 67 69 73 74 65 72 20 50 33 20 63 6f 6e 74 61  egister P3 conta
1ab0c 69 6e 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  ins the maximum 
1ab0d 6e 75 6d 62 65 72 20 6f 66 20 61 6c 6c 6f 77 65  number of allowe
1ab0e 64 20 65 72 72 6f 72 73 2e 0a 2a 2a 20 41 74 20  d errors..** At 
1ab0f 6d 6f 73 74 20 72 65 67 28 50 33 29 20 65 72 72  most reg(P3) err
1ab10 6f 72 73 20 77 69 6c 6c 20 62 65 20 72 65 70 6f  ors will be repo
1ab11 72 74 65 64 2e 0a 2a 2a 20 49 6e 20 6f 74 68 65  rted..** In othe
1ab12 72 20 77 6f 72 64 73 2c 20 74 68 65 20 61 6e 61  r words, the ana
1ab13 6c 79 73 69 73 20 73 74 6f 70 73 20 61 73 20 73  lysis stops as s
1ab14 6f 6f 6e 20 61 73 20 72 65 67 28 50 31 29 20 65  oon as reg(P1) e
1ab15 72 72 6f 72 73 20 61 72 65 20 0a 2a 2a 20 73 65  rrors are .** se
1ab16 65 6e 2e 20 20 52 65 67 28 50 31 29 20 69 73 20  en.  Reg(P1) is 
1ab17 75 70 64 61 74 65 64 20 77 69 74 68 20 74 68 65  updated with the
1ab18 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72 6f 72   number of error
1ab19 73 20 72 65 6d 61 69 6e 69 6e 67 2e 0a 2a 2a 0a  s remaining..**.
1ab1a 2a 2a 20 54 68 65 20 72 6f 6f 74 20 70 61 67 65  ** The root page
1ab1b 20 6e 75 6d 62 65 72 73 20 6f 66 20 61 6c 6c 20   numbers of all 
1ab1c 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 64 61  tables in the da
1ab1d 74 61 62 61 73 65 20 61 72 65 20 69 6e 74 65 67  tabase are integ
1ab1e 65 72 0a 2a 2a 20 73 74 6f 72 65 64 20 69 6e 20  er.** stored in 
1ab1f 72 65 67 28 50 31 29 2c 20 72 65 67 28 50 31 2b  reg(P1), reg(P1+
1ab20 31 29 2c 20 72 65 67 28 50 31 2b 32 29 2c 20 2e  1), reg(P1+2), .
1ab21 2e 2e 2e 20 20 54 68 65 72 65 20 61 72 65 20 50  ...  There are P
1ab22 32 20 74 61 62 6c 65 73 0a 2a 2a 20 74 6f 74 61  2 tables.** tota
1ab23 6c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 35 20 69  l..**.** If P5 i
1ab24 73 20 6e 6f 74 20 7a 65 72 6f 2c 20 74 68 65 20  s not zero, the 
1ab25 63 68 65 63 6b 20 69 73 20 64 6f 6e 65 20 6f 6e  check is done on
1ab26 20 74 68 65 20 61 75 78 69 6c 69 61 72 79 20 64   the auxiliary d
1ab27 61 74 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 2c  atabase.** file,
1ab28 20 6e 6f 74 20 74 68 65 20 6d 61 69 6e 20 64 61   not the main da
1ab29 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 2a 2a 0a  tabase file..**.
1ab2a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 69  ** This opcode i
1ab2b 73 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  s used to implem
1ab2c 65 6e 74 20 74 68 65 20 69 6e 74 65 67 72 69 74  ent the integrit
1ab2d 79 5f 63 68 65 63 6b 20 70 72 61 67 6d 61 2e 0a  y_check pragma..
1ab2e 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 6e 74 65 67  */.case OP_Integ
1ab2f 72 69 74 79 43 6b 3a 20 7b 0a 20 20 69 6e 74 20  rityCk: {.  int 
1ab30 6e 52 6f 6f 74 3b 20 20 20 20 20 20 2f 2a 20 4e  nRoot;      /* N
1ab31 75 6d 62 65 72 20 6f 66 20 74 61 62 6c 65 73 20  umber of tables 
1ab32 74 6f 20 63 68 65 63 6b 2e 20 20 28 4e 75 6d 62  to check.  (Numb
1ab33 65 72 20 6f 66 20 72 6f 6f 74 20 70 61 67 65 73  er of root pages
1ab34 2e 29 20 2a 2f 0a 20 20 69 6e 74 20 2a 61 52 6f  .) */.  int *aRo
1ab35 6f 74 3b 20 20 20 20 20 2f 2a 20 41 72 72 61 79  ot;     /* Array
1ab36 20 6f 66 20 72 6f 6f 74 70 61 67 65 20 6e 75 6d   of rootpage num
1ab37 62 65 72 73 20 66 6f 72 20 74 61 62 6c 65 73 20  bers for tables 
1ab38 74 6f 20 62 65 20 63 68 65 63 6b 65 64 20 2a 2f  to be checked */
1ab39 0a 20 20 69 6e 74 20 6a 3b 20 20 20 20 20 20 20  .  int j;       
1ab3a 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1ab3b 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72  er */.  int nErr
1ab3c 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  ;       /* Numbe
1ab3d 72 20 6f 66 20 65 72 72 6f 72 73 20 72 65 70 6f  r of errors repo
1ab3e 72 74 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  rted */.  char *
1ab3f 7a 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 65 78  z;        /* Tex
1ab40 74 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20 72  t of the error r
1ab41 65 70 6f 72 74 20 2a 2f 0a 20 20 4d 65 6d 20 2a  eport */.  Mem *
1ab42 70 6e 45 72 72 3b 20 20 20 20 20 2f 2a 20 52 65  pnErr;     /* Re
1ab43 67 69 73 74 65 72 20 6b 65 65 70 69 6e 67 20 74  gister keeping t
1ab44 72 61 63 6b 20 6f 66 20 65 72 72 6f 72 73 20 72  rack of errors r
1ab45 65 6d 61 69 6e 69 6e 67 20 2a 2f 0a 20 20 0a 20  emaining */.  . 
1ab46 20 6e 52 6f 6f 74 20 3d 20 70 4f 70 2d 3e 70 32   nRoot = pOp->p2
1ab47 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 52 6f 6f  ;.  assert( nRoo
1ab48 74 3e 30 20 29 3b 0a 20 20 61 52 6f 6f 74 20 3d  t>0 );.  aRoot =
1ab49 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
1ab4a 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 69  Raw(db, sizeof(i
1ab4b 6e 74 29 2a 28 6e 52 6f 6f 74 2b 31 29 20 29 3b  nt)*(nRoot+1) );
1ab4c 0a 20 20 69 66 28 20 61 52 6f 6f 74 3d 3d 30 20  .  if( aRoot==0 
1ab4d 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20  ) goto no_mem;. 
1ab4e 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33   assert( pOp->p3
1ab4f 3e 30 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70  >0 && pOp->p3<=p
1ab50 2d 3e 6e 4d 65 6d 20 29 3b 0a 20 20 70 6e 45 72  ->nMem );.  pnEr
1ab51 72 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70  r = &p->aMem[pOp
1ab52 2d 3e 70 33 5d 3b 0a 20 20 61 73 73 65 72 74 28  ->p3];.  assert(
1ab53 20 28 70 6e 45 72 72 2d 3e 66 6c 61 67 73 20 26   (pnErr->flags &
1ab54 20 4d 45 4d 5f 49 6e 74 29 21 3d 30 20 29 3b 0a   MEM_Int)!=0 );.
1ab55 20 20 61 73 73 65 72 74 28 20 28 70 6e 45 72 72    assert( (pnErr
1ab56 2d 3e 66 6c 61 67 73 20 26 20 28 4d 45 4d 5f 53  ->flags & (MEM_S
1ab57 74 72 7c 4d 45 4d 5f 42 6c 6f 62 29 29 3d 3d 30  tr|MEM_Blob))==0
1ab58 20 29 3b 0a 20 20 70 49 6e 31 20 3d 20 26 70 2d   );.  pIn1 = &p-
1ab59 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  >aMem[pOp->p1];.
1ab5a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 6e 52 6f    for(j=0; j<nRo
1ab5b 6f 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 61 52  ot; j++){.    aR
1ab5c 6f 6f 74 5b 6a 5d 20 3d 20 73 71 6c 69 74 65 33  oot[j] = sqlite3
1ab5d 56 64 62 65 49 6e 74 56 61 6c 75 65 28 26 70 49  VdbeIntValue(&pI
1ab5e 6e 31 5b 6a 5d 29 3b 0a 20 20 7d 0a 20 20 61 52  n1[j]);.  }.  aR
1ab5f 6f 6f 74 5b 6a 5d 20 3d 20 30 3b 0a 20 20 61 73  oot[j] = 0;.  as
1ab60 73 65 72 74 28 20 70 4f 70 2d 3e 70 35 3c 64 62  sert( pOp->p5<db
1ab61 2d 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72  ->nDb );.  asser
1ab62 74 28 20 28 70 2d 3e 62 74 72 65 65 4d 61 73 6b  t( (p->btreeMask
1ab63 20 26 20 28 31 3c 3c 70 4f 70 2d 3e 70 35 29 29   & (1<<pOp->p5))
1ab64 21 3d 30 20 29 3b 0a 20 20 7a 20 3d 20 73 71 6c  !=0 );.  z = sql
1ab65 69 74 65 33 42 74 72 65 65 49 6e 74 65 67 72 69  ite3BtreeIntegri
1ab66 74 79 43 68 65 63 6b 28 64 62 2d 3e 61 44 62 5b  tyCheck(db->aDb[
1ab67 70 4f 70 2d 3e 70 35 5d 2e 70 42 74 2c 20 61 52  pOp->p5].pBt, aR
1ab68 6f 6f 74 2c 20 6e 52 6f 6f 74 2c 0a 20 20 20 20  oot, nRoot,.    
1ab69 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ab6a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 6e 45               pnE
1ab6b 72 72 2d 3e 75 2e 69 2c 20 26 6e 45 72 72 29 3b  rr->u.i, &nErr);
1ab6c 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1ab6d 28 64 62 2c 20 61 52 6f 6f 74 29 3b 0a 20 20 70  (db, aRoot);.  p
1ab6e 6e 45 72 72 2d 3e 75 2e 69 20 2d 3d 20 6e 45 72  nErr->u.i -= nEr
1ab6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  r;.  sqlite3Vdbe
1ab70 4d 65 6d 53 65 74 4e 75 6c 6c 28 70 49 6e 31 29  MemSetNull(pIn1)
1ab71 3b 0a 20 20 69 66 28 20 6e 45 72 72 3d 3d 30 20  ;.  if( nErr==0 
1ab72 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 7a  ){.    assert( z
1ab73 3d 3d 30 20 29 3b 0a 20 20 7d 65 6c 73 65 20 69  ==0 );.  }else i
1ab74 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 67  f( z==0 ){.    g
1ab75 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a 20 20 7d 65  oto no_mem;.  }e
1ab76 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  lse{.    sqlite3
1ab77 56 64 62 65 4d 65 6d 53 65 74 53 74 72 28 70 49  VdbeMemSetStr(pI
1ab78 6e 31 2c 20 7a 2c 20 2d 31 2c 20 53 51 4c 49 54  n1, z, -1, SQLIT
1ab79 45 5f 55 54 46 38 2c 20 73 71 6c 69 74 65 33 5f  E_UTF8, sqlite3_
1ab7a 66 72 65 65 29 3b 0a 20 20 7d 0a 20 20 55 50 44  free);.  }.  UPD
1ab7b 41 54 45 5f 4d 41 58 5f 42 4c 4f 42 53 49 5a 45  ATE_MAX_BLOBSIZE
1ab7c 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65  (pIn1);.  sqlite
1ab7d 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f 64  3VdbeChangeEncod
1ab7e 69 6e 67 28 70 49 6e 31 2c 20 65 6e 63 6f 64 69  ing(pIn1, encodi
1ab7f 6e 67 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  ng);.  break;.}.
1ab80 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ab81 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49 54 59 5f  _OMIT_INTEGRITY_
1ab82 43 48 45 43 4b 20 2a 2f 0a 0a 2f 2a 20 4f 70 63  CHECK */../* Opc
1ab83 6f 64 65 3a 20 46 69 66 6f 57 72 69 74 65 20 50  ode: FifoWrite P
1ab84 31 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20  1 * * * *.**.** 
1ab85 57 72 69 74 65 20 74 68 65 20 69 6e 74 65 67 65  Write the intege
1ab86 72 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72 20  r from register 
1ab87 50 31 20 69 6e 74 6f 20 74 68 65 20 46 69 66 6f  P1 into the Fifo
1ab88 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 46 69 66  ..*/.case OP_Fif
1ab89 6f 57 72 69 74 65 3a 20 7b 20 20 20 20 20 20 20  oWrite: {       
1ab8a 20 2f 2a 20 69 6e 31 20 2a 2f 0a 20 20 70 2d 3e   /* in1 */.  p->
1ab8b 73 46 69 66 6f 2e 64 62 20 3d 20 64 62 3b 0a 20  sFifo.db = db;. 
1ab8c 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1ab8d 46 69 66 6f 50 75 73 68 28 26 70 2d 3e 73 46 69  FifoPush(&p->sFi
1ab8e 66 6f 2c 20 73 71 6c 69 74 65 33 56 64 62 65 49  fo, sqlite3VdbeI
1ab8f 6e 74 56 61 6c 75 65 28 70 49 6e 31 29 29 3d 3d  ntValue(pIn1))==
1ab90 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a  SQLITE_NOMEM ){.
1ab91 20 20 20 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b      goto no_mem;
1ab92 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1ab93 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 46 69 66 6f  ./* Opcode: Fifo
1ab94 52 65 61 64 20 50 31 20 50 32 20 2a 20 2a 20 2a  Read P1 P2 * * *
1ab95 0a 2a 2a 0a 2a 2a 20 41 74 74 65 6d 70 74 20 74  .**.** Attempt t
1ab96 6f 20 72 65 61 64 20 61 20 73 69 6e 67 6c 65 20  o read a single 
1ab97 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 74 68 65  integer from the
1ab98 20 46 69 66 6f 2e 20 20 53 74 6f 72 65 20 74 68   Fifo.  Store th
1ab99 61 74 0a 2a 2a 20 69 6e 74 65 67 65 72 20 69 6e  at.** integer in
1ab9a 20 72 65 67 69 73 74 65 72 20 50 31 2e 0a 2a 2a   register P1..**
1ab9b 20 0a 2a 2a 20 49 66 20 74 68 65 20 46 69 66 6f   .** If the Fifo
1ab9c 20 69 73 20 65 6d 70 74 79 20 6a 75 6d 70 20 74   is empty jump t
1ab9d 6f 20 50 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  o P2..*/.case OP
1ab9e 5f 46 69 66 6f 52 65 61 64 3a 20 7b 20 20 20 20  _FifoRead: {    
1ab9f 20 20 20 20 20 2f 2a 20 6a 75 6d 70 20 2a 2f 0a       /* jump */.
1aba0 20 20 43 48 45 43 4b 5f 46 4f 52 5f 49 4e 54 45    CHECK_FOR_INTE
1aba1 52 52 55 50 54 3b 0a 20 20 61 73 73 65 72 74 28  RRUPT;.  assert(
1aba2 20 70 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f   pOp->p1>0 && pO
1aba3 70 2d 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29  p->p1<=p->nMem )
1aba4 3b 0a 20 20 70 4f 75 74 20 3d 20 26 70 2d 3e 61  ;.  pOut = &p->a
1aba5 4d 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Mem[pOp->p1];.  
1aba6 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67 28 70  MemSetTypeFlag(p
1aba7 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b 0a 20  Out, MEM_Int);. 
1aba8 20 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65   if( sqlite3Vdbe
1aba9 46 69 66 6f 50 6f 70 28 26 70 2d 3e 73 46 69 66  FifoPop(&p->sFif
1abaa 6f 2c 20 26 70 4f 75 74 2d 3e 75 2e 69 29 3d 3d  o, &pOut->u.i)==
1abab 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
1abac 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70 32 20     pc = pOp->p2 
1abad 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  - 1;.  }.  break
1abae 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  ;.}..#ifndef SQL
1abaf 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1abb0 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74  ./* Opcode: Cont
1abb1 65 78 74 50 75 73 68 20 2a 20 2a 20 2a 20 0a 2a  extPush * * * .*
1abb2 2a 0a 2a 2a 20 53 61 76 65 20 74 68 65 20 63 75  *.** Save the cu
1abb3 72 72 65 6e 74 20 56 64 62 65 20 63 6f 6e 74 65  rrent Vdbe conte
1abb4 78 74 20 73 75 63 68 20 74 68 61 74 20 69 74 20  xt such that it 
1abb5 63 61 6e 20 62 65 20 72 65 73 74 6f 72 65 64 20  can be restored 
1abb6 62 79 20 61 20 43 6f 6e 74 65 78 74 50 6f 70 0a  by a ContextPop.
1abb7 2a 2a 20 6f 70 63 6f 64 65 2e 20 54 68 65 20 63  ** opcode. The c
1abb8 6f 6e 74 65 78 74 20 73 74 6f 72 65 73 20 74 68  ontext stores th
1abb9 65 20 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f  e last insert ro
1abba 77 20 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73  w id, the last s
1abbb 74 61 74 65 6d 65 6e 74 20 63 68 61 6e 67 65 0a  tatement change.
1abbc 2a 2a 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68  ** count, and th
1abbd 65 20 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d  e current statem
1abbe 65 6e 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74  ent change count
1abbf 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e  ..*/.case OP_Con
1abc0 74 65 78 74 50 75 73 68 3a 20 7b 0a 20 20 69 6e  textPush: {.  in
1abc1 74 20 69 20 3d 20 70 2d 3e 63 6f 6e 74 65 78 74  t i = p->context
1abc2 53 74 61 63 6b 54 6f 70 2b 2b 3b 0a 20 20 43 6f  StackTop++;.  Co
1abc3 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 3b  ntext *pContext;
1abc4 0a 0a 20 20 61 73 73 65 72 74 28 20 69 3e 3d 30  ..  assert( i>=0
1abc5 20 29 3b 0a 20 20 2f 2a 20 46 49 58 20 4d 45 3a   );.  /* FIX ME:
1abc6 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
1abc7 61 6c 6c 6f 63 61 74 65 64 20 61 73 20 70 61 72  allocated as par
1abc8 74 20 6f 66 20 74 68 65 20 76 64 62 65 20 61 74  t of the vdbe at
1abc9 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65 20 2a 2f   compile-time */
1abca 0a 20 20 69 66 28 20 69 3e 3d 70 2d 3e 63 6f 6e  .  if( i>=p->con
1abcb 74 65 78 74 53 74 61 63 6b 44 65 70 74 68 20 29  textStackDepth )
1abcc 7b 0a 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74  {.    p->context
1abcd 53 74 61 63 6b 44 65 70 74 68 20 3d 20 69 2b 31  StackDepth = i+1
1abce 3b 0a 20 20 20 20 70 2d 3e 63 6f 6e 74 65 78 74  ;.    p->context
1abcf 53 74 61 63 6b 20 3d 20 73 71 6c 69 74 65 33 44  Stack = sqlite3D
1abd0 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 64  bReallocOrFree(d
1abd1 62 2c 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61  b, p->contextSta
1abd2 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ck,.            
1abd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1abd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 69                si
1abd5 7a 65 6f 66 28 43 6f 6e 74 65 78 74 29 2a 28 69  zeof(Context)*(i
1abd6 2b 31 29 29 3b 0a 20 20 20 20 69 66 28 20 70 2d  +1));.    if( p-
1abd7 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 3d 3d 30  >contextStack==0
1abd8 20 29 20 67 6f 74 6f 20 6e 6f 5f 6d 65 6d 3b 0a   ) goto no_mem;.
1abd9 20 20 7d 0a 20 20 70 43 6f 6e 74 65 78 74 20 3d    }.  pContext =
1abda 20 26 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63   &p->contextStac
1abdb 6b 5b 69 5d 3b 0a 20 20 70 43 6f 6e 74 65 78 74  k[i];.  pContext
1abdc 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20 64 62  ->lastRowid = db
1abdd 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a 20 20 70  ->lastRowid;.  p
1abde 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67 65  Context->nChange
1abdf 20 3d 20 70 2d 3e 6e 43 68 61 6e 67 65 3b 0a 20   = p->nChange;. 
1abe0 20 70 43 6f 6e 74 65 78 74 2d 3e 73 46 69 66 6f   pContext->sFifo
1abe1 20 3d 20 70 2d 3e 73 46 69 66 6f 3b 0a 20 20 73   = p->sFifo;.  s
1abe2 71 6c 69 74 65 33 56 64 62 65 46 69 66 6f 49 6e  qlite3VdbeFifoIn
1abe3 69 74 28 26 70 2d 3e 73 46 69 66 6f 2c 20 64 62  it(&p->sFifo, db
1abe4 29 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 0a 2f  );.  break;.}../
1abe5 2a 20 4f 70 63 6f 64 65 3a 20 43 6f 6e 74 65 78  * Opcode: Contex
1abe6 74 50 6f 70 20 2a 20 2a 20 2a 20 0a 2a 2a 0a 2a  tPop * * * .**.*
1abe7 2a 20 52 65 73 74 6f 72 65 20 74 68 65 20 56 64  * Restore the Vd
1abe8 62 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 74 68  be context to th
1abe9 65 20 73 74 61 74 65 20 69 74 20 77 61 73 20 69  e state it was i
1abea 6e 20 77 68 65 6e 20 63 6f 6e 74 65 78 74 50 75  n when contextPu
1abeb 73 68 20 77 61 73 20 6c 61 73 74 0a 2a 2a 20 65  sh was last.** e
1abec 78 65 63 75 74 65 64 2e 20 54 68 65 20 63 6f 6e  xecuted. The con
1abed 74 65 78 74 20 73 74 6f 72 65 73 20 74 68 65 20  text stores the 
1abee 6c 61 73 74 20 69 6e 73 65 72 74 20 72 6f 77 20  last insert row 
1abef 69 64 2c 20 74 68 65 20 6c 61 73 74 20 73 74 61  id, the last sta
1abf0 74 65 6d 65 6e 74 0a 2a 2a 20 63 68 61 6e 67 65  tement.** change
1abf1 20 63 6f 75 6e 74 2c 20 61 6e 64 20 74 68 65 20   count, and the 
1abf2 63 75 72 72 65 6e 74 20 73 74 61 74 65 6d 65 6e  current statemen
1abf3 74 20 63 68 61 6e 67 65 20 63 6f 75 6e 74 2e 0a  t change count..
1abf4 2a 2f 0a 63 61 73 65 20 4f 50 5f 43 6f 6e 74 65  */.case OP_Conte
1abf5 78 74 50 6f 70 3a 20 7b 0a 20 20 43 6f 6e 74 65  xtPop: {.  Conte
1abf6 78 74 20 2a 70 43 6f 6e 74 65 78 74 20 3d 20 26  xt *pContext = &
1abf7 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b 5b  p->contextStack[
1abf8 2d 2d 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63  --p->contextStac
1abf9 6b 54 6f 70 5d 3b 0a 20 20 61 73 73 65 72 74 28  kTop];.  assert(
1abfa 20 70 2d 3e 63 6f 6e 74 65 78 74 53 74 61 63 6b   p->contextStack
1abfb 54 6f 70 3e 3d 30 20 29 3b 0a 20 20 64 62 2d 3e  Top>=0 );.  db->
1abfc 6c 61 73 74 52 6f 77 69 64 20 3d 20 70 43 6f 6e  lastRowid = pCon
1abfd 74 65 78 74 2d 3e 6c 61 73 74 52 6f 77 69 64 3b  text->lastRowid;
1abfe 0a 20 20 70 2d 3e 6e 43 68 61 6e 67 65 20 3d 20  .  p->nChange = 
1abff 70 43 6f 6e 74 65 78 74 2d 3e 6e 43 68 61 6e 67  pContext->nChang
1ac00 65 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  e;.  sqlite3Vdbe
1ac01 46 69 66 6f 43 6c 65 61 72 28 26 70 2d 3e 73 46  FifoClear(&p->sF
1ac02 69 66 6f 29 3b 0a 20 20 70 2d 3e 73 46 69 66 6f  ifo);.  p->sFifo
1ac03 20 3d 20 70 43 6f 6e 74 65 78 74 2d 3e 73 46 69   = pContext->sFi
1ac04 66 6f 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23  fo;.  break;.}.#
1ac05 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65 66  endif /* #ifndef
1ac06 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
1ac07 47 47 45 52 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GGER */..#ifndef
1ac08 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1ac09 4f 49 4e 43 52 45 4d 45 4e 54 0a 2f 2a 20 4f 70  OINCREMENT./* Op
1ac0a 63 6f 64 65 3a 20 4d 65 6d 4d 61 78 20 50 31 20  code: MemMax P1 
1ac0b 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1ac0c 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  et the value of 
1ac0d 72 65 67 69 73 74 65 72 20 50 31 20 74 6f 20 74  register P1 to t
1ac0e 68 65 20 6d 61 78 69 6d 75 6d 20 6f 66 20 69 74  he maximum of it
1ac0f 73 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 0a  s current value.
1ac10 2a 2a 20 61 6e 64 20 74 68 65 20 76 61 6c 75 65  ** and the value
1ac11 20 69 6e 20 72 65 67 69 73 74 65 72 20 50 32 2e   in register P2.
1ac12 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 6e 73 74  .**.** This inst
1ac13 72 75 63 74 69 6f 6e 20 74 68 72 6f 77 73 20 61  ruction throws a
1ac14 6e 20 65 72 72 6f 72 20 69 66 20 74 68 65 20 6d  n error if the m
1ac15 65 6d 6f 72 79 20 63 65 6c 6c 20 69 73 20 6e 6f  emory cell is no
1ac16 74 20 69 6e 69 74 69 61 6c 6c 79 0a 2a 2a 20 61  t initially.** a
1ac17 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2f 0a 63 61  n integer..*/.ca
1ac18 73 65 20 4f 50 5f 4d 65 6d 4d 61 78 3a 20 7b 20  se OP_MemMax: { 
1ac19 20 20 20 20 20 20 20 2f 2a 20 69 6e 31 2c 20 69         /* in1, i
1ac1a 6e 32 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  n2 */.  sqlite3V
1ac1b 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69 66 79  dbeMemIntegerify
1ac1c 28 70 49 6e 31 29 3b 0a 20 20 73 71 6c 69 74 65  (pIn1);.  sqlite
1ac1d 33 56 64 62 65 4d 65 6d 49 6e 74 65 67 65 72 69  3VdbeMemIntegeri
1ac1e 66 79 28 70 49 6e 32 29 3b 0a 20 20 69 66 28 20  fy(pIn2);.  if( 
1ac1f 70 49 6e 31 2d 3e 75 2e 69 3c 70 49 6e 32 2d 3e  pIn1->u.i<pIn2->
1ac20 75 2e 69 29 7b 0a 20 20 20 20 70 49 6e 31 2d 3e  u.i){.    pIn1->
1ac21 75 2e 69 20 3d 20 70 49 6e 32 2d 3e 75 2e 69 3b  u.i = pIn2->u.i;
1ac22 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  .  }.  break;.}.
1ac23 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1ac24 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
1ac25 45 4e 54 20 2a 2f 0a 0a 2f 2a 20 4f 70 63 6f 64  ENT */../* Opcod
1ac26 65 3a 20 49 66 50 6f 73 20 50 31 20 50 32 20 2a  e: IfPos P1 P2 *
1ac27 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68   * *.**.** If th
1ac28 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73  e value of regis
1ac29 74 65 72 20 50 31 20 69 73 20 31 20 6f 72 20 67  ter P1 is 1 or g
1ac2a 72 65 61 74 65 72 2c 20 6a 75 6d 70 20 74 6f 20  reater, jump to 
1ac2b 50 32 2e 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  P2..**.** It is 
1ac2c 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1ac2d 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1ac2e 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1ac2f 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1ac30 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1ac31 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1ac32 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1ac33 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1ac34 2f 0a 63 61 73 65 20 4f 50 5f 49 66 50 6f 73 3a  /.case OP_IfPos:
1ac35 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75 6d   {        /* jum
1ac36 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73 65  p, in1 */.  asse
1ac37 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73 26  rt( pIn1->flags&
1ac38 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66 28  MEM_Int );.  if(
1ac39 20 70 49 6e 31 2d 3e 75 2e 69 3e 30 20 29 7b 0a   pIn1->u.i>0 ){.
1ac3a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e 70       pc = pOp->p
1ac3b 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62 72 65  2 - 1;.  }.  bre
1ac3c 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f 64 65  ak;.}../* Opcode
1ac3d 3a 20 49 66 4e 65 67 20 50 31 20 50 32 20 2a 20  : IfNeg P1 P2 * 
1ac3e 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  * *.**.** If the
1ac3f 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1ac40 65 72 20 50 31 20 69 73 20 6c 65 73 73 20 74 68  er P1 is less th
1ac41 61 6e 20 7a 65 72 6f 2c 20 6a 75 6d 70 20 74 6f  an zero, jump to
1ac42 20 50 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69   P2. .**.** It i
1ac43 73 20 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65  s illegal to use
1ac44 20 74 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f   this instructio
1ac45 6e 20 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20  n on a register 
1ac46 74 68 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74  that does.** not
1ac47 20 63 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65   contain an inte
1ac48 67 65 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69  ger.  An asserti
1ac49 6f 6e 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65  on fault will re
1ac4a 73 75 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e  sult if you try.
1ac4b 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 49 66 4e 65  .*/.case OP_IfNe
1ac4c 67 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  g: {        /* j
1ac4d 75 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73  ump, in1 */.  as
1ac4e 73 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67  sert( pIn1->flag
1ac4f 73 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69  s&MEM_Int );.  i
1ac50 66 28 20 70 49 6e 31 2d 3e 75 2e 69 3c 30 20 29  f( pIn1->u.i<0 )
1ac51 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1ac52 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1ac53 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ac54 64 65 3a 20 49 66 5a 65 72 6f 20 50 31 20 50 32  de: IfZero P1 P2
1ac55 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 49 66 20   * * *.**.** If 
1ac56 74 68 65 20 76 61 6c 75 65 20 6f 66 20 72 65 67  the value of reg
1ac57 69 73 74 65 72 20 50 31 20 69 73 20 65 78 61 63  ister P1 is exac
1ac58 74 6c 79 20 30 2c 20 6a 75 6d 70 20 74 6f 20 50  tly 0, jump to P
1ac59 32 2e 20 0a 2a 2a 0a 2a 2a 20 49 74 20 69 73 20  2. .**.** It is 
1ac5a 69 6c 6c 65 67 61 6c 20 74 6f 20 75 73 65 20 74  illegal to use t
1ac5b 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e 20  his instruction 
1ac5c 6f 6e 20 61 20 72 65 67 69 73 74 65 72 20 74 68  on a register th
1ac5d 61 74 20 64 6f 65 73 0a 2a 2a 20 6e 6f 74 20 63  at does.** not c
1ac5e 6f 6e 74 61 69 6e 20 61 6e 20 69 6e 74 65 67 65  ontain an intege
1ac5f 72 2e 20 20 41 6e 20 61 73 73 65 72 74 69 6f 6e  r.  An assertion
1ac60 20 66 61 75 6c 74 20 77 69 6c 6c 20 72 65 73 75   fault will resu
1ac61 6c 74 20 69 66 20 79 6f 75 20 74 72 79 2e 0a 2a  lt if you try..*
1ac62 2f 0a 63 61 73 65 20 4f 50 5f 49 66 5a 65 72 6f  /.case OP_IfZero
1ac63 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a 75  : {        /* ju
1ac64 6d 70 2c 20 69 6e 31 20 2a 2f 0a 20 20 61 73 73  mp, in1 */.  ass
1ac65 65 72 74 28 20 70 49 6e 31 2d 3e 66 6c 61 67 73  ert( pIn1->flags
1ac66 26 4d 45 4d 5f 49 6e 74 20 29 3b 0a 20 20 69 66  &MEM_Int );.  if
1ac67 28 20 70 49 6e 31 2d 3e 75 2e 69 3d 3d 30 20 29  ( pIn1->u.i==0 )
1ac68 7b 0a 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d  {.     pc = pOp-
1ac69 3e 70 32 20 2d 20 31 3b 0a 20 20 7d 0a 20 20 62  >p2 - 1;.  }.  b
1ac6a 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63 6f  reak;.}../* Opco
1ac6b 64 65 3a 20 41 67 67 53 74 65 70 20 2a 20 50 32  de: AggStep * P2
1ac6c 20 50 33 20 50 34 20 50 35 0a 2a 2a 0a 2a 2a 20   P3 P4 P5.**.** 
1ac6d 45 78 65 63 75 74 65 20 74 68 65 20 73 74 65 70  Execute the step
1ac6e 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 61 6e   function for an
1ac6f 20 61 67 67 72 65 67 61 74 65 2e 20 20 54 68 65   aggregate.  The
1ac70 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 68 61 73  .** function has
1ac71 20 50 35 20 61 72 67 75 6d 65 6e 74 73 2e 20 20   P5 arguments.  
1ac72 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1ac73 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65 66 0a   to the FuncDef.
1ac74 2a 2a 20 73 74 72 75 63 74 75 72 65 20 74 68 61  ** structure tha
1ac75 74 20 73 70 65 63 69 66 69 65 73 20 74 68 65 20  t specifies the 
1ac76 66 75 6e 63 74 69 6f 6e 2e 20 20 55 73 65 20 72  function.  Use r
1ac77 65 67 69 73 74 65 72 0a 2a 2a 20 50 33 20 61 73  egister.** P3 as
1ac78 20 74 68 65 20 61 63 63 75 6d 75 6c 61 74 6f 72   the accumulator
1ac79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 50 35 20 61  ..**.** The P5 a
1ac7a 72 67 75 6d 65 6e 74 73 20 61 72 65 20 74 61 6b  rguments are tak
1ac7b 65 6e 20 66 72 6f 6d 20 72 65 67 69 73 74 65 72  en from register
1ac7c 20 50 32 20 61 6e 64 20 69 74 73 0a 2a 2a 20 73   P2 and its.** s
1ac7d 75 63 63 65 73 73 6f 72 73 2e 0a 2a 2f 0a 63 61  uccessors..*/.ca
1ac7e 73 65 20 4f 50 5f 41 67 67 53 74 65 70 3a 20 7b  se OP_AggStep: {
1ac7f 0a 20 20 69 6e 74 20 6e 20 3d 20 70 4f 70 2d 3e  .  int n = pOp->
1ac80 70 35 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 4d  p5;.  int i;.  M
1ac81 65 6d 20 2a 70 4d 65 6d 2c 20 2a 70 52 65 63 3b  em *pMem, *pRec;
1ac82 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
1ac83 78 74 20 63 74 78 3b 0a 20 20 73 71 6c 69 74 65  xt ctx;.  sqlite
1ac84 33 5f 76 61 6c 75 65 20 2a 2a 61 70 56 61 6c 3b  3_value **apVal;
1ac85 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 3d 30  ..  assert( n>=0
1ac86 20 29 3b 0a 20 20 70 52 65 63 20 3d 20 26 70 2d   );.  pRec = &p-
1ac87 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 32 5d 3b 0a  >aMem[pOp->p2];.
1ac88 20 20 61 70 56 61 6c 20 3d 20 70 2d 3e 61 70 41    apVal = p->apA
1ac89 72 67 3b 0a 20 20 61 73 73 65 72 74 28 20 61 70  rg;.  assert( ap
1ac8a 56 61 6c 20 7c 7c 20 6e 3d 3d 30 20 29 3b 0a 20  Val || n==0 );. 
1ac8b 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
1ac8c 2b 2b 2c 20 70 52 65 63 2b 2b 29 7b 0a 20 20 20  ++, pRec++){.   
1ac8d 20 61 70 56 61 6c 5b 69 5d 20 3d 20 70 52 65 63   apVal[i] = pRec
1ac8e 3b 0a 20 20 20 20 73 74 6f 72 65 54 79 70 65 49  ;.    storeTypeI
1ac8f 6e 66 6f 28 70 52 65 63 2c 20 65 6e 63 6f 64 69  nfo(pRec, encodi
1ac90 6e 67 29 3b 0a 20 20 7d 0a 20 20 63 74 78 2e 70  ng);.  }.  ctx.p
1ac91 46 75 6e 63 20 3d 20 70 4f 70 2d 3e 70 34 2e 70  Func = pOp->p4.p
1ac92 46 75 6e 63 3b 0a 20 20 61 73 73 65 72 74 28 20  Func;.  assert( 
1ac93 70 4f 70 2d 3e 70 33 3e 30 20 26 26 20 70 4f 70  pOp->p3>0 && pOp
1ac94 2d 3e 70 33 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b  ->p3<=p->nMem );
1ac95 0a 20 20 63 74 78 2e 70 4d 65 6d 20 3d 20 70 4d  .  ctx.pMem = pM
1ac96 65 6d 20 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f  em = &p->aMem[pO
1ac97 70 2d 3e 70 33 5d 3b 0a 20 20 70 4d 65 6d 2d 3e  p->p3];.  pMem->
1ac98 6e 2b 2b 3b 0a 20 20 63 74 78 2e 73 2e 66 6c 61  n++;.  ctx.s.fla
1ac99 67 73 20 3d 20 4d 45 4d 5f 4e 75 6c 6c 3b 0a 20  gs = MEM_Null;. 
1ac9a 20 63 74 78 2e 73 2e 7a 20 3d 20 30 3b 0a 20 20   ctx.s.z = 0;.  
1ac9b 63 74 78 2e 73 2e 7a 4d 61 6c 6c 6f 63 20 3d 20  ctx.s.zMalloc = 
1ac9c 30 3b 0a 20 20 63 74 78 2e 73 2e 78 44 65 6c 20  0;.  ctx.s.xDel 
1ac9d 3d 20 30 3b 0a 20 20 63 74 78 2e 73 2e 64 62 20  = 0;.  ctx.s.db 
1ac9e 3d 20 64 62 3b 0a 20 20 63 74 78 2e 69 73 45 72  = db;.  ctx.isEr
1ac9f 72 6f 72 20 3d 20 30 3b 0a 20 20 63 74 78 2e 70  ror = 0;.  ctx.p
1aca0 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 69 66 28 20  Coll = 0;.  if( 
1aca1 63 74 78 2e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  ctx.pFunc->flags
1aca2 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
1aca3 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 61  EEDCOLL ){.    a
1aca4 73 73 65 72 74 28 20 70 4f 70 3e 70 2d 3e 61 4f  ssert( pOp>p->aO
1aca5 70 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  p );.    assert(
1aca6 20 70 4f 70 5b 2d 31 5d 2e 70 34 74 79 70 65 3d   pOp[-1].p4type=
1aca7 3d 50 34 5f 43 4f 4c 4c 53 45 51 20 29 3b 0a 20  =P4_COLLSEQ );. 
1aca8 20 20 20 61 73 73 65 72 74 28 20 70 4f 70 5b 2d     assert( pOp[-
1aca9 31 5d 2e 6f 70 63 6f 64 65 3d 3d 4f 50 5f 43 6f  1].opcode==OP_Co
1acaa 6c 6c 53 65 71 20 29 3b 0a 20 20 20 20 63 74 78  llSeq );.    ctx
1acab 2e 70 43 6f 6c 6c 20 3d 20 70 4f 70 5b 2d 31 5d  .pColl = pOp[-1]
1acac 2e 70 34 2e 70 43 6f 6c 6c 3b 0a 20 20 7d 0a 20  .p4.pColl;.  }. 
1acad 20 28 63 74 78 2e 70 46 75 6e 63 2d 3e 78 53 74   (ctx.pFunc->xSt
1acae 65 70 29 28 26 63 74 78 2c 20 6e 2c 20 61 70 56  ep)(&ctx, n, apV
1acaf 61 6c 29 3b 0a 20 20 69 66 28 20 63 74 78 2e 69  al);.  if( ctx.i
1acb0 73 45 72 72 6f 72 20 29 7b 0a 20 20 20 20 73 71  sError ){.    sq
1acb1 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1acb2 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1acb3 22 25 73 22 2c 20 73 71 6c 69 74 65 33 5f 76 61  "%s", sqlite3_va
1acb4 6c 75 65 5f 74 65 78 74 28 26 63 74 78 2e 73 29  lue_text(&ctx.s)
1acb5 29 3b 0a 20 20 20 20 72 63 20 3d 20 63 74 78 2e  );.    rc = ctx.
1acb6 69 73 45 72 72 6f 72 3b 0a 20 20 7d 0a 20 20 73  isError;.  }.  s
1acb7 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c  qlite3VdbeMemRel
1acb8 65 61 73 65 28 26 63 74 78 2e 73 29 3b 0a 20 20  ease(&ctx.s);.  
1acb9 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 20 4f 70 63  break;.}../* Opc
1acba 6f 64 65 3a 20 41 67 67 46 69 6e 61 6c 20 50 31  ode: AggFinal P1
1acbb 20 50 32 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a   P2 * P4 *.**.**
1acbc 20 45 78 65 63 75 74 65 20 74 68 65 20 66 69 6e   Execute the fin
1acbd 61 6c 69 7a 65 72 20 66 75 6e 63 74 69 6f 6e 20  alizer function 
1acbe 66 6f 72 20 61 6e 20 61 67 67 72 65 67 61 74 65  for an aggregate
1acbf 2e 20 20 50 31 20 69 73 0a 2a 2a 20 74 68 65 20  .  P1 is.** the 
1acc0 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20  memory location 
1acc1 74 68 61 74 20 69 73 20 74 68 65 20 61 63 63 75  that is the accu
1acc2 6d 75 6c 61 74 6f 72 20 66 6f 72 20 74 68 65 20  mulator for the 
1acc3 61 67 67 72 65 67 61 74 65 2e 0a 2a 2a 0a 2a 2a  aggregate..**.**
1acc4 20 50 32 20 69 73 20 74 68 65 20 6e 75 6d 62 65   P2 is the numbe
1acc5 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 20 74  r of arguments t
1acc6 68 61 74 20 74 68 65 20 73 74 65 70 20 66 75 6e  hat the step fun
1acc7 63 74 69 6f 6e 20 74 61 6b 65 73 20 61 6e 64 0a  ction takes and.
1acc8 2a 2a 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74  ** P4 is a point
1acc9 65 72 20 74 6f 20 74 68 65 20 46 75 6e 63 44 65  er to the FuncDe
1acca 66 20 66 6f 72 20 74 68 69 73 20 66 75 6e 63 74  f for this funct
1accb 69 6f 6e 2e 20 20 54 68 65 20 50 32 0a 2a 2a 20  ion.  The P2.** 
1accc 61 72 67 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20  argument is not 
1accd 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70 63  used by this opc
1acce 6f 64 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ode.  It is only
1accf 20 74 68 65 72 65 20 74 6f 20 64 69 73 61 6d 62   there to disamb
1acd0 69 67 75 61 74 65 0a 2a 2a 20 66 75 6e 63 74 69  iguate.** functi
1acd1 6f 6e 73 20 74 68 61 74 20 63 61 6e 20 74 61 6b  ons that can tak
1acd2 65 20 76 61 72 79 69 6e 67 20 6e 75 6d 62 65 72  e varying number
1acd3 73 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20  s of arguments. 
1acd4 20 54 68 65 0a 2a 2a 20 50 34 20 61 72 67 75 6d   The.** P4 argum
1acd5 65 6e 74 20 69 73 20 6f 6e 6c 79 20 6e 65 65 64  ent is only need
1acd6 65 64 20 66 6f 72 20 74 68 65 20 64 65 67 65 6e  ed for the degen
1acd7 65 72 61 74 65 20 63 61 73 65 20 77 68 65 72 65  erate case where
1acd8 0a 2a 2a 20 74 68 65 20 73 74 65 70 20 66 75 6e  .** the step fun
1acd9 63 74 69 6f 6e 20 77 61 73 20 6e 6f 74 20 70 72  ction was not pr
1acda 65 76 69 6f 75 73 6c 79 20 63 61 6c 6c 65 64 2e  eviously called.
1acdb 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 41 67 67 46  .*/.case OP_AggF
1acdc 69 6e 61 6c 3a 20 7b 0a 20 20 4d 65 6d 20 2a 70  inal: {.  Mem *p
1acdd 4d 65 6d 3b 0a 20 20 61 73 73 65 72 74 28 20 70  Mem;.  assert( p
1acde 4f 70 2d 3e 70 31 3e 30 20 26 26 20 70 4f 70 2d  Op->p1>0 && pOp-
1acdf 3e 70 31 3c 3d 70 2d 3e 6e 4d 65 6d 20 29 3b 0a  >p1<=p->nMem );.
1ace0 20 20 70 4d 65 6d 20 3d 20 26 70 2d 3e 61 4d 65    pMem = &p->aMe
1ace1 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73  m[pOp->p1];.  as
1ace2 73 65 72 74 28 20 28 70 4d 65 6d 2d 3e 66 6c 61  sert( (pMem->fla
1ace3 67 73 20 26 20 7e 28 4d 45 4d 5f 4e 75 6c 6c 7c  gs & ~(MEM_Null|
1ace4 4d 45 4d 5f 41 67 67 29 29 3d 3d 30 20 29 3b 0a  MEM_Agg))==0 );.
1ace5 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 56 64    rc = sqlite3Vd
1ace6 62 65 4d 65 6d 46 69 6e 61 6c 69 7a 65 28 70 4d  beMemFinalize(pM
1ace7 65 6d 2c 20 70 4f 70 2d 3e 70 34 2e 70 46 75 6e  em, pOp->p4.pFun
1ace8 63 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  c);.  if( rc==SQ
1ace9 4c 49 54 45 5f 45 52 52 4f 52 20 29 7b 0a 20 20  LITE_ERROR ){.  
1acea 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
1aceb 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20  ng(&p->zErrMsg, 
1acec 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65  db, "%s", sqlite
1aced 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 70 4d 65  3_value_text(pMe
1acee 6d 29 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  m));.  }.  sqlit
1acef 65 33 56 64 62 65 43 68 61 6e 67 65 45 6e 63 6f  e3VdbeChangeEnco
1acf0 64 69 6e 67 28 70 4d 65 6d 2c 20 65 6e 63 6f 64  ding(pMem, encod
1acf1 69 6e 67 29 3b 0a 20 20 55 50 44 41 54 45 5f 4d  ing);.  UPDATE_M
1acf2 41 58 5f 42 4c 4f 42 53 49 5a 45 28 70 4d 65 6d  AX_BLOBSIZE(pMem
1acf3 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  );.  if( sqlite3
1acf4 56 64 62 65 4d 65 6d 54 6f 6f 42 69 67 28 70 4d  VdbeMemTooBig(pM
1acf5 65 6d 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  em) ){.    goto 
1acf6 74 6f 6f 5f 62 69 67 3b 0a 20 20 7d 0a 20 20 62  too_big;.  }.  b
1acf7 72 65 61 6b 3b 0a 7d 0a 0a 0a 23 69 66 20 21 64  reak;.}...#if !d
1acf8 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1acf9 49 54 5f 56 41 43 55 55 4d 29 20 26 26 20 21 64  IT_VACUUM) && !d
1acfa 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1acfb 49 54 5f 41 54 54 41 43 48 29 0a 2f 2a 20 4f 70  IT_ATTACH)./* Op
1acfc 63 6f 64 65 3a 20 56 61 63 75 75 6d 20 2a 20 2a  code: Vacuum * *
1acfd 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 56 61 63   * * *.**.** Vac
1acfe 75 75 6d 20 74 68 65 20 65 6e 74 69 72 65 20 64  uum the entire d
1acff 61 74 61 62 61 73 65 2e 20 20 54 68 69 73 20 6f  atabase.  This o
1ad00 70 63 6f 64 65 20 77 69 6c 6c 20 63 61 75 73 65  pcode will cause
1ad01 20 6f 74 68 65 72 20 76 69 72 74 75 61 6c 0a 2a   other virtual.*
1ad02 2a 20 6d 61 63 68 69 6e 65 73 20 74 6f 20 62 65  * machines to be
1ad03 20 63 72 65 61 74 65 64 20 61 6e 64 20 72 75 6e   created and run
1ad04 2e 20 20 49 74 20 6d 61 79 20 6e 6f 74 20 62 65  .  It may not be
1ad05 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74   called from wit
1ad06 68 69 6e 0a 2a 2a 20 61 20 74 72 61 6e 73 61 63  hin.** a transac
1ad07 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50  tion..*/.case OP
1ad08 5f 56 61 63 75 75 6d 3a 20 7b 0a 20 20 69 66 28  _Vacuum: {.  if(
1ad09 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1ad0a 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1ad0b 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1ad0c 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ; .  rc = sqlite
1ad0d 33 52 75 6e 56 61 63 75 75 6d 28 26 70 2d 3e 7a  3RunVacuum(&p->z
1ad0e 45 72 72 4d 73 67 2c 20 64 62 29 3b 0a 20 20 69  ErrMsg, db);.  i
1ad0f 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1ad10 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62  On(db) ) goto ab
1ad11 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73  ort_due_to_misus
1ad12 65 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  e;.  break;.}.#e
1ad13 6e 64 69 66 0a 0a 23 69 66 20 21 64 65 66 69 6e  ndif..#if !defin
1ad14 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ed(SQLITE_OMIT_A
1ad15 55 54 4f 56 41 43 55 55 4d 29 0a 2f 2a 20 4f 70  UTOVACUUM)./* Op
1ad16 63 6f 64 65 3a 20 49 6e 63 72 56 61 63 75 75 6d  code: IncrVacuum
1ad17 20 50 31 20 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a   P1 P2 * * *.**.
1ad18 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 73 69 6e  ** Perform a sin
1ad19 67 6c 65 20 73 74 65 70 20 6f 66 20 74 68 65 20  gle step of the 
1ad1a 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
1ad1b 75 6d 20 70 72 6f 63 65 64 75 72 65 20 6f 6e 0a  um procedure on.
1ad1c 2a 2a 20 74 68 65 20 50 31 20 64 61 74 61 62 61  ** the P1 databa
1ad1d 73 65 2e 20 49 66 20 74 68 65 20 76 61 63 75 75  se. If the vacuu
1ad1e 6d 20 68 61 73 20 66 69 6e 69 73 68 65 64 2c 20  m has finished, 
1ad1f 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63 74  jump to instruct
1ad20 69 6f 6e 0a 2a 2a 20 50 32 2e 20 4f 74 68 65 72  ion.** P2. Other
1ad21 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72 6f 75  wise, fall throu
1ad22 67 68 20 74 6f 20 74 68 65 20 6e 65 78 74 20 69  gh to the next i
1ad23 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63  nstruction..*/.c
1ad24 61 73 65 20 4f 50 5f 49 6e 63 72 56 61 63 75 75  ase OP_IncrVacuu
1ad25 6d 3a 20 7b 20 20 20 20 20 20 20 20 2f 2a 20 6a  m: {        /* j
1ad26 75 6d 70 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a  ump */.  Btree *
1ad27 70 42 74 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  pBt;..  assert( 
1ad28 70 4f 70 2d 3e 70 31 3e 3d 30 20 26 26 20 70 4f  pOp->p1>=0 && pO
1ad29 70 2d 3e 70 31 3c 64 62 2d 3e 6e 44 62 20 29 3b  p->p1<db->nDb );
1ad2a 0a 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e 62  .  assert( (p->b
1ad2b 74 72 65 65 4d 61 73 6b 20 26 20 28 31 3c 3c 70  treeMask & (1<<p
1ad2c 4f 70 2d 3e 70 31 29 29 21 3d 30 20 29 3b 0a 20  Op->p1))!=0 );. 
1ad2d 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b 70   pBt = db->aDb[p
1ad2e 4f 70 2d 3e 70 31 5d 2e 70 42 74 3b 0a 20 20 72  Op->p1].pBt;.  r
1ad2f 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  c = sqlite3Btree
1ad30 49 6e 63 72 56 61 63 75 75 6d 28 70 42 74 29 3b  IncrVacuum(pBt);
1ad31 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
1ad32 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 70 63  E_DONE ){.    pc
1ad33 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1ad34 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
1ad35 4f 4b 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  OK;.  }.  break;
1ad36 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 4f 70  .}.#endif../* Op
1ad37 63 6f 64 65 3a 20 45 78 70 69 72 65 20 50 31 20  code: Expire P1 
1ad38 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 43 61  * * * *.**.** Ca
1ad39 75 73 65 20 70 72 65 63 6f 6d 70 69 6c 65 64 20  use precompiled 
1ad3a 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 62 65  statements to be
1ad3b 63 6f 6d 65 20 65 78 70 69 72 65 64 2e 20 41 6e  come expired. An
1ad3c 20 65 78 70 69 72 65 64 20 73 74 61 74 65 6d 65   expired stateme
1ad3d 6e 74 0a 2a 2a 20 66 61 69 6c 73 20 77 69 74 68  nt.** fails with
1ad3e 20 61 6e 20 65 72 72 6f 72 20 63 6f 64 65 20 6f   an error code o
1ad3f 66 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20  f SQLITE_SCHEMA 
1ad40 69 66 20 69 74 20 69 73 20 65 76 65 72 20 65 78  if it is ever ex
1ad41 65 63 75 74 65 64 20 0a 2a 2a 20 28 76 69 61 20  ecuted .** (via 
1ad42 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 29 2e  sqlite3_step()).
1ad43 0a 2a 2a 20 0a 2a 2a 20 49 66 20 50 31 20 69 73  .** .** If P1 is
1ad44 20 30 2c 20 74 68 65 6e 20 61 6c 6c 20 53 51 4c   0, then all SQL
1ad45 20 73 74 61 74 65 6d 65 6e 74 73 20 62 65 63 6f   statements beco
1ad46 6d 65 20 65 78 70 69 72 65 64 2e 20 49 66 20 50  me expired. If P
1ad47 31 20 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 0a 2a  1 is non-zero,.*
1ad48 2a 20 74 68 65 6e 20 6f 6e 6c 79 20 74 68 65 20  * then only the 
1ad49 63 75 72 72 65 6e 74 6c 79 20 65 78 65 63 75 74  currently execut
1ad4a 69 6e 67 20 73 74 61 74 65 6d 65 6e 74 20 69 73  ing statement is
1ad4b 20 61 66 66 65 63 74 65 64 2e 20 0a 2a 2f 0a 63   affected. .*/.c
1ad4c 61 73 65 20 4f 50 5f 45 78 70 69 72 65 3a 20 7b  ase OP_Expire: {
1ad4d 0a 20 20 69 66 28 20 21 70 4f 70 2d 3e 70 31 20  .  if( !pOp->p1 
1ad4e 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1ad4f 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
1ad50 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 7d 65  ements(db);.  }e
1ad51 6c 73 65 7b 0a 20 20 20 20 70 2d 3e 65 78 70 69  lse{.    p->expi
1ad52 72 65 64 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 62  red = 1;.  }.  b
1ad53 72 65 61 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66  reak;.}..#ifndef
1ad54 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 48 41   SQLITE_OMIT_SHA
1ad55 52 45 44 5f 43 41 43 48 45 0a 2f 2a 20 4f 70 63  RED_CACHE./* Opc
1ad56 6f 64 65 3a 20 54 61 62 6c 65 4c 6f 63 6b 20 50  ode: TableLock P
1ad57 31 20 50 32 20 50 33 20 50 34 20 2a 0a 2a 2a 0a  1 P2 P3 P4 *.**.
1ad58 2a 2a 20 4f 62 74 61 69 6e 20 61 20 6c 6f 63 6b  ** Obtain a lock
1ad59 20 6f 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72   on a particular
1ad5a 20 74 61 62 6c 65 2e 20 54 68 69 73 20 69 6e 73   table. This ins
1ad5b 74 72 75 63 74 69 6f 6e 20 69 73 20 6f 6e 6c 79  truction is only
1ad5c 20 75 73 65 64 20 77 68 65 6e 0a 2a 2a 20 74 68   used when.** th
1ad5d 65 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 66  e shared-cache f
1ad5e 65 61 74 75 72 65 20 69 73 20 65 6e 61 62 6c 65  eature is enable
1ad5f 64 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 50 31 20  d. .**.** If P1 
1ad60 69 73 20 20 74 68 65 20 69 6e 64 65 78 20 6f 66  is  the index of
1ad61 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   the database in
1ad62 20 73 71 6c 69 74 65 33 2e 61 44 62 5b 5d 20 6f   sqlite3.aDb[] o
1ad63 66 20 74 68 65 20 64 61 74 61 62 61 73 65 0a 2a  f the database.*
1ad64 2a 20 6f 6e 20 77 68 69 63 68 20 74 68 65 20 6c  * on which the l
1ad65 6f 63 6b 20 69 73 20 61 63 71 75 69 72 65 64 2e  ock is acquired.
1ad66 20 20 41 20 72 65 61 64 6c 6f 63 6b 20 69 73 20    A readlock is 
1ad67 6f 62 74 61 69 6e 65 64 20 69 66 20 50 33 3d 3d  obtained if P3==
1ad68 30 20 6f 72 0a 2a 2a 20 61 20 77 72 69 74 65 20  0 or.** a write 
1ad69 6c 6f 63 6b 20 69 66 20 50 33 3d 3d 31 2e 0a 2a  lock if P3==1..*
1ad6a 2a 0a 2a 2a 20 50 32 20 63 6f 6e 74 61 69 6e 73  *.** P2 contains
1ad6b 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 20 6f   the root-page o
1ad6c 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c  f the table to l
1ad6d 6f 63 6b 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 63 6f  ock..**.** P4 co
1ad6e 6e 74 61 69 6e 73 20 61 20 70 6f 69 6e 74 65 72  ntains a pointer
1ad6f 20 74 6f 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   to the name of 
1ad70 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e 67 20  the table being 
1ad71 6c 6f 63 6b 65 64 2e 20 54 68 69 73 20 69 73 20  locked. This is 
1ad72 6f 6e 6c 79 0a 2a 2a 20 75 73 65 64 20 74 6f 20  only.** used to 
1ad73 67 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f  generate an erro
1ad74 72 20 6d 65 73 73 61 67 65 20 69 66 20 74 68 65  r message if the
1ad75 20 6c 6f 63 6b 20 63 61 6e 6e 6f 74 20 62 65 20   lock cannot be 
1ad76 6f 62 74 61 69 6e 65 64 2e 0a 2a 2f 0a 63 61 73  obtained..*/.cas
1ad77 65 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 3a 20  e OP_TableLock: 
1ad78 7b 0a 20 20 69 6e 74 20 70 31 20 3d 20 70 4f 70  {.  int p1 = pOp
1ad79 2d 3e 70 31 3b 20 0a 20 20 75 38 20 69 73 57 72  ->p1; .  u8 isWr
1ad7a 69 74 65 4c 6f 63 6b 20 3d 20 70 4f 70 2d 3e 70  iteLock = pOp->p
1ad7b 33 3b 0a 20 20 61 73 73 65 72 74 28 20 70 31 3e  3;.  assert( p1>
1ad7c 3d 30 20 26 26 20 70 31 3c 64 62 2d 3e 6e 44 62  =0 && p1<db->nDb
1ad7d 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70   );.  assert( (p
1ad7e 2d 3e 62 74 72 65 65 4d 61 73 6b 20 26 20 28 31  ->btreeMask & (1
1ad7f 3c 3c 70 31 29 29 21 3d 30 20 29 3b 0a 20 20 61  <<p1))!=0 );.  a
1ad80 73 73 65 72 74 28 20 69 73 57 72 69 74 65 4c 6f  ssert( isWriteLo
1ad81 63 6b 3d 3d 30 20 7c 7c 20 69 73 57 72 69 74 65  ck==0 || isWrite
1ad82 4c 6f 63 6b 3d 3d 31 20 29 3b 0a 20 20 72 63 20  Lock==1 );.  rc 
1ad83 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 6f  = sqlite3BtreeLo
1ad84 63 6b 54 61 62 6c 65 28 64 62 2d 3e 61 44 62 5b  ckTable(db->aDb[
1ad85 70 31 5d 2e 70 42 74 2c 20 70 4f 70 2d 3e 70 32  p1].pBt, pOp->p2
1ad86 2c 20 69 73 57 72 69 74 65 4c 6f 63 6b 29 3b 0a  , isWriteLock);.
1ad87 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1ad88 5f 4c 4f 43 4b 45 44 20 29 7b 0a 20 20 20 20 63  _LOCKED ){.    c
1ad89 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70  onst char *z = p
1ad8a 4f 70 2d 3e 70 34 2e 7a 3b 0a 20 20 20 20 73 71  Op->p4.z;.    sq
1ad8b 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1ad8c 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1ad8d 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20  "database table 
1ad8e 69 73 20 6c 6f 63 6b 65 64 3a 20 25 73 22 2c 20  is locked: %s", 
1ad8f 7a 29 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b 3b  z);.  }.  break;
1ad90 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1ad91 49 54 45 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f  ITE_OMIT_SHARED_
1ad92 43 41 43 48 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  CACHE */..#ifnde
1ad93 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1ad94 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
1ad95 63 6f 64 65 3a 20 56 42 65 67 69 6e 20 2a 20 2a  code: VBegin * *
1ad96 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34   * P4 *.**.** P4
1ad97 20 6d 61 79 20 62 65 20 61 20 70 6f 69 6e 74 65   may be a pointe
1ad98 72 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f  r to an sqlite3_
1ad99 76 74 61 62 20 73 74 72 75 63 74 75 72 65 2e 20  vtab structure. 
1ad9a 49 66 20 73 6f 2c 20 63 61 6c 6c 20 74 68 65 20  If so, call the 
1ad9b 0a 2a 2a 20 78 42 65 67 69 6e 20 6d 65 74 68 6f  .** xBegin metho
1ad9c 64 20 66 6f 72 20 74 68 61 74 20 74 61 62 6c 65  d for that table
1ad9d 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 2c 20 77 68  ..**.** Also, wh
1ad9e 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 50 34 20  ether or not P4 
1ad9f 69 73 20 73 65 74 2c 20 63 68 65 63 6b 20 74 68  is set, check th
1ada0 61 74 20 74 68 69 73 20 69 73 20 6e 6f 74 20 62  at this is not b
1ada1 65 69 6e 67 20 63 61 6c 6c 65 64 20 66 72 6f 6d  eing called from
1ada2 0a 2a 2a 20 77 69 74 68 69 6e 20 61 20 63 61 6c  .** within a cal
1ada3 6c 62 61 63 6b 20 74 6f 20 61 20 76 69 72 74 75  lback to a virtu
1ada4 61 6c 20 74 61 62 6c 65 20 78 53 79 6e 63 28 29  al table xSync()
1ada5 20 6d 65 74 68 6f 64 2e 20 49 66 20 69 74 20 69   method. If it i
1ada6 73 2c 20 73 65 74 20 74 68 65 0a 2a 2a 20 65 72  s, set the.** er
1ada7 72 6f 72 20 63 6f 64 65 20 74 6f 20 53 51 4c 49  ror code to SQLI
1ada8 54 45 5f 4c 4f 43 4b 45 44 2e 0a 2a 2f 0a 63 61  TE_LOCKED..*/.ca
1ada9 73 65 20 4f 50 5f 56 42 65 67 69 6e 3a 20 7b 0a  se OP_VBegin: {.
1adaa 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1adab 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
1adac 70 56 74 61 62 3b 0a 20 20 72 63 20 3d 20 73 71  pVtab;.  rc = sq
1adad 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e 28 64  lite3VtabBegin(d
1adae 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66 28  b, pVtab);.  if(
1adaf 20 70 56 74 61 62 20 29 7b 0a 20 20 20 20 73 71   pVtab ){.    sq
1adb0 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1adb1 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20  p->zErrMsg);.   
1adb2 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56   p->zErrMsg = pV
1adb3 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20  tab->zErrMsg;.  
1adb4 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
1adb5 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 62 72 65 61   = 0;.  }.  brea
1adb6 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  k;.}.#endif /* S
1adb7 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1adb8 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e  ALTABLE */..#ifn
1adb9 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1adba 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20  VIRTUALTABLE./* 
1adbb 4f 70 63 6f 64 65 3a 20 56 43 72 65 61 74 65 20  Opcode: VCreate 
1adbc 50 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a  P1 * * P4 *.**.*
1adbd 2a 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65  * P4 is the name
1adbe 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
1adbf 62 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20  ble in database 
1adc0 50 31 2e 20 43 61 6c 6c 20 74 68 65 20 78 43 72  P1. Call the xCr
1adc1 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a 20 66  eate method.** f
1adc2 6f 72 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a  or that table..*
1adc3 2f 0a 63 61 73 65 20 4f 50 5f 56 43 72 65 61 74  /.case OP_VCreat
1adc4 65 3a 20 7b 0a 20 20 72 63 20 3d 20 73 71 6c 69  e: {.  rc = sqli
1adc5 74 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74  te3VtabCallCreat
1adc6 65 28 64 62 2c 20 70 4f 70 2d 3e 70 31 2c 20 70  e(db, pOp->p1, p
1adc7 4f 70 2d 3e 70 34 2e 7a 2c 20 26 70 2d 3e 7a 45  Op->p4.z, &p->zE
1adc8 72 72 4d 73 67 29 3b 0a 20 20 62 72 65 61 6b 3b  rrMsg);.  break;
1adc9 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1adca 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1adcb 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1adcc 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1adcd 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
1adce 63 6f 64 65 3a 20 56 44 65 73 74 72 6f 79 20 50  code: VDestroy P
1adcf 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1add0 20 50 34 20 69 73 20 74 68 65 20 6e 61 6d 65 20   P4 is the name 
1add1 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  of a virtual tab
1add2 6c 65 20 69 6e 20 64 61 74 61 62 61 73 65 20 50  le in database P
1add3 31 2e 20 20 43 61 6c 6c 20 74 68 65 20 78 44 65  1.  Call the xDe
1add4 73 74 72 6f 79 20 6d 65 74 68 6f 64 0a 2a 2a 20  stroy method.** 
1add5 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e 0a 2a  of that table..*
1add6 2f 0a 63 61 73 65 20 4f 50 5f 56 44 65 73 74 72  /.case OP_VDestr
1add7 6f 79 3a 20 7b 0a 20 20 70 2d 3e 69 6e 56 74 61  oy: {.  p->inVta
1add8 62 4d 65 74 68 6f 64 20 3d 20 32 3b 0a 20 20 72  bMethod = 2;.  r
1add9 63 20 3d 20 73 71 6c 69 74 65 33 56 74 61 62 43  c = sqlite3VtabC
1adda 61 6c 6c 44 65 73 74 72 6f 79 28 64 62 2c 20 70  allDestroy(db, p
1addb 4f 70 2d 3e 70 31 2c 20 70 4f 70 2d 3e 70 34 2e  Op->p1, pOp->p4.
1addc 7a 29 3b 0a 20 20 70 2d 3e 69 6e 56 74 61 62 4d  z);.  p->inVtabM
1addd 65 74 68 6f 64 20 3d 20 30 3b 0a 20 20 62 72 65  ethod = 0;.  bre
1adde 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ak;.}.#endif /* 
1addf 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1ade0 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66  UALTABLE */..#if
1ade1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1ade2 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a  _VIRTUALTABLE./*
1ade3 20 4f 70 63 6f 64 65 3a 20 56 4f 70 65 6e 20 50   Opcode: VOpen P
1ade4 31 20 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a  1 * * P4 *.**.**
1ade5 20 50 34 20 69 73 20 61 20 70 6f 69 6e 74 65 72   P4 is a pointer
1ade6 20 74 6f 20 61 20 76 69 72 74 75 61 6c 20 74 61   to a virtual ta
1ade7 62 6c 65 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73  ble object, an s
1ade8 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74 72 75  qlite3_vtab stru
1ade9 63 74 75 72 65 2e 0a 2a 2a 20 50 31 20 69 73 20  cture..** P1 is 
1adea 61 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 2e  a cursor number.
1adeb 20 20 54 68 69 73 20 6f 70 63 6f 64 65 20 6f 70    This opcode op
1adec 65 6e 73 20 61 20 63 75 72 73 6f 72 20 74 6f 20  ens a cursor to 
1aded 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a 20 74  the virtual.** t
1adee 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 73 20  able and stores 
1adef 74 68 61 74 20 63 75 72 73 6f 72 20 69 6e 20 50  that cursor in P
1adf0 31 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 4f  1..*/.case OP_VO
1adf1 70 65 6e 3a 20 7b 0a 20 20 56 64 62 65 43 75 72  pen: {.  VdbeCur
1adf2 73 6f 72 20 2a 70 43 75 72 20 3d 20 30 3b 0a 20  sor *pCur = 0;. 
1adf3 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75   sqlite3_vtab_cu
1adf4 72 73 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f  rsor *pVtabCurso
1adf5 72 20 3d 20 30 3b 0a 0a 20 20 73 71 6c 69 74 65  r = 0;..  sqlite
1adf6 33 5f 76 74 61 62 20 2a 70 56 74 61 62 20 3d 20  3_vtab *pVtab = 
1adf7 70 4f 70 2d 3e 70 34 2e 70 56 74 61 62 3b 0a 20  pOp->p4.pVtab;. 
1adf8 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
1adf9 2a 70 4d 6f 64 75 6c 65 20 3d 20 28 73 71 6c 69  *pModule = (sqli
1adfa 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 29 70 56 74  te3_module *)pVt
1adfb 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20  ab->pModule;..  
1adfc 61 73 73 65 72 74 28 70 56 74 61 62 20 26 26 20  assert(pVtab && 
1adfd 70 4d 6f 64 75 6c 65 29 3b 0a 20 20 69 66 28 20  pModule);.  if( 
1adfe 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1adff 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72  (db) ) goto abor
1ae00 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b  t_due_to_misuse;
1ae01 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  .  rc = pModule-
1ae02 3e 78 4f 70 65 6e 28 70 56 74 61 62 2c 20 26 70  >xOpen(pVtab, &p
1ae03 56 74 61 62 43 75 72 73 6f 72 29 3b 0a 20 20 73  VtabCursor);.  s
1ae04 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
1ae05 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20   p->zErrMsg);.  
1ae06 70 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74  p->zErrMsg = pVt
1ae07 61 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70  ab->zErrMsg;.  p
1ae08 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20  Vtab->zErrMsg = 
1ae09 30 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  0;.  if( sqlite3
1ae0a 53 61 66 65 74 79 4f 6e 28 64 62 29 20 29 20 67  SafetyOn(db) ) g
1ae0b 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74 6f  oto abort_due_to
1ae0c 5f 6d 69 73 75 73 65 3b 0a 20 20 69 66 28 20 53  _misuse;.  if( S
1ae0d 51 4c 49 54 45 5f 4f 4b 3d 3d 72 63 20 29 7b 0a  QLITE_OK==rc ){.
1ae0e 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a      /* Initializ
1ae0f 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63  e sqlite3_vtab_c
1ae10 75 72 73 6f 72 20 62 61 73 65 20 63 6c 61 73 73  ursor base class
1ae11 20 2a 2f 0a 20 20 20 20 70 56 74 61 62 43 75 72   */.    pVtabCur
1ae12 73 6f 72 2d 3e 70 56 74 61 62 20 3d 20 70 56 74  sor->pVtab = pVt
1ae13 61 62 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74  ab;..    /* Init
1ae14 69 61 6c 69 73 65 20 76 64 62 65 20 63 75 72 73  ialise vdbe curs
1ae15 6f 72 20 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 20  or object */.   
1ae16 20 70 43 75 72 20 3d 20 61 6c 6c 6f 63 61 74 65   pCur = allocate
1ae17 43 75 72 73 6f 72 28 70 2c 20 70 4f 70 2d 3e 70  Cursor(p, pOp->p
1ae18 31 2c 20 26 70 4f 70 5b 2d 31 5d 2c 20 2d 31 2c  1, &pOp[-1], -1,
1ae19 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 43 75   0);.    if( pCu
1ae1a 72 20 29 7b 0a 20 20 20 20 20 20 70 43 75 72 2d  r ){.      pCur-
1ae1b 3e 70 56 74 61 62 43 75 72 73 6f 72 20 3d 20 70  >pVtabCursor = p
1ae1c 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20 20 20  VtabCursor;.    
1ae1d 20 20 70 43 75 72 2d 3e 70 4d 6f 64 75 6c 65 20    pCur->pModule 
1ae1e 3d 20 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70  = pVtabCursor->p
1ae1f 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20  Vtab->pModule;. 
1ae20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1ae21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1ae22 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 4d 6f 64   = 1;.      pMod
1ae23 75 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 56 74 61  ule->xClose(pVta
1ae24 62 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 7d 0a  bCursor);.    }.
1ae25 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1ae26 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1ae27 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1ae28 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1ae29 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1ae2a 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1ae2b 3a 20 56 46 69 6c 74 65 72 20 50 31 20 50 32 20  : VFilter P1 P2 
1ae2c 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 31  P3 P4 *.**.** P1
1ae2d 20 69 73 20 61 20 63 75 72 73 6f 72 20 6f 70 65   is a cursor ope
1ae2e 6e 65 64 20 75 73 69 6e 67 20 56 4f 70 65 6e 2e  ned using VOpen.
1ae2f 20 20 50 32 20 69 73 20 61 6e 20 61 64 64 72 65    P2 is an addre
1ae30 73 73 20 74 6f 20 6a 75 6d 70 20 74 6f 20 69 66  ss to jump to if
1ae31 0a 2a 2a 20 74 68 65 20 66 69 6c 74 65 72 65 64  .** the filtered
1ae32 20 72 65 73 75 6c 74 20 73 65 74 20 69 73 20 65   result set is e
1ae33 6d 70 74 79 2e 0a 2a 2a 0a 2a 2a 20 50 34 20 69  mpty..**.** P4 i
1ae34 73 20 65 69 74 68 65 72 20 4e 55 4c 4c 20 6f 72  s either NULL or
1ae35 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20 77   a string that w
1ae36 61 73 20 67 65 6e 65 72 61 74 65 64 20 62 79 20  as generated by 
1ae37 74 68 65 20 78 42 65 73 74 49 6e 64 65 78 0a 2a  the xBestIndex.*
1ae38 2a 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  * method of the 
1ae39 6d 6f 64 75 6c 65 2e 20 20 54 68 65 20 69 6e 74  module.  The int
1ae3a 65 72 70 72 65 74 61 74 69 6f 6e 20 6f 66 20 74  erpretation of t
1ae3b 68 65 20 50 34 20 73 74 72 69 6e 67 20 69 73 20  he P4 string is 
1ae3c 6c 65 66 74 0a 2a 2a 20 74 6f 20 74 68 65 20 6d  left.** to the m
1ae3d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  odule implementa
1ae3e 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  tion..**.** This
1ae3f 20 6f 70 63 6f 64 65 20 69 6e 76 6f 6b 65 73 20   opcode invokes 
1ae40 74 68 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68  the xFilter meth
1ae41 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61  od on the virtua
1ae42 6c 20 74 61 62 6c 65 20 73 70 65 63 69 66 69 65  l table specifie
1ae43 64 0a 2a 2a 20 62 79 20 50 31 2e 20 20 54 68 65  d.** by P1.  The
1ae44 20 69 6e 74 65 67 65 72 20 71 75 65 72 79 20 70   integer query p
1ae45 6c 61 6e 20 70 61 72 61 6d 65 74 65 72 20 74 6f  lan parameter to
1ae46 20 78 46 69 6c 74 65 72 20 69 73 20 73 74 6f 72   xFilter is stor
1ae47 65 64 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a  ed in register.*
1ae48 2a 20 50 33 2e 20 52 65 67 69 73 74 65 72 20 50  * P3. Register P
1ae49 33 2b 31 20 73 74 6f 72 65 73 20 74 68 65 20 61  3+1 stores the a
1ae4a 72 67 63 20 70 61 72 61 6d 65 74 65 72 20 74 6f  rgc parameter to
1ae4b 20 62 65 20 70 61 73 73 65 64 20 74 6f 20 74 68   be passed to th
1ae4c 65 0a 2a 2a 20 78 46 69 6c 74 65 72 20 6d 65 74  e.** xFilter met
1ae4d 68 6f 64 2e 20 52 65 67 69 73 74 65 72 73 20 50  hod. Registers P
1ae4e 33 2b 32 2e 2e 50 33 2b 31 2b 61 72 67 63 20 61  3+2..P3+1+argc a
1ae4f 72 65 20 74 68 65 20 61 72 67 63 0a 2a 2a 20 61  re the argc.** a
1ae50 64 64 69 74 69 6f 6e 61 6c 20 70 61 72 61 6d 65  dditional parame
1ae51 74 65 72 73 20 77 68 69 63 68 20 61 72 65 20 70  ters which are p
1ae52 61 73 73 65 64 20 74 6f 0a 2a 2a 20 78 46 69 6c  assed to.** xFil
1ae53 74 65 72 20 61 73 20 61 72 67 76 2e 20 52 65 67  ter as argv. Reg
1ae54 69 73 74 65 72 20 50 33 2b 32 20 62 65 63 6f 6d  ister P3+2 becom
1ae55 65 73 20 61 72 67 76 5b 30 5d 20 77 68 65 6e 20  es argv[0] when 
1ae56 70 61 73 73 65 64 20 74 6f 20 78 46 69 6c 74 65  passed to xFilte
1ae57 72 2e 0a 2a 2a 0a 2a 2a 20 41 20 6a 75 6d 70 20  r..**.** A jump 
1ae58 69 73 20 6d 61 64 65 20 74 6f 20 50 32 20 69 66  is made to P2 if
1ae59 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20   the result set 
1ae5a 61 66 74 65 72 20 66 69 6c 74 65 72 69 6e 67 20  after filtering 
1ae5b 77 6f 75 6c 64 20 62 65 20 65 6d 70 74 79 2e 0a  would be empty..
1ae5c 2a 2f 0a 63 61 73 65 20 4f 50 5f 56 46 69 6c 74  */.case OP_VFilt
1ae5d 65 72 3a 20 7b 20 20 20 2f 2a 20 6a 75 6d 70 20  er: {   /* jump 
1ae5e 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 3b 0a 20  */.  int nArg;. 
1ae5f 20 69 6e 74 20 69 51 75 65 72 79 3b 0a 20 20 63   int iQuery;.  c
1ae60 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64  onst sqlite3_mod
1ae61 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20  ule *pModule;.  
1ae62 4d 65 6d 20 2a 70 51 75 65 72 79 20 3d 20 26 70  Mem *pQuery = &p
1ae63 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70 33 5d 3b  ->aMem[pOp->p3];
1ae64 0a 20 20 4d 65 6d 20 2a 70 41 72 67 63 20 3d 20  .  Mem *pArgc = 
1ae65 26 70 51 75 65 72 79 5b 31 5d 3b 0a 20 20 73 71  &pQuery[1];.  sq
1ae66 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
1ae67 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 3b 0a  r *pVtabCursor;.
1ae68 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1ae69 70 56 74 61 62 3b 0a 0a 20 20 56 64 62 65 43 75  pVtab;..  VdbeCu
1ae6a 72 73 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e  rsor *pCur = p->
1ae6b 61 70 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a  apCsr[pOp->p1];.
1ae6c 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41 43  .  REGISTER_TRAC
1ae6d 45 28 70 4f 70 2d 3e 70 33 2c 20 70 51 75 65 72  E(pOp->p3, pQuer
1ae6e 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  y);.  assert( pC
1ae6f 75 72 2d 3e 70 56 74 61 62 43 75 72 73 6f 72 20  ur->pVtabCursor 
1ae70 29 3b 0a 20 20 70 56 74 61 62 43 75 72 73 6f 72  );.  pVtabCursor
1ae71 20 3d 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75   = pCur->pVtabCu
1ae72 72 73 6f 72 3b 0a 20 20 70 56 74 61 62 20 3d 20  rsor;.  pVtab = 
1ae73 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
1ae74 61 62 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20  ab;.  pModule = 
1ae75 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a  pVtab->pModule;.
1ae76 0a 20 20 2f 2a 20 47 72 61 62 20 74 68 65 20 69  .  /* Grab the i
1ae77 6e 64 65 78 20 6e 75 6d 62 65 72 20 61 6e 64 20  ndex number and 
1ae78 61 72 67 63 20 70 61 72 61 6d 65 74 65 72 73 20  argc parameters 
1ae79 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 28 70 51  */.  assert( (pQ
1ae7a 75 65 72 79 2d 3e 66 6c 61 67 73 26 4d 45 4d 5f  uery->flags&MEM_
1ae7b 49 6e 74 29 21 3d 30 20 26 26 20 70 41 72 67 63  Int)!=0 && pArgc
1ae7c 2d 3e 66 6c 61 67 73 3d 3d 4d 45 4d 5f 49 6e 74  ->flags==MEM_Int
1ae7d 20 29 3b 0a 20 20 6e 41 72 67 20 3d 20 70 41 72   );.  nArg = pAr
1ae7e 67 63 2d 3e 75 2e 69 3b 0a 20 20 69 51 75 65 72  gc->u.i;.  iQuer
1ae7f 79 20 3d 20 70 51 75 65 72 79 2d 3e 75 2e 69 3b  y = pQuery->u.i;
1ae80 0a 0a 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68  ..  /* Invoke th
1ae81 65 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64  e xFilter method
1ae82 20 2a 2f 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20   */.  {.    int 
1ae83 72 65 73 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74  res = 0;.    int
1ae84 20 69 3b 0a 20 20 20 20 4d 65 6d 20 2a 2a 61 70   i;.    Mem **ap
1ae85 41 72 67 20 3d 20 70 2d 3e 61 70 41 72 67 3b 0a  Arg = p->apArg;.
1ae86 20 20 20 20 66 6f 72 28 69 20 3d 20 30 3b 20 69      for(i = 0; i
1ae87 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nArg; i++){.   
1ae88 20 20 20 61 70 41 72 67 5b 69 5d 20 3d 20 26 70     apArg[i] = &p
1ae89 41 72 67 63 5b 69 2b 31 5d 3b 0a 20 20 20 20 20  Argc[i+1];.     
1ae8a 20 73 74 6f 72 65 54 79 70 65 49 6e 66 6f 28 61   storeTypeInfo(a
1ae8b 70 41 72 67 5b 69 5d 2c 20 30 29 3b 0a 20 20 20  pArg[i], 0);.   
1ae8c 20 7d 0a 0a 20 20 20 20 69 66 28 20 73 71 6c 69   }..    if( sqli
1ae8d 74 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29  te3SafetyOff(db)
1ae8e 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75   ) goto abort_du
1ae8f 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20  e_to_misuse;.   
1ae90 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b   sqlite3VtabLock
1ae91 28 70 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e  (pVtab);.    p->
1ae92 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31  inVtabMethod = 1
1ae93 3b 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64 75  ;.    rc = pModu
1ae94 6c 65 2d 3e 78 46 69 6c 74 65 72 28 70 56 74 61  le->xFilter(pVta
1ae95 62 43 75 72 73 6f 72 2c 20 69 51 75 65 72 79 2c  bCursor, iQuery,
1ae96 20 70 4f 70 2d 3e 70 34 2e 7a 2c 20 6e 41 72 67   pOp->p4.z, nArg
1ae97 2c 20 61 70 41 72 67 29 3b 0a 20 20 20 20 70 2d  , apArg);.    p-
1ae98 3e 69 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20  >inVtabMethod = 
1ae99 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62  0;.    sqlite3Db
1ae9a 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72  Free(db, p->zErr
1ae9b 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a 45 72  Msg);.    p->zEr
1ae9c 72 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45  rMsg = pVtab->zE
1ae9d 72 72 4d 73 67 3b 0a 20 20 20 20 70 56 74 61 62  rrMsg;.    pVtab
1ae9e 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20  ->zErrMsg = 0;. 
1ae9f 20 20 20 73 71 6c 69 74 65 33 56 74 61 62 55 6e     sqlite3VtabUn
1aea0 6c 6f 63 6b 28 64 62 2c 20 70 56 74 61 62 29 3b  lock(db, pVtab);
1aea1 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
1aea2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
1aea3 72 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78  res = pModule->x
1aea4 45 6f 66 28 70 56 74 61 62 43 75 72 73 6f 72 29  Eof(pVtabCursor)
1aea5 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
1aea6 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1aea7 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1aea8 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1aea9 0a 20 20 20 20 69 66 28 20 72 65 73 20 29 7b 0a  .    if( res ){.
1aeaa 20 20 20 20 20 20 70 63 20 3d 20 70 4f 70 2d 3e        pc = pOp->
1aeab 70 32 20 2d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  p2 - 1;.    }.  
1aeac 7d 0a 20 20 70 43 75 72 2d 3e 6e 75 6c 6c 52 6f  }.  pCur->nullRo
1aead 77 20 3d 20 30 3b 0a 0a 20 20 62 72 65 61 6b 3b  w = 0;..  break;
1aeae 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  .}.#endif /* SQL
1aeaf 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1aeb0 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65  TABLE */..#ifnde
1aeb1 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
1aeb2 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70  RTUALTABLE./* Op
1aeb3 63 6f 64 65 3a 20 56 52 6f 77 69 64 20 50 31 20  code: VRowid P1 
1aeb4 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53  P2 * * *.**.** S
1aeb5 74 6f 72 65 20 69 6e 74 6f 20 72 65 67 69 73 74  tore into regist
1aeb6 65 72 20 50 32 20 20 74 68 65 20 72 6f 77 69 64  er P2  the rowid
1aeb7 20 6f 66 0a 2a 2a 20 74 68 65 20 76 69 72 74 75   of.** the virtu
1aeb8 61 6c 2d 74 61 62 6c 65 20 74 68 61 74 20 74 68  al-table that th
1aeb9 65 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70  e P1 cursor is p
1aeba 6f 69 6e 74 69 6e 67 20 74 6f 2e 0a 2a 2f 0a 63  ointing to..*/.c
1aebb 61 73 65 20 4f 50 5f 56 52 6f 77 69 64 3a 20 7b  ase OP_VRowid: {
1aebc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1aebd 6f 75 74 32 2d 70 72 65 72 65 6c 65 61 73 65 20  out2-prerelease 
1aebe 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  */.  sqlite3_vta
1aebf 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73  b *pVtab;.  cons
1aec0 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1aec1 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 73 71 6c   *pModule;.  sql
1aec2 69 74 65 5f 69 6e 74 36 34 20 69 52 6f 77 3b 0a  ite_int64 iRow;.
1aec3 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70 43    VdbeCursor *pC
1aec4 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70 4f  ur = p->apCsr[pO
1aec5 70 2d 3e 70 31 5d 3b 0a 0a 20 20 61 73 73 65 72  p->p1];..  asser
1aec6 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75  t( pCur->pVtabCu
1aec7 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 43  rsor );.  if( pC
1aec8 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20  ur->nullRow ){. 
1aec9 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1aeca 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56  pVtab = pCur->pV
1aecb 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1aecc 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56  ;.  pModule = pV
1aecd 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
1aece 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d  assert( pModule-
1aecf 3e 78 52 6f 77 69 64 20 29 3b 0a 20 20 69 66 28  >xRowid );.  if(
1aed0 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
1aed1 66 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  f(db) ) goto abo
1aed2 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1aed3 3b 0a 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65  ;.  rc = pModule
1aed4 2d 3e 78 52 6f 77 69 64 28 70 43 75 72 2d 3e 70  ->xRowid(pCur->p
1aed5 56 74 61 62 43 75 72 73 6f 72 2c 20 26 69 52 6f  VtabCursor, &iRo
1aed6 77 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  w);.  sqlite3DbF
1aed7 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d  ree(db, p->zErrM
1aed8 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73  sg);.  p->zErrMs
1aed9 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d  g = pVtab->zErrM
1aeda 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72  sg;.  pVtab->zEr
1aedb 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 66 28 20  rMsg = 0;.  if( 
1aedc 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28  sqlite3SafetyOn(
1aedd 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74  db) ) goto abort
1aede 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a  _due_to_misuse;.
1aedf 20 20 4d 65 6d 53 65 74 54 79 70 65 46 6c 61 67    MemSetTypeFlag
1aee0 28 70 4f 75 74 2c 20 4d 45 4d 5f 49 6e 74 29 3b  (pOut, MEM_Int);
1aee1 0a 20 20 70 4f 75 74 2d 3e 75 2e 69 20 3d 20 69  .  pOut->u.i = i
1aee2 52 6f 77 3b 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a  Row;.  break;.}.
1aee3 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1aee4 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
1aee5 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53  LE */..#ifndef S
1aee6 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
1aee7 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64  ALTABLE./* Opcod
1aee8 65 3a 20 56 43 6f 6c 75 6d 6e 20 50 31 20 50 32  e: VColumn P1 P2
1aee9 20 50 33 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 53 74   P3 * *.**.** St
1aeea 6f 72 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66  ore the value of
1aeeb 20 74 68 65 20 50 32 2d 74 68 20 63 6f 6c 75 6d   the P2-th colum
1aeec 6e 20 6f 66 0a 2a 2a 20 74 68 65 20 72 6f 77 20  n of.** the row 
1aeed 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 2d 74  of the virtual-t
1aeee 61 62 6c 65 20 74 68 61 74 20 74 68 65 20 0a 2a  able that the .*
1aeef 2a 20 50 31 20 63 75 72 73 6f 72 20 69 73 20 70  * P1 cursor is p
1aef0 6f 69 6e 74 69 6e 67 20 74 6f 20 69 6e 74 6f 20  ointing to into 
1aef1 72 65 67 69 73 74 65 72 20 50 33 2e 0a 2a 2f 0a  register P3..*/.
1aef2 63 61 73 65 20 4f 50 5f 56 43 6f 6c 75 6d 6e 3a  case OP_VColumn:
1aef3 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61   {.  sqlite3_vta
1aef4 62 20 2a 70 56 74 61 62 3b 0a 20 20 63 6f 6e 73  b *pVtab;.  cons
1aef5 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
1aef6 20 2a 70 4d 6f 64 75 6c 65 3b 0a 20 20 4d 65 6d   *pModule;.  Mem
1aef7 20 2a 70 44 65 73 74 3b 0a 20 20 73 71 6c 69 74   *pDest;.  sqlit
1aef8 65 33 5f 63 6f 6e 74 65 78 74 20 73 43 6f 6e 74  e3_context sCont
1aef9 65 78 74 3b 0a 0a 20 20 56 64 62 65 43 75 72 73  ext;..  VdbeCurs
1aefa 6f 72 20 2a 70 43 75 72 20 3d 20 70 2d 3e 61 70  or *pCur = p->ap
1aefb 43 73 72 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20  Csr[pOp->p1];.  
1aefc 61 73 73 65 72 74 28 20 70 43 75 72 2d 3e 70 56  assert( pCur->pV
1aefd 74 61 62 43 75 72 73 6f 72 20 29 3b 0a 20 20 61  tabCursor );.  a
1aefe 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 33 3e 30  ssert( pOp->p3>0
1aeff 20 26 26 20 70 4f 70 2d 3e 70 33 3c 3d 70 2d 3e   && pOp->p3<=p->
1af00 6e 4d 65 6d 20 29 3b 0a 20 20 70 44 65 73 74 20  nMem );.  pDest 
1af01 3d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e  = &p->aMem[pOp->
1af02 70 33 5d 3b 0a 20 20 69 66 28 20 70 43 75 72 2d  p3];.  if( pCur-
1af03 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20 20 20 20  >nullRow ){.    
1af04 73 71 6c 69 74 65 33 56 64 62 65 4d 65 6d 53 65  sqlite3VdbeMemSe
1af05 74 4e 75 6c 6c 28 70 44 65 73 74 29 3b 0a 20 20  tNull(pDest);.  
1af06 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 70    break;.  }.  p
1af07 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56 74  Vtab = pCur->pVt
1af08 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62 3b  abCursor->pVtab;
1af09 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56 74  .  pModule = pVt
1af0a 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 61  ab->pModule;.  a
1af0b 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d 3e  ssert( pModule->
1af0c 78 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 6d 65 6d  xColumn );.  mem
1af0d 73 65 74 28 26 73 43 6f 6e 74 65 78 74 2c 20 30  set(&sContext, 0
1af0e 2c 20 73 69 7a 65 6f 66 28 73 43 6f 6e 74 65 78  , sizeof(sContex
1af0f 74 29 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 6f  t));..  /* The o
1af10 75 74 70 75 74 20 63 65 6c 6c 20 6d 61 79 20 61  utput cell may a
1af11 6c 72 65 61 64 79 20 68 61 76 65 20 61 20 62 75  lready have a bu
1af12 66 66 65 72 20 61 6c 6c 6f 63 61 74 65 64 2e 20  ffer allocated. 
1af13 4d 6f 76 65 0a 20 20 2a 2a 20 74 68 65 20 63 75  Move.  ** the cu
1af14 72 72 65 6e 74 20 63 6f 6e 74 65 6e 74 73 20 74  rrent contents t
1af15 6f 20 73 43 6f 6e 74 65 78 74 2e 73 20 73 6f 20  o sContext.s so 
1af16 69 6e 20 63 61 73 65 20 74 68 65 20 75 73 65 72  in case the user
1af17 2d 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2a 20  -function .  ** 
1af18 63 61 6e 20 75 73 65 20 74 68 65 20 61 6c 72 65  can use the alre
1af19 61 64 79 20 61 6c 6c 6f 63 61 74 65 64 20 62 75  ady allocated bu
1af1a 66 66 65 72 20 69 6e 73 74 65 61 64 20 6f 66 20  ffer instead of 
1af1b 61 6c 6c 6f 63 61 74 69 6e 67 20 61 20 0a 20 20  allocating a .  
1af1c 2a 2a 20 6e 65 77 20 6f 6e 65 2e 0a 20 20 2a 2f  ** new one..  */
1af1d 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 65  .  sqlite3VdbeMe
1af1e 6d 4d 6f 76 65 28 26 73 43 6f 6e 74 65 78 74 2e  mMove(&sContext.
1af1f 73 2c 20 70 44 65 73 74 29 3b 0a 20 20 4d 65 6d  s, pDest);.  Mem
1af20 53 65 74 54 79 70 65 46 6c 61 67 28 26 73 43 6f  SetTypeFlag(&sCo
1af21 6e 74 65 78 74 2e 73 2c 20 4d 45 4d 5f 4e 75 6c  ntext.s, MEM_Nul
1af22 6c 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  l);..  if( sqlit
1af23 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20  e3SafetyOff(db) 
1af24 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65  ) goto abort_due
1af25 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 72 63  _to_misuse;.  rc
1af26 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e 78 43 6f 6c   = pModule->xCol
1af27 75 6d 6e 28 70 43 75 72 2d 3e 70 56 74 61 62 43  umn(pCur->pVtabC
1af28 75 72 73 6f 72 2c 20 26 73 43 6f 6e 74 65 78 74  ursor, &sContext
1af29 2c 20 70 4f 70 2d 3e 70 32 29 3b 0a 20 20 73 71  , pOp->p2);.  sq
1af2a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1af2b 70 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 70  p->zErrMsg);.  p
1af2c 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 70 56 74 61  ->zErrMsg = pVta
1af2d 62 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 70 56  b->zErrMsg;.  pV
1af2e 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30  tab->zErrMsg = 0
1af2f 3b 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68 65  ;..  /* Copy the
1af30 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20 66   result of the f
1af31 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 50  unction to the P
1af32 33 20 72 65 67 69 73 74 65 72 2e 20 57 65 0a 20  3 register. We. 
1af33 20 2a 2a 20 64 6f 20 74 68 69 73 20 72 65 67 61   ** do this rega
1af34 72 64 6c 65 73 73 20 6f 66 20 77 68 65 74 68 65  rdless of whethe
1af35 72 20 6f 72 20 6e 6f 74 20 61 6e 20 65 72 72 6f  r or not an erro
1af36 72 20 6f 63 63 75 72 65 64 20 74 6f 20 65 6e 73  r occured to ens
1af37 75 72 65 20 61 6e 79 0a 20 20 2a 2a 20 64 79 6e  ure any.  ** dyn
1af38 61 6d 69 63 20 61 6c 6c 6f 63 61 74 69 6f 6e 20  amic allocation 
1af39 69 6e 20 73 43 6f 6e 74 65 78 74 2e 73 20 28 61  in sContext.s (a
1af3a 20 4d 65 6d 20 73 74 72 75 63 74 29 20 69 73 20   Mem struct) is 
1af3b 20 72 65 6c 65 61 73 65 64 2e 0a 20 20 2a 2f 0a   released..  */.
1af3c 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1af3d 6e 67 65 45 6e 63 6f 64 69 6e 67 28 26 73 43 6f  ngeEncoding(&sCo
1af3e 6e 74 65 78 74 2e 73 2c 20 65 6e 63 6f 64 69 6e  ntext.s, encodin
1af3f 67 29 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54  g);.  REGISTER_T
1af40 52 41 43 45 28 70 4f 70 2d 3e 70 33 2c 20 70 44  RACE(pOp->p3, pD
1af41 65 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  est);.  sqlite3V
1af42 64 62 65 4d 65 6d 4d 6f 76 65 28 70 44 65 73 74  dbeMemMove(pDest
1af43 2c 20 26 73 43 6f 6e 74 65 78 74 2e 73 29 3b 0a  , &sContext.s);.
1af44 20 20 55 50 44 41 54 45 5f 4d 41 58 5f 42 4c 4f    UPDATE_MAX_BLO
1af45 42 53 49 5a 45 28 70 44 65 73 74 29 3b 0a 0a 20  BSIZE(pDest);.. 
1af46 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
1af47 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
1af48 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f 74  goto abort_due_t
1af49 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 7d 0a 20 20  o_misuse;.  }.  
1af4a 69 66 28 20 73 71 6c 69 74 65 33 56 64 62 65 4d  if( sqlite3VdbeM
1af4b 65 6d 54 6f 6f 42 69 67 28 70 44 65 73 74 29 20  emTooBig(pDest) 
1af4c 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 6f 6f 5f  ){.    goto too_
1af4d 62 69 67 3b 0a 20 20 7d 0a 20 20 62 72 65 61 6b  big;.  }.  break
1af4e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
1af4f 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1af50 4c 54 41 42 4c 45 20 2a 2f 0a 0a 23 69 66 6e 64  LTABLE */..#ifnd
1af51 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1af52 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 20 4f  IRTUALTABLE./* O
1af53 70 63 6f 64 65 3a 20 56 4e 65 78 74 20 50 31 20  pcode: VNext P1 
1af54 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 41  P2 * * *.**.** A
1af55 64 76 61 6e 63 65 20 76 69 72 74 75 61 6c 20 74  dvance virtual t
1af56 61 62 6c 65 20 50 31 20 74 6f 20 74 68 65 20 6e  able P1 to the n
1af57 65 78 74 20 72 6f 77 20 69 6e 20 69 74 73 20 72  ext row in its r
1af58 65 73 75 6c 74 20 73 65 74 20 61 6e 64 0a 2a 2a  esult set and.**
1af59 20 6a 75 6d 70 20 74 6f 20 69 6e 73 74 72 75 63   jump to instruc
1af5a 74 69 6f 6e 20 50 32 2e 20 20 4f 72 2c 20 69 66  tion P2.  Or, if
1af5b 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
1af5c 6c 65 20 68 61 73 20 72 65 61 63 68 65 64 0a 2a  le has reached.*
1af5d 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 69 74 73  * the end of its
1af5e 20 72 65 73 75 6c 74 20 73 65 74 2c 20 74 68 65   result set, the
1af5f 6e 20 66 61 6c 6c 20 74 68 72 6f 75 67 68 20 74  n fall through t
1af60 6f 20 74 68 65 20 6e 65 78 74 20 69 6e 73 74 72  o the next instr
1af61 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 63 61 73 65 20  uction..*/.case 
1af62 4f 50 5f 56 4e 65 78 74 3a 20 7b 20 20 20 2f 2a  OP_VNext: {   /*
1af63 20 6a 75 6d 70 20 2a 2f 0a 20 20 73 71 6c 69 74   jump */.  sqlit
1af64 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a  e3_vtab *pVtab;.
1af65 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
1af66 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 3b  module *pModule;
1af67 0a 20 20 69 6e 74 20 72 65 73 20 3d 20 30 3b 0a  .  int res = 0;.
1af68 0a 20 20 56 64 62 65 43 75 72 73 6f 72 20 2a 70  .  VdbeCursor *p
1af69 43 75 72 20 3d 20 70 2d 3e 61 70 43 73 72 5b 70  Cur = p->apCsr[p
1af6a 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61 73 73 65 72  Op->p1];.  asser
1af6b 74 28 20 70 43 75 72 2d 3e 70 56 74 61 62 43 75  t( pCur->pVtabCu
1af6c 72 73 6f 72 20 29 3b 0a 20 20 69 66 28 20 70 43  rsor );.  if( pC
1af6d 75 72 2d 3e 6e 75 6c 6c 52 6f 77 20 29 7b 0a 20  ur->nullRow ){. 
1af6e 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20     break;.  }.  
1af6f 70 56 74 61 62 20 3d 20 70 43 75 72 2d 3e 70 56  pVtab = pCur->pV
1af70 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74 61 62  tabCursor->pVtab
1af71 3b 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56  ;.  pModule = pV
1af72 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20  tab->pModule;.  
1af73 61 73 73 65 72 74 28 20 70 4d 6f 64 75 6c 65 2d  assert( pModule-
1af74 3e 78 4e 65 78 74 20 29 3b 0a 0a 20 20 2f 2a 20  >xNext );..  /* 
1af75 49 6e 76 6f 6b 65 20 74 68 65 20 78 4e 65 78 74  Invoke the xNext
1af76 28 29 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65  () method of the
1af77 20 6d 6f 64 75 6c 65 2e 20 54 68 65 72 65 20 69   module. There i
1af78 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 74 68 65  s no way for the
1af79 0a 20 20 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67  .  ** underlying
1af7a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
1af7b 74 6f 20 72 65 74 75 72 6e 20 61 6e 20 65 72 72  to return an err
1af7c 6f 72 20 69 66 20 6f 6e 65 20 6f 63 63 75 72 73  or if one occurs
1af7d 20 64 75 72 69 6e 67 0a 20 20 2a 2a 20 78 4e 65   during.  ** xNe
1af7e 78 74 28 29 2e 20 49 6e 73 74 65 61 64 2c 20 69  xt(). Instead, i
1af7f 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
1af80 73 2c 20 74 72 75 65 20 69 73 20 72 65 74 75 72  s, true is retur
1af81 6e 65 64 20 28 69 6e 64 69 63 61 74 69 6e 67 20  ned (indicating 
1af82 74 68 61 74 20 0a 20 20 2a 2a 20 64 61 74 61 20  that .  ** data 
1af83 69 73 20 61 76 61 69 6c 61 62 6c 65 29 20 61 6e  is available) an
1af84 64 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64 65  d the error code
1af85 20 72 65 74 75 72 6e 65 64 20 77 68 65 6e 20 78   returned when x
1af86 43 6f 6c 75 6d 6e 20 6f 72 0a 20 20 2a 2a 20 73  Column or.  ** s
1af87 6f 6d 65 20 6f 74 68 65 72 20 6d 65 74 68 6f 64  ome other method
1af88 20 69 73 20 6e 65 78 74 20 69 6e 76 6f 6b 65 64   is next invoked
1af89 20 6f 6e 20 74 68 65 20 73 61 76 65 20 76 69 72   on the save vir
1af8a 74 75 61 6c 20 74 61 62 6c 65 20 63 75 72 73 6f  tual table curso
1af8b 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71  r..  */.  if( sq
1af8c 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1af8d 62 29 20 29 20 67 6f 74 6f 20 61 62 6f 72 74 5f  b) ) goto abort_
1af8e 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65 3b 0a 20  due_to_misuse;. 
1af8f 20 73 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b   sqlite3VtabLock
1af90 28 70 56 74 61 62 29 3b 0a 20 20 70 2d 3e 69 6e  (pVtab);.  p->in
1af91 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 31 3b 0a  VtabMethod = 1;.
1af92 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
1af93 78 4e 65 78 74 28 70 43 75 72 2d 3e 70 56 74 61  xNext(pCur->pVta
1af94 62 43 75 72 73 6f 72 29 3b 0a 20 20 70 2d 3e 69  bCursor);.  p->i
1af95 6e 56 74 61 62 4d 65 74 68 6f 64 20 3d 20 30 3b  nVtabMethod = 0;
1af96 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1af97 28 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29  (db, p->zErrMsg)
1af98 3b 0a 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20 3d  ;.  p->zErrMsg =
1af99 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 3b   pVtab->zErrMsg;
1af9a 0a 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73  .  pVtab->zErrMs
1af9b 67 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  g = 0;.  sqlite3
1af9c 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70  VtabUnlock(db, p
1af9d 56 74 61 62 29 3b 0a 20 20 69 66 28 20 72 63 3d  Vtab);.  if( rc=
1af9e 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
1af9f 20 20 72 65 73 20 3d 20 70 4d 6f 64 75 6c 65 2d    res = pModule-
1afa0 3e 78 45 6f 66 28 70 43 75 72 2d 3e 70 56 74 61  >xEof(pCur->pVta
1afa1 62 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20 20  bCursor);.  }.  
1afa2 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1afa3 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61  yOn(db) ) goto a
1afa4 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1afa5 73 65 3b 0a 0a 20 20 69 66 28 20 21 72 65 73 20  se;..  if( !res 
1afa6 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65  ){.    /* If the
1afa7 72 65 20 69 73 20 64 61 74 61 2c 20 6a 75 6d 70  re is data, jump
1afa8 20 74 6f 20 50 32 20 2a 2f 0a 20 20 20 20 70 63   to P2 */.    pc
1afa9 20 3d 20 70 4f 70 2d 3e 70 32 20 2d 20 31 3b 0a   = pOp->p2 - 1;.
1afaa 20 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23    }.  break;.}.#
1afab 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
1afac 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1afad 45 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51  E */..#ifndef SQ
1afae 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1afaf 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1afb0 3a 20 56 52 65 6e 61 6d 65 20 50 31 20 2a 20 2a  : VRename P1 * *
1afb1 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34 20 69   P4 *.**.** P4 i
1afb2 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
1afb3 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6f   virtual table o
1afb4 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69 74 65  bject, an sqlite
1afb5 33 5f 76 74 61 62 20 73 74 72 75 63 74 75 72 65  3_vtab structure
1afb6 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65  ..** This opcode
1afb7 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63 6f 72   invokes the cor
1afb8 72 65 73 70 6f 6e 64 69 6e 67 20 78 52 65 6e 61  responding xRena
1afb9 6d 65 20 6d 65 74 68 6f 64 2e 20 54 68 65 20 76  me method. The v
1afba 61 6c 75 65 0a 2a 2a 20 69 6e 20 72 65 67 69 73  alue.** in regis
1afbb 74 65 72 20 50 31 20 69 73 20 70 61 73 73 65 64  ter P1 is passed
1afbc 20 61 73 20 74 68 65 20 7a 4e 61 6d 65 20 61 72   as the zName ar
1afbd 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 78 52  gument to the xR
1afbe 65 6e 61 6d 65 20 6d 65 74 68 6f 64 2e 0a 2a 2f  ename method..*/
1afbf 0a 63 61 73 65 20 4f 50 5f 56 52 65 6e 61 6d 65  .case OP_VRename
1afc0 3a 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  : {.  sqlite3_vt
1afc1 61 62 20 2a 70 56 74 61 62 20 3d 20 70 4f 70 2d  ab *pVtab = pOp-
1afc2 3e 70 34 2e 70 56 74 61 62 3b 0a 20 20 4d 65 6d  >p4.pVtab;.  Mem
1afc3 20 2a 70 4e 61 6d 65 20 3d 20 26 70 2d 3e 61 4d   *pName = &p->aM
1afc4 65 6d 5b 70 4f 70 2d 3e 70 31 5d 3b 0a 20 20 61  em[pOp->p1];.  a
1afc5 73 73 65 72 74 28 20 70 56 74 61 62 2d 3e 70 4d  ssert( pVtab->pM
1afc6 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20 29  odule->xRename )
1afc7 3b 0a 20 20 52 45 47 49 53 54 45 52 5f 54 52 41  ;.  REGISTER_TRA
1afc8 43 45 28 70 4f 70 2d 3e 70 31 2c 20 70 4e 61 6d  CE(pOp->p1, pNam
1afc9 65 29 3b 0a 0a 20 20 53 74 72 69 6e 67 69 66 79  e);..  Stringify
1afca 28 70 4e 61 6d 65 2c 20 65 6e 63 6f 64 69 6e 67  (pName, encoding
1afcb 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65  );..  if( sqlite
1afcc 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1afcd 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1afce 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 73 71 6c  to_misuse;.  sql
1afcf 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70 56 74  ite3VtabLock(pVt
1afd0 61 62 29 3b 0a 20 20 72 63 20 3d 20 70 56 74 61  ab);.  rc = pVta
1afd1 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e  b->pModule->xRen
1afd2 61 6d 65 28 70 56 74 61 62 2c 20 70 4e 61 6d 65  ame(pVtab, pName
1afd3 2d 3e 7a 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ->z);.  sqlite3D
1afd4 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 7a 45 72  bFree(db, p->zEr
1afd5 72 4d 73 67 29 3b 0a 20 20 70 2d 3e 7a 45 72 72  rMsg);.  p->zErr
1afd6 4d 73 67 20 3d 20 70 56 74 61 62 2d 3e 7a 45 72  Msg = pVtab->zEr
1afd7 72 4d 73 67 3b 0a 20 20 70 56 74 61 62 2d 3e 7a  rMsg;.  pVtab->z
1afd8 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 73 71  ErrMsg = 0;.  sq
1afd9 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
1afda 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 69 66  db, pVtab);.  if
1afdb 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  ( sqlite3SafetyO
1afdc 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61 62 6f  n(db) ) goto abo
1afdd 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75 73 65  rt_due_to_misuse
1afde 3b 0a 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65  ;..  break;.}.#e
1afdf 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51  ndif..#ifndef SQ
1afe0 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1afe1 4c 54 41 42 4c 45 0a 2f 2a 20 4f 70 63 6f 64 65  LTABLE./* Opcode
1afe2 3a 20 56 55 70 64 61 74 65 20 50 31 20 50 32 20  : VUpdate P1 P2 
1afe3 50 33 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 50 34  P3 P4 *.**.** P4
1afe4 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
1afe5 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1afe6 20 6f 62 6a 65 63 74 2c 20 61 6e 20 73 71 6c 69   object, an sqli
1afe7 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
1afe8 72 65 2e 0a 2a 2a 20 54 68 69 73 20 6f 70 63 6f  re..** This opco
1afe9 64 65 20 69 6e 76 6f 6b 65 73 20 74 68 65 20 63  de invokes the c
1afea 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20 78 55 70  orresponding xUp
1afeb 64 61 74 65 20 6d 65 74 68 6f 64 2e 20 50 32 20  date method. P2 
1afec 76 61 6c 75 65 73 0a 2a 2a 20 61 72 65 20 63 6f  values.** are co
1afed 6e 74 69 67 75 6f 75 73 20 6d 65 6d 6f 72 79 20  ntiguous memory 
1afee 63 65 6c 6c 73 20 73 74 61 72 74 69 6e 67 20 61  cells starting a
1afef 74 20 50 33 20 74 6f 20 70 61 73 73 20 74 6f 20  t P3 to pass to 
1aff0 74 68 65 20 78 55 70 64 61 74 65 20 0a 2a 2a 20  the xUpdate .** 
1aff1 69 6e 76 6f 63 61 74 69 6f 6e 2e 20 54 68 65 20  invocation. The 
1aff2 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1aff3 72 20 28 50 33 2b 50 32 2d 31 29 20 63 6f 72 72  r (P3+P2-1) corr
1aff4 65 73 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 0a  esponds to the .
1aff5 2a 2a 20 70 32 74 68 20 65 6c 65 6d 65 6e 74 20  ** p2th element 
1aff6 6f 66 20 74 68 65 20 61 72 67 76 20 61 72 72 61  of the argv arra
1aff7 79 20 70 61 73 73 65 64 20 74 6f 20 78 55 70 64  y passed to xUpd
1aff8 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 78  ate..**.** The x
1aff9 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 77 69  Update method wi
1affa 6c 6c 20 64 6f 20 61 20 44 45 4c 45 54 45 20 6f  ll do a DELETE o
1affb 72 20 61 6e 20 49 4e 53 45 52 54 20 6f 72 20 62  r an INSERT or b
1affc 6f 74 68 2e 0a 2a 2a 20 54 68 65 20 61 72 67 76  oth..** The argv
1affd 5b 30 5d 20 65 6c 65 6d 65 6e 74 20 28 77 68 69  [0] element (whi
1affe 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73 20 74  ch corresponds t
1afff 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50 33  o memory cell P3
1b000 29 0a 2a 2a 20 69 73 20 74 68 65 20 72 6f 77 69  ).** is the rowi
1b001 64 20 6f 66 20 61 20 72 6f 77 20 74 6f 20 64 65  d of a row to de
1b002 6c 65 74 65 2e 20 20 49 66 20 61 72 67 76 5b 30  lete.  If argv[0
1b003 5d 20 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 6e  ] is NULL then n
1b004 6f 20 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 6f  o .** deletion o
1b005 63 63 75 72 73 2e 20 20 54 68 65 20 61 72 67 76  ccurs.  The argv
1b006 5b 31 5d 20 65 6c 65 6d 65 6e 74 20 69 73 20 74  [1] element is t
1b007 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20  he rowid of the 
1b008 6e 65 77 20 0a 2a 2a 20 72 6f 77 2e 20 20 54 68  new .** row.  Th
1b009 69 73 20 63 61 6e 20 62 65 20 4e 55 4c 4c 20 74  is can be NULL t
1b00a 6f 20 68 61 76 65 20 74 68 65 20 76 69 72 74 75  o have the virtu
1b00b 61 6c 20 74 61 62 6c 65 20 73 65 6c 65 63 74 20  al table select 
1b00c 74 68 65 20 6e 65 77 20 0a 2a 2a 20 72 6f 77 69  the new .** rowi
1b00d 64 20 66 6f 72 20 69 74 73 65 6c 66 2e 20 20 54  d for itself.  T
1b00e 68 65 20 73 75 62 73 65 71 75 65 6e 74 20 65 6c  he subsequent el
1b00f 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 61 72  ements in the ar
1b010 72 61 79 20 61 72 65 20 0a 2a 2a 20 74 68 65 20  ray are .** the 
1b011 76 61 6c 75 65 73 20 6f 66 20 63 6f 6c 75 6d 6e  values of column
1b012 73 20 69 6e 20 74 68 65 20 6e 65 77 20 72 6f 77  s in the new row
1b013 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 50 32 3d 3d 31  ..**.** If P2==1
1b014 20 74 68 65 6e 20 6e 6f 20 69 6e 73 65 72 74 20   then no insert 
1b015 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 20 20 61  is performed.  a
1b016 72 67 76 5b 30 5d 20 69 73 20 74 68 65 20 72 6f  rgv[0] is the ro
1b017 77 69 64 20 6f 66 0a 2a 2a 20 61 20 72 6f 77 20  wid of.** a row 
1b018 74 6f 20 64 65 6c 65 74 65 2e 0a 2a 2a 0a 2a 2a  to delete..**.**
1b019 20 50 31 20 69 73 20 61 20 62 6f 6f 6c 65 61 6e   P1 is a boolean
1b01a 20 66 6c 61 67 2e 20 49 66 20 69 74 20 69 73 20   flag. If it is 
1b01b 73 65 74 20 74 6f 20 74 72 75 65 20 61 6e 64 20  set to true and 
1b01c 74 68 65 20 78 55 70 64 61 74 65 20 63 61 6c 6c  the xUpdate call
1b01d 0a 2a 2a 20 69 73 20 73 75 63 63 65 73 73 66 75  .** is successfu
1b01e 6c 2c 20 74 68 65 6e 20 74 68 65 20 76 61 6c 75  l, then the valu
1b01f 65 20 72 65 74 75 72 6e 65 64 20 62 79 20 73 71  e returned by sq
1b020 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72  lite3_last_inser
1b021 74 5f 72 6f 77 69 64 28 29 20 0a 2a 2a 20 69 73  t_rowid() .** is
1b022 20 73 65 74 20 74 6f 20 74 68 65 20 76 61 6c 75   set to the valu
1b023 65 20 6f 66 20 74 68 65 20 72 6f 77 69 64 20 66  e of the rowid f
1b024 6f 72 20 74 68 65 20 72 6f 77 20 6a 75 73 74 20  or the row just 
1b025 69 6e 73 65 72 74 65 64 2e 0a 2a 2f 0a 63 61 73  inserted..*/.cas
1b026 65 20 4f 50 5f 56 55 70 64 61 74 65 3a 20 7b 0a  e OP_VUpdate: {.
1b027 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
1b028 70 56 74 61 62 20 3d 20 70 4f 70 2d 3e 70 34 2e  pVtab = pOp->p4.
1b029 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVtab;.  sqlite3
1b02a 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65  _module *pModule
1b02b 20 3d 20 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75   = (sqlite3_modu
1b02c 6c 65 20 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64  le *)pVtab->pMod
1b02d 75 6c 65 3b 0a 20 20 69 6e 74 20 6e 41 72 67 20  ule;.  int nArg 
1b02e 3d 20 70 4f 70 2d 3e 70 32 3b 0a 20 20 61 73 73  = pOp->p2;.  ass
1b02f 65 72 74 28 20 70 4f 70 2d 3e 70 34 74 79 70 65  ert( pOp->p4type
1b030 3d 3d 50 34 5f 56 54 41 42 20 29 3b 0a 20 20 69  ==P4_VTAB );.  i
1b031 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64  f( pModule->xUpd
1b032 61 74 65 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  ate==0 ){.    sq
1b033 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 26  lite3SetString(&
1b034 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62 2c 20  p->zErrMsg, db, 
1b035 22 72 65 61 64 2d 6f 6e 6c 79 20 74 61 62 6c 65  "read-only table
1b036 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c  ");.    rc = SQL
1b037 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 65 6c  ITE_ERROR;.  }el
1b038 73 65 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  se{.    int i;. 
1b039 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
1b03a 72 6f 77 69 64 3b 0a 20 20 20 20 4d 65 6d 20 2a  rowid;.    Mem *
1b03b 2a 61 70 41 72 67 20 3d 20 70 2d 3e 61 70 41 72  *apArg = p->apAr
1b03c 67 3b 0a 20 20 20 20 4d 65 6d 20 2a 70 58 20 3d  g;.    Mem *pX =
1b03d 20 26 70 2d 3e 61 4d 65 6d 5b 70 4f 70 2d 3e 70   &p->aMem[pOp->p
1b03e 33 5d 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  3];.    for(i=0;
1b03f 20 69 3c 6e 41 72 67 3b 20 69 2b 2b 29 7b 0a 20   i<nArg; i++){. 
1b040 20 20 20 20 20 73 74 6f 72 65 54 79 70 65 49 6e       storeTypeIn
1b041 66 6f 28 70 58 2c 20 30 29 3b 0a 20 20 20 20 20  fo(pX, 0);.     
1b042 20 61 70 41 72 67 5b 69 5d 20 3d 20 70 58 3b 0a   apArg[i] = pX;.
1b043 20 20 20 20 20 20 70 58 2b 2b 3b 0a 20 20 20 20        pX++;.    
1b044 7d 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  }.    if( sqlite
1b045 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 20 29  3SafetyOff(db) )
1b046 20 67 6f 74 6f 20 61 62 6f 72 74 5f 64 75 65 5f   goto abort_due_
1b047 74 6f 5f 6d 69 73 75 73 65 3b 0a 20 20 20 20 73  to_misuse;.    s
1b048 71 6c 69 74 65 33 56 74 61 62 4c 6f 63 6b 28 70  qlite3VtabLock(p
1b049 56 74 61 62 29 3b 0a 20 20 20 20 72 63 20 3d 20  Vtab);.    rc = 
1b04a 70 4d 6f 64 75 6c 65 2d 3e 78 55 70 64 61 74 65  pModule->xUpdate
1b04b 28 70 56 74 61 62 2c 20 6e 41 72 67 2c 20 61 70  (pVtab, nArg, ap
1b04c 41 72 67 2c 20 26 72 6f 77 69 64 29 3b 0a 20 20  Arg, &rowid);.  
1b04d 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1b04e 64 62 2c 20 70 2d 3e 7a 45 72 72 4d 73 67 29 3b  db, p->zErrMsg);
1b04f 0a 20 20 20 20 70 2d 3e 7a 45 72 72 4d 73 67 20  .    p->zErrMsg 
1b050 3d 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  = pVtab->zErrMsg
1b051 3b 0a 20 20 20 20 70 56 74 61 62 2d 3e 7a 45 72  ;.    pVtab->zEr
1b052 72 4d 73 67 20 3d 20 30 3b 0a 20 20 20 20 73 71  rMsg = 0;.    sq
1b053 6c 69 74 65 33 56 74 61 62 55 6e 6c 6f 63 6b 28  lite3VtabUnlock(
1b054 64 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20  db, pVtab);.    
1b055 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74  if( sqlite3Safet
1b056 79 4f 6e 28 64 62 29 20 29 20 67 6f 74 6f 20 61  yOn(db) ) goto a
1b057 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1b058 73 65 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d  se;.    if( pOp-
1b059 3e 70 31 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  >p1 && rc==SQLIT
1b05a 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 61 73  E_OK ){.      as
1b05b 73 65 72 74 28 20 6e 41 72 67 3e 31 20 26 26 20  sert( nArg>1 && 
1b05c 61 70 41 72 67 5b 30 5d 20 26 26 20 28 61 70 41  apArg[0] && (apA
1b05d 72 67 5b 30 5d 2d 3e 66 6c 61 67 73 26 4d 45 4d  rg[0]->flags&MEM
1b05e 5f 4e 75 6c 6c 29 20 29 3b 0a 20 20 20 20 20 20  _Null) );.      
1b05f 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 20 3d 20  db->lastRowid = 
1b060 72 6f 77 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20  rowid;.    }.   
1b061 20 70 2d 3e 6e 43 68 61 6e 67 65 2b 2b 3b 0a 20   p->nChange++;. 
1b062 20 7d 0a 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65   }.  break;.}.#e
1b063 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
1b064 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
1b065 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 20 53 51   */..#ifndef  SQ
1b066 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45 52 5f  LITE_OMIT_PAGER_
1b067 50 52 41 47 4d 41 53 0a 2f 2a 20 4f 70 63 6f 64  PRAGMAS./* Opcod
1b068 65 3a 20 50 61 67 65 63 6f 75 6e 74 20 50 31 20  e: Pagecount P1 
1b069 50 32 20 2a 20 2a 20 2a 0a 2a 2a 0a 2a 2a 20 57  P2 * * *.**.** W
1b06a 72 69 74 65 20 74 68 65 20 63 75 72 72 65 6e 74  rite the current
1b06b 20 6e 75 6d 62 65 72 20 6f 66 20 70 61 67 65 73   number of pages
1b06c 20 69 6e 20 64 61 74 61 62 61 73 65 20 50 31 20   in database P1 
1b06d 74 6f 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 50  to memory cell P
1b06e 32 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 50 61  2..*/.case OP_Pa
1b06f 67 65 63 6f 75 6e 74 3a 20 7b 20 20 20 20 20 20  gecount: {      
1b070 20 20 20 20 20 20 2f 2a 20 6f 75 74 32 2d 70 72        /* out2-pr
1b071 65 72 65 6c 65 61 73 65 20 2a 2f 0a 20 20 69 6e  erelease */.  in
1b072 74 20 70 31 20 3d 20 70 4f 70 2d 3e 70 31 3b 20  t p1 = pOp->p1; 
1b073 0a 20 20 69 6e 74 20 6e 50 61 67 65 3b 0a 20 20  .  int nPage;.  
1b074 50 61 67 65 72 20 2a 70 50 61 67 65 72 20 3d 20  Pager *pPager = 
1b075 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
1b076 72 28 64 62 2d 3e 61 44 62 5b 70 31 5d 2e 70 42  r(db->aDb[p1].pB
1b077 74 29 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  t);..  rc = sqli
1b078 74 65 33 50 61 67 65 72 50 61 67 65 63 6f 75 6e  te3PagerPagecoun
1b079 74 28 70 50 61 67 65 72 2c 20 26 6e 50 61 67 65  t(pPager, &nPage
1b07a 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1b07b 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 4f  ITE_OK ){.    pO
1b07c 75 74 2d 3e 66 6c 61 67 73 20 3d 20 4d 45 4d 5f  ut->flags = MEM_
1b07d 49 6e 74 3b 0a 20 20 20 20 70 4f 75 74 2d 3e 75  Int;.    pOut->u
1b07e 2e 69 20 3d 20 6e 50 61 67 65 3b 0a 20 20 7d 0a  .i = nPage;.  }.
1b07f 20 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69    break;.}.#endi
1b080 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  f..#ifndef SQLIT
1b081 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a 20  E_OMIT_TRACE./* 
1b082 4f 70 63 6f 64 65 3a 20 54 72 61 63 65 20 2a 20  Opcode: Trace * 
1b083 2a 20 2a 20 50 34 20 2a 0a 2a 2a 0a 2a 2a 20 49  * * P4 *.**.** I
1b084 66 20 74 72 61 63 69 6e 67 20 69 73 20 65 6e 61  f tracing is ena
1b085 62 6c 65 64 20 28 62 79 20 74 68 65 20 73 71 6c  bled (by the sql
1b086 69 74 65 33 5f 74 72 61 63 65 28 29 29 20 69 6e  ite3_trace()) in
1b087 74 65 72 66 61 63 65 2c 20 74 68 65 6e 0a 2a 2a  terface, then.**
1b088 20 74 68 65 20 55 54 46 2d 38 20 73 74 72 69 6e   the UTF-8 strin
1b089 67 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 50  g contained in P
1b08a 34 20 69 73 20 65 6d 69 74 74 65 64 20 6f 6e 20  4 is emitted on 
1b08b 74 68 65 20 74 72 61 63 65 20 63 61 6c 6c 62 61  the trace callba
1b08c 63 6b 2e 0a 2a 2f 0a 63 61 73 65 20 4f 50 5f 54  ck..*/.case OP_T
1b08d 72 61 63 65 3a 20 7b 0a 20 20 69 66 28 20 70 4f  race: {.  if( pO
1b08e 70 2d 3e 70 34 2e 7a 20 29 7b 0a 20 20 20 20 69  p->p4.z ){.    i
1b08f 66 28 20 64 62 2d 3e 78 54 72 61 63 65 20 29 7b  f( db->xTrace ){
1b090 0a 20 20 20 20 20 20 64 62 2d 3e 78 54 72 61 63  .      db->xTrac
1b091 65 28 64 62 2d 3e 70 54 72 61 63 65 41 72 67 2c  e(db->pTraceArg,
1b092 20 70 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20   pOp->p4.z);.   
1b093 20 7d 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45   }.#ifdef SQLITE
1b094 5f 44 45 42 55 47 0a 20 20 20 20 69 66 28 20 28  _DEBUG.    if( (
1b095 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1b096 54 45 5f 53 71 6c 54 72 61 63 65 29 21 3d 30 20  TE_SqlTrace)!=0 
1b097 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
1b098 44 65 62 75 67 50 72 69 6e 74 66 28 22 53 51 4c  DebugPrintf("SQL
1b099 2d 74 72 61 63 65 3a 20 25 73 5c 6e 22 2c 20 70  -trace: %s\n", p
1b09a 4f 70 2d 3e 70 34 2e 7a 29 3b 0a 20 20 20 20 7d  Op->p4.z);.    }
1b09b 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1b09c 45 5f 44 45 42 55 47 20 2a 2f 0a 20 20 7d 0a 20  E_DEBUG */.  }. 
1b09d 20 62 72 65 61 6b 3b 0a 7d 0a 23 65 6e 64 69 66   break;.}.#endif
1b09e 0a 0a 0a 2f 2a 20 4f 70 63 6f 64 65 3a 20 4e 6f  .../* Opcode: No
1b09f 6f 70 20 2a 20 2a 20 2a 20 2a 20 2a 0a 2a 2a 0a  op * * * * *.**.
1b0a0 2a 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 2e 20 20  ** Do nothing.  
1b0a1 54 68 69 73 20 69 6e 73 74 72 75 63 74 69 6f 6e  This instruction
1b0a2 20 69 73 20 6f 66 74 65 6e 20 75 73 65 66 75 6c   is often useful
1b0a3 20 61 73 20 61 20 6a 75 6d 70 0a 2a 2a 20 64 65   as a jump.** de
1b0a4 73 74 69 6e 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  stination..*/./*
1b0a5 0a 2a 2a 20 54 68 65 20 6d 61 67 69 63 20 45 78  .** The magic Ex
1b0a6 70 6c 61 69 6e 20 6f 70 63 6f 64 65 20 61 72 65  plain opcode are
1b0a7 20 6f 6e 6c 79 20 69 6e 73 65 72 74 65 64 20 77   only inserted w
1b0a8 68 65 6e 20 65 78 70 6c 61 69 6e 3d 3d 32 20 28  hen explain==2 (
1b0a9 77 68 69 63 68 0a 2a 2a 20 69 73 20 74 6f 20 73  which.** is to s
1b0aa 61 79 20 77 68 65 6e 20 74 68 65 20 45 58 50 4c  ay when the EXPL
1b0ab 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 73  AIN QUERY PLAN s
1b0ac 79 6e 74 61 78 20 69 73 20 75 73 65 64 2e 29 0a  yntax is used.).
1b0ad 2a 2a 20 54 68 69 73 20 6f 70 63 6f 64 65 20 72  ** This opcode r
1b0ae 65 63 6f 72 64 73 20 69 6e 66 6f 72 6d 61 74 69  ecords informati
1b0af 6f 6e 20 66 72 6f 6d 20 74 68 65 20 6f 70 74 69  on from the opti
1b0b0 6d 69 7a 65 72 2e 20 20 49 74 20 69 73 20 74 68  mizer.  It is th
1b0b1 65 0a 2a 2a 20 74 68 65 20 73 61 6d 65 20 61 73  e.** the same as
1b0b2 20 61 20 6e 6f 2d 6f 70 2e 20 20 54 68 69 73 20   a no-op.  This 
1b0b3 6f 70 63 6f 64 65 73 6e 65 76 65 72 20 61 70 70  opcodesnever app
1b0b4 65 61 72 73 20 69 6e 20 61 20 72 65 61 6c 20 56  ears in a real V
1b0b5 4d 20 70 72 6f 67 72 61 6d 2e 0a 2a 2f 0a 64 65  M program..*/.de
1b0b6 66 61 75 6c 74 3a 20 7b 20 20 20 20 20 20 20 20  fault: {        
1b0b7 20 20 2f 2a 20 54 68 69 73 20 69 73 20 72 65 61    /* This is rea
1b0b8 6c 6c 79 20 4f 50 5f 4e 6f 6f 70 20 61 6e 64 20  lly OP_Noop and 
1b0b9 4f 50 5f 45 78 70 6c 61 69 6e 20 2a 2f 0a 20 20  OP_Explain */.  
1b0ba 62 72 65 61 6b 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  break;.}../*****
1b0bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0bd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0be 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0bf 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 65 20  ********.** The 
1b0c0 63 61 73 65 73 20 6f 66 20 74 68 65 20 73 77 69  cases of the swi
1b0c1 74 63 68 20 73 74 61 74 65 6d 65 6e 74 20 61 62  tch statement ab
1b0c2 6f 76 65 20 74 68 69 73 20 6c 69 6e 65 20 73 68  ove this line sh
1b0c3 6f 75 6c 64 20 61 6c 6c 20 62 65 20 69 6e 64 65  ould all be inde
1b0c4 6e 74 65 64 0a 2a 2a 20 62 79 20 36 20 73 70 61  nted.** by 6 spa
1b0c5 63 65 73 2e 20 20 42 75 74 20 74 68 65 20 6c 65  ces.  But the le
1b0c6 66 74 2d 6d 6f 73 74 20 36 20 73 70 61 63 65 73  ft-most 6 spaces
1b0c7 20 68 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76   have been remov
1b0c8 65 64 20 74 6f 20 69 6d 70 72 6f 76 65 20 74 68  ed to improve th
1b0c9 65 0a 2a 2a 20 72 65 61 64 61 62 69 6c 69 74 79  e.** readability
1b0ca 2e 20 20 46 72 6f 6d 20 74 68 69 73 20 70 6f 69  .  From this poi
1b0cb 6e 74 20 6f 6e 20 64 6f 77 6e 2c 20 74 68 65 20  nt on down, the 
1b0cc 6e 6f 72 6d 61 6c 20 69 6e 64 65 6e 74 61 74 69  normal indentati
1b0cd 6f 6e 20 72 75 6c 65 73 20 61 72 65 0a 2a 2a 20  on rules are.** 
1b0ce 72 65 73 74 6f 72 65 64 2e 0a 2a 2a 2a 2a 2a 2a  restored..******
1b0cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b0d3 2a 2a 2a 2a 2a 2a 2a 2f 0a 20 20 20 20 7d 0a 0a  *******/.    }..
1b0d4 23 69 66 64 65 66 20 56 44 42 45 5f 50 52 4f 46  #ifdef VDBE_PROF
1b0d5 49 4c 45 0a 20 20 20 20 7b 0a 20 20 20 20 20 20  ILE.    {.      
1b0d6 75 36 34 20 65 6c 61 70 73 65 64 20 3d 20 73 71  u64 elapsed = sq
1b0d7 6c 69 74 65 33 48 77 74 69 6d 65 28 29 20 2d 20  lite3Hwtime() - 
1b0d8 73 74 61 72 74 3b 0a 20 20 20 20 20 20 70 4f 70  start;.      pOp
1b0d9 2d 3e 63 79 63 6c 65 73 20 2b 3d 20 65 6c 61 70  ->cycles += elap
1b0da 73 65 64 3b 0a 20 20 20 20 20 20 70 4f 70 2d 3e  sed;.      pOp->
1b0db 63 6e 74 2b 2b 3b 0a 23 69 66 20 30 0a 20 20 20  cnt++;.#if 0.   
1b0dc 20 20 20 20 20 66 70 72 69 6e 74 66 28 73 74 64       fprintf(std
1b0dd 6f 75 74 2c 20 22 25 31 30 6c 6c 75 20 22 2c 20  out, "%10llu ", 
1b0de 65 6c 61 70 73 65 64 29 3b 0a 20 20 20 20 20 20  elapsed);.      
1b0df 20 20 73 71 6c 69 74 65 33 56 64 62 65 50 72 69    sqlite3VdbePri
1b0e0 6e 74 4f 70 28 73 74 64 6f 75 74 2c 20 6f 72 69  ntOp(stdout, ori
1b0e1 67 50 63 2c 20 26 70 2d 3e 61 4f 70 5b 6f 72 69  gPc, &p->aOp[ori
1b0e2 67 50 63 5d 29 3b 0a 23 65 6e 64 69 66 0a 20 20  gPc]);.#endif.  
1b0e3 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
1b0e4 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  /* The following
1b0e5 20 63 6f 64 65 20 61 64 64 73 20 6e 6f 74 68 69   code adds nothi
1b0e6 6e 67 20 74 6f 20 74 68 65 20 61 63 74 75 61 6c  ng to the actual
1b0e7 20 66 75 6e 63 74 69 6f 6e 61 6c 69 74 79 0a 20   functionality. 
1b0e8 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 70 72 6f     ** of the pro
1b0e9 67 72 61 6d 2e 20 20 49 74 20 69 73 20 6f 6e 6c  gram.  It is onl
1b0ea 79 20 68 65 72 65 20 66 6f 72 20 74 65 73 74 69  y here for testi
1b0eb 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e 67  ng and debugging
1b0ec 2e 0a 20 20 20 20 2a 2a 20 4f 6e 20 74 68 65 20  ..    ** On the 
1b0ed 6f 74 68 65 72 20 68 61 6e 64 2c 20 69 74 20 64  other hand, it d
1b0ee 6f 65 73 20 62 75 72 6e 20 43 50 55 20 63 79 63  oes burn CPU cyc
1b0ef 6c 65 73 20 65 76 65 72 79 20 74 69 6d 65 20 74  les every time t
1b0f0 68 72 6f 75 67 68 0a 20 20 20 20 2a 2a 20 74 68  hrough.    ** th
1b0f1 65 20 65 76 61 6c 75 61 74 6f 72 20 6c 6f 6f 70  e evaluator loop
1b0f2 2e 20 20 53 6f 20 77 65 20 63 61 6e 20 6c 65 61  .  So we can lea
1b0f3 76 65 20 69 74 20 6f 75 74 20 77 68 65 6e 20 4e  ve it out when N
1b0f4 44 45 42 55 47 20 69 73 20 64 65 66 69 6e 65 64  DEBUG is defined
1b0f5 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ..    */.#ifndef
1b0f6 20 4e 44 45 42 55 47 0a 20 20 20 20 61 73 73 65   NDEBUG.    asse
1b0f7 72 74 28 20 70 63 3e 3d 2d 31 20 26 26 20 70 63  rt( pc>=-1 && pc
1b0f8 3c 70 2d 3e 6e 4f 70 20 29 3b 0a 0a 23 69 66 64  <p->nOp );..#ifd
1b0f9 65 66 20 53 51 4c 49 54 45 5f 44 45 42 55 47 0a  ef SQLITE_DEBUG.
1b0fa 20 20 20 20 69 66 28 20 70 2d 3e 74 72 61 63 65      if( p->trace
1b0fb 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 72 63   ){.      if( rc
1b0fc 21 3d 30 20 29 20 66 70 72 69 6e 74 66 28 70 2d  !=0 ) fprintf(p-
1b0fd 3e 74 72 61 63 65 2c 22 72 63 3d 25 64 5c 6e 22  >trace,"rc=%d\n"
1b0fe 2c 72 63 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ,rc);.      if( 
1b0ff 6f 70 50 72 6f 70 65 72 74 79 20 26 20 4f 50 46  opProperty & OPF
1b100 4c 47 5f 4f 55 54 32 5f 50 52 45 52 45 4c 45 41  LG_OUT2_PRERELEA
1b101 53 45 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  SE ){.        re
1b102 67 69 73 74 65 72 54 72 61 63 65 28 70 2d 3e 74  gisterTrace(p->t
1b103 72 61 63 65 2c 20 70 4f 70 2d 3e 70 32 2c 20 70  race, pOp->p2, p
1b104 4f 75 74 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Out);.      }.  
1b105 20 20 20 20 69 66 28 20 6f 70 50 72 6f 70 65 72      if( opProper
1b106 74 79 20 26 20 4f 50 46 4c 47 5f 4f 55 54 33 20  ty & OPFLG_OUT3 
1b107 29 7b 0a 20 20 20 20 20 20 20 20 72 65 67 69 73  ){.        regis
1b108 74 65 72 54 72 61 63 65 28 70 2d 3e 74 72 61 63  terTrace(p->trac
1b109 65 2c 20 70 4f 70 2d 3e 70 33 2c 20 70 4f 75 74  e, pOp->p3, pOut
1b10a 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
1b10b 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51 4c 49  .#endif  /* SQLI
1b10c 54 45 5f 44 45 42 55 47 20 2a 2f 0a 23 65 6e 64  TE_DEBUG */.#end
1b10d 69 66 20 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  if  /* NDEBUG */
1b10e 0a 20 20 7d 20 20 2f 2a 20 54 68 65 20 65 6e 64  .  }  /* The end
1b10f 20 6f 66 20 74 68 65 20 66 6f 72 28 3b 3b 29 20   of the for(;;) 
1b110 6c 6f 6f 70 20 74 68 65 20 6c 6f 6f 70 73 20 74  loop the loops t
1b111 68 72 6f 75 67 68 20 6f 70 63 6f 64 65 73 20 2a  hrough opcodes *
1b112 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 72 65  /..  /* If we re
1b113 61 63 68 20 74 68 69 73 20 70 6f 69 6e 74 2c 20  ach this point, 
1b114 69 74 20 6d 65 61 6e 73 20 74 68 61 74 20 65 78  it means that ex
1b115 65 63 75 74 69 6f 6e 20 69 73 20 66 69 6e 69 73  ecution is finis
1b116 68 65 64 20 77 69 74 68 0a 20 20 2a 2a 20 61 6e  hed with.  ** an
1b117 20 65 72 72 6f 72 20 6f 66 20 73 6f 6d 65 20 6b   error of some k
1b118 69 6e 64 2e 0a 20 20 2a 2f 0a 76 64 62 65 5f 65  ind..  */.vdbe_e
1b119 72 72 6f 72 5f 68 61 6c 74 3a 0a 20 20 61 73 73  rror_halt:.  ass
1b11a 65 72 74 28 20 72 63 20 29 3b 0a 20 20 70 2d 3e  ert( rc );.  p->
1b11b 72 63 20 3d 20 72 63 3b 0a 20 20 73 71 6c 69 74  rc = rc;.  sqlit
1b11c 65 33 56 64 62 65 48 61 6c 74 28 70 29 3b 0a 20  e3VdbeHalt(p);. 
1b11d 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
1b11e 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 20 64 62  IOERR_NOMEM ) db
1b11f 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1b120 20 31 3b 0a 20 20 72 63 20 3d 20 53 51 4c 49 54   1;.  rc = SQLIT
1b121 45 5f 45 52 52 4f 52 3b 0a 0a 20 20 2f 2a 20 54  E_ERROR;..  /* T
1b122 68 69 73 20 69 73 20 74 68 65 20 6f 6e 6c 79 20  his is the only 
1b123 77 61 79 20 6f 75 74 20 6f 66 20 74 68 69 73 20  way out of this 
1b124 70 72 6f 63 65 64 75 72 65 2e 20 20 57 65 20 68  procedure.  We h
1b125 61 76 65 20 74 6f 0a 20 20 2a 2a 20 72 65 6c 65  ave to.  ** rele
1b126 61 73 65 20 74 68 65 20 6d 75 74 65 78 65 73 20  ase the mutexes 
1b127 6f 6e 20 62 74 72 65 65 73 20 74 68 61 74 20 77  on btrees that w
1b128 65 72 65 20 61 63 71 75 69 72 65 64 20 61 74 20  ere acquired at 
1b129 74 68 65 0a 20 20 2a 2a 20 74 6f 70 2e 20 2a 2f  the.  ** top. */
1b12a 0a 76 64 62 65 5f 72 65 74 75 72 6e 3a 0a 20 20  .vdbe_return:.  
1b12b 73 71 6c 69 74 65 33 42 74 72 65 65 4d 75 74 65  sqlite3BtreeMute
1b12c 78 41 72 72 61 79 4c 65 61 76 65 28 26 70 2d 3e  xArrayLeave(&p->
1b12d 61 4d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  aMutex);.  retur
1b12e 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  n rc;..  /* Jump
1b12f 20 74 6f 20 68 65 72 65 20 69 66 20 61 20 73 74   to here if a st
1b130 72 69 6e 67 20 6f 72 20 62 6c 6f 62 20 6c 61 72  ring or blob lar
1b131 67 65 72 20 74 68 61 6e 20 53 51 4c 49 54 45 5f  ger than SQLITE_
1b132 4d 41 58 5f 4c 45 4e 47 54 48 0a 20 20 2a 2a 20  MAX_LENGTH.  ** 
1b133 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64 2e 0a  is encountered..
1b134 20 20 2a 2f 0a 74 6f 6f 5f 62 69 67 3a 0a 20 20    */.too_big:.  
1b135 73 71 6c 69 74 65 33 53 65 74 53 74 72 69 6e 67  sqlite3SetString
1b136 28 26 70 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  (&p->zErrMsg, db
1b137 2c 20 22 73 74 72 69 6e 67 20 6f 72 20 62 6c 6f  , "string or blo
1b138 62 20 74 6f 6f 20 62 69 67 22 29 3b 0a 20 20 72  b too big");.  r
1b139 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  c = SQLITE_TOOBI
1b13a 47 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65  G;.  goto vdbe_e
1b13b 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1b13c 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69 66   Jump to here if
1b13d 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c   a malloc() fail
1b13e 73 2e 0a 20 20 2a 2f 0a 6e 6f 5f 6d 65 6d 3a 0a  s..  */.no_mem:.
1b13f 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1b140 65 64 20 3d 20 31 3b 0a 20 20 73 71 6c 69 74 65  ed = 1;.  sqlite
1b141 33 53 65 74 53 74 72 69 6e 67 28 26 70 2d 3e 7a  3SetString(&p->z
1b142 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 6f 75 74  ErrMsg, db, "out
1b143 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20   of memory");.  
1b144 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
1b145 4d 3b 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f 65  M;.  goto vdbe_e
1b146 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f 2a  rror_halt;..  /*
1b147 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 66 6f   Jump to here fo
1b148 72 20 61 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55  r an SQLITE_MISU
1b149 53 45 20 65 72 72 6f 72 2e 0a 20 20 2a 2f 0a 61  SE error..  */.a
1b14a 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 6d 69 73 75  bort_due_to_misu
1b14b 73 65 3a 0a 20 20 72 63 20 3d 20 53 51 4c 49 54  se:.  rc = SQLIT
1b14c 45 5f 4d 49 53 55 53 45 3b 0a 20 20 2f 2a 20 46  E_MISUSE;.  /* F
1b14d 61 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 61 62  all thru into ab
1b14e 6f 72 74 5f 64 75 65 5f 74 6f 5f 65 72 72 6f 72  ort_due_to_error
1b14f 20 2a 2f 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74   */..  /* Jump t
1b150 6f 20 68 65 72 65 20 66 6f 72 20 61 6e 79 20 6f  o here for any o
1b151 74 68 65 72 20 6b 69 6e 64 20 6f 66 20 66 61 74  ther kind of fat
1b152 61 6c 20 65 72 72 6f 72 2e 20 20 54 68 65 20 22  al error.  The "
1b153 72 63 22 20 76 61 72 69 61 62 6c 65 0a 20 20 2a  rc" variable.  *
1b154 2a 20 73 68 6f 75 6c 64 20 68 6f 6c 64 20 74 68  * should hold th
1b155 65 20 65 72 72 6f 72 20 6e 75 6d 62 65 72 2e 0a  e error number..
1b156 20 20 2a 2f 0a 61 62 6f 72 74 5f 64 75 65 5f 74    */.abort_due_t
1b157 6f 5f 65 72 72 6f 72 3a 0a 20 20 61 73 73 65 72  o_error:.  asser
1b158 74 28 20 70 2d 3e 7a 45 72 72 4d 73 67 3d 3d 30  t( p->zErrMsg==0
1b159 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
1b15a 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 63 20  llocFailed ) rc 
1b15b 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
1b15c 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1b15d 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a  _IOERR_NOMEM ){.
1b15e 20 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74      sqlite3SetSt
1b15f 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1b160 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1b161 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
1b162 20 20 7d 0a 20 20 67 6f 74 6f 20 76 64 62 65 5f    }.  goto vdbe_
1b163 65 72 72 6f 72 5f 68 61 6c 74 3b 0a 0a 20 20 2f  error_halt;..  /
1b164 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69  * Jump to here i
1b165 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e  f the sqlite3_in
1b166 74 65 72 72 75 70 74 28 29 20 41 50 49 20 73 65  terrupt() API se
1b167 74 73 20 74 68 65 20 69 6e 74 65 72 72 75 70 74  ts the interrupt
1b168 0a 20 20 2a 2a 20 66 6c 61 67 2e 0a 20 20 2a 2f  .  ** flag..  */
1b169 0a 61 62 6f 72 74 5f 64 75 65 5f 74 6f 5f 69 6e  .abort_due_to_in
1b16a 74 65 72 72 75 70 74 3a 0a 20 20 61 73 73 65 72  terrupt:.  asser
1b16b 74 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65  t( db->u1.isInte
1b16c 72 72 75 70 74 65 64 20 29 3b 0a 20 20 72 63 20  rrupted );.  rc 
1b16d 3d 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55  = SQLITE_INTERRU
1b16e 50 54 3b 0a 20 20 70 2d 3e 72 63 20 3d 20 72 63  PT;.  p->rc = rc
1b16f 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 74 53 74  ;.  sqlite3SetSt
1b170 72 69 6e 67 28 26 70 2d 3e 7a 45 72 72 4d 73 67  ring(&p->zErrMsg
1b171 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
1b172 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
1b173 20 20 67 6f 74 6f 20 76 64 62 65 5f 65 72 72 6f    goto vdbe_erro
1b174 72 5f 68 61 6c 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  r_halt;.}../****
1b175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
1b176 66 20 76 64 62 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a  f vdbe.c *******
1b177 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b178 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b179 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
1b17a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
1b17b 20 66 69 6c 65 20 76 64 62 65 62 6c 6f 62 2e 63   file vdbeblob.c
1b17c 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1b17d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b17e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
1b17f 20 32 30 30 37 20 4d 61 79 20 31 0a 2a 2a 0a 2a   2007 May 1.**.*
1b180 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1b181 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1b182 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1b183 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1b184 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1b185 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1b186 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1b187 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1b188 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1b189 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1b18a 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1b18b 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1b18c 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1b18d 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1b18e 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1b18f 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1b190 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1b191 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1b192 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b193 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b194 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b195 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a  **********.**.**
1b196 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61   This file conta
1b197 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f  ins code used to
1b198 20 69 6d 70 6c 65 6d 65 6e 74 20 69 6e 63 72 65   implement incre
1b199 6d 65 6e 74 61 6c 20 42 4c 4f 42 20 49 2f 4f 2e  mental BLOB I/O.
1b19a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76 64 62 65  .**.** $Id: vdbe
1b19b 62 6c 6f 62 2e 63 2c 76 20 31 2e 32 36 20 32 30  blob.c,v 1.26 20
1b19c 30 38 2f 31 30 2f 30 32 20 31 34 3a 34 39 3a 30  08/10/02 14:49:0
1b19d 32 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  2 danielk1977 Ex
1b19e 70 20 24 0a 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66  p $.*/...#ifndef
1b19f 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49 4e 43   SQLITE_OMIT_INC
1b1a0 52 42 4c 4f 42 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c  RBLOB../*.** Val
1b1a1 69 64 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a  id sqlite3_blob*
1b1a2 20 68 61 6e 64 6c 65 73 20 70 6f 69 6e 74 20 74   handles point t
1b1a3 6f 20 49 6e 63 72 62 6c 6f 62 20 73 74 72 75 63  o Incrblob struc
1b1a4 74 75 72 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65  tures..*/.typede
1b1a5 66 20 73 74 72 75 63 74 20 49 6e 63 72 62 6c 6f  f struct Incrblo
1b1a6 62 20 49 6e 63 72 62 6c 6f 62 3b 0a 73 74 72 75  b Incrblob;.stru
1b1a7 63 74 20 49 6e 63 72 62 6c 6f 62 20 7b 0a 20 20  ct Incrblob {.  
1b1a8 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20  int flags;      
1b1a9 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20          /* Copy 
1b1aa 6f 66 20 22 66 6c 61 67 73 22 20 70 61 73 73 65  of "flags" passe
1b1ab 64 20 74 6f 20 73 71 6c 69 74 65 33 5f 62 6c 6f  d to sqlite3_blo
1b1ac 62 5f 6f 70 65 6e 28 29 20 2a 2f 0a 20 20 69 6e  b_open() */.  in
1b1ad 74 20 6e 42 79 74 65 3b 20 20 20 20 20 20 20 20  t nByte;        
1b1ae 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
1b1af 20 6f 70 65 6e 20 62 6c 6f 62 2c 20 69 6e 20 62   open blob, in b
1b1b0 79 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 4f  ytes */.  int iO
1b1b1 66 66 73 65 74 3b 20 20 20 20 20 20 20 20 20 20  ffset;          
1b1b2 20 20 2f 2a 20 42 79 74 65 20 6f 66 66 73 65 74    /* Byte offset
1b1b3 20 6f 66 20 62 6c 6f 62 20 69 6e 20 63 75 72 73   of blob in curs
1b1b4 6f 72 20 64 61 74 61 20 2a 2f 0a 20 20 42 74 43  or data */.  BtC
1b1b5 75 72 73 6f 72 20 2a 70 43 73 72 3b 20 20 20 20  ursor *pCsr;    
1b1b6 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 70       /* Cursor p
1b1b7 6f 69 6e 74 69 6e 67 20 61 74 20 62 6c 6f 62 20  ointing at blob 
1b1b8 72 6f 77 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  row */.  sqlite3
1b1b9 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20  _stmt *pStmt;   
1b1ba 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 20 68 6f   /* Statement ho
1b1bb 6c 64 69 6e 67 20 63 75 72 73 6f 72 20 6f 70 65  lding cursor ope
1b1bc 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a  n */.  sqlite3 *
1b1bd 64 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  db;            /
1b1be 2a 20 54 68 65 20 61 73 73 6f 63 69 61 74 65 64  * The associated
1b1bf 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 7d 3b 0a   database */.};.
1b1c0 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 62 6c  ./*.** Open a bl
1b1c1 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 53 51  ob handle..*/.SQ
1b1c2 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
1b1c3 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 28 0a  ite3_blob_open(.
1b1c4 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20 20    sqlite3* db,  
1b1c5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1b1c6 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   database connec
1b1c7 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  tion */.  const 
1b1c8 63 68 61 72 20 2a 7a 44 62 2c 20 20 20 20 20 20  char *zDb,      
1b1c9 20 20 2f 2a 20 54 68 65 20 61 74 74 61 63 68 65    /* The attache
1b1ca 64 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61  d database conta
1b1cb 69 6e 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a  ining the blob *
1b1cc 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1b1cd 7a 54 61 62 6c 65 2c 20 20 20 20 20 2f 2a 20 54  zTable,     /* T
1b1ce 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
1b1cf 69 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a  ing the blob */.
1b1d0 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43    const char *zC
1b1d1 6f 6c 75 6d 6e 2c 20 20 20 20 2f 2a 20 54 68 65  olumn,    /* The
1b1d2 20 63 6f 6c 75 6d 6e 20 63 6f 6e 74 61 69 6e 69   column containi
1b1d3 6e 67 20 74 68 65 20 62 6c 6f 62 20 2a 2f 0a 20  ng the blob */. 
1b1d4 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 52   sqlite_int64 iR
1b1d5 6f 77 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  ow,      /* The 
1b1d6 72 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  row containing t
1b1d7 68 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 69 6e 74  he glob */.  int
1b1d8 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 20   flags,         
1b1d9 20 20 20 20 20 2f 2a 20 54 72 75 65 20 2d 3e 20       /* True -> 
1b1da 72 65 61 64 2f 77 72 69 74 65 20 61 63 63 65 73  read/write acces
1b1db 73 2c 20 66 61 6c 73 65 20 2d 3e 20 72 65 61 64  s, false -> read
1b1dc 2d 6f 6e 6c 79 20 2a 2f 0a 20 20 73 71 6c 69 74  -only */.  sqlit
1b1dd 65 33 5f 62 6c 6f 62 20 2a 2a 70 70 42 6c 6f 62  e3_blob **ppBlob
1b1de 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 66 6f 72     /* Handle for
1b1df 20 61 63 63 65 73 73 69 6e 67 20 74 68 65 20 62   accessing the b
1b1e0 6c 6f 62 20 72 65 74 75 72 6e 65 64 20 68 65 72  lob returned her
1b1e1 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6e 41  e */.){.  int nA
1b1e2 74 74 65 6d 70 74 20 3d 20 30 3b 0a 20 20 69 6e  ttempt = 0;.  in
1b1e3 74 20 69 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  t iCol;         
1b1e4 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f        /* Index o
1b1e5 66 20 7a 43 6f 6c 75 6d 6e 20 69 6e 20 72 6f 77  f zColumn in row
1b1e6 2d 72 65 63 6f 72 64 20 2a 2f 0a 0a 20 20 2f 2a  -record */..  /*
1b1e7 20 54 68 69 73 20 56 44 42 45 20 70 72 6f 67 72   This VDBE progr
1b1e8 61 6d 20 73 65 65 6b 73 20 61 20 62 74 72 65 65  am seeks a btree
1b1e9 20 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69   cursor to the i
1b1ea 64 65 6e 74 69 66 69 65 64 20 0a 20 20 2a 2a 20  dentified .  ** 
1b1eb 64 62 2f 74 61 62 6c 65 2f 72 6f 77 20 65 6e 74  db/table/row ent
1b1ec 72 79 2e 20 54 68 65 20 72 65 61 73 6f 6e 20 66  ry. The reason f
1b1ed 6f 72 20 75 73 69 6e 67 20 61 20 76 64 62 65 20  or using a vdbe 
1b1ee 70 72 6f 67 72 61 6d 20 69 6e 73 74 65 61 64 0a  program instead.
1b1ef 20 20 2a 2a 20 6f 66 20 77 72 69 74 69 6e 67 20    ** of writing 
1b1f0 63 6f 64 65 20 74 6f 20 75 73 65 20 74 68 65 20  code to use the 
1b1f1 62 2d 74 72 65 65 20 6c 61 79 65 72 20 64 69 72  b-tree layer dir
1b1f2 65 63 74 6c 79 20 69 73 20 74 68 61 74 20 74 68  ectly is that th
1b1f3 65 0a 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67  e.  ** vdbe prog
1b1f4 72 61 6d 20 77 69 6c 6c 20 74 61 6b 65 20 61 64  ram will take ad
1b1f5 76 61 6e 74 61 67 65 20 6f 66 20 74 68 65 20 76  vantage of the v
1b1f6 61 72 69 6f 75 73 20 74 72 61 6e 73 61 63 74 69  arious transacti
1b1f7 6f 6e 2c 0a 20 20 2a 2a 20 6c 6f 63 6b 69 6e 67  on,.  ** locking
1b1f8 20 61 6e 64 20 65 72 72 6f 72 20 68 61 6e 64 6c   and error handl
1b1f9 69 6e 67 20 69 6e 66 72 61 73 74 72 75 63 74 75  ing infrastructu
1b1fa 72 65 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68  re built into th
1b1fb 65 20 76 64 62 65 2e 0a 20 20 2a 2a 0a 20 20 2a  e vdbe..  **.  *
1b1fc 2a 20 41 66 74 65 72 20 73 65 65 6b 69 6e 67 20  * After seeking 
1b1fd 74 68 65 20 63 75 72 73 6f 72 2c 20 74 68 65 20  the cursor, the 
1b1fe 76 64 62 65 20 65 78 65 63 75 74 65 73 20 61 6e  vdbe executes an
1b1ff 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2e 0a 20   OP_ResultRow.. 
1b200 20 2a 2a 20 43 6f 64 65 20 65 78 74 65 72 6e 61   ** Code externa
1b201 6c 20 74 6f 20 74 68 65 20 56 64 62 65 20 74 68  l to the Vdbe th
1b202 65 6e 20 22 62 6f 72 72 6f 77 73 22 20 74 68 65  en "borrows" the
1b203 20 62 2d 74 72 65 65 20 63 75 72 73 6f 72 20 61   b-tree cursor a
1b204 6e 64 0a 20 20 2a 2a 20 75 73 65 73 20 69 74 20  nd.  ** uses it 
1b205 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1b206 20 62 6c 6f 62 5f 72 65 61 64 28 29 2c 20 62 6c   blob_read(), bl
1b207 6f 62 5f 77 72 69 74 65 28 29 20 61 6e 64 20 0a  ob_write() and .
1b208 20 20 2a 2a 20 62 6c 6f 62 5f 62 79 74 65 73 28    ** blob_bytes(
1b209 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 20 20 2a  ) functions..  *
1b20a 2a 0a 20 20 2a 2a 20 54 68 65 20 73 71 6c 69 74  *.  ** The sqlit
1b20b 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 28 29 20  e3_blob_close() 
1b20c 66 75 6e 63 74 69 6f 6e 20 66 69 6e 61 6c 69 7a  function finaliz
1b20d 65 73 20 74 68 65 20 76 64 62 65 20 70 72 6f 67  es the vdbe prog
1b20e 72 61 6d 2c 0a 20 20 2a 2a 20 77 68 69 63 68 20  ram,.  ** which 
1b20f 63 6c 6f 73 65 73 20 74 68 65 20 62 2d 74 72 65  closes the b-tre
1b210 65 20 63 75 72 73 6f 72 20 61 6e 64 20 28 70 6f  e cursor and (po
1b211 73 73 69 62 6c 79 29 20 63 6f 6d 6d 69 74 73 20  ssibly) commits 
1b212 74 68 65 20 0a 20 20 2a 2a 20 74 72 61 6e 73 61  the .  ** transa
1b213 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 73 74  ction..  */.  st
1b214 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
1b215 70 4c 69 73 74 20 6f 70 65 6e 42 6c 6f 62 5b 5d  pList openBlob[]
1b216 20 3d 20 7b 0a 20 20 20 20 7b 4f 50 5f 54 72 61   = {.    {OP_Tra
1b217 6e 73 61 63 74 69 6f 6e 2c 20 30 2c 20 30 2c 20  nsaction, 0, 0, 
1b218 30 7d 2c 20 20 20 20 20 2f 2a 20 30 3a 20 53 74  0},     /* 0: St
1b219 61 72 74 20 61 20 74 72 61 6e 73 61 63 74 69 6f  art a transactio
1b21a 6e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 56 65 72  n */.    {OP_Ver
1b21b 69 66 79 43 6f 6f 6b 69 65 2c 20 30 2c 20 30 2c  ifyCookie, 0, 0,
1b21c 20 30 7d 2c 20 20 20 20 2f 2a 20 31 3a 20 43 68   0},    /* 1: Ch
1b21d 65 63 6b 20 74 68 65 20 73 63 68 65 6d 61 20 63  eck the schema c
1b21e 6f 6f 6b 69 65 20 2a 2f 0a 0a 20 20 20 20 2f 2a  ookie */..    /*
1b21f 20 4f 6e 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c   One of the foll
1b220 6f 77 69 6e 67 20 74 77 6f 20 69 6e 73 74 72 75  owing two instru
1b221 63 74 69 6f 6e 73 20 69 73 20 72 65 70 6c 61 63  ctions is replac
1b222 65 64 20 62 79 20 61 6e 0a 20 20 20 20 2a 2a 20  ed by an.    ** 
1b223 4f 50 5f 4e 6f 6f 70 20 62 65 66 6f 72 65 20 65  OP_Noop before e
1b224 78 65 63 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a  xection..    */.
1b225 20 20 20 20 7b 4f 50 5f 53 65 74 4e 75 6d 43 6f      {OP_SetNumCo
1b226 6c 75 6d 6e 73 2c 20 30 2c 20 30 2c 20 30 7d 2c  lumns, 0, 0, 0},
1b227 20 20 20 2f 2a 20 32 3a 20 4e 75 6d 20 63 6f 6c     /* 2: Num col
1b228 73 20 66 6f 72 20 63 75 72 73 6f 72 20 2a 2f 0a  s for cursor */.
1b229 20 20 20 20 7b 4f 50 5f 4f 70 65 6e 52 65 61 64      {OP_OpenRead
1b22a 2c 20 30 2c 20 30 2c 20 30 7d 2c 20 20 20 20 20  , 0, 0, 0},     
1b22b 20 20 20 2f 2a 20 33 3a 20 4f 70 65 6e 20 63 75     /* 3: Open cu
1b22c 72 73 6f 72 20 30 20 66 6f 72 20 72 65 61 64 69  rsor 0 for readi
1b22d 6e 67 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 53 65  ng */.    {OP_Se
1b22e 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20  tNumColumns, 0, 
1b22f 30 2c 20 30 7d 2c 20 20 20 2f 2a 20 34 3a 20 4e  0, 0},   /* 4: N
1b230 75 6d 20 63 6f 6c 73 20 66 6f 72 20 63 75 72 73  um cols for curs
1b231 6f 72 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 4f 70  or */.    {OP_Op
1b232 65 6e 57 72 69 74 65 2c 20 30 2c 20 30 2c 20 30  enWrite, 0, 0, 0
1b233 7d 2c 20 20 20 20 20 20 20 2f 2a 20 35 3a 20 4f  },       /* 5: O
1b234 70 65 6e 20 63 75 72 73 6f 72 20 30 20 66 6f 72  pen cursor 0 for
1b235 20 72 65 61 64 2f 77 72 69 74 65 20 2a 2f 0a 0a   read/write */..
1b236 20 20 20 20 7b 4f 50 5f 56 61 72 69 61 62 6c 65      {OP_Variable
1b237 2c 20 31 2c 20 31 2c 20 30 7d 2c 20 20 20 20 20  , 1, 1, 0},     
1b238 20 20 20 2f 2a 20 36 3a 20 50 75 73 68 20 74 68     /* 6: Push th
1b239 65 20 72 6f 77 69 64 20 74 6f 20 74 68 65 20 73  e rowid to the s
1b23a 74 61 63 6b 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f  tack */.    {OP_
1b23b 4e 6f 74 45 78 69 73 74 73 2c 20 30 2c 20 31 30  NotExists, 0, 10
1b23c 2c 20 31 7d 2c 20 20 20 20 20 20 2f 2a 20 37 3a  , 1},      /* 7:
1b23d 20 53 65 65 6b 20 74 68 65 20 63 75 72 73 6f 72   Seek the cursor
1b23e 20 2a 2f 0a 20 20 20 20 7b 4f 50 5f 43 6f 6c 75   */.    {OP_Colu
1b23f 6d 6e 2c 20 30 2c 20 30 2c 20 31 7d 2c 20 20 20  mn, 0, 0, 1},   
1b240 20 20 20 20 20 20 20 2f 2a 20 38 20 20 2a 2f 0a         /* 8  */.
1b241 20 20 20 20 7b 4f 50 5f 52 65 73 75 6c 74 52 6f      {OP_ResultRo
1b242 77 2c 20 31 2c 20 30 2c 20 30 7d 2c 20 20 20 20  w, 1, 0, 0},    
1b243 20 20 20 2f 2a 20 39 20 20 2a 2f 0a 20 20 20 20     /* 9  */.    
1b244 7b 4f 50 5f 43 6c 6f 73 65 2c 20 30 2c 20 30 2c  {OP_Close, 0, 0,
1b245 20 30 7d 2c 20 20 20 20 20 20 20 20 20 20 20 2f   0},           /
1b246 2a 20 31 30 20 20 2a 2f 0a 20 20 20 20 7b 4f 50  * 10  */.    {OP
1b247 5f 48 61 6c 74 2c 20 30 2c 20 30 2c 20 30 7d 2c  _Halt, 0, 0, 0},
1b248 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 31              /* 1
1b249 31 20 2a 2f 0a 20 20 7d 3b 0a 0a 20 20 56 64 62  1 */.  };..  Vdb
1b24a 65 20 2a 76 20 3d 20 30 3b 0a 20 20 69 6e 74 20  e *v = 0;.  int 
1b24b 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1b24c 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d    char zErr[128]
1b24d 3b 0a 0a 20 20 7a 45 72 72 5b 30 5d 20 3d 20 30  ;..  zErr[0] = 0
1b24e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
1b24f 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
1b250 78 29 3b 0a 20 20 64 6f 20 7b 0a 20 20 20 20 50  x);.  do {.    P
1b251 61 72 73 65 20 73 50 61 72 73 65 3b 0a 20 20 20  arse sParse;.   
1b252 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a 20   Table *pTab;.. 
1b253 20 20 20 6d 65 6d 73 65 74 28 26 73 50 61 72 73     memset(&sPars
1b254 65 2c 20 30 2c 20 73 69 7a 65 6f 66 28 50 61 72  e, 0, sizeof(Par
1b255 73 65 29 29 3b 0a 20 20 20 20 73 50 61 72 73 65  se));.    sParse
1b256 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 20 20 20 69  .db = db;..    i
1b257 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
1b258 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20  On(db) ){.      
1b259 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
1b25a 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
1b25b 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
1b25c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 20 20  ITE_MISUSE;.    
1b25d 7d 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74  }..    sqlite3Bt
1b25e 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29 3b  reeEnterAll(db);
1b25f 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c 69  .    pTab = sqli
1b260 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 26  te3LocateTable(&
1b261 73 50 61 72 73 65 2c 20 30 2c 20 7a 54 61 62 6c  sParse, 0, zTabl
1b262 65 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28  e, zDb);.    if(
1b263 20 70 54 61 62 20 26 26 20 49 73 56 69 72 74 75   pTab && IsVirtu
1b264 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
1b265 20 20 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20    pTab = 0;.    
1b266 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1b267 67 28 26 73 50 61 72 73 65 2c 20 22 63 61 6e 6e  g(&sParse, "cann
1b268 6f 74 20 6f 70 65 6e 20 76 69 72 74 75 61 6c 20  ot open virtual 
1b269 74 61 62 6c 65 3a 20 25 73 22 2c 20 7a 54 61 62  table: %s", zTab
1b26a 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  le);.    }.#ifnd
1b26b 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1b26c 49 45 57 0a 20 20 20 20 69 66 28 20 70 54 61 62  IEW.    if( pTab
1b26d 20 26 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63   && pTab->pSelec
1b26e 74 20 29 7b 0a 20 20 20 20 20 20 70 54 61 62 20  t ){.      pTab 
1b26f 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  = 0;.      sqlit
1b270 65 33 45 72 72 6f 72 4d 73 67 28 26 73 50 61 72  e3ErrorMsg(&sPar
1b271 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e  se, "cannot open
1b272 20 76 69 65 77 3a 20 25 73 22 2c 20 7a 54 61 62   view: %s", zTab
1b273 6c 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  le);.    }.#endi
1b274 66 0a 20 20 20 20 69 66 28 20 21 70 54 61 62 20  f.    if( !pTab 
1b275 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 50 61  ){.      if( sPa
1b276 72 73 65 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20  rse.zErrMsg ){. 
1b277 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
1b278 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1b279 45 72 72 29 2c 20 7a 45 72 72 2c 20 22 25 73 22  Err), zErr, "%s"
1b27a 2c 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73 67  , sParse.zErrMsg
1b27b 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1b27c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1b27d 62 2c 20 73 50 61 72 73 65 2e 7a 45 72 72 4d 73  b, sParse.zErrMs
1b27e 67 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53  g);.      rc = S
1b27f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1b280 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
1b281 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
1b282 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
1b283 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20  eLeaveAll(db);. 
1b284 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f 6f       goto blob_o
1b285 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 0a  pen_out;.    }..
1b286 20 20 20 20 2f 2a 20 4e 6f 77 20 73 65 61 72 63      /* Now searc
1b287 68 20 70 54 61 62 20 66 6f 72 20 74 68 65 20 65  h pTab for the e
1b288 78 61 63 74 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a  xact column. */.
1b289 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20      for(iCol=0; 
1b28a 69 43 6f 6c 20 3c 20 70 54 61 62 2d 3e 6e 43 6f  iCol < pTab->nCo
1b28b 6c 3b 20 69 43 6f 6c 2b 2b 29 20 7b 0a 20 20 20  l; iCol++) {.   
1b28c 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
1b28d 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f 6c  rICmp(pTab->aCol
1b28e 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 2c 20 7a 43  [iCol].zName, zC
1b28f 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b 0a 20 20 20  olumn)==0 ){.   
1b290 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1b291 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
1b292 28 20 69 43 6f 6c 3d 3d 70 54 61 62 2d 3e 6e 43  ( iCol==pTab->nC
1b293 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ol ){.      sqli
1b294 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a  te3_snprintf(siz
1b295 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c  eof(zErr), zErr,
1b296 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 75 6d 6e   "no such column
1b297 3a 20 5c 22 25 73 5c 22 22 2c 20 7a 43 6f 6c 75  : \"%s\"", zColu
1b298 6d 6e 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  mn);.      rc = 
1b299 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1b29a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
1b29b 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
1b29c 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72        sqlite3Btr
1b29d 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
1b29e 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f 62 5f        goto blob_
1b29f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a  open_out;.    }.
1b2a0 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 76  .    /* If the v
1b2a1 61 6c 75 65 20 69 73 20 62 65 69 6e 67 20 6f 70  alue is being op
1b2a2 65 6e 65 64 20 66 6f 72 20 77 72 69 74 69 6e 67  ened for writing
1b2a3 2c 20 63 68 65 63 6b 20 74 68 61 74 20 74 68 65  , check that the
1b2a4 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69  .    ** column i
1b2a5 73 20 6e 6f 74 20 69 6e 64 65 78 65 64 2e 20 49  s not indexed. I
1b2a6 74 20 69 73 20 61 67 61 69 6e 73 74 20 74 68 65  t is against the
1b2a7 20 72 75 6c 65 73 20 74 6f 20 6f 70 65 6e 20 61   rules to open a
1b2a8 6e 0a 20 20 20 20 2a 2a 20 69 6e 64 65 78 65 64  n.    ** indexed
1b2a9 20 63 6f 6c 75 6d 6e 20 66 6f 72 20 77 72 69 74   column for writ
1b2aa 69 6e 67 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ing..    */.    
1b2ab 69 66 28 20 66 6c 61 67 73 20 29 7b 0a 20 20 20  if( flags ){.   
1b2ac 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a     Index *pIdx;.
1b2ad 20 20 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70        for(pIdx=p
1b2ae 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
1b2af 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1b2b0 65 78 74 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ext){.        in
1b2b1 74 20 6a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72  t j;.        for
1b2b2 28 6a 3d 30 3b 20 6a 3c 70 49 64 78 2d 3e 6e 43  (j=0; j<pIdx->nC
1b2b3 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20  olumn; j++){.   
1b2b4 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
1b2b5 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3d 3d 69 43  >aiColumn[j]==iC
1b2b6 6f 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ol ){.          
1b2b7 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1b2b8 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c  tf(sizeof(zErr),
1b2b9 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20   zErr,.         
1b2ba 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b2bb 20 20 20 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e      "cannot open
1b2bc 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e 20   indexed column 
1b2bd 66 6f 72 20 77 72 69 74 69 6e 67 22 29 3b 0a 20  for writing");. 
1b2be 20 20 20 20 20 20 20 20 20 20 20 72 63 20 3d 20             rc = 
1b2bf 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
1b2c0 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64 29            (void)
1b2c1 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
1b2c2 28 64 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20  (db);.          
1b2c3 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65    sqlite3BtreeLe
1b2c4 61 76 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20  aveAll(db);.    
1b2c5 20 20 20 20 20 20 20 20 67 6f 74 6f 20 62 6c 6f          goto blo
1b2c6 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a 20 20 20 20  b_open_out;.    
1b2c7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1b2c8 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1b2c9 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65 33  .    v = sqlite3
1b2ca 56 64 62 65 43 72 65 61 74 65 28 64 62 29 3b 0a  VdbeCreate(db);.
1b2cb 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20      if( v ){.   
1b2cc 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c     int iDb = sql
1b2cd 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65  ite3SchemaToInde
1b2ce 78 28 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68  x(db, pTab->pSch
1b2cf 65 6d 61 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ema);.      sqli
1b2d0 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69 73 74  te3VdbeAddOpList
1b2d1 28 76 2c 20 73 69 7a 65 6f 66 28 6f 70 65 6e 42  (v, sizeof(openB
1b2d2 6c 6f 62 29 2f 73 69 7a 65 6f 66 28 56 64 62 65  lob)/sizeof(Vdbe
1b2d3 4f 70 4c 69 73 74 29 2c 20 6f 70 65 6e 42 6c 6f  OpList), openBlo
1b2d4 62 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 6f  b);..      /* Co
1b2d5 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50 5f 54  nfigure the OP_T
1b2d6 72 61 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20  ransaction */.  
1b2d7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1b2d8 68 61 6e 67 65 50 31 28 76 2c 20 30 2c 20 69 44  hangeP1(v, 0, iD
1b2d9 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
1b2da 33 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c  3VdbeChangeP2(v,
1b2db 20 30 2c 20 28 66 6c 61 67 73 20 3f 20 31 20 3a   0, (flags ? 1 :
1b2dc 20 30 29 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20   0));..      /* 
1b2dd 43 6f 6e 66 69 67 75 72 65 20 74 68 65 20 4f 50  Configure the OP
1b2de 5f 56 65 72 69 66 79 43 6f 6f 6b 69 65 20 2a 2f  _VerifyCookie */
1b2df 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1b2e0 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 31 2c  beChangeP1(v, 1,
1b2e1 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
1b2e2 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32  ite3VdbeChangeP2
1b2e3 28 76 2c 20 31 2c 20 70 54 61 62 2d 3e 70 53 63  (v, 1, pTab->pSc
1b2e4 68 65 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f  hema->schema_coo
1b2e5 6b 69 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  kie);..      /* 
1b2e6 4d 61 6b 65 20 73 75 72 65 20 61 20 6d 75 74 65  Make sure a mute
1b2e7 78 20 69 73 20 68 65 6c 64 20 6f 6e 20 74 68 65  x is held on the
1b2e8 20 74 61 62 6c 65 20 74 6f 20 62 65 20 61 63 63   table to be acc
1b2e9 65 73 73 65 64 20 2a 2f 0a 20 20 20 20 20 20 73  essed */.      s
1b2ea 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
1b2eb 72 65 65 28 76 2c 20 69 44 62 29 3b 20 0a 0a 20  ree(v, iDb); .. 
1b2ec 20 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 65       /* Remove e
1b2ed 69 74 68 65 72 20 74 68 65 20 4f 50 5f 4f 70 65  ither the OP_Ope
1b2ee 6e 57 72 69 74 65 20 6f 72 20 4f 70 65 6e 52 65  nWrite or OpenRe
1b2ef 61 64 2e 20 53 65 74 20 74 68 65 20 50 32 20 0a  ad. Set the P2 .
1b2f0 20 20 20 20 20 20 2a 2a 20 70 61 72 61 6d 65 74        ** paramet
1b2f1 65 72 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20  er of the other 
1b2f2 74 6f 20 70 54 61 62 2d 3e 74 6e 75 6d 2e 20 0a  to pTab->tnum. .
1b2f3 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1b2f4 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1b2f5 54 6f 4e 6f 6f 70 28 76 2c 20 28 66 6c 61 67 73  ToNoop(v, (flags
1b2f6 20 3f 20 33 20 3a 20 35 29 2c 20 31 29 3b 0a 20   ? 3 : 5), 1);. 
1b2f7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b2f8 43 68 61 6e 67 65 50 32 28 76 2c 20 28 66 6c 61  ChangeP2(v, (fla
1b2f9 67 73 20 3f 20 35 20 3a 20 33 29 2c 20 70 54 61  gs ? 5 : 3), pTa
1b2fa 62 2d 3e 74 6e 75 6d 29 3b 0a 20 20 20 20 20 20  b->tnum);.      
1b2fb 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
1b2fc 65 50 33 28 76 2c 20 28 66 6c 61 67 73 20 3f 20  eP3(v, (flags ? 
1b2fd 35 20 3a 20 33 29 2c 20 69 44 62 29 3b 0a 0a 20  5 : 3), iDb);.. 
1b2fe 20 20 20 20 20 2f 2a 20 43 6f 6e 66 69 67 75 72       /* Configur
1b2ff 65 20 74 68 65 20 4f 50 5f 53 65 74 4e 75 6d 43  e the OP_SetNumC
1b300 6f 6c 75 6d 6e 73 2e 20 43 6f 6e 66 69 67 75 72  olumns. Configur
1b301 65 20 74 68 65 20 63 75 72 73 6f 72 20 74 6f 0a  e the cursor to.
1b302 20 20 20 20 20 20 2a 2a 20 74 68 69 6e 6b 20 74        ** think t
1b303 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 68 61  hat the table ha
1b304 73 20 6f 6e 65 20 6d 6f 72 65 20 63 6f 6c 75 6d  s one more colum
1b305 6e 20 74 68 61 6e 20 69 74 20 72 65 61 6c 6c 79  n than it really
1b306 0a 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 2e 20  .      ** does. 
1b307 41 6e 20 4f 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20  An OP_Column to 
1b308 72 65 74 72 69 65 76 65 20 74 68 69 73 20 69 6d  retrieve this im
1b309 61 67 69 6e 61 72 79 20 63 6f 6c 75 6d 6e 20 77  aginary column w
1b30a 69 6c 6c 0a 20 20 20 20 20 20 2a 2a 20 61 6c 77  ill.      ** alw
1b30b 61 79 73 20 72 65 74 75 72 6e 20 61 6e 20 53 51  ays return an SQ
1b30c 4c 20 4e 55 4c 4c 2e 20 54 68 69 73 20 69 73 20  L NULL. This is 
1b30d 75 73 65 66 75 6c 20 62 65 63 61 75 73 65 20 69  useful because i
1b30e 74 20 6d 65 61 6e 73 0a 20 20 20 20 20 20 2a 2a  t means.      **
1b30f 20 77 65 20 63 61 6e 20 69 6e 76 6f 6b 65 20 4f   we can invoke O
1b310 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 66 69 6c 6c  P_Column to fill
1b311 20 69 6e 20 74 68 65 20 76 64 62 65 20 63 75 72   in the vdbe cur
1b312 73 6f 72 73 20 74 79 70 65 20 0a 20 20 20 20 20  sors type .     
1b313 20 2a 2a 20 61 6e 64 20 6f 66 66 73 65 74 20 63   ** and offset c
1b314 61 63 68 65 20 77 69 74 68 6f 75 74 20 63 61 75  ache without cau
1b315 73 69 6e 67 20 61 6e 79 20 49 4f 2e 0a 20 20 20  sing any IO..   
1b316 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69     */.      sqli
1b317 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
1b318 76 2c 20 66 6c 61 67 73 20 3f 20 34 20 3a 20 32  v, flags ? 4 : 2
1b319 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b  , pTab->nCol+1);
1b31a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1b31b 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 38 2c  beChangeP2(v, 8,
1b31c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
1b31d 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c      if( !db->mal
1b31e 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1b31f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1b320 4d 61 6b 65 52 65 61 64 79 28 76 2c 20 31 2c 20  MakeReady(v, 1, 
1b321 31 2c 20 31 2c 20 30 29 3b 0a 20 20 20 20 20 20  1, 1, 0);.      
1b322 7d 0a 20 20 20 20 7d 0a 20 20 20 0a 20 20 20 20  }.    }.   .    
1b323 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76  sqlite3BtreeLeav
1b324 65 41 6c 6c 28 64 62 29 3b 0a 20 20 20 20 72 63  eAll(db);.    rc
1b325 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65 74 79   = sqlite3Safety
1b326 4f 66 66 28 64 62 29 3b 0a 20 20 20 20 69 66 28  Off(db);.    if(
1b327 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
1b328 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
1b329 65 64 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  ed ){.      goto
1b32a 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3b 0a   blob_open_out;.
1b32b 20 20 20 20 7d 0a 0a 20 20 20 20 73 71 6c 69 74      }..    sqlit
1b32c 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 28 73  e3_bind_int64((s
1b32d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 2c  qlite3_stmt *)v,
1b32e 20 31 2c 20 69 52 6f 77 29 3b 0a 20 20 20 20 72   1, iRow);.    r
1b32f 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
1b330 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  ((sqlite3_stmt *
1b331 29 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  )v);.    if( rc!
1b332 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
1b333 20 20 20 20 20 6e 41 74 74 65 6d 70 74 2b 2b 3b       nAttempt++;
1b334 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
1b335 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 28 73 71  te3_finalize((sq
1b336 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 29 3b  lite3_stmt *)v);
1b337 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1b338 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1b339 45 72 72 29 2c 20 7a 45 72 72 2c 20 73 71 6c 69  Err), zErr, sqli
1b33a 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b  te3_errmsg(db));
1b33b 0a 20 20 20 20 20 20 76 20 3d 20 30 3b 0a 20 20  .      v = 0;.  
1b33c 20 20 7d 0a 20 20 7d 20 77 68 69 6c 65 28 20 6e    }.  } while( n
1b33d 41 74 74 65 6d 70 74 3c 35 20 26 26 20 72 63 3d  Attempt<5 && rc=
1b33e 3d 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 29  =SQLITE_SCHEMA )
1b33f 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
1b340 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 2f  ITE_ROW ){.    /
1b341 2a 20 54 68 65 20 72 6f 77 2d 72 65 63 6f 72 64  * The row-record
1b342 20 68 61 73 20 62 65 65 6e 20 6f 70 65 6e 65 64   has been opened
1b343 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 20 43   successfully. C
1b344 68 65 63 6b 20 74 68 61 74 20 74 68 65 0a 20 20  heck that the.  
1b345 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69 6e 20 71    ** column in q
1b346 75 65 73 74 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  uestion contains
1b347 20 74 65 78 74 20 6f 72 20 61 20 62 6c 6f 62 2e   text or a blob.
1b348 20 49 66 20 69 74 20 63 6f 6e 74 61 69 6e 73 0a   If it contains.
1b349 20 20 20 20 2a 2a 20 74 65 78 74 2c 20 69 74 20      ** text, it 
1b34a 69 73 20 75 70 20 74 6f 20 74 68 65 20 63 61 6c  is up to the cal
1b34b 6c 65 72 20 74 6f 20 67 65 74 20 74 68 65 20 65  ler to get the e
1b34c 6e 63 6f 64 69 6e 67 20 72 69 67 68 74 2e 0a 20  ncoding right.. 
1b34d 20 20 20 2a 2f 0a 20 20 20 20 49 6e 63 72 62 6c     */.    Incrbl
1b34e 6f 62 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20 75  ob *pBlob;.    u
1b34f 33 32 20 74 79 70 65 20 3d 20 76 2d 3e 61 70 43  32 type = v->apC
1b350 73 72 5b 30 5d 2d 3e 61 54 79 70 65 5b 69 43 6f  sr[0]->aType[iCo
1b351 6c 5d 3b 0a 0a 20 20 20 20 69 66 28 20 74 79 70  l];..    if( typ
1b352 65 3c 31 32 20 29 7b 0a 20 20 20 20 20 20 73 71  e<12 ){.      sq
1b353 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 73  lite3_snprintf(s
1b354 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72  izeof(zErr), zEr
1b355 72 2c 20 22 63 61 6e 6e 6f 74 20 6f 70 65 6e 20  r, "cannot open 
1b356 76 61 6c 75 65 20 6f 66 20 74 79 70 65 20 25 73  value of type %s
1b357 22 2c 0a 20 20 20 20 20 20 20 20 20 20 74 79 70  ",.          typ
1b358 65 3d 3d 30 3f 22 6e 75 6c 6c 22 3a 20 74 79 70  e==0?"null": typ
1b359 65 3d 3d 37 3f 22 72 65 61 6c 22 3a 20 22 69 6e  e==7?"real": "in
1b35a 74 65 67 65 72 22 0a 20 20 20 20 20 20 29 3b 0a  teger".      );.
1b35b 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
1b35c 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 67  E_ERROR;.      g
1b35d 6f 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75  oto blob_open_ou
1b35e 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c  t;.    }.    pBl
1b35f 6f 62 20 3d 20 28 49 6e 63 72 62 6c 6f 62 20 2a  ob = (Incrblob *
1b360 29 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63  )sqlite3DbMalloc
1b361 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
1b362 49 6e 63 72 62 6c 6f 62 29 29 3b 0a 20 20 20 20  Incrblob));.    
1b363 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1b364 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71  iled ){.      sq
1b365 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
1b366 70 42 6c 6f 62 29 3b 0a 20 20 20 20 20 20 67 6f  pBlob);.      go
1b367 74 6f 20 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74  to blob_open_out
1b368 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 42 6c 6f  ;.    }.    pBlo
1b369 62 2d 3e 66 6c 61 67 73 20 3d 20 66 6c 61 67 73  b->flags = flags
1b36a 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 70 43 73  ;.    pBlob->pCs
1b36b 72 20 3d 20 20 76 2d 3e 61 70 43 73 72 5b 30 5d  r =  v->apCsr[0]
1b36c 2d 3e 70 43 75 72 73 6f 72 3b 0a 20 20 20 20 73  ->pCursor;.    s
1b36d 71 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72  qlite3BtreeEnter
1b36e 43 75 72 73 6f 72 28 70 42 6c 6f 62 2d 3e 70 43  Cursor(pBlob->pC
1b36f 73 72 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  sr);.    sqlite3
1b370 42 74 72 65 65 43 61 63 68 65 4f 76 65 72 66 6c  BtreeCacheOverfl
1b371 6f 77 28 70 42 6c 6f 62 2d 3e 70 43 73 72 29 3b  ow(pBlob->pCsr);
1b372 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
1b373 65 4c 65 61 76 65 43 75 72 73 6f 72 28 70 42 6c  eLeaveCursor(pBl
1b374 6f 62 2d 3e 70 43 73 72 29 3b 0a 20 20 20 20 70  ob->pCsr);.    p
1b375 42 6c 6f 62 2d 3e 70 53 74 6d 74 20 3d 20 28 73  Blob->pStmt = (s
1b376 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 76 3b  qlite3_stmt *)v;
1b377 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 69 4f 66 66  .    pBlob->iOff
1b378 73 65 74 20 3d 20 76 2d 3e 61 70 43 73 72 5b 30  set = v->apCsr[0
1b379 5d 2d 3e 61 4f 66 66 73 65 74 5b 69 43 6f 6c 5d  ]->aOffset[iCol]
1b37a 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e 6e 42 79  ;.    pBlob->nBy
1b37b 74 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  te = sqlite3Vdbe
1b37c 53 65 72 69 61 6c 54 79 70 65 4c 65 6e 28 74 79  SerialTypeLen(ty
1b37d 70 65 29 3b 0a 20 20 20 20 70 42 6c 6f 62 2d 3e  pe);.    pBlob->
1b37e 64 62 20 3d 20 64 62 3b 0a 20 20 20 20 2a 70 70  db = db;.    *pp
1b37f 42 6c 6f 62 20 3d 20 28 73 71 6c 69 74 65 33 5f  Blob = (sqlite3_
1b380 62 6c 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20  blob *)pBlob;.  
1b381 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
1b382 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63  ;.  }else if( rc
1b383 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
1b384 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1b385 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29  ntf(sizeof(zErr)
1b386 2c 20 7a 45 72 72 2c 20 22 6e 6f 20 73 75 63 68  , zErr, "no such
1b387 20 72 6f 77 69 64 3a 20 25 6c 6c 64 22 2c 20 69   rowid: %lld", i
1b388 52 6f 77 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  Row);.    rc = S
1b389 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
1b38a 0a 0a 62 6c 6f 62 5f 6f 70 65 6e 5f 6f 75 74 3a  ..blob_open_out:
1b38b 0a 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28 7a  .  zErr[sizeof(z
1b38c 45 72 72 29 2d 31 5d 20 3d 20 27 5c 30 27 3b 0a  Err)-1] = '\0';.
1b38d 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
1b38e 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  _OK || db->mallo
1b38f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73  cFailed ){.    s
1b390 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
1b391 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29  (sqlite3_stmt *)
1b392 76 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  v);.  }.  sqlite
1b393 33 45 72 72 6f 72 28 64 62 2c 20 72 63 2c 20 28  3Error(db, rc, (
1b394 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 3f 30 3a  rc==SQLITE_OK?0:
1b395 7a 45 72 72 29 29 3b 0a 20 20 72 63 20 3d 20 73  zErr));.  rc = s
1b396 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
1b397 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33  , rc);.  sqlite3
1b398 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
1b399 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
1b39a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  n rc;.}../*.** C
1b39b 6c 6f 73 65 20 61 20 62 6c 6f 62 20 68 61 6e 64  lose a blob hand
1b39c 6c 65 20 74 68 61 74 20 77 61 73 20 70 72 65 76  le that was prev
1b39d 69 6f 75 73 6c 79 20 63 72 65 61 74 65 64 20 75  iously created u
1b39e 73 69 6e 67 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  sing.** sqlite3_
1b39f 62 6c 6f 62 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a  blob_open()..*/.
1b3a0 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b3a1 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73  qlite3_blob_clos
1b3a2 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
1b3a3 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c  pBlob){.  Incrbl
1b3a4 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f  ob *p = (Incrblo
1b3a5 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 69 6e 74  b *)pBlob;.  int
1b3a6 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c   rc;..  rc = sql
1b3a7 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 2d  ite3_finalize(p-
1b3a8 3e 70 53 74 6d 74 29 3b 0a 20 20 73 71 6c 69 74  >pStmt);.  sqlit
1b3a9 65 33 44 62 46 72 65 65 28 70 2d 3e 64 62 2c 20  e3DbFree(p->db, 
1b3aa 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  p);.  return rc;
1b3ab 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66 6f 72  .}../*.** Perfor
1b3ac 6d 20 61 20 72 65 61 64 20 6f 72 20 77 72 69 74  m a read or writ
1b3ad 65 20 6f 70 65 72 61 74 69 6f 6e 20 6f 6e 20 61  e operation on a
1b3ae 20 62 6c 6f 62 0a 2a 2f 0a 73 74 61 74 69 63 20   blob.*/.static 
1b3af 69 6e 74 20 62 6c 6f 62 52 65 61 64 57 72 69 74  int blobReadWrit
1b3b0 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c 6f  e(.  sqlite3_blo
1b3b1 62 20 2a 70 42 6c 6f 62 2c 20 0a 20 20 76 6f 69  b *pBlob, .  voi
1b3b2 64 20 2a 7a 2c 20 0a 20 20 69 6e 74 20 6e 2c 20  d *z, .  int n, 
1b3b3 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 2c 20  .  int iOffset, 
1b3b4 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 29 28  .  int (*xCall)(
1b3b5 42 74 43 75 72 73 6f 72 2a 2c 20 75 33 32 2c 20  BtCursor*, u32, 
1b3b6 75 33 32 2c 20 76 6f 69 64 2a 29 0a 29 7b 0a 20  u32, void*).){. 
1b3b7 20 69 6e 74 20 72 63 3b 0a 20 20 49 6e 63 72 62   int rc;.  Incrb
1b3b8 6c 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c  lob *p = (Incrbl
1b3b9 6f 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 56 64  ob *)pBlob;.  Vd
1b3ba 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33  be *v;.  sqlite3
1b3bb 20 2a 64 62 20 3d 20 70 2d 3e 64 62 3b 20 20 0a   *db = p->db;  .
1b3bc 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
1b3bd 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
1b3be 29 3b 0a 20 20 76 20 3d 20 28 56 64 62 65 2a 29  );.  v = (Vdbe*)
1b3bf 70 2d 3e 70 53 74 6d 74 3b 0a 0a 20 20 69 66 28  p->pStmt;..  if(
1b3c0 20 6e 3c 30 20 7c 7c 20 69 4f 66 66 73 65 74 3c   n<0 || iOffset<
1b3c1 30 20 7c 7c 20 28 69 4f 66 66 73 65 74 2b 6e 29  0 || (iOffset+n)
1b3c2 3e 70 2d 3e 6e 42 79 74 65 20 29 7b 0a 20 20 20  >p->nByte ){.   
1b3c3 20 2f 2a 20 52 65 71 75 65 73 74 20 69 73 20 6f   /* Request is o
1b3c4 75 74 20 6f 66 20 72 61 6e 67 65 2e 20 52 65 74  ut of range. Ret
1b3c5 75 72 6e 20 61 20 74 72 61 6e 73 69 65 6e 74 20  urn a transient 
1b3c6 65 72 72 6f 72 2e 20 2a 2f 0a 20 20 20 20 72 63  error. */.    rc
1b3c7 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
1b3c8 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1b3c9 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45 52 52  r(db, SQLITE_ERR
1b3ca 4f 52 2c 20 30 29 3b 0a 20 20 7d 20 65 6c 73 65  OR, 0);.  } else
1b3cb 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a 20 20 20   if( v==0 ){.   
1b3cc 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20   /* If there is 
1b3cd 6e 6f 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e  no statement han
1b3ce 64 6c 65 2c 20 74 68 65 6e 20 74 68 65 20 62 6c  dle, then the bl
1b3cf 6f 62 2d 68 61 6e 64 6c 65 20 68 61 73 0a 20 20  ob-handle has.  
1b3d0 20 20 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65    ** already bee
1b3d1 6e 20 69 6e 76 61 6c 69 64 61 74 65 64 2e 20 52  n invalidated. R
1b3d2 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 42 4f  eturn SQLITE_ABO
1b3d3 52 54 20 69 6e 20 74 68 69 73 20 63 61 73 65 2e  RT in this case.
1b3d4 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 63 20 3d  .    */.    rc =
1b3d5 20 53 51 4c 49 54 45 5f 41 42 4f 52 54 3b 0a 20   SQLITE_ABORT;. 
1b3d6 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 43   }else{.    /* C
1b3d7 61 6c 6c 20 65 69 74 68 65 72 20 42 74 72 65 65  all either Btree
1b3d8 44 61 74 61 28 29 20 6f 72 20 42 74 72 65 65 50  Data() or BtreeP
1b3d9 75 74 44 61 74 61 28 29 2e 20 49 66 20 53 51 4c  utData(). If SQL
1b3da 49 54 45 5f 41 42 4f 52 54 20 69 73 0a 20 20 20  ITE_ABORT is.   
1b3db 20 2a 2a 20 72 65 74 75 72 6e 65 64 2c 20 63 6c   ** returned, cl
1b3dc 65 61 6e 2d 75 70 20 74 68 65 20 73 74 61 74 65  ean-up the state
1b3dd 6d 65 6e 74 20 68 61 6e 64 6c 65 2e 0a 20 20 20  ment handle..   
1b3de 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28 20   */.    assert( 
1b3df 64 62 20 3d 3d 20 76 2d 3e 64 62 20 29 3b 0a 20  db == v->db );. 
1b3e0 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 45     sqlite3BtreeE
1b3e1 6e 74 65 72 43 75 72 73 6f 72 28 70 2d 3e 70 43  nterCursor(p->pC
1b3e2 73 72 29 3b 0a 20 20 20 20 72 63 20 3d 20 78 43  sr);.    rc = xC
1b3e3 61 6c 6c 28 70 2d 3e 70 43 73 72 2c 20 69 4f 66  all(p->pCsr, iOf
1b3e4 66 73 65 74 2b 70 2d 3e 69 4f 66 66 73 65 74 2c  fset+p->iOffset,
1b3e5 20 6e 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69   n, z);.    sqli
1b3e6 74 65 33 42 74 72 65 65 4c 65 61 76 65 43 75 72  te3BtreeLeaveCur
1b3e7 73 6f 72 28 70 2d 3e 70 43 73 72 29 3b 0a 20 20  sor(p->pCsr);.  
1b3e8 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
1b3e9 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20 20 20 20  _ABORT ){.      
1b3ea 73 71 6c 69 74 65 33 56 64 62 65 46 69 6e 61 6c  sqlite3VdbeFinal
1b3eb 69 7a 65 28 76 29 3b 0a 20 20 20 20 20 20 70 2d  ize(v);.      p-
1b3ec 3e 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20  >pStmt = 0;.    
1b3ed 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 62 2d  }else{.      db-
1b3ee 3e 65 72 72 43 6f 64 65 20 3d 20 72 63 3b 0a 20  >errCode = rc;. 
1b3ef 20 20 20 20 20 76 2d 3e 72 63 20 3d 20 72 63 3b       v->rc = rc;
1b3f0 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 63 20  .    }.  }.  rc 
1b3f1 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69 74  = sqlite3ApiExit
1b3f2 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71 6c 69  (db, rc);.  sqli
1b3f3 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28  te3_mutex_leave(
1b3f4 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65  db->mutex);.  re
1b3f5 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1b3f6 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f 6d  * Read data from
1b3f7 20 61 20 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a   a blob handle..
1b3f8 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
1b3f9 74 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72  t sqlite3_blob_r
1b3fa 65 61 64 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ead(sqlite3_blob
1b3fb 20 2a 70 42 6c 6f 62 2c 20 76 6f 69 64 20 2a 7a   *pBlob, void *z
1b3fc 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 69 4f 66  , int n, int iOf
1b3fd 66 73 65 74 29 7b 0a 20 20 72 65 74 75 72 6e 20  fset){.  return 
1b3fe 62 6c 6f 62 52 65 61 64 57 72 69 74 65 28 70 42  blobReadWrite(pB
1b3ff 6c 6f 62 2c 20 7a 2c 20 6e 2c 20 69 4f 66 66 73  lob, z, n, iOffs
1b400 65 74 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  et, sqlite3Btree
1b401 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Data);.}../*.** 
1b402 57 72 69 74 65 20 64 61 74 61 20 74 6f 20 61 20  Write data to a 
1b403 62 6c 6f 62 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a  blob handle..*/.
1b404 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b405 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
1b406 65 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  e(sqlite3_blob *
1b407 70 42 6c 6f 62 2c 20 63 6f 6e 73 74 20 76 6f 69  pBlob, const voi
1b408 64 20 2a 7a 2c 20 69 6e 74 20 6e 2c 20 69 6e 74  d *z, int n, int
1b409 20 69 4f 66 66 73 65 74 29 7b 0a 20 20 72 65 74   iOffset){.  ret
1b40a 75 72 6e 20 62 6c 6f 62 52 65 61 64 57 72 69 74  urn blobReadWrit
1b40b 65 28 70 42 6c 6f 62 2c 20 28 76 6f 69 64 20 2a  e(pBlob, (void *
1b40c 29 7a 2c 20 6e 2c 20 69 4f 66 66 73 65 74 2c 20  )z, n, iOffset, 
1b40d 73 71 6c 69 74 65 33 42 74 72 65 65 50 75 74 44  sqlite3BtreePutD
1b40e 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  ata);.}../*.** Q
1b40f 75 65 72 79 20 61 20 62 6c 6f 62 20 68 61 6e 64  uery a blob hand
1b410 6c 65 20 66 6f 72 20 74 68 65 20 73 69 7a 65 20  le for the size 
1b411 6f 66 20 74 68 65 20 64 61 74 61 2e 0a 2a 2a 0a  of the data..**.
1b412 2a 2a 20 54 68 65 20 49 6e 63 72 62 6c 6f 62 2e  ** The Incrblob.
1b413 6e 42 79 74 65 20 66 69 65 6c 64 20 69 73 20 66  nByte field is f
1b414 69 78 65 64 20 66 6f 72 20 74 68 65 20 6c 69 66  ixed for the lif
1b415 65 74 69 6d 65 20 6f 66 20 74 68 65 20 49 6e 63  etime of the Inc
1b416 72 62 6c 6f 62 0a 2a 2a 20 73 6f 20 6e 6f 20 6d  rblob.** so no m
1b417 75 74 65 78 20 69 73 20 72 65 71 75 69 72 65 64  utex is required
1b418 20 66 6f 72 20 61 63 63 65 73 73 2e 0a 2a 2f 0a   for access..*/.
1b419 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
1b41a 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74 65  qlite3_blob_byte
1b41b 73 28 73 71 6c 69 74 65 33 5f 62 6c 6f 62 20 2a  s(sqlite3_blob *
1b41c 70 42 6c 6f 62 29 7b 0a 20 20 49 6e 63 72 62 6c  pBlob){.  Incrbl
1b41d 6f 62 20 2a 70 20 3d 20 28 49 6e 63 72 62 6c 6f  ob *p = (Incrblo
1b41e 62 20 2a 29 70 42 6c 6f 62 3b 0a 20 20 72 65 74  b *)pBlob;.  ret
1b41f 75 72 6e 20 70 2d 3e 6e 42 79 74 65 3b 0a 7d 0a  urn p->nByte;.}.
1b420 0a 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64  .#endif /* #ifnd
1b421 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 49  ef SQLITE_OMIT_I
1b422 4e 43 52 42 4c 4f 42 20 2a 2f 0a 0a 2f 2a 2a 2a  NCRBLOB */../***
1b423 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1b424 6f 66 20 76 64 62 65 62 6c 6f 62 2e 63 20 2a 2a  of vdbeblob.c **
1b425 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b426 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b427 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1b428 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1b429 6e 20 66 69 6c 65 20 6a 6f 75 72 6e 61 6c 2e 63  n file journal.c
1b42a 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
1b42b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b42c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1b42d 2a 20 32 30 30 37 20 41 75 67 75 73 74 20 32 32  * 2007 August 22
1b42e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1b42f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1b430 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1b431 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1b432 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1b433 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1b434 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1b435 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1b436 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1b437 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1b438 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1b439 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1b43a 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1b43b 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1b43c 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1b43d 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1b43e 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1b43f 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1b440 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b441 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b442 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b443 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1b444 2a 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20  **.** @(#) $Id: 
1b445 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 38 20  journal.c,v 1.8 
1b446 32 30 30 38 2f 30 35 2f 30 31 20 31 38 3a 30 31  2008/05/01 18:01
1b447 3a 34 37 20 64 72 68 20 45 78 70 20 24 0a 2a 2f  :47 drh Exp $.*/
1b448 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
1b449 45 4e 41 42 4c 45 5f 41 54 4f 4d 49 43 5f 57 52  ENABLE_ATOMIC_WR
1b44a 49 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ITE../*.** This 
1b44b 66 69 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20  file implements 
1b44c 61 20 73 70 65 63 69 61 6c 20 6b 69 6e 64 20 6f  a special kind o
1b44d 66 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f  f sqlite3_file o
1b44e 62 6a 65 63 74 20 75 73 65 64 0a 2a 2a 20 62 79  bject used.** by
1b44f 20 53 51 4c 69 74 65 20 74 6f 20 63 72 65 61 74   SQLite to creat
1b450 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 73 20  e journal files 
1b451 69 66 20 74 68 65 20 61 74 6f 6d 69 63 2d 77 72  if the atomic-wr
1b452 69 74 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  ite optimization
1b453 0a 2a 2a 20 69 73 20 65 6e 61 62 6c 65 64 2e 0a  .** is enabled..
1b454 2a 2a 0a 2a 2a 20 54 68 65 20 64 69 73 74 69 6e  **.** The distin
1b455 63 74 69 76 65 20 63 68 61 72 61 63 74 65 72 69  ctive characteri
1b456 73 74 69 63 20 6f 66 20 74 68 69 73 20 73 71 6c  stic of this sql
1b457 69 74 65 33 5f 66 69 6c 65 20 69 73 20 74 68 61  ite3_file is tha
1b458 74 20 74 68 65 0a 2a 2a 20 61 63 74 75 61 6c 20  t the.** actual 
1b459 6f 6e 20 64 69 73 6b 20 66 69 6c 65 20 69 73 20  on disk file is 
1b45a 63 72 65 61 74 65 64 20 6c 61 7a 69 6c 79 2e 20  created lazily. 
1b45b 57 68 65 6e 20 74 68 65 20 66 69 6c 65 20 69 73  When the file is
1b45c 20 63 72 65 61 74 65 64 2c 0a 2a 2a 20 74 68 65   created,.** the
1b45d 20 63 61 6c 6c 65 72 20 73 70 65 63 69 66 69 65   caller specifie
1b45e 73 20 61 20 62 75 66 66 65 72 20 73 69 7a 65 20  s a buffer size 
1b45f 66 6f 72 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  for an in-memory
1b460 20 62 75 66 66 65 72 20 74 6f 0a 2a 2a 20 62 65   buffer to.** be
1b461 20 75 73 65 64 20 74 6f 20 73 65 72 76 69 63 65   used to service
1b462 20 72 65 61 64 28 29 20 61 6e 64 20 77 72 69 74   read() and writ
1b463 65 28 29 20 72 65 71 75 65 73 74 73 2e 20 54 68  e() requests. Th
1b464 65 20 61 63 74 75 61 6c 20 66 69 6c 65 0a 2a 2a  e actual file.**
1b465 20 6f 6e 20 64 69 73 6b 20 69 73 20 6e 6f 74 20   on disk is not 
1b466 63 72 65 61 74 65 64 20 6f 72 20 70 6f 70 75 6c  created or popul
1b467 61 74 65 64 20 75 6e 74 69 6c 20 65 69 74 68 65  ated until eithe
1b468 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 29 20 54 68  r:.**.**   1) Th
1b469 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 65 70 72  e in-memory repr
1b46a 65 73 65 6e 74 61 74 69 6f 6e 20 67 72 6f 77 73  esentation grows
1b46b 20 74 6f 6f 20 6c 61 72 67 65 20 66 6f 72 20 74   too large for t
1b46c 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 0a 2a 2a  he allocated .**
1b46d 20 20 20 20 20 20 62 75 66 66 65 72 2c 20 6f 72        buffer, or
1b46e 0a 2a 2a 20 20 20 32 29 20 54 68 65 20 78 53 79  .**   2) The xSy
1b46f 6e 63 28 29 20 6d 65 74 68 6f 64 20 69 73 20 63  nc() method is c
1b470 61 6c 6c 65 64 2e 0a 2a 2f 0a 0a 0a 0a 2f 2a 0a  alled..*/..../*.
1b471 2a 2a 20 41 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ** A JournalFile
1b472 20 6f 62 6a 65 63 74 20 69 73 20 61 20 73 75 62   object is a sub
1b473 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65 33  class of sqlite3
1b474 5f 66 69 6c 65 20 75 73 65 64 20 62 79 0a 2a 2a  _file used by.**
1b475 20 61 73 20 61 6e 20 6f 70 65 6e 20 66 69 6c 65   as an open file
1b476 20 68 61 6e 64 6c 65 20 66 6f 72 20 6a 6f 75 72   handle for jour
1b477 6e 61 6c 20 66 69 6c 65 73 2e 0a 2a 2f 0a 73 74  nal files..*/.st
1b478 72 75 63 74 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  ruct JournalFile
1b479 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6f 5f   {.  sqlite3_io_
1b47a 6d 65 74 68 6f 64 73 20 2a 70 4d 65 74 68 6f 64  methods *pMethod
1b47b 3b 20 20 20 20 2f 2a 20 49 2f 4f 20 6d 65 74 68  ;    /* I/O meth
1b47c 6f 64 73 20 6f 6e 20 6a 6f 75 72 6e 61 6c 20 66  ods on journal f
1b47d 69 6c 65 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  iles */.  int nB
1b47e 75 66 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  uf;             
1b47f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
1b480 65 20 6f 66 20 7a 42 75 66 5b 5d 20 69 6e 20 62  e of zBuf[] in b
1b481 79 74 65 73 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ytes */.  char *
1b482 7a 42 75 66 3b 20 20 20 20 20 20 20 20 20 20 20  zBuf;           
1b483 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61            /* Spa
1b484 63 65 20 74 6f 20 62 75 66 66 65 72 20 6a 6f 75  ce to buffer jou
1b485 72 6e 61 6c 20 77 72 69 74 65 73 20 2a 2f 0a 20  rnal writes */. 
1b486 20 69 6e 74 20 69 53 69 7a 65 3b 20 20 20 20 20   int iSize;     
1b487 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b488 20 2f 2a 20 41 6d 6f 75 6e 74 20 6f 66 20 7a 42   /* Amount of zB
1b489 75 66 5b 5d 20 63 75 72 72 65 6e 74 6c 79 20 75  uf[] currently u
1b48a 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 66 6c 61  sed */.  int fla
1b48b 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  gs;             
1b48c 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65           /* xOpe
1b48d 6e 20 66 6c 61 67 73 20 2a 2f 0a 20 20 73 71 6c  n flags */.  sql
1b48e 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 3b 20  ite3_vfs *pVfs; 
1b48f 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1b490 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64 65 72  The "real" under
1b491 6c 79 69 6e 67 20 56 46 53 20 2a 2f 0a 20 20 73  lying VFS */.  s
1b492 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52 65  qlite3_file *pRe
1b493 61 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  al;            /
1b494 2a 20 54 68 65 20 22 72 65 61 6c 22 20 75 6e 64  * The "real" und
1b495 65 72 6c 79 69 6e 67 20 66 69 6c 65 20 64 65 73  erlying file des
1b496 63 72 69 70 74 6f 72 20 2a 2f 0a 20 20 63 6f 6e  criptor */.  con
1b497 73 74 20 63 68 61 72 20 2a 7a 4a 6f 75 72 6e 61  st char *zJourna
1b498 6c 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  l;           /* 
1b499 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a 6f 75 72  Name of the jour
1b49a 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 7d 3b 0a 74  nal file */.};.t
1b49b 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4a 6f  ypedef struct Jo
1b49c 75 72 6e 61 6c 46 69 6c 65 20 4a 6f 75 72 6e 61  urnalFile Journa
1b49d 6c 46 69 6c 65 3b 0a 0a 2f 2a 0a 2a 2a 20 49 66  lFile;../*.** If
1b49e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 61 6c 72   it does not alr
1b49f 65 61 64 79 20 65 78 69 73 74 73 2c 20 63 72 65  eady exists, cre
1b4a0 61 74 65 20 61 6e 64 20 70 6f 70 75 6c 61 74 65  ate and populate
1b4a1 20 74 68 65 20 6f 6e 2d 64 69 73 6b 20 66 69 6c   the on-disk fil
1b4a2 65 20 0a 2a 2a 20 66 6f 72 20 4a 6f 75 72 6e 61  e .** for Journa
1b4a3 6c 46 69 6c 65 20 70 2e 0a 2a 2f 0a 73 74 61 74  lFile p..*/.stat
1b4a4 69 63 20 69 6e 74 20 63 72 65 61 74 65 46 69 6c  ic int createFil
1b4a5 65 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70  e(JournalFile *p
1b4a6 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1b4a7 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28 20 21  LITE_OK;.  if( !
1b4a8 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20 20  p->pReal ){.    
1b4a9 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 52  sqlite3_file *pR
1b4aa 65 61 6c 20 3d 20 28 73 71 6c 69 74 65 33 5f 66  eal = (sqlite3_f
1b4ab 69 6c 65 20 2a 29 26 70 5b 31 5d 3b 0a 20 20 20  ile *)&p[1];.   
1b4ac 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 4f   rc = sqlite3OsO
1b4ad 70 65 6e 28 70 2d 3e 70 56 66 73 2c 20 70 2d 3e  pen(p->pVfs, p->
1b4ae 7a 4a 6f 75 72 6e 61 6c 2c 20 70 52 65 61 6c 2c  zJournal, pReal,
1b4af 20 70 2d 3e 66 6c 61 67 73 2c 20 30 29 3b 0a 20   p->flags, 0);. 
1b4b0 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1b4b1 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 2d  E_OK ){.      p-
1b4b2 3e 70 52 65 61 6c 20 3d 20 70 52 65 61 6c 3b 0a  >pReal = pReal;.
1b4b3 20 20 20 20 20 20 69 66 28 20 70 2d 3e 69 53 69        if( p->iSi
1b4b4 7a 65 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  ze>0 ){.        
1b4b5 61 73 73 65 72 74 28 70 2d 3e 69 53 69 7a 65 3c  assert(p->iSize<
1b4b6 3d 70 2d 3e 6e 42 75 66 29 3b 0a 20 20 20 20 20  =p->nBuf);.     
1b4b7 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1b4b8 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c  sWrite(p->pReal,
1b4b9 20 70 2d 3e 7a 42 75 66 2c 20 70 2d 3e 69 53 69   p->zBuf, p->iSi
1b4ba 7a 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 7d 0a  ze, 0);.      }.
1b4bb 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1b4bc 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
1b4bd 43 6c 6f 73 65 20 74 68 65 20 66 69 6c 65 2e 0a  Close the file..
1b4be 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6a 72  */.static int jr
1b4bf 6e 6c 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  nlClose(sqlite3_
1b4c0 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4a  file *pJfd){.  J
1b4c1 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20 3d 20  ournalFile *p = 
1b4c2 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1b4c3 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e 70 52  Jfd;.  if( p->pR
1b4c4 65 61 6c 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  eal ){.    sqlit
1b4c5 65 33 4f 73 43 6c 6f 73 65 28 70 2d 3e 70 52 65  e3OsClose(p->pRe
1b4c6 61 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  al);.  }.  sqlit
1b4c7 65 33 5f 66 72 65 65 28 70 2d 3e 7a 42 75 66 29  e3_free(p->zBuf)
1b4c8 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
1b4c9 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  E_OK;.}../*.** R
1b4ca 65 61 64 20 64 61 74 61 20 66 72 6f 6d 20 74 68  ead data from th
1b4cb 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69  e file..*/.stati
1b4cc 63 20 69 6e 74 20 6a 72 6e 6c 52 65 61 64 28 0a  c int jrnlRead(.
1b4cd 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a    sqlite3_file *
1b4ce 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20  pJfd,    /* The 
1b4cf 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 66 72 6f  journal file fro
1b4d0 6d 20 77 68 69 63 68 20 74 6f 20 72 65 61 64 20  m which to read 
1b4d1 2a 2f 0a 20 20 76 6f 69 64 20 2a 7a 42 75 66 2c  */.  void *zBuf,
1b4d2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1b4d3 75 74 20 74 68 65 20 72 65 73 75 6c 74 73 20 68  ut the results h
1b4d4 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41 6d  ere */.  int iAm
1b4d5 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t,              
1b4d6 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
1b4d7 65 73 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20 20  es to read */.  
1b4d8 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4f 66  sqlite_int64 iOf
1b4d9 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 20  st     /* Begin 
1b4da 72 65 61 64 69 6e 67 20 61 74 20 74 68 69 73 20  reading at this 
1b4db 6f 66 66 73 65 74 20 2a 2f 0a 29 7b 0a 20 20 69  offset */.){.  i
1b4dc 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1b4dd 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  K;.  JournalFile
1b4de 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
1b4df 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28  le *)pJfd;.  if(
1b4e0 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20   p->pReal ){.   
1b4e1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 52   rc = sqlite3OsR
1b4e2 65 61 64 28 70 2d 3e 70 52 65 61 6c 2c 20 7a 42  ead(p->pReal, zB
1b4e3 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66 73 74 29  uf, iAmt, iOfst)
1b4e4 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61  ;.  }else{.    a
1b4e5 73 73 65 72 74 28 20 69 41 6d 74 2b 69 4f 66 73  ssert( iAmt+iOfs
1b4e6 74 3c 3d 70 2d 3e 69 53 69 7a 65 20 29 3b 0a 20  t<=p->iSize );. 
1b4e7 20 20 20 6d 65 6d 63 70 79 28 7a 42 75 66 2c 20     memcpy(zBuf, 
1b4e8 26 70 2d 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c  &p->zBuf[iOfst],
1b4e9 20 69 41 6d 74 29 3b 0a 20 20 7d 0a 20 20 72 65   iAmt);.  }.  re
1b4ea 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
1b4eb 2a 20 57 72 69 74 65 20 64 61 74 61 20 74 6f 20  * Write data to 
1b4ec 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1b4ed 74 69 63 20 69 6e 74 20 6a 72 6e 6c 57 72 69 74  tic int jrnlWrit
1b4ee 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 66 69 6c  e(.  sqlite3_fil
1b4ef 65 20 2a 70 4a 66 64 2c 20 20 20 20 2f 2a 20 54  e *pJfd,    /* T
1b4f0 68 65 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20  he journal file 
1b4f1 69 6e 74 6f 20 77 68 69 63 68 20 74 6f 20 77 72  into which to wr
1b4f2 69 74 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76  ite */.  const v
1b4f3 6f 69 64 20 2a 7a 42 75 66 2c 20 20 20 20 20 20  oid *zBuf,      
1b4f4 2f 2a 20 54 61 6b 65 20 64 61 74 61 20 74 6f 20  /* Take data to 
1b4f5 62 65 20 77 72 69 74 74 65 6e 20 66 72 6f 6d 20  be written from 
1b4f6 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 69 41  here */.  int iA
1b4f7 6d 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  mt,             
1b4f8 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79   /* Number of by
1b4f9 74 65 73 20 74 6f 20 77 72 69 74 65 20 2a 2f 0a  tes to write */.
1b4fa 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
1b4fb 4f 66 73 74 20 20 20 20 20 2f 2a 20 42 65 67 69  Ofst     /* Begi
1b4fc 6e 20 77 72 69 74 69 6e 67 20 61 74 20 74 68 69  n writing at thi
1b4fd 73 20 6f 66 66 73 65 74 20 69 6e 74 6f 20 74 68  s offset into th
1b4fe 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69  e file */.){.  i
1b4ff 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1b500 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  K;.  JournalFile
1b501 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
1b502 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28  le *)pJfd;.  if(
1b503 20 21 70 2d 3e 70 52 65 61 6c 20 26 26 20 28 69   !p->pReal && (i
1b504 4f 66 73 74 2b 69 41 6d 74 29 3e 70 2d 3e 6e 42  Ofst+iAmt)>p->nB
1b505 75 66 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 63  uf ){.    rc = c
1b506 72 65 61 74 65 46 69 6c 65 28 70 29 3b 0a 20 20  reateFile(p);.  
1b507 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  }.  if( rc==SQLI
1b508 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28  TE_OK ){.    if(
1b509 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20   p->pReal ){.   
1b50a 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1b50b 73 57 72 69 74 65 28 70 2d 3e 70 52 65 61 6c 2c  sWrite(p->pReal,
1b50c 20 7a 42 75 66 2c 20 69 41 6d 74 2c 20 69 4f 66   zBuf, iAmt, iOf
1b50d 73 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  st);.    }else{.
1b50e 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d        memcpy(&p-
1b50f 3e 7a 42 75 66 5b 69 4f 66 73 74 5d 2c 20 7a 42  >zBuf[iOfst], zB
1b510 75 66 2c 20 69 41 6d 74 29 3b 0a 20 20 20 20 20  uf, iAmt);.     
1b511 20 69 66 28 20 70 2d 3e 69 53 69 7a 65 3c 28 69   if( p->iSize<(i
1b512 4f 66 73 74 2b 69 41 6d 74 29 20 29 7b 0a 20 20  Ofst+iAmt) ){.  
1b513 20 20 20 20 20 20 70 2d 3e 69 53 69 7a 65 20 3d        p->iSize =
1b514 20 28 69 4f 66 73 74 2b 69 41 6d 74 29 3b 0a 20   (iOfst+iAmt);. 
1b515 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
1b516 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
1b517 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74 65 20  ./*.** Truncate 
1b518 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73 74 61  the file..*/.sta
1b519 74 69 63 20 69 6e 74 20 6a 72 6e 6c 54 72 75 6e  tic int jrnlTrun
1b51a 63 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69 6c  cate(sqlite3_fil
1b51b 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65 5f  e *pJfd, sqlite_
1b51c 69 6e 74 36 34 20 73 69 7a 65 29 7b 0a 20 20 69  int64 size){.  i
1b51d 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
1b51e 4b 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65  K;.  JournalFile
1b51f 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69   *p = (JournalFi
1b520 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69 66 28  le *)pJfd;.  if(
1b521 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20 20 20   p->pReal ){.   
1b522 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f 73 54   rc = sqlite3OsT
1b523 72 75 6e 63 61 74 65 28 70 2d 3e 70 52 65 61 6c  runcate(p->pReal
1b524 2c 20 73 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65  , size);.  }else
1b525 20 69 66 28 20 73 69 7a 65 3c 70 2d 3e 69 53 69   if( size<p->iSi
1b526 7a 65 20 29 7b 0a 20 20 20 20 70 2d 3e 69 53 69  ze ){.    p->iSi
1b527 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 7d 0a 20  ze = size;.  }. 
1b528 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
1b529 2a 0a 2a 2a 20 53 79 6e 63 20 74 68 65 20 66 69  *.** Sync the fi
1b52a 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
1b52b 74 20 6a 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74  t jrnlSync(sqlit
1b52c 65 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 69  e3_file *pJfd, i
1b52d 6e 74 20 66 6c 61 67 73 29 7b 0a 20 20 69 6e 74  nt flags){.  int
1b52e 20 72 63 3b 0a 20 20 4a 6f 75 72 6e 61 6c 46 69   rc;.  JournalFi
1b52f 6c 65 20 2a 70 20 3d 20 28 4a 6f 75 72 6e 61 6c  le *p = (Journal
1b530 46 69 6c 65 20 2a 29 70 4a 66 64 3b 0a 20 20 69  File *)pJfd;.  i
1b531 66 28 20 70 2d 3e 70 52 65 61 6c 20 29 7b 0a 20  f( p->pReal ){. 
1b532 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 4f     rc = sqlite3O
1b533 73 53 79 6e 63 28 70 2d 3e 70 52 65 61 6c 2c 20  sSync(p->pReal, 
1b534 66 6c 61 67 73 29 3b 0a 20 20 7d 65 6c 73 65 7b  flags);.  }else{
1b535 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
1b536 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  _OK;.  }.  retur
1b537 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 51  n rc;.}../*.** Q
1b538 75 65 72 79 20 74 68 65 20 73 69 7a 65 20 6f 66  uery the size of
1b539 20 74 68 65 20 66 69 6c 65 20 69 6e 20 62 79 74   the file in byt
1b53a 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  es..*/.static in
1b53b 74 20 6a 72 6e 6c 46 69 6c 65 53 69 7a 65 28 73  t jrnlFileSize(s
1b53c 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a 66  qlite3_file *pJf
1b53d 64 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  d, sqlite_int64 
1b53e 2a 70 53 69 7a 65 29 7b 0a 20 20 69 6e 74 20 72  *pSize){.  int r
1b53f 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
1b540 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70 20   JournalFile *p 
1b541 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a  = (JournalFile *
1b542 29 70 4a 66 64 3b 0a 20 20 69 66 28 20 70 2d 3e  )pJfd;.  if( p->
1b543 70 52 65 61 6c 20 29 7b 0a 20 20 20 20 72 63 20  pReal ){.    rc 
1b544 3d 20 73 71 6c 69 74 65 33 4f 73 46 69 6c 65 53  = sqlite3OsFileS
1b545 69 7a 65 28 70 2d 3e 70 52 65 61 6c 2c 20 70 53  ize(p->pReal, pS
1b546 69 7a 65 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ize);.  }else{. 
1b547 20 20 20 2a 70 53 69 7a 65 20 3d 20 28 73 71 6c     *pSize = (sql
1b548 69 74 65 5f 69 6e 74 36 34 29 20 70 2d 3e 69 53  ite_int64) p->iS
1b549 69 7a 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ize;.  }.  retur
1b54a 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
1b54b 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f 64 73 20  able of methods 
1b54c 66 6f 72 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20  for JournalFile 
1b54d 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f 62 6a  sqlite3_file obj
1b54e 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 73  ect..*/.static s
1b54f 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6f  truct sqlite3_io
1b550 5f 6d 65 74 68 6f 64 73 20 4a 6f 75 72 6e 61 6c  _methods Journal
1b551 46 69 6c 65 4d 65 74 68 6f 64 73 20 3d 20 7b 0a  FileMethods = {.
1b552 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20 20    1,            
1b553 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a   /* iVersion */.
1b554 20 20 6a 72 6e 6c 43 6c 6f 73 65 2c 20 20 20 20    jrnlClose,    
1b555 20 2f 2a 20 78 43 6c 6f 73 65 20 2a 2f 0a 20 20   /* xClose */.  
1b556 6a 72 6e 6c 52 65 61 64 2c 20 20 20 20 20 20 2f  jrnlRead,      /
1b557 2a 20 78 52 65 61 64 20 2a 2f 0a 20 20 6a 72 6e  * xRead */.  jrn
1b558 6c 57 72 69 74 65 2c 20 20 20 20 20 2f 2a 20 78  lWrite,     /* x
1b559 57 72 69 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c 54  Write */.  jrnlT
1b55a 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72  runcate,  /* xTr
1b55b 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6a 72 6e 6c  uncate */.  jrnl
1b55c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a 20 78 53  Sync,      /* xS
1b55d 79 6e 63 20 2a 2f 0a 20 20 6a 72 6e 6c 46 69 6c  ync */.  jrnlFil
1b55e 65 53 69 7a 65 2c 20 20 2f 2a 20 78 46 69 6c 65  eSize,  /* xFile
1b55f 53 69 7a 65 20 2a 2f 0a 20 20 30 2c 20 20 20 20  Size */.  0,    
1b560 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4c 6f 63           /* xLoc
1b561 6b 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20  k */.  0,       
1b562 20 20 20 20 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b        /* xUnlock
1b563 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1b564 20 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65       /* xCheckRe
1b565 73 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20  servedLock */.  
1b566 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0,             /
1b567 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c 20 2a  * xFileControl *
1b568 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20 20 20  /.  0,          
1b569 20 20 20 2f 2a 20 78 53 65 63 74 6f 72 53 69 7a     /* xSectorSiz
1b56a 65 20 2a 2f 0a 20 20 30 20 20 20 20 20 20 20 20  e */.  0        
1b56b 20 20 20 20 20 20 2f 2a 20 78 44 65 76 69 63 65        /* xDevice
1b56c 43 68 61 72 61 63 74 65 72 69 73 74 69 63 73 20  Characteristics 
1b56d 2a 2f 0a 7d 3b 0a 0a 2f 2a 20 0a 2a 2a 20 4f 70  */.};../* .** Op
1b56e 65 6e 20 61 20 6a 6f 75 72 6e 61 6c 20 66 69 6c  en a journal fil
1b56f 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1b570 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1b571 4a 6f 75 72 6e 61 6c 4f 70 65 6e 28 0a 20 20 73  JournalOpen(.  s
1b572 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1b573 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ,         /* The
1b574 20 56 46 53 20 74 6f 20 75 73 65 20 66 6f 72 20   VFS to use for 
1b575 61 63 74 75 61 6c 20 66 69 6c 65 20 49 2f 4f 20  actual file I/O 
1b576 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
1b577 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20 20 20 20  *zName,         
1b578 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 6a  /* Name of the j
1b579 6f 75 72 6e 61 6c 20 66 69 6c 65 20 2a 2f 0a 20  ournal file */. 
1b57a 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70   sqlite3_file *p
1b57b 4a 66 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 50  Jfd,        /* P
1b57c 72 65 61 6c 6c 6f 63 61 74 65 64 2c 20 62 6c 61  reallocated, bla
1b57d 6e 6b 20 66 69 6c 65 20 68 61 6e 64 6c 65 20 2a  nk file handle *
1b57e 2f 0a 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20  /.  int flags,  
1b57f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1b580 2a 20 4f 70 65 6e 69 6e 67 20 66 6c 61 67 73 20  * Opening flags 
1b581 2a 2f 0a 20 20 69 6e 74 20 6e 42 75 66 20 20 20  */.  int nBuf   
1b582 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b583 2f 2a 20 42 79 74 65 73 20 62 75 66 66 65 72 65  /* Bytes buffere
1b584 64 20 62 65 66 6f 72 65 20 6f 70 65 6e 69 6e 67  d before opening
1b585 20 74 68 65 20 66 69 6c 65 20 2a 2f 0a 29 7b 0a   the file */.){.
1b586 20 20 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 70    JournalFile *p
1b587 20 3d 20 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20   = (JournalFile 
1b588 2a 29 70 4a 66 64 3b 0a 20 20 6d 65 6d 73 65 74  *)pJfd;.  memset
1b589 28 70 2c 20 30 2c 20 73 71 6c 69 74 65 33 4a 6f  (p, 0, sqlite3Jo
1b58a 75 72 6e 61 6c 53 69 7a 65 28 70 56 66 73 29 29  urnalSize(pVfs))
1b58b 3b 0a 20 20 69 66 28 20 6e 42 75 66 3e 30 20 29  ;.  if( nBuf>0 )
1b58c 7b 0a 20 20 20 20 70 2d 3e 7a 42 75 66 20 3d 20  {.    p->zBuf = 
1b58d 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 5a 65 72  sqlite3MallocZer
1b58e 6f 28 6e 42 75 66 29 3b 0a 20 20 20 20 69 66 28  o(nBuf);.    if(
1b58f 20 21 70 2d 3e 7a 42 75 66 20 29 7b 0a 20 20 20   !p->zBuf ){.   
1b590 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b591 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20  _NOMEM;.    }.  
1b592 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72  }else{.    retur
1b593 6e 20 73 71 6c 69 74 65 33 4f 73 4f 70 65 6e 28  n sqlite3OsOpen(
1b594 70 56 66 73 2c 20 7a 4e 61 6d 65 2c 20 70 4a 66  pVfs, zName, pJf
1b595 64 2c 20 66 6c 61 67 73 2c 20 30 29 3b 0a 20 20  d, flags, 0);.  
1b596 7d 0a 20 20 70 2d 3e 70 4d 65 74 68 6f 64 20 3d  }.  p->pMethod =
1b597 20 26 4a 6f 75 72 6e 61 6c 46 69 6c 65 4d 65 74   &JournalFileMet
1b598 68 6f 64 73 3b 0a 20 20 70 2d 3e 6e 42 75 66 20  hods;.  p->nBuf 
1b599 3d 20 6e 42 75 66 3b 0a 20 20 70 2d 3e 66 6c 61  = nBuf;.  p->fla
1b59a 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 2d  gs = flags;.  p-
1b59b 3e 7a 4a 6f 75 72 6e 61 6c 20 3d 20 7a 4e 61 6d  >zJournal = zNam
1b59c 65 3b 0a 20 20 70 2d 3e 70 56 66 73 20 3d 20 70  e;.  p->pVfs = p
1b59d 56 66 73 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  Vfs;.  return SQ
1b59e 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1b59f 2a 20 49 66 20 74 68 65 20 61 72 67 75 6d 65 6e  * If the argumen
1b5a0 74 20 70 20 70 6f 69 6e 74 73 20 74 6f 20 61 20  t p points to a 
1b5a1 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 73 74 72 75  JournalFile stru
1b5a2 63 74 75 72 65 2c 20 61 6e 64 20 74 68 65 20 75  cture, and the u
1b5a3 6e 64 65 72 6c 79 69 6e 67 0a 2a 2a 20 66 69 6c  nderlying.** fil
1b5a4 65 20 68 61 73 20 6e 6f 74 20 79 65 74 20 62 65  e has not yet be
1b5a5 65 6e 20 63 72 65 61 74 65 64 2c 20 63 72 65 61  en created, crea
1b5a6 74 65 20 69 74 20 6e 6f 77 2e 0a 2a 2f 0a 53 51  te it now..*/.SQ
1b5a7 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b5a8 20 73 71 6c 69 74 65 33 4a 6f 75 72 6e 61 6c 43   sqlite3JournalC
1b5a9 72 65 61 74 65 28 73 71 6c 69 74 65 33 5f 66 69  reate(sqlite3_fi
1b5aa 6c 65 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d  le *p){.  if( p-
1b5ab 3e 70 4d 65 74 68 6f 64 73 21 3d 26 4a 6f 75 72  >pMethods!=&Jour
1b5ac 6e 61 6c 46 69 6c 65 4d 65 74 68 6f 64 73 20 29  nalFileMethods )
1b5ad 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1b5ae 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 72 65  ITE_OK;.  }.  re
1b5af 74 75 72 6e 20 63 72 65 61 74 65 46 69 6c 65 28  turn createFile(
1b5b0 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 20 2a 29 70  (JournalFile *)p
1b5b1 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 65 74  );.}../* .** Ret
1b5b2 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72 20 6f  urn the number o
1b5b3 66 20 62 79 74 65 73 20 72 65 71 75 69 72 65 64  f bytes required
1b5b4 20 74 6f 20 73 74 6f 72 65 20 61 20 4a 6f 75 72   to store a Jour
1b5b5 6e 61 6c 46 69 6c 65 20 74 68 61 74 20 75 73 65  nalFile that use
1b5b6 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20 74 6f  s vfs.** pVfs to
1b5b7 20 63 72 65 61 74 65 20 74 68 65 20 75 6e 64 65   create the unde
1b5b8 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b 20 66  rlying on-disk f
1b5b9 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  iles..*/.SQLITE_
1b5ba 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1b5bb 74 65 33 4a 6f 75 72 6e 61 6c 53 69 7a 65 28 73  te3JournalSize(s
1b5bc 71 6c 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73  qlite3_vfs *pVfs
1b5bd 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 56 66  ){.  return (pVf
1b5be 73 2d 3e 73 7a 4f 73 46 69 6c 65 2b 73 69 7a 65  s->szOsFile+size
1b5bf 6f 66 28 4a 6f 75 72 6e 61 6c 46 69 6c 65 29 29  of(JournalFile))
1b5c0 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a  ;.}.#endif../***
1b5c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20  *********** End 
1b5c2 6f 66 20 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a  of journal.c ***
1b5c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5c5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
1b5c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
1b5c7 6e 20 66 69 6c 65 20 6d 65 6d 6a 6f 75 72 6e 61  n file memjourna
1b5c8 6c 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  l.c ************
1b5c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
1b5cb 2a 20 32 30 30 38 20 4f 63 74 6f 62 65 72 20 37  * 2008 October 7
1b5cc 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1b5cd 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1b5ce 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1b5cf 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1b5d0 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1b5d1 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1b5d2 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1b5d3 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1b5d4 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1b5d5 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1b5d6 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1b5d7 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1b5d8 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1b5d9 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1b5da 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1b5db 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1b5dc 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1b5dd 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1b5de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5df 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5e0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b5e1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1b5e2 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20  **.** This file 
1b5e3 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73  contains code us
1b5e4 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 61  e to implement a
1b5e5 6e 20 69 6e 2d 6d 65 6d 6f 72 79 20 72 6f 6c 6c  n in-memory roll
1b5e6 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a  back journal..**
1b5e7 20 54 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 72   The in-memory r
1b5e8 6f 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20  ollback journal 
1b5e9 69 73 20 75 73 65 64 20 74 6f 20 6a 6f 75 72 6e  is used to journ
1b5ea 61 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  al transactions 
1b5eb 66 6f 72 0a 2a 2a 20 22 3a 6d 65 6d 6f 72 79 3a  for.** ":memory:
1b5ec 22 20 64 61 74 61 62 61 73 65 73 20 61 6e 64 20  " databases and 
1b5ed 77 68 65 6e 20 74 68 65 20 6a 6f 75 72 6e 61 6c  when the journal
1b5ee 5f 6d 6f 64 65 3d 4d 45 4d 4f 52 59 20 70 72 61  _mode=MEMORY pra
1b5ef 67 6d 61 20 69 73 20 75 73 65 64 2e 0a 2a 2a 0a  gma is used..**.
1b5f0 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 6d 65 6d  ** @(#) $Id: mem
1b5f1 6a 6f 75 72 6e 61 6c 2e 63 2c 76 20 31 2e 35 20  journal.c,v 1.5 
1b5f2 32 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32  2008/11/19 16:52
1b5f3 3a 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :44 danielk1977 
1b5f4 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20 46 6f 72  Exp $.*/../* For
1b5f5 77 61 72 64 20 72 65 66 65 72 65 6e 63 65 73 20  ward references 
1b5f6 74 6f 20 69 6e 74 65 72 6e 61 6c 20 73 74 72 75  to internal stru
1b5f7 63 74 75 72 65 73 20 2a 2f 0a 74 79 70 65 64 65  ctures */.typede
1b5f8 66 20 73 74 72 75 63 74 20 4d 65 6d 4a 6f 75 72  f struct MemJour
1b5f9 6e 61 6c 20 4d 65 6d 4a 6f 75 72 6e 61 6c 3b 0a  nal MemJournal;.
1b5fa 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
1b5fb 69 6c 65 50 6f 69 6e 74 20 46 69 6c 65 50 6f 69  ilePoint FilePoi
1b5fc 6e 74 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  nt;.typedef stru
1b5fd 63 74 20 46 69 6c 65 43 68 75 6e 6b 20 46 69 6c  ct FileChunk Fil
1b5fe 65 43 68 75 6e 6b 3b 0a 0a 2f 2a 20 53 70 61 63  eChunk;../* Spac
1b5ff 65 20 74 6f 20 68 6f 6c 64 20 74 68 65 20 72 6f  e to hold the ro
1b600 6c 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69  llback journal i
1b601 73 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 69  s allocated in i
1b602 6e 63 72 65 6d 65 6e 74 73 20 6f 66 0a 2a 2a 20  ncrements of.** 
1b603 74 68 69 73 20 6d 61 6e 79 20 62 79 74 65 73 2e  this many bytes.
1b604 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 4a 4f 55 52  .*/.#define JOUR
1b605 4e 41 4c 5f 43 48 55 4e 4b 53 49 5a 45 20 31 30  NAL_CHUNKSIZE 10
1b606 32 34 0a 0a 2f 2a 20 4d 61 63 72 6f 20 74 6f 20  24../* Macro to 
1b607 66 69 6e 64 20 74 68 65 20 6d 69 6e 69 6d 75 6d  find the minimum
1b608 20 6f 66 20 74 77 6f 20 6e 75 6d 65 72 69 63 20   of two numeric 
1b609 76 61 6c 75 65 73 2e 0a 2a 2f 0a 23 69 66 6e 64  values..*/.#ifnd
1b60a 65 66 20 4d 49 4e 0a 23 20 64 65 66 69 6e 65 20  ef MIN.# define 
1b60b 4d 49 4e 28 78 2c 79 29 20 28 28 78 29 3c 28 79  MIN(x,y) ((x)<(y
1b60c 29 3f 28 78 29 3a 28 79 29 29 0a 23 65 6e 64 69  )?(x):(y)).#endi
1b60d 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 72 6f 6c  f../*.** The rol
1b60e 6c 62 61 63 6b 20 6a 6f 75 72 6e 61 6c 20 69 73  lback journal is
1b60f 20 63 6f 6d 70 6f 73 65 64 20 6f 66 20 61 20 6c   composed of a l
1b610 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66 20 74 68  inked list of th
1b611 65 73 65 20 73 74 72 75 63 74 75 72 65 73 2e 0a  ese structures..
1b612 2a 2f 0a 73 74 72 75 63 74 20 46 69 6c 65 43 68  */.struct FileCh
1b613 75 6e 6b 20 7b 0a 20 20 46 69 6c 65 43 68 75 6e  unk {.  FileChun
1b614 6b 20 2a 70 4e 65 78 74 3b 20 20 20 20 20 20 20  k *pNext;       
1b615 20 20 20 20 20 20 20 20 2f 2a 20 4e 65 78 74 20          /* Next 
1b616 63 68 75 6e 6b 20 69 6e 20 74 68 65 20 6a 6f 75  chunk in the jou
1b617 72 6e 61 6c 20 2a 2f 0a 20 20 75 38 20 7a 43 68  rnal */.  u8 zCh
1b618 75 6e 6b 5b 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e  unk[JOURNAL_CHUN
1b619 4b 53 49 5a 45 5d 3b 20 20 20 2f 2a 20 43 6f 6e  KSIZE];   /* Con
1b61a 74 65 6e 74 20 6f 66 20 74 68 69 73 20 63 68 75  tent of this chu
1b61b 6e 6b 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20  nk */.};../*.** 
1b61c 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
1b61d 68 69 73 20 6f 62 6a 65 63 74 20 73 65 72 76 65  his object serve
1b61e 73 20 61 73 20 61 20 63 75 72 73 6f 72 20 69 6e  s as a cursor in
1b61f 74 6f 20 74 68 65 20 72 6f 6c 6c 62 61 63 6b 20  to the rollback 
1b620 6a 6f 75 72 6e 61 6c 2e 0a 2a 2a 20 54 68 65 20  journal..** The 
1b621 63 75 72 73 6f 72 20 63 61 6e 20 62 65 20 65 69  cursor can be ei
1b622 74 68 65 72 20 66 6f 72 20 72 65 61 64 69 6e 67  ther for reading
1b623 20 6f 72 20 77 72 69 74 69 6e 67 2e 0a 2a 2f 0a   or writing..*/.
1b624 73 74 72 75 63 74 20 46 69 6c 65 50 6f 69 6e 74  struct FilePoint
1b625 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 69 6e 74   {.  sqlite3_int
1b626 36 34 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20  64 iOffset;     
1b627 20 20 20 20 20 2f 2a 20 4f 66 66 73 65 74 20 66       /* Offset f
1b628 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
1b629 67 20 6f 66 20 74 68 65 20 66 69 6c 65 20 2a 2f  g of the file */
1b62a 0a 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 43  .  FileChunk *pC
1b62b 68 75 6e 6b 3b 20 20 20 20 20 20 20 20 20 20 20  hunk;           
1b62c 20 20 20 2f 2a 20 53 70 65 63 69 66 69 63 20 63     /* Specific c
1b62d 68 75 6e 6b 20 69 6e 74 6f 20 77 68 69 63 68 20  hunk into which 
1b62e 63 75 72 73 6f 72 20 70 6f 69 6e 74 73 20 2a 2f  cursor points */
1b62f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .};../*.** This 
1b630 73 75 62 63 6c 61 73 73 20 69 73 20 61 20 73 75  subclass is a su
1b631 62 63 6c 61 73 73 20 6f 66 20 73 71 6c 69 74 65  bclass of sqlite
1b632 33 5f 66 69 6c 65 2e 20 20 45 61 63 68 20 6f 70  3_file.  Each op
1b633 65 6e 20 6d 65 6d 6f 72 79 2d 6a 6f 75 72 6e 61  en memory-journa
1b634 6c 0a 2a 2a 20 69 73 20 61 6e 20 69 6e 73 74 61  l.** is an insta
1b635 6e 63 65 20 6f 66 20 74 68 69 73 20 63 6c 61 73  nce of this clas
1b636 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 4d 65 6d  s..*/.struct Mem
1b637 4a 6f 75 72 6e 61 6c 20 7b 0a 20 20 73 71 6c 69  Journal {.  sqli
1b638 74 65 33 5f 69 6f 5f 6d 65 74 68 6f 64 73 20 2a  te3_io_methods *
1b639 70 4d 65 74 68 6f 64 3b 20 20 20 20 2f 2a 20 50  pMethod;    /* P
1b63a 61 72 65 6e 74 20 63 6c 61 73 73 2e 20 4d 55 53  arent class. MUS
1b63b 54 20 42 45 20 46 49 52 53 54 20 2a 2f 0a 20 20  T BE FIRST */.  
1b63c 46 69 6c 65 43 68 75 6e 6b 20 2a 70 46 69 72 73  FileChunk *pFirs
1b63d 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
1b63e 2f 2a 20 48 65 61 64 20 6f 66 20 69 6e 2d 6d 65  /* Head of in-me
1b63f 6d 6f 72 79 20 63 68 75 6e 6b 2d 6c 69 73 74 20  mory chunk-list 
1b640 2a 2f 0a 20 20 46 69 6c 65 50 6f 69 6e 74 20 65  */.  FilePoint e
1b641 6e 64 70 6f 69 6e 74 3b 20 20 20 20 20 20 20 20  ndpoint;        
1b642 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20       /* Pointer 
1b643 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68  to the end of th
1b644 65 20 66 69 6c 65 20 2a 2f 0a 20 20 46 69 6c 65  e file */.  File
1b645 50 6f 69 6e 74 20 72 65 61 64 70 6f 69 6e 74 3b  Point readpoint;
1b646 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
1b647 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 65 6e  ointer to the en
1b648 64 20 6f 66 20 74 68 65 20 6c 61 73 74 20 78 52  d of the last xR
1b649 65 61 64 28 29 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a  ead() */.};../*.
1b64a 2a 2a 20 52 65 61 64 20 64 61 74 61 20 66 72 6f  ** Read data fro
1b64b 6d 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  m the file..*/.s
1b64c 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1b64d 6c 52 65 61 64 28 0a 20 20 73 71 6c 69 74 65 33  lRead(.  sqlite3
1b64e 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 20 20 20  _file *pJfd,    
1b64f 2f 2a 20 54 68 65 20 6a 6f 75 72 6e 61 6c 20 66  /* The journal f
1b650 69 6c 65 20 66 72 6f 6d 20 77 68 69 63 68 20 74  ile from which t
1b651 6f 20 72 65 61 64 20 2a 2f 0a 20 20 76 6f 69 64  o read */.  void
1b652 20 2a 7a 42 75 66 2c 20 20 20 20 20 20 20 20 20   *zBuf,         
1b653 20 20 20 2f 2a 20 50 75 74 20 74 68 65 20 72 65     /* Put the re
1b654 73 75 6c 74 73 20 68 65 72 65 20 2a 2f 0a 20 20  sults here */.  
1b655 69 6e 74 20 69 41 6d 74 2c 20 20 20 20 20 20 20  int iAmt,       
1b656 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1b657 20 6f 66 20 62 79 74 65 73 20 74 6f 20 72 65 61   of bytes to rea
1b658 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e  d */.  sqlite_in
1b659 74 36 34 20 69 4f 66 73 74 20 20 20 20 20 2f 2a  t64 iOfst     /*
1b65a 20 42 65 67 69 6e 20 72 65 61 64 69 6e 67 20 61   Begin reading a
1b65b 74 20 74 68 69 73 20 6f 66 66 73 65 74 20 2a 2f  t this offset */
1b65c 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c  .){.  MemJournal
1b65d 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61   *p = (MemJourna
1b65e 6c 20 2a 29 70 4a 66 64 3b 0a 20 20 75 38 20 2a  l *)pJfd;.  u8 *
1b65f 7a 4f 75 74 20 3d 20 7a 42 75 66 3b 0a 20 20 69  zOut = zBuf;.  i
1b660 6e 74 20 6e 52 65 61 64 20 3d 20 69 41 6d 74 3b  nt nRead = iAmt;
1b661 0a 20 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66  .  int iChunkOff
1b662 73 65 74 3b 0a 20 20 46 69 6c 65 43 68 75 6e 6b  set;.  FileChunk
1b663 20 2a 70 43 68 75 6e 6b 3b 0a 0a 20 20 61 73 73   *pChunk;..  ass
1b664 65 72 74 28 20 69 4f 66 73 74 2b 69 41 6d 74 3c  ert( iOfst+iAmt<
1b665 3d 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66  =p->endpoint.iOf
1b666 66 73 65 74 20 29 3b 0a 0a 20 20 69 66 28 20 70  fset );..  if( p
1b667 2d 3e 72 65 61 64 70 6f 69 6e 74 2e 69 4f 66 66  ->readpoint.iOff
1b668 73 65 74 21 3d 69 4f 66 73 74 20 7c 7c 20 69 4f  set!=iOfst || iO
1b669 66 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71  fst==0 ){.    sq
1b66a 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 4f 66 66  lite3_int64 iOff
1b66b 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 70 43   = 0;.    for(pC
1b66c 68 75 6e 6b 3d 70 2d 3e 70 46 69 72 73 74 3b 20  hunk=p->pFirst; 
1b66d 0a 20 20 20 20 20 20 20 20 70 43 68 75 6e 6b 20  .        pChunk 
1b66e 26 26 20 28 69 4f 66 66 2b 4a 4f 55 52 4e 41 4c  && (iOff+JOURNAL
1b66f 5f 43 48 55 4e 4b 53 49 5a 45 29 3c 3d 69 4f 66  _CHUNKSIZE)<=iOf
1b670 73 74 3b 0a 20 20 20 20 20 20 20 20 70 43 68 75  st;.        pChu
1b671 6e 6b 3d 70 43 68 75 6e 6b 2d 3e 70 4e 65 78 74  nk=pChunk->pNext
1b672 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 69 4f  .    ){.      iO
1b673 66 66 20 2b 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48  ff += JOURNAL_CH
1b674 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 7d 0a 20  UNKSIZE;.    }. 
1b675 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 43 68 75   }else{.    pChu
1b676 6e 6b 20 3d 20 70 2d 3e 72 65 61 64 70 6f 69 6e  nk = p->readpoin
1b677 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 7d 0a 0a 20  t.pChunk;.  }.. 
1b678 20 69 43 68 75 6e 6b 4f 66 66 73 65 74 20 3d 20   iChunkOffset = 
1b679 28 69 4f 66 73 74 25 4a 4f 55 52 4e 41 4c 5f 43  (iOfst%JOURNAL_C
1b67a 48 55 4e 4b 53 49 5a 45 29 3b 0a 20 20 64 6f 20  HUNKSIZE);.  do 
1b67b 7b 0a 20 20 20 20 69 6e 74 20 69 53 70 61 63 65  {.    int iSpace
1b67c 20 3d 20 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b   = JOURNAL_CHUNK
1b67d 53 49 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66  SIZE - iChunkOff
1b67e 73 65 74 3b 0a 20 20 20 20 69 6e 74 20 6e 43 6f  set;.    int nCo
1b67f 70 79 20 3d 20 4d 49 4e 28 6e 52 65 61 64 2c 20  py = MIN(nRead, 
1b680 28 4a 4f 55 52 4e 41 4c 5f 43 48 55 4e 4b 53 49  (JOURNAL_CHUNKSI
1b681 5a 45 20 2d 20 69 43 68 75 6e 6b 4f 66 66 73 65  ZE - iChunkOffse
1b682 74 29 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28  t));.    memcpy(
1b683 7a 4f 75 74 2c 20 26 70 43 68 75 6e 6b 2d 3e 7a  zOut, &pChunk->z
1b684 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66 66 73  Chunk[iChunkOffs
1b685 65 74 5d 2c 20 6e 43 6f 70 79 29 3b 0a 20 20 20  et], nCopy);.   
1b686 20 7a 4f 75 74 20 2b 3d 20 6e 43 6f 70 79 3b 0a   zOut += nCopy;.
1b687 20 20 20 20 6e 52 65 61 64 20 2d 3d 20 69 53 70      nRead -= iSp
1b688 61 63 65 3b 0a 20 20 20 20 69 43 68 75 6e 6b 4f  ace;.    iChunkO
1b689 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 7d 20 77  ffset = 0;.  } w
1b68a 68 69 6c 65 28 20 6e 52 65 61 64 3e 3d 30 20 26  hile( nRead>=0 &
1b68b 26 20 28 70 43 68 75 6e 6b 3d 70 43 68 75 6e 6b  & (pChunk=pChunk
1b68c 2d 3e 70 4e 65 78 74 29 20 26 26 20 6e 52 65 61  ->pNext) && nRea
1b68d 64 3e 30 20 29 3b 0a 20 20 70 2d 3e 72 65 61 64  d>0 );.  p->read
1b68e 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 20 3d 20  point.iOffset = 
1b68f 69 4f 66 73 74 2b 69 41 6d 74 3b 0a 20 20 70 2d  iOfst+iAmt;.  p-
1b690 3e 72 65 61 64 70 6f 69 6e 74 2e 70 43 68 75 6e  >readpoint.pChun
1b691 6b 20 3d 20 70 43 68 75 6e 6b 3b 0a 0a 20 20 72  k = pChunk;..  r
1b692 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
1b693 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  .}../*.** Write 
1b694 64 61 74 61 20 74 6f 20 74 68 65 20 66 69 6c 65  data to the file
1b695 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
1b696 6d 65 6d 6a 72 6e 6c 57 72 69 74 65 28 0a 20 20  memjrnlWrite(.  
1b697 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1b698 66 64 2c 20 20 20 20 2f 2a 20 54 68 65 20 6a 6f  fd,    /* The jo
1b699 75 72 6e 61 6c 20 66 69 6c 65 20 69 6e 74 6f 20  urnal file into 
1b69a 77 68 69 63 68 20 74 6f 20 77 72 69 74 65 20 2a  which to write *
1b69b 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a  /.  const void *
1b69c 7a 42 75 66 2c 20 20 20 20 20 20 2f 2a 20 54 61  zBuf,      /* Ta
1b69d 6b 65 20 64 61 74 61 20 74 6f 20 62 65 20 77 72  ke data to be wr
1b69e 69 74 74 65 6e 20 66 72 6f 6d 20 68 65 72 65 20  itten from here 
1b69f 2a 2f 0a 20 20 69 6e 74 20 69 41 6d 74 2c 20 20  */.  int iAmt,  
1b6a0 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1b6a1 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20 74  umber of bytes t
1b6a2 6f 20 77 72 69 74 65 20 2a 2f 0a 20 20 73 71 6c  o write */.  sql
1b6a3 69 74 65 5f 69 6e 74 36 34 20 69 4f 66 73 74 20  ite_int64 iOfst 
1b6a4 20 20 20 20 2f 2a 20 42 65 67 69 6e 20 77 72 69      /* Begin wri
1b6a5 74 69 6e 67 20 61 74 20 74 68 69 73 20 6f 66 66  ting at this off
1b6a6 73 65 74 20 69 6e 74 6f 20 74 68 65 20 66 69 6c  set into the fil
1b6a7 65 20 2a 2f 0a 29 7b 0a 20 20 4d 65 6d 4a 6f 75  e */.){.  MemJou
1b6a8 72 6e 61 6c 20 2a 70 20 3d 20 28 4d 65 6d 4a 6f  rnal *p = (MemJo
1b6a9 75 72 6e 61 6c 20 2a 29 70 4a 66 64 3b 0a 20 20  urnal *)pJfd;.  
1b6aa 69 6e 74 20 6e 57 72 69 74 65 20 3d 20 69 41 6d  int nWrite = iAm
1b6ab 74 3b 0a 20 20 75 38 20 2a 7a 57 72 69 74 65 20  t;.  u8 *zWrite 
1b6ac 3d 20 28 75 38 20 2a 29 7a 42 75 66 3b 0a 0a 20  = (u8 *)zBuf;.. 
1b6ad 20 2f 2a 20 41 6e 20 69 6e 2d 6d 65 6d 6f 72 79   /* An in-memory
1b6ae 20 6a 6f 75 72 6e 61 6c 20 66 69 6c 65 20 73 68   journal file sh
1b6af 6f 75 6c 64 20 6f 6e 6c 79 20 65 76 65 72 20 62  ould only ever b
1b6b0 65 20 61 70 70 65 6e 64 65 64 20 74 6f 2e 20 52  e appended to. R
1b6b1 61 6e 64 6f 6d 0a 20 20 2a 2a 20 61 63 63 65 73  andom.  ** acces
1b6b2 73 20 77 72 69 74 65 73 20 61 72 65 20 6e 6f 74  s writes are not
1b6b3 20 72 65 71 75 69 72 65 64 20 62 79 20 73 71 6c   required by sql
1b6b4 69 74 65 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ite..  */.  asse
1b6b5 72 74 28 69 4f 66 73 74 3d 3d 70 2d 3e 65 6e 64  rt(iOfst==p->end
1b6b6 70 6f 69 6e 74 2e 69 4f 66 66 73 65 74 29 3b 0a  point.iOffset);.
1b6b7 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
1b6b8 45 52 28 69 4f 66 73 74 29 3b 0a 0a 20 20 77 68  ER(iOfst);..  wh
1b6b9 69 6c 65 28 20 6e 57 72 69 74 65 3e 30 20 29 7b  ile( nWrite>0 ){
1b6ba 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a  .    FileChunk *
1b6bb 70 43 68 75 6e 6b 20 3d 20 70 2d 3e 65 6e 64 70  pChunk = p->endp
1b6bc 6f 69 6e 74 2e 70 43 68 75 6e 6b 3b 0a 20 20 20  oint.pChunk;.   
1b6bd 20 69 6e 74 20 69 43 68 75 6e 6b 4f 66 66 73 65   int iChunkOffse
1b6be 74 20 3d 20 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e  t = p->endpoint.
1b6bf 69 4f 66 66 73 65 74 25 4a 4f 55 52 4e 41 4c 5f  iOffset%JOURNAL_
1b6c0 43 48 55 4e 4b 53 49 5a 45 3b 0a 20 20 20 20 69  CHUNKSIZE;.    i
1b6c1 6e 74 20 69 53 70 61 63 65 20 3d 20 4d 49 4e 28  nt iSpace = MIN(
1b6c2 6e 57 72 69 74 65 2c 20 4a 4f 55 52 4e 41 4c 5f  nWrite, JOURNAL_
1b6c3 43 48 55 4e 4b 53 49 5a 45 20 2d 20 69 43 68 75  CHUNKSIZE - iChu
1b6c4 6e 6b 4f 66 66 73 65 74 29 3b 0a 0a 20 20 20 20  nkOffset);..    
1b6c5 69 66 28 20 69 43 68 75 6e 6b 4f 66 66 73 65 74  if( iChunkOffset
1b6c6 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  ==0 ){.      /* 
1b6c7 4e 65 77 20 63 68 75 6e 6b 20 69 73 20 72 65 71  New chunk is req
1b6c8 75 69 72 65 64 20 74 6f 20 65 78 74 65 6e 64 20  uired to extend 
1b6c9 74 68 65 20 66 69 6c 65 2e 20 2a 2f 0a 20 20 20  the file. */.   
1b6ca 20 20 20 46 69 6c 65 43 68 75 6e 6b 20 2a 70 4e     FileChunk *pN
1b6cb 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ew = sqlite3_mal
1b6cc 6c 6f 63 28 73 69 7a 65 6f 66 28 46 69 6c 65 43  loc(sizeof(FileC
1b6cd 68 75 6e 6b 29 29 3b 0a 20 20 20 20 20 20 69 66  hunk));.      if
1b6ce 28 20 21 70 4e 65 77 20 29 7b 0a 20 20 20 20 20  ( !pNew ){.     
1b6cf 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
1b6d0 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 3b 0a 20 20  _IOERR_NOMEM;.  
1b6d1 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4e 65 77      }.      pNew
1b6d2 2d 3e 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 20  ->pNext = 0;.   
1b6d3 20 20 20 69 66 28 20 70 43 68 75 6e 6b 20 29 7b     if( pChunk ){
1b6d4 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
1b6d5 20 70 2d 3e 70 46 69 72 73 74 20 29 3b 0a 20 20   p->pFirst );.  
1b6d6 20 20 20 20 20 20 70 43 68 75 6e 6b 2d 3e 70 4e        pChunk->pN
1b6d7 65 78 74 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20  ext = pNew;.    
1b6d8 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1b6d9 20 61 73 73 65 72 74 28 20 21 70 2d 3e 70 46 69   assert( !p->pFi
1b6da 72 73 74 20 29 3b 0a 20 20 20 20 20 20 20 20 70  rst );.        p
1b6db 2d 3e 70 46 69 72 73 74 20 3d 20 70 4e 65 77 3b  ->pFirst = pNew;
1b6dc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
1b6dd 2d 3e 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e  ->endpoint.pChun
1b6de 6b 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 7d 0a  k = pNew;.    }.
1b6df 0a 20 20 20 20 6d 65 6d 63 70 79 28 26 70 2d 3e  .    memcpy(&p->
1b6e0 65 6e 64 70 6f 69 6e 74 2e 70 43 68 75 6e 6b 2d  endpoint.pChunk-
1b6e1 3e 7a 43 68 75 6e 6b 5b 69 43 68 75 6e 6b 4f 66  >zChunk[iChunkOf
1b6e2 66 73 65 74 5d 2c 20 7a 57 72 69 74 65 2c 20 69  fset], zWrite, i
1b6e3 53 70 61 63 65 29 3b 0a 20 20 20 20 7a 57 72 69  Space);.    zWri
1b6e4 74 65 20 2b 3d 20 69 53 70 61 63 65 3b 0a 20 20  te += iSpace;.  
1b6e5 20 20 6e 57 72 69 74 65 20 2d 3d 20 69 53 70 61    nWrite -= iSpa
1b6e6 63 65 3b 0a 20 20 20 20 70 2d 3e 65 6e 64 70 6f  ce;.    p->endpo
1b6e7 69 6e 74 2e 69 4f 66 66 73 65 74 20 2b 3d 20 69  int.iOffset += i
1b6e8 53 70 61 63 65 3b 0a 20 20 7d 0a 0a 20 20 72 65  Space;.  }..  re
1b6e9 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
1b6ea 7d 0a 0a 2f 2a 0a 2a 2a 20 54 72 75 6e 63 61 74  }../*.** Truncat
1b6eb 65 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f 0a 73  e the file..*/.s
1b6ec 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a 72 6e  tatic int memjrn
1b6ed 6c 54 72 75 6e 63 61 74 65 28 73 71 6c 69 74 65  lTruncate(sqlite
1b6ee 33 5f 66 69 6c 65 20 2a 70 4a 66 64 2c 20 73 71  3_file *pJfd, sq
1b6ef 6c 69 74 65 5f 69 6e 74 36 34 20 73 69 7a 65 29  lite_int64 size)
1b6f0 7b 0a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a  {.  MemJournal *
1b6f1 70 20 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20  p = (MemJournal 
1b6f2 2a 29 70 4a 66 64 3b 0a 20 20 46 69 6c 65 43 68  *)pJfd;.  FileCh
1b6f3 75 6e 6b 20 2a 70 43 68 75 6e 6b 3b 0a 20 20 61  unk *pChunk;.  a
1b6f4 73 73 65 72 74 28 73 69 7a 65 3d 3d 30 29 3b 0a  ssert(size==0);.
1b6f5 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
1b6f6 45 52 28 73 69 7a 65 29 3b 0a 20 20 70 43 68 75  ER(size);.  pChu
1b6f7 6e 6b 20 3d 20 70 2d 3e 70 46 69 72 73 74 3b 0a  nk = p->pFirst;.
1b6f8 20 20 77 68 69 6c 65 28 20 70 43 68 75 6e 6b 20    while( pChunk 
1b6f9 29 7b 0a 20 20 20 20 46 69 6c 65 43 68 75 6e 6b  ){.    FileChunk
1b6fa 20 2a 70 54 6d 70 20 3d 20 70 43 68 75 6e 6b 3b   *pTmp = pChunk;
1b6fb 0a 20 20 20 20 70 43 68 75 6e 6b 20 3d 20 70 43  .    pChunk = pC
1b6fc 68 75 6e 6b 2d 3e 70 4e 65 78 74 3b 0a 20 20 20  hunk->pNext;.   
1b6fd 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 54   sqlite3_free(pT
1b6fe 6d 70 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  mp);.  }.  sqlit
1b6ff 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 4f 70 65 6e  e3MemJournalOpen
1b700 28 70 4a 66 64 29 3b 0a 20 20 72 65 74 75 72 6e  (pJfd);.  return
1b701 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
1b702 2a 0a 2a 2a 20 43 6c 6f 73 65 20 74 68 65 20 66  *.** Close the f
1b703 69 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ile..*/.static i
1b704 6e 74 20 6d 65 6d 6a 72 6e 6c 43 6c 6f 73 65 28  nt memjrnlClose(
1b705 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 2a 70 4a  sqlite3_file *pJ
1b706 66 64 29 7b 0a 20 20 6d 65 6d 6a 72 6e 6c 54 72  fd){.  memjrnlTr
1b707 75 6e 63 61 74 65 28 70 4a 66 64 2c 20 30 29 3b  uncate(pJfd, 0);
1b708 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
1b709 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 53  _OK;.}.../*.** S
1b70a 79 6e 63 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2f  ync the file..*/
1b70b 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 65 6d 6a  .static int memj
1b70c 72 6e 6c 53 79 6e 63 28 73 71 6c 69 74 65 33 5f  rnlSync(sqlite3_
1b70d 66 69 6c 65 20 2a 4e 6f 74 55 73 65 64 2c 20 69  file *NotUsed, i
1b70e 6e 74 20 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20  nt NotUsed2){.  
1b70f 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1b710 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
1b711 65 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  ed2);.  return S
1b712 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a  QLITE_OK;.}../*.
1b713 2a 2a 20 51 75 65 72 79 20 74 68 65 20 73 69 7a  ** Query the siz
1b714 65 20 6f 66 20 74 68 65 20 66 69 6c 65 20 69 6e  e of the file in
1b715 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
1b716 63 20 69 6e 74 20 6d 65 6d 6a 72 6e 6c 46 69 6c  c int memjrnlFil
1b717 65 53 69 7a 65 28 73 71 6c 69 74 65 33 5f 66 69  eSize(sqlite3_fi
1b718 6c 65 20 2a 70 4a 66 64 2c 20 73 71 6c 69 74 65  le *pJfd, sqlite
1b719 5f 69 6e 74 36 34 20 2a 70 53 69 7a 65 29 7b 0a  _int64 *pSize){.
1b71a 20 20 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20    MemJournal *p 
1b71b 3d 20 28 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29  = (MemJournal *)
1b71c 70 4a 66 64 3b 0a 20 20 2a 70 53 69 7a 65 20 3d  pJfd;.  *pSize =
1b71d 20 28 73 71 6c 69 74 65 5f 69 6e 74 36 34 29 20   (sqlite_int64) 
1b71e 70 2d 3e 65 6e 64 70 6f 69 6e 74 2e 69 4f 66 66  p->endpoint.iOff
1b71f 73 65 74 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  set;.  return SQ
1b720 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
1b721 2a 20 54 61 62 6c 65 20 6f 66 20 6d 65 74 68 6f  * Table of metho
1b722 64 73 20 66 6f 72 20 4d 65 6d 4a 6f 75 72 6e 61  ds for MemJourna
1b723 6c 20 73 71 6c 69 74 65 33 5f 66 69 6c 65 20 6f  l sqlite3_file o
1b724 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63  bject..*/.static
1b725 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
1b726 69 6f 5f 6d 65 74 68 6f 64 73 20 4d 65 6d 4a 6f  io_methods MemJo
1b727 75 72 6e 61 6c 4d 65 74 68 6f 64 73 20 3d 20 7b  urnalMethods = {
1b728 0a 20 20 31 2c 20 20 20 20 20 20 20 20 20 20 20  .  1,           
1b729 20 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e       /* iVersion
1b72a 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 43 6c 6f   */.  memjrnlClo
1b72b 73 65 2c 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73  se,     /* xClos
1b72c 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 52 65  e */.  memjrnlRe
1b72d 61 64 2c 20 20 20 20 20 20 2f 2a 20 78 52 65 61  ad,      /* xRea
1b72e 64 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 57 72  d */.  memjrnlWr
1b72f 69 74 65 2c 20 20 20 20 20 2f 2a 20 78 57 72 69  ite,     /* xWri
1b730 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a 72 6e 6c 54  te */.  memjrnlT
1b731 72 75 6e 63 61 74 65 2c 20 20 2f 2a 20 78 54 72  runcate,  /* xTr
1b732 75 6e 63 61 74 65 20 2a 2f 0a 20 20 6d 65 6d 6a  uncate */.  memj
1b733 72 6e 6c 53 79 6e 63 2c 20 20 20 20 20 20 2f 2a  rnlSync,      /*
1b734 20 78 53 79 6e 63 20 2a 2f 0a 20 20 6d 65 6d 6a   xSync */.  memj
1b735 72 6e 6c 46 69 6c 65 53 69 7a 65 2c 20 20 2f 2a  rnlFileSize,  /*
1b736 20 78 46 69 6c 65 53 69 7a 65 20 2a 2f 0a 20 20   xFileSize */.  
1b737 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1b738 20 20 2f 2a 20 78 4c 6f 63 6b 20 2a 2f 0a 20 20    /* xLock */.  
1b739 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1b73a 20 20 2f 2a 20 78 55 6e 6c 6f 63 6b 20 2a 2f 0a    /* xUnlock */.
1b73b 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1b73c 20 20 20 20 2f 2a 20 78 43 68 65 63 6b 52 65 73      /* xCheckRes
1b73d 65 72 76 65 64 4c 6f 63 6b 20 2a 2f 0a 20 20 30  ervedLock */.  0
1b73e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1b73f 20 2f 2a 20 78 46 69 6c 65 43 6f 6e 74 72 6f 6c   /* xFileControl
1b740 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20 20 20   */.  0,        
1b741 20 20 20 20 20 20 20 20 2f 2a 20 78 53 65 63 74          /* xSect
1b742 6f 72 53 69 7a 65 20 2a 2f 0a 20 20 30 20 20 20  orSize */.  0   
1b743 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1b744 20 78 44 65 76 69 63 65 43 68 61 72 61 63 74 65   xDeviceCharacte
1b745 72 69 73 74 69 63 73 20 2a 2f 0a 7d 3b 0a 0a 2f  ristics */.};../
1b746 2a 20 0a 2a 2a 20 4f 70 65 6e 20 61 20 6a 6f 75  * .** Open a jou
1b747 72 6e 61 6c 20 66 69 6c 65 2e 0a 2a 2f 0a 53 51  rnal file..*/.SQ
1b748 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1b749 64 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  d sqlite3MemJour
1b74a 6e 61 6c 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f  nalOpen(sqlite3_
1b74b 66 69 6c 65 20 2a 70 4a 66 64 29 7b 0a 20 20 4d  file *pJfd){.  M
1b74c 65 6d 4a 6f 75 72 6e 61 6c 20 2a 70 20 3d 20 28  emJournal *p = (
1b74d 4d 65 6d 4a 6f 75 72 6e 61 6c 20 2a 29 70 4a 66  MemJournal *)pJf
1b74e 64 3b 0a 20 20 6d 65 6d 73 65 74 28 70 2c 20 30  d;.  memset(p, 0
1b74f 2c 20 73 71 6c 69 74 65 33 4d 65 6d 4a 6f 75 72  , sqlite3MemJour
1b750 6e 61 6c 53 69 7a 65 28 29 29 3b 0a 20 20 70 2d  nalSize());.  p-
1b751 3e 70 4d 65 74 68 6f 64 20 3d 20 26 4d 65 6d 4a  >pMethod = &MemJ
1b752 6f 75 72 6e 61 6c 4d 65 74 68 6f 64 73 3b 0a 7d  ournalMethods;.}
1b753 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ../*.** Return t
1b754 72 75 65 20 69 66 20 74 68 65 20 66 69 6c 65 2d  rue if the file-
1b755 68 61 6e 64 6c 65 20 70 61 73 73 65 64 20 61 73  handle passed as
1b756 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 20   an argument is 
1b757 0a 2a 2a 20 61 6e 20 69 6e 2d 6d 65 6d 6f 72 79  .** an in-memory
1b758 20 6a 6f 75 72 6e 61 6c 20 0a 2a 2f 0a 53 51 4c   journal .*/.SQL
1b759 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1b75a 73 71 6c 69 74 65 33 49 73 4d 65 6d 4a 6f 75 72  sqlite3IsMemJour
1b75b 6e 61 6c 28 73 71 6c 69 74 65 33 5f 66 69 6c 65  nal(sqlite3_file
1b75c 20 2a 70 4a 66 64 29 7b 0a 20 20 72 65 74 75 72   *pJfd){.  retur
1b75d 6e 20 70 4a 66 64 2d 3e 70 4d 65 74 68 6f 64 73  n pJfd->pMethods
1b75e 3d 3d 26 4d 65 6d 4a 6f 75 72 6e 61 6c 4d 65 74  ==&MemJournalMet
1b75f 68 6f 64 73 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  hods;.}../* .** 
1b760 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
1b761 72 20 6f 66 20 62 79 74 65 73 20 72 65 71 75 69  r of bytes requi
1b762 72 65 64 20 74 6f 20 73 74 6f 72 65 20 61 20 4d  red to store a M
1b763 65 6d 4a 6f 75 72 6e 61 6c 20 74 68 61 74 20 75  emJournal that u
1b764 73 65 73 20 76 66 73 0a 2a 2a 20 70 56 66 73 20  ses vfs.** pVfs 
1b765 74 6f 20 63 72 65 61 74 65 20 74 68 65 20 75 6e  to create the un
1b766 64 65 72 6c 79 69 6e 67 20 6f 6e 2d 64 69 73 6b  derlying on-disk
1b767 20 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   files..*/.SQLIT
1b768 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1b769 6c 69 74 65 33 4d 65 6d 4a 6f 75 72 6e 61 6c 53  lite3MemJournalS
1b76a 69 7a 65 28 29 7b 0a 20 20 72 65 74 75 72 6e 20  ize(){.  return 
1b76b 73 69 7a 65 6f 66 28 4d 65 6d 4a 6f 75 72 6e 61  sizeof(MemJourna
1b76c 6c 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  l);.}../********
1b76d 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 65  ****** End of me
1b76e 6d 6a 6f 75 72 6e 61 6c 2e 63 20 2a 2a 2a 2a 2a  mjournal.c *****
1b76f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b770 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b771 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
1b772 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
1b773 65 20 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a  e walker.c *****
1b774 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b775 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b776 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
1b777 38 20 41 75 67 75 73 74 20 31 36 0a 2a 2a 0a 2a  8 August 16.**.*
1b778 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
1b779 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
1b77a 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
1b77b 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
1b77c 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
1b77d 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
1b77e 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
1b77f 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
1b780 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
1b781 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
1b782 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
1b783 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
1b784 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
1b785 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
1b786 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
1b787 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
1b788 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
1b789 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
1b78a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b78b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b78c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b78d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68  **********.** Th
1b78e 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73  is file contains
1b78f 20 72 6f 75 74 69 6e 65 73 20 75 73 65 64 20 66   routines used f
1b790 6f 72 20 77 61 6c 6b 69 6e 67 20 74 68 65 20 70  or walking the p
1b791 61 72 73 65 72 20 74 72 65 65 20 66 6f 72 0a 2a  arser tree for.*
1b792 2a 20 61 6e 20 53 51 4c 20 73 74 61 74 65 6d 65  * an SQL stateme
1b793 6e 74 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 77  nt..**.** $Id: w
1b794 61 6c 6b 65 72 2e 63 2c 76 20 31 2e 31 20 32 30  alker.c,v 1.1 20
1b795 30 38 2f 30 38 2f 32 30 20 31 36 3a 33 35 3a 31  08/08/20 16:35:1
1b796 30 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a  0 drh Exp $.*/..
1b797 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 61 6e 20 65  ./*.** Walk an e
1b798 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 20  xpression tree. 
1b799 20 49 6e 76 6f 6b 65 20 74 68 65 20 63 61 6c 6c   Invoke the call
1b79a 62 61 63 6b 20 6f 6e 63 65 20 66 6f 72 20 65 61  back once for ea
1b79b 63 68 20 6e 6f 64 65 0a 2a 2a 20 6f 66 20 74 68  ch node.** of th
1b79c 65 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 77 68  e expression, wh
1b79d 69 6c 65 20 64 65 63 65 6e 64 69 6e 67 2e 20 20  ile decending.  
1b79e 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 2c  (In other words,
1b79f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a   the callback.**
1b7a0 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 65 66 6f   is invoked befo
1b7a1 72 65 20 76 69 73 69 74 69 6e 67 20 63 68 69 6c  re visiting chil
1b7a2 64 72 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 54 68 65  dren.).**.** The
1b7a3 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72   return value fr
1b7a4 6f 6d 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  om the callback 
1b7a5 73 68 6f 75 6c 64 20 62 65 20 6f 6e 65 20 6f 66  should be one of
1b7a6 20 74 68 65 20 57 52 43 5f 2a 0a 2a 2a 20 63 6f   the WRC_*.** co
1b7a7 6e 73 74 61 6e 74 73 20 74 6f 20 73 70 65 63 69  nstants to speci
1b7a8 66 79 20 68 6f 77 20 74 6f 20 70 72 6f 63 65 65  fy how to procee
1b7a9 64 20 77 69 74 68 20 74 68 65 20 77 61 6c 6b 2e  d with the walk.
1b7aa 0a 2a 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 43 6f  .**.**    WRC_Co
1b7ab 6e 74 69 6e 75 65 20 20 20 20 20 20 43 6f 6e 74  ntinue      Cont
1b7ac 69 6e 75 65 20 64 65 73 63 65 6e 64 69 6e 67 20  inue descending 
1b7ad 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  down the tree..*
1b7ae 2a 0a 2a 2a 20 20 20 20 57 52 43 5f 50 72 75 6e  *.**    WRC_Prun
1b7af 65 20 20 20 20 20 20 20 20 20 44 6f 20 6e 6f 74  e         Do not
1b7b0 20 64 65 73 63 65 6e 64 20 69 6e 74 6f 20 63 68   descend into ch
1b7b1 69 6c 64 20 6e 6f 64 65 73 2e 20 20 42 75 74 20  ild nodes.  But 
1b7b2 61 6c 6c 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  allow.**        
1b7b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
1b7b4 65 20 77 61 6c 6b 20 74 6f 20 63 6f 6e 74 69 6e  e walk to contin
1b7b5 75 65 20 77 69 74 68 20 73 69 62 6c 69 6e 67 20  ue with sibling 
1b7b6 6e 6f 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  nodes..**.**    
1b7b7 57 52 43 5f 41 62 6f 72 74 20 20 20 20 20 20 20  WRC_Abort       
1b7b8 20 20 44 6f 20 6e 6f 20 6d 6f 72 65 20 63 61 6c    Do no more cal
1b7b9 6c 62 61 63 6b 73 2e 20 20 55 6e 77 69 6e 64 20  lbacks.  Unwind 
1b7ba 74 68 65 20 73 74 61 63 6b 20 61 6e 64 0a 2a 2a  the stack and.**
1b7bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1b7bc 20 20 20 20 20 20 72 65 74 75 72 6e 20 74 68 65        return the
1b7bd 20 74 6f 70 2d 6c 65 76 65 6c 20 77 61 6c 6b 20   top-level walk 
1b7be 63 61 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  call..**.** The 
1b7bf 72 65 74 75 72 6e 20 76 61 6c 75 65 20 66 72 6f  return value fro
1b7c0 6d 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  m this routine i
1b7c1 73 20 57 52 43 5f 41 62 6f 72 74 20 74 6f 20 61  s WRC_Abort to a
1b7c2 62 61 6e 64 6f 6e 20 74 68 65 20 74 72 65 65 20  bandon the tree 
1b7c3 77 61 6c 6b 0a 2a 2a 20 61 6e 64 20 57 52 43 5f  walk.** and WRC_
1b7c4 43 6f 6e 74 69 6e 75 65 20 74 6f 20 63 6f 6e 74  Continue to cont
1b7c5 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  inue..*/.SQLITE_
1b7c6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1b7c7 74 65 33 57 61 6c 6b 45 78 70 72 28 57 61 6c 6b  te3WalkExpr(Walk
1b7c8 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70  er *pWalker, Exp
1b7c9 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69 6e 74  r *pExpr){.  int
1b7ca 20 72 63 3b 0a 20 20 69 66 28 20 70 45 78 70 72   rc;.  if( pExpr
1b7cb 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
1b7cc 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 72 63 20  _Continue;.  rc 
1b7cd 3d 20 70 57 61 6c 6b 65 72 2d 3e 78 45 78 70 72  = pWalker->xExpr
1b7ce 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1b7cf 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66 28 20  , pExpr);.  if( 
1b7d0 72 63 3d 3d 57 52 43 5f 43 6f 6e 74 69 6e 75 65  rc==WRC_Continue
1b7d1 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
1b7d2 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1b7d3 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  ker, pExpr->pLef
1b7d4 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1b7d5 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20 73  Abort;.    if( s
1b7d6 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1b7d7 57 61 6c 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70  Walker, pExpr->p
1b7d8 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
1b7d9 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69  WRC_Abort;.    i
1b7da 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78  f( sqlite3WalkEx
1b7db 70 72 4c 69 73 74 28 70 57 61 6c 6b 65 72 2c 20  prList(pWalker, 
1b7dc 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 20  pExpr->pList) ) 
1b7dd 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1b7de 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1b7df 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
1b7e0 6b 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c  ker, pExpr->pSel
1b7e1 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ect) ){.      re
1b7e2 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1b7e3 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1b7e4 72 6e 20 72 63 20 26 20 57 52 43 5f 41 62 6f 72  rn rc & WRC_Abor
1b7e5 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c  t;.}../*.** Call
1b7e6 20 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72   sqlite3WalkExpr
1b7e7 28 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70  () for every exp
1b7e8 72 65 73 73 69 6f 6e 20 69 6e 20 6c 69 73 74 20  ression in list 
1b7e9 70 20 6f 72 20 75 6e 74 69 6c 0a 2a 2a 20 61 6e  p or until.** an
1b7ea 20 61 62 6f 72 74 20 72 65 71 75 65 73 74 20 69   abort request i
1b7eb 73 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  s seen..*/.SQLIT
1b7ec 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1b7ed 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 4c 69 73  lite3WalkExprLis
1b7ee 74 28 57 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65  t(Walker *pWalke
1b7ef 72 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b  r, ExprList *p){
1b7f0 0a 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 57  .  int i, rc = W
1b7f1 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 73  RC_Continue;.  s
1b7f2 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
1b7f3 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66  tem *pItem;.  if
1b7f4 28 20 70 20 29 7b 0a 20 20 20 20 66 6f 72 28 69  ( p ){.    for(i
1b7f5 3d 70 2d 3e 6e 45 78 70 72 2c 20 70 49 74 65 6d  =p->nExpr, pItem
1b7f6 3d 70 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d 2d 2c  =p->a; i>0; i--,
1b7f7 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1b7f8 20 69 66 28 20 73 71 6c 69 74 65 33 57 61 6c 6b   if( sqlite3Walk
1b7f9 45 78 70 72 28 70 57 61 6c 6b 65 72 2c 20 70 49  Expr(pWalker, pI
1b7fa 74 65 6d 2d 3e 70 45 78 70 72 29 20 29 20 72 65  tem->pExpr) ) re
1b7fb 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a  turn WRC_Abort;.
1b7fc 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1b7fd 72 6e 20 72 63 20 26 20 57 52 43 5f 43 6f 6e 74  rn rc & WRC_Cont
1b7fe 69 6e 75 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  inue;.}../*.** W
1b7ff 61 6c 6b 20 61 6c 6c 20 65 78 70 72 65 73 73 69  alk all expressi
1b800 6f 6e 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ons associated w
1b801 69 74 68 20 53 45 4c 45 43 54 20 73 74 61 74 65  ith SELECT state
1b802 6d 65 6e 74 20 70 2e 20 20 44 6f 0a 2a 2a 20 6e  ment p.  Do.** n
1b803 6f 74 20 69 6e 76 6f 6b 65 20 74 68 65 20 53 45  ot invoke the SE
1b804 4c 45 43 54 20 63 61 6c 6c 62 61 63 6b 20 6f 6e  LECT callback on
1b805 20 70 2c 20 62 75 74 20 64 6f 20 28 6f 66 20 63   p, but do (of c
1b806 6f 75 72 73 65 29 20 69 6e 76 6f 6b 65 0a 2a 2a  ourse) invoke.**
1b807 20 61 6e 79 20 65 78 70 72 20 63 61 6c 6c 62 61   any expr callba
1b808 63 6b 73 20 61 6e 64 20 53 45 4c 45 43 54 20 63  cks and SELECT c
1b809 61 6c 6c 62 61 63 6b 73 20 74 68 61 74 20 63 6f  allbacks that co
1b80a 6d 65 20 66 72 6f 6d 20 73 75 62 71 75 65 72 69  me from subqueri
1b80b 65 73 2e 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52  es..** Return WR
1b80c 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43 5f 43  C_Abort or WRC_C
1b80d 6f 6e 74 69 6e 75 65 2e 0a 2a 2f 0a 53 51 4c 49  ontinue..*/.SQLI
1b80e 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1b80f 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74  qlite3WalkSelect
1b810 45 78 70 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  Expr(Walker *pWa
1b811 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
1b812 7b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  {.  if( sqlite3W
1b813 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1b814 6b 65 72 2c 20 70 2d 3e 70 45 4c 69 73 74 29 20  ker, p->pEList) 
1b815 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f  ) return WRC_Abo
1b816 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  rt;.  if( sqlite
1b817 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c 6b 65  3WalkExpr(pWalke
1b818 72 2c 20 70 2d 3e 70 57 68 65 72 65 29 20 29 20  r, p->pWhere) ) 
1b819 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
1b81a 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 57  ;.  if( sqlite3W
1b81b 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57 61 6c  alkExprList(pWal
1b81c 6b 65 72 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  ker, p->pGroupBy
1b81d 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1b81e 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69  bort;.  if( sqli
1b81f 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61 6c  te3WalkExpr(pWal
1b820 6b 65 72 2c 20 70 2d 3e 70 48 61 76 69 6e 67 29  ker, p->pHaving)
1b821 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62   ) return WRC_Ab
1b822 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c 69 74  ort;.  if( sqlit
1b823 65 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70  e3WalkExprList(p
1b824 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4f 72 64 65  Walker, p->pOrde
1b825 72 42 79 29 20 29 20 72 65 74 75 72 6e 20 57 52  rBy) ) return WR
1b826 43 5f 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73  C_Abort;.  if( s
1b827 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70  qlite3WalkExpr(p
1b828 57 61 6c 6b 65 72 2c 20 70 2d 3e 70 4c 69 6d 69  Walker, p->pLimi
1b829 74 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f  t) ) return WRC_
1b82a 41 62 6f 72 74 3b 0a 20 20 69 66 28 20 73 71 6c  Abort;.  if( sql
1b82b 69 74 65 33 57 61 6c 6b 45 78 70 72 28 70 57 61  ite3WalkExpr(pWa
1b82c 6c 6b 65 72 2c 20 70 2d 3e 70 4f 66 66 73 65 74  lker, p->pOffset
1b82d 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1b82e 62 6f 72 74 3b 0a 20 20 72 65 74 75 72 6e 20 57  bort;.  return W
1b82f 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a  RC_Continue;.}..
1b830 2f 2a 0a 2a 2a 20 57 61 6c 6b 20 74 68 65 20 70  /*.** Walk the p
1b831 61 72 73 65 20 74 72 65 65 73 20 61 73 73 6f 63  arse trees assoc
1b832 69 61 74 65 64 20 77 69 74 68 20 61 6c 6c 20 73  iated with all s
1b833 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68 65  ubqueries in the
1b834 0a 2a 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  .** FROM clause 
1b835 6f 66 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  of SELECT statem
1b836 65 6e 74 20 70 2e 20 20 44 6f 20 6e 6f 74 20 69  ent p.  Do not i
1b837 6e 76 6f 6b 65 20 74 68 65 20 73 65 6c 65 63 74  nvoke the select
1b838 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 6f 6e 20  .** callback on 
1b839 70 2c 20 62 75 74 20 64 6f 20 69 6e 76 6f 6b 65  p, but do invoke
1b83a 20 69 74 20 6f 6e 20 65 61 63 68 20 46 52 4f 4d   it on each FROM
1b83b 20 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 79   clause subquery
1b83c 0a 2a 2a 20 61 6e 64 20 6f 6e 20 61 6e 79 20 73  .** and on any s
1b83d 75 62 71 75 65 72 69 65 73 20 66 75 72 74 68 65  ubqueries furthe
1b83e 72 20 64 6f 77 6e 20 69 6e 20 74 68 65 20 74 72  r down in the tr
1b83f 65 65 2e 20 20 52 65 74 75 72 6e 20 0a 2a 2a 20  ee.  Return .** 
1b840 57 52 43 5f 41 62 6f 72 74 20 6f 72 20 57 52 43  WRC_Abort or WRC
1b841 5f 43 6f 6e 74 69 6e 75 65 3b 0a 2a 2f 0a 53 51  _Continue;.*/.SQ
1b842 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1b843 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65   sqlite3WalkSele
1b844 63 74 46 72 6f 6d 28 57 61 6c 6b 65 72 20 2a 70  ctFrom(Walker *p
1b845 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1b846 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  p){.  SrcList *p
1b847 53 72 63 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  Src;.  int i;.  
1b848 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
1b849 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a 20 20 70  tem *pItem;..  p
1b84a 53 72 63 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20  Src = p->pSrc;. 
1b84b 20 69 66 28 20 70 53 72 63 20 29 7b 0a 20 20 20   if( pSrc ){.   
1b84c 20 66 6f 72 28 69 3d 70 53 72 63 2d 3e 6e 53 72   for(i=pSrc->nSr
1b84d 63 2c 20 70 49 74 65 6d 3d 70 53 72 63 2d 3e 61  c, pItem=pSrc->a
1b84e 3b 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65  ; i>0; i--, pIte
1b84f 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  m++){.      if( 
1b850 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1b851 74 28 70 57 61 6c 6b 65 72 2c 20 70 49 74 65 6d  t(pWalker, pItem
1b852 2d 3e 70 53 65 6c 65 63 74 29 20 29 7b 0a 20 20  ->pSelect) ){.  
1b853 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1b854 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a  _Abort;.      }.
1b855 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1b856 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b  rn WRC_Continue;
1b857 0a 7d 20 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20  .} ../*.** Call 
1b858 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1b859 29 20 66 6f 72 20 65 76 65 72 79 20 65 78 70 72  ) for every expr
1b85a 65 73 73 69 6f 6e 20 69 6e 20 53 65 6c 65 63 74  ession in Select
1b85b 20 73 74 61 74 65 6d 65 6e 74 20 70 2e 0a 2a 2a   statement p..**
1b85c 20 49 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33 57   Invoke sqlite3W
1b85d 61 6c 6b 53 65 6c 65 63 74 28 29 20 66 6f 72 20  alkSelect() for 
1b85e 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 74 68  subqueries in th
1b85f 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61 6e  e FROM clause an
1b860 64 0a 2a 2a 20 6f 6e 20 74 68 65 20 63 6f 6d 70  d.** on the comp
1b861 6f 75 6e 64 20 73 65 6c 65 63 74 20 63 68 61 69  ound select chai
1b862 6e 2c 20 70 2d 3e 70 50 72 69 6f 72 2e 0a 2a 2a  n, p->pPrior..**
1b863 0a 2a 2a 20 52 65 74 75 72 6e 20 57 52 43 5f 43  .** Return WRC_C
1b864 6f 6e 74 69 6e 75 65 20 75 6e 64 65 72 20 6e 6f  ontinue under no
1b865 72 6d 61 6c 20 63 6f 6e 64 69 74 69 6f 6e 73 2e  rmal conditions.
1b866 20 20 52 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    Return WRC_Abo
1b867 72 74 20 69 66 0a 2a 2a 20 74 68 65 72 65 20 69  rt if.** there i
1b868 73 20 61 6e 20 61 62 6f 72 74 20 72 65 71 75 65  s an abort reque
1b869 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  st..**.** If the
1b86a 20 57 61 6c 6b 65 72 20 64 6f 65 73 20 6e 6f 74   Walker does not
1b86b 20 68 61 76 65 20 61 6e 20 78 53 65 6c 65 63 74   have an xSelect
1b86c 43 61 6c 6c 62 61 63 6b 28 29 20 74 68 65 6e 20  Callback() then 
1b86d 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1b86e 69 73 20 61 20 6e 6f 2d 6f 70 20 72 65 74 75 72  is a no-op retur
1b86f 6e 69 6e 67 20 57 52 43 5f 43 6f 6e 74 69 6e 75  ning WRC_Continu
1b870 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1b871 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1b872 57 61 6c 6b 53 65 6c 65 63 74 28 57 61 6c 6b 65  WalkSelect(Walke
1b873 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1b874 63 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 72 63  ct *p){.  int rc
1b875 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20  ;.  if( p==0 || 
1b876 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74  pWalker->xSelect
1b877 43 61 6c 6c 62 61 63 6b 3d 3d 30 20 29 20 72 65  Callback==0 ) re
1b878 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75  turn WRC_Continu
1b879 65 3b 0a 20 20 72 63 20 3d 20 57 52 43 5f 43 6f  e;.  rc = WRC_Co
1b87a 6e 74 69 6e 75 65 3b 0a 20 20 77 68 69 6c 65 28  ntinue;.  while(
1b87b 20 70 20 20 29 7b 0a 20 20 20 20 72 63 20 3d 20   p  ){.    rc = 
1b87c 70 57 61 6c 6b 65 72 2d 3e 78 53 65 6c 65 63 74  pWalker->xSelect
1b87d 43 61 6c 6c 62 61 63 6b 28 70 57 61 6c 6b 65 72  Callback(pWalker
1b87e 2c 20 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63  , p);.    if( rc
1b87f 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 66   ) break;.    if
1b880 28 20 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c  ( sqlite3WalkSel
1b881 65 63 74 45 78 70 72 28 70 57 61 6c 6b 65 72 2c  ectExpr(pWalker,
1b882 20 70 29 20 29 20 72 65 74 75 72 6e 20 57 52 43   p) ) return WRC
1b883 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69 66 28 20  _Abort;.    if( 
1b884 73 71 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63  sqlite3WalkSelec
1b885 74 46 72 6f 6d 28 70 57 61 6c 6b 65 72 2c 20 70  tFrom(pWalker, p
1b886 29 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 41  ) ) return WRC_A
1b887 62 6f 72 74 3b 0a 20 20 20 20 70 20 3d 20 70 2d  bort;.    p = p-
1b888 3e 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72  >pPrior;.  }.  r
1b889 65 74 75 72 6e 20 72 63 20 26 20 57 52 43 5f 41  eturn rc & WRC_A
1b88a 62 6f 72 74 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  bort;.}../******
1b88b 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
1b88c 77 61 6c 6b 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  walker.c *******
1b88d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b88e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b88f 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
1b890 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
1b891 69 6c 65 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a  ile resolve.c **
1b892 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b893 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b894 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
1b895 30 30 38 20 41 75 67 75 73 74 20 31 38 0a 2a 2a  008 August 18.**
1b896 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
1b897 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
1b898 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
1b899 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
1b89a 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
1b89b 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
1b89c 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
1b89d 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
1b89e 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
1b89f 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
1b8a0 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
1b8a1 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
1b8a2 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
1b8a3 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
1b8a4 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
1b8a5 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
1b8a6 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
1b8a7 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
1b8a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8a9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1b8ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
1b8ac 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1b8ad 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20 75  tains routines u
1b8ae 73 65 64 20 66 6f 72 20 77 61 6c 6b 69 6e 67 20  sed for walking 
1b8af 74 68 65 20 70 61 72 73 65 72 20 74 72 65 65 20  the parser tree 
1b8b0 61 6e 64 0a 2a 2a 20 72 65 73 6f 6c 76 65 20 61  and.** resolve a
1b8b1 6c 6c 20 69 64 65 6e 74 69 66 69 65 72 73 20 62  ll identifiers b
1b8b2 79 20 61 73 73 6f 63 69 61 74 69 6e 67 20 74 68  y associating th
1b8b3 65 6d 20 77 69 74 68 20 61 20 70 61 72 74 69 63  em with a partic
1b8b4 75 6c 61 72 0a 2a 2a 20 74 61 62 6c 65 20 61 6e  ular.** table an
1b8b5 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20  d column..**.** 
1b8b6 24 49 64 3a 20 72 65 73 6f 6c 76 65 2e 63 2c 76  $Id: resolve.c,v
1b8b7 20 31 2e 31 31 20 32 30 30 38 2f 31 31 2f 31 37   1.11 2008/11/17
1b8b8 20 31 39 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c   19:18:55 daniel
1b8b9 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
1b8ba 2f 2a 0a 2a 2a 20 54 75 72 6e 20 74 68 65 20 70  /*.** Turn the p
1b8bb 45 78 70 72 20 65 78 70 72 65 73 73 69 6f 6e 20  Expr expression 
1b8bc 69 6e 74 6f 20 61 6e 20 61 6c 69 61 73 20 66 6f  into an alias fo
1b8bd 72 20 74 68 65 20 69 43 6f 6c 2d 74 68 20 63 6f  r the iCol-th co
1b8be 6c 75 6d 6e 20 6f 66 20 74 68 65 0a 2a 2a 20 72  lumn of the.** r
1b8bf 65 73 75 6c 74 20 73 65 74 20 69 6e 20 70 45 4c  esult set in pEL
1b8c0 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ist..**.** If th
1b8c1 65 20 72 65 73 75 6c 74 20 73 65 74 20 63 6f 6c  e result set col
1b8c2 75 6d 6e 20 69 73 20 61 20 73 69 6d 70 6c 65 20  umn is a simple 
1b8c3 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e 63 65  column reference
1b8c4 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74  , then this rout
1b8c5 69 6e 65 0a 2a 2a 20 6d 61 6b 65 73 20 61 6e 20  ine.** makes an 
1b8c6 65 78 61 63 74 20 63 6f 70 79 2e 20 20 42 75 74  exact copy.  But
1b8c7 20 66 6f 72 20 61 6e 79 20 6f 74 68 65 72 20 6b   for any other k
1b8c8 69 6e 64 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ind of expressio
1b8c9 6e 2c 20 74 68 69 73 0a 2a 2a 20 72 6f 75 74 69  n, this.** routi
1b8ca 6e 65 20 6d 61 6b 65 20 61 20 63 6f 70 79 20 6f  ne make a copy o
1b8cb 66 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  f the result set
1b8cc 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20 61   column as the a
1b8cd 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a  rgument to the.*
1b8ce 2a 20 54 4b 5f 41 53 20 6f 70 65 72 61 74 6f 72  * TK_AS operator
1b8cf 2e 20 20 54 68 65 20 54 4b 5f 41 53 20 6f 70 65  .  The TK_AS ope
1b8d0 72 61 74 6f 72 20 63 61 75 73 65 73 20 74 68 65  rator causes the
1b8d1 20 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 62   expression to b
1b8d2 65 0a 2a 2a 20 65 76 61 6c 75 61 74 65 64 20 6a  e.** evaluated j
1b8d3 75 73 74 20 6f 6e 63 65 20 61 6e 64 20 74 68 65  ust once and the
1b8d4 6e 20 72 65 75 73 65 64 20 66 6f 72 20 65 61 63  n reused for eac
1b8d5 68 20 61 6c 69 61 73 2e 0a 2a 2a 0a 2a 2a 20 54  h alias..**.** T
1b8d6 68 65 20 72 65 61 73 6f 6e 20 66 6f 72 20 73 75  he reason for su
1b8d7 70 70 72 65 73 73 69 6e 67 20 74 68 65 20 54 4b  ppressing the TK
1b8d8 5f 41 53 20 74 65 72 6d 20 77 68 65 6e 20 74 68  _AS term when th
1b8d9 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20  e expression is 
1b8da 61 20 73 69 6d 70 6c 65 0a 2a 2a 20 63 6f 6c 75  a simple.** colu
1b8db 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 69 73 20  mn reference is 
1b8dc 73 6f 20 74 68 61 74 20 74 68 65 20 63 6f 6c 75  so that the colu
1b8dd 6d 6e 20 72 65 66 65 72 65 6e 63 65 20 77 69 6c  mn reference wil
1b8de 6c 20 62 65 20 72 65 63 6f 67 6e 69 7a 65 64 20  l be recognized 
1b8df 61 73 0a 2a 2a 20 75 73 61 62 6c 65 20 62 79 20  as.** usable by 
1b8e0 69 6e 64 69 63 65 73 20 77 69 74 68 69 6e 20 74  indices within t
1b8e1 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1b8e2 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63  processing logic
1b8e3 2e 20 0a 2a 2a 0a 2a 2a 20 48 61 63 6b 3a 20 20  . .**.** Hack:  
1b8e4 54 68 65 20 54 4b 5f 41 53 20 6f 70 65 72 61 74  The TK_AS operat
1b8e5 6f 72 20 69 73 20 69 6e 68 69 62 69 74 65 64 20  or is inhibited 
1b8e6 69 66 20 7a 54 79 70 65 5b 30 5d 3d 3d 27 47 27  if zType[0]=='G'
1b8e7 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 0a 2a 2a  .  This means.**
1b8e8 20 74 68 61 74 20 69 6e 20 61 20 47 52 4f 55 50   that in a GROUP
1b8e9 20 42 59 20 63 6c 61 75 73 65 2c 20 74 68 65 20   BY clause, the 
1b8ea 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76  expression is ev
1b8eb 61 6c 75 61 74 65 64 20 74 77 69 63 65 2e 20 20  aluated twice.  
1b8ec 48 65 6e 63 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  Hence:.**.**    
1b8ed 20 53 45 4c 45 43 54 20 72 61 6e 64 6f 6d 28 29   SELECT random()
1b8ee 25 35 20 41 53 20 78 2c 20 63 6f 75 6e 74 28 2a  %5 AS x, count(*
1b8ef 29 20 46 52 4f 4d 20 74 61 62 20 47 52 4f 55 50  ) FROM tab GROUP
1b8f0 20 42 59 20 78 0a 2a 2a 0a 2a 2a 20 49 73 20 65   BY x.**.** Is e
1b8f1 71 75 69 76 61 6c 65 6e 74 20 74 6f 3a 0a 2a 2a  quivalent to:.**
1b8f2 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 72  .**     SELECT r
1b8f3 61 6e 64 6f 6d 28 29 25 35 20 41 53 20 78 2c 20  andom()%5 AS x, 
1b8f4 63 6f 75 6e 74 28 2a 29 20 46 52 4f 4d 20 74 61  count(*) FROM ta
1b8f5 62 20 47 52 4f 55 50 20 42 59 20 72 61 6e 64 6f  b GROUP BY rando
1b8f6 6d 28 29 25 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  m()%5.**.** The 
1b8f7 72 65 73 75 6c 74 20 6f 66 20 72 61 6e 64 6f 6d  result of random
1b8f8 28 29 25 35 20 69 6e 20 74 68 65 20 47 52 4f 55  ()%5 in the GROU
1b8f9 50 20 42 59 20 63 6c 61 75 73 65 20 69 73 20 70  P BY clause is p
1b8fa 72 6f 62 61 62 6c 79 20 64 69 66 66 65 72 65 6e  robably differen
1b8fb 74 0a 2a 2a 20 66 72 6f 6d 20 74 68 65 20 72 65  t.** from the re
1b8fc 73 75 6c 74 20 69 6e 20 74 68 65 20 72 65 73 75  sult in the resu
1b8fd 6c 74 2d 73 65 74 2e 20 20 57 65 20 6d 69 67 68  lt-set.  We migh
1b8fe 74 20 66 69 78 20 74 68 69 73 20 73 6f 6d 65 64  t fix this somed
1b8ff 61 79 2e 20 20 4f 72 0a 2a 2a 20 74 68 65 6e 20  ay.  Or.** then 
1b900 61 67 61 69 6e 2c 20 77 65 20 6d 69 67 68 74 20  again, we might 
1b901 6e 6f 74 2e 2e 2e 0a 2a 2f 0a 73 74 61 74 69 63  not....*/.static
1b902 20 76 6f 69 64 20 72 65 73 6f 6c 76 65 41 6c 69   void resolveAli
1b903 61 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  as(.  Parse *pPa
1b904 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  rse,         /* 
1b905 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20  Parsing context 
1b906 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
1b907 45 4c 69 73 74 2c 20 20 20 20 20 20 2f 2a 20 41  EList,      /* A
1b908 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1b909 20 69 6e 74 20 69 43 6f 6c 2c 20 20 20 20 20 20   int iCol,      
1b90a 20 20 20 20 20 20 20 20 2f 2a 20 41 20 63 6f 6c          /* A col
1b90b 75 6d 6e 20 69 6e 20 74 68 65 20 72 65 73 75 6c  umn in the resul
1b90c 74 20 73 65 74 2e 20 20 30 2e 2e 70 45 4c 69 73  t set.  0..pELis
1b90d 74 2d 3e 6e 45 78 70 72 2d 31 20 2a 2f 0a 20 20  t->nExpr-1 */.  
1b90e 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20 20  Expr *pExpr,    
1b90f 20 20 20 20 20 20 20 2f 2a 20 54 72 61 6e 73 66         /* Transf
1b910 6f 72 6d 20 74 68 69 73 20 69 6e 74 6f 20 61 6e  orm this into an
1b911 20 61 6c 69 61 73 20 74 6f 20 74 68 65 20 72 65   alias to the re
1b912 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 63 6f  sult set */.  co
1b913 6e 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20  nst char *zType 
1b914 20 20 20 20 20 2f 2a 20 22 47 52 4f 55 50 22 20       /* "GROUP" 
1b915 6f 72 20 22 4f 52 44 45 52 22 20 6f 72 20 22 22  or "ORDER" or ""
1b916 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20 2a 70   */.){.  Expr *p
1b917 4f 72 69 67 3b 20 20 20 20 20 20 20 20 20 20 20  Orig;           
1b918 2f 2a 20 54 68 65 20 69 43 6f 6c 2d 74 68 20 63  /* The iCol-th c
1b919 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 72 65 73  olumn of the res
1b91a 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 45 78 70  ult set */.  Exp
1b91b 72 20 2a 70 44 75 70 3b 20 20 20 20 20 20 20 20  r *pDup;        
1b91c 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 70      /* Copy of p
1b91d 4f 72 69 67 20 2a 2f 0a 20 20 73 71 6c 69 74 65  Orig */.  sqlite
1b91e 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20  3 *db;          
1b91f 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1b920 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 0a   connection */..
1b921 20 20 61 73 73 65 72 74 28 20 69 43 6f 6c 3e 3d    assert( iCol>=
1b922 30 20 26 26 20 69 43 6f 6c 3c 70 45 4c 69 73 74  0 && iCol<pEList
1b923 2d 3e 6e 45 78 70 72 20 29 3b 0a 20 20 70 4f 72  ->nExpr );.  pOr
1b924 69 67 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69  ig = pEList->a[i
1b925 43 6f 6c 5d 2e 70 45 78 70 72 3b 0a 20 20 61 73  Col].pExpr;.  as
1b926 73 65 72 74 28 20 70 4f 72 69 67 21 3d 30 20 29  sert( pOrig!=0 )
1b927 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 72 69  ;.  assert( pOri
1b928 67 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 52 65  g->flags & EP_Re
1b929 73 6f 6c 76 65 64 20 29 3b 0a 20 20 64 62 20 3d  solved );.  db =
1b92a 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70   pParse->db;.  p
1b92b 44 75 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70  Dup = sqlite3Exp
1b92c 72 44 75 70 28 64 62 2c 20 70 4f 72 69 67 29 3b  rDup(db, pOrig);
1b92d 0a 20 20 69 66 28 20 70 44 75 70 3d 3d 30 20 29  .  if( pDup==0 )
1b92e 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
1b92f 44 75 70 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  Dup->op!=TK_COLU
1b930 4d 4e 20 26 26 20 7a 54 79 70 65 5b 30 5d 21 3d  MN && zType[0]!=
1b931 27 47 27 20 29 7b 0a 20 20 20 20 70 44 75 70 20  'G' ){.    pDup 
1b932 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
1b933 50 61 72 73 65 2c 20 54 4b 5f 41 53 2c 20 70 44  Parse, TK_AS, pD
1b934 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  up, 0, 0);.    i
1b935 66 28 20 70 44 75 70 3d 3d 30 20 29 20 72 65 74  f( pDup==0 ) ret
1b936 75 72 6e 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  urn;.    if( pEL
1b937 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e 69 41 6c  ist->a[iCol].iAl
1b938 69 61 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ias==0 ){.      
1b939 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
1b93a 69 41 6c 69 61 73 20 3d 20 2b 2b 70 50 61 72 73  iAlias = ++pPars
1b93b 65 2d 3e 6e 41 6c 69 61 73 3b 0a 20 20 20 20 7d  e->nAlias;.    }
1b93c 0a 20 20 20 20 70 44 75 70 2d 3e 69 54 61 62 6c  .    pDup->iTabl
1b93d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 43  e = pEList->a[iC
1b93e 6f 6c 5d 2e 69 41 6c 69 61 73 3b 0a 20 20 7d 0a  ol].iAlias;.  }.
1b93f 20 20 69 66 28 20 70 45 78 70 72 2d 3e 66 6c 61    if( pExpr->fla
1b940 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
1b941 74 65 20 29 7b 0a 20 20 20 20 70 44 75 70 2d 3e  te ){.    pDup->
1b942 70 43 6f 6c 6c 20 3d 20 70 45 78 70 72 2d 3e 70  pColl = pExpr->p
1b943 43 6f 6c 6c 3b 0a 20 20 20 20 70 44 75 70 2d 3e  Coll;.    pDup->
1b944 66 6c 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43  flags |= EP_ExpC
1b945 6f 6c 6c 61 74 65 3b 0a 20 20 7d 0a 20 20 73 71  ollate;.  }.  sq
1b946 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 64  lite3ExprClear(d
1b947 62 2c 20 70 45 78 70 72 29 3b 0a 20 20 6d 65 6d  b, pExpr);.  mem
1b948 63 70 79 28 70 45 78 70 72 2c 20 70 44 75 70 2c  cpy(pExpr, pDup,
1b949 20 73 69 7a 65 6f 66 28 2a 70 45 78 70 72 29 29   sizeof(*pExpr))
1b94a 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1b94b 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a 0a  e(db, pDup);.}..
1b94c 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 74 68 65 20  /*.** Given the 
1b94d 6e 61 6d 65 20 6f 66 20 61 20 63 6f 6c 75 6d 6e  name of a column
1b94e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 2e 59   of the form X.Y
1b94f 2e 5a 20 6f 72 20 59 2e 5a 20 6f 72 20 6a 75 73  .Z or Y.Z or jus
1b950 74 20 5a 2c 20 6c 6f 6f 6b 20 75 70 0a 2a 2a 20  t Z, look up.** 
1b951 74 68 61 74 20 6e 61 6d 65 20 69 6e 20 74 68 65  that name in the
1b952 20 73 65 74 20 6f 66 20 73 6f 75 72 63 65 20 74   set of source t
1b953 61 62 6c 65 73 20 69 6e 20 70 53 72 63 4c 69 73  ables in pSrcLis
1b954 74 20 61 6e 64 20 6d 61 6b 65 20 74 68 65 20 70  t and make the p
1b955 45 78 70 72 20 0a 2a 2a 20 65 78 70 72 65 73 73  Expr .** express
1b956 69 6f 6e 20 6e 6f 64 65 20 72 65 66 65 72 20 62  ion node refer b
1b957 61 63 6b 20 74 6f 20 74 68 61 74 20 73 6f 75 72  ack to that sour
1b958 63 65 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 65 20  ce column.  The 
1b959 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 6e 67 65  following change
1b95a 73 0a 2a 2a 20 61 72 65 20 6d 61 64 65 20 74 6f  s.** are made to
1b95b 20 70 45 78 70 72 3a 0a 2a 2a 0a 2a 2a 20 20 20   pExpr:.**.**   
1b95c 20 70 45 78 70 72 2d 3e 69 44 62 20 20 20 20 20   pExpr->iDb     
1b95d 20 20 20 20 20 20 53 65 74 20 74 68 65 20 69 6e        Set the in
1b95e 64 65 78 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d  dex in db->aDb[]
1b95f 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1b960 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   X.**           
1b961 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 65                (e
1b962 76 65 6e 20 69 66 20 58 20 69 73 20 69 6d 70 6c  ven if X is impl
1b963 69 65 64 29 2e 0a 2a 2a 20 20 20 20 70 45 78 70  ied)..**    pExp
1b964 72 2d 3e 69 54 61 62 6c 65 20 20 20 20 20 20 20  r->iTable       
1b965 20 53 65 74 20 74 6f 20 74 68 65 20 63 75 72 73   Set to the curs
1b966 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  or number for th
1b967 65 20 74 61 62 6c 65 20 6f 62 74 61 69 6e 65 64  e table obtained
1b968 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
1b969 20 20 20 20 20 20 20 20 20 20 20 20 66 72 6f 6d              from
1b96a 20 70 53 72 63 4c 69 73 74 2e 0a 2a 2a 20 20 20   pSrcList..**   
1b96b 20 70 45 78 70 72 2d 3e 70 54 61 62 20 20 20 20   pExpr->pTab    
1b96c 20 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20        Points to 
1b96d 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  the Table struct
1b96e 75 72 65 20 6f 66 20 58 2e 59 20 28 65 76 65 6e  ure of X.Y (even
1b96f 20 69 66 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   if.**          
1b970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 58                 X
1b971 20 61 6e 64 2f 6f 72 20 59 20 61 72 65 20 69 6d   and/or Y are im
1b972 70 6c 69 65 64 2e 29 0a 2a 2a 20 20 20 20 70 45  plied.).**    pE
1b973 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 20 20 20  xpr->iColumn    
1b974 20 20 20 53 65 74 20 74 6f 20 74 68 65 20 63 6f     Set to the co
1b975 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 77 69 74 68  lumn number with
1b976 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2a  in the table..**
1b977 20 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 20 20      pExpr->op   
1b978 20 20 20 20 20 20 20 20 20 53 65 74 20 74 6f 20           Set to 
1b979 54 4b 5f 43 4f 4c 55 4d 4e 2e 0a 2a 2a 20 20 20  TK_COLUMN..**   
1b97a 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 20 20 20   pExpr->pLeft   
1b97b 20 20 20 20 20 20 41 6e 79 20 65 78 70 72 65 73        Any expres
1b97c 73 69 6f 6e 20 74 68 69 73 20 70 6f 69 6e 74 73  sion this points
1b97d 20 74 6f 20 69 73 20 64 65 6c 65 74 65 64 0a 2a   to is deleted.*
1b97e 2a 20 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67  *    pExpr->pRig
1b97f 68 74 20 20 20 20 20 20 20 20 41 6e 79 20 65 78  ht        Any ex
1b980 70 72 65 73 73 69 6f 6e 20 74 68 69 73 20 70 6f  pression this po
1b981 69 6e 74 73 20 74 6f 20 69 73 20 64 65 6c 65 74  ints to is delet
1b982 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 44  ed..**.** The pD
1b983 62 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 6e 61  bToken is the na
1b984 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61  me of the databa
1b985 73 65 20 28 74 68 65 20 22 58 22 29 2e 20 20 54  se (the "X").  T
1b986 68 69 73 20 76 61 6c 75 65 20 6d 61 79 20 62 65  his value may be
1b987 0a 2a 2a 20 4e 55 4c 4c 20 6d 65 61 6e 69 6e 67  .** NULL meaning
1b988 20 74 68 61 74 20 6e 61 6d 65 20 69 73 20 6f 66   that name is of
1b989 20 74 68 65 20 66 6f 72 6d 20 59 2e 5a 20 6f 72   the form Y.Z or
1b98a 20 5a 2e 20 20 41 6e 79 20 61 76 61 69 6c 61 62   Z.  Any availab
1b98b 6c 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 63  le database.** c
1b98c 61 6e 20 62 65 20 75 73 65 64 2e 20 20 54 68 65  an be used.  The
1b98d 20 70 54 61 62 6c 65 54 6f 6b 65 6e 20 69 73 20   pTableToken is 
1b98e 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20  the name of the 
1b98f 74 61 62 6c 65 20 28 74 68 65 20 22 59 22 29 2e  table (the "Y").
1b990 20 20 54 68 69 73 0a 2a 2a 20 76 61 6c 75 65 20    This.** value 
1b991 63 61 6e 20 62 65 20 4e 55 4c 4c 20 69 66 20 70  can be NULL if p
1b992 44 62 54 6f 6b 65 6e 20 69 73 20 61 6c 73 6f 20  DbToken is also 
1b993 4e 55 4c 4c 2e 20 20 49 66 20 70 54 61 62 6c 65  NULL.  If pTable
1b994 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c 20 69 74  Token is NULL it
1b995 0a 2a 2a 20 6d 65 61 6e 73 20 74 68 61 74 20 74  .** means that t
1b996 68 65 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 6e  he form of the n
1b997 61 6d 65 20 69 73 20 5a 20 61 6e 64 20 74 68 61  ame is Z and tha
1b998 74 20 63 6f 6c 75 6d 6e 73 20 66 72 6f 6d 20 61  t columns from a
1b999 6e 79 20 74 61 62 6c 65 0a 2a 2a 20 63 61 6e 20  ny table.** can 
1b99a 62 65 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20 49  be used..**.** I
1b99b 66 20 74 68 65 20 6e 61 6d 65 20 63 61 6e 6e 6f  f the name canno
1b99c 74 20 62 65 20 72 65 73 6f 6c 76 65 64 20 75 6e  t be resolved un
1b99d 61 6d 62 69 67 75 6f 75 73 6c 79 2c 20 6c 65 61  ambiguously, lea
1b99e 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
1b99f 61 67 65 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  age.** in pParse
1b9a0 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 6f 6e 2d   and return non-
1b9a1 7a 65 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65  zero.  Return ze
1b9a2 72 6f 20 6f 6e 20 73 75 63 63 65 73 73 2e 0a 2a  ro on success..*
1b9a3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 6f  /.static int loo
1b9a4 6b 75 70 4e 61 6d 65 28 0a 20 20 50 61 72 73 65  kupName(.  Parse
1b9a5 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1b9a6 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
1b9a7 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 6f 6b 65  ontext */.  Toke
1b9a8 6e 20 2a 70 44 62 54 6f 6b 65 6e 2c 20 20 20 20  n *pDbToken,    
1b9a9 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1b9aa 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1b9ab 69 6e 67 20 74 61 62 6c 65 2c 20 6f 72 20 4e 55  ing table, or NU
1b9ac 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  LL */.  Token *p
1b9ad 54 61 62 6c 65 54 6f 6b 65 6e 2c 20 20 2f 2a 20  TableToken,  /* 
1b9ae 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20 63 6f  Name of table co
1b9af 6e 74 61 69 6e 69 6e 67 20 63 6f 6c 75 6d 6e 2c  ntaining column,
1b9b0 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f   or NULL */.  To
1b9b1 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 54 6f 6b 65  ken *pColumnToke
1b9b2 6e 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  n, /* Name of th
1b9b3 65 20 63 6f 6c 75 6d 6e 2e 20 2a 2f 0a 20 20 4e  e column. */.  N
1b9b4 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c  ameContext *pNC,
1b9b5 20 20 20 20 2f 2a 20 54 68 65 20 6e 61 6d 65 20      /* The name 
1b9b6 63 6f 6e 74 65 78 74 20 75 73 65 64 20 74 6f 20  context used to 
1b9b7 72 65 73 6f 6c 76 65 20 74 68 65 20 6e 61 6d 65  resolve the name
1b9b8 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
1b9b9 72 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61  r          /* Ma
1b9ba 6b 65 20 74 68 69 73 20 45 58 50 52 20 6e 6f 64  ke this EXPR nod
1b9bb 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 73  e point to the s
1b9bc 65 6c 65 63 74 65 64 20 63 6f 6c 75 6d 6e 20 2a  elected column *
1b9bd 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a 44 62  /.){.  char *zDb
1b9be 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e   = 0;       /* N
1b9bf 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
1b9c0 61 73 65 2e 20 20 54 68 65 20 22 58 22 20 69 6e  ase.  The "X" in
1b9c1 20 58 2e 59 2e 5a 20 2a 2f 0a 20 20 63 68 61 72   X.Y.Z */.  char
1b9c2 20 2a 7a 54 61 62 20 3d 20 30 3b 20 20 20 20 20   *zTab = 0;     
1b9c3 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1b9c4 74 61 62 6c 65 2e 20 20 54 68 65 20 22 59 22 20  table.  The "Y" 
1b9c5 69 6e 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20  in X.Y.Z or Y.Z 
1b9c6 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 20  */.  char *zCol 
1b9c7 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 61 6d  = 0;      /* Nam
1b9c8 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2e  e of the column.
1b9c9 20 20 54 68 65 20 22 5a 22 20 2a 2f 0a 20 20 69    The "Z" */.  i
1b9ca 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
1b9cb 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
1b9cc 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 63 6e  ters */.  int cn
1b9cd 74 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  t = 0;          
1b9ce 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
1b9cf 75 6d 62 65 72 20 6f 66 20 6d 61 74 63 68 69 6e  umber of matchin
1b9d0 67 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a  g column names *
1b9d1 2f 0a 20 20 69 6e 74 20 63 6e 74 54 61 62 20 3d  /.  int cntTab =
1b9d2 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1b9d3 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
1b9d4 6f 66 20 6d 61 74 63 68 69 6e 67 20 74 61 62 6c  of matching tabl
1b9d5 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 73 71 6c  e names */.  sql
1b9d6 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1b9d7 65 2d 3e 64 62 3b 20 20 20 20 20 20 20 20 20 2f  e->db;         /
1b9d8 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1b9d9 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 73  onnection */.  s
1b9da 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
1b9db 65 6d 20 2a 70 49 74 65 6d 3b 20 20 20 20 20 20  em *pItem;      
1b9dc 20 2f 2a 20 55 73 65 20 66 6f 72 20 6c 6f 6f 70   /* Use for loop
1b9dd 69 6e 67 20 6f 76 65 72 20 70 53 72 63 4c 69 73  ing over pSrcLis
1b9de 74 20 69 74 65 6d 73 20 2a 2f 0a 20 20 73 74 72  t items */.  str
1b9df 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
1b9e0 20 2a 70 4d 61 74 63 68 20 3d 20 30 3b 20 20 2f   *pMatch = 0;  /
1b9e1 2a 20 54 68 65 20 6d 61 74 63 68 69 6e 67 20 70  * The matching p
1b9e2 53 72 63 4c 69 73 74 20 69 74 65 6d 20 2a 2f 0a  SrcList item */.
1b9e3 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1b9e4 54 6f 70 4e 43 20 3d 20 70 4e 43 3b 20 20 20 20  TopNC = pNC;    
1b9e5 20 20 20 20 2f 2a 20 46 69 72 73 74 20 6e 61 6d      /* First nam
1b9e6 65 63 6f 6e 74 65 78 74 20 69 6e 20 74 68 65 20  econtext in the 
1b9e7 6c 69 73 74 20 2a 2f 0a 20 20 53 63 68 65 6d 61  list */.  Schema
1b9e8 20 2a 70 53 63 68 65 6d 61 20 3d 20 30 3b 20 20   *pSchema = 0;  
1b9e9 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53              /* S
1b9ea 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65 78 70  chema of the exp
1b9eb 72 65 73 73 69 6f 6e 20 2a 2f 0a 0a 20 20 61 73  ression */..  as
1b9ec 73 65 72 74 28 20 70 43 6f 6c 75 6d 6e 54 6f 6b  sert( pColumnTok
1b9ed 65 6e 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b  en && pColumnTok
1b9ee 65 6e 2d 3e 7a 20 29 3b 20 2f 2a 20 54 68 65 20  en->z ); /* The 
1b9ef 5a 20 69 6e 20 58 2e 59 2e 5a 20 63 61 6e 6e 6f  Z in X.Y.Z canno
1b9f0 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 0a 20 20  t be NULL */..  
1b9f1 2f 2a 20 44 65 71 75 6f 74 65 20 61 6e 64 20 7a  /* Dequote and z
1b9f2 65 72 6f 2d 74 65 72 6d 69 6e 61 74 65 20 74 68  ero-terminate th
1b9f3 65 20 6e 61 6d 65 73 20 2a 2f 0a 20 20 7a 44 62  e names */.  zDb
1b9f4 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72   = sqlite3NameFr
1b9f5 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 44 62 54  omToken(db, pDbT
1b9f6 6f 6b 65 6e 29 3b 0a 20 20 7a 54 61 62 20 3d 20  oken);.  zTab = 
1b9f7 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1b9f8 6f 6b 65 6e 28 64 62 2c 20 70 54 61 62 6c 65 54  oken(db, pTableT
1b9f9 6f 6b 65 6e 29 3b 0a 20 20 7a 43 6f 6c 20 3d 20  oken);.  zCol = 
1b9fa 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1b9fb 6f 6b 65 6e 28 64 62 2c 20 70 43 6f 6c 75 6d 6e  oken(db, pColumn
1b9fc 54 6f 6b 65 6e 29 3b 0a 20 20 69 66 28 20 64 62  Token);.  if( db
1b9fd 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1b9fe 7b 0a 20 20 20 20 67 6f 74 6f 20 6c 6f 6f 6b 75  {.    goto looku
1b9ff 70 6e 61 6d 65 5f 65 6e 64 3b 0a 20 20 7d 0a 0a  pname_end;.  }..
1ba00 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
1ba01 74 68 65 20 6e 6f 64 65 20 74 6f 20 6e 6f 2d 6d  the node to no-m
1ba02 61 74 63 68 20 2a 2f 0a 20 20 70 45 78 70 72 2d  atch */.  pExpr-
1ba03 3e 69 54 61 62 6c 65 20 3d 20 2d 31 3b 0a 20 20  >iTable = -1;.  
1ba04 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 30 3b  pExpr->pTab = 0;
1ba05 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 61 74 20  ..  /* Start at 
1ba06 74 68 65 20 69 6e 6e 65 72 2d 6d 6f 73 74 20 63  the inner-most c
1ba07 6f 6e 74 65 78 74 20 61 6e 64 20 6d 6f 76 65 20  ontext and move 
1ba08 6f 75 74 77 61 72 64 20 75 6e 74 69 6c 20 61 20  outward until a 
1ba09 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 20 2a  match is found *
1ba0a 2f 0a 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26  /.  while( pNC &
1ba0b 26 20 63 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20  & cnt==0 ){.    
1ba0c 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1ba0d 3b 0a 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70  ;.    SrcList *p
1ba0e 53 72 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70  SrcList = pNC->p
1ba0f 53 72 63 4c 69 73 74 3b 0a 0a 20 20 20 20 69 66  SrcList;..    if
1ba10 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20  ( pSrcList ){.  
1ba11 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
1ba12 65 6d 3d 70 53 72 63 4c 69 73 74 2d 3e 61 3b 20  em=pSrcList->a; 
1ba13 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72 63  i<pSrcList->nSrc
1ba14 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i++, pItem++){
1ba15 0a 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a  .        Table *
1ba16 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 69 6e  pTab;.        in
1ba17 74 20 69 44 62 3b 0a 20 20 20 20 20 20 20 20 43  t iDb;.        C
1ba18 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 0a  olumn *pCol;.  .
1ba19 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1ba1a 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20  Item->pTab;.    
1ba1b 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62      assert( pTab
1ba1c 21 3d 30 20 26 26 20 70 54 61 62 2d 3e 7a 4e 61  !=0 && pTab->zNa
1ba1d 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  me!=0 );.       
1ba1e 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1ba1f 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
1ba20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
1ba21 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
1ba22 70 54 61 62 2d 3e 6e 43 6f 6c 3e 30 20 29 3b 0a  pTab->nCol>0 );.
1ba23 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62          if( zTab
1ba24 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
1ba25 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20  ( pItem->zAlias 
1ba26 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63  ){.            c
1ba27 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20  har *zTabName = 
1ba28 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20  pItem->zAlias;. 
1ba29 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
1ba2a 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54  qlite3StrICmp(zT
1ba2b 61 62 4e 61 6d 65 2c 20 7a 54 61 62 29 21 3d 30  abName, zTab)!=0
1ba2c 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1ba2d 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1ba2e 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
1ba2f 7a 54 61 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d  zTabName = pTab-
1ba30 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20  >zName;.        
1ba31 20 20 20 20 69 66 28 20 7a 54 61 62 4e 61 6d 65      if( zTabName
1ba32 3d 3d 30 20 7c 7c 20 73 71 6c 69 74 65 33 53 74  ==0 || sqlite3St
1ba33 72 49 43 6d 70 28 7a 54 61 62 4e 61 6d 65 2c 20  rICmp(zTabName, 
1ba34 7a 54 61 62 29 21 3d 30 20 29 20 63 6f 6e 74 69  zTab)!=0 ) conti
1ba35 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
1ba36 20 69 66 28 20 7a 44 62 21 3d 30 20 26 26 20 73   if( zDb!=0 && s
1ba37 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 64 62  qlite3StrICmp(db
1ba38 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
1ba39 2c 20 7a 44 62 29 21 3d 30 20 29 7b 0a 20 20 20  , zDb)!=0 ){.   
1ba3a 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69             conti
1ba3b 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 20  nue;.           
1ba3c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
1ba3d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1ba3e 20 69 66 28 20 30 3d 3d 28 63 6e 74 54 61 62 2b   if( 0==(cntTab+
1ba3f 2b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  +) ){.          
1ba40 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20  pExpr->iTable = 
1ba41 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b 0a  pItem->iCursor;.
1ba42 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1ba43 3e 70 54 61 62 20 3d 20 70 54 61 62 3b 0a 20 20  >pTab = pTab;.  
1ba44 20 20 20 20 20 20 20 20 70 53 63 68 65 6d 61 20          pSchema 
1ba45 3d 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3b  = pTab->pSchema;
1ba46 0a 20 20 20 20 20 20 20 20 20 20 70 4d 61 74 63  .          pMatc
1ba47 68 20 3d 20 70 49 74 65 6d 3b 0a 20 20 20 20 20  h = pItem;.     
1ba48 20 20 20 7d 0a 20 20 20 20 20 20 20 20 66 6f 72     }.        for
1ba49 28 6a 3d 30 2c 20 70 43 6f 6c 3d 70 54 61 62 2d  (j=0, pCol=pTab-
1ba4a 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62 2d 3e 6e  >aCol; j<pTab->n
1ba4b 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 43 6f 6c 2b 2b  Col; j++, pCol++
1ba4c 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
1ba4d 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1ba4e 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pCol->zName, zCo
1ba4f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  l)==0 ){.       
1ba50 20 20 20 20 20 49 64 4c 69 73 74 20 2a 70 55 73       IdList *pUs
1ba51 69 6e 67 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ing;.           
1ba52 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20   cnt++;.        
1ba53 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c      pExpr->iTabl
1ba54 65 20 3d 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  e = pItem->iCurs
1ba55 6f 72 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  or;.            
1ba56 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 54  pExpr->pTab = pT
1ba57 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ab;.            
1ba58 70 4d 61 74 63 68 20 3d 20 70 49 74 65 6d 3b 0a  pMatch = pItem;.
1ba59 20 20 20 20 20 20 20 20 20 20 20 20 70 53 63 68              pSch
1ba5a 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63 68  ema = pTab->pSch
1ba5b 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ema;.           
1ba5c 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65 20 74   /* Substitute t
1ba5d 68 65 20 72 6f 77 69 64 20 28 63 6f 6c 75 6d 6e  he rowid (column
1ba5e 20 2d 31 29 20 66 6f 72 20 74 68 65 20 49 4e 54   -1) for the INT
1ba5f 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59  EGER PRIMARY KEY
1ba60 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20   */.            
1ba61 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  pExpr->iColumn =
1ba62 20 6a 3d 3d 70 54 61 62 2d 3e 69 50 4b 65 79 20   j==pTab->iPKey 
1ba63 3f 20 2d 31 20 3a 20 6a 3b 0a 20 20 20 20 20 20  ? -1 : j;.      
1ba64 20 20 20 20 20 20 69 66 28 20 69 3c 70 53 72 63        if( i<pSrc
1ba65 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 20 29 7b 0a  List->nSrc-1 ){.
1ba66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
1ba67 28 20 70 49 74 65 6d 5b 31 5d 2e 6a 6f 69 6e 74  ( pItem[1].joint
1ba68 79 70 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c  ype & JT_NATURAL
1ba69 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1ba6a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6d      /* If this m
1ba6b 61 74 63 68 20 6f 63 63 75 72 72 65 64 20 69 6e  atch occurred in
1ba6c 20 74 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20   the left table 
1ba6d 6f 66 20 61 20 6e 61 74 75 72 61 6c 20 6a 6f 69  of a natural joi
1ba6e 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  n,.             
1ba6f 20 20 20 2a 2a 20 74 68 65 6e 20 73 6b 69 70 20     ** then skip 
1ba70 74 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 20  the right table 
1ba71 74 6f 20 61 76 6f 69 64 20 61 20 64 75 70 6c 69  to avoid a dupli
1ba72 63 61 74 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20  cate match */.  
1ba73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 49                pI
1ba74 74 65 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20  tem++;.         
1ba75 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20         i++;.    
1ba76 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20            }else 
1ba77 69 66 28 20 28 70 55 73 69 6e 67 20 3d 20 70 49  if( (pUsing = pI
1ba78 74 65 6d 5b 31 5d 2e 70 55 73 69 6e 67 29 21 3d  tem[1].pUsing)!=
1ba79 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1ba7a 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20       /* If this 
1ba7b 6d 61 74 63 68 20 6f 63 63 75 72 73 20 6f 6e 20  match occurs on 
1ba7c 61 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 69 73  a column that is
1ba7d 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63 6c   in the USING cl
1ba7e 61 75 73 65 0a 20 20 20 20 20 20 20 20 20 20 20  ause.           
1ba7f 20 20 20 20 20 2a 2a 20 6f 66 20 61 20 6a 6f 69       ** of a joi
1ba80 6e 2c 20 73 6b 69 70 20 74 68 65 20 73 65 61 72  n, skip the sear
1ba81 63 68 20 6f 66 20 74 68 65 20 72 69 67 68 74 20  ch of the right 
1ba82 74 61 62 6c 65 20 6f 66 20 74 68 65 20 6a 6f 69  table of the joi
1ba83 6e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n.              
1ba84 20 20 2a 2a 20 74 6f 20 61 76 6f 69 64 20 61 20    ** to avoid a 
1ba85 64 75 70 6c 69 63 61 74 65 20 6d 61 74 63 68 20  duplicate match 
1ba86 74 68 65 72 65 2e 20 2a 2f 0a 20 20 20 20 20 20  there. */.      
1ba87 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b            int k;
1ba88 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ba89 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 55 73 69   for(k=0; k<pUsi
1ba8a 6e 67 2d 3e 6e 49 64 3b 20 6b 2b 2b 29 7b 0a 20  ng->nId; k++){. 
1ba8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1ba8c 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1ba8d 43 6d 70 28 70 55 73 69 6e 67 2d 3e 61 5b 6b 5d  Cmp(pUsing->a[k]
1ba8e 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29 3d 3d 30  .zName, zCol)==0
1ba8f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1ba90 20 20 20 20 20 20 20 20 70 49 74 65 6d 2b 2b 3b          pItem++;
1ba91 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1ba92 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20       i++;.      
1ba93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
1ba94 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
1ba95 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1ba96 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1ba97 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1ba98 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1ba99 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1ba9a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1ba9b 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  }.      }.    }.
1ba9c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1ba9d 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20 20  OMIT_TRIGGER.   
1ba9e 20 2f 2a 20 49 66 20 77 65 20 68 61 76 65 20 6e   /* If we have n
1ba9f 6f 74 20 61 6c 72 65 61 64 79 20 72 65 73 6f 6c  ot already resol
1baa0 76 65 64 20 74 68 65 20 6e 61 6d 65 2c 20 74 68  ved the name, th
1baa1 65 6e 20 6d 61 79 62 65 20 0a 20 20 20 20 2a 2a  en maybe .    **
1baa2 20 69 74 20 69 73 20 61 20 6e 65 77 2e 2a 20 6f   it is a new.* o
1baa3 72 20 6f 6c 64 2e 2a 20 74 72 69 67 67 65 72 20  r old.* trigger 
1baa4 61 72 67 75 6d 65 6e 74 20 72 65 66 65 72 65 6e  argument referen
1baa5 63 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66  ce.    */.    if
1baa6 28 20 7a 44 62 3d 3d 30 20 26 26 20 7a 54 61 62  ( zDb==0 && zTab
1baa7 21 3d 30 20 26 26 20 63 6e 74 3d 3d 30 20 26 26  !=0 && cnt==0 &&
1baa8 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61   pParse->trigSta
1baa9 63 6b 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 54  ck!=0 ){.      T
1baaa 72 69 67 67 65 72 53 74 61 63 6b 20 2a 70 54 72  riggerStack *pTr
1baab 69 67 67 65 72 53 74 61 63 6b 20 3d 20 70 50 61  iggerStack = pPa
1baac 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b 0a  rse->trigStack;.
1baad 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
1baae 62 20 3d 20 30 3b 0a 20 20 20 20 20 20 75 33 32  b = 0;.      u32
1baaf 20 2a 70 69 43 6f 6c 4d 61 73 6b 3b 0a 20 20 20   *piColMask;.   
1bab0 20 20 20 69 66 28 20 70 54 72 69 67 67 65 72 53     if( pTriggerS
1bab1 74 61 63 6b 2d 3e 6e 65 77 49 64 78 20 21 3d 20  tack->newIdx != 
1bab2 2d 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  -1 && sqlite3Str
1bab3 49 43 6d 70 28 22 6e 65 77 22 2c 20 7a 54 61 62  ICmp("new", zTab
1bab4 29 20 3d 3d 20 30 20 29 7b 0a 20 20 20 20 20 20  ) == 0 ){.      
1bab5 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1bab6 3d 20 70 54 72 69 67 67 65 72 53 74 61 63 6b 2d  = pTriggerStack-
1bab7 3e 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20 20  >newIdx;.       
1bab8 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67 65   assert( pTrigge
1bab9 72 53 74 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a  rStack->pTab );.
1baba 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 70          pTab = p
1babb 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 70 54  TriggerStack->pT
1babc 61 62 3b 0a 20 20 20 20 20 20 20 20 70 69 43 6f  ab;.        piCo
1babd 6c 4d 61 73 6b 20 3d 20 26 28 70 54 72 69 67 67  lMask = &(pTrigg
1babe 65 72 53 74 61 63 6b 2d 3e 6e 65 77 43 6f 6c 4d  erStack->newColM
1babf 61 73 6b 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ask);.      }els
1bac0 65 20 69 66 28 20 70 54 72 69 67 67 65 72 53 74  e if( pTriggerSt
1bac1 61 63 6b 2d 3e 6f 6c 64 49 64 78 20 21 3d 20 2d  ack->oldIdx != -
1bac2 31 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  1 && sqlite3StrI
1bac3 43 6d 70 28 22 6f 6c 64 22 2c 20 7a 54 61 62 29  Cmp("old", zTab)
1bac4 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70  ==0 ){.        p
1bac5 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1bac6 54 72 69 67 67 65 72 53 74 61 63 6b 2d 3e 6f 6c  TriggerStack->ol
1bac7 64 49 64 78 3b 0a 20 20 20 20 20 20 20 20 61 73  dIdx;.        as
1bac8 73 65 72 74 28 20 70 54 72 69 67 67 65 72 53 74  sert( pTriggerSt
1bac9 61 63 6b 2d 3e 70 54 61 62 20 29 3b 0a 20 20 20  ack->pTab );.   
1baca 20 20 20 20 20 70 54 61 62 20 3d 20 70 54 72 69       pTab = pTri
1bacb 67 67 65 72 53 74 61 63 6b 2d 3e 70 54 61 62 3b  ggerStack->pTab;
1bacc 0a 20 20 20 20 20 20 20 20 70 69 43 6f 6c 4d 61  .        piColMa
1bacd 73 6b 20 3d 20 26 28 70 54 72 69 67 67 65 72 53  sk = &(pTriggerS
1bace 74 61 63 6b 2d 3e 6f 6c 64 43 6f 6c 4d 61 73 6b  tack->oldColMask
1bacf 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
1bad0 20 20 69 66 28 20 70 54 61 62 20 29 7b 20 0a 20    if( pTab ){ . 
1bad1 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 3b         int iCol;
1bad2 0a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e 20  .        Column 
1bad3 2a 70 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61 43  *pCol = pTab->aC
1bad4 6f 6c 3b 0a 0a 20 20 20 20 20 20 20 20 70 53 63  ol;..        pSc
1bad5 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
1bad6 68 65 6d 61 3b 0a 20 20 20 20 20 20 20 20 63 6e  hema;.        cn
1bad7 74 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  tTab++;.        
1bad8 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69 43 6f 6c  for(iCol=0; iCol
1bad9 20 3c 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   < pTab->nCol; i
1bada 43 6f 6c 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 20 7b  Col++, pCol++) {
1badb 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73  .          if( s
1badc 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43  qlite3StrICmp(pC
1badd 6f 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c 29  ol->zName, zCol)
1bade 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
1badf 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
1bae0 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f        pExpr->iCo
1bae1 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3d 3d 70 54 61  lumn = iCol==pTa
1bae2 62 2d 3e 69 50 4b 65 79 20 3f 20 2d 31 20 3a 20  b->iPKey ? -1 : 
1bae3 69 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  iCol;.          
1bae4 20 20 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20    pExpr->pTab = 
1bae5 70 54 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20  pTab;.          
1bae6 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 29 7b    if( iCol>=0 ){
1bae7 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74  .              t
1bae8 65 73 74 63 61 73 65 28 20 69 43 6f 6c 3d 3d 33  estcase( iCol==3
1bae9 31 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  1 );.           
1baea 20 20 20 74 65 73 74 63 61 73 65 28 20 69 43 6f     testcase( iCo
1baeb 6c 3d 3d 33 32 20 29 3b 0a 20 20 20 20 20 20 20  l==32 );.       
1baec 20 20 20 20 20 20 20 2a 70 69 43 6f 6c 4d 61 73         *piColMas
1baed 6b 20 7c 3d 20 28 28 75 33 32 29 31 3c 3c 69 43  k |= ((u32)1<<iC
1baee 6f 6c 29 20 7c 20 28 69 43 6f 6c 3e 3d 33 32 3f  ol) | (iCol>=32?
1baef 30 78 66 66 66 66 66 66 66 66 3a 30 29 3b 0a 20  0xffffffff:0);. 
1baf0 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
1baf1 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
1baf2 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
1baf3 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1baf4 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64    }.#endif /* !d
1baf5 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1baf6 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f 0a 0a  IT_TRIGGER) */..
1baf7 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20 50 65      /*.    ** Pe
1baf8 72 68 61 70 73 20 74 68 65 20 6e 61 6d 65 20 69  rhaps the name i
1baf9 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f  s a reference to
1bafa 20 74 68 65 20 52 4f 57 49 44 0a 20 20 20 20 2a   the ROWID.    *
1bafb 2f 0a 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30  /.    if( cnt==0
1bafc 20 26 26 20 63 6e 74 54 61 62 3d 3d 31 20 26 26   && cntTab==1 &&
1bafd 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
1bafe 7a 43 6f 6c 29 20 29 7b 0a 20 20 20 20 20 20 63  zCol) ){.      c
1baff 6e 74 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 45  nt = 1;.      pE
1bb00 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d 20 2d  xpr->iColumn = -
1bb01 31 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e  1;.      pExpr->
1bb02 61 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54  affinity = SQLIT
1bb03 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
1bb04 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
1bb05 20 2a 2a 20 49 66 20 74 68 65 20 69 6e 70 75 74   ** If the input
1bb06 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20   is of the form 
1bb07 5a 20 28 6e 6f 74 20 59 2e 5a 20 6f 72 20 58 2e  Z (not Y.Z or X.
1bb08 59 2e 5a 29 20 74 68 65 6e 20 74 68 65 20 6e 61  Y.Z) then the na
1bb09 6d 65 20 5a 0a 20 20 20 20 2a 2a 20 6d 69 67 68  me Z.    ** migh
1bb0a 74 20 72 65 66 65 72 20 74 6f 20 61 6e 20 72 65  t refer to an re
1bb0b 73 75 6c 74 2d 73 65 74 20 61 6c 69 61 73 2e 20  sult-set alias. 
1bb0c 20 54 68 69 73 20 68 61 70 70 65 6e 73 2c 20 66   This happens, f
1bb0d 6f 72 20 65 78 61 6d 70 6c 65 2c 20 77 68 65 6e  or example, when
1bb0e 0a 20 20 20 20 2a 2a 20 77 65 20 61 72 65 20 72  .    ** we are r
1bb0f 65 73 6f 6c 76 69 6e 67 20 6e 61 6d 65 73 20 69  esolving names i
1bb10 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  n the WHERE clau
1bb11 73 65 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  se of the follow
1bb12 69 6e 67 20 63 6f 6d 6d 61 6e 64 3a 0a 20 20 20  ing command:.   
1bb13 20 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 53   **.    **     S
1bb14 45 4c 45 43 54 20 61 2b 62 20 41 53 20 78 20 46  ELECT a+b AS x F
1bb15 52 4f 4d 20 74 61 62 6c 65 20 57 48 45 52 45 20  ROM table WHERE 
1bb16 78 3c 31 30 3b 0a 20 20 20 20 2a 2a 0a 20 20 20  x<10;.    **.   
1bb17 20 2a 2a 20 49 6e 20 63 61 73 65 73 20 6c 69 6b   ** In cases lik
1bb18 65 20 74 68 69 73 2c 20 72 65 70 6c 61 63 65 20  e this, replace 
1bb19 70 45 78 70 72 20 77 69 74 68 20 61 20 63 6f 70  pExpr with a cop
1bb1a 79 20 6f 66 20 74 68 65 20 65 78 70 72 65 73 73  y of the express
1bb1b 69 6f 6e 20 74 68 61 74 0a 20 20 20 20 2a 2a 20  ion that.    ** 
1bb1c 66 6f 72 6d 73 20 74 68 65 20 72 65 73 75 6c 74  forms the result
1bb1d 20 73 65 74 20 65 6e 74 72 79 20 28 22 61 2b 62   set entry ("a+b
1bb1e 22 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65  " in the example
1bb1f 29 20 61 6e 64 20 72 65 74 75 72 6e 20 69 6d 6d  ) and return imm
1bb20 65 64 69 61 74 65 6c 79 2e 0a 20 20 20 20 2a 2a  ediately..    **
1bb21 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 20 65   Note that the e
1bb22 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 74 68 65  xpression in the
1bb23 20 72 65 73 75 6c 74 20 73 65 74 20 73 68 6f 75   result set shou
1bb24 6c 64 20 68 61 76 65 20 61 6c 72 65 61 64 79 20  ld have already 
1bb25 62 65 65 6e 0a 20 20 20 20 2a 2a 20 72 65 73 6f  been.    ** reso
1bb26 6c 76 65 64 20 62 79 20 74 68 65 20 74 69 6d 65  lved by the time
1bb27 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   the WHERE claus
1bb28 65 20 69 73 20 72 65 73 6f 6c 76 65 64 2e 0a 20  e is resolved.. 
1bb29 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 6e     */.    if( cn
1bb2a 74 3d 3d 30 20 26 26 20 28 70 45 4c 69 73 74 20  t==0 && (pEList 
1bb2b 3d 20 70 4e 43 2d 3e 70 45 4c 69 73 74 29 21 3d  = pNC->pEList)!=
1bb2c 30 20 26 26 20 7a 54 61 62 3d 3d 30 20 29 7b 0a  0 && zTab==0 ){.
1bb2d 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1bb2e 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  <pEList->nExpr; 
1bb2f 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 63 68  j++){.        ch
1bb30 61 72 20 2a 7a 41 73 20 3d 20 70 45 4c 69 73 74  ar *zAs = pEList
1bb31 2d 3e 61 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  ->a[j].zName;.  
1bb32 20 20 20 20 20 20 69 66 28 20 7a 41 73 21 3d 30        if( zAs!=0
1bb33 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49 43   && sqlite3StrIC
1bb34 6d 70 28 7a 41 73 2c 20 7a 43 6f 6c 29 3d 3d 30  mp(zAs, zCol)==0
1bb35 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 45 78   ){.          Ex
1bb36 70 72 20 2a 70 4f 72 69 67 3b 0a 20 20 20 20 20  pr *pOrig;.     
1bb37 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45 78       assert( pEx
1bb38 70 72 2d 3e 70 4c 65 66 74 3d 3d 30 20 26 26 20  pr->pLeft==0 && 
1bb39 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d 30  pExpr->pRight==0
1bb3a 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20 61 73   );.          as
1bb3b 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 69  sert( pExpr->pLi
1bb3c 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  st==0 );.       
1bb3d 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1bb3e 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 3b 0a  ->pSelect==0 );.
1bb3f 20 20 20 20 20 20 20 20 20 20 70 4f 72 69 67 20            pOrig 
1bb40 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 70  = pEList->a[j].p
1bb41 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
1bb42 69 66 28 20 21 70 4e 43 2d 3e 61 6c 6c 6f 77 41  if( !pNC->allowA
1bb43 67 67 20 26 26 20 45 78 70 72 48 61 73 50 72 6f  gg && ExprHasPro
1bb44 70 65 72 74 79 28 70 4f 72 69 67 2c 20 45 50 5f  perty(pOrig, EP_
1bb45 41 67 67 29 20 29 7b 0a 20 20 20 20 20 20 20 20  Agg) ){.        
1bb46 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1bb47 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73  Msg(pParse, "mis
1bb48 75 73 65 20 6f 66 20 61 6c 69 61 73 65 64 20 61  use of aliased a
1bb49 67 67 72 65 67 61 74 65 20 25 73 22 2c 20 7a 41  ggregate %s", zA
1bb4a 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  s);.            
1bb4b 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1bb4c 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , zCol);.       
1bb4d 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20       return 2;. 
1bb4e 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
1bb4f 20 20 20 20 20 72 65 73 6f 6c 76 65 41 6c 69 61       resolveAlia
1bb50 73 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73 74  s(pParse, pEList
1bb51 2c 20 6a 2c 20 70 45 78 70 72 2c 20 22 22 29 3b  , j, pExpr, "");
1bb52 0a 20 20 20 20 20 20 20 20 20 20 63 6e 74 20 3d  .          cnt =
1bb53 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4d   1;.          pM
1bb54 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20 20 20  atch = 0;.      
1bb55 20 20 20 20 61 73 73 65 72 74 28 20 7a 54 61 62      assert( zTab
1bb56 3d 3d 30 20 26 26 20 7a 44 62 3d 3d 30 20 29 3b  ==0 && zDb==0 );
1bb57 0a 20 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20  .          goto 
1bb58 6c 6f 6f 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32  lookupname_end_2
1bb59 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
1bb5a 20 20 7d 20 0a 20 20 20 20 7d 0a 0a 20 20 20 20    } .    }..    
1bb5b 2f 2a 20 41 64 76 61 6e 63 65 20 74 6f 20 74 68  /* Advance to th
1bb5c 65 20 6e 65 78 74 20 6e 61 6d 65 20 63 6f 6e 74  e next name cont
1bb5d 65 78 74 2e 20 20 54 68 65 20 6c 6f 6f 70 20 77  ext.  The loop w
1bb5e 69 6c 6c 20 65 78 69 74 20 77 68 65 6e 20 65 69  ill exit when ei
1bb5f 74 68 65 72 0a 20 20 20 20 2a 2a 20 77 65 20 68  ther.    ** we h
1bb60 61 76 65 20 61 20 6d 61 74 63 68 20 28 63 6e 74  ave a match (cnt
1bb61 3e 30 29 20 6f 72 20 77 68 65 6e 20 77 65 20 72  >0) or when we r
1bb62 75 6e 20 6f 75 74 20 6f 66 20 6e 61 6d 65 20 63  un out of name c
1bb63 6f 6e 74 65 78 74 73 2e 0a 20 20 20 20 2a 2f 0a  ontexts..    */.
1bb64 20 20 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29      if( cnt==0 )
1bb65 7b 0a 20 20 20 20 20 20 70 4e 43 20 3d 20 70 4e  {.      pNC = pN
1bb66 43 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a  C->pNext;.    }.
1bb67 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 49    }..  /*.  ** I
1bb68 66 20 58 20 61 6e 64 20 59 20 61 72 65 20 4e 55  f X and Y are NU
1bb69 4c 4c 20 28 69 6e 20 6f 74 68 65 72 20 77 6f 72  LL (in other wor
1bb6a 64 73 20 69 66 20 6f 6e 6c 79 20 74 68 65 20 63  ds if only the c
1bb6b 6f 6c 75 6d 6e 20 6e 61 6d 65 20 5a 20 69 73 0a  olumn name Z is.
1bb6c 20 20 2a 2a 20 73 75 70 70 6c 69 65 64 29 20 61    ** supplied) a
1bb6d 6e 64 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  nd the value of 
1bb6e 5a 20 69 73 20 65 6e 63 6c 6f 73 65 64 20 69 6e  Z is enclosed in
1bb6f 20 64 6f 75 62 6c 65 2d 71 75 6f 74 65 73 2c 20   double-quotes, 
1bb70 74 68 65 6e 0a 20 20 2a 2a 20 5a 20 69 73 20 61  then.  ** Z is a
1bb71 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20   string literal 
1bb72 69 66 20 69 74 20 64 6f 65 73 6e 27 74 20 6d 61  if it doesn't ma
1bb73 74 63 68 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6e  tch any column n
1bb74 61 6d 65 73 2e 20 20 49 6e 20 74 68 61 74 0a 20  ames.  In that. 
1bb75 20 2a 2a 20 63 61 73 65 2c 20 77 65 20 6e 65 65   ** case, we nee
1bb76 64 20 74 6f 20 72 65 74 75 72 6e 20 72 69 67 68  d to return righ
1bb77 74 20 61 77 61 79 20 61 6e 64 20 6e 6f 74 20 6d  t away and not m
1bb78 61 6b 65 20 61 6e 79 20 63 68 61 6e 67 65 73 20  ake any changes 
1bb79 74 6f 0a 20 20 2a 2a 20 70 45 78 70 72 2e 0a 20  to.  ** pExpr.. 
1bb7a 20 2a 2a 0a 20 20 2a 2a 20 42 65 63 61 75 73 65   **.  ** Because
1bb7b 20 6e 6f 20 72 65 66 65 72 65 6e 63 65 20 77 61   no reference wa
1bb7c 73 20 6d 61 64 65 20 74 6f 20 6f 75 74 65 72 20  s made to outer 
1bb7d 63 6f 6e 74 65 78 74 73 2c 20 74 68 65 20 70 4e  contexts, the pN
1bb7e 43 2d 3e 6e 52 65 66 0a 20 20 2a 2a 20 66 69 65  C->nRef.  ** fie
1bb7f 6c 64 73 20 61 72 65 20 6e 6f 74 20 63 68 61 6e  lds are not chan
1bb80 67 65 64 20 69 6e 20 61 6e 79 20 63 6f 6e 74 65  ged in any conte
1bb81 78 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63  xt..  */.  if( c
1bb82 6e 74 3d 3d 30 20 26 26 20 7a 54 61 62 3d 3d 30  nt==0 && zTab==0
1bb83 20 26 26 20 70 43 6f 6c 75 6d 6e 54 6f 6b 65 6e   && pColumnToken
1bb84 2d 3e 7a 5b 30 5d 3d 3d 27 22 27 20 29 7b 0a 20  ->z[0]=='"' ){. 
1bb85 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1bb86 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  (db, zCol);.    
1bb87 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53  pExpr->op = TK_S
1bb88 54 52 49 4e 47 3b 0a 20 20 20 20 70 45 78 70 72  TRING;.    pExpr
1bb89 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 20 20  ->pTab = 0;.    
1bb8a 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
1bb8b 20 2f 2a 0a 20 20 2a 2a 20 63 6e 74 3d 3d 30 20   /*.  ** cnt==0 
1bb8c 6d 65 61 6e 73 20 74 68 65 72 65 20 77 61 73 20  means there was 
1bb8d 6e 6f 74 20 6d 61 74 63 68 2e 20 20 63 6e 74 3e  not match.  cnt>
1bb8e 31 20 6d 65 61 6e 73 20 74 68 65 72 65 20 77 65  1 means there we
1bb8f 72 65 20 74 77 6f 20 6f 72 0a 20 20 2a 2a 20 6d  re two or.  ** m
1bb90 6f 72 65 20 6d 61 74 63 68 65 73 2e 20 20 45 69  ore matches.  Ei
1bb91 74 68 65 72 20 77 61 79 2c 20 77 65 20 68 61 76  ther way, we hav
1bb92 65 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a 2f  e an error..  */
1bb93 0a 20 20 69 66 28 20 63 6e 74 21 3d 31 20 29 7b  .  if( cnt!=1 ){
1bb94 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
1bb95 2a 7a 45 72 72 3b 0a 20 20 20 20 7a 45 72 72 20  *zErr;.    zErr 
1bb96 3d 20 63 6e 74 3d 3d 30 20 3f 20 22 6e 6f 20 73  = cnt==0 ? "no s
1bb97 75 63 68 20 63 6f 6c 75 6d 6e 22 20 3a 20 22 61  uch column" : "a
1bb98 6d 62 69 67 75 6f 75 73 20 63 6f 6c 75 6d 6e 20  mbiguous column 
1bb99 6e 61 6d 65 22 3b 0a 20 20 20 20 69 66 28 20 7a  name";.    if( z
1bb9a 44 62 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  Db ){.      sqli
1bb9b 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1bb9c 73 65 2c 20 22 25 73 3a 20 25 73 2e 25 73 2e 25  se, "%s: %s.%s.%
1bb9d 73 22 2c 20 7a 45 72 72 2c 20 7a 44 62 2c 20 7a  s", zErr, zDb, z
1bb9e 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20  Tab, zCol);.    
1bb9f 7d 65 6c 73 65 20 69 66 28 20 7a 54 61 62 20 29  }else if( zTab )
1bba0 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1bba1 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1bba2 22 25 73 3a 20 25 73 2e 25 73 22 2c 20 7a 45 72  "%s: %s.%s", zEr
1bba3 72 2c 20 7a 54 61 62 2c 20 7a 43 6f 6c 29 3b 0a  r, zTab, zCol);.
1bba4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1bba5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1bba6 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73  (pParse, "%s: %s
1bba7 22 2c 20 7a 45 72 72 2c 20 7a 43 6f 6c 29 3b 0a  ", zErr, zCol);.
1bba8 20 20 20 20 7d 0a 20 20 20 20 70 54 6f 70 4e 43      }.    pTopNC
1bba9 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a 0a 20  ->nErr++;.  }.. 
1bbaa 20 2f 2a 20 49 66 20 61 20 63 6f 6c 75 6d 6e 20   /* If a column 
1bbab 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 69 6e 20  from a table in 
1bbac 70 53 72 63 4c 69 73 74 20 69 73 20 72 65 66 65  pSrcList is refe
1bbad 72 65 6e 63 65 64 2c 20 74 68 65 6e 20 72 65 63  renced, then rec
1bbae 6f 72 64 0a 20 20 2a 2a 20 74 68 69 73 20 66 61  ord.  ** this fa
1bbaf 63 74 20 69 6e 20 74 68 65 20 70 53 72 63 4c 69  ct in the pSrcLi
1bbb0 73 74 2e 61 5b 5d 2e 63 6f 6c 55 73 65 64 20 62  st.a[].colUsed b
1bbb1 69 74 6d 61 73 6b 2e 20 20 43 6f 6c 75 6d 6e 20  itmask.  Column 
1bbb2 30 20 63 61 75 73 65 73 0a 20 20 2a 2a 20 62 69  0 causes.  ** bi
1bbb3 74 20 30 20 74 6f 20 62 65 20 73 65 74 2e 20 20  t 0 to be set.  
1bbb4 43 6f 6c 75 6d 6e 20 31 20 73 65 74 73 20 62 69  Column 1 sets bi
1bbb5 74 20 31 2e 20 20 41 6e 64 20 73 6f 20 66 6f 72  t 1.  And so for
1bbb6 74 68 2e 20 20 49 66 20 74 68 65 0a 20 20 2a 2a  th.  If the.  **
1bbb7 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 69   column number i
1bbb8 73 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74  s greater than t
1bbb9 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 69 74  he number of bit
1bbba 73 20 69 6e 20 74 68 65 20 62 69 74 6d 61 73 6b  s in the bitmask
1bbbb 0a 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 74  .  ** then set t
1bbbc 68 65 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69  he high-order bi
1bbbd 74 20 6f 66 20 74 68 65 20 62 69 74 6d 61 73 6b  t of the bitmask
1bbbe 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 45 78  ..  */.  if( pEx
1bbbf 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26  pr->iColumn>=0 &
1bbc0 26 20 70 4d 61 74 63 68 21 3d 30 20 29 7b 0a 20  & pMatch!=0 ){. 
1bbc1 20 20 20 69 6e 74 20 6e 20 3d 20 70 45 78 70 72     int n = pExpr
1bbc2 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 74  ->iColumn;.    t
1bbc3 65 73 74 63 61 73 65 28 20 6e 3d 3d 42 4d 53 2d  estcase( n==BMS-
1bbc4 31 20 29 3b 0a 20 20 20 20 69 66 28 20 6e 3e 3d  1 );.    if( n>=
1bbc5 42 4d 53 20 29 7b 0a 20 20 20 20 20 20 6e 20 3d  BMS ){.      n =
1bbc6 20 42 4d 53 2d 31 3b 0a 20 20 20 20 7d 0a 20 20   BMS-1;.    }.  
1bbc7 20 20 61 73 73 65 72 74 28 20 70 4d 61 74 63 68    assert( pMatch
1bbc8 2d 3e 69 43 75 72 73 6f 72 3d 3d 70 45 78 70 72  ->iCursor==pExpr
1bbc9 2d 3e 69 54 61 62 6c 65 20 29 3b 0a 20 20 20 20  ->iTable );.    
1bbca 70 4d 61 74 63 68 2d 3e 63 6f 6c 55 73 65 64 20  pMatch->colUsed 
1bbcb 7c 3d 20 28 28 42 69 74 6d 61 73 6b 29 31 29 3c  |= ((Bitmask)1)<
1bbcc 3c 6e 3b 0a 20 20 7d 0a 0a 6c 6f 6f 6b 75 70 6e  <n;.  }..lookupn
1bbcd 61 6d 65 5f 65 6e 64 3a 0a 20 20 2f 2a 20 43 6c  ame_end:.  /* Cl
1bbce 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75 72  ean up and retur
1bbcf 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  n.  */.  sqlite3
1bbd0 44 62 46 72 65 65 28 64 62 2c 20 7a 44 62 29 3b  DbFree(db, zDb);
1bbd1 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1bbd2 28 64 62 2c 20 7a 54 61 62 29 3b 0a 20 20 73 71  (db, zTab);.  sq
1bbd3 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1bbd4 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74  db, pExpr->pLeft
1bbd5 29 3b 0a 20 20 70 45 78 70 72 2d 3e 70 4c 65 66  );.  pExpr->pLef
1bbd6 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33  t = 0;.  sqlite3
1bbd7 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1bbd8 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20  Expr->pRight);. 
1bbd9 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 20 3d   pExpr->pRight =
1bbda 20 30 3b 0a 20 20 70 45 78 70 72 2d 3e 6f 70 20   0;.  pExpr->op 
1bbdb 3d 20 54 4b 5f 43 4f 4c 55 4d 4e 3b 0a 6c 6f 6f  = TK_COLUMN;.loo
1bbdc 6b 75 70 6e 61 6d 65 5f 65 6e 64 5f 32 3a 0a 20  kupname_end_2:. 
1bbdd 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1bbde 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20  b, zCol);.  if( 
1bbdf 63 6e 74 3d 3d 31 20 29 7b 0a 20 20 20 20 61 73  cnt==1 ){.    as
1bbe0 73 65 72 74 28 20 70 4e 43 21 3d 30 20 29 3b 0a  sert( pNC!=0 );.
1bbe1 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68 52      sqlite3AuthR
1bbe2 65 61 64 28 70 50 61 72 73 65 2c 20 70 45 78 70  ead(pParse, pExp
1bbe3 72 2c 20 70 53 63 68 65 6d 61 2c 20 70 4e 43 2d  r, pSchema, pNC-
1bbe4 3e 70 53 72 63 4c 69 73 74 29 3b 0a 20 20 20 20  >pSrcList);.    
1bbe5 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 74 68 65  /* Increment the
1bbe6 20 6e 52 65 66 20 76 61 6c 75 65 20 6f 6e 20 61   nRef value on a
1bbe7 6c 6c 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 73  ll name contexts
1bbe8 20 66 72 6f 6d 20 54 6f 70 4e 43 20 75 70 20 74   from TopNC up t
1bbe9 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20 70 6f 69  o.    ** the poi
1bbea 6e 74 20 77 68 65 72 65 20 74 68 65 20 6e 61 6d  nt where the nam
1bbeb 65 20 6d 61 74 63 68 65 64 2e 20 2a 2f 0a 20 20  e matched. */.  
1bbec 20 20 66 6f 72 28 3b 3b 29 7b 0a 20 20 20 20 20    for(;;){.     
1bbed 20 61 73 73 65 72 74 28 20 70 54 6f 70 4e 43 21   assert( pTopNC!
1bbee 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 54 6f 70  =0 );.      pTop
1bbef 4e 43 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  NC->nRef++;.    
1bbf0 20 20 69 66 28 20 70 54 6f 70 4e 43 3d 3d 70 4e    if( pTopNC==pN
1bbf1 43 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  C ) break;.     
1bbf2 20 70 54 6f 70 4e 43 20 3d 20 70 54 6f 70 4e 43   pTopNC = pTopNC
1bbf3 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 0a 20  ->pNext;.    }. 
1bbf4 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1bbf5 20 65 6c 73 65 20 7b 0a 20 20 20 20 72 65 74 75   else {.    retu
1bbf6 72 6e 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 1;.  }.}../*.
1bbf7 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1bbf8 69 73 20 63 61 6c 6c 62 61 63 6b 20 66 6f 72 20  is callback for 
1bbf9 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1bbfa 29 2e 0a 2a 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65  )..**.** Resolve
1bbfb 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 73 20   symbolic names 
1bbfc 69 6e 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 6f  into TK_COLUMN o
1bbfd 70 65 72 61 74 6f 72 73 20 66 6f 72 20 74 68 65  perators for the
1bbfe 20 63 75 72 72 65 6e 74 0a 2a 2a 20 6e 6f 64 65   current.** node
1bbff 20 69 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   in the expressi
1bc00 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72 6e  on tree.  Return
1bc01 20 30 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 74   0 to continue t
1bc02 68 65 20 73 65 61 72 63 68 20 64 6f 77 6e 0a 2a  he search down.*
1bc03 2a 20 74 68 65 20 74 72 65 65 20 6f 72 20 32 20  * the tree or 2 
1bc04 74 6f 20 61 62 6f 72 74 20 74 68 65 20 74 72 65  to abort the tre
1bc05 65 20 77 61 6c 6b 2e 0a 2a 2a 0a 2a 2a 20 54 68  e walk..**.** Th
1bc06 69 73 20 72 6f 75 74 69 6e 65 20 61 6c 73 6f 20  is routine also 
1bc07 64 6f 65 73 20 65 72 72 6f 72 20 63 68 65 63 6b  does error check
1bc08 69 6e 67 20 61 6e 64 20 6e 61 6d 65 20 72 65 73  ing and name res
1bc09 6f 6c 75 74 69 6f 6e 20 66 6f 72 0a 2a 2a 20 66  olution for.** f
1bc0a 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 2e 20 20  unction names.  
1bc0b 54 68 65 20 6f 70 65 72 61 74 6f 72 20 66 6f 72  The operator for
1bc0c 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1bc0d 69 6f 6e 73 20 69 73 20 63 68 61 6e 67 65 64 0a  ions is changed.
1bc0e 2a 2a 20 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e  ** to TK_AGG_FUN
1bc0f 43 54 49 4f 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63  CTION..*/.static
1bc10 20 69 6e 74 20 72 65 73 6f 6c 76 65 45 78 70 72   int resolveExpr
1bc11 53 74 65 70 28 57 61 6c 6b 65 72 20 2a 70 57 61  Step(Walker *pWa
1bc12 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78 70  lker, Expr *pExp
1bc13 72 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78  r){.  NameContex
1bc14 74 20 2a 70 4e 43 3b 0a 20 20 50 61 72 73 65 20  t *pNC;.  Parse 
1bc15 2a 70 50 61 72 73 65 3b 0a 0a 20 20 70 4e 43 20  *pParse;..  pNC 
1bc16 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43  = pWalker->u.pNC
1bc17 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4e 43 21  ;.  assert( pNC!
1bc18 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 20 3d  =0 );.  pParse =
1bc19 20 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20   pNC->pParse;.  
1bc1a 61 73 73 65 72 74 28 20 70 50 61 72 73 65 3d 3d  assert( pParse==
1bc1b 70 57 61 6c 6b 65 72 2d 3e 70 50 61 72 73 65 20  pWalker->pParse 
1bc1c 29 3b 0a 0a 20 20 69 66 28 20 45 78 70 72 48 61  );..  if( ExprHa
1bc1d 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 45 78  sAnyProperty(pEx
1bc1e 70 72 2c 20 45 50 5f 52 65 73 6f 6c 76 65 64 29  pr, EP_Resolved)
1bc1f 20 29 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72   ) return WRC_Pr
1bc20 75 6e 65 3b 0a 20 20 45 78 70 72 53 65 74 50 72  une;.  ExprSetPr
1bc21 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1bc22 5f 52 65 73 6f 6c 76 65 64 29 3b 0a 23 69 66 6e  _Resolved);.#ifn
1bc23 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28  def NDEBUG.  if(
1bc24 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 20 26   pNC->pSrcList &
1bc25 26 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 2d  & pNC->pSrcList-
1bc26 3e 6e 41 6c 6c 6f 63 3e 30 20 29 7b 0a 20 20 20  >nAlloc>0 ){.   
1bc27 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 4c 69   SrcList *pSrcLi
1bc28 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63 4c 69  st = pNC->pSrcLi
1bc29 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  st;.    int i;. 
1bc2a 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4e     for(i=0; i<pN
1bc2b 43 2d 3e 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72  C->pSrcList->nSr
1bc2c 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 61  c; i++){.      a
1bc2d 73 73 65 72 74 28 20 70 53 72 63 4c 69 73 74 2d  ssert( pSrcList-
1bc2e 3e 61 5b 69 5d 2e 69 43 75 72 73 6f 72 3e 3d 30  >a[i].iCursor>=0
1bc2f 20 26 26 20 70 53 72 63 4c 69 73 74 2d 3e 61 5b   && pSrcList->a[
1bc30 69 5d 2e 69 43 75 72 73 6f 72 3c 70 50 61 72 73  i].iCursor<pPars
1bc31 65 2d 3e 6e 54 61 62 29 3b 0a 20 20 20 20 7d 0a  e->nTab);.    }.
1bc32 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 77 69    }.#endif.  swi
1bc33 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1bc34 7b 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  {..#if defined(S
1bc35 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44  QLITE_ENABLE_UPD
1bc36 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54  ATE_DELETE_LIMIT
1bc37 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51  ) && !defined(SQ
1bc38 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1bc39 52 59 29 0a 20 20 20 20 2f 2a 20 54 68 65 20 73  RY).    /* The s
1bc3a 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f 72 20  pecial operator 
1bc3b 54 4b 5f 52 4f 57 20 6d 65 61 6e 73 20 75 73 65  TK_ROW means use
1bc3c 20 74 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74   the rowid for t
1bc3d 68 65 20 66 69 72 73 74 0a 20 20 20 20 2a 2a 20  he first.    ** 
1bc3e 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 46 52  column in the FR
1bc3f 4f 4d 20 63 6c 61 75 73 65 2e 20 20 54 68 69 73  OM clause.  This
1bc40 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
1bc41 4c 49 4d 49 54 20 61 6e 64 20 4f 52 44 45 52 20  LIMIT and ORDER 
1bc42 42 59 0a 20 20 20 20 2a 2a 20 63 6c 61 75 73 65  BY.    ** clause
1bc43 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 6e 20 55   processing on U
1bc44 50 44 41 54 45 20 61 6e 64 20 44 45 4c 45 54 45  PDATE and DELETE
1bc45 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20   statements..   
1bc46 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   */.    case TK_
1bc47 52 4f 57 3a 20 7b 0a 20 20 20 20 20 20 53 72 63  ROW: {.      Src
1bc48 4c 69 73 74 20 2a 70 53 72 63 4c 69 73 74 20 3d  List *pSrcList =
1bc49 20 70 4e 43 2d 3e 70 53 72 63 4c 69 73 74 3b 0a   pNC->pSrcList;.
1bc4a 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
1bc4b 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1bc4c 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1bc4d 70 53 72 63 4c 69 73 74 20 26 26 20 70 53 72 63  pSrcList && pSrc
1bc4e 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  List->nSrc==1 );
1bc4f 0a 20 20 20 20 20 20 70 49 74 65 6d 20 3d 20 70  .      pItem = p
1bc50 53 72 63 4c 69 73 74 2d 3e 61 3b 20 0a 20 20 20  SrcList->a; .   
1bc51 20 20 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54     pExpr->op = T
1bc52 4b 5f 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20  K_COLUMN;.      
1bc53 70 45 78 70 72 2d 3e 70 54 61 62 20 3d 20 70 49  pExpr->pTab = pI
1bc54 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20  tem->pTab;.     
1bc55 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1bc56 20 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b   pItem->iCursor;
1bc57 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
1bc58 6f 6c 75 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20  olumn = -1;.    
1bc59 20 20 70 45 78 70 72 2d 3e 61 66 66 69 6e 69 74    pExpr->affinit
1bc5a 79 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 49  y = SQLITE_AFF_I
1bc5b 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 62 72  NTEGER;.      br
1bc5c 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  eak;.    }.#endi
1bc5d 66 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c  f /* defined(SQL
1bc5e 49 54 45 5f 45 4e 41 42 4c 45 5f 55 50 44 41 54  ITE_ENABLE_UPDAT
1bc5f 45 5f 44 45 4c 45 54 45 5f 4c 49 4d 49 54 29 20  E_DELETE_LIMIT) 
1bc60 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  && !defined(SQLI
1bc61 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
1bc62 29 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 41 20 6c  ) */..    /* A l
1bc63 6f 6e 65 20 69 64 65 6e 74 69 66 69 65 72 20 69  one identifier i
1bc64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20  s the name of a 
1bc65 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20  column..    */. 
1bc66 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 20 7b     case TK_ID: {
1bc67 0a 20 20 20 20 20 20 6c 6f 6f 6b 75 70 4e 61 6d  .      lookupNam
1bc68 65 28 70 50 61 72 73 65 2c 20 30 2c 20 30 2c 20  e(pParse, 0, 0, 
1bc69 26 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2c 20 70  &pExpr->token, p
1bc6a 4e 43 2c 20 70 45 78 70 72 29 3b 0a 20 20 20 20  NC, pExpr);.    
1bc6b 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1bc6c 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20  ne;.    }.  .   
1bc6d 20 2f 2a 20 41 20 74 61 62 6c 65 20 6e 61 6d 65   /* A table name
1bc6e 20 61 6e 64 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65   and column name
1bc6f 3a 20 20 20 20 20 49 44 2e 49 44 0a 20 20 20 20  :     ID.ID.    
1bc70 2a 2a 20 4f 72 20 61 20 64 61 74 61 62 61 73 65  ** Or a database
1bc71 2c 20 74 61 62 6c 65 20 61 6e 64 20 63 6f 6c 75  , table and colu
1bc72 6d 6e 3a 20 20 49 44 2e 49 44 2e 49 44 0a 20 20  mn:  ID.ID.ID.  
1bc73 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b    */.    case TK
1bc74 5f 44 4f 54 3a 20 7b 0a 20 20 20 20 20 20 54 6f  _DOT: {.      To
1bc75 6b 65 6e 20 2a 70 43 6f 6c 75 6d 6e 3b 0a 20 20  ken *pColumn;.  
1bc76 20 20 20 20 54 6f 6b 65 6e 20 2a 70 54 61 62 6c      Token *pTabl
1bc77 65 3b 0a 20 20 20 20 20 20 54 6f 6b 65 6e 20 2a  e;.      Token *
1bc78 70 44 62 3b 0a 20 20 20 20 20 20 45 78 70 72 20  pDb;.      Expr 
1bc79 2a 70 52 69 67 68 74 3b 0a 0a 20 20 20 20 20 20  *pRight;..      
1bc7a 2f 2a 20 69 66 28 20 70 53 72 63 4c 69 73 74 3d  /* if( pSrcList=
1bc7b 3d 30 20 29 20 62 72 65 61 6b 3b 20 2a 2f 0a 20  =0 ) break; */. 
1bc7c 20 20 20 20 20 70 52 69 67 68 74 20 3d 20 70 45       pRight = pE
1bc7d 78 70 72 2d 3e 70 52 69 67 68 74 3b 0a 20 20 20  xpr->pRight;.   
1bc7e 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e 6f     if( pRight->o
1bc7f 70 3d 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20  p==TK_ID ){.    
1bc80 20 20 20 20 70 44 62 20 3d 20 30 3b 0a 20 20 20      pDb = 0;.   
1bc81 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20 26 70       pTable = &p
1bc82 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 74 6f 6b  Expr->pLeft->tok
1bc83 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c  en;.        pCol
1bc84 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e 74  umn = &pRight->t
1bc85 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 7d 65 6c 73  oken;.      }els
1bc86 65 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  e{.        asser
1bc87 74 28 20 70 52 69 67 68 74 2d 3e 6f 70 3d 3d 54  t( pRight->op==T
1bc88 4b 5f 44 4f 54 20 29 3b 0a 20 20 20 20 20 20 20  K_DOT );.       
1bc89 20 70 44 62 20 3d 20 26 70 45 78 70 72 2d 3e 70   pDb = &pExpr->p
1bc8a 4c 65 66 74 2d 3e 74 6f 6b 65 6e 3b 0a 20 20 20  Left->token;.   
1bc8b 20 20 20 20 20 70 54 61 62 6c 65 20 3d 20 26 70       pTable = &p
1bc8c 52 69 67 68 74 2d 3e 70 4c 65 66 74 2d 3e 74 6f  Right->pLeft->to
1bc8d 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 70 43 6f  ken;.        pCo
1bc8e 6c 75 6d 6e 20 3d 20 26 70 52 69 67 68 74 2d 3e  lumn = &pRight->
1bc8f 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 3b 0a 20  pRight->token;. 
1bc90 20 20 20 20 20 7d 0a 20 20 20 20 20 20 6c 6f 6f       }.      loo
1bc91 6b 75 70 4e 61 6d 65 28 70 50 61 72 73 65 2c 20  kupName(pParse, 
1bc92 70 44 62 2c 20 70 54 61 62 6c 65 2c 20 70 43 6f  pDb, pTable, pCo
1bc93 6c 75 6d 6e 2c 20 70 4e 43 2c 20 70 45 78 70 72  lumn, pNC, pExpr
1bc94 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
1bc95 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 7d  WRC_Prune;.    }
1bc96 0a 0a 20 20 20 20 2f 2a 20 52 65 73 6f 6c 76 65  ..    /* Resolve
1bc97 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 73 0a   function names.
1bc98 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
1bc99 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e 43 3a 0a 20  TK_CONST_FUNC:. 
1bc9a 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43 54     case TK_FUNCT
1bc9b 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 45 78 70  ION: {.      Exp
1bc9c 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70  rList *pList = p
1bc9d 45 78 70 72 2d 3e 70 4c 69 73 74 3b 20 20 20 20  Expr->pList;    
1bc9e 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20  /* The argument 
1bc9f 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 69 6e  list */.      in
1bca0 74 20 6e 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c  t n = pList ? pL
1bca1 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 20  ist->nExpr : 0; 
1bca2 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72   /* Number of ar
1bca3 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 20  guments */.     
1bca4 20 69 6e 74 20 6e 6f 5f 73 75 63 68 5f 66 75 6e   int no_such_fun
1bca5 63 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  c = 0;       /* 
1bca6 54 72 75 65 20 69 66 20 6e 6f 20 73 75 63 68 20  True if no such 
1bca7 66 75 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 20  function exists 
1bca8 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 77 72 6f  */.      int wro
1bca9 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 3d 20 30 3b  ng_num_args = 0;
1bcaa 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1bcab 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  wrong number of 
1bcac 61 72 67 75 6d 65 6e 74 73 20 2a 2f 0a 20 20 20  arguments */.   
1bcad 20 20 20 69 6e 74 20 69 73 5f 61 67 67 20 3d 20     int is_agg = 
1bcae 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  0;             /
1bcaf 2a 20 54 72 75 65 20 69 66 20 69 73 20 61 6e 20  * True if is an 
1bcb0 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69  aggregate functi
1bcb1 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  on */.      int 
1bcb2 61 75 74 68 3b 20 20 20 20 20 20 20 20 20 20 20  auth;           
1bcb3 20 20 20 20 20 20 20 20 2f 2a 20 41 75 74 68 6f          /* Autho
1bcb4 72 69 7a 61 74 69 6f 6e 20 74 6f 20 75 73 65 20  rization to use 
1bcb5 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
1bcb6 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 20 20        int nId;  
1bcb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bcb8 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
1bcb9 68 61 72 61 63 74 65 72 73 20 69 6e 20 66 75 6e  haracters in fun
1bcba 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f 0a 20 20  ction name */.  
1bcbb 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
1bcbc 7a 49 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  zId;            
1bcbd 2f 2a 20 54 68 65 20 66 75 6e 63 74 69 6f 6e 20  /* The function 
1bcbe 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 46  name. */.      F
1bcbf 75 6e 63 44 65 66 20 2a 70 44 65 66 3b 20 20 20  uncDef *pDef;   
1bcc0 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
1bcc1 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
1bcc2 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  the function */.
1bcc3 20 20 20 20 20 20 69 6e 74 20 65 6e 63 20 3d 20        int enc = 
1bcc4 45 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 3b  ENC(pParse->db);
1bcc5 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
1bcc6 65 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 0a 20  e encoding */.. 
1bcc7 20 20 20 20 20 7a 49 64 20 3d 20 28 63 68 61 72       zId = (char
1bcc8 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a  *)pExpr->token.z
1bcc9 3b 0a 20 20 20 20 20 20 6e 49 64 20 3d 20 70 45  ;.      nId = pE
1bcca 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b 0a 20 20  xpr->token.n;.  
1bccb 20 20 20 20 70 44 65 66 20 3d 20 73 71 6c 69 74      pDef = sqlit
1bccc 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 70  e3FindFunction(p
1bccd 50 61 72 73 65 2d 3e 64 62 2c 20 7a 49 64 2c 20  Parse->db, zId, 
1bcce 6e 49 64 2c 20 6e 2c 20 65 6e 63 2c 20 30 29 3b  nId, n, enc, 0);
1bccf 0a 20 20 20 20 20 20 69 66 28 20 70 44 65 66 3d  .      if( pDef=
1bcd0 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44  =0 ){.        pD
1bcd1 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ef = sqlite3Find
1bcd2 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
1bcd3 3e 64 62 2c 20 7a 49 64 2c 20 6e 49 64 2c 20 2d  >db, zId, nId, -
1bcd4 31 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 20 20  1, enc, 0);.    
1bcd5 20 20 20 20 69 66 28 20 70 44 65 66 3d 3d 30 20      if( pDef==0 
1bcd6 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6e 6f 5f  ){.          no_
1bcd7 73 75 63 68 5f 66 75 6e 63 20 3d 20 31 3b 0a 20  such_func = 1;. 
1bcd8 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
1bcd9 20 20 20 20 20 20 20 20 77 72 6f 6e 67 5f 6e 75          wrong_nu
1bcda 6d 5f 61 72 67 73 20 3d 20 31 3b 0a 20 20 20 20  m_args = 1;.    
1bcdb 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73      }.      }els
1bcdc 65 7b 0a 20 20 20 20 20 20 20 20 69 73 5f 61 67  e{.        is_ag
1bcdd 67 20 3d 20 70 44 65 66 2d 3e 78 46 75 6e 63 3d  g = pDef->xFunc=
1bcde 3d 30 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66 6e  =0;.      }.#ifn
1bcdf 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bce0 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
1bce1 20 20 20 20 69 66 28 20 70 44 65 66 20 29 7b 0a      if( pDef ){.
1bce2 20 20 20 20 20 20 20 20 61 75 74 68 20 3d 20 73          auth = s
1bce3 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1bce4 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 46  pParse, SQLITE_F
1bce5 55 4e 43 54 49 4f 4e 2c 20 30 2c 20 70 44 65 66  UNCTION, 0, pDef
1bce6 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
1bce7 20 20 20 20 20 69 66 28 20 61 75 74 68 21 3d 53       if( auth!=S
1bce8 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
1bce9 20 20 20 20 20 20 69 66 28 20 61 75 74 68 3d 3d        if( auth==
1bcea 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
1bceb 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1bcec 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1bced 65 2c 20 22 6e 6f 74 20 61 75 74 68 6f 72 69 7a  e, "not authoriz
1bcee 65 64 20 74 6f 20 75 73 65 20 66 75 6e 63 74 69  ed to use functi
1bcef 6f 6e 3a 20 25 73 22 2c 0a 20 20 20 20 20 20 20  on: %s",.       
1bcf0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bcf1 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44 65               pDe
1bcf2 66 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  f->zName);.     
1bcf3 20 20 20 20 20 20 20 70 4e 43 2d 3e 6e 45 72 72         pNC->nErr
1bcf4 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ++;.          }.
1bcf5 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d            pExpr-
1bcf6 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a 20  >op = TK_NULL;. 
1bcf7 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1bcf8 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20 20  WRC_Prune;.     
1bcf9 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 23 65 6e     }.      }.#en
1bcfa 64 69 66 0a 20 20 20 20 20 20 69 66 28 20 69 73  dif.      if( is
1bcfb 5f 61 67 67 20 26 26 20 21 70 4e 43 2d 3e 61 6c  _agg && !pNC->al
1bcfc 6c 6f 77 41 67 67 20 29 7b 0a 20 20 20 20 20 20  lowAgg ){.      
1bcfd 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1bcfe 67 28 70 50 61 72 73 65 2c 20 22 6d 69 73 75 73  g(pParse, "misus
1bcff 65 20 6f 66 20 61 67 67 72 65 67 61 74 65 20 66  e of aggregate f
1bd00 75 6e 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c  unction %.*s()",
1bd01 20 6e 49 64 2c 7a 49 64 29 3b 0a 20 20 20 20 20   nId,zId);.     
1bd02 20 20 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a     pNC->nErr++;.
1bd03 20 20 20 20 20 20 20 20 69 73 5f 61 67 67 20 3d          is_agg =
1bd04 20 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20   0;.      }else 
1bd05 69 66 28 20 6e 6f 5f 73 75 63 68 5f 66 75 6e 63  if( no_such_func
1bd06 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
1bd07 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1bd08 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 66 75 6e  se, "no such fun
1bd09 63 74 69 6f 6e 3a 20 25 2e 2a 73 22 2c 20 6e 49  ction: %.*s", nI
1bd0a 64 2c 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20  d, zId);.       
1bd0b 20 70 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20   pNC->nErr++;.  
1bd0c 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 77 72      }else if( wr
1bd0d 6f 6e 67 5f 6e 75 6d 5f 61 72 67 73 20 29 7b 0a  ong_num_args ){.
1bd0e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
1bd0f 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22  rrorMsg(pParse,"
1bd10 77 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  wrong number of 
1bd11 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 66 75 6e  arguments to fun
1bd12 63 74 69 6f 6e 20 25 2e 2a 73 28 29 22 2c 0a 20  ction %.*s()",. 
1bd13 20 20 20 20 20 20 20 20 20 20 20 20 6e 49 64 2c              nId,
1bd14 20 7a 49 64 29 3b 0a 20 20 20 20 20 20 20 20 70   zId);.        p
1bd15 4e 43 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20  NC->nErr++;.    
1bd16 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 73    }.      if( is
1bd17 5f 61 67 67 20 29 7b 0a 20 20 20 20 20 20 20 20  _agg ){.        
1bd18 70 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41  pExpr->op = TK_A
1bd19 47 47 5f 46 55 4e 43 54 49 4f 4e 3b 0a 20 20 20  GG_FUNCTION;.   
1bd1a 20 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67       pNC->hasAgg
1bd1b 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
1bd1c 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20 29      if( is_agg )
1bd1d 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20 3d   pNC->allowAgg =
1bd1e 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   0;.      sqlite
1bd1f 33 57 61 6c 6b 45 78 70 72 4c 69 73 74 28 70 57  3WalkExprList(pW
1bd20 61 6c 6b 65 72 2c 20 70 4c 69 73 74 29 3b 0a 20  alker, pList);. 
1bd21 20 20 20 20 20 69 66 28 20 69 73 5f 61 67 67 20       if( is_agg 
1bd22 29 20 70 4e 43 2d 3e 61 6c 6c 6f 77 41 67 67 20  ) pNC->allowAgg 
1bd23 3d 20 31 3b 0a 20 20 20 20 20 20 2f 2a 20 46 49  = 1;.      /* FI
1bd24 58 20 4d 45 3a 20 20 43 6f 6d 70 75 74 65 20 70  X ME:  Compute p
1bd25 45 78 70 72 2d 3e 61 66 66 69 6e 69 74 79 20 62  Expr->affinity b
1bd26 61 73 65 64 20 6f 6e 20 74 68 65 20 65 78 70 65  ased on the expe
1bd27 63 74 65 64 20 72 65 74 75 72 6e 0a 20 20 20 20  cted return.    
1bd28 20 20 2a 2a 20 74 79 70 65 20 6f 66 20 74 68 65    ** type of the
1bd29 20 66 75 6e 63 74 69 6f 6e 20 0a 20 20 20 20 20   function .     
1bd2a 20 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e   */.      return
1bd2b 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 20 20   WRC_Prune;.    
1bd2c 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1bd2d 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20  _OMIT_SUBQUERY. 
1bd2e 20 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43     case TK_SELEC
1bd2f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  T:.    case TK_E
1bd30 58 49 53 54 53 3a 0a 23 65 6e 64 69 66 0a 20 20  XISTS:.#endif.  
1bd31 20 20 63 61 73 65 20 54 4b 5f 49 4e 3a 20 7b 0a    case TK_IN: {.
1bd32 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1bd33 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
1bd34 20 20 20 20 69 6e 74 20 6e 52 65 66 20 3d 20 70      int nRef = p
1bd35 4e 43 2d 3e 6e 52 65 66 3b 0a 23 69 66 6e 64 65  NC->nRef;.#ifnde
1bd36 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48  f SQLITE_OMIT_CH
1bd37 45 43 4b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ECK.        if( 
1bd38 70 4e 43 2d 3e 69 73 43 68 65 63 6b 20 29 7b 0a  pNC->isCheck ){.
1bd39 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1bd3a 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1bd3b 2c 22 73 75 62 71 75 65 72 69 65 73 20 70 72 6f  ,"subqueries pro
1bd3c 68 69 62 69 74 65 64 20 69 6e 20 43 48 45 43 4b  hibited in CHECK
1bd3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 22 29 3b 0a   constraints");.
1bd3e 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66          }.#endif
1bd3f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1bd40 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c 6b  WalkSelect(pWalk
1bd41 65 72 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65  er, pExpr->pSele
1bd42 63 74 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73  ct);.        ass
1bd43 65 72 74 28 20 70 4e 43 2d 3e 6e 52 65 66 3e 3d  ert( pNC->nRef>=
1bd44 6e 52 65 66 20 29 3b 0a 20 20 20 20 20 20 20 20  nRef );.        
1bd45 69 66 28 20 6e 52 65 66 21 3d 70 4e 43 2d 3e 6e  if( nRef!=pNC->n
1bd46 52 65 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20  Ref ){.         
1bd47 20 45 78 70 72 53 65 74 50 72 6f 70 65 72 74 79   ExprSetProperty
1bd48 28 70 45 78 70 72 2c 20 45 50 5f 56 61 72 53 65  (pExpr, EP_VarSe
1bd49 6c 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d  lect);.        }
1bd4a 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
1bd4b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e  reak;.    }.#ifn
1bd4c 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1bd4d 43 48 45 43 4b 0a 20 20 20 20 63 61 73 65 20 54  CHECK.    case T
1bd4e 4b 5f 56 41 52 49 41 42 4c 45 3a 20 7b 0a 20 20  K_VARIABLE: {.  
1bd4f 20 20 20 20 69 66 28 20 70 4e 43 2d 3e 69 73 43      if( pNC->isC
1bd50 68 65 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20  heck ){.        
1bd51 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1bd52 70 50 61 72 73 65 2c 22 70 61 72 61 6d 65 74 65  pParse,"paramete
1bd53 72 73 20 70 72 6f 68 69 62 69 74 65 64 20 69 6e  rs prohibited in
1bd54 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69 6e   CHECK constrain
1bd55 74 73 22 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ts");.      }.  
1bd56 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1bd57 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 72 65  .#endif.  }.  re
1bd58 74 75 72 6e 20 28 70 50 61 72 73 65 2d 3e 6e 45  turn (pParse->nE
1bd59 72 72 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62  rr || pParse->db
1bd5a 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20  ->mallocFailed) 
1bd5b 3f 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52  ? WRC_Abort : WR
1bd5c 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
1bd5d 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 69 73 20 61  *.** pEList is a
1bd5e 20 6c 69 73 74 20 6f 66 20 65 78 70 72 65 73 73   list of express
1bd5f 69 6f 6e 73 20 77 68 69 63 68 20 61 72 65 20 72  ions which are r
1bd60 65 61 6c 6c 79 20 74 68 65 20 72 65 73 75 6c 74  eally the result
1bd61 20 73 65 74 20 6f 66 20 74 68 65 0a 2a 2a 20 61   set of the.** a
1bd62 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
1bd63 74 2e 20 20 70 45 20 69 73 20 61 20 74 65 72 6d  t.  pE is a term
1bd64 20 69 6e 20 61 6e 20 4f 52 44 45 52 20 42 59 20   in an ORDER BY 
1bd65 6f 72 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  or GROUP BY clau
1bd66 73 65 2e 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  se..** This rout
1bd67 69 6e 65 20 63 68 65 63 6b 73 20 74 6f 20 73 65  ine checks to se
1bd68 65 20 69 66 20 70 45 20 69 73 20 61 20 73 69 6d  e if pE is a sim
1bd69 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 20 77  ple identifier w
1bd6a 68 69 63 68 20 63 6f 72 72 65 73 70 6f 6e 64 73  hich corresponds
1bd6b 0a 2a 2a 20 74 6f 20 74 68 65 20 41 53 2d 6e 61  .** to the AS-na
1bd6c 6d 65 20 6f 66 20 6f 6e 65 20 6f 66 20 74 68 65  me of one of the
1bd6d 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 65 78   terms of the ex
1bd6e 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 20 20  pression list.  
1bd6f 49 66 20 69 74 20 69 73 2c 0a 2a 2a 20 74 68 69  If it is,.** thi
1bd70 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e  s routine return
1bd71 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
1bd72 65 65 6e 20 31 20 61 6e 64 20 4e 20 77 68 65 72  een 1 and N wher
1bd73 65 20 4e 20 69 73 20 74 68 65 20 6e 75 6d 62 65  e N is the numbe
1bd74 72 20 6f 66 0a 2a 2a 20 65 6c 65 6d 65 6e 74 73  r of.** elements
1bd75 20 69 6e 20 70 45 4c 69 73 74 2c 20 63 6f 72 72   in pEList, corr
1bd76 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68 65  esponding to the
1bd77 20 6d 61 74 63 68 69 6e 67 20 65 6e 74 72 79 2e   matching entry.
1bd78 20 20 49 66 20 74 68 65 72 65 20 69 73 0a 2a 2a    If there is.**
1bd79 20 6e 6f 20 6d 61 74 63 68 2c 20 6f 72 20 69 66   no match, or if
1bd7a 20 70 45 20 69 73 20 6e 6f 74 20 61 20 73 69 6d   pE is not a sim
1bd7b 70 6c 65 20 69 64 65 6e 74 69 66 69 65 72 2c 20  ple identifier, 
1bd7c 74 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e  then this routin
1bd7d 65 0a 2a 2a 20 72 65 74 75 72 6e 20 30 2e 0a 2a  e.** return 0..*
1bd7e 2a 0a 2a 2a 20 70 45 4c 69 73 74 20 68 61 73 20  *.** pEList has 
1bd7f 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 20 20  been resolved.  
1bd80 70 45 20 68 61 73 20 6e 6f 74 2e 0a 2a 2f 0a 73  pE has not..*/.s
1bd81 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
1bd82 65 41 73 4e 61 6d 65 28 0a 20 20 50 61 72 73 65  eAsName(.  Parse
1bd83 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
1bd84 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
1bd85 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61   for error messa
1bd86 67 65 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73  ges */.  ExprLis
1bd87 74 20 2a 70 45 4c 69 73 74 2c 20 20 2f 2a 20 4c  t *pEList,  /* L
1bd88 69 73 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f  ist of expressio
1bd89 6e 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20  ns to scan */.  
1bd8a 45 78 70 72 20 2a 70 45 20 20 20 20 20 20 20 20  Expr *pE        
1bd8b 20 20 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e     /* Expression
1bd8c 20 77 65 20 61 72 65 20 74 72 79 69 6e 67 20 74   we are trying t
1bd8d 6f 20 6d 61 74 63 68 20 2a 2f 0a 29 7b 0a 20 20  o match */.){.  
1bd8e 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20 20  int i;          
1bd8f 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74     /* Loop count
1bd90 65 72 20 2a 2f 0a 0a 20 20 69 66 28 20 70 45 2d  er */..  if( pE-
1bd91 3e 6f 70 3d 3d 54 4b 5f 49 44 20 7c 7c 20 28 70  >op==TK_ID || (p
1bd92 45 2d 3e 6f 70 3d 3d 54 4b 5f 53 54 52 49 4e 47  E->op==TK_STRING
1bd93 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e 2e 7a 5b   && pE->token.z[
1bd94 30 5d 21 3d 27 5c 27 27 29 20 29 7b 0a 20 20 20  0]!='\'') ){.   
1bd95 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
1bd96 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 63  Parse->db;.    c
1bd97 68 61 72 20 2a 7a 43 6f 6c 20 3d 20 73 71 6c 69  har *zCol = sqli
1bd98 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1bd99 28 64 62 2c 20 26 70 45 2d 3e 74 6f 6b 65 6e 29  (db, &pE->token)
1bd9a 3b 0a 20 20 20 20 69 66 28 20 7a 43 6f 6c 3d 3d  ;.    if( zCol==
1bd9b 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
1bd9c 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  n -1;.    }.    
1bd9d 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73  for(i=0; i<pELis
1bd9e 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
1bd9f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 41 73 20        char *zAs 
1bda0 3d 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a  = pEList->a[i].z
1bda1 4e 61 6d 65 3b 0a 20 20 20 20 20 20 69 66 28 20  Name;.      if( 
1bda2 7a 41 73 21 3d 30 20 26 26 20 73 71 6c 69 74 65  zAs!=0 && sqlite
1bda3 33 53 74 72 49 43 6d 70 28 7a 41 73 2c 20 7a 43  3StrICmp(zAs, zC
1bda4 6f 6c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ol)==0 ){.      
1bda5 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1bda6 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20  db, zCol);.     
1bda7 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a 20     return i+1;. 
1bda8 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
1bda9 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1bdaa 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 7d 0a 20 20  b, zCol);.  }.  
1bdab 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
1bdac 2a 2a 20 70 45 20 69 73 20 61 20 70 6f 69 6e 74  ** pE is a point
1bdad 65 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73  er to an express
1bdae 69 6f 6e 20 77 68 69 63 68 20 69 73 20 61 20 73  ion which is a s
1bdaf 69 6e 67 6c 65 20 74 65 72 6d 20 69 6e 20 74 68  ingle term in th
1bdb0 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59 20 6f 66  e.** ORDER BY of
1bdb1 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45   a compound SELE
1bdb2 43 54 2e 20 20 54 68 65 20 65 78 70 72 65 73 73  CT.  The express
1bdb3 69 6f 6e 20 68 61 73 20 6e 6f 74 20 62 65 65 6e  ion has not been
1bdb4 0a 2a 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 76 65  .** name resolve
1bdb5 64 2e 0a 2a 2a 0a 2a 2a 20 41 74 20 74 68 65 20  d..**.** At the 
1bdb6 70 6f 69 6e 74 20 74 68 69 73 20 72 6f 75 74 69  point this routi
1bdb7 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20 77 65  ne is called, we
1bdb8 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77 20 74 68   already know th
1bdb9 61 74 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20  at the.** ORDER 
1bdba 42 59 20 74 65 72 6d 20 69 73 20 6e 6f 74 20 61  BY term is not a
1bdbb 6e 20 69 6e 74 65 67 65 72 20 69 6e 64 65 78 20  n integer index 
1bdbc 69 6e 74 6f 20 74 68 65 20 72 65 73 75 6c 74 20  into the result 
1bdbd 73 65 74 2e 20 20 54 68 61 74 0a 2a 2a 20 63 61  set.  That.** ca
1bdbe 73 65 20 69 73 20 68 61 6e 64 6c 65 64 20 62 79  se is handled by
1bdbf 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 72 6f 75   the calling rou
1bdc0 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 41 74 74 65  tine..**.** Atte
1bdc1 6d 70 74 20 74 6f 20 6d 61 74 63 68 20 70 45 20  mpt to match pE 
1bdc2 61 67 61 69 6e 73 74 20 72 65 73 75 6c 74 20 73  against result s
1bdc3 65 74 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68  et columns in th
1bdc4 65 20 6c 65 66 74 2d 6d 6f 73 74 0a 2a 2a 20 53  e left-most.** S
1bdc5 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
1bdc6 20 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64    Return the ind
1bdc7 65 78 20 69 20 6f 66 20 74 68 65 20 6d 61 74 63  ex i of the matc
1bdc8 68 69 6e 67 20 63 6f 6c 75 6d 6e 2c 0a 2a 2a 20  hing column,.** 
1bdc9 61 73 20 61 6e 20 69 6e 64 69 63 61 74 69 6f 6e  as an indication
1bdca 20 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 74   to the caller t
1bdcb 68 61 74 20 69 74 20 73 68 6f 75 6c 64 20 73 6f  hat it should so
1bdcc 72 74 20 62 79 20 74 68 65 20 69 2d 74 68 20 63  rt by the i-th c
1bdcd 6f 6c 75 6d 6e 2e 0a 2a 2a 20 54 68 65 20 6c 65  olumn..** The le
1bdce 66 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 69  ft-most column i
1bdcf 73 20 31 2e 20 20 49 6e 20 6f 74 68 65 72 20 77  s 1.  In other w
1bdd0 6f 72 64 73 2c 20 74 68 65 20 76 61 6c 75 65 20  ords, the value 
1bdd1 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65 0a  returned is the.
1bdd2 2a 2a 20 73 61 6d 65 20 69 6e 74 65 67 65 72 20  ** same integer 
1bdd3 76 61 6c 75 65 20 74 68 61 74 20 77 6f 75 6c 64  value that would
1bdd4 20 62 65 20 75 73 65 64 20 69 6e 20 74 68 65 20   be used in the 
1bdd5 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74 6f  SQL statement to
1bdd6 20 69 6e 64 69 63 61 74 65 0a 2a 2a 20 74 68 65   indicate.** the
1bdd7 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49   column..**.** I
1bdd8 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6d 61  f there is no ma
1bdd9 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20 20  tch, return 0.  
1bdda 52 65 74 75 72 6e 20 2d 31 20 69 66 20 61 6e 20  Return -1 if an 
1bddb 65 72 72 6f 72 20 6f 63 63 75 72 73 2e 0a 2a 2f  error occurs..*/
1bddc 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f  .static int reso
1bddd 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f  lveOrderByTermTo
1bdde 45 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73  ExprList(.  Pars
1bddf 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
1bde0 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1bde1 74 20 66 6f 72 20 65 72 72 6f 72 20 6d 65 73 73  t for error mess
1bde2 61 67 65 73 20 2a 2f 0a 20 20 53 65 6c 65 63 74  ages */.  Select
1bde3 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 2f 2a 20   *pSelect,   /* 
1bde4 54 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65  The SELECT state
1bde5 6d 65 6e 74 20 77 69 74 68 20 74 68 65 20 4f 52  ment with the OR
1bde6 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
1bde7 0a 20 20 45 78 70 72 20 2a 70 45 20 20 20 20 20  .  Expr *pE     
1bde8 20 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65        /* The spe
1bde9 63 69 66 69 63 20 4f 52 44 45 52 20 42 59 20 74  cific ORDER BY t
1bdea 65 72 6d 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  erm */.){.  int 
1bdeb 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  i;             /
1bdec 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a  * Loop counter *
1bded 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
1bdee 4c 69 73 74 3b 20 20 2f 2a 20 54 68 65 20 63 6f  List;  /* The co
1bdef 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 72 65 73  lumns of the res
1bdf0 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 4e 61 6d  ult set */.  Nam
1bdf1 65 43 6f 6e 74 65 78 74 20 6e 63 3b 20 20 20 20  eContext nc;    
1bdf2 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74 20  /* Name context 
1bdf3 66 6f 72 20 72 65 73 6f 6c 76 69 6e 67 20 70 45  for resolving pE
1bdf4 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 73   */..  assert( s
1bdf5 71 6c 69 74 65 33 45 78 70 72 49 73 49 6e 74 65  qlite3ExprIsInte
1bdf6 67 65 72 28 70 45 2c 20 26 69 29 3d 3d 30 20 29  ger(pE, &i)==0 )
1bdf7 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65  ;.  pEList = pSe
1bdf8 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 0a 20  lect->pEList;.. 
1bdf9 20 2f 2a 20 52 65 73 6f 6c 76 65 20 61 6c 6c 20   /* Resolve all 
1bdfa 6e 61 6d 65 73 20 69 6e 20 74 68 65 20 4f 52 44  names in the ORD
1bdfb 45 52 20 42 59 20 74 65 72 6d 20 65 78 70 72 65  ER BY term expre
1bdfc 73 73 69 6f 6e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  ssion.  */.  mem
1bdfd 73 65 74 28 26 6e 63 2c 20 30 2c 20 73 69 7a 65  set(&nc, 0, size
1bdfe 6f 66 28 6e 63 29 29 3b 0a 20 20 6e 63 2e 70 50  of(nc));.  nc.pP
1bdff 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20  arse = pParse;. 
1be00 20 6e 63 2e 70 53 72 63 4c 69 73 74 20 3d 20 70   nc.pSrcList = p
1be01 53 65 6c 65 63 74 2d 3e 70 53 72 63 3b 0a 20 20  Select->pSrc;.  
1be02 6e 63 2e 70 45 4c 69 73 74 20 3d 20 70 45 4c 69  nc.pEList = pELi
1be03 73 74 3b 0a 20 20 6e 63 2e 61 6c 6c 6f 77 41 67  st;.  nc.allowAg
1be04 67 20 3d 20 31 3b 0a 20 20 6e 63 2e 6e 45 72 72  g = 1;.  nc.nErr
1be05 20 3d 20 30 3b 0a 20 20 69 66 28 20 73 71 6c 69   = 0;.  if( sqli
1be06 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
1be07 6d 65 73 28 26 6e 63 2c 20 70 45 29 20 29 7b 0a  mes(&nc, pE) ){.
1be08 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1be09 43 6c 65 61 72 28 70 50 61 72 73 65 29 3b 0a 20  Clear(pParse);. 
1be0a 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
1be0b 0a 0a 20 20 2f 2a 20 54 72 79 20 74 6f 20 6d 61  ..  /* Try to ma
1be0c 74 63 68 20 74 68 65 20 4f 52 44 45 52 20 42 59  tch the ORDER BY
1be0d 20 65 78 70 72 65 73 73 69 6f 6e 20 61 67 61 69   expression agai
1be0e 6e 73 74 20 61 6e 20 65 78 70 72 65 73 73 69 6f  nst an expressio
1be0f 6e 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 72 65  n.  ** in the re
1be10 73 75 6c 74 20 73 65 74 2e 20 20 52 65 74 75 72  sult set.  Retur
1be11 6e 20 61 6e 20 31 2d 62 61 73 65 64 20 69 6e 64  n an 1-based ind
1be12 65 78 20 6f 66 20 74 68 65 20 6d 61 74 63 68 69  ex of the matchi
1be13 6e 67 0a 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73  ng.  ** result-s
1be14 65 74 20 65 6e 74 72 79 2e 0a 20 20 2a 2f 0a 20  et entry..  */. 
1be15 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69   for(i=0; i<pELi
1be16 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
1be17 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1be18 45 78 70 72 43 6f 6d 70 61 72 65 28 70 45 4c 69  ExprCompare(pELi
1be19 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20  st->a[i].pExpr, 
1be1a 70 45 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  pE) ){.      ret
1be1b 75 72 6e 20 69 2b 31 3b 0a 20 20 20 20 7d 0a 20  urn i+1;.    }. 
1be1c 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 6e 6f 20 6d   }..  /* If no m
1be1d 61 74 63 68 2c 20 72 65 74 75 72 6e 20 30 2e 20  atch, return 0. 
1be1e 2a 2f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d  */.  return 0;.}
1be1f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1be20 20 61 6e 20 4f 52 44 45 52 20 42 59 20 6f 72 20   an ORDER BY or 
1be21 47 52 4f 55 50 20 42 59 20 74 65 72 6d 20 6f 75  GROUP BY term ou
1be22 74 2d 6f 66 2d 72 61 6e 67 65 20 65 72 72 6f 72  t-of-range error
1be23 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1be24 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61 6e   resolveOutOfRan
1be25 67 65 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65  geError(.  Parse
1be26 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
1be27 20 20 2f 2a 20 54 68 65 20 65 72 72 6f 72 20 63    /* The error c
1be28 6f 6e 74 65 78 74 20 69 6e 74 6f 20 77 68 69 63  ontext into whic
1be29 68 20 74 6f 20 77 72 69 74 65 20 74 68 65 20 65  h to write the e
1be2a 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  rror */.  const 
1be2b 63 68 61 72 20 2a 7a 54 79 70 65 2c 20 20 20 20  char *zType,    
1be2c 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72 20 22   /* "ORDER" or "
1be2d 47 52 4f 55 50 22 20 2a 2f 0a 20 20 69 6e 74 20  GROUP" */.  int 
1be2e 69 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i,              
1be2f 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78 20     /* The index 
1be30 28 31 2d 62 61 73 65 64 29 20 6f 66 20 74 68 65  (1-based) of the
1be31 20 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e   term out of ran
1be32 67 65 20 2a 2f 0a 20 20 69 6e 74 20 6d 78 20 20  ge */.  int mx  
1be33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1be34 2a 20 4c 61 72 67 65 73 74 20 70 65 72 6d 69 73  * Largest permis
1be35 73 69 62 6c 65 20 76 61 6c 75 65 20 6f 66 20 69  sible value of i
1be36 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
1be37 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1be38 20 0a 20 20 20 20 22 25 72 20 25 73 20 42 59 20   .    "%r %s BY 
1be39 74 65 72 6d 20 6f 75 74 20 6f 66 20 72 61 6e 67  term out of rang
1be3a 65 20 2d 20 73 68 6f 75 6c 64 20 62 65 20 22 0a  e - should be ".
1be3b 20 20 20 20 22 62 65 74 77 65 65 6e 20 31 20 61      "between 1 a
1be3c 6e 64 20 25 64 22 2c 20 69 2c 20 7a 54 79 70 65  nd %d", i, zType
1be3d 2c 20 6d 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  , mx);.}../*.** 
1be3e 41 6e 61 6c 79 7a 65 20 74 68 65 20 4f 52 44 45  Analyze the ORDE
1be3f 52 20 42 59 20 63 6c 61 75 73 65 20 69 6e 20 61  R BY clause in a
1be40 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
1be41 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 20 4d 6f   statement.   Mo
1be42 64 69 66 79 0a 2a 2a 20 65 61 63 68 20 74 65 72  dify.** each ter
1be43 6d 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42  m of the ORDER B
1be44 59 20 63 6c 61 75 73 65 20 69 73 20 61 20 63 6f  Y clause is a co
1be45 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72 20 62  nstant integer b
1be46 65 74 77 65 65 6e 20 31 0a 2a 2a 20 61 6e 64 20  etween 1.** and 
1be47 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  N where N is the
1be48 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
1be49 6e 73 20 69 6e 20 74 68 65 20 63 6f 6d 70 6f 75  ns in the compou
1be4a 6e 64 20 53 45 4c 45 43 54 2e 0a 2a 2a 0a 2a 2a  nd SELECT..**.**
1be4b 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20   ORDER BY terms 
1be4c 74 68 61 74 20 61 72 65 20 61 6c 72 65 61 64 79  that are already
1be4d 20 61 6e 20 69 6e 74 65 67 65 72 20 62 65 74 77   an integer betw
1be4e 65 65 6e 20 31 20 61 6e 64 20 4e 20 61 72 65 0a  een 1 and N are.
1be4f 2a 2a 20 75 6e 6d 6f 64 69 66 69 65 64 2e 20 20  ** unmodified.  
1be50 4f 52 44 45 52 20 42 59 20 74 65 72 6d 73 20 74  ORDER BY terms t
1be51 68 61 74 20 61 72 65 20 69 6e 74 65 67 65 72 73  hat are integers
1be52 20 6f 75 74 73 69 64 65 20 74 68 65 20 72 61 6e   outside the ran
1be53 67 65 20 6f 66 0a 2a 2a 20 31 20 74 68 72 6f 75  ge of.** 1 throu
1be54 67 68 20 4e 20 67 65 6e 65 72 61 74 65 20 61 6e  gh N generate an
1be55 20 65 72 72 6f 72 2e 20 20 4f 52 44 45 52 20 42   error.  ORDER B
1be56 59 20 74 65 72 6d 73 20 74 68 61 74 20 61 72 65  Y terms that are
1be57 20 65 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20   expressions.** 
1be58 61 72 65 20 6d 61 74 63 68 65 64 20 61 67 61 69  are matched agai
1be59 6e 73 74 20 72 65 73 75 6c 74 20 73 65 74 20 65  nst result set e
1be5a 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 63 6f  xpressions of co
1be5b 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 0a 2a 2a  mpound SELECT.**
1be5c 20 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20   beginning with 
1be5d 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 53 45  the left-most SE
1be5e 4c 45 43 54 20 61 6e 64 20 77 6f 72 6b 69 6e 67  LECT and working
1be5f 20 74 6f 77 61 72 64 20 74 68 65 20 72 69 67 68   toward the righ
1be60 74 2e 0a 2a 2a 20 41 74 20 74 68 65 20 66 69 72  t..** At the fir
1be61 73 74 20 6d 61 74 63 68 2c 20 74 68 65 20 4f 52  st match, the OR
1be62 44 45 52 20 42 59 20 65 78 70 72 65 73 73 69 6f  DER BY expressio
1be63 6e 20 69 73 20 74 72 61 6e 73 66 6f 72 6d 65 64  n is transformed
1be64 20 69 6e 74 6f 0a 2a 2a 20 74 68 65 20 69 6e 74   into.** the int
1be65 65 67 65 72 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62  eger column numb
1be66 65 72 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e  er..**.** Return
1be67 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
1be68 72 72 6f 72 73 20 73 65 65 6e 2e 0a 2a 2f 0a 73  rrors seen..*/.s
1be69 74 61 74 69 63 20 69 6e 74 20 72 65 73 6f 6c 76  tatic int resolv
1be6a 65 43 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79  eCompoundOrderBy
1be6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1be6c 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
1be6d 73 69 6e 67 20 63 6f 6e 74 65 78 74 2e 20 20 4c  sing context.  L
1be6e 65 61 76 65 20 65 72 72 6f 72 20 6d 65 73 73 61  eave error messa
1be6f 67 65 73 20 68 65 72 65 20 2a 2f 0a 20 20 53 65  ges here */.  Se
1be70 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20  lect *pSelect   
1be71 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
1be72 54 20 73 74 61 74 65 6d 65 6e 74 20 63 6f 6e 74  T statement cont
1be73 61 69 6e 69 6e 67 20 74 68 65 20 4f 52 44 45 52  aining the ORDER
1be74 20 42 59 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20   BY */.){.  int 
1be75 69 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  i;.  ExprList *p
1be76 4f 72 64 65 72 42 79 3b 0a 20 20 45 78 70 72 4c  OrderBy;.  ExprL
1be77 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73  ist *pEList;.  s
1be78 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 69 6e  qlite3 *db;.  in
1be79 74 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 31 3b 0a  t moreToDo = 1;.
1be7a 0a 20 20 70 4f 72 64 65 72 42 79 20 3d 20 70 53  .  pOrderBy = pS
1be7b 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72 42 79 3b  elect->pOrderBy;
1be7c 0a 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d  .  if( pOrderBy=
1be7d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1be7e 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
1be7f 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
1be80 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f  _COLUMN.  if( pO
1be81 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62  rderBy->nExpr>db
1be82 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1be83 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b  LIMIT_COLUMN] ){
1be84 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1be85 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
1be86 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20  o many terms in 
1be87 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 22  ORDER BY clause"
1be88 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  );.    return 1;
1be89 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 66 6f  .  }.#endif.  fo
1be8a 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42  r(i=0; i<pOrderB
1be8b 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  y->nExpr; i++){.
1be8c 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b      pOrderBy->a[
1be8d 69 5d 2e 64 6f 6e 65 20 3d 20 30 3b 0a 20 20 7d  i].done = 0;.  }
1be8e 0a 20 20 70 53 65 6c 65 63 74 2d 3e 70 4e 65 78  .  pSelect->pNex
1be8f 74 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20  t = 0;.  while( 
1be90 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 20  pSelect->pPrior 
1be91 29 7b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d 3e  ){.    pSelect->
1be92 70 50 72 69 6f 72 2d 3e 70 4e 65 78 74 20 3d 20  pPrior->pNext = 
1be93 70 53 65 6c 65 63 74 3b 0a 20 20 20 20 70 53 65  pSelect;.    pSe
1be94 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e  lect = pSelect->
1be95 70 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 77 68  pPrior;.  }.  wh
1be96 69 6c 65 28 20 70 53 65 6c 65 63 74 20 26 26 20  ile( pSelect && 
1be97 6d 6f 72 65 54 6f 44 6f 20 29 7b 0a 20 20 20 20  moreToDo ){.    
1be98 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1be99 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
1be9a 20 6d 6f 72 65 54 6f 44 6f 20 3d 20 30 3b 0a 20   moreToDo = 0;. 
1be9b 20 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c     pEList = pSel
1be9c 65 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20  ect->pEList;.   
1be9d 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
1be9e 3d 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d  =0 );.    for(i=
1be9f 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65 72 42  0, pItem=pOrderB
1bea0 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72 42 79  y->a; i<pOrderBy
1bea1 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49  ->nExpr; i++, pI
1bea2 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e  tem++){.      in
1bea3 74 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20 20 20  t iCol = -1;.   
1bea4 20 20 20 45 78 70 72 20 2a 70 45 2c 20 2a 70 44     Expr *pE, *pD
1bea5 75 70 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49  up;.      if( pI
1bea6 74 65 6d 2d 3e 64 6f 6e 65 20 29 20 63 6f 6e 74  tem->done ) cont
1bea7 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20 3d  inue;.      pE =
1bea8 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 20   pItem->pExpr;. 
1bea9 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33       if( sqlite3
1beaa 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70 45  ExprIsInteger(pE
1beab 2c 20 26 69 43 6f 6c 29 20 29 7b 0a 20 20 20 20  , &iCol) ){.    
1beac 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 7c      if( iCol<0 |
1bead 7c 20 69 43 6f 6c 3e 70 45 4c 69 73 74 2d 3e 6e  | iCol>pEList->n
1beae 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20 20  Expr ){.        
1beaf 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52 61    resolveOutOfRa
1beb0 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ngeError(pParse,
1beb1 20 22 4f 52 44 45 52 22 2c 20 69 2b 31 2c 20 70   "ORDER", i+1, p
1beb2 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20  EList->nExpr);. 
1beb3 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
1beb4 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  1;.        }.   
1beb5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1beb6 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f 6c 76 65    iCol = resolve
1beb7 41 73 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70  AsName(pParse, p
1beb8 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20 20 20  EList, pE);.    
1beb9 20 20 20 20 69 66 28 20 69 43 6f 6c 3d 3d 30 20      if( iCol==0 
1beba 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 44 75  ){.          pDu
1bebb 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
1bebc 75 70 28 64 62 2c 20 70 45 29 3b 0a 20 20 20 20  up(db, pE);.    
1bebd 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1bebe 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1bebf 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
1bec0 74 28 70 44 75 70 29 3b 0a 20 20 20 20 20 20 20  t(pDup);.       
1bec1 20 20 20 20 20 69 43 6f 6c 20 3d 20 72 65 73 6f       iCol = reso
1bec2 6c 76 65 4f 72 64 65 72 42 79 54 65 72 6d 54 6f  lveOrderByTermTo
1bec3 45 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c  ExprList(pParse,
1bec4 20 70 53 65 6c 65 63 74 2c 20 70 44 75 70 29 3b   pSelect, pDup);
1bec5 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
1bec6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
1bec7 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 44 75  prDelete(db, pDu
1bec8 70 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  p);.        }.  
1bec9 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
1beca 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65   ){.          re
1becb 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 20 20  turn 1;.        
1becc 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  }.      }.      
1becd 69 66 28 20 69 43 6f 6c 3e 30 20 29 7b 0a 20 20  if( iCol>0 ){.  
1bece 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
1becf 43 6f 6c 6c 20 3d 20 70 45 2d 3e 70 43 6f 6c 6c  Coll = pE->pColl
1bed0 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 66 6c  ;.        int fl
1bed1 61 67 73 20 3d 20 70 45 2d 3e 66 6c 61 67 73 20  ags = pE->flags 
1bed2 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  & EP_ExpCollate;
1bed3 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1bed4 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1bed5 45 29 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65  E);.        pIte
1bed6 6d 2d 3e 70 45 78 70 72 20 3d 20 70 45 20 3d 20  m->pExpr = pE = 
1bed7 73 71 6c 69 74 65 33 45 78 70 72 28 64 62 2c 20  sqlite3Expr(db, 
1bed8 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30  TK_INTEGER, 0, 0
1bed9 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
1beda 28 20 70 45 3d 3d 30 20 29 20 72 65 74 75 72 6e  ( pE==0 ) return
1bedb 20 31 3b 0a 20 20 20 20 20 20 20 20 70 45 2d 3e   1;.        pE->
1bedc 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20  pColl = pColl;. 
1bedd 20 20 20 20 20 20 20 70 45 2d 3e 66 6c 61 67 73         pE->flags
1bede 20 7c 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 20   |= EP_IntValue 
1bedf 7c 20 66 6c 61 67 73 3b 0a 20 20 20 20 20 20 20  | flags;.       
1bee0 20 70 45 2d 3e 69 54 61 62 6c 65 20 3d 20 69 43   pE->iTable = iC
1bee1 6f 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 74 65  ol;.        pIte
1bee2 6d 2d 3e 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a  m->iCol = iCol;.
1bee3 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 64          pItem->d
1bee4 6f 6e 65 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d  one = 1;.      }
1bee5 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6d 6f  else{.        mo
1bee6 72 65 54 6f 44 6f 20 3d 20 31 3b 0a 20 20 20 20  reToDo = 1;.    
1bee7 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 53    }.    }.    pS
1bee8 65 6c 65 63 74 20 3d 20 70 53 65 6c 65 63 74 2d  elect = pSelect-
1bee9 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20 20 66 6f  >pNext;.  }.  fo
1beea 72 28 69 3d 30 3b 20 69 3c 70 4f 72 64 65 72 42  r(i=0; i<pOrderB
1beeb 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  y->nExpr; i++){.
1beec 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
1beed 2d 3e 61 5b 69 5d 2e 64 6f 6e 65 3d 3d 30 20 29  ->a[i].done==0 )
1beee 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1beef 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1bef0 22 25 72 20 4f 52 44 45 52 20 42 59 20 74 65 72  "%r ORDER BY ter
1bef1 6d 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68  m does not match
1bef2 20 61 6e 79 20 22 0a 20 20 20 20 20 20 20 20 20   any ".         
1bef3 20 20 20 22 63 6f 6c 75 6d 6e 20 69 6e 20 74 68     "column in th
1bef4 65 20 72 65 73 75 6c 74 20 73 65 74 22 2c 20 69  e result set", i
1bef5 2b 31 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  +1);.      retur
1bef6 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
1bef7 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
1bef8 0a 2a 2a 20 43 68 65 63 6b 20 65 76 65 72 79 20  .** Check every 
1bef9 74 65 72 6d 20 69 6e 20 74 68 65 20 4f 52 44 45  term in the ORDE
1befa 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
1befb 20 63 6c 61 75 73 65 20 70 4f 72 64 65 72 42 79   clause pOrderBy
1befc 20 6f 66 0a 2a 2a 20 74 68 65 20 53 45 4c 45 43   of.** the SELEC
1befd 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53 65 6c  T statement pSel
1befe 65 63 74 2e 20 20 49 66 20 61 6e 79 20 74 65 72  ect.  If any ter
1beff 6d 20 69 73 20 72 65 66 65 72 65 6e 63 65 20 74  m is reference t
1bf00 6f 20 61 0a 2a 2a 20 72 65 73 75 6c 74 20 73 65  o a.** result se
1bf01 74 20 65 78 70 72 65 73 73 69 6f 6e 20 28 61 73  t expression (as
1bf02 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20 74   determined by t
1bf03 68 65 20 45 78 70 72 4c 69 73 74 2e 61 2e 69 43  he ExprList.a.iC
1bf04 6f 6c 20 66 69 65 6c 64 29 0a 2a 2a 20 74 68 65  ol field).** the
1bf05 6e 20 63 6f 6e 76 65 72 74 20 74 68 61 74 20 74  n convert that t
1bf06 65 72 6d 20 69 6e 74 6f 20 61 20 63 6f 70 79 20  erm into a copy 
1bf07 6f 66 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e  of the correspon
1bf08 64 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74 0a  ding result set.
1bf09 2a 2a 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a 2a  ** column..**.**
1bf0a 20 49 66 20 61 6e 79 20 65 72 72 6f 72 73 20 61   If any errors a
1bf0b 72 65 20 64 65 74 65 63 74 65 64 2c 20 61 64 64  re detected, add
1bf0c 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   an error messag
1bf0d 65 20 74 6f 20 70 50 61 72 73 65 20 61 6e 64 0a  e to pParse and.
1bf0e 2a 2a 20 72 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65  ** return non-ze
1bf0f 72 6f 2e 20 20 52 65 74 75 72 6e 20 7a 65 72 6f  ro.  Return zero
1bf10 20 69 66 20 6e 6f 20 65 72 72 6f 72 73 20 61 72   if no errors ar
1bf11 65 20 73 65 65 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  e seen..*/.SQLIT
1bf12 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1bf13 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
1bf14 72 47 72 6f 75 70 42 79 28 0a 20 20 50 61 72 73  rGroupBy(.  Pars
1bf15 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1bf16 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1bf17 74 65 78 74 2e 20 20 4c 65 61 76 65 20 65 72 72  text.  Leave err
1bf18 6f 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65  or messages here
1bf19 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 53   */.  Select *pS
1bf1a 65 6c 65 63 74 2c 20 20 20 20 20 20 2f 2a 20 54  elect,      /* T
1bf1b 68 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  he SELECT statem
1bf1c 65 6e 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ent containing t
1bf1d 68 65 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45  he clause */.  E
1bf1e 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42  xprList *pOrderB
1bf1f 79 2c 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45  y,   /* The ORDE
1bf20 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
1bf21 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20 70 72   clause to be pr
1bf22 6f 63 65 73 73 65 64 20 2a 2f 0a 20 20 63 6f 6e  ocessed */.  con
1bf23 73 74 20 63 68 61 72 20 2a 7a 54 79 70 65 20 20  st char *zType  
1bf24 20 20 20 2f 2a 20 22 4f 52 44 45 52 22 20 6f 72     /* "ORDER" or
1bf25 20 22 47 52 4f 55 50 22 20 2a 2f 0a 29 7b 0a 20   "GROUP" */.){. 
1bf26 20 69 6e 74 20 69 3b 0a 20 20 73 71 6c 69 74 65   int i;.  sqlite
1bf27 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1bf28 64 62 3b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  db;.  ExprList *
1bf29 70 45 4c 69 73 74 3b 0a 20 20 73 74 72 75 63 74  pEList;.  struct
1bf2a 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1bf2b 70 49 74 65 6d 3b 0a 0a 20 20 69 66 28 20 70 4f  pItem;..  if( pO
1bf2c 72 64 65 72 42 79 3d 3d 30 20 7c 7c 20 70 50 61  rderBy==0 || pPa
1bf2d 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rse->db->mallocF
1bf2e 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30  ailed ) return 0
1bf2f 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
1bf30 5f 43 4f 4c 55 4d 4e 0a 20 20 69 66 28 20 70 4f  _COLUMN.  if( pO
1bf31 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3e 64 62  rderBy->nExpr>db
1bf32 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1bf33 4c 49 4d 49 54 5f 43 4f 4c 55 4d 4e 5d 20 29 7b  LIMIT_COLUMN] ){
1bf34 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1bf35 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
1bf36 6f 20 6d 61 6e 79 20 74 65 72 6d 73 20 69 6e 20  o many terms in 
1bf37 25 73 20 42 59 20 63 6c 61 75 73 65 22 2c 20 7a  %s BY clause", z
1bf38 54 79 70 65 29 3b 0a 20 20 20 20 72 65 74 75 72  Type);.    retur
1bf39 6e 20 31 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  n 1;.  }.#endif.
1bf3a 20 20 70 45 4c 69 73 74 20 3d 20 70 53 65 6c 65    pEList = pSele
1bf3b 63 74 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 61 73  ct->pEList;.  as
1bf3c 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
1bf3d 29 3b 20 20 2f 2a 20 73 71 6c 69 74 65 33 53 65  );  /* sqlite3Se
1bf3e 6c 65 63 74 4e 65 77 28 29 20 67 75 61 72 61 6e  lectNew() guaran
1bf3f 74 65 65 73 20 74 68 69 73 20 2a 2f 0a 20 20 66  tees this */.  f
1bf40 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f  or(i=0, pItem=pO
1bf41 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72  rderBy->a; i<pOr
1bf42 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b  derBy->nExpr; i+
1bf43 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  +, pItem++){.   
1bf44 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c   if( pItem->iCol
1bf45 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49   ){.      if( pI
1bf46 74 65 6d 2d 3e 69 43 6f 6c 3e 70 45 4c 69 73 74  tem->iCol>pEList
1bf47 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ->nExpr ){.     
1bf48 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52     resolveOutOfR
1bf49 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65  angeError(pParse
1bf4a 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 70 45  , zType, i+1, pE
1bf4b 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  List->nExpr);.  
1bf4c 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1bf4d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 65        }.      re
1bf4e 73 6f 6c 76 65 41 6c 69 61 73 28 70 50 61 72 73  solveAlias(pPars
1bf4f 65 2c 20 70 45 4c 69 73 74 2c 20 70 49 74 65 6d  e, pEList, pItem
1bf50 2d 3e 69 43 6f 6c 2d 31 2c 20 70 49 74 65 6d 2d  ->iCol-1, pItem-
1bf51 3e 70 45 78 70 72 2c 20 7a 54 79 70 65 29 3b 0a  >pExpr, zType);.
1bf52 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75      }.  }.  retu
1bf53 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70  rn 0;.}../*.** p
1bf54 4f 72 64 65 72 42 79 20 69 73 20 61 6e 20 4f 52  OrderBy is an OR
1bf55 44 45 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20  DER BY or GROUP 
1bf56 42 59 20 63 6c 61 75 73 65 20 69 6e 20 53 45 4c  BY clause in SEL
1bf57 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 70 53  ECT statement pS
1bf58 65 6c 65 63 74 2e 0a 2a 2a 20 54 68 65 20 4e 61  elect..** The Na
1bf59 6d 65 20 63 6f 6e 74 65 78 74 20 6f 66 20 74 68  me context of th
1bf5a 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
1bf5b 6e 74 20 69 73 20 70 4e 43 2e 20 20 7a 54 79 70  nt is pNC.  zTyp
1bf5c 65 20 69 73 20 65 69 74 68 65 72 0a 2a 2a 20 22  e is either.** "
1bf5d 4f 52 44 45 52 22 20 6f 72 20 22 47 52 4f 55 50  ORDER" or "GROUP
1bf5e 22 20 64 65 70 65 6e 64 69 6e 67 20 6f 6e 20 77  " depending on w
1bf5f 68 69 63 68 20 74 79 70 65 20 6f 66 20 63 6c 61  hich type of cla
1bf60 75 73 65 20 70 4f 72 64 65 72 42 79 20 69 73 2e  use pOrderBy is.
1bf61 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  .**.** This rout
1bf62 69 6e 65 20 72 65 73 6f 6c 76 65 73 20 65 61 63  ine resolves eac
1bf63 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6c  h term of the cl
1bf64 61 75 73 65 20 69 6e 74 6f 20 61 6e 20 65 78 70  ause into an exp
1bf65 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20 49 66 20 74  ression..** If t
1bf66 68 65 20 6f 72 64 65 72 2d 62 79 20 74 65 72 6d  he order-by term
1bf67 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 49   is an integer I
1bf68 20 62 65 74 77 65 65 6e 20 31 20 61 6e 64 20 4e   between 1 and N
1bf69 20 28 77 68 65 72 65 20 4e 20 69 73 20 74 68 65   (where N is the
1bf6a 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  .** number of co
1bf6b 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20 72 65 73  lumns in the res
1bf6c 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65 20 53  ult set of the S
1bf6d 45 4c 45 43 54 29 20 74 68 65 6e 20 74 68 65 20  ELECT) then the 
1bf6e 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 6e  expression.** in
1bf6f 20 74 68 65 20 72 65 73 6f 6c 75 74 69 6f 6e 20   the resolution 
1bf70 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  is a copy of the
1bf71 20 49 2d 74 68 20 72 65 73 75 6c 74 2d 73 65 74   I-th result-set
1bf72 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 49 66   expression.  If
1bf73 0a 2a 2a 20 74 68 65 20 6f 72 64 65 72 2d 62 79  .** the order-by
1bf74 20 74 65 72 6d 20 69 73 20 61 6e 20 69 64 65 6e   term is an iden
1bf75 74 69 66 79 20 74 68 61 74 20 63 6f 72 72 65 73  tify that corres
1bf76 70 6f 6e 64 73 20 74 6f 20 74 68 65 20 41 53 2d  ponds to the AS-
1bf77 6e 61 6d 65 20 6f 66 0a 2a 2a 20 61 20 72 65 73  name of.** a res
1bf78 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73 69  ult-set expressi
1bf79 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 74 65 72  on, then the ter
1bf7a 6d 20 72 65 73 6f 6c 76 65 73 20 74 6f 20 61 20  m resolves to a 
1bf7b 63 6f 70 79 20 6f 66 20 74 68 65 0a 2a 2a 20 72  copy of the.** r
1bf7c 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73  esult-set expres
1bf7d 73 69 6f 6e 2e 20 20 4f 74 68 65 72 77 69 73 65  sion.  Otherwise
1bf7e 2c 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  , the expression
1bf7f 20 69 73 20 72 65 73 6f 6c 76 65 64 20 69 6e 0a   is resolved in.
1bf80 2a 2a 20 74 68 65 20 75 73 75 61 6c 20 77 61 79  ** the usual way
1bf81 20 2d 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33   - using sqlite3
1bf82 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73  ResolveExprNames
1bf83 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ()..**.** This r
1bf84 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
1bf85 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72 72  he number of err
1bf86 6f 72 73 2e 20 20 49 66 20 65 72 72 6f 72 73 20  ors.  If errors 
1bf87 6f 63 63 75 72 2c 20 74 68 65 6e 0a 2a 2a 20 61  occur, then.** a
1bf88 6e 20 61 70 70 72 6f 70 72 69 61 74 65 20 65 72  n appropriate er
1bf89 72 6f 72 20 6d 65 73 73 61 67 65 20 6d 69 67 68  ror message migh
1bf8a 74 20 62 65 20 6c 65 66 74 20 69 6e 20 70 50 61  t be left in pPa
1bf8b 72 73 65 2e 20 20 28 4f 4f 4d 20 65 72 72 6f 72  rse.  (OOM error
1bf8c 73 0a 2a 2a 20 65 78 63 65 70 74 65 64 2e 29 0a  s.** excepted.).
1bf8d 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
1bf8e 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
1bf8f 79 28 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  y(.  NameContext
1bf90 20 2a 70 4e 43 2c 20 20 20 20 20 2f 2a 20 54 68   *pNC,     /* Th
1bf91 65 20 6e 61 6d 65 20 63 6f 6e 74 65 78 74 20 6f  e name context o
1bf92 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  f the SELECT sta
1bf93 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c 65  tement */.  Sele
1bf94 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20  ct *pSelect,    
1bf95 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20    /* The SELECT 
1bf96 73 74 61 74 65 6d 65 6e 74 20 68 6f 6c 64 69 6e  statement holdin
1bf97 67 20 70 4f 72 64 65 72 42 79 20 2a 2f 0a 20 20  g pOrderBy */.  
1bf98 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
1bf99 42 79 2c 20 20 20 2f 2a 20 41 6e 20 4f 52 44 45  By,   /* An ORDE
1bf9a 52 20 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59  R BY or GROUP BY
1bf9b 20 63 6c 61 75 73 65 20 74 6f 20 72 65 73 6f 6c   clause to resol
1bf9c 76 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ve */.  const ch
1bf9d 61 72 20 2a 7a 54 79 70 65 20 20 20 20 20 2f 2a  ar *zType     /*
1bf9e 20 45 69 74 68 65 72 20 22 4f 52 44 45 52 22 20   Either "ORDER" 
1bf9f 6f 72 20 22 47 52 4f 55 50 22 2c 20 61 73 20 61  or "GROUP", as a
1bfa0 70 70 72 6f 70 72 69 61 74 65 20 2a 2f 0a 29 7b  ppropriate */.){
1bfa1 0a 20 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20  .  int i;       
1bfa2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bfa3 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1bfa4 72 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 3b  r */.  int iCol;
1bfa5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bfa6 20 20 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20        /* Column 
1bfa7 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 74 72 75  number */.  stru
1bfa8 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d  ct ExprList_item
1bfa9 20 2a 70 49 74 65 6d 3b 20 20 20 2f 2a 20 41 20   *pItem;   /* A 
1bfaa 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52 44 45  term of the ORDE
1bfab 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20  R BY clause */. 
1bfac 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20   Parse *pParse; 
1bfad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1bfae 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1bfaf 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73  xt */.  int nRes
1bfb0 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ult;            
1bfb1 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
1bfb2 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65   of terms in the
1bfb3 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a   result set */..
1bfb4 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d 3d    if( pOrderBy==
1bfb5 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
1bfb6 6e 52 65 73 75 6c 74 20 3d 20 70 53 65 6c 65 63  nResult = pSelec
1bfb7 74 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72  t->pEList->nExpr
1bfb8 3b 0a 20 20 70 50 61 72 73 65 20 3d 20 70 4e 43  ;.  pParse = pNC
1bfb9 2d 3e 70 50 61 72 73 65 3b 0a 20 20 66 6f 72 28  ->pParse;.  for(
1bfba 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4f 72 64 65  i=0, pItem=pOrde
1bfbb 72 42 79 2d 3e 61 3b 20 69 3c 70 4f 72 64 65 72  rBy->a; i<pOrder
1bfbc 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  By->nExpr; i++, 
1bfbd 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 45 78  pItem++){.    Ex
1bfbe 70 72 20 2a 70 45 20 3d 20 70 49 74 65 6d 2d 3e  pr *pE = pItem->
1bfbf 70 45 78 70 72 3b 0a 20 20 20 20 69 43 6f 6c 20  pExpr;.    iCol 
1bfc0 3d 20 72 65 73 6f 6c 76 65 41 73 4e 61 6d 65 28  = resolveAsName(
1bfc1 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d  pParse, pSelect-
1bfc2 3e 70 45 4c 69 73 74 2c 20 70 45 29 3b 0a 20 20  >pEList, pE);.  
1bfc3 20 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a    if( iCol<0 ){.
1bfc4 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20        return 1; 
1bfc5 20 2f 2a 20 4f 4f 4d 20 65 72 72 6f 72 20 2a 2f   /* OOM error */
1bfc6 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69  .    }.    if( i
1bfc7 43 6f 6c 3e 30 20 29 7b 0a 20 20 20 20 20 20 2f  Col>0 ){.      /
1bfc8 2a 20 49 66 20 61 6e 20 41 53 2d 6e 61 6d 65 20  * If an AS-name 
1bfc9 6d 61 74 63 68 20 69 73 20 66 6f 75 6e 64 2c 20  match is found, 
1bfca 6d 61 72 6b 20 74 68 69 73 20 4f 52 44 45 52 20  mark this ORDER 
1bfcb 42 59 20 63 6f 6c 75 6d 6e 20 61 73 20 62 65 69  BY column as bei
1bfcc 6e 67 0a 20 20 20 20 20 20 2a 2a 20 61 20 63 6f  ng.      ** a co
1bfcd 70 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74  py of the iCol-t
1bfce 68 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f 6c  h result-set col
1bfcf 75 6d 6e 2e 20 20 54 68 65 20 73 75 62 73 65 71  umn.  The subseq
1bfd0 75 65 6e 74 20 63 61 6c 6c 20 74 6f 0a 20 20 20  uent call to.   
1bfd1 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 52 65 73     ** sqlite3Res
1bfd2 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
1bfd3 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72 74 20  () will convert 
1bfd4 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 74  the expression t
1bfd5 6f 20 61 0a 20 20 20 20 20 20 2a 2a 20 63 6f 70  o a.      ** cop
1bfd6 79 20 6f 66 20 74 68 65 20 69 43 6f 6c 2d 74 68  y of the iCol-th
1bfd7 20 72 65 73 75 6c 74 2d 73 65 74 20 65 78 70 72   result-set expr
1bfd8 65 73 73 69 6f 6e 2e 20 2a 2f 0a 20 20 20 20 20  ession. */.     
1bfd9 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 69   pItem->iCol = i
1bfda 43 6f 6c 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69  Col;.      conti
1bfdb 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  nue;.    }.    i
1bfdc 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49 73  f( sqlite3ExprIs
1bfdd 49 6e 74 65 67 65 72 28 70 45 2c 20 26 69 43 6f  Integer(pE, &iCo
1bfde 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  l) ){.      /* T
1bfdf 68 65 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d  he ORDER BY term
1bfe0 20 69 73 20 61 6e 20 69 6e 74 65 67 65 72 20 63   is an integer c
1bfe1 6f 6e 73 74 61 6e 74 2e 20 20 41 67 61 69 6e 2c  onstant.  Again,
1bfe2 20 73 65 74 20 74 68 65 20 63 6f 6c 75 6d 6e 0a   set the column.
1bfe3 20 20 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20        ** number 
1bfe4 73 6f 20 74 68 61 74 20 73 71 6c 69 74 65 33 52  so that sqlite3R
1bfe5 65 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70  esolveOrderGroup
1bfe6 42 79 28 29 20 77 69 6c 6c 20 63 6f 6e 76 65 72  By() will conver
1bfe7 74 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6f  t the.      ** o
1bfe8 72 64 65 72 2d 62 79 20 74 65 72 6d 20 74 6f 20  rder-by term to 
1bfe9 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 72 65  a copy of the re
1bfea 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1bfeb 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  ion */.      if(
1bfec 20 69 43 6f 6c 3c 31 20 29 7b 0a 20 20 20 20 20   iCol<1 ){.     
1bfed 20 20 20 72 65 73 6f 6c 76 65 4f 75 74 4f 66 52     resolveOutOfR
1bfee 61 6e 67 65 45 72 72 6f 72 28 70 50 61 72 73 65  angeError(pParse
1bfef 2c 20 7a 54 79 70 65 2c 20 69 2b 31 2c 20 6e 52  , zType, i+1, nR
1bff0 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 20 20  esult);.        
1bff1 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20  return 1;.      
1bff2 7d 0a 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69  }.      pItem->i
1bff3 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 20 20  Col = iCol;.    
1bff4 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
1bff5 7d 0a 0a 20 20 20 20 2f 2a 20 4f 74 68 65 72 77  }..    /* Otherw
1bff6 69 73 65 2c 20 74 72 65 61 74 20 74 68 65 20 4f  ise, treat the O
1bff7 52 44 45 52 20 42 59 20 74 65 72 6d 20 61 73 20  RDER BY term as 
1bff8 61 6e 20 6f 72 64 69 6e 61 72 79 20 65 78 70 72  an ordinary expr
1bff9 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 20 20 70 49  ession */.    pI
1bffa 74 65 6d 2d 3e 69 43 6f 6c 20 3d 20 30 3b 0a 20  tem->iCol = 0;. 
1bffb 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65     if( sqlite3Re
1bffc 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70  solveExprNames(p
1bffd 4e 43 2c 20 70 45 29 20 29 7b 0a 20 20 20 20 20  NC, pE) ){.     
1bffe 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1bfff 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71  .  }.  return sq
1c000 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72 64 65  lite3ResolveOrde
1c001 72 47 72 6f 75 70 42 79 28 70 50 61 72 73 65 2c  rGroupBy(pParse,
1c002 20 70 53 65 6c 65 63 74 2c 20 70 4f 72 64 65 72   pSelect, pOrder
1c003 42 79 2c 20 7a 54 79 70 65 29 3b 0a 7d 0a 0a 2f  By, zType);.}../
1c004 2a 0a 2a 2a 20 52 65 73 6f 6c 76 65 20 6e 61 6d  *.** Resolve nam
1c005 65 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  es in the SELECT
1c006 20 73 74 61 74 65 6d 65 6e 74 20 70 20 61 6e 64   statement p and
1c007 20 61 6c 6c 20 6f 66 20 69 74 73 20 64 65 73 63   all of its desc
1c008 65 6e 64 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74  endents..*/.stat
1c009 69 63 20 69 6e 74 20 72 65 73 6f 6c 76 65 53 65  ic int resolveSe
1c00a 6c 65 63 74 53 74 65 70 28 57 61 6c 6b 65 72 20  lectStep(Walker 
1c00b 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74  *pWalker, Select
1c00c 20 2a 70 29 7b 0a 20 20 4e 61 6d 65 43 6f 6e 74   *p){.  NameCont
1c00d 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 3b 20 20  ext *pOuterNC;  
1c00e 2f 2a 20 43 6f 6e 74 65 78 74 20 74 68 61 74 20  /* Context that 
1c00f 63 6f 6e 74 61 69 6e 73 20 74 68 69 73 20 53 45  contains this SE
1c010 4c 45 43 54 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  LECT */.  NameCo
1c011 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
1c012 20 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78    /* Name contex
1c013 74 20 6f 66 20 74 68 69 73 20 53 45 4c 45 43 54  t of this SELECT
1c014 20 2a 2f 0a 20 20 69 6e 74 20 69 73 43 6f 6d 70   */.  int isComp
1c015 6f 75 6e 64 3b 20 20 20 20 20 20 20 20 20 2f 2a  ound;         /*
1c016 20 54 72 75 65 20 69 66 20 70 20 69 73 20 61 20   True if p is a 
1c017 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 20  compound select 
1c018 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6d 70 6f 75  */.  int nCompou
1c019 6e 64 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  nd;          /* 
1c01a 4e 75 6d 62 65 72 20 6f 66 20 63 6f 6d 70 6f 75  Number of compou
1c01b 6e 64 20 74 65 72 6d 73 20 70 72 6f 63 65 73 73  nd terms process
1c01c 65 64 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20 50  ed so far */.  P
1c01d 61 72 73 65 20 2a 70 50 61 72 73 65 3b 20 20 20  arse *pParse;   
1c01e 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
1c01f 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
1c020 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1c021 20 20 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74         /* Result
1c022 20 73 65 74 20 65 78 70 72 65 73 73 69 6f 6e 20   set expression 
1c023 6c 69 73 74 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  list */.  int i;
1c024 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c025 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
1c026 72 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  r */.  ExprList 
1c027 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 20 2f  *pGroupBy;     /
1c028 2a 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63  * The GROUP BY c
1c029 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63  lause */.  Selec
1c02a 74 20 2a 70 4c 65 66 74 6d 6f 73 74 3b 20 20 20  t *pLeftmost;   
1c02b 20 20 20 2f 2a 20 4c 65 66 74 2d 6d 6f 73 74 20     /* Left-most 
1c02c 6f 66 20 53 45 4c 45 43 54 20 6f 66 20 61 20 63  of SELECT of a c
1c02d 6f 6d 70 6f 75 6e 64 20 2a 2f 0a 20 20 73 71 6c  ompound */.  sql
1c02e 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
1c02f 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
1c030 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1c031 20 0a 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d   ..  assert( p!=
1c032 30 20 29 3b 0a 20 20 69 66 28 20 70 2d 3e 73 65  0 );.  if( p->se
1c033 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73 6f  lFlags & SF_Reso
1c034 6c 76 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75  lved ){.    retu
1c035 72 6e 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20  rn WRC_Prune;.  
1c036 7d 0a 20 20 70 4f 75 74 65 72 4e 43 20 3d 20 70  }.  pOuterNC = p
1c037 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20  Walker->u.pNC;. 
1c038 20 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65   pParse = pWalke
1c039 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 64 62 20  r->pParse;.  db 
1c03a 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
1c03b 20 2f 2a 20 4e 6f 72 6d 61 6c 6c 79 20 73 71 6c   /* Normally sql
1c03c 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64  ite3SelectExpand
1c03d 28 29 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65  () will be calle
1c03e 64 20 66 69 72 73 74 20 61 6e 64 20 77 69 6c 6c  d first and will
1c03f 20 68 61 76 65 0a 20 20 2a 2a 20 61 6c 72 65 61   have.  ** alrea
1c040 64 79 20 65 78 70 61 6e 64 65 64 20 74 68 69 73  dy expanded this
1c041 20 53 45 4c 45 43 54 2e 20 20 48 6f 77 65 76 65   SELECT.  Howeve
1c042 72 2c 20 69 66 20 74 68 69 73 20 69 73 20 61 20  r, if this is a 
1c043 73 75 62 71 75 65 72 79 20 77 69 74 68 69 6e 0a  subquery within.
1c044 20 20 2a 2a 20 61 6e 20 65 78 70 72 65 73 73 69    ** an expressi
1c045 6f 6e 2c 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  on, sqlite3Resol
1c046 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 77 69  veExprNames() wi
1c047 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74  ll be called wit
1c048 68 6f 75 74 20 61 0a 20 20 2a 2a 20 70 72 69 6f  hout a.  ** prio
1c049 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65  r call to sqlite
1c04a 33 53 65 6c 65 63 74 45 78 70 61 6e 64 28 29 2e  3SelectExpand().
1c04b 20 20 57 68 65 6e 20 74 68 61 74 20 68 61 70 70    When that happ
1c04c 65 6e 73 2c 20 6c 65 74 0a 20 20 2a 2a 20 73 71  ens, let.  ** sq
1c04d 6c 69 74 65 33 53 65 6c 65 63 74 50 72 65 70 28  lite3SelectPrep(
1c04e 29 20 64 6f 20 61 6c 6c 20 6f 66 20 74 68 65 20  ) do all of the 
1c04f 70 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 74  processing for t
1c050 68 69 73 20 53 45 4c 45 43 54 2e 0a 20 20 2a 2a  his SELECT..  **
1c051 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 50 72   sqlite3SelectPr
1c052 65 70 28 29 20 77 69 6c 6c 20 69 6e 76 6f 6b 65  ep() will invoke
1c053 20 62 6f 74 68 20 73 71 6c 69 74 65 33 53 65 6c   both sqlite3Sel
1c054 65 63 74 45 78 70 61 6e 64 28 29 20 61 6e 64 0a  ectExpand() and.
1c055 20 20 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e    ** this routin
1c056 65 20 69 6e 20 74 68 65 20 63 6f 72 72 65 63 74  e in the correct
1c057 20 6f 72 64 65 72 2e 0a 20 20 2a 2f 0a 20 20 69   order..  */.  i
1c058 66 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20  f( (p->selFlags 
1c059 26 20 53 46 5f 45 78 70 61 6e 64 65 64 29 3d 3d  & SF_Expanded)==
1c05a 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
1c05b 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73  SelectPrep(pPars
1c05c 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e 43 29 3b  e, p, pOuterNC);
1c05d 0a 20 20 20 20 72 65 74 75 72 6e 20 28 70 50 61  .    return (pPa
1c05e 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
1c05f 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 29 20 3f  >mallocFailed) ?
1c060 20 57 52 43 5f 41 62 6f 72 74 20 3a 20 57 52 43   WRC_Abort : WRC
1c061 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 0a 20 20 69  _Prune;.  }..  i
1c062 73 43 6f 6d 70 6f 75 6e 64 20 3d 20 70 2d 3e 70  sCompound = p->p
1c063 50 72 69 6f 72 21 3d 30 3b 0a 20 20 6e 43 6f 6d  Prior!=0;.  nCom
1c064 70 6f 75 6e 64 20 3d 20 30 3b 0a 20 20 70 4c 65  pound = 0;.  pLe
1c065 66 74 6d 6f 73 74 20 3d 20 70 3b 0a 20 20 77 68  ftmost = p;.  wh
1c066 69 6c 65 28 20 70 20 29 7b 0a 20 20 20 20 61 73  ile( p ){.    as
1c067 73 65 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61  sert( (p->selFla
1c068 67 73 20 26 20 53 46 5f 45 78 70 61 6e 64 65 64  gs & SF_Expanded
1c069 29 21 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65  )!=0 );.    asse
1c06a 72 74 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73  rt( (p->selFlags
1c06b 20 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 3d   & SF_Resolved)=
1c06c 3d 30 20 29 3b 0a 20 20 20 20 70 2d 3e 73 65 6c  =0 );.    p->sel
1c06d 46 6c 61 67 73 20 7c 3d 20 53 46 5f 52 65 73 6f  Flags |= SF_Reso
1c06e 6c 76 65 64 3b 0a 0a 20 20 20 20 2f 2a 20 52 65  lved;..    /* Re
1c06f 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65 73  solve the expres
1c070 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 4c 49 4d  sions in the LIM
1c071 49 54 20 61 6e 64 20 4f 46 46 53 45 54 20 63 6c  IT and OFFSET cl
1c072 61 75 73 65 73 2e 20 54 68 65 73 65 0a 20 20 20  auses. These.   
1c073 20 2a 2a 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f   ** are not allo
1c074 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f 20  wed to refer to 
1c075 61 6e 79 20 6e 61 6d 65 73 2c 20 73 6f 20 70 61  any names, so pa
1c076 73 73 20 61 6e 20 65 6d 70 74 79 20 4e 61 6d 65  ss an empty Name
1c077 43 6f 6e 74 65 78 74 2e 0a 20 20 20 20 2a 2f 0a  Context..    */.
1c078 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43 2c      memset(&sNC,
1c079 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29 29   0, sizeof(sNC))
1c07a 3b 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65  ;.    sNC.pParse
1c07b 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 69   = pParse;.    i
1c07c 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
1c07d 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
1c07e 20 70 2d 3e 70 4c 69 6d 69 74 29 20 7c 7c 0a 20   p->pLimit) ||. 
1c07f 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65         sqlite3Re
1c080 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26  solveExprNames(&
1c081 73 4e 43 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29  sNC, p->pOffset)
1c082 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
1c083 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20   WRC_Abort;.    
1c084 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 53 65 74 20  }.  .    /* Set 
1c085 75 70 20 74 68 65 20 6c 6f 63 61 6c 20 6e 61 6d  up the local nam
1c086 65 2d 63 6f 6e 74 65 78 74 20 74 6f 20 70 61 73  e-context to pas
1c087 73 20 74 6f 20 73 71 6c 69 74 65 33 52 65 73 6f  s to sqlite3Reso
1c088 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 29 20 74  lveExprNames() t
1c089 6f 0a 20 20 20 20 2a 2a 20 72 65 73 6f 6c 76 65  o.    ** resolve
1c08a 20 74 68 65 20 72 65 73 75 6c 74 2d 73 65 74 20   the result-set 
1c08b 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e  expression list.
1c08c 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e  .    */.    sNC.
1c08d 61 6c 6c 6f 77 41 67 67 20 3d 20 31 3b 0a 20 20  allowAgg = 1;.  
1c08e 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20 3d    sNC.pSrcList =
1c08f 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20 20 73 4e   p->pSrc;.    sN
1c090 43 2e 70 4e 65 78 74 20 3d 20 70 4f 75 74 65 72  C.pNext = pOuter
1c091 4e 43 3b 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65  NC;.  .    /* Re
1c092 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 74  solve names in t
1c093 68 65 20 72 65 73 75 6c 74 20 73 65 74 2e 20 2a  he result set. *
1c094 2f 0a 20 20 20 20 70 45 4c 69 73 74 20 3d 20 70  /.    pEList = p
1c095 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 20 20 61 73  ->pEList;.    as
1c096 73 65 72 74 28 20 70 45 4c 69 73 74 21 3d 30 20  sert( pEList!=0 
1c097 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
1c098 69 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3b  i<pEList->nExpr;
1c099 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
1c09a 72 20 2a 70 58 20 3d 20 70 45 4c 69 73 74 2d 3e  r *pX = pEList->
1c09b 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
1c09c 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 73    if( sqlite3Res
1c09d 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 26 73  olveExprNames(&s
1c09e 4e 43 2c 20 70 58 29 20 29 7b 0a 20 20 20 20 20  NC, pX) ){.     
1c09f 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1c0a0 6f 72 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  ort;.      }.   
1c0a1 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 52 65 63   }.  .    /* Rec
1c0a2 75 72 73 69 76 65 6c 79 20 72 65 73 6f 6c 76 65  ursively resolve
1c0a3 20 6e 61 6d 65 73 20 69 6e 20 61 6c 6c 20 73 75   names in all su
1c0a4 62 71 75 65 72 69 65 73 0a 20 20 20 20 2a 2f 0a  bqueries.    */.
1c0a5 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1c0a6 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20 69 2b  ->pSrc->nSrc; i+
1c0a7 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75 63 74  +){.      struct
1c0a8 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70   SrcList_item *p
1c0a9 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72 63 2d  Item = &p->pSrc-
1c0aa 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66 28  >a[i];.      if(
1c0ab 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20   pItem->pSelect 
1c0ac 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  ){.        const
1c0ad 20 63 68 61 72 20 2a 7a 53 61 76 65 64 43 6f 6e   char *zSavedCon
1c0ae 74 65 78 74 20 3d 20 70 50 61 72 73 65 2d 3e 7a  text = pParse->z
1c0af 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 20 20 20  AuthContext;.   
1c0b0 20 20 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e       if( pItem->
1c0b1 7a 4e 61 6d 65 20 29 20 70 50 61 72 73 65 2d 3e  zName ) pParse->
1c0b2 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  zAuthContext = p
1c0b3 49 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20  Item->zName;.   
1c0b4 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 73 6f       sqlite3Reso
1c0b5 6c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70  lveSelectNames(p
1c0b6 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 70 53  Parse, pItem->pS
1c0b7 65 6c 65 63 74 2c 20 26 73 4e 43 29 3b 0a 20 20  elect, &sNC);.  
1c0b8 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41        pParse->zA
1c0b9 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61  uthContext = zSa
1c0ba 76 65 64 43 6f 6e 74 65 78 74 3b 0a 20 20 20 20  vedContext;.    
1c0bb 20 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e      if( pParse->
1c0bc 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
1c0bd 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
1c0be 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1c0bf 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 0a 20 20     }.    }.  .  
1c0c0 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1c0c1 65 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66  e no aggregate f
1c0c2 75 6e 63 74 69 6f 6e 73 20 69 6e 20 74 68 65 20  unctions in the 
1c0c3 72 65 73 75 6c 74 2d 73 65 74 2c 20 61 6e 64 20  result-set, and 
1c0c4 6e 6f 20 47 52 4f 55 50 20 42 59 20 0a 20 20 20  no GROUP BY .   
1c0c5 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2c 20   ** expression, 
1c0c6 64 6f 20 6e 6f 74 20 61 6c 6c 6f 77 20 61 67 67  do not allow agg
1c0c7 72 65 67 61 74 65 73 20 69 6e 20 61 6e 79 20 6f  regates in any o
1c0c8 66 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72  f the other expr
1c0c9 65 73 73 69 6f 6e 73 2e 0a 20 20 20 20 2a 2f 0a  essions..    */.
1c0ca 20 20 20 20 61 73 73 65 72 74 28 20 28 70 2d 3e      assert( (p->
1c0cb 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41 67  selFlags & SF_Ag
1c0cc 67 72 65 67 61 74 65 29 3d 3d 30 20 29 3b 0a 20  gregate)==0 );. 
1c0cd 20 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d     pGroupBy = p-
1c0ce 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 20 20 69  >pGroupBy;.    i
1c0cf 66 28 20 70 47 72 6f 75 70 42 79 20 7c 7c 20 73  f( pGroupBy || s
1c0d0 4e 43 2e 68 61 73 41 67 67 20 29 7b 0a 20 20 20  NC.hasAgg ){.   
1c0d1 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20 7c     p->selFlags |
1c0d2 3d 20 53 46 5f 41 67 67 72 65 67 61 74 65 3b 0a  = SF_Aggregate;.
1c0d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1c0d4 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67 20 3d 20   sNC.allowAgg = 
1c0d5 30 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20  0;.    }.  .    
1c0d6 2f 2a 20 49 66 20 61 20 48 41 56 49 4e 47 20 63  /* If a HAVING c
1c0d7 6c 61 75 73 65 20 69 73 20 70 72 65 73 65 6e 74  lause is present
1c0d8 2c 20 74 68 65 6e 20 74 68 65 72 65 20 6d 75 73  , then there mus
1c0d9 74 20 62 65 20 61 20 47 52 4f 55 50 20 42 59 20  t be a GROUP BY 
1c0da 63 6c 61 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  clause..    */. 
1c0db 20 20 20 69 66 28 20 70 2d 3e 70 48 61 76 69 6e     if( p->pHavin
1c0dc 67 20 26 26 20 21 70 47 72 6f 75 70 42 79 20 29  g && !pGroupBy )
1c0dd 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  {.      sqlite3E
1c0de 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1c0df 22 61 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75  "a GROUP BY clau
1c0e0 73 65 20 69 73 20 72 65 71 75 69 72 65 64 20 62  se is required b
1c0e1 65 66 6f 72 65 20 48 41 56 49 4e 47 22 29 3b 0a  efore HAVING");.
1c0e2 20 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43        return WRC
1c0e3 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20  _Abort;.    }.  
1c0e4 0a 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20  .    /* Add the 
1c0e5 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
1c0e6 74 6f 20 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74  to the name-cont
1c0e7 65 78 74 20 62 65 66 6f 72 65 20 70 61 72 73 69  ext before parsi
1c0e8 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 6f 74  ng the.    ** ot
1c0e9 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20  her expressions 
1c0ea 69 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  in the SELECT st
1c0eb 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69 73  atement. This is
1c0ec 20 73 6f 20 74 68 61 74 0a 20 20 20 20 2a 2a 20   so that.    ** 
1c0ed 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74  expressions in t
1c0ee 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
1c0ef 28 65 74 63 2e 29 20 63 61 6e 20 72 65 66 65 72  (etc.) can refer
1c0f0 20 74 6f 20 65 78 70 72 65 73 73 69 6f 6e 73 20   to expressions 
1c0f1 62 79 0a 20 20 20 20 2a 2a 20 61 6c 69 61 73 65  by.    ** aliase
1c0f2 73 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  s in the result 
1c0f3 73 65 74 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20  set..    **.    
1c0f4 2a 2a 20 4d 69 6e 6f 72 20 70 6f 69 6e 74 3a 20  ** Minor point: 
1c0f5 49 66 20 74 68 69 73 20 69 73 20 74 68 65 20 63  If this is the c
1c0f6 61 73 65 2c 20 74 68 65 6e 20 74 68 65 20 65 78  ase, then the ex
1c0f7 70 72 65 73 73 69 6f 6e 20 77 69 6c 6c 20 62 65  pression will be
1c0f8 0a 20 20 20 20 2a 2a 20 72 65 2d 65 76 61 6c 75  .    ** re-evalu
1c0f9 61 74 65 64 20 66 6f 72 20 65 61 63 68 20 72 65  ated for each re
1c0fa 66 65 72 65 6e 63 65 20 74 6f 20 69 74 2e 0a 20  ference to it.. 
1c0fb 20 20 20 2a 2f 0a 20 20 20 20 73 4e 43 2e 70 45     */.    sNC.pE
1c0fc 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69 73 74  List = p->pEList
1c0fd 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
1c0fe 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
1c0ff 73 28 26 73 4e 43 2c 20 70 2d 3e 70 57 68 65 72  s(&sNC, p->pWher
1c100 65 29 20 7c 7c 0a 20 20 20 20 20 20 20 73 71 6c  e) ||.       sql
1c101 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e  ite3ResolveExprN
1c102 61 6d 65 73 28 26 73 4e 43 2c 20 70 2d 3e 70 48  ames(&sNC, p->pH
1c103 61 76 69 6e 67 29 0a 20 20 20 20 29 7b 0a 20 20  aving).    ){.  
1c104 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41      return WRC_A
1c105 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  bort;.    }..   
1c106 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59   /* The ORDER BY
1c107 20 61 6e 64 20 47 52 4f 55 50 20 42 59 20 63 6c   and GROUP BY cl
1c108 61 75 73 65 73 20 6d 61 79 20 6e 6f 74 20 72 65  auses may not re
1c109 66 65 72 20 74 6f 20 74 65 72 6d 73 20 69 6e 0a  fer to terms in.
1c10a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75 65      ** outer que
1c10b 72 69 65 73 20 0a 20 20 20 20 2a 2f 0a 20 20 20  ries .    */.   
1c10c 20 73 4e 43 2e 70 4e 65 78 74 20 3d 20 30 3b 0a   sNC.pNext = 0;.
1c10d 20 20 20 20 73 4e 43 2e 61 6c 6c 6f 77 41 67 67      sNC.allowAgg
1c10e 20 3d 20 31 3b 0a 0a 20 20 20 20 2f 2a 20 50 72   = 1;..    /* Pr
1c10f 6f 63 65 73 73 20 74 68 65 20 4f 52 44 45 52 20  ocess the ORDER 
1c110 42 59 20 63 6c 61 75 73 65 20 66 6f 72 20 73 69  BY clause for si
1c111 6e 67 6c 65 74 6f 6e 20 53 45 4c 45 43 54 20 73  ngleton SELECT s
1c112 74 61 74 65 6d 65 6e 74 73 2e 0a 20 20 20 20 2a  tatements..    *
1c113 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
1c114 6c 61 75 73 65 20 66 6f 72 20 63 6f 6d 70 6f 75  lause for compou
1c115 6e 64 73 20 53 45 4c 45 43 54 20 73 74 61 74 65  nds SELECT state
1c116 6d 65 6e 74 73 20 69 73 20 68 61 6e 64 6c 65 64  ments is handled
1c117 0a 20 20 20 20 2a 2a 20 62 65 6c 6f 77 2c 20 61  .    ** below, a
1c118 66 74 65 72 20 61 6c 6c 20 6f 66 20 74 68 65 20  fter all of the 
1c119 72 65 73 75 6c 74 2d 73 65 74 73 20 66 6f 72 20  result-sets for 
1c11a 61 6c 6c 20 6f 66 20 74 68 65 20 65 6c 65 6d 65  all of the eleme
1c11b 6e 74 73 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68  nts of.    ** th
1c11c 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20  e compound have 
1c11d 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20  been resolved.. 
1c11e 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 69     */.    if( !i
1c11f 73 43 6f 6d 70 6f 75 6e 64 20 26 26 20 72 65 73  sCompound && res
1c120 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79  olveOrderGroupBy
1c121 28 26 73 4e 43 2c 20 70 2c 20 70 2d 3e 70 4f 72  (&sNC, p, p->pOr
1c122 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 20  derBy, "ORDER") 
1c123 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
1c124 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d  WRC_Abort;.    }
1c125 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  .    if( db->mal
1c126 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1c127 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62     return WRC_Ab
1c128 6f 72 74 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20  ort;.    }.  .  
1c129 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20 74 68 65    /* Resolve the
1c12a 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65   GROUP BY clause
1c12b 2e 20 20 41 74 20 74 68 65 20 73 61 6d 65 20 74  .  At the same t
1c12c 69 6d 65 2c 20 6d 61 6b 65 20 73 75 72 65 20 0a  ime, make sure .
1c12d 20 20 20 20 2a 2a 20 74 68 65 20 47 52 4f 55 50      ** the GROUP
1c12e 20 42 59 20 63 6c 61 75 73 65 20 64 6f 65 73 20   BY clause does 
1c12f 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 67 67 72  not contain aggr
1c130 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 2e  egate functions.
1c131 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
1c132 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20 20 20  pGroupBy ){.    
1c133 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73    struct ExprLis
1c134 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20  t_item *pItem;. 
1c135 20 20 20 0a 20 20 20 20 20 20 69 66 28 20 72 65     .      if( re
1c136 73 6f 6c 76 65 4f 72 64 65 72 47 72 6f 75 70 42  solveOrderGroupB
1c137 79 28 26 73 4e 43 2c 20 70 2c 20 70 47 72 6f 75  y(&sNC, p, pGrou
1c138 70 42 79 2c 20 22 47 52 4f 55 50 22 29 20 7c 7c  pBy, "GROUP") ||
1c139 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1c13a 64 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74  d ){.        ret
1c13b 75 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20  urn WRC_Abort;. 
1c13c 20 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72       }.      for
1c13d 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 47 72 6f  (i=0, pItem=pGro
1c13e 75 70 42 79 2d 3e 61 3b 20 69 3c 70 47 72 6f 75  upBy->a; i<pGrou
1c13f 70 42 79 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c  pBy->nExpr; i++,
1c140 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20   pItem++){.     
1c141 20 20 20 69 66 28 20 45 78 70 72 48 61 73 50 72     if( ExprHasPr
1c142 6f 70 65 72 74 79 28 70 49 74 65 6d 2d 3e 70 45  operty(pItem->pE
1c143 78 70 72 2c 20 45 50 5f 41 67 67 29 20 29 7b 0a  xpr, EP_Agg) ){.
1c144 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1c145 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1c146 2c 20 22 61 67 67 72 65 67 61 74 65 20 66 75 6e  , "aggregate fun
1c147 63 74 69 6f 6e 73 20 61 72 65 20 6e 6f 74 20 61  ctions are not a
1c148 6c 6c 6f 77 65 64 20 69 6e 20 22 0a 20 20 20 20  llowed in ".    
1c149 20 20 20 20 20 20 20 20 20 20 22 74 68 65 20 47            "the G
1c14a 52 4f 55 50 20 42 59 20 63 6c 61 75 73 65 22 29  ROUP BY clause")
1c14b 3b 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75  ;.          retu
1c14c 72 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20  rn WRC_Abort;.  
1c14d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
1c14e 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 64      }..    /* Ad
1c14f 76 61 6e 63 65 20 74 6f 20 74 68 65 20 6e 65 78  vance to the nex
1c150 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 63 6f  t term of the co
1c151 6d 70 6f 75 6e 64 0a 20 20 20 20 2a 2f 0a 20 20  mpound.    */.  
1c152 20 20 70 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b    p = p->pPrior;
1c153 0a 20 20 20 20 6e 43 6f 6d 70 6f 75 6e 64 2b 2b  .    nCompound++
1c154 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 6f  ;.  }..  /* Reso
1c155 6c 76 65 20 74 68 65 20 4f 52 44 45 52 20 42 59  lve the ORDER BY
1c156 20 6f 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 20 53   on a compound S
1c157 45 4c 45 43 54 20 61 66 74 65 72 20 61 6c 6c 20  ELECT after all 
1c158 74 65 72 6d 73 20 6f 66 0a 20 20 2a 2a 20 74 68  terms of.  ** th
1c159 65 20 63 6f 6d 70 6f 75 6e 64 20 68 61 76 65 20  e compound have 
1c15a 62 65 65 6e 20 72 65 73 6f 6c 76 65 64 2e 0a 20  been resolved.. 
1c15b 20 2a 2f 0a 20 20 69 66 28 20 69 73 43 6f 6d 70   */.  if( isComp
1c15c 6f 75 6e 64 20 26 26 20 72 65 73 6f 6c 76 65 43  ound && resolveC
1c15d 6f 6d 70 6f 75 6e 64 4f 72 64 65 72 42 79 28 70  ompoundOrderBy(p
1c15e 50 61 72 73 65 2c 20 70 4c 65 66 74 6d 6f 73 74  Parse, pLeftmost
1c15f 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  ) ){.    return 
1c160 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 0a  WRC_Abort;.  }..
1c161 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1c162 6e 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  ne;.}../*.** Thi
1c163 73 20 72 6f 75 74 69 6e 65 20 77 61 6c 6b 73 20  s routine walks 
1c164 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
1c165 65 65 20 61 6e 64 20 72 65 73 6f 6c 76 65 73 20  ee and resolves 
1c166 72 65 66 65 72 65 6e 63 65 73 20 74 6f 0a 2a 2a  references to.**
1c167 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 73 20 61   table columns a
1c168 6e 64 20 72 65 73 75 6c 74 2d 73 65 74 20 63 6f  nd result-set co
1c169 6c 75 6d 6e 73 2e 20 20 41 74 20 74 68 65 20 73  lumns.  At the s
1c16a 61 6d 65 20 74 69 6d 65 2c 20 64 6f 20 65 72 72  ame time, do err
1c16b 6f 72 0a 2a 2a 20 63 68 65 63 6b 69 6e 67 20 6f  or.** checking o
1c16c 6e 20 66 75 6e 63 74 69 6f 6e 20 75 73 61 67 65  n function usage
1c16d 20 61 6e 64 20 73 65 74 20 61 20 66 6c 61 67 20   and set a flag 
1c16e 69 66 20 61 6e 79 20 61 67 67 72 65 67 61 74 65  if any aggregate
1c16f 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 20 61 72   functions.** ar
1c170 65 20 73 65 65 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f  e seen..**.** To
1c171 20 72 65 73 6f 6c 76 65 20 74 61 62 6c 65 20 63   resolve table c
1c172 6f 6c 75 6d 6e 73 20 72 65 66 65 72 65 6e 63 65  olumns reference
1c173 73 20 77 65 20 6c 6f 6f 6b 20 66 6f 72 20 6e 6f  s we look for no
1c174 64 65 73 20 28 6f 72 20 73 75 62 74 72 65 65 73  des (or subtrees
1c175 29 20 6f 66 20 74 68 65 20 0a 2a 2a 20 66 6f 72  ) of the .** for
1c176 6d 20 58 2e 59 2e 5a 20 6f 72 20 59 2e 5a 20 6f  m X.Y.Z or Y.Z o
1c177 72 20 6a 75 73 74 20 5a 20 77 68 65 72 65 0a 2a  r just Z where.*
1c178 2a 0a 2a 2a 20 20 20 20 20 20 58 3a 20 20 20 54  *.**      X:   T
1c179 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64 61 74  he name of a dat
1c17a 61 62 61 73 65 2e 20 20 45 78 3a 20 20 22 6d 61  abase.  Ex:  "ma
1c17b 69 6e 22 20 6f 72 20 22 74 65 6d 70 22 20 6f 72  in" or "temp" or
1c17c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 74 68  .**           th
1c17d 65 20 73 79 6d 62 6f 6c 69 63 20 6e 61 6d 65 20  e symbolic name 
1c17e 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6e 20 41  assigned to an A
1c17f 54 54 41 43 48 2d 65 64 20 64 61 74 61 62 61 73  TTACH-ed databas
1c180 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 59 3a  e..**.**      Y:
1c181 20 20 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 61     The name of a
1c182 20 74 61 62 6c 65 20 69 6e 20 61 20 46 52 4f 4d   table in a FROM
1c183 20 63 6c 61 75 73 65 2e 20 20 4f 72 20 69 6e 20   clause.  Or in 
1c184 61 20 74 72 69 67 67 65 72 0a 2a 2a 20 20 20 20  a trigger.**    
1c185 20 20 20 20 20 20 20 6f 6e 65 20 6f 66 20 74 68         one of th
1c186 65 20 73 70 65 63 69 61 6c 20 6e 61 6d 65 73 20  e special names 
1c187 22 6f 6c 64 22 20 6f 72 20 22 6e 65 77 22 2e 0a  "old" or "new"..
1c188 2a 2a 0a 2a 2a 20 20 20 20 20 20 5a 3a 20 20 20  **.**      Z:   
1c189 54 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 63 6f  The name of a co
1c18a 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 59 2e  lumn in table Y.
1c18b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6e 6f 64 65 20  .**.** The node 
1c18c 61 74 20 74 68 65 20 72 6f 6f 74 20 6f 66 20 74  at the root of t
1c18d 68 65 20 73 75 62 74 72 65 65 20 69 73 20 6d 6f  he subtree is mo
1c18e 64 69 66 69 65 64 20 61 73 20 66 6f 6c 6c 6f 77  dified as follow
1c18f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 45 78 70 72  s:.**.**    Expr
1c190 2e 6f 70 20 20 20 20 20 20 20 20 43 68 61 6e 67  .op        Chang
1c191 65 64 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 0a  ed to TK_COLUMN.
1c192 2a 2a 20 20 20 20 45 78 70 72 2e 70 54 61 62 20  **    Expr.pTab 
1c193 20 20 20 20 20 50 6f 69 6e 74 73 20 74 6f 20 74       Points to t
1c194 68 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20  he Table object 
1c195 66 6f 72 20 58 2e 59 0a 2a 2a 20 20 20 20 45 78  for X.Y.**    Ex
1c196 70 72 2e 69 43 6f 6c 75 6d 6e 20 20 20 54 68 65  pr.iColumn   The
1c197 20 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 20 69 6e   column index in
1c198 20 58 2e 59 2e 20 20 2d 31 20 66 6f 72 20 74 68   X.Y.  -1 for th
1c199 65 20 72 6f 77 69 64 2e 0a 2a 2a 20 20 20 20 45  e rowid..**    E
1c19a 78 70 72 2e 69 54 61 62 6c 65 20 20 20 20 54 68  xpr.iTable    Th
1c19b 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
1c19c 6d 62 65 72 20 66 6f 72 20 58 2e 59 0a 2a 2a 0a  mber for X.Y.**.
1c19d 2a 2a 0a 2a 2a 20 54 6f 20 72 65 73 6f 6c 76 65  **.** To resolve
1c19e 20 72 65 73 75 6c 74 2d 73 65 74 20 72 65 66 65   result-set refe
1c19f 72 65 6e 63 65 73 2c 20 6c 6f 6f 6b 20 66 6f 72  rences, look for
1c1a0 20 65 78 70 72 65 73 73 69 6f 6e 20 6e 6f 64 65   expression node
1c1a1 73 20 6f 66 20 74 68 65 0a 2a 2a 20 66 6f 72 6d  s of the.** form
1c1a2 20 5a 20 28 77 69 74 68 20 6e 6f 20 58 20 61 6e   Z (with no X an
1c1a3 64 20 59 20 70 72 65 66 69 78 29 20 77 68 65 72  d Y prefix) wher
1c1a4 65 20 74 68 65 20 5a 20 6d 61 74 63 68 65 73 20  e the Z matches 
1c1a5 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 2a  the right-hand.*
1c1a6 2a 20 73 69 7a 65 20 6f 66 20 61 6e 20 41 53 20  * size of an AS 
1c1a7 63 6c 61 75 73 65 20 69 6e 20 74 68 65 20 72 65  clause in the re
1c1a8 73 75 6c 74 2d 73 65 74 20 6f 66 20 61 20 53 45  sult-set of a SE
1c1a9 4c 45 43 54 2e 20 20 54 68 65 20 5a 20 65 78 70  LECT.  The Z exp
1c1aa 72 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 72 65  ression.** is re
1c1ab 70 6c 61 63 65 64 20 62 79 20 61 20 63 6f 70 79  placed by a copy
1c1ac 20 6f 66 20 74 68 65 20 6c 65 66 74 2d 68 61 6e   of the left-han
1c1ad 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 72 65  d side of the re
1c1ae 73 75 6c 74 2d 73 65 74 20 65 78 70 72 65 73 73  sult-set express
1c1af 69 6f 6e 2e 0a 2a 2a 20 54 61 62 6c 65 2d 6e 61  ion..** Table-na
1c1b0 6d 65 20 61 6e 64 20 66 75 6e 63 74 69 6f 6e 20  me and function 
1c1b1 72 65 73 6f 6c 75 74 69 6f 6e 20 6f 63 63 75 72  resolution occur
1c1b2 73 20 6f 6e 20 74 68 65 20 73 75 62 73 74 69 74  s on the substit
1c1b3 75 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e 0a  uted expression.
1c1b4 2a 2a 20 74 72 65 65 2e 20 20 46 6f 72 20 65 78  ** tree.  For ex
1c1b5 61 6d 70 6c 65 2c 20 69 6e 3a 0a 2a 2a 0a 2a 2a  ample, in:.**.**
1c1b6 20 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b 62        SELECT a+b
1c1b7 20 41 53 20 78 2c 20 63 2b 64 20 41 53 20 79 20   AS x, c+d AS y 
1c1b8 46 52 4f 4d 20 74 31 20 4f 52 44 45 52 20 42 59  FROM t1 ORDER BY
1c1b9 20 78 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 22 78   x;.**.** The "x
1c1ba 22 20 74 65 72 6d 20 6f 66 20 74 68 65 20 6f 72  " term of the or
1c1bb 64 65 72 20 62 79 20 69 73 20 72 65 70 6c 61 63  der by is replac
1c1bc 65 64 20 62 79 20 22 61 2b 62 22 20 74 6f 20 72  ed by "a+b" to r
1c1bd 65 6e 64 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ender:.**.**    
1c1be 20 20 53 45 4c 45 43 54 20 61 2b 62 20 41 53 20    SELECT a+b AS 
1c1bf 78 2c 20 63 2b 64 20 41 53 20 79 20 46 52 4f 4d  x, c+d AS y FROM
1c1c0 20 74 31 20 4f 52 44 45 52 20 42 59 20 61 2b 62   t1 ORDER BY a+b
1c1c1 3b 0a 2a 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f 6e  ;.**.** Function
1c1c2 20 63 61 6c 6c 73 20 61 72 65 20 63 68 65 63 6b   calls are check
1c1c3 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
1c1c4 74 68 61 74 20 74 68 65 20 66 75 6e 63 74 69 6f  that the functio
1c1c5 6e 20 69 73 20 0a 2a 2a 20 64 65 66 69 6e 65 64  n is .** defined
1c1c6 20 61 6e 64 20 74 68 61 74 20 74 68 65 20 63 6f   and that the co
1c1c7 72 72 65 63 74 20 6e 75 6d 62 65 72 20 6f 66 20  rrect number of 
1c1c8 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20 73 70  arguments are sp
1c1c9 65 63 69 66 69 65 64 2e 0a 2a 2a 20 49 66 20 74  ecified..** If t
1c1ca 68 65 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 61  he function is a
1c1cb 6e 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63  n aggregate func
1c1cc 74 69 6f 6e 2c 20 74 68 65 6e 20 74 68 65 20 70  tion, then the p
1c1cd 4e 43 2d 3e 68 61 73 41 67 67 20 69 73 0a 2a 2a  NC->hasAgg is.**
1c1ce 20 73 65 74 20 61 6e 64 20 74 68 65 20 6f 70 63   set and the opc
1c1cf 6f 64 65 20 69 73 20 63 68 61 6e 67 65 64 20 66  ode is changed f
1c1d0 72 6f 6d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  rom TK_FUNCTION 
1c1d1 74 6f 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54 49  to TK_AGG_FUNCTI
1c1d2 4f 4e 2e 0a 2a 2a 20 49 66 20 61 6e 20 65 78 70  ON..** If an exp
1c1d3 72 65 73 73 69 6f 6e 20 63 6f 6e 74 61 69 6e 73  ression contains
1c1d4 20 61 67 67 72 65 67 61 74 65 20 66 75 6e 63 74   aggregate funct
1c1d5 69 6f 6e 73 20 74 68 65 6e 20 74 68 65 20 45 50  ions then the EP
1c1d6 5f 41 67 67 0a 2a 2a 20 70 72 6f 70 65 72 74 79  _Agg.** property
1c1d7 20 6f 6e 20 74 68 65 20 65 78 70 72 65 73 73 69   on the expressi
1c1d8 6f 6e 20 69 73 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  on is set..**.**
1c1d9 20 41 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67   An error messag
1c1da 65 20 69 73 20 6c 65 66 74 20 69 6e 20 70 50 61  e is left in pPa
1c1db 72 73 65 20 69 66 20 61 6e 79 74 68 69 6e 67 20  rse if anything 
1c1dc 69 73 20 61 6d 69 73 73 2e 20 20 54 68 65 20 6e  is amiss.  The n
1c1dd 75 6d 62 65 72 0a 2a 2a 20 69 66 20 65 72 72 6f  umber.** if erro
1c1de 72 73 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a  rs is returned..
1c1df 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1c1e0 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52 65 73  E int sqlite3Res
1c1e1 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 20 0a  olveExprNames( .
1c1e2 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70    NameContext *p
1c1e3 4e 43 2c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  NC,       /* Nam
1c1e4 65 73 70 61 63 65 20 74 6f 20 72 65 73 6f 6c 76  espace to resolv
1c1e5 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e  e expressions in
1c1e6 2e 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78  . */.  Expr *pEx
1c1e7 70 72 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  pr             /
1c1e8 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e  * The expression
1c1e9 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e   to be analyzed.
1c1ea 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 73 61 76   */.){.  int sav
1c1eb 65 64 48 61 73 41 67 67 3b 0a 20 20 57 61 6c 6b  edHasAgg;.  Walk
1c1ec 65 72 20 77 3b 0a 0a 20 20 69 66 28 20 70 45 78  er w;..  if( pEx
1c1ed 70 72 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30  pr==0 ) return 0
1c1ee 3b 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  ;.#if SQLITE_MAX
1c1ef 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20  _EXPR_DEPTH>0.  
1c1f0 7b 0a 20 20 20 20 50 61 72 73 65 20 2a 70 50 61  {.    Parse *pPa
1c1f1 72 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73  rse = pNC->pPars
1c1f2 65 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  e;.    if( sqlit
1c1f3 65 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68  e3ExprCheckHeigh
1c1f4 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  t(pParse, pExpr-
1c1f5 3e 6e 48 65 69 67 68 74 2b 70 4e 43 2d 3e 70 50  >nHeight+pNC->pP
1c1f6 61 72 73 65 2d 3e 6e 48 65 69 67 68 74 29 20 29  arse->nHeight) )
1c1f7 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  {.      return 1
1c1f8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
1c1f9 73 65 2d 3e 6e 48 65 69 67 68 74 20 2b 3d 20 70  se->nHeight += p
1c1fa 45 78 70 72 2d 3e 6e 48 65 69 67 68 74 3b 0a 20  Expr->nHeight;. 
1c1fb 20 7d 0a 23 65 6e 64 69 66 0a 20 20 73 61 76 65   }.#endif.  save
1c1fc 64 48 61 73 41 67 67 20 3d 20 70 4e 43 2d 3e 68  dHasAgg = pNC->h
1c1fd 61 73 41 67 67 3b 0a 20 20 70 4e 43 2d 3e 68 61  asAgg;.  pNC->ha
1c1fe 73 41 67 67 20 3d 20 30 3b 0a 20 20 77 2e 78 45  sAgg = 0;.  w.xE
1c1ff 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65  xprCallback = re
1c200 73 6f 6c 76 65 45 78 70 72 53 74 65 70 3b 0a 20  solveExprStep;. 
1c201 20 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61   w.xSelectCallba
1c202 63 6b 20 3d 20 72 65 73 6f 6c 76 65 53 65 6c 65  ck = resolveSele
1c203 63 74 53 74 65 70 3b 0a 20 20 77 2e 70 50 61 72  ctStep;.  w.pPar
1c204 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1c205 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d 20 70 4e  ;.  w.u.pNC = pN
1c206 43 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b  C;.  sqlite3Walk
1c207 45 78 70 72 28 26 77 2c 20 70 45 78 70 72 29 3b  Expr(&w, pExpr);
1c208 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f  .#if SQLITE_MAX_
1c209 45 58 50 52 5f 44 45 50 54 48 3e 30 0a 20 20 70  EXPR_DEPTH>0.  p
1c20a 4e 43 2d 3e 70 50 61 72 73 65 2d 3e 6e 48 65 69  NC->pParse->nHei
1c20b 67 68 74 20 2d 3d 20 70 45 78 70 72 2d 3e 6e 48  ght -= pExpr->nH
1c20c 65 69 67 68 74 3b 0a 23 65 6e 64 69 66 0a 20 20  eight;.#endif.  
1c20d 69 66 28 20 70 4e 43 2d 3e 6e 45 72 72 3e 30 20  if( pNC->nErr>0 
1c20e 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
1c20f 6f 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50  operty(pExpr, EP
1c210 5f 45 72 72 6f 72 29 3b 0a 20 20 7d 0a 20 20 69  _Error);.  }.  i
1c211 66 28 20 70 4e 43 2d 3e 68 61 73 41 67 67 20 29  f( pNC->hasAgg )
1c212 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72 6f  {.    ExprSetPro
1c213 70 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f  perty(pExpr, EP_
1c214 41 67 67 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66  Agg);.  }else if
1c215 28 20 73 61 76 65 64 48 61 73 41 67 67 20 29 7b  ( savedHasAgg ){
1c216 0a 20 20 20 20 70 4e 43 2d 3e 68 61 73 41 67 67  .    pNC->hasAgg
1c217 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 1;.  }.  retu
1c218 72 6e 20 45 78 70 72 48 61 73 50 72 6f 70 65 72  rn ExprHasProper
1c219 74 79 28 70 45 78 70 72 2c 20 45 50 5f 45 72 72  ty(pExpr, EP_Err
1c21a 6f 72 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 52  or);.}.../*.** R
1c21b 65 73 6f 6c 76 65 20 61 6c 6c 20 6e 61 6d 65 73  esolve all names
1c21c 20 69 6e 20 61 6c 6c 20 65 78 70 72 65 73 73 69   in all expressi
1c21d 6f 6e 73 20 6f 66 20 61 20 53 45 4c 45 43 54 20  ons of a SELECT 
1c21e 61 6e 64 20 69 6e 20 61 6c 6c 0a 2a 2a 20 64 65  and in all.** de
1c21f 63 65 6e 64 65 6e 74 73 20 6f 66 20 74 68 65 20  cendents of the 
1c220 53 45 4c 45 43 54 2c 20 69 6e 63 6c 75 64 69 6e  SELECT, includin
1c221 67 20 63 6f 6d 70 6f 75 6e 64 73 20 6f 66 66 20  g compounds off 
1c222 6f 66 20 70 2d 3e 70 50 72 69 6f 72 2c 0a 2a 2a  of p->pPrior,.**
1c223 20 73 75 62 71 75 65 72 69 65 73 20 69 6e 20 65   subqueries in e
1c224 78 70 72 65 73 73 69 6f 6e 73 2c 20 61 6e 64 20  xpressions, and 
1c225 73 75 62 71 75 65 72 69 65 73 20 75 73 65 64 20  subqueries used 
1c226 61 73 20 46 52 4f 4d 20 63 6c 61 75 73 65 0a 2a  as FROM clause.*
1c227 2a 20 74 65 72 6d 73 2e 0a 2a 2a 0a 2a 2a 20 53  * terms..**.** S
1c228 65 65 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ee sqlite3Resolv
1c229 65 45 78 70 72 4e 61 6d 65 73 28 29 20 66 6f 72  eExprNames() for
1c22a 20 61 20 64 65 73 63 72 69 70 74 69 6f 6e 20 6f   a description o
1c22b 66 20 74 68 65 20 6b 69 6e 64 73 20 6f 66 0a 2a  f the kinds of.*
1c22c 2a 20 74 72 61 6e 73 66 6f 72 6d 61 74 69 6f 6e  * transformation
1c22d 73 20 74 68 61 74 20 6f 63 63 75 72 2e 0a 2a 2a  s that occur..**
1c22e 0a 2a 2a 20 41 6c 6c 20 53 45 4c 45 43 54 20 73  .** All SELECT s
1c22f 74 61 74 65 6d 65 6e 74 73 20 73 68 6f 75 6c 64  tatements should
1c230 20 68 61 76 65 20 62 65 65 6e 20 65 78 70 61 6e   have been expan
1c231 64 65 64 20 75 73 69 6e 67 0a 2a 2a 20 73 71 6c  ded using.** sql
1c232 69 74 65 33 53 65 6c 65 63 74 45 78 70 61 6e 64  ite3SelectExpand
1c233 28 29 20 70 72 69 6f 72 20 74 6f 20 69 6e 76 6f  () prior to invo
1c234 6b 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e  king this routin
1c235 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1c236 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1c237 33 52 65 73 6f 6c 76 65 53 65 6c 65 63 74 4e 61  3ResolveSelectNa
1c238 6d 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50  mes(.  Parse *pP
1c239 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a  arse,         /*
1c23a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74   The parser cont
1c23b 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20  ext */.  Select 
1c23c 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  *p,             
1c23d 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
1c23e 61 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f  atement being co
1c23f 64 65 64 2e 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  ded. */.  NameCo
1c240 6e 74 65 78 74 20 2a 70 4f 75 74 65 72 4e 43 20  ntext *pOuterNC 
1c241 20 2f 2a 20 4e 61 6d 65 20 63 6f 6e 74 65 78 74   /* Name context
1c242 20 66 6f 72 20 70 61 72 65 6e 74 20 53 45 4c 45   for parent SELE
1c243 43 54 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  CT statement */.
1c244 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a 0a  ){.  Walker w;..
1c245 20 20 61 73 73 65 72 74 28 20 70 21 3d 30 20 29    assert( p!=0 )
1c246 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c 6c 62  ;.  w.xExprCallb
1c247 61 63 6b 20 3d 20 72 65 73 6f 6c 76 65 45 78 70  ack = resolveExp
1c248 72 53 74 65 70 3b 0a 20 20 77 2e 78 53 65 6c 65  rStep;.  w.xSele
1c249 63 74 43 61 6c 6c 62 61 63 6b 20 3d 20 72 65 73  ctCallback = res
1c24a 6f 6c 76 65 53 65 6c 65 63 74 53 74 65 70 3b 0a  olveSelectStep;.
1c24b 20 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61    w.pParse = pPa
1c24c 72 73 65 3b 0a 20 20 77 2e 75 2e 70 4e 43 20 3d  rse;.  w.u.pNC =
1c24d 20 70 4f 75 74 65 72 4e 43 3b 0a 20 20 73 71 6c   pOuterNC;.  sql
1c24e 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 26  ite3WalkSelect(&
1c24f 77 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  w, p);.}../*****
1c250 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
1c251 20 72 65 73 6f 6c 76 65 2e 63 20 2a 2a 2a 2a 2a   resolve.c *****
1c252 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c253 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c254 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
1c255 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
1c256 66 69 6c 65 20 65 78 70 72 2e 63 20 2a 2a 2a 2a  file expr.c ****
1c257 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c258 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c259 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
1c25a 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
1c25b 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
1c25c 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
1c25d 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
1c25e 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
1c25f 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
1c260 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
1c261 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
1c262 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
1c263 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
1c264 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
1c265 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
1c266 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
1c267 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
1c268 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
1c269 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
1c26a 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
1c26b 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
1c26c 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
1c26d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c26e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c26f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c270 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1c271 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
1c272 6e 74 61 69 6e 73 20 72 6f 75 74 69 6e 65 73 20  ntains routines 
1c273 75 73 65 64 20 66 6f 72 20 61 6e 61 6c 79 7a 69  used for analyzi
1c274 6e 67 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  ng expressions a
1c275 6e 64 0a 2a 2a 20 66 6f 72 20 67 65 6e 65 72 61  nd.** for genera
1c276 74 69 6e 67 20 56 44 42 45 20 63 6f 64 65 20 74  ting VDBE code t
1c277 68 61 74 20 65 76 61 6c 75 61 74 65 73 20 65 78  hat evaluates ex
1c278 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 53 51 4c  pressions in SQL
1c279 69 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  ite..**.** $Id: 
1c27a 65 78 70 72 2e 63 2c 76 20 31 2e 34 30 34 20 32  expr.c,v 1.404 2
1c27b 30 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a  008/11/19 16:52:
1c27c 34 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  44 danielk1977 E
1c27d 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  xp $.*/../*.** R
1c27e 65 74 75 72 6e 20 74 68 65 20 27 61 66 66 69 6e  eturn the 'affin
1c27f 69 74 79 27 20 6f 66 20 74 68 65 20 65 78 70 72  ity' of the expr
1c280 65 73 73 69 6f 6e 20 70 45 78 70 72 20 69 66 20  ession pExpr if 
1c281 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 45  any..**.** If pE
1c282 78 70 72 20 69 73 20 61 20 63 6f 6c 75 6d 6e 2c  xpr is a column,
1c283 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
1c284 61 20 63 6f 6c 75 6d 6e 20 76 69 61 20 61 6e 20  a column via an 
1c285 27 41 53 27 20 61 6c 69 61 73 2c 0a 2a 2a 20 6f  'AS' alias,.** o
1c286 72 20 61 20 73 75 62 2d 73 65 6c 65 63 74 20 77  r a sub-select w
1c287 69 74 68 20 61 20 63 6f 6c 75 6d 6e 20 61 73 20  ith a column as 
1c288 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
1c289 2c 20 74 68 65 6e 20 74 68 65 20 0a 2a 2a 20 61  , then the .** a
1c28a 66 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 20  ffinity of that 
1c28b 63 6f 6c 75 6d 6e 20 69 73 20 72 65 74 75 72 6e  column is return
1c28c 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 30  ed. Otherwise, 0
1c28d 78 30 30 20 69 73 20 72 65 74 75 72 6e 65 64 2c  x00 is returned,
1c28e 0a 2a 2a 20 69 6e 64 69 63 61 74 69 6e 67 20 6e  .** indicating n
1c28f 6f 20 61 66 66 69 6e 69 74 79 20 66 6f 72 20 74  o affinity for t
1c290 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a  he expression..*
1c291 2a 0a 2a 2a 20 69 2e 65 2e 20 74 68 65 20 57 48  *.** i.e. the WH
1c292 45 52 45 20 63 6c 61 75 73 65 20 65 78 70 72 65  ERE clause expre
1c293 73 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 66  sssions in the f
1c294 6f 6c 6c 6f 77 69 6e 67 20 73 74 61 74 65 6d 65  ollowing stateme
1c295 6e 74 73 20 61 6c 6c 0a 2a 2a 20 68 61 76 65 20  nts all.** have 
1c296 61 6e 20 61 66 66 69 6e 69 74 79 3a 0a 2a 2a 0a  an affinity:.**.
1c297 2a 2a 20 43 52 45 41 54 45 20 54 41 42 4c 45 20  ** CREATE TABLE 
1c298 74 31 28 61 29 3b 0a 2a 2a 20 53 45 4c 45 43 54  t1(a);.** SELECT
1c299 20 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45   * FROM t1 WHERE
1c29a 20 61 3b 0a 2a 2a 20 53 45 4c 45 43 54 20 61 20   a;.** SELECT a 
1c29b 41 53 20 62 20 46 52 4f 4d 20 74 31 20 57 48 45  AS b FROM t1 WHE
1c29c 52 45 20 62 3b 0a 2a 2a 20 53 45 4c 45 43 54 20  RE b;.** SELECT 
1c29d 2a 20 46 52 4f 4d 20 74 31 20 57 48 45 52 45 20  * FROM t1 WHERE 
1c29e 28 73 65 6c 65 63 74 20 61 20 66 72 6f 6d 20 74  (select a from t
1c29f 31 29 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  1);.*/.SQLITE_PR
1c2a0 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69 74  IVATE char sqlit
1c2a1 65 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 45  e3ExprAffinity(E
1c2a2 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20 20 69  xpr *pExpr){.  i
1c2a3 6e 74 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f  nt op = pExpr->o
1c2a4 70 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  p;.  if( op==TK_
1c2a5 53 45 4c 45 43 54 20 29 7b 0a 20 20 20 20 72 65  SELECT ){.    re
1c2a6 74 75 72 6e 20 73 71 6c 69 74 65 33 45 78 70 72  turn sqlite3Expr
1c2a7 41 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e  Affinity(pExpr->
1c2a8 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 2d  pSelect->pEList-
1c2a9 3e 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20  >a[0].pExpr);.  
1c2aa 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
1c2ab 5f 4f 4d 49 54 5f 43 41 53 54 0a 20 20 69 66 28  _OMIT_CAST.  if(
1c2ac 20 6f 70 3d 3d 54 4b 5f 43 41 53 54 20 29 7b 0a   op==TK_CAST ){.
1c2ad 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74      return sqlit
1c2ae 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 26  e3AffinityType(&
1c2af 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20  pExpr->token);. 
1c2b0 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
1c2b1 28 6f 70 3d 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55  (op==TK_AGG_COLU
1c2b2 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 43 4f 4c  MN || op==TK_COL
1c2b3 55 4d 4e 20 7c 7c 20 6f 70 3d 3d 54 4b 5f 52 45  UMN || op==TK_RE
1c2b4 47 49 53 54 45 52 29 20 0a 20 20 20 26 26 20 70  GISTER) .   && p
1c2b5 45 78 70 72 2d 3e 70 54 61 62 21 3d 30 0a 20 20  Expr->pTab!=0.  
1c2b6 29 7b 0a 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b  ){.    /* op==TK
1c2b7 5f 52 45 47 49 53 54 45 52 20 26 26 20 70 45 78  _REGISTER && pEx
1c2b8 70 72 2d 3e 70 54 61 62 21 3d 30 20 68 61 70 70  pr->pTab!=0 happ
1c2b9 65 6e 73 20 77 68 65 6e 20 70 45 78 70 72 20 77  ens when pExpr w
1c2ba 61 73 20 6f 72 69 67 69 6e 61 6c 6c 79 0a 20 20  as originally.  
1c2bb 20 20 2a 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e    ** a TK_COLUMN
1c2bc 20 62 75 74 20 77 61 73 20 70 72 65 76 69 6f 75   but was previou
1c2bd 73 6c 79 20 65 76 61 6c 75 61 74 65 64 20 61 6e  sly evaluated an
1c2be 64 20 63 61 63 68 65 64 20 69 6e 20 61 20 72 65  d cached in a re
1c2bf 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e  gister */.    in
1c2c0 74 20 6a 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f  t j = pExpr->iCo
1c2c1 6c 75 6d 6e 3b 0a 20 20 20 20 69 66 28 20 6a 3c  lumn;.    if( j<
1c2c2 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  0 ) return SQLIT
1c2c3 45 5f 41 46 46 5f 49 4e 54 45 47 45 52 3b 0a 20  E_AFF_INTEGER;. 
1c2c4 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1c2c5 2d 3e 70 54 61 62 20 26 26 20 6a 3c 70 45 78 70  ->pTab && j<pExp
1c2c6 72 2d 3e 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b  r->pTab->nCol );
1c2c7 0a 20 20 20 20 72 65 74 75 72 6e 20 70 45 78 70  .    return pExp
1c2c8 72 2d 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d  r->pTab->aCol[j]
1c2c9 2e 61 66 66 69 6e 69 74 79 3b 0a 20 20 7d 0a 20  .affinity;.  }. 
1c2ca 20 72 65 74 75 72 6e 20 70 45 78 70 72 2d 3e 61   return pExpr->a
1c2cb 66 66 69 6e 69 74 79 3b 0a 7d 0a 0a 2f 2a 0a 2a  ffinity;.}../*.*
1c2cc 2a 20 53 65 74 20 74 68 65 20 63 6f 6c 6c 61 74  * Set the collat
1c2cd 69 6e 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72  ing sequence for
1c2ce 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70   expression pExp
1c2cf 72 20 74 6f 20 62 65 20 74 68 65 20 63 6f 6c 6c  r to be the coll
1c2d0 61 74 69 6e 67 0a 2a 2a 20 73 65 71 75 65 6e 63  ating.** sequenc
1c2d1 65 20 6e 61 6d 65 64 20 62 79 20 70 54 6f 6b 65  e named by pToke
1c2d2 6e 2e 20 20 20 52 65 74 75 72 6e 20 61 20 70 6f  n.   Return a po
1c2d3 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 65 76  inter to the rev
1c2d4 69 73 65 64 20 65 78 70 72 65 73 73 69 6f 6e 2e  ised expression.
1c2d5 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e  .** The collatin
1c2d6 67 20 73 65 71 75 65 6e 63 65 20 69 73 20 6d 61  g sequence is ma
1c2d7 72 6b 65 64 20 61 73 20 22 65 78 70 6c 69 63 69  rked as "explici
1c2d8 74 22 20 75 73 69 6e 67 20 74 68 65 20 45 50 5f  t" using the EP_
1c2d9 45 78 70 43 6f 6c 6c 61 74 65 0a 2a 2a 20 66 6c  ExpCollate.** fl
1c2da 61 67 2e 20 20 41 6e 20 65 78 70 6c 69 63 69 74  ag.  An explicit
1c2db 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65   collating seque
1c2dc 6e 63 65 20 77 69 6c 6c 20 6f 76 65 72 72 69 64  nce will overrid
1c2dd 65 20 69 6d 70 6c 69 63 69 74 0a 2a 2a 20 63 6f  e implicit.** co
1c2de 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
1c2df 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  s..*/.SQLITE_PRI
1c2e0 56 41 54 45 20 45 78 70 72 20 2a 73 71 6c 69 74  VATE Expr *sqlit
1c2e1 65 33 45 78 70 72 53 65 74 43 6f 6c 6c 28 50 61  e3ExprSetColl(Pa
1c2e2 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
1c2e3 72 20 2a 70 45 78 70 72 2c 20 54 6f 6b 65 6e 20  r *pExpr, Token 
1c2e4 2a 70 43 6f 6c 6c 4e 61 6d 65 29 7b 0a 20 20 63  *pCollName){.  c
1c2e5 68 61 72 20 2a 7a 43 6f 6c 6c 20 3d 20 30 3b 20  har *zColl = 0; 
1c2e6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 65             /* De
1c2e7 71 75 6f 74 65 64 20 6e 61 6d 65 20 6f 66 20 63  quoted name of c
1c2e8 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1c2e9 65 20 2a 2f 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  e */.  CollSeq *
1c2ea 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33  pColl;.  sqlite3
1c2eb 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1c2ec 62 3b 0a 20 20 7a 43 6f 6c 6c 20 3d 20 73 71 6c  b;.  zColl = sql
1c2ed 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65  ite3NameFromToke
1c2ee 6e 28 64 62 2c 20 70 43 6f 6c 6c 4e 61 6d 65 29  n(db, pCollName)
1c2ef 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 26 26  ;.  if( pExpr &&
1c2f0 20 7a 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 70 43   zColl ){.    pC
1c2f1 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63  oll = sqlite3Loc
1c2f2 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73  ateCollSeq(pPars
1c2f3 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b 0a 20  e, zColl, -1);. 
1c2f4 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a     if( pColl ){.
1c2f5 20 20 20 20 20 20 70 45 78 70 72 2d 3e 70 43 6f        pExpr->pCo
1c2f6 6c 6c 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20 20  ll = pColl;.    
1c2f7 20 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 7c    pExpr->flags |
1c2f8 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 3b  = EP_ExpCollate;
1c2f9 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c  .    }.  }.  sql
1c2fa 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1c2fb 43 6f 6c 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20  Coll);.  return 
1c2fc 70 45 78 70 72 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  pExpr;.}../*.** 
1c2fd 52 65 74 75 72 6e 20 74 68 65 20 64 65 66 61 75  Return the defau
1c2fe 6c 74 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  lt collation seq
1c2ff 75 65 6e 63 65 20 66 6f 72 20 74 68 65 20 65 78  uence for the ex
1c300 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20  pression pExpr. 
1c301 49 66 0a 2a 2a 20 74 68 65 72 65 20 69 73 20 6e  If.** there is n
1c302 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  o default collat
1c303 69 6f 6e 20 74 79 70 65 2c 20 72 65 74 75 72 6e  ion type, return
1c304 20 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52   0..*/.SQLITE_PR
1c305 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a 73  IVATE CollSeq *s
1c306 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
1c307 71 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  q(Parse *pParse,
1c308 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
1c309 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
1c30a 3d 20 30 3b 0a 20 20 45 78 70 72 20 2a 70 20 3d  = 0;.  Expr *p =
1c30b 20 70 45 78 70 72 3b 0a 20 20 77 68 69 6c 65 28   pExpr;.  while(
1c30c 20 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 6f 70   p ){.    int op
1c30d 3b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 2d  ;.    pColl = p-
1c30e 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 69 66 28 20  >pColl;.    if( 
1c30f 70 43 6f 6c 6c 20 29 20 62 72 65 61 6b 3b 0a 20  pColl ) break;. 
1c310 20 20 20 6f 70 20 3d 20 70 2d 3e 6f 70 3b 0a 20     op = p->op;. 
1c311 20 20 20 69 66 28 20 28 6f 70 3d 3d 54 4b 5f 41     if( (op==TK_A
1c312 47 47 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70 3d  GG_COLUMN || op=
1c313 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c 20 6f 70  =TK_COLUMN || op
1c314 3d 3d 54 4b 5f 52 45 47 49 53 54 45 52 29 20 26  ==TK_REGISTER) &
1c315 26 20 70 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a  & p->pTab!=0 ){.
1c316 20 20 20 20 20 20 2f 2a 20 6f 70 3d 3d 54 4b 5f        /* op==TK_
1c317 52 45 47 49 53 54 45 52 20 26 26 20 70 2d 3e 70  REGISTER && p->p
1c318 54 61 62 21 3d 30 20 68 61 70 70 65 6e 73 20 77  Tab!=0 happens w
1c319 68 65 6e 20 70 45 78 70 72 20 77 61 73 20 6f 72  hen pExpr was or
1c31a 69 67 69 6e 61 6c 6c 79 0a 20 20 20 20 20 20 2a  iginally.      *
1c31b 2a 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e 20 62 75  * a TK_COLUMN bu
1c31c 74 20 77 61 73 20 70 72 65 76 69 6f 75 73 6c 79  t was previously
1c31d 20 65 76 61 6c 75 61 74 65 64 20 61 6e 64 20 63   evaluated and c
1c31e 61 63 68 65 64 20 69 6e 20 61 20 72 65 67 69 73  ached in a regis
1c31f 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 6f 6e  ter */.      con
1c320 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 0a  st char *zColl;.
1c321 20 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 2d        int j = p-
1c322 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
1c323 69 66 28 20 6a 3e 3d 30 20 29 7b 0a 20 20 20 20  if( j>=0 ){.    
1c324 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
1c325 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1c326 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 70 2d        zColl = p-
1c327 3e 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a  >pTab->aCol[j].z
1c328 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 70 43  Coll;.        pC
1c329 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
1c32a 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45 4e 43  dCollSeq(db, ENC
1c32b 28 64 62 29 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 2c  (db), zColl, -1,
1c32c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 70 45 78   0);.        pEx
1c32d 70 72 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c  pr->pColl = pCol
1c32e 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  l;.      }.     
1c32f 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1c330 20 20 69 66 28 20 6f 70 21 3d 54 4b 5f 43 41 53    if( op!=TK_CAS
1c331 54 20 26 26 20 6f 70 21 3d 54 4b 5f 55 50 4c 55  T && op!=TK_UPLU
1c332 53 20 29 7b 0a 20 20 20 20 20 20 62 72 65 61 6b  S ){.      break
1c333 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 20 3d 20  ;.    }.    p = 
1c334 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 7d 0a 20 20  p->pLeft;.  }.  
1c335 69 66 28 20 73 71 6c 69 74 65 33 43 68 65 63 6b  if( sqlite3Check
1c336 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
1c337 70 43 6f 6c 6c 29 20 29 7b 20 0a 20 20 20 20 70  pColl) ){ .    p
1c338 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Coll = 0;.  }.  
1c339 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a  return pColl;.}.
1c33a 0a 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20  ./*.** pExpr is 
1c33b 61 6e 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  an operand of a 
1c33c 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61  comparison opera
1c33d 74 6f 72 2e 20 20 61 66 66 32 20 69 73 20 74 68  tor.  aff2 is th
1c33e 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e 69  e.** type affini
1c33f 74 79 20 6f 66 20 74 68 65 20 6f 74 68 65 72 20  ty of the other 
1c340 6f 70 65 72 61 6e 64 2e 20 20 54 68 69 73 20 72  operand.  This r
1c341 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
1c342 68 65 0a 2a 2a 20 74 79 70 65 20 61 66 66 69 6e  he.** type affin
1c343 69 74 79 20 74 68 61 74 20 73 68 6f 75 6c 64 20  ity that should 
1c344 62 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  be used for the 
1c345 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65 72 61  comparison opera
1c346 74 6f 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  tor..*/.SQLITE_P
1c347 52 49 56 41 54 45 20 63 68 61 72 20 73 71 6c 69  RIVATE char sqli
1c348 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
1c349 74 79 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20  ty(Expr *pExpr, 
1c34a 63 68 61 72 20 61 66 66 32 29 7b 0a 20 20 63 68  char aff2){.  ch
1c34b 61 72 20 61 66 66 31 20 3d 20 73 71 6c 69 74 65  ar aff1 = sqlite
1c34c 33 45 78 70 72 41 66 66 69 6e 69 74 79 28 70 45  3ExprAffinity(pE
1c34d 78 70 72 29 3b 0a 20 20 69 66 28 20 61 66 66 31  xpr);.  if( aff1
1c34e 20 26 26 20 61 66 66 32 20 29 7b 0a 20 20 20 20   && aff2 ){.    
1c34f 2f 2a 20 42 6f 74 68 20 73 69 64 65 73 20 6f 66  /* Both sides of
1c350 20 74 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20   the comparison 
1c351 61 72 65 20 63 6f 6c 75 6d 6e 73 2e 20 49 66 20  are columns. If 
1c352 6f 6e 65 20 68 61 73 20 6e 75 6d 65 72 69 63 0a  one has numeric.
1c353 20 20 20 20 2a 2a 20 61 66 66 69 6e 69 74 79 2c      ** affinity,
1c354 20 75 73 65 20 74 68 61 74 2e 20 4f 74 68 65 72   use that. Other
1c355 77 69 73 65 20 75 73 65 20 6e 6f 20 61 66 66 69  wise use no affi
1c356 6e 69 74 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  nity..    */.   
1c357 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 75   if( sqlite3IsNu
1c358 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 61 66  mericAffinity(af
1c359 66 31 29 20 7c 7c 20 73 71 6c 69 74 65 33 49 73  f1) || sqlite3Is
1c35a 4e 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28  NumericAffinity(
1c35b 61 66 66 32 29 20 29 7b 0a 20 20 20 20 20 20 72  aff2) ){.      r
1c35c 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 41 46 46  eturn SQLITE_AFF
1c35d 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 20 20 7d 65  _NUMERIC;.    }e
1c35e 6c 73 65 7b 0a 20 20 20 20 20 20 72 65 74 75 72  lse{.      retur
1c35f 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  n SQLITE_AFF_NON
1c360 45 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  E;.    }.  }else
1c361 20 69 66 28 20 21 61 66 66 31 20 26 26 20 21 61   if( !aff1 && !a
1c362 66 66 32 20 29 7b 0a 20 20 20 20 2f 2a 20 4e 65  ff2 ){.    /* Ne
1c363 69 74 68 65 72 20 73 69 64 65 20 6f 66 20 74 68  ither side of th
1c364 65 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20  e comparison is 
1c365 61 20 63 6f 6c 75 6d 6e 2e 20 20 43 6f 6d 70 61  a column.  Compa
1c366 72 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 72 65  re the.    ** re
1c367 73 75 6c 74 73 20 64 69 72 65 63 74 6c 79 2e 0a  sults directly..
1c368 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 74 75 72      */.    retur
1c369 6e 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e 4f 4e  n SQLITE_AFF_NON
1c36a 45 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  E;.  }else{.    
1c36b 2f 2a 20 4f 6e 65 20 73 69 64 65 20 69 73 20 61  /* One side is a
1c36c 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 20 6f 74 68   column, the oth
1c36d 65 72 20 69 73 20 6e 6f 74 2e 20 55 73 65 20 74  er is not. Use t
1c36e 68 65 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e  he columns affin
1c36f 69 74 79 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65  ity. */.    asse
1c370 72 74 28 20 61 66 66 31 3d 3d 30 20 7c 7c 20 61  rt( aff1==0 || a
1c371 66 66 32 3d 3d 30 20 29 3b 0a 20 20 20 20 72 65  ff2==0 );.    re
1c372 74 75 72 6e 20 28 61 66 66 31 20 2b 20 61 66 66  turn (aff1 + aff
1c373 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  2);.  }.}../*.**
1c374 20 70 45 78 70 72 20 69 73 20 61 20 63 6f 6d 70   pExpr is a comp
1c375 61 72 69 73 6f 6e 20 6f 70 65 72 61 74 6f 72 2e  arison operator.
1c376 20 20 52 65 74 75 72 6e 20 74 68 65 20 74 79 70    Return the typ
1c377 65 20 61 66 66 69 6e 69 74 79 20 74 68 61 74 20  e affinity that 
1c378 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 61 70 70  should.** be app
1c379 6c 69 65 64 20 74 6f 20 62 6f 74 68 20 6f 70 65  lied to both ope
1c37a 72 61 6e 64 73 20 70 72 69 6f 72 20 74 6f 20 64  rands prior to d
1c37b 6f 69 6e 67 20 74 68 65 20 63 6f 6d 70 61 72 69  oing the compari
1c37c 73 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  son..*/.static c
1c37d 68 61 72 20 63 6f 6d 70 61 72 69 73 6f 6e 41 66  har comparisonAf
1c37e 66 69 6e 69 74 79 28 45 78 70 72 20 2a 70 45 78  finity(Expr *pEx
1c37f 70 72 29 7b 0a 20 20 63 68 61 72 20 61 66 66 3b  pr){.  char aff;
1c380 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
1c381 2d 3e 6f 70 3d 3d 54 4b 5f 45 51 20 7c 7c 20 70  ->op==TK_EQ || p
1c382 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20  Expr->op==TK_IN 
1c383 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  || pExpr->op==TK
1c384 5f 4c 54 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  _LT ||.         
1c385 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 47   pExpr->op==TK_G
1c386 54 20 7c 7c 20 70 45 78 70 72 2d 3e 6f 70 3d 3d  T || pExpr->op==
1c387 54 4b 5f 47 45 20 7c 7c 20 70 45 78 70 72 2d 3e  TK_GE || pExpr->
1c388 6f 70 3d 3d 54 4b 5f 4c 45 20 7c 7c 0a 20 20 20  op==TK_LE ||.   
1c389 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1c38a 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 61 73 73  ==TK_NE );.  ass
1c38b 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c 65 66  ert( pExpr->pLef
1c38c 74 20 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c  t );.  aff = sql
1c38d 69 74 65 33 45 78 70 72 41 66 66 69 6e 69 74 79  ite3ExprAffinity
1c38e 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a  (pExpr->pLeft);.
1c38f 20 20 69 66 28 20 70 45 78 70 72 2d 3e 70 52 69    if( pExpr->pRi
1c390 67 68 74 20 29 7b 0a 20 20 20 20 61 66 66 20 3d  ght ){.    aff =
1c391 20 73 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41   sqlite3CompareA
1c392 66 66 69 6e 69 74 79 28 70 45 78 70 72 2d 3e 70  ffinity(pExpr->p
1c393 52 69 67 68 74 2c 20 61 66 66 29 3b 0a 20 20 7d  Right, aff);.  }
1c394 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70  .  else if( pExp
1c395 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  r->pSelect ){.  
1c396 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 43    aff = sqlite3C
1c397 6f 6d 70 61 72 65 41 66 66 69 6e 69 74 79 28 70  ompareAffinity(p
1c398 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 2d 3e 70  Expr->pSelect->p
1c399 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70  EList->a[0].pExp
1c39a 72 2c 20 61 66 66 29 3b 0a 20 20 7d 0a 20 20 65  r, aff);.  }.  e
1c39b 6c 73 65 20 69 66 28 20 21 61 66 66 20 29 7b 0a  lse if( !aff ){.
1c39c 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45      aff = SQLITE
1c39d 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 7d 0a 20  _AFF_NONE;.  }. 
1c39e 20 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a   return aff;.}..
1c39f 2f 2a 0a 2a 2a 20 70 45 78 70 72 20 69 73 20 61  /*.** pExpr is a
1c3a0 20 63 6f 6d 70 61 72 69 73 6f 6e 20 65 78 70 72   comparison expr
1c3a1 65 73 73 69 6f 6e 2c 20 65 67 2e 20 27 3d 27 2c  ession, eg. '=',
1c3a2 20 27 3c 27 2c 20 49 4e 28 2e 2e 2e 29 20 65 74   '<', IN(...) et
1c3a3 63 2e 0a 2a 2a 20 69 64 78 5f 61 66 66 69 6e 69  c..** idx_affini
1c3a4 74 79 20 69 73 20 74 68 65 20 61 66 66 69 6e 69  ty is the affini
1c3a5 74 79 20 6f 66 20 61 6e 20 69 6e 64 65 78 65 64  ty of an indexed
1c3a6 20 63 6f 6c 75 6d 6e 2e 20 52 65 74 75 72 6e 20   column. Return 
1c3a7 74 72 75 65 0a 2a 2a 20 69 66 20 74 68 65 20 69  true.** if the i
1c3a8 6e 64 65 78 20 77 69 74 68 20 61 66 66 69 6e 69  ndex with affini
1c3a9 74 79 20 69 64 78 5f 61 66 66 69 6e 69 74 79 20  ty idx_affinity 
1c3aa 6d 61 79 20 62 65 20 75 73 65 64 20 74 6f 20 69  may be used to i
1c3ab 6d 70 6c 65 6d 65 6e 74 0a 2a 2a 20 74 68 65 20  mplement.** the 
1c3ac 63 6f 6d 70 61 72 69 73 6f 6e 20 69 6e 20 70 45  comparison in pE
1c3ad 78 70 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  xpr..*/.SQLITE_P
1c3ae 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
1c3af 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 4f  e3IndexAffinityO
1c3b0 6b 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 63  k(Expr *pExpr, c
1c3b1 68 61 72 20 69 64 78 5f 61 66 66 69 6e 69 74 79  har idx_affinity
1c3b2 29 7b 0a 20 20 63 68 61 72 20 61 66 66 20 3d 20  ){.  char aff = 
1c3b3 63 6f 6d 70 61 72 69 73 6f 6e 41 66 66 69 6e 69  comparisonAffini
1c3b4 74 79 28 70 45 78 70 72 29 3b 0a 20 20 73 77 69  ty(pExpr);.  swi
1c3b5 74 63 68 28 20 61 66 66 20 29 7b 0a 20 20 20 20  tch( aff ){.    
1c3b6 63 61 73 65 20 53 51 4c 49 54 45 5f 41 46 46 5f  case SQLITE_AFF_
1c3b7 4e 4f 4e 45 3a 0a 20 20 20 20 20 20 72 65 74 75  NONE:.      retu
1c3b8 72 6e 20 31 3b 0a 20 20 20 20 63 61 73 65 20 53  rn 1;.    case S
1c3b9 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3a 0a  QLITE_AFF_TEXT:.
1c3ba 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 64 78        return idx
1c3bb 5f 61 66 66 69 6e 69 74 79 3d 3d 53 51 4c 49 54  _affinity==SQLIT
1c3bc 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20 20 20 20  E_AFF_TEXT;.    
1c3bd 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20 72  default:.      r
1c3be 65 74 75 72 6e 20 73 71 6c 69 74 65 33 49 73 4e  eturn sqlite3IsN
1c3bf 75 6d 65 72 69 63 41 66 66 69 6e 69 74 79 28 69  umericAffinity(i
1c3c0 64 78 5f 61 66 66 69 6e 69 74 79 29 3b 0a 20 20  dx_affinity);.  
1c3c1 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  }.}../*.** Retur
1c3c2 6e 20 74 68 65 20 50 35 20 76 61 6c 75 65 20 74  n the P5 value t
1c3c3 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75 73  hat should be us
1c3c4 65 64 20 66 6f 72 20 61 20 62 69 6e 61 72 79 20  ed for a binary 
1c3c5 63 6f 6d 70 61 72 69 73 6f 6e 0a 2a 2a 20 6f 70  comparison.** op
1c3c6 63 6f 64 65 20 28 4f 50 5f 45 71 2c 20 4f 50 5f  code (OP_Eq, OP_
1c3c7 47 65 20 65 74 63 2e 29 20 75 73 65 64 20 74 6f  Ge etc.) used to
1c3c8 20 63 6f 6d 70 61 72 65 20 70 45 78 70 72 31 20   compare pExpr1 
1c3c9 61 6e 64 20 70 45 78 70 72 32 2e 0a 2a 2f 0a 73  and pExpr2..*/.s
1c3ca 74 61 74 69 63 20 75 38 20 62 69 6e 61 72 79 43  tatic u8 binaryC
1c3cb 6f 6d 70 61 72 65 50 35 28 45 78 70 72 20 2a 70  ompareP5(Expr *p
1c3cc 45 78 70 72 31 2c 20 45 78 70 72 20 2a 70 45 78  Expr1, Expr *pEx
1c3cd 70 72 32 2c 20 69 6e 74 20 6a 75 6d 70 49 66 4e  pr2, int jumpIfN
1c3ce 75 6c 6c 29 7b 0a 20 20 75 38 20 61 66 66 20 3d  ull){.  u8 aff =
1c3cf 20 28 63 68 61 72 29 73 71 6c 69 74 65 33 45 78   (char)sqlite3Ex
1c3d0 70 72 41 66 66 69 6e 69 74 79 28 70 45 78 70 72  prAffinity(pExpr
1c3d1 32 29 3b 0a 20 20 61 66 66 20 3d 20 73 71 6c 69  2);.  aff = sqli
1c3d2 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
1c3d3 74 79 28 70 45 78 70 72 31 2c 20 61 66 66 29 20  ty(pExpr1, aff) 
1c3d4 7c 20 6a 75 6d 70 49 66 4e 75 6c 6c 3b 0a 20 20  | jumpIfNull;.  
1c3d5 72 65 74 75 72 6e 20 61 66 66 3b 0a 7d 0a 0a 2f  return aff;.}../
1c3d6 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 70 6f  *.** Return a po
1c3d7 69 6e 74 65 72 20 74 6f 20 74 68 65 20 63 6f 6c  inter to the col
1c3d8 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1c3d9 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 75  that should be u
1c3da 73 65 64 20 62 79 0a 2a 2a 20 61 20 62 69 6e 61  sed by.** a bina
1c3db 72 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70  ry comparison op
1c3dc 65 72 61 74 6f 72 20 63 6f 6d 70 61 72 69 6e 67  erator comparing
1c3dd 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68   pLeft and pRigh
1c3de 74 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  t..**.** If the 
1c3df 6c 65 66 74 20 68 61 6e 64 20 65 78 70 72 65 73  left hand expres
1c3e0 73 69 6f 6e 20 68 61 73 20 61 20 63 6f 6c 6c 61  sion has a colla
1c3e1 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20 74 79  ting sequence ty
1c3e2 70 65 2c 20 74 68 65 6e 20 69 74 20 69 73 0a 2a  pe, then it is.*
1c3e3 2a 20 75 73 65 64 2e 20 4f 74 68 65 72 77 69 73  * used. Otherwis
1c3e4 65 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  e the collation 
1c3e5 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68 65  sequence for the
1c3e6 20 72 69 67 68 74 20 68 61 6e 64 20 65 78 70 72   right hand expr
1c3e7 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 75 73 65  ession.** is use
1c3e8 64 2c 20 6f 72 20 74 68 65 20 64 65 66 61 75 6c  d, or the defaul
1c3e9 74 20 28 42 49 4e 41 52 59 29 20 69 66 20 6e 65  t (BINARY) if ne
1c3ea 69 74 68 65 72 20 65 78 70 72 65 73 73 69 6f 6e  ither expression
1c3eb 20 68 61 73 20 61 20 63 6f 6c 6c 61 74 69 6e 67   has a collating
1c3ec 0a 2a 2a 20 74 79 70 65 2e 0a 2a 2a 0a 2a 2a 20  .** type..**.** 
1c3ed 41 72 67 75 6d 65 6e 74 20 70 52 69 67 68 74 20  Argument pRight 
1c3ee 28 62 75 74 20 6e 6f 74 20 70 4c 65 66 74 29 20  (but not pLeft) 
1c3ef 6d 61 79 20 62 65 20 61 20 6e 75 6c 6c 20 70 6f  may be a null po
1c3f0 69 6e 74 65 72 2e 20 49 6e 20 74 68 69 73 20 63  inter. In this c
1c3f1 61 73 65 2c 0a 2a 2a 20 69 74 20 69 73 20 6e 6f  ase,.** it is no
1c3f2 74 20 63 6f 6e 73 69 64 65 72 65 64 2e 0a 2a 2f  t considered..*/
1c3f3 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c3f4 43 6f 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33  CollSeq *sqlite3
1c3f5 42 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c  BinaryCompareCol
1c3f6 6c 53 65 71 28 0a 20 20 50 61 72 73 65 20 2a 70  lSeq(.  Parse *p
1c3f7 50 61 72 73 65 2c 20 0a 20 20 45 78 70 72 20 2a  Parse, .  Expr *
1c3f8 70 4c 65 66 74 2c 20 0a 20 20 45 78 70 72 20 2a  pLeft, .  Expr *
1c3f9 70 52 69 67 68 74 0a 29 7b 0a 20 20 43 6f 6c 6c  pRight.){.  Coll
1c3fa 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 61 73  Seq *pColl;.  as
1c3fb 73 65 72 74 28 20 70 4c 65 66 74 20 29 3b 0a 20  sert( pLeft );. 
1c3fc 20 69 66 28 20 70 4c 65 66 74 2d 3e 66 6c 61 67   if( pLeft->flag
1c3fd 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74  s & EP_ExpCollat
1c3fe 65 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  e ){.    assert(
1c3ff 20 70 4c 65 66 74 2d 3e 70 43 6f 6c 6c 20 29 3b   pLeft->pColl );
1c400 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70 4c 65  .    pColl = pLe
1c401 66 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c  ft->pColl;.  }el
1c402 73 65 20 69 66 28 20 70 52 69 67 68 74 20 26 26  se if( pRight &&
1c403 20 70 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26   pRight->flags &
1c404 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29   EP_ExpCollate )
1c405 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 52  {.    assert( pR
1c406 69 67 68 74 2d 3e 70 43 6f 6c 6c 20 29 3b 0a 20  ight->pColl );. 
1c407 20 20 20 70 43 6f 6c 6c 20 3d 20 70 52 69 67 68     pColl = pRigh
1c408 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 7d 65 6c 73  t->pColl;.  }els
1c409 65 7b 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73  e{.    pColl = s
1c40a 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65  qlite3ExprCollSe
1c40b 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66 74 29  q(pParse, pLeft)
1c40c 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c  ;.    if( !pColl
1c40d 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 20   ){.      pColl 
1c40e 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
1c40f 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 52 69  lSeq(pParse, pRi
1c410 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  ght);.    }.  }.
1c411 20 20 72 65 74 75 72 6e 20 70 43 6f 6c 6c 3b 0a    return pColl;.
1c412 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1c413 65 20 74 68 65 20 6f 70 65 72 61 6e 64 73 20 66  e the operands f
1c414 6f 72 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20  or a comparison 
1c415 6f 70 65 72 61 74 69 6f 6e 2e 20 20 42 65 66 6f  operation.  Befo
1c416 72 65 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  re.** generating
1c417 20 74 68 65 20 63 6f 64 65 20 66 6f 72 20 65 61   the code for ea
1c418 63 68 20 6f 70 65 72 61 6e 64 2c 20 73 65 74 20  ch operand, set 
1c419 74 68 65 20 45 50 5f 41 6e 79 41 66 66 0a 2a 2a  the EP_AnyAff.**
1c41a 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 65 78 70   flag on the exp
1c41b 72 65 73 73 69 6f 6e 20 73 6f 20 74 68 61 74 20  ression so that 
1c41c 69 74 20 77 69 6c 6c 20 62 65 20 61 62 6c 65 20  it will be able 
1c41d 74 6f 20 75 73 65 64 20 61 0a 2a 2a 20 63 61 63  to used a.** cac
1c41e 68 65 64 20 63 6f 6c 75 6d 6e 20 76 61 6c 75 65  hed column value
1c41f 20 74 68 61 74 20 68 61 73 20 70 72 65 76 69 6f   that has previo
1c420 75 73 6c 79 20 75 6e 64 65 72 67 6f 6e 65 20 61  usly undergone a
1c421 6e 0a 2a 2a 20 61 66 66 69 6e 69 74 79 20 63 68  n.** affinity ch
1c422 61 6e 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ange..*/.static 
1c423 76 6f 69 64 20 63 6f 64 65 43 6f 6d 70 61 72 65  void codeCompare
1c424 4f 70 65 72 61 6e 64 73 28 0a 20 20 50 61 72 73  Operands(.  Pars
1c425 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f 2a  e *pParse,    /*
1c426 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f 64   Parsing and cod
1c427 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f 6e  e generating con
1c428 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a  text */.  Expr *
1c429 70 4c 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54  pLeft,      /* T
1c42a 68 65 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20  he left operand 
1c42b 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65 67 4c 65  */.  int *pRegLe
1c42c 66 74 2c 20 20 20 20 2f 2a 20 52 65 67 69 73 74  ft,    /* Regist
1c42d 65 72 20 77 68 65 72 65 20 6c 65 66 74 20 6f 70  er where left op
1c42e 65 72 61 6e 64 20 69 73 20 73 74 6f 72 65 64 20  erand is stored 
1c42f 2a 2f 0a 20 20 69 6e 74 20 2a 70 46 72 65 65 4c  */.  int *pFreeL
1c430 65 66 74 2c 20 20 20 2f 2a 20 46 72 65 65 20 74  eft,   /* Free t
1c431 68 69 73 20 72 65 67 69 73 74 65 72 20 77 68 65  his register whe
1c432 6e 20 64 6f 6e 65 20 2a 2f 0a 20 20 45 78 70 72  n done */.  Expr
1c433 20 2a 70 52 69 67 68 74 2c 20 20 20 20 20 2f 2a   *pRight,     /*
1c434 20 54 68 65 20 72 69 67 68 74 20 6f 70 65 72 61   The right opera
1c435 6e 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 52 65  nd */.  int *pRe
1c436 67 52 69 67 68 74 2c 20 20 20 2f 2a 20 52 65 67  gRight,   /* Reg
1c437 69 73 74 65 72 20 77 68 65 72 65 20 72 69 67 68  ister where righ
1c438 74 20 6f 70 65 72 61 6e 64 20 69 73 20 73 74 6f  t operand is sto
1c439 72 65 64 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 46  red */.  int *pF
1c43a 72 65 65 52 69 67 68 74 20 20 20 2f 2a 20 57 72  reeRight   /* Wr
1c43b 69 74 65 20 74 65 6d 70 20 72 65 67 69 73 74 65  ite temp registe
1c43c 72 20 66 6f 72 20 72 69 67 68 74 20 6f 70 65 72  r for right oper
1c43d 61 6e 64 20 74 68 65 72 65 20 2a 2f 0a 29 7b 0a  and there */.){.
1c43e 20 20 77 68 69 6c 65 28 20 70 4c 65 66 74 2d 3e    while( pLeft->
1c43f 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70  op==TK_UPLUS ) p
1c440 4c 65 66 74 20 3d 20 70 4c 65 66 74 2d 3e 70 4c  Left = pLeft->pL
1c441 65 66 74 3b 0a 20 20 70 4c 65 66 74 2d 3e 66 6c  eft;.  pLeft->fl
1c442 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79 41 66 66  ags |= EP_AnyAff
1c443 3b 0a 20 20 2a 70 52 65 67 4c 65 66 74 20 3d 20  ;.  *pRegLeft = 
1c444 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1c445 65 6d 70 28 70 50 61 72 73 65 2c 20 70 4c 65 66  emp(pParse, pLef
1c446 74 2c 20 70 46 72 65 65 4c 65 66 74 29 3b 0a 20  t, pFreeLeft);. 
1c447 20 77 68 69 6c 65 28 20 70 52 69 67 68 74 2d 3e   while( pRight->
1c448 6f 70 3d 3d 54 4b 5f 55 50 4c 55 53 20 29 20 70  op==TK_UPLUS ) p
1c449 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 2d 3e  Right = pRight->
1c44a 70 4c 65 66 74 3b 0a 20 20 70 52 69 67 68 74 2d  pLeft;.  pRight-
1c44b 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f 41 6e 79  >flags |= EP_Any
1c44c 41 66 66 3b 0a 20 20 2a 70 52 65 67 52 69 67 68  Aff;.  *pRegRigh
1c44d 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  t = sqlite3ExprC
1c44e 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
1c44f 70 52 69 67 68 74 2c 20 70 46 72 65 65 52 69 67  pRight, pFreeRig
1c450 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ht);.}../*.** Ge
1c451 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
1c452 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70 65  a comparison ope
1c453 72 61 74 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63  rator..*/.static
1c454 20 69 6e 74 20 63 6f 64 65 43 6f 6d 70 61 72 65   int codeCompare
1c455 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1c456 65 2c 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72  e,    /* The par
1c457 73 69 6e 67 20 28 61 6e 64 20 63 6f 64 65 20 67  sing (and code g
1c458 65 6e 65 72 61 74 69 6e 67 29 20 63 6f 6e 74 65  enerating) conte
1c459 78 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c  xt */.  Expr *pL
1c45a 65 66 74 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  eft,      /* The
1c45b 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f   left operand */
1c45c 0a 20 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c  .  Expr *pRight,
1c45d 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
1c45e 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 69  t operand */.  i
1c45f 6e 74 20 6f 70 63 6f 64 65 2c 20 20 20 20 20 20  nt opcode,      
1c460 20 2f 2a 20 54 68 65 20 63 6f 6d 70 61 72 69 73   /* The comparis
1c461 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a 20 20 69  on opcode */.  i
1c462 6e 74 20 69 6e 31 2c 20 69 6e 74 20 69 6e 32 2c  nt in1, int in2,
1c463 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
1c464 64 69 6e 67 20 6f 70 65 72 61 6e 64 73 20 2a 2f  ding operands */
1c465 0a 20 20 69 6e 74 20 64 65 73 74 2c 20 20 20 20  .  int dest,    
1c466 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72       /* Jump her
1c467 65 20 69 66 20 74 72 75 65 2e 20 20 2a 2f 0a 20  e if true.  */. 
1c468 20 69 6e 74 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   int jumpIfNull 
1c469 20 20 20 2f 2a 20 49 66 20 74 72 75 65 2c 20 6a     /* If true, j
1c46a 75 6d 70 20 69 66 20 65 69 74 68 65 72 20 6f 70  ump if either op
1c46b 65 72 61 6e 64 20 69 73 20 4e 55 4c 4c 20 2a 2f  erand is NULL */
1c46c 0a 29 7b 0a 20 20 69 6e 74 20 70 35 3b 0a 20 20  .){.  int p5;.  
1c46d 69 6e 74 20 61 64 64 72 3b 0a 20 20 43 6f 6c 6c  int addr;.  Coll
1c46e 53 65 71 20 2a 70 34 3b 0a 0a 20 20 70 34 20 3d  Seq *p4;..  p4 =
1c46f 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43 6f   sqlite3BinaryCo
1c470 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50 61  mpareCollSeq(pPa
1c471 72 73 65 2c 20 70 4c 65 66 74 2c 20 70 52 69 67  rse, pLeft, pRig
1c472 68 74 29 3b 0a 20 20 70 35 20 3d 20 62 69 6e 61  ht);.  p5 = bina
1c473 72 79 43 6f 6d 70 61 72 65 50 35 28 70 4c 65 66  ryCompareP5(pLef
1c474 74 2c 20 70 52 69 67 68 74 2c 20 6a 75 6d 70 49  t, pRight, jumpI
1c475 66 4e 75 6c 6c 29 3b 0a 20 20 61 64 64 72 20 3d  fNull);.  addr =
1c476 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1c477 70 34 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  p4(pParse->pVdbe
1c478 2c 20 6f 70 63 6f 64 65 2c 20 69 6e 32 2c 20 64  , opcode, in2, d
1c479 65 73 74 2c 20 69 6e 31 2c 0a 20 20 20 20 20 20  est, in1,.      
1c47a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1c47b 20 20 20 20 20 28 76 6f 69 64 2a 29 70 34 2c 20       (void*)p4, 
1c47c 50 34 5f 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 73  P4_COLLSEQ);.  s
1c47d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1c47e 50 35 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  P5(pParse->pVdbe
1c47f 2c 20 70 35 29 3b 0a 20 20 69 66 28 20 28 70 35  , p5);.  if( (p5
1c480 20 26 20 53 51 4c 49 54 45 5f 41 46 46 5f 4d 41   & SQLITE_AFF_MA
1c481 53 4b 29 21 3d 53 51 4c 49 54 45 5f 41 46 46 5f  SK)!=SQLITE_AFF_
1c482 4e 4f 4e 45 20 29 7b 0a 20 20 20 20 73 71 6c 69  NONE ){.    sqli
1c483 74 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69  te3ExprCacheAffi
1c484 6e 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73  nityChange(pPars
1c485 65 2c 20 69 6e 31 2c 20 31 29 3b 0a 20 20 20 20  e, in1, 1);.    
1c486 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
1c487 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
1c488 50 61 72 73 65 2c 20 69 6e 32 2c 20 31 29 3b 0a  Parse, in2, 1);.
1c489 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 61 64 64    }.  return add
1c48a 72 3b 0a 7d 0a 0a 23 69 66 20 53 51 4c 49 54 45  r;.}..#if SQLITE
1c48b 5f 4d 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e  _MAX_EXPR_DEPTH>
1c48c 30 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 68  0./*.** Check th
1c48d 61 74 20 61 72 67 75 6d 65 6e 74 20 6e 48 65 69  at argument nHei
1c48e 67 68 74 20 69 73 20 6c 65 73 73 20 74 68 61 6e  ght is less than
1c48f 20 6f 72 20 65 71 75 61 6c 20 74 6f 20 74 68 65   or equal to the
1c490 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 65 78 70 72   maximum.** expr
1c491 65 73 73 69 6f 6e 20 64 65 70 74 68 20 61 6c 6c  ession depth all
1c492 6f 77 65 64 2e 20 49 66 20 69 74 20 69 73 20 6e  owed. If it is n
1c493 6f 74 2c 20 6c 65 61 76 65 20 61 6e 20 65 72 72  ot, leave an err
1c494 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 0a 2a 2a  or message in.**
1c495 20 70 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49   pParse..*/.SQLI
1c496 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1c497 71 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48  qlite3ExprCheckH
1c498 65 69 67 68 74 28 50 61 72 73 65 20 2a 70 50 61  eight(Parse *pPa
1c499 72 73 65 2c 20 69 6e 74 20 6e 48 65 69 67 68 74  rse, int nHeight
1c49a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ){.  int rc = SQ
1c49b 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6d  LITE_OK;.  int m
1c49c 78 48 65 69 67 68 74 20 3d 20 70 50 61 72 73 65  xHeight = pParse
1c49d 2d 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ->db->aLimit[SQL
1c49e 49 54 45 5f 4c 49 4d 49 54 5f 45 58 50 52 5f 44  ITE_LIMIT_EXPR_D
1c49f 45 50 54 48 5d 3b 0a 20 20 69 66 28 20 6e 48 65  EPTH];.  if( nHe
1c4a0 69 67 68 74 3e 6d 78 48 65 69 67 68 74 20 29 7b  ight>mxHeight ){
1c4a1 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1c4a2 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20  rMsg(pParse, .  
1c4a3 20 20 20 20 20 22 45 78 70 72 65 73 73 69 6f 6e       "Expression
1c4a4 20 74 72 65 65 20 69 73 20 74 6f 6f 20 6c 61 72   tree is too lar
1c4a5 67 65 20 28 6d 61 78 69 6d 75 6d 20 64 65 70 74  ge (maximum dept
1c4a6 68 20 25 64 29 22 2c 20 6d 78 48 65 69 67 68 74  h %d)", mxHeight
1c4a7 0a 20 20 20 20 29 3b 0a 20 20 20 20 72 63 20 3d  .    );.    rc =
1c4a8 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
1c4a9 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
1c4aa 7d 0a 0a 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  }../* The follow
1c4ab 69 6e 67 20 74 68 72 65 65 20 66 75 6e 63 74 69  ing three functi
1c4ac 6f 6e 73 2c 20 68 65 69 67 68 74 4f 66 45 78 70  ons, heightOfExp
1c4ad 72 28 29 2c 20 68 65 69 67 68 74 4f 66 45 78 70  r(), heightOfExp
1c4ae 72 4c 69 73 74 28 29 0a 2a 2a 20 61 6e 64 20 68  rList().** and h
1c4af 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 29 2c  eightOfSelect(),
1c4b0 20 61 72 65 20 75 73 65 64 20 74 6f 20 64 65 74   are used to det
1c4b1 65 72 6d 69 6e 65 20 74 68 65 20 6d 61 78 69 6d  ermine the maxim
1c4b2 75 6d 20 68 65 69 67 68 74 0a 2a 2a 20 6f 66 20  um height.** of 
1c4b3 61 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74  any expression t
1c4b4 72 65 65 20 72 65 66 65 72 65 6e 63 65 64 20 62  ree referenced b
1c4b5 79 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20  y the structure 
1c4b6 70 61 73 73 65 64 20 61 73 20 74 68 65 0a 2a 2a  passed as the.**
1c4b7 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 2e   first argument.
1c4b8 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 69 73 20 6d  .**.** If this m
1c4b9 61 78 69 6d 75 6d 20 68 65 69 67 68 74 20 69 73  aximum height is
1c4ba 20 67 72 65 61 74 65 72 20 74 68 61 6e 20 74 68   greater than th
1c4bb 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 20  e current value 
1c4bc 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79  pointed.** to by
1c4bd 20 70 6e 48 65 69 67 68 74 2c 20 74 68 65 20 73   pnHeight, the s
1c4be 65 63 6f 6e 64 20 70 61 72 61 6d 65 74 65 72 2c  econd parameter,
1c4bf 20 74 68 65 6e 20 73 65 74 20 2a 70 6e 48 65 69   then set *pnHei
1c4c0 67 68 74 20 74 6f 20 74 68 61 74 0a 2a 2a 20 76  ght to that.** v
1c4c1 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  alue..*/.static 
1c4c2 76 6f 69 64 20 68 65 69 67 68 74 4f 66 45 78 70  void heightOfExp
1c4c3 72 28 45 78 70 72 20 2a 70 2c 20 69 6e 74 20 2a  r(Expr *p, int *
1c4c4 70 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28  pnHeight){.  if(
1c4c5 20 70 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d   p ){.    if( p-
1c4c6 3e 6e 48 65 69 67 68 74 3e 2a 70 6e 48 65 69 67  >nHeight>*pnHeig
1c4c7 68 74 20 29 7b 0a 20 20 20 20 20 20 2a 70 6e 48  ht ){.      *pnH
1c4c8 65 69 67 68 74 20 3d 20 70 2d 3e 6e 48 65 69 67  eight = p->nHeig
1c4c9 68 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  ht;.    }.  }.}.
1c4ca 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 69 67  static void heig
1c4cb 68 74 4f 66 45 78 70 72 4c 69 73 74 28 45 78 70  htOfExprList(Exp
1c4cc 72 4c 69 73 74 20 2a 70 2c 20 69 6e 74 20 2a 70  rList *p, int *p
1c4cd 6e 48 65 69 67 68 74 29 7b 0a 20 20 69 66 28 20  nHeight){.  if( 
1c4ce 70 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  p ){.    int i;.
1c4cf 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1c4d0 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
1c4d1 20 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70       heightOfExp
1c4d2 72 28 70 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c  r(p->a[i].pExpr,
1c4d3 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
1c4d4 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76  }.  }.}.static v
1c4d5 6f 69 64 20 68 65 69 67 68 74 4f 66 53 65 6c 65  oid heightOfSele
1c4d6 63 74 28 53 65 6c 65 63 74 20 2a 70 2c 20 69 6e  ct(Select *p, in
1c4d7 74 20 2a 70 6e 48 65 69 67 68 74 29 7b 0a 20 20  t *pnHeight){.  
1c4d8 69 66 28 20 70 20 29 7b 0a 20 20 20 20 68 65 69  if( p ){.    hei
1c4d9 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 57 68  ghtOfExpr(p->pWh
1c4da 65 72 65 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a  ere, pnHeight);.
1c4db 20 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72      heightOfExpr
1c4dc 28 70 2d 3e 70 48 61 76 69 6e 67 2c 20 70 6e 48  (p->pHaving, pnH
1c4dd 65 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67  eight);.    heig
1c4de 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 69 6d  htOfExpr(p->pLim
1c4df 69 74 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  it, pnHeight);. 
1c4e0 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 28     heightOfExpr(
1c4e1 70 2d 3e 70 4f 66 66 73 65 74 2c 20 70 6e 48 65  p->pOffset, pnHe
1c4e2 69 67 68 74 29 3b 0a 20 20 20 20 68 65 69 67 68  ight);.    heigh
1c4e3 74 4f 66 45 78 70 72 4c 69 73 74 28 70 2d 3e 70  tOfExprList(p->p
1c4e4 45 4c 69 73 74 2c 20 70 6e 48 65 69 67 68 74 29  EList, pnHeight)
1c4e5 3b 0a 20 20 20 20 68 65 69 67 68 74 4f 66 45 78  ;.    heightOfEx
1c4e6 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70  prList(p->pGroup
1c4e7 42 79 2c 20 70 6e 48 65 69 67 68 74 29 3b 0a 20  By, pnHeight);. 
1c4e8 20 20 20 68 65 69 67 68 74 4f 66 45 78 70 72 4c     heightOfExprL
1c4e9 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79 2c  ist(p->pOrderBy,
1c4ea 20 70 6e 48 65 69 67 68 74 29 3b 0a 20 20 20 20   pnHeight);.    
1c4eb 68 65 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70  heightOfSelect(p
1c4ec 2d 3e 70 50 72 69 6f 72 2c 20 70 6e 48 65 69 67  ->pPrior, pnHeig
1c4ed 68 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  ht);.  }.}../*.*
1c4ee 2a 20 53 65 74 20 74 68 65 20 45 78 70 72 2e 6e  * Set the Expr.n
1c4ef 48 65 69 67 68 74 20 76 61 72 69 61 62 6c 65 20  Height variable 
1c4f0 69 6e 20 74 68 65 20 73 74 72 75 63 74 75 72 65  in the structure
1c4f1 20 70 61 73 73 65 64 20 61 73 20 61 6e 20 0a 2a   passed as an .*
1c4f2 2a 20 61 72 67 75 6d 65 6e 74 2e 20 41 6e 20 65  * argument. An e
1c4f3 78 70 72 65 73 73 69 6f 6e 20 77 69 74 68 20 6e  xpression with n
1c4f4 6f 20 63 68 69 6c 64 72 65 6e 2c 20 45 78 70 72  o children, Expr
1c4f5 2e 70 4c 69 73 74 20 6f 72 20 0a 2a 2a 20 45 78  .pList or .** Ex
1c4f6 70 72 2e 70 53 65 6c 65 63 74 20 6d 65 6d 62 65  pr.pSelect membe
1c4f7 72 20 68 61 73 20 61 20 68 65 69 67 68 74 20 6f  r has a height o
1c4f8 66 20 31 2e 20 41 6e 79 20 6f 74 68 65 72 20 65  f 1. Any other e
1c4f9 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 68 61 73  xpression.** has
1c4fa 20 61 20 68 65 69 67 68 74 20 65 71 75 61 6c 20   a height equal 
1c4fb 74 6f 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 68  to the maximum h
1c4fc 65 69 67 68 74 20 6f 66 20 61 6e 79 20 6f 74 68  eight of any oth
1c4fd 65 72 20 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65  er .** reference
1c4fe 64 20 45 78 70 72 20 70 6c 75 73 20 6f 6e 65 2e  d Expr plus one.
1c4ff 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1c500 65 78 70 72 53 65 74 48 65 69 67 68 74 28 45 78  exprSetHeight(Ex
1c501 70 72 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48  pr *p){.  int nH
1c502 65 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69  eight = 0;.  hei
1c503 67 68 74 4f 66 45 78 70 72 28 70 2d 3e 70 4c 65  ghtOfExpr(p->pLe
1c504 66 74 2c 20 26 6e 48 65 69 67 68 74 29 3b 0a 20  ft, &nHeight);. 
1c505 20 68 65 69 67 68 74 4f 66 45 78 70 72 28 70 2d   heightOfExpr(p-
1c506 3e 70 52 69 67 68 74 2c 20 26 6e 48 65 69 67 68  >pRight, &nHeigh
1c507 74 29 3b 0a 20 20 68 65 69 67 68 74 4f 66 45 78  t);.  heightOfEx
1c508 70 72 4c 69 73 74 28 70 2d 3e 70 4c 69 73 74 2c  prList(p->pList,
1c509 20 26 6e 48 65 69 67 68 74 29 3b 0a 20 20 68 65   &nHeight);.  he
1c50a 69 67 68 74 4f 66 53 65 6c 65 63 74 28 70 2d 3e  ightOfSelect(p->
1c50b 70 53 65 6c 65 63 74 2c 20 26 6e 48 65 69 67 68  pSelect, &nHeigh
1c50c 74 29 3b 0a 20 20 70 2d 3e 6e 48 65 69 67 68 74  t);.  p->nHeight
1c50d 20 3d 20 6e 48 65 69 67 68 74 20 2b 20 31 3b 0a   = nHeight + 1;.
1c50e 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
1c50f 20 45 78 70 72 2e 6e 48 65 69 67 68 74 20 76 61   Expr.nHeight va
1c510 72 69 61 62 6c 65 20 75 73 69 6e 67 20 74 68 65  riable using the
1c511 20 65 78 70 72 53 65 74 48 65 69 67 68 74 28 29   exprSetHeight()
1c512 20 66 75 6e 63 74 69 6f 6e 2e 20 49 66 0a 2a 2a   function. If.**
1c513 20 74 68 65 20 68 65 69 67 68 74 20 69 73 20 67   the height is g
1c514 72 65 61 74 65 72 20 74 68 61 6e 20 74 68 65 20  reater than the 
1c515 6d 61 78 69 6d 75 6d 20 61 6c 6c 6f 77 65 64 20  maximum allowed 
1c516 65 78 70 72 65 73 73 69 6f 6e 20 64 65 70 74 68  expression depth
1c517 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72  ,.** leave an er
1c518 72 6f 72 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a  ror in pParse..*
1c519 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c51a 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70   void sqlite3Exp
1c51b 72 53 65 74 48 65 69 67 68 74 28 50 61 72 73 65  rSetHeight(Parse
1c51c 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
1c51d 70 29 7b 0a 20 20 65 78 70 72 53 65 74 48 65 69  p){.  exprSetHei
1c51e 67 68 74 28 70 29 3b 0a 20 20 73 71 6c 69 74 65  ght(p);.  sqlite
1c51f 33 45 78 70 72 43 68 65 63 6b 48 65 69 67 68 74  3ExprCheckHeight
1c520 28 70 50 61 72 73 65 2c 20 70 2d 3e 6e 48 65 69  (pParse, p->nHei
1c521 67 68 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  ght);.}../*.** R
1c522 65 74 75 72 6e 20 74 68 65 20 6d 61 78 69 6d 75  eturn the maximu
1c523 6d 20 68 65 69 67 68 74 20 6f 66 20 61 6e 79 20  m height of any 
1c524 65 78 70 72 65 73 73 69 6f 6e 20 74 72 65 65 20  expression tree 
1c525 72 65 66 65 72 65 6e 63 65 64 0a 2a 2a 20 62 79  referenced.** by
1c526 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74   the select stat
1c527 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73 20  ement passed as 
1c528 61 6e 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a  an argument..*/.
1c529 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1c52a 6e 74 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  nt sqlite3Select
1c52b 45 78 70 72 48 65 69 67 68 74 28 53 65 6c 65 63  ExprHeight(Selec
1c52c 74 20 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 48 65  t *p){.  int nHe
1c52d 69 67 68 74 20 3d 20 30 3b 0a 20 20 68 65 69 67  ight = 0;.  heig
1c52e 68 74 4f 66 53 65 6c 65 63 74 28 70 2c 20 26 6e  htOfSelect(p, &n
1c52f 48 65 69 67 68 74 29 3b 0a 20 20 72 65 74 75 72  Height);.  retur
1c530 6e 20 6e 48 65 69 67 68 74 3b 0a 7d 0a 23 65 6c  n nHeight;.}.#el
1c531 73 65 0a 20 20 23 64 65 66 69 6e 65 20 65 78 70  se.  #define exp
1c532 72 53 65 74 48 65 69 67 68 74 28 79 29 0a 23 65  rSetHeight(y).#e
1c533 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4d  ndif /* SQLITE_M
1c534 41 58 5f 45 58 50 52 5f 44 45 50 54 48 3e 30 20  AX_EXPR_DEPTH>0 
1c535 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72  */../*.** Constr
1c536 75 63 74 20 61 20 6e 65 77 20 65 78 70 72 65 73  uct a new expres
1c537 73 69 6f 6e 20 6e 6f 64 65 20 61 6e 64 20 72 65  sion node and re
1c538 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74  turn a pointer t
1c539 6f 20 69 74 2e 20 20 4d 65 6d 6f 72 79 0a 2a 2a  o it.  Memory.**
1c53a 20 66 6f 72 20 74 68 69 73 20 6e 6f 64 65 20 69   for this node i
1c53b 73 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20  s obtained from 
1c53c 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29  sqlite3_malloc()
1c53d 2e 20 20 54 68 65 20 63 61 6c 6c 69 6e 67 20 66  .  The calling f
1c53e 75 6e 63 74 69 6f 6e 0a 2a 2a 20 69 73 20 72 65  unction.** is re
1c53f 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72 20 6d 61  sponsible for ma
1c540 6b 69 6e 67 20 73 75 72 65 20 74 68 65 20 6e 6f  king sure the no
1c541 64 65 20 65 76 65 6e 74 75 61 6c 6c 79 20 67 65  de eventually ge
1c542 74 73 20 66 72 65 65 64 2e 0a 2a 2f 0a 53 51 4c  ts freed..*/.SQL
1c543 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1c544 20 2a 73 71 6c 69 74 65 33 45 78 70 72 28 0a 20   *sqlite3Expr(. 
1c545 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
1c546 20 20 20 20 20 20 20 20 20 2f 2a 20 48 61 6e 64           /* Hand
1c547 6c 65 20 66 6f 72 20 73 71 6c 69 74 65 33 44 62  le for sqlite3Db
1c548 4d 61 6c 6c 6f 63 5a 65 72 6f 28 29 20 28 6d 61  MallocZero() (ma
1c549 79 20 62 65 20 6e 75 6c 6c 29 20 2a 2f 0a 20 20  y be null) */.  
1c54a 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20 20  int op,         
1c54b 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 65          /* Expre
1c54c 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a 2f 0a  ssion opcode */.
1c54d 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20 20    Expr *pLeft,  
1c54e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 66            /* Lef
1c54f 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 45  t operand */.  E
1c550 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20 20 20  xpr *pRight,    
1c551 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68 74 20         /* Right 
1c552 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63 6f 6e  operand */.  con
1c553 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  st Token *pToken
1c554 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74       /* Argument
1c555 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20 45   token */.){.  E
1c556 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20 70 4e 65  xpr *pNew;.  pNe
1c557 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  w = sqlite3DbMal
1c558 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
1c559 6f 66 28 45 78 70 72 29 29 3b 0a 20 20 69 66 28  of(Expr));.  if(
1c55a 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
1c55b 2f 2a 20 57 68 65 6e 20 6d 61 6c 6c 6f 63 20 66  /* When malloc f
1c55c 61 69 6c 73 2c 20 64 65 6c 65 74 65 20 70 4c 65  ails, delete pLe
1c55d 66 74 20 61 6e 64 20 70 52 69 67 68 74 2e 20 45  ft and pRight. E
1c55e 78 70 72 65 73 73 69 6f 6e 73 20 70 61 73 73 65  xpressions passe
1c55f 64 20 74 6f 20 0a 20 20 20 20 2a 2a 20 74 68 69  d to .    ** thi
1c560 73 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73 74 20  s function must 
1c561 61 6c 77 61 79 73 20 62 65 20 61 6c 6c 6f 63 61  always be alloca
1c562 74 65 64 20 77 69 74 68 20 73 71 6c 69 74 65 33  ted with sqlite3
1c563 45 78 70 72 28 29 20 66 6f 72 20 74 68 69 73 20  Expr() for this 
1c564 0a 20 20 20 20 2a 2a 20 72 65 61 73 6f 6e 2e 20  .    ** reason. 
1c565 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69  .    */.    sqli
1c566 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1c567 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20 73 71  , pLeft);.    sq
1c568 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1c569 64 62 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 20  db, pRight);.   
1c56a 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1c56b 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 6f 70 3b 0a   pNew->op = op;.
1c56c 20 20 70 4e 65 77 2d 3e 70 4c 65 66 74 20 3d 20    pNew->pLeft = 
1c56d 70 4c 65 66 74 3b 0a 20 20 70 4e 65 77 2d 3e 70  pLeft;.  pNew->p
1c56e 52 69 67 68 74 20 3d 20 70 52 69 67 68 74 3b 0a  Right = pRight;.
1c56f 20 20 70 4e 65 77 2d 3e 69 41 67 67 20 3d 20 2d    pNew->iAgg = -
1c570 31 3b 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e 2e  1;.  pNew->span.
1c571 7a 20 3d 20 28 75 38 2a 29 22 22 3b 0a 20 20 69  z = (u8*)"";.  i
1c572 66 28 20 70 54 6f 6b 65 6e 20 29 7b 0a 20 20 20  f( pToken ){.   
1c573 20 61 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d   assert( pToken-
1c574 3e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70  >dyn==0 );.    p
1c575 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65 77  New->span = pNew
1c576 2d 3e 74 6f 6b 65 6e 20 3d 20 2a 70 54 6f 6b 65  ->token = *pToke
1c577 6e 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  n;.  }else if( p
1c578 4c 65 66 74 20 29 7b 0a 20 20 20 20 69 66 28 20  Left ){.    if( 
1c579 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20  pRight ){.      
1c57a 69 66 28 20 70 52 69 67 68 74 2d 3e 73 70 61 6e  if( pRight->span
1c57b 2e 64 79 6e 3d 3d 30 20 26 26 20 70 4c 65 66 74  .dyn==0 && pLeft
1c57c 2d 3e 73 70 61 6e 2e 64 79 6e 3d 3d 30 20 29 7b  ->span.dyn==0 ){
1c57d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1c57e 45 78 70 72 53 70 61 6e 28 70 4e 65 77 2c 20 26  ExprSpan(pNew, &
1c57f 70 4c 65 66 74 2d 3e 73 70 61 6e 2c 20 26 70 52  pLeft->span, &pR
1c580 69 67 68 74 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  ight->span);.   
1c581 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
1c582 52 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45  Right->flags & E
1c583 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a  P_ExpCollate ){.
1c584 20 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c          pNew->fl
1c585 61 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c  ags |= EP_ExpCol
1c586 6c 61 74 65 3b 0a 20 20 20 20 20 20 20 20 70 4e  late;.        pN
1c587 65 77 2d 3e 70 43 6f 6c 6c 20 3d 20 70 52 69 67  ew->pColl = pRig
1c588 68 74 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  ht->pColl;.     
1c589 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
1c58a 20 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 26 20   pLeft->flags & 
1c58b 45 50 5f 45 78 70 43 6f 6c 6c 61 74 65 20 29 7b  EP_ExpCollate ){
1c58c 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 66 6c 61  .      pNew->fla
1c58d 67 73 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c  gs |= EP_ExpColl
1c58e 61 74 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d  ate;.      pNew-
1c58f 3e 70 43 6f 6c 6c 20 3d 20 70 4c 65 66 74 2d 3e  >pColl = pLeft->
1c590 70 43 6f 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 7d  pColl;.    }.  }
1c591 0a 0a 20 20 65 78 70 72 53 65 74 48 65 69 67 68  ..  exprSetHeigh
1c592 74 28 70 4e 65 77 29 3b 0a 20 20 72 65 74 75 72  t(pNew);.  retur
1c593 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  n pNew;.}../*.**
1c594 20 57 6f 72 6b 73 20 6c 69 6b 65 20 73 71 6c 69   Works like sqli
1c595 74 65 33 45 78 70 72 28 29 20 65 78 63 65 70 74  te3Expr() except
1c596 20 74 68 61 74 20 69 74 20 74 61 6b 65 73 20 61   that it takes a
1c597 6e 20 65 78 74 72 61 20 50 61 72 73 65 2a 0a 2a  n extra Parse*.*
1c598 2a 20 61 72 67 75 6d 65 6e 74 20 61 6e 64 20 6e  * argument and n
1c599 6f 74 69 66 69 65 73 20 74 68 65 20 61 73 73 6f  otifies the asso
1c59a 63 69 61 74 65 64 20 63 6f 6e 6e 65 63 74 69 6f  ciated connectio
1c59b 6e 20 6f 62 6a 65 63 74 20 69 66 20 6d 61 6c 6c  n object if mall
1c59c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f 0a 53 51 4c  oc fails..*/.SQL
1c59d 49 54 45 5f 50 52 49 56 41 54 45 20 45 78 70 72  ITE_PRIVATE Expr
1c59e 20 2a 73 71 6c 69 74 65 33 50 45 78 70 72 28 0a   *sqlite3PExpr(.
1c59f 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
1c5a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72            /* Par
1c5a1 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1c5a2 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20    int op,       
1c5a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70            /* Exp
1c5a4 72 65 73 73 69 6f 6e 20 6f 70 63 6f 64 65 20 2a  ression opcode *
1c5a5 2f 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c  /.  Expr *pLeft,
1c5a6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
1c5a7 65 66 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20  eft operand */. 
1c5a8 20 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 20   Expr *pRight,  
1c5a9 20 20 20 20 20 20 20 20 20 2f 2a 20 52 69 67 68           /* Righ
1c5aa 74 20 6f 70 65 72 61 6e 64 20 2a 2f 0a 20 20 63  t operand */.  c
1c5ab 6f 6e 73 74 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b  onst Token *pTok
1c5ac 65 6e 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65  en     /* Argume
1c5ad 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20  nt token */.){. 
1c5ae 20 45 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74   Expr *p = sqlit
1c5af 65 33 45 78 70 72 28 70 50 61 72 73 65 2d 3e 64  e3Expr(pParse->d
1c5b0 62 2c 20 6f 70 2c 20 70 4c 65 66 74 2c 20 70 52  b, op, pLeft, pR
1c5b1 69 67 68 74 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  ight, pToken);. 
1c5b2 20 69 66 28 20 70 20 29 7b 0a 20 20 20 20 73 71   if( p ){.    sq
1c5b3 6c 69 74 65 33 45 78 70 72 43 68 65 63 6b 48 65  lite3ExprCheckHe
1c5b4 69 67 68 74 28 70 50 61 72 73 65 2c 20 70 2d 3e  ight(pParse, p->
1c5b5 6e 48 65 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20  nHeight);.  }.  
1c5b6 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
1c5b7 2a 2a 20 57 68 65 6e 20 64 6f 69 6e 67 20 61 20  ** When doing a 
1c5b8 6e 65 73 74 65 64 20 70 61 72 73 65 2c 20 79 6f  nested parse, yo
1c5b9 75 20 63 61 6e 20 69 6e 63 6c 75 64 65 20 74 65  u can include te
1c5ba 72 6d 73 20 69 6e 20 61 6e 20 65 78 70 72 65 73  rms in an expres
1c5bb 73 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 6c 6f 6f  sion.** that loo
1c5bc 6b 20 6c 69 6b 65 20 74 68 69 73 3a 20 20 20 23  k like this:   #
1c5bd 31 20 23 32 20 2e 2e 2e 20 20 54 68 65 73 65 20  1 #2 ...  These 
1c5be 74 65 72 6d 73 20 72 65 66 65 72 20 74 6f 20 72  terms refer to r
1c5bf 65 67 69 73 74 65 72 73 0a 2a 2a 20 69 6e 20 74  egisters.** in t
1c5c0 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
1c5c1 6e 65 2e 20 20 23 4e 20 69 73 20 74 68 65 20 4e  ne.  #N is the N
1c5c2 2d 74 68 20 72 65 67 69 73 74 65 72 2e 0a 2a 2a  -th register..**
1c5c3 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
1c5c4 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
1c5c5 65 20 70 61 72 73 65 72 20 74 6f 20 64 65 61 6c  e parser to deal
1c5c6 20 77 69 74 68 20 6f 6e 20 6f 66 20 74 68 6f 73   with on of thos
1c5c7 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 49 74 20 69  e terms..** It i
1c5c8 6d 6d 65 64 69 61 74 65 6c 79 20 67 65 6e 65 72  mmediately gener
1c5c9 61 74 65 73 20 63 6f 64 65 20 74 6f 20 73 74 6f  ates code to sto
1c5ca 72 65 20 74 68 65 20 76 61 6c 75 65 20 69 6e 20  re the value in 
1c5cb 61 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f  a memory locatio
1c5cc 6e 2e 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e  n..** The return
1c5cd 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
1c5ce 74 68 61 74 20 77 69 6c 6c 20 63 6f 64 65 20 74  that will code t
1c5cf 6f 20 65 78 74 72 61 63 74 20 74 68 65 20 76 61  o extract the va
1c5d0 6c 75 65 20 66 72 6f 6d 0a 2a 2a 20 74 68 61 74  lue from.** that
1c5d1 20 6d 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e   memory location
1c5d2 20 61 73 20 6e 65 65 64 65 64 2e 0a 2a 2f 0a 53   as needed..*/.S
1c5d3 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1c5d4 70 72 20 2a 73 71 6c 69 74 65 33 52 65 67 69 73  pr *sqlite3Regis
1c5d5 74 65 72 45 78 70 72 28 50 61 72 73 65 20 2a 70  terExpr(Parse *p
1c5d6 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70 54  Parse, Token *pT
1c5d7 6f 6b 65 6e 29 7b 0a 20 20 56 64 62 65 20 2a 76  oken){.  Vdbe *v
1c5d8 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
1c5d9 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20 69  ;.  Expr *p;.  i
1c5da 66 28 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  f( pParse->neste
1c5db 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69  d==0 ){.    sqli
1c5dc 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1c5dd 73 65 2c 20 22 6e 65 61 72 20 5c 22 25 54 5c 22  se, "near \"%T\"
1c5de 3a 20 73 79 6e 74 61 78 20 65 72 72 6f 72 22 2c  : syntax error",
1c5df 20 70 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 72 65   pToken);.    re
1c5e0 74 75 72 6e 20 73 71 6c 69 74 65 33 50 45 78 70  turn sqlite3PExp
1c5e1 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 55 4c  r(pParse, TK_NUL
1c5e2 4c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 7d  L, 0, 0, 0);.  }
1c5e3 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
1c5e4 74 75 72 6e 20 30 3b 0a 20 20 70 20 3d 20 73 71  turn 0;.  p = sq
1c5e5 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
1c5e6 65 2c 20 54 4b 5f 52 45 47 49 53 54 45 52 2c 20  e, TK_REGISTER, 
1c5e7 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29 3b 0a 20  0, 0, pToken);. 
1c5e8 20 69 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20   if( p==0 ){.   
1c5e9 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 4d   return 0;  /* M
1c5ea 61 6c 6c 6f 63 20 66 61 69 6c 65 64 20 2a 2f 0a  alloc failed */.
1c5eb 20 20 7d 0a 20 20 70 2d 3e 69 54 61 62 6c 65 20    }.  p->iTable 
1c5ec 3d 20 61 74 6f 69 28 28 63 68 61 72 2a 29 26 70  = atoi((char*)&p
1c5ed 54 6f 6b 65 6e 2d 3e 7a 5b 31 5d 29 3b 0a 20 20  Token->z[1]);.  
1c5ee 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 0a  return p;.}../*.
1c5ef 2a 2a 20 4a 6f 69 6e 20 74 77 6f 20 65 78 70 72  ** Join two expr
1c5f0 65 73 73 69 6f 6e 73 20 75 73 69 6e 67 20 61 6e  essions using an
1c5f1 20 41 4e 44 20 6f 70 65 72 61 74 6f 72 2e 20 20   AND operator.  
1c5f2 49 66 20 65 69 74 68 65 72 20 65 78 70 72 65 73  If either expres
1c5f3 73 69 6f 6e 20 69 73 0a 2a 2a 20 4e 55 4c 4c 2c  sion is.** NULL,
1c5f4 20 74 68 65 6e 20 6a 75 73 74 20 72 65 74 75 72   then just retur
1c5f5 6e 20 74 68 65 20 6f 74 68 65 72 20 65 78 70 72  n the other expr
1c5f6 65 73 73 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ession..*/.SQLIT
1c5f7 45 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a  E_PRIVATE Expr *
1c5f8 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28 73  sqlite3ExprAnd(s
1c5f9 71 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72  qlite3 *db, Expr
1c5fa 20 2a 70 4c 65 66 74 2c 20 45 78 70 72 20 2a 70   *pLeft, Expr *p
1c5fb 52 69 67 68 74 29 7b 0a 20 20 69 66 28 20 70 4c  Right){.  if( pL
1c5fc 65 66 74 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  eft==0 ){.    re
1c5fd 74 75 72 6e 20 70 52 69 67 68 74 3b 0a 20 20 7d  turn pRight;.  }
1c5fe 65 6c 73 65 20 69 66 28 20 70 52 69 67 68 74 3d  else if( pRight=
1c5ff 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  =0 ){.    return
1c600 20 70 4c 65 66 74 3b 0a 20 20 7d 65 6c 73 65 7b   pLeft;.  }else{
1c601 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69  .    return sqli
1c602 74 65 33 45 78 70 72 28 64 62 2c 20 54 4b 5f 41  te3Expr(db, TK_A
1c603 4e 44 2c 20 70 4c 65 66 74 2c 20 70 52 69 67 68  ND, pLeft, pRigh
1c604 74 2c 20 30 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  t, 0);.  }.}../*
1c605 0a 2a 2a 20 53 65 74 20 74 68 65 20 45 78 70 72  .** Set the Expr
1c606 2e 73 70 61 6e 20 66 69 65 6c 64 20 6f 66 20 74  .span field of t
1c607 68 65 20 67 69 76 65 6e 20 65 78 70 72 65 73 73  he given express
1c608 69 6f 6e 20 74 6f 20 73 70 61 6e 20 61 6c 6c 0a  ion to span all.
1c609 2a 2a 20 74 65 78 74 20 62 65 74 77 65 65 6e 20  ** text between 
1c60a 74 68 65 20 74 77 6f 20 67 69 76 65 6e 20 74 6f  the two given to
1c60b 6b 65 6e 73 2e 20 20 42 6f 74 68 20 74 6f 6b 65  kens.  Both toke
1c60c 6e 73 20 6d 75 73 74 20 62 65 20 70 6f 69 6e 74  ns must be point
1c60d 69 6e 67 0a 2a 2a 20 61 74 20 74 68 65 20 73 61  ing.** at the sa
1c60e 6d 65 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51  me string..*/.SQ
1c60f 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1c610 64 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61  d sqlite3ExprSpa
1c611 6e 28 45 78 70 72 20 2a 70 45 78 70 72 2c 20 54  n(Expr *pExpr, T
1c612 6f 6b 65 6e 20 2a 70 4c 65 66 74 2c 20 54 6f 6b  oken *pLeft, Tok
1c613 65 6e 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61  en *pRight){.  a
1c614 73 73 65 72 74 28 20 70 52 69 67 68 74 21 3d 30  ssert( pRight!=0
1c615 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c   );.  assert( pL
1c616 65 66 74 21 3d 30 20 29 3b 0a 20 20 69 66 28 20  eft!=0 );.  if( 
1c617 70 45 78 70 72 20 29 7b 0a 20 20 20 20 70 45 78  pExpr ){.    pEx
1c618 70 72 2d 3e 73 70 61 6e 2e 7a 20 3d 20 70 4c 65  pr->span.z = pLe
1c619 66 74 2d 3e 7a 3b 0a 20 20 20 20 70 45 78 70 72  ft->z;.    pExpr
1c61a 2d 3e 73 70 61 6e 2e 6e 20 3d 20 70 52 69 67 68  ->span.n = pRigh
1c61b 74 2d 3e 6e 20 2b 20 28 70 52 69 67 68 74 2d 3e  t->n + (pRight->
1c61c 7a 20 2d 20 70 4c 65 66 74 2d 3e 7a 29 3b 0a 20  z - pLeft->z);. 
1c61d 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73   }.}../*.** Cons
1c61e 74 72 75 63 74 20 61 20 6e 65 77 20 65 78 70 72  truct a new expr
1c61f 65 73 73 69 6f 6e 20 6e 6f 64 65 20 66 6f 72 20  ession node for 
1c620 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
1c621 6d 75 6c 74 69 70 6c 65 0a 2a 2a 20 61 72 67 75  multiple.** argu
1c622 6d 65 6e 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ments..*/.SQLITE
1c623 5f 50 52 49 56 41 54 45 20 45 78 70 72 20 2a 73  _PRIVATE Expr *s
1c624 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74 69  qlite3ExprFuncti
1c625 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
1c626 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73  , ExprList *pLis
1c627 74 2c 20 54 6f 6b 65 6e 20 2a 70 54 6f 6b 65 6e  t, Token *pToken
1c628 29 7b 0a 20 20 45 78 70 72 20 2a 70 4e 65 77 3b  ){.  Expr *pNew;
1c629 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1c62a 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 61   pParse->db;.  a
1c62b 73 73 65 72 74 28 20 70 54 6f 6b 65 6e 20 29 3b  ssert( pToken );
1c62c 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65  .  pNew = sqlite
1c62d 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
1c62e 2c 20 73 69 7a 65 6f 66 28 45 78 70 72 29 20 29  , sizeof(Expr) )
1c62f 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20  ;.  if( pNew==0 
1c630 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
1c631 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
1c632 20 70 4c 69 73 74 29 3b 20 2f 2a 20 41 76 6f 69   pList); /* Avoi
1c633 64 20 6c 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79  d leaking memory
1c634 20 77 68 65 6e 20 6d 61 6c 6c 6f 63 20 66 61 69   when malloc fai
1c635 6c 73 20 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e  ls */.    return
1c636 20 30 3b 0a 20 20 7d 0a 20 20 70 4e 65 77 2d 3e   0;.  }.  pNew->
1c637 6f 70 20 3d 20 54 4b 5f 46 55 4e 43 54 49 4f 4e  op = TK_FUNCTION
1c638 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 73 74 20  ;.  pNew->pList 
1c639 3d 20 70 4c 69 73 74 3b 0a 20 20 61 73 73 65 72  = pList;.  asser
1c63a 74 28 20 70 54 6f 6b 65 6e 2d 3e 64 79 6e 3d 3d  t( pToken->dyn==
1c63b 30 20 29 3b 0a 20 20 70 4e 65 77 2d 3e 74 6f 6b  0 );.  pNew->tok
1c63c 65 6e 20 3d 20 2a 70 54 6f 6b 65 6e 3b 0a 20 20  en = *pToken;.  
1c63d 70 4e 65 77 2d 3e 73 70 61 6e 20 3d 20 70 4e 65  pNew->span = pNe
1c63e 77 2d 3e 74 6f 6b 65 6e 3b 0a 0a 20 20 73 71 6c  w->token;..  sql
1c63f 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
1c640 74 28 70 50 61 72 73 65 2c 20 70 4e 65 77 29 3b  t(pParse, pNew);
1c641 0a 20 20 72 65 74 75 72 6e 20 70 4e 65 77 3b 0a  .  return pNew;.
1c642 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73 69 67 6e 20  }../*.** Assign 
1c643 61 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  a variable numbe
1c644 72 20 74 6f 20 61 6e 20 65 78 70 72 65 73 73 69  r to an expressi
1c645 6f 6e 20 74 68 61 74 20 65 6e 63 6f 64 65 73 20  on that encodes 
1c646 61 20 77 69 6c 64 63 61 72 64 0a 2a 2a 20 69 6e  a wildcard.** in
1c647 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
1c648 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 0a 2a  L statement.  .*
1c649 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 63  *.** Wildcards c
1c64a 6f 6e 73 69 73 74 69 6e 67 20 6f 66 20 61 20 73  onsisting of a s
1c64b 69 6e 67 6c 65 20 22 3f 22 20 61 72 65 20 61 73  ingle "?" are as
1c64c 73 69 67 6e 65 64 20 74 68 65 20 6e 65 78 74 20  signed the next 
1c64d 73 65 71 75 65 6e 74 69 61 6c 0a 2a 2a 20 76 61  sequential.** va
1c64e 72 69 61 62 6c 65 20 6e 75 6d 62 65 72 2e 0a 2a  riable number..*
1c64f 2a 0a 2a 2a 20 57 69 6c 64 63 61 72 64 73 20 6f  *.** Wildcards o
1c650 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 6e 6e 6e  f the form "?nnn
1c651 22 20 61 72 65 20 61 73 73 69 67 6e 65 64 20 74  " are assigned t
1c652 68 65 20 6e 75 6d 62 65 72 20 22 6e 6e 6e 22 2e  he number "nnn".
1c653 20 20 57 65 20 6d 61 6b 65 0a 2a 2a 20 73 75 72    We make.** sur
1c654 65 20 22 6e 6e 6e 22 20 69 73 20 6e 6f 74 20 74  e "nnn" is not t
1c655 6f 6f 20 62 65 20 74 6f 20 61 76 6f 69 64 20 61  oo be to avoid a
1c656 20 64 65 6e 69 61 6c 20 6f 66 20 73 65 72 76 69   denial of servi
1c657 63 65 20 61 74 74 61 63 6b 20 77 68 65 6e 0a 2a  ce attack when.*
1c658 2a 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  * the SQL statem
1c659 65 6e 74 20 63 6f 6d 65 73 20 66 72 6f 6d 20 61  ent comes from a
1c65a 6e 20 65 78 74 65 72 6e 61 6c 20 73 6f 75 72 63  n external sourc
1c65b 65 2e 0a 2a 2a 0a 2a 2a 20 57 69 6c 64 63 61 72  e..**.** Wildcar
1c65c 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ds of the form "
1c65d 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 20  :aaa" or "$aaa" 
1c65e 61 72 65 20 61 73 73 69 67 6e 65 64 20 74 68 65  are assigned the
1c65f 20 73 61 6d 65 20 6e 75 6d 62 65 72 0a 2a 2a 20   same number.** 
1c660 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  as the previous 
1c661 69 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20  instance of the 
1c662 73 61 6d 65 20 77 69 6c 64 63 61 72 64 2e 20 20  same wildcard.  
1c663 4f 72 20 69 66 20 74 68 69 73 20 69 73 20 74 68  Or if this is th
1c664 65 20 66 69 72 73 74 0a 2a 2a 20 69 6e 73 74 61  e first.** insta
1c665 6e 63 65 20 6f 66 20 74 68 65 20 77 69 6c 64 63  nce of the wildc
1c666 61 72 64 2c 20 74 68 65 20 6e 65 78 74 20 73 65  ard, the next se
1c667 71 75 65 6e 69 61 6c 20 76 61 72 69 61 62 6c 65  quenial variable
1c668 20 6e 75 6d 62 65 72 20 69 73 0a 2a 2a 20 61 73   number is.** as
1c669 73 69 67 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  signed..*/.SQLIT
1c66a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1c66b 71 6c 69 74 65 33 45 78 70 72 41 73 73 69 67 6e  qlite3ExprAssign
1c66c 56 61 72 4e 75 6d 62 65 72 28 50 61 72 73 65 20  VarNumber(Parse 
1c66d 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1c66e 45 78 70 72 29 7b 0a 20 20 54 6f 6b 65 6e 20 2a  Expr){.  Token *
1c66f 70 54 6f 6b 65 6e 3b 0a 20 20 73 71 6c 69 74 65  pToken;.  sqlite
1c670 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1c671 64 62 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72  db;..  if( pExpr
1c672 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1c673 70 54 6f 6b 65 6e 20 3d 20 26 70 45 78 70 72 2d  pToken = &pExpr-
1c674 3e 74 6f 6b 65 6e 3b 0a 20 20 61 73 73 65 72 74  >token;.  assert
1c675 28 20 70 54 6f 6b 65 6e 2d 3e 6e 3e 3d 31 20 29  ( pToken->n>=1 )
1c676 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 6f 6b  ;.  assert( pTok
1c677 65 6e 2d 3e 7a 21 3d 30 20 29 3b 0a 20 20 61 73  en->z!=0 );.  as
1c678 73 65 72 74 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b  sert( pToken->z[
1c679 30 5d 21 3d 30 20 29 3b 0a 20 20 69 66 28 20 70  0]!=0 );.  if( p
1c67a 54 6f 6b 65 6e 2d 3e 6e 3d 3d 31 20 29 7b 0a 20  Token->n==1 ){. 
1c67b 20 20 20 2f 2a 20 57 69 6c 64 63 61 72 64 20 6f     /* Wildcard o
1c67c 66 20 74 68 65 20 66 6f 72 6d 20 22 3f 22 2e 20  f the form "?". 
1c67d 20 41 73 73 69 67 6e 20 74 68 65 20 6e 65 78 74   Assign the next
1c67e 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65 72   variable number
1c67f 20 2a 2f 0a 20 20 20 20 70 45 78 70 72 2d 3e 69   */.    pExpr->i
1c680 54 61 62 6c 65 20 3d 20 2b 2b 70 50 61 72 73 65  Table = ++pParse
1c681 2d 3e 6e 56 61 72 3b 0a 20 20 7d 65 6c 73 65 20  ->nVar;.  }else 
1c682 69 66 28 20 70 54 6f 6b 65 6e 2d 3e 7a 5b 30 5d  if( pToken->z[0]
1c683 3d 3d 27 3f 27 20 29 7b 0a 20 20 20 20 2f 2a 20  =='?' ){.    /* 
1c684 57 69 6c 64 63 61 72 64 20 6f 66 20 74 68 65 20  Wildcard of the 
1c685 66 6f 72 6d 20 22 3f 6e 6e 6e 22 2e 20 20 43 6f  form "?nnn".  Co
1c686 6e 76 65 72 74 20 22 6e 6e 6e 22 20 74 6f 20 61  nvert "nnn" to a
1c687 6e 20 69 6e 74 65 67 65 72 20 61 6e 64 0a 20 20  n integer and.  
1c688 20 20 2a 2a 20 75 73 65 20 69 74 20 61 73 20 74    ** use it as t
1c689 68 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62  he variable numb
1c68a 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 3b  er */.    int i;
1c68b 0a 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62  .    pExpr->iTab
1c68c 6c 65 20 3d 20 69 20 3d 20 61 74 6f 69 28 28 63  le = i = atoi((c
1c68d 68 61 72 2a 29 26 70 54 6f 6b 65 6e 2d 3e 7a 5b  har*)&pToken->z[
1c68e 31 5d 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73  1]);.    testcas
1c68f 65 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20 74  e( i==0 );.    t
1c690 65 73 74 63 61 73 65 28 20 69 3d 3d 31 20 29 3b  estcase( i==1 );
1c691 0a 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69  .    testcase( i
1c692 3d 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ==db->aLimit[SQL
1c693 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42  ITE_LIMIT_VARIAB
1c694 4c 45 5f 4e 55 4d 42 45 52 5d 2d 31 20 29 3b 0a  LE_NUMBER]-1 );.
1c695 20 20 20 20 74 65 73 74 63 61 73 65 28 20 69 3d      testcase( i=
1c696 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  =db->aLimit[SQLI
1c697 54 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c  TE_LIMIT_VARIABL
1c698 45 5f 4e 55 4d 42 45 52 5d 20 29 3b 0a 20 20 20  E_NUMBER] );.   
1c699 20 69 66 28 20 69 3c 31 20 7c 7c 20 69 3e 64 62   if( i<1 || i>db
1c69a 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f  ->aLimit[SQLITE_
1c69b 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45 5f 4e  LIMIT_VARIABLE_N
1c69c 55 4d 42 45 52 5d 20 29 7b 0a 20 20 20 20 20 20  UMBER] ){.      
1c69d 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1c69e 70 50 61 72 73 65 2c 20 22 76 61 72 69 61 62 6c  pParse, "variabl
1c69f 65 20 6e 75 6d 62 65 72 20 6d 75 73 74 20 62 65  e number must be
1c6a0 20 62 65 74 77 65 65 6e 20 3f 31 20 61 6e 64 20   between ?1 and 
1c6a1 3f 25 64 22 2c 0a 20 20 20 20 20 20 20 20 20 20  ?%d",.          
1c6a2 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
1c6a3 45 5f 4c 49 4d 49 54 5f 56 41 52 49 41 42 4c 45  E_LIMIT_VARIABLE
1c6a4 5f 4e 55 4d 42 45 52 5d 29 3b 0a 20 20 20 20 7d  _NUMBER]);.    }
1c6a5 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73  .    if( i>pPars
1c6a6 65 2d 3e 6e 56 61 72 20 29 7b 0a 20 20 20 20 20  e->nVar ){.     
1c6a7 20 70 50 61 72 73 65 2d 3e 6e 56 61 72 20 3d 20   pParse->nVar = 
1c6a8 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  i;.    }.  }else
1c6a9 7b 0a 20 20 20 20 2f 2a 20 57 69 6c 64 63 61 72  {.    /* Wildcar
1c6aa 64 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 22  ds of the form "
1c6ab 3a 61 61 61 22 20 6f 72 20 22 24 61 61 61 22 2e  :aaa" or "$aaa".
1c6ac 20 20 52 65 75 73 65 20 74 68 65 20 73 61 6d 65    Reuse the same
1c6ad 20 76 61 72 69 61 62 6c 65 0a 20 20 20 20 2a 2a   variable.    **
1c6ae 20 6e 75 6d 62 65 72 20 61 73 20 74 68 65 20 70   number as the p
1c6af 72 69 6f 72 20 61 70 70 65 61 72 61 6e 63 65 20  rior appearance 
1c6b0 6f 66 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  of the same name
1c6b1 2c 20 6f 72 20 69 66 20 74 68 65 20 6e 61 6d 65  , or if the name
1c6b2 0a 20 20 20 20 2a 2a 20 68 61 73 20 6e 65 76 65  .    ** has neve
1c6b3 72 20 61 70 70 65 61 72 65 64 20 62 65 66 6f 72  r appeared befor
1c6b4 65 2c 20 72 65 75 73 65 20 74 68 65 20 73 61 6d  e, reuse the sam
1c6b5 65 20 76 61 72 69 61 62 6c 65 20 6e 75 6d 62 65  e variable numbe
1c6b6 72 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74  r.    */.    int
1c6b7 20 69 2c 20 6e 3b 0a 20 20 20 20 6e 20 3d 20 70   i, n;.    n = p
1c6b8 54 6f 6b 65 6e 2d 3e 6e 3b 0a 20 20 20 20 66 6f  Token->n;.    fo
1c6b9 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
1c6ba 3e 6e 56 61 72 45 78 70 72 3b 20 69 2b 2b 29 7b  >nVarExpr; i++){
1c6bb 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 3b  .      Expr *pE;
1c6bc 0a 20 20 20 20 20 20 69 66 28 20 28 70 45 20 3d  .      if( (pE =
1c6bd 20 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78   pParse->apVarEx
1c6be 70 72 5b 69 5d 29 21 3d 30 0a 20 20 20 20 20 20  pr[i])!=0.      
1c6bf 20 20 20 20 26 26 20 70 45 2d 3e 74 6f 6b 65 6e      && pE->token
1c6c0 2e 6e 3d 3d 6e 0a 20 20 20 20 20 20 20 20 20 20  .n==n.          
1c6c1 26 26 20 6d 65 6d 63 6d 70 28 70 45 2d 3e 74 6f  && memcmp(pE->to
1c6c2 6b 65 6e 2e 7a 2c 20 70 54 6f 6b 65 6e 2d 3e 7a  ken.z, pToken->z
1c6c3 2c 20 6e 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , n)==0 ){.     
1c6c4 20 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65     pExpr->iTable
1c6c5 20 3d 20 70 45 2d 3e 69 54 61 62 6c 65 3b 0a 20   = pE->iTable;. 
1c6c6 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
1c6c7 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1c6c8 69 66 28 20 69 3e 3d 70 50 61 72 73 65 2d 3e 6e  if( i>=pParse->n
1c6c9 56 61 72 45 78 70 72 20 29 7b 0a 20 20 20 20 20  VarExpr ){.     
1c6ca 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d   pExpr->iTable =
1c6cb 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 56 61 72 3b   ++pParse->nVar;
1c6cc 0a 20 20 20 20 20 20 69 66 28 20 70 50 61 72 73  .      if( pPars
1c6cd 65 2d 3e 6e 56 61 72 45 78 70 72 3e 3d 70 50 61  e->nVarExpr>=pPa
1c6ce 72 73 65 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c  rse->nVarExprAll
1c6cf 6f 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  oc-1 ){.        
1c6d0 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70 72  pParse->nVarExpr
1c6d1 41 6c 6c 6f 63 20 2b 3d 20 70 50 61 72 73 65 2d  Alloc += pParse-
1c6d2 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 20 2b  >nVarExprAlloc +
1c6d3 20 31 30 3b 0a 20 20 20 20 20 20 20 20 70 50 61   10;.        pPa
1c6d4 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72 20 3d  rse->apVarExpr =
1c6d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  .            sql
1c6d6 69 74 65 33 44 62 52 65 61 6c 6c 6f 63 4f 72 46  ite3DbReallocOrF
1c6d7 72 65 65 28 0a 20 20 20 20 20 20 20 20 20 20 20  ree(.           
1c6d8 20 20 20 64 62 2c 0a 20 20 20 20 20 20 20 20 20     db,.         
1c6d9 20 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56       pParse->apV
1c6da 61 72 45 78 70 72 2c 0a 20 20 20 20 20 20 20 20  arExpr,.        
1c6db 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 56        pParse->nV
1c6dc 61 72 45 78 70 72 41 6c 6c 6f 63 2a 73 69 7a 65  arExprAlloc*size
1c6dd 6f 66 28 70 50 61 72 73 65 2d 3e 61 70 56 61 72  of(pParse->apVar
1c6de 45 78 70 72 5b 30 5d 29 0a 20 20 20 20 20 20 20  Expr[0]).       
1c6df 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20 7d 0a       );.      }.
1c6e0 20 20 20 20 20 20 69 66 28 20 21 64 62 2d 3e 6d        if( !db->m
1c6e1 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
1c6e2 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
1c6e3 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
1c6e4 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 70  !=0 );.        p
1c6e5 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70 72  Parse->apVarExpr
1c6e6 5b 70 50 61 72 73 65 2d 3e 6e 56 61 72 45 78 70  [pParse->nVarExp
1c6e7 72 2b 2b 5d 20 3d 20 70 45 78 70 72 3b 0a 20 20  r++] = pExpr;.  
1c6e8 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 20      }.    }.  } 
1c6e9 0a 20 20 69 66 28 20 21 70 50 61 72 73 65 2d 3e  .  if( !pParse->
1c6ea 6e 45 72 72 20 26 26 20 70 50 61 72 73 65 2d 3e  nErr && pParse->
1c6eb 6e 56 61 72 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b  nVar>db->aLimit[
1c6ec 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 56 41 52  SQLITE_LIMIT_VAR
1c6ed 49 41 42 4c 45 5f 4e 55 4d 42 45 52 5d 20 29 7b  IABLE_NUMBER] ){
1c6ee 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
1c6ef 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 6f  rMsg(pParse, "to
1c6f0 6f 20 6d 61 6e 79 20 53 51 4c 20 76 61 72 69 61  o many SQL varia
1c6f1 62 6c 65 73 22 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bles");.  }.}../
1c6f2 2a 0a 2a 2a 20 43 6c 65 61 72 20 61 6e 20 65 78  *.** Clear an ex
1c6f3 70 72 65 73 73 69 6f 6e 20 73 74 72 75 63 74 75  pression structu
1c6f4 72 65 20 77 69 74 68 6f 75 74 20 64 65 6c 65 74  re without delet
1c6f5 69 6e 67 20 74 68 65 20 73 74 72 75 63 74 75 72  ing the structur
1c6f6 65 20 69 74 73 65 6c 66 2e 0a 2a 2a 20 53 75 62  e itself..** Sub
1c6f7 73 74 72 75 63 74 75 72 65 20 69 73 20 64 65 6c  structure is del
1c6f8 65 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  eted..*/.SQLITE_
1c6f9 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1c6fa 69 74 65 33 45 78 70 72 43 6c 65 61 72 28 73 71  ite3ExprClear(sq
1c6fb 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
1c6fc 2a 70 29 7b 0a 20 20 69 66 28 20 70 2d 3e 73 70  *p){.  if( p->sp
1c6fd 61 6e 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33  an.dyn ) sqlite3
1c6fe 44 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72  DbFree(db, (char
1c6ff 2a 29 70 2d 3e 73 70 61 6e 2e 7a 29 3b 0a 20 20  *)p->span.z);.  
1c700 69 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 64 79 6e  if( p->token.dyn
1c701 20 29 20 73 71 6c 69 74 65 33 44 62 46 72 65 65   ) sqlite3DbFree
1c702 28 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 74  (db, (char*)p->t
1c703 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 73 71 6c 69 74  oken.z);.  sqlit
1c704 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
1c705 20 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 73 71   p->pLeft);.  sq
1c706 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
1c707 64 62 2c 20 70 2d 3e 70 52 69 67 68 74 29 3b 0a  db, p->pRight);.
1c708 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
1c709 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
1c70a 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  List);.  sqlite3
1c70b 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c  SelectDelete(db,
1c70c 20 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 7d 0a   p->pSelect);.}.
1c70d 0a 2f 2a 0a 2a 2a 20 52 65 63 75 72 73 69 76 65  ./*.** Recursive
1c70e 6c 79 20 64 65 6c 65 74 65 20 61 6e 20 65 78 70  ly delete an exp
1c70f 72 65 73 73 69 6f 6e 20 74 72 65 65 2e 0a 2a 2f  ression tree..*/
1c710 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1c711 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72  void sqlite3Expr
1c712 44 65 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a  Delete(sqlite3 *
1c713 64 62 2c 20 45 78 70 72 20 2a 70 29 7b 0a 20 20  db, Expr *p){.  
1c714 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72  if( p==0 ) retur
1c715 6e 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  n;.  sqlite3Expr
1c716 43 6c 65 61 72 28 64 62 2c 20 70 29 3b 0a 20 20  Clear(db, p);.  
1c717 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1c718 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  , p);.}../*.** T
1c719 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 20 66 69  he Expr.token fi
1c71a 65 6c 64 20 6d 69 67 68 74 20 62 65 20 61 20 73  eld might be a s
1c71b 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 20 74 68  tring literal th
1c71c 61 74 20 69 73 20 71 75 6f 74 65 64 2e 0a 2a 2a  at is quoted..**
1c71d 20 49 66 20 73 6f 2c 20 72 65 6d 6f 76 65 20 74   If so, remove t
1c71e 68 65 20 71 75 6f 74 61 74 69 6f 6e 20 6d 61 72  he quotation mar
1c71f 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ks..*/.SQLITE_PR
1c720 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1c721 65 33 44 65 71 75 6f 74 65 45 78 70 72 28 73 71  e3DequoteExpr(sq
1c722 6c 69 74 65 33 20 2a 64 62 2c 20 45 78 70 72 20  lite3 *db, Expr 
1c723 2a 70 29 7b 0a 20 20 69 66 28 20 45 78 70 72 48  *p){.  if( ExprH
1c724 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28 70 2c  asAnyProperty(p,
1c725 20 45 50 5f 44 65 71 75 6f 74 65 64 29 20 29 7b   EP_Dequoted) ){
1c726 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1c727 0a 20 20 45 78 70 72 53 65 74 50 72 6f 70 65 72  .  ExprSetProper
1c728 74 79 28 70 2c 20 45 50 5f 44 65 71 75 6f 74 65  ty(p, EP_Dequote
1c729 64 29 3b 0a 20 20 69 66 28 20 70 2d 3e 74 6f 6b  d);.  if( p->tok
1c72a 65 6e 2e 64 79 6e 3d 3d 30 20 29 7b 0a 20 20 20  en.dyn==0 ){.   
1c72b 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1c72c 79 28 64 62 2c 20 26 70 2d 3e 74 6f 6b 65 6e 2c  y(db, &p->token,
1c72d 20 26 70 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 7d   &p->token);.  }
1c72e 0a 20 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74  .  sqlite3Dequot
1c72f 65 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b 65  e((char*)p->toke
1c730 6e 2e 7a 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n.z);.}../*.** T
1c731 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67 72 6f  he following gro
1c732 75 70 20 6f 66 20 72 6f 75 74 69 6e 65 73 20 6d  up of routines m
1c733 61 6b 65 20 64 65 65 70 20 63 6f 70 69 65 73 20  ake deep copies 
1c734 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 0a  of expressions,.
1c735 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69  ** expression li
1c736 73 74 73 2c 20 49 44 20 6c 69 73 74 73 2c 20 61  sts, ID lists, a
1c737 6e 64 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d  nd select statem
1c738 65 6e 74 73 2e 20 20 54 68 65 20 63 6f 70 69 65  ents.  The copie
1c739 73 20 63 61 6e 0a 2a 2a 20 62 65 20 64 65 6c 65  s can.** be dele
1c73a 74 65 64 20 28 62 79 20 62 65 69 6e 67 20 70 61  ted (by being pa
1c73b 73 73 65 64 20 74 6f 20 74 68 65 69 72 20 72 65  ssed to their re
1c73c 73 70 65 63 74 69 76 65 20 2e 2e 2e 44 65 6c 65  spective ...Dele
1c73d 74 65 28 29 20 72 6f 75 74 69 6e 65 73 29 0a 2a  te() routines).*
1c73e 2a 20 77 69 74 68 6f 75 74 20 65 66 66 65 63 74  * without effect
1c73f 69 6e 67 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ing the original
1c740 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 65 78 70  s..**.** The exp
1c741 72 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 49 44  ression list, ID
1c742 2c 20 61 6e 64 20 73 6f 75 72 63 65 20 6c 69 73  , and source lis
1c743 74 73 20 72 65 74 75 72 6e 20 62 79 20 73 71 6c  ts return by sql
1c744 69 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28  ite3ExprListDup(
1c745 29 2c 0a 2a 2a 20 73 71 6c 69 74 65 33 49 64 4c  ),.** sqlite3IdL
1c746 69 73 74 44 75 70 28 29 2c 20 61 6e 64 20 73 71  istDup(), and sq
1c747 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28  lite3SrcListDup(
1c748 29 20 63 61 6e 20 6e 6f 74 20 62 65 20 66 75 72  ) can not be fur
1c749 74 68 65 72 20 65 78 70 61 6e 64 65 64 20 0a 2a  ther expanded .*
1c74a 2a 20 62 79 20 73 75 62 73 65 71 75 65 6e 74 20  * by subsequent 
1c74b 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69 74 65 2a  calls to sqlite*
1c74c 4c 69 73 74 41 70 70 65 6e 64 28 29 20 72 6f 75  ListAppend() rou
1c74d 74 69 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 79  tines..**.** Any
1c74e 20 74 61 62 6c 65 73 20 74 68 61 74 20 74 68 65   tables that the
1c74f 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20 70   SrcList might p
1c750 6f 69 6e 74 20 74 6f 20 61 72 65 20 6e 6f 74 20  oint to are not 
1c751 64 75 70 6c 69 63 61 74 65 64 2e 0a 2a 2f 0a 53  duplicated..*/.S
1c752 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45 78  QLITE_PRIVATE Ex
1c753 70 72 20 2a 73 71 6c 69 74 65 33 45 78 70 72 44  pr *sqlite3ExprD
1c754 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
1c755 45 78 70 72 20 2a 70 29 7b 0a 20 20 45 78 70 72  Expr *p){.  Expr
1c756 20 2a 70 4e 65 77 3b 0a 20 20 69 66 28 20 70 3d   *pNew;.  if( p=
1c757 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1c758 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
1c759 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
1c75a 69 7a 65 6f 66 28 2a 70 29 20 29 3b 0a 20 20 69  izeof(*p) );.  i
1c75b 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65 74  f( pNew==0 ) ret
1c75c 75 72 6e 20 30 3b 0a 20 20 6d 65 6d 63 70 79 28  urn 0;.  memcpy(
1c75d 70 4e 65 77 2c 20 70 2c 20 73 69 7a 65 6f 66 28  pNew, p, sizeof(
1c75e 2a 70 4e 65 77 29 29 3b 0a 20 20 69 66 28 20 70  *pNew));.  if( p
1c75f 2d 3e 74 6f 6b 65 6e 2e 7a 21 3d 30 20 29 7b 0a  ->token.z!=0 ){.
1c760 20 20 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e      pNew->token.
1c761 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33  z = (u8*)sqlite3
1c762 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
1c763 68 61 72 2a 29 70 2d 3e 74 6f 6b 65 6e 2e 7a 2c  har*)p->token.z,
1c764 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20   p->token.n);.  
1c765 20 20 70 4e 65 77 2d 3e 74 6f 6b 65 6e 2e 64 79    pNew->token.dy
1c766 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a  n = 1;.  }else{.
1c767 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77      assert( pNew
1c768 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30 20 29 3b 0a  ->token.z==0 );.
1c769 20 20 7d 0a 20 20 70 4e 65 77 2d 3e 73 70 61 6e    }.  pNew->span
1c76a 2e 7a 20 3d 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  .z = 0;.  pNew->
1c76b 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 45  pLeft = sqlite3E
1c76c 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c  xprDup(db, p->pL
1c76d 65 66 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 52  eft);.  pNew->pR
1c76e 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 45 78  ight = sqlite3Ex
1c76f 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 52 69  prDup(db, p->pRi
1c770 67 68 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c  ght);.  pNew->pL
1c771 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
1c772 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
1c773 70 4c 69 73 74 29 3b 0a 20 20 70 4e 65 77 2d 3e  pList);.  pNew->
1c774 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65  pSelect = sqlite
1c775 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20 70  3SelectDup(db, p
1c776 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 72 65  ->pSelect);.  re
1c777 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c  turn pNew;.}.SQL
1c778 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1c779 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1c77a 79 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 54  y(sqlite3 *db, T
1c77b 6f 6b 65 6e 20 2a 70 54 6f 2c 20 54 6f 6b 65 6e  oken *pTo, Token
1c77c 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28 20   *pFrom){.  if( 
1c77d 70 54 6f 2d 3e 64 79 6e 20 29 20 73 71 6c 69 74  pTo->dyn ) sqlit
1c77e 65 33 44 62 46 72 65 65 28 64 62 2c 20 28 63 68  e3DbFree(db, (ch
1c77f 61 72 2a 29 70 54 6f 2d 3e 7a 29 3b 0a 20 20 69  ar*)pTo->z);.  i
1c780 66 28 20 70 46 72 6f 6d 2d 3e 7a 20 29 7b 0a 20  f( pFrom->z ){. 
1c781 20 20 20 70 54 6f 2d 3e 6e 20 3d 20 70 46 72 6f     pTo->n = pFro
1c782 6d 2d 3e 6e 3b 0a 20 20 20 20 70 54 6f 2d 3e 7a  m->n;.    pTo->z
1c783 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33 44   = (u8*)sqlite3D
1c784 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63 68  bStrNDup(db, (ch
1c785 61 72 2a 29 70 46 72 6f 6d 2d 3e 7a 2c 20 70 46  ar*)pFrom->z, pF
1c786 72 6f 6d 2d 3e 6e 29 3b 0a 20 20 20 20 70 54 6f  rom->n);.    pTo
1c787 2d 3e 64 79 6e 20 3d 20 31 3b 0a 20 20 7d 65 6c  ->dyn = 1;.  }el
1c788 73 65 7b 0a 20 20 20 20 70 54 6f 2d 3e 7a 20 3d  se{.    pTo->z =
1c789 20 30 3b 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45   0;.  }.}.SQLITE
1c78a 5f 50 52 49 56 41 54 45 20 45 78 70 72 4c 69 73  _PRIVATE ExprLis
1c78b 74 20 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69  t *sqlite3ExprLi
1c78c 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
1c78d 62 2c 20 45 78 70 72 4c 69 73 74 20 2a 70 29 7b  b, ExprList *p){
1c78e 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e 65  .  ExprList *pNe
1c78f 77 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  w;.  struct Expr
1c790 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1c791 2c 20 2a 70 4f 6c 64 49 74 65 6d 3b 0a 20 20 69  , *pOldItem;.  i
1c792 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d 3d 30  nt i;.  if( p==0
1c793 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
1c794 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  New = sqlite3DbM
1c795 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73 69 7a  allocRaw(db, siz
1c796 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a 20 20  eof(*pNew) );.  
1c797 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20 72 65  if( pNew==0 ) re
1c798 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77 2d 3e  turn 0;.  pNew->
1c799 69 45 43 75 72 73 6f 72 20 3d 20 30 3b 0a 20 20  iECursor = 0;.  
1c79a 70 4e 65 77 2d 3e 6e 45 78 70 72 20 3d 20 70 4e  pNew->nExpr = pN
1c79b 65 77 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e  ew->nAlloc = p->
1c79c 6e 45 78 70 72 3b 0a 20 20 70 4e 65 77 2d 3e 61  nExpr;.  pNew->a
1c79d 20 3d 20 70 49 74 65 6d 20 3d 20 73 71 6c 69 74   = pItem = sqlit
1c79e 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
1c79f 2c 20 20 70 2d 3e 6e 45 78 70 72 2a 73 69 7a 65  ,  p->nExpr*size
1c7a0 6f 66 28 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20  of(p->a[0]) );. 
1c7a1 20 69 66 28 20 70 49 74 65 6d 3d 3d 30 20 29 7b   if( pItem==0 ){
1c7a2 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1c7a3 65 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20  ee(db, pNew);.  
1c7a4 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 20    return 0;.  } 
1c7a5 0a 20 20 70 4f 6c 64 49 74 65 6d 20 3d 20 70 2d  .  pOldItem = p-
1c7a6 3e 61 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  >a;.  for(i=0; i
1c7a7 3c 70 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20  <p->nExpr; i++, 
1c7a8 70 49 74 65 6d 2b 2b 2c 20 70 4f 6c 64 49 74 65  pItem++, pOldIte
1c7a9 6d 2b 2b 29 7b 0a 20 20 20 20 45 78 70 72 20 2a  m++){.    Expr *
1c7aa 70 4e 65 77 45 78 70 72 2c 20 2a 70 4f 6c 64 45  pNewExpr, *pOldE
1c7ab 78 70 72 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  xpr;.    pItem->
1c7ac 70 45 78 70 72 20 3d 20 70 4e 65 77 45 78 70 72  pExpr = pNewExpr
1c7ad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
1c7ae 70 28 64 62 2c 20 70 4f 6c 64 45 78 70 72 20 3d  p(db, pOldExpr =
1c7af 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 45 78 70 72   pOldItem->pExpr
1c7b0 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 45  );.    if( pOldE
1c7b1 78 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 26  xpr->span.z!=0 &
1c7b2 26 20 70 4e 65 77 45 78 70 72 20 29 7b 0a 20 20  & pNewExpr ){.  
1c7b3 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 6d 61      /* Always ma
1c7b4 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
1c7b5 20 73 70 61 6e 20 66 6f 72 20 74 6f 70 2d 6c 65   span for top-le
1c7b6 76 65 6c 20 65 78 70 72 65 73 73 69 6f 6e 73 20  vel expressions 
1c7b7 69 6e 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  in the.      ** 
1c7b8 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e  expression list.
1c7b9 20 20 54 68 65 20 6c 6f 67 69 63 20 69 6e 20 53    The logic in S
1c7ba 45 4c 45 43 54 20 70 72 6f 63 65 73 73 69 6e 67  ELECT processing
1c7bb 20 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73   that determines
1c7bc 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 6e 61  .      ** the na
1c7bd 6d 65 73 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  mes of columns i
1c7be 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73 65 74  n the result set
1c7bf 20 6e 65 65 64 73 20 74 68 69 73 20 69 6e 66 6f   needs this info
1c7c0 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20 20 20  rmation */.     
1c7c1 20 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70   sqlite3TokenCop
1c7c2 79 28 64 62 2c 20 26 70 4e 65 77 45 78 70 72 2d  y(db, &pNewExpr-
1c7c3 3e 73 70 61 6e 2c 20 26 70 4f 6c 64 45 78 70 72  >span, &pOldExpr
1c7c4 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 7d 0a 20  ->span);.    }. 
1c7c5 20 20 20 61 73 73 65 72 74 28 20 70 4e 65 77 45     assert( pNewE
1c7c6 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 65 77 45 78  xpr==0 || pNewEx
1c7c7 70 72 2d 3e 73 70 61 6e 2e 7a 21 3d 30 20 0a 20  pr->span.z!=0 . 
1c7c8 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20 70 4f             || pO
1c7c9 6c 64 45 78 70 72 2d 3e 73 70 61 6e 2e 7a 3d 3d  ldExpr->span.z==
1c7ca 30 0a 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c  0.            ||
1c7cb 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1c7cc 64 20 29 3b 0a 20 20 20 20 70 49 74 65 6d 2d 3e  d );.    pItem->
1c7cd 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44  zName = sqlite3D
1c7ce 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64  bStrDup(db, pOld
1c7cf 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  Item->zName);.  
1c7d0 20 20 70 49 74 65 6d 2d 3e 73 6f 72 74 4f 72 64    pItem->sortOrd
1c7d1 65 72 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 73  er = pOldItem->s
1c7d2 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 70 49  ortOrder;.    pI
1c7d3 74 65 6d 2d 3e 64 6f 6e 65 20 3d 20 30 3b 0a 20  tem->done = 0;. 
1c7d4 20 20 20 70 49 74 65 6d 2d 3e 69 43 6f 6c 20 3d     pItem->iCol =
1c7d5 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 6f 6c 3b   pOldItem->iCol;
1c7d6 0a 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c 69  .    pItem->iAli
1c7d7 61 73 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69  as = pOldItem->i
1c7d8 41 6c 69 61 73 3b 0a 20 20 7d 0a 20 20 72 65 74  Alias;.  }.  ret
1c7d9 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a  urn pNew;.}../*.
1c7da 2a 2a 20 49 66 20 63 75 72 73 6f 72 73 2c 20 74  ** If cursors, t
1c7db 72 69 67 67 65 72 73 2c 20 76 69 65 77 73 20 61  riggers, views a
1c7dc 6e 64 20 73 75 62 71 75 65 72 69 65 73 20 61 72  nd subqueries ar
1c7dd 65 20 61 6c 6c 20 6f 6d 69 74 74 65 64 20 66 72  e all omitted fr
1c7de 6f 6d 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64 2c  om.** the build,
1c7df 20 74 68 65 6e 20 6e 6f 6e 65 20 6f 66 20 74 68   then none of th
1c7e0 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74  e following rout
1c7e1 69 6e 65 73 2c 20 65 78 63 65 70 74 20 66 6f 72  ines, except for
1c7e2 20 0a 2a 2a 20 73 71 6c 69 74 65 33 53 65 6c 65   .** sqlite3Sele
1c7e3 63 74 44 75 70 28 29 2c 20 63 61 6e 20 62 65 20  ctDup(), can be 
1c7e4 63 61 6c 6c 65 64 2e 20 73 71 6c 69 74 65 33 53  called. sqlite3S
1c7e5 65 6c 65 63 74 44 75 70 28 29 20 69 73 20 73 6f  electDup() is so
1c7e6 6d 65 74 69 6d 65 73 0a 2a 2a 20 63 61 6c 6c 65  metimes.** calle
1c7e7 64 20 77 69 74 68 20 61 20 4e 55 4c 4c 20 61 72  d with a NULL ar
1c7e8 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 23 69 66 20 21  gument..*/.#if !
1c7e9 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1c7ea 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
1c7eb 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1c7ec 54 5f 54 52 49 47 47 45 52 29 20 5c 0a 20 7c 7c  T_TRIGGER) \. ||
1c7ed 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
1c7ee 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 29 0a  _OMIT_SUBQUERY).
1c7ef 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53  SQLITE_PRIVATE S
1c7f0 72 63 4c 69 73 74 20 2a 73 71 6c 69 74 65 33 53  rcList *sqlite3S
1c7f1 72 63 4c 69 73 74 44 75 70 28 73 71 6c 69 74 65  rcListDup(sqlite
1c7f2 33 20 2a 64 62 2c 20 53 72 63 4c 69 73 74 20 2a  3 *db, SrcList *
1c7f3 70 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70  p){.  SrcList *p
1c7f4 4e 65 77 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  New;.  int i;.  
1c7f5 69 6e 74 20 6e 42 79 74 65 3b 0a 20 20 69 66 28  int nByte;.  if(
1c7f6 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30   p==0 ) return 0
1c7f7 3b 0a 20 20 6e 42 79 74 65 20 3d 20 73 69 7a 65  ;.  nByte = size
1c7f8 6f 66 28 2a 70 29 20 2b 20 28 70 2d 3e 6e 53 72  of(*p) + (p->nSr
1c7f9 63 3e 30 20 3f 20 73 69 7a 65 6f 66 28 70 2d 3e  c>0 ? sizeof(p->
1c7fa 61 5b 30 5d 29 20 2a 20 28 70 2d 3e 6e 53 72 63  a[0]) * (p->nSrc
1c7fb 2d 31 29 20 3a 20 30 29 3b 0a 20 20 70 4e 65 77  -1) : 0);.  pNew
1c7fc 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1c7fd 6f 63 52 61 77 28 64 62 2c 20 6e 42 79 74 65 20  ocRaw(db, nByte 
1c7fe 29 3b 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30  );.  if( pNew==0
1c7ff 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70   ) return 0;.  p
1c800 4e 65 77 2d 3e 6e 53 72 63 20 3d 20 70 4e 65 77  New->nSrc = pNew
1c801 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 53  ->nAlloc = p->nS
1c802 72 63 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  rc;.  for(i=0; i
1c803 3c 70 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a  <p->nSrc; i++){.
1c804 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c 69      struct SrcLi
1c805 73 74 5f 69 74 65 6d 20 2a 70 4e 65 77 49 74 65  st_item *pNewIte
1c806 6d 20 3d 20 26 70 4e 65 77 2d 3e 61 5b 69 5d 3b  m = &pNew->a[i];
1c807 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c  .    struct SrcL
1c808 69 73 74 5f 69 74 65 6d 20 2a 70 4f 6c 64 49 74  ist_item *pOldIt
1c809 65 6d 20 3d 20 26 70 2d 3e 61 5b 69 5d 3b 0a 20  em = &p->a[i];. 
1c80a 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
1c80b 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 44      pNewItem->zD
1c80c 61 74 61 62 61 73 65 20 3d 20 73 71 6c 69 74 65  atabase = sqlite
1c80d 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
1c80e 6c 64 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73  ldItem->zDatabas
1c80f 65 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d  e);.    pNewItem
1c810 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65  ->zName = sqlite
1c811 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70 4f  3DbStrDup(db, pO
1c812 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a  ldItem->zName);.
1c813 20 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 7a 41      pNewItem->zA
1c814 6c 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62  lias = sqlite3Db
1c815 53 74 72 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  StrDup(db, pOldI
1c816 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a 20 20  tem->zAlias);.  
1c817 20 20 70 4e 65 77 49 74 65 6d 2d 3e 6a 6f 69 6e    pNewItem->join
1c818 74 79 70 65 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  type = pOldItem-
1c819 3e 6a 6f 69 6e 74 79 70 65 3b 0a 20 20 20 20 70  >jointype;.    p
1c81a 4e 65 77 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72  NewItem->iCursor
1c81b 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 43 75   = pOldItem->iCu
1c81c 72 73 6f 72 3b 0a 20 20 20 20 70 4e 65 77 49 74  rsor;.    pNewIt
1c81d 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20  em->isPopulated 
1c81e 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 69 73 50 6f  = pOldItem->isPo
1c81f 70 75 6c 61 74 65 64 3b 0a 20 20 20 20 70 4e 65  pulated;.    pNe
1c820 77 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20  wItem->zIndex = 
1c821 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
1c822 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 49  db, pOldItem->zI
1c823 6e 64 65 78 29 3b 0a 20 20 20 20 70 4e 65 77 49  ndex);.    pNewI
1c824 74 65 6d 2d 3e 6e 6f 74 49 6e 64 65 78 65 64 20  tem->notIndexed 
1c825 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e 6e 6f 74 49  = pOldItem->notI
1c826 6e 64 65 78 65 64 3b 0a 20 20 20 20 70 4e 65 77  ndexed;.    pNew
1c827 49 74 65 6d 2d 3e 70 49 6e 64 65 78 20 3d 20 70  Item->pIndex = p
1c828 4f 6c 64 49 74 65 6d 2d 3e 70 49 6e 64 65 78 3b  OldItem->pIndex;
1c829 0a 20 20 20 20 70 54 61 62 20 3d 20 70 4e 65 77  .    pTab = pNew
1c82a 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20 70 4f 6c  Item->pTab = pOl
1c82b 64 49 74 65 6d 2d 3e 70 54 61 62 3b 0a 20 20 20  dItem->pTab;.   
1c82c 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20 20   if( pTab ){.   
1c82d 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 2b 2b 3b     pTab->nRef++;
1c82e 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 49  .    }.    pNewI
1c82f 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 3d 20 73  tem->pSelect = s
1c830 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70 28  qlite3SelectDup(
1c831 64 62 2c 20 70 4f 6c 64 49 74 65 6d 2d 3e 70 53  db, pOldItem->pS
1c832 65 6c 65 63 74 29 3b 0a 20 20 20 20 70 4e 65 77  elect);.    pNew
1c833 49 74 65 6d 2d 3e 70 4f 6e 20 3d 20 73 71 6c 69  Item->pOn = sqli
1c834 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
1c835 4f 6c 64 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20  OldItem->pOn);. 
1c836 20 20 20 70 4e 65 77 49 74 65 6d 2d 3e 70 55 73     pNewItem->pUs
1c837 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 49 64 4c  ing = sqlite3IdL
1c838 69 73 74 44 75 70 28 64 62 2c 20 70 4f 6c 64 49  istDup(db, pOldI
1c839 74 65 6d 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20  tem->pUsing);.  
1c83a 20 20 70 4e 65 77 49 74 65 6d 2d 3e 63 6f 6c 55    pNewItem->colU
1c83b 73 65 64 20 3d 20 70 4f 6c 64 49 74 65 6d 2d 3e  sed = pOldItem->
1c83c 63 6f 6c 55 73 65 64 3b 0a 20 20 7d 0a 20 20 72  colUsed;.  }.  r
1c83d 65 74 75 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51  eturn pNew;.}.SQ
1c83e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 49 64 4c  LITE_PRIVATE IdL
1c83f 69 73 74 20 2a 73 71 6c 69 74 65 33 49 64 4c 69  ist *sqlite3IdLi
1c840 73 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  stDup(sqlite3 *d
1c841 62 2c 20 49 64 4c 69 73 74 20 2a 70 29 7b 0a 20  b, IdList *p){. 
1c842 20 49 64 4c 69 73 74 20 2a 70 4e 65 77 3b 0a 20   IdList *pNew;. 
1c843 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 3d   int i;.  if( p=
1c844 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
1c845 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 44   pNew = sqlite3D
1c846 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20 73  bMallocRaw(db, s
1c847 69 7a 65 6f 66 28 2a 70 4e 65 77 29 20 29 3b 0a  izeof(*pNew) );.
1c848 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 20    if( pNew==0 ) 
1c849 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65 77  return 0;.  pNew
1c84a 2d 3e 6e 49 64 20 3d 20 70 4e 65 77 2d 3e 6e 41  ->nId = pNew->nA
1c84b 6c 6c 6f 63 20 3d 20 70 2d 3e 6e 49 64 3b 0a 20  lloc = p->nId;. 
1c84c 20 70 4e 65 77 2d 3e 61 20 3d 20 73 71 6c 69 74   pNew->a = sqlit
1c84d 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
1c84e 2c 20 70 2d 3e 6e 49 64 2a 73 69 7a 65 6f 66 28  , p->nId*sizeof(
1c84f 70 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 69 66  p->a[0]) );.  if
1c850 28 20 70 4e 65 77 2d 3e 61 3d 3d 30 20 29 7b 0a  ( pNew->a==0 ){.
1c851 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
1c852 65 28 64 62 2c 20 70 4e 65 77 29 3b 0a 20 20 20  e(db, pNew);.   
1c853 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
1c854 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e   for(i=0; i<p->n
1c855 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  Id; i++){.    st
1c856 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d  ruct IdList_item
1c857 20 2a 70 4e 65 77 49 74 65 6d 20 3d 20 26 70 4e   *pNewItem = &pN
1c858 65 77 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 74  ew->a[i];.    st
1c859 72 75 63 74 20 49 64 4c 69 73 74 5f 69 74 65 6d  ruct IdList_item
1c85a 20 2a 70 4f 6c 64 49 74 65 6d 20 3d 20 26 70 2d   *pOldItem = &p-
1c85b 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 4e 65 77 49  >a[i];.    pNewI
1c85c 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  tem->zName = sql
1c85d 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
1c85e 20 70 4f 6c 64 49 74 65 6d 2d 3e 7a 4e 61 6d 65   pOldItem->zName
1c85f 29 3b 0a 20 20 20 20 70 4e 65 77 49 74 65 6d 2d  );.    pNewItem-
1c860 3e 69 64 78 20 3d 20 70 4f 6c 64 49 74 65 6d 2d  >idx = pOldItem-
1c861 3e 69 64 78 3b 0a 20 20 7d 0a 20 20 72 65 74 75  >idx;.  }.  retu
1c862 72 6e 20 70 4e 65 77 3b 0a 7d 0a 53 51 4c 49 54  rn pNew;.}.SQLIT
1c863 45 5f 50 52 49 56 41 54 45 20 53 65 6c 65 63 74  E_PRIVATE Select
1c864 20 2a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44   *sqlite3SelectD
1c865 75 70 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  up(sqlite3 *db, 
1c866 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53 65  Select *p){.  Se
1c867 6c 65 63 74 20 2a 70 4e 65 77 3b 0a 20 20 69 66  lect *pNew;.  if
1c868 28 20 70 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( p==0 ) return 
1c869 30 3b 0a 20 20 70 4e 65 77 20 3d 20 73 71 6c 69  0;.  pNew = sqli
1c86a 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64  te3DbMallocRaw(d
1c86b 62 2c 20 73 69 7a 65 6f 66 28 2a 70 29 20 29 3b  b, sizeof(*p) );
1c86c 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
1c86d 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 70 4e 65   return 0;.  pNe
1c86e 77 2d 3e 70 45 4c 69 73 74 20 3d 20 73 71 6c 69  w->pEList = sqli
1c86f 74 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64  te3ExprListDup(d
1c870 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  b, p->pEList);. 
1c871 20 70 4e 65 77 2d 3e 70 53 72 63 20 3d 20 73 71   pNew->pSrc = sq
1c872 6c 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28  lite3SrcListDup(
1c873 64 62 2c 20 70 2d 3e 70 53 72 63 29 3b 0a 20 20  db, p->pSrc);.  
1c874 70 4e 65 77 2d 3e 70 57 68 65 72 65 20 3d 20 73  pNew->pWhere = s
1c875 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64 62  qlite3ExprDup(db
1c876 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20  , p->pWhere);.  
1c877 70 4e 65 77 2d 3e 70 47 72 6f 75 70 42 79 20 3d  pNew->pGroupBy =
1c878 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
1c879 44 75 70 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75  Dup(db, p->pGrou
1c87a 70 42 79 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 48  pBy);.  pNew->pH
1c87b 61 76 69 6e 67 20 3d 20 73 71 6c 69 74 65 33 45  aving = sqlite3E
1c87c 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 48  xprDup(db, p->pH
1c87d 61 76 69 6e 67 29 3b 0a 20 20 70 4e 65 77 2d 3e  aving);.  pNew->
1c87e 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69 74  pOrderBy = sqlit
1c87f 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
1c880 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a  , p->pOrderBy);.
1c881 20 20 70 4e 65 77 2d 3e 6f 70 20 3d 20 70 2d 3e    pNew->op = p->
1c882 6f 70 3b 0a 20 20 70 4e 65 77 2d 3e 70 50 72 69  op;.  pNew->pPri
1c883 6f 72 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  or = sqlite3Sele
1c884 63 74 44 75 70 28 64 62 2c 20 70 2d 3e 70 50 72  ctDup(db, p->pPr
1c885 69 6f 72 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c  ior);.  pNew->pL
1c886 69 6d 69 74 20 3d 20 73 71 6c 69 74 65 33 45 78  imit = sqlite3Ex
1c887 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4c 69  prDup(db, p->pLi
1c888 6d 69 74 29 3b 0a 20 20 70 4e 65 77 2d 3e 70 4f  mit);.  pNew->pO
1c889 66 66 73 65 74 20 3d 20 73 71 6c 69 74 65 33 45  ffset = sqlite3E
1c88a 78 70 72 44 75 70 28 64 62 2c 20 70 2d 3e 70 4f  xprDup(db, p->pO
1c88b 66 66 73 65 74 29 3b 0a 20 20 70 4e 65 77 2d 3e  ffset);.  pNew->
1c88c 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 70 4e  iLimit = 0;.  pN
1c88d 65 77 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b  ew->iOffset = 0;
1c88e 0a 20 20 70 4e 65 77 2d 3e 73 65 6c 46 6c 61 67  .  pNew->selFlag
1c88f 73 20 3d 20 70 2d 3e 73 65 6c 46 6c 61 67 73 20  s = p->selFlags 
1c890 26 20 7e 53 46 5f 55 73 65 73 45 70 68 65 6d 65  & ~SF_UsesEpheme
1c891 72 61 6c 3b 0a 20 20 70 4e 65 77 2d 3e 70 52 69  ral;.  pNew->pRi
1c892 67 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 70  ghtmost = 0;.  p
1c893 4e 65 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68  New->addrOpenEph
1c894 6d 5b 30 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65  m[0] = -1;.  pNe
1c895 77 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  w->addrOpenEphm[
1c896 31 5d 20 3d 20 2d 31 3b 0a 20 20 70 4e 65 77 2d  1] = -1;.  pNew-
1c897 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
1c898 20 3d 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20   = -1;.  return 
1c899 70 4e 65 77 3b 0a 7d 0a 23 65 6c 73 65 0a 53 51  pNew;.}.#else.SQ
1c89a 4c 49 54 45 5f 50 52 49 56 41 54 45 20 53 65 6c  LITE_PRIVATE Sel
1c89b 65 63 74 20 2a 73 71 6c 69 74 65 33 53 65 6c 65  ect *sqlite3Sele
1c89c 63 74 44 75 70 28 73 71 6c 69 74 65 33 20 2a 64  ctDup(sqlite3 *d
1c89d 62 2c 20 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20  b, Select *p){. 
1c89e 20 61 73 73 65 72 74 28 20 70 3d 3d 30 20 29 3b   assert( p==0 );
1c89f 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
1c8a0 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 64  endif.../*.** Ad
1c8a1 64 20 61 20 6e 65 77 20 65 6c 65 6d 65 6e 74 20  d a new element 
1c8a2 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 61 6e  to the end of an
1c8a3 20 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74   expression list
1c8a4 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73 0a 2a  .  If pList is.*
1c8a5 2a 20 69 6e 69 74 69 61 6c 6c 79 20 4e 55 4c 4c  * initially NULL
1c8a6 2c 20 74 68 65 6e 20 63 72 65 61 74 65 20 61 20  , then create a 
1c8a7 6e 65 77 20 65 78 70 72 65 73 73 69 6f 6e 20 6c  new expression l
1c8a8 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ist..*/.SQLITE_P
1c8a9 52 49 56 41 54 45 20 45 78 70 72 4c 69 73 74 20  RIVATE ExprList 
1c8aa 2a 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74  *sqlite3ExprList
1c8ab 41 70 70 65 6e 64 28 0a 20 20 50 61 72 73 65 20  Append(.  Parse 
1c8ac 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
1c8ad 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
1c8ae 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69  text */.  ExprLi
1c8af 73 74 20 2a 70 4c 69 73 74 2c 20 20 20 20 20 20  st *pList,      
1c8b0 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 77 68 69    /* List to whi
1c8b1 63 68 20 74 6f 20 61 70 70 65 6e 64 2e 20 4d 69  ch to append. Mi
1c8b2 67 68 74 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20  ght be NULL */. 
1c8b3 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 20 20   Expr *pExpr,   
1c8b4 20 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72           /* Expr
1c8b5 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 61 70 70  ession to be app
1c8b6 65 6e 64 65 64 20 2a 2f 0a 20 20 54 6f 6b 65 6e  ended */.  Token
1c8b7 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20 20 20   *pName         
1c8b8 20 20 20 2f 2a 20 41 53 20 6b 65 79 77 6f 72 64     /* AS keyword
1c8b9 20 66 6f 72 20 74 68 65 20 65 78 70 72 65 73 73   for the express
1c8ba 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ion */.){.  sqli
1c8bb 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1c8bc 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 4c 69 73  ->db;.  if( pLis
1c8bd 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4c 69 73  t==0 ){.    pLis
1c8be 74 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  t = sqlite3DbMal
1c8bf 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
1c8c0 6f 66 28 45 78 70 72 4c 69 73 74 29 20 29 3b 0a  of(ExprList) );.
1c8c1 20 20 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30      if( pList==0
1c8c2 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e   ){.      goto n
1c8c3 6f 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20  o_mem;.    }.   
1c8c4 20 61 73 73 65 72 74 28 20 70 4c 69 73 74 2d 3e   assert( pList->
1c8c5 6e 41 6c 6c 6f 63 3d 3d 30 20 29 3b 0a 20 20 7d  nAlloc==0 );.  }
1c8c6 0a 20 20 69 66 28 20 70 4c 69 73 74 2d 3e 6e 41  .  if( pList->nA
1c8c7 6c 6c 6f 63 3c 3d 70 4c 69 73 74 2d 3e 6e 45 78  lloc<=pList->nEx
1c8c8 70 72 20 29 7b 0a 20 20 20 20 73 74 72 75 63 74  pr ){.    struct
1c8c9 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1c8ca 61 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 70  a;.    int n = p
1c8cb 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b  List->nAlloc*2 +
1c8cc 20 34 3b 0a 20 20 20 20 61 20 3d 20 73 71 6c 69   4;.    a = sqli
1c8cd 74 65 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c  te3DbRealloc(db,
1c8ce 20 70 4c 69 73 74 2d 3e 61 2c 20 6e 2a 73 69 7a   pList->a, n*siz
1c8cf 65 6f 66 28 70 4c 69 73 74 2d 3e 61 5b 30 5d 29  eof(pList->a[0])
1c8d0 29 3b 0a 20 20 20 20 69 66 28 20 61 3d 3d 30 20  );.    if( a==0 
1c8d1 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 6e 6f  ){.      goto no
1c8d2 5f 6d 65 6d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  _mem;.    }.    
1c8d3 70 4c 69 73 74 2d 3e 61 20 3d 20 61 3b 0a 20 20  pList->a = a;.  
1c8d4 20 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20    pList->nAlloc 
1c8d5 3d 20 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  = n;.  }.  asser
1c8d6 74 28 20 70 4c 69 73 74 2d 3e 61 21 3d 30 20 29  t( pList->a!=0 )
1c8d7 3b 0a 20 20 69 66 28 20 70 45 78 70 72 20 7c 7c  ;.  if( pExpr ||
1c8d8 20 70 4e 61 6d 65 20 29 7b 0a 20 20 20 20 73 74   pName ){.    st
1c8d9 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1c8da 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 4c 69  em *pItem = &pLi
1c8db 73 74 2d 3e 61 5b 70 4c 69 73 74 2d 3e 6e 45 78  st->a[pList->nEx
1c8dc 70 72 2b 2b 5d 3b 0a 20 20 20 20 6d 65 6d 73 65  pr++];.    memse
1c8dd 74 28 70 49 74 65 6d 2c 20 30 2c 20 73 69 7a 65  t(pItem, 0, size
1c8de 6f 66 28 2a 70 49 74 65 6d 29 29 3b 0a 20 20 20  of(*pItem));.   
1c8df 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d 65 20 3d 20   pItem->zName = 
1c8e0 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54  sqlite3NameFromT
1c8e1 6f 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b  oken(db, pName);
1c8e2 0a 20 20 20 20 70 49 74 65 6d 2d 3e 70 45 78 70  .    pItem->pExp
1c8e3 72 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 70  r = pExpr;.    p
1c8e4 49 74 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30  Item->iAlias = 0
1c8e5 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  ;.  }.  return p
1c8e6 4c 69 73 74 3b 0a 0a 6e 6f 5f 6d 65 6d 3a 20 20  List;..no_mem:  
1c8e7 20 20 20 0a 20 20 2f 2a 20 41 76 6f 69 64 20 6c     .  /* Avoid l
1c8e8 65 61 6b 69 6e 67 20 6d 65 6d 6f 72 79 20 69 66  eaking memory if
1c8e9 20 6d 61 6c 6c 6f 63 20 68 61 73 20 66 61 69 6c   malloc has fail
1c8ea 65 64 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ed. */.  sqlite3
1c8eb 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1c8ec 45 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Expr);.  sqlite3
1c8ed 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
1c8ee 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 72 65 74  b, pList);.  ret
1c8ef 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
1c8f0 49 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  If the expressio
1c8f1 6e 20 6c 69 73 74 20 70 45 4c 69 73 74 20 63 6f  n list pEList co
1c8f2 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74 68 61 6e  ntains more than
1c8f3 20 69 4c 69 6d 69 74 20 65 6c 65 6d 65 6e 74 73   iLimit elements
1c8f4 2c 0a 2a 2a 20 6c 65 61 76 65 20 61 6e 20 65 72  ,.** leave an er
1c8f5 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 20 70  ror message in p
1c8f6 50 61 72 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  Parse..*/.SQLITE
1c8f7 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1c8f8 6c 69 74 65 33 45 78 70 72 4c 69 73 74 43 68 65  lite3ExprListChe
1c8f9 63 6b 4c 65 6e 67 74 68 28 0a 20 20 50 61 72 73  ckLength(.  Pars
1c8fa 65 20 2a 70 50 61 72 73 65 2c 0a 20 20 45 78 70  e *pParse,.  Exp
1c8fb 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 0a 20  rList *pEList,. 
1c8fc 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 62   const char *zOb
1c8fd 6a 65 63 74 0a 29 7b 0a 20 20 69 6e 74 20 6d 78  ject.){.  int mx
1c8fe 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61   = pParse->db->a
1c8ff 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d  Limit[SQLITE_LIM
1c900 49 54 5f 43 4f 4c 55 4d 4e 5d 3b 0a 20 20 74 65  IT_COLUMN];.  te
1c901 73 74 63 61 73 65 28 20 70 45 4c 69 73 74 20 26  stcase( pEList &
1c902 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  & pEList->nExpr=
1c903 3d 6d 78 20 29 3b 0a 20 20 74 65 73 74 63 61 73  =mx );.  testcas
1c904 65 28 20 70 45 4c 69 73 74 20 26 26 20 70 45 4c  e( pEList && pEL
1c905 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 6d 78 2b 31  ist->nExpr==mx+1
1c906 20 29 3b 0a 20 20 69 66 28 20 70 45 4c 69 73 74   );.  if( pEList
1c907 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   && pEList->nExp
1c908 72 3e 6d 78 20 29 7b 0a 20 20 20 20 73 71 6c 69  r>mx ){.    sqli
1c909 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1c90a 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f  se, "too many co
1c90b 6c 75 6d 6e 73 20 69 6e 20 25 73 22 2c 20 7a 4f  lumns in %s", zO
1c90c 62 6a 65 63 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  bject);.  }.}../
1c90d 2a 0a 2a 2a 20 44 65 6c 65 74 65 20 61 6e 20 65  *.** Delete an e
1c90e 6e 74 69 72 65 20 65 78 70 72 65 73 73 69 6f 6e  ntire expression
1c90f 20 6c 69 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45   list..*/.SQLITE
1c910 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1c911 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
1c912 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  ete(sqlite3 *db,
1c913 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1c914 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
1c915 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1c916 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28  em *pItem;.  if(
1c917 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75   pList==0 ) retu
1c918 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4c  rn;.  assert( pL
1c919 69 73 74 2d 3e 61 21 3d 30 20 7c 7c 20 28 70 4c  ist->a!=0 || (pL
1c91a 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 30 20 26 26  ist->nExpr==0 &&
1c91b 20 70 4c 69 73 74 2d 3e 6e 41 6c 6c 6f 63 3d 3d   pList->nAlloc==
1c91c 30 29 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  0) );.  assert( 
1c91d 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3c 3d 70 4c  pList->nExpr<=pL
1c91e 69 73 74 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20  ist->nAlloc );. 
1c91f 20 66 6f 72 28 70 49 74 65 6d 3d 70 4c 69 73 74   for(pItem=pList
1c920 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c 70 4c 69 73  ->a, i=0; i<pLis
1c921 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70  t->nExpr; i++, p
1c922 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 73 71 6c  Item++){.    sql
1c923 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
1c924 62 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 29  b, pItem->pExpr)
1c925 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  ;.    sqlite3DbF
1c926 72 65 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a  ree(db, pItem->z
1c927 4e 61 6d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  Name);.  }.  sql
1c928 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
1c929 4c 69 73 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69  List->a);.  sqli
1c92a 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c  te3DbFree(db, pL
1c92b 69 73 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ist);.}../*.** T
1c92c 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20 61 72  hese routines ar
1c92d 65 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61 63  e Walker callbac
1c92e 6b 73 2e 20 20 57 61 6c 6b 65 72 2e 75 2e 70 69  ks.  Walker.u.pi
1c92f 20 69 73 20 61 20 70 6f 69 6e 74 65 72 0a 2a 2a   is a pointer.**
1c930 20 74 6f 20 61 6e 20 69 6e 74 65 67 65 72 2e 20   to an integer. 
1c931 20 54 68 65 73 65 20 72 6f 75 74 69 6e 65 73 20   These routines 
1c932 61 72 65 20 63 68 65 63 6b 69 6e 67 20 61 6e 20  are checking an 
1c933 65 78 70 72 65 73 73 69 6f 6e 20 74 6f 20 73 65  expression to se
1c934 65 0a 2a 2a 20 69 66 20 69 74 20 69 73 20 61 20  e.** if it is a 
1c935 63 6f 6e 73 74 61 6e 74 2e 20 20 53 65 74 20 2a  constant.  Set *
1c936 57 61 6c 6b 65 72 2e 75 2e 70 69 20 74 6f 20 30  Walker.u.pi to 0
1c937 20 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69   if the expressi
1c938 6f 6e 20 69 73 0a 2a 2a 20 6e 6f 74 20 63 6f 6e  on is.** not con
1c939 73 74 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  stant..**.** The
1c93a 73 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74  se callback rout
1c93b 69 6e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f  ines are used to
1c93c 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 66   implement the f
1c93d 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  ollowing:.**.** 
1c93e 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
1c93f 73 43 6f 6e 73 74 61 6e 74 28 29 0a 2a 2a 20 20  sConstant().**  
1c940 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 73     sqlite3ExprIs
1c941 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28  ConstantNotJoin(
1c942 29 0a 2a 2a 20 20 20 20 20 73 71 6c 69 74 65 33  ).**     sqlite3
1c943 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 4f 72  ExprIsConstantOr
1c944 46 75 6e 63 74 69 6f 6e 28 29 0a 2a 2a 0a 2a 2f  Function().**.*/
1c945 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78 70 72  .static int expr
1c946 4e 6f 64 65 49 73 43 6f 6e 73 74 61 6e 74 28 57  NodeIsConstant(W
1c947 61 6c 6b 65 72 20 2a 70 57 61 6c 6b 65 72 2c 20  alker *pWalker, 
1c948 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 0a 20  Expr *pExpr){.. 
1c949 20 2f 2a 20 49 66 20 70 57 61 6c 6b 65 72 2d 3e   /* If pWalker->
1c94a 75 2e 69 20 69 73 20 33 20 74 68 65 6e 20 61 6e  u.i is 3 then an
1c94b 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20 65 78  y term of the ex
1c94c 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f  pression that co
1c94d 6d 65 73 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68  mes from.  ** th
1c94e 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c  e ON or USING cl
1c94f 61 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 20  auses of a join 
1c950 64 69 73 71 75 61 6c 69 66 69 65 73 20 74 68 65  disqualifies the
1c951 20 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 2a 2a   expression.  **
1c952 20 66 72 6f 6d 20 62 65 69 6e 67 20 63 6f 6e 73   from being cons
1c953 69 64 65 72 65 64 20 63 6f 6e 73 74 61 6e 74 2e  idered constant.
1c954 20 2a 2f 0a 20 20 69 66 28 20 70 57 61 6c 6b 65   */.  if( pWalke
1c955 72 2d 3e 75 2e 69 3d 3d 33 20 26 26 20 45 78 70  r->u.i==3 && Exp
1c956 72 48 61 73 41 6e 79 50 72 6f 70 65 72 74 79 28  rHasAnyProperty(
1c957 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d 4a 6f  pExpr, EP_FromJo
1c958 69 6e 29 20 29 7b 0a 20 20 20 20 70 57 61 6c 6b  in) ){.    pWalk
1c959 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20 20 20  er->u.i = 0;.   
1c95a 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1c95b 74 3b 0a 20 20 7d 0a 0a 20 20 73 77 69 74 63 68  t;.  }..  switch
1c95c 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b 0a 20  ( pExpr->op ){. 
1c95d 20 20 20 2f 2a 20 43 6f 6e 73 69 64 65 72 20 66     /* Consider f
1c95e 75 6e 63 74 69 6f 6e 73 20 74 6f 20 62 65 20 63  unctions to be c
1c95f 6f 6e 73 74 61 6e 74 20 69 66 20 61 6c 6c 20 74  onstant if all t
1c960 68 65 69 72 20 61 72 67 75 6d 65 6e 74 73 20 61  heir arguments a
1c961 72 65 20 63 6f 6e 73 74 61 6e 74 0a 20 20 20 20  re constant.    
1c962 2a 2a 20 61 6e 64 20 70 57 61 6c 6b 65 72 2d 3e  ** and pWalker->
1c963 75 2e 69 3d 3d 32 20 2a 2f 0a 20 20 20 20 63 61  u.i==2 */.    ca
1c964 73 65 20 54 4b 5f 46 55 4e 43 54 49 4f 4e 3a 0a  se TK_FUNCTION:.
1c965 20 20 20 20 20 20 69 66 28 20 70 57 61 6c 6b 65        if( pWalke
1c966 72 2d 3e 75 2e 69 3d 3d 32 20 29 20 72 65 74 75  r->u.i==2 ) retu
1c967 72 6e 20 30 3b 0a 20 20 20 20 20 20 2f 2a 20 46  rn 0;.      /* F
1c968 61 6c 6c 20 74 68 72 6f 75 67 68 20 2a 2f 0a 20  all through */. 
1c969 20 20 20 63 61 73 65 20 54 4b 5f 49 44 3a 0a 20     case TK_ID:. 
1c96a 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d     case TK_COLUM
1c96b 4e 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 44  N:.    case TK_D
1c96c 4f 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  OT:.    case TK_
1c96d 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a 20 20  AGG_FUNCTION:.  
1c96e 20 20 63 61 73 65 20 54 4b 5f 41 47 47 5f 43 4f    case TK_AGG_CO
1c96f 4c 55 4d 4e 3a 0a 23 69 66 6e 64 65 66 20 53 51  LUMN:.#ifndef SQ
1c970 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1c971 52 59 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53  RY.    case TK_S
1c972 45 4c 45 43 54 3a 0a 20 20 20 20 63 61 73 65 20  ELECT:.    case 
1c973 54 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 20  TK_EXISTS:.     
1c974 20 74 65 73 74 63 61 73 65 28 20 70 45 78 70 72   testcase( pExpr
1c975 2d 3e 6f 70 3d 3d 54 4b 5f 53 45 4c 45 43 54 20  ->op==TK_SELECT 
1c976 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1c977 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
1c978 5f 45 58 49 53 54 53 20 29 3b 0a 23 65 6e 64 69  _EXISTS );.#endi
1c979 66 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  f.      testcase
1c97a 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
1c97b 49 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ID );.      test
1c97c 63 61 73 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d  case( pExpr->op=
1c97d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  =TK_COLUMN );.  
1c97e 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
1c97f 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 44 4f 54 20  xpr->op==TK_DOT 
1c980 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1c981 65 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b  e( pExpr->op==TK
1c982 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 20 29 3b  _AGG_FUNCTION );
1c983 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1c984 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 41   pExpr->op==TK_A
1c985 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20  GG_COLUMN );.   
1c986 20 20 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 69 20     pWalker->u.i 
1c987 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75 72  = 0;.      retur
1c988 6e 20 57 52 43 5f 41 62 6f 72 74 3b 0a 20 20 20  n WRC_Abort;.   
1c989 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20 20   default:.      
1c98a 72 65 74 75 72 6e 20 57 52 43 5f 43 6f 6e 74 69  return WRC_Conti
1c98b 6e 75 65 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69  nue;.  }.}.stati
1c98c 63 20 69 6e 74 20 73 65 6c 65 63 74 4e 6f 64 65  c int selectNode
1c98d 49 73 43 6f 6e 73 74 61 6e 74 28 57 61 6c 6b 65  IsConstant(Walke
1c98e 72 20 2a 70 57 61 6c 6b 65 72 2c 20 53 65 6c 65  r *pWalker, Sele
1c98f 63 74 20 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20  ct *NotUsed){.  
1c990 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1c991 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 70 57 61  (NotUsed);.  pWa
1c992 6c 6b 65 72 2d 3e 75 2e 69 20 3d 20 30 3b 0a 20  lker->u.i = 0;. 
1c993 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72   return WRC_Abor
1c994 74 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  t;.}.static int 
1c995 65 78 70 72 49 73 43 6f 6e 73 74 28 45 78 70 72  exprIsConst(Expr
1c996 20 2a 70 2c 20 69 6e 74 20 69 6e 69 74 46 6c 61   *p, int initFla
1c997 67 29 7b 0a 20 20 57 61 6c 6b 65 72 20 77 3b 0a  g){.  Walker w;.
1c998 20 20 77 2e 75 2e 69 20 3d 20 69 6e 69 74 46 6c    w.u.i = initFl
1c999 61 67 3b 0a 20 20 77 2e 78 45 78 70 72 43 61 6c  ag;.  w.xExprCal
1c99a 6c 62 61 63 6b 20 3d 20 65 78 70 72 4e 6f 64 65  lback = exprNode
1c99b 49 73 43 6f 6e 73 74 61 6e 74 3b 0a 20 20 77 2e  IsConstant;.  w.
1c99c 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
1c99d 3d 20 73 65 6c 65 63 74 4e 6f 64 65 49 73 43 6f  = selectNodeIsCo
1c99e 6e 73 74 61 6e 74 3b 0a 20 20 73 71 6c 69 74 65  nstant;.  sqlite
1c99f 33 57 61 6c 6b 45 78 70 72 28 26 77 2c 20 70 29  3WalkExpr(&w, p)
1c9a0 3b 0a 20 20 72 65 74 75 72 6e 20 77 2e 75 2e 69  ;.  return w.u.i
1c9a1 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c 6b 20  ;.}../*.** Walk 
1c9a2 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 72  an expression tr
1c9a3 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20 69 66  ee.  Return 1 if
1c9a4 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1c9a5 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a 20 61  is constant.** a
1c9a6 6e 64 20 30 20 69 66 20 69 74 20 69 6e 76 6f 6c  nd 0 if it invol
1c9a7 76 65 73 20 76 61 72 69 61 62 6c 65 73 20 6f 72  ves variables or
1c9a8 20 66 75 6e 63 74 69 6f 6e 20 63 61 6c 6c 73 2e   function calls.
1c9a9 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70  .**.** For the p
1c9aa 75 72 70 6f 73 65 73 20 6f 66 20 74 68 69 73 20  urposes of this 
1c9ab 66 75 6e 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62  function, a doub
1c9ac 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
1c9ad 20 28 65 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20   (ex: "abc").** 
1c9ae 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 61 20  is considered a 
1c9af 76 61 72 69 61 62 6c 65 20 62 75 74 20 61 20 73  variable but a s
1c9b0 69 6e 67 6c 65 2d 71 75 6f 74 65 64 20 73 74 72  ingle-quoted str
1c9b1 69 6e 67 20 28 65 78 3a 20 27 61 62 63 27 29 20  ing (ex: 'abc') 
1c9b2 69 73 0a 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74  is.** a constant
1c9b3 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1c9b4 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 45  ATE int sqlite3E
1c9b5 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 45 78  xprIsConstant(Ex
1c9b6 70 72 20 2a 70 29 7b 0a 20 20 72 65 74 75 72 6e  pr *p){.  return
1c9b7 20 65 78 70 72 49 73 43 6f 6e 73 74 28 70 2c 20   exprIsConst(p, 
1c9b8 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 61 6c  1);.}../*.** Wal
1c9b9 6b 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  k an expression 
1c9ba 74 72 65 65 2e 20 20 52 65 74 75 72 6e 20 31 20  tree.  Return 1 
1c9bb 69 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f  if the expressio
1c9bc 6e 20 69 73 20 63 6f 6e 73 74 61 6e 74 0a 2a 2a  n is constant.**
1c9bd 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 20 6f 72   that does no or
1c9be 69 67 69 6e 61 74 65 20 66 72 6f 6d 20 74 68 65  iginate from the
1c9bf 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20 63 6c 61   ON or USING cla
1c9c0 75 73 65 73 20 6f 66 20 61 20 6a 6f 69 6e 2e 0a  uses of a join..
1c9c1 2a 2a 20 52 65 74 75 72 6e 20 30 20 69 66 20 69  ** Return 0 if i
1c9c2 74 20 69 6e 76 6f 6c 76 65 73 20 76 61 72 69 61  t involves varia
1c9c3 62 6c 65 73 20 6f 72 20 66 75 6e 63 74 69 6f 6e  bles or function
1c9c4 20 63 61 6c 6c 73 20 6f 72 20 74 65 72 6d 73 20   calls or terms 
1c9c5 66 72 6f 6d 0a 2a 2a 20 61 6e 20 4f 4e 20 6f 72  from.** an ON or
1c9c6 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a 2a   USING clause..*
1c9c7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c9c8 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
1c9c9 49 73 43 6f 6e 73 74 61 6e 74 4e 6f 74 4a 6f 69  IsConstantNotJoi
1c9ca 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 72 65  n(Expr *p){.  re
1c9cb 74 75 72 6e 20 65 78 70 72 49 73 43 6f 6e 73 74  turn exprIsConst
1c9cc 28 70 2c 20 33 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  (p, 3);.}../*.**
1c9cd 20 57 61 6c 6b 20 61 6e 20 65 78 70 72 65 73 73   Walk an express
1c9ce 69 6f 6e 20 74 72 65 65 2e 20 20 52 65 74 75 72  ion tree.  Retur
1c9cf 6e 20 31 20 69 66 20 74 68 65 20 65 78 70 72 65  n 1 if the expre
1c9d0 73 73 69 6f 6e 20 69 73 20 63 6f 6e 73 74 61 6e  ssion is constan
1c9d1 74 0a 2a 2a 20 6f 72 20 61 20 66 75 6e 63 74 69  t.** or a functi
1c9d2 6f 6e 20 63 61 6c 6c 20 77 69 74 68 20 63 6f 6e  on call with con
1c9d3 73 74 61 6e 74 20 61 72 67 75 6d 65 6e 74 73 2e  stant arguments.
1c9d4 20 20 52 65 74 75 72 6e 20 61 6e 64 20 30 20 69    Return and 0 i
1c9d5 66 20 74 68 65 72 65 0a 2a 2a 20 61 72 65 20 61  f there.** are a
1c9d6 6e 79 20 76 61 72 69 61 62 6c 65 73 2e 0a 2a 2a  ny variables..**
1c9d7 0a 2a 2a 20 46 6f 72 20 74 68 65 20 70 75 72 70  .** For the purp
1c9d8 6f 73 65 73 20 6f 66 20 74 68 69 73 20 66 75 6e  oses of this fun
1c9d9 63 74 69 6f 6e 2c 20 61 20 64 6f 75 62 6c 65 2d  ction, a double-
1c9da 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 28 65  quoted string (e
1c9db 78 3a 20 22 61 62 63 22 29 0a 2a 2a 20 69 73 20  x: "abc").** is 
1c9dc 63 6f 6e 73 69 64 65 72 65 64 20 61 20 76 61 72  considered a var
1c9dd 69 61 62 6c 65 20 62 75 74 20 61 20 73 69 6e 67  iable but a sing
1c9de 6c 65 2d 71 75 6f 74 65 64 20 73 74 72 69 6e 67  le-quoted string
1c9df 20 28 65 78 3a 20 27 61 62 63 27 29 20 69 73 0a   (ex: 'abc') is.
1c9e0 2a 2a 20 61 20 63 6f 6e 73 74 61 6e 74 2e 0a 2a  ** a constant..*
1c9e1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1c9e2 20 69 6e 74 20 73 71 6c 69 74 65 33 45 78 70 72   int sqlite3Expr
1c9e3 49 73 43 6f 6e 73 74 61 6e 74 4f 72 46 75 6e 63  IsConstantOrFunc
1c9e4 74 69 6f 6e 28 45 78 70 72 20 2a 70 29 7b 0a 20  tion(Expr *p){. 
1c9e5 20 72 65 74 75 72 6e 20 65 78 70 72 49 73 43 6f   return exprIsCo
1c9e6 6e 73 74 28 70 2c 20 32 29 3b 0a 7d 0a 0a 2f 2a  nst(p, 2);.}../*
1c9e7 0a 2a 2a 20 49 66 20 74 68 65 20 65 78 70 72 65  .** If the expre
1c9e8 73 73 69 6f 6e 20 70 20 63 6f 64 65 73 20 61 20  ssion p codes a 
1c9e9 63 6f 6e 73 74 61 6e 74 20 69 6e 74 65 67 65 72  constant integer
1c9ea 20 74 68 61 74 20 69 73 20 73 6d 61 6c 6c 20 65   that is small e
1c9eb 6e 6f 75 67 68 0a 2a 2a 20 74 6f 20 66 69 74 20  nough.** to fit 
1c9ec 69 6e 20 61 20 33 32 2d 62 69 74 20 69 6e 74 65  in a 32-bit inte
1c9ed 67 65 72 2c 20 72 65 74 75 72 6e 20 31 20 61 6e  ger, return 1 an
1c9ee 64 20 70 75 74 20 74 68 65 20 76 61 6c 75 65 20  d put the value 
1c9ef 6f 66 20 74 68 65 20 69 6e 74 65 67 65 72 0a 2a  of the integer.*
1c9f0 2a 20 69 6e 20 2a 70 56 61 6c 75 65 2e 20 20 49  * in *pValue.  I
1c9f1 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1c9f2 20 69 73 20 6e 6f 74 20 61 6e 20 69 6e 74 65 67   is not an integ
1c9f3 65 72 20 6f 72 20 69 66 20 69 74 20 69 73 20 74  er or if it is t
1c9f4 6f 6f 20 62 69 67 0a 2a 2a 20 74 6f 20 66 69 74  oo big.** to fit
1c9f5 20 69 6e 20 61 20 73 69 67 6e 65 64 20 33 32 2d   in a signed 32-
1c9f6 62 69 74 20 69 6e 74 65 67 65 72 2c 20 72 65 74  bit integer, ret
1c9f7 75 72 6e 20 30 20 61 6e 64 20 6c 65 61 76 65 20  urn 0 and leave 
1c9f8 2a 70 56 61 6c 75 65 20 75 6e 63 68 61 6e 67 65  *pValue unchange
1c9f9 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  d..*/.SQLITE_PRI
1c9fa 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1c9fb 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 45 78  ExprIsInteger(Ex
1c9fc 70 72 20 2a 70 2c 20 69 6e 74 20 2a 70 56 61 6c  pr *p, int *pVal
1c9fd 75 65 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  ue){.  int rc = 
1c9fe 30 3b 0a 20 20 69 66 28 20 70 2d 3e 66 6c 61 67  0;.  if( p->flag
1c9ff 73 20 26 20 45 50 5f 49 6e 74 56 61 6c 75 65 20  s & EP_IntValue 
1ca00 29 7b 0a 20 20 20 20 2a 70 56 61 6c 75 65 20 3d  ){.    *pValue =
1ca01 20 70 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20   p->iTable;.    
1ca02 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
1ca03 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29 7b  switch( p->op ){
1ca04 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54  .    case TK_INT
1ca05 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 72 63  EGER: {.      rc
1ca06 20 3d 20 73 71 6c 69 74 65 33 47 65 74 49 6e 74   = sqlite3GetInt
1ca07 33 32 28 28 63 68 61 72 2a 29 70 2d 3e 74 6f 6b  32((char*)p->tok
1ca08 65 6e 2e 7a 2c 20 70 56 61 6c 75 65 29 3b 0a 20  en.z, pValue);. 
1ca09 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1ca0a 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 55 50  }.    case TK_UP
1ca0b 4c 55 53 3a 20 7b 0a 20 20 20 20 20 20 72 63 20  LUS: {.      rc 
1ca0c 3d 20 73 71 6c 69 74 65 33 45 78 70 72 49 73 49  = sqlite3ExprIsI
1ca0d 6e 74 65 67 65 72 28 70 2d 3e 70 4c 65 66 74 2c  nteger(p->pLeft,
1ca0e 20 70 56 61 6c 75 65 29 3b 0a 20 20 20 20 20 20   pValue);.      
1ca0f 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1ca10 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a   case TK_UMINUS:
1ca11 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 76 3b 0a   {.      int v;.
1ca12 20 20 20 20 20 20 69 66 28 20 73 71 6c 69 74 65        if( sqlite
1ca13 33 45 78 70 72 49 73 49 6e 74 65 67 65 72 28 70  3ExprIsInteger(p
1ca14 2d 3e 70 4c 65 66 74 2c 20 26 76 29 20 29 7b 0a  ->pLeft, &v) ){.
1ca15 20 20 20 20 20 20 20 20 2a 70 56 61 6c 75 65 20          *pValue 
1ca16 3d 20 2d 76 3b 0a 20 20 20 20 20 20 20 20 72 63  = -v;.        rc
1ca17 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20   = 1;.      }.  
1ca18 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1ca19 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 62 72  .    default: br
1ca1a 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72  eak;.  }.  if( r
1ca1b 63 20 29 7b 0a 20 20 20 20 70 2d 3e 6f 70 20 3d  c ){.    p->op =
1ca1c 20 54 4b 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20   TK_INTEGER;.   
1ca1d 20 70 2d 3e 66 6c 61 67 73 20 7c 3d 20 45 50 5f   p->flags |= EP_
1ca1e 49 6e 74 56 61 6c 75 65 3b 0a 20 20 20 20 70 2d  IntValue;.    p-
1ca1f 3e 69 54 61 62 6c 65 20 3d 20 2a 70 56 61 6c 75  >iTable = *pValu
1ca20 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  e;.  }.  return 
1ca21 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74  rc;.}../*.** Ret
1ca22 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65 20  urn TRUE if the 
1ca23 67 69 76 65 6e 20 73 74 72 69 6e 67 20 69 73 20  given string is 
1ca24 61 20 72 6f 77 2d 69 64 20 63 6f 6c 75 6d 6e 20  a row-id column 
1ca25 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  name..*/.SQLITE_
1ca26 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1ca27 74 65 33 49 73 52 6f 77 69 64 28 63 6f 6e 73 74  te3IsRowid(const
1ca28 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28   char *z){.  if(
1ca29 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
1ca2a 7a 2c 20 22 5f 52 4f 57 49 44 5f 22 29 3d 3d 30  z, "_ROWID_")==0
1ca2b 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69   ) return 1;.  i
1ca2c 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1ca2d 70 28 7a 2c 20 22 52 4f 57 49 44 22 29 3d 3d 30  p(z, "ROWID")==0
1ca2e 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 69   ) return 1;.  i
1ca2f 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1ca30 70 28 7a 2c 20 22 4f 49 44 22 29 3d 3d 30 20 29  p(z, "OID")==0 )
1ca31 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 72 65 74   return 1;.  ret
1ca32 75 72 6e 20 30 3b 0a 7d 0a 0a 23 69 66 64 65 66  urn 0;.}..#ifdef
1ca33 20 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 69   SQLITE_TEST.  i
1ca34 6e 74 20 73 71 6c 69 74 65 33 5f 65 6e 61 62 6c  nt sqlite3_enabl
1ca35 65 5f 69 6e 5f 6f 70 74 20 3d 20 31 3b 0a 23 65  e_in_opt = 1;.#e
1ca36 6c 73 65 0a 20 20 23 64 65 66 69 6e 65 20 73 71  lse.  #define sq
1ca37 6c 69 74 65 33 5f 65 6e 61 62 6c 65 5f 69 6e 5f  lite3_enable_in_
1ca38 6f 70 74 20 31 0a 23 65 6e 64 69 66 0a 0a 2f 2a  opt 1.#endif../*
1ca39 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20  .** Return true 
1ca3a 69 66 20 74 68 65 20 49 4e 20 6f 70 65 72 61 74  if the IN operat
1ca3b 6f 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  or optimization 
1ca3c 69 73 20 65 6e 61 62 6c 65 64 20 61 6e 64 0a 2a  is enabled and.*
1ca3d 2a 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  * the SELECT sta
1ca3e 74 65 6d 65 6e 74 20 70 20 65 78 69 73 74 73 20  tement p exists 
1ca3f 61 6e 64 20 69 73 20 6f 66 20 74 68 65 0a 2a 2a  and is of the.**
1ca40 20 73 69 6d 70 6c 65 20 66 6f 72 6d 3a 0a 2a 2a   simple form:.**
1ca41 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c  .**     SELECT <
1ca42 63 6f 6c 75 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61  column> FROM <ta
1ca43 62 6c 65 3e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  ble>.**.** If th
1ca44 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
1ca45 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62  it may be possib
1ca46 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69  le to use an exi
1ca47 73 74 69 6e 67 20 74 61 62 6c 65 0a 2a 2a 20 6f  sting table.** o
1ca48 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61 64 20  r index instead 
1ca49 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20 61 6e  of generating an
1ca4a 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62 6c 65   epheremal table
1ca4b 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
1ca4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
1ca4d 59 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 43  Y.static int isC
1ca4e 61 6e 64 69 64 61 74 65 46 6f 72 49 6e 4f 70 74  andidateForInOpt
1ca4f 28 53 65 6c 65 63 74 20 2a 70 29 7b 0a 20 20 53  (Select *p){.  S
1ca50 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20  rcList *pSrc;.  
1ca51 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1ca52 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
1ca53 0a 20 20 69 66 28 20 21 73 71 6c 69 74 65 33 5f  .  if( !sqlite3_
1ca54 65 6e 61 62 6c 65 5f 69 6e 5f 6f 70 74 20 29 20  enable_in_opt ) 
1ca55 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 49 4e 20  return 0; /* IN 
1ca56 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 6d 75 73  optimization mus
1ca57 74 20 62 65 20 65 6e 61 62 6c 65 64 20 2a 2f 0a  t be enabled */.
1ca58 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65 74    if( p==0 ) ret
1ca59 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20  urn 0;          
1ca5a 20 20 20 20 20 20 20 20 20 2f 2a 20 72 69 67 68           /* righ
1ca5b 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 49  t-hand side of I
1ca5c 4e 20 69 73 20 53 45 4c 45 43 54 20 2a 2f 0a 20  N is SELECT */. 
1ca5d 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20 29   if( p->pPrior )
1ca5e 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
1ca5f 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 74 20 61          /* Not a
1ca60 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
1ca61 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c   */.  if( p->sel
1ca62 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
1ca63 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
1ca64 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  e) ){.      retu
1ca65 72 6e 20 30 3b 20 2f 2a 20 4e 6f 20 44 49 53 54  rn 0; /* No DIST
1ca66 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 61 6e 64  INCT keyword and
1ca67 20 6e 6f 20 61 67 67 72 65 67 61 74 65 20 66 75   no aggregate fu
1ca68 6e 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 7d 0a 20  nctions */.  }. 
1ca69 20 69 66 28 20 70 2d 3e 70 47 72 6f 75 70 42 79   if( p->pGroupBy
1ca6a 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1ca6b 20 20 20 20 20 20 20 20 2f 2a 20 48 61 73 20 6e          /* Has n
1ca6c 6f 20 47 52 4f 55 50 20 42 59 20 63 6c 61 75 73  o GROUP BY claus
1ca6d 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 4c  e */.  if( p->pL
1ca6e 69 6d 69 74 20 29 20 72 65 74 75 72 6e 20 30 3b  imit ) return 0;
1ca6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ca70 20 48 61 73 20 6e 6f 20 4c 49 4d 49 54 20 63 6c   Has no LIMIT cl
1ca71 61 75 73 65 20 2a 2f 0a 20 20 69 66 28 20 70 2d  ause */.  if( p-
1ca72 3e 70 4f 66 66 73 65 74 20 29 20 72 65 74 75 72  >pOffset ) retur
1ca73 6e 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e 70 57  n 0;.  if( p->pW
1ca74 68 65 72 65 20 29 20 72 65 74 75 72 6e 20 30 3b  here ) return 0;
1ca75 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1ca76 20 48 61 73 20 6e 6f 20 57 48 45 52 45 20 63 6c   Has no WHERE cl
1ca77 61 75 73 65 20 2a 2f 0a 20 20 70 53 72 63 20 3d  ause */.  pSrc =
1ca78 20 70 2d 3e 70 53 72 63 3b 0a 20 20 69 66 28 20   p->pSrc;.  if( 
1ca79 70 53 72 63 3d 3d 30 20 29 20 72 65 74 75 72 6e  pSrc==0 ) return
1ca7a 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   0;             
1ca7b 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74     /* A single t
1ca7c 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f 4d  able in the FROM
1ca7d 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69 66 28   clause */.  if(
1ca7e 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29   pSrc->nSrc!=1 )
1ca7f 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1ca80 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c   pSrc->a[0].pSel
1ca81 65 63 74 20 29 20 72 65 74 75 72 6e 20 30 3b 20  ect ) return 0; 
1ca82 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61 75      /* FROM clau
1ca83 73 65 20 69 73 20 6e 6f 74 20 61 20 73 75 62 71  se is not a subq
1ca84 75 65 72 79 20 2a 2f 0a 20 20 70 54 61 62 20 3d  uery */.  pTab =
1ca85 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54 61 62   pSrc->a[0].pTab
1ca86 3b 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  ;.  if( pTab==0 
1ca87 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66  ) return 0;.  if
1ca88 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
1ca89 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
1ca8a 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61       /* FROM cla
1ca8b 75 73 65 20 69 73 20 6e 6f 74 20 61 20 76 69 65  use is not a vie
1ca8c 77 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72  w */.  if( IsVir
1ca8d 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65 74  tual(pTab) ) ret
1ca8e 75 72 6e 20 30 3b 20 20 20 20 20 20 20 20 2f 2a  urn 0;        /*
1ca8f 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6e 6f 74   FROM clause not
1ca90 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1ca91 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d 20 70   */.  pEList = p
1ca92 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
1ca93 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
1ca94 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20   ) return 0;    
1ca95 20 20 20 2f 2a 20 4f 6e 65 20 63 6f 6c 75 6d 6e     /* One column
1ca96 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
1ca97 65 74 20 2a 2f 0a 20 20 69 66 28 20 70 45 4c 69  et */.  if( pELi
1ca98 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e  st->a[0].pExpr->
1ca99 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 29 20  op!=TK_COLUMN ) 
1ca9a 72 65 74 75 72 6e 20 30 3b 20 2f 2a 20 52 65 73  return 0; /* Res
1ca9b 75 6c 74 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20  ult is a column 
1ca9c 2a 2f 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d  */.  return 1;.}
1ca9d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
1ca9e 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20  E_OMIT_SUBQUERY 
1ca9f 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66  */../*.** This f
1caa0 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20  unction is used 
1caa1 62 79 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74  by the implement
1caa2 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 49 4e 20  ation of the IN 
1caa3 28 2e 2e 2e 29 20 6f 70 65 72 61 74 6f 72 2e 0a  (...) operator..
1caa4 2a 2a 20 49 74 27 73 20 6a 6f 62 20 69 73 20 74  ** It's job is t
1caa5 6f 20 66 69 6e 64 20 6f 72 20 63 72 65 61 74 65  o find or create
1caa6 20 61 20 62 2d 74 72 65 65 20 73 74 72 75 63 74   a b-tree struct
1caa7 75 72 65 20 74 68 61 74 20 6d 61 79 20 62 65 20  ure that may be 
1caa8 75 73 65 64 0a 2a 2a 20 65 69 74 68 65 72 20 74  used.** either t
1caa9 6f 20 74 65 73 74 20 66 6f 72 20 6d 65 6d 62 65  o test for membe
1caaa 72 73 68 69 70 20 6f 66 20 74 68 65 20 28 2e 2e  rship of the (..
1caab 2e 29 20 73 65 74 20 6f 72 20 74 6f 20 69 74 65  .) set or to ite
1caac 72 61 74 65 20 74 68 72 6f 75 67 68 0a 2a 2a 20  rate through.** 
1caad 69 74 73 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69  its members, ski
1caae 70 70 69 6e 67 20 64 75 70 6c 69 63 61 74 65 73  pping duplicates
1caaf 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 75 72 73  ..**.** The curs
1cab0 6f 72 20 6f 70 65 6e 65 64 20 6f 6e 20 74 68 65  or opened on the
1cab1 20 73 74 72 75 63 74 75 72 65 20 28 64 61 74 61   structure (data
1cab2 62 61 73 65 20 74 61 62 6c 65 2c 20 64 61 74 61  base table, data
1cab3 62 61 73 65 20 69 6e 64 65 78 20 0a 2a 2a 20 6f  base index .** o
1cab4 72 20 65 70 68 65 72 6d 61 6c 20 74 61 62 6c 65  r ephermal table
1cab5 29 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 70  ) is stored in p
1cab6 58 2d 3e 69 54 61 62 6c 65 20 62 65 66 6f 72 65  X->iTable before
1cab7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72   this function r
1cab8 65 74 75 72 6e 73 2e 0a 2a 2a 20 54 68 65 20 72  eturns..** The r
1cab9 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 6e  eturned value in
1caba 64 69 63 61 74 65 73 20 74 68 65 20 73 74 72 75  dicates the stru
1cabb 63 74 75 72 65 20 74 79 70 65 2c 20 61 73 20 66  cture type, as f
1cabc 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
1cabd 49 4e 5f 49 4e 44 45 58 5f 52 4f 57 49 44 20 2d  IN_INDEX_ROWID -
1cabe 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73 20   The cursor was 
1cabf 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74 61  opened on a data
1cac0 62 61 73 65 20 74 61 62 6c 65 2e 0a 2a 2a 20 20  base table..**  
1cac1 20 49 4e 5f 49 4e 44 45 58 5f 49 4e 44 45 58 20   IN_INDEX_INDEX 
1cac2 2d 20 54 68 65 20 63 75 72 73 6f 72 20 77 61 73  - The cursor was
1cac3 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 64 61 74   opened on a dat
1cac4 61 62 61 73 65 20 69 6e 64 65 78 2e 0a 2a 2a 20  abase index..** 
1cac5 20 20 49 4e 5f 49 4e 44 45 58 5f 45 50 48 20 2d    IN_INDEX_EPH -
1cac6 20 20 20 54 68 65 20 63 75 72 73 6f 72 20 77 61     The cursor wa
1cac7 73 20 6f 70 65 6e 65 64 20 6f 6e 20 61 20 73 70  s opened on a sp
1cac8 65 63 69 61 6c 6c 79 20 63 72 65 61 74 65 64 20  ecially created 
1cac9 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  and.**          
1caca 20 20 20 20 20 20 20 20 20 20 70 6f 70 75 6c 61            popula
1cacb 74 65 64 20 65 70 68 65 72 65 6d 61 6c 20 74 61  ted epheremal ta
1cacc 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78  ble..**.** An ex
1cacd 69 73 74 69 6e 67 20 73 74 72 75 63 74 75 72 65  isting structure
1cace 20 6d 61 79 20 6f 6e 6c 79 20 62 65 20 75 73 65   may only be use
1cacf 64 20 69 66 20 74 68 65 20 53 45 4c 45 43 54 20  d if the SELECT 
1cad0 69 73 20 6f 66 20 74 68 65 20 73 69 6d 70 6c 65  is of the simple
1cad1 0a 2a 2a 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a 20  .** form:.**.** 
1cad2 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75      SELECT <colu
1cad3 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e  mn> FROM <table>
1cad4 0a 2a 2a 0a 2a 2a 20 49 66 20 70 72 4e 6f 74 46  .**.** If prNotF
1cad5 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20 69  ound parameter i
1cad6 73 20 30 2c 20 74 68 65 6e 20 74 68 65 20 73 74  s 0, then the st
1cad7 72 75 63 74 75 72 65 20 77 69 6c 6c 20 62 65 20  ructure will be 
1cad8 75 73 65 64 20 74 6f 20 69 74 65 72 61 74 65 0a  used to iterate.
1cad9 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 20 73  ** through the s
1cada 65 74 20 6d 65 6d 62 65 72 73 2c 20 73 6b 69 70  et members, skip
1cadb 70 69 6e 67 20 61 6e 79 20 64 75 70 6c 69 63 61  ping any duplica
1cadc 74 65 73 2e 20 49 6e 20 74 68 69 73 20 63 61 73  tes. In this cas
1cadd 65 20 61 6e 0a 2a 2a 20 65 70 68 65 72 65 6d 61  e an.** epherema
1cade 6c 20 74 61 62 6c 65 20 6d 75 73 74 20 62 65 20  l table must be 
1cadf 75 73 65 64 20 75 6e 6c 65 73 73 20 74 68 65 20  used unless the 
1cae0 73 65 6c 65 63 74 65 64 20 3c 63 6f 6c 75 6d 6e  selected <column
1cae1 3e 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 0a  > is guaranteed.
1cae2 2a 2a 20 74 6f 20 62 65 20 75 6e 69 71 75 65 20  ** to be unique 
1cae3 2d 20 65 69 74 68 65 72 20 62 65 63 61 75 73 65  - either because
1cae4 20 69 74 20 69 73 20 61 6e 20 49 4e 54 45 47 45   it is an INTEGE
1cae5 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f 72  R PRIMARY KEY or
1cae6 20 69 74 0a 2a 2a 20 69 73 20 75 6e 69 71 75 65   it.** is unique
1cae7 20 62 79 20 76 69 72 74 75 65 20 6f 66 20 61 20   by virtue of a 
1cae8 63 6f 6e 73 74 72 61 69 6e 74 20 6f 72 20 69 6d  constraint or im
1cae9 70 6c 69 63 69 74 20 69 6e 64 65 78 2e 0a 2a 2a  plicit index..**
1caea 0a 2a 2a 20 49 66 20 74 68 65 20 70 72 4e 6f 74  .** If the prNot
1caeb 46 6f 75 6e 64 20 70 61 72 61 6d 65 74 65 72 20  Found parameter 
1caec 69 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 74  is not 0, then t
1caed 68 65 20 73 74 72 75 63 74 75 72 65 20 77 69 6c  he structure wil
1caee 6c 20 62 65 20 75 73 65 64 20 0a 2a 2a 20 66 6f  l be used .** fo
1caef 72 20 66 61 73 74 20 73 65 74 20 6d 65 6d 62 65  r fast set membe
1caf0 72 73 68 69 70 20 74 65 73 74 73 2e 20 49 6e 20  rship tests. In 
1caf1 74 68 69 73 20 63 61 73 65 20 61 6e 20 65 70 68  this case an eph
1caf2 65 72 65 6d 61 6c 20 74 61 62 6c 65 20 6d 75 73  eremal table mus
1caf3 74 20 0a 2a 2a 20 62 65 20 75 73 65 64 20 75 6e  t .** be used un
1caf4 6c 65 73 73 20 3c 63 6f 6c 75 6d 6e 3e 20 69 73  less <column> is
1caf5 20 61 6e 20 49 4e 54 45 47 45 52 20 50 52 49 4d   an INTEGER PRIM
1caf6 41 52 59 20 4b 45 59 20 6f 72 20 61 6e 20 69 6e  ARY KEY or an in
1caf7 64 65 78 20 63 61 6e 20 0a 2a 2a 20 62 65 20 66  dex can .** be f
1caf8 6f 75 6e 64 20 77 69 74 68 20 3c 63 6f 6c 75 6d  ound with <colum
1caf9 6e 3e 20 61 73 20 69 74 73 20 6c 65 66 74 2d 6d  n> as its left-m
1cafa 6f 73 74 20 63 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  ost column..**.*
1cafb 2a 20 57 68 65 6e 20 74 68 65 20 73 74 72 75 63  * When the struc
1cafc 74 75 72 65 20 69 73 20 62 65 69 6e 67 20 75 73  ture is being us
1cafd 65 64 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65  ed for set membe
1cafe 72 73 68 69 70 20 74 65 73 74 73 2c 20 74 68 65  rship tests, the
1caff 20 75 73 65 72 0a 2a 2a 20 6e 65 65 64 73 20 74   user.** needs t
1cb00 6f 20 6b 6e 6f 77 20 77 68 65 74 68 65 72 20 6f  o know whether o
1cb01 72 20 6e 6f 74 20 74 68 65 20 73 74 72 75 63 74  r not the struct
1cb02 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20  ure contains an 
1cb03 53 51 4c 20 4e 55 4c 4c 20 0a 2a 2a 20 76 61 6c  SQL NULL .** val
1cb04 75 65 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 63  ue in order to c
1cb05 6f 72 72 65 63 74 6c 79 20 65 76 61 6c 75 61 74  orrectly evaluat
1cb06 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 6c 69  e expressions li
1cb07 6b 65 20 22 58 20 49 4e 20 28 59 2c 20 5a 29 22  ke "X IN (Y, Z)"
1cb08 2e 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69 73  ..** If there is
1cb09 20 61 20 63 68 61 6e 63 65 20 74 68 61 74 20 74   a chance that t
1cb0a 68 65 20 73 74 72 75 63 74 75 72 65 20 6d 61 79  he structure may
1cb0b 20 63 6f 6e 74 61 69 6e 20 61 20 4e 55 4c 4c 20   contain a NULL 
1cb0c 76 61 6c 75 65 20 61 74 0a 2a 2a 20 72 75 6e 74  value at.** runt
1cb0d 69 6d 65 2c 20 74 68 65 6e 20 61 20 72 65 67 69  ime, then a regi
1cb0e 73 74 65 72 20 69 73 20 61 6c 6c 6f 63 61 74 65  ster is allocate
1cb0f 64 20 61 6e 64 20 74 68 65 20 72 65 67 69 73 74  d and the regist
1cb10 65 72 20 6e 75 6d 62 65 72 20 77 72 69 74 74 65  er number writte
1cb11 6e 0a 2a 2a 20 74 6f 20 2a 70 72 4e 6f 74 46 6f  n.** to *prNotFo
1cb12 75 6e 64 2e 20 49 66 20 74 68 65 72 65 20 69 73  und. If there is
1cb13 20 6e 6f 20 63 68 61 6e 63 65 20 74 68 61 74 20   no chance that 
1cb14 74 68 65 20 73 74 72 75 63 74 75 72 65 20 63 6f  the structure co
1cb15 6e 74 61 69 6e 73 20 61 0a 2a 2a 20 4e 55 4c 4c  ntains a.** NULL
1cb16 20 76 61 6c 75 65 2c 20 74 68 65 6e 20 2a 70 72   value, then *pr
1cb17 4e 6f 74 46 6f 75 6e 64 20 69 73 20 6c 65 66 74  NotFound is left
1cb18 20 75 6e 63 68 61 6e 67 65 64 2e 0a 2a 2a 0a 2a   unchanged..**.*
1cb19 2a 20 49 66 20 61 20 72 65 67 69 73 74 65 72 20  * If a register 
1cb1a 69 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  is allocated and
1cb1b 20 69 74 73 20 6c 6f 63 61 74 69 6f 6e 20 73 74   its location st
1cb1c 6f 72 65 64 20 69 6e 20 2a 70 72 4e 6f 74 46 6f  ored in *prNotFo
1cb1d 75 6e 64 2c 20 74 68 65 6e 0a 2a 2a 20 69 74 73  und, then.** its
1cb1e 20 69 6e 69 74 69 61 6c 20 76 61 6c 75 65 20 69   initial value i
1cb1f 73 20 4e 55 4c 4c 2e 20 49 66 20 74 68 65 20 73  s NULL. If the s
1cb20 74 72 75 63 74 75 72 65 20 64 6f 65 73 20 6e 6f  tructure does no
1cb21 74 20 72 65 6d 61 69 6e 20 63 6f 6e 73 74 61 6e  t remain constan
1cb22 74 0a 2a 2a 20 66 6f 72 20 74 68 65 20 64 75 72  t.** for the dur
1cb23 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 65  ation of the que
1cb24 72 79 20 28 69 2e 65 2e 20 74 68 65 20 73 65 74  ry (i.e. the set
1cb25 20 69 73 20 61 20 63 6f 72 72 65 6c 61 74 65 64   is a correlated
1cb26 20 73 75 62 2d 73 65 6c 65 63 74 29 2c 20 0a 2a   sub-select), .*
1cb27 2a 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  * the value of t
1cb28 68 65 20 61 6c 6c 6f 63 61 74 65 64 20 72 65 67  he allocated reg
1cb29 69 73 74 65 72 20 69 73 20 72 65 73 65 74 20 74  ister is reset t
1cb2a 6f 20 4e 55 4c 4c 20 65 61 63 68 20 74 69 6d 65  o NULL each time
1cb2b 20 74 68 65 20 0a 2a 2a 20 73 74 72 75 63 74 75   the .** structu
1cb2c 72 65 20 69 73 20 72 65 70 6f 70 75 6c 61 74 65  re is repopulate
1cb2d 64 2e 20 54 68 69 73 20 61 6c 6c 6f 77 73 20 74  d. This allows t
1cb2e 68 65 20 63 61 6c 6c 65 72 20 74 6f 20 75 73 65  he caller to use
1cb2f 20 76 64 62 65 20 63 6f 64 65 20 0a 2a 2a 20 65   vdbe code .** e
1cb30 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 74 68 65  quivalent to the
1cb31 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a 2a 2a 0a 2a   following:.**.*
1cb32 2a 20 20 20 69 66 28 20 72 65 67 69 73 74 65 72  *   if( register
1cb33 3d 3d 4e 55 4c 4c 20 29 7b 0a 2a 2a 20 20 20 20  ==NULL ){.**    
1cb34 20 68 61 73 5f 6e 75 6c 6c 20 3d 20 3c 74 65 73   has_null = <tes
1cb35 74 20 69 66 20 64 61 74 61 20 73 74 72 75 63 74  t if data struct
1cb36 75 72 65 20 63 6f 6e 74 61 69 6e 73 20 6e 75 6c  ure contains nul
1cb37 6c 3e 0a 2a 2a 20 20 20 20 20 72 65 67 69 73 74  l>.**     regist
1cb38 65 72 20 3d 20 31 0a 2a 2a 20 20 20 7d 0a 2a 2a  er = 1.**   }.**
1cb39 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  .** in order to 
1cb3a 61 76 6f 69 64 20 72 75 6e 6e 69 6e 67 20 74 68  avoid running th
1cb3b 65 20 3c 74 65 73 74 20 69 66 20 64 61 74 61 20  e <test if data 
1cb3c 73 74 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69  structure contai
1cb3d 6e 73 20 6e 75 6c 6c 3e 0a 2a 2a 20 74 65 73 74  ns null>.** test
1cb3e 20 6d 6f 72 65 20 6f 66 74 65 6e 20 74 68 61 6e   more often than
1cb3f 20 69 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   is necessary..*
1cb40 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1cb41 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 53  _OMIT_SUBQUERY.S
1cb42 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1cb43 74 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 49  t sqlite3FindInI
1cb44 6e 64 65 78 28 50 61 72 73 65 20 2a 70 50 61 72  ndex(Parse *pPar
1cb45 73 65 2c 20 45 78 70 72 20 2a 70 58 2c 20 69 6e  se, Expr *pX, in
1cb46 74 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 29 7b 0a  t *prNotFound){.
1cb47 20 20 53 65 6c 65 63 74 20 2a 70 3b 0a 20 20 69    Select *p;.  i
1cb48 6e 74 20 65 54 79 70 65 20 3d 20 30 3b 0a 20 20  nt eType = 0;.  
1cb49 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61 72 73  int iTab = pPars
1cb4a 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 69 6e 74  e->nTab++;.  int
1cb4b 20 6d 75 73 74 42 65 55 6e 69 71 75 65 20 3d 20   mustBeUnique = 
1cb4c 21 70 72 4e 6f 74 46 6f 75 6e 64 3b 0a 0a 20 20  !prNotFound;..  
1cb4d 2f 2a 20 54 68 65 20 66 6f 6c 6c 77 69 6e 67 20  /* The follwing 
1cb4e 69 66 28 2e 2e 2e 29 20 65 78 70 72 65 73 73 69  if(...) expressi
1cb4f 6f 6e 20 69 73 20 74 72 75 65 20 69 66 20 74 68  on is true if th
1cb50 65 20 53 45 4c 45 43 54 20 69 73 20 6f 66 20 74  e SELECT is of t
1cb51 68 65 20 0a 20 20 2a 2a 20 73 69 6d 70 6c 65 20  he .  ** simple 
1cb52 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  form:.  **.  ** 
1cb53 20 20 20 20 53 45 4c 45 43 54 20 3c 63 6f 6c 75      SELECT <colu
1cb54 6d 6e 3e 20 46 52 4f 4d 20 3c 74 61 62 6c 65 3e  mn> FROM <table>
1cb55 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68  .  **.  ** If th
1cb56 69 73 20 69 73 20 74 68 65 20 63 61 73 65 2c 20  is is the case, 
1cb57 69 74 20 6d 61 79 20 62 65 20 70 6f 73 73 69 62  it may be possib
1cb58 6c 65 20 74 6f 20 75 73 65 20 61 6e 20 65 78 69  le to use an exi
1cb59 73 74 69 6e 67 20 74 61 62 6c 65 0a 20 20 2a 2a  sting table.  **
1cb5a 20 6f 72 20 69 6e 64 65 78 20 69 6e 73 74 65 61   or index instea
1cb5b 64 20 6f 66 20 67 65 6e 65 72 61 74 69 6e 67 20  d of generating 
1cb5c 61 6e 20 65 70 68 65 72 65 6d 61 6c 20 74 61 62  an epheremal tab
1cb5d 6c 65 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 70  le..  */.  p = p
1cb5e 58 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 69 66  X->pSelect;.  if
1cb5f 28 20 69 73 43 61 6e 64 69 64 61 74 65 46 6f 72  ( isCandidateFor
1cb60 49 6e 4f 70 74 28 70 29 20 29 7b 0a 20 20 20 20  InOpt(p) ){.    
1cb61 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1cb62 61 72 73 65 2d 3e 64 62 3b 0a 20 20 20 20 49 6e  arse->db;.    In
1cb63 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 45  dex *pIdx;.    E
1cb64 78 70 72 20 2a 70 45 78 70 72 20 3d 20 70 2d 3e  xpr *pExpr = p->
1cb65 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78  pEList->a[0].pEx
1cb66 70 72 3b 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c  pr;.    int iCol
1cb67 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d   = pExpr->iColum
1cb68 6e 3b 0a 20 20 20 20 56 64 62 65 20 2a 76 20 3d  n;.    Vdbe *v =
1cb69 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1cb6a 70 50 61 72 73 65 29 3b 0a 0a 20 20 20 20 2f 2a  pParse);..    /*
1cb6b 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1cb6c 73 20 6f 6e 6c 79 20 63 61 6c 6c 65 64 20 66 72  s only called fr
1cb6d 6f 6d 20 74 77 6f 20 70 6c 61 63 65 73 2e 20 49  om two places. I
1cb6e 6e 20 62 6f 74 68 20 63 61 73 65 73 20 74 68 65  n both cases the
1cb6f 20 76 64 62 65 0a 20 20 20 20 2a 2a 20 68 61 73   vdbe.    ** has
1cb70 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 61 6c   already been al
1cb71 6c 6f 63 61 74 65 64 2e 20 53 6f 20 61 73 73 75  located. So assu
1cb72 6d 65 20 73 71 6c 69 74 65 33 47 65 74 56 64 62  me sqlite3GetVdb
1cb73 65 28 29 20 69 73 20 61 6c 77 61 79 73 0a 20 20  e() is always.  
1cb74 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
1cb75 68 65 72 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  here..    */.   
1cb76 20 61 73 73 65 72 74 28 76 29 3b 0a 20 20 20 20   assert(v);.    
1cb77 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20 20  if( iCol<0 ){.  
1cb78 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20 2b      int iMem = +
1cb79 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
1cb7a 20 20 20 20 20 69 6e 74 20 69 41 64 64 72 3b 0a       int iAddr;.
1cb7b 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61        Table *pTa
1cb7c 62 20 3d 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 30  b = p->pSrc->a[0
1cb7d 5d 2e 70 54 61 62 3b 0a 20 20 20 20 20 20 69 6e  ].pTab;.      in
1cb7e 74 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53  t iDb = sqlite3S
1cb7f 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1cb80 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
1cb81 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1cb82 62 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69  beUsesBtree(v, i
1cb83 44 62 29 3b 0a 0a 20 20 20 20 20 20 69 41 64 64  Db);..      iAdd
1cb84 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  r = sqlite3VdbeA
1cb85 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20  ddOp1(v, OP_If, 
1cb86 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c  iMem);.      sql
1cb87 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1cb88 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
1cb89 20 69 4d 65 6d 29 3b 0a 0a 20 20 20 20 20 20 73   iMem);..      s
1cb8a 71 6c 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28  qlite3OpenTable(
1cb8b 70 50 61 72 73 65 2c 20 69 54 61 62 2c 20 69 44  pParse, iTab, iD
1cb8c 62 2c 20 70 54 61 62 2c 20 4f 50 5f 4f 70 65 6e  b, pTab, OP_Open
1cb8d 52 65 61 64 29 3b 0a 20 20 20 20 20 20 65 54 79  Read);.      eTy
1cb8e 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  pe = IN_INDEX_RO
1cb8f 57 49 44 3b 0a 0a 20 20 20 20 20 20 73 71 6c 69  WID;..      sqli
1cb90 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1cb91 76 2c 20 69 41 64 64 72 29 3b 0a 20 20 20 20 7d  v, iAddr);.    }
1cb92 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 54  else{.      /* T
1cb93 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  he collation seq
1cb94 75 65 6e 63 65 20 75 73 65 64 20 62 79 20 74 68  uence used by th
1cb95 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2e 20 49 66  e comparison. If
1cb96 20 61 6e 20 69 6e 64 65 78 20 69 73 20 74 6f 20   an index is to 
1cb97 0a 20 20 20 20 20 20 2a 2a 20 62 65 20 75 73 65  .      ** be use
1cb98 64 20 69 6e 20 70 6c 61 63 65 20 6f 66 20 61 20  d in place of a 
1cb99 74 65 6d 70 2d 74 61 62 6c 65 2c 20 69 74 20 6d  temp-table, it m
1cb9a 75 73 74 20 62 65 20 6f 72 64 65 72 65 64 20 61  ust be ordered a
1cb9b 63 63 6f 72 64 69 6e 67 0a 20 20 20 20 20 20 2a  ccording.      *
1cb9c 2a 20 74 6f 20 74 68 69 73 20 63 6f 6c 6c 61 74  * to this collat
1cb9d 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 0a 20 20  ion sequence..  
1cb9e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 43 6f 6c      */.      Col
1cb9f 6c 53 65 71 20 2a 70 52 65 71 20 3d 20 73 71 6c  lSeq *pReq = sql
1cba0 69 74 65 33 42 69 6e 61 72 79 43 6f 6d 70 61 72  ite3BinaryCompar
1cba1 65 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c  eCollSeq(pParse,
1cba2 20 70 58 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70   pX->pLeft, pExp
1cba3 72 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 43 68  r);..      /* Ch
1cba4 65 63 6b 20 74 68 61 74 20 74 68 65 20 61 66 66  eck that the aff
1cba5 69 6e 69 74 79 20 74 68 61 74 20 77 69 6c 6c 20  inity that will 
1cba6 62 65 20 75 73 65 64 20 74 6f 20 70 65 72 66 6f  be used to perfo
1cba7 72 6d 20 74 68 65 20 0a 20 20 20 20 20 20 2a 2a  rm the .      **
1cba8 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 74   comparison is t
1cba9 68 65 20 73 61 6d 65 20 61 73 20 74 68 65 20 61  he same as the a
1cbaa 66 66 69 6e 69 74 79 20 6f 66 20 74 68 65 20 63  ffinity of the c
1cbab 6f 6c 75 6d 6e 2e 20 49 66 0a 20 20 20 20 20 20  olumn. If.      
1cbac 2a 2a 20 69 74 20 69 73 20 6e 6f 74 2c 20 69 74  ** it is not, it
1cbad 20 69 73 20 6e 6f 74 20 70 6f 73 73 69 62 6c 65   is not possible
1cbae 20 74 6f 20 75 73 65 20 61 6e 79 20 69 6e 64 65   to use any inde
1cbaf 78 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  x..      */.    
1cbb0 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1cbb1 70 2d 3e 70 53 72 63 2d 3e 61 5b 30 5d 2e 70 54  p->pSrc->a[0].pT
1cbb2 61 62 3b 0a 20 20 20 20 20 20 63 68 61 72 20 61  ab;.      char a
1cbb3 66 66 20 3d 20 63 6f 6d 70 61 72 69 73 6f 6e 41  ff = comparisonA
1cbb4 66 66 69 6e 69 74 79 28 70 58 29 3b 0a 20 20 20  ffinity(pX);.   
1cbb5 20 20 20 69 6e 74 20 61 66 66 69 6e 69 74 79 5f     int affinity_
1cbb6 6f 6b 20 3d 20 28 70 54 61 62 2d 3e 61 43 6f 6c  ok = (pTab->aCol
1cbb7 5b 69 43 6f 6c 5d 2e 61 66 66 69 6e 69 74 79 3d  [iCol].affinity=
1cbb8 3d 61 66 66 7c 7c 61 66 66 3d 3d 53 51 4c 49 54  =aff||aff==SQLIT
1cbb9 45 5f 41 46 46 5f 4e 4f 4e 45 29 3b 0a 0a 20 20  E_AFF_NONE);..  
1cbba 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
1cbbb 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 20  b->pIndex; pIdx 
1cbbc 26 26 20 65 54 79 70 65 3d 3d 30 20 26 26 20 61  && eType==0 && a
1cbbd 66 66 69 6e 69 74 79 5f 6f 6b 3b 20 70 49 64 78  ffinity_ok; pIdx
1cbbe 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
1cbbf 20 20 20 20 20 20 20 69 66 28 20 28 70 49 64 78         if( (pIdx
1cbc0 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 30 5d 3d 3d 69  ->aiColumn[0]==i
1cbc1 43 6f 6c 29 0a 20 20 20 20 20 20 20 20 20 26 26  Col).         &&
1cbc2 20 28 70 52 65 71 3d 3d 73 71 6c 69 74 65 33 46   (pReq==sqlite3F
1cbc3 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 45  indCollSeq(db, E
1cbc4 4e 43 28 64 62 29 2c 20 70 49 64 78 2d 3e 61 7a  NC(db), pIdx->az
1cbc5 43 6f 6c 6c 5b 30 5d 2c 20 2d 31 2c 20 30 29 29  Coll[0], -1, 0))
1cbc6 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 21 6d  .         && (!m
1cbc7 75 73 74 42 65 55 6e 69 71 75 65 20 7c 7c 20 28  ustBeUnique || (
1cbc8 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 31  pIdx->nColumn==1
1cbc9 20 26 26 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f   && pIdx->onErro
1cbca 72 21 3d 4f 45 5f 4e 6f 6e 65 29 29 0a 20 20 20  r!=OE_None)).   
1cbcb 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20       ){.        
1cbcc 20 20 69 6e 74 20 69 44 62 3b 0a 20 20 20 20 20    int iDb;.     
1cbcd 20 20 20 20 20 69 6e 74 20 69 4d 65 6d 20 3d 20       int iMem = 
1cbce 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
1cbcf 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 41            int iA
1cbd0 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 63  ddr;.          c
1cbd1 68 61 72 20 2a 70 4b 65 79 3b 0a 20 20 0a 20 20  har *pKey;.  .  
1cbd2 20 20 20 20 20 20 20 20 70 4b 65 79 20 3d 20 28          pKey = (
1cbd3 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 49 6e  char *)sqlite3In
1cbd4 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73  dexKeyinfo(pPars
1cbd5 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20 20  e, pIdx);.      
1cbd6 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65      iDb = sqlite
1cbd7 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
1cbd8 62 2c 20 70 49 64 78 2d 3e 70 53 63 68 65 6d 61  b, pIdx->pSchema
1cbd9 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1cbda 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
1cbdb 65 28 76 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20  e(v, iDb);..    
1cbdc 20 20 20 20 20 20 69 41 64 64 72 20 3d 20 73 71        iAddr = sq
1cbdd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1cbde 76 2c 20 4f 50 5f 49 66 2c 20 69 4d 65 6d 29 3b  v, OP_If, iMem);
1cbdf 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
1cbe0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1cbe1 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 69  OP_Integer, 1, i
1cbe2 4d 65 6d 29 3b 0a 20 20 0a 20 20 20 20 20 20 20  Mem);.  .       
1cbe3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1cbe4 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75  dOp2(v, OP_SetNu
1cbe5 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 49 64  mColumns, 0, pId
1cbe6 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  x->nColumn);.   
1cbe7 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1cbe8 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
1cbe9 70 65 6e 52 65 61 64 2c 20 69 54 61 62 2c 20 70  penRead, iTab, p
1cbea 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a  Idx->tnum, iDb,.
1cbeb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1cbec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1cbed 4b 65 79 2c 50 34 5f 4b 45 59 49 4e 46 4f 5f 48  Key,P4_KEYINFO_H
1cbee 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20 20  ANDOFF);.       
1cbef 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1cbf0 76 2c 20 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a  v, "%s", pIdx->z
1cbf1 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20 20 20  Name));.        
1cbf2 20 20 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44    eType = IN_IND
1cbf3 45 58 5f 49 4e 44 45 58 3b 0a 0a 20 20 20 20 20  EX_INDEX;..     
1cbf4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cbf5 4a 75 6d 70 48 65 72 65 28 76 2c 20 69 41 64 64  JumpHere(v, iAdd
1cbf6 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66  r);.          if
1cbf7 28 20 70 72 4e 6f 74 46 6f 75 6e 64 20 26 26 20  ( prNotFound && 
1cbf8 21 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  !pTab->aCol[iCol
1cbf9 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b 0a 20 20 20  ].notNull ){.   
1cbfa 20 20 20 20 20 20 20 20 20 2a 70 72 4e 6f 74 46           *prNotF
1cbfb 6f 75 6e 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d  ound = ++pParse-
1cbfc 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 20  >nMem;.         
1cbfd 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
1cbfe 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a     }.    }.  }..
1cbff 20 20 69 66 28 20 65 54 79 70 65 3d 3d 30 20 29    if( eType==0 )
1cc00 7b 0a 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61  {.    int rMayHa
1cc01 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  veNull = 0;.    
1cc02 65 54 79 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58  eType = IN_INDEX
1cc03 5f 45 50 48 3b 0a 20 20 20 20 69 66 28 20 70 72  _EPH;.    if( pr
1cc04 4e 6f 74 46 6f 75 6e 64 20 29 7b 0a 20 20 20 20  NotFound ){.    
1cc05 20 20 2a 70 72 4e 6f 74 46 6f 75 6e 64 20 3d 20    *prNotFound = 
1cc06 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 3d 20 2b  rMayHaveNull = +
1cc07 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
1cc08 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 58 2d     }else if( pX-
1cc09 3e 70 4c 65 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3c  >pLeft->iColumn<
1cc0a 30 20 26 26 20 70 58 2d 3e 70 53 65 6c 65 63 74  0 && pX->pSelect
1cc0b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 54 79  ==0 ){.      eTy
1cc0c 70 65 20 3d 20 49 4e 5f 49 4e 44 45 58 5f 52 4f  pe = IN_INDEX_RO
1cc0d 57 49 44 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  WID;.    }.    s
1cc0e 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73 65 6c  qlite3CodeSubsel
1cc0f 65 63 74 28 70 50 61 72 73 65 2c 20 70 58 2c 20  ect(pParse, pX, 
1cc10 72 4d 61 79 48 61 76 65 4e 75 6c 6c 2c 20 65 54  rMayHaveNull, eT
1cc11 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58 5f 52 4f  ype==IN_INDEX_RO
1cc12 57 49 44 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  WID);.  }else{. 
1cc13 20 20 20 70 58 2d 3e 69 54 61 62 6c 65 20 3d 20     pX->iTable = 
1cc14 69 54 61 62 3b 0a 20 20 7d 0a 20 20 72 65 74 75  iTab;.  }.  retu
1cc15 72 6e 20 65 54 79 70 65 3b 0a 7d 0a 23 65 6e 64  rn eType;.}.#end
1cc16 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  if../*.** Genera
1cc17 74 65 20 63 6f 64 65 20 66 6f 72 20 73 63 61 6c  te code for scal
1cc18 61 72 20 73 75 62 71 75 65 72 69 65 73 20 75 73  ar subqueries us
1cc19 65 64 20 61 73 20 61 6e 20 65 78 70 72 65 73 73  ed as an express
1cc1a 69 6f 6e 0a 2a 2a 20 61 6e 64 20 49 4e 20 6f 70  ion.** and IN op
1cc1b 65 72 61 74 6f 72 73 2e 20 20 45 78 61 6d 70 6c  erators.  Exampl
1cc1c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 28 53  es:.**.**     (S
1cc1d 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 62 29 20  ELECT a FROM b) 
1cc1e 20 20 20 20 20 20 20 20 20 2d 2d 20 73 75 62 71           -- subq
1cc1f 75 65 72 79 0a 2a 2a 20 20 20 20 20 45 58 49 53  uery.**     EXIS
1cc20 54 53 20 28 53 45 4c 45 43 54 20 61 20 46 52 4f  TS (SELECT a FRO
1cc21 4d 20 62 29 20 20 20 2d 2d 20 45 58 49 53 54 53  M b)   -- EXISTS
1cc22 20 73 75 62 71 75 65 72 79 0a 2a 2a 20 20 20 20   subquery.**    
1cc23 20 78 20 49 4e 20 28 34 2c 35 2c 31 31 29 20 20   x IN (4,5,11)  
1cc24 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 49              -- I
1cc25 4e 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20  N operator with 
1cc26 6c 69 73 74 20 6f 6e 20 72 69 67 68 74 2d 68 61  list on right-ha
1cc27 6e 64 20 73 69 64 65 0a 2a 2a 20 20 20 20 20 78  nd side.**     x
1cc28 20 49 4e 20 28 53 45 4c 45 43 54 20 61 20 46 52   IN (SELECT a FR
1cc29 4f 4d 20 62 29 20 20 20 20 20 2d 2d 20 49 4e 20  OM b)     -- IN 
1cc2a 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73 75  operator with su
1cc2b 62 71 75 65 72 79 20 6f 6e 20 74 68 65 20 72 69  bquery on the ri
1cc2c 67 68 74 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 45  ght.**.** The pE
1cc2d 78 70 72 20 70 61 72 61 6d 65 74 65 72 20 64 65  xpr parameter de
1cc2e 73 63 72 69 62 65 73 20 74 68 65 20 65 78 70 72  scribes the expr
1cc2f 65 73 73 69 6f 6e 20 74 68 61 74 20 63 6f 6e 74  ession that cont
1cc30 61 69 6e 73 20 74 68 65 20 49 4e 0a 2a 2a 20 6f  ains the IN.** o
1cc31 70 65 72 61 74 6f 72 20 6f 72 20 73 75 62 71 75  perator or subqu
1cc32 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 70 61  ery..**.** If pa
1cc33 72 61 6d 65 74 65 72 20 69 73 52 6f 77 69 64 20  rameter isRowid 
1cc34 69 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74 68 65  is non-zero, the
1cc35 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78  n expression pEx
1cc36 70 72 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  pr is guaranteed
1cc37 0a 2a 2a 20 74 6f 20 62 65 20 6f 66 20 74 68 65  .** to be of the
1cc38 20 66 6f 72 6d 20 22 3c 72 6f 77 69 64 3e 20 49   form "<rowid> I
1cc39 4e 20 28 3f 2c 20 3f 2c 20 3f 29 22 2c 20 77 68  N (?, ?, ?)", wh
1cc3a 65 72 65 20 3c 72 6f 77 69 64 3e 20 69 73 20 61  ere <rowid> is a
1cc3b 20 72 65 66 65 72 65 6e 63 65 0a 2a 2a 20 74 6f   reference.** to
1cc3c 20 73 6f 6d 65 20 69 6e 74 65 67 65 72 20 6b 65   some integer ke
1cc3d 79 20 63 6f 6c 75 6d 6e 20 6f 66 20 61 20 74 61  y column of a ta
1cc3e 62 6c 65 20 42 2d 54 72 65 65 2e 20 49 6e 20 74  ble B-Tree. In t
1cc3f 68 69 73 20 63 61 73 65 2c 20 75 73 65 20 61 6e  his case, use an
1cc40 0a 2a 2a 20 69 6e 74 6b 65 79 20 42 2d 54 72 65  .** intkey B-Tre
1cc41 65 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20 73  e to store the s
1cc42 65 74 20 6f 66 20 49 4e 28 2e 2e 2e 29 20 76 61  et of IN(...) va
1cc43 6c 75 65 73 20 69 6e 73 74 65 61 64 20 6f 66 20  lues instead of 
1cc44 74 68 65 20 75 73 75 61 6c 0a 2a 2a 20 28 73 6c  the usual.** (sl
1cc45 6f 77 65 72 29 20 76 61 72 69 61 62 6c 65 20 6c  ower) variable l
1cc46 65 6e 67 74 68 20 6b 65 79 73 20 42 2d 54 72 65  ength keys B-Tre
1cc47 65 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  e..*/.#ifndef SQ
1cc48 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1cc49 52 59 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  RY.SQLITE_PRIVAT
1cc4a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
1cc4b 64 65 53 75 62 73 65 6c 65 63 74 28 0a 20 20 50  deSubselect(.  P
1cc4c 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 0a 20  arse *pParse, . 
1cc4d 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20 0a 20   Expr *pExpr, . 
1cc4e 20 69 6e 74 20 72 4d 61 79 48 61 76 65 4e 75 6c   int rMayHaveNul
1cc4f 6c 2c 0a 20 20 69 6e 74 20 69 73 52 6f 77 69 64  l,.  int isRowid
1cc50 0a 29 7b 0a 20 20 69 6e 74 20 74 65 73 74 41 64  .){.  int testAd
1cc51 64 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  dr = 0;         
1cc52 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1cc53 20 4f 6e 65 2d 74 69 6d 65 20 74 65 73 74 20 61   One-time test a
1cc54 64 64 72 65 73 73 20 2a 2f 0a 20 20 56 64 62 65  ddress */.  Vdbe
1cc55 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74   *v = sqlite3Get
1cc56 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1cc57 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72  if( v==0 ) retur
1cc58 6e 3b 0a 0a 0a 20 20 2f 2a 20 54 68 69 73 20 63  n;...  /* This c
1cc59 6f 64 65 20 6d 75 73 74 20 62 65 20 72 75 6e 20  ode must be run 
1cc5a 69 6e 20 69 74 73 20 65 6e 74 69 72 65 74 79 20  in its entirety 
1cc5b 65 76 65 72 79 20 74 69 6d 65 20 69 74 20 69 73  every time it is
1cc5c 20 65 6e 63 6f 75 6e 74 65 72 65 64 0a 20 20 2a   encountered.  *
1cc5d 2a 20 69 66 20 61 6e 79 20 6f 66 20 74 68 65 20  * if any of the 
1cc5e 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 74 72 75  following is tru
1cc5f 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20  e:.  **.  **    
1cc60 2a 20 20 54 68 65 20 72 69 67 68 74 2d 68 61 6e  *  The right-han
1cc61 64 20 73 69 64 65 20 69 73 20 61 20 63 6f 72 72  d side is a corr
1cc62 65 6c 61 74 65 64 20 73 75 62 71 75 65 72 79 0a  elated subquery.
1cc63 20 20 2a 2a 20 20 20 20 2a 20 20 54 68 65 20 72    **    *  The r
1cc64 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20 69  ight-hand side i
1cc65 73 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s an expression 
1cc66 6c 69 73 74 20 63 6f 6e 74 61 69 6e 69 6e 67 20  list containing 
1cc67 76 61 72 69 61 62 6c 65 73 0a 20 20 2a 2a 20 20  variables.  **  
1cc68 20 20 2a 20 20 57 65 20 61 72 65 20 69 6e 73 69    *  We are insi
1cc69 64 65 20 61 20 74 72 69 67 67 65 72 0a 20 20 2a  de a trigger.  *
1cc6a 2a 0a 20 20 2a 2a 20 49 66 20 61 6c 6c 20 6f 66  *.  ** If all of
1cc6b 20 74 68 65 20 61 62 6f 76 65 20 61 72 65 20 66   the above are f
1cc6c 61 6c 73 65 2c 20 74 68 65 6e 20 77 65 20 63 61  alse, then we ca
1cc6d 6e 20 72 75 6e 20 74 68 69 73 20 63 6f 64 65 20  n run this code 
1cc6e 6a 75 73 74 20 6f 6e 63 65 0a 20 20 2a 2a 20 73  just once.  ** s
1cc6f 61 76 65 20 74 68 65 20 72 65 73 75 6c 74 73 2c  ave the results,
1cc70 20 61 6e 64 20 72 65 75 73 65 20 74 68 65 20 73   and reuse the s
1cc71 61 6d 65 20 72 65 73 75 6c 74 20 6f 6e 20 73 75  ame result on su
1cc72 62 73 65 71 75 65 6e 74 20 69 6e 76 6f 63 61 74  bsequent invocat
1cc73 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ions..  */.  if(
1cc74 20 21 45 78 70 72 48 61 73 41 6e 79 50 72 6f 70   !ExprHasAnyProp
1cc75 65 72 74 79 28 70 45 78 70 72 2c 20 45 50 5f 56  erty(pExpr, EP_V
1cc76 61 72 53 65 6c 65 63 74 29 20 26 26 20 21 70 50  arSelect) && !pP
1cc77 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 20  arse->trigStack 
1cc78 29 7b 0a 20 20 20 20 69 6e 74 20 6d 65 6d 20 3d  ){.    int mem =
1cc79 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
1cc7a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1cc7b 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c  AddOp1(v, OP_If,
1cc7c 20 6d 65 6d 29 3b 0a 20 20 20 20 74 65 73 74 41   mem);.    testA
1cc7d 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
1cc7e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
1cc7f 74 65 67 65 72 2c 20 31 2c 20 6d 65 6d 29 3b 0a  teger, 1, mem);.
1cc80 20 20 20 20 61 73 73 65 72 74 28 20 74 65 73 74      assert( test
1cc81 41 64 64 72 3e 30 20 7c 7c 20 70 50 61 72 73 65  Addr>0 || pParse
1cc82 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ->db->mallocFail
1cc83 65 64 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  ed );.  }..  swi
1cc84 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1cc85 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 49 4e  {.    case TK_IN
1cc86 3a 20 7b 0a 20 20 20 20 20 20 63 68 61 72 20 61  : {.      char a
1cc87 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20 20 4b  ffinity;.      K
1cc88 65 79 49 6e 66 6f 20 6b 65 79 49 6e 66 6f 3b 0a  eyInfo keyInfo;.
1cc89 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 3b 20        int addr; 
1cc8a 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
1cc8b 73 20 6f 66 20 4f 50 5f 4f 70 65 6e 45 70 68 65  s of OP_OpenEphe
1cc8c 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f  meral instructio
1cc8d 6e 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20  n */.      Expr 
1cc8e 2a 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e  *pLeft = pExpr->
1cc8f 70 4c 65 66 74 3b 0a 0a 20 20 20 20 20 20 69 66  pLeft;..      if
1cc90 28 20 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29  ( rMayHaveNull )
1cc91 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1cc92 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1cc93 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 4d 61 79 48  P_Null, 0, rMayH
1cc94 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  aveNull);.      
1cc95 7d 0a 0a 20 20 20 20 20 20 61 66 66 69 6e 69 74  }..      affinit
1cc96 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 41  y = sqlite3ExprA
1cc97 66 66 69 6e 69 74 79 28 70 4c 65 66 74 29 3b 0a  ffinity(pLeft);.
1cc98 0a 20 20 20 20 20 20 2f 2a 20 57 68 65 74 68 65  .      /* Whethe
1cc99 72 20 74 68 69 73 20 69 73 20 61 6e 20 27 78 20  r this is an 'x 
1cc9a 49 4e 28 53 45 4c 45 43 54 2e 2e 2e 29 27 20 6f  IN(SELECT...)' o
1cc9b 72 20 61 6e 20 27 78 20 49 4e 28 3c 65 78 70 72  r an 'x IN(<expr
1cc9c 6c 69 73 74 3e 29 27 0a 20 20 20 20 20 20 2a 2a  list>)'.      **
1cc9d 20 65 78 70 72 65 73 73 69 6f 6e 20 69 74 20 69   expression it i
1cc9e 73 20 68 61 6e 64 6c 65 64 20 74 68 65 20 73 61  s handled the sa
1cc9f 6d 65 20 77 61 79 2e 20 41 20 76 69 72 74 75 61  me way. A virtua
1cca0 6c 20 74 61 62 6c 65 20 69 73 20 0a 20 20 20 20  l table is .    
1cca1 20 20 2a 2a 20 66 69 6c 6c 65 64 20 77 69 74 68    ** filled with
1cca2 20 73 69 6e 67 6c 65 2d 66 69 65 6c 64 20 69 6e   single-field in
1cca3 64 65 78 20 6b 65 79 73 20 72 65 70 72 65 73 65  dex keys represe
1cca4 6e 74 69 6e 67 20 74 68 65 20 72 65 73 75 6c 74  nting the result
1cca5 73 0a 20 20 20 20 20 20 2a 2a 20 66 72 6f 6d 20  s.      ** from 
1cca6 74 68 65 20 53 45 4c 45 43 54 20 6f 72 20 74 68  the SELECT or th
1cca7 65 20 3c 65 78 70 72 6c 69 73 74 3e 2e 0a 20 20  e <exprlist>..  
1cca8 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1cca9 49 66 20 74 68 65 20 27 78 27 20 65 78 70 72 65  If the 'x' expre
1ccaa 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75 6d  ssion is a colum
1ccab 6e 20 76 61 6c 75 65 2c 20 6f 72 20 74 68 65 20  n value, or the 
1ccac 53 45 4c 45 43 54 2e 2e 2e 0a 20 20 20 20 20 20  SELECT....      
1ccad 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 72 65 74  ** statement ret
1ccae 75 72 6e 73 20 61 20 63 6f 6c 75 6d 6e 20 76 61  urns a column va
1ccaf 6c 75 65 2c 20 74 68 65 6e 20 74 68 65 20 61 66  lue, then the af
1ccb0 66 69 6e 69 74 79 20 6f 66 20 74 68 61 74 0a 20  finity of that. 
1ccb1 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 69       ** column i
1ccb2 73 20 75 73 65 64 20 74 6f 20 62 75 69 6c 64 20  s used to build 
1ccb3 74 68 65 20 69 6e 64 65 78 20 6b 65 79 73 2e 20  the index keys. 
1ccb4 49 66 20 62 6f 74 68 20 27 78 27 20 61 6e 64 20  If both 'x' and 
1ccb5 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 53 45 4c  the.      ** SEL
1ccb6 45 43 54 2e 2e 2e 20 73 74 61 74 65 6d 65 6e 74  ECT... statement
1ccb7 20 61 72 65 20 63 6f 6c 75 6d 6e 73 2c 20 74 68   are columns, th
1ccb8 65 6e 20 6e 75 6d 65 72 69 63 20 61 66 66 69 6e  en numeric affin
1ccb9 69 74 79 20 69 73 20 75 73 65 64 0a 20 20 20 20  ity is used.    
1ccba 20 20 2a 2a 20 69 66 20 65 69 74 68 65 72 20 63    ** if either c
1ccbb 6f 6c 75 6d 6e 20 68 61 73 20 4e 55 4d 45 52 49  olumn has NUMERI
1ccbc 43 20 6f 72 20 49 4e 54 45 47 45 52 20 61 66 66  C or INTEGER aff
1ccbd 69 6e 69 74 79 2e 20 49 66 20 6e 65 69 74 68 65  inity. If neithe
1ccbe 72 0a 20 20 20 20 20 20 2a 2a 20 27 78 27 20 6e  r.      ** 'x' n
1ccbf 6f 72 20 74 68 65 20 53 45 4c 45 43 54 2e 2e 2e  or the SELECT...
1ccc0 20 73 74 61 74 65 6d 65 6e 74 20 61 72 65 20 63   statement are c
1ccc1 6f 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 6e 75 6d  olumns, then num
1ccc2 65 72 69 63 20 61 66 66 69 6e 69 74 79 0a 20 20  eric affinity.  
1ccc3 20 20 20 20 2a 2a 20 69 73 20 75 73 65 64 2e 0a      ** is used..
1ccc4 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 70        */.      p
1ccc5 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 3d 20 70  Expr->iTable = p
1ccc6 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
1ccc7 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
1ccc8 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1ccc9 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61   OP_OpenEphemera
1ccca 6c 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  l, pExpr->iTable
1cccb 2c 20 21 69 73 52 6f 77 69 64 29 3b 0a 20 20 20  , !isRowid);.   
1cccc 20 20 20 6d 65 6d 73 65 74 28 26 6b 65 79 49 6e     memset(&keyIn
1cccd 66 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6b 65  fo, 0, sizeof(ke
1ccce 79 49 6e 66 6f 29 29 3b 0a 20 20 20 20 20 20 6b  yInfo));.      k
1cccf 65 79 49 6e 66 6f 2e 6e 46 69 65 6c 64 20 3d 20  eyInfo.nField = 
1ccd0 31 3b 0a 0a 20 20 20 20 20 20 69 66 28 20 70 45  1;..      if( pE
1ccd1 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  xpr->pSelect ){.
1ccd2 20 20 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20          /* Case 
1ccd3 31 3a 20 20 20 20 20 65 78 70 72 20 49 4e 20 28  1:     expr IN (
1ccd4 53 45 4c 45 43 54 20 2e 2e 2e 29 0a 20 20 20 20  SELECT ...).    
1ccd5 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 2a      **.        *
1ccd6 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1ccd7 74 6f 20 77 72 69 74 65 20 74 68 65 20 72 65 73  to write the res
1ccd8 75 6c 74 73 20 6f 66 20 74 68 65 20 73 65 6c 65  ults of the sele
1ccd9 63 74 20 69 6e 74 6f 20 74 68 65 20 74 65 6d 70  ct into the temp
1ccda 6f 72 61 72 79 0a 20 20 20 20 20 20 20 20 2a 2a  orary.        **
1ccdb 20 74 61 62 6c 65 20 61 6c 6c 6f 63 61 74 65 64   table allocated
1ccdc 20 61 6e 64 20 6f 70 65 6e 65 64 20 61 62 6f 76   and opened abov
1ccdd 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
1ccde 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
1ccdf 20 64 65 73 74 3b 0a 20 20 20 20 20 20 20 20 45   dest;.        E
1cce0 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 3b  xprList *pEList;
1cce1 0a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ..        assert
1cce2 28 20 21 69 73 52 6f 77 69 64 20 29 3b 0a 20 20  ( !isRowid );.  
1cce3 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
1cce4 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73  ectDestInit(&des
1cce5 74 2c 20 53 52 54 5f 53 65 74 2c 20 70 45 78 70  t, SRT_Set, pExp
1cce6 72 2d 3e 69 54 61 62 6c 65 29 3b 0a 20 20 20 20  r->iTable);.    
1cce7 20 20 20 20 64 65 73 74 2e 61 66 66 69 6e 69 74      dest.affinit
1cce8 79 20 3d 20 28 69 6e 74 29 61 66 66 69 6e 69 74  y = (int)affinit
1cce9 79 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  y;.        asser
1ccea 74 28 20 28 70 45 78 70 72 2d 3e 69 54 61 62 6c  t( (pExpr->iTabl
1cceb 65 26 30 78 30 30 30 30 46 46 46 46 29 3d 3d 70  e&0x0000FFFF)==p
1ccec 45 78 70 72 2d 3e 69 54 61 62 6c 65 20 29 3b 0a  Expr->iTable );.
1cced 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c 69          if( sqli
1ccee 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
1ccef 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74  , pExpr->pSelect
1ccf0 2c 20 26 64 65 73 74 29 20 29 7b 0a 20 20 20 20  , &dest) ){.    
1ccf1 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1ccf2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1ccf3 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
1ccf4 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 3b  pSelect->pEList;
1ccf5 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 4c  .        if( pEL
1ccf6 69 73 74 20 26 26 20 70 45 4c 69 73 74 2d 3e 6e  ist && pEList->n
1ccf7 45 78 70 72 3e 30 20 29 7b 20 0a 20 20 20 20 20  Expr>0 ){ .     
1ccf8 20 20 20 20 20 6b 65 79 49 6e 66 6f 2e 61 43 6f       keyInfo.aCo
1ccf9 6c 6c 5b 30 5d 20 3d 20 73 71 6c 69 74 65 33 42  ll[0] = sqlite3B
1ccfa 69 6e 61 72 79 43 6f 6d 70 61 72 65 43 6f 6c 6c  inaryCompareColl
1ccfb 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78 70  Seq(pParse, pExp
1ccfc 72 2d 3e 70 4c 65 66 74 2c 0a 20 20 20 20 20 20  r->pLeft,.      
1ccfd 20 20 20 20 20 20 20 20 70 45 4c 69 73 74 2d 3e          pEList->
1ccfe 61 5b 30 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20  a[0].pExpr);.   
1ccff 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1cd00 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 70 4c  se if( pExpr->pL
1cd01 69 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ist ){.        /
1cd02 2a 20 43 61 73 65 20 32 3a 20 20 20 20 20 65 78  * Case 2:     ex
1cd03 70 72 20 49 4e 20 28 65 78 70 72 6c 69 73 74 29  pr IN (exprlist)
1cd04 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  .        **.    
1cd05 20 20 20 20 2a 2a 20 46 6f 72 20 65 61 63 68 20      ** For each 
1cd06 65 78 70 72 65 73 73 69 6f 6e 2c 20 62 75 69 6c  expression, buil
1cd07 64 20 61 6e 20 69 6e 64 65 78 20 6b 65 79 20 66  d an index key f
1cd08 72 6f 6d 20 74 68 65 20 65 76 61 6c 75 61 74 69  rom the evaluati
1cd09 6f 6e 20 61 6e 64 0a 20 20 20 20 20 20 20 20 2a  on and.        *
1cd0a 2a 20 73 74 6f 72 65 20 69 74 20 69 6e 20 74 68  * store it in th
1cd0b 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  e temporary tabl
1cd0c 65 2e 20 49 66 20 3c 65 78 70 72 3e 20 69 73 20  e. If <expr> is 
1cd0d 61 20 63 6f 6c 75 6d 6e 2c 20 74 68 65 6e 20 75  a column, then u
1cd0e 73 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 74 68  se.        ** th
1cd0f 61 74 20 63 6f 6c 75 6d 6e 73 20 61 66 66 69 6e  at columns affin
1cd10 69 74 79 20 77 68 65 6e 20 62 75 69 6c 64 69 6e  ity when buildin
1cd11 67 20 69 6e 64 65 78 20 6b 65 79 73 2e 20 49 66  g index keys. If
1cd12 20 3c 65 78 70 72 3e 20 69 73 20 6e 6f 74 0a 20   <expr> is not. 
1cd13 20 20 20 20 20 20 20 2a 2a 20 61 20 63 6f 6c 75         ** a colu
1cd14 6d 6e 2c 20 75 73 65 20 6e 75 6d 65 72 69 63 20  mn, use numeric 
1cd15 61 66 66 69 6e 69 74 79 2e 0a 20 20 20 20 20 20  affinity..      
1cd16 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
1cd17 20 69 3b 0a 20 20 20 20 20 20 20 20 45 78 70 72   i;.        Expr
1cd18 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d 20 70 45  List *pList = pE
1cd19 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20  xpr->pList;.    
1cd1a 20 20 20 20 73 74 72 75 63 74 20 45 78 70 72 4c      struct ExprL
1cd1b 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1cd1c 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 31 2c  .        int r1,
1cd1d 20 72 32 2c 20 72 33 3b 0a 0a 20 20 20 20 20 20   r2, r3;..      
1cd1e 20 20 69 66 28 20 21 61 66 66 69 6e 69 74 79 20    if( !affinity 
1cd1f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 66 66  ){.          aff
1cd20 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45 5f 41  inity = SQLITE_A
1cd21 46 46 5f 4e 4f 4e 45 3b 0a 20 20 20 20 20 20 20  FF_NONE;.       
1cd22 20 7d 0a 20 20 20 20 20 20 20 20 6b 65 79 49 6e   }.        keyIn
1cd23 66 6f 2e 61 43 6f 6c 6c 5b 30 5d 20 3d 20 73 71  fo.aColl[0] = sq
1cd24 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71  lite3ExprCollSeq
1cd25 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
1cd26 70 4c 65 66 74 29 3b 0a 0a 20 20 20 20 20 20 20  pLeft);..       
1cd27 20 2f 2a 20 4c 6f 6f 70 20 74 68 72 6f 75 67 68   /* Loop through
1cd28 20 65 61 63 68 20 65 78 70 72 65 73 73 69 6f 6e   each expression
1cd29 20 69 6e 20 3c 65 78 70 72 6c 69 73 74 3e 2e 20   in <exprlist>. 
1cd2a 2a 2f 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20  */.        r1 = 
1cd2b 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
1cd2c 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
1cd2d 20 20 20 72 32 20 3d 20 73 71 6c 69 74 65 33 47     r2 = sqlite3G
1cd2e 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
1cd2f 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1cd30 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1cd31 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 32 29 3b  OP_Null, 0, r2);
1cd32 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 70  .        for(i=p
1cd33 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 20 70 49 74  List->nExpr, pIt
1cd34 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3e 30  em=pList->a; i>0
1cd35 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b  ; i--, pItem++){
1cd36 0a 20 20 20 20 20 20 20 20 20 20 45 78 70 72 20  .          Expr 
1cd37 2a 70 45 32 20 3d 20 70 49 74 65 6d 2d 3e 70 45  *pE2 = pItem->pE
1cd38 78 70 72 3b 0a 0a 20 20 20 20 20 20 20 20 20 20  xpr;..          
1cd39 2f 2a 20 49 66 20 74 68 65 20 65 78 70 72 65 73  /* If the expres
1cd3a 73 69 6f 6e 20 69 73 20 6e 6f 74 20 63 6f 6e 73  sion is not cons
1cd3b 74 61 6e 74 20 74 68 65 6e 20 77 65 20 77 69 6c  tant then we wil
1cd3c 6c 20 6e 65 65 64 20 74 6f 0a 20 20 20 20 20 20  l need to.      
1cd3d 20 20 20 20 2a 2a 20 64 69 73 61 62 6c 65 20 74      ** disable t
1cd3e 68 65 20 74 65 73 74 20 74 68 61 74 20 77 61 73  he test that was
1cd3f 20 67 65 6e 65 72 61 74 65 64 20 61 62 6f 76 65   generated above
1cd40 20 74 68 61 74 20 6d 61 6b 65 73 20 73 75 72 65   that makes sure
1cd41 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
1cd42 69 73 20 63 6f 64 65 20 6f 6e 6c 79 20 65 78 65  is code only exe
1cd43 63 75 74 65 73 20 6f 6e 63 65 2e 20 20 42 65 63  cutes once.  Bec
1cd44 61 75 73 65 20 66 6f 72 20 61 20 6e 6f 6e 2d 63  ause for a non-c
1cd45 6f 6e 73 74 61 6e 74 0a 20 20 20 20 20 20 20 20  onstant.        
1cd46 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20    ** expression 
1cd47 77 65 20 6e 65 65 64 20 74 6f 20 72 65 72 75 6e  we need to rerun
1cd48 20 74 68 69 73 20 63 6f 64 65 20 65 61 63 68 20   this code each 
1cd49 74 69 6d 65 2e 0a 20 20 20 20 20 20 20 20 20 20  time..          
1cd4a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  */.          if(
1cd4b 20 74 65 73 74 41 64 64 72 20 26 26 20 21 73 71   testAddr && !sq
1cd4c 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73 74  lite3ExprIsConst
1cd4d 61 6e 74 28 70 45 32 29 20 29 7b 0a 20 20 20 20  ant(pE2) ){.    
1cd4e 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1cd4f 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
1cd50 76 2c 20 74 65 73 74 41 64 64 72 2d 31 2c 20 32  v, testAddr-1, 2
1cd51 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 74  );.            t
1cd52 65 73 74 41 64 64 72 20 3d 20 30 3b 0a 20 20 20  estAddr = 0;.   
1cd53 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
1cd54 20 20 20 20 2f 2a 20 45 76 61 6c 75 61 74 65 20      /* Evaluate 
1cd55 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 61  the expression a
1cd56 6e 64 20 69 6e 73 65 72 74 20 69 74 20 69 6e 74  nd insert it int
1cd57 6f 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65  o the temp table
1cd58 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 70 50   */.          pP
1cd59 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1cd5a 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 20  Cache++;.       
1cd5b 20 20 20 72 33 20 3d 20 73 71 6c 69 74 65 33 45     r3 = sqlite3E
1cd5c 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50  xprCodeTarget(pP
1cd5d 61 72 73 65 2c 20 70 45 32 2c 20 72 31 29 3b 0a  arse, pE2, r1);.
1cd5e 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
1cd5f 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1cd60 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20  eColCache>0 );. 
1cd61 20 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d           pParse-
1cd62 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65  >disableColCache
1cd63 2d 2d 3b 0a 0a 20 20 20 20 20 20 20 20 20 20 69  --;..          i
1cd64 66 28 20 69 73 52 6f 77 69 64 20 29 7b 0a 20 20  f( isRowid ){.  
1cd65 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1cd66 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1cd67 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 72 33 2c  P_MustBeInt, r3,
1cd68 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
1cd69 65 6e 74 41 64 64 72 28 76 29 2b 32 29 3b 0a 20  entAddr(v)+2);. 
1cd6a 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1cd6b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1cd6c 4f 50 5f 49 6e 73 65 72 74 2c 20 70 45 78 70 72  OP_Insert, pExpr
1cd6d 2d 3e 69 54 61 62 6c 65 2c 20 72 32 2c 20 72 33  ->iTable, r2, r3
1cd6e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c  );.          }el
1cd6f 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
1cd70 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1cd71 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  4(v, OP_MakeReco
1cd72 72 64 2c 20 72 33 2c 20 31 2c 20 72 32 2c 20 26  rd, r3, 1, r2, &
1cd73 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a 20 20  affinity, 1);.  
1cd74 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1cd75 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69  3ExprCacheAffini
1cd76 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c  tyChange(pParse,
1cd77 20 72 33 2c 20 31 29 3b 0a 20 20 20 20 20 20 20   r3, 1);.       
1cd78 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1cd79 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78  AddOp2(v, OP_Idx
1cd7a 49 6e 73 65 72 74 2c 20 70 45 78 70 72 2d 3e 69  Insert, pExpr->i
1cd7b 54 61 62 6c 65 2c 20 72 32 29 3b 0a 20 20 20 20  Table, r2);.    
1cd7c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1cd7d 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  }.        sqlite
1cd7e 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1cd7f 70 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20  pParse, r1);.   
1cd80 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65       sqlite3Rele
1cd81 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
1cd82 65 2c 20 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a  e, r2);.      }.
1cd83 20 20 20 20 20 20 69 66 28 20 21 69 73 52 6f 77        if( !isRow
1cd84 69 64 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  id ){.        sq
1cd85 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
1cd86 34 28 76 2c 20 61 64 64 72 2c 20 28 76 6f 69 64  4(v, addr, (void
1cd87 20 2a 29 26 6b 65 79 49 6e 66 6f 2c 20 50 34 5f   *)&keyInfo, P4_
1cd88 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20 20 20  KEYINFO);.      
1cd89 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
1cd8a 20 20 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 54     }..    case T
1cd8b 4b 5f 45 58 49 53 54 53 3a 0a 20 20 20 20 63 61  K_EXISTS:.    ca
1cd8c 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b 0a  se TK_SELECT: {.
1cd8d 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 68 61        /* This ha
1cd8e 73 20 74 6f 20 62 65 20 61 20 73 63 61 6c 61 72  s to be a scalar
1cd8f 20 53 45 4c 45 43 54 2e 20 20 47 65 6e 65 72 61   SELECT.  Genera
1cd90 74 65 20 63 6f 64 65 20 74 6f 20 70 75 74 20 74  te code to put t
1cd91 68 65 0a 20 20 20 20 20 20 2a 2a 20 76 61 6c 75  he.      ** valu
1cd92 65 20 6f 66 20 74 68 69 73 20 73 65 6c 65 63 74  e of this select
1cd93 20 69 6e 20 61 20 6d 65 6d 6f 72 79 20 63 65 6c   in a memory cel
1cd94 6c 20 61 6e 64 20 72 65 63 6f 72 64 20 74 68 65  l and record the
1cd95 20 6e 75 6d 62 65 72 0a 20 20 20 20 20 20 2a 2a   number.      **
1cd96 20 6f 66 20 74 68 65 20 6d 65 6d 6f 72 79 20 63   of the memory c
1cd97 65 6c 6c 20 69 6e 20 69 43 6f 6c 75 6d 6e 2e 0a  ell in iColumn..
1cd98 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73        */.      s
1cd99 74 61 74 69 63 20 63 6f 6e 73 74 20 54 6f 6b 65  tatic const Toke
1cd9a 6e 20 6f 6e 65 20 3d 20 7b 20 28 75 38 2a 29 22  n one = { (u8*)"
1cd9b 31 22 2c 20 30 2c 20 31 20 7d 3b 0a 20 20 20 20  1", 0, 1 };.    
1cd9c 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 0a    Select *pSel;.
1cd9d 20 20 20 20 20 20 53 65 6c 65 63 74 44 65 73 74        SelectDest
1cd9e 20 64 65 73 74 3b 0a 0a 20 20 20 20 20 20 70 53   dest;..      pS
1cd9f 65 6c 20 3d 20 70 45 78 70 72 2d 3e 70 53 65 6c  el = pExpr->pSel
1cda0 65 63 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ect;.      sqlit
1cda1 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
1cda2 28 26 64 65 73 74 2c 20 30 2c 20 2b 2b 70 50 61  (&dest, 0, ++pPa
1cda3 72 73 65 2d 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20  rse->nMem);.    
1cda4 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70 3d    if( pExpr->op=
1cda5 3d 54 4b 5f 53 45 4c 45 43 54 20 29 7b 0a 20 20  =TK_SELECT ){.  
1cda6 20 20 20 20 20 20 64 65 73 74 2e 65 44 65 73 74        dest.eDest
1cda7 20 3d 20 53 52 54 5f 4d 65 6d 3b 0a 20 20 20 20   = SRT_Mem;.    
1cda8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cda9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp2(v, OP_Null
1cdaa 2c 20 30 2c 20 64 65 73 74 2e 69 50 61 72 6d 29  , 0, dest.iParm)
1cdab 3b 0a 20 20 20 20 20 20 20 20 56 64 62 65 43 6f  ;.        VdbeCo
1cdac 6d 6d 65 6e 74 28 28 76 2c 20 22 49 6e 69 74 20  mment((v, "Init 
1cdad 73 75 62 71 75 65 72 79 20 72 65 73 75 6c 74 22  subquery result"
1cdae 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  ));.      }else{
1cdaf 0a 20 20 20 20 20 20 20 20 64 65 73 74 2e 65 44  .        dest.eD
1cdb0 65 73 74 20 3d 20 53 52 54 5f 45 78 69 73 74 73  est = SRT_Exists
1cdb1 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1cdb2 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1cdb3 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 64 65  P_Integer, 0, de
1cdb4 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20  st.iParm);.     
1cdb5 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
1cdb6 76 2c 20 22 49 6e 69 74 20 45 58 49 53 54 53 20  v, "Init EXISTS 
1cdb7 72 65 73 75 6c 74 22 29 29 3b 0a 20 20 20 20 20  result"));.     
1cdb8 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1cdb9 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73  ExprDelete(pPars
1cdba 65 2d 3e 64 62 2c 20 70 53 65 6c 2d 3e 70 4c 69  e->db, pSel->pLi
1cdbb 6d 69 74 29 3b 0a 20 20 20 20 20 20 70 53 65 6c  mit);.      pSel
1cdbc 2d 3e 70 4c 69 6d 69 74 20 3d 20 73 71 6c 69 74  ->pLimit = sqlit
1cdbd 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
1cdbe 54 4b 5f 49 4e 54 45 47 45 52 2c 20 30 2c 20 30  TK_INTEGER, 0, 0
1cdbf 2c 20 26 6f 6e 65 29 3b 0a 20 20 20 20 20 20 69  , &one);.      i
1cdc0 66 28 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  f( sqlite3Select
1cdc1 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2c 20 26  (pParse, pSel, &
1cdc2 64 65 73 74 29 20 29 7b 0a 20 20 20 20 20 20 20  dest) ){.       
1cdc3 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d   return;.      }
1cdc4 0a 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43  .      pExpr->iC
1cdc5 6f 6c 75 6d 6e 20 3d 20 64 65 73 74 2e 69 50 61  olumn = dest.iPa
1cdc6 72 6d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  rm;.      break;
1cdc7 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66  .    }.  }..  if
1cdc8 28 20 74 65 73 74 41 64 64 72 20 29 7b 0a 20 20  ( testAddr ){.  
1cdc9 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1cdca 70 48 65 72 65 28 76 2c 20 74 65 73 74 41 64 64  pHere(v, testAdd
1cdcb 72 2d 31 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  r-1);.  }..  ret
1cdcc 75 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  urn;.}.#endif /*
1cdcd 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
1cdce 51 55 45 52 59 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  QUERY */../*.** 
1cdcf 44 75 70 6c 69 63 61 74 65 20 61 6e 20 38 2d 62  Duplicate an 8-b
1cdd0 79 74 65 20 76 61 6c 75 65 0a 2a 2f 0a 73 74 61  yte value.*/.sta
1cdd1 74 69 63 20 63 68 61 72 20 2a 64 75 70 38 62 79  tic char *dup8by
1cdd2 74 65 73 28 56 64 62 65 20 2a 76 2c 20 63 6f 6e  tes(Vdbe *v, con
1cdd3 73 74 20 63 68 61 72 20 2a 69 6e 29 7b 0a 20 20  st char *in){.  
1cdd4 63 68 61 72 20 2a 6f 75 74 20 3d 20 73 71 6c 69  char *out = sqli
1cdd5 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 73  te3DbMallocRaw(s
1cdd6 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29 2c  qlite3VdbeDb(v),
1cdd7 20 38 29 3b 0a 20 20 69 66 28 20 6f 75 74 20 29   8);.  if( out )
1cdd8 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28 6f 75 74  {.    memcpy(out
1cdd9 2c 20 69 6e 2c 20 38 29 3b 0a 20 20 7d 0a 20 20  , in, 8);.  }.  
1cdda 72 65 74 75 72 6e 20 6f 75 74 3b 0a 7d 0a 0a 2f  return out;.}../
1cddb 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e  *.** Generate an
1cddc 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68 61   instruction tha
1cddd 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20 66  t will put the f
1cdde 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 2a 2a  loating point.**
1cddf 20 76 61 6c 75 65 20 64 65 73 63 72 69 62 65 64   value described
1cde0 20 62 79 20 7a 5b 30 2e 2e 6e 2d 31 5d 20 69 6e   by z[0..n-1] in
1cde1 74 6f 20 72 65 67 69 73 74 65 72 20 69 4d 65 6d  to register iMem
1cde2 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 7a 5b 5d 20  ..**.** The z[] 
1cde3 73 74 72 69 6e 67 20 77 69 6c 6c 20 70 72 6f 62  string will prob
1cde4 61 62 6c 79 20 6e 6f 74 20 62 65 20 7a 65 72 6f  ably not be zero
1cde5 2d 74 65 72 6d 69 6e 61 74 65 64 2e 20 20 42 75  -terminated.  Bu
1cde6 74 20 74 68 65 20 0a 2a 2a 20 7a 5b 6e 5d 20 63  t the .** z[n] c
1cde7 68 61 72 61 63 74 65 72 20 69 73 20 67 75 61 72  haracter is guar
1cde8 61 6e 74 65 65 64 20 74 6f 20 62 65 20 73 6f 6d  anteed to be som
1cde9 65 74 68 69 6e 67 20 74 68 61 74 20 64 6f 65 73  ething that does
1cdea 20 6e 6f 74 20 6c 6f 6f 6b 0a 2a 2a 20 6c 69 6b   not look.** lik
1cdeb 65 20 74 68 65 20 63 6f 6e 74 69 6e 75 61 74 69  e the continuati
1cdec 6f 6e 20 6f 66 20 74 68 65 20 6e 75 6d 62 65 72  on of the number
1cded 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1cdee 20 63 6f 64 65 52 65 61 6c 28 56 64 62 65 20 2a   codeReal(Vdbe *
1cdef 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  v, const char *z
1cdf0 2c 20 69 6e 74 20 6e 2c 20 69 6e 74 20 6e 65 67  , int n, int neg
1cdf1 61 74 65 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65  ateFlag, int iMe
1cdf2 6d 29 7b 0a 20 20 61 73 73 65 72 74 28 20 7a 20  m){.  assert( z 
1cdf3 7c 7c 20 76 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  || v==0 || sqlit
1cdf4 65 33 56 64 62 65 44 62 28 76 29 2d 3e 6d 61 6c  e3VdbeDb(v)->mal
1cdf5 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61  locFailed );.  a
1cdf6 73 73 65 72 74 28 20 21 7a 20 7c 7c 20 21 69 73  ssert( !z || !is
1cdf7 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20  digit(z[n]) );. 
1cdf8 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
1cdf9 52 28 6e 29 3b 0a 20 20 69 66 28 20 7a 20 29 7b  R(n);.  if( z ){
1cdfa 0a 20 20 20 20 64 6f 75 62 6c 65 20 76 61 6c 75  .    double valu
1cdfb 65 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 56 3b  e;.    char *zV;
1cdfc 0a 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f 46  .    sqlite3AtoF
1cdfd 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a 20 20 20  (z, &value);.   
1cdfe 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 4e 61   if( sqlite3IsNa
1cdff 4e 28 76 61 6c 75 65 29 20 29 7b 0a 20 20 20 20  N(value) ){.    
1ce00 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ce01 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
1ce02 30 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 7d 65  0, iMem);.    }e
1ce03 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 6e  lse{.      if( n
1ce04 65 67 61 74 65 46 6c 61 67 20 29 20 76 61 6c 75  egateFlag ) valu
1ce05 65 20 3d 20 2d 76 61 6c 75 65 3b 0a 20 20 20 20  e = -value;.    
1ce06 20 20 7a 56 20 3d 20 64 75 70 38 62 79 74 65 73    zV = dup8bytes
1ce07 28 76 2c 20 28 63 68 61 72 2a 29 26 76 61 6c 75  (v, (char*)&valu
1ce08 65 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  e);.      sqlite
1ce09 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1ce0a 50 5f 52 65 61 6c 2c 20 30 2c 20 69 4d 65 6d 2c  P_Real, 0, iMem,
1ce0b 20 30 2c 20 7a 56 2c 20 50 34 5f 52 45 41 4c 29   0, zV, P4_REAL)
1ce0c 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a  ;.    }.  }.}...
1ce0d 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 61  /*.** Generate a
1ce0e 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 74 68  n instruction th
1ce0f 61 74 20 77 69 6c 6c 20 70 75 74 20 74 68 65 20  at will put the 
1ce10 69 6e 74 65 67 65 72 20 64 65 73 63 72 69 62 65  integer describe
1ce11 20 62 79 0a 2a 2a 20 74 65 78 74 20 7a 5b 30 2e   by.** text z[0.
1ce12 2e 6e 2d 31 5d 20 69 6e 74 6f 20 72 65 67 69 73  .n-1] into regis
1ce13 74 65 72 20 69 4d 65 6d 2e 0a 2a 2a 0a 2a 2a 20  ter iMem..**.** 
1ce14 54 68 65 20 7a 5b 5d 20 73 74 72 69 6e 67 20 77  The z[] string w
1ce15 69 6c 6c 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74  ill probably not
1ce16 20 62 65 20 7a 65 72 6f 2d 74 65 72 6d 69 6e 61   be zero-termina
1ce17 74 65 64 2e 20 20 42 75 74 20 74 68 65 20 0a 2a  ted.  But the .*
1ce18 2a 20 7a 5b 6e 5d 20 63 68 61 72 61 63 74 65 72  * z[n] character
1ce19 20 69 73 20 67 75 61 72 61 6e 74 65 65 64 20 74   is guaranteed t
1ce1a 6f 20 62 65 20 73 6f 6d 65 74 68 69 6e 67 20 74  o be something t
1ce1b 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6c 6f 6f  hat does not loo
1ce1c 6b 0a 2a 2a 20 6c 69 6b 65 20 74 68 65 20 63 6f  k.** like the co
1ce1d 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74 68  ntinuation of th
1ce1e 65 20 6e 75 6d 62 65 72 2e 0a 2a 2f 0a 73 74 61  e number..*/.sta
1ce1f 74 69 63 20 76 6f 69 64 20 63 6f 64 65 49 6e 74  tic void codeInt
1ce20 65 67 65 72 28 56 64 62 65 20 2a 76 2c 20 45 78  eger(Vdbe *v, Ex
1ce21 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 6e  pr *pExpr, int n
1ce22 65 67 46 6c 61 67 2c 20 69 6e 74 20 69 4d 65 6d  egFlag, int iMem
1ce23 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
1ce24 2a 7a 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  *z;.  if( pExpr-
1ce25 3e 66 6c 61 67 73 20 26 20 45 50 5f 49 6e 74 56  >flags & EP_IntV
1ce26 61 6c 75 65 20 29 7b 0a 20 20 20 20 69 6e 74 20  alue ){.    int 
1ce27 69 20 3d 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  i = pExpr->iTabl
1ce28 65 3b 0a 20 20 20 20 69 66 28 20 6e 65 67 46 6c  e;.    if( negFl
1ce29 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20  ag ) i = -i;.   
1ce2a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ce2b 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1ce2c 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20 7d 65  , i, iMem);.  }e
1ce2d 6c 73 65 20 69 66 28 20 28 7a 20 3d 20 28 63 68  lse if( (z = (ch
1ce2e 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
1ce2f 2e 7a 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  .z)!=0 ){.    in
1ce30 74 20 69 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d  t i;.    int n =
1ce31 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3b   pExpr->token.n;
1ce32 0a 20 20 20 20 61 73 73 65 72 74 28 20 21 69 73  .    assert( !is
1ce33 64 69 67 69 74 28 7a 5b 6e 5d 29 20 29 3b 0a 20  digit(z[n]) );. 
1ce34 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 65     if( sqlite3Ge
1ce35 74 49 6e 74 33 32 28 7a 2c 20 26 69 29 20 29 7b  tInt32(z, &i) ){
1ce36 0a 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c  .      if( negFl
1ce37 61 67 20 29 20 69 20 3d 20 2d 69 3b 0a 20 20 20  ag ) i = -i;.   
1ce38 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ce39 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
1ce3a 65 72 2c 20 69 2c 20 69 4d 65 6d 29 3b 0a 20 20  er, i, iMem);.  
1ce3b 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
1ce3c 74 65 33 46 69 74 73 49 6e 36 34 42 69 74 73 28  te3FitsIn64Bits(
1ce3d 7a 2c 20 6e 65 67 46 6c 61 67 29 20 29 7b 0a 20  z, negFlag) ){. 
1ce3e 20 20 20 20 20 69 36 34 20 76 61 6c 75 65 3b 0a       i64 value;.
1ce3f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 56 3b 0a        char *zV;.
1ce40 20 20 20 20 20 20 73 71 6c 69 74 65 33 41 74 6f        sqlite3Ato
1ce41 69 36 34 28 7a 2c 20 26 76 61 6c 75 65 29 3b 0a  i64(z, &value);.
1ce42 20 20 20 20 20 20 69 66 28 20 6e 65 67 46 6c 61        if( negFla
1ce43 67 20 29 20 76 61 6c 75 65 20 3d 20 2d 76 61 6c  g ) value = -val
1ce44 75 65 3b 0a 20 20 20 20 20 20 7a 56 20 3d 20 64  ue;.      zV = d
1ce45 75 70 38 62 79 74 65 73 28 76 2c 20 28 63 68 61  up8bytes(v, (cha
1ce46 72 2a 29 26 76 61 6c 75 65 29 3b 0a 20 20 20 20  r*)&value);.    
1ce47 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ce48 4f 70 34 28 76 2c 20 4f 50 5f 49 6e 74 36 34 2c  Op4(v, OP_Int64,
1ce49 20 30 2c 20 69 4d 65 6d 2c 20 30 2c 20 7a 56 2c   0, iMem, 0, zV,
1ce4a 20 50 34 5f 49 4e 54 36 34 29 3b 0a 20 20 20 20   P4_INT64);.    
1ce4b 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 6f 64  }else{.      cod
1ce4c 65 52 65 61 6c 28 76 2c 20 7a 2c 20 6e 2c 20 6e  eReal(v, z, n, n
1ce4d 65 67 46 6c 61 67 2c 20 69 4d 65 6d 29 3b 0a 20  egFlag, iMem);. 
1ce4e 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a     }.  }.}.../*.
1ce4f 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1ce50 20 74 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61   that will extra
1ce51 63 74 20 74 68 65 20 69 43 6f 6c 75 6d 6e 2d 74  ct the iColumn-t
1ce52 68 20 63 6f 6c 75 6d 6e 20 66 72 6f 6d 0a 2a 2a  h column from.**
1ce53 20 74 61 62 6c 65 20 70 54 61 62 20 61 6e 64 20   table pTab and 
1ce54 73 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e  store the column
1ce55 20 76 61 6c 75 65 20 69 6e 20 61 20 72 65 67 69   value in a regi
1ce56 73 74 65 72 2e 20 20 41 6e 20 65 66 66 6f 72 74  ster.  An effort
1ce57 0a 2a 2a 20 69 73 20 6d 61 64 65 20 74 6f 20 73  .** is made to s
1ce58 74 6f 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  tore the column 
1ce59 76 61 6c 75 65 20 69 6e 20 72 65 67 69 73 74 65  value in registe
1ce5a 72 20 69 52 65 67 2c 20 62 75 74 20 74 68 69 73  r iReg, but this
1ce5b 20 69 73 0a 2a 2a 20 6e 6f 74 20 67 75 61 72 61   is.** not guara
1ce5c 6e 74 65 65 64 2e 20 20 54 68 65 20 6c 6f 63 61  nteed.  The loca
1ce5d 74 69 6f 6e 20 6f 66 20 74 68 65 20 63 6f 6c 75  tion of the colu
1ce5e 6d 6e 20 76 61 6c 75 65 20 69 73 20 72 65 74 75  mn value is retu
1ce5f 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72  rned..**.** Ther
1ce60 65 20 6d 75 73 74 20 62 65 20 61 6e 20 6f 70 65  e must be an ope
1ce61 6e 20 63 75 72 73 6f 72 20 74 6f 20 70 54 61 62  n cursor to pTab
1ce62 20 69 6e 20 69 54 61 62 6c 65 20 77 68 65 6e 20   in iTable when 
1ce63 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20  this routine.** 
1ce64 69 73 20 63 61 6c 6c 65 64 2e 20 20 49 66 20 69  is called.  If i
1ce65 43 6f 6c 75 6d 6e 3c 30 20 74 68 65 6e 20 63 6f  Column<0 then co
1ce66 64 65 20 69 73 20 67 65 6e 65 72 61 74 65 64 20  de is generated 
1ce67 74 68 61 74 20 65 78 74 72 61 63 74 73 20 74 68  that extracts th
1ce68 65 20 72 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54  e rowid..**.** T
1ce69 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 69 67 68  his routine migh
1ce6a 74 20 61 74 74 65 6d 70 74 20 74 6f 20 72 65 75  t attempt to reu
1ce6b 73 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  se the value of 
1ce6c 74 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 0a  the column that.
1ce6d 2a 2a 20 68 61 73 20 61 6c 72 65 61 64 79 20 62  ** has already b
1ce6e 65 65 6e 20 6c 6f 61 64 65 64 20 69 6e 74 6f 20  een loaded into 
1ce6f 61 20 72 65 67 69 73 74 65 72 2e 20 20 54 68 65  a register.  The
1ce70 20 76 61 6c 75 65 20 77 69 6c 6c 20 61 6c 77 61   value will alwa
1ce71 79 73 0a 2a 2a 20 62 65 20 75 73 65 64 20 69 66  ys.** be used if
1ce72 20 69 74 20 68 61 73 20 6e 6f 74 20 75 6e 64 65   it has not unde
1ce73 72 67 6f 6e 65 20 61 6e 79 20 61 66 66 69 6e 69  rgone any affini
1ce74 74 79 20 63 68 61 6e 67 65 73 2e 20 20 42 75 74  ty changes.  But
1ce75 20 69 66 0a 2a 2a 20 61 6e 20 61 66 66 69 6e 69   if.** an affini
1ce76 74 79 20 63 68 61 6e 67 65 20 68 61 73 20 6f 63  ty change has oc
1ce77 63 75 72 72 65 64 2c 20 74 68 65 6e 20 74 68 65  curred, then the
1ce78 20 63 61 63 68 65 64 20 76 61 6c 75 65 20 77 69   cached value wi
1ce79 6c 6c 20 6f 6e 6c 79 20 62 65 0a 2a 2a 20 75 73  ll only be.** us
1ce7a 65 64 20 69 66 20 61 6c 6c 6f 77 41 66 66 43 68  ed if allowAffCh
1ce7b 6e 67 20 69 73 20 74 72 75 65 2e 0a 2a 2f 0a 53  ng is true..*/.S
1ce7c 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1ce7d 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
1ce7e 65 47 65 74 43 6f 6c 75 6d 6e 28 0a 20 20 50 61  eGetColumn(.  Pa
1ce7f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 2f  rse *pParse,   /
1ce80 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
1ce81 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
1ce82 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
1ce83 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a 20 44   *pTab,     /* D
1ce84 65 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68  escription of th
1ce85 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20 72  e table we are r
1ce86 65 61 64 69 6e 67 20 66 72 6f 6d 20 2a 2f 0a 20  eading from */. 
1ce87 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20   int iColumn,   
1ce88 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
1ce89 65 20 74 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 2a  e table column *
1ce8a 2f 0a 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20  /.  int iTable, 
1ce8b 20 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73       /* The curs
1ce8c 6f 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74  or pointing to t
1ce8d 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  he table */.  in
1ce8e 74 20 69 52 65 67 2c 20 20 20 20 20 20 20 20 2f  t iReg,        /
1ce8f 2a 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73 20  * Store results 
1ce90 68 65 72 65 20 2a 2f 0a 20 20 69 6e 74 20 61 6c  here */.  int al
1ce91 6c 6f 77 41 66 66 43 68 6e 67 20 2f 2a 20 54 72  lowAffChng /* Tr
1ce92 75 65 20 69 66 20 70 72 69 6f 72 20 61 66 66 69  ue if prior affi
1ce93 6e 69 74 79 20 63 68 61 6e 67 65 73 20 61 72 65  nity changes are
1ce94 20 4f 4b 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65   OK */.){.  Vdbe
1ce95 20 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56   *v = pParse->pV
1ce96 64 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  dbe;.  int i;.  
1ce97 73 74 72 75 63 74 20 79 43 6f 6c 43 61 63 68 65  struct yColCache
1ce98 20 2a 70 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 2c   *p;..  for(i=0,
1ce99 20 70 3d 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43   p=pParse->aColC
1ce9a 61 63 68 65 3b 20 69 3c 70 50 61 72 73 65 2d 3e  ache; i<pParse->
1ce9b 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 2c 20  nColCache; i++, 
1ce9c 70 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  p++){.    if( p-
1ce9d 3e 69 54 61 62 6c 65 3d 3d 69 54 61 62 6c 65 20  >iTable==iTable 
1ce9e 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 69  && p->iColumn==i
1ce9f 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 20 20  Column.         
1cea0 20 20 26 26 20 28 21 70 2d 3e 61 66 66 43 68 61    && (!p->affCha
1cea1 6e 67 65 20 7c 7c 20 61 6c 6c 6f 77 41 66 66 43  nge || allowAffC
1cea2 68 6e 67 29 20 29 7b 0a 23 69 66 20 30 0a 20 20  hng) ){.#if 0.  
1cea3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1cea4 64 64 4f 70 30 28 76 2c 20 4f 50 5f 4e 6f 6f 70  ddOp0(v, OP_Noop
1cea5 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
1cea6 6d 65 6e 74 28 28 76 2c 20 22 4f 50 54 3a 20 74  ment((v, "OPT: t
1cea7 61 62 25 64 2e 63 6f 6c 25 64 20 2d 3e 20 72 25  ab%d.col%d -> r%
1cea8 64 22 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  d", iTable, iCol
1cea9 75 6d 6e 2c 20 70 2d 3e 69 52 65 67 29 29 3b 0a  umn, p->iReg));.
1ceaa 23 65 6e 64 69 66 0a 20 20 20 20 20 20 72 65 74  #endif.      ret
1ceab 75 72 6e 20 70 2d 3e 69 52 65 67 3b 0a 20 20 20  urn p->iReg;.   
1ceac 20 7d 0a 20 20 7d 20 20 0a 20 20 61 73 73 65 72   }.  }  .  asser
1cead 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 69 66 28  t( v!=0 );.  if(
1ceae 20 69 43 6f 6c 75 6d 6e 3c 30 20 29 7b 0a 20 20   iColumn<0 ){.  
1ceaf 20 20 69 6e 74 20 6f 70 20 3d 20 28 70 54 61 62    int op = (pTab
1ceb0 20 26 26 20 49 73 56 69 72 74 75 61 6c 28 70 54   && IsVirtual(pT
1ceb1 61 62 29 29 20 3f 20 4f 50 5f 56 52 6f 77 69 64  ab)) ? OP_VRowid
1ceb2 20 3a 20 4f 50 5f 52 6f 77 69 64 3b 0a 20 20 20   : OP_Rowid;.   
1ceb3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1ceb4 70 32 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65  p2(v, op, iTable
1ceb5 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65 6c 73 65  , iReg);.  }else
1ceb6 20 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a   if( pTab==0 ){.
1ceb7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ceb8 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
1ceb9 6d 6e 2c 20 69 54 61 62 6c 65 2c 20 69 43 6f 6c  mn, iTable, iCol
1ceba 75 6d 6e 2c 20 69 52 65 67 29 3b 0a 20 20 7d 65  umn, iReg);.  }e
1cebb 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6f 70 20  lse{.    int op 
1cebc 3d 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62  = IsVirtual(pTab
1cebd 29 20 3f 20 4f 50 5f 56 43 6f 6c 75 6d 6e 20 3a  ) ? OP_VColumn :
1cebe 20 4f 50 5f 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20   OP_Column;.    
1cebf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1cec0 33 28 76 2c 20 6f 70 2c 20 69 54 61 62 6c 65 2c  3(v, op, iTable,
1cec1 20 69 43 6f 6c 75 6d 6e 2c 20 69 52 65 67 29 3b   iColumn, iReg);
1cec2 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 6c 75  .    sqlite3Colu
1cec3 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70 54 61  mnDefault(v, pTa
1cec4 62 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 23 69 66  b, iColumn);.#if
1cec5 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1cec6 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
1cec7 20 20 20 20 69 66 28 20 70 54 61 62 2d 3e 61 43      if( pTab->aC
1cec8 6f 6c 5b 69 43 6f 6c 75 6d 6e 5d 2e 61 66 66 69  ol[iColumn].affi
1cec9 6e 69 74 79 3d 3d 53 51 4c 49 54 45 5f 41 46 46  nity==SQLITE_AFF
1ceca 5f 52 45 41 4c 20 29 7b 0a 20 20 20 20 20 20 73  _REAL ){.      s
1cecb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
1cecc 28 76 2c 20 4f 50 5f 52 65 61 6c 41 66 66 69 6e  (v, OP_RealAffin
1cecd 69 74 79 2c 20 69 52 65 67 29 3b 0a 20 20 20 20  ity, iReg);.    
1cece 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 20 20 69  }.#endif.  }.  i
1cecf 66 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62  f( pParse->disab
1ced0 6c 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 7b  leColCache==0 ){
1ced1 0a 20 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d  .    i = pParse-
1ced2 3e 69 43 6f 6c 43 61 63 68 65 3b 0a 20 20 20 20  >iColCache;.    
1ced3 70 20 3d 20 26 70 50 61 72 73 65 2d 3e 61 43 6f  p = &pParse->aCo
1ced4 6c 43 61 63 68 65 5b 69 5d 3b 0a 20 20 20 20 70  lCache[i];.    p
1ced5 2d 3e 69 54 61 62 6c 65 20 3d 20 69 54 61 62 6c  ->iTable = iTabl
1ced6 65 3b 0a 20 20 20 20 70 2d 3e 69 43 6f 6c 75 6d  e;.    p->iColum
1ced7 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  n = iColumn;.   
1ced8 20 70 2d 3e 69 52 65 67 20 3d 20 69 52 65 67 3b   p->iReg = iReg;
1ced9 0a 20 20 20 20 70 2d 3e 61 66 66 43 68 61 6e 67  .    p->affChang
1ceda 65 20 3d 20 30 3b 0a 20 20 20 20 69 2b 2b 3b 0a  e = 0;.    i++;.
1cedb 20 20 20 20 69 66 28 20 69 3e 3d 41 72 72 61 79      if( i>=Array
1cedc 53 69 7a 65 28 70 50 61 72 73 65 2d 3e 61 43 6f  Size(pParse->aCo
1cedd 6c 43 61 63 68 65 29 20 29 20 69 20 3d 20 30 3b  lCache) ) i = 0;
1cede 0a 20 20 20 20 69 66 28 20 69 3e 70 50 61 72 73  .    if( i>pPars
1cedf 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 20 29 20 70  e->nColCache ) p
1cee0 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
1cee1 20 3d 20 69 3b 0a 20 20 20 20 70 50 61 72 73 65   = i;.    pParse
1cee2 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 69 3b  ->iColCache = i;
1cee3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 52  .  }.  return iR
1cee4 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65  eg;.}../*.** Cle
1cee5 61 72 20 61 6c 6c 20 63 6f 6c 75 6d 6e 20 63 61  ar all column ca
1cee6 63 68 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f  che entries asso
1cee7 63 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20  ciated with the 
1cee8 76 64 62 65 0a 2a 2a 20 63 75 72 73 6f 72 20 77  vdbe.** cursor w
1cee9 69 74 68 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ith cursor numbe
1ceea 72 20 69 54 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  r iTable..*/.SQL
1ceeb 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1ceec 20 73 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61   sqlite3ExprClea
1ceed 72 43 6f 6c 75 6d 6e 43 61 63 68 65 28 50 61 72  rColumnCache(Par
1ceee 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20  se *pParse, int 
1ceef 69 54 61 62 6c 65 29 7b 0a 20 20 69 66 28 20 69  iTable){.  if( i
1cef0 54 61 62 6c 65 3c 30 20 29 7b 0a 20 20 20 20 70  Table<0 ){.    p
1cef1 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65  Parse->nColCache
1cef2 20 3d 20 30 3b 0a 20 20 20 20 70 50 61 72 73 65   = 0;.    pParse
1cef3 2d 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 30 3b  ->iColCache = 0;
1cef4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e  .  }else{.    in
1cef5 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
1cef6 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c  ; i<pParse->nCol
1cef7 43 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20  Cache; i++){.   
1cef8 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 61     if( pParse->a
1cef9 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 54 61 62  ColCache[i].iTab
1cefa 6c 65 3d 3d 69 54 61 62 6c 65 20 29 7b 0a 20 20  le==iTable ){.  
1cefb 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1cefc 69 3d 3d 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43  i==pParse->nColC
1cefd 61 63 68 65 2d 31 20 29 3b 0a 20 20 20 20 20 20  ache-1 );.      
1cefe 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61    pParse->aColCa
1ceff 63 68 65 5b 69 5d 20 3d 20 70 50 61 72 73 65 2d  che[i] = pParse-
1cf00 3e 61 43 6f 6c 43 61 63 68 65 5b 2d 2d 70 50 61  >aColCache[--pPa
1cf01 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 5d 3b  rse->nColCache];
1cf02 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
1cf03 3e 69 43 6f 6c 43 61 63 68 65 20 3d 20 70 50 61  >iColCache = pPa
1cf04 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b 0a  rse->nColCache;.
1cf05 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1cf06 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 72  }.}../*.** Recor
1cf07 64 20 74 68 65 20 66 61 63 74 20 74 68 61 74 20  d the fact that 
1cf08 61 6e 20 61 66 66 69 6e 69 74 79 20 63 68 61 6e  an affinity chan
1cf09 67 65 20 68 61 73 20 6f 63 63 75 72 72 65 64 20  ge has occurred 
1cf0a 6f 6e 20 69 43 6f 75 6e 74 0a 2a 2a 20 72 65 67  on iCount.** reg
1cf0b 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67 20  isters starting 
1cf0c 77 69 74 68 20 69 53 74 61 72 74 2e 0a 2a 2f 0a  with iStart..*/.
1cf0d 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1cf0e 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43  oid sqlite3ExprC
1cf0f 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
1cf10 67 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ge(Parse *pParse
1cf11 2c 20 69 6e 74 20 69 53 74 61 72 74 2c 20 69 6e  , int iStart, in
1cf12 74 20 69 43 6f 75 6e 74 29 7b 0a 20 20 69 6e 74  t iCount){.  int
1cf13 20 69 45 6e 64 20 3d 20 69 53 74 61 72 74 20 2b   iEnd = iStart +
1cf14 20 69 43 6f 75 6e 74 20 2d 20 31 3b 0a 20 20 69   iCount - 1;.  i
1cf15 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
1cf16 20 69 3c 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43   i<pParse->nColC
1cf17 61 63 68 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ache; i++){.    
1cf18 69 6e 74 20 72 20 3d 20 70 50 61 72 73 65 2d 3e  int r = pParse->
1cf19 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65  aColCache[i].iRe
1cf1a 67 3b 0a 20 20 20 20 69 66 28 20 72 3e 3d 69 53  g;.    if( r>=iS
1cf1b 74 61 72 74 20 26 26 20 72 3c 3d 69 45 6e 64 20  tart && r<=iEnd 
1cf1c 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  ){.      pParse-
1cf1d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 2e 61 66  >aColCache[i].af
1cf1e 66 43 68 61 6e 67 65 20 3d 20 31 3b 0a 20 20 20  fChange = 1;.   
1cf1f 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
1cf20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
1cf21 20 6d 6f 76 65 20 63 6f 6e 74 65 6e 74 20 66 72   move content fr
1cf22 6f 6d 20 72 65 67 69 73 74 65 72 73 20 69 46 72  om registers iFr
1cf23 6f 6d 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d  om...iFrom+nReg-
1cf24 31 0a 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f  1.** over to iTo
1cf25 2e 2e 69 54 6f 2b 6e 52 65 67 2d 31 2e 20 4b 65  ..iTo+nReg-1. Ke
1cf26 65 70 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 61  ep the column ca
1cf27 63 68 65 20 75 70 2d 74 6f 2d 64 61 74 65 2e 0a  che up-to-date..
1cf28 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1cf29 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 45 78  E void sqlite3Ex
1cf2a 70 72 43 6f 64 65 4d 6f 76 65 28 50 61 72 73 65  prCodeMove(Parse
1cf2b 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69 46   *pParse, int iF
1cf2c 72 6f 6d 2c 20 69 6e 74 20 69 54 6f 2c 20 69 6e  rom, int iTo, in
1cf2d 74 20 6e 52 65 67 29 7b 0a 20 20 69 6e 74 20 69  t nReg){.  int i
1cf2e 3b 0a 20 20 69 66 28 20 69 46 72 6f 6d 3d 3d 69  ;.  if( iFrom==i
1cf2f 54 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73  To ) return;.  s
1cf30 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1cf31 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20  (pParse->pVdbe, 
1cf32 4f 50 5f 4d 6f 76 65 2c 20 69 46 72 6f 6d 2c 20  OP_Move, iFrom, 
1cf33 69 54 6f 2c 20 6e 52 65 67 29 3b 0a 20 20 66 6f  iTo, nReg);.  fo
1cf34 72 28 69 3d 30 3b 20 69 3c 70 50 61 72 73 65 2d  r(i=0; i<pParse-
1cf35 3e 6e 43 6f 6c 43 61 63 68 65 3b 20 69 2b 2b 29  >nColCache; i++)
1cf36 7b 0a 20 20 20 20 69 6e 74 20 78 20 3d 20 70 50  {.    int x = pP
1cf37 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b  arse->aColCache[
1cf38 69 5d 2e 69 52 65 67 3b 0a 20 20 20 20 69 66 28  i].iReg;.    if(
1cf39 20 78 3e 3d 69 46 72 6f 6d 20 26 26 20 78 3c 69   x>=iFrom && x<i
1cf3a 46 72 6f 6d 2b 6e 52 65 67 20 29 7b 0a 20 20 20  From+nReg ){.   
1cf3b 20 20 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43     pParse->aColC
1cf3c 61 63 68 65 5b 69 5d 2e 69 52 65 67 20 2b 3d 20  ache[i].iReg += 
1cf3d 69 54 6f 2d 69 46 72 6f 6d 3b 0a 20 20 20 20 7d  iTo-iFrom;.    }
1cf3e 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  .  }.}../*.** Ge
1cf3f 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63  nerate code to c
1cf40 6f 70 79 20 63 6f 6e 74 65 6e 74 20 66 72 6f 6d  opy content from
1cf41 20 72 65 67 69 73 74 65 72 73 20 69 46 72 6f 6d   registers iFrom
1cf42 2e 2e 2e 69 46 72 6f 6d 2b 6e 52 65 67 2d 31 0a  ...iFrom+nReg-1.
1cf43 2a 2a 20 6f 76 65 72 20 74 6f 20 69 54 6f 2e 2e  ** over to iTo..
1cf44 69 54 6f 2b 6e 52 65 67 2d 31 2e 0a 2a 2f 0a 53  iTo+nReg-1..*/.S
1cf45 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1cf46 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f  id sqlite3ExprCo
1cf47 64 65 43 6f 70 79 28 50 61 72 73 65 20 2a 70 50  deCopy(Parse *pP
1cf48 61 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c  arse, int iFrom,
1cf49 20 69 6e 74 20 69 54 6f 2c 20 69 6e 74 20 6e 52   int iTo, int nR
1cf4a 65 67 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  eg){.  int i;.  
1cf4b 69 66 28 20 69 46 72 6f 6d 3d 3d 69 54 6f 20 29  if( iFrom==iTo )
1cf4c 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72 28 69   return;.  for(i
1cf4d 3d 30 3b 20 69 3c 6e 52 65 67 3b 20 69 2b 2b 29  =0; i<nReg; i++)
1cf4e 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
1cf4f 65 41 64 64 4f 70 32 28 70 50 61 72 73 65 2d 3e  eAddOp2(pParse->
1cf50 70 56 64 62 65 2c 20 4f 50 5f 43 6f 70 79 2c 20  pVdbe, OP_Copy, 
1cf51 69 46 72 6f 6d 2b 69 2c 20 69 54 6f 2b 69 29 3b  iFrom+i, iTo+i);
1cf52 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  .  }.}../*.** Re
1cf53 74 75 72 6e 20 74 72 75 65 20 69 66 20 61 6e 79  turn true if any
1cf54 20 72 65 67 69 73 74 65 72 20 69 6e 20 74 68 65   register in the
1cf55 20 72 61 6e 67 65 20 69 46 72 6f 6d 2e 2e 69 54   range iFrom..iT
1cf56 6f 20 28 69 6e 63 6c 75 73 69 76 65 29 0a 2a 2a  o (inclusive).**
1cf57 20 69 73 20 75 73 65 64 20 61 73 20 70 61 72 74   is used as part
1cf58 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63   of the column c
1cf59 61 63 68 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ache..*/.static 
1cf5a 69 6e 74 20 75 73 65 64 41 73 43 6f 6c 75 6d 6e  int usedAsColumn
1cf5b 43 61 63 68 65 28 50 61 72 73 65 20 2a 70 50 61  Cache(Parse *pPa
1cf5c 72 73 65 2c 20 69 6e 74 20 69 46 72 6f 6d 2c 20  rse, int iFrom, 
1cf5d 69 6e 74 20 69 54 6f 29 7b 0a 20 20 69 6e 74 20  int iTo){.  int 
1cf5e 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  i;.  for(i=0; i<
1cf5f 70 50 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68  pParse->nColCach
1cf60 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  e; i++){.    int
1cf61 20 72 20 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f   r = pParse->aCo
1cf62 6c 43 61 63 68 65 5b 69 5d 2e 69 52 65 67 3b 0a  lCache[i].iReg;.
1cf63 20 20 20 20 69 66 28 20 72 3e 3d 69 46 72 6f 6d      if( r>=iFrom
1cf64 20 26 26 20 72 3c 3d 69 54 6f 20 29 20 72 65 74   && r<=iTo ) ret
1cf65 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74  urn 1;.  }.  ret
1cf66 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 0;.}../*.** 
1cf67 54 68 65 72 65 73 20 69 73 20 61 20 76 61 6c 75  Theres is a valu
1cf68 65 20 69 6e 20 72 65 67 69 73 74 65 72 20 69 43  e in register iC
1cf69 75 72 72 65 6e 74 2e 20 20 57 65 20 75 6c 74 69  urrent.  We ulti
1cf6a 6d 61 74 65 6c 79 20 77 61 6e 74 0a 2a 2a 20 74  mately want.** t
1cf6b 68 65 20 76 61 6c 75 65 20 74 6f 20 62 65 20 69  he value to be i
1cf6c 6e 20 72 65 67 69 73 74 65 72 20 69 54 61 72 67  n register iTarg
1cf6d 65 74 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65  et.  It might be
1cf6e 20 74 68 61 74 0a 2a 2a 20 69 43 75 72 72 65 6e   that.** iCurren
1cf6f 74 20 61 6e 64 20 69 54 61 72 67 65 74 20 61 72  t and iTarget ar
1cf70 65 20 74 68 65 20 73 61 6d 65 20 72 65 67 69 73  e the same regis
1cf71 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 72  ter..**.** We ar
1cf72 65 20 67 6f 69 6e 67 20 74 6f 20 6d 6f 64 69 66  e going to modif
1cf73 79 20 74 68 65 20 76 61 6c 75 65 2c 20 73 6f 20  y the value, so 
1cf74 77 65 20 6e 65 65 64 20 74 6f 20 6d 61 6b 65 20  we need to make 
1cf75 73 75 72 65 20 69 74 0a 2a 2a 20 69 73 20 6e 6f  sure it.** is no
1cf76 74 20 61 20 63 61 63 68 65 64 20 72 65 67 69 73  t a cached regis
1cf77 74 65 72 2e 20 20 49 66 20 69 43 75 72 72 65 6e  ter.  If iCurren
1cf78 74 20 69 73 20 61 20 63 61 63 68 65 64 20 72 65  t is a cached re
1cf79 67 69 73 74 65 72 2c 0a 2a 2a 20 74 68 65 6e 20  gister,.** then 
1cf7a 74 72 79 20 74 6f 20 6d 6f 76 65 20 74 68 65 20  try to move the 
1cf7b 76 61 6c 75 65 20 6f 76 65 72 20 74 6f 20 69 54  value over to iT
1cf7c 61 72 67 65 74 2e 20 20 49 66 20 69 54 61 72 67  arget.  If iTarg
1cf7d 65 74 20 69 73 20 61 0a 2a 2a 20 63 61 63 68 65  et is a.** cache
1cf7e 64 20 72 65 67 69 73 74 65 72 2c 20 74 68 65 6e  d register, then
1cf7f 20 63 6c 65 61 72 20 74 68 65 20 63 6f 72 72 65   clear the corre
1cf80 73 70 6f 6e 64 69 6e 67 20 63 61 63 68 65 20 6c  sponding cache l
1cf81 69 6e 65 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72  ine..**.** Retur
1cf82 6e 20 74 68 65 20 72 65 67 69 73 74 65 72 20 74  n the register t
1cf83 68 61 74 20 74 68 65 20 76 61 6c 75 65 20 65 6e  hat the value en
1cf84 64 73 20 75 70 20 69 6e 2e 0a 2a 2f 0a 53 51 4c  ds up in..*/.SQL
1cf85 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1cf86 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74 61  sqlite3ExprWrita
1cf87 62 6c 65 52 65 67 69 73 74 65 72 28 50 61 72 73  bleRegister(Pars
1cf88 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 69  e *pParse, int i
1cf89 43 75 72 72 65 6e 74 2c 20 69 6e 74 20 69 54 61  Current, int iTa
1cf8a 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  rget){.  int i;.
1cf8b 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
1cf8c 2d 3e 70 56 64 62 65 21 3d 30 20 29 3b 0a 20 20  ->pVdbe!=0 );.  
1cf8d 69 66 28 20 21 75 73 65 64 41 73 43 6f 6c 75 6d  if( !usedAsColum
1cf8e 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c 20 69  nCache(pParse, i
1cf8f 43 75 72 72 65 6e 74 2c 20 69 43 75 72 72 65 6e  Current, iCurren
1cf90 74 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  t) ){.    return
1cf91 20 69 43 75 72 72 65 6e 74 3b 0a 20 20 7d 0a 20   iCurrent;.  }. 
1cf92 20 69 66 28 20 69 43 75 72 72 65 6e 74 21 3d 69   if( iCurrent!=i
1cf93 54 61 72 67 65 74 20 29 7b 0a 20 20 20 20 73 71  Target ){.    sq
1cf94 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1cf95 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
1cf96 50 5f 53 43 6f 70 79 2c 20 69 43 75 72 72 65 6e  P_SCopy, iCurren
1cf97 74 2c 20 69 54 61 72 67 65 74 29 3b 0a 20 20 7d  t, iTarget);.  }
1cf98 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50  .  for(i=0; i<pP
1cf99 61 72 73 65 2d 3e 6e 43 6f 6c 43 61 63 68 65 3b  arse->nColCache;
1cf9a 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70   i++){.    if( p
1cf9b 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61 63 68 65  Parse->aColCache
1cf9c 5b 69 5d 2e 69 52 65 67 3d 3d 69 54 61 72 67 65  [i].iReg==iTarge
1cf9d 74 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  t ){.      pPars
1cf9e 65 2d 3e 61 43 6f 6c 43 61 63 68 65 5b 69 5d 20  e->aColCache[i] 
1cf9f 3d 20 70 50 61 72 73 65 2d 3e 61 43 6f 6c 43 61  = pParse->aColCa
1cfa0 63 68 65 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 43  che[--pParse->nC
1cfa1 6f 6c 43 61 63 68 65 5d 3b 0a 20 20 20 20 20 20  olCache];.      
1cfa2 70 50 61 72 73 65 2d 3e 69 43 6f 6c 43 61 63 68  pParse->iColCach
1cfa3 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 43 6f 6c  e = pParse->nCol
1cfa4 43 61 63 68 65 3b 0a 20 20 20 20 7d 0a 20 20 7d  Cache;.    }.  }
1cfa5 0a 20 20 72 65 74 75 72 6e 20 69 54 61 72 67 65  .  return iTarge
1cfa6 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74  t;.}../*.** If t
1cfa7 68 65 20 6c 61 73 74 20 69 6e 73 74 72 75 63 74  he last instruct
1cfa8 69 6f 6e 20 63 6f 64 65 64 20 69 73 20 61 6e 20  ion coded is an 
1cfa9 65 70 68 65 6d 65 72 61 6c 20 63 6f 70 79 20 6f  ephemeral copy o
1cfaa 66 20 61 6e 79 20 6f 66 0a 2a 2a 20 74 68 65 20  f any of.** the 
1cfab 72 65 67 69 73 74 65 72 73 20 69 6e 20 74 68 65  registers in the
1cfac 20 6e 52 65 67 20 72 65 67 69 73 74 65 72 73 20   nReg registers 
1cfad 62 65 67 69 6e 6e 69 6e 67 20 77 69 74 68 20 69  beginning with i
1cfae 52 65 67 2c 20 74 68 65 6e 0a 2a 2a 20 63 6f 6e  Reg, then.** con
1cfaf 76 65 72 74 20 74 68 65 20 6c 61 73 74 20 69 6e  vert the last in
1cfb0 73 74 72 75 63 74 69 6f 6e 20 66 72 6f 6d 20 4f  struction from O
1cfb1 50 5f 53 43 6f 70 79 20 74 6f 20 4f 50 5f 43 6f  P_SCopy to OP_Co
1cfb2 70 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  py..*/.SQLITE_PR
1cfb3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1cfb4 65 33 45 78 70 72 48 61 72 64 43 6f 70 79 28 50  e3ExprHardCopy(P
1cfb5 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1cfb6 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67  t iReg, int nReg
1cfb7 29 7b 0a 20 20 69 6e 74 20 61 64 64 72 3b 0a 20  ){.  int addr;. 
1cfb8 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20 20   VdbeOp *pOp;.  
1cfb9 56 64 62 65 20 2a 76 3b 0a 0a 20 20 76 20 3d 20  Vdbe *v;..  v = 
1cfba 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
1cfbb 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56   addr = sqlite3V
1cfbc 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
1cfbd 29 3b 0a 20 20 70 4f 70 20 3d 20 73 71 6c 69 74  );.  pOp = sqlit
1cfbe 65 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 61  e3VdbeGetOp(v, a
1cfbf 64 64 72 2d 31 29 3b 0a 20 20 61 73 73 65 72 74  ddr-1);.  assert
1cfc0 28 20 70 4f 70 20 7c 7c 20 70 50 61 72 73 65 2d  ( pOp || pParse-
1cfc1 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  >db->mallocFaile
1cfc2 64 20 29 3b 0a 20 20 69 66 28 20 70 4f 70 20 26  d );.  if( pOp &
1cfc3 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f  & pOp->opcode==O
1cfc4 50 5f 53 43 6f 70 79 20 26 26 20 70 4f 70 2d 3e  P_SCopy && pOp->
1cfc5 70 31 3e 3d 69 52 65 67 20 26 26 20 70 4f 70 2d  p1>=iReg && pOp-
1cfc6 3e 70 31 3c 69 52 65 67 2b 6e 52 65 67 20 29 7b  >p1<iReg+nReg ){
1cfc7 0a 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65  .    pOp->opcode
1cfc8 20 3d 20 4f 50 5f 43 6f 70 79 3b 0a 20 20 7d 0a   = OP_Copy;.  }.
1cfc9 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
1cfca 65 20 63 6f 64 65 20 74 6f 20 73 74 6f 72 65 20  e code to store 
1cfcb 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  the value of the
1cfcc 20 69 41 6c 69 61 73 2d 74 68 20 61 6c 69 61 73   iAlias-th alias
1cfcd 20 69 6e 20 72 65 67 69 73 74 65 72 0a 2a 2a 20   in register.** 
1cfce 74 61 72 67 65 74 2e 20 20 54 68 65 20 66 69 72  target.  The fir
1cfcf 73 74 20 74 69 6d 65 20 74 68 69 73 20 69 73 20  st time this is 
1cfd0 63 61 6c 6c 65 64 2c 20 70 45 78 70 72 20 69 73  called, pExpr is
1cfd1 20 65 76 61 6c 75 61 74 65 64 20 74 6f 20 63 6f   evaluated to co
1cfd2 6d 70 75 74 65 0a 2a 2a 20 74 68 65 20 76 61 6c  mpute.** the val
1cfd3 75 65 20 6f 66 20 74 68 65 20 61 6c 69 61 73 2e  ue of the alias.
1cfd4 20 20 54 68 65 20 76 61 6c 75 65 20 69 73 20 73    The value is s
1cfd5 74 6f 72 65 64 20 69 6e 20 61 6e 20 61 75 78 69  tored in an auxi
1cfd6 6c 69 61 72 79 20 72 65 67 69 73 74 65 72 0a 2a  liary register.*
1cfd7 2a 20 61 6e 64 20 74 68 65 20 6e 75 6d 62 65 72  * and the number
1cfd8 20 6f 66 20 74 68 61 74 20 72 65 67 69 73 74 65   of that registe
1cfd9 72 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20  r is returned.  
1cfda 4f 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63 61  On subsequent ca
1cfdb 6c 6c 73 2c 0a 2a 2a 20 74 68 65 20 72 65 67 69  lls,.** the regi
1cfdc 73 74 65 72 20 6e 75 6d 62 65 72 20 69 73 20 72  ster number is r
1cfdd 65 74 75 72 6e 65 64 20 77 69 74 68 6f 75 74 20  eturned without 
1cfde 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 79 20 63  generating any c
1cfdf 6f 64 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20  ode..**.** Note 
1cfe0 74 68 61 74 20 69 6e 20 6f 72 64 65 72 20 66 6f  that in order fo
1cfe1 72 20 74 68 69 73 20 74 6f 20 77 6f 72 6b 2c 20  r this to work, 
1cfe2 63 6f 64 65 20 6d 75 73 74 20 62 65 20 67 65 6e  code must be gen
1cfe3 65 72 61 74 65 64 20 69 6e 20 74 68 65 0a 2a 2a  erated in the.**
1cfe4 20 73 61 6d 65 20 6f 72 64 65 72 20 74 68 61 74   same order that
1cfe5 20 69 74 20 69 73 20 65 78 65 63 75 74 65 64 2e   it is executed.
1cfe6 0a 2a 2a 0a 2a 2a 20 41 6c 69 61 73 65 73 20 61  .**.** Aliases a
1cfe7 72 65 20 6e 75 6d 62 65 72 65 64 20 73 74 61 72  re numbered star
1cfe8 74 69 6e 67 20 77 69 74 68 20 31 2e 20 20 53 6f  ting with 1.  So
1cfe9 20 69 41 6c 69 61 73 20 69 73 20 69 6e 20 74 68   iAlias is in th
1cfea 65 20 72 61 6e 67 65 0a 2a 2a 20 6f 66 20 31 20  e range.** of 1 
1cfeb 74 6f 20 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  to pParse->nAlia
1cfec 73 20 69 6e 63 6c 75 73 69 76 65 2e 20 20 0a 2a  s inclusive.  .*
1cfed 2a 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 61 41 6c  *.** pParse->aAl
1cfee 69 61 73 5b 69 41 6c 69 61 73 2d 31 5d 20 72 65  ias[iAlias-1] re
1cfef 63 6f 72 64 73 20 74 68 65 20 72 65 67 69 73 74  cords the regist
1cff0 65 72 20 6e 75 6d 62 65 72 20 77 68 65 72 65 20  er number where 
1cff1 74 68 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20  the value.** of 
1cff2 74 68 65 20 69 41 6c 69 61 73 2d 74 68 20 61 6c  the iAlias-th al
1cff3 69 61 73 20 69 73 20 73 74 6f 72 65 64 2e 20 20  ias is stored.  
1cff4 49 66 20 7a 65 72 6f 2c 20 74 68 61 74 20 6d 65  If zero, that me
1cff5 61 6e 73 20 74 68 61 74 20 74 68 65 0a 2a 2a 20  ans that the.** 
1cff6 61 6c 69 61 73 20 68 61 73 20 6e 6f 74 20 79 65  alias has not ye
1cff7 74 20 62 65 65 6e 20 63 6f 6d 70 75 74 65 64 2e  t been computed.
1cff8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
1cff9 6f 64 65 41 6c 69 61 73 28 50 61 72 73 65 20 2a  odeAlias(Parse *
1cffa 70 50 61 72 73 65 2c 20 69 6e 74 20 69 41 6c 69  pParse, int iAli
1cffb 61 73 2c 20 45 78 70 72 20 2a 70 45 78 70 72 2c  as, Expr *pExpr,
1cffc 20 69 6e 74 20 74 61 72 67 65 74 29 7b 0a 20 20   int target){.  
1cffd 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1cffe 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
1cfff 69 52 65 67 3b 0a 20 20 69 66 28 20 70 50 61 72  iReg;.  if( pPar
1d000 73 65 2d 3e 61 41 6c 69 61 73 3d 3d 30 20 29 7b  se->aAlias==0 ){
1d001 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 41 6c  .    pParse->aAl
1d002 69 61 73 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  ias = sqlite3DbM
1d003 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 0a 20  allocZero(db, . 
1d004 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d005 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d006 73 69 7a 65 6f 66 28 70 50 61 72 73 65 2d 3e 61  sizeof(pParse->a
1d007 41 6c 69 61 73 5b 30 5d 29 2a 70 50 61 72 73 65  Alias[0])*pParse
1d008 2d 3e 6e 41 6c 69 61 73 20 29 3b 0a 20 20 20 20  ->nAlias );.    
1d009 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
1d00a 69 6c 65 64 20 29 20 72 65 74 75 72 6e 20 30 3b  iled ) return 0;
1d00b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
1d00c 41 6c 69 61 73 3e 30 20 26 26 20 69 41 6c 69 61  Alias>0 && iAlia
1d00d 73 3c 3d 70 50 61 72 73 65 2d 3e 6e 41 6c 69 61  s<=pParse->nAlia
1d00e 73 20 29 3b 0a 20 20 69 52 65 67 20 3d 20 70 50  s );.  iReg = pP
1d00f 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b 69 41 6c  arse->aAlias[iAl
1d010 69 61 73 2d 31 5d 3b 0a 20 20 69 66 28 20 69 52  ias-1];.  if( iR
1d011 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  eg==0 ){.    if(
1d012 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1d013 43 6f 6c 43 61 63 68 65 20 29 7b 0a 20 20 20 20  ColCache ){.    
1d014 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74 65 33    iReg = sqlite3
1d015 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
1d016 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 74 61  Parse, pExpr, ta
1d017 72 67 65 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  rget);.    }else
1d018 7b 0a 20 20 20 20 20 20 69 52 65 67 20 3d 20 2b  {.      iReg = +
1d019 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
1d01a 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1d01b 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
1d01c 70 72 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 20  pr, iReg);.     
1d01d 20 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 5b   pParse->aAlias[
1d01e 69 41 6c 69 61 73 2d 31 5d 20 3d 20 69 52 65 67  iAlias-1] = iReg
1d01f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1d020 74 75 72 6e 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a  turn iReg;.}../*
1d021 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1d022 65 20 69 6e 74 6f 20 74 68 65 20 63 75 72 72 65  e into the curre
1d023 6e 74 20 56 64 62 65 20 74 6f 20 65 76 61 6c 75  nt Vdbe to evalu
1d024 61 74 65 20 74 68 65 20 67 69 76 65 6e 0a 2a 2a  ate the given.**
1d025 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 41 74   expression.  At
1d026 74 65 6d 70 74 20 74 6f 20 73 74 6f 72 65 20 74  tempt to store t
1d027 68 65 20 72 65 73 75 6c 74 73 20 69 6e 20 72 65  he results in re
1d028 67 69 73 74 65 72 20 22 74 61 72 67 65 74 22 2e  gister "target".
1d029 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 72  .** Return the r
1d02a 65 67 69 73 74 65 72 20 77 68 65 72 65 20 72 65  egister where re
1d02b 73 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64  sults are stored
1d02c 2e 0a 2a 2a 0a 2a 2a 20 57 69 74 68 20 74 68 69  ..**.** With thi
1d02d 73 20 72 6f 75 74 69 6e 65 2c 20 74 68 65 72 65  s routine, there
1d02e 20 69 73 20 6e 6f 20 67 75 61 72 61 6e 74 65 65   is no guarantee
1d02f 20 74 68 61 74 20 72 65 73 75 6c 74 73 20 77 69   that results wi
1d030 6c 6c 0a 2a 2a 20 62 65 20 73 74 6f 72 65 64 20  ll.** be stored 
1d031 69 6e 20 74 61 72 67 65 74 2e 20 20 54 68 65 20  in target.  The 
1d032 72 65 73 75 6c 74 20 6d 69 67 68 74 20 62 65 20  result might be 
1d033 73 74 6f 72 65 64 20 69 6e 20 73 6f 6d 65 20 6f  stored in some o
1d034 74 68 65 72 0a 2a 2a 20 72 65 67 69 73 74 65 72  ther.** register
1d035 20 69 66 20 69 74 20 69 73 20 63 6f 6e 76 65 6e   if it is conven
1d036 69 65 6e 74 20 74 6f 20 64 6f 20 73 6f 2e 20 20  ient to do so.  
1d037 54 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63  The calling func
1d038 74 69 6f 6e 0a 2a 2a 20 6d 75 73 74 20 63 68 65  tion.** must che
1d039 63 6b 20 74 68 65 20 72 65 74 75 72 6e 20 63 6f  ck the return co
1d03a 64 65 20 61 6e 64 20 6d 6f 76 65 20 74 68 65 20  de and move the 
1d03b 72 65 73 75 6c 74 73 20 74 6f 20 74 68 65 20 64  results to the d
1d03c 65 73 69 72 65 64 0a 2a 2a 20 72 65 67 69 73 74  esired.** regist
1d03d 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  er..*/.SQLITE_PR
1d03e 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1d03f 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28  3ExprCodeTarget(
1d040 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1d041 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
1d042 74 61 72 67 65 74 29 7b 0a 20 20 56 64 62 65 20  target){.  Vdbe 
1d043 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
1d044 62 65 3b 20 20 2f 2a 20 54 68 65 20 56 4d 20 75  be;  /* The VM u
1d045 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
1d046 6e 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 3b 20 20  n */.  int op;  
1d047 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d048 20 2f 2a 20 54 68 65 20 6f 70 63 6f 64 65 20 62   /* The opcode b
1d049 65 69 6e 67 20 63 6f 64 65 64 20 2a 2f 0a 20 20  eing coded */.  
1d04a 69 6e 74 20 69 6e 52 65 67 20 3d 20 74 61 72 67  int inReg = targ
1d04b 65 74 3b 20 20 20 20 20 20 20 2f 2a 20 52 65 73  et;       /* Res
1d04c 75 6c 74 73 20 73 74 6f 72 65 64 20 69 6e 20 72  ults stored in r
1d04d 65 67 69 73 74 65 72 20 69 6e 52 65 67 20 2a 2f  egister inReg */
1d04e 0a 20 20 69 6e 74 20 72 65 67 46 72 65 65 31 20  .  int regFree1 
1d04f 3d 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  = 0;         /* 
1d050 49 66 20 6e 6f 6e 2d 7a 65 72 6f 20 66 72 65 65  If non-zero free
1d051 20 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20   this temporary 
1d052 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 69 6e  register */.  in
1d053 74 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 20  t regFree2 = 0; 
1d054 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 6e 6f          /* If no
1d055 6e 2d 7a 65 72 6f 20 66 72 65 65 20 74 68 69 73  n-zero free this
1d056 20 74 65 6d 70 6f 72 61 72 79 20 72 65 67 69 73   temporary regis
1d057 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 31 2c  ter */.  int r1,
1d058 20 72 32 2c 20 72 33 2c 20 72 34 3b 20 20 20 20   r2, r3, r4;    
1d059 20 20 20 2f 2a 20 56 61 72 69 6f 75 73 20 72 65     /* Various re
1d05a 67 69 73 74 65 72 20 6e 75 6d 62 65 72 73 20 2a  gister numbers *
1d05b 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
1d05c 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ..  db = pParse-
1d05d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28 20 76  >db;.  assert( v
1d05e 21 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  !=0 || db->mallo
1d05f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73  cFailed );.  ass
1d060 65 72 74 28 20 74 61 72 67 65 74 3e 30 20 26 26  ert( target>0 &&
1d061 20 74 61 72 67 65 74 3c 3d 70 50 61 72 73 65 2d   target<=pParse-
1d062 3e 6e 4d 65 6d 20 29 3b 0a 20 20 69 66 28 20 76  >nMem );.  if( v
1d063 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
1d064 0a 20 20 69 66 28 20 70 45 78 70 72 3d 3d 30 20  .  if( pExpr==0 
1d065 29 7b 0a 20 20 20 20 6f 70 20 3d 20 54 4b 5f 4e  ){.    op = TK_N
1d066 55 4c 4c 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ULL;.  }else{.  
1d067 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70    op = pExpr->op
1d068 3b 0a 20 20 7d 0a 20 20 73 77 69 74 63 68 28 20  ;.  }.  switch( 
1d069 6f 70 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54  op ){.    case T
1d06a 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a  K_AGG_COLUMN: {.
1d06b 20 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70        AggInfo *p
1d06c 41 67 67 49 6e 66 6f 20 3d 20 70 45 78 70 72 2d  AggInfo = pExpr-
1d06d 3e 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20 20 20  >pAggInfo;.     
1d06e 20 73 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f   struct AggInfo_
1d06f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20 26 70 41 67  col *pCol = &pAg
1d070 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 70 45 78 70  gInfo->aCol[pExp
1d071 72 2d 3e 69 41 67 67 5d 3b 0a 20 20 20 20 20 20  r->iAgg];.      
1d072 69 66 28 20 21 70 41 67 67 49 6e 66 6f 2d 3e 64  if( !pAggInfo->d
1d073 69 72 65 63 74 4d 6f 64 65 20 29 7b 0a 20 20 20  irectMode ){.   
1d074 20 20 20 20 20 61 73 73 65 72 74 28 20 70 43 6f       assert( pCo
1d075 6c 2d 3e 69 4d 65 6d 3e 30 20 29 3b 0a 20 20 20  l->iMem>0 );.   
1d076 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 43 6f       inReg = pCo
1d077 6c 2d 3e 69 4d 65 6d 3b 0a 20 20 20 20 20 20 20  l->iMem;.       
1d078 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65   break;.      }e
1d079 6c 73 65 20 69 66 28 20 70 41 67 67 49 6e 66 6f  lse if( pAggInfo
1d07a 2d 3e 75 73 65 53 6f 72 74 69 6e 67 49 64 78 20  ->useSortingIdx 
1d07b 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1d07c 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
1d07d 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 70 41 67 67 49  OP_Column, pAggI
1d07e 6e 66 6f 2d 3e 73 6f 72 74 69 6e 67 49 64 78 2c  nfo->sortingIdx,
1d07f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1d080 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1d081 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
1d082 6d 6e 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20  mn, target);.   
1d083 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1d084 20 20 7d 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68    }.      /* Oth
1d085 65 72 77 69 73 65 2c 20 66 61 6c 6c 20 74 68 72  erwise, fall thr
1d086 75 20 69 6e 74 6f 20 74 68 65 20 54 4b 5f 43 4f  u into the TK_CO
1d087 4c 55 4d 4e 20 63 61 73 65 20 2a 2f 0a 20 20 20  LUMN case */.   
1d088 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43   }.    case TK_C
1d089 4f 4c 55 4d 4e 3a 20 7b 0a 20 20 20 20 20 20 69  OLUMN: {.      i
1d08a 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  f( pExpr->iTable
1d08b 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  <0 ){.        /*
1d08c 20 54 68 69 73 20 6f 6e 6c 79 20 68 61 70 70 65   This only happe
1d08d 6e 73 20 77 68 65 6e 20 63 6f 64 69 6e 67 20 63  ns when coding c
1d08e 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
1d08f 20 2a 2f 0a 20 20 20 20 20 20 20 20 61 73 73 65   */.        asse
1d090 72 74 28 20 70 50 61 72 73 65 2d 3e 63 6b 42 61  rt( pParse->ckBa
1d091 73 65 3e 30 20 29 3b 0a 20 20 20 20 20 20 20 20  se>0 );.        
1d092 69 6e 52 65 67 20 3d 20 70 45 78 70 72 2d 3e 69  inReg = pExpr->i
1d093 43 6f 6c 75 6d 6e 20 2b 20 70 50 61 72 73 65 2d  Column + pParse-
1d094 3e 63 6b 42 61 73 65 3b 0a 20 20 20 20 20 20 7d  >ckBase;.      }
1d095 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 74 65  else{.        te
1d096 73 74 63 61 73 65 28 20 28 70 45 78 70 72 2d 3e  stcase( (pExpr->
1d097 66 6c 61 67 73 20 26 20 45 50 5f 41 6e 79 41 66  flags & EP_AnyAf
1d098 66 29 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  f)!=0 );.       
1d099 20 69 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33   inReg = sqlite3
1d09a 45 78 70 72 43 6f 64 65 47 65 74 43 6f 6c 75 6d  ExprCodeGetColum
1d09b 6e 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  n(pParse, pExpr-
1d09c 3e 70 54 61 62 2c 0a 20 20 20 20 20 20 20 20 20  >pTab,.         
1d09d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d09e 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1d09f 43 6f 6c 75 6d 6e 2c 20 70 45 78 70 72 2d 3e 69  Column, pExpr->i
1d0a0 54 61 62 6c 65 2c 20 74 61 72 67 65 74 2c 0a 20  Table, target,. 
1d0a1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d0a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d0a3 70 45 78 70 72 2d 3e 66 6c 61 67 73 20 26 20 45  pExpr->flags & E
1d0a4 50 5f 41 6e 79 41 66 66 29 3b 0a 20 20 20 20 20  P_AnyAff);.     
1d0a5 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
1d0a6 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54      }.    case T
1d0a7 4b 5f 49 4e 54 45 47 45 52 3a 20 7b 0a 20 20 20  K_INTEGER: {.   
1d0a8 20 20 20 63 6f 64 65 49 6e 74 65 67 65 72 28 76     codeInteger(v
1d0a9 2c 20 70 45 78 70 72 2c 20 30 2c 20 74 61 72 67  , pExpr, 0, targ
1d0aa 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  et);.      break
1d0ab 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1d0ac 20 54 4b 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20   TK_FLOAT: {.   
1d0ad 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28     codeReal(v, (
1d0ae 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
1d0af 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b  en.z, pExpr->tok
1d0b0 65 6e 2e 6e 2c 20 30 2c 20 74 61 72 67 65 74 29  en.n, 0, target)
1d0b1 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d0b2 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1d0b3 5f 53 54 52 49 4e 47 3a 20 7b 0a 20 20 20 20 20  _STRING: {.     
1d0b4 20 73 71 6c 69 74 65 33 44 65 71 75 6f 74 65 45   sqlite3DequoteE
1d0b5 78 70 72 28 64 62 2c 20 70 45 78 70 72 29 3b 0a  xpr(db, pExpr);.
1d0b6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d0b7 65 41 64 64 4f 70 34 28 76 2c 4f 50 5f 53 74 72  eAddOp4(v,OP_Str
1d0b8 69 6e 67 38 2c 20 30 2c 20 74 61 72 67 65 74 2c  ing8, 0, target,
1d0b9 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
1d0ba 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
1d0bb 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
1d0bc 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  z, pExpr->token.
1d0bd 6e 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  n);.      break;
1d0be 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1d0bf 54 4b 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20  TK_NULL: {.     
1d0c0 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d0c1 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
1d0c2 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20 20  , target);.     
1d0c3 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69   break;.    }.#i
1d0c4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1d0c5 54 5f 42 4c 4f 42 5f 4c 49 54 45 52 41 4c 0a 20  T_BLOB_LITERAL. 
1d0c6 20 20 20 63 61 73 65 20 54 4b 5f 42 4c 4f 42 3a     case TK_BLOB:
1d0c7 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e 3b 0a   {.      int n;.
1d0c8 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
1d0c9 20 2a 7a 3b 0a 20 20 20 20 20 20 63 68 61 72 20   *z;.      char 
1d0ca 2a 7a 42 6c 6f 62 3b 0a 20 20 20 20 20 20 61 73  *zBlob;.      as
1d0cb 73 65 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b  sert( pExpr->tok
1d0cc 65 6e 2e 6e 3e 3d 33 20 29 3b 0a 20 20 20 20 20  en.n>=3 );.     
1d0cd 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
1d0ce 74 6f 6b 65 6e 2e 7a 5b 30 5d 3d 3d 27 78 27 20  token.z[0]=='x' 
1d0cf 7c 7c 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  || pExpr->token.
1d0d0 7a 5b 30 5d 3d 3d 27 58 27 20 29 3b 0a 20 20 20  z[0]=='X' );.   
1d0d1 20 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72     assert( pExpr
1d0d2 2d 3e 74 6f 6b 65 6e 2e 7a 5b 31 5d 3d 3d 27 5c  ->token.z[1]=='\
1d0d3 27 27 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  '' );.      asse
1d0d4 72 74 28 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  rt( pExpr->token
1d0d5 2e 7a 5b 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  .z[pExpr->token.
1d0d6 6e 2d 31 5d 3d 3d 27 5c 27 27 20 29 3b 0a 20 20  n-1]=='\'' );.  
1d0d7 20 20 20 20 6e 20 3d 20 70 45 78 70 72 2d 3e 74      n = pExpr->t
1d0d8 6f 6b 65 6e 2e 6e 20 2d 20 33 3b 0a 20 20 20 20  oken.n - 3;.    
1d0d9 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 70 45 78    z = (char*)pEx
1d0da 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 20 2b 20 32 3b  pr->token.z + 2;
1d0db 0a 20 20 20 20 20 20 7a 42 6c 6f 62 20 3d 20 73  .      zBlob = s
1d0dc 71 6c 69 74 65 33 48 65 78 54 6f 42 6c 6f 62 28  qlite3HexToBlob(
1d0dd 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
1d0de 2c 20 7a 2c 20 6e 29 3b 0a 20 20 20 20 20 20 73  , z, n);.      s
1d0df 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1d0e0 28 76 2c 20 4f 50 5f 42 6c 6f 62 2c 20 6e 2f 32  (v, OP_Blob, n/2
1d0e1 2c 20 74 61 72 67 65 74 2c 20 30 2c 20 7a 42 6c  , target, 0, zBl
1d0e2 6f 62 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  ob, P4_DYNAMIC);
1d0e3 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1d0e4 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
1d0e5 61 73 65 20 54 4b 5f 56 41 52 49 41 42 4c 45 3a  ase TK_VARIABLE:
1d0e6 20 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   {.      sqlite3
1d0e7 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1d0e8 5f 56 61 72 69 61 62 6c 65 2c 20 70 45 78 70 72  _Variable, pExpr
1d0e9 2d 3e 69 54 61 62 6c 65 2c 20 74 61 72 67 65 74  ->iTable, target
1d0ea 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45 78  );.      if( pEx
1d0eb 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 3e 31 20 29 7b  pr->token.n>1 ){
1d0ec 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1d0ed 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20  VdbeChangeP4(v, 
1d0ee 2d 31 2c 20 28 63 68 61 72 2a 29 70 45 78 70 72  -1, (char*)pExpr
1d0ef 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70 45 78 70 72  ->token.z, pExpr
1d0f0 2d 3e 74 6f 6b 65 6e 2e 6e 29 3b 0a 20 20 20 20  ->token.n);.    
1d0f1 20 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b    }.      break;
1d0f2 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1d0f3 54 4b 5f 52 45 47 49 53 54 45 52 3a 20 7b 0a 20  TK_REGISTER: {. 
1d0f4 20 20 20 20 20 69 6e 52 65 67 20 3d 20 70 45 78       inReg = pEx
1d0f5 70 72 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20  pr->iTable;.    
1d0f6 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1d0f7 20 20 20 63 61 73 65 20 54 4b 5f 41 53 3a 20 7b     case TK_AS: {
1d0f8 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20 63  .      inReg = c
1d0f9 6f 64 65 41 6c 69 61 73 28 70 50 61 72 73 65 2c  odeAlias(pParse,
1d0fa 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 2c 20   pExpr->iTable, 
1d0fb 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61  pExpr->pLeft, ta
1d0fc 72 67 65 74 29 3b 0a 20 20 20 20 20 20 62 72 65  rget);.      bre
1d0fd 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65  ak;.    }.#ifnde
1d0fe 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41  f SQLITE_OMIT_CA
1d0ff 53 54 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  ST.    case TK_C
1d100 41 53 54 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  AST: {.      /* 
1d101 45 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  Expressions of t
1d102 68 65 20 66 6f 72 6d 3a 20 20 20 43 41 53 54 28  he form:   CAST(
1d103 70 4c 65 66 74 20 41 53 20 74 6f 6b 65 6e 29 20  pLeft AS token) 
1d104 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 66 66  */.      int aff
1d105 2c 20 74 6f 5f 6f 70 3b 0a 20 20 20 20 20 20 69  , to_op;.      i
1d106 6e 52 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78  nReg = sqlite3Ex
1d107 70 72 43 6f 64 65 54 61 72 67 65 74 28 70 50 61  prCodeTarget(pPa
1d108 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1d109 74 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  t, target);.    
1d10a 20 20 61 66 66 20 3d 20 73 71 6c 69 74 65 33 41    aff = sqlite3A
1d10b 66 66 69 6e 69 74 79 54 79 70 65 28 26 70 45 78  ffinityType(&pEx
1d10c 70 72 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20  pr->token);.    
1d10d 20 20 74 6f 5f 6f 70 20 3d 20 61 66 66 20 2d 20    to_op = aff - 
1d10e 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 20  SQLITE_AFF_TEXT 
1d10f 2b 20 4f 50 5f 54 6f 54 65 78 74 3b 0a 20 20 20  + OP_ToText;.   
1d110 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
1d111 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 20 20 20 7c  ==OP_ToText    |
1d112 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
1d113 46 5f 54 45 58 54 20 20 20 20 29 3b 0a 20 20 20  F_TEXT    );.   
1d114 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
1d115 3d 3d 4f 50 5f 54 6f 42 6c 6f 62 20 20 20 20 7c  ==OP_ToBlob    |
1d116 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
1d117 46 5f 4e 4f 4e 45 20 20 20 20 29 3b 0a 20 20 20  F_NONE    );.   
1d118 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
1d119 3d 3d 4f 50 5f 54 6f 4e 75 6d 65 72 69 63 20 7c  ==OP_ToNumeric |
1d11a 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
1d11b 46 5f 4e 55 4d 45 52 49 43 20 29 3b 0a 20 20 20  F_NUMERIC );.   
1d11c 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
1d11d 3d 3d 4f 50 5f 54 6f 49 6e 74 20 20 20 20 20 7c  ==OP_ToInt     |
1d11e 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
1d11f 46 5f 49 4e 54 45 47 45 52 20 29 3b 0a 20 20 20  F_INTEGER );.   
1d120 20 20 20 61 73 73 65 72 74 28 20 74 6f 5f 6f 70     assert( to_op
1d121 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20 20 20 20 7c  ==OP_ToReal    |
1d122 7c 20 61 66 66 21 3d 53 51 4c 49 54 45 5f 41 46  | aff!=SQLITE_AF
1d123 46 5f 52 45 41 4c 20 20 20 20 29 3b 0a 20 20 20  F_REAL    );.   
1d124 20 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f     testcase( to_
1d125 6f 70 3d 3d 4f 50 5f 54 6f 54 65 78 74 20 29 3b  op==OP_ToText );
1d126 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1d127 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 42 6c 6f   to_op==OP_ToBlo
1d128 62 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  b );.      testc
1d129 61 73 65 28 20 74 6f 5f 6f 70 3d 3d 4f 50 5f 54  ase( to_op==OP_T
1d12a 6f 4e 75 6d 65 72 69 63 20 29 3b 0a 20 20 20 20  oNumeric );.    
1d12b 20 20 74 65 73 74 63 61 73 65 28 20 74 6f 5f 6f    testcase( to_o
1d12c 70 3d 3d 4f 50 5f 54 6f 49 6e 74 20 29 3b 0a 20  p==OP_ToInt );. 
1d12d 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 74       testcase( t
1d12e 6f 5f 6f 70 3d 3d 4f 50 5f 54 6f 52 65 61 6c 20  o_op==OP_ToReal 
1d12f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 6e 52  );.      if( inR
1d130 65 67 21 3d 74 61 72 67 65 74 20 29 7b 0a 20 20  eg!=target ){.  
1d131 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d132 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
1d133 6f 70 79 2c 20 69 6e 52 65 67 2c 20 74 61 72 67  opy, inReg, targ
1d134 65 74 29 3b 0a 20 20 20 20 20 20 20 20 69 6e 52  et);.        inR
1d135 65 67 20 3d 20 74 61 72 67 65 74 3b 0a 20 20 20  eg = target;.   
1d136 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1d137 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
1d138 74 6f 5f 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20  to_op, inReg);. 
1d139 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 75       testcase( u
1d13a 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63 68 65  sedAsColumnCache
1d13b 28 70 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20  (pParse, inReg, 
1d13c 69 6e 52 65 67 29 20 29 3b 0a 20 20 20 20 20 20  inReg) );.      
1d13d 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
1d13e 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
1d13f 50 61 72 73 65 2c 20 69 6e 52 65 67 2c 20 31 29  Parse, inReg, 1)
1d140 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d141 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53     }.#endif /* S
1d142 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 41 53 54 20  QLITE_OMIT_CAST 
1d143 2a 2f 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  */.    case TK_L
1d144 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4c  T:.    case TK_L
1d145 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  E:.    case TK_G
1d146 54 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 47  T:.    case TK_G
1d147 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  E:.    case TK_N
1d148 45 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45  E:.    case TK_E
1d149 51 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72  Q: {.      asser
1d14a 74 28 20 54 4b 5f 4c 54 3d 3d 4f 50 5f 4c 74 20  t( TK_LT==OP_Lt 
1d14b 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
1d14c 20 54 4b 5f 4c 45 3d 3d 4f 50 5f 4c 65 20 29 3b   TK_LE==OP_Le );
1d14d 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54  .      assert( T
1d14e 4b 5f 47 54 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20  K_GT==OP_Gt );. 
1d14f 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1d150 47 45 3d 3d 4f 50 5f 47 65 20 29 3b 0a 20 20 20  GE==OP_Ge );.   
1d151 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 45 51     assert( TK_EQ
1d152 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 20 20 20  ==OP_Eq );.     
1d153 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 45 3d 3d   assert( TK_NE==
1d154 4f 50 5f 4e 65 20 29 3b 0a 20 20 20 20 20 20 74  OP_Ne );.      t
1d155 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1d156 4c 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LT );.      test
1d157 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 45 20  case( op==TK_LE 
1d158 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1d159 65 28 20 6f 70 3d 3d 54 4b 5f 47 54 20 29 3b 0a  e( op==TK_GT );.
1d15a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1d15b 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20  op==TK_GE );.   
1d15c 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1d15d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20 20 20 20 20  =TK_EQ );.      
1d15e 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1d15f 5f 4e 45 20 29 3b 0a 20 20 20 20 20 20 63 6f 64  _NE );.      cod
1d160 65 43 6f 6d 70 61 72 65 4f 70 65 72 61 6e 64 73  eCompareOperands
1d161 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e  (pParse, pExpr->
1d162 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72 65 67  pLeft, &r1, &reg
1d163 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20 20 20  Free1,.         
1d164 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d165 20 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e           pExpr->
1d166 70 52 69 67 68 74 2c 20 26 72 32 2c 20 26 72 65  pRight, &r2, &re
1d167 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20 63  gFree2);.      c
1d168 6f 64 65 43 6f 6d 70 61 72 65 28 70 50 61 72 73  odeCompare(pPars
1d169 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
1d16a 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
1d16b 6f 70 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  op,.            
1d16c 20 20 20 20 20 20 72 31 2c 20 72 32 2c 20 69 6e        r1, r2, in
1d16d 52 65 67 2c 20 53 51 4c 49 54 45 5f 53 54 4f 52  Reg, SQLITE_STOR
1d16e 45 50 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74  EP2);.      test
1d16f 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1d170 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1d171 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1d172 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
1d173 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1d174 54 4b 5f 41 4e 44 3a 0a 20 20 20 20 63 61 73 65  TK_AND:.    case
1d175 20 54 4b 5f 4f 52 3a 0a 20 20 20 20 63 61 73 65   TK_OR:.    case
1d176 20 54 4b 5f 50 4c 55 53 3a 0a 20 20 20 20 63 61   TK_PLUS:.    ca
1d177 73 65 20 54 4b 5f 53 54 41 52 3a 0a 20 20 20 20  se TK_STAR:.    
1d178 63 61 73 65 20 54 4b 5f 4d 49 4e 55 53 3a 0a 20  case TK_MINUS:. 
1d179 20 20 20 63 61 73 65 20 54 4b 5f 52 45 4d 3a 0a     case TK_REM:.
1d17a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 49 54 41      case TK_BITA
1d17b 4e 44 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f  ND:.    case TK_
1d17c 42 49 54 4f 52 3a 0a 20 20 20 20 63 61 73 65 20  BITOR:.    case 
1d17d 54 4b 5f 53 4c 41 53 48 3a 0a 20 20 20 20 63 61  TK_SLASH:.    ca
1d17e 73 65 20 54 4b 5f 4c 53 48 49 46 54 3a 0a 20 20  se TK_LSHIFT:.  
1d17f 20 20 63 61 73 65 20 54 4b 5f 52 53 48 49 46 54    case TK_RSHIFT
1d180 3a 20 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 43  : .    case TK_C
1d181 4f 4e 43 41 54 3a 20 7b 0a 20 20 20 20 20 20 61  ONCAT: {.      a
1d182 73 73 65 72 74 28 20 54 4b 5f 41 4e 44 3d 3d 4f  ssert( TK_AND==O
1d183 50 5f 41 6e 64 20 29 3b 0a 20 20 20 20 20 20 61  P_And );.      a
1d184 73 73 65 72 74 28 20 54 4b 5f 4f 52 3d 3d 4f 50  ssert( TK_OR==OP
1d185 5f 4f 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73  _Or );.      ass
1d186 65 72 74 28 20 54 4b 5f 50 4c 55 53 3d 3d 4f 50  ert( TK_PLUS==OP
1d187 5f 41 64 64 20 29 3b 0a 20 20 20 20 20 20 61 73  _Add );.      as
1d188 73 65 72 74 28 20 54 4b 5f 4d 49 4e 55 53 3d 3d  sert( TK_MINUS==
1d189 4f 50 5f 53 75 62 74 72 61 63 74 20 29 3b 0a 20  OP_Subtract );. 
1d18a 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1d18b 52 45 4d 3d 3d 4f 50 5f 52 65 6d 61 69 6e 64 65  REM==OP_Remainde
1d18c 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  r );.      asser
1d18d 74 28 20 54 4b 5f 42 49 54 41 4e 44 3d 3d 4f 50  t( TK_BITAND==OP
1d18e 5f 42 69 74 41 6e 64 20 29 3b 0a 20 20 20 20 20  _BitAnd );.     
1d18f 20 61 73 73 65 72 74 28 20 54 4b 5f 42 49 54 4f   assert( TK_BITO
1d190 52 3d 3d 4f 50 5f 42 69 74 4f 72 20 29 3b 0a 20  R==OP_BitOr );. 
1d191 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1d192 53 4c 41 53 48 3d 3d 4f 50 5f 44 69 76 69 64 65  SLASH==OP_Divide
1d193 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1d194 28 20 54 4b 5f 4c 53 48 49 46 54 3d 3d 4f 50 5f  ( TK_LSHIFT==OP_
1d195 53 68 69 66 74 4c 65 66 74 20 29 3b 0a 20 20 20  ShiftLeft );.   
1d196 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 52 53     assert( TK_RS
1d197 48 49 46 54 3d 3d 4f 50 5f 53 68 69 66 74 52 69  HIFT==OP_ShiftRi
1d198 67 68 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73  ght );.      ass
1d199 65 72 74 28 20 54 4b 5f 43 4f 4e 43 41 54 3d 3d  ert( TK_CONCAT==
1d19a 4f 50 5f 43 6f 6e 63 61 74 20 29 3b 0a 20 20 20  OP_Concat );.   
1d19b 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1d19c 3d 54 4b 5f 41 4e 44 20 29 3b 0a 20 20 20 20 20  =TK_AND );.     
1d19d 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54   testcase( op==T
1d19e 4b 5f 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65  K_OR );.      te
1d19f 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 50  stcase( op==TK_P
1d1a0 4c 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73  LUS );.      tes
1d1a1 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4d 49  tcase( op==TK_MI
1d1a2 4e 55 53 20 29 3b 0a 20 20 20 20 20 20 74 65 73  NUS );.      tes
1d1a3 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 45  tcase( op==TK_RE
1d1a4 4d 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  M );.      testc
1d1a5 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 41  ase( op==TK_BITA
1d1a6 4e 44 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ND );.      test
1d1a7 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54  case( op==TK_BIT
1d1a8 4f 52 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  OR );.      test
1d1a9 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 53 4c 41  case( op==TK_SLA
1d1aa 53 48 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  SH );.      test
1d1ab 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4c 53 48  case( op==TK_LSH
1d1ac 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73  IFT );.      tes
1d1ad 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 52 53  tcase( op==TK_RS
1d1ae 48 49 46 54 20 29 3b 0a 20 20 20 20 20 20 74 65  HIFT );.      te
1d1af 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 43  stcase( op==TK_C
1d1b0 4f 4e 43 41 54 20 29 3b 0a 20 20 20 20 20 20 72  ONCAT );.      r
1d1b1 31 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  1 = sqlite3ExprC
1d1b2 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20  odeTemp(pParse, 
1d1b3 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72  pExpr->pLeft, &r
1d1b4 65 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20  egFree1);.      
1d1b5 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
1d1b6 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
1d1b7 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20   pExpr->pRight, 
1d1b8 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20  &regFree2);.    
1d1b9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d1ba 4f 70 33 28 76 2c 20 6f 70 2c 20 72 32 2c 20 72  Op3(v, op, r2, r
1d1bb 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  1, target);.    
1d1bc 20 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46    testcase( regF
1d1bd 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20  ree1==0 );.     
1d1be 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1d1bf 65 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee2==0 );.      
1d1c0 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20  break;.    }.   
1d1c1 20 63 61 73 65 20 54 4b 5f 55 4d 49 4e 55 53 3a   case TK_UMINUS:
1d1c2 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70   {.      Expr *p
1d1c3 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
1d1c4 65 66 74 3b 0a 20 20 20 20 20 20 61 73 73 65 72  eft;.      asser
1d1c5 74 28 20 70 4c 65 66 74 20 29 3b 0a 20 20 20 20  t( pLeft );.    
1d1c6 20 20 69 66 28 20 70 4c 65 66 74 2d 3e 6f 70 3d    if( pLeft->op=
1d1c7 3d 54 4b 5f 46 4c 4f 41 54 20 7c 7c 20 70 4c 65  =TK_FLOAT || pLe
1d1c8 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47  ft->op==TK_INTEG
1d1c9 45 52 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66  ER ){.        if
1d1ca 28 20 70 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f  ( pLeft->op==TK_
1d1cb 46 4c 4f 41 54 20 29 7b 0a 20 20 20 20 20 20 20  FLOAT ){.       
1d1cc 20 20 20 63 6f 64 65 52 65 61 6c 28 76 2c 20 28     codeReal(v, (
1d1cd 63 68 61 72 2a 29 70 4c 65 66 74 2d 3e 74 6f 6b  char*)pLeft->tok
1d1ce 65 6e 2e 7a 2c 20 70 4c 65 66 74 2d 3e 74 6f 6b  en.z, pLeft->tok
1d1cf 65 6e 2e 6e 2c 20 31 2c 20 74 61 72 67 65 74 29  en.n, 1, target)
1d1d0 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
1d1d1 0a 20 20 20 20 20 20 20 20 20 20 63 6f 64 65 49  .          codeI
1d1d2 6e 74 65 67 65 72 28 76 2c 20 70 4c 65 66 74 2c  nteger(v, pLeft,
1d1d3 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   1, target);.   
1d1d4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
1d1d5 73 65 7b 0a 20 20 20 20 20 20 20 20 72 65 67 46  se{.        regF
1d1d6 72 65 65 31 20 3d 20 72 31 20 3d 20 73 71 6c 69  ree1 = r1 = sqli
1d1d7 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50  te3GetTempReg(pP
1d1d8 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73  arse);.        s
1d1d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d1da 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1d1db 30 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20  0, r1);.        
1d1dc 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
1d1dd 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
1d1de 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26   pExpr->pLeft, &
1d1df 72 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20  regFree2);.     
1d1e0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1d1e1 64 4f 70 33 28 76 2c 20 4f 50 5f 53 75 62 74 72  dOp3(v, OP_Subtr
1d1e2 61 63 74 2c 20 72 32 2c 20 72 31 2c 20 74 61 72  act, r2, r1, tar
1d1e3 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 74 65  get);.        te
1d1e4 73 74 63 61 73 65 28 20 72 65 67 46 72 65 65 32  stcase( regFree2
1d1e5 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ==0 );.      }. 
1d1e6 20 20 20 20 20 69 6e 52 65 67 20 3d 20 74 61 72       inReg = tar
1d1e7 67 65 74 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  get;.      break
1d1e8 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1d1e9 20 54 4b 5f 42 49 54 4e 4f 54 3a 0a 20 20 20 20   TK_BITNOT:.    
1d1ea 63 61 73 65 20 54 4b 5f 4e 4f 54 3a 20 7b 0a 20  case TK_NOT: {. 
1d1eb 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f       assert( TK_
1d1ec 42 49 54 4e 4f 54 3d 3d 4f 50 5f 42 69 74 4e 6f  BITNOT==OP_BitNo
1d1ed 74 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t );.      asser
1d1ee 74 28 20 54 4b 5f 4e 4f 54 3d 3d 4f 50 5f 4e 6f  t( TK_NOT==OP_No
1d1ef 74 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  t );.      testc
1d1f0 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 42 49 54 4e  ase( op==TK_BITN
1d1f1 4f 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  OT );.      test
1d1f2 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54  case( op==TK_NOT
1d1f3 20 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20   );.      inReg 
1d1f4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1d1f5 65 54 61 72 67 65 74 28 70 50 61 72 73 65 2c 20  eTarget(pParse, 
1d1f6 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 74 61  pExpr->pLeft, ta
1d1f7 72 67 65 74 29 3b 0a 20 20 20 20 20 20 74 65 73  rget);.      tes
1d1f8 74 63 61 73 65 28 20 69 6e 52 65 67 3d 3d 74 61  tcase( inReg==ta
1d1f9 72 67 65 74 20 29 3b 0a 20 20 20 20 20 20 74 65  rget );.      te
1d1fa 73 74 63 61 73 65 28 20 75 73 65 64 41 73 43 6f  stcase( usedAsCo
1d1fb 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65  lumnCache(pParse
1d1fc 2c 20 69 6e 52 65 67 2c 20 69 6e 52 65 67 29 20  , inReg, inReg) 
1d1fd 29 3b 0a 20 20 20 20 20 20 69 6e 52 65 67 20 3d  );.      inReg =
1d1fe 20 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74   sqlite3ExprWrit
1d1ff 61 62 6c 65 52 65 67 69 73 74 65 72 28 70 50 61  ableRegister(pPa
1d200 72 73 65 2c 20 69 6e 52 65 67 2c 20 74 61 72 67  rse, inReg, targ
1d201 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  et);.      sqlit
1d202 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
1d203 6f 70 2c 20 69 6e 52 65 67 29 3b 0a 20 20 20 20  op, inReg);.    
1d204 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1d205 20 20 20 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c     case TK_ISNUL
1d206 4c 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e  L:.    case TK_N
1d207 4f 54 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  OTNULL: {.      
1d208 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 20 20  int addr;.      
1d209 61 73 73 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c  assert( TK_ISNUL
1d20a 4c 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  L==OP_IsNull );.
1d20b 20 20 20 20 20 20 61 73 73 65 72 74 28 20 54 4b        assert( TK
1d20c 5f 4e 4f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74  _NOTNULL==OP_Not
1d20d 4e 75 6c 6c 20 29 3b 0a 20 20 20 20 20 20 74 65  Null );.      te
1d20e 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49  stcase( op==TK_I
1d20f 53 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74  SNULL );.      t
1d210 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1d211 4e 4f 54 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20  NOTNULL );.     
1d212 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d213 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1d214 2c 20 31 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 1, target);.  
1d215 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33      r1 = sqlite3
1d216 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61  ExprCodeTemp(pPa
1d217 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66  rse, pExpr->pLef
1d218 74 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  t, &regFree1);. 
1d219 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1d21a 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
1d21b 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
1d21c 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
1d21d 6f 70 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73  op, r1);.      s
1d21e 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d21f 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 74  (v, OP_AddImm, t
1d220 61 72 67 65 74 2c 20 2d 31 29 3b 0a 20 20 20 20  arget, -1);.    
1d221 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
1d222 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
1d223 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d224 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41   }.    case TK_A
1d225 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20  GG_FUNCTION: {. 
1d226 20 20 20 20 20 41 67 67 49 6e 66 6f 20 2a 70 49       AggInfo *pI
1d227 6e 66 6f 20 3d 20 70 45 78 70 72 2d 3e 70 41 67  nfo = pExpr->pAg
1d228 67 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28  gInfo;.      if(
1d229 20 70 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20 20 20   pInfo==0 ){.   
1d22a 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1d22b 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6d 69  rMsg(pParse, "mi
1d22c 73 75 73 65 20 6f 66 20 61 67 67 72 65 67 61 74  suse of aggregat
1d22d 65 3a 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20  e: %T",.        
1d22e 20 20 20 20 26 70 45 78 70 72 2d 3e 73 70 61 6e      &pExpr->span
1d22f 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1d230 20 20 20 20 20 20 20 20 69 6e 52 65 67 20 3d 20          inReg = 
1d231 70 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 70 45 78  pInfo->aFunc[pEx
1d232 70 72 2d 3e 69 41 67 67 5d 2e 69 4d 65 6d 3b 0a  pr->iAgg].iMem;.
1d233 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1d234 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1d235 61 73 65 20 54 4b 5f 43 4f 4e 53 54 5f 46 55 4e  ase TK_CONST_FUN
1d236 43 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 46  C:.    case TK_F
1d237 55 4e 43 54 49 4f 4e 3a 20 7b 0a 20 20 20 20 20  UNCTION: {.     
1d238 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1d239 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
1d23a 0a 20 20 20 20 20 20 69 6e 74 20 6e 45 78 70 72  .      int nExpr
1d23b 20 3d 20 70 4c 69 73 74 20 3f 20 70 4c 69 73 74   = pList ? pList
1d23c 2d 3e 6e 45 78 70 72 20 3a 20 30 3b 0a 20 20 20  ->nExpr : 0;.   
1d23d 20 20 20 46 75 6e 63 44 65 66 20 2a 70 44 65 66     FuncDef *pDef
1d23e 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 49 64 3b  ;.      int nId;
1d23f 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
1d240 72 20 2a 7a 49 64 3b 0a 20 20 20 20 20 20 69 6e  r *zId;.      in
1d241 74 20 63 6f 6e 73 74 4d 61 73 6b 20 3d 20 30 3b  t constMask = 0;
1d242 0a 20 20 20 20 20 20 69 6e 74 20 69 3b 0a 20 20  .      int i;.  
1d243 20 20 20 20 75 38 20 65 6e 63 20 3d 20 45 4e 43      u8 enc = ENC
1d244 28 64 62 29 3b 0a 20 20 20 20 20 20 43 6f 6c 6c  (db);.      Coll
1d245 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20 30 3b 0a  Seq *pColl = 0;.
1d246 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1d247 20 6f 70 3d 3d 54 4b 5f 43 4f 4e 53 54 5f 46 55   op==TK_CONST_FU
1d248 4e 43 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  NC );.      test
1d249 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 46 55 4e  case( op==TK_FUN
1d24a 43 54 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 7a  CTION );.      z
1d24b 49 64 20 3d 20 28 63 68 61 72 2a 29 70 45 78 70  Id = (char*)pExp
1d24c 72 2d 3e 74 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20  r->token.z;.    
1d24d 20 20 6e 49 64 20 3d 20 70 45 78 70 72 2d 3e 74    nId = pExpr->t
1d24e 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 20 20 70 44  oken.n;.      pD
1d24f 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ef = sqlite3Find
1d250 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a 49 64  Function(db, zId
1d251 2c 20 6e 49 64 2c 20 6e 45 78 70 72 2c 20 65 6e  , nId, nExpr, en
1d252 63 2c 20 30 29 3b 0a 20 20 20 20 20 20 61 73 73  c, 0);.      ass
1d253 65 72 74 28 20 70 44 65 66 21 3d 30 20 29 3b 0a  ert( pDef!=0 );.
1d254 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74 20        if( pList 
1d255 29 7b 0a 20 20 20 20 20 20 20 20 6e 45 78 70 72  ){.        nExpr
1d256 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   = pList->nExpr;
1d257 0a 20 20 20 20 20 20 20 20 72 31 20 3d 20 73 71  .        r1 = sq
1d258 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e 67  lite3GetTempRang
1d259 65 28 70 50 61 72 73 65 2c 20 6e 45 78 70 72 29  e(pParse, nExpr)
1d25a 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1d25b 33 45 78 70 72 43 6f 64 65 45 78 70 72 4c 69 73  3ExprCodeExprLis
1d25c 74 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  t(pParse, pList,
1d25d 20 72 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d   r1, 1);.      }
1d25e 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 6e 45  else{.        nE
1d25f 78 70 72 20 3d 20 72 31 20 3d 20 30 3b 0a 20 20  xpr = r1 = 0;.  
1d260 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1d261 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
1d262 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 2f 2a 20  LTABLE.      /* 
1d263 50 6f 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61  Possibly overloa
1d264 64 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  d the function i
1d265 66 20 74 68 65 20 66 69 72 73 74 20 61 72 67 75  f the first argu
1d266 6d 65 6e 74 20 69 73 0a 20 20 20 20 20 20 2a 2a  ment is.      **
1d267 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65   a virtual table
1d268 20 63 6f 6c 75 6d 6e 2e 0a 20 20 20 20 20 20 2a   column..      *
1d269 2a 0a 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 69  *.      ** For i
1d26a 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73 20 28  nfix functions (
1d26b 4c 49 4b 45 2c 20 47 4c 4f 42 2c 20 52 45 47 45  LIKE, GLOB, REGE
1d26c 58 50 2c 20 61 6e 64 20 4d 41 54 43 48 29 20 75  XP, and MATCH) u
1d26d 73 65 20 74 68 65 0a 20 20 20 20 20 20 2a 2a 20  se the.      ** 
1d26e 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 2c  second argument,
1d26f 20 6e 6f 74 20 74 68 65 20 66 69 72 73 74 2c 20   not the first, 
1d270 61 73 20 74 68 65 20 61 72 67 75 6d 65 6e 74 20  as the argument 
1d271 74 6f 20 74 65 73 74 20 74 6f 0a 20 20 20 20 20  to test to.     
1d272 20 2a 2a 20 73 65 65 20 69 66 20 69 74 20 69 73   ** see if it is
1d273 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 76   a column in a v
1d274 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54  irtual table.  T
1d275 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 63 61  his is done beca
1d276 75 73 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65  use.      ** the
1d277 20 6c 65 66 74 20 6f 70 65 72 61 6e 64 20 6f 66   left operand of
1d278 20 69 6e 66 69 78 20 66 75 6e 63 74 69 6f 6e 73   infix functions
1d279 20 28 74 68 65 20 6f 70 65 72 61 6e 64 20 77 65   (the operand we
1d27a 20 77 61 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a   want to.      *
1d27b 2a 20 63 6f 6e 74 72 6f 6c 20 6f 76 65 72 6c 6f  * control overlo
1d27c 61 64 69 6e 67 29 20 65 6e 64 73 20 75 70 20 61  ading) ends up a
1d27d 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
1d27e 75 6d 65 6e 74 20 74 6f 20 74 68 65 0a 20 20 20  ument to the.   
1d27f 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e 2e 20     ** function. 
1d280 20 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   The expression 
1d281 22 41 20 67 6c 6f 62 20 42 22 20 69 73 20 65 71  "A glob B" is eq
1d282 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20  uivalent to .   
1d283 20 20 20 2a 2a 20 22 67 6c 6f 62 28 42 2c 41 29     ** "glob(B,A)
1d284 2e 20 20 57 65 20 77 61 6e 74 20 74 6f 20 75 73  .  We want to us
1d285 65 20 74 68 65 20 41 20 69 6e 20 22 41 20 67 6c  e the A in "A gl
1d286 6f 62 20 42 22 20 74 6f 20 74 65 73 74 0a 20 20  ob B" to test.  
1d287 20 20 20 20 2a 2a 20 66 6f 72 20 66 75 6e 63 74      ** for funct
1d288 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e 67 2e  ion overloading.
1d289 20 20 42 75 74 20 77 65 20 75 73 65 20 74 68 65    But we use the
1d28a 20 42 20 74 65 72 6d 20 69 6e 20 22 67 6c 6f 62   B term in "glob
1d28b 28 42 2c 41 29 22 2e 0a 20 20 20 20 20 20 2a 2f  (B,A)"..      */
1d28c 0a 20 20 20 20 20 20 69 66 28 20 6e 45 78 70 72  .      if( nExpr
1d28d 3e 3d 32 20 26 26 20 28 70 45 78 70 72 2d 3e 66  >=2 && (pExpr->f
1d28e 6c 61 67 73 20 26 20 45 50 5f 49 6e 66 69 78 46  lags & EP_InfixF
1d28f 75 6e 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20  unc) ){.        
1d290 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 56 74  pDef = sqlite3Vt
1d291 61 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69  abOverloadFuncti
1d292 6f 6e 28 64 62 2c 20 70 44 65 66 2c 20 6e 45 78  on(db, pDef, nEx
1d293 70 72 2c 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  pr, pList->a[1].
1d294 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 7d 65  pExpr);.      }e
1d295 6c 73 65 20 69 66 28 20 6e 45 78 70 72 3e 30 20  lse if( nExpr>0 
1d296 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65 66 20  ){.        pDef 
1d297 3d 20 73 71 6c 69 74 65 33 56 74 61 62 4f 76 65  = sqlite3VtabOve
1d298 72 6c 6f 61 64 46 75 6e 63 74 69 6f 6e 28 64 62  rloadFunction(db
1d299 2c 20 70 44 65 66 2c 20 6e 45 78 70 72 2c 20 70  , pDef, nExpr, p
1d29a 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72  List->a[0].pExpr
1d29b 29 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  );.      }.#endi
1d29c 66 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  f.      for(i=0;
1d29d 20 69 3c 6e 45 78 70 72 20 26 26 20 69 3c 33 32   i<nExpr && i<32
1d29e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
1d29f 69 66 28 20 73 71 6c 69 74 65 33 45 78 70 72 49  if( sqlite3ExprI
1d2a0 73 43 6f 6e 73 74 61 6e 74 28 70 4c 69 73 74 2d  sConstant(pList-
1d2a1 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b 0a  >a[i].pExpr) ){.
1d2a2 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 4d            constM
1d2a3 61 73 6b 20 7c 3d 20 28 31 3c 3c 69 29 3b 0a 20  ask |= (1<<i);. 
1d2a4 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1d2a5 20 69 66 28 20 28 70 44 65 66 2d 3e 66 6c 61 67   if( (pDef->flag
1d2a6 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f  s & SQLITE_FUNC_
1d2a7 4e 45 45 44 43 4f 4c 4c 29 21 3d 30 20 26 26 20  NEEDCOLL)!=0 && 
1d2a8 21 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20  !pColl ){.      
1d2a9 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
1d2aa 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
1d2ab 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b  Parse, pList->a[
1d2ac 69 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 20  i].pExpr);.     
1d2ad 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
1d2ae 20 20 20 69 66 28 20 70 44 65 66 2d 3e 66 6c 61     if( pDef->fla
1d2af 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43  gs & SQLITE_FUNC
1d2b0 5f 4e 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20  _NEEDCOLL ){.   
1d2b1 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c 20       if( !pColl 
1d2b2 29 20 70 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44  ) pColl = db->pD
1d2b3 66 6c 74 43 6f 6c 6c 3b 20 0a 20 20 20 20 20 20  fltColl; .      
1d2b4 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d2b5 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c 53 65  Op4(v, OP_CollSe
1d2b6 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63 68 61  q, 0, 0, 0, (cha
1d2b7 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f 43 4f  r *)pColl, P4_CO
1d2b8 4c 4c 53 45 51 29 3b 0a 20 20 20 20 20 20 7d 0a  LLSEQ);.      }.
1d2b9 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d2ba 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 46 75  eAddOp4(v, OP_Fu
1d2bb 6e 63 74 69 6f 6e 2c 20 63 6f 6e 73 74 4d 61 73  nction, constMas
1d2bc 6b 2c 20 72 31 2c 20 74 61 72 67 65 74 2c 0a 20  k, r1, target,. 
1d2bd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d2be 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 44         (char*)pD
1d2bf 65 66 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b  ef, P4_FUNCDEF);
1d2c0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d2c1 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 6e 45  beChangeP5(v, nE
1d2c2 78 70 72 29 3b 0a 20 20 20 20 20 20 69 66 28 20  xpr);.      if( 
1d2c3 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20 20 20  nExpr ){.       
1d2c4 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1d2c5 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
1d2c6 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20 20 20   r1, nExpr);.   
1d2c7 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
1d2c8 65 33 45 78 70 72 43 61 63 68 65 41 66 66 69 6e  e3ExprCacheAffin
1d2c9 69 74 79 43 68 61 6e 67 65 28 70 50 61 72 73 65  ityChange(pParse
1d2ca 2c 20 72 31 2c 20 6e 45 78 70 72 29 3b 0a 20 20  , r1, nExpr);.  
1d2cb 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d2cc 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1d2cd 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
1d2ce 20 20 63 61 73 65 20 54 4b 5f 45 58 49 53 54 53    case TK_EXISTS
1d2cf 3a 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 53 45  :.    case TK_SE
1d2d0 4c 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 74 65  LECT: {.      te
1d2d1 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45  stcase( op==TK_E
1d2d2 58 49 53 54 53 20 29 3b 0a 20 20 20 20 20 20 74  XISTS );.      t
1d2d3 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1d2d4 53 45 4c 45 43 54 20 29 3b 0a 20 20 20 20 20 20  SELECT );.      
1d2d5 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  if( pExpr->iColu
1d2d6 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  mn==0 ){.       
1d2d7 20 73 71 6c 69 74 65 33 43 6f 64 65 53 75 62 73   sqlite3CodeSubs
1d2d8 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 45  elect(pParse, pE
1d2d9 78 70 72 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  xpr, 0, 0);.    
1d2da 20 20 7d 0a 20 20 20 20 20 20 69 6e 52 65 67 20    }.      inReg 
1d2db 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  = pExpr->iColumn
1d2dc 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d2dd 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1d2de 5f 49 4e 3a 20 7b 0a 20 20 20 20 20 20 69 6e 74  _IN: {.      int
1d2df 20 72 4e 6f 74 46 6f 75 6e 64 20 3d 20 30 3b 0a   rNotFound = 0;.
1d2e0 20 20 20 20 20 20 69 6e 74 20 72 4d 61 79 48 61        int rMayHa
1d2e1 76 65 4e 75 6c 6c 20 3d 20 30 3b 0a 20 20 20 20  veNull = 0;.    
1d2e2 20 20 69 6e 74 20 6a 32 2c 20 6a 33 2c 20 6a 34    int j2, j3, j4
1d2e3 2c 20 6a 35 3b 0a 20 20 20 20 20 20 63 68 61 72  , j5;.      char
1d2e4 20 61 66 66 69 6e 69 74 79 3b 0a 20 20 20 20 20   affinity;.     
1d2e5 20 69 6e 74 20 65 54 79 70 65 3b 0a 0a 20 20 20   int eType;..   
1d2e6 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65     VdbeNoopComme
1d2e7 6e 74 28 28 76 2c 20 22 62 65 67 69 6e 20 49 4e  nt((v, "begin IN
1d2e8 20 65 78 70 72 20 72 25 64 22 2c 20 74 61 72 67   expr r%d", targ
1d2e9 65 74 29 29 3b 0a 20 20 20 20 20 20 65 54 79 70  et));.      eTyp
1d2ea 65 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49  e = sqlite3FindI
1d2eb 6e 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70  nIndex(pParse, p
1d2ec 45 78 70 72 2c 20 26 72 4d 61 79 48 61 76 65 4e  Expr, &rMayHaveN
1d2ed 75 6c 6c 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ull);.      if( 
1d2ee 72 4d 61 79 48 61 76 65 4e 75 6c 6c 20 29 7b 0a  rMayHaveNull ){.
1d2ef 20 20 20 20 20 20 20 20 72 4e 6f 74 46 6f 75 6e          rNotFoun
1d2f0 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d  d = ++pParse->nM
1d2f1 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  em;.      }..   
1d2f2 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74     /* Figure out
1d2f3 20 74 68 65 20 61 66 66 69 6e 69 74 79 20 74 6f   the affinity to
1d2f4 20 75 73 65 20 74 6f 20 63 72 65 61 74 65 20 61   use to create a
1d2f5 20 6b 65 79 20 66 72 6f 6d 20 74 68 65 20 72 65   key from the re
1d2f6 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a 20 6f  sults.      ** o
1d2f7 66 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e  f the expression
1d2f8 2e 20 61 66 66 69 6e 69 74 79 53 74 72 20 73 74  . affinityStr st
1d2f9 6f 72 65 73 20 61 20 73 74 61 74 69 63 20 73 74  ores a static st
1d2fa 72 69 6e 67 20 73 75 69 74 61 62 6c 65 20 66 6f  ring suitable fo
1d2fb 72 0a 20 20 20 20 20 20 2a 2a 20 50 34 20 6f 66  r.      ** P4 of
1d2fc 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2e 0a   OP_MakeRecord..
1d2fd 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61        */.      a
1d2fe 66 66 69 6e 69 74 79 20 3d 20 63 6f 6d 70 61 72  ffinity = compar
1d2ff 69 73 6f 6e 41 66 66 69 6e 69 74 79 28 70 45 78  isonAffinity(pEx
1d300 70 72 29 3b 0a 0a 0a 20 20 20 20 20 20 2f 2a 20  pr);...      /* 
1d301 43 6f 64 65 20 74 68 65 20 3c 65 78 70 72 3e 20  Code the <expr> 
1d302 66 72 6f 6d 20 22 3c 65 78 70 72 3e 20 49 4e 20  from "<expr> IN 
1d303 28 2e 2e 2e 29 22 2e 20 54 68 65 20 74 65 6d 70  (...)". The temp
1d304 6f 72 61 72 79 20 74 61 62 6c 65 0a 20 20 20 20  orary table.    
1d305 20 20 2a 2a 20 70 45 78 70 72 2d 3e 69 54 61 62    ** pExpr->iTab
1d306 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20  le contains the 
1d307 76 61 6c 75 65 73 20 74 68 61 74 20 6d 61 6b 65  values that make
1d308 20 75 70 20 74 68 65 20 28 2e 2e 2e 29 20 73 65   up the (...) se
1d309 74 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  t..      */.    
1d30a 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c    pParse->disabl
1d30b 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20  eColCache++;.   
1d30c 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1d30d 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
1d30e 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29  ->pLeft, target)
1d30f 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
1d310 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d  disableColCache-
1d311 2d 3b 0a 20 20 20 20 20 20 6a 32 20 3d 20 73 71  -;.      j2 = sq
1d312 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
1d313 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 74 61  v, OP_IsNull, ta
1d314 72 67 65 74 29 3b 0a 20 20 20 20 20 20 69 66 28  rget);.      if(
1d315 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45 58   eType==IN_INDEX
1d316 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20 20  _ROWID ){.      
1d317 20 20 6a 33 20 3d 20 73 71 6c 69 74 65 33 56 64    j3 = sqlite3Vd
1d318 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4d  beAddOp1(v, OP_M
1d319 75 73 74 42 65 49 6e 74 2c 20 74 61 72 67 65 74  ustBeInt, target
1d31a 29 3b 0a 20 20 20 20 20 20 20 20 6a 34 20 3d 20  );.        j4 = 
1d31b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d31c 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74  3(v, OP_NotExist
1d31d 73 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  s, pExpr->iTable
1d31e 2c 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20  , 0, target);.  
1d31f 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d320 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
1d321 74 65 67 65 72 2c 20 31 2c 20 74 61 72 67 65 74  teger, 1, target
1d322 29 3b 0a 20 20 20 20 20 20 20 20 6a 35 20 3d 20  );.        j5 = 
1d323 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d324 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20  0(v, OP_Goto);. 
1d325 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
1d326 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 33  beJumpHere(v, j3
1d327 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1d328 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1d329 2c 20 6a 34 29 3b 0a 20 20 20 20 20 20 20 20 73  , j4);.        s
1d32a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d32b 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1d32c 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20 20  0, target);.    
1d32d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
1d32e 20 72 32 20 3d 20 72 65 67 46 72 65 65 32 20 3d   r2 = regFree2 =
1d32f 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
1d330 65 67 28 70 50 61 72 73 65 29 3b 0a 0a 20 20 20  eg(pParse);..   
1d331 20 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61       /* Create a
1d332 20 72 65 63 6f 72 64 20 61 6e 64 20 74 65 73 74   record and test
1d333 20 66 6f 72 20 73 65 74 20 6d 65 6d 62 65 72 73   for set members
1d334 68 69 70 2e 20 49 66 20 74 68 65 20 73 65 74 20  hip. If the set 
1d335 63 6f 6e 74 61 69 6e 73 0a 20 20 20 20 20 20 20  contains.       
1d336 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 2c 20 74   ** the value, t
1d337 68 65 6e 20 6a 75 6d 70 20 74 6f 20 74 68 65 20  hen jump to the 
1d338 65 6e 64 20 6f 66 20 74 68 65 20 74 65 73 74 20  end of the test 
1d339 63 6f 64 65 2e 20 54 68 65 20 74 61 72 67 65 74  code. The target
1d33a 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 65 67 69  .        ** regi
1d33b 73 74 65 72 20 73 74 69 6c 6c 20 63 6f 6e 74 61  ster still conta
1d33c 69 6e 73 20 74 68 65 20 74 72 75 65 20 28 31 29  ins the true (1)
1d33d 20 76 61 6c 75 65 20 77 72 69 74 74 65 6e 20 74   value written t
1d33e 6f 20 69 74 20 65 61 72 6c 69 65 72 2e 0a 20 20  o it earlier..  
1d33f 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1d340 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1d341 70 34 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p4(v, OP_MakeRec
1d342 6f 72 64 2c 20 74 61 72 67 65 74 2c 20 31 2c 20  ord, target, 1, 
1d343 72 32 2c 20 26 61 66 66 69 6e 69 74 79 2c 20 31  r2, &affinity, 1
1d344 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
1d345 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1d346 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 74  OP_Integer, 1, t
1d347 61 72 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20  arget);.        
1d348 6a 35 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j5 = sqlite3Vdbe
1d349 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75  AddOp3(v, OP_Fou
1d34a 6e 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  nd, pExpr->iTabl
1d34b 65 2c 20 30 2c 20 72 32 29 3b 0a 0a 20 20 20 20  e, 0, r2);..    
1d34c 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 73 65      /* If the se
1d34d 74 20 6d 65 6d 62 65 72 73 68 69 70 20 74 65 73  t membership tes
1d34e 74 20 66 61 69 6c 73 2c 20 74 68 65 6e 20 74 68  t fails, then th
1d34f 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 20  e result of the 
1d350 0a 20 20 20 20 20 20 20 20 2a 2a 20 22 78 20 49  .        ** "x I
1d351 4e 20 28 2e 2e 2e 29 22 20 65 78 70 72 65 73 73  N (...)" express
1d352 69 6f 6e 20 6d 75 73 74 20 62 65 20 65 69 74 68  ion must be eith
1d353 65 72 20 30 20 6f 72 20 4e 55 4c 4c 2e 20 49 66  er 0 or NULL. If
1d354 20 74 68 65 20 73 65 74 0a 20 20 20 20 20 20 20   the set.       
1d355 20 2a 2a 20 63 6f 6e 74 61 69 6e 73 20 6e 6f 20   ** contains no 
1d356 4e 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65  NULL values, the
1d357 6e 20 74 68 65 20 72 65 73 75 6c 74 20 69 73 20  n the result is 
1d358 30 2e 20 49 66 20 74 68 65 20 73 65 74 20 0a 20  0. If the set . 
1d359 20 20 20 20 20 20 20 2a 2a 20 63 6f 6e 74 61 69         ** contai
1d35a 6e 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 4e  ns one or more N
1d35b 55 4c 4c 20 76 61 6c 75 65 73 2c 20 74 68 65 6e  ULL values, then
1d35c 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74   the result of t
1d35d 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 78  he.        ** ex
1d35e 70 72 65 73 73 69 6f 6e 20 69 73 20 61 6c 73 6f  pression is also
1d35f 20 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 20 20 2a   NULL..        *
1d360 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 4e  /.        if( rN
1d361 6f 74 46 6f 75 6e 64 3d 3d 30 20 29 7b 0a 20 20  otFound==0 ){.  
1d362 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20          /* This 
1d363 62 72 61 6e 63 68 20 72 75 6e 73 20 69 66 20 69  branch runs if i
1d364 74 20 69 73 20 6b 6e 6f 77 6e 20 61 74 20 63 6f  t is known at co
1d365 6d 70 69 6c 65 20 74 69 6d 65 20 28 6e 6f 77 29  mpile time (now)
1d366 20 74 68 61 74 20 0a 20 20 20 20 20 20 20 20 20   that .         
1d367 20 2a 2a 20 74 68 65 20 73 65 74 20 63 6f 6e 74   ** the set cont
1d368 61 69 6e 73 20 6e 6f 20 4e 55 4c 4c 20 76 61 6c  ains no NULL val
1d369 75 65 73 2e 20 54 68 69 73 20 68 61 70 70 65 6e  ues. This happen
1d36a 73 20 61 73 20 74 68 65 20 72 65 73 75 6c 74 0a  s as the result.
1d36b 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20            ** of 
1d36c 61 20 22 4e 4f 54 20 4e 55 4c 4c 22 20 63 6f 6e  a "NOT NULL" con
1d36d 73 74 72 61 69 6e 74 20 69 6e 20 74 68 65 20 64  straint in the d
1d36e 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20  atabase schema. 
1d36f 4e 6f 20 6e 65 65 64 0a 20 20 20 20 20 20 20 20  No need.        
1d370 20 20 2a 2a 20 74 6f 20 74 65 73 74 20 74 68 65    ** to test the
1d371 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20   data structure 
1d372 61 74 20 72 75 6e 74 69 6d 65 20 69 6e 20 74 68  at runtime in th
1d373 69 73 20 63 61 73 65 2e 0a 20 20 20 20 20 20 20  is case..       
1d374 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20     */.          
1d375 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1d376 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
1d377 20 30 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 20   0, target);.   
1d378 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
1d379 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c        /* This bl
1d37a 6f 63 6b 20 70 6f 70 75 6c 61 74 65 73 20 74 68  ock populates th
1d37b 65 20 72 4e 6f 74 46 6f 75 6e 64 20 72 65 67 69  e rNotFound regi
1d37c 73 74 65 72 20 77 69 74 68 20 65 69 74 68 65 72  ster with either
1d37d 20 4e 55 4c 4c 0a 20 20 20 20 20 20 20 20 20 20   NULL.          
1d37e 2a 2a 20 6f 72 20 30 20 28 61 6e 20 69 6e 74 65  ** or 0 (an inte
1d37f 67 65 72 20 76 61 6c 75 65 29 2e 20 49 66 20 74  ger value). If t
1d380 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  he data structur
1d381 65 20 63 6f 6e 74 61 69 6e 73 20 6f 6e 65 0a 20  e contains one. 
1d382 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 6d           ** or m
1d383 6f 72 65 20 4e 55 4c 4c 73 2c 20 74 68 65 6e 20  ore NULLs, then 
1d384 73 65 74 20 72 4e 6f 74 46 6f 75 6e 64 20 74 6f  set rNotFound to
1d385 20 4e 55 4c 4c 2e 20 4f 74 68 65 72 77 69 73 65   NULL. Otherwise
1d386 2c 20 73 65 74 20 69 74 0a 20 20 20 20 20 20 20  , set it.       
1d387 20 20 20 2a 2a 20 74 6f 20 30 2e 20 49 66 20 72     ** to 0. If r
1d388 65 67 69 73 74 65 72 20 72 4d 61 79 48 61 76 65  egister rMayHave
1d389 4e 75 6c 6c 20 69 73 20 61 6c 72 65 61 64 79 20  Null is already 
1d38a 73 65 74 20 74 6f 20 73 6f 6d 65 20 76 61 6c 75  set to some valu
1d38b 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f  e.          ** o
1d38c 74 68 65 72 20 74 68 61 6e 20 4e 55 4c 4c 2c 20  ther than NULL, 
1d38d 74 68 65 6e 20 74 68 65 20 74 65 73 74 20 68 61  then the test ha
1d38e 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72  s already been r
1d38f 75 6e 20 61 6e 64 20 0a 20 20 20 20 20 20 20 20  un and .        
1d390 20 20 2a 2a 20 72 4e 6f 74 46 6f 75 6e 64 20 69    ** rNotFound i
1d391 73 20 61 6c 72 65 61 64 79 20 70 6f 70 75 6c 61  s already popula
1d392 74 65 64 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  ted..          *
1d393 2f 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74  /.          stat
1d394 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6e 75  ic const char nu
1d395 6c 6c 52 65 63 6f 72 64 5b 5d 20 3d 20 7b 20 30  llRecord[] = { 0
1d396 78 30 32 2c 20 30 78 30 30 20 7d 3b 0a 20 20 20  x02, 0x00 };.   
1d397 20 20 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69         j3 = sqli
1d398 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
1d399 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 4d 61   OP_NotNull, rMa
1d39a 79 48 61 76 65 4e 75 6c 6c 29 3b 0a 20 20 20 20  yHaveNull);.    
1d39b 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1d39c 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75  eAddOp2(v, OP_Nu
1d39d 6c 6c 2c 20 30 2c 20 72 4e 6f 74 46 6f 75 6e 64  ll, 0, rNotFound
1d39e 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  );.          sql
1d39f 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
1d3a0 2c 20 4f 50 5f 42 6c 6f 62 2c 20 32 2c 20 72 4d  , OP_Blob, 2, rM
1d3a1 61 79 48 61 76 65 4e 75 6c 6c 2c 20 30 2c 20 0a  ayHaveNull, 0, .
1d3a2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3a3 20 20 20 20 20 20 20 20 20 20 20 20 20 6e 75 6c               nul
1d3a4 6c 52 65 63 6f 72 64 2c 20 50 34 5f 53 54 41 54  lRecord, P4_STAT
1d3a5 49 43 29 3b 0a 20 20 20 20 20 20 20 20 20 20 6a  IC);.          j
1d3a6 34 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  4 = sqlite3VdbeA
1d3a7 64 64 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e  ddOp3(v, OP_Foun
1d3a8 64 2c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  d, pExpr->iTable
1d3a9 2c 20 30 2c 20 72 4d 61 79 48 61 76 65 4e 75 6c  , 0, rMayHaveNul
1d3aa 6c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  l);.          sq
1d3ab 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1d3ac 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30  v, OP_Integer, 0
1d3ad 2c 20 72 4e 6f 74 46 6f 75 6e 64 29 3b 0a 20 20  , rNotFound);.  
1d3ae 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1d3af 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
1d3b0 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  4);.          sq
1d3b1 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
1d3b2 65 28 76 2c 20 6a 33 29 3b 0a 0a 20 20 20 20 20  e(v, j3);..     
1d3b3 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 74 68 65       /* Copy the
1d3b4 20 76 61 6c 75 65 20 6f 66 20 72 65 67 69 73 74   value of regist
1d3b5 65 72 20 72 4e 6f 74 46 6f 75 6e 64 20 28 77 68  er rNotFound (wh
1d3b6 69 63 68 20 69 73 20 65 69 74 68 65 72 20 4e 55  ich is either NU
1d3b7 4c 4c 20 6f 72 20 30 29 0a 20 20 20 20 20 20 20  LL or 0).       
1d3b8 20 20 20 2a 2a 20 69 6e 74 6f 20 74 68 65 20 74     ** into the t
1d3b9 61 72 67 65 74 20 72 65 67 69 73 74 65 72 2e 20  arget register. 
1d3ba 54 68 69 73 20 77 69 6c 6c 20 62 65 20 74 68 65  This will be the
1d3bb 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a 20   result of the. 
1d3bc 20 20 20 20 20 20 20 20 20 2a 2a 20 65 78 70 72           ** expr
1d3bd 65 73 73 69 6f 6e 2e 0a 20 20 20 20 20 20 20 20  ession..        
1d3be 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 73    */.          s
1d3bf 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d3c0 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20 72 4e 6f  (v, OP_Copy, rNo
1d3c1 74 46 6f 75 6e 64 2c 20 74 61 72 67 65 74 29 3b  tFound, target);
1d3c2 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1d3c3 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
1d3c4 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
1d3c5 6a 32 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  j2);.      sqlit
1d3c6 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
1d3c7 2c 20 6a 35 29 3b 0a 20 20 20 20 20 20 56 64 62  , j5);.      Vdb
1d3c8 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 65 6e  eComment((v, "en
1d3c9 64 20 49 4e 20 65 78 70 72 20 72 25 64 22 2c 20  d IN expr r%d", 
1d3ca 74 61 72 67 65 74 29 29 3b 0a 20 20 20 20 20 20  target));.      
1d3cb 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
1d3cc 64 69 66 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a  dif.    /*.    *
1d3cd 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
1d3ce 20 41 4e 44 20 7a 0a 20 20 20 20 2a 2a 0a 20 20   AND z.    **.  
1d3cf 20 20 2a 2a 20 54 68 69 73 20 69 73 20 65 71 75    ** This is equ
1d3d0 69 76 61 6c 65 6e 74 20 74 6f 0a 20 20 20 20 2a  ivalent to.    *
1d3d1 2a 0a 20 20 20 20 2a 2a 20 20 20 20 78 3e 3d 79  *.    **    x>=y
1d3d2 20 41 4e 44 20 78 3c 3d 7a 0a 20 20 20 20 2a 2a   AND x<=z.    **
1d3d3 0a 20 20 20 20 2a 2a 20 58 20 69 73 20 73 74 6f  .    ** X is sto
1d3d4 72 65 64 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c  red in pExpr->pL
1d3d5 65 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73  eft..    ** Y is
1d3d6 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
1d3d7 2d 3e 70 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45  ->pList->a[0].pE
1d3d8 78 70 72 2e 0a 20 20 20 20 2a 2a 20 5a 20 69 73  xpr..    ** Z is
1d3d9 20 73 74 6f 72 65 64 20 69 6e 20 70 45 78 70 72   stored in pExpr
1d3da 2d 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45  ->pList->a[1].pE
1d3db 78 70 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  xpr..    */.    
1d3dc 63 61 73 65 20 54 4b 5f 42 45 54 57 45 45 4e 3a  case TK_BETWEEN:
1d3dd 20 7b 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70   {.      Expr *p
1d3de 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
1d3df 65 66 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63  eft;.      struc
1d3e0 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
1d3e1 2a 70 4c 49 74 65 6d 20 3d 20 70 45 78 70 72 2d  *pLItem = pExpr-
1d3e2 3e 70 4c 69 73 74 2d 3e 61 3b 0a 20 20 20 20 20  >pList->a;.     
1d3e3 20 45 78 70 72 20 2a 70 52 69 67 68 74 20 3d 20   Expr *pRight = 
1d3e4 70 4c 49 74 65 6d 2d 3e 70 45 78 70 72 3b 0a 0a  pLItem->pExpr;..
1d3e5 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72        codeCompar
1d3e6 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73 65  eOperands(pParse
1d3e7 2c 20 70 4c 65 66 74 2c 20 26 72 31 2c 20 26 72  , pLeft, &r1, &r
1d3e8 65 67 46 72 65 65 31 2c 0a 20 20 20 20 20 20 20  egFree1,.       
1d3e9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3ea 20 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68             pRigh
1d3eb 74 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65  t, &r2, &regFree
1d3ec 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61  2);.      testca
1d3ed 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20  se( regFree1==0 
1d3ee 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1d3ef 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30 20 29  e( regFree2==0 )
1d3f0 3b 0a 20 20 20 20 20 20 72 33 20 3d 20 73 71 6c  ;.      r3 = sql
1d3f1 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1d3f2 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 72 34  Parse);.      r4
1d3f3 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1d3f4 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
1d3f5 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1d3f6 70 50 61 72 73 65 2c 20 70 4c 65 66 74 2c 20 70  pParse, pLeft, p
1d3f7 52 69 67 68 74 2c 20 4f 50 5f 47 65 2c 0a 20 20  Right, OP_Ge,.  
1d3f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d3f9 72 31 2c 20 72 32 2c 20 72 33 2c 20 53 51 4c 49  r1, r2, r3, SQLI
1d3fa 54 45 5f 53 54 4f 52 45 50 32 29 3b 0a 20 20 20  TE_STOREP2);.   
1d3fb 20 20 20 70 4c 49 74 65 6d 2b 2b 3b 0a 20 20 20     pLItem++;.   
1d3fc 20 20 20 70 52 69 67 68 74 20 3d 20 70 4c 49 74     pRight = pLIt
1d3fd 65 6d 2d 3e 70 45 78 70 72 3b 0a 20 20 20 20 20  em->pExpr;.     
1d3fe 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54   sqlite3ReleaseT
1d3ff 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72  empReg(pParse, r
1d400 65 67 46 72 65 65 32 29 3b 0a 20 20 20 20 20 20  egFree2);.      
1d401 72 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  r2 = sqlite3Expr
1d402 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
1d403 20 70 52 69 67 68 74 2c 20 26 72 65 67 46 72 65   pRight, &regFre
1d404 65 32 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  e2);.      testc
1d405 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1d406 20 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f   );.      codeCo
1d407 6d 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 4c  mpare(pParse, pL
1d408 65 66 74 2c 20 70 52 69 67 68 74 2c 20 4f 50 5f  eft, pRight, OP_
1d409 4c 65 2c 20 72 31 2c 20 72 32 2c 20 72 34 2c 20  Le, r1, r2, r4, 
1d40a 53 51 4c 49 54 45 5f 53 54 4f 52 45 50 32 29 3b  SQLITE_STOREP2);
1d40b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1d40c 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41  beAddOp3(v, OP_A
1d40d 6e 64 2c 20 72 33 2c 20 72 34 2c 20 74 61 72 67  nd, r3, r4, targ
1d40e 65 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  et);.      sqlit
1d40f 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
1d410 28 70 50 61 72 73 65 2c 20 72 33 29 3b 0a 20 20  (pParse, r3);.  
1d411 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
1d412 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
1d413 2c 20 72 34 29 3b 0a 20 20 20 20 20 20 62 72 65  , r4);.      bre
1d414 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61  ak;.    }.    ca
1d415 73 65 20 54 4b 5f 55 50 4c 55 53 3a 20 7b 0a 20  se TK_UPLUS: {. 
1d416 20 20 20 20 20 69 6e 52 65 67 20 3d 20 73 71 6c       inReg = sql
1d417 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
1d418 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  et(pParse, pExpr
1d419 2d 3e 70 4c 65 66 74 2c 20 74 61 72 67 65 74 29  ->pLeft, target)
1d41a 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d41b 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a 20 20 20     }..    /*.   
1d41c 20 2a 2a 20 46 6f 72 6d 20 41 3a 0a 20 20 20 20   ** Form A:.    
1d41d 2a 2a 20 20 20 43 41 53 45 20 78 20 57 48 45 4e  **   CASE x WHEN
1d41e 20 65 31 20 54 48 45 4e 20 72 31 20 57 48 45 4e   e1 THEN r1 WHEN
1d41f 20 65 32 20 54 48 45 4e 20 72 32 20 2e 2e 2e 20   e2 THEN r2 ... 
1d420 57 48 45 4e 20 65 4e 20 54 48 45 4e 20 72 4e 20  WHEN eN THEN rN 
1d421 45 4c 53 45 20 79 20 45 4e 44 0a 20 20 20 20 2a  ELSE y END.    *
1d422 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d 20 42 3a  *.    ** Form B:
1d423 0a 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57  .    **   CASE W
1d424 48 45 4e 20 65 31 20 54 48 45 4e 20 72 31 20 57  HEN e1 THEN r1 W
1d425 48 45 4e 20 65 32 20 54 48 45 4e 20 72 32 20 2e  HEN e2 THEN r2 .
1d426 2e 2e 20 57 48 45 4e 20 65 4e 20 54 48 45 4e 20  .. WHEN eN THEN 
1d427 72 4e 20 45 4c 53 45 20 79 20 45 4e 44 0a 20 20  rN ELSE y END.  
1d428 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 46 6f 72 6d    **.    ** Form
1d429 20 41 20 69 73 20 63 61 6e 20 62 65 20 74 72 61   A is can be tra
1d42a 6e 73 66 6f 72 6d 65 64 20 69 6e 74 6f 20 74 68  nsformed into th
1d42b 65 20 65 71 75 69 76 61 6c 65 6e 74 20 66 6f 72  e equivalent for
1d42c 6d 20 42 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  m B as follows:.
1d42d 20 20 20 20 2a 2a 20 20 20 43 41 53 45 20 57 48      **   CASE WH
1d42e 45 4e 20 78 3d 65 31 20 54 48 45 4e 20 72 31 20  EN x=e1 THEN r1 
1d42f 57 48 45 4e 20 78 3d 65 32 20 54 48 45 4e 20 72  WHEN x=e2 THEN r
1d430 32 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20  2 ....    **    
1d431 20 20 20 20 57 48 45 4e 20 78 3d 65 4e 20 54 48      WHEN x=eN TH
1d432 45 4e 20 72 4e 20 45 4c 53 45 20 79 20 45 4e 44  EN rN ELSE y END
1d433 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 58  .    **.    ** X
1d434 20 28 69 66 20 69 74 20 65 78 69 73 74 73 29 20   (if it exists) 
1d435 69 73 20 69 6e 20 70 45 78 70 72 2d 3e 70 4c 65  is in pExpr->pLe
1d436 66 74 2e 0a 20 20 20 20 2a 2a 20 59 20 69 73 20  ft..    ** Y is 
1d437 69 6e 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  in pExpr->pRight
1d438 2e 20 20 54 68 65 20 59 20 69 73 20 61 6c 73 6f  .  The Y is also
1d439 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 74   optional.  If t
1d43a 68 65 72 65 20 69 73 20 6e 6f 0a 20 20 20 20 2a  here is no.    *
1d43b 2a 20 45 4c 53 45 20 63 6c 61 75 73 65 20 61 6e  * ELSE clause an
1d43c 64 20 6e 6f 20 6f 74 68 65 72 20 74 65 72 6d 20  d no other term 
1d43d 6d 61 74 63 68 65 73 2c 20 74 68 65 6e 20 74 68  matches, then th
1d43e 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65 0a  e result of the.
1d43f 20 20 20 20 2a 2a 20 65 78 70 72 73 73 69 6f 6e      ** exprssion
1d440 20 69 73 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2a   is NULL..    **
1d441 20 45 69 20 69 73 20 69 6e 20 70 45 78 70 72 2d   Ei is in pExpr-
1d442 3e 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 5d 20 61  >pList->a[i*2] a
1d443 6e 64 20 52 69 20 69 73 20 70 45 78 70 72 2d 3e  nd Ri is pExpr->
1d444 70 4c 69 73 74 2d 3e 61 5b 69 2a 32 2b 31 5d 2e  pList->a[i*2+1].
1d445 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1d446 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
1d447 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74   expression is t
1d448 68 65 20 52 69 20 66 6f 72 20 74 68 65 20 66 69  he Ri for the fi
1d449 72 73 74 20 6d 61 74 63 68 69 6e 67 20 45 69 2c  rst matching Ei,
1d44a 0a 20 20 20 20 2a 2a 20 6f 72 20 69 66 20 74 68  .    ** or if th
1d44b 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68 69  ere is no matchi
1d44c 6e 67 20 45 69 2c 20 74 68 65 20 45 4c 53 45 20  ng Ei, the ELSE 
1d44d 74 65 72 6d 20 59 2c 20 6f 72 20 69 66 20 74 68  term Y, or if th
1d44e 65 72 65 20 69 73 0a 20 20 20 20 2a 2a 20 6e 6f  ere is.    ** no
1d44f 20 45 4c 53 45 20 74 65 72 6d 2c 20 4e 55 4c 4c   ELSE term, NULL
1d450 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73  ..    */.    cas
1d451 65 20 54 4b 5f 43 41 53 45 3a 20 7b 0a 20 20 20  e TK_CASE: {.   
1d452 20 20 20 69 6e 74 20 65 6e 64 4c 61 62 65 6c 3b     int endLabel;
1d453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d454 20 20 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62       /* GOTO lab
1d455 65 6c 20 66 6f 72 20 65 6e 64 20 6f 66 20 43 41  el for end of CA
1d456 53 45 20 73 74 6d 74 20 2a 2f 0a 20 20 20 20 20  SE stmt */.     
1d457 20 69 6e 74 20 6e 65 78 74 43 61 73 65 3b 20 20   int nextCase;  
1d458 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d459 20 20 20 2f 2a 20 47 4f 54 4f 20 6c 61 62 65 6c     /* GOTO label
1d45a 20 66 6f 72 20 6e 65 78 74 20 57 48 45 4e 20 63   for next WHEN c
1d45b 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 69  lause */.      i
1d45c 6e 74 20 6e 45 78 70 72 3b 20 20 20 20 20 20 20  nt nExpr;       
1d45d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d45e 20 2f 2a 20 32 78 20 6e 75 6d 62 65 72 20 6f 66   /* 2x number of
1d45f 20 57 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20   WHEN terms */. 
1d460 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20 20       int i;     
1d461 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d462 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63         /* Loop c
1d463 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20 20 20 20  ounter */.      
1d464 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
1d465 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d466 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 57 48 45    /* List of WHE
1d467 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20  N terms */.     
1d468 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
1d469 5f 69 74 65 6d 20 2a 61 4c 69 73 74 65 6c 65 6d  _item *aListelem
1d46a 3b 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 57  ;  /* Array of W
1d46b 48 45 4e 20 74 65 72 6d 73 20 2a 2f 0a 20 20 20  HEN terms */.   
1d46c 20 20 20 45 78 70 72 20 6f 70 43 6f 6d 70 61 72     Expr opCompar
1d46d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e;              
1d46e 20 20 20 20 20 2f 2a 20 54 68 65 20 58 3d 3d 45       /* The X==E
1d46f 69 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a  i expression */.
1d470 20 20 20 20 20 20 45 78 70 72 20 63 61 63 68 65        Expr cache
1d471 58 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  X;              
1d472 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
1d473 64 20 65 78 70 72 65 73 73 69 6f 6e 20 58 20 2a  d expression X *
1d474 2f 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 58  /.      Expr *pX
1d475 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
1d476 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
1d477 20 58 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f   X expression */
1d478 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 54 65  .      Expr *pTe
1d479 73 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  st;             
1d47a 20 20 20 20 20 20 20 20 20 2f 2a 20 58 3d 3d 45           /* X==E
1d47b 69 20 28 66 6f 72 6d 20 41 29 20 6f 72 20 6a 75  i (form A) or ju
1d47c 73 74 20 45 69 20 28 66 6f 72 6d 20 42 29 20 2a  st Ei (form B) *
1d47d 2f 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  /..      assert(
1d47e 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b 0a 20  pExpr->pList);. 
1d47f 20 20 20 20 20 61 73 73 65 72 74 28 28 70 45 78       assert((pEx
1d480 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72  pr->pList->nExpr
1d481 20 25 20 32 29 20 3d 3d 20 30 29 3b 0a 20 20 20   % 2) == 0);.   
1d482 20 20 20 61 73 73 65 72 74 28 70 45 78 70 72 2d     assert(pExpr-
1d483 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3e 20  >pList->nExpr > 
1d484 30 29 3b 0a 20 20 20 20 20 20 70 45 4c 69 73 74  0);.      pEList
1d485 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
1d486 0a 20 20 20 20 20 20 61 4c 69 73 74 65 6c 65 6d  .      aListelem
1d487 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20   = pEList->a;.  
1d488 20 20 20 20 6e 45 78 70 72 20 3d 20 70 45 4c 69      nExpr = pELi
1d489 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20  st->nExpr;.     
1d48a 20 65 6e 64 4c 61 62 65 6c 20 3d 20 73 71 6c 69   endLabel = sqli
1d48b 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
1d48c 28 76 29 3b 0a 20 20 20 20 20 20 69 66 28 20 28  (v);.      if( (
1d48d 70 58 20 3d 20 70 45 78 70 72 2d 3e 70 4c 65 66  pX = pExpr->pLef
1d48e 74 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  t)!=0 ){.       
1d48f 20 63 61 63 68 65 58 20 3d 20 2a 70 58 3b 0a 20   cacheX = *pX;. 
1d490 20 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28         testcase(
1d491 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55   pX->op==TK_COLU
1d492 4d 4e 20 7c 7c 20 70 58 2d 3e 6f 70 3d 3d 54 4b  MN || pX->op==TK
1d493 5f 52 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20  _REGISTER );.   
1d494 20 20 20 20 20 63 61 63 68 65 58 2e 69 54 61 62       cacheX.iTab
1d495 6c 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  le = sqlite3Expr
1d496 43 6f 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c  CodeTemp(pParse,
1d497 20 70 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b   pX, &regFree1);
1d498 0a 20 20 20 20 20 20 20 20 74 65 73 74 63 61 73  .        testcas
1d499 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
1d49a 3b 0a 20 20 20 20 20 20 20 20 63 61 63 68 65 58  ;.        cacheX
1d49b 2e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53 54 45  .op = TK_REGISTE
1d49c 52 3b 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d  R;.        opCom
1d49d 70 61 72 65 2e 6f 70 20 3d 20 54 4b 5f 45 51 3b  pare.op = TK_EQ;
1d49e 0a 20 20 20 20 20 20 20 20 6f 70 43 6f 6d 70 61  .        opCompa
1d49f 72 65 2e 70 4c 65 66 74 20 3d 20 26 63 61 63 68  re.pLeft = &cach
1d4a0 65 58 3b 0a 20 20 20 20 20 20 20 20 70 54 65 73  eX;.        pTes
1d4a1 74 20 3d 20 26 6f 70 43 6f 6d 70 61 72 65 3b 0a  t = &opCompare;.
1d4a2 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 50        }.      pP
1d4a3 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1d4a4 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 66  Cache++;.      f
1d4a5 6f 72 28 69 3d 30 3b 20 69 3c 6e 45 78 70 72 3b  or(i=0; i<nExpr;
1d4a6 20 69 3d 69 2b 32 29 7b 0a 20 20 20 20 20 20 20   i=i+2){.       
1d4a7 20 69 66 28 20 70 58 20 29 7b 0a 20 20 20 20 20   if( pX ){.     
1d4a8 20 20 20 20 20 6f 70 43 6f 6d 70 61 72 65 2e 70       opCompare.p
1d4a9 52 69 67 68 74 20 3d 20 61 4c 69 73 74 65 6c 65  Right = aListele
1d4aa 6d 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  m[i].pExpr;.    
1d4ab 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1d4ac 20 20 20 20 20 70 54 65 73 74 20 3d 20 61 4c 69       pTest = aLi
1d4ad 73 74 65 6c 65 6d 5b 69 5d 2e 70 45 78 70 72 3b  stelem[i].pExpr;
1d4ae 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1d4af 20 20 20 6e 65 78 74 43 61 73 65 20 3d 20 73 71     nextCase = sq
1d4b0 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1d4b1 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 74  el(v);.        t
1d4b2 65 73 74 63 61 73 65 28 20 70 54 65 73 74 2d 3e  estcase( pTest->
1d4b3 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c 7c  op==TK_COLUMN ||
1d4b4 20 70 54 65 73 74 2d 3e 6f 70 3d 3d 54 4b 5f 52   pTest->op==TK_R
1d4b5 45 47 49 53 54 45 52 20 29 3b 0a 20 20 20 20 20  EGISTER );.     
1d4b6 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
1d4b7 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 54  False(pParse, pT
1d4b8 65 73 74 2c 20 6e 65 78 74 43 61 73 65 2c 20 53  est, nextCase, S
1d4b9 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c 4c  QLITE_JUMPIFNULL
1d4ba 29 3b 0a 20 20 20 20 20 20 20 20 74 65 73 74 63  );.        testc
1d4bb 61 73 65 28 20 61 4c 69 73 74 65 6c 65 6d 5b 69  ase( aListelem[i
1d4bc 2b 31 5d 2e 70 45 78 70 72 2d 3e 6f 70 3d 3d 54  +1].pExpr->op==T
1d4bd 4b 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20 20 20  K_COLUMN );.    
1d4be 20 20 20 20 74 65 73 74 63 61 73 65 28 20 61 4c      testcase( aL
1d4bf 69 73 74 65 6c 65 6d 5b 69 2b 31 5d 2e 70 45 78  istelem[i+1].pEx
1d4c0 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 52 45 47 49 53  pr->op==TK_REGIS
1d4c1 54 45 52 20 29 3b 0a 20 20 20 20 20 20 20 20 73  TER );.        s
1d4c2 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1d4c3 50 61 72 73 65 2c 20 61 4c 69 73 74 65 6c 65 6d  Parse, aListelem
1d4c4 5b 69 2b 31 5d 2e 70 45 78 70 72 2c 20 74 61 72  [i+1].pExpr, tar
1d4c5 67 65 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71  get);.        sq
1d4c6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1d4c7 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 65  v, OP_Goto, 0, e
1d4c8 6e 64 4c 61 62 65 6c 29 3b 0a 20 20 20 20 20 20  ndLabel);.      
1d4c9 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
1d4ca 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 6e 65 78  olveLabel(v, nex
1d4cb 74 43 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a  tCase);.      }.
1d4cc 20 20 20 20 20 20 69 66 28 20 70 45 78 70 72 2d        if( pExpr-
1d4cd 3e 70 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20  >pRight ){.     
1d4ce 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
1d4cf 64 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  de(pParse, pExpr
1d4d0 2d 3e 70 52 69 67 68 74 2c 20 74 61 72 67 65 74  ->pRight, target
1d4d1 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  );.      }else{.
1d4d2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
1d4d3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1d4d4 4e 75 6c 6c 2c 20 30 2c 20 74 61 72 67 65 74 29  Null, 0, target)
1d4d5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1d4d6 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
1d4d7 76 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4c 61  veLabel(v, endLa
1d4d8 62 65 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65  bel);.      asse
1d4d9 72 74 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61  rt( pParse->disa
1d4da 62 6c 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b  bleColCache>0 );
1d4db 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
1d4dc 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d  isableColCache--
1d4dd 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d4de 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c     }.#ifndef SQL
1d4df 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1d4e0 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 41 49  .    case TK_RAI
1d4e1 53 45 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  SE: {.      if( 
1d4e2 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61  !pParse->trigSta
1d4e3 63 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ck ){.        sq
1d4e4 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1d4e5 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 20 20  arse,.          
1d4e6 20 20 20 20 20 20 20 20 20 20 20 20 20 22 52 41               "RA
1d4e7 49 53 45 28 29 20 6d 61 79 20 6f 6e 6c 79 20 62  ISE() may only b
1d4e8 65 20 75 73 65 64 20 77 69 74 68 69 6e 20 61 20  e used within a 
1d4e9 74 72 69 67 67 65 72 2d 70 72 6f 67 72 61 6d 22  trigger-program"
1d4ea 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  );.        retur
1d4eb 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 0;.      }.   
1d4ec 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43     if( pExpr->iC
1d4ed 6f 6c 75 6d 6e 21 3d 4f 45 5f 49 67 6e 6f 72 65  olumn!=OE_Ignore
1d4ee 20 29 7b 0a 20 20 20 20 20 20 20 20 20 61 73 73   ){.         ass
1d4ef 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c  ert( pExpr->iCol
1d4f0 75 6d 6e 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b  umn==OE_Rollback
1d4f1 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
1d4f2 20 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c       pExpr->iCol
1d4f3 75 6d 6e 20 3d 3d 20 4f 45 5f 41 62 6f 72 74 20  umn == OE_Abort 
1d4f4 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ||.             
1d4f5 20 20 20 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75      pExpr->iColu
1d4f6 6d 6e 20 3d 3d 20 4f 45 5f 46 61 69 6c 20 29 3b  mn == OE_Fail );
1d4f7 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  .         sqlite
1d4f8 33 44 65 71 75 6f 74 65 45 78 70 72 28 64 62 2c  3DequoteExpr(db,
1d4f9 20 70 45 78 70 72 29 3b 0a 20 20 20 20 20 20 20   pExpr);.       
1d4fa 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d4fb 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20  Op4(v, OP_Halt, 
1d4fc 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e  SQLITE_CONSTRAIN
1d4fd 54 2c 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d  T, pExpr->iColum
1d4fe 6e 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  n, 0,.          
1d4ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
1d500 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65  har*)pExpr->toke
1d501 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b 65  n.z, pExpr->toke
1d502 6e 2e 6e 29 3b 0a 20 20 20 20 20 20 7d 20 65 6c  n.n);.      } el
1d503 73 65 20 7b 0a 20 20 20 20 20 20 20 20 20 61 73  se {.         as
1d504 73 65 72 74 28 20 70 45 78 70 72 2d 3e 69 43 6f  sert( pExpr->iCo
1d505 6c 75 6d 6e 20 3d 3d 20 4f 45 5f 49 67 6e 6f 72  lumn == OE_Ignor
1d506 65 20 29 3b 0a 20 20 20 20 20 20 20 20 20 73 71  e );.         sq
1d507 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1d508 76 2c 20 4f 50 5f 43 6f 6e 74 65 78 74 50 6f 70  v, OP_ContextPop
1d509 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
1d50a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d50b 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
1d50c 30 2c 20 70 50 61 72 73 65 2d 3e 74 72 69 67 53  0, pParse->trigS
1d50d 74 61 63 6b 2d 3e 69 67 6e 6f 72 65 4a 75 6d 70  tack->ignoreJump
1d50e 29 3b 0a 20 20 20 20 20 20 20 20 20 56 64 62 65  );.         Vdbe
1d50f 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 61 69  Comment((v, "rai
1d510 73 65 28 49 47 4e 4f 52 45 29 22 29 29 3b 0a 20  se(IGNORE)"));. 
1d511 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72 65       }.      bre
1d512 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
1d513 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65  .  }.  sqlite3Re
1d514 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
1d515 72 73 65 2c 20 72 65 67 46 72 65 65 31 29 3b 0a  rse, regFree1);.
1d516 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
1d517 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
1d518 72 65 67 46 72 65 65 32 29 3b 0a 20 20 72 65 74  regFree2);.  ret
1d519 75 72 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a  urn inReg;.}../*
1d51a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1d51b 65 20 74 6f 20 65 76 61 6c 75 61 74 65 20 61 6e  e to evaluate an
1d51c 20 65 78 70 72 65 73 73 69 6f 6e 20 61 6e 64 20   expression and 
1d51d 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74  store the result
1d51e 73 0a 2a 2a 20 69 6e 74 6f 20 61 20 72 65 67 69  s.** into a regi
1d51f 73 74 65 72 2e 20 20 52 65 74 75 72 6e 20 74 68  ster.  Return th
1d520 65 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  e register numbe
1d521 72 20 77 68 65 72 65 20 74 68 65 20 72 65 73 75  r where the resu
1d522 6c 74 73 0a 2a 2a 20 61 72 65 20 73 74 6f 72 65  lts.** are store
1d523 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
1d524 72 65 67 69 73 74 65 72 20 69 73 20 61 20 74 65  register is a te
1d525 6d 70 6f 72 61 72 79 20 72 65 67 69 73 74 65 72  mporary register
1d526 20 74 68 61 74 20 63 61 6e 20 62 65 20 64 65 61   that can be dea
1d527 6c 6c 6f 63 61 74 65 64 2c 0a 2a 2a 20 74 68 65  llocated,.** the
1d528 6e 20 77 72 69 74 65 20 69 74 73 20 6e 75 6d 62  n write its numb
1d529 65 72 20 69 6e 74 6f 20 2a 70 52 65 67 2e 20 20  er into *pReg.  
1d52a 49 66 20 74 68 65 20 72 65 73 75 6c 74 20 72 65  If the result re
1d52b 67 69 73 74 65 72 20 69 73 20 6e 6f 74 0a 2a 2a  gister is not.**
1d52c 20 61 20 74 65 6d 70 6f 72 61 72 79 2c 20 74 68   a temporary, th
1d52d 65 6e 20 73 65 74 20 2a 70 52 65 67 20 74 6f 20  en set *pReg to 
1d52e 7a 65 72 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  zero..*/.SQLITE_
1d52f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1d530 74 65 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28  te3ExprCodeTemp(
1d531 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45  Parse *pParse, E
1d532 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
1d533 2a 70 52 65 67 29 7b 0a 20 20 69 6e 74 20 72 31  *pReg){.  int r1
1d534 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d   = sqlite3GetTem
1d535 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20  pReg(pParse);.  
1d536 69 6e 74 20 72 32 20 3d 20 73 71 6c 69 74 65 33  int r2 = sqlite3
1d537 45 78 70 72 43 6f 64 65 54 61 72 67 65 74 28 70  ExprCodeTarget(p
1d538 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20 72 31  Parse, pExpr, r1
1d539 29 3b 0a 20 20 69 66 28 20 72 32 3d 3d 72 31 20  );.  if( r2==r1 
1d53a 29 7b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 72  ){.    *pReg = r
1d53b 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  1;.  }else{.    
1d53c 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
1d53d 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 31  mpReg(pParse, r1
1d53e 29 3b 0a 20 20 20 20 2a 70 52 65 67 20 3d 20 30  );.    *pReg = 0
1d53f 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72  ;.  }.  return r
1d540 32 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  2;.}../*.** Gene
1d541 72 61 74 65 20 63 6f 64 65 20 74 68 61 74 20 77  rate code that w
1d542 69 6c 6c 20 65 76 61 6c 75 61 74 65 20 65 78 70  ill evaluate exp
1d543 72 65 73 73 69 6f 6e 20 70 45 78 70 72 20 61 6e  ression pExpr an
1d544 64 20 73 74 6f 72 65 20 74 68 65 0a 2a 2a 20 72  d store the.** r
1d545 65 73 75 6c 74 73 20 69 6e 20 72 65 67 69 73 74  esults in regist
1d546 65 72 20 74 61 72 67 65 74 2e 20 20 54 68 65 20  er target.  The 
1d547 72 65 73 75 6c 74 73 20 61 72 65 20 67 75 61 72  results are guar
1d548 61 6e 74 65 65 64 20 74 6f 20 61 70 70 65 61 72  anteed to appear
1d549 0a 2a 2a 20 69 6e 20 72 65 67 69 73 74 65 72 20  .** in register 
1d54a 74 61 72 67 65 74 2e 0a 2a 2f 0a 53 51 4c 49 54  target..*/.SQLIT
1d54b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1d54c 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 50 61  lite3ExprCode(Pa
1d54d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70  rse *pParse, Exp
1d54e 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20 74 61  r *pExpr, int ta
1d54f 72 67 65 74 29 7b 0a 20 20 69 6e 74 20 69 6e 52  rget){.  int inR
1d550 65 67 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 74  eg;..  assert( t
1d551 61 72 67 65 74 3e 30 20 26 26 20 74 61 72 67 65  arget>0 && targe
1d552 74 3c 3d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  t<=pParse->nMem 
1d553 29 3b 0a 20 20 69 6e 52 65 67 20 3d 20 73 71 6c  );.  inReg = sql
1d554 69 74 65 33 45 78 70 72 43 6f 64 65 54 61 72 67  ite3ExprCodeTarg
1d555 65 74 28 70 50 61 72 73 65 2c 20 70 45 78 70 72  et(pParse, pExpr
1d556 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61 73 73  , target);.  ass
1d557 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 56 64  ert( pParse->pVd
1d558 62 65 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62  be || pParse->db
1d559 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1d55a 3b 0a 20 20 69 66 28 20 69 6e 52 65 67 21 3d 74  ;.  if( inReg!=t
1d55b 61 72 67 65 74 20 26 26 20 70 50 61 72 73 65 2d  arget && pParse-
1d55c 3e 70 56 64 62 65 20 29 7b 0a 20 20 20 20 73 71  >pVdbe ){.    sq
1d55d 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
1d55e 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f  pParse->pVdbe, O
1d55f 50 5f 53 43 6f 70 79 2c 20 69 6e 52 65 67 2c 20  P_SCopy, inReg, 
1d560 74 61 72 67 65 74 29 3b 0a 20 20 7d 0a 20 20 72  target);.  }.  r
1d561 65 74 75 72 6e 20 74 61 72 67 65 74 3b 0a 7d 0a  eturn target;.}.
1d562 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1d563 63 6f 64 65 20 74 68 61 74 20 65 76 61 6c 75 74  code that evalut
1d564 65 73 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  es the given exp
1d565 72 65 73 73 69 6f 6e 20 61 6e 64 20 70 75 74 73  ression and puts
1d566 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69   the result.** i
1d567 6e 20 72 65 67 69 73 74 65 72 20 74 61 72 67 65  n register targe
1d568 74 2e 0a 2a 2a 0a 2a 2a 20 41 6c 73 6f 20 6d 61  t..**.** Also ma
1d569 6b 65 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65  ke a copy of the
1d56a 20 65 78 70 72 65 73 73 69 6f 6e 20 72 65 73 75   expression resu
1d56b 6c 74 73 20 69 6e 74 6f 20 61 6e 6f 74 68 65 72  lts into another
1d56c 20 22 63 61 63 68 65 22 20 72 65 67 69 73 74 65   "cache" registe
1d56d 72 0a 2a 2a 20 61 6e 64 20 6d 6f 64 69 66 79 20  r.** and modify 
1d56e 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 73  the expression s
1d56f 6f 20 74 68 61 74 20 74 68 65 20 6e 65 78 74 20  o that the next 
1d570 74 69 6d 65 20 69 74 20 69 73 20 65 76 61 6c 75  time it is evalu
1d571 61 74 65 64 2c 0a 2a 2a 20 74 68 65 20 72 65 73  ated,.** the res
1d572 75 6c 74 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ult is a copy of
1d573 20 74 68 65 20 63 61 63 68 65 20 72 65 67 69 73   the cache regis
1d574 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ter..**.** This 
1d575 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
1d576 66 6f 72 20 65 78 70 72 65 73 73 69 6f 6e 73 20  for expressions 
1d577 74 68 61 74 20 61 72 65 20 75 73 65 64 20 6d 75  that are used mu
1d578 6c 74 69 70 6c 65 20 0a 2a 2a 20 74 69 6d 65 73  ltiple .** times
1d579 2e 20 20 54 68 65 79 20 61 72 65 20 65 76 61 6c  .  They are eval
1d57a 75 61 74 65 64 20 6f 6e 63 65 20 61 6e 64 20 74  uated once and t
1d57b 68 65 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  he results of th
1d57c 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
1d57d 61 72 65 20 72 65 75 73 65 64 2e 0a 2a 2f 0a 53  are reused..*/.S
1d57e 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
1d57f 74 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  t sqlite3ExprCod
1d580 65 41 6e 64 43 61 63 68 65 28 50 61 72 73 65 20  eAndCache(Parse 
1d581 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70  *pParse, Expr *p
1d582 45 78 70 72 2c 20 69 6e 74 20 74 61 72 67 65 74  Expr, int target
1d583 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
1d584 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
1d585 69 6e 74 20 69 6e 52 65 67 3b 0a 20 20 69 6e 52  int inReg;.  inR
1d586 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
1d587 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 45 78  Code(pParse, pEx
1d588 70 72 2c 20 74 61 72 67 65 74 29 3b 0a 20 20 61  pr, target);.  a
1d589 73 73 65 72 74 28 20 74 61 72 67 65 74 3e 30 20  ssert( target>0 
1d58a 29 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  );.  if( pExpr->
1d58b 6f 70 21 3d 54 4b 5f 52 45 47 49 53 54 45 52 20  op!=TK_REGISTER 
1d58c 29 7b 20 20 0a 20 20 20 20 69 6e 74 20 69 4d 65  ){  .    int iMe
1d58d 6d 3b 0a 20 20 20 20 69 4d 65 6d 20 3d 20 2b 2b  m;.    iMem = ++
1d58e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
1d58f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1d590 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c 20  Op2(v, OP_Copy, 
1d591 69 6e 52 65 67 2c 20 69 4d 65 6d 29 3b 0a 20 20  inReg, iMem);.  
1d592 20 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 20    pExpr->iTable 
1d593 3d 20 69 4d 65 6d 3b 0a 20 20 20 20 70 45 78 70  = iMem;.    pExp
1d594 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49 53  r->op = TK_REGIS
1d595 54 45 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  TER;.  }.  retur
1d596 6e 20 69 6e 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a  n inReg;.}../*.*
1d597 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66  * Return TRUE if
1d598 20 70 45 78 70 72 20 69 73 20 61 6e 20 63 6f 6e   pExpr is an con
1d599 73 74 61 6e 74 20 65 78 70 72 65 73 73 69 6f 6e  stant expression
1d59a 20 74 68 61 74 20 69 73 20 61 70 70 72 6f 70 72   that is appropr
1d59b 69 61 74 65 0a 2a 2a 20 66 6f 72 20 66 61 63 74  iate.** for fact
1d59c 6f 72 69 6e 67 20 6f 75 74 20 6f 66 20 61 20 6c  oring out of a l
1d59d 6f 6f 70 2e 20 20 41 70 70 72 6f 70 72 69 61 74  oop.  Appropriat
1d59e 65 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  e expressions ar
1d59f 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 41  e:.**.**    *  A
1d5a0 6e 79 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68  ny expression th
1d5a1 61 74 20 65 76 61 6c 75 61 74 65 73 20 74 6f 20  at evaluates to 
1d5a2 74 77 6f 20 6f 72 20 6d 6f 72 65 20 6f 70 63 6f  two or more opco
1d5a3 64 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20  des..**.**    * 
1d5a4 20 41 6e 79 20 4f 50 5f 49 6e 74 65 67 65 72 2c   Any OP_Integer,
1d5a5 20 4f 50 5f 52 65 61 6c 2c 20 4f 50 5f 53 74 72   OP_Real, OP_Str
1d5a6 69 6e 67 2c 20 4f 50 5f 42 6c 6f 62 2c 20 4f 50  ing, OP_Blob, OP
1d5a7 5f 4e 75 6c 6c 2c 20 0a 2a 2a 20 20 20 20 20 20  _Null, .**      
1d5a8 20 6f 72 20 4f 50 5f 56 61 72 69 61 62 6c 65 20   or OP_Variable 
1d5a9 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 6e 65  that does not ne
1d5aa 65 64 20 74 6f 20 62 65 20 70 6c 61 63 65 64 20  ed to be placed 
1d5ab 69 6e 20 61 20 0a 2a 2a 20 20 20 20 20 20 20 73  in a .**       s
1d5ac 70 65 63 69 66 69 63 20 72 65 67 69 73 74 65 72  pecific register
1d5ad 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65 20 69 73  ..**.** There is
1d5ae 20 6e 6f 20 70 6f 69 6e 74 20 69 6e 20 66 61 63   no point in fac
1d5af 74 6f 72 69 6e 67 20 6f 75 74 20 73 69 6e 67 6c  toring out singl
1d5b0 65 2d 69 6e 73 74 72 75 63 74 69 6f 6e 20 63 6f  e-instruction co
1d5b1 6e 73 74 61 6e 74 0a 2a 2a 20 65 78 70 72 65 73  nstant.** expres
1d5b2 73 69 6f 6e 73 20 74 68 61 74 20 6e 65 65 64 20  sions that need 
1d5b3 74 6f 20 62 65 20 70 6c 61 63 65 64 20 69 6e 20  to be placed in 
1d5b4 61 20 70 61 72 74 69 63 75 6c 61 72 20 72 65 67  a particular reg
1d5b5 69 73 74 65 72 2e 20 20 0a 2a 2a 20 57 65 20 63  ister.  .** We c
1d5b6 6f 75 6c 64 20 66 61 63 74 6f 72 20 74 68 65 6d  ould factor them
1d5b7 20 6f 75 74 2c 20 62 75 74 20 74 68 65 6e 20 77   out, but then w
1d5b8 65 20 77 6f 75 6c 64 20 65 6e 64 20 75 70 20 61  e would end up a
1d5b9 64 64 69 6e 67 20 61 6e 0a 2a 2a 20 4f 50 5f 53  dding an.** OP_S
1d5ba 43 6f 70 79 20 69 6e 73 74 72 75 63 74 69 6f 6e  Copy instruction
1d5bb 20 74 6f 20 6d 6f 76 65 20 74 68 65 20 76 61 6c   to move the val
1d5bc 75 65 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 72  ue into the corr
1d5bd 65 63 74 20 72 65 67 69 73 74 65 72 0a 2a 2a 20  ect register.** 
1d5be 6c 61 74 65 72 2e 20 20 57 65 20 6d 69 67 68 74  later.  We might
1d5bf 20 61 73 20 77 65 6c 6c 20 6a 75 73 74 20 75 73   as well just us
1d5c0 65 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 69  e the original i
1d5c1 6e 73 74 72 75 63 74 69 6f 6e 20 61 6e 64 0a 2a  nstruction and.*
1d5c2 2a 20 61 76 6f 69 64 20 74 68 65 20 4f 50 5f 53  * avoid the OP_S
1d5c3 43 6f 70 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  Copy..*/.static 
1d5c4 69 6e 74 20 69 73 41 70 70 72 6f 70 72 69 61 74  int isAppropriat
1d5c5 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 45 78  eForFactoring(Ex
1d5c6 70 72 20 2a 70 29 7b 0a 20 20 69 66 28 20 21 73  pr *p){.  if( !s
1d5c7 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e 73  qlite3ExprIsCons
1d5c8 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 29 20 29  tantNotJoin(p) )
1d5c9 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
1d5ca 20 2f 2a 20 4f 6e 6c 79 20 63 6f 6e 73 74 61 6e   /* Only constan
1d5cb 74 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61 72  t expressions ar
1d5cc 65 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f  e appropriate fo
1d5cd 72 20 66 61 63 74 6f 72 69 6e 67 20 2a 2f 0a 20  r factoring */. 
1d5ce 20 7d 0a 20 20 69 66 28 20 28 70 2d 3e 66 6c 61   }.  if( (p->fla
1d5cf 67 73 20 26 20 45 50 5f 46 69 78 65 64 44 65 73  gs & EP_FixedDes
1d5d0 74 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74  t)==0 ){.    ret
1d5d1 75 72 6e 20 31 3b 20 20 2f 2a 20 41 6e 79 20 63  urn 1;  /* Any c
1d5d2 6f 6e 73 74 61 6e 74 20 77 69 74 68 6f 75 74 20  onstant without 
1d5d3 61 20 66 69 78 65 64 20 64 65 73 74 69 6e 61 74  a fixed destinat
1d5d4 69 6f 6e 20 69 73 20 61 70 70 72 6f 70 72 69 61  ion is appropria
1d5d5 74 65 20 2a 2f 0a 20 20 7d 0a 20 20 77 68 69 6c  te */.  }.  whil
1d5d6 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 55 50 4c  e( p->op==TK_UPL
1d5d7 55 53 20 29 20 70 20 3d 20 70 2d 3e 70 4c 65 66  US ) p = p->pLef
1d5d8 74 3b 0a 20 20 73 77 69 74 63 68 28 20 70 2d 3e  t;.  switch( p->
1d5d9 6f 70 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  op ){.#ifndef SQ
1d5da 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42 5f 4c  LITE_OMIT_BLOB_L
1d5db 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73 65 20  ITERAL.    case 
1d5dc 54 4b 5f 42 4c 4f 42 3a 0a 23 65 6e 64 69 66 0a  TK_BLOB:.#endif.
1d5dd 20 20 20 20 63 61 73 65 20 54 4b 5f 56 41 52 49      case TK_VARI
1d5de 41 42 4c 45 3a 0a 20 20 20 20 63 61 73 65 20 54  ABLE:.    case T
1d5df 4b 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20 20 63  K_INTEGER:.    c
1d5e0 61 73 65 20 54 4b 5f 46 4c 4f 41 54 3a 0a 20 20  ase TK_FLOAT:.  
1d5e1 20 20 63 61 73 65 20 54 4b 5f 4e 55 4c 4c 3a 0a    case TK_NULL:.
1d5e2 20 20 20 20 63 61 73 65 20 54 4b 5f 53 54 52 49      case TK_STRI
1d5e3 4e 47 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  NG: {.      test
1d5e4 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
1d5e5 42 4c 4f 42 20 29 3b 0a 20 20 20 20 20 20 74 65  BLOB );.      te
1d5e6 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
1d5e7 4b 5f 56 41 52 49 41 42 4c 45 20 29 3b 0a 20 20  K_VARIABLE );.  
1d5e8 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 2d      testcase( p-
1d5e9 3e 6f 70 3d 3d 54 4b 5f 49 4e 54 45 47 45 52 20  >op==TK_INTEGER 
1d5ea 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1d5eb 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f  e( p->op==TK_FLO
1d5ec 41 54 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  AT );.      test
1d5ed 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  case( p->op==TK_
1d5ee 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65  NULL );.      te
1d5ef 73 74 63 61 73 65 28 20 70 2d 3e 6f 70 3d 3d 54  stcase( p->op==T
1d5f0 4b 5f 53 54 52 49 4e 47 20 29 3b 0a 20 20 20 20  K_STRING );.    
1d5f1 20 20 2f 2a 20 53 69 6e 67 6c 65 2d 69 6e 73 74    /* Single-inst
1d5f2 72 75 63 74 69 6f 6e 20 63 6f 6e 73 74 61 6e 74  ruction constant
1d5f3 73 20 77 69 74 68 20 61 20 66 69 78 65 64 20 64  s with a fixed d
1d5f4 65 73 74 69 6e 61 74 69 6f 6e 20 61 72 65 0a 20  estination are. 
1d5f5 20 20 20 20 20 2a 2a 20 62 65 74 74 65 72 20 64       ** better d
1d5f6 6f 6e 65 20 69 6e 2d 6c 69 6e 65 2e 20 20 49 66  one in-line.  If
1d5f7 20 77 65 20 66 61 63 74 6f 72 20 74 68 65 6d 2c   we factor them,
1d5f8 20 74 68 65 79 20 77 69 6c 6c 20 6a 75 73 74 20   they will just 
1d5f9 65 6e 64 0a 20 20 20 20 20 20 2a 2a 20 75 70 20  end.      ** up 
1d5fa 67 65 6e 65 72 61 74 69 6e 67 20 61 6e 20 4f 50  generating an OP
1d5fb 5f 53 43 6f 70 79 20 74 6f 20 6d 6f 76 65 20 74  _SCopy to move t
1d5fc 68 65 20 76 61 6c 75 65 20 74 6f 20 74 68 65 20  he value to the 
1d5fd 64 65 73 74 69 6e 61 74 69 6f 6e 0a 20 20 20 20  destination.    
1d5fe 20 20 2a 2a 20 72 65 67 69 73 74 65 72 2e 20 2a    ** register. *
1d5ff 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  /.      return 0
1d600 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65  ;.    }.    case
1d601 20 54 4b 5f 55 4d 49 4e 55 53 3a 20 7b 0a 20 20   TK_UMINUS: {.  
1d602 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4c 65 66       if( p->pLef
1d603 74 2d 3e 6f 70 3d 3d 54 4b 5f 46 4c 4f 41 54 20  t->op==TK_FLOAT 
1d604 7c 7c 20 70 2d 3e 70 4c 65 66 74 2d 3e 6f 70 3d  || p->pLeft->op=
1d605 3d 54 4b 5f 49 4e 54 45 47 45 52 20 29 7b 0a 20  =TK_INTEGER ){. 
1d606 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
1d607 3b 0a 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ;.       }.     
1d608 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1d609 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20     default: {.  
1d60a 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d60b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b  .  }.  return 1;
1d60c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 70 45 78  .}../*.** If pEx
1d60d 70 72 20 69 73 20 61 20 63 6f 6e 73 74 61 6e 74  pr is a constant
1d60e 20 65 78 70 72 65 73 73 69 6f 6e 20 74 68 61 74   expression that
1d60f 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65 20   is appropriate 
1d610 66 6f 72 0a 2a 2a 20 66 61 63 74 6f 72 69 6e 67  for.** factoring
1d611 20 6f 75 74 20 6f 66 20 61 20 6c 6f 6f 70 2c 20   out of a loop, 
1d612 74 68 65 6e 20 65 76 61 6c 75 61 74 65 20 74 68  then evaluate th
1d613 65 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20  e expression.** 
1d614 69 6e 74 6f 20 61 20 72 65 67 69 73 74 65 72 20  into a register 
1d615 61 6e 64 20 63 6f 6e 76 65 72 74 20 74 68 65 20  and convert the 
1d616 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 74 6f 20  expression into 
1d617 61 20 54 4b 5f 52 45 47 49 53 54 45 52 0a 2a 2a  a TK_REGISTER.**
1d618 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2f 0a   expression..*/.
1d619 73 74 61 74 69 63 20 69 6e 74 20 65 76 61 6c 43  static int evalC
1d61a 6f 6e 73 74 45 78 70 72 28 57 61 6c 6b 65 72 20  onstExpr(Walker 
1d61b 2a 70 57 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a  *pWalker, Expr *
1d61c 70 45 78 70 72 29 7b 0a 20 20 50 61 72 73 65 20  pExpr){.  Parse 
1d61d 2a 70 50 61 72 73 65 20 3d 20 70 57 61 6c 6b 65  *pParse = pWalke
1d61e 72 2d 3e 70 50 61 72 73 65 3b 0a 20 20 73 77 69  r->pParse;.  swi
1d61f 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29  tch( pExpr->op )
1d620 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 52 45  {.    case TK_RE
1d621 47 49 53 54 45 52 3a 20 7b 0a 20 20 20 20 20 20  GISTER: {.      
1d622 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1d623 20 20 20 20 63 61 73 65 20 54 4b 5f 46 55 4e 43      case TK_FUNC
1d624 54 49 4f 4e 3a 0a 20 20 20 20 63 61 73 65 20 54  TION:.    case T
1d625 4b 5f 41 47 47 5f 46 55 4e 43 54 49 4f 4e 3a 0a  K_AGG_FUNCTION:.
1d626 20 20 20 20 63 61 73 65 20 54 4b 5f 43 4f 4e 53      case TK_CONS
1d627 54 5f 46 55 4e 43 3a 20 7b 0a 20 20 20 20 20 20  T_FUNC: {.      
1d628 2f 2a 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73  /* The arguments
1d629 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 68   to a function h
1d62a 61 76 65 20 61 20 66 69 78 65 64 20 64 65 73 74  ave a fixed dest
1d62b 69 6e 61 74 69 6f 6e 2e 0a 20 20 20 20 20 20 2a  ination..      *
1d62c 2a 20 4d 61 72 6b 20 74 68 65 6d 20 74 68 69 73  * Mark them this
1d62d 20 77 61 79 20 74 6f 20 61 76 6f 69 64 20 67 65   way to avoid ge
1d62e 6e 65 72 61 74 65 64 20 75 6e 6e 65 65 64 65 64  nerated unneeded
1d62f 20 4f 50 5f 53 43 6f 70 79 0a 20 20 20 20 20 20   OP_SCopy.      
1d630 2a 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 73 2e  ** instructions.
1d631 20 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20   .      */.     
1d632 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
1d633 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b   = pExpr->pList;
1d634 0a 20 20 20 20 20 20 69 66 28 20 70 4c 69 73 74  .      if( pList
1d635 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
1d636 69 20 3d 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  i = pList->nExpr
1d637 3b 0a 20 20 20 20 20 20 20 20 73 74 72 75 63 74  ;.        struct
1d638 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a   ExprList_item *
1d639 70 49 74 65 6d 20 3d 20 70 4c 69 73 74 2d 3e 61  pItem = pList->a
1d63a 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 3b 20  ;.        for(; 
1d63b 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d 2b  i>0; i--, pItem+
1d63c 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  +){.          if
1d63d 28 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 20 29  ( pItem->pExpr )
1d63e 20 70 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 66   pItem->pExpr->f
1d63f 6c 61 67 73 20 7c 3d 20 45 50 5f 46 69 78 65 64  lags |= EP_Fixed
1d640 44 65 73 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a  Dest;.        }.
1d641 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62 72        }.      br
1d642 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
1d643 20 69 66 28 20 69 73 41 70 70 72 6f 70 72 69 61   if( isAppropria
1d644 74 65 46 6f 72 46 61 63 74 6f 72 69 6e 67 28 70  teForFactoring(p
1d645 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Expr) ){.    int
1d646 20 72 31 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e   r1 = ++pParse->
1d647 6e 4d 65 6d 3b 0a 20 20 20 20 69 6e 74 20 72 32  nMem;.    int r2
1d648 3b 0a 20 20 20 20 72 32 20 3d 20 73 71 6c 69 74  ;.    r2 = sqlit
1d649 65 33 45 78 70 72 43 6f 64 65 54 61 72 67 65 74  e3ExprCodeTarget
1d64a 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2c 20  (pParse, pExpr, 
1d64b 72 31 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21  r1);.    if( r1!
1d64c 3d 72 32 20 29 20 73 71 6c 69 74 65 33 52 65 6c  =r2 ) sqlite3Rel
1d64d 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1d64e 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 70 45 78  se, r1);.    pEx
1d64f 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 52 45 47 49  pr->op = TK_REGI
1d650 53 54 45 52 3b 0a 20 20 20 20 70 45 78 70 72 2d  STER;.    pExpr-
1d651 3e 69 54 61 62 6c 65 20 3d 20 72 32 3b 0a 20 20  >iTable = r2;.  
1d652 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
1d653 6e 65 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  ne;.  }.  return
1d654 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d   WRC_Continue;.}
1d655 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 65 76 61 6c 75  ../*.** Preevalu
1d656 61 74 65 20 63 6f 6e 73 74 61 6e 74 20 73 75 62  ate constant sub
1d657 65 78 70 72 65 73 73 69 6f 6e 73 20 77 69 74 68  expressions with
1d658 69 6e 20 70 45 78 70 72 20 61 6e 64 20 73 74 6f  in pExpr and sto
1d659 72 65 20 74 68 65 0a 2a 2a 20 72 65 73 75 6c 74  re the.** result
1d65a 73 20 69 6e 20 72 65 67 69 73 74 65 72 73 2e 20  s in registers. 
1d65b 20 4d 6f 64 69 66 79 20 70 45 78 70 72 20 73 6f   Modify pExpr so
1d65c 20 74 68 61 74 20 74 68 65 20 63 6f 6e 73 74 61   that the consta
1d65d 6e 74 20 73 75 62 65 78 70 72 65 73 69 6f 6e 73  nt subexpresions
1d65e 0a 2a 2a 20 61 72 65 20 54 4b 5f 52 45 47 49 53  .** are TK_REGIS
1d65f 54 45 52 20 6f 70 63 6f 64 65 73 20 74 68 61 74  TER opcodes that
1d660 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 70 72   refer to the pr
1d661 65 63 6f 6d 70 75 74 65 64 20 76 61 6c 75 65 73  ecomputed values
1d662 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1d663 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1d664 45 78 70 72 43 6f 64 65 43 6f 6e 73 74 61 6e 74  ExprCodeConstant
1d665 73 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  s(Parse *pParse,
1d666 20 45 78 70 72 20 2a 70 45 78 70 72 29 7b 0a 20   Expr *pExpr){. 
1d667 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e 78   Walker w;.  w.x
1d668 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65  ExprCallback = e
1d669 76 61 6c 43 6f 6e 73 74 45 78 70 72 3b 0a 20 20  valConstExpr;.  
1d66a 77 2e 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63  w.xSelectCallbac
1d66b 6b 20 3d 20 30 3b 0a 20 20 77 2e 70 50 61 72 73  k = 0;.  w.pPars
1d66c 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 71  e = pParse;.  sq
1d66d 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28 26 77  lite3WalkExpr(&w
1d66e 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 0a 2f 2a  , pExpr);.}.../*
1d66f 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
1d670 65 20 74 68 61 74 20 70 75 73 68 65 73 20 74 68  e that pushes th
1d671 65 20 76 61 6c 75 65 20 6f 66 20 65 76 65 72 79  e value of every
1d672 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65 20   element of the 
1d673 67 69 76 65 6e 0a 2a 2a 20 65 78 70 72 65 73 73  given.** express
1d674 69 6f 6e 20 6c 69 73 74 20 69 6e 74 6f 20 61 20  ion list into a 
1d675 73 65 71 75 65 6e 63 65 20 6f 66 20 72 65 67 69  sequence of regi
1d676 73 74 65 72 73 20 62 65 67 69 6e 6e 69 6e 67 20  sters beginning 
1d677 61 74 20 74 61 72 67 65 74 2e 0a 2a 2a 0a 2a 2a  at target..**.**
1d678 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62   Return the numb
1d679 65 72 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 65  er of elements e
1d67a 76 61 6c 75 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c  valuated..*/.SQL
1d67b 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
1d67c 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45  sqlite3ExprCodeE
1d67d 78 70 72 4c 69 73 74 28 0a 20 20 50 61 72 73 65  xprList(.  Parse
1d67e 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f 2a   *pParse,     /*
1d67f 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
1d680 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
1d681 70 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20  pList,   /* The 
1d682 65 78 70 72 65 73 73 69 6f 6e 20 6c 69 73 74 20  expression list 
1d683 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a 20  to be coded */. 
1d684 20 69 6e 74 20 74 61 72 67 65 74 2c 20 20 20 20   int target,    
1d685 20 20 20 20 2f 2a 20 57 68 65 72 65 20 74 6f 20      /* Where to 
1d686 77 72 69 74 65 20 72 65 73 75 6c 74 73 20 2a 2f  write results */
1d687 0a 20 20 69 6e 74 20 64 6f 48 61 72 64 43 6f 70  .  int doHardCop
1d688 79 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 61 20  y     /* Make a 
1d689 68 61 72 64 20 63 6f 70 79 20 6f 66 20 65 76 65  hard copy of eve
1d68a 72 79 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 29 7b  ry element */.){
1d68b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c 69  .  struct ExprLi
1d68c 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a  st_item *pItem;.
1d68d 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 61 73    int i, n;.  as
1d68e 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20 29  sert( pList!=0 )
1d68f 3b 0a 20 20 61 73 73 65 72 74 28 20 74 61 72 67  ;.  assert( targ
1d690 65 74 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 70 4c  et>0 );.  n = pL
1d691 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 66 6f  ist->nExpr;.  fo
1d692 72 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61  r(pItem=pList->a
1d693 2c 20 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b 2c  , i=0; i<n; i++,
1d694 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 69   pItem++){.    i
1d695 66 28 20 70 49 74 65 6d 2d 3e 69 41 6c 69 61 73  f( pItem->iAlias
1d696 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 52   ){.      int iR
1d697 65 67 20 3d 20 63 6f 64 65 41 6c 69 61 73 28 70  eg = codeAlias(p
1d698 50 61 72 73 65 2c 20 70 49 74 65 6d 2d 3e 69 41  Parse, pItem->iA
1d699 6c 69 61 73 2c 20 70 49 74 65 6d 2d 3e 70 45 78  lias, pItem->pEx
1d69a 70 72 2c 20 74 61 72 67 65 74 2b 69 29 3b 0a 20  pr, target+i);. 
1d69b 20 20 20 20 20 56 64 62 65 20 2a 76 20 3d 20 73       Vdbe *v = s
1d69c 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
1d69d 61 72 73 65 29 3b 0a 20 20 20 20 20 20 69 66 28  arse);.      if(
1d69e 20 69 52 65 67 21 3d 74 61 72 67 65 74 2b 69 20   iReg!=target+i 
1d69f 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
1d6a0 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1d6a1 4f 50 5f 53 43 6f 70 79 2c 20 69 52 65 67 2c 20  OP_SCopy, iReg, 
1d6a2 74 61 72 67 65 74 2b 69 29 3b 0a 20 20 20 20 20  target+i);.     
1d6a3 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
1d6a4 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
1d6a5 6f 64 65 28 70 50 61 72 73 65 2c 20 70 49 74 65  ode(pParse, pIte
1d6a6 6d 2d 3e 70 45 78 70 72 2c 20 74 61 72 67 65 74  m->pExpr, target
1d6a7 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  +i);.    }.    i
1d6a8 66 28 20 64 6f 48 61 72 64 43 6f 70 79 20 29 7b  f( doHardCopy ){
1d6a9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1d6aa 70 72 48 61 72 64 43 6f 70 79 28 70 50 61 72 73  prHardCopy(pPars
1d6ab 65 2c 20 74 61 72 67 65 74 2c 20 6e 29 3b 0a 20  e, target, n);. 
1d6ac 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1d6ad 6e 20 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n n;.}../*.** Ge
1d6ae 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20  nerate code for 
1d6af 61 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73  a boolean expres
1d6b0 73 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61  sion such that a
1d6b1 20 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a   jump is made.**
1d6b2 20 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64   to the label "d
1d6b3 65 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72  est" if the expr
1d6b4 65 73 73 69 6f 6e 20 69 73 20 74 72 75 65 20 62  ession is true b
1d6b5 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  ut execution.** 
1d6b6 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67  continues straig
1d6b7 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65  ht thru if the e
1d6b8 78 70 72 65 73 73 69 6f 6e 20 69 73 20 66 61 6c  xpression is fal
1d6b9 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65  se..**.** If the
1d6ba 20 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c   expression eval
1d6bb 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e  uates to NULL (n
1d6bc 65 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20  either true nor 
1d6bd 66 61 6c 73 65 29 2c 20 74 68 65 6e 0a 2a 2a 20  false), then.** 
1d6be 74 61 6b 65 20 74 68 65 20 6a 75 6d 70 20 69 66  take the jump if
1d6bf 20 74 68 65 20 6a 75 6d 70 49 66 4e 75 6c 6c 20   the jumpIfNull 
1d6c0 66 6c 61 67 20 69 73 20 53 51 4c 49 54 45 5f 4a  flag is SQLITE_J
1d6c1 55 4d 50 49 46 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a  UMPIFNULL..**.**
1d6c2 20 54 68 69 73 20 63 6f 64 65 20 64 65 70 65 6e   This code depen
1d6c3 64 73 20 6f 6e 20 74 68 65 20 66 61 63 74 20 74  ds on the fact t
1d6c4 68 61 74 20 63 65 72 74 61 69 6e 20 74 6f 6b 65  hat certain toke
1d6c5 6e 20 76 61 6c 75 65 73 20 28 65 78 3a 20 54 4b  n values (ex: TK
1d6c6 5f 45 51 29 0a 2a 2a 20 61 72 65 20 74 68 65 20  _EQ).** are the 
1d6c7 73 61 6d 65 20 61 73 20 6f 70 63 6f 64 65 20 76  same as opcode v
1d6c8 61 6c 75 65 73 20 28 65 78 3a 20 4f 50 5f 45 71  alues (ex: OP_Eq
1d6c9 29 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74  ) that implement
1d6ca 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
1d6cb 6e 67 0a 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 2e  ng.** operation.
1d6cc 20 20 53 70 65 63 69 61 6c 20 63 6f 6d 6d 65 6e    Special commen
1d6cd 74 73 20 69 6e 20 76 64 62 65 2e 63 20 61 6e 64  ts in vdbe.c and
1d6ce 20 74 68 65 20 6d 6b 6f 70 63 6f 64 65 68 2e 61   the mkopcodeh.a
1d6cf 77 6b 20 73 63 72 69 70 74 20 69 6e 0a 2a 2a 20  wk script in.** 
1d6d0 74 68 65 20 6d 61 6b 65 20 70 72 6f 63 65 73 73  the make process
1d6d1 20 63 61 75 73 65 20 74 68 65 73 65 20 76 61 6c   cause these val
1d6d2 75 65 73 20 74 6f 20 61 6c 69 67 6e 2e 20 20 41  ues to align.  A
1d6d3 73 73 65 72 74 28 29 73 20 69 6e 20 74 68 65 20  ssert()s in the 
1d6d4 63 6f 64 65 0a 2a 2a 20 62 65 6c 6f 77 20 76 65  code.** below ve
1d6d5 72 69 66 79 20 74 68 61 74 20 74 68 65 20 6e 75  rify that the nu
1d6d6 6d 62 65 72 73 20 61 72 65 20 61 6c 69 67 6e 65  mbers are aligne
1d6d7 64 20 63 6f 72 72 65 63 74 6c 79 2e 0a 2a 2f 0a  d correctly..*/.
1d6d8 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1d6d9 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
1d6da 66 54 72 75 65 28 50 61 72 73 65 20 2a 70 50 61  fTrue(Parse *pPa
1d6db 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70 72  rse, Expr *pExpr
1d6dc 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74 20  , int dest, int 
1d6dd 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20 56  jumpIfNull){.  V
1d6de 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65 2d  dbe *v = pParse-
1d6df 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f 70  >pVdbe;.  int op
1d6e0 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67 46   = 0;.  int regF
1d6e1 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74 20  ree1 = 0;.  int 
1d6e2 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20 20  regFree2 = 0;.  
1d6e3 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20 61  int r1, r2;..  a
1d6e4 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75 6c  ssert( jumpIfNul
1d6e5 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  l==SQLITE_JUMPIF
1d6e6 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e 75  NULL || jumpIfNu
1d6e7 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20 76  ll==0 );.  if( v
1d6e8 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30 20  ==0 || pExpr==0 
1d6e9 29 20 72 65 74 75 72 6e 3b 0a 20 20 6f 70 20 3d  ) return;.  op =
1d6ea 20 70 45 78 70 72 2d 3e 6f 70 3b 0a 20 20 73 77   pExpr->op;.  sw
1d6eb 69 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20  itch( op ){.    
1d6ec 63 61 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20  case TK_AND: {. 
1d6ed 20 20 20 20 20 69 6e 74 20 64 32 20 3d 20 73 71       int d2 = sq
1d6ee 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1d6ef 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 74 65 73  el(v);.      tes
1d6f0 74 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c  tcase( jumpIfNul
1d6f1 6c 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65  l==0 );.      te
1d6f2 73 74 63 61 73 65 28 20 70 50 61 72 73 65 2d 3e  stcase( pParse->
1d6f3 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3d  disableColCache=
1d6f4 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  =0 );.      sqli
1d6f5 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28 70  te3ExprIfFalse(p
1d6f6 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1d6f7 65 66 74 2c 20 64 32 2c 6a 75 6d 70 49 66 4e 75  eft, d2,jumpIfNu
1d6f8 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  ll^SQLITE_JUMPIF
1d6f9 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50 61  NULL);.      pPa
1d6fa 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1d6fb 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 73 71  ache++;.      sq
1d6fc 6c 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28  lite3ExprIfTrue(
1d6fd 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1d6fe 52 69 67 68 74 2c 20 64 65 73 74 2c 20 6a 75 6d  Right, dest, jum
1d6ff 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20  pIfNull);.      
1d700 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
1d701 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3e  disableColCache>
1d702 30 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  0 );.      pPars
1d703 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
1d704 68 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69  he--;.      sqli
1d705 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
1d706 62 65 6c 28 76 2c 20 64 32 29 3b 0a 20 20 20 20  bel(v, d2);.    
1d707 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
1d708 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a 20 7b     case TK_OR: {
1d709 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1d70a 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29   jumpIfNull==0 )
1d70b 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1d70c 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1d70d 65 43 6f 6c 43 61 63 68 65 3d 3d 30 20 29 3b 0a  eColCache==0 );.
1d70e 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1d70f 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
1d710 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65  pExpr->pLeft, de
1d711 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b  st, jumpIfNull);
1d712 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64  .      pParse->d
1d713 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b  isableColCache++
1d714 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45  ;.      sqlite3E
1d715 78 70 72 49 66 54 72 75 65 28 70 50 61 72 73 65  xprIfTrue(pParse
1d716 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c  , pExpr->pRight,
1d717 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c   dest, jumpIfNul
1d718 6c 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  l);.      assert
1d719 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
1d71a 65 43 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20  eColCache>0 );. 
1d71b 20 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73       pParse->dis
1d71c 61 62 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a  ableColCache--;.
1d71d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
1d71e 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e   }.    case TK_N
1d71f 4f 54 3a 20 7b 0a 20 20 20 20 20 20 74 65 73 74  OT: {.      test
1d720 63 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c  case( jumpIfNull
1d721 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
1d722 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
1d723 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1d724 4c 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70  Left, dest, jump
1d725 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62  IfNull);.      b
1d726 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1d727 63 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20  case TK_LT:.    
1d728 63 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20  case TK_LE:.    
1d729 63 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20  case TK_GT:.    
1d72a 63 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20  case TK_GE:.    
1d72b 63 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20  case TK_NE:.    
1d72c 63 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20  case TK_EQ: {.  
1d72d 20 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c      assert( TK_L
1d72e 54 3d 3d 4f 50 5f 4c 74 20 29 3b 0a 20 20 20 20  T==OP_Lt );.    
1d72f 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 45 3d    assert( TK_LE=
1d730 3d 4f 50 5f 4c 65 20 29 3b 0a 20 20 20 20 20 20  =OP_Le );.      
1d731 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3d 3d 4f  assert( TK_GT==O
1d732 50 5f 47 74 20 29 3b 0a 20 20 20 20 20 20 61 73  P_Gt );.      as
1d733 73 65 72 74 28 20 54 4b 5f 47 45 3d 3d 4f 50 5f  sert( TK_GE==OP_
1d734 47 65 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  Ge );.      asse
1d735 72 74 28 20 54 4b 5f 45 51 3d 3d 4f 50 5f 45 71  rt( TK_EQ==OP_Eq
1d736 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   );.      assert
1d737 28 20 54 4b 5f 4e 45 3d 3d 4f 50 5f 4e 65 20 29  ( TK_NE==OP_Ne )
1d738 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1d739 28 20 6f 70 3d 3d 54 4b 5f 4c 54 20 29 3b 0a 20  ( op==TK_LT );. 
1d73a 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f       testcase( o
1d73b 70 3d 3d 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20  p==TK_LE );.    
1d73c 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
1d73d 54 4b 5f 47 54 20 29 3b 0a 20 20 20 20 20 20 74  TK_GT );.      t
1d73e 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f  estcase( op==TK_
1d73f 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  GE );.      test
1d740 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 45 51 20  case( op==TK_EQ 
1d741 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1d742 65 28 20 6f 70 3d 3d 54 4b 5f 4e 45 20 29 3b 0a  e( op==TK_NE );.
1d743 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1d744 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b  jumpIfNull==0 );
1d745 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d 70 61  .      codeCompa
1d746 72 65 4f 70 65 72 61 6e 64 73 28 70 50 61 72 73  reOperands(pPars
1d747 65 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2c  e, pExpr->pLeft,
1d748 20 26 72 31 2c 20 26 72 65 67 46 72 65 65 31 2c   &r1, &regFree1,
1d749 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1d74a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d74b 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74     pExpr->pRight
1d74c 2c 20 26 72 32 2c 20 26 72 65 67 46 72 65 65 32  , &r2, &regFree2
1d74d 29 3b 0a 20 20 20 20 20 20 63 6f 64 65 43 6f 6d  );.      codeCom
1d74e 70 61 72 65 28 70 50 61 72 73 65 2c 20 70 45 78  pare(pParse, pEx
1d74f 70 72 2d 3e 70 4c 65 66 74 2c 20 70 45 78 70 72  pr->pLeft, pExpr
1d750 2d 3e 70 52 69 67 68 74 2c 20 6f 70 2c 0a 20 20  ->pRight, op,.  
1d751 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d752 72 31 2c 20 72 32 2c 20 64 65 73 74 2c 20 6a 75  r1, r2, dest, ju
1d753 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
1d754 20 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72   testcase( regFr
1d755 65 65 31 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  ee1==0 );.      
1d756 74 65 73 74 63 61 73 65 28 20 72 65 67 46 72 65  testcase( regFre
1d757 65 32 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 62  e2==0 );.      b
1d758 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
1d759 63 61 73 65 20 54 4b 5f 49 53 4e 55 4c 4c 3a 0a  case TK_ISNULL:.
1d75a 20 20 20 20 63 61 73 65 20 54 4b 5f 4e 4f 54 4e      case TK_NOTN
1d75b 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20 61 73 73  ULL: {.      ass
1d75c 65 72 74 28 20 54 4b 5f 49 53 4e 55 4c 4c 3d 3d  ert( TK_ISNULL==
1d75d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a 20 20 20  OP_IsNull );.   
1d75e 20 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4e 4f     assert( TK_NO
1d75f 54 4e 55 4c 4c 3d 3d 4f 50 5f 4e 6f 74 4e 75 6c  TNULL==OP_NotNul
1d760 6c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  l );.      testc
1d761 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55  ase( op==TK_ISNU
1d762 4c 4c 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  LL );.      test
1d763 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54  case( op==TK_NOT
1d764 4e 55 4c 4c 20 29 3b 0a 20 20 20 20 20 20 72 31  NULL );.      r1
1d765 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
1d766 64 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 70  deTemp(pParse, p
1d767 45 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65  Expr->pLeft, &re
1d768 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
1d769 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1d76a 28 76 2c 20 6f 70 2c 20 72 31 2c 20 64 65 73 74  (v, op, r1, dest
1d76b 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1d76c 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29  e( regFree1==0 )
1d76d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
1d76e 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b     }.    case TK
1d76f 5f 42 45 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20  _BETWEEN: {.    
1d770 20 20 2f 2a 20 20 20 20 78 20 42 45 54 57 45 45    /*    x BETWEE
1d771 4e 20 79 20 41 4e 44 20 7a 0a 20 20 20 20 20 20  N y AND z.      
1d772 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 49 73 20 65  **.      ** Is e
1d773 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20  quivalent to .  
1d774 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1d775 20 20 20 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a     x>=y AND x<=z
1d776 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20  .      **.      
1d777 2a 2a 20 43 6f 64 65 20 69 74 20 61 73 20 73 75  ** Code it as su
1d778 63 68 2c 20 74 61 6b 69 6e 67 20 63 61 72 65 20  ch, taking care 
1d779 74 6f 20 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e  to do the common
1d77a 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 0a 20   subexpression. 
1d77b 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61       ** elementa
1d77c 74 69 6f 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20  tion of x..     
1d77d 20 2a 2f 0a 20 20 20 20 20 20 45 78 70 72 20 65   */.      Expr e
1d77e 78 70 72 41 6e 64 3b 0a 20 20 20 20 20 20 45 78  xprAnd;.      Ex
1d77f 70 72 20 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20  pr compLeft;.   
1d780 20 20 20 45 78 70 72 20 63 6f 6d 70 52 69 67 68     Expr compRigh
1d781 74 3b 0a 20 20 20 20 20 20 45 78 70 72 20 65 78  t;.      Expr ex
1d782 70 72 58 3b 0a 0a 20 20 20 20 20 20 65 78 70 72  prX;..      expr
1d783 58 20 3d 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66  X = *pExpr->pLef
1d784 74 3b 0a 20 20 20 20 20 20 65 78 70 72 41 6e 64  t;.      exprAnd
1d785 2e 6f 70 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20  .op = TK_AND;.  
1d786 20 20 20 20 65 78 70 72 41 6e 64 2e 70 4c 65 66      exprAnd.pLef
1d787 74 20 3d 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20  t = &compLeft;. 
1d788 20 20 20 20 20 65 78 70 72 41 6e 64 2e 70 52 69       exprAnd.pRi
1d789 67 68 74 20 3d 20 26 63 6f 6d 70 52 69 67 68 74  ght = &compRight
1d78a 3b 0a 20 20 20 20 20 20 63 6f 6d 70 4c 65 66 74  ;.      compLeft
1d78b 2e 6f 70 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20  .op = TK_GE;.   
1d78c 20 20 20 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66     compLeft.pLef
1d78d 74 20 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20  t = &exprX;.    
1d78e 20 20 63 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68    compLeft.pRigh
1d78f 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74  t = pExpr->pList
1d790 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
1d791 20 20 20 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70      compRight.op
1d792 20 3d 20 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20   = TK_LE;.      
1d793 63 6f 6d 70 52 69 67 68 74 2e 70 4c 65 66 74 20  compRight.pLeft 
1d794 3d 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20  = &exprX;.      
1d795 63 6f 6d 70 52 69 67 68 74 2e 70 52 69 67 68 74  compRight.pRight
1d796 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d   = pExpr->pList-
1d797 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20  >a[1].pExpr;.   
1d798 20 20 20 65 78 70 72 58 2e 69 54 61 62 6c 65 20     exprX.iTable 
1d799 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64  = sqlite3ExprCod
1d79a 65 54 65 6d 70 28 70 50 61 72 73 65 2c 20 26 65  eTemp(pParse, &e
1d79b 78 70 72 58 2c 20 26 72 65 67 46 72 65 65 31 29  xprX, &regFree1)
1d79c 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  ;.      testcase
1d79d 28 20 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b  ( regFree1==0 );
1d79e 0a 20 20 20 20 20 20 65 78 70 72 58 2e 6f 70 20  .      exprX.op 
1d79f 3d 20 54 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20  = TK_REGISTER;. 
1d7a0 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6a       testcase( j
1d7a1 75 6d 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a  umpIfNull==0 );.
1d7a2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
1d7a3 72 49 66 54 72 75 65 28 70 50 61 72 73 65 2c 20  rIfTrue(pParse, 
1d7a4 26 65 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20  &exprAnd, dest, 
1d7a5 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20  jumpIfNull);.   
1d7a6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
1d7a7 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20      default: {. 
1d7a8 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65       r1 = sqlite
1d7a9 33 45 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50  3ExprCodeTemp(pP
1d7aa 61 72 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65  arse, pExpr, &re
1d7ab 67 46 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73  gFree1);.      s
1d7ac 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1d7ad 28 76 2c 20 4f 50 5f 49 66 2c 20 72 31 2c 20 64  (v, OP_If, r1, d
1d7ae 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 21  est, jumpIfNull!
1d7af 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  =0);.      testc
1d7b0 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d 30  ase( regFree1==0
1d7b1 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1d7b2 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d 3d  se( jumpIfNull==
1d7b3 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  0 );.      break
1d7b4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71  ;.    }.  }.  sq
1d7b5 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1d7b6 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 46  Reg(pParse, regF
1d7b7 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65 33  ree1);.  sqlite3
1d7b8 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
1d7b9 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32 29  Parse, regFree2)
1d7ba 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  ;  .}../*.** Gen
1d7bb 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
1d7bc 20 62 6f 6f 6c 65 61 6e 20 65 78 70 72 65 73 73   boolean express
1d7bd 69 6f 6e 20 73 75 63 68 20 74 68 61 74 20 61 20  ion such that a 
1d7be 6a 75 6d 70 20 69 73 20 6d 61 64 65 0a 2a 2a 20  jump is made.** 
1d7bf 74 6f 20 74 68 65 20 6c 61 62 65 6c 20 22 64 65  to the label "de
1d7c0 73 74 22 20 69 66 20 74 68 65 20 65 78 70 72 65  st" if the expre
1d7c1 73 73 69 6f 6e 20 69 73 20 66 61 6c 73 65 20 62  ssion is false b
1d7c2 75 74 20 65 78 65 63 75 74 69 6f 6e 0a 2a 2a 20  ut execution.** 
1d7c3 63 6f 6e 74 69 6e 75 65 73 20 73 74 72 61 69 67  continues straig
1d7c4 68 74 20 74 68 72 75 20 69 66 20 74 68 65 20 65  ht thru if the e
1d7c5 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 72 75  xpression is tru
1d7c6 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
1d7c7 65 78 70 72 65 73 73 69 6f 6e 20 65 76 61 6c 75  expression evalu
1d7c8 61 74 65 73 20 74 6f 20 4e 55 4c 4c 20 28 6e 65  ates to NULL (ne
1d7c9 69 74 68 65 72 20 74 72 75 65 20 6e 6f 72 20 66  ither true nor f
1d7ca 61 6c 73 65 29 20 74 68 65 6e 0a 2a 2a 20 6a 75  alse) then.** ju
1d7cb 6d 70 20 69 66 20 6a 75 6d 70 49 66 4e 75 6c 6c  mp if jumpIfNull
1d7cc 20 69 73 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49   is SQLITE_JUMPI
1d7cd 46 4e 55 4c 4c 20 6f 72 20 66 61 6c 6c 20 74 68  FNULL or fall th
1d7ce 72 6f 75 67 68 20 69 66 20 6a 75 6d 70 49 66 4e  rough if jumpIfN
1d7cf 75 6c 6c 0a 2a 2a 20 69 73 20 30 2e 0a 2a 2f 0a  ull.** is 0..*/.
1d7d0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1d7d1 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 49  oid sqlite3ExprI
1d7d2 66 46 61 6c 73 65 28 50 61 72 73 65 20 2a 70 50  fFalse(Parse *pP
1d7d3 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78 70  arse, Expr *pExp
1d7d4 72 2c 20 69 6e 74 20 64 65 73 74 2c 20 69 6e 74  r, int dest, int
1d7d5 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 7b 0a 20 20   jumpIfNull){.  
1d7d6 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
1d7d7 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20 6f  ->pVdbe;.  int o
1d7d8 70 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 65 67  p = 0;.  int reg
1d7d9 46 72 65 65 31 20 3d 20 30 3b 0a 20 20 69 6e 74  Free1 = 0;.  int
1d7da 20 72 65 67 46 72 65 65 32 20 3d 20 30 3b 0a 20   regFree2 = 0;. 
1d7db 20 69 6e 74 20 72 31 2c 20 72 32 3b 0a 0a 20 20   int r1, r2;..  
1d7dc 61 73 73 65 72 74 28 20 6a 75 6d 70 49 66 4e 75  assert( jumpIfNu
1d7dd 6c 6c 3d 3d 53 51 4c 49 54 45 5f 4a 55 4d 50 49  ll==SQLITE_JUMPI
1d7de 46 4e 55 4c 4c 20 7c 7c 20 6a 75 6d 70 49 66 4e  FNULL || jumpIfN
1d7df 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 69 66 28 20  ull==0 );.  if( 
1d7e0 76 3d 3d 30 20 7c 7c 20 70 45 78 70 72 3d 3d 30  v==0 || pExpr==0
1d7e1 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 2f 2a   ) return;..  /*
1d7e2 20 54 68 65 20 76 61 6c 75 65 20 6f 66 20 70 45   The value of pE
1d7e3 78 70 72 2d 3e 6f 70 20 61 6e 64 20 6f 70 20 61  xpr->op and op a
1d7e4 72 65 20 72 65 6c 61 74 65 64 20 61 73 20 66 6f  re related as fo
1d7e5 6c 6c 6f 77 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  llows:.  **.  **
1d7e6 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 6f 70         pExpr->op
1d7e7 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 0a 20              op. 
1d7e8 20 2a 2a 20 20 20 20 20 20 20 2d 2d 2d 2d 2d 2d   **       ------
1d7e9 2d 2d 2d 20 20 20 20 20 20 20 20 20 20 2d 2d 2d  ---          ---
1d7ea 2d 2d 2d 2d 2d 2d 2d 0a 20 20 2a 2a 20 20 20 20  -------.  **    
1d7eb 20 20 20 54 4b 5f 49 53 4e 55 4c 4c 20 20 20 20     TK_ISNULL    
1d7ec 20 20 20 20 20 20 4f 50 5f 4e 6f 74 4e 75 6c 6c        OP_NotNull
1d7ed 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 4e  .  **       TK_N
1d7ee 4f 54 4e 55 4c 4c 20 20 20 20 20 20 20 20 20 4f  OTNULL         O
1d7ef 50 5f 49 73 4e 75 6c 6c 0a 20 20 2a 2a 20 20 20  P_IsNull.  **   
1d7f0 20 20 20 20 54 4b 5f 4e 45 20 20 20 20 20 20 20      TK_NE       
1d7f1 20 20 20 20 20 20 20 4f 50 5f 45 71 0a 20 20 2a         OP_Eq.  *
1d7f2 2a 20 20 20 20 20 20 20 54 4b 5f 45 51 20 20 20  *       TK_EQ   
1d7f3 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 4e 65             OP_Ne
1d7f4 0a 20 20 2a 2a 20 20 20 20 20 20 20 54 4b 5f 47  .  **       TK_G
1d7f5 54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 4f  T              O
1d7f6 50 5f 4c 65 0a 20 20 2a 2a 20 20 20 20 20 20 20  P_Le.  **       
1d7f7 54 4b 5f 4c 45 20 20 20 20 20 20 20 20 20 20 20  TK_LE           
1d7f8 20 20 20 4f 50 5f 47 74 0a 20 20 2a 2a 20 20 20     OP_Gt.  **   
1d7f9 20 20 20 20 54 4b 5f 47 45 20 20 20 20 20 20 20      TK_GE       
1d7fa 20 20 20 20 20 20 20 4f 50 5f 4c 74 0a 20 20 2a         OP_Lt.  *
1d7fb 2a 20 20 20 20 20 20 20 54 4b 5f 4c 54 20 20 20  *       TK_LT   
1d7fc 20 20 20 20 20 20 20 20 20 20 20 4f 50 5f 47 65             OP_Ge
1d7fd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 46 6f 72 20 6f  .  **.  ** For o
1d7fe 74 68 65 72 20 76 61 6c 75 65 73 20 6f 66 20 70  ther values of p
1d7ff 45 78 70 72 2d 3e 6f 70 2c 20 6f 70 20 69 73 20  Expr->op, op is 
1d800 75 6e 64 65 66 69 6e 65 64 20 61 6e 64 20 75 6e  undefined and un
1d801 75 73 65 64 2e 0a 20 20 2a 2a 20 54 68 65 20 76  used..  ** The v
1d802 61 6c 75 65 20 6f 66 20 54 4b 5f 20 61 6e 64 20  alue of TK_ and 
1d803 4f 50 5f 20 63 6f 6e 73 74 61 6e 74 73 20 61 72  OP_ constants ar
1d804 65 20 61 72 72 61 6e 67 65 64 20 73 75 63 68 20  e arranged such 
1d805 74 68 61 74 20 77 65 0a 20 20 2a 2a 20 63 61 6e  that we.  ** can
1d806 20 63 6f 6d 70 75 74 65 20 74 68 65 20 6d 61 70   compute the map
1d807 70 69 6e 67 20 61 62 6f 76 65 20 75 73 69 6e 67  ping above using
1d808 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65   the following e
1d809 78 70 72 65 73 73 69 6f 6e 2e 0a 20 20 2a 2a 20  xpression..  ** 
1d80a 41 73 73 65 72 74 28 29 73 20 76 65 72 69 66 79  Assert()s verify
1d80b 20 74 68 61 74 20 74 68 65 20 63 6f 6d 70 75 74   that the comput
1d80c 61 74 69 6f 6e 20 69 73 20 63 6f 72 72 65 63 74  ation is correct
1d80d 2e 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 28 28  ..  */.  op = ((
1d80e 70 45 78 70 72 2d 3e 6f 70 2b 28 54 4b 5f 49 53  pExpr->op+(TK_IS
1d80f 4e 55 4c 4c 26 31 29 29 5e 31 29 2d 28 54 4b 5f  NULL&1))^1)-(TK_
1d810 49 53 4e 55 4c 4c 26 31 29 3b 0a 0a 20 20 2f 2a  ISNULL&1);..  /*
1d811 20 56 65 72 69 66 79 20 63 6f 72 72 65 63 74 20   Verify correct 
1d812 61 6c 69 67 6e 6d 65 6e 74 20 6f 66 20 54 4b 5f  alignment of TK_
1d813 20 61 6e 64 20 4f 50 5f 20 63 6f 6e 73 74 61 6e   and OP_ constan
1d814 74 73 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74  ts.  */.  assert
1d815 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f  ( pExpr->op!=TK_
1d816 49 53 4e 55 4c 4c 20 7c 7c 20 6f 70 3d 3d 4f 50  ISNULL || op==OP
1d817 5f 4e 6f 74 4e 75 6c 6c 20 29 3b 0a 20 20 61 73  _NotNull );.  as
1d818 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
1d819 3d 54 4b 5f 4e 4f 54 4e 55 4c 4c 20 7c 7c 20 6f  =TK_NOTNULL || o
1d81a 70 3d 3d 4f 50 5f 49 73 4e 75 6c 6c 20 29 3b 0a  p==OP_IsNull );.
1d81b 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d    assert( pExpr-
1d81c 3e 6f 70 21 3d 54 4b 5f 4e 45 20 7c 7c 20 6f 70  >op!=TK_NE || op
1d81d 3d 3d 4f 50 5f 45 71 20 29 3b 0a 20 20 61 73 73  ==OP_Eq );.  ass
1d81e 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21 3d  ert( pExpr->op!=
1d81f 54 4b 5f 45 51 20 7c 7c 20 6f 70 3d 3d 4f 50 5f  TK_EQ || op==OP_
1d820 4e 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Ne );.  assert( 
1d821 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 4c 54  pExpr->op!=TK_LT
1d822 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 47 65 20 29 3b   || op==OP_Ge );
1d823 0a 20 20 61 73 73 65 72 74 28 20 70 45 78 70 72  .  assert( pExpr
1d824 2d 3e 6f 70 21 3d 54 4b 5f 4c 45 20 7c 7c 20 6f  ->op!=TK_LE || o
1d825 70 3d 3d 4f 50 5f 47 74 20 29 3b 0a 20 20 61 73  p==OP_Gt );.  as
1d826 73 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 21  sert( pExpr->op!
1d827 3d 54 4b 5f 47 54 20 7c 7c 20 6f 70 3d 3d 4f 50  =TK_GT || op==OP
1d828 5f 4c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _Le );.  assert(
1d829 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 47   pExpr->op!=TK_G
1d82a 45 20 7c 7c 20 6f 70 3d 3d 4f 50 5f 4c 74 20 29  E || op==OP_Lt )
1d82b 3b 0a 0a 20 20 73 77 69 74 63 68 28 20 70 45 78  ;..  switch( pEx
1d82c 70 72 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61  pr->op ){.    ca
1d82d 73 65 20 54 4b 5f 41 4e 44 3a 20 7b 0a 20 20 20  se TK_AND: {.   
1d82e 20 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d     testcase( jum
1d82f 70 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20  pIfNull==0 );.  
1d830 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 50      testcase( pP
1d831 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
1d832 43 61 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20  Cache==0 );.    
1d833 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1d834 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1d835 70 72 2d 3e 70 4c 65 66 74 2c 20 64 65 73 74 2c  pr->pLeft, dest,
1d836 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20   jumpIfNull);.  
1d837 20 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61      pParse->disa
1d838 62 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20  bleColCache++;. 
1d839 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
1d83a 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
1d83b 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 64  pExpr->pRight, d
1d83c 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29  est, jumpIfNull)
1d83d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1d83e 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1d83f 6f 6c 43 61 63 68 65 3e 30 20 29 3b 0a 20 20 20  olCache>0 );.   
1d840 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
1d841 6c 65 43 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20  leColCache--;.  
1d842 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
1d843 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 4f 52 3a  .    case TK_OR:
1d844 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 32 20   {.      int d2 
1d845 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b  = sqlite3VdbeMak
1d846 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20  eLabel(v);.     
1d847 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1d848 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1d849 20 20 74 65 73 74 63 61 73 65 28 20 70 50 61 72    testcase( pPar
1d84a 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61  se->disableColCa
1d84b 63 68 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  che==0 );.      
1d84c 73 71 6c 69 74 65 33 45 78 70 72 49 66 54 72 75  sqlite3ExprIfTru
1d84d 65 28 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d  e(pParse, pExpr-
1d84e 3e 70 4c 65 66 74 2c 20 64 32 2c 20 6a 75 6d 70  >pLeft, d2, jump
1d84f 49 66 4e 75 6c 6c 5e 53 51 4c 49 54 45 5f 4a 55  IfNull^SQLITE_JU
1d850 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20  MPIFNULL);.     
1d851 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65   pParse->disable
1d852 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20 20 20  ColCache++;.    
1d853 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
1d854 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70 45 78  alse(pParse, pEx
1d855 70 72 2d 3e 70 52 69 67 68 74 2c 20 64 65 73 74  pr->pRight, dest
1d856 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20  , jumpIfNull);. 
1d857 20 20 20 20 20 61 73 73 65 72 74 28 20 70 50 61       assert( pPa
1d858 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
1d859 61 63 68 65 3e 30 20 29 3b 0a 20 20 20 20 20 20  ache>0 );.      
1d85a 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43  pParse->disableC
1d85b 6f 6c 43 61 63 68 65 2d 2d 3b 0a 20 20 20 20 20  olCache--;.     
1d85c 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f   sqlite3VdbeReso
1d85d 6c 76 65 4c 61 62 65 6c 28 76 2c 20 64 32 29 3b  lveLabel(v, d2);
1d85e 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
1d85f 20 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f    }.    case TK_
1d860 4e 4f 54 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  NOT: {.      sql
1d861 69 74 65 33 45 78 70 72 49 66 54 72 75 65 28 70  ite3ExprIfTrue(p
1d862 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70 4c  Parse, pExpr->pL
1d863 65 66 74 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49  eft, dest, jumpI
1d864 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20 20 62 72  fNull);.      br
1d865 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  eak;.    }.    c
1d866 61 73 65 20 54 4b 5f 4c 54 3a 0a 20 20 20 20 63  ase TK_LT:.    c
1d867 61 73 65 20 54 4b 5f 4c 45 3a 0a 20 20 20 20 63  ase TK_LE:.    c
1d868 61 73 65 20 54 4b 5f 47 54 3a 0a 20 20 20 20 63  ase TK_GT:.    c
1d869 61 73 65 20 54 4b 5f 47 45 3a 0a 20 20 20 20 63  ase TK_GE:.    c
1d86a 61 73 65 20 54 4b 5f 4e 45 3a 0a 20 20 20 20 63  ase TK_NE:.    c
1d86b 61 73 65 20 54 4b 5f 45 51 3a 20 7b 0a 20 20 20  ase TK_EQ: {.   
1d86c 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
1d86d 3d 54 4b 5f 4c 54 20 29 3b 0a 20 20 20 20 20 20  =TK_LT );.      
1d86e 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b  testcase( op==TK
1d86f 5f 4c 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73  _LE );.      tes
1d870 74 63 61 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 54  tcase( op==TK_GT
1d871 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
1d872 73 65 28 20 6f 70 3d 3d 54 4b 5f 47 45 20 29 3b  se( op==TK_GE );
1d873 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
1d874 20 6f 70 3d 3d 54 4b 5f 45 51 20 29 3b 0a 20 20   op==TK_EQ );.  
1d875 20 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70      testcase( op
1d876 3d 3d 54 4b 5f 4e 45 20 29 3b 0a 20 20 20 20 20  ==TK_NE );.     
1d877 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70 49   testcase( jumpI
1d878 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  fNull==0 );.    
1d879 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 4f 70 65    codeCompareOpe
1d87a 72 61 6e 64 73 28 70 50 61 72 73 65 2c 20 70 45  rands(pParse, pE
1d87b 78 70 72 2d 3e 70 4c 65 66 74 2c 20 26 72 31 2c  xpr->pLeft, &r1,
1d87c 20 26 72 65 67 46 72 65 65 31 2c 0a 20 20 20 20   &regFree1,.    
1d87d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1d87e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
1d87f 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 26 72 32  xpr->pRight, &r2
1d880 2c 20 26 72 65 67 46 72 65 65 32 29 3b 0a 20 20  , &regFree2);.  
1d881 20 20 20 20 63 6f 64 65 43 6f 6d 70 61 72 65 28      codeCompare(
1d882 70 50 61 72 73 65 2c 20 70 45 78 70 72 2d 3e 70  pParse, pExpr->p
1d883 4c 65 66 74 2c 20 70 45 78 70 72 2d 3e 70 52 69  Left, pExpr->pRi
1d884 67 68 74 2c 20 6f 70 2c 0a 20 20 20 20 20 20 20  ght, op,.       
1d885 20 20 20 20 20 20 20 20 20 20 20 72 31 2c 20 72             r1, r
1d886 32 2c 20 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e  2, dest, jumpIfN
1d887 75 6c 6c 29 3b 0a 20 20 20 20 20 20 74 65 73 74  ull);.      test
1d888 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1d889 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1d88a 61 73 65 28 20 72 65 67 46 72 65 65 32 3d 3d 30  ase( regFree2==0
1d88b 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   );.      break;
1d88c 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
1d88d 54 4b 5f 49 53 4e 55 4c 4c 3a 0a 20 20 20 20 63  TK_ISNULL:.    c
1d88e 61 73 65 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 3a 20  ase TK_NOTNULL: 
1d88f 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1d890 28 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20  ( op==TK_ISNULL 
1d891 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
1d892 65 28 20 6f 70 3d 3d 54 4b 5f 4e 4f 54 4e 55 4c  e( op==TK_NOTNUL
1d893 4c 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  L );.      r1 = 
1d894 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54  sqlite3ExprCodeT
1d895 65 6d 70 28 70 50 61 72 73 65 2c 20 70 45 78 70  emp(pParse, pExp
1d896 72 2d 3e 70 4c 65 66 74 2c 20 26 72 65 67 46 72  r->pLeft, &regFr
1d897 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  ee1);.      sqli
1d898 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1d899 20 6f 70 2c 20 72 31 2c 20 64 65 73 74 29 3b 0a   op, r1, dest);.
1d89a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
1d89b 72 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20  regFree1==0 );. 
1d89c 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1d89d 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 42 45  }.    case TK_BE
1d89e 54 57 45 45 4e 3a 20 7b 0a 20 20 20 20 20 20 2f  TWEEN: {.      /
1d89f 2a 20 20 20 20 78 20 42 45 54 57 45 45 4e 20 79  *    x BETWEEN y
1d8a0 20 41 4e 44 20 7a 0a 20 20 20 20 20 20 2a 2a 0a   AND z.      **.
1d8a1 20 20 20 20 20 20 2a 2a 20 49 73 20 65 71 75 69        ** Is equi
1d8a2 76 61 6c 65 6e 74 20 74 6f 20 0a 20 20 20 20 20  valent to .     
1d8a3 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20   **.      **    
1d8a4 78 3e 3d 79 20 41 4e 44 20 78 3c 3d 7a 0a 20 20  x>=y AND x<=z.  
1d8a5 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
1d8a6 43 6f 64 65 20 69 74 20 61 73 20 73 75 63 68 2c  Code it as such,
1d8a7 20 74 61 6b 69 6e 67 20 63 61 72 65 20 74 6f 20   taking care to 
1d8a8 64 6f 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 73 75  do the common su
1d8a9 62 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20  bexpression.    
1d8aa 20 20 2a 2a 20 65 6c 65 6d 65 6e 74 61 74 69 6f    ** elementatio
1d8ab 6e 20 6f 66 20 78 2e 0a 20 20 20 20 20 20 2a 2f  n of x..      */
1d8ac 0a 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72  .      Expr expr
1d8ad 41 6e 64 3b 0a 20 20 20 20 20 20 45 78 70 72 20  And;.      Expr 
1d8ae 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20 20 20  compLeft;.      
1d8af 45 78 70 72 20 63 6f 6d 70 52 69 67 68 74 3b 0a  Expr compRight;.
1d8b0 20 20 20 20 20 20 45 78 70 72 20 65 78 70 72 58        Expr exprX
1d8b1 3b 0a 0a 20 20 20 20 20 20 65 78 70 72 58 20 3d  ;..      exprX =
1d8b2 20 2a 70 45 78 70 72 2d 3e 70 4c 65 66 74 3b 0a   *pExpr->pLeft;.
1d8b3 20 20 20 20 20 20 65 78 70 72 41 6e 64 2e 6f 70        exprAnd.op
1d8b4 20 3d 20 54 4b 5f 41 4e 44 3b 0a 20 20 20 20 20   = TK_AND;.     
1d8b5 20 65 78 70 72 41 6e 64 2e 70 4c 65 66 74 20 3d   exprAnd.pLeft =
1d8b6 20 26 63 6f 6d 70 4c 65 66 74 3b 0a 20 20 20 20   &compLeft;.    
1d8b7 20 20 65 78 70 72 41 6e 64 2e 70 52 69 67 68 74    exprAnd.pRight
1d8b8 20 3d 20 26 63 6f 6d 70 52 69 67 68 74 3b 0a 20   = &compRight;. 
1d8b9 20 20 20 20 20 63 6f 6d 70 4c 65 66 74 2e 6f 70       compLeft.op
1d8ba 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20 20 20   = TK_GE;.      
1d8bb 63 6f 6d 70 4c 65 66 74 2e 70 4c 65 66 74 20 3d  compLeft.pLeft =
1d8bc 20 26 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63   &exprX;.      c
1d8bd 6f 6d 70 4c 65 66 74 2e 70 52 69 67 68 74 20 3d  ompLeft.pRight =
1d8be 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61   pExpr->pList->a
1d8bf 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [0].pExpr;.     
1d8c0 20 63 6f 6d 70 52 69 67 68 74 2e 6f 70 20 3d 20   compRight.op = 
1d8c1 54 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 63 6f 6d  TK_LE;.      com
1d8c2 70 52 69 67 68 74 2e 70 4c 65 66 74 20 3d 20 26  pRight.pLeft = &
1d8c3 65 78 70 72 58 3b 0a 20 20 20 20 20 20 63 6f 6d  exprX;.      com
1d8c4 70 52 69 67 68 74 2e 70 52 69 67 68 74 20 3d 20  pRight.pRight = 
1d8c5 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
1d8c6 31 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20  1].pExpr;.      
1d8c7 65 78 70 72 58 2e 69 54 61 62 6c 65 20 3d 20 73  exprX.iTable = s
1d8c8 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 54 65  qlite3ExprCodeTe
1d8c9 6d 70 28 70 50 61 72 73 65 2c 20 26 65 78 70 72  mp(pParse, &expr
1d8ca 58 2c 20 26 72 65 67 46 72 65 65 31 29 3b 0a 20  X, &regFree1);. 
1d8cb 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 72       testcase( r
1d8cc 65 67 46 72 65 65 31 3d 3d 30 20 29 3b 0a 20 20  egFree1==0 );.  
1d8cd 20 20 20 20 65 78 70 72 58 2e 6f 70 20 3d 20 54      exprX.op = T
1d8ce 4b 5f 52 45 47 49 53 54 45 52 3b 0a 20 20 20 20  K_REGISTER;.    
1d8cf 20 20 74 65 73 74 63 61 73 65 28 20 6a 75 6d 70    testcase( jump
1d8d0 49 66 4e 75 6c 6c 3d 3d 30 20 29 3b 0a 20 20 20  IfNull==0 );.   
1d8d1 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66     sqlite3ExprIf
1d8d2 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 26 65  False(pParse, &e
1d8d3 78 70 72 41 6e 64 2c 20 64 65 73 74 2c 20 6a 75  xprAnd, dest, ju
1d8d4 6d 70 49 66 4e 75 6c 6c 29 3b 0a 20 20 20 20 20  mpIfNull);.     
1d8d5 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1d8d6 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
1d8d7 20 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 45     r1 = sqlite3E
1d8d8 78 70 72 43 6f 64 65 54 65 6d 70 28 70 50 61 72  xprCodeTemp(pPar
1d8d9 73 65 2c 20 70 45 78 70 72 2c 20 26 72 65 67 46  se, pExpr, &regF
1d8da 72 65 65 31 29 3b 0a 20 20 20 20 20 20 73 71 6c  ree1);.      sql
1d8db 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1d8dc 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72 31 2c 20  , OP_IfNot, r1, 
1d8dd 64 65 73 74 2c 20 6a 75 6d 70 49 66 4e 75 6c 6c  dest, jumpIfNull
1d8de 21 3d 30 29 3b 0a 20 20 20 20 20 20 74 65 73 74  !=0);.      test
1d8df 63 61 73 65 28 20 72 65 67 46 72 65 65 31 3d 3d  case( regFree1==
1d8e0 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
1d8e1 61 73 65 28 20 6a 75 6d 70 49 66 4e 75 6c 6c 3d  ase( jumpIfNull=
1d8e2 3d 30 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61  =0 );.      brea
1d8e3 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  k;.    }.  }.  s
1d8e4 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1d8e5 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
1d8e6 46 72 65 65 31 29 3b 0a 20 20 73 71 6c 69 74 65  Free1);.  sqlite
1d8e7 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
1d8e8 70 50 61 72 73 65 2c 20 72 65 67 46 72 65 65 32  pParse, regFree2
1d8e9 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61  );.}../*.** Do a
1d8ea 20 64 65 65 70 20 63 6f 6d 70 61 72 69 73 6f 6e   deep comparison
1d8eb 20 6f 66 20 74 77 6f 20 65 78 70 72 65 73 73 69   of two expressi
1d8ec 6f 6e 20 74 72 65 65 73 2e 20 20 52 65 74 75 72  on trees.  Retur
1d8ed 6e 20 54 52 55 45 20 28 6e 6f 6e 2d 7a 65 72 6f  n TRUE (non-zero
1d8ee 29 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65  ).** if they are
1d8ef 20 69 64 65 6e 74 69 63 61 6c 20 61 6e 64 20 72   identical and r
1d8f0 65 74 75 72 6e 20 46 41 4c 53 45 20 69 66 20 74  eturn FALSE if t
1d8f1 68 65 79 20 64 69 66 66 65 72 20 69 6e 20 61 6e  hey differ in an
1d8f2 79 20 77 61 79 2e 0a 2a 2a 0a 2a 2a 20 53 6f 6d  y way..**.** Som
1d8f3 65 74 69 6d 65 73 20 74 68 69 73 20 72 6f 75 74  etimes this rout
1d8f4 69 6e 65 20 77 69 6c 6c 20 72 65 74 75 72 6e 20  ine will return 
1d8f5 46 41 4c 53 45 20 65 76 65 6e 20 69 66 20 74 68  FALSE even if th
1d8f6 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f 6e  e two expression
1d8f7 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 61 72 65 20  s.** really are 
1d8f8 65 71 75 69 76 61 6c 65 6e 74 2e 20 20 49 66 20  equivalent.  If 
1d8f9 77 65 20 63 61 6e 6e 6f 74 20 70 72 6f 76 65 20  we cannot prove 
1d8fa 74 68 61 74 20 74 68 65 20 65 78 70 72 65 73 73  that the express
1d8fb 69 6f 6e 73 20 61 72 65 0a 2a 2a 20 69 64 65 6e  ions are.** iden
1d8fc 74 69 63 61 6c 2c 20 77 65 20 72 65 74 75 72 6e  tical, we return
1d8fd 20 46 41 4c 53 45 20 6a 75 73 74 20 74 6f 20 62   FALSE just to b
1d8fe 65 20 73 61 66 65 2e 20 20 53 6f 20 69 66 20 74  e safe.  So if t
1d8ff 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 72  his routine.** r
1d900 65 74 75 72 6e 73 20 66 61 6c 73 65 2c 20 74 68  eturns false, th
1d901 65 6e 20 79 6f 75 20 64 6f 20 6e 6f 74 20 72 65  en you do not re
1d902 61 6c 6c 79 20 6b 6e 6f 77 20 66 6f 72 20 63 65  ally know for ce
1d903 72 74 61 69 6e 20 69 66 20 74 68 65 20 74 77 6f  rtain if the two
1d904 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 73 20  .** expressions 
1d905 61 72 65 20 74 68 65 20 73 61 6d 65 2e 20 20 42  are the same.  B
1d906 75 74 20 69 66 20 79 6f 75 20 67 65 74 20 61 20  ut if you get a 
1d907 54 52 55 45 20 72 65 74 75 72 6e 2c 20 74 68 65  TRUE return, the
1d908 6e 20 79 6f 75 0a 2a 2a 20 63 61 6e 20 62 65 20  n you.** can be 
1d909 73 75 72 65 20 74 68 65 20 65 78 70 72 65 73 73  sure the express
1d90a 69 6f 6e 73 20 61 72 65 20 74 68 65 20 73 61 6d  ions are the sam
1d90b 65 2e 20 20 49 6e 20 74 68 65 20 70 6c 61 63 65  e.  In the place
1d90c 73 20 77 68 65 72 65 0a 2a 2a 20 74 68 69 73 20  s where.** this 
1d90d 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 2c  routine is used,
1d90e 20 69 74 20 64 6f 65 73 20 6e 6f 74 20 68 75 72   it does not hur
1d90f 74 20 74 6f 20 67 65 74 20 61 6e 20 65 78 74 72  t to get an extr
1d910 61 20 46 41 4c 53 45 20 2d 20 74 68 61 74 0a 2a  a FALSE - that.*
1d911 2a 20 6a 75 73 74 20 6d 69 67 68 74 20 72 65 73  * just might res
1d912 75 6c 74 20 69 6e 20 73 6f 6d 65 20 73 6c 69 67  ult in some slig
1d913 68 74 6c 79 20 73 6c 6f 77 65 72 20 63 6f 64 65  htly slower code
1d914 2e 20 20 42 75 74 20 72 65 74 75 72 6e 69 6e 67  .  But returning
1d915 0a 2a 2a 20 61 6e 20 69 6e 63 6f 72 72 65 63 74  .** an incorrect
1d916 20 54 52 55 45 20 63 6f 75 6c 64 20 6c 65 61 64   TRUE could lead
1d917 20 74 6f 20 61 20 6d 61 6c 66 75 6e 63 74 69 6f   to a malfunctio
1d918 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  n..*/.SQLITE_PRI
1d919 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1d91a 45 78 70 72 43 6f 6d 70 61 72 65 28 45 78 70 72  ExprCompare(Expr
1d91b 20 2a 70 41 2c 20 45 78 70 72 20 2a 70 42 29 7b   *pA, Expr *pB){
1d91c 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1d91d 70 41 3d 3d 30 7c 7c 70 42 3d 3d 30 20 29 7b 0a  pA==0||pB==0 ){.
1d91e 20 20 20 20 72 65 74 75 72 6e 20 70 42 3d 3d 70      return pB==p
1d91f 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d  A;.  }.  if( pA-
1d920 3e 6f 70 21 3d 70 42 2d 3e 6f 70 20 29 20 72 65  >op!=pB->op ) re
1d921 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 28 70  turn 0;.  if( (p
1d922 41 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 44 69  A->flags & EP_Di
1d923 73 74 69 6e 63 74 29 21 3d 28 70 42 2d 3e 66 6c  stinct)!=(pB->fl
1d924 61 67 73 20 26 20 45 50 5f 44 69 73 74 69 6e 63  ags & EP_Distinc
1d925 74 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  t) ) return 0;. 
1d926 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
1d927 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 65  rCompare(pA->pLe
1d928 66 74 2c 20 70 42 2d 3e 70 4c 65 66 74 29 20 29  ft, pB->pLeft) )
1d929 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28   return 0;.  if(
1d92a 20 21 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6d   !sqlite3ExprCom
1d92b 70 61 72 65 28 70 41 2d 3e 70 52 69 67 68 74 2c  pare(pA->pRight,
1d92c 20 70 42 2d 3e 70 52 69 67 68 74 29 20 29 20 72   pB->pRight) ) r
1d92d 65 74 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70  eturn 0;.  if( p
1d92e 41 2d 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20  A->pList ){.    
1d92f 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 3d 3d 30  if( pB->pList==0
1d930 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
1d931 20 69 66 28 20 70 41 2d 3e 70 4c 69 73 74 2d 3e   if( pA->pList->
1d932 6e 45 78 70 72 21 3d 70 42 2d 3e 70 4c 69 73 74  nExpr!=pB->pList
1d933 2d 3e 6e 45 78 70 72 20 29 20 72 65 74 75 72 6e  ->nExpr ) return
1d934 20 30 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   0;.    for(i=0;
1d935 20 69 3c 70 41 2d 3e 70 4c 69 73 74 2d 3e 6e 45   i<pA->pList->nE
1d936 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  xpr; i++){.     
1d937 20 69 66 28 20 21 73 71 6c 69 74 65 33 45 78 70   if( !sqlite3Exp
1d938 72 43 6f 6d 70 61 72 65 28 70 41 2d 3e 70 4c 69  rCompare(pA->pLi
1d939 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 2c 20  st->a[i].pExpr, 
1d93a 70 42 2d 3e 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  pB->pList->a[i].
1d93b 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
1d93c 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
1d93d 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
1d93e 65 20 69 66 28 20 70 42 2d 3e 70 4c 69 73 74 20  e if( pB->pList 
1d93f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
1d940 0a 20 20 7d 0a 20 20 69 66 28 20 70 41 2d 3e 70  .  }.  if( pA->p
1d941 53 65 6c 65 63 74 20 7c 7c 20 70 42 2d 3e 70 53  Select || pB->pS
1d942 65 6c 65 63 74 20 29 20 72 65 74 75 72 6e 20 30  elect ) return 0
1d943 3b 0a 20 20 69 66 28 20 70 41 2d 3e 69 54 61 62  ;.  if( pA->iTab
1d944 6c 65 21 3d 70 42 2d 3e 69 54 61 62 6c 65 20 7c  le!=pB->iTable |
1d945 7c 20 70 41 2d 3e 69 43 6f 6c 75 6d 6e 21 3d 70  | pA->iColumn!=p
1d946 42 2d 3e 69 43 6f 6c 75 6d 6e 20 29 20 72 65 74  B->iColumn ) ret
1d947 75 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 41 2d  urn 0;.  if( pA-
1d948 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26  >op!=TK_COLUMN &
1d949 26 20 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 20 29 7b  & pA->token.z ){
1d94a 0a 20 20 20 20 69 66 28 20 70 42 2d 3e 74 6f 6b  .    if( pB->tok
1d94b 65 6e 2e 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  en.z==0 ) return
1d94c 20 30 3b 0a 20 20 20 20 69 66 28 20 70 42 2d 3e   0;.    if( pB->
1d94d 74 6f 6b 65 6e 2e 6e 21 3d 70 41 2d 3e 74 6f 6b  token.n!=pA->tok
1d94e 65 6e 2e 6e 20 29 20 72 65 74 75 72 6e 20 30 3b  en.n ) return 0;
1d94f 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
1d950 53 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29  StrNICmp((char*)
1d951 70 41 2d 3e 74 6f 6b 65 6e 2e 7a 2c 28 63 68 61  pA->token.z,(cha
1d952 72 2a 29 70 42 2d 3e 74 6f 6b 65 6e 2e 7a 2c 70  r*)pB->token.z,p
1d953 42 2d 3e 74 6f 6b 65 6e 2e 6e 29 21 3d 30 20 29  B->token.n)!=0 )
1d954 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30  {.      return 0
1d955 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65  ;.    }.  }.  re
1d956 74 75 72 6e 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a  turn 1;.}.../*.*
1d957 2a 20 41 64 64 20 61 20 6e 65 77 20 65 6c 65 6d  * Add a new elem
1d958 65 6e 74 20 74 6f 20 74 68 65 20 70 41 67 67 49  ent to the pAggI
1d959 6e 66 6f 2d 3e 61 43 6f 6c 5b 5d 20 61 72 72 61  nfo->aCol[] arra
1d95a 79 2e 20 20 52 65 74 75 72 6e 20 74 68 65 20 69  y.  Return the i
1d95b 6e 64 65 78 20 6f 66 0a 2a 2a 20 74 68 65 20 6e  ndex of.** the n
1d95c 65 77 20 65 6c 65 6d 65 6e 74 2e 20 20 52 65 74  ew element.  Ret
1d95d 75 72 6e 20 61 20 6e 65 67 61 74 69 76 65 20 6e  urn a negative n
1d95e 75 6d 62 65 72 20 69 66 20 6d 61 6c 6c 6f 63 20  umber if malloc 
1d95f 66 61 69 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  fails..*/.static
1d960 20 69 6e 74 20 61 64 64 41 67 67 49 6e 66 6f 43   int addAggInfoC
1d961 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 20 2a 64  olumn(sqlite3 *d
1d962 62 2c 20 41 67 67 49 6e 66 6f 20 2a 70 49 6e 66  b, AggInfo *pInf
1d963 6f 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 70  o){.  int i;.  p
1d964 49 6e 66 6f 2d 3e 61 43 6f 6c 20 3d 20 73 71 6c  Info->aCol = sql
1d965 69 74 65 33 41 72 72 61 79 41 6c 6c 6f 63 61 74  ite3ArrayAllocat
1d966 65 28 0a 20 20 20 20 20 20 20 64 62 2c 0a 20 20  e(.       db,.  
1d967 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c       pInfo->aCol
1d968 2c 0a 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28  ,.       sizeof(
1d969 70 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 30 5d 29 2c  pInfo->aCol[0]),
1d96a 0a 20 20 20 20 20 20 20 33 2c 0a 20 20 20 20 20  .       3,.     
1d96b 20 20 26 70 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d    &pInfo->nColum
1d96c 6e 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66 6f  n,.       &pInfo
1d96d 2d 3e 6e 43 6f 6c 75 6d 6e 41 6c 6c 6f 63 2c 0a  ->nColumnAlloc,.
1d96e 20 20 20 20 20 20 20 26 69 0a 20 20 29 3b 0a 20         &i.  );. 
1d96f 20 72 65 74 75 72 6e 20 69 3b 0a 7d 20 20 20 20   return i;.}    
1d970 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20 6e 65  ../*.** Add a ne
1d971 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74 68 65  w element to the
1d972 20 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63   pAggInfo->aFunc
1d973 5b 5d 20 61 72 72 61 79 2e 20 20 52 65 74 75 72  [] array.  Retur
1d974 6e 20 74 68 65 20 69 6e 64 65 78 20 6f 66 0a 2a  n the index of.*
1d975 2a 20 74 68 65 20 6e 65 77 20 65 6c 65 6d 65 6e  * the new elemen
1d976 74 2e 20 20 52 65 74 75 72 6e 20 61 20 6e 65 67  t.  Return a neg
1d977 61 74 69 76 65 20 6e 75 6d 62 65 72 20 69 66 20  ative number if 
1d978 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 2e 0a 2a 2f  malloc fails..*/
1d979 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 41  .static int addA
1d97a 67 67 49 6e 66 6f 46 75 6e 63 28 73 71 6c 69 74  ggInfoFunc(sqlit
1d97b 65 33 20 2a 64 62 2c 20 41 67 67 49 6e 66 6f 20  e3 *db, AggInfo 
1d97c 2a 70 49 6e 66 6f 29 7b 0a 20 20 69 6e 74 20 69  *pInfo){.  int i
1d97d 3b 0a 20 20 70 49 6e 66 6f 2d 3e 61 46 75 6e 63  ;.  pInfo->aFunc
1d97e 20 3d 20 73 71 6c 69 74 65 33 41 72 72 61 79 41   = sqlite3ArrayA
1d97f 6c 6c 6f 63 61 74 65 28 0a 20 20 20 20 20 20 20  llocate(.       
1d980 64 62 2c 20 0a 20 20 20 20 20 20 20 70 49 6e 66  db, .       pInf
1d981 6f 2d 3e 61 46 75 6e 63 2c 0a 20 20 20 20 20 20  o->aFunc,.      
1d982 20 73 69 7a 65 6f 66 28 70 49 6e 66 6f 2d 3e 61   sizeof(pInfo->a
1d983 46 75 6e 63 5b 30 5d 29 2c 0a 20 20 20 20 20 20  Func[0]),.      
1d984 20 33 2c 0a 20 20 20 20 20 20 20 26 70 49 6e 66   3,.       &pInf
1d985 6f 2d 3e 6e 46 75 6e 63 2c 0a 20 20 20 20 20 20  o->nFunc,.      
1d986 20 26 70 49 6e 66 6f 2d 3e 6e 46 75 6e 63 41 6c   &pInfo->nFuncAl
1d987 6c 6f 63 2c 0a 20 20 20 20 20 20 20 26 69 0a 20  loc,.       &i. 
1d988 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 69 3b 0a   );.  return i;.
1d989 7d 20 20 20 20 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  }    ../*.** Thi
1d98a 73 20 69 73 20 74 68 65 20 78 45 78 70 72 43 61  s is the xExprCa
1d98b 6c 6c 62 61 63 6b 20 66 6f 72 20 61 20 74 72 65  llback for a tre
1d98c 65 20 77 61 6c 6b 65 72 2e 20 20 49 74 20 69 73  e walker.  It is
1d98d 20 75 73 65 64 20 74 6f 0a 2a 2a 20 69 6d 70 6c   used to.** impl
1d98e 65 6d 65 6e 74 20 73 71 6c 69 74 65 33 45 78 70  ement sqlite3Exp
1d98f 72 41 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74  rAnalyzeAggregat
1d990 65 73 28 29 2e 20 20 53 65 65 20 73 71 6c 69 74  es().  See sqlit
1d991 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1d992 72 65 67 61 74 65 73 0a 2a 2a 20 66 6f 72 20 61  regates.** for a
1d993 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
1d994 61 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ation..*/.static
1d995 20 69 6e 74 20 61 6e 61 6c 79 7a 65 41 67 67 72   int analyzeAggr
1d996 65 67 61 74 65 28 57 61 6c 6b 65 72 20 2a 70 57  egate(Walker *pW
1d997 61 6c 6b 65 72 2c 20 45 78 70 72 20 2a 70 45 78  alker, Expr *pEx
1d998 70 72 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  pr){.  int i;.  
1d999 4e 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43  NameContext *pNC
1d99a 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e   = pWalker->u.pN
1d99b 43 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  C;.  Parse *pPar
1d99c 73 65 20 3d 20 70 4e 43 2d 3e 70 50 61 72 73 65  se = pNC->pParse
1d99d 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  ;.  SrcList *pSr
1d99e 63 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72  cList = pNC->pSr
1d99f 63 4c 69 73 74 3b 0a 20 20 41 67 67 49 6e 66 6f  cList;.  AggInfo
1d9a0 20 2a 70 41 67 67 49 6e 66 6f 20 3d 20 70 4e 43   *pAggInfo = pNC
1d9a1 2d 3e 70 41 67 67 49 6e 66 6f 3b 0a 0a 20 20 73  ->pAggInfo;..  s
1d9a2 77 69 74 63 68 28 20 70 45 78 70 72 2d 3e 6f 70  witch( pExpr->op
1d9a3 20 29 7b 0a 20 20 20 20 63 61 73 65 20 54 4b 5f   ){.    case TK_
1d9a4 41 47 47 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20  AGG_COLUMN:.    
1d9a5 63 61 73 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20  case TK_COLUMN: 
1d9a6 7b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65  {.      testcase
1d9a7 28 20 70 45 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f  ( pExpr->op==TK_
1d9a8 41 47 47 5f 43 4f 4c 55 4d 4e 20 29 3b 0a 20 20  AGG_COLUMN );.  
1d9a9 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 45      testcase( pE
1d9aa 78 70 72 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  xpr->op==TK_COLU
1d9ab 4d 4e 20 29 3b 0a 20 20 20 20 20 20 2f 2a 20 43  MN );.      /* C
1d9ac 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74  heck to see if t
1d9ad 68 65 20 63 6f 6c 75 6d 6e 20 69 73 20 69 6e 20  he column is in 
1d9ae 6f 6e 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  one of the table
1d9af 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 0a 20 20  s in the FROM.  
1d9b0 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 6f 66      ** clause of
1d9b1 20 74 68 65 20 61 67 67 72 65 67 61 74 65 20 71   the aggregate q
1d9b2 75 65 72 79 20 2a 2f 0a 20 20 20 20 20 20 69 66  uery */.      if
1d9b3 28 20 70 53 72 63 4c 69 73 74 20 29 7b 0a 20 20  ( pSrcList ){.  
1d9b4 20 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63        struct Src
1d9b5 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1d9b6 20 3d 20 70 53 72 63 4c 69 73 74 2d 3e 61 3b 0a   = pSrcList->a;.
1d9b7 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
1d9b8 20 69 3c 70 53 72 63 4c 69 73 74 2d 3e 6e 53 72   i<pSrcList->nSr
1d9b9 63 3b 20 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29  c; i++, pItem++)
1d9ba 7b 0a 20 20 20 20 20 20 20 20 20 20 73 74 72 75  {.          stru
1d9bb 63 74 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a  ct AggInfo_col *
1d9bc 70 43 6f 6c 3b 0a 20 20 20 20 20 20 20 20 20 20  pCol;.          
1d9bd 69 66 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c  if( pExpr->iTabl
1d9be 65 3d 3d 70 49 74 65 6d 2d 3e 69 43 75 72 73 6f  e==pItem->iCurso
1d9bf 72 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  r ){.           
1d9c0 20 2f 2a 20 49 66 20 77 65 20 72 65 61 63 68 20   /* If we reach 
1d9c1 74 68 69 73 20 70 6f 69 6e 74 2c 20 69 74 20 6d  this point, it m
1d9c2 65 61 6e 73 20 74 68 61 74 20 70 45 78 70 72 20  eans that pExpr 
1d9c3 72 65 66 65 72 73 20 74 6f 20 61 20 74 61 62 6c  refers to a tabl
1d9c4 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a  e.            **
1d9c5 20 74 68 61 74 20 69 73 20 69 6e 20 74 68 65 20   that is in the 
1d9c6 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
1d9c7 68 65 20 61 67 67 72 65 67 61 74 65 20 71 75 65  he aggregate que
1d9c8 72 79 2e 20 20 0a 20 20 20 20 20 20 20 20 20 20  ry.  .          
1d9c9 20 20 2a 2a 0a 20 20 20 20 20 20 20 20 20 20 20    **.           
1d9ca 20 2a 2a 20 4d 61 6b 65 20 61 6e 20 65 6e 74 72   ** Make an entr
1d9cb 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e  y for the column
1d9cc 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43   in pAggInfo->aC
1d9cd 6f 6c 5b 5d 20 69 66 20 74 68 65 72 65 0a 20 20  ol[] if there.  
1d9ce 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69 73 20            ** is 
1d9cf 6e 6f 74 20 61 6e 20 65 6e 74 72 79 20 74 68 65  not an entry the
1d9d0 72 65 20 61 6c 72 65 61 64 79 2e 0a 20 20 20 20  re already..    
1d9d1 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
1d9d2 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20         int k;.  
1d9d3 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c 20 3d            pCol =
1d9d4 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 3b   pAggInfo->aCol;
1d9d5 0a 20 20 20 20 20 20 20 20 20 20 20 20 66 6f 72  .            for
1d9d6 28 6b 3d 30 3b 20 6b 3c 70 41 67 67 49 6e 66 6f  (k=0; k<pAggInfo
1d9d7 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b 2c 20  ->nColumn; k++, 
1d9d8 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  pCol++){.       
1d9d9 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
1d9da 3e 69 54 61 62 6c 65 3d 3d 70 45 78 70 72 2d 3e  >iTable==pExpr->
1d9db 69 54 61 62 6c 65 20 26 26 0a 20 20 20 20 20 20  iTable &&.      
1d9dc 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f 6c              pCol
1d9dd 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72  ->iColumn==pExpr
1d9de 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20  ->iColumn ){.   
1d9df 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65               bre
1d9e0 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
1d9e1 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1d9e2 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66  }.            if
1d9e3 28 20 28 6b 3e 3d 70 41 67 67 49 6e 66 6f 2d 3e  ( (k>=pAggInfo->
1d9e4 6e 43 6f 6c 75 6d 6e 29 0a 20 20 20 20 20 20 20  nColumn).       
1d9e5 20 20 20 20 20 20 26 26 20 28 6b 20 3d 20 61 64        && (k = ad
1d9e6 64 41 67 67 49 6e 66 6f 43 6f 6c 75 6d 6e 28 70  dAggInfoColumn(p
1d9e7 50 61 72 73 65 2d 3e 64 62 2c 20 70 41 67 67 49  Parse->db, pAggI
1d9e8 6e 66 6f 29 29 3e 3d 30 20 0a 20 20 20 20 20 20  nfo))>=0 .      
1d9e9 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
1d9ea 20 20 20 20 20 20 20 70 43 6f 6c 20 3d 20 26 70         pCol = &p
1d9eb 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c 5b 6b 5d  AggInfo->aCol[k]
1d9ec 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
1d9ed 70 43 6f 6c 2d 3e 70 54 61 62 20 3d 20 70 45 78  pCol->pTab = pEx
1d9ee 70 72 2d 3e 70 54 61 62 3b 0a 20 20 20 20 20 20  pr->pTab;.      
1d9ef 20 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 69 54          pCol->iT
1d9f0 61 62 6c 65 20 3d 20 70 45 78 70 72 2d 3e 69 54  able = pExpr->iT
1d9f1 61 62 6c 65 3b 0a 20 20 20 20 20 20 20 20 20 20  able;.          
1d9f2 20 20 20 20 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d      pCol->iColum
1d9f3 6e 20 3d 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75  n = pExpr->iColu
1d9f4 6d 6e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  mn;.            
1d9f5 20 20 70 43 6f 6c 2d 3e 69 4d 65 6d 20 3d 20 2b    pCol->iMem = +
1d9f6 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
1d9f7 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 6f               pCo
1d9f8 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e  l->iSorterColumn
1d9f9 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20 20   = -1;.         
1d9fa 20 20 20 20 20 70 43 6f 6c 2d 3e 70 45 78 70 72       pCol->pExpr
1d9fb 20 3d 20 70 45 78 70 72 3b 0a 20 20 20 20 20 20   = pExpr;.      
1d9fc 20 20 20 20 20 20 20 20 69 66 28 20 70 41 67 67          if( pAgg
1d9fd 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 20 29  Info->pGroupBy )
1d9fe 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
1d9ff 20 20 69 6e 74 20 6a 2c 20 6e 3b 0a 20 20 20 20    int j, n;.    
1da00 20 20 20 20 20 20 20 20 20 20 20 20 45 78 70 72              Expr
1da01 4c 69 73 74 20 2a 70 47 42 20 3d 20 70 41 67 67  List *pGB = pAgg
1da02 49 6e 66 6f 2d 3e 70 47 72 6f 75 70 42 79 3b 0a  Info->pGroupBy;.
1da03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da04 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
1da05 69 74 65 6d 20 2a 70 54 65 72 6d 20 3d 20 70 47  item *pTerm = pG
1da06 42 2d 3e 61 3b 0a 20 20 20 20 20 20 20 20 20 20  B->a;.          
1da07 20 20 20 20 20 20 6e 20 3d 20 70 47 42 2d 3e 6e        n = pGB->n
1da08 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 20 20  Expr;.          
1da09 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
1da0a 3c 6e 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d 2b 2b  <n; j++, pTerm++
1da0b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
1da0c 20 20 20 20 20 45 78 70 72 20 2a 70 45 20 3d 20       Expr *pE = 
1da0d 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a 20 20  pTerm->pExpr;.  
1da0e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da0f 69 66 28 20 70 45 2d 3e 6f 70 3d 3d 54 4b 5f 43  if( pE->op==TK_C
1da10 4f 4c 55 4d 4e 20 26 26 20 70 45 2d 3e 69 54 61  OLUMN && pE->iTa
1da11 62 6c 65 3d 3d 70 45 78 70 72 2d 3e 69 54 61 62  ble==pExpr->iTab
1da12 6c 65 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  le &&.          
1da13 20 20 20 20 20 20 20 20 20 20 20 20 70 45 2d 3e              pE->
1da14 69 43 6f 6c 75 6d 6e 3d 3d 70 45 78 70 72 2d 3e  iColumn==pExpr->
1da15 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20  iColumn ){.     
1da16 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1da17 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f 6c 75  Col->iSorterColu
1da18 6d 6e 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20 20  mn = j;.        
1da19 20 20 20 20 20 20 20 20 20 20 20 20 62 72 65 61              brea
1da1a 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  k;.             
1da1b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
1da1c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1da1d 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
1da1e 20 20 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d         if( pCol-
1da1f 3e 69 53 6f 72 74 65 72 43 6f 6c 75 6d 6e 3c 30  >iSorterColumn<0
1da20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
1da21 20 20 20 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65      pCol->iSorte
1da22 72 43 6f 6c 75 6d 6e 20 3d 20 70 41 67 67 49 6e  rColumn = pAggIn
1da23 66 6f 2d 3e 6e 53 6f 72 74 69 6e 67 43 6f 6c 75  fo->nSortingColu
1da24 6d 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20  mn++;.          
1da25 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20      }.          
1da26 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20    }.            
1da27 2f 2a 20 54 68 65 72 65 20 69 73 20 6e 6f 77 20  /* There is now 
1da28 61 6e 20 65 6e 74 72 79 20 66 6f 72 20 70 45 78  an entry for pEx
1da29 70 72 20 69 6e 20 70 41 67 67 49 6e 66 6f 2d 3e  pr in pAggInfo->
1da2a 61 43 6f 6c 5b 5d 20 28 65 69 74 68 65 72 0a 20  aCol[] (either. 
1da2b 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 62 65             ** be
1da2c 63 61 75 73 65 20 69 74 20 77 61 73 20 74 68 65  cause it was the
1da2d 72 65 20 62 65 66 6f 72 65 20 6f 72 20 62 65 63  re before or bec
1da2e 61 75 73 65 20 77 65 20 6a 75 73 74 20 63 72 65  ause we just cre
1da2f 61 74 65 64 20 69 74 29 2e 0a 20 20 20 20 20 20  ated it)..      
1da30 20 20 20 20 20 20 2a 2a 20 43 6f 6e 76 65 72 74        ** Convert
1da31 20 74 68 65 20 70 45 78 70 72 20 74 6f 20 62 65   the pExpr to be
1da32 20 61 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e   a TK_AGG_COLUMN
1da33 20 72 65 66 65 72 72 69 6e 67 20 74 6f 20 74 68   referring to th
1da34 61 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  at.            *
1da35 2a 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  * pAggInfo->aCol
1da36 5b 5d 20 65 6e 74 72 79 2e 0a 20 20 20 20 20 20  [] entry..      
1da37 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20        */.       
1da38 20 20 20 20 20 70 45 78 70 72 2d 3e 70 41 67 67       pExpr->pAgg
1da39 49 6e 66 6f 20 3d 20 70 41 67 67 49 6e 66 6f 3b  Info = pAggInfo;
1da3a 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78  .            pEx
1da3b 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 41 47 47 5f  pr->op = TK_AGG_
1da3c 43 4f 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20  COLUMN;.        
1da3d 20 20 20 20 70 45 78 70 72 2d 3e 69 41 67 67 20      pExpr->iAgg 
1da3e 3d 20 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = k;.           
1da3f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
1da40 20 20 7d 20 2f 2a 20 65 6e 64 69 66 20 70 45 78    } /* endif pEx
1da41 70 72 2d 3e 69 54 61 62 6c 65 3d 3d 70 49 74 65  pr->iTable==pIte
1da42 6d 2d 3e 69 43 75 72 73 6f 72 20 2a 2f 0a 20 20  m->iCursor */.  
1da43 20 20 20 20 20 20 7d 20 2f 2a 20 65 6e 64 20 6c        } /* end l
1da44 6f 6f 70 20 6f 76 65 72 20 70 53 72 63 4c 69 73  oop over pSrcLis
1da45 74 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  t */.      }.   
1da46 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72     return WRC_Pr
1da47 75 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  une;.    }.    c
1da48 61 73 65 20 54 4b 5f 41 47 47 5f 46 55 4e 43 54  ase TK_AGG_FUNCT
1da49 49 4f 4e 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20  ION: {.      /* 
1da4a 54 68 65 20 70 4e 43 2d 3e 6e 44 65 70 74 68 3d  The pNC->nDepth=
1da4b 3d 30 20 74 65 73 74 20 63 61 75 73 65 73 20 61  =0 test causes a
1da4c 67 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f  ggregate functio
1da4d 6e 73 20 69 6e 20 73 75 62 71 75 65 72 69 65 73  ns in subqueries
1da4e 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20 62 65 20  .      ** to be 
1da4f 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20 20 20 20  ignored */.     
1da50 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68   if( pNC->nDepth
1da51 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==0 ){.        /
1da52 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
1da53 66 20 70 45 78 70 72 20 69 73 20 61 20 64 75 70  f pExpr is a dup
1da54 6c 69 63 61 74 65 20 6f 66 20 61 6e 6f 74 68 65  licate of anothe
1da55 72 20 61 67 67 72 65 67 61 74 65 20 0a 20 20 20  r aggregate .   
1da56 20 20 20 20 20 2a 2a 20 66 75 6e 63 74 69 6f 6e       ** function
1da57 20 74 68 61 74 20 69 73 20 61 6c 72 65 61 64 79   that is already
1da58 20 69 6e 20 74 68 65 20 70 41 67 67 49 6e 66 6f   in the pAggInfo
1da59 20 73 74 72 75 63 74 75 72 65 0a 20 20 20 20 20   structure.     
1da5a 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 74     */.        st
1da5b 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e  ruct AggInfo_fun
1da5c 63 20 2a 70 49 74 65 6d 20 3d 20 70 41 67 67 49  c *pItem = pAggI
1da5d 6e 66 6f 2d 3e 61 46 75 6e 63 3b 0a 20 20 20 20  nfo->aFunc;.    
1da5e 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1da5f 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b 20  AggInfo->nFunc; 
1da60 69 2b 2b 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20  i++, pItem++){. 
1da61 20 20 20 20 20 20 20 20 20 69 66 28 20 73 71 6c           if( sql
1da62 69 74 65 33 45 78 70 72 43 6f 6d 70 61 72 65 28  ite3ExprCompare(
1da63 70 49 74 65 6d 2d 3e 70 45 78 70 72 2c 20 70 45  pItem->pExpr, pE
1da64 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20  xpr) ){.        
1da65 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
1da66 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
1da67 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d  .        if( i>=
1da68 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 20  pAggInfo->nFunc 
1da69 29 7b 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ){.          /* 
1da6a 70 45 78 70 72 20 69 73 20 6f 72 69 67 69 6e 61  pExpr is origina
1da6b 6c 2e 20 20 4d 61 6b 65 20 61 20 6e 65 77 20 65  l.  Make a new e
1da6c 6e 74 72 79 20 69 6e 20 70 41 67 67 49 6e 66 6f  ntry in pAggInfo
1da6d 2d 3e 61 46 75 6e 63 5b 5d 0a 20 20 20 20 20 20  ->aFunc[].      
1da6e 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 20      */.         
1da6f 20 75 38 20 65 6e 63 20 3d 20 45 4e 43 28 70 50   u8 enc = ENC(pP
1da70 61 72 73 65 2d 3e 64 62 29 3b 0a 20 20 20 20 20  arse->db);.     
1da71 20 20 20 20 20 69 20 3d 20 61 64 64 41 67 67 49       i = addAggI
1da72 6e 66 6f 46 75 6e 63 28 70 50 61 72 73 65 2d 3e  nfoFunc(pParse->
1da73 64 62 2c 20 70 41 67 67 49 6e 66 6f 29 3b 0a 20  db, pAggInfo);. 
1da74 20 20 20 20 20 20 20 20 20 69 66 28 20 69 3e 3d           if( i>=
1da75 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
1da76 20 70 49 74 65 6d 20 3d 20 26 70 41 67 67 49 6e   pItem = &pAggIn
1da77 66 6f 2d 3e 61 46 75 6e 63 5b 69 5d 3b 0a 20 20  fo->aFunc[i];.  
1da78 20 20 20 20 20 20 20 20 20 20 70 49 74 65 6d 2d            pItem-
1da79 3e 70 45 78 70 72 20 3d 20 70 45 78 70 72 3b 0a  >pExpr = pExpr;.
1da7a 20 20 20 20 20 20 20 20 20 20 20 20 70 49 74 65              pIte
1da7b 6d 2d 3e 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72  m->iMem = ++pPar
1da7c 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20  se->nMem;.      
1da7d 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 70 46 75        pItem->pFu
1da7e 6e 63 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  nc = sqlite3Find
1da7f 46 75 6e 63 74 69 6f 6e 28 70 50 61 72 73 65 2d  Function(pParse-
1da80 3e 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20  >db,.           
1da81 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
1da82 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 7a 2c 20 70  Expr->token.z, p
1da83 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 2c 0a 20  Expr->token.n,. 
1da84 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1da85 20 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 20 3f    pExpr->pList ?
1da86 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e   pExpr->pList->n
1da87 45 78 70 72 20 3a 20 30 2c 20 65 6e 63 2c 20 30  Expr : 0, enc, 0
1da88 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  );.            i
1da89 66 28 20 70 45 78 70 72 2d 3e 66 6c 61 67 73 20  f( pExpr->flags 
1da8a 26 20 45 50 5f 44 69 73 74 69 6e 63 74 20 29 7b  & EP_Distinct ){
1da8b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
1da8c 49 74 65 6d 2d 3e 69 44 69 73 74 69 6e 63 74 20  Item->iDistinct 
1da8d 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
1da8e 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 65  ;.            }e
1da8f 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
1da90 20 20 20 70 49 74 65 6d 2d 3e 69 44 69 73 74 69     pItem->iDisti
1da91 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20  nct = -1;.      
1da92 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
1da93 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1da94 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 70 45        /* Make pE
1da95 78 70 72 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  xpr point to the
1da96 20 61 70 70 72 6f 70 72 69 61 74 65 20 70 41 67   appropriate pAg
1da97 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 5b 5d 20 65  gInfo->aFunc[] e
1da98 6e 74 72 79 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ntry.        */.
1da99 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69          pExpr->i
1da9a 41 67 67 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  Agg = i;.       
1da9b 20 70 45 78 70 72 2d 3e 70 41 67 67 49 6e 66 6f   pExpr->pAggInfo
1da9c 20 3d 20 70 41 67 67 49 6e 66 6f 3b 0a 20 20 20   = pAggInfo;.   
1da9d 20 20 20 20 20 72 65 74 75 72 6e 20 57 52 43 5f       return WRC_
1da9e 50 72 75 6e 65 3b 0a 20 20 20 20 20 20 7d 0a 20  Prune;.      }. 
1da9f 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
1daa0 6e 20 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a  n WRC_Continue;.
1daa1 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 61 6e 61  }.static int ana
1daa2 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73 49 6e  lyzeAggregatesIn
1daa3 53 65 6c 65 63 74 28 57 61 6c 6b 65 72 20 2a 70  Select(Walker *p
1daa4 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
1daa5 70 53 65 6c 65 63 74 29 7b 0a 20 20 4e 61 6d 65  pSelect){.  Name
1daa6 43 6f 6e 74 65 78 74 20 2a 70 4e 43 20 3d 20 70  Context *pNC = p
1daa7 57 61 6c 6b 65 72 2d 3e 75 2e 70 4e 43 3b 0a 20  Walker->u.pNC;. 
1daa8 20 69 66 28 20 70 4e 43 2d 3e 6e 44 65 70 74 68   if( pNC->nDepth
1daa9 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 43 2d 3e  ==0 ){.    pNC->
1daaa 6e 44 65 70 74 68 2b 2b 3b 0a 20 20 20 20 73 71  nDepth++;.    sq
1daab 6c 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28  lite3WalkSelect(
1daac 70 57 61 6c 6b 65 72 2c 20 70 53 65 6c 65 63 74  pWalker, pSelect
1daad 29 3b 0a 20 20 20 20 70 4e 43 2d 3e 6e 44 65 70  );.    pNC->nDep
1daae 74 68 2d 2d 3b 0a 20 20 20 20 72 65 74 75 72 6e  th--;.    return
1daaf 20 57 52 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 65   WRC_Prune;.  }e
1dab0 6c 73 65 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  lse{.    return 
1dab1 57 52 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 20 20  WRC_Continue;.  
1dab2 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79  }.}../*.** Analy
1dab3 7a 65 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  ze the given exp
1dab4 72 65 73 73 69 6f 6e 20 6c 6f 6f 6b 69 6e 67 20  ression looking 
1dab5 66 6f 72 20 61 67 67 72 65 67 61 74 65 20 66 75  for aggregate fu
1dab6 6e 63 74 69 6f 6e 73 20 61 6e 64 0a 2a 2a 20 66  nctions and.** f
1dab7 6f 72 20 76 61 72 69 61 62 6c 65 73 20 74 68 61  or variables tha
1dab8 74 20 6e 65 65 64 20 74 6f 20 62 65 20 61 64 64  t need to be add
1dab9 65 64 20 74 6f 20 74 68 65 20 70 50 61 72 73 65  ed to the pParse
1daba 2d 3e 61 41 67 67 5b 5d 20 61 72 72 61 79 2e 0a  ->aAgg[] array..
1dabb 2a 2a 20 4d 61 6b 65 20 61 64 64 69 74 69 6f 6e  ** Make addition
1dabc 61 6c 20 65 6e 74 72 69 65 73 20 74 6f 20 74 68  al entries to th
1dabd 65 20 70 50 61 72 73 65 2d 3e 61 41 67 67 5b 5d  e pParse->aAgg[]
1dabe 20 61 72 72 61 79 20 61 73 20 6e 65 63 65 73 73   array as necess
1dabf 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ary..**.** This 
1dac0 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6f  routine should o
1dac1 6e 6c 79 20 62 65 20 63 61 6c 6c 65 64 20 61 66  nly be called af
1dac2 74 65 72 20 74 68 65 20 65 78 70 72 65 73 73 69  ter the expressi
1dac3 6f 6e 20 68 61 73 20 62 65 65 6e 0a 2a 2a 20 61  on has been.** a
1dac4 6e 61 6c 79 7a 65 64 20 62 79 20 73 71 6c 69 74  nalyzed by sqlit
1dac5 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d  e3ResolveExprNam
1dac6 65 73 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  es()..*/.SQLITE_
1dac7 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1dac8 69 74 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41  ite3ExprAnalyzeA
1dac9 67 67 72 65 67 61 74 65 73 28 4e 61 6d 65 43 6f  ggregates(NameCo
1daca 6e 74 65 78 74 20 2a 70 4e 43 2c 20 45 78 70 72  ntext *pNC, Expr
1dacb 20 2a 70 45 78 70 72 29 7b 0a 20 20 57 61 6c 6b   *pExpr){.  Walk
1dacc 65 72 20 77 3b 0a 20 20 77 2e 78 45 78 70 72 43  er w;.  w.xExprC
1dacd 61 6c 6c 62 61 63 6b 20 3d 20 61 6e 61 6c 79 7a  allback = analyz
1dace 65 41 67 67 72 65 67 61 74 65 3b 0a 20 20 77 2e  eAggregate;.  w.
1dacf 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
1dad0 3d 20 61 6e 61 6c 79 7a 65 41 67 67 72 65 67 61  = analyzeAggrega
1dad1 74 65 73 49 6e 53 65 6c 65 63 74 3b 0a 20 20 77  tesInSelect;.  w
1dad2 2e 75 2e 70 4e 43 20 3d 20 70 4e 43 3b 0a 20 20  .u.pNC = pNC;.  
1dad3 73 71 6c 69 74 65 33 57 61 6c 6b 45 78 70 72 28  sqlite3WalkExpr(
1dad4 26 77 2c 20 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f  &w, pExpr);.}../
1dad5 2a 0a 2a 2a 20 43 61 6c 6c 20 73 71 6c 69 74 65  *.** Call sqlite
1dad6 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67 72  3ExprAnalyzeAggr
1dad7 65 67 61 74 65 73 28 29 20 66 6f 72 20 65 76 65  egates() for eve
1dad8 72 79 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e  ry expression in
1dad9 20 61 6e 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f   an.** expressio
1dada 6e 20 6c 69 73 74 2e 20 20 52 65 74 75 72 6e 20  n list.  Return 
1dadb 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 72  the number of er
1dadc 72 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61  rors..**.** If a
1dadd 6e 20 65 72 72 6f 72 20 69 73 20 66 6f 75 6e 64  n error is found
1dade 2c 20 74 68 65 20 61 6e 61 6c 79 73 69 73 20 69  , the analysis i
1dadf 73 20 63 75 74 20 73 68 6f 72 74 2e 0a 2a 2f 0a  s cut short..*/.
1dae0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1dae1 6f 69 64 20 73 71 6c 69 74 65 33 45 78 70 72 41  oid sqlite3ExprA
1dae2 6e 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 4e 61  nalyzeAggList(Na
1dae3 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4e 43 2c 20  meContext *pNC, 
1dae4 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 29  ExprList *pList)
1dae5 7b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72 4c  {.  struct ExprL
1dae6 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b  ist_item *pItem;
1dae7 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20  .  int i;.  if( 
1dae8 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 66 6f 72  pList ){.    for
1dae9 28 70 49 74 65 6d 3d 70 4c 69 73 74 2d 3e 61 2c  (pItem=pList->a,
1daea 20 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e   i=0; i<pList->n
1daeb 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
1daec 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
1daed 65 33 45 78 70 72 41 6e 61 6c 79 7a 65 41 67 67  e3ExprAnalyzeAgg
1daee 72 65 67 61 74 65 73 28 70 4e 43 2c 20 70 49 74  regates(pNC, pIt
1daef 65 6d 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20  em->pExpr);.    
1daf0 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  }.  }.}../*.** A
1daf1 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c 6c  llocate or deall
1daf2 6f 63 61 74 65 20 74 65 6d 70 6f 72 61 72 79 20  ocate temporary 
1daf3 75 73 65 20 72 65 67 69 73 74 65 72 73 20 64 75  use registers du
1daf4 72 69 6e 67 20 63 6f 64 65 20 67 65 6e 65 72 61  ring code genera
1daf5 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tion..*/.SQLITE_
1daf6 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1daf7 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 50 61  te3GetTempReg(Pa
1daf8 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
1daf9 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 54 65 6d  if( pParse->nTem
1dafa 70 52 65 67 3d 3d 30 20 29 7b 0a 20 20 20 20 72  pReg==0 ){.    r
1dafb 65 74 75 72 6e 20 2b 2b 70 50 61 72 73 65 2d 3e  eturn ++pParse->
1dafc 6e 4d 65 6d 3b 0a 20 20 7d 0a 20 20 72 65 74 75  nMem;.  }.  retu
1dafd 72 6e 20 70 50 61 72 73 65 2d 3e 61 54 65 6d 70  rn pParse->aTemp
1dafe 52 65 67 5b 2d 2d 70 50 61 72 73 65 2d 3e 6e 54  Reg[--pParse->nT
1daff 65 6d 70 52 65 67 5d 3b 0a 7d 0a 53 51 4c 49 54  empReg];.}.SQLIT
1db00 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1db01 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
1db02 70 52 65 67 28 50 61 72 73 65 20 2a 70 50 61 72  pReg(Parse *pPar
1db03 73 65 2c 20 69 6e 74 20 69 52 65 67 29 7b 0a 20  se, int iReg){. 
1db04 20 69 66 28 20 69 52 65 67 20 26 26 20 70 50 61   if( iReg && pPa
1db05 72 73 65 2d 3e 6e 54 65 6d 70 52 65 67 3c 41 72  rse->nTempReg<Ar
1db06 72 61 79 53 69 7a 65 28 70 50 61 72 73 65 2d 3e  raySize(pParse->
1db07 61 54 65 6d 70 52 65 67 29 20 29 7b 0a 20 20 20  aTempReg) ){.   
1db08 20 73 71 6c 69 74 65 33 45 78 70 72 57 72 69 74   sqlite3ExprWrit
1db09 61 62 6c 65 52 65 67 69 73 74 65 72 28 70 50 61  ableRegister(pPa
1db0a 72 73 65 2c 20 69 52 65 67 2c 20 69 52 65 67 29  rse, iReg, iReg)
1db0b 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 54  ;.    pParse->aT
1db0c 65 6d 70 52 65 67 5b 70 50 61 72 73 65 2d 3e 6e  empReg[pParse->n
1db0d 54 65 6d 70 52 65 67 2b 2b 5d 20 3d 20 69 52 65  TempReg++] = iRe
1db0e 67 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  g;.  }.}../*.** 
1db0f 41 6c 6c 6f 63 61 74 65 20 6f 72 20 64 65 61 6c  Allocate or deal
1db10 6c 6f 63 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f  locate a block o
1db11 66 20 6e 52 65 67 20 63 6f 6e 73 65 63 75 74 69  f nReg consecuti
1db12 76 65 20 72 65 67 69 73 74 65 72 73 0a 2a 2f 0a  ve registers.*/.
1db13 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
1db14 6e 74 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d  nt sqlite3GetTem
1db15 70 52 61 6e 67 65 28 50 61 72 73 65 20 2a 70 50  pRange(Parse *pP
1db16 61 72 73 65 2c 20 69 6e 74 20 6e 52 65 67 29 7b  arse, int nReg){
1db17 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69  .  int i, n;.  i
1db18 20 3d 20 70 50 61 72 73 65 2d 3e 69 52 61 6e 67   = pParse->iRang
1db19 65 52 65 67 3b 0a 20 20 6e 20 3d 20 70 50 61 72  eReg;.  n = pPar
1db1a 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 3b 0a 20  se->nRangeReg;. 
1db1b 20 69 66 28 20 6e 52 65 67 3c 3d 6e 20 26 26 20   if( nReg<=n && 
1db1c 21 75 73 65 64 41 73 43 6f 6c 75 6d 6e 43 61 63  !usedAsColumnCac
1db1d 68 65 28 70 50 61 72 73 65 2c 20 69 2c 20 69 2b  he(pParse, i, i+
1db1e 6e 2d 31 29 20 29 7b 0a 20 20 20 20 70 50 61 72  n-1) ){.    pPar
1db1f 73 65 2d 3e 69 52 61 6e 67 65 52 65 67 20 2b 3d  se->iRangeReg +=
1db20 20 6e 52 65 67 3b 0a 20 20 20 20 70 50 61 72 73   nReg;.    pPars
1db21 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20 2d 3d 20  e->nRangeReg -= 
1db22 6e 52 65 67 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  nReg;.  }else{. 
1db23 20 20 20 69 20 3d 20 70 50 61 72 73 65 2d 3e 6e     i = pParse->n
1db24 4d 65 6d 2b 31 3b 0a 20 20 20 20 70 50 61 72 73  Mem+1;.    pPars
1db25 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 6e 52 65 67 3b  e->nMem += nReg;
1db26 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 3b  .  }.  return i;
1db27 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  .}.SQLITE_PRIVAT
1db28 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52 65  E void sqlite3Re
1db29 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 50  leaseTempRange(P
1db2a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1db2b 74 20 69 52 65 67 2c 20 69 6e 74 20 6e 52 65 67  t iReg, int nReg
1db2c 29 7b 0a 20 20 69 66 28 20 6e 52 65 67 3e 70 50  ){.  if( nReg>pP
1db2d 61 72 73 65 2d 3e 6e 52 61 6e 67 65 52 65 67 20  arse->nRangeReg 
1db2e 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e  ){.    pParse->n
1db2f 52 61 6e 67 65 52 65 67 20 3d 20 6e 52 65 67 3b  RangeReg = nReg;
1db30 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 69 52 61  .    pParse->iRa
1db31 6e 67 65 52 65 67 20 3d 20 69 52 65 67 3b 0a 20  ngeReg = iReg;. 
1db32 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a   }.}../*********
1db33 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 65 78 70  ***** End of exp
1db34 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
1db35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db36 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db37 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
1db38 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
1db39 20 61 6c 74 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a   alter.c *******
1db3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db3c 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
1db3d 20 46 65 62 72 75 61 72 79 20 31 35 0a 2a 2a 0a   February 15.**.
1db3e 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
1db3f 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
1db40 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
1db41 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
1db42 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
1db43 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
1db44 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
1db45 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
1db46 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
1db47 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1db48 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
1db49 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
1db4a 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
1db4b 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
1db4c 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
1db4d 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
1db4e 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
1db4f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
1db50 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db51 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db52 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1db53 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54  ***********.** T
1db54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e  his file contain
1db55 73 20 43 20 63 6f 64 65 20 72 6f 75 74 69 6e 65  s C code routine
1db56 73 20 74 68 61 74 20 75 73 65 64 20 74 6f 20 67  s that used to g
1db57 65 6e 65 72 61 74 65 20 56 44 42 45 20 63 6f 64  enerate VDBE cod
1db58 65 0a 2a 2a 20 74 68 61 74 20 69 6d 70 6c 65 6d  e.** that implem
1db59 65 6e 74 73 20 74 68 65 20 41 4c 54 45 52 20 54  ents the ALTER T
1db5a 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  ABLE command..**
1db5b 0a 2a 2a 20 24 49 64 3a 20 61 6c 74 65 72 2e 63  .** $Id: alter.c
1db5c 2c 76 20 31 2e 35 30 20 32 30 30 38 2f 31 31 2f  ,v 1.50 2008/11/
1db5d 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69  19 09:05:27 dani
1db5e 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
1db5f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ../*.** The code
1db60 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 6f 6e   in this file on
1db61 6c 79 20 65 78 69 73 74 73 20 69 66 20 77 65 20  ly exists if we 
1db62 61 72 65 20 6e 6f 74 20 6f 6d 69 74 74 69 6e 67  are not omitting
1db63 20 74 68 65 0a 2a 2a 20 41 4c 54 45 52 20 54 41   the.** ALTER TA
1db64 42 4c 45 20 6c 6f 67 69 63 20 66 72 6f 6d 20 74  BLE logic from t
1db65 68 65 20 62 75 69 6c 64 2e 0a 2a 2f 0a 23 69 66  he build..*/.#if
1db66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1db67 5f 41 4c 54 45 52 54 41 42 4c 45 0a 0a 0a 2f 2a  _ALTERTABLE.../*
1db68 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
1db69 6e 20 69 73 20 75 73 65 64 20 62 79 20 53 51 4c  n is used by SQL
1db6a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 69 6d   generated to im
1db6b 70 6c 65 6d 65 6e 74 20 74 68 65 20 0a 2a 2a 20  plement the .** 
1db6c 41 4c 54 45 52 20 54 41 42 4c 45 20 63 6f 6d 6d  ALTER TABLE comm
1db6d 61 6e 64 2e 20 54 68 65 20 66 69 72 73 74 20 61  and. The first a
1db6e 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 74  rgument is the t
1db6f 65 78 74 20 6f 66 20 61 20 43 52 45 41 54 45 20  ext of a CREATE 
1db70 54 41 42 4c 45 20 6f 72 0a 2a 2a 20 43 52 45 41  TABLE or.** CREA
1db71 54 45 20 49 4e 44 45 58 20 63 6f 6d 6d 61 6e 64  TE INDEX command
1db72 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73 20  . The second is 
1db73 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54 68  a table name. Th
1db74 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e 20  e table name in 
1db75 0a 2a 2a 20 74 68 65 20 43 52 45 41 54 45 20 54  .** the CREATE T
1db76 41 42 4c 45 20 6f 72 20 43 52 45 41 54 45 20 49  ABLE or CREATE I
1db77 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 69  NDEX statement i
1db78 73 20 72 65 70 6c 61 63 65 64 20 77 69 74 68 20  s replaced with 
1db79 74 68 65 20 74 68 69 72 64 0a 2a 2a 20 61 72 67  the third.** arg
1db7a 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 65  ument and the re
1db7b 73 75 6c 74 20 72 65 74 75 72 6e 65 64 2e 20 45  sult returned. E
1db7c 78 61 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 73  xamples:.**.** s
1db7d 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 61 62  qlite_rename_tab
1db7e 6c 65 28 27 43 52 45 41 54 45 20 54 41 42 4c 45  le('CREATE TABLE
1db7f 20 61 62 63 28 61 2c 20 62 2c 20 63 29 27 2c 20   abc(a, b, c)', 
1db80 27 64 65 66 27 29 0a 2a 2a 20 20 20 20 20 2d 3e  'def').**     ->
1db81 20 27 43 52 45 41 54 45 20 54 41 42 4c 45 20 64   'CREATE TABLE d
1db82 65 66 28 61 2c 20 62 2c 20 63 29 27 0a 2a 2a 0a  ef(a, b, c)'.**.
1db83 2a 2a 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65  ** sqlite_rename
1db84 5f 74 61 62 6c 65 28 27 43 52 45 41 54 45 20 49  _table('CREATE I
1db85 4e 44 45 58 20 69 20 4f 4e 20 61 62 63 28 61 29  NDEX i ON abc(a)
1db86 27 2c 20 27 64 65 66 27 29 0a 2a 2a 20 20 20 20  ', 'def').**    
1db87 20 2d 3e 20 27 43 52 45 41 54 45 20 49 4e 44 45   -> 'CREATE INDE
1db88 58 20 69 20 4f 4e 20 64 65 66 28 61 2c 20 62 2c  X i ON def(a, b,
1db89 20 63 29 27 0a 2a 2f 0a 73 74 61 74 69 63 20 76   c)'.*/.static v
1db8a 6f 69 64 20 72 65 6e 61 6d 65 54 61 62 6c 65 46  oid renameTableF
1db8b 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
1db8c 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1db8d 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
1db8e 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1db8f 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69  **argv.){.  unsi
1db90 67 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20  gned char const 
1db91 2a 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f  *zSql = sqlite3_
1db92 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
1db93 30 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20  0]);.  unsigned 
1db94 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62  char const *zTab
1db95 6c 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33  leName = sqlite3
1db96 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
1db97 5b 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b  [1]);..  int tok
1db98 65 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d  en;.  Token tnam
1db99 65 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  e;.  unsigned ch
1db9a 61 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d  ar const *zCsr =
1db9b 20 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e   zSql;.  int len
1db9c 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52   = 0;.  char *zR
1db9d 65 74 3b 0a 0a 20 20 73 71 6c 69 74 65 33 20 2a  et;..  sqlite3 *
1db9e 64 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e  db = sqlite3_con
1db9f 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63  text_db_handle(c
1dba0 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53  ontext);..  UNUS
1dba1 45 44 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74  ED_PARAMETER(Not
1dba2 55 73 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  Used);..  /* The
1dba3 20 70 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20   principle used 
1dba4 74 6f 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61  to locate the ta
1dba5 62 6c 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20  ble name in the 
1dba6 43 52 45 41 54 45 20 54 41 42 4c 45 20 0a 20 20  CREATE TABLE .  
1dba7 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ** statement is 
1dba8 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e  that the table n
1dba9 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74  ame is the first
1dbaa 20 6e 6f 6e 2d 73 70 61 63 65 20 74 6f 6b 65 6e   non-space token
1dbab 20 74 68 61 74 0a 20 20 2a 2a 20 69 73 20 69 6d   that.  ** is im
1dbac 6d 65 64 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77  mediately follow
1dbad 65 64 20 62 79 20 61 20 54 4b 5f 4c 50 20 6f 72  ed by a TK_LP or
1dbae 20 54 4b 5f 55 53 49 4e 47 20 74 6f 6b 65 6e 2e   TK_USING token.
1dbaf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 7a 53 71 6c  .  */.  if( zSql
1dbb0 20 29 7b 0a 20 20 20 20 64 6f 20 7b 0a 20 20 20   ){.    do {.   
1dbb1 20 20 20 69 66 28 20 21 2a 7a 43 73 72 20 29 7b     if( !*zCsr ){
1dbb2 0a 20 20 20 20 20 20 20 20 2f 2a 20 52 61 6e 20  .        /* Ran 
1dbb3 6f 75 74 20 6f 66 20 69 6e 70 75 74 20 62 65 66  out of input bef
1dbb4 6f 72 65 20 66 69 6e 64 69 6e 67 20 61 6e 20 6f  ore finding an o
1dbb5 70 65 6e 69 6e 67 20 62 72 61 63 6b 65 74 2e 20  pening bracket. 
1dbb6 52 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  Return NULL. */.
1dbb7 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a          return;.
1dbb8 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
1dbb9 2a 20 53 74 6f 72 65 20 74 68 65 20 74 6f 6b 65  * Store the toke
1dbba 6e 20 74 68 61 74 20 7a 43 73 72 20 70 6f 69 6e  n that zCsr poin
1dbbb 74 73 20 74 6f 20 69 6e 20 74 6e 61 6d 65 2e 20  ts to in tname. 
1dbbc 2a 2f 0a 20 20 20 20 20 20 74 6e 61 6d 65 2e 7a  */.      tname.z
1dbbd 20 3d 20 7a 43 73 72 3b 0a 20 20 20 20 20 20 74   = zCsr;.      t
1dbbe 6e 61 6d 65 2e 6e 20 3d 20 6c 65 6e 3b 0a 0a 20  name.n = len;.. 
1dbbf 20 20 20 20 20 2f 2a 20 41 64 76 61 6e 63 65 20       /* Advance 
1dbc0 7a 43 73 72 20 74 6f 20 74 68 65 20 6e 65 78 74  zCsr to the next
1dbc1 20 74 6f 6b 65 6e 2e 20 53 74 6f 72 65 20 74 68   token. Store th
1dbc2 61 74 20 74 6f 6b 65 6e 20 74 79 70 65 20 69 6e  at token type in
1dbc3 20 27 74 6f 6b 65 6e 27 2c 0a 20 20 20 20 20 20   'token',.      
1dbc4 2a 2a 20 61 6e 64 20 69 74 73 20 6c 65 6e 67 74  ** and its lengt
1dbc5 68 20 69 6e 20 27 6c 65 6e 27 20 28 74 6f 20 62  h in 'len' (to b
1dbc6 65 20 75 73 65 64 20 6e 65 78 74 20 69 74 65 72  e used next iter
1dbc7 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 6c 6f  ation of this lo
1dbc8 6f 70 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  op)..      */.  
1dbc9 20 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 20 20      do {.       
1dbca 20 7a 43 73 72 20 2b 3d 20 6c 65 6e 3b 0a 20 20   zCsr += len;.  
1dbcb 20 20 20 20 20 20 6c 65 6e 20 3d 20 73 71 6c 69        len = sqli
1dbcc 74 65 33 47 65 74 54 6f 6b 65 6e 28 7a 43 73 72  te3GetToken(zCsr
1dbcd 2c 20 26 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  , &token);.     
1dbce 20 7d 20 77 68 69 6c 65 28 20 74 6f 6b 65 6e 3d   } while( token=
1dbcf 3d 54 4b 5f 53 50 41 43 45 20 29 3b 0a 20 20 20  =TK_SPACE );.   
1dbd0 20 20 20 61 73 73 65 72 74 28 20 6c 65 6e 3e 30     assert( len>0
1dbd1 20 29 3b 0a 20 20 20 20 7d 20 77 68 69 6c 65 28   );.    } while(
1dbd2 20 74 6f 6b 65 6e 21 3d 54 4b 5f 4c 50 20 26 26   token!=TK_LP &&
1dbd3 20 74 6f 6b 65 6e 21 3d 54 4b 5f 55 53 49 4e 47   token!=TK_USING
1dbd4 20 29 3b 0a 0a 20 20 20 20 7a 52 65 74 20 3d 20   );..    zRet = 
1dbd5 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1dbd6 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c 22 25 73  b, "%.*s\"%w\"%s
1dbd7 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20 7a 53 71  ", tname.z - zSq
1dbd8 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20 20 20 20  l, zSql, .      
1dbd9 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 74 6e 61   zTableName, tna
1dbda 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29 3b 0a 20  me.z+tname.n);. 
1dbdb 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
1dbdc 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
1dbdd 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45  zRet, -1, SQLITE
1dbde 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 7d  _DYNAMIC);.  }.}
1dbdf 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1dbe0 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 2f 2a  _OMIT_TRIGGER./*
1dbe1 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   This function i
1dbe2 73 20 75 73 65 64 20 62 79 20 53 51 4c 20 67 65  s used by SQL ge
1dbe3 6e 65 72 61 74 65 64 20 74 6f 20 69 6d 70 6c 65  nerated to imple
1dbe4 6d 65 6e 74 20 74 68 65 0a 2a 2a 20 41 4c 54 45  ment the.** ALTE
1dbe5 52 20 54 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 2e  R TABLE command.
1dbe6 20 54 68 65 20 66 69 72 73 74 20 61 72 67 75 6d   The first argum
1dbe7 65 6e 74 20 69 73 20 74 68 65 20 74 65 78 74 20  ent is the text 
1dbe8 6f 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47  of a CREATE TRIG
1dbe9 47 45 52 20 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  GER .** statemen
1dbea 74 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 69 73  t. The second is
1dbeb 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 54   a table name. T
1dbec 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 69 6e  he table name in
1dbed 20 74 68 65 20 43 52 45 41 54 45 20 0a 2a 2a 20   the CREATE .** 
1dbee 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e  TRIGGER statemen
1dbef 74 20 69 73 20 72 65 70 6c 61 63 65 64 20 77 69  t is replaced wi
1dbf0 74 68 20 74 68 65 20 74 68 69 72 64 20 61 72 67  th the third arg
1dbf1 75 6d 65 6e 74 20 61 6e 64 20 74 68 65 20 72 65  ument and the re
1dbf2 73 75 6c 74 20 0a 2a 2a 20 72 65 74 75 72 6e 65  sult .** returne
1dbf3 64 2e 20 54 68 69 73 20 69 73 20 61 6e 61 6c 61  d. This is anala
1dbf4 67 6f 75 73 20 74 6f 20 72 65 6e 61 6d 65 54 61  gous to renameTa
1dbf5 62 6c 65 46 75 6e 63 28 29 20 61 62 6f 76 65 2c  bleFunc() above,
1dbf6 20 65 78 63 65 70 74 20 66 6f 72 20 43 52 45 41   except for CREA
1dbf7 54 45 0a 2a 2a 20 54 52 49 47 47 45 52 2c 20 6e  TE.** TRIGGER, n
1dbf8 6f 74 20 43 52 45 41 54 45 20 49 4e 44 45 58 20  ot CREATE INDEX 
1dbf9 61 6e 64 20 43 52 45 41 54 45 20 54 41 42 4c 45  and CREATE TABLE
1dbfa 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1dbfb 20 72 65 6e 61 6d 65 54 72 69 67 67 65 72 46 75   renameTriggerFu
1dbfc 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
1dbfd 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
1dbfe 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20    int NotUsed,. 
1dbff 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
1dc00 2a 61 72 67 76 0a 29 7b 0a 20 20 75 6e 73 69 67  *argv.){.  unsig
1dc01 6e 65 64 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ned char const *
1dc02 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76  zSql = sqlite3_v
1dc03 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
1dc04 5d 29 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63  ]);.  unsigned c
1dc05 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 61 62 6c  har const *zTabl
1dc06 65 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f  eName = sqlite3_
1dc07 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b  value_text(argv[
1dc08 31 5d 29 3b 0a 0a 20 20 69 6e 74 20 74 6f 6b 65  1]);..  int toke
1dc09 6e 3b 0a 20 20 54 6f 6b 65 6e 20 74 6e 61 6d 65  n;.  Token tname
1dc0a 3b 0a 20 20 69 6e 74 20 64 69 73 74 20 3d 20 33  ;.  int dist = 3
1dc0b 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  ;.  unsigned cha
1dc0c 72 20 63 6f 6e 73 74 20 2a 7a 43 73 72 20 3d 20  r const *zCsr = 
1dc0d 7a 53 71 6c 3b 0a 20 20 69 6e 74 20 6c 65 6e 20  zSql;.  int len 
1dc0e 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 7a 52 65  = 0;.  char *zRe
1dc0f 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t;.  sqlite3 *db
1dc10 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65   = sqlite3_conte
1dc11 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e  xt_db_handle(con
1dc12 74 65 78 74 29 3b 0a 0a 20 20 55 4e 55 53 45 44  text);..  UNUSED
1dc13 5f 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73  _PARAMETER(NotUs
1dc14 65 64 29 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 70  ed);..  /* The p
1dc15 72 69 6e 63 69 70 6c 65 20 75 73 65 64 20 74 6f  rinciple used to
1dc16 20 6c 6f 63 61 74 65 20 74 68 65 20 74 61 62 6c   locate the tabl
1dc17 65 20 6e 61 6d 65 20 69 6e 20 74 68 65 20 43 52  e name in the CR
1dc18 45 41 54 45 20 54 52 49 47 47 45 52 20 0a 20 20  EATE TRIGGER .  
1dc19 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  ** statement is 
1dc1a 74 68 61 74 20 74 68 65 20 74 61 62 6c 65 20 6e  that the table n
1dc1b 61 6d 65 20 69 73 20 74 68 65 20 66 69 72 73 74  ame is the first
1dc1c 20 74 6f 6b 65 6e 20 74 68 61 74 20 69 73 20 69   token that is i
1dc1d 6d 6d 65 64 69 61 74 65 64 6c 79 0a 20 20 2a 2a  mmediatedly.  **
1dc1e 20 70 72 65 63 65 64 65 64 20 62 79 20 65 69 74   preceded by eit
1dc1f 68 65 72 20 54 4b 5f 4f 4e 20 6f 72 20 54 4b 5f  her TK_ON or TK_
1dc20 44 4f 54 20 61 6e 64 20 69 6d 6d 65 64 69 61 74  DOT and immediat
1dc21 65 64 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62 79  edly followed by
1dc22 20 6f 6e 65 0a 20 20 2a 2a 20 6f 66 20 54 4b 5f   one.  ** of TK_
1dc23 57 48 45 4e 2c 20 54 4b 5f 42 45 47 49 4e 20 6f  WHEN, TK_BEGIN o
1dc24 72 20 54 4b 5f 46 4f 52 2e 0a 20 20 2a 2f 0a 20  r TK_FOR..  */. 
1dc25 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20 20 20   if( zSql ){.   
1dc26 20 64 6f 20 7b 0a 0a 20 20 20 20 20 20 69 66 28   do {..      if(
1dc27 20 21 2a 7a 43 73 72 20 29 7b 0a 20 20 20 20 20   !*zCsr ){.     
1dc28 20 20 20 2f 2a 20 52 61 6e 20 6f 75 74 20 6f 66     /* Ran out of
1dc29 20 69 6e 70 75 74 20 62 65 66 6f 72 65 20 66 69   input before fi
1dc2a 6e 64 69 6e 67 20 74 68 65 20 74 61 62 6c 65 20  nding the table 
1dc2b 6e 61 6d 65 2e 20 52 65 74 75 72 6e 20 4e 55 4c  name. Return NUL
1dc2c 4c 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65  L. */.        re
1dc2d 74 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  turn;.      }.. 
1dc2e 20 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68       /* Store th
1dc2f 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 7a 43 73  e token that zCs
1dc30 72 20 70 6f 69 6e 74 73 20 74 6f 20 69 6e 20 74  r points to in t
1dc31 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 20 20 74  name. */.      t
1dc32 6e 61 6d 65 2e 7a 20 3d 20 7a 43 73 72 3b 0a 20  name.z = zCsr;. 
1dc33 20 20 20 20 20 74 6e 61 6d 65 2e 6e 20 3d 20 6c       tname.n = l
1dc34 65 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 41 64  en;..      /* Ad
1dc35 76 61 6e 63 65 20 7a 43 73 72 20 74 6f 20 74 68  vance zCsr to th
1dc36 65 20 6e 65 78 74 20 74 6f 6b 65 6e 2e 20 53 74  e next token. St
1dc37 6f 72 65 20 74 68 61 74 20 74 6f 6b 65 6e 20 74  ore that token t
1dc38 79 70 65 20 69 6e 20 27 74 6f 6b 65 6e 27 2c 0a  ype in 'token',.
1dc39 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 69 74 73        ** and its
1dc3a 20 6c 65 6e 67 74 68 20 69 6e 20 27 6c 65 6e 27   length in 'len'
1dc3b 20 28 74 6f 20 62 65 20 75 73 65 64 20 6e 65 78   (to be used nex
1dc3c 74 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74  t iteration of t
1dc3d 68 69 73 20 6c 6f 6f 70 29 2e 0a 20 20 20 20 20  his loop)..     
1dc3e 20 2a 2f 0a 20 20 20 20 20 20 64 6f 20 7b 0a 20   */.      do {. 
1dc3f 20 20 20 20 20 20 20 7a 43 73 72 20 2b 3d 20 6c         zCsr += l
1dc40 65 6e 3b 0a 20 20 20 20 20 20 20 20 6c 65 6e 20  en;.        len 
1dc41 3d 20 73 71 6c 69 74 65 33 47 65 74 54 6f 6b 65  = sqlite3GetToke
1dc42 6e 28 7a 43 73 72 2c 20 26 74 6f 6b 65 6e 29 3b  n(zCsr, &token);
1dc43 0a 20 20 20 20 20 20 7d 77 68 69 6c 65 28 20 74  .      }while( t
1dc44 6f 6b 65 6e 3d 3d 54 4b 5f 53 50 41 43 45 20 29  oken==TK_SPACE )
1dc45 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
1dc46 6c 65 6e 3e 30 20 29 3b 0a 0a 20 20 20 20 20 20  len>0 );..      
1dc47 2f 2a 20 56 61 72 69 61 62 6c 65 20 27 64 69 73  /* Variable 'dis
1dc48 74 27 20 73 74 6f 72 65 73 20 74 68 65 20 6e 75  t' stores the nu
1dc49 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 72  mber of tokens r
1dc4a 65 61 64 20 73 69 6e 63 65 20 74 68 65 20 6d 6f  ead since the mo
1dc4b 73 74 0a 20 20 20 20 20 20 2a 2a 20 72 65 63 65  st.      ** rece
1dc4c 6e 74 20 54 4b 5f 44 4f 54 20 6f 72 20 54 4b 5f  nt TK_DOT or TK_
1dc4d 4f 4e 2e 20 54 68 69 73 20 6d 65 61 6e 73 20 74  ON. This means t
1dc4e 68 61 74 20 77 68 65 6e 20 61 20 57 48 45 4e 2c  hat when a WHEN,
1dc4f 20 46 4f 52 20 6f 72 20 42 45 47 49 4e 20 0a 20   FOR or BEGIN . 
1dc50 20 20 20 20 20 2a 2a 20 74 6f 6b 65 6e 20 69 73       ** token is
1dc51 20 72 65 61 64 20 61 6e 64 20 27 64 69 73 74 27   read and 'dist'
1dc52 20 65 71 75 61 6c 73 20 32 2c 20 74 68 65 20 63   equals 2, the c
1dc53 6f 6e 64 69 74 69 6f 6e 20 73 74 61 74 65 64 20  ondition stated 
1dc54 61 62 6f 76 65 0a 20 20 20 20 20 20 2a 2a 20 74  above.      ** t
1dc55 6f 20 62 65 20 6d 65 74 2e 0a 20 20 20 20 20 20  o be met..      
1dc56 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 4e 6f 74 65  **.      ** Note
1dc57 20 74 68 61 74 20 4f 4e 20 63 61 6e 6e 6f 74 20   that ON cannot 
1dc58 62 65 20 61 20 64 61 74 61 62 61 73 65 2c 20 74  be a database, t
1dc59 61 62 6c 65 20 6f 72 20 63 6f 6c 75 6d 6e 20 6e  able or column n
1dc5a 61 6d 65 2c 20 73 6f 0a 20 20 20 20 20 20 2a 2a  ame, so.      **
1dc5b 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 65   there is no nee
1dc5c 64 20 74 6f 20 77 6f 72 72 79 20 61 62 6f 75 74  d to worry about
1dc5d 20 73 79 6e 74 61 78 20 6c 69 6b 65 20 0a 20 20   syntax like .  
1dc5e 20 20 20 20 2a 2a 20 22 43 52 45 41 54 45 20 54      ** "CREATE T
1dc5f 52 49 47 47 45 52 20 2e 2e 2e 20 4f 4e 20 4f 4e  RIGGER ... ON ON
1dc60 2e 4f 4e 20 42 45 47 49 4e 20 2e 2e 2e 22 20 65  .ON BEGIN ..." e
1dc61 74 63 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  tc..      */.   
1dc62 20 20 20 64 69 73 74 2b 2b 3b 0a 20 20 20 20 20     dist++;.     
1dc63 20 69 66 28 20 74 6f 6b 65 6e 3d 3d 54 4b 5f 44   if( token==TK_D
1dc64 4f 54 20 7c 7c 20 74 6f 6b 65 6e 3d 3d 54 4b 5f  OT || token==TK_
1dc65 4f 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 64 69  ON ){.        di
1dc66 73 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  st = 0;.      }.
1dc67 20 20 20 20 7d 20 77 68 69 6c 65 28 20 64 69 73      } while( dis
1dc68 74 21 3d 32 20 7c 7c 20 28 74 6f 6b 65 6e 21 3d  t!=2 || (token!=
1dc69 54 4b 5f 57 48 45 4e 20 26 26 20 74 6f 6b 65 6e  TK_WHEN && token
1dc6a 21 3d 54 4b 5f 46 4f 52 20 26 26 20 74 6f 6b 65  !=TK_FOR && toke
1dc6b 6e 21 3d 54 4b 5f 42 45 47 49 4e 29 20 29 3b 0a  n!=TK_BEGIN) );.
1dc6c 0a 20 20 20 20 2f 2a 20 56 61 72 69 61 62 6c 65  .    /* Variable
1dc6d 20 74 6e 61 6d 65 20 6e 6f 77 20 63 6f 6e 74 61   tname now conta
1dc6e 69 6e 73 20 74 68 65 20 74 6f 6b 65 6e 20 74 68  ins the token th
1dc6f 61 74 20 69 73 20 74 68 65 20 6f 6c 64 20 74 61  at is the old ta
1dc70 62 6c 65 2d 6e 61 6d 65 0a 20 20 20 20 2a 2a 20  ble-name.    ** 
1dc71 69 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 52  in the CREATE TR
1dc72 49 47 47 45 52 20 73 74 61 74 65 6d 65 6e 74 2e  IGGER statement.
1dc73 0a 20 20 20 20 2a 2f 0a 20 20 20 20 7a 52 65 74  .    */.    zRet
1dc74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
1dc75 66 28 64 62 2c 20 22 25 2e 2a 73 5c 22 25 77 5c  f(db, "%.*s\"%w\
1dc76 22 25 73 22 2c 20 74 6e 61 6d 65 2e 7a 20 2d 20  "%s", tname.z - 
1dc77 7a 53 71 6c 2c 20 7a 53 71 6c 2c 20 0a 20 20 20  zSql, zSql, .   
1dc78 20 20 20 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20      zTableName, 
1dc79 74 6e 61 6d 65 2e 7a 2b 74 6e 61 6d 65 2e 6e 29  tname.z+tname.n)
1dc7a 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65  ;.    sqlite3_re
1dc7b 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78  sult_text(contex
1dc7c 74 2c 20 7a 52 65 74 2c 20 2d 31 2c 20 53 51 4c  t, zRet, -1, SQL
1dc7d 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
1dc7e 7d 0a 7d 0a 23 65 6e 64 69 66 20 20 20 2f 2a 20  }.}.#endif   /* 
1dc7f 21 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  !SQLITE_OMIT_TRI
1dc80 47 47 45 52 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52  GGER */../*.** R
1dc81 65 67 69 73 74 65 72 20 62 75 69 6c 74 2d 69 6e  egister built-in
1dc82 20 66 75 6e 63 74 69 6f 6e 73 20 75 73 65 64 20   functions used 
1dc83 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d 65 6e  to help implemen
1dc84 74 20 41 4c 54 45 52 20 54 41 42 4c 45 0a 2a 2f  t ALTER TABLE.*/
1dc85 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1dc86 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c 74 65  void sqlite3Alte
1dc87 72 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74  rFunctions(sqlit
1dc88 65 33 20 2a 64 62 29 7b 0a 20 20 73 71 6c 69 74  e3 *db){.  sqlit
1dc89 65 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c  e3CreateFunc(db,
1dc8a 20 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f   "sqlite_rename_
1dc8b 74 61 62 6c 65 22 2c 20 32 2c 20 53 51 4c 49 54  table", 2, SQLIT
1dc8c 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20 20  E_UTF8, 0,.     
1dc8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc8e 20 20 20 20 72 65 6e 61 6d 65 54 61 62 6c 65 46      renameTableF
1dc8f 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23 69 66 6e  unc, 0, 0);.#ifn
1dc90 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1dc91 54 52 49 47 47 45 52 0a 20 20 73 71 6c 69 74 65  TRIGGER.  sqlite
1dc92 33 43 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20  3CreateFunc(db, 
1dc93 22 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74  "sqlite_rename_t
1dc94 72 69 67 67 65 72 22 2c 20 32 2c 20 53 51 4c 49  rigger", 2, SQLI
1dc95 54 45 5f 55 54 46 38 2c 20 30 2c 0a 20 20 20 20  TE_UTF8, 0,.    
1dc96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dc97 20 20 20 20 20 72 65 6e 61 6d 65 54 72 69 67 67       renameTrigg
1dc98 65 72 46 75 6e 63 2c 20 30 2c 20 30 29 3b 0a 23  erFunc, 0, 0);.#
1dc99 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  endif.}../*.** G
1dc9a 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78 74  enerate the text
1dc9b 20 6f 66 20 61 20 57 48 45 52 45 20 65 78 70 72   of a WHERE expr
1dc9c 65 73 73 69 6f 6e 20 77 68 69 63 68 20 63 61 6e  ession which can
1dc9d 20 62 65 20 75 73 65 64 20 74 6f 20 73 65 6c 65   be used to sele
1dc9e 63 74 20 61 6c 6c 0a 2a 2a 20 74 65 6d 70 6f 72  ct all.** tempor
1dc9f 61 72 79 20 74 72 69 67 67 65 72 73 20 6f 6e 20  ary triggers on 
1dca0 74 61 62 6c 65 20 70 54 61 62 20 66 72 6f 6d 20  table pTab from 
1dca1 74 68 65 20 73 71 6c 69 74 65 5f 74 65 6d 70 5f  the sqlite_temp_
1dca2 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 20 49 66  master table. If
1dca3 0a 2a 2a 20 74 61 62 6c 65 20 70 54 61 62 20 68  .** table pTab h
1dca4 61 73 20 6e 6f 20 74 65 6d 70 6f 72 61 72 79 20  as no temporary 
1dca5 74 72 69 67 67 65 72 73 2c 20 6f 72 20 69 73 20  triggers, or is 
1dca6 69 74 73 65 6c 66 20 73 74 6f 72 65 64 20 69 6e  itself stored in
1dca7 20 74 68 65 20 0a 2a 2a 20 74 65 6d 70 6f 72 61   the .** tempora
1dca8 72 79 20 64 61 74 61 62 61 73 65 2c 20 4e 55 4c  ry database, NUL
1dca9 4c 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  L is returned..*
1dcaa 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 77  /.static char *w
1dcab 68 65 72 65 54 65 6d 70 54 72 69 67 67 65 72 73  hereTempTriggers
1dcac 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1dcad 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20  Table *pTab){.  
1dcae 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 3b 0a  Trigger *pTrig;.
1dcaf 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 20 3d    char *zWhere =
1dcb0 20 30 3b 0a 20 20 63 68 61 72 20 2a 74 6d 70 20   0;.  char *tmp 
1dcb1 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 53 63 68  = 0;.  const Sch
1dcb2 65 6d 61 20 2a 70 54 65 6d 70 53 63 68 65 6d 61  ema *pTempSchema
1dcb3 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61   = pParse->db->a
1dcb4 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 3b 20 2f  Db[1].pSchema; /
1dcb5 2a 20 54 65 6d 70 20 64 62 20 73 63 68 65 6d 61  * Temp db schema
1dcb6 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
1dcb7 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20 6c 6f   table is not lo
1dcb8 63 61 74 65 64 20 69 6e 20 74 68 65 20 74 65 6d  cated in the tem
1dcb9 70 2d 64 62 20 28 69 6e 20 77 68 69 63 68 20 63  p-db (in which c
1dcba 61 73 65 20 4e 55 4c 4c 20 69 73 20 0a 20 20 2a  ase NULL is .  *
1dcbb 2a 20 72 65 74 75 72 6e 65 64 2c 20 6c 6f 6f 70  * returned, loop
1dcbc 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62   through the tab
1dcbd 6c 65 73 20 6c 69 73 74 20 6f 66 20 74 72 69 67  les list of trig
1dcbe 67 65 72 73 2e 20 46 6f 72 20 65 61 63 68 20 74  gers. For each t
1dcbf 72 69 67 67 65 72 0a 20 20 2a 2a 20 74 68 61 74  rigger.  ** that
1dcc0 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20   is not part of 
1dcc1 74 68 65 20 74 65 6d 70 2d 64 62 20 73 63 68 65  the temp-db sche
1dcc2 6d 61 2c 20 61 64 64 20 61 20 63 6c 61 75 73 65  ma, add a clause
1dcc3 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 0a 20   to the WHERE . 
1dcc4 20 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 20 62   ** expression b
1dcc5 65 69 6e 67 20 62 75 69 6c 74 20 75 70 20 69 6e  eing built up in
1dcc6 20 7a 57 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20   zWhere..  */.  
1dcc7 69 66 28 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  if( pTab->pSchem
1dcc8 61 21 3d 70 54 65 6d 70 53 63 68 65 6d 61 20 29  a!=pTempSchema )
1dcc9 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64  {.    sqlite3 *d
1dcca 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1dccb 20 20 20 20 66 6f 72 28 20 70 54 72 69 67 3d 70      for( pTrig=p
1dccc 54 61 62 2d 3e 70 54 72 69 67 67 65 72 3b 20 70  Tab->pTrigger; p
1dccd 54 72 69 67 3b 20 70 54 72 69 67 3d 70 54 72 69  Trig; pTrig=pTri
1dcce 67 2d 3e 70 4e 65 78 74 20 29 7b 0a 20 20 20 20  g->pNext ){.    
1dccf 20 20 69 66 28 20 70 54 72 69 67 2d 3e 70 53 63    if( pTrig->pSc
1dcd0 68 65 6d 61 3d 3d 70 54 65 6d 70 53 63 68 65 6d  hema==pTempSchem
1dcd1 61 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28  a ){.        if(
1dcd2 20 21 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20   !zWhere ){.    
1dcd3 20 20 20 20 20 20 7a 57 68 65 72 65 20 3d 20 73        zWhere = s
1dcd4 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
1dcd5 2c 20 22 6e 61 6d 65 3d 25 51 22 2c 20 70 54 72  , "name=%Q", pTr
1dcd6 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20 20 20 20 20  ig->name);.     
1dcd7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1dcd8 20 20 20 20 74 6d 70 20 3d 20 7a 57 68 65 72 65      tmp = zWhere
1dcd9 3b 0a 20 20 20 20 20 20 20 20 20 20 7a 57 68 65  ;.          zWhe
1dcda 72 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  re = sqlite3MPri
1dcdb 6e 74 66 28 64 62 2c 20 22 25 73 20 4f 52 20 6e  ntf(db, "%s OR n
1dcdc 61 6d 65 3d 25 51 22 2c 20 7a 57 68 65 72 65 2c  ame=%Q", zWhere,
1dcdd 20 70 54 72 69 67 2d 3e 6e 61 6d 65 29 3b 0a 20   pTrig->name);. 
1dcde 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
1dcdf 44 62 46 72 65 65 28 64 62 2c 20 74 6d 70 29 3b  DbFree(db, tmp);
1dce0 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
1dce1 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72   }.    }.  }.  r
1dce2 65 74 75 72 6e 20 7a 57 68 65 72 65 3b 0a 7d 0a  eturn zWhere;.}.
1dce3 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1dce4 63 6f 64 65 20 74 6f 20 64 72 6f 70 20 61 6e 64  code to drop and
1dce5 20 72 65 6c 6f 61 64 20 74 68 65 20 69 6e 74 65   reload the inte
1dce6 72 6e 61 6c 20 72 65 70 72 65 73 65 6e 74 61 74  rnal representat
1dce7 69 6f 6e 20 6f 66 20 74 61 62 6c 65 0a 2a 2a 20  ion of table.** 
1dce8 70 54 61 62 20 66 72 6f 6d 20 74 68 65 20 64 61  pTab from the da
1dce9 74 61 62 61 73 65 2c 20 69 6e 63 6c 75 64 69 6e  tabase, includin
1dcea 67 20 74 72 69 67 67 65 72 73 20 61 6e 64 20 74  g triggers and t
1dceb 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65 72  emporary trigger
1dcec 73 2e 0a 2a 2a 20 41 72 67 75 6d 65 6e 74 20 7a  s..** Argument z
1dced 4e 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65  Name is the name
1dcee 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 69 6e   of the table in
1dcef 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63   the database sc
1dcf0 68 65 6d 61 20 61 74 0a 2a 2a 20 74 68 65 20 74  hema at.** the t
1dcf1 69 6d 65 20 74 68 65 20 67 65 6e 65 72 61 74 65  ime the generate
1dcf2 64 20 63 6f 64 65 20 69 73 20 65 78 65 63 75 74  d code is execut
1dcf3 65 64 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  ed. This can be 
1dcf4 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 2a  different from.*
1dcf5 2a 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 69 66  * pTab->zName if
1dcf6 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69   this function i
1dcf7 73 20 62 65 69 6e 67 20 63 61 6c 6c 65 64 20 74  s being called t
1dcf8 6f 20 63 6f 64 65 20 70 61 72 74 20 6f 66 20 61  o code part of a
1dcf9 6e 20 0a 2a 2a 20 22 41 4c 54 45 52 20 54 41 42  n .** "ALTER TAB
1dcfa 4c 45 20 52 45 4e 41 4d 45 20 54 4f 22 20 73 74  LE RENAME TO" st
1dcfb 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61 74  atement..*/.stat
1dcfc 69 63 20 76 6f 69 64 20 72 65 6c 6f 61 64 54 61  ic void reloadTa
1dcfd 62 6c 65 53 63 68 65 6d 61 28 50 61 72 73 65 20  bleSchema(Parse 
1dcfe 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
1dcff 70 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72  pTab, const char
1dd00 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 56 64 62 65   *zName){.  Vdbe
1dd01 20 2a 76 3b 0a 20 20 63 68 61 72 20 2a 7a 57 68   *v;.  char *zWh
1dd02 65 72 65 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20  ere;.  int iDb; 
1dd03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd04 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61    /* Index of da
1dd05 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
1dd06 67 20 70 54 61 62 20 2a 2f 0a 23 69 66 6e 64 65  g pTab */.#ifnde
1dd07 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52  f SQLITE_OMIT_TR
1dd08 49 47 47 45 52 0a 20 20 54 72 69 67 67 65 72 20  IGGER.  Trigger 
1dd09 2a 70 54 72 69 67 3b 0a 23 65 6e 64 69 66 0a 0a  *pTrig;.#endif..
1dd0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
1dd0b 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
1dd0c 69 66 28 20 21 76 20 29 20 72 65 74 75 72 6e 3b  if( !v ) return;
1dd0d 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1dd0e 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
1dd0f 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
1dd10 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  b) );.  iDb = sq
1dd11 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1dd12 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
1dd13 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1dd14 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1dd15 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  );..#ifndef SQLI
1dd16 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1dd17 20 20 2f 2a 20 44 72 6f 70 20 61 6e 79 20 74 61    /* Drop any ta
1dd18 62 6c 65 20 74 72 69 67 67 65 72 73 20 66 72 6f  ble triggers fro
1dd19 6d 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  m the internal s
1dd1a 63 68 65 6d 61 2e 20 2a 2f 0a 20 20 66 6f 72 28  chema. */.  for(
1dd1b 70 54 72 69 67 3d 70 54 61 62 2d 3e 70 54 72 69  pTrig=pTab->pTri
1dd1c 67 67 65 72 3b 20 70 54 72 69 67 3b 20 70 54 72  gger; pTrig; pTr
1dd1d 69 67 3d 70 54 72 69 67 2d 3e 70 4e 65 78 74 29  ig=pTrig->pNext)
1dd1e 7b 0a 20 20 20 20 69 6e 74 20 69 54 72 69 67 44  {.    int iTrigD
1dd1f 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1dd20 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
1dd21 3e 64 62 2c 20 70 54 72 69 67 2d 3e 70 53 63 68  >db, pTrig->pSch
1dd22 65 6d 61 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ema);.    assert
1dd23 28 20 69 54 72 69 67 44 62 3d 3d 69 44 62 20 7c  ( iTrigDb==iDb |
1dd24 7c 20 69 54 72 69 67 44 62 3d 3d 31 20 29 3b 0a  | iTrigDb==1 );.
1dd25 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1dd26 64 64 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70  ddOp4(v, OP_Drop
1dd27 54 72 69 67 67 65 72 2c 20 69 54 72 69 67 44 62  Trigger, iTrigDb
1dd28 2c 20 30 2c 20 30 2c 20 70 54 72 69 67 2d 3e 6e  , 0, 0, pTrig->n
1dd29 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 23 65 6e  ame, 0);.  }.#en
1dd2a 64 69 66 0a 0a 20 20 2f 2a 20 44 72 6f 70 20 74  dif..  /* Drop t
1dd2b 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64  he table and ind
1dd2c 65 78 20 66 72 6f 6d 20 74 68 65 20 69 6e 74 65  ex from the inte
1dd2d 72 6e 61 6c 20 73 63 68 65 6d 61 20 2a 2f 0a 20  rnal schema */. 
1dd2e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1dd2f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61 62  p4(v, OP_DropTab
1dd30 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20 70  le, iDb, 0, 0, p
1dd31 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  Tab->zName, 0);.
1dd32 0a 20 20 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65  .  /* Reload the
1dd33 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 20 61 6e   table, index an
1dd34 64 20 70 65 72 6d 61 6e 65 6e 74 20 74 72 69 67  d permanent trig
1dd35 67 65 72 20 73 63 68 65 6d 61 73 2e 20 2a 2f 0a  ger schemas. */.
1dd36 20 20 7a 57 68 65 72 65 20 3d 20 73 71 6c 69 74    zWhere = sqlit
1dd37 65 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65  e3MPrintf(pParse
1dd38 2d 3e 64 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d  ->db, "tbl_name=
1dd39 25 51 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 69  %Q", zName);.  i
1dd3a 66 28 20 21 7a 57 68 65 72 65 20 29 20 72 65 74  f( !zWhere ) ret
1dd3b 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  urn;.  sqlite3Vd
1dd3c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 50  beAddOp4(v, OP_P
1dd3d 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62 2c  arseSchema, iDb,
1dd3e 20 30 2c 20 30 2c 20 7a 57 68 65 72 65 2c 20 50   0, 0, zWhere, P
1dd3f 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 0a 23 69 66  4_DYNAMIC);..#if
1dd40 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1dd41 5f 54 52 49 47 47 45 52 0a 20 20 2f 2a 20 4e 6f  _TRIGGER.  /* No
1dd42 77 2c 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  w, if the table 
1dd43 69 73 20 6e 6f 74 20 73 74 6f 72 65 64 20 69 6e  is not stored in
1dd44 20 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61   the temp databa
1dd45 73 65 2c 20 72 65 6c 6f 61 64 20 61 6e 79 20 74  se, reload any t
1dd46 65 6d 70 20 0a 20 20 2a 2a 20 74 72 69 67 67 65  emp .  ** trigge
1dd47 72 73 2e 20 44 6f 6e 27 74 20 75 73 65 20 49 4e  rs. Don't use IN
1dd48 28 2e 2e 2e 29 20 69 6e 20 63 61 73 65 20 53 51  (...) in case SQ
1dd49 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45  LITE_OMIT_SUBQUE
1dd4a 52 59 20 69 73 20 64 65 66 69 6e 65 64 2e 20 0a  RY is defined. .
1dd4b 20 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65    */.  if( (zWhe
1dd4c 72 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67  re=whereTempTrig
1dd4d 67 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61  gers(pParse, pTa
1dd4e 62 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  b))!=0 ){.    sq
1dd4f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
1dd50 76 2c 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d  v, OP_ParseSchem
1dd51 61 2c 20 31 2c 20 30 2c 20 30 2c 20 7a 57 68 65  a, 1, 0, 0, zWhe
1dd52 72 65 2c 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b  re, P4_DYNAMIC);
1dd53 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f  .  }.#endif.}../
1dd54 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1dd55 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
1dd56 74 68 65 20 22 41 4c 54 45 52 20 54 41 42 4c 45  the "ALTER TABLE
1dd57 20 78 78 78 20 52 45 4e 41 4d 45 20 54 4f 20 79   xxx RENAME TO y
1dd58 79 79 22 20 0a 2a 2a 20 63 6f 6d 6d 61 6e 64 2e  yy" .** command.
1dd59 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56   .*/.SQLITE_PRIV
1dd5a 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1dd5b 41 6c 74 65 72 52 65 6e 61 6d 65 54 61 62 6c 65  AlterRenameTable
1dd5c 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
1dd5d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  e,            /*
1dd5e 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e   Parser context.
1dd5f 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   */.  SrcList *p
1dd60 53 72 63 2c 20 20 20 20 20 20 20 20 20 20 20 20  Src,            
1dd61 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f 20  /* The table to 
1dd62 72 65 6e 61 6d 65 2e 20 2a 2f 0a 20 20 54 6f 6b  rename. */.  Tok
1dd63 65 6e 20 2a 70 4e 61 6d 65 20 20 20 20 20 20 20  en *pName       
1dd64 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
1dd65 77 20 74 61 62 6c 65 20 6e 61 6d 65 2e 20 2a 2f  w table name. */
1dd66 0a 29 7b 0a 20 20 69 6e 74 20 69 44 62 3b 20 20  .){.  int iDb;  
1dd67 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1dd68 2f 2a 20 44 61 74 61 62 61 73 65 20 74 68 61 74  /* Database that
1dd69 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 61   contains the ta
1dd6a 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a  ble */.  char *z
1dd6b 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
1dd6c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 64 61     /* Name of da
1dd6d 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
1dd6e 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20  Table *pTab;    
1dd6f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62            /* Tab
1dd70 6c 65 20 62 65 69 6e 67 20 72 65 6e 61 6d 65 64  le being renamed
1dd71 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d   */.  char *zNam
1dd72 65 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  e = 0;          
1dd73 2f 2a 20 4e 55 4c 4c 2d 74 65 72 6d 69 6e 61 74  /* NULL-terminat
1dd74 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 70 4e  ed version of pN
1dd75 61 6d 65 20 2a 2f 20 0a 20 20 73 71 6c 69 74 65  ame */ .  sqlite
1dd76 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1dd77 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65 20  db; /* Database 
1dd78 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
1dd79 69 6e 74 20 6e 54 61 62 4e 61 6d 65 3b 20 20 20  int nTabName;   
1dd7a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
1dd7b 62 65 72 20 6f 66 20 55 54 46 2d 38 20 63 68 61  ber of UTF-8 cha
1dd7c 72 61 63 74 65 72 73 20 69 6e 20 7a 54 61 62 4e  racters in zTabN
1dd7d 61 6d 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ame */.  const c
1dd7e 68 61 72 20 2a 7a 54 61 62 4e 61 6d 65 3b 20 20  har *zTabName;  
1dd7f 20 20 20 2f 2a 20 4f 72 69 67 69 6e 61 6c 20 6e     /* Original n
1dd80 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1dd81 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 0a 23   */.  Vdbe *v;.#
1dd82 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1dd83 49 54 5f 54 52 49 47 47 45 52 0a 20 20 63 68 61  IT_TRIGGER.  cha
1dd84 72 20 2a 7a 57 68 65 72 65 20 3d 20 30 3b 20 20  r *zWhere = 0;  
1dd85 20 20 20 20 20 20 20 2f 2a 20 57 68 65 72 65 20         /* Where 
1dd86 63 6c 61 75 73 65 20 74 6f 20 6c 6f 63 61 74 65  clause to locate
1dd87 20 74 65 6d 70 20 74 72 69 67 67 65 72 73 20 2a   temp triggers *
1dd88 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e 74 20 69  /.#endif.  int i
1dd89 73 56 69 72 74 75 61 6c 52 65 6e 61 6d 65 20 3d  sVirtualRename =
1dd8a 20 30 3b 20 20 2f 2a 20 54 72 75 65 20 69 66 20   0;  /* True if 
1dd8b 74 68 69 73 20 69 73 20 61 20 76 2d 74 61 62 6c  this is a v-tabl
1dd8c 65 20 77 69 74 68 20 61 6e 20 78 52 65 6e 61 6d  e with an xRenam
1dd8d 65 28 29 20 2a 2f 0a 20 20 0a 20 20 69 66 28 20  e() */.  .  if( 
1dd8e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
1dd8f 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e   ) goto exit_ren
1dd90 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 61 73 73  ame_table;.  ass
1dd91 65 72 74 28 20 70 53 72 63 2d 3e 6e 53 72 63 3d  ert( pSrc->nSrc=
1dd92 3d 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =1 );.  assert( 
1dd93 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
1dd94 73 41 6c 6c 4d 75 74 65 78 65 73 28 70 50 61 72  sAllMutexes(pPar
1dd95 73 65 2d 3e 64 62 29 20 29 3b 0a 0a 20 20 70 54  se->db) );..  pT
1dd96 61 62 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61  ab = sqlite3Loca
1dd97 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20  teTable(pParse, 
1dd98 30 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e 7a 4e  0, pSrc->a[0].zN
1dd99 61 6d 65 2c 20 70 53 72 63 2d 3e 61 5b 30 5d 2e  ame, pSrc->a[0].
1dd9a 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 69 66  zDatabase);.  if
1dd9b 28 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65  ( !pTab ) goto e
1dd9c 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65  xit_rename_table
1dd9d 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
1dd9e 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
1dd9f 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
1dda0 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 7a 44 62  >pSchema);.  zDb
1dda1 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
1dda2 7a 4e 61 6d 65 3b 0a 0a 20 20 2f 2a 20 47 65 74  zName;..  /* Get
1dda3 20 61 20 4e 55 4c 4c 20 74 65 72 6d 69 6e 61 74   a NULL terminat
1dda4 65 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 74 68  ed version of th
1dda5 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61 6d 65  e new table name
1dda6 2e 20 2a 2f 0a 20 20 7a 4e 61 6d 65 20 3d 20 73  . */.  zName = s
1dda7 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1dda8 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a  ken(db, pName);.
1dda9 20 20 69 66 28 20 21 7a 4e 61 6d 65 20 29 20 67    if( !zName ) g
1ddaa 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
1ddab 74 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 43 68 65  table;..  /* Che
1ddac 63 6b 20 74 68 61 74 20 61 20 74 61 62 6c 65 20  ck that a table 
1ddad 6f 72 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 27  or index named '
1ddae 7a 4e 61 6d 65 27 20 64 6f 65 73 20 6e 6f 74 20  zName' does not 
1ddaf 61 6c 72 65 61 64 79 20 65 78 69 73 74 0a 20 20  already exist.  
1ddb0 2a 2a 20 69 6e 20 64 61 74 61 62 61 73 65 20 69  ** in database i
1ddb1 44 62 2e 20 49 66 20 73 6f 2c 20 74 68 69 73 20  Db. If so, this 
1ddb2 69 73 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 2a  is an error..  *
1ddb3 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 46  /.  if( sqlite3F
1ddb4 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 4e 61  indTable(db, zNa
1ddb5 6d 65 2c 20 7a 44 62 29 20 7c 7c 20 73 71 6c 69  me, zDb) || sqli
1ddb6 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c  te3FindIndex(db,
1ddb7 20 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a   zName, zDb) ){.
1ddb8 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1ddb9 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20  Msg(pParse, .   
1ddba 20 20 20 20 20 22 74 68 65 72 65 20 69 73 20 61       "there is a
1ddbb 6c 72 65 61 64 79 20 61 6e 6f 74 68 65 72 20 74  lready another t
1ddbc 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 20 77 69  able or index wi
1ddbd 74 68 20 74 68 69 73 20 6e 61 6d 65 3a 20 25 73  th this name: %s
1ddbe 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  ", zName);.    g
1ddbf 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f  oto exit_rename_
1ddc0 74 61 62 6c 65 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  table;.  }..  /*
1ddc1 20 4d 61 6b 65 20 73 75 72 65 20 69 74 20 69 73   Make sure it is
1ddc2 20 6e 6f 74 20 61 20 73 79 73 74 65 6d 20 74 61   not a system ta
1ddc3 62 6c 65 20 62 65 69 6e 67 20 61 6c 74 65 72 65  ble being altere
1ddc4 64 2c 20 6f 72 20 61 20 72 65 73 65 72 76 65 64  d, or a reserved
1ddc5 20 6e 61 6d 65 0a 20 20 2a 2a 20 74 68 61 74 20   name.  ** that 
1ddc6 74 68 65 20 74 61 62 6c 65 20 69 73 20 62 65 69  the table is bei
1ddc7 6e 67 20 72 65 6e 61 6d 65 64 20 74 6f 2e 0a 20  ng renamed to.. 
1ddc8 20 2a 2f 0a 20 20 69 66 28 20 73 74 72 6c 65 6e   */.  if( strlen
1ddc9 28 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3e 36 20  (pTab->zName)>6 
1ddca 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72  && 0==sqlite3Str
1ddcb 4e 49 43 6d 70 28 70 54 61 62 2d 3e 7a 4e 61 6d  NICmp(pTab->zNam
1ddcc 65 2c 20 22 73 71 6c 69 74 65 5f 22 2c 20 37 29  e, "sqlite_", 7)
1ddcd 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
1ddce 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
1ddcf 22 74 61 62 6c 65 20 25 73 20 6d 61 79 20 6e 6f  "table %s may no
1ddd0 74 20 62 65 20 61 6c 74 65 72 65 64 22 2c 20 70  t be altered", p
1ddd1 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  Tab->zName);.   
1ddd2 20 67 6f 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d   goto exit_renam
1ddd3 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69  e_table;.  }.  i
1ddd4 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1ddd5 6c 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74  lite3CheckObject
1ddd6 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 7a 4e 61  Name(pParse, zNa
1ddd7 6d 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  me) ){.    goto 
1ddd8 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
1ddd9 65 3b 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20  e;.  }..#ifndef 
1ddda 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1dddb 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
1dddc 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
1dddd 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1ddde 73 65 2c 20 22 76 69 65 77 20 25 73 20 6d 61 79  se, "view %s may
1dddf 20 6e 6f 74 20 62 65 20 61 6c 74 65 72 65 64 22   not be altered"
1dde0 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pTab->zName);.
1dde1 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
1dde2 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
1dde3 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1dde4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
1dde5 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49  ORIZATION.  /* I
1dde6 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68 6f 72  nvoke the author
1dde7 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
1dde8 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74  . */.  if( sqlit
1dde9 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
1ddea 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54 45 52  se, SQLITE_ALTER
1ddeb 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70 54 61  _TABLE, zDb, pTa
1ddec 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29 7b 0a  b->zName, 0) ){.
1dded 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 72 65      goto exit_re
1ddee 6e 61 6d 65 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a  name_table;.  }.
1ddef 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64 65 66 20  #endif..#ifndef 
1ddf0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1ddf1 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 73  UALTABLE.  if( s
1ddf2 71 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c  qlite3ViewGetCol
1ddf3 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c  umnNames(pParse,
1ddf4 20 70 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f   pTab) ){.    go
1ddf5 74 6f 20 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74  to exit_rename_t
1ddf6 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66 28 20  able;.  }.  if( 
1ddf7 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
1ddf8 26 26 20 70 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70  && pTab->pMod->p
1ddf9 4d 6f 64 75 6c 65 2d 3e 78 52 65 6e 61 6d 65 20  Module->xRename 
1ddfa 29 7b 0a 20 20 20 20 69 73 56 69 72 74 75 61 6c  ){.    isVirtual
1ddfb 52 65 6e 61 6d 65 20 3d 20 31 3b 0a 20 20 7d 0a  Rename = 1;.  }.
1ddfc 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 42 65 67  #endif..  /* Beg
1ddfd 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
1ddfe 20 61 6e 64 20 63 6f 64 65 20 74 68 65 20 56 65   and code the Ve
1ddff 72 69 66 79 43 6f 6f 6b 69 65 20 66 6f 72 20 64  rifyCookie for d
1de00 61 74 61 62 61 73 65 20 69 44 62 2e 20 0a 20 20  atabase iDb. .  
1de01 2a 2a 20 54 68 65 6e 20 6d 6f 64 69 66 79 20 74  ** Then modify t
1de02 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
1de03 20 28 73 69 6e 63 65 20 74 68 65 20 41 4c 54 45   (since the ALTE
1de04 52 20 54 41 42 4c 45 20 6d 6f 64 69 66 69 65 73  R TABLE modifies
1de05 20 74 68 65 0a 20 20 2a 2a 20 73 63 68 65 6d 61   the.  ** schema
1de06 29 2e 20 4f 70 65 6e 20 61 20 73 74 61 74 65 6d  ). Open a statem
1de07 65 6e 74 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  ent transaction 
1de08 69 66 20 74 68 65 20 74 61 62 6c 65 20 69 73 20  if the table is 
1de09 61 20 76 69 72 74 75 61 6c 0a 20 20 2a 2a 20 74  a virtual.  ** t
1de0a 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 76 20 3d  able..  */.  v =
1de0b 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
1de0c 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
1de0d 3d 3d 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  ==0 ){.    goto 
1de0e 65 78 69 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c  exit_rename_tabl
1de0f 65 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  e;.  }.  sqlite3
1de10 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
1de11 69 6f 6e 28 70 50 61 72 73 65 2c 20 69 73 56 69  ion(pParse, isVi
1de12 72 74 75 61 6c 52 65 6e 61 6d 65 2c 20 69 44 62  rtualRename, iDb
1de13 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 68 61 6e  );.  sqlite3Chan
1de14 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
1de15 20 69 44 62 29 3b 0a 0a 20 20 2f 2a 20 49 66 20   iDb);..  /* If 
1de16 74 68 69 73 20 69 73 20 61 20 76 69 72 74 75 61  this is a virtua
1de17 6c 20 74 61 62 6c 65 2c 20 69 6e 76 6f 6b 65 20  l table, invoke 
1de18 74 68 65 20 78 52 65 6e 61 6d 65 28 29 20 66 75  the xRename() fu
1de19 6e 63 74 69 6f 6e 20 69 66 0a 20 20 2a 2a 20 6f  nction if.  ** o
1de1a 6e 65 20 69 73 20 64 65 66 69 6e 65 64 2e 20 54  ne is defined. T
1de1b 68 65 20 78 52 65 6e 61 6d 65 28 29 20 63 61 6c  he xRename() cal
1de1c 6c 62 61 63 6b 20 77 69 6c 6c 20 6d 6f 64 69 66  lback will modif
1de1d 79 20 74 68 65 20 6e 61 6d 65 73 0a 20 20 2a 2a  y the names.  **
1de1e 20 6f 66 20 61 6e 79 20 72 65 73 6f 75 72 63 65   of any resource
1de1f 73 20 75 73 65 64 20 62 79 20 74 68 65 20 76 2d  s used by the v-
1de20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
1de21 74 69 6f 6e 20 28 69 6e 63 6c 75 64 69 6e 67 20  tion (including 
1de22 6f 74 68 65 72 0a 20 20 2a 2a 20 53 51 4c 69 74  other.  ** SQLit
1de23 65 20 74 61 62 6c 65 73 29 20 74 68 61 74 20 61  e tables) that a
1de24 72 65 20 69 64 65 6e 74 69 66 69 65 64 20 62 79  re identified by
1de25 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
1de26 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
1de27 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c    */.#ifndef SQL
1de28 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1de29 54 41 42 4c 45 0a 20 20 69 66 28 20 69 73 56 69  TABLE.  if( isVi
1de2a 72 74 75 61 6c 52 65 6e 61 6d 65 20 29 7b 0a 20  rtualRename ){. 
1de2b 20 20 20 69 6e 74 20 69 20 3d 20 2b 2b 70 50 61     int i = ++pPa
1de2c 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73  rse->nMem;.    s
1de2d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
1de2e 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
1de2f 30 2c 20 69 2c 20 30 2c 20 7a 4e 61 6d 65 2c 20  0, i, 0, zName, 
1de30 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1de31 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1de32 56 52 65 6e 61 6d 65 2c 20 69 2c 20 30 2c 20 30  VRename, i, 0, 0
1de33 2c 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 70 54  ,(const char*)pT
1de34 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f 56 54  ab->pVtab, P4_VT
1de35 41 42 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  AB);.  }.#endif.
1de36 0a 20 20 2f 2a 20 66 69 67 75 72 65 20 6f 75 74  .  /* figure out
1de37 20 68 6f 77 20 6d 61 6e 79 20 55 54 46 2d 38 20   how many UTF-8 
1de38 63 68 61 72 61 63 74 65 72 73 20 61 72 65 20 69  characters are i
1de39 6e 20 7a 4e 61 6d 65 20 2a 2f 0a 20 20 7a 54 61  n zName */.  zTa
1de3a 62 4e 61 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e  bName = pTab->zN
1de3b 61 6d 65 3b 0a 20 20 6e 54 61 62 4e 61 6d 65 20  ame;.  nTabName 
1de3c 3d 20 73 71 6c 69 74 65 33 55 74 66 38 43 68 61  = sqlite3Utf8Cha
1de3d 72 4c 65 6e 28 7a 54 61 62 4e 61 6d 65 2c 20 2d  rLen(zTabName, -
1de3e 31 29 3b 0a 0a 20 20 2f 2a 20 4d 6f 64 69 66 79  1);..  /* Modify
1de3f 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
1de40 65 72 20 74 61 62 6c 65 20 74 6f 20 75 73 65 20  er table to use 
1de41 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e 61  the new table na
1de42 6d 65 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  me. */.  sqlite3
1de43 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72  NestedParse(pPar
1de44 73 65 2c 0a 20 20 20 20 20 20 22 55 50 44 41 54  se,.      "UPDAT
1de45 45 20 25 51 2e 25 73 20 53 45 54 20 22 0a 23 69  E %Q.%s SET ".#i
1de46 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
1de47 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20 20  _TRIGGER.       
1de48 20 20 20 22 73 71 6c 20 3d 20 73 71 6c 69 74 65     "sql = sqlite
1de49 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 28 73 71  _rename_table(sq
1de4a 6c 2c 20 25 51 29 2c 20 22 0a 23 65 6c 73 65 0a  l, %Q), ".#else.
1de4b 20 20 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d            "sql =
1de4c 20 43 41 53 45 20 22 0a 20 20 20 20 20 20 20 20   CASE ".        
1de4d 20 20 20 20 22 57 48 45 4e 20 74 79 70 65 20 3d      "WHEN type =
1de4e 20 27 74 72 69 67 67 65 72 27 20 54 48 45 4e 20   'trigger' THEN 
1de4f 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f 74 72  sqlite_rename_tr
1de50 69 67 67 65 72 28 73 71 6c 2c 20 25 51 29 22 0a  igger(sql, %Q)".
1de51 20 20 20 20 20 20 20 20 20 20 20 20 22 45 4c 53              "ELS
1de52 45 20 73 71 6c 69 74 65 5f 72 65 6e 61 6d 65 5f  E sqlite_rename_
1de53 74 61 62 6c 65 28 73 71 6c 2c 20 25 51 29 20 45  table(sql, %Q) E
1de54 4e 44 2c 20 22 0a 23 65 6e 64 69 66 0a 20 20 20  ND, ".#endif.   
1de55 20 20 20 20 20 20 20 22 74 62 6c 5f 6e 61 6d 65         "tbl_name
1de56 20 3d 20 25 51 2c 20 22 0a 20 20 20 20 20 20 20   = %Q, ".       
1de57 20 20 20 22 6e 61 6d 65 20 3d 20 43 41 53 45 20     "name = CASE 
1de58 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 57  ".            "W
1de59 48 45 4e 20 74 79 70 65 3d 27 74 61 62 6c 65 27  HEN type='table'
1de5a 20 54 48 45 4e 20 25 51 20 22 0a 20 20 20 20 20   THEN %Q ".     
1de5b 20 20 20 20 20 20 20 22 57 48 45 4e 20 6e 61 6d         "WHEN nam
1de5c 65 20 4c 49 4b 45 20 27 73 71 6c 69 74 65 5f 61  e LIKE 'sqlite_a
1de5d 75 74 6f 69 6e 64 65 78 25 25 27 20 41 4e 44 20  utoindex%%' AND 
1de5e 74 79 70 65 3d 27 69 6e 64 65 78 27 20 54 48 45  type='index' THE
1de5f 4e 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  N ".            
1de60 20 22 27 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e   "'sqlite_autoin
1de61 64 65 78 5f 27 20 7c 7c 20 25 51 20 7c 7c 20 73  dex_' || %Q || s
1de62 75 62 73 74 72 28 6e 61 6d 65 2c 25 64 2b 31 38  ubstr(name,%d+18
1de63 29 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  ) ".            
1de64 22 45 4c 53 45 20 6e 61 6d 65 20 45 4e 44 20 22  "ELSE name END "
1de65 0a 20 20 20 20 20 20 22 57 48 45 52 45 20 74 62  .      "WHERE tb
1de66 6c 5f 6e 61 6d 65 3d 25 51 20 41 4e 44 20 22 0a  l_name=%Q AND ".
1de67 20 20 20 20 20 20 20 20 20 20 22 28 74 79 70 65            "(type
1de68 3d 27 74 61 62 6c 65 27 20 4f 52 20 74 79 70 65  ='table' OR type
1de69 3d 27 69 6e 64 65 78 27 20 4f 52 20 74 79 70 65  ='index' OR type
1de6a 3d 27 74 72 69 67 67 65 72 27 29 3b 22 2c 20 0a  ='trigger');", .
1de6b 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45 4d        zDb, SCHEM
1de6c 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 7a 4e  A_TABLE(iDb), zN
1de6d 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 4e 61 6d  ame, zName, zNam
1de6e 65 2c 20 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  e, .#ifndef SQLI
1de6f 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1de70 20 20 20 20 20 20 7a 4e 61 6d 65 2c 0a 23 65 6e        zName,.#en
1de71 64 69 66 0a 20 20 20 20 20 20 7a 4e 61 6d 65 2c  dif.      zName,
1de72 20 6e 54 61 62 4e 61 6d 65 2c 20 7a 54 61 62 4e   nTabName, zTabN
1de73 61 6d 65 0a 20 20 29 3b 0a 0a 23 69 66 6e 64 65  ame.  );..#ifnde
1de74 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1de75 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 2f 2a  TOINCREMENT.  /*
1de76 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73   If the sqlite_s
1de77 65 71 75 65 6e 63 65 20 74 61 62 6c 65 20 65 78  equence table ex
1de78 69 73 74 73 20 69 6e 20 74 68 69 73 20 64 61 74  ists in this dat
1de79 61 62 61 73 65 2c 20 74 68 65 6e 20 75 70 64 61  abase, then upda
1de7a 74 65 20 0a 20 20 2a 2a 20 69 74 20 77 69 74 68  te .  ** it with
1de7b 20 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 6e   the new table n
1de7c 61 6d 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ame..  */.  if( 
1de7d 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
1de7e 28 64 62 2c 20 22 73 71 6c 69 74 65 5f 73 65 71  (db, "sqlite_seq
1de7f 75 65 6e 63 65 22 2c 20 7a 44 62 29 20 29 7b 0a  uence", zDb) ){.
1de80 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
1de81 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
1de82 20 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c         "UPDATE \
1de83 22 25 77 5c 22 2e 73 71 6c 69 74 65 5f 73 65 71  "%w\".sqlite_seq
1de84 75 65 6e 63 65 20 73 65 74 20 6e 61 6d 65 20 3d  uence set name =
1de85 20 25 51 20 57 48 45 52 45 20 6e 61 6d 65 20 3d   %Q WHERE name =
1de86 20 25 51 22 2c 0a 20 20 20 20 20 20 20 20 7a 44   %Q",.        zD
1de87 62 2c 20 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e  b, zName, pTab->
1de88 7a 4e 61 6d 65 29 3b 0a 20 20 7d 0a 23 65 6e 64  zName);.  }.#end
1de89 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
1de8a 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a  TE_OMIT_TRIGGER.
1de8b 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72    /* If there ar
1de8c 65 20 54 45 4d 50 20 74 72 69 67 67 65 72 73 20  e TEMP triggers 
1de8d 6f 6e 20 74 68 69 73 20 74 61 62 6c 65 2c 20 6d  on this table, m
1de8e 6f 64 69 66 79 20 74 68 65 20 73 71 6c 69 74 65  odify the sqlite
1de8f 5f 74 65 6d 70 5f 6d 61 73 74 65 72 0a 20 20 2a  _temp_master.  *
1de90 2a 20 74 61 62 6c 65 2e 20 44 6f 6e 27 74 20 64  * table. Don't d
1de91 6f 20 74 68 69 73 20 69 66 20 74 68 65 20 74 61  o this if the ta
1de92 62 6c 65 20 62 65 69 6e 67 20 41 4c 54 45 52 65  ble being ALTERe
1de93 64 20 69 73 20 69 74 73 65 6c 66 20 6c 6f 63 61  d is itself loca
1de94 74 65 64 20 69 6e 0a 20 20 2a 2a 20 74 68 65 20  ted in.  ** the 
1de95 74 65 6d 70 20 64 61 74 61 62 61 73 65 2e 0a 20  temp database.. 
1de96 20 2a 2f 0a 20 20 69 66 28 20 28 7a 57 68 65 72   */.  if( (zWher
1de97 65 3d 77 68 65 72 65 54 65 6d 70 54 72 69 67 67  e=whereTempTrigg
1de98 65 72 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ers(pParse, pTab
1de99 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  ))!=0 ){.    sql
1de9a 69 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28  ite3NestedParse(
1de9b 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20  pParse, .       
1de9c 20 22 55 50 44 41 54 45 20 73 71 6c 69 74 65 5f   "UPDATE sqlite_
1de9d 74 65 6d 70 5f 6d 61 73 74 65 72 20 53 45 54 20  temp_master SET 
1de9e 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 73  ".            "s
1de9f 71 6c 20 3d 20 73 71 6c 69 74 65 5f 72 65 6e 61  ql = sqlite_rena
1dea0 6d 65 5f 74 72 69 67 67 65 72 28 73 71 6c 2c 20  me_trigger(sql, 
1dea1 25 51 29 2c 20 22 0a 20 20 20 20 20 20 20 20 20  %Q), ".         
1dea2 20 20 20 22 74 62 6c 5f 6e 61 6d 65 20 3d 20 25     "tbl_name = %
1dea3 51 20 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  Q ".            
1dea4 22 57 48 45 52 45 20 25 73 3b 22 2c 20 7a 4e 61  "WHERE %s;", zNa
1dea5 6d 65 2c 20 7a 4e 61 6d 65 2c 20 7a 57 68 65 72  me, zName, zWher
1dea6 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  e);.    sqlite3D
1dea7 62 46 72 65 65 28 64 62 2c 20 7a 57 68 65 72 65  bFree(db, zWhere
1dea8 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  );.  }.#endif.. 
1dea9 20 2f 2a 20 44 72 6f 70 20 61 6e 64 20 72 65 6c   /* Drop and rel
1deaa 6f 61 64 20 74 68 65 20 69 6e 74 65 72 6e 61 6c  oad the internal
1deab 20 74 61 62 6c 65 20 73 63 68 65 6d 61 2e 20 2a   table schema. *
1deac 2f 0a 20 20 72 65 6c 6f 61 64 54 61 62 6c 65 53  /.  reloadTableS
1dead 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20 70 54  chema(pParse, pT
1deae 61 62 2c 20 7a 4e 61 6d 65 29 3b 0a 0a 65 78 69  ab, zName);..exi
1deaf 74 5f 72 65 6e 61 6d 65 5f 74 61 62 6c 65 3a 0a  t_rename_table:.
1deb0 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
1deb1 44 65 6c 65 74 65 28 64 62 2c 20 70 53 72 63 29  Delete(db, pSrc)
1deb2 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
1deb3 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 7d 0a  e(db, zName);.}.
1deb4 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1deb5 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
1deb6 61 66 74 65 72 20 61 6e 20 22 41 4c 54 45 52 20  after an "ALTER 
1deb7 54 41 42 4c 45 20 2e 2e 2e 20 41 44 44 22 20 73  TABLE ... ADD" s
1deb8 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 68 61 73 20  tatement.** has 
1deb9 62 65 65 6e 20 70 61 72 73 65 64 2e 20 41 72 67  been parsed. Arg
1deba 75 6d 65 6e 74 20 70 43 6f 6c 44 65 66 20 63 6f  ument pColDef co
1debb 6e 74 61 69 6e 73 20 74 68 65 20 74 65 78 74 20  ntains the text 
1debc 6f 66 20 74 68 65 20 6e 65 77 0a 2a 2a 20 63 6f  of the new.** co
1debd 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2e  lumn definition.
1debe 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61 62 6c 65  .**.** The Table
1debf 20 73 74 72 75 63 74 75 72 65 20 70 50 61 72 73   structure pPars
1dec0 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 77 61 73  e->pNewTable was
1dec1 20 65 78 74 65 6e 64 65 64 20 74 6f 20 69 6e 63   extended to inc
1dec2 6c 75 64 65 0a 2a 2a 20 74 68 65 20 6e 65 77 20  lude.** the new 
1dec3 63 6f 6c 75 6d 6e 20 64 75 72 69 6e 67 20 70 61  column during pa
1dec4 72 73 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45  rsing..*/.SQLITE
1dec5 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1dec6 6c 69 74 65 33 41 6c 74 65 72 46 69 6e 69 73 68  lite3AlterFinish
1dec7 41 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20  AddColumn(Parse 
1dec8 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a  *pParse, Token *
1dec9 70 43 6f 6c 44 65 66 29 7b 0a 20 20 54 61 62 6c  pColDef){.  Tabl
1deca 65 20 2a 70 4e 65 77 3b 20 20 20 20 20 20 20 20  e *pNew;        
1decb 20 20 20 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66        /* Copy of
1decc 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1decd 6c 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70  le */.  Table *p
1dece 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  Tab;            
1decf 20 20 2f 2a 20 54 61 62 6c 65 20 62 65 69 6e 67    /* Table being
1ded0 20 61 6c 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e   altered */.  in
1ded1 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
1ded2 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1ded3 61 73 65 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  ase number */.  
1ded4 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
1ded5 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74            /* Dat
1ded6 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20 20  abase name */.  
1ded7 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62  const char *zTab
1ded8 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 61 62  ;         /* Tab
1ded9 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 63 68 61  le name */.  cha
1deda 72 20 2a 7a 43 6f 6c 3b 20 20 20 20 20 20 20 20  r *zCol;        
1dedb 20 20 20 20 20 20 20 2f 2a 20 4e 75 6c 6c 2d 74         /* Null-t
1dedc 65 72 6d 69 6e 61 74 65 64 20 63 6f 6c 75 6d 6e  erminated column
1dedd 20 64 65 66 69 6e 69 74 69 6f 6e 20 2a 2f 0a 20   definition */. 
1dede 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 20 20   Column *pCol;  
1dedf 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
1dee0 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 2a 2f 0a  e new column */.
1dee1 20 20 45 78 70 72 20 2a 70 44 66 6c 74 3b 20 20    Expr *pDflt;  
1dee2 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
1dee3 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
1dee4 20 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20   the new column 
1dee5 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  */.  sqlite3 *db
1dee6 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1dee7 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
1dee8 6f 6e 6e 65 63 74 69 6f 6e 3b 20 2a 2f 0a 0a 20  onnection; */.. 
1dee9 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62   db = pParse->db
1deea 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
1deeb 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  nErr || db->mall
1deec 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72  ocFailed ) retur
1deed 6e 3b 0a 20 20 70 4e 65 77 20 3d 20 70 50 61 72  n;.  pNew = pPar
1deee 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a 20  se->pNewTable;. 
1deef 20 61 73 73 65 72 74 28 20 70 4e 65 77 20 29 3b   assert( pNew );
1def0 0a 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69  ..  assert( sqli
1def1 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c  te3BtreeHoldsAll
1def2 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b 0a 20  Mutexes(db) );. 
1def3 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1def4 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
1def5 70 4e 65 77 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pNew->pSchema);.
1def6 20 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b    zDb = db->aDb[
1def7 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7a 54  iDb].zName;.  zT
1def8 61 62 20 3d 20 70 4e 65 77 2d 3e 7a 4e 61 6d 65  ab = pNew->zName
1def9 3b 0a 20 20 70 43 6f 6c 20 3d 20 26 70 4e 65 77  ;.  pCol = &pNew
1defa 2d 3e 61 43 6f 6c 5b 70 4e 65 77 2d 3e 6e 43 6f  ->aCol[pNew->nCo
1defb 6c 2d 31 5d 3b 0a 20 20 70 44 66 6c 74 20 3d 20  l-1];.  pDflt = 
1defc 70 43 6f 6c 2d 3e 70 44 66 6c 74 3b 0a 20 20 70  pCol->pDflt;.  p
1defd 54 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  Tab = sqlite3Fin
1defe 64 54 61 62 6c 65 28 64 62 2c 20 7a 54 61 62 2c  dTable(db, zTab,
1deff 20 7a 44 62 29 3b 0a 20 20 61 73 73 65 72 74 28   zDb);.  assert(
1df00 20 70 54 61 62 20 29 3b 0a 0a 23 69 66 6e 64 65   pTab );..#ifnde
1df01 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1df02 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a  THORIZATION.  /*
1df03 20 49 6e 76 6f 6b 65 20 74 68 65 20 61 75 74 68   Invoke the auth
1df04 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
1df05 63 6b 2e 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ck. */.  if( sql
1df06 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
1df07 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 41 4c 54  arse, SQLITE_ALT
1df08 45 52 5f 54 41 42 4c 45 2c 20 7a 44 62 2c 20 70  ER_TABLE, zDb, p
1df09 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 20 29  Tab->zName, 0) )
1df0a 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1df0b 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
1df0c 66 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  f the default va
1df0d 6c 75 65 20 66 6f 72 20 74 68 65 20 6e 65 77 20  lue for the new 
1df0e 63 6f 6c 75 6d 6e 20 77 61 73 20 73 70 65 63 69  column was speci
1df0f 66 69 65 64 20 77 69 74 68 20 61 20 0a 20 20 2a  fied with a .  *
1df10 2a 20 6c 69 74 65 72 61 6c 20 4e 55 4c 4c 2c 20  * literal NULL, 
1df11 74 68 65 6e 20 73 65 74 20 70 44 66 6c 74 20 74  then set pDflt t
1df12 6f 20 30 2e 20 54 68 69 73 20 73 69 6d 70 6c 69  o 0. This simpli
1df13 66 69 65 73 20 63 68 65 63 6b 69 6e 67 0a 20 20  fies checking.  
1df14 2a 2a 20 66 6f 72 20 61 6e 20 53 51 4c 20 4e 55  ** for an SQL NU
1df15 4c 4c 20 64 65 66 61 75 6c 74 20 62 65 6c 6f 77  LL default below
1df16 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 66  ..  */.  if( pDf
1df17 6c 74 20 26 26 20 70 44 66 6c 74 2d 3e 6f 70 3d  lt && pDflt->op=
1df18 3d 54 4b 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  =TK_NULL ){.    
1df19 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a  pDflt = 0;.  }..
1df1a 20 20 2f 2a 20 43 68 65 63 6b 20 74 68 61 74 20    /* Check that 
1df1b 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69  the new column i
1df1c 73 20 6e 6f 74 20 73 70 65 63 69 66 69 65 64 20  s not specified 
1df1d 61 73 20 50 52 49 4d 41 52 59 20 4b 45 59 20 6f  as PRIMARY KEY o
1df1e 72 20 55 4e 49 51 55 45 2e 0a 20 20 2a 2a 20 49  r UNIQUE..  ** I
1df1f 66 20 74 68 65 72 65 20 69 73 20 61 20 4e 4f 54  f there is a NOT
1df20 20 4e 55 4c 4c 20 63 6f 6e 73 74 72 61 69 6e 74   NULL constraint
1df21 2c 20 74 68 65 6e 20 74 68 65 20 64 65 66 61 75  , then the defau
1df22 6c 74 20 76 61 6c 75 65 20 66 6f 72 20 74 68 65  lt value for the
1df23 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 6d 75 73  .  ** column mus
1df24 74 20 6e 6f 74 20 62 65 20 4e 55 4c 4c 2e 0a 20  t not be NULL.. 
1df25 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 2d 3e   */.  if( pCol->
1df26 69 73 50 72 69 6d 4b 65 79 20 29 7b 0a 20 20 20  isPrimKey ){.   
1df27 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1df28 28 70 50 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74  (pParse, "Cannot
1df29 20 61 64 64 20 61 20 50 52 49 4d 41 52 59 20 4b   add a PRIMARY K
1df2a 45 59 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20 20  EY column");.   
1df2b 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
1df2c 66 28 20 70 4e 65 77 2d 3e 70 49 6e 64 65 78 20  f( pNew->pIndex 
1df2d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
1df2e 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1df2f 43 61 6e 6e 6f 74 20 61 64 64 20 61 20 55 4e 49  Cannot add a UNI
1df30 51 55 45 20 63 6f 6c 75 6d 6e 22 29 3b 0a 20 20  QUE column");.  
1df31 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20    return;.  }.  
1df32 69 66 28 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c  if( pCol->notNul
1df33 6c 20 26 26 20 21 70 44 66 6c 74 20 29 7b 0a 20  l && !pDflt ){. 
1df34 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1df35 73 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20  sg(pParse, .    
1df36 20 20 20 20 22 43 61 6e 6e 6f 74 20 61 64 64 20      "Cannot add 
1df37 61 20 4e 4f 54 20 4e 55 4c 4c 20 63 6f 6c 75 6d  a NOT NULL colum
1df38 6e 20 77 69 74 68 20 64 65 66 61 75 6c 74 20 76  n with default v
1df39 61 6c 75 65 20 4e 55 4c 4c 22 29 3b 0a 20 20 20  alue NULL");.   
1df3a 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
1df3b 2f 2a 20 45 6e 73 75 72 65 20 74 68 65 20 64 65  /* Ensure the de
1df3c 66 61 75 6c 74 20 65 78 70 72 65 73 73 69 6f 6e  fault expression
1df3d 20 69 73 20 73 6f 6d 65 74 68 69 6e 67 20 74 68   is something th
1df3e 61 74 20 73 71 6c 69 74 65 33 56 61 6c 75 65 46  at sqlite3ValueF
1df3f 72 6f 6d 45 78 70 72 28 29 0a 20 20 2a 2a 20 63  romExpr().  ** c
1df40 61 6e 20 68 61 6e 64 6c 65 20 28 69 2e 65 2e 20  an handle (i.e. 
1df41 6e 6f 74 20 43 55 52 52 45 4e 54 5f 54 49 4d 45  not CURRENT_TIME
1df42 20 65 74 63 2e 29 0a 20 20 2a 2f 0a 20 20 69 66   etc.).  */.  if
1df43 28 20 70 44 66 6c 74 20 29 7b 0a 20 20 20 20 73  ( pDflt ){.    s
1df44 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
1df45 61 6c 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69  al;.    if( sqli
1df46 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72  te3ValueFromExpr
1df47 28 64 62 2c 20 70 44 66 6c 74 2c 20 53 51 4c 49  (db, pDflt, SQLI
1df48 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54 45 5f  TE_UTF8, SQLITE_
1df49 41 46 46 5f 4e 4f 4e 45 2c 20 26 70 56 61 6c 29  AFF_NONE, &pVal)
1df4a 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61   ){.      db->ma
1df4b 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
1df4c 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20        return;.  
1df4d 20 20 7d 0a 20 20 20 20 69 66 28 20 21 70 56 61    }.    if( !pVa
1df4e 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  l ){.      sqlit
1df4f 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1df50 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64 64 20 61  e, "Cannot add a
1df51 20 63 6f 6c 75 6d 6e 20 77 69 74 68 20 6e 6f 6e   column with non
1df52 2d 63 6f 6e 73 74 61 6e 74 20 64 65 66 61 75 6c  -constant defaul
1df53 74 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72  t");.      retur
1df54 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  n;.    }.    sql
1df55 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70 56  ite3ValueFree(pV
1df56 61 6c 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  al);.  }..  /* M
1df57 6f 64 69 66 79 20 74 68 65 20 43 52 45 41 54 45  odify the CREATE
1df58 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
1df59 2e 20 2a 2f 0a 20 20 7a 43 6f 6c 20 3d 20 73 71  . */.  zCol = sq
1df5a 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28 64  lite3DbStrNDup(d
1df5b 62 2c 20 28 63 68 61 72 2a 29 70 43 6f 6c 44 65  b, (char*)pColDe
1df5c 66 2d 3e 7a 2c 20 70 43 6f 6c 44 65 66 2d 3e 6e  f->z, pColDef->n
1df5d 29 3b 0a 20 20 69 66 28 20 7a 43 6f 6c 20 29 7b  );.  if( zCol ){
1df5e 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 6e 64 20  .    char *zEnd 
1df5f 3d 20 26 7a 43 6f 6c 5b 70 43 6f 6c 44 65 66 2d  = &zCol[pColDef-
1df60 3e 6e 2d 31 5d 3b 0a 20 20 20 20 77 68 69 6c 65  >n-1];.    while
1df61 28 20 28 7a 45 6e 64 3e 7a 43 6f 6c 20 26 26 20  ( (zEnd>zCol && 
1df62 2a 7a 45 6e 64 3d 3d 27 3b 27 29 20 7c 7c 20 69  *zEnd==';') || i
1df63 73 73 70 61 63 65 28 2a 28 75 6e 73 69 67 6e 65  sspace(*(unsigne
1df64 64 20 63 68 61 72 20 2a 29 7a 45 6e 64 29 20 29  d char *)zEnd) )
1df65 7b 0a 20 20 20 20 20 20 2a 7a 45 6e 64 2d 2d 20  {.      *zEnd-- 
1df66 3d 20 27 5c 30 27 3b 0a 20 20 20 20 7d 0a 20 20  = '\0';.    }.  
1df67 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
1df68 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20 20  arse(pParse, .  
1df69 20 20 20 20 20 20 22 55 50 44 41 54 45 20 5c 22        "UPDATE \"
1df6a 25 77 5c 22 2e 25 73 20 53 45 54 20 22 0a 20 20  %w\".%s SET ".  
1df6b 20 20 20 20 20 20 20 20 22 73 71 6c 20 3d 20 73          "sql = s
1df6c 75 62 73 74 72 28 73 71 6c 2c 31 2c 25 64 29 20  ubstr(sql,1,%d) 
1df6d 7c 7c 20 27 2c 20 27 20 7c 7c 20 25 51 20 7c 7c  || ', ' || %Q ||
1df6e 20 73 75 62 73 74 72 28 73 71 6c 2c 25 64 29 20   substr(sql,%d) 
1df6f 22 0a 20 20 20 20 20 20 20 20 22 57 48 45 52 45  ".        "WHERE
1df70 20 74 79 70 65 20 3d 20 27 74 61 62 6c 65 27 20   type = 'table' 
1df71 41 4e 44 20 6e 61 6d 65 20 3d 20 25 51 22 2c 20  AND name = %Q", 
1df72 0a 20 20 20 20 20 20 7a 44 62 2c 20 53 43 48 45  .      zDb, SCHE
1df73 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 2c 20 70  MA_TABLE(iDb), p
1df74 4e 65 77 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65  New->addColOffse
1df75 74 2c 20 7a 43 6f 6c 2c 20 70 4e 65 77 2d 3e 61  t, zCol, pNew->a
1df76 64 64 43 6f 6c 4f 66 66 73 65 74 2b 31 2c 0a 20  ddColOffset+1,. 
1df77 20 20 20 20 20 7a 54 61 62 0a 20 20 20 20 29 3b       zTab.    );
1df78 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1df79 65 65 28 64 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20  ee(db, zCol);.  
1df7a 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 64  }..  /* If the d
1df7b 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f 66 20  efault value of 
1df7c 74 68 65 20 6e 65 77 20 63 6f 6c 75 6d 6e 20 69  the new column i
1df7d 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 73 65 74  s NULL, then set
1df7e 20 74 68 65 20 66 69 6c 65 0a 20 20 2a 2a 20 66   the file.  ** f
1df7f 6f 72 6d 61 74 20 74 6f 20 32 2e 20 49 66 20 74  ormat to 2. If t
1df80 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65  he default value
1df81 20 6f 66 20 74 68 65 20 6e 65 77 20 63 6f 6c 75   of the new colu
1df82 6d 6e 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 0a  mn is not NULL,.
1df83 20 20 2a 2a 20 74 68 65 20 66 69 6c 65 20 66 6f    ** the file fo
1df84 72 6d 61 74 20 62 65 63 6f 6d 65 73 20 33 2e 0a  rmat becomes 3..
1df85 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 4d 69    */.  sqlite3Mi
1df86 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74 28  nimumFileFormat(
1df87 70 50 61 72 73 65 2c 20 69 44 62 2c 20 70 44 66  pParse, iDb, pDf
1df88 6c 74 20 3f 20 33 20 3a 20 32 29 3b 0a 0a 20 20  lt ? 3 : 2);..  
1df89 2f 2a 20 52 65 6c 6f 61 64 20 74 68 65 20 73 63  /* Reload the sc
1df8a 68 65 6d 61 20 6f 66 20 74 68 65 20 6d 6f 64 69  hema of the modi
1df8b 66 69 65 64 20 74 61 62 6c 65 2e 20 2a 2f 0a 20  fied table. */. 
1df8c 20 72 65 6c 6f 61 64 54 61 62 6c 65 53 63 68 65   reloadTableSche
1df8d 6d 61 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  ma(pParse, pTab,
1df8e 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 7d   pTab->zName);.}
1df8f 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1df90 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20  ction is called 
1df91 62 79 20 74 68 65 20 70 61 72 73 65 72 20 61 66  by the parser af
1df92 74 65 72 20 74 68 65 20 74 61 62 6c 65 2d 6e 61  ter the table-na
1df93 6d 65 20 69 6e 0a 2a 2a 20 61 6e 20 22 41 4c 54  me in.** an "ALT
1df94 45 52 20 54 41 42 4c 45 20 3c 74 61 62 6c 65 2d  ER TABLE <table-
1df95 6e 61 6d 65 3e 20 41 44 44 22 20 73 74 61 74 65  name> ADD" state
1df96 6d 65 6e 74 20 69 73 20 70 61 72 73 65 64 2e 20  ment is parsed. 
1df97 41 72 67 75 6d 65 6e 74 20 0a 2a 2a 20 70 53 72  Argument .** pSr
1df98 63 20 69 73 20 74 68 65 20 66 75 6c 6c 2d 6e 61  c is the full-na
1df99 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20  me of the table 
1df9a 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 0a 2a  being altered..*
1df9b 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1df9c 65 20 6d 61 6b 65 73 20 61 20 28 70 61 72 74 69  e makes a (parti
1df9d 61 6c 29 20 63 6f 70 79 20 6f 66 20 74 68 65 20  al) copy of the 
1df9e 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 0a  Table structure.
1df9f 2a 2a 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65  ** for the table
1dfa0 20 62 65 69 6e 67 20 61 6c 74 65 72 65 64 20 61   being altered a
1dfa1 6e 64 20 73 65 74 73 20 50 61 72 73 65 2e 70 4e  nd sets Parse.pN
1dfa2 65 77 54 61 62 6c 65 20 74 6f 20 70 6f 69 6e 74  ewTable to point
1dfa3 0a 2a 2a 20 74 6f 20 69 74 2e 20 52 6f 75 74 69  .** to it. Routi
1dfa4 6e 65 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68  nes called by th
1dfa5 65 20 70 61 72 73 65 72 20 61 73 20 74 68 65 20  e parser as the 
1dfa6 63 6f 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f  column definitio
1dfa7 6e 0a 2a 2a 20 69 73 20 70 61 72 73 65 64 20 28  n.** is parsed (
1dfa8 69 2e 65 2e 20 73 71 6c 69 74 65 33 41 64 64 43  i.e. sqlite3AddC
1dfa9 6f 6c 75 6d 6e 28 29 29 20 61 64 64 20 74 68 65  olumn()) add the
1dfaa 20 6e 65 77 20 43 6f 6c 75 6d 6e 20 64 61 74 61   new Column data
1dfab 20 74 6f 20 0a 2a 2a 20 74 68 65 20 63 6f 70 79   to .** the copy
1dfac 2e 20 54 68 65 20 63 6f 70 79 20 6f 66 20 74 68  . The copy of th
1dfad 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75 72  e Table structur
1dfae 65 20 69 73 20 64 65 6c 65 74 65 64 20 62 79 20  e is deleted by 
1dfaf 74 6f 6b 65 6e 69 7a 65 2e 63 20 0a 2a 2a 20 61  tokenize.c .** a
1dfb0 66 74 65 72 20 70 61 72 73 69 6e 67 20 69 73 20  fter parsing is 
1dfb1 66 69 6e 69 73 68 65 64 2e 0a 2a 2a 0a 2a 2a 20  finished..**.** 
1dfb2 52 6f 75 74 69 6e 65 20 73 71 6c 69 74 65 33 41  Routine sqlite3A
1dfb3 6c 74 65 72 46 69 6e 69 73 68 41 64 64 43 6f 6c  lterFinishAddCol
1dfb4 75 6d 6e 28 29 20 77 69 6c 6c 20 62 65 20 63 61  umn() will be ca
1dfb5 6c 6c 65 64 20 74 6f 20 63 6f 6d 70 6c 65 74 65  lled to complete
1dfb6 0a 2a 2a 20 63 6f 64 69 6e 67 20 74 68 65 20 22  .** coding the "
1dfb7 41 4c 54 45 52 20 54 41 42 4c 45 20 2e 2e 2e 20  ALTER TABLE ... 
1dfb8 41 44 44 22 20 73 74 61 74 65 6d 65 6e 74 2e 0a  ADD" statement..
1dfb9 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1dfba 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 6c  E void sqlite3Al
1dfbb 74 65 72 42 65 67 69 6e 41 64 64 43 6f 6c 75 6d  terBeginAddColum
1dfbc 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  n(Parse *pParse,
1dfbd 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 29 7b   SrcList *pSrc){
1dfbe 0a 20 20 54 61 62 6c 65 20 2a 70 4e 65 77 3b 0a  .  Table *pNew;.
1dfbf 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20    Table *pTab;. 
1dfc0 20 56 64 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20   Vdbe *v;.  int 
1dfc1 69 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  iDb;.  int i;.  
1dfc2 69 6e 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 73 71  int nAlloc;.  sq
1dfc3 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
1dfc4 73 65 2d 3e 64 62 3b 0a 0a 20 20 2f 2a 20 4c 6f  se->db;..  /* Lo
1dfc5 6f 6b 20 75 70 20 74 68 65 20 74 61 62 6c 65 20  ok up the table 
1dfc6 62 65 69 6e 67 20 61 6c 74 65 72 65 64 2e 20 2a  being altered. *
1dfc7 2f 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  /.  assert( pPar
1dfc8 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3d 3d 30  se->pNewTable==0
1dfc9 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71   );.  assert( sq
1dfca 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64 73 41  lite3BtreeHoldsA
1dfcb 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20 29 3b  llMutexes(db) );
1dfcc 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1dfcd 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 65  cFailed ) goto e
1dfce 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f  xit_begin_add_co
1dfcf 6c 75 6d 6e 3b 0a 20 20 70 54 61 62 20 3d 20 73  lumn;.  pTab = s
1dfd0 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
1dfd1 65 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 72  e(pParse, 0, pSr
1dfd2 63 2d 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70  c->a[0].zName, p
1dfd3 53 72 63 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62  Src->a[0].zDatab
1dfd4 61 73 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61  ase);.  if( !pTa
1dfd5 62 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65  b ) goto exit_be
1dfd6 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
1dfd7 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1dfd8 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1dfd9 45 0a 20 20 69 66 28 20 49 73 56 69 72 74 75 61  E.  if( IsVirtua
1dfda 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 73  l(pTab) ){.    s
1dfdb 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1dfdc 50 61 72 73 65 2c 20 22 76 69 72 74 75 61 6c 20  Parse, "virtual 
1dfdd 74 61 62 6c 65 73 20 6d 61 79 20 6e 6f 74 20 62  tables may not b
1dfde 65 20 61 6c 74 65 72 65 64 22 29 3b 0a 20 20 20  e altered");.   
1dfdf 20 67 6f 74 6f 20 65 78 69 74 5f 62 65 67 69 6e   goto exit_begin
1dfe0 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 7d  _add_column;.  }
1dfe1 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61  .#endif..  /* Ma
1dfe2 6b 65 20 73 75 72 65 20 74 68 69 73 20 69 73 20  ke sure this is 
1dfe3 6e 6f 74 20 61 6e 20 61 74 74 65 6d 70 74 20 74  not an attempt t
1dfe4 6f 20 41 4c 54 45 52 20 61 20 76 69 65 77 2e 20  o ALTER a view. 
1dfe5 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  */.  if( pTab->p
1dfe6 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71  Select ){.    sq
1dfe7 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1dfe8 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 61 64  arse, "Cannot ad
1dfe9 64 20 61 20 63 6f 6c 75 6d 6e 20 74 6f 20 61 20  d a column to a 
1dfea 76 69 65 77 22 29 3b 0a 20 20 20 20 67 6f 74 6f  view");.    goto
1dfeb 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
1dfec 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 0a 20 20 61  column;.  }..  a
1dfed 73 73 65 72 74 28 20 70 54 61 62 2d 3e 61 64 64  ssert( pTab->add
1dfee 43 6f 6c 4f 66 66 73 65 74 3e 30 20 29 3b 0a 20  ColOffset>0 );. 
1dfef 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
1dff0 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
1dff1 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
1dff2 0a 20 20 2f 2a 20 50 75 74 20 61 20 63 6f 70 79  .  /* Put a copy
1dff3 20 6f 66 20 74 68 65 20 54 61 62 6c 65 20 73 74   of the Table st
1dff4 72 75 63 74 20 69 6e 20 50 61 72 73 65 2e 70 4e  ruct in Parse.pN
1dff5 65 77 54 61 62 6c 65 20 66 6f 72 20 74 68 65 0a  ewTable for the.
1dff6 20 20 2a 2a 20 73 71 6c 69 74 65 33 41 64 64 43    ** sqlite3AddC
1dff7 6f 6c 75 6d 6e 28 29 20 66 75 6e 63 74 69 6f 6e  olumn() function
1dff8 20 61 6e 64 20 66 72 69 65 6e 64 73 20 74 6f 20   and friends to 
1dff9 6d 6f 64 69 66 79 2e 0a 20 20 2a 2f 0a 20 20 70  modify..  */.  p
1dffa 4e 65 77 20 3d 20 28 54 61 62 6c 65 2a 29 73 71  New = (Table*)sq
1dffb 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
1dffc 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 61 62  o(db, sizeof(Tab
1dffd 6c 65 29 29 3b 0a 20 20 69 66 28 20 21 70 4e 65  le));.  if( !pNe
1dffe 77 20 29 20 67 6f 74 6f 20 65 78 69 74 5f 62 65  w ) goto exit_be
1dfff 67 69 6e 5f 61 64 64 5f 63 6f 6c 75 6d 6e 3b 0a  gin_add_column;.
1e000 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61    pParse->pNewTa
1e001 62 6c 65 20 3d 20 70 4e 65 77 3b 0a 20 20 70 4e  ble = pNew;.  pN
1e002 65 77 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20 20  ew->nRef = 1;.  
1e003 70 4e 65 77 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  pNew->db = db;. 
1e004 20 70 4e 65 77 2d 3e 6e 43 6f 6c 20 3d 20 70 54   pNew->nCol = pT
1e005 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 61 73 73 65  ab->nCol;.  asse
1e006 72 74 28 20 70 4e 65 77 2d 3e 6e 43 6f 6c 3e 30  rt( pNew->nCol>0
1e007 20 29 3b 0a 20 20 6e 41 6c 6c 6f 63 20 3d 20 28   );.  nAlloc = (
1e008 28 28 70 4e 65 77 2d 3e 6e 43 6f 6c 2d 31 29 2f  ((pNew->nCol-1)/
1e009 38 29 2a 38 29 2b 38 3b 0a 20 20 61 73 73 65 72  8)*8)+8;.  asser
1e00a 74 28 20 6e 41 6c 6c 6f 63 3e 3d 70 4e 65 77 2d  t( nAlloc>=pNew-
1e00b 3e 6e 43 6f 6c 20 26 26 20 6e 41 6c 6c 6f 63 25  >nCol && nAlloc%
1e00c 38 3d 3d 30 20 26 26 20 6e 41 6c 6c 6f 63 2d 70  8==0 && nAlloc-p
1e00d 4e 65 77 2d 3e 6e 43 6f 6c 3c 38 20 29 3b 0a 20  New->nCol<8 );. 
1e00e 20 70 4e 65 77 2d 3e 61 43 6f 6c 20 3d 20 28 43   pNew->aCol = (C
1e00f 6f 6c 75 6d 6e 2a 29 73 71 6c 69 74 65 33 44 62  olumn*)sqlite3Db
1e010 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73  MallocZero(db, s
1e011 69 7a 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 6e 41  izeof(Column)*nA
1e012 6c 6c 6f 63 29 3b 0a 20 20 70 4e 65 77 2d 3e 7a  lloc);.  pNew->z
1e013 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62  Name = sqlite3Db
1e014 53 74 72 44 75 70 28 64 62 2c 20 70 54 61 62 2d  StrDup(db, pTab-
1e015 3e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21  >zName);.  if( !
1e016 70 4e 65 77 2d 3e 61 43 6f 6c 20 7c 7c 20 21 70  pNew->aCol || !p
1e017 4e 65 77 2d 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20  New->zName ){.  
1e018 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
1e019 65 64 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  ed = 1;.    goto
1e01a 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f   exit_begin_add_
1e01b 63 6f 6c 75 6d 6e 3b 0a 20 20 7d 0a 20 20 6d 65  column;.  }.  me
1e01c 6d 63 70 79 28 70 4e 65 77 2d 3e 61 43 6f 6c 2c  mcpy(pNew->aCol,
1e01d 20 70 54 61 62 2d 3e 61 43 6f 6c 2c 20 73 69 7a   pTab->aCol, siz
1e01e 65 6f 66 28 43 6f 6c 75 6d 6e 29 2a 70 4e 65 77  eof(Column)*pNew
1e01f 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 66 6f 72 28 69  ->nCol);.  for(i
1e020 3d 30 3b 20 69 3c 70 4e 65 77 2d 3e 6e 43 6f 6c  =0; i<pNew->nCol
1e021 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 43 6f 6c 75  ; i++){.    Colu
1e022 6d 6e 20 2a 70 43 6f 6c 20 3d 20 26 70 4e 65 77  mn *pCol = &pNew
1e023 2d 3e 61 43 6f 6c 5b 69 5d 3b 0a 20 20 20 20 70  ->aCol[i];.    p
1e024 43 6f 6c 2d 3e 7a 4e 61 6d 65 20 3d 20 73 71 6c  Col->zName = sql
1e025 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62 2c  ite3DbStrDup(db,
1e026 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pCol->zName);. 
1e027 20 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d     pCol->zColl =
1e028 20 30 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 7a 54   0;.    pCol->zT
1e029 79 70 65 20 3d 20 30 3b 0a 20 20 20 20 70 43 6f  ype = 0;.    pCo
1e02a 6c 2d 3e 70 44 66 6c 74 20 3d 20 30 3b 0a 20 20  l->pDflt = 0;.  
1e02b 7d 0a 20 20 70 4e 65 77 2d 3e 70 53 63 68 65 6d  }.  pNew->pSchem
1e02c 61 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  a = db->aDb[iDb]
1e02d 2e 70 53 63 68 65 6d 61 3b 0a 20 20 70 4e 65 77  .pSchema;.  pNew
1e02e 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d  ->addColOffset =
1e02f 20 70 54 61 62 2d 3e 61 64 64 43 6f 6c 4f 66 66   pTab->addColOff
1e030 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 6e 52 65  set;.  pNew->nRe
1e031 66 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 42 65 67  f = 1;..  /* Beg
1e032 69 6e 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e  in a transaction
1e033 20 61 6e 64 20 69 6e 63 72 65 6d 65 6e 74 20 74   and increment t
1e034 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
1e035 2e 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 42  .  */.  sqlite3B
1e036 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
1e037 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
1e038 62 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  b);.  v = sqlite
1e039 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1e03a 3b 0a 20 20 69 66 28 20 21 76 20 29 20 67 6f 74  ;.  if( !v ) got
1e03b 6f 20 65 78 69 74 5f 62 65 67 69 6e 5f 61 64 64  o exit_begin_add
1e03c 5f 63 6f 6c 75 6d 6e 3b 0a 20 20 73 71 6c 69 74  _column;.  sqlit
1e03d 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28 70  e3ChangeCookie(p
1e03e 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 0a 65 78  Parse, iDb);..ex
1e03f 69 74 5f 62 65 67 69 6e 5f 61 64 64 5f 63 6f 6c  it_begin_add_col
1e040 75 6d 6e 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  umn:.  sqlite3Sr
1e041 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
1e042 70 53 72 63 29 3b 0a 20 20 72 65 74 75 72 6e 3b  pSrc);.  return;
1e043 0a 7d 0a 23 65 6e 64 69 66 20 20 2f 2a 20 53 51  .}.#endif  /* SQ
1e044 4c 49 54 45 5f 41 4c 54 45 52 5f 54 41 42 4c 45  LITE_ALTER_TABLE
1e045 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
1e046 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 6c 74 65  **** End of alte
1e047 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  r.c ************
1e048 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e049 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e04a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1e04b 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1e04c 61 6e 61 6c 79 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a  analyze.c ******
1e04d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e04e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e04f 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35 20  ***/./*.** 2005 
1e050 4a 75 6c 79 20 38 0a 2a 2a 0a 2a 2a 20 54 68 65  July 8.**.** The
1e051 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
1e052 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
1e053 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
1e054 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
1e055 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
1e056 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
1e057 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
1e058 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
1e059 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
1e05a 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
1e05b 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
1e05c 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
1e05d 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
1e05e 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
1e05f 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
1e060 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
1e061 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
1e062 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e063 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e064 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e065 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e066 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
1e067 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
1e068 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68   associated with
1e069 20 74 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d   the ANALYZE com
1e06a 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 40 28 23 29  mand..**.** @(#)
1e06b 20 24 49 64 3a 20 61 6e 61 6c 79 7a 65 2e 63 2c   $Id: analyze.c,
1e06c 76 20 31 2e 34 36 20 32 30 30 38 2f 31 31 2f 31  v 1.46 2008/11/1
1e06d 39 20 31 36 3a 35 32 3a 34 34 20 64 61 6e 69 65  9 16:52:44 danie
1e06e 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a  lk1977 Exp $.*/.
1e06f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1e070 4d 49 54 5f 41 4e 41 4c 59 5a 45 0a 0a 2f 2a 0a  MIT_ANALYZE../*.
1e071 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
1e072 67 65 6e 65 72 61 74 65 73 20 63 6f 64 65 20 74  generates code t
1e073 68 61 74 20 6f 70 65 6e 73 20 74 68 65 20 73 71  hat opens the sq
1e074 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1e075 20 6f 6e 20 63 75 72 73 6f 72 0a 2a 2a 20 69 53   on cursor.** iS
1e076 74 61 74 43 75 72 2e 0a 2a 2a 0a 2a 2a 20 49 66  tatCur..**.** If
1e077 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
1e078 31 20 74 61 62 6c 65 73 20 64 6f 65 73 20 6e 6f  1 tables does no
1e079 74 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69  t previously exi
1e07a 73 74 2c 20 69 74 20 69 73 20 63 72 65 61 74 65  st, it is create
1e07b 64 2e 0a 2a 2a 20 49 66 20 69 74 20 64 6f 65 73  d..** If it does
1e07c 20 70 72 65 76 69 6f 75 73 6c 79 20 65 78 69 73   previously exis
1e07d 74 2c 20 61 6c 6c 20 65 6e 74 69 72 65 73 20 61  t, all entires a
1e07e 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
1e07f 61 62 6c 65 20 7a 57 68 65 72 65 0a 2a 2a 20 61  able zWhere.** a
1e080 72 65 20 72 65 6d 6f 76 65 64 2e 20 20 49 66 20  re removed.  If 
1e081 7a 57 68 65 72 65 3d 3d 30 20 74 68 65 6e 20 61  zWhere==0 then a
1e082 6c 6c 20 65 6e 74 72 69 65 73 20 61 72 65 20 72  ll entries are r
1e083 65 6d 6f 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  emoved..*/.stati
1e084 63 20 76 6f 69 64 20 6f 70 65 6e 53 74 61 74 54  c void openStatT
1e085 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
1e086 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
1e087 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
1e088 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c  xt */.  int iDb,
1e089 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e08a 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20  /* The database 
1e08b 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e 67 20 69  we are looking i
1e08c 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61 74  n */.  int iStat
1e08d 43 75 72 2c 20 20 20 20 20 20 20 20 20 20 20 2f  Cur,           /
1e08e 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69 74  * Open the sqlit
1e08f 65 5f 73 74 61 74 31 20 74 61 62 6c 65 20 6f 6e  e_stat1 table on
1e090 20 74 68 69 73 20 63 75 72 73 6f 72 20 2a 2f 0a   this cursor */.
1e091 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 57    const char *zW
1e092 68 65 72 65 20 20 20 20 20 20 2f 2a 20 44 65 6c  here      /* Del
1e093 65 74 65 20 65 6e 74 72 69 65 73 20 61 73 73 6f  ete entries asso
1e094 63 69 61 74 65 64 20 77 69 74 68 20 74 68 69 73  ciated with this
1e095 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 73   table */.){.  s
1e096 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1e097 72 73 65 2d 3e 64 62 3b 0a 20 20 44 62 20 2a 70  rse->db;.  Db *p
1e098 44 62 3b 0a 20 20 69 6e 74 20 69 52 6f 6f 74 50  Db;.  int iRootP
1e099 61 67 65 3b 0a 20 20 69 6e 74 20 63 72 65 61 74  age;.  int creat
1e09a 65 53 74 61 74 31 20 3d 20 30 3b 0a 20 20 54 61  eStat1 = 0;.  Ta
1e09b 62 6c 65 20 2a 70 53 74 61 74 3b 0a 20 20 56 64  ble *pStat;.  Vd
1e09c 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
1e09d 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1e09e 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65  .  if( v==0 ) re
1e09f 74 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  turn;.  assert( 
1e0a0 73 71 6c 69 74 65 33 42 74 72 65 65 48 6f 6c 64  sqlite3BtreeHold
1e0a1 73 41 6c 6c 4d 75 74 65 78 65 73 28 64 62 29 20  sAllMutexes(db) 
1e0a2 29 3b 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c  );.  assert( sql
1e0a3 69 74 65 33 56 64 62 65 44 62 28 76 29 3d 3d 64  ite3VdbeDb(v)==d
1e0a4 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64 62  b );.  pDb = &db
1e0a5 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66  ->aDb[iDb];.  if
1e0a6 28 20 28 70 53 74 61 74 20 3d 20 73 71 6c 69 74  ( (pStat = sqlit
1e0a7 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20  e3FindTable(db, 
1e0a8 22 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c 20  "sqlite_stat1", 
1e0a9 70 44 62 2d 3e 7a 4e 61 6d 65 29 29 3d 3d 30 20  pDb->zName))==0 
1e0aa 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 73 71  ){.    /* The sq
1e0ab 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65  lite_stat1 table
1e0ac 73 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74  s does not exist
1e0ad 2e 20 20 43 72 65 61 74 65 20 69 74 2e 20 20 0a  .  Create it.  .
1e0ae 20 20 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74      ** Note that
1e0af 20 61 20 73 69 64 65 2d 65 66 66 65 63 74 20 6f   a side-effect o
1e0b0 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  f the CREATE TAB
1e0b1 4c 45 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  LE statement is 
1e0b2 74 6f 20 6c 65 61 76 65 0a 20 20 20 20 2a 2a 20  to leave.    ** 
1e0b3 74 68 65 20 72 6f 6f 74 70 61 67 65 20 6f 66 20  the rootpage of 
1e0b4 74 68 65 20 6e 65 77 20 74 61 62 6c 65 20 69 6e  the new table in
1e0b5 20 72 65 67 69 73 74 65 72 20 70 50 61 72 73 65   register pParse
1e0b6 2d 3e 72 65 67 52 6f 6f 74 2e 20 20 54 68 69 73  ->regRoot.  This
1e0b7 20 69 73 0a 20 20 20 20 2a 2a 20 69 6d 70 6f 72   is.    ** impor
1e0b8 74 61 6e 74 20 62 65 63 61 75 73 65 20 74 68 65  tant because the
1e0b9 20 4f 70 65 6e 57 72 69 74 65 20 6f 70 63 6f 64   OpenWrite opcod
1e0ba 65 20 62 65 6c 6f 77 20 77 69 6c 6c 20 62 65 20  e below will be 
1e0bb 6e 65 65 64 69 6e 67 20 69 74 2e 20 2a 2f 0a 20  needing it. */. 
1e0bc 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
1e0bd 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20  Parse(pParse,.  
1e0be 20 20 20 20 22 43 52 45 41 54 45 20 54 41 42 4c      "CREATE TABL
1e0bf 45 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61 74  E %Q.sqlite_stat
1e0c0 31 28 74 62 6c 2c 69 64 78 2c 73 74 61 74 29 22  1(tbl,idx,stat)"
1e0c1 2c 0a 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e 61  ,.      pDb->zNa
1e0c2 6d 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 52  me.    );.    iR
1e0c3 6f 6f 74 50 61 67 65 20 3d 20 70 50 61 72 73 65  ootPage = pParse
1e0c4 2d 3e 72 65 67 52 6f 6f 74 3b 0a 20 20 20 20 63  ->regRoot;.    c
1e0c5 72 65 61 74 65 53 74 61 74 31 20 3d 20 31 3b 20  reateStat1 = 1; 
1e0c6 20 2f 2a 20 43 61 75 73 65 20 72 6f 6f 74 70 61   /* Cause rootpa
1e0c7 67 65 20 74 6f 20 62 65 20 74 61 6b 65 6e 20 66  ge to be taken f
1e0c8 72 6f 6d 20 74 6f 70 20 6f 66 20 73 74 61 63 6b  rom top of stack
1e0c9 20 2a 2f 0a 20 20 7d 65 6c 73 65 20 69 66 28 20   */.  }else if( 
1e0ca 7a 57 68 65 72 65 20 29 7b 0a 20 20 20 20 2f 2a  zWhere ){.    /*
1e0cb 20 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   The sqlite_stat
1e0cc 31 20 74 61 62 6c 65 20 65 78 69 73 74 73 2e 20  1 table exists. 
1e0cd 20 44 65 6c 65 74 65 20 61 6c 6c 20 65 6e 74 72   Delete all entr
1e0ce 69 65 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  ies associated w
1e0cf 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74  ith.    ** the t
1e0d0 61 62 6c 65 20 7a 57 68 65 72 65 2e 20 2a 2f 0a  able zWhere. */.
1e0d1 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
1e0d2 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
1e0d3 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52        "DELETE FR
1e0d4 4f 4d 20 25 51 2e 73 71 6c 69 74 65 5f 73 74 61  OM %Q.sqlite_sta
1e0d5 74 31 20 57 48 45 52 45 20 74 62 6c 3d 25 51 22  t1 WHERE tbl=%Q"
1e0d6 2c 0a 20 20 20 20 20 20 20 70 44 62 2d 3e 7a 4e  ,.       pDb->zN
1e0d7 61 6d 65 2c 20 7a 57 68 65 72 65 0a 20 20 20 20  ame, zWhere.    
1e0d8 29 3b 0a 20 20 20 20 69 52 6f 6f 74 50 61 67 65  );.    iRootPage
1e0d9 20 3d 20 70 53 74 61 74 2d 3e 74 6e 75 6d 3b 0a   = pStat->tnum;.
1e0da 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20    }else{.    /* 
1e0db 54 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  The sqlite_stat1
1e0dc 20 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65   table already e
1e0dd 78 69 73 74 73 2e 20 20 44 65 6c 65 74 65 20 61  xists.  Delete a
1e0de 6c 6c 20 72 6f 77 73 2e 20 2a 2f 0a 20 20 20 20  ll rows. */.    
1e0df 69 52 6f 6f 74 50 61 67 65 20 3d 20 70 53 74 61  iRootPage = pSta
1e0e0 74 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c  t->tnum;.    sql
1e0e1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1e0e2 2c 20 4f 50 5f 43 6c 65 61 72 2c 20 70 53 74 61  , OP_Clear, pSta
1e0e3 74 2d 3e 74 6e 75 6d 2c 20 69 44 62 29 3b 0a 20  t->tnum, iDb);. 
1e0e4 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68   }..  /* Open th
1e0e5 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74  e sqlite_stat1 t
1e0e6 61 62 6c 65 20 66 6f 72 20 77 72 69 74 69 6e 67  able for writing
1e0e7 2e 20 55 6e 6c 65 73 73 20 69 74 20 77 61 73 20  . Unless it was 
1e0e8 63 72 65 61 74 65 64 0a 20 20 2a 2a 20 62 79 20  created.  ** by 
1e0e9 74 68 69 73 20 76 64 62 65 20 70 72 6f 67 72 61  this vdbe progra
1e0ea 6d 2c 20 6c 6f 63 6b 20 69 74 20 66 6f 72 20 77  m, lock it for w
1e0eb 72 69 74 69 6e 67 20 61 74 20 74 68 65 20 73 68  riting at the sh
1e0ec 61 72 65 64 2d 63 61 63 68 65 20 6c 65 76 65 6c  ared-cache level
1e0ed 2e 20 0a 20 20 2a 2a 20 49 66 20 74 68 69 73 20  . .  ** If this 
1e0ee 76 64 62 65 20 64 69 64 20 63 72 65 61 74 65 20  vdbe did create 
1e0ef 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74 31  the sqlite_stat1
1e0f0 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 69 74 20   table, then it 
1e0f1 6d 75 73 74 20 68 61 76 65 20 0a 20 20 2a 2a 20  must have .  ** 
1e0f2 61 6c 72 65 61 64 79 20 6f 62 74 61 69 6e 65 64  already obtained
1e0f3 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63 6b 2c 20   a schema-lock, 
1e0f4 6d 61 6b 69 6e 67 20 74 68 65 20 77 72 69 74 65  making the write
1e0f5 2d 6c 6f 63 6b 20 72 65 64 75 6e 64 61 6e 74 2e  -lock redundant.
1e0f6 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 72 65  .  */.  if( !cre
1e0f7 61 74 65 53 74 61 74 31 20 29 7b 0a 20 20 20 20  ateStat1 ){.    
1e0f8 73 71 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b  sqlite3TableLock
1e0f9 28 70 50 61 72 73 65 2c 20 69 44 62 2c 20 69 52  (pParse, iDb, iR
1e0fa 6f 6f 74 50 61 67 65 2c 20 31 2c 20 22 73 71 6c  ootPage, 1, "sql
1e0fb 69 74 65 5f 73 74 61 74 31 22 29 3b 0a 20 20 7d  ite_stat1");.  }
1e0fc 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1e0fd 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75  dOp2(v, OP_SetNu
1e0fe 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 33 29 3b  mColumns, 0, 3);
1e0ff 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
1e100 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp3(v, OP_OpenW
1e101 72 69 74 65 2c 20 69 53 74 61 74 43 75 72 2c 20  rite, iStatCur, 
1e102 69 52 6f 6f 74 50 61 67 65 2c 20 69 44 62 29 3b  iRootPage, iDb);
1e103 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68  .  sqlite3VdbeCh
1e104 61 6e 67 65 50 35 28 76 2c 20 63 72 65 61 74 65  angeP5(v, create
1e105 53 74 61 74 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  Stat1);.}../*.**
1e106 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74   Generate code t
1e107 6f 20 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73  o do an analysis
1e108 20 6f 66 20 61 6c 6c 20 69 6e 64 69 63 65 73 20   of all indices 
1e109 61 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 0a  associated with.
1e10a 2a 2a 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c  ** a single tabl
1e10b 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1e10c 64 20 61 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c  d analyzeOneTabl
1e10d 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
1e10e 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 65 72 20  se,   /* Parser 
1e10f 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62  context */.  Tab
1e110 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 2f 2a  le *pTab,     /*
1e111 20 54 61 62 6c 65 20 77 68 6f 73 65 20 69 6e 64   Table whose ind
1e112 69 63 65 73 20 61 72 65 20 74 6f 20 62 65 20 61  ices are to be a
1e113 6e 61 6c 79 7a 65 64 20 2a 2f 0a 20 20 69 6e 74  nalyzed */.  int
1e114 20 69 53 74 61 74 43 75 72 2c 20 20 20 20 2f 2a   iStatCur,    /*
1e115 20 49 6e 64 65 78 20 6f 66 20 56 64 62 65 43 75   Index of VdbeCu
1e116 72 73 6f 72 20 74 68 61 74 20 77 72 69 74 65 73  rsor that writes
1e117 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61 74   the sqlite_stat
1e118 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  1 table */.  int
1e119 20 69 4d 65 6d 20 20 20 20 20 20 20 20 20 2f 2a   iMem         /*
1e11a 20 41 76 61 69 6c 61 62 6c 65 20 6d 65 6d 6f 72   Available memor
1e11b 79 20 6c 6f 63 61 74 69 6f 6e 73 20 62 65 67 69  y locations begi
1e11c 6e 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 49  n here */.){.  I
1e11d 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
1e11e 2f 2a 20 41 6e 20 69 6e 64 65 78 20 74 6f 20 62  /* An index to b
1e11f 65 69 6e 67 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  eing analyzed */
1e120 0a 20 20 69 6e 74 20 69 49 64 78 43 75 72 3b 20  .  int iIdxCur; 
1e121 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
1e122 56 64 62 65 43 75 72 73 6f 72 20 66 6f 72 20 69  VdbeCursor for i
1e123 6e 64 65 78 20 62 65 69 6e 67 20 61 6e 61 6c 79  ndex being analy
1e124 7a 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f  zed */.  int nCo
1e125 6c 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  l;        /* Num
1e126 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69  ber of columns i
1e127 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20  n the index */. 
1e128 20 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20   Vdbe *v;       
1e129 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
1e12a 20 6d 61 63 68 69 6e 65 20 62 65 69 6e 67 20 62   machine being b
1e12b 75 69 6c 74 20 75 70 20 2a 2f 0a 20 20 69 6e 74  uilt up */.  int
1e12c 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a   i;           /*
1e12d 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
1e12e 0a 20 20 69 6e 74 20 74 6f 70 4f 66 4c 6f 6f 70  .  int topOfLoop
1e12f 3b 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f  ;   /* The top o
1e130 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
1e131 69 6e 74 20 65 6e 64 4f 66 4c 6f 6f 70 3b 20 20  int endOfLoop;  
1e132 20 2f 2a 20 54 68 65 20 65 6e 64 20 6f 66 20 74   /* The end of t
1e133 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74  he loop */.  int
1e134 20 61 64 64 72 3b 20 20 20 20 20 20 20 20 2f 2a   addr;        /*
1e135 20 54 68 65 20 61 64 64 72 65 73 73 20 6f 66 20   The address of 
1e136 61 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 2a  an instruction *
1e137 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
1e138 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1e139 20 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69   database contai
1e13a 6e 69 6e 67 20 70 54 61 62 20 2a 2f 0a 0a 20 20  ning pTab */..  
1e13b 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
1e13c 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
1e13d 28 20 76 3d 3d 30 20 7c 7c 20 70 54 61 62 3d 3d  ( v==0 || pTab==
1e13e 30 20 7c 7c 20 70 54 61 62 2d 3e 70 49 6e 64 65  0 || pTab->pInde
1e13f 78 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 44  x==0 ){.    /* D
1e140 6f 20 6e 6f 20 61 6e 61 6c 79 73 69 73 20 66 6f  o no analysis fo
1e141 72 20 74 61 62 6c 65 73 20 74 68 61 74 20 68 61  r tables that ha
1e142 76 65 20 6e 6f 20 69 6e 64 69 63 65 73 20 2a 2f  ve no indices */
1e143 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1e144 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74  .  assert( sqlit
1e145 65 33 42 74 72 65 65 48 6f 6c 64 73 41 6c 6c 4d  e3BtreeHoldsAllM
1e146 75 74 65 78 65 73 28 70 50 61 72 73 65 2d 3e 64  utexes(pParse->d
1e147 62 29 20 29 3b 0a 20 20 69 44 62 20 3d 20 73 71  b) );.  iDb = sq
1e148 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64  lite3SchemaToInd
1e149 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  ex(pParse->db, p
1e14a 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20  Tab->pSchema);. 
1e14b 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
1e14c 29 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  );.#ifndef SQLIT
1e14d 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1e14e 54 49 4f 4e 0a 20 20 69 66 28 20 73 71 6c 69 74  TION.  if( sqlit
1e14f 65 33 41 75 74 68 43 68 65 63 6b 28 70 50 61 72  e3AuthCheck(pPar
1e150 73 65 2c 20 53 51 4c 49 54 45 5f 41 4e 41 4c 59  se, SQLITE_ANALY
1e151 5a 45 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c  ZE, pTab->zName,
1e152 20 30 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65   0,.      pParse
1e153 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a  ->db->aDb[iDb].z
1e154 4e 61 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65  Name ) ){.    re
1e155 74 75 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  turn;.  }.#endif
1e156 0a 0a 20 20 2f 2a 20 45 73 74 61 62 6c 69 73 68  ..  /* Establish
1e157 20 61 20 72 65 61 64 2d 6c 6f 63 6b 20 6f 6e 20   a read-lock on 
1e158 74 68 65 20 74 61 62 6c 65 20 61 74 20 74 68 65  the table at the
1e159 20 73 68 61 72 65 64 2d 63 61 63 68 65 20 6c 65   shared-cache le
1e15a 76 65 6c 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65  vel. */.  sqlite
1e15b 33 54 61 62 6c 65 4c 6f 63 6b 28 70 50 61 72 73  3TableLock(pPars
1e15c 65 2c 20 69 44 62 2c 20 70 54 61 62 2d 3e 74 6e  e, iDb, pTab->tn
1e15d 75 6d 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61  um, 0, pTab->zNa
1e15e 6d 65 29 3b 0a 0a 20 20 69 49 64 78 43 75 72 20  me);..  iIdxCur 
1e15f 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a  = pParse->nTab;.
1e160 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d    for(pIdx=pTab-
1e161 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70  >pIndex; pIdx; p
1e162 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29  Idx=pIdx->pNext)
1e163 7b 0a 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70  {.    KeyInfo *p
1e164 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64  Key = sqlite3Ind
1e165 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65  exKeyinfo(pParse
1e166 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 6e 74  , pIdx);.    int
1e167 20 72 65 67 46 69 65 6c 64 73 3b 20 20 20 20 2f   regFields;    /
1e168 2a 20 52 65 67 69 73 74 65 72 20 62 6c 6f 63 6b  * Register block
1e169 20 66 6f 72 20 62 75 69 6c 64 69 6e 67 20 72 65   for building re
1e16a 63 6f 72 64 73 20 2a 2f 0a 20 20 20 20 69 6e 74  cords */.    int
1e16b 20 72 65 67 52 65 63 3b 20 20 20 20 20 20 20 2f   regRec;       /
1e16c 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69  * Register holdi
1e16d 6e 67 20 63 6f 6d 70 6c 65 74 65 64 20 72 65 63  ng completed rec
1e16e 6f 72 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 72  ord */.    int r
1e16f 65 67 54 65 6d 70 3b 20 20 20 20 20 20 2f 2a 20  egTemp;      /* 
1e170 54 65 6d 70 6f 72 61 72 79 20 75 73 65 20 72 65  Temporary use re
1e171 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20 69 6e  gister */.    in
1e172 74 20 72 65 67 43 6f 6c 3b 20 20 20 20 20 20 20  t regCol;       
1e173 2f 2a 20 43 6f 6e 74 65 6e 74 20 6f 66 20 61 20  /* Content of a 
1e174 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20  column from the 
1e175 74 61 62 6c 65 20 62 65 69 6e 67 20 61 6e 61 6c  table being anal
1e176 79 7a 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20  yzed */.    int 
1e177 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20 2f 2a  regRowid;     /*
1e178 20 52 6f 77 69 64 20 66 6f 72 20 74 68 65 20 69   Rowid for the i
1e179 6e 73 65 72 74 65 64 20 72 65 63 6f 72 64 20 2a  nserted record *
1e17a 2f 0a 20 20 20 20 69 6e 74 20 72 65 67 46 32 3b  /.    int regF2;
1e17b 0a 0a 20 20 20 20 2f 2a 20 4f 70 65 6e 20 61 20  ..    /* Open a 
1e17c 63 75 72 73 6f 72 20 74 6f 20 74 68 65 20 69 6e  cursor to the in
1e17d 64 65 78 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a  dex to be analyz
1e17e 65 64 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73  ed.    */.    as
1e17f 73 65 72 74 28 20 69 44 62 3d 3d 73 71 6c 69 74  sert( iDb==sqlit
1e180 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1e181 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 49 64 78  pParse->db, pIdx
1e182 2d 3e 70 53 63 68 65 6d 61 29 20 29 3b 0a 20 20  ->pSchema) );.  
1e183 20 20 6e 43 6f 6c 20 3d 20 70 49 64 78 2d 3e 6e    nCol = pIdx->n
1e184 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 73 71 6c 69  Column;.    sqli
1e185 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1e186 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e   OP_SetNumColumn
1e187 73 2c 20 30 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20  s, 0, nCol+1);. 
1e188 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e189 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 52  dOp4(v, OP_OpenR
1e18a 65 61 64 2c 20 69 49 64 78 43 75 72 2c 20 70 49  ead, iIdxCur, pI
1e18b 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c 0a 20  dx->tnum, iDb,. 
1e18c 20 20 20 20 20 20 20 28 63 68 61 72 20 2a 29 70         (char *)p
1e18d 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
1e18e 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56 64  HANDOFF);.    Vd
1e18f 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 25  beComment((v, "%
1e190 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65 29  s", pIdx->zName)
1e191 29 3b 0a 20 20 20 20 72 65 67 46 69 65 6c 64 73  );.    regFields
1e192 20 3d 20 69 4d 65 6d 2b 6e 43 6f 6c 2a 32 3b 0a   = iMem+nCol*2;.
1e193 20 20 20 20 72 65 67 54 65 6d 70 20 3d 20 72 65      regTemp = re
1e194 67 52 6f 77 69 64 20 3d 20 72 65 67 43 6f 6c 20  gRowid = regCol 
1e195 3d 20 72 65 67 46 69 65 6c 64 73 2b 33 3b 0a 20  = regFields+3;. 
1e196 20 20 20 72 65 67 52 65 63 20 3d 20 72 65 67 43     regRec = regC
1e197 6f 6c 2b 31 3b 0a 20 20 20 20 69 66 28 20 72 65  ol+1;.    if( re
1e198 67 52 65 63 3e 70 50 61 72 73 65 2d 3e 6e 4d 65  gRec>pParse->nMe
1e199 6d 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72 73  m ){.      pPars
1e19a 65 2d 3e 6e 4d 65 6d 20 3d 20 72 65 67 52 65 63  e->nMem = regRec
1e19b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
1e19c 4d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 61 72 65  Memory cells are
1e19d 20 75 73 65 64 20 61 73 20 66 6f 6c 6c 6f 77 73   used as follows
1e19e 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20  :.    **.    ** 
1e19f 20 20 20 6d 65 6d 5b 69 4d 65 6d 5d 3a 20 20 20     mem[iMem]:   
1e1a0 20 20 20 20 20 20 20 20 20 20 54 68 65 20 74 6f            The to
1e1a1 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  tal number of ro
1e1a2 77 73 20 69 6e 20 74 68 65 20 74 61 62 6c 65 2e  ws in the table.
1e1a3 0a 20 20 20 20 2a 2a 20 20 20 20 6d 65 6d 5b 69  .    **    mem[i
1e1a4 4d 65 6d 2b 31 5d 3a 20 20 20 20 20 20 20 20 20  Mem+1]:         
1e1a5 20 20 4e 75 6d 62 65 72 20 6f 66 20 64 69 73 74    Number of dist
1e1a6 69 6e 63 74 20 76 61 6c 75 65 73 20 69 6e 20 63  inct values in c
1e1a7 6f 6c 75 6d 6e 20 31 0a 20 20 20 20 2a 2a 20 20  olumn 1.    **  
1e1a8 20 20 2e 2e 2e 0a 20 20 20 20 2a 2a 20 20 20 20    ....    **    
1e1a9 6d 65 6d 5b 69 4d 65 6d 2b 6e 43 6f 6c 5d 3a 20  mem[iMem+nCol]: 
1e1aa 20 20 20 20 20 20 20 4e 75 6d 62 65 72 20 6f 66         Number of
1e1ab 20 64 69 73 74 69 6e 63 74 20 76 61 6c 75 65 73   distinct values
1e1ac 20 69 6e 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20   in column N.   
1e1ad 20 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b   **    mem[iMem+
1e1ae 6e 43 6f 6c 2b 31 5d 20 20 20 20 20 20 20 4c 61  nCol+1]       La
1e1af 73 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75  st observed valu
1e1b0 65 20 6f 66 20 63 6f 6c 75 6d 6e 20 31 0a 20 20  e of column 1.  
1e1b1 20 20 2a 2a 20 20 20 20 2e 2e 2e 0a 20 20 20 20    **    ....    
1e1b2 2a 2a 20 20 20 20 6d 65 6d 5b 69 4d 65 6d 2b 6e  **    mem[iMem+n
1e1b3 43 6f 6c 2b 6e 43 6f 6c 5d 3a 20 20 20 4c 61 73  Col+nCol]:   Las
1e1b4 74 20 6f 62 73 65 72 76 65 64 20 76 61 6c 75 65  t observed value
1e1b5 20 6f 66 20 63 6f 6c 75 6d 6e 20 4e 0a 20 20 20   of column N.   
1e1b6 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 65 6c 6c 73   **.    ** Cells
1e1b7 20 69 4d 65 6d 20 74 68 72 6f 75 67 68 20 69 4d   iMem through iM
1e1b8 65 6d 2b 6e 43 6f 6c 20 61 72 65 20 69 6e 69 74  em+nCol are init
1e1b9 69 61 6c 69 7a 65 64 20 74 6f 20 30 2e 20 20 54  ialized to 0.  T
1e1ba 68 65 20 6f 74 68 65 72 73 0a 20 20 20 20 2a 2a  he others.    **
1e1bb 20 61 72 65 20 69 6e 69 74 69 61 6c 69 7a 65 64   are initialized
1e1bc 20 74 6f 20 4e 55 4c 4c 2e 0a 20 20 20 20 2a 2f   to NULL..    */
1e1bd 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
1e1be 3d 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  =nCol; i++){.   
1e1bf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e1c0 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
1e1c1 65 72 2c 20 30 2c 20 69 4d 65 6d 2b 69 29 3b 0a  er, 0, iMem+i);.
1e1c2 20 20 20 20 7d 0a 20 20 20 20 66 6f 72 28 69 3d      }.    for(i=
1e1c3 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
1e1c4 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1e1c5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
1e1c6 75 6c 6c 2c 20 30 2c 20 69 4d 65 6d 2b 6e 43 6f  ull, 0, iMem+nCo
1e1c7 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d 0a 0a 20  l+i+1);.    }.. 
1e1c8 20 20 20 2f 2a 20 44 6f 20 74 68 65 20 61 6e 61     /* Do the ana
1e1c9 6c 79 73 69 73 2e 0a 20 20 20 20 2a 2f 0a 20 20  lysis..    */.  
1e1ca 20 20 65 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71    endOfLoop = sq
1e1cb 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
1e1cc 65 6c 28 76 29 3b 0a 20 20 20 20 73 71 6c 69 74  el(v);.    sqlit
1e1cd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e1ce 4f 50 5f 52 65 77 69 6e 64 2c 20 69 49 64 78 43  OP_Rewind, iIdxC
1e1cf 75 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a  ur, endOfLoop);.
1e1d0 20 20 20 20 74 6f 70 4f 66 4c 6f 6f 70 20 3d 20      topOfLoop = 
1e1d1 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65  sqlite3VdbeCurre
1e1d2 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 73  ntAddr(v);.    s
1e1d3 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1e1d4 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 69  (v, OP_AddImm, i
1e1d5 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20 66 6f 72  Mem, 1);.    for
1e1d6 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b  (i=0; i<nCol; i+
1e1d7 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
1e1d8 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
1e1d9 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64 78 43 75  P_Column, iIdxCu
1e1da 72 2c 20 69 2c 20 72 65 67 43 6f 6c 29 3b 0a 20  r, i, regCol);. 
1e1db 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1e1dc 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 65 2c  AddOp3(v, OP_Ne,
1e1dd 20 72 65 67 43 6f 6c 2c 20 30 2c 20 69 4d 65 6d   regCol, 0, iMem
1e1de 2b 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20  +nCol+i+1);.    
1e1df 20 20 2f 2a 2a 2a 2a 20 54 4f 44 4f 3a 20 20 61    /**** TODO:  a
1e1e0 64 64 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  dd collating seq
1e1e1 75 65 6e 63 65 20 2a 2a 2a 2a 2a 2f 0a 20 20 20  uence *****/.   
1e1e2 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
1e1e3 61 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45  angeP5(v, SQLITE
1e1e4 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
1e1e5 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56    }.    sqlite3V
1e1e6 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
1e1e7 47 6f 74 6f 2c 20 30 2c 20 65 6e 64 4f 66 4c 6f  Goto, 0, endOfLo
1e1e8 6f 70 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  op);.    for(i=0
1e1e9 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
1e1ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e1eb 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 74 6f 70  eJumpHere(v, top
1e1ec 4f 66 4c 6f 6f 70 20 2b 20 32 2a 28 69 20 2b 20  OfLoop + 2*(i + 
1e1ed 31 29 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  1));.      sqlit
1e1ee 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1e1ef 4f 50 5f 41 64 64 49 6d 6d 2c 20 69 4d 65 6d 2b  OP_AddImm, iMem+
1e1f0 69 2b 31 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  i+1, 1);.      s
1e1f1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
1e1f2 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69  (v, OP_Column, i
1e1f3 49 64 78 43 75 72 2c 20 69 2c 20 69 4d 65 6d 2b  IdxCur, i, iMem+
1e1f4 6e 43 6f 6c 2b 69 2b 31 29 3b 0a 20 20 20 20 7d  nCol+i+1);.    }
1e1f5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
1e1f6 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
1e1f7 65 6e 64 4f 66 4c 6f 6f 70 29 3b 0a 20 20 20 20  endOfLoop);.    
1e1f8 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e1f9 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 49  2(v, OP_Next, iI
1e1fa 64 78 43 75 72 2c 20 74 6f 70 4f 66 4c 6f 6f 70  dxCur, topOfLoop
1e1fb 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1e1fc 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43  beAddOp1(v, OP_C
1e1fd 6c 6f 73 65 2c 20 69 49 64 78 43 75 72 29 3b 0a  lose, iIdxCur);.
1e1fe 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68  .    /* Store th
1e1ff 65 20 72 65 73 75 6c 74 73 2e 20 20 0a 20 20 20  e results.  .   
1e200 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 72   **.    ** The r
1e201 65 73 75 6c 74 20 69 73 20 61 20 73 69 6e 67 6c  esult is a singl
1e202 65 20 72 6f 77 20 6f 66 20 74 68 65 20 73 71 6c  e row of the sql
1e203 69 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e  ite_stat1 table.
1e204 20 20 54 68 65 20 66 69 72 73 74 0a 20 20 20 20    The first.    
1e205 2a 2a 20 74 77 6f 20 63 6f 6c 75 6d 6e 73 20 61  ** two columns a
1e206 72 65 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20  re the names of 
1e207 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 69 6e  the table and in
1e208 64 65 78 2e 20 20 54 68 65 20 74 68 69 72 64 20  dex.  The third 
1e209 63 6f 6c 75 6d 6e 0a 20 20 20 20 2a 2a 20 69 73  column.    ** is
1e20a 20 61 20 73 74 72 69 6e 67 20 63 6f 6d 70 6f 73   a string compos
1e20b 65 64 20 6f 66 20 61 20 6c 69 73 74 20 6f 66 20  ed of a list of 
1e20c 69 6e 74 65 67 65 72 20 73 74 61 74 69 73 74 69  integer statisti
1e20d 63 73 20 61 62 6f 75 74 20 74 68 65 0a 20 20 20  cs about the.   
1e20e 20 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65 20   ** index.  The 
1e20f 66 69 72 73 74 20 69 6e 74 65 67 65 72 20 69 6e  first integer in
1e210 20 74 68 65 20 6c 69 73 74 20 69 73 20 74 68 65   the list is the
1e211 20 74 6f 74 61 6c 20 6e 75 6d 62 65 72 20 6f 66   total number of
1e212 20 65 6e 74 69 72 65 73 0a 20 20 20 20 2a 2a 20   entires.    ** 
1e213 69 6e 20 74 68 65 20 69 6e 64 65 78 2e 20 20 54  in the index.  T
1e214 68 65 72 65 20 69 73 20 6f 6e 65 20 61 64 64 69  here is one addi
1e215 74 69 6f 6e 61 6c 20 69 6e 74 65 67 65 72 20 69  tional integer i
1e216 6e 20 74 68 65 20 6c 69 73 74 20 66 6f 72 20 65  n the list for e
1e217 61 63 68 0a 20 20 20 20 2a 2a 20 63 6f 6c 75 6d  ach.    ** colum
1e218 6e 20 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20  n of the table. 
1e219 20 54 68 69 73 20 61 64 64 69 74 69 6f 6e 61 6c   This additional
1e21a 20 69 6e 74 65 67 65 72 20 69 73 20 61 20 67 75   integer is a gu
1e21b 65 73 73 20 6f 66 20 68 6f 77 20 6d 61 6e 79 0a  ess of how many.
1e21c 20 20 20 20 2a 2a 20 72 6f 77 73 20 6f 66 20 74      ** rows of t
1e21d 68 65 20 74 61 62 6c 65 20 74 68 65 20 69 6e 64  he table the ind
1e21e 65 78 20 77 69 6c 6c 20 73 65 6c 65 63 74 2e 20  ex will select. 
1e21f 20 49 66 20 44 20 69 73 20 74 68 65 20 63 6f 75   If D is the cou
1e220 6e 74 20 6f 66 20 64 69 73 74 69 6e 63 74 0a 20  nt of distinct. 
1e221 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 61 6e 64     ** values and
1e222 20 4b 20 69 73 20 74 68 65 20 74 6f 74 61 6c 20   K is the total 
1e223 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 2c 20  number of rows, 
1e224 74 68 65 6e 20 74 68 65 20 69 6e 74 65 67 65 72  then the integer
1e225 20 69 73 20 63 6f 6d 70 75 74 65 64 0a 20 20 20   is computed.   
1e226 20 2a 2a 20 61 73 3a 0a 20 20 20 20 2a 2a 0a 20   ** as:.    **. 
1e227 20 20 20 2a 2a 20 20 20 20 20 20 20 20 49 20 3d     **        I =
1e228 20 28 4b 2b 44 2d 31 29 2f 44 0a 20 20 20 20 2a   (K+D-1)/D.    *
1e229 2a 0a 20 20 20 20 2a 2a 20 49 66 20 4b 3d 3d 30  *.    ** If K==0
1e22a 20 74 68 65 6e 20 6e 6f 20 65 6e 74 72 79 20 69   then no entry i
1e22b 73 20 6d 61 64 65 20 69 6e 74 6f 20 74 68 65 20  s made into the 
1e22c 73 71 6c 69 74 65 5f 73 74 61 74 31 20 74 61 62  sqlite_stat1 tab
1e22d 6c 65 2e 20 20 0a 20 20 20 20 2a 2a 20 49 66 20  le.  .    ** If 
1e22e 4b 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61  K>0 then it is a
1e22f 6c 77 61 79 73 20 74 68 65 20 63 61 73 65 20 74  lways the case t
1e230 68 65 20 44 3e 30 20 73 6f 20 64 69 76 69 73 69  he D>0 so divisi
1e231 6f 6e 20 62 79 20 7a 65 72 6f 0a 20 20 20 20 2a  on by zero.    *
1e232 2a 20 69 73 20 6e 65 76 65 72 20 70 6f 73 73 69  * is never possi
1e233 62 6c 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ble..    */.    
1e234 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
1e235 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49  beAddOp1(v, OP_I
1e236 66 4e 6f 74 2c 20 69 4d 65 6d 29 3b 0a 20 20 20  fNot, iMem);.   
1e237 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1e238 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
1e239 2c 20 30 2c 20 72 65 67 46 69 65 6c 64 73 2c 20  , 0, regFields, 
1e23a 30 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20  0, pTab->zName, 
1e23b 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
1e23c 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1e23d 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 46  String8, 0, regF
1e23e 69 65 6c 64 73 2b 31 2c 20 30 2c 20 70 49 64 78  ields+1, 0, pIdx
1e23f 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
1e240 20 72 65 67 46 32 20 3d 20 72 65 67 46 69 65 6c   regF2 = regFiel
1e241 64 73 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65  ds+2;.    sqlite
1e242 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
1e243 50 5f 53 43 6f 70 79 2c 20 69 4d 65 6d 2c 20 72  P_SCopy, iMem, r
1e244 65 67 46 32 29 3b 0a 20 20 20 20 66 6f 72 28 69  egF2);.    for(i
1e245 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
1e246 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
1e247 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1e248 53 74 72 69 6e 67 38 2c 20 30 2c 20 72 65 67 54  String8, 0, regT
1e249 65 6d 70 2c 20 30 2c 20 22 20 22 2c 20 30 29 3b  emp, 0, " ", 0);
1e24a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
1e24b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
1e24c 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c 20  oncat, regTemp, 
1e24d 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a 20  regF2, regF2);. 
1e24e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1e24f 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64  AddOp3(v, OP_Add
1e250 2c 20 69 4d 65 6d 2c 20 69 4d 65 6d 2b 69 2b 31  , iMem, iMem+i+1
1e251 2c 20 72 65 67 54 65 6d 70 29 3b 0a 20 20 20 20  , regTemp);.    
1e252 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1e253 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d  Op2(v, OP_AddImm
1e254 2c 20 72 65 67 54 65 6d 70 2c 20 2d 31 29 3b 0a  , regTemp, -1);.
1e255 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1e256 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 44 69  eAddOp3(v, OP_Di
1e257 76 69 64 65 2c 20 69 4d 65 6d 2b 69 2b 31 2c 20  vide, iMem+i+1, 
1e258 72 65 67 54 65 6d 70 2c 20 72 65 67 54 65 6d 70  regTemp, regTemp
1e259 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
1e25a 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
1e25b 5f 54 6f 49 6e 74 2c 20 72 65 67 54 65 6d 70 29  _ToInt, regTemp)
1e25c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1e25d 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1e25e 43 6f 6e 63 61 74 2c 20 72 65 67 54 65 6d 70 2c  Concat, regTemp,
1e25f 20 72 65 67 46 32 2c 20 72 65 67 46 32 29 3b 0a   regF2, regF2);.
1e260 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
1e261 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1e262 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65  P_MakeRecord, re
1e263 67 46 69 65 6c 64 73 2c 20 33 2c 20 72 65 67 52  gFields, 3, regR
1e264 65 63 2c 20 22 61 61 61 22 2c 20 30 29 3b 0a 20  ec, "aaa", 0);. 
1e265 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1e266 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f  dOp2(v, OP_NewRo
1e267 77 69 64 2c 20 69 53 74 61 74 43 75 72 2c 20 72  wid, iStatCur, r
1e268 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73 71  egRowid);.    sq
1e269 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
1e26a 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 69 53  v, OP_Insert, iS
1e26b 74 61 74 43 75 72 2c 20 72 65 67 52 65 63 2c 20  tatCur, regRec, 
1e26c 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 73  regRowid);.    s
1e26d 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
1e26e 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50 50  P5(v, OPFLAG_APP
1e26f 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  END);.    sqlite
1e270 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1e271 20 61 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f   addr);.  }.}../
1e272 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f  *.** Generate co
1e273 64 65 20 74 68 61 74 20 77 69 6c 6c 20 63 61 75  de that will cau
1e274 73 65 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  se the most rece
1e275 6e 74 20 69 6e 64 65 78 20 61 6e 61 6c 79 73 69  nt index analysi
1e276 73 20 74 6f 0a 2a 2a 20 62 65 20 6c 61 6f 64 65  s to.** be laode
1e277 64 20 69 6e 74 6f 20 69 6e 74 65 72 6e 61 6c 20  d into internal 
1e278 68 61 73 68 20 74 61 62 6c 65 73 20 77 68 65 72  hash tables wher
1e279 65 20 69 73 20 63 61 6e 20 62 65 20 75 73 65 64  e is can be used
1e27a 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
1e27b 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73 28 50 61   loadAnalysis(Pa
1e27c 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e 74  rse *pParse, int
1e27d 20 69 44 62 29 7b 0a 20 20 56 64 62 65 20 2a 76   iDb){.  Vdbe *v
1e27e 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
1e27f 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28  e(pParse);.  if(
1e280 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65   v ){.    sqlite
1e281 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
1e282 50 5f 4c 6f 61 64 41 6e 61 6c 79 73 69 73 2c 20  P_LoadAnalysis, 
1e283 69 44 62 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  iDb);.  }.}../*.
1e284 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  ** Generate code
1e285 20 74 68 61 74 20 77 69 6c 6c 20 64 6f 20 61 6e   that will do an
1e286 20 61 6e 61 6c 79 73 69 73 20 6f 66 20 61 6e 20   analysis of an 
1e287 65 6e 74 69 72 65 20 64 61 74 61 62 61 73 65 0a  entire database.
1e288 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 61  */.static void a
1e289 6e 61 6c 79 7a 65 44 61 74 61 62 61 73 65 28 50  nalyzeDatabase(P
1e28a 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
1e28b 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65  t iDb){.  sqlite
1e28c 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e  3 *db = pParse->
1e28d 64 62 3b 0a 20 20 53 63 68 65 6d 61 20 2a 70 53  db;.  Schema *pS
1e28e 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61 44 62 5b  chema = db->aDb[
1e28f 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b 20 20 20  iDb].pSchema;   
1e290 20 2f 2a 20 53 63 68 65 6d 61 20 6f 66 20 64 61   /* Schema of da
1e291 74 61 62 61 73 65 20 69 44 62 20 2a 2f 0a 20 20  tabase iDb */.  
1e292 48 61 73 68 45 6c 65 6d 20 2a 6b 3b 0a 20 20 69  HashElem *k;.  i
1e293 6e 74 20 69 53 74 61 74 43 75 72 3b 0a 20 20 69  nt iStatCur;.  i
1e294 6e 74 20 69 4d 65 6d 3b 0a 0a 20 20 73 71 6c 69  nt iMem;..  sqli
1e295 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
1e296 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 30  ration(pParse, 0
1e297 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74 43  , iDb);.  iStatC
1e298 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
1e299 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74 54  b++;.  openStatT
1e29a 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44 62  able(pParse, iDb
1e29b 2c 20 69 53 74 61 74 43 75 72 2c 20 30 29 3b 0a  , iStatCur, 0);.
1e29c 20 20 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d    iMem = pParse-
1e29d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20 66 6f 72 28 6b  >nMem+1;.  for(k
1e29e 3d 73 71 6c 69 74 65 48 61 73 68 46 69 72 73 74  =sqliteHashFirst
1e29f 28 26 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61  (&pSchema->tblHa
1e2a0 73 68 29 3b 20 6b 3b 20 6b 3d 73 71 6c 69 74 65  sh); k; k=sqlite
1e2a1 48 61 73 68 4e 65 78 74 28 6b 29 29 7b 0a 20 20  HashNext(k)){.  
1e2a2 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1e2a3 28 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61  (Table*)sqliteHa
1e2a4 73 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 61  shData(k);.    a
1e2a5 6e 61 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70  nalyzeOneTable(p
1e2a6 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74  Parse, pTab, iSt
1e2a7 61 74 43 75 72 2c 20 69 4d 65 6d 29 3b 0a 20 20  atCur, iMem);.  
1e2a8 7d 0a 20 20 6c 6f 61 64 41 6e 61 6c 79 73 69 73  }.  loadAnalysis
1e2a9 28 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 7d  (pParse, iDb);.}
1e2aa 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65  ../*.** Generate
1e2ab 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c 20   code that will 
1e2ac 64 6f 20 61 6e 20 61 6e 61 6c 79 73 69 73 20 6f  do an analysis o
1e2ad 66 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  f a single table
1e2ae 20 69 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73   in.** a databas
1e2af 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  e..*/.static voi
1e2b0 64 20 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 50  d analyzeTable(P
1e2b1 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61  arse *pParse, Ta
1e2b2 62 6c 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e  ble *pTab){.  in
1e2b3 74 20 69 44 62 3b 0a 20 20 69 6e 74 20 69 53 74  t iDb;.  int iSt
1e2b4 61 74 43 75 72 3b 0a 0a 20 20 61 73 73 65 72 74  atCur;..  assert
1e2b5 28 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 61  ( pTab!=0 );.  a
1e2b6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 42 74  ssert( sqlite3Bt
1e2b7 72 65 65 48 6f 6c 64 73 41 6c 6c 4d 75 74 65 78  reeHoldsAllMutex
1e2b8 65 73 28 70 50 61 72 73 65 2d 3e 64 62 29 20 29  es(pParse->db) )
1e2b9 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  ;.  iDb = sqlite
1e2ba 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70  3SchemaToIndex(p
1e2bb 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d  Parse->db, pTab-
1e2bc 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c  >pSchema);.  sql
1e2bd 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
1e2be 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
1e2bf 30 2c 20 69 44 62 29 3b 0a 20 20 69 53 74 61 74  0, iDb);.  iStat
1e2c0 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  Cur = pParse->nT
1e2c1 61 62 2b 2b 3b 0a 20 20 6f 70 65 6e 53 74 61 74  ab++;.  openStat
1e2c2 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 44  Table(pParse, iD
1e2c3 62 2c 20 69 53 74 61 74 43 75 72 2c 20 70 54 61  b, iStatCur, pTa
1e2c4 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 61 6e 61  b->zName);.  ana
1e2c5 6c 79 7a 65 4f 6e 65 54 61 62 6c 65 28 70 50 61  lyzeOneTable(pPa
1e2c6 72 73 65 2c 20 70 54 61 62 2c 20 69 53 74 61 74  rse, pTab, iStat
1e2c7 43 75 72 2c 20 70 50 61 72 73 65 2d 3e 6e 4d 65  Cur, pParse->nMe
1e2c8 6d 2b 31 29 3b 0a 20 20 6c 6f 61 64 41 6e 61 6c  m+1);.  loadAnal
1e2c9 79 73 69 73 28 70 50 61 72 73 65 2c 20 69 44 62  ysis(pParse, iDb
1e2ca 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  );.}../*.** Gene
1e2cb 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74 68  rate code for th
1e2cc 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61 6e  e ANALYZE comman
1e2cd 64 2e 20 20 54 68 65 20 70 61 72 73 65 72 20 63  d.  The parser c
1e2ce 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e  alls this routin
1e2cf 65 0a 2a 2a 20 77 68 65 6e 20 69 74 20 72 65 63  e.** when it rec
1e2d0 6f 67 6e 69 7a 65 73 20 61 6e 20 41 4e 41 4c 59  ognizes an ANALY
1e2d1 5a 45 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a  ZE command..**.*
1e2d2 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59 5a 45  *        ANALYZE
1e2d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e2d4 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d 20 31              -- 1
1e2d5 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41 4c 59  .**        ANALY
1e2d6 5a 45 20 20 3c 64 61 74 61 62 61 73 65 3e 20 20  ZE  <database>  
1e2d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 2d                --
1e2d8 20 32 0a 2a 2a 20 20 20 20 20 20 20 20 41 4e 41   2.**        ANA
1e2d9 4c 59 5a 45 20 20 3f 3c 64 61 74 61 62 61 73 65  LYZE  ?<database
1e2da 3e 2e 3f 3c 74 61 62 6c 65 6e 61 6d 65 3e 20 20  >.?<tablename>  
1e2db 2d 2d 20 33 0a 2a 2a 0a 2a 2a 20 46 6f 72 6d 20  -- 3.**.** Form 
1e2dc 31 20 63 61 75 73 65 73 20 61 6c 6c 20 69 6e 64  1 causes all ind
1e2dd 69 63 65 73 20 69 6e 20 61 6c 6c 20 61 74 74 61  ices in all atta
1e2de 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 74  ched databases t
1e2df 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 2e 0a 2a  o be analyzed..*
1e2e0 2a 20 46 6f 72 6d 20 32 20 61 6e 61 6c 79 7a 65  * Form 2 analyze
1e2e1 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 74 68  s all indices th
1e2e2 65 20 73 69 6e 67 6c 65 20 64 61 74 61 62 61 73  e single databas
1e2e3 65 20 6e 61 6d 65 64 2e 0a 2a 2a 20 46 6f 72 6d  e named..** Form
1e2e4 20 33 20 61 6e 61 6c 79 7a 65 73 20 61 6c 6c 20   3 analyzes all 
1e2e5 69 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74  indices associat
1e2e6 65 64 20 77 69 74 68 20 74 68 65 20 6e 61 6d 65  ed with the name
1e2e7 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49  d table..*/.SQLI
1e2e8 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1e2e9 73 71 6c 69 74 65 33 41 6e 61 6c 79 7a 65 28 50  sqlite3Analyze(P
1e2ea 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
1e2eb 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b  ken *pName1, Tok
1e2ec 65 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 73  en *pName2){.  s
1e2ed 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
1e2ee 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69  rse->db;.  int i
1e2ef 44 62 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63  Db;.  int i;.  c
1e2f0 68 61 72 20 2a 7a 2c 20 2a 7a 44 62 3b 0a 20 20  har *z, *zDb;.  
1e2f1 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 54  Table *pTab;.  T
1e2f2 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
1e2f3 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
1e2f4 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1e2f5 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1e2f6 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65  curs, leave an e
1e2f7 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a  rror message.  *
1e2f8 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50  * and code in pP
1e2f9 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  arse and return 
1e2fa 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 61 73 73 65 72  NULL. */.  asser
1e2fb 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
1e2fc 6f 6c 64 73 41 6c 6c 4d 75 74 65 78 65 73 28 70  oldsAllMutexes(p
1e2fd 50 61 72 73 65 2d 3e 64 62 29 20 29 3b 0a 20 20  Parse->db) );.  
1e2fe 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73  if( SQLITE_OK!=s
1e2ff 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61  qlite3ReadSchema
1e300 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
1e301 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 69  return;.  }..  i
1e302 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 29 7b 0a  f( pName1==0 ){.
1e303 20 20 20 20 2f 2a 20 46 6f 72 6d 20 31 3a 20 20      /* Form 1:  
1e304 41 6e 61 6c 79 7a 65 20 65 76 65 72 79 74 68 69  Analyze everythi
1e305 6e 67 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  ng */.    for(i=
1e306 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1e307 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 3d  +){.      if( i=
1e308 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b 20 20  =1 ) continue;  
1e309 2f 2a 20 44 6f 20 6e 6f 74 20 61 6e 61 6c 79 7a  /* Do not analyz
1e30a 65 20 74 68 65 20 54 45 4d 50 20 64 61 74 61 62  e the TEMP datab
1e30b 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 61 6e 61  ase */.      ana
1e30c 6c 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61  lyzeDatabase(pPa
1e30d 72 73 65 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20  rse, i);.    }. 
1e30e 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d 65   }else if( pName
1e30f 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e  2==0 || pName2->
1e310 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 46  n==0 ){.    /* F
1e311 6f 72 6d 20 32 3a 20 20 41 6e 61 6c 79 7a 65 20  orm 2:  Analyze 
1e312 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72 20  the database or 
1e313 74 61 62 6c 65 20 6e 61 6d 65 64 20 2a 2f 0a 20  table named */. 
1e314 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33     iDb = sqlite3
1e315 46 69 6e 64 44 62 28 64 62 2c 20 70 4e 61 6d 65  FindDb(db, pName
1e316 31 29 3b 0a 20 20 20 20 69 66 28 20 69 44 62 3e  1);.    if( iDb>
1e317 3d 30 20 29 7b 0a 20 20 20 20 20 20 61 6e 61 6c  =0 ){.      anal
1e318 79 7a 65 44 61 74 61 62 61 73 65 28 70 50 61 72  yzeDatabase(pPar
1e319 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20 7d 65  se, iDb);.    }e
1e31a 6c 73 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73  lse{.      z = s
1e31b 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
1e31c 6b 65 6e 28 64 62 2c 20 70 4e 61 6d 65 31 29 3b  ken(db, pName1);
1e31d 0a 20 20 20 20 20 20 69 66 28 20 7a 20 29 7b 0a  .      if( z ){.
1e31e 20 20 20 20 20 20 20 20 70 54 61 62 20 3d 20 73          pTab = s
1e31f 71 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c  qlite3LocateTabl
1e320 65 28 70 50 61 72 73 65 2c 20 30 2c 20 7a 2c 20  e(pParse, 0, z, 
1e321 30 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  0);.        sqli
1e322 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
1e323 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54  ;.        if( pT
1e324 61 62 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ab ){.          
1e325 61 6e 61 6c 79 7a 65 54 61 62 6c 65 28 70 50 61  analyzeTable(pPa
1e326 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
1e327 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
1e328 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1e329 20 2f 2a 20 46 6f 72 6d 20 33 3a 20 41 6e 61 6c   /* Form 3: Anal
1e32a 79 7a 65 20 74 68 65 20 66 75 6c 6c 79 20 71 75  yze the fully qu
1e32b 61 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61  alified table na
1e32c 6d 65 20 2a 2f 0a 20 20 20 20 69 44 62 20 3d 20  me */.    iDb = 
1e32d 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
1e32e 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d 65  me(pParse, pName
1e32f 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 54 61 62  1, pName2, &pTab
1e330 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28  leName);.    if(
1e331 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20 20   iDb>=0 ){.     
1e332 20 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69   zDb = db->aDb[i
1e333 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  Db].zName;.     
1e334 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65   z = sqlite3Name
1e335 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54  FromToken(db, pT
1e336 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ableName);.     
1e337 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20   if( z ){.      
1e338 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
1e339 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61 72  LocateTable(pPar
1e33a 73 65 2c 20 30 2c 20 7a 2c 20 7a 44 62 29 3b 0a  se, 0, z, zDb);.
1e33b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
1e33c 62 46 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20  bFree(db, z);.  
1e33d 20 20 20 20 20 20 69 66 28 20 70 54 61 62 20 29        if( pTab )
1e33e 7b 0a 20 20 20 20 20 20 20 20 20 20 61 6e 61 6c  {.          anal
1e33f 79 7a 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  yzeTable(pParse,
1e340 20 70 54 61 62 29 3b 0a 20 20 20 20 20 20 20 20   pTab);.        
1e341 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 20  }.      }.    } 
1e342 20 20 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20    .  }.}../*.** 
1e343 55 73 65 64 20 74 6f 20 70 61 73 73 20 69 6e 66  Used to pass inf
1e344 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68  ormation from th
1e345 65 20 61 6e 61 6c 79 7a 65 72 20 72 65 61 64 65  e analyzer reade
1e346 72 20 74 68 72 6f 75 67 68 20 74 6f 20 74 68 65  r through to the
1e347 0a 2a 2a 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75  .** callback rou
1e348 74 69 6e 65 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tine..*/.typedef
1e349 20 73 74 72 75 63 74 20 61 6e 61 6c 79 73 69 73   struct analysis
1e34a 49 6e 66 6f 20 61 6e 61 6c 79 73 69 73 49 6e 66  Info analysisInf
1e34b 6f 3b 0a 73 74 72 75 63 74 20 61 6e 61 6c 79 73  o;.struct analys
1e34c 69 73 49 6e 66 6f 20 7b 0a 20 20 73 71 6c 69 74  isInfo {.  sqlit
1e34d 65 33 20 2a 64 62 3b 0a 20 20 63 6f 6e 73 74 20  e3 *db;.  const 
1e34e 63 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 3b  char *zDatabase;
1e34f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  .};../*.** This 
1e350 63 61 6c 6c 62 61 63 6b 20 69 73 20 69 6e 76 6f  callback is invo
1e351 6b 65 64 20 6f 6e 63 65 20 66 6f 72 20 65 61 63  ked once for eac
1e352 68 20 69 6e 64 65 78 20 77 68 65 6e 20 72 65 61  h index when rea
1e353 64 69 6e 67 20 74 68 65 0a 2a 2a 20 73 71 6c 69  ding the.** sqli
1e354 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2e 20  te_stat1 table. 
1e355 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20 61 72 67 76   .**.**     argv
1e356 5b 30 5d 20 3d 20 6e 61 6d 65 20 6f 66 20 74 68  [0] = name of th
1e357 65 20 69 6e 64 65 78 0a 2a 2a 20 20 20 20 20 61  e index.**     a
1e358 72 67 76 5b 31 5d 20 3d 20 72 65 73 75 6c 74 73  rgv[1] = results
1e359 20 6f 66 20 61 6e 61 6c 79 73 69 73 20 2d 20 6f   of analysis - o
1e35a 6e 20 69 6e 74 65 67 65 72 20 66 6f 72 20 65 61  n integer for ea
1e35b 63 68 20 63 6f 6c 75 6d 6e 0a 2a 2f 0a 73 74 61  ch column.*/.sta
1e35c 74 69 63 20 69 6e 74 20 61 6e 61 6c 79 73 69 73  tic int analysis
1e35d 4c 6f 61 64 65 72 28 76 6f 69 64 20 2a 70 44 61  Loader(void *pDa
1e35e 74 61 2c 20 69 6e 74 20 61 72 67 63 2c 20 63 68  ta, int argc, ch
1e35f 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72 20  ar **argv, char 
1e360 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 61 6e  **NotUsed){.  an
1e361 61 6c 79 73 69 73 49 6e 66 6f 20 2a 70 49 6e 66  alysisInfo *pInf
1e362 6f 20 3d 20 28 61 6e 61 6c 79 73 69 73 49 6e 66  o = (analysisInf
1e363 6f 2a 29 70 44 61 74 61 3b 0a 20 20 49 6e 64 65  o*)pData;.  Inde
1e364 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 69 6e 74  x *pIndex;.  int
1e365 20 69 2c 20 63 3b 0a 20 20 75 6e 73 69 67 6e 65   i, c;.  unsigne
1e366 64 20 69 6e 74 20 76 3b 0a 20 20 63 6f 6e 73 74  d int v;.  const
1e367 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73   char *z;..  ass
1e368 65 72 74 28 20 61 72 67 63 3d 3d 32 20 29 3b 0a  ert( argc==2 );.
1e369 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
1e36a 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 61 72 67  ER2(NotUsed, arg
1e36b 63 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 76 3d  c);..  if( argv=
1e36c 3d 30 20 7c 7c 20 61 72 67 76 5b 30 5d 3d 3d 30  =0 || argv[0]==0
1e36d 20 7c 7c 20 61 72 67 76 5b 31 5d 3d 3d 30 20 29   || argv[1]==0 )
1e36e 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
1e36f 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
1e370 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
1e371 70 49 6e 66 6f 2d 3e 64 62 2c 20 61 72 67 76 5b  pInfo->db, argv[
1e372 30 5d 2c 20 70 49 6e 66 6f 2d 3e 7a 44 61 74 61  0], pInfo->zData
1e373 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 49 6e  base);.  if( pIn
1e374 64 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  dex==0 ){.    re
1e375 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 7a 20  turn 0;.  }.  z 
1e376 3d 20 61 72 67 76 5b 31 5d 3b 0a 20 20 66 6f 72  = argv[1];.  for
1e377 28 69 3d 30 3b 20 2a 7a 20 26 26 20 69 3c 3d 70  (i=0; *z && i<=p
1e378 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20  Index->nColumn; 
1e379 69 2b 2b 29 7b 0a 20 20 20 20 76 20 3d 20 30 3b  i++){.    v = 0;
1e37a 0a 20 20 20 20 77 68 69 6c 65 28 20 28 63 3d 7a  .    while( (c=z
1e37b 5b 30 5d 29 3e 3d 27 30 27 20 26 26 20 63 3c 3d  [0])>='0' && c<=
1e37c 27 39 27 20 29 7b 0a 20 20 20 20 20 20 76 20 3d  '9' ){.      v =
1e37d 20 76 2a 31 30 20 2b 20 63 20 2d 20 27 30 27 3b   v*10 + c - '0';
1e37e 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
1e37f 7d 0a 20 20 20 20 70 49 6e 64 65 78 2d 3e 61 69  }.    pIndex->ai
1e380 52 6f 77 45 73 74 5b 69 5d 20 3d 20 76 3b 0a 20  RowEst[i] = v;. 
1e381 20 20 20 69 66 28 20 2a 7a 3d 3d 27 20 27 20 29     if( *z==' ' )
1e382 20 7a 2b 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75   z++;.  }.  retu
1e383 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c  rn 0;.}../*.** L
1e384 6f 61 64 20 74 68 65 20 63 6f 6e 74 65 6e 74 20  oad the content 
1e385 6f 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74  of the sqlite_st
1e386 61 74 31 20 74 61 62 6c 65 20 69 6e 74 6f 20 74  at1 table into t
1e387 68 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61  he index hash ta
1e388 62 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bles..*/.SQLITE_
1e389 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
1e38a 74 65 33 41 6e 61 6c 79 73 69 73 4c 6f 61 64 28  te3AnalysisLoad(
1e38b 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
1e38c 20 69 44 62 29 7b 0a 20 20 61 6e 61 6c 79 73 69   iDb){.  analysi
1e38d 73 49 6e 66 6f 20 73 49 6e 66 6f 3b 0a 20 20 48  sInfo sInfo;.  H
1e38e 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 63 68  ashElem *i;.  ch
1e38f 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 69 6e 74 20  ar *zSql;.  int 
1e390 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69  rc;..  assert( i
1e391 44 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d  Db>=0 && iDb<db-
1e392 3e 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74  >nDb );.  assert
1e393 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70  ( db->aDb[iDb].p
1e394 42 74 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72  Bt!=0 );.  asser
1e395 74 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 48  t( sqlite3BtreeH
1e396 6f 6c 64 73 4d 75 74 65 78 28 64 62 2d 3e 61 44  oldsMutex(db->aD
1e397 62 5b 69 44 62 5d 2e 70 42 74 29 20 29 3b 0a 0a  b[iDb].pBt) );..
1e398 20 20 2f 2a 20 43 6c 65 61 72 20 61 6e 79 20 70    /* Clear any p
1e399 72 69 6f 72 20 73 74 61 74 69 73 74 69 63 73 20  rior statistics 
1e39a 2a 2f 0a 20 20 66 6f 72 28 69 3d 73 71 6c 69 74  */.  for(i=sqlit
1e39b 65 48 61 73 68 46 69 72 73 74 28 26 64 62 2d 3e  eHashFirst(&db->
1e39c 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
1e39d 2d 3e 69 64 78 48 61 73 68 29 3b 69 3b 69 3d 73  ->idxHash);i;i=s
1e39e 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 69 29  qliteHashNext(i)
1e39f 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49  ){.    Index *pI
1e3a0 64 78 20 3d 20 73 71 6c 69 74 65 48 61 73 68 44  dx = sqliteHashD
1e3a1 61 74 61 28 69 29 3b 0a 20 20 20 20 73 71 6c 69  ata(i);.    sqli
1e3a2 74 65 33 44 65 66 61 75 6c 74 52 6f 77 45 73 74  te3DefaultRowEst
1e3a3 28 70 49 64 78 29 3b 0a 20 20 7d 0a 0a 20 20 2f  (pIdx);.  }..  /
1e3a4 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65 20  * Check to make 
1e3a5 73 75 72 65 20 74 68 65 20 73 71 6c 69 74 65 5f  sure the sqlite_
1e3a6 73 74 61 74 31 20 74 61 62 6c 65 20 65 78 69 73  stat1 table exis
1e3a7 74 73 73 20 2a 2f 0a 20 20 73 49 6e 66 6f 2e 64  tss */.  sInfo.d
1e3a8 62 20 3d 20 64 62 3b 0a 20 20 73 49 6e 66 6f 2e  b = db;.  sInfo.
1e3a9 7a 44 61 74 61 62 61 73 65 20 3d 20 64 62 2d 3e  zDatabase = db->
1e3aa 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a  aDb[iDb].zName;.
1e3ab 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
1e3ac 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69  dTable(db, "sqli
1e3ad 74 65 5f 73 74 61 74 31 22 2c 20 73 49 6e 66 6f  te_stat1", sInfo
1e3ae 2e 7a 44 61 74 61 62 61 73 65 29 3d 3d 30 20 29  .zDatabase)==0 )
1e3af 7b 0a 20 20 20 20 20 72 65 74 75 72 6e 20 53 51  {.     return SQ
1e3b0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
1e3b1 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 6e 65 77 20  ..  /* Load new 
1e3b2 73 74 61 74 69 73 74 69 63 73 20 6f 75 74 20 6f  statistics out o
1e3b3 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
1e3b4 74 31 20 74 61 62 6c 65 20 2a 2f 0a 20 20 7a 53  t1 table */.  zS
1e3b5 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  ql = sqlite3MPri
1e3b6 6e 74 66 28 64 62 2c 20 22 53 45 4c 45 43 54 20  ntf(db, "SELECT 
1e3b7 69 64 78 2c 20 73 74 61 74 20 46 52 4f 4d 20 25  idx, stat FROM %
1e3b8 51 2e 73 71 6c 69 74 65 5f 73 74 61 74 31 22 2c  Q.sqlite_stat1",
1e3b9 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1e3ba 20 20 20 20 20 20 20 20 20 73 49 6e 66 6f 2e 7a           sInfo.z
1e3bb 44 61 74 61 62 61 73 65 29 3b 0a 20 20 28 76 6f  Database);.  (vo
1e3bc 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79  id)sqlite3Safety
1e3bd 4f 66 66 28 64 62 29 3b 0a 20 20 72 63 20 3d 20  Off(db);.  rc = 
1e3be 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
1e3bf 20 7a 53 71 6c 2c 20 61 6e 61 6c 79 73 69 73 4c   zSql, analysisL
1e3c0 6f 61 64 65 72 2c 20 26 73 49 6e 66 6f 2c 20 30  oader, &sInfo, 0
1e3c1 29 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  );.  (void)sqlit
1e3c2 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
1e3c3 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1e3c4 64 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 72 65 74  db, zSql);.  ret
1e3c5 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 23 65 6e 64  urn rc;.}...#end
1e3c6 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49  if /* SQLITE_OMI
1e3c7 54 5f 41 4e 41 4c 59 5a 45 20 2a 2f 0a 0a 2f 2a  T_ANALYZE */../*
1e3c8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
1e3c9 64 20 6f 66 20 61 6e 61 6c 79 7a 65 2e 63 20 2a  d of analyze.c *
1e3ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e3cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e3cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1e3cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
1e3ce 67 69 6e 20 66 69 6c 65 20 61 74 74 61 63 68 2e  gin file attach.
1e3cf 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
1e3d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e3d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
1e3d2 0a 2a 2a 20 32 30 30 33 20 41 70 72 69 6c 20 36  .** 2003 April 6
1e3d3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
1e3d4 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
1e3d5 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
1e3d6 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
1e3d7 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
1e3d8 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
1e3d9 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
1e3da 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
1e3db 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
1e3dc 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
1e3dd 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
1e3de 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
1e3df 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
1e3e0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
1e3e1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
1e3e2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
1e3e3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
1e3e4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
1e3e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e3e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e3e7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e3e8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
1e3e9 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
1e3ea 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20  tains code used 
1e3eb 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65  to implement the
1e3ec 20 41 54 54 41 43 48 20 61 6e 64 20 44 45 54 41   ATTACH and DETA
1e3ed 43 48 20 63 6f 6d 6d 61 6e 64 73 2e 0a 2a 2a 0a  CH commands..**.
1e3ee 2a 2a 20 24 49 64 3a 20 61 74 74 61 63 68 2e 63  ** $Id: attach.c
1e3ef 2c 76 20 31 2e 38 30 20 32 30 30 38 2f 31 31 2f  ,v 1.80 2008/11/
1e3f0 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e 69  19 09:05:27 dani
1e3f1 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f  elk1977 Exp $.*/
1e3f2 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1e3f3 5f 4f 4d 49 54 5f 41 54 54 41 43 48 0a 2f 2a 0a  _OMIT_ATTACH./*.
1e3f4 2a 2a 20 52 65 73 6f 6c 76 65 20 61 6e 20 65 78  ** Resolve an ex
1e3f5 70 72 65 73 73 69 6f 6e 20 74 68 61 74 20 77 61  pression that wa
1e3f6 73 20 70 61 72 74 20 6f 66 20 61 6e 20 41 54 54  s part of an ATT
1e3f7 41 43 48 20 6f 72 20 44 45 54 41 43 48 20 73 74  ACH or DETACH st
1e3f8 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 0a 2a 2a  atement. This.**
1e3f9 20 69 73 20 73 6c 69 67 68 74 6c 79 20 64 69 66   is slightly dif
1e3fa 66 65 72 65 6e 74 20 66 72 6f 6d 20 72 65 73 6f  ferent from reso
1e3fb 6c 76 69 6e 67 20 61 20 6e 6f 72 6d 61 6c 20 53  lving a normal S
1e3fc 51 4c 20 65 78 70 72 65 73 73 69 6f 6e 2c 20 62  QL expression, b
1e3fd 65 63 61 75 73 65 20 73 69 6d 70 6c 65 0a 2a 2a  ecause simple.**
1e3fe 20 69 64 65 6e 74 69 66 69 65 72 73 20 61 72 65   identifiers are
1e3ff 20 74 72 65 61 74 65 64 20 61 73 20 73 74 72 69   treated as stri
1e400 6e 67 73 2c 20 6e 6f 74 20 70 6f 73 73 69 62 6c  ngs, not possibl
1e401 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 6f  e column names o
1e402 72 20 61 6c 69 61 73 65 73 2e 0a 2a 2a 0a 2a 2a  r aliases..**.**
1e403 20 69 2e 65 2e 20 69 66 20 74 68 65 20 70 61 72   i.e. if the par
1e404 73 65 72 20 73 65 65 73 3a 0a 2a 2a 0a 2a 2a 20  ser sees:.**.** 
1e405 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
1e406 41 53 45 20 61 62 63 20 41 53 20 64 65 66 0a 2a  ASE abc AS def.*
1e407 2a 0a 2a 2a 20 69 74 20 74 72 65 61 74 73 20 74  *.** it treats t
1e408 68 65 20 74 77 6f 20 65 78 70 72 65 73 73 69 6f  he two expressio
1e409 6e 73 20 61 73 20 6c 69 74 65 72 61 6c 20 73 74  ns as literal st
1e40a 72 69 6e 67 73 20 27 61 62 63 27 20 61 6e 64 20  rings 'abc' and 
1e40b 27 64 65 66 27 20 69 6e 73 74 65 61 64 20 6f 66  'def' instead of
1e40c 0a 2a 2a 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20  .** looking for 
1e40d 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 73  columns of the s
1e40e 61 6d 65 20 6e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20  ame name..**.** 
1e40f 54 68 69 73 20 6f 6e 6c 79 20 61 70 70 6c 69 65  This only applie
1e410 73 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f  s to the root no
1e411 64 65 20 6f 66 20 70 45 78 70 72 2c 20 73 6f 20  de of pExpr, so 
1e412 74 68 65 20 73 74 61 74 65 6d 65 6e 74 3a 0a 2a  the statement:.*
1e413 2a 0a 2a 2a 20 20 20 20 20 41 54 54 41 43 48 20  *.**     ATTACH 
1e414 44 41 54 41 42 41 53 45 20 61 62 63 7c 7c 64 65  DATABASE abc||de
1e415 66 20 41 53 20 27 64 62 32 27 0a 2a 2a 0a 2a 2a  f AS 'db2'.**.**
1e416 20 77 69 6c 6c 20 66 61 69 6c 20 62 65 63 61 75   will fail becau
1e417 73 65 20 6e 65 69 74 68 65 72 20 61 62 63 20 6f  se neither abc o
1e418 72 20 64 65 66 20 63 61 6e 20 62 65 20 72 65 73  r def can be res
1e419 6f 6c 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  olved..*/.static
1e41a 20 69 6e 74 20 72 65 73 6f 6c 76 65 41 74 74 61   int resolveAtta
1e41b 63 68 45 78 70 72 28 4e 61 6d 65 43 6f 6e 74 65  chExpr(NameConte
1e41c 78 74 20 2a 70 4e 61 6d 65 2c 20 45 78 70 72 20  xt *pName, Expr 
1e41d 2a 70 45 78 70 72 29 0a 7b 0a 20 20 69 6e 74 20  *pExpr).{.  int 
1e41e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
1e41f 20 20 69 66 28 20 70 45 78 70 72 20 29 7b 0a 20    if( pExpr ){. 
1e420 20 20 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70     if( pExpr->op
1e421 21 3d 54 4b 5f 49 44 20 29 7b 0a 20 20 20 20 20  !=TK_ID ){.     
1e422 20 72 63 20 3d 20 73 71 6c 69 74 65 33 52 65 73   rc = sqlite3Res
1e423 6f 6c 76 65 45 78 70 72 4e 61 6d 65 73 28 70 4e  olveExprNames(pN
1e424 61 6d 65 2c 20 70 45 78 70 72 29 3b 0a 20 20 20  ame, pExpr);.   
1e425 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
1e426 45 5f 4f 4b 20 26 26 20 21 73 71 6c 69 74 65 33  E_OK && !sqlite3
1e427 45 78 70 72 49 73 43 6f 6e 73 74 61 6e 74 28 70  ExprIsConstant(p
1e428 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20  Expr) ){.       
1e429 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1e42a 28 70 4e 61 6d 65 2d 3e 70 50 61 72 73 65 2c 20  (pName->pParse, 
1e42b 22 69 6e 76 61 6c 69 64 20 6e 61 6d 65 3a 20 5c  "invalid name: \
1e42c 22 25 54 5c 22 22 2c 20 26 70 45 78 70 72 2d 3e  "%T\"", &pExpr->
1e42d 73 70 61 6e 29 3b 0a 20 20 20 20 20 20 20 20 72  span);.        r
1e42e 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
1e42f 4f 52 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  OR;.      }.    
1e430 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78  }else{.      pEx
1e431 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 53 54 52 49  pr->op = TK_STRI
1e432 4e 47 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  NG;.    }.  }.  
1e433 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1e434 0a 2a 2a 20 41 6e 20 53 51 4c 20 75 73 65 72 2d  .** An SQL user-
1e435 66 75 6e 63 74 69 6f 6e 20 72 65 67 69 73 74 65  function registe
1e436 72 65 64 20 74 6f 20 64 6f 20 74 68 65 20 77 6f  red to do the wo
1e437 72 6b 20 6f 66 20 61 6e 20 41 54 54 41 43 48 20  rk of an ATTACH 
1e438 73 74 61 74 65 6d 65 6e 74 2e 20 54 68 65 0a 2a  statement. The.*
1e439 2a 20 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74  * three argument
1e43a 73 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f  s to the functio
1e43b 6e 20 63 6f 6d 65 20 64 69 72 65 63 74 6c 79 20  n come directly 
1e43c 66 72 6f 6d 20 61 6e 20 61 74 74 61 63 68 20 73  from an attach s
1e43d 74 61 74 65 6d 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20  tatement:.**.** 
1e43e 20 20 20 20 41 54 54 41 43 48 20 44 41 54 41 42      ATTACH DATAB
1e43f 41 53 45 20 78 20 41 53 20 79 20 4b 45 59 20 7a  ASE x AS y KEY z
1e440 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
1e441 54 20 73 71 6c 69 74 65 5f 61 74 74 61 63 68 28  T sqlite_attach(
1e442 78 2c 20 79 2c 20 7a 29 0a 2a 2a 0a 2a 2a 20 49  x, y, z).**.** I
1e443 66 20 74 68 65 20 6f 70 74 69 6f 6e 61 6c 20 22  f the optional "
1e444 4b 45 59 20 7a 22 20 73 79 6e 74 61 78 20 69 73  KEY z" syntax is
1e445 20 6f 6d 69 74 74 65 64 2c 20 61 6e 20 53 51 4c   omitted, an SQL
1e446 20 4e 55 4c 4c 20 69 73 20 70 61 73 73 65 64 20   NULL is passed 
1e447 61 73 20 74 68 65 0a 2a 2a 20 74 68 69 72 64 20  as the.** third 
1e448 61 72 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 61  argument..*/.sta
1e449 74 69 63 20 76 6f 69 64 20 61 74 74 61 63 68 46  tic void attachF
1e44a 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
1e44b 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
1e44c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 0a  .  int NotUsed,.
1e44d 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
1e44e 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20  **argv.){.  int 
1e44f 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 30 3b  i;.  int rc = 0;
1e450 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1e451 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
1e452 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
1e453 78 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  xt);.  const cha
1e454 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 63 6f 6e 73  r *zName;.  cons
1e455 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 3b 0a 20  t char *zFile;. 
1e456 20 44 62 20 2a 61 4e 65 77 3b 0a 20 20 63 68 61   Db *aNew;.  cha
1e457 72 20 2a 7a 45 72 72 44 79 6e 20 3d 20 30 3b 0a  r *zErrDyn = 0;.
1e458 20 20 63 68 61 72 20 7a 45 72 72 5b 31 32 38 5d    char zErr[128]
1e459 3b 0a 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41  ;..  UNUSED_PARA
1e45a 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a  METER(NotUsed);.
1e45b 0a 20 20 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73  .  zFile = (cons
1e45c 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
1e45d 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
1e45e 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d 20  [0]);.  zName = 
1e45f 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
1e460 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
1e461 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28  (argv[1]);.  if(
1e462 20 7a 46 69 6c 65 3d 3d 30 20 29 20 7a 46 69 6c   zFile==0 ) zFil
1e463 65 20 3d 20 22 22 3b 0a 20 20 69 66 28 20 7a 4e  e = "";.  if( zN
1e464 61 6d 65 3d 3d 30 20 29 20 7a 4e 61 6d 65 20 3d  ame==0 ) zName =
1e465 20 22 22 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b   "";..  /* Check
1e466 20 66 6f 72 20 74 68 65 20 66 6f 6c 6c 6f 77 69   for the followi
1e467 6e 67 20 65 72 72 6f 72 73 3a 0a 20 20 2a 2a 0a  ng errors:.  **.
1e468 20 20 2a 2a 20 20 20 20 20 2a 20 54 6f 6f 20 6d    **     * Too m
1e469 61 6e 79 20 61 74 74 61 63 68 65 64 20 64 61 74  any attached dat
1e46a 61 62 61 73 65 73 2c 0a 20 20 2a 2a 20 20 20 20  abases,.  **    
1e46b 20 2a 20 54 72 61 6e 73 61 63 74 69 6f 6e 20 63   * Transaction c
1e46c 75 72 72 65 6e 74 6c 79 20 6f 70 65 6e 0a 20 20  urrently open.  
1e46d 2a 2a 20 20 20 20 20 2a 20 53 70 65 63 69 66 69  **     * Specifi
1e46e 65 64 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65  ed database name
1e46f 20 61 6c 72 65 61 64 79 20 62 65 69 6e 67 20 75   already being u
1e470 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  sed..  */.  if( 
1e471 64 62 2d 3e 6e 44 62 3e 3d 64 62 2d 3e 61 4c 69  db->nDb>=db->aLi
1e472 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54  mit[SQLITE_LIMIT
1e473 5f 41 54 54 41 43 48 45 44 5d 2b 32 20 29 7b 0a  _ATTACHED]+2 ){.
1e474 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1e475 69 6e 74 66 28 0a 20 20 20 20 20 20 73 69 7a 65  intf(.      size
1e476 6f 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20  of(zErr), zErr, 
1e477 22 74 6f 6f 20 6d 61 6e 79 20 61 74 74 61 63 68  "too many attach
1e478 65 64 20 64 61 74 61 62 61 73 65 73 20 2d 20 6d  ed databases - m
1e479 61 78 20 25 64 22 2c 20 0a 20 20 20 20 20 20 64  ax %d", .      d
1e47a 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45  b->aLimit[SQLITE
1e47b 5f 4c 49 4d 49 54 5f 41 54 54 41 43 48 45 44 5d  _LIMIT_ATTACHED]
1e47c 0a 20 20 20 20 29 3b 0a 20 20 20 20 67 6f 74 6f  .    );.    goto
1e47d 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1e47e 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75   }.  if( !db->au
1e47f 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20  toCommit ){.    
1e480 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1e481 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1e482 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Err,.           
1e483 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
1e484 74 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  t ATTACH databas
1e485 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
1e486 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
1e487 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1e488 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c   }.  for(i=0; i<
1e489 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
1e48a 20 20 20 63 68 61 72 20 2a 7a 20 3d 20 64 62 2d     char *z = db-
1e48b 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20  >aDb[i].zName;. 
1e48c 20 20 20 69 66 28 20 7a 20 26 26 20 7a 4e 61 6d     if( z && zNam
1e48d 65 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 49  e && sqlite3StrI
1e48e 43 6d 70 28 7a 2c 20 7a 4e 61 6d 65 29 3d 3d 30  Cmp(z, zName)==0
1e48f 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1e490 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
1e491 66 28 7a 45 72 72 29 2c 20 7a 45 72 72 2c 20 0a  f(zErr), zErr, .
1e492 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e493 20 20 20 20 20 20 20 22 64 61 74 61 62 61 73 65         "database
1e494 20 25 73 20 69 73 20 61 6c 72 65 61 64 79 20 69   %s is already i
1e495 6e 20 75 73 65 22 2c 20 7a 4e 61 6d 65 29 3b 0a  n use", zName);.
1e496 20 20 20 20 20 20 67 6f 74 6f 20 61 74 74 61 63        goto attac
1e497 68 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20  h_error;.    }. 
1e498 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74   }..  /* Allocat
1e499 65 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20  e the new entry 
1e49a 69 6e 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d  in the db->aDb[]
1e49b 20 61 72 72 61 79 20 61 6e 64 20 69 6e 69 74 69   array and initi
1e49c 61 6c 69 73 65 20 74 68 65 20 73 63 68 65 6d 61  alise the schema
1e49d 0a 20 20 2a 2a 20 68 61 73 68 20 74 61 62 6c 65  .  ** hash table
1e49e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  s..  */.  if( db
1e49f 2d 3e 61 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74  ->aDb==db->aDbSt
1e4a0 61 74 69 63 20 29 7b 0a 20 20 20 20 61 4e 65 77  atic ){.    aNew
1e4a1 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c   = sqlite3DbMall
1e4a2 6f 63 52 61 77 28 64 62 2c 20 73 69 7a 65 6f 66  ocRaw(db, sizeof
1e4a3 28 64 62 2d 3e 61 44 62 5b 30 5d 29 2a 33 20 29  (db->aDb[0])*3 )
1e4a4 3b 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d  ;.    if( aNew==
1e4a5 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
1e4a6 6d 65 6d 63 70 79 28 61 4e 65 77 2c 20 64 62 2d  memcpy(aNew, db-
1e4a7 3e 61 44 62 2c 20 73 69 7a 65 6f 66 28 64 62 2d  >aDb, sizeof(db-
1e4a8 3e 61 44 62 5b 30 5d 29 2a 32 29 3b 0a 20 20 7d  >aDb[0])*2);.  }
1e4a9 65 6c 73 65 7b 0a 20 20 20 20 61 4e 65 77 20 3d  else{.    aNew =
1e4aa 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
1e4ab 63 28 64 62 2c 20 64 62 2d 3e 61 44 62 2c 20 73  c(db, db->aDb, s
1e4ac 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62 5b 30 5d  izeof(db->aDb[0]
1e4ad 29 2a 28 64 62 2d 3e 6e 44 62 2b 31 29 20 29 3b  )*(db->nDb+1) );
1e4ae 0a 20 20 20 20 69 66 28 20 61 4e 65 77 3d 3d 30  .    if( aNew==0
1e4af 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20   ) return;.  }. 
1e4b0 20 64 62 2d 3e 61 44 62 20 3d 20 61 4e 65 77 3b   db->aDb = aNew;
1e4b1 0a 20 20 61 4e 65 77 20 3d 20 26 64 62 2d 3e 61  .  aNew = &db->a
1e4b2 44 62 5b 64 62 2d 3e 6e 44 62 2b 2b 5d 3b 0a 20  Db[db->nDb++];. 
1e4b3 20 6d 65 6d 73 65 74 28 61 4e 65 77 2c 20 30 2c   memset(aNew, 0,
1e4b4 20 73 69 7a 65 6f 66 28 2a 61 4e 65 77 29 29 3b   sizeof(*aNew));
1e4b5 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 74 68 65 20  ..  /* Open the 
1e4b6 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 49  database file. I
1e4b7 66 20 74 68 65 20 62 74 72 65 65 20 69 73 20 73  f the btree is s
1e4b8 75 63 63 65 73 73 66 75 6c 6c 79 20 6f 70 65 6e  uccessfully open
1e4b9 65 64 2c 20 75 73 65 0a 20 20 2a 2a 20 69 74 20  ed, use.  ** it 
1e4ba 74 6f 20 6f 62 74 61 69 6e 20 74 68 65 20 64 61  to obtain the da
1e4bb 74 61 62 61 73 65 20 73 63 68 65 6d 61 2e 20 41  tabase schema. A
1e4bc 74 20 74 68 69 73 20 70 6f 69 6e 74 20 74 68 65  t this point the
1e4bd 20 73 63 68 65 6d 61 20 6d 61 79 0a 20 20 2a 2a   schema may.  **
1e4be 20 6f 72 20 6d 61 79 20 6e 6f 74 20 62 65 20 69   or may not be i
1e4bf 6e 69 74 69 61 6c 69 73 65 64 2e 0a 20 20 2a 2f  nitialised..  */
1e4c0 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42  .  rc = sqlite3B
1e4c1 74 72 65 65 46 61 63 74 6f 72 79 28 64 62 2c 20  treeFactory(db, 
1e4c2 7a 46 69 6c 65 2c 20 30 2c 20 53 51 4c 49 54 45  zFile, 0, SQLITE
1e4c3 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45 5f 53  _DEFAULT_CACHE_S
1e4c4 49 5a 45 2c 0a 20 20 20 20 20 20 20 20 20 20 20  IZE,.           
1e4c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4c6 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73 20 7c 20  db->openFlags | 
1e4c7 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
1e4c8 5f 44 42 2c 0a 20 20 20 20 20 20 20 20 20 20 20  _DB,.           
1e4c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e4ca 26 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 69  &aNew->pBt);.  i
1e4cb 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
1e4cc 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70   ){.    Pager *p
1e4cd 50 61 67 65 72 3b 0a 20 20 20 20 61 4e 65 77 2d  Pager;.    aNew-
1e4ce 3e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74  >pSchema = sqlit
1e4cf 65 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20  e3SchemaGet(db, 
1e4d0 61 4e 65 77 2d 3e 70 42 74 29 3b 0a 20 20 20 20  aNew->pBt);.    
1e4d1 69 66 28 20 21 61 4e 65 77 2d 3e 70 53 63 68 65  if( !aNew->pSche
1e4d2 6d 61 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  ma ){.      rc =
1e4d3 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
1e4d4 20 20 20 7d 65 6c 73 65 20 69 66 28 20 61 4e 65     }else if( aNe
1e4d5 77 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65  w->pSchema->file
1e4d6 5f 66 6f 72 6d 61 74 20 26 26 20 61 4e 65 77 2d  _format && aNew-
1e4d7 3e 70 53 63 68 65 6d 61 2d 3e 65 6e 63 21 3d 45  >pSchema->enc!=E
1e4d8 4e 43 28 64 62 29 20 29 7b 0a 20 20 20 20 20 20  NC(db) ){.      
1e4d9 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1e4da 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1e4db 45 72 72 2c 20 0a 20 20 20 20 20 20 20 20 22 61  Err, .        "a
1e4dc 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65  ttached database
1e4dd 73 20 6d 75 73 74 20 75 73 65 20 74 68 65 20 73  s must use the s
1e4de 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ame text encodin
1e4df 67 20 61 73 20 6d 61 69 6e 20 64 61 74 61 62 61  g as main databa
1e4e0 73 65 22 29 3b 0a 20 20 20 20 20 20 67 6f 74 6f  se");.      goto
1e4e1 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1e4e2 20 20 20 7d 0a 20 20 20 20 70 50 61 67 65 72 20     }.    pPager 
1e4e3 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 50 61  = sqlite3BtreePa
1e4e4 67 65 72 28 61 4e 65 77 2d 3e 70 42 74 29 3b 0a  ger(aNew->pBt);.
1e4e5 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
1e4e6 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67  LockingMode(pPag
1e4e7 65 72 2c 20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b  er, db->dfltLock
1e4e8 4d 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74  Mode);.    sqlit
1e4e9 65 33 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f  e3PagerJournalMo
1e4ea 64 65 28 70 50 61 67 65 72 2c 20 64 62 2d 3e 64  de(pPager, db->d
1e4eb 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 29 3b  fltJournalMode);
1e4ec 0a 20 20 7d 0a 20 20 61 4e 65 77 2d 3e 7a 4e 61  .  }.  aNew->zNa
1e4ed 6d 65 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74  me = sqlite3DbSt
1e4ee 72 44 75 70 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  rDup(db, zName);
1e4ef 0a 20 20 61 4e 65 77 2d 3e 73 61 66 65 74 79 5f  .  aNew->safety_
1e4f0 6c 65 76 65 6c 20 3d 20 33 3b 0a 0a 23 69 66 20  level = 3;..#if 
1e4f1 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45 43  SQLITE_HAS_CODEC
1e4f2 0a 20 20 7b 0a 20 20 20 20 65 78 74 65 72 6e 20  .  {.    extern 
1e4f3 69 6e 74 20 73 71 6c 69 74 65 33 43 6f 64 65 63  int sqlite3Codec
1e4f4 41 74 74 61 63 68 28 73 71 6c 69 74 65 33 2a 2c  Attach(sqlite3*,
1e4f5 20 69 6e 74 2c 20 63 6f 6e 73 74 20 76 6f 69 64   int, const void
1e4f6 2a 2c 20 69 6e 74 29 3b 0a 20 20 20 20 65 78 74  *, int);.    ext
1e4f7 65 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ern void sqlite3
1e4f8 43 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69  CodecGetKey(sqli
1e4f9 74 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a  te3*, int, void*
1e4fa 2a 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e  *, int*);.    in
1e4fb 74 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72  t nKey;.    char
1e4fc 20 2a 7a 4b 65 79 3b 0a 20 20 20 20 69 6e 74 20   *zKey;.    int 
1e4fd 74 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  t = sqlite3_valu
1e4fe 65 5f 74 79 70 65 28 61 72 67 76 5b 32 5d 29 3b  e_type(argv[2]);
1e4ff 0a 20 20 20 20 73 77 69 74 63 68 28 20 74 20 29  .    switch( t )
1e500 7b 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c  {.      case SQL
1e501 49 54 45 5f 49 4e 54 45 47 45 52 3a 0a 20 20 20  ITE_INTEGER:.   
1e502 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 46     case SQLITE_F
1e503 4c 4f 41 54 3a 0a 20 20 20 20 20 20 20 20 7a 45  LOAT:.        zE
1e504 72 72 44 79 6e 20 3d 20 73 71 6c 69 74 65 33 44  rrDyn = sqlite3D
1e505 62 53 74 72 44 75 70 28 64 62 2c 20 22 49 6e 76  bStrDup(db, "Inv
1e506 61 6c 69 64 20 6b 65 79 20 76 61 6c 75 65 22 29  alid key value")
1e507 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ;.        rc = S
1e508 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
1e509 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
1e50a 20 20 20 20 0a 20 20 20 20 20 20 63 61 73 65 20      .      case 
1e50b 53 51 4c 49 54 45 5f 54 45 58 54 3a 0a 20 20 20  SQLITE_TEXT:.   
1e50c 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 42     case SQLITE_B
1e50d 4c 4f 42 3a 0a 20 20 20 20 20 20 20 20 6e 4b 65  LOB:.        nKe
1e50e 79 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  y = sqlite3_valu
1e50f 65 5f 62 79 74 65 73 28 61 72 67 76 5b 32 5d 29  e_bytes(argv[2])
1e510 3b 0a 20 20 20 20 20 20 20 20 7a 4b 65 79 20 3d  ;.        zKey =
1e511 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33   (char *)sqlite3
1e512 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67 76  _value_blob(argv
1e513 5b 32 5d 29 3b 0a 20 20 20 20 20 20 20 20 73 71  [2]);.        sq
1e514 6c 69 74 65 33 43 6f 64 65 63 41 74 74 61 63 68  lite3CodecAttach
1e515 28 64 62 2c 20 64 62 2d 3e 6e 44 62 2d 31 2c 20  (db, db->nDb-1, 
1e516 7a 4b 65 79 2c 20 6e 4b 65 79 29 3b 0a 20 20 20  zKey, nKey);.   
1e517 20 20 20 20 20 62 72 65 61 6b 3b 0a 0a 20 20 20       break;..   
1e518 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 4e     case SQLITE_N
1e519 55 4c 4c 3a 0a 20 20 20 20 20 20 20 20 2f 2a 20  ULL:.        /* 
1e51a 4e 6f 20 6b 65 79 20 73 70 65 63 69 66 69 65 64  No key specified
1e51b 2e 20 20 55 73 65 20 74 68 65 20 6b 65 79 20 66  .  Use the key f
1e51c 72 6f 6d 20 74 68 65 20 6d 61 69 6e 20 64 61 74  rom the main dat
1e51d 61 62 61 73 65 20 2a 2f 0a 20 20 20 20 20 20 20  abase */.       
1e51e 20 73 71 6c 69 74 65 33 43 6f 64 65 63 47 65 74   sqlite3CodecGet
1e51f 4b 65 79 28 64 62 2c 20 30 2c 20 28 76 6f 69 64  Key(db, 0, (void
1e520 2a 2a 29 26 7a 4b 65 79 2c 20 26 6e 4b 65 79 29  **)&zKey, &nKey)
1e521 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
1e522 33 43 6f 64 65 63 41 74 74 61 63 68 28 64 62 2c  3CodecAttach(db,
1e523 20 64 62 2d 3e 6e 44 62 2d 31 2c 20 7a 4b 65 79   db->nDb-1, zKey
1e524 2c 20 6e 4b 65 79 29 3b 0a 20 20 20 20 20 20 20  , nKey);.       
1e525 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1e526 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 49  }.#endif..  /* I
1e527 66 20 74 68 65 20 66 69 6c 65 20 77 61 73 20 6f  f the file was o
1e528 70 65 6e 65 64 20 73 75 63 63 65 73 73 66 75 6c  pened successful
1e529 6c 79 2c 20 72 65 61 64 20 74 68 65 20 73 63 68  ly, read the sch
1e52a 65 6d 61 20 66 6f 72 20 74 68 65 20 6e 65 77 20  ema for the new 
1e52b 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49  database..  ** I
1e52c 66 20 74 68 69 73 20 66 61 69 6c 73 2c 20 6f 72  f this fails, or
1e52d 20 69 66 20 6f 70 65 6e 69 6e 67 20 74 68 65 20   if opening the 
1e52e 66 69 6c 65 20 66 61 69 6c 65 64 2c 20 74 68 65  file failed, the
1e52f 6e 20 63 6c 6f 73 65 20 74 68 65 20 66 69 6c 65  n close the file
1e530 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65 6d 6f 76   and .  ** remov
1e531 65 20 74 68 65 20 65 6e 74 72 79 20 66 72 6f 6d  e the entry from
1e532 20 74 68 65 20 64 62 2d 3e 61 44 62 5b 5d 20 61   the db->aDb[] a
1e533 72 72 61 79 2e 20 69 2e 65 2e 20 70 75 74 20 65  rray. i.e. put e
1e534 76 65 72 79 74 68 69 6e 67 20 62 61 63 6b 20 74  verything back t
1e535 68 65 20 77 61 79 0a 20 20 2a 2a 20 77 65 20 66  he way.  ** we f
1e536 6f 75 6e 64 20 69 74 2e 0a 20 20 2a 2f 0a 20 20  ound it..  */.  
1e537 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
1e538 4b 20 29 7b 0a 20 20 20 20 28 76 6f 69 64 29 73  K ){.    (void)s
1e539 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e 28 64  qlite3SafetyOn(d
1e53a 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  b);.    sqlite3B
1e53b 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
1e53c 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
1e53d 65 33 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72  e3Init(db, &zErr
1e53e 44 79 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  Dyn);.    sqlite
1e53f 33 42 74 72 65 65 4c 65 61 76 65 41 6c 6c 28 64  3BtreeLeaveAll(d
1e540 62 29 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71  b);.    (void)sq
1e541 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28 64  lite3SafetyOff(d
1e542 62 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63  b);.  }.  if( rc
1e543 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 44 62 20   ){.    int iDb 
1e544 3d 20 64 62 2d 3e 6e 44 62 20 2d 20 31 3b 0a 20  = db->nDb - 1;. 
1e545 20 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d     assert( iDb>=
1e546 32 20 29 3b 0a 20 20 20 20 69 66 28 20 64 62 2d  2 );.    if( db-
1e547 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 20 29 7b  >aDb[iDb].pBt ){
1e548 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
1e549 72 65 65 43 6c 6f 73 65 28 64 62 2d 3e 61 44 62  reeClose(db->aDb
1e54a 5b 69 44 62 5d 2e 70 42 74 29 3b 0a 20 20 20 20  [iDb].pBt);.    
1e54b 20 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70    db->aDb[iDb].p
1e54c 42 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 64 62  Bt = 0;.      db
1e54d 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
1e54e 6d 61 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  ma = 0;.    }.  
1e54f 20 20 73 71 6c 69 74 65 33 52 65 73 65 74 49 6e    sqlite3ResetIn
1e550 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c  ternalSchema(db,
1e551 20 30 29 3b 0a 20 20 20 20 64 62 2d 3e 6e 44 62   0);.    db->nDb
1e552 20 3d 20 69 44 62 3b 0a 20 20 20 20 69 66 28 20   = iDb;.    if( 
1e553 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  rc==SQLITE_NOMEM
1e554 20 7c 7c 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49   || rc==SQLITE_I
1e555 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  OERR_NOMEM ){.  
1e556 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1e557 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
1e558 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1e559 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45  (sizeof(zErr),zE
1e55a 72 72 2c 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f  rr, "out of memo
1e55b 72 79 22 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ry");.    }else{
1e55c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
1e55d 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a  nprintf(sizeof(z
1e55e 45 72 72 29 2c 7a 45 72 72 2c 20 22 75 6e 61 62  Err),zErr, "unab
1e55f 6c 65 20 74 6f 20 6f 70 65 6e 20 64 61 74 61 62  le to open datab
1e560 61 73 65 3a 20 25 73 22 2c 20 7a 46 69 6c 65 29  ase: %s", zFile)
1e561 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74 6f  ;.    }.    goto
1e562 20 61 74 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   attach_error;. 
1e563 20 7d 0a 20 20 0a 20 20 72 65 74 75 72 6e 3b 0a   }.  .  return;.
1e564 0a 61 74 74 61 63 68 5f 65 72 72 6f 72 3a 0a 20  .attach_error:. 
1e565 20 2f 2a 20 52 65 74 75 72 6e 20 61 6e 20 65 72   /* Return an er
1e566 72 6f 72 20 69 66 20 77 65 20 67 65 74 20 68 65  ror if we get he
1e567 72 65 20 2a 2f 0a 20 20 69 66 28 20 7a 45 72 72  re */.  if( zErr
1e568 44 79 6e 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  Dyn ){.    sqlit
1e569 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
1e56a 63 6f 6e 74 65 78 74 2c 20 7a 45 72 72 44 79 6e  context, zErrDyn
1e56b 2c 20 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  , -1);.    sqlit
1e56c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
1e56d 72 44 79 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  rDyn);.  }else{.
1e56e 20 20 20 20 7a 45 72 72 5b 73 69 7a 65 6f 66 28      zErr[sizeof(
1e56f 7a 45 72 72 29 2d 31 5d 20 3d 20 30 3b 0a 20 20  zErr)-1] = 0;.  
1e570 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
1e571 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
1e572 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 0a 20  zErr, -1);.  }. 
1e573 20 69 66 28 20 72 63 20 29 20 73 71 6c 69 74 65   if( rc ) sqlite
1e574 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
1e575 6f 64 65 28 63 6f 6e 74 65 78 74 2c 20 72 63 29  ode(context, rc)
1e576 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 53 51  ;.}../*.** An SQ
1e577 4c 20 75 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20  L user-function 
1e578 72 65 67 69 73 74 65 72 65 64 20 74 6f 20 64 6f  registered to do
1e579 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 61 6e 20   the work of an 
1e57a 44 45 54 41 43 48 20 73 74 61 74 65 6d 65 6e 74  DETACH statement
1e57b 2e 20 54 68 65 0a 2a 2a 20 74 68 72 65 65 20 61  . The.** three a
1e57c 72 67 75 6d 65 6e 74 73 20 74 6f 20 74 68 65 20  rguments to the 
1e57d 66 75 6e 63 74 69 6f 6e 20 63 6f 6d 65 20 64 69  function come di
1e57e 72 65 63 74 6c 79 20 66 72 6f 6d 20 61 20 64 65  rectly from a de
1e57f 74 61 63 68 20 73 74 61 74 65 6d 65 6e 74 3a 0a  tach statement:.
1e580 2a 2a 0a 2a 2a 20 20 20 20 20 44 45 54 41 43 48  **.**     DETACH
1e581 20 44 41 54 41 42 41 53 45 20 78 0a 2a 2a 0a 2a   DATABASE x.**.*
1e582 2a 20 20 20 20 20 53 45 4c 45 43 54 20 73 71 6c  *     SELECT sql
1e583 69 74 65 5f 64 65 74 61 63 68 28 78 29 0a 2a 2f  ite_detach(x).*/
1e584 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 65 74  .static void det
1e585 61 63 68 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  achFunc(.  sqlit
1e586 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
1e587 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
1e588 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
1e589 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
1e58a 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1e58b 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
1e58c 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
1e58d 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
1e58e 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 73   sqlite3 *db = s
1e58f 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
1e590 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74  b_handle(context
1e591 29 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 44 62  );.  int i;.  Db
1e592 20 2a 70 44 62 20 3d 20 30 3b 0a 20 20 63 68 61   *pDb = 0;.  cha
1e593 72 20 7a 45 72 72 5b 31 32 38 5d 3b 0a 0a 20 20  r zErr[128];..  
1e594 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
1e595 28 4e 6f 74 55 73 65 64 29 3b 0a 0a 20 20 69 66  (NotUsed);..  if
1e596 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 7a 4e 61  ( zName==0 ) zNa
1e597 6d 65 20 3d 20 22 22 3b 0a 20 20 66 6f 72 28 69  me = "";.  for(i
1e598 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69  =0; i<db->nDb; i
1e599 2b 2b 29 7b 0a 20 20 20 20 70 44 62 20 3d 20 26  ++){.    pDb = &
1e59a 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20 20  db->aDb[i];.    
1e59b 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30 20  if( pDb->pBt==0 
1e59c 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
1e59d 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
1e59e 6d 70 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  mp(pDb->zName, z
1e59f 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  Name)==0 ) break
1e5a0 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 3e 3d  ;.  }..  if( i>=
1e5a1 64 62 2d 3e 6e 44 62 20 29 7b 0a 20 20 20 20 73  db->nDb ){.    s
1e5a2 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1e5a3 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72  sizeof(zErr),zEr
1e5a4 72 2c 20 22 6e 6f 20 73 75 63 68 20 64 61 74 61  r, "no such data
1e5a5 62 61 73 65 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  base: %s", zName
1e5a6 29 3b 0a 20 20 20 20 67 6f 74 6f 20 64 65 74 61  );.    goto deta
1e5a7 63 68 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20 20  ch_error;.  }.  
1e5a8 69 66 28 20 69 3c 32 20 29 7b 0a 20 20 20 20 73  if( i<2 ){.    s
1e5a9 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28  qlite3_snprintf(
1e5aa 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 7a 45 72  sizeof(zErr),zEr
1e5ab 72 2c 20 22 63 61 6e 6e 6f 74 20 64 65 74 61 63  r, "cannot detac
1e5ac 68 20 64 61 74 61 62 61 73 65 20 25 73 22 2c 20  h database %s", 
1e5ad 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
1e5ae 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   detach_error;. 
1e5af 20 7d 0a 20 20 69 66 28 20 21 64 62 2d 3e 61 75   }.  if( !db->au
1e5b0 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20 20 20 20  toCommit ){.    
1e5b1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
1e5b2 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2c 20 7a  (sizeof(zErr), z
1e5b3 45 72 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Err,.           
1e5b4 20 20 20 20 20 20 20 20 20 20 22 63 61 6e 6e 6f            "canno
1e5b5 74 20 44 45 54 41 43 48 20 64 61 74 61 62 61 73  t DETACH databas
1e5b6 65 20 77 69 74 68 69 6e 20 74 72 61 6e 73 61 63  e within transac
1e5b7 74 69 6f 6e 22 29 3b 0a 20 20 20 20 67 6f 74 6f  tion");.    goto
1e5b8 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a 20   detach_error;. 
1e5b9 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33   }.  if( sqlite3
1e5ba 42 74 72 65 65 49 73 49 6e 52 65 61 64 54 72 61  BtreeIsInReadTra
1e5bb 6e 73 28 70 44 62 2d 3e 70 42 74 29 20 29 7b 0a  ns(pDb->pBt) ){.
1e5bc 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72      sqlite3_snpr
1e5bd 69 6e 74 66 28 73 69 7a 65 6f 66 28 7a 45 72 72  intf(sizeof(zErr
1e5be 29 2c 7a 45 72 72 2c 20 22 64 61 74 61 62 61 73  ),zErr, "databas
1e5bf 65 20 25 73 20 69 73 20 6c 6f 63 6b 65 64 22 2c  e %s is locked",
1e5c0 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74   zName);.    got
1e5c1 6f 20 64 65 74 61 63 68 5f 65 72 72 6f 72 3b 0a  o detach_error;.
1e5c2 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 42 74    }..  sqlite3Bt
1e5c3 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42  reeClose(pDb->pB
1e5c4 74 29 3b 0a 20 20 70 44 62 2d 3e 70 42 74 20 3d  t);.  pDb->pBt =
1e5c5 20 30 3b 0a 20 20 70 44 62 2d 3e 70 53 63 68 65   0;.  pDb->pSche
1e5c6 6d 61 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65  ma = 0;.  sqlite
1e5c7 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
1e5c8 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20 72  hema(db, 0);.  r
1e5c9 65 74 75 72 6e 3b 0a 0a 64 65 74 61 63 68 5f 65  eturn;..detach_e
1e5ca 72 72 6f 72 3a 0a 20 20 73 71 6c 69 74 65 33 5f  rror:.  sqlite3_
1e5cb 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
1e5cc 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b  text, zErr, -1);
1e5cd 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 70  .}../*.** This p
1e5ce 72 6f 63 65 64 75 72 65 20 67 65 6e 65 72 61 74  rocedure generat
1e5cf 65 73 20 56 44 42 45 20 63 6f 64 65 20 66 6f 72  es VDBE code for
1e5d0 20 61 20 73 69 6e 67 6c 65 20 69 6e 76 6f 63 61   a single invoca
1e5d1 74 69 6f 6e 20 6f 66 20 65 69 74 68 65 72 20 74  tion of either t
1e5d2 68 65 0a 2a 2a 20 73 71 6c 69 74 65 5f 64 65 74  he.** sqlite_det
1e5d3 61 63 68 28 29 20 6f 72 20 73 71 6c 69 74 65 5f  ach() or sqlite_
1e5d4 61 74 74 61 63 68 28 29 20 53 51 4c 20 75 73 65  attach() SQL use
1e5d5 72 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  r functions..*/.
1e5d6 73 74 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65  static void code
1e5d7 41 74 74 61 63 68 28 0a 20 20 50 61 72 73 65 20  Attach(.  Parse 
1e5d8 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 2f  *pParse,       /
1e5d9 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
1e5da 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 74 79  text */.  int ty
1e5db 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  pe,            /
1e5dc 2a 20 45 69 74 68 65 72 20 53 51 4c 49 54 45 5f  * Either SQLITE_
1e5dd 41 54 54 41 43 48 20 6f 72 20 53 51 4c 49 54 45  ATTACH or SQLITE
1e5de 5f 44 45 54 41 43 48 20 2a 2f 0a 20 20 46 75 6e  _DETACH */.  Fun
1e5df 63 44 65 66 20 2a 70 46 75 6e 63 2c 20 20 20 20  cDef *pFunc,    
1e5e0 20 20 2f 2a 20 46 75 6e 63 44 65 66 20 77 72 61    /* FuncDef wra
1e5e1 70 70 65 72 20 66 6f 72 20 64 65 74 61 63 68 46  pper for detachF
1e5e2 75 6e 63 28 29 20 6f 72 20 61 74 74 61 63 68 46  unc() or attachF
1e5e3 75 6e 63 28 29 20 2a 2f 0a 20 20 45 78 70 72 20  unc() */.  Expr 
1e5e4 2a 70 41 75 74 68 41 72 67 2c 20 20 20 20 20 20  *pAuthArg,      
1e5e5 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 74 6f  /* Expression to
1e5e6 20 70 61 73 73 20 74 6f 20 61 75 74 68 6f 72 69   pass to authori
1e5e7 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b 20  zation callback 
1e5e8 2a 2f 0a 20 20 45 78 70 72 20 2a 70 46 69 6c 65  */.  Expr *pFile
1e5e9 6e 61 6d 65 2c 20 20 20 20 20 2f 2a 20 4e 61 6d  name,     /* Nam
1e5ea 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 66 69  e of database fi
1e5eb 6c 65 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 44  le */.  Expr *pD
1e5ec 62 6e 61 6d 65 2c 20 20 20 20 20 20 20 2f 2a 20  bname,       /* 
1e5ed 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61  Name of the data
1e5ee 62 61 73 65 20 74 6f 20 75 73 65 20 69 6e 74 65  base to use inte
1e5ef 72 6e 61 6c 6c 79 20 2a 2f 0a 20 20 45 78 70 72  rnally */.  Expr
1e5f0 20 2a 70 4b 65 79 20 20 20 20 20 20 20 20 20 20   *pKey          
1e5f1 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6b 65 79   /* Database key
1e5f2 20 66 6f 72 20 65 6e 63 72 79 70 74 69 6f 6e 20   for encryption 
1e5f3 65 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 29 7b 0a  extension */.){.
1e5f4 20 20 69 6e 74 20 72 63 3b 0a 20 20 4e 61 6d 65    int rc;.  Name
1e5f5 43 6f 6e 74 65 78 74 20 73 4e 61 6d 65 3b 0a 20  Context sName;. 
1e5f6 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71 6c 69   Vdbe *v;.  sqli
1e5f7 74 65 33 2a 20 64 62 20 3d 20 70 50 61 72 73 65  te3* db = pParse
1e5f8 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 72 65 67 41  ->db;.  int regA
1e5f9 72 67 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  rgs;..#ifndef SQ
1e5fa 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
1e5fb 49 5a 41 54 49 4f 4e 0a 20 20 61 73 73 65 72 74  IZATION.  assert
1e5fc 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
1e5fd 65 64 20 7c 7c 20 70 41 75 74 68 41 72 67 20 29  ed || pAuthArg )
1e5fe 3b 0a 20 20 69 66 28 20 70 41 75 74 68 41 72 67  ;.  if( pAuthArg
1e5ff 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 41   ){.    char *zA
1e600 75 74 68 41 72 67 20 3d 20 73 71 6c 69 74 65 33  uthArg = sqlite3
1e601 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62  NameFromToken(db
1e602 2c 20 26 70 41 75 74 68 41 72 67 2d 3e 73 70 61  , &pAuthArg->spa
1e603 6e 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 41 75  n);.    if( !zAu
1e604 74 68 41 72 67 20 29 7b 0a 20 20 20 20 20 20 67  thArg ){.      g
1e605 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a  oto attach_end;.
1e606 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
1e607 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1e608 70 50 61 72 73 65 2c 20 74 79 70 65 2c 20 7a 41  pParse, type, zA
1e609 75 74 68 41 72 67 2c 20 30 2c 20 30 29 3b 0a 20  uthArg, 0, 0);. 
1e60a 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
1e60b 28 64 62 2c 20 7a 41 75 74 68 41 72 67 29 3b 0a  (db, zAuthArg);.
1e60c 20 20 20 20 69 66 28 72 63 21 3d 53 51 4c 49 54      if(rc!=SQLIT
1e60d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 67 6f  E_OK ){.      go
1e60e 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a 20  to attach_end;. 
1e60f 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 20     }.  }.#endif 
1e610 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  /* SQLITE_OMIT_A
1e611 55 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a  UTHORIZATION */.
1e612 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e 61 6d 65  .  memset(&sName
1e613 2c 20 30 2c 20 73 69 7a 65 6f 66 28 4e 61 6d 65  , 0, sizeof(Name
1e614 43 6f 6e 74 65 78 74 29 29 3b 0a 20 20 73 4e 61  Context));.  sNa
1e615 6d 65 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72  me.pParse = pPar
1e616 73 65 3b 0a 0a 20 20 69 66 28 20 0a 20 20 20 20  se;..  if( .    
1e617 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
1e618 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68   = resolveAttach
1e619 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 46 69  Expr(&sName, pFi
1e61a 6c 65 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20  lename)) ||.    
1e61b 20 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63    SQLITE_OK!=(rc
1e61c 20 3d 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68   = resolveAttach
1e61d 45 78 70 72 28 26 73 4e 61 6d 65 2c 20 70 44 62  Expr(&sName, pDb
1e61e 6e 61 6d 65 29 29 20 7c 7c 0a 20 20 20 20 20 20  name)) ||.      
1e61f 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
1e620 20 72 65 73 6f 6c 76 65 41 74 74 61 63 68 45 78   resolveAttachEx
1e621 70 72 28 26 73 4e 61 6d 65 2c 20 70 4b 65 79 29  pr(&sName, pKey)
1e622 29 0a 20 20 29 7b 0a 20 20 20 20 70 50 61 72 73  ).  ){.    pPars
1e623 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67  e->nErr++;.    g
1e624 6f 74 6f 20 61 74 74 61 63 68 5f 65 6e 64 3b 0a  oto attach_end;.
1e625 20 20 7d 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74    }..  v = sqlit
1e626 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1e627 29 3b 0a 20 20 72 65 67 41 72 67 73 20 3d 20 73  );.  regArgs = s
1e628 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61 6e  qlite3GetTempRan
1e629 67 65 28 70 50 61 72 73 65 2c 20 34 29 3b 0a 20  ge(pParse, 4);. 
1e62a 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
1e62b 28 70 50 61 72 73 65 2c 20 70 46 69 6c 65 6e 61  (pParse, pFilena
1e62c 6d 65 2c 20 72 65 67 41 72 67 73 29 3b 0a 20 20  me, regArgs);.  
1e62d 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
1e62e 70 50 61 72 73 65 2c 20 70 44 62 6e 61 6d 65 2c  pParse, pDbname,
1e62f 20 72 65 67 41 72 67 73 2b 31 29 3b 0a 20 20 73   regArgs+1);.  s
1e630 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
1e631 50 61 72 73 65 2c 20 70 4b 65 79 2c 20 72 65 67  Parse, pKey, reg
1e632 41 72 67 73 2b 32 29 3b 0a 0a 20 20 61 73 73 65  Args+2);..  asse
1e633 72 74 28 20 76 20 7c 7c 20 64 62 2d 3e 6d 61 6c  rt( v || db->mal
1e634 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69  locFailed );.  i
1e635 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
1e636 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1e637 20 4f 50 5f 46 75 6e 63 74 69 6f 6e 2c 20 30 2c   OP_Function, 0,
1e638 20 72 65 67 41 72 67 73 2b 33 2d 70 46 75 6e 63   regArgs+3-pFunc
1e639 2d 3e 6e 41 72 67 2c 20 72 65 67 41 72 67 73 2b  ->nArg, regArgs+
1e63a 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  3);.    sqlite3V
1e63b 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 70  dbeChangeP5(v, p
1e63c 46 75 6e 63 2d 3e 6e 41 72 67 29 3b 0a 20 20 20  Func->nArg);.   
1e63d 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1e63e 67 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 68 61  geP4(v, -1, (cha
1e63f 72 20 2a 29 70 46 75 6e 63 2c 20 50 34 5f 46 55  r *)pFunc, P4_FU
1e640 4e 43 44 45 46 29 3b 0a 0a 20 20 20 20 2f 2a 20  NCDEF);..    /* 
1e641 43 6f 64 65 20 61 6e 20 4f 50 5f 45 78 70 69 72  Code an OP_Expir
1e642 65 2e 20 46 6f 72 20 61 6e 20 41 54 54 41 43 48  e. For an ATTACH
1e643 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65 74 20   statement, set 
1e644 50 31 20 74 6f 20 74 72 75 65 20 28 65 78 70 69  P1 to true (expi
1e645 72 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 73  re this.    ** s
1e646 74 61 74 65 6d 65 6e 74 20 6f 6e 6c 79 29 2e 20  tatement only). 
1e647 46 6f 72 20 44 45 54 41 43 48 2c 20 73 65 74 20  For DETACH, set 
1e648 69 74 20 74 6f 20 66 61 6c 73 65 20 28 65 78 70  it to false (exp
1e649 69 72 65 20 61 6c 6c 20 65 78 69 73 74 69 6e 67  ire all existing
1e64a 0a 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  .    ** statemen
1e64b 74 73 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ts)..    */.    
1e64c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1e64d 31 28 76 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20  1(v, OP_Expire, 
1e64e 28 74 79 70 65 3d 3d 53 51 4c 49 54 45 5f 41 54  (type==SQLITE_AT
1e64f 54 41 43 48 29 29 3b 0a 20 20 7d 0a 20 20 0a 61  TACH));.  }.  .a
1e650 74 74 61 63 68 5f 65 6e 64 3a 0a 20 20 73 71 6c  ttach_end:.  sql
1e651 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
1e652 62 2c 20 70 46 69 6c 65 6e 61 6d 65 29 3b 0a 20  b, pFilename);. 
1e653 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
1e654 74 65 28 64 62 2c 20 70 44 62 6e 61 6d 65 29 3b  te(db, pDbname);
1e655 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65  .  sqlite3ExprDe
1e656 6c 65 74 65 28 64 62 2c 20 70 4b 65 79 29 3b 0a  lete(db, pKey);.
1e657 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 65 64 20  }../*.** Called 
1e658 62 79 20 74 68 65 20 70 61 72 73 65 72 20 74 6f  by the parser to
1e659 20 63 6f 6d 70 69 6c 65 20 61 20 44 45 54 41 43   compile a DETAC
1e65a 48 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a  H statement..**.
1e65b 2a 2a 20 20 20 20 20 44 45 54 41 43 48 20 70 44  **     DETACH pD
1e65c 62 6e 61 6d 65 0a 2a 2f 0a 53 51 4c 49 54 45 5f  bname.*/.SQLITE_
1e65d 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1e65e 69 74 65 33 44 65 74 61 63 68 28 50 61 72 73 65  ite3Detach(Parse
1e65f 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20 2a   *pParse, Expr *
1e660 70 44 62 6e 61 6d 65 29 7b 0a 20 20 73 74 61 74  pDbname){.  stat
1e661 69 63 20 46 75 6e 63 44 65 66 20 64 65 74 61 63  ic FuncDef detac
1e662 68 5f 66 75 6e 63 20 3d 20 7b 0a 20 20 20 20 31  h_func = {.    1
1e663 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
1e664 20 2f 2a 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20   /* nArg */.    
1e665 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20  SQLITE_UTF8,    
1e666 20 20 2f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f    /* iPrefEnc */
1e667 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20  .    0,         
1e668 20 20 20 20 20 20 20 2f 2a 20 66 6c 61 67 73 20         /* flags 
1e669 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20 20 20 20  */.    0,       
1e66a 20 20 20 20 20 20 20 20 20 2f 2a 20 70 55 73 65           /* pUse
1e66b 72 44 61 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20  rData */.    0, 
1e66c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1e66d 2a 20 70 4e 65 78 74 20 2a 2f 0a 20 20 20 20 64  * pNext */.    d
1e66e 65 74 61 63 68 46 75 6e 63 2c 20 20 20 20 20 20  etachFunc,      
1e66f 20 2f 2a 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20   /* xFunc */.   
1e670 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
1e671 20 20 20 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20     /* xStep */. 
1e672 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20     0,           
1e673 20 20 20 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a       /* xFinaliz
1e674 65 20 2a 2f 0a 20 20 20 20 22 73 71 6c 69 74 65  e */.    "sqlite
1e675 5f 64 65 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e  _detach",  /* zN
1e676 61 6d 65 20 2a 2f 0a 20 20 20 20 30 20 20 20 20  ame */.    0    
1e677 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1e678 70 48 61 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20  pHash */.  };.  
1e679 63 6f 64 65 41 74 74 61 63 68 28 70 50 61 72 73  codeAttach(pPars
1e67a 65 2c 20 53 51 4c 49 54 45 5f 44 45 54 41 43 48  e, SQLITE_DETACH
1e67b 2c 20 26 64 65 74 61 63 68 5f 66 75 6e 63 2c 20  , &detach_func, 
1e67c 70 44 62 6e 61 6d 65 2c 20 30 2c 20 30 2c 20 70  pDbname, 0, 0, p
1e67d 44 62 6e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  Dbname);.}../*.*
1e67e 2a 20 43 61 6c 6c 65 64 20 62 79 20 74 68 65 20  * Called by the 
1e67f 70 61 72 73 65 72 20 74 6f 20 63 6f 6d 70 69 6c  parser to compil
1e680 65 20 61 6e 20 41 54 54 41 43 48 20 73 74 61 74  e an ATTACH stat
1e681 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  ement..**.**    
1e682 20 41 54 54 41 43 48 20 70 20 41 53 20 70 44 62   ATTACH p AS pDb
1e683 6e 61 6d 65 20 4b 45 59 20 70 4b 65 79 0a 2a 2f  name KEY pKey.*/
1e684 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e685 76 6f 69 64 20 73 71 6c 69 74 65 33 41 74 74 61  void sqlite3Atta
1e686 63 68 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ch(Parse *pParse
1e687 2c 20 45 78 70 72 20 2a 70 2c 20 45 78 70 72 20  , Expr *p, Expr 
1e688 2a 70 44 62 6e 61 6d 65 2c 20 45 78 70 72 20 2a  *pDbname, Expr *
1e689 70 4b 65 79 29 7b 0a 20 20 73 74 61 74 69 63 20  pKey){.  static 
1e68a 46 75 6e 63 44 65 66 20 61 74 74 61 63 68 5f 66  FuncDef attach_f
1e68b 75 6e 63 20 3d 20 7b 0a 20 20 20 20 33 2c 20 20  unc = {.    3,  
1e68c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1e68d 20 6e 41 72 67 20 2a 2f 0a 20 20 20 20 53 51 4c   nArg */.    SQL
1e68e 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 2f  ITE_UTF8,      /
1e68f 2a 20 69 50 72 65 66 45 6e 63 20 2a 2f 0a 20 20  * iPrefEnc */.  
1e690 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
1e691 20 20 20 20 2f 2a 20 66 6c 61 67 73 20 2a 2f 0a      /* flags */.
1e692 20 20 20 20 30 2c 20 20 20 20 20 20 20 20 20 20      0,          
1e693 20 20 20 20 20 20 2f 2a 20 70 55 73 65 72 44 61        /* pUserDa
1e694 74 61 20 2a 2f 0a 20 20 20 20 30 2c 20 20 20 20  ta */.    0,    
1e695 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70              /* p
1e696 4e 65 78 74 20 2a 2f 0a 20 20 20 20 61 74 74 61  Next */.    atta
1e697 63 68 46 75 6e 63 2c 20 20 20 20 20 20 20 2f 2a  chFunc,       /*
1e698 20 78 46 75 6e 63 20 2a 2f 0a 20 20 20 20 30 2c   xFunc */.    0,
1e699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1e69a 2f 2a 20 78 53 74 65 70 20 2a 2f 0a 20 20 20 20  /* xStep */.    
1e69b 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  0,              
1e69c 20 20 2f 2a 20 78 46 69 6e 61 6c 69 7a 65 20 2a    /* xFinalize *
1e69d 2f 0a 20 20 20 20 22 73 71 6c 69 74 65 5f 61 74  /.    "sqlite_at
1e69e 74 61 63 68 22 2c 20 20 2f 2a 20 7a 4e 61 6d 65  tach",  /* zName
1e69f 20 2a 2f 0a 20 20 20 20 30 20 20 20 20 20 20 20   */.    0       
1e6a0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 48 61            /* pHa
1e6a1 73 68 20 2a 2f 0a 20 20 7d 3b 0a 20 20 63 6f 64  sh */.  };.  cod
1e6a2 65 41 74 74 61 63 68 28 70 50 61 72 73 65 2c 20  eAttach(pParse, 
1e6a3 53 51 4c 49 54 45 5f 41 54 54 41 43 48 2c 20 26  SQLITE_ATTACH, &
1e6a4 61 74 74 61 63 68 5f 66 75 6e 63 2c 20 70 2c 20  attach_func, p, 
1e6a5 70 2c 20 70 44 62 6e 61 6d 65 2c 20 70 4b 65 79  p, pDbname, pKey
1e6a6 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  );.}.#endif /* S
1e6a7 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41 43  QLITE_OMIT_ATTAC
1e6a8 48 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74  H */../*.** Init
1e6a9 69 61 6c 69 7a 65 20 61 20 44 62 46 69 78 65 72  ialize a DbFixer
1e6aa 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 69   structure.  Thi
1e6ab 73 20 72 6f 75 74 69 6e 65 20 6d 75 73 74 20 62  s routine must b
1e6ac 65 20 63 61 6c 6c 65 64 20 70 72 69 6f 72 0a 2a  e called prior.*
1e6ad 2a 20 74 6f 20 70 61 73 73 69 6e 67 20 74 68 65  * to passing the
1e6ae 20 73 74 72 75 63 74 75 72 65 20 74 6f 20 6f 6e   structure to on
1e6af 65 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65 46  e of the sqliteF
1e6b0 69 78 41 41 41 41 28 29 20 72 6f 75 74 69 6e 65  ixAAAA() routine
1e6b1 73 20 62 65 6c 6f 77 2e 0a 2a 2a 0a 2a 2a 20 54  s below..**.** T
1e6b2 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65 20  he return value 
1e6b3 69 6e 64 69 63 61 74 65 73 20 77 68 65 74 68 65  indicates whethe
1e6b4 72 20 6f 72 20 6e 6f 74 20 66 69 78 61 74 69 6f  r or not fixatio
1e6b5 6e 20 69 73 20 72 65 71 75 69 72 65 64 2e 20 20  n is required.  
1e6b6 54 52 55 45 0a 2a 2a 20 6d 65 61 6e 73 20 77 65  TRUE.** means we
1e6b7 20 64 6f 20 6e 65 65 64 20 74 6f 20 66 69 78 20   do need to fix 
1e6b8 74 68 65 20 64 61 74 61 62 61 73 65 20 72 65 66  the database ref
1e6b9 65 72 65 6e 63 65 73 2c 20 46 41 4c 53 45 20 6d  erences, FALSE m
1e6ba 65 61 6e 73 20 77 65 20 64 6f 20 6e 6f 74 2e 0a  eans we do not..
1e6bb 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e6bc 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
1e6bd 49 6e 69 74 28 0a 20 20 44 62 46 69 78 65 72 20  Init(.  DbFixer 
1e6be 2a 70 46 69 78 2c 20 20 20 20 20 20 2f 2a 20 54  *pFix,      /* T
1e6bf 68 65 20 66 69 78 65 72 20 74 6f 20 62 65 20 69  he fixer to be i
1e6c0 6e 69 74 69 61 6c 69 7a 65 64 20 2a 2f 0a 20 20  nitialized */.  
1e6c1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1e6c2 20 20 20 20 2f 2a 20 45 72 72 6f 72 20 6d 65 73      /* Error mes
1e6c3 73 61 67 65 73 20 77 69 6c 6c 20 62 65 20 77 72  sages will be wr
1e6c4 69 74 74 65 6e 20 68 65 72 65 20 2a 2f 0a 20 20  itten here */.  
1e6c5 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
1e6c6 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
1e6c7 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61 74  he database that
1e6c8 20 6d 75 73 74 20 62 65 20 75 73 65 64 20 2a 2f   must be used */
1e6c9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
1e6ca 54 79 70 65 2c 20 20 2f 2a 20 22 76 69 65 77 22  Type,  /* "view"
1e6cb 2c 20 22 74 72 69 67 67 65 72 22 2c 20 6f 72 20  , "trigger", or 
1e6cc 22 69 6e 64 65 78 22 20 2a 2f 0a 20 20 63 6f 6e  "index" */.  con
1e6cd 73 74 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 20  st Token *pName 
1e6ce 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20   /* Name of the 
1e6cf 76 69 65 77 2c 20 74 72 69 67 67 65 72 2c 20 6f  view, trigger, o
1e6d0 72 20 69 6e 64 65 78 20 2a 2f 0a 29 7b 0a 20 20  r index */.){.  
1e6d1 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20  sqlite3 *db;..  
1e6d2 69 66 28 20 69 44 62 3c 30 20 7c 7c 20 69 44 62  if( iDb<0 || iDb
1e6d3 3d 3d 31 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==1 ) return 0;.
1e6d4 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64    db = pParse->d
1e6d5 62 3b 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d  b;.  assert( db-
1e6d6 3e 6e 44 62 3e 69 44 62 20 29 3b 0a 20 20 70 46  >nDb>iDb );.  pF
1e6d7 69 78 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61  ix->pParse = pPa
1e6d8 72 73 65 3b 0a 20 20 70 46 69 78 2d 3e 7a 44 62  rse;.  pFix->zDb
1e6d9 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e   = db->aDb[iDb].
1e6da 7a 4e 61 6d 65 3b 0a 20 20 70 46 69 78 2d 3e 7a  zName;.  pFix->z
1e6db 54 79 70 65 20 3d 20 7a 54 79 70 65 3b 0a 20 20  Type = zType;.  
1e6dc 70 46 69 78 2d 3e 70 4e 61 6d 65 20 3d 20 70 4e  pFix->pName = pN
1e6dd 61 6d 65 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ame;.  return 1;
1e6de 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f  .}../*.** The fo
1e6df 6c 6c 6f 77 69 6e 67 20 73 65 74 20 6f 66 20 72  llowing set of r
1e6e0 6f 75 74 69 6e 65 73 20 77 61 6c 6b 20 74 68 72  outines walk thr
1e6e1 6f 75 67 68 20 74 68 65 20 70 61 72 73 65 20 74  ough the parse t
1e6e2 72 65 65 20 61 6e 64 20 61 73 73 69 67 6e 0a 2a  ree and assign.*
1e6e3 2a 20 61 20 73 70 65 63 69 66 69 63 20 64 61 74  * a specific dat
1e6e4 61 62 61 73 65 20 74 6f 20 61 6c 6c 20 74 61 62  abase to all tab
1e6e5 6c 65 20 72 65 66 65 72 65 6e 63 65 73 20 77 68  le references wh
1e6e6 65 72 65 20 74 68 65 20 64 61 74 61 62 61 73 65  ere the database
1e6e7 20 6e 61 6d 65 0a 2a 2a 20 77 61 73 20 6c 65 66   name.** was lef
1e6e8 74 20 75 6e 73 70 65 63 69 66 69 65 64 20 69 6e  t unspecified in
1e6e9 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
1e6ea 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68  L statement.  Th
1e6eb 65 20 70 46 69 78 20 73 74 72 75 63 74 75 72 65  e pFix structure
1e6ec 0a 2a 2a 20 6d 75 73 74 20 68 61 76 65 20 62 65  .** must have be
1e6ed 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 62  en initialized b
1e6ee 79 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74  y a prior call t
1e6ef 6f 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74  o sqlite3FixInit
1e6f0 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ()..**.** These 
1e6f1 72 6f 75 74 69 6e 65 73 20 61 72 65 20 75 73 65  routines are use
1e6f2 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74  d to make sure t
1e6f3 68 61 74 20 61 6e 20 69 6e 64 65 78 2c 20 74 72  hat an index, tr
1e6f4 69 67 67 65 72 2c 20 6f 72 0a 2a 2a 20 76 69 65  igger, or.** vie
1e6f5 77 20 69 6e 20 6f 6e 65 20 64 61 74 61 62 61 73  w in one databas
1e6f6 65 20 64 6f 65 73 20 6e 6f 74 20 72 65 66 65 72  e does not refer
1e6f7 20 74 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 61   to objects in a
1e6f8 20 64 69 66 66 65 72 65 6e 74 20 64 61 74 61 62   different datab
1e6f9 61 73 65 2e 0a 2a 2a 20 28 45 78 63 65 70 74 69  ase..** (Excepti
1e6fa 6f 6e 3a 20 69 6e 64 69 63 65 73 2c 20 74 72 69  on: indices, tri
1e6fb 67 67 65 72 73 2c 20 61 6e 64 20 76 69 65 77 73  ggers, and views
1e6fc 20 69 6e 20 74 68 65 20 54 45 4d 50 20 64 61 74   in the TEMP dat
1e6fd 61 62 61 73 65 20 61 72 65 0a 2a 2a 20 61 6c 6c  abase are.** all
1e6fe 6f 77 65 64 20 74 6f 20 72 65 66 65 72 20 74 6f  owed to refer to
1e6ff 20 61 6e 79 74 68 69 6e 67 2e 29 20 20 49 66 20   anything.)  If 
1e700 61 20 72 65 66 65 72 65 6e 63 65 20 69 73 20 65  a reference is e
1e701 78 70 6c 69 63 69 74 6c 79 20 6d 61 64 65 0a 2a  xplicitly made.*
1e702 2a 20 74 6f 20 61 6e 20 6f 62 6a 65 63 74 20 69  * to an object i
1e703 6e 20 61 20 64 69 66 66 65 72 65 6e 74 20 64 61  n a different da
1e704 74 61 62 61 73 65 2c 20 61 6e 20 65 72 72 6f 72  tabase, an error
1e705 20 6d 65 73 73 61 67 65 20 69 73 20 61 64 64 65   message is adde
1e706 64 20 74 6f 0a 2a 2a 20 70 50 61 72 73 65 2d 3e  d to.** pParse->
1e707 7a 45 72 72 4d 73 67 20 61 6e 64 20 74 68 65 73  zErrMsg and thes
1e708 65 20 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72  e routines retur
1e709 6e 20 6e 6f 6e 2d 7a 65 72 6f 2e 20 20 49 66 20  n non-zero.  If 
1e70a 65 76 65 72 79 74 68 69 6e 67 0a 2a 2a 20 63 68  everything.** ch
1e70b 65 63 6b 73 20 6f 75 74 2c 20 74 68 65 73 65 20  ecks out, these 
1e70c 72 6f 75 74 69 6e 65 73 20 72 65 74 75 72 6e 20  routines return 
1e70d 30 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  0..*/.SQLITE_PRI
1e70e 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
1e70f 46 69 78 53 72 63 4c 69 73 74 28 0a 20 20 44 62  FixSrcList(.  Db
1e710 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20  Fixer *pFix,    
1e711 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
1e712 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
1e713 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73  .  SrcList *pLis
1e714 74 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 53  t       /* The S
1e715 6f 75 72 63 65 20 6c 69 73 74 20 74 6f 20 63 68  ource list to ch
1e716 65 63 6b 20 61 6e 64 20 6d 6f 64 69 66 79 20 2a  eck and modify *
1e717 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  /.){.  int i;.  
1e718 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
1e719 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
1e71a 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 0a  t_item *pItem;..
1e71b 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
1e71c 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7a 44 62   return 0;.  zDb
1e71d 20 3d 20 70 46 69 78 2d 3e 7a 44 62 3b 0a 20 20   = pFix->zDb;.  
1e71e 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
1e71f 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74  List->a; i<pList
1e720 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 49 74  ->nSrc; i++, pIt
1e721 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70  em++){.    if( p
1e722 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73 65 3d  Item->zDatabase=
1e723 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 49 74 65  =0 ){.      pIte
1e724 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73  m->zDatabase = s
1e725 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 70  qlite3DbStrDup(p
1e726 46 69 78 2d 3e 70 50 61 72 73 65 2d 3e 64 62 2c  Fix->pParse->db,
1e727 20 7a 44 62 29 3b 0a 20 20 20 20 7d 65 6c 73 65   zDb);.    }else
1e728 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1e729 43 6d 70 28 70 49 74 65 6d 2d 3e 7a 44 61 74 61  Cmp(pItem->zData
1e72a 62 61 73 65 2c 7a 44 62 29 21 3d 30 20 29 7b 0a  base,zDb)!=0 ){.
1e72b 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1e72c 6f 72 4d 73 67 28 70 46 69 78 2d 3e 70 50 61 72  orMsg(pFix->pPar
1e72d 73 65 2c 0a 20 20 20 20 20 20 20 20 20 22 25 73  se,.         "%s
1e72e 20 25 54 20 63 61 6e 6e 6f 74 20 72 65 66 65 72   %T cannot refer
1e72f 65 6e 63 65 20 6f 62 6a 65 63 74 73 20 69 6e 20  ence objects in 
1e730 64 61 74 61 62 61 73 65 20 25 73 22 2c 0a 20 20  database %s",.  
1e731 20 20 20 20 20 20 20 70 46 69 78 2d 3e 7a 54 79         pFix->zTy
1e732 70 65 2c 20 70 46 69 78 2d 3e 70 4e 61 6d 65 2c  pe, pFix->pName,
1e733 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61 73   pItem->zDatabas
1e734 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
1e735 20 31 3b 0a 20 20 20 20 7d 0a 23 69 66 20 21 64   1;.    }.#if !d
1e736 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1e737 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65 66  IT_VIEW) || !def
1e738 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
1e739 5f 54 52 49 47 47 45 52 29 0a 20 20 20 20 69 66  _TRIGGER).    if
1e73a 28 20 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65  ( sqlite3FixSele
1e73b 63 74 28 70 46 69 78 2c 20 70 49 74 65 6d 2d 3e  ct(pFix, pItem->
1e73c 70 53 65 6c 65 63 74 29 20 29 20 72 65 74 75 72  pSelect) ) retur
1e73d 6e 20 31 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  n 1;.    if( sql
1e73e 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
1e73f 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 20 29 20  , pItem->pOn) ) 
1e740 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66  return 1;.#endif
1e741 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
1e742 0a 7d 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  .}.#if !defined(
1e743 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1e744 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
1e745 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
1e746 52 29 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  R).SQLITE_PRIVAT
1e747 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78  E int sqlite3Fix
1e748 53 65 6c 65 63 74 28 0a 20 20 44 62 46 69 78 65  Select(.  DbFixe
1e749 72 20 2a 70 46 69 78 2c 20 20 20 20 20 20 20 2f  r *pFix,       /
1e74a 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65  * Context of the
1e74b 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 53   fixation */.  S
1e74c 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20  elect *pSelect  
1e74d 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43      /* The SELEC
1e74e 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 62  T statement to b
1e74f 65 20 66 69 78 65 64 20 74 6f 20 6f 6e 65 20 64  e fixed to one d
1e750 61 74 61 62 61 73 65 20 2a 2f 0a 29 7b 0a 20 20  atabase */.){.  
1e751 77 68 69 6c 65 28 20 70 53 65 6c 65 63 74 20 29  while( pSelect )
1e752 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
1e753 33 46 69 78 45 78 70 72 4c 69 73 74 28 70 46 69  3FixExprList(pFi
1e754 78 2c 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69  x, pSelect->pELi
1e755 73 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74  st) ){.      ret
1e756 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20  urn 1;.    }.   
1e757 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 53   if( sqlite3FixS
1e758 72 63 4c 69 73 74 28 70 46 69 78 2c 20 70 53 65  rcList(pFix, pSe
1e759 6c 65 63 74 2d 3e 70 53 72 63 29 20 29 7b 0a 20  lect->pSrc) ){. 
1e75a 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1e75b 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1e75c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69 78  ite3FixExpr(pFix
1e75d 2c 20 70 53 65 6c 65 63 74 2d 3e 70 57 68 65 72  , pSelect->pWher
1e75e 65 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  e) ){.      retu
1e75f 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  rn 1;.    }.    
1e760 69 66 28 20 73 71 6c 69 74 65 33 46 69 78 45 78  if( sqlite3FixEx
1e761 70 72 28 70 46 69 78 2c 20 70 53 65 6c 65 63 74  pr(pFix, pSelect
1e762 2d 3e 70 48 61 76 69 6e 67 29 20 29 7b 0a 20 20  ->pHaving) ){.  
1e763 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
1e764 20 20 7d 0a 20 20 20 20 70 53 65 6c 65 63 74 20    }.    pSelect 
1e765 3d 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  = pSelect->pPrio
1e766 72 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  r;.  }.  return 
1e767 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56  0;.}.SQLITE_PRIV
1e768 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
1e769 69 78 45 78 70 72 28 0a 20 20 44 62 46 69 78 65  ixExpr(.  DbFixe
1e76a 72 20 2a 70 46 69 78 2c 20 20 20 20 20 2f 2a 20  r *pFix,     /* 
1e76b 43 6f 6e 74 65 78 74 20 6f 66 20 74 68 65 20 66  Context of the f
1e76c 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  ixation */.  Exp
1e76d 72 20 2a 70 45 78 70 72 20 20 20 20 20 20 20 20  r *pExpr        
1e76e 2f 2a 20 54 68 65 20 65 78 70 72 65 73 73 69 6f  /* The expressio
1e76f 6e 20 74 6f 20 62 65 20 66 69 78 65 64 20 74 6f  n to be fixed to
1e770 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20 2a 2f   one database */
1e771 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 45 78  .){.  while( pEx
1e772 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71  pr ){.    if( sq
1e773 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74 28 70  lite3FixSelect(p
1e774 46 69 78 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c  Fix, pExpr->pSel
1e775 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20 72 65  ect) ){.      re
1e776 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
1e777 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 78    if( sqlite3Fix
1e778 45 78 70 72 4c 69 73 74 28 70 46 69 78 2c 20 70  ExprList(pFix, p
1e779 45 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 7b 0a  Expr->pList) ){.
1e77a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
1e77b 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71      }.    if( sq
1e77c 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46 69  lite3FixExpr(pFi
1e77d 78 2c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74  x, pExpr->pRight
1e77e 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
1e77f 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  n 1;.    }.    p
1e780 45 78 70 72 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Expr = pExpr->pL
1e781 65 66 74 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  eft;.  }.  retur
1e782 6e 20 30 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  n 0;.}.SQLITE_PR
1e783 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1e784 33 46 69 78 45 78 70 72 4c 69 73 74 28 0a 20 20  3FixExprList(.  
1e785 44 62 46 69 78 65 72 20 2a 70 46 69 78 2c 20 20  DbFixer *pFix,  
1e786 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66     /* Context of
1e787 20 74 68 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f   the fixation */
1e788 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69  .  ExprList *pLi
1e789 73 74 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70  st    /* The exp
1e78a 72 65 73 73 69 6f 6e 20 74 6f 20 62 65 20 66 69  ression to be fi
1e78b 78 65 64 20 74 6f 20 6f 6e 65 20 64 61 74 61 62  xed to one datab
1e78c 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ase */.){.  int 
1e78d 69 3b 0a 20 20 73 74 72 75 63 74 20 45 78 70 72  i;.  struct Expr
1e78e 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
1e78f 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
1e790 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 66   ) return 0;.  f
1e791 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(i=0, pItem=pL
1e792 69 73 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d  ist->a; i<pList-
1e793 3e 6e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74  >nExpr; i++, pIt
1e794 65 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73  em++){.    if( s
1e795 71 6c 69 74 65 33 46 69 78 45 78 70 72 28 70 46  qlite3FixExpr(pF
1e796 69 78 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  ix, pItem->pExpr
1e797 29 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  ) ){.      retur
1e798 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 1;.    }.  }. 
1e799 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
1e79a 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
1e79b 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
1e79c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1e79d 69 6e 74 20 73 71 6c 69 74 65 33 46 69 78 54 72  int sqlite3FixTr
1e79e 69 67 67 65 72 53 74 65 70 28 0a 20 20 44 62 46  iggerStep(.  DbF
1e79f 69 78 65 72 20 2a 70 46 69 78 2c 20 20 20 20 20  ixer *pFix,     
1e7a0 2f 2a 20 43 6f 6e 74 65 78 74 20 6f 66 20 74 68  /* Context of th
1e7a1 65 20 66 69 78 61 74 69 6f 6e 20 2a 2f 0a 20 20  e fixation */.  
1e7a2 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
1e7a3 65 70 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65  ep /* The trigge
1e7a4 72 20 73 74 65 70 20 62 65 20 66 69 78 65 64 20  r step be fixed 
1e7a5 74 6f 20 6f 6e 65 20 64 61 74 61 62 61 73 65 20  to one database 
1e7a6 2a 2f 0a 29 7b 0a 20 20 77 68 69 6c 65 28 20 70  */.){.  while( p
1e7a7 53 74 65 70 20 29 7b 0a 20 20 20 20 69 66 28 20  Step ){.    if( 
1e7a8 73 71 6c 69 74 65 33 46 69 78 53 65 6c 65 63 74  sqlite3FixSelect
1e7a9 28 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 53  (pFix, pStep->pS
1e7aa 65 6c 65 63 74 29 20 29 7b 0a 20 20 20 20 20 20  elect) ){.      
1e7ab 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a  return 1;.    }.
1e7ac 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
1e7ad 69 78 45 78 70 72 28 70 46 69 78 2c 20 70 53 74  ixExpr(pFix, pSt
1e7ae 65 70 2d 3e 70 57 68 65 72 65 29 20 29 7b 0a 20  ep->pWhere) ){. 
1e7af 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
1e7b0 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1e7b1 69 74 65 33 46 69 78 45 78 70 72 4c 69 73 74 28  ite3FixExprList(
1e7b2 70 46 69 78 2c 20 70 53 74 65 70 2d 3e 70 45 78  pFix, pStep->pEx
1e7b3 70 72 4c 69 73 74 29 20 29 7b 0a 20 20 20 20 20  prList) ){.     
1e7b4 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
1e7b5 0a 20 20 20 20 70 53 74 65 70 20 3d 20 70 53 74  .    pStep = pSt
1e7b6 65 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d 0a 20  ep->pNext;.  }. 
1e7b7 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e   return 0;.}.#en
1e7b8 64 69 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  dif../**********
1e7b9 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 61 74 74 61  **** End of atta
1e7ba 63 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ch.c ***********
1e7bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7bd 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ***/./**********
1e7be 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20  **** Begin file 
1e7bf 61 75 74 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  auth.c *********
1e7c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7c2 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20  ***/./*.** 2003 
1e7c3 4a 61 6e 75 61 72 79 20 31 31 0a 2a 2a 0a 2a 2a  January 11.**.**
1e7c4 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
1e7c5 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
1e7c6 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
1e7c7 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
1e7c8 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
1e7c9 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
1e7ca 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
1e7cb 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
1e7cc 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
1e7cd 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
1e7ce 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
1e7cf 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
1e7d0 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
1e7d1 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
1e7d2 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
1e7d3 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
1e7d4 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
1e7d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e7d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
1e7da 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
1e7db 63 6f 64 65 20 75 73 65 64 20 74 6f 20 69 6d 70  code used to imp
1e7dc 6c 65 6d 65 6e 74 20 74 68 65 20 73 71 6c 69 74  lement the sqlit
1e7dd 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
1e7de 72 28 29 0a 2a 2a 20 41 50 49 2e 20 20 54 68 69  r().** API.  Thi
1e7df 73 20 66 61 63 69 6c 69 74 79 20 69 73 20 61 6e  s facility is an
1e7e0 20 6f 70 74 69 6f 6e 61 6c 20 66 65 61 74 75 72   optional featur
1e7e1 65 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79  e of the library
1e7e2 2e 20 20 45 6d 62 65 64 64 65 64 0a 2a 2a 20 73  .  Embedded.** s
1e7e3 79 73 74 65 6d 73 20 74 68 61 74 20 64 6f 20 6e  ystems that do n
1e7e4 6f 74 20 6e 65 65 64 20 74 68 69 73 20 66 61 63  ot need this fac
1e7e5 69 6c 69 74 79 20 6d 61 79 20 6f 6d 69 74 20 69  ility may omit i
1e7e6 74 20 62 79 20 72 65 63 6f 6d 70 69 6c 69 6e 67  t by recompiling
1e7e7 0a 2a 2a 20 74 68 65 20 6c 69 62 72 61 72 79 20  .** the library 
1e7e8 77 69 74 68 20 2d 44 53 51 4c 49 54 45 5f 4f 4d  with -DSQLITE_OM
1e7e9 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e  IT_AUTHORIZATION
1e7ea 3d 31 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 61 75  =1.**.** $Id: au
1e7eb 74 68 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 37  th.c,v 1.29 2007
1e7ec 2f 30 39 2f 31 38 20 31 35 3a 35 35 3a 30 37 20  /09/18 15:55:07 
1e7ed 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a  drh Exp $.*/../*
1e7ee 0a 2a 2a 20 41 6c 6c 20 6f 66 20 74 68 65 20 63  .** All of the c
1e7ef 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c 65  ode in this file
1e7f0 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64 20   may be omitted 
1e7f1 62 79 20 64 65 66 69 6e 69 6e 67 20 61 20 73 69  by defining a si
1e7f2 6e 67 6c 65 0a 2a 2a 20 6d 61 63 72 6f 2e 0a 2a  ngle.** macro..*
1e7f3 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
1e7f4 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
1e7f5 49 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 6f  ION../*.** Set o
1e7f6 72 20 63 6c 65 61 72 20 74 68 65 20 61 63 63 65  r clear the acce
1e7f7 73 73 20 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e  ss authorization
1e7f8 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a   function..**.**
1e7f9 20 54 68 65 20 61 63 63 65 73 73 20 61 75 74 68   The access auth
1e7fa 6f 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69  orization functi
1e7fb 6f 6e 20 69 73 20 62 65 20 63 61 6c 6c 65 64 20  on is be called 
1e7fc 64 75 72 69 6e 67 20 74 68 65 20 63 6f 6d 70 69  during the compi
1e7fd 6c 61 74 69 6f 6e 0a 2a 2a 20 70 68 61 73 65 20  lation.** phase 
1e7fe 74 6f 20 76 65 72 69 66 79 20 74 68 61 74 20 74  to verify that t
1e7ff 68 65 20 75 73 65 72 20 68 61 73 20 72 65 61 64  he user has read
1e800 20 61 6e 64 2f 6f 72 20 77 72 69 74 65 20 61 63   and/or write ac
1e801 63 65 73 73 20 70 65 72 6d 69 73 73 69 6f 6e 20  cess permission 
1e802 6f 6e 0a 2a 2a 20 76 61 72 69 6f 75 73 20 66 69  on.** various fi
1e803 65 6c 64 73 20 6f 66 20 74 68 65 20 64 61 74 61  elds of the data
1e804 62 61 73 65 2e 20 20 54 68 65 20 66 69 72 73 74  base.  The first
1e805 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68 65   argument to the
1e806 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a   auth function.*
1e807 2a 20 69 73 20 61 20 63 6f 70 79 20 6f 66 20 74  * is a copy of t
1e808 68 65 20 33 72 64 20 61 72 67 75 6d 65 6e 74 20  he 3rd argument 
1e809 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  to this routine.
1e80a 20 20 54 68 65 20 73 65 63 6f 6e 64 20 61 72 67    The second arg
1e80b 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
1e80c 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 69 73  auth function is
1e80d 20 6f 6e 65 20 6f 66 20 74 68 65 73 65 20 63 6f   one of these co
1e80e 6e 73 74 61 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  nstants:.**.**  
1e80f 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41       SQLITE_CREA
1e810 54 45 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20  TE_INDEX.**     
1e811 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f    SQLITE_CREATE_
1e812 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20 53  TABLE.**       S
1e813 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54 45 4d  QLITE_CREATE_TEM
1e814 50 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20  P_INDEX.**      
1e815 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f 54   SQLITE_CREATE_T
1e816 45 4d 50 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20  EMP_TABLE.**    
1e817 20 20 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45     SQLITE_CREATE
1e818 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 0a 2a 2a  _TEMP_TRIGGER.**
1e819 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
1e81a 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 0a 2a  EATE_TEMP_VIEW.*
1e81b 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43  *       SQLITE_C
1e81c 52 45 41 54 45 5f 54 52 49 47 47 45 52 0a 2a 2a  REATE_TRIGGER.**
1e81d 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 43 52         SQLITE_CR
1e81e 45 41 54 45 5f 56 49 45 57 0a 2a 2a 20 20 20 20  EATE_VIEW.**    
1e81f 20 20 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45     SQLITE_DELETE
1e820 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45  .**       SQLITE
1e821 5f 44 52 4f 50 5f 49 4e 44 45 58 0a 2a 2a 20 20  _DROP_INDEX.**  
1e822 20 20 20 20 20 53 51 4c 49 54 45 5f 44 52 4f 50       SQLITE_DROP
1e823 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
1e824 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1e825 5f 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20 20 20  _INDEX.**       
1e826 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1e827 5f 54 41 42 4c 45 0a 2a 2a 20 20 20 20 20 20 20  _TABLE.**       
1e828 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1e829 5f 54 52 49 47 47 45 52 0a 2a 2a 20 20 20 20 20  _TRIGGER.**     
1e82a 20 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45    SQLITE_DROP_TE
1e82b 4d 50 5f 56 49 45 57 0a 2a 2a 20 20 20 20 20 20  MP_VIEW.**      
1e82c 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 52 49   SQLITE_DROP_TRI
1e82d 47 47 45 52 0a 2a 2a 20 20 20 20 20 20 20 53 51  GGER.**       SQ
1e82e 4c 49 54 45 5f 44 52 4f 50 5f 56 49 45 57 0a 2a  LITE_DROP_VIEW.*
1e82f 2a 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 49  *       SQLITE_I
1e830 4e 53 45 52 54 0a 2a 2a 20 20 20 20 20 20 20 53  NSERT.**       S
1e831 51 4c 49 54 45 5f 50 52 41 47 4d 41 0a 2a 2a 20  QLITE_PRAGMA.** 
1e832 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 45 41        SQLITE_REA
1e833 44 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 49 54  D.**       SQLIT
1e834 45 5f 53 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20  E_SELECT.**     
1e835 20 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 41 43    SQLITE_TRANSAC
1e836 54 49 4f 4e 0a 2a 2a 20 20 20 20 20 20 20 53 51  TION.**       SQ
1e837 4c 49 54 45 5f 55 50 44 41 54 45 0a 2a 2a 0a 2a  LITE_UPDATE.**.*
1e838 2a 20 54 68 65 20 74 68 69 72 64 20 61 6e 64 20  * The third and 
1e839 66 6f 75 72 74 68 20 61 72 67 75 6d 65 6e 74 73  fourth arguments
1e83a 20 74 6f 20 74 68 65 20 61 75 74 68 20 66 75 6e   to the auth fun
1e83b 63 74 69 6f 6e 20 61 72 65 20 74 68 65 20 6e 61  ction are the na
1e83c 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61 62  me of.** the tab
1e83d 6c 65 20 61 6e 64 20 74 68 65 20 63 6f 6c 75 6d  le and the colum
1e83e 6e 20 74 68 61 74 20 61 72 65 20 62 65 69 6e 67  n that are being
1e83f 20 61 63 63 65 73 73 65 64 2e 20 20 54 68 65 20   accessed.  The 
1e840 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a  auth function.**
1e841 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 65   should return e
1e842 69 74 68 65 72 20 53 51 4c 49 54 45 5f 4f 4b 2c  ither SQLITE_OK,
1e843 20 53 51 4c 49 54 45 5f 44 45 4e 59 2c 20 6f 72   SQLITE_DENY, or
1e844 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2e 20   SQLITE_IGNORE. 
1e845 20 49 66 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b   If.** SQLITE_OK
1e846 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 69 74   is returned, it
1e847 20 6d 65 61 6e 73 20 74 68 61 74 20 61 63 63 65   means that acce
1e848 73 73 20 69 73 20 61 6c 6c 6f 77 65 64 2e 20 20  ss is allowed.  
1e849 53 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 6d  SQLITE_DENY.** m
1e84a 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 53 51  eans that the SQ
1e84b 4c 20 73 74 61 74 65 6d 65 6e 74 20 77 69 6c 6c  L statement will
1e84c 20 6e 65 76 65 72 2d 72 75 6e 20 2d 20 74 68 65   never-run - the
1e84d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20   sqlite3_exec() 
1e84e 63 61 6c 6c 0a 2a 2a 20 77 69 6c 6c 20 72 65 74  call.** will ret
1e84f 75 72 6e 20 77 69 74 68 20 61 6e 20 65 72 72 6f  urn with an erro
1e850 72 2e 20 20 53 51 4c 49 54 45 5f 49 47 4e 4f 52  r.  SQLITE_IGNOR
1e851 45 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  E means that the
1e852 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 0a 2a   SQL statement.*
1e853 2a 20 73 68 6f 75 6c 64 20 72 75 6e 20 62 75 74  * should run but
1e854 20 61 74 74 65 6d 70 74 73 20 74 6f 20 72 65 61   attempts to rea
1e855 64 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20  d the specified 
1e856 63 6f 6c 75 6d 6e 20 77 69 6c 6c 20 72 65 74 75  column will retu
1e857 72 6e 20 4e 55 4c 4c 0a 2a 2a 20 61 6e 64 20 61  rn NULL.** and a
1e858 74 74 65 6d 70 74 73 20 74 6f 20 77 72 69 74 65  ttempts to write
1e859 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77 69 6c 6c   the column will
1e85a 20 62 65 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a   be ignored..**.
1e85b 2a 2a 20 53 65 74 74 69 6e 67 20 74 68 65 20 61  ** Setting the a
1e85c 75 74 68 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20  uth function to 
1e85d 4e 55 4c 4c 20 64 69 73 61 62 6c 65 73 20 74 68  NULL disables th
1e85e 69 73 20 68 6f 6f 6b 2e 20 20 54 68 65 20 64 65  is hook.  The de
1e85f 66 61 75 6c 74 0a 2a 2a 20 73 65 74 74 69 6e 67  fault.** setting
1e860 20 6f 66 20 74 68 65 20 61 75 74 68 20 66 75 6e   of the auth fun
1e861 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2e 0a 2a  ction is NULL..*
1e862 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
1e863 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 74   sqlite3_set_aut
1e864 68 6f 72 69 7a 65 72 28 0a 20 20 73 71 6c 69 74  horizer(.  sqlit
1e865 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28 2a  e3 *db,.  int (*
1e866 78 41 75 74 68 29 28 76 6f 69 64 2a 2c 69 6e 74  xAuth)(void*,int
1e867 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1e868 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
1e869 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1e86a 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 0a  ),.  void *pArg.
1e86b 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
1e86c 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
1e86d 65 78 29 3b 0a 20 20 64 62 2d 3e 78 41 75 74 68  ex);.  db->xAuth
1e86e 20 3d 20 78 41 75 74 68 3b 0a 20 20 64 62 2d 3e   = xAuth;.  db->
1e86f 70 41 75 74 68 41 72 67 20 3d 20 70 41 72 67 3b  pAuthArg = pArg;
1e870 0a 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65  .  sqlite3Expire
1e871 50 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e  PreparedStatemen
1e872 74 73 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65  ts(db);.  sqlite
1e873 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62  3_mutex_leave(db
1e874 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75  ->mutex);.  retu
1e875 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
1e876 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 61 6e 20  ./*.** Write an 
1e877 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e  error message in
1e878 74 6f 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d  to pParse->zErrM
1e879 73 67 20 74 68 61 74 20 65 78 70 6c 61 69 6e 73  sg that explains
1e87a 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 75 73 65   that the.** use
1e87b 72 2d 73 75 70 70 6c 69 65 64 20 61 75 74 68 6f  r-supplied autho
1e87c 72 69 7a 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f  rization functio
1e87d 6e 20 72 65 74 75 72 6e 65 64 20 61 6e 20 69 6c  n returned an il
1e87e 6c 65 67 61 6c 20 76 61 6c 75 65 2e 0a 2a 2f 0a  legal value..*/.
1e87f 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
1e880 74 65 41 75 74 68 42 61 64 52 65 74 75 72 6e 43  teAuthBadReturnC
1e881 6f 64 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ode(Parse *pPars
1e882 65 2c 20 69 6e 74 20 72 63 29 7b 0a 20 20 73 71  e, int rc){.  sq
1e883 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1e884 61 72 73 65 2c 20 22 69 6c 6c 65 67 61 6c 20 72  arse, "illegal r
1e885 65 74 75 72 6e 20 76 61 6c 75 65 20 28 25 64 29  eturn value (%d)
1e886 20 66 72 6f 6d 20 74 68 65 20 22 0a 20 20 20 20   from the ".    
1e887 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 66  "authorization f
1e888 75 6e 63 74 69 6f 6e 20 2d 20 73 68 6f 75 6c 64  unction - should
1e889 20 62 65 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 53   be SQLITE_OK, S
1e88a 51 4c 49 54 45 5f 49 47 4e 4f 52 45 2c 20 22 0a  QLITE_IGNORE, ".
1e88b 20 20 20 20 22 6f 72 20 53 51 4c 49 54 45 5f 44      "or SQLITE_D
1e88c 45 4e 59 22 2c 20 72 63 29 3b 0a 20 20 70 50 61  ENY", rc);.  pPa
1e88d 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45  rse->rc = SQLITE
1e88e 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  _ERROR;.}../*.**
1e88f 20 54 68 65 20 70 45 78 70 72 20 73 68 6f 75 6c   The pExpr shoul
1e890 64 20 62 65 20 61 20 54 4b 5f 43 4f 4c 55 4d 4e  d be a TK_COLUMN
1e891 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54 68   expression.  Th
1e892 65 20 74 61 62 6c 65 20 72 65 66 65 72 72 65 64  e table referred
1e893 20 74 6f 0a 2a 2a 20 69 73 20 69 6e 20 70 54 61   to.** is in pTa
1e894 62 4c 69 73 74 20 6f 72 20 65 6c 73 65 20 69 74  bList or else it
1e895 20 69 73 20 74 68 65 20 4e 45 57 20 6f 72 20 4f   is the NEW or O
1e896 4c 44 20 74 61 62 6c 65 20 6f 66 20 61 20 74 72  LD table of a tr
1e897 69 67 67 65 72 2e 20 20 0a 2a 2a 20 43 68 65 63  igger.  .** Chec
1e898 6b 20 74 6f 20 73 65 65 20 69 66 20 69 74 20 69  k to see if it i
1e899 73 20 4f 4b 20 74 6f 20 72 65 61 64 20 74 68 69  s OK to read thi
1e89a 73 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6c  s particular col
1e89b 75 6d 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  umn..**.** If th
1e89c 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f 6e 20  e auth function 
1e89d 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 49  returns SQLITE_I
1e89e 47 4e 4f 52 45 2c 20 63 68 61 6e 67 65 20 74 68  GNORE, change th
1e89f 65 20 54 4b 5f 43 4f 4c 55 4d 4e 20 0a 2a 2a 20  e TK_COLUMN .** 
1e8a0 69 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 74 6f  instruction into
1e8a1 20 61 20 54 4b 5f 4e 55 4c 4c 2e 20 20 49 66 20   a TK_NULL.  If 
1e8a2 74 68 65 20 61 75 74 68 20 66 75 6e 63 74 69 6f  the auth functio
1e8a3 6e 20 72 65 74 75 72 6e 73 20 53 51 4c 49 54 45  n returns SQLITE
1e8a4 5f 44 45 4e 59 2c 0a 2a 2a 20 74 68 65 6e 20 67  _DENY,.** then g
1e8a5 65 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72  enerate an error
1e8a6 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1e8a7 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1e8a8 41 75 74 68 52 65 61 64 28 0a 20 20 50 61 72 73  AuthRead(.  Pars
1e8a9 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
1e8aa 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20    /* The parser 
1e8ab 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
1e8ac 72 20 2a 70 45 78 70 72 2c 20 20 20 20 20 20 20  r *pExpr,       
1e8ad 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72 65 73     /* The expres
1e8ae 73 69 6f 6e 20 74 6f 20 63 68 65 63 6b 20 61 75  sion to check au
1e8af 74 68 6f 72 69 7a 61 74 69 6f 6e 20 6f 6e 20 2a  thorization on *
1e8b0 2f 0a 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68  /.  Schema *pSch
1e8b1 65 6d 61 2c 20 20 20 20 20 20 2f 2a 20 54 68 65  ema,      /* The
1e8b2 20 73 63 68 65 6d 61 20 6f 66 20 74 68 65 20 65   schema of the e
1e8b3 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 53  xpression */.  S
1e8b4 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73 74  rcList *pTabList
1e8b5 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c       /* All tabl
1e8b6 65 20 74 68 61 74 20 70 45 78 70 72 20 6d 69 67  e that pExpr mig
1e8b7 68 74 20 72 65 66 65 72 20 74 6f 20 2a 2f 0a 29  ht refer to */.)
1e8b8 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  {.  sqlite3 *db 
1e8b9 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1e8ba 69 6e 74 20 72 63 3b 0a 20 20 54 61 62 6c 65 20  int rc;.  Table 
1e8bb 2a 70 54 61 62 20 3d 20 30 3b 20 20 20 20 20 20  *pTab = 0;      
1e8bc 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  /* The table bei
1e8bd 6e 67 20 72 65 61 64 20 2a 2f 0a 20 20 63 6f 6e  ng read */.  con
1e8be 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 3b 20 20  st char *zCol;  
1e8bf 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
1e8c0 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  e column of the 
1e8c1 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
1e8c2 53 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20  Src;            
1e8c3 20 2f 2a 20 49 6e 64 65 78 20 69 6e 20 70 54 61   /* Index in pTa
1e8c4 62 4c 69 73 74 2d 3e 61 5b 5d 20 6f 66 20 74 61  bList->a[] of ta
1e8c5 62 6c 65 20 62 65 69 6e 67 20 72 65 61 64 20 2a  ble being read *
1e8c6 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
1e8c7 7a 44 42 61 73 65 3b 20 20 20 2f 2a 20 4e 61 6d  zDBase;   /* Nam
1e8c8 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 62 65  e of database be
1e8c9 69 6e 67 20 61 63 63 65 73 73 65 64 20 2a 2f 0a  ing accessed */.
1e8ca 20 20 54 72 69 67 67 65 72 53 74 61 63 6b 20 2a    TriggerStack *
1e8cb 70 53 74 61 63 6b 3b 20 2f 2a 20 54 68 65 20 73  pStack; /* The s
1e8cc 74 61 63 6b 20 6f 66 20 63 75 72 72 65 6e 74 20  tack of current 
1e8cd 74 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e  triggers */.  in
1e8ce 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
1e8cf 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65 78      /* The index
1e8d0 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65   of the database
1e8d1 20 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20   the expression 
1e8d2 72 65 66 65 72 73 20 74 6f 20 2a 2f 0a 0a 20 20  refers to */..  
1e8d3 69 66 28 20 64 62 2d 3e 78 41 75 74 68 3d 3d 30  if( db->xAuth==0
1e8d4 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28   ) return;.  if(
1e8d5 20 70 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43   pExpr->op!=TK_C
1e8d6 4f 4c 55 4d 4e 20 29 20 72 65 74 75 72 6e 3b 0a  OLUMN ) return;.
1e8d7 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1e8d8 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
1e8d9 72 73 65 2d 3e 64 62 2c 20 70 53 63 68 65 6d 61  rse->db, pSchema
1e8da 29 3b 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  );.  if( iDb<0 )
1e8db 7b 0a 20 20 20 20 2f 2a 20 41 6e 20 61 74 74 65  {.    /* An atte
1e8dc 6d 70 74 20 74 6f 20 72 65 61 64 20 61 20 63 6f  mpt to read a co
1e8dd 6c 75 6d 6e 20 6f 75 74 20 6f 66 20 61 20 73 75  lumn out of a su
1e8de 62 71 75 65 72 79 20 6f 72 20 6f 74 68 65 72 0a  bquery or other.
1e8df 20 20 20 20 2a 2a 20 74 65 6d 70 6f 72 61 72 79      ** temporary
1e8e0 20 74 61 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 72   table. */.    r
1e8e1 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72  eturn;.  }.  for
1e8e2 28 69 53 72 63 3d 30 3b 20 70 54 61 62 4c 69 73  (iSrc=0; pTabLis
1e8e3 74 20 26 26 20 69 53 72 63 3c 70 54 61 62 4c 69  t && iSrc<pTabLi
1e8e4 73 74 2d 3e 6e 53 72 63 3b 20 69 53 72 63 2b 2b  st->nSrc; iSrc++
1e8e5 29 7b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72  ){.    if( pExpr
1e8e6 2d 3e 69 54 61 62 6c 65 3d 3d 70 54 61 62 4c 69  ->iTable==pTabLi
1e8e7 73 74 2d 3e 61 5b 69 53 72 63 5d 2e 69 43 75 72  st->a[iSrc].iCur
1e8e8 73 6f 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 7d  sor ) break;.  }
1e8e9 0a 20 20 69 66 28 20 69 53 72 63 3e 3d 30 20 26  .  if( iSrc>=0 &
1e8ea 26 20 70 54 61 62 4c 69 73 74 20 26 26 20 69 53  & pTabList && iS
1e8eb 72 63 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72  rc<pTabList->nSr
1e8ec 63 20 29 7b 0a 20 20 20 20 70 54 61 62 20 3d 20  c ){.    pTab = 
1e8ed 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 53 72 63  pTabList->a[iSrc
1e8ee 5d 2e 70 54 61 62 3b 0a 20 20 7d 65 6c 73 65 20  ].pTab;.  }else 
1e8ef 69 66 28 20 28 70 53 74 61 63 6b 20 3d 20 70 50  if( (pStack = pP
1e8f0 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 29  arse->trigStack)
1e8f1 21 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  !=0 ){.    /* Th
1e8f2 69 73 20 6d 75 73 74 20 62 65 20 61 6e 20 61 74  is must be an at
1e8f3 74 65 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68  tempt to read th
1e8f4 65 20 4e 45 57 20 6f 72 20 4f 4c 44 20 70 73 65  e NEW or OLD pse
1e8f5 75 64 6f 2d 74 61 62 6c 65 73 0a 20 20 20 20 2a  udo-tables.    *
1e8f6 2a 20 6f 66 20 61 20 74 72 69 67 67 65 72 2e 0a  * of a trigger..
1e8f7 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65 72      */.    asser
1e8f8 74 28 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65  t( pExpr->iTable
1e8f9 3d 3d 70 53 74 61 63 6b 2d 3e 6e 65 77 49 64 78  ==pStack->newIdx
1e8fa 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c   || pExpr->iTabl
1e8fb 65 3d 3d 70 53 74 61 63 6b 2d 3e 6f 6c 64 49 64  e==pStack->oldId
1e8fc 78 20 29 3b 0a 20 20 20 20 70 54 61 62 20 3d 20  x );.    pTab = 
1e8fd 70 53 74 61 63 6b 2d 3e 70 54 61 62 3b 0a 20 20  pStack->pTab;.  
1e8fe 7d 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20  }.  if( pTab==0 
1e8ff 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
1e900 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3e 3d  pExpr->iColumn>=
1e901 30 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  0 ){.    assert(
1e902 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3c   pExpr->iColumn<
1e903 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20 20  pTab->nCol );.  
1e904 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
1e905 43 6f 6c 5b 70 45 78 70 72 2d 3e 69 43 6f 6c 75  Col[pExpr->iColu
1e906 6d 6e 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c  mn].zName;.  }el
1e907 73 65 20 69 66 28 20 70 54 61 62 2d 3e 69 50 4b  se if( pTab->iPK
1e908 65 79 3e 3d 30 20 29 7b 0a 20 20 20 20 61 73 73  ey>=0 ){.    ass
1e909 65 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79  ert( pTab->iPKey
1e90a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a 20  <pTab->nCol );. 
1e90b 20 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e     zCol = pTab->
1e90c 61 43 6f 6c 5b 70 54 61 62 2d 3e 69 50 4b 65 79  aCol[pTab->iPKey
1e90d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 7d 65 6c 73 65  ].zName;.  }else
1e90e 7b 0a 20 20 20 20 7a 43 6f 6c 20 3d 20 22 52 4f  {.    zCol = "RO
1e90f 57 49 44 22 3b 0a 20 20 7d 0a 20 20 61 73 73 65  WID";.  }.  asse
1e910 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69 44  rt( iDb>=0 && iD
1e911 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a  b<db->nDb );.  z
1e912 44 42 61 73 65 20 3d 20 64 62 2d 3e 61 44 62 5b  DBase = db->aDb[
1e913 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63  iDb].zName;.  rc
1e914 20 3d 20 64 62 2d 3e 78 41 75 74 68 28 64 62 2d   = db->xAuth(db-
1e915 3e 70 41 75 74 68 41 72 67 2c 20 53 51 4c 49 54  >pAuthArg, SQLIT
1e916 45 5f 52 45 41 44 2c 20 70 54 61 62 2d 3e 7a 4e  E_READ, pTab->zN
1e917 61 6d 65 2c 20 7a 43 6f 6c 2c 20 7a 44 42 61 73  ame, zCol, zDBas
1e918 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  e, .            
1e919 20 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 41 75       pParse->zAu
1e91a 74 68 43 6f 6e 74 65 78 74 29 3b 0a 20 20 69 66  thContext);.  if
1e91b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 47 4e  ( rc==SQLITE_IGN
1e91c 4f 52 45 20 29 7b 0a 20 20 20 20 70 45 78 70 72  ORE ){.    pExpr
1e91d 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b 0a  ->op = TK_NULL;.
1e91e 20 20 7d 65 6c 73 65 20 69 66 28 20 72 63 3d 3d    }else if( rc==
1e91f 53 51 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20  SQLITE_DENY ){. 
1e920 20 20 20 69 66 28 20 64 62 2d 3e 6e 44 62 3e 32     if( db->nDb>2
1e921 20 7c 7c 20 69 44 62 21 3d 30 20 29 7b 0a 20 20   || iDb!=0 ){.  
1e922 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1e923 4d 73 67 28 70 50 61 72 73 65 2c 20 22 61 63 63  Msg(pParse, "acc
1e924 65 73 73 20 74 6f 20 25 73 2e 25 73 2e 25 73 20  ess to %s.%s.%s 
1e925 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 20  is prohibited", 
1e926 0a 20 20 20 20 20 20 20 20 20 7a 44 42 61 73 65  .         zDBase
1e927 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
1e928 43 6f 6c 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  Col);.    }else{
1e929 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
1e92a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
1e92b 61 63 63 65 73 73 20 74 6f 20 25 73 2e 25 73 20  access to %s.%s 
1e92c 69 73 20 70 72 6f 68 69 62 69 74 65 64 22 2c 70  is prohibited",p
1e92d 54 61 62 2d 3e 7a 4e 61 6d 65 2c 7a 43 6f 6c 29  Tab->zName,zCol)
1e92e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72  ;.    }.    pPar
1e92f 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f  se->rc = SQLITE_
1e930 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65 20 69 66  AUTH;.  }else if
1e931 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
1e932 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74  ){.    sqliteAut
1e933 68 42 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70  hBadReturnCode(p
1e934 50 61 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a  Parse, rc);.  }.
1e935 7d 0a 0a 2f 2a 0a 2a 2a 20 44 6f 20 61 6e 20 61  }../*.** Do an a
1e936 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 68 65  uthorization che
1e937 63 6b 20 75 73 69 6e 67 20 74 68 65 20 63 6f 64  ck using the cod
1e938 65 20 61 6e 64 20 61 72 67 75 6d 65 6e 74 73 20  e and arguments 
1e939 67 69 76 65 6e 2e 20 20 52 65 74 75 72 6e 0a 2a  given.  Return.*
1e93a 2a 20 65 69 74 68 65 72 20 53 51 4c 49 54 45 5f  * either SQLITE_
1e93b 4f 4b 20 28 7a 65 72 6f 29 20 6f 72 20 53 51 4c  OK (zero) or SQL
1e93c 49 54 45 5f 49 47 4e 4f 52 45 20 6f 72 20 53 51  ITE_IGNORE or SQ
1e93d 4c 49 54 45 5f 44 45 4e 59 2e 20 20 49 66 20 53  LITE_DENY.  If S
1e93e 51 4c 49 54 45 5f 44 45 4e 59 0a 2a 2a 20 69 73  QLITE_DENY.** is
1e93f 20 72 65 74 75 72 6e 65 64 2c 20 74 68 65 6e 20   returned, then 
1e940 74 68 65 20 65 72 72 6f 72 20 63 6f 75 6e 74 20  the error count 
1e941 61 6e 64 20 65 72 72 6f 72 20 6d 65 73 73 61 67  and error messag
1e942 65 20 69 6e 20 70 50 61 72 73 65 20 61 72 65 0a  e in pParse are.
1e943 2a 2a 20 6d 6f 64 69 66 69 65 64 20 61 70 70 72  ** modified appr
1e944 6f 70 72 69 61 74 65 6c 79 2e 0a 2a 2f 0a 53 51  opriately..*/.SQ
1e945 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
1e946 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1e947 6b 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  k(.  Parse *pPar
1e948 73 65 2c 0a 20 20 69 6e 74 20 63 6f 64 65 2c 0a  se,.  int code,.
1e949 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
1e94a 72 67 31 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  rg1,.  const cha
1e94b 72 20 2a 7a 41 72 67 32 2c 0a 20 20 63 6f 6e 73  r *zArg2,.  cons
1e94c 74 20 63 68 61 72 20 2a 7a 41 72 67 33 0a 29 7b  t char *zArg3.){
1e94d 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1e94e 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69   pParse->db;.  i
1e94f 6e 74 20 72 63 3b 0a 0a 20 20 2f 2a 20 44 6f 6e  nt rc;..  /* Don
1e950 27 74 20 64 6f 20 61 6e 79 20 61 75 74 68 6f 72  't do any author
1e951 69 7a 61 74 69 6f 6e 20 63 68 65 63 6b 73 20 69  ization checks i
1e952 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  f the database i
1e953 73 20 69 6e 69 74 69 61 6c 69 73 69 6e 67 0a 20  s initialising. 
1e954 20 2a 2a 20 6f 72 20 69 66 20 74 68 65 20 70 61   ** or if the pa
1e955 72 73 65 72 20 69 73 20 62 65 69 6e 67 20 69 6e  rser is being in
1e956 76 6f 6b 65 64 20 66 72 6f 6d 20 77 69 74 68 69  voked from withi
1e957 6e 20 73 71 6c 69 74 65 33 5f 64 65 63 6c 61 72  n sqlite3_declar
1e958 65 5f 76 74 61 62 2e 0a 20 20 2a 2f 0a 20 20 69  e_vtab..  */.  i
1e959 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
1e95a 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f 56   || IN_DECLARE_V
1e95b 54 41 42 20 29 7b 0a 20 20 20 20 72 65 74 75 72  TAB ){.    retur
1e95c 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
1e95d 0a 0a 20 20 69 66 28 20 64 62 2d 3e 78 41 75 74  ..  if( db->xAut
1e95e 68 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75  h==0 ){.    retu
1e95f 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
1e960 7d 0a 20 20 72 63 20 3d 20 64 62 2d 3e 78 41 75  }.  rc = db->xAu
1e961 74 68 28 64 62 2d 3e 70 41 75 74 68 41 72 67 2c  th(db->pAuthArg,
1e962 20 63 6f 64 65 2c 20 7a 41 72 67 31 2c 20 7a 41   code, zArg1, zA
1e963 72 67 32 2c 20 7a 41 72 67 33 2c 20 70 50 61 72  rg2, zArg3, pPar
1e964 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65 78 74  se->zAuthContext
1e965 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
1e966 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20 20  ITE_DENY ){.    
1e967 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1e968 70 50 61 72 73 65 2c 20 22 6e 6f 74 20 61 75 74  pParse, "not aut
1e969 68 6f 72 69 7a 65 64 22 29 3b 0a 20 20 20 20 70  horized");.    p
1e96a 50 61 72 73 65 2d 3e 72 63 20 3d 20 53 51 4c 49  Parse->rc = SQLI
1e96b 54 45 5f 41 55 54 48 3b 0a 20 20 7d 65 6c 73 65  TE_AUTH;.  }else
1e96c 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
1e96d 4f 4b 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45  OK && rc!=SQLITE
1e96e 5f 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 72  _IGNORE ){.    r
1e96f 63 20 3d 20 53 51 4c 49 54 45 5f 44 45 4e 59 3b  c = SQLITE_DENY;
1e970 0a 20 20 20 20 73 71 6c 69 74 65 41 75 74 68 42  .    sqliteAuthB
1e971 61 64 52 65 74 75 72 6e 43 6f 64 65 28 70 50 61  adReturnCode(pPa
1e972 72 73 65 2c 20 72 63 29 3b 0a 20 20 7d 0a 20 20  rse, rc);.  }.  
1e973 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
1e974 0a 2a 2a 20 50 75 73 68 20 61 6e 20 61 75 74 68  .** Push an auth
1e975 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78  orization contex
1e976 74 2e 20 20 41 66 74 65 72 20 74 68 69 73 20 72  t.  After this r
1e977 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1e978 2c 20 74 68 65 0a 2a 2a 20 7a 41 72 67 33 20 61  , the.** zArg3 a
1e979 72 67 75 6d 65 6e 74 20 74 6f 20 61 75 74 68 6f  rgument to autho
1e97a 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63  rization callbac
1e97b 6b 73 20 77 69 6c 6c 20 62 65 20 7a 43 6f 6e 74  ks will be zCont
1e97c 65 78 74 20 75 6e 74 69 6c 0a 2a 2a 20 70 6f 70  ext until.** pop
1e97d 70 65 64 2e 20 20 4f 72 20 69 66 20 70 50 61 72  ped.  Or if pPar
1e97e 73 65 3d 3d 30 2c 20 74 68 69 73 20 72 6f 75 74  se==0, this rout
1e97f 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a  ine is a no-op..
1e980 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e981 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41 75  E void sqlite3Au
1e982 74 68 43 6f 6e 74 65 78 74 50 75 73 68 28 0a 20  thContextPush(. 
1e983 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 0a   Parse *pParse,.
1e984 20 20 41 75 74 68 43 6f 6e 74 65 78 74 20 2a 70    AuthContext *p
1e985 43 6f 6e 74 65 78 74 2c 20 0a 20 20 63 6f 6e 73  Context, .  cons
1e986 74 20 63 68 61 72 20 2a 7a 43 6f 6e 74 65 78 74  t char *zContext
1e987 0a 29 7b 0a 20 20 70 43 6f 6e 74 65 78 74 2d 3e  .){.  pContext->
1e988 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b  pParse = pParse;
1e989 0a 20 20 69 66 28 20 70 50 61 72 73 65 20 29 7b  .  if( pParse ){
1e98a 0a 20 20 20 20 70 43 6f 6e 74 65 78 74 2d 3e 7a  .    pContext->z
1e98b 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 50  AuthContext = pP
1e98c 61 72 73 65 2d 3e 7a 41 75 74 68 43 6f 6e 74 65  arse->zAuthConte
1e98d 78 74 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  xt;.    pParse->
1e98e 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 7a  zAuthContext = z
1e98f 43 6f 6e 74 65 78 74 3b 0a 20 20 7d 0a 7d 0a 0a  Context;.  }.}..
1e990 2f 2a 0a 2a 2a 20 50 6f 70 20 61 6e 20 61 75 74  /*.** Pop an aut
1e991 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e 74 65  horization conte
1e992 78 74 20 74 68 61 74 20 77 61 73 20 70 72 65 76  xt that was prev
1e993 69 6f 75 73 6c 79 20 70 75 73 68 65 64 0a 2a 2a  iously pushed.**
1e994 20 62 79 20 73 71 6c 69 74 65 33 41 75 74 68 43   by sqlite3AuthC
1e995 6f 6e 74 65 78 74 50 75 73 68 0a 2a 2f 0a 53 51  ontextPush.*/.SQ
1e996 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
1e997 64 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e  d sqlite3AuthCon
1e998 74 65 78 74 50 6f 70 28 41 75 74 68 43 6f 6e 74  textPop(AuthCont
1e999 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 29 7b 0a  ext *pContext){.
1e99a 20 20 69 66 28 20 70 43 6f 6e 74 65 78 74 2d 3e    if( pContext->
1e99b 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20 70 43  pParse ){.    pC
1e99c 6f 6e 74 65 78 74 2d 3e 70 50 61 72 73 65 2d 3e  ontext->pParse->
1e99d 7a 41 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70  zAuthContext = p
1e99e 43 6f 6e 74 65 78 74 2d 3e 7a 41 75 74 68 43 6f  Context->zAuthCo
1e99f 6e 74 65 78 74 3b 0a 20 20 20 20 70 43 6f 6e 74  ntext;.    pCont
1e9a0 65 78 74 2d 3e 70 50 61 72 73 65 20 3d 20 30 3b  ext->pParse = 0;
1e9a1 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f  .  }.}..#endif /
1e9a2 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  * SQLITE_OMIT_AU
1e9a3 54 48 4f 52 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a  THORIZATION */..
1e9a4 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1e9a5 45 6e 64 20 6f 66 20 61 75 74 68 2e 63 20 2a 2a  End of auth.c **
1e9a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9a7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9a8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1e9a9 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
1e9aa 42 65 67 69 6e 20 66 69 6c 65 20 62 75 69 6c 64  Begin file build
1e9ab 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
1e9ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
1e9ae 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65  /*.** 2001 Septe
1e9af 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68  mber 15.**.** Th
1e9b0 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
1e9b1 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
1e9b2 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
1e9b3 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
1e9b4 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
1e9b5 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
1e9b6 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
1e9b7 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
1e9b8 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
1e9b9 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
1e9ba 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
1e9bb 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
1e9bc 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
1e9bd 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
1e9be 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
1e9bf 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
1e9c0 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
1e9c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
1e9c5 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
1e9c6 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ile contains C c
1e9c7 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61  ode routines tha
1e9c8 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20  t are called by 
1e9c9 74 68 65 20 53 51 4c 69 74 65 20 70 61 72 73 65  the SQLite parse
1e9ca 72 0a 2a 2a 20 77 68 65 6e 20 73 79 6e 74 61 78  r.** when syntax
1e9cb 20 72 75 6c 65 73 20 61 72 65 20 72 65 64 75 63   rules are reduc
1e9cc 65 64 2e 20 20 54 68 65 20 72 6f 75 74 69 6e 65  ed.  The routine
1e9cd 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 68  s in this file h
1e9ce 61 6e 64 6c 65 20 74 68 65 0a 2a 2a 20 66 6f 6c  andle the.** fol
1e9cf 6c 6f 77 69 6e 67 20 6b 69 6e 64 73 20 6f 66 20  lowing kinds of 
1e9d0 53 51 4c 20 73 79 6e 74 61 78 3a 0a 2a 2a 0a 2a  SQL syntax:.**.*
1e9d1 2a 20 20 20 20 20 43 52 45 41 54 45 20 54 41 42  *     CREATE TAB
1e9d2 4c 45 0a 2a 2a 20 20 20 20 20 44 52 4f 50 20 54  LE.**     DROP T
1e9d3 41 42 4c 45 0a 2a 2a 20 20 20 20 20 43 52 45 41  ABLE.**     CREA
1e9d4 54 45 20 49 4e 44 45 58 0a 2a 2a 20 20 20 20 20  TE INDEX.**     
1e9d5 44 52 4f 50 20 49 4e 44 45 58 0a 2a 2a 20 20 20  DROP INDEX.**   
1e9d6 20 20 63 72 65 61 74 69 6e 67 20 49 44 20 6c 69    creating ID li
1e9d7 73 74 73 0a 2a 2a 20 20 20 20 20 42 45 47 49 4e  sts.**     BEGIN
1e9d8 20 54 52 41 4e 53 41 43 54 49 4f 4e 0a 2a 2a 20   TRANSACTION.** 
1e9d9 20 20 20 20 43 4f 4d 4d 49 54 0a 2a 2a 20 20 20      COMMIT.**   
1e9da 20 20 52 4f 4c 4c 42 41 43 4b 0a 2a 2a 0a 2a 2a    ROLLBACK.**.**
1e9db 20 24 49 64 3a 20 62 75 69 6c 64 2e 63 2c 76 20   $Id: build.c,v 
1e9dc 31 2e 35 30 33 20 32 30 30 38 2f 31 31 2f 31 37  1.503 2008/11/17
1e9dd 20 31 39 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c   19:18:55 daniel
1e9de 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a  k1977 Exp $.*/..
1e9df 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
1e9e0 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 77 68 65  ne is called whe
1e9e1 6e 20 61 20 6e 65 77 20 53 51 4c 20 73 74 61 74  n a new SQL stat
1e9e2 65 6d 65 6e 74 20 69 73 20 62 65 67 69 6e 6e 69  ement is beginni
1e9e3 6e 67 20 74 6f 0a 2a 2a 20 62 65 20 70 61 72 73  ng to.** be pars
1e9e4 65 64 2e 20 20 49 6e 69 74 69 61 6c 69 7a 65 20  ed.  Initialize 
1e9e5 74 68 65 20 70 50 61 72 73 65 20 73 74 72 75 63  the pParse struc
1e9e6 74 75 72 65 20 61 73 20 6e 65 65 64 65 64 2e 0a  ture as needed..
1e9e7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
1e9e8 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 42 65  E void sqlite3Be
1e9e9 67 69 6e 50 61 72 73 65 28 50 61 72 73 65 20 2a  ginParse(Parse *
1e9ea 70 50 61 72 73 65 2c 20 69 6e 74 20 65 78 70 6c  pParse, int expl
1e9eb 61 69 6e 46 6c 61 67 29 7b 0a 20 20 70 50 61 72  ainFlag){.  pPar
1e9ec 73 65 2d 3e 65 78 70 6c 61 69 6e 20 3d 20 65 78  se->explain = ex
1e9ed 70 6c 61 69 6e 46 6c 61 67 3b 0a 20 20 70 50 61  plainFlag;.  pPa
1e9ee 72 73 65 2d 3e 6e 56 61 72 20 3d 20 30 3b 0a 7d  rse->nVar = 0;.}
1e9ef 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
1e9f0 5f 4f 4d 49 54 5f 53 48 41 52 45 44 5f 43 41 43  _OMIT_SHARED_CAC
1e9f1 48 45 0a 2f 2a 0a 2a 2a 20 54 68 65 20 54 61 62  HE./*.** The Tab
1e9f2 6c 65 4c 6f 63 6b 20 73 74 72 75 63 74 75 72 65  leLock structure
1e9f3 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 62 79   is only used by
1e9f4 20 74 68 65 20 73 71 6c 69 74 65 33 54 61 62 6c   the sqlite3Tabl
1e9f5 65 4c 6f 63 6b 28 29 20 61 6e 64 0a 2a 2a 20 63  eLock() and.** c
1e9f6 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 29 20  odeTableLocks() 
1e9f7 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74  functions..*/.st
1e9f8 72 75 63 74 20 54 61 62 6c 65 4c 6f 63 6b 20 7b  ruct TableLock {
1e9f9 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20  .  int iDb;     
1e9fa 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
1e9fb 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69  atabase containi
1e9fc 6e 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  ng the table to 
1e9fd 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 69  be locked */.  i
1e9fe 6e 74 20 69 54 61 62 3b 20 20 20 20 20 20 20 20  nt iTab;        
1e9ff 20 20 20 20 2f 2a 20 54 68 65 20 72 6f 6f 74 20      /* The root 
1ea00 70 61 67 65 20 6f 66 20 74 68 65 20 74 61 62 6c  page of the tabl
1ea01 65 20 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 2a  e to be locked *
1ea02 2f 0a 20 20 75 38 20 69 73 57 72 69 74 65 4c 6f  /.  u8 isWriteLo
1ea03 63 6b 3b 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ck;      /* True
1ea04 20 66 6f 72 20 77 72 69 74 65 20 6c 6f 63 6b 2e   for write lock.
1ea05 20 20 46 61 6c 73 65 20 66 6f 72 20 61 20 72 65    False for a re
1ea06 61 64 20 6c 6f 63 6b 20 2a 2f 0a 20 20 63 6f 6e  ad lock */.  con
1ea07 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20  st char *zName; 
1ea08 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
1ea09 20 74 61 62 6c 65 20 2a 2f 0a 7d 3b 0a 0a 2f 2a   table */.};../*
1ea0a 0a 2a 2a 20 52 65 63 6f 72 64 20 74 68 65 20 66  .** Record the f
1ea0b 61 63 74 20 74 68 61 74 20 77 65 20 77 61 6e 74  act that we want
1ea0c 20 74 6f 20 6c 6f 63 6b 20 61 20 74 61 62 6c 65   to lock a table
1ea0d 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 0a   at run-time.  .
1ea0e 2a 2a 0a 2a 2a 20 54 68 65 20 74 61 62 6c 65 20  **.** The table 
1ea0f 74 6f 20 62 65 20 6c 6f 63 6b 65 64 20 68 61 73  to be locked has
1ea10 20 72 6f 6f 74 20 70 61 67 65 20 69 54 61 62 20   root page iTab 
1ea11 61 6e 64 20 69 73 20 66 6f 75 6e 64 20 69 6e 20  and is found in 
1ea12 64 61 74 61 62 61 73 65 20 69 44 62 2e 0a 2a 2a  database iDb..**
1ea13 20 41 20 72 65 61 64 20 6f 72 20 61 20 77 72 69   A read or a wri
1ea14 74 65 20 6c 6f 63 6b 20 63 61 6e 20 62 65 20 74  te lock can be t
1ea15 61 6b 65 6e 20 64 65 70 65 6e 64 69 6e 67 20 6f  aken depending o
1ea16 6e 20 69 73 57 72 69 74 65 6c 6f 63 6b 2e 0a 2a  n isWritelock..*
1ea17 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
1ea18 65 20 6a 75 73 74 20 72 65 63 6f 72 64 73 20 74  e just records t
1ea19 68 65 20 66 61 63 74 20 74 68 61 74 20 74 68 65  he fact that the
1ea1a 20 6c 6f 63 6b 20 69 73 20 64 65 73 69 72 65 64   lock is desired
1ea1b 2e 20 20 54 68 65 0a 2a 2a 20 63 6f 64 65 20 74  .  The.** code t
1ea1c 6f 20 6d 61 6b 65 20 74 68 65 20 6c 6f 63 6b 20  o make the lock 
1ea1d 6f 63 63 75 72 20 69 73 20 67 65 6e 65 72 61 74  occur is generat
1ea1e 65 64 20 62 79 20 61 20 6c 61 74 65 72 20 63 61  ed by a later ca
1ea1f 6c 6c 20 74 6f 0a 2a 2a 20 63 6f 64 65 54 61 62  ll to.** codeTab
1ea20 6c 65 4c 6f 63 6b 73 28 29 20 77 68 69 63 68 20  leLocks() which 
1ea21 6f 63 63 75 72 73 20 64 75 72 69 6e 67 20 73 71  occurs during sq
1ea22 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69 6e  lite3FinishCodin
1ea23 67 28 29 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  g()..*/.SQLITE_P
1ea24 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1ea25 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 0a 20 20  te3TableLock(.  
1ea26 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
1ea27 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f     /* Parsing co
1ea28 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 69  ntext */.  int i
1ea29 44 62 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  Db,           /*
1ea2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 64 61   Index of the da
1ea2b 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
1ea2c 67 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20 6c  g the table to l
1ea2d 6f 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  ock */.  int iTa
1ea2e 62 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52  b,          /* R
1ea2f 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65 72 20  oot page number 
1ea30 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
1ea31 62 65 20 6c 6f 63 6b 65 64 20 2a 2f 0a 20 20 75  be locked */.  u
1ea32 38 20 69 73 57 72 69 74 65 4c 6f 63 6b 2c 20 20  8 isWriteLock,  
1ea33 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 61 20    /* True for a 
1ea34 77 72 69 74 65 20 6c 6f 63 6b 20 2a 2f 0a 20 20  write lock */.  
1ea35 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1ea36 65 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68  e  /* Name of th
1ea37 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6c 6f  e table to be lo
1ea38 63 6b 65 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  cked */.){.  int
1ea39 20 69 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   i;.  int nBytes
1ea3a 3b 0a 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70  ;.  TableLock *p
1ea3b 3b 0a 0a 20 20 69 66 28 20 69 44 62 3c 30 20 29  ;..  if( iDb<0 )
1ea3c 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  {.    return;.  
1ea3d 7d 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  }..  for(i=0; i<
1ea3e 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
1ea3f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 70 20  ck; i++){.    p 
1ea40 3d 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c  = &pParse->aTabl
1ea41 65 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 66  eLock[i];.    if
1ea42 28 20 70 2d 3e 69 44 62 3d 3d 69 44 62 20 26 26  ( p->iDb==iDb &&
1ea43 20 70 2d 3e 69 54 61 62 3d 3d 69 54 61 62 20 29   p->iTab==iTab )
1ea44 7b 0a 20 20 20 20 20 20 70 2d 3e 69 73 57 72 69  {.      p->isWri
1ea45 74 65 4c 6f 63 6b 20 3d 20 28 70 2d 3e 69 73 57  teLock = (p->isW
1ea46 72 69 74 65 4c 6f 63 6b 20 7c 7c 20 69 73 57 72  riteLock || isWr
1ea47 69 74 65 4c 6f 63 6b 29 3b 0a 20 20 20 20 20 20  iteLock);.      
1ea48 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20  return;.    }.  
1ea49 7d 0a 0a 20 20 6e 42 79 74 65 73 20 3d 20 73 69  }..  nBytes = si
1ea4a 7a 65 6f 66 28 54 61 62 6c 65 4c 6f 63 6b 29 20  zeof(TableLock) 
1ea4b 2a 20 28 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c  * (pParse->nTabl
1ea4c 65 4c 6f 63 6b 2b 31 29 3b 0a 20 20 70 50 61 72  eLock+1);.  pPar
1ea4d 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d  se->aTableLock =
1ea4e 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44   .      sqlite3D
1ea4f 62 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70  bReallocOrFree(p
1ea50 50 61 72 73 65 2d 3e 64 62 2c 20 70 50 61 72 73  Parse->db, pPars
1ea51 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 2c 20 6e  e->aTableLock, n
1ea52 42 79 74 65 73 29 3b 0a 20 20 69 66 28 20 70 50  Bytes);.  if( pP
1ea53 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b  arse->aTableLock
1ea54 20 29 7b 0a 20 20 20 20 70 20 3d 20 26 70 50 61   ){.    p = &pPa
1ea55 72 73 65 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 5b  rse->aTableLock[
1ea56 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65 4c 6f  pParse->nTableLo
1ea57 63 6b 2b 2b 5d 3b 0a 20 20 20 20 70 2d 3e 69 44  ck++];.    p->iD
1ea58 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 70 2d 3e  b = iDb;.    p->
1ea59 69 54 61 62 20 3d 20 69 54 61 62 3b 0a 20 20 20  iTab = iTab;.   
1ea5a 20 70 2d 3e 69 73 57 72 69 74 65 4c 6f 63 6b 20   p->isWriteLock 
1ea5b 3d 20 69 73 57 72 69 74 65 4c 6f 63 6b 3b 0a 20  = isWriteLock;. 
1ea5c 20 20 20 70 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e     p->zName = zN
1ea5d 61 6d 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ame;.  }else{.  
1ea5e 20 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 6c 65    pParse->nTable
1ea5f 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20 20 20 70 50  Lock = 0;.    pP
1ea60 61 72 73 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63  arse->db->malloc
1ea61 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 0a  Failed = 1;.  }.
1ea62 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 64 65 20 61 6e  }../*.** Code an
1ea63 20 4f 50 5f 54 61 62 6c 65 4c 6f 63 6b 20 69 6e   OP_TableLock in
1ea64 73 74 72 75 63 74 69 6f 6e 20 66 6f 72 20 65 61  struction for ea
1ea65 63 68 20 74 61 62 6c 65 20 6c 6f 63 6b 65 64 20  ch table locked 
1ea66 62 79 20 74 68 65 0a 2a 2a 20 73 74 61 74 65 6d  by the.** statem
1ea67 65 6e 74 20 28 63 6f 6e 66 69 67 75 72 65 64 20  ent (configured 
1ea68 62 79 20 63 61 6c 6c 73 20 74 6f 20 73 71 6c 69  by calls to sqli
1ea69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 29 29 2e  te3TableLock()).
1ea6a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1ea6b 63 6f 64 65 54 61 62 6c 65 4c 6f 63 6b 73 28 50  codeTableLocks(P
1ea6c 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
1ea6d 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
1ea6e 70 56 64 62 65 3b 20 0a 0a 20 20 69 66 28 20 30  pVdbe; ..  if( 0
1ea6f 3d 3d 28 70 56 64 62 65 20 3d 20 73 71 6c 69 74  ==(pVdbe = sqlit
1ea70 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1ea71 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  )) ){.    return
1ea72 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30  ;.  }..  for(i=0
1ea73 3b 20 69 3c 70 50 61 72 73 65 2d 3e 6e 54 61 62  ; i<pParse->nTab
1ea74 6c 65 4c 6f 63 6b 3b 20 69 2b 2b 29 7b 0a 20 20  leLock; i++){.  
1ea75 20 20 54 61 62 6c 65 4c 6f 63 6b 20 2a 70 20 3d    TableLock *p =
1ea76 20 26 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65   &pParse->aTable
1ea77 4c 6f 63 6b 5b 69 5d 3b 0a 20 20 20 20 69 6e 74  Lock[i];.    int
1ea78 20 70 31 20 3d 20 70 2d 3e 69 44 62 3b 0a 20 20   p1 = p->iDb;.  
1ea79 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1ea7a 4f 70 34 28 70 56 64 62 65 2c 20 4f 50 5f 54 61  Op4(pVdbe, OP_Ta
1ea7b 62 6c 65 4c 6f 63 6b 2c 20 70 31 2c 20 70 2d 3e  bleLock, p1, p->
1ea7c 69 54 61 62 2c 20 70 2d 3e 69 73 57 72 69 74 65  iTab, p->isWrite
1ea7d 4c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20  Lock,.          
1ea7e 20 20 20 20 20 20 20 20 20 20 20 20 70 2d 3e 7a              p->z
1ea7f 4e 61 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29  Name, P4_STATIC)
1ea80 3b 0a 20 20 7d 0a 7d 0a 23 65 6c 73 65 0a 20 20  ;.  }.}.#else.  
1ea81 23 64 65 66 69 6e 65 20 63 6f 64 65 54 61 62 6c  #define codeTabl
1ea82 65 4c 6f 63 6b 73 28 78 29 0a 23 65 6e 64 69 66  eLocks(x).#endif
1ea83 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1ea84 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 61  tine is called a
1ea85 66 74 65 72 20 61 20 73 69 6e 67 6c 65 20 53 51  fter a single SQ
1ea86 4c 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  L statement has 
1ea87 62 65 65 6e 0a 2a 2a 20 70 61 72 73 65 64 20 61  been.** parsed a
1ea88 6e 64 20 61 20 56 44 42 45 20 70 72 6f 67 72 61  nd a VDBE progra
1ea89 6d 20 74 6f 20 65 78 65 63 75 74 65 20 74 68 61  m to execute tha
1ea8a 74 20 73 74 61 74 65 6d 65 6e 74 20 68 61 73 20  t statement has 
1ea8b 62 65 65 6e 0a 2a 2a 20 70 72 65 70 61 72 65 64  been.** prepared
1ea8c 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  .  This routine 
1ea8d 70 75 74 73 20 74 68 65 20 66 69 6e 69 73 68 69  puts the finishi
1ea8e 6e 67 20 74 6f 75 63 68 65 73 20 6f 6e 20 74 68  ng touches on th
1ea8f 65 0a 2a 2a 20 56 44 42 45 20 70 72 6f 67 72 61  e.** VDBE progra
1ea90 6d 20 61 6e 64 20 72 65 73 65 74 73 20 74 68 65  m and resets the
1ea91 20 70 50 61 72 73 65 20 73 74 72 75 63 74 75 72   pParse structur
1ea92 65 20 66 6f 72 20 74 68 65 20 6e 65 78 74 0a 2a  e for the next.*
1ea93 2a 20 70 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e  * parse..**.** N
1ea94 6f 74 65 20 74 68 61 74 20 69 66 20 61 6e 20 65  ote that if an e
1ea95 72 72 6f 72 20 6f 63 63 75 72 72 65 64 2c 20 69  rror occurred, i
1ea96 74 20 6d 69 67 68 74 20 62 65 20 74 68 65 20 63  t might be the c
1ea97 61 73 65 20 74 68 61 74 0a 2a 2a 20 6e 6f 20 56  ase that.** no V
1ea98 44 42 45 20 63 6f 64 65 20 77 61 73 20 67 65 6e  DBE code was gen
1ea99 65 72 61 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54  erated..*/.SQLIT
1ea9a 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
1ea9b 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64 69  qlite3FinishCodi
1ea9c 6e 67 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ng(Parse *pParse
1ea9d 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
1ea9e 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20  ;.  Vdbe *v;..  
1ea9f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1eaa0 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
1eaa1 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
1eaa2 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  ;.  if( pParse->
1eaa3 6e 65 73 74 65 64 20 29 20 72 65 74 75 72 6e 3b  nested ) return;
1eaa4 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
1eaa5 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  Err ) return;.. 
1eaa6 20 2f 2a 20 42 65 67 69 6e 20 62 79 20 67 65 6e   /* Begin by gen
1eaa7 65 72 61 74 69 6e 67 20 73 6f 6d 65 20 74 65 72  erating some ter
1eaa8 6d 69 6e 61 74 69 6f 6e 20 63 6f 64 65 20 61 74  mination code at
1eaa9 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 0a   the end of the.
1eaaa 20 20 2a 2a 20 76 64 62 65 20 70 72 6f 67 72 61    ** vdbe progra
1eaab 6d 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  m.  */.  v = sql
1eaac 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
1eaad 73 65 29 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a  se);.  if( v ){.
1eaae 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1eaaf 64 64 4f 70 30 28 76 2c 20 4f 50 5f 48 61 6c 74  ddOp0(v, OP_Halt
1eab0 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 63  );..    /* The c
1eab1 6f 6f 6b 69 65 20 6d 61 73 6b 20 63 6f 6e 74 61  ookie mask conta
1eab2 69 6e 73 20 6f 6e 65 20 62 69 74 20 66 6f 72 20  ins one bit for 
1eab3 65 61 63 68 20 64 61 74 61 62 61 73 65 20 66 69  each database fi
1eab4 6c 65 20 6f 70 65 6e 2e 0a 20 20 20 20 2a 2a 20  le open..    ** 
1eab5 28 42 69 74 20 30 20 69 73 20 66 6f 72 20 6d 61  (Bit 0 is for ma
1eab6 69 6e 2c 20 62 69 74 20 31 20 69 73 20 66 6f 72  in, bit 1 is for
1eab7 20 74 65 6d 70 2c 20 61 6e 64 20 73 6f 20 66 6f   temp, and so fo
1eab8 72 74 68 2e 29 20 20 42 69 74 73 20 61 72 65 0a  rth.)  Bits are.
1eab9 20 20 20 20 2a 2a 20 73 65 74 20 66 6f 72 20 65      ** set for e
1eaba 61 63 68 20 64 61 74 61 62 61 73 65 20 74 68 61  ach database tha
1eabb 74 20 69 73 20 75 73 65 64 2e 20 20 47 65 6e 65  t is used.  Gene
1eabc 72 61 74 65 20 63 6f 64 65 20 74 6f 20 73 74 61  rate code to sta
1eabd 72 74 20 61 0a 20 20 20 20 2a 2a 20 74 72 61 6e  rt a.    ** tran
1eabe 73 61 63 74 69 6f 6e 20 6f 6e 20 65 61 63 68 20  saction on each 
1eabf 75 73 65 64 20 64 61 74 61 62 61 73 65 20 61 6e  used database an
1eac0 64 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 20  d to verify the 
1eac1 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 0a 20 20  schema cookie.  
1eac2 20 20 2a 2a 20 6f 6e 20 65 61 63 68 20 75 73 65    ** on each use
1eac3 64 20 64 61 74 61 62 61 73 65 2e 0a 20 20 20 20  d database..    
1eac4 2a 2f 0a 20 20 20 20 69 66 28 20 70 50 61 72 73  */.    if( pPars
1eac5 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 3e 30 20  e->cookieGoto>0 
1eac6 29 7b 0a 20 20 20 20 20 20 75 33 32 20 6d 61 73  ){.      u32 mas
1eac7 6b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 44 62  k;.      int iDb
1eac8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1eac9 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 70  dbeJumpHere(v, p
1eaca 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
1eacb 6f 2d 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28  o-1);.      for(
1eacc 69 44 62 3d 30 2c 20 6d 61 73 6b 3d 31 3b 20 69  iDb=0, mask=1; i
1eacd 44 62 3c 64 62 2d 3e 6e 44 62 3b 20 6d 61 73 6b  Db<db->nDb; mask
1eace 3c 3c 3d 31 2c 20 69 44 62 2b 2b 29 7b 0a 20 20  <<=1, iDb++){.  
1eacf 20 20 20 20 20 20 69 66 28 20 28 6d 61 73 6b 20        if( (mask 
1ead0 26 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65  & pParse->cookie
1ead1 4d 61 73 6b 29 3d 3d 30 20 29 20 63 6f 6e 74 69  Mask)==0 ) conti
1ead2 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  nue;.        sql
1ead3 69 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65  ite3VdbeUsesBtre
1ead4 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  e(v, iDb);.     
1ead5 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1ead6 64 4f 70 32 28 76 2c 4f 50 5f 54 72 61 6e 73 61  dOp2(v,OP_Transa
1ead7 63 74 69 6f 6e 2c 20 69 44 62 2c 20 28 6d 61 73  ction, iDb, (mas
1ead8 6b 20 26 20 70 50 61 72 73 65 2d 3e 77 72 69 74  k & pParse->writ
1ead9 65 4d 61 73 6b 29 21 3d 30 29 3b 0a 20 20 20 20  eMask)!=0);.    
1eada 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1eadb 64 64 4f 70 32 28 76 2c 4f 50 5f 56 65 72 69 66  ddOp2(v,OP_Verif
1eadc 79 43 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 70 50  yCookie, iDb, pP
1eadd 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 56 61 6c 75  arse->cookieValu
1eade 65 5b 69 44 62 5d 29 3b 0a 20 20 20 20 20 20 7d  e[iDb]);.      }
1eadf 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1eae0 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1eae1 45 0a 20 20 20 20 20 20 7b 0a 20 20 20 20 20 20  E.      {.      
1eae2 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20    int i;.       
1eae3 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 50 61 72   for(i=0; i<pPar
1eae4 73 65 2d 3e 6e 56 74 61 62 4c 6f 63 6b 3b 20 69  se->nVtabLock; i
1eae5 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63  ++){.          c
1eae6 68 61 72 20 2a 76 74 61 62 20 3d 20 28 63 68 61  har *vtab = (cha
1eae7 72 20 2a 29 70 50 61 72 73 65 2d 3e 61 70 56 74  r *)pParse->apVt
1eae8 61 62 4c 6f 63 6b 5b 69 5d 2d 3e 70 56 74 61 62  abLock[i]->pVtab
1eae9 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
1eaea 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
1eaeb 20 4f 50 5f 56 42 65 67 69 6e 2c 20 30 2c 20 30   OP_VBegin, 0, 0
1eaec 2c 20 30 2c 20 76 74 61 62 2c 20 50 34 5f 56 54  , 0, vtab, P4_VT
1eaed 41 42 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  AB);.        }. 
1eaee 20 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 6e         pParse->n
1eaef 56 74 61 62 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  VtabLock = 0;.  
1eaf0 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20      }.#endif..  
1eaf1 20 20 20 20 2f 2a 20 4f 6e 63 65 20 61 6c 6c 20      /* Once all 
1eaf2 74 68 65 20 63 6f 6f 6b 69 65 73 20 68 61 76 65  the cookies have
1eaf3 20 62 65 65 6e 20 76 65 72 69 66 69 65 64 20 61   been verified a
1eaf4 6e 64 20 74 72 61 6e 73 61 63 74 69 6f 6e 73 20  nd transactions 
1eaf5 6f 70 65 6e 65 64 2c 20 0a 20 20 20 20 20 20 2a  opened, .      *
1eaf6 2a 20 6f 62 74 61 69 6e 20 74 68 65 20 72 65 71  * obtain the req
1eaf7 75 69 72 65 64 20 74 61 62 6c 65 2d 6c 6f 63 6b  uired table-lock
1eaf8 73 2e 20 54 68 69 73 20 69 73 20 61 20 6e 6f 2d  s. This is a no-
1eaf9 6f 70 20 75 6e 6c 65 73 73 20 74 68 65 20 0a 20  op unless the . 
1eafa 20 20 20 20 20 2a 2a 20 73 68 61 72 65 64 2d 63       ** shared-c
1eafb 61 63 68 65 20 66 65 61 74 75 72 65 20 69 73 20  ache feature is 
1eafc 65 6e 61 62 6c 65 64 2e 0a 20 20 20 20 20 20 2a  enabled..      *
1eafd 2f 0a 20 20 20 20 20 20 63 6f 64 65 54 61 62 6c  /.      codeTabl
1eafe 65 4c 6f 63 6b 73 28 70 50 61 72 73 65 29 3b 0a  eLocks(pParse);.
1eaff 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
1eb00 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
1eb01 74 6f 2c 20 30 2c 20 70 50 61 72 73 65 2d 3e 63  to, 0, pParse->c
1eb02 6f 6f 6b 69 65 47 6f 74 6f 29 3b 0a 20 20 20 20  ookieGoto);.    
1eb03 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
1eb04 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 20 20 20  E_OMIT_TRACE.   
1eb05 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62   if( !db->init.b
1eb06 75 73 79 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  usy ){.      /* 
1eb07 43 68 61 6e 67 65 20 74 68 65 20 50 34 20 61 72  Change the P4 ar
1eb08 67 75 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 69  gument of the fi
1eb09 72 73 74 20 6f 70 63 6f 64 65 20 28 77 68 69 63  rst opcode (whic
1eb0a 68 20 77 69 6c 6c 20 61 6c 77 61 79 73 20 62 65  h will always be
1eb0b 0a 20 20 20 20 20 20 2a 2a 20 61 6e 20 4f 50 5f  .      ** an OP_
1eb0c 54 72 61 63 65 29 20 74 6f 20 62 65 20 74 68 65  Trace) to be the
1eb0d 20 63 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f   complete text o
1eb0e 66 20 74 68 65 20 63 75 72 72 65 6e 74 20 53 51  f the current SQ
1eb0f 4c 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20  L statement..   
1eb10 20 20 20 2a 2f 0a 20 20 20 20 20 20 56 64 62 65     */.      Vdbe
1eb11 4f 70 20 2a 70 4f 70 20 3d 20 73 71 6c 69 74 65  Op *pOp = sqlite
1eb12 33 56 64 62 65 47 65 74 4f 70 28 76 2c 20 30 29  3VdbeGetOp(v, 0)
1eb13 3b 0a 20 20 20 20 20 20 69 66 28 20 70 4f 70 20  ;.      if( pOp 
1eb14 26 26 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  && pOp->opcode==
1eb15 4f 50 5f 54 72 61 63 65 20 29 7b 0a 20 20 20 20  OP_Trace ){.    
1eb16 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
1eb17 68 61 6e 67 65 50 34 28 76 2c 20 30 2c 20 70 50  hangeP4(v, 0, pP
1eb18 61 72 73 65 2d 3e 7a 53 71 6c 2c 20 70 50 61 72  arse->zSql, pPar
1eb19 73 65 2d 3e 7a 54 61 69 6c 2d 70 50 61 72 73 65  se->zTail-pParse
1eb1a 2d 3e 7a 53 71 6c 29 3b 0a 20 20 20 20 20 20 7d  ->zSql);.      }
1eb1b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
1eb1c 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
1eb1d 43 45 20 2a 2f 0a 20 20 7d 0a 0a 0a 20 20 2f 2a  CE */.  }...  /*
1eb1e 20 47 65 74 20 74 68 65 20 56 44 42 45 20 70 72   Get the VDBE pr
1eb1f 6f 67 72 61 6d 20 72 65 61 64 79 20 66 6f 72 20  ogram ready for 
1eb20 65 78 65 63 75 74 69 6f 6e 0a 20 20 2a 2f 0a 20  execution.  */. 
1eb21 20 69 66 28 20 76 20 26 26 20 70 50 61 72 73 65   if( v && pParse
1eb22 2d 3e 6e 45 72 72 3d 3d 30 20 26 26 20 21 64 62  ->nErr==0 && !db
1eb23 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
1eb24 7b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  {.#ifdef SQLITE_
1eb25 44 45 42 55 47 0a 20 20 20 20 46 49 4c 45 20 2a  DEBUG.    FILE *
1eb26 74 72 61 63 65 20 3d 20 28 64 62 2d 3e 66 6c 61  trace = (db->fla
1eb27 67 73 20 26 20 53 51 4c 49 54 45 5f 56 64 62 65  gs & SQLITE_Vdbe
1eb28 54 72 61 63 65 29 21 3d 30 20 3f 20 73 74 64 6f  Trace)!=0 ? stdo
1eb29 75 74 20 3a 20 30 3b 0a 20 20 20 20 73 71 6c 69  ut : 0;.    sqli
1eb2a 74 65 33 56 64 62 65 54 72 61 63 65 28 76 2c 20  te3VdbeTrace(v, 
1eb2b 74 72 61 63 65 29 3b 0a 23 65 6e 64 69 66 0a 20  trace);.#endif. 
1eb2c 20 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73     assert( pPars
1eb2d 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
1eb2e 68 65 3d 3d 30 20 29 3b 20 20 2f 2a 20 44 69 73  he==0 );  /* Dis
1eb2f 61 62 6c 65 73 20 61 6e 64 20 72 65 2d 65 6e 61  ables and re-ena
1eb30 62 6c 65 73 20 6d 61 74 63 68 20 2a 2f 0a 20 20  bles match */.  
1eb31 20 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b    sqlite3VdbeMak
1eb32 65 52 65 61 64 79 28 76 2c 20 70 50 61 72 73 65  eReady(v, pParse
1eb33 2d 3e 6e 56 61 72 2c 20 70 50 61 72 73 65 2d 3e  ->nVar, pParse->
1eb34 6e 4d 65 6d 2b 33 2c 0a 20 20 20 20 20 20 20 20  nMem+3,.        
1eb35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1eb36 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 33 2c   pParse->nTab+3,
1eb37 20 70 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e   pParse->explain
1eb38 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  );.    pParse->r
1eb39 63 20 3d 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b  c = SQLITE_DONE;
1eb3a 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c  .    pParse->col
1eb3b 4e 61 6d 65 73 53 65 74 20 3d 20 30 3b 0a 20 20  NamesSet = 0;.  
1eb3c 7d 65 6c 73 65 20 69 66 28 20 70 50 61 72 73 65  }else if( pParse
1eb3d 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ->rc==SQLITE_OK 
1eb3e 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72  ){.    pParse->r
1eb3f 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  c = SQLITE_ERROR
1eb40 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e  ;.  }.  pParse->
1eb41 6e 54 61 62 20 3d 20 30 3b 0a 20 20 70 50 61 72  nTab = 0;.  pPar
1eb42 73 65 2d 3e 6e 4d 65 6d 20 3d 20 30 3b 0a 20 20  se->nMem = 0;.  
1eb43 70 50 61 72 73 65 2d 3e 6e 53 65 74 20 3d 20 30  pParse->nSet = 0
1eb44 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e 56 61 72  ;.  pParse->nVar
1eb45 20 3d 20 30 3b 0a 20 20 70 50 61 72 73 65 2d 3e   = 0;.  pParse->
1eb46 63 6f 6f 6b 69 65 4d 61 73 6b 20 3d 20 30 3b 0a  cookieMask = 0;.
1eb47 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65    pParse->cookie
1eb48 47 6f 74 6f 20 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a  Goto = 0;.}../*.
1eb49 2a 2a 20 52 75 6e 20 74 68 65 20 70 61 72 73 65  ** Run the parse
1eb4a 72 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  r and code gener
1eb4b 61 74 6f 72 20 72 65 63 75 72 73 69 76 65 6c 79  ator recursively
1eb4c 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65 6e   in order to gen
1eb4d 65 72 61 74 65 0a 2a 2a 20 63 6f 64 65 20 66 6f  erate.** code fo
1eb4e 72 20 74 68 65 20 53 51 4c 20 73 74 61 74 65 6d  r the SQL statem
1eb4f 65 6e 74 20 67 69 76 65 6e 20 6f 6e 74 6f 20 74  ent given onto t
1eb50 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 70 50  he end of the pP
1eb51 61 72 73 65 20 63 6f 6e 74 65 78 74 0a 2a 2a 20  arse context.** 
1eb52 63 75 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20  currently under 
1eb53 63 6f 6e 73 74 72 75 63 74 69 6f 6e 2e 20 20 57  construction.  W
1eb54 68 65 6e 20 74 68 65 20 70 61 72 73 65 72 20 69  hen the parser i
1eb55 73 20 72 75 6e 20 72 65 63 75 72 73 69 76 65 6c  s run recursivel
1eb56 79 0a 2a 2a 20 74 68 69 73 20 77 61 79 2c 20 74  y.** this way, t
1eb57 68 65 20 66 69 6e 61 6c 20 4f 50 5f 48 61 6c 74  he final OP_Halt
1eb58 20 69 73 20 6e 6f 74 20 61 70 70 65 6e 64 65 64   is not appended
1eb59 20 61 6e 64 20 6f 74 68 65 72 20 69 6e 69 74 69   and other initi
1eb5a 61 6c 69 7a 61 74 69 6f 6e 0a 2a 2a 20 61 6e 64  alization.** and
1eb5b 20 66 69 6e 61 6c 69 7a 61 74 69 6f 6e 20 73 74   finalization st
1eb5c 65 70 73 20 61 72 65 20 6f 6d 69 74 74 65 64 20  eps are omitted 
1eb5d 62 65 63 61 75 73 65 20 74 68 6f 73 65 20 61 72  because those ar
1eb5e 65 20 68 61 6e 64 6c 69 6e 67 20 62 79 20 74 68  e handling by th
1eb5f 65 0a 2a 2a 20 6f 75 74 65 72 6d 6f 73 74 20 70  e.** outermost p
1eb60 61 72 73 65 72 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74  arser..**.** Not
1eb61 20 65 76 65 72 79 74 68 69 6e 67 20 69 73 20 6e   everything is n
1eb62 65 73 74 61 62 6c 65 2e 20 20 54 68 69 73 20 66  estable.  This f
1eb63 61 63 69 6c 69 74 79 20 69 73 20 64 65 73 69 67  acility is desig
1eb64 6e 65 64 20 74 6f 20 70 65 72 6d 69 74 0a 2a 2a  ned to permit.**
1eb65 20 49 4e 53 45 52 54 2c 20 55 50 44 41 54 45 2c   INSERT, UPDATE,
1eb66 20 61 6e 64 20 44 45 4c 45 54 45 20 6f 70 65 72   and DELETE oper
1eb67 61 74 69 6f 6e 73 20 61 67 61 69 6e 73 74 20 53  ations against S
1eb68 51 4c 49 54 45 5f 4d 41 53 54 45 52 2e 20 20 55  QLITE_MASTER.  U
1eb69 73 65 0a 2a 2a 20 63 61 72 65 20 69 66 20 79 6f  se.** care if yo
1eb6a 75 20 64 65 63 69 64 65 20 74 6f 20 74 72 79 20  u decide to try 
1eb6b 74 6f 20 75 73 65 20 74 68 69 73 20 72 6f 75 74  to use this rout
1eb6c 69 6e 65 20 66 6f 72 20 73 6f 6d 65 20 6f 74 68  ine for some oth
1eb6d 65 72 20 70 75 72 70 6f 73 65 73 2e 0a 2a 2f 0a  er purposes..*/.
1eb6e 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1eb6f 6f 69 64 20 73 71 6c 69 74 65 33 4e 65 73 74 65  oid sqlite3Neste
1eb70 64 50 61 72 73 65 28 50 61 72 73 65 20 2a 70 50  dParse(Parse *pP
1eb71 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61 72  arse, const char
1eb72 20 2a 7a 46 6f 72 6d 61 74 2c 20 2e 2e 2e 29 7b   *zFormat, ...){
1eb73 0a 20 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20  .  va_list ap;. 
1eb74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 63   char *zSql;.  c
1eb75 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
1eb76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
1eb77 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 23 20  = pParse->db;.# 
1eb78 64 65 66 69 6e 65 20 53 41 56 45 5f 53 5a 20 20  define SAVE_SZ  
1eb79 28 73 69 7a 65 6f 66 28 50 61 72 73 65 29 20 2d  (sizeof(Parse) -
1eb7a 20 6f 66 66 73 65 74 6f 66 28 50 61 72 73 65 2c   offsetof(Parse,
1eb7b 6e 56 61 72 29 29 0a 20 20 63 68 61 72 20 73 61  nVar)).  char sa
1eb7c 76 65 42 75 66 5b 53 41 56 45 5f 53 5a 5d 3b 0a  veBuf[SAVE_SZ];.
1eb7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
1eb7e 45 72 72 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  Err ) return;.  
1eb7f 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
1eb80 6e 65 73 74 65 64 3c 31 30 20 29 3b 20 20 2f 2a  nested<10 );  /*
1eb81 20 4e 65 73 74 69 6e 67 20 73 68 6f 75 6c 64 20   Nesting should 
1eb82 6f 6e 6c 79 20 62 65 20 6f 66 20 6c 69 6d 69 74  only be of limit
1eb83 65 64 20 64 65 70 74 68 20 2a 2f 0a 20 20 76 61  ed depth */.  va
1eb84 5f 73 74 61 72 74 28 61 70 2c 20 7a 46 6f 72 6d  _start(ap, zForm
1eb85 61 74 29 3b 0a 20 20 7a 53 71 6c 20 3d 20 73 71  at);.  zSql = sq
1eb86 6c 69 74 65 33 56 4d 50 72 69 6e 74 66 28 64 62  lite3VMPrintf(db
1eb87 2c 20 7a 46 6f 72 6d 61 74 2c 20 61 70 29 3b 0a  , zFormat, ap);.
1eb88 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20    va_end(ap);.  
1eb89 69 66 28 20 7a 53 71 6c 3d 3d 30 20 29 7b 0a 20  if( zSql==0 ){. 
1eb8a 20 20 20 72 65 74 75 72 6e 3b 20 20 20 2f 2a 20     return;   /* 
1eb8b 41 20 6d 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61  A malloc must ha
1eb8c 76 65 20 66 61 69 6c 65 64 20 2a 2f 0a 20 20 7d  ve failed */.  }
1eb8d 0a 20 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65  .  pParse->neste
1eb8e 64 2b 2b 3b 0a 20 20 6d 65 6d 63 70 79 28 73 61  d++;.  memcpy(sa
1eb8f 76 65 42 75 66 2c 20 26 70 50 61 72 73 65 2d 3e  veBuf, &pParse->
1eb90 6e 56 61 72 2c 20 53 41 56 45 5f 53 5a 29 3b 0a  nVar, SAVE_SZ);.
1eb91 20 20 6d 65 6d 73 65 74 28 26 70 50 61 72 73 65    memset(&pParse
1eb92 2d 3e 6e 56 61 72 2c 20 30 2c 20 53 41 56 45 5f  ->nVar, 0, SAVE_
1eb93 53 5a 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 75  SZ);.  sqlite3Ru
1eb94 6e 50 61 72 73 65 72 28 70 50 61 72 73 65 2c 20  nParser(pParse, 
1eb95 7a 53 71 6c 2c 20 26 7a 45 72 72 4d 73 67 29 3b  zSql, &zErrMsg);
1eb96 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1eb97 28 64 62 2c 20 7a 45 72 72 4d 73 67 29 3b 0a 20  (db, zErrMsg);. 
1eb98 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1eb99 62 2c 20 7a 53 71 6c 29 3b 0a 20 20 6d 65 6d 63  b, zSql);.  memc
1eb9a 70 79 28 26 70 50 61 72 73 65 2d 3e 6e 56 61 72  py(&pParse->nVar
1eb9b 2c 20 73 61 76 65 42 75 66 2c 20 53 41 56 45 5f  , saveBuf, SAVE_
1eb9c 53 5a 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 6e  SZ);.  pParse->n
1eb9d 65 73 74 65 64 2d 2d 3b 0a 7d 0a 0a 2f 2a 0a 2a  ested--;.}../*.*
1eb9e 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 69 6e 2d  * Locate the in-
1eb9f 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75 72 65  memory structure
1eba0 20 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20   that describes 
1eba1 61 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74  a particular dat
1eba2 61 62 61 73 65 0a 2a 2a 20 74 61 62 6c 65 20 67  abase.** table g
1eba3 69 76 65 6e 20 74 68 65 20 6e 61 6d 65 20 6f 66  iven the name of
1eba4 20 74 68 61 74 20 74 61 62 6c 65 20 61 6e 64 20   that table and 
1eba5 28 6f 70 74 69 6f 6e 61 6c 6c 79 29 20 74 68 65  (optionally) the
1eba6 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20   name of the.** 
1eba7 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
1eba8 69 6e 67 20 74 68 65 20 74 61 62 6c 65 2e 20 20  ing the table.  
1eba9 52 65 74 75 72 6e 20 4e 55 4c 4c 20 69 66 20 6e  Return NULL if n
1ebaa 6f 74 20 66 6f 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20  ot found..**.** 
1ebab 49 66 20 7a 44 61 74 61 62 61 73 65 20 69 73 20  If zDatabase is 
1ebac 30 2c 20 61 6c 6c 20 64 61 74 61 62 61 73 65 73  0, all databases
1ebad 20 61 72 65 20 73 65 61 72 63 68 65 64 20 66 6f   are searched fo
1ebae 72 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  r the table and 
1ebaf 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 6d 61 74  the.** first mat
1ebb0 63 68 69 6e 67 20 74 61 62 6c 65 20 69 73 20 72  ching table is r
1ebb1 65 74 75 72 6e 65 64 2e 20 20 28 4e 6f 20 63 68  eturned.  (No ch
1ebb2 65 63 6b 69 6e 67 20 66 6f 72 20 64 75 70 6c 69  ecking for dupli
1ebb3 63 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 6e 61  cate table.** na
1ebb4 6d 65 73 20 69 73 20 64 6f 6e 65 2e 29 20 20 54  mes is done.)  T
1ebb5 68 65 20 73 65 61 72 63 68 20 6f 72 64 65 72 20  he search order 
1ebb6 69 73 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74  is TEMP first, t
1ebb7 68 65 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61  hen MAIN, then a
1ebb8 6e 79 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79 20  ny.** auxiliary 
1ebb9 64 61 74 61 62 61 73 65 73 20 61 64 64 65 64 20  databases added 
1ebba 75 73 69 6e 67 20 74 68 65 20 41 54 54 41 43 48  using the ATTACH
1ebbb 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20   command..**.** 
1ebbc 53 65 65 20 61 6c 73 6f 20 73 71 6c 69 74 65 33  See also sqlite3
1ebbd 4c 6f 63 61 74 65 54 61 62 6c 65 28 29 2e 0a 2a  LocateTable()..*
1ebbe 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ebbf 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 46   Table *sqlite3F
1ebc0 69 6e 64 54 61 62 6c 65 28 73 71 6c 69 74 65 33  indTable(sqlite3
1ebc1 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   *db, const char
1ebc2 20 2a 7a 4e 61 6d 65 2c 20 63 6f 6e 73 74 20 63   *zName, const c
1ebc3 68 61 72 20 2a 7a 44 61 74 61 62 61 73 65 29 7b  har *zDatabase){
1ebc4 0a 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 30 3b  .  Table *p = 0;
1ebc5 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20  .  int i;.  int 
1ebc6 6e 4e 61 6d 65 3b 0a 20 20 61 73 73 65 72 74 28  nName;.  assert(
1ebc7 20 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 6e   zName!=0 );.  n
1ebc8 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 53 74  Name = sqlite3St
1ebc9 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65 29 20  rlen(db, zName) 
1ebca 2b 20 31 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49  + 1;.  for(i=OMI
1ebcb 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e  T_TEMPDB; i<db->
1ebcc 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
1ebcd 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69  nt j = (i<2) ? i
1ebce 5e 31 20 3a 20 69 3b 20 20 20 2f 2a 20 53 65 61  ^1 : i;   /* Sea
1ebcf 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20  rch TEMP before 
1ebd0 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20  MAIN */.    if( 
1ebd1 7a 44 61 74 61 62 61 73 65 21 3d 30 20 26 26 20  zDatabase!=0 && 
1ebd2 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
1ebd3 44 61 74 61 62 61 73 65 2c 20 64 62 2d 3e 61 44  Database, db->aD
1ebd4 62 5b 6a 5d 2e 7a 4e 61 6d 65 29 20 29 20 63 6f  b[j].zName) ) co
1ebd5 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 20 3d 20  ntinue;.    p = 
1ebd6 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
1ebd7 26 64 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68  &db->aDb[j].pSch
1ebd8 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e  ema->tblHash, zN
1ebd9 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
1ebda 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a   if( p ) break;.
1ebdb 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
1ebdc 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20  }../*.** Locate 
1ebdd 74 68 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 73 74  the in-memory st
1ebde 72 75 63 74 75 72 65 20 74 68 61 74 20 64 65 73  ructure that des
1ebdf 63 72 69 62 65 73 20 61 20 70 61 72 74 69 63 75  cribes a particu
1ebe0 6c 61 72 20 64 61 74 61 62 61 73 65 0a 2a 2a 20  lar database.** 
1ebe1 74 61 62 6c 65 20 67 69 76 65 6e 20 74 68 65 20  table given the 
1ebe2 6e 61 6d 65 20 6f 66 20 74 68 61 74 20 74 61 62  name of that tab
1ebe3 6c 65 20 61 6e 64 20 28 6f 70 74 69 6f 6e 61 6c  le and (optional
1ebe4 6c 79 29 20 74 68 65 20 6e 61 6d 65 20 6f 66 20  ly) the name of 
1ebe5 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  the.** database 
1ebe6 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
1ebe7 61 62 6c 65 2e 20 20 52 65 74 75 72 6e 20 4e 55  able.  Return NU
1ebe8 4c 4c 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e  LL if not found.
1ebe9 20 20 41 6c 73 6f 20 6c 65 61 76 65 20 61 6e 0a    Also leave an.
1ebea 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ** error message
1ebeb 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a 45 72 72   in pParse->zErr
1ebec 4d 73 67 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  Msg..**.** The d
1ebed 69 66 66 65 72 65 6e 63 65 20 62 65 74 77 65 65  ifference betwee
1ebee 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 61  n this routine a
1ebef 6e 64 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61  nd sqlite3FindTa
1ebf0 62 6c 65 28 29 20 69 73 20 74 68 61 74 20 74 68  ble() is that th
1ebf1 69 73 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 6c 65  is.** routine le
1ebf2 61 76 65 73 20 61 6e 20 65 72 72 6f 72 20 6d 65  aves an error me
1ebf3 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d  ssage in pParse-
1ebf4 3e 7a 45 72 72 4d 73 67 20 77 68 65 72 65 0a 2a  >zErrMsg where.*
1ebf5 2a 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  * sqlite3FindTab
1ebf6 6c 65 28 29 20 64 6f 65 73 20 6e 6f 74 2e 0a 2a  le() does not..*
1ebf7 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ebf8 20 54 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 4c   Table *sqlite3L
1ebf9 6f 63 61 74 65 54 61 62 6c 65 28 0a 20 20 50 61  ocateTable(.  Pa
1ebfa 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
1ebfb 20 20 20 20 20 2f 2a 20 63 6f 6e 74 65 78 74 20       /* context 
1ebfc 69 6e 20 77 68 69 63 68 20 74 6f 20 72 65 70 6f  in which to repo
1ebfd 72 74 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 69  rt errors */.  i
1ebfe 6e 74 20 69 73 56 69 65 77 2c 20 20 20 20 20 20  nt isView,      
1ebff 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66        /* True if
1ec00 20 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 61 20 56   looking for a V
1ec01 49 45 57 20 72 61 74 68 65 72 20 74 68 61 6e 20  IEW rather than 
1ec02 61 20 54 41 42 4c 45 20 2a 2f 0a 20 20 63 6f 6e  a TABLE */.  con
1ec03 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20  st char *zName, 
1ec04 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
1ec05 68 65 20 74 61 62 6c 65 20 77 65 20 61 72 65 20  he table we are 
1ec06 6c 6f 6f 6b 69 6e 67 20 66 6f 72 20 2a 2f 0a 20  looking for */. 
1ec07 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62   const char *zDb
1ec08 61 73 65 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20  ase     /* Name 
1ec09 6f 66 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  of the database.
1ec0a 20 20 4d 69 67 68 74 20 62 65 20 4e 55 4c 4c 20    Might be NULL 
1ec0b 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
1ec0c 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65  ;..  /* Read the
1ec0d 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61   database schema
1ec0e 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  . If an error oc
1ec0f 63 75 72 73 2c 20 6c 65 61 76 65 20 61 6e 20 65  curs, leave an e
1ec10 72 72 6f 72 20 6d 65 73 73 61 67 65 0a 20 20 2a  rror message.  *
1ec11 2a 20 61 6e 64 20 63 6f 64 65 20 69 6e 20 70 50  * and code in pP
1ec12 61 72 73 65 20 61 6e 64 20 72 65 74 75 72 6e 20  arse and return 
1ec13 4e 55 4c 4c 2e 20 2a 2f 0a 20 20 69 66 28 20 53  NULL. */.  if( S
1ec14 51 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65  QLITE_OK!=sqlite
1ec15 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
1ec16 73 65 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  se) ){.    retur
1ec17 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 20 3d 20  n 0;.  }..  p = 
1ec18 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65  sqlite3FindTable
1ec19 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 7a 4e 61  (pParse->db, zNa
1ec1a 6d 65 2c 20 7a 44 62 61 73 65 29 3b 0a 20 20 69  me, zDbase);.  i
1ec1b 66 28 20 70 3d 3d 30 20 29 7b 0a 20 20 20 20 63  f( p==0 ){.    c
1ec1c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 73 67 20  onst char *zMsg 
1ec1d 3d 20 69 73 56 69 65 77 20 3f 20 22 6e 6f 20 73  = isView ? "no s
1ec1e 75 63 68 20 76 69 65 77 22 20 3a 20 22 6e 6f 20  uch view" : "no 
1ec1f 73 75 63 68 20 74 61 62 6c 65 22 3b 0a 20 20 20  such table";.   
1ec20 20 69 66 28 20 7a 44 62 61 73 65 20 29 7b 0a 20   if( zDbase ){. 
1ec21 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
1ec22 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 73  rMsg(pParse, "%s
1ec23 3a 20 25 73 2e 25 73 22 2c 20 7a 4d 73 67 2c 20  : %s.%s", zMsg, 
1ec24 7a 44 62 61 73 65 2c 20 7a 4e 61 6d 65 29 3b 0a  zDbase, zName);.
1ec25 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1ec26 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1ec27 28 70 50 61 72 73 65 2c 20 22 25 73 3a 20 25 73  (pParse, "%s: %s
1ec28 22 2c 20 7a 4d 73 67 2c 20 7a 4e 61 6d 65 29 3b  ", zMsg, zName);
1ec29 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
1ec2a 65 2d 3e 63 68 65 63 6b 53 63 68 65 6d 61 20 3d  e->checkSchema =
1ec2b 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   1;.  }.  return
1ec2c 20 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63   p;.}../*.** Loc
1ec2d 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d 6f 72  ate the in-memor
1ec2e 79 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74  y structure that
1ec2f 20 64 65 73 63 72 69 62 65 73 20 0a 2a 2a 20 61   describes .** a
1ec30 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e 64 65   particular inde
1ec31 78 20 67 69 76 65 6e 20 74 68 65 20 6e 61 6d 65  x given the name
1ec32 20 6f 66 20 74 68 61 74 20 69 6e 64 65 78 0a 2a   of that index.*
1ec33 2a 20 61 6e 64 20 74 68 65 20 6e 61 6d 65 20 6f  * and the name o
1ec34 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74  f the database t
1ec35 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  hat contains the
1ec36 20 69 6e 64 65 78 2e 0a 2a 2a 20 52 65 74 75 72   index..** Retur
1ec37 6e 20 4e 55 4c 4c 20 69 66 20 6e 6f 74 20 66 6f  n NULL if not fo
1ec38 75 6e 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 7a 44  und..**.** If zD
1ec39 61 74 61 62 61 73 65 20 69 73 20 30 2c 20 61 6c  atabase is 0, al
1ec3a 6c 20 64 61 74 61 62 61 73 65 73 20 61 72 65 20  l databases are 
1ec3b 73 65 61 72 63 68 65 64 20 66 6f 72 20 74 68 65  searched for the
1ec3c 0a 2a 2a 20 74 61 62 6c 65 20 61 6e 64 20 74 68  .** table and th
1ec3d 65 20 66 69 72 73 74 20 6d 61 74 63 68 69 6e 67  e first matching
1ec3e 20 69 6e 64 65 78 20 69 73 20 72 65 74 75 72 6e   index is return
1ec3f 65 64 2e 20 20 28 4e 6f 20 63 68 65 63 6b 69 6e  ed.  (No checkin
1ec40 67 0a 2a 2a 20 66 6f 72 20 64 75 70 6c 69 63 61  g.** for duplica
1ec41 74 65 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 69  te index names i
1ec42 73 20 64 6f 6e 65 2e 29 20 20 54 68 65 20 73 65  s done.)  The se
1ec43 61 72 63 68 20 6f 72 64 65 72 20 69 73 0a 2a 2a  arch order is.**
1ec44 20 54 45 4d 50 20 66 69 72 73 74 2c 20 74 68 65   TEMP first, the
1ec45 6e 20 4d 41 49 4e 2c 20 74 68 65 6e 20 61 6e 79  n MAIN, then any
1ec46 20 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62   auxiliary datab
1ec47 61 73 65 73 20 61 64 64 65 64 0a 2a 2a 20 75 73  ases added.** us
1ec48 69 6e 67 20 74 68 65 20 41 54 54 41 43 48 20 63  ing the ATTACH c
1ec49 6f 6d 6d 61 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54  ommand..*/.SQLIT
1ec4a 45 5f 50 52 49 56 41 54 45 20 49 6e 64 65 78 20  E_PRIVATE Index 
1ec4b 2a 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65  *sqlite3FindInde
1ec4c 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 63  x(sqlite3 *db, c
1ec4d 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
1ec4e 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44  , const char *zD
1ec4f 62 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 20 3d  b){.  Index *p =
1ec50 20 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69   0;.  int i;.  i
1ec51 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74  nt nName = sqlit
1ec52 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61  e3Strlen(db, zNa
1ec53 6d 65 29 2b 31 3b 0a 20 20 66 6f 72 28 69 3d 4f  me)+1;.  for(i=O
1ec54 4d 49 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62  MIT_TEMPDB; i<db
1ec55 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20  ->nDb; i++){.   
1ec56 20 69 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f   int j = (i<2) ?
1ec57 20 69 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65   i^1 : i;  /* Se
1ec58 61 72 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65  arch TEMP before
1ec59 20 4d 41 49 4e 20 2a 2f 0a 20 20 20 20 53 63 68   MAIN */.    Sch
1ec5a 65 6d 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 64  ema *pSchema = d
1ec5b 62 2d 3e 61 44 62 5b 6a 5d 2e 70 53 63 68 65 6d  b->aDb[j].pSchem
1ec5c 61 3b 0a 20 20 20 20 69 66 28 20 7a 44 62 20 26  a;.    if( zDb &
1ec5d 26 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  & sqlite3StrICmp
1ec5e 28 7a 44 62 2c 20 64 62 2d 3e 61 44 62 5b 6a 5d  (zDb, db->aDb[j]
1ec5f 2e 7a 4e 61 6d 65 29 20 29 20 63 6f 6e 74 69 6e  .zName) ) contin
1ec60 75 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  ue;.    assert( 
1ec61 70 53 63 68 65 6d 61 20 7c 7c 20 28 6a 3d 3d 31  pSchema || (j==1
1ec62 20 26 26 20 21 64 62 2d 3e 61 44 62 5b 31 5d 2e   && !db->aDb[1].
1ec63 70 42 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20  pBt) );.    if( 
1ec64 70 53 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20  pSchema ){.     
1ec65 20 70 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68   p = sqlite3Hash
1ec66 46 69 6e 64 28 26 70 53 63 68 65 6d 61 2d 3e 69  Find(&pSchema->i
1ec67 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20 6e  dxHash, zName, n
1ec68 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Name);.    }.   
1ec69 20 69 66 28 20 70 20 29 20 62 72 65 61 6b 3b 0a   if( p ) break;.
1ec6a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a    }.  return p;.
1ec6b 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6c 61 69 6d  }../*.** Reclaim
1ec6c 20 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64   the memory used
1ec6d 20 62 79 20 61 6e 20 69 6e 64 65 78 0a 2a 2f 0a   by an index.*/.
1ec6e 73 74 61 74 69 63 20 76 6f 69 64 20 66 72 65 65  static void free
1ec6f 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 29 7b  Index(Index *p){
1ec70 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
1ec71 20 70 2d 3e 70 54 61 62 6c 65 2d 3e 64 62 3b 0a   p->pTable->db;.
1ec72 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ec73 64 62 2c 20 70 2d 3e 7a 43 6f 6c 41 66 66 29 3b  db, p->zColAff);
1ec74 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
1ec75 28 64 62 2c 20 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (db, p);.}../*.*
1ec76 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 67 69 76  * Remove the giv
1ec77 65 6e 20 69 6e 64 65 78 20 66 72 6f 6d 20 74 68  en index from th
1ec78 65 20 69 6e 64 65 78 20 68 61 73 68 20 74 61 62  e index hash tab
1ec79 6c 65 2c 20 61 6e 64 20 66 72 65 65 0a 2a 2a 20  le, and free.** 
1ec7a 69 74 73 20 6d 65 6d 6f 72 79 20 73 74 72 75 63  its memory struc
1ec7b 74 75 72 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  tures..**.** The
1ec7c 20 69 6e 64 65 78 20 69 73 20 72 65 6d 6f 76 65   index is remove
1ec7d 64 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62  d from the datab
1ec7e 61 73 65 20 68 61 73 68 20 74 61 62 6c 65 73 20  ase hash tables 
1ec7f 62 75 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74  but.** it is not
1ec80 20 75 6e 6c 69 6e 6b 65 64 20 66 72 6f 6d 20 74   unlinked from t
1ec81 68 65 20 54 61 62 6c 65 20 74 68 61 74 20 69 74  he Table that it
1ec82 20 69 6e 64 65 78 65 73 2e 0a 2a 2a 20 55 6e 6c   indexes..** Unl
1ec83 69 6e 6b 69 6e 67 20 66 72 6f 6d 20 74 68 65 20  inking from the 
1ec84 54 61 62 6c 65 20 6d 75 73 74 20 62 65 20 64 6f  Table must be do
1ec85 6e 65 20 62 79 20 74 68 65 20 63 61 6c 6c 69 6e  ne by the callin
1ec86 67 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2f 0a 73  g function..*/.s
1ec87 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
1ec88 65 44 65 6c 65 74 65 49 6e 64 65 78 28 49 6e 64  eDeleteIndex(Ind
1ec89 65 78 20 2a 70 29 7b 0a 20 20 49 6e 64 65 78 20  ex *p){.  Index 
1ec8a 2a 70 4f 6c 64 3b 0a 20 20 63 6f 6e 73 74 20 63  *pOld;.  const c
1ec8b 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 2d 3e  har *zName = p->
1ec8c 7a 4e 61 6d 65 3b 0a 0a 20 20 70 4f 6c 64 20 3d  zName;..  pOld =
1ec8d 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
1ec8e 72 74 28 26 70 2d 3e 70 53 63 68 65 6d 61 2d 3e  rt(&p->pSchema->
1ec8f 69 64 78 48 61 73 68 2c 20 7a 4e 61 6d 65 2c 20  idxHash, zName, 
1ec90 73 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 2b 31 2c  strlen(zName)+1,
1ec91 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70   0);.  assert( p
1ec92 4f 6c 64 3d 3d 30 20 7c 7c 20 70 4f 6c 64 3d 3d  Old==0 || pOld==
1ec93 70 20 29 3b 0a 20 20 66 72 65 65 49 6e 64 65 78  p );.  freeIndex
1ec94 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 6f  (p);.}../*.** Fo
1ec95 72 20 74 68 65 20 69 6e 64 65 78 20 63 61 6c 6c  r the index call
1ec96 65 64 20 7a 49 64 78 4e 61 6d 65 20 77 68 69 63  ed zIdxName whic
1ec97 68 20 69 73 20 66 6f 75 6e 64 20 69 6e 20 74 68  h is found in th
1ec98 65 20 64 61 74 61 62 61 73 65 20 69 44 62 2c 0a  e database iDb,.
1ec99 2a 2a 20 75 6e 6c 69 6b 65 20 74 68 61 74 20 69  ** unlike that i
1ec9a 6e 64 65 78 20 66 72 6f 6d 20 69 74 73 20 54 61  ndex from its Ta
1ec9b 62 6c 65 20 74 68 65 6e 20 72 65 6d 6f 76 65 20  ble then remove 
1ec9c 74 68 65 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a  the index from.*
1ec9d 2a 20 74 68 65 20 69 6e 64 65 78 20 68 61 73 68  * the index hash
1ec9e 20 74 61 62 6c 65 20 61 6e 64 20 66 72 65 65 20   table and free 
1ec9f 61 6c 6c 20 6d 65 6d 6f 72 79 20 73 74 72 75 63  all memory struc
1eca0 74 75 72 65 73 20 61 73 73 6f 63 69 61 74 65 64  tures associated
1eca1 0a 2a 2a 20 77 69 74 68 20 74 68 65 20 69 6e 64  .** with the ind
1eca2 65 78 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ex..*/.SQLITE_PR
1eca3 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1eca4 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65 74  e3UnlinkAndDelet
1eca5 65 49 6e 64 65 78 28 73 71 6c 69 74 65 33 20 2a  eIndex(sqlite3 *
1eca6 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f 6e  db, int iDb, con
1eca7 73 74 20 63 68 61 72 20 2a 7a 49 64 78 4e 61 6d  st char *zIdxNam
1eca8 65 29 7b 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e  e){.  Index *pIn
1eca9 64 65 78 3b 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a  dex;.  int len;.
1ecaa 20 20 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20    Hash *pHash = 
1ecab 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  &db->aDb[iDb].pS
1ecac 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a  chema->idxHash;.
1ecad 0a 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33  .  len = sqlite3
1ecae 53 74 72 6c 65 6e 28 64 62 2c 20 7a 49 64 78 4e  Strlen(db, zIdxN
1ecaf 61 6d 65 29 3b 0a 20 20 70 49 6e 64 65 78 20 3d  ame);.  pIndex =
1ecb0 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65   sqlite3HashInse
1ecb1 72 74 28 70 48 61 73 68 2c 20 7a 49 64 78 4e 61  rt(pHash, zIdxNa
1ecb2 6d 65 2c 20 6c 65 6e 2b 31 2c 20 30 29 3b 0a 20  me, len+1, 0);. 
1ecb3 20 69 66 28 20 70 49 6e 64 65 78 20 29 7b 0a 20   if( pIndex ){. 
1ecb4 20 20 20 69 66 28 20 70 49 6e 64 65 78 2d 3e 70     if( pIndex->p
1ecb5 54 61 62 6c 65 2d 3e 70 49 6e 64 65 78 3d 3d 70  Table->pIndex==p
1ecb6 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 20 20 70  Index ){.      p
1ecb7 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70  Index->pTable->p
1ecb8 49 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 2d 3e  Index = pIndex->
1ecb9 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65 6c 73 65  pNext;.    }else
1ecba 7b 0a 20 20 20 20 20 20 49 6e 64 65 78 20 2a 70  {.      Index *p
1ecbb 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 3d 70 49  ;.      for(p=pI
1ecbc 6e 64 65 78 2d 3e 70 54 61 62 6c 65 2d 3e 70 49  ndex->pTable->pI
1ecbd 6e 64 65 78 3b 20 70 20 26 26 20 70 2d 3e 70 4e  ndex; p && p->pN
1ecbe 65 78 74 21 3d 70 49 6e 64 65 78 3b 20 70 3d 70  ext!=pIndex; p=p
1ecbf 2d 3e 70 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20  ->pNext){}.     
1ecc0 20 69 66 28 20 70 20 26 26 20 70 2d 3e 70 4e 65   if( p && p->pNe
1ecc1 78 74 3d 3d 70 49 6e 64 65 78 20 29 7b 0a 20 20  xt==pIndex ){.  
1ecc2 20 20 20 20 20 20 70 2d 3e 70 4e 65 78 74 20 3d        p->pNext =
1ecc3 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 3b 0a   pIndex->pNext;.
1ecc4 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
1ecc5 20 20 66 72 65 65 49 6e 64 65 78 28 70 49 6e 64    freeIndex(pInd
1ecc6 65 78 29 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 66  ex);.  }.  db->f
1ecc7 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
1ecc8 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 7d 0a  nternChanges;.}.
1ecc9 0a 2f 2a 0a 2a 2a 20 45 72 61 73 65 20 61 6c 6c  ./*.** Erase all
1ecca 20 73 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74   schema informat
1eccb 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d  ion from the in-
1eccc 6d 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c  memory hash tabl
1eccd 65 73 20 6f 66 0a 2a 2a 20 61 20 73 69 6e 67 6c  es of.** a singl
1ecce 65 20 64 61 74 61 62 61 73 65 2e 20 20 54 68 69  e database.  Thi
1eccf 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1ecd0 6c 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  led to reclaim m
1ecd1 65 6d 6f 72 79 0a 2a 2a 20 62 65 66 6f 72 65 20  emory.** before 
1ecd2 74 68 65 20 64 61 74 61 62 61 73 65 20 63 6c 6f  the database clo
1ecd3 73 65 73 2e 20 20 49 74 20 69 73 20 61 6c 73 6f  ses.  It is also
1ecd4 20 63 61 6c 6c 65 64 20 64 75 72 69 6e 67 20 61   called during a
1ecd5 20 72 6f 6c 6c 62 61 63 6b 0a 2a 2a 20 69 66 20   rollback.** if 
1ecd6 74 68 65 72 65 20 77 65 72 65 20 73 63 68 65 6d  there were schem
1ecd7 61 20 63 68 61 6e 67 65 73 20 64 75 72 69 6e 67  a changes during
1ecd8 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f 6e   the transaction
1ecd9 20 6f 72 20 69 66 20 61 0a 2a 2a 20 73 63 68 65   or if a.** sche
1ecda 6d 61 2d 63 6f 6f 6b 69 65 20 6d 69 73 6d 61 74  ma-cookie mismat
1ecdb 63 68 20 6f 63 63 75 72 73 2e 0a 2a 2a 0a 2a 2a  ch occurs..**.**
1ecdc 20 49 66 20 69 44 62 3c 3d 30 20 74 68 65 6e 20   If iDb<=0 then 
1ecdd 72 65 73 65 74 20 74 68 65 20 69 6e 74 65 72 6e  reset the intern
1ecde 61 6c 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  al schema tables
1ecdf 20 66 6f 72 20 61 6c 6c 20 64 61 74 61 62 61 73   for all databas
1ece0 65 0a 2a 2a 20 66 69 6c 65 73 2e 20 20 49 66 20  e.** files.  If 
1ece1 69 44 62 3e 3d 32 20 74 68 65 6e 20 72 65 73 65  iDb>=2 then rese
1ece2 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 73  t the internal s
1ece3 63 68 65 6d 61 20 66 6f 72 20 6f 6e 6c 79 20 74  chema for only t
1ece4 68 65 0a 2a 2a 20 73 69 6e 67 6c 65 20 66 69 6c  he.** single fil
1ece5 65 20 69 6e 64 69 63 61 74 65 64 2e 0a 2a 2f 0a  e indicated..*/.
1ece6 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1ece7 6f 69 64 20 73 71 6c 69 74 65 33 52 65 73 65 74  oid sqlite3Reset
1ece8 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 73  InternalSchema(s
1ece9 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20  qlite3 *db, int 
1ecea 69 44 62 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a  iDb){.  int i, j
1eceb 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e  ;.  assert( iDb>
1ecec 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44  =0 && iDb<db->nD
1eced 62 20 29 3b 0a 0a 20 20 69 66 28 20 69 44 62 3d  b );..  if( iDb=
1ecee 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  =0 ){.    sqlite
1ecef 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64  3BtreeEnterAll(d
1ecf0 62 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  b);.  }.  for(i=
1ecf1 69 44 62 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  iDb; i<db->nDb; 
1ecf2 69 2b 2b 29 7b 0a 20 20 20 20 44 62 20 2a 70 44  i++){.    Db *pD
1ecf3 62 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 5d 3b  b = &db->aDb[i];
1ecf4 0a 20 20 20 20 69 66 28 20 70 44 62 2d 3e 70 53  .    if( pDb->pS
1ecf5 63 68 65 6d 61 20 29 7b 0a 20 20 20 20 20 20 61  chema ){.      a
1ecf6 73 73 65 72 74 28 69 3d 3d 31 20 7c 7c 20 28 70  ssert(i==1 || (p
1ecf7 44 62 2d 3e 70 42 74 20 26 26 20 73 71 6c 69 74  Db->pBt && sqlit
1ecf8 65 33 42 74 72 65 65 48 6f 6c 64 73 4d 75 74 65  e3BtreeHoldsMute
1ecf9 78 28 70 44 62 2d 3e 70 42 74 29 29 29 3b 0a 20  x(pDb->pBt)));. 
1ecfa 20 20 20 20 20 73 71 6c 69 74 65 33 53 63 68 65       sqlite3Sche
1ecfb 6d 61 46 72 65 65 28 70 44 62 2d 3e 70 53 63 68  maFree(pDb->pSch
1ecfc 65 6d 61 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ema);.    }.    
1ecfd 69 66 28 20 69 44 62 3e 30 20 29 20 72 65 74 75  if( iDb>0 ) retu
1ecfe 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  rn;.  }.  assert
1ecff 28 20 69 44 62 3d 3d 30 20 29 3b 0a 20 20 64 62  ( iDb==0 );.  db
1ed00 2d 3e 66 6c 61 67 73 20 26 3d 20 7e 53 51 4c 49  ->flags &= ~SQLI
1ed01 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65 73  TE_InternChanges
1ed02 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72 65 65  ;.  sqlite3Btree
1ed03 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a 0a 20  LeaveAll(db);.. 
1ed04 20 2f 2a 20 49 66 20 6f 6e 65 20 6f 72 20 6d 6f   /* If one or mo
1ed05 72 65 20 6f 66 20 74 68 65 20 61 75 78 69 6c 69  re of the auxili
1ed06 61 72 79 20 64 61 74 61 62 61 73 65 20 66 69 6c  ary database fil
1ed07 65 73 20 68 61 73 20 62 65 65 6e 20 63 6c 6f 73  es has been clos
1ed08 65 64 2c 0a 20 20 2a 2a 20 74 68 65 6e 20 72 65  ed,.  ** then re
1ed09 6d 6f 76 65 20 74 68 65 6d 20 66 72 6f 6d 20 74  move them from t
1ed0a 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  he auxiliary dat
1ed0b 61 62 61 73 65 20 6c 69 73 74 2e 20 20 57 65 20  abase list.  We 
1ed0c 74 61 6b 65 20 74 68 65 0a 20 20 2a 2a 20 6f 70  take the.  ** op
1ed0d 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 64 6f 20  portunity to do 
1ed0e 74 68 69 73 20 68 65 72 65 20 73 69 6e 63 65 20  this here since 
1ed0f 77 65 20 68 61 76 65 20 6a 75 73 74 20 64 65 6c  we have just del
1ed10 65 74 65 64 20 61 6c 6c 20 6f 66 20 74 68 65 0a  eted all of the.
1ed11 20 20 2a 2a 20 73 63 68 65 6d 61 20 68 61 73 68    ** schema hash
1ed12 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65 72   tables and ther
1ed13 65 66 6f 72 65 20 64 6f 20 6e 6f 74 20 68 61 76  efore do not hav
1ed14 65 20 74 6f 20 6d 61 6b 65 20 61 6e 79 20 63 68  e to make any ch
1ed15 61 6e 67 65 73 0a 20 20 2a 2a 20 74 6f 20 61 6e  anges.  ** to an
1ed16 79 20 6f 66 20 74 68 6f 73 65 20 74 61 62 6c 65  y of those table
1ed17 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d  s..  */.  for(i=
1ed18 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b  0; i<db->nDb; i+
1ed19 2b 29 7b 0a 20 20 20 20 73 74 72 75 63 74 20 44  +){.    struct D
1ed1a 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e 61 44  b *pDb = &db->aD
1ed1b 62 5b 69 5d 3b 0a 20 20 20 20 69 66 28 20 70 44  b[i];.    if( pD
1ed1c 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20 20 20  b->pBt==0 ){.   
1ed1d 20 20 20 69 66 28 20 70 44 62 2d 3e 70 41 75 78     if( pDb->pAux
1ed1e 20 26 26 20 70 44 62 2d 3e 78 46 72 65 65 41 75   && pDb->xFreeAu
1ed1f 78 20 29 20 70 44 62 2d 3e 78 46 72 65 65 41 75  x ) pDb->xFreeAu
1ed20 78 28 70 44 62 2d 3e 70 41 75 78 29 3b 0a 20 20  x(pDb->pAux);.  
1ed21 20 20 20 20 70 44 62 2d 3e 70 41 75 78 20 3d 20      pDb->pAux = 
1ed22 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  0;.    }.  }.  f
1ed23 6f 72 28 69 3d 6a 3d 32 3b 20 69 3c 64 62 2d 3e  or(i=j=2; i<db->
1ed24 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  nDb; i++){.    s
1ed25 74 72 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20  truct Db *pDb = 
1ed26 26 64 62 2d 3e 61 44 62 5b 69 5d 3b 0a 20 20 20  &db->aDb[i];.   
1ed27 20 69 66 28 20 70 44 62 2d 3e 70 42 74 3d 3d 30   if( pDb->pBt==0
1ed28 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1ed29 33 44 62 46 72 65 65 28 64 62 2c 20 70 44 62 2d  3DbFree(db, pDb-
1ed2a 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 70  >zName);.      p
1ed2b 44 62 2d 3e 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20  Db->zName = 0;. 
1ed2c 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20       continue;. 
1ed2d 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6a 3c 69     }.    if( j<i
1ed2e 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 61 44   ){.      db->aD
1ed2f 62 5b 6a 5d 20 3d 20 64 62 2d 3e 61 44 62 5b 69  b[j] = db->aDb[i
1ed30 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 2b 2b  ];.    }.    j++
1ed31 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 26  ;.  }.  memset(&
1ed32 64 62 2d 3e 61 44 62 5b 6a 5d 2c 20 30 2c 20 28  db->aDb[j], 0, (
1ed33 64 62 2d 3e 6e 44 62 2d 6a 29 2a 73 69 7a 65 6f  db->nDb-j)*sizeo
1ed34 66 28 64 62 2d 3e 61 44 62 5b 6a 5d 29 29 3b 0a  f(db->aDb[j]));.
1ed35 20 20 64 62 2d 3e 6e 44 62 20 3d 20 6a 3b 0a 20    db->nDb = j;. 
1ed36 20 69 66 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20   if( db->nDb<=2 
1ed37 26 26 20 64 62 2d 3e 61 44 62 21 3d 64 62 2d 3e  && db->aDb!=db->
1ed38 61 44 62 53 74 61 74 69 63 20 29 7b 0a 20 20 20  aDbStatic ){.   
1ed39 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61 44 62 53   memcpy(db->aDbS
1ed3a 74 61 74 69 63 2c 20 64 62 2d 3e 61 44 62 2c 20  tatic, db->aDb, 
1ed3b 32 2a 73 69 7a 65 6f 66 28 64 62 2d 3e 61 44 62  2*sizeof(db->aDb
1ed3c 5b 30 5d 29 29 3b 0a 20 20 20 20 73 71 6c 69 74  [0]));.    sqlit
1ed3d 65 33 44 62 46 72 65 65 28 64 62 2c 20 64 62 2d  e3DbFree(db, db-
1ed3e 3e 61 44 62 29 3b 0a 20 20 20 20 64 62 2d 3e 61  >aDb);.    db->a
1ed3f 44 62 20 3d 20 64 62 2d 3e 61 44 62 53 74 61 74  Db = db->aDbStat
1ed40 69 63 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  ic;.  }.}../*.**
1ed41 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1ed42 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 20 63   called when a c
1ed43 6f 6d 6d 69 74 20 6f 63 63 75 72 73 2e 0a 2a 2f  ommit occurs..*/
1ed44 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1ed45 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d  void sqlite3Comm
1ed46 69 74 49 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65  itInternalChange
1ed47 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a  s(sqlite3 *db){.
1ed48 20 20 64 62 2d 3e 66 6c 61 67 73 20 26 3d 20 7e    db->flags &= ~
1ed49 53 51 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61  SQLITE_InternCha
1ed4a 6e 67 65 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  nges;.}../*.** C
1ed4b 6c 65 61 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20  lear the column 
1ed4c 6e 61 6d 65 73 20 66 72 6f 6d 20 61 20 74 61 62  names from a tab
1ed4d 6c 65 20 6f 72 20 76 69 65 77 2e 0a 2a 2f 0a 73  le or view..*/.s
1ed4e 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
1ed4f 65 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65  eResetColumnName
1ed50 73 28 54 61 62 6c 65 20 2a 70 54 61 62 6c 65 29  s(Table *pTable)
1ed51 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f 6c  {.  int i;.  Col
1ed52 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c  umn *pCol;.  sql
1ed53 69 74 65 33 20 2a 64 62 20 3d 20 70 54 61 62 6c  ite3 *db = pTabl
1ed54 65 2d 3e 64 62 3b 0a 20 20 61 73 73 65 72 74 28  e->db;.  assert(
1ed55 20 70 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20   pTable!=0 );.  
1ed56 69 66 28 20 28 70 43 6f 6c 20 3d 20 70 54 61 62  if( (pCol = pTab
1ed57 6c 65 2d 3e 61 43 6f 6c 29 21 3d 30 20 29 7b 0a  le->aCol)!=0 ){.
1ed58 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
1ed59 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b  Table->nCol; i++
1ed5a 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20  , pCol++){.     
1ed5b 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1ed5c 62 2c 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b  b, pCol->zName);
1ed5d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
1ed5e 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f  prDelete(db, pCo
1ed5f 6c 2d 3e 70 44 66 6c 74 29 3b 0a 20 20 20 20 20  l->pDflt);.     
1ed60 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1ed61 62 2c 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 29 3b  b, pCol->zType);
1ed62 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
1ed63 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
1ed64 43 6f 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20  Coll);.    }.   
1ed65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1ed66 62 2c 20 70 54 61 62 6c 65 2d 3e 61 43 6f 6c 29  b, pTable->aCol)
1ed67 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65 2d 3e  ;.  }.  pTable->
1ed68 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 70 54 61 62  aCol = 0;.  pTab
1ed69 6c 65 2d 3e 6e 43 6f 6c 20 3d 20 30 3b 0a 7d 0a  le->nCol = 0;.}.
1ed6a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 74 68  ./*.** Remove th
1ed6b 65 20 6d 65 6d 6f 72 79 20 64 61 74 61 20 73 74  e memory data st
1ed6c 72 75 63 74 75 72 65 73 20 61 73 73 6f 63 69 61  ructures associa
1ed6d 74 65 64 20 77 69 74 68 20 74 68 65 20 67 69 76  ted with the giv
1ed6e 65 6e 0a 2a 2a 20 54 61 62 6c 65 2e 20 20 4e 6f  en.** Table.  No
1ed6f 20 63 68 61 6e 67 65 73 20 61 72 65 20 6d 61 64   changes are mad
1ed70 65 20 74 6f 20 64 69 73 6b 20 62 79 20 74 68 69  e to disk by thi
1ed71 73 20 72 6f 75 74 69 6e 65 2e 0a 2a 2a 0a 2a 2a  s routine..**.**
1ed72 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6a 75   This routine ju
1ed73 73 74 20 64 65 6c 65 74 65 73 20 74 68 65 20 64  st deletes the d
1ed74 61 74 61 20 73 74 72 75 63 74 75 72 65 2e 20 20  ata structure.  
1ed75 49 74 20 64 6f 65 73 20 6e 6f 74 20 75 6e 6c 69  It does not unli
1ed76 6e 6b 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 20  nk.** the table 
1ed77 64 61 74 61 20 73 74 72 75 63 74 75 72 65 20 66  data structure f
1ed78 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
1ed79 6c 65 2e 20 20 4e 6f 72 20 64 6f 65 73 20 69 74  le.  Nor does it
1ed7a 20 72 65 6d 6f 76 65 0a 2a 2a 20 66 6f 72 65 69   remove.** forei
1ed7b 67 6e 20 6b 65 79 73 20 66 72 6f 6d 20 74 68 65  gn keys from the
1ed7c 20 73 71 6c 69 74 65 2e 61 46 4b 65 79 20 68 61   sqlite.aFKey ha
1ed7d 73 68 20 74 61 62 6c 65 2e 20 20 42 75 74 20 69  sh table.  But i
1ed7e 74 20 64 6f 65 73 20 64 65 73 74 72 6f 79 0a 2a  t does destroy.*
1ed7f 2a 20 6d 65 6d 6f 72 79 20 73 74 72 75 63 74 75  * memory structu
1ed80 72 65 73 20 6f 66 20 74 68 65 20 69 6e 64 69 63  res of the indic
1ed81 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  es and foreign k
1ed82 65 79 73 20 61 73 73 6f 63 69 61 74 65 64 20 77  eys associated w
1ed83 69 74 68 20 0a 2a 2a 20 74 68 65 20 74 61 62 6c  ith .** the tabl
1ed84 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
1ed85 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65  VATE void sqlite
1ed86 33 44 65 6c 65 74 65 54 61 62 6c 65 28 54 61 62  3DeleteTable(Tab
1ed87 6c 65 20 2a 70 54 61 62 6c 65 29 7b 0a 20 20 49  le *pTable){.  I
1ed88 6e 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 2a 70  ndex *pIndex, *p
1ed89 4e 65 78 74 3b 0a 20 20 46 4b 65 79 20 2a 70 46  Next;.  FKey *pF
1ed8a 4b 65 79 2c 20 2a 70 4e 65 78 74 46 4b 65 79 3b  Key, *pNextFKey;
1ed8b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a  .  sqlite3 *db;.
1ed8c 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
1ed8d 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 64 62 20   ) return;.  db 
1ed8e 3d 20 70 54 61 62 6c 65 2d 3e 64 62 3b 0a 0a 20  = pTable->db;.. 
1ed8f 20 2f 2a 20 44 6f 20 6e 6f 74 20 64 65 6c 65 74   /* Do not delet
1ed90 65 20 74 68 65 20 74 61 62 6c 65 20 75 6e 74 69  e the table unti
1ed91 6c 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 20  l the reference 
1ed92 63 6f 75 6e 74 20 72 65 61 63 68 65 73 20 7a 65  count reaches ze
1ed93 72 6f 2e 20 2a 2f 0a 20 20 70 54 61 62 6c 65 2d  ro. */.  pTable-
1ed94 3e 6e 52 65 66 2d 2d 3b 0a 20 20 69 66 28 20 70  >nRef--;.  if( p
1ed95 54 61 62 6c 65 2d 3e 6e 52 65 66 3e 30 20 29 7b  Table->nRef>0 ){
1ed96 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
1ed97 0a 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c  .  assert( pTabl
1ed98 65 2d 3e 6e 52 65 66 3d 3d 30 20 29 3b 0a 0a 20  e->nRef==0 );.. 
1ed99 20 2f 2a 20 44 65 6c 65 74 65 20 61 6c 6c 20 69   /* Delete all i
1ed9a 6e 64 69 63 65 73 20 61 73 73 6f 63 69 61 74 65  ndices associate
1ed9b 64 20 77 69 74 68 20 74 68 69 73 20 74 61 62 6c  d with this tabl
1ed9c 65 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70 49 6e  e.  */.  for(pIn
1ed9d 64 65 78 20 3d 20 70 54 61 62 6c 65 2d 3e 70 49  dex = pTable->pI
1ed9e 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20 70 49  ndex; pIndex; pI
1ed9f 6e 64 65 78 3d 70 4e 65 78 74 29 7b 0a 20 20 20  ndex=pNext){.   
1eda0 20 70 4e 65 78 74 20 3d 20 70 49 6e 64 65 78 2d   pNext = pIndex-
1eda1 3e 70 4e 65 78 74 3b 0a 20 20 20 20 61 73 73 65  >pNext;.    asse
1eda2 72 74 28 20 70 49 6e 64 65 78 2d 3e 70 53 63 68  rt( pIndex->pSch
1eda3 65 6d 61 3d 3d 70 54 61 62 6c 65 2d 3e 70 53 63  ema==pTable->pSc
1eda4 68 65 6d 61 20 29 3b 0a 20 20 20 20 73 71 6c 69  hema );.    sqli
1eda5 74 65 44 65 6c 65 74 65 49 6e 64 65 78 28 70 49  teDeleteIndex(pI
1eda6 6e 64 65 78 29 3b 0a 20 20 7d 0a 0a 23 69 66 6e  ndex);.  }..#ifn
1eda7 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1eda8 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20 2f 2a  FOREIGN_KEY.  /*
1eda9 20 44 65 6c 65 74 65 20 61 6c 6c 20 66 6f 72 65   Delete all fore
1edaa 69 67 6e 20 6b 65 79 73 20 61 73 73 6f 63 69 61  ign keys associa
1edab 74 65 64 20 77 69 74 68 20 74 68 69 73 20 74 61  ted with this ta
1edac 62 6c 65 2e 20 20 54 68 65 20 6b 65 79 73 0a 20  ble.  The keys. 
1edad 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61 76 65 20   ** should have 
1edae 61 6c 72 65 61 64 79 20 62 65 65 6e 20 75 6e 6c  already been unl
1edaf 69 6e 6b 65 64 20 66 72 6f 6d 20 74 68 65 20 70  inked from the p
1edb0 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 20 68 61  Schema->aFKey ha
1edb1 73 68 20 74 61 62 6c 65 20 0a 20 20 2a 2f 0a 20  sh table .  */. 
1edb2 20 66 6f 72 28 70 46 4b 65 79 3d 70 54 61 62 6c   for(pFKey=pTabl
1edb3 65 2d 3e 70 46 4b 65 79 3b 20 70 46 4b 65 79 3b  e->pFKey; pFKey;
1edb4 20 70 46 4b 65 79 3d 70 4e 65 78 74 46 4b 65 79   pFKey=pNextFKey
1edb5 29 7b 0a 20 20 20 20 70 4e 65 78 74 46 4b 65 79  ){.    pNextFKey
1edb6 20 3d 20 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46   = pFKey->pNextF
1edb7 72 6f 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28  rom;.    assert(
1edb8 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
1edb9 28 26 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d  (&pTable->pSchem
1edba 61 2d 3e 61 46 4b 65 79 2c 0a 20 20 20 20 20 20  a->aFKey,.      
1edbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1edbc 20 20 20 20 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c       pFKey->zTo,
1edbd 20 73 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a   strlen(pFKey->z
1edbe 54 6f 29 2b 31 29 21 3d 70 46 4b 65 79 20 29 3b  To)+1)!=pFKey );
1edbf 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1edc0 65 65 28 64 62 2c 20 70 46 4b 65 79 29 3b 0a 20  ee(db, pFKey);. 
1edc1 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20   }.#endif..  /* 
1edc2 44 65 6c 65 74 65 20 74 68 65 20 54 61 62 6c 65  Delete the Table
1edc3 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65 6c   structure itsel
1edc4 66 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  f..  */.  sqlite
1edc5 52 65 73 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ResetColumnNames
1edc6 28 70 54 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69  (pTable);.  sqli
1edc7 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 54  te3DbFree(db, pT
1edc8 61 62 6c 65 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  able->zName);.  
1edc9 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
1edca 2c 20 70 54 61 62 6c 65 2d 3e 7a 43 6f 6c 41 66  , pTable->zColAf
1edcb 66 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c  f);.  sqlite3Sel
1edcc 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 54  ectDelete(db, pT
1edcd 61 62 6c 65 2d 3e 70 53 65 6c 65 63 74 29 3b 0a  able->pSelect);.
1edce 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1edcf 4d 49 54 5f 43 48 45 43 4b 0a 20 20 73 71 6c 69  MIT_CHECK.  sqli
1edd0 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
1edd1 2c 20 70 54 61 62 6c 65 2d 3e 70 43 68 65 63 6b  , pTable->pCheck
1edd2 29 3b 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  );.#endif.  sqli
1edd3 74 65 33 56 74 61 62 43 6c 65 61 72 28 70 54 61  te3VtabClear(pTa
1edd4 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  ble);.  sqlite3D
1edd5 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65  bFree(db, pTable
1edd6 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 69  );.}../*.** Unli
1edd7 6e 6b 20 74 68 65 20 67 69 76 65 6e 20 74 61 62  nk the given tab
1edd8 6c 65 20 66 72 6f 6d 20 74 68 65 20 68 61 73 68  le from the hash
1edd9 20 74 61 62 6c 65 73 20 61 6e 64 20 74 68 65 20   tables and the 
1edda 64 65 6c 65 74 65 20 74 68 65 0a 2a 2a 20 74 61  delete the.** ta
1eddb 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 77 69  ble structure wi
1eddc 74 68 20 61 6c 6c 20 69 74 73 20 69 6e 64 69 63  th all its indic
1eddd 65 73 20 61 6e 64 20 66 6f 72 65 69 67 6e 20 6b  es and foreign k
1edde 65 79 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  eys..*/.SQLITE_P
1eddf 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
1ede0 74 65 33 55 6e 6c 69 6e 6b 41 6e 64 44 65 6c 65  te3UnlinkAndDele
1ede1 74 65 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20  teTable(sqlite3 
1ede2 2a 64 62 2c 20 69 6e 74 20 69 44 62 2c 20 63 6f  *db, int iDb, co
1ede3 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 4e 61  nst char *zTabNa
1ede4 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b  me){.  Table *p;
1ede5 0a 20 20 46 4b 65 79 20 2a 70 46 31 2c 20 2a 70  .  FKey *pF1, *p
1ede6 46 32 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a 0a  F2;.  Db *pDb;..
1ede7 20 20 61 73 73 65 72 74 28 20 64 62 21 3d 30 20    assert( db!=0 
1ede8 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
1ede9 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e 6e  >=0 && iDb<db->n
1edea 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  Db );.  assert( 
1edeb 7a 54 61 62 4e 61 6d 65 20 26 26 20 7a 54 61 62  zTabName && zTab
1edec 4e 61 6d 65 5b 30 5d 20 29 3b 0a 20 20 70 44 62  Name[0] );.  pDb
1eded 20 3d 20 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d   = &db->aDb[iDb]
1edee 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48  ;.  p = sqlite3H
1edef 61 73 68 49 6e 73 65 72 74 28 26 70 44 62 2d 3e  ashInsert(&pDb->
1edf0 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48 61 73 68  pSchema->tblHash
1edf1 2c 20 7a 54 61 62 4e 61 6d 65 2c 20 73 74 72 6c  , zTabName, strl
1edf2 65 6e 28 7a 54 61 62 4e 61 6d 65 29 2b 31 2c 30  en(zTabName)+1,0
1edf3 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 23 69  );.  if( p ){.#i
1edf4 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
1edf5 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
1edf6 20 20 66 6f 72 28 70 46 31 3d 70 2d 3e 70 46 4b    for(pF1=p->pFK
1edf7 65 79 3b 20 70 46 31 3b 20 70 46 31 3d 70 46 31  ey; pF1; pF1=pF1
1edf8 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20  ->pNextFrom){.  
1edf9 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20 73 74      int nTo = st
1edfa 72 6c 65 6e 28 70 46 31 2d 3e 7a 54 6f 29 20 2b  rlen(pF1->zTo) +
1edfb 20 31 3b 0a 20 20 20 20 20 20 70 46 32 20 3d 20   1;.      pF2 = 
1edfc 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28  sqlite3HashFind(
1edfd 26 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 61  &pDb->pSchema->a
1edfe 46 4b 65 79 2c 20 70 46 31 2d 3e 7a 54 6f 2c 20  FKey, pF1->zTo, 
1edff 6e 54 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20  nTo);.      if( 
1ee00 70 46 32 3d 3d 70 46 31 20 29 7b 0a 20 20 20 20  pF2==pF1 ){.    
1ee01 20 20 20 20 73 71 6c 69 74 65 33 48 61 73 68 49      sqlite3HashI
1ee02 6e 73 65 72 74 28 26 70 44 62 2d 3e 70 53 63 68  nsert(&pDb->pSch
1ee03 65 6d 61 2d 3e 61 46 4b 65 79 2c 20 70 46 31 2d  ema->aFKey, pF1-
1ee04 3e 7a 54 6f 2c 20 6e 54 6f 2c 20 70 46 31 2d 3e  >zTo, nTo, pF1->
1ee05 70 4e 65 78 74 54 6f 29 3b 0a 20 20 20 20 20 20  pNextTo);.      
1ee06 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 77  }else{.        w
1ee07 68 69 6c 65 28 20 70 46 32 20 26 26 20 70 46 32  hile( pF2 && pF2
1ee08 2d 3e 70 4e 65 78 74 54 6f 21 3d 70 46 31 20 29  ->pNextTo!=pF1 )
1ee09 7b 20 70 46 32 3d 70 46 32 2d 3e 70 4e 65 78 74  { pF2=pF2->pNext
1ee0a 54 6f 3b 20 7d 0a 20 20 20 20 20 20 20 20 69 66  To; }.        if
1ee0b 28 20 70 46 32 20 29 7b 0a 20 20 20 20 20 20 20  ( pF2 ){.       
1ee0c 20 20 20 70 46 32 2d 3e 70 4e 65 78 74 54 6f 20     pF2->pNextTo 
1ee0d 3d 20 70 46 31 2d 3e 70 4e 65 78 74 54 6f 3b 0a  = pF1->pNextTo;.
1ee0e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
1ee0f 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
1ee10 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
1ee11 54 61 62 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20  Table(p);.  }.  
1ee12 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
1ee13 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
1ee14 73 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65  s;.}../*.** Give
1ee15 6e 20 61 20 74 6f 6b 65 6e 2c 20 72 65 74 75 72  n a token, retur
1ee16 6e 20 61 20 73 74 72 69 6e 67 20 74 68 61 74 20  n a string that 
1ee17 63 6f 6e 73 69 73 74 73 20 6f 66 20 74 68 65 20  consists of the 
1ee18 74 65 78 74 20 6f 66 20 74 68 61 74 0a 2a 2a 20  text of that.** 
1ee19 74 6f 6b 65 6e 20 77 69 74 68 20 61 6e 79 20 71  token with any q
1ee1a 75 6f 74 61 74 69 6f 6e 73 20 72 65 6d 6f 76 65  uotations remove
1ee1b 64 2e 20 20 53 70 61 63 65 20 74 6f 20 68 6f 6c  d.  Space to hol
1ee1c 64 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 73  d the returned s
1ee1d 74 72 69 6e 67 0a 2a 2a 20 69 73 20 6f 62 74 61  tring.** is obta
1ee1e 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
1ee1f 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75 73  Malloc() and mus
1ee20 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74 68  t be freed by th
1ee21 65 20 63 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e  e calling.** fun
1ee22 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 6f 6b  ction..**.** Tok
1ee23 65 6e 73 20 61 72 65 20 6f 66 74 65 6e 20 6a 75  ens are often ju
1ee24 73 74 20 70 6f 69 6e 74 65 72 73 20 69 6e 74 6f  st pointers into
1ee25 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51   the original SQ
1ee26 4c 20 74 65 78 74 20 61 6e 64 20 73 6f 0a 2a 2a  L text and so.**
1ee27 20 61 72 65 20 6e 6f 74 20 5c 30 30 30 20 74 65   are not \000 te
1ee28 72 6d 69 6e 61 74 65 64 20 61 6e 64 20 61 72 65  rminated and are
1ee29 20 6e 6f 74 20 70 65 72 73 69 73 74 65 6e 74 2e   not persistent.
1ee2a 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 20 73    The returned s
1ee2b 74 72 69 6e 67 0a 2a 2a 20 69 73 20 5c 30 30 30  tring.** is \000
1ee2c 20 74 65 72 6d 69 6e 61 74 65 64 20 61 6e 64 20   terminated and 
1ee2d 69 73 20 70 65 72 73 69 73 74 65 6e 74 2e 0a 2a  is persistent..*
1ee2e 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ee2f 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 4e 61   char *sqlite3Na
1ee30 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 73 71 6c 69  meFromToken(sqli
1ee31 74 65 33 20 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a  te3 *db, Token *
1ee32 70 4e 61 6d 65 29 7b 0a 20 20 63 68 61 72 20 2a  pName){.  char *
1ee33 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 70 4e 61  zName;.  if( pNa
1ee34 6d 65 20 29 7b 0a 20 20 20 20 7a 4e 61 6d 65 20  me ){.    zName 
1ee35 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44  = sqlite3DbStrND
1ee36 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 4e  up(db, (char*)pN
1ee37 61 6d 65 2d 3e 7a 2c 20 70 4e 61 6d 65 2d 3e 6e  ame->z, pName->n
1ee38 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
1ee39 71 75 6f 74 65 28 7a 4e 61 6d 65 29 3b 0a 20 20  quote(zName);.  
1ee3a 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 4e 61 6d 65  }else{.    zName
1ee3b 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
1ee3c 72 6e 20 7a 4e 61 6d 65 3b 0a 7d 0a 0a 2f 2a 0a  rn zName;.}../*.
1ee3d 2a 2a 20 4f 70 65 6e 20 74 68 65 20 73 71 6c 69  ** Open the sqli
1ee3e 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20  te_master table 
1ee3f 73 74 6f 72 65 64 20 69 6e 20 64 61 74 61 62 61  stored in databa
1ee40 73 65 20 6e 75 6d 62 65 72 20 69 44 62 20 66 6f  se number iDb fo
1ee41 72 0a 2a 2a 20 77 72 69 74 69 6e 67 2e 20 54 68  r.** writing. Th
1ee42 65 20 74 61 62 6c 65 20 69 73 20 6f 70 65 6e 65  e table is opene
1ee43 64 20 75 73 69 6e 67 20 63 75 72 73 6f 72 20 30  d using cursor 0
1ee44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1ee45 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1ee46 4f 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28  OpenMasterTable(
1ee47 50 61 72 73 65 20 2a 70 2c 20 69 6e 74 20 69 44  Parse *p, int iD
1ee48 62 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  b){.  Vdbe *v = 
1ee49 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
1ee4a 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 61 62 6c  );.  sqlite3Tabl
1ee4b 65 4c 6f 63 6b 28 70 2c 20 69 44 62 2c 20 4d 41  eLock(p, iDb, MA
1ee4c 53 54 45 52 5f 52 4f 4f 54 2c 20 31 2c 20 53 43  STER_ROOT, 1, SC
1ee4d 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 29  HEMA_TABLE(iDb))
1ee4e 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
1ee4f 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e  ddOp2(v, OP_SetN
1ee50 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 35 29  umColumns, 0, 5)
1ee51 3b 2f 2a 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ;/* sqlite_maste
1ee52 72 20 68 61 73 20 35 20 63 6f 6c 75 6d 6e 73 20  r has 5 columns 
1ee53 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
1ee54 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
1ee55 6e 57 72 69 74 65 2c 20 30 2c 20 4d 41 53 54 45  nWrite, 0, MASTE
1ee56 52 5f 52 4f 4f 54 2c 20 69 44 62 29 3b 0a 7d 0a  R_ROOT, iDb);.}.
1ee57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 74 6f 6b 65 6e  ./*.** The token
1ee58 20 2a 70 4e 61 6d 65 20 63 6f 6e 74 61 69 6e 73   *pName contains
1ee59 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 61 20 64   the name of a d
1ee5a 61 74 61 62 61 73 65 20 28 65 69 74 68 65 72 20  atabase (either 
1ee5b 22 6d 61 69 6e 22 20 6f 72 0a 2a 2a 20 22 74 65  "main" or.** "te
1ee5c 6d 70 22 20 6f 72 20 74 68 65 20 6e 61 6d 65 20  mp" or the name 
1ee5d 6f 66 20 61 6e 20 61 74 74 61 63 68 65 64 20 64  of an attached d
1ee5e 62 29 2e 20 54 68 69 73 20 72 6f 75 74 69 6e 65  b). This routine
1ee5f 20 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20   returns the.** 
1ee60 69 6e 64 65 78 20 6f 66 20 74 68 65 20 6e 61 6d  index of the nam
1ee61 65 64 20 64 61 74 61 62 61 73 65 20 69 6e 20 64  ed database in d
1ee62 62 2d 3e 61 44 62 5b 5d 2c 20 6f 72 20 2d 31 20  b->aDb[], or -1 
1ee63 69 66 20 74 68 65 20 6e 61 6d 65 64 20 64 62 20  if the named db 
1ee64 0a 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  .** does not exi
1ee65 73 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  st..*/.SQLITE_PR
1ee66 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
1ee67 33 46 69 6e 64 44 62 28 73 71 6c 69 74 65 33 20  3FindDb(sqlite3 
1ee68 2a 64 62 2c 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  *db, Token *pNam
1ee69 65 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31  e){.  int i = -1
1ee6a 3b 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  ;    /* Database
1ee6b 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73 69 7a   number */.  siz
1ee6c 65 5f 74 20 6e 3b 20 20 20 20 20 20 2f 2a 20 4e  e_t n;      /* N
1ee6d 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
1ee6e 65 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20  ers in the name 
1ee6f 2a 2f 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20  */.  Db *pDb;   
1ee70 20 20 20 20 2f 2a 20 41 20 64 61 74 61 62 61 73      /* A databas
1ee71 65 20 77 68 6f 73 65 20 6e 61 6d 65 20 73 70 61  e whose name spa
1ee72 63 65 20 69 73 20 62 65 69 6e 67 20 73 65 61 72  ce is being sear
1ee73 63 68 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ched */.  char *
1ee74 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 4e 61 6d 65  zName;   /* Name
1ee75 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e   we are searchin
1ee76 67 20 66 6f 72 20 2a 2f 0a 0a 20 20 7a 4e 61 6d  g for */..  zNam
1ee77 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
1ee78 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4e 61  romToken(db, pNa
1ee79 6d 65 29 3b 0a 20 20 69 66 28 20 7a 4e 61 6d 65  me);.  if( zName
1ee7a 20 29 7b 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c   ){.    n = strl
1ee7b 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 66  en(zName);.    f
1ee7c 6f 72 28 69 3d 28 64 62 2d 3e 6e 44 62 2d 31 29  or(i=(db->nDb-1)
1ee7d 2c 20 70 44 62 3d 26 64 62 2d 3e 61 44 62 5b 69  , pDb=&db->aDb[i
1ee7e 5d 3b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 70 44  ]; i>=0; i--, pD
1ee7f 62 2d 2d 29 7b 0a 20 20 20 20 20 20 69 66 28 20  b--){.      if( 
1ee80 28 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 7c 7c  (!OMIT_TEMPDB ||
1ee81 20 69 21 3d 31 20 29 20 26 26 20 6e 3d 3d 73 74   i!=1 ) && n==st
1ee82 72 6c 65 6e 28 70 44 62 2d 3e 7a 4e 61 6d 65 29  rlen(pDb->zName)
1ee83 20 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 30   && .          0
1ee84 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
1ee85 28 70 44 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61  (pDb->zName, zNa
1ee86 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62  me) ){.        b
1ee87 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
1ee88 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44    }.    sqlite3D
1ee89 62 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29  bFree(db, zName)
1ee8a 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69  ;.  }.  return i
1ee8b 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 74 61 62 6c  ;.}../* The tabl
1ee8c 65 20 6f 72 20 76 69 65 77 20 6f 72 20 74 72 69  e or view or tri
1ee8d 67 67 65 72 20 6e 61 6d 65 20 69 73 20 70 61 73  gger name is pas
1ee8e 73 65 64 20 74 6f 20 74 68 69 73 20 72 6f 75 74  sed to this rout
1ee8f 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e 73 0a 2a  ine via tokens.*
1ee90 2a 20 70 4e 61 6d 65 31 20 61 6e 64 20 70 4e 61  * pName1 and pNa
1ee91 6d 65 32 2e 20 49 66 20 74 68 65 20 74 61 62 6c  me2. If the tabl
1ee92 65 20 6e 61 6d 65 20 77 61 73 20 66 75 6c 6c 79  e name was fully
1ee93 20 71 75 61 6c 69 66 69 65 64 2c 20 66 6f 72 20   qualified, for 
1ee94 65 78 61 6d 70 6c 65 3a 0a 2a 2a 0a 2a 2a 20 43  example:.**.** C
1ee95 52 45 41 54 45 20 54 41 42 4c 45 20 78 78 78 2e  REATE TABLE xxx.
1ee96 79 79 79 20 28 2e 2e 2e 29 3b 0a 2a 2a 20 0a 2a  yyy (...);.** .*
1ee97 2a 20 54 68 65 6e 20 70 4e 61 6d 65 31 20 69 73  * Then pName1 is
1ee98 20 73 65 74 20 74 6f 20 22 78 78 78 22 20 61 6e   set to "xxx" an
1ee99 64 20 70 4e 61 6d 65 32 20 22 79 79 79 22 2e 20  d pName2 "yyy". 
1ee9a 4f 6e 20 74 68 65 20 6f 74 68 65 72 20 68 61 6e  On the other han
1ee9b 64 20 69 66 0a 2a 2a 20 74 68 65 20 74 61 62 6c  d if.** the tabl
1ee9c 65 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75  e name is not fu
1ee9d 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69  lly qualified, i
1ee9e 2e 65 2e 3a 0a 2a 2a 0a 2a 2a 20 43 52 45 41 54  .e.:.**.** CREAT
1ee9f 45 20 54 41 42 4c 45 20 79 79 79 28 2e 2e 2e 29  E TABLE yyy(...)
1eea0 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 70 4e 61  ;.**.** Then pNa
1eea1 6d 65 31 20 69 73 20 73 65 74 20 74 6f 20 22 79  me1 is set to "y
1eea2 79 79 22 20 61 6e 64 20 70 4e 61 6d 65 32 20 69  yy" and pName2 i
1eea3 73 20 22 22 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  s ""..**.** This
1eea4 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
1eea5 65 20 2a 70 70 55 6e 71 75 61 6c 20 70 6f 69 6e  e *ppUnqual poin
1eea6 74 65 72 20 74 6f 20 70 6f 69 6e 74 20 61 74 20  ter to point at 
1eea7 74 68 65 20 74 6f 6b 65 6e 20 28 70 4e 61 6d 65  the token (pName
1eea8 31 20 6f 72 0a 2a 2a 20 70 4e 61 6d 65 32 29 20  1 or.** pName2) 
1eea9 74 68 61 74 20 73 74 6f 72 65 73 20 74 68 65 20  that stores the 
1eeaa 75 6e 71 75 61 6c 69 66 69 65 64 20 74 61 62 6c  unqualified tabl
1eeab 65 20 6e 61 6d 65 2e 20 20 54 68 65 20 69 6e 64  e name.  The ind
1eeac 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 64 61 74  ex of the.** dat
1eead 61 62 61 73 65 20 22 78 78 78 22 20 69 73 20 72  abase "xxx" is r
1eeae 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
1eeaf 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
1eeb0 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61 6d  qlite3TwoPartNam
1eeb1 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
1eeb2 73 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73  se,      /* Pars
1eeb3 69 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e  ing and code gen
1eeb4 65 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20  erating context 
1eeb5 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d  */.  Token *pNam
1eeb6 65 31 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  e1,      /* The 
1eeb7 22 78 78 78 22 20 69 6e 20 74 68 65 20 6e 61 6d  "xxx" in the nam
1eeb8 65 20 22 78 78 78 2e 79 79 79 22 20 6f 72 20 22  e "xxx.yyy" or "
1eeb9 78 78 78 22 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  xxx" */.  Token 
1eeba 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 2f 2a  *pName2,      /*
1eebb 20 54 68 65 20 22 79 79 79 22 20 69 6e 20 74 68   The "yyy" in th
1eebc 65 20 6e 61 6d 65 20 22 78 78 78 2e 79 79 79 22  e name "xxx.yyy"
1eebd 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 2a 70 55   */.  Token **pU
1eebe 6e 71 75 61 6c 20 20 20 20 20 2f 2a 20 57 72 69  nqual     /* Wri
1eebf 74 65 20 74 68 65 20 75 6e 71 75 61 6c 69 66 69  te the unqualifi
1eec0 65 64 20 6f 62 6a 65 63 74 20 6e 61 6d 65 20 68  ed object name h
1eec1 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
1eec2 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
1eec3 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1eec4 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 65 20  ase holding the 
1eec5 6f 62 6a 65 63 74 20 2a 2f 0a 20 20 73 71 6c 69  object */.  sqli
1eec6 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1eec7 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 70 4e 61  ->db;..  if( pNa
1eec8 6d 65 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e  me2 && pName2->n
1eec9 3e 30 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62  >0 ){.    if( db
1eeca 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 20 7b 0a  ->init.busy ) {.
1eecb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1eecc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63  orMsg(pParse, "c
1eecd 6f 72 72 75 70 74 20 64 61 74 61 62 61 73 65 22  orrupt database"
1eece 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
1eecf 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72  >nErr++;.      r
1eed0 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20 20 7d 0a  eturn -1;.    }.
1eed1 20 20 20 20 2a 70 55 6e 71 75 61 6c 20 3d 20 70      *pUnqual = p
1eed2 4e 61 6d 65 32 3b 0a 20 20 20 20 69 44 62 20 3d  Name2;.    iDb =
1eed3 20 73 71 6c 69 74 65 33 46 69 6e 64 44 62 28 64   sqlite3FindDb(d
1eed4 62 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20  b, pName1);.    
1eed5 69 66 28 20 69 44 62 3c 30 20 29 7b 0a 20 20 20  if( iDb<0 ){.   
1eed6 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1eed7 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b 6e  sg(pParse, "unkn
1eed8 6f 77 6e 20 64 61 74 61 62 61 73 65 20 25 54 22  own database %T"
1eed9 2c 20 70 4e 61 6d 65 31 29 3b 0a 20 20 20 20 20  , pName1);.     
1eeda 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b   pParse->nErr++;
1eedb 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 2d 31  .      return -1
1eedc 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
1eedd 0a 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d  .    assert( db-
1eede 3e 69 6e 69 74 2e 69 44 62 3d 3d 30 20 7c 7c 20  >init.iDb==0 || 
1eedf 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29 3b  db->init.busy );
1eee0 0a 20 20 20 20 69 44 62 20 3d 20 64 62 2d 3e 69  .    iDb = db->i
1eee1 6e 69 74 2e 69 44 62 3b 0a 20 20 20 20 2a 70 55  nit.iDb;.    *pU
1eee2 6e 71 75 61 6c 20 3d 20 70 4e 61 6d 65 31 3b 0a  nqual = pName1;.
1eee3 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 69 44 62    }.  return iDb
1eee4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  ;.}../*.** This 
1eee5 72 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20  routine is used 
1eee6 74 6f 20 63 68 65 63 6b 20 69 66 20 74 68 65 20  to check if the 
1eee7 55 54 46 2d 38 20 73 74 72 69 6e 67 20 7a 4e 61  UTF-8 string zNa
1eee8 6d 65 20 69 73 20 61 20 6c 65 67 61 6c 0a 2a 2a  me is a legal.**
1eee9 20 75 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d   unqualified nam
1eeea 65 20 66 6f 72 20 61 20 6e 65 77 20 73 63 68 65  e for a new sche
1eeeb 6d 61 20 6f 62 6a 65 63 74 20 28 74 61 62 6c 65  ma object (table
1eeec 2c 20 69 6e 64 65 78 2c 20 76 69 65 77 20 6f 72  , index, view or
1eeed 0a 2a 2a 20 74 72 69 67 67 65 72 29 2e 20 41 6c  .** trigger). Al
1eeee 6c 20 6e 61 6d 65 73 20 61 72 65 20 6c 65 67 61  l names are lega
1eeef 6c 20 65 78 63 65 70 74 20 74 68 6f 73 65 20 74  l except those t
1eef0 68 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 74  hat begin with t
1eef1 68 65 20 73 74 72 69 6e 67 0a 2a 2a 20 22 73 71  he string.** "sq
1eef2 6c 69 74 65 5f 22 20 28 69 6e 20 75 70 70 65 72  lite_" (in upper
1eef3 2c 20 6c 6f 77 65 72 20 6f 72 20 6d 69 78 65 64  , lower or mixed
1eef4 20 63 61 73 65 29 2e 20 54 68 69 73 20 70 6f 72   case). This por
1eef5 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 61 6d 65  tion of the name
1eef6 73 70 61 63 65 0a 2a 2a 20 69 73 20 72 65 73 65  space.** is rese
1eef7 72 76 65 64 20 66 6f 72 20 69 6e 74 65 72 6e 61  rved for interna
1eef8 6c 20 75 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45  l use..*/.SQLITE
1eef9 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
1eefa 69 74 65 33 43 68 65 63 6b 4f 62 6a 65 63 74 4e  ite3CheckObjectN
1eefb 61 6d 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ame(Parse *pPars
1eefc 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  e, const char *z
1eefd 4e 61 6d 65 29 7b 0a 20 20 69 66 28 20 21 70 50  Name){.  if( !pP
1eefe 61 72 73 65 2d 3e 64 62 2d 3e 69 6e 69 74 2e 62  arse->db->init.b
1eeff 75 73 79 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  usy && pParse->n
1ef00 65 73 74 65 64 3d 3d 30 20 0a 20 20 20 20 20 20  ested==0 .      
1ef01 20 20 20 20 26 26 20 28 70 50 61 72 73 65 2d 3e      && (pParse->
1ef02 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
1ef03 54 45 5f 57 72 69 74 65 53 63 68 65 6d 61 29 3d  TE_WriteSchema)=
1ef04 3d 30 0a 20 20 20 20 20 20 20 20 20 20 26 26 20  =0.          && 
1ef05 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 4e 49 43  0==sqlite3StrNIC
1ef06 6d 70 28 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74  mp(zName, "sqlit
1ef07 65 5f 22 2c 20 37 29 20 29 7b 0a 20 20 20 20 73  e_", 7) ){.    s
1ef08 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1ef09 50 61 72 73 65 2c 20 22 6f 62 6a 65 63 74 20 6e  Parse, "object n
1ef0a 61 6d 65 20 72 65 73 65 72 76 65 64 20 66 6f 72  ame reserved for
1ef0b 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 3a 20 25   internal use: %
1ef0c 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
1ef0d 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
1ef0e 52 4f 52 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ROR;.  }.  retur
1ef0f 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
1ef10 2f 2a 0a 2a 2a 20 42 65 67 69 6e 20 63 6f 6e 73  /*.** Begin cons
1ef11 74 72 75 63 74 69 6e 67 20 61 20 6e 65 77 20 74  tructing a new t
1ef12 61 62 6c 65 20 72 65 70 72 65 73 65 6e 74 61 74  able representat
1ef13 69 6f 6e 20 69 6e 20 6d 65 6d 6f 72 79 2e 20 20  ion in memory.  
1ef14 54 68 69 73 20 69 73 0a 2a 2a 20 74 68 65 20 66  This is.** the f
1ef15 69 72 73 74 20 6f 66 20 73 65 76 65 72 61 6c 20  irst of several 
1ef16 61 63 74 69 6f 6e 20 72 6f 75 74 69 6e 65 73 20  action routines 
1ef17 74 68 61 74 20 67 65 74 20 63 61 6c 6c 65 64 20  that get called 
1ef18 69 6e 20 72 65 73 70 6f 6e 73 65 0a 2a 2a 20 74  in response.** t
1ef19 6f 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45  o a CREATE TABLE
1ef1a 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 49 6e 20   statement.  In 
1ef1b 70 61 72 74 69 63 75 6c 61 72 2c 20 74 68 69 73  particular, this
1ef1c 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
1ef1d 65 64 0a 2a 2a 20 61 66 74 65 72 20 73 65 65 69  ed.** after seei
1ef1e 6e 67 20 74 6f 6b 65 6e 73 20 22 43 52 45 41 54  ng tokens "CREAT
1ef1f 45 22 20 61 6e 64 20 22 54 41 42 4c 45 22 20 61  E" and "TABLE" a
1ef20 6e 64 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d  nd the table nam
1ef21 65 2e 20 54 68 65 20 69 73 54 65 6d 70 0a 2a 2a  e. The isTemp.**
1ef22 20 66 6c 61 67 20 69 73 20 74 72 75 65 20 69 66   flag is true if
1ef23 20 74 68 65 20 74 61 62 6c 65 20 73 68 6f 75 6c   the table shoul
1ef24 64 20 62 65 20 73 74 6f 72 65 64 20 69 6e 20 74  d be stored in t
1ef25 68 65 20 61 75 78 69 6c 69 61 72 79 20 64 61 74  he auxiliary dat
1ef26 61 62 61 73 65 0a 2a 2a 20 66 69 6c 65 20 69 6e  abase.** file in
1ef27 73 74 65 61 64 20 6f 66 20 69 6e 20 74 68 65 20  stead of in the 
1ef28 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20 66 69  main database fi
1ef29 6c 65 2e 20 20 54 68 69 73 20 69 73 20 6e 6f 72  le.  This is nor
1ef2a 6d 61 6c 6c 79 20 74 68 65 20 63 61 73 65 0a 2a  mally the case.*
1ef2b 2a 20 77 68 65 6e 20 74 68 65 20 22 54 45 4d 50  * when the "TEMP
1ef2c 22 20 6f 72 20 22 54 45 4d 50 4f 52 41 52 59 22  " or "TEMPORARY"
1ef2d 20 6b 65 79 77 6f 72 64 20 6f 63 63 75 72 73 20   keyword occurs 
1ef2e 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20 43 52  in between.** CR
1ef2f 45 41 54 45 20 61 6e 64 20 54 41 42 4c 45 2e 0a  EATE and TABLE..
1ef30 2a 2a 0a 2a 2a 20 54 68 65 20 6e 65 77 20 74 61  **.** The new ta
1ef31 62 6c 65 20 72 65 63 6f 72 64 20 69 73 20 69 6e  ble record is in
1ef32 69 74 69 61 6c 69 7a 65 64 20 61 6e 64 20 70 75  itialized and pu
1ef33 74 20 69 6e 20 70 50 61 72 73 65 2d 3e 70 4e 65  t in pParse->pNe
1ef34 77 54 61 62 6c 65 2e 0a 2a 2a 20 41 73 20 6d 6f  wTable..** As mo
1ef35 72 65 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  re of the CREATE
1ef36 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
1ef37 20 69 73 20 70 61 72 73 65 64 2c 20 61 64 64 69   is parsed, addi
1ef38 74 69 6f 6e 61 6c 20 61 63 74 69 6f 6e 0a 2a 2a  tional action.**
1ef39 20 72 6f 75 74 69 6e 65 73 20 77 69 6c 6c 20 62   routines will b
1ef3a 65 20 63 61 6c 6c 65 64 20 74 6f 20 61 64 64 20  e called to add 
1ef3b 6d 6f 72 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  more information
1ef3c 20 74 6f 20 74 68 69 73 20 72 65 63 6f 72 64 2e   to this record.
1ef3d 0a 2a 2a 20 41 74 20 74 68 65 20 65 6e 64 20 6f  .** At the end o
1ef3e 66 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  f the CREATE TAB
1ef3f 4c 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  LE statement, th
1ef40 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  e sqlite3EndTabl
1ef41 65 28 29 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 69  e() routine.** i
1ef42 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f 6d 70  s called to comp
1ef43 6c 65 74 65 20 74 68 65 20 63 6f 6e 73 74 72 75  lete the constru
1ef44 63 74 69 6f 6e 20 6f 66 20 74 68 65 20 6e 65 77  ction of the new
1ef45 20 74 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a   table record..*
1ef46 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
1ef47 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 74 61   void sqlite3Sta
1ef48 72 74 54 61 62 6c 65 28 0a 20 20 50 61 72 73 65  rtTable(.  Parse
1ef49 20 2a 70 50 61 72 73 65 2c 20 20 20 2f 2a 20 50   *pParse,   /* P
1ef4a 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f  arser context */
1ef4b 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31  .  Token *pName1
1ef4c 2c 20 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72  ,   /* First par
1ef4d 74 20 6f 66 20 74 68 65 20 6e 61 6d 65 20 6f 66  t of the name of
1ef4e 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76 69   the table or vi
1ef4f 65 77 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  ew */.  Token *p
1ef50 4e 61 6d 65 32 2c 20 20 20 2f 2a 20 53 65 63 6f  Name2,   /* Seco
1ef51 6e 64 20 70 61 72 74 20 6f 66 20 74 68 65 20 6e  nd part of the n
1ef52 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
1ef53 20 6f 72 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e   or view */.  in
1ef54 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 2f  t isTemp,      /
1ef55 2a 20 54 72 75 65 20 69 66 20 74 68 69 73 20 69  * True if this i
1ef56 73 20 61 20 54 45 4d 50 20 74 61 62 6c 65 20 2a  s a TEMP table *
1ef57 2f 0a 20 20 69 6e 74 20 69 73 56 69 65 77 2c 20  /.  int isView, 
1ef58 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
1ef59 74 68 69 73 20 69 73 20 61 20 56 49 45 57 20 2a  this is a VIEW *
1ef5a 2f 0a 20 20 69 6e 74 20 69 73 56 69 72 74 75 61  /.  int isVirtua
1ef5b 6c 2c 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  l,   /* True if 
1ef5c 74 68 69 73 20 69 73 20 61 20 56 49 52 54 55 41  this is a VIRTUA
1ef5d 4c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  L table */.  int
1ef5e 20 6e 6f 45 72 72 20 20 20 20 20 20 20 20 2f 2a   noErr        /*
1ef5f 20 44 6f 20 6e 6f 74 68 69 6e 67 20 69 66 20 74   Do nothing if t
1ef60 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78 69  able already exi
1ef61 73 74 73 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c  sts */.){.  Tabl
1ef62 65 20 2a 70 54 61 62 6c 65 3b 0a 20 20 63 68 61  e *pTable;.  cha
1ef63 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 2f 2a  r *zName = 0; /*
1ef64 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   The name of the
1ef65 20 6e 65 77 20 74 61 62 6c 65 20 2a 2f 0a 20 20   new table */.  
1ef66 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
1ef67 61 72 73 65 2d 3e 64 62 3b 0a 20 20 56 64 62 65  arse->db;.  Vdbe
1ef68 20 2a 76 3b 0a 20 20 69 6e 74 20 69 44 62 3b 20   *v;.  int iDb; 
1ef69 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
1ef6a 61 73 65 20 6e 75 6d 62 65 72 20 74 6f 20 63 72  ase number to cr
1ef6b 65 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 69  eate the table i
1ef6c 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e  n */.  Token *pN
1ef6d 61 6d 65 3b 20 20 20 20 2f 2a 20 55 6e 71 75 61  ame;    /* Unqua
1ef6e 6c 69 66 69 65 64 20 6e 61 6d 65 20 6f 66 20 74  lified name of t
1ef6f 68 65 20 74 61 62 6c 65 20 74 6f 20 63 72 65 61  he table to crea
1ef70 74 65 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  te */..  /* The 
1ef71 74 61 62 6c 65 20 6f 72 20 76 69 65 77 20 6e 61  table or view na
1ef72 6d 65 20 74 6f 20 63 72 65 61 74 65 20 69 73 20  me to create is 
1ef73 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 72  passed to this r
1ef74 6f 75 74 69 6e 65 20 76 69 61 20 74 6f 6b 65 6e  outine via token
1ef75 73 0a 20 20 2a 2a 20 70 4e 61 6d 65 31 20 61 6e  s.  ** pName1 an
1ef76 64 20 70 4e 61 6d 65 32 2e 20 49 66 20 74 68 65  d pName2. If the
1ef77 20 74 61 62 6c 65 20 6e 61 6d 65 20 77 61 73 20   table name was 
1ef78 66 75 6c 6c 79 20 71 75 61 6c 69 66 69 65 64 2c  fully qualified,
1ef79 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20 20   for example:.  
1ef7a 2a 2a 0a 20 20 2a 2a 20 43 52 45 41 54 45 20 54  **.  ** CREATE T
1ef7b 41 42 4c 45 20 78 78 78 2e 79 79 79 20 28 2e 2e  ABLE xxx.yyy (..
1ef7c 2e 29 3b 0a 20 20 2a 2a 20 0a 20 20 2a 2a 20 54  .);.  ** .  ** T
1ef7d 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65  hen pName1 is se
1ef7e 74 20 74 6f 20 22 78 78 78 22 20 61 6e 64 20 70  t to "xxx" and p
1ef7f 4e 61 6d 65 32 20 22 79 79 79 22 2e 20 4f 6e 20  Name2 "yyy". On 
1ef80 74 68 65 20 6f 74 68 65 72 20 68 61 6e 64 20 69  the other hand i
1ef81 66 0a 20 20 2a 2a 20 74 68 65 20 74 61 62 6c 65  f.  ** the table
1ef82 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 66 75 6c   name is not ful
1ef83 6c 79 20 71 75 61 6c 69 66 69 65 64 2c 20 69 2e  ly qualified, i.
1ef84 65 2e 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 43 52  e.:.  **.  ** CR
1ef85 45 41 54 45 20 54 41 42 4c 45 20 79 79 79 28 2e  EATE TABLE yyy(.
1ef86 2e 2e 29 3b 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54  ..);.  **.  ** T
1ef87 68 65 6e 20 70 4e 61 6d 65 31 20 69 73 20 73 65  hen pName1 is se
1ef88 74 20 74 6f 20 22 79 79 79 22 20 61 6e 64 20 70  t to "yyy" and p
1ef89 4e 61 6d 65 32 20 69 73 20 22 22 2e 0a 20 20 2a  Name2 is ""..  *
1ef8a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 61 6c 6c 20  *.  ** The call 
1ef8b 62 65 6c 6f 77 20 73 65 74 73 20 74 68 65 20 70  below sets the p
1ef8c 4e 61 6d 65 20 70 6f 69 6e 74 65 72 20 74 6f 20  Name pointer to 
1ef8d 70 6f 69 6e 74 20 61 74 20 74 68 65 20 74 6f 6b  point at the tok
1ef8e 65 6e 20 28 70 4e 61 6d 65 31 20 6f 72 0a 20 20  en (pName1 or.  
1ef8f 2a 2a 20 70 4e 61 6d 65 32 29 20 74 68 61 74 20  ** pName2) that 
1ef90 73 74 6f 72 65 73 20 74 68 65 20 75 6e 71 75 61  stores the unqua
1ef91 6c 69 66 69 65 64 20 74 61 62 6c 65 20 6e 61 6d  lified table nam
1ef92 65 2e 20 54 68 65 20 76 61 72 69 61 62 6c 65 20  e. The variable 
1ef93 69 44 62 20 69 73 0a 20 20 2a 2a 20 73 65 74 20  iDb is.  ** set 
1ef94 74 6f 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20  to the index of 
1ef95 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68 61  the database tha
1ef96 74 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20 76  t the table or v
1ef97 69 65 77 20 69 73 20 74 6f 20 62 65 0a 20 20 2a  iew is to be.  *
1ef98 2a 20 63 72 65 61 74 65 64 20 69 6e 2e 0a 20 20  * created in..  
1ef99 2a 2f 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  */.  iDb = sqlit
1ef9a 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
1ef9b 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
1ef9c 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20  ame2, &pName);. 
1ef9d 20 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74   if( iDb<0 ) ret
1ef9e 75 72 6e 3b 0a 20 20 69 66 28 20 21 4f 4d 49 54  urn;.  if( !OMIT
1ef9f 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54 65 6d  _TEMPDB && isTem
1efa0 70 20 26 26 20 69 44 62 3e 31 20 29 7b 0a 20 20  p && iDb>1 ){.  
1efa1 20 20 2f 2a 20 49 66 20 63 72 65 61 74 69 6e 67    /* If creating
1efa2 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 20 74   a temp table, t
1efa3 68 65 20 6e 61 6d 65 20 6d 61 79 20 6e 6f 74 20  he name may not 
1efa4 62 65 20 71 75 61 6c 69 66 69 65 64 20 2a 2f 0a  be qualified */.
1efa5 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
1efa6 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d  Msg(pParse, "tem
1efa7 70 6f 72 61 72 79 20 74 61 62 6c 65 20 6e 61 6d  porary table nam
1efa8 65 20 6d 75 73 74 20 62 65 20 75 6e 71 75 61 6c  e must be unqual
1efa9 69 66 69 65 64 22 29 3b 0a 20 20 20 20 72 65 74  ified");.    ret
1efaa 75 72 6e 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21  urn;.  }.  if( !
1efab 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
1efac 73 54 65 6d 70 20 29 20 69 44 62 20 3d 20 31 3b  sTemp ) iDb = 1;
1efad 0a 0a 20 20 70 50 61 72 73 65 2d 3e 73 4e 61 6d  ..  pParse->sNam
1efae 65 54 6f 6b 65 6e 20 3d 20 2a 70 4e 61 6d 65 3b  eToken = *pName;
1efaf 0a 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74  .  zName = sqlit
1efb0 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28  e3NameFromToken(
1efb1 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66  db, pName);.  if
1efb2 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 20 72 65 74  ( zName==0 ) ret
1efb3 75 72 6e 3b 0a 20 20 69 66 28 20 53 51 4c 49 54  urn;.  if( SQLIT
1efb4 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
1efb5 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
1efb6 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
1efb7 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61     goto begin_ta
1efb8 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 7d 0a 20  ble_error;.  }. 
1efb9 20 69 66 28 20 64 62 2d 3e 69 6e 69 74 2e 69 44   if( db->init.iD
1efba 62 3d 3d 31 20 29 20 69 73 54 65 6d 70 20 3d 20  b==1 ) isTemp = 
1efbb 31 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  1;.#ifndef SQLIT
1efbc 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41  E_OMIT_AUTHORIZA
1efbd 54 49 4f 4e 0a 20 20 61 73 73 65 72 74 28 20 28  TION.  assert( (
1efbe 69 73 54 65 6d 70 20 26 20 31 29 3d 3d 69 73 54  isTemp & 1)==isT
1efbf 65 6d 70 20 29 3b 0a 20 20 7b 0a 20 20 20 20 69  emp );.  {.    i
1efc0 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 68 61  nt code;.    cha
1efc1 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62  r *zDb = db->aDb
1efc2 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  [iDb].zName;.   
1efc3 20 69 66 28 20 73 71 6c 69 74 65 33 41 75 74 68   if( sqlite3Auth
1efc4 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
1efc5 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 53 43 48  LITE_INSERT, SCH
1efc6 45 4d 41 5f 54 41 42 4c 45 28 69 73 54 65 6d 70  EMA_TABLE(isTemp
1efc7 29 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  ), 0, zDb) ){.  
1efc8 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
1efc9 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20  able_error;.    
1efca 7d 0a 20 20 20 20 69 66 28 20 69 73 56 69 65 77  }.    if( isView
1efcb 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f   ){.      if( !O
1efcc 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73  MIT_TEMPDB && is
1efcd 54 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20  Temp ){.        
1efce 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
1efcf 45 41 54 45 5f 54 45 4d 50 5f 56 49 45 57 3b 0a  EATE_TEMP_VIEW;.
1efd0 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1efd1 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
1efd2 54 45 5f 43 52 45 41 54 45 5f 56 49 45 57 3b 0a  TE_CREATE_VIEW;.
1efd3 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73        }.    }els
1efd4 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d  e{.      if( !OM
1efd5 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 73 54  IT_TEMPDB && isT
1efd6 65 6d 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63  emp ){.        c
1efd7 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52 45  ode = SQLITE_CRE
1efd8 41 54 45 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a  ATE_TEMP_TABLE;.
1efd9 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
1efda 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
1efdb 54 45 5f 43 52 45 41 54 45 5f 54 41 42 4c 45 3b  TE_CREATE_TABLE;
1efdc 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
1efdd 20 20 20 69 66 28 20 21 69 73 56 69 72 74 75 61     if( !isVirtua
1efde 6c 20 26 26 20 73 71 6c 69 74 65 33 41 75 74 68  l && sqlite3Auth
1efdf 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f  Check(pParse, co
1efe0 64 65 2c 20 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44  de, zName, 0, zD
1efe1 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
1efe2 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
1efe3 6f 72 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  or;.    }.  }.#e
1efe4 6e 64 69 66 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  ndif..  /* Make 
1efe5 73 75 72 65 20 74 68 65 20 6e 65 77 20 74 61 62  sure the new tab
1efe6 6c 65 20 6e 61 6d 65 20 64 6f 65 73 20 6e 6f 74  le name does not
1efe7 20 63 6f 6c 6c 69 64 65 20 77 69 74 68 20 61 6e   collide with an
1efe8 20 65 78 69 73 74 69 6e 67 0a 20 20 2a 2a 20 69   existing.  ** i
1efe9 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 6e 61  ndex or table na
1efea 6d 65 20 69 6e 20 74 68 65 20 73 61 6d 65 20 64  me in the same d
1efeb 61 74 61 62 61 73 65 2e 20 20 49 73 73 75 65 20  atabase.  Issue 
1efec 61 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  an error message
1efed 20 69 66 0a 20 20 2a 2a 20 69 74 20 64 6f 65 73   if.  ** it does
1efee 2e 20 54 68 65 20 65 78 63 65 70 74 69 6f 6e 20  . The exception 
1efef 69 73 20 69 66 20 74 68 65 20 73 74 61 74 65 6d  is if the statem
1eff0 65 6e 74 20 62 65 69 6e 67 20 70 61 72 73 65 64  ent being parsed
1eff1 20 77 61 73 20 70 61 73 73 65 64 0a 20 20 2a 2a   was passed.  **
1eff2 20 74 6f 20 61 6e 20 73 71 6c 69 74 65 33 5f 64   to an sqlite3_d
1eff3 65 63 6c 61 72 65 5f 76 74 61 62 28 29 20 63 61  eclare_vtab() ca
1eff4 6c 6c 2e 20 49 6e 20 74 68 61 74 20 63 61 73 65  ll. In that case
1eff5 20 6f 6e 6c 79 20 74 68 65 20 63 6f 6c 75 6d 6e   only the column
1eff6 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 61 6e 64 20   names.  ** and 
1eff7 74 79 70 65 73 20 77 69 6c 6c 20 62 65 20 75 73  types will be us
1eff8 65 64 2c 20 73 6f 20 74 68 65 72 65 20 69 73 20  ed, so there is 
1eff9 6e 6f 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20  no need to test 
1effa 66 6f 72 20 6e 61 6d 65 73 70 61 63 65 0a 20 20  for namespace.  
1effb 2a 2a 20 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 20  ** collisions.. 
1effc 20 2a 2f 0a 20 20 69 66 28 20 21 49 4e 5f 44 45   */.  if( !IN_DE
1effd 43 4c 41 52 45 5f 56 54 41 42 20 29 7b 0a 20 20  CLARE_VTAB ){.  
1effe 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
1efff 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
1f000 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
1f001 20 20 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74      goto begin_t
1f002 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20  able_error;.    
1f003 7d 0a 20 20 20 20 70 54 61 62 6c 65 20 3d 20 73  }.    pTable = s
1f004 71 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28  qlite3FindTable(
1f005 64 62 2c 20 7a 4e 61 6d 65 2c 20 64 62 2d 3e 61  db, zName, db->a
1f006 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a  Db[iDb].zName);.
1f007 20 20 20 20 69 66 28 20 70 54 61 62 6c 65 20 29      if( pTable )
1f008 7b 0a 20 20 20 20 20 20 69 66 28 20 21 6e 6f 45  {.      if( !noE
1f009 72 72 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  rr ){.        sq
1f00a 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
1f00b 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25 54 20  arse, "table %T 
1f00c 61 6c 72 65 61 64 79 20 65 78 69 73 74 73 22 2c  already exists",
1f00d 20 70 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 7d   pName);.      }
1f00e 0a 20 20 20 20 20 20 67 6f 74 6f 20 62 65 67 69  .      goto begi
1f00f 6e 5f 74 61 62 6c 65 5f 65 72 72 6f 72 3b 0a 20  n_table_error;. 
1f010 20 20 20 7d 0a 20 20 20 20 69 66 28 20 73 71 6c     }.    if( sql
1f011 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28 64 62  ite3FindIndex(db
1f012 2c 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 26  , zName, 0)!=0 &
1f013 26 20 28 69 44 62 3d 3d 30 20 7c 7c 20 21 64 62  & (iDb==0 || !db
1f014 2d 3e 69 6e 69 74 2e 62 75 73 79 29 20 29 7b 0a  ->init.busy) ){.
1f015 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1f016 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
1f017 68 65 72 65 20 69 73 20 61 6c 72 65 61 64 79 20  here is already 
1f018 61 6e 20 69 6e 64 65 78 20 6e 61 6d 65 64 20 25  an index named %
1f019 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  s", zName);.    
1f01a 20 20 67 6f 74 6f 20 62 65 67 69 6e 5f 74 61 62    goto begin_tab
1f01b 6c 65 5f 65 72 72 6f 72 3b 0a 20 20 20 20 7d 0a  le_error;.    }.
1f01c 20 20 7d 0a 0a 20 20 70 54 61 62 6c 65 20 3d 20    }..  pTable = 
1f01d 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
1f01e 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 54  ero(db, sizeof(T
1f01f 61 62 6c 65 29 29 3b 0a 20 20 69 66 28 20 70 54  able));.  if( pT
1f020 61 62 6c 65 3d 3d 30 20 29 7b 0a 20 20 20 20 64  able==0 ){.    d
1f021 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
1f022 3d 20 31 3b 0a 20 20 20 20 70 50 61 72 73 65 2d  = 1;.    pParse-
1f023 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d  >rc = SQLITE_NOM
1f024 45 4d 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  EM;.    pParse->
1f025 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67 6f 74 6f  nErr++;.    goto
1f026 20 62 65 67 69 6e 5f 74 61 62 6c 65 5f 65 72 72   begin_table_err
1f027 6f 72 3b 0a 20 20 7d 0a 20 20 70 54 61 62 6c 65  or;.  }.  pTable
1f028 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b  ->zName = zName;
1f029 0a 20 20 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79  .  pTable->iPKey
1f02a 20 3d 20 2d 31 3b 0a 20 20 70 54 61 62 6c 65 2d   = -1;.  pTable-
1f02b 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e 61  >pSchema = db->a
1f02c 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 3b  Db[iDb].pSchema;
1f02d 0a 20 20 70 54 61 62 6c 65 2d 3e 6e 52 65 66 20  .  pTable->nRef 
1f02e 3d 20 31 3b 0a 20 20 70 54 61 62 6c 65 2d 3e 64  = 1;.  pTable->d
1f02f 62 20 3d 20 64 62 3b 0a 20 20 69 66 28 20 70 50  b = db;.  if( pP
1f030 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
1f031 29 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  ) sqlite3DeleteT
1f032 61 62 6c 65 28 70 50 61 72 73 65 2d 3e 70 4e 65  able(pParse->pNe
1f033 77 54 61 62 6c 65 29 3b 0a 20 20 70 50 61 72 73  wTable);.  pPars
1f034 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 3d 20 70  e->pNewTable = p
1f035 54 61 62 6c 65 3b 0a 0a 20 20 2f 2a 20 49 66 20  Table;..  /* If 
1f036 74 68 69 73 20 69 73 20 74 68 65 20 6d 61 67 69  this is the magi
1f037 63 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63  c sqlite_sequenc
1f038 65 20 74 61 62 6c 65 20 75 73 65 64 20 62 79 20  e table used by 
1f039 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74 2c 0a 20  autoincrement,. 
1f03a 20 2a 2a 20 74 68 65 6e 20 72 65 63 6f 72 64 20   ** then record 
1f03b 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69  a pointer to thi
1f03c 73 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 6d  s table in the m
1f03d 61 69 6e 20 64 61 74 61 62 61 73 65 20 73 74 72  ain database str
1f03e 75 63 74 75 72 65 0a 20 20 2a 2a 20 73 6f 20 74  ucture.  ** so t
1f03f 68 61 74 20 49 4e 53 45 52 54 20 63 61 6e 20 66  hat INSERT can f
1f040 69 6e 64 20 74 68 65 20 74 61 62 6c 65 20 65 61  ind the table ea
1f041 73 69 6c 79 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64  sily..  */.#ifnd
1f042 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f043 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 69  UTOINCREMENT.  i
1f044 66 28 20 21 70 50 61 72 73 65 2d 3e 6e 65 73 74  f( !pParse->nest
1f045 65 64 20 26 26 20 73 74 72 63 6d 70 28 7a 4e 61  ed && strcmp(zNa
1f046 6d 65 2c 20 22 73 71 6c 69 74 65 5f 73 65 71 75  me, "sqlite_sequ
1f047 65 6e 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  ence")==0 ){.   
1f048 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
1f049 2d 3e 70 53 65 71 54 61 62 20 3d 20 70 54 61 62  ->pSeqTab = pTab
1f04a 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  le;.  }.#endif..
1f04b 20 20 2f 2a 20 42 65 67 69 6e 20 67 65 6e 65 72    /* Begin gener
1f04c 61 74 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74  ating the code t
1f04d 68 61 74 20 77 69 6c 6c 20 69 6e 73 65 72 74 20  hat will insert 
1f04e 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72 64  the table record
1f04f 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65 20 53   into.  ** the S
1f050 51 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62  QLITE_MASTER tab
1f051 6c 65 2e 20 20 4e 6f 74 65 20 69 6e 20 70 61 72  le.  Note in par
1f052 74 69 63 75 6c 61 72 20 74 68 61 74 20 77 65 20  ticular that we 
1f053 6d 75 73 74 20 67 6f 20 61 68 65 61 64 0a 20 20  must go ahead.  
1f054 2a 2a 20 61 6e 64 20 61 6c 6c 6f 63 61 74 65 20  ** and allocate 
1f055 74 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65  the record numbe
1f056 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20  r for the table 
1f057 65 6e 74 72 79 20 6e 6f 77 2e 20 20 42 65 66 6f  entry now.  Befo
1f058 72 65 20 61 6e 79 0a 20 20 2a 2a 20 50 52 49 4d  re any.  ** PRIM
1f059 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
1f05a 45 20 6b 65 79 77 6f 72 64 73 20 61 72 65 20 70  E keywords are p
1f05b 61 72 73 65 64 2e 20 20 54 68 6f 73 65 20 6b 65  arsed.  Those ke
1f05c 79 77 6f 72 64 73 20 77 69 6c 6c 20 63 61 75 73  ywords will caus
1f05d 65 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 74  e.  ** indices t
1f05e 6f 20 62 65 20 63 72 65 61 74 65 64 20 61 6e 64  o be created and
1f05f 20 74 68 65 20 74 61 62 6c 65 20 72 65 63 6f 72   the table recor
1f060 64 20 6d 75 73 74 20 63 6f 6d 65 20 62 65 66 6f  d must come befo
1f061 72 65 20 74 68 65 20 0a 20 20 2a 2a 20 69 6e 64  re the .  ** ind
1f062 69 63 65 73 2e 20 20 48 65 6e 63 65 2c 20 74 68  ices.  Hence, th
1f063 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
1f064 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 6d 75  for the table mu
1f065 73 74 20 62 65 20 61 6c 6c 6f 63 61 74 65 64 0a  st be allocated.
1f066 20 20 2a 2a 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20    ** now..  */. 
1f067 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62   if( !db->init.b
1f068 75 73 79 20 26 26 20 28 76 20 3d 20 73 71 6c 69  usy && (v = sqli
1f069 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
1f06a 65 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 69 6e  e))!=0 ){.    in
1f06b 74 20 6a 31 3b 0a 20 20 20 20 69 6e 74 20 66 69  t j1;.    int fi
1f06c 6c 65 46 6f 72 6d 61 74 3b 0a 20 20 20 20 69 6e  leFormat;.    in
1f06d 74 20 72 65 67 31 2c 20 72 65 67 32 2c 20 72 65  t reg1, reg2, re
1f06e 67 33 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  g3;.    sqlite3B
1f06f 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
1f070 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
1f071 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  b);..#ifndef SQL
1f072 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
1f073 54 41 42 4c 45 0a 20 20 20 20 69 66 28 20 69 73  TABLE.    if( is
1f074 56 69 72 74 75 61 6c 20 29 7b 0a 20 20 20 20 20  Virtual ){.     
1f075 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f076 70 30 28 76 2c 20 4f 50 5f 56 42 65 67 69 6e 29  p0(v, OP_VBegin)
1f077 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  ;.    }.#endif..
1f078 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 66 69      /* If the fi
1f079 6c 65 20 66 6f 72 6d 61 74 20 61 6e 64 20 65 6e  le format and en
1f07a 63 6f 64 69 6e 67 20 69 6e 20 74 68 65 20 64 61  coding in the da
1f07b 74 61 62 61 73 65 20 68 61 76 65 20 6e 6f 74 20  tabase have not 
1f07c 62 65 65 6e 20 73 65 74 2c 20 0a 20 20 20 20 2a  been set, .    *
1f07d 2a 20 73 65 74 20 74 68 65 6d 20 6e 6f 77 2e 0a  * set them now..
1f07e 20 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 31 20      */.    reg1 
1f07f 3d 20 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 77  = pParse->regRow
1f080 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  id = ++pParse->n
1f081 4d 65 6d 3b 0a 20 20 20 20 72 65 67 32 20 3d 20  Mem;.    reg2 = 
1f082 70 50 61 72 73 65 2d 3e 72 65 67 52 6f 6f 74 20  pParse->regRoot 
1f083 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
1f084 3b 0a 20 20 20 20 72 65 67 33 20 3d 20 2b 2b 70  ;.    reg3 = ++p
1f085 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
1f086 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f087 70 33 28 76 2c 20 4f 50 5f 52 65 61 64 43 6f 6f  p3(v, OP_ReadCoo
1f088 6b 69 65 2c 20 69 44 62 2c 20 72 65 67 33 2c 20  kie, iDb, reg3, 
1f089 31 29 3b 20 20 20 2f 2a 20 66 69 6c 65 5f 66 6f  1);   /* file_fo
1f08a 72 6d 61 74 20 2a 2f 0a 20 20 20 20 73 71 6c 69  rmat */.    sqli
1f08b 74 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65  te3VdbeUsesBtree
1f08c 28 76 2c 20 69 44 62 29 3b 0a 20 20 20 20 6a 31  (v, iDb);.    j1
1f08d 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
1f08e 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp1(v, OP_If, r
1f08f 65 67 33 29 3b 0a 20 20 20 20 66 69 6c 65 46 6f  eg3);.    fileFo
1f090 72 6d 61 74 20 3d 20 28 64 62 2d 3e 66 6c 61 67  rmat = (db->flag
1f091 73 20 26 20 53 51 4c 49 54 45 5f 4c 65 67 61 63  s & SQLITE_Legac
1f092 79 46 69 6c 65 46 6d 74 29 21 3d 30 20 3f 0a 20  yFileFmt)!=0 ?. 
1f093 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f094 20 31 20 3a 20 53 51 4c 49 54 45 5f 4d 41 58 5f   1 : SQLITE_MAX_
1f095 46 49 4c 45 5f 46 4f 52 4d 41 54 3b 0a 20 20 20  FILE_FORMAT;.   
1f096 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f097 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
1f098 2c 20 66 69 6c 65 46 6f 72 6d 61 74 2c 20 72 65  , fileFormat, re
1f099 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  g3);.    sqlite3
1f09a 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
1f09b 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62 2c  _SetCookie, iDb,
1f09c 20 31 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73   1, reg3);.    s
1f09d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
1f09e 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
1f09f 45 4e 43 28 64 62 29 2c 20 72 65 67 33 29 3b 0a  ENC(db), reg3);.
1f0a0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1f0a1 64 64 4f 70 33 28 76 2c 20 4f 50 5f 53 65 74 43  ddOp3(v, OP_SetC
1f0a2 6f 6f 6b 69 65 2c 20 69 44 62 2c 20 34 2c 20 72  ookie, iDb, 4, r
1f0a3 65 67 33 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  eg3);.    sqlite
1f0a4 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
1f0a5 20 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 54 68   j1);..    /* Th
1f0a6 69 73 20 6a 75 73 74 20 63 72 65 61 74 65 73 20  is just creates 
1f0a7 61 20 70 6c 61 63 65 2d 68 6f 6c 64 65 72 20 72  a place-holder r
1f0a8 65 63 6f 72 64 20 69 6e 20 74 68 65 20 73 71 6c  ecord in the sql
1f0a9 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
1f0aa 2e 0a 20 20 20 20 2a 2a 20 54 68 65 20 72 65 63  ..    ** The rec
1f0ab 6f 72 64 20 63 72 65 61 74 65 64 20 64 6f 65 73  ord created does
1f0ac 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 61 6e 79   not contain any
1f0ad 74 68 69 6e 67 20 79 65 74 2e 20 20 49 74 20 77  thing yet.  It w
1f0ae 69 6c 6c 20 62 65 20 72 65 70 6c 61 63 65 64 0a  ill be replaced.
1f0af 20 20 20 20 2a 2a 20 62 79 20 74 68 65 20 72 65      ** by the re
1f0b0 61 6c 20 65 6e 74 72 79 20 69 6e 20 63 6f 64 65  al entry in code
1f0b1 20 67 65 6e 65 72 61 74 65 64 20 61 74 20 73 71   generated at sq
1f0b2 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 28 29 2e  lite3EndTable().
1f0b3 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54  .    **.    ** T
1f0b4 68 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65  he rowid for the
1f0b5 20 6e 65 77 20 65 6e 74 72 79 20 69 73 20 6c 65   new entry is le
1f0b6 66 74 20 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66  ft on the top of
1f0b7 20 74 68 65 20 73 74 61 63 6b 2e 0a 20 20 20 20   the stack..    
1f0b8 2a 2a 20 54 68 65 20 72 6f 77 69 64 20 76 61 6c  ** The rowid val
1f0b9 75 65 20 69 73 20 6e 65 65 64 65 64 20 62 79 20  ue is needed by 
1f0ba 74 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71  the code that sq
1f0bb 6c 69 74 65 33 45 6e 64 54 61 62 6c 65 20 77 69  lite3EndTable wi
1f0bc 6c 6c 0a 20 20 20 20 2a 2a 20 67 65 6e 65 72 61  ll.    ** genera
1f0bd 74 65 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 20 21  te..    */.#if !
1f0be 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1f0bf 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64 65  MIT_VIEW) || !de
1f0c0 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
1f0c1 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29 0a  T_VIRTUALTABLE).
1f0c2 20 20 20 20 69 66 28 20 69 73 56 69 65 77 20 7c      if( isView |
1f0c3 7c 20 69 73 56 69 72 74 75 61 6c 20 29 7b 0a 20  | isVirtual ){. 
1f0c4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f0c5 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
1f0c6 65 67 65 72 2c 20 30 2c 20 72 65 67 32 29 3b 0a  eger, 0, reg2);.
1f0c7 20 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66      }else.#endif
1f0c8 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c  .    {.      sql
1f0c9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f0ca 2c 20 4f 50 5f 43 72 65 61 74 65 54 61 62 6c 65  , OP_CreateTable
1f0cb 2c 20 69 44 62 2c 20 72 65 67 32 29 3b 0a 20 20  , iDb, reg2);.  
1f0cc 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 4f    }.    sqlite3O
1f0cd 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70  penMasterTable(p
1f0ce 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20  Parse, iDb);.   
1f0cf 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f0d0 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
1f0d1 64 2c 20 30 2c 20 72 65 67 31 29 3b 0a 20 20 20  d, 0, reg1);.   
1f0d2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
1f0d3 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
1f0d4 2c 20 72 65 67 33 29 3b 0a 20 20 20 20 73 71 6c  , reg3);.    sql
1f0d5 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1f0d6 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20 30 2c 20  , OP_Insert, 0, 
1f0d7 72 65 67 33 2c 20 72 65 67 31 29 3b 0a 20 20 20  reg3, reg1);.   
1f0d8 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
1f0d9 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
1f0da 50 50 45 4e 44 29 3b 0a 20 20 20 20 73 71 6c 69  PPEND);.    sqli
1f0db 74 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c  te3VdbeAddOp0(v,
1f0dc 20 4f 50 5f 43 6c 6f 73 65 29 3b 0a 20 20 7d 0a   OP_Close);.  }.
1f0dd 0a 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 28 6e 6f  .  /* Normal (no
1f0de 6e 2d 65 72 72 6f 72 29 20 72 65 74 75 72 6e 2e  n-error) return.
1f0df 20 2a 2f 0a 20 20 72 65 74 75 72 6e 3b 0a 0a 20   */.  return;.. 
1f0e0 20 2f 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20   /* If an error 
1f0e1 6f 63 63 75 72 73 2c 20 77 65 20 6a 75 6d 70 20  occurs, we jump 
1f0e2 68 65 72 65 20 2a 2f 0a 62 65 67 69 6e 5f 74 61  here */.begin_ta
1f0e3 62 6c 65 5f 65 72 72 6f 72 3a 0a 20 20 73 71 6c  ble_error:.  sql
1f0e4 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a  ite3DbFree(db, z
1f0e5 4e 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 3b  Name);.  return;
1f0e6 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 6d  .}../*.** This m
1f0e7 61 63 72 6f 20 69 73 20 75 73 65 64 20 74 6f 20  acro is used to 
1f0e8 63 6f 6d 70 61 72 65 20 74 77 6f 20 73 74 72 69  compare two stri
1f0e9 6e 67 73 20 69 6e 20 61 20 63 61 73 65 2d 69 6e  ngs in a case-in
1f0ea 73 65 6e 73 69 74 69 76 65 20 6d 61 6e 6e 65 72  sensitive manner
1f0eb 2e 0a 2a 2a 20 49 74 20 69 73 20 73 6c 69 67 68  ..** It is sligh
1f0ec 74 6c 79 20 66 61 73 74 65 72 20 74 68 61 6e 20  tly faster than 
1f0ed 63 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 53  calling sqlite3S
1f0ee 74 72 49 43 6d 70 28 29 20 64 69 72 65 63 74 6c  trICmp() directl
1f0ef 79 2c 20 62 75 74 0a 2a 2a 20 70 72 6f 64 75 63  y, but.** produc
1f0f0 65 73 20 6c 61 72 67 65 72 20 63 6f 64 65 2e 0a  es larger code..
1f0f1 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47 3a 20 54  **.** WARNING: T
1f0f2 68 69 73 20 6d 61 63 72 6f 20 69 73 20 6e 6f 74  his macro is not
1f0f3 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74 68   compatible with
1f0f4 20 74 68 65 20 73 74 72 63 6d 70 28 29 20 66 61   the strcmp() fa
1f0f5 6d 69 6c 79 2e 20 49 74 0a 2a 2a 20 72 65 74 75  mily. It.** retu
1f0f6 72 6e 73 20 74 72 75 65 20 69 66 20 74 68 65 20  rns true if the 
1f0f7 74 77 6f 20 73 74 72 69 6e 67 73 20 61 72 65 20  two strings are 
1f0f8 65 71 75 61 6c 2c 20 6f 74 68 65 72 77 69 73 65  equal, otherwise
1f0f9 20 66 61 6c 73 65 2e 0a 2a 2f 0a 23 64 65 66 69   false..*/.#defi
1f0fa 6e 65 20 53 54 52 49 43 4d 50 28 78 2c 20 79 29  ne STRICMP(x, y)
1f0fb 20 28 5c 0a 73 71 6c 69 74 65 33 55 70 70 65 72   (\.sqlite3Upper
1f0fc 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67 6e  ToLower[*(unsign
1f0fd 65 64 20 63 68 61 72 20 2a 29 28 78 29 5d 3d 3d  ed char *)(x)]==
1f0fe 20 20 20 5c 0a 73 71 6c 69 74 65 33 55 70 70 65     \.sqlite3Uppe
1f0ff 72 54 6f 4c 6f 77 65 72 5b 2a 28 75 6e 73 69 67  rToLower[*(unsig
1f100 6e 65 64 20 63 68 61 72 20 2a 29 28 79 29 5d 20  ned char *)(y)] 
1f101 20 20 20 20 5c 0a 26 26 20 73 71 6c 69 74 65 33      \.&& sqlite3
1f102 53 74 72 49 43 6d 70 28 28 78 29 2b 31 2c 28 79  StrICmp((x)+1,(y
1f103 29 2b 31 29 3d 3d 30 20 29 0a 0a 2f 2a 0a 2a 2a  )+1)==0 )../*.**
1f104 20 41 64 64 20 61 20 6e 65 77 20 63 6f 6c 75 6d   Add a new colum
1f105 6e 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63  n to the table c
1f106 75 72 72 65 6e 74 6c 79 20 62 65 69 6e 67 20 63  urrently being c
1f107 6f 6e 73 74 72 75 63 74 65 64 2e 0a 2a 2a 0a 2a  onstructed..**.*
1f108 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c  * The parser cal
1f109 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
1f10a 6f 6e 63 65 20 66 6f 72 20 65 61 63 68 20 63 6f  once for each co
1f10b 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f 6e  lumn declaration
1f10c 0a 2a 2a 20 69 6e 20 61 20 43 52 45 41 54 45 20  .** in a CREATE 
1f10d 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
1f10e 20 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61    sqlite3StartTa
1f10f 62 6c 65 28 29 20 67 65 74 73 20 63 61 6c 6c 65  ble() gets calle
1f110 64 0a 2a 2a 20 66 69 72 73 74 20 74 6f 20 67 65  d.** first to ge
1f111 74 20 74 68 69 6e 67 73 20 67 6f 69 6e 67 2e 20  t things going. 
1f112 20 54 68 65 6e 20 74 68 69 73 20 72 6f 75 74 69   Then this routi
1f113 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72  ne is called for
1f114 20 65 61 63 68 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e   each.** column.
1f115 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1f116 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 41  TE void sqlite3A
1f117 64 64 43 6f 6c 75 6d 6e 28 50 61 72 73 65 20 2a  ddColumn(Parse *
1f118 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
1f119 4e 61 6d 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Name){.  Table *
1f11a 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68  p;.  int i;.  ch
1f11b 61 72 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e 20  ar *z;.  Column 
1f11c 2a 70 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33  *pCol;.  sqlite3
1f11d 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1f11e 62 3b 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50  b;.  if( (p = pP
1f11f 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29  arse->pNewTable)
1f120 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 23 69  ==0 ) return;.#i
1f121 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  f SQLITE_MAX_COL
1f122 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 6e 43 6f  UMN.  if( p->nCo
1f123 6c 2b 31 3e 64 62 2d 3e 61 4c 69 6d 69 74 5b 53  l+1>db->aLimit[S
1f124 51 4c 49 54 45 5f 4c 49 4d 49 54 5f 43 4f 4c 55  QLITE_LIMIT_COLU
1f125 4d 4e 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  MN] ){.    sqlit
1f126 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
1f127 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 63 6f 6c  e, "too many col
1f128 75 6d 6e 73 20 6f 6e 20 25 73 22 2c 20 70 2d 3e  umns on %s", p->
1f129 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75  zName);.    retu
1f12a 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20  rn;.  }.#endif. 
1f12b 20 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65   z = sqlite3Name
1f12c 46 72 6f 6d 54 6f 6b 65 6e 28 70 50 61 72 73 65  FromToken(pParse
1f12d 2d 3e 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20  ->db, pName);.  
1f12e 69 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72  if( z==0 ) retur
1f12f 6e 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  n;.  for(i=0; i<
1f130 70 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20  p->nCol; i++){. 
1f131 20 20 20 69 66 28 20 53 54 52 49 43 4d 50 28 7a     if( STRICMP(z
1f132 2c 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61  , p->aCol[i].zNa
1f133 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  me) ){.      sql
1f134 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1f135 72 73 65 2c 20 22 64 75 70 6c 69 63 61 74 65 20  rse, "duplicate 
1f136 63 6f 6c 75 6d 6e 20 6e 61 6d 65 3a 20 25 73 22  column name: %s"
1f137 2c 20 7a 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , z);.      sqli
1f138 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
1f139 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
1f13a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20      }.  }.  if( 
1f13b 28 70 2d 3e 6e 43 6f 6c 20 26 20 30 78 37 29 3d  (p->nCol & 0x7)=
1f13c 3d 30 20 29 7b 0a 20 20 20 20 43 6f 6c 75 6d 6e  =0 ){.    Column
1f13d 20 2a 61 4e 65 77 3b 0a 20 20 20 20 61 4e 65 77   *aNew;.    aNew
1f13e 20 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c   = sqlite3DbReal
1f13f 6c 6f 63 28 70 50 61 72 73 65 2d 3e 64 62 2c 70  loc(pParse->db,p
1f140 2d 3e 61 43 6f 6c 2c 28 70 2d 3e 6e 43 6f 6c 2b  ->aCol,(p->nCol+
1f141 38 29 2a 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f  8)*sizeof(p->aCo
1f142 6c 5b 30 5d 29 29 3b 0a 20 20 20 20 69 66 28 20  l[0]));.    if( 
1f143 61 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20  aNew==0 ){.     
1f144 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1f145 62 2c 20 7a 29 3b 0a 20 20 20 20 20 20 72 65 74  b, z);.      ret
1f146 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  urn;.    }.    p
1f147 2d 3e 61 43 6f 6c 20 3d 20 61 4e 65 77 3b 0a 20  ->aCol = aNew;. 
1f148 20 7d 0a 20 20 70 43 6f 6c 20 3d 20 26 70 2d 3e   }.  pCol = &p->
1f149 61 43 6f 6c 5b 70 2d 3e 6e 43 6f 6c 5d 3b 0a 20  aCol[p->nCol];. 
1f14a 20 6d 65 6d 73 65 74 28 70 43 6f 6c 2c 20 30 2c   memset(pCol, 0,
1f14b 20 73 69 7a 65 6f 66 28 70 2d 3e 61 43 6f 6c 5b   sizeof(p->aCol[
1f14c 30 5d 29 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a 4e  0]));.  pCol->zN
1f14d 61 6d 65 20 3d 20 7a 3b 0a 20 0a 20 20 2f 2a 20  ame = z;. .  /* 
1f14e 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 74  If there is no t
1f14f 79 70 65 20 73 70 65 63 69 66 69 65 64 2c 20 63  ype specified, c
1f150 6f 6c 75 6d 6e 73 20 68 61 76 65 20 74 68 65 20  olumns have the 
1f151 64 65 66 61 75 6c 74 20 61 66 66 69 6e 69 74 79  default affinity
1f152 0a 20 20 2a 2a 20 27 4e 4f 4e 45 27 2e 20 49 66  .  ** 'NONE'. If
1f153 20 74 68 65 72 65 20 69 73 20 61 20 74 79 70 65   there is a type
1f154 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e   specified, then
1f155 20 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 75 6d   sqlite3AddColum
1f156 6e 54 79 70 65 28 29 20 77 69 6c 6c 0a 20 20 2a  nType() will.  *
1f157 2a 20 62 65 20 63 61 6c 6c 65 64 20 6e 65 78 74  * be called next
1f158 20 74 6f 20 73 65 74 20 70 43 6f 6c 2d 3e 61 66   to set pCol->af
1f159 66 69 6e 69 74 79 20 63 6f 72 72 65 63 74 6c 79  finity correctly
1f15a 2e 0a 20 20 2a 2f 0a 20 20 70 43 6f 6c 2d 3e 61  ..  */.  pCol->a
1f15b 66 66 69 6e 69 74 79 20 3d 20 53 51 4c 49 54 45  ffinity = SQLITE
1f15c 5f 41 46 46 5f 4e 4f 4e 45 3b 0a 20 20 70 2d 3e  _AFF_NONE;.  p->
1f15d 6e 43 6f 6c 2b 2b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  nCol++;.}../*.**
1f15e 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
1f15f 20 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70   called by the p
1f160 61 72 73 65 72 20 77 68 69 6c 65 20 69 6e 20 74  arser while in t
1f161 68 65 20 6d 69 64 64 6c 65 20 6f 66 0a 2a 2a 20  he middle of.** 
1f162 70 61 72 73 69 6e 67 20 61 20 43 52 45 41 54 45  parsing a CREATE
1f163 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74   TABLE statement
1f164 2e 20 20 41 20 22 4e 4f 54 20 4e 55 4c 4c 22 20  .  A "NOT NULL" 
1f165 63 6f 6e 73 74 72 61 69 6e 74 20 68 61 73 0a 2a  constraint has.*
1f166 2a 20 62 65 65 6e 20 73 65 65 6e 20 6f 6e 20 61  * been seen on a
1f167 20 63 6f 6c 75 6d 6e 2e 20 20 54 68 69 73 20 72   column.  This r
1f168 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68 65 20  outine sets the 
1f169 6e 6f 74 4e 75 6c 6c 20 66 6c 61 67 20 6f 6e 0a  notNull flag on.
1f16a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 63 75  ** the column cu
1f16b 72 72 65 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f  rrently under co
1f16c 6e 73 74 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53  nstruction..*/.S
1f16d 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f16e 69 64 20 73 71 6c 69 74 65 33 41 64 64 4e 6f 74  id sqlite3AddNot
1f16f 4e 75 6c 6c 28 50 61 72 73 65 20 2a 70 50 61 72  Null(Parse *pPar
1f170 73 65 2c 20 69 6e 74 20 6f 6e 45 72 72 6f 72 29  se, int onError)
1f171 7b 0a 20 20 54 61 62 6c 65 20 2a 70 3b 0a 20 20  {.  Table *p;.  
1f172 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 28 70 20  int i;.  if( (p 
1f173 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  = pParse->pNewTa
1f174 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75 72 6e  ble)==0 ) return
1f175 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f 6c 2d  ;.  i = p->nCol-
1f176 31 3b 0a 20 20 69 66 28 20 69 3e 3d 30 20 29 20  1;.  if( i>=0 ) 
1f177 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75  p->aCol[i].notNu
1f178 6c 6c 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a 7d 0a  ll = onError;.}.
1f179 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20 74 68 65 20  ./*.** Scan the 
1f17a 63 6f 6c 75 6d 6e 20 74 79 70 65 20 6e 61 6d 65  column type name
1f17b 20 7a 54 79 70 65 20 28 6c 65 6e 67 74 68 20 6e   zType (length n
1f17c 54 79 70 65 29 20 61 6e 64 20 72 65 74 75 72 6e  Type) and return
1f17d 20 74 68 65 0a 2a 2a 20 61 73 73 6f 63 69 61 74   the.** associat
1f17e 65 64 20 61 66 66 69 6e 69 74 79 20 74 79 70 65  ed affinity type
1f17f 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ..**.** This rou
1f180 74 69 6e 65 20 64 6f 65 73 20 61 20 63 61 73 65  tine does a case
1f181 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 73 65 61  -independent sea
1f182 72 63 68 20 6f 66 20 7a 54 79 70 65 20 66 6f 72  rch of zType for
1f183 20 74 68 65 20 0a 2a 2a 20 73 75 62 73 74 72 69   the .** substri
1f184 6e 67 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  ngs in the follo
1f185 77 69 6e 67 20 74 61 62 6c 65 2e 20 49 66 20 6f  wing table. If o
1f186 6e 65 20 6f 66 20 74 68 65 20 73 75 62 73 74 72  ne of the substr
1f187 69 6e 67 73 20 69 73 0a 2a 2a 20 66 6f 75 6e 64  ings is.** found
1f188 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  , the correspond
1f189 69 6e 67 20 61 66 66 69 6e 69 74 79 20 69 73 20  ing affinity is 
1f18a 72 65 74 75 72 6e 65 64 2e 20 49 66 20 7a 54 79  returned. If zTy
1f18b 70 65 20 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 6d  pe contains.** m
1f18c 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 6f 66 20  ore than one of 
1f18d 74 68 65 20 73 75 62 73 74 72 69 6e 67 73 2c 20  the substrings, 
1f18e 65 6e 74 72 69 65 73 20 74 6f 77 61 72 64 20 74  entries toward t
1f18f 68 65 20 74 6f 70 20 6f 66 20 0a 2a 2a 20 74 68  he top of .** th
1f190 65 20 74 61 62 6c 65 20 74 61 6b 65 20 70 72 69  e table take pri
1f191 6f 72 69 74 79 2e 20 46 6f 72 20 65 78 61 6d 70  ority. For examp
1f192 6c 65 2c 20 69 66 20 7a 54 79 70 65 20 69 73 20  le, if zType is 
1f193 27 42 4c 4f 42 49 4e 54 27 2c 20 0a 2a 2a 20 53  'BLOBINT', .** S
1f194 51 4c 49 54 45 5f 41 46 46 5f 49 4e 54 45 47 45  QLITE_AFF_INTEGE
1f195 52 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a  R is returned..*
1f196 2a 0a 2a 2a 20 53 75 62 73 74 72 69 6e 67 20 20  *.** Substring  
1f197 20 20 20 7c 20 41 66 66 69 6e 69 74 79 0a 2a 2a     | Affinity.**
1f198 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
1f199 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
1f19a 2d 0a 2a 2a 20 27 49 4e 54 27 20 20 20 20 20 20  -.** 'INT'      
1f19b 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f     | SQLITE_AFF_
1f19c 49 4e 54 45 47 45 52 0a 2a 2a 20 27 43 48 41 52  INTEGER.** 'CHAR
1f19d 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54  '        | SQLIT
1f19e 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a 20 27 43  E_AFF_TEXT.** 'C
1f19f 4c 4f 42 27 20 20 20 20 20 20 20 20 7c 20 53 51  LOB'        | SQ
1f1a0 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 0a 2a 2a  LITE_AFF_TEXT.**
1f1a1 20 27 54 45 58 54 27 20 20 20 20 20 20 20 20 7c   'TEXT'        |
1f1a2 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45 58 54   SQLITE_AFF_TEXT
1f1a3 0a 2a 2a 20 27 42 4c 4f 42 27 20 20 20 20 20 20  .** 'BLOB'      
1f1a4 20 20 7c 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e    | SQLITE_AFF_N
1f1a5 4f 4e 45 0a 2a 2a 20 27 52 45 41 4c 27 20 20 20  ONE.** 'REAL'   
1f1a6 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 41 46       | SQLITE_AF
1f1a7 46 5f 52 45 41 4c 0a 2a 2a 20 27 46 4c 4f 41 27  F_REAL.** 'FLOA'
1f1a8 20 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45          | SQLITE
1f1a9 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 20 27 44 4f  _AFF_REAL.** 'DO
1f1aa 55 42 27 20 20 20 20 20 20 20 20 7c 20 53 51 4c  UB'        | SQL
1f1ab 49 54 45 5f 41 46 46 5f 52 45 41 4c 0a 2a 2a 0a  ITE_AFF_REAL.**.
1f1ac 2a 2a 20 49 66 20 6e 6f 6e 65 20 6f 66 20 74 68  ** If none of th
1f1ad 65 20 73 75 62 73 74 72 69 6e 67 73 20 69 6e 20  e substrings in 
1f1ae 74 68 65 20 61 62 6f 76 65 20 74 61 62 6c 65 20  the above table 
1f1af 61 72 65 20 66 6f 75 6e 64 2c 0a 2a 2a 20 53 51  are found,.** SQ
1f1b0 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43  LITE_AFF_NUMERIC
1f1b1 20 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2f   is returned..*/
1f1b2 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f1b3 63 68 61 72 20 73 71 6c 69 74 65 33 41 66 66 69  char sqlite3Affi
1f1b4 6e 69 74 79 54 79 70 65 28 63 6f 6e 73 74 20 54  nityType(const T
1f1b5 6f 6b 65 6e 20 2a 70 54 79 70 65 29 7b 0a 20 20  oken *pType){.  
1f1b6 75 33 32 20 68 20 3d 20 30 3b 0a 20 20 63 68 61  u32 h = 0;.  cha
1f1b7 72 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41  r aff = SQLITE_A
1f1b8 46 46 5f 4e 55 4d 45 52 49 43 3b 0a 20 20 63 6f  FF_NUMERIC;.  co
1f1b9 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
1f1ba 72 20 2a 7a 49 6e 20 3d 20 70 54 79 70 65 2d 3e  r *zIn = pType->
1f1bb 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  z;.  const unsig
1f1bc 6e 65 64 20 63 68 61 72 20 2a 7a 45 6e 64 20 3d  ned char *zEnd =
1f1bd 20 26 70 54 79 70 65 2d 3e 7a 5b 70 54 79 70 65   &pType->z[pType
1f1be 2d 3e 6e 5d 3b 0a 0a 20 20 77 68 69 6c 65 28 20  ->n];..  while( 
1f1bf 7a 49 6e 21 3d 7a 45 6e 64 20 29 7b 0a 20 20 20  zIn!=zEnd ){.   
1f1c0 20 68 20 3d 20 28 68 3c 3c 38 29 20 2b 20 73 71   h = (h<<8) + sq
1f1c1 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
1f1c2 72 5b 2a 7a 49 6e 5d 3b 0a 20 20 20 20 7a 49 6e  r[*zIn];.    zIn
1f1c3 2b 2b 3b 0a 20 20 20 20 69 66 28 20 68 3d 3d 28  ++;.    if( h==(
1f1c4 28 27 63 27 3c 3c 32 34 29 2b 28 27 68 27 3c 3c  ('c'<<24)+('h'<<
1f1c5 31 36 29 2b 28 27 61 27 3c 3c 38 29 2b 27 72 27  16)+('a'<<8)+'r'
1f1c6 29 20 29 7b 20 20 20 20 20 20 20 20 20 20 20 20  ) ){            
1f1c7 20 2f 2a 20 43 48 41 52 20 2a 2f 0a 20 20 20 20   /* CHAR */.    
1f1c8 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
1f1c9 46 46 5f 54 45 58 54 3b 20 0a 20 20 20 20 7d 65  FF_TEXT; .    }e
1f1ca 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27 63 27  lse if( h==(('c'
1f1cb 3c 3c 32 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b  <<24)+('l'<<16)+
1f1cc 28 27 6f 27 3c 3c 38 29 2b 27 62 27 29 20 29 7b  ('o'<<8)+'b') ){
1f1cd 20 20 20 20 20 20 20 2f 2a 20 43 4c 4f 42 20 2a         /* CLOB *
1f1ce 2f 0a 20 20 20 20 20 20 61 66 66 20 3d 20 53 51  /.      aff = SQ
1f1cf 4c 49 54 45 5f 41 46 46 5f 54 45 58 54 3b 0a 20  LITE_AFF_TEXT;. 
1f1d0 20 20 20 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d     }else if( h==
1f1d1 28 28 27 74 27 3c 3c 32 34 29 2b 28 27 65 27 3c  (('t'<<24)+('e'<
1f1d2 3c 31 36 29 2b 28 27 78 27 3c 3c 38 29 2b 27 74  <16)+('x'<<8)+'t
1f1d3 27 29 20 29 7b 20 20 20 20 20 20 20 2f 2a 20 54  ') ){       /* T
1f1d4 45 58 54 20 2a 2f 0a 20 20 20 20 20 20 61 66 66  EXT */.      aff
1f1d5 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 54 45   = SQLITE_AFF_TE
1f1d6 58 54 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  XT;.    }else if
1f1d7 28 20 68 3d 3d 28 28 27 62 27 3c 3c 32 34 29 2b  ( h==(('b'<<24)+
1f1d8 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f 27 3c 3c  ('l'<<16)+('o'<<
1f1d9 38 29 2b 27 62 27 29 20 20 20 20 20 20 20 20 20  8)+'b')         
1f1da 20 2f 2a 20 42 4c 4f 42 20 2a 2f 0a 20 20 20 20   /* BLOB */.    
1f1db 20 20 20 20 26 26 20 28 61 66 66 3d 3d 53 51 4c      && (aff==SQL
1f1dc 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20  ITE_AFF_NUMERIC 
1f1dd 7c 7c 20 61 66 66 3d 3d 53 51 4c 49 54 45 5f 41  || aff==SQLITE_A
1f1de 46 46 5f 52 45 41 4c 29 20 29 7b 0a 20 20 20 20  FF_REAL) ){.    
1f1df 20 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41    aff = SQLITE_A
1f1e0 46 46 5f 4e 4f 4e 45 3b 0a 23 69 66 6e 64 65 66  FF_NONE;.#ifndef
1f1e1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f   SQLITE_OMIT_FLO
1f1e2 41 54 49 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20  ATING_POINT.    
1f1e3 7d 65 6c 73 65 20 69 66 28 20 68 3d 3d 28 28 27  }else if( h==(('
1f1e4 72 27 3c 3c 32 34 29 2b 28 27 65 27 3c 3c 31 36  r'<<24)+('e'<<16
1f1e5 29 2b 28 27 61 27 3c 3c 38 29 2b 27 6c 27 29 20  )+('a'<<8)+'l') 
1f1e6 20 20 20 20 20 20 20 20 20 2f 2a 20 52 45 41 4c           /* REAL
1f1e7 20 2a 2f 0a 20 20 20 20 20 20 20 20 26 26 20 61   */.        && a
1f1e8 66 66 3d 3d 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ff==SQLITE_AFF_N
1f1e9 55 4d 45 52 49 43 20 29 7b 0a 20 20 20 20 20 20  UMERIC ){.      
1f1ea 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46 46  aff = SQLITE_AFF
1f1eb 5f 52 45 41 4c 3b 0a 20 20 20 20 7d 65 6c 73 65  _REAL;.    }else
1f1ec 20 69 66 28 20 68 3d 3d 28 28 27 66 27 3c 3c 32   if( h==(('f'<<2
1f1ed 34 29 2b 28 27 6c 27 3c 3c 31 36 29 2b 28 27 6f  4)+('l'<<16)+('o
1f1ee 27 3c 3c 38 29 2b 27 61 27 29 20 20 20 20 20 20  '<<8)+'a')      
1f1ef 20 20 20 20 2f 2a 20 46 4c 4f 41 20 2a 2f 0a 20      /* FLOA */. 
1f1f0 20 20 20 20 20 20 20 26 26 20 61 66 66 3d 3d 53         && aff==S
1f1f1 51 4c 49 54 45 5f 41 46 46 5f 4e 55 4d 45 52 49  QLITE_AFF_NUMERI
1f1f2 43 20 29 7b 0a 20 20 20 20 20 20 61 66 66 20 3d  C ){.      aff =
1f1f3 20 53 51 4c 49 54 45 5f 41 46 46 5f 52 45 41 4c   SQLITE_AFF_REAL
1f1f4 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20  ;.    }else if( 
1f1f5 68 3d 3d 28 28 27 64 27 3c 3c 32 34 29 2b 28 27  h==(('d'<<24)+('
1f1f6 6f 27 3c 3c 31 36 29 2b 28 27 75 27 3c 3c 38 29  o'<<16)+('u'<<8)
1f1f7 2b 27 62 27 29 20 20 20 20 20 20 20 20 20 20 2f  +'b')          /
1f1f8 2a 20 44 4f 55 42 20 2a 2f 0a 20 20 20 20 20 20  * DOUB */.      
1f1f9 20 20 26 26 20 61 66 66 3d 3d 53 51 4c 49 54 45    && aff==SQLITE
1f1fa 5f 41 46 46 5f 4e 55 4d 45 52 49 43 20 29 7b 0a  _AFF_NUMERIC ){.
1f1fb 20 20 20 20 20 20 61 66 66 20 3d 20 53 51 4c 49        aff = SQLI
1f1fc 54 45 5f 41 46 46 5f 52 45 41 4c 3b 0a 23 65 6e  TE_AFF_REAL;.#en
1f1fd 64 69 66 0a 20 20 20 20 7d 65 6c 73 65 20 69 66  dif.    }else if
1f1fe 28 20 28 68 26 30 78 30 30 46 46 46 46 46 46 29  ( (h&0x00FFFFFF)
1f1ff 3d 3d 28 28 27 69 27 3c 3c 31 36 29 2b 28 27 6e  ==(('i'<<16)+('n
1f200 27 3c 3c 38 29 2b 27 74 27 29 20 29 7b 20 20 20  '<<8)+'t') ){   
1f201 20 2f 2a 20 49 4e 54 20 2a 2f 0a 20 20 20 20 20   /* INT */.     
1f202 20 61 66 66 20 3d 20 53 51 4c 49 54 45 5f 41 46   aff = SQLITE_AF
1f203 46 5f 49 4e 54 45 47 45 52 3b 0a 20 20 20 20 20  F_INTEGER;.     
1f204 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1f205 7d 0a 0a 20 20 72 65 74 75 72 6e 20 61 66 66 3b  }..  return aff;
1f206 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1f207 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1f208 20 62 79 20 74 68 65 20 70 61 72 73 65 72 20 77   by the parser w
1f209 68 69 6c 65 20 69 6e 20 74 68 65 20 6d 69 64 64  hile in the midd
1f20a 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73 69 6e 67  le of.** parsing
1f20b 20 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20   a CREATE TABLE 
1f20c 73 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 65 20  statement.  The 
1f20d 70 46 69 72 73 74 20 74 6f 6b 65 6e 20 69 73 20  pFirst token is 
1f20e 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 74 6f 6b  the first.** tok
1f20f 65 6e 20 69 6e 20 74 68 65 20 73 65 71 75 65 6e  en in the sequen
1f210 63 65 20 6f 66 20 74 6f 6b 65 6e 73 20 74 68 61  ce of tokens tha
1f211 74 20 64 65 73 63 72 69 62 65 20 74 68 65 20 74  t describe the t
1f212 79 70 65 20 6f 66 20 74 68 65 0a 2a 2a 20 63 6f  ype of the.** co
1f213 6c 75 6d 6e 20 63 75 72 72 65 6e 74 6c 79 20 75  lumn currently u
1f214 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f  nder constructio
1f215 6e 2e 20 20 20 70 4c 61 73 74 20 69 73 20 74 68  n.   pLast is th
1f216 65 20 6c 61 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20  e last token.** 
1f217 69 6e 20 74 68 65 20 73 65 71 75 65 6e 63 65 2e  in the sequence.
1f218 20 20 55 73 65 20 74 68 69 73 20 69 6e 66 6f 72    Use this infor
1f219 6d 61 74 69 6f 6e 20 74 6f 20 63 6f 6e 73 74 72  mation to constr
1f21a 75 63 74 20 61 20 73 74 72 69 6e 67 0a 2a 2a 20  uct a string.** 
1f21b 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 20 74 68  that contains th
1f21c 65 20 74 79 70 65 6e 61 6d 65 20 6f 66 20 74 68  e typename of th
1f21d 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20 73 74 6f  e column and sto
1f21e 72 65 20 74 68 61 74 20 73 74 72 69 6e 67 0a 2a  re that string.*
1f21f 2a 20 69 6e 20 7a 54 79 70 65 2e 0a 2a 2f 20 0a  * in zType..*/ .
1f220 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
1f221 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 43 6f  oid sqlite3AddCo
1f222 6c 75 6d 6e 54 79 70 65 28 50 61 72 73 65 20 2a  lumnType(Parse *
1f223 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e 20 2a 70  pParse, Token *p
1f224 54 79 70 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a  Type){.  Table *
1f225 70 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 43 6f  p;.  int i;.  Co
1f226 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a 20 20 73 71  lumn *pCol;.  sq
1f227 6c 69 74 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66  lite3 *db;..  if
1f228 28 20 28 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  ( (p = pParse->p
1f229 4e 65 77 54 61 62 6c 65 29 3d 3d 30 20 29 20 72  NewTable)==0 ) r
1f22a 65 74 75 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e  eturn;.  i = p->
1f22b 6e 43 6f 6c 2d 31 3b 0a 20 20 69 66 28 20 69 3c  nCol-1;.  if( i<
1f22c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70 43  0 ) return;.  pC
1f22d 6f 6c 20 3d 20 26 70 2d 3e 61 43 6f 6c 5b 69 5d  ol = &p->aCol[i]
1f22e 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  ;.  db = pParse-
1f22f 3e 64 62 3b 0a 20 20 73 71 6c 69 74 65 33 44 62  >db;.  sqlite3Db
1f230 46 72 65 65 28 64 62 2c 20 70 43 6f 6c 2d 3e 7a  Free(db, pCol->z
1f231 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e 7a  Type);.  pCol->z
1f232 54 79 70 65 20 3d 20 73 71 6c 69 74 65 33 4e 61  Type = sqlite3Na
1f233 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
1f234 70 54 79 70 65 29 3b 0a 20 20 70 43 6f 6c 2d 3e  pType);.  pCol->
1f235 61 66 66 69 6e 69 74 79 20 3d 20 73 71 6c 69 74  affinity = sqlit
1f236 65 33 41 66 66 69 6e 69 74 79 54 79 70 65 28 70  e3AffinityType(p
1f237 54 79 70 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  Type);.}../*.** 
1f238 54 68 65 20 65 78 70 72 65 73 73 69 6f 6e 20 69  The expression i
1f239 73 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61  s the default va
1f23a 6c 75 65 20 66 6f 72 20 74 68 65 20 6d 6f 73 74  lue for the most
1f23b 20 72 65 63 65 6e 74 6c 79 20 61 64 64 65 64 20   recently added 
1f23c 63 6f 6c 75 6d 6e 0a 2a 2a 20 6f 66 20 74 68 65  column.** of the
1f23d 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79   table currently
1f23e 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74   under construct
1f23f 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 44 65 66 61 75  ion..**.** Defau
1f240 6c 74 20 76 61 6c 75 65 20 65 78 70 72 65 73 73  lt value express
1f241 69 6f 6e 73 20 6d 75 73 74 20 62 65 20 63 6f 6e  ions must be con
1f242 73 74 61 6e 74 2e 20 20 52 61 69 73 65 20 61 6e  stant.  Raise an
1f243 20 65 78 63 65 70 74 69 6f 6e 20 69 66 20 74 68   exception if th
1f244 69 73 0a 2a 2a 20 69 73 20 6e 6f 74 20 74 68 65  is.** is not the
1f245 20 63 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69   case..**.** Thi
1f246 73 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c  s routine is cal
1f247 6c 65 64 20 62 79 20 74 68 65 20 70 61 72 73 65  led by the parse
1f248 72 20 77 68 69 6c 65 20 69 6e 20 74 68 65 20 6d  r while in the m
1f249 69 64 64 6c 65 20 6f 66 0a 2a 2a 20 70 61 72 73  iddle of.** pars
1f24a 69 6e 67 20 61 20 43 52 45 41 54 45 20 54 41 42  ing a CREATE TAB
1f24b 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f  LE statement..*/
1f24c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f24d 76 6f 69 64 20 73 71 6c 69 74 65 33 41 64 64 44  void sqlite3AddD
1f24e 65 66 61 75 6c 74 56 61 6c 75 65 28 50 61 72 73  efaultValue(Pars
1f24f 65 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 20  e *pParse, Expr 
1f250 2a 70 45 78 70 72 29 7b 0a 20 20 54 61 62 6c 65  *pExpr){.  Table
1f251 20 2a 70 3b 0a 20 20 43 6f 6c 75 6d 6e 20 2a 70   *p;.  Column *p
1f252 43 6f 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  Col;.  sqlite3 *
1f253 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1f254 0a 20 20 69 66 28 20 28 70 20 3d 20 70 50 61 72  .  if( (p = pPar
1f255 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 29 21 3d  se->pNewTable)!=
1f256 30 20 29 7b 0a 20 20 20 20 70 43 6f 6c 20 3d 20  0 ){.    pCol = 
1f257 26 28 70 2d 3e 61 43 6f 6c 5b 70 2d 3e 6e 43 6f  &(p->aCol[p->nCo
1f258 6c 2d 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 21  l-1]);.    if( !
1f259 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
1f25a 73 74 61 6e 74 4f 72 46 75 6e 63 74 69 6f 6e 28  stantOrFunction(
1f25b 70 45 78 70 72 29 20 29 7b 0a 20 20 20 20 20 20  pExpr) ){.      
1f25c 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f25d 70 50 61 72 73 65 2c 20 22 64 65 66 61 75 6c 74  pParse, "default
1f25e 20 76 61 6c 75 65 20 6f 66 20 63 6f 6c 75 6d 6e   value of column
1f25f 20 5b 25 73 5d 20 69 73 20 6e 6f 74 20 63 6f 6e   [%s] is not con
1f260 73 74 61 6e 74 22 2c 0a 20 20 20 20 20 20 20 20  stant",.        
1f261 20 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a    pCol->zName);.
1f262 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f263 20 45 78 70 72 20 2a 70 43 6f 70 79 3b 0a 20 20   Expr *pCopy;.  
1f264 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 44      sqlite3ExprD
1f265 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c 2d 3e  elete(db, pCol->
1f266 70 44 66 6c 74 29 3b 0a 20 20 20 20 20 20 70 43  pDflt);.      pC
1f267 6f 6c 2d 3e 70 44 66 6c 74 20 3d 20 70 43 6f 70  ol->pDflt = pCop
1f268 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  y = sqlite3ExprD
1f269 75 70 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20  up(db, pExpr);. 
1f26a 20 20 20 20 20 69 66 28 20 70 43 6f 70 79 20 29       if( pCopy )
1f26b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1f26c 33 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26  3TokenCopy(db, &
1f26d 70 43 6f 70 79 2d 3e 73 70 61 6e 2c 20 26 70 45  pCopy->span, &pE
1f26e 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20  xpr->span);.    
1f26f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
1f270 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
1f271 65 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 7d 0a  e(db, pExpr);.}.
1f272 0a 2f 2a 0a 2a 2a 20 44 65 73 69 67 6e 61 74 65  ./*.** Designate
1f273 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
1f274 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20   for the table. 
1f275 20 70 4c 69 73 74 20 69 73 20 61 20 6c 69 73 74   pList is a list
1f276 20 6f 66 20 6e 61 6d 65 73 20 0a 2a 2a 20 6f 66   of names .** of
1f277 20 63 6f 6c 75 6d 6e 73 20 74 68 61 74 20 66 6f   columns that fo
1f278 72 6d 20 74 68 65 20 70 72 69 6d 61 72 79 20 6b  rm the primary k
1f279 65 79 2e 20 20 49 66 20 70 4c 69 73 74 20 69 73  ey.  If pList is
1f27a 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 74 68 65 0a   NULL, then the.
1f27b 2a 2a 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79  ** most recently
1f27c 20 61 64 64 65 64 20 63 6f 6c 75 6d 6e 20 6f 66   added column of
1f27d 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   the table is th
1f27e 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 0a 2a  e primary key..*
1f27f 2a 0a 2a 2a 20 41 20 74 61 62 6c 65 20 63 61 6e  *.** A table can
1f280 20 68 61 76 65 20 61 74 20 6d 6f 73 74 20 6f 6e   have at most on
1f281 65 20 70 72 69 6d 61 72 79 20 6b 65 79 2e 20 20  e primary key.  
1f282 49 66 20 74 68 65 20 74 61 62 6c 65 20 61 6c 72  If the table alr
1f283 65 61 64 79 20 68 61 73 0a 2a 2a 20 61 20 70 72  eady has.** a pr
1f284 69 6d 61 72 79 20 6b 65 79 20 28 61 6e 64 20 74  imary key (and t
1f285 68 69 73 20 69 73 20 74 68 65 20 73 65 63 6f 6e  his is the secon
1f286 64 20 70 72 69 6d 61 72 79 20 6b 65 79 29 20 74  d primary key) t
1f287 68 65 6e 20 63 72 65 61 74 65 20 61 6e 0a 2a 2a  hen create an.**
1f288 20 65 72 72 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66   error..**.** If
1f289 20 74 68 65 20 50 52 49 4d 41 52 59 20 4b 45 59   the PRIMARY KEY
1f28a 20 69 73 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20   is on a single 
1f28b 63 6f 6c 75 6d 6e 20 77 68 6f 73 65 20 64 61 74  column whose dat
1f28c 61 74 79 70 65 20 69 73 20 49 4e 54 45 47 45 52  atype is INTEGER
1f28d 2c 0a 2a 2a 20 74 68 65 6e 20 77 65 20 77 69 6c  ,.** then we wil
1f28e 6c 20 74 72 79 20 74 6f 20 75 73 65 20 74 68 61  l try to use tha
1f28f 74 20 63 6f 6c 75 6d 6e 20 61 73 20 74 68 65 20  t column as the 
1f290 72 6f 77 69 64 2e 20 20 53 65 74 20 74 68 65 20  rowid.  Set the 
1f291 54 61 62 6c 65 2e 69 50 4b 65 79 0a 2a 2a 20 66  Table.iPKey.** f
1f292 69 65 6c 64 20 6f 66 20 74 68 65 20 74 61 62 6c  ield of the tabl
1f293 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63  e under construc
1f294 74 69 6f 6e 20 74 6f 20 62 65 20 74 68 65 20 69  tion to be the i
1f295 6e 64 65 78 20 6f 66 20 74 68 65 0a 2a 2a 20 49  ndex of the.** I
1f296 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
1f297 45 59 20 63 6f 6c 75 6d 6e 2e 20 20 54 61 62 6c  EY column.  Tabl
1f298 65 2e 69 50 4b 65 79 20 69 73 20 73 65 74 20 74  e.iPKey is set t
1f299 6f 20 2d 31 20 69 66 20 74 68 65 72 65 20 69 73  o -1 if there is
1f29a 0a 2a 2a 20 6e 6f 20 49 4e 54 45 47 45 52 20 50  .** no INTEGER P
1f29b 52 49 4d 41 52 59 20 4b 45 59 2e 0a 2a 2a 0a 2a  RIMARY KEY..**.*
1f29c 2a 20 49 66 20 74 68 65 20 6b 65 79 20 69 73 20  * If the key is 
1f29d 6e 6f 74 20 61 6e 20 49 4e 54 45 47 45 52 20 50  not an INTEGER P
1f29e 52 49 4d 41 52 59 20 4b 45 59 2c 20 74 68 65 6e  RIMARY KEY, then
1f29f 20 63 72 65 61 74 65 20 61 20 75 6e 69 71 75 65   create a unique
1f2a0 0a 2a 2a 20 69 6e 64 65 78 20 66 6f 72 20 74 68  .** index for th
1f2a1 65 20 6b 65 79 2e 20 20 4e 6f 20 69 6e 64 65 78  e key.  No index
1f2a2 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72 20   is created for 
1f2a3 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20  INTEGER PRIMARY 
1f2a4 4b 45 59 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  KEYs..*/.SQLITE_
1f2a5 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1f2a6 69 74 65 33 41 64 64 50 72 69 6d 61 72 79 4b 65  ite3AddPrimaryKe
1f2a7 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  y(.  Parse *pPar
1f2a8 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  se,    /* Parsin
1f2a9 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45  g context */.  E
1f2aa 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20  xprList *pList, 
1f2ab 20 2f 2a 20 4c 69 73 74 20 6f 66 20 66 69 65 6c   /* List of fiel
1f2ac 64 20 6e 61 6d 65 73 20 74 6f 20 62 65 20 69 6e  d names to be in
1f2ad 64 65 78 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  dexed */.  int o
1f2ae 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 2f 2a 20  nError,      /* 
1f2af 57 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20  What to do with 
1f2b0 61 20 75 6e 69 71 75 65 6e 65 73 73 20 63 6f 6e  a uniqueness con
1f2b1 66 6c 69 63 74 20 2a 2f 0a 20 20 69 6e 74 20 61  flict */.  int a
1f2b2 75 74 6f 49 6e 63 2c 20 20 20 20 20 20 2f 2a 20  utoInc,      /* 
1f2b3 54 72 75 65 20 69 66 20 74 68 65 20 41 55 54 4f  True if the AUTO
1f2b4 49 4e 43 52 45 4d 45 4e 54 20 6b 65 79 77 6f 72  INCREMENT keywor
1f2b5 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a 2f 0a  d is present */.
1f2b6 20 20 69 6e 74 20 73 6f 72 74 4f 72 64 65 72 20    int sortOrder 
1f2b7 20 20 20 20 2f 2a 20 53 51 4c 49 54 45 5f 53 4f      /* SQLITE_SO
1f2b8 5f 41 53 43 20 6f 72 20 53 51 4c 49 54 45 5f 53  _ASC or SQLITE_S
1f2b9 4f 5f 44 45 53 43 20 2a 2f 0a 29 7b 0a 20 20 54  O_DESC */.){.  T
1f2ba 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
1f2bb 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
1f2bc 20 20 63 68 61 72 20 2a 7a 54 79 70 65 20 3d 20    char *zType = 
1f2bd 30 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20  0;.  int iCol = 
1f2be 2d 31 2c 20 69 3b 0a 20 20 69 66 28 20 70 54 61  -1, i;.  if( pTa
1f2bf 62 3d 3d 30 20 7c 7c 20 49 4e 5f 44 45 43 4c 41  b==0 || IN_DECLA
1f2c0 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f 20 70  RE_VTAB ) goto p
1f2c1 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78 69 74 3b  rimary_key_exit;
1f2c2 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62  .  if( pTab->tab
1f2c3 46 6c 61 67 73 20 26 20 54 46 5f 48 61 73 50 72  Flags & TF_HasPr
1f2c4 69 6d 61 72 79 4b 65 79 20 29 7b 0a 20 20 20 20  imaryKey ){.    
1f2c5 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f2c6 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 22  pParse, .      "
1f2c7 74 61 62 6c 65 20 5c 22 25 73 5c 22 20 68 61 73  table \"%s\" has
1f2c8 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 70   more than one p
1f2c9 72 69 6d 61 72 79 20 6b 65 79 22 2c 20 70 54 61  rimary key", pTa
1f2ca 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
1f2cb 6f 74 6f 20 70 72 69 6d 61 72 79 5f 6b 65 79 5f  oto primary_key_
1f2cc 65 78 69 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62  exit;.  }.  pTab
1f2cd 2d 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46  ->tabFlags |= TF
1f2ce 5f 48 61 73 50 72 69 6d 61 72 79 4b 65 79 3b 0a  _HasPrimaryKey;.
1f2cf 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29    if( pList==0 )
1f2d0 7b 0a 20 20 20 20 69 43 6f 6c 20 3d 20 70 54 61  {.    iCol = pTa
1f2d1 62 2d 3e 6e 43 6f 6c 20 2d 20 31 3b 0a 20 20 20  b->nCol - 1;.   
1f2d2 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c   pTab->aCol[iCol
1f2d3 5d 2e 69 73 50 72 69 6d 4b 65 79 20 3d 20 31 3b  ].isPrimKey = 1;
1f2d4 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 66 6f  .  }else{.    fo
1f2d5 72 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e  r(i=0; i<pList->
1f2d6 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nExpr; i++){.   
1f2d7 20 20 20 66 6f 72 28 69 43 6f 6c 3d 30 3b 20 69     for(iCol=0; i
1f2d8 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20  Col<pTab->nCol; 
1f2d9 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 20  iCol++){.       
1f2da 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49   if( sqlite3StrI
1f2db 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  Cmp(pList->a[i].
1f2dc 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 61 43 6f  zName, pTab->aCo
1f2dd 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 29 3d 3d  l[iCol].zName)==
1f2de 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 62  0 ){.          b
1f2df 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
1f2e0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
1f2e1 28 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43 6f  ( iCol<pTab->nCo
1f2e2 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70 54 61  l ){.        pTa
1f2e3 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 69 73  b->aCol[iCol].is
1f2e4 50 72 69 6d 4b 65 79 20 3d 20 31 3b 0a 20 20 20  PrimKey = 1;.   
1f2e5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1f2e6 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3e  f( pList->nExpr>
1f2e7 31 20 29 20 69 43 6f 6c 20 3d 20 2d 31 3b 0a 20  1 ) iCol = -1;. 
1f2e8 20 7d 0a 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30   }.  if( iCol>=0
1f2e9 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e   && iCol<pTab->n
1f2ea 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 54 79 70 65  Col ){.    zType
1f2eb 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43   = pTab->aCol[iC
1f2ec 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 7d 0a 20  ol].zType;.  }. 
1f2ed 20 69 66 28 20 7a 54 79 70 65 20 26 26 20 73 71   if( zType && sq
1f2ee 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 79  lite3StrICmp(zTy
1f2ef 70 65 2c 20 22 49 4e 54 45 47 45 52 22 29 3d 3d  pe, "INTEGER")==
1f2f0 30 0a 20 20 20 20 20 20 20 20 26 26 20 73 6f 72  0.        && sor
1f2f1 74 4f 72 64 65 72 3d 3d 53 51 4c 49 54 45 5f 53  tOrder==SQLITE_S
1f2f2 4f 5f 41 53 43 20 29 7b 0a 20 20 20 20 70 54 61  O_ASC ){.    pTa
1f2f3 62 2d 3e 69 50 4b 65 79 20 3d 20 69 43 6f 6c 3b  b->iPKey = iCol;
1f2f4 0a 20 20 20 20 70 54 61 62 2d 3e 6b 65 79 43 6f  .    pTab->keyCo
1f2f5 6e 66 20 3d 20 6f 6e 45 72 72 6f 72 3b 0a 20 20  nf = onError;.  
1f2f6 20 20 61 73 73 65 72 74 28 20 61 75 74 6f 49 6e    assert( autoIn
1f2f7 63 3d 3d 30 20 7c 7c 20 61 75 74 6f 49 6e 63 3d  c==0 || autoInc=
1f2f8 3d 31 20 29 3b 0a 20 20 20 20 70 54 61 62 2d 3e  =1 );.    pTab->
1f2f9 74 61 62 46 6c 61 67 73 20 7c 3d 20 61 75 74 6f  tabFlags |= auto
1f2fa 49 6e 63 2a 54 46 5f 41 75 74 6f 69 6e 63 72 65  Inc*TF_Autoincre
1f2fb 6d 65 6e 74 3b 0a 20 20 7d 65 6c 73 65 20 69 66  ment;.  }else if
1f2fc 28 20 61 75 74 6f 49 6e 63 20 29 7b 0a 23 69 66  ( autoInc ){.#if
1f2fd 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f2fe 5f 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20  _AUTOINCREMENT. 
1f2ff 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f300 73 67 28 70 50 61 72 73 65 2c 20 22 41 55 54 4f  sg(pParse, "AUTO
1f301 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 6f 6e 6c  INCREMENT is onl
1f302 79 20 61 6c 6c 6f 77 65 64 20 6f 6e 20 61 6e 20  y allowed on an 
1f303 22 0a 20 20 20 20 20 20 20 22 49 4e 54 45 47 45  ".       "INTEGE
1f304 52 20 50 52 49 4d 41 52 59 20 4b 45 59 22 29 3b  R PRIMARY KEY");
1f305 0a 23 65 6e 64 69 66 0a 20 20 7d 65 6c 73 65 7b  .#endif.  }else{
1f306 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61  .    sqlite3Crea
1f307 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  teIndex(pParse, 
1f308 30 2c 20 30 2c 20 30 2c 20 70 4c 69 73 74 2c 20  0, 0, 0, pList, 
1f309 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 30 2c 20 73  onError, 0, 0, s
1f30a 6f 72 74 4f 72 64 65 72 2c 20 30 29 3b 0a 20 20  ortOrder, 0);.  
1f30b 20 20 70 4c 69 73 74 20 3d 20 30 3b 0a 20 20 7d    pList = 0;.  }
1f30c 0a 0a 70 72 69 6d 61 72 79 5f 6b 65 79 5f 65 78  ..primary_key_ex
1f30d 69 74 3a 0a 20 20 73 71 6c 69 74 65 33 45 78 70  it:.  sqlite3Exp
1f30e 72 4c 69 73 74 44 65 6c 65 74 65 28 70 50 61 72  rListDelete(pPar
1f30f 73 65 2d 3e 64 62 2c 20 70 4c 69 73 74 29 3b 0a  se->db, pList);.
1f310 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a    return;.}../*.
1f311 2a 2a 20 41 64 64 20 61 20 6e 65 77 20 43 48 45  ** Add a new CHE
1f312 43 4b 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 6f  CK constraint to
1f313 20 74 68 65 20 74 61 62 6c 65 20 63 75 72 72 65   the table curre
1f314 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
1f315 72 75 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49  ruction..*/.SQLI
1f316 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
1f317 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b 43  sqlite3AddCheckC
1f318 6f 6e 73 74 72 61 69 6e 74 28 0a 20 20 50 61 72  onstraint(.  Par
1f319 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 2f  se *pParse,    /
1f31a 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78  * Parsing contex
1f31b 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 43 68  t */.  Expr *pCh
1f31c 65 63 6b 45 78 70 72 20 20 2f 2a 20 54 68 65 20  eckExpr  /* The 
1f31d 63 68 65 63 6b 20 65 78 70 72 65 73 73 69 6f 6e  check expression
1f31e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33   */.){.  sqlite3
1f31f 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1f320 62 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  b;.#ifndef SQLIT
1f321 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20 20 54  E_OMIT_CHECK.  T
1f322 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 50 61  able *pTab = pPa
1f323 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
1f324 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 49    if( pTab && !I
1f325 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41 42 20 29  N_DECLARE_VTAB )
1f326 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 43 48 45  {.    /* The CHE
1f327 43 4b 20 65 78 70 72 65 73 73 69 6f 6e 20 6d 75  CK expression mu
1f328 73 74 20 62 65 20 64 75 70 6c 69 63 61 74 65 64  st be duplicated
1f329 20 73 6f 20 74 68 61 74 20 74 6f 6b 65 6e 73 20   so that tokens 
1f32a 72 65 66 65 72 0a 20 20 20 20 2a 2a 20 74 6f 20  refer.    ** to 
1f32b 6d 61 6c 6c 6f 63 65 64 20 73 70 61 63 65 20 61  malloced space a
1f32c 6e 64 20 6e 6f 74 20 74 68 65 20 28 65 70 68 65  nd not the (ephe
1f32d 6d 65 72 61 6c 29 20 74 65 78 74 20 6f 66 20 74  meral) text of t
1f32e 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45 0a  he CREATE TABLE.
1f32f 20 20 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74      ** statement
1f330 20 2a 2f 0a 20 20 20 20 70 54 61 62 2d 3e 70 43   */.    pTab->pC
1f331 68 65 63 6b 20 3d 20 73 71 6c 69 74 65 33 45 78  heck = sqlite3Ex
1f332 70 72 41 6e 64 28 64 62 2c 20 70 54 61 62 2d 3e  prAnd(db, pTab->
1f333 70 43 68 65 63 6b 2c 20 0a 20 20 20 20 20 20 20  pCheck, .       
1f334 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f335 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
1f336 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 43  e3ExprDup(db, pC
1f337 68 65 63 6b 45 78 70 72 29 29 3b 0a 20 20 7d 0a  heckExpr));.  }.
1f338 23 65 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33  #endif.  sqlite3
1f339 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
1f33a 43 68 65 63 6b 45 78 70 72 29 3b 0a 7d 0a 0a 2f  CheckExpr);.}../
1f33b 2a 0a 2a 2a 20 53 65 74 20 74 68 65 20 63 6f 6c  *.** Set the col
1f33c 6c 61 74 69 6f 6e 20 66 75 6e 63 74 69 6f 6e 20  lation function 
1f33d 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65  of the most rece
1f33e 6e 74 6c 79 20 70 61 72 73 65 64 20 74 61 62 6c  ntly parsed tabl
1f33f 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 74  e column.** to t
1f340 68 65 20 43 6f 6c 6c 53 65 71 20 67 69 76 65 6e  he CollSeq given
1f341 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
1f342 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
1f343 41 64 64 43 6f 6c 6c 61 74 65 54 79 70 65 28 50  AddCollateType(P
1f344 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f  arse *pParse, To
1f345 6b 65 6e 20 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20  ken *pToken){.  
1f346 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
1f347 69 3b 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6c 6c  i;.  char *zColl
1f348 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ;              /
1f349 2a 20 44 65 71 75 6f 74 65 64 20 6e 61 6d 65 20  * Dequoted name 
1f34a 6f 66 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  of collation seq
1f34b 75 65 6e 63 65 20 2a 2f 0a 20 20 73 71 6c 69 74  uence */.  sqlit
1f34c 65 33 20 2a 64 62 3b 0a 0a 20 20 69 66 28 20 28  e3 *db;..  if( (
1f34d 70 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  p = pParse->pNew
1f34e 54 61 62 6c 65 29 3d 3d 30 20 29 20 72 65 74 75  Table)==0 ) retu
1f34f 72 6e 3b 0a 20 20 69 20 3d 20 70 2d 3e 6e 43 6f  rn;.  i = p->nCo
1f350 6c 2d 31 3b 0a 20 20 64 62 20 3d 20 70 50 61 72  l-1;.  db = pPar
1f351 73 65 2d 3e 64 62 3b 0a 20 20 7a 43 6f 6c 6c 20  se->db;.  zColl 
1f352 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  = sqlite3NameFro
1f353 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f 6b 65  mToken(db, pToke
1f354 6e 29 3b 0a 20 20 69 66 28 20 21 7a 43 6f 6c 6c  n);.  if( !zColl
1f355 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66   ) return;..  if
1f356 28 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43  ( sqlite3LocateC
1f357 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a  ollSeq(pParse, z
1f358 43 6f 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20  Coll, -1) ){.   
1f359 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20   Index *pIdx;.  
1f35a 20 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f    p->aCol[i].zCo
1f35b 6c 6c 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20 0a 20  ll = zColl;.  . 
1f35c 20 20 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c     /* If the col
1f35d 75 6d 6e 20 69 73 20 64 65 63 6c 61 72 65 64 20  umn is declared 
1f35e 61 73 20 22 3c 6e 61 6d 65 3e 20 50 52 49 4d 41  as "<name> PRIMA
1f35f 52 59 20 4b 45 59 20 43 4f 4c 4c 41 54 45 20 3c  RY KEY COLLATE <
1f360 74 79 70 65 3e 22 2c 0a 20 20 20 20 2a 2a 20 74  type>",.    ** t
1f361 68 65 6e 20 61 6e 20 69 6e 64 65 78 20 6d 61 79  hen an index may
1f362 20 68 61 76 65 20 62 65 65 6e 20 63 72 65 61 74   have been creat
1f363 65 64 20 6f 6e 20 74 68 69 73 20 63 6f 6c 75 6d  ed on this colum
1f364 6e 20 62 65 66 6f 72 65 20 74 68 65 0a 20 20 20  n before the.   
1f365 20 2a 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 74 79   ** collation ty
1f366 70 65 20 77 61 73 20 61 64 64 65 64 2e 20 43 6f  pe was added. Co
1f367 72 72 65 63 74 20 74 68 69 73 20 69 66 20 69 74  rrect this if it
1f368 20 69 73 20 74 68 65 20 63 61 73 65 2e 0a 20 20   is the case..  
1f369 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 70 49 64    */.    for(pId
1f36a 78 3d 70 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  x=p->pIndex; pId
1f36b 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
1f36c 65 78 74 29 7b 0a 20 20 20 20 20 20 61 73 73 65  ext){.      asse
1f36d 72 74 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  rt( pIdx->nColum
1f36e 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 69 66  n==1 );.      if
1f36f 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e  ( pIdx->aiColumn
1f370 5b 30 5d 3d 3d 69 20 29 7b 0a 20 20 20 20 20 20  [0]==i ){.      
1f371 20 20 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 30    pIdx->azColl[0
1f372 5d 20 3d 20 70 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a  ] = p->aCol[i].z
1f373 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
1f374 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20    }.  }else{.   
1f375 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
1f376 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 7d 0a 7d  b, zColl);.  }.}
1f377 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e  ../*.** This fun
1f378 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 74 68  ction returns th
1f379 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  e collation sequ
1f37a 65 6e 63 65 20 66 6f 72 20 64 61 74 61 62 61 73  ence for databas
1f37b 65 20 6e 61 74 69 76 65 20 74 65 78 74 0a 2a 2a  e native text.**
1f37c 20 65 6e 63 6f 64 69 6e 67 20 69 64 65 6e 74 69   encoding identi
1f37d 66 69 65 64 20 62 79 20 74 68 65 20 73 74 72 69  fied by the stri
1f37e 6e 67 20 7a 4e 61 6d 65 2c 20 6c 65 6e 67 74 68  ng zName, length
1f37f 20 6e 4e 61 6d 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   nName..**.** If
1f380 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20 63   the requested c
1f381 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
1f382 65 20 69 73 20 6e 6f 74 20 61 76 61 69 6c 61 62  e is not availab
1f383 6c 65 2c 20 6f 72 20 6e 6f 74 20 61 76 61 69 6c  le, or not avail
1f384 61 62 6c 65 0a 2a 2a 20 69 6e 20 74 68 65 20 64  able.** in the d
1f385 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 20 65  atabase native e
1f386 6e 63 6f 64 69 6e 67 2c 20 74 68 65 20 63 6f 6c  ncoding, the col
1f387 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72 79 20 69  lation factory i
1f388 73 20 69 6e 76 6f 6b 65 64 20 74 6f 0a 2a 2a 20  s invoked to.** 
1f389 72 65 71 75 65 73 74 20 69 74 2e 20 49 66 20 74  request it. If t
1f38a 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63  he collation fac
1f38b 74 6f 72 79 20 64 6f 65 73 20 6e 6f 74 20 73 75  tory does not su
1f38c 70 70 6c 79 20 73 75 63 68 20 61 20 73 65 71 75  pply such a sequ
1f38d 65 6e 63 65 2c 0a 2a 2a 20 61 6e 64 20 74 68 65  ence,.** and the
1f38e 20 73 65 71 75 65 6e 63 65 20 69 73 20 61 76 61   sequence is ava
1f38f 69 6c 61 62 6c 65 20 69 6e 20 61 6e 6f 74 68 65  ilable in anothe
1f390 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 2c  r text encoding,
1f391 20 74 68 65 6e 20 74 68 61 74 20 69 73 0a 2a 2a   then that is.**
1f392 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74 65 61   returned instea
1f393 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 76  d..**.** If no v
1f394 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 72  ersions of the r
1f395 65 71 75 65 73 74 65 64 20 63 6f 6c 6c 61 74 69  equested collati
1f396 6f 6e 73 20 73 65 71 75 65 6e 63 65 20 61 72 65  ons sequence are
1f397 20 61 76 61 69 6c 61 62 6c 65 2c 20 6f 72 0a 2a   available, or.*
1f398 2a 20 61 6e 6f 74 68 65 72 20 65 72 72 6f 72 20  * another error 
1f399 6f 63 63 75 72 73 2c 20 4e 55 4c 4c 20 69 73 20  occurs, NULL is 
1f39a 72 65 74 75 72 6e 65 64 20 61 6e 64 20 61 6e 20  returned and an 
1f39b 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 72  error message wr
1f39c 69 74 74 65 6e 20 69 6e 74 6f 0a 2a 2a 20 70 50  itten into.** pP
1f39d 61 72 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  arse..**.** This
1f39e 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 77 72   routine is a wr
1f39f 61 70 70 65 72 20 61 72 6f 75 6e 64 20 73 71 6c  apper around sql
1f3a0 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
1f3a1 29 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  ).  This routine
1f3a2 0a 2a 2a 20 69 6e 76 6f 6b 65 73 20 74 68 65 20  .** invokes the 
1f3a3 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63 74 6f 72  collation factor
1f3a4 79 20 69 66 20 74 68 65 20 6e 61 6d 65 64 20 63  y if the named c
1f3a5 6f 6c 6c 61 74 69 6f 6e 20 63 61 6e 6e 6f 74 20  ollation cannot 
1f3a6 62 65 20 66 6f 75 6e 64 0a 2a 2a 20 61 6e 64 20  be found.** and 
1f3a7 67 65 6e 65 72 61 74 65 73 20 61 6e 20 65 72 72  generates an err
1f3a8 6f 72 20 6d 65 73 73 61 67 65 2e 0a 2a 2f 0a 53  or message..*/.S
1f3a9 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 43 6f  QLITE_PRIVATE Co
1f3aa 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 4c 6f  llSeq *sqlite3Lo
1f3ab 63 61 74 65 43 6f 6c 6c 53 65 71 28 50 61 72 73  cateCollSeq(Pars
1f3ac 65 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74  e *pParse, const
1f3ad 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 69 6e   char *zName, in
1f3ae 74 20 6e 4e 61 6d 65 29 7b 0a 20 20 73 71 6c 69  t nName){.  sqli
1f3af 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1f3b0 2d 3e 64 62 3b 0a 20 20 75 38 20 65 6e 63 20 3d  ->db;.  u8 enc =
1f3b1 20 45 4e 43 28 64 62 29 3b 0a 20 20 75 38 20 69   ENC(db);.  u8 i
1f3b2 6e 69 74 62 75 73 79 20 3d 20 64 62 2d 3e 69 6e  nitbusy = db->in
1f3b3 69 74 2e 62 75 73 79 3b 0a 20 20 43 6f 6c 6c 53  it.busy;.  CollS
1f3b4 65 71 20 2a 70 43 6f 6c 6c 3b 0a 0a 20 20 70 43  eq *pColl;..  pC
1f3b5 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  oll = sqlite3Fin
1f3b6 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20 65 6e 63  dCollSeq(db, enc
1f3b7 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
1f3b8 69 6e 69 74 62 75 73 79 29 3b 0a 20 20 69 66 28  initbusy);.  if(
1f3b9 20 21 69 6e 69 74 62 75 73 79 20 26 26 20 28 21   !initbusy && (!
1f3ba 70 43 6f 6c 6c 20 7c 7c 20 21 70 43 6f 6c 6c 2d  pColl || !pColl-
1f3bb 3e 78 43 6d 70 29 20 29 7b 0a 20 20 20 20 70 43  >xCmp) ){.    pC
1f3bc 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 47 65 74  oll = sqlite3Get
1f3bd 43 6f 6c 6c 53 65 71 28 64 62 2c 20 70 43 6f 6c  CollSeq(db, pCol
1f3be 6c 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29  l, zName, nName)
1f3bf 3b 0a 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c  ;.    if( !pColl
1f3c0 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 6e 4e   ){.      if( nN
1f3c1 61 6d 65 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ame<0 ){.       
1f3c2 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
1f3c3 53 74 72 6c 65 6e 28 64 62 2c 20 7a 4e 61 6d 65  Strlen(db, zName
1f3c4 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
1f3c5 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1f3c6 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63  (pParse, "no suc
1f3c7 68 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  h collation sequ
1f3c8 65 6e 63 65 3a 20 25 2e 2a 73 22 2c 20 6e 4e 61  ence: %.*s", nNa
1f3c9 6d 65 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  me, zName);.    
1f3ca 20 20 70 43 6f 6c 6c 20 3d 20 30 3b 0a 20 20 20    pColl = 0;.   
1f3cb 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
1f3cc 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a   pColl;.}.../*.*
1f3cd 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1f3ce 74 68 61 74 20 77 69 6c 6c 20 69 6e 63 72 65 6d  that will increm
1f3cf 65 6e 74 20 74 68 65 20 73 63 68 65 6d 61 20 63  ent the schema c
1f3d0 6f 6f 6b 69 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  ookie..**.** The
1f3d1 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20 69   schema cookie i
1f3d2 73 20 75 73 65 64 20 74 6f 20 64 65 74 65 72 6d  s used to determ
1f3d3 69 6e 65 20 77 68 65 6e 20 74 68 65 20 73 63 68  ine when the sch
1f3d4 65 6d 61 20 66 6f 72 20 74 68 65 0a 2a 2a 20 64  ema for the.** d
1f3d5 61 74 61 62 61 73 65 20 63 68 61 6e 67 65 73 2e  atabase changes.
1f3d6 20 20 41 66 74 65 72 20 65 61 63 68 20 73 63 68    After each sch
1f3d7 65 6d 61 20 63 68 61 6e 67 65 2c 20 74 68 65 20  ema change, the 
1f3d8 63 6f 6f 6b 69 65 20 76 61 6c 75 65 0a 2a 2a 20  cookie value.** 
1f3d9 63 68 61 6e 67 65 73 2e 20 20 57 68 65 6e 20 61  changes.  When a
1f3da 20 70 72 6f 63 65 73 73 20 66 69 72 73 74 20 72   process first r
1f3db 65 61 64 73 20 74 68 65 20 73 63 68 65 6d 61 20  eads the schema 
1f3dc 69 74 20 72 65 63 6f 72 64 73 20 74 68 65 0a 2a  it records the.*
1f3dd 2a 20 63 6f 6f 6b 69 65 2e 20 20 54 68 65 72 65  * cookie.  There
1f3de 61 66 74 65 72 2c 20 77 68 65 6e 65 76 65 72 20  after, whenever 
1f3df 69 74 20 67 6f 65 73 20 74 6f 20 61 63 63 65 73  it goes to acces
1f3e0 73 20 74 68 65 20 64 61 74 61 62 61 73 65 2c 0a  s the database,.
1f3e1 2a 2a 20 69 74 20 63 68 65 63 6b 73 20 74 68 65  ** it checks the
1f3e2 20 63 6f 6f 6b 69 65 20 74 6f 20 6d 61 6b 65 20   cookie to make 
1f3e3 73 75 72 65 20 74 68 65 20 73 63 68 65 6d 61 20  sure the schema 
1f3e4 68 61 73 20 6e 6f 74 20 63 68 61 6e 67 65 64 0a  has not changed.
1f3e5 2a 2a 20 73 69 6e 63 65 20 69 74 20 77 61 73 20  ** since it was 
1f3e6 6c 61 73 74 20 72 65 61 64 2e 0a 2a 2a 0a 2a 2a  last read..**.**
1f3e7 20 54 68 69 73 20 70 6c 61 6e 20 69 73 20 6e 6f   This plan is no
1f3e8 74 20 63 6f 6d 70 6c 65 74 65 6c 79 20 62 75 6c  t completely bul
1f3e9 6c 65 74 2d 70 72 6f 6f 66 2e 20 20 49 74 20 69  let-proof.  It i
1f3ea 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72 0a 2a  s possible for.*
1f3eb 2a 20 74 68 65 20 73 63 68 65 6d 61 20 74 6f 20  * the schema to 
1f3ec 63 68 61 6e 67 65 20 6d 75 6c 74 69 70 6c 65 20  change multiple 
1f3ed 74 69 6d 65 73 20 61 6e 64 20 66 6f 72 20 74 68  times and for th
1f3ee 65 20 63 6f 6f 6b 69 65 20 74 6f 20 62 65 0a 2a  e cookie to be.*
1f3ef 2a 20 73 65 74 20 62 61 63 6b 20 74 6f 20 70 72  * set back to pr
1f3f0 69 6f 72 20 76 61 6c 75 65 2e 20 20 42 75 74 20  ior value.  But 
1f3f1 73 63 68 65 6d 61 20 63 68 61 6e 67 65 73 20 61  schema changes a
1f3f2 72 65 20 69 6e 66 72 65 71 75 65 6e 74 0a 2a 2a  re infrequent.**
1f3f3 20 61 6e 64 20 74 68 65 20 70 72 6f 62 61 62 69   and the probabi
1f3f4 6c 69 74 79 20 6f 66 20 68 69 74 74 69 6e 67 20  lity of hitting 
1f3f5 74 68 65 20 73 61 6d 65 20 63 6f 6f 6b 69 65 20  the same cookie 
1f3f6 76 61 6c 75 65 20 69 73 20 6f 6e 6c 79 0a 2a 2a  value is only.**
1f3f7 20 31 20 63 68 61 6e 63 65 20 69 6e 20 32 5e 33   1 chance in 2^3
1f3f8 32 2e 20 20 53 6f 20 77 65 27 72 65 20 73 61 66  2.  So we're saf
1f3f9 65 20 65 6e 6f 75 67 68 2e 0a 2a 2f 0a 53 51 4c  e enough..*/.SQL
1f3fa 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
1f3fb 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f   sqlite3ChangeCo
1f3fc 6f 6b 69 65 28 50 61 72 73 65 20 2a 70 50 61 72  okie(Parse *pPar
1f3fd 73 65 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20  se, int iDb){.  
1f3fe 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33  int r1 = sqlite3
1f3ff 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
1f400 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  e);.  sqlite3 *d
1f401 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
1f402 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
1f403 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c  se->pVdbe;.  sql
1f404 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
1f405 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 64 62  , OP_Integer, db
1f406 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
1f407 6d 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69  ma->schema_cooki
1f408 65 2b 31 2c 20 72 31 29 3b 0a 20 20 73 71 6c 69  e+1, r1);.  sqli
1f409 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
1f40a 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69   OP_SetCookie, i
1f40b 44 62 2c 20 30 2c 20 72 31 29 3b 0a 20 20 73 71  Db, 0, r1);.  sq
1f40c 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
1f40d 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
1f40e 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d 65 61 73 75 72  .}../*.** Measur
1f40f 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  e the number of 
1f410 63 68 61 72 61 63 74 65 72 73 20 6e 65 65 64 65  characters neede
1f411 64 20 74 6f 20 6f 75 74 70 75 74 20 74 68 65 20  d to output the 
1f412 67 69 76 65 6e 0a 2a 2a 20 69 64 65 6e 74 69 66  given.** identif
1f413 69 65 72 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  ier.  The number
1f414 20 72 65 74 75 72 6e 65 64 20 69 6e 63 6c 75 64   returned includ
1f415 65 73 20 61 6e 79 20 71 75 6f 74 65 73 20 75 73  es any quotes us
1f416 65 64 0a 2a 2a 20 62 75 74 20 64 6f 65 73 20 6e  ed.** but does n
1f417 6f 74 20 69 6e 63 6c 75 64 65 20 74 68 65 20 6e  ot include the n
1f418 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 2e 0a  ull terminator..
1f419 2a 2a 0a 2a 2a 20 54 68 65 20 65 73 74 69 6d 61  **.** The estima
1f41a 74 65 20 69 73 20 63 6f 6e 73 65 72 76 61 74 69  te is conservati
1f41b 76 65 2e 20 20 49 74 20 6d 69 67 68 74 20 62 65  ve.  It might be
1f41c 20 6c 61 72 67 65 72 20 74 68 61 74 20 77 68 61   larger that wha
1f41d 74 20 69 73 0a 2a 2a 20 72 65 61 6c 6c 79 20 6e  t is.** really n
1f41e 65 65 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  eeded..*/.static
1f41f 20 69 6e 74 20 69 64 65 6e 74 4c 65 6e 67 74 68   int identLength
1f420 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b  (const char *z){
1f421 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66 6f 72 28  .  int n;.  for(
1f422 6e 3d 30 3b 20 2a 7a 3b 20 6e 2b 2b 2c 20 7a 2b  n=0; *z; n++, z+
1f423 2b 29 7b 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d  +){.    if( *z==
1f424 27 22 27 20 29 7b 20 6e 2b 2b 3b 20 7d 0a 20 20  '"' ){ n++; }.  
1f425 7d 0a 20 20 72 65 74 75 72 6e 20 6e 20 2b 20 32  }.  return n + 2
1f426 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65  ;.}../*.** Write
1f427 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20 6f   an identifier o
1f428 6e 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74  nto the end of t
1f429 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e 67 2e  he given string.
1f42a 20 20 41 64 64 0a 2a 2a 20 71 75 6f 74 65 20 63    Add.** quote c
1f42b 68 61 72 61 63 74 65 72 73 20 61 73 20 6e 65 65  haracters as nee
1f42c 64 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ded..*/.static v
1f42d 6f 69 64 20 69 64 65 6e 74 50 75 74 28 63 68 61  oid identPut(cha
1f42e 72 20 2a 7a 2c 20 69 6e 74 20 2a 70 49 64 78 2c  r *z, int *pIdx,
1f42f 20 63 68 61 72 20 2a 7a 53 69 67 6e 65 64 49 64   char *zSignedId
1f430 65 6e 74 29 7b 0a 20 20 75 6e 73 69 67 6e 65 64  ent){.  unsigned
1f431 20 63 68 61 72 20 2a 7a 49 64 65 6e 74 20 3d 20   char *zIdent = 
1f432 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29  (unsigned char*)
1f433 7a 53 69 67 6e 65 64 49 64 65 6e 74 3b 0a 20 20  zSignedIdent;.  
1f434 69 6e 74 20 69 2c 20 6a 2c 20 6e 65 65 64 51 75  int i, j, needQu
1f435 6f 74 65 3b 0a 20 20 69 20 3d 20 2a 70 49 64 78  ote;.  i = *pIdx
1f436 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 7a 49 64  ;.  for(j=0; zId
1f437 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29 7b 0a 20 20  ent[j]; j++){.  
1f438 20 20 69 66 28 20 21 69 73 61 6c 6e 75 6d 28 7a    if( !isalnum(z
1f439 49 64 65 6e 74 5b 6a 5d 29 20 26 26 20 7a 49 64  Ident[j]) && zId
1f43a 65 6e 74 5b 6a 5d 21 3d 27 5f 27 20 29 20 62 72  ent[j]!='_' ) br
1f43b 65 61 6b 3b 0a 20 20 7d 0a 20 20 6e 65 65 64 51  eak;.  }.  needQ
1f43c 75 6f 74 65 20 3d 20 20 7a 49 64 65 6e 74 5b 6a  uote =  zIdent[j
1f43d 5d 21 3d 30 20 7c 7c 20 69 73 64 69 67 69 74 28  ]!=0 || isdigit(
1f43e 7a 49 64 65 6e 74 5b 30 5d 29 0a 20 20 20 20 20  zIdent[0]).     
1f43f 20 20 20 20 20 20 20 20 20 20 20 20 20 7c 7c 20               || 
1f440 73 71 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f  sqlite3KeywordCo
1f441 64 65 28 7a 49 64 65 6e 74 2c 20 6a 29 21 3d 54  de(zIdent, j)!=T
1f442 4b 5f 49 44 3b 0a 20 20 69 66 28 20 6e 65 65 64  K_ID;.  if( need
1f443 51 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d  Quote ) z[i++] =
1f444 20 27 22 27 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b   '"';.  for(j=0;
1f445 20 7a 49 64 65 6e 74 5b 6a 5d 3b 20 6a 2b 2b 29   zIdent[j]; j++)
1f446 7b 0a 20 20 20 20 7a 5b 69 2b 2b 5d 20 3d 20 7a  {.    z[i++] = z
1f447 49 64 65 6e 74 5b 6a 5d 3b 0a 20 20 20 20 69 66  Ident[j];.    if
1f448 28 20 7a 49 64 65 6e 74 5b 6a 5d 3d 3d 27 22 27  ( zIdent[j]=='"'
1f449 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20 27 22 27 3b   ) z[i++] = '"';
1f44a 0a 20 20 7d 0a 20 20 69 66 28 20 6e 65 65 64 51  .  }.  if( needQ
1f44b 75 6f 74 65 20 29 20 7a 5b 69 2b 2b 5d 20 3d 20  uote ) z[i++] = 
1f44c 27 22 27 3b 0a 20 20 7a 5b 69 5d 20 3d 20 30 3b  '"';.  z[i] = 0;
1f44d 0a 20 20 2a 70 49 64 78 20 3d 20 69 3b 0a 7d 0a  .  *pIdx = i;.}.
1f44e 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
1f44f 61 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73  a CREATE TABLE s
1f450 74 61 74 65 6d 65 6e 74 20 61 70 70 72 6f 70 72  tatement appropr
1f451 69 61 74 65 20 66 6f 72 20 74 68 65 20 67 69 76  iate for the giv
1f452 65 6e 0a 2a 2a 20 74 61 62 6c 65 2e 20 20 4d 65  en.** table.  Me
1f453 6d 6f 72 79 20 74 6f 20 68 6f 6c 64 20 74 68 65  mory to hold the
1f454 20 74 65 78 74 20 6f 66 20 74 68 65 20 73 74 61   text of the sta
1f455 74 65 6d 65 6e 74 20 69 73 20 6f 62 74 61 69 6e  tement is obtain
1f456 65 64 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74  ed.** from sqlit
1f457 65 4d 61 6c 6c 6f 63 28 29 20 61 6e 64 20 6d 75  eMalloc() and mu
1f458 73 74 20 62 65 20 66 72 65 65 64 20 62 79 20 74  st be freed by t
1f459 68 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74  he calling funct
1f45a 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ion..*/.static c
1f45b 68 61 72 20 2a 63 72 65 61 74 65 54 61 62 6c 65  har *createTable
1f45c 53 74 6d 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Stmt(sqlite3 *db
1f45d 2c 20 54 61 62 6c 65 20 2a 70 2c 20 69 6e 74 20  , Table *p, int 
1f45e 69 73 54 65 6d 70 29 7b 0a 20 20 69 6e 74 20 69  isTemp){.  int i
1f45f 2c 20 6b 2c 20 6e 3b 0a 20 20 63 68 61 72 20 2a  , k, n;.  char *
1f460 7a 53 74 6d 74 3b 0a 20 20 63 68 61 72 20 2a 7a  zStmt;.  char *z
1f461 53 65 70 2c 20 2a 7a 53 65 70 32 2c 20 2a 7a 45  Sep, *zSep2, *zE
1f462 6e 64 2c 20 2a 7a 3b 0a 20 20 43 6f 6c 75 6d 6e  nd, *z;.  Column
1f463 20 2a 70 43 6f 6c 3b 0a 20 20 6e 20 3d 20 30 3b   *pCol;.  n = 0;
1f464 0a 20 20 66 6f 72 28 70 43 6f 6c 20 3d 20 70 2d  .  for(pCol = p-
1f465 3e 61 43 6f 6c 2c 20 69 3d 30 3b 20 69 3c 70 2d  >aCol, i=0; i<p-
1f466 3e 6e 43 6f 6c 3b 20 69 2b 2b 2c 20 70 43 6f 6c  >nCol; i++, pCol
1f467 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d 20 69 64  ++){.    n += id
1f468 65 6e 74 4c 65 6e 67 74 68 28 70 43 6f 6c 2d 3e  entLength(pCol->
1f469 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7a 20 3d 20  zName);.    z = 
1f46a 70 43 6f 6c 2d 3e 7a 54 79 70 65 3b 0a 20 20 20  pCol->zType;.   
1f46b 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20 20 20   if( z ){.      
1f46c 6e 20 2b 3d 20 28 73 74 72 6c 65 6e 28 7a 29 20  n += (strlen(z) 
1f46d 2b 20 31 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  + 1);.    }.  }.
1f46e 20 20 6e 20 2b 3d 20 69 64 65 6e 74 4c 65 6e 67    n += identLeng
1f46f 74 68 28 70 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  th(p->zName);.  
1f470 69 66 28 20 6e 3c 35 30 20 29 7b 0a 20 20 20 20  if( n<50 ){.    
1f471 7a 53 65 70 20 3d 20 22 22 3b 0a 20 20 20 20 7a  zSep = "";.    z
1f472 53 65 70 32 20 3d 20 22 2c 22 3b 0a 20 20 20 20  Sep2 = ",";.    
1f473 7a 45 6e 64 20 3d 20 22 29 22 3b 0a 20 20 7d 65  zEnd = ")";.  }e
1f474 6c 73 65 7b 0a 20 20 20 20 7a 53 65 70 20 3d 20  lse{.    zSep = 
1f475 22 5c 6e 20 20 22 3b 0a 20 20 20 20 7a 53 65 70  "\n  ";.    zSep
1f476 32 20 3d 20 22 2c 5c 6e 20 20 22 3b 0a 20 20 20  2 = ",\n  ";.   
1f477 20 7a 45 6e 64 20 3d 20 22 5c 6e 29 22 3b 0a 20   zEnd = "\n)";. 
1f478 20 7d 0a 20 20 6e 20 2b 3d 20 33 35 20 2b 20 36   }.  n += 35 + 6
1f479 2a 70 2d 3e 6e 43 6f 6c 3b 0a 20 20 7a 53 74 6d  *p->nCol;.  zStm
1f47a 74 20 3d 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f  t = sqlite3Mallo
1f47b 63 28 20 6e 20 29 3b 0a 20 20 69 66 28 20 7a 53  c( n );.  if( zS
1f47c 74 6d 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64 62  tmt==0 ){.    db
1f47d 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d  ->mallocFailed =
1f47e 20 31 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   1;.    return 0
1f47f 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
1f480 73 6e 70 72 69 6e 74 66 28 6e 2c 20 7a 53 74 6d  snprintf(n, zStm
1f481 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
1f482 20 20 20 20 20 21 4f 4d 49 54 5f 54 45 4d 50 44       !OMIT_TEMPD
1f483 42 26 26 69 73 54 65 6d 70 20 3f 20 22 43 52 45  B&&isTemp ? "CRE
1f484 41 54 45 20 54 45 4d 50 20 54 41 42 4c 45 20 22  ATE TEMP TABLE "
1f485 3a 22 43 52 45 41 54 45 20 54 41 42 4c 45 20 22  :"CREATE TABLE "
1f486 29 3b 0a 20 20 6b 20 3d 20 73 74 72 6c 65 6e 28  );.  k = strlen(
1f487 7a 53 74 6d 74 29 3b 0a 20 20 69 64 65 6e 74 50  zStmt);.  identP
1f488 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c 20 70 2d  ut(zStmt, &k, p-
1f489 3e 7a 4e 61 6d 65 29 3b 0a 20 20 7a 53 74 6d 74  >zName);.  zStmt
1f48a 5b 6b 2b 2b 5d 20 3d 20 27 28 27 3b 0a 20 20 66  [k++] = '(';.  f
1f48b 6f 72 28 70 43 6f 6c 3d 70 2d 3e 61 43 6f 6c 2c  or(pCol=p->aCol,
1f48c 20 69 3d 30 3b 20 69 3c 70 2d 3e 6e 43 6f 6c 3b   i=0; i<p->nCol;
1f48d 20 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20   i++, pCol++){. 
1f48e 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
1f48f 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b  ntf(n-k, &zStmt[
1f490 6b 5d 2c 20 7a 53 65 70 29 3b 0a 20 20 20 20 6b  k], zSep);.    k
1f491 20 2b 3d 20 73 74 72 6c 65 6e 28 26 7a 53 74 6d   += strlen(&zStm
1f492 74 5b 6b 5d 29 3b 0a 20 20 20 20 7a 53 65 70 20  t[k]);.    zSep 
1f493 3d 20 7a 53 65 70 32 3b 0a 20 20 20 20 69 64 65  = zSep2;.    ide
1f494 6e 74 50 75 74 28 7a 53 74 6d 74 2c 20 26 6b 2c  ntPut(zStmt, &k,
1f495 20 70 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pCol->zName);. 
1f496 20 20 20 69 66 28 20 28 7a 20 3d 20 70 43 6f 6c     if( (z = pCol
1f497 2d 3e 7a 54 79 70 65 29 21 3d 30 20 29 7b 0a 20  ->zType)!=0 ){. 
1f498 20 20 20 20 20 7a 53 74 6d 74 5b 6b 2b 2b 5d 20       zStmt[k++] 
1f499 3d 20 27 20 27 3b 0a 20 20 20 20 20 20 61 73 73  = ' ';.      ass
1f49a 65 72 74 28 20 28 69 6e 74 29 28 73 74 72 6c 65  ert( (int)(strle
1f49b 6e 28 7a 29 2b 6b 2b 31 29 3c 3d 6e 20 29 3b 0a  n(z)+k+1)<=n );.
1f49c 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
1f49d 70 72 69 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74  printf(n-k, &zSt
1f49e 6d 74 5b 6b 5d 2c 20 22 25 73 22 2c 20 7a 29 3b  mt[k], "%s", z);
1f49f 0a 20 20 20 20 20 20 6b 20 2b 3d 20 73 74 72 6c  .      k += strl
1f4a0 65 6e 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  en(z);.    }.  }
1f4a1 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
1f4a2 6e 74 66 28 6e 2d 6b 2c 20 26 7a 53 74 6d 74 5b  ntf(n-k, &zStmt[
1f4a3 6b 5d 2c 20 22 25 73 22 2c 20 7a 45 6e 64 29 3b  k], "%s", zEnd);
1f4a4 0a 20 20 72 65 74 75 72 6e 20 7a 53 74 6d 74 3b  .  return zStmt;
1f4a5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1f4a6 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1f4a7 20 74 6f 20 72 65 70 6f 72 74 20 74 68 65 20 66   to report the f
1f4a8 69 6e 61 6c 20 22 29 22 20 74 68 61 74 20 74 65  inal ")" that te
1f4a9 72 6d 69 6e 61 74 65 73 0a 2a 2a 20 61 20 43 52  rminates.** a CR
1f4aa 45 41 54 45 20 54 41 42 4c 45 20 73 74 61 74 65  EATE TABLE state
1f4ab 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
1f4ac 74 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  table structure 
1f4ad 74 68 61 74 20 6f 74 68 65 72 20 61 63 74 69 6f  that other actio
1f4ae 6e 20 72 6f 75 74 69 6e 65 73 20 68 61 76 65 20  n routines have 
1f4af 62 65 65 6e 20 62 75 69 6c 64 69 6e 67 0a 2a 2a  been building.**
1f4b0 20 69 73 20 61 64 64 65 64 20 74 6f 20 74 68 65   is added to the
1f4b1 20 69 6e 74 65 72 6e 61 6c 20 68 61 73 68 20 74   internal hash t
1f4b2 61 62 6c 65 73 2c 20 61 73 73 75 6d 69 6e 67 20  ables, assuming 
1f4b3 6e 6f 20 65 72 72 6f 72 73 20 68 61 76 65 0a 2a  no errors have.*
1f4b4 2a 20 6f 63 63 75 72 72 65 64 2e 0a 2a 2a 0a 2a  * occurred..**.*
1f4b5 2a 20 41 6e 20 65 6e 74 72 79 20 66 6f 72 20 74  * An entry for t
1f4b6 68 65 20 74 61 62 6c 65 20 69 73 20 6d 61 64 65  he table is made
1f4b7 20 69 6e 20 74 68 65 20 6d 61 73 74 65 72 20 74   in the master t
1f4b8 61 62 6c 65 20 6f 6e 20 64 69 73 6b 2c 20 75 6e  able on disk, un
1f4b9 6c 65 73 73 0a 2a 2a 20 74 68 69 73 20 69 73 20  less.** this is 
1f4ba 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
1f4bb 65 20 6f 72 20 64 62 2d 3e 69 6e 69 74 2e 62 75  e or db->init.bu
1f4bc 73 79 3d 3d 31 2e 20 20 57 68 65 6e 20 64 62 2d  sy==1.  When db-
1f4bd 3e 69 6e 69 74 2e 62 75 73 79 3d 3d 31 0a 2a 2a  >init.busy==1.**
1f4be 20 69 74 20 6d 65 61 6e 73 20 77 65 20 61 72 65   it means we are
1f4bf 20 72 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c   reading the sql
1f4c0 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
1f4c1 20 62 65 63 61 75 73 65 20 77 65 20 6a 75 73 74   because we just
1f4c2 0a 2a 2a 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f  .** connected to
1f4c3 20 74 68 65 20 64 61 74 61 62 61 73 65 20 6f 72   the database or
1f4c4 20 62 65 63 61 75 73 65 20 74 68 65 20 73 71 6c   because the sql
1f4c5 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65  ite_master table
1f4c6 20 68 61 73 0a 2a 2a 20 72 65 63 65 6e 74 6c 79   has.** recently
1f4c7 20 63 68 61 6e 67 65 64 2c 20 73 6f 20 74 68 65   changed, so the
1f4c8 20 65 6e 74 72 79 20 66 6f 72 20 74 68 69 73 20   entry for this 
1f4c9 74 61 62 6c 65 20 61 6c 72 65 61 64 79 20 65 78  table already ex
1f4ca 69 73 74 73 20 69 6e 0a 2a 2a 20 74 68 65 20 73  ists in.** the s
1f4cb 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1f4cc 6c 65 2e 20 20 57 65 20 64 6f 20 6e 6f 74 20 77  le.  We do not w
1f4cd 61 6e 74 20 74 6f 20 63 72 65 61 74 65 20 69 74  ant to create it
1f4ce 20 61 67 61 69 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66   again..**.** If
1f4cf 20 74 68 65 20 70 53 65 6c 65 63 74 20 61 72 67   the pSelect arg
1f4d0 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c  ument is not NUL
1f4d1 4c 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61 74  L, it means that
1f4d2 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
1f4d3 20 77 61 73 20 63 61 6c 6c 65 64 20 74 6f 20 63   was called to c
1f4d4 72 65 61 74 65 20 61 20 74 61 62 6c 65 20 67 65  reate a table ge
1f4d5 6e 65 72 61 74 65 64 20 66 72 6f 6d 20 61 20 0a  nerated from a .
1f4d6 2a 2a 20 22 43 52 45 41 54 45 20 54 41 42 4c 45  ** "CREATE TABLE
1f4d7 20 2e 2e 2e 20 41 53 20 53 45 4c 45 43 54 20 2e   ... AS SELECT .
1f4d8 2e 2e 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  .." statement.  
1f4d9 54 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  The column names
1f4da 20 6f 66 0a 2a 2a 20 74 68 65 20 6e 65 77 20 74   of.** the new t
1f4db 61 62 6c 65 20 77 69 6c 6c 20 6d 61 74 63 68 20  able will match 
1f4dc 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f  the result set o
1f4dd 66 20 74 68 65 20 53 45 4c 45 43 54 2e 0a 2a 2f  f the SELECT..*/
1f4de 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
1f4df 76 6f 69 64 20 73 71 6c 69 74 65 33 45 6e 64 54  void sqlite3EndT
1f4e0 61 62 6c 65 28 0a 20 20 50 61 72 73 65 20 2a 70  able(.  Parse *p
1f4e1 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
1f4e2 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  /* Parse context
1f4e3 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 43 6f   */.  Token *pCo
1f4e4 6e 73 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  ns,           /*
1f4e5 20 54 68 65 20 27 2c 27 20 74 6f 6b 65 6e 20 61   The ',' token a
1f4e6 66 74 65 72 20 74 68 65 20 6c 61 73 74 20 63 6f  fter the last co
1f4e7 6c 75 6d 6e 20 64 65 66 6e 2e 20 2a 2f 0a 20 20  lumn defn. */.  
1f4e8 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20 20  Token *pEnd,    
1f4e9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 66          /* The f
1f4ea 69 6e 61 6c 20 27 29 27 20 74 6f 6b 65 6e 20 69  inal ')' token i
1f4eb 6e 20 74 68 65 20 43 52 45 41 54 45 20 54 41 42  n the CREATE TAB
1f4ec 4c 45 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  LE */.  Select *
1f4ed 70 53 65 6c 65 63 74 20 20 20 20 20 20 20 20 20  pSelect         
1f4ee 2f 2a 20 53 65 6c 65 63 74 20 66 72 6f 6d 20 61  /* Select from a
1f4ef 20 22 43 52 45 41 54 45 20 2e 2e 2e 20 41 53 20   "CREATE ... AS 
1f4f0 53 45 4c 45 43 54 22 20 2a 2f 0a 29 7b 0a 20 20  SELECT" */.){.  
1f4f1 54 61 62 6c 65 20 2a 70 3b 0a 20 20 73 71 6c 69  Table *p;.  sqli
1f4f2 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
1f4f3 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44 62 3b  ->db;.  int iDb;
1f4f4 0a 0a 20 20 69 66 28 20 28 70 45 6e 64 3d 3d 30  ..  if( (pEnd==0
1f4f5 20 26 26 20 70 53 65 6c 65 63 74 3d 3d 30 29 20   && pSelect==0) 
1f4f6 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  || pParse->nErr 
1f4f7 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1f4f8 6c 65 64 20 29 20 7b 0a 20 20 20 20 72 65 74 75  led ) {.    retu
1f4f9 72 6e 3b 0a 20 20 7d 0a 20 20 70 20 3d 20 70 50  rn;.  }.  p = pP
1f4fa 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
1f4fb 0a 20 20 69 66 28 20 70 3d 3d 30 20 29 20 72 65  .  if( p==0 ) re
1f4fc 74 75 72 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28  turn;..  assert(
1f4fd 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
1f4fe 7c 7c 20 21 70 53 65 6c 65 63 74 20 29 3b 0a 0a  || !pSelect );..
1f4ff 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
1f500 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
1f501 20 70 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 23   p->pSchema);..#
1f502 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f503 49 54 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 52 65  IT_CHECK.  /* Re
1f504 73 6f 6c 76 65 20 6e 61 6d 65 73 20 69 6e 20 61  solve names in a
1f505 6c 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  ll CHECK constra
1f506 69 6e 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e  int expressions.
1f507 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70  .  */.  if( p->p
1f508 43 68 65 63 6b 20 29 7b 0a 20 20 20 20 53 72 63  Check ){.    Src
1f509 4c 69 73 74 20 73 53 72 63 3b 20 20 20 20 20 20  List sSrc;      
1f50a 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
1f50b 46 61 6b 65 20 53 72 63 4c 69 73 74 20 66 6f 72  Fake SrcList for
1f50c 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1f50d 6c 65 20 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f  le */.    NameCo
1f50e 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
1f50f 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
1f510 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70 50  e context for pP
1f511 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
1f512 2a 2f 0a 0a 20 20 20 20 6d 65 6d 73 65 74 28 26  */..    memset(&
1f513 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73  sNC, 0, sizeof(s
1f514 4e 43 29 29 3b 0a 20 20 20 20 6d 65 6d 73 65 74  NC));.    memset
1f515 28 26 73 53 72 63 2c 20 30 2c 20 73 69 7a 65 6f  (&sSrc, 0, sizeo
1f516 66 28 73 53 72 63 29 29 3b 0a 20 20 20 20 73 53  f(sSrc));.    sS
1f517 72 63 2e 6e 53 72 63 20 3d 20 31 3b 0a 20 20 20  rc.nSrc = 1;.   
1f518 20 73 53 72 63 2e 61 5b 30 5d 2e 7a 4e 61 6d 65   sSrc.a[0].zName
1f519 20 3d 20 70 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20   = p->zName;.   
1f51a 20 73 53 72 63 2e 61 5b 30 5d 2e 70 54 61 62 20   sSrc.a[0].pTab 
1f51b 3d 20 70 3b 0a 20 20 20 20 73 53 72 63 2e 61 5b  = p;.    sSrc.a[
1f51c 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b  0].iCursor = -1;
1f51d 0a 20 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20  .    sNC.pParse 
1f51e 3d 20 70 50 61 72 73 65 3b 0a 20 20 20 20 73 4e  = pParse;.    sN
1f51f 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 26 73 53  C.pSrcList = &sS
1f520 72 63 3b 0a 20 20 20 20 73 4e 43 2e 69 73 43 68  rc;.    sNC.isCh
1f521 65 63 6b 20 3d 20 31 3b 0a 20 20 20 20 69 66 28  eck = 1;.    if(
1f522 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45   sqlite3ResolveE
1f523 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70  xprNames(&sNC, p
1f524 2d 3e 70 43 68 65 63 6b 29 20 29 7b 0a 20 20 20  ->pCheck) ){.   
1f525 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
1f526 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 21  .  }.#endif /* !
1f527 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
1f528 4d 49 54 5f 43 48 45 43 4b 29 20 2a 2f 0a 0a 20  MIT_CHECK) */.. 
1f529 20 2f 2a 20 49 66 20 74 68 65 20 64 62 2d 3e 69   /* If the db->i
1f52a 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 69 74  nit.busy is 1 it
1f52b 20 6d 65 61 6e 73 20 77 65 20 61 72 65 20 72 65   means we are re
1f52c 61 64 69 6e 67 20 74 68 65 20 53 51 4c 20 6f 66  ading the SQL of
1f52d 66 20 74 68 65 0a 20 20 2a 2a 20 22 73 71 6c 69  f the.  ** "sqli
1f52e 74 65 5f 6d 61 73 74 65 72 22 20 6f 72 20 22 73  te_master" or "s
1f52f 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65  qlite_temp_maste
1f530 72 22 20 74 61 62 6c 65 20 6f 6e 20 74 68 65 20  r" table on the 
1f531 64 69 73 6b 2e 0a 20 20 2a 2a 20 53 6f 20 64 6f  disk..  ** So do
1f532 20 6e 6f 74 20 77 72 69 74 65 20 74 6f 20 74 68   not write to th
1f533 65 20 64 69 73 6b 20 61 67 61 69 6e 2e 20 20 45  e disk again.  E
1f534 78 74 72 61 63 74 20 74 68 65 20 72 6f 6f 74 20  xtract the root 
1f535 70 61 67 65 20 6e 75 6d 62 65 72 0a 20 20 2a 2a  page number.  **
1f536 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 66   for the table f
1f537 72 6f 6d 20 74 68 65 20 64 62 2d 3e 69 6e 69 74  rom the db->init
1f538 2e 6e 65 77 54 6e 75 6d 20 66 69 65 6c 64 2e 20  .newTnum field. 
1f539 20 28 54 68 65 20 70 61 67 65 20 6e 75 6d 62 65   (The page numbe
1f53a 72 0a 20 20 2a 2a 20 73 68 6f 75 6c 64 20 68 61  r.  ** should ha
1f53b 76 65 20 62 65 65 6e 20 70 75 74 20 74 68 65 72  ve been put ther
1f53c 65 20 62 79 20 74 68 65 20 73 71 6c 69 74 65 4f  e by the sqliteO
1f53d 70 65 6e 43 62 20 72 6f 75 74 69 6e 65 2e 29 0a  penCb routine.).
1f53e 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 69    */.  if( db->i
1f53f 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
1f540 70 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e  p->tnum = db->in
1f541 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 7d 0a  it.newTnum;.  }.
1f542 0a 20 20 2f 2a 20 49 66 20 6e 6f 74 20 69 6e 69  .  /* If not ini
1f543 74 69 61 6c 69 7a 69 6e 67 2c 20 74 68 65 6e 20  tializing, then 
1f544 63 72 65 61 74 65 20 61 20 72 65 63 6f 72 64 20  create a record 
1f545 66 6f 72 20 74 68 65 20 6e 65 77 20 74 61 62 6c  for the new tabl
1f546 65 0a 20 20 2a 2a 20 69 6e 20 74 68 65 20 53 51  e.  ** in the SQ
1f547 4c 49 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c  LITE_MASTER tabl
1f548 65 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  e of the databas
1f549 65 2e 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e  e.  The record n
1f54a 75 6d 62 65 72 0a 20 20 2a 2a 20 66 6f 72 20 74  umber.  ** for t
1f54b 68 65 20 6e 65 77 20 74 61 62 6c 65 20 65 6e 74  he new table ent
1f54c 72 79 20 73 68 6f 75 6c 64 20 61 6c 72 65 61 64  ry should alread
1f54d 79 20 62 65 20 6f 6e 20 74 68 65 20 73 74 61 63  y be on the stac
1f54e 6b 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20  k..  **.  ** If 
1f54f 74 68 69 73 20 69 73 20 61 20 54 45 4d 50 4f 52  this is a TEMPOR
1f550 41 52 59 20 74 61 62 6c 65 2c 20 77 72 69 74 65  ARY table, write
1f551 20 74 68 65 20 65 6e 74 72 79 20 69 6e 74 6f 20   the entry into 
1f552 74 68 65 20 61 75 78 69 6c 69 61 72 79 0a 20 20  the auxiliary.  
1f553 2a 2a 20 66 69 6c 65 20 69 6e 73 74 65 61 64 20  ** file instead 
1f554 6f 66 20 69 6e 74 6f 20 74 68 65 20 6d 61 69 6e  of into the main
1f555 20 64 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a   database file..
1f556 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e    */.  if( !db->
1f557 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20  init.busy ){.   
1f558 20 69 6e 74 20 6e 3b 0a 20 20 20 20 56 64 62 65   int n;.    Vdbe
1f559 20 2a 76 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a   *v;.    char *z
1f55a 54 79 70 65 3b 20 20 20 20 2f 2a 20 22 76 69 65  Type;    /* "vie
1f55b 77 22 20 6f 72 20 22 74 61 62 6c 65 22 20 2a 2f  w" or "table" */
1f55c 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 79 70 65  .    char *zType
1f55d 32 3b 20 20 20 2f 2a 20 22 56 49 45 57 22 20 6f  2;   /* "VIEW" o
1f55e 72 20 22 54 41 42 4c 45 22 20 2a 2f 0a 20 20 20  r "TABLE" */.   
1f55f 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 20 20 20   char *zStmt;   
1f560 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
1f561 43 52 45 41 54 45 20 54 41 42 4c 45 20 6f 72 20  CREATE TABLE or 
1f562 43 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74  CREATE VIEW stat
1f563 65 6d 65 6e 74 20 2a 2f 0a 0a 20 20 20 20 76 20  ement */..    v 
1f564 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
1f565 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66  (pParse);.    if
1f566 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  ( v==0 ) return;
1f567 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ..    sqlite3Vdb
1f568 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c  eAddOp1(v, OP_Cl
1f569 6f 73 65 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  ose, 0);..    /*
1f56a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f 6f 74   Create the root
1f56b 70 61 67 65 20 66 6f 72 20 74 68 65 20 6e 65 77  page for the new
1f56c 20 74 61 62 6c 65 20 61 6e 64 20 70 75 73 68 20   table and push 
1f56d 69 74 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63  it onto the stac
1f56e 6b 2e 0a 20 20 20 20 2a 2a 20 41 20 76 69 65 77  k..    ** A view
1f56f 20 68 61 73 20 6e 6f 20 72 6f 6f 74 70 61 67 65   has no rootpage
1f570 2c 20 73 6f 20 6a 75 73 74 20 70 75 73 68 20 61  , so just push a
1f571 20 7a 65 72 6f 20 6f 6e 74 6f 20 74 68 65 20 73   zero onto the s
1f572 74 61 63 6b 20 66 6f 72 0a 20 20 20 20 2a 2a 20  tack for.    ** 
1f573 76 69 65 77 73 2e 20 20 49 6e 69 74 69 61 6c 69  views.  Initiali
1f574 7a 65 20 7a 54 79 70 65 20 61 74 20 74 68 65 20  ze zType at the 
1f575 73 61 6d 65 20 74 69 6d 65 2e 0a 20 20 20 20 2a  same time..    *
1f576 2f 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 53 65  /.    if( p->pSe
1f577 6c 65 63 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  lect==0 ){.     
1f578 20 2f 2a 20 41 20 72 65 67 75 6c 61 72 20 74 61   /* A regular ta
1f579 62 6c 65 20 2a 2f 0a 20 20 20 20 20 20 7a 54 79  ble */.      zTy
1f57a 70 65 20 3d 20 22 74 61 62 6c 65 22 3b 0a 20 20  pe = "table";.  
1f57b 20 20 20 20 7a 54 79 70 65 32 20 3d 20 22 54 41      zType2 = "TA
1f57c 42 4c 45 22 3b 0a 23 69 66 6e 64 65 66 20 53 51  BLE";.#ifndef SQ
1f57d 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20  LITE_OMIT_VIEW. 
1f57e 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1f57f 2f 2a 20 41 20 76 69 65 77 20 2a 2f 0a 20 20 20  /* A view */.   
1f580 20 20 20 7a 54 79 70 65 20 3d 20 22 76 69 65 77     zType = "view
1f581 22 3b 0a 20 20 20 20 20 20 7a 54 79 70 65 32 20  ";.      zType2 
1f582 3d 20 22 56 49 45 57 22 3b 0a 23 65 6e 64 69 66  = "VIEW";.#endif
1f583 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 49  .    }..    /* I
1f584 66 20 74 68 69 73 20 69 73 20 61 20 43 52 45 41  f this is a CREA
1f585 54 45 20 54 41 42 4c 45 20 78 78 20 41 53 20 53  TE TABLE xx AS S
1f586 45 4c 45 43 54 20 2e 2e 2e 2c 20 65 78 65 63 75  ELECT ..., execu
1f587 74 65 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20  te the SELECT.  
1f588 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e 74 20 74    ** statement t
1f589 6f 20 70 6f 70 75 6c 61 74 65 20 74 68 65 20 6e  o populate the n
1f58a 65 77 20 74 61 62 6c 65 2e 20 54 68 65 20 72 6f  ew table. The ro
1f58b 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20 66  ot-page number f
1f58c 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20 6e 65  or the.    ** ne
1f58d 77 20 74 61 62 6c 65 20 69 73 20 6f 6e 20 74 68  w table is on th
1f58e 65 20 74 6f 70 20 6f 66 20 74 68 65 20 76 64 62  e top of the vdb
1f58f 65 20 73 74 61 63 6b 2e 0a 20 20 20 20 2a 2a 0a  e stack..    **.
1f590 20 20 20 20 2a 2a 20 4f 6e 63 65 20 74 68 65 20      ** Once the 
1f591 53 45 4c 45 43 54 20 68 61 73 20 62 65 65 6e 20  SELECT has been 
1f592 63 6f 64 65 64 20 62 79 20 73 71 6c 69 74 65 33  coded by sqlite3
1f593 53 65 6c 65 63 74 28 29 2c 20 69 74 20 69 73 20  Select(), it is 
1f594 69 6e 20 61 0a 20 20 20 20 2a 2a 20 73 75 69 74  in a.    ** suit
1f595 61 62 6c 65 20 73 74 61 74 65 20 74 6f 20 71 75  able state to qu
1f596 65 72 79 20 66 6f 72 20 74 68 65 20 63 6f 6c 75  ery for the colu
1f597 6d 6e 20 6e 61 6d 65 73 20 61 6e 64 20 74 79 70  mn names and typ
1f598 65 73 20 74 6f 20 62 65 20 75 73 65 64 0a 20 20  es to be used.  
1f599 20 20 2a 2a 20 62 79 20 74 68 65 20 6e 65 77 20    ** by the new 
1f59a 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 0a 20 20  table..    **.  
1f59b 20 20 2a 2a 20 41 20 73 68 61 72 65 64 2d 63 61    ** A shared-ca
1f59c 63 68 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 69  che write-lock i
1f59d 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64 20 74  s not required t
1f59e 6f 20 77 72 69 74 65 20 74 6f 20 74 68 65 20 6e  o write to the n
1f59f 65 77 20 74 61 62 6c 65 2c 0a 20 20 20 20 2a 2a  ew table,.    **
1f5a0 20 61 73 20 61 20 73 63 68 65 6d 61 2d 6c 6f 63   as a schema-loc
1f5a1 6b 20 6d 75 73 74 20 68 61 76 65 20 61 6c 72 65  k must have alre
1f5a2 61 64 79 20 62 65 65 6e 20 6f 62 74 61 69 6e 65  ady been obtaine
1f5a3 64 20 74 6f 20 63 72 65 61 74 65 20 69 74 2e 20  d to create it. 
1f5a4 53 69 6e 63 65 0a 20 20 20 20 2a 2a 20 61 20 73  Since.    ** a s
1f5a5 63 68 65 6d 61 2d 6c 6f 63 6b 20 65 78 63 6c 75  chema-lock exclu
1f5a6 64 65 73 20 61 6c 6c 20 6f 74 68 65 72 20 64 61  des all other da
1f5a7 74 61 62 61 73 65 20 75 73 65 72 73 2c 20 74 68  tabase users, th
1f5a8 65 20 77 72 69 74 65 2d 6c 6f 63 6b 20 77 6f 75  e write-lock wou
1f5a9 6c 64 0a 20 20 20 20 2a 2a 20 62 65 20 72 65 64  ld.    ** be red
1f5aa 75 6e 64 61 6e 74 2e 0a 20 20 20 20 2a 2f 0a 20  undant..    */. 
1f5ab 20 20 20 69 66 28 20 70 53 65 6c 65 63 74 20 29     if( pSelect )
1f5ac 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 44 65  {.      SelectDe
1f5ad 73 74 20 64 65 73 74 3b 0a 20 20 20 20 20 20 54  st dest;.      T
1f5ae 61 62 6c 65 20 2a 70 53 65 6c 54 61 62 3b 0a 0a  able *pSelTab;..
1f5af 20 20 20 20 20 20 61 73 73 65 72 74 28 70 50 61        assert(pPa
1f5b0 72 73 65 2d 3e 6e 54 61 62 3d 3d 30 29 3b 0a 20  rse->nTab==0);. 
1f5b1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
1f5b2 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4f 70 65  AddOp3(v, OP_Ope
1f5b3 6e 57 72 69 74 65 2c 20 31 2c 20 70 50 61 72 73  nWrite, 1, pPars
1f5b4 65 2d 3e 72 65 67 52 6f 6f 74 2c 20 69 44 62 29  e->regRoot, iDb)
1f5b5 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1f5b6 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 31  dbeChangeP5(v, 1
1f5b7 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
1f5b8 3e 6e 54 61 62 20 3d 20 32 3b 0a 20 20 20 20 20  >nTab = 2;.     
1f5b9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
1f5ba 73 74 49 6e 69 74 28 26 64 65 73 74 2c 20 53 52  stInit(&dest, SR
1f5bb 54 5f 54 61 62 6c 65 2c 20 31 29 3b 0a 20 20 20  T_Table, 1);.   
1f5bc 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
1f5bd 28 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74  (pParse, pSelect
1f5be 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20  , &dest);.      
1f5bf 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1f5c0 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 31  1(v, OP_Close, 1
1f5c1 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  );.      if( pPa
1f5c2 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b 0a  rse->nErr==0 ){.
1f5c3 20 20 20 20 20 20 20 20 70 53 65 6c 54 61 62 20          pSelTab 
1f5c4 3d 20 73 71 6c 69 74 65 33 52 65 73 75 6c 74 53  = sqlite3ResultS
1f5c5 65 74 4f 66 53 65 6c 65 63 74 28 70 50 61 72 73  etOfSelect(pPars
1f5c6 65 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  e, pSelect);.   
1f5c7 20 20 20 20 20 69 66 28 20 70 53 65 6c 54 61 62       if( pSelTab
1f5c8 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
1f5c9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
1f5ca 3e 61 43 6f 6c 3d 3d 30 20 29 3b 0a 20 20 20 20  >aCol==0 );.    
1f5cb 20 20 20 20 70 2d 3e 6e 43 6f 6c 20 3d 20 70 53      p->nCol = pS
1f5cc 65 6c 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20 20 20  elTab->nCol;.   
1f5cd 20 20 20 20 20 70 2d 3e 61 43 6f 6c 20 3d 20 70       p->aCol = p
1f5ce 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20  SelTab->aCol;.  
1f5cf 20 20 20 20 20 20 70 53 65 6c 54 61 62 2d 3e 6e        pSelTab->n
1f5d0 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  Col = 0;.       
1f5d1 20 70 53 65 6c 54 61 62 2d 3e 61 43 6f 6c 20 3d   pSelTab->aCol =
1f5d2 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   0;.        sqli
1f5d3 74 65 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70  te3DeleteTable(p
1f5d4 53 65 6c 54 61 62 29 3b 0a 20 20 20 20 20 20 7d  SelTab);.      }
1f5d5 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
1f5d6 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70 6c  ompute the compl
1f5d7 65 74 65 20 74 65 78 74 20 6f 66 20 74 68 65 20  ete text of the 
1f5d8 43 52 45 41 54 45 20 73 74 61 74 65 6d 65 6e 74  CREATE statement
1f5d9 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53 65 6c   */.    if( pSel
1f5da 65 63 74 20 29 7b 0a 20 20 20 20 20 20 7a 53 74  ect ){.      zSt
1f5db 6d 74 20 3d 20 63 72 65 61 74 65 54 61 62 6c 65  mt = createTable
1f5dc 53 74 6d 74 28 64 62 2c 20 70 2c 20 70 2d 3e 70  Stmt(db, p, p->p
1f5dd 53 63 68 65 6d 61 3d 3d 64 62 2d 3e 61 44 62 5b  Schema==db->aDb[
1f5de 31 5d 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  1].pSchema);.   
1f5df 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 20   }else{.      n 
1f5e0 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61 72  = pEnd->z - pPar
1f5e1 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 7a  se->sNameToken.z
1f5e2 20 2b 20 31 3b 0a 20 20 20 20 20 20 7a 53 74 6d   + 1;.      zStm
1f5e3 74 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  t = sqlite3MPrin
1f5e4 74 66 28 64 62 2c 20 0a 20 20 20 20 20 20 20 20  tf(db, .        
1f5e5 20 20 22 43 52 45 41 54 45 20 25 73 20 25 2e 2a    "CREATE %s %.*
1f5e6 73 22 2c 20 7a 54 79 70 65 32 2c 20 6e 2c 20 70  s", zType2, n, p
1f5e7 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65  Parse->sNameToke
1f5e8 6e 2e 7a 0a 20 20 20 20 20 20 29 3b 0a 20 20 20  n.z.      );.   
1f5e9 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f   }..    /* A slo
1f5ea 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  t for the record
1f5eb 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
1f5ec 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74  n allocated in t
1f5ed 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54  he .    ** SQLIT
1f5ee 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20  E_MASTER table. 
1f5ef 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f   We just need to
1f5f0 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f   update that slo
1f5f1 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a  t with all.    *
1f5f2 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  * the informatio
1f5f3 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65  n we've collecte
1f5f4 64 2e 20 20 54 68 65 20 72 6f 77 69 64 20 66 6f  d.  The rowid fo
1f5f5 72 20 74 68 65 20 70 72 65 61 6c 6c 6f 63 61 74  r the preallocat
1f5f6 65 64 0a 20 20 20 20 2a 2a 20 73 6c 6f 74 20 69  ed.    ** slot i
1f5f7 73 20 74 68 65 20 32 6e 64 20 69 74 65 6d 20 6f  s the 2nd item o
1f5f8 6e 20 74 68 65 20 73 74 61 63 6b 2e 20 20 54 68  n the stack.  Th
1f5f9 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61  e top of the sta
1f5fa 63 6b 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a  ck is the.    **
1f5fb 20 72 6f 6f 74 20 70 61 67 65 20 66 6f 72 20 74   root page for t
1f5fc 68 65 20 6e 65 77 20 74 61 62 6c 65 20 28 6f 72  he new table (or
1f5fd 20 61 20 30 20 69 66 20 74 68 69 73 20 69 73 20   a 0 if this is 
1f5fe 61 20 76 69 65 77 29 2e 0a 20 20 20 20 2a 2f 0a  a view)..    */.
1f5ff 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
1f600 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
1f601 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e       "UPDATE %Q.
1f602 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22 53  %s ".         "S
1f603 45 54 20 74 79 70 65 3d 27 25 73 27 2c 20 6e 61  ET type='%s', na
1f604 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61 6d 65 3d  me=%Q, tbl_name=
1f605 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d 23 25 64  %Q, rootpage=#%d
1f606 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20 20  , sql=%Q ".     
1f607 20 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d 23    "WHERE rowid=#
1f608 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e 61  %d",.      db->a
1f609 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
1f60a 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
1f60b 2c 0a 20 20 20 20 20 20 7a 54 79 70 65 2c 0a 20  ,.      zType,. 
1f60c 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20       p->zName,. 
1f60d 20 20 20 20 20 70 2d 3e 7a 4e 61 6d 65 2c 0a 20       p->zName,. 
1f60e 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72 65 67       pParse->reg
1f60f 52 6f 6f 74 2c 0a 20 20 20 20 20 20 7a 53 74 6d  Root,.      zStm
1f610 74 2c 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  t,.      pParse-
1f611 3e 72 65 67 52 6f 77 69 64 0a 20 20 20 20 29 3b  >regRowid.    );
1f612 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
1f613 65 65 28 64 62 2c 20 7a 53 74 6d 74 29 3b 0a 20  ee(db, zStmt);. 
1f614 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67 65     sqlite3Change
1f615 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20 69  Cookie(pParse, i
1f616 44 62 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51  Db);..#ifndef SQ
1f617 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e  LITE_OMIT_AUTOIN
1f618 43 52 45 4d 45 4e 54 0a 20 20 20 20 2f 2a 20 43  CREMENT.    /* C
1f619 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 77  heck to see if w
1f61a 65 20 6e 65 65 64 20 74 6f 20 63 72 65 61 74 65  e need to create
1f61b 20 61 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65   an sqlite_seque
1f61c 6e 63 65 20 74 61 62 6c 65 20 66 6f 72 0a 20 20  nce table for.  
1f61d 20 20 2a 2a 20 6b 65 65 70 69 6e 67 20 74 72 61    ** keeping tra
1f61e 63 6b 20 6f 66 20 61 75 74 6f 69 6e 63 72 65 6d  ck of autoincrem
1f61f 65 6e 74 20 6b 65 79 73 2e 0a 20 20 20 20 2a 2f  ent keys..    */
1f620 0a 20 20 20 20 69 66 28 20 70 2d 3e 74 61 62 46  .    if( p->tabF
1f621 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69 6e  lags & TF_Autoin
1f622 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20 20  crement ){.     
1f623 20 44 62 20 2a 70 44 62 20 3d 20 26 64 62 2d 3e   Db *pDb = &db->
1f624 61 44 62 5b 69 44 62 5d 3b 0a 20 20 20 20 20 20  aDb[iDb];.      
1f625 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  if( pDb->pSchema
1f626 2d 3e 70 53 65 71 54 61 62 3d 3d 30 20 29 7b 0a  ->pSeqTab==0 ){.
1f627 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e          sqlite3N
1f628 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
1f629 65 2c 0a 20 20 20 20 20 20 20 20 20 20 22 43 52  e,.          "CR
1f62a 45 41 54 45 20 54 41 42 4c 45 20 25 51 2e 73 71  EATE TABLE %Q.sq
1f62b 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 28 6e 61  lite_sequence(na
1f62c 6d 65 2c 73 65 71 29 22 2c 0a 20 20 20 20 20 20  me,seq)",.      
1f62d 20 20 20 20 70 44 62 2d 3e 7a 4e 61 6d 65 0a 20      pDb->zName. 
1f62e 20 20 20 20 20 20 20 29 3b 0a 20 20 20 20 20 20         );.      
1f62f 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  }.    }.#endif..
1f630 20 20 20 20 2f 2a 20 52 65 70 61 72 73 65 20 65      /* Reparse e
1f631 76 65 72 79 74 68 69 6e 67 20 74 6f 20 75 70 64  verything to upd
1f632 61 74 65 20 6f 75 72 20 69 6e 74 65 72 6e 61 6c  ate our internal
1f633 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
1f634 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
1f635 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1f636 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62  ParseSchema, iDb
1f637 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 0, 0,.        
1f638 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1f639 62 2c 20 22 74 62 6c 5f 6e 61 6d 65 3d 27 25 71  b, "tbl_name='%q
1f63a 27 22 2c 70 2d 3e 7a 4e 61 6d 65 29 2c 20 50 34  '",p->zName), P4
1f63b 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 7d 0a 0a  _DYNAMIC);.  }..
1f63c 0a 20 20 2f 2a 20 41 64 64 20 74 68 65 20 74 61  .  /* Add the ta
1f63d 62 6c 65 20 74 6f 20 74 68 65 20 69 6e 2d 6d 65  ble to the in-me
1f63e 6d 6f 72 79 20 72 65 70 72 65 73 65 6e 74 61 74  mory representat
1f63f 69 6f 6e 20 6f 66 20 74 68 65 20 64 61 74 61 62  ion of the datab
1f640 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ase..  */.  if( 
1f641 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26  db->init.busy &&
1f642 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30   pParse->nErr==0
1f643 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
1f644 4f 6c 64 3b 0a 20 20 20 20 46 4b 65 79 20 2a 70  Old;.    FKey *p
1f645 46 4b 65 79 3b 20 0a 20 20 20 20 53 63 68 65 6d  FKey; .    Schem
1f646 61 20 2a 70 53 63 68 65 6d 61 20 3d 20 70 2d 3e  a *pSchema = p->
1f647 70 53 63 68 65 6d 61 3b 0a 20 20 20 20 70 4f 6c  pSchema;.    pOl
1f648 64 20 3d 20 73 71 6c 69 74 65 33 48 61 73 68 49  d = sqlite3HashI
1f649 6e 73 65 72 74 28 26 70 53 63 68 65 6d 61 2d 3e  nsert(&pSchema->
1f64a 74 62 6c 48 61 73 68 2c 20 70 2d 3e 7a 4e 61 6d  tblHash, p->zNam
1f64b 65 2c 20 73 74 72 6c 65 6e 28 70 2d 3e 7a 4e 61  e, strlen(p->zNa
1f64c 6d 65 29 2b 31 2c 70 29 3b 0a 20 20 20 20 69 66  me)+1,p);.    if
1f64d 28 20 70 4f 6c 64 20 29 7b 0a 20 20 20 20 20 20  ( pOld ){.      
1f64e 61 73 73 65 72 74 28 20 70 3d 3d 70 4f 6c 64 20  assert( p==pOld 
1f64f 29 3b 20 20 2f 2a 20 4d 61 6c 6c 6f 63 20 6d 75  );  /* Malloc mu
1f650 73 74 20 68 61 76 65 20 66 61 69 6c 65 64 20 69  st have failed i
1f651 6e 73 69 64 65 20 48 61 73 68 49 6e 73 65 72 74  nside HashInsert
1f652 28 29 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e  () */.      db->
1f653 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31  mallocFailed = 1
1f654 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  ;.      return;.
1f655 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51      }.#ifndef SQ
1f656 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47  LITE_OMIT_FOREIG
1f657 4e 5f 4b 45 59 0a 20 20 20 20 66 6f 72 28 70 46  N_KEY.    for(pF
1f658 4b 65 79 3d 70 2d 3e 70 46 4b 65 79 3b 20 70 46  Key=p->pFKey; pF
1f659 4b 65 79 3b 20 70 46 4b 65 79 3d 70 46 4b 65 79  Key; pFKey=pFKey
1f65a 2d 3e 70 4e 65 78 74 46 72 6f 6d 29 7b 0a 20 20  ->pNextFrom){.  
1f65b 20 20 20 20 76 6f 69 64 20 2a 64 61 74 61 3b 0a      void *data;.
1f65c 20 20 20 20 20 20 69 6e 74 20 6e 54 6f 20 3d 20        int nTo = 
1f65d 73 74 72 6c 65 6e 28 70 46 4b 65 79 2d 3e 7a 54  strlen(pFKey->zT
1f65e 6f 29 20 2b 20 31 3b 0a 20 20 20 20 20 20 70 46  o) + 1;.      pF
1f65f 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d 20 73  Key->pNextTo = s
1f660 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
1f661 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79 2c 20  pSchema->aFKey, 
1f662 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54 6f 29  pFKey->zTo, nTo)
1f663 3b 0a 20 20 20 20 20 20 64 61 74 61 20 3d 20 73  ;.      data = s
1f664 71 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74  qlite3HashInsert
1f665 28 26 70 53 63 68 65 6d 61 2d 3e 61 46 4b 65 79  (&pSchema->aFKey
1f666 2c 20 70 46 4b 65 79 2d 3e 7a 54 6f 2c 20 6e 54  , pFKey->zTo, nT
1f667 6f 2c 20 70 46 4b 65 79 29 3b 0a 20 20 20 20 20  o, pFKey);.     
1f668 20 69 66 28 20 64 61 74 61 3d 3d 28 76 6f 69 64   if( data==(void
1f669 20 2a 29 70 46 4b 65 79 20 29 7b 0a 20 20 20 20   *)pFKey ){.    
1f66a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
1f66b 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
1f66c 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  }.    }.#endif. 
1f66d 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54     pParse->pNewT
1f66e 61 62 6c 65 20 3d 20 30 3b 0a 20 20 20 20 64 62  able = 0;.    db
1f66f 2d 3e 6e 54 61 62 6c 65 2b 2b 3b 0a 20 20 20 20  ->nTable++;.    
1f670 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
1f671 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67 65  ITE_InternChange
1f672 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  s;..#ifndef SQLI
1f673 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54 41 42  TE_OMIT_ALTERTAB
1f674 4c 45 0a 20 20 20 20 69 66 28 20 21 70 2d 3e 70  LE.    if( !p->p
1f675 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20  Select ){.      
1f676 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d  const char *zNam
1f677 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
1f678 2a 29 70 50 61 72 73 65 2d 3e 73 4e 61 6d 65 54  *)pParse->sNameT
1f679 6f 6b 65 6e 2e 7a 3b 0a 20 20 20 20 20 20 69 6e  oken.z;.      in
1f67a 74 20 6e 4e 61 6d 65 3b 0a 20 20 20 20 20 20 61  t nName;.      a
1f67b 73 73 65 72 74 28 20 21 70 53 65 6c 65 63 74 20  ssert( !pSelect 
1f67c 26 26 20 70 43 6f 6e 73 20 26 26 20 70 45 6e 64  && pCons && pEnd
1f67d 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70 43   );.      if( pC
1f67e 6f 6e 73 2d 3e 7a 3d 3d 30 20 29 7b 0a 20 20 20  ons->z==0 ){.   
1f67f 20 20 20 20 20 70 43 6f 6e 73 20 3d 20 70 45 6e       pCons = pEn
1f680 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  d;.      }.     
1f681 20 6e 4e 61 6d 65 20 3d 20 28 63 6f 6e 73 74 20   nName = (const 
1f682 63 68 61 72 20 2a 29 70 43 6f 6e 73 2d 3e 7a 20  char *)pCons->z 
1f683 2d 20 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 70  - zName;.      p
1f684 2d 3e 61 64 64 43 6f 6c 4f 66 66 73 65 74 20 3d  ->addColOffset =
1f685 20 31 33 20 2b 20 73 71 6c 69 74 65 33 55 74 66   13 + sqlite3Utf
1f686 38 43 68 61 72 4c 65 6e 28 7a 4e 61 6d 65 2c 20  8CharLen(zName, 
1f687 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 23 65  nName);.    }.#e
1f688 6e 64 69 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e  ndif.  }.}..#ifn
1f689 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f68a 56 49 45 57 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70  VIEW./*.** The p
1f68b 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73  arser calls this
1f68c 20 72 6f 75 74 69 6e 65 20 69 6e 20 6f 72 64 65   routine in orde
1f68d 72 20 74 6f 20 63 72 65 61 74 65 20 61 20 6e 65  r to create a ne
1f68e 77 20 56 49 45 57 0a 2a 2f 0a 53 51 4c 49 54 45  w VIEW.*/.SQLITE
1f68f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1f690 6c 69 74 65 33 43 72 65 61 74 65 56 69 65 77 28  lite3CreateView(
1f691 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1f692 2c 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72  ,     /* The par
1f693 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
1f694 20 20 54 6f 6b 65 6e 20 2a 70 42 65 67 69 6e 2c    Token *pBegin,
1f695 20 20 20 20 20 2f 2a 20 54 68 65 20 43 52 45 41       /* The CREA
1f696 54 45 20 74 6f 6b 65 6e 20 74 68 61 74 20 62 65  TE token that be
1f697 67 69 6e 73 20 74 68 65 20 73 74 61 74 65 6d 65  gins the stateme
1f698 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
1f699 4e 61 6d 65 31 2c 20 20 20 20 20 2f 2a 20 54 68  Name1,     /* Th
1f69a 65 20 74 6f 6b 65 6e 20 74 68 61 74 20 68 6f 6c  e token that hol
1f69b 64 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74  ds the name of t
1f69c 68 65 20 76 69 65 77 20 2a 2f 0a 20 20 54 6f 6b  he view */.  Tok
1f69d 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20  en *pName2,     
1f69e 2f 2a 20 54 68 65 20 74 6f 6b 65 6e 20 74 68 61  /* The token tha
1f69f 74 20 68 6f 6c 64 73 20 74 68 65 20 6e 61 6d 65  t holds the name
1f6a0 20 6f 66 20 74 68 65 20 76 69 65 77 20 2a 2f 0a   of the view */.
1f6a1 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
1f6a2 74 2c 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54  t,   /* A SELECT
1f6a3 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74 20   statement that 
1f6a4 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 20  will become the 
1f6a5 6e 65 77 20 76 69 65 77 20 2a 2f 0a 20 20 69 6e  new view */.  in
1f6a6 74 20 69 73 54 65 6d 70 2c 20 20 20 20 20 20 20  t isTemp,       
1f6a7 20 2f 2a 20 54 52 55 45 20 66 6f 72 20 61 20 54   /* TRUE for a T
1f6a8 45 4d 50 4f 52 41 52 59 20 76 69 65 77 20 2a 2f  EMPORARY view */
1f6a9 0a 20 20 69 6e 74 20 6e 6f 45 72 72 20 20 20 20  .  int noErr    
1f6aa 20 20 20 20 20 20 2f 2a 20 53 75 70 70 72 65 73        /* Suppres
1f6ab 73 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73  s error messages
1f6ac 20 69 66 20 56 49 45 57 20 61 6c 72 65 61 64 79   if VIEW already
1f6ad 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a 20 20   exists */.){.  
1f6ae 54 61 62 6c 65 20 2a 70 3b 0a 20 20 69 6e 74 20  Table *p;.  int 
1f6af 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  n;.  const unsig
1f6b0 6e 65 64 20 63 68 61 72 20 2a 7a 3b 0a 20 20 54  ned char *z;.  T
1f6b1 6f 6b 65 6e 20 73 45 6e 64 3b 0a 20 20 44 62 46  oken sEnd;.  DbF
1f6b2 69 78 65 72 20 73 46 69 78 3b 0a 20 20 54 6f 6b  ixer sFix;.  Tok
1f6b3 65 6e 20 2a 70 4e 61 6d 65 3b 0a 20 20 69 6e 74  en *pName;.  int
1f6b4 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65 33 20   iDb;.  sqlite3 
1f6b5 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
1f6b6 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d  ;..  if( pParse-
1f6b7 3e 6e 56 61 72 3e 30 20 29 7b 0a 20 20 20 20 73  >nVar>0 ){.    s
1f6b8 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1f6b9 50 61 72 73 65 2c 20 22 70 61 72 61 6d 65 74 65  Parse, "paramete
1f6ba 72 73 20 61 72 65 20 6e 6f 74 20 61 6c 6c 6f 77  rs are not allow
1f6bb 65 64 20 69 6e 20 76 69 65 77 73 22 29 3b 0a 20  ed in views");. 
1f6bc 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
1f6bd 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65  Delete(db, pSele
1f6be 63 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ct);.    return;
1f6bf 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 74  .  }.  sqlite3St
1f6c0 61 72 74 54 61 62 6c 65 28 70 50 61 72 73 65 2c  artTable(pParse,
1f6c1 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
1f6c2 20 69 73 54 65 6d 70 2c 20 31 2c 20 30 2c 20 6e   isTemp, 1, 0, n
1f6c3 6f 45 72 72 29 3b 0a 20 20 70 20 3d 20 70 50 61  oErr);.  p = pPa
1f6c4 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b 0a  rse->pNewTable;.
1f6c5 20 20 69 66 28 20 70 3d 3d 30 20 7c 7c 20 70 50    if( p==0 || pP
1f6c6 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20  arse->nErr ){.  
1f6c7 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
1f6c8 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63  elete(db, pSelec
1f6c9 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  t);.    return;.
1f6ca 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 54 77 6f    }.  sqlite3Two
1f6cb 50 61 72 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  PartName(pParse,
1f6cc 20 70 4e 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c   pName1, pName2,
1f6cd 20 26 70 4e 61 6d 65 29 3b 0a 20 20 69 44 62 20   &pName);.  iDb 
1f6ce 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
1f6cf 6f 49 6e 64 65 78 28 64 62 2c 20 70 2d 3e 70 53  oIndex(db, p->pS
1f6d0 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 73 71  chema);.  if( sq
1f6d1 6c 69 74 65 33 46 69 78 49 6e 69 74 28 26 73 46  lite3FixInit(&sF
1f6d2 69 78 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c  ix, pParse, iDb,
1f6d3 20 22 76 69 65 77 22 2c 20 70 4e 61 6d 65 29 0a   "view", pName).
1f6d4 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 46 69      && sqlite3Fi
1f6d5 78 53 65 6c 65 63 74 28 26 73 46 69 78 2c 20 70  xSelect(&sFix, p
1f6d6 53 65 6c 65 63 74 29 0a 20 20 29 7b 0a 20 20 20  Select).  ){.   
1f6d7 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
1f6d8 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
1f6d9 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
1f6da 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 61 20   }..  /* Make a 
1f6db 63 6f 70 79 20 6f 66 20 74 68 65 20 65 6e 74 69  copy of the enti
1f6dc 72 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  re SELECT statem
1f6dd 65 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73  ent that defines
1f6de 20 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2a 20   the view..  ** 
1f6df 54 68 69 73 20 77 69 6c 6c 20 66 6f 72 63 65 20  This will force 
1f6e0 61 6c 6c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b  all the Expr.tok
1f6e1 65 6e 2e 7a 20 76 61 6c 75 65 73 20 74 6f 20 62  en.z values to b
1f6e2 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 0a 20 20  e dynamically.  
1f6e3 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 72 61 74  ** allocated rat
1f6e4 68 65 72 20 74 68 61 6e 20 70 6f 69 6e 74 20 74  her than point t
1f6e5 6f 20 74 68 65 20 69 6e 70 75 74 20 73 74 72 69  o the input stri
1f6e6 6e 67 20 2d 20 77 68 69 63 68 20 6d 65 61 6e 73  ng - which means
1f6e7 20 74 68 61 74 0a 20 20 2a 2a 20 74 68 65 79 20   that.  ** they 
1f6e8 77 69 6c 6c 20 70 65 72 73 69 73 74 20 61 66 74  will persist aft
1f6e9 65 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 73  er the current s
1f6ea 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 63 61  qlite3_exec() ca
1f6eb 6c 6c 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f  ll returns..  */
1f6ec 0a 20 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20  .  p->pSelect = 
1f6ed 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
1f6ee 28 64 62 2c 20 70 53 65 6c 65 63 74 29 3b 0a 20  (db, pSelect);. 
1f6ef 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
1f6f0 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74  lete(db, pSelect
1f6f1 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
1f6f2 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20  locFailed ){.   
1f6f3 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 69   return;.  }.  i
1f6f4 66 28 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73  f( !db->init.bus
1f6f5 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  y ){.    sqlite3
1f6f6 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d  ViewGetColumnNam
1f6f7 65 73 28 70 50 61 72 73 65 2c 20 70 29 3b 0a 20  es(pParse, p);. 
1f6f8 20 7d 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20   }..  /* Locate 
1f6f9 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 43  the end of the C
1f6fa 52 45 41 54 45 20 56 49 45 57 20 73 74 61 74 65  REATE VIEW state
1f6fb 6d 65 6e 74 2e 20 20 4d 61 6b 65 20 73 45 6e 64  ment.  Make sEnd
1f6fc 20 70 6f 69 6e 74 20 74 6f 0a 20 20 2a 2a 20 74   point to.  ** t
1f6fd 68 65 20 65 6e 64 2e 0a 20 20 2a 2f 0a 20 20 73  he end..  */.  s
1f6fe 45 6e 64 20 3d 20 70 50 61 72 73 65 2d 3e 73 4c  End = pParse->sL
1f6ff 61 73 74 54 6f 6b 65 6e 3b 0a 20 20 69 66 28 20  astToken;.  if( 
1f700 73 45 6e 64 2e 7a 5b 30 5d 21 3d 30 20 26 26 20  sEnd.z[0]!=0 && 
1f701 73 45 6e 64 2e 7a 5b 30 5d 21 3d 27 3b 27 20 29  sEnd.z[0]!=';' )
1f702 7b 0a 20 20 20 20 73 45 6e 64 2e 7a 20 2b 3d 20  {.    sEnd.z += 
1f703 73 45 6e 64 2e 6e 3b 0a 20 20 7d 0a 20 20 73 45  sEnd.n;.  }.  sE
1f704 6e 64 2e 6e 20 3d 20 30 3b 0a 20 20 6e 20 3d 20  nd.n = 0;.  n = 
1f705 73 45 6e 64 2e 7a 20 2d 20 70 42 65 67 69 6e 2d  sEnd.z - pBegin-
1f706 3e 7a 3b 0a 20 20 7a 20 3d 20 28 63 6f 6e 73 74  >z;.  z = (const
1f707 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29   unsigned char*)
1f708 70 42 65 67 69 6e 2d 3e 7a 3b 0a 20 20 77 68 69  pBegin->z;.  whi
1f709 6c 65 28 20 6e 3e 30 20 26 26 20 28 7a 5b 6e 2d  le( n>0 && (z[n-
1f70a 31 5d 3d 3d 27 3b 27 20 7c 7c 20 69 73 73 70 61  1]==';' || isspa
1f70b 63 65 28 7a 5b 6e 2d 31 5d 29 29 20 29 7b 20 6e  ce(z[n-1])) ){ n
1f70c 2d 2d 3b 20 7d 0a 20 20 73 45 6e 64 2e 7a 20 3d  --; }.  sEnd.z =
1f70d 20 26 7a 5b 6e 2d 31 5d 3b 0a 20 20 73 45 6e 64   &z[n-1];.  sEnd
1f70e 2e 6e 20 3d 20 31 3b 0a 0a 20 20 2f 2a 20 55 73  .n = 1;..  /* Us
1f70f 65 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c  e sqlite3EndTabl
1f710 65 28 29 20 74 6f 20 61 64 64 20 74 68 65 20 76  e() to add the v
1f711 69 65 77 20 74 6f 20 74 68 65 20 53 51 4c 49 54  iew to the SQLIT
1f712 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20 2a  E_MASTER table *
1f713 2f 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61  /.  sqlite3EndTa
1f714 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c 20 26  ble(pParse, 0, &
1f715 73 45 6e 64 2c 20 30 29 3b 0a 20 20 72 65 74 75  sEnd, 0);.  retu
1f716 72 6e 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  rn;.}.#endif /* 
1f717 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1f718 20 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65   */..#if !define
1f719 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  d(SQLITE_OMIT_VI
1f71a 45 57 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28  EW) || !defined(
1f71b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1f71c 55 41 4c 54 41 42 4c 45 29 0a 2f 2a 0a 2a 2a 20  UALTABLE)./*.** 
1f71d 54 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74  The Table struct
1f71e 75 72 65 20 70 54 61 62 6c 65 20 69 73 20 72 65  ure pTable is re
1f71f 61 6c 6c 79 20 61 20 56 49 45 57 2e 20 20 46 69  ally a VIEW.  Fi
1f720 6c 6c 20 69 6e 20 74 68 65 20 6e 61 6d 65 73 20  ll in the names 
1f721 6f 66 0a 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e  of.** the column
1f722 73 20 6f 66 20 74 68 65 20 76 69 65 77 20 69 6e  s of the view in
1f723 20 74 68 65 20 70 54 61 62 6c 65 20 73 74 72 75   the pTable stru
1f724 63 74 75 72 65 2e 20 20 52 65 74 75 72 6e 20 74  cture.  Return t
1f725 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f 66 20  he number.** of 
1f726 65 72 72 6f 72 73 2e 20 20 49 66 20 61 6e 20 65  errors.  If an e
1f727 72 72 6f 72 20 69 73 20 73 65 65 6e 20 6c 65 61  rror is seen lea
1f728 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
1f729 61 67 65 20 69 6e 20 70 50 61 72 73 65 2d 3e 7a  age in pParse->z
1f72a 45 72 72 4d 73 67 2e 0a 2a 2f 0a 53 51 4c 49 54  ErrMsg..*/.SQLIT
1f72b 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
1f72c 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
1f72d 6d 6e 4e 61 6d 65 73 28 50 61 72 73 65 20 2a 70  mnNames(Parse *p
1f72e 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a 70 54  Parse, Table *pT
1f72f 61 62 6c 65 29 7b 0a 20 20 54 61 62 6c 65 20 2a  able){.  Table *
1f730 70 53 65 6c 54 61 62 3b 20 20 20 2f 2a 20 41 20  pSelTab;   /* A 
1f731 66 61 6b 65 20 74 61 62 6c 65 20 66 72 6f 6d 20  fake table from 
1f732 77 68 69 63 68 20 77 65 20 67 65 74 20 74 68 65  which we get the
1f733 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 20   result set */. 
1f734 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 3b 20 20   Select *pSel;  
1f735 20 20 20 2f 2a 20 43 6f 70 79 20 6f 66 20 74 68     /* Copy of th
1f736 65 20 53 45 4c 45 43 54 20 74 68 61 74 20 69 6d  e SELECT that im
1f737 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 76 69 65  plements the vie
1f738 77 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 72 72 20  w */.  int nErr 
1f739 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62  = 0;     /* Numb
1f73a 65 72 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63  er of errors enc
1f73b 6f 75 6e 74 65 72 65 64 20 2a 2f 0a 20 20 69 6e  ountered */.  in
1f73c 74 20 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20  t n;            
1f73d 2f 2a 20 54 65 6d 70 6f 72 61 72 69 6c 79 20 68  /* Temporarily h
1f73e 6f 6c 64 73 20 74 68 65 20 6e 75 6d 62 65 72 20  olds the number 
1f73f 6f 66 20 63 75 72 73 6f 72 73 20 61 73 73 69 67  of cursors assig
1f740 6e 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ned */.  sqlite3
1f741 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
1f742 62 3b 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20  b;  /* Database 
1f743 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20 6d  connection for m
1f744 61 6c 6c 6f 63 20 65 72 72 6f 72 73 20 2a 2f 0a  alloc errors */.
1f745 20 20 69 6e 74 20 28 2a 78 41 75 74 68 29 28 76    int (*xAuth)(v
1f746 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63  oid*,int,const c
1f747 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
1f748 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e  ,const char*,con
1f749 73 74 20 63 68 61 72 2a 29 3b 0a 0a 20 20 61 73  st char*);..  as
1f74a 73 65 72 74 28 20 70 54 61 62 6c 65 20 29 3b 0a  sert( pTable );.
1f74b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
1f74c 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
1f74d 45 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 56  E.  if( sqlite3V
1f74e 74 61 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 70  tabCallConnect(p
1f74f 50 61 72 73 65 2c 20 70 54 61 62 6c 65 29 20 29  Parse, pTable) )
1f750 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  {.    return SQL
1f751 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a 20  ITE_ERROR;.  }. 
1f752 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28 70   if( IsVirtual(p
1f753 54 61 62 6c 65 29 20 29 20 72 65 74 75 72 6e 20  Table) ) return 
1f754 30 3b 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e 64  0;.#endif..#ifnd
1f755 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
1f756 49 45 57 0a 20 20 2f 2a 20 41 20 70 6f 73 69 74  IEW.  /* A posit
1f757 69 76 65 20 6e 43 6f 6c 20 6d 65 61 6e 73 20 74  ive nCol means t
1f758 68 65 20 63 6f 6c 75 6d 6e 73 20 6e 61 6d 65 73  he columns names
1f759 20 66 6f 72 20 74 68 69 73 20 76 69 65 77 20 61   for this view a
1f75a 72 65 0a 20 20 2a 2a 20 61 6c 72 65 61 64 79 20  re.  ** already 
1f75b 6b 6e 6f 77 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66  known..  */.  if
1f75c 28 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 3e 30  ( pTable->nCol>0
1f75d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20   ) return 0;..  
1f75e 2f 2a 20 41 20 6e 65 67 61 74 69 76 65 20 6e 43  /* A negative nC
1f75f 6f 6c 20 69 73 20 61 20 73 70 65 63 69 61 6c 20  ol is a special 
1f760 6d 61 72 6b 65 72 20 6d 65 61 6e 69 6e 67 20 74  marker meaning t
1f761 68 61 74 20 77 65 20 61 72 65 20 63 75 72 72 65  hat we are curre
1f762 6e 74 6c 79 0a 20 20 2a 2a 20 74 72 79 69 6e 67  ntly.  ** trying
1f763 20 74 6f 20 63 6f 6d 70 75 74 65 20 74 68 65 20   to compute the 
1f764 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 49  column names.  I
1f765 66 20 77 65 20 65 6e 74 65 72 20 74 68 69 73 20  f we enter this 
1f766 72 6f 75 74 69 6e 65 20 77 69 74 68 0a 20 20 2a  routine with.  *
1f767 2a 20 61 20 6e 65 67 61 74 69 76 65 20 6e 43 6f  * a negative nCo
1f768 6c 2c 20 69 74 20 6d 65 61 6e 73 20 74 77 6f 20  l, it means two 
1f769 6f 72 20 6d 6f 72 65 20 76 69 65 77 73 20 66 6f  or more views fo
1f76a 72 6d 20 61 20 6c 6f 6f 70 2c 20 6c 69 6b 65 20  rm a loop, like 
1f76b 74 68 69 73 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  this:.  **.  ** 
1f76c 20 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20      CREATE VIEW 
1f76d 6f 6e 65 20 41 53 20 53 45 4c 45 43 54 20 2a 20  one AS SELECT * 
1f76e 46 52 4f 4d 20 74 77 6f 3b 0a 20 20 2a 2a 20 20  FROM two;.  **  
1f76f 20 20 20 43 52 45 41 54 45 20 56 49 45 57 20 74     CREATE VIEW t
1f770 77 6f 20 41 53 20 53 45 4c 45 43 54 20 2a 20 46  wo AS SELECT * F
1f771 52 4f 4d 20 6f 6e 65 3b 0a 20 20 2a 2a 0a 20 20  ROM one;.  **.  
1f772 2a 2a 20 41 63 74 75 61 6c 6c 79 2c 20 74 68 69  ** Actually, thi
1f773 73 20 65 72 72 6f 72 20 69 73 20 63 61 75 67 68  s error is caugh
1f774 74 20 70 72 65 76 69 6f 75 73 6c 79 20 61 6e 64  t previously and
1f775 20 73 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   so the followin
1f776 67 20 74 65 73 74 0a 20 20 2a 2a 20 73 68 6f 75  g test.  ** shou
1f777 6c 64 20 61 6c 77 61 79 73 20 66 61 69 6c 2e 20  ld always fail. 
1f778 20 42 75 74 20 77 65 20 77 69 6c 6c 20 6c 65 61   But we will lea
1f779 76 65 20 69 74 20 69 6e 20 70 6c 61 63 65 20 6a  ve it in place j
1f77a 75 73 74 20 74 6f 20 62 65 20 73 61 66 65 2e 0a  ust to be safe..
1f77b 20 20 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 6c    */.  if( pTabl
1f77c 65 2d 3e 6e 43 6f 6c 3c 30 20 29 7b 0a 20 20 20  e->nCol<0 ){.   
1f77d 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
1f77e 28 70 50 61 72 73 65 2c 20 22 76 69 65 77 20 25  (pParse, "view %
1f77f 73 20 69 73 20 63 69 72 63 75 6c 61 72 6c 79 20  s is circularly 
1f780 64 65 66 69 6e 65 64 22 2c 20 70 54 61 62 6c 65  defined", pTable
1f781 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65  ->zName);.    re
1f782 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 61 73  turn 1;.  }.  as
1f783 73 65 72 74 28 20 70 54 61 62 6c 65 2d 3e 6e 43  sert( pTable->nC
1f784 6f 6c 3e 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20 49  ol>=0 );..  /* I
1f785 66 20 77 65 20 67 65 74 20 74 68 69 73 20 66 61  f we get this fa
1f786 72 2c 20 69 74 20 6d 65 61 6e 73 20 77 65 20 6e  r, it means we n
1f787 65 65 64 20 74 6f 20 63 6f 6d 70 75 74 65 20 74  eed to compute t
1f788 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 73 2e 0a  he table names..
1f789 20 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    ** Note that t
1f78a 68 65 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  he call to sqlit
1f78b 65 33 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c  e3ResultSetOfSel
1f78c 65 63 74 28 29 20 77 69 6c 6c 20 65 78 70 61 6e  ect() will expan
1f78d 64 20 61 6e 79 0a 20 20 2a 2a 20 22 2a 22 20 65  d any.  ** "*" e
1f78e 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 72  lements in the r
1f78f 65 73 75 6c 74 73 20 73 65 74 20 6f 66 20 74 68  esults set of th
1f790 65 20 76 69 65 77 20 61 6e 64 20 77 69 6c 6c 20  e view and will 
1f791 61 73 73 69 67 6e 20 63 75 72 73 6f 72 73 0a 20  assign cursors. 
1f792 20 2a 2a 20 74 6f 20 74 68 65 20 65 6c 65 6d 65   ** to the eleme
1f793 6e 74 73 20 6f 66 20 74 68 65 20 46 52 4f 4d 20  nts of the FROM 
1f794 63 6c 61 75 73 65 2e 20 20 42 75 74 20 77 65 20  clause.  But we 
1f795 64 6f 20 6e 6f 74 20 77 61 6e 74 20 74 68 65 73  do not want thes
1f796 65 20 63 68 61 6e 67 65 73 0a 20 20 2a 2a 20 74  e changes.  ** t
1f797 6f 20 62 65 20 70 65 72 6d 61 6e 65 6e 74 2e 20  o be permanent. 
1f798 20 53 6f 20 74 68 65 20 63 6f 6d 70 75 74 61 74   So the computat
1f799 69 6f 6e 20 69 73 20 64 6f 6e 65 20 6f 6e 20 61  ion is done on a
1f79a 20 63 6f 70 79 20 6f 66 20 74 68 65 20 53 45 4c   copy of the SEL
1f79b 45 43 54 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65  ECT.  ** stateme
1f79c 6e 74 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  nt that defines 
1f79d 74 68 65 20 76 69 65 77 2e 0a 20 20 2a 2f 0a 20  the view..  */. 
1f79e 20 61 73 73 65 72 74 28 20 70 54 61 62 6c 65 2d   assert( pTable-
1f79f 3e 70 53 65 6c 65 63 74 20 29 3b 0a 20 20 70 53  >pSelect );.  pS
1f7a0 65 6c 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  el = sqlite3Sele
1f7a1 63 74 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65  ctDup(db, pTable
1f7a2 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66  ->pSelect);.  if
1f7a3 28 20 70 53 65 6c 20 29 7b 0a 20 20 20 20 6e 20  ( pSel ){.    n 
1f7a4 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a  = pParse->nTab;.
1f7a5 20 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69      sqlite3SrcLi
1f7a6 73 74 41 73 73 69 67 6e 43 75 72 73 6f 72 73 28  stAssignCursors(
1f7a7 70 50 61 72 73 65 2c 20 70 53 65 6c 2d 3e 70 53  pParse, pSel->pS
1f7a8 72 63 29 3b 0a 20 20 20 20 70 54 61 62 6c 65 2d  rc);.    pTable-
1f7a9 3e 6e 43 6f 6c 20 3d 20 2d 31 3b 0a 23 69 66 6e  >nCol = -1;.#ifn
1f7aa 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f7ab 41 55 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20  AUTHORIZATION.  
1f7ac 20 20 78 41 75 74 68 20 3d 20 64 62 2d 3e 78 41    xAuth = db->xA
1f7ad 75 74 68 3b 0a 20 20 20 20 64 62 2d 3e 78 41 75  uth;.    db->xAu
1f7ae 74 68 20 3d 20 30 3b 0a 20 20 20 20 70 53 65 6c  th = 0;.    pSel
1f7af 54 61 62 20 3d 20 73 71 6c 69 74 65 33 52 65 73  Tab = sqlite3Res
1f7b0 75 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 70  ultSetOfSelect(p
1f7b1 50 61 72 73 65 2c 20 70 53 65 6c 29 3b 0a 20 20  Parse, pSel);.  
1f7b2 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41    db->xAuth = xA
1f7b3 75 74 68 3b 0a 23 65 6c 73 65 0a 20 20 20 20 70  uth;.#else.    p
1f7b4 53 65 6c 54 61 62 20 3d 20 73 71 6c 69 74 65 33  SelTab = sqlite3
1f7b5 52 65 73 75 6c 74 53 65 74 4f 66 53 65 6c 65 63  ResultSetOfSelec
1f7b6 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 29 3b  t(pParse, pSel);
1f7b7 0a 23 65 6e 64 69 66 0a 20 20 20 20 70 50 61 72  .#endif.    pPar
1f7b8 73 65 2d 3e 6e 54 61 62 20 3d 20 6e 3b 0a 20 20  se->nTab = n;.  
1f7b9 20 20 69 66 28 20 70 53 65 6c 54 61 62 20 29 7b    if( pSelTab ){
1f7ba 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
1f7bb 54 61 62 6c 65 2d 3e 61 43 6f 6c 3d 3d 30 20 29  Table->aCol==0 )
1f7bc 3b 0a 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e  ;.      pTable->
1f7bd 6e 43 6f 6c 20 3d 20 70 53 65 6c 54 61 62 2d 3e  nCol = pSelTab->
1f7be 6e 43 6f 6c 3b 0a 20 20 20 20 20 20 70 54 61 62  nCol;.      pTab
1f7bf 6c 65 2d 3e 61 43 6f 6c 20 3d 20 70 53 65 6c 54  le->aCol = pSelT
1f7c0 61 62 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20 20 20  ab->aCol;.      
1f7c1 70 53 65 6c 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20  pSelTab->nCol = 
1f7c2 30 3b 0a 20 20 20 20 20 20 70 53 65 6c 54 61 62  0;.      pSelTab
1f7c3 2d 3e 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  ->aCol = 0;.    
1f7c4 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
1f7c5 61 62 6c 65 28 70 53 65 6c 54 61 62 29 3b 0a 20  able(pSelTab);. 
1f7c6 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 53 63       pTable->pSc
1f7c7 68 65 6d 61 2d 3e 66 6c 61 67 73 20 7c 3d 20 44  hema->flags |= D
1f7c8 42 5f 55 6e 72 65 73 65 74 56 69 65 77 73 3b 0a  B_UnresetViews;.
1f7c9 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f7ca 20 70 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20 3d 20   pTable->nCol = 
1f7cb 30 3b 0a 20 20 20 20 20 20 6e 45 72 72 2b 2b 3b  0;.      nErr++;
1f7cc 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
1f7cd 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64  e3SelectDelete(d
1f7ce 62 2c 20 70 53 65 6c 29 3b 0a 20 20 7d 20 65 6c  b, pSel);.  } el
1f7cf 73 65 20 7b 0a 20 20 20 20 6e 45 72 72 2b 2b 3b  se {.    nErr++;
1f7d0 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  .  }.#endif /* S
1f7d1 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 20  QLITE_OMIT_VIEW 
1f7d2 2a 2f 0a 20 20 72 65 74 75 72 6e 20 6e 45 72 72  */.  return nErr
1f7d3 3b 20 20 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20  ;  .}.#endif /* 
1f7d4 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1f7d5 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
1f7d6 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
1f7d7 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 29  IT_VIRTUALTABLE)
1f7d8 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
1f7d9 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 2f 2a  ITE_OMIT_VIEW./*
1f7da 0a 2a 2a 20 43 6c 65 61 72 20 74 68 65 20 63 6f  .** Clear the co
1f7db 6c 75 6d 6e 20 6e 61 6d 65 73 20 66 72 6f 6d 20  lumn names from 
1f7dc 65 76 65 72 79 20 56 49 45 57 20 69 6e 20 64 61  every VIEW in da
1f7dd 74 61 62 61 73 65 20 69 64 78 2e 0a 2a 2f 0a 73  tabase idx..*/.s
1f7de 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74  tatic void sqlit
1f7df 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28 73 71  eViewResetAll(sq
1f7e0 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 69  lite3 *db, int i
1f7e1 64 78 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d 20  dx){.  HashElem 
1f7e2 2a 69 3b 0a 20 20 69 66 28 20 21 44 62 48 61 73  *i;.  if( !DbHas
1f7e3 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69 64 78  Property(db, idx
1f7e4 2c 20 44 42 5f 55 6e 72 65 73 65 74 56 69 65 77  , DB_UnresetView
1f7e5 73 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66  s) ) return;.  f
1f7e6 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68 46  or(i=sqliteHashF
1f7e7 69 72 73 74 28 26 64 62 2d 3e 61 44 62 5b 69 64  irst(&db->aDb[id
1f7e8 78 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 62 6c 48  x].pSchema->tblH
1f7e9 61 73 68 29 3b 20 69 3b 69 3d 73 71 6c 69 74 65  ash); i;i=sqlite
1f7ea 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
1f7eb 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1f7ec 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69  sqliteHashData(i
1f7ed 29 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62 2d  );.    if( pTab-
1f7ee 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20  >pSelect ){.    
1f7ef 20 20 73 71 6c 69 74 65 52 65 73 65 74 43 6f 6c    sqliteResetCol
1f7f0 75 6d 6e 4e 61 6d 65 73 28 70 54 61 62 29 3b 0a  umnNames(pTab);.
1f7f1 20 20 20 20 7d 0a 20 20 7d 0a 20 20 44 62 43 6c      }.  }.  DbCl
1f7f2 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c 20  earProperty(db, 
1f7f3 69 64 78 2c 20 44 42 5f 55 6e 72 65 73 65 74 56  idx, DB_UnresetV
1f7f4 69 65 77 73 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23  iews);.}.#else.#
1f7f5 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 56 69   define sqliteVi
1f7f6 65 77 52 65 73 65 74 41 6c 6c 28 41 2c 42 29 0a  ewResetAll(A,B).
1f7f7 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
1f7f8 5f 4f 4d 49 54 5f 56 49 45 57 20 2a 2f 0a 0a 2f  _OMIT_VIEW */../
1f7f9 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
1f7fa 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  on is called by 
1f7fb 74 68 65 20 56 44 42 45 20 74 6f 20 61 64 6a 75  the VDBE to adju
1f7fc 73 74 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20  st the internal 
1f7fd 73 63 68 65 6d 61 0a 2a 2a 20 75 73 65 64 20 62  schema.** used b
1f7fe 79 20 53 51 4c 69 74 65 20 77 68 65 6e 20 74 68  y SQLite when th
1f7ff 65 20 62 74 72 65 65 20 6c 61 79 65 72 20 6d 6f  e btree layer mo
1f800 76 65 73 20 61 20 74 61 62 6c 65 20 72 6f 6f 74  ves a table root
1f801 20 70 61 67 65 2e 20 54 68 65 0a 2a 2a 20 72 6f   page. The.** ro
1f802 6f 74 2d 70 61 67 65 20 6f 66 20 61 20 74 61 62  ot-page of a tab
1f803 6c 65 20 6f 72 20 69 6e 64 65 78 20 69 6e 20 64  le or index in d
1f804 61 74 61 62 61 73 65 20 69 44 62 20 68 61 73 20  atabase iDb has 
1f805 63 68 61 6e 67 65 64 20 66 72 6f 6d 20 69 46 72  changed from iFr
1f806 6f 6d 0a 2a 2a 20 74 6f 20 69 54 6f 2e 0a 2a 2a  om.** to iTo..**
1f807 0a 2a 2a 20 54 69 63 6b 65 74 20 23 31 37 32 38  .** Ticket #1728
1f808 3a 20 20 54 68 65 20 73 79 6d 62 6f 6c 20 74 61  :  The symbol ta
1f809 62 6c 65 20 6d 69 67 68 74 20 73 74 69 6c 6c 20  ble might still 
1f80a 63 6f 6e 74 61 69 6e 20 69 6e 66 6f 72 6d 61 74  contain informat
1f80b 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 61 62 6c 65 73  ion.** on tables
1f80c 20 61 6e 64 2f 6f 72 20 69 6e 64 69 63 65 73 20   and/or indices 
1f80d 74 68 61 74 20 61 72 65 20 74 68 65 20 70 72 6f  that are the pro
1f80e 63 65 73 73 20 6f 66 20 62 65 69 6e 67 20 64 65  cess of being de
1f80f 6c 65 74 65 64 2e 0a 2a 2a 20 49 66 20 79 6f 75  leted..** If you
1f810 20 61 72 65 20 75 6e 6c 75 63 6b 79 2c 20 6f 6e   are unlucky, on
1f811 65 20 6f 66 20 74 68 6f 73 65 20 64 65 6c 65 74  e of those delet
1f812 65 64 20 69 6e 64 69 63 65 73 20 6f 72 20 74 61  ed indices or ta
1f813 62 6c 65 73 20 6d 69 67 68 74 0a 2a 2a 20 68 61  bles might.** ha
1f814 76 65 20 74 68 65 20 73 61 6d 65 20 72 6f 6f 74  ve the same root
1f815 70 61 67 65 20 6e 75 6d 62 65 72 20 61 73 20 74  page number as t
1f816 68 65 20 72 65 61 6c 20 74 61 62 6c 65 20 6f 72  he real table or
1f817 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 0a 2a   index that is.*
1f818 2a 20 62 65 69 6e 67 20 6d 6f 76 65 64 2e 20 20  * being moved.  
1f819 53 6f 20 77 65 20 63 61 6e 6e 6f 74 20 73 74 6f  So we cannot sto
1f81a 70 20 73 65 61 72 63 68 69 6e 67 20 61 66 74 65  p searching afte
1f81b 72 20 74 68 65 20 66 69 72 73 74 20 6d 61 74 63  r the first matc
1f81c 68 20 0a 2a 2a 20 62 65 63 61 75 73 65 20 74 68  h .** because th
1f81d 65 20 66 69 72 73 74 20 6d 61 74 63 68 20 6d 69  e first match mi
1f81e 67 68 74 20 62 65 20 66 6f 72 20 6f 6e 65 20 6f  ght be for one o
1f81f 66 20 74 68 65 20 64 65 6c 65 74 65 64 20 69 6e  f the deleted in
1f820 64 69 63 65 73 0a 2a 2a 20 6f 72 20 74 61 62 6c  dices.** or tabl
1f821 65 73 20 61 6e 64 20 6e 6f 74 20 74 68 65 20 74  es and not the t
1f822 61 62 6c 65 2f 69 6e 64 65 78 20 74 68 61 74 20  able/index that 
1f823 69 73 20 61 63 74 75 61 6c 6c 79 20 62 65 69 6e  is actually bein
1f824 67 20 6d 6f 76 65 64 2e 0a 2a 2a 20 57 65 20 6d  g moved..** We m
1f825 75 73 74 20 63 6f 6e 74 69 6e 75 65 20 6c 6f 6f  ust continue loo
1f826 70 69 6e 67 20 75 6e 74 69 6c 20 61 6c 6c 20 74  ping until all t
1f827 61 62 6c 65 73 20 61 6e 64 20 69 6e 64 69 63 65  ables and indice
1f828 73 20 77 69 74 68 0a 2a 2a 20 72 6f 6f 74 70 61  s with.** rootpa
1f829 67 65 3d 3d 69 46 72 6f 6d 20 68 61 76 65 20 62  ge==iFrom have b
1f82a 65 65 6e 20 63 6f 6e 76 65 72 74 65 64 20 74 6f  een converted to
1f82b 20 68 61 76 65 20 61 20 72 6f 6f 74 70 61 67 65   have a rootpage
1f82c 20 6f 66 20 69 54 6f 0a 2a 2a 20 69 6e 20 6f 72   of iTo.** in or
1f82d 64 65 72 20 74 6f 20 62 65 20 63 65 72 74 61 69  der to be certai
1f82e 6e 20 74 68 61 74 20 77 65 20 67 6f 74 20 74 68  n that we got th
1f82f 65 20 72 69 67 68 74 20 6f 6e 65 2e 0a 2a 2f 0a  e right one..*/.
1f830 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1f831 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 53  MIT_AUTOVACUUM.S
1f832 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f833 69 64 20 73 71 6c 69 74 65 33 52 6f 6f 74 50 61  id sqlite3RootPa
1f834 67 65 4d 6f 76 65 64 28 44 62 20 2a 70 44 62 2c  geMoved(Db *pDb,
1f835 20 69 6e 74 20 69 46 72 6f 6d 2c 20 69 6e 74 20   int iFrom, int 
1f836 69 54 6f 29 7b 0a 20 20 48 61 73 68 45 6c 65 6d  iTo){.  HashElem
1f837 20 2a 70 45 6c 65 6d 3b 0a 20 20 48 61 73 68 20   *pElem;.  Hash 
1f838 2a 70 48 61 73 68 3b 0a 0a 20 20 70 48 61 73 68  *pHash;..  pHash
1f839 20 3d 20 26 70 44 62 2d 3e 70 53 63 68 65 6d 61   = &pDb->pSchema
1f83a 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 66 6f 72  ->tblHash;.  for
1f83b 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73  (pElem=sqliteHas
1f83c 68 46 69 72 73 74 28 70 48 61 73 68 29 3b 20 70  hFirst(pHash); p
1f83d 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73 71 6c 69  Elem; pElem=sqli
1f83e 74 65 48 61 73 68 4e 65 78 74 28 70 45 6c 65 6d  teHashNext(pElem
1f83f 29 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  )){.    Table *p
1f840 54 61 62 20 3d 20 73 71 6c 69 74 65 48 61 73 68  Tab = sqliteHash
1f841 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a 20 20 20  Data(pElem);.   
1f842 20 69 66 28 20 70 54 61 62 2d 3e 74 6e 75 6d 3d   if( pTab->tnum=
1f843 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20 20 20 20  =iFrom ){.      
1f844 70 54 61 62 2d 3e 74 6e 75 6d 20 3d 20 69 54 6f  pTab->tnum = iTo
1f845 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 48  ;.    }.  }.  pH
1f846 61 73 68 20 3d 20 26 70 44 62 2d 3e 70 53 63 68  ash = &pDb->pSch
1f847 65 6d 61 2d 3e 69 64 78 48 61 73 68 3b 0a 20 20  ema->idxHash;.  
1f848 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c 69 74 65  for(pElem=sqlite
1f849 48 61 73 68 46 69 72 73 74 28 70 48 61 73 68 29  HashFirst(pHash)
1f84a 3b 20 70 45 6c 65 6d 3b 20 70 45 6c 65 6d 3d 73  ; pElem; pElem=s
1f84b 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 70 45  qliteHashNext(pE
1f84c 6c 65 6d 29 29 7b 0a 20 20 20 20 49 6e 64 65 78  lem)){.    Index
1f84d 20 2a 70 49 64 78 20 3d 20 73 71 6c 69 74 65 48   *pIdx = sqliteH
1f84e 61 73 68 44 61 74 61 28 70 45 6c 65 6d 29 3b 0a  ashData(pElem);.
1f84f 20 20 20 20 69 66 28 20 70 49 64 78 2d 3e 74 6e      if( pIdx->tn
1f850 75 6d 3d 3d 69 46 72 6f 6d 20 29 7b 0a 20 20 20  um==iFrom ){.   
1f851 20 20 20 70 49 64 78 2d 3e 74 6e 75 6d 20 3d 20     pIdx->tnum = 
1f852 69 54 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d  iTo;.    }.  }.}
1f853 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 57  .#endif../*.** W
1f854 72 69 74 65 20 63 6f 64 65 20 74 6f 20 65 72 61  rite code to era
1f855 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69 74  se the table wit
1f856 68 20 72 6f 6f 74 2d 70 61 67 65 20 69 54 61 62  h root-page iTab
1f857 6c 65 20 66 72 6f 6d 20 64 61 74 61 62 61 73 65  le from database
1f858 20 69 44 62 2e 0a 2a 2a 20 41 6c 73 6f 20 77 72   iDb..** Also wr
1f859 69 74 65 20 63 6f 64 65 20 74 6f 20 6d 6f 64 69  ite code to modi
1f85a 66 79 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61  fy the sqlite_ma
1f85b 73 74 65 72 20 74 61 62 6c 65 20 61 6e 64 20 69  ster table and i
1f85c 6e 74 65 72 6e 61 6c 20 73 63 68 65 6d 61 0a 2a  nternal schema.*
1f85d 2a 20 69 66 20 61 20 72 6f 6f 74 2d 70 61 67 65  * if a root-page
1f85e 20 6f 66 20 61 6e 6f 74 68 65 72 20 74 61 62 6c   of another tabl
1f85f 65 20 69 73 20 6d 6f 76 65 64 20 62 79 20 74 68  e is moved by th
1f860 65 20 62 74 72 65 65 2d 6c 61 79 65 72 20 77 68  e btree-layer wh
1f861 69 6c 73 74 0a 2a 2a 20 65 72 61 73 69 6e 67 20  ilst.** erasing 
1f862 69 54 61 62 6c 65 20 28 74 68 69 73 20 63 61 6e  iTable (this can
1f863 20 68 61 70 70 65 6e 20 77 69 74 68 20 61 6e 20   happen with an 
1f864 61 75 74 6f 2d 76 61 63 75 75 6d 20 64 61 74 61  auto-vacuum data
1f865 62 61 73 65 29 2e 0a 2a 2f 20 0a 73 74 61 74 69  base)..*/ .stati
1f866 63 20 76 6f 69 64 20 64 65 73 74 72 6f 79 52 6f  c void destroyRo
1f867 6f 74 50 61 67 65 28 50 61 72 73 65 20 2a 70 50  otPage(Parse *pP
1f868 61 72 73 65 2c 20 69 6e 74 20 69 54 61 62 6c 65  arse, int iTable
1f869 2c 20 69 6e 74 20 69 44 62 29 7b 0a 20 20 56 64  , int iDb){.  Vd
1f86a 62 65 20 2a 76 20 3d 20 73 71 6c 69 74 65 33 47  be *v = sqlite3G
1f86b 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1f86c 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74    int r1 = sqlit
1f86d 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
1f86e 72 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  rse);.  sqlite3V
1f86f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
1f870 44 65 73 74 72 6f 79 2c 20 69 54 61 62 6c 65 2c  Destroy, iTable,
1f871 20 72 31 2c 20 69 44 62 29 3b 0a 23 69 66 6e 64   r1, iDb);.#ifnd
1f872 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
1f873 55 54 4f 56 41 43 55 55 4d 0a 20 20 2f 2a 20 4f  UTOVACUUM.  /* O
1f874 50 5f 44 65 73 74 72 6f 79 20 73 74 6f 72 65 73  P_Destroy stores
1f875 20 61 6e 20 69 6e 20 69 6e 74 65 67 65 72 20 72   an in integer r
1f876 31 2e 20 49 66 20 74 68 69 73 20 69 6e 74 65 67  1. If this integ
1f877 65 72 0a 20 20 2a 2a 20 69 73 20 6e 6f 6e 2d 7a  er.  ** is non-z
1f878 65 72 6f 2c 20 74 68 65 6e 20 69 74 20 69 73 20  ero, then it is 
1f879 74 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75  the root page nu
1f87a 6d 62 65 72 20 6f 66 20 61 20 74 61 62 6c 65 20  mber of a table 
1f87b 6d 6f 76 65 64 20 74 6f 0a 20 20 2a 2a 20 6c 6f  moved to.  ** lo
1f87c 63 61 74 69 6f 6e 20 69 54 61 62 6c 65 2e 20 54  cation iTable. T
1f87d 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
1f87e 65 20 6d 6f 64 69 66 69 65 73 20 74 68 65 20 73  e modifies the s
1f87f 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61 62  qlite_master tab
1f880 6c 65 20 74 6f 0a 20 20 2a 2a 20 72 65 66 6c 65  le to.  ** refle
1f881 63 74 20 74 68 69 73 2e 0a 20 20 2a 2a 0a 20 20  ct this..  **.  
1f882 2a 2a 20 54 68 65 20 22 23 25 64 22 20 69 6e 20  ** The "#%d" in 
1f883 74 68 65 20 53 51 4c 20 69 73 20 61 20 73 70 65  the SQL is a spe
1f884 63 69 61 6c 20 63 6f 6e 73 74 61 6e 74 20 74 68  cial constant th
1f885 61 74 20 6d 65 61 6e 73 20 77 68 61 74 65 76 65  at means whateve
1f886 72 20 76 61 6c 75 65 0a 20 20 2a 2a 20 69 73 20  r value.  ** is 
1f887 6f 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68  on the top of th
1f888 65 20 73 74 61 63 6b 2e 20 20 53 65 65 20 73 71  e stack.  See sq
1f889 6c 69 74 65 33 52 65 67 69 73 74 65 72 45 78 70  lite3RegisterExp
1f88a 72 28 29 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  r()..  */.  sqli
1f88b 74 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 70  te3NestedParse(p
1f88c 50 61 72 73 65 2c 20 0a 20 20 20 20 20 22 55 50  Parse, .     "UP
1f88d 44 41 54 45 20 25 51 2e 25 73 20 53 45 54 20 72  DATE %Q.%s SET r
1f88e 6f 6f 74 70 61 67 65 3d 25 64 20 57 48 45 52 45  ootpage=%d WHERE
1f88f 20 23 25 64 20 41 4e 44 20 72 6f 6f 74 70 61 67   #%d AND rootpag
1f890 65 3d 23 25 64 22 2c 0a 20 20 20 20 20 70 50 61  e=#%d",.     pPa
1f891 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62  rse->db->aDb[iDb
1f892 5d 2e 7a 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f  ].zName, SCHEMA_
1f893 54 41 42 4c 45 28 69 44 62 29 2c 20 69 54 61 62  TABLE(iDb), iTab
1f894 6c 65 2c 20 72 31 2c 20 72 31 29 3b 0a 23 65 6e  le, r1, r1);.#en
1f895 64 69 66 0a 20 20 73 71 6c 69 74 65 33 52 65 6c  dif.  sqlite3Rel
1f896 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
1f897 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  se, r1);.}../*.*
1f898 2a 20 57 72 69 74 65 20 56 44 42 45 20 63 6f 64  * Write VDBE cod
1f899 65 20 74 6f 20 65 72 61 73 65 20 74 61 62 6c 65  e to erase table
1f89a 20 70 54 61 62 20 61 6e 64 20 61 6c 6c 20 61 73   pTab and all as
1f89b 73 6f 63 69 61 74 65 64 20 69 6e 64 69 63 65 73  sociated indices
1f89c 20 6f 6e 20 64 69 73 6b 2e 0a 2a 2a 20 43 6f 64   on disk..** Cod
1f89d 65 20 74 6f 20 75 70 64 61 74 65 20 74 68 65 20  e to update the 
1f89e 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 74 61  sqlite_master ta
1f89f 62 6c 65 73 20 61 6e 64 20 69 6e 74 65 72 6e 61  bles and interna
1f8a0 6c 20 73 63 68 65 6d 61 20 64 65 66 69 6e 69 74  l schema definit
1f8a1 69 6f 6e 73 0a 2a 2a 20 69 6e 20 63 61 73 65 20  ions.** in case 
1f8a2 61 20 72 6f 6f 74 2d 70 61 67 65 20 62 65 6c 6f  a root-page belo
1f8a3 6e 67 69 6e 67 20 74 6f 20 61 6e 6f 74 68 65 72  nging to another
1f8a4 20 74 61 62 6c 65 20 69 73 20 6d 6f 76 65 64 20   table is moved 
1f8a5 62 79 20 74 68 65 20 62 74 72 65 65 20 6c 61 79  by the btree lay
1f8a6 65 72 0a 2a 2a 20 69 73 20 61 6c 73 6f 20 61 64  er.** is also ad
1f8a7 64 65 64 20 28 74 68 69 73 20 63 61 6e 20 68 61  ded (this can ha
1f8a8 70 70 65 6e 20 77 69 74 68 20 61 6e 20 61 75 74  ppen with an aut
1f8a9 6f 2d 76 61 63 75 75 6d 20 64 61 74 61 62 61 73  o-vacuum databas
1f8aa 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e)..*/.static vo
1f8ab 69 64 20 64 65 73 74 72 6f 79 54 61 62 6c 65 28  id destroyTable(
1f8ac 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54  Parse *pParse, T
1f8ad 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a 23 69 66  able *pTab){.#if
1f8ae 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
1f8af 41 55 54 4f 56 41 43 55 55 4d 0a 20 20 49 6e 64  AUTOVACUUM.  Ind
1f8b0 65 78 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20  ex *pIdx;.  int 
1f8b1 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
1f8b2 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
1f8b3 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63  e->db, pTab->pSc
1f8b4 68 65 6d 61 29 3b 0a 20 20 64 65 73 74 72 6f 79  hema);.  destroy
1f8b5 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c  RootPage(pParse,
1f8b6 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69 44 62   pTab->tnum, iDb
1f8b7 29 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d 70 54  );.  for(pIdx=pT
1f8b8 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78  ab->pIndex; pIdx
1f8b9 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65  ; pIdx=pIdx->pNe
1f8ba 78 74 29 7b 0a 20 20 20 20 64 65 73 74 72 6f 79  xt){.    destroy
1f8bb 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65 2c  RootPage(pParse,
1f8bc 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62   pIdx->tnum, iDb
1f8bd 29 3b 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 2f  );.  }.#else.  /
1f8be 2a 20 49 66 20 74 68 65 20 64 61 74 61 62 61 73  * If the databas
1f8bf 65 20 6d 61 79 20 62 65 20 61 75 74 6f 2d 76 61  e may be auto-va
1f8c0 63 75 75 6d 20 63 61 70 61 62 6c 65 20 28 69 66  cuum capable (if
1f8c1 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54   SQLITE_OMIT_AUT
1f8c2 4f 56 41 43 55 55 4d 0a 20 20 2a 2a 20 69 73 20  OVACUUM.  ** is 
1f8c3 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 74 68  not defined), th
1f8c4 65 6e 20 69 74 20 69 73 20 69 6d 70 6f 72 74 61  en it is importa
1f8c5 6e 74 20 74 6f 20 63 61 6c 6c 20 4f 50 5f 44 65  nt to call OP_De
1f8c6 73 74 72 6f 79 20 6f 6e 20 74 68 65 0a 20 20 2a  stroy on the.  *
1f8c7 2a 20 74 61 62 6c 65 20 61 6e 64 20 69 6e 64 65  * table and inde
1f8c8 78 20 72 6f 6f 74 2d 70 61 67 65 73 20 69 6e 20  x root-pages in 
1f8c9 6f 72 64 65 72 2c 20 73 74 61 72 74 69 6e 67 20  order, starting 
1f8ca 77 69 74 68 20 74 68 65 20 6e 75 6d 65 72 69 63  with the numeric
1f8cb 61 6c 6c 79 20 0a 20 20 2a 2a 20 6c 61 72 67 65  ally .  ** large
1f8cc 73 74 20 72 6f 6f 74 2d 70 61 67 65 20 6e 75 6d  st root-page num
1f8cd 62 65 72 2e 20 54 68 69 73 20 67 75 61 72 61 6e  ber. This guaran
1f8ce 74 65 65 73 20 74 68 61 74 20 6e 6f 6e 65 20 6f  tees that none o
1f8cf 66 20 74 68 65 20 72 6f 6f 74 2d 70 61 67 65 73  f the root-pages
1f8d0 0a 20 20 2a 2a 20 74 6f 20 62 65 20 64 65 73 74  .  ** to be dest
1f8d1 72 6f 79 65 64 20 69 73 20 72 65 6c 6f 63 61 74  royed is relocat
1f8d2 65 64 20 62 79 20 61 6e 20 65 61 72 6c 69 65 72  ed by an earlier
1f8d3 20 4f 50 5f 44 65 73 74 72 6f 79 2e 20 69 2e 65   OP_Destroy. i.e
1f8d4 2e 20 69 66 20 74 68 65 0a 20 20 2a 2a 20 66 6f  . if the.  ** fo
1f8d5 6c 6c 6f 77 69 6e 67 20 77 65 72 65 20 63 6f 64  llowing were cod
1f8d6 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4f 50  ed:.  **.  ** OP
1f8d7 5f 44 65 73 74 72 6f 79 20 34 20 30 0a 20 20 2a  _Destroy 4 0.  *
1f8d8 2a 20 2e 2e 2e 0a 20 20 2a 2a 20 4f 50 5f 44 65  * ....  ** OP_De
1f8d9 73 74 72 6f 79 20 35 20 30 0a 20 20 2a 2a 0a 20  stroy 5 0.  **. 
1f8da 20 2a 2a 20 61 6e 64 20 72 6f 6f 74 20 70 61 67   ** and root pag
1f8db 65 20 35 20 68 61 70 70 65 6e 65 64 20 74 6f 20  e 5 happened to 
1f8dc 62 65 20 74 68 65 20 6c 61 72 67 65 73 74 20 72  be the largest r
1f8dd 6f 6f 74 2d 70 61 67 65 20 6e 75 6d 62 65 72 20  oot-page number 
1f8de 69 6e 20 74 68 65 0a 20 20 2a 2a 20 64 61 74 61  in the.  ** data
1f8df 62 61 73 65 2c 20 74 68 65 6e 20 72 6f 6f 74 20  base, then root 
1f8e0 70 61 67 65 20 35 20 77 6f 75 6c 64 20 62 65 20  page 5 would be 
1f8e1 6d 6f 76 65 64 20 74 6f 20 70 61 67 65 20 34 20  moved to page 4 
1f8e2 62 79 20 74 68 65 20 0a 20 20 2a 2a 20 22 4f 50  by the .  ** "OP
1f8e3 5f 44 65 73 74 72 6f 79 20 34 20 30 22 20 6f 70  _Destroy 4 0" op
1f8e4 63 6f 64 65 2e 20 54 68 65 20 73 75 62 73 65 71  code. The subseq
1f8e5 75 65 6e 74 20 22 4f 50 5f 44 65 73 74 72 6f 79  uent "OP_Destroy
1f8e6 20 35 20 30 22 20 77 6f 75 6c 64 20 68 69 74 0a   5 0" would hit.
1f8e7 20 20 2a 2a 20 61 20 66 72 65 65 2d 6c 69 73 74    ** a free-list
1f8e8 20 70 61 67 65 2e 0a 20 20 2a 2f 0a 20 20 69 6e   page..  */.  in
1f8e9 74 20 69 54 61 62 20 3d 20 70 54 61 62 2d 3e 74  t iTab = pTab->t
1f8ea 6e 75 6d 3b 0a 20 20 69 6e 74 20 69 44 65 73 74  num;.  int iDest
1f8eb 72 6f 79 65 64 20 3d 20 30 3b 0a 0a 20 20 77 68  royed = 0;..  wh
1f8ec 69 6c 65 28 20 31 20 29 7b 0a 20 20 20 20 49 6e  ile( 1 ){.    In
1f8ed 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 69  dex *pIdx;.    i
1f8ee 6e 74 20 69 4c 61 72 67 65 73 74 20 3d 20 30 3b  nt iLargest = 0;
1f8ef 0a 0a 20 20 20 20 69 66 28 20 69 44 65 73 74 72  ..    if( iDestr
1f8f0 6f 79 65 64 3d 3d 30 20 7c 7c 20 69 54 61 62 3c  oyed==0 || iTab<
1f8f1 69 44 65 73 74 72 6f 79 65 64 20 29 7b 0a 20 20  iDestroyed ){.  
1f8f2 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20 69      iLargest = i
1f8f3 54 61 62 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66  Tab;.    }.    f
1f8f4 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  or(pIdx=pTab->pI
1f8f5 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
1f8f6 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a 20  =pIdx->pNext){. 
1f8f7 20 20 20 20 20 69 6e 74 20 69 49 64 78 20 3d 20       int iIdx = 
1f8f8 70 49 64 78 2d 3e 74 6e 75 6d 3b 0a 20 20 20 20  pIdx->tnum;.    
1f8f9 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
1f8fa 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
1f8fb 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 20 20  Schema );.      
1f8fc 69 66 28 20 28 69 44 65 73 74 72 6f 79 65 64 3d  if( (iDestroyed=
1f8fd 3d 30 20 7c 7c 20 28 69 49 64 78 3c 69 44 65 73  =0 || (iIdx<iDes
1f8fe 74 72 6f 79 65 64 29 29 20 26 26 20 69 49 64 78  troyed)) && iIdx
1f8ff 3e 69 4c 61 72 67 65 73 74 20 29 7b 0a 20 20 20  >iLargest ){.   
1f900 20 20 20 20 20 69 4c 61 72 67 65 73 74 20 3d 20       iLargest = 
1f901 69 49 64 78 3b 0a 20 20 20 20 20 20 7d 0a 20 20  iIdx;.      }.  
1f902 20 20 7d 0a 20 20 20 20 69 66 28 20 69 4c 61 72    }.    if( iLar
1f903 67 65 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20  gest==0 ){.     
1f904 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 65 6c   return;.    }el
1f905 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 44  se{.      int iD
1f906 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
1f907 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d  aToIndex(pParse-
1f908 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
1f909 6d 61 29 3b 0a 20 20 20 20 20 20 64 65 73 74 72  ma);.      destr
1f90a 6f 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73  oyRootPage(pPars
1f90b 65 2c 20 69 4c 61 72 67 65 73 74 2c 20 69 44 62  e, iLargest, iDb
1f90c 29 3b 0a 20 20 20 20 20 20 69 44 65 73 74 72 6f  );.      iDestro
1f90d 79 65 64 20 3d 20 69 4c 61 72 67 65 73 74 3b 0a  yed = iLargest;.
1f90e 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66      }.  }.#endif
1f90f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1f910 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1f911 20 74 6f 20 64 6f 20 74 68 65 20 77 6f 72 6b 20   to do the work 
1f912 6f 66 20 61 20 44 52 4f 50 20 54 41 42 4c 45 20  of a DROP TABLE 
1f913 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 70 4e  statement..** pN
1f914 61 6d 65 20 69 73 20 74 68 65 20 6e 61 6d 65 20  ame is the name 
1f915 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f 20  of the table to 
1f916 62 65 20 64 72 6f 70 70 65 64 2e 0a 2a 2f 0a 53  be dropped..*/.S
1f917 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
1f918 69 64 20 73 71 6c 69 74 65 33 44 72 6f 70 54 61  id sqlite3DropTa
1f919 62 6c 65 28 50 61 72 73 65 20 2a 70 50 61 72 73  ble(Parse *pPars
1f91a 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d  e, SrcList *pNam
1f91b 65 2c 20 69 6e 74 20 69 73 56 69 65 77 2c 20 69  e, int isView, i
1f91c 6e 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 61 62  nt noErr){.  Tab
1f91d 6c 65 20 2a 70 54 61 62 3b 0a 20 20 56 64 62 65  le *pTab;.  Vdbe
1f91e 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a   *v;.  sqlite3 *
1f91f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
1f920 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20 69  .  int iDb;..  i
1f921 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20  f( pParse->nErr 
1f922 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  || db->mallocFai
1f923 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
1f924 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b  exit_drop_table;
1f925 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
1f926 4e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b  Name->nSrc==1 );
1f927 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
1f928 33 4c 6f 63 61 74 65 54 61 62 6c 65 28 70 50 61  3LocateTable(pPa
1f929 72 73 65 2c 20 69 73 56 69 65 77 2c 20 0a 20 20  rse, isView, .  
1f92a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1f92b 20 20 20 20 20 20 20 20 20 20 70 4e 61 6d 65 2d            pName-
1f92c 3e 61 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 70 4e 61  >a[0].zName, pNa
1f92d 6d 65 2d 3e 61 5b 30 5d 2e 7a 44 61 74 61 62 61  me->a[0].zDataba
1f92e 73 65 29 3b 0a 0a 20 20 69 66 28 20 70 54 61 62  se);..  if( pTab
1f92f 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  ==0 ){.    if( n
1f930 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20 73 71  oErr ){.      sq
1f931 6c 69 74 65 33 45 72 72 6f 72 43 6c 65 61 72 28  lite3ErrorClear(
1f932 70 50 61 72 73 65 29 3b 0a 20 20 20 20 7d 0a 20  pParse);.    }. 
1f933 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f     goto exit_dro
1f934 70 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69  p_table;.  }.  i
1f935 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65  Db = sqlite3Sche
1f936 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54  maToIndex(db, pT
1f937 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20  ab->pSchema);.  
1f938 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20 26  assert( iDb>=0 &
1f939 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b  & iDb<db->nDb );
1f93a 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61 62 20 69  ..  /* If pTab i
1f93b 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
1f93c 65 2c 20 63 61 6c 6c 20 56 69 65 77 47 65 74 43  e, call ViewGetC
1f93d 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 74 6f 20  olumnNames() to 
1f93e 65 6e 73 75 72 65 0a 20 20 2a 2a 20 69 74 20 69  ensure.  ** it i
1f93f 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20  s initialized.. 
1f940 20 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74   */.  if( IsVirt
1f941 75 61 6c 28 70 54 61 62 29 20 26 26 20 73 71 6c  ual(pTab) && sql
1f942 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d  ite3ViewGetColum
1f943 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70  nNames(pParse, p
1f944 54 61 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  Tab) ){.    goto
1f945 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65   exit_drop_table
1f946 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51  ;.  }.#ifndef SQ
1f947 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f 52  LITE_OMIT_AUTHOR
1f948 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20 20  IZATION.  {.    
1f949 69 6e 74 20 63 6f 64 65 3b 0a 20 20 20 20 63 6f  int code;.    co
1f94a 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20 3d  nst char *zTab =
1f94b 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44   SCHEMA_TABLE(iD
1f94c 62 29 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68  b);.    const ch
1f94d 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44  ar *zDb = db->aD
1f94e 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  b[iDb].zName;.  
1f94f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
1f950 72 67 32 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  rg2 = 0;.    if(
1f951 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
1f952 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
1f953 5f 44 45 4c 45 54 45 2c 20 7a 54 61 62 2c 20 30  _DELETE, zTab, 0
1f954 2c 20 7a 44 62 29 29 7b 0a 20 20 20 20 20 20 67  , zDb)){.      g
1f955 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
1f956 62 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ble;.    }.    i
1f957 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20 20 20  f( isView ){.   
1f958 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
1f959 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
1f95a 0a 20 20 20 20 20 20 20 20 63 6f 64 65 20 3d 20  .        code = 
1f95b 53 51 4c 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50  SQLITE_DROP_TEMP
1f95c 5f 56 49 45 57 3b 0a 20 20 20 20 20 20 7d 65 6c  _VIEW;.      }el
1f95d 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 64 65  se{.        code
1f95e 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f 50 5f 56   = SQLITE_DROP_V
1f95f 49 45 57 3b 0a 20 20 20 20 20 20 7d 0a 23 69 66  IEW;.      }.#if
1f960 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
1f961 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
1f962 20 20 7d 65 6c 73 65 20 69 66 28 20 49 73 56 69    }else if( IsVi
1f963 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20  rtual(pTab) ){. 
1f964 20 20 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49       code = SQLI
1f965 54 45 5f 44 52 4f 50 5f 56 54 41 42 4c 45 3b 0a  TE_DROP_VTABLE;.
1f966 20 20 20 20 20 20 7a 41 72 67 32 20 3d 20 70 54        zArg2 = pT
1f967 61 62 2d 3e 70 4d 6f 64 2d 3e 7a 4e 61 6d 65 3b  ab->pMod->zName;
1f968 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c 73  .#endif.    }els
1f969 65 7b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d  e{.      if( !OM
1f96a 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62  IT_TEMPDB && iDb
1f96b 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 63  ==1 ){.        c
1f96c 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
1f96d 50 5f 54 45 4d 50 5f 54 41 42 4c 45 3b 0a 20 20  P_TEMP_TABLE;.  
1f96e 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
1f96f 20 20 20 63 6f 64 65 20 3d 20 53 51 4c 49 54 45     code = SQLITE
1f970 5f 44 52 4f 50 5f 54 41 42 4c 45 3b 0a 20 20 20  _DROP_TABLE;.   
1f971 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69     }.    }.    i
1f972 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
1f973 65 63 6b 28 70 50 61 72 73 65 2c 20 63 6f 64 65  eck(pParse, code
1f974 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a  , pTab->zName, z
1f975 41 72 67 32 2c 20 7a 44 62 29 20 29 7b 0a 20 20  Arg2, zDb) ){.  
1f976 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72      goto exit_dr
1f977 6f 70 5f 74 61 62 6c 65 3b 0a 20 20 20 20 7d 0a  op_table;.    }.
1f978 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
1f979 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
1f97a 20 53 51 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20   SQLITE_DELETE, 
1f97b 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20  pTab->zName, 0, 
1f97c 7a 44 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f  zDb) ){.      go
1f97d 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61 62  to exit_drop_tab
1f97e 6c 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  le;.    }.  }.#e
1f97f 6e 64 69 66 0a 20 20 69 66 28 20 73 71 6c 69 74  ndif.  if( sqlit
1f980 65 33 53 74 72 4e 49 43 6d 70 28 70 54 61 62 2d  e3StrNICmp(pTab-
1f981 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69 74 65 5f  >zName, "sqlite_
1f982 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 7)==0 ){.    
1f983 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
1f984 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
1f985 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 64 72 6f  s may not be dro
1f986 70 70 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e 61  pped", pTab->zNa
1f987 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78  me);.    goto ex
1f988 69 74 5f 64 72 6f 70 5f 74 61 62 6c 65 3b 0a 20  it_drop_table;. 
1f989 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   }..#ifndef SQLI
1f98a 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 20 20 2f  TE_OMIT_VIEW.  /
1f98b 2a 20 45 6e 73 75 72 65 20 44 52 4f 50 20 54 41  * Ensure DROP TA
1f98c 42 4c 45 20 69 73 20 6e 6f 74 20 75 73 65 64 20  BLE is not used 
1f98d 6f 6e 20 61 20 76 69 65 77 2c 20 61 6e 64 20 44  on a view, and D
1f98e 52 4f 50 20 56 49 45 57 20 69 73 20 6e 6f 74 20  ROP VIEW is not 
1f98f 75 73 65 64 0a 20 20 2a 2a 20 6f 6e 20 61 20 74  used.  ** on a t
1f990 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  able..  */.  if(
1f991 20 69 73 56 69 65 77 20 26 26 20 70 54 61 62 2d   isView && pTab-
1f992 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20  >pSelect==0 ){. 
1f993 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1f994 73 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20  sg(pParse, "use 
1f995 44 52 4f 50 20 54 41 42 4c 45 20 74 6f 20 64 65  DROP TABLE to de
1f996 6c 65 74 65 20 74 61 62 6c 65 20 25 73 22 2c 20  lete table %s", 
1f997 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
1f998 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
1f999 5f 74 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 69 66  _table;.  }.  if
1f99a 28 20 21 69 73 56 69 65 77 20 26 26 20 70 54 61  ( !isView && pTa
1f99b 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  b->pSelect ){.  
1f99c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1f99d 67 28 70 50 61 72 73 65 2c 20 22 75 73 65 20 44  g(pParse, "use D
1f99e 52 4f 50 20 56 49 45 57 20 74 6f 20 64 65 6c 65  ROP VIEW to dele
1f99f 74 65 20 76 69 65 77 20 25 73 22 2c 20 70 54 61  te view %s", pTa
1f9a0 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67  b->zName);.    g
1f9a1 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70 5f 74 61  oto exit_drop_ta
1f9a2 62 6c 65 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a  ble;.  }.#endif.
1f9a3 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63  .  /* Generate c
1f9a4 6f 64 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68  ode to remove th
1f9a5 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 74 68 65  e table from the
1f9a6 20 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20 20   master table.  
1f9a7 2a 2a 20 6f 6e 20 64 69 73 6b 2e 0a 20 20 2a 2f  ** on disk..  */
1f9a8 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65  .  v = sqlite3Ge
1f9a9 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20  tVdbe(pParse);. 
1f9aa 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 54 72   if( v ){.    Tr
1f9ab 69 67 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b  igger *pTrigger;
1f9ac 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20 26  .    Db *pDb = &
1f9ad 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20  db->aDb[iDb];.  
1f9ae 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72    sqlite3BeginWr
1f9af 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61  iteOperation(pPa
1f9b0 72 73 65 2c 20 31 2c 20 69 44 62 29 3b 0a 0a 23  rse, 1, iDb);..#
1f9b1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
1f9b2 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
1f9b3 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61      if( IsVirtua
1f9b4 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
1f9b5 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
1f9b6 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1f9b7 29 3b 0a 20 20 20 20 20 20 69 66 28 20 76 20 29  );.      if( v )
1f9b8 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
1f9b9 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f  3VdbeAddOp0(v, O
1f9ba 50 5f 56 42 65 67 69 6e 29 3b 0a 20 20 20 20 20  P_VBegin);.     
1f9bb 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a   }.    }.#endif.
1f9bc 0a 20 20 20 20 2f 2a 20 44 72 6f 70 20 61 6c 6c  .    /* Drop all
1f9bd 20 74 72 69 67 67 65 72 73 20 61 73 73 6f 63 69   triggers associ
1f9be 61 74 65 64 20 77 69 74 68 20 74 68 65 20 74 61  ated with the ta
1f9bf 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
1f9c0 64 2e 20 43 6f 64 65 0a 20 20 20 20 2a 2a 20 69  d. Code.    ** i
1f9c1 73 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 72  s generated to r
1f9c2 65 6d 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72  emove entries fr
1f9c3 6f 6d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72  om sqlite_master
1f9c4 20 61 6e 64 2f 6f 72 0a 20 20 20 20 2a 2a 20 73   and/or.    ** s
1f9c5 71 6c 69 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65  qlite_temp_maste
1f9c6 72 20 69 66 20 72 65 71 75 69 72 65 64 2e 0a 20  r if required.. 
1f9c7 20 20 20 2a 2f 0a 20 20 20 20 70 54 72 69 67 67     */.    pTrigg
1f9c8 65 72 20 3d 20 70 54 61 62 2d 3e 70 54 72 69 67  er = pTab->pTrig
1f9c9 67 65 72 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  ger;.    while( 
1f9ca 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
1f9cb 20 20 61 73 73 65 72 74 28 20 70 54 72 69 67 67    assert( pTrigg
1f9cc 65 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61  er->pSchema==pTa
1f9cd 62 2d 3e 70 53 63 68 65 6d 61 20 7c 7c 20 0a 20  b->pSchema || . 
1f9ce 20 20 20 20 20 20 20 20 20 70 54 72 69 67 67 65           pTrigge
1f9cf 72 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  r->pSchema==db->
1f9d0 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
1f9d1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  ;.      sqlite3D
1f9d2 72 6f 70 54 72 69 67 67 65 72 50 74 72 28 70 50  ropTriggerPtr(pP
1f9d3 61 72 73 65 2c 20 70 54 72 69 67 67 65 72 29 3b  arse, pTrigger);
1f9d4 0a 20 20 20 20 20 20 70 54 72 69 67 67 65 72 20  .      pTrigger 
1f9d5 3d 20 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78  = pTrigger->pNex
1f9d6 74 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  t;.    }..#ifnde
1f9d7 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
1f9d8 54 4f 49 4e 43 52 45 4d 45 4e 54 0a 20 20 20 20  TOINCREMENT.    
1f9d9 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 65 6e  /* Remove any en
1f9da 74 72 69 65 73 20 6f 66 20 74 68 65 20 73 71 6c  tries of the sql
1f9db 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
1f9dc 6c 65 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  le associated wi
1f9dd 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61  th.    ** the ta
1f9de 62 6c 65 20 62 65 69 6e 67 20 64 72 6f 70 70 65  ble being droppe
1f9df 64 2e 20 54 68 69 73 20 69 73 20 64 6f 6e 65 20  d. This is done 
1f9e0 62 65 66 6f 72 65 20 74 68 65 20 74 61 62 6c 65  before the table
1f9e1 20 69 73 20 64 72 6f 70 70 65 64 0a 20 20 20 20   is dropped.    
1f9e2 2a 2a 20 61 74 20 74 68 65 20 62 74 72 65 65 20  ** at the btree 
1f9e3 6c 65 76 65 6c 2c 20 69 6e 20 63 61 73 65 20 74  level, in case t
1f9e4 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
1f9e5 63 65 20 74 61 62 6c 65 20 6e 65 65 64 73 20 74  ce table needs t
1f9e6 6f 0a 20 20 20 20 2a 2a 20 6d 6f 76 65 20 61 73  o.    ** move as
1f9e7 20 61 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65   a result of the
1f9e8 20 64 72 6f 70 20 28 63 61 6e 20 68 61 70 70 65   drop (can happe
1f9e9 6e 20 69 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  n in auto-vacuum
1f9ea 20 6d 6f 64 65 29 2e 0a 20 20 20 20 2a 2f 0a 20   mode)..    */. 
1f9eb 20 20 20 69 66 28 20 70 54 61 62 2d 3e 74 61 62     if( pTab->tab
1f9ec 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f 69  Flags & TF_Autoi
1f9ed 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20 20 20  ncrement ){.    
1f9ee 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50    sqlite3NestedP
1f9ef 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20 20 20  arse(pParse,.   
1f9f0 20 20 20 20 20 22 44 45 4c 45 54 45 20 46 52 4f       "DELETE FRO
1f9f1 4d 20 25 73 2e 73 71 6c 69 74 65 5f 73 65 71 75  M %s.sqlite_sequ
1f9f2 65 6e 63 65 20 57 48 45 52 45 20 6e 61 6d 65 3d  ence WHERE name=
1f9f3 25 51 22 2c 0a 20 20 20 20 20 20 20 20 70 44 62  %Q",.        pDb
1f9f4 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d 3e 7a  ->zName, pTab->z
1f9f5 4e 61 6d 65 0a 20 20 20 20 20 20 29 3b 0a 20 20  Name.      );.  
1f9f6 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20    }.#endif..    
1f9f7 2f 2a 20 44 72 6f 70 20 61 6c 6c 20 53 51 4c 49  /* Drop all SQLI
1f9f8 54 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 20  TE_MASTER table 
1f9f9 61 6e 64 20 69 6e 64 65 78 20 65 6e 74 72 69 65  and index entrie
1f9fa 73 20 74 68 61 74 20 72 65 66 65 72 20 74 6f 20  s that refer to 
1f9fb 74 68 65 0a 20 20 20 20 2a 2a 20 74 61 62 6c 65  the.    ** table
1f9fc 2e 20 54 68 65 20 70 72 6f 67 72 61 6d 20 6e 61  . The program na
1f9fd 6d 65 20 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68  me loops through
1f9fe 20 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c   the master tabl
1f9ff 65 20 61 6e 64 20 64 65 6c 65 74 65 73 0a 20 20  e and deletes.  
1fa00 20 20 2a 2a 20 65 76 65 72 79 20 72 6f 77 20 74    ** every row t
1fa01 68 61 74 20 72 65 66 65 72 73 20 74 6f 20 61 20  hat refers to a 
1fa02 74 61 62 6c 65 20 6f 66 20 74 68 65 20 73 61 6d  table of the sam
1fa03 65 20 6e 61 6d 65 20 61 73 20 74 68 65 20 6f 6e  e name as the on
1fa04 65 20 62 65 69 6e 67 0a 20 20 20 20 2a 2a 20 64  e being.    ** d
1fa05 72 6f 70 70 65 64 2e 20 54 72 69 67 67 65 72 73  ropped. Triggers
1fa06 20 61 72 65 20 68 61 6e 64 6c 65 64 20 73 65 70   are handled sep
1fa07 65 72 61 74 65 6c 79 20 62 65 63 61 75 73 65 20  erately because 
1fa08 61 20 74 72 69 67 67 65 72 20 63 61 6e 20 62 65  a trigger can be
1fa09 0a 20 20 20 20 2a 2a 20 63 72 65 61 74 65 64 20  .    ** created 
1fa0a 69 6e 20 74 68 65 20 74 65 6d 70 20 64 61 74 61  in the temp data
1fa0b 62 61 73 65 20 74 68 61 74 20 72 65 66 65 72 73  base that refers
1fa0c 20 74 6f 20 61 20 74 61 62 6c 65 20 69 6e 20 61   to a table in a
1fa0d 6e 6f 74 68 65 72 0a 20 20 20 20 2a 2a 20 64 61  nother.    ** da
1fa0e 74 61 62 61 73 65 2e 0a 20 20 20 20 2a 2f 0a 20  tabase..    */. 
1fa0f 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64     sqlite3Nested
1fa10 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 0a 20  Parse(pParse, . 
1fa11 20 20 20 20 20 20 20 22 44 45 4c 45 54 45 20 46         "DELETE F
1fa12 52 4f 4d 20 25 51 2e 25 73 20 57 48 45 52 45 20  ROM %Q.%s WHERE 
1fa13 74 62 6c 5f 6e 61 6d 65 3d 25 51 20 61 6e 64 20  tbl_name=%Q and 
1fa14 74 79 70 65 21 3d 27 74 72 69 67 67 65 72 27 22  type!='trigger'"
1fa15 2c 0a 20 20 20 20 20 20 20 20 70 44 62 2d 3e 7a  ,.        pDb->z
1fa16 4e 61 6d 65 2c 20 53 43 48 45 4d 41 5f 54 41 42  Name, SCHEMA_TAB
1fa17 4c 45 28 69 44 62 29 2c 20 70 54 61 62 2d 3e 7a  LE(iDb), pTab->z
1fa18 4e 61 6d 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  Name);..    /* D
1fa19 72 6f 70 20 61 6e 79 20 73 74 61 74 69 73 74 69  rop any statisti
1fa1a 63 73 20 66 72 6f 6d 20 74 68 65 20 73 71 6c 69  cs from the sqli
1fa1b 74 65 5f 73 74 61 74 31 20 74 61 62 6c 65 2c 20  te_stat1 table, 
1fa1c 69 66 20 69 74 20 65 78 69 73 74 73 20 2a 2f 0a  if it exists */.
1fa1d 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 46      if( sqlite3F
1fa1e 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71  indTable(db, "sq
1fa1f 6c 69 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d  lite_stat1", db-
1fa20 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29  >aDb[iDb].zName)
1fa21 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1fa22 33 4e 65 73 74 65 64 50 61 72 73 65 28 70 50 61  3NestedParse(pPa
1fa23 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45  rse,.        "DE
1fa24 4c 45 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c  LETE FROM %Q.sql
1fa25 69 74 65 5f 73 74 61 74 31 20 57 48 45 52 45 20  ite_stat1 WHERE 
1fa26 74 62 6c 3d 25 51 22 2c 20 70 44 62 2d 3e 7a 4e  tbl=%Q", pDb->zN
1fa27 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
1fa28 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
1fa29 0a 20 20 20 20 69 66 28 20 21 69 73 56 69 65 77  .    if( !isView
1fa2a 20 26 26 20 21 49 73 56 69 72 74 75 61 6c 28 70   && !IsVirtual(p
1fa2b 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 64 65  Tab) ){.      de
1fa2c 73 74 72 6f 79 54 61 62 6c 65 28 70 50 61 72 73  stroyTable(pPars
1fa2d 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a  e, pTab);.    }.
1fa2e 0a 20 20 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  .    /* Remove t
1fa2f 68 65 20 74 61 62 6c 65 20 65 6e 74 72 79 20 66  he table entry f
1fa30 72 6f 6d 20 53 51 4c 69 74 65 27 73 20 69 6e 74  rom SQLite's int
1fa31 65 72 6e 61 6c 20 73 63 68 65 6d 61 20 61 6e 64  ernal schema and
1fa32 20 6d 6f 64 69 66 79 0a 20 20 20 20 2a 2a 20 74   modify.    ** t
1fa33 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65  he schema cookie
1fa34 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
1fa35 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
1fa36 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
1fa37 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1fa38 50 5f 56 44 65 73 74 72 6f 79 2c 20 69 44 62 2c  P_VDestroy, iDb,
1fa39 20 30 2c 20 30 2c 20 70 54 61 62 2d 3e 7a 4e 61   0, 0, pTab->zNa
1fa3a 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  me, 0);.    }.  
1fa3b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
1fa3c 4f 70 34 28 76 2c 20 4f 50 5f 44 72 6f 70 54 61  Op4(v, OP_DropTa
1fa3d 62 6c 65 2c 20 69 44 62 2c 20 30 2c 20 30 2c 20  ble, iDb, 0, 0, 
1fa3e 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b  pTab->zName, 0);
1fa3f 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e  .    sqlite3Chan
1fa40 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c  geCookie(pParse,
1fa41 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20 73 71 6c   iDb);.  }.  sql
1fa42 69 74 65 56 69 65 77 52 65 73 65 74 41 6c 6c 28  iteViewResetAll(
1fa43 64 62 2c 20 69 44 62 29 3b 0a 0a 65 78 69 74 5f  db, iDb);..exit_
1fa44 64 72 6f 70 5f 74 61 62 6c 65 3a 0a 20 20 73 71  drop_table:.  sq
1fa45 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65  lite3SrcListDele
1fa46 74 65 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 7d  te(db, pName);.}
1fa47 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  ../*.** This rou
1fa48 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74  tine is called t
1fa49 6f 20 63 72 65 61 74 65 20 61 20 6e 65 77 20 66  o create a new f
1fa4a 6f 72 65 69 67 6e 20 6b 65 79 20 6f 6e 20 74 68  oreign key on th
1fa4b 65 20 74 61 62 6c 65 0a 2a 2a 20 63 75 72 72 65  e table.** curre
1fa4c 6e 74 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74  ntly under const
1fa4d 72 75 63 74 69 6f 6e 2e 20 20 70 46 72 6f 6d 43  ruction.  pFromC
1fa4e 6f 6c 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68  ol determines wh
1fa4f 69 63 68 20 63 6f 6c 75 6d 6e 73 0a 2a 2a 20 69  ich columns.** i
1fa50 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 61  n the current ta
1fa51 62 6c 65 20 70 6f 69 6e 74 20 74 6f 20 74 68 65  ble point to the
1fa52 20 66 6f 72 65 69 67 6e 20 6b 65 79 2e 20 20 49   foreign key.  I
1fa53 66 20 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 74 68  f pFromCol==0 th
1fa54 65 6e 0a 2a 2a 20 63 6f 6e 6e 65 63 74 20 74 68  en.** connect th
1fa55 65 20 6b 65 79 20 74 6f 20 74 68 65 20 6c 61 73  e key to the las
1fa56 74 20 63 6f 6c 75 6d 6e 20 69 6e 73 65 72 74 65  t column inserte
1fa57 64 2e 20 20 70 54 6f 20 69 73 20 74 68 65 20 6e  d.  pTo is the n
1fa58 61 6d 65 20 6f 66 0a 2a 2a 20 74 68 65 20 74 61  ame of.** the ta
1fa59 62 6c 65 20 72 65 66 65 72 72 65 64 20 74 6f 2e  ble referred to.
1fa5a 20 20 70 54 6f 43 6f 6c 20 69 73 20 61 20 6c 69    pToCol is a li
1fa5b 73 74 20 6f 66 20 74 61 62 6c 65 73 20 69 6e 20  st of tables in 
1fa5c 74 68 65 20 6f 74 68 65 72 0a 2a 2a 20 70 54 6f  the other.** pTo
1fa5d 20 74 61 62 6c 65 20 74 68 61 74 20 74 68 65 20   table that the 
1fa5e 66 6f 72 65 69 67 6e 20 6b 65 79 20 70 6f 69 6e  foreign key poin
1fa5f 74 73 20 74 6f 2e 20 20 66 6c 61 67 73 20 63 6f  ts to.  flags co
1fa60 6e 74 61 69 6e 73 20 61 6c 6c 0a 2a 2a 20 69 6e  ntains all.** in
1fa61 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
1fa62 74 68 65 20 63 6f 6e 66 6c 69 63 74 20 72 65 73  the conflict res
1fa63 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74 68  olution algorith
1fa64 6d 73 20 73 70 65 63 69 66 69 65 64 0a 2a 2a 20  ms specified.** 
1fa65 69 6e 20 74 68 65 20 4f 4e 20 44 45 4c 45 54 45  in the ON DELETE
1fa66 2c 20 4f 4e 20 55 50 44 41 54 45 20 61 6e 64 20  , ON UPDATE and 
1fa67 4f 4e 20 49 4e 53 45 52 54 20 63 6c 61 75 73 65  ON INSERT clause
1fa68 73 2e 0a 2a 2a 0a 2a 2a 20 41 6e 20 46 4b 65 79  s..**.** An FKey
1fa69 20 73 74 72 75 63 74 75 72 65 20 69 73 20 63 72   structure is cr
1fa6a 65 61 74 65 64 20 61 6e 64 20 61 64 64 65 64 20  eated and added 
1fa6b 74 6f 20 74 68 65 20 74 61 62 6c 65 20 63 75 72  to the table cur
1fa6c 72 65 6e 74 6c 79 0a 2a 2a 20 75 6e 64 65 72 20  rently.** under 
1fa6d 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 69 6e 20  construction in 
1fa6e 74 68 65 20 70 50 61 72 73 65 2d 3e 70 4e 65 77  the pParse->pNew
1fa6f 54 61 62 6c 65 20 66 69 65 6c 64 2e 20 20 54 68  Table field.  Th
1fa70 65 20 6e 65 77 20 46 4b 65 79 0a 2a 2a 20 69 73  e new FKey.** is
1fa71 20 6e 6f 74 20 6c 69 6e 6b 65 64 20 69 6e 74 6f   not linked into
1fa72 20 64 62 2d 3e 61 46 4b 65 79 20 61 74 20 74 68   db->aFKey at th
1fa73 69 73 20 70 6f 69 6e 74 20 2d 20 74 68 61 74 20  is point - that 
1fa74 64 6f 65 73 20 6e 6f 74 20 68 61 70 70 65 6e 0a  does not happen.
1fa75 2a 2a 20 75 6e 74 69 6c 20 73 71 6c 69 74 65 33  ** until sqlite3
1fa76 45 6e 64 54 61 62 6c 65 28 29 2e 0a 2a 2a 0a 2a  EndTable()..**.*
1fa77 2a 20 54 68 65 20 66 6f 72 65 69 67 6e 20 6b 65  * The foreign ke
1fa78 79 20 69 73 20 73 65 74 20 66 6f 72 20 49 4d 4d  y is set for IMM
1fa79 45 44 49 41 54 45 20 70 72 6f 63 65 73 73 69 6e  EDIATE processin
1fa7a 67 2e 20 20 41 20 73 75 62 73 65 71 75 65 6e 74  g.  A subsequent
1fa7b 20 63 61 6c 6c 0a 2a 2a 20 74 6f 20 73 71 6c 69   call.** to sqli
1fa7c 74 65 33 44 65 66 65 72 46 6f 72 65 69 67 6e 4b  te3DeferForeignK
1fa7d 65 79 28 29 20 6d 69 67 68 74 20 63 68 61 6e 67  ey() might chang
1fa7e 65 20 74 68 69 73 20 74 6f 20 44 45 46 45 52 52  e this to DEFERR
1fa7f 45 44 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ED..*/.SQLITE_PR
1fa80 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
1fa81 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e 4b  e3CreateForeignK
1fa82 65 79 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ey(.  Parse *pPa
1fa83 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61  rse,       /* Pa
1fa84 72 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  rsing context */
1fa85 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 46 72  .  ExprList *pFr
1fa86 6f 6d 43 6f 6c 2c 20 20 2f 2a 20 43 6f 6c 75 6d  omCol,  /* Colum
1fa87 6e 73 20 69 6e 20 74 68 69 73 20 74 61 62 6c 65  ns in this table
1fa88 20 74 68 61 74 20 70 6f 69 6e 74 20 74 6f 20 6f   that point to o
1fa89 74 68 65 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20  ther table */.  
1fa8a 54 6f 6b 65 6e 20 2a 70 54 6f 2c 20 20 20 20 20  Token *pTo,     
1fa8b 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20       /* Name of 
1fa8c 74 68 65 20 6f 74 68 65 72 20 74 61 62 6c 65 20  the other table 
1fa8d 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
1fa8e 54 6f 43 6f 6c 2c 20 20 20 20 2f 2a 20 43 6f 6c  ToCol,    /* Col
1fa8f 75 6d 6e 73 20 69 6e 20 74 68 65 20 6f 74 68 65  umns in the othe
1fa90 72 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  r table */.  int
1fa91 20 66 6c 61 67 73 20 20 20 20 20 20 20 20 20 20   flags          
1fa92 20 20 2f 2a 20 43 6f 6e 66 6c 69 63 74 20 72 65    /* Conflict re
1fa93 73 6f 6c 75 74 69 6f 6e 20 61 6c 67 6f 72 69 74  solution algorit
1fa94 68 6d 73 2e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  hms. */.){.  sql
1fa95 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
1fa96 65 2d 3e 64 62 3b 0a 23 69 66 6e 64 65 66 20 53  e->db;.#ifndef S
1fa97 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
1fa98 47 4e 5f 4b 45 59 0a 20 20 46 4b 65 79 20 2a 70  GN_KEY.  FKey *p
1fa99 46 4b 65 79 20 3d 20 30 3b 0a 20 20 54 61 62 6c  FKey = 0;.  Tabl
1fa9a 65 20 2a 70 20 3d 20 70 50 61 72 73 65 2d 3e 70  e *p = pParse->p
1fa9b 4e 65 77 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20  NewTable;.  int 
1fa9c 6e 42 79 74 65 3b 0a 20 20 69 6e 74 20 69 3b 0a  nByte;.  int i;.
1fa9d 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20 20 63 68    int nCol;.  ch
1fa9e 61 72 20 2a 7a 3b 0a 0a 20 20 61 73 73 65 72 74  ar *z;..  assert
1fa9f 28 20 70 54 6f 21 3d 30 20 29 3b 0a 20 20 69 66  ( pTo!=0 );.  if
1faa0 28 20 70 3d 3d 30 20 7c 7c 20 70 50 61 72 73 65  ( p==0 || pParse
1faa1 2d 3e 6e 45 72 72 20 7c 7c 20 49 4e 5f 44 45 43  ->nErr || IN_DEC
1faa2 4c 41 52 45 5f 56 54 41 42 20 29 20 67 6f 74 6f  LARE_VTAB ) goto
1faa3 20 66 6b 5f 65 6e 64 3b 0a 20 20 69 66 28 20 70   fk_end;.  if( p
1faa4 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20 20  FromCol==0 ){.  
1faa5 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70 2d 3e    int iCol = p->
1faa6 6e 43 6f 6c 2d 31 3b 0a 20 20 20 20 69 66 28 20  nCol-1;.    if( 
1faa7 69 43 6f 6c 3c 30 20 29 20 67 6f 74 6f 20 66 6b  iCol<0 ) goto fk
1faa8 5f 65 6e 64 3b 0a 20 20 20 20 69 66 28 20 70 54  _end;.    if( pT
1faa9 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d 3e  oCol && pToCol->
1faaa 6e 45 78 70 72 21 3d 31 20 29 7b 0a 20 20 20 20  nExpr!=1 ){.    
1faab 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
1faac 67 28 70 50 61 72 73 65 2c 20 22 66 6f 72 65 69  g(pParse, "forei
1faad 67 6e 20 6b 65 79 20 6f 6e 20 25 73 22 0a 20 20  gn key on %s".  
1faae 20 20 20 20 20 20 20 22 20 73 68 6f 75 6c 64 20         " should 
1faaf 72 65 66 65 72 65 6e 63 65 20 6f 6e 6c 79 20 6f  reference only o
1fab0 6e 65 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 61 62  ne column of tab
1fab1 6c 65 20 25 54 22 2c 0a 20 20 20 20 20 20 20 20  le %T",.        
1fab2 20 70 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a   p->aCol[iCol].z
1fab3 4e 61 6d 65 2c 20 70 54 6f 29 3b 0a 20 20 20 20  Name, pTo);.    
1fab4 20 20 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20    goto fk_end;. 
1fab5 20 20 20 7d 0a 20 20 20 20 6e 43 6f 6c 20 3d 20     }.    nCol = 
1fab6 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 70  1;.  }else if( p
1fab7 54 6f 43 6f 6c 20 26 26 20 70 54 6f 43 6f 6c 2d  ToCol && pToCol-
1fab8 3e 6e 45 78 70 72 21 3d 70 46 72 6f 6d 43 6f 6c  >nExpr!=pFromCol
1fab9 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 73  ->nExpr ){.    s
1faba 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
1fabb 50 61 72 73 65 2c 0a 20 20 20 20 20 20 20 20 22  Parse,.        "
1fabc 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e  number of column
1fabd 73 20 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79  s in foreign key
1fabe 20 64 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20   does not match 
1fabf 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 22 0a  the number of ".
1fac0 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e 73          "columns
1fac1 20 69 6e 20 74 68 65 20 72 65 66 65 72 65 6e 63   in the referenc
1fac2 65 64 20 74 61 62 6c 65 22 29 3b 0a 20 20 20 20  ed table");.    
1fac3 67 6f 74 6f 20 66 6b 5f 65 6e 64 3b 0a 20 20 7d  goto fk_end;.  }
1fac4 65 6c 73 65 7b 0a 20 20 20 20 6e 43 6f 6c 20 3d  else{.    nCol =
1fac5 20 70 46 72 6f 6d 43 6f 6c 2d 3e 6e 45 78 70 72   pFromCol->nExpr
1fac6 3b 0a 20 20 7d 0a 20 20 6e 42 79 74 65 20 3d 20  ;.  }.  nByte = 
1fac7 73 69 7a 65 6f 66 28 2a 70 46 4b 65 79 29 20 2b  sizeof(*pFKey) +
1fac8 20 6e 43 6f 6c 2a 73 69 7a 65 6f 66 28 70 46 4b   nCol*sizeof(pFK
1fac9 65 79 2d 3e 61 43 6f 6c 5b 30 5d 29 20 2b 20 70  ey->aCol[0]) + p
1faca 54 6f 2d 3e 6e 20 2b 20 31 3b 0a 20 20 69 66 28  To->n + 1;.  if(
1facb 20 70 54 6f 43 6f 6c 20 29 7b 0a 20 20 20 20 66   pToCol ){.    f
1facc 6f 72 28 69 3d 30 3b 20 69 3c 70 54 6f 43 6f 6c  or(i=0; i<pToCol
1facd 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a 20  ->nExpr; i++){. 
1face 20 20 20 20 20 6e 42 79 74 65 20 2b 3d 20 73 74       nByte += st
1facf 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69  rlen(pToCol->a[i
1fad0 5d 2e 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20 20  ].zName) + 1;.  
1fad1 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b 65 79 20    }.  }.  pFKey 
1fad2 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
1fad3 63 5a 65 72 6f 28 64 62 2c 20 6e 42 79 74 65 20  cZero(db, nByte 
1fad4 29 3b 0a 20 20 69 66 28 20 70 46 4b 65 79 3d 3d  );.  if( pFKey==
1fad5 30 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 66 6b  0 ){.    goto fk
1fad6 5f 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 46 4b 65  _end;.  }.  pFKe
1fad7 79 2d 3e 70 46 72 6f 6d 20 3d 20 70 3b 0a 20 20  y->pFrom = p;.  
1fad8 70 46 4b 65 79 2d 3e 70 4e 65 78 74 46 72 6f 6d  pFKey->pNextFrom
1fad9 20 3d 20 70 2d 3e 70 46 4b 65 79 3b 0a 20 20 7a   = p->pFKey;.  z
1fada 20 3d 20 28 63 68 61 72 2a 29 26 70 46 4b 65 79   = (char*)&pFKey
1fadb 5b 31 5d 3b 0a 20 20 70 46 4b 65 79 2d 3e 61 43  [1];.  pFKey->aC
1fadc 6f 6c 20 3d 20 28 73 74 72 75 63 74 20 73 43 6f  ol = (struct sCo
1fadd 6c 4d 61 70 2a 29 7a 3b 0a 20 20 7a 20 2b 3d 20  lMap*)z;.  z += 
1fade 73 69 7a 65 6f 66 28 73 74 72 75 63 74 20 73 43  sizeof(struct sC
1fadf 6f 6c 4d 61 70 29 2a 6e 43 6f 6c 3b 0a 20 20 70  olMap)*nCol;.  p
1fae0 46 4b 65 79 2d 3e 7a 54 6f 20 3d 20 7a 3b 0a 20  FKey->zTo = z;. 
1fae1 20 6d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 2d 3e   memcpy(z, pTo->
1fae2 7a 2c 20 70 54 6f 2d 3e 6e 29 3b 0a 20 20 7a 5b  z, pTo->n);.  z[
1fae3 70 54 6f 2d 3e 6e 5d 20 3d 20 30 3b 0a 20 20 7a  pTo->n] = 0;.  z
1fae4 20 2b 3d 20 70 54 6f 2d 3e 6e 2b 31 3b 0a 20 20   += pTo->n+1;.  
1fae5 70 46 4b 65 79 2d 3e 70 4e 65 78 74 54 6f 20 3d  pFKey->pNextTo =
1fae6 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 6e 43 6f   0;.  pFKey->nCo
1fae7 6c 20 3d 20 6e 43 6f 6c 3b 0a 20 20 69 66 28 20  l = nCol;.  if( 
1fae8 70 46 72 6f 6d 43 6f 6c 3d 3d 30 20 29 7b 0a 20  pFromCol==0 ){. 
1fae9 20 20 20 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 30     pFKey->aCol[0
1faea 5d 2e 69 46 72 6f 6d 20 3d 20 70 2d 3e 6e 43 6f  ].iFrom = p->nCo
1faeb 6c 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  l-1;.  }else{.  
1faec 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 43 6f    for(i=0; i<nCo
1faed 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
1faee 6e 74 20 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28  nt j;.      for(
1faef 6a 3d 30 3b 20 6a 3c 70 2d 3e 6e 43 6f 6c 3b 20  j=0; j<p->nCol; 
1faf0 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66  j++){.        if
1faf1 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
1faf2 28 70 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d  (p->aCol[j].zNam
1faf3 65 2c 20 70 46 72 6f 6d 43 6f 6c 2d 3e 61 5b 69  e, pFromCol->a[i
1faf4 5d 2e 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  ].zName)==0 ){. 
1faf5 20 20 20 20 20 20 20 20 20 70 46 4b 65 79 2d 3e           pFKey->
1faf6 61 43 6f 6c 5b 69 5d 2e 69 46 72 6f 6d 20 3d 20  aCol[i].iFrom = 
1faf7 6a 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65  j;.          bre
1faf8 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ak;.        }.  
1faf9 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20      }.      if( 
1fafa 6a 3e 3d 70 2d 3e 6e 43 6f 6c 20 29 7b 0a 20 20  j>=p->nCol ){.  
1fafb 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
1fafc 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
1fafd 20 20 20 20 20 20 20 20 20 22 75 6e 6b 6e 6f 77           "unknow
1fafe 6e 20 63 6f 6c 75 6d 6e 20 5c 22 25 73 5c 22 20  n column \"%s\" 
1faff 69 6e 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64  in foreign key d
1fb00 65 66 69 6e 69 74 69 6f 6e 22 2c 20 0a 20 20 20  efinition", .   
1fb01 20 20 20 20 20 20 20 70 46 72 6f 6d 43 6f 6c 2d         pFromCol-
1fb02 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  >a[i].zName);.  
1fb03 20 20 20 20 20 20 67 6f 74 6f 20 66 6b 5f 65 6e        goto fk_en
1fb04 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  d;.      }.    }
1fb05 0a 20 20 7d 0a 20 20 69 66 28 20 70 54 6f 43 6f  .  }.  if( pToCo
1fb06 6c 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  l ){.    for(i=0
1fb07 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ; i<nCol; i++){.
1fb08 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 73 74        int n = st
1fb09 72 6c 65 6e 28 70 54 6f 43 6f 6c 2d 3e 61 5b 69  rlen(pToCol->a[i
1fb0a 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ].zName);.      
1fb0b 70 46 4b 65 79 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a  pFKey->aCol[i].z
1fb0c 43 6f 6c 20 3d 20 7a 3b 0a 20 20 20 20 20 20 6d  Col = z;.      m
1fb0d 65 6d 63 70 79 28 7a 2c 20 70 54 6f 43 6f 6c 2d  emcpy(z, pToCol-
1fb0e 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 6e 29 3b  >a[i].zName, n);
1fb0f 0a 20 20 20 20 20 20 7a 5b 6e 5d 20 3d 20 30 3b  .      z[n] = 0;
1fb10 0a 20 20 20 20 20 20 7a 20 2b 3d 20 6e 2b 31 3b  .      z += n+1;
1fb11 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 70 46 4b  .    }.  }.  pFK
1fb12 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64 20 3d  ey->isDeferred =
1fb13 20 30 3b 0a 20 20 70 46 4b 65 79 2d 3e 64 65 6c   0;.  pFKey->del
1fb14 65 74 65 43 6f 6e 66 20 3d 20 66 6c 61 67 73 20  eteConf = flags 
1fb15 26 20 30 78 66 66 3b 0a 20 20 70 46 4b 65 79 2d  & 0xff;.  pFKey-
1fb16 3e 75 70 64 61 74 65 43 6f 6e 66 20 3d 20 28 66  >updateConf = (f
1fb17 6c 61 67 73 20 3e 3e 20 38 20 29 20 26 20 30 78  lags >> 8 ) & 0x
1fb18 66 66 3b 0a 20 20 70 46 4b 65 79 2d 3e 69 6e 73  ff;.  pFKey->ins
1fb19 65 72 74 43 6f 6e 66 20 3d 20 28 66 6c 61 67 73  ertConf = (flags
1fb1a 20 3e 3e 20 31 36 20 29 20 26 20 30 78 66 66 3b   >> 16 ) & 0xff;
1fb1b 0a 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20  ..  /* Link the 
1fb1c 66 6f 72 65 69 67 6e 20 6b 65 79 20 74 6f 20 74  foreign key to t
1fb1d 68 65 20 74 61 62 6c 65 20 61 73 20 74 68 65 20  he table as the 
1fb1e 6c 61 73 74 20 73 74 65 70 2e 0a 20 20 2a 2f 0a  last step..  */.
1fb1f 20 20 70 2d 3e 70 46 4b 65 79 20 3d 20 70 46 4b    p->pFKey = pFK
1fb20 65 79 3b 0a 20 20 70 46 4b 65 79 20 3d 20 30 3b  ey;.  pFKey = 0;
1fb21 0a 0a 66 6b 5f 65 6e 64 3a 0a 20 20 73 71 6c 69  ..fk_end:.  sqli
1fb22 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 46  te3DbFree(db, pF
1fb23 4b 65 79 29 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  Key);.#endif /* 
1fb24 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
1fb25 4f 4d 49 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59  OMIT_FOREIGN_KEY
1fb26 29 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 78  ) */.  sqlite3Ex
1fb27 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
1fb28 20 70 46 72 6f 6d 43 6f 6c 29 3b 0a 20 20 73 71   pFromCol);.  sq
1fb29 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
1fb2a 65 74 65 28 64 62 2c 20 70 54 6f 43 6f 6c 29 3b  ete(db, pToCol);
1fb2b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
1fb2c 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64  outine is called
1fb2d 20 77 68 65 6e 20 61 6e 20 49 4e 49 54 49 41 4c   when an INITIAL
1fb2e 4c 59 20 49 4d 4d 45 44 49 41 54 45 20 6f 72 20  LY IMMEDIATE or 
1fb2f 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
1fb30 45 44 0a 2a 2a 20 63 6c 61 75 73 65 20 69 73 20  ED.** clause is 
1fb31 73 65 65 6e 20 61 73 20 70 61 72 74 20 6f 66 20  seen as part of 
1fb32 61 20 66 6f 72 65 69 67 6e 20 6b 65 79 20 64 65  a foreign key de
1fb33 66 69 6e 69 74 69 6f 6e 2e 20 20 54 68 65 20 69  finition.  The i
1fb34 73 44 65 66 65 72 72 65 64 0a 2a 2a 20 70 61 72  sDeferred.** par
1fb35 61 6d 65 74 65 72 20 69 73 20 31 20 66 6f 72 20  ameter is 1 for 
1fb36 49 4e 49 54 49 41 4c 4c 59 20 44 45 46 45 52 52  INITIALLY DEFERR
1fb37 45 44 20 61 6e 64 20 30 20 66 6f 72 20 49 4e 49  ED and 0 for INI
1fb38 54 49 41 4c 4c 59 20 49 4d 4d 45 44 49 41 54 45  TIALLY IMMEDIATE
1fb39 2e 0a 2a 2a 20 54 68 65 20 62 65 68 61 76 69 6f  ..** The behavio
1fb3a 72 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  r of the most re
1fb3b 63 65 6e 74 6c 79 20 63 72 65 61 74 65 64 20 66  cently created f
1fb3c 6f 72 65 69 67 6e 20 6b 65 79 20 69 73 20 61 64  oreign key is ad
1fb3d 6a 75 73 74 65 64 0a 2a 2a 20 61 63 63 6f 72 64  justed.** accord
1fb3e 69 6e 67 6c 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ingly..*/.SQLITE
1fb3f 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
1fb40 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69 67  lite3DeferForeig
1fb41 6e 4b 65 79 28 50 61 72 73 65 20 2a 70 50 61 72  nKey(Parse *pPar
1fb42 73 65 2c 20 69 6e 74 20 69 73 44 65 66 65 72 72  se, int isDeferr
1fb43 65 64 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  ed){.#ifndef SQL
1fb44 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49 47 4e  ITE_OMIT_FOREIGN
1fb45 5f 4b 45 59 0a 20 20 54 61 62 6c 65 20 2a 70 54  _KEY.  Table *pT
1fb46 61 62 3b 0a 20 20 46 4b 65 79 20 2a 70 46 4b 65  ab;.  FKey *pFKe
1fb47 79 3b 0a 20 20 69 66 28 20 28 70 54 61 62 20 3d  y;.  if( (pTab =
1fb48 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62   pParse->pNewTab
1fb49 6c 65 29 3d 3d 30 20 7c 7c 20 28 70 46 4b 65 79  le)==0 || (pFKey
1fb4a 20 3d 20 70 54 61 62 2d 3e 70 46 4b 65 79 29 3d   = pTab->pFKey)=
1fb4b 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 70  =0 ) return;.  p
1fb4c 46 4b 65 79 2d 3e 69 73 44 65 66 65 72 72 65 64  FKey->isDeferred
1fb4d 20 3d 20 69 73 44 65 66 65 72 72 65 64 3b 0a 23   = isDeferred;.#
1fb4e 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  endif.}../*.** G
1fb4f 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61  enerate code tha
1fb50 74 20 77 69 6c 6c 20 65 72 61 73 65 20 61 6e 64  t will erase and
1fb51 20 72 65 66 69 6c 6c 20 69 6e 64 65 78 20 2a 70   refill index *p
1fb52 49 64 78 2e 20 20 54 68 69 73 20 69 73 0a 2a 2a  Idx.  This is.**
1fb53 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69 61 6c   used to initial
1fb54 69 7a 65 20 61 20 6e 65 77 6c 79 20 63 72 65 61  ize a newly crea
1fb55 74 65 64 20 69 6e 64 65 78 20 6f 72 20 74 6f 20  ted index or to 
1fb56 72 65 63 6f 6d 70 75 74 65 20 74 68 65 0a 2a 2a  recompute the.**
1fb57 20 63 6f 6e 74 65 6e 74 20 6f 66 20 61 6e 20 69   content of an i
1fb58 6e 64 65 78 20 69 6e 20 72 65 73 70 6f 6e 73 65  ndex in response
1fb59 20 74 6f 20 61 20 52 45 49 4e 44 45 58 20 63 6f   to a REINDEX co
1fb5a 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 69 66 20  mmand..**.** if 
1fb5b 6d 65 6d 52 6f 6f 74 50 61 67 65 20 69 73 20 6e  memRootPage is n
1fb5c 6f 74 20 6e 65 67 61 74 69 76 65 2c 20 69 74 20  ot negative, it 
1fb5d 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 69  means that the i
1fb5e 6e 64 65 78 20 69 73 20 6e 65 77 6c 79 0a 2a 2a  ndex is newly.**
1fb5f 20 63 72 65 61 74 65 64 2e 20 20 54 68 65 20 72   created.  The r
1fb60 65 67 69 73 74 65 72 20 73 70 65 63 69 66 69 65  egister specifie
1fb61 64 20 62 79 20 6d 65 6d 52 6f 6f 74 50 61 67 65  d by memRootPage
1fb62 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 0a 2a 2a   contains the.**
1fb63 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d 62 65   root page numbe
1fb64 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78 2e 20  r of the index. 
1fb65 20 49 66 20 6d 65 6d 52 6f 6f 74 50 61 67 65 20   If memRootPage 
1fb66 69 73 20 6e 65 67 61 74 69 76 65 2c 20 74 68 65  is negative, the
1fb67 6e 0a 2a 2a 20 74 68 65 20 69 6e 64 65 78 20 61  n.** the index a
1fb68 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 61 6e  lready exists an
1fb69 64 20 6d 75 73 74 20 62 65 20 63 6c 65 61 72 65  d must be cleare
1fb6a 64 20 62 65 66 6f 72 65 20 62 65 69 6e 67 20 72  d before being r
1fb6b 65 66 69 6c 6c 65 64 20 61 6e 64 0a 2a 2a 20 74  efilled and.** t
1fb6c 68 65 20 72 6f 6f 74 20 70 61 67 65 20 6e 75 6d  he root page num
1fb6d 62 65 72 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ber of the index
1fb6e 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d 20 70   is taken from p
1fb6f 49 6e 64 65 78 2d 3e 74 6e 75 6d 2e 0a 2a 2f 0a  Index->tnum..*/.
1fb70 73 74 61 74 69 63 20 76 6f 69 64 20 73 71 6c 69  static void sqli
1fb71 74 65 33 52 65 66 69 6c 6c 49 6e 64 65 78 28 50  te3RefillIndex(P
1fb72 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e  arse *pParse, In
1fb73 64 65 78 20 2a 70 49 6e 64 65 78 2c 20 69 6e 74  dex *pIndex, int
1fb74 20 6d 65 6d 52 6f 6f 74 50 61 67 65 29 7b 0a 20   memRootPage){. 
1fb75 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 70   Table *pTab = p
1fb76 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b 20 20  Index->pTable;  
1fb77 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 61  /* The table tha
1fb78 74 20 69 73 20 69 6e 64 65 78 65 64 20 2a 2f 0a  t is indexed */.
1fb79 20 20 69 6e 74 20 69 54 61 62 20 3d 20 70 50 61    int iTab = pPa
1fb7a 72 73 65 2d 3e 6e 54 61 62 3b 20 20 20 20 20 20  rse->nTab;      
1fb7b 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73 6f 72   /* Btree cursor
1fb7c 20 75 73 65 64 20 66 6f 72 20 70 54 61 62 20 2a   used for pTab *
1fb7d 2f 0a 20 20 69 6e 74 20 69 49 64 78 20 3d 20 70  /.  int iIdx = p
1fb7e 50 61 72 73 65 2d 3e 6e 54 61 62 2b 31 3b 20 20  Parse->nTab+1;  
1fb7f 20 20 20 2f 2a 20 42 74 72 65 65 20 63 75 72 73     /* Btree curs
1fb80 6f 72 20 75 73 65 64 20 66 6f 72 20 70 49 6e 64  or used for pInd
1fb81 65 78 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ex */.  int addr
1fb82 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
1fb83 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
1fb84 73 20 6f 66 20 74 6f 70 20 6f 66 20 6c 6f 6f 70  s of top of loop
1fb85 20 2a 2f 0a 20 20 69 6e 74 20 74 6e 75 6d 3b 20   */.  int tnum; 
1fb86 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb87 20 20 20 20 20 2f 2a 20 52 6f 6f 74 20 70 61 67       /* Root pag
1fb88 65 20 6f 66 20 69 6e 64 65 78 20 2a 2f 0a 20 20  e of index */.  
1fb89 56 64 62 65 20 2a 76 3b 20 20 20 20 20 20 20 20  Vdbe *v;        
1fb8a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
1fb8b 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
1fb8c 69 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61  into this virtua
1fb8d 6c 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 4b  l machine */.  K
1fb8e 65 79 49 6e 66 6f 20 2a 70 4b 65 79 3b 20 20 20  eyInfo *pKey;   
1fb8f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
1fb90 20 4b 65 79 49 6e 66 6f 20 66 6f 72 20 69 6e 64   KeyInfo for ind
1fb91 65 78 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 49  ex */.  int regI
1fb92 64 78 4b 65 79 3b 20 20 20 20 20 20 20 20 20 20  dxKey;          
1fb93 20 20 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74         /* Regist
1fb94 65 72 73 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ers containing t
1fb95 68 65 20 69 6e 64 65 78 20 6b 65 79 20 2a 2f 0a  he index key */.
1fb96 20 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b    int regRecord;
1fb97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fb98 20 2f 2a 20 52 65 67 69 73 74 65 72 20 68 6f 6c   /* Register hol
1fb99 64 69 6e 67 20 61 73 73 65 6d 62 6c 69 65 64 20  ding assemblied 
1fb9a 69 6e 64 65 78 20 72 65 63 6f 72 64 20 2a 2f 0a  index record */.
1fb9b 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
1fb9c 70 50 61 72 73 65 2d 3e 64 62 3b 20 20 20 20 20  pParse->db;     
1fb9d 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73 65   /* The database
1fb9e 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
1fb9f 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69 74   int iDb = sqlit
1fba0 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1fba1 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63 68  db, pIndex->pSch
1fba2 65 6d 61 29 3b 0a 0a 23 69 66 6e 64 65 66 20 53  ema);..#ifndef S
1fba3 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
1fba4 52 49 5a 41 54 49 4f 4e 0a 20 20 69 66 28 20 73  RIZATION.  if( s
1fba5 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
1fba6 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 52  pParse, SQLITE_R
1fba7 45 49 4e 44 45 58 2c 20 70 49 6e 64 65 78 2d 3e  EINDEX, pIndex->
1fba8 7a 4e 61 6d 65 2c 20 30 2c 0a 20 20 20 20 20 20  zName, 0,.      
1fba9 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61  db->aDb[iDb].zNa
1fbaa 6d 65 20 29 20 29 7b 0a 20 20 20 20 72 65 74 75  me ) ){.    retu
1fbab 72 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a  rn;.  }.#endif..
1fbac 20 20 2f 2a 20 52 65 71 75 69 72 65 20 61 20 77    /* Require a w
1fbad 72 69 74 65 2d 6c 6f 63 6b 20 6f 6e 20 74 68 65  rite-lock on the
1fbae 20 74 61 62 6c 65 20 74 6f 20 70 65 72 66 6f 72   table to perfor
1fbaf 6d 20 74 68 69 73 20 6f 70 65 72 61 74 69 6f 6e  m this operation
1fbb0 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 54 61 62   */.  sqlite3Tab
1fbb1 6c 65 4c 6f 63 6b 28 70 50 61 72 73 65 2c 20 69  leLock(pParse, i
1fbb2 44 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20  Db, pTab->tnum, 
1fbb3 31 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b  1, pTab->zName);
1fbb4 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  ..  v = sqlite3G
1fbb5 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
1fbb6 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
1fbb7 75 72 6e 3b 0a 20 20 69 66 28 20 6d 65 6d 52 6f  urn;.  if( memRo
1fbb8 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20 20  otPage>=0 ){.   
1fbb9 20 74 6e 75 6d 20 3d 20 6d 65 6d 52 6f 6f 74 50   tnum = memRootP
1fbba 61 67 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  age;.  }else{.  
1fbbb 20 20 74 6e 75 6d 20 3d 20 70 49 6e 64 65 78 2d    tnum = pIndex-
1fbbc 3e 74 6e 75 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >tnum;.    sqlit
1fbbd 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
1fbbe 4f 50 5f 43 6c 65 61 72 2c 20 74 6e 75 6d 2c 20  OP_Clear, tnum, 
1fbbf 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 4b 65 79  iDb);.  }.  pKey
1fbc0 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b   = sqlite3IndexK
1fbc1 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70  eyinfo(pParse, p
1fbc2 49 6e 64 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  Index);.  sqlite
1fbc3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
1fbc4 50 5f 4f 70 65 6e 57 72 69 74 65 2c 20 69 49 64  P_OpenWrite, iId
1fbc5 78 2c 20 74 6e 75 6d 2c 20 69 44 62 2c 20 0a 20  x, tnum, iDb, . 
1fbc6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbc7 20 20 20 28 63 68 61 72 20 2a 29 70 4b 65 79 2c     (char *)pKey,
1fbc8 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
1fbc9 4f 46 46 29 3b 0a 20 20 69 66 28 20 6d 65 6d 52  OFF);.  if( memR
1fbca 6f 6f 74 50 61 67 65 3e 3d 30 20 29 7b 0a 20 20  ootPage>=0 ){.  
1fbcb 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
1fbcc 6e 67 65 50 35 28 76 2c 20 31 29 3b 0a 20 20 7d  ngeP5(v, 1);.  }
1fbcd 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54 61  .  sqlite3OpenTa
1fbce 62 6c 65 28 70 50 61 72 73 65 2c 20 69 54 61 62  ble(pParse, iTab
1fbcf 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50 5f  , iDb, pTab, OP_
1fbd0 4f 70 65 6e 52 65 61 64 29 3b 0a 20 20 61 64 64  OpenRead);.  add
1fbd1 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
1fbd2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77  AddOp2(v, OP_Rew
1fbd3 69 6e 64 2c 20 69 54 61 62 2c 20 30 29 3b 0a 20  ind, iTab, 0);. 
1fbd4 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71 6c   regRecord = sql
1fbd5 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
1fbd6 50 61 72 73 65 29 3b 0a 20 20 72 65 67 49 64 78  Parse);.  regIdx
1fbd7 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 47 65 6e  Key = sqlite3Gen
1fbd8 65 72 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50  erateIndexKey(pP
1fbd9 61 72 73 65 2c 20 70 49 6e 64 65 78 2c 20 69 54  arse, pIndex, iT
1fbda 61 62 2c 20 72 65 67 52 65 63 6f 72 64 2c 20 31  ab, regRecord, 1
1fbdb 29 3b 0a 20 20 69 66 28 20 70 49 6e 64 65 78 2d  );.  if( pIndex-
1fbdc 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e  >onError!=OE_Non
1fbdd 65 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a 31 2c  e ){.    int j1,
1fbde 20 6a 32 3b 0a 20 20 20 20 69 6e 74 20 72 65 67   j2;.    int reg
1fbdf 52 6f 77 69 64 3b 0a 0a 20 20 20 20 72 65 67 52  Rowid;..    regR
1fbe0 6f 77 69 64 20 3d 20 72 65 67 49 64 78 4b 65 79  owid = regIdxKey
1fbe1 20 2b 20 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75   + pIndex->nColu
1fbe2 6d 6e 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c  mn;.    j1 = sql
1fbe3 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1fbe4 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67  , OP_IsNull, reg
1fbe5 49 64 78 4b 65 79 2c 20 30 2c 20 70 49 6e 64 65  IdxKey, 0, pInde
1fbe6 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  x->nColumn);.   
1fbe7 20 6a 32 20 3d 20 73 71 6c 69 74 65 33 56 64 62   j2 = sqlite3Vdb
1fbe8 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 49 73  eAddOp4(v, OP_Is
1fbe9 55 6e 69 71 75 65 2c 20 69 49 64 78 2c 0a 20 20  Unique, iIdx,.  
1fbea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fbeb 20 20 20 20 20 20 20 20 20 30 2c 20 72 65 67 52           0, regR
1fbec 6f 77 69 64 2c 20 53 51 4c 49 54 45 5f 49 4e 54  owid, SQLITE_INT
1fbed 5f 54 4f 5f 50 54 52 28 72 65 67 52 65 63 6f 72  _TO_PTR(regRecor
1fbee 64 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20  d), P4_INT32);. 
1fbef 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
1fbf0 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp4(v, OP_Halt,
1fbf1 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
1fbf2 4e 54 2c 20 4f 45 5f 41 62 6f 72 74 2c 20 30 2c  NT, OE_Abort, 0,
1fbf3 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
1fbf4 20 20 20 20 20 22 69 6e 64 65 78 65 64 20 63 6f       "indexed co
1fbf5 6c 75 6d 6e 73 20 61 72 65 20 6e 6f 74 20 75 6e  lumns are not un
1fbf6 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43  ique", P4_STATIC
1fbf7 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1fbf8 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31  beJumpHere(v, j1
1fbf9 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
1fbfa 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 32  beJumpHere(v, j2
1fbfb 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
1fbfc 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
1fbfd 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 49 64 78  _IdxInsert, iIdx
1fbfe 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
1fbff 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
1fc00 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
1fc01 67 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69  gRecord);.  sqli
1fc02 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
1fc03 20 4f 50 5f 4e 65 78 74 2c 20 69 54 61 62 2c 20   OP_Next, iTab, 
1fc04 61 64 64 72 31 2b 31 29 3b 0a 20 20 73 71 6c 69  addr1+1);.  sqli
1fc05 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
1fc06 76 2c 20 61 64 64 72 31 29 3b 0a 20 20 73 71 6c  v, addr1);.  sql
1fc07 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
1fc08 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 54 61 62  , OP_Close, iTab
1fc09 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
1fc0a 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
1fc0b 73 65 2c 20 69 49 64 78 29 3b 0a 7d 0a 0a 2f 2a  se, iIdx);.}../*
1fc0c 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77  .** Create a new
1fc0d 20 69 6e 64 65 78 20 66 6f 72 20 61 6e 20 53 51   index for an SQ
1fc0e 4c 20 74 61 62 6c 65 2e 20 20 70 4e 61 6d 65 31  L table.  pName1
1fc0f 2e 70 4e 61 6d 65 32 20 69 73 20 74 68 65 20 6e  .pName2 is the n
1fc10 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
1fc11 20 0a 2a 2a 20 61 6e 64 20 70 54 62 6c 4c 69 73   .** and pTblLis
1fc12 74 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66  t is the name of
1fc13 20 74 68 65 20 74 61 62 6c 65 20 74 68 61 74 20   the table that 
1fc14 69 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64  is to be indexed
1fc15 2e 20 20 42 6f 74 68 20 77 69 6c 6c 20 0a 2a 2a  .  Both will .**
1fc16 20 62 65 20 4e 55 4c 4c 20 66 6f 72 20 61 20 70   be NULL for a p
1fc17 72 69 6d 61 72 79 20 6b 65 79 20 6f 72 20 61 6e  rimary key or an
1fc18 20 69 6e 64 65 78 20 74 68 61 74 20 69 73 20 63   index that is c
1fc19 72 65 61 74 65 64 20 74 6f 20 73 61 74 69 73 66  reated to satisf
1fc1a 79 20 61 0a 2a 2a 20 55 4e 49 51 55 45 20 63 6f  y a.** UNIQUE co
1fc1b 6e 73 74 72 61 69 6e 74 2e 20 20 49 66 20 70 54  nstraint.  If pT
1fc1c 61 62 6c 65 20 61 6e 64 20 70 49 6e 64 65 78 20  able and pIndex 
1fc1d 61 72 65 20 4e 55 4c 4c 2c 20 75 73 65 20 70 50  are NULL, use pP
1fc1e 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 0a  arse->pNewTable.
1fc1f 2a 2a 20 61 73 20 74 68 65 20 74 61 62 6c 65 20  ** as the table 
1fc20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e 20 20  to be indexed.  
1fc21 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c  pParse->pNewTabl
1fc22 65 20 69 73 20 61 20 74 61 62 6c 65 20 74 68 61  e is a table tha
1fc23 74 20 69 73 0a 2a 2a 20 63 75 72 72 65 6e 74 6c  t is.** currentl
1fc24 79 20 62 65 69 6e 67 20 63 6f 6e 73 74 72 75 63  y being construc
1fc25 74 65 64 20 62 79 20 61 20 43 52 45 41 54 45 20  ted by a CREATE 
1fc26 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 2e  TABLE statement.
1fc27 0a 2a 2a 0a 2a 2a 20 70 4c 69 73 74 20 69 73 20  .**.** pList is 
1fc28 61 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  a list of column
1fc29 73 20 74 6f 20 62 65 20 69 6e 64 65 78 65 64 2e  s to be indexed.
1fc2a 20 20 70 4c 69 73 74 20 77 69 6c 6c 20 62 65 20    pList will be 
1fc2b 4e 55 4c 4c 20 69 66 20 74 68 69 73 0a 2a 2a 20  NULL if this.** 
1fc2c 69 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79  is a primary key
1fc2d 20 6f 72 20 75 6e 69 71 75 65 2d 63 6f 6e 73 74   or unique-const
1fc2e 72 61 69 6e 74 20 6f 6e 20 74 68 65 20 6d 6f 73  raint on the mos
1fc2f 74 20 72 65 63 65 6e 74 20 63 6f 6c 75 6d 6e 20  t recent column 
1fc30 61 64 64 65 64 0a 2a 2a 20 74 6f 20 74 68 65 20  added.** to the 
1fc31 74 61 62 6c 65 20 63 75 72 72 65 6e 74 6c 79 20  table currently 
1fc32 75 6e 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69  under constructi
1fc33 6f 6e 2e 20 20 0a 2a 2f 0a 53 51 4c 49 54 45 5f  on.  .*/.SQLITE_
1fc34 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
1fc35 69 74 65 33 43 72 65 61 74 65 49 6e 64 65 78 28  ite3CreateIndex(
1fc36 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
1fc37 2c 20 20 20 20 20 2f 2a 20 41 6c 6c 20 69 6e 66  ,     /* All inf
1fc38 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74  ormation about t
1fc39 68 69 73 20 70 61 72 73 65 20 2a 2f 0a 20 20 54  his parse */.  T
1fc3a 6f 6b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 20 20  oken *pName1,   
1fc3b 20 20 2f 2a 20 46 69 72 73 74 20 70 61 72 74 20    /* First part 
1fc3c 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e 20 4d  of index name. M
1fc3d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a 20 20  ay be NULL */.  
1fc3e 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 32 2c 20 20  Token *pName2,  
1fc3f 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20 70 61 72     /* Second par
1fc40 74 20 6f 66 20 69 6e 64 65 78 20 6e 61 6d 65 2e  t of index name.
1fc41 20 4d 61 79 20 62 65 20 4e 55 4c 4c 20 2a 2f 0a   May be NULL */.
1fc42 20 20 53 72 63 4c 69 73 74 20 2a 70 54 62 6c 4e    SrcList *pTblN
1fc43 61 6d 65 2c 20 2f 2a 20 54 61 62 6c 65 20 74 6f  ame, /* Table to
1fc44 20 69 6e 64 65 78 2e 20 55 73 65 20 70 50 61 72   index. Use pPar
1fc45 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20 69 66  se->pNewTable if
1fc46 20 30 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74   0 */.  ExprList
1fc47 20 2a 70 4c 69 73 74 2c 20 20 20 2f 2a 20 41 20   *pList,   /* A 
1fc48 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  list of columns 
1fc49 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
1fc4a 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 2c 20  .  int onError, 
1fc4b 20 20 20 20 20 20 2f 2a 20 4f 45 5f 41 62 6f 72        /* OE_Abor
1fc4c 74 2c 20 4f 45 5f 49 67 6e 6f 72 65 2c 20 4f 45  t, OE_Ignore, OE
1fc4d 5f 52 65 70 6c 61 63 65 2c 20 6f 72 20 4f 45 5f  _Replace, or OE_
1fc4e 4e 6f 6e 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  None */.  Token 
1fc4f 2a 70 53 74 61 72 74 2c 20 20 20 20 20 2f 2a 20  *pStart,     /* 
1fc50 54 68 65 20 43 52 45 41 54 45 20 74 6f 6b 65 6e  The CREATE token
1fc51 20 74 68 61 74 20 62 65 67 69 6e 73 20 74 68 69   that begins thi
1fc52 73 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20  s statement */. 
1fc53 20 54 6f 6b 65 6e 20 2a 70 45 6e 64 2c 20 20 20   Token *pEnd,   
1fc54 20 20 20 20 2f 2a 20 54 68 65 20 22 29 22 20 74      /* The ")" t
1fc55 68 61 74 20 63 6c 6f 73 65 73 20 74 68 65 20 43  hat closes the C
1fc56 52 45 41 54 45 20 49 4e 44 45 58 20 73 74 61 74  REATE INDEX stat
1fc57 65 6d 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 73  ement */.  int s
1fc58 6f 72 74 4f 72 64 65 72 2c 20 20 20 20 20 2f 2a  ortOrder,     /*
1fc59 20 53 6f 72 74 20 6f 72 64 65 72 20 6f 66 20 70   Sort order of p
1fc5a 72 69 6d 61 72 79 20 6b 65 79 20 77 68 65 6e 20  rimary key when 
1fc5b 70 4c 69 73 74 3d 3d 4e 55 4c 4c 20 2a 2f 0a 20  pList==NULL */. 
1fc5c 20 69 6e 74 20 69 66 4e 6f 74 45 78 69 73 74 20   int ifNotExist 
1fc5d 20 20 20 20 2f 2a 20 4f 6d 69 74 20 65 72 72 6f      /* Omit erro
1fc5e 72 20 69 66 20 69 6e 64 65 78 20 61 6c 72 65 61  r if index alrea
1fc5f 64 79 20 65 78 69 73 74 73 20 2a 2f 0a 29 7b 0a  dy exists */.){.
1fc60 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20    Table *pTab = 
1fc61 30 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20  0;     /* Table 
1fc62 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
1fc63 0a 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78  .  Index *pIndex
1fc64 20 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 69   = 0;   /* The i
1fc65 6e 64 65 78 20 74 6f 20 62 65 20 63 72 65 61 74  ndex to be creat
1fc66 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4e  ed */.  char *zN
1fc67 61 6d 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ame = 0;     /* 
1fc68 4e 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65  Name of the inde
1fc69 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61 6d 65  x */.  int nName
1fc6a 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ;           /* N
1fc6b 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
1fc6c 65 72 73 20 69 6e 20 7a 4e 61 6d 65 20 2a 2f 0a  ers in zName */.
1fc6d 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 54 6f    int i, j;.  To
1fc6e 6b 65 6e 20 6e 75 6c 6c 49 64 3b 20 20 20 20 20  ken nullId;     
1fc6f 20 20 20 2f 2a 20 46 61 6b 65 20 74 6f 6b 65 6e     /* Fake token
1fc70 20 66 6f 72 20 61 6e 20 65 6d 70 74 79 20 49 44   for an empty ID
1fc71 20 6c 69 73 74 20 2a 2f 0a 20 20 44 62 46 69 78   list */.  DbFix
1fc72 65 72 20 73 46 69 78 3b 20 20 20 20 20 20 20 20  er sFix;        
1fc73 2f 2a 20 46 6f 72 20 61 73 73 69 67 6e 69 6e 67  /* For assigning
1fc74 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 73 20   database names 
1fc75 74 6f 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 69  to pTable */.  i
1fc76 6e 74 20 73 6f 72 74 4f 72 64 65 72 4d 61 73 6b  nt sortOrderMask
1fc77 3b 20 20 20 2f 2a 20 31 20 74 6f 20 68 6f 6e 6f  ;   /* 1 to hono
1fc78 72 20 44 45 53 43 20 69 6e 20 69 6e 64 65 78 2e  r DESC in index.
1fc79 20 20 30 20 74 6f 20 69 67 6e 6f 72 65 2e 20 2a    0 to ignore. *
1fc7a 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  /.  sqlite3 *db 
1fc7b 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
1fc7c 44 62 20 2a 70 44 62 3b 20 20 20 20 20 20 20 20  Db *pDb;        
1fc7d 20 20 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63       /* The spec
1fc7e 69 66 69 63 20 74 61 62 6c 65 20 63 6f 6e 74 61  ific table conta
1fc7f 69 6e 69 6e 67 20 74 68 65 20 69 6e 64 65 78 65  ining the indexe
1fc80 64 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  d database */.  
1fc81 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20  int iDb;        
1fc82 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66       /* Index of
1fc83 20 74 68 65 20 64 61 74 61 62 61 73 65 20 74 68   the database th
1fc84 61 74 20 69 73 20 62 65 69 6e 67 20 77 72 69 74  at is being writ
1fc85 74 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a  ten */.  Token *
1fc86 70 4e 61 6d 65 20 3d 20 30 3b 20 20 20 20 2f 2a  pName = 0;    /*
1fc87 20 55 6e 71 75 61 6c 69 66 69 65 64 20 6e 61 6d   Unqualified nam
1fc88 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74  e of the index t
1fc89 6f 20 63 72 65 61 74 65 20 2a 2f 0a 20 20 73 74  o create */.  st
1fc8a 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74  ruct ExprList_it
1fc8b 65 6d 20 2a 70 4c 69 73 74 49 74 65 6d 3b 20 2f  em *pListItem; /
1fc8c 2a 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76  * For looping ov
1fc8d 65 72 20 70 4c 69 73 74 20 2a 2f 0a 20 20 69 6e  er pList */.  in
1fc8e 74 20 6e 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 45  t nCol;.  int nE
1fc8f 78 74 72 61 20 3d 20 30 3b 0a 20 20 63 68 61 72  xtra = 0;.  char
1fc90 20 2a 7a 45 78 74 72 61 3b 0a 0a 20 20 69 66 28   *zExtra;..  if(
1fc91 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c   pParse->nErr ||
1fc92 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
1fc93 64 20 7c 7c 20 49 4e 5f 44 45 43 4c 41 52 45 5f  d || IN_DECLARE_
1fc94 56 54 41 42 20 29 7b 0a 20 20 20 20 67 6f 74 6f  VTAB ){.    goto
1fc95 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1fc96 65 78 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  ex;.  }..  /*.  
1fc97 2a 2a 20 46 69 6e 64 20 74 68 65 20 74 61 62 6c  ** Find the tabl
1fc98 65 20 74 68 61 74 20 69 73 20 74 6f 20 62 65 20  e that is to be 
1fc99 69 6e 64 65 78 65 64 2e 20 20 52 65 74 75 72 6e  indexed.  Return
1fc9a 20 65 61 72 6c 79 20 69 66 20 6e 6f 74 20 66 6f   early if not fo
1fc9b 75 6e 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  und..  */.  if( 
1fc9c 70 54 62 6c 4e 61 6d 65 21 3d 30 20 29 7b 0a 0a  pTblName!=0 ){..
1fc9d 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 74      /* Use the t
1fc9e 77 6f 2d 70 61 72 74 20 69 6e 64 65 78 20 6e 61  wo-part index na
1fc9f 6d 65 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20  me to determine 
1fca0 74 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20  the database .  
1fca1 20 20 2a 2a 20 74 6f 20 73 65 61 72 63 68 20 66    ** to search f
1fca2 6f 72 20 74 68 65 20 74 61 62 6c 65 2e 20 27 46  or the table. 'F
1fca3 69 78 27 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ix' the table na
1fca4 6d 65 20 74 6f 20 74 68 69 73 20 64 62 0a 20 20  me to this db.  
1fca5 20 20 2a 2a 20 62 65 66 6f 72 65 20 6c 6f 6f 6b    ** before look
1fca6 69 6e 67 20 75 70 20 74 68 65 20 74 61 62 6c 65  ing up the table
1fca7 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73  ..    */.    ass
1fca8 65 72 74 28 20 70 4e 61 6d 65 31 20 26 26 20 70  ert( pName1 && p
1fca9 4e 61 6d 65 32 20 29 3b 0a 20 20 20 20 69 44 62  Name2 );.    iDb
1fcaa 20 3d 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72   = sqlite3TwoPar
1fcab 74 4e 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e  tName(pParse, pN
1fcac 61 6d 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70  ame1, pName2, &p
1fcad 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 69  Name);.    if( i
1fcae 44 62 3c 30 20 29 20 67 6f 74 6f 20 65 78 69 74  Db<0 ) goto exit
1fcaf 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 0a  _create_index;..
1fcb0 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1fcb1 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20 20 20 2f  MIT_TEMPDB.    /
1fcb2 2a 20 49 66 20 74 68 65 20 69 6e 64 65 78 20 6e  * If the index n
1fcb3 61 6d 65 20 77 61 73 20 75 6e 71 75 61 6c 69 66  ame was unqualif
1fcb4 69 65 64 2c 20 63 68 65 63 6b 20 69 66 20 74 68  ied, check if th
1fcb5 65 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20  e the table.    
1fcb6 2a 2a 20 69 73 20 61 20 74 65 6d 70 20 74 61 62  ** is a temp tab
1fcb7 6c 65 2e 20 49 66 20 73 6f 2c 20 73 65 74 20 74  le. If so, set t
1fcb8 68 65 20 64 61 74 61 62 61 73 65 20 74 6f 20 31  he database to 1
1fcb9 2e 20 44 6f 20 6e 6f 74 20 64 6f 20 74 68 69 73  . Do not do this
1fcba 0a 20 20 20 20 2a 2a 20 69 66 20 69 6e 69 74 69  .    ** if initi
1fcbb 61 6c 69 73 69 6e 67 20 61 20 64 61 74 61 62 61  alising a databa
1fcbc 73 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a  se schema..    *
1fcbd 2f 0a 20 20 20 20 69 66 28 20 21 64 62 2d 3e 69  /.    if( !db->i
1fcbe 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20  nit.busy ){.    
1fcbf 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
1fcc0 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50  SrcListLookup(pP
1fcc1 61 72 73 65 2c 20 70 54 62 6c 4e 61 6d 65 29 3b  arse, pTblName);
1fcc2 0a 20 20 20 20 20 20 69 66 28 20 70 4e 61 6d 65  .      if( pName
1fcc3 32 20 26 26 20 70 4e 61 6d 65 32 2d 3e 6e 3d 3d  2 && pName2->n==
1fcc4 30 20 26 26 20 70 54 61 62 20 26 26 20 70 54 61  0 && pTab && pTa
1fcc5 62 2d 3e 70 53 63 68 65 6d 61 3d 3d 64 62 2d 3e  b->pSchema==db->
1fcc6 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
1fcc7 7b 0a 20 20 20 20 20 20 20 20 69 44 62 20 3d 20  {.        iDb = 
1fcc8 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  1;.      }.    }
1fcc9 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 69 66 28  .#endif..    if(
1fcca 20 73 71 6c 69 74 65 33 46 69 78 49 6e 69 74 28   sqlite3FixInit(
1fccb 26 73 46 69 78 2c 20 70 50 61 72 73 65 2c 20 69  &sFix, pParse, i
1fccc 44 62 2c 20 22 69 6e 64 65 78 22 2c 20 70 4e 61  Db, "index", pNa
1fccd 6d 65 29 20 26 26 0a 20 20 20 20 20 20 20 20 73  me) &&.        s
1fcce 71 6c 69 74 65 33 46 69 78 53 72 63 4c 69 73 74  qlite3FixSrcList
1fccf 28 26 73 46 69 78 2c 20 70 54 62 6c 4e 61 6d 65  (&sFix, pTblName
1fcd0 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 2f  ).    ){.      /
1fcd1 2a 20 42 65 63 61 75 73 65 20 74 68 65 20 70 61  * Because the pa
1fcd2 72 73 65 72 20 63 6f 6e 73 74 72 75 63 74 73 20  rser constructs 
1fcd3 70 54 62 6c 4e 61 6d 65 20 66 72 6f 6d 20 61 20  pTblName from a 
1fcd4 73 69 6e 67 6c 65 20 69 64 65 6e 74 69 66 69 65  single identifie
1fcd5 72 2c 0a 20 20 20 20 20 20 2a 2a 20 73 71 6c 69  r,.      ** sqli
1fcd6 74 65 33 46 69 78 53 72 63 4c 69 73 74 20 63 61  te3FixSrcList ca
1fcd7 6e 20 6e 65 76 65 72 20 66 61 69 6c 2e 20 2a 2f  n never fail. */
1fcd8 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 30 29  .      assert(0)
1fcd9 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 54 61 62  ;.    }.    pTab
1fcda 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65   = sqlite3Locate
1fcdb 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 30 2c  Table(pParse, 0,
1fcdc 20 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e   pTblName->a[0].
1fcdd 7a 4e 61 6d 65 2c 20 0a 20 20 20 20 20 20 20 20  zName, .        
1fcde 70 54 62 6c 4e 61 6d 65 2d 3e 61 5b 30 5d 2e 7a  pTblName->a[0].z
1fcdf 44 61 74 61 62 61 73 65 29 3b 0a 20 20 20 20 69  Database);.    i
1fce0 66 28 20 21 70 54 61 62 20 7c 7c 20 64 62 2d 3e  f( !pTab || db->
1fce1 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
1fce2 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
1fce3 69 6e 64 65 78 3b 0a 20 20 20 20 61 73 73 65 72  index;.    asser
1fce4 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
1fce5 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e 70  pSchema==pTab->p
1fce6 53 63 68 65 6d 61 20 29 3b 0a 20 20 7d 65 6c 73  Schema );.  }els
1fce7 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  e{.    assert( p
1fce8 4e 61 6d 65 3d 3d 30 20 29 3b 0a 20 20 20 20 70  Name==0 );.    p
1fce9 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e  Tab = pParse->pN
1fcea 65 77 54 61 62 6c 65 3b 0a 20 20 20 20 69 66 28  ewTable;.    if(
1fceb 20 21 70 54 61 62 20 29 20 67 6f 74 6f 20 65 78   !pTab ) goto ex
1fcec 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
1fced 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
1fcee 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
1fcef 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d  db, pTab->pSchem
1fcf0 61 29 3b 0a 20 20 7d 0a 20 20 70 44 62 20 3d 20  a);.  }.  pDb = 
1fcf1 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a  &db->aDb[iDb];..
1fcf2 20 20 69 66 28 20 70 54 61 62 3d 3d 30 20 7c 7c    if( pTab==0 ||
1fcf3 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20   pParse->nErr ) 
1fcf4 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65  goto exit_create
1fcf5 5f 69 6e 64 65 78 3b 0a 20 20 69 66 28 20 73 71  _index;.  if( sq
1fcf6 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 70 54  lite3StrNICmp(pT
1fcf7 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71 6c 69  ab->zName, "sqli
1fcf8 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20  te_", 7)==0 ){. 
1fcf9 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1fcfa 73 67 28 70 50 61 72 73 65 2c 20 22 74 61 62 6c  sg(pParse, "tabl
1fcfb 65 20 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20  e %s may not be 
1fcfc 69 6e 64 65 78 65 64 22 2c 20 70 54 61 62 2d 3e  indexed", pTab->
1fcfd 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 67 6f 74 6f  zName);.    goto
1fcfe 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1fcff 65 78 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20  ex;.  }.#ifndef 
1fd00 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
1fd01 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70 53 65  .  if( pTab->pSe
1fd02 6c 65 63 74 20 29 7b 0a 20 20 20 20 73 71 6c 69  lect ){.    sqli
1fd03 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
1fd04 73 65 2c 20 22 76 69 65 77 73 20 6d 61 79 20 6e  se, "views may n
1fd05 6f 74 20 62 65 20 69 6e 64 65 78 65 64 22 29 3b  ot be indexed");
1fd06 0a 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f 63  .    goto exit_c
1fd07 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d  reate_index;.  }
1fd08 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20  .#endif.#ifndef 
1fd09 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
1fd0a 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 49  UALTABLE.  if( I
1fd0b 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
1fd0c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
1fd0d 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 76  orMsg(pParse, "v
1fd0e 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d 61  irtual tables ma
1fd0f 79 20 6e 6f 74 20 62 65 20 69 6e 64 65 78 65 64  y not be indexed
1fd10 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65 78 69  ");.    goto exi
1fd11 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a  t_create_index;.
1fd12 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
1fd13 0a 20 20 2a 2a 20 46 69 6e 64 20 74 68 65 20 6e  .  ** Find the n
1fd14 61 6d 65 20 6f 66 20 74 68 65 20 69 6e 64 65 78  ame of the index
1fd15 2e 20 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  .  Make sure the
1fd16 72 65 20 69 73 20 6e 6f 74 20 61 6c 72 65 61 64  re is not alread
1fd17 79 20 61 6e 6f 74 68 65 72 0a 20 20 2a 2a 20 69  y another.  ** i
1fd18 6e 64 65 78 20 6f 72 20 74 61 62 6c 65 20 77 69  ndex or table wi
1fd19 74 68 20 74 68 65 20 73 61 6d 65 20 6e 61 6d 65  th the same name
1fd1a 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45 78  .  .  **.  ** Ex
1fd1b 63 65 70 74 69 6f 6e 3a 20 20 49 66 20 77 65 20  ception:  If we 
1fd1c 61 72 65 20 72 65 61 64 69 6e 67 20 74 68 65 20  are reading the 
1fd1d 6e 61 6d 65 73 20 6f 66 20 70 65 72 6d 61 6e 65  names of permane
1fd1e 6e 74 20 69 6e 64 69 63 65 73 20 66 72 6f 6d 20  nt indices from 
1fd1f 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f  the.  ** sqlite_
1fd20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 28 62 65  master table (be
1fd21 63 61 75 73 65 20 73 6f 6d 65 20 6f 74 68 65 72  cause some other
1fd22 20 70 72 6f 63 65 73 73 20 63 68 61 6e 67 65 64   process changed
1fd23 20 74 68 65 20 73 63 68 65 6d 61 29 20 61 6e 64   the schema) and
1fd24 0a 20 20 2a 2a 20 6f 6e 65 20 6f 66 20 74 68 65  .  ** one of the
1fd25 20 69 6e 64 65 78 20 6e 61 6d 65 73 20 63 6f 6c   index names col
1fd26 6c 69 64 65 73 20 77 69 74 68 20 74 68 65 20 6e  lides with the n
1fd27 61 6d 65 20 6f 66 20 61 20 74 65 6d 70 6f 72 61  ame of a tempora
1fd28 72 79 20 74 61 62 6c 65 20 6f 72 0a 20 20 2a 2a  ry table or.  **
1fd29 20 69 6e 64 65 78 2c 20 74 68 65 6e 20 77 65 20   index, then we 
1fd2a 77 69 6c 6c 20 63 6f 6e 74 69 6e 75 65 20 74 6f  will continue to
1fd2b 20 70 72 6f 63 65 73 73 20 74 68 69 73 20 69 6e   process this in
1fd2c 64 65 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49  dex..  **.  ** I
1fd2d 66 20 70 4e 61 6d 65 3d 3d 30 20 69 74 20 6d 65  f pName==0 it me
1fd2e 61 6e 73 20 74 68 61 74 20 77 65 20 61 72 65 0a  ans that we are.
1fd2f 20 20 2a 2a 20 64 65 61 6c 69 6e 67 20 77 69 74    ** dealing wit
1fd30 68 20 61 20 70 72 69 6d 61 72 79 20 6b 65 79 20  h a primary key 
1fd31 6f 72 20 55 4e 49 51 55 45 20 63 6f 6e 73 74 72  or UNIQUE constr
1fd32 61 69 6e 74 2e 20 20 57 65 20 68 61 76 65 20 74  aint.  We have t
1fd33 6f 20 69 6e 76 65 6e 74 20 6f 75 72 0a 20 20 2a  o invent our.  *
1fd34 2a 20 6f 77 6e 20 6e 61 6d 65 2e 0a 20 20 2a 2f  * own name..  */
1fd35 0a 20 20 69 66 28 20 70 4e 61 6d 65 20 29 7b 0a  .  if( pName ){.
1fd36 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69      zName = sqli
1fd37 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e  te3NameFromToken
1fd38 28 64 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 20  (db, pName);.   
1fd39 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
1fd3a 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d  sqlite3ReadSchem
1fd3b 61 28 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f  a(pParse) ) goto
1fd3c 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1fd3d 65 78 3b 0a 20 20 20 20 69 66 28 20 7a 4e 61 6d  ex;.    if( zNam
1fd3e 65 3d 3d 30 20 29 20 67 6f 74 6f 20 65 78 69 74  e==0 ) goto exit
1fd3f 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
1fd40 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b     if( SQLITE_OK
1fd41 21 3d 73 71 6c 69 74 65 33 43 68 65 63 6b 4f 62  !=sqlite3CheckOb
1fd42 6a 65 63 74 4e 61 6d 65 28 70 50 61 72 73 65 2c  jectName(pParse,
1fd43 20 7a 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20   zName) ){.     
1fd44 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
1fd45 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
1fd46 20 20 20 69 66 28 20 21 64 62 2d 3e 69 6e 69 74     if( !db->init
1fd47 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 20 20 69  .busy ){.      i
1fd48 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 73 71  f( SQLITE_OK!=sq
1fd49 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
1fd4a 70 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 65  pParse) ) goto e
1fd4b 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78  xit_create_index
1fd4c 3b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  ;.      if( sqli
1fd4d 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62 2c  te3FindTable(db,
1fd4e 20 7a 4e 61 6d 65 2c 20 30 29 21 3d 30 20 29 7b   zName, 0)!=0 ){
1fd4f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
1fd50 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
1fd51 20 22 74 68 65 72 65 20 69 73 20 61 6c 72 65 61   "there is alrea
1fd52 64 79 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 64  dy a table named
1fd53 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20   %s", zName);.  
1fd54 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
1fd55 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
1fd56 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
1fd57 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e 64 49  if( sqlite3FindI
1fd58 6e 64 65 78 28 64 62 2c 20 7a 4e 61 6d 65 2c 20  ndex(db, zName, 
1fd59 70 44 62 2d 3e 7a 4e 61 6d 65 29 21 3d 30 20 29  pDb->zName)!=0 )
1fd5a 7b 0a 20 20 20 20 20 20 69 66 28 20 21 69 66 4e  {.      if( !ifN
1fd5b 6f 74 45 78 69 73 74 20 29 7b 0a 20 20 20 20 20  otExist ){.     
1fd5c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
1fd5d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e 64 65  sg(pParse, "inde
1fd5e 78 20 25 73 20 61 6c 72 65 61 64 79 20 65 78 69  x %s already exi
1fd5f 73 74 73 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20  sts", zName);.  
1fd60 20 20 20 20 7d 0a 20 20 20 20 20 20 67 6f 74 6f      }.      goto
1fd61 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1fd62 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  ex;.    }.  }els
1fd63 65 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20  e{.    int n;.  
1fd64 20 20 49 6e 64 65 78 20 2a 70 4c 6f 6f 70 3b 0a    Index *pLoop;.
1fd65 20 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 54      for(pLoop=pT
1fd66 61 62 2d 3e 70 49 6e 64 65 78 2c 20 6e 3d 31 3b  ab->pIndex, n=1;
1fd67 20 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c   pLoop; pLoop=pL
1fd68 6f 6f 70 2d 3e 70 4e 65 78 74 2c 20 6e 2b 2b 29  oop->pNext, n++)
1fd69 7b 7d 0a 20 20 20 20 7a 4e 61 6d 65 20 3d 20 73  {}.    zName = s
1fd6a 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
1fd6b 2c 20 22 73 71 6c 69 74 65 5f 61 75 74 6f 69 6e  , "sqlite_autoin
1fd6c 64 65 78 5f 25 73 5f 25 64 22 2c 20 70 54 61 62  dex_%s_%d", pTab
1fd6d 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a 20 20 20  ->zName, n);.   
1fd6e 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20 29 7b   if( zName==0 ){
1fd6f 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74  .      goto exit
1fd70 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20  _create_index;. 
1fd71 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43     }.  }..  /* C
1fd72 68 65 63 6b 20 66 6f 72 20 61 75 74 68 6f 72 69  heck for authori
1fd73 7a 61 74 69 6f 6e 20 74 6f 20 63 72 65 61 74 65  zation to create
1fd74 20 61 6e 20 69 6e 64 65 78 2e 0a 20 20 2a 2f 0a   an index..  */.
1fd75 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
1fd76 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
1fd77 4e 0a 20 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20  N.  {.    const 
1fd78 63 68 61 72 20 2a 7a 44 62 20 3d 20 70 44 62 2d  char *zDb = pDb-
1fd79 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69 66 28 20  >zName;.    if( 
1fd7a 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
1fd7b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
1fd7c 49 4e 53 45 52 54 2c 20 53 43 48 45 4d 41 5f 54  INSERT, SCHEMA_T
1fd7d 41 42 4c 45 28 69 44 62 29 2c 20 30 2c 20 7a 44  ABLE(iDb), 0, zD
1fd7e 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
1fd7f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1fd80 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 20  ex;.    }.    i 
1fd81 3d 20 53 51 4c 49 54 45 5f 43 52 45 41 54 45 5f  = SQLITE_CREATE_
1fd82 49 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 21  INDEX;.    if( !
1fd83 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20 69  OMIT_TEMPDB && i
1fd84 44 62 3d 3d 31 20 29 20 69 20 3d 20 53 51 4c 49  Db==1 ) i = SQLI
1fd85 54 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 49  TE_CREATE_TEMP_I
1fd86 4e 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71  NDEX;.    if( sq
1fd87 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
1fd88 50 61 72 73 65 2c 20 69 2c 20 7a 4e 61 6d 65 2c  Parse, i, zName,
1fd89 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 44   pTab->zName, zD
1fd8a 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
1fd8b 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69 6e 64   exit_create_ind
1fd8c 65 78 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65  ex;.    }.  }.#e
1fd8d 6e 64 69 66 0a 0a 20 20 2f 2a 20 49 66 20 70 4c  ndif..  /* If pL
1fd8e 69 73 74 3d 3d 30 2c 20 69 74 20 6d 65 61 6e 73  ist==0, it means
1fd8f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 77 61   this routine wa
1fd90 73 20 63 61 6c 6c 65 64 20 74 6f 20 6d 61 6b 65  s called to make
1fd91 20 61 20 70 72 69 6d 61 72 79 0a 20 20 2a 2a 20   a primary.  ** 
1fd92 6b 65 79 20 6f 75 74 20 6f 66 20 74 68 65 20 6c  key out of the l
1fd93 61 73 74 20 63 6f 6c 75 6d 6e 20 61 64 64 65 64  ast column added
1fd94 20 74 6f 20 74 68 65 20 74 61 62 6c 65 20 75 6e   to the table un
1fd95 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
1fd96 2e 0a 20 20 2a 2a 20 53 6f 20 63 72 65 61 74 65  ..  ** So create
1fd97 20 61 20 66 61 6b 65 20 6c 69 73 74 20 74 6f 20   a fake list to 
1fd98 73 69 6d 75 6c 61 74 65 20 74 68 69 73 2e 0a 20  simulate this.. 
1fd99 20 2a 2f 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   */.  if( pList=
1fd9a 3d 30 20 29 7b 0a 20 20 20 20 6e 75 6c 6c 49 64  =0 ){.    nullId
1fd9b 2e 7a 20 3d 20 28 75 38 2a 29 70 54 61 62 2d 3e  .z = (u8*)pTab->
1fd9c 61 43 6f 6c 5b 70 54 61 62 2d 3e 6e 43 6f 6c 2d  aCol[pTab->nCol-
1fd9d 31 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 75  1].zName;.    nu
1fd9e 6c 6c 49 64 2e 6e 20 3d 20 73 74 72 6c 65 6e 28  llId.n = strlen(
1fd9f 28 63 68 61 72 2a 29 6e 75 6c 6c 49 64 2e 7a 29  (char*)nullId.z)
1fda0 3b 0a 20 20 20 20 70 4c 69 73 74 20 3d 20 73 71  ;.    pList = sq
1fda1 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
1fda2 65 6e 64 28 70 50 61 72 73 65 2c 20 30 2c 20 30  end(pParse, 0, 0
1fda3 2c 20 26 6e 75 6c 6c 49 64 29 3b 0a 20 20 20 20  , &nullId);.    
1fda4 69 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 67  if( pList==0 ) g
1fda5 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f  oto exit_create_
1fda6 69 6e 64 65 78 3b 0a 20 20 20 20 70 4c 69 73 74  index;.    pList
1fda7 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64 65 72  ->a[0].sortOrder
1fda8 20 3d 20 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20   = sortOrder;.  
1fda9 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f  }..  /* Figure o
1fdaa 75 74 20 68 6f 77 20 6d 61 6e 79 20 62 79 74 65  ut how many byte
1fdab 73 20 6f 66 20 73 70 61 63 65 20 61 72 65 20 72  s of space are r
1fdac 65 71 75 69 72 65 64 20 74 6f 20 73 74 6f 72 65  equired to store
1fdad 20 65 78 70 6c 69 63 69 74 6c 79 0a 20 20 2a 2a   explicitly.  **
1fdae 20 73 70 65 63 69 66 69 65 64 20 63 6f 6c 6c 61   specified colla
1fdaf 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
1fdb0 6d 65 73 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28  mes..  */.  for(
1fdb1 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e 45  i=0; i<pList->nE
1fdb2 78 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 45  xpr; i++){.    E
1fdb3 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20  xpr *pExpr;.    
1fdb4 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a  CollSeq *pColl;.
1fdb5 20 20 20 20 69 66 28 20 28 70 45 78 70 72 20 3d      if( (pExpr =
1fdb6 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78   pList->a[i].pEx
1fdb7 70 72 29 21 3d 30 20 26 26 20 28 70 43 6f 6c 6c  pr)!=0 && (pColl
1fdb8 20 3d 20 70 45 78 70 72 2d 3e 70 43 6f 6c 6c 29   = pExpr->pColl)
1fdb9 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 6e 45 78  !=0 ){.      nEx
1fdba 74 72 61 20 2b 3d 20 28 31 20 2b 20 73 74 72 6c  tra += (1 + strl
1fdbb 65 6e 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 29  en(pColl->zName)
1fdbc 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
1fdbd 2f 2a 20 0a 20 20 2a 2a 20 41 6c 6c 6f 63 61 74  /* .  ** Allocat
1fdbe 65 20 74 68 65 20 69 6e 64 65 78 20 73 74 72 75  e the index stru
1fdbf 63 74 75 72 65 2e 20 0a 20 20 2a 2f 0a 20 20 6e  cture. .  */.  n
1fdc0 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e  Name = strlen(zN
1fdc1 61 6d 65 29 3b 0a 20 20 6e 43 6f 6c 20 3d 20 70  ame);.  nCol = p
1fdc2 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70  List->nExpr;.  p
1fdc3 49 6e 64 65 78 20 3d 20 73 71 6c 69 74 65 33 44  Index = sqlite3D
1fdc4 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
1fdc5 0a 20 20 20 20 20 20 73 69 7a 65 6f 66 28 49 6e  .      sizeof(In
1fdc6 64 65 78 29 20 2b 20 20 20 20 20 20 20 20 20 20  dex) +          
1fdc7 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 73 74 72      /* Index str
1fdc8 75 63 74 75 72 65 20 20 2a 2f 0a 20 20 20 20 20  ucture  */.     
1fdc9 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 6f   sizeof(int)*nCo
1fdca 6c 20 2b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l +           /*
1fdcb 20 49 6e 64 65 78 2e 61 69 43 6f 6c 75 6d 6e 20   Index.aiColumn 
1fdcc 20 20 2a 2f 0a 20 20 20 20 20 20 73 69 7a 65 6f    */.      sizeo
1fdcd 66 28 69 6e 74 29 2a 28 6e 43 6f 6c 2b 31 29 20  f(int)*(nCol+1) 
1fdce 2b 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78  +       /* Index
1fdcf 2e 61 69 52 6f 77 45 73 74 20 20 20 2a 2f 0a 20  .aiRowEst   */. 
1fdd0 20 20 20 20 20 73 69 7a 65 6f 66 28 63 68 61 72       sizeof(char
1fdd1 20 2a 29 2a 6e 43 6f 6c 20 2b 20 20 20 20 20 20   *)*nCol +      
1fdd2 20 20 2f 2a 20 49 6e 64 65 78 2e 61 7a 43 6f 6c    /* Index.azCol
1fdd3 6c 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73  l     */.      s
1fdd4 69 7a 65 6f 66 28 75 38 29 2a 6e 43 6f 6c 20 2b  izeof(u8)*nCol +
1fdd5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49              /* I
1fdd6 6e 64 65 78 2e 61 53 6f 72 74 4f 72 64 65 72 20  ndex.aSortOrder 
1fdd7 2a 2f 0a 20 20 20 20 20 20 6e 4e 61 6d 65 20 2b  */.      nName +
1fdd8 20 31 20 2b 20 20 20 20 20 20 20 20 20 20 20 20   1 +            
1fdd9 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 2e 7a        /* Index.z
1fdda 4e 61 6d 65 20 20 20 20 20 20 2a 2f 0a 20 20 20  Name      */.   
1fddb 20 20 20 6e 45 78 74 72 61 20 20 20 20 20 20 20     nExtra       
1fddc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
1fddd 2f 2a 20 43 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  /* Collation seq
1fdde 75 65 6e 63 65 20 6e 61 6d 65 73 20 2a 2f 0a 20  uence names */. 
1fddf 20 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   );.  if( db->ma
1fde0 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
1fde1 20 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61    goto exit_crea
1fde2 74 65 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20  te_index;.  }.  
1fde3 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 20 3d  pIndex->azColl =
1fde4 20 28 63 68 61 72 2a 2a 29 28 26 70 49 6e 64 65   (char**)(&pInde
1fde5 78 5b 31 5d 29 3b 0a 20 20 70 49 6e 64 65 78 2d  x[1]);.  pIndex-
1fde6 3e 61 69 43 6f 6c 75 6d 6e 20 3d 20 28 69 6e 74  >aiColumn = (int
1fde7 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61 7a 43   *)(&pIndex->azC
1fde8 6f 6c 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49  oll[nCol]);.  pI
1fde9 6e 64 65 78 2d 3e 61 69 52 6f 77 45 73 74 20 3d  ndex->aiRowEst =
1fdea 20 28 75 6e 73 69 67 6e 65 64 20 2a 29 28 26 70   (unsigned *)(&p
1fdeb 49 6e 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b  Index->aiColumn[
1fdec 6e 43 6f 6c 5d 29 3b 0a 20 20 70 49 6e 64 65 78  nCol]);.  pIndex
1fded 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28  ->aSortOrder = (
1fdee 75 38 20 2a 29 28 26 70 49 6e 64 65 78 2d 3e 61  u8 *)(&pIndex->a
1fdef 69 52 6f 77 45 73 74 5b 6e 43 6f 6c 2b 31 5d 29  iRowEst[nCol+1])
1fdf0 3b 0a 20 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d  ;.  pIndex->zNam
1fdf1 65 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49  e = (char *)(&pI
1fdf2 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72  ndex->aSortOrder
1fdf3 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 7a 45 78 74 72  [nCol]);.  zExtr
1fdf4 61 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 49  a = (char *)(&pI
1fdf5 6e 64 65 78 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d  ndex->zName[nNam
1fdf6 65 2b 31 5d 29 3b 0a 20 20 6d 65 6d 63 70 79 28  e+1]);.  memcpy(
1fdf7 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 7a  pIndex->zName, z
1fdf8 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29 3b 0a  Name, nName+1);.
1fdf9 20 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65    pIndex->pTable
1fdfa 20 3d 20 70 54 61 62 3b 0a 20 20 70 49 6e 64 65   = pTab;.  pInde
1fdfb 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c 69  x->nColumn = pLi
1fdfc 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e  st->nExpr;.  pIn
1fdfd 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 3d 20 6f  dex->onError = o
1fdfe 6e 45 72 72 6f 72 3b 0a 20 20 70 49 6e 64 65 78  nError;.  pIndex
1fdff 2d 3e 61 75 74 6f 49 6e 64 65 78 20 3d 20 70 4e  ->autoIndex = pN
1fe00 61 6d 65 3d 3d 30 3b 0a 20 20 70 49 6e 64 65 78  ame==0;.  pIndex
1fe01 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62 2d 3e  ->pSchema = db->
1fe02 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61  aDb[iDb].pSchema
1fe03 3b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  ;..  /* Check to
1fe04 20 73 65 65 20 69 66 20 77 65 20 73 68 6f 75 6c   see if we shoul
1fe05 64 20 68 6f 6e 6f 72 20 44 45 53 43 20 72 65 71  d honor DESC req
1fe06 75 65 73 74 73 20 6f 6e 20 69 6e 64 65 78 20 63  uests on index c
1fe07 6f 6c 75 6d 6e 73 0a 20 20 2a 2f 0a 20 20 69 66  olumns.  */.  if
1fe08 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e  ( pDb->pSchema->
1fe09 66 69 6c 65 5f 66 6f 72 6d 61 74 3e 3d 34 20 29  file_format>=4 )
1fe0a 7b 0a 20 20 20 20 73 6f 72 74 4f 72 64 65 72 4d  {.    sortOrderM
1fe0b 61 73 6b 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 48  ask = -1;   /* H
1fe0c 6f 6e 6f 72 20 44 45 53 43 20 2a 2f 0a 20 20 7d  onor DESC */.  }
1fe0d 65 6c 73 65 7b 0a 20 20 20 20 73 6f 72 74 4f 72  else{.    sortOr
1fe0e 64 65 72 4d 61 73 6b 20 3d 20 30 3b 20 20 20 20  derMask = 0;    
1fe0f 2f 2a 20 49 67 6e 6f 72 65 20 44 45 53 43 20 2a  /* Ignore DESC *
1fe10 2f 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 63 61 6e  /.  }..  /* Scan
1fe11 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 20 74 68   the names of th
1fe12 65 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65  e columns of the
1fe13 20 74 61 62 6c 65 20 74 6f 20 62 65 20 69 6e 64   table to be ind
1fe14 65 78 65 64 20 61 6e 64 0a 20 20 2a 2a 20 6c 6f  exed and.  ** lo
1fe15 61 64 20 74 68 65 20 63 6f 6c 75 6d 6e 20 69 6e  ad the column in
1fe16 64 69 63 65 73 20 69 6e 74 6f 20 74 68 65 20 49  dices into the I
1fe17 6e 64 65 78 20 73 74 72 75 63 74 75 72 65 2e 20  ndex structure. 
1fe18 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
1fe19 0a 20 20 2a 2a 20 69 66 20 61 6e 79 20 63 6f 6c  .  ** if any col
1fe1a 75 6d 6e 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64  umn is not found
1fe1b 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30  ..  */.  for(i=0
1fe1c 2c 20 70 4c 69 73 74 49 74 65 6d 3d 70 4c 69 73  , pListItem=pLis
1fe1d 74 2d 3e 61 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  t->a; i<pList->n
1fe1e 45 78 70 72 3b 20 69 2b 2b 2c 20 70 4c 69 73 74  Expr; i++, pList
1fe1f 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e  Item++){.    con
1fe20 73 74 20 63 68 61 72 20 2a 7a 43 6f 6c 4e 61 6d  st char *zColNam
1fe21 65 20 3d 20 70 4c 69 73 74 49 74 65 6d 2d 3e 7a  e = pListItem->z
1fe22 4e 61 6d 65 3b 0a 20 20 20 20 43 6f 6c 75 6d 6e  Name;.    Column
1fe23 20 2a 70 54 61 62 43 6f 6c 3b 0a 20 20 20 20 69   *pTabCol;.    i
1fe24 6e 74 20 72 65 71 75 65 73 74 65 64 53 6f 72 74  nt requestedSort
1fe25 4f 72 64 65 72 3b 0a 20 20 20 20 63 68 61 72 20  Order;.    char 
1fe26 2a 7a 43 6f 6c 6c 3b 20 20 20 20 20 20 20 20 20  *zColl;         
1fe27 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
1fe28 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
1fe29 6e 61 6d 65 20 2a 2f 0a 0a 20 20 20 20 66 6f 72  name */..    for
1fe2a 28 6a 3d 30 2c 20 70 54 61 62 43 6f 6c 3d 70 54  (j=0, pTabCol=pT
1fe2b 61 62 2d 3e 61 43 6f 6c 3b 20 6a 3c 70 54 61 62  ab->aCol; j<pTab
1fe2c 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 2c 20 70 54 61  ->nCol; j++, pTa
1fe2d 62 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 20 20 69  bCol++){.      i
1fe2e 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
1fe2f 70 28 7a 43 6f 6c 4e 61 6d 65 2c 20 70 54 61 62  p(zColName, pTab
1fe30 43 6f 6c 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20 29  Col->zName)==0 )
1fe31 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
1fe32 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e    if( j>=pTab->n
1fe33 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Col ){.      sql
1fe34 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
1fe35 72 73 65 2c 20 22 74 61 62 6c 65 20 25 73 20 68  rse, "table %s h
1fe36 61 73 20 6e 6f 20 63 6f 6c 75 6d 6e 20 6e 61 6d  as no column nam
1fe37 65 64 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20  ed %s",.        
1fe38 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 7a 43 6f  pTab->zName, zCo
1fe39 6c 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 67 6f  lName);.      go
1fe3a 74 6f 20 65 78 69 74 5f 63 72 65 61 74 65 5f 69  to exit_create_i
1fe3b 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20 20 20 20  ndex;.    }.    
1fe3c 2f 2a 20 54 4f 44 4f 3a 20 20 41 64 64 20 61 20  /* TODO:  Add a 
1fe3d 74 65 73 74 20 74 6f 20 6d 61 6b 65 20 73 75 72  test to make sur
1fe3e 65 20 74 68 61 74 20 74 68 65 20 73 61 6d 65 20  e that the same 
1fe3f 63 6f 6c 75 6d 6e 20 69 73 20 6e 6f 74 20 6e 61  column is not na
1fe40 6d 65 64 0a 20 20 20 20 2a 2a 20 6d 6f 72 65 20  med.    ** more 
1fe41 74 68 61 6e 20 6f 6e 63 65 20 77 69 74 68 69 6e  than once within
1fe42 20 74 68 65 20 73 61 6d 65 20 69 6e 64 65 78 2e   the same index.
1fe43 20 20 4f 6e 6c 79 20 74 68 65 20 66 69 72 73 74    Only the first
1fe44 20 69 6e 73 74 61 6e 63 65 20 6f 66 0a 20 20 20   instance of.   
1fe45 20 2a 2a 20 74 68 65 20 63 6f 6c 75 6d 6e 20 77   ** the column w
1fe46 69 6c 6c 20 65 76 65 72 20 62 65 20 75 73 65 64  ill ever be used
1fe47 20 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65   by the optimize
1fe48 72 2e 20 20 4e 6f 74 65 20 74 68 61 74 20 75 73  r.  Note that us
1fe49 69 6e 67 20 74 68 65 0a 20 20 20 20 2a 2a 20 73  ing the.    ** s
1fe4a 61 6d 65 20 63 6f 6c 75 6d 6e 20 6d 6f 72 65 20  ame column more 
1fe4b 74 68 61 6e 20 6f 6e 63 65 20 63 61 6e 6e 6f 74  than once cannot
1fe4c 20 62 65 20 61 6e 20 65 72 72 6f 72 20 62 65 63   be an error bec
1fe4d 61 75 73 65 20 74 68 61 74 20 77 6f 75 6c 64 20  ause that would 
1fe4e 0a 20 20 20 20 2a 2a 20 62 72 65 61 6b 20 62 61  .    ** break ba
1fe4f 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74 69 62  ckwards compatib
1fe50 69 6c 69 74 79 20 2d 20 69 74 20 6e 65 65 64 73  ility - it needs
1fe51 20 74 6f 20 62 65 20 61 20 77 61 72 6e 69 6e 67   to be a warning
1fe52 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 49 6e  ..    */.    pIn
1fe53 64 65 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d  dex->aiColumn[i]
1fe54 20 3d 20 6a 3b 0a 20 20 20 20 69 66 28 20 70 4c   = j;.    if( pL
1fe55 69 73 74 49 74 65 6d 2d 3e 70 45 78 70 72 20 26  istItem->pExpr &
1fe56 26 20 70 4c 69 73 74 49 74 65 6d 2d 3e 70 45 78  & pListItem->pEx
1fe57 70 72 2d 3e 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  pr->pColl ){.   
1fe58 20 20 20 61 73 73 65 72 74 28 20 70 4c 69 73 74     assert( pList
1fe59 49 74 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f  Item->pExpr->pCo
1fe5a 6c 6c 20 29 3b 0a 20 20 20 20 20 20 7a 43 6f 6c  ll );.      zCol
1fe5b 6c 20 3d 20 7a 45 78 74 72 61 3b 0a 20 20 20 20  l = zExtra;.    
1fe5c 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
1fe5d 74 66 28 6e 45 78 74 72 61 2c 20 7a 45 78 74 72  tf(nExtra, zExtr
1fe5e 61 2c 20 22 25 73 22 2c 20 70 4c 69 73 74 49 74  a, "%s", pListIt
1fe5f 65 6d 2d 3e 70 45 78 70 72 2d 3e 70 43 6f 6c 6c  em->pExpr->pColl
1fe60 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
1fe61 7a 45 78 74 72 61 20 2b 3d 20 28 73 74 72 6c 65  zExtra += (strle
1fe62 6e 28 7a 43 6f 6c 6c 29 20 2b 20 31 29 3b 0a 20  n(zColl) + 1);. 
1fe63 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1fe64 7a 43 6f 6c 6c 20 3d 20 70 54 61 62 2d 3e 61 43  zColl = pTab->aC
1fe65 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 6c 3b 0a 20 20 20  ol[j].zColl;.   
1fe66 20 20 20 69 66 28 20 21 7a 43 6f 6c 6c 20 29 7b     if( !zColl ){
1fe67 0a 20 20 20 20 20 20 20 20 7a 43 6f 6c 6c 20 3d  .        zColl =
1fe68 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 2d 3e   db->pDfltColl->
1fe69 7a 4e 61 6d 65 3b 0a 20 20 20 20 20 20 7d 0a 20  zName;.      }. 
1fe6a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 21 64 62     }.    if( !db
1fe6b 2d 3e 69 6e 69 74 2e 62 75 73 79 20 26 26 20 21  ->init.busy && !
1fe6c 73 71 6c 69 74 65 33 4c 6f 63 61 74 65 43 6f 6c  sqlite3LocateCol
1fe6d 6c 53 65 71 28 70 50 61 72 73 65 2c 20 7a 43 6f  lSeq(pParse, zCo
1fe6e 6c 6c 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20  ll, -1) ){.     
1fe6f 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61 74   goto exit_creat
1fe70 65 5f 69 6e 64 65 78 3b 0a 20 20 20 20 7d 0a 20  e_index;.    }. 
1fe71 20 20 20 70 49 6e 64 65 78 2d 3e 61 7a 43 6f 6c     pIndex->azCol
1fe72 6c 5b 69 5d 20 3d 20 7a 43 6f 6c 6c 3b 0a 20 20  l[i] = zColl;.  
1fe73 20 20 72 65 71 75 65 73 74 65 64 53 6f 72 74 4f    requestedSortO
1fe74 72 64 65 72 20 3d 20 70 4c 69 73 74 49 74 65 6d  rder = pListItem
1fe75 2d 3e 73 6f 72 74 4f 72 64 65 72 20 26 20 73 6f  ->sortOrder & so
1fe76 72 74 4f 72 64 65 72 4d 61 73 6b 3b 0a 20 20 20  rtOrderMask;.   
1fe77 20 70 49 6e 64 65 78 2d 3e 61 53 6f 72 74 4f 72   pIndex->aSortOr
1fe78 64 65 72 5b 69 5d 20 3d 20 72 65 71 75 65 73 74  der[i] = request
1fe79 65 64 53 6f 72 74 4f 72 64 65 72 3b 0a 20 20 7d  edSortOrder;.  }
1fe7a 0a 20 20 73 71 6c 69 74 65 33 44 65 66 61 75 6c  .  sqlite3Defaul
1fe7b 74 52 6f 77 45 73 74 28 70 49 6e 64 65 78 29 3b  tRowEst(pIndex);
1fe7c 0a 0a 20 20 69 66 28 20 70 54 61 62 3d 3d 70 50  ..  if( pTab==pP
1fe7d 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 20  arse->pNewTable 
1fe7e 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 72  ){.    /* This r
1fe7f 6f 75 74 69 6e 65 20 68 61 73 20 62 65 65 6e 20  outine has been 
1fe80 63 61 6c 6c 65 64 20 74 6f 20 63 72 65 61 74 65  called to create
1fe81 20 61 6e 20 61 75 74 6f 6d 61 74 69 63 20 69 6e   an automatic in
1fe82 64 65 78 20 61 73 20 61 0a 20 20 20 20 2a 2a 20  dex as a.    ** 
1fe83 72 65 73 75 6c 74 20 6f 66 20 61 20 50 52 49 4d  result of a PRIM
1fe84 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
1fe85 45 20 63 6c 61 75 73 65 20 6f 6e 20 61 20 63 6f  E clause on a co
1fe86 6c 75 6d 6e 20 64 65 66 69 6e 69 74 69 6f 6e 2c  lumn definition,
1fe87 20 6f 72 0a 20 20 20 20 2a 2a 20 61 20 50 52 49   or.    ** a PRI
1fe88 4d 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51  MARY KEY or UNIQ
1fe89 55 45 20 63 6c 61 75 73 65 20 66 6f 6c 6c 6f 77  UE clause follow
1fe8a 69 6e 67 20 74 68 65 20 63 6f 6c 75 6d 6e 20 64  ing the column d
1fe8b 65 66 69 6e 69 74 69 6f 6e 73 2e 0a 20 20 20 20  efinitions..    
1fe8c 2a 2a 20 69 2e 65 2e 20 6f 6e 65 20 6f 66 3a 0a  ** i.e. one of:.
1fe8d 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 43 52      **.    ** CR
1fe8e 45 41 54 45 20 54 41 42 4c 45 20 74 28 78 20 50  EATE TABLE t(x P
1fe8f 52 49 4d 41 52 59 20 4b 45 59 2c 20 79 29 3b 0a  RIMARY KEY, y);.
1fe90 20 20 20 20 2a 2a 20 43 52 45 41 54 45 20 54 41      ** CREATE TA
1fe91 42 4c 45 20 74 28 78 2c 20 79 2c 20 55 4e 49 51  BLE t(x, y, UNIQ
1fe92 55 45 28 78 2c 20 79 29 29 3b 0a 20 20 20 20 2a  UE(x, y));.    *
1fe93 2a 0a 20 20 20 20 2a 2a 20 45 69 74 68 65 72 20  *.    ** Either 
1fe94 77 61 79 2c 20 63 68 65 63 6b 20 74 6f 20 73 65  way, check to se
1fe95 65 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 61  e if the table a
1fe96 6c 72 65 61 64 79 20 68 61 73 20 73 75 63 68 20  lready has such 
1fe97 61 6e 20 69 6e 64 65 78 2e 20 49 66 0a 20 20 20  an index. If.   
1fe98 20 2a 2a 20 73 6f 2c 20 64 6f 6e 27 74 20 62 6f   ** so, don't bo
1fe99 74 68 65 72 20 63 72 65 61 74 69 6e 67 20 74 68  ther creating th
1fe9a 69 73 20 6f 6e 65 2e 20 54 68 69 73 20 6f 6e 6c  is one. This onl
1fe9b 79 20 61 70 70 6c 69 65 73 20 74 6f 0a 20 20 20  y applies to.   
1fe9c 20 2a 2a 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c   ** automaticall
1fe9d 79 20 63 72 65 61 74 65 64 20 69 6e 64 69 63 65  y created indice
1fe9e 73 2e 20 55 73 65 72 73 20 63 61 6e 20 64 6f 20  s. Users can do 
1fe9f 61 73 20 74 68 65 79 20 77 69 73 68 20 77 69 74  as they wish wit
1fea0 68 0a 20 20 20 20 2a 2a 20 65 78 70 6c 69 63 69  h.    ** explici
1fea1 74 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 2a  t indices..    *
1fea2 2f 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  /.    Index *pId
1fea3 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d  x;.    for(pIdx=
1fea4 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
1fea5 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
1fea6 4e 65 78 74 29 7b 0a 20 20 20 20 20 20 69 6e 74  Next){.      int
1fea7 20 6b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74   k;.      assert
1fea8 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 21  ( pIdx->onError!
1fea9 3d 4f 45 5f 4e 6f 6e 65 20 29 3b 0a 20 20 20 20  =OE_None );.    
1feaa 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d 3e    assert( pIdx->
1feab 61 75 74 6f 49 6e 64 65 78 20 29 3b 0a 20 20 20  autoIndex );.   
1feac 20 20 20 61 73 73 65 72 74 28 20 70 49 6e 64 65     assert( pInde
1fead 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 4e  x->onError!=OE_N
1feae 6f 6e 65 20 29 3b 0a 0a 20 20 20 20 20 20 69 66  one );..      if
1feaf 28 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 21  ( pIdx->nColumn!
1feb0 3d 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75 6d 6e  =pIndex->nColumn
1feb1 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
1feb2 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 49     for(k=0; k<pI
1feb3 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6b 2b 2b  dx->nColumn; k++
1feb4 29 7b 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74  ){.        const
1feb5 20 63 68 61 72 20 2a 7a 31 20 3d 20 70 49 64 78   char *z1 = pIdx
1feb6 2d 3e 61 7a 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20  ->azColl[k];.   
1feb7 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
1feb8 2a 7a 32 20 3d 20 70 49 6e 64 65 78 2d 3e 61 7a  *z2 = pIndex->az
1feb9 43 6f 6c 6c 5b 6b 5d 3b 0a 20 20 20 20 20 20 20  Coll[k];.       
1feba 20 69 66 28 20 70 49 64 78 2d 3e 61 69 43 6f 6c   if( pIdx->aiCol
1febb 75 6d 6e 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e  umn[k]!=pIndex->
1febc 61 69 43 6f 6c 75 6d 6e 5b 6b 5d 20 29 20 62 72  aiColumn[k] ) br
1febd 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  eak;.        if(
1febe 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72 64 65   pIdx->aSortOrde
1febf 72 5b 6b 5d 21 3d 70 49 6e 64 65 78 2d 3e 61 53  r[k]!=pIndex->aS
1fec0 6f 72 74 4f 72 64 65 72 5b 6b 5d 20 29 20 62 72  ortOrder[k] ) br
1fec1 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28  eak;.        if(
1fec2 20 7a 31 21 3d 7a 32 20 26 26 20 73 71 6c 69 74   z1!=z2 && sqlit
1fec3 65 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32  e3StrICmp(z1, z2
1fec4 29 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  ) ) break;.     
1fec5 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6b 3d 3d   }.      if( k==
1fec6 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b  pIdx->nColumn ){
1fec7 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 49 64  .        if( pId
1fec8 78 2d 3e 6f 6e 45 72 72 6f 72 21 3d 70 49 6e 64  x->onError!=pInd
1fec9 65 78 2d 3e 6f 6e 45 72 72 6f 72 20 29 7b 0a 20  ex->onError ){. 
1feca 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73           /* This
1fecb 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 72 65 61   constraint crea
1fecc 74 65 73 20 74 68 65 20 73 61 6d 65 20 69 6e 64  tes the same ind
1fecd 65 78 20 61 73 20 61 20 70 72 65 76 69 6f 75 73  ex as a previous
1fece 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f  .          ** co
1fecf 6e 73 74 72 61 69 6e 74 20 73 70 65 63 69 66 69  nstraint specifi
1fed0 65 64 20 73 6f 6d 65 77 68 65 72 65 20 69 6e 20  ed somewhere in 
1fed1 74 68 65 20 43 52 45 41 54 45 20 54 41 42 4c 45  the CREATE TABLE
1fed2 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20   statement..    
1fed3 20 20 20 20 20 20 2a 2a 20 48 6f 77 65 76 65 72        ** However
1fed4 20 74 68 65 20 4f 4e 20 43 4f 4e 46 4c 49 43 54   the ON CONFLICT
1fed5 20 63 6c 61 75 73 65 73 20 61 72 65 20 64 69 66   clauses are dif
1fed6 66 65 72 65 6e 74 2e 20 49 66 20 62 6f 74 68 20  ferent. If both 
1fed7 74 68 69 73 20 0a 20 20 20 20 20 20 20 20 20 20  this .          
1fed8 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 61 6e  ** constraint an
1fed9 64 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 65  d the previous e
1feda 71 75 69 76 61 6c 65 6e 74 20 63 6f 6e 73 74 72  quivalent constr
1fedb 61 69 6e 74 20 68 61 76 65 20 65 78 70 6c 69 63  aint have explic
1fedc 69 74 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20  it.          ** 
1fedd 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75  ON CONFLICT clau
1fede 73 65 73 20 74 68 69 73 20 69 73 20 61 6e 20 65  ses this is an e
1fedf 72 72 6f 72 2e 20 4f 74 68 65 72 77 69 73 65 2c  rror. Otherwise,
1fee0 20 75 73 65 20 74 68 65 0a 20 20 20 20 20 20 20   use the.       
1fee1 20 20 20 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79     ** explicitly
1fee2 20 73 70 65 63 69 66 69 65 64 20 62 65 68 61 76   specified behav
1fee3 69 6f 75 72 20 66 6f 72 20 74 68 65 20 69 6e 64  iour for the ind
1fee4 65 78 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f  ex..          */
1fee5 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 21  .          if( !
1fee6 28 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d  (pIdx->onError==
1fee7 4f 45 5f 44 65 66 61 75 6c 74 20 7c 7c 20 70 49  OE_Default || pI
1fee8 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  ndex->onError==O
1fee9 45 5f 44 65 66 61 75 6c 74 29 20 29 7b 0a 20 20  E_Default) ){.  
1feea 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
1feeb 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
1feec 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
1feed 20 20 20 22 63 6f 6e 66 6c 69 63 74 69 6e 67 20     "conflicting 
1feee 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 63 6c 61 75  ON CONFLICT clau
1feef 73 65 73 20 73 70 65 63 69 66 69 65 64 22 2c 20  ses specified", 
1fef0 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  0);.          }.
1fef1 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70 49            if( pI
1fef2 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  dx->onError==OE_
1fef3 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
1fef4 20 20 20 20 20 20 20 70 49 64 78 2d 3e 6f 6e 45         pIdx->onE
1fef5 72 72 6f 72 20 3d 20 70 49 6e 64 65 78 2d 3e 6f  rror = pIndex->o
1fef6 6e 45 72 72 6f 72 3b 0a 20 20 20 20 20 20 20 20  nError;.        
1fef7 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
1fef8 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
1fef9 63 72 65 61 74 65 5f 69 6e 64 65 78 3b 0a 20 20  create_index;.  
1fefa 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
1fefb 0a 20 20 2f 2a 20 4c 69 6e 6b 20 74 68 65 20 6e  .  /* Link the n
1fefc 65 77 20 49 6e 64 65 78 20 73 74 72 75 63 74 75  ew Index structu
1fefd 72 65 20 74 6f 20 69 74 73 20 74 61 62 6c 65 20  re to its table 
1fefe 61 6e 64 20 74 6f 20 74 68 65 20 6f 74 68 65 72  and to the other
1feff 0a 20 20 2a 2a 20 69 6e 2d 6d 65 6d 6f 72 79 20  .  ** in-memory 
1ff00 64 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75  database structu
1ff01 72 65 73 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28  res. .  */.  if(
1ff02 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
1ff03 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 3b 0a  {.    Index *p;.
1ff04 20 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 48      p = sqlite3H
1ff05 61 73 68 49 6e 73 65 72 74 28 26 70 49 6e 64 65  ashInsert(&pInde
1ff06 78 2d 3e 70 53 63 68 65 6d 61 2d 3e 69 64 78 48  x->pSchema->idxH
1ff07 61 73 68 2c 20 0a 20 20 20 20 20 20 20 20 20 20  ash, .          
1ff08 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
1ff09 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 2c 20 73 74  Index->zName, st
1ff0a 72 6c 65 6e 28 70 49 6e 64 65 78 2d 3e 7a 4e 61  rlen(pIndex->zNa
1ff0b 6d 65 29 2b 31 2c 20 70 49 6e 64 65 78 29 3b 0a  me)+1, pIndex);.
1ff0c 20 20 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20      if( p ){.   
1ff0d 20 20 20 61 73 73 65 72 74 28 20 70 3d 3d 70 49     assert( p==pI
1ff0e 6e 64 65 78 20 29 3b 20 20 2f 2a 20 4d 61 6c 6c  ndex );  /* Mall
1ff0f 6f 63 20 6d 75 73 74 20 68 61 76 65 20 66 61 69  oc must have fai
1ff10 6c 65 64 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d  led */.      db-
1ff11 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
1ff12 31 3b 0a 20 20 20 20 20 20 67 6f 74 6f 20 65 78  1;.      goto ex
1ff13 69 74 5f 63 72 65 61 74 65 5f 69 6e 64 65 78 3b  it_create_index;
1ff14 0a 20 20 20 20 7d 0a 20 20 20 20 64 62 2d 3e 66  .    }.    db->f
1ff15 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
1ff16 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20  nternChanges;.  
1ff17 20 20 69 66 28 20 70 54 62 6c 4e 61 6d 65 21 3d    if( pTblName!=
1ff18 30 20 29 7b 0a 20 20 20 20 20 20 70 49 6e 64 65  0 ){.      pInde
1ff19 78 2d 3e 74 6e 75 6d 20 3d 20 64 62 2d 3e 69 6e  x->tnum = db->in
1ff1a 69 74 2e 6e 65 77 54 6e 75 6d 3b 0a 20 20 20 20  it.newTnum;.    
1ff1b 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
1ff1c 68 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  he db->init.busy
1ff1d 20 69 73 20 30 20 74 68 65 6e 20 63 72 65 61 74   is 0 then creat
1ff1e 65 20 74 68 65 20 69 6e 64 65 78 20 6f 6e 20 64  e the index on d
1ff1f 69 73 6b 2e 20 20 54 68 69 73 0a 20 20 2a 2a 20  isk.  This.  ** 
1ff20 69 6e 76 6f 6c 76 65 73 20 77 72 69 74 69 6e 67  involves writing
1ff21 20 74 68 65 20 69 6e 64 65 78 20 69 6e 74 6f 20   the index into 
1ff22 74 68 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65  the master table
1ff23 20 61 6e 64 20 66 69 6c 6c 69 6e 67 20 69 6e 20   and filling in 
1ff24 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 77  the.  ** index w
1ff25 69 74 68 20 74 68 65 20 63 75 72 72 65 6e 74 20  ith the current 
1ff26 74 61 62 6c 65 20 63 6f 6e 74 65 6e 74 73 2e 0a  table contents..
1ff27 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 62    **.  ** The db
1ff28 2d 3e 69 6e 69 74 2e 62 75 73 79 20 69 73 20 30  ->init.busy is 0
1ff29 20 77 68 65 6e 20 74 68 65 20 75 73 65 72 20 66   when the user f
1ff2a 69 72 73 74 20 65 6e 74 65 72 73 20 61 20 43 52  irst enters a CR
1ff2b 45 41 54 45 20 49 4e 44 45 58 20 0a 20 20 2a 2a  EATE INDEX .  **
1ff2c 20 63 6f 6d 6d 61 6e 64 2e 20 20 64 62 2d 3e 69   command.  db->i
1ff2d 6e 69 74 2e 62 75 73 79 20 69 73 20 31 20 77 68  nit.busy is 1 wh
1ff2e 65 6e 20 61 20 64 61 74 61 62 61 73 65 20 69 73  en a database is
1ff2f 20 6f 70 65 6e 65 64 20 61 6e 64 20 0a 20 20 2a   opened and .  *
1ff30 2a 20 43 52 45 41 54 45 20 49 4e 44 45 58 20 73  * CREATE INDEX s
1ff31 74 61 74 65 6d 65 6e 74 73 20 61 72 65 20 72 65  tatements are re
1ff32 61 64 20 6f 75 74 20 6f 66 20 74 68 65 20 6d 61  ad out of the ma
1ff33 73 74 65 72 20 74 61 62 6c 65 2e 20 20 49 6e 0a  ster table.  In.
1ff34 20 20 2a 2a 20 74 68 65 20 6c 61 74 74 65 72 20    ** the latter 
1ff35 63 61 73 65 20 74 68 65 20 69 6e 64 65 78 20 61  case the index a
1ff36 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 6f 6e  lready exists on
1ff37 20 64 69 73 6b 2c 20 77 68 69 63 68 20 69 73 20   disk, which is 
1ff38 77 68 79 0a 20 20 2a 2a 20 77 65 20 64 6f 6e 27  why.  ** we don'
1ff39 74 20 77 61 6e 74 20 74 6f 20 72 65 63 72 65 61  t want to recrea
1ff3a 74 65 20 69 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  te it..  **.  **
1ff3b 20 49 66 20 70 54 62 6c 4e 61 6d 65 3d 3d 30 20   If pTblName==0 
1ff3c 69 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 6e  it means this in
1ff3d 64 65 78 20 69 73 20 67 65 6e 65 72 61 74 65 64  dex is generated
1ff3e 20 61 73 20 61 20 70 72 69 6d 61 72 79 20 6b 65   as a primary ke
1ff3f 79 0a 20 20 2a 2a 20 6f 72 20 55 4e 49 51 55 45  y.  ** or UNIQUE
1ff40 20 63 6f 6e 73 74 72 61 69 6e 74 20 6f 66 20 61   constraint of a
1ff41 20 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74   CREATE TABLE st
1ff42 61 74 65 6d 65 6e 74 2e 20 20 53 69 6e 63 65 20  atement.  Since 
1ff43 74 68 65 20 74 61 62 6c 65 0a 20 20 2a 2a 20 68  the table.  ** h
1ff44 61 73 20 6a 75 73 74 20 62 65 65 6e 20 63 72 65  as just been cre
1ff45 61 74 65 64 2c 20 69 74 20 63 6f 6e 74 61 69 6e  ated, it contain
1ff46 73 20 6e 6f 20 64 61 74 61 20 61 6e 64 20 74 68  s no data and th
1ff47 65 20 69 6e 64 65 78 20 69 6e 69 74 69 61 6c 69  e index initiali
1ff48 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 73 74 65 70  zation.  ** step
1ff49 20 63 61 6e 20 62 65 20 73 6b 69 70 70 65 64 2e   can be skipped.
1ff4a 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 69 66 28  .  */.  else if(
1ff4b 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 3d 3d   db->init.busy==
1ff4c 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a 76  0 ){.    Vdbe *v
1ff4d 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 74 6d  ;.    char *zStm
1ff4e 74 3b 0a 20 20 20 20 69 6e 74 20 69 4d 65 6d 20  t;.    int iMem 
1ff4f 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
1ff50 3b 0a 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  ;..    v = sqlit
1ff51 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
1ff52 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20  );.    if( v==0 
1ff53 29 20 67 6f 74 6f 20 65 78 69 74 5f 63 72 65 61  ) goto exit_crea
1ff54 74 65 5f 69 6e 64 65 78 3b 0a 0a 0a 20 20 20 20  te_index;...    
1ff55 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 72 6f  /* Create the ro
1ff56 6f 74 70 61 67 65 20 66 6f 72 20 74 68 65 20 69  otpage for the i
1ff57 6e 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ndex.    */.    
1ff58 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
1ff59 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
1ff5a 65 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 1, iDb);.    
1ff5b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
1ff5c 32 28 76 2c 20 4f 50 5f 43 72 65 61 74 65 49 6e  2(v, OP_CreateIn
1ff5d 64 65 78 2c 20 69 44 62 2c 20 69 4d 65 6d 29 3b  dex, iDb, iMem);
1ff5e 0a 0a 20 20 20 20 2f 2a 20 47 61 74 68 65 72 20  ..    /* Gather 
1ff5f 74 68 65 20 63 6f 6d 70 6c 65 74 65 20 74 65 78  the complete tex
1ff60 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20  t of the CREATE 
1ff61 49 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20  INDEX statement 
1ff62 69 6e 74 6f 0a 20 20 20 20 2a 2a 20 74 68 65 20  into.    ** the 
1ff63 7a 53 74 6d 74 20 76 61 72 69 61 62 6c 65 0a 20  zStmt variable. 
1ff64 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53     */.    if( pS
1ff65 74 61 72 74 20 26 26 20 70 45 6e 64 20 29 7b 0a  tart && pEnd ){.
1ff66 20 20 20 20 20 20 2f 2a 20 41 20 6e 61 6d 65 64        /* A named
1ff67 20 69 6e 64 65 78 20 77 69 74 68 20 61 6e 20 65   index with an e
1ff68 78 70 6c 69 63 69 74 20 43 52 45 41 54 45 20 49  xplicit CREATE I
1ff69 4e 44 45 58 20 73 74 61 74 65 6d 65 6e 74 20 2a  NDEX statement *
1ff6a 2f 0a 20 20 20 20 20 20 7a 53 74 6d 74 20 3d 20  /.      zStmt = 
1ff6b 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64  sqlite3MPrintf(d
1ff6c 62 2c 20 22 43 52 45 41 54 45 25 73 20 49 4e 44  b, "CREATE%s IND
1ff6d 45 58 20 25 2e 2a 73 22 2c 0a 20 20 20 20 20 20  EX %.*s",.      
1ff6e 20 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 4e 6f    onError==OE_No
1ff6f 6e 65 20 3f 20 22 22 20 3a 20 22 20 55 4e 49 51  ne ? "" : " UNIQ
1ff70 55 45 22 2c 0a 20 20 20 20 20 20 20 20 70 45 6e  UE",.        pEn
1ff71 64 2d 3e 7a 20 2d 20 70 4e 61 6d 65 2d 3e 7a 20  d->z - pName->z 
1ff72 2b 20 31 2c 0a 20 20 20 20 20 20 20 20 70 4e 61  + 1,.        pNa
1ff73 6d 65 2d 3e 7a 29 3b 0a 20 20 20 20 7d 65 6c 73  me->z);.    }els
1ff74 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20 61  e{.      /* An a
1ff75 75 74 6f 6d 61 74 69 63 20 69 6e 64 65 78 20 63  utomatic index c
1ff76 72 65 61 74 65 64 20 62 79 20 61 20 50 52 49 4d  reated by a PRIM
1ff77 41 52 59 20 4b 45 59 20 6f 72 20 55 4e 49 51 55  ARY KEY or UNIQU
1ff78 45 20 63 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a  E constraint */.
1ff79 20 20 20 20 20 20 2f 2a 20 7a 53 74 6d 74 20 3d        /* zStmt =
1ff7a 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1ff7b 22 22 29 3b 20 2a 2f 0a 20 20 20 20 20 20 7a 53  ""); */.      zS
1ff7c 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 0a  tmt = 0;.    }..
1ff7d 20 20 20 20 2f 2a 20 41 64 64 20 61 6e 20 65 6e      /* Add an en
1ff7e 74 72 79 20 69 6e 20 73 71 6c 69 74 65 5f 6d 61  try in sqlite_ma
1ff7f 73 74 65 72 20 66 6f 72 20 74 68 69 73 20 69 6e  ster for this in
1ff80 64 65 78 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73  dex.    */.    s
1ff81 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
1ff82 65 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  e(pParse, .     
1ff83 20 20 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20     "INSERT INTO 
1ff84 25 51 2e 25 73 20 56 41 4c 55 45 53 28 27 69 6e  %Q.%s VALUES('in
1ff85 64 65 78 27 2c 25 51 2c 25 51 2c 23 25 64 2c 25  dex',%Q,%Q,#%d,%
1ff86 51 29 3b 22 2c 0a 20 20 20 20 20 20 20 20 64 62  Q);",.        db
1ff87 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
1ff88 2c 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  , SCHEMA_TABLE(i
1ff89 44 62 29 2c 0a 20 20 20 20 20 20 20 20 70 49 6e  Db),.        pIn
1ff8a 64 65 78 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20  dex->zName,.    
1ff8b 20 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c      pTab->zName,
1ff8c 0a 20 20 20 20 20 20 20 20 69 4d 65 6d 2c 0a 20  .        iMem,. 
1ff8d 20 20 20 20 20 20 20 7a 53 74 6d 74 0a 20 20 20         zStmt.   
1ff8e 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
1ff8f 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29  bFree(db, zStmt)
1ff90 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6c 6c 20 74  ;..    /* Fill t
1ff91 68 65 20 69 6e 64 65 78 20 77 69 74 68 20 64 61  he index with da
1ff92 74 61 20 61 6e 64 20 72 65 70 61 72 73 65 20 74  ta and reparse t
1ff93 68 65 20 73 63 68 65 6d 61 2e 20 43 6f 64 65 20  he schema. Code 
1ff94 61 6e 20 4f 50 5f 45 78 70 69 72 65 0a 20 20 20  an OP_Expire.   
1ff95 20 2a 2a 20 74 6f 20 69 6e 76 61 6c 69 64 61 74   ** to invalidat
1ff96 65 20 61 6c 6c 20 70 72 65 2d 63 6f 6d 70 69 6c  e all pre-compil
1ff97 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 20  ed statements.. 
1ff98 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 54     */.    if( pT
1ff99 62 6c 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20  blName ){.      
1ff9a 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64  sqlite3RefillInd
1ff9b 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65  ex(pParse, pInde
1ff9c 78 2c 20 69 4d 65 6d 29 3b 0a 20 20 20 20 20 20  x, iMem);.      
1ff9d 73 71 6c 69 74 65 33 43 68 61 6e 67 65 43 6f 6f  sqlite3ChangeCoo
1ff9e 6b 69 65 28 70 50 61 72 73 65 2c 20 69 44 62 29  kie(pParse, iDb)
1ff9f 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
1ffa0 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
1ffa1 50 61 72 73 65 53 63 68 65 6d 61 2c 20 69 44 62  ParseSchema, iDb
1ffa2 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20  , 0, 0,.        
1ffa3 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
1ffa4 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c  db, "name='%q'",
1ffa5 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65 29 2c   pIndex->zName),
1ffa6 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20   P4_DYNAMIC);.  
1ffa7 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
1ffa8 64 64 4f 70 31 28 76 2c 20 4f 50 5f 45 78 70 69  ddOp1(v, OP_Expi
1ffa9 72 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  re, 0);.    }.  
1ffaa 7d 0a 0a 20 20 2f 2a 20 57 68 65 6e 20 61 64 64  }..  /* When add
1ffab 69 6e 67 20 61 6e 20 69 6e 64 65 78 20 74 6f 20  ing an index to 
1ffac 74 68 65 20 6c 69 73 74 20 6f 66 20 69 6e 64 69  the list of indi
1ffad 63 65 73 20 66 6f 72 20 61 20 74 61 62 6c 65 2c  ces for a table,
1ffae 20 6d 61 6b 65 0a 20 20 2a 2a 20 73 75 72 65 20   make.  ** sure 
1ffaf 61 6c 6c 20 69 6e 64 69 63 65 73 20 6c 61 62 65  all indices labe
1ffb0 6c 65 64 20 4f 45 5f 52 65 70 6c 61 63 65 20 63  led OE_Replace c
1ffb1 6f 6d 65 20 61 66 74 65 72 20 61 6c 6c 20 74 68  ome after all th
1ffb2 6f 73 65 20 6c 61 62 65 6c 65 64 0a 20 20 2a 2a  ose labeled.  **
1ffb3 20 4f 45 5f 49 67 6e 6f 72 65 2e 20 20 54 68 69   OE_Ignore.  Thi
1ffb4 73 20 69 73 20 6e 65 63 65 73 73 61 72 79 20 66  s is necessary f
1ffb5 6f 72 20 74 68 65 20 63 6f 72 72 65 63 74 20 6f  or the correct o
1ffb6 70 65 72 61 74 69 6f 6e 20 6f 66 20 55 50 44 41  peration of UPDA
1ffb7 54 45 0a 20 20 2a 2a 20 61 6e 64 20 49 4e 53 45  TE.  ** and INSE
1ffb8 52 54 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64  RT..  */.  if( d
1ffb9 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 7c 7c 20  b->init.busy || 
1ffba 70 54 62 6c 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20  pTblName==0 ){. 
1ffbb 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d     if( onError!=
1ffbc 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54  OE_Replace || pT
1ffbd 61 62 2d 3e 70 49 6e 64 65 78 3d 3d 30 0a 20 20  ab->pIndex==0.  
1ffbe 20 20 20 20 20 20 20 7c 7c 20 70 54 61 62 2d 3e         || pTab->
1ffbf 70 49 6e 64 65 78 2d 3e 6f 6e 45 72 72 6f 72 3d  pIndex->onError=
1ffc0 3d 4f 45 5f 52 65 70 6c 61 63 65 29 7b 0a 20 20  =OE_Replace){.  
1ffc1 20 20 20 20 70 49 6e 64 65 78 2d 3e 70 4e 65 78      pIndex->pNex
1ffc2 74 20 3d 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  t = pTab->pIndex
1ffc3 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 70 49  ;.      pTab->pI
1ffc4 6e 64 65 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20  ndex = pIndex;. 
1ffc5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
1ffc6 49 6e 64 65 78 20 2a 70 4f 74 68 65 72 20 3d 20  Index *pOther = 
1ffc7 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20  pTab->pIndex;.  
1ffc8 20 20 20 20 77 68 69 6c 65 28 20 70 4f 74 68 65      while( pOthe
1ffc9 72 2d 3e 70 4e 65 78 74 20 26 26 20 70 4f 74 68  r->pNext && pOth
1ffca 65 72 2d 3e 70 4e 65 78 74 2d 3e 6f 6e 45 72 72  er->pNext->onErr
1ffcb 6f 72 21 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29  or!=OE_Replace )
1ffcc 7b 0a 20 20 20 20 20 20 20 20 70 4f 74 68 65 72  {.        pOther
1ffcd 20 3d 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74   = pOther->pNext
1ffce 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
1ffcf 70 49 6e 64 65 78 2d 3e 70 4e 65 78 74 20 3d 20  pIndex->pNext = 
1ffd0 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  pOther->pNext;. 
1ffd1 20 20 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65       pOther->pNe
1ffd2 78 74 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20  xt = pIndex;.   
1ffd3 20 7d 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20   }.    pIndex = 
1ffd4 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6c 65  0;.  }..  /* Cle
1ffd5 61 6e 20 75 70 20 62 65 66 6f 72 65 20 65 78 69  an up before exi
1ffd6 74 69 6e 67 20 2a 2f 0a 65 78 69 74 5f 63 72 65  ting */.exit_cre
1ffd7 61 74 65 5f 69 6e 64 65 78 3a 0a 20 20 69 66 28  ate_index:.  if(
1ffd8 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 66   pIndex ){.    f
1ffd9 72 65 65 49 6e 64 65 78 28 70 49 6e 64 65 78 29  reeIndex(pIndex)
1ffda 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45  ;.  }.  sqlite3E
1ffdb 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62  xprListDelete(db
1ffdc 2c 20 70 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  , pList);.  sqli
1ffdd 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65  te3SrcListDelete
1ffde 28 64 62 2c 20 70 54 62 6c 4e 61 6d 65 29 3b 0a  (db, pTblName);.
1ffdf 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
1ffe0 64 62 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 72 65  db, zName);.  re
1ffe1 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47  turn;.}../*.** G
1ffe2 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
1ffe3 6d 61 6b 65 20 73 75 72 65 20 74 68 65 20 66 69  make sure the fi
1ffe4 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62 65 72  le format number
1ffe5 20 69 73 20 61 74 20 6c 65 61 73 74 20 6d 69 6e   is at least min
1ffe6 46 6f 72 6d 61 74 2e 0a 2a 2a 20 54 68 65 20 67  Format..** The g
1ffe7 65 6e 65 72 61 74 65 64 20 63 6f 64 65 20 77 69  enerated code wi
1ffe8 6c 6c 20 69 6e 63 72 65 61 73 65 20 74 68 65 20  ll increase the 
1ffe9 66 69 6c 65 20 66 6f 72 6d 61 74 20 6e 75 6d 62  file format numb
1ffea 65 72 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  er if necessary.
1ffeb 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
1ffec 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
1ffed 69 6e 69 6d 75 6d 46 69 6c 65 46 6f 72 6d 61 74  inimumFileFormat
1ffee 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20  (Parse *pParse, 
1ffef 69 6e 74 20 69 44 62 2c 20 69 6e 74 20 6d 69 6e  int iDb, int min
1fff0 46 6f 72 6d 61 74 29 7b 0a 20 20 56 64 62 65 20  Format){.  Vdbe 
1fff1 2a 76 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  *v;.  v = sqlite
1fff2 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
1fff3 3b 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20  ;.  if( v ){.   
1fff4 20 69 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65   int r1 = sqlite
1fff5 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
1fff6 73 65 29 3b 0a 20 20 20 20 69 6e 74 20 72 32 20  se);.    int r2 
1fff7 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
1fff8 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
1fff9 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 73 71 6c   int j1;.    sql
1fffa 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
1fffb 2c 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c  , OP_ReadCookie,
1fffc 20 69 44 62 2c 20 72 31 2c 20 31 29 3b 0a 20 20   iDb, r1, 1);.  
1fffd 20 20 73 71 6c 69 74 65 33 56 64 62 65 55 73 65    sqlite3VdbeUse
1fffe 73 42 74 72 65 65 28 76 2c 20 69 44 62 29 3b 0a  sBtree(v, iDb);.
1ffff 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20000 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
20001 67 65 72 2c 20 6d 69 6e 46 6f 72 6d 61 74 2c 20  ger, minFormat, 
20002 72 32 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  r2);.    j1 = sq
20003 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
20004 76 2c 20 4f 50 5f 47 65 2c 20 72 32 2c 20 30 2c  v, OP_Ge, r2, 0,
20005 20 72 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   r1);.    sqlite
20006 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
20007 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44 62  P_SetCookie, iDb
20008 2c 20 31 2c 20 72 32 29 3b 0a 20 20 20 20 73 71  , 1, r2);.    sq
20009 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
2000a 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20 73 71  e(v, j1);.    sq
2000b 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
2000c 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
2000d 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
2000e 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73  aseTempReg(pPars
2000f 65 2c 20 72 32 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  e, r2);.  }.}../
20010 2a 0a 2a 2a 20 46 69 6c 6c 20 74 68 65 20 49 6e  *.** Fill the In
20011 64 65 78 2e 61 69 52 6f 77 45 73 74 5b 5d 20 61  dex.aiRowEst[] a
20012 72 72 61 79 20 77 69 74 68 20 64 65 66 61 75 6c  rray with defaul
20013 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 2d 20  t information - 
20014 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 2a 2a 20 74  information.** t
20015 6f 20 62 65 20 75 73 65 64 20 77 68 65 6e 20 77  o be used when w
20016 65 20 68 61 76 65 20 6e 6f 74 20 72 75 6e 20 74  e have not run t
20017 68 65 20 41 4e 41 4c 59 5a 45 20 63 6f 6d 6d 61  he ANALYZE comma
20018 6e 64 2e 0a 2a 2a 0a 2a 2a 20 61 69 52 6f 77 45  nd..**.** aiRowE
20019 73 74 5b 30 5d 20 69 73 20 73 75 70 70 6f 73 65  st[0] is suppose
2001a 20 74 6f 20 63 6f 6e 74 61 69 6e 20 74 68 65 20   to contain the 
2001b 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
2001c 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 2e  ts in the index.
2001d 0a 2a 2a 20 53 69 6e 63 65 20 77 65 20 64 6f 20  .** Since we do 
2001e 6e 6f 74 20 6b 6e 6f 77 2c 20 67 75 65 73 73 20  not know, guess 
2001f 31 20 6d 69 6c 6c 69 6f 6e 2e 20 20 61 69 52 6f  1 million.  aiRo
20020 77 45 73 74 5b 31 5d 20 69 73 20 61 6e 20 65 73  wEst[1] is an es
20021 74 69 6d 61 74 65 20 6f 66 20 74 68 65 0a 2a 2a  timate of the.**
20022 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
20023 69 6e 20 74 68 65 20 74 61 62 6c 65 20 74 68 61  in the table tha
20024 74 20 6d 61 74 63 68 20 61 6e 79 20 70 61 72 74  t match any part
20025 69 63 75 6c 61 72 20 76 61 6c 75 65 20 6f 66 20  icular value of 
20026 74 68 65 0a 2a 2a 20 66 69 72 73 74 20 63 6f 6c  the.** first col
20027 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64 65 78  umn of the index
20028 2e 20 20 61 69 52 6f 77 45 73 74 5b 32 5d 20 69  .  aiRowEst[2] i
20029 73 20 61 6e 20 65 73 74 69 6d 61 74 65 20 6f 66  s an estimate of
2002a 20 74 68 65 20 6e 75 6d 62 65 72 0a 2a 2a 20 6f   the number.** o
2002b 66 20 72 6f 77 73 20 74 68 61 74 20 6d 61 74 63  f rows that matc
2002c 68 20 61 6e 79 20 70 61 72 74 69 63 75 6c 61 72  h any particular
2002d 20 63 6f 6d 62 69 6e 69 61 74 69 6f 6e 20 6f 66   combiniation of
2002e 20 74 68 65 20 66 69 72 73 74 20 32 20 63 6f 6c   the first 2 col
2002f 75 6d 6e 73 0a 2a 2a 20 6f 66 20 74 68 65 20 69  umns.** of the i
20030 6e 64 65 78 2e 20 20 41 6e 64 20 73 6f 20 66 6f  ndex.  And so fo
20031 72 74 68 2e 20 20 49 74 20 6d 75 73 74 20 61 6c  rth.  It must al
20032 77 61 79 73 20 62 65 20 74 68 65 20 63 61 73 65  ways be the case
20033 20 74 68 61 74 0a 2a 0a 2a 2a 20 20 20 20 20 20   that.*.**      
20034 20 20 20 20 20 61 69 52 6f 77 45 73 74 5b 4e 5d       aiRowEst[N]
20035 3c 3d 61 69 52 6f 77 45 73 74 5b 4e 2d 31 5d 0a  <=aiRowEst[N-1].
20036 2a 2a 20 20 20 20 20 20 20 20 20 20 20 61 69 52  **           aiR
20037 6f 77 45 73 74 5b 4e 5d 3e 3d 31 0a 2a 2a 0a 2a  owEst[N]>=1.**.*
20038 2a 20 41 70 61 72 74 20 66 72 6f 6d 20 74 68 61  * Apart from tha
20039 74 2c 20 77 65 20 68 61 76 65 20 6c 69 74 74 6c  t, we have littl
2003a 65 20 74 6f 20 67 6f 20 6f 6e 20 62 65 73 69 64  e to go on besid
2003b 65 73 20 69 6e 74 75 69 74 69 6f 6e 20 61 73 20  es intuition as 
2003c 74 6f 0a 2a 2a 20 68 6f 77 20 61 69 52 6f 77 45  to.** how aiRowE
2003d 73 74 5b 5d 20 73 68 6f 75 6c 64 20 62 65 20 69  st[] should be i
2003e 6e 69 74 69 61 6c 69 7a 65 64 2e 20 20 54 68 65  nitialized.  The
2003f 20 6e 75 6d 62 65 72 73 20 67 65 6e 65 72 61 74   numbers generat
20040 65 64 20 68 65 72 65 0a 2a 2a 20 61 72 65 20 62  ed here.** are b
20041 61 73 65 64 20 6f 6e 20 74 79 70 69 63 61 6c 20  ased on typical 
20042 76 61 6c 75 65 73 20 66 6f 75 6e 64 20 69 6e 20  values found in 
20043 61 63 74 75 61 6c 20 69 6e 64 69 63 65 73 2e 0a  actual indices..
20044 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20045 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65  E void sqlite3De
20046 66 61 75 6c 74 52 6f 77 45 73 74 28 49 6e 64 65  faultRowEst(Inde
20047 78 20 2a 70 49 64 78 29 7b 0a 20 20 75 6e 73 69  x *pIdx){.  unsi
20048 67 6e 65 64 20 2a 61 20 3d 20 70 49 64 78 2d 3e  gned *a = pIdx->
20049 61 69 52 6f 77 45 73 74 3b 0a 20 20 69 6e 74 20  aiRowEst;.  int 
2004a 69 3b 0a 20 20 61 73 73 65 72 74 28 20 61 21 3d  i;.  assert( a!=
2004b 30 20 29 3b 0a 20 20 61 5b 30 5d 20 3d 20 31 30  0 );.  a[0] = 10
2004c 30 30 30 30 30 3b 0a 20 20 66 6f 72 28 69 3d 70  00000;.  for(i=p
2004d 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 3e  Idx->nColumn; i>
2004e 3d 35 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 61 5b  =5; i--){.    a[
2004f 69 5d 20 3d 20 35 3b 0a 20 20 7d 0a 20 20 77 68  i] = 5;.  }.  wh
20050 69 6c 65 28 20 69 3e 3d 31 20 29 7b 0a 20 20 20  ile( i>=1 ){.   
20051 20 61 5b 69 5d 20 3d 20 31 31 20 2d 20 69 3b 0a   a[i] = 11 - i;.
20052 20 20 20 20 69 2d 2d 3b 0a 20 20 7d 0a 20 20 69      i--;.  }.  i
20053 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72  f( pIdx->onError
20054 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20 20  !=OE_None ){.   
20055 20 61 5b 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e   a[pIdx->nColumn
20056 5d 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  ] = 1;.  }.}../*
20057 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
20058 20 77 69 6c 6c 20 64 72 6f 70 20 61 6e 20 65 78   will drop an ex
20059 69 73 74 69 6e 67 20 6e 61 6d 65 64 20 69 6e 64  isting named ind
2005a 65 78 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ex.  This routin
2005b 65 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 73 20  e.** implements 
2005c 74 68 65 20 44 52 4f 50 20 49 4e 44 45 58 20 73  the DROP INDEX s
2005d 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c  tatement..*/.SQL
2005e 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2005f 20 73 71 6c 69 74 65 33 44 72 6f 70 49 6e 64 65   sqlite3DropInde
20060 78 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  x(Parse *pParse,
20061 20 53 72 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c   SrcList *pName,
20062 20 69 6e 74 20 69 66 45 78 69 73 74 73 29 7b 0a   int ifExists){.
20063 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b    Index *pIndex;
20064 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 73 71  .  Vdbe *v;.  sq
20065 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20066 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 69 44  se->db;.  int iD
20067 62 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  b;..  if( pParse
20068 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
20069 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
2006a 20 20 67 6f 74 6f 20 65 78 69 74 5f 64 72 6f 70    goto exit_drop
2006b 5f 69 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 61 73  _index;.  }.  as
2006c 73 65 72 74 28 20 70 4e 61 6d 65 2d 3e 6e 53 72  sert( pName->nSr
2006d 63 3d 3d 31 20 29 3b 0a 20 20 69 66 28 20 53 51  c==1 );.  if( SQ
2006e 4c 49 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33  LITE_OK!=sqlite3
2006f 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72 73  ReadSchema(pPars
20070 65 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 65  e) ){.    goto e
20071 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
20072 20 20 7d 0a 20 20 70 49 6e 64 65 78 20 3d 20 73    }.  pIndex = s
20073 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78 28  qlite3FindIndex(
20074 64 62 2c 20 70 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  db, pName->a[0].
20075 7a 4e 61 6d 65 2c 20 70 4e 61 6d 65 2d 3e 61 5b  zName, pName->a[
20076 30 5d 2e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20  0].zDatabase);. 
20077 20 69 66 28 20 70 49 6e 64 65 78 3d 3d 30 20 29   if( pIndex==0 )
20078 7b 0a 20 20 20 20 69 66 28 20 21 69 66 45 78 69  {.    if( !ifExi
20079 73 74 73 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  sts ){.      sql
2007a 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
2007b 72 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e  rse, "no such in
2007c 64 65 78 3a 20 25 53 22 2c 20 70 4e 61 6d 65 2c  dex: %S", pName,
2007d 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70   0);.    }.    p
2007e 50 61 72 73 65 2d 3e 63 68 65 63 6b 53 63 68 65  Parse->checkSche
2007f 6d 61 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f  ma = 1;.    goto
20080 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
20081 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 49 6e 64  ;.  }.  if( pInd
20082 65 78 2d 3e 61 75 74 6f 49 6e 64 65 78 20 29 7b  ex->autoIndex ){
20083 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
20084 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 69 6e  rMsg(pParse, "in
20085 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
20086 69 74 68 20 55 4e 49 51 55 45 20 22 0a 20 20 20  ith UNIQUE ".   
20087 20 20 20 22 6f 72 20 50 52 49 4d 41 52 59 20 4b     "or PRIMARY K
20088 45 59 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 61  EY constraint ca
20089 6e 6e 6f 74 20 62 65 20 64 72 6f 70 70 65 64 22  nnot be dropped"
2008a 2c 20 30 29 3b 0a 20 20 20 20 67 6f 74 6f 20 65  , 0);.    goto e
2008b 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3b 0a  xit_drop_index;.
2008c 20 20 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69    }.  iDb = sqli
2008d 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2008e 28 64 62 2c 20 70 49 6e 64 65 78 2d 3e 70 53 63  (db, pIndex->pSc
2008f 68 65 6d 61 29 3b 0a 23 69 66 6e 64 65 66 20 53  hema);.#ifndef S
20090 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48 4f  QLITE_OMIT_AUTHO
20091 52 49 5a 41 54 49 4f 4e 0a 20 20 7b 0a 20 20 20  RIZATION.  {.   
20092 20 69 6e 74 20 63 6f 64 65 20 3d 20 53 51 4c 49   int code = SQLI
20093 54 45 5f 44 52 4f 50 5f 49 4e 44 45 58 3b 0a 20  TE_DROP_INDEX;. 
20094 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20 3d     Table *pTab =
20095 20 70 49 6e 64 65 78 2d 3e 70 54 61 62 6c 65 3b   pIndex->pTable;
20096 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
20097 2a 7a 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69  *zDb = db->aDb[i
20098 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
20099 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 62 20  onst char *zTab 
2009a 3d 20 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69  = SCHEMA_TABLE(i
2009b 44 62 29 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Db);.    if( sql
2009c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
2009d 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 44 45 4c  arse, SQLITE_DEL
2009e 45 54 45 2c 20 7a 54 61 62 2c 20 30 2c 20 7a 44  ETE, zTab, 0, zD
2009f 62 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  b) ){.      goto
200a0 20 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78   exit_drop_index
200a1 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
200a2 21 4f 4d 49 54 5f 54 45 4d 50 44 42 20 26 26 20  !OMIT_TEMPDB && 
200a3 69 44 62 20 29 20 63 6f 64 65 20 3d 20 53 51 4c  iDb ) code = SQL
200a4 49 54 45 5f 44 52 4f 50 5f 54 45 4d 50 5f 49 4e  ITE_DROP_TEMP_IN
200a5 44 45 58 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  DEX;.    if( sql
200a6 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
200a7 61 72 73 65 2c 20 63 6f 64 65 2c 20 70 49 6e 64  arse, code, pInd
200a8 65 78 2d 3e 7a 4e 61 6d 65 2c 20 70 54 61 62 2d  ex->zName, pTab-
200a9 3e 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 7b 0a  >zName, zDb) ){.
200aa 20 20 20 20 20 20 67 6f 74 6f 20 65 78 69 74 5f        goto exit_
200ab 64 72 6f 70 5f 69 6e 64 65 78 3b 0a 20 20 20 20  drop_index;.    
200ac 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
200ad 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
200ae 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 65 20 69   to remove the i
200af 6e 64 65 78 20 61 6e 64 20 66 72 6f 6d 20 74 68  ndex and from th
200b0 65 20 6d 61 73 74 65 72 20 74 61 62 6c 65 20 2a  e master table *
200b1 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47  /.  v = sqlite3G
200b2 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a  etVdbe(pParse);.
200b3 20 20 69 66 28 20 76 20 29 7b 0a 20 20 20 20 73    if( v ){.    s
200b4 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
200b5 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
200b6 2c 20 31 2c 20 69 44 62 29 3b 0a 20 20 20 20 73  , 1, iDb);.    s
200b7 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
200b8 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
200b9 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 25 51   "DELETE FROM %Q
200ba 2e 25 73 20 57 48 45 52 45 20 6e 61 6d 65 3d 25  .%s WHERE name=%
200bb 51 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  Q",.       db->a
200bc 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
200bd 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
200be 2c 0a 20 20 20 20 20 20 20 70 49 6e 64 65 78 2d  ,.       pIndex-
200bf 3e 7a 4e 61 6d 65 0a 20 20 20 20 29 3b 0a 20 20  >zName.    );.  
200c0 20 20 69 66 28 20 73 71 6c 69 74 65 33 46 69 6e    if( sqlite3Fin
200c1 64 54 61 62 6c 65 28 64 62 2c 20 22 73 71 6c 69  dTable(db, "sqli
200c2 74 65 5f 73 74 61 74 31 22 2c 20 64 62 2d 3e 61  te_stat1", db->a
200c3 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 20 29  Db[iDb].zName) )
200c4 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4e  {.      sqlite3N
200c5 65 73 74 65 64 50 61 72 73 65 28 70 50 61 72 73  estedParse(pPars
200c6 65 2c 0a 20 20 20 20 20 20 20 20 22 44 45 4c 45  e,.        "DELE
200c7 54 45 20 46 52 4f 4d 20 25 51 2e 73 71 6c 69 74  TE FROM %Q.sqlit
200c8 65 5f 73 74 61 74 31 20 57 48 45 52 45 20 69 64  e_stat1 WHERE id
200c9 78 3d 25 51 22 2c 0a 20 20 20 20 20 20 20 20 64  x=%Q",.        d
200ca 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
200cb 65 2c 20 70 49 6e 64 65 78 2d 3e 7a 4e 61 6d 65  e, pIndex->zName
200cc 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20 7d 0a  .      );.    }.
200cd 20 20 20 20 73 71 6c 69 74 65 33 43 68 61 6e 67      sqlite3Chang
200ce 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65 2c 20  eCookie(pParse, 
200cf 69 44 62 29 3b 0a 20 20 20 20 64 65 73 74 72 6f  iDb);.    destro
200d0 79 52 6f 6f 74 50 61 67 65 28 70 50 61 72 73 65  yRootPage(pParse
200d1 2c 20 70 49 6e 64 65 78 2d 3e 74 6e 75 6d 2c 20  , pIndex->tnum, 
200d2 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
200d3 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
200d4 50 5f 44 72 6f 70 49 6e 64 65 78 2c 20 69 44 62  P_DropIndex, iDb
200d5 2c 20 30 2c 20 30 2c 20 70 49 6e 64 65 78 2d 3e  , 0, 0, pIndex->
200d6 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 7d 0a 0a  zName, 0);.  }..
200d7 65 78 69 74 5f 64 72 6f 70 5f 69 6e 64 65 78 3a  exit_drop_index:
200d8 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
200d9 74 44 65 6c 65 74 65 28 64 62 2c 20 70 4e 61 6d  tDelete(db, pNam
200da 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 41 72  e);.}../*.** pAr
200db 72 61 79 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ray is a pointer
200dc 20 74 6f 20 61 6e 20 61 72 72 61 79 20 6f 66 20   to an array of 
200dd 6f 62 6a 65 63 74 73 2e 20 20 45 61 63 68 20 6f  objects.  Each o
200de 62 6a 65 63 74 20 69 6e 20 74 68 65 0a 2a 2a 20  bject in the.** 
200df 61 72 72 61 79 20 69 73 20 73 7a 45 6e 74 72 79  array is szEntry
200e0 20 62 79 74 65 73 20 69 6e 20 73 69 7a 65 2e 20   bytes in size. 
200e1 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61 6c   This routine al
200e2 6c 6f 63 61 74 65 73 20 61 20 6e 65 77 0a 2a 2a  locates a new.**
200e3 20 6f 62 6a 65 63 74 20 6f 6e 20 74 68 65 20 65   object on the e
200e4 6e 64 20 6f 66 20 74 68 65 20 61 72 72 61 79 2e  nd of the array.
200e5 0a 2a 2a 0a 2a 2a 20 2a 70 6e 45 6e 74 72 79 20  .**.** *pnEntry 
200e6 69 73 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  is the number of
200e7 20 65 6e 74 72 69 65 73 20 61 6c 72 65 61 64 79   entries already
200e8 20 69 6e 20 75 73 65 2e 20 20 2a 70 6e 41 6c 6c   in use.  *pnAll
200e9 6f 63 20 69 73 0a 2a 2a 20 74 68 65 20 70 72 65  oc is.** the pre
200ea 76 69 6f 75 73 6c 79 20 61 6c 6c 6f 63 61 74 65  viously allocate
200eb 64 20 73 69 7a 65 20 6f 66 20 74 68 65 20 61 72  d size of the ar
200ec 72 61 79 2e 20 20 69 6e 69 74 53 69 7a 65 20 69  ray.  initSize i
200ed 73 20 74 68 65 0a 2a 2a 20 73 75 67 67 65 73 74  s the.** suggest
200ee 65 64 20 69 6e 69 74 69 61 6c 20 61 72 72 61 79  ed initial array
200ef 20 73 69 7a 65 20 61 6c 6c 6f 63 61 74 69 6f 6e   size allocation
200f0 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 64 65  ..**.** The inde
200f1 78 20 6f 66 20 74 68 65 20 6e 65 77 20 65 6e 74  x of the new ent
200f2 72 79 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ry is returned i
200f3 6e 20 2a 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 54  n *pIdx..**.** T
200f4 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74 75  his routine retu
200f5 72 6e 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  rns a pointer to
200f6 20 74 68 65 20 61 72 72 61 79 20 6f 66 20 6f 62   the array of ob
200f7 6a 65 63 74 73 2e 20 20 54 68 69 73 0a 2a 2a 20  jects.  This.** 
200f8 6d 69 67 68 74 20 62 65 20 74 68 65 20 73 61 6d  might be the sam
200f9 65 20 61 73 20 74 68 65 20 70 41 72 72 61 79 20  e as the pArray 
200fa 70 61 72 61 6d 65 74 65 72 20 6f 72 20 69 74 20  parameter or it 
200fb 6d 69 67 68 74 20 62 65 20 61 20 64 69 66 66 65  might be a diffe
200fc 72 65 6e 74 0a 2a 2a 20 70 6f 69 6e 74 65 72 20  rent.** pointer 
200fd 69 66 20 74 68 65 20 61 72 72 61 79 20 77 61 73  if the array was
200fe 20 72 65 73 69 7a 65 64 2e 0a 2a 2f 0a 53 51 4c   resized..*/.SQL
200ff 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
20100 20 2a 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c   *sqlite3ArrayAl
20101 6c 6f 63 61 74 65 28 0a 20 20 73 71 6c 69 74 65  locate(.  sqlite
20102 33 20 2a 64 62 2c 20 20 20 20 20 20 2f 2a 20 43  3 *db,      /* C
20103 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 6e 6f 74  onnection to not
20104 69 66 79 20 6f 66 20 6d 61 6c 6c 6f 63 20 66 61  ify of malloc fa
20105 69 6c 75 72 65 73 20 2a 2f 0a 20 20 76 6f 69 64  ilures */.  void
20106 20 2a 70 41 72 72 61 79 2c 20 20 20 20 20 2f 2a   *pArray,     /*
20107 20 41 72 72 61 79 20 6f 66 20 6f 62 6a 65 63 74   Array of object
20108 73 2e 20 20 4d 69 67 68 74 20 62 65 20 72 65 61  s.  Might be rea
20109 6c 6c 6f 63 61 74 65 64 20 2a 2f 0a 20 20 69 6e  llocated */.  in
2010a 74 20 73 7a 45 6e 74 72 79 2c 20 20 20 20 20 20  t szEntry,      
2010b 2f 2a 20 53 69 7a 65 20 6f 66 20 65 61 63 68 20  /* Size of each 
2010c 6f 62 6a 65 63 74 20 69 6e 20 74 68 65 20 61 72  object in the ar
2010d 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20 69 6e 69  ray */.  int ini
2010e 74 53 69 7a 65 2c 20 20 20 20 20 2f 2a 20 53 75  tSize,     /* Su
2010f 67 67 65 73 74 65 64 20 69 6e 69 74 69 61 6c 20  ggested initial 
20110 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69 6e 20 65  allocation, in e
20111 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20 69 6e 74  lements */.  int
20112 20 2a 70 6e 45 6e 74 72 79 2c 20 20 20 20 20 2f   *pnEntry,     /
20113 2a 20 4e 75 6d 62 65 72 20 6f 66 20 6f 62 6a 65  * Number of obje
20114 63 74 73 20 63 75 72 72 65 6e 74 6c 79 20 69 6e  cts currently in
20115 20 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70   use */.  int *p
20116 6e 41 6c 6c 6f 63 2c 20 20 20 20 20 2f 2a 20 43  nAlloc,     /* C
20117 75 72 72 65 6e 74 20 73 69 7a 65 20 6f 66 20 74  urrent size of t
20118 68 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2c 20 69  he allocation, i
20119 6e 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a 20 20  n elements */.  
2011a 69 6e 74 20 2a 70 49 64 78 20 20 20 20 20 20 20  int *pIdx       
2011b 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 69    /* Write the i
2011c 6e 64 65 78 20 6f 66 20 61 20 6e 65 77 20 73 6c  ndex of a new sl
2011d 6f 74 20 68 65 72 65 20 2a 2f 0a 29 7b 0a 20 20  ot here */.){.  
2011e 63 68 61 72 20 2a 7a 3b 0a 20 20 69 66 28 20 2a  char *z;.  if( *
2011f 70 6e 45 6e 74 72 79 20 3e 3d 20 2a 70 6e 41 6c  pnEntry >= *pnAl
20120 6c 6f 63 20 29 7b 0a 20 20 20 20 76 6f 69 64 20  loc ){.    void 
20121 2a 70 4e 65 77 3b 0a 20 20 20 20 69 6e 74 20 6e  *pNew;.    int n
20122 65 77 53 69 7a 65 3b 0a 20 20 20 20 6e 65 77 53  ewSize;.    newS
20123 69 7a 65 20 3d 20 28 2a 70 6e 41 6c 6c 6f 63 29  ize = (*pnAlloc)
20124 2a 32 20 2b 20 69 6e 69 74 53 69 7a 65 3b 0a 20  *2 + initSize;. 
20125 20 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65     pNew = sqlite
20126 33 44 62 52 65 61 6c 6c 6f 63 28 64 62 2c 20 70  3DbRealloc(db, p
20127 41 72 72 61 79 2c 20 6e 65 77 53 69 7a 65 2a 73  Array, newSize*s
20128 7a 45 6e 74 72 79 29 3b 0a 20 20 20 20 69 66 28  zEntry);.    if(
20129 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20   pNew==0 ){.    
2012a 20 20 2a 70 49 64 78 20 3d 20 2d 31 3b 0a 20 20    *pIdx = -1;.  
2012b 20 20 20 20 72 65 74 75 72 6e 20 70 41 72 72 61      return pArra
2012c 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 6e  y;.    }.    *pn
2012d 41 6c 6c 6f 63 20 3d 20 6e 65 77 53 69 7a 65 3b  Alloc = newSize;
2012e 0a 20 20 20 20 70 41 72 72 61 79 20 3d 20 70 4e  .    pArray = pN
2012f 65 77 3b 0a 20 20 7d 0a 20 20 7a 20 3d 20 28 63  ew;.  }.  z = (c
20130 68 61 72 2a 29 70 41 72 72 61 79 3b 0a 20 20 6d  har*)pArray;.  m
20131 65 6d 73 65 74 28 26 7a 5b 2a 70 6e 45 6e 74 72  emset(&z[*pnEntr
20132 79 20 2a 20 73 7a 45 6e 74 72 79 5d 2c 20 30 2c  y * szEntry], 0,
20133 20 73 7a 45 6e 74 72 79 29 3b 0a 20 20 2a 70 49   szEntry);.  *pI
20134 64 78 20 3d 20 2a 70 6e 45 6e 74 72 79 3b 0a 20  dx = *pnEntry;. 
20135 20 2b 2b 2a 70 6e 45 6e 74 72 79 3b 0a 20 20 72   ++*pnEntry;.  r
20136 65 74 75 72 6e 20 70 41 72 72 61 79 3b 0a 7d 0a  eturn pArray;.}.
20137 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20 61 20  ./*.** Append a 
20138 6e 65 77 20 65 6c 65 6d 65 6e 74 20 74 6f 20 74  new element to t
20139 68 65 20 67 69 76 65 6e 20 49 64 4c 69 73 74 2e  he given IdList.
2013a 20 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 49    Create a new I
2013b 64 4c 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64  dList if.** need
2013c 20 62 65 2e 0a 2a 2a 0a 2a 2a 20 41 20 6e 65 77   be..**.** A new
2013d 20 49 64 4c 69 73 74 20 69 73 20 72 65 74 75 72   IdList is retur
2013e 6e 65 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20  ned, or NULL if 
2013f 6d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 2e 0a  malloc() fails..
20140 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20141 45 20 49 64 4c 69 73 74 20 2a 73 71 6c 69 74 65  E IdList *sqlite
20142 33 49 64 4c 69 73 74 41 70 70 65 6e 64 28 73 71  3IdListAppend(sq
20143 6c 69 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73  lite3 *db, IdLis
20144 74 20 2a 70 4c 69 73 74 2c 20 54 6f 6b 65 6e 20  t *pList, Token 
20145 2a 70 54 6f 6b 65 6e 29 7b 0a 20 20 69 6e 74 20  *pToken){.  int 
20146 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d  i;.  if( pList==
20147 30 20 29 7b 0a 20 20 20 20 70 4c 69 73 74 20 3d  0 ){.    pList =
20148 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63   sqlite3DbMalloc
20149 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28  Zero(db, sizeof(
2014a 49 64 4c 69 73 74 29 20 29 3b 0a 20 20 20 20 69  IdList) );.    i
2014b 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 20 72 65  f( pList==0 ) re
2014c 74 75 72 6e 20 30 3b 0a 20 20 20 20 70 4c 69 73  turn 0;.    pLis
2014d 74 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b 0a 20  t->nAlloc = 0;. 
2014e 20 7d 0a 20 20 70 4c 69 73 74 2d 3e 61 20 3d 20   }.  pList->a = 
2014f 73 71 6c 69 74 65 33 41 72 72 61 79 41 6c 6c 6f  sqlite3ArrayAllo
20150 63 61 74 65 28 0a 20 20 20 20 20 20 64 62 2c 0a  cate(.      db,.
20151 20 20 20 20 20 20 70 4c 69 73 74 2d 3e 61 2c 0a        pList->a,.
20152 20 20 20 20 20 20 73 69 7a 65 6f 66 28 70 4c 69        sizeof(pLi
20153 73 74 2d 3e 61 5b 30 5d 29 2c 0a 20 20 20 20 20  st->a[0]),.     
20154 20 35 2c 0a 20 20 20 20 20 20 26 70 4c 69 73 74   5,.      &pList
20155 2d 3e 6e 49 64 2c 0a 20 20 20 20 20 20 26 70 4c  ->nId,.      &pL
20156 69 73 74 2d 3e 6e 41 6c 6c 6f 63 2c 0a 20 20 20  ist->nAlloc,.   
20157 20 20 20 26 69 0a 20 20 29 3b 0a 20 20 69 66 28     &i.  );.  if(
20158 20 69 3c 30 20 29 7b 0a 20 20 20 20 73 71 6c 69   i<0 ){.    sqli
20159 74 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28  te3IdListDelete(
2015a 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20  db, pList);.    
2015b 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2015c 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
2015d 65 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  e = sqlite3NameF
2015e 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 54 6f  romToken(db, pTo
2015f 6b 65 6e 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  ken);.  return p
20160 4c 69 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44  List;.}../*.** D
20161 65 6c 65 74 65 20 61 6e 20 49 64 4c 69 73 74 2e  elete an IdList.
20162 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20163 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49  TE void sqlite3I
20164 64 4c 69 73 74 44 65 6c 65 74 65 28 73 71 6c 69  dListDelete(sqli
20165 74 65 33 20 2a 64 62 2c 20 49 64 4c 69 73 74 20  te3 *db, IdList 
20166 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69  *pList){.  int i
20167 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d 3d 30  ;.  if( pList==0
20168 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 66 6f 72   ) return;.  for
20169 28 69 3d 30 3b 20 69 3c 70 4c 69 73 74 2d 3e 6e  (i=0; i<pList->n
2016a 49 64 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71  Id; i++){.    sq
2016b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2016c 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  pList->a[i].zNam
2016d 65 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  e);.  }.  sqlite
2016e 33 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73  3DbFree(db, pLis
2016f 74 2d 3e 61 29 3b 0a 20 20 73 71 6c 69 74 65 33  t->a);.  sqlite3
20170 44 62 46 72 65 65 28 64 62 2c 20 70 4c 69 73 74  DbFree(db, pList
20171 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
20172 72 6e 20 74 68 65 20 69 6e 64 65 78 20 69 6e 20  rn the index in 
20173 70 4c 69 73 74 20 6f 66 20 74 68 65 20 69 64 65  pList of the ide
20174 6e 74 69 66 69 65 72 20 6e 61 6d 65 64 20 7a 49  ntifier named zI
20175 64 2e 20 20 52 65 74 75 72 6e 20 2d 31 0a 2a 2a  d.  Return -1.**
20176 20 69 66 20 6e 6f 74 20 66 6f 75 6e 64 2e 0a 2a   if not found..*
20177 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20178 20 69 6e 74 20 73 71 6c 69 74 65 33 49 64 4c 69   int sqlite3IdLi
20179 73 74 49 6e 64 65 78 28 49 64 4c 69 73 74 20 2a  stIndex(IdList *
2017a 70 4c 69 73 74 2c 20 63 6f 6e 73 74 20 63 68 61  pList, const cha
2017b 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 69 6e 74  r *zName){.  int
2017c 20 69 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 3d   i;.  if( pList=
2017d 3d 30 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a  =0 ) return -1;.
2017e 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
2017f 73 74 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  st->nId; i++){. 
20180 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
20181 72 49 43 6d 70 28 70 4c 69 73 74 2d 3e 61 5b 69  rICmp(pList->a[i
20182 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 29 3d  ].zName, zName)=
20183 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b 0a 20  =0 ) return i;. 
20184 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a   }.  return -1;.
20185 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 70 61 6e 64 20  }../*.** Expand 
20186 74 68 65 20 73 70 61 63 65 20 61 6c 6c 6f 63 61  the space alloca
20187 74 65 64 20 66 6f 72 20 74 68 65 20 67 69 76 65  ted for the give
20188 6e 20 53 72 63 4c 69 73 74 20 6f 62 6a 65 63 74  n SrcList object
20189 20 62 79 0a 2a 2a 20 63 72 65 61 74 69 6e 67 20   by.** creating 
2018a 6e 45 78 74 72 61 20 6e 65 77 20 73 6c 6f 74 73  nExtra new slots
2018b 20 62 65 67 69 6e 6e 69 6e 67 20 61 74 20 69 53   beginning at iS
2018c 74 61 72 74 2e 20 20 69 53 74 61 72 74 20 69 73  tart.  iStart is
2018d 20 7a 65 72 6f 20 62 61 73 65 64 2e 0a 2a 2a 20   zero based..** 
2018e 4e 65 77 20 73 6c 6f 74 73 20 61 72 65 20 7a 65  New slots are ze
2018f 72 6f 65 64 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20  roed..**.** For 
20190 65 78 61 6d 70 6c 65 2c 20 73 75 70 70 6f 73 65  example, suppose
20191 20 61 20 53 72 63 4c 69 73 74 20 69 6e 69 74 69   a SrcList initi
20192 61 6c 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74 77  ally contains tw
20193 6f 20 65 6e 74 72 69 65 73 3a 20 41 2c 42 2e 0a  o entries: A,B..
20194 2a 2a 20 54 6f 20 61 70 70 65 6e 64 20 33 20 6e  ** To append 3 n
20195 65 77 20 65 6e 74 72 69 65 73 20 6f 6e 74 6f 20  ew entries onto 
20196 74 68 65 20 65 6e 64 2c 20 64 6f 20 74 68 69 73  the end, do this
20197 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74  :.**.**    sqlit
20198 65 33 53 72 63 4c 69 73 74 45 6e 6c 61 72 67 65  e3SrcListEnlarge
20199 28 64 62 2c 20 70 53 72 63 6c 69 73 74 2c 20 33  (db, pSrclist, 3
2019a 2c 20 32 29 3b 0a 2a 2a 0a 2a 2a 20 41 66 74 65  , 2);.**.** Afte
2019b 72 20 74 68 65 20 63 61 6c 6c 20 61 62 6f 76 65  r the call above
2019c 20 69 74 20 77 6f 75 6c 64 20 63 6f 6e 74 61 69   it would contai
2019d 6e 3a 20 20 41 2c 20 42 2c 20 6e 69 6c 2c 20 6e  n:  A, B, nil, n
2019e 69 6c 2c 20 6e 69 6c 2e 0a 2a 2a 20 49 66 20 74  il, nil..** If t
2019f 68 65 20 69 53 74 61 72 74 20 61 72 67 75 6d 65  he iStart argume
201a0 6e 74 20 68 61 64 20 62 65 65 6e 20 31 20 69 6e  nt had been 1 in
201a1 73 74 65 61 64 20 6f 66 20 32 2c 20 74 68 65 6e  stead of 2, then
201a2 20 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 77   the result.** w
201a3 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 3a 20  ould have been: 
201a4 20 41 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 6e 69   A, nil, nil, ni
201a5 6c 2c 20 42 2e 20 20 54 6f 20 70 72 65 70 65 6e  l, B.  To prepen
201a6 64 20 74 68 65 20 6e 65 77 20 73 6c 6f 74 73 2c  d the new slots,
201a7 0a 2a 2a 20 74 68 65 20 69 53 74 61 72 74 20 76  .** the iStart v
201a8 61 6c 75 65 20 77 6f 75 6c 64 20 62 65 20 30 2e  alue would be 0.
201a9 20 20 54 68 65 20 72 65 73 75 6c 74 20 74 68 65    The result the
201aa 6e 20 77 6f 75 6c 64 0a 2a 2a 20 62 65 3a 20 6e  n would.** be: n
201ab 69 6c 2c 20 6e 69 6c 2c 20 6e 69 6c 2c 20 41 2c  il, nil, nil, A,
201ac 20 42 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 6d   B..**.** If a m
201ad 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f 6e  emory allocation
201ae 20 66 61 69 6c 73 20 74 68 65 20 53 72 63 4c 69   fails the SrcLi
201af 73 74 20 69 73 20 75 6e 63 68 61 6e 67 65 64 2e  st is unchanged.
201b0 20 20 54 68 65 0a 2a 2a 20 64 62 2d 3e 6d 61 6c    The.** db->mal
201b1 6c 6f 63 46 61 69 6c 65 64 20 66 6c 61 67 20 77  locFailed flag w
201b2 69 6c 6c 20 62 65 20 73 65 74 20 74 6f 20 74 72  ill be set to tr
201b3 75 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ue..*/.SQLITE_PR
201b4 49 56 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73  IVATE SrcList *s
201b5 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e 6c  qlite3SrcListEnl
201b6 61 72 67 65 28 0a 20 20 73 71 6c 69 74 65 33 20  arge(.  sqlite3 
201b7 2a 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 44 61  *db,       /* Da
201b8 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
201b9 6e 20 74 6f 20 6e 6f 74 69 66 79 20 6f 66 20 4f  n to notify of O
201ba 4f 4d 20 65 72 72 6f 72 73 20 2a 2f 0a 20 20 53  OM errors */.  S
201bb 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
201bc 20 20 2f 2a 20 54 68 65 20 53 72 63 4c 69 73 74    /* The SrcList
201bd 20 74 6f 20 62 65 20 65 6e 6c 61 72 67 65 64 20   to be enlarged 
201be 2a 2f 0a 20 20 69 6e 74 20 6e 45 78 74 72 61 2c  */.  int nExtra,
201bf 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
201c0 72 20 6f 66 20 6e 65 77 20 73 6c 6f 74 73 20 74  r of new slots t
201c1 6f 20 61 64 64 20 74 6f 20 70 53 72 63 2d 3e 61  o add to pSrc->a
201c2 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61  [] */.  int iSta
201c3 72 74 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e  rt         /* In
201c4 64 65 78 20 69 6e 20 70 53 72 63 2d 3e 61 5b 5d  dex in pSrc->a[]
201c5 20 6f 66 20 66 69 72 73 74 20 6e 65 77 20 73 6c   of first new sl
201c6 6f 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  ot */.){.  int i
201c7 3b 0a 0a 20 20 2f 2a 20 53 61 6e 69 74 79 20 63  ;..  /* Sanity c
201c8 68 65 63 6b 69 6e 67 20 6f 6e 20 63 61 6c 6c 69  hecking on calli
201c9 6e 67 20 70 61 72 61 6d 65 74 65 72 73 20 2a 2f  ng parameters */
201ca 0a 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72  .  assert( iStar
201cb 74 3e 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  t>=0 );.  assert
201cc 28 20 6e 45 78 74 72 61 3e 3d 31 20 29 3b 0a 20  ( nExtra>=1 );. 
201cd 20 69 66 28 20 70 53 72 63 3d 3d 30 20 7c 7c 20   if( pSrc==0 || 
201ce 69 53 74 61 72 74 3e 70 53 72 63 2d 3e 6e 53 72  iStart>pSrc->nSr
201cf 63 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  c ){.    assert(
201d0 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
201d1 64 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  d );.    return 
201d2 70 53 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  pSrc;.  }..  /* 
201d3 41 6c 6c 6f 63 61 74 65 20 61 64 64 69 74 69 6f  Allocate additio
201d4 6e 61 6c 20 73 70 61 63 65 20 69 66 20 6e 65 65  nal space if nee
201d5 64 65 64 20 2a 2f 0a 20 20 69 66 28 20 70 53 72  ded */.  if( pSr
201d6 63 2d 3e 6e 53 72 63 2b 6e 45 78 74 72 61 3e 70  c->nSrc+nExtra>p
201d7 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  Src->nAlloc ){. 
201d8 20 20 20 53 72 63 4c 69 73 74 20 2a 70 4e 65 77     SrcList *pNew
201d9 3b 0a 20 20 20 20 69 6e 74 20 6e 41 6c 6c 6f 63  ;.    int nAlloc
201da 20 3d 20 70 53 72 63 2d 3e 6e 53 72 63 2b 6e 45   = pSrc->nSrc+nE
201db 78 74 72 61 3b 0a 20 20 20 20 70 4e 65 77 20 3d  xtra;.    pNew =
201dc 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f   sqlite3DbReallo
201dd 63 28 64 62 2c 20 70 53 72 63 2c 0a 20 20 20 20  c(db, pSrc,.    
201de 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
201df 66 28 2a 70 53 72 63 29 20 2b 20 28 6e 41 6c 6c  f(*pSrc) + (nAll
201e0 6f 63 2d 31 29 2a 73 69 7a 65 6f 66 28 70 53 72  oc-1)*sizeof(pSr
201e1 63 2d 3e 61 5b 30 5d 29 20 29 3b 0a 20 20 20 20  c->a[0]) );.    
201e2 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
201e3 20 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d       assert( db-
201e4 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b  >mallocFailed );
201e5 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 70 53  .      return pS
201e6 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 53  rc;.    }.    pS
201e7 72 63 20 3d 20 70 4e 65 77 3b 0a 20 20 20 20 70  rc = pNew;.    p
201e8 53 72 63 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 6e 41  Src->nAlloc = nA
201e9 6c 6c 6f 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  lloc;.  }..  /* 
201ea 4d 6f 76 65 20 65 78 69 73 74 69 6e 67 20 73 6c  Move existing sl
201eb 6f 74 73 20 74 68 61 74 20 63 6f 6d 65 20 61 66  ots that come af
201ec 74 65 72 20 74 68 65 20 6e 65 77 6c 79 20 69 6e  ter the newly in
201ed 73 65 72 74 65 64 20 73 6c 6f 74 73 0a 20 20 2a  serted slots.  *
201ee 2a 20 6f 75 74 20 6f 66 20 74 68 65 20 77 61 79  * out of the way
201ef 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 70 53 72 63   */.  for(i=pSrc
201f0 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 3d 69 53 74  ->nSrc-1; i>=iSt
201f1 61 72 74 3b 20 69 2d 2d 29 7b 0a 20 20 20 20 70  art; i--){.    p
201f2 53 72 63 2d 3e 61 5b 69 2b 6e 45 78 74 72 61 5d  Src->a[i+nExtra]
201f3 20 3d 20 70 53 72 63 2d 3e 61 5b 69 5d 3b 0a 20   = pSrc->a[i];. 
201f4 20 7d 0a 20 20 70 53 72 63 2d 3e 6e 53 72 63 20   }.  pSrc->nSrc 
201f5 2b 3d 20 6e 45 78 74 72 61 3b 0a 0a 20 20 2f 2a  += nExtra;..  /*
201f6 20 5a 65 72 6f 20 74 68 65 20 6e 65 77 6c 79 20   Zero the newly 
201f7 61 6c 6c 6f 63 61 74 65 64 20 73 6c 6f 74 73 20  allocated slots 
201f8 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 70 53 72  */.  memset(&pSr
201f9 63 2d 3e 61 5b 69 53 74 61 72 74 5d 2c 20 30 2c  c->a[iStart], 0,
201fa 20 73 69 7a 65 6f 66 28 70 53 72 63 2d 3e 61 5b   sizeof(pSrc->a[
201fb 30 5d 29 2a 6e 45 78 74 72 61 29 3b 0a 20 20 66  0])*nExtra);.  f
201fc 6f 72 28 69 3d 69 53 74 61 72 74 3b 20 69 3c 69  or(i=iStart; i<i
201fd 53 74 61 72 74 2b 6e 45 78 74 72 61 3b 20 69 2b  Start+nExtra; i+
201fe 2b 29 7b 0a 20 20 20 20 70 53 72 63 2d 3e 61 5b  +){.    pSrc->a[
201ff 69 5d 2e 69 43 75 72 73 6f 72 20 3d 20 2d 31 3b  i].iCursor = -1;
20200 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 74 75 72  .  }..  /* Retur
20201 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74  n a pointer to t
20202 68 65 20 65 6e 6c 61 72 67 65 64 20 53 72 63 4c  he enlarged SrcL
20203 69 73 74 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20  ist */.  return 
20204 70 53 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  pSrc;.}.../*.** 
20205 41 70 70 65 6e 64 20 61 20 6e 65 77 20 74 61 62  Append a new tab
20206 6c 65 20 6e 61 6d 65 20 74 6f 20 74 68 65 20 67  le name to the g
20207 69 76 65 6e 20 53 72 63 4c 69 73 74 2e 20 20 43  iven SrcList.  C
20208 72 65 61 74 65 20 61 20 6e 65 77 20 53 72 63 4c  reate a new SrcL
20209 69 73 74 20 69 66 0a 2a 2a 20 6e 65 65 64 20 62  ist if.** need b
2020a 65 2e 20 20 41 20 6e 65 77 20 65 6e 74 72 79 20  e.  A new entry 
2020b 69 73 20 63 72 65 61 74 65 64 20 69 6e 20 74 68  is created in th
2020c 65 20 53 72 63 4c 69 73 74 20 65 76 65 6e 20 69  e SrcList even i
2020d 66 20 70 54 6f 6b 65 6e 20 69 73 20 4e 55 4c 4c  f pToken is NULL
2020e 2e 0a 2a 2a 0a 2a 2a 20 41 20 53 72 63 4c 69 73  ..**.** A SrcLis
2020f 74 20 69 73 20 72 65 74 75 72 6e 65 64 2c 20 6f  t is returned, o
20210 72 20 4e 55 4c 4c 20 69 66 20 74 68 65 72 65 20  r NULL if there 
20211 69 73 20 61 6e 20 4f 4f 4d 20 65 72 72 6f 72 2e  is an OOM error.
20212 20 20 54 68 65 20 72 65 74 75 72 6e 65 64 0a 2a    The returned.*
20213 2a 20 53 72 63 4c 69 73 74 20 6d 69 67 68 74 20  * SrcList might 
20214 62 65 20 74 68 65 20 73 61 6d 65 20 61 73 20 74  be the same as t
20215 68 65 20 53 72 63 4c 69 73 74 20 74 68 61 74 20  he SrcList that 
20216 77 61 73 20 69 6e 70 75 74 20 6f 72 20 69 74 20  was input or it 
20217 6d 69 67 68 74 20 62 65 0a 2a 2a 20 61 20 6e 65  might be.** a ne
20218 77 20 6f 6e 65 2e 20 20 49 66 20 61 6e 20 4f 4f  w one.  If an OO
20219 4d 20 65 72 72 6f 72 20 64 6f 65 73 20 6f 63 63  M error does occ
2021a 75 72 73 2c 20 74 68 65 6e 20 74 68 65 20 70 72  urs, then the pr
2021b 69 6f 72 20 76 61 6c 75 65 20 6f 66 20 70 4c 69  ior value of pLi
2021c 73 74 0a 2a 2a 20 74 68 61 74 20 69 73 20 69 6e  st.** that is in
2021d 70 75 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74  put to this rout
2021e 69 6e 65 20 69 73 20 61 75 74 6f 6d 61 74 69 63  ine is automatic
2021f 61 6c 6c 79 20 66 72 65 65 64 2e 0a 2a 2a 0a 2a  ally freed..**.*
20220 2a 20 49 66 20 70 44 61 74 61 62 61 73 65 20 69  * If pDatabase i
20221 73 20 6e 6f 74 20 6e 75 6c 6c 2c 20 69 74 20 6d  s not null, it m
20222 65 61 6e 73 20 74 68 61 74 20 74 68 65 20 74 61  eans that the ta
20223 62 6c 65 20 68 61 73 20 61 6e 20 6f 70 74 69 6f  ble has an optio
20224 6e 61 6c 0a 2a 2a 20 64 61 74 61 62 61 73 65 20  nal.** database 
20225 6e 61 6d 65 20 70 72 65 66 69 78 2e 20 20 4c 69  name prefix.  Li
20226 6b 65 20 74 68 69 73 3a 20 20 22 64 61 74 61 62  ke this:  "datab
20227 61 73 65 2e 74 61 62 6c 65 22 2e 20 20 54 68 65  ase.table".  The
20228 20 70 44 61 74 61 62 61 73 65 0a 2a 2a 20 70 6f   pDatabase.** po
20229 69 6e 74 73 20 74 6f 20 74 68 65 20 74 61 62 6c  ints to the tabl
2022a 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 65 20 70  e name and the p
2022b 54 61 62 6c 65 20 70 6f 69 6e 74 73 20 74 6f 20  Table points to 
2022c 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
2022d 65 2e 0a 2a 2a 20 54 68 65 20 53 72 63 4c 69 73  e..** The SrcLis
2022e 74 2e 61 5b 5d 2e 7a 4e 61 6d 65 20 66 69 65 6c  t.a[].zName fiel
2022f 64 20 69 73 20 66 69 6c 6c 65 64 20 77 69 74 68  d is filled with
20230 20 74 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20   the table name 
20231 77 68 69 63 68 20 6d 69 67 68 74 0a 2a 2a 20 63  which might.** c
20232 6f 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c 65 20  ome from pTable 
20233 28 69 66 20 70 44 61 74 61 62 61 73 65 20 69 73  (if pDatabase is
20234 20 4e 55 4c 4c 29 20 6f 72 20 66 72 6f 6d 20 70   NULL) or from p
20235 44 61 74 61 62 61 73 65 2e 20 20 0a 2a 2a 20 53  Database.  .** S
20236 72 63 4c 69 73 74 2e 61 5b 5d 2e 7a 44 61 74 61  rcList.a[].zData
20237 62 61 73 65 20 69 73 20 66 69 6c 6c 65 64 20 77  base is filled w
20238 69 74 68 20 74 68 65 20 64 61 74 61 62 61 73 65  ith the database
20239 20 6e 61 6d 65 20 66 72 6f 6d 20 70 54 61 62 6c   name from pTabl
2023a 65 2c 0a 2a 2a 20 6f 72 20 77 69 74 68 20 4e 55  e,.** or with NU
2023b 4c 4c 20 69 66 20 6e 6f 20 64 61 74 61 62 61 73  LL if no databas
2023c 65 20 69 73 20 73 70 65 63 69 66 69 65 64 2e 0a  e is specified..
2023d 2a 2a 0a 2a 2a 20 49 6e 20 6f 74 68 65 72 20 77  **.** In other w
2023e 6f 72 64 73 2c 20 69 66 20 63 61 6c 6c 20 6c 69  ords, if call li
2023f 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20  ke this:.**.**  
20240 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 72         sqlite3Sr
20241 63 4c 69 73 74 41 70 70 65 6e 64 28 44 2c 41 2c  cListAppend(D,A,
20242 42 2c 30 29 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e  B,0);.**.** Then
20243 20 42 20 69 73 20 61 20 74 61 62 6c 65 20 6e 61   B is a table na
20244 6d 65 20 61 6e 64 20 74 68 65 20 64 61 74 61 62  me and the datab
20245 61 73 65 20 6e 61 6d 65 20 69 73 20 75 6e 73 70  ase name is unsp
20246 65 63 69 66 69 65 64 2e 20 20 49 66 20 63 61 6c  ecified.  If cal
20247 6c 65 64 0a 2a 2a 20 6c 69 6b 65 20 74 68 69 73  led.** like this
20248 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
20249 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
2024a 70 65 6e 64 28 44 2c 41 2c 42 2c 43 29 3b 0a 2a  pend(D,A,B,C);.*
2024b 2a 0a 2a 2a 20 54 68 65 6e 20 43 20 69 73 20 74  *.** Then C is t
2024c 68 65 20 74 61 62 6c 65 20 6e 61 6d 65 20 61 6e  he table name an
2024d 64 20 42 20 69 73 20 74 68 65 20 64 61 74 61 62  d B is the datab
2024e 61 73 65 20 6e 61 6d 65 2e 0a 2a 2f 0a 53 51 4c  ase name..*/.SQL
2024f 49 54 45 5f 50 52 49 56 41 54 45 20 53 72 63 4c  ITE_PRIVATE SrcL
20250 69 73 74 20 2a 73 71 6c 69 74 65 33 53 72 63 4c  ist *sqlite3SrcL
20251 69 73 74 41 70 70 65 6e 64 28 0a 20 20 73 71 6c  istAppend(.  sql
20252 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
20253 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20 74   /* Connection t
20254 6f 20 6e 6f 74 69 66 79 20 6f 66 20 6d 61 6c 6c  o notify of mall
20255 6f 63 20 66 61 69 6c 75 72 65 73 20 2a 2f 0a 20  oc failures */. 
20256 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74 2c   SrcList *pList,
20257 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20 74       /* Append t
20258 6f 20 74 68 69 73 20 53 72 63 4c 69 73 74 2e 20  o this SrcList. 
20259 4e 55 4c 4c 20 63 72 65 61 74 65 73 20 61 20 6e  NULL creates a n
2025a 65 77 20 53 72 63 4c 69 73 74 20 2a 2f 0a 20 20  ew SrcList */.  
2025b 54 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 2c 20 20  Token *pTable,  
2025c 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20      /* Table to 
2025d 61 70 70 65 6e 64 20 2a 2f 0a 20 20 54 6f 6b 65  append */.  Toke
2025e 6e 20 2a 70 44 61 74 61 62 61 73 65 20 20 20 20  n *pDatabase    
2025f 2f 2a 20 44 61 74 61 62 61 73 65 20 6f 66 20 74  /* Database of t
20260 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20  he table */.){. 
20261 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
20262 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
20263 66 28 20 70 4c 69 73 74 3d 3d 30 20 29 7b 0a 20  f( pList==0 ){. 
20264 20 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74     pList = sqlit
20265 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64  e3DbMallocZero(d
20266 62 2c 20 73 69 7a 65 6f 66 28 53 72 63 4c 69 73  b, sizeof(SrcLis
20267 74 29 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  t) );.    if( pL
20268 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ist==0 ) return 
20269 30 3b 0a 20 20 20 20 70 4c 69 73 74 2d 3e 6e 41  0;.    pList->nA
2026a 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 7d 0a 20 20  lloc = 1;.  }.  
2026b 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 53  pList = sqlite3S
2026c 72 63 4c 69 73 74 45 6e 6c 61 72 67 65 28 64 62  rcListEnlarge(db
2026d 2c 20 70 4c 69 73 74 2c 20 31 2c 20 70 4c 69 73  , pList, 1, pLis
2026e 74 2d 3e 6e 53 72 63 29 3b 0a 20 20 69 66 28 20  t->nSrc);.  if( 
2026f 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
20270 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53   ){.    sqlite3S
20271 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
20272 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 72 65 74   pList);.    ret
20273 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 49 74  urn 0;.  }.  pIt
20274 65 6d 20 3d 20 26 70 4c 69 73 74 2d 3e 61 5b 70  em = &pList->a[p
20275 4c 69 73 74 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20  List->nSrc-1];. 
20276 20 69 66 28 20 70 44 61 74 61 62 61 73 65 20 26   if( pDatabase &
20277 26 20 70 44 61 74 61 62 61 73 65 2d 3e 7a 3d 3d  & pDatabase->z==
20278 30 20 29 7b 0a 20 20 20 20 70 44 61 74 61 62 61  0 ){.    pDataba
20279 73 65 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  se = 0;.  }.  if
2027a 28 20 70 44 61 74 61 62 61 73 65 20 26 26 20 70  ( pDatabase && p
2027b 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 54 6f 6b  Table ){.    Tok
2027c 65 6e 20 2a 70 54 65 6d 70 20 3d 20 70 44 61 74  en *pTemp = pDat
2027d 61 62 61 73 65 3b 0a 20 20 20 20 70 44 61 74 61  abase;.    pData
2027e 62 61 73 65 20 3d 20 70 54 61 62 6c 65 3b 0a 20  base = pTable;. 
2027f 20 20 20 70 54 61 62 6c 65 20 3d 20 70 54 65 6d     pTable = pTem
20280 70 3b 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e  p;.  }.  pItem->
20281 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4e  zName = sqlite3N
20282 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c  ameFromToken(db,
20283 20 70 54 61 62 6c 65 29 3b 0a 20 20 70 49 74 65   pTable);.  pIte
20284 6d 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 73  m->zDatabase = s
20285 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
20286 6b 65 6e 28 64 62 2c 20 70 44 61 74 61 62 61 73  ken(db, pDatabas
20287 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 4c 69  e);.  return pLi
20288 73 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 73 73  st;.}../*.** Ass
20289 69 67 6e 20 56 64 62 65 43 75 72 73 6f 72 20 69  ign VdbeCursor i
2028a 6e 64 65 78 20 6e 75 6d 62 65 72 73 20 74 6f 20  ndex numbers to 
2028b 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 20  all tables in a 
2028c 53 72 63 4c 69 73 74 0a 2a 2f 0a 53 51 4c 49 54  SrcList.*/.SQLIT
2028d 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2028e 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73 73  qlite3SrcListAss
2028f 69 67 6e 43 75 72 73 6f 72 73 28 50 61 72 73 65  ignCursors(Parse
20290 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
20291 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69 6e 74  t *pList){.  int
20292 20 69 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63   i;.  struct Src
20293 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d  List_item *pItem
20294 3b 0a 20 20 61 73 73 65 72 74 28 70 4c 69 73 74  ;.  assert(pList
20295 20 7c 7c 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e   || pParse->db->
20296 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a  mallocFailed );.
20297 20 20 69 66 28 20 70 4c 69 73 74 20 29 7b 0a 20    if( pList ){. 
20298 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74 65     for(i=0, pIte
20299 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 4c  m=pList->a; i<pL
2029a 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20  ist->nSrc; i++, 
2029b 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
2029c 69 66 28 20 70 49 74 65 6d 2d 3e 69 43 75 72 73  if( pItem->iCurs
2029d 6f 72 3e 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20  or>=0 ) break;. 
2029e 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 43 75 72       pItem->iCur
2029f 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  sor = pParse->nT
202a0 61 62 2b 2b 3b 0a 20 20 20 20 20 20 69 66 28 20  ab++;.      if( 
202a1 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63 74 20 29  pItem->pSelect )
202a2 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
202a3 33 53 72 63 4c 69 73 74 41 73 73 69 67 6e 43 75  3SrcListAssignCu
202a4 72 73 6f 72 73 28 70 50 61 72 73 65 2c 20 70 49  rsors(pParse, pI
202a5 74 65 6d 2d 3e 70 53 65 6c 65 63 74 2d 3e 70 53  tem->pSelect->pS
202a6 72 63 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rc);.      }.   
202a7 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
202a8 44 65 6c 65 74 65 20 61 6e 20 65 6e 74 69 72 65  Delete an entire
202a9 20 53 72 63 4c 69 73 74 20 69 6e 63 6c 75 64 69   SrcList includi
202aa 6e 67 20 61 6c 6c 20 69 74 73 20 73 75 62 73 74  ng all its subst
202ab 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49  ructure..*/.SQLI
202ac 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
202ad 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 44 65  sqlite3SrcListDe
202ae 6c 65 74 65 28 73 71 6c 69 74 65 33 20 2a 64 62  lete(sqlite3 *db
202af 2c 20 53 72 63 4c 69 73 74 20 2a 70 4c 69 73 74  , SrcList *pList
202b0 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74  ){.  int i;.  st
202b1 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
202b2 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69 66 28 20  m *pItem;.  if( 
202b3 70 4c 69 73 74 3d 3d 30 20 29 20 72 65 74 75 72  pList==0 ) retur
202b4 6e 3b 0a 20 20 66 6f 72 28 70 49 74 65 6d 3d 70  n;.  for(pItem=p
202b5 4c 69 73 74 2d 3e 61 2c 20 69 3d 30 3b 20 69 3c  List->a, i=0; i<
202b6 70 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b  pList->nSrc; i++
202b7 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20  , pItem++){.    
202b8 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
202b9 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62 61  , pItem->zDataba
202ba 73 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  se);.    sqlite3
202bb 44 62 46 72 65 65 28 64 62 2c 20 70 49 74 65 6d  DbFree(db, pItem
202bc 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 73 71  ->zName);.    sq
202bd 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
202be 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  pItem->zAlias);.
202bf 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
202c0 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 7a 49 6e  e(db, pItem->zIn
202c1 64 65 78 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  dex);.    sqlite
202c2 33 44 65 6c 65 74 65 54 61 62 6c 65 28 70 49 74  3DeleteTable(pIt
202c3 65 6d 2d 3e 70 54 61 62 29 3b 0a 20 20 20 20 73  em->pTab);.    s
202c4 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
202c5 74 65 28 64 62 2c 20 70 49 74 65 6d 2d 3e 70 53  te(db, pItem->pS
202c6 65 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69  elect);.    sqli
202c7 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62  te3ExprDelete(db
202c8 2c 20 70 49 74 65 6d 2d 3e 70 4f 6e 29 3b 0a 20  , pItem->pOn);. 
202c9 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
202ca 44 65 6c 65 74 65 28 64 62 2c 20 70 49 74 65 6d  Delete(db, pItem
202cb 2d 3e 70 55 73 69 6e 67 29 3b 0a 20 20 7d 0a 20  ->pUsing);.  }. 
202cc 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
202cd 62 2c 20 70 4c 69 73 74 29 3b 0a 7d 0a 0a 2f 2a  b, pList);.}../*
202ce 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
202cf 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20 74 68   is called by th
202d0 65 20 70 61 72 73 65 72 20 74 6f 20 61 64 64 20  e parser to add 
202d1 61 20 6e 65 77 20 74 65 72 6d 20 74 6f 20 74 68  a new term to th
202d2 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 61 20 67 72  e.** end of a gr
202d3 6f 77 69 6e 67 20 46 52 4f 4d 20 63 6c 61 75 73  owing FROM claus
202d4 65 2e 20 20 54 68 65 20 22 70 22 20 70 61 72 61  e.  The "p" para
202d5 6d 65 74 65 72 20 69 73 20 74 68 65 20 70 61 72  meter is the par
202d6 74 20 6f 66 0a 2a 2a 20 74 68 65 20 46 52 4f 4d  t of.** the FROM
202d7 20 63 6c 61 75 73 65 20 74 68 61 74 20 68 61 73   clause that has
202d8 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f   already been co
202d9 6e 73 74 72 75 63 74 65 64 2e 20 20 22 70 22 20  nstructed.  "p" 
202da 69 73 20 4e 55 4c 4c 0a 2a 2a 20 69 66 20 74 68  is NULL.** if th
202db 69 73 20 69 73 20 74 68 65 20 66 69 72 73 74 20  is is the first 
202dc 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52 4f 4d  term of the FROM
202dd 20 63 6c 61 75 73 65 2e 20 20 70 54 61 62 6c 65   clause.  pTable
202de 20 61 6e 64 20 70 44 61 74 61 62 61 73 65 0a 2a   and pDatabase.*
202df 2a 20 61 72 65 20 74 68 65 20 6e 61 6d 65 20 6f  * are the name o
202e0 66 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20  f the table and 
202e1 64 61 74 61 62 61 73 65 20 6e 61 6d 65 64 20 69  database named i
202e2 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
202e3 65 20 74 65 72 6d 2e 0a 2a 2a 20 70 44 61 74 61  e term..** pData
202e4 62 61 73 65 20 69 73 20 4e 55 4c 4c 20 69 66 20  base is NULL if 
202e5 74 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 6d  the database nam
202e6 65 20 71 75 61 6c 69 66 69 65 72 20 69 73 20 6d  e qualifier is m
202e7 69 73 73 69 6e 67 20 2d 20 74 68 65 0a 2a 2a 20  issing - the.** 
202e8 75 73 75 61 6c 20 63 61 73 65 2e 20 20 49 66 20  usual case.  If 
202e9 74 68 65 20 74 65 72 6d 20 68 61 73 20 61 20 61  the term has a a
202ea 6c 69 61 73 2c 20 74 68 65 6e 20 70 41 6c 69 61  lias, then pAlia
202eb 73 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 0a  s points to the.
202ec 2a 2a 20 61 6c 69 61 73 20 74 6f 6b 65 6e 2e 20  ** alias token. 
202ed 20 49 66 20 74 68 65 20 74 65 72 6d 20 69 73 20   If the term is 
202ee 61 20 73 75 62 71 75 65 72 79 2c 20 74 68 65 6e  a subquery, then
202ef 20 70 53 75 62 71 75 65 72 79 20 69 73 20 74 68   pSubquery is th
202f0 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  e.** SELECT stat
202f1 65 6d 65 6e 74 20 74 68 61 74 20 74 68 65 20 73  ement that the s
202f2 75 62 71 75 65 72 79 20 65 6e 63 6f 64 65 73 2e  ubquery encodes.
202f3 20 20 54 68 65 20 70 54 61 62 6c 65 20 61 6e 64    The pTable and
202f4 0a 2a 2a 20 70 44 61 74 61 62 61 73 65 20 70 61  .** pDatabase pa
202f5 72 61 6d 65 74 65 72 73 20 61 72 65 20 4e 55 4c  rameters are NUL
202f6 4c 20 66 6f 72 20 73 75 62 71 75 65 72 69 65 73  L for subqueries
202f7 2e 20 20 54 68 65 20 70 4f 6e 20 61 6e 64 20 70  .  The pOn and p
202f8 55 73 69 6e 67 0a 2a 2a 20 70 61 72 61 6d 65 74  Using.** paramet
202f9 65 72 73 20 61 72 65 20 74 68 65 20 63 6f 6e 74  ers are the cont
202fa 65 6e 74 20 6f 66 20 74 68 65 20 4f 4e 20 61 6e  ent of the ON an
202fb 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 73 2e  d USING clauses.
202fc 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  .**.** Return a 
202fd 6e 65 77 20 53 72 63 4c 69 73 74 20 77 68 69 63  new SrcList whic
202fe 68 20 65 6e 63 6f 64 65 73 20 69 73 20 74 68 65  h encodes is the
202ff 20 46 52 4f 4d 20 77 69 74 68 20 74 68 65 20 6e   FROM with the n
20300 65 77 0a 2a 2a 20 74 65 72 6d 20 61 64 64 65 64  ew.** term added
20301 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
20302 41 54 45 20 53 72 63 4c 69 73 74 20 2a 73 71 6c  ATE SrcList *sql
20303 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
20304 64 46 72 6f 6d 54 65 72 6d 28 0a 20 20 50 61 72  dFromTerm(.  Par
20305 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
20306 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
20307 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63  context */.  Src
20308 4c 69 73 74 20 2a 70 2c 20 20 20 20 20 20 20 20  List *p,        
20309 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 65 66 74       /* The left
2030a 20 70 61 72 74 20 6f 66 20 74 68 65 20 46 52 4f   part of the FRO
2030b 4d 20 63 6c 61 75 73 65 20 61 6c 72 65 61 64 79  M clause already
2030c 20 73 65 65 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e   seen */.  Token
2030d 20 2a 70 54 61 62 6c 65 2c 20 20 20 20 20 20 20   *pTable,       
2030e 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68     /* Name of th
2030f 65 20 74 61 62 6c 65 20 74 6f 20 61 64 64 20 74  e table to add t
20310 6f 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  o the FROM claus
20311 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 44  e */.  Token *pD
20312 61 74 61 62 61 73 65 2c 20 20 20 20 20 20 20 2f  atabase,       /
20313 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 64 61  * Name of the da
20314 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
20315 67 20 70 54 61 62 6c 65 20 2a 2f 0a 20 20 54 6f  g pTable */.  To
20316 6b 65 6e 20 2a 70 41 6c 69 61 73 2c 20 20 20 20  ken *pAlias,    
20317 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67        /* The rig
20318 68 74 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20  ht-hand side of 
20319 74 68 65 20 41 53 20 73 75 62 65 78 70 72 65 73  the AS subexpres
2031a 73 69 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74  sion */.  Select
2031b 20 2a 70 53 75 62 71 75 65 72 79 2c 20 20 20 20   *pSubquery,    
2031c 20 20 2f 2a 20 41 20 73 75 62 71 75 65 72 79 20    /* A subquery 
2031d 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f 66  used in place of
2031e 20 61 20 74 61 62 6c 65 20 6e 61 6d 65 20 2a 2f   a table name */
2031f 0a 20 20 45 78 70 72 20 2a 70 4f 6e 2c 20 20 20  .  Expr *pOn,   
20320 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
20321 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20 61  e ON clause of a
20322 20 6a 6f 69 6e 20 2a 2f 0a 20 20 49 64 4c 69 73   join */.  IdLis
20323 74 20 2a 70 55 73 69 6e 67 20 20 20 20 20 20 20  t *pUsing       
20324 20 20 20 2f 2a 20 54 68 65 20 55 53 49 4e 47 20     /* The USING 
20325 63 6c 61 75 73 65 20 6f 66 20 61 20 6a 6f 69 6e  clause of a join
20326 20 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20   */.){.  struct 
20327 53 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49  SrcList_item *pI
20328 74 65 6d 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  tem;.  sqlite3 *
20329 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
2032a 0a 20 20 70 20 3d 20 73 71 6c 69 74 65 33 53 72  .  p = sqlite3Sr
2032b 63 4c 69 73 74 41 70 70 65 6e 64 28 64 62 2c 20  cListAppend(db, 
2032c 70 2c 20 70 54 61 62 6c 65 2c 20 70 44 61 74 61  p, pTable, pData
2032d 62 61 73 65 29 3b 0a 20 20 69 66 28 20 70 3d 3d  base);.  if( p==
2032e 30 20 7c 7c 20 70 2d 3e 6e 53 72 63 3d 3d 30 20  0 || p->nSrc==0 
2032f 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78  ){.    sqlite3Ex
20330 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 4f 6e  prDelete(db, pOn
20331 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 49 64  );.    sqlite3Id
20332 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
20333 55 73 69 6e 67 29 3b 0a 20 20 20 20 73 71 6c 69  Using);.    sqli
20334 74 65 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28  te3SelectDelete(
20335 64 62 2c 20 70 53 75 62 71 75 65 72 79 29 3b 0a  db, pSubquery);.
20336 20 20 20 20 72 65 74 75 72 6e 20 70 3b 0a 20 20      return p;.  
20337 7d 0a 20 20 70 49 74 65 6d 20 3d 20 26 70 2d 3e  }.  pItem = &p->
20338 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20  a[p->nSrc-1];.  
20339 69 66 28 20 70 41 6c 69 61 73 20 26 26 20 70 41  if( pAlias && pA
2033a 6c 69 61 73 2d 3e 6e 20 29 7b 0a 20 20 20 20 70  lias->n ){.    p
2033b 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 73  Item->zAlias = s
2033c 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f  qlite3NameFromTo
2033d 6b 65 6e 28 64 62 2c 20 70 41 6c 69 61 73 29 3b  ken(db, pAlias);
2033e 0a 20 20 7d 0a 20 20 70 49 74 65 6d 2d 3e 70 53  .  }.  pItem->pS
2033f 65 6c 65 63 74 20 3d 20 70 53 75 62 71 75 65 72  elect = pSubquer
20340 79 3b 0a 20 20 70 49 74 65 6d 2d 3e 70 4f 6e 20  y;.  pItem->pOn 
20341 3d 20 70 4f 6e 3b 0a 20 20 70 49 74 65 6d 2d 3e  = pOn;.  pItem->
20342 70 55 73 69 6e 67 20 3d 20 70 55 73 69 6e 67 3b  pUsing = pUsing;
20343 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a  .  return p;.}..
20344 2f 2a 0a 2a 2a 20 41 64 64 20 61 6e 20 49 4e 44  /*.** Add an IND
20345 45 58 45 44 20 42 59 20 6f 72 20 4e 4f 54 20 49  EXED BY or NOT I
20346 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20 74 6f  NDEXED clause to
20347 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
20348 6c 79 20 61 64 64 65 64 20 0a 2a 2a 20 65 6c 65  ly added .** ele
20349 6d 65 6e 74 20 6f 66 20 74 68 65 20 73 6f 75 72  ment of the sour
2034a 63 65 2d 6c 69 73 74 20 70 61 73 73 65 64 20 61  ce-list passed a
2034b 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
2034c 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ument..*/.SQLITE
2034d 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2034e 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65  lite3SrcListInde
2034f 78 65 64 42 79 28 50 61 72 73 65 20 2a 70 50 61  xedBy(Parse *pPa
20350 72 73 65 2c 20 53 72 63 4c 69 73 74 20 2a 70 2c  rse, SrcList *p,
20351 20 54 6f 6b 65 6e 20 2a 70 49 6e 64 65 78 65 64   Token *pIndexed
20352 42 79 29 7b 0a 20 20 69 66 28 20 70 49 6e 64 65  By){.  if( pInde
20353 78 65 64 42 79 20 26 26 20 70 20 26 26 20 70 2d  xedBy && p && p-
20354 3e 6e 53 72 63 3e 30 20 29 7b 0a 20 20 20 20 73  >nSrc>0 ){.    s
20355 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
20356 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e  em *pItem = &p->
20357 61 5b 70 2d 3e 6e 53 72 63 2d 31 5d 3b 0a 20 20  a[p->nSrc-1];.  
20358 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d    assert( pItem-
20359 3e 6e 6f 74 49 6e 64 65 78 65 64 3d 3d 30 20 26  >notIndexed==0 &
2035a 26 20 70 49 74 65 6d 2d 3e 7a 49 6e 64 65 78 3d  & pItem->zIndex=
2035b 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 49  =0 );.    if( pI
2035c 6e 64 65 78 65 64 42 79 2d 3e 6e 3d 3d 31 20 26  ndexedBy->n==1 &
2035d 26 20 21 70 49 6e 64 65 78 65 64 42 79 2d 3e 7a  & !pIndexedBy->z
2035e 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 41 20 22   ){.      /* A "
2035f 4e 4f 54 20 49 4e 44 45 58 45 44 22 20 63 6c 61  NOT INDEXED" cla
20360 75 73 65 20 77 61 73 20 73 75 70 70 6c 69 65 64  use was supplied
20361 2e 20 53 65 65 20 70 61 72 73 65 2e 79 20 0a 20  . See parse.y . 
20362 20 20 20 20 20 2a 2a 20 63 6f 6e 73 74 72 75 63       ** construc
20363 74 20 22 69 6e 64 65 78 65 64 5f 6f 70 74 22 20  t "indexed_opt" 
20364 66 6f 72 20 64 65 74 61 69 6c 73 2e 20 2a 2f 0a  for details. */.
20365 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 6e 6f 74        pItem->not
20366 49 6e 64 65 78 65 64 20 3d 20 31 3b 0a 20 20 20  Indexed = 1;.   
20367 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49   }else{.      pI
20368 74 65 6d 2d 3e 7a 49 6e 64 65 78 20 3d 20 73 71  tem->zIndex = sq
20369 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
2036a 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  en(pParse->db, p
2036b 49 6e 64 65 78 65 64 42 79 29 3b 0a 20 20 20 20  IndexedBy);.    
2036c 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 57  }.  }.}../*.** W
2036d 68 65 6e 20 62 75 69 6c 64 69 6e 67 20 75 70 20  hen building up 
2036e 61 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 69 6e  a FROM clause in
2036f 20 74 68 65 20 70 61 72 73 65 72 2c 20 74 68 65   the parser, the
20370 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f 72 0a 2a   join operator.*
20371 2a 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 20 61  * is initially a
20372 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c  ttached to the l
20373 65 66 74 20 6f 70 65 72 61 6e 64 2e 20 20 42 75  eft operand.  Bu
20374 74 20 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72  t the code gener
20375 61 74 6f 72 0a 2a 2a 20 65 78 70 65 63 74 73 20  ator.** expects 
20376 74 68 65 20 6a 6f 69 6e 20 6f 70 65 72 61 74 6f  the join operato
20377 72 20 74 6f 20 62 65 20 6f 6e 20 74 68 65 20 72  r to be on the r
20378 69 67 68 74 20 6f 70 65 72 61 6e 64 2e 20 20 54  ight operand.  T
20379 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 53  his routine.** S
2037a 68 69 66 74 73 20 61 6c 6c 20 6a 6f 69 6e 20 6f  hifts all join o
2037b 70 65 72 61 74 6f 72 73 20 66 72 6f 6d 20 6c 65  perators from le
2037c 66 74 20 74 6f 20 72 69 67 68 74 20 66 6f 72 20  ft to right for 
2037d 61 6e 20 65 6e 74 69 72 65 20 46 52 4f 4d 0a 2a  an entire FROM.*
2037e 2a 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20  * clause..**.** 
2037f 45 78 61 6d 70 6c 65 3a 20 53 75 70 70 6f 73 65  Example: Suppose
20380 20 74 68 65 20 6a 6f 69 6e 20 69 73 20 6c 69 6b   the join is lik
20381 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
20382 20 20 20 20 20 20 20 20 41 20 6e 61 74 75 72 61          A natura
20383 6c 20 63 72 6f 73 73 20 6a 6f 69 6e 20 42 0a 2a  l cross join B.*
20384 2a 0a 2a 2a 20 54 68 65 20 6f 70 65 72 61 74 6f  *.** The operato
20385 72 20 69 73 20 22 6e 61 74 75 72 61 6c 20 63 72  r is "natural cr
20386 6f 73 73 20 6a 6f 69 6e 22 2e 20 20 54 68 65 20  oss join".  The 
20387 41 20 61 6e 64 20 42 20 6f 70 65 72 61 6e 64 73  A and B operands
20388 20 61 72 65 20 73 74 6f 72 65 64 0a 2a 2a 20 69   are stored.** i
20389 6e 20 70 2d 3e 61 5b 30 5d 20 61 6e 64 20 70 2d  n p->a[0] and p-
2038a 3e 61 5b 31 5d 2c 20 72 65 73 70 65 63 74 69 76  >a[1], respectiv
2038b 65 6c 79 2e 20 20 54 68 65 20 70 61 72 73 65 72  ely.  The parser
2038c 20 69 6e 69 74 69 61 6c 6c 79 20 73 74 6f 72 65   initially store
2038d 73 20 74 68 65 0a 2a 2a 20 6f 70 65 72 61 74 6f  s the.** operato
2038e 72 20 77 69 74 68 20 41 2e 20 20 54 68 69 73 20  r with A.  This 
2038f 72 6f 75 74 69 6e 65 20 73 68 69 66 74 73 20 74  routine shifts t
20390 68 61 74 20 6f 70 65 72 61 74 6f 72 20 6f 76 65  hat operator ove
20391 72 20 74 6f 20 42 2e 0a 2a 2f 0a 53 51 4c 49 54  r to B..*/.SQLIT
20392 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20393 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53 68 69  qlite3SrcListShi
20394 66 74 4a 6f 69 6e 54 79 70 65 28 53 72 63 4c 69  ftJoinType(SrcLi
20395 73 74 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20  st *p){.  if( p 
20396 26 26 20 70 2d 3e 61 20 29 7b 0a 20 20 20 20 69  && p->a ){.    i
20397 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
20398 70 2d 3e 6e 53 72 63 2d 31 3b 20 69 3e 30 3b 20  p->nSrc-1; i>0; 
20399 69 2d 2d 29 7b 0a 20 20 20 20 20 20 70 2d 3e 61  i--){.      p->a
2039a 5b 69 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 70  [i].jointype = p
2039b 2d 3e 61 5b 69 2d 31 5d 2e 6a 6f 69 6e 74 79 70  ->a[i-1].jointyp
2039c 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 2d 3e  e;.    }.    p->
2039d 61 5b 30 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20  a[0].jointype = 
2039e 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  0;.  }.}../*.** 
2039f 42 65 67 69 6e 20 61 20 74 72 61 6e 73 61 63 74  Begin a transact
203a0 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ion.*/.SQLITE_PR
203a1 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
203a2 65 33 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69  e3BeginTransacti
203a3 6f 6e 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  on(Parse *pParse
203a4 2c 20 69 6e 74 20 74 79 70 65 29 7b 0a 20 20 73  , int type){.  s
203a5 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64  qlite3 *db;.  Vd
203a6 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 69 3b 0a  be *v;.  int i;.
203a7 0a 20 20 69 66 28 20 70 50 61 72 73 65 3d 3d 30  .  if( pParse==0
203a8 20 7c 7c 20 28 64 62 3d 70 50 61 72 73 65 2d 3e   || (db=pParse->
203a9 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d 3e 61 44  db)==0 || db->aD
203aa 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29 20 72 65  b[0].pBt==0 ) re
203ab 74 75 72 6e 3b 0a 20 20 69 66 28 20 70 50 61 72  turn;.  if( pPar
203ac 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
203ad 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
203ae 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c  eturn;.  if( sql
203af 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70 50  ite3AuthCheck(pP
203b0 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 54 52 41  arse, SQLITE_TRA
203b1 4e 53 41 43 54 49 4f 4e 2c 20 22 42 45 47 49 4e  NSACTION, "BEGIN
203b2 22 2c 20 30 2c 20 30 29 20 29 20 72 65 74 75 72  ", 0, 0) ) retur
203b3 6e 3b 0a 0a 20 20 76 20 3d 20 73 71 6c 69 74 65  n;..  v = sqlite
203b4 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
203b5 3b 0a 20 20 69 66 28 20 21 76 20 29 20 72 65 74  ;.  if( !v ) ret
203b6 75 72 6e 3b 0a 20 20 69 66 28 20 74 79 70 65 21  urn;.  if( type!
203b7 3d 54 4b 5f 44 45 46 45 52 52 45 44 20 29 7b 0a  =TK_DEFERRED ){.
203b8 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 64      for(i=0; i<d
203b9 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20  b->nDb; i++){.  
203ba 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
203bb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 54 72 61 6e  ddOp2(v, OP_Tran
203bc 73 61 63 74 69 6f 6e 2c 20 69 2c 20 28 74 79 70  saction, i, (typ
203bd 65 3d 3d 54 4b 5f 45 58 43 4c 55 53 49 56 45 29  e==TK_EXCLUSIVE)
203be 2b 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  +1);.      sqlit
203bf 65 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28  e3VdbeUsesBtree(
203c0 76 2c 20 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  v, i);.    }.  }
203c1 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
203c2 64 4f 70 32 28 76 2c 20 4f 50 5f 41 75 74 6f 43  dOp2(v, OP_AutoC
203c3 6f 6d 6d 69 74 2c 20 30 2c 20 30 29 3b 0a 7d 0a  ommit, 0, 0);.}.
203c4 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 69 74 20 61 20  ./*.** Commit a 
203c5 74 72 61 6e 73 61 63 74 69 6f 6e 0a 2a 2f 0a 53  transaction.*/.S
203c6 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
203c7 69 64 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74  id sqlite3Commit
203c8 54 72 61 6e 73 61 63 74 69 6f 6e 28 50 61 72 73  Transaction(Pars
203c9 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
203ca 6c 69 74 65 33 20 2a 64 62 3b 0a 20 20 56 64 62  lite3 *db;.  Vdb
203cb 65 20 2a 76 3b 0a 0a 20 20 69 66 28 20 70 50 61  e *v;..  if( pPa
203cc 72 73 65 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50  rse==0 || (db=pP
203cd 61 72 73 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20  arse->db)==0 || 
203ce 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d  db->aDb[0].pBt==
203cf 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66  0 ) return;.  if
203d0 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c  ( pParse->nErr |
203d1 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
203d2 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  ed ) return;.  i
203d3 66 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68  f( sqlite3AuthCh
203d4 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
203d5 54 45 5f 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20  TE_TRANSACTION, 
203d6 22 43 4f 4d 4d 49 54 22 2c 20 30 2c 20 30 29 20  "COMMIT", 0, 0) 
203d7 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d  ) return;..  v =
203d8 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
203d9 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76  pParse);.  if( v
203da 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56   ){.    sqlite3V
203db 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
203dc 41 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 30  AutoCommit, 1, 0
203dd 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
203de 52 6f 6c 6c 62 61 63 6b 20 61 20 74 72 61 6e 73  Rollback a trans
203df 61 63 74 69 6f 6e 0a 2a 2f 0a 53 51 4c 49 54 45  action.*/.SQLITE
203e0 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
203e1 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 54 72 61  lite3RollbackTra
203e2 6e 73 61 63 74 69 6f 6e 28 50 61 72 73 65 20 2a  nsaction(Parse *
203e3 70 50 61 72 73 65 29 7b 0a 20 20 73 71 6c 69 74  pParse){.  sqlit
203e4 65 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a  e3 *db;.  Vdbe *
203e5 76 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73 65  v;..  if( pParse
203e6 3d 3d 30 20 7c 7c 20 28 64 62 3d 70 50 61 72 73  ==0 || (db=pPars
203e7 65 2d 3e 64 62 29 3d 3d 30 20 7c 7c 20 64 62 2d  e->db)==0 || db-
203e8 3e 61 44 62 5b 30 5d 2e 70 42 74 3d 3d 30 20 29  >aDb[0].pBt==0 )
203e9 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 70   return;.  if( p
203ea 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64  Parse->nErr || d
203eb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
203ec 29 20 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20  ) return;.  if( 
203ed 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
203ee 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
203ef 54 52 41 4e 53 41 43 54 49 4f 4e 2c 20 22 52 4f  TRANSACTION, "RO
203f0 4c 4c 42 41 43 4b 22 2c 20 30 2c 20 30 29 20 29  LLBACK", 0, 0) )
203f1 20 72 65 74 75 72 6e 3b 0a 0a 20 20 76 20 3d 20   return;..  v = 
203f2 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70  sqlite3GetVdbe(p
203f3 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 76 20  Parse);.  if( v 
203f4 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
203f5 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41  beAddOp2(v, OP_A
203f6 75 74 6f 43 6f 6d 6d 69 74 2c 20 31 2c 20 31 29  utoCommit, 1, 1)
203f7 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4d  ;.  }.}../*.** M
203f8 61 6b 65 20 73 75 72 65 20 74 68 65 20 54 45 4d  ake sure the TEM
203f9 50 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70  P database is op
203fa 65 6e 20 61 6e 64 20 61 76 61 69 6c 61 62 6c 65  en and available
203fb 20 66 6f 72 20 75 73 65 2e 20 20 52 65 74 75 72   for use.  Retur
203fc 6e 0a 2a 2a 20 74 68 65 20 6e 75 6d 62 65 72 20  n.** the number 
203fd 6f 66 20 65 72 72 6f 72 73 2e 20 20 4c 65 61 76  of errors.  Leav
203fe 65 20 61 6e 79 20 65 72 72 6f 72 20 6d 65 73 73  e any error mess
203ff 61 67 65 73 20 69 6e 20 74 68 65 20 70 50 61 72  ages in the pPar
20400 73 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f  se structure..*/
20401 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20402 69 6e 74 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  int sqlite3OpenT
20403 65 6d 70 44 61 74 61 62 61 73 65 28 50 61 72 73  empDatabase(Pars
20404 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20 73 71  e *pParse){.  sq
20405 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
20406 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 64 62  se->db;.  if( db
20407 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 3d 3d 30 20  ->aDb[1].pBt==0 
20408 26 26 20 21 70 50 61 72 73 65 2d 3e 65 78 70 6c  && !pParse->expl
20409 61 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20 72  ain ){.    int r
2040a 63 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  c;.    static co
2040b 6e 73 74 20 69 6e 74 20 66 6c 61 67 73 20 3d 20  nst int flags = 
2040c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54  .          SQLIT
2040d 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52 49 54 45  E_OPEN_READWRITE
2040e 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c   |.          SQL
2040f 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 20  ITE_OPEN_CREATE 
20410 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51 4c 49  |.          SQLI
20411 54 45 5f 4f 50 45 4e 5f 45 58 43 4c 55 53 49 56  TE_OPEN_EXCLUSIV
20412 45 20 7c 0a 20 20 20 20 20 20 20 20 20 20 53 51  E |.          SQ
20413 4c 49 54 45 5f 4f 50 45 4e 5f 44 45 4c 45 54 45  LITE_OPEN_DELETE
20414 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20 20 20 20 20  ONCLOSE |.      
20415 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f      SQLITE_OPEN_
20416 54 45 4d 50 5f 44 42 3b 0a 0a 20 20 20 20 72 63  TEMP_DB;..    rc
20417 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 46   = sqlite3BtreeF
20418 61 63 74 6f 72 79 28 64 62 2c 20 30 2c 20 30 2c  actory(db, 0, 0,
20419 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
2041a 43 41 43 48 45 5f 53 49 5a 45 2c 20 66 6c 61 67  CACHE_SIZE, flag
2041b 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  s,.             
2041c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2041d 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 31 5d 2e      &db->aDb[1].
2041e 70 42 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63  pBt);.    if( rc
2041f 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
20420 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
20421 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e  rMsg(pParse, "un
20422 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20 61 20 74  able to open a t
20423 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
20424 65 20 22 0a 20 20 20 20 20 20 20 20 22 66 69 6c  e ".        "fil
20425 65 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 74 65  e for storing te
20426 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 22 29  mporary tables")
20427 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  ;.      pParse->
20428 72 63 20 3d 20 72 63 3b 0a 20 20 20 20 20 20 72  rc = rc;.      r
20429 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20  eturn 1;.    }. 
2042a 20 20 20 61 73 73 65 72 74 28 20 28 64 62 2d 3e     assert( (db->
2042b 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 49  flags & SQLITE_I
2042c 6e 54 72 61 6e 73 29 3d 3d 30 20 7c 7c 20 64 62  nTrans)==0 || db
2042d 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 3b 0a  ->autoCommit );.
2042e 20 20 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e      assert( db->
2042f 61 44 62 5b 31 5d 2e 70 53 63 68 65 6d 61 20 29  aDb[1].pSchema )
20430 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 50 61 67  ;.    sqlite3Pag
20431 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 73 71  erJournalMode(sq
20432 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
20433 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 2c  db->aDb[1].pBt),
20434 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
20435 20 20 20 20 20 20 20 20 20 20 20 20 20 64 62 2d               db-
20436 3e 64 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65  >dfltJournalMode
20437 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
20438 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65  0;.}../*.** Gene
20439 72 61 74 65 20 56 44 42 45 20 63 6f 64 65 20 74  rate VDBE code t
2043a 68 61 74 20 77 69 6c 6c 20 76 65 72 69 66 79 20  hat will verify 
2043b 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  the schema cooki
2043c 65 20 61 6e 64 20 73 74 61 72 74 0a 2a 2a 20 61  e and start.** a
2043d 20 72 65 61 64 2d 74 72 61 6e 73 61 63 74 69 6f   read-transactio
2043e 6e 20 66 6f 72 20 61 6c 6c 20 6e 61 6d 65 64 20  n for all named 
2043f 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 2e 0a  database files..
20440 2a 2a 0a 2a 2a 20 49 74 20 69 73 20 69 6d 70 6f  **.** It is impo
20441 72 74 61 6e 74 20 74 68 61 74 20 61 6c 6c 20 73  rtant that all s
20442 63 68 65 6d 61 20 63 6f 6f 6b 69 65 73 20 62 65  chema cookies be
20443 20 76 65 72 69 66 69 65 64 20 61 6e 64 20 61 6c   verified and al
20444 6c 0a 2a 2a 20 72 65 61 64 20 74 72 61 6e 73 61  l.** read transa
20445 63 74 69 6f 6e 73 20 62 65 20 73 74 61 72 74 65  ctions be starte
20446 64 20 62 65 66 6f 72 65 20 61 6e 79 74 68 69 6e  d before anythin
20447 67 20 65 6c 73 65 20 68 61 70 70 65 6e 73 20 69  g else happens i
20448 6e 0a 2a 2a 20 74 68 65 20 56 44 42 45 20 70 72  n.** the VDBE pr
20449 6f 67 72 61 6d 2e 20 20 42 75 74 20 74 68 69 73  ogram.  But this
2044a 20 72 6f 75 74 69 6e 65 20 63 61 6e 20 62 65 20   routine can be 
2044b 63 61 6c 6c 65 64 20 61 66 74 65 72 20 6d 75 63  called after muc
2044c 68 20 6f 74 68 65 72 0a 2a 2a 20 63 6f 64 65 20  h other.** code 
2044d 68 61 73 20 62 65 65 6e 20 67 65 6e 65 72 61 74  has been generat
2044e 65 64 2e 20 20 53 6f 20 68 65 72 65 20 69 73 20  ed.  So here is 
2044f 77 68 61 74 20 77 65 20 64 6f 3a 0a 2a 2a 0a 2a  what we do:.**.*
20450 2a 20 54 68 65 20 66 69 72 73 74 20 74 69 6d 65  * The first time
20451 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   this routine is
20452 20 63 61 6c 6c 65 64 2c 20 77 65 20 63 6f 64 65   called, we code
20453 20 61 6e 20 4f 50 5f 47 6f 74 6f 20 74 68 61 74   an OP_Goto that
20454 0a 2a 2a 20 77 69 6c 6c 20 6a 75 6d 70 20 74 6f  .** will jump to
20455 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 61 74   a subroutine at
20456 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
20457 70 72 6f 67 72 61 6d 2e 20 20 54 68 65 6e 20 77  program.  Then w
20458 65 0a 2a 2a 20 72 65 63 6f 72 64 20 65 76 65 72  e.** record ever
20459 79 20 64 61 74 61 62 61 73 65 20 74 68 61 74 20  y database that 
2045a 6e 65 65 64 73 20 69 74 73 20 73 63 68 65 6d 61  needs its schema
2045b 20 76 65 72 69 66 69 65 64 20 69 6e 20 74 68 65   verified in the
2045c 0a 2a 2a 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b  .** pParse->cook
2045d 69 65 4d 61 73 6b 20 66 69 65 6c 64 2e 20 20 4c  ieMask field.  L
2045e 61 74 65 72 2c 20 61 66 74 65 72 20 61 6c 6c 20  ater, after all 
2045f 6f 74 68 65 72 20 63 6f 64 65 20 68 61 73 20 62  other code has b
20460 65 65 6e 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64  een.** generated
20461 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  , the subroutine
20462 20 74 68 61 74 20 64 6f 65 73 20 74 68 65 20 63   that does the c
20463 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61 74 69  ookie verificati
20464 6f 6e 73 20 61 6e 64 0a 2a 2a 20 73 74 61 72 74  ons and.** start
20465 73 20 74 68 65 20 74 72 61 6e 73 61 63 74 69 6f  s the transactio
20466 6e 73 20 77 69 6c 6c 20 62 65 20 63 6f 64 65 64  ns will be coded
20467 20 61 6e 64 20 74 68 65 20 4f 50 5f 47 6f 74 6f   and the OP_Goto
20468 20 50 32 20 76 61 6c 75 65 0a 2a 2a 20 77 69 6c   P2 value.** wil
20469 6c 20 62 65 20 6d 61 64 65 20 74 6f 20 70 6f 69  l be made to poi
2046a 6e 74 20 74 6f 20 74 68 61 74 20 73 75 62 72 6f  nt to that subro
2046b 75 74 69 6e 65 2e 20 20 54 68 65 20 67 65 6e 65  utine.  The gene
2046c 72 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 2a 2a  ration of the.**
2046d 20 63 6f 6f 6b 69 65 20 76 65 72 69 66 69 63 61   cookie verifica
2046e 74 69 6f 6e 20 73 75 62 72 6f 75 74 69 6e 65 20  tion subroutine 
2046f 63 6f 64 65 20 68 61 70 70 65 6e 73 20 69 6e 20  code happens in 
20470 73 71 6c 69 74 65 33 46 69 6e 69 73 68 43 6f 64  sqlite3FinishCod
20471 69 6e 67 28 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ing()..**.** If 
20472 69 44 62 3c 30 20 74 68 65 6e 20 63 6f 64 65 20  iDb<0 then code 
20473 74 68 65 20 4f 50 5f 47 6f 74 6f 20 6f 6e 6c 79  the OP_Goto only
20474 20 2d 20 64 6f 6e 27 74 20 73 65 74 20 66 6c 61   - don't set fla
20475 67 20 74 6f 20 76 65 72 69 66 79 20 74 68 65 0a  g to verify the.
20476 2a 2a 20 73 63 68 65 6d 61 20 6f 6e 20 61 6e 79  ** schema on any
20477 20 64 61 74 61 62 61 73 65 73 2e 20 20 54 68 69   databases.  Thi
20478 73 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f  s can be used to
20479 20 70 6f 73 69 74 69 6f 6e 20 74 68 65 20 4f 50   position the OP
2047a 5f 47 6f 74 6f 0a 2a 2a 20 65 61 72 6c 79 20 69  _Goto.** early i
2047b 6e 20 74 68 65 20 63 6f 64 65 2c 20 62 65 66 6f  n the code, befo
2047c 72 65 20 77 65 20 6b 6e 6f 77 20 69 66 20 61 6e  re we know if an
2047d 79 20 64 61 74 61 62 61 73 65 20 74 61 62 6c 65  y database table
2047e 73 20 77 69 6c 6c 20 62 65 20 75 73 65 64 2e 0a  s will be used..
2047f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20480 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43 6f  E void sqlite3Co
20481 64 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 50  deVerifySchema(P
20482 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 69 6e  arse *pParse, in
20483 74 20 69 44 62 29 7b 0a 20 20 73 71 6c 69 74 65  t iDb){.  sqlite
20484 33 20 2a 64 62 3b 0a 20 20 56 64 62 65 20 2a 76  3 *db;.  Vdbe *v
20485 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 3b 0a 0a 20  ;.  int mask;.. 
20486 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56   v = sqlite3GetV
20487 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
20488 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( v==0 ) return
20489 3b 20 20 2f 2a 20 54 68 69 73 20 6f 6e 6c 79 20  ;  /* This only 
2048a 68 61 70 70 65 6e 73 20 69 66 20 74 68 65 72 65  happens if there
2048b 20 77 61 73 20 61 20 70 72 69 6f 72 20 65 72 72   was a prior err
2048c 6f 72 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50 61  or */.  db = pPa
2048d 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70  rse->db;.  if( p
2048e 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74  Parse->cookieGot
2048f 6f 3d 3d 30 20 29 7b 0a 20 20 20 20 70 50 61 72  o==0 ){.    pPar
20490 73 65 2d 3e 63 6f 6f 6b 69 65 47 6f 74 6f 20 3d  se->cookieGoto =
20491 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20492 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
20493 2c 20 30 29 2b 31 3b 0a 20 20 7d 0a 20 20 69 66  , 0)+1;.  }.  if
20494 28 20 69 44 62 3e 3d 30 20 29 7b 0a 20 20 20 20  ( iDb>=0 ){.    
20495 61 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e  assert( iDb<db->
20496 6e 44 62 20 29 3b 0a 20 20 20 20 61 73 73 65 72  nDb );.    asser
20497 74 28 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e  t( db->aDb[iDb].
20498 70 42 74 21 3d 30 20 7c 7c 20 69 44 62 3d 3d 31  pBt!=0 || iDb==1
20499 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2049a 69 44 62 3c 53 51 4c 49 54 45 5f 4d 41 58 5f 41  iDb<SQLITE_MAX_A
2049b 54 54 41 43 48 45 44 2b 32 20 29 3b 0a 20 20 20  TTACHED+2 );.   
2049c 20 6d 61 73 6b 20 3d 20 31 3c 3c 69 44 62 3b 0a   mask = 1<<iDb;.
2049d 20 20 20 20 69 66 28 20 28 70 50 61 72 73 65 2d      if( (pParse-
2049e 3e 63 6f 6f 6b 69 65 4d 61 73 6b 20 26 20 6d 61  >cookieMask & ma
2049f 73 6b 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  sk)==0 ){.      
204a0 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65 4d 61  pParse->cookieMa
204a1 73 6b 20 7c 3d 20 6d 61 73 6b 3b 0a 20 20 20 20  sk |= mask;.    
204a2 20 20 70 50 61 72 73 65 2d 3e 63 6f 6f 6b 69 65    pParse->cookie
204a3 56 61 6c 75 65 5b 69 44 62 5d 20 3d 20 64 62 2d  Value[iDb] = db-
204a4 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
204a5 61 2d 3e 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65  a->schema_cookie
204a6 3b 0a 20 20 20 20 20 20 69 66 28 20 21 4f 4d 49  ;.      if( !OMI
204a7 54 5f 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d  T_TEMPDB && iDb=
204a8 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  =1 ){.        sq
204a9 6c 69 74 65 33 4f 70 65 6e 54 65 6d 70 44 61 74  lite3OpenTempDat
204aa 61 62 61 73 65 28 70 50 61 72 73 65 29 3b 0a 20  abase(pParse);. 
204ab 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
204ac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
204ad 74 65 20 56 44 42 45 20 63 6f 64 65 20 74 68 61  te VDBE code tha
204ae 74 20 70 72 65 70 61 72 65 73 20 66 6f 72 20 64  t prepares for d
204af 6f 69 6e 67 20 61 6e 20 6f 70 65 72 61 74 69 6f  oing an operatio
204b0 6e 20 74 68 61 74 0a 2a 2a 20 6d 69 67 68 74 20  n that.** might 
204b1 63 68 61 6e 67 65 20 74 68 65 20 64 61 74 61 62  change the datab
204b2 61 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ase..**.** This 
204b3 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73 20 61  routine starts a
204b4 20 6e 65 77 20 74 72 61 6e 73 61 63 74 69 6f 6e   new transaction
204b5 20 69 66 20 77 65 20 61 72 65 20 6e 6f 74 20 61   if we are not a
204b6 6c 72 65 61 64 79 20 77 69 74 68 69 6e 0a 2a 2a  lready within.**
204b7 20 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 20   a transaction. 
204b8 20 49 66 20 77 65 20 61 72 65 20 61 6c 72 65 61   If we are alrea
204b9 64 79 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e  dy within a tran
204ba 73 61 63 74 69 6f 6e 2c 20 74 68 65 6e 20 61 20  saction, then a 
204bb 63 68 65 63 6b 70 6f 69 6e 74 0a 2a 2a 20 69 73  checkpoint.** is
204bc 20 73 65 74 20 69 66 20 74 68 65 20 73 65 74 53   set if the setS
204bd 74 61 74 65 6d 65 6e 74 20 70 61 72 61 6d 65 74  tatement paramet
204be 65 72 20 69 73 20 74 72 75 65 2e 20 20 41 20 63  er is true.  A c
204bf 68 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64  heckpoint should
204c0 0a 2a 2a 20 62 65 20 73 65 74 20 66 6f 72 20 6f  .** be set for o
204c1 70 65 72 61 74 69 6f 6e 73 20 74 68 61 74 20 6d  perations that m
204c2 69 67 68 74 20 66 61 69 6c 20 28 64 75 65 20 74  ight fail (due t
204c3 6f 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 29 20  o a constraint) 
204c4 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20 77  part of.** the w
204c5 61 79 20 74 68 72 6f 75 67 68 20 61 6e 64 20 77  ay through and w
204c6 68 69 63 68 20 77 69 6c 6c 20 6e 65 65 64 20 74  hich will need t
204c7 6f 20 75 6e 64 6f 20 73 6f 6d 65 20 77 72 69 74  o undo some writ
204c8 65 73 20 77 69 74 68 6f 75 74 20 68 61 76 69 6e  es without havin
204c9 67 20 74 6f 0a 2a 2a 20 72 6f 6c 6c 62 61 63 6b  g to.** rollback
204ca 20 74 68 65 20 77 68 6f 6c 65 20 74 72 61 6e 73   the whole trans
204cb 61 63 74 69 6f 6e 2e 20 20 46 6f 72 20 6f 70 65  action.  For ope
204cc 72 61 74 69 6f 6e 73 20 77 68 65 72 65 20 61 6c  rations where al
204cd 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 2a 2a  l constraints.**
204ce 20 63 61 6e 20 62 65 20 63 68 65 63 6b 65 64 20   can be checked 
204cf 62 65 66 6f 72 65 20 61 6e 79 20 63 68 61 6e 67  before any chang
204d0 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74  es are made to t
204d1 68 65 20 64 61 74 61 62 61 73 65 2c 20 69 74 20  he database, it 
204d2 69 73 20 6e 65 76 65 72 0a 2a 2a 20 6e 65 63 65  is never.** nece
204d3 73 73 61 72 79 20 74 6f 20 75 6e 64 6f 20 61 20  ssary to undo a 
204d4 77 72 69 74 65 20 61 6e 64 20 74 68 65 20 63 68  write and the ch
204d5 65 63 6b 70 6f 69 6e 74 20 73 68 6f 75 6c 64 20  eckpoint should 
204d6 6e 6f 74 20 62 65 20 73 65 74 2e 0a 2a 2a 0a 2a  not be set..**.*
204d7 2a 20 4f 6e 6c 79 20 64 61 74 61 62 61 73 65 20  * Only database 
204d8 69 44 62 20 61 6e 64 20 74 68 65 20 74 65 6d 70  iDb and the temp
204d9 20 64 61 74 61 62 61 73 65 20 61 72 65 20 6d 61   database are ma
204da 64 65 20 77 72 69 74 61 62 6c 65 20 62 79 20 74  de writable by t
204db 68 69 73 20 63 61 6c 6c 2e 0a 2a 2a 20 49 66 20  his call..** If 
204dc 69 44 62 3d 3d 30 2c 20 74 68 65 6e 20 74 68 65  iDb==0, then the
204dd 20 6d 61 69 6e 20 61 6e 64 20 74 65 6d 70 20 64   main and temp d
204de 61 74 61 62 61 73 65 73 20 61 72 65 20 6d 61 64  atabases are mad
204df 65 20 77 72 69 74 61 62 6c 65 2e 20 20 20 49 66  e writable.   If
204e0 0a 2a 2a 20 69 44 62 3d 3d 31 20 74 68 65 6e 20  .** iDb==1 then 
204e1 6f 6e 6c 79 20 74 68 65 20 74 65 6d 70 20 64 61  only the temp da
204e2 74 61 62 61 73 65 20 69 73 20 6d 61 64 65 20 77  tabase is made w
204e3 72 69 74 61 62 6c 65 2e 20 20 49 66 20 69 44 62  ritable.  If iDb
204e4 3e 31 20 74 68 65 6e 20 74 68 65 0a 2a 2a 20 73  >1 then the.** s
204e5 70 65 63 69 66 69 65 64 20 61 75 78 69 6c 69 61  pecified auxilia
204e6 72 79 20 64 61 74 61 62 61 73 65 20 61 6e 64 20  ry database and 
204e7 74 68 65 20 74 65 6d 70 20 64 61 74 61 62 61 73  the temp databas
204e8 65 20 61 72 65 20 6d 61 64 65 20 77 72 69 74 61  e are made writa
204e9 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
204ea 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
204eb 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65  te3BeginWriteOpe
204ec 72 61 74 69 6f 6e 28 50 61 72 73 65 20 2a 70 50  ration(Parse *pP
204ed 61 72 73 65 2c 20 69 6e 74 20 73 65 74 53 74 61  arse, int setSta
204ee 74 65 6d 65 6e 74 2c 20 69 6e 74 20 69 44 62 29  tement, int iDb)
204ef 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 73 71  {.  Vdbe *v = sq
204f0 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
204f1 72 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30  rse);.  if( v==0
204f2 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 71 6c   ) return;.  sql
204f3 69 74 65 33 43 6f 64 65 56 65 72 69 66 79 53 63  ite3CodeVerifySc
204f4 68 65 6d 61 28 70 50 61 72 73 65 2c 20 69 44 62  hema(pParse, iDb
204f5 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 77 72 69  );.  pParse->wri
204f6 74 65 4d 61 73 6b 20 7c 3d 20 31 3c 3c 69 44 62  teMask |= 1<<iDb
204f7 3b 0a 20 20 69 66 28 20 73 65 74 53 74 61 74 65  ;.  if( setState
204f8 6d 65 6e 74 20 26 26 20 70 50 61 72 73 65 2d 3e  ment && pParse->
204f9 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20  nested==0 ){.   
204fa 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
204fb 70 31 28 76 2c 20 4f 50 5f 53 74 61 74 65 6d 65  p1(v, OP_Stateme
204fc 6e 74 2c 20 69 44 62 29 3b 0a 20 20 7d 0a 20 20  nt, iDb);.  }.  
204fd 69 66 28 20 28 4f 4d 49 54 5f 54 45 4d 50 44 42  if( (OMIT_TEMPDB
204fe 20 7c 7c 20 69 44 62 21 3d 31 29 20 26 26 20 70   || iDb!=1) && p
204ff 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b 31  Parse->db->aDb[1
20500 5d 2e 70 42 74 21 3d 30 20 29 7b 0a 20 20 20 20  ].pBt!=0 ){.    
20501 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
20502 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
20503 65 2c 20 73 65 74 53 74 61 74 65 6d 65 6e 74 2c  e, setStatement,
20504 20 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a   1);.  }.}../*.*
20505 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
20506 66 20 70 49 6e 64 65 78 20 75 73 65 73 20 74 68  f pIndex uses th
20507 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  e collating sequ
20508 65 6e 63 65 20 70 43 6f 6c 6c 2e 20 20 52 65 74  ence pColl.  Ret
20509 75 72 6e 0a 2a 2a 20 74 72 75 65 20 69 66 20 69  urn.** true if i
2050a 74 20 64 6f 65 73 20 61 6e 64 20 66 61 6c 73 65  t does and false
2050b 20 69 66 20 69 74 20 64 6f 65 73 20 6e 6f 74 2e   if it does not.
2050c 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .*/.#ifndef SQLI
2050d 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58 0a  TE_OMIT_REINDEX.
2050e 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 61  static int colla
2050f 74 69 6f 6e 4d 61 74 63 68 28 63 6f 6e 73 74 20  tionMatch(const 
20510 63 68 61 72 20 2a 7a 43 6f 6c 6c 2c 20 49 6e 64  char *zColl, Ind
20511 65 78 20 2a 70 49 6e 64 65 78 29 7b 0a 20 20 69  ex *pIndex){.  i
20512 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
20513 20 69 3c 70 49 6e 64 65 78 2d 3e 6e 43 6f 6c 75   i<pIndex->nColu
20514 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f  mn; i++){.    co
20515 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 70 49  nst char *z = pI
20516 6e 64 65 78 2d 3e 61 7a 43 6f 6c 6c 5b 69 5d 3b  ndex->azColl[i];
20517 0a 20 20 20 20 69 66 28 20 7a 3d 3d 7a 43 6f 6c  .    if( z==zCol
20518 6c 20 7c 7c 20 28 7a 20 26 26 20 7a 43 6f 6c 6c  l || (z && zColl
20519 20 26 26 20 30 3d 3d 73 71 6c 69 74 65 33 53 74   && 0==sqlite3St
2051a 72 49 43 6d 70 28 7a 2c 20 7a 43 6f 6c 6c 29 29  rICmp(z, zColl))
2051b 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2051c 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
2051d 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65 6e 64  return 0;.}.#end
2051e 69 66 0a 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70  if../*.** Recomp
2051f 75 74 65 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  ute all indices 
20520 6f 66 20 70 54 61 62 20 74 68 61 74 20 75 73 65  of pTab that use
20521 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73   the collating s
20522 65 71 75 65 6e 63 65 20 70 43 6f 6c 6c 2e 0a 2a  equence pColl..*
20523 2a 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20 74 68  * If pColl==0 th
20524 65 6e 20 72 65 63 6f 6d 70 75 74 65 20 61 6c 6c  en recompute all
20525 20 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62   indices of pTab
20526 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c  ..*/.#ifndef SQL
20527 49 54 45 5f 4f 4d 49 54 5f 52 45 49 4e 44 45 58  ITE_OMIT_REINDEX
20528 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 69  .static void rei
20529 6e 64 65 78 54 61 62 6c 65 28 50 61 72 73 65 20  ndexTable(Parse 
2052a 2a 70 50 61 72 73 65 2c 20 54 61 62 6c 65 20 2a  *pParse, Table *
2052b 70 54 61 62 2c 20 63 68 61 72 20 63 6f 6e 73 74  pTab, char const
2052c 20 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 49 6e 64 65   *zColl){.  Inde
2052d 78 20 2a 70 49 6e 64 65 78 3b 20 20 20 20 20 20  x *pIndex;      
2052e 20 20 20 20 20 20 20 20 2f 2a 20 41 6e 20 69 6e          /* An in
2052f 64 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77  dex associated w
20530 69 74 68 20 70 54 61 62 20 2a 2f 0a 0a 20 20 66  ith pTab */..  f
20531 6f 72 28 70 49 6e 64 65 78 3d 70 54 61 62 2d 3e  or(pIndex=pTab->
20532 70 49 6e 64 65 78 3b 20 70 49 6e 64 65 78 3b 20  pIndex; pIndex; 
20533 70 49 6e 64 65 78 3d 70 49 6e 64 65 78 2d 3e 70  pIndex=pIndex->p
20534 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20 7a  Next){.    if( z
20535 43 6f 6c 6c 3d 3d 30 20 7c 7c 20 63 6f 6c 6c 61  Coll==0 || colla
20536 74 69 6f 6e 4d 61 74 63 68 28 7a 43 6f 6c 6c 2c  tionMatch(zColl,
20537 20 70 49 6e 64 65 78 29 20 29 7b 0a 20 20 20 20   pIndex) ){.    
20538 20 20 69 6e 74 20 69 44 62 20 3d 20 73 71 6c 69    int iDb = sqli
20539 74 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78  te3SchemaToIndex
2053a 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61  (pParse->db, pTa
2053b 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 20  b->pSchema);.   
2053c 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e 57     sqlite3BeginW
2053d 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70 50  riteOperation(pP
2053e 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20  arse, 0, iDb);. 
2053f 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 66 69       sqlite3Refi
20540 6c 6c 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  llIndex(pParse, 
20541 70 49 6e 64 65 78 2c 20 2d 31 29 3b 0a 20 20 20  pIndex, -1);.   
20542 20 7d 0a 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 0a   }.  }.}.#endif.
20543 0a 2f 2a 0a 2a 2a 20 52 65 63 6f 6d 70 75 74 65  ./*.** Recompute
20544 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 6f 66 20   all indices of 
20545 61 6c 6c 20 74 61 62 6c 65 73 20 69 6e 20 61 6c  all tables in al
20546 6c 20 64 61 74 61 62 61 73 65 73 20 77 68 65 72  l databases wher
20547 65 20 74 68 65 0a 2a 2a 20 69 6e 64 69 63 65 73  e the.** indices
20548 20 75 73 65 20 74 68 65 20 63 6f 6c 6c 61 74 69   use the collati
20549 6e 67 20 73 65 71 75 65 6e 63 65 20 70 43 6f 6c  ng sequence pCol
2054a 6c 2e 20 20 49 66 20 70 43 6f 6c 6c 3d 3d 30 20  l.  If pColl==0 
2054b 74 68 65 6e 20 72 65 63 6f 6d 70 75 74 65 0a 2a  then recompute.*
2054c 2a 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 65 76  * all indices ev
2054d 65 72 79 77 68 65 72 65 2e 0a 2a 2f 0a 23 69 66  erywhere..*/.#if
2054e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2054f 5f 52 45 49 4e 44 45 58 0a 73 74 61 74 69 63 20  _REINDEX.static 
20550 76 6f 69 64 20 72 65 69 6e 64 65 78 44 61 74 61  void reindexData
20551 62 61 73 65 73 28 50 61 72 73 65 20 2a 70 50 61  bases(Parse *pPa
20552 72 73 65 2c 20 63 68 61 72 20 63 6f 6e 73 74 20  rse, char const 
20553 2a 7a 43 6f 6c 6c 29 7b 0a 20 20 44 62 20 2a 70  *zColl){.  Db *p
20554 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
20555 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
20556 6c 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  le database */. 
20557 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20   int iDb;       
20558 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20559 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e 64  The database ind
2055a 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 73  ex number */.  s
2055b 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61  qlite3 *db = pPa
2055c 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54 68  rse->db;   /* Th
2055d 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  e database conne
2055e 63 74 69 6f 6e 20 2a 2f 0a 20 20 48 61 73 68 45  ction */.  HashE
2055f 6c 65 6d 20 2a 6b 3b 20 20 20 20 20 20 20 20 20  lem *k;         
20560 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f         /* For lo
20561 6f 70 69 6e 67 20 6f 76 65 72 20 74 61 62 6c 65  oping over table
20562 73 20 69 6e 20 70 44 62 20 2a 2f 0a 20 20 54 61  s in pDb */.  Ta
20563 62 6c 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20  ble *pTab;      
20564 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 20 74            /* A t
20565 61 62 6c 65 20 69 6e 20 74 68 65 20 64 61 74 61  able in the data
20566 62 61 73 65 20 2a 2f 0a 0a 20 20 66 6f 72 28 69  base */..  for(i
20567 44 62 3d 30 2c 20 70 44 62 3d 64 62 2d 3e 61 44  Db=0, pDb=db->aD
20568 62 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20  b; iDb<db->nDb; 
20569 69 44 62 2b 2b 2c 20 70 44 62 2b 2b 29 7b 0a 20  iDb++, pDb++){. 
2056a 20 20 20 61 73 73 65 72 74 28 20 70 44 62 21 3d     assert( pDb!=
2056b 30 20 29 3b 0a 20 20 20 20 66 6f 72 28 6b 3d 73  0 );.    for(k=s
2056c 71 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26  qliteHashFirst(&
2056d 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 74 62  pDb->pSchema->tb
2056e 6c 48 61 73 68 29 3b 20 20 6b 3b 20 6b 3d 73 71  lHash);  k; k=sq
2056f 6c 69 74 65 48 61 73 68 4e 65 78 74 28 6b 29 29  liteHashNext(k))
20570 7b 0a 20 20 20 20 20 20 70 54 61 62 20 3d 20 28  {.      pTab = (
20571 54 61 62 6c 65 2a 29 73 71 6c 69 74 65 48 61 73  Table*)sqliteHas
20572 68 44 61 74 61 28 6b 29 3b 0a 20 20 20 20 20 20  hData(k);.      
20573 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70 50 61  reindexTable(pPa
20574 72 73 65 2c 20 70 54 61 62 2c 20 7a 43 6f 6c 6c  rse, pTab, zColl
20575 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 23  );.    }.  }.}.#
20576 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  endif../*.** Gen
20577 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 74  erate code for t
20578 68 65 20 52 45 49 4e 44 45 58 20 63 6f 6d 6d 61  he REINDEX comma
20579 6e 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  nd..**.**       
2057a 20 52 45 49 4e 44 45 58 20 20 20 20 20 20 20 20   REINDEX        
2057b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2057c 20 20 20 20 2d 2d 20 31 0a 2a 2a 20 20 20 20 20      -- 1.**     
2057d 20 20 20 52 45 49 4e 44 45 58 20 20 3c 63 6f 6c     REINDEX  <col
2057e 6c 61 74 69 6f 6e 3e 20 20 20 20 20 20 20 20 20  lation>         
2057f 20 20 20 20 20 20 2d 2d 20 32 0a 2a 2a 20 20 20        -- 2.**   
20580 20 20 20 20 20 52 45 49 4e 44 45 58 20 20 3f 3c       REINDEX  ?<
20581 64 61 74 61 62 61 73 65 3e 2e 3f 3c 74 61 62 6c  database>.?<tabl
20582 65 6e 61 6d 65 3e 20 20 2d 2d 20 33 0a 2a 2a 20  ename>  -- 3.** 
20583 20 20 20 20 20 20 20 52 45 49 4e 44 45 58 20 20         REINDEX  
20584 3f 3c 64 61 74 61 62 61 73 65 3e 2e 3f 3c 69 6e  ?<database>.?<in
20585 64 65 78 6e 61 6d 65 3e 20 20 2d 2d 20 34 0a 2a  dexname>  -- 4.*
20586 2a 0a 2a 2a 20 46 6f 72 6d 20 31 20 63 61 75 73  *.** Form 1 caus
20587 65 73 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 69  es all indices i
20588 6e 20 61 6c 6c 20 61 74 74 61 63 68 65 64 20 64  n all attached d
20589 61 74 61 62 61 73 65 73 20 74 6f 20 62 65 20 72  atabases to be r
2058a 65 62 75 69 6c 74 2e 0a 2a 2a 20 46 6f 72 6d 20  ebuilt..** Form 
2058b 32 20 72 65 62 75 69 6c 64 73 20 61 6c 6c 20 69  2 rebuilds all i
2058c 6e 64 69 63 65 73 20 69 6e 20 61 6c 6c 20 64 61  ndices in all da
2058d 74 61 62 61 73 65 73 20 74 68 61 74 20 75 73 65  tabases that use
2058e 20 74 68 65 20 6e 61 6d 65 64 0a 2a 2a 20 63 6f   the named.** co
2058f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f 6e  llating function
20590 2e 20 20 46 6f 72 6d 73 20 33 20 61 6e 64 20 34  .  Forms 3 and 4
20591 20 72 65 62 75 69 6c 64 20 74 68 65 20 6e 61 6d   rebuild the nam
20592 65 64 20 69 6e 64 65 78 20 6f 72 20 61 6c 6c 0a  ed index or all.
20593 2a 2a 20 69 6e 64 69 63 65 73 20 61 73 73 6f 63  ** indices assoc
20594 69 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6e  iated with the n
20595 61 6d 65 64 20 74 61 62 6c 65 2e 0a 2a 2f 0a 23  amed table..*/.#
20596 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
20597 49 54 5f 52 45 49 4e 44 45 58 0a 53 51 4c 49 54  IT_REINDEX.SQLIT
20598 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
20599 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28 50 61  qlite3Reindex(Pa
2059a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b  rse *pParse, Tok
2059b 65 6e 20 2a 70 4e 61 6d 65 31 2c 20 54 6f 6b 65  en *pName1, Toke
2059c 6e 20 2a 70 4e 61 6d 65 32 29 7b 0a 20 20 43 6f  n *pName2){.  Co
2059d 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 20 20 20  llSeq *pColl;   
2059e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 6c            /* Col
2059f 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
205a0 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64 2c  to be reindexed,
205a1 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20 63 68   or NULL */.  ch
205a2 61 72 20 2a 7a 3b 20 20 20 20 20 20 20 20 20 20  ar *z;          
205a3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
205a4 65 20 6f 66 20 61 20 74 61 62 6c 65 20 6f 72 20  e of a table or 
205a5 69 6e 64 65 78 20 2a 2f 0a 20 20 63 6f 6e 73 74  index */.  const
205a6 20 63 68 61 72 20 2a 7a 44 62 3b 20 20 20 20 20   char *zDb;     
205a7 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
205a8 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 2a  f the database *
205a9 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
205aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
205ab 2f 2a 20 41 20 74 61 62 6c 65 20 69 6e 20 74 68  /* A table in th
205ac 65 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20 20  e database */.  
205ad 49 6e 64 65 78 20 2a 70 49 6e 64 65 78 3b 20 20  Index *pIndex;  
205ae 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
205af 6e 20 69 6e 64 65 78 20 61 73 73 6f 63 69 61 74  n index associat
205b0 65 64 20 77 69 74 68 20 70 54 61 62 20 2a 2f 0a  ed with pTab */.
205b1 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20 20 20    int iDb;      
205b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
205b3 20 54 68 65 20 64 61 74 61 62 61 73 65 20 69 6e   The database in
205b4 64 65 78 20 6e 75 6d 62 65 72 20 2a 2f 0a 20 20  dex number */.  
205b5 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
205b6 61 72 73 65 2d 3e 64 62 3b 20 20 20 2f 2a 20 54  arse->db;   /* T
205b7 68 65 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  he database conn
205b8 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65  ection */.  Toke
205b9 6e 20 2a 70 4f 62 6a 4e 61 6d 65 3b 20 20 20 20  n *pObjName;    
205ba 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
205bb 6f 66 20 74 68 65 20 74 61 62 6c 65 20 6f 72 20  of the table or 
205bc 69 6e 64 65 78 20 74 6f 20 62 65 20 72 65 69 6e  index to be rein
205bd 64 65 78 65 64 20 2a 2f 0a 0a 20 20 2f 2a 20 52  dexed */..  /* R
205be 65 61 64 20 74 68 65 20 64 61 74 61 62 61 73 65  ead the database
205bf 20 73 63 68 65 6d 61 2e 20 49 66 20 61 6e 20 65   schema. If an e
205c0 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 6c 65 61  rror occurs, lea
205c1 76 65 20 61 6e 20 65 72 72 6f 72 20 6d 65 73 73  ve an error mess
205c2 61 67 65 0a 20 20 2a 2a 20 61 6e 64 20 63 6f 64  age.  ** and cod
205c3 65 20 69 6e 20 70 50 61 72 73 65 20 61 6e 64 20  e in pParse and 
205c4 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 2a 2f 0a  return NULL. */.
205c5 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
205c6 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63 68 65  =sqlite3ReadSche
205c7 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20  ma(pParse) ){.  
205c8 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
205c9 20 69 66 28 20 70 4e 61 6d 65 31 3d 3d 30 20 7c   if( pName1==0 |
205ca 7c 20 70 4e 61 6d 65 31 2d 3e 7a 3d 3d 30 20 29  | pName1->z==0 )
205cb 7b 0a 20 20 20 20 72 65 69 6e 64 65 78 44 61 74  {.    reindexDat
205cc 61 62 61 73 65 73 28 70 50 61 72 73 65 2c 20 30  abases(pParse, 0
205cd 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
205ce 20 7d 65 6c 73 65 20 69 66 28 20 70 4e 61 6d 65   }else if( pName
205cf 32 3d 3d 30 20 7c 7c 20 70 4e 61 6d 65 32 2d 3e  2==0 || pName2->
205d0 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 63 68 61 72  z==0 ){.    char
205d1 20 2a 7a 43 6f 6c 6c 3b 0a 20 20 20 20 61 73 73   *zColl;.    ass
205d2 65 72 74 28 20 70 4e 61 6d 65 31 2d 3e 7a 20 29  ert( pName1->z )
205d3 3b 0a 20 20 20 20 7a 43 6f 6c 6c 20 3d 20 73 71  ;.    zColl = sq
205d4 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
205d5 65 6e 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70  en(pParse->db, p
205d6 4e 61 6d 65 31 29 3b 0a 20 20 20 20 69 66 28 20  Name1);.    if( 
205d7 21 7a 43 6f 6c 6c 20 29 20 72 65 74 75 72 6e 3b  !zColl ) return;
205d8 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c  .    pColl = sql
205d9 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
205da 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 43 6f  db, ENC(db), zCo
205db 6c 6c 2c 20 2d 31 2c 20 30 29 3b 0a 20 20 20 20  ll, -1, 0);.    
205dc 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
205dd 20 20 20 69 66 28 20 7a 43 6f 6c 6c 20 29 7b 0a     if( zColl ){.
205de 20 20 20 20 20 20 20 20 72 65 69 6e 64 65 78 44          reindexD
205df 61 74 61 62 61 73 65 73 28 70 50 61 72 73 65 2c  atabases(pParse,
205e0 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20 20 20   zColl);.       
205e1 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
205e2 62 2c 20 7a 43 6f 6c 6c 29 3b 0a 20 20 20 20 20  b, zColl);.     
205e3 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b   }.      return;
205e4 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
205e5 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 43 6f  e3DbFree(db, zCo
205e6 6c 6c 29 3b 0a 20 20 7d 0a 20 20 69 44 62 20 3d  ll);.  }.  iDb =
205e7 20 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e   sqlite3TwoPartN
205e8 61 6d 65 28 70 50 61 72 73 65 2c 20 70 4e 61 6d  ame(pParse, pNam
205e9 65 31 2c 20 70 4e 61 6d 65 32 2c 20 26 70 4f 62  e1, pName2, &pOb
205ea 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 69 44  jName);.  if( iD
205eb 62 3c 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  b<0 ) return;.  
205ec 7a 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d 65 46  z = sqlite3NameF
205ed 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4f 62  romToken(db, pOb
205ee 6a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 7a 3d  jName);.  if( z=
205ef 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a  =0 ) return;.  z
205f0 44 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  Db = db->aDb[iDb
205f1 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 70 54 61 62 20  ].zName;.  pTab 
205f2 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
205f3 6c 65 28 64 62 2c 20 7a 2c 20 7a 44 62 29 3b 0a  le(db, z, zDb);.
205f4 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a 20 20    if( pTab ){.  
205f5 20 20 72 65 69 6e 64 65 78 54 61 62 6c 65 28 70    reindexTable(p
205f6 50 61 72 73 65 2c 20 70 54 61 62 2c 20 30 29 3b  Parse, pTab, 0);
205f7 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72  .    sqlite3DbFr
205f8 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 20 20 72  ee(db, z);.    r
205f9 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 49 6e  eturn;.  }.  pIn
205fa 64 65 78 20 3d 20 73 71 6c 69 74 65 33 46 69 6e  dex = sqlite3Fin
205fb 64 49 6e 64 65 78 28 64 62 2c 20 7a 2c 20 7a 44  dIndex(db, z, zD
205fc 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  b);.  sqlite3DbF
205fd 72 65 65 28 64 62 2c 20 7a 29 3b 0a 20 20 69 66  ree(db, z);.  if
205fe 28 20 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20  ( pIndex ){.    
205ff 73 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74  sqlite3BeginWrit
20600 65 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73  eOperation(pPars
20601 65 2c 20 30 2c 20 69 44 62 29 3b 0a 20 20 20 20  e, 0, iDb);.    
20602 73 71 6c 69 74 65 33 52 65 66 69 6c 6c 49 6e 64  sqlite3RefillInd
20603 65 78 28 70 50 61 72 73 65 2c 20 70 49 6e 64 65  ex(pParse, pInde
20604 78 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75  x, -1);.    retu
20605 72 6e 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  rn;.  }.  sqlite
20606 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65  3ErrorMsg(pParse
20607 2c 20 22 75 6e 61 62 6c 65 20 74 6f 20 69 64 65  , "unable to ide
20608 6e 74 69 66 79 20 74 68 65 20 6f 62 6a 65 63 74  ntify the object
20609 20 74 6f 20 62 65 20 72 65 69 6e 64 65 78 65 64   to be reindexed
2060a 22 29 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a  ");.}.#endif../*
2060b 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e  .** Return a dyn
2060c 61 6d 69 63 6c 79 20 61 6c 6c 6f 63 61 74 65 64  amicly allocated
2060d 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
2060e 72 65 20 74 68 61 74 20 63 61 6e 20 62 65 20 75  re that can be u
2060f 73 65 64 0a 2a 2a 20 77 69 74 68 20 4f 50 5f 4f  sed.** with OP_O
20610 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70  penRead or OP_Op
20611 65 6e 57 72 69 74 65 20 74 6f 20 61 63 63 65 73  enWrite to acces
20612 73 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  s database index
20613 20 70 49 64 78 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   pIdx..**.** If 
20614 73 75 63 63 65 73 73 66 75 6c 2c 20 61 20 70 6f  successful, a po
20615 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
20616 20 73 74 72 75 63 74 75 72 65 20 69 73 20 72 65   structure is re
20617 74 75 72 6e 65 64 2e 20 49 6e 20 74 68 69 73 20  turned. In this 
20618 63 61 73 65 0a 2a 2a 20 74 68 65 20 63 61 6c 6c  case.** the call
20619 65 72 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  er is responsibl
2061a 65 20 66 6f 72 20 63 61 6c 6c 69 6e 67 20 73 71  e for calling sq
2061b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2061c 29 20 6f 6e 20 74 68 65 20 72 65 74 75 72 6e 65  ) on the returne
2061d 64 20 0a 2a 2a 20 70 6f 69 6e 74 65 72 2e 20 49  d .** pointer. I
2061e 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
2061f 73 20 28 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79  s (out of memory
20620 20 6f 72 20 6d 69 73 73 69 6e 67 20 63 6f 6c 6c   or missing coll
20621 61 74 69 6f 6e 20 0a 2a 2a 20 73 65 71 75 65 6e  ation .** sequen
20622 63 65 29 2c 20 4e 55 4c 4c 20 69 73 20 72 65 74  ce), NULL is ret
20623 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 73 74  urned and the st
20624 61 74 65 20 6f 66 20 70 50 61 72 73 65 20 75 70  ate of pParse up
20625 64 61 74 65 64 20 74 6f 20 72 65 66 6c 65 63 74  dated to reflect
20626 0a 2a 2a 20 74 68 65 20 65 72 72 6f 72 2e 0a 2a  .** the error..*
20627 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20628 20 4b 65 79 49 6e 66 6f 20 2a 73 71 6c 69 74 65   KeyInfo *sqlite
20629 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28 50 61  3IndexKeyinfo(Pa
2062a 72 73 65 20 2a 70 50 61 72 73 65 2c 20 49 6e 64  rse *pParse, Ind
2062b 65 78 20 2a 70 49 64 78 29 7b 0a 20 20 69 6e 74  ex *pIdx){.  int
2062c 20 69 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 20 3d   i;.  int nCol =
2062d 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a   pIdx->nColumn;.
2062e 20 20 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73    int nBytes = s
2062f 69 7a 65 6f 66 28 4b 65 79 49 6e 66 6f 29 20 2b  izeof(KeyInfo) +
20630 20 28 6e 43 6f 6c 2d 31 29 2a 73 69 7a 65 6f 66   (nCol-1)*sizeof
20631 28 43 6f 6c 6c 53 65 71 2a 29 20 2b 20 6e 43 6f  (CollSeq*) + nCo
20632 6c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  l;.  sqlite3 *db
20633 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
20634 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
20635 20 28 4b 65 79 49 6e 66 6f 20 2a 29 73 71 6c 69   (KeyInfo *)sqli
20636 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
20637 64 62 2c 20 6e 42 79 74 65 73 29 3b 0a 0a 20 20  db, nBytes);..  
20638 69 66 28 20 70 4b 65 79 20 29 7b 0a 20 20 20 20  if( pKey ){.    
20639 70 4b 65 79 2d 3e 64 62 20 3d 20 70 50 61 72 73  pKey->db = pPars
2063a 65 2d 3e 64 62 3b 0a 20 20 20 20 70 4b 65 79 2d  e->db;.    pKey-
2063b 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20 28 75  >aSortOrder = (u
2063c 38 20 2a 29 26 28 70 4b 65 79 2d 3e 61 43 6f 6c  8 *)&(pKey->aCol
2063d 6c 5b 6e 43 6f 6c 5d 29 3b 0a 20 20 20 20 61 73  l[nCol]);.    as
2063e 73 65 72 74 28 20 26 70 4b 65 79 2d 3e 61 53 6f  sert( &pKey->aSo
2063f 72 74 4f 72 64 65 72 5b 6e 43 6f 6c 5d 3d 3d 26  rtOrder[nCol]==&
20640 28 28 28 75 38 20 2a 29 70 4b 65 79 29 5b 6e 42  (((u8 *)pKey)[nB
20641 79 74 65 73 5d 29 20 29 3b 0a 20 20 20 20 66 6f  ytes]) );.    fo
20642 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
20643 2b 2b 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20  ++){.      char 
20644 2a 7a 43 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61  *zColl = pIdx->a
20645 7a 43 6f 6c 6c 5b 69 5d 3b 0a 20 20 20 20 20 20  zColl[i];.      
20646 61 73 73 65 72 74 28 20 7a 43 6f 6c 6c 20 29 3b  assert( zColl );
20647 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 43 6f  .      pKey->aCo
20648 6c 6c 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33 4c  ll[i] = sqlite3L
20649 6f 63 61 74 65 43 6f 6c 6c 53 65 71 28 70 50 61  ocateCollSeq(pPa
2064a 72 73 65 2c 20 7a 43 6f 6c 6c 2c 20 2d 31 29 3b  rse, zColl, -1);
2064b 0a 20 20 20 20 20 20 70 4b 65 79 2d 3e 61 53 6f  .      pKey->aSo
2064c 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 70 49 64  rtOrder[i] = pId
2064d 78 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d  x->aSortOrder[i]
2064e 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4b 65 79  ;.    }.    pKey
2064f 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 43 6f 6c 3b  ->nField = nCol;
20650 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70 50 61 72  .  }..  if( pPar
20651 73 65 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20  se->nErr ){.    
20652 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
20653 2c 20 70 4b 65 79 29 3b 0a 20 20 20 20 70 4b 65  , pKey);.    pKe
20654 79 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  y = 0;.  }.  ret
20655 75 72 6e 20 70 4b 65 79 3b 0a 7d 0a 0a 2f 2a 2a  urn pKey;.}../**
20656 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
20657 20 6f 66 20 62 75 69 6c 64 2e 63 20 2a 2a 2a 2a   of build.c ****
20658 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20659 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2065a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
2065b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
2065c 69 6e 20 66 69 6c 65 20 63 61 6c 6c 62 61 63 6b  in file callback
2065d 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2065e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2065f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
20660 2a 2a 20 32 30 30 35 20 4d 61 79 20 32 33 20 0a  ** 2005 May 23 .
20661 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
20662 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
20663 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
20664 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
20665 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
20666 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
20667 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
20668 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
20669 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2066a 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2066b 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2066c 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2066d 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2066e 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2066f 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
20670 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
20671 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
20672 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
20673 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20674 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20675 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20676 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
20677 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
20678 6f 6e 74 61 69 6e 73 20 66 75 6e 63 74 69 6f 6e  ontains function
20679 73 20 75 73 65 64 20 74 6f 20 61 63 63 65 73 73  s used to access
2067a 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 68 61   the internal ha
2067b 73 68 20 74 61 62 6c 65 73 0a 2a 2a 20 6f 66 20  sh tables.** of 
2067c 75 73 65 72 20 64 65 66 69 6e 65 64 20 66 75 6e  user defined fun
2067d 63 74 69 6f 6e 73 20 61 6e 64 20 63 6f 6c 6c 61  ctions and colla
2067e 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2e 0a  tion sequences..
2067f 2a 2a 0a 2a 2a 20 24 49 64 3a 20 63 61 6c 6c 62  **.** $Id: callb
20680 61 63 6b 2e 63 2c 76 20 31 2e 33 32 20 32 30 30  ack.c,v 1.32 200
20681 38 2f 31 30 2f 31 30 20 31 37 3a 34 31 3a 32 39  8/10/10 17:41:29
20682 20 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a   drh Exp $.*/...
20683 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20 74 68 65  /*.** Invoke the
20684 20 27 63 6f 6c 6c 61 74 69 6f 6e 20 6e 65 65 64   'collation need
20685 65 64 27 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20  ed' callback to 
20686 72 65 71 75 65 73 74 20 61 20 63 6f 6c 6c 61 74  request a collat
20687 69 6f 6e 20 73 65 71 75 65 6e 63 65 0a 2a 2a 20  ion sequence.** 
20688 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20  in the database 
20689 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 6f 66  text encoding of
2068a 20 6e 61 6d 65 20 7a 4e 61 6d 65 2c 20 6c 65 6e   name zName, len
2068b 67 74 68 20 6e 4e 61 6d 65 2e 0a 2a 2a 20 49 66  gth nName..** If
2068c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 73   the collation s
2068d 65 71 75 65 6e 63 65 0a 2a 2f 0a 73 74 61 74 69  equence.*/.stati
2068e 63 20 76 6f 69 64 20 63 61 6c 6c 43 6f 6c 6c 4e  c void callCollN
2068f 65 65 64 65 64 28 73 71 6c 69 74 65 33 20 2a 64  eeded(sqlite3 *d
20690 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
20691 4e 61 6d 65 2c 20 69 6e 74 20 6e 4e 61 6d 65 29  Name, int nName)
20692 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62 2d  {.  assert( !db-
20693 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 7c 7c 20  >xCollNeeded || 
20694 21 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64  !db->xCollNeeded
20695 31 36 20 29 3b 0a 20 20 69 66 28 20 6e 4e 61 6d  16 );.  if( nNam
20696 65 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 71  e<0 ) nName = sq
20697 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20  lite3Strlen(db, 
20698 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 64 62  zName);.  if( db
20699 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65 64 20 29 7b  ->xCollNeeded ){
2069a 0a 20 20 20 20 63 68 61 72 20 2a 7a 45 78 74 65  .    char *zExte
2069b 72 6e 61 6c 20 3d 20 73 71 6c 69 74 65 33 44 62  rnal = sqlite3Db
2069c 53 74 72 4e 44 75 70 28 64 62 2c 20 7a 4e 61 6d  StrNDup(db, zNam
2069d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 69  e, nName);.    i
2069e 66 28 20 21 7a 45 78 74 65 72 6e 61 6c 20 29 20  f( !zExternal ) 
2069f 72 65 74 75 72 6e 3b 0a 20 20 20 20 64 62 2d 3e  return;.    db->
206a0 78 43 6f 6c 6c 4e 65 65 64 65 64 28 64 62 2d 3e  xCollNeeded(db->
206a1 70 43 6f 6c 6c 4e 65 65 64 65 64 41 72 67 2c 20  pCollNeededArg, 
206a2 64 62 2c 20 28 69 6e 74 29 45 4e 43 28 64 62 29  db, (int)ENC(db)
206a3 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a 20 20  , zExternal);.  
206a4 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
206a5 64 62 2c 20 7a 45 78 74 65 72 6e 61 6c 29 3b 0a  db, zExternal);.
206a6 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49    }.#ifndef SQLI
206a7 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 20 20  TE_OMIT_UTF16.  
206a8 69 66 28 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65  if( db->xCollNee
206a9 64 65 64 31 36 20 29 7b 0a 20 20 20 20 63 68 61  ded16 ){.    cha
206aa 72 20 63 6f 6e 73 74 20 2a 7a 45 78 74 65 72 6e  r const *zExtern
206ab 61 6c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  al;.    sqlite3_
206ac 76 61 6c 75 65 20 2a 70 54 6d 70 20 3d 20 73 71  value *pTmp = sq
206ad 6c 69 74 65 33 56 61 6c 75 65 4e 65 77 28 64 62  lite3ValueNew(db
206ae 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 61  );.    sqlite3Va
206af 6c 75 65 53 65 74 53 74 72 28 70 54 6d 70 2c 20  lueSetStr(pTmp, 
206b0 6e 4e 61 6d 65 2c 20 7a 4e 61 6d 65 2c 20 53 51  nName, zName, SQ
206b1 4c 49 54 45 5f 55 54 46 38 2c 20 53 51 4c 49 54  LITE_UTF8, SQLIT
206b2 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7a  E_STATIC);.    z
206b3 45 78 74 65 72 6e 61 6c 20 3d 20 73 71 6c 69 74  External = sqlit
206b4 65 33 56 61 6c 75 65 54 65 78 74 28 70 54 6d 70  e3ValueText(pTmp
206b5 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  , SQLITE_UTF16NA
206b6 54 49 56 45 29 3b 0a 20 20 20 20 69 66 28 20 7a  TIVE);.    if( z
206b7 45 78 74 65 72 6e 61 6c 20 29 7b 0a 20 20 20 20  External ){.    
206b8 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64 65    db->xCollNeede
206b9 64 31 36 28 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65  d16(db->pCollNee
206ba 64 65 64 41 72 67 2c 20 64 62 2c 20 28 69 6e 74  dedArg, db, (int
206bb 29 45 4e 43 28 64 62 29 2c 20 7a 45 78 74 65 72  )ENC(db), zExter
206bc 6e 61 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  nal);.    }.    
206bd 73 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65  sqlite3ValueFree
206be 28 70 54 6d 70 29 3b 0a 20 20 7d 0a 23 65 6e 64  (pTmp);.  }.#end
206bf 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  if.}../*.** This
206c0 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
206c1 65 64 20 69 66 20 74 68 65 20 63 6f 6c 6c 61 74  ed if the collat
206c2 69 6f 6e 20 66 61 63 74 6f 72 79 20 66 61 69 6c  ion factory fail
206c3 73 20 74 6f 20 64 65 6c 69 76 65 72 20 61 0a 2a  s to deliver a.*
206c4 2a 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 75 6e 63  * collation func
206c5 74 69 6f 6e 20 69 6e 20 74 68 65 20 62 65 73 74  tion in the best
206c6 20 65 6e 63 6f 64 69 6e 67 20 62 75 74 20 74 68   encoding but th
206c7 65 72 65 20 6d 61 79 20 62 65 20 6f 74 68 65 72  ere may be other
206c8 20 76 65 72 73 69 6f 6e 73 0a 2a 2a 20 6f 66 20   versions.** of 
206c9 74 68 69 73 20 63 6f 6c 6c 61 74 69 6f 6e 20 66  this collation f
206ca 75 6e 63 74 69 6f 6e 20 28 66 6f 72 20 6f 74 68  unction (for oth
206cb 65 72 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67  er text encoding
206cc 73 29 20 61 76 61 69 6c 61 62 6c 65 2e 20 55 73  s) available. Us
206cd 65 20 6f 6e 65 0a 2a 2a 20 6f 66 20 74 68 65 73  e one.** of thes
206ce 65 20 69 6e 73 74 65 61 64 20 69 66 20 74 68 65  e instead if the
206cf 79 20 65 78 69 73 74 2e 20 41 76 6f 69 64 20 61  y exist. Avoid a
206d0 20 55 54 46 2d 38 20 3c 2d 3e 20 55 54 46 2d 31   UTF-8 <-> UTF-1
206d1 36 20 63 6f 6e 76 65 72 73 69 6f 6e 20 69 66 0a  6 conversion if.
206d2 2a 2a 20 70 6f 73 73 69 62 6c 65 2e 0a 2a 2f 0a  ** possible..*/.
206d3 73 74 61 74 69 63 20 69 6e 74 20 73 79 6e 74 68  static int synth
206d4 43 6f 6c 6c 53 65 71 28 73 71 6c 69 74 65 33 20  CollSeq(sqlite3 
206d5 2a 64 62 2c 20 43 6f 6c 6c 53 65 71 20 2a 70 43  *db, CollSeq *pC
206d6 6f 6c 6c 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20  oll){.  CollSeq 
206d7 2a 70 43 6f 6c 6c 32 3b 0a 20 20 63 68 61 72 20  *pColl2;.  char 
206d8 2a 7a 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  *z = pColl->zNam
206d9 65 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 73 74 72  e;.  int n = str
206da 6c 65 6e 28 7a 29 3b 0a 20 20 69 6e 74 20 69 3b  len(z);.  int i;
206db 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
206dc 75 38 20 61 45 6e 63 5b 5d 20 3d 20 7b 20 53 51  u8 aEnc[] = { SQ
206dd 4c 49 54 45 5f 55 54 46 31 36 42 45 2c 20 53 51  LITE_UTF16BE, SQ
206de 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c 20 53 51  LITE_UTF16LE, SQ
206df 4c 49 54 45 5f 55 54 46 38 20 7d 3b 0a 20 20 66  LITE_UTF8 };.  f
206e0 6f 72 28 69 3d 30 3b 20 69 3c 33 3b 20 69 2b 2b  or(i=0; i<3; i++
206e1 29 7b 0a 20 20 20 20 70 43 6f 6c 6c 32 20 3d 20  ){.    pColl2 = 
206e2 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
206e3 65 71 28 64 62 2c 20 61 45 6e 63 5b 69 5d 2c 20  eq(db, aEnc[i], 
206e4 7a 2c 20 6e 2c 20 30 29 3b 0a 20 20 20 20 69 66  z, n, 0);.    if
206e5 28 20 70 43 6f 6c 6c 32 2d 3e 78 43 6d 70 21 3d  ( pColl2->xCmp!=
206e6 30 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70  0 ){.      memcp
206e7 79 28 70 43 6f 6c 6c 2c 20 70 43 6f 6c 6c 32 2c  y(pColl, pColl2,
206e8 20 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71 29   sizeof(CollSeq)
206e9 29 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 2d 3e  );.      pColl->
206ea 78 44 65 6c 20 3d 20 30 3b 20 20 20 20 20 20 20  xDel = 0;       
206eb 20 20 2f 2a 20 44 6f 20 6e 6f 74 20 63 6f 70 79    /* Do not copy
206ec 20 74 68 65 20 64 65 73 74 72 75 63 74 6f 72 20   the destructor 
206ed 2a 2f 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  */.      return 
206ee 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
206ef 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
206f0 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 7d 0a 0a 2f  LITE_ERROR;.}../
206f1 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69  *.** This functi
206f2 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
206f3 65 20 66 6f 72 20 69 6e 76 6f 6b 69 6e 67 20 74  e for invoking t
206f4 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20 66 61 63  he collation fac
206f5 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a  tory callback.**
206f6 20 6f 72 20 73 75 62 73 74 69 74 75 74 69 6e 67   or substituting
206f7 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71   a collation seq
206f8 75 65 6e 63 65 20 6f 66 20 61 20 64 69 66 66 65  uence of a diffe
206f9 72 65 6e 74 20 65 6e 63 6f 64 69 6e 67 20 77 68  rent encoding wh
206fa 65 6e 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73  en the.** reques
206fb 74 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  ted collation se
206fc 71 75 65 6e 63 65 20 69 73 20 6e 6f 74 20 61 76  quence is not av
206fd 61 69 6c 61 62 6c 65 20 69 6e 20 74 68 65 20 64  ailable in the d
206fe 61 74 61 62 61 73 65 20 6e 61 74 69 76 65 0a 2a  atabase native.*
206ff 2a 20 65 6e 63 6f 64 69 6e 67 2e 0a 2a 2a 20 0a  * encoding..** .
20700 2a 2a 20 49 66 20 69 74 20 69 73 20 6e 6f 74 20  ** If it is not 
20701 4e 55 4c 4c 2c 20 74 68 65 6e 20 70 43 6f 6c 6c  NULL, then pColl
20702 20 6d 75 73 74 20 70 6f 69 6e 74 20 74 6f 20 74   must point to t
20703 68 65 20 64 61 74 61 62 61 73 65 20 6e 61 74 69  he database nati
20704 76 65 20 65 6e 63 6f 64 69 6e 67 20 0a 2a 2a 20  ve encoding .** 
20705 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
20706 63 65 20 77 69 74 68 20 6e 61 6d 65 20 7a 4e 61  ce with name zNa
20707 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61 6d 65  me, length nName
20708 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
20709 72 6e 20 76 61 6c 75 65 20 69 73 20 65 69 74 68  rn value is eith
2070a 65 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  er the collation
2070b 20 73 65 71 75 65 6e 63 65 20 74 6f 20 62 65 20   sequence to be 
2070c 75 73 65 64 20 69 6e 20 64 61 74 61 62 61 73 65  used in database
2070d 0a 2a 2a 20 64 62 20 66 6f 72 20 63 6f 6c 6c 61  .** db for colla
2070e 74 69 6f 6e 20 74 79 70 65 20 6e 61 6d 65 20 7a  tion type name z
2070f 4e 61 6d 65 2c 20 6c 65 6e 67 74 68 20 6e 4e 61  Name, length nNa
20710 6d 65 2c 20 6f 72 20 4e 55 4c 4c 2c 20 69 66 20  me, or NULL, if 
20711 6e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 0a 2a 2a 20  no collation.** 
20712 73 65 71 75 65 6e 63 65 20 63 61 6e 20 62 65 20  sequence can be 
20713 66 6f 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45  found..*/.SQLITE
20714 5f 50 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71  _PRIVATE CollSeq
20715 20 2a 73 71 6c 69 74 65 33 47 65 74 43 6f 6c 6c   *sqlite3GetColl
20716 53 65 71 28 0a 20 20 73 71 6c 69 74 65 33 2a 20  Seq(.  sqlite3* 
20717 64 62 2c 20 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  db, .  CollSeq *
20718 70 43 6f 6c 6c 2c 20 0a 20 20 63 6f 6e 73 74 20  pColl, .  const 
20719 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20 20  char *zName, .  
2071a 69 6e 74 20 6e 4e 61 6d 65 0a 29 7b 0a 20 20 43  int nName.){.  C
2071b 6f 6c 6c 53 65 71 20 2a 70 3b 0a 0a 20 20 70 20  ollSeq *p;..  p 
2071c 3d 20 70 43 6f 6c 6c 3b 0a 20 20 69 66 28 20 21  = pColl;.  if( !
2071d 70 20 29 7b 0a 20 20 20 20 70 20 3d 20 73 71 6c  p ){.    p = sql
2071e 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28  ite3FindCollSeq(
2071f 64 62 2c 20 45 4e 43 28 64 62 29 2c 20 7a 4e 61  db, ENC(db), zNa
20720 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20  me, nName, 0);. 
20721 20 7d 0a 20 20 69 66 28 20 21 70 20 7c 7c 20 21   }.  if( !p || !
20722 70 2d 3e 78 43 6d 70 20 29 7b 0a 20 20 20 20 2f  p->xCmp ){.    /
20723 2a 20 4e 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  * No collation s
20724 65 71 75 65 6e 63 65 20 6f 66 20 74 68 69 73 20  equence of this 
20725 74 79 70 65 20 66 6f 72 20 74 68 69 73 20 65 6e  type for this en
20726 63 6f 64 69 6e 67 20 69 73 20 72 65 67 69 73 74  coding is regist
20727 65 72 65 64 2e 0a 20 20 20 20 2a 2a 20 43 61 6c  ered..    ** Cal
20728 6c 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  l the collation 
20729 66 61 63 74 6f 72 79 20 74 6f 20 73 65 65 20 69  factory to see i
2072a 66 20 69 74 20 63 61 6e 20 73 75 70 70 6c 79 20  f it can supply 
2072b 75 73 20 77 69 74 68 20 6f 6e 65 2e 0a 20 20 20  us with one..   
2072c 20 2a 2f 0a 20 20 20 20 63 61 6c 6c 43 6f 6c 6c   */.    callColl
2072d 4e 65 65 64 65 64 28 64 62 2c 20 7a 4e 61 6d 65  Needed(db, zName
2072e 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 20  , nName);.    p 
2072f 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c  = sqlite3FindCol
20730 6c 53 65 71 28 64 62 2c 20 45 4e 43 28 64 62 29  lSeq(db, ENC(db)
20731 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
20732 30 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 20  0);.  }.  if( p 
20733 26 26 20 21 70 2d 3e 78 43 6d 70 20 26 26 20 73  && !p->xCmp && s
20734 79 6e 74 68 43 6f 6c 6c 53 65 71 28 64 62 2c 20  ynthCollSeq(db, 
20735 70 29 20 29 7b 0a 20 20 20 20 70 20 3d 20 30 3b  p) ){.    p = 0;
20736 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 21  .  }.  assert( !
20737 70 20 7c 7c 20 70 2d 3e 78 43 6d 70 20 29 3b 0a  p || p->xCmp );.
20738 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 2f    return p;.}../
20739 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2073a 65 20 69 73 20 63 61 6c 6c 65 64 20 6f 6e 20 61  e is called on a
2073b 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65   collation seque
2073c 6e 63 65 20 62 65 66 6f 72 65 20 69 74 20 69 73  nce before it is
2073d 20 75 73 65 64 20 74 6f 0a 2a 2a 20 63 68 65 63   used to.** chec
2073e 6b 20 74 68 61 74 20 69 74 20 69 73 20 64 65 66  k that it is def
2073f 69 6e 65 64 2e 20 41 6e 20 75 6e 64 65 66 69 6e  ined. An undefin
20740 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ed collation seq
20741 75 65 6e 63 65 20 65 78 69 73 74 73 20 77 68 65  uence exists whe
20742 6e 0a 2a 2a 20 61 20 64 61 74 61 62 61 73 65 20  n.** a database 
20743 69 73 20 6c 6f 61 64 65 64 20 74 68 61 74 20 63  is loaded that c
20744 6f 6e 74 61 69 6e 73 20 72 65 66 65 72 65 6e 63  ontains referenc
20745 65 73 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20  es to collation 
20746 73 65 71 75 65 6e 63 65 73 0a 2a 2a 20 74 68 61  sequences.** tha
20747 74 20 68 61 76 65 20 6e 6f 74 20 62 65 65 6e 20  t have not been 
20748 64 65 66 69 6e 65 64 20 62 79 20 73 71 6c 69 74  defined by sqlit
20749 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
2074a 69 6f 6e 28 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a  ion() etc..**.**
2074b 20 49 66 20 72 65 71 75 69 72 65 64 2c 20 74 68   If required, th
2074c 69 73 20 72 6f 75 74 69 6e 65 20 63 61 6c 6c 73  is routine calls
2074d 20 74 68 65 20 27 63 6f 6c 6c 61 74 69 6f 6e 20   the 'collation 
2074e 6e 65 65 64 65 64 27 20 63 61 6c 6c 62 61 63 6b  needed' callback
2074f 20 74 6f 0a 2a 2a 20 72 65 71 75 65 73 74 20 61   to.** request a
20750 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74   definition of t
20751 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  he collating seq
20752 75 65 6e 63 65 2e 20 49 66 20 74 68 69 73 20 64  uence. If this d
20753 6f 65 73 6e 27 74 20 77 6f 72 6b 2c 20 0a 2a 2a  oesn't work, .**
20754 20 61 6e 20 65 71 75 69 76 61 6c 65 6e 74 20 63   an equivalent c
20755 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
20756 65 20 74 68 61 74 20 75 73 65 73 20 61 20 74 65  e that uses a te
20757 78 74 20 65 6e 63 6f 64 69 6e 67 20 64 69 66 66  xt encoding diff
20758 65 72 65 6e 74 0a 2a 2a 20 66 72 6f 6d 20 74 68  erent.** from th
20759 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
2075a 69 73 20 73 75 62 73 74 69 74 75 74 65 64 2c 20  is substituted, 
2075b 69 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61  if one is availa
2075c 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
2075d 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2075e 65 33 43 68 65 63 6b 43 6f 6c 6c 53 65 71 28 50  e3CheckCollSeq(P
2075f 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 43 6f  arse *pParse, Co
20760 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 29 7b 0a 20  llSeq *pColl){. 
20761 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20   if( pColl ){.  
20762 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
20763 61 6d 65 20 3d 20 70 43 6f 6c 6c 2d 3e 7a 4e 61  ame = pColl->zNa
20764 6d 65 3b 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20  me;.    CollSeq 
20765 2a 70 20 3d 20 73 71 6c 69 74 65 33 47 65 74 43  *p = sqlite3GetC
20766 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2d 3e 64  ollSeq(pParse->d
20767 62 2c 20 70 43 6f 6c 6c 2c 20 7a 4e 61 6d 65 2c  b, pColl, zName,
20768 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 21 70   -1);.    if( !p
20769 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 50   ){.      if( pP
2076a 61 72 73 65 2d 3e 6e 45 72 72 3d 3d 30 20 29 7b  arse->nErr==0 ){
2076b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
2076c 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
2076d 20 22 6e 6f 20 73 75 63 68 20 63 6f 6c 6c 61 74   "no such collat
2076e 69 6f 6e 20 73 65 71 75 65 6e 63 65 3a 20 25 73  ion sequence: %s
2076f 22 2c 20 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  ", zName);.     
20770 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d   }.      pParse-
20771 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 72  >nErr++;.      r
20772 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
20773 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 73  OR;.    }.    as
20774 73 65 72 74 28 20 70 3d 3d 70 43 6f 6c 6c 20 29  sert( p==pColl )
20775 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53  ;.  }.  return S
20776 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 0a 2f  QLITE_OK;.}..../
20777 2a 0a 2a 2a 20 4c 6f 63 61 74 65 20 61 6e 64 20  *.** Locate and 
20778 72 65 74 75 72 6e 20 61 6e 20 65 6e 74 72 79 20  return an entry 
20779 66 72 6f 6d 20 74 68 65 20 64 62 2e 61 43 6f 6c  from the db.aCol
2077a 6c 53 65 71 20 68 61 73 68 20 74 61 62 6c 65 2e  lSeq hash table.
2077b 20 49 66 20 74 68 65 20 65 6e 74 72 79 0a 2a 2a   If the entry.**
2077c 20 73 70 65 63 69 66 69 65 64 20 62 79 20 7a 4e   specified by zN
2077d 61 6d 65 20 61 6e 64 20 6e 4e 61 6d 65 20 69 73  ame and nName is
2077e 20 6e 6f 74 20 66 6f 75 6e 64 20 61 6e 64 20 70   not found and p
2077f 61 72 61 6d 65 74 65 72 20 27 63 72 65 61 74 65  arameter 'create
20780 27 20 69 73 0a 2a 2a 20 74 72 75 65 2c 20 74 68  ' is.** true, th
20781 65 6e 20 63 72 65 61 74 65 20 61 20 6e 65 77 20  en create a new 
20782 65 6e 74 72 79 2e 20 4f 74 68 65 72 77 69 73 65  entry. Otherwise
20783 20 72 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a   return NULL..**
20784 0a 2a 2a 20 45 61 63 68 20 70 6f 69 6e 74 65 72  .** Each pointer
20785 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 73   stored in the s
20786 71 6c 69 74 65 33 2e 61 43 6f 6c 6c 53 65 71 20  qlite3.aCollSeq 
20787 68 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61  hash table conta
20788 69 6e 73 20 61 6e 0a 2a 2a 20 61 72 72 61 79 20  ins an.** array 
20789 6f 66 20 74 68 72 65 65 20 43 6f 6c 6c 53 65 71  of three CollSeq
2078a 20 73 74 72 75 63 74 75 72 65 73 2e 20 54 68 65   structures. The
2078b 20 66 69 72 73 74 20 69 73 20 74 68 65 20 63 6f   first is the co
2078c 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65  llation sequence
2078d 0a 2a 2a 20 70 72 65 66 66 65 72 72 65 64 20 66  .** prefferred f
2078e 6f 72 20 55 54 46 2d 38 2c 20 74 68 65 20 73 65  or UTF-8, the se
2078f 63 6f 6e 64 20 55 54 46 2d 31 36 6c 65 2c 20 61  cond UTF-16le, a
20790 6e 64 20 74 68 65 20 74 68 69 72 64 20 55 54 46  nd the third UTF
20791 2d 31 36 62 65 2e 0a 2a 2a 0a 2a 2a 20 53 74 6f  -16be..**.** Sto
20792 72 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  red immediately 
20793 61 66 74 65 72 20 74 68 65 20 74 68 72 65 65 20  after the three 
20794 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
20795 63 65 73 20 69 73 20 61 20 63 6f 70 79 20 6f 66  ces is a copy of
20796 0a 2a 2a 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f  .** the collatio
20797 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 2e  n sequence name.
20798 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
20799 69 73 20 73 74 72 69 6e 67 20 69 73 20 73 74 6f  is string is sto
2079a 72 65 64 20 69 6e 0a 2a 2a 20 65 61 63 68 20 63  red in.** each c
2079b 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2079c 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
2079d 73 74 61 74 69 63 20 43 6f 6c 6c 53 65 71 20 2a  static CollSeq *
2079e 66 69 6e 64 43 6f 6c 6c 53 65 71 45 6e 74 72 79  findCollSeqEntry
2079f 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
207a0 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
207a1 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d  Name,.  int nNam
207a2 65 2c 0a 20 20 69 6e 74 20 63 72 65 61 74 65 0a  e,.  int create.
207a3 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  ){.  CollSeq *pC
207a4 6f 6c 6c 3b 0a 20 20 69 66 28 20 6e 4e 61 6d 65  oll;.  if( nName
207a5 3c 30 20 29 20 6e 4e 61 6d 65 20 3d 20 73 71 6c  <0 ) nName = sql
207a6 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20 7a  ite3Strlen(db, z
207a7 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c 20 3d  Name);.  pColl =
207a8 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64   sqlite3HashFind
207a9 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20  (&db->aCollSeq, 
207aa 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 0a  zName, nName);..
207ab 20 20 69 66 28 20 30 3d 3d 70 43 6f 6c 6c 20 26    if( 0==pColl &
207ac 26 20 63 72 65 61 74 65 20 29 7b 0a 20 20 20 20  & create ){.    
207ad 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 44  pColl = sqlite3D
207ae 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20  bMallocZero(db, 
207af 33 2a 73 69 7a 65 6f 66 28 2a 70 43 6f 6c 6c 29  3*sizeof(*pColl)
207b0 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 20 29 3b 0a   + nName + 1 );.
207b1 20 20 20 20 69 66 28 20 70 43 6f 6c 6c 20 29 7b      if( pColl ){
207b2 0a 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a  .      CollSeq *
207b3 70 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20  pDel = 0;.      
207b4 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 20 3d  pColl[0].zName =
207b5 20 28 63 68 61 72 2a 29 26 70 43 6f 6c 6c 5b 33   (char*)&pColl[3
207b6 5d 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 30  ];.      pColl[0
207b7 5d 2e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55  ].enc = SQLITE_U
207b8 54 46 38 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c  TF8;.      pColl
207b9 5b 31 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  [1].zName = (cha
207ba 72 2a 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20  r*)&pColl[3];.  
207bb 20 20 20 20 70 43 6f 6c 6c 5b 31 5d 2e 65 6e 63      pColl[1].enc
207bc 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c   = SQLITE_UTF16L
207bd 45 3b 0a 20 20 20 20 20 20 70 43 6f 6c 6c 5b 32  E;.      pColl[2
207be 5d 2e 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 2a  ].zName = (char*
207bf 29 26 70 43 6f 6c 6c 5b 33 5d 3b 0a 20 20 20 20  )&pColl[3];.    
207c0 20 20 70 43 6f 6c 6c 5b 32 5d 2e 65 6e 63 20 3d    pColl[2].enc =
207c1 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3b   SQLITE_UTF16BE;
207c2 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 70 43  .      memcpy(pC
207c3 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 7a 4e  oll[0].zName, zN
207c4 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
207c5 20 20 20 70 43 6f 6c 6c 5b 30 5d 2e 7a 4e 61 6d     pColl[0].zNam
207c6 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b 0a 20 20  e[nName] = 0;.  
207c7 20 20 20 20 70 44 65 6c 20 3d 20 73 71 6c 69 74      pDel = sqlit
207c8 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64 62  e3HashInsert(&db
207c9 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 70 43 6f 6c  ->aCollSeq, pCol
207ca 6c 5b 30 5d 2e 7a 4e 61 6d 65 2c 20 6e 4e 61 6d  l[0].zName, nNam
207cb 65 2c 20 70 43 6f 6c 6c 29 3b 0a 0a 20 20 20 20  e, pColl);..    
207cc 20 20 2f 2a 20 49 66 20 61 20 6d 61 6c 6c 6f 63    /* If a malloc
207cd 28 29 20 66 61 69 6c 75 72 65 20 6f 63 63 75 72  () failure occur
207ce 65 64 20 69 6e 20 73 71 6c 69 74 65 33 48 61 73  ed in sqlite3Has
207cf 68 49 6e 73 65 72 74 28 29 2c 20 69 74 20 77 69  hInsert(), it wi
207d0 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20 72 65 74  ll .      ** ret
207d1 75 72 6e 20 74 68 65 20 70 43 6f 6c 6c 20 70 6f  urn the pColl po
207d2 69 6e 74 65 72 20 74 6f 20 62 65 20 64 65 6c 65  inter to be dele
207d3 74 65 64 20 28 62 65 63 61 75 73 65 20 69 74 20  ted (because it 
207d4 77 61 73 6e 27 74 20 61 64 64 65 64 0a 20 20 20  wasn't added.   
207d5 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 68 61 73     ** to the has
207d6 68 20 74 61 62 6c 65 29 2e 0a 20 20 20 20 20 20  h table)..      
207d7 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  */.      assert(
207d8 20 70 44 65 6c 3d 3d 30 20 7c 7c 20 70 44 65 6c   pDel==0 || pDel
207d9 3d 3d 70 43 6f 6c 6c 20 29 3b 0a 20 20 20 20 20  ==pColl );.     
207da 20 69 66 28 20 70 44 65 6c 21 3d 30 20 29 7b 0a   if( pDel!=0 ){.
207db 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c          db->mall
207dc 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
207dd 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
207de 72 65 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20  ree(db, pDel);. 
207df 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 30         pColl = 0
207e0 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
207e1 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70 43 6f    }.  return pCo
207e2 6c 6c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 61 72  ll;.}../*.** Par
207e3 61 6d 65 74 65 72 20 7a 4e 61 6d 65 20 70 6f 69  ameter zName poi
207e4 6e 74 73 20 74 6f 20 61 20 55 54 46 2d 38 20 65  nts to a UTF-8 e
207e5 6e 63 6f 64 65 64 20 73 74 72 69 6e 67 20 6e 4e  ncoded string nN
207e6 61 6d 65 20 62 79 74 65 73 20 6c 6f 6e 67 2e 0a  ame bytes long..
207e7 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 43 6f  ** Return the Co
207e8 6c 6c 53 65 71 2a 20 70 6f 69 6e 74 65 72 20 66  llSeq* pointer f
207e9 6f 72 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e  or the collation
207ea 20 73 65 71 75 65 6e 63 65 20 6e 61 6d 65 64 20   sequence named 
207eb 7a 4e 61 6d 65 0a 2a 2a 20 66 6f 72 20 74 68 65  zName.** for the
207ec 20 65 6e 63 6f 64 69 6e 67 20 27 65 6e 63 27 20   encoding 'enc' 
207ed 66 72 6f 6d 20 74 68 65 20 64 61 74 61 62 61 73  from the databas
207ee 65 20 27 64 62 27 2e 0a 2a 2a 0a 2a 2a 20 49 66  e 'db'..**.** If
207ef 20 74 68 65 20 65 6e 74 72 79 20 73 70 65 63 69   the entry speci
207f0 66 69 65 64 20 69 73 20 6e 6f 74 20 66 6f 75 6e  fied is not foun
207f1 64 20 61 6e 64 20 27 63 72 65 61 74 65 27 20 69  d and 'create' i
207f2 73 20 74 72 75 65 2c 20 74 68 65 6e 20 63 72 65  s true, then cre
207f3 61 74 65 20 61 0a 2a 2a 20 6e 65 77 20 65 6e 74  ate a.** new ent
207f4 72 79 2e 20 20 4f 74 68 65 72 77 69 73 65 20 72  ry.  Otherwise r
207f5 65 74 75 72 6e 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  eturn NULL..**.*
207f6 2a 20 41 20 73 65 70 61 72 61 74 65 20 66 75 6e  * A separate fun
207f7 63 74 69 6f 6e 20 73 71 6c 69 74 65 33 4c 6f 63  ction sqlite3Loc
207f8 61 74 65 43 6f 6c 6c 53 65 71 28 29 20 69 73 20  ateCollSeq() is 
207f9 61 20 77 72 61 70 70 65 72 20 61 72 6f 75 6e 64  a wrapper around
207fa 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65  .** this routine
207fb 2e 20 20 73 71 6c 69 74 65 33 4c 6f 63 61 74 65  .  sqlite3Locate
207fc 43 6f 6c 6c 53 65 71 28 29 20 69 6e 76 6f 6b 65  CollSeq() invoke
207fd 73 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  s the collation 
207fe 66 61 63 74 6f 72 79 0a 2a 2a 20 69 66 20 6e 65  factory.** if ne
207ff 63 65 73 73 61 72 79 20 61 6e 64 20 67 65 6e 65  cessary and gene
20800 72 61 74 65 73 20 61 6e 20 65 72 72 6f 72 20 6d  rates an error m
20801 65 73 73 61 67 65 20 69 66 20 74 68 65 20 63 6f  essage if the co
20802 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
20803 0a 2a 2a 20 63 61 6e 6e 6f 74 20 62 65 20 66 6f  .** cannot be fo
20804 75 6e 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  und..*/.SQLITE_P
20805 52 49 56 41 54 45 20 43 6f 6c 6c 53 65 71 20 2a  RIVATE CollSeq *
20806 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c 53  sqlite3FindCollS
20807 65 71 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  eq(.  sqlite3 *d
20808 62 2c 0a 20 20 75 38 20 65 6e 63 2c 0a 20 20 63  b,.  u8 enc,.  c
20809 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2080a 2c 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 2c 0a 20  ,.  int nName,. 
2080b 20 69 6e 74 20 63 72 65 61 74 65 0a 29 7b 0a 20   int create.){. 
2080c 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b   CollSeq *pColl;
2080d 0a 20 20 69 66 28 20 7a 4e 61 6d 65 20 29 7b 0a  .  if( zName ){.
2080e 20 20 20 20 70 43 6f 6c 6c 20 3d 20 66 69 6e 64      pColl = find
2080f 43 6f 6c 6c 53 65 71 45 6e 74 72 79 28 64 62 2c  CollSeqEntry(db,
20810 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 63   zName, nName, c
20811 72 65 61 74 65 29 3b 0a 20 20 7d 65 6c 73 65 7b  reate);.  }else{
20812 0a 20 20 20 20 70 43 6f 6c 6c 20 3d 20 64 62 2d  .    pColl = db-
20813 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 7d 0a  >pDfltColl;.  }.
20814 20 20 61 73 73 65 72 74 28 20 53 51 4c 49 54 45    assert( SQLITE
20815 5f 55 54 46 38 3d 3d 31 20 26 26 20 53 51 4c 49  _UTF8==1 && SQLI
20816 54 45 5f 55 54 46 31 36 4c 45 3d 3d 32 20 26 26  TE_UTF16LE==2 &&
20817 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 3d   SQLITE_UTF16BE=
20818 3d 33 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =3 );.  assert( 
20819 65 6e 63 3e 3d 53 51 4c 49 54 45 5f 55 54 46 38  enc>=SQLITE_UTF8
2081a 20 26 26 20 65 6e 63 3c 3d 53 51 4c 49 54 45 5f   && enc<=SQLITE_
2081b 55 54 46 31 36 42 45 20 29 3b 0a 20 20 69 66 28  UTF16BE );.  if(
2081c 20 70 43 6f 6c 6c 20 29 20 70 43 6f 6c 6c 20 2b   pColl ) pColl +
2081d 3d 20 65 6e 63 2d 31 3b 0a 20 20 72 65 74 75 72  = enc-1;.  retur
2081e 6e 20 70 43 6f 6c 6c 3b 0a 7d 0a 0a 2f 2a 20 44  n pColl;.}../* D
2081f 75 72 69 6e 67 20 74 68 65 20 73 65 61 72 63 68  uring the search
20820 20 66 6f 72 20 74 68 65 20 62 65 73 74 20 66 75   for the best fu
20821 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
20822 6e 2c 20 74 68 69 73 20 70 72 6f 63 65 64 75 72  n, this procedur
20823 65 0a 2a 2a 20 69 73 20 63 61 6c 6c 65 64 20 74  e.** is called t
20824 6f 20 74 65 73 74 20 68 6f 77 20 77 65 6c 6c 20  o test how well 
20825 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 70 61 73  the function pas
20826 73 65 64 20 61 73 20 74 68 65 20 66 69 72 73 74  sed as the first
20827 20 61 72 67 75 6d 65 6e 74 0a 2a 2a 20 6d 61 74   argument.** mat
20828 63 68 65 73 20 74 68 65 20 72 65 71 75 65 73 74  ches the request
20829 20 66 6f 72 20 61 20 66 75 6e 63 74 69 6f 6e 20   for a function 
2082a 77 69 74 68 20 6e 41 72 67 20 61 72 67 75 6d 65  with nArg argume
2082b 6e 74 73 20 69 6e 20 61 20 73 79 73 74 65 6d 0a  nts in a system.
2082c 2a 2a 20 74 68 61 74 20 75 73 65 73 20 65 6e 63  ** that uses enc
2082d 6f 64 69 6e 67 20 65 6e 63 2e 20 54 68 65 20 76  oding enc. The v
2082e 61 6c 75 65 20 72 65 74 75 72 6e 65 64 20 69 6e  alue returned in
2082f 64 69 63 61 74 65 73 20 68 6f 77 20 77 65 6c 6c  dicates how well
20830 20 74 68 65 0a 2a 2a 20 72 65 71 75 65 73 74 20   the.** request 
20831 69 73 20 6d 61 74 63 68 65 64 2e 20 41 20 68 69  is matched. A hi
20832 67 68 65 72 20 76 61 6c 75 65 20 69 6e 64 69 63  gher value indic
20833 61 74 65 73 20 61 20 62 65 74 74 65 72 20 6d 61  ates a better ma
20834 74 63 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  tch..**.** The r
20835 65 74 75 72 6e 65 64 20 76 61 6c 75 65 20 69 73  eturned value is
20836 20 61 6c 77 61 79 73 20 62 65 74 77 65 65 6e 20   always between 
20837 31 20 61 6e 64 20 36 2c 20 61 73 20 66 6f 6c 6c  1 and 6, as foll
20838 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 31 3a 20 41 20  ows:.**.** 1: A 
20839 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65 6e  variable argumen
2083a 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ts function that
2083b 20 70 72 65 66 65 72 73 20 55 54 46 2d 38 20 77   prefers UTF-8 w
2083c 68 65 6e 20 61 20 55 54 46 2d 31 36 0a 2a 2a 20  hen a UTF-16.** 
2083d 20 20 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 72     encoding is r
2083e 65 71 75 65 73 74 65 64 2c 20 6f 72 20 76 69 63  equested, or vic
2083f 65 20 76 65 72 73 61 2e 0a 2a 2a 20 32 3a 20 41  e versa..** 2: A
20840 20 76 61 72 69 61 62 6c 65 20 61 72 67 75 6d 65   variable argume
20841 6e 74 73 20 66 75 6e 63 74 69 6f 6e 20 74 68 61  nts function tha
20842 74 20 75 73 65 73 20 55 54 46 2d 31 36 42 45 20  t uses UTF-16BE 
20843 77 68 65 6e 20 55 54 46 2d 31 36 4c 45 20 69 73  when UTF-16LE is
20844 0a 2a 2a 20 20 20 20 72 65 71 75 65 73 74 65 64  .**    requested
20845 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73 61 2e  , or vice versa.
20846 0a 2a 2a 20 33 3a 20 41 20 76 61 72 69 61 62 6c  .** 3: A variabl
20847 65 20 61 72 67 75 6d 65 6e 74 73 20 66 75 6e 63  e arguments func
20848 74 69 6f 6e 20 75 73 69 6e 67 20 74 68 65 20 73  tion using the s
20849 61 6d 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  ame text encodin
2084a 67 2e 0a 2a 2a 20 34 3a 20 41 20 66 75 6e 63 74  g..** 4: A funct
2084b 69 6f 6e 20 77 69 74 68 20 74 68 65 20 65 78 61  ion with the exa
2084c 63 74 20 6e 75 6d 62 65 72 20 6f 66 20 61 72 67  ct number of arg
2084d 75 6d 65 6e 74 73 20 72 65 71 75 65 73 74 65 64  uments requested
2084e 20 74 68 61 74 0a 2a 2a 20 20 20 20 70 72 65 66   that.**    pref
2084f 65 72 73 20 55 54 46 2d 38 20 77 68 65 6e 20 61  ers UTF-8 when a
20850 20 55 54 46 2d 31 36 20 65 6e 63 6f 64 69 6e 67   UTF-16 encoding
20851 20 69 73 20 72 65 71 75 65 73 74 65 64 2c 20 6f   is requested, o
20852 72 20 76 69 63 65 20 76 65 72 73 61 2e 0a 2a 2a  r vice versa..**
20853 20 35 3a 20 41 20 66 75 6e 63 74 69 6f 6e 20 77   5: A function w
20854 69 74 68 20 74 68 65 20 65 78 61 63 74 20 6e 75  ith the exact nu
20855 6d 62 65 72 20 6f 66 20 61 72 67 75 6d 65 6e 74  mber of argument
20856 73 20 72 65 71 75 65 73 74 65 64 20 74 68 61 74  s requested that
20857 0a 2a 2a 20 20 20 20 70 72 65 66 65 72 73 20 55  .**    prefers U
20858 54 46 2d 31 36 4c 45 20 77 68 65 6e 20 55 54 46  TF-16LE when UTF
20859 2d 31 36 42 45 20 69 73 20 72 65 71 75 65 73 74  -16BE is request
2085a 65 64 2c 20 6f 72 20 76 69 63 65 20 76 65 72 73  ed, or vice vers
2085b 61 2e 0a 2a 2a 20 36 3a 20 41 6e 20 65 78 61 63  a..** 6: An exac
2085c 74 20 6d 61 74 63 68 2e 0a 2a 2a 0a 2a 2f 0a 73  t match..**.*/.s
2085d 74 61 74 69 63 20 69 6e 74 20 6d 61 74 63 68 51  tatic int matchQ
2085e 75 61 6c 69 74 79 28 46 75 6e 63 44 65 66 20 2a  uality(FuncDef *
2085f 70 2c 20 69 6e 74 20 6e 41 72 67 2c 20 75 38 20  p, int nArg, u8 
20860 65 6e 63 29 7b 0a 20 20 69 6e 74 20 6d 61 74 63  enc){.  int matc
20861 68 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 2d 3e  h = 0;.  if( p->
20862 6e 41 72 67 3d 3d 2d 31 20 7c 7c 20 70 2d 3e 6e  nArg==-1 || p->n
20863 41 72 67 3d 3d 6e 41 72 67 20 7c 7c 20 6e 41 72  Arg==nArg || nAr
20864 67 3d 3d 2d 31 20 29 7b 0a 20 20 20 20 6d 61 74  g==-1 ){.    mat
20865 63 68 20 3d 20 31 3b 0a 20 20 20 20 69 66 28 20  ch = 1;.    if( 
20866 70 2d 3e 6e 41 72 67 3d 3d 6e 41 72 67 20 7c 7c  p->nArg==nArg ||
20867 20 6e 41 72 67 3d 3d 2d 31 20 29 7b 0a 20 20 20   nArg==-1 ){.   
20868 20 20 20 6d 61 74 63 68 20 3d 20 34 3b 0a 20 20     match = 4;.  
20869 20 20 7d 0a 20 20 20 20 69 66 28 20 65 6e 63 3d    }.    if( enc=
2086a 3d 70 2d 3e 69 50 72 65 66 45 6e 63 20 29 7b 0a  =p->iPrefEnc ){.
2086b 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d 20 32        match += 2
2086c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65  ;.    }.    else
2086d 20 69 66 28 20 28 65 6e 63 3d 3d 53 51 4c 49 54   if( (enc==SQLIT
2086e 45 5f 55 54 46 31 36 4c 45 20 26 26 20 70 2d 3e  E_UTF16LE && p->
2086f 69 50 72 65 66 45 6e 63 3d 3d 53 51 4c 49 54 45  iPrefEnc==SQLITE
20870 5f 55 54 46 31 36 42 45 29 20 7c 7c 0a 20 20 20  _UTF16BE) ||.   
20871 20 20 20 20 20 20 20 20 20 20 28 65 6e 63 3d 3d            (enc==
20872 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 26  SQLITE_UTF16BE &
20873 26 20 70 2d 3e 69 50 72 65 66 45 6e 63 3d 3d 53  & p->iPrefEnc==S
20874 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 29 20 29  QLITE_UTF16LE) )
20875 7b 0a 20 20 20 20 20 20 6d 61 74 63 68 20 2b 3d  {.      match +=
20876 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   1;.    }.  }.  
20877 72 65 74 75 72 6e 20 6d 61 74 63 68 3b 0a 7d 0a  return match;.}.
20878 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63 68 20 61 20  ./*.** Search a 
20879 46 75 6e 63 44 65 66 48 61 73 68 20 66 6f 72 20  FuncDefHash for 
2087a 61 20 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20  a function with 
2087b 74 68 65 20 67 69 76 65 6e 20 6e 61 6d 65 2e 20  the given name. 
2087c 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 70 6f 69   Return.** a poi
2087d 6e 74 65 72 20 74 6f 20 74 68 65 20 6d 61 74 63  nter to the matc
2087e 68 69 6e 67 20 46 75 6e 63 44 65 66 20 69 66 20  hing FuncDef if 
2087f 66 6f 75 6e 64 2c 20 6f 72 20 30 20 69 66 20 74  found, or 0 if t
20880 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
20881 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 46 75 6e 63  ..*/.static Func
20882 44 65 66 20 2a 66 75 6e 63 74 69 6f 6e 53 65 61  Def *functionSea
20883 72 63 68 28 0a 20 20 46 75 6e 63 44 65 66 48 61  rch(.  FuncDefHa
20884 73 68 20 2a 70 48 61 73 68 2c 20 20 2f 2a 20 48  sh *pHash,  /* H
20885 61 73 68 20 74 61 62 6c 65 20 74 6f 20 73 65 61  ash table to sea
20886 72 63 68 20 2a 2f 0a 20 20 69 6e 74 20 68 2c 20  rch */.  int h, 
20887 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20888 20 48 61 73 68 20 6f 66 20 74 68 65 20 6e 61 6d   Hash of the nam
20889 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  e */.  const cha
2088a 72 20 2a 7a 46 75 6e 63 2c 20 20 20 2f 2a 20 4e  r *zFunc,   /* N
2088b 61 6d 65 20 6f 66 20 66 75 6e 63 74 69 6f 6e 20  ame of function 
2088c 2a 2f 0a 20 20 69 6e 74 20 6e 46 75 6e 63 20 20  */.  int nFunc  
2088d 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2088e 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
2088f 7a 46 75 6e 63 20 2a 2f 0a 29 7b 0a 20 20 46 75  zFunc */.){.  Fu
20890 6e 63 44 65 66 20 2a 70 3b 0a 20 20 66 6f 72 28  ncDef *p;.  for(
20891 70 3d 70 48 61 73 68 2d 3e 61 5b 68 5d 3b 20 70  p=pHash->a[h]; p
20892 3b 20 70 3d 70 2d 3e 70 48 61 73 68 29 7b 0a 20  ; p=p->pHash){. 
20893 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74     if( sqlite3St
20894 72 4e 49 43 6d 70 28 70 2d 3e 7a 4e 61 6d 65 2c  rNICmp(p->zName,
20895 20 7a 46 75 6e 63 2c 20 6e 46 75 6e 63 29 3d 3d   zFunc, nFunc)==
20896 30 20 26 26 20 70 2d 3e 7a 4e 61 6d 65 5b 6e 46  0 && p->zName[nF
20897 75 6e 63 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20  unc]==0 ){.     
20898 20 72 65 74 75 72 6e 20 70 3b 0a 20 20 20 20 7d   return p;.    }
20899 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b  .  }.  return 0;
2089a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74  .}../*.** Insert
2089b 20 61 20 6e 65 77 20 46 75 6e 63 44 65 66 20 69   a new FuncDef i
2089c 6e 74 6f 20 61 20 46 75 6e 63 44 65 66 48 61 73  nto a FuncDefHas
2089d 68 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a 2f  h hash table..*/
2089e 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2089f 76 6f 69 64 20 73 71 6c 69 74 65 33 46 75 6e 63  void sqlite3Func
208a0 44 65 66 49 6e 73 65 72 74 28 0a 20 20 46 75 6e  DefInsert(.  Fun
208a1 63 44 65 66 48 61 73 68 20 2a 70 48 61 73 68 2c  cDefHash *pHash,
208a2 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20 74 61    /* The hash ta
208a3 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 74  ble into which t
208a4 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 46 75  o insert */.  Fu
208a5 6e 63 44 65 66 20 2a 70 44 65 66 20 20 20 20 20  ncDef *pDef     
208a6 20 20 20 2f 2a 20 54 68 65 20 66 75 6e 63 74 69     /* The functi
208a7 6f 6e 20 64 65 66 69 6e 69 74 69 6f 6e 20 74 6f  on definition to
208a8 20 69 6e 73 65 72 74 20 2a 2f 0a 29 7b 0a 20 20   insert */.){.  
208a9 46 75 6e 63 44 65 66 20 2a 70 4f 74 68 65 72 3b  FuncDef *pOther;
208aa 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73  .  int nName = s
208ab 74 72 6c 65 6e 28 70 44 65 66 2d 3e 7a 4e 61 6d  trlen(pDef->zNam
208ac 65 29 3b 0a 20 20 75 38 20 63 31 20 3d 20 28 75  e);.  u8 c1 = (u
208ad 38 29 70 44 65 66 2d 3e 7a 4e 61 6d 65 5b 30 5d  8)pDef->zName[0]
208ae 3b 0a 20 20 69 6e 74 20 68 20 3d 20 28 73 71 6c  ;.  int h = (sql
208af 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72  ite3UpperToLower
208b0 5b 63 31 5d 20 2b 20 6e 4e 61 6d 65 29 20 25 20  [c1] + nName) % 
208b1 41 72 72 61 79 53 69 7a 65 28 70 48 61 73 68 2d  ArraySize(pHash-
208b2 3e 61 29 3b 0a 20 20 70 4f 74 68 65 72 20 3d 20  >a);.  pOther = 
208b3 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68 28 70  functionSearch(p
208b4 48 61 73 68 2c 20 68 2c 20 70 44 65 66 2d 3e 7a  Hash, h, pDef->z
208b5 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
208b6 69 66 28 20 70 4f 74 68 65 72 20 29 7b 0a 20 20  if( pOther ){.  
208b7 20 20 70 44 65 66 2d 3e 70 4e 65 78 74 20 3d 20    pDef->pNext = 
208b8 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74 3b 0a 20  pOther->pNext;. 
208b9 20 20 20 70 4f 74 68 65 72 2d 3e 70 4e 65 78 74     pOther->pNext
208ba 20 3d 20 70 44 65 66 3b 0a 20 20 7d 65 6c 73 65   = pDef;.  }else
208bb 7b 0a 20 20 20 20 70 44 65 66 2d 3e 70 4e 65 78  {.    pDef->pNex
208bc 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 65 66 2d  t = 0;.    pDef-
208bd 3e 70 48 61 73 68 20 3d 20 70 48 61 73 68 2d 3e  >pHash = pHash->
208be 61 5b 68 5d 3b 0a 20 20 20 20 70 48 61 73 68 2d  a[h];.    pHash-
208bf 3e 61 5b 68 5d 20 3d 20 70 44 65 66 3b 0a 20 20  >a[h] = pDef;.  
208c0 7d 0a 7d 0a 20 20 0a 20 20 0a 0a 2f 2a 0a 2a 2a  }.}.  .  ../*.**
208c1 20 4c 6f 63 61 74 65 20 61 20 75 73 65 72 20 66   Locate a user f
208c2 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 61 20  unction given a 
208c3 6e 61 6d 65 2c 20 61 20 6e 75 6d 62 65 72 20 6f  name, a number o
208c4 66 20 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20  f arguments and 
208c5 61 20 66 6c 61 67 0a 2a 2a 20 69 6e 64 69 63 61  a flag.** indica
208c6 74 69 6e 67 20 77 68 65 74 68 65 72 20 74 68 65  ting whether the
208c7 20 66 75 6e 63 74 69 6f 6e 20 70 72 65 66 65 72   function prefer
208c8 73 20 55 54 46 2d 31 36 20 6f 76 65 72 20 55 54  s UTF-16 over UT
208c9 46 2d 38 2e 20 20 52 65 74 75 72 6e 20 61 0a 2a  F-8.  Return a.*
208ca 2a 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  * pointer to the
208cb 20 46 75 6e 63 44 65 66 20 73 74 72 75 63 74 75   FuncDef structu
208cc 72 65 20 74 68 61 74 20 64 65 66 69 6e 65 73 20  re that defines 
208cd 74 68 61 74 20 66 75 6e 63 74 69 6f 6e 2c 20 6f  that function, o
208ce 72 20 72 65 74 75 72 6e 0a 2a 2a 20 4e 55 4c 4c  r return.** NULL
208cf 20 69 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   if the function
208d0 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2e   does not exist.
208d1 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 63 72  .**.** If the cr
208d2 65 61 74 65 46 6c 61 67 20 61 72 67 75 6d 65 6e  eateFlag argumen
208d3 74 20 69 73 20 74 72 75 65 2c 20 74 68 65 6e 20  t is true, then 
208d4 61 20 6e 65 77 20 28 62 6c 61 6e 6b 29 20 46 75  a new (blank) Fu
208d5 6e 63 44 65 66 0a 2a 2a 20 73 74 72 75 63 74 75  ncDef.** structu
208d6 72 65 20 69 73 20 63 72 65 61 74 65 64 20 61 6e  re is created an
208d7 64 20 6c 69 6b 65 64 20 69 6e 74 6f 20 74 68 65  d liked into the
208d8 20 22 64 62 22 20 73 74 72 75 63 74 75 72 65 20   "db" structure 
208d9 69 66 20 61 0a 2a 2a 20 6e 6f 20 6d 61 74 63 68  if a.** no match
208da 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 70 72 65  ing function pre
208db 76 69 6f 75 73 6c 79 20 65 78 69 73 74 65 64 2e  viously existed.
208dc 20 20 57 68 65 6e 20 63 72 65 61 74 65 46 6c 61    When createFla
208dd 67 20 69 73 20 74 72 75 65 0a 2a 2a 20 61 6e 64  g is true.** and
208de 20 74 68 65 20 6e 41 72 67 20 70 61 72 61 6d 65   the nArg parame
208df 74 65 72 20 69 73 20 2d 31 2c 20 74 68 65 6e 20  ter is -1, then 
208e0 6f 6e 6c 79 20 61 20 66 75 6e 63 74 69 6f 6e 20  only a function 
208e1 74 68 61 74 20 61 63 63 65 70 74 73 0a 2a 2a 20  that accepts.** 
208e2 61 6e 79 20 6e 75 6d 62 65 72 20 6f 66 20 61 72  any number of ar
208e3 67 75 6d 65 6e 74 73 20 77 69 6c 6c 20 62 65 20  guments will be 
208e4 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20  returned..**.** 
208e5 49 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73  If createFlag is
208e6 20 66 61 6c 73 65 20 61 6e 64 20 6e 41 72 67 20   false and nArg 
208e7 69 73 20 2d 31 2c 20 74 68 65 6e 20 74 68 65 20  is -1, then the 
208e8 66 69 72 73 74 20 76 61 6c 69 64 0a 2a 2a 20 66  first valid.** f
208e9 75 6e 63 74 69 6f 6e 20 66 6f 75 6e 64 20 69 73  unction found is
208ea 20 72 65 74 75 72 6e 65 64 2e 20 20 41 20 66 75   returned.  A fu
208eb 6e 63 74 69 6f 6e 20 69 73 20 76 61 6c 69 64 20  nction is valid 
208ec 69 66 20 65 69 74 68 65 72 20 78 46 75 6e 63 0a  if either xFunc.
208ed 2a 2a 20 6f 72 20 78 53 74 65 70 20 69 73 20 6e  ** or xStep is n
208ee 6f 6e 2d 7a 65 72 6f 2e 0a 2a 2a 0a 2a 2a 20 49  on-zero..**.** I
208ef 66 20 63 72 65 61 74 65 46 6c 61 67 20 69 73 20  f createFlag is 
208f0 66 61 6c 73 65 2c 20 74 68 65 6e 20 61 20 66 75  false, then a fu
208f1 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68 65 20  nction with the 
208f2 72 65 71 75 69 72 65 64 20 6e 61 6d 65 20 61 6e  required name an
208f3 64 0a 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20 61  d.** number of a
208f4 72 67 75 6d 65 6e 74 73 20 6d 61 79 20 62 65 20  rguments may be 
208f5 72 65 74 75 72 6e 65 64 20 65 76 65 6e 20 69 66  returned even if
208f6 20 74 68 65 20 65 54 65 78 74 52 65 70 20 66 6c   the eTextRep fl
208f7 61 67 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 6d  ag does not.** m
208f8 61 74 63 68 20 74 68 61 74 20 72 65 71 75 65 73  atch that reques
208f9 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ted..*/.SQLITE_P
208fa 52 49 56 41 54 45 20 46 75 6e 63 44 65 66 20 2a  RIVATE FuncDef *
208fb 73 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74  sqlite3FindFunct
208fc 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ion(.  sqlite3 *
208fd 64 62 2c 20 20 20 20 20 20 20 2f 2a 20 41 6e 20  db,       /* An 
208fe 6f 70 65 6e 20 64 61 74 61 62 61 73 65 20 2a 2f  open database */
208ff 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
20900 4e 61 6d 65 2c 20 2f 2a 20 4e 61 6d 65 20 6f 66  Name, /* Name of
20901 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20   the function.  
20902 4e 6f 74 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  Not null-termina
20903 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61  ted */.  int nNa
20904 6d 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  me,         /* N
20905 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
20906 65 72 73 20 69 6e 20 74 68 65 20 6e 61 6d 65 20  ers in the name 
20907 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
20908 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
20909 72 20 6f 66 20 61 72 67 75 6d 65 6e 74 73 2e 20  r of arguments. 
2090a 20 2d 31 20 6d 65 61 6e 73 20 61 6e 79 20 6e 75   -1 means any nu
2090b 6d 62 65 72 20 2a 2f 0a 20 20 75 38 20 65 6e 63  mber */.  u8 enc
2090c 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2090d 50 72 65 66 65 72 72 65 64 20 74 65 78 74 20 65  Preferred text e
2090e 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 69 6e 74  ncoding */.  int
2090f 20 63 72 65 61 74 65 46 6c 61 67 20 20 20 20 20   createFlag     
20910 2f 2a 20 43 72 65 61 74 65 20 6e 65 77 20 65 6e  /* Create new en
20911 74 72 79 20 69 66 20 74 72 75 65 20 61 6e 64 20  try if true and 
20912 64 6f 65 73 20 6e 6f 74 20 6f 74 68 65 72 77 69  does not otherwi
20913 73 65 20 65 78 69 73 74 20 2a 2f 0a 29 7b 0a 20  se exist */.){. 
20914 20 46 75 6e 63 44 65 66 20 2a 70 3b 20 20 20 20   FuncDef *p;    
20915 20 20 20 20 20 2f 2a 20 49 74 65 72 61 74 6f 72       /* Iterator
20916 20 76 61 72 69 61 62 6c 65 20 2a 2f 0a 20 20 46   variable */.  F
20917 75 6e 63 44 65 66 20 2a 70 42 65 73 74 20 3d 20  uncDef *pBest = 
20918 30 3b 20 2f 2a 20 42 65 73 74 20 6d 61 74 63 68  0; /* Best match
20919 20 66 6f 75 6e 64 20 73 6f 20 66 61 72 20 2a 2f   found so far */
2091a 0a 20 20 69 6e 74 20 62 65 73 74 53 63 6f 72 65  .  int bestScore
2091b 20 3d 20 30 3b 20 20 2f 2a 20 53 63 6f 72 65 20   = 0;  /* Score 
2091c 6f 66 20 62 65 73 74 20 6d 61 74 63 68 20 2a 2f  of best match */
2091d 0a 20 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20  .  int h;       
2091e 20 20 20 20 20 20 20 2f 2a 20 48 61 73 68 20 76         /* Hash v
2091f 61 6c 75 65 20 2a 2f 0a 0a 0a 20 20 61 73 73 65  alue */...  asse
20920 72 74 28 20 65 6e 63 3d 3d 53 51 4c 49 54 45 5f  rt( enc==SQLITE_
20921 55 54 46 38 20 7c 7c 20 65 6e 63 3d 3d 53 51 4c  UTF8 || enc==SQL
20922 49 54 45 5f 55 54 46 31 36 4c 45 20 7c 7c 20 65  ITE_UTF16LE || e
20923 6e 63 3d 3d 53 51 4c 49 54 45 5f 55 54 46 31 36  nc==SQLITE_UTF16
20924 42 45 20 29 3b 0a 20 20 69 66 28 20 6e 41 72 67  BE );.  if( nArg
20925 3c 2d 31 20 29 20 6e 41 72 67 20 3d 20 2d 31 3b  <-1 ) nArg = -1;
20926 0a 20 20 68 20 3d 20 28 73 71 6c 69 74 65 33 55  .  h = (sqlite3U
20927 70 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 38 29  pperToLower[(u8)
20928 7a 4e 61 6d 65 5b 30 5d 5d 20 2b 20 6e 4e 61 6d  zName[0]] + nNam
20929 65 29 20 25 20 41 72 72 61 79 53 69 7a 65 28 64  e) % ArraySize(d
2092a 62 2d 3e 61 46 75 6e 63 2e 61 29 3b 0a 0a 20 20  b->aFunc.a);..  
2092b 2f 2a 20 46 69 72 73 74 20 73 65 61 72 63 68 20  /* First search 
2092c 66 6f 72 20 61 20 6d 61 74 63 68 20 61 6d 6f 6e  for a match amon
2092d 67 73 74 20 74 68 65 20 61 70 70 6c 69 63 61 74  gst the applicat
2092e 69 6f 6e 2d 64 65 66 69 6e 65 64 20 66 75 6e 63  ion-defined func
2092f 74 69 6f 6e 73 2e 0a 20 20 2a 2f 0a 20 20 70 20  tions..  */.  p 
20930 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61 72 63 68  = functionSearch
20931 28 26 64 62 2d 3e 61 46 75 6e 63 2c 20 68 2c 20  (&db->aFunc, h, 
20932 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20  zName, nName);. 
20933 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
20934 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d 61 74   int score = mat
20935 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e 41 72  chQuality(p, nAr
20936 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 69 66 28  g, enc);.    if(
20937 20 73 63 6f 72 65 3e 62 65 73 74 53 63 6f 72 65   score>bestScore
20938 20 29 7b 0a 20 20 20 20 20 20 70 42 65 73 74 20   ){.      pBest 
20939 3d 20 70 3b 0a 20 20 20 20 20 20 62 65 73 74 53  = p;.      bestS
2093a 63 6f 72 65 20 3d 20 73 63 6f 72 65 3b 0a 20 20  core = score;.  
2093b 20 20 7d 0a 20 20 20 20 70 20 3d 20 70 2d 3e 70    }.    p = p->p
2093c 4e 65 78 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  Next;.  }..  /* 
2093d 49 66 20 6e 6f 20 6d 61 74 63 68 20 69 73 20 66  If no match is f
2093e 6f 75 6e 64 2c 20 73 65 61 72 63 68 20 74 68 65  ound, search the
2093f 20 62 75 69 6c 74 2d 69 6e 20 66 75 6e 63 74 69   built-in functi
20940 6f 6e 73 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 45  ons..  **.  ** E
20941 78 63 65 70 74 2c 20 69 66 20 63 72 65 61 74 65  xcept, if create
20942 46 6c 61 67 20 69 73 20 74 72 75 65 2c 20 74 68  Flag is true, th
20943 61 74 20 6d 65 61 6e 73 20 74 68 61 74 20 77 65  at means that we
20944 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 0a 20   are trying to. 
20945 20 2a 2a 20 69 6e 73 74 61 6c 6c 20 61 20 6e 65   ** install a ne
20946 77 20 66 75 6e 63 74 69 6f 6e 2e 20 20 57 68 61  w function.  Wha
20947 74 65 76 65 72 20 46 75 6e 63 44 65 66 20 73 74  tever FuncDef st
20948 72 75 63 74 75 72 65 20 69 73 20 72 65 74 75 72  ructure is retur
20949 6e 65 64 20 77 69 6c 6c 0a 20 20 2a 2a 20 68 61  ned will.  ** ha
2094a 76 65 20 66 69 65 6c 64 73 20 6f 76 65 72 77 72  ve fields overwr
2094b 69 74 74 65 6e 20 77 69 74 68 20 6e 65 77 20 69  itten with new i
2094c 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 70 70 72 6f  nformation appro
2094d 70 72 69 61 74 65 20 66 6f 72 20 74 68 65 0a 20  priate for the. 
2094e 20 2a 2a 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e   ** new function
2094f 2e 20 20 42 75 74 20 74 68 65 20 46 75 6e 63 44  .  But the FuncD
20950 65 66 73 20 66 6f 72 20 62 75 69 6c 74 2d 69 6e  efs for built-in
20951 20 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 72   functions are r
20952 65 61 64 2d 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53  ead-only..  ** S
20953 6f 20 77 65 20 6d 75 73 74 20 6e 6f 74 20 73 65  o we must not se
20954 61 72 63 68 20 66 6f 72 20 62 75 69 6c 74 2d 69  arch for built-i
20955 6e 73 20 77 68 65 6e 20 63 72 65 61 74 69 6e 67  ns when creating
20956 20 61 20 6e 65 77 20 66 75 6e 63 74 69 6f 6e 2e   a new function.
20957 0a 20 20 2a 2f 20 0a 20 20 69 66 28 20 21 63 72  .  */ .  if( !cr
20958 65 61 74 65 46 6c 61 67 20 26 26 20 21 70 42 65  eateFlag && !pBe
20959 73 74 20 29 7b 0a 20 20 20 20 46 75 6e 63 44 65  st ){.    FuncDe
2095a 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 26  fHash *pHash = &
2095b 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48 61  GLOBAL(FuncDefHa
2095c 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61  sh, sqlite3Globa
2095d 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20 20  lFunctions);.   
2095e 20 70 20 3d 20 66 75 6e 63 74 69 6f 6e 53 65 61   p = functionSea
2095f 72 63 68 28 70 48 61 73 68 2c 20 68 2c 20 7a 4e  rch(pHash, h, zN
20960 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20  ame, nName);.   
20961 20 77 68 69 6c 65 28 20 70 20 29 7b 0a 20 20 20   while( p ){.   
20962 20 20 20 69 6e 74 20 73 63 6f 72 65 20 3d 20 6d     int score = m
20963 61 74 63 68 51 75 61 6c 69 74 79 28 70 2c 20 6e  atchQuality(p, n
20964 41 72 67 2c 20 65 6e 63 29 3b 0a 20 20 20 20 20  Arg, enc);.     
20965 20 69 66 28 20 73 63 6f 72 65 3e 62 65 73 74 53   if( score>bestS
20966 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 20 20  core ){.        
20967 70 42 65 73 74 20 3d 20 70 3b 0a 20 20 20 20 20  pBest = p;.     
20968 20 20 20 62 65 73 74 53 63 6f 72 65 20 3d 20 73     bestScore = s
20969 63 6f 72 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  core;.      }.  
2096a 20 20 20 20 70 20 3d 20 70 2d 3e 70 4e 65 78 74      p = p->pNext
2096b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
2096c 2a 20 49 66 20 74 68 65 20 63 72 65 61 74 65 46  * If the createF
2096d 6c 61 67 20 70 61 72 61 6d 65 74 65 72 20 69 73  lag parameter is
2096e 20 74 72 75 65 20 61 6e 64 20 74 68 65 20 73 65   true and the se
2096f 61 72 63 68 20 64 69 64 20 6e 6f 74 20 72 65 76  arch did not rev
20970 65 61 6c 20 61 6e 0a 20 20 2a 2a 20 65 78 61 63  eal an.  ** exac
20971 74 20 6d 61 74 63 68 20 66 6f 72 20 74 68 65 20  t match for the 
20972 6e 61 6d 65 2c 20 6e 75 6d 62 65 72 20 6f 66 20  name, number of 
20973 61 72 67 75 6d 65 6e 74 73 20 61 6e 64 20 65 6e  arguments and en
20974 63 6f 64 69 6e 67 2c 20 74 68 65 6e 20 61 64 64  coding, then add
20975 20 61 0a 20 20 2a 2a 20 6e 65 77 20 65 6e 74 72   a.  ** new entr
20976 79 20 74 6f 20 74 68 65 20 68 61 73 68 20 74 61  y to the hash ta
20977 62 6c 65 20 61 6e 64 20 72 65 74 75 72 6e 20 69  ble and return i
20978 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 63 72  t..  */.  if( cr
20979 65 61 74 65 46 6c 61 67 20 26 26 20 28 62 65 73  eateFlag && (bes
2097a 74 53 63 6f 72 65 3c 36 20 7c 7c 20 70 42 65 73  tScore<6 || pBes
2097b 74 2d 3e 6e 41 72 67 21 3d 6e 41 72 67 29 20 26  t->nArg!=nArg) &
2097c 26 20 0a 20 20 20 20 20 20 28 70 42 65 73 74 20  & .      (pBest 
2097d 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
2097e 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
2097f 28 2a 70 42 65 73 74 29 2b 6e 4e 61 6d 65 2b 31  (*pBest)+nName+1
20980 29 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 42 65  ))!=0 ){.    pBe
20981 73 74 2d 3e 7a 4e 61 6d 65 20 3d 20 28 63 68 61  st->zName = (cha
20982 72 20 2a 29 26 70 42 65 73 74 5b 31 5d 3b 0a 20  r *)&pBest[1];. 
20983 20 20 20 70 42 65 73 74 2d 3e 6e 41 72 67 20 3d     pBest->nArg =
20984 20 6e 41 72 67 3b 0a 20 20 20 20 70 42 65 73 74   nArg;.    pBest
20985 2d 3e 69 50 72 65 66 45 6e 63 20 3d 20 65 6e 63  ->iPrefEnc = enc
20986 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70 42 65  ;.    memcpy(pBe
20987 73 74 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65  st->zName, zName
20988 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20 20 20 70 42  , nName);.    pB
20989 65 73 74 2d 3e 7a 4e 61 6d 65 5b 6e 4e 61 6d 65  est->zName[nName
2098a 5d 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74  ] = 0;.    sqlit
2098b 65 33 46 75 6e 63 44 65 66 49 6e 73 65 72 74 28  e3FuncDefInsert(
2098c 26 64 62 2d 3e 61 46 75 6e 63 2c 20 70 42 65 73  &db->aFunc, pBes
2098d 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 70  t);.  }..  if( p
2098e 42 65 73 74 20 26 26 20 28 70 42 65 73 74 2d 3e  Best && (pBest->
2098f 78 53 74 65 70 20 7c 7c 20 70 42 65 73 74 2d 3e  xStep || pBest->
20990 78 46 75 6e 63 20 7c 7c 20 63 72 65 61 74 65 46  xFunc || createF
20991 6c 61 67 29 20 29 7b 0a 20 20 20 20 72 65 74 75  lag) ){.    retu
20992 72 6e 20 70 42 65 73 74 3b 0a 20 20 7d 0a 20 20  rn pBest;.  }.  
20993 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a  return 0;.}../*.
20994 2a 2a 20 46 72 65 65 20 61 6c 6c 20 72 65 73 6f  ** Free all reso
20995 75 72 63 65 73 20 68 65 6c 64 20 62 79 20 74 68  urces held by th
20996 65 20 73 63 68 65 6d 61 20 73 74 72 75 63 74 75  e schema structu
20997 72 65 2e 20 54 68 65 20 76 6f 69 64 2a 20 61 72  re. The void* ar
20998 67 75 6d 65 6e 74 20 70 6f 69 6e 74 73 0a 2a 2a  gument points.**
20999 20 61 74 20 61 20 53 63 68 65 6d 61 20 73 74 72   at a Schema str
2099a 75 63 74 2e 20 54 68 69 73 20 66 75 6e 63 74 69  uct. This functi
2099b 6f 6e 20 64 6f 65 73 20 6e 6f 74 20 63 61 6c 6c  on does not call
2099c 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
2099d 62 2c 20 29 20 6f 6e 20 74 68 65 20 0a 2a 2a 20  b, ) on the .** 
2099e 70 6f 69 6e 74 65 72 20 69 74 73 65 6c 66 2c 20  pointer itself, 
2099f 69 74 20 6a 75 73 74 20 63 6c 65 61 6e 73 20 75  it just cleans u
209a0 70 20 73 75 62 73 69 64 75 61 72 79 20 72 65 73  p subsiduary res
209a1 6f 75 72 63 65 73 20 28 69 2e 65 2e 20 74 68 65  ources (i.e. the
209a2 20 63 6f 6e 74 65 6e 74 73 0a 2a 2a 20 6f 66 20   contents.** of 
209a3 74 68 65 20 73 63 68 65 6d 61 20 68 61 73 68 20  the schema hash 
209a4 74 61 62 6c 65 73 29 2e 0a 2a 2a 0a 2a 2a 20 54  tables)..**.** T
209a5 68 65 20 53 63 68 65 6d 61 2e 63 61 63 68 65 5f  he Schema.cache_
209a6 73 69 7a 65 20 76 61 72 69 61 62 6c 65 20 69 73  size variable is
209a7 20 6e 6f 74 20 63 6c 65 61 72 65 64 2e 0a 2a 2f   not cleared..*/
209a8 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
209a9 76 6f 69 64 20 73 71 6c 69 74 65 33 53 63 68 65  void sqlite3Sche
209aa 6d 61 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b  maFree(void *p){
209ab 0a 20 20 48 61 73 68 20 74 65 6d 70 31 3b 0a 20  .  Hash temp1;. 
209ac 20 48 61 73 68 20 74 65 6d 70 32 3b 0a 20 20 48   Hash temp2;.  H
209ad 61 73 68 45 6c 65 6d 20 2a 70 45 6c 65 6d 3b 0a  ashElem *pElem;.
209ae 20 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d    Schema *pSchem
209af 61 20 3d 20 28 53 63 68 65 6d 61 20 2a 29 70 3b  a = (Schema *)p;
209b0 0a 0a 20 20 74 65 6d 70 31 20 3d 20 70 53 63 68  ..  temp1 = pSch
209b1 65 6d 61 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20  ema->tblHash;.  
209b2 74 65 6d 70 32 20 3d 20 70 53 63 68 65 6d 61 2d  temp2 = pSchema-
209b3 3e 74 72 69 67 48 61 73 68 3b 0a 20 20 73 71 6c  >trigHash;.  sql
209b4 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 53  ite3HashInit(&pS
209b5 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 2c  chema->trigHash,
209b6 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 48 61   0);.  sqlite3Ha
209b7 73 68 43 6c 65 61 72 28 26 70 53 63 68 65 6d 61  shClear(&pSchema
209b8 2d 3e 61 46 4b 65 79 29 3b 0a 20 20 73 71 6c 69  ->aFKey);.  sqli
209b9 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 70 53  te3HashClear(&pS
209ba 63 68 65 6d 61 2d 3e 69 64 78 48 61 73 68 29 3b  chema->idxHash);
209bb 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71 6c  .  for(pElem=sql
209bc 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74 65  iteHashFirst(&te
209bd 6d 70 32 29 3b 20 70 45 6c 65 6d 3b 20 70 45 6c  mp2); pElem; pEl
209be 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65 78  em=sqliteHashNex
209bf 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20 73  t(pElem)){.    s
209c0 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72 69 67  qlite3DeleteTrig
209c1 67 65 72 28 30 2c 20 28 54 72 69 67 67 65 72 2a  ger(0, (Trigger*
209c2 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61 28  )sqliteHashData(
209c3 70 45 6c 65 6d 29 29 3b 0a 20 20 7d 0a 20 20 73  pElem));.  }.  s
209c4 71 6c 69 74 65 33 48 61 73 68 43 6c 65 61 72 28  qlite3HashClear(
209c5 26 74 65 6d 70 32 29 3b 0a 20 20 73 71 6c 69 74  &temp2);.  sqlit
209c6 65 33 48 61 73 68 49 6e 69 74 28 26 70 53 63 68  e3HashInit(&pSch
209c7 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 30 29  ema->tblHash, 0)
209c8 3b 0a 20 20 66 6f 72 28 70 45 6c 65 6d 3d 73 71  ;.  for(pElem=sq
209c9 6c 69 74 65 48 61 73 68 46 69 72 73 74 28 26 74  liteHashFirst(&t
209ca 65 6d 70 31 29 3b 20 70 45 6c 65 6d 3b 20 70 45  emp1); pElem; pE
209cb 6c 65 6d 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  lem=sqliteHashNe
209cc 78 74 28 70 45 6c 65 6d 29 29 7b 0a 20 20 20 20  xt(pElem)){.    
209cd 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71  Table *pTab = sq
209ce 6c 69 74 65 48 61 73 68 44 61 74 61 28 70 45 6c  liteHashData(pEl
209cf 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  em);.    sqlite3
209d0 44 65 6c 65 74 65 54 61 62 6c 65 28 70 54 61 62  DeleteTable(pTab
209d1 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
209d2 48 61 73 68 43 6c 65 61 72 28 26 74 65 6d 70 31  HashClear(&temp1
209d3 29 3b 0a 20 20 70 53 63 68 65 6d 61 2d 3e 70 53  );.  pSchema->pS
209d4 65 71 54 61 62 20 3d 20 30 3b 0a 20 20 70 53 63  eqTab = 0;.  pSc
209d5 68 65 6d 61 2d 3e 66 6c 61 67 73 20 26 3d 20 7e  hema->flags &= ~
209d6 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 3b  DB_SchemaLoaded;
209d7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 61  .}../*.** Find a
209d8 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 73 63  nd return the sc
209d9 68 65 6d 61 20 61 73 73 6f 63 69 61 74 65 64 20  hema associated 
209da 77 69 74 68 20 61 20 42 54 72 65 65 2e 20 20 43  with a BTree.  C
209db 72 65 61 74 65 0a 2a 2a 20 61 20 6e 65 77 20 6f  reate.** a new o
209dc 6e 65 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e  ne if necessary.
209dd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
209de 54 45 20 53 63 68 65 6d 61 20 2a 73 71 6c 69 74  TE Schema *sqlit
209df 65 33 53 63 68 65 6d 61 47 65 74 28 73 71 6c 69  e3SchemaGet(sqli
209e0 74 65 33 20 2a 64 62 2c 20 42 74 72 65 65 20 2a  te3 *db, Btree *
209e1 70 42 74 29 7b 0a 20 20 53 63 68 65 6d 61 20 2a  pBt){.  Schema *
209e2 20 70 3b 0a 20 20 69 66 28 20 70 42 74 20 29 7b   p;.  if( pBt ){
209e3 0a 20 20 20 20 70 20 3d 20 28 53 63 68 65 6d 61  .    p = (Schema
209e4 20 2a 29 73 71 6c 69 74 65 33 42 74 72 65 65 53   *)sqlite3BtreeS
209e5 63 68 65 6d 61 28 70 42 74 2c 20 73 69 7a 65 6f  chema(pBt, sizeo
209e6 66 28 53 63 68 65 6d 61 29 2c 20 73 71 6c 69 74  f(Schema), sqlit
209e7 65 33 53 63 68 65 6d 61 46 72 65 65 29 3b 0a 20  e3SchemaFree);. 
209e8 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 20 3d 20   }else{.    p = 
209e9 28 53 63 68 65 6d 61 20 2a 29 73 71 6c 69 74 65  (Schema *)sqlite
209ea 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 69 7a 65  3MallocZero(size
209eb 6f 66 28 53 63 68 65 6d 61 29 29 3b 0a 20 20 7d  of(Schema));.  }
209ec 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20 20 20  .  if( !p ){.   
209ed 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65   db->mallocFaile
209ee 64 20 3d 20 31 3b 0a 20 20 7d 65 6c 73 65 20 69  d = 1;.  }else i
209ef 66 20 28 20 30 3d 3d 70 2d 3e 66 69 6c 65 5f 66  f ( 0==p->file_f
209f0 6f 72 6d 61 74 20 29 7b 0a 20 20 20 20 73 71 6c  ormat ){.    sql
209f1 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d  ite3HashInit(&p-
209f2 3e 74 62 6c 48 61 73 68 2c 20 30 29 3b 0a 20 20  >tblHash, 0);.  
209f3 20 20 73 71 6c 69 74 65 33 48 61 73 68 49 6e 69    sqlite3HashIni
209f4 74 28 26 70 2d 3e 69 64 78 48 61 73 68 2c 20 30  t(&p->idxHash, 0
209f5 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 48 61  );.    sqlite3Ha
209f6 73 68 49 6e 69 74 28 26 70 2d 3e 74 72 69 67 48  shInit(&p->trigH
209f7 61 73 68 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  ash, 0);.    sql
209f8 69 74 65 33 48 61 73 68 49 6e 69 74 28 26 70 2d  ite3HashInit(&p-
209f9 3e 61 46 4b 65 79 2c 20 31 29 3b 0a 20 20 20 20  >aFKey, 1);.    
209fa 70 2d 3e 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f  p->enc = SQLITE_
209fb 55 54 46 38 3b 0a 20 20 7d 0a 20 20 72 65 74 75  UTF8;.  }.  retu
209fc 72 6e 20 70 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  rn p;.}../******
209fd 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
209fe 63 61 6c 6c 62 61 63 6b 2e 63 20 2a 2a 2a 2a 2a  callback.c *****
209ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a00 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a01 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
20a02 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
20a03 69 6c 65 20 64 65 6c 65 74 65 2e 63 20 2a 2a 2a  ile delete.c ***
20a04 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a06 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
20a07 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35  001 September 15
20a08 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
20a09 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
20a0a 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
20a0b 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
20a0c 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
20a0d 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
20a0e 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
20a0f 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
20a10 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
20a11 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
20a12 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
20a13 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
20a14 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
20a15 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
20a16 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
20a17 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
20a18 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
20a19 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
20a1a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a1b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20a1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
20a1e 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
20a1f 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f 75  tains C code rou
20a20 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20 63  tines that are c
20a21 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61 72  alled by the par
20a22 73 65 72 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20  ser.** in order 
20a23 74 6f 20 67 65 6e 65 72 61 74 65 20 63 6f 64 65  to generate code
20a24 20 66 6f 72 20 44 45 4c 45 54 45 20 46 52 4f 4d   for DELETE FROM
20a25 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
20a26 2a 2a 20 24 49 64 3a 20 64 65 6c 65 74 65 2e 63  ** $Id: delete.c
20a27 2c 76 20 31 2e 31 38 37 20 32 30 30 38 2f 31 31  ,v 1.187 2008/11
20a28 2f 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e  /19 09:05:27 dan
20a29 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
20a2a 2f 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 6f 6b 20 75 70  /../*.** Look up
20a2b 20 65 76 65 72 79 20 74 61 62 6c 65 20 74 68 61   every table tha
20a2c 74 20 69 73 20 6e 61 6d 65 64 20 69 6e 20 70 53  t is named in pS
20a2d 72 63 2e 20 20 49 66 20 61 6e 79 20 74 61 62 6c  rc.  If any tabl
20a2e 65 20 69 73 20 6e 6f 74 20 66 6f 75 6e 64 2c 0a  e is not found,.
20a2f 2a 2a 20 61 64 64 20 61 6e 20 65 72 72 6f 72 20  ** add an error 
20a30 6d 65 73 73 61 67 65 20 74 6f 20 70 50 61 72 73  message to pPars
20a31 65 2d 3e 7a 45 72 72 4d 73 67 20 61 6e 64 20 72  e->zErrMsg and r
20a32 65 74 75 72 6e 20 4e 55 4c 4c 2e 20 20 49 66 20  eturn NULL.  If 
20a33 61 6c 6c 20 74 61 62 6c 65 73 0a 2a 2a 20 61 72  all tables.** ar
20a34 65 20 66 6f 75 6e 64 2c 20 72 65 74 75 72 6e 20  e found, return 
20a35 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  a pointer to the
20a36 20 6c 61 73 74 20 74 61 62 6c 65 2e 0a 2a 2f 0a   last table..*/.
20a37 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54  SQLITE_PRIVATE T
20a38 61 62 6c 65 20 2a 73 71 6c 69 74 65 33 53 72 63  able *sqlite3Src
20a39 4c 69 73 74 4c 6f 6f 6b 75 70 28 50 61 72 73 65  ListLookup(Parse
20a3a 20 2a 70 50 61 72 73 65 2c 20 53 72 63 4c 69 73   *pParse, SrcLis
20a3b 74 20 2a 70 53 72 63 29 7b 0a 20 20 73 74 72 75  t *pSrc){.  stru
20a3c 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
20a3d 2a 70 49 74 65 6d 20 3d 20 70 53 72 63 2d 3e 61  *pItem = pSrc->a
20a3e 3b 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  ;.  Table *pTab;
20a3f 0a 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d  .  assert( pItem
20a40 20 26 26 20 70 53 72 63 2d 3e 6e 53 72 63 3d 3d   && pSrc->nSrc==
20a41 31 20 29 3b 0a 20 20 70 54 61 62 20 3d 20 73 71  1 );.  pTab = sq
20a42 6c 69 74 65 33 4c 6f 63 61 74 65 54 61 62 6c 65  lite3LocateTable
20a43 28 70 50 61 72 73 65 2c 20 30 2c 20 70 49 74 65  (pParse, 0, pIte
20a44 6d 2d 3e 7a 4e 61 6d 65 2c 20 70 49 74 65 6d 2d  m->zName, pItem-
20a45 3e 7a 44 61 74 61 62 61 73 65 29 3b 0a 20 20 73  >zDatabase);.  s
20a46 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62 6c  qlite3DeleteTabl
20a47 65 28 70 49 74 65 6d 2d 3e 70 54 61 62 29 3b 0a  e(pItem->pTab);.
20a48 20 20 70 49 74 65 6d 2d 3e 70 54 61 62 20 3d 20    pItem->pTab = 
20a49 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54 61 62  pTab;.  if( pTab
20a4a 20 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 6e 52   ){.    pTab->nR
20a4b 65 66 2b 2b 3b 0a 20 20 7d 0a 20 20 69 66 28 20  ef++;.  }.  if( 
20a4c 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42 79  sqlite3IndexedBy
20a4d 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20 70  Lookup(pParse, p
20a4e 49 74 65 6d 29 20 29 7b 0a 20 20 20 20 70 54 61  Item) ){.    pTa
20a4f 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74  b = 0;.  }.  ret
20a50 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a 2f 2a 0a  urn pTab;.}../*.
20a51 2a 2a 20 43 68 65 63 6b 20 74 6f 20 6d 61 6b 65  ** Check to make
20a52 20 73 75 72 65 20 74 68 65 20 67 69 76 65 6e 20   sure the given 
20a53 74 61 62 6c 65 20 69 73 20 77 72 69 74 61 62 6c  table is writabl
20a54 65 2e 20 20 49 66 20 69 74 20 69 73 20 6e 6f 74  e.  If it is not
20a55 0a 2a 2a 20 77 72 69 74 61 62 6c 65 2c 20 67 65  .** writable, ge
20a56 6e 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20  nerate an error 
20a57 6d 65 73 73 61 67 65 20 61 6e 64 20 72 65 74 75  message and retu
20a58 72 6e 20 31 2e 20 20 49 66 20 69 74 20 69 73 0a  rn 1.  If it is.
20a59 2a 2a 20 77 72 69 74 61 62 6c 65 20 72 65 74 75  ** writable retu
20a5a 72 6e 20 30 3b 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rn 0;.*/.SQLITE_
20a5b 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
20a5c 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 50 61  te3IsReadOnly(Pa
20a5d 72 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62  rse *pParse, Tab
20a5e 6c 65 20 2a 70 54 61 62 2c 20 69 6e 74 20 76 69  le *pTab, int vi
20a5f 65 77 4f 6b 29 7b 0a 20 20 69 66 28 20 28 28 70  ewOk){.  if( ((p
20a60 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20  Tab->tabFlags & 
20a61 54 46 5f 52 65 61 64 6f 6e 6c 79 29 21 3d 30 0a  TF_Readonly)!=0.
20a62 20 20 20 20 20 20 20 20 26 26 20 28 70 50 61 72          && (pPar
20a63 73 65 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20  se->db->flags & 
20a64 53 51 4c 49 54 45 5f 57 72 69 74 65 53 63 68 65  SQLITE_WriteSche
20a65 6d 61 29 3d 3d 30 0a 20 20 20 20 20 20 20 20 26  ma)==0.        &
20a66 26 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64  & pParse->nested
20a67 3d 3d 30 29 20 0a 23 69 66 6e 64 65 66 20 53 51  ==0) .#ifndef SQ
20a68 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
20a69 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 7c 7c 20  LTABLE.      || 
20a6a 28 70 54 61 62 2d 3e 70 4d 6f 64 20 26 26 20 70  (pTab->pMod && p
20a6b 54 61 62 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75  Tab->pMod->pModu
20a6c 6c 65 2d 3e 78 55 70 64 61 74 65 3d 3d 30 29 0a  le->xUpdate==0).
20a6d 23 65 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20  #endif.  ){.    
20a6e 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
20a6f 70 50 61 72 73 65 2c 20 22 74 61 62 6c 65 20 25  pParse, "table %
20a70 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 6d 6f 64  s may not be mod
20a71 69 66 69 65 64 22 2c 20 70 54 61 62 2d 3e 7a 4e  ified", pTab->zN
20a72 61 6d 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  ame);.    return
20a73 20 31 3b 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   1;.  }.#ifndef 
20a74 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57  SQLITE_OMIT_VIEW
20a75 0a 20 20 69 66 28 20 21 76 69 65 77 4f 6b 20 26  .  if( !viewOk &
20a76 26 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  & pTab->pSelect 
20a77 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
20a78 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 22 63  rorMsg(pParse,"c
20a79 61 6e 6e 6f 74 20 6d 6f 64 69 66 79 20 25 73 20  annot modify %s 
20a7a 62 65 63 61 75 73 65 20 69 74 20 69 73 20 61 20  because it is a 
20a7b 76 69 65 77 22 2c 70 54 61 62 2d 3e 7a 4e 61 6d  view",pTab->zNam
20a7c 65 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  e);.    return 1
20a7d 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 72  ;.  }.#endif.  r
20a7e 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
20a7f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
20a80 74 68 61 74 20 77 69 6c 6c 20 6f 70 65 6e 20 61  that will open a
20a81 20 74 61 62 6c 65 20 66 6f 72 20 72 65 61 64 69   table for readi
20a82 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ng..*/.SQLITE_PR
20a83 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20a84 65 33 4f 70 65 6e 54 61 62 6c 65 28 0a 20 20 50  e3OpenTable(.  P
20a85 61 72 73 65 20 2a 70 2c 20 20 20 20 20 20 20 2f  arse *p,       /
20a86 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
20a87 69 6e 74 6f 20 74 68 69 73 20 56 44 42 45 20 2a  into this VDBE *
20a88 2f 0a 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20  /.  int iCur,   
20a89 20 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f      /* The curso
20a8a 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  r number of the 
20a8b 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69  table */.  int i
20a8c 44 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  Db,        /* Th
20a8d 65 20 64 61 74 61 62 61 73 65 20 69 6e 64 65 78  e database index
20a8e 20 69 6e 20 73 71 6c 69 74 65 33 2e 61 44 62 5b   in sqlite3.aDb[
20a8f 5d 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  ] */.  Table *pT
20a90 61 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 74 61  ab,    /* The ta
20a91 62 6c 65 20 74 6f 20 62 65 20 6f 70 65 6e 65 64  ble to be opened
20a92 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 63 6f 64 65   */.  int opcode
20a93 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f 70 65 6e        /* OP_Open
20a94 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70 65 6e 57  Read or OP_OpenW
20a95 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62  rite */.){.  Vdb
20a96 65 20 2a 76 3b 0a 20 20 69 66 28 20 49 73 56 69  e *v;.  if( IsVi
20a97 72 74 75 61 6c 28 70 54 61 62 29 20 29 20 72 65  rtual(pTab) ) re
20a98 74 75 72 6e 3b 0a 20 20 76 20 3d 20 73 71 6c 69  turn;.  v = sqli
20a99 74 65 33 47 65 74 56 64 62 65 28 70 29 3b 0a 20  te3GetVdbe(p);. 
20a9a 20 61 73 73 65 72 74 28 20 6f 70 63 6f 64 65 3d   assert( opcode=
20a9b 3d 4f 50 5f 4f 70 65 6e 57 72 69 74 65 20 7c 7c  =OP_OpenWrite ||
20a9c 20 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e   opcode==OP_Open
20a9d 52 65 61 64 20 29 3b 0a 20 20 73 71 6c 69 74 65  Read );.  sqlite
20a9e 33 54 61 62 6c 65 4c 6f 63 6b 28 70 2c 20 69 44  3TableLock(p, iD
20a9f 62 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 28  b, pTab->tnum, (
20aa0 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 57  opcode==OP_OpenW
20aa1 72 69 74 65 29 2c 20 70 54 61 62 2d 3e 7a 4e 61  rite), pTab->zNa
20aa2 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  me);.  sqlite3Vd
20aa3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53  beAddOp2(v, OP_S
20aa4 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c  etNumColumns, 0,
20aa5 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20   pTab->nCol);.  
20aa6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20aa7 33 28 76 2c 20 6f 70 63 6f 64 65 2c 20 69 43 75  3(v, opcode, iCu
20aa8 72 2c 20 70 54 61 62 2d 3e 74 6e 75 6d 2c 20 69  r, pTab->tnum, i
20aa9 44 62 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65  Db);.  VdbeComme
20aaa 6e 74 28 28 76 2c 20 22 25 73 22 2c 20 70 54 61  nt((v, "%s", pTa
20aab 62 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 7d 0a 0a 0a  b->zName));.}...
20aac 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c  #if !defined(SQL
20aad 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26  ITE_OMIT_VIEW) &
20aae 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54  & !defined(SQLIT
20aaf 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a  E_OMIT_TRIGGER).
20ab0 2f 2a 0a 2a 2a 20 45 76 61 6c 75 61 74 65 20 61  /*.** Evaluate a
20ab1 20 76 69 65 77 20 61 6e 64 20 73 74 6f 72 65 20   view and store 
20ab2 69 74 73 20 72 65 73 75 6c 74 20 69 6e 20 61 6e  its result in an
20ab3 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
20ab4 2e 20 20 54 68 65 0a 2a 2a 20 70 57 68 65 72 65  .  The.** pWhere
20ab5 20 61 72 67 75 6d 65 6e 74 20 69 73 20 61 6e 20   argument is an 
20ab6 6f 70 74 69 6f 6e 61 6c 20 57 48 45 52 45 20 63  optional WHERE c
20ab7 6c 61 75 73 65 20 74 68 61 74 20 72 65 73 74 72  lause that restr
20ab8 69 63 74 73 20 74 68 65 0a 2a 2a 20 73 65 74 20  icts the.** set 
20ab9 6f 66 20 72 6f 77 73 20 69 6e 20 74 68 65 20 76  of rows in the v
20aba 69 65 77 20 74 68 61 74 20 61 72 65 20 74 6f 20  iew that are to 
20abb 62 65 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  be added to the 
20abc 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 2e  ephemeral table.
20abd 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
20abe 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 4d  TE void sqlite3M
20abf 61 74 65 72 69 61 6c 69 7a 65 56 69 65 77 28 0a  aterializeView(.
20ac0 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
20ac1 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
20ac2 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
20ac3 61 62 6c 65 20 2a 70 56 69 65 77 2c 20 20 20 20  able *pView,    
20ac4 20 20 20 20 2f 2a 20 56 69 65 77 20 64 65 66 69      /* View defi
20ac5 6e 69 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70 72  nition */.  Expr
20ac6 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20 20   *pWhere,       
20ac7 20 2f 2a 20 4f 70 74 69 6f 6e 61 6c 20 57 48 45   /* Optional WHE
20ac8 52 45 20 63 6c 61 75 73 65 20 74 6f 20 62 65 20  RE clause to be 
20ac9 61 64 64 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69  added */.  int i
20aca 43 75 72 20 20 20 20 20 20 20 20 20 20 20 20 20  Cur             
20acb 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
20acc 20 66 6f 72 20 65 70 68 65 6d 65 72 69 61 6c 20   for ephemerial 
20acd 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 53 65  table */.){.  Se
20ace 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a 20  lectDest dest;. 
20acf 20 53 65 6c 65 63 74 20 2a 70 44 75 70 3b 0a 20   Select *pDup;. 
20ad0 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
20ad1 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20 70 44  Parse->db;..  pD
20ad2 75 70 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65  up = sqlite3Sele
20ad3 63 74 44 75 70 28 64 62 2c 20 70 56 69 65 77 2d  ctDup(db, pView-
20ad4 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 69 66 28  >pSelect);.  if(
20ad5 20 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 53   pWhere ){.    S
20ad6 72 63 4c 69 73 74 20 2a 70 46 72 6f 6d 3b 0a 20  rcList *pFrom;. 
20ad7 20 20 20 54 6f 6b 65 6e 20 76 69 65 77 4e 61 6d     Token viewNam
20ad8 65 3b 0a 20 20 20 20 0a 20 20 20 20 70 57 68 65  e;.    .    pWhe
20ad9 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
20ada 44 75 70 28 64 62 2c 20 70 57 68 65 72 65 29 3b  Dup(db, pWhere);
20adb 0a 20 20 20 20 76 69 65 77 4e 61 6d 65 2e 7a 20  .    viewName.z 
20adc 3d 20 28 75 38 2a 29 70 56 69 65 77 2d 3e 7a 4e  = (u8*)pView->zN
20add 61 6d 65 3b 0a 20 20 20 20 76 69 65 77 4e 61 6d  ame;.    viewNam
20ade 65 2e 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20  e.n = (unsigned 
20adf 69 6e 74 29 73 74 72 6c 65 6e 28 28 63 6f 6e 73  int)strlen((cons
20ae0 74 20 63 68 61 72 2a 29 76 69 65 77 4e 61 6d 65  t char*)viewName
20ae1 2e 7a 29 3b 0a 20 20 20 20 70 46 72 6f 6d 20 3d  .z);.    pFrom =
20ae2 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
20ae3 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70 50  ppendFromTerm(pP
20ae4 61 72 73 65 2c 20 30 2c 20 30 2c 20 30 2c 20 26  arse, 0, 0, 0, &
20ae5 76 69 65 77 4e 61 6d 65 2c 20 70 44 75 70 2c 20  viewName, pDup, 
20ae6 30 2c 30 29 3b 0a 20 20 20 20 70 44 75 70 20 3d  0,0);.    pDup =
20ae7 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65   sqlite3SelectNe
20ae8 77 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46 72  w(pParse, 0, pFr
20ae9 6f 6d 2c 20 70 57 68 65 72 65 2c 20 30 2c 20 30  om, pWhere, 0, 0
20aea 2c 20 30 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  , 0, 0, 0, 0);. 
20aeb 20 7d 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65   }.  sqlite3Sele
20aec 63 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74  ctDestInit(&dest
20aed 2c 20 53 52 54 5f 45 70 68 65 6d 54 61 62 2c 20  , SRT_EphemTab, 
20aee 69 43 75 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  iCur);.  sqlite3
20aef 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70  Select(pParse, p
20af0 44 75 70 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  Dup, &dest);.  s
20af1 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
20af2 74 65 28 64 62 2c 20 70 44 75 70 29 3b 0a 7d 0a  te(db, pDup);.}.
20af3 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
20af4 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ed(SQLITE_OMIT_V
20af5 49 45 57 29 20 26 26 20 21 64 65 66 69 6e 65 64  IEW) && !defined
20af6 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49  (SQLITE_OMIT_TRI
20af7 47 47 45 52 29 20 2a 2f 0a 0a 23 69 66 20 64 65  GGER) */..#if de
20af8 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
20af9 42 4c 45 5f 55 50 44 41 54 45 5f 44 45 4c 45 54  BLE_UPDATE_DELET
20afa 45 5f 4c 49 4d 49 54 29 20 26 26 20 21 64 65 66  E_LIMIT) && !def
20afb 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
20afc 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a 2a 2a  _SUBQUERY)./*.**
20afd 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65 78 70   Generate an exp
20afe 72 65 73 73 69 6f 6e 20 74 72 65 65 20 74 6f 20  ression tree to 
20aff 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 57 48  implement the WH
20b00 45 52 45 2c 20 4f 52 44 45 52 20 42 59 2c 0a 2a  ERE, ORDER BY,.*
20b01 2a 20 61 6e 64 20 4c 49 4d 49 54 2f 4f 46 46 53  * and LIMIT/OFFS
20b02 45 54 20 70 6f 72 74 69 6f 6e 20 6f 66 20 44 45  ET portion of DE
20b03 4c 45 54 45 20 61 6e 64 20 55 50 44 41 54 45 20  LETE and UPDATE 
20b04 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a 2a  statements..**.*
20b05 2a 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f  *     DELETE FRO
20b06 4d 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45  M table_wxyz WHE
20b07 52 45 20 61 3c 35 20 4f 52 44 45 52 20 42 59 20  RE a<5 ORDER BY 
20b08 61 20 4c 49 4d 49 54 20 31 3b 0a 2a 2a 20 20 20  a LIMIT 1;.**   
20b09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b0a 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
20b0b 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
20b0c 5f 5f 5f 5f 2f 0a 2a 2a 20 20 20 20 20 20 20 20  ____/.**        
20b0d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20b0e 20 20 20 20 20 20 20 70 4c 69 6d 69 74 57 68 65         pLimitWhe
20b0f 72 65 20 28 70 49 6e 43 6c 61 75 73 65 29 0a 2a  re (pInClause).*
20b10 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
20b11 20 45 78 70 72 20 2a 73 71 6c 69 74 65 33 4c 69   Expr *sqlite3Li
20b12 6d 69 74 57 68 65 72 65 28 0a 20 20 50 61 72 73  mitWhere(.  Pars
20b13 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
20b14 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
20b15 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
20b16 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53 72  /.  SrcList *pSr
20b17 63 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  c,              
20b18 20 2f 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   /* the FROM cla
20b19 75 73 65 20 2d 2d 20 77 68 69 63 68 20 74 61 62  use -- which tab
20b1a 6c 65 73 20 74 6f 20 73 63 61 6e 20 2a 2f 0a 20  les to scan */. 
20b1b 20 45 78 70 72 20 2a 70 57 68 65 72 65 2c 20 20   Expr *pWhere,  
20b1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
20b1d 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75 73   The WHERE claus
20b1e 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
20b1f 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
20b20 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20 20 20  OrderBy,        
20b21 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20 42    /* The ORDER B
20b22 59 20 63 6c 61 75 73 65 2e 20 20 4d 61 79 20 62  Y clause.  May b
20b23 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20 45 78 70 72  e null */.  Expr
20b24 20 2a 70 4c 69 6d 69 74 2c 20 20 20 20 20 20 20   *pLimit,       
20b25 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
20b26 4c 49 4d 49 54 20 63 6c 61 75 73 65 2e 20 20 4d  LIMIT clause.  M
20b27 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f 0a 20 20  ay be null */.  
20b28 45 78 70 72 20 2a 70 4f 66 66 73 65 74 2c 20 20  Expr *pOffset,  
20b29 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
20b2a 54 68 65 20 4f 46 46 53 45 54 20 63 6c 61 75 73  The OFFSET claus
20b2b 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20  e.  May be null 
20b2c 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 53 74 6d 74  */.  char *zStmt
20b2d 54 79 70 65 20 20 20 20 20 20 20 20 20 20 20 20  Type            
20b2e 20 20 2f 2a 20 45 69 74 68 65 72 20 44 45 4c 45    /* Either DELE
20b2f 54 45 20 6f 72 20 55 50 44 41 54 45 2e 20 20 46  TE or UPDATE.  F
20b30 6f 72 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  or error message
20b31 73 2e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  s. */.){.  Expr 
20b32 2a 70 57 68 65 72 65 52 6f 77 69 64 20 3d 20 4e  *pWhereRowid = N
20b33 55 4c 4c 3b 20 20 20 20 2f 2a 20 57 48 45 52 45  ULL;    /* WHERE
20b34 20 72 6f 77 69 64 20 2e 2e 20 2a 2f 0a 20 20 45   rowid .. */.  E
20b35 78 70 72 20 2a 70 49 6e 43 6c 61 75 73 65 20 3d  xpr *pInClause =
20b36 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20 57   NULL;      /* W
20b37 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
20b38 73 65 6c 65 63 74 20 29 20 2a 2f 0a 20 20 45 78  select ) */.  Ex
20b39 70 72 20 2a 70 53 65 6c 65 63 74 52 6f 77 69 64  pr *pSelectRowid
20b3a 20 3d 20 4e 55 4c 4c 3b 20 20 20 2f 2a 20 53 45   = NULL;   /* SE
20b3b 4c 45 43 54 20 72 6f 77 69 64 20 2e 2e 2e 20 2a  LECT rowid ... *
20b3c 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
20b3d 4c 69 73 74 20 3d 20 4e 55 4c 4c 3b 20 20 20 20  List = NULL;    
20b3e 20 2f 2a 20 45 78 70 72 65 73 73 69 6f 6e 20 6c   /* Expression l
20b3f 69 73 74 20 63 6f 6e 74 61 6e 69 6e 67 20 6f 6e  ist contaning on
20b40 6c 79 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20  ly pSelectRowid 
20b41 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
20b42 65 6c 65 63 74 53 72 63 20 3d 20 4e 55 4c 4c 3b  electSrc = NULL;
20b43 20 20 2f 2a 20 53 45 4c 45 43 54 20 72 6f 77 69    /* SELECT rowi
20b44 64 20 46 52 4f 4d 20 78 20 2e 2e 2e 20 28 64 75  d FROM x ... (du
20b45 70 20 6f 66 20 70 53 72 63 29 20 2a 2f 0a 20 20  p of pSrc) */.  
20b46 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20  Select *pSelect 
20b47 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 2f 2a 20  = NULL;      /* 
20b48 43 6f 6d 70 6c 65 74 65 20 53 45 4c 45 43 54 20  Complete SELECT 
20b49 74 72 65 65 20 2a 2f 0a 0a 20 20 2f 2a 20 43 68  tree */..  /* Ch
20b4a 65 63 6b 20 74 68 61 74 20 74 68 65 72 65 20 69  eck that there i
20b4b 73 6e 27 74 20 61 6e 20 4f 52 44 45 52 20 42 59  sn't an ORDER BY
20b4c 20 77 69 74 68 6f 75 74 20 61 20 4c 49 4d 49 54   without a LIMIT
20b4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20   clause..  */.  
20b4e 69 66 28 20 70 4f 72 64 65 72 42 79 20 26 26 20  if( pOrderBy && 
20b4f 28 70 4c 69 6d 69 74 20 3d 3d 20 30 29 20 29 20  (pLimit == 0) ) 
20b50 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
20b51 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 4f  orMsg(pParse, "O
20b52 52 44 45 52 20 42 59 20 77 69 74 68 6f 75 74 20  RDER BY without 
20b53 4c 49 4d 49 54 20 6f 6e 20 25 73 22 2c 20 7a 53  LIMIT on %s", zS
20b54 74 6d 74 54 79 70 65 29 3b 0a 20 20 20 20 70 50  tmtType);.    pP
20b55 61 72 73 65 2d 3e 70 61 72 73 65 45 72 72 6f 72  arse->parseError
20b56 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6c   = 1;.    goto l
20b57 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
20b58 75 70 5f 32 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  up_2;.  }..  /* 
20b59 57 65 20 6f 6e 6c 79 20 6e 65 65 64 20 74 6f 20  We only need to 
20b5a 67 65 6e 65 72 61 74 65 20 61 20 73 65 6c 65 63  generate a selec
20b5b 74 20 65 78 70 72 65 73 73 69 6f 6e 20 69 66 20  t expression if 
20b5c 74 68 65 72 65 0a 20 20 2a 2a 20 69 73 20 61 20  there.  ** is a 
20b5d 6c 69 6d 69 74 2f 6f 66 66 73 65 74 20 74 65 72  limit/offset ter
20b5e 6d 20 74 6f 20 65 6e 66 6f 72 63 65 2e 0a 20 20  m to enforce..  
20b5f 2a 2f 0a 20 20 69 66 28 20 70 4c 69 6d 69 74 20  */.  if( pLimit 
20b60 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20 2f 2a 20  == 0 ) {.    /* 
20b61 69 66 20 70 4c 69 6d 69 74 20 69 73 20 6e 75 6c  if pLimit is nul
20b62 6c 2c 20 70 4f 66 66 73 65 74 20 77 69 6c 6c 20  l, pOffset will 
20b63 61 6c 77 61 79 73 20 62 65 20 6e 75 6c 6c 20 61  always be null a
20b64 73 20 77 65 6c 6c 2e 20 2a 2f 0a 20 20 20 20 61  s well. */.    a
20b65 73 73 65 72 74 28 20 70 4f 66 66 73 65 74 20 3d  ssert( pOffset =
20b66 3d 20 30 20 29 3b 0a 20 20 20 20 72 65 74 75 72  = 0 );.    retur
20b67 6e 20 70 57 68 65 72 65 3b 0a 20 20 7d 0a 0a 20  n pWhere;.  }.. 
20b68 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
20b69 65 6c 65 63 74 20 65 78 70 72 65 73 73 69 6f 6e  elect expression
20b6a 20 74 72 65 65 20 74 6f 20 65 6e 66 6f 72 63 65   tree to enforce
20b6b 20 74 68 65 20 6c 69 6d 69 74 2f 6f 66 66 73 65   the limit/offse
20b6c 74 20 0a 20 20 2a 2a 20 74 65 72 6d 20 66 6f 72  t .  ** term for
20b6d 20 74 68 65 20 44 45 4c 45 54 45 20 6f 72 20 55   the DELETE or U
20b6e 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
20b6f 20 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 20    For example:. 
20b70 20 2a 2a 20 20 20 44 45 4c 45 54 45 20 46 52 4f   **   DELETE FRO
20b71 4d 20 74 61 62 6c 65 5f 61 20 57 48 45 52 45 20  M table_a WHERE 
20b72 63 6f 6c 31 3d 31 20 4f 52 44 45 52 20 42 59 20  col1=1 ORDER BY 
20b73 63 6f 6c 32 20 4c 49 4d 49 54 20 31 20 4f 46 46  col2 LIMIT 1 OFF
20b74 53 45 54 20 31 0a 20 20 2a 2a 20 62 65 63 6f 6d  SET 1.  ** becom
20b75 65 73 3a 0a 20 20 2a 2a 20 20 20 44 45 4c 45 54  es:.  **   DELET
20b76 45 20 46 52 4f 4d 20 74 61 62 6c 65 5f 61 20 57  E FROM table_a W
20b77 48 45 52 45 20 72 6f 77 69 64 20 49 4e 20 28 20  HERE rowid IN ( 
20b78 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45 43 54  .  **     SELECT
20b79 20 72 6f 77 69 64 20 46 52 4f 4d 20 74 61 62 6c   rowid FROM tabl
20b7a 65 5f 61 20 57 48 45 52 45 20 63 6f 6c 31 3d 31  e_a WHERE col1=1
20b7b 20 4f 52 44 45 52 20 42 59 20 63 6f 6c 32 20 4c   ORDER BY col2 L
20b7c 49 4d 49 54 20 31 20 4f 46 46 53 45 54 20 31 0a  IMIT 1 OFFSET 1.
20b7d 20 20 2a 2a 20 20 20 29 3b 0a 20 20 2a 2f 0a 0a    **   );.  */..
20b7e 20 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d    pSelectRowid =
20b7f 20 73 71 6c 69 74 65 33 45 78 70 72 28 70 50 61   sqlite3Expr(pPa
20b80 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f 57 2c  rse->db, TK_ROW,
20b81 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69 66 28   0, 0, 0);.  if(
20b82 20 70 53 65 6c 65 63 74 52 6f 77 69 64 20 3d 3d   pSelectRowid ==
20b83 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f   0 ) goto limit_
20b84 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b  where_cleanup_2;
20b85 0a 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c 69  .  pEList = sqli
20b86 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
20b87 64 28 70 50 61 72 73 65 2c 20 30 2c 20 70 53 65  d(pParse, 0, pSe
20b88 6c 65 63 74 52 6f 77 69 64 2c 20 30 29 3b 0a 20  lectRowid, 0);. 
20b89 20 69 66 28 20 70 45 4c 69 73 74 20 3d 3d 20 30   if( pEList == 0
20b8a 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74 5f 77 68   ) goto limit_wh
20b8b 65 72 65 5f 63 6c 65 61 6e 75 70 5f 32 3b 0a 0a  ere_cleanup_2;..
20b8c 20 20 2f 2a 20 64 75 70 6c 69 63 61 74 65 20 74    /* duplicate t
20b8d 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 61  he FROM clause a
20b8e 73 20 69 74 20 69 73 20 6e 65 65 64 65 64 20 62  s it is needed b
20b8f 79 20 62 6f 74 68 20 74 68 65 20 44 45 4c 45 54  y both the DELET
20b90 45 2f 55 50 44 41 54 45 20 74 72 65 65 0a 20 20  E/UPDATE tree.  
20b91 2a 2a 20 61 6e 64 20 74 68 65 20 53 45 4c 45 43  ** and the SELEC
20b92 54 20 73 75 62 74 72 65 65 2e 20 2a 2f 0a 20 20  T subtree. */.  
20b93 70 53 65 6c 65 63 74 53 72 63 20 3d 20 73 71 6c  pSelectSrc = sql
20b94 69 74 65 33 53 72 63 4c 69 73 74 44 75 70 28 70  ite3SrcListDup(p
20b95 50 61 72 73 65 2d 3e 64 62 2c 20 70 53 72 63 29  Parse->db, pSrc)
20b96 3b 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74 53  ;.  if( pSelectS
20b97 72 63 20 3d 3d 20 30 20 29 20 7b 0a 20 20 20 20  rc == 0 ) {.    
20b98 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
20b99 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
20b9a 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 67  , pEList);.    g
20b9b 6f 74 6f 20 6c 69 6d 69 74 5f 77 68 65 72 65 5f  oto limit_where_
20b9c 63 6c 65 61 6e 75 70 5f 32 3b 0a 20 20 7d 0a 0a  cleanup_2;.  }..
20b9d 20 20 2f 2a 20 67 65 6e 65 72 61 74 65 20 74 68    /* generate th
20b9e 65 20 53 45 4c 45 43 54 20 65 78 70 72 65 73 73  e SELECT express
20b9f 69 6f 6e 20 74 72 65 65 2e 20 2a 2f 0a 20 20 70  ion tree. */.  p
20ba0 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
20ba1 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
20ba2 2c 70 45 4c 69 73 74 2c 70 53 65 6c 65 63 74 53  ,pEList,pSelectS
20ba3 72 63 2c 70 57 68 65 72 65 2c 30 2c 30 2c 70 4f  rc,pWhere,0,0,pO
20ba4 72 64 65 72 42 79 2c 30 2c 70 4c 69 6d 69 74 2c  rderBy,0,pLimit,
20ba5 70 4f 66 66 73 65 74 29 3b 0a 20 20 69 66 28 20  pOffset);.  if( 
20ba6 70 53 65 6c 65 63 74 20 3d 3d 20 30 20 29 20 72  pSelect == 0 ) r
20ba7 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 6e  eturn 0;..  /* n
20ba8 6f 77 20 67 65 6e 65 72 61 74 65 20 74 68 65 20  ow generate the 
20ba9 6e 65 77 20 57 48 45 52 45 20 72 6f 77 69 64 20  new WHERE rowid 
20baa 49 4e 20 63 6c 61 75 73 65 20 66 6f 72 20 74 68  IN clause for th
20bab 65 20 44 45 4c 45 54 45 2f 55 44 50 41 54 45 20  e DELETE/UDPATE 
20bac 2a 2f 0a 20 20 70 57 68 65 72 65 52 6f 77 69 64  */.  pWhereRowid
20bad 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 70   = sqlite3Expr(p
20bae 50 61 72 73 65 2d 3e 64 62 2c 20 54 4b 5f 52 4f  Parse->db, TK_RO
20baf 57 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 69  W, 0, 0, 0);.  i
20bb0 66 28 20 70 57 68 65 72 65 52 6f 77 69 64 20 3d  f( pWhereRowid =
20bb1 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69 74  = 0 ) goto limit
20bb2 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f 31  _where_cleanup_1
20bb3 3b 0a 20 20 70 49 6e 43 6c 61 75 73 65 20 3d 20  ;.  pInClause = 
20bb4 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
20bb5 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 70 57 68 65  rse, TK_IN, pWhe
20bb6 72 65 52 6f 77 69 64 2c 20 30 2c 20 30 29 3b 0a  reRowid, 0, 0);.
20bb7 20 20 69 66 28 20 70 49 6e 43 6c 61 75 73 65 20    if( pInClause 
20bb8 3d 3d 20 30 20 29 20 67 6f 74 6f 20 6c 69 6d 69  == 0 ) goto limi
20bb9 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e 75 70 5f  t_where_cleanup_
20bba 31 3b 0a 0a 20 20 70 49 6e 43 6c 61 75 73 65 2d  1;..  pInClause-
20bbb 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65  >pSelect = pSele
20bbc 63 74 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  ct;.  sqlite3Exp
20bbd 72 53 65 74 48 65 69 67 68 74 28 70 50 61 72 73  rSetHeight(pPars
20bbe 65 2c 20 70 49 6e 43 6c 61 75 73 65 29 3b 0a 20  e, pInClause);. 
20bbf 20 72 65 74 75 72 6e 20 70 49 6e 43 6c 61 75 73   return pInClaus
20bc0 65 3b 0a 0a 20 20 2f 2a 20 73 6f 6d 65 74 68 69  e;..  /* somethi
20bc1 6e 67 20 77 65 6e 74 20 77 72 6f 6e 67 2e 20 63  ng went wrong. c
20bc2 6c 65 61 6e 20 75 70 20 61 6e 79 74 68 69 6e 67  lean up anything
20bc3 20 61 6c 6c 6f 63 61 74 65 64 2e 20 2a 2f 0a 6c   allocated. */.l
20bc4 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
20bc5 75 70 5f 31 3a 0a 20 20 73 71 6c 69 74 65 33 53  up_1:.  sqlite3S
20bc6 65 6c 65 63 74 44 65 6c 65 74 65 28 70 50 61 72  electDelete(pPar
20bc7 73 65 2d 3e 64 62 2c 20 70 53 65 6c 65 63 74 29  se->db, pSelect)
20bc8 3b 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 0a 6c  ;.  return 0;..l
20bc9 69 6d 69 74 5f 77 68 65 72 65 5f 63 6c 65 61 6e  imit_where_clean
20bca 75 70 5f 32 3a 0a 20 20 73 71 6c 69 74 65 33 45  up_2:.  sqlite3E
20bcb 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73 65  xprDelete(pParse
20bcc 2d 3e 64 62 2c 20 70 57 68 65 72 65 29 3b 0a 20  ->db, pWhere);. 
20bcd 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
20bce 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64  Delete(pParse->d
20bcf 62 2c 20 70 4f 72 64 65 72 42 79 29 3b 0a 20 20  b, pOrderBy);.  
20bd0 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74  sqlite3ExprDelet
20bd1 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 4c  e(pParse->db, pL
20bd2 69 6d 69 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  imit);.  sqlite3
20bd3 45 78 70 72 44 65 6c 65 74 65 28 70 50 61 72 73  ExprDelete(pPars
20bd4 65 2d 3e 64 62 2c 20 70 4f 66 66 73 65 74 29 3b  e->db, pOffset);
20bd5 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23  .  return 0;.}.#
20bd6 65 6e 64 69 66 20 2f 2a 20 64 65 66 69 6e 65 64  endif /* defined
20bd7 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 55  (SQLITE_ENABLE_U
20bd8 50 44 41 54 45 5f 44 45 4c 45 54 45 5f 4c 49 4d  PDATE_DELETE_LIM
20bd9 49 54 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  IT) && !defined(
20bda 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
20bdb 55 45 52 59 29 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  UERY) */../*.** 
20bdc 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
20bdd 72 20 61 20 44 45 4c 45 54 45 20 46 52 4f 4d 20  r a DELETE FROM 
20bde 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a  statement..**.**
20bdf 20 20 20 20 20 44 45 4c 45 54 45 20 46 52 4f 4d       DELETE FROM
20be0 20 74 61 62 6c 65 5f 77 78 79 7a 20 57 48 45 52   table_wxyz WHER
20be1 45 20 61 3c 35 20 41 4e 44 20 62 20 4e 4f 54 20  E a<5 AND b NOT 
20be2 4e 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20  NULL;.**        
20be3 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
20be4 5f 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f  __/       \_____
20be5 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 2a 20  ___________/.** 
20be6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20be7 20 70 54 61 62 4c 69 73 74 20 20 20 20 20 20 20   pTabList       
20be8 20 20 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f         pWhere.*/
20be9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
20bea 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
20beb 74 65 46 72 6f 6d 28 0a 20 20 50 61 72 73 65 20  teFrom(.  Parse 
20bec 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
20bed 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
20bee 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
20bef 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
20bf0 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
20bf1 66 72 6f 6d 20 77 68 69 63 68 20 77 65 20 73 68  from which we sh
20bf2 6f 75 6c 64 20 64 65 6c 65 74 65 20 74 68 69 6e  ould delete thin
20bf3 67 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  gs */.  Expr *pW
20bf4 68 65 72 65 20 20 20 20 20 20 20 20 20 20 20 2f  here           /
20bf5 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
20bf6 73 65 2e 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c  se.  May be null
20bf7 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a 76   */.){.  Vdbe *v
20bf8 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
20bf9 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c 20 64  /* The virtual d
20bfa 61 74 61 62 61 73 65 20 65 6e 67 69 6e 65 20 2a  atabase engine *
20bfb 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b  /.  Table *pTab;
20bfc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
20bfd 65 20 74 61 62 6c 65 20 66 72 6f 6d 20 77 68 69  e table from whi
20bfe 63 68 20 72 65 63 6f 72 64 73 20 77 69 6c 6c 20  ch records will 
20bff 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  be deleted */.  
20c00 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b  const char *zDb;
20c01 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
20c02 66 20 64 61 74 61 62 61 73 65 20 68 6f 6c 64 69  f database holdi
20c03 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e 74  ng pTab */.  int
20c04 20 65 6e 64 2c 20 61 64 64 72 20 3d 20 30 3b 20   end, addr = 0; 
20c05 20 20 20 20 2f 2a 20 41 20 63 6f 75 70 6c 65 20      /* A couple 
20c06 61 64 64 72 65 73 73 65 73 20 6f 66 20 67 65 6e  addresses of gen
20c07 65 72 61 74 65 64 20 63 6f 64 65 20 2a 2f 0a 20  erated code */. 
20c08 20 69 6e 74 20 69 3b 20 20 20 20 20 20 20 20 20   int i;         
20c09 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20          /* Loop 
20c0a 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 57 68 65  counter */.  Whe
20c0b 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20  reInfo *pWInfo; 
20c0c 20 20 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69      /* Informati
20c0d 6f 6e 20 61 62 6f 75 74 20 74 68 65 20 57 48 45  on about the WHE
20c0e 52 45 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 49  RE clause */.  I
20c0f 6e 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20  ndex *pIdx;     
20c10 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f        /* For loo
20c11 70 69 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65  ping over indice
20c12 73 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a  s of the table *
20c13 2f 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20  /.  int iCur;   
20c14 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44             /* VD
20c15 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  BE Cursor number
20c16 20 66 6f 72 20 70 54 61 62 20 2a 2f 0a 20 20 73   for pTab */.  s
20c17 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
20c18 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64 61        /* Main da
20c19 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72 65  tabase structure
20c1a 20 2a 2f 0a 20 20 41 75 74 68 43 6f 6e 74 65 78   */.  AuthContex
20c1b 74 20 73 43 6f 6e 74 65 78 74 3b 20 20 2f 2a 20  t sContext;  /* 
20c1c 41 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f  Authorization co
20c1d 6e 74 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 6f  ntext */.  int o
20c1e 6c 64 49 64 78 20 3d 20 2d 31 3b 20 20 20 20 20  ldIdx = -1;     
20c1f 20 20 2f 2a 20 43 75 72 73 6f 72 20 66 6f 72 20    /* Cursor for 
20c20 74 68 65 20 4f 4c 44 20 74 61 62 6c 65 20 6f 66  the OLD table of
20c21 20 41 46 54 45 52 20 74 72 69 67 67 65 72 73 20   AFTER triggers 
20c22 2a 2f 0a 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74  */.  NameContext
20c23 20 73 4e 43 3b 20 20 20 20 20 20 20 2f 2a 20 4e   sNC;       /* N
20c24 61 6d 65 20 63 6f 6e 74 65 78 74 20 74 6f 20 72  ame context to r
20c25 65 73 6f 6c 76 65 20 65 78 70 72 65 73 73 69 6f  esolve expressio
20c26 6e 73 20 69 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ns in */.  int i
20c27 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
20c28 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 6e 75    /* Database nu
20c29 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20 6d 65  mber */.  int me
20c2a 6d 43 6e 74 20 3d 20 2d 31 3b 20 20 20 20 20 20  mCnt = -1;      
20c2b 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20   /* Memory cell 
20c2c 75 73 65 64 20 66 6f 72 20 63 68 61 6e 67 65 20  used for change 
20c2d 63 6f 75 6e 74 69 6e 67 20 2a 2f 0a 20 20 69 6e  counting */.  in
20c2e 74 20 72 63 61 75 74 68 3b 20 20 20 20 20 20 20  t rcauth;       
20c2f 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 72 65       /* Value re
20c30 74 75 72 6e 65 64 20 62 79 20 61 75 74 68 6f 72  turned by author
20c31 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61 63 6b  ization callback
20c32 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
20c33 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
20c34 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20 20  .  int isView;  
20c35 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
20c36 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d  /* True if attem
20c37 70 74 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20  pting to delete 
20c38 66 72 6f 6d 20 61 20 76 69 65 77 20 2a 2f 0a 20  from a view */. 
20c39 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78   int triggers_ex
20c3a 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  ist = 0;      /*
20c3b 20 54 72 75 65 20 69 66 20 61 6e 79 20 74 72 69   True if any tri
20c3c 67 67 65 72 73 20 65 78 69 73 74 20 2a 2f 0a 23  ggers exist */.#
20c3d 65 6e 64 69 66 0a 20 20 69 6e 74 20 69 42 65 67  endif.  int iBeg
20c3e 69 6e 41 66 74 65 72 54 72 69 67 67 65 72 3b 20  inAfterTrigger; 
20c3f 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
20c40 6f 66 20 61 66 74 65 72 20 74 72 69 67 67 65 72  of after trigger
20c41 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e   program */.  in
20c42 74 20 69 45 6e 64 41 66 74 65 72 54 72 69 67 67  t iEndAfterTrigg
20c43 65 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 45 78  er;        /* Ex
20c44 69 74 20 6f 66 20 61 66 74 65 72 20 74 72 69 67  it of after trig
20c45 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20  ger program */. 
20c46 20 69 6e 74 20 69 42 65 67 69 6e 42 65 66 6f 72   int iBeginBefor
20c47 65 54 72 69 67 67 65 72 3b 20 20 20 20 20 2f 2a  eTrigger;     /*
20c48 20 41 64 64 72 65 73 73 20 6f 66 20 62 65 66 6f   Address of befo
20c49 72 65 20 74 72 69 67 67 65 72 20 70 72 6f 67 72  re trigger progr
20c4a 61 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 45 6e 64  am */.  int iEnd
20c4b 42 65 66 6f 72 65 54 72 69 67 67 65 72 3b 20 20  BeforeTrigger;  
20c4c 20 20 20 20 20 2f 2a 20 45 78 69 74 20 6f 66 20       /* Exit of 
20c4d 62 65 66 6f 72 65 20 74 72 69 67 67 65 72 20 70  before trigger p
20c4e 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 75 33 32 20  rogram */.  u32 
20c4f 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 20 3d 20 30  old_col_mask = 0
20c50 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b  ;        /* Mask
20c51 20 6f 66 20 4f 4c 44 2e 2a 20 63 6f 6c 75 6d 6e   of OLD.* column
20c52 73 20 69 6e 20 75 73 65 20 2a 2f 0a 0a 20 20 73  s in use */..  s
20c53 43 6f 6e 74 65 78 74 2e 70 50 61 72 73 65 20 3d  Context.pParse =
20c54 20 30 3b 0a 20 20 64 62 20 3d 20 70 50 61 72 73   0;.  db = pPars
20c55 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61  e->db;.  if( pPa
20c56 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d  rse->nErr || db-
20c57 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
20c58 0a 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65  .    goto delete
20c59 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20  _from_cleanup;. 
20c5a 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 54 61   }.  assert( pTa
20c5b 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31 20 29  bList->nSrc==1 )
20c5c 3b 0a 0a 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74  ;..  /* Locate t
20c5d 68 65 20 74 61 62 6c 65 20 77 68 69 63 68 20 77  he table which w
20c5e 65 20 77 61 6e 74 20 74 6f 20 64 65 6c 65 74 65  e want to delete
20c5f 2e 20 20 54 68 69 73 20 74 61 62 6c 65 20 68 61  .  This table ha
20c60 73 20 74 6f 20 62 65 0a 20 20 2a 2a 20 70 75 74  s to be.  ** put
20c61 20 69 6e 20 61 6e 20 53 72 63 4c 69 73 74 20 73   in an SrcList s
20c62 74 72 75 63 74 75 72 65 20 62 65 63 61 75 73 65  tructure because
20c63 20 73 6f 6d 65 20 6f 66 20 74 68 65 20 73 75 62   some of the sub
20c64 72 6f 75 74 69 6e 65 73 20 77 65 0a 20 20 2a 2a  routines we.  **
20c65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c 69 6e 67   will be calling
20c66 20 61 72 65 20 64 65 73 69 67 6e 65 64 20 74 6f   are designed to
20c67 20 77 6f 72 6b 20 77 69 74 68 20 6d 75 6c 74 69   work with multi
20c68 70 6c 65 20 74 61 62 6c 65 73 20 61 6e 64 20 65  ple tables and e
20c69 78 70 65 63 74 0a 20 20 2a 2a 20 61 6e 20 53 72  xpect.  ** an Sr
20c6a 63 4c 69 73 74 2a 20 70 61 72 61 6d 65 74 65 72  cList* parameter
20c6b 20 69 6e 73 74 65 61 64 20 6f 66 20 6a 75 73 74   instead of just
20c6c 20 61 20 54 61 62 6c 65 2a 20 70 61 72 61 6d 65   a Table* parame
20c6d 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 70 54 61 62  ter..  */.  pTab
20c6e 20 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73   = sqlite3SrcLis
20c6f 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  tLookup(pParse, 
20c70 70 54 61 62 4c 69 73 74 29 3b 0a 20 20 69 66 28  pTabList);.  if(
20c71 20 70 54 61 62 3d 3d 30 20 29 20 20 67 6f 74 6f   pTab==0 )  goto
20c72 20 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65   delete_from_cle
20c73 61 6e 75 70 3b 0a 0a 20 20 2f 2a 20 46 69 67 75  anup;..  /* Figu
20c74 72 65 20 6f 75 74 20 69 66 20 77 65 20 68 61 76  re out if we hav
20c75 65 20 61 6e 79 20 74 72 69 67 67 65 72 73 20 61  e any triggers a
20c76 6e 64 20 69 66 20 74 68 65 20 74 61 62 6c 65 20  nd if the table 
20c77 62 65 69 6e 67 0a 20 20 2a 2a 20 64 65 6c 65 74  being.  ** delet
20c78 65 64 20 66 72 6f 6d 20 69 73 20 61 20 76 69 65  ed from is a vie
20c79 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53  w.  */.#ifndef S
20c7a 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47  QLITE_OMIT_TRIGG
20c7b 45 52 0a 20 20 74 72 69 67 67 65 72 73 5f 65 78  ER.  triggers_ex
20c7c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69  ist = sqlite3Tri
20c7d 67 67 65 72 73 45 78 69 73 74 28 70 54 61 62 2c  ggersExist(pTab,
20c7e 20 54 4b 5f 44 45 4c 45 54 45 2c 20 30 29 3b 0a   TK_DELETE, 0);.
20c7f 20 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d    isView = pTab-
20c80 3e 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c  >pSelect!=0;.#el
20c81 73 65 0a 23 20 64 65 66 69 6e 65 20 74 72 69 67  se.# define trig
20c82 67 65 72 73 5f 65 78 69 73 74 20 30 0a 23 20 64  gers_exist 0.# d
20c83 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23  efine isView 0.#
20c84 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c  endif.#ifdef SQL
20c85 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20  ITE_OMIT_VIEW.# 
20c86 75 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20 64  undef isView.# d
20c87 65 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23  efine isView 0.#
20c88 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71 6c  endif..  if( sql
20c89 69 74 65 33 49 73 52 65 61 64 4f 6e 6c 79 28 70  ite3IsReadOnly(p
20c8a 50 61 72 73 65 2c 20 70 54 61 62 2c 20 74 72 69  Parse, pTab, tri
20c8b 67 67 65 72 73 5f 65 78 69 73 74 29 20 29 7b 0a  ggers_exist) ){.
20c8c 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
20c8d 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
20c8e 7d 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65  }.  iDb = sqlite
20c8f 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64  3SchemaToIndex(d
20c90 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61  b, pTab->pSchema
20c91 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44 62  );.  assert( iDb
20c92 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20 7a 44  <db->nDb );.  zD
20c93 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  b = db->aDb[iDb]
20c94 2e 7a 4e 61 6d 65 3b 0a 20 20 72 63 61 75 74 68  .zName;.  rcauth
20c95 20 3d 20 73 71 6c 69 74 65 33 41 75 74 68 43 68   = sqlite3AuthCh
20c96 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49  eck(pParse, SQLI
20c97 54 45 5f 44 45 4c 45 54 45 2c 20 70 54 61 62 2d  TE_DELETE, pTab-
20c98 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a 44 62 29 3b  >zName, 0, zDb);
20c99 0a 20 20 61 73 73 65 72 74 28 20 72 63 61 75 74  .  assert( rcaut
20c9a 68 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  h==SQLITE_OK || 
20c9b 72 63 61 75 74 68 3d 3d 53 51 4c 49 54 45 5f 44  rcauth==SQLITE_D
20c9c 45 4e 59 20 7c 7c 20 72 63 61 75 74 68 3d 3d 53  ENY || rcauth==S
20c9d 51 4c 49 54 45 5f 49 47 4e 4f 52 45 20 29 3b 0a  QLITE_IGNORE );.
20c9e 20 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51    if( rcauth==SQ
20c9f 4c 49 54 45 5f 44 45 4e 59 20 29 7b 0a 20 20 20  LITE_DENY ){.   
20ca0 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
20ca1 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20  m_cleanup;.  }. 
20ca2 20 61 73 73 65 72 74 28 21 69 73 56 69 65 77 20   assert(!isView 
20ca3 7c 7c 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  || triggers_exis
20ca4 74 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 70 54 61  t);..  /* If pTa
20ca5 62 20 69 73 20 72 65 61 6c 6c 79 20 61 20 76 69  b is really a vi
20ca6 65 77 2c 20 6d 61 6b 65 20 73 75 72 65 20 69 74  ew, make sure it
20ca7 20 68 61 73 20 62 65 65 6e 20 69 6e 69 74 69 61   has been initia
20ca8 6c 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66  lized..  */.  if
20ca9 28 20 73 71 6c 69 74 65 33 56 69 65 77 47 65 74  ( sqlite3ViewGet
20caa 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61 72  ColumnNames(pPar
20cab 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20 20 20  se, pTab) ){.   
20cac 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f   goto delete_fro
20cad 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  m_cleanup;.  }..
20cae 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20    /* Allocate a 
20caf 63 75 72 73 6f 72 20 75 73 65 64 20 74 6f 20 73  cursor used to s
20cb0 74 6f 72 65 20 74 68 65 20 6f 6c 64 2e 2a 20 64  tore the old.* d
20cb1 61 74 61 20 66 6f 72 20 61 20 74 72 69 67 67 65  ata for a trigge
20cb2 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72  r..  */.  if( tr
20cb3 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 20  iggers_exist ){ 
20cb4 0a 20 20 20 20 6f 6c 64 49 64 78 20 3d 20 70 50  .    oldIdx = pP
20cb5 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20  arse->nTab++;.  
20cb6 7d 0a 0a 20 20 2f 2a 20 41 73 73 69 67 6e 20 20  }..  /* Assign  
20cb7 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 74 6f  cursor number to
20cb8 20 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 61   the table and a
20cb9 6c 6c 20 69 74 73 20 69 6e 64 69 63 65 73 2e 0a  ll its indices..
20cba 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
20cbb 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3d 3d 31  TabList->nSrc==1
20cbc 20 29 3b 0a 20 20 69 43 75 72 20 3d 20 70 54 61   );.  iCur = pTa
20cbd 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e 69 43 75 72  bList->a[0].iCur
20cbe 73 6f 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  sor = pParse->nT
20cbf 61 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78  ab++;.  for(pIdx
20cc0 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
20cc1 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
20cc2 70 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72  pNext){.    pPar
20cc3 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a  se->nTab++;.  }.
20cc4 0a 20 20 2f 2a 20 53 74 61 72 74 20 74 68 65 20  .  /* Start the 
20cc5 76 69 65 77 20 63 6f 6e 74 65 78 74 0a 20 20 2a  view context.  *
20cc6 2f 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29  /.  if( isView )
20cc7 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 41 75 74  {.    sqlite3Aut
20cc8 68 43 6f 6e 74 65 78 74 50 75 73 68 28 70 50 61  hContextPush(pPa
20cc9 72 73 65 2c 20 26 73 43 6f 6e 74 65 78 74 2c 20  rse, &sContext, 
20cca 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20  pTab->zName);.  
20ccb 7d 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67 65  }..  /* Begin ge
20ccc 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a 20  nerating code.. 
20ccd 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74 65   */.  v = sqlite
20cce 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
20ccf 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b 0a  ;.  if( v==0 ){.
20cd0 20 20 20 20 67 6f 74 6f 20 64 65 6c 65 74 65 5f      goto delete_
20cd1 66 72 6f 6d 5f 63 6c 65 61 6e 75 70 3b 0a 20 20  from_cleanup;.  
20cd2 7d 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e  }.  if( pParse->
20cd3 6e 65 73 74 65 64 3d 3d 30 20 29 20 73 71 6c 69  nested==0 ) sqli
20cd4 74 65 33 56 64 62 65 43 6f 75 6e 74 43 68 61 6e  te3VdbeCountChan
20cd5 67 65 73 28 76 29 3b 0a 20 20 73 71 6c 69 74 65  ges(v);.  sqlite
20cd6 33 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61  3BeginWriteOpera
20cd7 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 74 72 69  tion(pParse, tri
20cd8 67 67 65 72 73 5f 65 78 69 73 74 2c 20 69 44 62  ggers_exist, iDb
20cd9 29 3b 0a 0a 20 20 69 66 28 20 74 72 69 67 67 65  );..  if( trigge
20cda 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20  rs_exist ){.    
20cdb 69 6e 74 20 6f 72 63 6f 6e 66 20 3d 20 28 28 70  int orconf = ((p
20cdc 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
20cdd 29 3f 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74  )?pParse->trigSt
20cde 61 63 6b 2d 3e 6f 72 63 6f 6e 66 3a 4f 45 5f 44  ack->orconf:OE_D
20cdf 65 66 61 75 6c 74 29 3b 0a 20 20 20 20 69 6e 74  efault);.    int
20ce0 20 69 47 6f 74 6f 20 3d 20 73 71 6c 69 74 65 33   iGoto = sqlite3
20ce1 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20 4f 50  VdbeAddOp0(v, OP
20ce2 5f 47 6f 74 6f 29 3b 0a 20 20 20 20 61 64 64 72  _Goto);.    addr
20ce3 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
20ce4 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20  keLabel(v);..   
20ce5 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69   iBeginBeforeTri
20ce6 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64  gger = sqlite3Vd
20ce7 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
20ce8 3b 0a 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69  ;.    (void)sqli
20ce9 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67 65  te3CodeRowTrigge
20cea 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44 45 4c  r(pParse, TK_DEL
20ceb 45 54 45 2c 20 30 2c 20 54 52 49 47 47 45 52 5f  ETE, 0, TRIGGER_
20cec 42 45 46 4f 52 45 2c 20 70 54 61 62 2c 0a 20 20  BEFORE, pTab,.  
20ced 20 20 20 20 20 20 2d 31 2c 20 6f 6c 64 49 64 78        -1, oldIdx
20cee 2c 20 6f 72 63 6f 6e 66 2c 20 61 64 64 72 2c 20  , orconf, addr, 
20cef 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 30  &old_col_mask, 0
20cf0 29 3b 0a 20 20 20 20 69 45 6e 64 42 65 66 6f 72  );.    iEndBefor
20cf1 65 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  eTrigger = sqlit
20cf2 65 33 56 64 62 65 41 64 64 4f 70 30 28 76 2c 20  e3VdbeAddOp0(v, 
20cf3 4f 50 5f 47 6f 74 6f 29 3b 0a 0a 20 20 20 20 69  OP_Goto);..    i
20cf4 42 65 67 69 6e 41 66 74 65 72 54 72 69 67 67 65  BeginAfterTrigge
20cf5 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  r = sqlite3VdbeC
20cf6 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
20cf7 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33     (void)sqlite3
20cf8 43 6f 64 65 52 6f 77 54 72 69 67 67 65 72 28 70  CodeRowTrigger(p
20cf9 50 61 72 73 65 2c 20 54 4b 5f 44 45 4c 45 54 45  Parse, TK_DELETE
20cfa 2c 20 30 2c 20 54 52 49 47 47 45 52 5f 41 46 54  , 0, TRIGGER_AFT
20cfb 45 52 2c 20 70 54 61 62 2c 20 2d 31 2c 0a 20 20  ER, pTab, -1,.  
20cfc 20 20 20 20 20 20 6f 6c 64 49 64 78 2c 20 6f 72        oldIdx, or
20cfd 63 6f 6e 66 2c 20 61 64 64 72 2c 20 26 6f 6c 64  conf, addr, &old
20cfe 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 30 29 3b 0a 20  _col_mask, 0);. 
20cff 20 20 20 69 45 6e 64 41 66 74 65 72 54 72 69 67     iEndAfterTrig
20d00 67 65 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ger = sqlite3Vdb
20d01 65 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f  eAddOp0(v, OP_Go
20d02 74 6f 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65  to);..    sqlite
20d03 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
20d04 20 69 47 6f 74 6f 29 3b 0a 20 20 7d 0a 0a 20 20   iGoto);.  }..  
20d05 2f 2a 20 49 66 20 77 65 20 61 72 65 20 74 72 79  /* If we are try
20d06 69 6e 67 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ing to delete fr
20d07 6f 6d 20 61 20 76 69 65 77 2c 20 72 65 61 6c 69  om a view, reali
20d08 7a 65 20 74 68 61 74 20 76 69 65 77 20 69 6e 74  ze that view int
20d09 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65 6d 65 72  o.  ** a ephemer
20d0a 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 23  al table..  */.#
20d0b 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
20d0c 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20 26 26  TE_OMIT_VIEW) &&
20d0d 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
20d0e 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 0a 20  _OMIT_TRIGGER). 
20d0f 20 69 66 28 20 69 73 56 69 65 77 20 29 7b 0a 20   if( isView ){. 
20d10 20 20 20 73 71 6c 69 74 65 33 4d 61 74 65 72 69     sqlite3Materi
20d11 61 6c 69 7a 65 56 69 65 77 28 70 50 61 72 73 65  alizeView(pParse
20d12 2c 20 70 54 61 62 2c 20 70 57 68 65 72 65 2c 20  , pTab, pWhere, 
20d13 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69  iCur);.  }.#endi
20d14 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c 76 65 20  f..  /* Resolve 
20d15 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73  the column names
20d16 20 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   in the WHERE cl
20d17 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d  ause..  */.  mem
20d18 73 65 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a  set(&sNC, 0, siz
20d19 65 6f 66 28 73 4e 43 29 29 3b 0a 20 20 73 4e 43  eof(sNC));.  sNC
20d1a 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
20d1b 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74  ;.  sNC.pSrcList
20d1c 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20 20 69   = pTabList;.  i
20d1d 66 28 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  f( sqlite3Resolv
20d1e 65 45 78 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c  eExprNames(&sNC,
20d1f 20 70 57 68 65 72 65 29 20 29 7b 0a 20 20 20 20   pWhere) ){.    
20d20 67 6f 74 6f 20 64 65 6c 65 74 65 5f 66 72 6f 6d  goto delete_from
20d21 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
20d22 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
20d23 68 65 20 63 6f 75 6e 74 65 72 20 6f 66 20 74 68  he counter of th
20d24 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73  e number of rows
20d25 20 64 65 6c 65 74 65 64 2c 20 69 66 0a 20 20 2a   deleted, if.  *
20d26 2a 20 77 65 20 61 72 65 20 63 6f 75 6e 74 69 6e  * we are countin
20d27 67 20 72 6f 77 73 2e 0a 20 20 2a 2f 0a 20 20 69  g rows..  */.  i
20d28 66 28 20 64 62 2d 3e 66 6c 61 67 73 20 26 20 53  f( db->flags & S
20d29 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f 77 73 20  QLITE_CountRows 
20d2a 29 7b 0a 20 20 20 20 6d 65 6d 43 6e 74 20 3d 20  ){.    memCnt = 
20d2b 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a  ++pParse->nMem;.
20d2c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20d2d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
20d2e 67 65 72 2c 20 30 2c 20 6d 65 6d 43 6e 74 29 3b  ger, 0, memCnt);
20d2f 0a 20 20 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  .  }..#ifndef SQ
20d30 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41  LITE_OMIT_TRUNCA
20d31 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a  TE_OPTIMIZATION.
20d32 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
20d33 65 3a 20 41 20 44 45 4c 45 54 45 20 77 69 74 68  e: A DELETE with
20d34 6f 75 74 20 61 20 57 48 45 52 45 20 63 6c 61 75  out a WHERE clau
20d35 73 65 20 64 65 6c 65 74 65 73 20 65 76 65 72 79  se deletes every
20d36 74 68 69 6e 67 2e 0a 20 20 2a 2a 20 49 74 20 69  thing..  ** It i
20d37 73 20 65 61 73 69 65 72 20 6a 75 73 74 20 74 6f  s easier just to
20d38 20 65 72 61 73 65 20 74 68 65 20 77 68 6f 6c 65   erase the whole
20d39 20 74 61 62 6c 65 2e 20 20 4e 6f 74 65 2c 20 68   table.  Note, h
20d3a 6f 77 65 76 65 72 2c 20 74 68 61 74 0a 20 20 2a  owever, that.  *
20d3b 2a 20 74 68 69 73 20 6d 65 61 6e 73 20 74 68 61  * this means tha
20d3c 74 20 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65  t the row change
20d3d 20 63 6f 75 6e 74 20 77 69 6c 6c 20 62 65 20 69   count will be i
20d3e 6e 63 6f 72 72 65 63 74 2e 0a 20 20 2a 2f 0a 20  ncorrect..  */. 
20d3f 20 69 66 28 20 72 63 61 75 74 68 3d 3d 53 51 4c   if( rcauth==SQL
20d40 49 54 45 5f 4f 4b 20 26 26 20 70 57 68 65 72 65  ITE_OK && pWhere
20d41 3d 3d 30 20 26 26 20 21 74 72 69 67 67 65 72 73  ==0 && !triggers
20d42 5f 65 78 69 73 74 20 26 26 20 21 49 73 56 69 72  _exist && !IsVir
20d43 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
20d44 20 20 61 73 73 65 72 74 28 20 21 69 73 56 69 65    assert( !isVie
20d45 77 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  w );.    sqlite3
20d46 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
20d47 5f 43 6c 65 61 72 2c 20 70 54 61 62 2d 3e 74 6e  _Clear, pTab->tn
20d48 75 6d 2c 20 69 44 62 2c 20 6d 65 6d 43 6e 74 29  um, iDb, memCnt)
20d49 3b 0a 20 20 20 20 69 66 28 20 21 70 50 61 72 73  ;.    if( !pPars
20d4a 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a 20 20 20  e->nested ){.   
20d4b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
20d4c 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70 54  angeP4(v, -1, pT
20d4d 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53 54  ab->zName, P4_ST
20d4e 41 54 49 43 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ATIC);.    }.   
20d4f 20 66 6f 72 28 70 49 64 78 3d 70 54 61 62 2d 3e   for(pIdx=pTab->
20d50 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49  pIndex; pIdx; pI
20d51 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
20d52 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
20d53 49 64 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70 54  Idx->pSchema==pT
20d54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a 20  ab->pSchema );. 
20d55 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20d56 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 65  AddOp2(v, OP_Cle
20d57 61 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20  ar, pIdx->tnum, 
20d58 69 44 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65  iDb);.    }.  }e
20d59 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  lse.#endif /* SQ
20d5a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 55 4e 43 41  LITE_OMIT_TRUNCA
20d5b 54 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  TE_OPTIMIZATION 
20d5c 2a 2f 0a 20 20 2f 2a 20 54 68 65 20 75 73 75 61  */.  /* The usua
20d5d 6c 20 63 61 73 65 3a 20 54 68 65 72 65 20 69 73  l case: There is
20d5e 20 61 20 57 48 45 52 45 20 63 6c 61 75 73 65 20   a WHERE clause 
20d5f 73 6f 20 77 65 20 68 61 76 65 20 74 6f 20 73 63  so we have to sc
20d60 61 6e 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20  an through.  ** 
20d61 74 68 65 20 74 61 62 6c 65 20 61 6e 64 20 70 69  the table and pi
20d62 63 6b 20 77 68 69 63 68 20 72 65 63 6f 72 64 73  ck which records
20d63 20 74 6f 20 64 65 6c 65 74 65 2e 0a 20 20 2a 2f   to delete..  */
20d64 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 69 52 6f  .  {.    int iRo
20d65 77 69 64 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  wid = ++pParse->
20d66 6e 4d 65 6d 3b 20 20 20 20 2f 2a 20 55 73 65 64  nMem;    /* Used
20d67 20 66 6f 72 20 73 74 6f 72 69 6e 67 20 72 6f 77   for storing row
20d68 69 64 20 76 61 6c 75 65 73 2e 20 2a 2f 0a 0a 20  id values. */.. 
20d69 20 20 20 2f 2a 20 42 65 67 69 6e 20 74 68 65 20     /* Begin the 
20d6a 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a 20 20  database scan.  
20d6b 20 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66 6f 20    */.    pWInfo 
20d6c 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65 42 65  = sqlite3WhereBe
20d6d 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62  gin(pParse, pTab
20d6e 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20 30 2c  List, pWhere, 0,
20d6f 20 30 29 3b 0a 20 20 20 20 69 66 28 20 70 57 49   0);.    if( pWI
20d70 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20 64 65  nfo==0 ) goto de
20d71 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61 6e 75  lete_from_cleanu
20d72 70 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 6d 65 6d  p;..    /* Remem
20d73 62 65 72 20 74 68 65 20 72 6f 77 69 64 20 6f 66  ber the rowid of
20d74 20 65 76 65 72 79 20 69 74 65 6d 20 74 6f 20 62   every item to b
20d75 65 20 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a  e deleted..    *
20d76 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  /.    sqlite3Vdb
20d77 65 41 64 64 4f 70 32 28 76 2c 20 49 73 56 69 72  eAddOp2(v, IsVir
20d78 74 75 61 6c 28 70 54 61 62 29 20 3f 20 4f 50 5f  tual(pTab) ? OP_
20d79 56 52 6f 77 69 64 20 3a 20 4f 50 5f 52 6f 77 69  VRowid : OP_Rowi
20d7a 64 2c 20 69 43 75 72 2c 20 69 52 6f 77 69 64 29  d, iCur, iRowid)
20d7b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
20d7c 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 46 69  eAddOp1(v, OP_Fi
20d7d 66 6f 57 72 69 74 65 2c 20 69 52 6f 77 69 64 29  foWrite, iRowid)
20d7e 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 66 6c  ;.    if( db->fl
20d7f 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
20d80 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 20 20  ntRows ){.      
20d81 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
20d82 32 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20  2(v, OP_AddImm, 
20d83 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20  memCnt, 1);.    
20d84 7d 0a 0a 20 20 20 20 2f 2a 20 45 6e 64 20 74 68  }..    /* End th
20d85 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 20  e database scan 
20d86 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  loop..    */.   
20d87 20 73 71 6c 69 74 65 33 57 68 65 72 65 45 6e 64   sqlite3WhereEnd
20d88 28 70 57 49 6e 66 6f 29 3b 0a 0a 20 20 20 20 2f  (pWInfo);..    /
20d89 2a 20 4f 70 65 6e 20 74 68 65 20 70 73 65 75 64  * Open the pseud
20d8a 6f 2d 74 61 62 6c 65 20 75 73 65 64 20 74 6f 20  o-table used to 
20d8b 73 74 6f 72 65 20 4f 4c 44 20 69 66 20 74 68 65  store OLD if the
20d8c 72 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2e  re are triggers.
20d8d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
20d8e 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20 29  triggers_exist )
20d8f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
20d90 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
20d91 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30  SetNumColumns, 0
20d92 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20  , pTab->nCol);. 
20d93 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
20d94 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4f 70 65  AddOp1(v, OP_Ope
20d95 6e 50 73 65 75 64 6f 2c 20 6f 6c 64 49 64 78 29  nPseudo, oldIdx)
20d96 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20  ;.    }..    /* 
20d97 44 65 6c 65 74 65 20 65 76 65 72 79 20 69 74 65  Delete every ite
20d98 6d 20 77 68 6f 73 65 20 6b 65 79 20 77 61 73 20  m whose key was 
20d99 77 72 69 74 74 65 6e 20 74 6f 20 74 68 65 20 6c  written to the l
20d9a 69 73 74 20 64 75 72 69 6e 67 20 74 68 65 0a 20  ist during the. 
20d9b 20 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73     ** database s
20d9c 63 61 6e 2e 20 20 57 65 20 68 61 76 65 20 74 6f  can.  We have to
20d9d 20 64 65 6c 65 74 65 20 69 74 65 6d 73 20 61 66   delete items af
20d9e 74 65 72 20 74 68 65 20 73 63 61 6e 20 69 73 20  ter the scan is 
20d9f 63 6f 6d 70 6c 65 74 65 0a 20 20 20 20 2a 2a 20  complete.    ** 
20da0 62 65 63 61 75 73 65 20 64 65 6c 65 74 69 6e 67  because deleting
20da1 20 61 6e 20 69 74 65 6d 20 63 61 6e 20 63 68 61   an item can cha
20da2 6e 67 65 20 74 68 65 20 73 63 61 6e 20 6f 72 64  nge the scan ord
20da3 65 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 65  er..    */.    e
20da4 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  nd = sqlite3Vdbe
20da5 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20  MakeLabel(v);.. 
20da6 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29     if( !isView )
20da7 7b 0a 20 20 20 20 20 20 2f 2a 20 4f 70 65 6e 20  {.      /* Open 
20da8 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68 65 20  cursors for the 
20da9 74 61 62 6c 65 20 77 65 20 61 72 65 20 64 65 6c  table we are del
20daa 65 74 69 6e 67 20 66 72 6f 6d 20 61 6e 64 20 0a  eting from and .
20dab 20 20 20 20 20 20 2a 2a 20 61 6c 6c 20 69 74 73        ** all its
20dac 20 69 6e 64 69 63 65 73 2e 0a 20 20 20 20 20 20   indices..      
20dad 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
20dae 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69  OpenTableAndIndi
20daf 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61 62  ces(pParse, pTab
20db0 2c 20 69 43 75 72 2c 20 4f 50 5f 4f 70 65 6e 57  , iCur, OP_OpenW
20db1 72 69 74 65 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  rite);.    }..  
20db2 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65    /* This is the
20db3 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
20db4 65 20 64 65 6c 65 74 65 20 6c 6f 6f 70 2e 20 49  e delete loop. I
20db5 66 20 61 20 74 72 69 67 67 65 72 20 65 6e 63 6f  f a trigger enco
20db6 75 6e 74 65 72 73 0a 20 20 20 20 2a 2a 20 61 6e  unters.    ** an
20db7 20 49 47 4e 4f 52 45 20 63 6f 6e 73 74 72 61 69   IGNORE constrai
20db8 6e 74 2c 20 69 74 20 6a 75 6d 70 73 20 62 61 63  nt, it jumps bac
20db9 6b 20 74 6f 20 68 65 72 65 2e 0a 20 20 20 20 2a  k to here..    *
20dba 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67 67 65  /.    if( trigge
20dbb 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20  rs_exist ){.    
20dbc 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
20dbd 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
20dbe 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 64  r);.    }.    ad
20dbf 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
20dc0 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 69 66  AddOp2(v, OP_Fif
20dc1 6f 52 65 61 64 2c 20 69 52 6f 77 69 64 2c 20 65  oRead, iRowid, e
20dc2 6e 64 29 3b 0a 0a 20 20 20 20 69 66 28 20 74 72  nd);..    if( tr
20dc3 69 67 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a  iggers_exist ){.
20dc4 20 20 20 20 20 20 69 6e 74 20 69 44 61 74 61 20        int iData 
20dc5 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
20dc6 3b 20 20 20 2f 2a 20 46 6f 72 20 73 74 6f 72 69  ;   /* For stori
20dc7 6e 67 20 72 6f 77 20 64 61 74 61 20 6f 66 20 4f  ng row data of O
20dc8 4c 44 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 20  LD table */..   
20dc9 20 20 20 2f 2a 20 49 66 20 74 68 65 20 72 65 63     /* If the rec
20dca 6f 72 64 20 69 73 20 6e 6f 20 6c 6f 6e 67 65 72  ord is no longer
20dcb 20 70 72 65 73 65 6e 74 20 69 6e 20 74 68 65 20   present in the 
20dcc 74 61 62 6c 65 2c 20 6a 75 6d 70 20 74 6f 20 74  table, jump to t
20dcd 68 65 0a 20 20 20 20 20 20 2a 2a 20 6e 65 78 74  he.      ** next
20dce 20 69 74 65 72 61 74 69 6f 6e 20 6f 66 20 74 68   iteration of th
20dcf 65 20 6c 6f 6f 70 20 74 68 72 6f 75 67 68 20 74  e loop through t
20dd0 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74  he contents of t
20dd1 68 65 20 66 69 66 6f 2e 0a 20 20 20 20 20 20 2a  he fifo..      *
20dd2 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  /.      sqlite3V
20dd3 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
20dd4 4e 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c  NotExists, iCur,
20dd5 20 61 64 64 72 2c 20 69 52 6f 77 69 64 29 3b 0a   addr, iRowid);.
20dd6 0a 20 20 20 20 20 20 2f 2a 20 50 6f 70 75 6c 61  .      /* Popula
20dd7 74 65 20 74 68 65 20 4f 4c 44 2e 2a 20 70 73 65  te the OLD.* pse
20dd8 75 64 6f 2d 74 61 62 6c 65 20 2a 2f 0a 20 20 20  udo-table */.   
20dd9 20 20 20 69 66 28 20 6f 6c 64 5f 63 6f 6c 5f 6d     if( old_col_m
20dda 61 73 6b 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ask ){.        s
20ddb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
20ddc 28 76 2c 20 4f 50 5f 52 6f 77 44 61 74 61 2c 20  (v, OP_RowData, 
20ddd 69 43 75 72 2c 20 69 44 61 74 61 29 3b 0a 20 20  iCur, iData);.  
20dde 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
20ddf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20de0 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
20de1 20 30 2c 20 69 44 61 74 61 29 3b 0a 20 20 20 20   0, iData);.    
20de2 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
20de3 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
20de4 50 5f 49 6e 73 65 72 74 2c 20 6f 6c 64 49 64 78  P_Insert, oldIdx
20de5 2c 20 69 44 61 74 61 2c 20 69 52 6f 77 69 64 29  , iData, iRowid)
20de6 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70  ;..      /* Jump
20de7 20 62 61 63 6b 20 61 6e 64 20 72 75 6e 20 74 68   back and run th
20de8 65 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  e BEFORE trigger
20de9 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74  s */.      sqlit
20dea 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
20deb 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 42 65 67  OP_Goto, 0, iBeg
20dec 69 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 29  inBeforeTrigger)
20ded 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
20dee 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 69  dbeJumpHere(v, i
20def 45 6e 64 42 65 66 6f 72 65 54 72 69 67 67 65 72  EndBeforeTrigger
20df0 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
20df1 28 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  ( !isView ){.   
20df2 20 20 20 2f 2a 20 44 65 6c 65 74 65 20 74 68 65     /* Delete the
20df3 20 72 6f 77 20 2a 2f 0a 23 69 66 6e 64 65 66 20   row */.#ifndef 
20df4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
20df5 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 20 20 69  UALTABLE.      i
20df6 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
20df7 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 6f  b) ){.        co
20df8 6e 73 74 20 63 68 61 72 20 2a 70 56 74 61 62 20  nst char *pVtab 
20df9 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
20dfa 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20 20 20  pTab->pVtab;.   
20dfb 20 20 20 20 20 73 71 6c 69 74 65 33 56 74 61 62       sqlite3Vtab
20dfc 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 70 50 61  MakeWritable(pPa
20dfd 72 73 65 2c 20 70 54 61 62 29 3b 0a 20 20 20 20  rse, pTab);.    
20dfe 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
20dff 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56 55 70 64  ddOp4(v, OP_VUpd
20e00 61 74 65 2c 20 30 2c 20 31 2c 20 69 52 6f 77 69  ate, 0, 1, iRowi
20e01 64 2c 20 70 56 74 61 62 2c 20 50 34 5f 56 54 41  d, pVtab, P4_VTA
20e02 42 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 0a  B);.      }else.
20e03 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a 20  #endif.      {. 
20e04 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 65         sqlite3Ge
20e05 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28  nerateRowDelete(
20e06 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20 69 43  pParse, pTab, iC
20e07 75 72 2c 20 69 52 6f 77 69 64 2c 20 70 50 61 72  ur, iRowid, pPar
20e08 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 29 3b 0a  se->nested==0);.
20e09 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
20e0a 20 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61     /* If there a
20e0b 72 65 20 72 6f 77 20 74 72 69 67 67 65 72 73 2c  re row triggers,
20e0c 20 63 6c 6f 73 65 20 61 6c 6c 20 63 75 72 73 6f   close all curso
20e0d 72 73 20 74 68 65 6e 20 69 6e 76 6f 6b 65 0a 20  rs then invoke. 
20e0e 20 20 20 2a 2a 20 74 68 65 20 41 46 54 45 52 20     ** the AFTER 
20e0f 74 72 69 67 67 65 72 73 0a 20 20 20 20 2a 2f 0a  triggers.    */.
20e10 20 20 20 20 69 66 28 20 74 72 69 67 67 65 72 73      if( triggers
20e11 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 20 20  _exist ){.      
20e12 2f 2a 20 4a 75 6d 70 20 62 61 63 6b 20 61 6e 64  /* Jump back and
20e13 20 72 75 6e 20 74 68 65 20 41 46 54 45 52 20 74   run the AFTER t
20e14 72 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 20  riggers */.     
20e15 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20e16 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
20e17 2c 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69  , iBeginAfterTri
20e18 67 67 65 72 29 3b 0a 20 20 20 20 20 20 73 71 6c  gger);.      sql
20e19 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
20e1a 28 76 2c 20 69 45 6e 64 41 66 74 65 72 54 72 69  (v, iEndAfterTri
20e1b 67 67 65 72 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  gger);.    }..  
20e1c 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20    /* End of the 
20e1d 64 65 6c 65 74 65 20 6c 6f 6f 70 20 2a 2f 0a 20  delete loop */. 
20e1e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
20e1f 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
20e20 20 30 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73   0, addr);.    s
20e21 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76  qlite3VdbeResolv
20e22 65 4c 61 62 65 6c 28 76 2c 20 65 6e 64 29 3b 0a  eLabel(v, end);.
20e23 0a 20 20 20 20 2f 2a 20 43 6c 6f 73 65 20 74 68  .    /* Close th
20e24 65 20 63 75 72 73 6f 72 73 20 61 66 74 65 72 20  e cursors after 
20e25 74 68 65 20 6c 6f 6f 70 20 69 66 20 74 68 65 72  the loop if ther
20e26 65 20 61 72 65 20 6e 6f 20 72 6f 77 20 74 72 69  e are no row tri
20e27 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 69 66 28  ggers */.    if(
20e28 20 21 69 73 56 69 65 77 20 20 26 26 20 21 49 73   !isView  && !Is
20e29 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b  Virtual(pTab) ){
20e2a 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 2c 20  .      for(i=1, 
20e2b 70 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65  pIdx=pTab->pInde
20e2c 78 3b 20 70 49 64 78 3b 20 69 2b 2b 2c 20 70 49  x; pIdx; i++, pI
20e2d 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 29 7b  dx=pIdx->pNext){
20e2e 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
20e2f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
20e30 5f 43 6c 6f 73 65 2c 20 69 43 75 72 20 2b 20 69  _Close, iCur + i
20e31 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 29 3b 0a 20  , pIdx->tnum);. 
20e32 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c       }.      sql
20e33 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
20e34 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69 43 75 72  , OP_Close, iCur
20e35 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
20e36 2f 2a 0a 20 20 2a 2a 20 52 65 74 75 72 6e 20 74  /*.  ** Return t
20e37 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77  he number of row
20e38 73 20 74 68 61 74 20 77 65 72 65 20 64 65 6c 65  s that were dele
20e39 74 65 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75  ted. If this rou
20e3a 74 69 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65  tine is .  ** ge
20e3b 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65  nerating code be
20e3c 63 61 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20  cause of a call 
20e3d 74 6f 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64  to sqlite3Nested
20e3e 50 61 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a  Parse(), do not.
20e3f 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20    ** invoke the 
20e40 63 61 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f  callback functio
20e41 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62  n..  */.  if( db
20e42 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45  ->flags & SQLITE
20e43 5f 43 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50  _CountRows && pP
20e44 61 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20  arse->nested==0 
20e45 26 26 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67  && !pParse->trig
20e46 53 74 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c  Stack ){.    sql
20e47 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
20e48 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
20e49 6d 65 6d 43 6e 74 2c 20 31 29 3b 0a 20 20 20 20  memCnt, 1);.    
20e4a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
20e4b 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20  mCols(v, 1);.   
20e4c 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
20e4d 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
20e4e 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73  NAME_NAME, "rows
20e4f 20 64 65 6c 65 74 65 64 22 2c 20 53 51 4c 49 54   deleted", SQLIT
20e50 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a  E_STATIC);.  }..
20e51 64 65 6c 65 74 65 5f 66 72 6f 6d 5f 63 6c 65 61  delete_from_clea
20e52 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 41 75  nup:.  sqlite3Au
20e53 74 68 43 6f 6e 74 65 78 74 50 6f 70 28 26 73 43  thContextPop(&sC
20e54 6f 6e 74 65 78 74 29 3b 0a 20 20 73 71 6c 69 74  ontext);.  sqlit
20e55 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
20e56 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20  db, pTabList);. 
20e57 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
20e58 74 65 28 64 62 2c 20 70 57 68 65 72 65 29 3b 0a  te(db, pWhere);.
20e59 20 20 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 0a    return;.}../*.
20e5a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
20e5b 67 65 6e 65 72 61 74 65 73 20 56 44 42 45 20 63  generates VDBE c
20e5c 6f 64 65 20 74 68 61 74 20 63 61 75 73 65 73 20  ode that causes 
20e5d 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 6f 66 20  a single row of 
20e5e 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 74 61 62 6c  a.** single tabl
20e5f 65 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2e  e to be deleted.
20e60 0a 2a 2a 0a 2a 2a 20 54 68 65 20 56 44 42 45 20  .**.** The VDBE 
20e61 6d 75 73 74 20 62 65 20 69 6e 20 61 20 70 61 72  must be in a par
20e62 74 69 63 75 6c 61 72 20 73 74 61 74 65 20 77 68  ticular state wh
20e63 65 6e 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  en this routine 
20e64 69 73 20 63 61 6c 6c 65 64 2e 0a 2a 2a 20 54 68  is called..** Th
20e65 65 73 65 20 61 72 65 20 74 68 65 20 72 65 71 75  ese are the requ
20e66 69 72 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20  irements:.**.** 
20e67 20 20 31 2e 20 20 41 20 72 65 61 64 2f 77 72 69    1.  A read/wri
20e68 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69  te cursor pointi
20e69 6e 67 20 74 6f 20 70 54 61 62 2c 20 74 68 65 20  ng to pTab, the 
20e6a 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67  table containing
20e6b 20 74 68 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20   the row.**     
20e6c 20 20 74 6f 20 62 65 20 64 65 6c 65 74 65 64 2c    to be deleted,
20e6d 20 6d 75 73 74 20 62 65 20 6f 70 65 6e 65 64 20   must be opened 
20e6e 61 73 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  as cursor number
20e6f 20 22 62 61 73 65 22 2e 0a 2a 2a 0a 2a 2a 20 20   "base"..**.**  
20e70 20 32 2e 20 20 52 65 61 64 2f 77 72 69 74 65 20   2.  Read/write 
20e71 63 75 72 73 6f 72 73 20 66 6f 72 20 61 6c 6c 20  cursors for all 
20e72 69 6e 64 69 63 65 73 20 6f 66 20 70 54 61 62 20  indices of pTab 
20e73 6d 75 73 74 20 62 65 20 6f 70 65 6e 20 61 73 0a  must be open as.
20e74 2a 2a 20 20 20 20 20 20 20 63 75 72 73 6f 72 20  **       cursor 
20e75 6e 75 6d 62 65 72 20 62 61 73 65 2b 69 20 66 6f  number base+i fo
20e76 72 20 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78  r the i-th index
20e77 2e 0a 2a 2a 0a 2a 2a 20 20 20 33 2e 20 20 54 68  ..**.**   3.  Th
20e78 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72 20  e record number 
20e79 6f 66 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65  of the row to be
20e7a 20 64 65 6c 65 74 65 64 20 6d 75 73 74 20 62 65   deleted must be
20e7b 20 73 74 6f 72 65 64 20 69 6e 0a 2a 2a 20 20 20   stored in.**   
20e7c 20 20 20 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20      memory cell 
20e7d 69 52 6f 77 69 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  iRowid..**.** Th
20e7e 69 73 20 72 6f 75 74 69 6e 65 20 70 6f 70 73 20  is routine pops 
20e7f 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 20 73  the top of the s
20e80 74 61 63 6b 20 74 6f 20 72 65 6d 6f 76 65 20 74  tack to remove t
20e81 68 65 20 72 65 63 6f 72 64 20 6e 75 6d 62 65 72  he record number
20e82 0a 2a 2a 20 61 6e 64 20 74 68 65 6e 20 67 65 6e  .** and then gen
20e83 65 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 72  erates code to r
20e84 65 6d 6f 76 65 20 62 6f 74 68 20 74 68 65 20 74  emove both the t
20e85 61 62 6c 65 20 72 65 63 6f 72 64 20 61 6e 64 20  able record and 
20e86 61 6c 6c 20 69 6e 64 65 78 0a 2a 2a 20 65 6e 74  all index.** ent
20e87 72 69 65 73 20 74 68 61 74 20 70 6f 69 6e 74 20  ries that point 
20e88 74 6f 20 74 68 61 74 20 72 65 63 6f 72 64 2e 0a  to that record..
20e89 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
20e8a 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 47 65  E void sqlite3Ge
20e8b 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74 65 28  nerateRowDelete(
20e8c 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
20e8d 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67  ,     /* Parsing
20e8e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61   context */.  Ta
20e8f 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20  ble *pTab,      
20e90 20 2f 2a 20 54 61 62 6c 65 20 63 6f 6e 74 61 69   /* Table contai
20e91 6e 69 6e 67 20 74 68 65 20 72 6f 77 20 74 6f 20  ning the row to 
20e92 62 65 20 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20  be deleted */.  
20e93 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20 20 20  int iCur,       
20e94 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
20e95 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62 6c  ber for the tabl
20e96 65 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 77 69  e */.  int iRowi
20e97 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 4d 65 6d  d,        /* Mem
20e98 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 63 6f  ory cell that co
20e99 6e 74 61 69 6e 73 20 74 68 65 20 72 6f 77 69 64  ntains the rowid
20e9a 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f 0a 20 20   to delete */.  
20e9b 69 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20  int count       
20e9c 20 20 20 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20     /* Increment 
20e9d 74 68 65 20 72 6f 77 20 63 68 61 6e 67 65 20 63  the row change c
20e9e 6f 75 6e 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69  ounter */.){.  i
20e9f 6e 74 20 61 64 64 72 3b 0a 20 20 56 64 62 65 20  nt addr;.  Vdbe 
20ea0 2a 76 3b 0a 0a 20 20 76 20 3d 20 70 50 61 72 73  *v;..  v = pPars
20ea1 65 2d 3e 70 56 64 62 65 3b 0a 20 20 61 64 64 72  e->pVdbe;.  addr
20ea2 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
20ea3 64 4f 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78  dOp3(v, OP_NotEx
20ea4 69 73 74 73 2c 20 69 43 75 72 2c 20 30 2c 20 69  ists, iCur, 0, i
20ea5 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74 65  Rowid);.  sqlite
20ea6 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
20ea7 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  xDelete(pParse, 
20ea8 70 54 61 62 2c 20 69 43 75 72 2c 20 30 29 3b 0a  pTab, iCur, 0);.
20ea9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
20eaa 4f 70 32 28 76 2c 20 4f 50 5f 44 65 6c 65 74 65  Op2(v, OP_Delete
20eab 2c 20 69 43 75 72 2c 20 28 63 6f 75 6e 74 3f 4f  , iCur, (count?O
20eac 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 3a 30 29  PFLAG_NCHANGE:0)
20ead 29 3b 0a 20 20 69 66 28 20 63 6f 75 6e 74 20 29  );.  if( count )
20eae 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
20eaf 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c  eChangeP4(v, -1,
20eb0 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34   pTab->zName, P4
20eb1 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20  _STATIC);.  }.  
20eb2 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
20eb3 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a 7d 0a  ere(v, addr);.}.
20eb4 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
20eb5 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 56 44  ine generates VD
20eb6 42 45 20 63 6f 64 65 20 74 68 61 74 20 63 61 75  BE code that cau
20eb7 73 65 73 20 74 68 65 20 64 65 6c 65 74 69 6f 6e  ses the deletion
20eb8 20 6f 66 20 61 6c 6c 0a 2a 2a 20 69 6e 64 65 78   of all.** index
20eb9 20 65 6e 74 72 69 65 73 20 61 73 73 6f 63 69 61   entries associa
20eba 74 65 64 20 77 69 74 68 20 61 20 73 69 6e 67 6c  ted with a singl
20ebb 65 20 72 6f 77 20 6f 66 20 61 20 73 69 6e 67 6c  e row of a singl
20ebc 65 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54  e table..**.** T
20ebd 68 65 20 56 44 42 45 20 6d 75 73 74 20 62 65 20  he VDBE must be 
20ebe 69 6e 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  in a particular 
20ebf 73 74 61 74 65 20 77 68 65 6e 20 74 68 69 73 20  state when this 
20ec0 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
20ec1 64 2e 0a 2a 2a 20 54 68 65 73 65 20 61 72 65 20  d..** These are 
20ec2 74 68 65 20 72 65 71 75 69 72 65 6d 65 6e 74 73  the requirements
20ec3 3a 0a 2a 2a 0a 2a 2a 20 20 20 31 2e 20 20 41 20  :.**.**   1.  A 
20ec4 72 65 61 64 2f 77 72 69 74 65 20 63 75 72 73 6f  read/write curso
20ec5 72 20 70 6f 69 6e 74 69 6e 67 20 74 6f 20 70 54  r pointing to pT
20ec6 61 62 2c 20 74 68 65 20 74 61 62 6c 65 20 63 6f  ab, the table co
20ec7 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 72 6f 77  ntaining the row
20ec8 0a 2a 2a 20 20 20 20 20 20 20 74 6f 20 62 65 20  .**       to be 
20ec9 64 65 6c 65 74 65 64 2c 20 6d 75 73 74 20 62 65  deleted, must be
20eca 20 6f 70 65 6e 65 64 20 61 73 20 63 75 72 73 6f   opened as curso
20ecb 72 20 6e 75 6d 62 65 72 20 22 69 43 75 72 22 2e  r number "iCur".
20ecc 0a 2a 2a 0a 2a 2a 20 20 20 32 2e 20 20 52 65 61  .**.**   2.  Rea
20ecd 64 2f 77 72 69 74 65 20 63 75 72 73 6f 72 73 20  d/write cursors 
20ece 66 6f 72 20 61 6c 6c 20 69 6e 64 69 63 65 73 20  for all indices 
20ecf 6f 66 20 70 54 61 62 20 6d 75 73 74 20 62 65 20  of pTab must be 
20ed0 6f 70 65 6e 20 61 73 0a 2a 2a 20 20 20 20 20 20  open as.**      
20ed1 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 69   cursor number i
20ed2 43 75 72 2b 69 20 66 6f 72 20 74 68 65 20 69 2d  Cur+i for the i-
20ed3 74 68 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a 2a 20  th index..**.** 
20ed4 20 20 33 2e 20 20 54 68 65 20 22 69 43 75 72 22    3.  The "iCur"
20ed5 20 63 75 72 73 6f 72 20 6d 75 73 74 20 62 65 20   cursor must be 
20ed6 70 6f 69 6e 74 69 6e 67 20 74 6f 20 74 68 65 20  pointing to the 
20ed7 72 6f 77 20 74 68 61 74 20 69 73 20 74 6f 20 62  row that is to b
20ed8 65 0a 2a 2a 20 20 20 20 20 20 20 64 65 6c 65 74  e.**       delet
20ed9 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ed..*/.SQLITE_PR
20eda 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
20edb 65 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64  e3GenerateRowInd
20edc 65 78 44 65 6c 65 74 65 28 0a 20 20 50 61 72 73  exDelete(.  Pars
20edd 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 2f  e *pParse,     /
20ede 2a 20 50 61 72 73 69 6e 67 20 61 6e 64 20 63 6f  * Parsing and co
20edf 64 65 20 67 65 6e 65 72 61 74 69 6e 67 20 63 6f  de generating co
20ee0 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c 65  ntext */.  Table
20ee1 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 2f 2a   *pTab,       /*
20ee2 20 54 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   Table containin
20ee3 67 20 74 68 65 20 72 6f 77 20 74 6f 20 62 65 20  g the row to be 
20ee4 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 69 6e 74  deleted */.  int
20ee5 20 69 43 75 72 2c 20 20 20 20 20 20 20 20 20 20   iCur,          
20ee6 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72  /* Cursor number
20ee7 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 20 2a   for the table *
20ee8 2f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78  /.  int *aRegIdx
20ee9 20 20 20 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 64         /* Only d
20eea 65 6c 65 74 65 20 69 66 20 61 52 65 67 49 64 78  elete if aRegIdx
20eeb 21 3d 30 20 26 26 20 61 52 65 67 49 64 78 5b 69  !=0 && aRegIdx[i
20eec 5d 3e 30 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ]>0 */.){.  int 
20eed 69 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78  i;.  Index *pIdx
20eee 3b 0a 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 66  ;.  int r1;..  f
20eef 6f 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61  or(i=1, pIdx=pTa
20ef0 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
20ef1 20 69 2b 2b 2c 20 70 49 64 78 3d 70 49 64 78 2d   i++, pIdx=pIdx-
20ef2 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28  >pNext){.    if(
20ef3 20 61 52 65 67 49 64 78 21 3d 30 20 26 26 20 61   aRegIdx!=0 && a
20ef4 52 65 67 49 64 78 5b 69 2d 31 5d 3d 3d 30 20 29  RegIdx[i-1]==0 )
20ef5 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72   continue;.    r
20ef6 31 20 3d 20 73 71 6c 69 74 65 33 47 65 6e 65 72  1 = sqlite3Gener
20ef7 61 74 65 49 6e 64 65 78 4b 65 79 28 70 50 61 72  ateIndexKey(pPar
20ef8 73 65 2c 20 70 49 64 78 2c 20 69 43 75 72 2c 20  se, pIdx, iCur, 
20ef9 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
20efa 65 33 56 64 62 65 41 64 64 4f 70 33 28 70 50 61  e3VdbeAddOp3(pPa
20efb 72 73 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 49  rse->pVdbe, OP_I
20efc 64 78 44 65 6c 65 74 65 2c 20 69 43 75 72 2b 69  dxDelete, iCur+i
20efd 2c 20 72 31 2c 70 49 64 78 2d 3e 6e 43 6f 6c 75  , r1,pIdx->nColu
20efe 6d 6e 2b 31 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  mn+1);.  }.}../*
20eff 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64  .** Generate cod
20f00 65 20 74 68 61 74 20 77 69 6c 6c 20 61 73 73 65  e that will asse
20f01 6d 62 6c 65 20 61 6e 20 69 6e 64 65 78 20 6b 65  mble an index ke
20f02 79 20 61 6e 64 20 70 75 74 20 69 74 20 69 6e 20  y and put it in 
20f03 72 65 67 69 73 74 65 72 0a 2a 2a 20 72 65 67 4f  register.** regO
20f04 75 74 2e 20 20 54 68 65 20 6b 65 79 20 77 69 74  ut.  The key wit
20f05 68 20 62 65 20 66 6f 72 20 69 6e 64 65 78 20 70  h be for index p
20f06 49 64 78 20 77 68 69 63 68 20 69 73 20 61 6e 20  Idx which is an 
20f07 69 6e 64 65 78 20 6f 6e 20 70 54 61 62 2e 0a 2a  index on pTab..*
20f08 2a 20 69 43 75 72 20 69 73 20 74 68 65 20 69 6e  * iCur is the in
20f09 64 65 78 20 6f 66 20 61 20 63 75 72 73 6f 72 20  dex of a cursor 
20f0a 6f 70 65 6e 20 6f 6e 20 74 68 65 20 70 54 61 62  open on the pTab
20f0b 20 74 61 62 6c 65 20 61 6e 64 20 70 6f 69 6e 74   table and point
20f0c 69 6e 67 20 74 6f 0a 2a 2a 20 74 68 65 20 65 6e  ing to.** the en
20f0d 74 72 79 20 74 68 61 74 20 6e 65 65 64 73 20 69  try that needs i
20f0e 6e 64 65 78 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 52  ndexing..**.** R
20f0f 65 74 75 72 6e 20 61 20 72 65 67 69 73 74 65 72  eturn a register
20f10 20 6e 75 6d 62 65 72 20 77 68 69 63 68 20 69 73   number which is
20f11 20 74 68 65 20 66 69 72 73 74 20 69 6e 20 61 20   the first in a 
20f12 62 6c 6f 63 6b 20 6f 66 0a 2a 2a 20 72 65 67 69  block of.** regi
20f13 73 74 65 72 73 20 74 68 61 74 20 68 6f 6c 64 73  sters that holds
20f14 20 74 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66   the elements of
20f15 20 74 68 65 20 69 6e 64 65 78 20 6b 65 79 2e 20   the index key. 
20f16 20 54 68 65 0a 2a 2a 20 62 6c 6f 63 6b 20 6f 66   The.** block of
20f17 20 72 65 67 69 73 74 65 72 73 20 68 61 73 20 61   registers has a
20f18 6c 72 65 61 64 79 20 62 65 65 6e 20 64 65 61 6c  lready been deal
20f19 6c 6f 63 61 74 65 64 20 62 79 20 74 68 65 20 74  located by the t
20f1a 69 6d 65 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  ime.** this rout
20f1b 69 6e 65 20 72 65 74 75 72 6e 73 2e 0a 2a 2f 0a  ine returns..*/.
20f1c 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
20f1d 6e 74 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  nt sqlite3Genera
20f1e 74 65 49 6e 64 65 78 4b 65 79 28 0a 20 20 50 61  teIndexKey(.  Pa
20f1f 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
20f20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74   /* Parsing cont
20f21 65 78 74 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a  ext */.  Index *
20f22 70 49 64 78 2c 20 20 20 20 20 20 20 2f 2a 20 54  pIdx,       /* T
20f23 68 65 20 69 6e 64 65 78 20 66 6f 72 20 77 68 69  he index for whi
20f24 63 68 20 74 6f 20 67 65 6e 65 72 61 74 65 20 61  ch to generate a
20f25 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 69 43   key */.  int iC
20f26 75 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ur,          /* 
20f27 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f  Cursor number fo
20f28 72 20 74 68 65 20 70 49 64 78 2d 3e 70 54 61 62  r the pIdx->pTab
20f29 6c 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e  le table */.  in
20f2a 74 20 72 65 67 4f 75 74 2c 20 20 20 20 20 20 20  t regOut,       
20f2b 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 6e 65   /* Write the ne
20f2c 77 20 69 6e 64 65 78 20 6b 65 79 20 74 6f 20 74  w index key to t
20f2d 68 69 73 20 72 65 67 69 73 74 65 72 20 2a 2f 0a  his register */.
20f2e 20 20 69 6e 74 20 64 6f 4d 61 6b 65 52 65 63 20    int doMakeRec 
20f2f 20 20 20 20 20 2f 2a 20 52 75 6e 20 74 68 65 20       /* Run the 
20f30 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 20 69 6e  OP_MakeRecord in
20f31 73 74 72 75 63 74 69 6f 6e 20 69 66 20 74 72 75  struction if tru
20f32 65 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  e */.){.  Vdbe *
20f33 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
20f34 65 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 54 61  e;.  int j;.  Ta
20f35 62 6c 65 20 2a 70 54 61 62 20 3d 20 70 49 64 78  ble *pTab = pIdx
20f36 2d 3e 70 54 61 62 6c 65 3b 0a 20 20 69 6e 74 20  ->pTable;.  int 
20f37 72 65 67 42 61 73 65 3b 0a 20 20 69 6e 74 20 6e  regBase;.  int n
20f38 43 6f 6c 3b 0a 0a 20 20 6e 43 6f 6c 20 3d 20 70  Col;..  nCol = p
20f39 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20  Idx->nColumn;.  
20f3a 72 65 67 42 61 73 65 20 3d 20 73 71 6c 69 74 65  regBase = sqlite
20f3b 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50  3GetTempRange(pP
20f3c 61 72 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20  arse, nCol+1);. 
20f3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20f3e 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
20f3f 69 43 75 72 2c 20 72 65 67 42 61 73 65 2b 6e 43  iCur, regBase+nC
20f40 6f 6c 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20  ol);.  for(j=0; 
20f41 6a 3c 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b 0a 20 20  j<nCol; j++){.  
20f42 20 20 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78    int idx = pIdx
20f43 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 3b 0a 20  ->aiColumn[j];. 
20f44 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61 62     if( idx==pTab
20f45 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
20f46 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
20f47 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20  p2(v, OP_SCopy, 
20f48 72 65 67 42 61 73 65 2b 6e 43 6f 6c 2c 20 72 65  regBase+nCol, re
20f49 67 42 61 73 65 2b 6a 29 3b 0a 20 20 20 20 7d 65  gBase+j);.    }e
20f4a 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
20f4b 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
20f4c 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c  OP_Column, iCur,
20f4d 20 69 64 78 2c 20 72 65 67 42 61 73 65 2b 6a 29   idx, regBase+j)
20f4e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 43  ;.      sqlite3C
20f4f 6f 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20  olumnDefault(v, 
20f50 70 54 61 62 2c 20 69 64 78 29 3b 0a 20 20 20 20  pTab, idx);.    
20f51 7d 0a 20 20 7d 0a 20 20 69 66 28 20 64 6f 4d 61  }.  }.  if( doMa
20f52 6b 65 52 65 63 20 29 7b 0a 20 20 20 20 73 71 6c  keRec ){.    sql
20f53 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
20f54 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
20f55 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6c 2b 31   regBase, nCol+1
20f56 2c 20 72 65 67 4f 75 74 29 3b 0a 20 20 20 20 73  , regOut);.    s
20f57 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
20f58 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b  ityStr(v, pIdx);
20f59 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
20f5a 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
20f5b 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42  nge(pParse, regB
20f5c 61 73 65 2c 20 6e 43 6f 6c 2b 31 29 3b 0a 20 20  ase, nCol+1);.  
20f5d 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  }.  sqlite3Relea
20f5e 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  seTempRange(pPar
20f5f 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f  se, regBase, nCo
20f60 6c 2b 31 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  l+1);.  return r
20f61 65 67 42 61 73 65 3b 0a 7d 0a 0a 2f 2a 20 4d 61  egBase;.}../* Ma
20f62 6b 65 20 73 75 72 65 20 22 69 73 56 69 65 77 22  ke sure "isView"
20f63 20 67 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20   gets undefined 
20f64 69 6e 20 63 61 73 65 20 74 68 69 73 20 66 69 6c  in case this fil
20f65 65 20 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f  e becomes part o
20f66 66 0a 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d  f.** the amalgam
20f67 61 74 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20  ation - so that 
20f68 73 75 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73  subsequent files
20f69 20 64 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69   do not see isVi
20f6a 65 77 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f  ew as a.** macro
20f6b 2e 20 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69  . */.#undef isVi
20f6c 65 77 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ew../***********
20f6d 2a 2a 2a 20 45 6e 64 20 6f 66 20 64 65 6c 65 74  *** End of delet
20f6e 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e.c ************
20f6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f71 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
20f72 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
20f73 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  unc.c **********
20f74 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f75 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f76 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 32 20 46  **/./*.** 2002 F
20f77 65 62 72 75 61 72 79 20 32 33 0a 2a 2a 0a 2a 2a  ebruary 23.**.**
20f78 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
20f79 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
20f7a 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
20f7b 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f  ode.  In place o
20f7c 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74  f.** a legal not
20f7d 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62  ice, here is a b
20f7e 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20  lessing:.**.**  
20f7f 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f    May you do goo
20f80 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a  d and not evil..
20f81 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69  **    May you fi
20f82 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66  nd forgiveness f
20f83 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20  or yourself and 
20f84 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a  forgive others..
20f85 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68  **    May you sh
20f86 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65  are freely, neve
20f87 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68  r taking more th
20f88 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a  an you give..**.
20f89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f8c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
20f8d 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69  *********.** Thi
20f8e 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
20f8f 74 68 65 20 43 20 66 75 6e 63 74 69 6f 6e 73 20  the C functions 
20f90 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20 76  that implement v
20f91 61 72 69 6f 75 73 20 53 51 4c 0a 2a 2a 20 66 75  arious SQL.** fu
20f92 6e 63 74 69 6f 6e 73 20 6f 66 20 53 51 4c 69 74  nctions of SQLit
20f93 65 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 72 65  e.  .**.** There
20f94 20 69 73 20 6f 6e 6c 79 20 6f 6e 65 20 65 78 70   is only one exp
20f95 6f 72 74 65 64 20 73 79 6d 62 6f 6c 20 69 6e 20  orted symbol in 
20f96 74 68 69 73 20 66 69 6c 65 20 2d 20 74 68 65 20  this file - the 
20f97 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 73 71 6c 69  function.** sqli
20f98 74 65 52 65 67 69 73 74 65 72 42 75 69 6c 64 69  teRegisterBuildi
20f99 6e 46 75 6e 63 74 69 6f 6e 73 28 29 20 66 6f 75  nFunctions() fou
20f9a 6e 64 20 61 74 20 74 68 65 20 62 6f 74 74 6f 6d  nd at the bottom
20f9b 20 6f 66 20 74 68 65 20 66 69 6c 65 2e 0a 2a 2a   of the file..**
20f9c 20 41 6c 6c 20 6f 74 68 65 72 20 63 6f 64 65 20   All other code 
20f9d 68 61 73 20 66 69 6c 65 20 73 63 6f 70 65 2e 0a  has file scope..
20f9e 2a 2a 0a 2a 2a 20 24 49 64 3a 20 66 75 6e 63 2e  **.** $Id: func.
20f9f 63 2c 76 20 31 2e 32 30 36 20 32 30 30 38 2f 31  c,v 1.206 2008/1
20fa0 31 2f 31 39 20 31 36 3a 35 32 3a 34 34 20 64 61  1/19 16:52:44 da
20fa1 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a  nielk1977 Exp $.
20fa2 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  */../*.** Return
20fa3 20 74 68 65 20 63 6f 6c 6c 61 74 69 6e 67 20 66   the collating f
20fa4 75 6e 63 74 69 6f 6e 20 61 73 73 6f 63 69 61 74  unction associat
20fa5 65 64 20 77 69 74 68 20 61 20 66 75 6e 63 74 69  ed with a functi
20fa6 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f  on..*/.static Co
20fa7 6c 6c 53 65 71 20 2a 73 71 6c 69 74 65 33 47 65  llSeq *sqlite3Ge
20fa8 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28 73 71 6c  tFuncCollSeq(sql
20fa9 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f  ite3_context *co
20faa 6e 74 65 78 74 29 7b 0a 20 20 72 65 74 75 72 6e  ntext){.  return
20fab 20 63 6f 6e 74 65 78 74 2d 3e 70 43 6f 6c 6c 3b   context->pColl;
20fac 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
20fad 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
20fae 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65 20 6d 69  non-aggregate mi
20faf 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 66 75  n() and max() fu
20fb0 6e 63 74 69 6f 6e 73 0a 2a 2f 0a 73 74 61 74 69  nctions.*/.stati
20fb1 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 46 75 6e  c void minmaxFun
20fb2 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  c(.  sqlite3_con
20fb3 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20  text *context,. 
20fb4 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c   int argc,.  sql
20fb5 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
20fb6 76 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  v.){.  int i;.  
20fb7 69 6e 74 20 6d 61 73 6b 3b 20 20 20 20 2f 2a 20  int mask;    /* 
20fb8 30 20 66 6f 72 20 6d 69 6e 28 29 20 6f 72 20 30  0 for min() or 0
20fb9 78 66 66 66 66 66 66 66 66 20 66 6f 72 20 6d 61  xffffffff for ma
20fba 78 28 29 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65  x() */.  int iBe
20fbb 73 74 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  st;.  CollSeq *p
20fbc 43 6f 6c 6c 3b 0a 0a 20 20 69 66 28 20 61 72 67  Coll;..  if( arg
20fbd 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20  c==0 ) return;. 
20fbe 20 6d 61 73 6b 20 3d 20 73 71 6c 69 74 65 33 5f   mask = sqlite3_
20fbf 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
20fc0 74 29 3d 3d 30 20 3f 20 30 20 3a 20 2d 31 3b 0a  t)==0 ? 0 : -1;.
20fc1 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65    pColl = sqlite
20fc2 33 47 65 74 46 75 6e 63 43 6f 6c 6c 53 65 71 28  3GetFuncCollSeq(
20fc3 63 6f 6e 74 65 78 74 29 3b 0a 20 20 61 73 73 65  context);.  asse
20fc4 72 74 28 20 70 43 6f 6c 6c 20 29 3b 0a 20 20 61  rt( pColl );.  a
20fc5 73 73 65 72 74 28 20 6d 61 73 6b 3d 3d 2d 31 20  ssert( mask==-1 
20fc6 7c 7c 20 6d 61 73 6b 3d 3d 30 20 29 3b 0a 20 20  || mask==0 );.  
20fc7 69 42 65 73 74 20 3d 20 30 3b 0a 20 20 69 66 28  iBest = 0;.  if(
20fc8 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
20fc9 79 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51  ype(argv[0])==SQ
20fca 4c 49 54 45 5f 4e 55 4c 4c 20 29 20 72 65 74 75  LITE_NULL ) retu
20fcb 72 6e 3b 0a 20 20 66 6f 72 28 69 3d 31 3b 20 69  rn;.  for(i=1; i
20fcc 3c 61 72 67 63 3b 20 69 2b 2b 29 7b 0a 20 20 20  <argc; i++){.   
20fcd 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
20fce 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69 5d 29  ue_type(argv[i])
20fcf 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 20  ==SQLITE_NULL ) 
20fd0 72 65 74 75 72 6e 3b 0a 20 20 20 20 69 66 28 20  return;.    if( 
20fd1 28 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61  (sqlite3MemCompa
20fd2 72 65 28 61 72 67 76 5b 69 42 65 73 74 5d 2c 20  re(argv[iBest], 
20fd3 61 72 67 76 5b 69 5d 2c 20 70 43 6f 6c 6c 29 5e  argv[i], pColl)^
20fd4 6d 61 73 6b 29 3e 3d 30 20 29 7b 0a 20 20 20 20  mask)>=0 ){.    
20fd5 20 20 69 42 65 73 74 20 3d 20 69 3b 0a 20 20 20    iBest = i;.   
20fd6 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33   }.  }.  sqlite3
20fd7 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f  _result_value(co
20fd8 6e 74 65 78 74 2c 20 61 72 67 76 5b 69 42 65 73  ntext, argv[iBes
20fd9 74 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  t]);.}../*.** Re
20fda 74 75 72 6e 20 74 68 65 20 74 79 70 65 20 6f 66  turn the type of
20fdb 20 74 68 65 20 61 72 67 75 6d 65 6e 74 2e 0a 2a   the argument..*
20fdc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 79  /.static void ty
20fdd 70 65 6f 66 46 75 6e 63 28 0a 20 20 73 71 6c 69  peofFunc(.  sqli
20fde 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
20fdf 74 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55  text,.  int NotU
20fe0 73 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  sed,.  sqlite3_v
20fe1 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
20fe2 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
20fe3 20 30 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52   0;.  UNUSED_PAR
20fe4 41 4d 45 54 45 52 28 4e 6f 74 55 73 65 64 29 3b  AMETER(NotUsed);
20fe5 0a 20 20 73 77 69 74 63 68 28 20 73 71 6c 69 74  .  switch( sqlit
20fe6 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
20fe7 67 76 5b 30 5d 29 20 29 7b 0a 20 20 20 20 63 61  gv[0]) ){.    ca
20fe8 73 65 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 3a 20  se SQLITE_NULL: 
20fe9 20 20 20 7a 20 3d 20 22 6e 75 6c 6c 22 3b 20 20     z = "null";  
20fea 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
20feb 65 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52  e SQLITE_INTEGER
20fec 3a 20 7a 20 3d 20 22 69 6e 74 65 67 65 72 22 3b  : z = "integer";
20fed 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
20fee 20 53 51 4c 49 54 45 5f 54 45 58 54 3a 20 20 20   SQLITE_TEXT:   
20fef 20 7a 20 3d 20 22 74 65 78 74 22 3b 20 20 20 20   z = "text";    
20ff0 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
20ff1 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 20 20  SQLITE_FLOAT:   
20ff2 7a 20 3d 20 22 72 65 61 6c 22 3b 20 20 20 20 62  z = "real";    b
20ff3 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 53  reak;.    case S
20ff4 51 4c 49 54 45 5f 42 4c 4f 42 3a 20 20 20 20 7a  QLITE_BLOB:    z
20ff5 20 3d 20 22 62 6c 6f 62 22 3b 20 20 20 20 62 72   = "blob";    br
20ff6 65 61 6b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  eak;.  }.  sqlit
20ff7 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
20ff8 6f 6e 74 65 78 74 2c 20 7a 2c 20 2d 31 2c 20 53  ontext, z, -1, S
20ff9 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d  QLITE_STATIC);.}
20ffa 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  .../*.** Impleme
20ffb 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
20ffc 65 6e 67 74 68 28 29 20 66 75 6e 63 74 69 6f 6e  ength() function
20ffd 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
20ffe 6c 65 6e 67 74 68 46 75 6e 63 28 0a 20 20 73 71  lengthFunc(.  sq
20fff 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
21000 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72  ontext,.  int ar
21001 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  gc,.  sqlite3_va
21002 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20  lue **argv.){.  
21003 69 6e 74 20 6c 65 6e 3b 0a 0a 20 20 61 73 73 65  int len;..  asse
21004 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
21005 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
21006 52 28 61 72 67 63 29 3b 0a 20 20 73 77 69 74 63  R(argc);.  switc
21007 68 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  h( sqlite3_value
21008 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 20 29  _type(argv[0]) )
21009 7b 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  {.    case SQLIT
2100a 45 5f 42 4c 4f 42 3a 0a 20 20 20 20 63 61 73 65  E_BLOB:.    case
2100b 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 3a   SQLITE_INTEGER:
2100c 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2100d 5f 46 4c 4f 41 54 3a 20 7b 0a 20 20 20 20 20 20  _FLOAT: {.      
2100e 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69  sqlite3_result_i
2100f 6e 74 28 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69  nt(context, sqli
21010 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
21011 61 72 67 76 5b 30 5d 29 29 3b 0a 20 20 20 20 20  argv[0]));.     
21012 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21013 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 54 45    case SQLITE_TE
21014 58 54 3a 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73  XT: {.      cons
21015 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
21016 2a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  *z = sqlite3_val
21017 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
21018 3b 0a 20 20 20 20 20 20 69 66 28 20 7a 3d 3d 30  ;.      if( z==0
21019 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20   ) return;.     
2101a 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 20 20 20 20   len = 0;.      
2101b 77 68 69 6c 65 28 20 2a 7a 20 29 7b 0a 20 20 20  while( *z ){.   
2101c 20 20 20 20 20 6c 65 6e 2b 2b 3b 0a 20 20 20 20       len++;.    
2101d 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f      SQLITE_SKIP_
2101e 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20 7d  UTF8(z);.      }
2101f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
21020 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78  esult_int(contex
21021 74 2c 20 6c 65 6e 29 3b 0a 20 20 20 20 20 20 62  t, len);.      b
21022 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
21023 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
21024 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
21025 6e 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20  null(context);. 
21026 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21027 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  }.  }.}../*.** I
21028 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
21029 20 74 68 65 20 61 62 73 28 29 20 66 75 6e 63 74   the abs() funct
2102a 69 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ion.*/.static vo
2102b 69 64 20 61 62 73 46 75 6e 63 28 73 71 6c 69 74  id absFunc(sqlit
2102c 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
2102d 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
2102e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
2102f 72 67 76 29 7b 0a 20 20 61 73 73 65 72 74 28 20  rgv){.  assert( 
21030 61 72 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55  argc==1 );.  UNU
21031 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72  SED_PARAMETER(ar
21032 67 63 29 3b 0a 20 20 73 77 69 74 63 68 28 20 73  gc);.  switch( s
21033 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
21034 65 28 61 72 67 76 5b 30 5d 29 20 29 7b 0a 20 20  e(argv[0]) ){.  
21035 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
21036 54 45 47 45 52 3a 20 7b 0a 20 20 20 20 20 20 69  TEGER: {.      i
21037 36 34 20 69 56 61 6c 20 3d 20 73 71 6c 69 74 65  64 iVal = sqlite
21038 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 61 72  3_value_int64(ar
21039 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 66  gv[0]);.      if
2103a 28 20 69 56 61 6c 3c 30 20 29 7b 0a 20 20 20 20  ( iVal<0 ){.    
2103b 20 20 20 20 69 66 28 20 28 69 56 61 6c 3c 3c 31      if( (iVal<<1
2103c 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2103d 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2103e 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
2103f 22 69 6e 74 65 67 65 72 20 6f 76 65 72 66 6c 6f  "integer overflo
21040 77 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 20  w", -1);.       
21041 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 20     return;.     
21042 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 56 61     }.        iVa
21043 6c 20 3d 20 2d 69 56 61 6c 3b 0a 20 20 20 20 20  l = -iVal;.     
21044 20 7d 20 0a 20 20 20 20 20 20 73 71 6c 69 74 65   } .      sqlite
21045 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
21046 6f 6e 74 65 78 74 2c 20 69 56 61 6c 29 3b 0a 20  ontext, iVal);. 
21047 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
21048 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54  }.    case SQLIT
21049 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20 20 20 20  E_NULL: {.      
2104a 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 6e  sqlite3_result_n
2104b 75 6c 6c 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  ull(context);.  
2104c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2104d 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
2104e 20 20 20 20 20 20 64 6f 75 62 6c 65 20 72 56 61        double rVa
2104f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  l = sqlite3_valu
21050 65 5f 64 6f 75 62 6c 65 28 61 72 67 76 5b 30 5d  e_double(argv[0]
21051 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 56 61  );.      if( rVa
21052 6c 3c 30 20 29 20 72 56 61 6c 20 3d 20 2d 72 56  l<0 ) rVal = -rV
21053 61 6c 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  al;.      sqlite
21054 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28  3_result_double(
21055 63 6f 6e 74 65 78 74 2c 20 72 56 61 6c 29 3b 0a  context, rVal);.
21056 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
21057 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   }.  }.}../*.** 
21058 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
21059 66 20 74 68 65 20 73 75 62 73 74 72 28 29 20 66  f the substr() f
2105a 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 73  unction..**.** s
2105b 75 62 73 74 72 28 78 2c 70 31 2c 70 32 29 20 20  ubstr(x,p1,p2)  
2105c 72 65 74 75 72 6e 73 20 70 32 20 63 68 61 72 61  returns p2 chara
2105d 63 74 65 72 73 20 6f 66 20 78 5b 5d 20 62 65 67  cters of x[] beg
2105e 69 6e 6e 69 6e 67 20 77 69 74 68 20 70 31 2e 0a  inning with p1..
2105f 2a 2a 20 70 31 20 69 73 20 31 2d 69 6e 64 65 78  ** p1 is 1-index
21060 65 64 2e 20 20 53 6f 20 73 75 62 73 74 72 28 78  ed.  So substr(x
21061 2c 31 2c 31 29 20 72 65 74 75 72 6e 73 20 74 68  ,1,1) returns th
21062 65 20 66 69 72 73 74 20 63 68 61 72 61 63 74 65  e first characte
21063 72 0a 2a 2a 20 6f 66 20 78 2e 20 20 49 66 20 78  r.** of x.  If x
21064 20 69 73 20 74 65 78 74 2c 20 74 68 65 6e 20 77   is text, then w
21065 65 20 61 63 74 75 61 6c 6c 79 20 63 6f 75 6e 74  e actually count
21066 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
21067 73 2e 0a 2a 2a 20 49 66 20 78 20 69 73 20 61 20  s..** If x is a 
21068 62 6c 6f 62 2c 20 74 68 65 6e 20 77 65 20 63 6f  blob, then we co
21069 75 6e 74 20 62 79 74 65 73 2e 0a 2a 2a 0a 2a 2a  unt bytes..**.**
2106a 20 49 66 20 70 31 20 69 73 20 6e 65 67 61 74 69   If p1 is negati
2106b 76 65 2c 20 74 68 65 6e 20 77 65 20 62 65 67 69  ve, then we begi
2106c 6e 20 61 62 73 28 70 31 29 20 66 72 6f 6d 20 74  n abs(p1) from t
2106d 68 65 20 65 6e 64 20 6f 66 20 78 5b 5d 2e 0a 2a  he end of x[]..*
2106e 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 75  /.static void su
2106f 62 73 74 72 46 75 6e 63 28 0a 20 20 73 71 6c 69  bstrFunc(.  sqli
21070 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
21071 74 65 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63  text,.  int argc
21072 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
21073 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f  e **argv.){.  co
21074 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
21075 72 20 2a 7a 3b 0a 20 20 63 6f 6e 73 74 20 75 6e  r *z;.  const un
21076 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 32 3b  signed char *z2;
21077 0a 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 69 6e  .  int len;.  in
21078 74 20 70 30 74 79 70 65 3b 0a 20 20 69 36 34 20  t p0type;.  i64 
21079 70 31 2c 20 70 32 3b 0a 0a 20 20 61 73 73 65 72  p1, p2;..  asser
2107a 74 28 20 61 72 67 63 3d 3d 33 20 7c 7c 20 61 72  t( argc==3 || ar
2107b 67 63 3d 3d 32 20 29 3b 0a 20 20 70 30 74 79 70  gc==2 );.  p0typ
2107c 65 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  e = sqlite3_valu
2107d 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d 29 3b  e_type(argv[0]);
2107e 0a 20 20 69 66 28 20 70 30 74 79 70 65 3d 3d 53  .  if( p0type==S
2107f 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
21080 20 20 6c 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f    len = sqlite3_
21081 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
21082 5b 30 5d 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71  [0]);.    z = sq
21083 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
21084 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 69  (argv[0]);.    i
21085 66 28 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e  f( z==0 ) return
21086 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6c 65  ;.    assert( le
21087 6e 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  n==sqlite3_value
21088 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 20  _bytes(argv[0]) 
21089 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
2108a 7a 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  z = sqlite3_valu
2108b 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2108c 0a 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20  .    if( z==0 ) 
2108d 72 65 74 75 72 6e 3b 0a 20 20 20 20 6c 65 6e 20  return;.    len 
2108e 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 7a 32 3d  = 0;.    for(z2=
2108f 7a 3b 20 2a 7a 32 3b 20 6c 65 6e 2b 2b 29 7b 0a  z; *z2; len++){.
21090 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49        SQLITE_SKI
21091 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20 20 20  P_UTF8(z2);.    
21092 7d 0a 20 20 7d 0a 20 20 70 31 20 3d 20 73 71 6c  }.  }.  p1 = sql
21093 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 28 61  ite3_value_int(a
21094 72 67 76 5b 31 5d 29 3b 0a 20 20 69 66 28 20 61  rgv[1]);.  if( a
21095 72 67 63 3d 3d 33 20 29 7b 0a 20 20 20 20 70 32  rgc==3 ){.    p2
21096 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
21097 5f 69 6e 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20  _int(argv[2]);. 
21098 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 32 20 3d   }else{.    p2 =
21099 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2109a 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65  _db_handle(conte
2109b 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49  xt)->aLimit[SQLI
2109c 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
2109d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 31 3c 30  ;.  }.  if( p1<0
2109e 20 29 7b 0a 20 20 20 20 70 31 20 2b 3d 20 6c 65   ){.    p1 += le
2109f 6e 3b 0a 20 20 20 20 69 66 28 20 70 31 3c 30 20  n;.    if( p1<0 
210a0 29 7b 0a 20 20 20 20 20 20 70 32 20 2b 3d 20 70  ){.      p2 += p
210a1 31 3b 0a 20 20 20 20 20 20 70 31 20 3d 20 30 3b  1;.      p1 = 0;
210a2 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20 69  .    }.  }else i
210a3 66 28 20 70 31 3e 30 20 29 7b 0a 20 20 20 20 70  f( p1>0 ){.    p
210a4 31 2d 2d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  1--;.  }.  if( p
210a5 31 2b 70 32 3e 6c 65 6e 20 29 7b 0a 20 20 20 20  1+p2>len ){.    
210a6 70 32 20 3d 20 6c 65 6e 2d 70 31 3b 0a 20 20 7d  p2 = len-p1;.  }
210a7 0a 20 20 69 66 28 20 70 30 74 79 70 65 21 3d 53  .  if( p0type!=S
210a8 51 4c 49 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20  QLITE_BLOB ){.  
210a9 20 20 77 68 69 6c 65 28 20 2a 7a 20 26 26 20 70    while( *z && p
210aa 31 20 29 7b 0a 20 20 20 20 20 20 53 51 4c 49 54  1 ){.      SQLIT
210ab 45 5f 53 4b 49 50 5f 55 54 46 38 28 7a 29 3b 0a  E_SKIP_UTF8(z);.
210ac 20 20 20 20 20 20 70 31 2d 2d 3b 0a 20 20 20 20        p1--;.    
210ad 7d 0a 20 20 20 20 66 6f 72 28 7a 32 3d 7a 3b 20  }.    for(z2=z; 
210ae 2a 7a 32 20 26 26 20 70 32 3b 20 70 32 2d 2d 29  *z2 && p2; p2--)
210af 7b 0a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53  {.      SQLITE_S
210b0 4b 49 50 5f 55 54 46 38 28 7a 32 29 3b 0a 20 20  KIP_UTF8(z2);.  
210b1 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 5f    }.    sqlite3_
210b2 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
210b3 65 78 74 2c 20 28 63 68 61 72 2a 29 7a 2c 20 7a  ext, (char*)z, z
210b4 32 2d 7a 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e  2-z, SQLITE_TRAN
210b5 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65 7b  SIENT);.  }else{
210b6 0a 20 20 20 20 69 66 28 20 70 32 3c 30 20 29 20  .    if( p2<0 ) 
210b7 70 32 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69  p2 = 0;.    sqli
210b8 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28  te3_result_blob(
210b9 63 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29  context, (char*)
210ba 26 7a 5b 70 31 5d 2c 20 70 32 2c 20 53 51 4c 49  &z[p1], p2, SQLI
210bb 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20  TE_TRANSIENT);. 
210bc 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
210bd 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
210be 65 20 72 6f 75 6e 64 28 29 20 66 75 6e 63 74 69  e round() functi
210bf 6f 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  on.*/.static voi
210c0 64 20 72 6f 75 6e 64 46 75 6e 63 28 73 71 6c 69  d roundFunc(sqli
210c1 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
210c2 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
210c3 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
210c4 61 72 67 76 29 7b 0a 20 20 69 6e 74 20 6e 20 3d  argv){.  int n =
210c5 20 30 3b 0a 20 20 64 6f 75 62 6c 65 20 72 3b 0a   0;.  double r;.
210c6 20 20 63 68 61 72 20 7a 42 75 66 5b 35 30 30 5d    char zBuf[500]
210c7 3b 20 20 2f 2a 20 6c 61 72 67 65 72 20 74 68 61  ;  /* larger tha
210c8 6e 20 74 68 65 20 25 66 20 72 65 70 72 65 73 65  n the %f represe
210c9 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
210ca 61 72 67 65 73 74 20 64 6f 75 62 6c 65 20 2a 2f  argest double */
210cb 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d  .  assert( argc=
210cc 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29 3b  =1 || argc==2 );
210cd 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32 20 29  .  if( argc==2 )
210ce 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
210cf 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76  _NULL==sqlite3_v
210d0 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 31  alue_type(argv[1
210d1 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20  ]) ) return;.   
210d2 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
210d3 75 65 5f 69 6e 74 28 61 72 67 76 5b 31 5d 29 3b  ue_int(argv[1]);
210d4 0a 20 20 20 20 69 66 28 20 6e 3e 33 30 20 29 20  .    if( n>30 ) 
210d5 6e 20 3d 20 33 30 3b 0a 20 20 20 20 69 66 28 20  n = 30;.    if( 
210d6 6e 3c 30 20 29 20 6e 20 3d 20 30 3b 0a 20 20 7d  n<0 ) n = 0;.  }
210d7 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76  .  if( sqlite3_v
210d8 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30  alue_type(argv[0
210d9 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  ])==SQLITE_NULL 
210da 29 20 72 65 74 75 72 6e 3b 0a 20 20 72 20 3d 20  ) return;.  r = 
210db 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f  sqlite3_value_do
210dc 75 62 6c 65 28 61 72 67 76 5b 30 5d 29 3b 0a 20  uble(argv[0]);. 
210dd 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
210de 66 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2c 7a  f(sizeof(zBuf),z
210df 42 75 66 2c 22 25 2e 2a 66 22 2c 6e 2c 72 29 3b  Buf,"%.*f",n,r);
210e0 0a 20 20 73 71 6c 69 74 65 33 41 74 6f 46 28 7a  .  sqlite3AtoF(z
210e1 42 75 66 2c 20 26 72 29 3b 0a 20 20 73 71 6c 69  Buf, &r);.  sqli
210e2 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
210e3 65 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b 0a 7d  e(context, r);.}
210e4 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
210e5 20 6e 42 79 74 65 20 62 79 74 65 73 20 6f 66 20   nByte bytes of 
210e6 73 70 61 63 65 20 75 73 69 6e 67 20 73 71 6c 69  space using sqli
210e7 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e 20 49 66  te3_malloc(). If
210e8 20 74 68 65 0a 2a 2a 20 61 6c 6c 6f 63 61 74 69   the.** allocati
210e9 6f 6e 20 66 61 69 6c 73 2c 20 63 61 6c 6c 20 73  on fails, call s
210ea 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
210eb 72 6f 72 5f 6e 6f 6d 65 6d 28 29 20 74 6f 20 6e  ror_nomem() to n
210ec 6f 74 69 66 79 0a 2a 2a 20 74 68 65 20 64 61 74  otify.** the dat
210ed 61 62 61 73 65 20 68 61 6e 64 6c 65 20 74 68 61  abase handle tha
210ee 74 20 6d 61 6c 6c 6f 63 28 29 20 68 61 73 20 66  t malloc() has f
210ef 61 69 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ailed..*/.static
210f0 20 76 6f 69 64 20 2a 63 6f 6e 74 65 78 74 4d 61   void *contextMa
210f1 6c 6c 6f 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e  lloc(sqlite3_con
210f2 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69  text *context, i
210f3 36 34 20 6e 42 79 74 65 29 7b 0a 20 20 63 68 61  64 nByte){.  cha
210f4 72 20 2a 7a 3b 0a 20 20 69 66 28 20 6e 42 79 74  r *z;.  if( nByt
210f5 65 3e 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e>sqlite3_contex
210f6 74 5f 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74  t_db_handle(cont
210f7 65 78 74 29 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c  ext)->aLimit[SQL
210f8 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48  ITE_LIMIT_LENGTH
210f9 5d 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  ] ){.    sqlite3
210fa 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 74 6f  _result_error_to
210fb 6f 62 69 67 28 63 6f 6e 74 65 78 74 29 3b 0a 20  obig(context);. 
210fc 20 20 20 7a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73     z = 0;.  }els
210fd 65 7b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69 74  e{.    z = sqlit
210fe 65 33 4d 61 6c 6c 6f 63 28 6e 42 79 74 65 29 3b  e3Malloc(nByte);
210ff 0a 20 20 20 20 69 66 28 20 21 7a 20 26 26 20 6e  .    if( !z && n
21100 42 79 74 65 3e 30 20 29 7b 0a 20 20 20 20 20 20  Byte>0 ){.      
21101 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
21102 72 72 6f 72 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65  rror_nomem(conte
21103 78 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  xt);.    }.  }. 
21104 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 0a 2f 2a   return z;.}../*
21105 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
21106 6f 6e 20 6f 66 20 74 68 65 20 75 70 70 65 72 28  on of the upper(
21107 29 20 61 6e 64 20 6c 6f 77 65 72 28 29 20 53 51  ) and lower() SQ
21108 4c 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a  L functions..*/.
21109 73 74 61 74 69 63 20 76 6f 69 64 20 75 70 70 65  static void uppe
2110a 72 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  rFunc(sqlite3_co
2110b 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20  ntext *context, 
2110c 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65  int argc, sqlite
2110d 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b  3_value **argv){
2110e 0a 20 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63  .  char *z1;.  c
2110f 6f 6e 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20  onst char *z2;. 
21110 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28   int i, n;.  if(
21111 20 61 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54   argc<1 || SQLIT
21112 45 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f  E_NULL==sqlite3_
21113 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
21114 30 5d 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  0]) ) return;.  
21115 7a 32 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69  z2 = (char*)sqli
21116 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
21117 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73  rgv[0]);.  n = s
21118 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
21119 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f  es(argv[0]);.  /
2111a 2a 20 56 65 72 69 66 79 20 74 68 61 74 20 74 68  * Verify that th
2111b 65 20 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73  e call to _bytes
2111c 28 29 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61  () does not inva
2111d 6c 69 64 61 74 65 20 74 68 65 20 5f 74 65 78 74  lidate the _text
2111e 28 29 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20  () pointer */.  
2111f 61 73 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61  assert( z2==(cha
21120 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  r*)sqlite3_value
21121 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29  _text(argv[0]) )
21122 3b 0a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20  ;.  if( z2 ){.  
21123 20 20 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61    z1 = contextMa
21124 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28  lloc(context, ((
21125 69 36 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69  i64)n)+1);.    i
21126 66 28 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d  f( z1 ){.      m
21127 65 6d 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b  emcpy(z1, z2, n+
21128 31 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d  1);.      for(i=
21129 30 3b 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  0; z1[i]; i++){.
2112a 20 20 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20          z1[i] = 
2112b 74 6f 75 70 70 65 72 28 7a 31 5b 69 5d 29 3b 0a  toupper(z1[i]);.
2112c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71        }.      sq
2112d 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2112e 74 28 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d  t(context, z1, -
2112f 31 2c 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29  1, sqlite3_free)
21130 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74  ;.    }.  }.}.st
21131 61 74 69 63 20 76 6f 69 64 20 6c 6f 77 65 72 46  atic void lowerF
21132 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  unc(sqlite3_cont
21133 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 69 6e  ext *context, in
21134 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
21135 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
21136 20 63 68 61 72 20 2a 7a 31 3b 0a 20 20 63 6f 6e   char *z1;.  con
21137 73 74 20 63 68 61 72 20 2a 7a 32 3b 0a 20 20 69  st char *z2;.  i
21138 6e 74 20 69 2c 20 6e 3b 0a 20 20 69 66 28 20 61  nt i, n;.  if( a
21139 72 67 63 3c 31 20 7c 7c 20 53 51 4c 49 54 45 5f  rgc<1 || SQLITE_
2113a 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 76 61  NULL==sqlite3_va
2113b 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
2113c 29 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 7a 32  ) ) return;.  z2
2113d 20 3d 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65   = (char*)sqlite
2113e 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
2113f 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c  v[0]);.  n = sql
21140 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
21141 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 2f 2a 20  (argv[0]);.  /* 
21142 56 65 72 69 66 79 20 74 68 61 74 20 74 68 65 20  Verify that the 
21143 63 61 6c 6c 20 74 6f 20 5f 62 79 74 65 73 28 29  call to _bytes()
21144 20 64 6f 65 73 20 6e 6f 74 20 69 6e 76 61 6c 69   does not invali
21145 64 61 74 65 20 74 68 65 20 5f 74 65 78 74 28 29  date the _text()
21146 20 70 6f 69 6e 74 65 72 20 2a 2f 0a 20 20 61 73   pointer */.  as
21147 73 65 72 74 28 20 7a 32 3d 3d 28 63 68 61 72 2a  sert( z2==(char*
21148 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74  )sqlite3_value_t
21149 65 78 74 28 61 72 67 76 5b 30 5d 29 20 29 3b 0a  ext(argv[0]) );.
2114a 20 20 69 66 28 20 7a 32 20 29 7b 0a 20 20 20 20    if( z2 ){.    
2114b 7a 31 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  z1 = contextMall
2114c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36  oc(context, ((i6
2114d 34 29 6e 29 2b 31 29 3b 0a 20 20 20 20 69 66 28  4)n)+1);.    if(
2114e 20 7a 31 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d   z1 ){.      mem
2114f 63 70 79 28 7a 31 2c 20 7a 32 2c 20 6e 2b 31 29  cpy(z1, z2, n+1)
21150 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
21151 20 7a 31 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20   z1[i]; i++){.  
21152 20 20 20 20 20 20 7a 31 5b 69 5d 20 3d 20 74 6f        z1[i] = to
21153 6c 6f 77 65 72 28 7a 31 5b 69 5d 29 3b 0a 20 20  lower(z1[i]);.  
21154 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
21155 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
21156 63 6f 6e 74 65 78 74 2c 20 7a 31 2c 20 2d 31 2c  context, z1, -1,
21157 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
21158 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a      }.  }.}../*.
21159 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
2115a 6e 20 6f 66 20 74 68 65 20 49 46 4e 55 4c 4c 28  n of the IFNULL(
2115b 29 2c 20 4e 56 4c 28 29 2c 20 61 6e 64 20 43 4f  ), NVL(), and CO
2115c 41 4c 45 53 43 45 28 29 20 66 75 6e 63 74 69 6f  ALESCE() functio
2115d 6e 73 2e 20 20 0a 2a 2a 20 41 6c 6c 20 74 68 72  ns.  .** All thr
2115e 65 65 20 64 6f 20 74 68 65 20 73 61 6d 65 20 74  ee do the same t
2115f 68 69 6e 67 2e 20 20 54 68 65 79 20 72 65 74 75  hing.  They retu
21160 72 6e 20 74 68 65 20 66 69 72 73 74 20 6e 6f 6e  rn the first non
21161 2d 4e 55 4c 4c 0a 2a 2a 20 61 72 67 75 6d 65 6e  -NULL.** argumen
21162 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  t..*/.static voi
21163 64 20 69 66 6e 75 6c 6c 46 75 6e 63 28 0a 20 20  d ifnullFunc(.  
21164 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
21165 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  *context,.  int 
21166 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
21167 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
21168 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69    int i;.  for(i
21169 3d 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29  =0; i<argc; i++)
2116a 7b 0a 20 20 20 20 69 66 28 20 53 51 4c 49 54 45  {.    if( SQLITE
2116b 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65 33 5f 76  _NULL!=sqlite3_v
2116c 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 69  alue_type(argv[i
2116d 5d 29 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ]) ){.      sqli
2116e 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c 75 65  te3_result_value
2116f 28 63 6f 6e 74 65 78 74 2c 20 61 72 67 76 5b 69  (context, argv[i
21170 5d 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  ]);.      break;
21171 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a  .    }.  }.}../*
21172 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
21173 6f 6e 20 6f 66 20 72 61 6e 64 6f 6d 28 29 2e 20  on of random(). 
21174 20 52 65 74 75 72 6e 20 61 20 72 61 6e 64 6f 6d   Return a random
21175 20 69 6e 74 65 67 65 72 2e 20 20 0a 2a 2f 0a 73   integer.  .*/.s
21176 74 61 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f  tatic void rando
21177 6d 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33  mFunc(.  sqlite3
21178 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
21179 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65 64  t,.  int NotUsed
2117a 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  ,.  sqlite3_valu
2117b 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29 7b 0a  e **NotUsed2.){.
2117c 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 72    sqlite_int64 r
2117d 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  ;.  UNUSED_PARAM
2117e 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
2117f 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69  otUsed2);.  sqli
21180 74 65 33 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 73  te3_randomness(s
21181 69 7a 65 6f 66 28 72 29 2c 20 26 72 29 3b 0a 20  izeof(r), &r);. 
21182 20 69 66 28 20 28 72 3c 3c 31 29 3d 3d 30 20 29   if( (r<<1)==0 )
21183 20 72 20 3d 20 30 3b 20 20 2f 2a 20 50 72 65 76   r = 0;  /* Prev
21184 65 6e 74 20 30 78 38 30 30 30 2e 2e 2e 2e 20 61  ent 0x8000.... a
21185 73 20 74 68 65 20 72 65 73 75 6c 74 20 73 6f 20  s the result so 
21186 74 68 61 74 20 77 65 20 2a 2f 0a 20 20 20 20 20  that we */.     
21187 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21188 20 20 20 20 20 2f 2a 20 63 61 6e 20 61 6c 77 61       /* can alwa
21189 79 73 20 64 6f 20 61 62 73 28 29 20 6f 66 20 74  ys do abs() of t
2118a 68 65 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 73  he result */.  s
2118b 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e  qlite3_result_in
2118c 74 36 34 28 63 6f 6e 74 65 78 74 2c 20 72 29 3b  t64(context, r);
2118d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  .}../*.** Implem
2118e 65 6e 74 61 74 69 6f 6e 20 6f 66 20 72 61 6e 64  entation of rand
2118f 6f 6d 62 6c 6f 62 28 4e 29 2e 20 20 52 65 74 75  omblob(N).  Retu
21190 72 6e 20 61 20 72 61 6e 64 6f 6d 20 62 6c 6f 62  rn a random blob
21191 0a 2a 2a 20 74 68 61 74 20 69 73 20 4e 20 62 79  .** that is N by
21192 74 65 73 20 6c 6f 6e 67 2e 0a 2a 2f 0a 73 74 61  tes long..*/.sta
21193 74 69 63 20 76 6f 69 64 20 72 61 6e 64 6f 6d 42  tic void randomB
21194 6c 6f 62 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  lob(.  sqlite3_c
21195 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
21196 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73  .  int argc,.  s
21197 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
21198 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 6e 3b 0a  rgv.){.  int n;.
21199 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
2119a 2a 70 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  *p;.  assert( ar
2119b 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
2119c 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
2119d 29 3b 0a 20 20 6e 20 3d 20 73 71 6c 69 74 65 33  );.  n = sqlite3
2119e 5f 76 61 6c 75 65 5f 69 6e 74 28 61 72 67 76 5b  _value_int(argv[
2119f 30 5d 29 3b 0a 20 20 69 66 28 20 6e 3c 31 20 29  0]);.  if( n<1 )
211a0 7b 0a 20 20 20 20 6e 20 3d 20 31 3b 0a 20 20 7d  {.    n = 1;.  }
211a1 0a 20 20 70 20 3d 20 63 6f 6e 74 65 78 74 4d 61  .  p = contextMa
211a2 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 6e 29  lloc(context, n)
211a3 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20 20 20  ;.  if( p ){.   
211a4 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
211a5 65 73 73 28 6e 2c 20 70 29 3b 0a 20 20 20 20 73  ess(n, p);.    s
211a6 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 62 6c  qlite3_result_bl
211a7 6f 62 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  ob(context, (cha
211a8 72 2a 29 70 2c 20 6e 2c 20 73 71 6c 69 74 65 33  r*)p, n, sqlite3
211a9 5f 66 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f  _free);.  }.}../
211aa 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
211ab 69 6f 6e 20 6f 66 20 74 68 65 20 6c 61 73 74 5f  ion of the last_
211ac 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29 20 53  insert_rowid() S
211ad 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68  QL function.  Th
211ae 65 20 72 65 74 75 72 6e 0a 2a 2a 20 76 61 6c 75  e return.** valu
211af 65 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73  e is the same as
211b0 20 74 68 65 20 73 71 6c 69 74 65 33 5f 6c 61 73   the sqlite3_las
211b1 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 29  t_insert_rowid()
211b2 20 41 50 49 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a   API function..*
211b3 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 61  /.static void la
211b4 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 28  st_insert_rowid(
211b5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
211b6 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20 20  xt *context, .  
211b7 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a 20 20  int NotUsed, .  
211b8 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
211b9 4e 6f 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71  NotUsed2.){.  sq
211ba 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
211bb 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
211bc 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
211bd 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
211be 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74  ER2(NotUsed, Not
211bf 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65  Used2);.  sqlite
211c0 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28 63  3_result_int64(c
211c1 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33 5f  ontext, sqlite3_
211c2 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
211c3 64 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  d(db));.}../*.**
211c4 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
211c5 6f 66 20 74 68 65 20 63 68 61 6e 67 65 73 28 29  of the changes()
211c6 20 53 51 4c 20 66 75 6e 63 74 69 6f 6e 2e 20 20   SQL function.  
211c7 54 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  The return value
211c8 20 69 73 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20   is the.** same 
211c9 61 73 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63  as the sqlite3_c
211ca 68 61 6e 67 65 73 28 29 20 41 50 49 20 66 75 6e  hanges() API fun
211cb 63 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63  ction..*/.static
211cc 20 76 6f 69 64 20 63 68 61 6e 67 65 73 28 0a 20   void changes(. 
211cd 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
211ce 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
211cf 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71 6c 69   NotUsed,.  sqli
211d0 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f 74 55  te3_value **NotU
211d1 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69 74 65  sed2.){.  sqlite
211d2 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 5f  3 *db = sqlite3_
211d3 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
211d4 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 55 4e  e(context);.  UN
211d5 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
211d6 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65 64  NotUsed, NotUsed
211d7 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  2);.  sqlite3_re
211d8 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
211d9 2c 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67 65  , sqlite3_change
211da 73 28 64 62 29 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  s(db));.}../*.**
211db 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   Implementation 
211dc 6f 66 20 74 68 65 20 74 6f 74 61 6c 5f 63 68 61  of the total_cha
211dd 6e 67 65 73 28 29 20 53 51 4c 20 66 75 6e 63 74  nges() SQL funct
211de 69 6f 6e 2e 20 20 54 68 65 20 72 65 74 75 72 6e  ion.  The return
211df 20 76 61 6c 75 65 20 69 73 0a 2a 2a 20 74 68 65   value is.** the
211e0 20 73 61 6d 65 20 61 73 20 74 68 65 20 73 71 6c   same as the sql
211e1 69 74 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67  ite3_total_chang
211e2 65 73 28 29 20 41 50 49 20 66 75 6e 63 74 69 6f  es() API functio
211e3 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  n..*/.static voi
211e4 64 20 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73 28  d total_changes(
211e5 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
211e6 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69  xt *context,.  i
211e7 6e 74 20 4e 6f 74 55 73 65 64 2c 0a 20 20 73 71  nt NotUsed,.  sq
211e8 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 4e 6f  lite3_value **No
211e9 74 55 73 65 64 32 0a 29 7b 0a 20 20 73 71 6c 69  tUsed2.){.  sqli
211ea 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65  te3 *db = sqlite
211eb 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e  3_context_db_han
211ec 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20  dle(context);.  
211ed 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52  UNUSED_PARAMETER
211ee 32 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73  2(NotUsed, NotUs
211ef 65 64 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ed2);.  sqlite3_
211f0 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65  result_int(conte
211f1 78 74 2c 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  xt, sqlite3_tota
211f2 6c 5f 63 68 61 6e 67 65 73 28 64 62 29 29 3b 0a  l_changes(db));.
211f3 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 73 74 72 75 63  }../*.** A struc
211f4 74 75 72 65 20 64 65 66 69 6e 69 6e 67 20 68 6f  ture defining ho
211f5 77 20 74 6f 20 64 6f 20 47 4c 4f 42 2d 73 74 79  w to do GLOB-sty
211f6 6c 65 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a  le comparisons..
211f7 2a 2f 0a 73 74 72 75 63 74 20 63 6f 6d 70 61 72  */.struct compar
211f8 65 49 6e 66 6f 20 7b 0a 20 20 75 38 20 6d 61 74  eInfo {.  u8 mat
211f9 63 68 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63  chAll;.  u8 matc
211fa 68 4f 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68  hOne;.  u8 match
211fb 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73 65  Set;.  u8 noCase
211fc 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 46 6f 72 20  ;.};../*.** For 
211fd 4c 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 6d 61  LIKE and GLOB ma
211fe 74 63 68 69 6e 67 20 6f 6e 20 45 42 43 44 49 43  tching on EBCDIC
211ff 20 6d 61 63 68 69 6e 65 73 2c 20 61 73 73 75 6d   machines, assum
21200 65 20 74 68 61 74 20 65 76 65 72 79 0a 2a 2a 20  e that every.** 
21201 63 68 61 72 61 63 74 65 72 20 69 73 20 65 78 61  character is exa
21202 63 74 6c 79 20 6f 6e 65 20 62 79 74 65 20 69 6e  ctly one byte in
21203 20 73 69 7a 65 2e 20 20 41 6c 73 6f 2c 20 61 6c   size.  Also, al
21204 6c 20 63 68 61 72 61 63 74 65 72 73 20 61 72 65  l characters are
21205 0a 2a 2a 20 61 62 6c 65 20 74 6f 20 70 61 72 74  .** able to part
21206 69 63 69 70 61 74 65 20 69 6e 20 75 70 70 65 72  icipate in upper
21207 2d 63 61 73 65 2d 74 6f 2d 6c 6f 77 65 72 2d 63  -case-to-lower-c
21208 61 73 65 20 6d 61 70 70 69 6e 67 73 20 69 6e 20  ase mappings in 
21209 45 42 43 44 49 43 0a 2a 2a 20 77 68 65 72 65 61  EBCDIC.** wherea
2120a 73 20 6f 6e 6c 79 20 63 68 61 72 61 63 74 65 72  s only character
2120b 73 20 6c 65 73 73 20 74 68 61 6e 20 30 78 38 30  s less than 0x80
2120c 20 64 6f 20 69 6e 20 41 53 43 49 49 2e 0a 2a 2f   do in ASCII..*/
2120d 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
2120e 49 54 45 5f 45 42 43 44 49 43 29 0a 23 20 64 65  ITE_EBCDIC).# de
2120f 66 69 6e 65 20 73 71 6c 69 74 65 33 55 74 66 38  fine sqlite3Utf8
21210 52 65 61 64 28 41 2c 42 2c 43 29 20 20 28 2a 28  Read(A,B,C)  (*(
21211 41 2b 2b 29 29 0a 23 20 64 65 66 69 6e 65 20 47  A++)).# define G
21212 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65 72 28  logUpperToLower(
21213 41 29 20 20 20 20 20 41 20 3d 20 73 71 6c 69 74  A)     A = sqlit
21214 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41  e3UpperToLower[A
21215 5d 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e 65  ].#else.# define
21216 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
21217 72 28 41 29 20 20 20 20 20 69 66 28 20 41 3c 30  r(A)     if( A<0
21218 78 38 30 20 29 7b 20 41 20 3d 20 73 71 6c 69 74  x80 ){ A = sqlit
21219 65 33 55 70 70 65 72 54 6f 4c 6f 77 65 72 5b 41  e3UpperToLower[A
2121a 5d 3b 20 7d 0a 23 65 6e 64 69 66 0a 0a 73 74 61  ]; }.#endif..sta
2121b 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75 63 74  tic const struct
2121c 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 67 6c 6f   compareInfo glo
2121d 62 49 6e 66 6f 20 3d 20 7b 20 27 2a 27 2c 20 27  bInfo = { '*', '
2121e 3f 27 2c 20 27 5b 27 2c 20 30 20 7d 3b 0a 2f 2a  ?', '[', 0 };./*
2121f 20 54 68 65 20 63 6f 72 72 65 63 74 20 53 51 4c   The correct SQL
21220 2d 39 32 20 62 65 68 61 76 69 6f 72 20 69 73 20  -92 behavior is 
21221 66 6f 72 20 74 68 65 20 4c 49 4b 45 20 6f 70 65  for the LIKE ope
21222 72 61 74 6f 72 20 74 6f 20 69 67 6e 6f 72 65 0a  rator to ignore.
21223 2a 2a 20 63 61 73 65 2e 20 20 54 68 75 73 20 20  ** case.  Thus  
21224 27 61 27 20 4c 49 4b 45 20 27 41 27 20 77 6f 75  'a' LIKE 'A' wou
21225 6c 64 20 62 65 20 74 72 75 65 2e 20 2a 2f 0a 73  ld be true. */.s
21226 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
21227 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20 6c  ct compareInfo l
21228 69 6b 65 49 6e 66 6f 4e 6f 72 6d 20 3d 20 7b 20  ikeInfoNorm = { 
21229 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20 31  '%', '_',   0, 1
2122a 20 7d 3b 0a 2f 2a 20 49 66 20 53 51 4c 49 54 45   };./* If SQLITE
2122b 5f 43 41 53 45 5f 53 45 4e 53 49 54 49 56 45 5f  _CASE_SENSITIVE_
2122c 4c 49 4b 45 20 69 73 20 64 65 66 69 6e 65 64 2c  LIKE is defined,
2122d 20 74 68 65 6e 20 74 68 65 20 4c 49 4b 45 20 6f   then the LIKE o
2122e 70 65 72 61 74 6f 72 0a 2a 2a 20 69 73 20 63 61  perator.** is ca
2122f 73 65 20 73 65 6e 73 69 74 69 76 65 20 63 61 75  se sensitive cau
21230 73 69 6e 67 20 27 61 27 20 4c 49 4b 45 20 27 41  sing 'a' LIKE 'A
21231 27 20 74 6f 20 62 65 20 66 61 6c 73 65 20 2a 2f  ' to be false */
21232 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74  .static const st
21233 72 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f  ruct compareInfo
21234 20 6c 69 6b 65 49 6e 66 6f 41 6c 74 20 3d 20 7b   likeInfoAlt = {
21235 20 27 25 27 2c 20 27 5f 27 2c 20 20 20 30 2c 20   '%', '_',   0, 
21236 30 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  0 };../*.** Comp
21237 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73 74  are two UTF-8 st
21238 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c 69  rings for equali
21239 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69 72  ty where the fir
2123a 73 74 20 73 74 72 69 6e 67 20 63 61 6e 0a 2a 2a  st string can.**
2123b 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 62 65 20   potentially be 
2123c 61 20 22 67 6c 6f 62 22 20 65 78 70 72 65 73 73  a "glob" express
2123d 69 6f 6e 2e 20 20 52 65 74 75 72 6e 20 74 72 75  ion.  Return tru
2123e 65 20 28 31 29 20 69 66 20 74 68 65 79 0a 2a 2a  e (1) if they.**
2123f 20 61 72 65 20 74 68 65 20 73 61 6d 65 20 61 6e   are the same an
21240 64 20 66 61 6c 73 65 20 28 30 29 20 69 66 20 74  d false (0) if t
21241 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65 6e  hey are differen
21242 74 2e 0a 2a 2a 0a 2a 2a 20 47 6c 6f 62 62 69 6e  t..**.** Globbin
21243 67 20 72 75 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 20  g rules:.**.**  
21244 20 20 20 20 27 2a 27 20 20 20 20 20 20 20 4d 61      '*'       Ma
21245 74 63 68 65 73 20 61 6e 79 20 73 65 71 75 65 6e  tches any sequen
21246 63 65 20 6f 66 20 7a 65 72 6f 20 6f 72 20 6d 6f  ce of zero or mo
21247 72 65 20 63 68 61 72 61 63 74 65 72 73 2e 0a 2a  re characters..*
21248 2a 0a 2a 2a 20 20 20 20 20 20 27 3f 27 20 20 20  *.**      '?'   
21249 20 20 20 20 4d 61 74 63 68 65 73 20 65 78 61 63      Matches exac
2124a 74 6c 79 20 6f 6e 65 20 63 68 61 72 61 63 74 65  tly one characte
2124b 72 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 5b 2e 2e  r..**.**     [..
2124c 2e 5d 20 20 20 20 20 20 4d 61 74 63 68 65 73 20  .]      Matches 
2124d 6f 6e 65 20 63 68 61 72 61 63 74 65 72 20 66 72  one character fr
2124e 6f 6d 20 74 68 65 20 65 6e 63 6c 6f 73 65 64 20  om the enclosed 
2124f 6c 69 73 74 20 6f 66 0a 2a 2a 20 20 20 20 20 20  list of.**      
21250 20 20 20 20 20 20 20 20 20 20 63 68 61 72 61 63            charac
21251 74 65 72 73 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ters..**.**     
21252 5b 5e 2e 2e 2e 5d 20 20 20 20 20 4d 61 74 63 68  [^...]     Match
21253 65 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  es one character
21254 20 6e 6f 74 20 69 6e 20 74 68 65 20 65 6e 63 6c   not in the encl
21255 6f 73 65 64 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a  osed list..**.**
21256 20 57 69 74 68 20 74 68 65 20 5b 2e 2e 2e 5d 20   With the [...] 
21257 61 6e 64 20 5b 5e 2e 2e 2e 5d 20 6d 61 74 63 68  and [^...] match
21258 69 6e 67 2c 20 61 20 27 5d 27 20 63 68 61 72 61  ing, a ']' chara
21259 63 74 65 72 20 63 61 6e 20 62 65 20 69 6e 63 6c  cter can be incl
2125a 75 64 65 64 0a 2a 2a 20 69 6e 20 74 68 65 20 6c  uded.** in the l
2125b 69 73 74 20 62 79 20 6d 61 6b 69 6e 67 20 69 74  ist by making it
2125c 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61   the first chara
2125d 63 74 65 72 20 61 66 74 65 72 20 27 5b 27 20 6f  cter after '[' o
2125e 72 20 27 5e 27 2e 20 20 41 0a 2a 2a 20 72 61 6e  r '^'.  A.** ran
2125f 67 65 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  ge of characters
21260 20 63 61 6e 20 62 65 20 73 70 65 63 69 66 69 65   can be specifie
21261 64 20 75 73 69 6e 67 20 27 2d 27 2e 20 20 45 78  d using '-'.  Ex
21262 61 6d 70 6c 65 3a 0a 2a 2a 20 22 5b 61 2d 7a 5d  ample:.** "[a-z]
21263 22 20 6d 61 74 63 68 65 73 20 61 6e 79 20 73 69  " matches any si
21264 6e 67 6c 65 20 6c 6f 77 65 72 2d 63 61 73 65 20  ngle lower-case 
21265 6c 65 74 74 65 72 2e 20 20 54 6f 20 6d 61 74 63  letter.  To matc
21266 68 20 61 20 27 2d 27 2c 20 6d 61 6b 65 0a 2a 2a  h a '-', make.**
21267 20 69 74 20 74 68 65 20 6c 61 73 74 20 63 68 61   it the last cha
21268 72 61 63 74 65 72 20 69 6e 20 74 68 65 20 6c 69  racter in the li
21269 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  st..**.** This r
2126a 6f 75 74 69 6e 65 20 69 73 20 75 73 75 61 6c 6c  outine is usuall
2126b 79 20 71 75 69 63 6b 2c 20 62 75 74 20 63 61 6e  y quick, but can
2126c 20 62 65 20 4e 2a 2a 32 20 69 6e 20 74 68 65 20   be N**2 in the 
2126d 77 6f 72 73 74 20 63 61 73 65 2e 0a 2a 2a 0a 2a  worst case..**.*
2126e 2a 20 48 69 6e 74 73 3a 20 74 6f 20 6d 61 74 63  * Hints: to matc
2126f 68 20 27 2a 27 20 6f 72 20 27 3f 27 2c 20 70 75  h '*' or '?', pu
21270 74 20 74 68 65 6d 20 69 6e 20 22 5b 5d 22 2e 20  t them in "[]". 
21271 20 4c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   Like this:.**.*
21272 2a 20 20 20 20 20 20 20 20 20 61 62 63 5b 2a 5d  *         abc[*]
21273 78 79 7a 20 20 20 20 20 20 20 20 4d 61 74 63 68  xyz        Match
21274 65 73 20 22 61 62 63 2a 78 79 7a 22 20 6f 6e 6c  es "abc*xyz" onl
21275 79 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  y.*/.static int 
21276 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28 0a  patternCompare(.
21277 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 50 61 74    const u8 *zPat
21278 74 65 72 6e 2c 20 20 20 20 20 20 20 20 20 20 20  tern,           
21279 20 20 20 2f 2a 20 54 68 65 20 67 6c 6f 62 20 70     /* The glob p
2127a 61 74 74 65 72 6e 20 2a 2f 0a 20 20 63 6f 6e 73  attern */.  cons
2127b 74 20 75 38 20 2a 7a 53 74 72 69 6e 67 2c 20 20  t u8 *zString,  
2127c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2127d 54 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f  The string to co
2127e 6d 70 61 72 65 20 61 67 61 69 6e 73 74 20 74 68  mpare against th
2127f 65 20 67 6c 6f 62 20 2a 2f 0a 20 20 63 6f 6e 73  e glob */.  cons
21280 74 20 73 74 72 75 63 74 20 63 6f 6d 70 61 72 65  t struct compare
21281 49 6e 66 6f 20 2a 70 49 6e 66 6f 2c 20 2f 2a 20  Info *pInfo, /* 
21282 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75  Information abou
21283 74 20 68 6f 77 20 74 6f 20 64 6f 20 74 68 65 20  t how to do the 
21284 63 6f 6d 70 61 72 65 20 2a 2f 0a 20 20 63 6f 6e  compare */.  con
21285 73 74 20 69 6e 74 20 65 73 63 20 20 20 20 20 20  st int esc      
21286 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21287 20 54 68 65 20 65 73 63 61 70 65 20 63 68 61 72   The escape char
21288 61 63 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 69 6e  acter */.){.  in
21289 74 20 63 2c 20 63 32 3b 0a 20 20 69 6e 74 20 69  t c, c2;.  int i
2128a 6e 76 65 72 74 3b 0a 20 20 69 6e 74 20 73 65 65  nvert;.  int see
2128b 6e 3b 0a 20 20 75 38 20 6d 61 74 63 68 4f 6e 65  n;.  u8 matchOne
2128c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68 4f   = pInfo->matchO
2128d 6e 65 3b 0a 20 20 75 38 20 6d 61 74 63 68 41 6c  ne;.  u8 matchAl
2128e 6c 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63 68  l = pInfo->match
2128f 41 6c 6c 3b 0a 20 20 75 38 20 6d 61 74 63 68 53  All;.  u8 matchS
21290 65 74 20 3d 20 70 49 6e 66 6f 2d 3e 6d 61 74 63  et = pInfo->matc
21291 68 53 65 74 3b 0a 20 20 75 38 20 6e 6f 43 61 73  hSet;.  u8 noCas
21292 65 20 3d 20 70 49 6e 66 6f 2d 3e 6e 6f 43 61 73  e = pInfo->noCas
21293 65 3b 20 0a 20 20 69 6e 74 20 70 72 65 76 45 73  e; .  int prevEs
21294 63 61 70 65 20 3d 20 30 3b 20 20 20 20 20 2f 2a  cape = 0;     /*
21295 20 54 72 75 65 20 69 66 20 74 68 65 20 70 72 65   True if the pre
21296 76 69 6f 75 73 20 63 68 61 72 61 63 74 65 72 20  vious character 
21297 77 61 73 20 27 65 73 63 61 70 65 27 20 2a 2f 0a  was 'escape' */.
21298 0a 20 20 77 68 69 6c 65 28 20 28 63 20 3d 20 73  .  while( (c = s
21299 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
2129a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50 61 74 74  Pattern,0,&zPatt
2129b 65 72 6e 29 29 21 3d 30 20 29 7b 0a 20 20 20 20  ern))!=0 ){.    
2129c 69 66 28 20 21 70 72 65 76 45 73 63 61 70 65 20  if( !prevEscape 
2129d 26 26 20 63 3d 3d 6d 61 74 63 68 41 6c 6c 20 29  && c==matchAll )
2129e 7b 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 28  {.      while( (
2129f 63 3d 73 71 6c 69 74 65 33 55 74 66 38 52 65 61  c=sqlite3Utf8Rea
212a0 64 28 7a 50 61 74 74 65 72 6e 2c 30 2c 26 7a 50  d(zPattern,0,&zP
212a1 61 74 74 65 72 6e 29 29 20 3d 3d 20 6d 61 74 63  attern)) == matc
212a2 68 41 6c 6c 0a 20 20 20 20 20 20 20 20 20 20 20  hAll.           
212a3 20 20 20 20 7c 7c 20 63 20 3d 3d 20 6d 61 74 63      || c == matc
212a4 68 4f 6e 65 20 29 7b 0a 20 20 20 20 20 20 20 20  hOne ){.        
212a5 69 66 28 20 63 3d 3d 6d 61 74 63 68 4f 6e 65 20  if( c==matchOne 
212a6 26 26 20 73 71 6c 69 74 65 33 55 74 66 38 52 65  && sqlite3Utf8Re
212a7 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26  ad(zString, 0, &
212a8 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29 7b 0a 20  zString)==0 ){. 
212a9 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20           return 
212aa 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  0;.        }.   
212ab 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63     }.      if( c
212ac 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72  ==0 ){.        r
212ad 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
212ae 65 6c 73 65 20 69 66 28 20 63 3d 3d 65 73 63 20  else if( c==esc 
212af 29 7b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73  ){.        c = s
212b0 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
212b1 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61  Pattern, 0, &zPa
212b2 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 20 20  ttern);.        
212b3 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20 20  if( c==0 ){.    
212b4 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
212b5 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
212b6 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
212b7 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 20  chSet ){.       
212b8 20 61 73 73 65 72 74 28 20 65 73 63 3d 3d 30 20   assert( esc==0 
212b9 29 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68  );         /* Th
212ba 69 73 20 69 73 20 47 4c 4f 42 2c 20 6e 6f 74 20  is is GLOB, not 
212bb 4c 49 4b 45 20 2a 2f 0a 20 20 20 20 20 20 20 20  LIKE */.        
212bc 61 73 73 65 72 74 28 20 6d 61 74 63 68 53 65 74  assert( matchSet
212bd 3c 30 78 38 30 20 29 3b 20 20 2f 2a 20 27 5b 27  <0x80 );  /* '['
212be 20 69 73 20 61 20 73 69 6e 67 6c 65 2d 62 79 74   is a single-byt
212bf 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f 0a 20  e character */. 
212c0 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a         while( *z
212c1 53 74 72 69 6e 67 20 26 26 20 70 61 74 74 65 72  String && patter
212c2 6e 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65  nCompare(&zPatte
212c3 72 6e 5b 2d 31 5d 2c 7a 53 74 72 69 6e 67 2c 70  rn[-1],zString,p
212c4 49 6e 66 6f 2c 65 73 63 29 3d 3d 30 20 29 7b 0a  Info,esc)==0 ){.
212c5 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
212c6 5f 53 4b 49 50 5f 55 54 46 38 28 7a 53 74 72 69  _SKIP_UTF8(zStri
212c7 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ng);.        }. 
212c8 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 2a 7a         return *z
212c9 53 74 72 69 6e 67 21 3d 30 3b 0a 20 20 20 20 20  String!=0;.     
212ca 20 7d 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20   }.      while( 
212cb 28 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74 66  (c2 = sqlite3Utf
212cc 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 30 2c  8Read(zString,0,
212cd 26 7a 53 74 72 69 6e 67 29 29 21 3d 30 20 29 7b  &zString))!=0 ){
212ce 0a 20 20 20 20 20 20 20 20 69 66 28 20 6e 6f 43  .        if( noC
212cf 61 73 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ase ){.         
212d0 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
212d1 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20  r(c2);.         
212d2 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
212d3 72 28 63 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r(c);.          
212d4 77 68 69 6c 65 28 20 63 32 20 21 3d 20 30 20 26  while( c2 != 0 &
212d5 26 20 63 32 20 21 3d 20 63 20 29 7b 0a 20 20 20  & c2 != c ){.   
212d6 20 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71           c2 = sq
212d7 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 53  lite3Utf8Read(zS
212d8 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53 74 72 69  tring, 0, &zStri
212d9 6e 67 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  ng);.           
212da 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f 77 65   GlogUpperToLowe
212db 72 28 63 32 29 3b 0a 20 20 20 20 20 20 20 20 20  r(c2);.         
212dc 20 7d 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65   }.        }else
212dd 7b 0a 20 20 20 20 20 20 20 20 20 20 77 68 69 6c  {.          whil
212de 65 28 20 63 32 20 21 3d 20 30 20 26 26 20 63 32  e( c2 != 0 && c2
212df 20 21 3d 20 63 20 29 7b 0a 20 20 20 20 20 20 20   != c ){.       
212e0 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69 74 65       c2 = sqlite
212e1 33 55 74 66 38 52 65 61 64 28 7a 53 74 72 69 6e  3Utf8Read(zStrin
212e2 67 2c 20 30 2c 20 26 7a 53 74 72 69 6e 67 29 3b  g, 0, &zString);
212e3 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
212e4 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
212e5 66 28 20 63 32 3d 3d 30 20 29 20 72 65 74 75 72  f( c2==0 ) retur
212e6 6e 20 30 3b 0a 20 20 20 20 20 20 20 20 69 66 28  n 0;.        if(
212e7 20 70 61 74 74 65 72 6e 43 6f 6d 70 61 72 65 28   patternCompare(
212e8 7a 50 61 74 74 65 72 6e 2c 7a 53 74 72 69 6e 67  zPattern,zString
212e9 2c 70 49 6e 66 6f 2c 65 73 63 29 20 29 20 72 65  ,pInfo,esc) ) re
212ea 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a  turn 1;.      }.
212eb 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a        return 0;.
212ec 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 21 70      }else if( !p
212ed 72 65 76 45 73 63 61 70 65 20 26 26 20 63 3d 3d  revEscape && c==
212ee 6d 61 74 63 68 4f 6e 65 20 29 7b 0a 20 20 20 20  matchOne ){.    
212ef 20 20 69 66 28 20 73 71 6c 69 74 65 33 55 74 66    if( sqlite3Utf
212f0 38 52 65 61 64 28 7a 53 74 72 69 6e 67 2c 20 30  8Read(zString, 0
212f1 2c 20 26 7a 53 74 72 69 6e 67 29 3d 3d 30 20 29  , &zString)==0 )
212f2 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
212f3 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   0;.      }.    
212f4 7d 65 6c 73 65 20 69 66 28 20 63 3d 3d 6d 61 74  }else if( c==mat
212f5 63 68 53 65 74 20 29 7b 0a 20 20 20 20 20 20 69  chSet ){.      i
212f6 6e 74 20 70 72 69 6f 72 5f 63 20 3d 20 30 3b 0a  nt prior_c = 0;.
212f7 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65 73        assert( es
212f8 63 3d 3d 30 20 29 3b 20 20 20 20 2f 2a 20 54 68  c==0 );    /* Th
212f9 69 73 20 6f 6e 6c 79 20 6f 63 63 75 72 73 20 66  is only occurs f
212fa 6f 72 20 47 4c 4f 42 2c 20 6e 6f 74 20 4c 49 4b  or GLOB, not LIK
212fb 45 20 2a 2f 0a 20 20 20 20 20 20 73 65 65 6e 20  E */.      seen 
212fc 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 76 65 72  = 0;.      inver
212fd 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 63 20 3d  t = 0;.      c =
212fe 20 73 71 6c 69 74 65 33 55 74 66 38 52 65 61 64   sqlite3Utf8Read
212ff 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26 7a 53  (zString, 0, &zS
21300 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 69 66  tring);.      if
21301 28 20 63 3d 3d 30 20 29 20 72 65 74 75 72 6e 20  ( c==0 ) return 
21302 30 3b 0a 20 20 20 20 20 20 63 32 20 3d 20 73 71  0;.      c2 = sq
21303 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50  lite3Utf8Read(zP
21304 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74  attern, 0, &zPat
21305 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 69 66 28  tern);.      if(
21306 20 63 32 3d 3d 27 5e 27 20 29 7b 0a 20 20 20 20   c2=='^' ){.    
21307 20 20 20 20 69 6e 76 65 72 74 20 3d 20 31 3b 0a      invert = 1;.
21308 20 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c          c2 = sql
21309 69 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61  ite3Utf8Read(zPa
2130a 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74  ttern, 0, &zPatt
2130b 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ern);.      }.  
2130c 20 20 20 20 69 66 28 20 63 32 3d 3d 27 5d 27 20      if( c2==']' 
2130d 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63  ){.        if( c
2130e 3d 3d 27 5d 27 20 29 20 73 65 65 6e 20 3d 20 31  ==']' ) seen = 1
2130f 3b 0a 20 20 20 20 20 20 20 20 63 32 20 3d 20 73  ;.        c2 = s
21310 71 6c 69 74 65 33 55 74 66 38 52 65 61 64 28 7a  qlite3Utf8Read(z
21311 50 61 74 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61  Pattern, 0, &zPa
21312 74 74 65 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a  ttern);.      }.
21313 20 20 20 20 20 20 77 68 69 6c 65 28 20 63 32 20        while( c2 
21314 26 26 20 63 32 21 3d 27 5d 27 20 29 7b 0a 20 20  && c2!=']' ){.  
21315 20 20 20 20 20 20 69 66 28 20 63 32 3d 3d 27 2d        if( c2=='-
21316 27 20 26 26 20 7a 50 61 74 74 65 72 6e 5b 30 5d  ' && zPattern[0]
21317 21 3d 27 5d 27 20 26 26 20 7a 50 61 74 74 65 72  !=']' && zPatter
21318 6e 5b 30 5d 21 3d 30 20 26 26 20 70 72 69 6f 72  n[0]!=0 && prior
21319 5f 63 3e 30 20 29 7b 0a 20 20 20 20 20 20 20 20  _c>0 ){.        
2131a 20 20 63 32 20 3d 20 73 71 6c 69 74 65 33 55 74    c2 = sqlite3Ut
2131b 66 38 52 65 61 64 28 7a 50 61 74 74 65 72 6e 2c  f8Read(zPattern,
2131c 20 30 2c 20 26 7a 50 61 74 74 65 72 6e 29 3b 0a   0, &zPattern);.
2131d 20 20 20 20 20 20 20 20 20 20 69 66 28 20 63 3e            if( c>
2131e 3d 70 72 69 6f 72 5f 63 20 26 26 20 63 3c 3d 63  =prior_c && c<=c
2131f 32 20 29 20 73 65 65 6e 20 3d 20 31 3b 0a 20 20  2 ) seen = 1;.  
21320 20 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20          prior_c 
21321 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  = 0;.        }el
21322 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66  se{.          if
21323 28 20 63 3d 3d 63 32 20 29 7b 0a 20 20 20 20 20  ( c==c2 ){.     
21324 20 20 20 20 20 20 20 73 65 65 6e 20 3d 20 31 3b         seen = 1;
21325 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
21326 20 20 20 20 20 20 20 70 72 69 6f 72 5f 63 20 3d         prior_c =
21327 20 63 32 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20   c2;.        }. 
21328 20 20 20 20 20 20 20 63 32 20 3d 20 73 71 6c 69         c2 = sqli
21329 74 65 33 55 74 66 38 52 65 61 64 28 7a 50 61 74  te3Utf8Read(zPat
2132a 74 65 72 6e 2c 20 30 2c 20 26 7a 50 61 74 74 65  tern, 0, &zPatte
2132b 72 6e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  rn);.      }.   
2132c 20 20 20 69 66 28 20 63 32 3d 3d 30 20 7c 7c 20     if( c2==0 || 
2132d 28 73 65 65 6e 20 5e 20 69 6e 76 65 72 74 29 3d  (seen ^ invert)=
2132e 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  =0 ){.        re
2132f 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a  turn 0;.      }.
21330 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 65 73      }else if( es
21331 63 3d 3d 63 20 26 26 20 21 70 72 65 76 45 73 63  c==c && !prevEsc
21332 61 70 65 20 29 7b 0a 20 20 20 20 20 20 70 72 65  ape ){.      pre
21333 76 45 73 63 61 70 65 20 3d 20 31 3b 0a 20 20 20  vEscape = 1;.   
21334 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 63 32   }else{.      c2
21335 20 3d 20 73 71 6c 69 74 65 33 55 74 66 38 52 65   = sqlite3Utf8Re
21336 61 64 28 7a 53 74 72 69 6e 67 2c 20 30 2c 20 26  ad(zString, 0, &
21337 7a 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20  zString);.      
21338 69 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20  if( noCase ){.  
21339 20 20 20 20 20 20 47 6c 6f 67 55 70 70 65 72 54        GlogUpperT
2133a 6f 4c 6f 77 65 72 28 63 29 3b 0a 20 20 20 20 20  oLower(c);.     
2133b 20 20 20 47 6c 6f 67 55 70 70 65 72 54 6f 4c 6f     GlogUpperToLo
2133c 77 65 72 28 63 32 29 3b 0a 20 20 20 20 20 20 7d  wer(c2);.      }
2133d 0a 20 20 20 20 20 20 69 66 28 20 63 21 3d 63 32  .      if( c!=c2
2133e 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
2133f 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
21340 20 20 20 20 70 72 65 76 45 73 63 61 70 65 20 3d      prevEscape =
21341 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
21342 72 65 74 75 72 6e 20 2a 7a 53 74 72 69 6e 67 3d  return *zString=
21343 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 75  =0;.}../*.** Cou
21344 6e 74 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  nt the number of
21345 20 74 69 6d 65 73 20 74 68 61 74 20 74 68 65 20   times that the 
21346 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20 28 6f  LIKE operator (o
21347 72 20 47 4c 4f 42 20 77 68 69 63 68 20 69 73 0a  r GLOB which is.
21348 2a 2a 20 6a 75 73 74 20 61 20 76 61 72 69 61 74  ** just a variat
21349 69 6f 6e 20 6f 66 20 4c 49 4b 45 29 20 67 65 74  ion of LIKE) get
2134a 73 20 63 61 6c 6c 65 64 2e 20 20 54 68 69 73 20  s called.  This 
2134b 69 73 20 75 73 65 64 20 66 6f 72 20 74 65 73 74  is used for test
2134c 69 6e 67 0a 2a 2a 20 6f 6e 6c 79 2e 0a 2a 2f 0a  ing.** only..*/.
2134d 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45  #ifdef SQLITE_TE
2134e 53 54 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  ST.SQLITE_API in
2134f 74 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65 5f 63  t sqlite3_like_c
21350 6f 75 6e 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ount = 0;.#endif
21351 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  .../*.** Impleme
21352 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6c  ntation of the l
21353 69 6b 65 28 29 20 53 51 4c 20 66 75 6e 63 74 69  ike() SQL functi
21354 6f 6e 2e 20 20 54 68 69 73 20 66 75 6e 63 74 69  on.  This functi
21355 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a  on implements.**
21356 20 74 68 65 20 62 75 69 6c 64 2d 69 6e 20 4c 49   the build-in LI
21357 4b 45 20 6f 70 65 72 61 74 6f 72 2e 20 20 54 68  KE operator.  Th
21358 65 20 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74  e first argument
21359 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   to the function
2135a 20 69 73 20 74 68 65 0a 2a 2a 20 70 61 74 74 65   is the.** patte
2135b 72 6e 20 61 6e 64 20 74 68 65 20 73 65 63 6f 6e  rn and the secon
2135c 64 20 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68  d argument is th
2135d 65 20 73 74 72 69 6e 67 2e 20 20 53 6f 2c 20 74  e string.  So, t
2135e 68 65 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74  he SQL statement
2135f 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 41  s:.**.**       A
21360 20 4c 49 4b 45 20 42 0a 2a 2a 0a 2a 2a 20 69 73   LIKE B.**.** is
21361 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 61 73 20   implemented as 
21362 6c 69 6b 65 28 42 2c 41 29 2e 0a 2a 2a 0a 2a 2a  like(B,A)..**.**
21363 20 54 68 69 73 20 73 61 6d 65 20 66 75 6e 63 74   This same funct
21364 69 6f 6e 20 28 77 69 74 68 20 61 20 64 69 66 66  ion (with a diff
21365 65 72 65 6e 74 20 63 6f 6d 70 61 72 65 49 6e 66  erent compareInf
21366 6f 20 73 74 72 75 63 74 75 72 65 29 20 63 6f 6d  o structure) com
21367 70 75 74 65 73 0a 2a 2a 20 74 68 65 20 47 4c 4f  putes.** the GLO
21368 42 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2f 0a 73  B operator..*/.s
21369 74 61 74 69 63 20 76 6f 69 64 20 6c 69 6b 65 46  tatic void likeF
2136a 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63  unc(.  sqlite3_c
2136b 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c  ontext *context,
2136c 20 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 0a 20   .  int argc, . 
2136d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
2136e 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
2136f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
21370 7a 41 2c 20 2a 7a 42 3b 0a 20 20 69 6e 74 20 65  zA, *zB;.  int e
21371 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 73 71 6c  scape = 0;.  sql
21372 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74  ite3 *db = sqlit
21373 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61  e3_context_db_ha
21374 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 0a  ndle(context);..
21375 20 20 7a 42 20 3d 20 73 71 6c 69 74 65 33 5f 76    zB = sqlite3_v
21376 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
21377 5d 29 3b 0a 20 20 7a 41 20 3d 20 73 71 6c 69 74  ]);.  zA = sqlit
21378 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
21379 67 76 5b 31 5d 29 3b 0a 0a 20 20 2f 2a 20 4c 69  gv[1]);..  /* Li
2137a 6d 69 74 20 74 68 65 20 6c 65 6e 67 74 68 20 6f  mit the length o
2137b 66 20 74 68 65 20 4c 49 4b 45 20 6f 72 20 47 4c  f the LIKE or GL
2137c 4f 42 20 70 61 74 74 65 72 6e 20 74 6f 20 61 76  OB pattern to av
2137d 6f 69 64 20 70 72 6f 62 6c 65 6d 73 0a 20 20 2a  oid problems.  *
2137e 2a 20 6f 66 20 64 65 65 70 20 72 65 63 75 72 73  * of deep recurs
2137f 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20 62 65 68 61  ion and N*N beha
21380 76 69 6f 72 20 69 6e 20 70 61 74 74 65 72 6e 43  vior in patternC
21381 6f 6d 70 61 72 65 28 29 2e 0a 20 20 2a 2f 0a 20  ompare()..  */. 
21382 20 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c   if( sqlite3_val
21383 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d  ue_bytes(argv[0]
21384 29 20 3e 0a 20 20 20 20 20 20 20 20 64 62 2d 3e  ) >.        db->
21385 61 4c 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49  aLimit[SQLITE_LI
21386 4d 49 54 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e  MIT_LIKE_PATTERN
21387 5f 4c 45 4e 47 54 48 5d 20 29 7b 0a 20 20 20 20  _LENGTH] ){.    
21388 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
21389 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 22 4c  rror(context, "L
2138a 49 4b 45 20 6f 72 20 47 4c 4f 42 20 70 61 74 74  IKE or GLOB patt
2138b 65 72 6e 20 74 6f 6f 20 63 6f 6d 70 6c 65 78 22  ern too complex"
2138c 2c 20 2d 31 29 3b 0a 20 20 20 20 72 65 74 75 72  , -1);.    retur
2138d 6e 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28  n;.  }.  assert(
2138e 20 7a 42 3d 3d 73 71 6c 69 74 65 33 5f 76 61 6c   zB==sqlite3_val
2138f 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29  ue_text(argv[0])
21390 20 29 3b 20 20 2f 2a 20 45 6e 63 6f 64 69 6e 67   );  /* Encoding
21391 20 64 69 64 20 6e 6f 74 20 63 68 61 6e 67 65 20   did not change 
21392 2a 2f 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d  */..  if( argc==
21393 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20  3 ){.    /* The 
21394 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65 72  escape character
21395 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f 6e   string must con
21396 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c 65  sist of a single
21397 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65 72   UTF-8 character
21398 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77 69  ..    ** Otherwi
21399 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65 72  se, return an er
2139a 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ror..    */.    
2139b 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2139c 68 61 72 20 2a 7a 45 73 63 20 3d 20 73 71 6c 69  har *zEsc = sqli
2139d 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61  te3_value_text(a
2139e 72 67 76 5b 32 5d 29 3b 0a 20 20 20 20 69 66 28  rgv[2]);.    if(
2139f 20 7a 45 73 63 3d 3d 30 20 29 20 72 65 74 75 72   zEsc==0 ) retur
213a0 6e 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74  n;.    if( sqlit
213a1 65 33 55 74 66 38 43 68 61 72 4c 65 6e 28 28 63  e3Utf8CharLen((c
213a2 68 61 72 2a 29 7a 45 73 63 2c 20 2d 31 29 21 3d  har*)zEsc, -1)!=
213a3 31 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  1 ){.      sqlit
213a4 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
213a5 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20 20 20  context, .      
213a6 20 20 20 20 22 45 53 43 41 50 45 20 65 78 70 72      "ESCAPE expr
213a7 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65 20 61  ession must be a
213a8 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63 74 65   single characte
213a9 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20 20 72  r", -1);.      r
213aa 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 20  eturn;.    }.   
213ab 20 65 73 63 61 70 65 20 3d 20 73 71 6c 69 74 65   escape = sqlite
213ac 33 55 74 66 38 52 65 61 64 28 7a 45 73 63 2c 20  3Utf8Read(zEsc, 
213ad 30 2c 20 26 7a 45 73 63 29 3b 0a 20 20 7d 0a 20  0, &zEsc);.  }. 
213ae 20 69 66 28 20 7a 41 20 26 26 20 7a 42 20 29 7b   if( zA && zB ){
213af 0a 20 20 20 20 73 74 72 75 63 74 20 63 6f 6d 70  .    struct comp
213b0 61 72 65 49 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d  areInfo *pInfo =
213b1 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61   sqlite3_user_da
213b2 74 61 28 63 6f 6e 74 65 78 74 29 3b 0a 23 69 66  ta(context);.#if
213b3 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54 0a  def SQLITE_TEST.
213b4 20 20 20 20 73 71 6c 69 74 65 33 5f 6c 69 6b 65      sqlite3_like
213b5 5f 63 6f 75 6e 74 2b 2b 3b 0a 23 65 6e 64 69 66  _count++;.#endif
213b6 0a 20 20 20 20 0a 20 20 20 20 73 71 6c 69 74 65  .    .    sqlite
213b7 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e  3_result_int(con
213b8 74 65 78 74 2c 20 70 61 74 74 65 72 6e 43 6f 6d  text, patternCom
213b9 70 61 72 65 28 7a 42 2c 20 7a 41 2c 20 70 49 6e  pare(zB, zA, pIn
213ba 66 6f 2c 20 65 73 63 61 70 65 29 29 3b 0a 20 20  fo, escape));.  
213bb 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  }.}../*.** Imple
213bc 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
213bd 20 4e 55 4c 4c 49 46 28 78 2c 79 29 20 66 75 6e   NULLIF(x,y) fun
213be 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73 75  ction.  The resu
213bf 6c 74 20 69 73 20 74 68 65 20 66 69 72 73 74 0a  lt is the first.
213c0 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 66 20 74  ** argument if t
213c1 68 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65  he arguments are
213c2 20 64 69 66 66 65 72 65 6e 74 2e 20 20 54 68 65   different.  The
213c3 20 72 65 73 75 6c 74 20 69 73 20 4e 55 4c 4c 20   result is NULL 
213c4 69 66 20 74 68 65 0a 2a 2a 20 61 72 67 75 6d 65  if the.** argume
213c5 6e 74 73 20 61 72 65 20 65 71 75 61 6c 20 74 6f  nts are equal to
213c6 20 65 61 63 68 20 6f 74 68 65 72 2e 0a 2a 2f 0a   each other..*/.
213c7 73 74 61 74 69 63 20 76 6f 69 64 20 6e 75 6c 6c  static void null
213c8 69 66 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  ifFunc(.  sqlite
213c9 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
213ca 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73 65  xt,.  int NotUse
213cb 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  d,.  sqlite3_val
213cc 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 43  ue **argv.){.  C
213cd 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
213ce 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f  sqlite3GetFuncCo
213cf 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a  llSeq(context);.
213d0 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54    UNUSED_PARAMET
213d1 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20 69  ER(NotUsed);.  i
213d2 66 28 20 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d  f( sqlite3MemCom
213d3 70 61 72 65 28 61 72 67 76 5b 30 5d 2c 20 61 72  pare(argv[0], ar
213d4 67 76 5b 31 5d 2c 20 70 43 6f 6c 6c 29 21 3d 30  gv[1], pColl)!=0
213d5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
213d6 72 65 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e  result_value(con
213d7 74 65 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a  text, argv[0]);.
213d8 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70    }.}../*.** Imp
213d9 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74  lementation of t
213da 68 65 20 56 45 52 53 49 4f 4e 28 2a 29 20 66 75  he VERSION(*) fu
213db 6e 63 74 69 6f 6e 2e 20 20 54 68 65 20 72 65 73  nction.  The res
213dc 75 6c 74 20 69 73 20 74 68 65 20 76 65 72 73 69  ult is the versi
213dd 6f 6e 0a 2a 2a 20 6f 66 20 74 68 65 20 53 51 4c  on.** of the SQL
213de 69 74 65 20 6c 69 62 72 61 72 79 20 74 68 61 74  ite library that
213df 20 69 73 20 72 75 6e 6e 69 6e 67 2e 0a 2a 2f 0a   is running..*/.
213e0 73 74 61 74 69 63 20 76 6f 69 64 20 76 65 72 73  static void vers
213e1 69 6f 6e 46 75 6e 63 28 0a 20 20 73 71 6c 69 74  ionFunc(.  sqlit
213e2 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
213e3 65 78 74 2c 0a 20 20 69 6e 74 20 4e 6f 74 55 73  ext,.  int NotUs
213e4 65 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  ed,.  sqlite3_va
213e5 6c 75 65 20 2a 2a 4e 6f 74 55 73 65 64 32 0a 29  lue **NotUsed2.)
213e6 7b 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d  {.  UNUSED_PARAM
213e7 45 54 45 52 32 28 4e 6f 74 55 73 65 64 2c 20 4e  ETER2(NotUsed, N
213e8 6f 74 55 73 65 64 32 29 3b 0a 20 20 73 71 6c 69  otUsed2);.  sqli
213e9 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
213ea 63 6f 6e 74 65 78 74 2c 20 73 71 6c 69 74 65 33  context, sqlite3
213eb 5f 76 65 72 73 69 6f 6e 2c 20 2d 31 2c 20 53 51  _version, -1, SQ
213ec 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 7d 0a  LITE_STATIC);.}.
213ed 0a 2f 2a 20 41 72 72 61 79 20 66 6f 72 20 63 6f  ./* Array for co
213ee 6e 76 65 72 74 69 6e 67 20 66 72 6f 6d 20 68 61  nverting from ha
213ef 6c 66 2d 62 79 74 65 73 20 28 6e 79 62 62 6c 65  lf-bytes (nybble
213f0 73 29 20 69 6e 74 6f 20 41 53 43 49 49 20 68 65  s) into ASCII he
213f1 78 0a 2a 2a 20 64 69 67 69 74 73 2e 20 2a 2f 0a  x.** digits. */.
213f2 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
213f3 72 20 68 65 78 64 69 67 69 74 73 5b 5d 20 3d 20  r hexdigits[] = 
213f4 7b 0a 20 20 27 30 27 2c 20 27 31 27 2c 20 27 32  {.  '0', '1', '2
213f5 27 2c 20 27 33 27 2c 20 27 34 27 2c 20 27 35 27  ', '3', '4', '5'
213f6 2c 20 27 36 27 2c 20 27 37 27 2c 0a 20 20 27 38  , '6', '7',.  '8
213f7 27 2c 20 27 39 27 2c 20 27 41 27 2c 20 27 42 27  ', '9', 'A', 'B'
213f8 2c 20 27 43 27 2c 20 27 44 27 2c 20 27 45 27 2c  , 'C', 'D', 'E',
213f9 20 27 46 27 20 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   'F' .};../*.** 
213fa 45 58 50 45 52 49 4d 45 4e 54 41 4c 20 2d 20 54  EXPERIMENTAL - T
213fb 68 69 73 20 69 73 20 6e 6f 74 20 61 6e 20 6f 66  his is not an of
213fc 66 69 63 69 61 6c 20 66 75 6e 63 74 69 6f 6e 2e  ficial function.
213fd 20 20 54 68 65 20 69 6e 74 65 72 66 61 63 65 20    The interface 
213fe 6d 61 79 0a 2a 2a 20 63 68 61 6e 67 65 2e 20 20  may.** change.  
213ff 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d 61  This function ma
21400 79 20 64 69 73 61 70 70 65 61 72 2e 20 20 44 6f  y disappear.  Do
21401 20 6e 6f 74 20 77 72 69 74 65 20 63 6f 64 65 20   not write code 
21402 74 68 61 74 20 64 65 70 65 6e 64 73 0a 2a 2a 20  that depends.** 
21403 6f 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e  on this function
21404 2e 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ..**.** Implemen
21405 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 51 55  tation of the QU
21406 4f 54 45 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20  OTE() function. 
21407 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 74   This function t
21408 61 6b 65 73 20 61 20 73 69 6e 67 6c 65 0a 2a 2a  akes a single.**
21409 20 61 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74   argument.  If t
2140a 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73 20 6e  he argument is n
2140b 75 6d 65 72 69 63 2c 20 74 68 65 20 72 65 74 75  umeric, the retu
2140c 72 6e 20 76 61 6c 75 65 20 69 73 20 74 68 65 20  rn value is the 
2140d 73 61 6d 65 20 61 73 0a 2a 2a 20 74 68 65 20 61  same as.** the a
2140e 72 67 75 6d 65 6e 74 2e 20 20 49 66 20 74 68 65  rgument.  If the
2140f 20 61 72 67 75 6d 65 6e 74 20 69 73 20 4e 55 4c   argument is NUL
21410 4c 2c 20 74 68 65 20 72 65 74 75 72 6e 20 76 61  L, the return va
21411 6c 75 65 20 69 73 20 74 68 65 20 73 74 72 69 6e  lue is the strin
21412 67 0a 2a 2a 20 22 4e 55 4c 4c 22 2e 20 20 4f 74  g.** "NULL".  Ot
21413 68 65 72 77 69 73 65 2c 20 74 68 65 20 61 72 67  herwise, the arg
21414 75 6d 65 6e 74 20 69 73 20 65 6e 63 6c 6f 73 65  ument is enclose
21415 64 20 69 6e 20 73 69 6e 67 6c 65 20 71 75 6f 74  d in single quot
21416 65 73 20 77 69 74 68 0a 2a 2a 20 73 69 6e 67 6c  es with.** singl
21417 65 2d 71 75 6f 74 65 20 65 73 63 61 70 65 73 2e  e-quote escapes.
21418 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
21419 71 75 6f 74 65 46 75 6e 63 28 73 71 6c 69 74 65  quoteFunc(sqlite
2141a 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
2141b 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71  xt, int argc, sq
2141c 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
2141d 67 76 29 7b 0a 20 20 69 66 28 20 61 72 67 63 3c  gv){.  if( argc<
2141e 31 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 73 77  1 ) return;.  sw
2141f 69 74 63 68 28 20 73 71 6c 69 74 65 33 5f 76 61  itch( sqlite3_va
21420 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  lue_type(argv[0]
21421 29 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  ) ){.    case SQ
21422 4c 49 54 45 5f 4e 55 4c 4c 3a 20 7b 0a 20 20 20  LITE_NULL: {.   
21423 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
21424 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  t_text(context, 
21425 22 4e 55 4c 4c 22 2c 20 34 2c 20 53 51 4c 49 54  "NULL", 4, SQLIT
21426 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
21427 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20   break;.    }.  
21428 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 49 4e    case SQLITE_IN
21429 54 45 47 45 52 3a 0a 20 20 20 20 63 61 73 65 20  TEGER:.    case 
2142a 53 51 4c 49 54 45 5f 46 4c 4f 41 54 3a 20 7b 0a  SQLITE_FLOAT: {.
2142b 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65        sqlite3_re
2142c 73 75 6c 74 5f 76 61 6c 75 65 28 63 6f 6e 74 65  sult_value(conte
2142d 78 74 2c 20 61 72 67 76 5b 30 5d 29 3b 0a 20 20  xt, argv[0]);.  
2142e 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2142f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
21430 5f 42 4c 4f 42 3a 20 7b 0a 20 20 20 20 20 20 63  _BLOB: {.      c
21431 68 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a  har *zText = 0;.
21432 20 20 20 20 20 20 63 68 61 72 20 63 6f 6e 73 74        char const
21433 20 2a 7a 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65   *zBlob = sqlite
21434 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
21435 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 69 6e 74  v[0]);.      int
21436 20 6e 42 6c 6f 62 20 3d 20 73 71 6c 69 74 65 33   nBlob = sqlite3
21437 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
21438 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 61 73 73  v[0]);.      ass
21439 65 72 74 28 20 7a 42 6c 6f 62 3d 3d 73 71 6c 69  ert( zBlob==sqli
2143a 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61  te3_value_blob(a
2143b 72 67 76 5b 30 5d 29 20 29 3b 20 2f 2a 20 4e 6f  rgv[0]) ); /* No
2143c 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65   encoding change
2143d 20 2a 2f 0a 20 20 20 20 20 20 7a 54 65 78 74 20   */.      zText 
2143e 3d 20 28 63 68 61 72 20 2a 29 63 6f 6e 74 65 78  = (char *)contex
2143f 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c  tMalloc(context,
21440 20 28 32 2a 28 69 36 34 29 6e 42 6c 6f 62 29 2b   (2*(i64)nBlob)+
21441 34 29 3b 20 0a 20 20 20 20 20 20 69 66 28 20 7a  4); .      if( z
21442 54 65 78 74 20 29 7b 0a 20 20 20 20 20 20 20 20  Text ){.        
21443 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 20 20 66  int i;.        f
21444 6f 72 28 69 3d 30 3b 20 69 3c 6e 42 6c 6f 62 3b  or(i=0; i<nBlob;
21445 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   i++){.         
21446 20 7a 54 65 78 74 5b 28 69 2a 32 29 2b 32 5d 20   zText[(i*2)+2] 
21447 3d 20 68 65 78 64 69 67 69 74 73 5b 28 7a 42 6c  = hexdigits[(zBl
21448 6f 62 5b 69 5d 3e 3e 34 29 26 30 78 30 46 5d 3b  ob[i]>>4)&0x0F];
21449 0a 20 20 20 20 20 20 20 20 20 20 7a 54 65 78 74  .          zText
2144a 5b 28 69 2a 32 29 2b 33 5d 20 3d 20 68 65 78 64  [(i*2)+3] = hexd
2144b 69 67 69 74 73 5b 28 7a 42 6c 6f 62 5b 69 5d 29  igits[(zBlob[i])
2144c 26 30 78 30 46 5d 3b 0a 20 20 20 20 20 20 20 20  &0x0F];.        
2144d 7d 0a 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b  }.        zText[
2144e 28 6e 42 6c 6f 62 2a 32 29 2b 32 5d 20 3d 20 27  (nBlob*2)+2] = '
2144f 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 54 65  \'';.        zTe
21450 78 74 5b 28 6e 42 6c 6f 62 2a 32 29 2b 33 5d 20  xt[(nBlob*2)+3] 
21451 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20 20 20 20  = '\0';.        
21452 7a 54 65 78 74 5b 30 5d 20 3d 20 27 58 27 3b 0a  zText[0] = 'X';.
21453 20 20 20 20 20 20 20 20 7a 54 65 78 74 5b 31 5d          zText[1]
21454 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20   = '\'';.       
21455 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
21456 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 7a 54  text(context, zT
21457 65 78 74 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f  ext, -1, SQLITE_
21458 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
21459 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
2145a 28 7a 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7d  (zText);.      }
2145b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2145c 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c    }.    case SQL
2145d 49 54 45 5f 54 45 58 54 3a 20 7b 0a 20 20 20 20  ITE_TEXT: {.    
2145e 20 20 69 6e 74 20 69 2c 6a 3b 0a 20 20 20 20 20    int i,j;.     
2145f 20 75 36 34 20 6e 3b 0a 20 20 20 20 20 20 63 6f   u64 n;.      co
21460 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61  nst unsigned cha
21461 72 20 2a 7a 41 72 67 20 3d 20 73 71 6c 69 74 65  r *zArg = sqlite
21462 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67  3_value_text(arg
21463 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 63 68 61  v[0]);.      cha
21464 72 20 2a 7a 3b 0a 0a 20 20 20 20 20 20 69 66 28  r *z;..      if(
21465 20 7a 41 72 67 3d 3d 30 20 29 20 72 65 74 75 72   zArg==0 ) retur
21466 6e 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30  n;.      for(i=0
21467 2c 20 6e 3d 30 3b 20 7a 41 72 67 5b 69 5d 3b 20  , n=0; zArg[i]; 
21468 69 2b 2b 29 7b 20 69 66 28 20 7a 41 72 67 5b 69  i++){ if( zArg[i
21469 5d 3d 3d 27 5c 27 27 20 29 20 6e 2b 2b 3b 20 7d  ]=='\'' ) n++; }
2146a 0a 20 20 20 20 20 20 7a 20 3d 20 63 6f 6e 74 65  .      z = conte
2146b 78 74 4d 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74  xtMalloc(context
2146c 2c 20 28 28 69 36 34 29 69 29 2b 28 28 69 36 34  , ((i64)i)+((i64
2146d 29 6e 29 2b 33 29 3b 0a 20 20 20 20 20 20 69 66  )n)+3);.      if
2146e 28 20 7a 20 29 7b 0a 20 20 20 20 20 20 20 20 7a  ( z ){.        z
2146f 5b 30 5d 20 3d 20 27 5c 27 27 3b 0a 20 20 20 20  [0] = '\'';.    
21470 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 31      for(i=0, j=1
21471 3b 20 7a 41 72 67 5b 69 5d 3b 20 69 2b 2b 29 7b  ; zArg[i]; i++){
21472 0a 20 20 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b  .          z[j++
21473 5d 20 3d 20 7a 41 72 67 5b 69 5d 3b 0a 20 20 20  ] = zArg[i];.   
21474 20 20 20 20 20 20 20 69 66 28 20 7a 41 72 67 5b         if( zArg[
21475 69 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20  i]=='\'' ){.    
21476 20 20 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d          z[j++] =
21477 20 27 5c 27 27 3b 0a 20 20 20 20 20 20 20 20 20   '\'';.         
21478 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
21479 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 27 5c       z[j++] = '\
2147a 27 27 3b 0a 20 20 20 20 20 20 20 20 7a 5b 6a 5d  '';.        z[j]
2147b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 73 71   = 0;.        sq
2147c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
2147d 74 28 63 6f 6e 74 65 78 74 2c 20 7a 2c 20 6a 2c  t(context, z, j,
2147e 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
2147f 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
21480 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 68  }.}../*.** The h
21481 65 78 28 29 20 66 75 6e 63 74 69 6f 6e 2e 20 20  ex() function.  
21482 49 6e 74 65 72 70 72 65 74 20 74 68 65 20 61 72  Interpret the ar
21483 67 75 6d 65 6e 74 20 61 73 20 61 20 62 6c 6f 62  gument as a blob
21484 2e 20 20 52 65 74 75 72 6e 0a 2a 2a 20 61 20 68  .  Return.** a h
21485 65 78 61 64 65 63 69 6d 61 6c 20 72 65 6e 64 65  exadecimal rende
21486 72 69 6e 67 20 61 73 20 74 65 78 74 2e 0a 2a 2f  ring as text..*/
21487 0a 73 74 61 74 69 63 20 76 6f 69 64 20 68 65 78  .static void hex
21488 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  Func(.  sqlite3_
21489 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2148a 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
2148b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2148c 61 72 67 76 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  argv.){.  int i,
2148d 20 6e 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69   n;.  const unsi
2148e 67 6e 65 64 20 63 68 61 72 20 2a 70 42 6c 6f 62  gned char *pBlob
2148f 3b 0a 20 20 63 68 61 72 20 2a 7a 48 65 78 2c 20  ;.  char *zHex, 
21490 2a 7a 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72  *z;.  assert( ar
21491 67 63 3d 3d 31 20 29 3b 0a 20 20 55 4e 55 53 45  gc==1 );.  UNUSE
21492 44 5f 50 41 52 41 4d 45 54 45 52 28 61 72 67 63  D_PARAMETER(argc
21493 29 3b 0a 20 20 70 42 6c 6f 62 20 3d 20 73 71 6c  );.  pBlob = sql
21494 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28  ite3_value_blob(
21495 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 20 3d 20  argv[0]);.  n = 
21496 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
21497 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
21498 61 73 73 65 72 74 28 20 70 42 6c 6f 62 3d 3d 73  assert( pBlob==s
21499 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
2149a 62 28 61 72 67 76 5b 30 5d 29 20 29 3b 20 20 2f  b(argv[0]) );  /
2149b 2a 20 4e 6f 20 65 6e 63 6f 64 69 6e 67 20 63 68  * No encoding ch
2149c 61 6e 67 65 20 2a 2f 0a 20 20 7a 20 3d 20 7a 48  ange */.  z = zH
2149d 65 78 20 3d 20 63 6f 6e 74 65 78 74 4d 61 6c 6c  ex = contextMall
2149e 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28 28 69 36  oc(context, ((i6
2149f 34 29 6e 29 2a 32 20 2b 20 31 29 3b 0a 20 20 69  4)n)*2 + 1);.  i
214a0 66 28 20 7a 48 65 78 20 29 7b 0a 20 20 20 20 66  f( zHex ){.    f
214a1 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69 2b 2b  or(i=0; i<n; i++
214a2 2c 20 70 42 6c 6f 62 2b 2b 29 7b 0a 20 20 20 20  , pBlob++){.    
214a3 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20    unsigned char 
214a4 63 20 3d 20 2a 70 42 6c 6f 62 3b 0a 20 20 20 20  c = *pBlob;.    
214a5 20 20 2a 28 7a 2b 2b 29 20 3d 20 68 65 78 64 69    *(z++) = hexdi
214a6 67 69 74 73 5b 28 63 3e 3e 34 29 26 30 78 66 5d  gits[(c>>4)&0xf]
214a7 3b 0a 20 20 20 20 20 20 2a 28 7a 2b 2b 29 20 3d  ;.      *(z++) =
214a8 20 68 65 78 64 69 67 69 74 73 5b 63 26 30 78 66   hexdigits[c&0xf
214a9 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 7a 20  ];.    }.    *z 
214aa 3d 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  = 0;.    sqlite3
214ab 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e  _result_text(con
214ac 74 65 78 74 2c 20 7a 48 65 78 2c 20 6e 2a 32 2c  text, zHex, n*2,
214ad 20 73 71 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a   sqlite3_free);.
214ae 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65    }.}../*.** The
214af 20 7a 65 72 6f 62 6c 6f 62 28 4e 29 20 66 75 6e   zeroblob(N) fun
214b0 63 74 69 6f 6e 20 72 65 74 75 72 6e 73 20 61 20  ction returns a 
214b1 7a 65 72 6f 2d 66 69 6c 6c 65 64 20 62 6c 6f 62  zero-filled blob
214b2 20 6f 66 20 73 69 7a 65 20 4e 20 62 79 74 65 73   of size N bytes
214b3 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
214b4 20 7a 65 72 6f 62 6c 6f 62 46 75 6e 63 28 0a 20   zeroblobFunc(. 
214b5 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
214b6 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
214b7 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
214b8 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
214b9 0a 20 20 69 36 34 20 6e 3b 0a 20 20 61 73 73 65  .  i64 n;.  asse
214ba 72 74 28 20 61 72 67 63 3d 3d 31 20 29 3b 0a 20  rt( argc==1 );. 
214bb 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45   UNUSED_PARAMETE
214bc 52 28 61 72 67 63 29 3b 0a 20 20 6e 20 3d 20 73  R(argc);.  n = s
214bd 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
214be 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69  64(argv[0]);.  i
214bf 66 28 20 6e 3e 53 51 4c 49 54 45 5f 4d 41 58 5f  f( n>SQLITE_MAX_
214c0 4c 45 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71  LENGTH ){.    sq
214c1 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
214c2 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
214c3 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  t);.  }else{.   
214c4 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
214c5 7a 65 72 6f 62 6c 6f 62 28 63 6f 6e 74 65 78 74  zeroblob(context
214c6 2c 20 6e 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  , n);.  }.}../*.
214c7 2a 2a 20 54 68 65 20 72 65 70 6c 61 63 65 28 29  ** The replace()
214c8 20 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 72 65   function.  Thre
214c9 65 20 61 72 67 75 6d 65 6e 74 73 20 61 72 65 20  e arguments are 
214ca 61 6c 6c 20 73 74 72 69 6e 67 73 3a 20 63 61 6c  all strings: cal
214cb 6c 0a 2a 2a 20 74 68 65 6d 20 41 2c 20 42 2c 20  l.** them A, B, 
214cc 61 6e 64 20 43 2e 20 54 68 65 20 72 65 73 75 6c  and C. The resul
214cd 74 20 69 73 20 61 6c 73 6f 20 61 20 73 74 72 69  t is also a stri
214ce 6e 67 20 77 68 69 63 68 20 69 73 20 64 65 72 69  ng which is deri
214cf 76 65 64 0a 2a 2a 20 66 72 6f 6d 20 41 20 62 79  ved.** from A by
214d0 20 72 65 70 6c 61 63 69 6e 67 20 65 76 65 72 79   replacing every
214d1 20 6f 63 63 75 72 61 6e 63 65 20 6f 66 20 42 20   occurance of B 
214d2 77 69 74 68 20 43 2e 20 20 54 68 65 20 6d 61 74  with C.  The mat
214d3 63 68 0a 2a 2a 20 6d 75 73 74 20 62 65 20 65 78  ch.** must be ex
214d4 61 63 74 2e 20 20 43 6f 6c 6c 61 74 69 6e 67 20  act.  Collating 
214d5 73 65 71 75 65 6e 63 65 73 20 61 72 65 20 6e 6f  sequences are no
214d6 74 20 75 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  t used..*/.stati
214d7 63 20 76 6f 69 64 20 72 65 70 6c 61 63 65 46 75  c void replaceFu
214d8 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
214d9 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
214da 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
214db 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
214dc 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
214dd 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 53 74  signed char *zSt
214de 72 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65  r;        /* The
214df 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 41 20   input string A 
214e0 2a 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  */.  const unsig
214e1 6e 65 64 20 63 68 61 72 20 2a 7a 50 61 74 74 65  ned char *zPatte
214e2 72 6e 3b 20 20 20 20 2f 2a 20 54 68 65 20 70 61  rn;    /* The pa
214e3 74 74 65 72 6e 20 73 74 72 69 6e 67 20 42 20 2a  ttern string B *
214e4 2f 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e  /.  const unsign
214e5 65 64 20 63 68 61 72 20 2a 7a 52 65 70 3b 20 20  ed char *zRep;  
214e6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 70        /* The rep
214e7 6c 61 63 65 6d 65 6e 74 20 73 74 72 69 6e 67 20  lacement string 
214e8 43 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20  C */.  unsigned 
214e9 63 68 61 72 20 2a 7a 4f 75 74 3b 20 20 20 20 20  char *zOut;     
214ea 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
214eb 6f 75 74 70 75 74 20 2a 2f 0a 20 20 69 6e 74 20  output */.  int 
214ec 6e 53 74 72 3b 20 20 20 20 20 20 20 20 20 20 20  nStr;           
214ed 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
214ee 7a 53 74 72 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  zStr */.  int nP
214ef 61 74 74 65 72 6e 3b 20 20 20 20 20 20 20 20 20  attern;         
214f0 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 7a 50     /* Size of zP
214f1 61 74 74 65 72 6e 20 2a 2f 0a 20 20 69 6e 74 20  attern */.  int 
214f2 6e 52 65 70 3b 20 20 20 20 20 20 20 20 20 20 20  nRep;           
214f3 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20       /* Size of 
214f4 7a 52 65 70 20 2a 2f 0a 20 20 69 36 34 20 6e 4f  zRep */.  i64 nO
214f5 75 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ut;             
214f6 20 20 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 73 69     /* Maximum si
214f7 7a 65 20 6f 66 20 7a 4f 75 74 20 2a 2f 0a 20 20  ze of zOut */.  
214f8 69 6e 74 20 6c 6f 6f 70 4c 69 6d 69 74 3b 20 20  int loopLimit;  
214f9 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 73 74           /* Last
214fa 20 7a 53 74 72 5b 5d 20 74 68 61 74 20 6d 69 67   zStr[] that mig
214fb 68 74 20 6d 61 74 63 68 20 7a 50 61 74 74 65 72  ht match zPatter
214fc 6e 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69 2c 20  n[] */.  int i, 
214fd 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
214fe 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
214ff 72 73 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28  rs */..  assert(
21500 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e   argc==3 );.  UN
21501 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 28 61  USED_PARAMETER(a
21502 72 67 63 29 3b 0a 20 20 7a 53 74 72 20 3d 20 73  rgc);.  zStr = s
21503 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
21504 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 69 66  t(argv[0]);.  if
21505 28 20 7a 53 74 72 3d 3d 30 20 29 20 72 65 74 75  ( zStr==0 ) retu
21506 72 6e 3b 0a 20 20 6e 53 74 72 20 3d 20 73 71 6c  rn;.  nStr = sql
21507 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73  ite3_value_bytes
21508 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 61 73 73  (argv[0]);.  ass
21509 65 72 74 28 20 7a 53 74 72 3d 3d 73 71 6c 69 74  ert( zStr==sqlit
2150a 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
2150b 67 76 5b 30 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f  gv[0]) );  /* No
2150c 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65   encoding change
2150d 20 2a 2f 0a 20 20 7a 50 61 74 74 65 72 6e 20 3d   */.  zPattern =
2150e 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
2150f 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20  ext(argv[1]);.  
21510 69 66 28 20 7a 50 61 74 74 65 72 6e 3d 3d 30 20  if( zPattern==0 
21511 7c 7c 20 7a 50 61 74 74 65 72 6e 5b 30 5d 3d 3d  || zPattern[0]==
21512 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 6e 50  0 ) return;.  nP
21513 61 74 74 65 72 6e 20 3d 20 73 71 6c 69 74 65 33  attern = sqlite3
21514 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
21515 76 5b 31 5d 29 3b 0a 20 20 61 73 73 65 72 74 28  v[1]);.  assert(
21516 20 7a 50 61 74 74 65 72 6e 3d 3d 73 71 6c 69 74   zPattern==sqlit
21517 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
21518 67 76 5b 31 5d 29 20 29 3b 20 20 2f 2a 20 4e 6f  gv[1]) );  /* No
21519 20 65 6e 63 6f 64 69 6e 67 20 63 68 61 6e 67 65   encoding change
2151a 20 2a 2f 0a 20 20 7a 52 65 70 20 3d 20 73 71 6c   */.  zRep = sql
2151b 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2151c 61 72 67 76 5b 32 5d 29 3b 0a 20 20 69 66 28 20  argv[2]);.  if( 
2151d 7a 52 65 70 3d 3d 30 20 29 20 72 65 74 75 72 6e  zRep==0 ) return
2151e 3b 0a 20 20 6e 52 65 70 20 3d 20 73 71 6c 69 74  ;.  nRep = sqlit
2151f 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
21520 72 67 76 5b 32 5d 29 3b 0a 20 20 61 73 73 65 72  rgv[2]);.  asser
21521 74 28 20 7a 52 65 70 3d 3d 73 71 6c 69 74 65 33  t( zRep==sqlite3
21522 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76  _value_text(argv
21523 5b 32 5d 29 20 29 3b 0a 20 20 6e 4f 75 74 20 3d  [2]) );.  nOut =
21524 20 6e 53 74 72 20 2b 20 31 3b 0a 20 20 61 73 73   nStr + 1;.  ass
21525 65 72 74 28 20 6e 4f 75 74 3c 53 51 4c 49 54 45  ert( nOut<SQLITE
21526 5f 4d 41 58 5f 4c 45 4e 47 54 48 20 29 3b 0a 20  _MAX_LENGTH );. 
21527 20 7a 4f 75 74 20 3d 20 63 6f 6e 74 65 78 74 4d   zOut = contextM
21528 61 6c 6c 6f 63 28 63 6f 6e 74 65 78 74 2c 20 28  alloc(context, (
21529 69 36 34 29 6e 4f 75 74 29 3b 0a 20 20 69 66 28  i64)nOut);.  if(
2152a 20 7a 4f 75 74 3d 3d 30 20 29 7b 0a 20 20 20 20   zOut==0 ){.    
2152b 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6c 6f  return;.  }.  lo
2152c 6f 70 4c 69 6d 69 74 20 3d 20 6e 53 74 72 20 2d  opLimit = nStr -
2152d 20 6e 50 61 74 74 65 72 6e 3b 20 20 0a 20 20 66   nPattern;  .  f
2152e 6f 72 28 69 3d 6a 3d 30 3b 20 69 3c 3d 6c 6f 6f  or(i=j=0; i<=loo
2152f 70 4c 69 6d 69 74 3b 20 69 2b 2b 29 7b 0a 20 20  pLimit; i++){.  
21530 20 20 69 66 28 20 7a 53 74 72 5b 69 5d 21 3d 7a    if( zStr[i]!=z
21531 50 61 74 74 65 72 6e 5b 30 5d 20 7c 7c 20 6d 65  Pattern[0] || me
21532 6d 63 6d 70 28 26 7a 53 74 72 5b 69 5d 2c 20 7a  mcmp(&zStr[i], z
21533 50 61 74 74 65 72 6e 2c 20 6e 50 61 74 74 65 72  Pattern, nPatter
21534 6e 29 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74  n) ){.      zOut
21535 5b 6a 2b 2b 5d 20 3d 20 7a 53 74 72 5b 69 5d 3b  [j++] = zStr[i];
21536 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
21537 20 20 75 38 20 2a 7a 4f 6c 64 3b 0a 20 20 20 20    u8 *zOld;.    
21538 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
21539 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
2153a 64 62 5f 68 61 6e 64 6c 65 28 63 6f 6e 74 65 78  db_handle(contex
2153b 74 29 3b 0a 20 20 20 20 20 20 6e 4f 75 74 20 2b  t);.      nOut +
2153c 3d 20 6e 52 65 70 20 2d 20 6e 50 61 74 74 65 72  = nRep - nPatter
2153d 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 4f 75  n;.      if( nOu
2153e 74 3e 3d 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51  t>=db->aLimit[SQ
2153f 4c 49 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54  LITE_LIMIT_LENGT
21540 48 5d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  H] ){.        sq
21541 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
21542 6f 72 5f 74 6f 6f 62 69 67 28 63 6f 6e 74 65 78  or_toobig(contex
21543 74 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  t);.        sqli
21544 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4f  te3DbFree(db, zO
21545 75 74 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74  ut);.        ret
21546 75 72 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  urn;.      }.   
21547 20 20 20 7a 4f 6c 64 20 3d 20 7a 4f 75 74 3b 0a     zOld = zOut;.
21548 20 20 20 20 20 20 7a 4f 75 74 20 3d 20 73 71 6c        zOut = sql
21549 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 7a 4f 75  ite3_realloc(zOu
2154a 74 2c 20 28 69 6e 74 29 6e 4f 75 74 29 3b 0a 20  t, (int)nOut);. 
2154b 20 20 20 20 20 69 66 28 20 7a 4f 75 74 3d 3d 30       if( zOut==0
2154c 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
2154d 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
2154e 5f 6e 6f 6d 65 6d 28 63 6f 6e 74 65 78 74 29 3b  _nomem(context);
2154f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
21550 44 62 46 72 65 65 28 64 62 2c 20 7a 4f 6c 64 29  DbFree(db, zOld)
21551 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
21552 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
21553 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c  memcpy(&zOut[j],
21554 20 7a 52 65 70 2c 20 6e 52 65 70 29 3b 0a 20 20   zRep, nRep);.  
21555 20 20 20 20 6a 20 2b 3d 20 6e 52 65 70 3b 0a 20      j += nRep;. 
21556 20 20 20 20 20 69 20 2b 3d 20 6e 50 61 74 74 65       i += nPatte
21557 72 6e 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn-1;.    }.  }.
21558 20 20 61 73 73 65 72 74 28 20 6a 2b 6e 53 74 72    assert( j+nStr
21559 2d 69 2b 31 3d 3d 6e 4f 75 74 20 29 3b 0a 20 20  -i+1==nOut );.  
2155a 6d 65 6d 63 70 79 28 26 7a 4f 75 74 5b 6a 5d 2c  memcpy(&zOut[j],
2155b 20 26 7a 53 74 72 5b 69 5d 2c 20 6e 53 74 72 2d   &zStr[i], nStr-
2155c 69 29 3b 0a 20 20 6a 20 2b 3d 20 6e 53 74 72 20  i);.  j += nStr 
2155d 2d 20 69 3b 0a 20 20 61 73 73 65 72 74 28 20 6a  - i;.  assert( j
2155e 3c 3d 6e 4f 75 74 20 29 3b 0a 20 20 7a 4f 75 74  <=nOut );.  zOut
2155f 5b 6a 5d 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  [j] = 0;.  sqlit
21560 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 63  e3_result_text(c
21561 6f 6e 74 65 78 74 2c 20 28 63 68 61 72 2a 29 7a  ontext, (char*)z
21562 4f 75 74 2c 20 6a 2c 20 73 71 6c 69 74 65 33 5f  Out, j, sqlite3_
21563 66 72 65 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  free);.}../*.** 
21564 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
21565 66 20 74 68 65 20 54 52 49 4d 28 29 2c 20 4c 54  f the TRIM(), LT
21566 52 49 4d 28 29 2c 20 61 6e 64 20 52 54 52 49 4d  RIM(), and RTRIM
21567 28 29 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a  () functions..**
21568 20 54 68 65 20 75 73 65 72 64 61 74 61 20 69 73   The userdata is
21569 20 30 78 31 20 66 6f 72 20 6c 65 66 74 20 74 72   0x1 for left tr
2156a 69 6d 2c 20 30 78 32 20 66 6f 72 20 72 69 67 68  im, 0x2 for righ
2156b 74 20 74 72 69 6d 2c 20 30 78 33 20 66 6f 72 20  t trim, 0x3 for 
2156c 62 6f 74 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  both..*/.static 
2156d 76 6f 69 64 20 74 72 69 6d 46 75 6e 63 28 0a 20  void trimFunc(. 
2156e 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
2156f 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
21570 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
21571 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
21572 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  .  const unsigne
21573 64 20 63 68 61 72 20 2a 7a 49 6e 3b 20 20 20 20  d char *zIn;    
21574 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74       /* Input st
21575 72 69 6e 67 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ring */.  const 
21576 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
21577 43 68 61 72 53 65 74 3b 20 20 20 20 2f 2a 20 53  CharSet;    /* S
21578 65 74 20 6f 66 20 63 68 61 72 61 63 74 65 72 73  et of characters
21579 20 74 6f 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e   to trim */.  in
2157a 74 20 6e 49 6e 3b 20 20 20 20 20 20 20 20 20 20  t nIn;          
2157b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2157c 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
2157d 65 73 20 69 6e 20 69 6e 70 75 74 20 2a 2f 0a 20  es in input */. 
2157e 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20   int flags;     
2157f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21580 20 20 20 2f 2a 20 31 3a 20 74 72 69 6d 6c 65 66     /* 1: trimlef
21581 74 20 20 32 3a 20 74 72 69 6d 72 69 67 68 74 20  t  2: trimright 
21582 20 33 3a 20 74 72 69 6d 20 2a 2f 0a 20 20 69 6e   3: trim */.  in
21583 74 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20  t i;            
21584 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21585 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
21586 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68  */.  unsigned ch
21587 61 72 20 2a 61 4c 65 6e 3b 20 20 20 20 20 20 20  ar *aLen;       
21588 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
21589 20 6f 66 20 65 61 63 68 20 63 68 61 72 61 63 74   of each charact
2158a 65 72 20 69 6e 20 7a 43 68 61 72 53 65 74 20 2a  er in zCharSet *
2158b 2f 0a 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61  /.  unsigned cha
2158c 72 20 2a 2a 61 7a 43 68 61 72 3b 20 20 20 20 20  r **azChar;     
2158d 20 20 20 20 20 20 2f 2a 20 49 6e 64 69 76 69 64        /* Individ
2158e 75 61 6c 20 63 68 61 72 61 63 74 65 72 73 20 69  ual characters i
2158f 6e 20 7a 43 68 61 72 53 65 74 20 2a 2f 0a 20 20  n zCharSet */.  
21590 69 6e 74 20 6e 43 68 61 72 3b 20 20 20 20 20 20  int nChar;      
21591 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21592 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 63    /* Number of c
21593 68 61 72 61 63 74 65 72 73 20 69 6e 20 7a 43 68  haracters in zCh
21594 61 72 53 65 74 20 2a 2f 0a 0a 20 20 69 66 28 20  arSet */..  if( 
21595 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
21596 70 65 28 61 72 67 76 5b 30 5d 29 3d 3d 53 51 4c  pe(argv[0])==SQL
21597 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
21598 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 7a 49  return;.  }.  zI
21599 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  n = sqlite3_valu
2159a 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b  e_text(argv[0]);
2159b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20 29 20  .  if( zIn==0 ) 
2159c 72 65 74 75 72 6e 3b 0a 20 20 6e 49 6e 20 3d 20  return;.  nIn = 
2159d 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
2159e 74 65 73 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20  tes(argv[0]);.  
2159f 61 73 73 65 72 74 28 20 7a 49 6e 3d 3d 73 71 6c  assert( zIn==sql
215a0 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
215a1 61 72 67 76 5b 30 5d 29 20 29 3b 0a 20 20 69 66  argv[0]) );.  if
215a2 28 20 61 72 67 63 3d 3d 31 20 29 7b 0a 20 20 20  ( argc==1 ){.   
215a3 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
215a4 73 69 67 6e 65 64 20 63 68 61 72 20 6c 65 6e 4f  signed char lenO
215a5 6e 65 5b 5d 20 3d 20 7b 20 31 20 7d 3b 0a 20 20  ne[] = { 1 };.  
215a6 20 20 73 74 61 74 69 63 20 75 6e 73 69 67 6e 65    static unsigne
215a7 64 20 63 68 61 72 20 2a 20 63 6f 6e 73 74 20 61  d char * const a
215a8 7a 4f 6e 65 5b 5d 20 3d 20 7b 20 28 75 38 2a 29  zOne[] = { (u8*)
215a9 22 20 22 20 7d 3b 0a 20 20 20 20 6e 43 68 61 72  " " };.    nChar
215aa 20 3d 20 31 3b 0a 20 20 20 20 61 4c 65 6e 20 3d   = 1;.    aLen =
215ab 20 28 75 38 2a 29 6c 65 6e 4f 6e 65 3b 0a 20 20   (u8*)lenOne;.  
215ac 20 20 61 7a 43 68 61 72 20 3d 20 28 75 6e 73 69    azChar = (unsi
215ad 67 6e 65 64 20 63 68 61 72 20 2a 2a 29 61 7a 4f  gned char **)azO
215ae 6e 65 3b 0a 20 20 20 20 7a 43 68 61 72 53 65 74  ne;.    zCharSet
215af 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20 69 66   = 0;.  }else if
215b0 28 20 28 7a 43 68 61 72 53 65 74 20 3d 20 73 71  ( (zCharSet = sq
215b1 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
215b2 28 61 72 67 76 5b 31 5d 29 29 3d 3d 30 20 29 7b  (argv[1]))==0 ){
215b3 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
215b4 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20  else{.    const 
215b5 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a  unsigned char *z
215b6 3b 0a 20 20 20 20 66 6f 72 28 7a 3d 7a 43 68 61  ;.    for(z=zCha
215b7 72 53 65 74 2c 20 6e 43 68 61 72 3d 30 3b 20 2a  rSet, nChar=0; *
215b8 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b 0a 20 20 20  z; nChar++){.   
215b9 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50 5f 55     SQLITE_SKIP_U
215ba 54 46 38 28 7a 29 3b 0a 20 20 20 20 7d 0a 20 20  TF8(z);.    }.  
215bb 20 20 69 66 28 20 6e 43 68 61 72 3e 30 20 29 7b    if( nChar>0 ){
215bc 0a 20 20 20 20 20 20 61 7a 43 68 61 72 20 3d 20  .      azChar = 
215bd 63 6f 6e 74 65 78 74 4d 61 6c 6c 6f 63 28 63 6f  contextMalloc(co
215be 6e 74 65 78 74 2c 20 28 28 69 36 34 29 6e 43 68  ntext, ((i64)nCh
215bf 61 72 29 2a 28 73 69 7a 65 6f 66 28 63 68 61 72  ar)*(sizeof(char
215c0 2a 29 2b 31 29 29 3b 0a 20 20 20 20 20 20 69 66  *)+1));.      if
215c1 28 20 61 7a 43 68 61 72 3d 3d 30 20 29 7b 0a 20  ( azChar==0 ){. 
215c2 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20         return;. 
215c3 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 4c 65       }.      aLe
215c4 6e 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  n = (unsigned ch
215c5 61 72 2a 29 26 61 7a 43 68 61 72 5b 6e 43 68 61  ar*)&azChar[nCha
215c6 72 5d 3b 0a 20 20 20 20 20 20 66 6f 72 28 7a 3d  r];.      for(z=
215c7 7a 43 68 61 72 53 65 74 2c 20 6e 43 68 61 72 3d  zCharSet, nChar=
215c8 30 3b 20 2a 7a 3b 20 6e 43 68 61 72 2b 2b 29 7b  0; *z; nChar++){
215c9 0a 20 20 20 20 20 20 20 20 61 7a 43 68 61 72 5b  .        azChar[
215ca 6e 43 68 61 72 5d 20 3d 20 28 75 6e 73 69 67 6e  nChar] = (unsign
215cb 65 64 20 63 68 61 72 20 2a 29 7a 3b 0a 20 20 20  ed char *)z;.   
215cc 20 20 20 20 20 53 51 4c 49 54 45 5f 53 4b 49 50       SQLITE_SKIP
215cd 5f 55 54 46 38 28 7a 29 3b 0a 20 20 20 20 20 20  _UTF8(z);.      
215ce 20 20 61 4c 65 6e 5b 6e 43 68 61 72 5d 20 3d 20    aLen[nChar] = 
215cf 7a 20 2d 20 61 7a 43 68 61 72 5b 6e 43 68 61 72  z - azChar[nChar
215d0 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  ];.      }.    }
215d1 0a 20 20 7d 0a 20 20 69 66 28 20 6e 43 68 61 72  .  }.  if( nChar
215d2 3e 30 20 29 7b 0a 20 20 20 20 66 6c 61 67 73 20  >0 ){.    flags 
215d3 3d 20 53 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f  = SQLITE_PTR_TO_
215d4 49 4e 54 28 73 71 6c 69 74 65 33 5f 75 73 65 72  INT(sqlite3_user
215d5 5f 64 61 74 61 28 63 6f 6e 74 65 78 74 29 29 3b  _data(context));
215d6 0a 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26  .    if( flags &
215d7 20 31 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c   1 ){.      whil
215d8 65 28 20 6e 49 6e 3e 30 20 29 7b 0a 20 20 20 20  e( nIn>0 ){.    
215d9 20 20 20 20 69 6e 74 20 6c 65 6e 3b 0a 20 20 20      int len;.   
215da 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c       for(i=0; i<
215db 6e 43 68 61 72 3b 20 69 2b 2b 29 7b 0a 20 20 20  nChar; i++){.   
215dc 20 20 20 20 20 20 20 6c 65 6e 20 3d 20 61 4c 65         len = aLe
215dd 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  n[i];.          
215de 69 66 28 20 6d 65 6d 63 6d 70 28 7a 49 6e 2c 20  if( memcmp(zIn, 
215df 61 7a 43 68 61 72 5b 69 5d 2c 20 6c 65 6e 29 3d  azChar[i], len)=
215e0 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
215e1 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
215e2 28 20 69 3e 3d 6e 43 68 61 72 20 29 20 62 72 65  ( i>=nChar ) bre
215e3 61 6b 3b 0a 20 20 20 20 20 20 20 20 7a 49 6e 20  ak;.        zIn 
215e4 2b 3d 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20  += len;.        
215e5 6e 49 6e 20 2d 3d 20 6c 65 6e 3b 0a 20 20 20 20  nIn -= len;.    
215e6 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
215e7 28 20 66 6c 61 67 73 20 26 20 32 20 29 7b 0a 20  ( flags & 2 ){. 
215e8 20 20 20 20 20 77 68 69 6c 65 28 20 6e 49 6e 3e       while( nIn>
215e9 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  0 ){.        int
215ea 20 6c 65 6e 3b 0a 20 20 20 20 20 20 20 20 66 6f   len;.        fo
215eb 72 28 69 3d 30 3b 20 69 3c 6e 43 68 61 72 3b 20  r(i=0; i<nChar; 
215ec 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  i++){.          
215ed 6c 65 6e 20 3d 20 61 4c 65 6e 5b 69 5d 3b 0a 20  len = aLen[i];. 
215ee 20 20 20 20 20 20 20 20 20 69 66 28 20 6c 65 6e           if( len
215ef 3c 3d 6e 49 6e 20 26 26 20 6d 65 6d 63 6d 70 28  <=nIn && memcmp(
215f0 26 7a 49 6e 5b 6e 49 6e 2d 6c 65 6e 5d 2c 61 7a  &zIn[nIn-len],az
215f1 43 68 61 72 5b 69 5d 2c 6c 65 6e 29 3d 3d 30 20  Char[i],len)==0 
215f2 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20  ) break;.       
215f3 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 69   }.        if( i
215f4 3e 3d 6e 43 68 61 72 20 29 20 62 72 65 61 6b 3b  >=nChar ) break;
215f5 0a 20 20 20 20 20 20 20 20 6e 49 6e 20 2d 3d 20  .        nIn -= 
215f6 6c 65 6e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  len;.      }.   
215f7 20 7d 0a 20 20 20 20 69 66 28 20 7a 43 68 61 72   }.    if( zChar
215f8 53 65 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Set ){.      sql
215f9 69 74 65 33 5f 66 72 65 65 28 61 7a 43 68 61 72  ite3_free(azChar
215fa 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 73  );.    }.  }.  s
215fb 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65  qlite3_result_te
215fc 78 74 28 63 6f 6e 74 65 78 74 2c 20 28 63 68 61  xt(context, (cha
215fd 72 2a 29 7a 49 6e 2c 20 6e 49 6e 2c 20 53 51 4c  r*)zIn, nIn, SQL
215fe 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a  ITE_TRANSIENT);.
215ff 7d 0a 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  }...#ifdef SQLIT
21600 45 5f 53 4f 55 4e 44 45 58 0a 2f 2a 0a 2a 2a 20  E_SOUNDEX./*.** 
21601 43 6f 6d 70 75 74 65 20 74 68 65 20 73 6f 75 6e  Compute the soun
21602 64 65 78 20 65 6e 63 6f 64 69 6e 67 20 6f 66 20  dex encoding of 
21603 61 20 77 6f 72 64 2e 0a 2a 2f 0a 73 74 61 74 69  a word..*/.stati
21604 63 20 76 6f 69 64 20 73 6f 75 6e 64 65 78 46 75  c void soundexFu
21605 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
21606 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 0a  ntext *context,.
21607 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71    int argc,.  sq
21608 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
21609 67 76 0a 29 7b 0a 20 20 63 68 61 72 20 7a 52 65  gv.){.  char zRe
2160a 73 75 6c 74 5b 38 5d 3b 0a 20 20 63 6f 6e 73 74  sult[8];.  const
2160b 20 75 38 20 2a 7a 49 6e 3b 0a 20 20 69 6e 74 20   u8 *zIn;.  int 
2160c 69 2c 20 6a 3b 0a 20 20 73 74 61 74 69 63 20 63  i, j;.  static c
2160d 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68  onst unsigned ch
2160e 61 72 20 69 43 6f 64 65 5b 5d 20 3d 20 7b 0a 20  ar iCode[] = {. 
2160f 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30     0, 0, 0, 0, 0
21610 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
21611 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
21612 30 2c 0a 20 20 20 20 30 2c 20 30 2c 20 30 2c 20  0,.    0, 0, 0, 
21613 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
21614 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
21615 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
21616 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
21617 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
21618 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20 20 30  , 0, 0, 0,.    0
21619 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
2161a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2161b 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
2161c 20 20 20 30 2c 20 30 2c 20 31 2c 20 32 2c 20 33     0, 0, 1, 2, 3
2161d 2c 20 30 2c 20 31 2c 20 32 2c 20 30 2c 20 30 2c  , 0, 1, 2, 0, 0,
2161e 20 32 2c 20 32 2c 20 34 2c 20 35 2c 20 35 2c 20   2, 2, 4, 5, 5, 
2161f 30 2c 0a 20 20 20 20 31 2c 20 32 2c 20 36 2c 20  0,.    1, 2, 6, 
21620 32 2c 20 33 2c 20 30 2c 20 31 2c 20 30 2c 20 32  2, 3, 0, 1, 0, 2
21621 2c 20 30 2c 20 32 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 2, 0, 0, 0,
21622 20 30 2c 20 30 2c 0a 20 20 20 20 30 2c 20 30 2c   0, 0,.    0, 0,
21623 20 31 2c 20 32 2c 20 33 2c 20 30 2c 20 31 2c 20   1, 2, 3, 0, 1, 
21624 32 2c 20 30 2c 20 30 2c 20 32 2c 20 32 2c 20 34  2, 0, 0, 2, 2, 4
21625 2c 20 35 2c 20 35 2c 20 30 2c 0a 20 20 20 20 31  , 5, 5, 0,.    1
21626 2c 20 32 2c 20 36 2c 20 32 2c 20 33 2c 20 30 2c  , 2, 6, 2, 3, 0,
21627 20 31 2c 20 30 2c 20 32 2c 20 30 2c 20 32 2c 20   1, 0, 2, 0, 2, 
21628 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20  0, 0, 0, 0, 0,. 
21629 20 7d 3b 0a 20 20 61 73 73 65 72 74 28 20 61 72   };.  assert( ar
2162a 67 63 3d 3d 31 20 29 3b 0a 20 20 7a 49 6e 20 3d  gc==1 );.  zIn =
2162b 20 28 75 38 2a 29 73 71 6c 69 74 65 33 5f 76 61   (u8*)sqlite3_va
2162c 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
2162d 29 3b 0a 20 20 69 66 28 20 7a 49 6e 3d 3d 30 20  );.  if( zIn==0 
2162e 29 20 7a 49 6e 20 3d 20 28 75 38 2a 29 22 22 3b  ) zIn = (u8*)"";
2162f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 7a 49 6e 5b  .  for(i=0; zIn[
21630 69 5d 20 26 26 20 21 69 73 61 6c 70 68 61 28 7a  i] && !isalpha(z
21631 49 6e 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  In[i]); i++){}. 
21632 20 69 66 28 20 7a 49 6e 5b 69 5d 20 29 7b 0a 20   if( zIn[i] ){. 
21633 20 20 20 75 38 20 70 72 65 76 63 6f 64 65 20 3d     u8 prevcode =
21634 20 69 43 6f 64 65 5b 7a 49 6e 5b 69 5d 26 30 78   iCode[zIn[i]&0x
21635 37 66 5d 3b 0a 20 20 20 20 7a 52 65 73 75 6c 74  7f];.    zResult
21636 5b 30 5d 20 3d 20 74 6f 75 70 70 65 72 28 7a 49  [0] = toupper(zI
21637 6e 5b 69 5d 29 3b 0a 20 20 20 20 66 6f 72 28 6a  n[i]);.    for(j
21638 3d 31 3b 20 6a 3c 34 20 26 26 20 7a 49 6e 5b 69  =1; j<4 && zIn[i
21639 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  ]; i++){.      i
2163a 6e 74 20 63 6f 64 65 20 3d 20 69 43 6f 64 65 5b  nt code = iCode[
2163b 7a 49 6e 5b 69 5d 26 30 78 37 66 5d 3b 0a 20 20  zIn[i]&0x7f];.  
2163c 20 20 20 20 69 66 28 20 63 6f 64 65 3e 30 20 29      if( code>0 )
2163d 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 6f  {.        if( co
2163e 64 65 21 3d 70 72 65 76 63 6f 64 65 20 29 7b 0a  de!=prevcode ){.
2163f 20 20 20 20 20 20 20 20 20 20 70 72 65 76 63 6f            prevco
21640 64 65 20 3d 20 63 6f 64 65 3b 0a 20 20 20 20 20  de = code;.     
21641 20 20 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b       zResult[j++
21642 5d 20 3d 20 63 6f 64 65 20 2b 20 27 30 27 3b 0a  ] = code + '0';.
21643 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
21644 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70  }else{.        p
21645 72 65 76 63 6f 64 65 20 3d 20 30 3b 0a 20 20 20  revcode = 0;.   
21646 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 77     }.    }.    w
21647 68 69 6c 65 28 20 6a 3c 34 20 29 7b 0a 20 20 20  hile( j<4 ){.   
21648 20 20 20 7a 52 65 73 75 6c 74 5b 6a 2b 2b 5d 20     zResult[j++] 
21649 3d 20 27 30 27 3b 0a 20 20 20 20 7d 0a 20 20 20  = '0';.    }.   
2164a 20 7a 52 65 73 75 6c 74 5b 6a 5d 20 3d 20 30 3b   zResult[j] = 0;
2164b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
2164c 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74 65 78 74  ult_text(context
2164d 2c 20 7a 52 65 73 75 6c 74 2c 20 34 2c 20 53 51  , zResult, 4, SQ
2164e 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
2164f 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
21650 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
21651 74 28 63 6f 6e 74 65 78 74 2c 20 22 3f 30 30 30  t(context, "?000
21652 22 2c 20 34 2c 20 53 51 4c 49 54 45 5f 53 54 41  ", 4, SQLITE_STA
21653 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e 64  TIC);.  }.}.#end
21654 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  if..#ifndef SQLI
21655 54 45 5f 4f 4d 49 54 5f 4c 4f 41 44 5f 45 58 54  TE_OMIT_LOAD_EXT
21656 45 4e 53 49 4f 4e 0a 2f 2a 0a 2a 2a 20 41 20 66  ENSION./*.** A f
21657 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 6c 6f 61  unction that loa
21658 64 73 20 61 20 73 68 61 72 65 64 2d 6c 69 62 72  ds a shared-libr
21659 61 72 79 20 65 78 74 65 6e 73 69 6f 6e 20 74 68  ary extension th
2165a 65 6e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 2e  en returns NULL.
2165b 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2165c 6c 6f 61 64 45 78 74 28 73 71 6c 69 74 65 33 5f  loadExt(sqlite3_
2165d 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
2165e 2c 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c 69  , int argc, sqli
2165f 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
21660 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  ){.  const char 
21661 2a 7a 46 69 6c 65 20 3d 20 28 63 6f 6e 73 74 20  *zFile = (const 
21662 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33 5f 76  char *)sqlite3_v
21663 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30  alue_text(argv[0
21664 5d 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  ]);.  const char
21665 20 2a 7a 50 72 6f 63 3b 0a 20 20 73 71 6c 69 74   *zProc;.  sqlit
21666 65 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33  e3 *db = sqlite3
21667 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64  _context_db_hand
21668 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 63  le(context);.  c
21669 68 61 72 20 2a 7a 45 72 72 4d 73 67 20 3d 20 30  har *zErrMsg = 0
2166a 3b 0a 0a 20 20 69 66 28 20 61 72 67 63 3d 3d 32  ;..  if( argc==2
2166b 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20   ){.    zProc = 
2166c 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71  (const char *)sq
2166d 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
2166e 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d 65 6c  (argv[1]);.  }el
2166f 73 65 7b 0a 20 20 20 20 7a 50 72 6f 63 20 3d 20  se{.    zProc = 
21670 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 7a 46 69  0;.  }.  if( zFi
21671 6c 65 20 26 26 20 73 71 6c 69 74 65 33 5f 6c 6f  le && sqlite3_lo
21672 61 64 5f 65 78 74 65 6e 73 69 6f 6e 28 64 62 2c  ad_extension(db,
21673 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c 20 26   zFile, zProc, &
21674 7a 45 72 72 4d 73 67 29 20 29 7b 0a 20 20 20 20  zErrMsg) ){.    
21675 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
21676 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20 7a 45  rror(context, zE
21677 72 72 4d 73 67 2c 20 2d 31 29 3b 0a 20 20 20 20  rrMsg, -1);.    
21678 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45 72  sqlite3_free(zEr
21679 72 4d 73 67 29 3b 0a 20 20 7d 0a 7d 0a 23 65 6e  rMsg);.  }.}.#en
2167a 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  dif.../*.** An i
2167b 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
2167c 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
2167d 72 65 20 68 6f 6c 64 73 20 74 68 65 20 63 6f 6e  re holds the con
2167e 74 65 78 74 20 6f 66 20 61 0a 2a 2a 20 73 75 6d  text of a.** sum
2167f 28 29 20 6f 72 20 61 76 67 28 29 20 61 67 67 72  () or avg() aggr
21680 65 67 61 74 65 20 63 6f 6d 70 75 74 61 74 69 6f  egate computatio
21681 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  n..*/.typedef st
21682 72 75 63 74 20 53 75 6d 43 74 78 20 53 75 6d 43  ruct SumCtx SumC
21683 74 78 3b 0a 73 74 72 75 63 74 20 53 75 6d 43 74  tx;.struct SumCt
21684 78 20 7b 0a 20 20 64 6f 75 62 6c 65 20 72 53 75  x {.  double rSu
21685 6d 3b 20 20 20 20 20 20 2f 2a 20 46 6c 6f 61 74  m;      /* Float
21686 69 6e 67 20 70 6f 69 6e 74 20 73 75 6d 20 2a 2f  ing point sum */
21687 0a 20 20 69 36 34 20 69 53 75 6d 3b 20 20 20 20  .  i64 iSum;    
21688 20 20 20 20 20 2f 2a 20 49 6e 74 65 67 65 72 20       /* Integer 
21689 73 75 6d 20 2a 2f 20 20 20 0a 20 20 69 36 34 20  sum */   .  i64 
2168a 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  cnt;          /*
2168b 20 4e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65   Number of eleme
2168c 6e 74 73 20 73 75 6d 6d 65 64 20 2a 2f 0a 20 20  nts summed */.  
2168d 75 38 20 6f 76 65 72 66 6c 6f 77 3b 20 20 20 20  u8 overflow;    
2168e 20 20 2f 2a 20 54 72 75 65 20 69 66 20 69 6e 74    /* True if int
2168f 65 67 65 72 20 6f 76 65 72 66 6c 6f 77 20 73 65  eger overflow se
21690 65 6e 20 2a 2f 0a 20 20 75 38 20 61 70 70 72 6f  en */.  u8 appro
21691 78 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75  x;        /* Tru
21692 65 20 69 66 20 6e 6f 6e 2d 69 6e 74 65 67 65 72  e if non-integer
21693 20 76 61 6c 75 65 20 77 61 73 20 69 6e 70 75 74   value was input
21694 20 74 6f 20 74 68 65 20 73 75 6d 20 2a 2f 0a 7d   to the sum */.}
21695 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74 69 6e 65  ;../*.** Routine
21696 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  s used to comput
21697 65 20 74 68 65 20 73 75 6d 2c 20 61 76 65 72 61  e the sum, avera
21698 67 65 2c 20 61 6e 64 20 74 6f 74 61 6c 2e 0a 2a  ge, and total..*
21699 2a 0a 2a 2a 20 54 68 65 20 53 55 4d 28 29 20 66  *.** The SUM() f
2169a 75 6e 63 74 69 6f 6e 20 66 6f 6c 6c 6f 77 73 20  unction follows 
2169b 74 68 65 20 28 62 72 6f 6b 65 6e 29 20 53 51 4c  the (broken) SQL
2169c 20 73 74 61 6e 64 61 72 64 20 77 68 69 63 68 20   standard which 
2169d 6d 65 61 6e 73 0a 2a 2a 20 74 68 61 74 20 69 74  means.** that it
2169e 20 72 65 74 75 72 6e 73 20 4e 55 4c 4c 20 69 66   returns NULL if
2169f 20 69 74 20 73 75 6d 73 20 6f 76 65 72 20 6e 6f   it sums over no
216a0 20 69 6e 70 75 74 73 2e 20 20 54 4f 54 41 4c 20   inputs.  TOTAL 
216a1 72 65 74 75 72 6e 73 0a 2a 2a 20 30 2e 30 20 69  returns.** 0.0 i
216a2 6e 20 74 68 61 74 20 63 61 73 65 2e 20 20 49 6e  n that case.  In
216a3 20 61 64 64 69 74 69 6f 6e 2c 20 54 4f 54 41 4c   addition, TOTAL
216a4 20 61 6c 77 61 79 73 20 72 65 74 75 72 6e 73 20   always returns 
216a5 61 20 66 6c 6f 61 74 20 77 68 65 72 65 0a 2a 2a  a float where.**
216a6 20 53 55 4d 20 6d 69 67 68 74 20 72 65 74 75 72   SUM might retur
216a7 6e 20 61 6e 20 69 6e 74 65 67 65 72 20 69 66 20  n an integer if 
216a8 69 74 20 6e 65 76 65 72 20 65 6e 63 6f 75 6e 74  it never encount
216a9 65 72 73 20 61 20 66 6c 6f 61 74 69 6e 67 20 70  ers a floating p
216aa 6f 69 6e 74 0a 2a 2a 20 76 61 6c 75 65 2e 20 20  oint.** value.  
216ab 54 4f 54 41 4c 20 6e 65 76 65 72 20 66 61 69 6c  TOTAL never fail
216ac 73 2c 20 62 75 74 20 53 55 4d 20 6d 69 67 68 74  s, but SUM might
216ad 20 74 68 72 6f 75 67 68 20 61 6e 20 65 78 63 65   through an exce
216ae 70 74 69 6f 6e 20 69 66 0a 2a 2a 20 69 74 20 6f  ption if.** it o
216af 76 65 72 66 6c 6f 77 73 20 61 6e 20 69 6e 74 65  verflows an inte
216b0 67 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ger..*/.static v
216b1 6f 69 64 20 73 75 6d 53 74 65 70 28 73 71 6c 69  oid sumStep(sqli
216b2 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e  te3_context *con
216b3 74 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20  text, int argc, 
216b4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
216b5 61 72 67 76 29 7b 0a 20 20 53 75 6d 43 74 78 20  argv){.  SumCtx 
216b6 2a 70 3b 0a 20 20 69 6e 74 20 74 79 70 65 3b 0a  *p;.  int type;.
216b7 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
216b8 31 20 29 3b 0a 20 20 55 4e 55 53 45 44 5f 50 41  1 );.  UNUSED_PA
216b9 52 41 4d 45 54 45 52 28 61 72 67 63 29 3b 0a 20  RAMETER(argc);. 
216ba 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
216bb 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
216bc 6f 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a  ontext, sizeof(*
216bd 70 29 29 3b 0a 20 20 74 79 70 65 20 3d 20 73 71  p));.  type = sq
216be 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e 75 6d 65  lite3_value_nume
216bf 72 69 63 5f 74 79 70 65 28 61 72 67 76 5b 30 5d  ric_type(argv[0]
216c0 29 3b 0a 20 20 69 66 28 20 70 20 26 26 20 74 79  );.  if( p && ty
216c1 70 65 21 3d 53 51 4c 49 54 45 5f 4e 55 4c 4c 20  pe!=SQLITE_NULL 
216c2 29 7b 0a 20 20 20 20 70 2d 3e 63 6e 74 2b 2b 3b  ){.    p->cnt++;
216c3 0a 20 20 20 20 69 66 28 20 74 79 70 65 3d 3d 53  .    if( type==S
216c4 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 29 7b  QLITE_INTEGER ){
216c5 0a 20 20 20 20 20 20 69 36 34 20 76 20 3d 20 73  .      i64 v = s
216c6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74  qlite3_value_int
216c7 36 34 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20  64(argv[0]);.   
216c8 20 20 20 70 2d 3e 72 53 75 6d 20 2b 3d 20 76 3b     p->rSum += v;
216c9 0a 20 20 20 20 20 20 69 66 28 20 28 70 2d 3e 61  .      if( (p->a
216ca 70 70 72 6f 78 7c 70 2d 3e 6f 76 65 72 66 6c 6f  pprox|p->overflo
216cb 77 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  w)==0 ){.       
216cc 20 69 36 34 20 69 4e 65 77 53 75 6d 20 3d 20 70   i64 iNewSum = p
216cd 2d 3e 69 53 75 6d 20 2b 20 76 3b 0a 20 20 20 20  ->iSum + v;.    
216ce 20 20 20 20 69 6e 74 20 73 31 20 3d 20 70 2d 3e      int s1 = p->
216cf 69 53 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28  iSum >> (sizeof(
216d0 69 36 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20  i64)*8-1);.     
216d1 20 20 20 69 6e 74 20 73 32 20 3d 20 76 20 20 20     int s2 = v   
216d2 20 20 20 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69      >> (sizeof(i
216d3 36 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20  64)*8-1);.      
216d4 20 20 69 6e 74 20 73 33 20 3d 20 69 4e 65 77 53    int s3 = iNewS
216d5 75 6d 20 3e 3e 20 28 73 69 7a 65 6f 66 28 69 36  um >> (sizeof(i6
216d6 34 29 2a 38 2d 31 29 3b 0a 20 20 20 20 20 20 20  4)*8-1);.       
216d7 20 70 2d 3e 6f 76 65 72 66 6c 6f 77 20 3d 20 28   p->overflow = (
216d8 73 31 26 73 32 26 7e 73 33 29 20 7c 20 28 7e 73  s1&s2&~s3) | (~s
216d9 31 26 7e 73 32 26 73 33 29 3b 0a 20 20 20 20 20  1&~s2&s3);.     
216da 20 20 20 70 2d 3e 69 53 75 6d 20 3d 20 69 4e 65     p->iSum = iNe
216db 77 53 75 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  wSum;.      }.  
216dc 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
216dd 2d 3e 72 53 75 6d 20 2b 3d 20 73 71 6c 69 74 65  ->rSum += sqlite
216de 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28 61  3_value_double(a
216df 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20 70  rgv[0]);.      p
216e0 2d 3e 61 70 70 72 6f 78 20 3d 20 31 3b 0a 20 20  ->approx = 1;.  
216e1 20 20 7d 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63    }.  }.}.static
216e2 20 76 6f 69 64 20 73 75 6d 46 69 6e 61 6c 69 7a   void sumFinaliz
216e3 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  e(sqlite3_contex
216e4 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53  t *context){.  S
216e5 75 6d 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20  umCtx *p;.  p = 
216e6 73 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74  sqlite3_aggregat
216e7 65 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78  e_context(contex
216e8 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 20 26  t, 0);.  if( p &
216e9 26 20 70 2d 3e 63 6e 74 3e 30 20 29 7b 0a 20 20  & p->cnt>0 ){.  
216ea 20 20 69 66 28 20 70 2d 3e 6f 76 65 72 66 6c 6f    if( p->overflo
216eb 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  w ){.      sqlit
216ec 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28  e3_result_error(
216ed 63 6f 6e 74 65 78 74 2c 22 69 6e 74 65 67 65 72  context,"integer
216ee 20 6f 76 65 72 66 6c 6f 77 22 2c 2d 31 29 3b 0a   overflow",-1);.
216ef 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 2d      }else if( p-
216f0 3e 61 70 70 72 6f 78 20 29 7b 0a 20 20 20 20 20  >approx ){.     
216f1 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
216f2 64 6f 75 62 6c 65 28 63 6f 6e 74 65 78 74 2c 20  double(context, 
216f3 70 2d 3e 72 53 75 6d 29 3b 0a 20 20 20 20 7d 65  p->rSum);.    }e
216f4 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  lse{.      sqlit
216f5 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36 34 28  e3_result_int64(
216f6 63 6f 6e 74 65 78 74 2c 20 70 2d 3e 69 53 75 6d  context, p->iSum
216f7 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 73  );.    }.  }.}.s
216f8 74 61 74 69 63 20 76 6f 69 64 20 61 76 67 46 69  tatic void avgFi
216f9 6e 61 6c 69 7a 65 28 73 71 6c 69 74 65 33 5f 63  nalize(sqlite3_c
216fa 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 29  ontext *context)
216fb 7b 0a 20 20 53 75 6d 43 74 78 20 2a 70 3b 0a 20  {.  SumCtx *p;. 
216fc 20 70 20 3d 20 73 71 6c 69 74 65 33 5f 61 67 67   p = sqlite3_agg
216fd 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63  regate_context(c
216fe 6f 6e 74 65 78 74 2c 20 30 29 3b 0a 20 20 69 66  ontext, 0);.  if
216ff 28 20 70 20 26 26 20 70 2d 3e 63 6e 74 3e 30 20  ( p && p->cnt>0 
21700 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  ){.    sqlite3_r
21701 65 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e  esult_double(con
21702 74 65 78 74 2c 20 70 2d 3e 72 53 75 6d 2f 28 64  text, p->rSum/(d
21703 6f 75 62 6c 65 29 70 2d 3e 63 6e 74 29 3b 0a 20  ouble)p->cnt);. 
21704 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
21705 20 74 6f 74 61 6c 46 69 6e 61 6c 69 7a 65 28 73   totalFinalize(s
21706 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
21707 63 6f 6e 74 65 78 74 29 7b 0a 20 20 53 75 6d 43  context){.  SumC
21708 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c  tx *p;.  p = sql
21709 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63  ite3_aggregate_c
2170a 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20  ontext(context, 
2170b 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  0);.  sqlite3_re
2170c 73 75 6c 74 5f 64 6f 75 62 6c 65 28 63 6f 6e 74  sult_double(cont
2170d 65 78 74 2c 20 70 20 3f 20 70 2d 3e 72 53 75 6d  ext, p ? p->rSum
2170e 20 3a 20 30 2e 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   : 0.0);.}../*.*
2170f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
21710 73 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20  structure keeps 
21711 74 72 61 63 6b 20 6f 66 20 73 74 61 74 65 20 69  track of state i
21712 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 74  nformation for t
21713 68 65 0a 2a 2a 20 63 6f 75 6e 74 28 29 20 61 67  he.** count() ag
21714 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
21715 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72  ..*/.typedef str
21716 75 63 74 20 43 6f 75 6e 74 43 74 78 20 43 6f 75  uct CountCtx Cou
21717 6e 74 43 74 78 3b 0a 73 74 72 75 63 74 20 43 6f  ntCtx;.struct Co
21718 75 6e 74 43 74 78 20 7b 0a 20 20 69 36 34 20 6e  untCtx {.  i64 n
21719 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  ;.};../*.** Rout
2171a 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e  ines to implemen
2171b 74 20 74 68 65 20 63 6f 75 6e 74 28 29 20 61 67  t the count() ag
2171c 67 72 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e  gregate function
2171d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2171e 20 63 6f 75 6e 74 53 74 65 70 28 73 71 6c 69 74   countStep(sqlit
2171f 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74  e3_context *cont
21720 65 78 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 73  ext, int argc, s
21721 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61  qlite3_value **a
21722 72 67 76 29 7b 0a 20 20 43 6f 75 6e 74 43 74 78  rgv){.  CountCtx
21723 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71 6c 69 74   *p;.  p = sqlit
21724 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 6e  e3_aggregate_con
21725 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 69  text(context, si
21726 7a 65 6f 66 28 2a 70 29 29 3b 0a 20 20 69 66 28  zeof(*p));.  if(
21727 20 28 61 72 67 63 3d 3d 30 20 7c 7c 20 53 51 4c   (argc==0 || SQL
21728 49 54 45 5f 4e 55 4c 4c 21 3d 73 71 6c 69 74 65  ITE_NULL!=sqlite
21729 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67  3_value_type(arg
2172a 76 5b 30 5d 29 29 20 26 26 20 70 20 29 7b 0a 20  v[0])) && p ){. 
2172b 20 20 20 70 2d 3e 6e 2b 2b 3b 0a 20 20 7d 0a 7d     p->n++;.  }.}
2172c 20 20 20 0a 73 74 61 74 69 63 20 76 6f 69 64 20     .static void 
2172d 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65 28 73 71  countFinalize(sq
2172e 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63  lite3_context *c
2172f 6f 6e 74 65 78 74 29 7b 0a 20 20 43 6f 75 6e 74  ontext){.  Count
21730 43 74 78 20 2a 70 3b 0a 20 20 70 20 3d 20 73 71  Ctx *p;.  p = sq
21731 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
21732 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
21733 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72   0);.  sqlite3_r
21734 65 73 75 6c 74 5f 69 6e 74 36 34 28 63 6f 6e 74  esult_int64(cont
21735 65 78 74 2c 20 70 20 3f 20 70 2d 3e 6e 20 3a 20  ext, p ? p->n : 
21736 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75  0);.}../*.** Rou
21737 74 69 6e 65 73 20 74 6f 20 69 6d 70 6c 65 6d 65  tines to impleme
21738 6e 74 20 6d 69 6e 28 29 20 61 6e 64 20 6d 61 78  nt min() and max
21739 28 29 20 61 67 67 72 65 67 61 74 65 20 66 75 6e  () aggregate fun
2173a 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69  ctions..*/.stati
2173b 63 20 76 6f 69 64 20 6d 69 6e 6d 61 78 53 74 65  c void minmaxSte
2173c 70 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e  p(.  sqlite3_con
2173d 74 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a  text *context, .
2173e 20 20 69 6e 74 20 4e 6f 74 55 73 65 64 2c 20 0a    int NotUsed, .
2173f 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
21740 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 4d 65 6d 20  **argv.){.  Mem 
21741 2a 70 41 72 67 20 20 3d 20 28 4d 65 6d 20 2a 29  *pArg  = (Mem *)
21742 61 72 67 76 5b 30 5d 3b 0a 20 20 4d 65 6d 20 2a  argv[0];.  Mem *
21743 70 42 65 73 74 3b 0a 20 20 55 4e 55 53 45 44 5f  pBest;.  UNUSED_
21744 50 41 52 41 4d 45 54 45 52 28 4e 6f 74 55 73 65  PARAMETER(NotUse
21745 64 29 3b 0a 0a 20 20 69 66 28 20 73 71 6c 69 74  d);..  if( sqlit
21746 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
21747 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
21748 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ULL ) return;.  
21749 70 42 65 73 74 20 3d 20 28 4d 65 6d 20 2a 29 73  pBest = (Mem *)s
2174a 71 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65  qlite3_aggregate
2174b 5f 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74  _context(context
2174c 2c 20 73 69 7a 65 6f 66 28 2a 70 42 65 73 74 29  , sizeof(*pBest)
2174d 29 3b 0a 20 20 69 66 28 20 21 70 42 65 73 74 20  );.  if( !pBest 
2174e 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69 66 28  ) return;..  if(
2174f 20 70 42 65 73 74 2d 3e 66 6c 61 67 73 20 29 7b   pBest->flags ){
21750 0a 20 20 20 20 69 6e 74 20 6d 61 78 3b 0a 20 20  .    int max;.  
21751 20 20 69 6e 74 20 63 6d 70 3b 0a 20 20 20 20 43    int cmp;.    C
21752 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20 3d 20  ollSeq *pColl = 
21753 73 71 6c 69 74 65 33 47 65 74 46 75 6e 63 43 6f  sqlite3GetFuncCo
21754 6c 6c 53 65 71 28 63 6f 6e 74 65 78 74 29 3b 0a  llSeq(context);.
21755 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74 65 70      /* This step
21756 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73 65   function is use
21757 64 20 66 6f 72 20 62 6f 74 68 20 74 68 65 20 6d  d for both the m
21758 69 6e 28 29 20 61 6e 64 20 6d 61 78 28 29 20 61  in() and max() a
21759 67 67 72 65 67 61 74 65 73 2c 0a 20 20 20 20 2a  ggregates,.    *
2175a 2a 20 74 68 65 20 6f 6e 6c 79 20 64 69 66 66 65  * the only diffe
2175b 72 65 6e 63 65 20 62 65 74 77 65 65 6e 20 74 68  rence between th
2175c 65 20 74 77 6f 20 62 65 69 6e 67 20 74 68 61 74  e two being that
2175d 20 74 68 65 20 73 65 6e 73 65 20 6f 66 20 74 68   the sense of th
2175e 65 0a 20 20 20 20 2a 2a 20 63 6f 6d 70 61 72 69  e.    ** compari
2175f 73 6f 6e 20 69 73 20 69 6e 76 65 72 74 65 64 2e  son is inverted.
21760 20 46 6f 72 20 74 68 65 20 6d 61 78 28 29 20 61   For the max() a
21761 67 67 72 65 67 61 74 65 2c 20 74 68 65 0a 20 20  ggregate, the.  
21762 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f 75 73 65    ** sqlite3_use
21763 72 5f 64 61 74 61 28 29 20 66 75 6e 63 74 69 6f  r_data() functio
21764 6e 20 72 65 74 75 72 6e 73 20 28 76 6f 69 64 20  n returns (void 
21765 2a 29 2d 31 2e 20 46 6f 72 20 6d 69 6e 28 29 20  *)-1. For min() 
21766 69 74 0a 20 20 20 20 2a 2a 20 72 65 74 75 72 6e  it.    ** return
21767 73 20 28 76 6f 69 64 20 2a 29 64 62 2c 20 77 68  s (void *)db, wh
21768 65 72 65 20 64 62 20 69 73 20 74 68 65 20 73 71  ere db is the sq
21769 6c 69 74 65 33 2a 20 64 61 74 61 62 61 73 65 20  lite3* database 
2176a 70 6f 69 6e 74 65 72 2e 0a 20 20 20 20 2a 2a 20  pointer..    ** 
2176b 54 68 65 72 65 66 6f 72 65 20 74 68 65 20 6e 65  Therefore the ne
2176c 78 74 20 73 74 61 74 65 6d 65 6e 74 20 73 65 74  xt statement set
2176d 73 20 76 61 72 69 61 62 6c 65 20 27 6d 61 78 27  s variable 'max'
2176e 20 74 6f 20 31 20 66 6f 72 20 74 68 65 20 6d 61   to 1 for the ma
2176f 78 28 29 0a 20 20 20 20 2a 2a 20 61 67 67 72 65  x().    ** aggre
21770 67 61 74 65 2c 20 6f 72 20 30 20 66 6f 72 20 6d  gate, or 0 for m
21771 69 6e 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  in()..    */.   
21772 20 6d 61 78 20 3d 20 73 71 6c 69 74 65 33 5f 75   max = sqlite3_u
21773 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78 74  ser_data(context
21774 29 21 3d 30 3b 0a 20 20 20 20 63 6d 70 20 3d 20  )!=0;.    cmp = 
21775 73 71 6c 69 74 65 33 4d 65 6d 43 6f 6d 70 61 72  sqlite3MemCompar
21776 65 28 70 42 65 73 74 2c 20 70 41 72 67 2c 20 70  e(pBest, pArg, p
21777 43 6f 6c 6c 29 3b 0a 20 20 20 20 69 66 28 20 28  Coll);.    if( (
21778 6d 61 78 20 26 26 20 63 6d 70 3c 30 29 20 7c 7c  max && cmp<0) ||
21779 20 28 21 6d 61 78 20 26 26 20 63 6d 70 3e 30 29   (!max && cmp>0)
2177a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2177b 33 56 64 62 65 4d 65 6d 43 6f 70 79 28 70 42 65  3VdbeMemCopy(pBe
2177c 73 74 2c 20 70 41 72 67 29 3b 0a 20 20 20 20 7d  st, pArg);.    }
2177d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
2177e 6c 69 74 65 33 56 64 62 65 4d 65 6d 43 6f 70 79  lite3VdbeMemCopy
2177f 28 70 42 65 73 74 2c 20 70 41 72 67 29 3b 0a 20  (pBest, pArg);. 
21780 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64   }.}.static void
21781 20 6d 69 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 28   minMaxFinalize(
21782 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20  sqlite3_context 
21783 2a 63 6f 6e 74 65 78 74 29 7b 0a 20 20 73 71 6c  *context){.  sql
21784 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 73  ite3_value *pRes
21785 3b 0a 20 20 70 52 65 73 20 3d 20 28 73 71 6c 69  ;.  pRes = (sqli
21786 74 65 33 5f 76 61 6c 75 65 20 2a 29 73 71 6c 69  te3_value *)sqli
21787 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f 63 6f  te3_aggregate_co
21788 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 30  ntext(context, 0
21789 29 3b 0a 20 20 69 66 28 20 70 52 65 73 20 29 7b  );.  if( pRes ){
2178a 0a 20 20 20 20 69 66 28 20 70 52 65 73 2d 3e 66  .    if( pRes->f
2178b 6c 61 67 73 20 29 7b 0a 20 20 20 20 20 20 73 71  lags ){.      sq
2178c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 76 61 6c  lite3_result_val
2178d 75 65 28 63 6f 6e 74 65 78 74 2c 20 70 52 65 73  ue(context, pRes
2178e 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  );.    }.    sql
2178f 69 74 65 33 56 64 62 65 4d 65 6d 52 65 6c 65 61  ite3VdbeMemRelea
21790 73 65 28 70 52 65 73 29 3b 0a 20 20 7d 0a 7d 0a  se(pRes);.  }.}.
21791 0a 2f 2a 0a 2a 2a 20 67 72 6f 75 70 5f 63 6f 6e  ./*.** group_con
21792 63 61 74 28 45 58 50 52 2c 20 3f 53 45 50 41 52  cat(EXPR, ?SEPAR
21793 41 54 4f 52 3f 29 0a 2a 2f 0a 73 74 61 74 69 63  ATOR?).*/.static
21794 20 76 6f 69 64 20 67 72 6f 75 70 43 6f 6e 63 61   void groupConca
21795 74 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65 33  tStep(.  sqlite3
21796 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78  _context *contex
21797 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20  t,.  int argc,. 
21798 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a   sqlite3_value *
21799 2a 61 72 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74  *argv.){.  const
2179a 20 63 68 61 72 20 2a 7a 56 61 6c 3b 0a 20 20 53   char *zVal;.  S
2179b 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75 6d 3b  trAccum *pAccum;
2179c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2179d 53 65 70 3b 0a 20 20 69 6e 74 20 6e 56 61 6c 2c  Sep;.  int nVal,
2179e 20 6e 53 65 70 2c 20 69 3b 0a 20 20 69 66 28 20   nSep, i;.  if( 
2179f 61 72 67 63 3d 3d 30 20 7c 7c 20 73 71 6c 69 74  argc==0 || sqlit
217a0 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
217a1 67 76 5b 30 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  gv[0])==SQLITE_N
217a2 55 4c 4c 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ULL ) return;.  
217a3 70 41 63 63 75 6d 20 3d 20 28 53 74 72 41 63 63  pAccum = (StrAcc
217a4 75 6d 2a 29 73 71 6c 69 74 65 33 5f 61 67 67 72  um*)sqlite3_aggr
217a5 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 28 63 6f  egate_context(co
217a6 6e 74 65 78 74 2c 20 73 69 7a 65 6f 66 28 2a 70  ntext, sizeof(*p
217a7 41 63 63 75 6d 29 29 3b 0a 0a 20 20 69 66 28 20  Accum));..  if( 
217a8 70 41 63 63 75 6d 20 29 7b 0a 20 20 20 20 73 71  pAccum ){.    sq
217a9 6c 69 74 65 33 20 2a 64 62 20 3d 20 73 71 6c 69  lite3 *db = sqli
217aa 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64 62 5f 68  te3_context_db_h
217ab 61 6e 64 6c 65 28 63 6f 6e 74 65 78 74 29 3b 0a  andle(context);.
217ac 20 20 20 20 70 41 63 63 75 6d 2d 3e 75 73 65 4d      pAccum->useM
217ad 61 6c 6c 6f 63 20 3d 20 31 3b 0a 20 20 20 20 70  alloc = 1;.    p
217ae 41 63 63 75 6d 2d 3e 6d 78 41 6c 6c 6f 63 20 3d  Accum->mxAlloc =
217af 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
217b0 54 45 5f 4c 49 4d 49 54 5f 4c 45 4e 47 54 48 5d  TE_LIMIT_LENGTH]
217b1 3b 0a 20 20 20 20 69 66 28 20 70 41 63 63 75 6d  ;.    if( pAccum
217b2 2d 3e 6e 43 68 61 72 20 29 7b 0a 20 20 20 20 20  ->nChar ){.     
217b3 20 69 66 28 20 61 72 67 63 3e 31 20 29 7b 0a 20   if( argc>1 ){. 
217b4 20 20 20 20 20 20 20 7a 53 65 70 20 3d 20 28 63         zSep = (c
217b5 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c  har*)sqlite3_val
217b6 75 65 5f 74 65 78 74 28 61 72 67 76 5b 61 72 67  ue_text(argv[arg
217b7 63 2d 31 5d 29 3b 0a 20 20 20 20 20 20 20 20 6e  c-1]);.        n
217b8 53 65 70 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  Sep = sqlite3_va
217b9 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 61  lue_bytes(argv[a
217ba 72 67 63 2d 31 5d 29 3b 0a 20 20 20 20 20 20 7d  rgc-1]);.      }
217bb 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 7a 53  else{.        zS
217bc 65 70 20 3d 20 22 2c 22 3b 0a 20 20 20 20 20 20  ep = ",";.      
217bd 20 20 6e 53 65 70 20 3d 20 31 3b 0a 20 20 20 20    nSep = 1;.    
217be 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
217bf 33 53 74 72 41 63 63 75 6d 41 70 70 65 6e 64 28  3StrAccumAppend(
217c0 70 41 63 63 75 6d 2c 20 7a 53 65 70 2c 20 6e 53  pAccum, zSep, nS
217c1 65 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  ep);.    }.    i
217c2 20 3d 20 30 3b 0a 20 20 20 20 64 6f 7b 0a 20 20   = 0;.    do{.  
217c3 20 20 20 20 7a 56 61 6c 20 3d 20 28 63 68 61 72      zVal = (char
217c4 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
217c5 74 65 78 74 28 61 72 67 76 5b 69 5d 29 3b 0a 20  text(argv[i]);. 
217c6 20 20 20 20 20 6e 56 61 6c 20 3d 20 73 71 6c 69       nVal = sqli
217c7 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28  te3_value_bytes(
217c8 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 20 20  argv[i]);.      
217c9 73 71 6c 69 74 65 33 53 74 72 41 63 63 75 6d 41  sqlite3StrAccumA
217ca 70 70 65 6e 64 28 70 41 63 63 75 6d 2c 20 7a 56  ppend(pAccum, zV
217cb 61 6c 2c 20 6e 56 61 6c 29 3b 0a 20 20 20 20 20  al, nVal);.     
217cc 20 69 2b 2b 3b 0a 20 20 20 20 7d 77 68 69 6c 65   i++;.    }while
217cd 28 20 69 3c 61 72 67 63 2d 31 20 29 3b 0a 20 20  ( i<argc-1 );.  
217ce 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  }.}.static void 
217cf 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
217d0 69 7a 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ize(sqlite3_cont
217d1 65 78 74 20 2a 63 6f 6e 74 65 78 74 29 7b 0a 20  ext *context){. 
217d2 20 53 74 72 41 63 63 75 6d 20 2a 70 41 63 63 75   StrAccum *pAccu
217d3 6d 3b 0a 20 20 70 41 63 63 75 6d 20 3d 20 73 71  m;.  pAccum = sq
217d4 6c 69 74 65 33 5f 61 67 67 72 65 67 61 74 65 5f  lite3_aggregate_
217d5 63 6f 6e 74 65 78 74 28 63 6f 6e 74 65 78 74 2c  context(context,
217d6 20 30 29 3b 0a 20 20 69 66 28 20 70 41 63 63 75   0);.  if( pAccu
217d7 6d 20 29 7b 0a 20 20 20 20 69 66 28 20 70 41 63  m ){.    if( pAc
217d8 63 75 6d 2d 3e 74 6f 6f 42 69 67 20 29 7b 0a 20  cum->tooBig ){. 
217d9 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73       sqlite3_res
217da 75 6c 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67  ult_error_toobig
217db 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 7d  (context);.    }
217dc 65 6c 73 65 20 69 66 28 20 70 41 63 63 75 6d 2d  else if( pAccum-
217dd 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
217de 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 72  .      sqlite3_r
217df 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65  esult_error_nome
217e0 6d 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20  m(context);.    
217e1 7d 65 6c 73 65 7b 20 20 20 20 0a 20 20 20 20 20  }else{    .     
217e2 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
217e3 74 65 78 74 28 63 6f 6e 74 65 78 74 2c 20 73 71  text(context, sq
217e4 6c 69 74 65 33 53 74 72 41 63 63 75 6d 46 69 6e  lite3StrAccumFin
217e5 69 73 68 28 70 41 63 63 75 6d 29 2c 20 2d 31 2c  ish(pAccum), -1,
217e6 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
217e7 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
217e8 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 20 20 7d  te3_free);.    }
217e9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
217ea 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 67 69  is function regi
217eb 73 74 65 72 65 64 20 61 6c 6c 20 6f 66 20 74 68  stered all of th
217ec 65 20 61 62 6f 76 65 20 43 20 66 75 6e 63 74 69  e above C functi
217ed 6f 6e 73 20 61 73 20 53 51 4c 0a 2a 2a 20 66 75  ons as SQL.** fu
217ee 6e 63 74 69 6f 6e 73 2e 20 20 54 68 69 73 20 73  nctions.  This s
217ef 68 6f 75 6c 64 20 62 65 20 74 68 65 20 6f 6e 6c  hould be the onl
217f0 79 20 72 6f 75 74 69 6e 65 20 69 6e 20 74 68 69  y routine in thi
217f1 73 20 66 69 6c 65 20 77 69 74 68 0a 2a 2a 20 65  s file with.** e
217f2 78 74 65 72 6e 61 6c 20 6c 69 6e 6b 61 67 65 2e  xternal linkage.
217f3 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
217f4 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 52  TE void sqlite3R
217f5 65 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75  egisterBuiltinFu
217f6 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65 33 20  nctions(sqlite3 
217f7 2a 64 62 29 7b 0a 23 69 66 6e 64 65 66 20 53 51  *db){.#ifndef SQ
217f8 4c 49 54 45 5f 4f 4d 49 54 5f 41 4c 54 45 52 54  LITE_OMIT_ALTERT
217f9 41 42 4c 45 0a 20 20 73 71 6c 69 74 65 33 41 6c  ABLE.  sqlite3Al
217fa 74 65 72 46 75 6e 63 74 69 6f 6e 73 28 64 62 29  terFunctions(db)
217fb 3b 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20 21  ;.#endif.  if( !
217fc 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
217fd 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d   ){.    int rc =
217fe 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61   sqlite3_overloa
217ff 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22  d_function(db, "
21800 4d 41 54 43 48 22 2c 20 32 29 3b 0a 20 20 20 20  MATCH", 2);.    
21801 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49  assert( rc==SQLI
21802 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20 72 63 3d 3d  TE_NOMEM || rc==
21803 53 51 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 20  SQLITE_OK );.   
21804 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
21805 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 64  NOMEM ){.      d
21806 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
21807 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23  = 1;.    }.  }.#
21808 69 66 64 65 66 20 53 51 4c 49 54 45 5f 53 53 45  ifdef SQLITE_SSE
21809 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65 33  .  (void)sqlite3
2180a 53 73 65 46 75 6e 63 74 69 6f 6e 73 28 64 62 29  SseFunctions(db)
2180b 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  ;.#endif.}../*.*
2180c 2a 20 53 65 74 20 74 68 65 20 4c 49 4b 45 4f 50  * Set the LIKEOP
2180d 54 20 66 6c 61 67 20 6f 6e 20 74 68 65 20 32 2d  T flag on the 2-
2180e 61 72 67 75 6d 65 6e 74 20 66 75 6e 63 74 69 6f  argument functio
2180f 6e 20 77 69 74 68 20 74 68 65 20 67 69 76 65 6e  n with the given
21810 20 6e 61 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63   name..*/.static
21811 20 76 6f 69 64 20 73 65 74 4c 69 6b 65 4f 70 74   void setLikeOpt
21812 46 6c 61 67 28 73 71 6c 69 74 65 33 20 2a 64 62  Flag(sqlite3 *db
21813 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e  , const char *zN
21814 61 6d 65 2c 20 69 6e 74 20 66 6c 61 67 56 61 6c  ame, int flagVal
21815 29 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 44  ){.  FuncDef *pD
21816 65 66 3b 0a 20 20 70 44 65 66 20 3d 20 73 71 6c  ef;.  pDef = sql
21817 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
21818 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 73 74 72 6c  (db, zName, strl
21819 65 6e 28 7a 4e 61 6d 65 29 2c 20 32 2c 20 53 51  en(zName), 2, SQ
2181a 4c 49 54 45 5f 55 54 46 38 2c 20 30 29 3b 0a 20  LITE_UTF8, 0);. 
2181b 20 69 66 28 20 70 44 65 66 20 29 7b 0a 20 20 20   if( pDef ){.   
2181c 20 70 44 65 66 2d 3e 66 6c 61 67 73 20 3d 20 66   pDef->flags = f
2181d 6c 61 67 56 61 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f  lagVal;.  }.}../
2181e 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68  *.** Register th
2181f 65 20 62 75 69 6c 74 2d 69 6e 20 4c 49 4b 45 20  e built-in LIKE 
21820 61 6e 64 20 47 4c 4f 42 20 66 75 6e 63 74 69 6f  and GLOB functio
21821 6e 73 2e 20 20 54 68 65 20 63 61 73 65 53 65 6e  ns.  The caseSen
21822 73 69 74 69 76 65 0a 2a 2a 20 70 61 72 61 6d 65  sitive.** parame
21823 74 65 72 20 64 65 74 65 72 6d 69 6e 65 73 20 77  ter determines w
21824 68 65 74 68 65 72 20 6f 72 20 6e 6f 74 20 74 68  hether or not th
21825 65 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f 72 20  e LIKE operator 
21826 69 73 20 63 61 73 65 0a 2a 2a 20 73 65 6e 73 69  is case.** sensi
21827 74 69 76 65 2e 20 20 47 4c 4f 42 20 69 73 20 61  tive.  GLOB is a
21828 6c 77 61 79 73 20 63 61 73 65 20 73 65 6e 73 69  lways case sensi
21829 74 69 76 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  tive..*/.SQLITE_
2182a 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
2182b 69 74 65 33 52 65 67 69 73 74 65 72 4c 69 6b 65  ite3RegisterLike
2182c 46 75 6e 63 74 69 6f 6e 73 28 73 71 6c 69 74 65  Functions(sqlite
2182d 33 20 2a 64 62 2c 20 69 6e 74 20 63 61 73 65 53  3 *db, int caseS
2182e 65 6e 73 69 74 69 76 65 29 7b 0a 20 20 73 74 72  ensitive){.  str
2182f 75 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 20  uct compareInfo 
21830 2a 70 49 6e 66 6f 3b 0a 20 20 69 66 28 20 63 61  *pInfo;.  if( ca
21831 73 65 53 65 6e 73 69 74 69 76 65 20 29 7b 0a 20  seSensitive ){. 
21832 20 20 20 70 49 6e 66 6f 20 3d 20 28 73 74 72 75     pInfo = (stru
21833 63 74 20 63 6f 6d 70 61 72 65 49 6e 66 6f 2a 29  ct compareInfo*)
21834 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 3b 0a 20 20  &likeInfoAlt;.  
21835 7d 65 6c 73 65 7b 0a 20 20 20 20 70 49 6e 66 6f  }else{.    pInfo
21836 20 3d 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61   = (struct compa
21837 72 65 49 6e 66 6f 2a 29 26 6c 69 6b 65 49 6e 66  reInfo*)&likeInf
21838 6f 4e 6f 72 6d 3b 0a 20 20 7d 0a 20 20 73 71 6c  oNorm;.  }.  sql
21839 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
2183a 62 2c 20 22 6c 69 6b 65 22 2c 20 32 2c 20 53 51  b, "like", 2, SQ
2183b 4c 49 54 45 5f 55 54 46 38 2c 20 70 49 6e 66 6f  LITE_UTF8, pInfo
2183c 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 20 30  , likeFunc, 0, 0
2183d 29 3b 0a 20 20 73 71 6c 69 74 65 33 43 72 65 61  );.  sqlite3Crea
2183e 74 65 46 75 6e 63 28 64 62 2c 20 22 6c 69 6b 65  teFunc(db, "like
2183f 22 2c 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46  ", 3, SQLITE_UTF
21840 38 2c 20 70 49 6e 66 6f 2c 20 6c 69 6b 65 46 75  8, pInfo, likeFu
21841 6e 63 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c  nc, 0, 0);.  sql
21842 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
21843 62 2c 20 22 67 6c 6f 62 22 2c 20 32 2c 20 53 51  b, "glob", 2, SQ
21844 4c 49 54 45 5f 55 54 46 38 2c 20 0a 20 20 20 20  LITE_UTF8, .    
21845 20 20 28 73 74 72 75 63 74 20 63 6f 6d 70 61 72    (struct compar
21846 65 49 6e 66 6f 2a 29 26 67 6c 6f 62 49 6e 66 6f  eInfo*)&globInfo
21847 2c 20 6c 69 6b 65 46 75 6e 63 2c 20 30 2c 30 29  , likeFunc, 0,0)
21848 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70 74 46 6c  ;.  setLikeOptFl
21849 61 67 28 64 62 2c 20 22 67 6c 6f 62 22 2c 20 53  ag(db, "glob", S
2184a 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 20  QLITE_FUNC_LIKE 
2184b 7c 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41  | SQLITE_FUNC_CA
2184c 53 45 29 3b 0a 20 20 73 65 74 4c 69 6b 65 4f 70  SE);.  setLikeOp
2184d 74 46 6c 61 67 28 64 62 2c 20 22 6c 69 6b 65 22  tFlag(db, "like"
2184e 2c 20 0a 20 20 20 20 20 20 63 61 73 65 53 65 6e  , .      caseSen
2184f 73 69 74 69 76 65 20 3f 20 28 53 51 4c 49 54 45  sitive ? (SQLITE
21850 5f 46 55 4e 43 5f 4c 49 4b 45 20 7c 20 53 51 4c  _FUNC_LIKE | SQL
21851 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29 20 3a  ITE_FUNC_CASE) :
21852 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b   SQLITE_FUNC_LIK
21853 45 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 78  E);.}../*.** pEx
21854 70 72 20 70 6f 69 6e 74 73 20 74 6f 20 61 6e 20  pr points to an 
21855 65 78 70 72 65 73 73 69 6f 6e 20 77 68 69 63 68  expression which
21856 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 66 75   implements a fu
21857 6e 63 74 69 6f 6e 2e 20 20 49 66 0a 2a 2a 20 69  nction.  If.** i
21858 74 20 69 73 20 61 70 70 72 6f 70 72 69 61 74 65  t is appropriate
21859 20 74 6f 20 61 70 70 6c 79 20 74 68 65 20 4c 49   to apply the LI
2185a 4b 45 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  KE optimization 
2185b 74 6f 20 74 68 61 74 20 66 75 6e 63 74 69 6f 6e  to that function
2185c 0a 2a 2a 20 74 68 65 6e 20 73 65 74 20 61 57 63  .** then set aWc
2185d 5b 30 5d 20 74 68 72 6f 75 67 68 20 61 57 63 5b  [0] through aWc[
2185e 32 5d 20 74 6f 20 74 68 65 20 77 69 6c 64 63 61  2] to the wildca
2185f 72 64 20 63 68 61 72 61 63 74 65 72 73 20 61 6e  rd characters an
21860 64 0a 2a 2a 20 72 65 74 75 72 6e 20 54 52 55 45  d.** return TRUE
21861 2e 20 20 49 66 20 74 68 65 20 66 75 6e 63 74 69  .  If the functi
21862 6f 6e 20 69 73 20 6e 6f 74 20 61 20 4c 49 4b 45  on is not a LIKE
21863 2d 73 74 79 6c 65 20 66 75 6e 63 74 69 6f 6e 20  -style function 
21864 74 68 65 6e 0a 2a 2a 20 72 65 74 75 72 6e 20 46  then.** return F
21865 41 4c 53 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ALSE..*/.SQLITE_
21866 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
21867 74 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f  te3IsLikeFunctio
21868 6e 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 45  n(sqlite3 *db, E
21869 78 70 72 20 2a 70 45 78 70 72 2c 20 69 6e 74 20  xpr *pExpr, int 
2186a 2a 70 49 73 4e 6f 63 61 73 65 2c 20 63 68 61 72  *pIsNocase, char
2186b 20 2a 61 57 63 29 7b 0a 20 20 46 75 6e 63 44 65   *aWc){.  FuncDe
2186c 66 20 2a 70 44 65 66 3b 0a 20 20 69 66 28 20 70  f *pDef;.  if( p
2186d 45 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 46 55 4e  Expr->op!=TK_FUN
2186e 43 54 49 4f 4e 20 7c 7c 20 21 70 45 78 70 72 2d  CTION || !pExpr-
2186f 3e 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 72 65  >pList ){.    re
21870 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
21871 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e  ( pExpr->pList->
21872 6e 45 78 70 72 21 3d 32 20 29 7b 0a 20 20 20 20  nExpr!=2 ){.    
21873 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
21874 70 44 65 66 20 3d 20 73 71 6c 69 74 65 33 46 69  pDef = sqlite3Fi
21875 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62 2c 20 28  ndFunction(db, (
21876 63 68 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b  char*)pExpr->tok
21877 65 6e 2e 7a 2c 20 70 45 78 70 72 2d 3e 74 6f 6b  en.z, pExpr->tok
21878 65 6e 2e 6e 2c 20 32 2c 0a 20 20 20 20 20 20 20  en.n, 2,.       
21879 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2187a 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46        SQLITE_UTF
2187b 38 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 44 65  8, 0);.  if( pDe
2187c 66 3d 3d 30 20 7c 7c 20 28 70 44 65 66 2d 3e 66  f==0 || (pDef->f
2187d 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 55  lags & SQLITE_FU
2187e 4e 43 5f 4c 49 4b 45 29 3d 3d 30 20 29 7b 0a 20  NC_LIKE)==0 ){. 
2187f 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
21880 0a 0a 20 20 2f 2a 20 54 68 65 20 6d 65 6d 63 70  ..  /* The memcp
21881 79 28 29 20 73 74 61 74 65 6d 65 6e 74 20 61 73  y() statement as
21882 73 75 6d 65 73 20 74 68 61 74 20 74 68 65 20 77  sumes that the w
21883 69 6c 64 63 61 72 64 20 63 68 61 72 61 63 74 65  ildcard characte
21884 72 73 20 61 72 65 0a 20 20 2a 2a 20 74 68 65 20  rs are.  ** the 
21885 66 69 72 73 74 20 74 68 72 65 65 20 73 74 61 74  first three stat
21886 65 6d 65 6e 74 73 20 69 6e 20 74 68 65 20 63 6f  ements in the co
21887 6d 70 61 72 65 49 6e 66 6f 20 73 74 72 75 63 74  mpareInfo struct
21888 75 72 65 2e 20 20 54 68 65 0a 20 20 2a 2a 20 61  ure.  The.  ** a
21889 73 73 65 72 74 73 28 29 20 74 68 61 74 20 66 6f  sserts() that fo
2188a 6c 6c 6f 77 20 76 65 72 69 66 79 20 74 68 61 74  llow verify that
2188b 20 61 73 73 75 6d 70 74 69 6f 6e 0a 20 20 2a 2f   assumption.  */
2188c 0a 20 20 6d 65 6d 63 70 79 28 61 57 63 2c 20 70  .  memcpy(aWc, p
2188d 44 65 66 2d 3e 70 55 73 65 72 44 61 74 61 2c 20  Def->pUserData, 
2188e 33 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 63  3);.  assert( (c
2188f 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c  har*)&likeInfoAl
21890 74 20 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b  t == (char*)&lik
21891 65 49 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 41 6c  eInfoAlt.matchAl
21892 6c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 26  l );.  assert( &
21893 28 28 63 68 61 72 2a 29 26 6c 69 6b 65 49 6e 66  ((char*)&likeInf
21894 6f 41 6c 74 29 5b 31 5d 20 3d 3d 20 28 63 68 61  oAlt)[1] == (cha
21895 72 2a 29 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2e  r*)&likeInfoAlt.
21896 6d 61 74 63 68 4f 6e 65 20 29 3b 0a 20 20 61 73  matchOne );.  as
21897 73 65 72 74 28 20 26 28 28 63 68 61 72 2a 29 26  sert( &((char*)&
21898 6c 69 6b 65 49 6e 66 6f 41 6c 74 29 5b 32 5d 20  likeInfoAlt)[2] 
21899 3d 3d 20 28 63 68 61 72 2a 29 26 6c 69 6b 65 49  == (char*)&likeI
2189a 6e 66 6f 41 6c 74 2e 6d 61 74 63 68 53 65 74 20  nfoAlt.matchSet 
2189b 29 3b 0a 20 20 2a 70 49 73 4e 6f 63 61 73 65 20  );.  *pIsNocase 
2189c 3d 20 28 70 44 65 66 2d 3e 66 6c 61 67 73 20 26  = (pDef->flags &
2189d 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53   SQLITE_FUNC_CAS
2189e 45 29 3d 3d 30 3b 0a 20 20 72 65 74 75 72 6e 20  E)==0;.  return 
2189f 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 20  1;.}../*.** All 
218a0 61 6c 6c 20 6f 66 20 74 68 65 20 46 75 6e 63 44  all of the FuncD
218a1 65 66 20 73 74 72 75 63 74 75 72 65 73 20 69 6e  ef structures in
218a2 20 74 68 65 20 61 42 75 69 6c 74 69 6e 46 75 6e   the aBuiltinFun
218a3 63 5b 5d 20 61 72 72 61 79 20 61 62 6f 76 65 0a  c[] array above.
218a4 2a 2a 20 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c  ** to the global
218a5 20 66 75 6e 63 74 69 6f 6e 20 68 61 73 68 20 74   function hash t
218a6 61 62 6c 65 2e 20 20 54 68 69 73 20 6f 63 63 75  able.  This occu
218a7 72 73 20 61 74 20 73 74 61 72 74 2d 74 69 6d 65  rs at start-time
218a8 20 28 61 73 0a 2a 2a 20 61 20 63 6f 6e 73 65 71   (as.** a conseq
218a9 75 65 6e 63 65 20 6f 66 20 63 61 6c 6c 69 6e 67  uence of calling
218aa 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
218ab 69 7a 65 28 29 29 2e 0a 2a 2a 0a 2a 2a 20 41 66  ize())..**.** Af
218ac 74 65 72 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ter this routine
218ad 20 72 75 6e 73 0a 2a 2f 0a 53 51 4c 49 54 45 5f   runs.*/.SQLITE_
218ae 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
218af 69 74 65 33 52 65 67 69 73 74 65 72 47 6c 6f 62  ite3RegisterGlob
218b0 61 6c 46 75 6e 63 74 69 6f 6e 73 28 76 6f 69 64  alFunctions(void
218b1 29 7b 0a 20 20 2f 2a 0a 20 20 2a 2a 20 54 68 65  ){.  /*.  ** The
218b2 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 72 72 61 79   following array
218b3 20 68 6f 6c 64 73 20 46 75 6e 63 44 65 66 20 73   holds FuncDef s
218b4 74 72 75 63 74 75 72 65 73 20 66 6f 72 20 61 6c  tructures for al
218b5 6c 20 6f 66 20 74 68 65 20 66 75 6e 63 74 69 6f  l of the functio
218b6 6e 73 0a 20 20 2a 2a 20 64 65 66 69 6e 65 64 20  ns.  ** defined 
218b7 69 6e 20 74 68 69 73 20 66 69 6c 65 2e 0a 20 20  in this file..  
218b8 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 61 72 72 61  **.  ** The arra
218b9 79 20 63 61 6e 6e 6f 74 20 62 65 20 63 6f 6e 73  y cannot be cons
218ba 74 61 6e 74 20 73 69 6e 63 65 20 63 68 61 6e 67  tant since chang
218bb 65 73 20 61 72 65 20 6d 61 64 65 20 74 6f 20 74  es are made to t
218bc 68 65 0a 20 20 2a 2a 20 46 75 6e 63 44 65 66 2e  he.  ** FuncDef.
218bd 70 48 61 73 68 20 65 6c 65 6d 65 6e 74 73 20 61  pHash elements a
218be 74 20 73 74 61 72 74 2d 74 69 6d 65 2e 20 20 54  t start-time.  T
218bf 68 65 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74  he elements of t
218c0 68 69 73 20 61 72 72 61 79 0a 20 20 2a 2a 20 61  his array.  ** a
218c1 72 65 20 72 65 61 64 2d 6f 6e 6c 79 20 61 66 74  re read-only aft
218c2 65 72 20 69 6e 69 74 69 61 6c 69 7a 61 74 69 6f  er initializatio
218c3 6e 20 69 73 20 63 6f 6d 70 6c 65 74 65 2e 0a 20  n is complete.. 
218c4 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 53 51 4c   */.  static SQL
218c5 49 54 45 5f 57 53 44 20 46 75 6e 63 44 65 66 20  ITE_WSD FuncDef 
218c6 61 42 75 69 6c 74 69 6e 46 75 6e 63 5b 5d 20 3d  aBuiltinFunc[] =
218c7 20 7b 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28   {.    FUNCTION(
218c8 6c 74 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20  ltrim,          
218c9 20 20 20 20 31 2c 20 31 2c 20 30 2c 20 74 72 69      1, 1, 0, tri
218ca 6d 46 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c  mFunc         ),
218cb 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6c 74  .    FUNCTION(lt
218cc 72 69 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  rim,            
218cd 20 20 32 2c 20 31 2c 20 30 2c 20 74 72 69 6d 46    2, 1, 0, trimF
218ce 75 6e 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20  unc         ),. 
218cf 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69     FUNCTION(rtri
218d0 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  m,              
218d1 31 2c 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e  1, 2, 0, trimFun
218d2 63 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  c         ),.   
218d3 20 46 55 4e 43 54 49 4f 4e 28 72 74 72 69 6d 2c   FUNCTION(rtrim,
218d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
218d5 20 32 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20   2, 0, trimFunc 
218d6 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
218d7 55 4e 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20  UNCTION(trim,   
218d8 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 33              1, 3
218d9 2c 20 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20  , 0, trimFunc   
218da 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
218db 43 54 49 4f 4e 28 74 72 69 6d 2c 20 20 20 20 20  CTION(trim,     
218dc 20 20 20 20 20 20 20 20 20 20 32 2c 20 33 2c 20            2, 3, 
218dd 30 2c 20 74 72 69 6d 46 75 6e 63 20 20 20 20 20  0, trimFunc     
218de 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
218df 49 4f 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20  ION(min,        
218e0 20 20 20 20 20 20 20 2d 31 2c 20 30 2c 20 31 2c         -1, 0, 1,
218e1 20 6d 69 6e 6d 61 78 46 75 6e 63 20 20 20 20 20   minmaxFunc     
218e2 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
218e3 4e 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20  N(min,          
218e4 20 20 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 30        0, 0, 1, 0
218e5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
218e6 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41 54 45  ),.    AGGREGATE
218e7 28 6d 69 6e 2c 20 20 20 20 20 20 20 20 20 20 20  (min,           
218e8 20 20 20 20 31 2c 20 30 2c 20 31 2c 20 6d 69 6e      1, 0, 1, min
218e9 6d 61 78 53 74 65 70 2c 20 20 20 20 20 20 6d 69  maxStep,      mi
218ea 6e 4d 61 78 46 69 6e 61 6c 69 7a 65 20 29 2c 0a  nMaxFinalize ),.
218eb 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78      FUNCTION(max
218ec 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
218ed 2d 31 2c 20 31 2c 20 31 2c 20 6d 69 6e 6d 61 78  -1, 1, 1, minmax
218ee 46 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20  Func       ),.  
218ef 20 20 46 55 4e 43 54 49 4f 4e 28 6d 61 78 2c 20    FUNCTION(max, 
218f0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
218f1 2c 20 31 2c 20 31 2c 20 30 20 20 20 20 20 20 20  , 1, 1, 0       
218f2 20 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20           ),.    
218f3 41 47 47 52 45 47 41 54 45 28 6d 61 78 2c 20 20  AGGREGATE(max,  
218f4 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20               1, 
218f5 31 2c 20 31 2c 20 6d 69 6e 6d 61 78 53 74 65 70  1, 1, minmaxStep
218f6 2c 20 20 20 20 20 20 6d 69 6e 4d 61 78 46 69 6e  ,      minMaxFin
218f7 61 6c 69 7a 65 20 29 2c 0a 20 20 20 20 46 55 4e  alize ),.    FUN
218f8 43 54 49 4f 4e 28 74 79 70 65 6f 66 2c 20 20 20  CTION(typeof,   
218f9 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
218fa 30 2c 20 74 79 70 65 6f 66 46 75 6e 63 20 20 20  0, typeofFunc   
218fb 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
218fc 49 4f 4e 28 6c 65 6e 67 74 68 2c 20 20 20 20 20  ION(length,     
218fd 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
218fe 20 6c 65 6e 67 74 68 46 75 6e 63 20 20 20 20 20   lengthFunc     
218ff 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
21900 4e 28 73 75 62 73 74 72 2c 20 20 20 20 20 20 20  N(substr,       
21901 20 20 20 20 20 20 32 2c 20 30 2c 20 30 2c 20 73        2, 0, 0, s
21902 75 62 73 74 72 46 75 6e 63 20 20 20 20 20 20 20  ubstrFunc       
21903 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
21904 73 75 62 73 74 72 2c 20 20 20 20 20 20 20 20 20  substr,         
21905 20 20 20 20 33 2c 20 30 2c 20 30 2c 20 73 75 62      3, 0, 0, sub
21906 73 74 72 46 75 6e 63 20 20 20 20 20 20 20 29 2c  strFunc       ),
21907 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 61 62  .    FUNCTION(ab
21908 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  s,              
21909 20 20 31 2c 20 30 2c 20 30 2c 20 61 62 73 46 75    1, 0, 0, absFu
2190a 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20  nc          ),. 
2190b 20 20 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e     FUNCTION(roun
2190c 64 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  d,              
2190d 31 2c 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75  1, 0, 0, roundFu
2190e 6e 63 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20  nc        ),.   
2190f 20 46 55 4e 43 54 49 4f 4e 28 72 6f 75 6e 64 2c   FUNCTION(round,
21910 20 20 20 20 20 20 20 20 20 20 20 20 20 20 32 2c                2,
21911 20 30 2c 20 30 2c 20 72 6f 75 6e 64 46 75 6e 63   0, 0, roundFunc
21912 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
21913 55 4e 43 54 49 4f 4e 28 75 70 70 65 72 2c 20 20  UNCTION(upper,  
21914 20 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30              1, 0
21915 2c 20 30 2c 20 75 70 70 65 72 46 75 6e 63 20 20  , 0, upperFunc  
21916 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
21917 43 54 49 4f 4e 28 6c 6f 77 65 72 2c 20 20 20 20  CTION(lower,    
21918 20 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20            1, 0, 
21919 30 2c 20 6c 6f 77 65 72 46 75 6e 63 20 20 20 20  0, lowerFunc    
2191a 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2191b 49 4f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20  ION(coalesce,   
2191c 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c          1, 0, 0,
2191d 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
2191e 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2191f 4e 28 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20  N(coalesce,     
21920 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 69       -1, 0, 0, i
21921 66 6e 75 6c 6c 46 75 6e 63 20 20 20 20 20 20 20  fnullFunc       
21922 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
21923 63 6f 61 6c 65 73 63 65 2c 20 20 20 20 20 20 20  coalesce,       
21924 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 20 20      0, 0, 0, 0  
21925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 29 2c                ),
21926 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 68 65  .    FUNCTION(he
21927 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  x,              
21928 20 20 31 2c 20 30 2c 20 30 2c 20 68 65 78 46 75    1, 0, 0, hexFu
21929 6e 63 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20  nc          ),. 
2192a 20 20 20 46 55 4e 43 54 49 4f 4e 28 69 66 6e 75     FUNCTION(ifnu
2192b 6c 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ll,             
2192c 32 2c 20 30 2c 20 31 2c 20 69 66 6e 75 6c 6c 46  2, 0, 1, ifnullF
2192d 75 6e 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20  unc       ),.   
2192e 20 46 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d   FUNCTION(random
2192f 2c 20 20 20 20 20 20 20 20 20 20 20 20 2d 31 2c  ,            -1,
21930 20 30 2c 20 30 2c 20 72 61 6e 64 6f 6d 46 75 6e   0, 0, randomFun
21931 63 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  c       ),.    F
21932 55 4e 43 54 49 4f 4e 28 72 61 6e 64 6f 6d 62 6c  UNCTION(randombl
21933 6f 62 2c 20 20 20 20 20 20 20 20 20 31 2c 20 30  ob,         1, 0
21934 2c 20 30 2c 20 72 61 6e 64 6f 6d 42 6c 6f 62 20  , 0, randomBlob 
21935 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e        ),.    FUN
21936 43 54 49 4f 4e 28 6e 75 6c 6c 69 66 2c 20 20 20  CTION(nullif,   
21937 20 20 20 20 20 20 20 20 20 20 32 2c 20 30 2c 20            2, 0, 
21938 31 2c 20 6e 75 6c 6c 69 66 46 75 6e 63 20 20 20  1, nullifFunc   
21939 20 20 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54      ),.    FUNCT
2193a 49 4f 4e 28 73 71 6c 69 74 65 5f 76 65 72 73 69  ION(sqlite_versi
2193b 6f 6e 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c  on,     0, 0, 0,
2193c 20 76 65 72 73 69 6f 6e 46 75 6e 63 20 20 20 20   versionFunc    
2193d 20 20 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f    ),.    FUNCTIO
2193e 4e 28 71 75 6f 74 65 2c 20 20 20 20 20 20 20 20  N(quote,        
2193f 20 20 20 20 20 20 31 2c 20 30 2c 20 30 2c 20 71        1, 0, 0, q
21940 75 6f 74 65 46 75 6e 63 20 20 20 20 20 20 20 20  uoteFunc        
21941 29 2c 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28  ),.    FUNCTION(
21942 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
21943 64 2c 20 20 30 2c 20 30 2c 20 30 2c 20 6c 61 73  d,  0, 0, 0, las
21944 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69 64 29 2c  t_insert_rowid),
21945 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 63 68  .    FUNCTION(ch
21946 61 6e 67 65 73 2c 20 20 20 20 20 20 20 20 20 20  anges,          
21947 20 20 30 2c 20 30 2c 20 30 2c 20 63 68 61 6e 67    0, 0, 0, chang
21948 65 73 20 20 20 20 20 20 20 20 20 20 29 2c 0a 20  es          ),. 
21949 20 20 20 46 55 4e 43 54 49 4f 4e 28 74 6f 74 61     FUNCTION(tota
2194a 6c 5f 63 68 61 6e 67 65 73 2c 20 20 20 20 20 20  l_changes,      
2194b 30 2c 20 30 2c 20 30 2c 20 74 6f 74 61 6c 5f 63  0, 0, 0, total_c
2194c 68 61 6e 67 65 73 20 20 20 20 29 2c 0a 20 20 20  hanges    ),.   
2194d 20 46 55 4e 43 54 49 4f 4e 28 72 65 70 6c 61 63   FUNCTION(replac
2194e 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 33 2c  e,            3,
2194f 20 30 2c 20 30 2c 20 72 65 70 6c 61 63 65 46 75   0, 0, replaceFu
21950 6e 63 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46  nc      ),.    F
21951 55 4e 43 54 49 4f 4e 28 7a 65 72 6f 62 6c 6f 62  UNCTION(zeroblob
21952 2c 20 20 20 20 20 20 20 20 20 20 20 31 2c 20 30  ,           1, 0
21953 2c 20 30 2c 20 7a 65 72 6f 62 6c 6f 62 46 75 6e  , 0, zeroblobFun
21954 63 20 20 20 20 20 29 2c 0a 20 20 23 69 66 64 65  c     ),.  #ifde
21955 66 20 53 51 4c 49 54 45 5f 53 4f 55 4e 44 45 58  f SQLITE_SOUNDEX
21956 0a 20 20 20 20 46 55 4e 43 54 49 4f 4e 28 73 6f  .    FUNCTION(so
21957 75 6e 64 65 78 2c 20 20 20 20 20 20 20 20 20 20  undex,          
21958 20 20 31 2c 20 30 2c 20 30 2c 20 73 6f 75 6e 64    1, 0, 0, sound
21959 65 78 46 75 6e 63 20 20 20 20 20 20 29 2c 0a 20  exFunc      ),. 
2195a 20 23 65 6e 64 69 66 0a 20 20 23 69 66 6e 64 65   #endif.  #ifnde
2195b 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 4f  f SQLITE_OMIT_LO
2195c 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a 20 20 20  AD_EXTENSION.   
2195d 20 46 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65   FUNCTION(load_e
2195e 78 74 65 6e 73 69 6f 6e 2c 20 20 20 20 20 31 2c  xtension,     1,
2195f 20 30 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20   0, 0, loadExt  
21960 20 20 20 20 20 20 20 20 29 2c 0a 20 20 20 20 46          ),.    F
21961 55 4e 43 54 49 4f 4e 28 6c 6f 61 64 5f 65 78 74  UNCTION(load_ext
21962 65 6e 73 69 6f 6e 2c 20 20 20 20 20 32 2c 20 30  ension,     2, 0
21963 2c 20 30 2c 20 6c 6f 61 64 45 78 74 20 20 20 20  , 0, loadExt    
21964 20 20 20 20 20 20 29 2c 0a 20 20 23 65 6e 64 69        ),.  #endi
21965 66 0a 20 20 20 20 41 47 47 52 45 47 41 54 45 28  f.    AGGREGATE(
21966 73 75 6d 2c 20 20 20 20 20 20 20 20 20 20 20 20  sum,            
21967 20 20 20 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53     1, 0, 0, sumS
21968 74 65 70 2c 20 20 20 20 20 20 20 20 20 73 75 6d  tep,         sum
21969 46 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20  Finalize    ),. 
2196a 20 20 20 41 47 47 52 45 47 41 54 45 28 74 6f 74     AGGREGATE(tot
2196b 61 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  al,             
2196c 31 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70  1, 0, 0, sumStep
2196d 2c 20 20 20 20 20 20 20 20 20 74 6f 74 61 6c 46  ,         totalF
2196e 69 6e 61 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20  inalize    ),.  
2196f 20 20 41 47 47 52 45 47 41 54 45 28 61 76 67 2c    AGGREGATE(avg,
21970 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
21971 2c 20 30 2c 20 30 2c 20 73 75 6d 53 74 65 70 2c  , 0, 0, sumStep,
21972 20 20 20 20 20 20 20 20 20 61 76 67 46 69 6e 61           avgFina
21973 6c 69 7a 65 20 20 20 20 29 2c 0a 20 20 20 20 41  lize    ),.    A
21974 47 47 52 45 47 41 54 45 28 63 6f 75 6e 74 2c 20  GGREGATE(count, 
21975 20 20 20 20 20 20 20 20 20 20 20 20 30 2c 20 30              0, 0
21976 2c 20 30 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20  , 0, countStep, 
21977 20 20 20 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c        countFinal
21978 69 7a 65 20 20 29 2c 0a 20 20 20 20 41 47 47 52  ize  ),.    AGGR
21979 45 47 41 54 45 28 63 6f 75 6e 74 2c 20 20 20 20  EGATE(count,    
2197a 20 20 20 20 20 20 20 20 20 31 2c 20 30 2c 20 30           1, 0, 0
2197b 2c 20 63 6f 75 6e 74 53 74 65 70 2c 20 20 20 20  , countStep,    
2197c 20 20 20 63 6f 75 6e 74 46 69 6e 61 6c 69 7a 65     countFinalize
2197d 20 20 29 2c 0a 20 20 20 20 41 47 47 52 45 47 41    ),.    AGGREGA
2197e 54 45 28 67 72 6f 75 70 5f 63 6f 6e 63 61 74 2c  TE(group_concat,
2197f 20 20 20 20 20 2d 31 2c 20 30 2c 20 30 2c 20 67       -1, 0, 0, g
21980 72 6f 75 70 43 6f 6e 63 61 74 53 74 65 70 2c 20  roupConcatStep, 
21981 67 72 6f 75 70 43 6f 6e 63 61 74 46 69 6e 61 6c  groupConcatFinal
21982 69 7a 65 29 2c 0a 20 20 0a 20 20 20 20 4c 49 4b  ize),.  .    LIK
21983 45 46 55 4e 43 28 67 6c 6f 62 2c 20 32 2c 20 26  EFUNC(glob, 2, &
21984 67 6c 6f 62 49 6e 66 6f 2c 20 53 51 4c 49 54 45  globInfo, SQLITE
21985 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53 51 4c 49 54  _FUNC_LIKE|SQLIT
21986 45 5f 46 55 4e 43 5f 43 41 53 45 29 2c 0a 20 20  E_FUNC_CASE),.  
21987 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 41  #ifdef SQLITE_CA
21988 53 45 5f 53 45 4e 53 49 54 49 56 45 5f 4c 49 4b  SE_SENSITIVE_LIK
21989 45 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c  E.    LIKEFUNC(l
2198a 69 6b 65 2c 20 32 2c 20 26 6c 69 6b 65 49 6e 66  ike, 2, &likeInf
2198b 6f 41 6c 74 2c 20 53 51 4c 49 54 45 5f 46 55 4e  oAlt, SQLITE_FUN
2198c 43 5f 4c 49 4b 45 7c 53 51 4c 49 54 45 5f 46 55  C_LIKE|SQLITE_FU
2198d 4e 43 5f 43 41 53 45 29 2c 0a 20 20 20 20 4c 49  NC_CASE),.    LI
2198e 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 33 2c 20  KEFUNC(like, 3, 
2198f 26 6c 69 6b 65 49 6e 66 6f 41 6c 74 2c 20 53 51  &likeInfoAlt, SQ
21990 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 7c 53  LITE_FUNC_LIKE|S
21991 51 4c 49 54 45 5f 46 55 4e 43 5f 43 41 53 45 29  QLITE_FUNC_CASE)
21992 2c 0a 20 20 23 65 6c 73 65 0a 20 20 20 20 4c 49  ,.  #else.    LI
21993 4b 45 46 55 4e 43 28 6c 69 6b 65 2c 20 32 2c 20  KEFUNC(like, 2, 
21994 26 6c 69 6b 65 49 6e 66 6f 4e 6f 72 6d 2c 20 53  &likeInfoNorm, S
21995 51 4c 49 54 45 5f 46 55 4e 43 5f 4c 49 4b 45 29  QLITE_FUNC_LIKE)
21996 2c 0a 20 20 20 20 4c 49 4b 45 46 55 4e 43 28 6c  ,.    LIKEFUNC(l
21997 69 6b 65 2c 20 33 2c 20 26 6c 69 6b 65 49 6e 66  ike, 3, &likeInf
21998 6f 4e 6f 72 6d 2c 20 53 51 4c 49 54 45 5f 46 55  oNorm, SQLITE_FU
21999 4e 43 5f 4c 49 4b 45 29 2c 0a 20 20 23 65 6e 64  NC_LIKE),.  #end
2199a 69 66 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69  if.  };..  int i
2199b 3b 0a 20 20 46 75 6e 63 44 65 66 48 61 73 68 20  ;.  FuncDefHash 
2199c 2a 70 48 61 73 68 20 3d 20 26 47 4c 4f 42 41 4c  *pHash = &GLOBAL
2199d 28 46 75 6e 63 44 65 66 48 61 73 68 2c 20 73 71  (FuncDefHash, sq
2199e 6c 69 74 65 33 47 6c 6f 62 61 6c 46 75 6e 63 74  lite3GlobalFunct
2199f 69 6f 6e 73 29 3b 0a 20 20 46 75 6e 63 44 65 66  ions);.  FuncDef
219a0 20 2a 61 46 75 6e 63 20 3d 20 28 46 75 6e 63 44   *aFunc = (FuncD
219a1 65 66 2a 29 26 47 4c 4f 42 41 4c 28 46 75 6e 63  ef*)&GLOBAL(Func
219a2 44 65 66 2c 20 61 42 75 69 6c 74 69 6e 46 75 6e  Def, aBuiltinFun
219a3 63 29 3b 0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20  c);..  for(i=0; 
219a4 69 3c 41 72 72 61 79 53 69 7a 65 28 61 42 75 69  i<ArraySize(aBui
219a5 6c 74 69 6e 46 75 6e 63 29 3b 20 69 2b 2b 29 7b  ltinFunc); i++){
219a6 0a 20 20 20 20 73 71 6c 69 74 65 33 46 75 6e 63  .    sqlite3Func
219a7 44 65 66 49 6e 73 65 72 74 28 70 48 61 73 68 2c  DefInsert(pHash,
219a8 20 26 61 46 75 6e 63 5b 69 5d 29 3b 0a 20 20 7d   &aFunc[i]);.  }
219a9 0a 20 20 73 71 6c 69 74 65 33 52 65 67 69 73 74  .  sqlite3Regist
219aa 65 72 44 61 74 65 54 69 6d 65 46 75 6e 63 74 69  erDateTimeFuncti
219ab 6f 6e 73 28 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a  ons();.}../*****
219ac 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
219ad 20 66 75 6e 63 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   func.c ********
219ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219b0 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
219b1 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
219b2 66 69 6c 65 20 69 6e 73 65 72 74 2e 63 20 2a 2a  file insert.c **
219b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219b4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219b5 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
219b6 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
219b7 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
219b8 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
219b9 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
219ba 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
219bb 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
219bc 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
219bd 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
219be 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
219bf 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
219c0 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
219c1 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
219c2 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
219c3 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
219c4 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
219c5 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
219c6 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
219c7 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
219c8 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
219c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
219cd 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f  .** This file co
219ce 6e 74 61 69 6e 73 20 43 20 63 6f 64 65 20 72 6f  ntains C code ro
219cf 75 74 69 6e 65 73 20 74 68 61 74 20 61 72 65 20  utines that are 
219d0 63 61 6c 6c 65 64 20 62 79 20 74 68 65 20 70 61  called by the pa
219d1 72 73 65 72 0a 2a 2a 20 74 6f 20 68 61 6e 64 6c  rser.** to handl
219d2 65 20 49 4e 53 45 52 54 20 73 74 61 74 65 6d 65  e INSERT stateme
219d3 6e 74 73 20 69 6e 20 53 51 4c 69 74 65 2e 0a 2a  nts in SQLite..*
219d4 2a 0a 2a 2a 20 24 49 64 3a 20 69 6e 73 65 72 74  *.** $Id: insert
219d5 2e 63 2c 76 20 31 2e 32 35 33 20 32 30 30 38 2f  .c,v 1.253 2008/
219d6 31 31 2f 31 39 20 30 39 3a 30 35 3a 32 37 20 64  11/19 09:05:27 d
219d7 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24  anielk1977 Exp $
219d8 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 50  .*/../*.** Set P
219d9 34 20 6f 66 20 74 68 65 20 6d 6f 73 74 20 72 65  4 of the most re
219da 63 65 6e 74 6c 79 20 69 6e 73 65 72 74 65 64 20  cently inserted 
219db 6f 70 63 6f 64 65 20 74 6f 20 61 20 63 6f 6c 75  opcode to a colu
219dc 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a 2a 20 73  mn affinity.** s
219dd 74 72 69 6e 67 20 66 6f 72 20 69 6e 64 65 78 20  tring for index 
219de 70 49 64 78 2e 20 41 20 63 6f 6c 75 6d 6e 20 61  pIdx. A column a
219df 66 66 69 6e 69 74 79 20 73 74 72 69 6e 67 20 68  ffinity string h
219e0 61 73 20 6f 6e 65 20 63 68 61 72 61 63 74 65 72  as one character
219e1 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  .** for each col
219e2 75 6d 6e 20 69 6e 20 74 68 65 20 74 61 62 6c 65  umn in the table
219e3 2c 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74  , according to t
219e4 68 65 20 61 66 66 69 6e 69 74 79 20 6f 66 20 74  he affinity of t
219e5 68 65 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a  he column:.**.**
219e6 20 20 43 68 61 72 61 63 74 65 72 20 20 20 20 20    Character     
219e7 20 43 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79   Column affinity
219e8 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**  -----------
219e9 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
219ea 2d 2d 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20  ---.**  'a'     
219eb 20 20 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20         TEXT.**  
219ec 27 62 27 20 20 20 20 20 20 20 20 20 20 20 20 4e  'b'            N
219ed 4f 4e 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20  ONE.**  'c'     
219ee 20 20 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a         NUMERIC.*
219ef 2a 20 20 27 64 27 20 20 20 20 20 20 20 20 20 20  *  'd'          
219f0 20 20 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65    INTEGER.**  'e
219f1 27 20 20 20 20 20 20 20 20 20 20 20 20 52 45 41  '            REA
219f2 4c 0a 2a 2a 0a 2a 2a 20 41 6e 20 65 78 74 72 61  L.**.** An extra
219f3 20 27 62 27 20 69 73 20 61 70 70 65 6e 64 65 64   'b' is appended
219f4 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
219f5 68 65 20 73 74 72 69 6e 67 20 74 6f 20 63 6f 76  he string to cov
219f6 65 72 20 74 68 65 0a 2a 2a 20 72 6f 77 69 64 20  er the.** rowid 
219f7 74 68 61 74 20 61 70 70 65 61 72 73 20 61 73 20  that appears as 
219f8 74 68 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20  the last column 
219f9 69 6e 20 65 76 65 72 79 20 69 6e 64 65 78 2e 0a  in every index..
219fa 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
219fb 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 49 6e  E void sqlite3In
219fc 64 65 78 41 66 66 69 6e 69 74 79 53 74 72 28 56  dexAffinityStr(V
219fd 64 62 65 20 2a 76 2c 20 49 6e 64 65 78 20 2a 70  dbe *v, Index *p
219fe 49 64 78 29 7b 0a 20 20 69 66 28 20 21 70 49 64  Idx){.  if( !pId
219ff 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a 20 20  x->zColAff ){.  
21a00 20 20 2f 2a 20 54 68 65 20 66 69 72 73 74 20 74    /* The first t
21a01 69 6d 65 20 61 20 63 6f 6c 75 6d 6e 20 61 66 66  ime a column aff
21a02 69 6e 69 74 79 20 73 74 72 69 6e 67 20 66 6f 72  inity string for
21a03 20 61 20 70 61 72 74 69 63 75 6c 61 72 20 69 6e   a particular in
21a04 64 65 78 20 69 73 0a 20 20 20 20 2a 2a 20 72 65  dex is.    ** re
21a05 71 75 69 72 65 64 2c 20 69 74 20 69 73 20 61 6c  quired, it is al
21a06 6c 6f 63 61 74 65 64 20 61 6e 64 20 70 6f 70 75  located and popu
21a07 6c 61 74 65 64 20 68 65 72 65 2e 20 49 74 20 69  lated here. It i
21a08 73 20 74 68 65 6e 20 73 74 6f 72 65 64 20 61 73  s then stored as
21a09 0a 20 20 20 20 2a 2a 20 61 20 6d 65 6d 62 65 72  .    ** a member
21a0a 20 6f 66 20 74 68 65 20 49 6e 64 65 78 20 73 74   of the Index st
21a0b 72 75 63 74 75 72 65 20 66 6f 72 20 73 75 62 73  ructure for subs
21a0c 65 71 75 65 6e 74 20 75 73 65 2e 0a 20 20 20 20  equent use..    
21a0d 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 63 6f  **.    ** The co
21a0e 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74  lumn affinity st
21a0f 72 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75  ring will eventu
21a10 61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20  ally be deleted 
21a11 62 79 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74 65  by.    ** sqlite
21a12 44 65 6c 65 74 65 49 6e 64 65 78 28 29 20 77 68  DeleteIndex() wh
21a13 65 6e 20 74 68 65 20 49 6e 64 65 78 20 73 74 72  en the Index str
21a14 75 63 74 75 72 65 20 69 74 73 65 6c 66 20 69 73  ucture itself is
21a15 20 63 6c 65 61 6e 65 64 0a 20 20 20 20 2a 2a 20   cleaned.    ** 
21a16 75 70 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  up..    */.    i
21a17 6e 74 20 6e 3b 0a 20 20 20 20 54 61 62 6c 65 20  nt n;.    Table 
21a18 2a 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54  *pTab = pIdx->pT
21a19 61 62 6c 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  able;.    sqlite
21a1a 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56  3 *db = sqlite3V
21a1b 64 62 65 44 62 28 76 29 3b 0a 20 20 20 20 70 49  dbeDb(v);.    pI
21a1c 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 3d 20 28 63  dx->zColAff = (c
21a1d 68 61 72 20 2a 29 73 71 6c 69 74 65 33 4d 61 6c  har *)sqlite3Mal
21a1e 6c 6f 63 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  loc(pIdx->nColum
21a1f 6e 2b 32 29 3b 0a 20 20 20 20 69 66 28 20 21 70  n+2);.    if( !p
21a20 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 20 29 7b 0a  Idx->zColAff ){.
21a21 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63        db->malloc
21a22 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20  Failed = 1;.    
21a23 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
21a24 20 20 20 20 66 6f 72 28 6e 3d 30 3b 20 6e 3c 70      for(n=0; n<p
21a25 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6e 2b  Idx->nColumn; n+
21a26 2b 29 7b 0a 20 20 20 20 20 20 70 49 64 78 2d 3e  +){.      pIdx->
21a27 7a 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 70 54 61  zColAff[n] = pTa
21a28 62 2d 3e 61 43 6f 6c 5b 70 49 64 78 2d 3e 61 69  b->aCol[pIdx->ai
21a29 43 6f 6c 75 6d 6e 5b 6e 5d 5d 2e 61 66 66 69 6e  Column[n]].affin
21a2a 69 74 79 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  ity;.    }.    p
21a2b 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 5b 6e 2b 2b  Idx->zColAff[n++
21a2c 5d 20 3d 20 53 51 4c 49 54 45 5f 41 46 46 5f 4e  ] = SQLITE_AFF_N
21a2d 4f 4e 45 3b 0a 20 20 20 20 70 49 64 78 2d 3e 7a  ONE;.    pIdx->z
21a2e 43 6f 6c 41 66 66 5b 6e 5d 20 3d 20 30 3b 0a 20  ColAff[n] = 0;. 
21a2f 20 7d 0a 20 0a 20 20 73 71 6c 69 74 65 33 56 64   }. .  sqlite3Vd
21a30 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
21a31 2c 20 70 49 64 78 2d 3e 7a 43 6f 6c 41 66 66 2c  , pIdx->zColAff,
21a32 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65   0);.}../*.** Se
21a33 74 20 50 34 20 6f 66 20 74 68 65 20 6d 6f 73 74  t P4 of the most
21a34 20 72 65 63 65 6e 74 6c 79 20 69 6e 73 65 72 74   recently insert
21a35 65 64 20 6f 70 63 6f 64 65 20 74 6f 20 61 20 63  ed opcode to a c
21a36 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a  olumn affinity.*
21a37 2a 20 73 74 72 69 6e 67 20 66 6f 72 20 74 61 62  * string for tab
21a38 6c 65 20 70 54 61 62 2e 20 41 20 63 6f 6c 75 6d  le pTab. A colum
21a39 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69 6e  n affinity strin
21a3a 67 20 68 61 73 20 6f 6e 65 20 63 68 61 72 61 63  g has one charac
21a3b 74 65 72 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20  ter.** for each 
21a3c 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 65 64 20 62  column indexed b
21a3d 79 20 74 68 65 20 69 6e 64 65 78 2c 20 61 63 63  y the index, acc
21a3e 6f 72 64 69 6e 67 20 74 6f 20 74 68 65 20 61 66  ording to the af
21a3f 66 69 6e 69 74 79 20 6f 66 20 74 68 65 0a 2a 2a  finity of the.**
21a40 20 63 6f 6c 75 6d 6e 3a 0a 2a 2a 0a 2a 2a 20 20   column:.**.**  
21a41 43 68 61 72 61 63 74 65 72 20 20 20 20 20 20 43  Character      C
21a42 6f 6c 75 6d 6e 20 61 66 66 69 6e 69 74 79 0a 2a  olumn affinity.*
21a43 2a 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  *  -------------
21a44 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
21a45 2d 0a 2a 2a 20 20 27 61 27 20 20 20 20 20 20 20  -.**  'a'       
21a46 20 20 20 20 20 54 45 58 54 0a 2a 2a 20 20 27 62       TEXT.**  'b
21a47 27 20 20 20 20 20 20 20 20 20 20 20 20 4e 4f 4e  '            NON
21a48 45 0a 2a 2a 20 20 27 63 27 20 20 20 20 20 20 20  E.**  'c'       
21a49 20 20 20 20 20 4e 55 4d 45 52 49 43 0a 2a 2a 20       NUMERIC.** 
21a4a 20 27 64 27 20 20 20 20 20 20 20 20 20 20 20 20   'd'            
21a4b 49 4e 54 45 47 45 52 0a 2a 2a 20 20 27 65 27 20  INTEGER.**  'e' 
21a4c 20 20 20 20 20 20 20 20 20 20 20 52 45 41 4c 0a             REAL.
21a4d 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
21a4e 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 54 61  E void sqlite3Ta
21a4f 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28 56  bleAffinityStr(V
21a50 64 62 65 20 2a 76 2c 20 54 61 62 6c 65 20 2a 70  dbe *v, Table *p
21a51 54 61 62 29 7b 0a 20 20 2f 2a 20 54 68 65 20 66  Tab){.  /* The f
21a52 69 72 73 74 20 74 69 6d 65 20 61 20 63 6f 6c 75  irst time a colu
21a53 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72 69  mn affinity stri
21a54 6e 67 20 66 6f 72 20 61 20 70 61 72 74 69 63 75  ng for a particu
21a55 6c 61 72 20 74 61 62 6c 65 0a 20 20 2a 2a 20 69  lar table.  ** i
21a56 73 20 72 65 71 75 69 72 65 64 2c 20 69 74 20 69  s required, it i
21a57 73 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64 20  s allocated and 
21a58 70 6f 70 75 6c 61 74 65 64 20 68 65 72 65 2e 20  populated here. 
21a59 49 74 20 69 73 20 74 68 65 6e 20 0a 20 20 2a 2a  It is then .  **
21a5a 20 73 74 6f 72 65 64 20 61 73 20 61 20 6d 65 6d   stored as a mem
21a5b 62 65 72 20 6f 66 20 74 68 65 20 54 61 62 6c 65  ber of the Table
21a5c 20 73 74 72 75 63 74 75 72 65 20 66 6f 72 20 73   structure for s
21a5d 75 62 73 65 71 75 65 6e 74 20 75 73 65 2e 0a 20  ubsequent use.. 
21a5e 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 63 6f 6c   **.  ** The col
21a5f 75 6d 6e 20 61 66 66 69 6e 69 74 79 20 73 74 72  umn affinity str
21a60 69 6e 67 20 77 69 6c 6c 20 65 76 65 6e 74 75 61  ing will eventua
21a61 6c 6c 79 20 62 65 20 64 65 6c 65 74 65 64 20 62  lly be deleted b
21a62 79 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 44 65  y.  ** sqlite3De
21a63 6c 65 74 65 54 61 62 6c 65 28 29 20 77 68 65 6e  leteTable() when
21a64 20 74 68 65 20 54 61 62 6c 65 20 73 74 72 75 63   the Table struc
21a65 74 75 72 65 20 69 74 73 65 6c 66 20 69 73 20 63  ture itself is c
21a66 6c 65 61 6e 65 64 20 75 70 2e 0a 20 20 2a 2f 0a  leaned up..  */.
21a67 20 20 69 66 28 20 21 70 54 61 62 2d 3e 7a 43 6f    if( !pTab->zCo
21a68 6c 41 66 66 20 29 7b 0a 20 20 20 20 63 68 61 72  lAff ){.    char
21a69 20 2a 7a 43 6f 6c 41 66 66 3b 0a 20 20 20 20 69   *zColAff;.    i
21a6a 6e 74 20 69 3b 0a 20 20 20 20 73 71 6c 69 74 65  nt i;.    sqlite
21a6b 33 20 2a 64 62 20 3d 20 73 71 6c 69 74 65 33 56  3 *db = sqlite3V
21a6c 64 62 65 44 62 28 76 29 3b 0a 0a 20 20 20 20 7a  dbeDb(v);..    z
21a6d 43 6f 6c 41 66 66 20 3d 20 28 63 68 61 72 20 2a  ColAff = (char *
21a6e 29 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 70  )sqlite3Malloc(p
21a6f 54 61 62 2d 3e 6e 43 6f 6c 2b 31 29 3b 0a 20 20  Tab->nCol+1);.  
21a70 20 20 69 66 28 20 21 7a 43 6f 6c 41 66 66 20 29    if( !zColAff )
21a71 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c 6c  {.      db->mall
21a72 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20 20  ocFailed = 1;.  
21a73 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
21a74 7d 0a 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  }..    for(i=0; 
21a75 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
21a76 2b 29 7b 0a 20 20 20 20 20 20 7a 43 6f 6c 41 66  +){.      zColAf
21a77 66 5b 69 5d 20 3d 20 70 54 61 62 2d 3e 61 43 6f  f[i] = pTab->aCo
21a78 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20  l[i].affinity;. 
21a79 20 20 20 7d 0a 20 20 20 20 7a 43 6f 6c 41 66 66     }.    zColAff
21a7a 5b 70 54 61 62 2d 3e 6e 43 6f 6c 5d 20 3d 20 27  [pTab->nCol] = '
21a7b 5c 30 27 3b 0a 0a 20 20 20 20 70 54 61 62 2d 3e  \0';..    pTab->
21a7c 7a 43 6f 6c 41 66 66 20 3d 20 7a 43 6f 6c 41 66  zColAff = zColAf
21a7d 66 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65  f;.  }..  sqlite
21a7e 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
21a7f 20 2d 31 2c 20 70 54 61 62 2d 3e 7a 43 6f 6c 41   -1, pTab->zColA
21a80 66 66 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ff, 0);.}../*.**
21a81 20 52 65 74 75 72 6e 20 6e 6f 6e 2d 7a 65 72 6f   Return non-zero
21a82 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 70 54   if the table pT
21a83 61 62 20 69 6e 20 64 61 74 61 62 61 73 65 20 69  ab in database i
21a84 44 62 20 6f 72 20 61 6e 79 20 6f 66 20 69 74 73  Db or any of its
21a85 20 69 6e 64 69 63 65 73 0a 2a 2a 20 68 61 76 65   indices.** have
21a86 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 61 74 20   been opened at 
21a87 61 6e 79 20 70 6f 69 6e 74 20 69 6e 20 74 68 65  any point in the
21a88 20 56 44 42 45 20 70 72 6f 67 72 61 6d 20 62 65   VDBE program be
21a89 67 69 6e 6e 69 6e 67 20 61 74 20 6c 6f 63 61 74  ginning at locat
21a8a 69 6f 6e 0a 2a 2a 20 69 53 74 61 72 74 41 64 64  ion.** iStartAdd
21a8b 72 20 74 68 72 6f 75 67 68 74 20 74 68 65 20 65  r throught the e
21a8c 6e 64 20 6f 66 20 74 68 65 20 70 72 6f 67 72 61  nd of the progra
21a8d 6d 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  m.  This is used
21a8e 20 74 6f 20 73 65 65 20 69 66 20 0a 2a 2a 20 61   to see if .** a
21a8f 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20 74 68   statement of th
21a90 65 20 66 6f 72 6d 20 20 22 49 4e 53 45 52 54 20  e form  "INSERT 
21a91 49 4e 54 4f 20 3c 69 44 62 2c 20 70 54 61 62 3e  INTO <iDb, pTab>
21a92 20 53 45 4c 45 43 54 20 2e 2e 2e 22 20 63 61 6e   SELECT ..." can
21a93 20 0a 2a 2a 20 72 75 6e 20 77 69 74 68 6f 75 74   .** run without
21a94 20 75 73 69 6e 67 20 74 65 6d 70 6f 72 61 72 79   using temporary
21a95 20 74 61 62 6c 65 20 66 6f 72 20 74 68 65 20 72   table for the r
21a96 65 73 75 6c 74 73 20 6f 66 20 74 68 65 20 53 45  esults of the SE
21a97 4c 45 43 54 2e 20 0a 2a 2f 0a 73 74 61 74 69 63  LECT. .*/.static
21a98 20 69 6e 74 20 72 65 61 64 73 54 61 62 6c 65 28   int readsTable(
21a99 56 64 62 65 20 2a 76 2c 20 69 6e 74 20 69 53 74  Vdbe *v, int iSt
21a9a 61 72 74 41 64 64 72 2c 20 69 6e 74 20 69 44 62  artAddr, int iDb
21a9b 2c 20 54 61 62 6c 65 20 2a 70 54 61 62 29 7b 0a  , Table *pTab){.
21a9c 20 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69    int i;.  int i
21a9d 45 6e 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62  End = sqlite3Vdb
21a9e 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b  eCurrentAddr(v);
21a9f 0a 20 20 66 6f 72 28 69 3d 69 53 74 61 72 74 41  .  for(i=iStartA
21aa0 64 64 72 3b 20 69 3c 69 45 6e 64 3b 20 69 2b 2b  ddr; i<iEnd; i++
21aa1 29 7b 0a 20 20 20 20 56 64 62 65 4f 70 20 2a 70  ){.    VdbeOp *p
21aa2 4f 70 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  Op = sqlite3Vdbe
21aa3 47 65 74 4f 70 28 76 2c 20 69 29 3b 0a 20 20 20  GetOp(v, i);.   
21aa4 20 61 73 73 65 72 74 28 20 70 4f 70 21 3d 30 20   assert( pOp!=0 
21aa5 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 70 2d 3e  );.    if( pOp->
21aa6 6f 70 63 6f 64 65 3d 3d 4f 50 5f 4f 70 65 6e 52  opcode==OP_OpenR
21aa7 65 61 64 20 26 26 20 70 4f 70 2d 3e 70 33 3d 3d  ead && pOp->p3==
21aa8 69 44 62 20 29 7b 0a 20 20 20 20 20 20 49 6e 64  iDb ){.      Ind
21aa9 65 78 20 2a 70 49 6e 64 65 78 3b 0a 20 20 20 20  ex *pIndex;.    
21aaa 20 20 69 6e 74 20 74 6e 75 6d 20 3d 20 70 4f 70    int tnum = pOp
21aab 2d 3e 70 32 3b 0a 20 20 20 20 20 20 69 66 28 20  ->p2;.      if( 
21aac 74 6e 75 6d 3d 3d 70 54 61 62 2d 3e 74 6e 75 6d  tnum==pTab->tnum
21aad 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75   ){.        retu
21aae 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 1;.      }.  
21aaf 20 20 20 20 66 6f 72 28 70 49 6e 64 65 78 3d 70      for(pIndex=p
21ab0 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 6e  Tab->pIndex; pIn
21ab1 64 65 78 3b 20 70 49 6e 64 65 78 3d 70 49 6e 64  dex; pIndex=pInd
21ab2 65 78 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  ex->pNext){.    
21ab3 20 20 20 20 69 66 28 20 74 6e 75 6d 3d 3d 70 49      if( tnum==pI
21ab4 6e 64 65 78 2d 3e 74 6e 75 6d 20 29 7b 0a 20 20  ndex->tnum ){.  
21ab5 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
21ab6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
21ab7 20 20 7d 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65    }.    }.#ifnde
21ab8 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
21ab9 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69  RTUALTABLE.    i
21aba 66 28 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d  f( pOp->opcode==
21abb 4f 50 5f 56 4f 70 65 6e 20 26 26 20 70 4f 70 2d  OP_VOpen && pOp-
21abc 3e 70 34 2e 70 56 74 61 62 3d 3d 70 54 61 62 2d  >p4.pVtab==pTab-
21abd 3e 70 56 74 61 62 20 29 7b 0a 20 20 20 20 20 20  >pVtab ){.      
21abe 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70 34 2e  assert( pOp->p4.
21abf 70 56 74 61 62 21 3d 30 20 29 3b 0a 20 20 20 20  pVtab!=0 );.    
21ac0 20 20 61 73 73 65 72 74 28 20 70 4f 70 2d 3e 70    assert( pOp->p
21ac1 34 74 79 70 65 3d 3d 50 34 5f 56 54 41 42 20 29  4type==P4_VTAB )
21ac2 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31  ;.      return 1
21ac3 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
21ac4 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
21ac5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
21ac6 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43 52 45 4d  _OMIT_AUTOINCREM
21ac7 45 4e 54 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20  ENT./*.** Write 
21ac8 6f 75 74 20 63 6f 64 65 20 74 6f 20 69 6e 69 74  out code to init
21ac9 69 61 6c 69 7a 65 20 74 68 65 20 61 75 74 6f 69  ialize the autoi
21aca 6e 63 72 65 6d 65 6e 74 20 6c 6f 67 69 63 2e 20  ncrement logic. 
21acb 20 54 68 69 73 20 63 6f 64 65 0a 2a 2a 20 6c 6f   This code.** lo
21acc 6f 6b 73 20 75 70 20 74 68 65 20 63 75 72 72 65  oks up the curre
21acd 6e 74 20 61 75 74 6f 69 6e 63 72 65 6d 65 6e 74  nt autoincrement
21ace 20 76 61 6c 75 65 20 69 6e 20 74 68 65 20 73 71   value in the sq
21acf 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 0a 2a 2a  lite_sequence.**
21ad0 20 74 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65   table and store
21ad1 73 20 74 68 61 74 20 76 61 6c 75 65 20 69 6e 20  s that value in 
21ad2 61 20 72 65 67 69 73 74 65 72 2e 20 20 43 6f 64  a register.  Cod
21ad3 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 0a 2a  e generated by.*
21ad4 2a 20 61 75 74 6f 49 6e 63 53 74 65 70 28 29 20  * autoIncStep() 
21ad5 77 69 6c 6c 20 6b 65 65 70 20 74 68 61 74 20 72  will keep that r
21ad6 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
21ad7 74 68 65 20 6c 61 72 67 65 73 74 0a 2a 2a 20 72  the largest.** r
21ad8 6f 77 69 64 20 76 61 6c 75 65 2e 20 20 43 6f 64  owid value.  Cod
21ad9 65 20 67 65 6e 65 72 61 74 65 64 20 62 79 20 61  e generated by a
21ada 75 74 6f 49 6e 63 45 6e 64 28 29 20 77 69 6c 6c  utoIncEnd() will
21adb 20 77 72 69 74 65 20 74 68 65 20 6e 65 77 0a 2a   write the new.*
21adc 2a 20 6c 61 72 67 65 73 74 20 76 61 6c 75 65 20  * largest value 
21add 6f 66 20 74 68 65 20 63 6f 75 6e 74 65 72 20 62  of the counter b
21ade 61 63 6b 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ack into the sql
21adf 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62  ite_sequence tab
21ae0 6c 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  le..**.** This r
21ae1 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 74  outine returns t
21ae2 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20  he index of the 
21ae3 6d 65 6d 5b 5d 20 63 65 6c 6c 20 74 68 61 74 20  mem[] cell that 
21ae4 63 6f 6e 74 61 69 6e 73 0a 2a 2a 20 74 68 65 20  contains.** the 
21ae5 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 63 6f  maximum rowid co
21ae6 75 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 72  unter..**.** Thr
21ae7 65 65 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72  ee consecutive r
21ae8 65 67 69 73 74 65 72 73 20 61 72 65 20 61 6c 6c  egisters are all
21ae9 6f 63 61 74 65 64 20 62 79 20 74 68 69 73 20 72  ocated by this r
21aea 6f 75 74 69 6e 65 2e 20 20 54 68 65 0a 2a 2a 20  outine.  The.** 
21aeb 66 69 72 73 74 20 74 77 6f 20 68 6f 6c 64 20 74  first two hold t
21aec 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
21aed 61 72 67 65 74 20 74 61 62 6c 65 20 61 6e 64 20  arget table and 
21aee 74 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69  the maximum rowi
21aef 64 20 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 69  d .** inserted i
21af0 6e 74 6f 20 74 68 65 20 74 61 72 67 65 74 20 74  nto the target t
21af1 61 62 6c 65 2c 20 72 65 73 70 65 63 74 69 76 65  able, respective
21af2 6c 79 2e 0a 2a 2a 20 54 68 65 20 74 68 69 72 64  ly..** The third
21af3 20 68 6f 6c 64 73 20 74 68 65 20 72 6f 77 69 64   holds the rowid
21af4 20 69 6e 20 73 71 6c 69 74 65 5f 73 65 71 75 65   in sqlite_seque
21af5 6e 63 65 20 77 68 65 72 65 20 77 65 20 77 69 6c  nce where we wil
21af6 6c 0a 2a 2a 20 77 72 69 74 65 20 62 61 63 6b 20  l.** write back 
21af7 74 68 65 20 72 65 76 69 73 65 64 20 6d 61 78 69  the revised maxi
21af8 6d 75 6d 20 72 6f 77 69 64 2e 20 20 54 68 69 73  mum rowid.  This
21af9 20 72 6f 75 74 69 6e 65 20 72 65 74 75 72 6e 73   routine returns
21afa 20 74 68 65 0a 2a 2a 20 69 6e 64 65 78 20 6f 66   the.** index of
21afb 20 74 68 65 20 73 65 63 6f 6e 64 20 6f 66 20 74   the second of t
21afc 68 65 73 65 20 74 68 72 65 65 20 72 65 67 69 73  hese three regis
21afd 74 65 72 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ters..*/.static 
21afe 69 6e 74 20 61 75 74 6f 49 6e 63 42 65 67 69 6e  int autoIncBegin
21aff 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
21b00 65 2c 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69  e,      /* Parsi
21b01 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  ng context */.  
21b02 69 6e 74 20 69 44 62 2c 20 20 20 20 20 20 20 20  int iDb,        
21b03 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
21b04 74 68 65 20 64 61 74 61 62 61 73 65 20 68 6f 6c  the database hol
21b05 64 69 6e 67 20 70 54 61 62 20 2a 2f 0a 20 20 54  ding pTab */.  T
21b06 61 62 6c 65 20 2a 70 54 61 62 20 20 20 20 20 20  able *pTab      
21b07 20 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20     /* The table 
21b08 77 65 20 61 72 65 20 77 72 69 74 69 6e 67 20 74  we are writing t
21b09 6f 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 6d 65  o */.){.  int me
21b0a 6d 49 64 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a  mId = 0;      /*
21b0b 20 52 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e   Register holdin
21b0c 67 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20  g maximum rowid 
21b0d 2a 2f 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 74  */.  if( pTab->t
21b0e 61 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74  abFlags & TF_Aut
21b0f 6f 69 6e 63 72 65 6d 65 6e 74 20 29 7b 0a 20 20  oincrement ){.  
21b10 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72    Vdbe *v = pPar
21b11 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 20 20 44  se->pVdbe;.    D
21b12 62 20 2a 70 44 62 20 3d 20 26 70 50 61 72 73 65  b *pDb = &pParse
21b13 2d 3e 64 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a  ->db->aDb[iDb];.
21b14 20 20 20 20 69 6e 74 20 69 43 75 72 20 3d 20 70      int iCur = p
21b15 50 61 72 73 65 2d 3e 6e 54 61 62 3b 0a 20 20 20  Parse->nTab;.   
21b16 20 69 6e 74 20 61 64 64 72 3b 20 20 20 20 20 20   int addr;      
21b17 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
21b18 65 73 73 20 6f 66 20 74 68 65 20 74 6f 70 20 6f  ess of the top o
21b19 66 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20 20  f the loop */.  
21b1a 20 20 61 73 73 65 72 74 28 20 76 20 29 3b 0a 20    assert( v );. 
21b1b 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b     pParse->nMem+
21b1c 2b 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  +;         /* Ho
21b1d 6c 64 73 20 6e 61 6d 65 20 6f 66 20 74 61 62 6c  lds name of tabl
21b1e 65 20 2a 2f 0a 20 20 20 20 6d 65 6d 49 64 20 3d  e */.    memId =
21b1f 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
21b20 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65  .    pParse->nMe
21b21 6d 2b 2b 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  m++;.    sqlite3
21b22 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73 65  OpenTable(pParse
21b23 2c 20 69 43 75 72 2c 20 69 44 62 2c 20 70 44 62  , iCur, iDb, pDb
21b24 2d 3e 70 53 63 68 65 6d 61 2d 3e 70 53 65 71 54  ->pSchema->pSeqT
21b25 61 62 2c 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29  ab, OP_OpenRead)
21b26 3b 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c  ;.    addr = sql
21b27 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74 41  ite3VdbeCurrentA
21b28 64 64 72 28 76 29 3b 0a 20 20 20 20 73 71 6c 69  ddr(v);.    sqli
21b29 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
21b2a 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
21b2b 6d 65 6d 49 64 2d 31 2c 20 30 2c 20 70 54 61 62  memId-1, 0, pTab
21b2c 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
21b2d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b2e 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
21b2f 20 69 43 75 72 2c 20 61 64 64 72 2b 39 29 3b 0a   iCur, addr+9);.
21b30 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21b31 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
21b32 6d 6e 2c 20 69 43 75 72 2c 20 30 2c 20 6d 65 6d  mn, iCur, 0, mem
21b33 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Id);.    sqlite3
21b34 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
21b35 5f 4e 65 2c 20 6d 65 6d 49 64 2d 31 2c 20 61 64  _Ne, memId-1, ad
21b36 64 72 2b 37 2c 20 6d 65 6d 49 64 29 3b 0a 20 20  dr+7, memId);.  
21b37 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
21b38 6e 67 65 50 35 28 76 2c 20 53 51 4c 49 54 45 5f  ngeP5(v, SQLITE_
21b39 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
21b3a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b3b 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20  p2(v, OP_Rowid, 
21b3c 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31 29 3b 0a  iCur, memId+1);.
21b3d 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21b3e 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75  ddOp3(v, OP_Colu
21b3f 6d 6e 2c 20 69 43 75 72 2c 20 31 2c 20 6d 65 6d  mn, iCur, 1, mem
21b40 49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  Id);.    sqlite3
21b41 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21b42 5f 47 6f 74 6f 2c 20 30 2c 20 61 64 64 72 2b 39  _Goto, 0, addr+9
21b43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
21b44 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
21b45 65 78 74 2c 20 69 43 75 72 2c 20 61 64 64 72 2b  ext, iCur, addr+
21b46 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  2);.    sqlite3V
21b47 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21b48 49 6e 74 65 67 65 72 2c 20 30 2c 20 6d 65 6d 49  Integer, 0, memI
21b49 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  d);.    sqlite3V
21b4a 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21b4b 43 6c 6f 73 65 2c 20 69 43 75 72 2c 20 30 29 3b  Close, iCur, 0);
21b4c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 65  .  }.  return me
21b4d 6d 49 64 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 55 70  mId;.}../*.** Up
21b4e 64 61 74 65 20 74 68 65 20 6d 61 78 69 6d 75 6d  date the maximum
21b4f 20 72 6f 77 69 64 20 66 6f 72 20 61 6e 20 61 75   rowid for an au
21b50 74 6f 69 6e 63 72 65 6d 65 6e 74 20 63 61 6c 63  toincrement calc
21b51 75 6c 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54  ulation..**.** T
21b52 68 69 73 20 72 6f 75 74 69 6e 65 20 73 68 6f 75  his routine shou
21b53 6c 64 20 62 65 20 63 61 6c 6c 65 64 20 77 68 65  ld be called whe
21b54 6e 20 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65  n the top of the
21b55 20 73 74 61 63 6b 20 68 6f 6c 64 73 20 61 0a 2a   stack holds a.*
21b56 2a 20 6e 65 77 20 72 6f 77 69 64 20 74 68 61 74  * new rowid that
21b57 20 69 73 20 61 62 6f 75 74 20 74 6f 20 62 65 20   is about to be 
21b58 69 6e 73 65 72 74 65 64 2e 20 20 49 66 20 74 68  inserted.  If th
21b59 61 74 20 6e 65 77 20 72 6f 77 69 64 20 69 73 0a  at new rowid is.
21b5a 2a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 74  ** larger than t
21b5b 68 65 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64  he maximum rowid
21b5c 20 69 6e 20 74 68 65 20 6d 65 6d 49 64 20 6d 65   in the memId me
21b5d 6d 6f 72 79 20 63 65 6c 6c 2c 20 74 68 65 6e 20  mory cell, then 
21b5e 74 68 65 0a 2a 2a 20 6d 65 6d 6f 72 79 20 63 65  the.** memory ce
21b5f 6c 6c 20 69 73 20 75 70 64 61 74 65 64 2e 20 20  ll is updated.  
21b60 54 68 65 20 73 74 61 63 6b 20 69 73 20 75 6e 63  The stack is unc
21b61 68 61 6e 67 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  hanged..*/.stati
21b62 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63 53 74  c void autoIncSt
21b63 65 70 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  ep(Parse *pParse
21b64 2c 20 69 6e 74 20 6d 65 6d 49 64 2c 20 69 6e 74  , int memId, int
21b65 20 72 65 67 52 6f 77 69 64 29 7b 0a 20 20 69 66   regRowid){.  if
21b66 28 20 6d 65 6d 49 64 3e 30 20 29 7b 0a 20 20 20  ( memId>0 ){.   
21b67 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21b68 70 32 28 70 50 61 72 73 65 2d 3e 70 56 64 62 65  p2(pParse->pVdbe
21b69 2c 20 4f 50 5f 4d 65 6d 4d 61 78 2c 20 6d 65 6d  , OP_MemMax, mem
21b6a 49 64 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  Id, regRowid);. 
21b6b 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 66 74 65   }.}../*.** Afte
21b6c 72 20 64 6f 69 6e 67 20 6f 6e 65 20 6f 72 20 6d  r doing one or m
21b6d 6f 72 65 20 69 6e 73 65 72 74 73 2c 20 74 68 65  ore inserts, the
21b6e 20 6d 61 78 69 6d 75 6d 20 72 6f 77 69 64 20 69   maximum rowid i
21b6f 73 20 73 74 6f 72 65 64 0a 2a 2a 20 69 6e 20 72  s stored.** in r
21b70 65 67 5b 6d 65 6d 49 64 5d 2e 20 20 47 65 6e 65  eg[memId].  Gene
21b71 72 61 74 65 20 63 6f 64 65 20 74 6f 20 77 72 69  rate code to wri
21b72 74 65 20 74 68 69 73 20 76 61 6c 75 65 20 62 61  te this value ba
21b73 63 6b 20 69 6e 74 6f 20 74 68 65 0a 2a 2a 20 74  ck into the.** t
21b74 68 65 20 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  he sqlite_sequen
21b75 63 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61  ce table..*/.sta
21b76 74 69 63 20 76 6f 69 64 20 61 75 74 6f 49 6e 63  tic void autoInc
21b77 45 6e 64 28 0a 20 20 50 61 72 73 65 20 2a 70 50  End(.  Parse *pP
21b78 61 72 73 65 2c 20 20 20 20 20 2f 2a 20 54 68 65  arse,     /* The
21b79 20 70 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   parsing context
21b7a 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 2c 20 20   */.  int iDb,  
21b7b 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65           /* Inde
21b7c 78 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  x of the databas
21b7d 65 20 68 6f 6c 64 69 6e 67 20 70 54 61 62 20 2a  e holding pTab *
21b7e 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c  /.  Table *pTab,
21b7f 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
21b80 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
21b81 20 69 6e 74 6f 20 2a 2f 0a 20 20 69 6e 74 20 6d   into */.  int m
21b82 65 6d 49 64 20 20 20 20 20 20 20 20 20 20 2f 2a  emId          /*
21b83 20 4d 65 6d 6f 72 79 20 63 65 6c 6c 20 68 6f 6c   Memory cell hol
21b84 64 69 6e 67 20 74 68 65 20 6d 61 78 69 6d 75 6d  ding the maximum
21b85 20 72 6f 77 69 64 20 2a 2f 0a 29 7b 0a 20 20 69   rowid */.){.  i
21b86 66 28 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67  f( pTab->tabFlag
21b87 73 20 26 20 54 46 5f 41 75 74 6f 69 6e 63 72 65  s & TF_Autoincre
21b88 6d 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  ment ){.    int 
21b89 69 43 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iCur = pParse->n
21b8a 54 61 62 3b 0a 20 20 20 20 56 64 62 65 20 2a 76  Tab;.    Vdbe *v
21b8b 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
21b8c 3b 0a 20 20 20 20 44 62 20 2a 70 44 62 20 3d 20  ;.    Db *pDb = 
21b8d 26 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62  &pParse->db->aDb
21b8e 5b 69 44 62 5d 3b 0a 20 20 20 20 69 6e 74 20 6a  [iDb];.    int j
21b8f 31 3b 0a 20 20 20 20 69 6e 74 20 69 52 65 63 20  1;.    int iRec 
21b90 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
21b91 3b 20 20 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63  ;    /* Memory c
21b92 65 6c 6c 20 75 73 65 64 20 66 6f 72 20 72 65 63  ell used for rec
21b93 6f 72 64 20 2a 2f 0a 0a 20 20 20 20 61 73 73 65  ord */..    asse
21b94 72 74 28 20 76 20 29 3b 0a 20 20 20 20 73 71 6c  rt( v );.    sql
21b95 69 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50  ite3OpenTable(pP
21b96 61 72 73 65 2c 20 69 43 75 72 2c 20 69 44 62 2c  arse, iCur, iDb,
21b97 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 70   pDb->pSchema->p
21b98 53 65 71 54 61 62 2c 20 4f 50 5f 4f 70 65 6e 57  SeqTab, OP_OpenW
21b99 72 69 74 65 29 3b 0a 20 20 20 20 6a 31 20 3d 20  rite);.    j1 = 
21b9a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21b9b 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c 6c 2c  1(v, OP_NotNull,
21b9c 20 6d 65 6d 49 64 2b 31 29 3b 0a 20 20 20 20 73   memId+1);.    s
21b9d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
21b9e 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
21b9f 20 69 43 75 72 2c 20 6d 65 6d 49 64 2b 31 29 3b   iCur, memId+1);
21ba0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21ba1 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b  JumpHere(v, j1);
21ba2 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21ba3 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b  AddOp3(v, OP_Mak
21ba4 65 52 65 63 6f 72 64 2c 20 6d 65 6d 49 64 2d 31  eRecord, memId-1
21ba5 2c 20 32 2c 20 69 52 65 63 29 3b 0a 20 20 20 20  , 2, iRec);.    
21ba6 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
21ba7 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c 20  3(v, OP_Insert, 
21ba8 69 43 75 72 2c 20 69 52 65 63 2c 20 6d 65 6d 49  iCur, iRec, memI
21ba9 64 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  d+1);.    sqlite
21baa 33 56 64 62 65 43 68 61 6e 67 65 50 35 28 76 2c  3VdbeChangeP5(v,
21bab 20 4f 50 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b   OPFLAG_APPEND);
21bac 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
21bad 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp1(v, OP_Clo
21bae 73 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 7d  se, iCur);.  }.}
21baf 0a 23 65 6c 73 65 0a 2f 2a 0a 2a 2a 20 49 66 20  .#else./*.** If 
21bb0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
21bb1 49 4e 43 52 45 4d 45 4e 54 20 69 73 20 64 65 66  INCREMENT is def
21bb2 69 6e 65 64 2c 20 74 68 65 6e 20 74 68 65 20 74  ined, then the t
21bb3 68 72 65 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a  hree routines.**
21bb4 20 61 62 6f 76 65 20 61 72 65 20 61 6c 6c 20 6e   above are all n
21bb5 6f 2d 6f 70 73 0a 2a 2f 0a 23 20 64 65 66 69 6e  o-ops.*/.# defin
21bb6 65 20 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 41  e autoIncBegin(A
21bb7 2c 42 2c 43 29 20 28 30 29 0a 23 20 64 65 66 69  ,B,C) (0).# defi
21bb8 6e 65 20 61 75 74 6f 49 6e 63 53 74 65 70 28 41  ne autoIncStep(A
21bb9 2c 42 2c 43 29 0a 23 20 64 65 66 69 6e 65 20 61  ,B,C).# define a
21bba 75 74 6f 49 6e 63 45 6e 64 28 41 2c 42 2c 43 2c  utoIncEnd(A,B,C,
21bbb 44 29 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  D).#endif /* SQL
21bbc 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 43  ITE_OMIT_AUTOINC
21bbd 52 45 4d 45 4e 54 20 2a 2f 0a 0a 0a 2f 2a 20 46  REMENT */.../* F
21bbe 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69  orward declarati
21bbf 6f 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  on */.static int
21bc0 20 78 66 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f   xferOptimizatio
21bc1 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  n(.  Parse *pPar
21bc2 73 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61  se,        /* Pa
21bc3 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
21bc4 20 20 54 61 62 6c 65 20 2a 70 44 65 73 74 2c 20    Table *pDest, 
21bc5 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
21bc6 61 62 6c 65 20 77 65 20 61 72 65 20 69 6e 73 65  able we are inse
21bc7 72 74 69 6e 67 20 69 6e 74 6f 20 2a 2f 0a 20 20  rting into */.  
21bc8 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 2c  Select *pSelect,
21bc9 20 20 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43        /* A SELEC
21bca 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 75  T statement to u
21bcb 73 65 20 61 73 20 74 68 65 20 64 61 74 61 20 73  se as the data s
21bcc 6f 75 72 63 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  ource */.  int o
21bcd 6e 45 72 72 6f 72 2c 20 20 20 20 20 20 20 20 20  nError,         
21bce 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64 6c   /* How to handl
21bcf 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72 72  e constraint err
21bd0 6f 72 73 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62  ors */.  int iDb
21bd1 44 65 73 74 20 20 20 20 20 20 20 20 20 20 20 2f  Dest           /
21bd2 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f  * The database o
21bd3 66 20 70 44 65 73 74 20 2a 2f 0a 29 3b 0a 0a 2f  f pDest */.);../
21bd4 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
21bd5 65 20 69 73 20 63 61 6c 6c 20 74 6f 20 68 61 6e  e is call to han
21bd6 64 6c 65 20 53 51 4c 20 6f 66 20 74 68 65 20 66  dle SQL of the f
21bd7 6f 6c 6c 6f 77 69 6e 67 20 66 6f 72 6d 73 3a 0a  ollowing forms:.
21bd8 2a 2a 0a 2a 2a 20 20 20 20 69 6e 73 65 72 74 20  **.**    insert 
21bd9 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44 4c 49  into TABLE (IDLI
21bda 53 54 29 20 76 61 6c 75 65 73 28 45 58 50 52 4c  ST) values(EXPRL
21bdb 49 53 54 29 0a 2a 2a 20 20 20 20 69 6e 73 65 72  IST).**    inser
21bdc 74 20 69 6e 74 6f 20 54 41 42 4c 45 20 28 49 44  t into TABLE (ID
21bdd 4c 49 53 54 29 20 73 65 6c 65 63 74 0a 2a 2a 0a  LIST) select.**.
21bde 2a 2a 20 54 68 65 20 49 44 4c 49 53 54 20 66 6f  ** The IDLIST fo
21bdf 6c 6c 6f 77 69 6e 67 20 74 68 65 20 74 61 62 6c  llowing the tabl
21be0 65 20 6e 61 6d 65 20 69 73 20 61 6c 77 61 79 73  e name is always
21be1 20 6f 70 74 69 6f 6e 61 6c 2e 20 20 49 66 20 6f   optional.  If o
21be2 6d 69 74 74 65 64 2c 0a 2a 2a 20 74 68 65 6e 20  mitted,.** then 
21be3 61 20 6c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f  a list of all co
21be4 6c 75 6d 6e 73 20 66 6f 72 20 74 68 65 20 74 61  lumns for the ta
21be5 62 6c 65 20 69 73 20 73 75 62 73 74 69 74 75 74  ble is substitut
21be6 65 64 2e 20 20 54 68 65 20 49 44 4c 49 53 54 0a  ed.  The IDLIST.
21be7 2a 2a 20 61 70 70 65 61 72 73 20 69 6e 20 74 68  ** appears in th
21be8 65 20 70 43 6f 6c 75 6d 6e 20 70 61 72 61 6d 65  e pColumn parame
21be9 74 65 72 2e 20 20 70 43 6f 6c 75 6d 6e 20 69 73  ter.  pColumn is
21bea 20 4e 55 4c 4c 20 69 66 20 49 44 4c 49 53 54 20   NULL if IDLIST 
21beb 69 73 20 6f 6d 69 74 74 65 64 2e 0a 2a 2a 0a 2a  is omitted..**.*
21bec 2a 20 54 68 65 20 70 4c 69 73 74 20 70 61 72 61  * The pList para
21bed 6d 65 74 65 72 20 68 6f 6c 64 73 20 45 58 50 52  meter holds EXPR
21bee 4c 49 53 54 20 69 6e 20 74 68 65 20 66 69 72 73  LIST in the firs
21bef 74 20 66 6f 72 6d 20 6f 66 20 74 68 65 20 49 4e  t form of the IN
21bf0 53 45 52 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e  SERT.** statemen
21bf1 74 20 61 62 6f 76 65 2c 20 61 6e 64 20 70 53 65  t above, and pSe
21bf2 6c 65 63 74 20 69 73 20 4e 55 4c 4c 2e 20 20 46  lect is NULL.  F
21bf3 6f 72 20 74 68 65 20 73 65 63 6f 6e 64 20 66 6f  or the second fo
21bf4 72 6d 2c 20 70 4c 69 73 74 20 69 73 0a 2a 2a 20  rm, pList is.** 
21bf5 4e 55 4c 4c 20 61 6e 64 20 70 53 65 6c 65 63 74  NULL and pSelect
21bf6 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f   is a pointer to
21bf7 20 74 68 65 20 73 65 6c 65 63 74 20 73 74 61 74   the select stat
21bf8 65 6d 65 6e 74 20 75 73 65 64 20 74 6f 20 67 65  ement used to ge
21bf9 6e 65 72 61 74 65 0a 2a 2a 20 64 61 74 61 20 66  nerate.** data f
21bfa 6f 72 20 74 68 65 20 69 6e 73 65 72 74 2e 0a 2a  or the insert..*
21bfb 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 67 65  *.** The code ge
21bfc 6e 65 72 61 74 65 64 20 66 6f 6c 6c 6f 77 73 20  nerated follows 
21bfd 6f 6e 65 20 6f 66 20 66 6f 75 72 20 74 65 6d 70  one of four temp
21bfe 6c 61 74 65 73 2e 20 20 46 6f 72 20 61 20 73 69  lates.  For a si
21bff 6d 70 6c 65 0a 2a 2a 20 73 65 6c 65 63 74 20 77  mple.** select w
21c00 69 74 68 20 64 61 74 61 20 63 6f 6d 69 6e 67 20  ith data coming 
21c01 66 72 6f 6d 20 61 20 56 41 4c 55 45 53 20 63 6c  from a VALUES cl
21c02 61 75 73 65 2c 20 74 68 65 20 63 6f 64 65 20 65  ause, the code e
21c03 78 65 63 75 74 65 73 0a 2a 2a 20 6f 6e 63 65 20  xecutes.** once 
21c04 73 74 72 61 69 67 68 74 20 64 6f 77 6e 20 74 68  straight down th
21c05 72 6f 75 67 68 2e 20 20 50 73 65 75 64 6f 2d 63  rough.  Pseudo-c
21c06 6f 64 65 20 66 6f 6c 6c 6f 77 73 20 28 77 65 20  ode follows (we 
21c07 63 61 6c 6c 20 74 68 69 73 0a 2a 2a 20 74 68 65  call this.** the
21c08 20 22 31 73 74 20 74 65 6d 70 6c 61 74 65 22 29   "1st template")
21c09 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
21c0a 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73 6f  open write curso
21c0b 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e 64  r to <table> and
21c0c 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a 20   its indices.** 
21c0d 20 20 20 20 20 20 20 20 70 75 74 73 20 56 41 4c          puts VAL
21c0e 55 45 53 20 63 6c 61 75 73 65 20 65 78 70 72 65  UES clause expre
21c0f 73 73 69 6f 6e 73 20 6f 6e 74 6f 20 74 68 65 20  ssions onto the 
21c10 73 74 61 63 6b 0a 2a 2a 20 20 20 20 20 20 20 20  stack.**        
21c11 20 77 72 69 74 65 20 74 68 65 20 72 65 73 75 6c   write the resul
21c12 74 69 6e 67 20 72 65 63 6f 72 64 20 69 6e 74 6f  ting record into
21c13 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20   <table>.**     
21c14 20 20 20 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a      cleanup.**.*
21c15 2a 20 54 68 65 20 74 68 72 65 65 20 72 65 6d 61  * The three rema
21c16 69 6e 69 6e 67 20 74 65 6d 70 6c 61 74 65 73 20  ining templates 
21c17 61 73 73 75 6d 65 20 74 68 65 20 73 74 61 74 65  assume the state
21c18 6d 65 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66  ment is of the f
21c19 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 49 4e 53 45  orm.**.**   INSE
21c1a 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c 65 3e 20  RT INTO <table> 
21c1b 53 45 4c 45 43 54 20 2e 2e 2e 0a 2a 2a 0a 2a 2a  SELECT ....**.**
21c1c 20 49 66 20 74 68 65 20 53 45 4c 45 43 54 20 63   If the SELECT c
21c1d 6c 61 75 73 65 20 69 73 20 6f 66 20 74 68 65 20  lause is of the 
21c1e 72 65 73 74 72 69 63 74 65 64 20 66 6f 72 6d 20  restricted form 
21c1f 22 53 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 3c  "SELECT * FROM <
21c20 74 61 62 6c 65 32 3e 22 20 2d 0a 2a 2a 20 69 6e  table2>" -.** in
21c21 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66 20   other words if 
21c22 74 68 65 20 53 45 4c 45 43 54 20 70 75 6c 6c 73  the SELECT pulls
21c23 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 66 72 6f   all columns fro
21c24 6d 20 61 20 73 69 6e 67 6c 65 20 74 61 62 6c 65  m a single table
21c25 0a 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 69 73  .** and there is
21c26 20 6e 6f 20 57 48 45 52 45 20 6f 72 20 4c 49 4d   no WHERE or LIM
21c27 49 54 20 6f 72 20 47 52 4f 55 50 20 42 59 20 6f  IT or GROUP BY o
21c28 72 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  r ORDER BY claus
21c29 65 73 2c 20 61 6e 64 0a 2a 2a 20 69 66 20 3c 74  es, and.** if <t
21c2a 61 62 6c 65 32 3e 20 61 6e 64 20 3c 74 61 62 6c  able2> and <tabl
21c2b 65 31 3e 20 61 72 65 20 64 69 73 74 69 6e 63 74  e1> are distinct
21c2c 20 74 61 62 6c 65 73 20 62 75 74 20 68 61 76 65   tables but have
21c2d 20 69 64 65 6e 74 69 63 61 6c 0a 2a 2a 20 73 63   identical.** sc
21c2e 68 65 6d 61 73 2c 20 69 6e 63 6c 75 64 69 6e 67  hemas, including
21c2f 20 61 6c 6c 20 74 68 65 20 73 61 6d 65 20 69 6e   all the same in
21c30 64 69 63 65 73 2c 20 74 68 65 6e 20 61 20 73 70  dices, then a sp
21c31 65 63 69 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69  ecial optimizati
21c32 6f 6e 0a 2a 2a 20 69 73 20 69 6e 76 6f 6b 65 64  on.** is invoked
21c33 20 74 68 61 74 20 63 6f 70 69 65 73 20 72 61 77   that copies raw
21c34 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 20 3c 74   records from <t
21c35 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20 3c  able2> over to <
21c36 74 61 62 6c 65 31 3e 2e 0a 2a 2a 20 53 65 65 20  table1>..** See 
21c37 74 68 65 20 78 66 65 72 4f 70 74 69 6d 69 7a 61  the xferOptimiza
21c38 74 69 6f 6e 28 29 20 66 75 6e 63 74 69 6f 6e 20  tion() function 
21c39 66 6f 72 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e  for the implemen
21c3a 74 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 0a 2a  tation of this.*
21c3b 2a 20 74 65 6d 70 6c 61 74 65 2e 20 20 54 68 69  * template.  Thi
21c3c 73 20 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d  s is the 2nd tem
21c3d 70 6c 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20  plate..**.**    
21c3e 20 20 20 20 20 6f 70 65 6e 20 61 20 77 72 69 74       open a writ
21c3f 65 20 63 75 72 73 6f 72 20 74 6f 20 3c 74 61 62  e cursor to <tab
21c40 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20 6f  le>.**         o
21c41 70 65 6e 20 72 65 61 64 20 63 75 72 73 6f 72 20  pen read cursor 
21c42 6f 6e 20 3c 74 61 62 6c 65 32 3e 0a 2a 2a 20 20  on <table2>.**  
21c43 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72 20         transfer 
21c44 61 6c 6c 20 72 65 63 6f 72 64 73 20 69 6e 20 3c  all records in <
21c45 74 61 62 6c 65 32 3e 20 6f 76 65 72 20 74 6f 20  table2> over to 
21c46 3c 74 61 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20  <table>.**      
21c47 20 20 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73     close cursors
21c48 0a 2a 2a 20 20 20 20 20 20 20 20 20 66 6f 72 65  .**         fore
21c49 61 63 68 20 69 6e 64 65 78 20 6f 6e 20 3c 74 61  ach index on <ta
21c4a 62 6c 65 3e 0a 2a 2a 20 20 20 20 20 20 20 20 20  ble>.**         
21c4b 20 20 6f 70 65 6e 20 61 20 77 72 69 74 65 20 63    open a write c
21c4c 75 72 73 6f 72 20 6f 6e 20 74 68 65 20 3c 74 61  ursor on the <ta
21c4d 62 6c 65 3e 20 69 6e 64 65 78 0a 2a 2a 20 20 20  ble> index.**   
21c4e 20 20 20 20 20 20 20 20 6f 70 65 6e 20 61 20 72          open a r
21c4f 65 61 64 20 63 75 72 73 6f 72 20 6f 6e 20 74 68  ead cursor on th
21c50 65 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 20  e corresponding 
21c51 3c 74 61 62 6c 65 32 3e 20 69 6e 64 65 78 0a 2a  <table2> index.*
21c52 2a 20 20 20 20 20 20 20 20 20 20 20 74 72 61 6e  *           tran
21c53 73 66 65 72 20 61 6c 6c 20 72 65 63 6f 72 64 73  sfer all records
21c54 20 66 72 6f 6d 20 74 68 65 20 72 65 61 64 20 74   from the read t
21c55 6f 20 74 68 65 20 77 72 69 74 65 20 63 75 72 73  o the write curs
21c56 6f 72 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ors.**          
21c57 20 63 6c 6f 73 65 20 63 75 72 73 6f 72 73 0a 2a   close cursors.*
21c58 2a 20 20 20 20 20 20 20 20 20 65 6e 64 20 66 6f  *         end fo
21c59 72 65 61 63 68 0a 2a 2a 0a 2a 2a 20 54 68 65 20  reach.**.** The 
21c5a 33 72 64 20 74 65 6d 70 6c 61 74 65 20 69 73 20  3rd template is 
21c5b 66 6f 72 20 77 68 65 6e 20 74 68 65 20 73 65 63  for when the sec
21c5c 6f 6e 64 20 74 65 6d 70 6c 61 74 65 20 64 6f 65  ond template doe
21c5d 73 20 6e 6f 74 20 61 70 70 6c 79 0a 2a 2a 20 61  s not apply.** a
21c5e 6e 64 20 74 68 65 20 53 45 4c 45 43 54 20 63 6c  nd the SELECT cl
21c5f 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 72 65  ause does not re
21c60 61 64 20 66 72 6f 6d 20 3c 74 61 62 6c 65 3e 20  ad from <table> 
21c61 61 74 20 61 6e 79 20 74 69 6d 65 2e 0a 2a 2a 20  at any time..** 
21c62 54 68 65 20 67 65 6e 65 72 61 74 65 64 20 63 6f  The generated co
21c63 64 65 20 66 6f 6c 6c 6f 77 73 20 74 68 69 73 20  de follows this 
21c64 74 65 6d 70 6c 61 74 65 3a 0a 2a 2a 0a 2a 2a 20  template:.**.** 
21c65 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 30          EOF <- 0
21c66 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d  .**         X <-
21c67 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67 6f   A.**         go
21c68 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a 20  to B.**      A: 
21c69 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53 45  setup for the SE
21c6a 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20  LECT.**         
21c6b 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 72 6f  loop over the ro
21c6c 77 73 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54  ws in the SELECT
21c6d 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f  .**           lo
21c6e 61 64 20 76 61 6c 75 65 73 20 69 6e 74 6f 20 72  ad values into r
21c6f 65 67 69 73 74 65 72 73 20 52 2e 2e 52 2b 6e 0a  egisters R..R+n.
21c70 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 69 65  **           yie
21c71 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20  ld X.**         
21c72 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20 20  end loop.**     
21c73 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74 65      cleanup afte
21c74 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a 20  r the SELECT.** 
21c75 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31          EOF <- 1
21c76 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65 6c  .**         yiel
21c77 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20 67  d X.**         g
21c78 6f 74 6f 20 41 0a 2a 2a 20 20 20 20 20 20 42 3a  oto A.**      B:
21c79 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73   open write curs
21c7a 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e  or to <table> an
21c7b 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a  d its indices.**
21c7c 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64 20 58        C: yield X
21c7d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 66 20 45  .**         if E
21c7e 4f 46 20 67 6f 74 6f 20 44 0a 2a 2a 20 20 20 20  OF goto D.**    
21c7f 20 20 20 20 20 69 6e 73 65 72 74 20 74 68 65 20       insert the 
21c80 73 65 6c 65 63 74 20 72 65 73 75 6c 74 20 69 6e  select result in
21c81 74 6f 20 3c 74 61 62 6c 65 3e 20 66 72 6f 6d 20  to <table> from 
21c82 52 2e 2e 52 2b 6e 0a 2a 2a 20 20 20 20 20 20 20  R..R+n.**       
21c83 20 20 67 6f 74 6f 20 43 0a 2a 2a 20 20 20 20 20    goto C.**     
21c84 20 44 3a 20 63 6c 65 61 6e 75 70 0a 2a 2a 0a 2a   D: cleanup.**.*
21c85 2a 20 54 68 65 20 34 74 68 20 74 65 6d 70 6c 61  * The 4th templa
21c86 74 65 20 69 73 20 75 73 65 64 20 69 66 20 74 68  te is used if th
21c87 65 20 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65  e insert stateme
21c88 6e 74 20 74 61 6b 65 73 20 69 74 73 0a 2a 2a 20  nt takes its.** 
21c89 76 61 6c 75 65 73 20 66 72 6f 6d 20 61 20 53 45  values from a SE
21c8a 4c 45 43 54 20 62 75 74 20 74 68 65 20 64 61 74  LECT but the dat
21c8b 61 20 69 73 20 62 65 69 6e 67 20 69 6e 73 65 72  a is being inser
21c8c 74 65 64 20 69 6e 74 6f 20 61 20 74 61 62 6c 65  ted into a table
21c8d 0a 2a 2a 20 74 68 61 74 20 69 73 20 61 6c 73 6f  .** that is also
21c8e 20 72 65 61 64 20 61 73 20 70 61 72 74 20 6f 66   read as part of
21c8f 20 74 68 65 20 53 45 4c 45 43 54 2e 20 20 49 6e   the SELECT.  In
21c90 20 74 68 65 20 74 68 69 72 64 20 66 6f 72 6d 2c   the third form,
21c91 0a 2a 2a 20 77 65 20 68 61 76 65 20 74 6f 20 75  .** we have to u
21c92 73 65 20 61 20 69 6e 74 65 72 6d 65 64 69 61 74  se a intermediat
21c93 65 20 74 61 62 6c 65 20 74 6f 20 73 74 6f 72 65  e table to store
21c94 20 74 68 65 20 72 65 73 75 6c 74 73 20 6f 66 0a   the results of.
21c95 2a 2a 20 74 68 65 20 73 65 6c 65 63 74 2e 20 20  ** the select.  
21c96 54 68 65 20 74 65 6d 70 6c 61 74 65 20 69 73 20  The template is 
21c97 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a  like this:.**.**
21c98 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20           EOF <- 
21c99 30 0a 2a 2a 20 20 20 20 20 20 20 20 20 58 20 3c  0.**         X <
21c9a 2d 20 41 0a 2a 2a 20 20 20 20 20 20 20 20 20 67  - A.**         g
21c9b 6f 74 6f 20 42 0a 2a 2a 20 20 20 20 20 20 41 3a  oto B.**      A:
21c9c 20 73 65 74 75 70 20 66 6f 72 20 74 68 65 20 53   setup for the S
21c9d 45 4c 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20  ELECT.**        
21c9e 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 65 20 74   loop over the t
21c9f 61 62 6c 65 73 20 69 6e 20 74 68 65 20 53 45 4c  ables in the SEL
21ca0 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ECT.**          
21ca1 20 6c 6f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f   load value into
21ca2 20 72 65 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e   register R..R+n
21ca3 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79 69  .**           yi
21ca4 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20  eld X.**        
21ca5 20 65 6e 64 20 6c 6f 6f 70 0a 2a 2a 20 20 20 20   end loop.**    
21ca6 20 20 20 20 20 63 6c 65 61 6e 75 70 20 61 66 74       cleanup aft
21ca7 65 72 20 74 68 65 20 53 45 4c 45 43 54 0a 2a 2a  er the SELECT.**
21ca8 20 20 20 20 20 20 20 20 20 45 4f 46 20 3c 2d 20           EOF <- 
21ca9 31 0a 2a 2a 20 20 20 20 20 20 20 20 20 79 69 65  1.**         yie
21caa 6c 64 20 58 0a 2a 2a 20 20 20 20 20 20 20 20 20  ld X.**         
21cab 68 61 6c 74 2d 65 72 72 6f 72 0a 2a 2a 20 20 20  halt-error.**   
21cac 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d 70 20     B: open temp 
21cad 74 61 62 6c 65 0a 2a 2a 20 20 20 20 20 20 4c 3a  table.**      L:
21cae 20 79 69 65 6c 64 20 58 0a 2a 2a 20 20 20 20 20   yield X.**     
21caf 20 20 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20      if EOF goto 
21cb0 4d 0a 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 73  M.**         ins
21cb1 65 72 74 20 72 6f 77 20 66 72 6f 6d 20 52 2e 2e  ert row from R..
21cb2 52 2b 6e 20 69 6e 74 6f 20 74 65 6d 70 20 74 61  R+n into temp ta
21cb3 62 6c 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 67  ble.**         g
21cb4 6f 74 6f 20 4c 0a 2a 2a 20 20 20 20 20 20 4d 3a  oto L.**      M:
21cb5 20 6f 70 65 6e 20 77 72 69 74 65 20 63 75 72 73   open write curs
21cb6 6f 72 20 74 6f 20 3c 74 61 62 6c 65 3e 20 61 6e  or to <table> an
21cb7 64 20 69 74 73 20 69 6e 64 69 63 65 73 0a 2a 2a  d its indices.**
21cb8 20 20 20 20 20 20 20 20 20 72 65 77 69 6e 64 20           rewind 
21cb9 74 65 6d 70 20 74 61 62 6c 65 0a 2a 2a 20 20 20  temp table.**   
21cba 20 20 20 43 3a 20 6c 6f 6f 70 20 6f 76 65 72 20     C: loop over 
21cbb 72 6f 77 73 20 6f 66 20 69 6e 74 65 72 6d 65 64  rows of intermed
21cbc 69 61 74 65 20 74 61 62 6c 65 0a 2a 2a 20 20 20  iate table.**   
21cbd 20 20 20 20 20 20 20 20 74 72 61 6e 73 66 65 72          transfer
21cbe 20 76 61 6c 75 65 73 20 66 6f 72 6d 20 69 6e 74   values form int
21cbf 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 20  ermediate table 
21cc0 69 6e 74 6f 20 3c 74 61 62 6c 65 3e 0a 2a 2a 20  into <table>.** 
21cc1 20 20 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70          end loop
21cc2 0a 2a 2a 20 20 20 20 20 20 44 3a 20 63 6c 65 61  .**      D: clea
21cc3 6e 75 70 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  nup.*/.SQLITE_PR
21cc4 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
21cc5 65 33 49 6e 73 65 72 74 28 0a 20 20 50 61 72 73  e3Insert(.  Pars
21cc6 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
21cc7 20 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74    /* Parser cont
21cc8 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74  ext */.  SrcList
21cc9 20 2a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 2f   *pTabList,    /
21cca 2a 20 4e 61 6d 65 20 6f 66 20 74 61 62 6c 65 20  * Name of table 
21ccb 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 61 72  into which we ar
21ccc 65 20 69 6e 73 65 72 74 69 6e 67 20 2a 2f 0a 20  e inserting */. 
21ccd 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
21cce 2c 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ,      /* List o
21ccf 66 20 76 61 6c 75 65 73 20 74 6f 20 62 65 20 69  f values to be i
21cd0 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 53 65 6c  nserted */.  Sel
21cd1 65 63 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20  ect *pSelect,   
21cd2 20 20 20 2f 2a 20 41 20 53 45 4c 45 43 54 20 73     /* A SELECT s
21cd3 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 73 65 20  tatement to use 
21cd4 61 73 20 74 68 65 20 64 61 74 61 20 73 6f 75 72  as the data sour
21cd5 63 65 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a  ce */.  IdList *
21cd6 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 2f 2a  pColumn,      /*
21cd7 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 63 6f   Column names co
21cd8 72 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 49  rresponding to I
21cd9 44 4c 49 53 54 2e 20 2a 2f 0a 20 20 69 6e 74 20  DLIST. */.  int 
21cda 6f 6e 45 72 72 6f 72 20 20 20 20 20 20 20 20 20  onError         
21cdb 20 20 2f 2a 20 48 6f 77 20 74 6f 20 68 61 6e 64    /* How to hand
21cdc 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 65 72  le constraint er
21cdd 72 6f 72 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  rors */.){.  sql
21cde 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
21cdf 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20 64     /* The main d
21ce0 61 74 61 62 61 73 65 20 73 74 72 75 63 74 75 72  atabase structur
21ce1 65 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54  e */.  Table *pT
21ce2 61 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ab;          /* 
21ce3 54 68 65 20 74 61 62 6c 65 20 74 6f 20 69 6e 73  The table to ins
21ce4 65 72 74 20 69 6e 74 6f 2e 20 20 61 6b 61 20 54  ert into.  aka T
21ce5 41 42 4c 45 20 2a 2f 0a 20 20 63 68 61 72 20 2a  ABLE */.  char *
21ce6 7a 54 61 62 3b 20 20 20 20 20 20 20 20 20 20 20  zTab;           
21ce7 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 74  /* Name of the t
21ce8 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20  able into which 
21ce9 77 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67  we are inserting
21cea 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
21ceb 20 2a 7a 44 62 3b 20 20 20 20 20 20 2f 2a 20 4e   *zDb;      /* N
21cec 61 6d 65 20 6f 66 20 74 68 65 20 64 61 74 61 62  ame of the datab
21ced 61 73 65 20 68 6f 6c 64 69 6e 67 20 74 68 69 73  ase holding this
21cee 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20   table */.  int 
21cef 69 2c 20 6a 2c 20 69 64 78 3b 20 20 20 20 20 20  i, j, idx;      
21cf0 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
21cf1 72 73 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b  rs */.  Vdbe *v;
21cf2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
21cf3 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 69   Generate code i
21cf4 6e 74 6f 20 74 68 69 73 20 76 69 72 74 75 61 6c  nto this virtual
21cf5 20 6d 61 63 68 69 6e 65 20 2a 2f 0a 20 20 49 6e   machine */.  In
21cf6 64 65 78 20 2a 70 49 64 78 3b 20 20 20 20 20 20  dex *pIdx;      
21cf7 20 20 20 20 2f 2a 20 46 6f 72 20 6c 6f 6f 70 69      /* For loopi
21cf8 6e 67 20 6f 76 65 72 20 69 6e 64 69 63 65 73 20  ng over indices 
21cf9 6f 66 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a  of the table */.
21cfa 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
21cfb 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
21cfc 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20  r of columns in 
21cfd 74 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e  the data */.  in
21cfe 74 20 6e 48 69 64 64 65 6e 20 3d 20 30 3b 20 20  t nHidden = 0;  
21cff 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
21d00 20 68 69 64 64 65 6e 20 63 6f 6c 75 6d 6e 73 20   hidden columns 
21d01 69 66 20 54 41 42 4c 45 20 69 73 20 76 69 72 74  if TABLE is virt
21d02 75 61 6c 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73  ual */.  int bas
21d03 65 43 75 72 20 3d 20 30 3b 20 20 20 20 20 20 2f  eCur = 0;      /
21d04 2a 20 56 44 42 45 20 43 75 72 73 6f 72 20 6e 75  * VDBE Cursor nu
21d05 6d 62 65 72 20 66 6f 72 20 70 54 61 62 20 2a 2f  mber for pTab */
21d06 0a 20 20 69 6e 74 20 6b 65 79 43 6f 6c 75 6d 6e  .  int keyColumn
21d07 20 3d 20 2d 31 3b 20 20 20 2f 2a 20 43 6f 6c 75   = -1;   /* Colu
21d08 6d 6e 20 74 68 61 74 20 69 73 20 74 68 65 20 49  mn that is the I
21d09 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
21d0a 45 59 20 2a 2f 0a 20 20 69 6e 74 20 65 6e 64 4f  EY */.  int endO
21d0b 66 4c 6f 6f 70 3b 20 20 20 20 20 20 20 20 2f 2a  fLoop;        /*
21d0c 20 4c 61 62 65 6c 20 66 6f 72 20 74 68 65 20 65   Label for the e
21d0d 6e 64 20 6f 66 20 74 68 65 20 69 6e 73 65 72 74  nd of the insert
21d0e 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e  ion loop */.  in
21d0f 74 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 3d  t useTempTable =
21d10 20 30 3b 20 2f 2a 20 53 74 6f 72 65 20 53 45 4c   0; /* Store SEL
21d11 45 43 54 20 72 65 73 75 6c 74 73 20 69 6e 20 69  ECT results in i
21d12 6e 74 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c  ntermediate tabl
21d13 65 20 2a 2f 0a 20 20 69 6e 74 20 73 72 63 54 61  e */.  int srcTa
21d14 62 20 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20  b = 0;       /* 
21d15 44 61 74 61 20 63 6f 6d 65 73 20 66 72 6f 6d 20  Data comes from 
21d16 74 68 69 73 20 74 65 6d 70 6f 72 61 72 79 20 63  this temporary c
21d17 75 72 73 6f 72 20 69 66 20 3e 3d 30 20 2a 2f 0a  ursor if >=0 */.
21d18 20 20 69 6e 74 20 61 64 64 72 49 6e 73 54 6f 70    int addrInsTop
21d19 20 3d 20 30 3b 20 20 20 2f 2a 20 4a 75 6d 70 20   = 0;   /* Jump 
21d1a 74 6f 20 6c 61 62 65 6c 20 22 44 22 20 2a 2f 0a  to label "D" */.
21d1b 20 20 69 6e 74 20 61 64 64 72 43 6f 6e 74 20 3d    int addrCont =
21d1c 20 30 3b 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f   0;     /* Top o
21d1d 66 20 69 6e 73 65 72 74 20 6c 6f 6f 70 2e 20 4c  f insert loop. L
21d1e 61 62 65 6c 20 22 43 22 20 69 6e 20 74 65 6d 70  abel "C" in temp
21d1f 6c 61 74 65 73 20 33 20 61 6e 64 20 34 20 2a 2f  lates 3 and 4 */
21d20 0a 20 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63  .  int addrSelec
21d21 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 64 64 72  t = 0;   /* Addr
21d22 65 73 73 20 6f 66 20 63 6f 72 6f 75 74 69 6e 65  ess of coroutine
21d23 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73   that implements
21d24 20 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20   the SELECT */. 
21d25 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
21d26 3b 20 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e  ;      /* Destin
21d27 61 74 69 6f 6e 20 66 6f 72 20 53 45 4c 45 43 54  ation for SELECT
21d28 20 6f 6e 20 72 68 73 20 6f 66 20 49 4e 53 45 52   on rhs of INSER
21d29 54 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77 49 64  T */.  int newId
21d2a 78 20 3d 20 2d 31 3b 20 20 20 20 20 20 2f 2a 20  x = -1;      /* 
21d2b 43 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 4e  Cursor for the N
21d2c 45 57 20 70 73 65 75 64 6f 2d 74 61 62 6c 65 20  EW pseudo-table 
21d2d 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20  */.  int iDb;   
21d2e 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
21d2f 64 65 78 20 6f 66 20 64 61 74 61 62 61 73 65 20  dex of database 
21d30 68 6f 6c 64 69 6e 67 20 54 41 42 4c 45 20 2a 2f  holding TABLE */
21d31 0a 20 20 44 62 20 2a 70 44 62 3b 20 20 20 20 20  .  Db *pDb;     
21d32 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
21d33 64 61 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e  database contain
21d34 69 6e 67 20 74 61 62 6c 65 20 62 65 69 6e 67 20  ing table being 
21d35 69 6e 73 65 72 74 65 64 20 69 6e 74 6f 20 2a 2f  inserted into */
21d36 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 46 6c 61  .  int appendFla
21d37 67 20 3d 20 30 3b 20 20 20 2f 2a 20 54 72 75 65  g = 0;   /* True
21d38 20 69 66 20 74 68 65 20 69 6e 73 65 72 74 20 69   if the insert i
21d39 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20 61  s likely to be a
21d3a 6e 20 61 70 70 65 6e 64 20 2a 2f 0a 0a 20 20 2f  n append */..  /
21d3b 2a 20 52 65 67 69 73 74 65 72 20 61 6c 6c 6f 63  * Register alloc
21d3c 61 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  ations */.  int 
21d3d 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 3b 20 20  regFromSelect;  
21d3e 20 20 2f 2a 20 42 61 73 65 20 72 65 67 69 73 74    /* Base regist
21d3f 65 72 20 66 6f 72 20 64 61 74 61 20 63 6f 6d 69  er for data comi
21d40 6e 67 20 66 72 6f 6d 20 53 45 4c 45 43 54 20 2a  ng from SELECT *
21d41 2f 0a 20 20 69 6e 74 20 72 65 67 41 75 74 6f 69  /.  int regAutoi
21d42 6e 63 20 3d 20 30 3b 20 20 20 2f 2a 20 52 65 67  nc = 0;   /* Reg
21d43 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74 68  ister holding th
21d44 65 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20  e AUTOINCREMENT 
21d45 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74  counter */.  int
21d46 20 72 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 30   regRowCount = 0
21d47 3b 20 20 2f 2a 20 4d 65 6d 6f 72 79 20 63 65 6c  ;  /* Memory cel
21d48 6c 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 72  l used for the r
21d49 6f 77 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20  ow counter */.  
21d4a 69 6e 74 20 72 65 67 49 6e 73 3b 20 20 20 20 20  int regIns;     
21d4b 20 20 20 20 20 20 2f 2a 20 42 6c 6f 63 6b 20 6f        /* Block o
21d4c 66 20 72 65 67 73 20 68 6f 6c 64 69 6e 67 20 72  f regs holding r
21d4d 6f 77 69 64 2b 64 61 74 61 20 62 65 69 6e 67 20  owid+data being 
21d4e 69 6e 73 65 72 74 65 64 20 2a 2f 0a 20 20 69 6e  inserted */.  in
21d4f 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20 20 20  t regRowid;     
21d50 20 20 20 20 2f 2a 20 72 65 67 69 73 74 65 72 73      /* registers
21d51 20 68 6f 6c 64 69 6e 67 20 69 6e 73 65 72 74 20   holding insert 
21d52 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 72  rowid */.  int r
21d53 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20 20  egData;         
21d54 20 2f 2a 20 72 65 67 69 73 74 65 72 20 68 6f 6c   /* register hol
21d55 64 69 6e 67 20 66 69 72 73 74 20 63 6f 6c 75 6d  ding first colum
21d56 6e 20 74 6f 20 69 6e 73 65 72 74 20 2a 2f 0a 20  n to insert */. 
21d57 20 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 20   int regRecord; 
21d58 20 20 20 20 20 20 20 2f 2a 20 48 6f 6c 64 73 20         /* Holds 
21d59 74 68 65 20 61 73 73 65 6d 62 6c 69 65 64 20 72  the assemblied r
21d5a 6f 77 20 72 65 63 6f 72 64 20 2a 2f 0a 20 20 69  ow record */.  i
21d5b 6e 74 20 72 65 67 45 6f 66 3b 20 20 20 20 20 20  nt regEof;      
21d5c 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
21d5d 20 72 65 63 6f 72 64 69 6e 67 20 65 6e 64 20 6f   recording end o
21d5e 66 20 53 45 4c 45 43 54 20 64 61 74 61 20 2a 2f  f SELECT data */
21d5f 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20  .  int *aRegIdx 
21d60 3d 20 30 3b 20 20 20 20 20 2f 2a 20 4f 6e 65 20  = 0;     /* One 
21d61 72 65 67 69 73 74 65 72 20 61 6c 6c 6f 63 61 74  register allocat
21d62 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78  ed to each index
21d63 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66 20 53 51   */...#ifndef SQ
21d64 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
21d65 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77 3b 20  R.  int isView; 
21d66 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21d67 2f 2a 20 54 72 75 65 20 69 66 20 61 74 74 65 6d  /* True if attem
21d68 70 74 69 6e 67 20 74 6f 20 69 6e 73 65 72 74 20  pting to insert 
21d69 69 6e 74 6f 20 61 20 76 69 65 77 20 2a 2f 0a 20  into a view */. 
21d6a 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f 65 78   int triggers_ex
21d6b 69 73 74 20 3d 20 30 3b 20 20 20 20 20 2f 2a 20  ist = 0;     /* 
21d6c 54 72 75 65 20 69 66 20 74 68 65 72 65 20 61 72  True if there ar
21d6d 65 20 46 4f 52 20 45 41 43 48 20 52 4f 57 20 74  e FOR EACH ROW t
21d6e 72 69 67 67 65 72 73 20 2a 2f 0a 23 65 6e 64 69  riggers */.#endi
21d6f 66 0a 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65  f..  db = pParse
21d70 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 50 61 72  ->db;.  if( pPar
21d71 73 65 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e  se->nErr || db->
21d72 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a  mallocFailed ){.
21d73 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f      goto insert_
21d74 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20  cleanup;.  }..  
21d75 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20 74 61  /* Locate the ta
21d76 62 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77  ble into which w
21d77 65 20 77 69 6c 6c 20 62 65 20 69 6e 73 65 72 74  e will be insert
21d78 69 6e 67 20 6e 65 77 20 69 6e 66 6f 72 6d 61 74  ing new informat
21d79 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65  ion..  */.  asse
21d7a 72 74 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53  rt( pTabList->nS
21d7b 72 63 3d 3d 31 20 29 3b 0a 20 20 7a 54 61 62 20  rc==1 );.  zTab 
21d7c 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d  = pTabList->a[0]
21d7d 2e 7a 4e 61 6d 65 3b 0a 20 20 69 66 28 20 7a 54  .zName;.  if( zT
21d7e 61 62 3d 3d 30 20 29 20 67 6f 74 6f 20 69 6e 73  ab==0 ) goto ins
21d7f 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 70  ert_cleanup;.  p
21d80 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
21d81 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
21d82 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
21d83 69 66 28 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20  if( pTab==0 ){. 
21d84 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
21d85 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 44  leanup;.  }.  iD
21d86 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d  b = sqlite3Schem
21d87 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70 54 61  aToIndex(db, pTa
21d88 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61  b->pSchema);.  a
21d89 73 73 65 72 74 28 20 69 44 62 3c 64 62 2d 3e 6e  ssert( iDb<db->n
21d8a 44 62 20 29 3b 0a 20 20 70 44 62 20 3d 20 26 64  Db );.  pDb = &d
21d8b 62 2d 3e 61 44 62 5b 69 44 62 5d 3b 0a 20 20 7a  b->aDb[iDb];.  z
21d8c 44 62 20 3d 20 70 44 62 2d 3e 7a 4e 61 6d 65 3b  Db = pDb->zName;
21d8d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 41 75  .  if( sqlite3Au
21d8e 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20  thCheck(pParse, 
21d8f 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20 70  SQLITE_INSERT, p
21d90 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 30 2c 20 7a  Tab->zName, 0, z
21d91 44 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  Db) ){.    goto 
21d92 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
21d93 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65    }..  /* Figure
21d94 20 6f 75 74 20 69 66 20 77 65 20 68 61 76 65 20   out if we have 
21d95 61 6e 79 20 74 72 69 67 67 65 72 73 20 61 6e 64  any triggers and
21d96 20 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65   if the table be
21d97 69 6e 67 0a 20 20 2a 2a 20 69 6e 73 65 72 74 65  ing.  ** inserte
21d98 64 20 69 6e 74 6f 20 69 73 20 61 20 76 69 65 77  d into is a view
21d99 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51  .  */.#ifndef SQ
21d9a 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
21d9b 52 0a 20 20 74 72 69 67 67 65 72 73 5f 65 78 69  R.  triggers_exi
21d9c 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72 69 67  st = sqlite3Trig
21d9d 67 65 72 73 45 78 69 73 74 28 70 54 61 62 2c 20  gersExist(pTab, 
21d9e 54 4b 5f 49 4e 53 45 52 54 2c 20 30 29 3b 0a 20  TK_INSERT, 0);. 
21d9f 20 69 73 56 69 65 77 20 3d 20 70 54 61 62 2d 3e   isView = pTab->
21da0 70 53 65 6c 65 63 74 21 3d 30 3b 0a 23 65 6c 73  pSelect!=0;.#els
21da1 65 0a 23 20 64 65 66 69 6e 65 20 74 72 69 67 67  e.# define trigg
21da2 65 72 73 5f 65 78 69 73 74 20 30 0a 23 20 64 65  ers_exist 0.# de
21da3 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65  fine isView 0.#e
21da4 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
21da5 54 45 5f 4f 4d 49 54 5f 56 49 45 57 0a 23 20 75  TE_OMIT_VIEW.# u
21da6 6e 64 65 66 20 69 73 56 69 65 77 0a 23 20 64 65  ndef isView.# de
21da7 66 69 6e 65 20 69 73 56 69 65 77 20 30 0a 23 65  fine isView 0.#e
21da8 6e 64 69 66 0a 0a 20 20 2f 2a 20 45 6e 73 75 72  ndif..  /* Ensur
21da9 65 20 74 68 61 74 3a 0a 20 20 2a 20 20 28 61 29  e that:.  *  (a)
21daa 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f   the table is no
21dab 74 20 72 65 61 64 2d 6f 6e 6c 79 2c 20 0a 20 20  t read-only, .  
21dac 2a 20 20 28 62 29 20 74 68 61 74 20 69 66 20 69  *  (b) that if i
21dad 74 20 69 73 20 61 20 76 69 65 77 20 74 68 65 6e  t is a view then
21dae 20 4f 4e 20 49 4e 53 45 52 54 20 74 72 69 67 67   ON INSERT trigg
21daf 65 72 73 20 65 78 69 73 74 0a 20 20 2a 2f 0a 20  ers exist.  */. 
21db0 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 65   if( sqlite3IsRe
21db1 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70  adOnly(pParse, p
21db2 54 61 62 2c 20 74 72 69 67 67 65 72 73 5f 65 78  Tab, triggers_ex
21db3 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ist) ){.    goto
21db4 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
21db5 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
21db6 54 61 62 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  Tab!=0 );..  /* 
21db7 49 66 20 70 54 61 62 20 69 73 20 72 65 61 6c 6c  If pTab is reall
21db8 79 20 61 20 76 69 65 77 2c 20 6d 61 6b 65 20 73  y a view, make s
21db9 75 72 65 20 69 74 20 68 61 73 20 62 65 65 6e 20  ure it has been 
21dba 69 6e 69 74 69 61 6c 69 7a 65 64 2e 0a 20 20 2a  initialized..  *
21dbb 2a 20 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  * ViewGetColumnN
21dbc 61 6d 65 73 28 29 20 69 73 20 61 20 6e 6f 2d 6f  ames() is a no-o
21dbd 70 20 69 66 20 70 54 61 62 20 69 73 20 6e 6f 74  p if pTab is not
21dbe 20 61 20 76 69 65 77 20 28 6f 72 20 76 69 72 74   a view (or virt
21dbf 75 61 6c 20 0a 20 20 2a 2a 20 6d 6f 64 75 6c 65  ual .  ** module
21dc0 20 74 61 62 6c 65 29 2e 0a 20 20 2a 2f 0a 20 20   table)..  */.  
21dc1 69 66 28 20 73 71 6c 69 74 65 33 56 69 65 77 47  if( sqlite3ViewG
21dc2 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  etColumnNames(pP
21dc3 61 72 73 65 2c 20 70 54 61 62 29 20 29 7b 0a 20  arse, pTab) ){. 
21dc4 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63     goto insert_c
21dc5 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f  leanup;.  }..  /
21dc6 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 56 44 42  * Allocate a VDB
21dc7 45 0a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c  E.  */.  v = sql
21dc8 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
21dc9 73 65 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20  se);.  if( v==0 
21dca 29 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c  ) goto insert_cl
21dcb 65 61 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61  eanup;.  if( pPa
21dcc 72 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29  rse->nested==0 )
21dcd 20 73 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e   sqlite3VdbeCoun
21dce 74 43 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73  tChanges(v);.  s
21dcf 71 6c 69 74 65 33 42 65 67 69 6e 57 72 69 74 65  qlite3BeginWrite
21dd0 4f 70 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65  Operation(pParse
21dd1 2c 20 70 53 65 6c 65 63 74 20 7c 7c 20 74 72 69  , pSelect || tri
21dd2 67 67 65 72 73 5f 65 78 69 73 74 2c 20 69 44 62  ggers_exist, iDb
21dd3 29 3b 0a 0a 20 20 2f 2a 20 69 66 20 74 68 65 72  );..  /* if ther
21dd4 65 20 61 72 65 20 72 6f 77 20 74 72 69 67 67 65  e are row trigge
21dd5 72 73 2c 20 61 6c 6c 6f 63 61 74 65 20 61 20 74  rs, allocate a t
21dd6 65 6d 70 20 74 61 62 6c 65 20 66 6f 72 20 6e 65  emp table for ne
21dd7 77 2e 2a 20 72 65 66 65 72 65 6e 63 65 73 2e 20  w.* references. 
21dd8 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67 65 72  */.  if( trigger
21dd9 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20 6e  s_exist ){.    n
21dda 65 77 49 64 78 20 3d 20 70 50 61 72 73 65 2d 3e  ewIdx = pParse->
21ddb 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a 23 69 66  nTab++;.  }..#if
21ddc 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
21ddd 5f 58 46 45 52 5f 4f 50 54 0a 20 20 2f 2a 20 49  _XFER_OPT.  /* I
21dde 66 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 20  f the statement 
21ddf 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20  is of the form. 
21de0 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 20 49   **.  **       I
21de1 4e 53 45 52 54 20 49 4e 54 4f 20 3c 74 61 62 6c  NSERT INTO <tabl
21de2 65 31 3e 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  e1> SELECT * FRO
21de3 4d 20 3c 74 61 62 6c 65 32 3e 3b 0a 20 20 2a 2a  M <table2>;.  **
21de4 0a 20 20 2a 2a 20 54 68 65 6e 20 73 70 65 63 69  .  ** Then speci
21de5 61 6c 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  al optimizations
21de6 20 63 61 6e 20 62 65 20 61 70 70 6c 69 65 64 20   can be applied 
21de7 74 68 61 74 20 6d 61 6b 65 20 74 68 65 20 74 72  that make the tr
21de8 61 6e 73 66 65 72 0a 20 20 2a 2a 20 76 65 72 79  ansfer.  ** very
21de9 20 66 61 73 74 20 61 6e 64 20 77 68 69 63 68 20   fast and which 
21dea 72 65 64 75 63 65 20 66 72 61 67 6d 65 6e 74 61  reduce fragmenta
21deb 74 69 6f 6e 20 6f 66 20 69 6e 64 69 63 65 73 2e  tion of indices.
21dec 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
21ded 69 73 20 74 68 65 20 32 6e 64 20 74 65 6d 70 6c  is the 2nd templ
21dee 61 74 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ate..  */.  if( 
21def 70 43 6f 6c 75 6d 6e 3d 3d 30 20 26 26 20 78 66  pColumn==0 && xf
21df0 65 72 4f 70 74 69 6d 69 7a 61 74 69 6f 6e 28 70  erOptimization(p
21df1 50 61 72 73 65 2c 20 70 54 61 62 2c 20 70 53 65  Parse, pTab, pSe
21df2 6c 65 63 74 2c 20 6f 6e 45 72 72 6f 72 2c 20 69  lect, onError, i
21df3 44 62 29 20 29 7b 0a 20 20 20 20 61 73 73 65 72  Db) ){.    asser
21df4 74 28 20 21 74 72 69 67 67 65 72 73 5f 65 78 69  t( !triggers_exi
21df5 73 74 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  st );.    assert
21df6 28 20 70 4c 69 73 74 3d 3d 30 20 29 3b 0a 20 20  ( pList==0 );.  
21df7 20 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c    goto insert_cl
21df8 65 61 6e 75 70 3b 0a 20 20 7d 0a 23 65 6e 64 69  eanup;.  }.#endi
21df9 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
21dfa 5f 58 46 45 52 5f 4f 50 54 20 2a 2f 0a 0a 20 20  _XFER_OPT */..  
21dfb 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 6e  /* If this is an
21dfc 20 41 55 54 4f 49 4e 43 52 45 4d 45 4e 54 20 74   AUTOINCREMENT t
21dfd 61 62 6c 65 2c 20 6c 6f 6f 6b 20 75 70 20 74 68  able, look up th
21dfe 65 20 73 65 71 75 65 6e 63 65 20 6e 75 6d 62 65  e sequence numbe
21dff 72 20 69 6e 20 74 68 65 0a 20 20 2a 2a 20 73 71  r in the.  ** sq
21e00 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61  lite_sequence ta
21e01 62 6c 65 20 61 6e 64 20 73 74 6f 72 65 20 69 74  ble and store it
21e02 20 69 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20   in memory cell 
21e03 72 65 67 41 75 74 6f 69 6e 63 2e 0a 20 20 2a 2f  regAutoinc..  */
21e04 0a 20 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20  .  regAutoinc = 
21e05 61 75 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61  autoIncBegin(pPa
21e06 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62 29 3b  rse, iDb, pTab);
21e07 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75  ..  /* Figure ou
21e08 74 20 68 6f 77 20 6d 61 6e 79 20 63 6f 6c 75 6d  t how many colum
21e09 6e 73 20 6f 66 20 64 61 74 61 20 61 72 65 20 73  ns of data are s
21e0a 75 70 70 6c 69 65 64 2e 20 20 49 66 20 74 68 65  upplied.  If the
21e0b 20 64 61 74 61 0a 20 20 2a 2a 20 69 73 20 63 6f   data.  ** is co
21e0c 6d 69 6e 67 20 66 72 6f 6d 20 61 20 53 45 4c 45  ming from a SELE
21e0d 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68  CT statement, th
21e0e 65 6e 20 67 65 6e 65 72 61 74 65 20 61 20 63 6f  en generate a co
21e0f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 0a 20 20  -routine that.  
21e10 2a 2a 20 70 72 6f 64 75 63 65 73 20 61 20 73 69  ** produces a si
21e11 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20  ngle row of the 
21e12 53 45 4c 45 43 54 20 6f 6e 20 65 61 63 68 20 69  SELECT on each i
21e13 6e 76 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 0a  nvocation.  The.
21e14 20 20 2a 2a 20 63 6f 2d 72 6f 75 74 69 6e 65 20    ** co-routine 
21e15 69 73 20 74 68 65 20 63 6f 6d 6d 6f 6e 20 68 65  is the common he
21e16 61 64 65 72 20 74 6f 20 74 68 65 20 33 72 64 20  ader to the 3rd 
21e17 61 6e 64 20 34 74 68 20 74 65 6d 70 6c 61 74 65  and 4th template
21e18 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  s..  */.  if( pS
21e19 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 2f 2a 20  elect ){.    /* 
21e1a 44 61 74 61 20 69 73 20 63 6f 6d 69 6e 67 20 66  Data is coming f
21e1b 72 6f 6d 20 61 20 53 45 4c 45 43 54 2e 20 20 47  rom a SELECT.  G
21e1c 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
21e1d 69 6d 70 6c 65 6d 65 6e 74 20 74 68 61 74 20 53  implement that S
21e1e 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 61 73 20  ELECT.    ** as 
21e1f 61 20 63 6f 2d 72 6f 75 74 69 6e 65 2e 20 20 54  a co-routine.  T
21e20 68 65 20 63 6f 64 65 20 69 73 20 63 6f 6d 6d 6f  he code is commo
21e21 6e 20 74 6f 20 62 6f 74 68 20 74 68 65 20 33 72  n to both the 3r
21e22 64 20 61 6e 64 20 34 74 68 0a 20 20 20 20 2a 2a  d and 4th.    **
21e23 20 74 65 6d 70 6c 61 74 65 73 3a 0a 20 20 20 20   templates:.    
21e24 2a 2a 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20  **.    **       
21e25 20 20 45 4f 46 20 3c 2d 20 30 0a 20 20 20 20 2a    EOF <- 0.    *
21e26 2a 20 20 20 20 20 20 20 20 20 58 20 3c 2d 20 41  *         X <- A
21e27 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
21e28 67 6f 74 6f 20 42 0a 20 20 20 20 2a 2a 20 20 20  goto B.    **   
21e29 20 20 20 41 3a 20 73 65 74 75 70 20 66 6f 72 20     A: setup for 
21e2a 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20 20 2a  the SELECT.    *
21e2b 2a 20 20 20 20 20 20 20 20 20 6c 6f 6f 70 20 6f  *         loop o
21e2c 76 65 72 20 74 68 65 20 74 61 62 6c 65 73 20 69  ver the tables i
21e2d 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 20 20 20  n the SELECT.   
21e2e 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 6c 6f   **           lo
21e2f 61 64 20 76 61 6c 75 65 20 69 6e 74 6f 20 72 65  ad value into re
21e30 67 69 73 74 65 72 20 52 2e 2e 52 2b 6e 0a 20 20  gister R..R+n.  
21e31 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 79    **           y
21e32 69 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20  ield X.    **   
21e33 20 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20        end loop. 
21e34 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6c     **         cl
21e35 65 61 6e 75 70 20 61 66 74 65 72 20 74 68 65 20  eanup after the 
21e36 53 45 4c 45 43 54 0a 20 20 20 20 2a 2a 20 20 20  SELECT.    **   
21e37 20 20 20 20 20 20 45 4f 46 20 3c 2d 20 31 0a 20        EOF <- 1. 
21e38 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 79 69     **         yi
21e39 65 6c 64 20 58 0a 20 20 20 20 2a 2a 20 20 20 20  eld X.    **    
21e3a 20 20 20 20 20 68 61 6c 74 2d 65 72 72 6f 72 0a       halt-error.
21e3b 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 4f 6e      **.    ** On
21e3c 20 65 61 63 68 20 69 6e 76 6f 63 61 74 69 6f 6e   each invocation
21e3d 20 6f 66 20 74 68 65 20 63 6f 2d 72 6f 75 74 69   of the co-routi
21e3e 6e 65 2c 20 69 74 20 70 75 74 73 20 61 20 73 69  ne, it puts a si
21e3f 6e 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 0a  ngle row of the.
21e40 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20 72 65      ** SELECT re
21e41 73 75 6c 74 20 69 6e 74 6f 20 72 65 67 69 73 74  sult into regist
21e42 65 72 73 20 64 65 73 74 2e 69 4d 65 6d 2e 2e 2e  ers dest.iMem...
21e43 64 65 73 74 2e 69 4d 65 6d 2b 64 65 73 74 2e 6e  dest.iMem+dest.n
21e44 4d 65 6d 2d 31 2e 0a 20 20 20 20 2a 2a 20 28 54  Mem-1..    ** (T
21e45 68 65 73 65 20 6f 75 74 70 75 74 20 72 65 67 69  hese output regi
21e46 73 74 65 72 73 20 61 72 65 20 61 6c 6c 6f 63 61  sters are alloca
21e47 74 65 64 20 62 79 20 73 71 6c 69 74 65 33 53 65  ted by sqlite3Se
21e48 6c 65 63 74 28 29 2e 29 20 20 57 68 65 6e 0a 20  lect().)  When. 
21e49 20 20 20 2a 2a 20 74 68 65 20 53 45 4c 45 43 54     ** the SELECT
21e4a 20 63 6f 6d 70 6c 65 74 65 73 2c 20 69 74 20 73   completes, it s
21e4b 65 74 73 20 74 68 65 20 45 4f 46 20 66 6c 61 67  ets the EOF flag
21e4c 20 73 74 6f 72 65 64 20 69 6e 20 72 65 67 45 6f   stored in regEo
21e4d 66 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 6e  f..    */.    in
21e4e 74 20 72 63 2c 20 6a 31 3b 0a 0a 20 20 20 20 72  t rc, j1;..    r
21e4f 65 67 45 6f 66 20 3d 20 2b 2b 70 50 61 72 73 65  egEof = ++pParse
21e50 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69  ->nMem;.    sqli
21e51 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
21e52 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
21e53 72 65 67 45 6f 66 29 3b 20 20 20 20 20 20 2f 2a  regEof);      /*
21e54 20 45 4f 46 20 3c 2d 20 30 20 2a 2f 0a 20 20 20   EOF <- 0 */.   
21e55 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c   VdbeComment((v,
21e56 20 22 53 45 4c 45 43 54 20 65 6f 66 20 66 6c 61   "SELECT eof fla
21e57 67 22 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  g"));.    sqlite
21e58 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
21e59 26 64 65 73 74 2c 20 53 52 54 5f 43 6f 72 6f 75  &dest, SRT_Corou
21e5a 74 69 6e 65 2c 20 2b 2b 70 50 61 72 73 65 2d 3e  tine, ++pParse->
21e5b 6e 4d 65 6d 29 3b 0a 20 20 20 20 61 64 64 72 53  nMem);.    addrS
21e5c 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33 56  elect = sqlite3V
21e5d 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76  dbeCurrentAddr(v
21e5e 29 2b 32 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  )+2;.    sqlite3
21e5f 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21e60 5f 49 6e 74 65 67 65 72 2c 20 61 64 64 72 53 65  _Integer, addrSe
21e61 6c 65 63 74 2d 31 2c 20 64 65 73 74 2e 69 50 61  lect-1, dest.iPa
21e62 72 6d 29 3b 0a 20 20 20 20 6a 31 20 3d 20 73 71  rm);.    j1 = sq
21e63 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21e64 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30  v, OP_Goto, 0, 0
21e65 29 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  );.    VdbeComme
21e66 6e 74 28 28 76 2c 20 22 4a 75 6d 70 20 6f 76 65  nt((v, "Jump ove
21e67 72 20 53 45 4c 45 43 54 20 63 6f 72 6f 75 74 69  r SELECT corouti
21e68 6e 65 22 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 52  ne"));..    /* R
21e69 65 73 6f 6c 76 65 20 74 68 65 20 65 78 70 72 65  esolve the expre
21e6a 73 73 69 6f 6e 73 20 69 6e 20 74 68 65 20 53 45  ssions in the SE
21e6b 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
21e6c 6e 64 20 65 78 65 63 75 74 65 20 69 74 2e 20 2a  nd execute it. *
21e6d 2f 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  /.    rc = sqlit
21e6e 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
21e6f 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74 29   pSelect, &dest)
21e70 3b 0a 20 20 20 20 69 66 28 20 72 63 20 7c 7c 20  ;.    if( rc || 
21e71 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 7c 7c 20  pParse->nErr || 
21e72 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
21e73 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 69   ){.      goto i
21e74 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20  nsert_cleanup;. 
21e75 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
21e76 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
21e77 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20 72 65 67  _Integer, 1, reg
21e78 45 6f 66 29 3b 20 20 20 20 20 20 20 20 20 2f 2a  Eof);         /*
21e79 20 45 4f 46 20 3c 2d 20 31 20 2a 2f 0a 20 20 20   EOF <- 1 */.   
21e7a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21e7b 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
21e7c 64 65 73 74 2e 69 50 61 72 6d 29 3b 20 20 20 2f  dest.iParm);   /
21e7d 2a 20 79 69 65 6c 64 20 58 20 2a 2f 0a 20 20 20  * yield X */.   
21e7e 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21e7f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p2(v, OP_Halt, S
21e80 51 4c 49 54 45 5f 49 4e 54 45 52 4e 41 4c 2c 20  QLITE_INTERNAL, 
21e81 4f 45 5f 41 62 6f 72 74 29 3b 0a 20 20 20 20 56  OE_Abort);.    V
21e82 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
21e83 45 6e 64 20 6f 66 20 53 45 4c 45 43 54 20 63 6f  End of SELECT co
21e84 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20 20 20  routine"));.    
21e85 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
21e86 65 72 65 28 76 2c 20 6a 31 29 3b 20 20 20 20 20  ere(v, j1);     
21e87 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
21e88 20 20 20 20 20 2f 2a 20 6c 61 62 65 6c 20 42 3a       /* label B:
21e89 20 2a 2f 0a 0a 20 20 20 20 72 65 67 46 72 6f 6d   */..    regFrom
21e8a 53 65 6c 65 63 74 20 3d 20 64 65 73 74 2e 69 4d  Select = dest.iM
21e8b 65 6d 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  em;.    assert( 
21e8c 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74 20  pSelect->pEList 
21e8d 29 3b 0a 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d  );.    nColumn =
21e8e 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69 73 74   pSelect->pEList
21e8f 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73  ->nExpr;.    ass
21e90 65 72 74 28 20 64 65 73 74 2e 6e 4d 65 6d 3d 3d  ert( dest.nMem==
21e91 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 0a 20 20 20 20  nColumn );..    
21e92 2f 2a 20 53 65 74 20 75 73 65 54 65 6d 70 54 61  /* Set useTempTa
21e93 62 6c 65 20 74 6f 20 54 52 55 45 20 69 66 20 74  ble to TRUE if t
21e94 68 65 20 72 65 73 75 6c 74 20 6f 66 20 74 68 65  he result of the
21e95 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
21e96 74 0a 20 20 20 20 2a 2a 20 73 68 6f 75 6c 64 20  t.    ** should 
21e97 62 65 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20  be written into 
21e98 61 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c  a temporary tabl
21e99 65 20 28 74 65 6d 70 6c 61 74 65 20 34 29 2e 20  e (template 4). 
21e9a 20 53 65 74 20 74 6f 0a 20 20 20 20 2a 2a 20 46   Set to.    ** F
21e9b 41 4c 53 45 20 69 66 20 65 61 63 68 2a 20 72 6f  ALSE if each* ro
21e9c 77 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  w of the SELECT 
21e9d 63 61 6e 20 62 65 20 77 72 69 74 74 65 6e 20 64  can be written d
21e9e 69 72 65 63 74 6c 79 20 69 6e 74 6f 0a 20 20 20  irectly into.   
21e9f 20 2a 2a 20 74 68 65 20 64 65 73 74 69 6e 61 74   ** the destinat
21ea0 69 6f 6e 20 74 61 62 6c 65 20 28 74 65 6d 70 6c  ion table (templ
21ea1 61 74 65 20 33 29 2e 0a 20 20 20 20 2a 2a 0a 20  ate 3)..    **. 
21ea2 20 20 20 2a 2a 20 41 20 74 65 6d 70 20 74 61 62     ** A temp tab
21ea3 6c 65 20 6d 75 73 74 20 62 65 20 75 73 65 64 20  le must be used 
21ea4 69 66 20 74 68 65 20 74 61 62 6c 65 20 62 65 69  if the table bei
21ea5 6e 67 20 75 70 64 61 74 65 64 20 69 73 20 61 6c  ng updated is al
21ea6 73 6f 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 6f 66  so one.    ** of
21ea7 20 74 68 65 20 74 61 62 6c 65 73 20 62 65 69 6e   the tables bein
21ea8 67 20 72 65 61 64 20 62 79 20 74 68 65 20 53 45  g read by the SE
21ea9 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20  LECT statement. 
21eaa 20 41 6c 73 6f 20 75 73 65 20 61 20 0a 20 20 20   Also use a .   
21eab 20 2a 2a 20 74 65 6d 70 20 74 61 62 6c 65 20 69   ** temp table i
21eac 6e 20 74 68 65 20 63 61 73 65 20 6f 66 20 72 6f  n the case of ro
21ead 77 20 74 72 69 67 67 65 72 73 2e 0a 20 20 20 20  w triggers..    
21eae 2a 2f 0a 20 20 20 20 69 66 28 20 74 72 69 67 67  */.    if( trigg
21eaf 65 72 73 5f 65 78 69 73 74 20 7c 7c 20 72 65 61  ers_exist || rea
21eb0 64 73 54 61 62 6c 65 28 76 2c 20 61 64 64 72 53  dsTable(v, addrS
21eb1 65 6c 65 63 74 2c 20 69 44 62 2c 20 70 54 61 62  elect, iDb, pTab
21eb2 29 20 29 7b 0a 20 20 20 20 20 20 75 73 65 54 65  ) ){.      useTe
21eb3 6d 70 54 61 62 6c 65 20 3d 20 31 3b 0a 20 20 20  mpTable = 1;.   
21eb4 20 7d 0a 0a 20 20 20 20 69 66 28 20 75 73 65 54   }..    if( useT
21eb5 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20  empTable ){.    
21eb6 20 20 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20    /* Invoke the 
21eb7 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 78 74  coroutine to ext
21eb8 72 61 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ract information
21eb9 20 66 72 6f 6d 20 74 68 65 20 53 45 4c 45 43 54   from the SELECT
21eba 0a 20 20 20 20 20 20 2a 2a 20 61 6e 64 20 61 64  .      ** and ad
21ebb 64 20 69 74 20 74 6f 20 61 20 74 72 61 6e 73 69  d it to a transi
21ebc 65 6e 74 20 74 61 62 6c 65 20 73 72 63 54 61 62  ent table srcTab
21ebd 2e 20 20 54 68 65 20 63 6f 64 65 20 67 65 6e 65  .  The code gene
21ebe 72 61 74 65 64 0a 20 20 20 20 20 20 2a 2a 20 68  rated.      ** h
21ebf 65 72 65 20 69 73 20 66 72 6f 6d 20 74 68 65 20  ere is from the 
21ec0 34 74 68 20 74 65 6d 70 6c 61 74 65 3a 0a 20 20  4th template:.  
21ec1 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
21ec2 20 20 20 20 20 42 3a 20 6f 70 65 6e 20 74 65 6d       B: open tem
21ec3 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  p table.      **
21ec4 20 20 20 20 20 20 4c 3a 20 79 69 65 6c 64 20 58        L: yield X
21ec5 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
21ec6 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 4d 0a    if EOF goto M.
21ec7 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
21ec8 20 69 6e 73 65 72 74 20 72 6f 77 20 66 72 6f 6d   insert row from
21ec9 20 52 2e 2e 52 2b 6e 20 69 6e 74 6f 20 74 65 6d   R..R+n into tem
21eca 70 20 74 61 62 6c 65 0a 20 20 20 20 20 20 2a 2a  p table.      **
21ecb 20 20 20 20 20 20 20 20 20 67 6f 74 6f 20 4c 0a           goto L.
21ecc 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 4d 3a        **      M:
21ecd 20 2e 2e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20   ....      */.  
21ece 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 20      int regRec; 
21ecf 20 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72       /* Register
21ed0 20 74 6f 20 68 6f 6c 64 20 70 61 63 6b 65 64 20   to hold packed 
21ed1 72 65 63 6f 72 64 20 2a 2f 0a 20 20 20 20 20 20  record */.      
21ed2 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 20 20 20  int regRowid;   
21ed3 20 2f 2a 20 52 65 67 69 73 74 65 72 20 74 6f 20   /* Register to 
21ed4 68 6f 6c 64 20 74 65 6d 70 20 74 61 62 6c 65 20  hold temp table 
21ed5 52 4f 57 49 44 20 2a 2f 0a 20 20 20 20 20 20 69  ROWID */.      i
21ed6 6e 74 20 61 64 64 72 54 6f 70 3b 20 20 20 20 20  nt addrTop;     
21ed7 2f 2a 20 4c 61 62 65 6c 20 22 4c 22 20 2a 2f 0a  /* Label "L" */.
21ed8 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 49 66        int addrIf
21ed9 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
21eda 73 20 6f 66 20 6a 75 6d 70 20 74 6f 20 4d 20 2a  s of jump to M *
21edb 2f 0a 0a 20 20 20 20 20 20 73 72 63 54 61 62 20  /..      srcTab 
21edc 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
21edd 3b 0a 20 20 20 20 20 20 72 65 67 52 65 63 20 3d  ;.      regRec =
21ede 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
21edf 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
21ee0 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71 6c    regRowid = sql
21ee1 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
21ee2 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 73 71  Parse);.      sq
21ee3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
21ee4 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65  v, OP_OpenEpheme
21ee5 72 61 6c 2c 20 73 72 63 54 61 62 2c 20 6e 43 6f  ral, srcTab, nCo
21ee6 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 61 64 64  lumn);.      add
21ee7 72 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  rTop = sqlite3Vd
21ee8 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 59  beAddOp1(v, OP_Y
21ee9 69 65 6c 64 2c 20 64 65 73 74 2e 69 50 61 72 6d  ield, dest.iParm
21eea 29 3b 0a 20 20 20 20 20 20 61 64 64 72 49 66 20  );.      addrIf 
21eeb 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
21eec 4f 70 31 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65  Op1(v, OP_If, re
21eed 67 45 6f 66 29 3b 0a 20 20 20 20 20 20 73 71 6c  gEof);.      sql
21eee 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
21eef 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
21ef0 20 72 65 67 46 72 6f 6d 53 65 6c 65 63 74 2c 20   regFromSelect, 
21ef1 6e 43 6f 6c 75 6d 6e 2c 20 72 65 67 52 65 63 29  nColumn, regRec)
21ef2 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
21ef3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
21ef4 4e 65 77 52 6f 77 69 64 2c 20 73 72 63 54 61 62  NewRowid, srcTab
21ef5 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
21ef6 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
21ef7 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
21ef8 74 2c 20 73 72 63 54 61 62 2c 20 72 65 67 52 65  t, srcTab, regRe
21ef9 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
21efa 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
21efb 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f  ddOp2(v, OP_Goto
21efc 2c 20 30 2c 20 61 64 64 72 54 6f 70 29 3b 0a 20  , 0, addrTop);. 
21efd 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
21efe 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
21eff 49 66 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  If);.      sqlit
21f00 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
21f01 28 70 50 61 72 73 65 2c 20 72 65 67 52 65 63 29  (pParse, regRec)
21f02 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 52  ;.      sqlite3R
21f03 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50  eleaseTempReg(pP
21f04 61 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b  arse, regRowid);
21f05 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
21f06 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20 74      /* This is t
21f07 68 65 20 63 61 73 65 20 69 66 20 74 68 65 20 64  he case if the d
21f08 61 74 61 20 66 6f 72 20 74 68 65 20 49 4e 53 45  ata for the INSE
21f09 52 54 20 69 73 20 63 6f 6d 69 6e 67 20 66 72 6f  RT is coming fro
21f0a 6d 20 61 20 56 41 4c 55 45 53 0a 20 20 20 20 2a  m a VALUES.    *
21f0b 2a 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2f 0a  * clause.    */.
21f0c 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65 78 74 20      NameContext 
21f0d 73 4e 43 3b 0a 20 20 20 20 6d 65 6d 73 65 74 28  sNC;.    memset(
21f0e 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28  &sNC, 0, sizeof(
21f0f 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43 2e 70  sNC));.    sNC.p
21f10 50 61 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a  Parse = pParse;.
21f11 20 20 20 20 73 72 63 54 61 62 20 3d 20 2d 31 3b      srcTab = -1;
21f12 0a 20 20 20 20 61 73 73 65 72 74 28 20 75 73 65  .    assert( use
21f13 54 65 6d 70 54 61 62 6c 65 3d 3d 30 20 29 3b 0a  TempTable==0 );.
21f14 20 20 20 20 6e 43 6f 6c 75 6d 6e 20 3d 20 70 4c      nColumn = pL
21f15 69 73 74 20 3f 20 70 4c 69 73 74 2d 3e 6e 45 78  ist ? pList->nEx
21f16 70 72 20 3a 20 30 3b 0a 20 20 20 20 66 6f 72 28  pr : 0;.    for(
21f17 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e 3b 20  i=0; i<nColumn; 
21f18 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
21f19 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
21f1a 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 4c  prNames(&sNC, pL
21f1b 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29  ist->a[i].pExpr)
21f1c 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
21f1d 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
21f1e 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
21f1f 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 73 75   }..  /* Make su
21f20 72 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  re the number of
21f21 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65 20   columns in the 
21f22 73 6f 75 72 63 65 20 64 61 74 61 20 6d 61 74 63  source data matc
21f23 68 65 73 20 74 68 65 20 6e 75 6d 62 65 72 0a 20  hes the number. 
21f24 20 2a 2a 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 74   ** of columns t
21f25 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 69 6e  o be inserted in
21f26 74 6f 20 74 68 65 20 74 61 62 6c 65 2e 0a 20 20  to the table..  
21f27 2a 2f 0a 20 20 69 66 28 20 49 73 56 69 72 74 75  */.  if( IsVirtu
21f28 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20  al(pTab) ){.    
21f29 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d  for(i=0; i<pTab-
21f2a 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  >nCol; i++){.   
21f2b 20 20 20 6e 48 69 64 64 65 6e 20 2b 3d 20 28 49     nHidden += (I
21f2c 73 48 69 64 64 65 6e 43 6f 6c 75 6d 6e 28 26 70  sHiddenColumn(&p
21f2d 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 29 20 3f 20  Tab->aCol[i]) ? 
21f2e 31 20 3a 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20  1 : 0);.    }.  
21f2f 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d  }.  if( pColumn=
21f30 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 20 26 26  =0 && nColumn &&
21f31 20 6e 43 6f 6c 75 6d 6e 21 3d 28 70 54 61 62 2d   nColumn!=(pTab-
21f32 3e 6e 43 6f 6c 2d 6e 48 69 64 64 65 6e 29 20 29  >nCol-nHidden) )
21f33 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  {.    sqlite3Err
21f34 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a 20  orMsg(pParse, . 
21f35 20 20 20 20 20 20 22 74 61 62 6c 65 20 25 53 20        "table %S 
21f36 68 61 73 20 25 64 20 63 6f 6c 75 6d 6e 73 20 62  has %d columns b
21f37 75 74 20 25 64 20 76 61 6c 75 65 73 20 77 65 72  ut %d values wer
21f38 65 20 73 75 70 70 6c 69 65 64 22 2c 0a 20 20 20  e supplied",.   
21f39 20 20 20 20 70 54 61 62 4c 69 73 74 2c 20 30 2c      pTabList, 0,
21f3a 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 6e 43 6f   pTab->nCol, nCo
21f3b 6c 75 6d 6e 29 3b 0a 20 20 20 20 67 6f 74 6f 20  lumn);.    goto 
21f3c 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a  insert_cleanup;.
21f3d 20 20 7d 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d    }.  if( pColum
21f3e 6e 21 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 21  n!=0 && nColumn!
21f3f 3d 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b  =pColumn->nId ){
21f40 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
21f41 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 25 64  rMsg(pParse, "%d
21f42 20 76 61 6c 75 65 73 20 66 6f 72 20 25 64 20 63   values for %d c
21f43 6f 6c 75 6d 6e 73 22 2c 20 6e 43 6f 6c 75 6d 6e  olumns", nColumn
21f44 2c 20 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 29 3b  , pColumn->nId);
21f45 0a 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74  .    goto insert
21f46 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a 20  _cleanup;.  }.. 
21f47 20 2f 2a 20 49 66 20 74 68 65 20 49 4e 53 45 52   /* If the INSER
21f48 54 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 63 6c  T statement incl
21f49 75 64 65 64 20 61 6e 20 49 44 4c 49 53 54 20 74  uded an IDLIST t
21f4a 65 72 6d 2c 20 74 68 65 6e 20 6d 61 6b 65 20 73  erm, then make s
21f4b 75 72 65 0a 20 20 2a 2a 20 61 6c 6c 20 65 6c 65  ure.  ** all ele
21f4c 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 49 44 4c  ments of the IDL
21f4d 49 53 54 20 72 65 61 6c 6c 79 20 61 72 65 20 63  IST really are c
21f4e 6f 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 74 61  olumns of the ta
21f4f 62 6c 65 20 61 6e 64 20 0a 20 20 2a 2a 20 72 65  ble and .  ** re
21f50 6d 65 6d 62 65 72 20 74 68 65 20 63 6f 6c 75 6d  member the colum
21f51 6e 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 0a  n indices..  **.
21f52 20 20 2a 2a 20 49 66 20 74 68 65 20 74 61 62 6c    ** If the tabl
21f53 65 20 68 61 73 20 61 6e 20 49 4e 54 45 47 45 52  e has an INTEGER
21f54 20 50 52 49 4d 41 52 59 20 4b 45 59 20 63 6f 6c   PRIMARY KEY col
21f55 75 6d 6e 20 61 6e 64 20 74 68 61 74 20 63 6f 6c  umn and that col
21f56 75 6d 6e 0a 20 20 2a 2a 20 69 73 20 6e 61 6d 65  umn.  ** is name
21f57 64 20 69 6e 20 74 68 65 20 49 44 4c 49 53 54 2c  d in the IDLIST,
21f58 20 74 68 65 6e 20 72 65 63 6f 72 64 20 69 6e 20   then record in 
21f59 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76 61  the keyColumn va
21f5a 72 69 61 62 6c 65 0a 20 20 2a 2a 20 74 68 65 20  riable.  ** the 
21f5b 69 6e 64 65 78 20 69 6e 74 6f 20 49 44 4c 49 53  index into IDLIS
21f5c 54 20 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79  T of the primary
21f5d 20 6b 65 79 20 63 6f 6c 75 6d 6e 2e 20 20 6b 65   key column.  ke
21f5e 79 43 6f 6c 75 6d 6e 20 69 73 0a 20 20 2a 2a 20  yColumn is.  ** 
21f5f 74 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68 65  the index of the
21f60 20 70 72 69 6d 61 72 79 20 6b 65 79 20 61 73 20   primary key as 
21f61 69 74 20 61 70 70 65 61 72 73 20 69 6e 20 49 44  it appears in ID
21f62 4c 49 53 54 2c 20 6e 6f 74 20 61 73 0a 20 20 2a  LIST, not as.  *
21f63 2a 20 69 73 20 61 70 70 65 61 72 73 20 69 6e 20  * is appears in 
21f64 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
21f65 6c 65 2e 20 20 28 54 68 65 20 69 6e 64 65 78 20  le.  (The index 
21f66 6f 66 20 74 68 65 20 70 72 69 6d 61 72 79 0a 20  of the primary. 
21f67 20 2a 2a 20 6b 65 79 20 69 6e 20 74 68 65 20 6f   ** key in the o
21f68 72 69 67 69 6e 61 6c 20 74 61 62 6c 65 20 69 73  riginal table is
21f69 20 70 54 61 62 2d 3e 69 50 4b 65 79 2e 29 0a 20   pTab->iPKey.). 
21f6a 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
21f6b 6e 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30  n ){.    for(i=0
21f6c 3b 20 69 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49 64  ; i<pColumn->nId
21f6d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 70 43  ; i++){.      pC
21f6e 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 69 64 78 20  olumn->a[i].idx 
21f6f 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20  = -1;.    }.    
21f70 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 6f 6c 75  for(i=0; i<pColu
21f71 6d 6e 2d 3e 6e 49 64 3b 20 69 2b 2b 29 7b 0a 20  mn->nId; i++){. 
21f72 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
21f73 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  pTab->nCol; j++)
21f74 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  {.        if( sq
21f75 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 43 6f  lite3StrICmp(pCo
21f76 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65  lumn->a[i].zName
21f77 2c 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e  , pTab->aCol[j].
21f78 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
21f79 20 20 20 20 20 20 20 70 43 6f 6c 75 6d 6e 2d 3e         pColumn->
21f7a 61 5b 69 5d 2e 69 64 78 20 3d 20 6a 3b 0a 20 20  a[i].idx = j;.  
21f7b 20 20 20 20 20 20 20 20 69 66 28 20 6a 3d 3d 70          if( j==p
21f7c 54 61 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20  Tab->iPKey ){.  
21f7d 20 20 20 20 20 20 20 20 20 20 6b 65 79 43 6f 6c            keyCol
21f7e 75 6d 6e 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  umn = i;.       
21f7f 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62     }.          b
21f80 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
21f81 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
21f82 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20  ( j>=pTab->nCol 
21f83 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  ){.        if( s
21f84 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28 70 43  qlite3IsRowid(pC
21f85 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
21f86 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  e) ){.          
21f87 6b 65 79 43 6f 6c 75 6d 6e 20 3d 20 69 3b 0a 20  keyColumn = i;. 
21f88 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
21f89 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
21f8a 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
21f8b 22 74 61 62 6c 65 20 25 53 20 68 61 73 20 6e 6f  "table %S has no
21f8c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 64 20 25 73   column named %s
21f8d 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ",.             
21f8e 20 70 54 61 62 4c 69 73 74 2c 20 30 2c 20 70 43   pTabList, 0, pC
21f8f 6f 6c 75 6d 6e 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d  olumn->a[i].zNam
21f90 65 29 3b 0a 20 20 20 20 20 20 20 20 20 20 70 50  e);.          pP
21f91 61 72 73 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20  arse->nErr++;.  
21f92 20 20 20 20 20 20 20 20 67 6f 74 6f 20 69 6e 73          goto ins
21f93 65 72 74 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ert_cleanup;.   
21f94 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
21f95 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49     }.  }..  /* I
21f96 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 49 44  f there is no ID
21f97 4c 49 53 54 20 74 65 72 6d 20 62 75 74 20 74 68  LIST term but th
21f98 65 20 74 61 62 6c 65 20 68 61 73 20 61 6e 20 69  e table has an i
21f99 6e 74 65 67 65 72 20 70 72 69 6d 61 72 79 0a 20  nteger primary. 
21f9a 20 2a 2a 20 6b 65 79 2c 20 74 68 65 20 73 65 74   ** key, the set
21f9b 20 74 68 65 20 6b 65 79 43 6f 6c 75 6d 6e 20 76   the keyColumn v
21f9c 61 72 69 61 62 6c 65 20 74 6f 20 74 68 65 20 70  ariable to the p
21f9d 72 69 6d 61 72 79 20 6b 65 79 20 63 6f 6c 75 6d  rimary key colum
21f9e 6e 20 69 6e 64 65 78 0a 20 20 2a 2a 20 69 6e 20  n index.  ** in 
21f9f 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 74 61 62  the original tab
21fa0 6c 65 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a 20  le definition.. 
21fa1 20 2a 2f 0a 20 20 69 66 28 20 70 43 6f 6c 75 6d   */.  if( pColum
21fa2 6e 3d 3d 30 20 26 26 20 6e 43 6f 6c 75 6d 6e 3e  n==0 && nColumn>
21fa3 30 20 29 7b 0a 20 20 20 20 6b 65 79 43 6f 6c 75  0 ){.    keyColu
21fa4 6d 6e 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79  mn = pTab->iPKey
21fa5 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e  ;.  }..  /* Open
21fa6 20 74 68 65 20 74 65 6d 70 20 74 61 62 6c 65 20   the temp table 
21fa7 66 6f 72 20 46 4f 52 20 45 41 43 48 20 52 4f 57  for FOR EACH ROW
21fa8 20 74 72 69 67 67 65 72 73 0a 20 20 2a 2f 0a 20   triggers.  */. 
21fa9 20 69 66 28 20 74 72 69 67 67 65 72 73 5f 65 78   if( triggers_ex
21faa 69 73 74 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ist ){.    sqlit
21fab 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
21fac 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d 6e 73  OP_SetNumColumns
21fad 2c 20 30 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  , 0, pTab->nCol)
21fae 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
21faf 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
21fb0 65 6e 50 73 65 75 64 6f 2c 20 6e 65 77 49 64 78  enPseudo, newIdx
21fb1 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 20 20 0a 20  , 0);.  }.    . 
21fb2 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20 74   /* Initialize t
21fb3 68 65 20 63 6f 75 6e 74 20 6f 66 20 72 6f 77 73  he count of rows
21fb4 20 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 0a   to be inserted.
21fb5 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66    */.  if( db->f
21fb6 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f  lags & SQLITE_Co
21fb7 75 6e 74 52 6f 77 73 20 29 7b 0a 20 20 20 20 72  untRows ){.    r
21fb8 65 67 52 6f 77 43 6f 75 6e 74 20 3d 20 2b 2b 70  egRowCount = ++p
21fb9 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20  Parse->nMem;.   
21fba 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
21fbb 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
21fbc 2c 20 30 2c 20 72 65 67 52 6f 77 43 6f 75 6e 74  , 0, regRowCount
21fbd 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20  );.  }..  /* If 
21fbe 74 68 69 73 20 69 73 20 6e 6f 74 20 61 20 76 69  this is not a vi
21fbf 65 77 2c 20 6f 70 65 6e 20 74 68 65 20 74 61 62  ew, open the tab
21fc0 6c 65 20 61 6e 64 20 61 6e 64 20 61 6c 6c 20 69  le and and all i
21fc1 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 66 28 20  ndices */.  if( 
21fc2 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 69  !isView ){.    i
21fc3 6e 74 20 6e 49 64 78 3b 0a 20 20 20 20 69 6e 74  nt nIdx;.    int
21fc4 20 69 3b 0a 0a 20 20 20 20 62 61 73 65 43 75 72   i;..    baseCur
21fc5 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 3b   = pParse->nTab;
21fc6 0a 20 20 20 20 6e 49 64 78 20 3d 20 73 71 6c 69  .    nIdx = sqli
21fc7 74 65 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49  te3OpenTableAndI
21fc8 6e 64 69 63 65 73 28 70 50 61 72 73 65 2c 20 70  ndices(pParse, p
21fc9 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 4f 50  Tab, baseCur, OP
21fca 5f 4f 70 65 6e 57 72 69 74 65 29 3b 0a 20 20 20  _OpenWrite);.   
21fcb 20 61 52 65 67 49 64 78 20 3d 20 73 71 6c 69 74   aRegIdx = sqlit
21fcc 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
21fcd 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 28 6e  , sizeof(int)*(n
21fce 49 64 78 2b 31 29 29 3b 0a 20 20 20 20 69 66 28  Idx+1));.    if(
21fcf 20 61 52 65 67 49 64 78 3d 3d 30 20 29 7b 0a 20   aRegIdx==0 ){. 
21fd0 20 20 20 20 20 67 6f 74 6f 20 69 6e 73 65 72 74       goto insert
21fd1 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
21fd2 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
21fd3 49 64 78 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Idx; i++){.     
21fd4 20 61 52 65 67 49 64 78 5b 69 5d 20 3d 20 2b 2b   aRegIdx[i] = ++
21fd5 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
21fd6 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 68    }.  }..  /* Th
21fd7 69 73 20 69 73 20 74 68 65 20 74 6f 70 20 6f 66  is is the top of
21fd8 20 74 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74   the main insert
21fd9 69 6f 6e 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66  ion loop */.  if
21fda 28 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29  ( useTempTable )
21fdb 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62 6c  {.    /* This bl
21fdc 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74 6f  ock codes the to
21fdd 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e 20  p of loop only. 
21fde 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c 6f   The complete lo
21fdf 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20 2a 2a  op is the.    **
21fe0 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75 64   following pseud
21fe1 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65 20  ocode (template 
21fe2 34 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  4):.    **.    *
21fe3 2a 20 20 20 20 20 20 20 20 20 72 65 77 69 6e 64  *         rewind
21fe4 20 74 65 6d 70 20 74 61 62 6c 65 0a 20 20 20 20   temp table.    
21fe5 2a 2a 20 20 20 20 20 20 43 3a 20 6c 6f 6f 70 20  **      C: loop 
21fe6 6f 76 65 72 20 72 6f 77 73 20 6f 66 20 69 6e 74  over rows of int
21fe7 65 72 6d 65 64 69 61 74 65 20 74 61 62 6c 65 0a  ermediate table.
21fe8 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20      **          
21fe9 20 74 72 61 6e 73 66 65 72 20 76 61 6c 75 65 73   transfer values
21fea 20 66 6f 72 6d 20 69 6e 74 65 72 6d 65 64 69 61   form intermedia
21feb 74 65 20 74 61 62 6c 65 20 69 6e 74 6f 20 3c 74  te table into <t
21fec 61 62 6c 65 3e 0a 20 20 20 20 2a 2a 20 20 20 20  able>.    **    
21fed 20 20 20 20 20 65 6e 64 20 6c 6f 6f 70 0a 20 20       end loop.  
21fee 20 20 2a 2a 20 20 20 20 20 20 44 3a 20 2e 2e 2e    **      D: ...
21fef 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 64 64 72  .    */.    addr
21ff0 49 6e 73 54 6f 70 20 3d 20 73 71 6c 69 74 65 33  InsTop = sqlite3
21ff1 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
21ff2 5f 52 65 77 69 6e 64 2c 20 73 72 63 54 61 62 29  _Rewind, srcTab)
21ff3 3b 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20 3d  ;.    addrCont =
21ff4 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
21ff5 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 7d 65  entAddr(v);.  }e
21ff6 6c 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20  lse if( pSelect 
21ff7 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 62  ){.    /* This b
21ff8 6c 6f 63 6b 20 63 6f 64 65 73 20 74 68 65 20 74  lock codes the t
21ff9 6f 70 20 6f 66 20 6c 6f 6f 70 20 6f 6e 6c 79 2e  op of loop only.
21ffa 20 20 54 68 65 20 63 6f 6d 70 6c 65 74 65 20 6c    The complete l
21ffb 6f 6f 70 20 69 73 20 74 68 65 0a 20 20 20 20 2a  oop is the.    *
21ffc 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 73 65 75  * following pseu
21ffd 64 6f 63 6f 64 65 20 28 74 65 6d 70 6c 61 74 65  docode (template
21ffe 20 33 29 3a 0a 20 20 20 20 2a 2a 0a 20 20 20 20   3):.    **.    
21fff 2a 2a 20 20 20 20 20 20 43 3a 20 79 69 65 6c 64  **      C: yield
22000 20 58 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20   X.    **       
22001 20 20 69 66 20 45 4f 46 20 67 6f 74 6f 20 44 0a    if EOF goto D.
22002 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 69      **         i
22003 6e 73 65 72 74 20 74 68 65 20 73 65 6c 65 63 74  nsert the select
22004 20 72 65 73 75 6c 74 20 69 6e 74 6f 20 3c 74 61   result into <ta
22005 62 6c 65 3e 20 66 72 6f 6d 20 52 2e 2e 52 2b 6e  ble> from R..R+n
22006 0a 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20  .    **         
22007 67 6f 74 6f 20 43 0a 20 20 20 20 2a 2a 20 20 20  goto C.    **   
22008 20 20 20 44 3a 20 2e 2e 2e 0a 20 20 20 20 2a 2f     D: ....    */
22009 0a 20 20 20 20 61 64 64 72 43 6f 6e 74 20 3d 20  .    addrCont = 
2200a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2200b 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 64  1(v, OP_Yield, d
2200c 65 73 74 2e 69 50 61 72 6d 29 3b 0a 20 20 20 20  est.iParm);.    
2200d 61 64 64 72 49 6e 73 54 6f 70 20 3d 20 73 71 6c  addrInsTop = sql
2200e 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2200f 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 29  , OP_If, regEof)
22010 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  ;.  }..  /* Allo
22011 63 61 74 65 20 72 65 67 69 73 74 65 72 73 20 66  cate registers f
22012 6f 72 20 68 6f 6c 64 69 6e 67 20 74 68 65 20 72  or holding the r
22013 6f 77 69 64 20 6f 66 20 74 68 65 20 6e 65 77 20  owid of the new 
22014 72 6f 77 2c 0a 20 20 2a 2a 20 74 68 65 20 63 6f  row,.  ** the co
22015 6e 74 65 6e 74 20 6f 66 20 74 68 65 20 6e 65 77  ntent of the new
22016 20 72 6f 77 2c 20 61 6e 64 20 74 68 65 20 61 73   row, and the as
22017 73 65 6d 62 6c 69 65 64 20 72 6f 77 20 72 65 63  semblied row rec
22018 6f 72 64 2e 0a 20 20 2a 2f 0a 20 20 72 65 67 52  ord..  */.  regR
22019 65 63 6f 72 64 20 3d 20 2b 2b 70 50 61 72 73 65  ecord = ++pParse
2201a 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 52 6f 77  ->nMem;.  regRow
2201b 69 64 20 3d 20 72 65 67 49 6e 73 20 3d 20 70 50  id = regIns = pP
2201c 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31 3b 0a 20 20  arse->nMem+1;.  
2201d 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20  pParse->nMem += 
2201e 70 54 61 62 2d 3e 6e 43 6f 6c 20 2b 20 31 3b 0a  pTab->nCol + 1;.
2201f 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
22020 70 54 61 62 29 20 29 7b 0a 20 20 20 20 72 65 67  pTab) ){.    reg
22021 52 6f 77 69 64 2b 2b 3b 0a 20 20 20 20 70 50 61  Rowid++;.    pPa
22022 72 73 65 2d 3e 6e 4d 65 6d 2b 2b 3b 0a 20 20 7d  rse->nMem++;.  }
22023 0a 20 20 72 65 67 44 61 74 61 20 3d 20 72 65 67  .  regData = reg
22024 52 6f 77 69 64 2b 31 3b 0a 0a 20 20 2f 2a 20 52  Rowid+1;..  /* R
22025 75 6e 20 74 68 65 20 42 45 46 4f 52 45 20 61 6e  un the BEFORE an
22026 64 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69  d INSTEAD OF tri
22027 67 67 65 72 73 2c 20 69 66 20 74 68 65 72 65 20  ggers, if there 
22028 61 72 65 20 61 6e 79 0a 20 20 2a 2f 0a 20 20 65  are any.  */.  e
22029 6e 64 4f 66 4c 6f 6f 70 20 3d 20 73 71 6c 69 74  ndOfLoop = sqlit
2202a 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2202b 76 29 3b 0a 20 20 69 66 28 20 74 72 69 67 67 65  v);.  if( trigge
2202c 72 73 5f 65 78 69 73 74 20 26 20 54 52 49 47 47  rs_exist & TRIGG
2202d 45 52 5f 42 45 46 4f 52 45 20 29 7b 0a 20 20 20  ER_BEFORE ){.   
2202e 20 69 6e 74 20 72 65 67 52 6f 77 69 64 3b 0a 20   int regRowid;. 
2202f 20 20 20 69 6e 74 20 72 65 67 43 6f 6c 73 3b 0a     int regCols;.
22030 20 20 20 20 69 6e 74 20 72 65 67 52 65 63 3b 0a      int regRec;.
22031 0a 20 20 20 20 2f 2a 20 62 75 69 6c 64 20 74 68  .    /* build th
22032 65 20 4e 45 57 2e 2a 20 72 65 66 65 72 65 6e 63  e NEW.* referenc
22033 65 20 72 6f 77 2e 20 20 4e 6f 74 65 20 74 68 61  e row.  Note tha
22034 74 20 69 66 20 74 68 65 72 65 20 69 73 20 61 6e  t if there is an
22035 20 49 4e 54 45 47 45 52 0a 20 20 20 20 2a 2a 20   INTEGER.    ** 
22036 50 52 49 4d 41 52 59 20 4b 45 59 20 69 6e 74 6f  PRIMARY KEY into
22037 20 77 68 69 63 68 20 61 20 4e 55 4c 4c 20 69 73   which a NULL is
22038 20 62 65 69 6e 67 20 69 6e 73 65 72 74 65 64 2c   being inserted,
22039 20 74 68 61 74 20 4e 55 4c 4c 20 77 69 6c 6c 20   that NULL will 
2203a 62 65 0a 20 20 20 20 2a 2a 20 74 72 61 6e 73 6c  be.    ** transl
2203b 61 74 65 64 20 69 6e 74 6f 20 61 20 75 6e 69 71  ated into a uniq
2203c 75 65 20 49 44 20 66 6f 72 20 74 68 65 20 72 6f  ue ID for the ro
2203d 77 2e 20 20 42 75 74 20 6f 6e 20 61 20 42 45 46  w.  But on a BEF
2203e 4f 52 45 20 74 72 69 67 67 65 72 2c 0a 20 20 20  ORE trigger,.   
2203f 20 2a 2a 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e   ** we do not kn
22040 6f 77 20 77 68 61 74 20 74 68 65 20 75 6e 69 71  ow what the uniq
22041 75 65 20 49 44 20 77 69 6c 6c 20 62 65 20 28 62  ue ID will be (b
22042 65 63 61 75 73 65 20 74 68 65 20 69 6e 73 65 72  ecause the inser
22043 74 20 68 61 73 0a 20 20 20 20 2a 2a 20 6e 6f 74  t has.    ** not
22044 20 68 61 70 70 65 6e 65 64 20 79 65 74 29 20 73   happened yet) s
22045 6f 20 77 65 20 73 75 62 73 74 69 74 75 74 65 20  o we substitute 
22046 61 20 72 6f 77 69 64 20 6f 66 20 2d 31 0a 20 20  a rowid of -1.  
22047 20 20 2a 2f 0a 20 20 20 20 72 65 67 52 6f 77 69    */.    regRowi
22048 64 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  d = sqlite3GetTe
22049 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20  mpReg(pParse);. 
2204a 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e     if( keyColumn
2204b 3c 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  <0 ){.      sqli
2204c 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2204d 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c   OP_Integer, -1,
2204e 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
2204f 7d 65 6c 73 65 20 69 66 28 20 75 73 65 54 65 6d  }else if( useTem
22050 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 20 20  pTable ){.      
22051 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22052 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20  3(v, OP_Column, 
22053 73 72 63 54 61 62 2c 20 6b 65 79 43 6f 6c 75 6d  srcTab, keyColum
22054 6e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  n, regRowid);.  
22055 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69    }else{.      i
22056 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20 61 73 73  nt j1;.      ass
22057 65 72 74 28 20 70 53 65 6c 65 63 74 3d 3d 30 20  ert( pSelect==0 
22058 29 3b 20 20 2f 2a 20 4f 74 68 65 72 77 69 73 65  );  /* Otherwise
22059 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 69 73   useTempTable is
2205a 20 74 72 75 65 20 2a 2f 0a 20 20 20 20 20 20 73   true */.      s
2205b 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2205c 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61 5b  Parse, pList->a[
2205d 6b 65 79 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72  keyColumn].pExpr
2205e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
2205f 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56     j1 = sqlite3V
22060 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
22061 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77 69  NotNull, regRowi
22062 64 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  d);.      sqlite
22063 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22064 50 5f 49 6e 74 65 67 65 72 2c 20 2d 31 2c 20 72  P_Integer, -1, r
22065 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20  egRowid);.      
22066 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48  sqlite3VdbeJumpH
22067 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20 20 20 20  ere(v, j1);.    
22068 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
22069 4f 70 31 28 76 2c 20 4f 50 5f 4d 75 73 74 42 65  Op1(v, OP_MustBe
2206a 49 6e 74 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Int, regRowid);.
2206b 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 61      }..    /* Ca
2206c 6e 6e 6f 74 20 68 61 76 65 20 74 72 69 67 67 65  nnot have trigge
2206d 72 73 20 6f 6e 20 61 20 76 69 72 74 75 61 6c 20  rs on a virtual 
2206e 74 61 62 6c 65 2e 20 49 66 20 69 74 20 77 65 72  table. If it wer
2206f 65 20 70 6f 73 73 69 62 6c 65 2c 0a 20 20 20 20  e possible,.    
22070 2a 2a 20 74 68 69 73 20 62 6c 6f 63 6b 20 77 6f  ** this block wo
22071 75 6c 64 20 68 61 76 65 20 74 6f 20 61 63 63 6f  uld have to acco
22072 75 6e 74 20 66 6f 72 20 68 69 64 64 65 6e 20 63  unt for hidden c
22073 6f 6c 75 6d 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  olumn..    */.  
22074 20 20 61 73 73 65 72 74 28 21 49 73 56 69 72 74    assert(!IsVirt
22075 75 61 6c 28 70 54 61 62 29 29 3b 0a 0a 20 20 20  ual(pTab));..   
22076 20 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 6e   /* Create the n
22077 65 77 20 63 6f 6c 75 6d 6e 20 64 61 74 61 0a 20  ew column data. 
22078 20 20 20 2a 2f 0a 20 20 20 20 72 65 67 43 6f 6c     */.    regCol
22079 73 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  s = sqlite3GetTe
2207a 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
2207b 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20  pTab->nCol);.   
2207c 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
2207d 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
2207e 20 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 3d      if( pColumn=
2207f 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 20  =0 ){.        j 
22080 3d 20 69 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  = i;.      }else
22081 7b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d  {.        for(j=
22082 30 3b 20 6a 3c 70 43 6f 6c 75 6d 6e 2d 3e 6e 49  0; j<pColumn->nI
22083 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  d; j++){.       
22084 20 20 20 69 66 28 20 70 43 6f 6c 75 6d 6e 2d 3e     if( pColumn->
22085 61 5b 6a 5d 2e 69 64 78 3d 3d 69 20 29 20 62 72  a[j].idx==i ) br
22086 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
22087 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
22088 20 70 43 6f 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70   pColumn && j>=p
22089 43 6f 6c 75 6d 6e 2d 3e 6e 49 64 20 29 7b 0a 20  Column->nId ){. 
2208a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
2208b 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
2208c 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66  Tab->aCol[i].pDf
2208d 6c 74 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a  lt, regCols+i);.
2208e 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2208f 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a  useTempTable ){.
22090 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
22091 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
22092 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20  Column, srcTab, 
22093 6a 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 20 0a  j, regCols+i); .
22094 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
22095 20 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65       assert( pSe
22096 6c 65 63 74 3d 3d 30 20 29 3b 20 2f 2a 20 4f 74  lect==0 ); /* Ot
22097 68 65 72 77 69 73 65 20 75 73 65 54 65 6d 70 54  herwise useTempT
22098 61 62 6c 65 20 69 73 20 74 72 75 65 20 2a 2f 0a  able is true */.
22099 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
2209a 78 70 72 43 6f 64 65 41 6e 64 43 61 63 68 65 28  xprCodeAndCache(
2209b 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e 61  pParse, pList->a
2209c 5b 6a 5d 2e 70 45 78 70 72 2c 20 72 65 67 43 6f  [j].pExpr, regCo
2209d 6c 73 2b 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20  ls+i);.      }. 
2209e 20 20 20 7d 0a 20 20 20 20 72 65 67 52 65 63 20     }.    regRec 
2209f 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
220a0 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
220a1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
220a2 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
220a3 6f 72 64 2c 20 72 65 67 43 6f 6c 73 2c 20 70 54  ord, regCols, pT
220a4 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 65 63  ab->nCol, regRec
220a5 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68  );..    /* If th
220a6 69 73 20 69 73 20 61 6e 20 49 4e 53 45 52 54 20  is is an INSERT 
220a7 6f 6e 20 61 20 76 69 65 77 20 77 69 74 68 20 61  on a view with a
220a8 6e 20 49 4e 53 54 45 41 44 20 4f 46 20 49 4e 53  n INSTEAD OF INS
220a9 45 52 54 20 74 72 69 67 67 65 72 2c 0a 20 20 20  ERT trigger,.   
220aa 20 2a 2a 20 64 6f 20 6e 6f 74 20 61 74 74 65 6d   ** do not attem
220ab 70 74 20 61 6e 79 20 63 6f 6e 76 65 72 73 69 6f  pt any conversio
220ac 6e 73 20 62 65 66 6f 72 65 20 61 73 73 65 6d 62  ns before assemb
220ad 6c 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64 2e  ling the record.
220ae 0a 20 20 20 20 2a 2a 20 49 66 20 74 68 69 73 20  .    ** If this 
220af 69 73 20 61 20 72 65 61 6c 20 74 61 62 6c 65 2c  is a real table,
220b0 20 61 74 74 65 6d 70 74 20 63 6f 6e 76 65 72 73   attempt convers
220b1 69 6f 6e 73 20 61 73 20 72 65 71 75 69 72 65 64  ions as required
220b2 20 62 79 20 74 68 65 0a 20 20 20 20 2a 2a 20 74   by the.    ** t
220b3 61 62 6c 65 20 63 6f 6c 75 6d 6e 20 61 66 66 69  able column affi
220b4 6e 69 74 69 65 73 2e 0a 20 20 20 20 2a 2f 0a 20  nities..    */. 
220b5 20 20 20 69 66 28 20 21 69 73 56 69 65 77 20 29     if( !isView )
220b6 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  {.      sqlite3T
220b7 61 62 6c 65 41 66 66 69 6e 69 74 79 53 74 72 28  ableAffinityStr(
220b8 76 2c 20 70 54 61 62 29 3b 0a 20 20 20 20 7d 0a  v, pTab);.    }.
220b9 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
220ba 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
220bb 72 74 2c 20 6e 65 77 49 64 78 2c 20 72 65 67 52  rt, newIdx, regR
220bc 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20  ec, regRowid);. 
220bd 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
220be 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
220bf 20 72 65 67 52 65 63 29 3b 0a 20 20 20 20 73 71   regRec);.    sq
220c0 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
220c1 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52  Reg(pParse, regR
220c2 6f 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74  owid);.    sqlit
220c3 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
220c4 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43 6f  ge(pParse, regCo
220c5 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29 3b  ls, pTab->nCol);
220c6 0a 0a 20 20 20 20 2f 2a 20 46 69 72 65 20 42 45  ..    /* Fire BE
220c7 46 4f 52 45 20 6f 72 20 49 4e 53 54 45 41 44 20  FORE or INSTEAD 
220c8 4f 46 20 74 72 69 67 67 65 72 73 20 2a 2f 0a 20  OF triggers */. 
220c9 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 43 6f     if( sqlite3Co
220ca 64 65 52 6f 77 54 72 69 67 67 65 72 28 70 50 61  deRowTrigger(pPa
220cb 72 73 65 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 20  rse, TK_INSERT, 
220cc 30 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f 52  0, TRIGGER_BEFOR
220cd 45 2c 20 70 54 61 62 2c 20 0a 20 20 20 20 20 20  E, pTab, .      
220ce 20 20 6e 65 77 49 64 78 2c 20 2d 31 2c 20 6f 6e    newIdx, -1, on
220cf 45 72 72 6f 72 2c 20 65 6e 64 4f 66 4c 6f 6f 70  Error, endOfLoop
220d0 2c 20 30 2c 20 30 29 20 29 7b 0a 20 20 20 20 20  , 0, 0) ){.     
220d1 20 67 6f 74 6f 20 69 6e 73 65 72 74 5f 63 6c 65   goto insert_cle
220d2 61 6e 75 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  anup;.    }.  }.
220d3 0a 20 20 2f 2a 20 50 75 73 68 20 74 68 65 20 72  .  /* Push the r
220d4 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 66 6f 72  ecord number for
220d5 20 74 68 65 20 6e 65 77 20 65 6e 74 72 79 20 6f   the new entry o
220d6 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2e 20 20  nto the stack.  
220d7 54 68 65 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20  The.  ** record 
220d8 6e 75 6d 62 65 72 20 69 73 20 61 20 72 61 6e 64  number is a rand
220d9 6f 6d 6c 79 20 67 65 6e 65 72 61 74 65 20 69 6e  omly generate in
220da 74 65 67 65 72 20 63 72 65 61 74 65 64 20 62 79  teger created by
220db 20 4e 65 77 52 6f 77 69 64 0a 20 20 2a 2a 20 65   NewRowid.  ** e
220dc 78 63 65 70 74 20 77 68 65 6e 20 74 68 65 20 74  xcept when the t
220dd 61 62 6c 65 20 68 61 73 20 61 6e 20 49 4e 54 45  able has an INTE
220de 47 45 52 20 50 52 49 4d 41 52 59 20 4b 45 59 20  GER PRIMARY KEY 
220df 63 6f 6c 75 6d 6e 2c 20 69 6e 20 77 68 69 63 68  column, in which
220e0 0a 20 20 2a 2a 20 63 61 73 65 20 74 68 65 20 72  .  ** case the r
220e1 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
220e2 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 61 74  the same as that
220e3 20 63 6f 6c 75 6d 6e 2e 20 0a 20 20 2a 2f 0a 20   column. .  */. 
220e4 20 69 66 28 20 21 69 73 56 69 65 77 20 29 7b 0a   if( !isView ){.
220e5 20 20 20 20 69 66 28 20 49 73 56 69 72 74 75 61      if( IsVirtua
220e6 6c 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20  l(pTab) ){.     
220e7 20 2f 2a 20 54 68 65 20 72 6f 77 20 74 68 61 74   /* The row that
220e8 20 74 68 65 20 56 55 70 64 61 74 65 20 6f 70 63   the VUpdate opc
220e9 6f 64 65 20 77 69 6c 6c 20 64 65 6c 65 74 65 3a  ode will delete:
220ea 20 6e 6f 6e 65 20 2a 2f 0a 20 20 20 20 20 20 73   none */.      s
220eb 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
220ec 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
220ed 72 65 67 49 6e 73 29 3b 0a 20 20 20 20 7d 0a 20  regIns);.    }. 
220ee 20 20 20 69 66 28 20 6b 65 79 43 6f 6c 75 6d 6e     if( keyColumn
220ef 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 66 28  >=0 ){.      if(
220f0 20 75 73 65 54 65 6d 70 54 61 62 6c 65 20 29 7b   useTempTable ){
220f1 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
220f2 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50  VdbeAddOp3(v, OP
220f3 5f 43 6f 6c 75 6d 6e 2c 20 73 72 63 54 61 62 2c  _Column, srcTab,
220f4 20 6b 65 79 43 6f 6c 75 6d 6e 2c 20 72 65 67 52   keyColumn, regR
220f5 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 7d 65 6c  owid);.      }el
220f6 73 65 20 69 66 28 20 70 53 65 6c 65 63 74 20 29  se if( pSelect )
220f7 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
220f8 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
220f9 50 5f 53 43 6f 70 79 2c 20 72 65 67 46 72 6f 6d  P_SCopy, regFrom
220fa 53 65 6c 65 63 74 2b 6b 65 79 43 6f 6c 75 6d 6e  Select+keyColumn
220fb 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
220fc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
220fd 20 20 56 64 62 65 4f 70 20 2a 70 4f 70 3b 0a 20    VdbeOp *pOp;. 
220fe 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
220ff 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70  prCode(pParse, p
22100 4c 69 73 74 2d 3e 61 5b 6b 65 79 43 6f 6c 75 6d  List->a[keyColum
22101 6e 5d 2e 70 45 78 70 72 2c 20 72 65 67 52 6f 77  n].pExpr, regRow
22102 69 64 29 3b 0a 20 20 20 20 20 20 20 20 70 4f 70  id);.        pOp
22103 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 47 65   = sqlite3VdbeGe
22104 74 4f 70 28 76 2c 20 73 71 6c 69 74 65 33 56 64  tOp(v, sqlite3Vd
22105 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
22106 20 2d 20 31 29 3b 0a 20 20 20 20 20 20 20 20 69   - 1);.        i
22107 66 28 20 70 4f 70 20 26 26 20 70 4f 70 2d 3e 6f  f( pOp && pOp->o
22108 70 63 6f 64 65 3d 3d 4f 50 5f 4e 75 6c 6c 20 26  pcode==OP_Null &
22109 26 20 21 49 73 56 69 72 74 75 61 6c 28 70 54 61  & !IsVirtual(pTa
2210a 62 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  b) ){.          
2210b 61 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a  appendFlag = 1;.
2210c 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 6f            pOp->o
2210d 70 63 6f 64 65 20 3d 20 4f 50 5f 4e 65 77 52 6f  pcode = OP_NewRo
2210e 77 69 64 3b 0a 20 20 20 20 20 20 20 20 20 20 70  wid;.          p
2210f 4f 70 2d 3e 70 31 20 3d 20 62 61 73 65 43 75 72  Op->p1 = baseCur
22110 3b 0a 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d  ;.          pOp-
22111 3e 70 32 20 3d 20 72 65 67 52 6f 77 69 64 3b 0a  >p2 = regRowid;.
22112 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e 70            pOp->p
22113 33 20 3d 20 72 65 67 41 75 74 6f 69 6e 63 3b 0a  3 = regAutoinc;.
22114 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22115 7d 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  }.      /* If th
22116 65 20 50 52 49 4d 41 52 59 20 4b 45 59 20 65 78  e PRIMARY KEY ex
22117 70 72 65 73 73 69 6f 6e 20 69 73 20 4e 55 4c 4c  pression is NULL
22118 2c 20 74 68 65 6e 20 75 73 65 20 4f 50 5f 4e 65  , then use OP_Ne
22119 77 52 6f 77 69 64 0a 20 20 20 20 20 20 2a 2a 20  wRowid.      ** 
2211a 74 6f 20 67 65 6e 65 72 61 74 65 20 61 20 75 6e  to generate a un
2211b 69 71 75 65 20 70 72 69 6d 61 72 79 20 6b 65 79  ique primary key
2211c 20 76 61 6c 75 65 2e 0a 20 20 20 20 20 20 2a 2f   value..      */
2211d 0a 20 20 20 20 20 20 69 66 28 20 21 61 70 70 65  .      if( !appe
2211e 6e 64 46 6c 61 67 20 29 7b 0a 20 20 20 20 20 20  ndFlag ){.      
2211f 20 20 69 6e 74 20 6a 31 3b 0a 20 20 20 20 20 20    int j1;.      
22120 20 20 69 66 28 20 21 49 73 56 69 72 74 75 61 6c    if( !IsVirtual
22121 28 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20  (pTab) ){.      
22122 20 20 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33      j1 = sqlite3
22123 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
22124 5f 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 52 6f 77  _NotNull, regRow
22125 69 64 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  id);.          s
22126 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
22127 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69 64 2c  (v, OP_NewRowid,
22128 20 62 61 73 65 43 75 72 2c 20 72 65 67 52 6f 77   baseCur, regRow
22129 69 64 2c 20 72 65 67 41 75 74 6f 69 6e 63 29 3b  id, regAutoinc);
2212a 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2212b 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2212c 2c 20 6a 31 29 3b 0a 20 20 20 20 20 20 20 20 7d  , j1);.        }
2212d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2212e 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
2212f 43 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a  CurrentAddr(v);.
22130 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22131 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22132 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 52 6f 77  P_IsNull, regRow
22133 69 64 2c 20 6a 31 2b 32 29 3b 0a 20 20 20 20 20  id, j1+2);.     
22134 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
22135 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
22136 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
22137 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
22138 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28   }.    }else if(
22139 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
2213a 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
2213b 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2213c 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 52 6f  P_Null, 0, regRo
2213d 77 69 64 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  wid);.    }else{
2213e 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
2213f 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
22140 65 77 52 6f 77 69 64 2c 20 62 61 73 65 43 75 72  ewRowid, baseCur
22141 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67 41  , regRowid, regA
22142 75 74 6f 69 6e 63 29 3b 0a 20 20 20 20 20 20 61  utoinc);.      a
22143 70 70 65 6e 64 46 6c 61 67 20 3d 20 31 3b 0a 20  ppendFlag = 1;. 
22144 20 20 20 7d 0a 20 20 20 20 61 75 74 6f 49 6e 63     }.    autoInc
22145 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65 67  Step(pParse, reg
22146 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77 69  Autoinc, regRowi
22147 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73 68  d);..    /* Push
22148 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 2c   onto the stack,
22149 20 64 61 74 61 20 66 6f 72 20 61 6c 6c 20 63 6f   data for all co
2214a 6c 75 6d 6e 73 20 6f 66 20 74 68 65 20 6e 65 77  lumns of the new
2214b 20 65 6e 74 72 79 2c 20 62 65 67 69 6e 6e 69 6e   entry, beginnin
2214c 67 0a 20 20 20 20 2a 2a 20 77 69 74 68 20 74 68  g.    ** with th
2214d 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 2e 0a  e first column..
2214e 20 20 20 20 2a 2f 0a 20 20 20 20 6e 48 69 64 64      */.    nHidd
2214f 65 6e 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28  en = 0;.    for(
22150 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
22151 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
22152 6e 74 20 69 52 65 67 53 74 6f 72 65 20 3d 20 72  nt iRegStore = r
22153 65 67 52 6f 77 69 64 2b 31 2b 69 3b 0a 20 20 20  egRowid+1+i;.   
22154 20 20 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e     if( i==pTab->
22155 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 20  iPKey ){.       
22156 20 2f 2a 20 54 68 65 20 76 61 6c 75 65 20 6f 66   /* The value of
22157 20 74 68 65 20 49 4e 54 45 47 45 52 20 50 52 49   the INTEGER PRI
22158 4d 41 52 59 20 4b 45 59 20 63 6f 6c 75 6d 6e 20  MARY KEY column 
22159 69 73 20 61 6c 77 61 79 73 20 61 20 4e 55 4c 4c  is always a NULL
2215a 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 57 68 65  ..        ** Whe
2215b 6e 65 76 65 72 20 74 68 69 73 20 63 6f 6c 75 6d  never this colum
2215c 6e 20 69 73 20 72 65 61 64 2c 20 74 68 65 20 72  n is read, the r
2215d 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 77 69 6c  ecord number wil
2215e 6c 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  l be substituted
2215f 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 6e 20 69  .        ** in i
22160 74 73 20 70 6c 61 63 65 2e 20 20 53 6f 20 77 69  ts place.  So wi
22161 6c 6c 20 66 69 6c 6c 20 74 68 69 73 20 63 6f 6c  ll fill this col
22162 75 6d 6e 20 77 69 74 68 20 61 20 4e 55 4c 4c 20  umn with a NULL 
22163 74 6f 20 61 76 6f 69 64 0a 20 20 20 20 20 20 20  to avoid.       
22164 20 2a 2a 20 74 61 6b 69 6e 67 20 75 70 20 64 61   ** taking up da
22165 74 61 20 73 70 61 63 65 20 77 69 74 68 20 69 6e  ta space with in
22166 66 6f 72 6d 61 74 69 6f 6e 20 74 68 61 74 20 77  formation that w
22167 69 6c 6c 20 6e 65 76 65 72 20 62 65 20 75 73 65  ill never be use
22168 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71  d. */.        sq
22169 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2216a 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 69  v, OP_Null, 0, i
2216b 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20 20  RegStore);.     
2216c 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2216d 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70     }.      if( p
2216e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b 0a 20 20 20  Column==0 ){.   
2216f 20 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65       if( IsHidde
22170 6e 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61  nColumn(&pTab->a
22171 43 6f 6c 5b 69 5d 29 20 29 7b 0a 20 20 20 20 20  Col[i]) ){.     
22172 20 20 20 20 20 61 73 73 65 72 74 28 20 49 73 56       assert( IsV
22173 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 3b 0a  irtual(pTab) );.
22174 20 20 20 20 20 20 20 20 20 20 6a 20 3d 20 2d 31            j = -1
22175 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 48 69 64  ;.          nHid
22176 64 65 6e 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  den++;.        }
22177 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
22178 6a 20 3d 20 69 20 2d 20 6e 48 69 64 64 65 6e 3b  j = i - nHidden;
22179 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2217a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2217b 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 43 6f 6c 75  for(j=0; j<pColu
2217c 6d 6e 2d 3e 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20  mn->nId; j++){. 
2217d 20 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f           if( pCo
2217e 6c 75 6d 6e 2d 3e 61 5b 6a 5d 2e 69 64 78 3d 3d  lumn->a[j].idx==
2217f 69 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  i ) break;.     
22180 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
22181 20 20 20 69 66 28 20 6a 3c 30 20 7c 7c 20 6e 43     if( j<0 || nC
22182 6f 6c 75 6d 6e 3d 3d 30 20 7c 7c 20 28 70 43 6f  olumn==0 || (pCo
22183 6c 75 6d 6e 20 26 26 20 6a 3e 3d 70 43 6f 6c 75  lumn && j>=pColu
22184 6d 6e 2d 3e 6e 49 64 29 20 29 7b 0a 20 20 20 20  mn->nId) ){.    
22185 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
22186 6f 64 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  ode(pParse, pTab
22187 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66 6c 74 2c  ->aCol[i].pDflt,
22188 20 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20   iRegStore);.   
22189 20 20 20 7d 65 6c 73 65 20 69 66 28 20 75 73 65     }else if( use
2218a 54 65 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20  TempTable ){.   
2218b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2218c 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c  AddOp3(v, OP_Col
2218d 75 6d 6e 2c 20 73 72 63 54 61 62 2c 20 6a 2c 20  umn, srcTab, j, 
2218e 69 52 65 67 53 74 6f 72 65 29 3b 20 0a 20 20 20  iRegStore); .   
2218f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 53 65     }else if( pSe
22190 6c 65 63 74 20 29 7b 0a 20 20 20 20 20 20 20 20  lect ){.        
22191 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22192 32 28 76 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72  2(v, OP_SCopy, r
22193 65 67 46 72 6f 6d 53 65 6c 65 63 74 2b 6a 2c 20  egFromSelect+j, 
22194 69 52 65 67 53 74 6f 72 65 29 3b 0a 20 20 20 20  iRegStore);.    
22195 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
22196 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65   sqlite3ExprCode
22197 28 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2d 3e  (pParse, pList->
22198 61 5b 6a 5d 2e 70 45 78 70 72 2c 20 69 52 65 67  a[j].pExpr, iReg
22199 53 74 6f 72 65 29 3b 0a 20 20 20 20 20 20 7d 0a  Store);.      }.
2219a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 47 65      }..    /* Ge
2219b 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 63  nerate code to c
2219c 68 65 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73  heck constraints
2219d 20 61 6e 64 20 67 65 6e 65 72 61 74 65 20 69 6e   and generate in
2219e 64 65 78 20 6b 65 79 73 20 61 6e 64 0a 20 20 20  dex keys and.   
2219f 20 2a 2a 20 64 6f 20 74 68 65 20 69 6e 73 65 72   ** do the inser
221a0 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 23 69 66  tion..    */.#if
221a1 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
221a2 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
221a3 20 20 69 66 28 20 49 73 56 69 72 74 75 61 6c 28    if( IsVirtual(
221a4 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 73  pTab) ){.      s
221a5 71 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72  qlite3VtabMakeWr
221a6 69 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70  itable(pParse, p
221a7 54 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Tab);.      sqli
221a8 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
221a9 20 4f 50 5f 56 55 70 64 61 74 65 2c 20 31 2c 20   OP_VUpdate, 1, 
221aa 70 54 61 62 2d 3e 6e 43 6f 6c 2b 32 2c 20 72 65  pTab->nCol+2, re
221ab 67 49 6e 73 2c 0a 20 20 20 20 20 20 20 20 20 20  gIns,.          
221ac 20 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e 73             (cons
221ad 74 20 63 68 61 72 2a 29 70 54 61 62 2d 3e 70 56  t char*)pTab->pV
221ae 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a 20  tab, P4_VTAB);. 
221af 20 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a     }else.#endif.
221b0 20 20 20 20 7b 0a 20 20 20 20 20 20 73 71 6c 69      {.      sqli
221b1 74 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74  te3GenerateConst
221b2 72 61 69 6e 74 43 68 65 63 6b 73 28 0a 20 20 20  raintChecks(.   
221b3 20 20 20 20 20 20 20 70 50 61 72 73 65 2c 0a 20         pParse,. 
221b4 20 20 20 20 20 20 20 20 20 70 54 61 62 2c 0a 20           pTab,. 
221b5 20 20 20 20 20 20 20 20 20 62 61 73 65 43 75 72           baseCur
221b6 2c 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 49  ,.          regI
221b7 6e 73 2c 0a 20 20 20 20 20 20 20 20 20 20 61 52  ns,.          aR
221b8 65 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20  egIdx,.         
221b9 20 6b 65 79 43 6f 6c 75 6d 6e 3e 3d 30 2c 0a 20   keyColumn>=0,. 
221ba 20 20 20 20 20 20 20 20 20 30 2c 0a 20 20 20 20           0,.    
221bb 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 2c 0a 20        onError,. 
221bc 20 20 20 20 20 20 20 20 20 65 6e 64 4f 66 4c 6f           endOfLo
221bd 6f 70 0a 20 20 20 20 20 20 29 3b 0a 20 20 20 20  op.      );.    
221be 20 20 73 71 6c 69 74 65 33 43 6f 6d 70 6c 65 74    sqlite3Complet
221bf 65 49 6e 73 65 72 74 69 6f 6e 28 0a 20 20 20 20  eInsertion(.    
221c0 20 20 20 20 20 20 70 50 61 72 73 65 2c 0a 20 20        pParse,.  
221c1 20 20 20 20 20 20 20 20 70 54 61 62 2c 0a 20 20          pTab,.  
221c2 20 20 20 20 20 20 20 20 62 61 73 65 43 75 72 2c          baseCur,
221c3 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 49 6e  .          regIn
221c4 73 2c 0a 20 20 20 20 20 20 20 20 20 20 61 52 65  s,.          aRe
221c5 67 49 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20  gIdx,.          
221c6 30 2c 0a 20 20 20 20 20 20 20 20 20 20 28 74 72  0,.          (tr
221c7 69 67 67 65 72 73 5f 65 78 69 73 74 20 26 20 54  iggers_exist & T
221c8 52 49 47 47 45 52 5f 41 46 54 45 52 29 21 3d 30  RIGGER_AFTER)!=0
221c9 20 3f 20 6e 65 77 49 64 78 20 3a 20 2d 31 2c 0a   ? newIdx : -1,.
221ca 20 20 20 20 20 20 20 20 20 20 61 70 70 65 6e 64            append
221cb 46 6c 61 67 0a 20 20 20 20 20 20 20 29 3b 0a 20  Flag.       );. 
221cc 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55     }.  }..  /* U
221cd 70 64 61 74 65 20 74 68 65 20 63 6f 75 6e 74 20  pdate the count 
221ce 6f 66 20 72 6f 77 73 20 74 68 61 74 20 61 72 65  of rows that are
221cf 20 69 6e 73 65 72 74 65 64 0a 20 20 2a 2f 0a 20   inserted.  */. 
221d0 20 69 66 28 20 28 64 62 2d 3e 66 6c 61 67 73 20   if( (db->flags 
221d1 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
221d2 77 73 29 21 3d 30 20 29 7b 0a 20 20 20 20 73 71  ws)!=0 ){.    sq
221d3 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
221d4 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72 65  v, OP_AddImm, re
221d5 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20  gRowCount, 1);. 
221d6 20 7d 0a 0a 20 20 69 66 28 20 74 72 69 67 67 65   }..  if( trigge
221d7 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20 20  rs_exist ){.    
221d8 2f 2a 20 43 6f 64 65 20 41 46 54 45 52 20 74 72  /* Code AFTER tr
221d9 69 67 67 65 72 73 20 2a 2f 0a 20 20 20 20 69 66  iggers */.    if
221da 28 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77  ( sqlite3CodeRow
221db 54 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20  Trigger(pParse, 
221dc 54 4b 5f 49 4e 53 45 52 54 2c 20 30 2c 20 54 52  TK_INSERT, 0, TR
221dd 49 47 47 45 52 5f 41 46 54 45 52 2c 20 70 54 61  IGGER_AFTER, pTa
221de 62 2c 0a 20 20 20 20 20 20 20 20 20 20 6e 65 77  b,.          new
221df 49 64 78 2c 20 2d 31 2c 20 6f 6e 45 72 72 6f 72  Idx, -1, onError
221e0 2c 20 65 6e 64 4f 66 4c 6f 6f 70 2c 20 30 2c 20  , endOfLoop, 0, 
221e1 30 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f  0) ){.      goto
221e2 20 69 6e 73 65 72 74 5f 63 6c 65 61 6e 75 70 3b   insert_cleanup;
221e3 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
221e4 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f 66 20 74   The bottom of t
221e5 68 65 20 6d 61 69 6e 20 69 6e 73 65 72 74 69 6f  he main insertio
221e6 6e 20 6c 6f 6f 70 2c 20 69 66 20 74 68 65 20 64  n loop, if the d
221e7 61 74 61 20 73 6f 75 72 63 65 0a 20 20 2a 2a 20  ata source.  ** 
221e8 69 73 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  is a SELECT stat
221e9 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 73 71  ement..  */.  sq
221ea 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
221eb 4c 61 62 65 6c 28 76 2c 20 65 6e 64 4f 66 4c 6f  Label(v, endOfLo
221ec 6f 70 29 3b 0a 20 20 69 66 28 20 75 73 65 54 65  op);.  if( useTe
221ed 6d 70 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 73  mpTable ){.    s
221ee 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
221ef 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 72 63  (v, OP_Next, src
221f0 54 61 62 2c 20 61 64 64 72 43 6f 6e 74 29 3b 0a  Tab, addrCont);.
221f1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
221f2 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 49  umpHere(v, addrI
221f3 6e 73 54 6f 70 29 3b 0a 20 20 20 20 73 71 6c 69  nsTop);.    sqli
221f4 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
221f5 20 4f 50 5f 43 6c 6f 73 65 2c 20 73 72 63 54 61   OP_Close, srcTa
221f6 62 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  b);.  }else if( 
221f7 70 53 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 73  pSelect ){.    s
221f8 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
221f9 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
221fa 61 64 64 72 43 6f 6e 74 29 3b 0a 20 20 20 20 73  addrCont);.    s
221fb 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
221fc 72 65 28 76 2c 20 61 64 64 72 49 6e 73 54 6f 70  re(v, addrInsTop
221fd 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21 49  );.  }..  if( !I
221fe 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 26  sVirtual(pTab) &
221ff 26 20 21 69 73 56 69 65 77 20 29 7b 0a 20 20 20  & !isView ){.   
22200 20 2f 2a 20 43 6c 6f 73 65 20 61 6c 6c 20 74 61   /* Close all ta
22201 62 6c 65 73 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  bles opened */. 
22202 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
22203 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73 65  dOp1(v, OP_Close
22204 2c 20 62 61 73 65 43 75 72 29 3b 0a 20 20 20 20  , baseCur);.    
22205 66 6f 72 28 69 64 78 3d 31 2c 20 70 49 64 78 3d  for(idx=1, pIdx=
22206 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
22207 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
22208 4e 65 78 74 2c 20 69 64 78 2b 2b 29 7b 0a 20 20  Next, idx++){.  
22209 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2220a 64 64 4f 70 31 28 76 2c 20 4f 50 5f 43 6c 6f 73  ddOp1(v, OP_Clos
2220b 65 2c 20 69 64 78 2b 62 61 73 65 43 75 72 29 3b  e, idx+baseCur);
2220c 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2220d 20 55 70 64 61 74 65 20 74 68 65 20 73 71 6c 69   Update the sqli
2220e 74 65 5f 73 65 71 75 65 6e 63 65 20 74 61 62 6c  te_sequence tabl
2220f 65 20 62 79 20 73 74 6f 72 69 6e 67 20 74 68 65  e by storing the
22210 20 63 6f 6e 74 65 6e 74 20 6f 66 20 74 68 65 0a   content of the.
22211 20 20 2a 2a 20 63 6f 75 6e 74 65 72 20 76 61 6c    ** counter val
22212 75 65 20 69 6e 20 6d 65 6d 6f 72 79 20 72 65 67  ue in memory reg
22213 41 75 74 6f 69 6e 63 20 62 61 63 6b 20 69 6e 74  Autoinc back int
22214 6f 20 74 68 65 20 73 71 6c 69 74 65 5f 73 65 71  o the sqlite_seq
22215 75 65 6e 63 65 0a 20 20 2a 2a 20 74 61 62 6c 65  uence.  ** table
22216 2e 0a 20 20 2a 2f 0a 20 20 61 75 74 6f 49 6e 63  ..  */.  autoInc
22217 45 6e 64 28 70 50 61 72 73 65 2c 20 69 44 62 2c  End(pParse, iDb,
22218 20 70 54 61 62 2c 20 72 65 67 41 75 74 6f 69 6e   pTab, regAutoin
22219 63 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 52  c);..  /*.  ** R
2221a 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65 72  eturn the number
2221b 20 6f 66 20 72 6f 77 73 20 69 6e 73 65 72 74 65   of rows inserte
2221c 64 2e 20 49 66 20 74 68 69 73 20 72 6f 75 74 69  d. If this routi
2221d 6e 65 20 69 73 20 0a 20 20 2a 2a 20 67 65 6e 65  ne is .  ** gene
2221e 72 61 74 69 6e 67 20 63 6f 64 65 20 62 65 63 61  rating code beca
2221f 75 73 65 20 6f 66 20 61 20 63 61 6c 6c 20 74 6f  use of a call to
22220 20 73 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61   sqlite3NestedPa
22221 72 73 65 28 29 2c 20 64 6f 20 6e 6f 74 0a 20 20  rse(), do not.  
22222 2a 2a 20 69 6e 76 6f 6b 65 20 74 68 65 20 63 61  ** invoke the ca
22223 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 2e  llback function.
22224 0a 20 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  .  */.  if( db->
22225 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43  flags & SQLITE_C
22226 6f 75 6e 74 52 6f 77 73 20 26 26 20 70 50 61 72  ountRows && pPar
22227 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 26 26  se->nested==0 &&
22228 20 21 70 50 61 72 73 65 2d 3e 74 72 69 67 53 74   !pParse->trigSt
22229 61 63 6b 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ack ){.    sqlit
2222a 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2222b 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 72 65  OP_ResultRow, re
2222c 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a 20  gRowCount, 1);. 
2222d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
2222e 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31 29 3b 0a  tNumCols(v, 1);.
2222f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
22230 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
22231 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 72  COLNAME_NAME, "r
22232 6f 77 73 20 69 6e 73 65 72 74 65 64 22 2c 20 53  ows inserted", S
22233 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
22234 20 7d 0a 0a 69 6e 73 65 72 74 5f 63 6c 65 61 6e   }..insert_clean
22235 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72 63  up:.  sqlite3Src
22236 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70  ListDelete(db, p
22237 54 61 62 4c 69 73 74 29 3b 0a 20 20 73 71 6c 69  TabList);.  sqli
22238 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
22239 65 28 64 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20  e(db, pList);.  
2223a 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c  sqlite3SelectDel
2223b 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63 74 29  ete(db, pSelect)
2223c 3b 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73  ;.  sqlite3IdLis
2223d 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 6f 6c  tDelete(db, pCol
2223e 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 44  umn);.  sqlite3D
2223f 62 46 72 65 65 28 64 62 2c 20 61 52 65 67 49 64  bFree(db, aRegId
22240 78 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e  x);.}../*.** Gen
22241 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 64 6f  erate code to do
22242 20 63 6f 6e 73 74 72 61 69 6e 74 20 63 68 65 63   constraint chec
22243 6b 73 20 70 72 69 6f 72 20 74 6f 20 61 6e 20 49  ks prior to an I
22244 4e 53 45 52 54 20 6f 72 20 61 6e 20 55 50 44 41  NSERT or an UPDA
22245 54 45 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e  TE..**.** The in
22246 70 75 74 20 69 73 20 61 20 72 61 6e 67 65 20 6f  put is a range o
22247 66 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 65  f consecutive re
22248 67 69 73 74 65 72 73 20 61 73 20 66 6f 6c 6c 6f  gisters as follo
22249 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 31 2e 20  ws:.**.**    1. 
2224a 20 54 68 65 20 72 6f 77 69 64 20 6f 66 20 74 68   The rowid of th
2224b 65 20 72 6f 77 20 74 6f 20 62 65 20 75 70 64 61  e row to be upda
2224c 74 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 75  ted before the u
2224d 70 64 61 74 65 2e 20 20 54 68 69 73 0a 2a 2a 20  pdate.  This.** 
2224e 20 20 20 20 20 20 20 76 61 6c 75 65 20 69 73 20         value is 
2224f 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73 73 20 77  omitted unless w
22250 65 20 61 72 65 20 64 6f 69 6e 67 20 61 6e 20 55  e are doing an U
22251 50 44 41 54 45 20 74 68 61 74 20 69 6e 76 6f 6c  PDATE that invol
22252 76 65 73 20 61 0a 2a 2a 20 20 20 20 20 20 20 20  ves a.**        
22253 63 68 61 6e 67 65 20 74 6f 20 74 68 65 20 72 65  change to the re
22254 63 6f 72 64 20 6e 75 6d 62 65 72 20 6f 72 20 77  cord number or w
22255 72 69 74 69 6e 67 20 74 6f 20 61 20 76 69 72 74  riting to a virt
22256 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a  ual table..**.**
22257 20 20 20 20 32 2e 20 20 54 68 65 20 72 6f 77 69      2.  The rowi
22258 64 20 6f 66 20 74 68 65 20 72 6f 77 20 61 66 74  d of the row aft
22259 65 72 20 74 68 65 20 75 70 64 61 74 65 2e 0a 2a  er the update..*
2225a 2a 0a 2a 2a 20 20 20 20 33 2e 20 20 54 68 65 20  *.**    3.  The 
2225b 64 61 74 61 20 69 6e 20 74 68 65 20 66 69 72 73  data in the firs
2225c 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
2225d 65 6e 74 72 79 20 61 66 74 65 72 20 74 68 65 20  entry after the 
2225e 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 20 20  update..**.**   
2225f 20 69 2e 20 20 44 61 74 61 20 66 72 6f 6d 20 6d   i.  Data from m
22260 69 64 64 6c 65 20 63 6f 6c 75 6d 6e 73 2e 2e 2e  iddle columns...
22261 0a 2a 2a 0a 2a 2a 20 20 20 20 4e 2e 20 20 54 68  .**.**    N.  Th
22262 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 6c 61  e data in the la
22263 73 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  st column of the
22264 20 65 6e 74 72 79 20 61 66 74 65 72 20 74 68 65   entry after the
22265 20 75 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54   update..**.** T
22266 68 65 20 72 65 67 52 6f 77 69 64 20 70 61 72 61  he regRowid para
22267 6d 65 74 65 72 20 69 73 20 74 68 65 20 69 6e 64  meter is the ind
22268 65 78 20 6f 66 20 74 68 65 20 72 65 67 69 73 74  ex of the regist
22269 65 72 20 63 6f 6e 74 61 69 6e 69 6e 67 20 28 32  er containing (2
2226a 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6f 6c 64  )..**.** The old
2226b 20 72 6f 77 69 64 20 73 68 6f 77 6e 20 61 73 20   rowid shown as 
2226c 65 6e 74 72 79 20 28 31 29 20 61 62 6f 76 65 20  entry (1) above 
2226d 69 73 20 6f 6d 69 74 74 65 64 20 75 6e 6c 65 73  is omitted unles
2226e 73 20 62 6f 74 68 20 69 73 55 70 64 61 74 65 0a  s both isUpdate.
2226f 2a 2a 20 61 6e 64 20 72 6f 77 69 64 43 68 6e 67  ** and rowidChng
22270 20 61 72 65 20 31 2e 20 20 69 73 55 70 64 61 74   are 1.  isUpdat
22271 65 20 69 73 20 74 72 75 65 20 66 6f 72 20 55 50  e is true for UP
22272 44 41 54 45 73 20 61 6e 64 20 66 61 6c 73 65 20  DATEs and false 
22273 66 6f 72 0a 2a 2a 20 49 4e 53 45 52 54 73 2e 20  for.** INSERTs. 
22274 20 52 6f 77 69 64 43 68 6e 67 20 6d 65 61 6e 73   RowidChng means
22275 20 74 68 61 74 20 74 68 65 20 6e 65 77 20 72 6f   that the new ro
22276 77 69 64 20 69 73 20 65 78 70 6c 69 63 69 74 6c  wid is explicitl
22277 79 20 73 70 65 63 69 66 69 65 64 20 62 79 0a 2a  y specified by.*
22278 2a 20 74 68 65 20 75 70 64 61 74 65 20 6f 72 20  * the update or 
22279 69 6e 73 65 72 74 20 73 74 61 74 65 6d 65 6e 74  insert statement
2227a 2e 20 20 49 66 20 72 6f 77 69 64 43 68 6e 67 20  .  If rowidChng 
2227b 69 73 20 66 61 6c 73 65 2c 20 69 74 20 6d 65 61  is false, it mea
2227c 6e 73 20 74 68 61 74 0a 2a 2a 20 74 68 65 20 72  ns that.** the r
2227d 6f 77 69 64 20 69 73 20 63 6f 6d 70 75 74 65 64  owid is computed
2227e 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 69   automatically i
2227f 6e 20 61 6e 20 69 6e 73 65 72 74 20 6f 72 20 74  n an insert or t
22280 68 61 74 20 74 68 65 20 72 6f 77 69 64 20 76 61  hat the rowid va
22281 6c 75 65 0a 2a 2a 20 69 73 20 6e 6f 74 20 6d 6f  lue.** is not mo
22282 64 69 66 69 65 64 20 62 79 20 74 68 65 20 75 70  dified by the up
22283 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  date..**.** The 
22284 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 62  code generated b
22285 79 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  y this routine s
22286 74 6f 72 65 20 6e 65 77 20 69 6e 64 65 78 20 65  tore new index e
22287 6e 74 72 69 65 73 20 69 6e 74 6f 0a 2a 2a 20 72  ntries into.** r
22288 65 67 69 73 74 65 72 73 20 69 64 65 6e 74 69 66  egisters identif
22289 69 65 64 20 62 79 20 61 52 65 67 49 64 78 5b 5d  ied by aRegIdx[]
2228a 2e 20 20 4e 6f 20 69 6e 64 65 78 20 65 6e 74 72  .  No index entr
2228b 79 20 69 73 20 63 72 65 61 74 65 64 20 66 6f 72  y is created for
2228c 0a 2a 2a 20 69 6e 64 69 63 65 73 20 77 68 65 72  .** indices wher
2228d 65 20 61 52 65 67 49 64 78 5b 69 5d 3d 3d 30 2e  e aRegIdx[i]==0.
2228e 20 20 54 68 65 20 6f 72 64 65 72 20 6f 66 20 69    The order of i
2228f 6e 64 69 63 65 73 20 69 6e 20 61 52 65 67 49 64  ndices in aRegId
22290 78 5b 5d 20 69 73 0a 2a 2a 20 74 68 65 20 73 61  x[] is.** the sa
22291 6d 65 20 61 73 20 74 68 65 20 6f 72 64 65 72 20  me as the order 
22292 6f 66 20 69 6e 64 69 63 65 73 20 6f 6e 20 74 68  of indices on th
22293 65 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20 6f 66  e linked list of
22294 20 69 6e 64 69 63 65 73 0a 2a 2a 20 61 74 74 61   indices.** atta
22295 63 68 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c  ched to the tabl
22296 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  e..**.** This ro
22297 75 74 69 6e 65 20 61 6c 73 6f 20 67 65 6e 65 72  utine also gener
22298 61 74 65 73 20 63 6f 64 65 20 74 6f 20 63 68 65  ates code to che
22299 63 6b 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20  ck constraints. 
2229a 20 4e 4f 54 20 4e 55 4c 4c 2c 0a 2a 2a 20 43 48   NOT NULL,.** CH
2229b 45 43 4b 2c 20 61 6e 64 20 55 4e 49 51 55 45 20  ECK, and UNIQUE 
2229c 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20  constraints are 
2229d 61 6c 6c 20 63 68 65 63 6b 65 64 2e 20 20 49 66  all checked.  If
2229e 20 61 20 63 6f 6e 73 74 72 61 69 6e 74 20 66 61   a constraint fa
2229f 69 6c 73 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65  ils,.** then the
222a0 20 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74   appropriate act
222a1 69 6f 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64  ion is performed
222a2 2e 20 20 54 68 65 72 65 20 61 72 65 20 66 69 76  .  There are fiv
222a3 65 20 70 6f 73 73 69 62 6c 65 0a 2a 2a 20 61 63  e possible.** ac
222a4 74 69 6f 6e 73 3a 20 52 4f 4c 4c 42 41 43 4b 2c  tions: ROLLBACK,
222a5 20 41 42 4f 52 54 2c 20 46 41 49 4c 2c 20 52 45   ABORT, FAIL, RE
222a6 50 4c 41 43 45 2c 20 61 6e 64 20 49 47 4e 4f 52  PLACE, and IGNOR
222a7 45 2e 0a 2a 2a 0a 2a 2a 20 20 43 6f 6e 73 74 72  E..**.**  Constr
222a8 61 69 6e 74 20 74 79 70 65 20 20 41 63 74 69 6f  aint type  Actio
222a9 6e 20 20 20 20 20 20 20 57 68 61 74 20 48 61 70  n       What Hap
222aa 70 65 6e 73 0a 2a 2a 20 20 2d 2d 2d 2d 2d 2d 2d  pens.**  -------
222ab 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d 2d 2d 2d  --------  ------
222ac 2d 2d 2d 2d 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----   ---------
222ad 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
222ae 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
222af 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
222b0 20 20 20 20 20 52 4f 4c 4c 42 41 43 4b 20 20 20       ROLLBACK   
222b1 20 20 54 68 65 20 63 75 72 72 65 6e 74 20 74 72    The current tr
222b2 61 6e 73 61 63 74 69 6f 6e 20 69 73 20 72 6f 6c  ansaction is rol
222b3 6c 65 64 20 62 61 63 6b 20 61 6e 64 0a 2a 2a 20  led back and.** 
222b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
222b6 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 72 65  qlite3_exec() re
222b7 74 75 72 6e 73 20 69 6d 6d 65 64 69 61 74 65 6c  turns immediatel
222b8 79 20 77 69 74 68 20 61 0a 2a 2a 20 20 20 20 20  y with a.**     
222b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222ba 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
222bb 6e 20 63 6f 64 65 20 6f 66 20 53 51 4c 49 54 45  n code of SQLITE
222bc 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a 2a 0a  _CONSTRAINT..**.
222bd 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
222be 20 20 20 20 20 41 42 4f 52 54 20 20 20 20 20 20       ABORT      
222bf 20 20 42 61 63 6b 20 6f 75 74 20 63 68 61 6e 67    Back out chang
222c0 65 73 20 66 72 6f 6d 20 74 68 65 20 63 75 72 72  es from the curr
222c1 65 6e 74 20 63 6f 6d 6d 61 6e 64 0a 2a 2a 20 20  ent command.**  
222c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 6e                on
222c4 6c 79 20 28 64 6f 20 6e 6f 74 20 64 6f 20 61 20  ly (do not do a 
222c5 63 6f 6d 70 6c 65 74 65 20 72 6f 6c 6c 62 61 63  complete rollbac
222c6 6b 29 20 74 68 65 6e 0a 2a 2a 20 20 20 20 20 20  k) then.**      
222c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222c8 20 20 20 20 20 20 20 20 20 20 63 61 75 73 65 20            cause 
222c9 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 20 74  sqlite3_exec() t
222ca 6f 20 72 65 74 75 72 6e 20 69 6d 6d 65 64 69 61  o return immedia
222cb 74 65 6c 79 0a 2a 2a 20 20 20 20 20 20 20 20 20  tely.**         
222cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222cd 20 20 20 20 20 20 20 77 69 74 68 20 53 51 4c 49         with SQLI
222ce 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e 0a 2a  TE_CONSTRAINT..*
222cf 2a 0a 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20  *.**  any       
222d0 20 20 20 20 20 20 20 46 41 49 4c 20 20 20 20 20         FAIL     
222d1 20 20 20 20 53 71 6c 69 74 65 5f 65 78 65 63 28      Sqlite_exec(
222d2 29 20 72 65 74 75 72 6e 73 20 69 6d 6d 65 64 69  ) returns immedi
222d3 61 74 65 6c 79 20 77 69 74 68 20 61 0a 2a 2a 20  ately with a.** 
222d4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72                 r
222d6 65 74 75 72 6e 20 63 6f 64 65 20 6f 66 20 53 51  eturn code of SQ
222d7 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2e  LITE_CONSTRAINT.
222d8 20 20 54 68 65 0a 2a 2a 20 20 20 20 20 20 20 20    The.**        
222d9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222da 20 20 20 20 20 20 20 20 74 72 61 6e 73 61 63 74          transact
222db 69 6f 6e 20 69 73 20 6e 6f 74 20 72 6f 6c 6c 65  ion is not rolle
222dc 64 20 62 61 63 6b 20 61 6e 64 20 61 6e 79 0a 2a  d back and any.*
222dd 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
222de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222df 20 70 72 69 6f 72 20 63 68 61 6e 67 65 73 20 61   prior changes a
222e0 72 65 20 72 65 74 61 69 6e 65 64 2e 0a 2a 2a 0a  re retained..**.
222e1 2a 2a 20 20 61 6e 79 20 20 20 20 20 20 20 20 20  **  any         
222e2 20 20 20 20 20 49 47 4e 4f 52 45 20 20 20 20 20       IGNORE     
222e3 20 20 54 68 65 20 72 65 63 6f 72 64 20 6e 75 6d    The record num
222e4 62 65 72 20 61 6e 64 20 64 61 74 61 20 69 73 20  ber and data is 
222e5 70 6f 70 70 65 64 20 66 72 6f 6d 0a 2a 2a 20 20  popped from.**  
222e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 68                th
222e8 65 20 73 74 61 63 6b 20 61 6e 64 20 74 68 65 72  e stack and ther
222e9 65 20 69 73 20 61 6e 20 69 6d 6d 65 64 69 61 74  e is an immediat
222ea 65 20 6a 75 6d 70 0a 2a 2a 20 20 20 20 20 20 20  e jump.**       
222eb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222ec 20 20 20 20 20 20 20 20 20 74 6f 20 6c 61 62 65           to labe
222ed 6c 20 69 67 6e 6f 72 65 44 65 73 74 2e 0a 2a 2a  l ignoreDest..**
222ee 0a 2a 2a 20 20 4e 4f 54 20 4e 55 4c 4c 20 20 20  .**  NOT NULL   
222ef 20 20 20 20 20 20 52 45 50 4c 41 43 45 20 20 20        REPLACE   
222f0 20 20 20 54 68 65 20 4e 55 4c 4c 20 76 61 6c 75     The NULL valu
222f1 65 20 69 73 20 72 65 70 6c 61 63 65 20 62 79 20  e is replace by 
222f2 74 68 65 20 64 65 66 61 75 6c 74 0a 2a 2a 20 20  the default.**  
222f3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 76 61                va
222f5 6c 75 65 20 66 6f 72 20 74 68 61 74 20 63 6f 6c  lue for that col
222f6 75 6d 6e 2e 20 20 49 66 20 74 68 65 20 64 65 66  umn.  If the def
222f7 61 75 6c 74 20 76 61 6c 75 65 0a 2a 2a 20 20 20  ault value.**   
222f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
222f9 20 20 20 20 20 20 20 20 20 20 20 20 20 69 73 20               is 
222fa 4e 55 4c 4c 2c 20 74 68 65 20 61 63 74 69 6f 6e  NULL, the action
222fb 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
222fc 41 42 4f 52 54 2e 0a 2a 2a 0a 2a 2a 20 20 55 4e  ABORT..**.**  UN
222fd 49 51 55 45 20 20 20 20 20 20 20 20 20 20 20 52  IQUE           R
222fe 45 50 4c 41 43 45 20 20 20 20 20 20 54 68 65 20  EPLACE      The 
222ff 6f 74 68 65 72 20 72 6f 77 20 74 68 61 74 20 63  other row that c
22300 6f 6e 66 6c 69 63 74 73 20 77 69 74 68 20 74 68  onflicts with th
22301 65 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20 20 20  e row.**        
22302 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22303 20 20 20 20 20 20 20 20 62 65 69 6e 67 20 69 6e          being in
22304 73 65 72 74 65 64 20 69 73 20 72 65 6d 6f 76 65  serted is remove
22305 64 2e 0a 2a 2a 0a 2a 2a 20 20 43 48 45 43 4b 20  d..**.**  CHECK 
22306 20 20 20 20 20 20 20 20 20 20 20 52 45 50 4c 41             REPLA
22307 43 45 20 20 20 20 20 20 49 6c 6c 65 67 61 6c 2e  CE      Illegal.
22308 20 20 54 68 65 20 72 65 73 75 6c 74 73 20 69 6e    The results in
22309 20 61 6e 20 65 78 63 65 70 74 69 6f 6e 2e 0a 2a   an exception..*
2230a 2a 0a 2a 2a 20 57 68 69 63 68 20 61 63 74 69 6f  *.** Which actio
2230b 6e 20 74 6f 20 74 61 6b 65 20 69 73 20 64 65 74  n to take is det
2230c 65 72 6d 69 6e 65 64 20 62 79 20 74 68 65 20 6f  ermined by the o
2230d 76 65 72 72 69 64 65 45 72 72 6f 72 20 70 61 72  verrideError par
2230e 61 6d 65 74 65 72 2e 0a 2a 2a 20 4f 72 20 69 66  ameter..** Or if
2230f 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 3d 3d   overrideError==
22310 4f 45 5f 44 65 66 61 75 6c 74 2c 20 74 68 65 6e  OE_Default, then
22311 20 74 68 65 20 70 50 61 72 73 65 2d 3e 6f 6e 45   the pParse->onE
22312 72 72 6f 72 20 70 61 72 61 6d 65 74 65 72 0a 2a  rror parameter.*
22313 2a 20 69 73 20 75 73 65 64 2e 20 20 4f 72 20 69  * is used.  Or i
22314 66 20 70 50 61 72 73 65 2d 3e 6f 6e 45 72 72 6f  f pParse->onErro
22315 72 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 74 68  r==OE_Default th
22316 65 6e 20 74 68 65 20 6f 6e 45 72 72 6f 72 20 76  en the onError v
22317 61 6c 75 65 0a 2a 2a 20 66 6f 72 20 74 68 65 20  alue.** for the 
22318 63 6f 6e 73 74 72 61 69 6e 74 20 69 73 20 75 73  constraint is us
22319 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61  ed..**.** The ca
2231a 6c 6c 69 6e 67 20 72 6f 75 74 69 6e 65 20 6d 75  lling routine mu
2231b 73 74 20 6f 70 65 6e 20 61 20 72 65 61 64 2f 77  st open a read/w
2231c 72 69 74 65 20 63 75 72 73 6f 72 20 66 6f 72 20  rite cursor for 
2231d 70 54 61 62 20 77 69 74 68 0a 2a 2a 20 63 75 72  pTab with.** cur
2231e 73 6f 72 20 6e 75 6d 62 65 72 20 22 62 61 73 65  sor number "base
2231f 43 75 72 22 2e 20 20 41 6c 6c 20 69 6e 64 69 63  Cur".  All indic
22320 65 73 20 6f 66 20 70 54 61 62 20 6d 75 73 74 20  es of pTab must 
22321 61 6c 73 6f 20 68 61 76 65 20 6f 70 65 6e 0a 2a  also have open.*
22322 2a 20 72 65 61 64 2f 77 72 69 74 65 20 63 75 72  * read/write cur
22323 73 6f 72 73 20 77 69 74 68 20 63 75 72 73 6f 72  sors with cursor
22324 20 6e 75 6d 62 65 72 20 62 61 73 65 43 75 72 2b   number baseCur+
22325 69 20 66 6f 72 20 74 68 65 20 69 2d 74 68 20 63  i for the i-th c
22326 75 72 73 6f 72 2e 0a 2a 2a 20 45 78 63 65 70 74  ursor..** Except
22327 2c 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  , if there is no
22328 20 70 6f 73 73 69 62 69 6c 69 74 79 20 6f 66 20   possibility of 
22329 61 20 52 45 50 4c 41 43 45 20 61 63 74 69 6f 6e  a REPLACE action
2232a 20 74 68 65 6e 0a 2a 2a 20 63 75 72 73 6f 72 73   then.** cursors
2232b 20 64 6f 20 6e 6f 74 20 6e 65 65 64 20 74 6f 20   do not need to 
2232c 62 65 20 6f 70 65 6e 20 66 6f 72 20 69 6e 64 69  be open for indi
2232d 63 65 73 20 77 68 65 72 65 20 61 52 65 67 49 64  ces where aRegId
2232e 78 5b 69 5d 3d 3d 30 2e 0a 2a 2f 0a 53 51 4c 49  x[i]==0..*/.SQLI
2232f 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20  TE_PRIVATE void 
22330 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65 43  sqlite3GenerateC
22331 6f 6e 73 74 72 61 69 6e 74 43 68 65 63 6b 73 28  onstraintChecks(
22332 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65  .  Parse *pParse
22333 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61  ,      /* The pa
22334 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  rser context */.
22335 20 20 54 61 62 6c 65 20 2a 70 54 61 62 2c 20 20    Table *pTab,  
22336 20 20 20 20 20 20 2f 2a 20 74 68 65 20 74 61 62        /* the tab
22337 6c 65 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65  le into which we
22338 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20 2a   are inserting *
22339 2f 0a 20 20 69 6e 74 20 62 61 73 65 43 75 72 2c  /.  int baseCur,
2233a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2233b 20 6f 66 20 61 20 72 65 61 64 2f 77 72 69 74 65   of a read/write
2233c 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 69 6e 67   cursor pointing
2233d 20 61 74 20 70 54 61 62 20 2a 2f 0a 20 20 69 6e   at pTab */.  in
2233e 74 20 72 65 67 52 6f 77 69 64 2c 20 20 20 20 20  t regRowid,     
2233f 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68    /* Index of th
22340 65 20 72 61 6e 67 65 20 6f 66 20 69 6e 70 75 74  e range of input
22341 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a 20 20   registers */.  
22342 69 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20 20  int *aRegIdx,   
22343 20 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20      /* Register 
22344 75 73 65 64 20 62 79 20 65 61 63 68 20 69 6e 64  used by each ind
22345 65 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73 65  ex.  0 for unuse
22346 64 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69  d indices */.  i
22347 6e 74 20 72 6f 77 69 64 43 68 6e 67 2c 20 20 20  nt rowidChng,   
22348 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 74 68     /* True if th
22349 65 20 72 6f 77 69 64 20 6d 69 67 68 74 20 63 6f  e rowid might co
2234a 6c 6c 69 64 65 20 77 69 74 68 20 65 78 69 73 74  llide with exist
2234b 69 6e 67 20 65 6e 74 72 79 20 2a 2f 0a 20 20 69  ing entry */.  i
2234c 6e 74 20 69 73 55 70 64 61 74 65 2c 20 20 20 20  nt isUpdate,    
2234d 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55     /* True for U
2234e 50 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f 72  PDATE, False for
2234f 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74   INSERT */.  int
22350 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 2c 20   overrideError, 
22351 20 2f 2a 20 4f 76 65 72 72 69 64 65 20 6f 6e 45   /* Override onE
22352 72 72 6f 72 20 74 6f 20 74 68 69 73 20 69 66 20  rror to this if 
22353 6e 6f 74 20 4f 45 5f 44 65 66 61 75 6c 74 20 2a  not OE_Default *
22354 2f 0a 20 20 69 6e 74 20 69 67 6e 6f 72 65 44 65  /.  int ignoreDe
22355 73 74 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20  st      /* Jump 
22356 74 6f 20 74 68 69 73 20 6c 61 62 65 6c 20 6f 6e  to this label on
22357 20 61 6e 20 4f 45 5f 49 67 6e 6f 72 65 20 72 65   an OE_Ignore re
22358 73 6f 6c 75 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  solution */.){. 
22359 20 69 6e 74 20 69 3b 0a 20 20 56 64 62 65 20 2a   int i;.  Vdbe *
2235a 76 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c 3b 0a 20  v;.  int nCol;. 
2235b 20 69 6e 74 20 6f 6e 45 72 72 6f 72 3b 0a 20 20   int onError;.  
2235c 69 6e 74 20 6a 31 2c 20 6a 32 2c 20 6a 33 3b 20  int j1, j2, j3; 
2235d 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73      /* Addresses
2235e 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72 75 63   of jump instruc
2235f 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 72  tions */.  int r
22360 65 67 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f  egData;        /
22361 2a 20 52 65 67 69 73 74 65 72 20 63 6f 6e 74 61  * Register conta
22362 69 6e 69 6e 67 20 66 69 72 73 74 20 64 61 74 61  ining first data
22363 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 69 6e 74   column */.  int
22364 20 69 43 75 72 3b 0a 20 20 49 6e 64 65 78 20 2a   iCur;.  Index *
22365 70 49 64 78 3b 0a 20 20 69 6e 74 20 73 65 65 6e  pIdx;.  int seen
22366 52 65 70 6c 61 63 65 20 3d 20 30 3b 0a 20 20 69  Replace = 0;.  i
22367 6e 74 20 68 61 73 54 77 6f 52 6f 77 69 64 73 20  nt hasTwoRowids 
22368 3d 20 28 69 73 55 70 64 61 74 65 20 26 26 20 72  = (isUpdate && r
22369 6f 77 69 64 43 68 6e 67 29 3b 0a 0a 20 20 76 20  owidChng);..  v 
2236a 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65  = sqlite3GetVdbe
2236b 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73 65  (pParse);.  asse
2236c 72 74 28 20 76 21 3d 30 20 29 3b 0a 20 20 61 73  rt( v!=0 );.  as
2236d 73 65 72 74 28 20 70 54 61 62 2d 3e 70 53 65 6c  sert( pTab->pSel
2236e 65 63 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 54 68  ect==0 );  /* Th
2236f 69 73 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  is table is not 
22370 61 20 56 49 45 57 20 2a 2f 0a 20 20 6e 43 6f 6c  a VIEW */.  nCol
22371 20 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 3b 0a 20   = pTab->nCol;. 
22372 20 72 65 67 44 61 74 61 20 3d 20 72 65 67 52 6f   regData = regRo
22373 77 69 64 20 2b 20 31 3b 0a 0a 0a 20 20 2f 2a 20  wid + 1;...  /* 
22374 54 65 73 74 20 61 6c 6c 20 4e 4f 54 20 4e 55 4c  Test all NOT NUL
22375 4c 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a 20  L constraints.. 
22376 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69   */.  for(i=0; i
22377 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  <nCol; i++){.   
22378 20 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50   if( i==pTab->iP
22379 4b 65 79 20 29 7b 0a 20 20 20 20 20 20 63 6f 6e  Key ){.      con
2237a 74 69 6e 75 65 3b 0a 20 20 20 20 7d 0a 20 20 20  tinue;.    }.   
2237b 20 6f 6e 45 72 72 6f 72 20 3d 20 70 54 61 62 2d   onError = pTab-
2237c 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c  >aCol[i].notNull
2237d 3b 0a 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f  ;.    if( onErro
2237e 72 3d 3d 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e  r==OE_None ) con
2237f 74 69 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 6f  tinue;.    if( o
22380 76 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45  verrideError!=OE
22381 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20  _Default ){.    
22382 20 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72    onError = over
22383 72 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d  rideError;.    }
22384 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72  else if( onError
22385 3d 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a  ==OE_Default ){.
22386 20 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20        onError = 
22387 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a  OE_Abort;.    }.
22388 20 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d      if( onError=
22389 3d 4f 45 5f 52 65 70 6c 61 63 65 20 26 26 20 70  =OE_Replace && p
2238a 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70 44 66  Tab->aCol[i].pDf
2238b 6c 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 6f  lt==0 ){.      o
2238c 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72  nError = OE_Abor
2238d 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6a 31 20  t;.    }.    j1 
2238e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
2238f 4f 70 31 28 76 2c 20 4f 50 5f 4e 6f 74 4e 75 6c  Op1(v, OP_NotNul
22390 6c 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20  l, regData+i);. 
22391 20 20 20 61 73 73 65 72 74 28 20 6f 6e 45 72 72     assert( onErr
22392 6f 72 3d 3d 4f 45 5f 52 6f 6c 6c 62 61 63 6b 20  or==OE_Rollback 
22393 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 41  || onError==OE_A
22394 62 6f 72 74 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d  bort || onError=
22395 3d 4f 45 5f 46 61 69 6c 0a 20 20 20 20 20 20 20  =OE_Fail.       
22396 20 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f   || onError==OE_
22397 49 67 6e 6f 72 65 20 7c 7c 20 6f 6e 45 72 72 6f  Ignore || onErro
22398 72 3d 3d 4f 45 5f 52 65 70 6c 61 63 65 20 29 3b  r==OE_Replace );
22399 0a 20 20 20 20 73 77 69 74 63 68 28 20 6f 6e 45  .    switch( onE
2239a 72 72 6f 72 20 29 7b 0a 20 20 20 20 20 20 63 61  rror ){.      ca
2239b 73 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a  se OE_Rollback:.
2239c 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41 62        case OE_Ab
2239d 6f 72 74 3a 0a 20 20 20 20 20 20 63 61 73 65 20  ort:.      case 
2239e 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20 20 20 20  OE_Fail: {.     
2239f 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20     char *zMsg;. 
223a0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
223a1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
223a2 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53  alt, SQLITE_CONS
223a3 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 29  TRAINT, onError)
223a4 3b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  ;.        zMsg =
223a5 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
223a6 70 50 61 72 73 65 2d 3e 64 62 2c 20 22 25 73 2e  pParse->db, "%s.
223a7 25 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 4e 55  %s may not be NU
223a8 4c 4c 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  LL",.           
223a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
223aa 20 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20     pTab->zName, 
223ab 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e  pTab->aCol[i].zN
223ac 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  ame);.        sq
223ad 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
223ae 34 28 76 2c 20 2d 31 2c 20 7a 4d 73 67 2c 20 50  4(v, -1, zMsg, P
223af 34 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  4_DYNAMIC);.    
223b0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
223b1 20 7d 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45   }.      case OE
223b2 5f 49 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20  _Ignore: {.     
223b3 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
223b4 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
223b5 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b   0, ignoreDest);
223b6 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
223b7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
223b8 73 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b  se OE_Replace: {
223b9 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
223ba 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
223bb 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 70   pTab->aCol[i].p
223bc 44 66 6c 74 2c 20 72 65 67 44 61 74 61 2b 69 29  Dflt, regData+i)
223bd 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  ;.        break;
223be 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
223bf 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
223c0 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20  mpHere(v, j1);. 
223c1 20 7d 0a 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c   }..  /* Test al
223c2 6c 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61 69  l CHECK constrai
223c3 6e 74 73 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  nts.  */.#ifndef
223c4 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
223c5 43 4b 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 70  CK.  if( pTab->p
223c6 43 68 65 63 6b 20 26 26 20 28 70 50 61 72 73 65  Check && (pParse
223c7 2d 3e 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51  ->db->flags & SQ
223c8 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65 63 6b  LITE_IgnoreCheck
223c9 73 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  s)==0 ){.    int
223ca 20 61 6c 6c 4f 6b 20 3d 20 73 71 6c 69 74 65 33   allOk = sqlite3
223cb 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29  VdbeMakeLabel(v)
223cc 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 63 6b  ;.    pParse->ck
223cd 42 61 73 65 20 3d 20 72 65 67 44 61 74 61 3b 0a  Base = regData;.
223ce 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
223cf 66 54 72 75 65 28 70 50 61 72 73 65 2c 20 70 54  fTrue(pParse, pT
223d0 61 62 2d 3e 70 43 68 65 63 6b 2c 20 61 6c 6c 4f  ab->pCheck, allO
223d1 6b 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  k, SQLITE_JUMPIF
223d2 4e 55 4c 4c 29 3b 0a 20 20 20 20 6f 6e 45 72 72  NULL);.    onErr
223d3 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72 72  or = overrideErr
223d4 6f 72 21 3d 4f 45 5f 44 65 66 61 75 6c 74 20 3f  or!=OE_Default ?
223d5 20 6f 76 65 72 72 69 64 65 45 72 72 6f 72 20 3a   overrideError :
223d6 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 69   OE_Abort;.    i
223d7 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49  f( onError==OE_I
223d8 67 6e 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 73  gnore ){.      s
223d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
223da 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
223db 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20 20 20  ignoreDest);.   
223dc 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
223dd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
223de 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53 51 4c 49  v, OP_Halt, SQLI
223df 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54 2c 20 6f  TE_CONSTRAINT, o
223e0 6e 45 72 72 6f 72 29 3b 0a 20 20 20 20 7d 0a 20  nError);.    }. 
223e1 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
223e2 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 6c  solveLabel(v, al
223e3 6c 4f 6b 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  lOk);.  }.#endif
223e4 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
223e5 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 29 20  ITE_OMIT_CHECK) 
223e6 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 68  */..  /* If we h
223e7 61 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50  ave an INTEGER P
223e8 52 49 4d 41 52 59 20 4b 45 59 2c 20 6d 61 6b 65  RIMARY KEY, make
223e9 20 73 75 72 65 20 74 68 65 20 70 72 69 6d 61 72   sure the primar
223ea 79 20 6b 65 79 0a 20 20 2a 2a 20 6f 66 20 74 68  y key.  ** of th
223eb 65 20 6e 65 77 20 72 65 63 6f 72 64 20 64 6f 65  e new record doe
223ec 73 20 6e 6f 74 20 70 72 65 76 69 6f 75 73 6c 79  s not previously
223ed 20 65 78 69 73 74 2e 20 20 45 78 63 65 70 74 2c   exist.  Except,
223ee 20 69 66 20 74 68 69 73 0a 20 20 2a 2a 20 69 73   if this.  ** is
223ef 20 61 6e 20 55 50 44 41 54 45 20 61 6e 64 20 74   an UPDATE and t
223f0 68 65 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69  he primary key i
223f1 73 20 6e 6f 74 20 63 68 61 6e 67 69 6e 67 2c 20  s not changing, 
223f2 74 68 61 74 20 69 73 20 4f 4b 2e 0a 20 20 2a 2f  that is OK..  */
223f3 0a 20 20 69 66 28 20 72 6f 77 69 64 43 68 6e 67  .  if( rowidChng
223f4 20 29 7b 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20   ){.    onError 
223f5 3d 20 70 54 61 62 2d 3e 6b 65 79 43 6f 6e 66 3b  = pTab->keyConf;
223f6 0a 20 20 20 20 69 66 28 20 6f 76 65 72 72 69 64  .    if( overrid
223f7 65 45 72 72 6f 72 21 3d 4f 45 5f 44 65 66 61 75  eError!=OE_Defau
223f8 6c 74 20 29 7b 0a 20 20 20 20 20 20 6f 6e 45 72  lt ){.      onEr
223f9 72 6f 72 20 3d 20 6f 76 65 72 72 69 64 65 45 72  ror = overrideEr
223fa 72 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ror;.    }else i
223fb 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 44  f( onError==OE_D
223fc 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20 20  efault ){.      
223fd 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f  onError = OE_Abo
223fe 72 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 0a 20  rt;.    }.    . 
223ff 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 21 3d     if( onError!=
22400 4f 45 5f 52 65 70 6c 61 63 65 20 7c 7c 20 70 54  OE_Replace || pT
22401 61 62 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20  ab->pIndex ){.  
22402 20 20 20 20 69 66 28 20 69 73 55 70 64 61 74 65      if( isUpdate
22403 20 29 7b 0a 20 20 20 20 20 20 20 20 6a 32 20 3d   ){.        j2 =
22404 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22405 70 33 28 76 2c 20 4f 50 5f 45 71 2c 20 72 65 67  p3(v, OP_Eq, reg
22406 52 6f 77 69 64 2c 20 30 2c 20 72 65 67 52 6f 77  Rowid, 0, regRow
22407 69 64 2d 31 29 3b 0a 20 20 20 20 20 20 7d 0a 20  id-1);.      }. 
22408 20 20 20 20 20 6a 33 20 3d 20 73 71 6c 69 74 65       j3 = sqlite
22409 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2240a 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 62 61 73  P_NotExists, bas
2240b 65 43 75 72 2c 20 30 2c 20 72 65 67 52 6f 77 69  eCur, 0, regRowi
2240c 64 29 3b 0a 20 20 20 20 20 20 73 77 69 74 63 68  d);.      switch
2240d 28 20 6f 6e 45 72 72 6f 72 20 29 7b 0a 20 20 20  ( onError ){.   
2240e 20 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a       default: {.
2240f 20 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f            onErro
22410 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b 0a 20 20  r = OE_Abort;.  
22411 20 20 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 20          /* Fall 
22412 74 68 72 75 20 69 6e 74 6f 20 74 68 65 20 6e 65  thru into the ne
22413 78 74 20 63 61 73 65 20 2a 2f 0a 20 20 20 20 20  xt case */.     
22414 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73     }.        cas
22415 65 20 4f 45 5f 52 6f 6c 6c 62 61 63 6b 3a 0a 20  e OE_Rollback:. 
22416 20 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 41         case OE_A
22417 62 6f 72 74 3a 0a 20 20 20 20 20 20 20 20 63 61  bort:.        ca
22418 73 65 20 4f 45 5f 46 61 69 6c 3a 20 7b 0a 20 20  se OE_Fail: {.  
22419 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2241a 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
2241b 48 61 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e  Halt, SQLITE_CON
2241c 53 54 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72  STRAINT, onError
2241d 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
2241e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2241f 22 50 52 49 4d 41 52 59 20 4b 45 59 20 6d 75 73  "PRIMARY KEY mus
22420 74 20 62 65 20 75 6e 69 71 75 65 22 2c 20 50 34  t be unique", P4
22421 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
22422 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
22423 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63 61 73     }.        cas
22424 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b 0a  e OE_Replace: {.
22425 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22426 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
22427 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  xDelete(pParse, 
22428 70 54 61 62 2c 20 62 61 73 65 43 75 72 2c 20 30  pTab, baseCur, 0
22429 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 65 65  );.          see
2242a 6e 52 65 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20  nReplace = 1;.  
2242b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2242c 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2242d 20 63 61 73 65 20 4f 45 5f 49 67 6e 6f 72 65 3a   case OE_Ignore:
2242e 20 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73   {.          ass
2242f 65 72 74 28 20 73 65 65 6e 52 65 70 6c 61 63 65  ert( seenReplace
22430 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20  ==0 );.         
22431 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22432 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30  p2(v, OP_Goto, 0
22433 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b 0a 20  , ignoreDest);. 
22434 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
22435 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22436 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  }.      sqlite3V
22437 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a  dbeJumpHere(v, j
22438 33 29 3b 0a 20 20 20 20 20 20 69 66 28 20 69 73  3);.      if( is
22439 55 70 64 61 74 65 20 29 7b 0a 20 20 20 20 20 20  Update ){.      
2243a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
2243b 70 48 65 72 65 28 76 2c 20 6a 32 29 3b 0a 20 20  pHere(v, j2);.  
2243c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2243d 0a 20 20 2f 2a 20 54 65 73 74 20 61 6c 6c 20 55  .  /* Test all U
2243e 4e 49 51 55 45 20 63 6f 6e 73 74 72 61 69 6e 74  NIQUE constraint
2243f 73 20 62 79 20 63 72 65 61 74 69 6e 67 20 65 6e  s by creating en
22440 74 72 69 65 73 20 66 6f 72 20 65 61 63 68 20 55  tries for each U
22441 4e 49 51 55 45 0a 20 20 2a 2a 20 69 6e 64 65 78  NIQUE.  ** index
22442 20 61 6e 64 20 6d 61 6b 69 6e 67 20 73 75 72 65   and making sure
22443 20 74 68 61 74 20 64 75 70 6c 69 63 61 74 65 20   that duplicate 
22444 65 6e 74 72 69 65 73 20 64 6f 20 6e 6f 74 20 61  entries do not a
22445 6c 72 65 61 64 79 20 65 78 69 73 74 2e 0a 20 20  lready exist..  
22446 2a 2a 20 41 64 64 20 74 68 65 20 6e 65 77 20 72  ** Add the new r
22447 65 63 6f 72 64 73 20 74 6f 20 74 68 65 20 69 6e  ecords to the in
22448 64 69 63 65 73 20 61 73 20 77 65 20 67 6f 2e 0a  dices as we go..
22449 20 20 2a 2f 0a 20 20 66 6f 72 28 69 43 75 72 3d    */.  for(iCur=
2244a 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
2244b 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
2244c 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 69 43  =pIdx->pNext, iC
2244d 75 72 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 72  ur++){.    int r
2244e 65 67 49 64 78 3b 0a 20 20 20 20 69 6e 74 20 72  egIdx;.    int r
2244f 65 67 52 3b 0a 0a 20 20 20 20 69 66 28 20 61 52  egR;..    if( aR
22450 65 67 49 64 78 5b 69 43 75 72 5d 3d 3d 30 20 29  egIdx[iCur]==0 )
22451 20 63 6f 6e 74 69 6e 75 65 3b 20 20 2f 2a 20 53   continue;  /* S
22452 6b 69 70 20 75 6e 75 73 65 64 20 69 6e 64 69 63  kip unused indic
22453 65 73 20 2a 2f 0a 0a 20 20 20 20 2f 2a 20 43 72  es */..    /* Cr
22454 65 61 74 65 20 61 20 6b 65 79 20 66 6f 72 20 61  eate a key for a
22455 63 63 65 73 73 69 6e 67 20 74 68 65 20 69 6e 64  ccessing the ind
22456 65 78 20 65 6e 74 72 79 20 2a 2f 0a 20 20 20 20  ex entry */.    
22457 72 65 67 49 64 78 20 3d 20 73 71 6c 69 74 65 33  regIdx = sqlite3
22458 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61  GetTempRange(pPa
22459 72 73 65 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75  rse, pIdx->nColu
2245a 6d 6e 2b 31 29 3b 0a 20 20 20 20 66 6f 72 28 69  mn+1);.    for(i
2245b 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c  =0; i<pIdx->nCol
2245c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
2245d 20 69 6e 74 20 69 64 78 20 3d 20 70 49 64 78 2d   int idx = pIdx-
2245e 3e 61 69 43 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20  >aiColumn[i];.  
2245f 20 20 20 20 69 66 28 20 69 64 78 3d 3d 70 54 61      if( idx==pTa
22460 62 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20  b->iPKey ){.    
22461 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
22462 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
22463 79 2c 20 72 65 67 52 6f 77 69 64 2c 20 72 65 67  y, regRowid, reg
22464 49 64 78 2b 69 29 3b 0a 20 20 20 20 20 20 7d 65  Idx+i);.      }e
22465 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  lse{.        sql
22466 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
22467 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 65 67 44  , OP_SCopy, regD
22468 61 74 61 2b 69 64 78 2c 20 72 65 67 49 64 78 2b  ata+idx, regIdx+
22469 69 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  i);.      }.    
2246a 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
2246b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43  eAddOp2(v, OP_SC
2246c 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2c 20 72  opy, regRowid, r
2246d 65 67 49 64 78 2b 69 29 3b 0a 20 20 20 20 73 71  egIdx+i);.    sq
2246e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2246f 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
22470 2c 20 72 65 67 49 64 78 2c 20 70 49 64 78 2d 3e  , regIdx, pIdx->
22471 6e 43 6f 6c 75 6d 6e 2b 31 2c 20 61 52 65 67 49  nColumn+1, aRegI
22472 64 78 5b 69 43 75 72 5d 29 3b 0a 20 20 20 20 73  dx[iCur]);.    s
22473 71 6c 69 74 65 33 49 6e 64 65 78 41 66 66 69 6e  qlite3IndexAffin
22474 69 74 79 53 74 72 28 76 2c 20 70 49 64 78 29 3b  ityStr(v, pIdx);
22475 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
22476 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
22477 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 49  nge(pParse, regI
22478 64 78 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  dx, pIdx->nColum
22479 6e 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  n+1);.    sqlite
2247a 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67  3ReleaseTempRang
2247b 65 28 70 50 61 72 73 65 2c 20 72 65 67 49 64 78  e(pParse, regIdx
2247c 2c 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b  , pIdx->nColumn+
2247d 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 69 6e 64  1);..    /* Find
2247e 20 6f 75 74 20 77 68 61 74 20 61 63 74 69 6f 6e   out what action
2247f 20 74 6f 20 74 61 6b 65 20 69 6e 20 63 61 73 65   to take in case
22480 20 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e 64   there is an ind
22481 65 78 69 6e 67 20 63 6f 6e 66 6c 69 63 74 20 2a  exing conflict *
22482 2f 0a 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20  /.    onError = 
22483 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3b 0a 20  pIdx->onError;. 
22484 20 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d     if( onError==
22485 4f 45 5f 4e 6f 6e 65 20 29 20 63 6f 6e 74 69 6e  OE_None ) contin
22486 75 65 3b 20 20 2f 2a 20 70 49 64 78 20 69 73 20  ue;  /* pIdx is 
22487 6e 6f 74 20 61 20 55 4e 49 51 55 45 20 69 6e 64  not a UNIQUE ind
22488 65 78 20 2a 2f 0a 20 20 20 20 69 66 28 20 6f 76  ex */.    if( ov
22489 65 72 72 69 64 65 45 72 72 6f 72 21 3d 4f 45 5f  errideError!=OE_
2248a 44 65 66 61 75 6c 74 20 29 7b 0a 20 20 20 20 20  Default ){.     
2248b 20 6f 6e 45 72 72 6f 72 20 3d 20 6f 76 65 72 72   onError = overr
2248c 69 64 65 45 72 72 6f 72 3b 0a 20 20 20 20 7d 65  ideError;.    }e
2248d 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d  lse if( onError=
2248e 3d 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20  =OE_Default ){. 
2248f 20 20 20 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f       onError = O
22490 45 5f 41 62 6f 72 74 3b 0a 20 20 20 20 7d 0a 20  E_Abort;.    }. 
22491 20 20 20 69 66 28 20 73 65 65 6e 52 65 70 6c 61     if( seenRepla
22492 63 65 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20  ce ){.      if( 
22493 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f  onError==OE_Igno
22494 72 65 20 29 20 6f 6e 45 72 72 6f 72 20 3d 20 4f  re ) onError = O
22495 45 5f 52 65 70 6c 61 63 65 3b 0a 20 20 20 20 20  E_Replace;.     
22496 20 65 6c 73 65 20 69 66 28 20 6f 6e 45 72 72 6f   else if( onErro
22497 72 3d 3d 4f 45 5f 46 61 69 6c 20 29 20 6f 6e 45  r==OE_Fail ) onE
22498 72 72 6f 72 20 3d 20 4f 45 5f 41 62 6f 72 74 3b  rror = OE_Abort;
22499 0a 20 20 20 20 7d 0a 20 20 20 20 0a 0a 20 20 20  .    }.    ..   
2249a 20 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65   /* Check to see
2249b 20 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65   if the new inde
2249c 78 20 65 6e 74 72 79 20 77 69 6c 6c 20 62 65 20  x entry will be 
2249d 75 6e 69 71 75 65 20 2a 2f 0a 20 20 20 20 6a 32  unique */.    j2
2249e 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64   = sqlite3VdbeAd
2249f 64 4f 70 33 28 76 2c 20 4f 50 5f 49 73 4e 75 6c  dOp3(v, OP_IsNul
224a0 6c 2c 20 72 65 67 49 64 78 2c 20 30 2c 20 70 49  l, regIdx, 0, pI
224a1 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  dx->nColumn);.  
224a2 20 20 72 65 67 52 20 3d 20 73 71 6c 69 74 65 33    regR = sqlite3
224a3 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
224a4 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  e);.    sqlite3V
224a5 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
224a6 53 43 6f 70 79 2c 20 72 65 67 52 6f 77 69 64 2d  SCopy, regRowid-
224a7 68 61 73 54 77 6f 52 6f 77 69 64 73 2c 20 72 65  hasTwoRowids, re
224a8 67 52 29 3b 0a 20 20 20 20 6a 33 20 3d 20 73 71  gR);.    j3 = sq
224a9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
224aa 76 2c 20 4f 50 5f 49 73 55 6e 69 71 75 65 2c 20  v, OP_IsUnique, 
224ab 62 61 73 65 43 75 72 2b 69 43 75 72 2b 31 2c 20  baseCur+iCur+1, 
224ac 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
224ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 72 65                re
224ae 67 52 2c 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54  gR, SQLITE_INT_T
224af 4f 5f 50 54 52 28 61 52 65 67 49 64 78 5b 69 43  O_PTR(aRegIdx[iC
224b0 75 72 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20  ur]),.          
224b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
224b2 20 50 34 5f 49 4e 54 33 32 29 3b 0a 0a 20 20 20   P4_INT32);..   
224b3 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64   /* Generate cod
224b4 65 20 74 68 61 74 20 65 78 65 63 75 74 65 73 20  e that executes 
224b5 69 66 20 74 68 65 20 6e 65 77 20 69 6e 64 65 78  if the new index
224b6 20 65 6e 74 72 79 20 69 73 20 6e 6f 74 20 75 6e   entry is not un
224b7 69 71 75 65 20 2a 2f 0a 20 20 20 20 61 73 73 65  ique */.    asse
224b8 72 74 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f  rt( onError==OE_
224b9 52 6f 6c 6c 62 61 63 6b 20 7c 7c 20 6f 6e 45 72  Rollback || onEr
224ba 72 6f 72 3d 3d 4f 45 5f 41 62 6f 72 74 20 7c 7c  ror==OE_Abort ||
224bb 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 46 61 69   onError==OE_Fai
224bc 6c 0a 20 20 20 20 20 20 20 20 7c 7c 20 6f 6e 45  l.        || onE
224bd 72 72 6f 72 3d 3d 4f 45 5f 49 67 6e 6f 72 65 20  rror==OE_Ignore 
224be 7c 7c 20 6f 6e 45 72 72 6f 72 3d 3d 4f 45 5f 52  || onError==OE_R
224bf 65 70 6c 61 63 65 20 29 3b 0a 20 20 20 20 73 77  eplace );.    sw
224c0 69 74 63 68 28 20 6f 6e 45 72 72 6f 72 20 29 7b  itch( onError ){
224c1 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 52  .      case OE_R
224c2 6f 6c 6c 62 61 63 6b 3a 0a 20 20 20 20 20 20 63  ollback:.      c
224c3 61 73 65 20 4f 45 5f 41 62 6f 72 74 3a 0a 20 20  ase OE_Abort:.  
224c4 20 20 20 20 63 61 73 65 20 4f 45 5f 46 61 69 6c      case OE_Fail
224c5 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  : {.        int 
224c6 6a 2c 20 6e 31 2c 20 6e 32 3b 0a 20 20 20 20 20  j, n1, n2;.     
224c7 20 20 20 63 68 61 72 20 7a 45 72 72 4d 73 67 5b     char zErrMsg[
224c8 32 30 30 5d 3b 0a 20 20 20 20 20 20 20 20 73 71  200];.        sq
224c9 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 41  lite3_snprintf(A
224ca 72 72 61 79 53 69 7a 65 28 7a 45 72 72 4d 73 67  rraySize(zErrMsg
224cb 29 2c 20 7a 45 72 72 4d 73 67 2c 0a 20 20 20 20  ), zErrMsg,.    
224cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
224cd 20 20 20 20 20 70 49 64 78 2d 3e 6e 43 6f 6c 75       pIdx->nColu
224ce 6d 6e 3e 31 20 3f 20 22 63 6f 6c 75 6d 6e 73 20  mn>1 ? "columns 
224cf 22 20 3a 20 22 63 6f 6c 75 6d 6e 20 22 29 3b 0a  " : "column ");.
224d0 20 20 20 20 20 20 20 20 6e 31 20 3d 20 73 74 72          n1 = str
224d1 6c 65 6e 28 7a 45 72 72 4d 73 67 29 3b 0a 20 20  len(zErrMsg);.  
224d2 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
224d3 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 20 26  <pIdx->nColumn &
224d4 26 20 6e 31 3c 41 72 72 61 79 53 69 7a 65 28 7a  & n1<ArraySize(z
224d5 45 72 72 4d 73 67 29 2d 33 30 3b 20 6a 2b 2b 29  ErrMsg)-30; j++)
224d6 7b 0a 20 20 20 20 20 20 20 20 20 20 63 68 61 72  {.          char
224d7 20 2a 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61   *zCol = pTab->a
224d8 43 6f 6c 5b 70 49 64 78 2d 3e 61 69 43 6f 6c 75  Col[pIdx->aiColu
224d9 6d 6e 5b 6a 5d 5d 2e 7a 4e 61 6d 65 3b 0a 20 20  mn[j]].zName;.  
224da 20 20 20 20 20 20 20 20 6e 32 20 3d 20 73 74 72          n2 = str
224db 6c 65 6e 28 7a 43 6f 6c 29 3b 0a 20 20 20 20 20  len(zCol);.     
224dc 20 20 20 20 20 69 66 28 20 6a 3e 30 20 29 7b 0a       if( j>0 ){.
224dd 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
224de 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 41 72 72  te3_snprintf(Arr
224df 61 79 53 69 7a 65 28 7a 45 72 72 4d 73 67 29 2d  aySize(zErrMsg)-
224e0 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e 31 5d  n1, &zErrMsg[n1]
224e1 2c 20 22 2c 20 22 29 3b 0a 20 20 20 20 20 20 20  , ", ");.       
224e2 20 20 20 20 20 6e 31 20 2b 3d 20 32 3b 0a 20 20       n1 += 2;.  
224e3 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
224e4 20 20 20 20 69 66 28 20 6e 31 2b 6e 32 3e 41 72      if( n1+n2>Ar
224e5 72 61 79 53 69 7a 65 28 7a 45 72 72 4d 73 67 29  raySize(zErrMsg)
224e6 2d 33 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  -30 ){.         
224e7 20 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69     sqlite3_snpri
224e8 6e 74 66 28 41 72 72 61 79 53 69 7a 65 28 7a 45  ntf(ArraySize(zE
224e9 72 72 4d 73 67 29 2d 6e 31 2c 20 26 7a 45 72 72  rrMsg)-n1, &zErr
224ea 4d 73 67 5b 6e 31 5d 2c 20 22 2e 2e 2e 22 29 3b  Msg[n1], "...");
224eb 0a 20 20 20 20 20 20 20 20 20 20 20 20 6e 31 20  .            n1 
224ec 2b 3d 20 33 3b 0a 20 20 20 20 20 20 20 20 20 20  += 3;.          
224ed 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
224ee 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
224ef 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 6e        sqlite3_sn
224f0 70 72 69 6e 74 66 28 41 72 72 61 79 53 69 7a 65  printf(ArraySize
224f1 28 7a 45 72 72 4d 73 67 29 2d 6e 31 2c 20 26 7a  (zErrMsg)-n1, &z
224f2 45 72 72 4d 73 67 5b 6e 31 5d 2c 20 22 25 73 22  ErrMsg[n1], "%s"
224f3 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20  , zCol);.       
224f4 20 20 20 20 20 6e 31 20 2b 3d 20 6e 32 3b 0a 20       n1 += n2;. 
224f5 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
224f6 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73 71 6c     }.        sql
224f7 69 74 65 33 5f 73 6e 70 72 69 6e 74 66 28 41 72  ite3_snprintf(Ar
224f8 72 61 79 53 69 7a 65 28 7a 45 72 72 4d 73 67 29  raySize(zErrMsg)
224f9 2d 6e 31 2c 20 26 7a 45 72 72 4d 73 67 5b 6e 31  -n1, &zErrMsg[n1
224fa 5d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  ], .            
224fb 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 31 20  pIdx->nColumn>1 
224fc 3f 20 22 20 61 72 65 20 6e 6f 74 20 75 6e 69 71  ? " are not uniq
224fd 75 65 22 20 3a 20 22 20 69 73 20 6e 6f 74 20 75  ue" : " is not u
224fe 6e 69 71 75 65 22 29 3b 0a 20 20 20 20 20 20 20  nique");.       
224ff 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22500 70 34 28 76 2c 20 4f 50 5f 48 61 6c 74 2c 20 53  p4(v, OP_Halt, S
22501 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49 4e 54  QLITE_CONSTRAINT
22502 2c 20 6f 6e 45 72 72 6f 72 2c 20 30 2c 20 7a 45  , onError, 0, zE
22503 72 72 4d 73 67 2c 30 29 3b 0a 20 20 20 20 20 20  rrMsg,0);.      
22504 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
22505 0a 20 20 20 20 20 20 63 61 73 65 20 4f 45 5f 49  .      case OE_I
22506 67 6e 6f 72 65 3a 20 7b 0a 20 20 20 20 20 20 20  gnore: {.       
22507 20 61 73 73 65 72 74 28 20 73 65 65 6e 52 65 70   assert( seenRep
22508 6c 61 63 65 3d 3d 30 20 29 3b 0a 20 20 20 20 20  lace==0 );.     
22509 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2250a 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
2250b 20 30 2c 20 69 67 6e 6f 72 65 44 65 73 74 29 3b   0, ignoreDest);
2250c 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2250d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61        }.      ca
2250e 73 65 20 4f 45 5f 52 65 70 6c 61 63 65 3a 20 7b  se OE_Replace: {
2250f 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
22510 47 65 6e 65 72 61 74 65 52 6f 77 44 65 6c 65 74  GenerateRowDelet
22511 65 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c 20  e(pParse, pTab, 
22512 62 61 73 65 43 75 72 2c 20 72 65 67 52 2c 20 30  baseCur, regR, 0
22513 29 3b 0a 20 20 20 20 20 20 20 20 73 65 65 6e 52  );.        seenR
22514 65 70 6c 61 63 65 20 3d 20 31 3b 0a 20 20 20 20  eplace = 1;.    
22515 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
22516 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
22517 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
22518 28 76 2c 20 6a 32 29 3b 0a 20 20 20 20 73 71 6c  (v, j2);.    sql
22519 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65  ite3VdbeJumpHere
2251a 28 76 2c 20 6a 33 29 3b 0a 20 20 20 20 73 71 6c  (v, j3);.    sql
2251b 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
2251c 65 67 28 70 50 61 72 73 65 2c 20 72 65 67 52 29  eg(pParse, regR)
2251d 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2251e 68 69 73 20 72 6f 75 74 69 6e 65 20 67 65 6e 65  his routine gene
2251f 72 61 74 65 73 20 63 6f 64 65 20 74 6f 20 66 69  rates code to fi
22520 6e 69 73 68 20 74 68 65 20 49 4e 53 45 52 54 20  nish the INSERT 
22521 6f 72 20 55 50 44 41 54 45 20 6f 70 65 72 61 74  or UPDATE operat
22522 69 6f 6e 0a 2a 2a 20 74 68 61 74 20 77 61 73 20  ion.** that was 
22523 73 74 61 72 74 65 64 20 62 79 20 61 20 70 72 69  started by a pri
22524 6f 72 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74  or call to sqlit
22525 65 33 47 65 6e 65 72 61 74 65 43 6f 6e 73 74 72  e3GenerateConstr
22526 61 69 6e 74 43 68 65 63 6b 73 2e 0a 2a 2a 20 41  aintChecks..** A
22527 20 63 6f 6e 73 65 63 75 74 69 76 65 20 72 61 6e   consecutive ran
22528 67 65 20 6f 66 20 72 65 67 69 73 74 65 72 73 20  ge of registers 
22529 73 74 61 72 74 69 6e 67 20 61 74 20 72 65 67 52  starting at regR
2252a 6f 77 69 64 20 63 6f 6e 74 61 69 6e 73 20 74 68  owid contains th
2252b 65 0a 2a 2a 20 72 6f 77 69 64 20 61 6e 64 20 74  e.** rowid and t
2252c 68 65 20 63 6f 6e 74 65 6e 74 20 74 6f 20 62 65  he content to be
2252d 20 69 6e 73 65 72 74 65 64 2e 0a 2a 2a 0a 2a 2a   inserted..**.**
2252e 20 54 68 65 20 61 72 67 75 6d 65 6e 74 73 20 74   The arguments t
2252f 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 73  o this routine s
22530 68 6f 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d  hould be the sam
22531 65 20 61 73 20 74 68 65 20 66 69 72 73 74 20 73  e as the first s
22532 69 78 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 73 20  ix.** arguments 
22533 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61  to sqlite3Genera
22534 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68 65 63  teConstraintChec
22535 6b 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  ks..*/.SQLITE_PR
22536 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
22537 65 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74  e3CompleteInsert
22538 69 6f 6e 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ion(.  Parse *pP
22539 61 72 73 65 2c 20 20 20 20 20 20 2f 2a 20 54 68  arse,      /* Th
2253a 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74  e parser context
2253b 20 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61   */.  Table *pTa
2253c 62 2c 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65  b,        /* the
2253d 20 74 61 62 6c 65 20 69 6e 74 6f 20 77 68 69 63   table into whic
2253e 68 20 77 65 20 61 72 65 20 69 6e 73 65 72 74 69  h we are inserti
2253f 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 62 61 73 65  ng */.  int base
22540 43 75 72 2c 20 20 20 20 20 20 20 20 2f 2a 20 49  Cur,        /* I
22541 6e 64 65 78 20 6f 66 20 61 20 72 65 61 64 2f 77  ndex of a read/w
22542 72 69 74 65 20 63 75 72 73 6f 72 20 70 6f 69 6e  rite cursor poin
22543 74 69 6e 67 20 61 74 20 70 54 61 62 20 2a 2f 0a  ting at pTab */.
22544 20 20 69 6e 74 20 72 65 67 52 6f 77 69 64 2c 20    int regRowid, 
22545 20 20 20 20 20 20 2f 2a 20 52 61 6e 67 65 20 6f        /* Range o
22546 66 20 63 6f 6e 74 65 6e 74 20 2a 2f 0a 20 20 69  f content */.  i
22547 6e 74 20 2a 61 52 65 67 49 64 78 2c 20 20 20 20  nt *aRegIdx,    
22548 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 20 75     /* Register u
22549 73 65 64 20 62 79 20 65 61 63 68 20 69 6e 64 65  sed by each inde
2254a 78 2e 20 20 30 20 66 6f 72 20 75 6e 75 73 65 64  x.  0 for unused
2254b 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69 6e   indices */.  in
2254c 74 20 69 73 55 70 64 61 74 65 2c 20 20 20 20 20  t isUpdate,     
2254d 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 55 50    /* True for UP
2254e 44 41 54 45 2c 20 46 61 6c 73 65 20 66 6f 72 20  DATE, False for 
2254f 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69 6e 74 20  INSERT */.  int 
22550 6e 65 77 49 64 78 2c 20 20 20 20 20 20 20 20 20  newIdx,         
22551 2f 2a 20 49 6e 64 65 78 20 6f 66 20 4e 45 57 20  /* Index of NEW 
22552 74 61 62 6c 65 20 66 6f 72 20 74 72 69 67 67 65  table for trigge
22553 72 73 2e 20 20 2d 31 20 69 66 20 6e 6f 6e 65 20  rs.  -1 if none 
22554 2a 2f 0a 20 20 69 6e 74 20 61 70 70 65 6e 64 42  */.  int appendB
22555 69 61 73 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ias      /* True
22556 20 69 66 20 74 68 69 73 20 69 73 20 6c 69 6b 65   if this is like
22557 6c 79 20 74 6f 20 62 65 20 61 6e 20 61 70 70 65  ly to be an appe
22558 6e 64 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69  nd */.){.  int i
22559 3b 0a 20 20 56 64 62 65 20 2a 76 3b 0a 20 20 69  ;.  Vdbe *v;.  i
2255a 6e 74 20 6e 49 64 78 3b 0a 20 20 49 6e 64 65 78  nt nIdx;.  Index
2255b 20 2a 70 49 64 78 3b 0a 20 20 69 6e 74 20 70 69   *pIdx;.  int pi
2255c 6b 5f 66 6c 61 67 73 3b 0a 20 20 69 6e 74 20 72  k_flags;.  int r
2255d 65 67 44 61 74 61 3b 0a 20 20 69 6e 74 20 72 65  egData;.  int re
2255e 67 52 65 63 3b 0a 0a 20 20 76 20 3d 20 73 71 6c  gRec;..  v = sql
2255f 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72  ite3GetVdbe(pPar
22560 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76  se);.  assert( v
22561 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  !=0 );.  assert(
22562 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 3d 3d   pTab->pSelect==
22563 30 20 29 3b 20 20 2f 2a 20 54 68 69 73 20 74 61  0 );  /* This ta
22564 62 6c 65 20 69 73 20 6e 6f 74 20 61 20 56 49 45  ble is not a VIE
22565 57 20 2a 2f 0a 20 20 66 6f 72 28 6e 49 64 78 3d  W */.  for(nIdx=
22566 30 2c 20 70 49 64 78 3d 70 54 61 62 2d 3e 70 49  0, pIdx=pTab->pI
22567 6e 64 65 78 3b 20 70 49 64 78 3b 20 70 49 64 78  ndex; pIdx; pIdx
22568 3d 70 49 64 78 2d 3e 70 4e 65 78 74 2c 20 6e 49  =pIdx->pNext, nI
22569 64 78 2b 2b 29 7b 7d 0a 20 20 66 6f 72 28 69 3d  dx++){}.  for(i=
2256a 6e 49 64 78 2d 31 3b 20 69 3e 3d 30 3b 20 69 2d  nIdx-1; i>=0; i-
2256b 2d 29 7b 0a 20 20 20 20 69 66 28 20 61 52 65 67  -){.    if( aReg
2256c 49 64 78 5b 69 5d 3d 3d 30 20 29 20 63 6f 6e 74  Idx[i]==0 ) cont
2256d 69 6e 75 65 3b 0a 20 20 20 20 73 71 6c 69 74 65  inue;.    sqlite
2256e 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2256f 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 62 61 73  P_IdxInsert, bas
22570 65 43 75 72 2b 69 2b 31 2c 20 61 52 65 67 49 64  eCur+i+1, aRegId
22571 78 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 72 65 67  x[i]);.  }.  reg
22572 44 61 74 61 20 3d 20 72 65 67 52 6f 77 69 64 20  Data = regRowid 
22573 2b 20 31 3b 0a 20 20 72 65 67 52 65 63 20 3d 20  + 1;.  regRec = 
22574 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
22575 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  g(pParse);.  sql
22576 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
22577 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c  , OP_MakeRecord,
22578 20 72 65 67 44 61 74 61 2c 20 70 54 61 62 2d 3e   regData, pTab->
22579 6e 43 6f 6c 2c 20 72 65 67 52 65 63 29 3b 0a 20  nCol, regRec);. 
2257a 20 73 71 6c 69 74 65 33 54 61 62 6c 65 41 66 66   sqlite3TableAff
2257b 69 6e 69 74 79 53 74 72 28 76 2c 20 70 54 61 62  inityStr(v, pTab
2257c 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2257d 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
2257e 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 44  nge(pParse, regD
2257f 61 74 61 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ata, pTab->nCol)
22580 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
22581 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52 0a 20 20  _OMIT_TRIGGER.  
22582 69 66 28 20 6e 65 77 49 64 78 3e 3d 30 20 29 7b  if( newIdx>=0 ){
22583 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
22584 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73  AddOp3(v, OP_Ins
22585 65 72 74 2c 20 6e 65 77 49 64 78 2c 20 72 65 67  ert, newIdx, reg
22586 52 65 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Rec, regRowid);.
22587 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28    }.#endif.  if(
22588 20 70 50 61 72 73 65 2d 3e 6e 65 73 74 65 64 20   pParse->nested 
22589 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61 67 73  ){.    pik_flags
2258a 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20   = 0;.  }else{. 
2258b 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 3d 20 4f     pik_flags = O
2258c 50 46 4c 41 47 5f 4e 43 48 41 4e 47 45 3b 0a 20  PFLAG_NCHANGE;. 
2258d 20 20 20 70 69 6b 5f 66 6c 61 67 73 20 7c 3d 20     pik_flags |= 
2258e 28 69 73 55 70 64 61 74 65 3f 4f 50 46 4c 41 47  (isUpdate?OPFLAG
2258f 5f 49 53 55 50 44 41 54 45 3a 4f 50 46 4c 41 47  _ISUPDATE:OPFLAG
22590 5f 4c 41 53 54 52 4f 57 49 44 29 3b 0a 20 20 7d  _LASTROWID);.  }
22591 0a 20 20 69 66 28 20 61 70 70 65 6e 64 42 69 61  .  if( appendBia
22592 73 20 29 7b 0a 20 20 20 20 70 69 6b 5f 66 6c 61  s ){.    pik_fla
22593 67 73 20 7c 3d 20 4f 50 46 4c 41 47 5f 41 50 50  gs |= OPFLAG_APP
22594 45 4e 44 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  END;.  }.  sqlit
22595 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
22596 4f 50 5f 49 6e 73 65 72 74 2c 20 62 61 73 65 43  OP_Insert, baseC
22597 75 72 2c 20 72 65 67 52 65 63 2c 20 72 65 67 52  ur, regRec, regR
22598 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 21 70 50  owid);.  if( !pP
22599 61 72 73 65 2d 3e 6e 65 73 74 65 64 20 29 7b 0a  arse->nested ){.
2259a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
2259b 68 61 6e 67 65 50 34 28 76 2c 20 2d 31 2c 20 70  hangeP4(v, -1, p
2259c 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 50 34 5f 53  Tab->zName, P4_S
2259d 54 41 54 49 43 29 3b 0a 20 20 7d 0a 20 20 73 71  TATIC);.  }.  sq
2259e 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
2259f 35 28 76 2c 20 70 69 6b 5f 66 6c 61 67 73 29 3b  5(v, pik_flags);
225a0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61  .}../*.** Genera
225a1 74 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c  te code that wil
225a2 6c 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 20 66  l open cursors f
225a3 6f 72 20 61 20 74 61 62 6c 65 20 61 6e 64 20 66  or a table and f
225a4 6f 72 20 61 6c 6c 0a 2a 2a 20 69 6e 64 69 63 65  or all.** indice
225a5 73 20 6f 66 20 74 68 61 74 20 74 61 62 6c 65 2e  s of that table.
225a6 20 20 54 68 65 20 22 62 61 73 65 43 75 72 22 20    The "baseCur" 
225a7 70 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65  parameter is the
225a8 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 75   cursor number u
225a9 73 65 64 0a 2a 2a 20 66 6f 72 20 74 68 65 20 74  sed.** for the t
225aa 61 62 6c 65 2e 20 20 49 6e 64 69 63 65 73 20 61  able.  Indices a
225ab 72 65 20 6f 70 65 6e 65 64 20 6f 6e 20 73 75 62  re opened on sub
225ac 73 65 71 75 65 6e 74 20 63 75 72 73 6f 72 73 2e  sequent cursors.
225ad 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  .**.** Return th
225ae 65 20 6e 75 6d 62 65 72 20 6f 66 20 69 6e 64 69  e number of indi
225af 63 65 73 20 6f 6e 20 74 68 65 20 74 61 62 6c 65  ces on the table
225b0 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
225b1 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 4f  ATE int sqlite3O
225b2 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64 69 63  penTableAndIndic
225b3 65 73 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  es(.  Parse *pPa
225b4 72 73 65 2c 20 20 20 2f 2a 20 50 61 72 73 69 6e  rse,   /* Parsin
225b5 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54  g context */.  T
225b6 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
225b7 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20 6f  /* Table to be o
225b8 70 65 6e 65 64 20 2a 2f 0a 20 20 69 6e 74 20 62  pened */.  int b
225b9 61 73 65 43 75 72 2c 20 20 20 20 20 2f 2a 20 43  aseCur,     /* C
225ba 75 72 73 6f 72 20 6e 75 6d 62 65 72 20 61 73 73  ursor number ass
225bb 69 67 6e 65 64 20 74 6f 20 74 68 65 20 74 61 62  igned to the tab
225bc 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 6f 70 20 20  le */.  int op  
225bd 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 50 5f 4f           /* OP_O
225be 70 65 6e 52 65 61 64 20 6f 72 20 4f 50 5f 4f 70  penRead or OP_Op
225bf 65 6e 57 72 69 74 65 20 2a 2f 0a 29 7b 0a 20 20  enWrite */.){.  
225c0 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 44 62  int i;.  int iDb
225c1 3b 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b  ;.  Index *pIdx;
225c2 0a 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 69  .  Vdbe *v;..  i
225c3 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
225c4 62 29 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  b) ) return 0;. 
225c5 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
225c6 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72  hemaToIndex(pPar
225c7 73 65 2d 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53  se->db, pTab->pS
225c8 63 68 65 6d 61 29 3b 0a 20 20 76 20 3d 20 73 71  chema);.  v = sq
225c9 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50 61  lite3GetVdbe(pPa
225ca 72 73 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20  rse);.  assert( 
225cb 76 21 3d 30 20 29 3b 0a 20 20 73 71 6c 69 74 65  v!=0 );.  sqlite
225cc 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61 72 73  3OpenTable(pPars
225cd 65 2c 20 62 61 73 65 43 75 72 2c 20 69 44 62 2c  e, baseCur, iDb,
225ce 20 70 54 61 62 2c 20 6f 70 29 3b 0a 20 20 66 6f   pTab, op);.  fo
225cf 72 28 69 3d 31 2c 20 70 49 64 78 3d 70 54 61 62  r(i=1, pIdx=pTab
225d0 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b 20  ->pIndex; pIdx; 
225d1 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74  pIdx=pIdx->pNext
225d2 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 4b 65 79 49  , i++){.    KeyI
225d3 6e 66 6f 20 2a 70 4b 65 79 20 3d 20 73 71 6c 69  nfo *pKey = sqli
225d4 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f 28  te3IndexKeyinfo(
225d5 70 50 61 72 73 65 2c 20 70 49 64 78 29 3b 0a 20  pParse, pIdx);. 
225d6 20 20 20 61 73 73 65 72 74 28 20 70 49 64 78 2d     assert( pIdx-
225d7 3e 70 53 63 68 65 6d 61 3d 3d 70 54 61 62 2d 3e  >pSchema==pTab->
225d8 70 53 63 68 65 6d 61 20 29 3b 0a 20 20 20 20 73  pSchema );.    s
225d9 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
225da 28 76 2c 20 6f 70 2c 20 69 2b 62 61 73 65 43 75  (v, op, i+baseCu
225db 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69  r, pIdx->tnum, i
225dc 44 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  Db,.            
225dd 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
225de 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
225df 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
225e0 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
225e1 22 25 73 22 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d  "%s", pIdx->zNam
225e2 65 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  e));.  }.  if( p
225e3 50 61 72 73 65 2d 3e 6e 54 61 62 3c 3d 62 61 73  Parse->nTab<=bas
225e4 65 43 75 72 2b 69 20 29 7b 0a 20 20 20 20 70 50  eCur+i ){.    pP
225e5 61 72 73 65 2d 3e 6e 54 61 62 20 3d 20 62 61 73  arse->nTab = bas
225e6 65 43 75 72 2b 69 3b 0a 20 20 7d 0a 20 20 72 65  eCur+i;.  }.  re
225e7 74 75 72 6e 20 69 2d 31 3b 0a 7d 0a 0a 0a 23 69  turn i-1;.}...#i
225e8 66 64 65 66 20 53 51 4c 49 54 45 5f 54 45 53 54  fdef SQLITE_TEST
225e9 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ./*.** The follo
225ea 77 69 6e 67 20 67 6c 6f 62 61 6c 20 76 61 72 69  wing global vari
225eb 61 62 6c 65 20 69 73 20 69 6e 63 72 65 6d 65 6e  able is incremen
225ec 74 65 64 20 77 68 65 6e 65 76 65 72 20 74 68 65  ted whenever the
225ed 0a 2a 2a 20 74 72 61 6e 73 66 65 72 20 6f 70 74  .** transfer opt
225ee 69 6d 69 7a 61 74 69 6f 6e 20 69 73 20 75 73 65  imization is use
225ef 64 2e 20 20 54 68 69 73 20 69 73 20 75 73 65 64  d.  This is used
225f0 20 66 6f 72 20 74 65 73 74 69 6e 67 0a 2a 2a 20   for testing.** 
225f1 70 75 72 70 6f 73 65 73 20 6f 6e 6c 79 20 2d 20  purposes only - 
225f2 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68 65  to make sure the
225f3 20 74 72 61 6e 73 66 65 72 20 6f 70 74 69 6d 69   transfer optimi
225f4 7a 61 74 69 6f 6e 20 72 65 61 6c 6c 79 0a 2a 2a  zation really.**
225f5 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20 77 68   is happening wh
225f6 65 6e 20 69 74 20 69 73 20 73 75 70 70 6f 73 65  en it is suppose
225f7 20 74 6f 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41   to..*/.SQLITE_A
225f8 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 78  PI int sqlite3_x
225f9 66 65 72 6f 70 74 5f 63 6f 75 6e 74 3b 0a 23 65  feropt_count;.#e
225fa 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 54  ndif /* SQLITE_T
225fb 45 53 54 20 2a 2f 0a 0a 0a 23 69 66 6e 64 65 66  EST */...#ifndef
225fc 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45   SQLITE_OMIT_XFE
225fd 52 5f 4f 50 54 0a 2f 2a 0a 2a 2a 20 43 68 65 63  R_OPT./*.** Chec
225fe 6b 20 74 6f 20 63 6f 6c 6c 61 74 69 6f 6e 20 6e  k to collation n
225ff 61 6d 65 73 20 74 6f 20 73 65 65 20 69 66 20 74  ames to see if t
22600 68 65 79 20 61 72 65 20 63 6f 6d 70 61 74 69 62  hey are compatib
22601 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
22602 74 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c 65  t xferCompatible
22603 43 6f 6c 6c 61 74 69 6f 6e 28 63 6f 6e 73 74 20  Collation(const 
22604 63 68 61 72 20 2a 7a 31 2c 20 63 6f 6e 73 74 20  char *z1, const 
22605 63 68 61 72 20 2a 7a 32 29 7b 0a 20 20 69 66 28  char *z2){.  if(
22606 20 7a 31 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65   z1==0 ){.    re
22607 74 75 72 6e 20 7a 32 3d 3d 30 3b 0a 20 20 7d 0a  turn z2==0;.  }.
22608 20 20 69 66 28 20 7a 32 3d 3d 30 20 29 7b 0a 20    if( z2==0 ){. 
22609 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d     return 0;.  }
2260a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65  .  return sqlite
2260b 33 53 74 72 49 43 6d 70 28 7a 31 2c 20 7a 32 29  3StrICmp(z1, z2)
2260c 3d 3d 30 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43  ==0;.}.../*.** C
2260d 68 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 69  heck to see if i
2260e 6e 64 65 78 20 70 53 72 63 20 69 73 20 63 6f 6d  ndex pSrc is com
2260f 70 61 74 69 62 6c 65 20 61 73 20 61 20 73 6f 75  patible as a sou
22610 72 63 65 20 6f 66 20 64 61 74 61 0a 2a 2a 20 66  rce of data.** f
22611 6f 72 20 69 6e 64 65 78 20 70 44 65 73 74 20 69  or index pDest i
22612 6e 20 61 6e 20 69 6e 73 65 72 74 20 74 72 61 6e  n an insert tran
22613 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f  sfer optimizatio
22614 6e 2e 20 20 54 68 65 20 72 75 6c 65 73 0a 2a 2a  n.  The rules.**
22615 20 66 6f 72 20 61 20 63 6f 6d 70 61 74 69 62 6c   for a compatibl
22616 65 20 69 6e 64 65 78 3a 0a 2a 2a 0a 2a 2a 20 20  e index:.**.**  
22617 20 20 2a 20 20 20 54 68 65 20 69 6e 64 65 78 20    *   The index 
22618 69 73 20 6f 76 65 72 20 74 68 65 20 73 61 6d 65  is over the same
22619 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 0a   set of columns.
2261a 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 20 73 61  **    *   The sa
2261b 6d 65 20 44 45 53 43 20 61 6e 64 20 41 53 43 20  me DESC and ASC 
2261c 6d 61 72 6b 69 6e 67 73 20 6f 63 63 75 72 73 20  markings occurs 
2261d 6f 6e 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73 0a 2a  on all columns.*
2261e 2a 20 20 20 20 2a 20 20 20 54 68 65 20 73 61 6d  *    *   The sam
2261f 65 20 6f 6e 45 72 72 6f 72 20 70 72 6f 63 65 73  e onError proces
22620 73 69 6e 67 20 28 4f 45 5f 41 62 6f 72 74 2c 20  sing (OE_Abort, 
22621 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29 0a  OE_Ignore, etc).
22622 2a 2a 20 20 20 20 2a 20 20 20 54 68 65 20 73 61  **    *   The sa
22623 6d 65 20 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71  me collating seq
22624 75 65 6e 63 65 20 6f 6e 20 65 61 63 68 20 63 6f  uence on each co
22625 6c 75 6d 6e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  lumn.*/.static i
22626 6e 74 20 78 66 65 72 43 6f 6d 70 61 74 69 62 6c  nt xferCompatibl
22627 65 49 6e 64 65 78 28 49 6e 64 65 78 20 2a 70 44  eIndex(Index *pD
22628 65 73 74 2c 20 49 6e 64 65 78 20 2a 70 53 72 63  est, Index *pSrc
22629 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61 73  ){.  int i;.  as
2262a 73 65 72 74 28 20 70 44 65 73 74 20 26 26 20 70  sert( pDest && p
2262b 53 72 63 20 29 3b 0a 20 20 61 73 73 65 72 74 28  Src );.  assert(
2262c 20 70 44 65 73 74 2d 3e 70 54 61 62 6c 65 21 3d   pDest->pTable!=
2262d 70 53 72 63 2d 3e 70 54 61 62 6c 65 20 29 3b 0a  pSrc->pTable );.
2262e 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6e 43 6f    if( pDest->nCo
2262f 6c 75 6d 6e 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c  lumn!=pSrc->nCol
22630 75 6d 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  umn ){.    retur
22631 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66 65 72  n 0;   /* Differ
22632 65 6e 74 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f  ent number of co
22633 6c 75 6d 6e 73 20 2a 2f 0a 20 20 7d 0a 20 20 69  lumns */.  }.  i
22634 66 28 20 70 44 65 73 74 2d 3e 6f 6e 45 72 72 6f  f( pDest->onErro
22635 72 21 3d 70 53 72 63 2d 3e 6f 6e 45 72 72 6f 72  r!=pSrc->onError
22636 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
22637 3b 20 20 20 2f 2a 20 44 69 66 66 65 72 65 6e 74  ;   /* Different
22638 20 63 6f 6e 66 6c 69 63 74 20 72 65 73 6f 6c 75   conflict resolu
22639 74 69 6f 6e 20 73 74 72 61 74 65 67 69 65 73 20  tion strategies 
2263a 2a 2f 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  */.  }.  for(i=0
2263b 3b 20 69 3c 70 53 72 63 2d 3e 6e 43 6f 6c 75 6d  ; i<pSrc->nColum
2263c 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  n; i++){.    if(
2263d 20 70 53 72 63 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pSrc->aiColumn[
2263e 69 5d 21 3d 70 44 65 73 74 2d 3e 61 69 43 6f 6c  i]!=pDest->aiCol
2263f 75 6d 6e 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20  umn[i] ){.      
22640 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44  return 0;   /* D
22641 69 66 66 65 72 65 6e 74 20 63 6f 6c 75 6d 6e 73  ifferent columns
22642 20 69 6e 64 65 78 65 64 20 2a 2f 0a 20 20 20 20   indexed */.    
22643 7d 0a 20 20 20 20 69 66 28 20 70 53 72 63 2d 3e  }.    if( pSrc->
22644 61 53 6f 72 74 4f 72 64 65 72 5b 69 5d 21 3d 70  aSortOrder[i]!=p
22645 44 65 73 74 2d 3e 61 53 6f 72 74 4f 72 64 65 72  Dest->aSortOrder
22646 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65 74  [i] ){.      ret
22647 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66 66  urn 0;   /* Diff
22648 65 72 65 6e 74 20 73 6f 72 74 20 6f 72 64 65 72  erent sort order
22649 73 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 69  s */.    }.    i
2264a 66 28 20 70 53 72 63 2d 3e 61 7a 43 6f 6c 6c 5b  f( pSrc->azColl[
2264b 69 5d 21 3d 70 44 65 73 74 2d 3e 61 7a 43 6f 6c  i]!=pDest->azCol
2264c 6c 5b 69 5d 20 29 7b 0a 20 20 20 20 20 20 72 65  l[i] ){.      re
2264d 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 44 69 66  turn 0;   /* Dif
2264e 66 65 72 65 6e 74 20 63 6f 6c 6c 61 74 69 6e 67  ferent collating
2264f 20 73 65 71 75 65 6e 63 65 73 20 2a 2f 0a 20 20   sequences */.  
22650 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66    }.  }..  /* If
22651 20 6e 6f 20 74 65 73 74 20 61 62 6f 76 65 20 66   no test above f
22652 61 69 6c 73 20 74 68 65 6e 20 74 68 65 20 69 6e  ails then the in
22653 64 69 63 65 73 20 6d 75 73 74 20 62 65 20 63 6f  dices must be co
22654 6d 70 61 74 69 62 6c 65 20 2a 2f 0a 20 20 72 65  mpatible */.  re
22655 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  turn 1;.}../*.**
22656 20 41 74 74 65 6d 70 74 20 74 68 65 20 74 72 61   Attempt the tra
22657 6e 73 66 65 72 20 6f 70 74 69 6d 69 7a 61 74 69  nsfer optimizati
22658 6f 6e 20 6f 6e 20 49 4e 53 45 52 54 73 20 6f 66  on on INSERTs of
22659 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20   the form.**.** 
2265a 20 20 20 20 49 4e 53 45 52 54 20 49 4e 54 4f 20      INSERT INTO 
2265b 74 61 62 31 20 53 45 4c 45 43 54 20 2a 20 46 52  tab1 SELECT * FR
2265c 4f 4d 20 74 61 62 32 3b 0a 2a 2a 0a 2a 2a 20 54  OM tab2;.**.** T
2265d 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  his optimization
2265e 20 69 73 20 6f 6e 6c 79 20 61 74 74 65 6d 70 74   is only attempt
2265f 65 64 20 69 66 0a 2a 2a 0a 2a 2a 20 20 20 20 28  ed if.**.**    (
22660 31 29 20 20 74 61 62 31 20 61 6e 64 20 74 61 62  1)  tab1 and tab
22661 32 20 68 61 76 65 20 69 64 65 6e 74 69 63 61 6c  2 have identical
22662 20 73 63 68 65 6d 61 73 20 69 6e 63 6c 75 64 69   schemas includi
22663 6e 67 20 61 6c 6c 20 74 68 65 0a 2a 2a 20 20 20  ng all the.**   
22664 20 20 20 20 20 20 73 61 6d 65 20 69 6e 64 69 63        same indic
22665 65 73 20 61 6e 64 20 63 6f 6e 73 74 72 61 69 6e  es and constrain
22666 74 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20  ts.**.**    (2) 
22667 20 74 61 62 31 20 61 6e 64 20 74 61 62 32 20 61   tab1 and tab2 a
22668 72 65 20 64 69 66 66 65 72 65 6e 74 20 74 61 62  re different tab
22669 6c 65 73 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29  les.**.**    (3)
2266a 20 20 54 68 65 72 65 20 6d 75 73 74 20 62 65 20    There must be 
2266b 6e 6f 20 74 72 69 67 67 65 72 73 20 6f 6e 20 74  no triggers on t
2266c 61 62 31 0a 2a 2a 0a 2a 2a 20 20 20 20 28 34 29  ab1.**.**    (4)
2266d 20 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74    The result set
2266e 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73   of the SELECT s
2266f 74 61 74 65 6d 65 6e 74 20 69 73 20 22 2a 22 0a  tatement is "*".
22670 2a 2a 0a 2a 2a 20 20 20 20 28 35 29 20 20 54 68  **.**    (5)  Th
22671 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
22672 6e 74 20 68 61 73 20 6e 6f 20 57 48 45 52 45 2c  nt has no WHERE,
22673 20 48 41 56 49 4e 47 2c 20 4f 52 44 45 52 20 42   HAVING, ORDER B
22674 59 2c 20 47 52 4f 55 50 20 42 59 2c 0a 2a 2a 20  Y, GROUP BY,.** 
22675 20 20 20 20 20 20 20 20 6f 72 20 4c 49 4d 49 54          or LIMIT
22676 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20   clause..**.**  
22677 20 20 28 36 29 20 20 54 68 65 20 53 45 4c 45 43    (6)  The SELEC
22678 54 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61  T statement is a
22679 20 73 69 6d 70 6c 65 20 28 6e 6f 74 20 61 20 63   simple (not a c
2267a 6f 6d 70 6f 75 6e 64 29 20 73 65 6c 65 63 74 20  ompound) select 
2267b 74 68 61 74 0a 2a 2a 20 20 20 20 20 20 20 20 20  that.**         
2267c 63 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 74 61  contains only ta
2267d 62 32 20 69 6e 20 69 74 73 20 46 52 4f 4d 20 63  b2 in its FROM c
2267e 6c 61 75 73 65 0a 2a 2a 0a 2a 2a 20 54 68 69 73  lause.**.** This
2267f 20 6d 65 74 68 6f 64 20 66 6f 72 20 69 6d 70 6c   method for impl
22680 65 6d 65 6e 74 69 6e 67 20 74 68 65 20 49 4e 53  ementing the INS
22681 45 52 54 20 74 72 61 6e 73 66 65 72 73 20 72 61  ERT transfers ra
22682 77 20 72 65 63 6f 72 64 73 20 66 72 6f 6d 0a 2a  w records from.*
22683 2a 20 74 61 62 32 20 6f 76 65 72 20 74 6f 20 74  * tab2 over to t
22684 61 62 31 2e 20 20 54 68 65 20 63 6f 6c 75 6d 6e  ab1.  The column
22685 73 20 61 72 65 20 6e 6f 74 20 64 65 63 6f 64 65  s are not decode
22686 64 2e 20 20 52 61 77 20 72 65 63 6f 72 64 73 20  d.  Raw records 
22687 66 72 6f 6d 0a 2a 2a 20 74 68 65 20 69 6e 64 69  from.** the indi
22688 63 65 73 20 6f 66 20 74 61 62 32 20 61 72 65 20  ces of tab2 are 
22689 74 72 61 6e 73 66 65 72 65 64 20 74 6f 20 74 61  transfered to ta
2268a 62 31 20 61 73 20 77 65 6c 6c 2e 20 20 49 6e 20  b1 as well.  In 
2268b 73 6f 20 64 6f 69 6e 67 2c 0a 2a 2a 20 74 68 65  so doing,.** the
2268c 20 72 65 73 75 6c 74 69 6e 67 20 74 61 62 31 20   resulting tab1 
2268d 68 61 73 20 6d 75 63 68 20 6c 65 73 73 20 66 72  has much less fr
2268e 61 67 6d 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 0a  agmentation..**.
2268f 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
22690 72 65 74 75 72 6e 73 20 54 52 55 45 20 69 66 20  returns TRUE if 
22691 74 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e  the optimization
22692 20 69 73 20 61 74 74 65 6d 70 74 65 64 2e 20 20   is attempted.  
22693 49 66 20 61 6e 79 0a 2a 2a 20 6f 66 20 74 68 65  If any.** of the
22694 20 63 6f 6e 64 69 74 69 6f 6e 73 20 61 62 6f 76   conditions abov
22695 65 20 66 61 69 6c 20 73 6f 20 74 68 61 74 20 74  e fail so that t
22696 68 65 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20  he optimization 
22697 73 68 6f 75 6c 64 20 6e 6f 74 0a 2a 2a 20 62 65  should not.** be
22698 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 65 6e   attempted, then
22699 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
2269a 74 75 72 6e 73 20 46 41 4c 53 45 2e 0a 2a 2f 0a  turns FALSE..*/.
2269b 73 74 61 74 69 63 20 69 6e 74 20 78 66 65 72 4f  static int xferO
2269c 70 74 69 6d 69 7a 61 74 69 6f 6e 28 0a 20 20 50  ptimization(.  P
2269d 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20  arse *pParse,   
2269e 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
2269f 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 61 62 6c  ontext */.  Tabl
226a0 65 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 20  e *pDest,       
226a1 20 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 77    /* The table w
226a2 65 20 61 72 65 20 69 6e 73 65 72 74 69 6e 67 20  e are inserting 
226a3 69 6e 74 6f 20 2a 2f 0a 20 20 53 65 6c 65 63 74  into */.  Select
226a4 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20 20   *pSelect,      
226a5 2f 2a 20 41 20 53 45 4c 45 43 54 20 73 74 61 74  /* A SELECT stat
226a6 65 6d 65 6e 74 20 74 6f 20 75 73 65 20 61 73 20  ement to use as 
226a7 74 68 65 20 64 61 74 61 20 73 6f 75 72 63 65 20  the data source 
226a8 2a 2f 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72  */.  int onError
226a9 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f  ,          /* Ho
226aa 77 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73  w to handle cons
226ab 74 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f  traint errors */
226ac 0a 20 20 69 6e 74 20 69 44 62 44 65 73 74 20 20  .  int iDbDest  
226ad 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
226ae 64 61 74 61 62 61 73 65 20 6f 66 20 70 44 65 73  database of pDes
226af 74 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 4c 69  t */.){.  ExprLi
226b0 73 74 20 2a 70 45 4c 69 73 74 3b 20 20 20 20 20  st *pEList;     
226b1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
226b2 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
226b3 74 68 65 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  the SELECT */.  
226b4 54 61 62 6c 65 20 2a 70 53 72 63 3b 20 20 20 20  Table *pSrc;    
226b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226b6 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 69 6e   /* The table in
226b7 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
226b8 20 6f 66 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20   of SELECT */.  
226b9 49 6e 64 65 78 20 2a 70 53 72 63 49 64 78 2c 20  Index *pSrcIdx, 
226ba 2a 70 44 65 73 74 49 64 78 3b 20 20 20 20 20 20  *pDestIdx;      
226bb 20 2f 2a 20 53 6f 75 72 63 65 20 61 6e 64 20 64   /* Source and d
226bc 65 73 74 69 6e 61 74 69 6f 6e 20 69 6e 64 69 63  estination indic
226bd 65 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53  es */.  struct S
226be 72 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 49 74  rcList_item *pIt
226bf 65 6d 3b 20 20 20 20 20 20 2f 2a 20 41 6e 20 65  em;      /* An e
226c0 6c 65 6d 65 6e 74 20 6f 66 20 70 53 65 6c 65 63  lement of pSelec
226c1 74 2d 3e 70 53 72 63 20 2a 2f 0a 20 20 69 6e 74  t->pSrc */.  int
226c2 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
226c3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
226c4 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
226c5 0a 20 20 69 6e 74 20 69 44 62 53 72 63 3b 20 20  .  int iDbSrc;  
226c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226c7 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
226c8 61 73 65 20 6f 66 20 70 53 72 63 20 2a 2f 0a 20  ase of pSrc */. 
226c9 20 69 6e 74 20 69 53 72 63 2c 20 69 44 65 73 74   int iSrc, iDest
226ca 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
226cb 20 20 2f 2a 20 43 75 72 73 6f 72 73 20 66 72 6f    /* Cursors fro
226cc 6d 20 73 6f 75 72 63 65 20 61 6e 64 20 64 65 73  m source and des
226cd 74 69 6e 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  tination */.  in
226ce 74 20 61 64 64 72 31 2c 20 61 64 64 72 32 3b 20  t addr1, addr2; 
226cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
226d0 2a 20 4c 6f 6f 70 20 61 64 64 72 65 73 73 65 73  * Loop addresses
226d1 20 2a 2f 0a 20 20 69 6e 74 20 65 6d 70 74 79 44   */.  int emptyD
226d2 65 73 74 54 65 73 74 3b 20 20 20 20 20 20 20 20  estTest;        
226d3 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
226d4 73 20 6f 66 20 74 65 73 74 20 66 6f 72 20 65 6d  s of test for em
226d5 70 74 79 20 70 44 65 73 74 20 2a 2f 0a 20 20 69  pty pDest */.  i
226d6 6e 74 20 65 6d 70 74 79 53 72 63 54 65 73 74 3b  nt emptySrcTest;
226d7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226d8 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 65  /* Address of te
226d9 73 74 20 66 6f 72 20 65 6d 70 74 79 20 70 53 72  st for empty pSr
226da 63 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20  c */.  Vdbe *v; 
226db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226dc 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 56          /* The V
226dd 44 42 45 20 77 65 20 61 72 65 20 62 75 69 6c 64  DBE we are build
226de 69 6e 67 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f  ing */.  KeyInfo
226df 20 2a 70 4b 65 79 3b 20 20 20 20 20 20 20 20 20   *pKey;         
226e0 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4b 65 79            /* Key
226e1 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72   information for
226e2 20 61 6e 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69   an index */.  i
226e3 6e 74 20 72 65 67 41 75 74 6f 69 6e 63 3b 20 20  nt regAutoinc;  
226e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
226e5 2f 2a 20 4d 65 6d 6f 72 79 20 72 65 67 69 73 74  /* Memory regist
226e6 65 72 20 75 73 65 64 20 62 79 20 41 55 54 4f 49  er used by AUTOI
226e7 4e 43 20 2a 2f 0a 20 20 69 6e 74 20 64 65 73 74  NC */.  int dest
226e8 48 61 73 55 6e 69 71 75 65 49 64 78 20 3d 20 30  HasUniqueIdx = 0
226e9 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65  ;        /* True
226ea 20 69 66 20 70 44 65 73 74 20 68 61 73 20 61 20   if pDest has a 
226eb 55 4e 49 51 55 45 20 69 6e 64 65 78 20 2a 2f 0a  UNIQUE index */.
226ec 20 20 69 6e 74 20 72 65 67 44 61 74 61 2c 20 72    int regData, r
226ed 65 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20  egRowid;        
226ee 20 20 20 2f 2a 20 52 65 67 69 73 74 65 72 73 20     /* Registers 
226ef 68 6f 6c 64 69 6e 67 20 64 61 74 61 20 61 6e 64  holding data and
226f0 20 72 6f 77 69 64 20 2a 2f 0a 0a 20 20 69 66 28   rowid */..  if(
226f1 20 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a 20   pSelect==0 ){. 
226f2 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
226f3 2a 20 4d 75 73 74 20 62 65 20 6f 66 20 74 68 65  * Must be of the
226f4 20 66 6f 72 6d 20 20 49 4e 53 45 52 54 20 49 4e   form  INSERT IN
226f5 54 4f 20 2e 2e 2e 20 53 45 4c 45 43 54 20 2e 2e  TO ... SELECT ..
226f6 2e 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70  . */.  }.  if( p
226f7 44 65 73 74 2d 3e 70 54 72 69 67 67 65 72 20 29  Dest->pTrigger )
226f8 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20  {.    return 0; 
226f9 20 20 2f 2a 20 74 61 62 31 20 6d 75 73 74 20 6e    /* tab1 must n
226fa 6f 74 20 68 61 76 65 20 74 72 69 67 67 65 72 73  ot have triggers
226fb 20 2a 2f 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20   */.  }.#ifndef 
226fc 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
226fd 55 41 4c 54 41 42 4c 45 0a 20 20 69 66 28 20 70  UALTABLE.  if( p
226fe 44 65 73 74 2d 3e 74 61 62 46 6c 61 67 73 20 26  Dest->tabFlags &
226ff 20 54 46 5f 56 69 72 74 75 61 6c 20 29 7b 0a 20   TF_Virtual ){. 
22700 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
22701 2a 20 74 61 62 31 20 6d 75 73 74 20 6e 6f 74 20  * tab1 must not 
22702 62 65 20 61 20 76 69 72 74 75 61 6c 20 74 61 62  be a virtual tab
22703 6c 65 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69 66  le */.  }.#endif
22704 0a 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d  .  if( onError==
22705 4f 45 5f 44 65 66 61 75 6c 74 20 29 7b 0a 20 20  OE_Default ){.  
22706 20 20 6f 6e 45 72 72 6f 72 20 3d 20 4f 45 5f 41    onError = OE_A
22707 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69 66 28 20  bort;.  }.  if( 
22708 6f 6e 45 72 72 6f 72 21 3d 4f 45 5f 41 62 6f 72  onError!=OE_Abor
22709 74 20 26 26 20 6f 6e 45 72 72 6f 72 21 3d 4f 45  t && onError!=OE
2270a 5f 52 6f 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  _Rollback ){.   
2270b 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
2270c 43 61 6e 6e 6f 74 20 64 6f 20 4f 52 20 52 45 50  Cannot do OR REP
2270d 4c 41 43 45 20 6f 72 20 4f 52 20 49 47 4e 4f 52  LACE or OR IGNOR
2270e 45 20 6f 72 20 4f 52 20 46 41 49 4c 20 2a 2f 0a  E or OR FAIL */.
2270f 20 20 7d 0a 20 20 61 73 73 65 72 74 28 70 53 65    }.  assert(pSe
22710 6c 65 63 74 2d 3e 70 53 72 63 29 3b 20 20 20 2f  lect->pSrc);   /
22711 2a 20 61 6c 6c 6f 63 61 74 65 64 20 65 76 65 6e  * allocated even
22712 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   if there is no 
22713 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
22714 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 53   if( pSelect->pS
22715 72 63 2d 3e 6e 53 72 63 21 3d 31 20 29 7b 0a 20  rc->nSrc!=1 ){. 
22716 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f     return 0;   /
22717 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6d 75  * FROM clause mu
22718 73 74 20 68 61 76 65 20 65 78 61 63 74 6c 79 20  st have exactly 
22719 6f 6e 65 20 74 65 72 6d 20 2a 2f 0a 20 20 7d 0a  one term */.  }.
2271a 20 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70    if( pSelect->p
2271b 53 72 63 2d 3e 61 5b 30 5d 2e 70 53 65 6c 65 63  Src->a[0].pSelec
2271c 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  t ){.    return 
2271d 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61  0;   /* FROM cla
2271e 75 73 65 20 63 61 6e 6e 6f 74 20 63 6f 6e 74 61  use cannot conta
2271f 69 6e 20 61 20 73 75 62 71 75 65 72 79 20 2a 2f  in a subquery */
22720 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 65 6c 65  .  }.  if( pSele
22721 63 74 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20 20  ct->pWhere ){.  
22722 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
22723 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
22724 68 61 76 65 20 61 20 57 48 45 52 45 20 63 6c 61  have a WHERE cla
22725 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  use */.  }.  if(
22726 20 70 53 65 6c 65 63 74 2d 3e 70 4f 72 64 65 72   pSelect->pOrder
22727 42 79 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  By ){.    return
22728 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20   0;   /* SELECT 
22729 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 6e 20  may not have an 
2272a 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
2272b 2a 2f 0a 20 20 7d 0a 20 20 2f 2a 20 44 6f 20 6e  */.  }.  /* Do n
2272c 6f 74 20 6e 65 65 64 20 74 6f 20 74 65 73 74 20  ot need to test 
2272d 66 6f 72 20 61 20 48 41 56 49 4e 47 20 63 6c 61  for a HAVING cla
2272e 75 73 65 2e 20 20 49 66 20 48 41 56 49 4e 47 20  use.  If HAVING 
2272f 69 73 20 70 72 65 73 65 6e 74 20 62 75 74 0a 20  is present but. 
22730 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e 6f 20   ** there is no 
22731 4f 52 44 45 52 20 42 59 2c 20 77 65 20 77 69 6c  ORDER BY, we wil
22732 6c 20 67 65 74 20 61 6e 20 65 72 72 6f 72 2e 20  l get an error. 
22733 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63 74  */.  if( pSelect
22734 2d 3e 70 47 72 6f 75 70 42 79 20 29 7b 0a 20 20  ->pGroupBy ){.  
22735 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a    return 0;   /*
22736 20 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20   SELECT may not 
22737 68 61 76 65 20 61 20 47 52 4f 55 50 20 42 59 20  have a GROUP BY 
22738 63 6c 61 75 73 65 20 2a 2f 0a 20 20 7d 0a 20 20  clause */.  }.  
22739 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 70 4c 69  if( pSelect->pLi
2273a 6d 69 74 20 29 7b 0a 20 20 20 20 72 65 74 75 72  mit ){.    retur
2273b 6e 20 30 3b 20 20 20 2f 2a 20 53 45 4c 45 43 54  n 0;   /* SELECT
2273c 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 61 20   may not have a 
2273d 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 2a 2f 0a  LIMIT clause */.
2273e 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70 53    }.  assert( pS
2273f 65 6c 65 63 74 2d 3e 70 4f 66 66 73 65 74 3d 3d  elect->pOffset==
22740 30 20 29 3b 20 20 2f 2a 20 4d 75 73 74 20 62 65  0 );  /* Must be
22741 20 73 6f 20 69 66 20 70 4c 69 6d 69 74 3d 3d 30   so if pLimit==0
22742 20 2a 2f 0a 20 20 69 66 28 20 70 53 65 6c 65 63   */.  if( pSelec
22743 74 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  t->pPrior ){.   
22744 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20   return 0;   /* 
22745 53 45 4c 45 43 54 20 6d 61 79 20 6e 6f 74 20 62  SELECT may not b
22746 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20 71 75 65  e a compound que
22747 72 79 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20  ry */.  }.  if( 
22748 70 53 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67  pSelect->selFlag
22749 73 20 26 20 53 46 5f 44 69 73 74 69 6e 63 74 20  s & SF_Distinct 
2274a 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2274b 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 6d 61 79     /* SELECT may
2274c 20 6e 6f 74 20 62 65 20 44 49 53 54 49 4e 43 54   not be DISTINCT
2274d 20 2a 2f 0a 20 20 7d 0a 20 20 70 45 4c 69 73 74   */.  }.  pEList
2274e 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 45 4c 69   = pSelect->pELi
2274f 73 74 3b 0a 20 20 61 73 73 65 72 74 28 20 70 45  st;.  assert( pE
22750 4c 69 73 74 21 3d 30 20 29 3b 0a 20 20 69 66 28  List!=0 );.  if(
22751 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d   pEList->nExpr!=
22752 31 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  1 ){.    return 
22753 30 3b 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75  0;   /* The resu
22754 6c 74 20 73 65 74 20 6d 75 73 74 20 68 61 76 65  lt set must have
22755 20 65 78 61 63 74 6c 79 20 6f 6e 65 20 63 6f 6c   exactly one col
22756 75 6d 6e 20 2a 2f 0a 20 20 7d 0a 20 20 61 73 73  umn */.  }.  ass
22757 65 72 74 28 20 70 45 4c 69 73 74 2d 3e 61 5b 30  ert( pEList->a[0
22758 5d 2e 70 45 78 70 72 20 29 3b 0a 20 20 69 66 28  ].pExpr );.  if(
22759 20 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70 45   pEList->a[0].pE
2275a 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 20  xpr->op!=TK_ALL 
2275b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2275c 20 20 20 2f 2a 20 54 68 65 20 72 65 73 75 6c 74     /* The result
2275d 20 73 65 74 20 6d 75 73 74 20 62 65 20 74 68 65   set must be the
2275e 20 73 70 65 63 69 61 6c 20 6f 70 65 72 61 74 6f   special operato
2275f 72 20 22 2a 22 20 2a 2f 0a 20 20 7d 0a 0a 20 20  r "*" */.  }..  
22760 2f 2a 20 41 74 20 74 68 69 73 20 70 6f 69 6e 74  /* At this point
22761 20 77 65 20 68 61 76 65 20 65 73 74 61 62 6c 69   we have establi
22762 73 68 65 64 20 74 68 61 74 20 74 68 65 20 73 74  shed that the st
22763 61 74 65 6d 65 6e 74 20 69 73 20 6f 66 20 74 68  atement is of th
22764 65 0a 20 20 2a 2a 20 63 6f 72 72 65 63 74 20 73  e.  ** correct s
22765 79 6e 74 61 63 74 69 63 20 66 6f 72 6d 20 74 6f  yntactic form to
22766 20 70 61 72 74 69 63 69 70 61 74 65 20 69 6e 20   participate in 
22767 74 68 69 73 20 6f 70 74 69 6d 69 7a 61 74 69 6f  this optimizatio
22768 6e 2e 20 20 4e 6f 77 0a 20 20 2a 2a 20 77 65 20  n.  Now.  ** we 
22769 68 61 76 65 20 74 6f 20 63 68 65 63 6b 20 74 68  have to check th
2276a 65 20 73 65 6d 61 6e 74 69 63 73 2e 0a 20 20 2a  e semantics..  *
2276b 2f 0a 20 20 70 49 74 65 6d 20 3d 20 70 53 65 6c  /.  pItem = pSel
2276c 65 63 74 2d 3e 70 53 72 63 2d 3e 61 3b 0a 20 20  ect->pSrc->a;.  
2276d 70 53 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f  pSrc = sqlite3Lo
2276e 63 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  cateTable(pParse
2276f 2c 20 30 2c 20 70 49 74 65 6d 2d 3e 7a 4e 61 6d  , 0, pItem->zNam
22770 65 2c 20 70 49 74 65 6d 2d 3e 7a 44 61 74 61 62  e, pItem->zDatab
22771 61 73 65 29 3b 0a 20 20 69 66 28 20 70 53 72 63  ase);.  if( pSrc
22772 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
22773 6e 20 30 3b 20 20 20 2f 2a 20 46 52 4f 4d 20 63  n 0;   /* FROM c
22774 6c 61 75 73 65 20 64 6f 65 73 20 6e 6f 74 20 63  lause does not c
22775 6f 6e 74 61 69 6e 20 61 20 72 65 61 6c 20 74 61  ontain a real ta
22776 62 6c 65 20 2a 2f 0a 20 20 7d 0a 20 20 69 66 28  ble */.  }.  if(
22777 20 70 53 72 63 3d 3d 70 44 65 73 74 20 29 7b 0a   pSrc==pDest ){.
22778 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20      return 0;   
22779 2f 2a 20 74 61 62 31 20 61 6e 64 20 74 61 62 32  /* tab1 and tab2
2277a 20 6d 61 79 20 6e 6f 74 20 62 65 20 74 68 65 20   may not be the 
2277b 73 61 6d 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20  same table */.  
2277c 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  }.#ifndef SQLITE
2277d 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2277e 4c 45 0a 20 20 69 66 28 20 70 53 72 63 2d 3e 74  LE.  if( pSrc->t
2277f 61 62 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72  abFlags & TF_Vir
22780 74 75 61 6c 20 29 7b 0a 20 20 20 20 72 65 74 75  tual ){.    retu
22781 72 6e 20 30 3b 20 20 20 2f 2a 20 74 61 62 32 20  rn 0;   /* tab2 
22782 6d 75 73 74 20 6e 6f 74 20 62 65 20 61 20 76 69  must not be a vi
22783 72 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20  rtual table */. 
22784 20 7d 0a 23 65 6e 64 69 66 0a 20 20 69 66 28 20   }.#endif.  if( 
22785 70 53 72 63 2d 3e 70 53 65 6c 65 63 74 20 29 7b  pSrc->pSelect ){
22786 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20  .    return 0;  
22787 20 2f 2a 20 74 61 62 32 20 6d 61 79 20 6e 6f 74   /* tab2 may not
22788 20 62 65 20 61 20 76 69 65 77 20 2a 2f 0a 20 20   be a view */.  
22789 7d 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 6e  }.  if( pDest->n
2278a 43 6f 6c 21 3d 70 53 72 63 2d 3e 6e 43 6f 6c 20  Col!=pSrc->nCol 
2278b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
2278c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
2278d 63 6f 6c 75 6d 6e 73 20 6d 75 73 74 20 62 65 20  columns must be 
2278e 74 68 65 20 73 61 6d 65 20 69 6e 20 74 61 62 31  the same in tab1
2278f 20 61 6e 64 20 74 61 62 32 20 2a 2f 0a 20 20 7d   and tab2 */.  }
22790 0a 20 20 69 66 28 20 70 44 65 73 74 2d 3e 69 50  .  if( pDest->iP
22791 4b 65 79 21 3d 70 53 72 63 2d 3e 69 50 4b 65 79  Key!=pSrc->iPKey
22792 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
22793 3b 20 20 20 2f 2a 20 42 6f 74 68 20 74 61 62 6c  ;   /* Both tabl
22794 65 73 20 6d 75 73 74 20 68 61 76 65 20 74 68 65  es must have the
22795 20 73 61 6d 65 20 49 4e 54 45 47 45 52 20 50 52   same INTEGER PR
22796 49 4d 41 52 59 20 4b 45 59 20 2a 2f 0a 20 20 7d  IMARY KEY */.  }
22797 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 44  .  for(i=0; i<pD
22798 65 73 74 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  est->nCol; i++){
22799 0a 20 20 20 20 69 66 28 20 70 44 65 73 74 2d 3e  .    if( pDest->
2279a 61 43 6f 6c 5b 69 5d 2e 61 66 66 69 6e 69 74 79  aCol[i].affinity
2279b 21 3d 70 53 72 63 2d 3e 61 43 6f 6c 5b 69 5d 2e  !=pSrc->aCol[i].
2279c 61 66 66 69 6e 69 74 79 20 29 7b 0a 20 20 20 20  affinity ){.    
2279d 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 2f    return 0;    /
2279e 2a 20 41 66 66 69 6e 69 74 79 20 6d 75 73 74 20  * Affinity must 
2279f 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20 61  be the same on a
227a0 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20  ll columns */.  
227a1 20 20 7d 0a 20 20 20 20 69 66 28 20 21 78 66 65    }.    if( !xfe
227a2 72 43 6f 6d 70 61 74 69 62 6c 65 43 6f 6c 6c 61  rCompatibleColla
227a3 74 69 6f 6e 28 70 44 65 73 74 2d 3e 61 43 6f 6c  tion(pDest->aCol
227a4 5b 69 5d 2e 7a 43 6f 6c 6c 2c 20 70 53 72 63 2d  [i].zColl, pSrc-
227a5 3e 61 43 6f 6c 5b 69 5d 2e 7a 43 6f 6c 6c 29 20  >aCol[i].zColl) 
227a6 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
227a7 30 3b 20 20 20 20 2f 2a 20 43 6f 6c 6c 61 74 69  0;    /* Collati
227a8 6e 67 20 73 65 71 75 65 6e 63 65 20 6d 75 73 74  ng sequence must
227a9 20 62 65 20 74 68 65 20 73 61 6d 65 20 6f 6e 20   be the same on 
227aa 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20  all columns */. 
227ab 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 44 65     }.    if( pDe
227ac 73 74 2d 3e 61 43 6f 6c 5b 69 5d 2e 6e 6f 74 4e  st->aCol[i].notN
227ad 75 6c 6c 20 26 26 20 21 70 53 72 63 2d 3e 61 43  ull && !pSrc->aC
227ae 6f 6c 5b 69 5d 2e 6e 6f 74 4e 75 6c 6c 20 29 7b  ol[i].notNull ){
227af 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  .      return 0;
227b0 20 20 20 20 2f 2a 20 74 61 62 32 20 6d 75 73 74      /* tab2 must
227b1 20 62 65 20 4e 4f 54 20 4e 55 4c 4c 20 69 66 20   be NOT NULL if 
227b2 74 61 62 31 20 69 73 20 2a 2f 0a 20 20 20 20 7d  tab1 is */.    }
227b3 0a 20 20 7d 0a 20 20 66 6f 72 28 70 44 65 73 74  .  }.  for(pDest
227b4 49 64 78 3d 70 44 65 73 74 2d 3e 70 49 6e 64 65  Idx=pDest->pInde
227b5 78 3b 20 70 44 65 73 74 49 64 78 3b 20 70 44 65  x; pDestIdx; pDe
227b6 73 74 49 64 78 3d 70 44 65 73 74 49 64 78 2d 3e  stIdx=pDestIdx->
227b7 70 4e 65 78 74 29 7b 0a 20 20 20 20 69 66 28 20  pNext){.    if( 
227b8 70 44 65 73 74 49 64 78 2d 3e 6f 6e 45 72 72 6f  pDestIdx->onErro
227b9 72 21 3d 4f 45 5f 4e 6f 6e 65 20 29 7b 0a 20 20  r!=OE_None ){.  
227ba 20 20 20 20 64 65 73 74 48 61 73 55 6e 69 71 75      destHasUniqu
227bb 65 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  eIdx = 1;.    }.
227bc 20 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d      for(pSrcIdx=
227bd 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53  pSrc->pIndex; pS
227be 72 63 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70  rcIdx; pSrcIdx=p
227bf 53 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  SrcIdx->pNext){.
227c0 20 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f        if( xferCo
227c1 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44  mpatibleIndex(pD
227c2 65 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29  estIdx, pSrcIdx)
227c3 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
227c4 20 20 20 20 69 66 28 20 70 53 72 63 49 64 78 3d      if( pSrcIdx=
227c5 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75  =0 ){.      retu
227c6 72 6e 20 30 3b 20 20 20 20 2f 2a 20 70 44 65 73  rn 0;    /* pDes
227c7 74 49 64 78 20 68 61 73 20 6e 6f 20 63 6f 72 72  tIdx has no corr
227c8 65 73 70 6f 6e 64 69 6e 67 20 69 6e 64 65 78 20  esponding index 
227c9 69 6e 20 70 53 72 63 20 2a 2f 0a 20 20 20 20 7d  in pSrc */.    }
227ca 0a 20 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c  .  }.#ifndef SQL
227cb 49 54 45 5f 4f 4d 49 54 5f 43 48 45 43 4b 0a 20  ITE_OMIT_CHECK. 
227cc 20 69 66 28 20 70 44 65 73 74 2d 3e 70 43 68 65   if( pDest->pChe
227cd 63 6b 20 26 26 20 21 73 71 6c 69 74 65 33 45 78  ck && !sqlite3Ex
227ce 70 72 43 6f 6d 70 61 72 65 28 70 53 72 63 2d 3e  prCompare(pSrc->
227cf 70 43 68 65 63 6b 2c 20 70 44 65 73 74 2d 3e 70  pCheck, pDest->p
227d0 43 68 65 63 6b 29 20 29 7b 0a 20 20 20 20 72 65  Check) ){.    re
227d1 74 75 72 6e 20 30 3b 20 20 20 2f 2a 20 54 61 62  turn 0;   /* Tab
227d2 6c 65 73 20 68 61 76 65 20 64 69 66 66 65 72 65  les have differe
227d3 6e 74 20 43 48 45 43 4b 20 63 6f 6e 73 74 72 61  nt CHECK constra
227d4 69 6e 74 73 2e 20 20 54 69 63 6b 65 74 20 23 32  ints.  Ticket #2
227d5 32 35 32 20 2a 2f 0a 20 20 7d 0a 23 65 6e 64 69  252 */.  }.#endi
227d6 66 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 67 65  f..  /* If we ge
227d7 74 20 74 68 69 73 20 66 61 72 2c 20 69 74 20 6d  t this far, it m
227d8 65 61 6e 73 20 65 69 74 68 65 72 3a 0a 20 20 2a  eans either:.  *
227d9 2a 0a 20 20 2a 2a 20 20 20 20 2a 20 20 20 57 65  *.  **    *   We
227da 20 63 61 6e 20 61 6c 77 61 79 73 20 64 6f 20 74   can always do t
227db 68 65 20 74 72 61 6e 73 66 65 72 20 69 66 20 74  he transfer if t
227dc 68 65 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e  he table contain
227dd 73 20 61 6e 0a 20 20 2a 2a 20 20 20 20 20 20 20  s an.  **       
227de 20 61 6e 20 69 6e 74 65 67 65 72 20 70 72 69 6d   an integer prim
227df 61 72 79 20 6b 65 79 0a 20 20 2a 2a 0a 20 20 2a  ary key.  **.  *
227e0 2a 20 20 20 20 2a 20 20 20 57 65 20 63 61 6e 20  *    *   We can 
227e1 63 6f 6e 64 69 74 69 6f 6e 61 6c 6c 79 20 64 6f  conditionally do
227e2 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 69 66   the transfer if
227e3 20 74 68 65 20 64 65 73 74 69 6e 61 74 69 6f 6e   the destination
227e4 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 74 61 62  .  **        tab
227e5 6c 65 20 69 73 20 65 6d 70 74 79 2e 0a 20 20 2a  le is empty..  *
227e6 2f 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  /.#ifdef SQLITE_
227e7 54 45 53 54 0a 20 20 73 71 6c 69 74 65 33 5f 78  TEST.  sqlite3_x
227e8 66 65 72 6f 70 74 5f 63 6f 75 6e 74 2b 2b 3b 0a  feropt_count++;.
227e9 23 65 6e 64 69 66 0a 20 20 69 44 62 53 72 63 20  #endif.  iDbSrc 
227ea 3d 20 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54  = sqlite3SchemaT
227eb 6f 49 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64  oIndex(pParse->d
227ec 62 2c 20 70 53 72 63 2d 3e 70 53 63 68 65 6d 61  b, pSrc->pSchema
227ed 29 3b 0a 20 20 76 20 3d 20 73 71 6c 69 74 65 33  );.  v = sqlite3
227ee 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29 3b  GetVdbe(pParse);
227ef 0a 20 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65  .  sqlite3CodeVe
227f0 72 69 66 79 53 63 68 65 6d 61 28 70 50 61 72 73  rifySchema(pPars
227f1 65 2c 20 69 44 62 53 72 63 29 3b 0a 20 20 69 53  e, iDbSrc);.  iS
227f2 72 63 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  rc = pParse->nTa
227f3 62 2b 2b 3b 0a 20 20 69 44 65 73 74 20 3d 20 70  b++;.  iDest = p
227f4 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
227f5 20 72 65 67 41 75 74 6f 69 6e 63 20 3d 20 61 75   regAutoinc = au
227f6 74 6f 49 6e 63 42 65 67 69 6e 28 70 50 61 72 73  toIncBegin(pPars
227f7 65 2c 20 69 44 62 44 65 73 74 2c 20 70 44 65 73  e, iDbDest, pDes
227f8 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 4f 70 65  t);.  sqlite3Ope
227f9 6e 54 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69  nTable(pParse, i
227fa 44 65 73 74 2c 20 69 44 62 44 65 73 74 2c 20 70  Dest, iDbDest, p
227fb 44 65 73 74 2c 20 4f 50 5f 4f 70 65 6e 57 72 69  Dest, OP_OpenWri
227fc 74 65 29 3b 0a 20 20 69 66 28 20 28 70 44 65 73  te);.  if( (pDes
227fd 74 2d 3e 69 50 4b 65 79 3c 30 20 26 26 20 70 44  t->iPKey<0 && pD
227fe 65 73 74 2d 3e 70 49 6e 64 65 78 21 3d 30 29 20  est->pIndex!=0) 
227ff 7c 7c 20 64 65 73 74 48 61 73 55 6e 69 71 75 65  || destHasUnique
22800 49 64 78 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66  Idx ){.    /* If
22801 20 74 61 62 6c 65 73 20 64 6f 20 6e 6f 74 20 68   tables do not h
22802 61 76 65 20 61 6e 20 49 4e 54 45 47 45 52 20 50  ave an INTEGER P
22803 52 49 4d 41 52 59 20 4b 45 59 20 61 6e 64 20 74  RIMARY KEY and t
22804 68 65 72 65 0a 20 20 20 20 2a 2a 20 61 72 65 20  here.    ** are 
22805 69 6e 64 69 63 65 73 20 74 6f 20 62 65 20 63 6f  indices to be co
22806 70 69 65 64 20 61 6e 64 20 74 68 65 20 64 65 73  pied and the des
22807 74 69 6e 61 74 69 6f 6e 20 69 73 20 6e 6f 74 20  tination is not 
22808 65 6d 70 74 79 2c 0a 20 20 20 20 2a 2a 20 77 65  empty,.    ** we
22809 20 68 61 76 65 20 74 6f 20 64 69 73 61 6c 6c 6f   have to disallo
2280a 77 20 74 68 65 20 74 72 61 6e 73 66 65 72 20 6f  w the transfer o
2280b 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62 65 63 61  ptimization beca
2280c 75 73 65 20 74 68 65 0a 20 20 20 20 2a 2a 20 74  use the.    ** t
2280d 68 65 20 72 6f 77 69 64 73 20 6d 69 67 68 74 20  he rowids might 
2280e 63 68 61 6e 67 65 20 77 68 69 63 68 20 77 69 6c  change which wil
2280f 6c 20 6d 65 73 73 20 75 70 20 69 6e 64 65 78 69  l mess up indexi
22810 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20 20 20 2a  ng..    **.    *
22811 2a 20 4f 72 20 69 66 20 74 68 65 20 64 65 73 74  * Or if the dest
22812 69 6e 61 74 69 6f 6e 20 68 61 73 20 61 20 55 4e  ination has a UN
22813 49 51 55 45 20 69 6e 64 65 78 20 61 6e 64 20 69  IQUE index and i
22814 73 20 6e 6f 74 20 65 6d 70 74 79 2c 0a 20 20 20  s not empty,.   
22815 20 2a 2a 20 77 65 20 61 6c 73 6f 20 64 69 73 61   ** we also disa
22816 6c 6c 6f 77 20 74 68 65 20 74 72 61 6e 73 66 65  llow the transfe
22817 72 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 20 62  r optimization b
22818 65 63 61 75 73 65 20 77 65 20 63 61 6e 6e 6f 74  ecause we cannot
22819 0a 20 20 20 20 2a 2a 20 69 6e 73 75 72 65 20 74  .    ** insure t
2281a 68 61 74 20 61 6c 6c 20 65 6e 74 72 69 65 73 20  hat all entries 
2281b 69 6e 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20  in the union of 
2281c 44 45 53 54 20 61 6e 64 20 53 52 43 20 77 69 6c  DEST and SRC wil
2281d 6c 20 62 65 0a 20 20 20 20 2a 2a 20 75 6e 69 71  l be.    ** uniq
2281e 75 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  ue..    */.    a
2281f 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64  ddr1 = sqlite3Vd
22820 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
22821 65 77 69 6e 64 2c 20 69 44 65 73 74 2c 20 30 29  ewind, iDest, 0)
22822 3b 0a 20 20 20 20 65 6d 70 74 79 44 65 73 74 54  ;.    emptyDestT
22823 65 73 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  est = sqlite3Vdb
22824 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f  eAddOp2(v, OP_Go
22825 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  to, 0, 0);.    s
22826 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65  qlite3VdbeJumpHe
22827 72 65 28 76 2c 20 61 64 64 72 31 29 3b 0a 20 20  re(v, addr1);.  
22828 7d 65 6c 73 65 7b 0a 20 20 20 20 65 6d 70 74 79  }else{.    empty
22829 44 65 73 74 54 65 73 74 20 3d 20 30 3b 0a 20 20  DestTest = 0;.  
2282a 7d 0a 20 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  }.  sqlite3OpenT
2282b 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 53 72  able(pParse, iSr
2282c 63 2c 20 69 44 62 53 72 63 2c 20 70 53 72 63 2c  c, iDbSrc, pSrc,
2282d 20 4f 50 5f 4f 70 65 6e 52 65 61 64 29 3b 0a 20   OP_OpenRead);. 
2282e 20 65 6d 70 74 79 53 72 63 54 65 73 74 20 3d 20   emptySrcTest = 
2282f 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22830 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
22831 69 53 72 63 2c 20 30 29 3b 0a 20 20 72 65 67 44  iSrc, 0);.  regD
22832 61 74 61 20 3d 20 73 71 6c 69 74 65 33 47 65 74  ata = sqlite3Get
22833 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
22834 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20 73 71  .  regRowid = sq
22835 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
22836 70 50 61 72 73 65 29 3b 0a 20 20 69 66 28 20 70  pParse);.  if( p
22837 44 65 73 74 2d 3e 69 50 4b 65 79 3e 3d 30 20 29  Dest->iPKey>=0 )
22838 7b 0a 20 20 20 20 61 64 64 72 31 20 3d 20 73 71  {.    addr1 = sq
22839 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2283a 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69 53 72  v, OP_Rowid, iSr
2283b 63 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20  c, regRowid);.  
2283c 20 20 61 64 64 72 32 20 3d 20 73 71 6c 69 74 65    addr2 = sqlite
2283d 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
2283e 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69 44 65  P_NotExists, iDe
2283f 73 74 2c 20 30 2c 20 72 65 67 52 6f 77 69 64 29  st, 0, regRowid)
22840 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
22841 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 48 61  eAddOp4(v, OP_Ha
22842 6c 74 2c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54  lt, SQLITE_CONST
22843 52 41 49 4e 54 2c 20 6f 6e 45 72 72 6f 72 2c 20  RAINT, onError, 
22844 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  0,.             
22845 20 20 20 20 20 20 20 20 20 22 50 52 49 4d 41 52           "PRIMAR
22846 59 20 4b 45 59 20 6d 75 73 74 20 62 65 20 75 6e  Y KEY must be un
22847 69 71 75 65 22 2c 20 50 34 5f 53 54 41 54 49 43  ique", P4_STATIC
22848 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22849 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
2284a 64 72 32 29 3b 0a 20 20 20 20 61 75 74 6f 49 6e  dr2);.    autoIn
2284b 63 53 74 65 70 28 70 50 61 72 73 65 2c 20 72 65  cStep(pParse, re
2284c 67 41 75 74 6f 69 6e 63 2c 20 72 65 67 52 6f 77  gAutoinc, regRow
2284d 69 64 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  id);.  }else if(
2284e 20 70 44 65 73 74 2d 3e 70 49 6e 64 65 78 3d 3d   pDest->pIndex==
2284f 30 20 29 7b 0a 20 20 20 20 61 64 64 72 31 20 3d  0 ){.    addr1 =
22850 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
22851 70 32 28 76 2c 20 4f 50 5f 4e 65 77 52 6f 77 69  p2(v, OP_NewRowi
22852 64 2c 20 69 44 65 73 74 2c 20 72 65 67 52 6f 77  d, iDest, regRow
22853 69 64 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  id);.  }else{.  
22854 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65    addr1 = sqlite
22855 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
22856 50 5f 52 6f 77 69 64 2c 20 69 53 72 63 2c 20 72  P_Rowid, iSrc, r
22857 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 61 73  egRowid);.    as
22858 73 65 72 74 28 20 28 70 44 65 73 74 2d 3e 74 61  sert( (pDest->ta
22859 62 46 6c 61 67 73 20 26 20 54 46 5f 41 75 74 6f  bFlags & TF_Auto
2285a 69 6e 63 72 65 6d 65 6e 74 29 3d 3d 30 20 29 3b  increment)==0 );
2285b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 64  .  }.  sqlite3Vd
2285c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
2285d 6f 77 44 61 74 61 2c 20 69 53 72 63 2c 20 72 65  owData, iSrc, re
2285e 67 44 61 74 61 29 3b 0a 20 20 73 71 6c 69 74 65  gData);.  sqlite
2285f 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f  3VdbeAddOp3(v, O
22860 50 5f 49 6e 73 65 72 74 2c 20 69 44 65 73 74 2c  P_Insert, iDest,
22861 20 72 65 67 44 61 74 61 2c 20 72 65 67 52 6f 77   regData, regRow
22862 69 64 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  id);.  sqlite3Vd
22863 62 65 43 68 61 6e 67 65 50 35 28 76 2c 20 4f 50  beChangeP5(v, OP
22864 46 4c 41 47 5f 4e 43 48 41 4e 47 45 7c 4f 50 46  FLAG_NCHANGE|OPF
22865 4c 41 47 5f 4c 41 53 54 52 4f 57 49 44 7c 4f 50  LAG_LASTROWID|OP
22866 46 4c 41 47 5f 41 50 50 45 4e 44 29 3b 0a 20 20  FLAG_APPEND);.  
22867 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
22868 65 50 34 28 76 2c 20 2d 31 2c 20 70 44 65 73 74  eP4(v, -1, pDest
22869 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 73  ->zName, 0);.  s
2286a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2286b 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69 53 72  (v, OP_Next, iSr
2286c 63 2c 20 61 64 64 72 31 29 3b 0a 20 20 61 75 74  c, addr1);.  aut
2286d 6f 49 6e 63 45 6e 64 28 70 50 61 72 73 65 2c 20  oIncEnd(pParse, 
2286e 69 44 62 44 65 73 74 2c 20 70 44 65 73 74 2c 20  iDbDest, pDest, 
2286f 72 65 67 41 75 74 6f 69 6e 63 29 3b 0a 20 20 66  regAutoinc);.  f
22870 6f 72 28 70 44 65 73 74 49 64 78 3d 70 44 65 73  or(pDestIdx=pDes
22871 74 2d 3e 70 49 6e 64 65 78 3b 20 70 44 65 73 74  t->pIndex; pDest
22872 49 64 78 3b 20 70 44 65 73 74 49 64 78 3d 70 44  Idx; pDestIdx=pD
22873 65 73 74 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  estIdx->pNext){.
22874 20 20 20 20 66 6f 72 28 70 53 72 63 49 64 78 3d      for(pSrcIdx=
22875 70 53 72 63 2d 3e 70 49 6e 64 65 78 3b 20 70 53  pSrc->pIndex; pS
22876 72 63 49 64 78 3b 20 70 53 72 63 49 64 78 3d 70  rcIdx; pSrcIdx=p
22877 53 72 63 49 64 78 2d 3e 70 4e 65 78 74 29 7b 0a  SrcIdx->pNext){.
22878 20 20 20 20 20 20 69 66 28 20 78 66 65 72 43 6f        if( xferCo
22879 6d 70 61 74 69 62 6c 65 49 6e 64 65 78 28 70 44  mpatibleIndex(pD
2287a 65 73 74 49 64 78 2c 20 70 53 72 63 49 64 78 29  estIdx, pSrcIdx)
2287b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a   ) break;.    }.
2287c 20 20 20 20 61 73 73 65 72 74 28 20 70 53 72 63      assert( pSrc
2287d 49 64 78 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Idx );.    sqlit
2287e 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2287f 4f 50 5f 43 6c 6f 73 65 2c 20 69 53 72 63 2c 20  OP_Close, iSrc, 
22880 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  0);.    sqlite3V
22881 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
22882 43 6c 6f 73 65 2c 20 69 44 65 73 74 2c 20 30 29  Close, iDest, 0)
22883 3b 0a 20 20 20 20 70 4b 65 79 20 3d 20 73 71 6c  ;.    pKey = sql
22884 69 74 65 33 49 6e 64 65 78 4b 65 79 69 6e 66 6f  ite3IndexKeyinfo
22885 28 70 50 61 72 73 65 2c 20 70 53 72 63 49 64 78  (pParse, pSrcIdx
22886 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
22887 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
22888 70 65 6e 52 65 61 64 2c 20 69 53 72 63 2c 20 70  penRead, iSrc, p
22889 53 72 63 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44  SrcIdx->tnum, iD
2288a 62 53 72 63 2c 0a 20 20 20 20 20 20 20 20 20 20  bSrc,.          
2288b 20 20 20 20 20 20 20 20 20 20 20 20 28 63 68 61              (cha
2288c 72 2a 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49  r*)pKey, P4_KEYI
2288d 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20  NFO_HANDOFF);.  
2288e 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
2288f 2c 20 22 25 73 22 2c 20 70 53 72 63 49 64 78 2d  , "%s", pSrcIdx-
22890 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 70 4b  >zName));.    pK
22891 65 79 20 3d 20 73 71 6c 69 74 65 33 49 6e 64 65  ey = sqlite3Inde
22892 78 4b 65 79 69 6e 66 6f 28 70 50 61 72 73 65 2c  xKeyinfo(pParse,
22893 20 70 44 65 73 74 49 64 78 29 3b 0a 20 20 20 20   pDestIdx);.    
22894 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
22895 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57 72 69 74  4(v, OP_OpenWrit
22896 65 2c 20 69 44 65 73 74 2c 20 70 44 65 73 74 49  e, iDest, pDestI
22897 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 44 65 73  dx->tnum, iDbDes
22898 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
22899 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
2289a 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  pKey, P4_KEYINFO
2289b 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 56  _HANDOFF);.    V
2289c 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2289d 25 73 22 2c 20 70 44 65 73 74 49 64 78 2d 3e 7a  %s", pDestIdx->z
2289e 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61 64 64 72  Name));.    addr
2289f 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41  1 = sqlite3VdbeA
228a0 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69  ddOp2(v, OP_Rewi
228a1 6e 64 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20  nd, iSrc, 0);.  
228a2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
228a3 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 4b 65 79  Op2(v, OP_RowKey
228a4 2c 20 69 53 72 63 2c 20 72 65 67 44 61 74 61 29  , iSrc, regData)
228a5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
228a6 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 64  eAddOp3(v, OP_Id
228a7 78 49 6e 73 65 72 74 2c 20 69 44 65 73 74 2c 20  xInsert, iDest, 
228a8 72 65 67 44 61 74 61 2c 20 31 29 3b 0a 20 20 20  regData, 1);.   
228a9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
228aa 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 69  p2(v, OP_Next, i
228ab 53 72 63 2c 20 61 64 64 72 31 2b 31 29 3b 0a 20  Src, addr1+1);. 
228ac 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75     sqlite3VdbeJu
228ad 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29  mpHere(v, addr1)
228ae 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56  ;.  }.  sqlite3V
228af 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65  dbeJumpHere(v, e
228b0 6d 70 74 79 53 72 63 54 65 73 74 29 3b 0a 20 20  mptySrcTest);.  
228b1 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65  sqlite3ReleaseTe
228b2 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65  mpReg(pParse, re
228b3 67 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74  gRowid);.  sqlit
228b4 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
228b5 28 70 50 61 72 73 65 2c 20 72 65 67 44 61 74 61  (pParse, regData
228b6 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
228b7 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
228b8 73 65 2c 20 69 53 72 63 2c 20 30 29 3b 0a 20 20  se, iSrc, 0);.  
228b9 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
228ba 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 69  2(v, OP_Close, i
228bb 44 65 73 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  Dest, 0);.  if( 
228bc 65 6d 70 74 79 44 65 73 74 54 65 73 74 20 29 7b  emptyDestTest ){
228bd 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
228be 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c  AddOp2(v, OP_Hal
228bf 74 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 30 29  t, SQLITE_OK, 0)
228c0 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
228c1 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 65 6d 70  eJumpHere(v, emp
228c2 74 79 44 65 73 74 54 65 73 74 29 3b 0a 20 20 20  tyDestTest);.   
228c3 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
228c4 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20  p2(v, OP_Close, 
228c5 69 44 65 73 74 2c 20 30 29 3b 0a 20 20 20 20 72  iDest, 0);.    r
228c6 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65 6c 73 65  eturn 0;.  }else
228c7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  {.    return 1;.
228c8 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
228c9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 58 46 45 52  SQLITE_OMIT_XFER
228ca 5f 4f 50 54 20 2a 2f 0a 0a 2f 2a 20 4d 61 6b 65  _OPT */../* Make
228cb 20 73 75 72 65 20 22 69 73 56 69 65 77 22 20 67   sure "isView" g
228cc 65 74 73 20 75 6e 64 65 66 69 6e 65 64 20 69 6e  ets undefined in
228cd 20 63 61 73 65 20 74 68 69 73 20 66 69 6c 65 20   case this file 
228ce 62 65 63 6f 6d 65 73 20 70 61 72 74 20 6f 66 0a  becomes part of.
228cf 2a 2a 20 74 68 65 20 61 6d 61 6c 67 61 6d 61 74  ** the amalgamat
228d0 69 6f 6e 20 2d 20 73 6f 20 74 68 61 74 20 73 75  ion - so that su
228d1 62 73 65 71 75 65 6e 74 20 66 69 6c 65 73 20 64  bsequent files d
228d2 6f 20 6e 6f 74 20 73 65 65 20 69 73 56 69 65 77  o not see isView
228d3 20 61 73 20 61 0a 2a 2a 20 6d 61 63 72 6f 2e 20   as a.** macro. 
228d4 2a 2f 0a 23 75 6e 64 65 66 20 69 73 56 69 65 77  */.#undef isView
228d5 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
228d6 2a 20 45 6e 64 20 6f 66 20 69 6e 73 65 72 74 2e  * End of insert.
228d7 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
228d8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228d9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228da 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
228db 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 6c 65 67  * Begin file leg
228dc 61 63 79 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  acy.c **********
228dd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228de 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228df 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
228e0 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
228e1 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
228e2 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
228e3 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
228e4 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
228e5 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
228e6 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
228e7 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
228e8 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
228e9 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
228ea 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
228eb 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
228ec 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
228ed 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
228ee 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
228ef 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
228f0 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
228f1 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
228f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
228f6 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 4d 61 69 6e  ********.** Main
228f7 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20 53 51   file for the SQ
228f8 4c 69 74 65 20 6c 69 62 72 61 72 79 2e 20 20 54  Lite library.  T
228f9 68 65 20 72 6f 75 74 69 6e 65 73 20 69 6e 20 74  he routines in t
228fa 68 69 73 20 66 69 6c 65 0a 2a 2a 20 69 6d 70 6c  his file.** impl
228fb 65 6d 65 6e 74 20 74 68 65 20 70 72 6f 67 72 61  ement the progra
228fc 6d 6d 65 72 20 69 6e 74 65 72 66 61 63 65 20 74  mmer interface t
228fd 6f 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20 20  o the library.  
228fe 52 6f 75 74 69 6e 65 73 20 69 6e 0a 2a 2a 20 6f  Routines in.** o
228ff 74 68 65 72 20 66 69 6c 65 73 20 61 72 65 20 66  ther files are f
22900 6f 72 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20  or internal use 
22901 62 79 20 53 51 4c 69 74 65 20 61 6e 64 20 73 68  by SQLite and sh
22902 6f 75 6c 64 20 6e 6f 74 20 62 65 0a 2a 2a 20 61  ould not be.** a
22903 63 63 65 73 73 65 64 20 62 79 20 75 73 65 72 73  ccessed by users
22904 20 6f 66 20 74 68 65 20 6c 69 62 72 61 72 79 2e   of the library.
22905 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 6c 65 67 61  .**.** $Id: lega
22906 63 79 2e 63 2c 76 20 31 2e 32 39 20 32 30 30 38  cy.c,v 1.29 2008
22907 2f 30 38 2f 30 32 20 30 33 3a 35 30 3a 33 39 20  /08/02 03:50:39 
22908 64 72 68 20 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f  drh Exp $.*/.../
22909 2a 0a 2a 2a 20 45 78 65 63 75 74 65 20 53 51 4c  *.** Execute SQL
2290a 20 63 6f 64 65 2e 20 20 52 65 74 75 72 6e 20 6f   code.  Return o
2290b 6e 65 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45  ne of the SQLITE
2290c 5f 20 73 75 63 63 65 73 73 2f 66 61 69 6c 75 72  _ success/failur
2290d 65 0a 2a 2a 20 63 6f 64 65 73 2e 20 20 41 6c 73  e.** codes.  Als
2290e 6f 20 77 72 69 74 65 20 61 6e 20 65 72 72 6f 72  o write an error
2290f 20 6d 65 73 73 61 67 65 20 69 6e 74 6f 20 6d 65   message into me
22910 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 20 66 72  mory obtained fr
22911 6f 6d 0a 2a 2a 20 6d 61 6c 6c 6f 63 28 29 20 61  om.** malloc() a
22912 6e 64 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73  nd make *pzErrMs
22913 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 20  g point to that 
22914 6d 65 73 73 61 67 65 2e 0a 2a 2a 0a 2a 2a 20 49  message..**.** I
22915 66 20 74 68 65 20 53 51 4c 20 69 73 20 61 20 71  f the SQL is a q
22916 75 65 72 79 2c 20 74 68 65 6e 20 66 6f 72 20 65  uery, then for e
22917 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 71  ach row in the q
22918 75 65 72 79 20 72 65 73 75 6c 74 0a 2a 2a 20 74  uery result.** t
22919 68 65 20 78 43 61 6c 6c 62 61 63 6b 28 29 20 66  he xCallback() f
2291a 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65  unction is calle
2291b 64 2e 20 20 70 41 72 67 20 62 65 63 6f 6d 65 73  d.  pArg becomes
2291c 20 74 68 65 20 66 69 72 73 74 0a 2a 2a 20 61 72   the first.** ar
2291d 67 75 6d 65 6e 74 20 74 6f 20 78 43 61 6c 6c 62  gument to xCallb
2291e 61 63 6b 28 29 2e 20 20 49 66 20 78 43 61 6c 6c  ack().  If xCall
2291f 62 61 63 6b 3d 4e 55 4c 4c 20 74 68 65 6e 20 6e  back=NULL then n
22920 6f 20 63 61 6c 6c 62 61 63 6b 0a 2a 2a 20 69 73  o callback.** is
22921 20 69 6e 76 6f 6b 65 64 2c 20 65 76 65 6e 20 66   invoked, even f
22922 6f 72 20 71 75 65 72 69 65 73 2e 0a 2a 2f 0a 53  or queries..*/.S
22923 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
22924 6c 69 74 65 33 5f 65 78 65 63 28 0a 20 20 73 71  lite3_exec(.  sq
22925 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
22926 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
22927 20 64 61 74 61 62 61 73 65 20 6f 6e 20 77 68 69   database on whi
22928 63 68 20 74 68 65 20 53 51 4c 20 65 78 65 63 75  ch the SQL execu
22929 74 65 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  tes */.  const c
2292a 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20  har *zSql,      
2292b 20 20 20 20 20 2f 2a 20 54 68 65 20 53 51 4c 20       /* The SQL 
2292c 74 6f 20 62 65 20 65 78 65 63 75 74 65 64 20 2a  to be executed *
2292d 2f 0a 20 20 73 71 6c 69 74 65 33 5f 63 61 6c 6c  /.  sqlite3_call
2292e 62 61 63 6b 20 78 43 61 6c 6c 62 61 63 6b 2c 20  back xCallback, 
2292f 2f 2a 20 49 6e 76 6f 6b 65 20 74 68 69 73 20 63  /* Invoke this c
22930 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69 6e 65 20  allback routine 
22931 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 2c  */.  void *pArg,
22932 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22933 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
22934 6e 74 20 74 6f 20 78 43 61 6c 6c 62 61 63 6b 28  nt to xCallback(
22935 29 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  ) */.  char **pz
22936 45 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20  ErrMsg          
22937 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f     /* Write erro
22938 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20  r messages here 
22939 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  */.){.  int rc =
2293a 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f   SQLITE_OK;.  co
2293b 6e 73 74 20 63 68 61 72 20 2a 7a 4c 65 66 74 6f  nst char *zLefto
2293c 76 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  ver;.  sqlite3_s
2293d 74 6d 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a  tmt *pStmt = 0;.
2293e 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 73 20    char **azCols 
2293f 3d 20 30 3b 0a 0a 20 20 69 6e 74 20 6e 52 65 74  = 0;..  int nRet
22940 72 79 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 43  ry = 0;.  int nC
22941 61 6c 6c 62 61 63 6b 3b 0a 0a 20 20 69 66 28 20  allback;..  if( 
22942 7a 53 71 6c 3d 3d 30 20 29 20 7a 53 71 6c 20 3d  zSql==0 ) zSql =
22943 20 22 22 3b 0a 0a 20 20 73 71 6c 69 74 65 33 5f   "";..  sqlite3_
22944 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
22945 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74 65  mutex);.  sqlite
22946 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49 54  3Error(db, SQLIT
22947 45 5f 4f 4b 2c 20 30 29 3b 0a 20 20 77 68 69 6c  E_OK, 0);.  whil
22948 65 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  e( (rc==SQLITE_O
22949 4b 20 7c 7c 20 28 72 63 3d 3d 53 51 4c 49 54 45  K || (rc==SQLITE
2294a 5f 53 43 48 45 4d 41 20 26 26 20 28 2b 2b 6e 52  _SCHEMA && (++nR
2294b 65 74 72 79 29 3c 32 29 29 20 26 26 20 7a 53 71  etry)<2)) && zSq
2294c 6c 5b 30 5d 20 29 7b 0a 20 20 20 20 69 6e 74 20  l[0] ){.    int 
2294d 6e 43 6f 6c 3b 0a 20 20 20 20 63 68 61 72 20 2a  nCol;.    char *
2294e 2a 61 7a 56 61 6c 73 20 3d 20 30 3b 0a 0a 20 20  *azVals = 0;..  
2294f 20 20 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20    pStmt = 0;.   
22950 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
22951 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
22952 2d 31 2c 20 26 70 53 74 6d 74 2c 20 26 7a 4c 65  -1, &pStmt, &zLe
22953 66 74 6f 76 65 72 29 3b 0a 20 20 20 20 61 73 73  ftover);.    ass
22954 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f  ert( rc==SQLITE_
22955 4f 4b 20 7c 7c 20 70 53 74 6d 74 3d 3d 30 20 29  OK || pStmt==0 )
22956 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
22957 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
22958 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
22959 0a 20 20 20 20 69 66 28 20 21 70 53 74 6d 74 20  .    if( !pStmt 
2295a 29 7b 0a 20 20 20 20 20 20 2f 2a 20 74 68 69 73  ){.      /* this
2295b 20 68 61 70 70 65 6e 73 20 66 6f 72 20 61 20 63   happens for a c
2295c 6f 6d 6d 65 6e 74 20 6f 72 20 77 68 69 74 65 2d  omment or white-
2295d 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 7a  space */.      z
2295e 53 71 6c 20 3d 20 7a 4c 65 66 74 6f 76 65 72 3b  Sql = zLeftover;
2295f 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b  .      continue;
22960 0a 20 20 20 20 7d 0a 0a 20 20 20 20 6e 43 61 6c  .    }..    nCal
22961 6c 62 61 63 6b 20 3d 20 30 3b 0a 20 20 20 20 6e  lback = 0;.    n
22962 43 6f 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  Col = sqlite3_co
22963 6c 75 6d 6e 5f 63 6f 75 6e 74 28 70 53 74 6d 74  lumn_count(pStmt
22964 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 31  );..    while( 1
22965 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 3b   ){.      int i;
22966 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69  .      rc = sqli
22967 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 3b  te3_step(pStmt);
22968 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 76 6f 6b  ..      /* Invok
22969 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 66  e the callback f
2296a 75 6e 63 74 69 6f 6e 20 69 66 20 72 65 71 75 69  unction if requi
2296b 72 65 64 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  red */.      if(
2296c 20 78 43 61 6c 6c 62 61 63 6b 20 26 26 20 28 53   xCallback && (S
2296d 51 4c 49 54 45 5f 52 4f 57 3d 3d 72 63 20 7c 7c  QLITE_ROW==rc ||
2296e 20 0a 20 20 20 20 20 20 20 20 20 20 28 53 51 4c   .          (SQL
2296f 49 54 45 5f 44 4f 4e 45 3d 3d 72 63 20 26 26 20  ITE_DONE==rc && 
22970 21 6e 43 61 6c 6c 62 61 63 6b 20 26 26 20 64 62  !nCallback && db
22971 2d 3e 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 4e  ->flags&SQLITE_N
22972 75 6c 6c 43 61 6c 6c 62 61 63 6b 29 29 20 29 7b  ullCallback)) ){
22973 0a 20 20 20 20 20 20 20 20 69 66 28 20 30 3d 3d  .        if( 0==
22974 6e 43 61 6c 6c 62 61 63 6b 20 29 7b 0a 20 20 20  nCallback ){.   
22975 20 20 20 20 20 20 20 69 66 28 20 61 7a 43 6f 6c         if( azCol
22976 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  s==0 ){.        
22977 20 20 20 20 61 7a 43 6f 6c 73 20 3d 20 73 71 6c      azCols = sql
22978 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
22979 28 64 62 2c 20 32 2a 6e 43 6f 6c 2a 73 69 7a 65  (db, 2*nCol*size
2297a 6f 66 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20  of(const char*) 
2297b 2b 20 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20  + 1);.          
2297c 20 20 69 66 28 20 61 7a 43 6f 6c 73 3d 3d 30 20    if( azCols==0 
2297d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2297e 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a   goto exec_out;.
2297f 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
22980 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
22981 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
22982 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  Col; i++){.     
22983 20 20 20 20 20 20 20 61 7a 43 6f 6c 73 5b 69 5d         azCols[i]
22984 20 3d 20 28 63 68 61 72 20 2a 29 73 71 6c 69 74   = (char *)sqlit
22985 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 28 70  e3_column_name(p
22986 53 74 6d 74 2c 20 69 29 3b 0a 20 20 20 20 20 20  Stmt, i);.      
22987 20 20 20 20 20 20 2f 2a 20 73 71 6c 69 74 65 33        /* sqlite3
22988 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 29  VdbeSetColName()
22989 20 69 6e 73 74 61 6c 6c 73 20 63 6f 6c 75 6d 6e   installs column
2298a 20 6e 61 6d 65 73 20 61 73 20 55 54 46 38 0a 20   names as UTF8. 
2298b 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73 74             ** st
2298c 72 69 6e 67 73 20 73 6f 20 74 68 65 72 65 20 69  rings so there i
2298d 73 20 6e 6f 20 77 61 79 20 66 6f 72 20 73 71 6c  s no way for sql
2298e 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
2298f 28 29 20 74 6f 20 66 61 69 6c 2e 20 2a 2f 0a 20  () to fail. */. 
22990 20 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72             asser
22991 74 28 20 61 7a 43 6f 6c 73 5b 69 5d 21 3d 30 20  t( azCols[i]!=0 
22992 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20  );.          }. 
22993 20 20 20 20 20 20 20 20 20 6e 43 61 6c 6c 62 61           nCallba
22994 63 6b 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ck++;.        }.
22995 20 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d          if( rc==
22996 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20  SQLITE_ROW ){.  
22997 20 20 20 20 20 20 20 20 61 7a 56 61 6c 73 20 3d          azVals =
22998 20 26 61 7a 43 6f 6c 73 5b 6e 43 6f 6c 5d 3b 0a   &azCols[nCol];.
22999 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d            for(i=
2299a 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29 7b  0; i<nCol; i++){
2299b 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 7a 56  .            azV
2299c 61 6c 73 5b 69 5d 20 3d 20 28 63 68 61 72 20 2a  als[i] = (char *
2299d 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f  )sqlite3_column_
2299e 74 65 78 74 28 70 53 74 6d 74 2c 20 69 29 3b 0a  text(pStmt, i);.
2299f 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20              if( 
229a0 21 61 7a 56 61 6c 73 5b 69 5d 20 26 26 20 73 71  !azVals[i] && sq
229a1 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
229a2 65 28 70 53 74 6d 74 2c 20 69 29 21 3d 53 51 4c  e(pStmt, i)!=SQL
229a3 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20 20 20  ITE_NULL ){.    
229a4 20 20 20 20 20 20 20 20 20 20 64 62 2d 3e 6d 61            db->ma
229a5 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
229a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 67 6f                go
229a7 74 6f 20 65 78 65 63 5f 6f 75 74 3b 0a 20 20 20  to exec_out;.   
229a8 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
229a9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
229aa 0a 20 20 20 20 20 20 20 20 69 66 28 20 78 43 61  .        if( xCa
229ab 6c 6c 62 61 63 6b 28 70 41 72 67 2c 20 6e 43 6f  llback(pArg, nCo
229ac 6c 2c 20 61 7a 56 61 6c 73 2c 20 61 7a 43 6f 6c  l, azVals, azCol
229ad 73 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  s) ){.          
229ae 72 63 20 3d 20 53 51 4c 49 54 45 5f 41 42 4f 52  rc = SQLITE_ABOR
229af 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  T;.          sql
229b0 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
229b1 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20  tmt);.          
229b2 70 53 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20  pStmt = 0;.     
229b3 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
229b4 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 41 42 4f  r(db, SQLITE_ABO
229b5 52 54 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  RT, 0);.        
229b6 20 20 67 6f 74 6f 20 65 78 65 63 5f 6f 75 74 3b    goto exec_out;
229b7 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
229b8 20 7d 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63   }..      if( rc
229b9 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  !=SQLITE_ROW ){.
229ba 20 20 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c          rc = sql
229bb 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
229bc 74 6d 74 29 3b 0a 20 20 20 20 20 20 20 20 70 53  tmt);.        pS
229bd 74 6d 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  tmt = 0;.       
229be 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
229bf 53 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 20 20  SCHEMA ){.      
229c0 20 20 20 20 6e 52 65 74 72 79 20 3d 20 30 3b 0a      nRetry = 0;.
229c1 20 20 20 20 20 20 20 20 20 20 7a 53 71 6c 20 3d            zSql =
229c2 20 7a 4c 65 66 74 6f 76 65 72 3b 0a 20 20 20 20   zLeftover;.    
229c3 20 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 73        while( iss
229c4 70 61 63 65 28 28 75 6e 73 69 67 6e 65 64 20 63  pace((unsigned c
229c5 68 61 72 29 7a 53 71 6c 5b 30 5d 29 20 29 20 7a  har)zSql[0]) ) z
229c6 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d  Sql++;.        }
229c7 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
229c8 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
229c9 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
229ca 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b 0a 20 20  (db, azCols);.  
229cb 20 20 61 7a 43 6f 6c 73 20 3d 20 30 3b 0a 20 20    azCols = 0;.  
229cc 7d 0a 0a 65 78 65 63 5f 6f 75 74 3a 0a 20 20 69  }..exec_out:.  i
229cd 66 28 20 70 53 74 6d 74 20 29 20 73 71 6c 69 74  f( pStmt ) sqlit
229ce 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d  e3_finalize(pStm
229cf 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  t);.  sqlite3DbF
229d0 72 65 65 28 64 62 2c 20 61 7a 43 6f 6c 73 29 3b  ree(db, azCols);
229d1 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
229d2 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
229d3 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
229d4 45 5f 4f 4b 20 26 26 20 72 63 3d 3d 73 71 6c 69  E_OK && rc==sqli
229d5 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62 29 20  te3_errcode(db) 
229d6 26 26 20 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20  && pzErrMsg ){. 
229d7 20 20 20 69 6e 74 20 6e 45 72 72 4d 73 67 20 3d     int nErrMsg =
229d8 20 31 20 2b 20 73 74 72 6c 65 6e 28 73 71 6c 69   1 + strlen(sqli
229d9 74 65 33 5f 65 72 72 6d 73 67 28 64 62 29 29 3b  te3_errmsg(db));
229da 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d  .    *pzErrMsg =
229db 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 6e   sqlite3Malloc(n
229dc 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69 66 28  ErrMsg);.    if(
229dd 20 2a 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20   *pzErrMsg ){.  
229de 20 20 20 20 6d 65 6d 63 70 79 28 2a 70 7a 45 72      memcpy(*pzEr
229df 72 4d 73 67 2c 20 73 71 6c 69 74 65 33 5f 65 72  rMsg, sqlite3_er
229e0 72 6d 73 67 28 64 62 29 2c 20 6e 45 72 72 4d 73  rmsg(db), nErrMs
229e1 67 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  g);.    }.  }els
229e2 65 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29  e if( pzErrMsg )
229e3 7b 0a 20 20 20 20 2a 70 7a 45 72 72 4d 73 67 20  {.    *pzErrMsg 
229e4 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65  = 0;.  }..  asse
229e5 72 74 28 20 28 72 63 26 64 62 2d 3e 65 72 72 4d  rt( (rc&db->errM
229e6 61 73 6b 29 3d 3d 72 63 20 29 3b 0a 20 20 73 71  ask)==rc );.  sq
229e7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
229e8 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
229e9 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
229ea 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
229eb 64 20 6f 66 20 6c 65 67 61 63 79 2e 63 20 2a 2a  d of legacy.c **
229ec 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229ed 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
229ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
229f0 67 69 6e 20 66 69 6c 65 20 6c 6f 61 64 65 78 74  gin file loadext
229f1 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
229f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
229f3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
229f4 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20 37 0a  .** 2006 June 7.
229f5 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
229f6 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
229f7 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
229f8 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
229f9 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
229fa 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
229fb 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
229fc 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
229fd 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
229fe 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
229ff 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
22a00 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
22a01 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
22a02 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
22a03 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
22a04 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
22a05 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
22a06 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
22a07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
22a0b 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74  * This file cont
22a0c 61 69 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74  ains code used t
22a0d 6f 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 6c 6f  o dynamically lo
22a0e 61 64 20 65 78 74 65 6e 73 69 6f 6e 73 20 69 6e  ad extensions in
22a0f 74 6f 0a 2a 2a 20 74 68 65 20 53 51 4c 69 74 65  to.** the SQLite
22a10 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20   library..**.** 
22a11 24 49 64 3a 20 6c 6f 61 64 65 78 74 2e 63 2c 76  $Id: loadext.c,v
22a12 20 31 2e 35 36 20 32 30 30 38 2f 31 30 2f 31 32   1.56 2008/10/12
22a13 20 30 30 3a 32 37 3a 35 33 20 73 68 61 6e 65 20   00:27:53 shane 
22a14 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65  Exp $.*/..#ifnde
22a15 66 20 53 51 4c 49 54 45 5f 43 4f 52 45 0a 20 20  f SQLITE_CORE.  
22a16 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 43  #define SQLITE_C
22a17 4f 52 45 20 31 20 20 2f 2a 20 44 69 73 61 62 6c  ORE 1  /* Disabl
22a18 65 20 74 68 65 20 41 50 49 20 72 65 64 65 66 69  e the API redefi
22a19 6e 69 74 69 6f 6e 20 69 6e 20 73 71 6c 69 74 65  nition in sqlite
22a1a 33 65 78 74 2e 68 20 2a 2f 0a 23 65 6e 64 69 66  3ext.h */.#endif
22a1b 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22a1c 20 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 33   Include sqlite3
22a1d 65 78 74 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ext.h in the mid
22a1e 64 6c 65 20 6f 66 20 6c 6f 61 64 65 78 74 2e 63  dle of loadext.c
22a1f 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f   **************/
22a20 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
22a21 20 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69   Begin file sqli
22a22 74 65 33 65 78 74 2e 68 20 2a 2a 2a 2a 2a 2a 2a  te3ext.h *******
22a23 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a24 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
22a25 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65  ./*.** 2006 June
22a26 20 37 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74   7.**.** The aut
22a27 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
22a28 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
22a29 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
22a2a 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
22a2b 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
22a2c 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
22a2d 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
22a2e 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
22a2f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
22a30 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
22a31 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
22a32 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
22a33 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
22a34 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
22a35 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
22a36 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
22a37 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
22a38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a3b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22a3c 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72  *.** This header
22a3d 20 66 69 6c 65 20 64 65 66 69 6e 65 73 20 74 68   file defines th
22a3e 65 20 53 51 4c 69 74 65 20 69 6e 74 65 72 66 61  e SQLite interfa
22a3f 63 65 20 66 6f 72 20 75 73 65 20 62 79 0a 2a 2a  ce for use by.**
22a40 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 69 65   shared librarie
22a41 73 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 62  s that want to b
22a42 65 20 69 6d 70 6f 72 74 65 64 20 61 73 20 65 78  e imported as ex
22a43 74 65 6e 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a  tensions into.**
22a44 20 61 6e 20 53 51 4c 69 74 65 20 69 6e 73 74 61   an SQLite insta
22a45 6e 63 65 2e 20 20 53 68 61 72 65 64 20 6c 69 62  nce.  Shared lib
22a46 72 61 72 69 65 73 20 74 68 61 74 20 69 6e 74 65  raries that inte
22a47 6e 64 20 74 6f 20 62 65 20 6c 6f 61 64 65 64 0a  nd to be loaded.
22a48 2a 2a 20 61 73 20 65 78 74 65 6e 73 69 6f 6e 73  ** as extensions
22a49 20 62 79 20 53 51 4c 69 74 65 20 73 68 6f 75 6c   by SQLite shoul
22a4a 64 20 23 69 6e 63 6c 75 64 65 20 74 68 69 73 20  d #include this 
22a4b 66 69 6c 65 20 69 6e 73 74 65 61 64 20 6f 66 20  file instead of 
22a4c 0a 2a 2a 20 73 71 6c 69 74 65 33 2e 68 2e 0a 2a  .** sqlite3.h..*
22a4d 2a 0a 2a 2a 20 40 28 23 29 20 24 49 64 3a 20 73  *.** @(#) $Id: s
22a4e 71 6c 69 74 65 33 65 78 74 2e 68 2c 76 20 31 2e  qlite3ext.h,v 1.
22a4f 32 35 20 32 30 30 38 2f 31 30 2f 31 32 20 30 30  25 2008/10/12 00
22a50 3a 32 37 3a 35 34 20 73 68 61 6e 65 20 45 78 70  :27:54 shane Exp
22a51 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 53   $.*/.#ifndef _S
22a52 51 4c 49 54 45 33 45 58 54 5f 48 5f 0a 23 64 65  QLITE3EXT_H_.#de
22a53 66 69 6e 65 20 5f 53 51 4c 49 54 45 33 45 58 54  fine _SQLITE3EXT
22a54 5f 48 5f 0a 0a 74 79 70 65 64 65 66 20 73 74 72  _H_..typedef str
22a55 75 63 74 20 73 71 6c 69 74 65 33 5f 61 70 69 5f  uct sqlite3_api_
22a56 72 6f 75 74 69 6e 65 73 20 73 71 6c 69 74 65 33  routines sqlite3
22a57 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 3b 0a 0a  _api_routines;..
22a58 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
22a59 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 68 6f  ing structure ho
22a5a 6c 64 73 20 70 6f 69 6e 74 65 72 73 20 74 6f 20  lds pointers to 
22a5b 61 6c 6c 20 6f 66 20 74 68 65 20 53 51 4c 69 74  all of the SQLit
22a5c 65 20 41 50 49 0a 2a 2a 20 72 6f 75 74 69 6e 65  e API.** routine
22a5d 73 2e 0a 2a 2a 0a 2a 2a 20 57 41 52 4e 49 4e 47  s..**.** WARNING
22a5e 3a 20 20 49 6e 20 6f 72 64 65 72 20 74 6f 20 6d  :  In order to m
22a5f 61 69 6e 74 61 69 6e 20 62 61 63 6b 77 61 72 64  aintain backward
22a60 73 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2c  s compatibility,
22a61 20 61 64 64 20 6e 65 77 0a 2a 2a 20 69 6e 74 65   add new.** inte
22a62 72 66 61 63 65 73 20 74 6f 20 74 68 65 20 65 6e  rfaces to the en
22a63 64 20 6f 66 20 74 68 69 73 20 73 74 72 75 63 74  d of this struct
22a64 75 72 65 20 6f 6e 6c 79 2e 20 20 49 66 20 79 6f  ure only.  If yo
22a65 75 20 69 6e 73 65 72 74 20 6e 65 77 0a 2a 2a 20  u insert new.** 
22a66 69 6e 74 65 72 66 61 63 65 73 20 69 6e 20 74 68  interfaces in th
22a67 65 20 6d 69 64 64 6c 65 20 6f 66 20 74 68 69 73  e middle of this
22a68 20 73 74 72 75 63 74 75 72 65 2c 20 74 68 65 6e   structure, then
22a69 20 6f 6c 64 65 72 20 64 69 66 66 65 72 65 6e 74   older different
22a6a 0a 2a 2a 20 76 65 72 73 69 6f 6e 73 20 6f 66 20  .** versions of 
22a6b 53 51 4c 69 74 65 20 77 69 6c 6c 20 6e 6f 74 20  SQLite will not 
22a6c 62 65 20 61 62 6c 65 20 74 6f 20 6c 6f 61 64 20  be able to load 
22a6d 65 61 63 68 20 6f 74 68 65 72 73 27 20 73 68 61  each others' sha
22a6e 72 65 64 0a 2a 2a 20 6c 69 62 72 61 72 69 65 73  red.** libraries
22a6f 21 0a 2a 2f 0a 73 74 72 75 63 74 20 73 71 6c 69  !.*/.struct sqli
22a70 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
22a71 20 7b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 61 67   {.  void * (*ag
22a72 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 29  gregate_context)
22a73 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22a74 2a 2c 69 6e 74 20 6e 42 79 74 65 73 29 3b 0a 20  *,int nBytes);. 
22a75 20 69 6e 74 20 20 28 2a 61 67 67 72 65 67 61 74   int  (*aggregat
22a76 65 5f 63 6f 75 6e 74 29 28 73 71 6c 69 74 65 33  e_count)(sqlite3
22a77 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 69 6e  _context*);.  in
22a78 74 20 20 28 2a 62 69 6e 64 5f 62 6c 6f 62 29 28  t  (*bind_blob)(
22a79 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e  sqlite3_stmt*,in
22a7a 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  t,const void*,in
22a7b 74 20 6e 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64  t n,void(*)(void
22a7c 2a 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62 69  *));.  int  (*bi
22a7d 6e 64 5f 64 6f 75 62 6c 65 29 28 73 71 6c 69 74  nd_double)(sqlit
22a7e 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 64 6f 75  e3_stmt*,int,dou
22a7f 62 6c 65 29 3b 0a 20 20 69 6e 74 20 20 28 2a 62  ble);.  int  (*b
22a80 69 6e 64 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  ind_int)(sqlite3
22a81 5f 73 74 6d 74 2a 2c 69 6e 74 2c 69 6e 74 29 3b  _stmt*,int,int);
22a82 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 69  .  int  (*bind_i
22a83 6e 74 36 34 29 28 73 71 6c 69 74 65 33 5f 73 74  nt64)(sqlite3_st
22a84 6d 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 5f 69  mt*,int,sqlite_i
22a85 6e 74 36 34 29 3b 0a 20 20 69 6e 74 20 20 28 2a  nt64);.  int  (*
22a86 62 69 6e 64 5f 6e 75 6c 6c 29 28 73 71 6c 69 74  bind_null)(sqlit
22a87 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
22a88 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72   int  (*bind_par
22a89 61 6d 65 74 65 72 5f 63 6f 75 6e 74 29 28 73 71  ameter_count)(sq
22a8a 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20  lite3_stmt*);.  
22a8b 69 6e 74 20 20 28 2a 62 69 6e 64 5f 70 61 72 61  int  (*bind_para
22a8c 6d 65 74 65 72 5f 69 6e 64 65 78 29 28 73 71 6c  meter_index)(sql
22a8d 69 74 65 33 5f 73 74 6d 74 2a 2c 63 6f 6e 73 74  ite3_stmt*,const
22a8e 20 63 68 61 72 2a 7a 4e 61 6d 65 29 3b 0a 20 20   char*zName);.  
22a8f 63 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 62  const char * (*b
22a90 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f 6e 61  ind_parameter_na
22a91 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  me)(sqlite3_stmt
22a92 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 20 28  *,int);.  int  (
22a93 2a 62 69 6e 64 5f 74 65 78 74 29 28 73 71 6c 69  *bind_text)(sqli
22a94 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 2c 63 6f  te3_stmt*,int,co
22a95 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 20 6e 2c  nst char*,int n,
22a96 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b  void(*)(void*));
22a97 0a 20 20 69 6e 74 20 20 28 2a 62 69 6e 64 5f 74  .  int  (*bind_t
22a98 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 73  ext16)(sqlite3_s
22a99 74 6d 74 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76  tmt*,int,const v
22a9a 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29  oid*,int,void(*)
22a9b 28 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e 74 20  (void*));.  int 
22a9c 20 28 2a 62 69 6e 64 5f 76 61 6c 75 65 29 28 73   (*bind_value)(s
22a9d 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
22a9e 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 76  ,const sqlite3_v
22a9f 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28  alue*);.  int  (
22aa0 2a 62 75 73 79 5f 68 61 6e 64 6c 65 72 29 28 73  *busy_handler)(s
22aa1 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76  qlite3*,int(*)(v
22aa2 6f 69 64 2a 2c 69 6e 74 29 2c 76 6f 69 64 2a 29  oid*,int),void*)
22aa3 3b 0a 20 20 69 6e 74 20 20 28 2a 62 75 73 79 5f  ;.  int  (*busy_
22aa4 74 69 6d 65 6f 75 74 29 28 73 71 6c 69 74 65 33  timeout)(sqlite3
22aa5 2a 2c 69 6e 74 20 6d 73 29 3b 0a 20 20 69 6e 74  *,int ms);.  int
22aa6 20 20 28 2a 63 68 61 6e 67 65 73 29 28 73 71 6c    (*changes)(sql
22aa7 69 74 65 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28  ite3*);.  int  (
22aa8 2a 63 6c 6f 73 65 29 28 73 71 6c 69 74 65 33 2a  *close)(sqlite3*
22aa9 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c  );.  int  (*coll
22aaa 61 74 69 6f 6e 5f 6e 65 65 64 65 64 29 28 73 71  ation_needed)(sq
22aab 6c 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69  lite3*,void*,voi
22aac 64 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74  d(*)(void*,sqlit
22aad 65 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70  e3*,int eTextRep
22aae 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 29 3b 0a  ,const char*));.
22aaf 20 20 69 6e 74 20 20 28 2a 63 6f 6c 6c 61 74 69    int  (*collati
22ab0 6f 6e 5f 6e 65 65 64 65 64 31 36 29 28 73 71 6c  on_needed16)(sql
22ab1 69 74 65 33 2a 2c 76 6f 69 64 2a 2c 76 6f 69 64  ite3*,void*,void
22ab2 28 2a 29 28 76 6f 69 64 2a 2c 73 71 6c 69 74 65  (*)(void*,sqlite
22ab3 33 2a 2c 69 6e 74 20 65 54 65 78 74 52 65 70 2c  3*,int eTextRep,
22ab4 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20  const void*));. 
22ab5 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
22ab6 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 29 28 73 71 6c  column_blob)(sql
22ab7 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
22ab8 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
22ab9 6f 6c 75 6d 6e 5f 62 79 74 65 73 29 28 73 71 6c  olumn_bytes)(sql
22aba 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69  ite3_stmt*,int i
22abb 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  Col);.  int  (*c
22abc 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 29 28 73  olumn_bytes16)(s
22abd 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
22abe 20 69 43 6f 6c 29 3b 0a 20 20 69 6e 74 20 20 28   iCol);.  int  (
22abf 2a 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 29 28 73  *column_count)(s
22ac0 71 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d  qlite3_stmt*pStm
22ac1 74 29 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  t);.  const char
22ac2 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 61 74 61   * (*column_data
22ac3 62 61 73 65 5f 6e 61 6d 65 29 28 73 71 6c 69 74  base_name)(sqlit
22ac4 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20  e3_stmt*,int);. 
22ac5 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
22ac6 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f  column_database_
22ac7 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33 5f  name16)(sqlite3_
22ac8 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f  stmt*,int);.  co
22ac9 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  nst char * (*col
22aca 75 6d 6e 5f 64 65 63 6c 74 79 70 65 29 28 73 71  umn_decltype)(sq
22acb 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
22acc 69 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64  i);.  const void
22acd 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 64 65 63 6c   * (*column_decl
22ace 74 79 70 65 31 36 29 28 73 71 6c 69 74 65 33 5f  type16)(sqlite3_
22acf 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 64 6f  stmt*,int);.  do
22ad0 75 62 6c 65 20 20 28 2a 63 6f 6c 75 6d 6e 5f 64  uble  (*column_d
22ad1 6f 75 62 6c 65 29 28 73 71 6c 69 74 65 33 5f 73  ouble)(sqlite3_s
22ad2 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a  tmt*,int iCol);.
22ad3 20 20 69 6e 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f    int  (*column_
22ad4 69 6e 74 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  int)(sqlite3_stm
22ad5 74 2a 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20  t*,int iCol);.  
22ad6 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 20 28 2a  sqlite_int64  (*
22ad7 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 29 28 73 71  column_int64)(sq
22ad8 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20  lite3_stmt*,int 
22ad9 69 43 6f 6c 29 3b 0a 20 20 63 6f 6e 73 74 20 63  iCol);.  const c
22ada 68 61 72 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6e  har * (*column_n
22adb 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
22adc 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
22add 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
22ade 5f 6e 61 6d 65 31 36 29 28 73 71 6c 69 74 65 33  _name16)(sqlite3
22adf 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b 0a 20 20 63  _stmt*,int);.  c
22ae0 6f 6e 73 74 20 63 68 61 72 20 2a 20 28 2a 63 6f  onst char * (*co
22ae1 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65  lumn_origin_name
22ae2 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
22ae3 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f  int);.  const vo
22ae4 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 6f 72  id * (*column_or
22ae5 69 67 69 6e 5f 6e 61 6d 65 31 36 29 28 73 71 6c  igin_name16)(sql
22ae6 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 29 3b  ite3_stmt*,int);
22ae7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  .  const char * 
22ae8 28 2a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  (*column_table_n
22ae9 61 6d 65 29 28 73 71 6c 69 74 65 33 5f 73 74 6d  ame)(sqlite3_stm
22aea 74 2a 2c 69 6e 74 29 3b 0a 20 20 63 6f 6e 73 74  t*,int);.  const
22aeb 20 76 6f 69 64 20 2a 20 28 2a 63 6f 6c 75 6d 6e   void * (*column
22aec 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 29 28 73  _table_name16)(s
22aed 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
22aee 29 3b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69 67  );.  const unsig
22aef 6e 65 64 20 63 68 61 72 20 2a 20 28 2a 63 6f 6c  ned char * (*col
22af0 75 6d 6e 5f 74 65 78 74 29 28 73 71 6c 69 74 65  umn_text)(sqlite
22af1 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43 6f 6c  3_stmt*,int iCol
22af2 29 3b 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20  );.  const void 
22af3 2a 20 28 2a 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  * (*column_text1
22af4 36 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  6)(sqlite3_stmt*
22af5 2c 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 69 6e  ,int iCol);.  in
22af6 74 20 20 28 2a 63 6f 6c 75 6d 6e 5f 74 79 70 65  t  (*column_type
22af7 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c  )(sqlite3_stmt*,
22af8 69 6e 74 20 69 43 6f 6c 29 3b 0a 20 20 73 71 6c  int iCol);.  sql
22af9 69 74 65 33 5f 76 61 6c 75 65 2a 20 28 2a 63 6f  ite3_value* (*co
22afa 6c 75 6d 6e 5f 76 61 6c 75 65 29 28 73 71 6c 69  lumn_value)(sqli
22afb 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74 20 69 43  te3_stmt*,int iC
22afc 6f 6c 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a  ol);.  void * (*
22afd 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 29 28 73 71 6c  commit_hook)(sql
22afe 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28 76 6f 69  ite3*,int(*)(voi
22aff 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20 69 6e  d*),void*);.  in
22b00 74 20 20 28 2a 63 6f 6d 70 6c 65 74 65 29 28 63  t  (*complete)(c
22b01 6f 6e 73 74 20 63 68 61 72 2a 73 71 6c 29 3b 0a  onst char*sql);.
22b02 20 20 69 6e 74 20 20 28 2a 63 6f 6d 70 6c 65 74    int  (*complet
22b03 65 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  e16)(const void*
22b04 73 71 6c 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63  sql);.  int  (*c
22b05 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 29  reate_collation)
22b06 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
22b07 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c  char*,int,void*,
22b08 69 6e 74 28 2a 29 28 76 6f 69 64 2a 2c 69 6e 74  int(*)(void*,int
22b09 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
22b0a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29 29 3b 0a  ,const void*));.
22b0b 20 20 69 6e 74 20 20 28 2a 63 72 65 61 74 65 5f    int  (*create_
22b0c 63 6f 6c 6c 61 74 69 6f 6e 31 36 29 28 73 71 6c  collation16)(sql
22b0d 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64  ite3*,const void
22b0e 2a 2c 69 6e 74 2c 76 6f 69 64 2a 2c 69 6e 74 28  *,int,void*,int(
22b0f 2a 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  *)(void*,int,con
22b10 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  st void*,int,con
22b11 73 74 20 76 6f 69 64 2a 29 29 3b 0a 20 20 69 6e  st void*));.  in
22b12 74 20 20 28 2a 63 72 65 61 74 65 5f 66 75 6e 63  t  (*create_func
22b13 74 69 6f 6e 29 28 73 71 6c 69 74 65 33 2a 2c 63  tion)(sqlite3*,c
22b14 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 69  onst char*,int,i
22b15 6e 74 2c 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a  nt,void*,void (*
22b16 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
22b17 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
22b18 74 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69  te3_value**),voi
22b19 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69 74  d (*xStep)(sqlit
22b1a 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c  e3_context*,int,
22b1b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 2a 29  sqlite3_value**)
22b1c 2c 76 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28  ,void (*xFinal)(
22b1d 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
22b1e 29 29 3b 0a 20 20 69 6e 74 20 20 28 2a 63 72 65  ));.  int  (*cre
22b1f 61 74 65 5f 66 75 6e 63 74 69 6f 6e 31 36 29 28  ate_function16)(
22b20 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 76  sqlite3*,const v
22b21 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74 2c 76 6f 69  oid*,int,int,voi
22b22 64 2a 2c 76 6f 69 64 20 28 2a 78 46 75 6e 63 29  d*,void (*xFunc)
22b23 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22b24 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61  *,int,sqlite3_va
22b25 6c 75 65 2a 2a 29 2c 76 6f 69 64 20 28 2a 78 53  lue**),void (*xS
22b26 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  tep)(sqlite3_con
22b27 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
22b28 33 5f 76 61 6c 75 65 2a 2a 29 2c 76 6f 69 64 20  3_value**),void 
22b29 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c 69 74 65  (*xFinal)(sqlite
22b2a 33 5f 63 6f 6e 74 65 78 74 2a 29 29 3b 0a 20 20  3_context*));.  
22b2b 69 6e 74 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64  int (*create_mod
22b2c 75 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  ule)(sqlite3*,co
22b2d 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
22b2e 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c  sqlite3_module*,
22b2f 76 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28  void*);.  int  (
22b30 2a 64 61 74 61 5f 63 6f 75 6e 74 29 28 73 71 6c  *data_count)(sql
22b31 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
22b32 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 20 28 2a  ;.  sqlite3 * (*
22b33 64 62 5f 68 61 6e 64 6c 65 29 28 73 71 6c 69 74  db_handle)(sqlit
22b34 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
22b35 20 28 2a 64 65 63 6c 61 72 65 5f 76 74 61 62 29   (*declare_vtab)
22b36 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20  (sqlite3*,const 
22b37 63 68 61 72 2a 29 3b 0a 20 20 69 6e 74 20 20 28  char*);.  int  (
22b38 2a 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63  *enable_shared_c
22b39 61 63 68 65 29 28 69 6e 74 29 3b 0a 20 20 69 6e  ache)(int);.  in
22b3a 74 20 20 28 2a 65 72 72 63 6f 64 65 29 28 73 71  t  (*errcode)(sq
22b3b 6c 69 74 65 33 2a 64 62 29 3b 0a 20 20 63 6f 6e  lite3*db);.  con
22b3c 73 74 20 63 68 61 72 20 2a 20 28 2a 65 72 72 6d  st char * (*errm
22b3d 73 67 29 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20  sg)(sqlite3*);. 
22b3e 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
22b3f 65 72 72 6d 73 67 31 36 29 28 73 71 6c 69 74 65  errmsg16)(sqlite
22b40 33 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 65 78  3*);.  int  (*ex
22b41 65 63 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e  ec)(sqlite3*,con
22b42 73 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33  st char*,sqlite3
22b43 5f 63 61 6c 6c 62 61 63 6b 2c 76 6f 69 64 2a 2c  _callback,void*,
22b44 63 68 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20  char**);.  int  
22b45 28 2a 65 78 70 69 72 65 64 29 28 73 71 6c 69 74  (*expired)(sqlit
22b46 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 69 6e 74  e3_stmt*);.  int
22b47 20 20 28 2a 66 69 6e 61 6c 69 7a 65 29 28 73 71    (*finalize)(sq
22b48 6c 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74  lite3_stmt*pStmt
22b49 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 66 72 65  );.  void  (*fre
22b4a 65 29 28 76 6f 69 64 2a 29 3b 0a 20 20 76 6f 69  e)(void*);.  voi
22b4b 64 20 20 28 2a 66 72 65 65 5f 74 61 62 6c 65 29  d  (*free_table)
22b4c 28 63 68 61 72 2a 2a 72 65 73 75 6c 74 29 3b 0a  (char**result);.
22b4d 20 20 69 6e 74 20 20 28 2a 67 65 74 5f 61 75 74    int  (*get_aut
22b4e 6f 63 6f 6d 6d 69 74 29 28 73 71 6c 69 74 65 33  ocommit)(sqlite3
22b4f 2a 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 67  *);.  void * (*g
22b50 65 74 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69  et_auxdata)(sqli
22b51 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
22b52 29 3b 0a 20 20 69 6e 74 20 20 28 2a 67 65 74 5f  );.  int  (*get_
22b53 74 61 62 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c  table)(sqlite3*,
22b54 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 68 61 72  const char*,char
22b55 2a 2a 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 63 68  ***,int*,int*,ch
22b56 61 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  ar**);.  int  (*
22b57 67 6c 6f 62 61 6c 5f 72 65 63 6f 76 65 72 29 28  global_recover)(
22b58 76 6f 69 64 29 3b 0a 20 20 76 6f 69 64 20 20 28  void);.  void  (
22b59 2a 69 6e 74 65 72 72 75 70 74 78 29 28 73 71 6c  *interruptx)(sql
22b5a 69 74 65 33 2a 29 3b 0a 20 20 73 71 6c 69 74 65  ite3*);.  sqlite
22b5b 5f 69 6e 74 36 34 20 20 28 2a 6c 61 73 74 5f 69  _int64  (*last_i
22b5c 6e 73 65 72 74 5f 72 6f 77 69 64 29 28 73 71 6c  nsert_rowid)(sql
22b5d 69 74 65 33 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  ite3*);.  const 
22b5e 63 68 61 72 20 2a 20 28 2a 6c 69 62 76 65 72 73  char * (*libvers
22b5f 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20 20 69 6e  ion)(void);.  in
22b60 74 20 20 28 2a 6c 69 62 76 65 72 73 69 6f 6e 5f  t  (*libversion_
22b61 6e 75 6d 62 65 72 29 28 76 6f 69 64 29 3b 0a 20  number)(void);. 
22b62 20 76 6f 69 64 20 2a 28 2a 6d 61 6c 6c 6f 63 29   void *(*malloc)
22b63 28 69 6e 74 29 3b 0a 20 20 63 68 61 72 20 2a 20  (int);.  char * 
22b64 28 2a 6d 70 72 69 6e 74 66 29 28 63 6f 6e 73 74  (*mprintf)(const
22b65 20 63 68 61 72 2a 2c 2e 2e 2e 29 3b 0a 20 20 69   char*,...);.  i
22b66 6e 74 20 20 28 2a 6f 70 65 6e 29 28 63 6f 6e 73  nt  (*open)(cons
22b67 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
22b68 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 6f 70 65  *);.  int  (*ope
22b69 6e 31 36 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  n16)(const void*
22b6a 2c 73 71 6c 69 74 65 33 2a 2a 29 3b 0a 20 20 69  ,sqlite3**);.  i
22b6b 6e 74 20 20 28 2a 70 72 65 70 61 72 65 29 28 73  nt  (*prepare)(s
22b6c 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68  qlite3*,const ch
22b6d 61 72 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33 5f  ar*,int,sqlite3_
22b6e 73 74 6d 74 2a 2a 2c 63 6f 6e 73 74 20 63 68 61  stmt**,const cha
22b6f 72 2a 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 70  r**);.  int  (*p
22b70 72 65 70 61 72 65 31 36 29 28 73 71 6c 69 74 65  repare16)(sqlite
22b71 33 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69  3*,const void*,i
22b72 6e 74 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  nt,sqlite3_stmt*
22b73 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2a 29 3b  *,const void**);
22b74 0a 20 20 76 6f 69 64 20 2a 20 28 2a 70 72 6f 66  .  void * (*prof
22b75 69 6c 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ile)(sqlite3*,vo
22b76 69 64 28 2a 29 28 76 6f 69 64 2a 2c 63 6f 6e 73  id(*)(void*,cons
22b77 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75  t char*,sqlite_u
22b78 69 6e 74 36 34 29 2c 76 6f 69 64 2a 29 3b 0a 20  int64),void*);. 
22b79 20 76 6f 69 64 20 20 28 2a 70 72 6f 67 72 65 73   void  (*progres
22b7a 73 5f 68 61 6e 64 6c 65 72 29 28 73 71 6c 69 74  s_handler)(sqlit
22b7b 65 33 2a 2c 69 6e 74 2c 69 6e 74 28 2a 29 28 76  e3*,int,int(*)(v
22b7c 6f 69 64 2a 29 2c 76 6f 69 64 2a 29 3b 0a 20 20  oid*),void*);.  
22b7d 76 6f 69 64 20 2a 28 2a 72 65 61 6c 6c 6f 63 29  void *(*realloc)
22b7e 28 76 6f 69 64 2a 2c 69 6e 74 29 3b 0a 20 20 69  (void*,int);.  i
22b7f 6e 74 20 20 28 2a 72 65 73 65 74 29 28 73 71 6c  nt  (*reset)(sql
22b80 69 74 65 33 5f 73 74 6d 74 2a 70 53 74 6d 74 29  ite3_stmt*pStmt)
22b81 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73 75  ;.  void  (*resu
22b82 6c 74 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  lt_blob)(sqlite3
22b83 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20  _context*,const 
22b84 76 6f 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a  void*,int,void(*
22b85 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69  )(void*));.  voi
22b86 64 20 20 28 2a 72 65 73 75 6c 74 5f 64 6f 75 62  d  (*result_doub
22b87 6c 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  le)(sqlite3_cont
22b88 65 78 74 2a 2c 64 6f 75 62 6c 65 29 3b 0a 20 20  ext*,double);.  
22b89 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 65  void  (*result_e
22b8a 72 72 6f 72 29 28 73 71 6c 69 74 65 33 5f 63 6f  rror)(sqlite3_co
22b8b 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 63 68 61  ntext*,const cha
22b8c 72 2a 2c 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20  r*,int);.  void 
22b8d 20 28 2a 72 65 73 75 6c 74 5f 65 72 72 6f 72 31   (*result_error1
22b8e 36 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  6)(sqlite3_conte
22b8f 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  xt*,const void*,
22b90 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  int);.  void  (*
22b91 72 65 73 75 6c 74 5f 69 6e 74 29 28 73 71 6c 69  result_int)(sqli
22b92 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
22b93 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
22b94 75 6c 74 5f 69 6e 74 36 34 29 28 73 71 6c 69 74  ult_int64)(sqlit
22b95 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
22b96 74 65 5f 69 6e 74 36 34 29 3b 0a 20 20 76 6f 69  te_int64);.  voi
22b97 64 20 20 28 2a 72 65 73 75 6c 74 5f 6e 75 6c 6c  d  (*result_null
22b98 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  )(sqlite3_contex
22b99 74 2a 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72  t*);.  void  (*r
22b9a 65 73 75 6c 74 5f 74 65 78 74 29 28 73 71 6c 69  esult_text)(sqli
22b9b 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 63 6f 6e  te3_context*,con
22b9c 73 74 20 63 68 61 72 2a 2c 69 6e 74 2c 76 6f 69  st char*,int,voi
22b9d 64 28 2a 29 28 76 6f 69 64 2a 29 29 3b 0a 20 20  d(*)(void*));.  
22b9e 76 6f 69 64 20 20 28 2a 72 65 73 75 6c 74 5f 74  void  (*result_t
22b9f 65 78 74 31 36 29 28 73 71 6c 69 74 65 33 5f 63  ext16)(sqlite3_c
22ba0 6f 6e 74 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f  ontext*,const vo
22ba1 69 64 2a 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28  id*,int,void(*)(
22ba2 76 6f 69 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20  void*));.  void 
22ba3 20 28 2a 72 65 73 75 6c 74 5f 74 65 78 74 31 36   (*result_text16
22ba4 62 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  be)(sqlite3_cont
22ba5 65 78 74 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  ext*,const void*
22ba6 2c 69 6e 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69  ,int,void(*)(voi
22ba7 64 2a 29 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a  d*));.  void  (*
22ba8 72 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 29  result_text16le)
22ba9 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74  (sqlite3_context
22baa 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e  *,const void*,in
22bab 74 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29  t,void(*)(void*)
22bac 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 72 65 73  );.  void  (*res
22bad 75 6c 74 5f 76 61 6c 75 65 29 28 73 71 6c 69 74  ult_value)(sqlit
22bae 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 73 71 6c 69  e3_context*,sqli
22baf 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 76  te3_value*);.  v
22bb0 6f 69 64 20 2a 20 28 2a 72 6f 6c 6c 62 61 63 6b  oid * (*rollback
22bb1 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33 2a 2c  _hook)(sqlite3*,
22bb2 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 29 2c 76  void(*)(void*),v
22bb3 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
22bb4 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 29 28  set_authorizer)(
22bb5 73 71 6c 69 74 65 33 2a 2c 69 6e 74 28 2a 29 28  sqlite3*,int(*)(
22bb6 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20  void*,int,const 
22bb7 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
22bb8 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f  *,const char*,co
22bb9 6e 73 74 20 63 68 61 72 2a 29 2c 76 6f 69 64 2a  nst char*),void*
22bba 29 3b 0a 20 20 76 6f 69 64 20 20 28 2a 73 65 74  );.  void  (*set
22bbb 5f 61 75 78 64 61 74 61 29 28 73 71 6c 69 74 65  _auxdata)(sqlite
22bbc 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 76  3_context*,int,v
22bbd 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 29 28 76 6f  oid*,void (*)(vo
22bbe 69 64 2a 29 29 3b 0a 20 20 63 68 61 72 20 2a 20  id*));.  char * 
22bbf 28 2a 73 6e 70 72 69 6e 74 66 29 28 69 6e 74 2c  (*snprintf)(int,
22bc0 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72  char*,const char
22bc1 2a 2c 2e 2e 2e 29 3b 0a 20 20 69 6e 74 20 20 28  *,...);.  int  (
22bc2 2a 73 74 65 70 29 28 73 71 6c 69 74 65 33 5f 73  *step)(sqlite3_s
22bc3 74 6d 74 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  tmt*);.  int  (*
22bc4 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
22bc5 61 64 61 74 61 29 28 73 71 6c 69 74 65 33 2a 2c  adata)(sqlite3*,
22bc6 63 6f 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73  const char*,cons
22bc7 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68  t char*,const ch
22bc8 61 72 2a 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a  ar*,char const**
22bc9 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2a 2c 69 6e  ,char const**,in
22bca 74 2a 2c 69 6e 74 2a 2c 69 6e 74 2a 29 3b 0a 20  t*,int*,int*);. 
22bcb 20 76 6f 69 64 20 20 28 2a 74 68 72 65 61 64 5f   void  (*thread_
22bcc 63 6c 65 61 6e 75 70 29 28 76 6f 69 64 29 3b 0a  cleanup)(void);.
22bcd 20 20 69 6e 74 20 20 28 2a 74 6f 74 61 6c 5f 63    int  (*total_c
22bce 68 61 6e 67 65 73 29 28 73 71 6c 69 74 65 33 2a  hanges)(sqlite3*
22bcf 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 74 72  );.  void * (*tr
22bd0 61 63 65 29 28 73 71 6c 69 74 65 33 2a 2c 76 6f  ace)(sqlite3*,vo
22bd1 69 64 28 2a 78 54 72 61 63 65 29 28 76 6f 69 64  id(*xTrace)(void
22bd2 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 2c 76  *,const char*),v
22bd3 6f 69 64 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a  oid*);.  int  (*
22bd4 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67  transfer_binding
22bd5 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a  s)(sqlite3_stmt*
22bd6 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  ,sqlite3_stmt*);
22bd7 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 70 64 61  .  void * (*upda
22bd8 74 65 5f 68 6f 6f 6b 29 28 73 71 6c 69 74 65 33  te_hook)(sqlite3
22bd9 2a 2c 76 6f 69 64 28 2a 29 28 76 6f 69 64 2a 2c  *,void(*)(void*,
22bda 69 6e 74 20 2c 63 68 61 72 20 63 6f 6e 73 74 2a  int ,char const*
22bdb 2c 63 68 61 72 20 63 6f 6e 73 74 2a 2c 73 71 6c  ,char const*,sql
22bdc 69 74 65 5f 69 6e 74 36 34 29 2c 76 6f 69 64 2a  ite_int64),void*
22bdd 29 3b 0a 20 20 76 6f 69 64 20 2a 20 28 2a 75 73  );.  void * (*us
22bde 65 72 5f 64 61 74 61 29 28 73 71 6c 69 74 65 33  er_data)(sqlite3
22bdf 5f 63 6f 6e 74 65 78 74 2a 29 3b 0a 20 20 63 6f  _context*);.  co
22be0 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a 76 61 6c  nst void * (*val
22be1 75 65 5f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33  ue_blob)(sqlite3
22be2 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
22be3 20 28 2a 76 61 6c 75 65 5f 62 79 74 65 73 29 28   (*value_bytes)(
22be4 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
22be5 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75 65 5f  .  int  (*value_
22be6 62 79 74 65 73 31 36 29 28 73 71 6c 69 74 65 33  bytes16)(sqlite3
22be7 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 64 6f 75 62  _value*);.  doub
22be8 6c 65 20 20 28 2a 76 61 6c 75 65 5f 64 6f 75 62  le  (*value_doub
22be9 6c 65 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75  le)(sqlite3_valu
22bea 65 2a 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61  e*);.  int  (*va
22beb 6c 75 65 5f 69 6e 74 29 28 73 71 6c 69 74 65 33  lue_int)(sqlite3
22bec 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 73 71 6c 69  _value*);.  sqli
22bed 74 65 5f 69 6e 74 36 34 20 20 28 2a 76 61 6c 75  te_int64  (*valu
22bee 65 5f 69 6e 74 36 34 29 28 73 71 6c 69 74 65 33  e_int64)(sqlite3
22bef 5f 76 61 6c 75 65 2a 29 3b 0a 20 20 69 6e 74 20  _value*);.  int 
22bf0 20 28 2a 76 61 6c 75 65 5f 6e 75 6d 65 72 69 63   (*value_numeric
22bf1 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f 76  _type)(sqlite3_v
22bf2 61 6c 75 65 2a 29 3b 0a 20 20 63 6f 6e 73 74 20  alue*);.  const 
22bf3 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 20  unsigned char * 
22bf4 28 2a 76 61 6c 75 65 5f 74 65 78 74 29 28 73 71  (*value_text)(sq
22bf5 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
22bf6 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
22bf7 76 61 6c 75 65 5f 74 65 78 74 31 36 29 28 73 71  value_text16)(sq
22bf8 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b 0a 20  lite3_value*);. 
22bf9 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20 28 2a   const void * (*
22bfa 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 29 28  value_text16be)(
22bfb 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a 29 3b  sqlite3_value*);
22bfc 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 20  .  const void * 
22bfd 28 2a 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  (*value_text16le
22bfe 29 28 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  )(sqlite3_value*
22bff 29 3b 0a 20 20 69 6e 74 20 20 28 2a 76 61 6c 75  );.  int  (*valu
22c00 65 5f 74 79 70 65 29 28 73 71 6c 69 74 65 33 5f  e_type)(sqlite3_
22c01 76 61 6c 75 65 2a 29 3b 0a 20 20 63 68 61 72 20  value*);.  char 
22c02 2a 28 2a 76 6d 70 72 69 6e 74 66 29 28 63 6f 6e  *(*vmprintf)(con
22c03 73 74 20 63 68 61 72 2a 2c 76 61 5f 6c 69 73 74  st char*,va_list
22c04 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 3f 3f  );.  /* Added ??
22c05 3f 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 6f 76 65  ? */.  int (*ove
22c06 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 29 28  rload_function)(
22c07 73 71 6c 69 74 65 33 2a 2c 20 63 6f 6e 73 74 20  sqlite3*, const 
22c08 63 68 61 72 20 2a 7a 46 75 6e 63 4e 61 6d 65 2c  char *zFuncName,
22c09 20 69 6e 74 20 6e 41 72 67 29 3b 0a 20 20 2f 2a   int nArg);.  /*
22c0a 20 41 64 64 65 64 20 62 79 20 33 2e 33 2e 31 33   Added by 3.3.13
22c0b 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 70 72 65 70   */.  int (*prep
22c0c 61 72 65 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  are_v2)(sqlite3*
22c0d 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
22c0e 2c 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c  ,sqlite3_stmt**,
22c0f 63 6f 6e 73 74 20 63 68 61 72 2a 2a 29 3b 0a 20  const char**);. 
22c10 20 69 6e 74 20 28 2a 70 72 65 70 61 72 65 31 36   int (*prepare16
22c11 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
22c12 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 73 71  nst void*,int,sq
22c13 6c 69 74 65 33 5f 73 74 6d 74 2a 2a 2c 63 6f 6e  lite3_stmt**,con
22c14 73 74 20 76 6f 69 64 2a 2a 29 3b 0a 20 20 69 6e  st void**);.  in
22c15 74 20 28 2a 63 6c 65 61 72 5f 62 69 6e 64 69 6e  t (*clear_bindin
22c16 67 73 29 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  gs)(sqlite3_stmt
22c17 2a 29 3b 0a 20 20 2f 2a 20 41 64 64 65 64 20 62  *);.  /* Added b
22c18 79 20 33 2e 34 2e 31 20 2a 2f 0a 20 20 69 6e 74  y 3.4.1 */.  int
22c19 20 28 2a 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65   (*create_module
22c1a 5f 76 32 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f  _v2)(sqlite3*,co
22c1b 6e 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20  nst char*,const 
22c1c 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2a 2c  sqlite3_module*,
22c1d 76 6f 69 64 2a 2c 76 6f 69 64 20 28 2a 78 44 65  void*,void (*xDe
22c1e 73 74 72 6f 79 29 28 76 6f 69 64 20 2a 29 29 3b  stroy)(void *));
22c1f 0a 20 20 2f 2a 20 41 64 64 65 64 20 62 79 20 33  .  /* Added by 3
22c20 2e 35 2e 30 20 2a 2f 0a 20 20 69 6e 74 20 28 2a  .5.0 */.  int (*
22c21 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 29 28 73  bind_zeroblob)(s
22c22 71 6c 69 74 65 33 5f 73 74 6d 74 2a 2c 69 6e 74  qlite3_stmt*,int
22c23 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20 28 2a 62  ,int);.  int (*b
22c24 6c 6f 62 5f 62 79 74 65 73 29 28 73 71 6c 69 74  lob_bytes)(sqlit
22c25 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20 20 69 6e 74  e3_blob*);.  int
22c26 20 28 2a 62 6c 6f 62 5f 63 6c 6f 73 65 29 28 73   (*blob_close)(s
22c27 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 29 3b 0a 20  qlite3_blob*);. 
22c28 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 6f 70 65 6e   int (*blob_open
22c29 29 28 73 71 6c 69 74 65 33 2a 2c 63 6f 6e 73 74  )(sqlite3*,const
22c2a 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61   char*,const cha
22c2b 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 73  r*,const char*,s
22c2c 71 6c 69 74 65 33 5f 69 6e 74 36 34 2c 69 6e 74  qlite3_int64,int
22c2d 2c 73 71 6c 69 74 65 33 5f 62 6c 6f 62 2a 2a 29  ,sqlite3_blob**)
22c2e 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f 72  ;.  int (*blob_r
22c2f 65 61 64 29 28 73 71 6c 69 74 65 33 5f 62 6c 6f  ead)(sqlite3_blo
22c30 62 2a 2c 76 6f 69 64 2a 2c 69 6e 74 2c 69 6e 74  b*,void*,int,int
22c31 29 3b 0a 20 20 69 6e 74 20 28 2a 62 6c 6f 62 5f  );.  int (*blob_
22c32 77 72 69 74 65 29 28 73 71 6c 69 74 65 33 5f 62  write)(sqlite3_b
22c33 6c 6f 62 2a 2c 63 6f 6e 73 74 20 76 6f 69 64 2a  lob*,const void*
22c34 2c 69 6e 74 2c 69 6e 74 29 3b 0a 20 20 69 6e 74  ,int,int);.  int
22c35 20 28 2a 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74   (*create_collat
22c36 69 6f 6e 5f 76 32 29 28 73 71 6c 69 74 65 33 2a  ion_v2)(sqlite3*
22c37 2c 63 6f 6e 73 74 20 63 68 61 72 2a 2c 69 6e 74  ,const char*,int
22c38 2c 76 6f 69 64 2a 2c 69 6e 74 28 2a 29 28 76 6f  ,void*,int(*)(vo
22c39 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
22c3a 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 76 6f  id*,int,const vo
22c3b 69 64 2a 29 2c 76 6f 69 64 28 2a 29 28 76 6f 69  id*),void(*)(voi
22c3c 64 2a 29 29 3b 0a 20 20 69 6e 74 20 28 2a 66 69  d*));.  int (*fi
22c3d 6c 65 5f 63 6f 6e 74 72 6f 6c 29 28 73 71 6c 69  le_control)(sqli
22c3e 74 65 33 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  te3*,const char*
22c3f 2c 69 6e 74 2c 76 6f 69 64 2a 29 3b 0a 20 20 73  ,int,void*);.  s
22c40 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 28 2a 6d  qlite3_int64 (*m
22c41 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 29  emory_highwater)
22c42 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33  (int);.  sqlite3
22c43 5f 69 6e 74 36 34 20 28 2a 6d 65 6d 6f 72 79 5f  _int64 (*memory_
22c44 75 73 65 64 29 28 76 6f 69 64 29 3b 0a 20 20 73  used)(void);.  s
22c45 71 6c 69 74 65 33 5f 6d 75 74 65 78 20 2a 28 2a  qlite3_mutex *(*
22c46 6d 75 74 65 78 5f 61 6c 6c 6f 63 29 28 69 6e 74  mutex_alloc)(int
22c47 29 3b 0a 20 20 76 6f 69 64 20 28 2a 6d 75 74 65  );.  void (*mute
22c48 78 5f 65 6e 74 65 72 29 28 73 71 6c 69 74 65 33  x_enter)(sqlite3
22c49 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 76 6f 69 64  _mutex*);.  void
22c4a 20 28 2a 6d 75 74 65 78 5f 66 72 65 65 29 28 73   (*mutex_free)(s
22c4b 71 6c 69 74 65 33 5f 6d 75 74 65 78 2a 29 3b 0a  qlite3_mutex*);.
22c4c 20 20 76 6f 69 64 20 28 2a 6d 75 74 65 78 5f 6c    void (*mutex_l
22c4d 65 61 76 65 29 28 73 71 6c 69 74 65 33 5f 6d 75  eave)(sqlite3_mu
22c4e 74 65 78 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 6d  tex*);.  int (*m
22c4f 75 74 65 78 5f 74 72 79 29 28 73 71 6c 69 74 65  utex_try)(sqlite
22c50 33 5f 6d 75 74 65 78 2a 29 3b 0a 20 20 69 6e 74  3_mutex*);.  int
22c51 20 28 2a 6f 70 65 6e 5f 76 32 29 28 63 6f 6e 73   (*open_v2)(cons
22c52 74 20 63 68 61 72 2a 2c 73 71 6c 69 74 65 33 2a  t char*,sqlite3*
22c53 2a 2c 69 6e 74 2c 63 6f 6e 73 74 20 63 68 61 72  *,int,const char
22c54 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 72 65 6c 65  *);.  int (*rele
22c55 61 73 65 5f 6d 65 6d 6f 72 79 29 28 69 6e 74 29  ase_memory)(int)
22c56 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
22c57 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 29 28 73  t_error_nomem)(s
22c58 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29  qlite3_context*)
22c59 3b 0a 20 20 76 6f 69 64 20 28 2a 72 65 73 75 6c  ;.  void (*resul
22c5a 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 29 28  t_error_toobig)(
22c5b 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a  sqlite3_context*
22c5c 29 3b 0a 20 20 69 6e 74 20 28 2a 73 6c 65 65 70  );.  int (*sleep
22c5d 29 28 69 6e 74 29 3b 0a 20 20 76 6f 69 64 20 28  )(int);.  void (
22c5e 2a 73 6f 66 74 5f 68 65 61 70 5f 6c 69 6d 69 74  *soft_heap_limit
22c5f 29 28 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65  )(int);.  sqlite
22c60 33 5f 76 66 73 20 2a 28 2a 76 66 73 5f 66 69 6e  3_vfs *(*vfs_fin
22c61 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b  d)(const char*);
22c62 0a 20 20 69 6e 74 20 28 2a 76 66 73 5f 72 65 67  .  int (*vfs_reg
22c63 69 73 74 65 72 29 28 73 71 6c 69 74 65 33 5f 76  ister)(sqlite3_v
22c64 66 73 2a 2c 69 6e 74 29 3b 0a 20 20 69 6e 74 20  fs*,int);.  int 
22c65 28 2a 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72  (*vfs_unregister
22c66 29 28 73 71 6c 69 74 65 33 5f 76 66 73 2a 29 3b  )(sqlite3_vfs*);
22c67 0a 20 20 69 6e 74 20 28 2a 78 74 68 72 65 61 64  .  int (*xthread
22c68 73 61 66 65 29 28 76 6f 69 64 29 3b 0a 20 20 76  safe)(void);.  v
22c69 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 7a 65 72  oid (*result_zer
22c6a 6f 62 6c 6f 62 29 28 73 71 6c 69 74 65 33 5f 63  oblob)(sqlite3_c
22c6b 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b 0a 20 20  ontext*,int);.  
22c6c 76 6f 69 64 20 28 2a 72 65 73 75 6c 74 5f 65 72  void (*result_er
22c6d 72 6f 72 5f 63 6f 64 65 29 28 73 71 6c 69 74 65  ror_code)(sqlite
22c6e 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74 29 3b  3_context*,int);
22c6f 0a 20 20 69 6e 74 20 28 2a 74 65 73 74 5f 63 6f  .  int (*test_co
22c70 6e 74 72 6f 6c 29 28 69 6e 74 2c 20 2e 2e 2e 29  ntrol)(int, ...)
22c71 3b 0a 20 20 76 6f 69 64 20 28 2a 72 61 6e 64 6f  ;.  void (*rando
22c72 6d 6e 65 73 73 29 28 69 6e 74 2c 76 6f 69 64 2a  mness)(int,void*
22c73 29 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 28 2a  );.  sqlite3 *(*
22c74 63 6f 6e 74 65 78 74 5f 64 62 5f 68 61 6e 64 6c  context_db_handl
22c75 65 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  e)(sqlite3_conte
22c76 78 74 2a 29 3b 0a 20 20 69 6e 74 20 28 2a 65 78  xt*);.  int (*ex
22c77 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f  tended_result_co
22c78 64 65 73 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e  des)(sqlite3*,in
22c79 74 29 3b 0a 20 20 69 6e 74 20 28 2a 6c 69 6d 69  t);.  int (*limi
22c7a 74 29 28 73 71 6c 69 74 65 33 2a 2c 69 6e 74 2c  t)(sqlite3*,int,
22c7b 69 6e 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  int);.  sqlite3_
22c7c 73 74 6d 74 20 2a 28 2a 6e 65 78 74 5f 73 74 6d  stmt *(*next_stm
22c7d 74 29 28 73 71 6c 69 74 65 33 2a 2c 73 71 6c 69  t)(sqlite3*,sqli
22c7e 74 65 33 5f 73 74 6d 74 2a 29 3b 0a 20 20 63 6f  te3_stmt*);.  co
22c7f 6e 73 74 20 63 68 61 72 20 2a 28 2a 73 71 6c 29  nst char *(*sql)
22c80 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 3b  (sqlite3_stmt*);
22c81 0a 20 20 69 6e 74 20 28 2a 73 74 61 74 75 73 29  .  int (*status)
22c82 28 69 6e 74 2c 69 6e 74 2a 2c 69 6e 74 2a 2c 69  (int,int*,int*,i
22c83 6e 74 29 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54  nt);.};../*.** T
22c84 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61 63  he following mac
22c85 72 6f 73 20 72 65 64 65 66 69 6e 65 20 74 68 65  ros redefine the
22c86 20 41 50 49 20 72 6f 75 74 69 6e 65 73 20 73 6f   API routines so
22c87 20 74 68 61 74 20 74 68 65 79 20 61 72 65 0a 2a   that they are.*
22c88 2a 20 72 65 64 69 72 65 63 74 65 64 20 74 68 72  * redirected thr
22c89 6f 75 67 68 74 20 74 68 65 20 67 6c 6f 62 61 6c  ought the global
22c8a 20 73 71 6c 69 74 65 33 5f 61 70 69 20 73 74 72   sqlite3_api str
22c8b 75 63 74 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68  ucture..**.** Th
22c8c 69 73 20 68 65 61 64 65 72 20 66 69 6c 65 20 69  is header file i
22c8d 73 20 61 6c 73 6f 20 75 73 65 64 20 62 79 20 74  s also used by t
22c8e 68 65 20 6c 6f 61 64 65 78 74 2e 63 20 73 6f 75  he loadext.c sou
22c8f 72 63 65 20 66 69 6c 65 0a 2a 2a 20 28 70 61 72  rce file.** (par
22c90 74 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 53 51  t of the main SQ
22c91 4c 69 74 65 20 6c 69 62 72 61 72 79 20 2d 20 6e  Lite library - n
22c92 6f 74 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 29  ot an extension)
22c93 20 73 6f 20 74 68 61 74 0a 2a 2a 20 69 74 20 63   so that.** it c
22c94 61 6e 20 67 65 74 20 61 63 63 65 73 73 20 74 6f  an get access to
22c95 20 74 68 65 20 73 71 6c 69 74 65 33 5f 61 70 69   the sqlite3_api
22c96 5f 72 6f 75 74 69 6e 65 73 20 73 74 72 75 63 74  _routines struct
22c97 75 72 65 0a 2a 2a 20 64 65 66 69 6e 69 74 69 6f  ure.** definitio
22c98 6e 2e 20 20 42 75 74 20 74 68 65 20 6d 61 69 6e  n.  But the main
22c99 20 6c 69 62 72 61 72 79 20 64 6f 65 73 20 6e 6f   library does no
22c9a 74 20 77 61 6e 74 20 74 6f 20 72 65 64 65 66 69  t want to redefi
22c9b 6e 65 0a 2a 2a 20 74 68 65 20 41 50 49 2e 20 20  ne.** the API.  
22c9c 53 6f 20 74 68 65 20 72 65 64 65 66 69 6e 69 74  So the redefinit
22c9d 69 6f 6e 20 6d 61 63 72 6f 73 20 61 72 65 20 6f  ion macros are o
22c9e 6e 6c 79 20 76 61 6c 69 64 20 69 66 20 74 68 65  nly valid if the
22c9f 0a 2a 2a 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  .** SQLITE_CORE 
22ca0 6d 61 63 72 6f 73 20 69 73 20 75 6e 64 65 66 69  macros is undefi
22ca1 6e 65 64 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20  ned..*/.#ifndef 
22ca2 53 51 4c 49 54 45 5f 43 4f 52 45 0a 23 64 65 66  SQLITE_CORE.#def
22ca3 69 6e 65 20 73 71 6c 69 74 65 33 5f 61 67 67 72  ine sqlite3_aggr
22ca4 65 67 61 74 65 5f 63 6f 6e 74 65 78 74 20 20 20  egate_context   
22ca5 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22ca6 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
22ca7 74 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  t.#ifndef SQLITE
22ca8 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45 44  _OMIT_DEPRECATED
22ca9 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
22caa 5f 61 67 67 72 65 67 61 74 65 5f 63 6f 75 6e 74  _aggregate_count
22cab 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
22cac 61 70 69 2d 3e 61 67 67 72 65 67 61 74 65 5f 63  api->aggregate_c
22cad 6f 75 6e 74 0a 23 65 6e 64 69 66 0a 23 64 65 66  ount.#endif.#def
22cae 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
22caf 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
22cb0 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22cb1 62 69 6e 64 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  bind_blob.#defin
22cb2 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 64  e sqlite3_bind_d
22cb3 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  ouble           
22cb4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
22cb5 6e 64 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69 6e  nd_double.#defin
22cb6 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  e sqlite3_bind_i
22cb7 6e 74 20 20 20 20 20 20 20 20 20 20 20 20 20 20  nt              
22cb8 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69   sqlite3_api->bi
22cb9 6e 64 5f 69 6e 74 0a 23 64 65 66 69 6e 65 20 73  nd_int.#define s
22cba 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
22cbb 34 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  4             sq
22cbc 6c 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f  lite3_api->bind_
22cbd 69 6e 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71  int64.#define sq
22cbe 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 20  lite3_bind_null 
22cbf 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
22cc0 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 6e  ite3_api->bind_n
22cc1 75 6c 6c 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ull.#define sqli
22cc2 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
22cc3 65 72 5f 63 6f 75 6e 74 20 20 20 73 71 6c 69 74  er_count   sqlit
22cc4 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70 61 72  e3_api->bind_par
22cc5 61 6d 65 74 65 72 5f 63 6f 75 6e 74 0a 23 64 65  ameter_count.#de
22cc6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e  fine sqlite3_bin
22cc7 64 5f 70 61 72 61 6d 65 74 65 72 5f 69 6e 64 65  d_parameter_inde
22cc8 78 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  x   sqlite3_api-
22cc9 3e 62 69 6e 64 5f 70 61 72 61 6d 65 74 65 72 5f  >bind_parameter_
22cca 69 6e 64 65 78 0a 23 64 65 66 69 6e 65 20 73 71  index.#define sq
22ccb 6c 69 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d  lite3_bind_param
22ccc 65 74 65 72 5f 6e 61 6d 65 20 20 20 20 73 71 6c  eter_name    sql
22ccd 69 74 65 33 5f 61 70 69 2d 3e 62 69 6e 64 5f 70  ite3_api->bind_p
22cce 61 72 61 6d 65 74 65 72 5f 6e 61 6d 65 0a 23 64  arameter_name.#d
22ccf 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
22cd0 6e 64 5f 74 65 78 74 20 20 20 20 20 20 20 20 20  nd_text         
22cd1 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22cd2 2d 3e 62 69 6e 64 5f 74 65 78 74 0a 23 64 65 66  ->bind_text.#def
22cd3 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
22cd4 5f 74 65 78 74 31 36 20 20 20 20 20 20 20 20 20  _text16         
22cd5 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22cd6 62 69 6e 64 5f 74 65 78 74 31 36 0a 23 64 65 66  bind_text16.#def
22cd7 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  ine sqlite3_bind
22cd8 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
22cd9 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22cda 62 69 6e 64 5f 76 61 6c 75 65 0a 23 64 65 66 69  bind_value.#defi
22cdb 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79 5f  ne sqlite3_busy_
22cdc 68 61 6e 64 6c 65 72 20 20 20 20 20 20 20 20 20  handler         
22cdd 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
22cde 75 73 79 5f 68 61 6e 64 6c 65 72 0a 23 64 65 66  usy_handler.#def
22cdf 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 75 73 79  ine sqlite3_busy
22ce0 5f 74 69 6d 65 6f 75 74 20 20 20 20 20 20 20 20  _timeout        
22ce1 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22ce2 62 75 73 79 5f 74 69 6d 65 6f 75 74 0a 23 64 65  busy_timeout.#de
22ce3 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 68 61  fine sqlite3_cha
22ce4 6e 67 65 73 20 20 20 20 20 20 20 20 20 20 20 20  nges            
22ce5 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22ce6 3e 63 68 61 6e 67 65 73 0a 23 64 65 66 69 6e 65  >changes.#define
22ce7 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 20 20   sqlite3_close  
22ce8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22ce9 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 6f  sqlite3_api->clo
22cea 73 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  se.#define sqlit
22ceb 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e 65 65  e3_collation_nee
22cec 64 65 64 20 20 20 20 20 20 20 73 71 6c 69 74 65  ded       sqlite
22ced 33 5f 61 70 69 2d 3e 63 6f 6c 6c 61 74 69 6f 6e  3_api->collation
22cee 5f 6e 65 65 64 65 64 0a 23 64 65 66 69 6e 65 20  _needed.#define 
22cef 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
22cf0 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 73  n_needed16     s
22cf1 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 6c  qlite3_api->coll
22cf2 61 74 69 6f 6e 5f 6e 65 65 64 65 64 31 36 0a 23  ation_needed16.#
22cf3 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
22cf4 6f 6c 75 6d 6e 5f 62 6c 6f 62 20 20 20 20 20 20  olumn_blob      
22cf5 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22cf6 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 0a 23  i->column_blob.#
22cf7 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
22cf8 6f 6c 75 6d 6e 5f 62 79 74 65 73 20 20 20 20 20  olumn_bytes     
22cf9 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22cfa 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 0a  i->column_bytes.
22cfb 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22cfc 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36 20 20  column_bytes16  
22cfd 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22cfe 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 62 79 74 65 73  pi->column_bytes
22cff 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
22d00 65 33 5f 63 6f 6c 75 6d 6e 5f 63 6f 75 6e 74 20  e3_column_count 
22d01 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22d02 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 63 6f  3_api->column_co
22d03 75 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  unt.#define sqli
22d04 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
22d05 61 73 65 5f 6e 61 6d 65 20 20 20 73 71 6c 69 74  ase_name   sqlit
22d06 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64  e3_api->column_d
22d07 61 74 61 62 61 73 65 5f 6e 61 6d 65 0a 23 64 65  atabase_name.#de
22d08 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
22d09 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d  umn_database_nam
22d0a 65 31 36 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  e16 sqlite3_api-
22d0b 3e 63 6f 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65  >column_database
22d0c 5f 6e 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20  _name16.#define 
22d0d 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64  sqlite3_column_d
22d0e 65 63 6c 74 79 70 65 20 20 20 20 20 20 20 20 73  ecltype        s
22d0f 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75  qlite3_api->colu
22d10 6d 6e 5f 64 65 63 6c 74 79 70 65 0a 23 64 65 66  mn_decltype.#def
22d11 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
22d12 6d 6e 5f 64 65 63 6c 74 79 70 65 31 36 20 20 20  mn_decltype16   
22d13 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22d14 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65 31  column_decltype1
22d15 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
22d16 33 5f 63 6f 6c 75 6d 6e 5f 64 6f 75 62 6c 65 20  3_column_double 
22d17 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22d18 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 64 6f 75  _api->column_dou
22d19 62 6c 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ble.#define sqli
22d1a 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 20 20  te3_column_int  
22d1b 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22d1c 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69  e3_api->column_i
22d1d 6e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  nt.#define sqlit
22d1e 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36 34 20  e3_column_int64 
22d1f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22d20 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 69 6e  3_api->column_in
22d21 74 36 34 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  t64.#define sqli
22d22 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 20  te3_column_name 
22d23 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22d24 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
22d25 61 6d 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ame.#define sqli
22d26 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 31  te3_column_name1
22d27 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
22d28 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 6e  e3_api->column_n
22d29 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
22d2a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
22d2b 67 69 6e 5f 6e 61 6d 65 20 20 20 20 20 73 71 6c  gin_name     sql
22d2c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
22d2d 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 0a 23 64 65  _origin_name.#de
22d2e 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c  fine sqlite3_col
22d2f 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 31  umn_origin_name1
22d30 36 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d  6   sqlite3_api-
22d31 3e 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e  >column_origin_n
22d32 61 6d 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ame16.#define sq
22d33 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
22d34 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 73 71 6c  le_name      sql
22d35 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
22d36 5f 74 61 62 6c 65 5f 6e 61 6d 65 0a 23 64 65 66  _table_name.#def
22d37 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
22d38 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d 65 31 36 20  mn_table_name16 
22d39 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22d3a 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
22d3b 65 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  e16.#define sqli
22d3c 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 20  te3_column_text 
22d3d 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22d3e 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
22d3f 65 78 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ext.#define sqli
22d40 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 31  te3_column_text1
22d41 36 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  6          sqlit
22d42 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e 5f 74  e3_api->column_t
22d43 65 78 74 31 36 0a 23 64 65 66 69 6e 65 20 73 71  ext16.#define sq
22d44 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70  lite3_column_typ
22d45 65 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  e            sql
22d46 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
22d47 5f 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71  _type.#define sq
22d48 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
22d49 75 65 20 20 20 20 20 20 20 20 20 20 20 73 71 6c  ue           sql
22d4a 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6c 75 6d 6e  ite3_api->column
22d4b 5f 76 61 6c 75 65 0a 23 64 65 66 69 6e 65 20 73  _value.#define s
22d4c 71 6c 69 74 65 33 5f 63 6f 6d 6d 69 74 5f 68 6f  qlite3_commit_ho
22d4d 6f 6b 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ok            sq
22d4e 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 6d 69  lite3_api->commi
22d4f 74 5f 68 6f 6f 6b 0a 23 64 65 66 69 6e 65 20 73  t_hook.#define s
22d50 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 20  qlite3_complete 
22d51 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
22d52 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c  lite3_api->compl
22d53 65 74 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ete.#define sqli
22d54 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31 36 20 20  te3_complete16  
22d55 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22d56 65 33 5f 61 70 69 2d 3e 63 6f 6d 70 6c 65 74 65  e3_api->complete
22d57 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
22d58 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74  e3_create_collat
22d59 69 6f 6e 20 20 20 20 20 20 20 73 71 6c 69 74 65  ion       sqlite
22d5a 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 63 6f  3_api->create_co
22d5b 6c 6c 61 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  llation.#define 
22d5c 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
22d5d 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20 20 73  ollation16     s
22d5e 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61  qlite3_api->crea
22d5f 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 0a 23  te_collation16.#
22d60 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63  define sqlite3_c
22d61 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 20 20  reate_function  
22d62 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22d63 69 2d 3e 63 72 65 61 74 65 5f 66 75 6e 63 74 69  i->create_functi
22d64 6f 6e 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  on.#define sqlit
22d65 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69  e3_create_functi
22d66 6f 6e 31 36 20 20 20 20 20 20 73 71 6c 69 74 65  on16      sqlite
22d67 33 5f 61 70 69 2d 3e 63 72 65 61 74 65 5f 66 75  3_api->create_fu
22d68 6e 63 74 69 6f 6e 31 36 0a 23 64 65 66 69 6e 65  nction16.#define
22d69 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
22d6a 6d 6f 64 75 6c 65 20 20 20 20 20 20 20 20 20 20  module          
22d6b 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 72 65  sqlite3_api->cre
22d6c 61 74 65 5f 6d 6f 64 75 6c 65 0a 23 64 65 66 69  ate_module.#defi
22d6d 6e 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  ne sqlite3_creat
22d6e 65 5f 6d 6f 64 75 6c 65 5f 76 32 20 20 20 20 20  e_module_v2     
22d6f 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63    sqlite3_api->c
22d70 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 0a  reate_module_v2.
22d71 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22d72 64 61 74 61 5f 63 6f 75 6e 74 20 20 20 20 20 20  data_count      
22d73 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22d74 70 69 2d 3e 64 61 74 61 5f 63 6f 75 6e 74 0a 23  pi->data_count.#
22d75 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64  define sqlite3_d
22d76 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 20 20  b_handle        
22d77 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22d78 69 2d 3e 64 62 5f 68 61 6e 64 6c 65 0a 23 64 65  i->db_handle.#de
22d79 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 64 65 63  fine sqlite3_dec
22d7a 6c 61 72 65 5f 76 74 61 62 20 20 20 20 20 20 20  lare_vtab       
22d7b 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22d7c 3e 64 65 63 6c 61 72 65 5f 76 74 61 62 0a 23 64  >declare_vtab.#d
22d7d 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
22d7e 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
22d7f 65 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69  e    sqlite3_api
22d80 2d 3e 65 6e 61 62 6c 65 5f 73 68 61 72 65 64 5f  ->enable_shared_
22d81 63 61 63 68 65 0a 23 64 65 66 69 6e 65 20 73 71  cache.#define sq
22d82 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 20 20 20  lite3_errcode   
22d83 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
22d84 69 74 65 33 5f 61 70 69 2d 3e 65 72 72 63 6f 64  ite3_api->errcod
22d85 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
22d86 33 5f 65 72 72 6d 73 67 20 20 20 20 20 20 20 20  3_errmsg        
22d87 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22d88 5f 61 70 69 2d 3e 65 72 72 6d 73 67 0a 23 64 65  _api->errmsg.#de
22d89 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72 72  fine sqlite3_err
22d8a 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20 20  msg16           
22d8b 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22d8c 3e 65 72 72 6d 73 67 31 36 0a 23 64 65 66 69 6e  >errmsg16.#defin
22d8d 65 20 73 71 6c 69 74 65 33 5f 65 78 65 63 20 20  e sqlite3_exec  
22d8e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22d8f 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 65 78   sqlite3_api->ex
22d90 65 63 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ec.#ifndef SQLIT
22d91 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
22d92 44 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  D.#define sqlite
22d93 33 5f 65 78 70 69 72 65 64 20 20 20 20 20 20 20  3_expired       
22d94 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22d95 5f 61 70 69 2d 3e 65 78 70 69 72 65 64 0a 23 65  _api->expired.#e
22d96 6e 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  ndif.#define sql
22d97 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 20 20 20  ite3_finalize   
22d98 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
22d99 74 65 33 5f 61 70 69 2d 3e 66 69 6e 61 6c 69 7a  te3_api->finaliz
22d9a 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  e.#define sqlite
22d9b 33 5f 66 72 65 65 20 20 20 20 20 20 20 20 20 20  3_free          
22d9c 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22d9d 5f 61 70 69 2d 3e 66 72 65 65 0a 23 64 65 66 69  _api->free.#defi
22d9e 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f  ne sqlite3_free_
22d9f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20 20  table           
22da0 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 66    sqlite3_api->f
22da1 72 65 65 5f 74 61 62 6c 65 0a 23 64 65 66 69 6e  ree_table.#defin
22da2 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75  e sqlite3_get_au
22da3 74 6f 63 6f 6d 6d 69 74 20 20 20 20 20 20 20 20  tocommit        
22da4 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 65   sqlite3_api->ge
22da5 74 5f 61 75 74 6f 63 6f 6d 6d 69 74 0a 23 64 65  t_autocommit.#de
22da6 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
22da7 5f 61 75 78 64 61 74 61 20 20 20 20 20 20 20 20  _auxdata        
22da8 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22da9 3e 67 65 74 5f 61 75 78 64 61 74 61 0a 23 64 65  >get_auxdata.#de
22daa 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65 74  fine sqlite3_get
22dab 5f 74 61 62 6c 65 20 20 20 20 20 20 20 20 20 20  _table          
22dac 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22dad 3e 67 65 74 5f 74 61 62 6c 65 0a 23 69 66 6e 64  >get_table.#ifnd
22dae 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
22daf 45 50 52 45 43 41 54 45 44 0a 23 64 65 66 69 6e  EPRECATED.#defin
22db0 65 20 73 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c  e sqlite3_global
22db1 5f 72 65 63 6f 76 65 72 20 20 20 20 20 20 20 20  _recover        
22db2 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 67 6c   sqlite3_api->gl
22db3 6f 62 61 6c 5f 72 65 63 6f 76 65 72 0a 23 65 6e  obal_recover.#en
22db4 64 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  dif.#define sqli
22db5 74 65 33 5f 69 6e 74 65 72 72 75 70 74 20 20 20  te3_interrupt   
22db6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22db7 65 33 5f 61 70 69 2d 3e 69 6e 74 65 72 72 75 70  e3_api->interrup
22db8 74 78 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  tx.#define sqlit
22db9 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72  e3_last_insert_r
22dba 6f 77 69 64 20 20 20 20 20 20 73 71 6c 69 74 65  owid      sqlite
22dbb 33 5f 61 70 69 2d 3e 6c 61 73 74 5f 69 6e 73 65  3_api->last_inse
22dbc 72 74 5f 72 6f 77 69 64 0a 23 64 65 66 69 6e 65  rt_rowid.#define
22dbd 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73   sqlite3_libvers
22dbe 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20 20  ion             
22dbf 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62  sqlite3_api->lib
22dc0 76 65 72 73 69 6f 6e 0a 23 64 65 66 69 6e 65 20  version.#define 
22dc1 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
22dc2 6f 6e 5f 6e 75 6d 62 65 72 20 20 20 20 20 20 73  on_number      s
22dc3 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 62 76  qlite3_api->libv
22dc4 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 0a 23 64  ersion_number.#d
22dc5 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d 61  efine sqlite3_ma
22dc6 6c 6c 6f 63 20 20 20 20 20 20 20 20 20 20 20 20  lloc            
22dc7 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22dc8 2d 3e 6d 61 6c 6c 6f 63 0a 23 64 65 66 69 6e 65  ->malloc.#define
22dc9 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
22dca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22dcb 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6d 70 72  sqlite3_api->mpr
22dcc 69 6e 74 66 0a 23 64 65 66 69 6e 65 20 73 71 6c  intf.#define sql
22dcd 69 74 65 33 5f 6f 70 65 6e 20 20 20 20 20 20 20  ite3_open       
22dce 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
22dcf 74 65 33 5f 61 70 69 2d 3e 6f 70 65 6e 0a 23 64  te3_api->open.#d
22dd0 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6f 70  efine sqlite3_op
22dd1 65 6e 31 36 20 20 20 20 20 20 20 20 20 20 20 20  en16            
22dd2 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22dd3 2d 3e 6f 70 65 6e 31 36 0a 23 64 65 66 69 6e 65  ->open16.#define
22dd4 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65   sqlite3_prepare
22dd5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
22dd6 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65  sqlite3_api->pre
22dd7 70 61 72 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  pare.#define sql
22dd8 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 20 20  ite3_prepare16  
22dd9 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
22dda 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65  te3_api->prepare
22ddb 31 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  16.#define sqlit
22ddc 65 33 5f 70 72 65 70 61 72 65 5f 76 32 20 20 20  e3_prepare_v2   
22ddd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22dde 33 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 5f 76  3_api->prepare_v
22ddf 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  2.#define sqlite
22de0 33 5f 70 72 65 70 61 72 65 31 36 5f 76 32 20 20  3_prepare16_v2  
22de1 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22de2 5f 61 70 69 2d 3e 70 72 65 70 61 72 65 31 36 5f  _api->prepare16_
22de3 76 32 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  v2.#define sqlit
22de4 65 33 5f 70 72 6f 66 69 6c 65 20 20 20 20 20 20  e3_profile      
22de5 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22de6 33 5f 61 70 69 2d 3e 70 72 6f 66 69 6c 65 0a 23  3_api->profile.#
22de7 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 70  define sqlite3_p
22de8 72 6f 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 20  rogress_handler 
22de9 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22dea 69 2d 3e 70 72 6f 67 72 65 73 73 5f 68 61 6e 64  i->progress_hand
22deb 6c 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ler.#define sqli
22dec 74 65 33 5f 72 65 61 6c 6c 6f 63 20 20 20 20 20  te3_realloc     
22ded 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22dee 65 33 5f 61 70 69 2d 3e 72 65 61 6c 6c 6f 63 0a  e3_api->realloc.
22def 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22df0 72 65 73 65 74 20 20 20 20 20 20 20 20 20 20 20  reset           
22df1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22df2 70 69 2d 3e 72 65 73 65 74 0a 23 64 65 66 69 6e  pi->reset.#defin
22df3 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
22df4 5f 62 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20  _blob           
22df5 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
22df6 73 75 6c 74 5f 62 6c 6f 62 0a 23 64 65 66 69 6e  sult_blob.#defin
22df7 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
22df8 5f 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20  _double         
22df9 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
22dfa 73 75 6c 74 5f 64 6f 75 62 6c 65 0a 23 64 65 66  sult_double.#def
22dfb 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ine sqlite3_resu
22dfc 6c 74 5f 65 72 72 6f 72 20 20 20 20 20 20 20 20  lt_error        
22dfd 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22dfe 72 65 73 75 6c 74 5f 65 72 72 6f 72 0a 23 64 65  result_error.#de
22dff 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 73  fine sqlite3_res
22e00 75 6c 74 5f 65 72 72 6f 72 31 36 20 20 20 20 20  ult_error16     
22e01 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22e02 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72 31 36 0a  >result_error16.
22e03 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e04 72 65 73 75 6c 74 5f 69 6e 74 20 20 20 20 20 20  result_int      
22e05 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e06 70 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 0a 23  pi->result_int.#
22e07 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72  define sqlite3_r
22e08 65 73 75 6c 74 5f 69 6e 74 36 34 20 20 20 20 20  esult_int64     
22e09 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22e0a 69 2d 3e 72 65 73 75 6c 74 5f 69 6e 74 36 34 0a  i->result_int64.
22e0b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e0c 72 65 73 75 6c 74 5f 6e 75 6c 6c 20 20 20 20 20  result_null     
22e0d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e0e 70 69 2d 3e 72 65 73 75 6c 74 5f 6e 75 6c 6c 0a  pi->result_null.
22e0f 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e10 72 65 73 75 6c 74 5f 74 65 78 74 20 20 20 20 20  result_text     
22e11 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e12 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 0a  pi->result_text.
22e13 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e14 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20  result_text16   
22e15 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e16 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78 74 31  pi->result_text1
22e17 36 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  6.#define sqlite
22e18 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31 36 62  3_result_text16b
22e19 65 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  e        sqlite3
22e1a 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 74 65 78  _api->result_tex
22e1b 74 31 36 62 65 0a 23 64 65 66 69 6e 65 20 73 71  t16be.#define sq
22e1c 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78  lite3_result_tex
22e1d 74 31 36 6c 65 20 20 20 20 20 20 20 20 73 71 6c  t16le        sql
22e1e 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c 74  ite3_api->result
22e1f 5f 74 65 78 74 31 36 6c 65 0a 23 64 65 66 69 6e  _text16le.#defin
22e20 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
22e21 5f 76 61 6c 75 65 20 20 20 20 20 20 20 20 20 20  _value          
22e22 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65   sqlite3_api->re
22e23 73 75 6c 74 5f 76 61 6c 75 65 0a 23 64 65 66 69  sult_value.#defi
22e24 6e 65 20 73 71 6c 69 74 65 33 5f 72 6f 6c 6c 62  ne sqlite3_rollb
22e25 61 63 6b 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20  ack_hook        
22e26 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72    sqlite3_api->r
22e27 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 0a 23 64 65  ollback_hook.#de
22e28 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 65 74  fine sqlite3_set
22e29 5f 61 75 74 68 6f 72 69 7a 65 72 20 20 20 20 20  _authorizer     
22e2a 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22e2b 3e 73 65 74 5f 61 75 74 68 6f 72 69 7a 65 72 0a  >set_authorizer.
22e2c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e2d 73 65 74 5f 61 75 78 64 61 74 61 20 20 20 20 20  set_auxdata     
22e2e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e2f 70 69 2d 3e 73 65 74 5f 61 75 78 64 61 74 61 0a  pi->set_auxdata.
22e30 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e31 73 6e 70 72 69 6e 74 66 20 20 20 20 20 20 20 20  snprintf        
22e32 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e33 70 69 2d 3e 73 6e 70 72 69 6e 74 66 0a 23 64 65  pi->snprintf.#de
22e34 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73 74 65  fine sqlite3_ste
22e35 70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p               
22e36 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22e37 3e 73 74 65 70 0a 23 64 65 66 69 6e 65 20 73 71  >step.#define sq
22e38 6c 69 74 65 33 5f 74 61 62 6c 65 5f 63 6f 6c 75  lite3_table_colu
22e39 6d 6e 5f 6d 65 74 61 64 61 74 61 20 20 73 71 6c  mn_metadata  sql
22e3a 69 74 65 33 5f 61 70 69 2d 3e 74 61 62 6c 65 5f  ite3_api->table_
22e3b 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61 74 61 0a  column_metadata.
22e3c 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e3d 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 20 20  thread_cleanup  
22e3e 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e3f 70 69 2d 3e 74 68 72 65 61 64 5f 63 6c 65 61 6e  pi->thread_clean
22e40 75 70 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  up.#define sqlit
22e41 65 33 5f 74 6f 74 61 6c 5f 63 68 61 6e 67 65 73  e3_total_changes
22e42 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22e43 33 5f 61 70 69 2d 3e 74 6f 74 61 6c 5f 63 68 61  3_api->total_cha
22e44 6e 67 65 73 0a 23 64 65 66 69 6e 65 20 73 71 6c  nges.#define sql
22e45 69 74 65 33 5f 74 72 61 63 65 20 20 20 20 20 20  ite3_trace      
22e46 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
22e47 74 65 33 5f 61 70 69 2d 3e 74 72 61 63 65 0a 23  te3_api->trace.#
22e48 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
22e49 49 54 5f 44 45 50 52 45 43 41 54 45 44 0a 23 64  IT_DEPRECATED.#d
22e4a 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 72  efine sqlite3_tr
22e4b 61 6e 73 66 65 72 5f 62 69 6e 64 69 6e 67 73 20  ansfer_bindings 
22e4c 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22e4d 2d 3e 74 72 61 6e 73 66 65 72 5f 62 69 6e 64 69  ->transfer_bindi
22e4e 6e 67 73 0a 23 65 6e 64 69 66 0a 23 64 65 66 69  ngs.#endif.#defi
22e4f 6e 65 20 73 71 6c 69 74 65 33 5f 75 70 64 61 74  ne sqlite3_updat
22e50 65 5f 68 6f 6f 6b 20 20 20 20 20 20 20 20 20 20  e_hook          
22e51 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75    sqlite3_api->u
22e52 70 64 61 74 65 5f 68 6f 6f 6b 0a 23 64 65 66 69  pdate_hook.#defi
22e53 6e 65 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  ne sqlite3_user_
22e54 64 61 74 61 20 20 20 20 20 20 20 20 20 20 20 20  data            
22e55 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 75    sqlite3_api->u
22e56 73 65 72 5f 64 61 74 61 0a 23 64 65 66 69 6e 65  ser_data.#define
22e57 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
22e58 6c 6f 62 20 20 20 20 20 20 20 20 20 20 20 20 20  lob             
22e59 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
22e5a 75 65 5f 62 6c 6f 62 0a 23 64 65 66 69 6e 65 20  ue_blob.#define 
22e5b 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
22e5c 74 65 73 20 20 20 20 20 20 20 20 20 20 20 20 73  tes            s
22e5d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
22e5e 65 5f 62 79 74 65 73 0a 23 64 65 66 69 6e 65 20  e_bytes.#define 
22e5f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
22e60 74 65 73 31 36 20 20 20 20 20 20 20 20 20 20 73  tes16          s
22e61 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75  qlite3_api->valu
22e62 65 5f 62 79 74 65 73 31 36 0a 23 64 65 66 69 6e  e_bytes16.#defin
22e63 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  e sqlite3_value_
22e64 64 6f 75 62 6c 65 20 20 20 20 20 20 20 20 20 20  double          
22e65 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61   sqlite3_api->va
22e66 6c 75 65 5f 64 6f 75 62 6c 65 0a 23 64 65 66 69  lue_double.#defi
22e67 6e 65 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  ne sqlite3_value
22e68 5f 69 6e 74 20 20 20 20 20 20 20 20 20 20 20 20  _int            
22e69 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
22e6a 61 6c 75 65 5f 69 6e 74 0a 23 64 65 66 69 6e 65  alue_int.#define
22e6b 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
22e6c 6e 74 36 34 20 20 20 20 20 20 20 20 20 20 20 20  nt64            
22e6d 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
22e6e 75 65 5f 69 6e 74 36 34 0a 23 64 65 66 69 6e 65  ue_int64.#define
22e6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 6e   sqlite3_value_n
22e70 75 6d 65 72 69 63 5f 74 79 70 65 20 20 20 20 20  umeric_type     
22e71 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c  sqlite3_api->val
22e72 75 65 5f 6e 75 6d 65 72 69 63 5f 74 79 70 65 0a  ue_numeric_type.
22e73 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e74 76 61 6c 75 65 5f 74 65 78 74 20 20 20 20 20 20  value_text      
22e75 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e76 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 0a 23  pi->value_text.#
22e77 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76  define sqlite3_v
22e78 61 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20  alue_text16     
22e79 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22e7a 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36 0a  i->value_text16.
22e7b 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22e7c 76 61 6c 75 65 5f 74 65 78 74 31 36 62 65 20 20  value_text16be  
22e7d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22e7e 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78 74 31 36  pi->value_text16
22e7f 62 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  be.#define sqlit
22e80 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c  e3_value_text16l
22e81 65 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  e         sqlite
22e82 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f 74 65 78  3_api->value_tex
22e83 74 31 36 6c 65 0a 23 64 65 66 69 6e 65 20 73 71  t16le.#define sq
22e84 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
22e85 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
22e86 69 74 65 33 5f 61 70 69 2d 3e 76 61 6c 75 65 5f  ite3_api->value_
22e87 74 79 70 65 0a 23 64 65 66 69 6e 65 20 73 71 6c  type.#define sql
22e88 69 74 65 33 5f 76 6d 70 72 69 6e 74 66 20 20 20  ite3_vmprintf   
22e89 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
22e8a 74 65 33 5f 61 70 69 2d 3e 76 6d 70 72 69 6e 74  te3_api->vmprint
22e8b 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  f.#define sqlite
22e8c 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
22e8d 69 6f 6e 20 20 20 20 20 20 73 71 6c 69 74 65 33  ion      sqlite3
22e8e 5f 61 70 69 2d 3e 6f 76 65 72 6c 6f 61 64 5f 66  _api->overload_f
22e8f 75 6e 63 74 69 6f 6e 0a 23 64 65 66 69 6e 65 20  unction.#define 
22e90 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
22e91 76 32 20 20 20 20 20 20 20 20 20 20 20 20 20 73  v2             s
22e92 71 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70  qlite3_api->prep
22e93 61 72 65 5f 76 32 0a 23 64 65 66 69 6e 65 20 73  are_v2.#define s
22e94 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 31 36  qlite3_prepare16
22e95 5f 76 32 20 20 20 20 20 20 20 20 20 20 20 73 71  _v2           sq
22e96 6c 69 74 65 33 5f 61 70 69 2d 3e 70 72 65 70 61  lite3_api->prepa
22e97 72 65 31 36 5f 76 32 0a 23 64 65 66 69 6e 65 20  re16_v2.#define 
22e98 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62 69  sqlite3_clear_bi
22e99 6e 64 69 6e 67 73 20 20 20 20 20 20 20 20 20 73  ndings         s
22e9a 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6c 65 61  qlite3_api->clea
22e9b 72 5f 62 69 6e 64 69 6e 67 73 0a 23 64 65 66 69  r_bindings.#defi
22e9c 6e 65 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f  ne sqlite3_bind_
22e9d 7a 65 72 6f 62 6c 6f 62 20 20 20 20 20 20 20 20  zeroblob        
22e9e 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
22e9f 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 0a 23 64 65  ind_zeroblob.#de
22ea0 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f  fine sqlite3_blo
22ea1 62 5f 62 79 74 65 73 20 20 20 20 20 20 20 20 20  b_bytes         
22ea2 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22ea3 3e 62 6c 6f 62 5f 62 79 74 65 73 0a 23 64 65 66  >blob_bytes.#def
22ea4 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62  ine sqlite3_blob
22ea5 5f 63 6c 6f 73 65 20 20 20 20 20 20 20 20 20 20  _close          
22ea6 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e     sqlite3_api->
22ea7 62 6c 6f 62 5f 63 6c 6f 73 65 0a 23 64 65 66 69  blob_close.#defi
22ea8 6e 65 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f  ne sqlite3_blob_
22ea9 6f 70 65 6e 20 20 20 20 20 20 20 20 20 20 20 20  open            
22eaa 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62    sqlite3_api->b
22eab 6c 6f 62 5f 6f 70 65 6e 0a 23 64 65 66 69 6e 65  lob_open.#define
22eac 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
22ead 61 64 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ad              
22eae 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f  sqlite3_api->blo
22eaf 62 5f 72 65 61 64 0a 23 64 65 66 69 6e 65 20 73  b_read.#define s
22eb0 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
22eb1 65 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71  e             sq
22eb2 6c 69 74 65 33 5f 61 70 69 2d 3e 62 6c 6f 62 5f  lite3_api->blob_
22eb3 77 72 69 74 65 0a 23 64 65 66 69 6e 65 20 73 71  write.#define sq
22eb4 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c  lite3_create_col
22eb5 6c 61 74 69 6f 6e 5f 76 32 20 20 20 20 73 71 6c  lation_v2    sql
22eb6 69 74 65 33 5f 61 70 69 2d 3e 63 72 65 61 74 65  ite3_api->create
22eb7 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 76 32 0a 23 64  _collation_v2.#d
22eb8 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 69  efine sqlite3_fi
22eb9 6c 65 5f 63 6f 6e 74 72 6f 6c 20 20 20 20 20 20  le_control      
22eba 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22ebb 2d 3e 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 0a 23  ->file_control.#
22ebc 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 6d  define sqlite3_m
22ebd 65 6d 6f 72 79 5f 68 69 67 68 77 61 74 65 72 20  emory_highwater 
22ebe 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22ebf 69 2d 3e 6d 65 6d 6f 72 79 5f 68 69 67 68 77 61  i->memory_highwa
22ec0 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
22ec1 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73 65 64 20  te3_memory_used 
22ec2 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22ec3 65 33 5f 61 70 69 2d 3e 6d 65 6d 6f 72 79 5f 75  e3_api->memory_u
22ec4 73 65 64 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  sed.#define sqli
22ec5 74 65 33 5f 6d 75 74 65 78 5f 61 6c 6c 6f 63 20  te3_mutex_alloc 
22ec6 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22ec7 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 61 6c  e3_api->mutex_al
22ec8 6c 6f 63 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  loc.#define sqli
22ec9 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 20  te3_mutex_enter 
22eca 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22ecb 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 65 6e  e3_api->mutex_en
22ecc 74 65 72 0a 23 64 65 66 69 6e 65 20 73 71 6c 69  ter.#define sqli
22ecd 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65 20 20  te3_mutex_free  
22ece 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
22ecf 65 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 66 72  e3_api->mutex_fr
22ed0 65 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ee.#define sqlit
22ed1 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 20 20  e3_mutex_leave  
22ed2 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22ed3 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 6c 65 61  3_api->mutex_lea
22ed4 76 65 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  ve.#define sqlit
22ed5 65 33 5f 6d 75 74 65 78 5f 74 72 79 20 20 20 20  e3_mutex_try    
22ed6 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
22ed7 33 5f 61 70 69 2d 3e 6d 75 74 65 78 5f 74 72 79  3_api->mutex_try
22ed8 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
22ed9 5f 6f 70 65 6e 5f 76 32 20 20 20 20 20 20 20 20  _open_v2        
22eda 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
22edb 61 70 69 2d 3e 6f 70 65 6e 5f 76 32 0a 23 64 65  api->open_v2.#de
22edc 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65 6c  fine sqlite3_rel
22edd 65 61 73 65 5f 6d 65 6d 6f 72 79 20 20 20 20 20  ease_memory     
22ede 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22edf 3e 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79 0a  >release_memory.
22ee0 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22ee1 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d  result_error_nom
22ee2 65 6d 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61  em     sqlite3_a
22ee3 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72 6f 72  pi->result_error
22ee4 5f 6e 6f 6d 65 6d 0a 23 64 65 66 69 6e 65 20 73  _nomem.#define s
22ee5 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
22ee6 72 6f 72 5f 74 6f 6f 62 69 67 20 20 20 20 73 71  ror_toobig    sq
22ee7 6c 69 74 65 33 5f 61 70 69 2d 3e 72 65 73 75 6c  lite3_api->resul
22ee8 74 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 0a 23  t_error_toobig.#
22ee9 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 73  define sqlite3_s
22eea 6c 65 65 70 20 20 20 20 20 20 20 20 20 20 20 20  leep            
22eeb 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70        sqlite3_ap
22eec 69 2d 3e 73 6c 65 65 70 0a 23 64 65 66 69 6e 65  i->sleep.#define
22eed 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68 65   sqlite3_soft_he
22eee 61 70 5f 6c 69 6d 69 74 20 20 20 20 20 20 20 20  ap_limit        
22eef 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 6f 66  sqlite3_api->sof
22ef0 74 5f 68 65 61 70 5f 6c 69 6d 69 74 0a 23 64 65  t_heap_limit.#de
22ef1 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73  fine sqlite3_vfs
22ef2 5f 66 69 6e 64 20 20 20 20 20 20 20 20 20 20 20  _find           
22ef3 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d      sqlite3_api-
22ef4 3e 76 66 73 5f 66 69 6e 64 0a 23 64 65 66 69 6e  >vfs_find.#defin
22ef5 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65  e sqlite3_vfs_re
22ef6 67 69 73 74 65 72 20 20 20 20 20 20 20 20 20 20  gister          
22ef7 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76 66   sqlite3_api->vf
22ef8 73 5f 72 65 67 69 73 74 65 72 0a 23 64 65 66 69  s_register.#defi
22ef9 6e 65 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 75  ne sqlite3_vfs_u
22efa 6e 72 65 67 69 73 74 65 72 20 20 20 20 20 20 20  nregister       
22efb 20 20 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 76    sqlite3_api->v
22efc 66 73 5f 75 6e 72 65 67 69 73 74 65 72 0a 23 64  fs_unregister.#d
22efd 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 74 68  efine sqlite3_th
22efe 72 65 61 64 73 61 66 65 20 20 20 20 20 20 20 20  readsafe        
22eff 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22f00 2d 3e 78 74 68 72 65 61 64 73 61 66 65 0a 23 64  ->xthreadsafe.#d
22f01 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 72 65  efine sqlite3_re
22f02 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62 20 20 20  sult_zeroblob   
22f03 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 70 69       sqlite3_api
22f04 2d 3e 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f  ->result_zeroblo
22f05 62 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  b.#define sqlite
22f06 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 5f 63  3_result_error_c
22f07 6f 64 65 20 20 20 20 20 20 73 71 6c 69 74 65 33  ode      sqlite3
22f08 5f 61 70 69 2d 3e 72 65 73 75 6c 74 5f 65 72 72  _api->result_err
22f09 6f 72 5f 63 6f 64 65 0a 23 64 65 66 69 6e 65 20  or_code.#define 
22f0a 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f 6e  sqlite3_test_con
22f0b 74 72 6f 6c 20 20 20 20 20 20 20 20 20 20 20 73  trol           s
22f0c 71 6c 69 74 65 33 5f 61 70 69 2d 3e 74 65 73 74  qlite3_api->test
22f0d 5f 63 6f 6e 74 72 6f 6c 0a 23 64 65 66 69 6e 65  _control.#define
22f0e 20 73 71 6c 69 74 65 33 5f 72 61 6e 64 6f 6d 6e   sqlite3_randomn
22f0f 65 73 73 20 20 20 20 20 20 20 20 20 20 20 20 20  ess             
22f10 73 71 6c 69 74 65 33 5f 61 70 69 2d 3e 72 61 6e  sqlite3_api->ran
22f11 64 6f 6d 6e 65 73 73 0a 23 64 65 66 69 6e 65 20  domness.#define 
22f12 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f  sqlite3_context_
22f13 64 62 5f 68 61 6e 64 6c 65 20 20 20 20 20 20 73  db_handle      s
22f14 71 6c 69 74 65 33 5f 61 70 69 2d 3e 63 6f 6e 74  qlite3_api->cont
22f15 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 0a 23 64  ext_db_handle.#d
22f16 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 78  efine sqlite3_ex
22f17 74 65 6e 64 65 64 5f 72 65 73 75 6c 74 5f 63 6f  tended_result_co
22f18 64 65 73 20 20 73 71 6c 69 74 65 33 5f 61 70 69  des  sqlite3_api
22f19 2d 3e 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  ->extended_resul
22f1a 74 5f 63 6f 64 65 73 0a 23 64 65 66 69 6e 65 20  t_codes.#define 
22f1b 73 71 6c 69 74 65 33 5f 6c 69 6d 69 74 20 20 20  sqlite3_limit   
22f1c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
22f1d 71 6c 69 74 65 33 5f 61 70 69 2d 3e 6c 69 6d 69  qlite3_api->limi
22f1e 74 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  t.#define sqlite
22f1f 33 5f 6e 65 78 74 5f 73 74 6d 74 20 20 20 20 20  3_next_stmt     
22f20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
22f21 5f 61 70 69 2d 3e 6e 65 78 74 5f 73 74 6d 74 0a  _api->next_stmt.
22f22 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f  #define sqlite3_
22f23 73 71 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  sql             
22f24 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61         sqlite3_a
22f25 70 69 2d 3e 73 71 6c 0a 23 64 65 66 69 6e 65 20  pi->sql.#define 
22f26 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73 20 20  sqlite3_status  
22f27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
22f28 71 6c 69 74 65 33 5f 61 70 69 2d 3e 73 74 61 74  qlite3_api->stat
22f29 75 73 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  us.#endif /* SQL
22f2a 49 54 45 5f 43 4f 52 45 20 2a 2f 0a 0a 23 64 65  ITE_CORE */..#de
22f2b 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
22f2c 4e 53 49 4f 4e 5f 49 4e 49 54 31 20 20 20 20 20  NSION_INIT1     
22f2d 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61 70  const sqlite3_ap
22f2e 69 5f 72 6f 75 74 69 6e 65 73 20 2a 73 71 6c 69  i_routines *sqli
22f2f 74 65 33 5f 61 70 69 20 3d 20 30 3b 0a 23 64 65  te3_api = 0;.#de
22f30 66 69 6e 65 20 53 51 4c 49 54 45 5f 45 58 54 45  fine SQLITE_EXTE
22f31 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 76 29 20 20  NSION_INIT2(v)  
22f32 73 71 6c 69 74 65 33 5f 61 70 69 20 3d 20 76 3b  sqlite3_api = v;
22f33 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 5f 53 51 4c  ..#endif /* _SQL
22f34 49 54 45 33 45 58 54 5f 48 5f 20 2a 2f 0a 0a 2f  ITE3EXT_H_ */../
22f35 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
22f36 6e 64 20 6f 66 20 73 71 6c 69 74 65 33 65 78 74  nd of sqlite3ext
22f37 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
22f38 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
22f39 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
22f3a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 43  ************** C
22f3b 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65 20  ontinuing where 
22f3c 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20 6c  we left off in l
22f3d 6f 61 64 65 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a  oadext.c *******
22f3e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a  *************/..
22f3f 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
22f40 4d 49 54 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49  MIT_LOAD_EXTENSI
22f41 4f 4e 0a 0a 2f 2a 0a 2a 2a 20 53 6f 6d 65 20 41  ON../*.** Some A
22f42 50 49 20 72 6f 75 74 69 6e 65 73 20 61 72 65 20  PI routines are 
22f43 6f 6d 69 74 74 65 64 20 77 68 65 6e 20 76 61 72  omitted when var
22f44 69 6f 75 73 20 66 65 61 74 75 72 65 73 20 61 72  ious features ar
22f45 65 0a 2a 2a 20 65 78 63 6c 75 64 65 64 20 66 72  e.** excluded fr
22f46 6f 6d 20 61 20 62 75 69 6c 64 20 6f 66 20 53 51  om a build of SQ
22f47 4c 69 74 65 2e 20 20 53 75 62 73 74 69 74 75 74  Lite.  Substitut
22f48 65 20 61 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72  e a NULL pointer
22f49 0a 2a 2a 20 66 6f 72 20 61 6e 79 20 6d 69 73 73  .** for any miss
22f4a 69 6e 67 20 41 50 49 73 2e 0a 2a 2f 0a 23 69 66  ing APIs..*/.#if
22f4b 6e 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42  ndef SQLITE_ENAB
22f4c 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54 41 44 41  LE_COLUMN_METADA
22f4d 54 41 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69  TA.# define sqli
22f4e 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
22f4f 61 73 65 5f 6e 61 6d 65 20 20 20 30 0a 23 20 64  ase_name   0.# d
22f50 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
22f51 6c 75 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61  lumn_database_na
22f52 6d 65 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20  me16 0.# define 
22f53 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74  sqlite3_column_t
22f54 61 62 6c 65 5f 6e 61 6d 65 20 20 20 20 20 20 30  able_name      0
22f55 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
22f56 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e  3_column_table_n
22f57 61 6d 65 31 36 20 20 20 20 30 0a 23 20 64 65 66  ame16    0.# def
22f58 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
22f59 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61 6d 65 20 20  mn_origin_name  
22f5a 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
22f5b 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69  lite3_column_ori
22f5c 67 69 6e 5f 6e 61 6d 65 31 36 20 20 20 30 0a 23  gin_name16   0.#
22f5d 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
22f5e 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
22f5f 61 64 61 74 61 20 20 30 0a 23 65 6e 64 69 66 0a  adata  0.#endif.
22f60 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
22f61 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
22f62 4e 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74  N.# define sqlit
22f63 65 33 5f 73 65 74 5f 61 75 74 68 6f 72 69 7a 65  e3_set_authorize
22f64 72 20 20 20 20 20 20 20 20 20 30 0a 23 65 6e 64  r         0.#end
22f65 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
22f66 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 23 20 64  E_OMIT_UTF16.# d
22f67 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 62 69  efine sqlite3_bi
22f68 6e 64 5f 74 65 78 74 31 36 20 20 20 20 20 20 20  nd_text16       
22f69 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
22f6a 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
22f6b 6e 5f 6e 65 65 64 65 64 31 36 20 20 20 20 20 30  n_needed16     0
22f6c 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
22f6d 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70  3_column_decltyp
22f6e 65 31 36 20 20 20 20 20 20 30 0a 23 20 64 65 66  e16      0.# def
22f6f 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
22f70 6d 6e 5f 6e 61 6d 65 31 36 20 20 20 20 20 20 20  mn_name16       
22f71 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
22f72 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
22f73 74 31 36 20 20 20 20 20 20 20 20 20 20 30 0a 23  t16          0.#
22f74 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
22f75 63 6f 6d 70 6c 65 74 65 31 36 20 20 20 20 20 20  complete16      
22f76 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
22f77 65 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  e sqlite3_create
22f78 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 20 20 20 20  _collation16    
22f79 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
22f7a 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
22f7b 69 6f 6e 31 36 20 20 20 20 20 20 30 0a 23 20 64  ion16      0.# d
22f7c 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 72  efine sqlite3_er
22f7d 72 6d 73 67 31 36 20 20 20 20 20 20 20 20 20 20  rmsg16          
22f7e 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
22f7f 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31 36 20 20  sqlite3_open16  
22f80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
22f81 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
22f82 33 5f 70 72 65 70 61 72 65 31 36 20 20 20 20 20  3_prepare16     
22f83 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
22f84 69 6e 65 20 73 71 6c 69 74 65 33 5f 70 72 65 70  ine sqlite3_prep
22f85 61 72 65 31 36 5f 76 32 20 20 20 20 20 20 20 20  are16_v2        
22f86 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71     0.# define sq
22f87 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
22f88 6f 72 31 36 20 20 20 20 20 20 20 20 20 30 0a 23  or16         0.#
22f89 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
22f8a 72 65 73 75 6c 74 5f 74 65 78 74 31 36 20 20 20  result_text16   
22f8b 20 20 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e         0.# defin
22f8c 65 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74  e sqlite3_result
22f8d 5f 74 65 78 74 31 36 62 65 20 20 20 20 20 20 20  _text16be       
22f8e 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69   0.# define sqli
22f8f 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
22f90 36 6c 65 20 20 20 20 20 20 20 20 30 0a 23 20 64  6le        0.# d
22f91 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 76 61  efine sqlite3_va
22f92 6c 75 65 5f 74 65 78 74 31 36 20 20 20 20 20 20  lue_text16      
22f93 20 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20       0.# define 
22f94 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
22f95 78 74 31 36 62 65 20 20 20 20 20 20 20 20 20 30  xt16be         0
22f96 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
22f97 33 5f 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65  3_value_text16le
22f98 20 20 20 20 20 20 20 20 20 30 0a 23 20 64 65 66           0.# def
22f99 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ine sqlite3_colu
22f9a 6d 6e 5f 64 61 74 61 62 61 73 65 5f 6e 61 6d 65  mn_database_name
22f9b 31 36 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  16 0.# define sq
22f9c 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62  lite3_column_tab
22f9d 6c 65 5f 6e 61 6d 65 31 36 20 20 20 20 30 0a 23  le_name16    0.#
22f9e 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f   define sqlite3_
22f9f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e 5f 6e 61  column_origin_na
22fa0 6d 65 31 36 20 20 20 30 0a 23 65 6e 64 69 66 0a  me16   0.#endif.
22fa1 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f  .#ifdef SQLITE_O
22fa2 4d 49 54 5f 43 4f 4d 50 4c 45 54 45 0a 23 20 64  MIT_COMPLETE.# d
22fa3 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 63 6f  efine sqlite3_co
22fa4 6d 70 6c 65 74 65 20 30 0a 23 20 64 65 66 69 6e  mplete 0.# defin
22fa5 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65  e sqlite3_comple
22fa6 74 65 31 36 20 30 0a 23 65 6e 64 69 66 0a 0a 23  te16 0.#endif..#
22fa7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
22fa8 54 5f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42  T_PROGRESS_CALLB
22fa9 41 43 4b 0a 23 20 64 65 66 69 6e 65 20 73 71 6c  ACK.# define sql
22faa 69 74 65 33 5f 70 72 6f 67 72 65 73 73 5f 68 61  ite3_progress_ha
22fab 6e 64 6c 65 72 20 30 0a 23 65 6e 64 69 66 0a 0a  ndler 0.#endif..
22fac 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  #ifdef SQLITE_OM
22fad 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a  IT_VIRTUALTABLE.
22fae 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  # define sqlite3
22faf 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 20 30  _create_module 0
22fb0 0a 23 20 64 65 66 69 6e 65 20 73 71 6c 69 74 65  .# define sqlite
22fb1 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f  3_create_module_
22fb2 76 32 20 30 0a 23 20 64 65 66 69 6e 65 20 73 71  v2 0.# define sq
22fb3 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74  lite3_declare_vt
22fb4 61 62 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66  ab 0.#endif..#if
22fb5 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
22fb6 53 48 41 52 45 44 5f 43 41 43 48 45 0a 23 20 64  SHARED_CACHE.# d
22fb7 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 65 6e  efine sqlite3_en
22fb8 61 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68  able_shared_cach
22fb9 65 20 30 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64  e 0.#endif..#ifd
22fba 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
22fbb 52 41 43 45 0a 23 20 64 65 66 69 6e 65 20 73 71  RACE.# define sq
22fbc 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 20 20 20  lite3_profile   
22fbd 20 20 20 20 30 0a 23 20 64 65 66 69 6e 65 20 73      0.# define s
22fbe 71 6c 69 74 65 33 5f 74 72 61 63 65 20 20 20 20  qlite3_trace    
22fbf 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a 23       0.#endif..#
22fc0 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
22fc1 54 5f 47 45 54 5f 54 41 42 4c 45 0a 23 20 64 65  T_GET_TABLE.# de
22fc2 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 66 72 65  fine sqlite3_fre
22fc3 65 5f 74 61 62 6c 65 20 20 20 20 30 0a 23 20 64  e_table    0.# d
22fc4 65 66 69 6e 65 20 73 71 6c 69 74 65 33 5f 67 65  efine sqlite3_ge
22fc5 74 5f 74 61 62 6c 65 20 20 20 20 20 30 0a 23 65  t_table     0.#e
22fc6 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
22fc7 49 54 45 5f 4f 4d 49 54 5f 49 4e 43 52 42 4c 4f  ITE_OMIT_INCRBLO
22fc8 42 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65  B.#define sqlite
22fc9 33 5f 62 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 20  3_bind_zeroblob 
22fca 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74   0.#define sqlit
22fcb 65 33 5f 62 6c 6f 62 5f 62 79 74 65 73 20 20 20  e3_blob_bytes   
22fcc 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c 69    0.#define sqli
22fcd 74 65 33 5f 62 6c 6f 62 5f 63 6c 6f 73 65 20 20  te3_blob_close  
22fce 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71 6c     0.#define sql
22fcf 69 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 20 20  ite3_blob_open  
22fd0 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73 71      0.#define sq
22fd1 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65 61 64 20  lite3_blob_read 
22fd2 20 20 20 20 20 30 0a 23 64 65 66 69 6e 65 20 73       0.#define s
22fd3 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 77 72 69 74  qlite3_blob_writ
22fd4 65 20 20 20 20 20 30 0a 23 65 6e 64 69 66 0a 0a  e     0.#endif..
22fd5 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
22fd6 69 6e 67 20 73 74 72 75 63 74 75 72 65 20 63 6f  ing structure co
22fd7 6e 74 61 69 6e 73 20 70 6f 69 6e 74 65 72 73 20  ntains pointers 
22fd8 74 6f 20 61 6c 6c 20 53 51 4c 69 74 65 20 41 50  to all SQLite AP
22fd9 49 20 72 6f 75 74 69 6e 65 73 2e 0a 2a 2a 20 41  I routines..** A
22fda 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 69 73   pointer to this
22fdb 20 73 74 72 75 63 74 75 72 65 20 69 73 20 70 61   structure is pa
22fdc 73 73 65 64 20 69 6e 74 6f 20 65 78 74 65 6e 73  ssed into extens
22fdd 69 6f 6e 73 20 77 68 65 6e 20 74 68 65 79 20 61  ions when they a
22fde 72 65 0a 2a 2a 20 6c 6f 61 64 65 64 20 73 6f 20  re.** loaded so 
22fdf 74 68 61 74 20 74 68 65 20 65 78 74 65 6e 73 69  that the extensi
22fe0 6f 6e 20 63 61 6e 20 6d 61 6b 65 20 63 61 6c 6c  on can make call
22fe1 73 20 62 61 63 6b 20 69 6e 74 6f 20 74 68 65 20  s back into the 
22fe2 53 51 4c 69 74 65 0a 2a 2a 20 6c 69 62 72 61 72  SQLite.** librar
22fe3 79 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 64  y..**.** When ad
22fe4 64 69 6e 67 20 6e 65 77 20 41 50 49 73 2c 20 61  ding new APIs, a
22fe5 64 64 20 74 68 65 6d 20 74 6f 20 74 68 65 20 62  dd them to the b
22fe6 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 20 73 74  ottom of this st
22fe7 72 75 63 74 75 72 65 0a 2a 2a 20 69 6e 20 6f 72  ructure.** in or
22fe8 64 65 72 20 74 6f 20 70 72 65 73 65 72 76 65 20  der to preserve 
22fe9 62 61 63 6b 77 61 72 64 73 20 63 6f 6d 70 61 74  backwards compat
22fea 69 62 69 6c 69 74 79 2e 0a 2a 2a 0a 2a 2a 20 45  ibility..**.** E
22feb 78 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 75  xtensions that u
22fec 73 65 20 6e 65 77 65 72 20 41 50 49 73 20 73 68  se newer APIs sh
22fed 6f 75 6c 64 20 66 69 72 73 74 20 63 61 6c 6c 20  ould first call 
22fee 74 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 5f 6c  the.** sqlite3_l
22fef 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72  ibversion_number
22ff0 28 29 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  () to make sure 
22ff1 74 68 61 74 20 74 68 65 20 41 50 49 20 74 68 65  that the API the
22ff2 79 0a 2a 2a 20 69 6e 74 65 6e 64 20 74 6f 20 75  y.** intend to u
22ff3 73 65 20 69 73 20 73 75 70 70 6f 72 74 65 64 20  se is supported 
22ff4 62 79 20 74 68 65 20 6c 69 62 72 61 72 79 2e 20  by the library. 
22ff5 20 45 78 74 65 6e 73 69 6f 6e 73 20 73 68 6f 75   Extensions shou
22ff6 6c 64 0a 2a 2a 20 61 6c 73 6f 20 63 68 65 63 6b  ld.** also check
22ff7 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 74 68   to make sure th
22ff8 61 74 20 74 68 65 20 70 6f 69 6e 74 65 72 20 74  at the pointer t
22ff9 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  o the function i
22ffa 73 0a 2a 2a 20 6e 6f 74 20 4e 55 4c 4c 20 62 65  s.** not NULL be
22ffb 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20 69 74 2e  fore calling it.
22ffc 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
22ffd 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
22ffe 74 69 6e 65 73 20 73 71 6c 69 74 65 33 41 70 69  tines sqlite3Api
22fff 73 20 3d 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f  s = {.  sqlite3_
23000 61 67 67 72 65 67 61 74 65 5f 63 6f 6e 74 65 78  aggregate_contex
23001 74 2c 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  t,.#ifndef SQLIT
23002 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41 54 45  E_OMIT_DEPRECATE
23003 44 0a 20 20 73 71 6c 69 74 65 33 5f 61 67 67 72  D.  sqlite3_aggr
23004 65 67 61 74 65 5f 63 6f 75 6e 74 2c 0a 23 65 6c  egate_count,.#el
23005 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
23006 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c   sqlite3_bind_bl
23007 6f 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 69  ob,.  sqlite3_bi
23008 6e 64 5f 64 6f 75 62 6c 65 2c 0a 20 20 73 71 6c  nd_double,.  sql
23009 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 2c 0a 20  ite3_bind_int,. 
2300a 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2300b 74 36 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  t64,.  sqlite3_b
2300c 69 6e 64 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69  ind_null,.  sqli
2300d 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
2300e 65 72 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  er_count,.  sqli
2300f 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
23010 65 72 5f 69 6e 64 65 78 2c 0a 20 20 73 71 6c 69  er_index,.  sqli
23011 74 65 33 5f 62 69 6e 64 5f 70 61 72 61 6d 65 74  te3_bind_paramet
23012 65 72 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  er_name,.  sqlit
23013 65 33 5f 62 69 6e 64 5f 74 65 78 74 2c 0a 20 20  e3_bind_text,.  
23014 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78  sqlite3_bind_tex
23015 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62  t16,.  sqlite3_b
23016 69 6e 64 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c  ind_value,.  sql
23017 69 74 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65  ite3_busy_handle
23018 72 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 75 73  r,.  sqlite3_bus
23019 79 5f 74 69 6d 65 6f 75 74 2c 0a 20 20 73 71 6c  y_timeout,.  sql
2301a 69 74 65 33 5f 63 68 61 6e 67 65 73 2c 0a 20 20  ite3_changes,.  
2301b 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 2c 0a 20  sqlite3_close,. 
2301c 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69   sqlite3_collati
2301d 6f 6e 5f 6e 65 65 64 65 64 2c 0a 20 20 73 71 6c  on_needed,.  sql
2301e 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 6e  ite3_collation_n
2301f 65 65 64 65 64 31 36 2c 0a 20 20 73 71 6c 69 74  eeded16,.  sqlit
23020 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 2c 0a  e3_column_blob,.
23021 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e    sqlite3_column
23022 5f 62 79 74 65 73 2c 0a 20 20 73 71 6c 69 74 65  _bytes,.  sqlite
23023 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 31 36  3_column_bytes16
23024 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
23025 6d 6e 5f 63 6f 75 6e 74 2c 0a 20 20 73 71 6c 69  mn_count,.  sqli
23026 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
23027 61 73 65 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69  ase_name,.  sqli
23028 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 61 74 61 62  te3_column_datab
23029 61 73 65 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71  ase_name16,.  sq
2302a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 64 65 63  lite3_column_dec
2302b 6c 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33  ltype,.  sqlite3
2302c 5f 63 6f 6c 75 6d 6e 5f 64 65 63 6c 74 79 70 65  _column_decltype
2302d 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  16,.  sqlite3_co
2302e 6c 75 6d 6e 5f 64 6f 75 62 6c 65 2c 0a 20 20 73  lumn_double,.  s
2302f 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
23030 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c  t,.  sqlite3_col
23031 75 6d 6e 5f 69 6e 74 36 34 2c 0a 20 20 73 71 6c  umn_int64,.  sql
23032 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6e 61 6d 65  ite3_column_name
23033 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ,.  sqlite3_colu
23034 6d 6e 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c  mn_name16,.  sql
23035 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67  ite3_column_orig
23036 69 6e 5f 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74  in_name,.  sqlit
23037 65 33 5f 63 6f 6c 75 6d 6e 5f 6f 72 69 67 69 6e  e3_column_origin
23038 5f 6e 61 6d 65 31 36 2c 0a 20 20 73 71 6c 69 74  _name16,.  sqlit
23039 65 33 5f 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f  e3_column_table_
2303a 6e 61 6d 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  name,.  sqlite3_
2303b 63 6f 6c 75 6d 6e 5f 74 61 62 6c 65 5f 6e 61 6d  column_table_nam
2303c 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
2303d 6f 6c 75 6d 6e 5f 74 65 78 74 2c 0a 20 20 73 71  olumn_text,.  sq
2303e 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78  lite3_column_tex
2303f 74 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  t16,.  sqlite3_c
23040 6f 6c 75 6d 6e 5f 74 79 70 65 2c 0a 20 20 73 71  olumn_type,.  sq
23041 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76 61 6c  lite3_column_val
23042 75 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  ue,.  sqlite3_co
23043 6d 6d 69 74 5f 68 6f 6f 6b 2c 0a 20 20 73 71 6c  mmit_hook,.  sql
23044 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 2c 0a 20  ite3_complete,. 
23045 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74   sqlite3_complet
23046 65 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 63  e16,.  sqlite3_c
23047 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 2c  reate_collation,
23048 0a 20 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74  .  sqlite3_creat
23049 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 2c 0a 20  e_collation16,. 
2304a 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2304b 66 75 6e 63 74 69 6f 6e 2c 0a 20 20 73 71 6c 69  function,.  sqli
2304c 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
2304d 69 6f 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65 33  ion16,.  sqlite3
2304e 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65 2c 0a  _create_module,.
2304f 20 20 73 71 6c 69 74 65 33 5f 64 61 74 61 5f 63    sqlite3_data_c
23050 6f 75 6e 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f  ount,.  sqlite3_
23051 64 62 5f 68 61 6e 64 6c 65 2c 0a 20 20 73 71 6c  db_handle,.  sql
23052 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76 74 61  ite3_declare_vta
23053 62 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 6e 61  b,.  sqlite3_ena
23054 62 6c 65 5f 73 68 61 72 65 64 5f 63 61 63 68 65  ble_shared_cache
23055 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65 72 72 63  ,.  sqlite3_errc
23056 6f 64 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 65  ode,.  sqlite3_e
23057 72 72 6d 73 67 2c 0a 20 20 73 71 6c 69 74 65 33  rrmsg,.  sqlite3
23058 5f 65 72 72 6d 73 67 31 36 2c 0a 20 20 73 71 6c  _errmsg16,.  sql
23059 69 74 65 33 5f 65 78 65 63 2c 0a 23 69 66 6e 64  ite3_exec,.#ifnd
2305a 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44  ef SQLITE_OMIT_D
2305b 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c 69  EPRECATED.  sqli
2305c 74 65 33 5f 65 78 70 69 72 65 64 2c 0a 23 65 6c  te3_expired,.#el
2305d 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69 66 0a 20  se.  0,.#endif. 
2305e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
2305f 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  e,.  sqlite3_fre
23060 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  e,.  sqlite3_fre
23061 65 5f 74 61 62 6c 65 2c 0a 20 20 73 71 6c 69 74  e_table,.  sqlit
23062 65 33 5f 67 65 74 5f 61 75 74 6f 63 6f 6d 6d 69  e3_get_autocommi
23063 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 67 65 74  t,.  sqlite3_get
23064 5f 61 75 78 64 61 74 61 2c 0a 20 20 73 71 6c 69  _auxdata,.  sqli
23065 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 2c 0a 20  te3_get_table,. 
23066 20 30 2c 20 20 20 20 20 2f 2a 20 57 61 73 20 73   0,     /* Was s
23067 71 6c 69 74 65 33 5f 67 6c 6f 62 61 6c 5f 72 65  qlite3_global_re
23068 63 6f 76 65 72 28 29 2c 20 62 75 74 20 74 68 61  cover(), but tha
23069 74 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 64 65  t function is de
2306a 70 72 65 63 61 74 65 64 20 2a 2f 0a 20 20 73 71  precated */.  sq
2306b 6c 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 2c  lite3_interrupt,
2306c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f  .  sqlite3_last_
2306d 69 6e 73 65 72 74 5f 72 6f 77 69 64 2c 0a 20 20  insert_rowid,.  
2306e 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69  sqlite3_libversi
2306f 6f 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6c 69  on,.  sqlite3_li
23070 62 76 65 72 73 69 6f 6e 5f 6e 75 6d 62 65 72 2c  bversion_number,
23071 0a 20 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f  .  sqlite3_mallo
23072 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 70 72  c,.  sqlite3_mpr
23073 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f  intf,.  sqlite3_
23074 6f 70 65 6e 2c 0a 20 20 73 71 6c 69 74 65 33 5f  open,.  sqlite3_
23075 6f 70 65 6e 31 36 2c 0a 20 20 73 71 6c 69 74 65  open16,.  sqlite
23076 33 5f 70 72 65 70 61 72 65 2c 0a 20 20 73 71 6c  3_prepare,.  sql
23077 69 74 65 33 5f 70 72 65 70 61 72 65 31 36 2c 0a  ite3_prepare16,.
23078 20 20 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c    sqlite3_profil
23079 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 6f  e,.  sqlite3_pro
2307a 67 72 65 73 73 5f 68 61 6e 64 6c 65 72 2c 0a 20  gress_handler,. 
2307b 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63   sqlite3_realloc
2307c 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65  ,.  sqlite3_rese
2307d 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  t,.  sqlite3_res
2307e 75 6c 74 5f 62 6c 6f 62 2c 0a 20 20 73 71 6c 69  ult_blob,.  sqli
2307f 74 65 33 5f 72 65 73 75 6c 74 5f 64 6f 75 62 6c  te3_result_doubl
23080 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  e,.  sqlite3_res
23081 75 6c 74 5f 65 72 72 6f 72 2c 0a 20 20 73 71 6c  ult_error,.  sql
23082 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
23083 72 31 36 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  r16,.  sqlite3_r
23084 65 73 75 6c 74 5f 69 6e 74 2c 0a 20 20 73 71 6c  esult_int,.  sql
23085 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74 36  ite3_result_int6
23086 34 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  4,.  sqlite3_res
23087 75 6c 74 5f 6e 75 6c 6c 2c 0a 20 20 73 71 6c 69  ult_null,.  sqli
23088 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 2c  te3_result_text,
23089 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c  .  sqlite3_resul
2308a 74 5f 74 65 78 74 31 36 2c 0a 20 20 73 71 6c 69  t_text16,.  sqli
2308b 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 31  te3_result_text1
2308c 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72  6be,.  sqlite3_r
2308d 65 73 75 6c 74 5f 74 65 78 74 31 36 6c 65 2c 0a  esult_text16le,.
2308e 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
2308f 5f 76 61 6c 75 65 2c 0a 20 20 73 71 6c 69 74 65  _value,.  sqlite
23090 33 5f 72 6f 6c 6c 62 61 63 6b 5f 68 6f 6f 6b 2c  3_rollback_hook,
23091 0a 20 20 73 71 6c 69 74 65 33 5f 73 65 74 5f 61  .  sqlite3_set_a
23092 75 74 68 6f 72 69 7a 65 72 2c 0a 20 20 73 71 6c  uthorizer,.  sql
23093 69 74 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61  ite3_set_auxdata
23094 2c 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72  ,.  sqlite3_snpr
23095 69 6e 74 66 2c 0a 20 20 73 71 6c 69 74 65 33 5f  intf,.  sqlite3_
23096 73 74 65 70 2c 0a 20 20 73 71 6c 69 74 65 33 5f  step,.  sqlite3_
23097 74 61 62 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74  table_column_met
23098 61 64 61 74 61 2c 0a 23 69 66 6e 64 65 66 20 53  adata,.#ifndef S
23099 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
2309a 43 41 54 45 44 0a 20 20 73 71 6c 69 74 65 33 5f  CATED.  sqlite3_
2309b 74 68 72 65 61 64 5f 63 6c 65 61 6e 75 70 2c 0a  thread_cleanup,.
2309c 23 65 6c 73 65 0a 20 20 30 2c 0a 23 65 6e 64 69  #else.  0,.#endi
2309d 66 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 74 61  f.  sqlite3_tota
2309e 6c 5f 63 68 61 6e 67 65 73 2c 0a 20 20 73 71 6c  l_changes,.  sql
2309f 69 74 65 33 5f 74 72 61 63 65 2c 0a 23 69 66 6e  ite3_trace,.#ifn
230a0 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
230a1 44 45 50 52 45 43 41 54 45 44 0a 20 20 73 71 6c  DEPRECATED.  sql
230a2 69 74 65 33 5f 74 72 61 6e 73 66 65 72 5f 62 69  ite3_transfer_bi
230a3 6e 64 69 6e 67 73 2c 0a 23 65 6c 73 65 0a 20 20  ndings,.#else.  
230a4 30 2c 0a 23 65 6e 64 69 66 0a 20 20 73 71 6c 69  0,.#endif.  sqli
230a5 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 2c  te3_update_hook,
230a6 0a 20 20 73 71 6c 69 74 65 33 5f 75 73 65 72 5f  .  sqlite3_user_
230a7 64 61 74 61 2c 0a 20 20 73 71 6c 69 74 65 33 5f  data,.  sqlite3_
230a8 76 61 6c 75 65 5f 62 6c 6f 62 2c 0a 20 20 73 71  value_blob,.  sq
230a9 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65  lite3_value_byte
230aa 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  s,.  sqlite3_val
230ab 75 65 5f 62 79 74 65 73 31 36 2c 0a 20 20 73 71  ue_bytes16,.  sq
230ac 6c 69 74 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62  lite3_value_doub
230ad 6c 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  le,.  sqlite3_va
230ae 6c 75 65 5f 69 6e 74 2c 0a 20 20 73 71 6c 69 74  lue_int,.  sqlit
230af 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34 2c 0a  e3_value_int64,.
230b0 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
230b1 6e 75 6d 65 72 69 63 5f 74 79 70 65 2c 0a 20 20  numeric_type,.  
230b2 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
230b3 78 74 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76 61  xt,.  sqlite3_va
230b4 6c 75 65 5f 74 65 78 74 31 36 2c 0a 20 20 73 71  lue_text16,.  sq
230b5 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74  lite3_value_text
230b6 31 36 62 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  16be,.  sqlite3_
230b7 76 61 6c 75 65 5f 74 65 78 74 31 36 6c 65 2c 0a  value_text16le,.
230b8 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f    sqlite3_value_
230b9 74 79 70 65 2c 0a 20 20 73 71 6c 69 74 65 33 5f  type,.  sqlite3_
230ba 76 6d 70 72 69 6e 74 66 2c 0a 20 20 2f 2a 0a 20  vmprintf,.  /*. 
230bb 20 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e 61 6c   ** The original
230bc 20 41 50 49 20 73 65 74 20 65 6e 64 73 20 68 65   API set ends he
230bd 72 65 2e 20 20 41 6c 6c 20 65 78 74 65 6e 73 69  re.  All extensi
230be 6f 6e 73 20 63 61 6e 20 63 61 6c 6c 20 61 6e 79  ons can call any
230bf 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 41 50 49  .  ** of the API
230c0 73 20 61 62 6f 76 65 20 70 72 6f 76 69 64 65 64  s above provided
230c1 20 74 68 61 74 20 74 68 65 20 70 6f 69 6e 74 65   that the pointe
230c2 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 2e 20 20  r is not NULL.  
230c3 42 75 74 0a 20 20 2a 2a 20 62 65 66 6f 72 65 20  But.  ** before 
230c4 63 61 6c 6c 69 6e 67 20 41 50 49 73 20 74 68 61  calling APIs tha
230c5 74 20 66 6f 6c 6c 6f 77 2c 20 65 78 74 65 6e 73  t follow, extens
230c6 69 6f 6e 20 73 68 6f 75 6c 64 20 63 68 65 63 6b  ion should check
230c7 20 74 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65   the.  ** sqlite
230c8 33 5f 6c 69 62 76 65 72 73 69 6f 6e 5f 6e 75 6d  3_libversion_num
230c9 62 65 72 28 29 20 74 6f 20 6d 61 6b 65 20 73 75  ber() to make su
230ca 72 65 20 74 68 65 79 20 61 72 65 20 64 65 61 6c  re they are deal
230cb 69 6e 67 20 77 69 74 68 0a 20 20 2a 2a 20 61 20  ing with.  ** a 
230cc 6c 69 62 72 61 72 79 20 74 68 61 74 20 69 73 20  library that is 
230cd 6e 65 77 20 65 6e 6f 75 67 68 20 74 6f 20 73 75  new enough to su
230ce 70 70 6f 72 74 20 74 68 61 74 20 41 50 49 2e 0a  pport that API..
230cf 20 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a    **************
230d0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
230d1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
230d2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
230d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 20 2a 2f  ***********.  */
230d4 0a 20 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  .  sqlite3_overl
230d5 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 2c 0a 0a 20  oad_function,.. 
230d6 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65 64 20 61   /*.  ** Added a
230d7 66 74 65 72 20 33 2e 33 2e 31 33 0a 20 20 2a 2f  fter 3.3.13.  */
230d8 0a 20 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61  .  sqlite3_prepa
230d9 72 65 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65 33  re_v2,.  sqlite3
230da 5f 70 72 65 70 61 72 65 31 36 5f 76 32 2c 0a 20  _prepare16_v2,. 
230db 20 73 71 6c 69 74 65 33 5f 63 6c 65 61 72 5f 62   sqlite3_clear_b
230dc 69 6e 64 69 6e 67 73 2c 0a 0a 20 20 2f 2a 0a 20  indings,..  /*. 
230dd 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33 2e   ** Added for 3.
230de 34 2e 31 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  4.1.  */.  sqlit
230df 65 33 5f 63 72 65 61 74 65 5f 6d 6f 64 75 6c 65  e3_create_module
230e0 5f 76 32 2c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  _v2,..  /*.  ** 
230e1 41 64 64 65 64 20 66 6f 72 20 33 2e 35 2e 30 0a  Added for 3.5.0.
230e2 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62    */.  sqlite3_b
230e3 69 6e 64 5f 7a 65 72 6f 62 6c 6f 62 2c 0a 20 20  ind_zeroblob,.  
230e4 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 62 79 74  sqlite3_blob_byt
230e5 65 73 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  es,.  sqlite3_bl
230e6 6f 62 5f 63 6c 6f 73 65 2c 0a 20 20 73 71 6c 69  ob_close,.  sqli
230e7 74 65 33 5f 62 6c 6f 62 5f 6f 70 65 6e 2c 0a 20  te3_blob_open,. 
230e8 20 73 71 6c 69 74 65 33 5f 62 6c 6f 62 5f 72 65   sqlite3_blob_re
230e9 61 64 2c 0a 20 20 73 71 6c 69 74 65 33 5f 62 6c  ad,.  sqlite3_bl
230ea 6f 62 5f 77 72 69 74 65 2c 0a 20 20 73 71 6c 69  ob_write,.  sqli
230eb 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
230ec 74 69 6f 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74  tion_v2,.  sqlit
230ed 65 33 5f 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 2c  e3_file_control,
230ee 0a 20 20 73 71 6c 69 74 65 33 5f 6d 65 6d 6f 72  .  sqlite3_memor
230ef 79 5f 68 69 67 68 77 61 74 65 72 2c 0a 20 20 73  y_highwater,.  s
230f0 71 6c 69 74 65 33 5f 6d 65 6d 6f 72 79 5f 75 73  qlite3_memory_us
230f1 65 64 2c 0a 23 69 66 64 65 66 20 53 51 4c 49 54  ed,.#ifdef SQLIT
230f2 45 5f 4d 55 54 45 58 5f 4f 4d 49 54 0a 20 20 30  E_MUTEX_OMIT.  0
230f3 2c 20 0a 20 20 30 2c 20 0a 20 20 30 2c 0a 20 20  , .  0, .  0,.  
230f4 30 2c 0a 20 20 30 2c 0a 23 65 6c 73 65 0a 20 20  0,.  0,.#else.  
230f5 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 61 6c  sqlite3_mutex_al
230f6 6c 6f 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d  loc,.  sqlite3_m
230f7 75 74 65 78 5f 65 6e 74 65 72 2c 0a 20 20 73 71  utex_enter,.  sq
230f8 6c 69 74 65 33 5f 6d 75 74 65 78 5f 66 72 65 65  lite3_mutex_free
230f9 2c 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ,.  sqlite3_mute
230fa 78 5f 6c 65 61 76 65 2c 0a 20 20 73 71 6c 69 74  x_leave,.  sqlit
230fb 65 33 5f 6d 75 74 65 78 5f 74 72 79 2c 0a 23 65  e3_mutex_try,.#e
230fc 6e 64 69 66 0a 20 20 73 71 6c 69 74 65 33 5f 6f  ndif.  sqlite3_o
230fd 70 65 6e 5f 76 32 2c 0a 20 20 73 71 6c 69 74 65  pen_v2,.  sqlite
230fe 33 5f 72 65 6c 65 61 73 65 5f 6d 65 6d 6f 72 79  3_release_memory
230ff 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
23100 6c 74 5f 65 72 72 6f 72 5f 6e 6f 6d 65 6d 2c 0a  lt_error_nomem,.
23101 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
23102 5f 65 72 72 6f 72 5f 74 6f 6f 62 69 67 2c 0a 20  _error_toobig,. 
23103 20 73 71 6c 69 74 65 33 5f 73 6c 65 65 70 2c 0a   sqlite3_sleep,.
23104 20 20 73 71 6c 69 74 65 33 5f 73 6f 66 74 5f 68    sqlite3_soft_h
23105 65 61 70 5f 6c 69 6d 69 74 2c 0a 20 20 73 71 6c  eap_limit,.  sql
23106 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 2c 0a 20  ite3_vfs_find,. 
23107 20 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67   sqlite3_vfs_reg
23108 69 73 74 65 72 2c 0a 20 20 73 71 6c 69 74 65 33  ister,.  sqlite3
23109 5f 76 66 73 5f 75 6e 72 65 67 69 73 74 65 72 2c  _vfs_unregister,
2310a 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 41 64 64 65  ..  /*.  ** Adde
2310b 64 20 66 6f 72 20 33 2e 35 2e 38 0a 20 20 2a 2f  d for 3.5.8.  */
2310c 0a 20 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61  .  sqlite3_threa
2310d 64 73 61 66 65 2c 0a 20 20 73 71 6c 69 74 65 33  dsafe,.  sqlite3
2310e 5f 72 65 73 75 6c 74 5f 7a 65 72 6f 62 6c 6f 62  _result_zeroblob
2310f 2c 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ,.  sqlite3_resu
23110 6c 74 5f 65 72 72 6f 72 5f 63 6f 64 65 2c 0a 20  lt_error_code,. 
23111 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f 63 6f   sqlite3_test_co
23112 6e 74 72 6f 6c 2c 0a 20 20 73 71 6c 69 74 65 33  ntrol,.  sqlite3
23113 5f 72 61 6e 64 6f 6d 6e 65 73 73 2c 0a 20 20 73  _randomness,.  s
23114 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
23115 62 5f 68 61 6e 64 6c 65 2c 0a 0a 20 20 2f 2a 0a  b_handle,..  /*.
23116 20 20 2a 2a 20 41 64 64 65 64 20 66 6f 72 20 33    ** Added for 3
23117 2e 36 2e 30 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  .6.0.  */.  sqli
23118 74 65 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73  te3_extended_res
23119 75 6c 74 5f 63 6f 64 65 73 2c 0a 20 20 73 71 6c  ult_codes,.  sql
2311a 69 74 65 33 5f 6c 69 6d 69 74 2c 0a 20 20 73 71  ite3_limit,.  sq
2311b 6c 69 74 65 33 5f 6e 65 78 74 5f 73 74 6d 74 2c  lite3_next_stmt,
2311c 0a 20 20 73 71 6c 69 74 65 33 5f 73 71 6c 2c 0a  .  sqlite3_sql,.
2311d 20 20 73 71 6c 69 74 65 33 5f 73 74 61 74 75 73    sqlite3_status
2311e 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65  ,.};../*.** Atte
2311f 6d 70 74 20 74 6f 20 6c 6f 61 64 20 61 6e 20 53  mpt to load an S
23120 51 4c 69 74 65 20 65 78 74 65 6e 73 69 6f 6e 20  QLite extension 
23121 6c 69 62 72 61 72 79 20 63 6f 6e 74 61 69 6e 65  library containe
23122 64 20 69 6e 20 74 68 65 20 66 69 6c 65 0a 2a 2a  d in the file.**
23123 20 7a 46 69 6c 65 2e 20 20 54 68 65 20 65 6e 74   zFile.  The ent
23124 72 79 20 70 6f 69 6e 74 20 69 73 20 7a 50 72 6f  ry point is zPro
23125 63 2e 20 20 7a 50 72 6f 63 20 6d 61 79 20 62 65  c.  zProc may be
23126 20 30 20 69 6e 20 77 68 69 63 68 20 63 61 73 65   0 in which case
23127 20 61 0a 2a 2a 20 64 65 66 61 75 6c 74 20 65 6e   a.** default en
23128 74 72 79 20 70 6f 69 6e 74 20 6e 61 6d 65 20 28  try point name (
23129 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f  sqlite3_extensio
2312a 6e 5f 69 6e 69 74 29 20 69 73 20 75 73 65 64 2e  n_init) is used.
2312b 20 20 55 73 65 0a 2a 2a 20 6f 66 20 74 68 65 20    Use.** of the 
2312c 64 65 66 61 75 6c 74 20 6e 61 6d 65 20 69 73 20  default name is 
2312d 72 65 63 6f 6d 6d 65 6e 64 65 64 2e 0a 2a 2a 0a  recommended..**.
2312e 2a 2a 20 52 65 74 75 72 6e 20 53 51 4c 49 54 45  ** Return SQLITE
2312f 5f 4f 4b 20 6f 6e 20 73 75 63 63 65 73 73 20 61  _OK on success a
23130 6e 64 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 20  nd SQLITE_ERROR 
23131 69 66 20 73 6f 6d 65 74 68 69 6e 67 20 67 6f 65  if something goe
23132 73 20 77 72 6f 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49  s wrong..**.** I
23133 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72  f an error occur
23134 73 20 61 6e 64 20 70 7a 45 72 72 4d 73 67 20 69  s and pzErrMsg i
23135 73 20 6e 6f 74 20 30 2c 20 74 68 65 6e 20 66 69  s not 0, then fi
23136 6c 6c 20 2a 70 7a 45 72 72 4d 73 67 20 77 69 74  ll *pzErrMsg wit
23137 68 20 0a 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73  h .** error mess
23138 61 67 65 20 74 65 78 74 2e 20 20 54 68 65 20 63  age text.  The c
23139 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20  alling function 
2313a 73 68 6f 75 6c 64 20 66 72 65 65 20 74 68 69 73  should free this
2313b 20 6d 65 6d 6f 72 79 0a 2a 2a 20 62 79 20 63 61   memory.** by ca
2313c 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 44 62 46  lling sqlite3DbF
2313d 72 65 65 28 64 62 2c 20 29 2e 0a 2a 2f 0a 73 74  ree(db, )..*/.st
2313e 61 74 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33  atic int sqlite3
2313f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 0a 20  LoadExtension(. 
23140 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
23141 20 20 20 20 20 20 20 2f 2a 20 4c 6f 61 64 20 74         /* Load t
23142 68 65 20 65 78 74 65 6e 73 69 6f 6e 20 69 6e 74  he extension int
23143 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20  o this database 
23144 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
23145 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46 69 6c  const char *zFil
23146 65 2c 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66  e,    /* Name of
23147 20 74 68 65 20 73 68 61 72 65 64 20 6c 69 62 72   the shared libr
23148 61 72 79 20 63 6f 6e 74 61 69 6e 69 6e 67 20 65  ary containing e
23149 78 74 65 6e 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f  xtension */.  co
2314a 6e 73 74 20 63 68 61 72 20 2a 7a 50 72 6f 63 2c  nst char *zProc,
2314b 20 20 20 20 2f 2a 20 45 6e 74 72 79 20 70 6f 69      /* Entry poi
2314c 6e 74 2e 20 20 55 73 65 20 22 73 71 6c 69 74 65  nt.  Use "sqlite
2314d 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69 74  3_extension_init
2314e 22 20 69 66 20 30 20 2a 2f 0a 20 20 63 68 61 72  " if 0 */.  char
2314f 20 2a 2a 70 7a 45 72 72 4d 73 67 20 20 20 20 20   **pzErrMsg     
23150 20 20 2f 2a 20 50 75 74 20 65 72 72 6f 72 20 6d    /* Put error m
23151 65 73 73 61 67 65 20 68 65 72 65 20 69 66 20 6e  essage here if n
23152 6f 74 20 30 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  ot 0 */.){.  sql
23153 69 74 65 33 5f 76 66 73 20 2a 70 56 66 73 20 3d  ite3_vfs *pVfs =
23154 20 64 62 2d 3e 70 56 66 73 3b 0a 20 20 76 6f 69   db->pVfs;.  voi
23155 64 20 2a 68 61 6e 64 6c 65 3b 0a 20 20 69 6e 74  d *handle;.  int
23156 20 28 2a 78 49 6e 69 74 29 28 73 71 6c 69 74 65   (*xInit)(sqlite
23157 33 2a 2c 63 68 61 72 2a 2a 2c 63 6f 6e 73 74 20  3*,char**,const 
23158 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75 74  sqlite3_api_rout
23159 69 6e 65 73 2a 29 3b 0a 20 20 63 68 61 72 20 2a  ines*);.  char *
2315a 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a 20 20 76  zErrmsg = 0;.  v
2315b 6f 69 64 20 2a 2a 61 48 61 6e 64 6c 65 3b 0a 0a  oid **aHandle;..
2315c 20 20 2f 2a 20 54 69 63 6b 65 74 20 23 31 38 36    /* Ticket #186
2315d 33 2e 20 20 54 6f 20 61 76 6f 69 64 20 61 20 63  3.  To avoid a c
2315e 72 65 61 74 69 6e 67 20 73 65 63 75 72 69 74 79  reating security
2315f 20 70 72 6f 62 6c 65 6d 73 20 66 6f 72 20 6f 6c   problems for ol
23160 64 65 72 0a 20 20 2a 2a 20 61 70 70 6c 69 63 61  der.  ** applica
23161 74 69 6f 6e 73 20 74 68 61 74 20 72 65 6c 69 6e  tions that relin
23162 6b 20 61 67 61 69 6e 73 74 20 6e 65 77 65 72 20  k against newer 
23163 76 65 72 73 69 6f 6e 73 20 6f 66 20 53 51 4c 69  versions of SQLi
23164 74 65 2c 20 74 68 65 0a 20 20 2a 2a 20 61 62 69  te, the.  ** abi
23165 6c 69 74 79 20 74 6f 20 72 75 6e 20 6c 6f 61 64  lity to run load
23166 5f 65 78 74 65 6e 73 69 6f 6e 20 69 73 20 74 75  _extension is tu
23167 72 6e 65 64 20 6f 66 66 20 62 79 20 64 65 66 61  rned off by defa
23168 75 6c 74 2e 20 20 4f 6e 65 0a 20 20 2a 2a 20 6d  ult.  One.  ** m
23169 75 73 74 20 63 61 6c 6c 20 73 71 6c 69 74 65 33  ust call sqlite3
2316a 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f 65 78 74  _enable_load_ext
2316b 65 6e 73 69 6f 6e 28 29 20 74 6f 20 74 75 72 6e  ension() to turn
2316c 20 6f 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 20 20   on extension.  
2316d 2a 2a 20 6c 6f 61 64 69 6e 67 2e 20 20 4f 74 68  ** loading.  Oth
2316e 65 72 77 69 73 65 20 79 6f 75 20 67 65 74 20 74  erwise you get t
2316f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 65 72 72  he following err
23170 6f 72 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  or..  */.  if( (
23171 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
23172 54 45 5f 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e  TE_LoadExtension
23173 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  )==0 ){.    if( 
23174 70 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20 20 20  pzErrMsg ){.    
23175 20 20 2a 70 7a 45 72 72 4d 73 67 20 3d 20 73 71    *pzErrMsg = sq
23176 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 6e  lite3_mprintf("n
23177 6f 74 20 61 75 74 68 6f 72 69 7a 65 64 22 29 3b  ot authorized");
23178 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
23179 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
2317a 20 20 7d 0a 0a 20 20 69 66 28 20 7a 50 72 6f 63    }..  if( zProc
2317b 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 50 72 6f 63  ==0 ){.    zProc
2317c 20 3d 20 22 73 71 6c 69 74 65 33 5f 65 78 74 65   = "sqlite3_exte
2317d 6e 73 69 6f 6e 5f 69 6e 69 74 22 3b 0a 20 20 7d  nsion_init";.  }
2317e 0a 0a 20 20 68 61 6e 64 6c 65 20 3d 20 73 71 6c  ..  handle = sql
2317f 69 74 65 33 4f 73 44 6c 4f 70 65 6e 28 70 56 66  ite3OsDlOpen(pVf
23180 73 2c 20 7a 46 69 6c 65 29 3b 0a 20 20 69 66 28  s, zFile);.  if(
23181 20 68 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a 20 20   handle==0 ){.  
23182 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
23183 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 45 72  {.      char zEr
23184 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20 7a 45  r[256];.      zE
23185 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d  rr[sizeof(zErr)-
23186 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  1] = '\0';.     
23187 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
23188 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  f(sizeof(zErr)-1
23189 2c 20 7a 45 72 72 2c 20 0a 20 20 20 20 20 20 20  , zErr, .       
2318a 20 20 20 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70     "unable to op
2318b 65 6e 20 73 68 61 72 65 64 20 6c 69 62 72 61 72  en shared librar
2318c 79 20 5b 25 73 5d 22 2c 20 7a 46 69 6c 65 29 3b  y [%s]", zFile);
2318d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73  .      sqlite3Os
2318e 44 6c 45 72 72 6f 72 28 70 56 66 73 2c 20 73 69  DlError(pVfs, si
2318f 7a 65 6f 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45  zeof(zErr)-1, zE
23190 72 72 29 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72  rr);.      *pzEr
23191 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33 44 62  rMsg = sqlite3Db
23192 53 74 72 44 75 70 28 30 2c 20 7a 45 72 72 29 3b  StrDup(0, zErr);
23193 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
23194 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
23195 20 20 7d 0a 20 20 78 49 6e 69 74 20 3d 20 28 69    }.  xInit = (i
23196 6e 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63  nt(*)(sqlite3*,c
23197 68 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69  har**,const sqli
23198 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73  te3_api_routines
23199 2a 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20  *)).            
2319a 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 4f 73         sqlite3Os
2319b 44 6c 53 79 6d 28 70 56 66 73 2c 20 68 61 6e 64  DlSym(pVfs, hand
2319c 6c 65 2c 20 7a 50 72 6f 63 29 3b 0a 20 20 69 66  le, zProc);.  if
2319d 28 20 78 49 6e 69 74 3d 3d 30 20 29 7b 0a 20 20  ( xInit==0 ){.  
2319e 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67 20 29    if( pzErrMsg )
2319f 7b 0a 20 20 20 20 20 20 63 68 61 72 20 7a 45 72  {.      char zEr
231a0 72 5b 32 35 36 5d 3b 0a 20 20 20 20 20 20 7a 45  r[256];.      zE
231a1 72 72 5b 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d  rr[sizeof(zErr)-
231a2 31 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 20 20 20  1] = '\0';.     
231a3 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74   sqlite3_snprint
231a4 66 28 73 69 7a 65 6f 66 28 7a 45 72 72 29 2d 31  f(sizeof(zErr)-1
231a5 2c 20 7a 45 72 72 2c 0a 20 20 20 20 20 20 20 20  , zErr,.        
231a6 20 20 22 6e 6f 20 65 6e 74 72 79 20 70 6f 69 6e    "no entry poin
231a7 74 20 5b 25 73 5d 20 69 6e 20 73 68 61 72 65 64  t [%s] in shared
231a8 20 6c 69 62 72 61 72 79 20 5b 25 73 5d 22 2c 20   library [%s]", 
231a9 7a 50 72 6f 63 2c 7a 46 69 6c 65 29 3b 0a 20 20  zProc,zFile);.  
231aa 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 45      sqlite3OsDlE
231ab 72 72 6f 72 28 70 56 66 73 2c 20 73 69 7a 65 6f  rror(pVfs, sizeo
231ac 66 28 7a 45 72 72 29 2d 31 2c 20 7a 45 72 72 29  f(zErr)-1, zErr)
231ad 3b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72 4d 73  ;.      *pzErrMs
231ae 67 20 3d 20 73 71 6c 69 74 65 33 44 62 53 74 72  g = sqlite3DbStr
231af 44 75 70 28 30 2c 20 7a 45 72 72 29 3b 0a 20 20  Dup(0, zErr);.  
231b0 20 20 20 20 73 71 6c 69 74 65 33 4f 73 44 6c 43      sqlite3OsDlC
231b1 6c 6f 73 65 28 70 56 66 73 2c 20 68 61 6e 64 6c  lose(pVfs, handl
231b2 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65  e);.    }.    re
231b3 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f  turn SQLITE_ERRO
231b4 52 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 78  R;.  }else if( x
231b5 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 6d 73  Init(db, &zErrms
231b6 67 2c 20 26 73 71 6c 69 74 65 33 41 70 69 73 29  g, &sqlite3Apis)
231b7 20 29 7b 0a 20 20 20 20 69 66 28 20 70 7a 45 72   ){.    if( pzEr
231b8 72 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 2a 70  rMsg ){.      *p
231b9 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
231ba 33 5f 6d 70 72 69 6e 74 66 28 22 65 72 72 6f 72  3_mprintf("error
231bb 20 64 75 72 69 6e 67 20 69 6e 69 74 69 61 6c 69   during initiali
231bc 7a 61 74 69 6f 6e 3a 20 25 73 22 2c 20 7a 45 72  zation: %s", zEr
231bd 72 6d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20  rmsg);.    }.   
231be 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 45   sqlite3_free(zE
231bf 72 72 6d 73 67 29 3b 0a 20 20 20 20 73 71 6c 69  rrmsg);.    sqli
231c0 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28 70 56 66  te3OsDlClose(pVf
231c1 73 2c 20 68 61 6e 64 6c 65 29 3b 0a 20 20 20 20  s, handle);.    
231c2 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
231c3 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41  ROR;.  }..  /* A
231c4 70 70 65 6e 64 20 74 68 65 20 6e 65 77 20 73 68  ppend the new sh
231c5 61 72 65 64 20 6c 69 62 72 61 72 79 20 68 61 6e  ared library han
231c6 64 6c 65 20 74 6f 20 74 68 65 20 64 62 2d 3e 61  dle to the db->a
231c7 45 78 74 65 6e 73 69 6f 6e 20 61 72 72 61 79 2e  Extension array.
231c8 20 2a 2f 0a 20 20 61 48 61 6e 64 6c 65 20 3d 20   */.  aHandle = 
231c9 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
231ca 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 68  ero(db, sizeof(h
231cb 61 6e 64 6c 65 29 2a 28 64 62 2d 3e 6e 45 78 74  andle)*(db->nExt
231cc 65 6e 73 69 6f 6e 2b 31 29 29 3b 0a 20 20 69 66  ension+1));.  if
231cd 28 20 61 48 61 6e 64 6c 65 3d 3d 30 20 29 7b 0a  ( aHandle==0 ){.
231ce 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
231cf 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69  E_NOMEM;.  }.  i
231d0 66 28 20 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f  f( db->nExtensio
231d1 6e 3e 30 20 29 7b 0a 20 20 20 20 6d 65 6d 63 70  n>0 ){.    memcp
231d2 79 28 61 48 61 6e 64 6c 65 2c 20 64 62 2d 3e 61  y(aHandle, db->a
231d3 45 78 74 65 6e 73 69 6f 6e 2c 20 73 69 7a 65 6f  Extension, sizeo
231d4 66 28 68 61 6e 64 6c 65 29 2a 64 62 2d 3e 6e 45  f(handle)*db->nE
231d5 78 74 65 6e 73 69 6f 6e 29 3b 0a 20 20 7d 0a 20  xtension);.  }. 
231d6 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
231d7 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  b, db->aExtensio
231d8 6e 29 3b 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e  n);.  db->aExten
231d9 73 69 6f 6e 20 3d 20 61 48 61 6e 64 6c 65 3b 0a  sion = aHandle;.
231da 0a 20 20 64 62 2d 3e 61 45 78 74 65 6e 73 69 6f  .  db->aExtensio
231db 6e 5b 64 62 2d 3e 6e 45 78 74 65 6e 73 69 6f 6e  n[db->nExtension
231dc 2b 2b 5d 20 3d 20 68 61 6e 64 6c 65 3b 0a 20 20  ++] = handle;.  
231dd 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
231de 3b 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  ;.}.SQLITE_API i
231df 6e 74 20 73 71 6c 69 74 65 33 5f 6c 6f 61 64 5f  nt sqlite3_load_
231e0 65 78 74 65 6e 73 69 6f 6e 28 0a 20 20 73 71 6c  extension(.  sql
231e1 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20  ite3 *db,       
231e2 20 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20 65     /* Load the e
231e3 78 74 65 6e 73 69 6f 6e 20 69 6e 74 6f 20 74 68  xtension into th
231e4 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
231e5 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73  ection */.  cons
231e6 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 2c 20 20  t char *zFile,  
231e7 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65    /* Name of the
231e8 20 73 68 61 72 65 64 20 6c 69 62 72 61 72 79 20   shared library 
231e9 63 6f 6e 74 61 69 6e 69 6e 67 20 65 78 74 65 6e  containing exten
231ea 73 69 6f 6e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sion */.  const 
231eb 63 68 61 72 20 2a 7a 50 72 6f 63 2c 20 20 20 20  char *zProc,    
231ec 2f 2a 20 45 6e 74 72 79 20 70 6f 69 6e 74 2e 20  /* Entry point. 
231ed 20 55 73 65 20 22 73 71 6c 69 74 65 33 5f 65 78   Use "sqlite3_ex
231ee 74 65 6e 73 69 6f 6e 5f 69 6e 69 74 22 20 69 66  tension_init" if
231ef 20 30 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70   0 */.  char **p
231f0 7a 45 72 72 4d 73 67 20 20 20 20 20 20 20 2f 2a  zErrMsg       /*
231f1 20 50 75 74 20 65 72 72 6f 72 20 6d 65 73 73 61   Put error messa
231f2 67 65 20 68 65 72 65 20 69 66 20 6e 6f 74 20 30  ge here if not 0
231f3 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   */.){.  int rc;
231f4 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
231f5 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
231f6 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
231f7 33 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 28 64  3LoadExtension(d
231f8 62 2c 20 7a 46 69 6c 65 2c 20 7a 50 72 6f 63 2c  b, zFile, zProc,
231f9 20 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 73 71   pzErrMsg);.  sq
231fa 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
231fb 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
231fc 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
231fd 0a 2a 2a 20 43 61 6c 6c 20 74 68 69 73 20 72 6f  .** Call this ro
231fe 75 74 69 6e 65 20 77 68 65 6e 20 74 68 65 20 64  utine when the d
231ff 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
23200 6f 6e 20 69 73 20 63 6c 6f 73 69 6e 67 20 69 6e  on is closing in
23201 20 6f 72 64 65 72 0a 2a 2a 20 74 6f 20 63 6c 65   order.** to cle
23202 61 6e 20 75 70 20 6c 6f 61 64 65 64 20 65 78 74  an up loaded ext
23203 65 6e 73 69 6f 6e 73 0a 2a 2f 0a 53 51 4c 49 54  ensions.*/.SQLIT
23204 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
23205 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74 65 6e  qlite3CloseExten
23206 73 69 6f 6e 73 28 73 71 6c 69 74 65 33 20 2a 64  sions(sqlite3 *d
23207 62 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 61  b){.  int i;.  a
23208 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d  ssert( sqlite3_m
23209 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75  utex_held(db->mu
2320a 74 65 78 29 20 29 3b 0a 20 20 66 6f 72 28 69 3d  tex) );.  for(i=
2320b 30 3b 20 69 3c 64 62 2d 3e 6e 45 78 74 65 6e 73  0; i<db->nExtens
2320c 69 6f 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73  ion; i++){.    s
2320d 71 6c 69 74 65 33 4f 73 44 6c 43 6c 6f 73 65 28  qlite3OsDlClose(
2320e 64 62 2d 3e 70 56 66 73 2c 20 64 62 2d 3e 61 45  db->pVfs, db->aE
2320f 78 74 65 6e 73 69 6f 6e 5b 69 5d 29 3b 0a 20 20  xtension[i]);.  
23210 7d 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  }.  sqlite3DbFre
23211 65 28 64 62 2c 20 64 62 2d 3e 61 45 78 74 65 6e  e(db, db->aExten
23212 73 69 6f 6e 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  sion);.}../*.** 
23213 45 6e 61 62 6c 65 20 6f 72 20 64 69 73 61 62 6c  Enable or disabl
23214 65 20 65 78 74 65 6e 73 69 6f 6e 20 6c 6f 61 64  e extension load
23215 69 6e 67 2e 20 20 45 78 74 65 6e 73 69 6f 6e 20  ing.  Extension 
23216 6c 6f 61 64 69 6e 67 20 69 73 20 64 69 73 61 62  loading is disab
23217 6c 65 64 20 62 79 0a 2a 2a 20 64 65 66 61 75 6c  led by.** defaul
23218 74 20 73 6f 20 61 73 20 6e 6f 74 20 74 6f 20 6f  t so as not to o
23219 70 65 6e 20 73 65 63 75 72 69 74 79 20 68 6f 6c  pen security hol
2321a 65 73 20 69 6e 20 6f 6c 64 65 72 20 61 70 70 6c  es in older appl
2321b 69 63 61 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  ications..*/.SQL
2321c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2321d 74 65 33 5f 65 6e 61 62 6c 65 5f 6c 6f 61 64 5f  te3_enable_load_
2321e 65 78 74 65 6e 73 69 6f 6e 28 73 71 6c 69 74 65  extension(sqlite
2321f 33 20 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66  3 *db, int onoff
23220 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  ){.  sqlite3_mut
23221 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
23222 65 78 29 3b 0a 20 20 69 66 28 20 6f 6e 6f 66 66  ex);.  if( onoff
23223 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67   ){.    db->flag
23224 73 20 7c 3d 20 53 51 4c 49 54 45 5f 4c 6f 61 64  s |= SQLITE_Load
23225 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 65 6c  Extension;.  }el
23226 73 65 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67  se{.    db->flag
23227 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 6f 61  s &= ~SQLITE_Loa
23228 64 45 78 74 65 6e 73 69 6f 6e 3b 0a 20 20 7d 0a  dExtension;.  }.
23229 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2322a 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29  leave(db->mutex)
2322b 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2322c 45 5f 4f 4b 3b 0a 7d 0a 0a 23 65 6e 64 69 66 20  E_OK;.}..#endif 
2322d 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c  /* SQLITE_OMIT_L
2322e 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 20 2a 2f  OAD_EXTENSION */
2322f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 61 75 74 6f  ../*.** The auto
23230 2d 65 78 74 65 6e 73 69 6f 6e 20 63 6f 64 65 20  -extension code 
23231 61 64 64 65 64 20 72 65 67 61 72 64 6c 65 73 73  added regardless
23232 20 6f 66 20 77 68 65 74 68 65 72 20 6f 72 20 6e   of whether or n
23233 6f 74 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20  ot extension.** 
23234 6c 6f 61 64 69 6e 67 20 69 73 20 73 75 70 70 6f  loading is suppo
23235 72 74 65 64 2e 20 20 57 65 20 6e 65 65 64 20 61  rted.  We need a
23236 20 64 75 6d 6d 79 20 73 71 6c 69 74 65 33 41 70   dummy sqlite3Ap
23237 69 73 20 70 6f 69 6e 74 65 72 20 66 6f 72 20 74  is pointer for t
23238 68 61 74 0a 2a 2a 20 63 6f 64 65 20 69 66 20 72  hat.** code if r
23239 65 67 75 6c 61 72 20 65 78 74 65 6e 73 69 6f 6e  egular extension
2323a 20 6c 6f 61 64 69 6e 67 20 69 73 20 6e 6f 74 20   loading is not 
2323b 61 76 61 69 6c 61 62 6c 65 2e 20 20 54 68 69 73  available.  This
2323c 20 69 73 20 74 68 61 74 0a 2a 2a 20 64 75 6d 6d   is that.** dumm
2323d 79 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 23 69  y pointer..*/.#i
2323e 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
2323f 5f 4c 4f 41 44 5f 45 58 54 45 4e 53 49 4f 4e 0a  _LOAD_EXTENSION.
23240 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
23241 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
23242 73 20 73 71 6c 69 74 65 33 41 70 69 73 20 3d 20  s sqlite3Apis = 
23243 7b 20 30 20 7d 3b 0a 23 65 6e 64 69 66 0a 0a 0a  { 0 };.#endif...
23244 2f 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  /*.** The follow
23245 69 6e 67 20 6f 62 6a 65 63 74 20 68 6f 6c 64 73  ing object holds
23246 20 74 68 65 20 6c 69 73 74 20 6f 66 20 61 75 74   the list of aut
23247 6f 6d 61 74 69 63 61 6c 6c 79 20 6c 6f 61 64 65  omatically loade
23248 64 0a 2a 2a 20 65 78 74 65 6e 73 69 6f 6e 73 2e  d.** extensions.
23249 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6c 69 73 74  .**.** This list
2324a 20 69 73 20 73 68 61 72 65 64 20 61 63 72 6f 73   is shared acros
2324b 73 20 74 68 72 65 61 64 73 2e 20 20 54 68 65 20  s threads.  The 
2324c 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
2324d 54 49 43 5f 4d 41 53 54 45 52 0a 2a 2a 20 6d 75  TIC_MASTER.** mu
2324e 74 65 78 20 6d 75 73 74 20 62 65 20 68 65 6c 64  tex must be held
2324f 20 77 68 69 6c 65 20 61 63 63 65 73 73 69 6e 67   while accessing
23250 20 74 68 69 73 20 6c 69 73 74 2e 0a 2a 2f 0a 74   this list..*/.t
23251 79 70 65 64 65 66 20 73 74 72 75 63 74 20 73 71  ypedef struct sq
23252 6c 69 74 65 33 45 78 74 54 79 70 65 20 73 71 6c  lite3ExtType sql
23253 69 74 65 33 45 78 74 54 79 70 65 3b 0a 73 74 61  ite3ExtType;.sta
23254 74 69 63 20 53 51 4c 49 54 45 5f 57 53 44 20 73  tic SQLITE_WSD s
23255 74 72 75 63 74 20 73 71 6c 69 74 65 33 45 78 74  truct sqlite3Ext
23256 54 79 70 65 20 7b 0a 20 20 69 6e 74 20 6e 45 78  Type {.  int nEx
23257 74 3b 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d  t;        /* Num
23258 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 69  ber of entries i
23259 6e 20 61 45 78 74 5b 5d 20 2a 2f 20 20 20 20 20  n aExt[] */     
2325a 20 20 20 20 20 0a 20 20 76 6f 69 64 20 2a 2a 61       .  void **a
2325b 45 78 74 3b 20 20 20 20 20 2f 2a 20 50 6f 69 6e  Ext;     /* Poin
2325c 74 65 72 73 20 74 6f 20 74 68 65 20 65 78 74 65  ters to the exte
2325d 6e 73 69 6f 6e 20 69 6e 69 74 20 66 75 6e 63 74  nsion init funct
2325e 69 6f 6e 73 20 2a 2f 0a 7d 20 73 71 6c 69 74 65  ions */.} sqlite
2325f 33 41 75 74 6f 65 78 74 20 3d 20 7b 20 30 2c 20  3Autoext = { 0, 
23260 30 20 7d 3b 0a 0a 2f 2a 20 54 68 65 20 22 77 73  0 };../* The "ws
23261 64 41 75 74 6f 65 78 74 22 20 6d 61 63 72 6f 20  dAutoext" macro 
23262 77 69 6c 6c 20 72 65 73 6f 6c 76 65 20 74 6f 20  will resolve to 
23263 74 68 65 20 61 75 74 6f 65 78 74 65 6e 73 69 6f  the autoextensio
23264 6e 0a 2a 2a 20 73 74 61 74 65 20 76 65 63 74 6f  n.** state vecto
23265 72 2e 20 20 49 66 20 77 72 69 74 61 62 6c 65 20  r.  If writable 
23266 73 74 61 74 69 63 20 64 61 74 61 20 69 73 20 75  static data is u
23267 6e 73 75 70 70 6f 72 74 65 64 20 6f 6e 20 74 68  nsupported on th
23268 65 20 74 61 72 67 65 74 2c 0a 2a 2a 20 77 65 20  e target,.** we 
23269 68 61 76 65 20 74 6f 20 6c 6f 63 61 74 65 20 74  have to locate t
2326a 68 65 20 73 74 61 74 65 20 76 65 63 74 6f 72 20  he state vector 
2326b 61 74 20 72 75 6e 2d 74 69 6d 65 2e 20 20 49 6e  at run-time.  In
2326c 20 74 68 65 20 6d 6f 72 65 20 63 6f 6d 6d 6f 6e   the more common
2326d 0a 2a 2a 20 63 61 73 65 20 77 68 65 72 65 20 77  .** case where w
2326e 72 69 74 61 62 6c 65 20 73 74 61 74 69 63 20 64  ritable static d
2326f 61 74 61 20 69 73 20 73 75 70 70 6f 72 74 65 64  ata is supported
23270 2c 20 77 73 64 53 74 61 74 20 63 61 6e 20 72 65  , wsdStat can re
23271 66 65 72 20 64 69 72 65 63 74 6c 79 0a 2a 2a 20  fer directly.** 
23272 74 6f 20 74 68 65 20 22 73 71 6c 69 74 65 33 41  to the "sqlite3A
23273 75 74 6f 65 78 74 22 20 73 74 61 74 65 20 76 65  utoext" state ve
23274 63 74 6f 72 20 64 65 63 6c 61 72 65 64 20 61 62  ctor declared ab
23275 6f 76 65 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 53  ove..*/.#ifdef S
23276 51 4c 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 23  QLITE_OMIT_WSD.#
23277 20 64 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65   define wsdAutoe
23278 78 74 49 6e 69 74 20 5c 0a 20 20 73 71 6c 69 74  xtInit \.  sqlit
23279 65 33 45 78 74 54 79 70 65 20 2a 78 20 3d 20 26  e3ExtType *x = &
2327a 47 4c 4f 42 41 4c 28 73 71 6c 69 74 65 33 45 78  GLOBAL(sqlite3Ex
2327b 74 54 79 70 65 2c 73 71 6c 69 74 65 33 41 75 74  tType,sqlite3Aut
2327c 6f 65 78 74 29 0a 23 20 64 65 66 69 6e 65 20 77  oext).# define w
2327d 73 64 41 75 74 6f 65 78 74 20 78 5b 30 5d 0a 23  sdAutoext x[0].#
2327e 65 6c 73 65 0a 23 20 64 65 66 69 6e 65 20 77 73  else.# define ws
2327f 64 41 75 74 6f 65 78 74 49 6e 69 74 0a 23 20 64  dAutoextInit.# d
23280 65 66 69 6e 65 20 77 73 64 41 75 74 6f 65 78 74  efine wsdAutoext
23281 20 73 71 6c 69 74 65 33 41 75 74 6f 65 78 74 0a   sqlite3Autoext.
23282 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52  #endif.../*.** R
23283 65 67 69 73 74 65 72 20 61 20 73 74 61 74 69 63  egister a static
23284 61 6c 6c 79 20 6c 69 6e 6b 65 64 20 65 78 74 65  ally linked exte
23285 6e 73 69 6f 6e 20 74 68 61 74 20 69 73 20 61 75  nsion that is au
23286 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20 6c  tomatically.** l
23287 6f 61 64 65 64 20 62 79 20 65 76 65 72 79 20 6e  oaded by every n
23288 65 77 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  ew database conn
23289 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
2328a 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2328b 33 5f 61 75 74 6f 5f 65 78 74 65 6e 73 69 6f 6e  3_auto_extension
2328c 28 76 6f 69 64 20 2a 78 49 6e 69 74 29 7b 0a 20  (void *xInit){. 
2328d 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
2328e 5f 4f 4b 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c  _OK;.#ifndef SQL
2328f 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49  ITE_OMIT_AUTOINI
23290 54 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  T.  rc = sqlite3
23291 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20  _initialize();. 
23292 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 72   if( rc ){.    r
23293 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 65 6c 73  eturn rc;.  }els
23294 65 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 20 20 20  e.#endif.  {.   
23295 20 69 6e 74 20 69 3b 0a 23 69 66 20 53 51 4c 49   int i;.#if SQLI
23296 54 45 5f 54 48 52 45 41 44 53 41 46 45 0a 20 20  TE_THREADSAFE.  
23297 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 20    sqlite3_mutex 
23298 2a 6d 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33  *mutex = sqlite3
23299 4d 75 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54  MutexAlloc(SQLIT
2329a 45 5f 4d 55 54 45 58 5f 53 54 41 54 49 43 5f 4d  E_MUTEX_STATIC_M
2329b 41 53 54 45 52 29 3b 0a 23 65 6e 64 69 66 0a 20  ASTER);.#endif. 
2329c 20 20 20 77 73 64 41 75 74 6f 65 78 74 49 6e 69     wsdAutoextIni
2329d 74 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  t;.    sqlite3_m
2329e 75 74 65 78 5f 65 6e 74 65 72 28 6d 75 74 65 78  utex_enter(mutex
2329f 29 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  );.    for(i=0; 
232a0 69 3c 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  i<wsdAutoext.nEx
232a1 74 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  t; i++){.      i
232a2 66 28 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45  f( wsdAutoext.aE
232a3 78 74 5b 69 5d 3d 3d 78 49 6e 69 74 20 29 20 62  xt[i]==xInit ) b
232a4 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
232a5 69 66 28 20 69 3d 3d 77 73 64 41 75 74 6f 65 78  if( i==wsdAutoex
232a6 74 2e 6e 45 78 74 20 29 7b 0a 20 20 20 20 20 20  t.nExt ){.      
232a7 69 6e 74 20 6e 42 79 74 65 20 3d 20 28 77 73 64  int nByte = (wsd
232a8 41 75 74 6f 65 78 74 2e 6e 45 78 74 2b 31 29 2a  Autoext.nExt+1)*
232a9 73 69 7a 65 6f 66 28 77 73 64 41 75 74 6f 65 78  sizeof(wsdAutoex
232aa 74 2e 61 45 78 74 5b 30 5d 29 3b 0a 20 20 20 20  t.aExt[0]);.    
232ab 20 20 76 6f 69 64 20 2a 2a 61 4e 65 77 3b 0a 20    void **aNew;. 
232ac 20 20 20 20 20 61 4e 65 77 20 3d 20 73 71 6c 69       aNew = sqli
232ad 74 65 33 5f 72 65 61 6c 6c 6f 63 28 77 73 64 41  te3_realloc(wsdA
232ae 75 74 6f 65 78 74 2e 61 45 78 74 2c 20 6e 42 79  utoext.aExt, nBy
232af 74 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 61  te);.      if( a
232b0 4e 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  New==0 ){.      
232b1 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
232b2 4d 45 4d 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  MEM;.      }else
232b3 7b 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74  {.        wsdAut
232b4 6f 65 78 74 2e 61 45 78 74 20 3d 20 61 4e 65 77  oext.aExt = aNew
232b5 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41 75 74  ;.        wsdAut
232b6 6f 65 78 74 2e 61 45 78 74 5b 77 73 64 41 75 74  oext.aExt[wsdAut
232b7 6f 65 78 74 2e 6e 45 78 74 5d 20 3d 20 78 49 6e  oext.nExt] = xIn
232b8 69 74 3b 0a 20 20 20 20 20 20 20 20 77 73 64 41  it;.        wsdA
232b9 75 74 6f 65 78 74 2e 6e 45 78 74 2b 2b 3b 0a 20  utoext.nExt++;. 
232ba 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
232bb 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
232bc 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20  eave(mutex);.   
232bd 20 61 73 73 65 72 74 28 20 28 72 63 26 30 78 66   assert( (rc&0xf
232be 66 29 3d 3d 72 63 20 29 3b 0a 20 20 20 20 72 65  f)==rc );.    re
232bf 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a  turn rc;.  }.}..
232c0 2f 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20  /*.** Reset the 
232c1 61 75 74 6f 6d 61 74 69 63 20 65 78 74 65 6e 73  automatic extens
232c2 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 6d 65 63 68  ion loading mech
232c3 61 6e 69 73 6d 2e 0a 2a 2f 0a 53 51 4c 49 54 45  anism..*/.SQLITE
232c4 5f 41 50 49 20 76 6f 69 64 20 73 71 6c 69 74 65  _API void sqlite
232c5 33 5f 72 65 73 65 74 5f 61 75 74 6f 5f 65 78 74  3_reset_auto_ext
232c6 65 6e 73 69 6f 6e 28 76 6f 69 64 29 7b 0a 23 69  ension(void){.#i
232c7 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
232c8 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 69 66 28  T_AUTOINIT.  if(
232c9 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
232ca 69 7a 65 28 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b  ize()==SQLITE_OK
232cb 20 29 0a 23 65 6e 64 69 66 0a 20 20 7b 0a 23 69   ).#endif.  {.#i
232cc 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
232cd 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  AFE.    sqlite3_
232ce 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d 20 73  mutex *mutex = s
232cf 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63  qlite3MutexAlloc
232d0 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54  (SQLITE_MUTEX_ST
232d1 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 23 65  ATIC_MASTER);.#e
232d2 6e 64 69 66 0a 20 20 20 20 77 73 64 41 75 74 6f  ndif.    wsdAuto
232d3 65 78 74 49 6e 69 74 3b 0a 20 20 20 20 73 71 6c  extInit;.    sql
232d4 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72  ite3_mutex_enter
232d5 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 73 71 6c  (mutex);.    sql
232d6 69 74 65 33 5f 66 72 65 65 28 77 73 64 41 75 74  ite3_free(wsdAut
232d7 6f 65 78 74 2e 61 45 78 74 29 3b 0a 20 20 20 20  oext.aExt);.    
232d8 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74 20  wsdAutoext.aExt 
232d9 3d 20 30 3b 0a 20 20 20 20 77 73 64 41 75 74 6f  = 0;.    wsdAuto
232da 65 78 74 2e 6e 45 78 74 20 3d 20 30 3b 0a 20 20  ext.nExt = 0;.  
232db 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
232dc 6c 65 61 76 65 28 6d 75 74 65 78 29 3b 0a 20 20  leave(mutex);.  
232dd 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 61 64 20  }.}../*.** Load 
232de 61 6c 6c 20 61 75 74 6f 6d 61 74 69 63 20 65 78  all automatic ex
232df 74 65 6e 73 69 6f 6e 73 2e 0a 2a 2f 0a 53 51 4c  tensions..*/.SQL
232e0 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
232e1 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61 64 45  sqlite3AutoLoadE
232e2 78 74 65 6e 73 69 6f 6e 73 28 73 71 6c 69 74 65  xtensions(sqlite
232e3 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69 3b  3 *db){.  int i;
232e4 0a 20 20 69 6e 74 20 67 6f 20 3d 20 31 3b 0a 20  .  int go = 1;. 
232e5 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
232e6 5f 4f 4b 3b 0a 20 20 69 6e 74 20 28 2a 78 49 6e  _OK;.  int (*xIn
232e7 69 74 29 28 73 71 6c 69 74 65 33 2a 2c 63 68 61  it)(sqlite3*,cha
232e8 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74 65  r**,const sqlite
232e9 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a 29  3_api_routines*)
232ea 3b 0a 0a 20 20 77 73 64 41 75 74 6f 65 78 74 49  ;..  wsdAutoextI
232eb 6e 69 74 3b 0a 20 20 69 66 28 20 77 73 64 41 75  nit;.  if( wsdAu
232ec 74 6f 65 78 74 2e 6e 45 78 74 3d 3d 30 20 29 7b  toext.nExt==0 ){
232ed 0a 20 20 20 20 2f 2a 20 43 6f 6d 6d 6f 6e 20 63  .    /* Common c
232ee 61 73 65 3a 20 65 61 72 6c 79 20 6f 75 74 20 77  ase: early out w
232ef 69 74 68 6f 75 74 20 65 76 65 72 79 20 68 61 76  ithout every hav
232f0 69 6e 67 20 74 6f 20 61 63 71 75 69 72 65 20 61  ing to acquire a
232f1 20 6d 75 74 65 78 20 2a 2f 0a 20 20 20 20 72 65   mutex */.    re
232f2 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
232f3 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30 3b 20 67    }.  for(i=0; g
232f4 6f 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  o; i++){.    cha
232f5 72 20 2a 7a 45 72 72 6d 73 67 20 3d 20 30 3b 0a  r *zErrmsg = 0;.
232f6 23 69 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41  #if SQLITE_THREA
232f7 44 53 41 46 45 0a 20 20 20 20 73 71 6c 69 74 65  DSAFE.    sqlite
232f8 33 5f 6d 75 74 65 78 20 2a 6d 75 74 65 78 20 3d  3_mutex *mutex =
232f9 20 73 71 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c   sqlite3MutexAll
232fa 6f 63 28 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f  oc(SQLITE_MUTEX_
232fb 53 54 41 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a  STATIC_MASTER);.
232fc 23 65 6e 64 69 66 0a 20 20 20 20 73 71 6c 69 74  #endif.    sqlit
232fd 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 6d  e3_mutex_enter(m
232fe 75 74 65 78 29 3b 0a 20 20 20 20 69 66 28 20 69  utex);.    if( i
232ff 3e 3d 77 73 64 41 75 74 6f 65 78 74 2e 6e 45 78  >=wsdAutoext.nEx
23300 74 20 29 7b 0a 20 20 20 20 20 20 78 49 6e 69 74  t ){.      xInit
23301 20 3d 20 30 3b 0a 20 20 20 20 20 20 67 6f 20 3d   = 0;.      go =
23302 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   0;.    }else{. 
23303 20 20 20 20 20 78 49 6e 69 74 20 3d 20 28 69 6e       xInit = (in
23304 74 28 2a 29 28 73 71 6c 69 74 65 33 2a 2c 63 68  t(*)(sqlite3*,ch
23305 61 72 2a 2a 2c 63 6f 6e 73 74 20 73 71 6c 69 74  ar**,const sqlit
23306 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65 73 2a  e3_api_routines*
23307 29 29 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  )).             
23308 20 77 73 64 41 75 74 6f 65 78 74 2e 61 45 78 74   wsdAutoext.aExt
23309 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73  [i];.    }.    s
2330a 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2330b 76 65 28 6d 75 74 65 78 29 3b 0a 20 20 20 20 69  ve(mutex);.    i
2330c 66 28 20 78 49 6e 69 74 20 26 26 20 78 49 6e 69  f( xInit && xIni
2330d 74 28 64 62 2c 20 26 7a 45 72 72 6d 73 67 2c 20  t(db, &zErrmsg, 
2330e 26 73 71 6c 69 74 65 33 41 70 69 73 29 20 29 7b  &sqlite3Apis) ){
2330f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
23310 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
23311 52 52 4f 52 2c 0a 20 20 20 20 20 20 20 20 20 20  RROR,.          
23312 20 20 22 61 75 74 6f 6d 61 74 69 63 20 65 78 74    "automatic ext
23313 65 6e 73 69 6f 6e 20 6c 6f 61 64 69 6e 67 20 66  ension loading f
23314 61 69 6c 65 64 3a 20 25 73 22 2c 20 7a 45 72 72  ailed: %s", zErr
23315 6d 73 67 29 3b 0a 20 20 20 20 20 20 67 6f 20 3d  msg);.      go =
23316 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 53   0;.      rc = S
23317 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
23318 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28     sqlite3_free(
23319 7a 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 7d 0a  zErrmsg);.    }.
2331a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
2331b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
2331c 2a 2a 2a 20 45 6e 64 20 6f 66 20 6c 6f 61 64 65  *** End of loade
2331d 78 74 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  xt.c ***********
2331e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2331f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23320 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
23321 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 70  *** Begin file p
23322 72 61 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  ragma.c ********
23323 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23324 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23325 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 33 20 41  **/./*.** 2003 A
23326 70 72 69 6c 20 36 0a 2a 2a 0a 2a 2a 20 54 68 65  pril 6.**.** The
23327 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
23328 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
23329 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
2332a 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
2332b 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
2332c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
2332d 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
2332e 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
2332f 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
23330 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
23331 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
23332 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
23333 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
23334 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
23335 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
23336 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
23337 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
23338 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23339 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2333a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2333b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2333c 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
2333d 6c 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65  le contains code
2333e 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65   used to impleme
2333f 6e 74 20 74 68 65 20 50 52 41 47 4d 41 20 63 6f  nt the PRAGMA co
23340 6d 6d 61 6e 64 2e 0a 2a 2a 0a 2a 2a 20 24 49 64  mmand..**.** $Id
23341 3a 20 70 72 61 67 6d 61 2e 63 2c 76 20 31 2e 31  : pragma.c,v 1.1
23342 39 34 20 32 30 30 38 2f 31 31 2f 31 37 20 31 39  94 2008/11/17 19
23343 3a 31 38 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39  :18:55 danielk19
23344 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 20  77 Exp $.*/../* 
23345 49 67 6e 6f 72 65 20 74 68 69 73 20 77 68 6f 6c  Ignore this whol
23346 65 20 66 69 6c 65 20 69 66 20 70 72 61 67 6d 61  e file if pragma
23347 73 20 61 72 65 20 64 69 73 61 62 6c 65 64 0a 2a  s are disabled.*
23348 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
23349 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 52 41 47 4d  QLITE_OMIT_PRAGM
2334a 41 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  A) && !defined(S
2334b 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45  QLITE_OMIT_PARSE
2334c 52 29 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70  R)../*.** Interp
2334d 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73 74  ret the given st
2334e 72 69 6e 67 20 61 73 20 61 20 73 61 66 65 74 79  ring as a safety
2334f 20 6c 65 76 65 6c 2e 20 20 52 65 74 75 72 6e 20   level.  Return 
23350 30 20 66 6f 72 20 4f 46 46 2c 0a 2a 2a 20 31 20  0 for OFF,.** 1 
23351 66 6f 72 20 4f 4e 20 6f 72 20 4e 4f 52 4d 41 4c  for ON or NORMAL
23352 20 61 6e 64 20 32 20 66 6f 72 20 46 55 4c 4c 2e   and 2 for FULL.
23353 20 20 52 65 74 75 72 6e 20 31 20 66 6f 72 20 61    Return 1 for a
23354 6e 20 65 6d 70 74 79 20 6f 72 20 0a 2a 2a 20 75  n empty or .** u
23355 6e 72 65 63 6f 67 6e 69 7a 65 64 20 73 74 72 69  nrecognized stri
23356 6e 67 20 61 72 67 75 6d 65 6e 74 2e 0a 2a 2a 0a  ng argument..**.
23357 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65  ** Note that the
23358 20 76 61 6c 75 65 73 20 72 65 74 75 72 6e 65 64   values returned
23359 20 61 72 65 20 6f 6e 65 20 6c 65 73 73 20 74 68   are one less th
2335a 61 74 20 74 68 65 20 76 61 6c 75 65 73 20 74 68  at the values th
2335b 61 74 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20  at.** should be 
2335c 70 61 73 73 65 64 20 69 6e 74 6f 20 73 71 6c 69  passed into sqli
2335d 74 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74  te3BtreeSetSafet
2335e 79 4c 65 76 65 6c 28 29 2e 20 20 54 68 65 20 69  yLevel().  The i
2335f 73 20 64 6f 6e 65 0a 2a 2a 20 74 6f 20 73 75 70  s done.** to sup
23360 70 6f 72 74 20 6c 65 67 61 63 79 20 53 51 4c 20  port legacy SQL 
23361 63 6f 64 65 2e 20 20 54 68 65 20 73 61 66 65 74  code.  The safet
23362 79 20 6c 65 76 65 6c 20 75 73 65 64 20 74 6f 20  y level used to 
23363 62 65 20 62 6f 6f 6c 65 61 6e 0a 2a 2a 20 61 6e  be boolean.** an
23364 64 20 6f 6c 64 65 72 20 73 63 72 69 70 74 73 20  d older scripts 
23365 6d 61 79 20 68 61 76 65 20 75 73 65 64 20 6e 75  may have used nu
23366 6d 62 65 72 73 20 30 20 66 6f 72 20 4f 46 46 20  mbers 0 for OFF 
23367 61 6e 64 20 31 20 66 6f 72 20 4f 4e 2e 0a 2a 2f  and 1 for ON..*/
23368 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 53  .static int getS
23369 61 66 65 74 79 4c 65 76 65 6c 28 63 6f 6e 73 74  afetyLevel(const
2336a 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 20 20 20   char *z){.     
2336b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2336c 20 20 20 20 20 20 20 20 2f 2a 20 31 32 33 34 35          /* 12345
2336d 36 37 38 39 20 31 32 33 34 35 36 37 38 39 20 2a  6789 123456789 *
2336e 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
2336f 20 63 68 61 72 20 7a 54 65 78 74 5b 5d 20 3d 20   char zText[] = 
23370 22 6f 6e 6f 66 66 61 6c 73 65 79 65 73 74 72 75  "onoffalseyestru
23371 65 66 75 6c 6c 22 3b 0a 20 20 73 74 61 74 69 63  efull";.  static
23372 20 63 6f 6e 73 74 20 75 38 20 69 4f 66 66 73 65   const u8 iOffse
23373 74 5b 5d 20 3d 20 7b 30 2c 20 31 2c 20 32 2c 20  t[] = {0, 1, 2, 
23374 34 2c 20 39 2c 20 31 32 2c 20 31 36 7d 3b 0a 20  4, 9, 12, 16};. 
23375 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 38   static const u8
23376 20 69 4c 65 6e 67 74 68 5b 5d 20 3d 20 7b 32 2c   iLength[] = {2,
23377 20 32 2c 20 33 2c 20 35 2c 20 33 2c 20 34 2c 20   2, 3, 5, 3, 4, 
23378 34 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  4};.  static con
23379 73 74 20 75 38 20 69 56 61 6c 75 65 5b 5d 20 3d  st u8 iValue[] =
2337a 20 20 7b 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31    {1, 0, 0, 0, 1
2337b 2c 20 31 2c 20 32 7d 3b 0a 20 20 69 6e 74 20 69  , 1, 2};.  int i
2337c 2c 20 6e 3b 0a 20 20 69 66 28 20 69 73 64 69 67  , n;.  if( isdig
2337d 69 74 28 2a 7a 29 20 29 7b 0a 20 20 20 20 72 65  it(*z) ){.    re
2337e 74 75 72 6e 20 61 74 6f 69 28 7a 29 3b 0a 20 20  turn atoi(z);.  
2337f 7d 0a 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  }.  n = strlen(z
23380 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
23381 41 72 72 61 79 53 69 7a 65 28 69 4c 65 6e 67 74  ArraySize(iLengt
23382 68 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66  h); i++){.    if
23383 28 20 69 4c 65 6e 67 74 68 5b 69 5d 3d 3d 6e 20  ( iLength[i]==n 
23384 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
23385 6d 70 28 26 7a 54 65 78 74 5b 69 4f 66 66 73 65  mp(&zText[iOffse
23386 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30 20 29 7b  t[i]],z,n)==0 ){
23387 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 56  .      return iV
23388 61 6c 75 65 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20  alue[i];.    }. 
23389 20 7d 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d   }.  return 1;.}
2338a 0a 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  ../*.** Interpre
2338b 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
2338c 6e 67 20 61 73 20 61 20 62 6f 6f 6c 65 61 6e 20  ng as a boolean 
2338d 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  value..*/.static
2338e 20 69 6e 74 20 67 65 74 42 6f 6f 6c 65 61 6e 28   int getBoolean(
2338f 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a  const char *z){.
23390 20 20 72 65 74 75 72 6e 20 67 65 74 53 61 66 65    return getSafe
23391 74 79 4c 65 76 65 6c 28 7a 29 26 31 3b 0a 7d 0a  tyLevel(z)&1;.}.
23392 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65 74  ./*.** Interpret
23393 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69 6e   the given strin
23394 67 20 61 73 20 61 20 6c 6f 63 6b 69 6e 67 20 6d  g as a locking m
23395 6f 64 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  ode value..*/.st
23396 61 74 69 63 20 69 6e 74 20 67 65 74 4c 6f 63 6b  atic int getLock
23397 69 6e 67 4d 6f 64 65 28 63 6f 6e 73 74 20 63 68  ingMode(const ch
23398 61 72 20 2a 7a 29 7b 0a 20 20 69 66 28 20 7a 20  ar *z){.  if( z 
23399 29 7b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71  ){.    if( 0==sq
2339a 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
2339b 22 65 78 63 6c 75 73 69 76 65 22 29 20 29 20 72  "exclusive") ) r
2339c 65 74 75 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b  eturn PAGER_LOCK
2339d 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
2339e 45 3b 0a 20 20 20 20 69 66 28 20 30 3d 3d 73 71  E;.    if( 0==sq
2339f 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c 20  lite3StrICmp(z, 
233a0 22 6e 6f 72 6d 61 6c 22 29 20 29 20 72 65 74 75  "normal") ) retu
233a1 72 6e 20 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  rn PAGER_LOCKING
233a2 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 3b 0a 20 20 7d  MODE_NORMAL;.  }
233a3 0a 20 20 72 65 74 75 72 6e 20 50 41 47 45 52 5f  .  return PAGER_
233a4 4c 4f 43 4b 49 4e 47 4d 4f 44 45 5f 51 55 45 52  LOCKINGMODE_QUER
233a5 59 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  Y;.}..#ifndef SQ
233a6 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f 56 41  LITE_OMIT_AUTOVA
233a7 43 55 55 4d 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72  CUUM./*.** Inter
233a8 70 72 65 74 20 74 68 65 20 67 69 76 65 6e 20 73  pret the given s
233a9 74 72 69 6e 67 20 61 73 20 61 6e 20 61 75 74 6f  tring as an auto
233aa 2d 76 61 63 75 75 6d 20 6d 6f 64 65 20 76 61 6c  -vacuum mode val
233ab 75 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f  ue..**.** The fo
233ac 6c 6c 6f 77 69 6e 67 20 73 74 72 69 6e 67 73 2c  llowing strings,
233ad 20 22 6e 6f 6e 65 22 2c 20 22 66 75 6c 6c 22 20   "none", "full" 
233ae 61 6e 64 20 22 69 6e 63 72 65 6d 65 6e 74 61 6c  and "incremental
233af 22 20 61 72 65 20 0a 2a 2a 20 61 63 63 65 70 74  " are .** accept
233b0 61 62 6c 65 2c 20 61 73 20 61 72 65 20 74 68 65  able, as are the
233b1 69 72 20 6e 75 6d 65 72 69 63 20 65 71 75 69 76  ir numeric equiv
233b2 61 6c 65 6e 74 73 3a 20 30 2c 20 31 20 61 6e 64  alents: 0, 1 and
233b3 20 32 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e   2 respectively.
233b4 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
233b5 65 74 41 75 74 6f 56 61 63 75 75 6d 28 63 6f 6e  etAutoVacuum(con
233b6 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
233b7 6e 74 20 69 3b 0a 20 20 69 66 28 20 30 3d 3d 73  nt i;.  if( 0==s
233b8 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
233b9 20 22 6e 6f 6e 65 22 29 20 29 20 72 65 74 75 72   "none") ) retur
233ba 6e 20 42 54 52 45 45 5f 41 55 54 4f 56 41 43 55  n BTREE_AUTOVACU
233bb 55 4d 5f 4e 4f 4e 45 3b 0a 20 20 69 66 28 20 30  UM_NONE;.  if( 0
233bc 3d 3d 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ==sqlite3StrICmp
233bd 28 7a 2c 20 22 66 75 6c 6c 22 29 20 29 20 72 65  (z, "full") ) re
233be 74 75 72 6e 20 42 54 52 45 45 5f 41 55 54 4f 56  turn BTREE_AUTOV
233bf 41 43 55 55 4d 5f 46 55 4c 4c 3b 0a 20 20 69 66  ACUUM_FULL;.  if
233c0 28 20 30 3d 3d 73 71 6c 69 74 65 33 53 74 72 49  ( 0==sqlite3StrI
233c1 43 6d 70 28 7a 2c 20 22 69 6e 63 72 65 6d 65 6e  Cmp(z, "incremen
233c2 74 61 6c 22 29 20 29 20 72 65 74 75 72 6e 20 42  tal") ) return B
233c3 54 52 45 45 5f 41 55 54 4f 56 41 43 55 55 4d 5f  TREE_AUTOVACUUM_
233c4 49 4e 43 52 3b 0a 20 20 69 20 3d 20 61 74 6f 69  INCR;.  i = atoi
233c5 28 7a 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 28  (z);.  return ((
233c6 69 3e 3d 30 26 26 69 3c 3d 32 29 3f 69 3a 30 29  i>=0&&i<=2)?i:0)
233c7 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 69 66  ;.}.#endif /* if
233c8 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
233c9 5f 41 55 54 4f 56 41 43 55 55 4d 20 2a 2f 0a 0a  _AUTOVACUUM */..
233ca 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
233cb 4d 49 54 5f 50 41 47 45 52 5f 50 52 41 47 4d 41  MIT_PAGER_PRAGMA
233cc 53 0a 2f 2a 0a 2a 2a 20 49 6e 74 65 72 70 72 65  S./*.** Interpre
233cd 74 20 74 68 65 20 67 69 76 65 6e 20 73 74 72 69  t the given stri
233ce 6e 67 20 61 73 20 61 20 74 65 6d 70 20 64 62 20  ng as a temp db 
233cf 6c 6f 63 61 74 69 6f 6e 2e 20 52 65 74 75 72 6e  location. Return
233d0 20 31 20 66 6f 72 20 66 69 6c 65 0a 2a 2a 20 62   1 for file.** b
233d1 61 63 6b 65 64 20 74 65 6d 70 6f 72 61 72 79 20  acked temporary 
233d2 64 61 74 61 62 61 73 65 73 2c 20 32 20 66 6f 72  databases, 2 for
233d3 20 74 68 65 20 52 65 64 2d 42 6c 61 63 6b 20 74   the Red-Black t
233d4 72 65 65 20 69 6e 20 6d 65 6d 6f 72 79 20 64 61  ree in memory da
233d5 74 61 62 61 73 65 0a 2a 2a 20 61 6e 64 20 30 20  tabase.** and 0 
233d6 74 6f 20 75 73 65 20 74 68 65 20 63 6f 6d 70 69  to use the compi
233d7 6c 65 2d 74 69 6d 65 20 64 65 66 61 75 6c 74 2e  le-time default.
233d8 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 67  .*/.static int g
233d9 65 74 54 65 6d 70 53 74 6f 72 65 28 63 6f 6e 73  etTempStore(cons
233da 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69 66  t char *z){.  if
233db 28 20 7a 5b 30 5d 3e 3d 27 30 27 20 26 26 20 7a  ( z[0]>='0' && z
233dc 5b 30 5d 3c 3d 27 32 27 20 29 7b 0a 20 20 20 20  [0]<='2' ){.    
233dd 72 65 74 75 72 6e 20 7a 5b 30 5d 20 2d 20 27 30  return z[0] - '0
233de 27 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  ';.  }else if( s
233df 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 2c  qlite3StrICmp(z,
233e0 20 22 66 69 6c 65 22 29 3d 3d 30 20 29 7b 0a 20   "file")==0 ){. 
233e1 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
233e2 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33  else if( sqlite3
233e3 53 74 72 49 43 6d 70 28 7a 2c 20 22 6d 65 6d 6f  StrICmp(z, "memo
233e4 72 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 72  ry")==0 ){.    r
233e5 65 74 75 72 6e 20 32 3b 0a 20 20 7d 65 6c 73 65  eturn 2;.  }else
233e6 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
233e7 20 20 7d 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20    }.}.#endif /* 
233e8 53 51 4c 49 54 45 5f 50 41 47 45 52 5f 50 52 41  SQLITE_PAGER_PRA
233e9 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
233ea 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
233eb 45 52 5f 50 52 41 47 4d 41 53 0a 2f 2a 0a 2a 2a  ER_PRAGMAS./*.**
233ec 20 49 6e 76 61 6c 69 64 61 74 65 20 74 65 6d 70   Invalidate temp
233ed 20 73 74 6f 72 61 67 65 2c 20 65 69 74 68 65 72   storage, either
233ee 20 77 68 65 6e 20 74 68 65 20 74 65 6d 70 20 73   when the temp s
233ef 74 6f 72 61 67 65 20 69 73 20 63 68 61 6e 67 65  torage is change
233f0 64 0a 2a 2a 20 66 72 6f 6d 20 64 65 66 61 75 6c  d.** from defaul
233f1 74 2c 20 6f 72 20 77 68 65 6e 20 27 66 69 6c 65  t, or when 'file
233f2 27 20 61 6e 64 20 74 68 65 20 74 65 6d 70 5f 73  ' and the temp_s
233f3 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 20 68  tore_directory h
233f4 61 73 20 63 68 61 6e 67 65 64 0a 2a 2f 0a 73 74  as changed.*/.st
233f5 61 74 69 63 20 69 6e 74 20 69 6e 76 61 6c 69 64  atic int invalid
233f6 61 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 50  ateTempStorage(P
233f7 61 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20  arse *pParse){. 
233f8 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70   sqlite3 *db = p
233f9 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28  Parse->db;.  if(
233fa 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 21   db->aDb[1].pBt!
233fb 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 64  =0 ){.    if( !d
233fc 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 7c 7c  b->autoCommit ||
233fd 20 73 71 6c 69 74 65 33 42 74 72 65 65 49 73 49   sqlite3BtreeIsI
233fe 6e 52 65 61 64 54 72 61 6e 73 28 64 62 2d 3e 61  nReadTrans(db->a
233ff 44 62 5b 31 5d 2e 70 42 74 29 20 29 7b 0a 20 20  Db[1].pBt) ){.  
23400 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
23401 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74 65 6d  Msg(pParse, "tem
23402 70 6f 72 61 72 79 20 73 74 6f 72 61 67 65 20 63  porary storage c
23403 61 6e 6e 6f 74 20 62 65 20 63 68 61 6e 67 65 64  annot be changed
23404 20 22 0a 20 20 20 20 20 20 20 20 22 66 72 6f 6d   ".        "from
23405 20 77 69 74 68 69 6e 20 61 20 74 72 61 6e 73 61   within a transa
23406 63 74 69 6f 6e 22 29 3b 0a 20 20 20 20 20 20 72  ction");.      r
23407 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
23408 4f 52 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  OR;.    }.    sq
23409 6c 69 74 65 33 42 74 72 65 65 43 6c 6f 73 65 28  lite3BtreeClose(
2340a 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 42 74 29 3b  db->aDb[1].pBt);
2340b 0a 20 20 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  .    db->aDb[1].
2340c 70 42 74 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  pBt = 0;.    sql
2340d 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
2340e 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
2340f 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
23410 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
23411 20 2f 2a 20 53 51 4c 49 54 45 5f 50 41 47 45 52   /* SQLITE_PAGER
23412 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69 66  _PRAGMAS */..#if
23413 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23414 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 2f  _PAGER_PRAGMAS./
23415 2a 0a 2a 2a 20 49 66 20 74 68 65 20 54 45 4d 50  *.** If the TEMP
23416 20 64 61 74 61 62 61 73 65 20 69 73 20 6f 70 65   database is ope
23417 6e 2c 20 63 6c 6f 73 65 20 69 74 20 61 6e 64 20  n, close it and 
23418 6d 61 72 6b 20 74 68 65 20 64 61 74 61 62 61 73  mark the databas
23419 65 20 73 63 68 65 6d 61 0a 2a 2a 20 61 73 20 6e  e schema.** as n
2341a 65 65 64 69 6e 67 20 72 65 6c 6f 61 64 69 6e 67  eeding reloading
2341b 2e 20 20 54 68 69 73 20 6d 75 73 74 20 62 65 20  .  This must be 
2341c 64 6f 6e 65 20 77 68 65 6e 20 75 73 69 6e 67 20  done when using 
2341d 74 68 65 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f  the SQLITE_TEMP_
2341e 53 54 4f 52 45 0a 2a 2a 20 6f 72 20 44 45 46 41  STORE.** or DEFA
2341f 55 4c 54 5f 54 45 4d 50 5f 53 54 4f 52 45 20 70  ULT_TEMP_STORE p
23420 72 61 67 6d 61 73 2e 0a 2a 2f 0a 73 74 61 74 69  ragmas..*/.stati
23421 63 20 69 6e 74 20 63 68 61 6e 67 65 54 65 6d 70  c int changeTemp
23422 53 74 6f 72 61 67 65 28 50 61 72 73 65 20 2a 70  Storage(Parse *p
23423 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68 61  Parse, const cha
23424 72 20 2a 7a 53 74 6f 72 61 67 65 54 79 70 65 29  r *zStorageType)
23425 7b 0a 20 20 69 6e 74 20 74 73 20 3d 20 67 65 74  {.  int ts = get
23426 54 65 6d 70 53 74 6f 72 65 28 7a 53 74 6f 72 61  TempStore(zStora
23427 67 65 54 79 70 65 29 3b 0a 20 20 73 71 6c 69 74  geType);.  sqlit
23428 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
23429 3e 64 62 3b 0a 20 20 69 66 28 20 64 62 2d 3e 74  >db;.  if( db->t
2342a 65 6d 70 5f 73 74 6f 72 65 3d 3d 74 73 20 29 20  emp_store==ts ) 
2342b 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
2342c 3b 0a 20 20 69 66 28 20 69 6e 76 61 6c 69 64 61  ;.  if( invalida
2342d 74 65 54 65 6d 70 53 74 6f 72 61 67 65 28 20 70  teTempStorage( p
2342e 50 61 72 73 65 20 29 20 21 3d 20 53 51 4c 49 54  Parse ) != SQLIT
2342f 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74 75  E_OK ){.    retu
23430 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b  rn SQLITE_ERROR;
23431 0a 20 20 7d 0a 20 20 64 62 2d 3e 74 65 6d 70 5f  .  }.  db->temp_
23432 73 74 6f 72 65 20 3d 20 74 73 3b 0a 20 20 72 65  store = ts;.  re
23433 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
23434 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
23435 54 45 5f 50 41 47 45 52 5f 50 52 41 47 4d 41 53  TE_PAGER_PRAGMAS
23436 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72   */../*.** Gener
23437 61 74 65 20 63 6f 64 65 20 74 6f 20 72 65 74 75  ate code to retu
23438 72 6e 20 61 20 73 69 6e 67 6c 65 20 69 6e 74 65  rn a single inte
23439 67 65 72 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74  ger value..*/.st
2343a 61 74 69 63 20 76 6f 69 64 20 72 65 74 75 72 6e  atic void return
2343b 53 69 6e 67 6c 65 49 6e 74 28 50 61 72 73 65 20  SingleInt(Parse 
2343c 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63  *pParse, const c
2343d 68 61 72 20 2a 7a 4c 61 62 65 6c 2c 20 69 6e 74  har *zLabel, int
2343e 20 76 61 6c 75 65 29 7b 0a 20 20 56 64 62 65 20   value){.  Vdbe 
2343f 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
23440 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
23441 6e 74 20 6d 65 6d 20 3d 20 2b 2b 70 50 61 72 73  nt mem = ++pPars
23442 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 73 71 6c 69 74  e->nMem;.  sqlit
23443 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23444 4f 50 5f 49 6e 74 65 67 65 72 2c 20 76 61 6c 75  OP_Integer, valu
23445 65 2c 20 6d 65 6d 29 3b 0a 20 20 69 66 28 20 70  e, mem);.  if( p
23446 50 61 72 73 65 2d 3e 65 78 70 6c 61 69 6e 3d 3d  Parse->explain==
23447 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  0 ){.    sqlite3
23448 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
23449 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  , 1);.    sqlite
2344a 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
2344b 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
2344c 4d 45 2c 20 7a 4c 61 62 65 6c 2c 20 53 51 4c 49  ME, zLabel, SQLI
2344d 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a  TE_STATIC);.  }.
2344e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2344f 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74  Op2(v, OP_Result
23450 52 6f 77 2c 20 6d 65 6d 2c 20 31 29 3b 0a 7d 0a  Row, mem, 1);.}.
23451 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
23452 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41  OMIT_FLAG_PRAGMA
23453 53 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f  S./*.** Check to
23454 20 73 65 65 20 69 66 20 7a 52 69 67 68 74 20 61   see if zRight a
23455 6e 64 20 7a 4c 65 66 74 20 72 65 66 65 72 20 74  nd zLeft refer t
23456 6f 20 61 20 70 72 61 67 6d 61 20 74 68 61 74 20  o a pragma that 
23457 71 75 65 72 69 65 73 0a 2a 2a 20 6f 72 20 63 68  queries.** or ch
23458 61 6e 67 65 73 20 6f 6e 65 20 6f 66 20 74 68 65  anges one of the
23459 20 66 6c 61 67 73 20 69 6e 20 64 62 2d 3e 66 6c   flags in db->fl
2345a 61 67 73 2e 20 20 52 65 74 75 72 6e 20 31 20 69  ags.  Return 1 i
2345b 66 20 73 6f 20 61 6e 64 20 30 20 69 66 20 6e 6f  f so and 0 if no
2345c 74 2e 0a 2a 2a 20 41 6c 73 6f 2c 20 69 6d 70 6c  t..** Also, impl
2345d 65 6d 65 6e 74 20 74 68 65 20 70 72 61 67 6d 61  ement the pragma
2345e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2345f 66 6c 61 67 50 72 61 67 6d 61 28 50 61 72 73 65  flagPragma(Parse
23460 20 2a 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20   *pParse, const 
23461 63 68 61 72 20 2a 7a 4c 65 66 74 2c 20 63 6f 6e  char *zLeft, con
23462 73 74 20 63 68 61 72 20 2a 7a 52 69 67 68 74 29  st char *zRight)
23463 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  {.  static const
23464 20 73 74 72 75 63 74 20 73 50 72 61 67 6d 61 54   struct sPragmaT
23465 79 70 65 20 7b 0a 20 20 20 20 63 6f 6e 73 74 20  ype {.    const 
23466 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 2f 2a  char *zName;  /*
23467 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 70 72 61   Name of the pra
23468 67 6d 61 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6d  gma */.    int m
23469 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20 20 2f  ask;           /
2346a 2a 20 4d 61 73 6b 20 66 6f 72 20 74 68 65 20 64  * Mask for the d
2346b 62 2d 3e 66 6c 61 67 73 20 76 61 6c 75 65 20 2a  b->flags value *
2346c 2f 0a 20 20 7d 20 61 50 72 61 67 6d 61 5b 5d 20  /.  } aPragma[] 
2346d 3d 20 7b 0a 20 20 20 20 7b 20 22 66 75 6c 6c 5f  = {.    { "full_
2346e 63 6f 6c 75 6d 6e 5f 6e 61 6d 65 73 22 2c 20 20  column_names",  
2346f 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46 75 6c        SQLITE_Ful
23470 6c 43 6f 6c 4e 61 6d 65 73 20 20 7d 2c 0a 20 20  lColNames  },.  
23471 20 20 7b 20 22 73 68 6f 72 74 5f 63 6f 6c 75 6d    { "short_colum
23472 6e 5f 6e 61 6d 65 73 22 2c 20 20 20 20 20 20 20  n_names",       
23473 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e  SQLITE_ShortColN
23474 61 6d 65 73 20 7d 2c 0a 20 20 20 20 7b 20 22 63  ames },.    { "c
23475 6f 75 6e 74 5f 63 68 61 6e 67 65 73 22 2c 20 20  ount_changes",  
23476 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
23477 5f 43 6f 75 6e 74 52 6f 77 73 20 20 20 20 20 7d  _CountRows     }
23478 2c 0a 20 20 20 20 7b 20 22 65 6d 70 74 79 5f 72  ,.    { "empty_r
23479 65 73 75 6c 74 5f 63 61 6c 6c 62 61 63 6b 73 22  esult_callbacks"
2347a 2c 20 20 20 53 51 4c 49 54 45 5f 4e 75 6c 6c 43  ,   SQLITE_NullC
2347b 61 6c 6c 62 61 63 6b 20 20 7d 2c 0a 20 20 20 20  allback  },.    
2347c 7b 20 22 6c 65 67 61 63 79 5f 66 69 6c 65 5f 66  { "legacy_file_f
2347d 6f 72 6d 61 74 22 2c 20 20 20 20 20 20 20 53 51  ormat",       SQ
2347e 4c 49 54 45 5f 4c 65 67 61 63 79 46 69 6c 65 46  LITE_LegacyFileF
2347f 6d 74 20 7d 2c 0a 20 20 20 20 7b 20 22 66 75 6c  mt },.    { "ful
23480 6c 66 73 79 6e 63 22 2c 20 20 20 20 20 20 20 20  lfsync",        
23481 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46          SQLITE_F
23482 75 6c 6c 46 53 79 6e 63 20 20 20 20 20 7d 2c 0a  ullFSync     },.
23483 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44 45  #ifdef SQLITE_DE
23484 42 55 47 0a 20 20 20 20 7b 20 22 73 71 6c 5f 74  BUG.    { "sql_t
23485 72 61 63 65 22 2c 20 20 20 20 20 20 20 20 20 20  race",          
23486 20 20 20 20 20 20 53 51 4c 49 54 45 5f 53 71 6c        SQLITE_Sql
23487 54 72 61 63 65 20 20 20 20 20 20 7d 2c 0a 20 20  Trace      },.  
23488 20 20 7b 20 22 76 64 62 65 5f 6c 69 73 74 69 6e    { "vdbe_listin
23489 67 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  g",             
2348a 53 51 4c 49 54 45 5f 56 64 62 65 4c 69 73 74 69  SQLITE_VdbeListi
2348b 6e 67 20 20 20 7d 2c 0a 20 20 20 20 7b 20 22 76  ng   },.    { "v
2348c 64 62 65 5f 74 72 61 63 65 22 2c 20 20 20 20 20  dbe_trace",     
2348d 20 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45            SQLITE
2348e 5f 56 64 62 65 54 72 61 63 65 20 20 20 20 20 7d  _VdbeTrace     }
2348f 2c 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  ,.#endif.#ifndef
23490 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 48 45   SQLITE_OMIT_CHE
23491 43 4b 0a 20 20 20 20 7b 20 22 69 67 6e 6f 72 65  CK.    { "ignore
23492 5f 63 68 65 63 6b 5f 63 6f 6e 73 74 72 61 69 6e  _check_constrain
23493 74 73 22 2c 20 53 51 4c 49 54 45 5f 49 67 6e 6f  ts", SQLITE_Igno
23494 72 65 43 68 65 63 6b 73 20 20 7d 2c 0a 23 65 6e  reChecks  },.#en
23495 64 69 66 0a 20 20 20 20 2f 2a 20 54 68 65 20 66  dif.    /* The f
23496 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 56 45 52 59  ollowing is VERY
23497 20 65 78 70 65 72 69 6d 65 6e 74 61 6c 20 2a 2f   experimental */
23498 0a 20 20 20 20 7b 20 22 77 72 69 74 61 62 6c 65  .    { "writable
23499 5f 73 63 68 65 6d 61 22 2c 20 20 20 20 20 20 20  _schema",       
2349a 20 20 20 53 51 4c 49 54 45 5f 57 72 69 74 65 53     SQLITE_WriteS
2349b 63 68 65 6d 61 7c 53 51 4c 49 54 45 5f 52 65 63  chema|SQLITE_Rec
2349c 6f 76 65 72 79 4d 6f 64 65 20 7d 2c 0a 20 20 20  overyMode },.   
2349d 20 7b 20 22 6f 6d 69 74 5f 72 65 61 64 6c 6f 63   { "omit_readloc
2349e 6b 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 53  k",            S
2349f 51 4c 49 54 45 5f 4e 6f 52 65 61 64 6c 6f 63 6b  QLITE_NoReadlock
234a0 20 20 20 20 7d 2c 0a 0a 20 20 20 20 2f 2a 20 54      },..    /* T
234a1 4f 44 4f 3a 20 4d 61 79 62 65 20 69 74 20 73 68  ODO: Maybe it sh
234a2 6f 75 6c 64 6e 27 74 20 62 65 20 70 6f 73 73 69  ouldn't be possi
234a3 62 6c 65 20 74 6f 20 63 68 61 6e 67 65 20 74 68  ble to change th
234a4 65 20 52 65 61 64 55 6e 63 6f 6d 6d 69 74 74 65  e ReadUncommitte
234a5 64 0a 20 20 20 20 2a 2a 20 66 6c 61 67 20 69 66  d.    ** flag if
234a6 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 61   there are any a
234a7 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e 74 73  ctive statements
234a8 2e 20 2a 2f 0a 20 20 20 20 7b 20 22 72 65 61 64  . */.    { "read
234a9 5f 75 6e 63 6f 6d 6d 69 74 74 65 64 22 2c 20 20  _uncommitted",  
234aa 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 52 65         SQLITE_Re
234ab 61 64 55 6e 63 6f 6d 6d 69 74 74 65 64 20 7d 2c  adUncommitted },
234ac 0a 20 20 7d 3b 0a 20 20 69 6e 74 20 69 3b 0a 20  .  };.  int i;. 
234ad 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 73 50   const struct sP
234ae 72 61 67 6d 61 54 79 70 65 20 2a 70 3b 0a 20 20  ragmaType *p;.  
234af 66 6f 72 28 69 3d 30 2c 20 70 3d 61 50 72 61 67  for(i=0, p=aPrag
234b0 6d 61 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28  ma; i<ArraySize(
234b1 61 50 72 61 67 6d 61 29 3b 20 69 2b 2b 2c 20 70  aPragma); i++, p
234b2 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ++){.    if( sql
234b3 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
234b4 74 2c 20 70 2d 3e 7a 4e 61 6d 65 29 3d 3d 30 20  t, p->zName)==0 
234b5 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
234b6 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
234b7 62 3b 0a 20 20 20 20 20 20 56 64 62 65 20 2a 76  b;.      Vdbe *v
234b8 3b 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69  ;.      v = sqli
234b9 74 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73  te3GetVdbe(pPars
234ba 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 76 20  e);.      if( v 
234bb 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a  ){.        if( z
234bc 52 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  Right==0 ){.    
234bd 20 20 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67        returnSing
234be 6c 65 49 6e 74 28 70 50 61 72 73 65 2c 20 70 2d  leInt(pParse, p-
234bf 3e 7a 4e 61 6d 65 2c 20 28 64 62 2d 3e 66 6c 61  >zName, (db->fla
234c0 67 73 20 26 20 70 2d 3e 6d 61 73 6b 29 21 3d 30  gs & p->mask)!=0
234c1 20 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73   );.        }els
234c2 65 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  e{.          if(
234c3 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69 67   getBoolean(zRig
234c4 68 74 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ht) ){.         
234c5 20 20 20 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20     db->flags |= 
234c6 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20 20 20 20 20  p->mask;.       
234c7 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
234c8 20 20 20 20 20 20 64 62 2d 3e 66 6c 61 67 73 20        db->flags 
234c9 26 3d 20 7e 70 2d 3e 6d 61 73 6b 3b 0a 20 20 20  &= ~p->mask;.   
234ca 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20         }..      
234cb 20 20 20 20 2f 2a 20 4d 61 6e 79 20 6f 66 20 74      /* Many of t
234cc 68 65 20 66 6c 61 67 2d 70 72 61 67 6d 61 73 20  he flag-pragmas 
234cd 6d 6f 64 69 66 79 20 74 68 65 20 63 6f 64 65 20  modify the code 
234ce 67 65 6e 65 72 61 74 65 64 20 62 79 20 74 68 65  generated by the
234cf 20 53 51 4c 20 0a 20 20 20 20 20 20 20 20 20 20   SQL .          
234d0 2a 2a 20 63 6f 6d 70 69 6c 65 72 20 28 65 67 2e  ** compiler (eg.
234d1 20 63 6f 75 6e 74 5f 63 68 61 6e 67 65 73 29 2e   count_changes).
234d2 20 53 6f 20 61 64 64 20 61 6e 20 6f 70 63 6f 64   So add an opcod
234d3 65 20 74 6f 20 65 78 70 69 72 65 20 61 6c 6c 0a  e to expire all.
234d4 20 20 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 6d            ** com
234d5 70 69 6c 65 64 20 53 51 4c 20 73 74 61 74 65 6d  piled SQL statem
234d6 65 6e 74 73 20 61 66 74 65 72 20 6d 6f 64 69 66  ents after modif
234d7 79 69 6e 67 20 61 20 70 72 61 67 6d 61 20 76 61  ying a pragma va
234d8 6c 75 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  lue..          *
234d9 2f 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  /.          sqli
234da 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
234db 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20 30   OP_Expire, 0, 0
234dc 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
234dd 20 20 20 7d 0a 0a 20 20 20 20 20 20 72 65 74 75     }..      retu
234de 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  rn 1;.    }.  }.
234df 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65    return 0;.}.#e
234e0 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
234e1 4d 49 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53  MIT_FLAG_PRAGMAS
234e2 20 2a 2f 0a 0a 73 74 61 74 69 63 20 63 6f 6e 73   */..static cons
234e3 74 20 63 68 61 72 20 2a 61 63 74 69 6f 6e 4e 61  t char *actionNa
234e4 6d 65 28 75 38 20 61 63 74 69 6f 6e 29 7b 0a 20  me(u8 action){. 
234e5 20 73 77 69 74 63 68 28 20 61 63 74 69 6f 6e 20   switch( action 
234e6 29 7b 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 53  ){.    case OE_S
234e7 65 74 4e 75 6c 6c 3a 20 20 72 65 74 75 72 6e 20  etNull:  return 
234e8 22 53 45 54 20 4e 55 4c 4c 22 3b 0a 20 20 20 20  "SET NULL";.    
234e9 63 61 73 65 20 4f 45 5f 53 65 74 44 66 6c 74 3a  case OE_SetDflt:
234ea 20 20 72 65 74 75 72 6e 20 22 53 45 54 20 44 45    return "SET DE
234eb 46 41 55 4c 54 22 3b 0a 20 20 20 20 63 61 73 65  FAULT";.    case
234ec 20 4f 45 5f 52 65 73 74 72 69 63 74 3a 20 72 65   OE_Restrict: re
234ed 74 75 72 6e 20 22 52 45 53 54 52 49 43 54 22 3b  turn "RESTRICT";
234ee 0a 20 20 20 20 63 61 73 65 20 4f 45 5f 43 61 73  .    case OE_Cas
234ef 63 61 64 65 3a 20 20 72 65 74 75 72 6e 20 22 43  cade:  return "C
234f0 41 53 43 41 44 45 22 3b 0a 20 20 7d 0a 20 20 72  ASCADE";.  }.  r
234f1 65 74 75 72 6e 20 22 22 3b 0a 7d 0a 0a 2f 2a 0a  eturn "";.}../*.
234f2 2a 2a 20 50 72 6f 63 65 73 73 20 61 20 70 72 61  ** Process a pra
234f3 67 6d 61 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  gma statement.  
234f4 0a 2a 2a 0a 2a 2a 20 50 72 61 67 6d 61 73 20 61  .**.** Pragmas a
234f5 72 65 20 6f 66 20 74 68 69 73 20 66 6f 72 6d 3a  re of this form:
234f6 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 50 52 41 47  .**.**      PRAG
234f7 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 69 64  MA [database.]id
234f8 20 5b 3d 20 76 61 6c 75 65 5d 0a 2a 2a 0a 2a 2a   [= value].**.**
234f9 20 54 68 65 20 69 64 65 6e 74 69 66 69 65 72 20   The identifier 
234fa 6d 69 67 68 74 20 61 6c 73 6f 20 62 65 20 61 20  might also be a 
234fb 73 74 72 69 6e 67 2e 20 20 54 68 65 20 76 61 6c  string.  The val
234fc 75 65 20 69 73 20 61 20 73 74 72 69 6e 67 2c 20  ue is a string, 
234fd 61 6e 64 0a 2a 2a 20 69 64 65 6e 74 69 66 69 65  and.** identifie
234fe 72 2c 20 6f 72 20 61 20 6e 75 6d 62 65 72 2e 20  r, or a number. 
234ff 20 49 66 20 6d 69 6e 75 73 46 6c 61 67 20 69 73   If minusFlag is
23500 20 74 72 75 65 2c 20 74 68 65 6e 20 74 68 65 20   true, then the 
23501 76 61 6c 75 65 20 69 73 0a 2a 2a 20 61 20 6e 75  value is.** a nu
23502 6d 62 65 72 20 74 68 61 74 20 77 61 73 20 70 72  mber that was pr
23503 65 63 65 64 65 64 20 62 79 20 61 20 6d 69 6e 75  eceded by a minu
23504 73 20 73 69 67 6e 2e 0a 2a 2a 0a 2a 2a 20 49 66  s sign..**.** If
23505 20 74 68 65 20 6c 65 66 74 20 73 69 64 65 20 69   the left side i
23506 73 20 22 64 61 74 61 62 61 73 65 2e 69 64 22 20  s "database.id" 
23507 74 68 65 6e 20 70 49 64 31 20 69 73 20 74 68 65  then pId1 is the
23508 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a   database name.*
23509 2a 20 61 6e 64 20 70 49 64 32 20 69 73 20 74 68  * and pId2 is th
2350a 65 20 69 64 2e 20 20 49 66 20 74 68 65 20 6c 65  e id.  If the le
2350b 66 74 20 73 69 64 65 20 69 73 20 6a 75 73 74 20  ft side is just 
2350c 22 69 64 22 20 74 68 65 6e 20 70 49 64 31 20 69  "id" then pId1 i
2350d 73 20 74 68 65 0a 2a 2a 20 69 64 20 61 6e 64 20  s the.** id and 
2350e 70 49 64 32 20 69 73 20 61 6e 79 20 65 6d 70 74  pId2 is any empt
2350f 79 20 73 74 72 69 6e 67 2e 0a 2a 2f 0a 53 51 4c  y string..*/.SQL
23510 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
23511 20 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 0a   sqlite3Pragma(.
23512 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
23513 20 0a 20 20 54 6f 6b 65 6e 20 2a 70 49 64 31 2c   .  Token *pId1,
23514 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
23515 20 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61   part of [databa
23516 73 65 2e 5d 69 64 20 66 69 65 6c 64 20 2a 2f 0a  se.]id field */.
23517 20 20 54 6f 6b 65 6e 20 2a 70 49 64 32 2c 20 20    Token *pId2,  
23518 20 20 20 20 20 20 2f 2a 20 53 65 63 6f 6e 64 20        /* Second 
23519 70 61 72 74 20 6f 66 20 5b 64 61 74 61 62 61 73  part of [databas
2351a 65 2e 5d 69 64 20 66 69 65 6c 64 2c 20 6f 72 20  e.]id field, or 
2351b 4e 55 4c 4c 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  NULL */.  Token 
2351c 2a 70 56 61 6c 75 65 2c 20 20 20 20 20 20 2f 2a  *pValue,      /*
2351d 20 54 6f 6b 65 6e 20 66 6f 72 20 3c 76 61 6c 75   Token for <valu
2351e 65 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20  e>, or NULL */. 
2351f 20 69 6e 74 20 6d 69 6e 75 73 46 6c 61 67 20 20   int minusFlag  
23520 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
23521 61 20 27 2d 27 20 73 69 67 6e 20 70 72 65 63 65  a '-' sign prece
23522 64 65 64 20 3c 76 61 6c 75 65 3e 20 2a 2f 0a 29  ded <value> */.)
23523 7b 0a 20 20 63 68 61 72 20 2a 7a 4c 65 66 74 20  {.  char *zLeft 
23524 3d 20 30 3b 20 20 20 20 20 20 20 2f 2a 20 4e 75  = 0;       /* Nu
23525 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
23526 2d 38 20 73 74 72 69 6e 67 20 3c 69 64 3e 20 2a  -8 string <id> *
23527 2f 0a 20 20 63 68 61 72 20 2a 7a 52 69 67 68 74  /.  char *zRight
23528 20 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4e 75   = 0;      /* Nu
23529 6c 2d 74 65 72 6d 69 6e 61 74 65 64 20 55 54 46  l-terminated UTF
2352a 2d 38 20 73 74 72 69 6e 67 20 3c 76 61 6c 75 65  -8 string <value
2352b 3e 2c 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a 20 20  >, or NULL */.  
2352c 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20  const char *zDb 
2352d 3d 20 30 3b 20 20 20 2f 2a 20 54 68 65 20 64 61  = 0;   /* The da
2352e 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
2352f 20 54 6f 6b 65 6e 20 2a 70 49 64 3b 20 20 20 20   Token *pId;    
23530 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
23531 65 72 20 74 6f 20 3c 69 64 3e 20 74 6f 6b 65 6e  er to <id> token
23532 20 2a 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20   */.  int iDb;  
23533 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
23534 44 61 74 61 62 61 73 65 20 69 6e 64 65 78 20 66  Database index f
23535 6f 72 20 3c 64 61 74 61 62 61 73 65 3e 20 2a 2f  or <database> */
23536 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
23537 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 44   pParse->db;.  D
23538 62 20 2a 70 44 62 3b 0a 20 20 56 64 62 65 20 2a  b *pDb;.  Vdbe *
23539 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
2353a 65 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  e = sqlite3VdbeC
2353b 72 65 61 74 65 28 64 62 29 3b 0a 20 20 69 66 28  reate(db);.  if(
2353c 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   v==0 ) return;.
2353d 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d    pParse->nMem =
2353e 20 32 3b 0a 0a 20 20 2f 2a 20 49 6e 74 65 72 70   2;..  /* Interp
2353f 72 65 74 20 74 68 65 20 5b 64 61 74 61 62 61 73  ret the [databas
23540 65 2e 5d 20 70 61 72 74 20 6f 66 20 74 68 65 20  e.] part of the 
23541 70 72 61 67 6d 61 20 73 74 61 74 65 6d 65 6e 74  pragma statement
23542 2e 20 69 44 62 20 69 73 20 74 68 65 0a 20 20 2a  . iDb is the.  *
23543 2a 20 69 6e 64 65 78 20 6f 66 20 74 68 65 20 64  * index of the d
23544 61 74 61 62 61 73 65 20 74 68 69 73 20 70 72 61  atabase this pra
23545 67 6d 61 20 69 73 20 62 65 69 6e 67 20 61 70 70  gma is being app
23546 6c 69 65 64 20 74 6f 20 69 6e 20 64 62 2e 61 44  lied to in db.aD
23547 62 5b 5d 2e 20 2a 2f 0a 20 20 69 44 62 20 3d 20  b[]. */.  iDb = 
23548 73 71 6c 69 74 65 33 54 77 6f 50 61 72 74 4e 61  sqlite3TwoPartNa
23549 6d 65 28 70 50 61 72 73 65 2c 20 70 49 64 31 2c  me(pParse, pId1,
2354a 20 70 49 64 32 2c 20 26 70 49 64 29 3b 0a 20 20   pId2, &pId);.  
2354b 69 66 28 20 69 44 62 3c 30 20 29 20 72 65 74 75  if( iDb<0 ) retu
2354c 72 6e 3b 0a 20 20 70 44 62 20 3d 20 26 64 62 2d  rn;.  pDb = &db-
2354d 3e 61 44 62 5b 69 44 62 5d 3b 0a 0a 20 20 2f 2a  >aDb[iDb];..  /*
2354e 20 49 66 20 74 68 65 20 74 65 6d 70 20 64 61 74   If the temp dat
2354f 61 62 61 73 65 20 68 61 73 20 62 65 65 6e 20 65  abase has been e
23550 78 70 6c 69 63 69 74 6c 79 20 6e 61 6d 65 64 20  xplicitly named 
23551 61 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 0a  as part of the .
23552 20 20 2a 2a 20 70 72 61 67 6d 61 2c 20 6d 61 6b    ** pragma, mak
23553 65 20 73 75 72 65 20 69 74 20 69 73 20 6f 70 65  e sure it is ope
23554 6e 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  n. .  */.  if( i
23555 44 62 3d 3d 31 20 26 26 20 73 71 6c 69 74 65 33  Db==1 && sqlite3
23556 4f 70 65 6e 54 65 6d 70 44 61 74 61 62 61 73 65  OpenTempDatabase
23557 28 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20  (pParse) ){.    
23558 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 7a  return;.  }..  z
23559 4c 65 66 74 20 3d 20 73 71 6c 69 74 65 33 4e 61  Left = sqlite3Na
2355a 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20  meFromToken(db, 
2355b 70 49 64 29 3b 0a 20 20 69 66 28 20 21 7a 4c 65  pId);.  if( !zLe
2355c 66 74 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 69  ft ) return;.  i
2355d 66 28 20 6d 69 6e 75 73 46 6c 61 67 20 29 7b 0a  f( minusFlag ){.
2355e 20 20 20 20 7a 52 69 67 68 74 20 3d 20 73 71 6c      zRight = sql
2355f 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20  ite3MPrintf(db, 
23560 22 2d 25 54 22 2c 20 70 56 61 6c 75 65 29 3b 0a  "-%T", pValue);.
23561 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 7a 52 69    }else{.    zRi
23562 67 68 74 20 3d 20 73 71 6c 69 74 65 33 4e 61 6d  ght = sqlite3Nam
23563 65 46 72 6f 6d 54 6f 6b 65 6e 28 64 62 2c 20 70  eFromToken(db, p
23564 56 61 6c 75 65 29 3b 0a 20 20 7d 0a 0a 20 20 7a  Value);.  }..  z
23565 44 62 20 3d 20 28 28 70 49 64 32 20 26 26 20 70  Db = ((pId2 && p
23566 49 64 32 2d 3e 6e 3e 30 29 3f 70 44 62 2d 3e 7a  Id2->n>0)?pDb->z
23567 4e 61 6d 65 3a 30 29 3b 0a 20 20 69 66 28 20 73  Name:0);.  if( s
23568 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
23569 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 50  pParse, SQLITE_P
2356a 52 41 47 4d 41 2c 20 7a 4c 65 66 74 2c 20 7a 52  RAGMA, zLeft, zR
2356b 69 67 68 74 2c 20 7a 44 62 29 20 29 7b 0a 20 20  ight, zDb) ){.  
2356c 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75    goto pragma_ou
2356d 74 3b 0a 20 20 7d 0a 20 0a 23 69 66 6e 64 65 66  t;.  }. .#ifndef
2356e 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
2356f 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a 0a  ER_PRAGMAS.  /*.
23570 20 20 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61    **  PRAGMA [da
23571 74 61 62 61 73 65 2e 5d 64 65 66 61 75 6c 74 5f  tabase.]default_
23572 63 61 63 68 65 5f 73 69 7a 65 0a 20 20 2a 2a 20  cache_size.  ** 
23573 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
23574 65 2e 5d 64 65 66 61 75 6c 74 5f 63 61 63 68 65  e.]default_cache
23575 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a 20 20 2a  _size=N.  **.  *
23576 2a 20 54 68 65 20 66 69 72 73 74 20 66 6f 72 6d  * The first form
23577 20 72 65 70 6f 72 74 73 20 74 68 65 20 63 75 72   reports the cur
23578 72 65 6e 74 20 70 65 72 73 69 73 74 65 6e 74 20  rent persistent 
23579 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a  setting for the.
2357a 20 20 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20    ** page cache 
2357b 73 69 7a 65 2e 20 20 54 68 65 20 76 61 6c 75 65  size.  The value
2357c 20 72 65 74 75 72 6e 65 64 20 69 73 20 74 68 65   returned is the
2357d 20 6d 61 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20   maximum number 
2357e 6f 66 0a 20 20 2a 2a 20 70 61 67 65 73 20 69 6e  of.  ** pages in
2357f 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65 2e   the page cache.
23580 20 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72    The second for
23581 6d 20 73 65 74 73 20 62 6f 74 68 20 74 68 65 20  m sets both the 
23582 63 75 72 72 65 6e 74 0a 20 20 2a 2a 20 70 61 67  current.  ** pag
23583 65 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c  e cache size val
23584 75 65 20 61 6e 64 20 74 68 65 20 70 65 72 73 69  ue and the persi
23585 73 74 65 6e 74 20 70 61 67 65 20 63 61 63 68 65  stent page cache
23586 20 73 69 7a 65 20 76 61 6c 75 65 0a 20 20 2a 2a   size value.  **
23587 20 73 74 6f 72 65 64 20 69 6e 20 74 68 65 20 64   stored in the d
23588 61 74 61 62 61 73 65 20 66 69 6c 65 2e 0a 20 20  atabase file..  
23589 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 64 65 66 61  **.  ** The defa
2358a 75 6c 74 20 63 61 63 68 65 20 73 69 7a 65 20 69  ult cache size i
2358b 73 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 74 61  s stored in meta
2358c 2d 76 61 6c 75 65 20 32 20 6f 66 20 70 61 67 65  -value 2 of page
2358d 20 31 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 64   1 of the.  ** d
2358e 61 74 61 62 61 73 65 20 66 69 6c 65 2e 20 20 54  atabase file.  T
2358f 68 65 20 63 61 63 68 65 20 73 69 7a 65 20 69 73  he cache size is
23590 20 61 63 74 75 61 6c 6c 79 20 74 68 65 20 61 62   actually the ab
23591 73 6f 6c 75 74 65 20 76 61 6c 75 65 20 6f 66 0a  solute value of.
23592 20 20 2a 2a 20 74 68 69 73 20 6d 65 6d 6f 72 79    ** this memory
23593 20 6c 6f 63 61 74 69 6f 6e 2e 20 20 54 68 65 20   location.  The 
23594 73 69 67 6e 20 6f 66 20 6d 65 74 61 2d 76 61 6c  sign of meta-val
23595 75 65 20 32 20 64 65 74 65 72 6d 69 6e 65 73 20  ue 2 determines 
23596 74 68 65 0a 20 20 2a 2a 20 73 79 6e 63 68 72 6f  the.  ** synchro
23597 6e 6f 75 73 20 73 65 74 74 69 6e 67 2e 20 20 41  nous setting.  A
23598 20 6e 65 67 61 74 69 76 65 20 76 61 6c 75 65 20   negative value 
23599 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f 6e 6f 75  means synchronou
2359a 73 20 69 73 20 6f 66 66 0a 20 20 2a 2a 20 61 6e  s is off.  ** an
2359b 64 20 61 20 70 6f 73 69 74 69 76 65 20 76 61 6c  d a positive val
2359c 75 65 20 6d 65 61 6e 73 20 73 79 6e 63 68 72 6f  ue means synchro
2359d 6e 6f 75 73 20 69 73 20 6f 6e 2e 0a 20 20 2a 2f  nous is on..  */
2359e 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
2359f 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22 64 65 66  rICmp(zLeft,"def
235a0 61 75 6c 74 5f 63 61 63 68 65 5f 73 69 7a 65 22  ault_cache_size"
235a1 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73 74 61 74  )==0 ){.    stat
235a2 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c  ic const VdbeOpL
235a3 69 73 74 20 67 65 74 43 61 63 68 65 53 69 7a 65  ist getCacheSize
235a4 5b 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f  [] = {.      { O
235a5 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20 20 30  P_ReadCookie,  0
235a6 2c 20 31 2c 20 20 20 20 20 20 20 20 32 7d 2c 20  , 1,        2}, 
235a7 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 0 */.      {
235a8 20 4f 50 5f 49 66 50 6f 73 2c 20 20 20 20 20 20   OP_IfPos,      
235a9 20 31 2c 20 36 2c 20 20 20 20 20 20 20 20 30 7d   1, 6,        0}
235aa 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74  ,.      { OP_Int
235ab 65 67 65 72 2c 20 20 20 20 20 30 2c 20 32 2c 20  eger,     0, 2, 
235ac 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 20         0},.     
235ad 20 7b 20 4f 50 5f 53 75 62 74 72 61 63 74 2c 20   { OP_Subtract, 
235ae 20 20 20 31 2c 20 32 2c 20 20 20 20 20 20 20 20     1, 2,        
235af 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 49  1},.      { OP_I
235b0 66 50 6f 73 2c 20 20 20 20 20 20 20 31 2c 20 36  fPos,       1, 6
235b1 2c 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20  ,        0},.   
235b2 20 20 20 7b 20 4f 50 5f 49 6e 74 65 67 65 72 2c     { OP_Integer,
235b3 20 20 20 20 20 30 2c 20 31 2c 20 20 20 20 20 20       0, 1,      
235b4 20 20 30 7d 2c 20 20 2f 2a 20 35 20 2a 2f 0a 20    0},  /* 5 */. 
235b5 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c 74       { OP_Result
235b6 52 6f 77 2c 20 20 20 31 2c 20 31 2c 20 20 20 20  Row,   1, 1,    
235b7 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a 20      0},.    };. 
235b8 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
235b9 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
235ba 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
235bb 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
235bc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
235bd 65 55 73 65 73 42 74 72 65 65 28 76 2c 20 69 44  eUsesBtree(v, iD
235be 62 29 3b 0a 20 20 20 20 69 66 28 20 21 7a 52 69  b);.    if( !zRi
235bf 67 68 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ght ){.      sql
235c0 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
235c1 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20 20  ls(v, 1);.      
235c2 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
235c3 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
235c4 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 61 63 68 65  AME_NAME, "cache
235c5 5f 73 69 7a 65 22 2c 20 53 51 4c 49 54 45 5f 53  _size", SQLITE_S
235c6 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 70 50  TATIC);.      pP
235c7 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 3d 20 32 3b  arse->nMem += 2;
235c8 0a 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71  .      addr = sq
235c9 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
235ca 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
235cb 67 65 74 43 61 63 68 65 53 69 7a 65 29 2c 20 67  getCacheSize), g
235cc 65 74 43 61 63 68 65 53 69 7a 65 29 3b 0a 20 20  etCacheSize);.  
235cd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
235ce 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2c  hangeP1(v, addr,
235cf 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c   iDb);.      sql
235d0 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 31  ite3VdbeChangeP1
235d1 28 76 2c 20 61 64 64 72 2b 35 2c 20 53 51 4c 49  (v, addr+5, SQLI
235d2 54 45 5f 44 45 46 41 55 4c 54 5f 43 41 43 48 45  TE_DEFAULT_CACHE
235d3 5f 53 49 5a 45 29 3b 0a 20 20 20 20 7d 65 6c 73  _SIZE);.    }els
235d4 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a  e{.      int siz
235d5 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68 74 29  e = atoi(zRight)
235d6 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69 7a 65  ;.      if( size
235d7 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73 69 7a  <0 ) size = -siz
235d8 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  e;.      sqlite3
235d9 42 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74  BeginWriteOperat
235da 69 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69  ion(pParse, 0, i
235db 44 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  Db);.      sqlit
235dc 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
235dd 4f 50 5f 49 6e 74 65 67 65 72 2c 20 73 69 7a 65  OP_Integer, size
235de 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
235df 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
235e0 20 4f 50 5f 52 65 61 64 43 6f 6f 6b 69 65 2c 20   OP_ReadCookie, 
235e1 69 44 62 2c 20 32 2c 20 32 29 3b 0a 20 20 20 20  iDb, 2, 2);.    
235e2 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
235e3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
235e4 5f 49 66 50 6f 73 2c 20 32 2c 20 30 29 3b 0a 20  _IfPos, 2, 0);. 
235e5 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
235e6 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
235e7 65 67 65 72 2c 20 2d 73 69 7a 65 2c 20 31 29 3b  eger, -size, 1);
235e8 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
235e9 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64  beJumpHere(v, ad
235ea 64 72 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  dr);.      sqlit
235eb 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
235ec 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c 20 69 44  OP_SetCookie, iD
235ed 62 2c 20 32 2c 20 31 29 3b 0a 20 20 20 20 20 20  b, 2, 1);.      
235ee 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61  pDb->pSchema->ca
235ef 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65 3b  che_size = size;
235f0 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 42 74  .      sqlite3Bt
235f1 72 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28  reeSetCacheSize(
235f2 70 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70  pDb->pBt, pDb->p
235f3 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69  Schema->cache_si
235f4 7a 65 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  ze);.    }.  }el
235f5 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50  se..  /*.  **  P
235f6 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
235f7 5d 70 61 67 65 5f 73 69 7a 65 0a 20 20 2a 2a 20  ]page_size.  ** 
235f8 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
235f9 65 2e 5d 70 61 67 65 5f 73 69 7a 65 3d 4e 0a 20  e.]page_size=N. 
235fa 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 66 69 72   **.  ** The fir
235fb 73 74 20 66 6f 72 6d 20 72 65 70 6f 72 74 73 20  st form reports 
235fc 74 68 65 20 63 75 72 72 65 6e 74 20 73 65 74 74  the current sett
235fd 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20 2a 2a  ing for the.  **
235fe 20 64 61 74 61 62 61 73 65 20 70 61 67 65 20 73   database page s
235ff 69 7a 65 20 69 6e 20 62 79 74 65 73 2e 20 20 54  ize in bytes.  T
23600 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73  he second form s
23601 65 74 73 20 74 68 65 0a 20 20 2a 2a 20 64 61 74  ets the.  ** dat
23602 61 62 61 73 65 20 70 61 67 65 20 73 69 7a 65 20  abase page size 
23603 76 61 6c 75 65 2e 20 20 54 68 65 20 76 61 6c 75  value.  The valu
23604 65 20 63 61 6e 20 6f 6e 6c 79 20 62 65 20 73 65  e can only be se
23605 74 20 69 66 0a 20 20 2a 2a 20 74 68 65 20 64 61  t if.  ** the da
23606 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74 20 79  tabase has not y
23607 65 74 20 62 65 65 6e 20 63 72 65 61 74 65 64 2e  et been created.
23608 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69  .  */.  if( sqli
23609 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
2360a 2c 22 70 61 67 65 5f 73 69 7a 65 22 29 3d 3d 30  ,"page_size")==0
2360b 20 29 7b 0a 20 20 20 20 42 74 72 65 65 20 2a 70   ){.    Btree *p
2360c 42 74 20 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20  Bt = pDb->pBt;. 
2360d 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
2360e 7b 0a 20 20 20 20 20 20 69 6e 74 20 73 69 7a 65  {.      int size
2360f 20 3d 20 70 42 74 20 3f 20 73 71 6c 69 74 65 33   = pBt ? sqlite3
23610 42 74 72 65 65 47 65 74 50 61 67 65 53 69 7a 65  BtreeGetPageSize
23611 28 70 42 74 29 20 3a 20 30 3b 0a 20 20 20 20 20  (pBt) : 0;.     
23612 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74   returnSingleInt
23613 28 70 50 61 72 73 65 2c 20 22 70 61 67 65 5f 73  (pParse, "page_s
23614 69 7a 65 22 2c 20 73 69 7a 65 29 3b 0a 20 20 20  ize", size);.   
23615 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a   }else{.      /*
23616 20 4d 61 6c 6c 6f 63 20 6d 61 79 20 66 61 69 6c   Malloc may fail
23617 20 77 68 65 6e 20 73 65 74 74 69 6e 67 20 74 68   when setting th
23618 65 20 70 61 67 65 2d 73 69 7a 65 2c 20 61 73 20  e page-size, as 
23619 74 68 65 72 65 20 69 73 20 61 6e 20 69 6e 74 65  there is an inte
2361a 72 6e 61 6c 0a 20 20 20 20 20 20 2a 2a 20 62 75  rnal.      ** bu
2361b 66 66 65 72 20 74 68 61 74 20 74 68 65 20 70 61  ffer that the pa
2361c 67 65 72 20 6d 6f 64 75 6c 65 20 72 65 73 69 7a  ger module resiz
2361d 65 73 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33  es using sqlite3
2361e 5f 72 65 61 6c 6c 6f 63 28 29 2e 0a 20 20 20 20  _realloc()..    
2361f 20 20 2a 2f 0a 20 20 20 20 20 20 64 62 2d 3e 6e    */.      db->n
23620 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 61 74  extPagesize = at
23621 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20  oi(zRight);.    
23622 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 4f 4d    if( SQLITE_NOM
23623 45 4d 3d 3d 73 71 6c 69 74 65 33 42 74 72 65 65  EM==sqlite3Btree
23624 53 65 74 50 61 67 65 53 69 7a 65 28 70 42 74 2c  SetPageSize(pBt,
23625 20 64 62 2d 3e 6e 65 78 74 50 61 67 65 73 69 7a   db->nextPagesiz
23626 65 2c 20 2d 31 29 20 29 7b 0a 20 20 20 20 20 20  e, -1) ){.      
23627 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
23628 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
23629 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
2362a 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41   /*.  **  PRAGMA
2362b 20 5b 64 61 74 61 62 61 73 65 2e 5d 6d 61 78 5f   [database.]max_
2362c 70 61 67 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 20  page_count.  ** 
2362d 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
2362e 65 2e 5d 6d 61 78 5f 70 61 67 65 5f 63 6f 75 6e  e.]max_page_coun
2362f 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  t=N.  **.  ** Th
23630 65 20 66 69 72 73 74 20 66 6f 72 6d 20 72 65 70  e first form rep
23631 6f 72 74 73 20 74 68 65 20 63 75 72 72 65 6e 74  orts the current
23632 20 73 65 74 74 69 6e 67 20 66 6f 72 20 74 68 65   setting for the
23633 0a 20 20 2a 2a 20 6d 61 78 69 6d 75 6d 20 6e 75  .  ** maximum nu
23634 6d 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e  mber of pages in
23635 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66 69   the database fi
23636 6c 65 2e 20 20 54 68 65 20 0a 20 20 2a 2a 20 73  le.  The .  ** s
23637 65 63 6f 6e 64 20 66 6f 72 6d 20 61 74 74 65 6d  econd form attem
23638 70 74 73 20 74 6f 20 63 68 61 6e 67 65 20 74 68  pts to change th
23639 69 73 20 73 65 74 74 69 6e 67 2e 20 20 42 6f 74  is setting.  Bot
2363a 68 0a 20 20 2a 2a 20 66 6f 72 6d 73 20 72 65 74  h.  ** forms ret
2363b 75 72 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20  urn the current 
2363c 73 65 74 74 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  setting..  */.  
2363d 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
2363e 6d 70 28 7a 4c 65 66 74 2c 22 6d 61 78 5f 70 61  mp(zLeft,"max_pa
2363f 67 65 5f 63 6f 75 6e 74 22 29 3d 3d 30 20 29 7b  ge_count")==0 ){
23640 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 20  .    Btree *pBt 
23641 3d 20 70 44 62 2d 3e 70 42 74 3b 0a 20 20 20 20  = pDb->pBt;.    
23642 69 6e 74 20 6e 65 77 4d 61 78 20 3d 20 30 3b 0a  int newMax = 0;.
23643 20 20 20 20 69 66 28 20 7a 52 69 67 68 74 20 29      if( zRight )
23644 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78 20 3d  {.      newMax =
23645 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
23646 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70 42 74     }.    if( pBt
23647 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 4d 61 78   ){.      newMax
23648 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 4d   = sqlite3BtreeM
23649 61 78 50 61 67 65 43 6f 75 6e 74 28 70 42 74 2c  axPageCount(pBt,
2364a 20 6e 65 77 4d 61 78 29 3b 0a 20 20 20 20 7d 0a   newMax);.    }.
2364b 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65      returnSingle
2364c 49 6e 74 28 70 50 61 72 73 65 2c 20 22 6d 61 78  Int(pParse, "max
2364d 5f 70 61 67 65 5f 63 6f 75 6e 74 22 2c 20 6e 65  _page_count", ne
2364e 77 4d 61 78 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  wMax);.  }else..
2364f 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
23650 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 70 61 67  A [database.]pag
23651 65 5f 63 6f 75 6e 74 0a 20 20 2a 2a 0a 20 20 2a  e_count.  **.  *
23652 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d  * Return the num
23653 62 65 72 20 6f 66 20 70 61 67 65 73 20 69 6e 20  ber of pages in 
23654 74 68 65 20 73 70 65 63 69 66 69 65 64 20 64 61  the specified da
23655 74 61 62 61 73 65 2e 0a 20 20 2a 2f 0a 20 20 69  tabase..  */.  i
23656 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
23657 70 28 7a 4c 65 66 74 2c 22 70 61 67 65 5f 63 6f  p(zLeft,"page_co
23658 75 6e 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  unt")==0 ){.    
23659 56 64 62 65 20 2a 76 3b 0a 20 20 20 20 69 6e 74  Vdbe *v;.    int
2365a 20 69 52 65 67 3b 0a 20 20 20 20 76 20 3d 20 73   iReg;.    v = s
2365b 71 6c 69 74 65 33 47 65 74 56 64 62 65 28 70 50  qlite3GetVdbe(pP
2365c 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28 20 21  arse);.    if( !
2365d 76 20 7c 7c 20 73 71 6c 69 74 65 33 52 65 61 64  v || sqlite3Read
2365e 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
2365f 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
23660 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 6f 64  ;.    sqlite3Cod
23661 65 56 65 72 69 66 79 53 63 68 65 6d 61 28 70 50  eVerifySchema(pP
23662 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20 20  arse, iDb);.    
23663 69 52 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iReg = ++pParse-
23664 3e 6e 4d 65 6d 3b 0a 20 20 20 20 73 71 6c 69 74  >nMem;.    sqlit
23665 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23666 4f 50 5f 50 61 67 65 63 6f 75 6e 74 2c 20 69 44  OP_Pagecount, iD
23667 62 2c 20 69 52 65 67 29 3b 0a 20 20 20 20 73 71  b, iReg);.    sq
23668 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23669 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
2366a 20 69 52 65 67 2c 20 31 29 3b 0a 20 20 20 20 73   iReg, 1);.    s
2366b 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
2366c 43 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20  Cols(v, 1);.    
2366d 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
2366e 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e  lName(v, 0, COLN
2366f 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 61 67 65 5f  AME_NAME, "page_
23670 63 6f 75 6e 74 22 2c 20 53 51 4c 49 54 45 5f 53  count", SQLITE_S
23671 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c 73 65 0a  TATIC);.  }else.
23672 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47  .  /*.  **  PRAG
23673 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6c 6f  MA [database.]lo
23674 63 6b 69 6e 67 5f 6d 6f 64 65 0a 20 20 2a 2a 20  cking_mode.  ** 
23675 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
23676 65 2e 5d 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 20  e.]locking_mode 
23677 3d 20 28 6e 6f 72 6d 61 6c 7c 65 78 63 6c 75 73  = (normal|exclus
23678 69 76 65 29 0a 20 20 2a 2f 0a 20 20 69 66 28 20  ive).  */.  if( 
23679 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2367a 4c 65 66 74 2c 22 6c 6f 63 6b 69 6e 67 5f 6d 6f  Left,"locking_mo
2367b 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 63  de")==0 ){.    c
2367c 6f 6e 73 74 20 63 68 61 72 20 2a 7a 52 65 74 20  onst char *zRet 
2367d 3d 20 22 6e 6f 72 6d 61 6c 22 3b 0a 20 20 20 20  = "normal";.    
2367e 69 6e 74 20 65 4d 6f 64 65 20 3d 20 67 65 74 4c  int eMode = getL
2367f 6f 63 6b 69 6e 67 4d 6f 64 65 28 7a 52 69 67 68  ockingMode(zRigh
23680 74 29 3b 0a 0a 20 20 20 20 69 66 28 20 70 49 64  t);..    if( pId
23681 32 2d 3e 6e 3d 3d 30 20 26 26 20 65 4d 6f 64 65  2->n==0 && eMode
23682 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47 4d  ==PAGER_LOCKINGM
23683 4f 44 45 5f 51 55 45 52 59 20 29 7b 0a 20 20 20  ODE_QUERY ){.   
23684 20 20 20 2f 2a 20 53 69 6d 70 6c 65 20 22 50 52     /* Simple "PR
23685 41 47 4d 41 20 6c 6f 63 6b 69 6e 67 5f 6d 6f 64  AGMA locking_mod
23686 65 3b 22 20 73 74 61 74 65 6d 65 6e 74 2e 20 54  e;" statement. T
23687 68 69 73 20 69 73 20 61 20 71 75 65 72 79 20 66  his is a query f
23688 6f 72 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  or.      ** the 
23689 63 75 72 72 65 6e 74 20 64 65 66 61 75 6c 74 20  current default 
2368a 6c 6f 63 6b 69 6e 67 20 6d 6f 64 65 20 28 77 68  locking mode (wh
2368b 69 63 68 20 6d 61 79 20 62 65 20 64 69 66 66 65  ich may be diffe
2368c 72 65 6e 74 20 74 6f 0a 20 20 20 20 20 20 2a 2a  rent to.      **
2368d 20 74 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64   the locking-mod
2368e 65 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64 61  e of the main da
2368f 74 61 62 61 73 65 29 2e 0a 20 20 20 20 20 20 2a  tabase)..      *
23690 2f 0a 20 20 20 20 20 20 65 4d 6f 64 65 20 3d 20  /.      eMode = 
23691 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
23692 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
23693 20 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72     Pager *pPager
23694 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 64 32  ;.      if( pId2
23695 2d 3e 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  ->n==0 ){.      
23696 20 20 2f 2a 20 54 68 69 73 20 69 6e 64 69 63 61    /* This indica
23697 74 65 73 20 74 68 61 74 20 6e 6f 20 64 61 74 61  tes that no data
23698 62 61 73 65 20 6e 61 6d 65 20 77 61 73 20 73 70  base name was sp
23699 65 63 69 66 69 65 64 20 61 73 20 70 61 72 74 0a  ecified as part.
2369a 20 20 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68          ** of th
2369b 65 20 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64  e PRAGMA command
2369c 2e 20 49 6e 20 74 68 69 73 20 63 61 73 65 20 74  . In this case t
2369d 68 65 20 6c 6f 63 6b 69 6e 67 2d 6d 6f 64 65 20  he locking-mode 
2369e 6d 75 73 74 20 62 65 0a 20 20 20 20 20 20 20 20  must be.        
2369f 2a 2a 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74  ** set on all at
236a0 74 61 63 68 65 64 20 64 61 74 61 62 61 73 65 73  tached databases
236a1 2c 20 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65  , as well as the
236a2 20 6d 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20   main db file.. 
236a3 20 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20         **.      
236a4 20 20 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73    ** Also, the s
236a5 71 6c 69 74 65 33 2e 64 66 6c 74 4c 6f 63 6b 4d  qlite3.dfltLockM
236a6 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73 20  ode variable is 
236a7 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20 20  set so that.    
236a8 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73 65      ** any subse
236a9 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65 64  quently attached
236aa 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f 20   databases also 
236ab 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69 65  use the specifie
236ac 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6c 6f 63  d.        ** loc
236ad 6b 69 6e 67 20 6d 6f 64 65 2e 0a 20 20 20 20 20  king mode..     
236ae 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e     */.        in
236af 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61 73  t ii;.        as
236b0 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e 61  sert(pDb==&db->a
236b1 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20 20  Db[0]);.        
236b2 66 6f 72 28 69 69 3d 32 3b 20 69 69 3c 64 62 2d  for(ii=2; ii<db-
236b3 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  >nDb; ii++){.   
236b4 20 20 20 20 20 20 20 70 50 61 67 65 72 20 3d 20         pPager = 
236b5 73 71 6c 69 74 65 33 42 74 72 65 65 50 61 67 65  sqlite3BtreePage
236b6 72 28 64 62 2d 3e 61 44 62 5b 69 69 5d 2e 70 42  r(db->aDb[ii].pB
236b7 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  t);.          sq
236b8 6c 69 74 65 33 50 61 67 65 72 4c 6f 63 6b 69 6e  lite3PagerLockin
236b9 67 4d 6f 64 65 28 70 50 61 67 65 72 2c 20 65 4d  gMode(pPager, eM
236ba 6f 64 65 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ode);.        }.
236bb 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66 6c 74          db->dflt
236bc 4c 6f 63 6b 4d 6f 64 65 20 3d 20 65 4d 6f 64 65  LockMode = eMode
236bd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
236be 70 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33  pPager = sqlite3
236bf 42 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e  BtreePager(pDb->
236c0 70 42 74 29 3b 0a 20 20 20 20 20 20 65 4d 6f 64  pBt);.      eMod
236c1 65 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72  e = sqlite3Pager
236c2 4c 6f 63 6b 69 6e 67 4d 6f 64 65 28 70 50 61 67  LockingMode(pPag
236c3 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20  er, eMode);.    
236c4 7d 0a 0a 20 20 20 20 61 73 73 65 72 74 28 65 4d  }..    assert(eM
236c5 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49  ode==PAGER_LOCKI
236c6 4e 47 4d 4f 44 45 5f 4e 4f 52 4d 41 4c 7c 7c 65  NGMODE_NORMAL||e
236c7 4d 6f 64 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b  Mode==PAGER_LOCK
236c8 49 4e 47 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56  INGMODE_EXCLUSIV
236c9 45 29 3b 0a 20 20 20 20 69 66 28 20 65 4d 6f 64  E);.    if( eMod
236ca 65 3d 3d 50 41 47 45 52 5f 4c 4f 43 4b 49 4e 47  e==PAGER_LOCKING
236cb 4d 4f 44 45 5f 45 58 43 4c 55 53 49 56 45 20 29  MODE_EXCLUSIVE )
236cc 7b 0a 20 20 20 20 20 20 7a 52 65 74 20 3d 20 22  {.      zRet = "
236cd 65 78 63 6c 75 73 69 76 65 22 3b 0a 20 20 20 20  exclusive";.    
236ce 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
236cf 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 31  eSetNumCols(v, 1
236d0 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
236d1 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20  beSetColName(v, 
236d2 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c  0, COLNAME_NAME,
236d3 20 22 6c 6f 63 6b 69 6e 67 5f 6d 6f 64 65 22 2c   "locking_mode",
236d4 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
236d5 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
236d6 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
236d7 69 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 7a  ing8, 0, 1, 0, z
236d8 52 65 74 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c  Ret, 0);.    sql
236d9 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
236da 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
236db 31 2c 20 31 29 3b 0a 20 20 7d 65 6c 73 65 0a 0a  1, 1);.  }else..
236dc 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d    /*.  **  PRAGM
236dd 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 6a 6f 75  A [database.]jou
236de 72 6e 61 6c 5f 6d 6f 64 65 0a 20 20 2a 2a 20 20  rnal_mode.  **  
236df 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
236e0 2e 5d 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 20 3d  .]journal_mode =
236e1 20 28 64 65 6c 65 74 65 7c 70 65 72 73 69 73 74   (delete|persist
236e2 7c 6f 66 66 7c 74 72 75 6e 63 61 74 65 7c 6d 65  |off|truncate|me
236e3 6d 6f 72 79 29 0a 20 20 2a 2f 0a 20 20 69 66 28  mory).  */.  if(
236e4 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
236e5 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c 5f 6d  zLeft,"journal_m
236e6 6f 64 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ode")==0 ){.    
236e7 69 6e 74 20 65 4d 6f 64 65 3b 0a 20 20 20 20 73  int eMode;.    s
236e8 74 61 74 69 63 20 63 68 61 72 20 2a 20 63 6f 6e  tatic char * con
236e9 73 74 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 5d 20  st azModeName[] 
236ea 3d 20 7b 0a 20 20 20 20 20 20 22 64 65 6c 65 74  = {.      "delet
236eb 65 22 2c 20 22 70 65 72 73 69 73 74 22 2c 20 22  e", "persist", "
236ec 6f 66 66 22 2c 20 22 74 72 75 6e 63 61 74 65 22  off", "truncate"
236ed 2c 20 22 6d 65 6d 6f 72 79 22 0a 20 20 20 20 7d  , "memory".    }
236ee 3b 0a 0a 20 20 20 20 69 66 28 20 7a 52 69 67 68  ;..    if( zRigh
236ef 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 65 4d  t==0 ){.      eM
236f0 6f 64 65 20 3d 20 50 41 47 45 52 5f 4a 4f 55 52  ode = PAGER_JOUR
236f1 4e 41 4c 4d 4f 44 45 5f 51 55 45 52 59 3b 0a 20  NALMODE_QUERY;. 
236f2 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
236f3 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28 7a  int n = strlen(z
236f4 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 65 4d  Right);.      eM
236f5 6f 64 65 20 3d 20 73 69 7a 65 6f 66 28 61 7a 4d  ode = sizeof(azM
236f6 6f 64 65 4e 61 6d 65 29 2f 73 69 7a 65 6f 66 28  odeName)/sizeof(
236f7 61 7a 4d 6f 64 65 4e 61 6d 65 5b 30 5d 29 20 2d  azModeName[0]) -
236f8 20 31 3b 0a 20 20 20 20 20 20 77 68 69 6c 65 28   1;.      while(
236f9 20 65 4d 6f 64 65 3e 3d 30 20 26 26 20 73 71 6c   eMode>=0 && sql
236fa 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 52 69  ite3StrNICmp(zRi
236fb 67 68 74 2c 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b  ght, azModeName[
236fc 65 4d 6f 64 65 5d 2c 20 6e 29 21 3d 30 20 29 7b  eMode], n)!=0 ){
236fd 0a 20 20 20 20 20 20 20 20 65 4d 6f 64 65 2d 2d  .        eMode--
236fe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
236ff 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e 6e 3d      if( pId2->n=
23700 3d 30 20 26 26 20 65 4d 6f 64 65 3d 3d 50 41 47  =0 && eMode==PAG
23701 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 51  ER_JOURNALMODE_Q
23702 55 45 52 59 20 29 7b 0a 20 20 20 20 20 20 2f 2a  UERY ){.      /*
23703 20 53 69 6d 70 6c 65 20 22 50 52 41 47 4d 41 20   Simple "PRAGMA 
23704 6a 6f 75 72 6e 61 6c 5f 6d 6f 64 65 3b 22 20 73  journal_mode;" s
23705 74 61 74 65 6d 65 6e 74 2e 20 54 68 69 73 20 69  tatement. This i
23706 73 20 61 20 71 75 65 72 79 20 66 6f 72 0a 20 20  s a query for.  
23707 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65      ** the curre
23708 6e 74 20 64 65 66 61 75 6c 74 20 6a 6f 75 72 6e  nt default journ
23709 61 6c 20 6d 6f 64 65 20 28 77 68 69 63 68 20 6d  al mode (which m
2370a 61 79 20 62 65 20 64 69 66 66 65 72 65 6e 74 20  ay be different 
2370b 74 6f 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20  to.      ** the 
2370c 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6f 66 20  journal-mode of 
2370d 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73  the main databas
2370e 65 29 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  e)..      */.   
2370f 20 20 20 65 4d 6f 64 65 20 3d 20 64 62 2d 3e 64     eMode = db->d
23710 66 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 3b 0a  fltJournalMode;.
23711 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
23712 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a   Pager *pPager;.
23713 20 20 20 20 20 20 69 66 28 20 70 49 64 32 2d 3e        if( pId2->
23714 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  n==0 ){.        
23715 2f 2a 20 54 68 69 73 20 69 6e 64 69 63 61 74 65  /* This indicate
23716 73 20 74 68 61 74 20 6e 6f 20 64 61 74 61 62 61  s that no databa
23717 73 65 20 6e 61 6d 65 20 77 61 73 20 73 70 65 63  se name was spec
23718 69 66 69 65 64 20 61 73 20 70 61 72 74 0a 20 20  ified as part.  
23719 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20        ** of the 
2371a 50 52 41 47 4d 41 20 63 6f 6d 6d 61 6e 64 2e 20  PRAGMA command. 
2371b 49 6e 20 74 68 69 73 20 63 61 73 65 20 74 68 65  In this case the
2371c 20 6a 6f 75 72 6e 61 6c 2d 6d 6f 64 65 20 6d 75   journal-mode mu
2371d 73 74 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a  st be.        **
2371e 20 73 65 74 20 6f 6e 20 61 6c 6c 20 61 74 74 61   set on all atta
2371f 63 68 65 64 20 64 61 74 61 62 61 73 65 73 2c 20  ched databases, 
23720 61 73 20 77 65 6c 6c 20 61 73 20 74 68 65 20 6d  as well as the m
23721 61 69 6e 20 64 62 20 66 69 6c 65 2e 0a 20 20 20  ain db file..   
23722 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 20 20       **.        
23723 2a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 73 71 6c  ** Also, the sql
23724 69 74 65 33 2e 64 66 6c 74 4a 6f 75 72 6e 61 6c  ite3.dfltJournal
23725 4d 6f 64 65 20 76 61 72 69 61 62 6c 65 20 69 73  Mode variable is
23726 20 73 65 74 20 73 6f 20 74 68 61 74 0a 20 20 20   set so that.   
23727 20 20 20 20 20 2a 2a 20 61 6e 79 20 73 75 62 73       ** any subs
23728 65 71 75 65 6e 74 6c 79 20 61 74 74 61 63 68 65  equently attache
23729 64 20 64 61 74 61 62 61 73 65 73 20 61 6c 73 6f  d databases also
2372a 20 75 73 65 20 74 68 65 20 73 70 65 63 69 66 69   use the specifi
2372b 65 64 0a 20 20 20 20 20 20 20 20 2a 2a 20 6a 6f  ed.        ** jo
2372c 75 72 6e 61 6c 20 6d 6f 64 65 2e 0a 20 20 20 20  urnal mode..    
2372d 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69      */.        i
2372e 6e 74 20 69 69 3b 0a 20 20 20 20 20 20 20 20 61  nt ii;.        a
2372f 73 73 65 72 74 28 70 44 62 3d 3d 26 64 62 2d 3e  ssert(pDb==&db->
23730 61 44 62 5b 30 5d 29 3b 0a 20 20 20 20 20 20 20  aDb[0]);.       
23731 20 66 6f 72 28 69 69 3d 31 3b 20 69 69 3c 64 62   for(ii=1; ii<db
23732 2d 3e 6e 44 62 3b 20 69 69 2b 2b 29 7b 0a 20 20  ->nDb; ii++){.  
23733 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
23734 61 44 62 5b 69 69 5d 2e 70 42 74 20 29 7b 0a 20  aDb[ii].pBt ){. 
23735 20 20 20 20 20 20 20 20 20 20 20 70 50 61 67 65             pPage
23736 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65  r = sqlite3Btree
23737 50 61 67 65 72 28 64 62 2d 3e 61 44 62 5b 69 69  Pager(db->aDb[ii
23738 5d 2e 70 42 74 29 3b 0a 20 20 20 20 20 20 20 20  ].pBt);.        
23739 20 20 20 20 73 71 6c 69 74 65 33 50 61 67 65 72      sqlite3Pager
2373a 4a 6f 75 72 6e 61 6c 4d 6f 64 65 28 70 50 61 67  JournalMode(pPag
2373b 65 72 2c 20 65 4d 6f 64 65 29 3b 0a 20 20 20 20  er, eMode);.    
2373c 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2373d 7d 0a 20 20 20 20 20 20 20 20 64 62 2d 3e 64 66  }.        db->df
2373e 6c 74 4a 6f 75 72 6e 61 6c 4d 6f 64 65 20 3d 20  ltJournalMode = 
2373f 65 4d 6f 64 65 3b 0a 20 20 20 20 20 20 7d 0a 20  eMode;.      }. 
23740 20 20 20 20 20 70 50 61 67 65 72 20 3d 20 73 71       pPager = sq
23741 6c 69 74 65 33 42 74 72 65 65 50 61 67 65 72 28  lite3BtreePager(
23742 70 44 62 2d 3e 70 42 74 29 3b 0a 20 20 20 20 20  pDb->pBt);.     
23743 20 65 4d 6f 64 65 20 3d 20 73 71 6c 69 74 65 33   eMode = sqlite3
23744 50 61 67 65 72 4a 6f 75 72 6e 61 6c 4d 6f 64 65  PagerJournalMode
23745 28 70 50 61 67 65 72 2c 20 65 4d 6f 64 65 29 3b  (pPager, eMode);
23746 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
23747 74 28 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52 5f  t( eMode==PAGER_
23748 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 44 45 4c 45  JOURNALMODE_DELE
23749 54 45 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  TE.             
2374a 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41 47 45 52   || eMode==PAGER
2374b 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f 54 52 55  _JOURNALMODE_TRU
2374c 4e 43 41 54 45 0a 20 20 20 20 20 20 20 20 20 20  NCATE.          
2374d 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
2374e 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
2374f 50 45 52 53 49 53 54 0a 20 20 20 20 20 20 20 20  PERSIST.        
23750 20 20 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d        || eMode==
23751 50 41 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44  PAGER_JOURNALMOD
23752 45 5f 4f 46 46 0a 20 20 20 20 20 20 20 20 20 20  E_OFF.          
23753 20 20 20 20 7c 7c 20 65 4d 6f 64 65 3d 3d 50 41      || eMode==PA
23754 47 45 52 5f 4a 4f 55 52 4e 41 4c 4d 4f 44 45 5f  GER_JOURNALMODE_
23755 4d 45 4d 4f 52 59 20 29 3b 0a 20 20 20 20 73 71  MEMORY );.    sq
23756 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
23757 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73  ols(v, 1);.    s
23758 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
23759 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41  Name(v, 0, COLNA
2375a 4d 45 5f 4e 41 4d 45 2c 20 22 6a 6f 75 72 6e 61  ME_NAME, "journa
2375b 6c 5f 6d 6f 64 65 22 2c 20 53 51 4c 49 54 45 5f  l_mode", SQLITE_
2375c 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c  STATIC);.    sql
2375d 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76  ite3VdbeAddOp4(v
2375e 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c  , OP_String8, 0,
2375f 20 31 2c 20 30 2c 20 0a 20 20 20 20 20 20 20 20   1, 0, .        
23760 20 20 20 61 7a 4d 6f 64 65 4e 61 6d 65 5b 65 4d     azModeName[eM
23761 6f 64 65 5d 2c 20 50 34 5f 53 54 41 54 49 43 29  ode], P4_STATIC)
23762 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23763 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65  eAddOp2(v, OP_Re
23764 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a  sultRow, 1, 1);.
23765 20 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20    }else..  /*.  
23766 2a 2a 20 20 50 52 41 47 4d 41 20 5b 64 61 74 61  **  PRAGMA [data
23767 62 61 73 65 2e 5d 6a 6f 75 72 6e 61 6c 5f 73 69  base.]journal_si
23768 7a 65 5f 6c 69 6d 69 74 0a 20 20 2a 2a 20 20 50  ze_limit.  **  P
23769 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e  RAGMA [database.
2376a 5d 6a 6f 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69  ]journal_size_li
2376b 6d 69 74 3d 4e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  mit=N.  **.  ** 
2376c 47 65 74 20 6f 72 20 73 65 74 20 74 68 65 20 28  Get or set the (
2376d 62 6f 6f 6c 65 61 6e 29 20 76 61 6c 75 65 20 6f  boolean) value o
2376e 66 20 74 68 65 20 64 61 74 61 62 61 73 65 20 27  f the database '
2376f 61 75 74 6f 2d 76 61 63 75 75 6d 27 20 70 61 72  auto-vacuum' par
23770 61 6d 65 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 69  ameter..  */.  i
23771 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
23772 70 28 7a 4c 65 66 74 2c 22 6a 6f 75 72 6e 61 6c  p(zLeft,"journal
23773 5f 73 69 7a 65 5f 6c 69 6d 69 74 22 29 3d 3d 30  _size_limit")==0
23774 20 29 7b 0a 20 20 20 20 50 61 67 65 72 20 2a 70   ){.    Pager *p
23775 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42  Pager = sqlite3B
23776 74 72 65 65 50 61 67 65 72 28 70 44 62 2d 3e 70  treePager(pDb->p
23777 42 74 29 3b 0a 20 20 20 20 69 36 34 20 69 4c 69  Bt);.    i64 iLi
23778 6d 69 74 20 3d 20 2d 32 3b 0a 20 20 20 20 69 66  mit = -2;.    if
23779 28 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20  ( zRight ){.    
2377a 20 20 69 6e 74 20 69 4c 69 6d 69 74 33 32 20 3d    int iLimit32 =
2377b 20 61 74 6f 69 28 7a 52 69 67 68 74 29 3b 0a 20   atoi(zRight);. 
2377c 20 20 20 20 20 69 66 28 20 69 4c 69 6d 69 74 33       if( iLimit3
2377d 32 3c 2d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  2<-1 ){.        
2377e 69 4c 69 6d 69 74 33 32 20 3d 20 2d 31 3b 0a 20  iLimit32 = -1;. 
2377f 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 4c 69       }.      iLi
23780 6d 69 74 20 3d 20 69 4c 69 6d 69 74 33 32 3b 0a  mit = iLimit32;.
23781 20 20 20 20 7d 0a 20 20 20 20 69 4c 69 6d 69 74      }.    iLimit
23782 20 3d 20 73 71 6c 69 74 65 33 50 61 67 65 72 4a   = sqlite3PagerJ
23783 6f 75 72 6e 61 6c 53 69 7a 65 4c 69 6d 69 74 28  ournalSizeLimit(
23784 70 50 61 67 65 72 2c 20 69 4c 69 6d 69 74 29 3b  pPager, iLimit);
23785 0a 20 20 20 20 72 65 74 75 72 6e 53 69 6e 67 6c  .    returnSingl
23786 65 49 6e 74 28 70 50 61 72 73 65 2c 20 22 6a 6f  eInt(pParse, "jo
23787 75 72 6e 61 6c 5f 73 69 7a 65 5f 6c 69 6d 69 74  urnal_size_limit
23788 22 2c 20 28 69 6e 74 29 69 4c 69 6d 69 74 29 3b  ", (int)iLimit);
23789 0a 20 20 7d 65 6c 73 65 0a 0a 23 65 6e 64 69 66  .  }else..#endif
2378a 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2378b 50 41 47 45 52 5f 50 52 41 47 4d 41 53 20 2a 2f  PAGER_PRAGMAS */
2378c 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 50 52 41  ..  /*.  **  PRA
2378d 47 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 61  GMA [database.]a
2378e 75 74 6f 5f 76 61 63 75 75 6d 0a 20 20 2a 2a 20  uto_vacuum.  ** 
2378f 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
23790 65 2e 5d 61 75 74 6f 5f 76 61 63 75 75 6d 3d 4e  e.]auto_vacuum=N
23791 0a 20 20 2a 2a 0a 20 20 2a 2a 20 47 65 74 20 6f  .  **.  ** Get o
23792 72 20 73 65 74 20 74 68 65 20 28 62 6f 6f 6c 65  r set the (boole
23793 61 6e 29 20 76 61 6c 75 65 20 6f 66 20 74 68 65  an) value of the
23794 20 64 61 74 61 62 61 73 65 20 27 61 75 74 6f 2d   database 'auto-
23795 76 61 63 75 75 6d 27 20 70 61 72 61 6d 65 74 65  vacuum' paramete
23796 72 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  r..  */.#ifndef 
23797 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 4f  SQLITE_OMIT_AUTO
23798 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73 71 6c  VACUUM.  if( sql
23799 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
2379a 74 2c 22 61 75 74 6f 5f 76 61 63 75 75 6d 22 29  t,"auto_vacuum")
2379b 3d 3d 30 20 29 7b 0a 20 20 20 20 42 74 72 65 65  ==0 ){.    Btree
2379c 20 2a 70 42 74 20 3d 20 70 44 62 2d 3e 70 42 74   *pBt = pDb->pBt
2379d 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  ;.    if( sqlite
2379e 33 52 65 61 64 53 63 68 65 6d 61 28 70 50 61 72  3ReadSchema(pPar
2379f 73 65 29 20 29 7b 0a 20 20 20 20 20 20 67 6f 74  se) ){.      got
237a0 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20 20  o pragma_out;.  
237a1 20 20 7d 0a 20 20 20 20 69 66 28 20 21 7a 52 69    }.    if( !zRi
237a2 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ght ){.      int
237a3 20 61 75 74 6f 5f 76 61 63 75 75 6d 20 3d 20 0a   auto_vacuum = .
237a4 20 20 20 20 20 20 20 20 20 20 70 42 74 20 3f 20            pBt ? 
237a5 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 41  sqlite3BtreeGetA
237a6 75 74 6f 56 61 63 75 75 6d 28 70 42 74 29 20 3a  utoVacuum(pBt) :
237a7 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
237a8 41 55 54 4f 56 41 43 55 55 4d 3b 0a 20 20 20 20  AUTOVACUUM;.    
237a9 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
237aa 74 28 70 50 61 72 73 65 2c 20 22 61 75 74 6f 5f  t(pParse, "auto_
237ab 76 61 63 75 75 6d 22 2c 20 61 75 74 6f 5f 76 61  vacuum", auto_va
237ac 63 75 75 6d 29 3b 0a 20 20 20 20 7d 65 6c 73 65  cuum);.    }else
237ad 7b 0a 20 20 20 20 20 20 69 6e 74 20 65 41 75 74  {.      int eAut
237ae 6f 20 3d 20 67 65 74 41 75 74 6f 56 61 63 75 75  o = getAutoVacuu
237af 6d 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  m(zRight);.     
237b0 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63   db->nextAutovac
237b1 20 3d 20 65 41 75 74 6f 3b 0a 20 20 20 20 20 20   = eAuto;.      
237b2 69 66 28 20 65 41 75 74 6f 3e 3d 30 20 29 7b 0a  if( eAuto>=0 ){.
237b3 20 20 20 20 20 20 20 20 2f 2a 20 43 61 6c 6c 20          /* Call 
237b4 53 65 74 41 75 74 6f 56 61 63 75 75 6d 28 29 20  SetAutoVacuum() 
237b5 74 6f 20 73 65 74 20 69 6e 69 74 69 61 6c 69 7a  to set initializ
237b6 65 20 74 68 65 20 69 6e 74 65 72 6e 61 6c 20 61  e the internal a
237b7 75 74 6f 20 61 6e 64 0a 20 20 20 20 20 20 20 20  uto and.        
237b8 2a 2a 20 69 6e 63 72 2d 76 61 63 75 75 6d 20 66  ** incr-vacuum f
237b9 6c 61 67 73 2e 20 54 68 69 73 20 69 73 20 72 65  lags. This is re
237ba 71 75 69 72 65 64 20 69 6e 20 63 61 73 65 20 74  quired in case t
237bb 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a 20  his connection. 
237bc 20 20 20 20 20 20 20 2a 2a 20 63 72 65 61 74 65         ** create
237bd 73 20 74 68 65 20 64 61 74 61 62 61 73 65 20 66  s the database f
237be 69 6c 65 2e 20 49 74 20 69 73 20 69 6d 70 6f 72  ile. It is impor
237bf 74 61 6e 74 20 74 68 61 74 20 69 74 20 69 73 20  tant that it is 
237c0 63 72 65 61 74 65 64 0a 20 20 20 20 20 20 20 20  created.        
237c1 2a 2a 20 61 73 20 61 6e 20 61 75 74 6f 2d 76 61  ** as an auto-va
237c2 63 75 75 6d 20 63 61 70 61 62 6c 65 20 64 62 2e  cuum capable db.
237c3 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  .        */.    
237c4 20 20 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c      int rc = sql
237c5 69 74 65 33 42 74 72 65 65 53 65 74 41 75 74 6f  ite3BtreeSetAuto
237c6 56 61 63 75 75 6d 28 70 42 74 2c 20 65 41 75 74  Vacuum(pBt, eAut
237c7 6f 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  o);.        if( 
237c8 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26  rc==SQLITE_OK &&
237c9 20 28 65 41 75 74 6f 3d 3d 31 20 7c 7c 20 65 41   (eAuto==1 || eA
237ca 75 74 6f 3d 3d 32 29 20 29 7b 0a 20 20 20 20 20  uto==2) ){.     
237cb 20 20 20 20 20 2f 2a 20 57 68 65 6e 20 73 65 74       /* When set
237cc 74 69 6e 67 20 74 68 65 20 61 75 74 6f 5f 76 61  ting the auto_va
237cd 63 75 75 6d 20 6d 6f 64 65 20 74 6f 20 65 69 74  cuum mode to eit
237ce 68 65 72 20 22 66 75 6c 6c 22 20 6f 72 20 0a 20  her "full" or . 
237cf 20 20 20 20 20 20 20 20 20 2a 2a 20 22 69 6e 63           ** "inc
237d0 72 65 6d 65 6e 74 61 6c 22 2c 20 77 72 69 74 65  remental", write
237d1 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 6d 65   the value of me
237d2 74 61 5b 36 5d 20 69 6e 20 74 68 65 20 64 61 74  ta[6] in the dat
237d3 61 62 61 73 65 0a 20 20 20 20 20 20 20 20 20 20  abase.          
237d4 2a 2a 20 66 69 6c 65 2e 20 42 65 66 6f 72 65 20  ** file. Before 
237d5 77 72 69 74 69 6e 67 20 74 6f 20 6d 65 74 61 5b  writing to meta[
237d6 36 5d 2c 20 63 68 65 63 6b 20 74 68 61 74 20 6d  6], check that m
237d7 65 74 61 5b 33 5d 20 69 6e 64 69 63 61 74 65 73  eta[3] indicates
237d8 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 68  .          ** th
237d9 61 74 20 74 68 69 73 20 72 65 61 6c 6c 79 20 69  at this really i
237da 73 20 61 6e 20 61 75 74 6f 2d 76 61 63 75 75 6d  s an auto-vacuum
237db 20 63 61 70 61 62 6c 65 20 64 61 74 61 62 61 73   capable databas
237dc 65 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a  e..          */.
237dd 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69 63            static
237de 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73   const VdbeOpLis
237df 74 20 73 65 74 4d 65 74 61 36 5b 5d 20 3d 20 7b  t setMeta6[] = {
237e0 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
237e1 50 5f 54 72 61 6e 73 61 63 74 69 6f 6e 2c 20 20  P_Transaction,  
237e2 20 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20    0,            
237e3 20 20 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c     1,        0},
237e4 20 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20      /* 0 */.    
237e5 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61          { OP_Rea
237e6 64 43 6f 6f 6b 69 65 2c 20 20 20 20 20 30 2c 20  dCookie,     0, 
237e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 2c                1,
237e8 20 20 20 20 20 20 20 20 33 7d 2c 20 20 20 20 2f          3},    /
237e9 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20 20 20  * 1 */.         
237ea 20 20 20 7b 20 4f 50 5f 49 66 2c 20 20 20 20 20     { OP_If,     
237eb 20 20 20 20 20 20 20 20 31 2c 20 20 20 20 20 20          1,      
237ec 20 20 20 20 20 20 20 20 20 30 2c 20 20 20 20 20           0,     
237ed 20 20 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a     0},    /* 2 *
237ee 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20  /.            { 
237ef 4f 50 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20  OP_Halt,        
237f0 20 20 20 53 51 4c 49 54 45 5f 4f 4b 2c 20 20 20     SQLITE_OK,   
237f1 20 20 20 20 4f 45 5f 41 62 6f 72 74 2c 20 30 7d      OE_Abort, 0}
237f2 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20  ,    /* 3 */.   
237f3 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e           { OP_In
237f4 74 65 67 65 72 2c 20 20 20 20 20 20 20 20 30 2c  teger,        0,
237f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31                 1
237f6 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
237f7 2f 2a 20 34 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 4 */.        
237f8 20 20 20 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b      { OP_SetCook
237f9 69 65 2c 20 20 20 20 20 20 30 2c 20 20 20 20 20  ie,      0,     
237fa 20 20 20 20 20 20 20 20 20 20 36 2c 20 20 20 20            6,    
237fb 20 20 20 20 31 7d 2c 20 20 20 20 2f 2a 20 35 20      1},    /* 5 
237fc 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7d 3b 0a  */.          };.
237fd 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69 41            int iA
237fe 64 64 72 3b 0a 20 20 20 20 20 20 20 20 20 20 69  ddr;.          i
237ff 41 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  Addr = sqlite3Vd
23800 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
23801 72 72 61 79 53 69 7a 65 28 73 65 74 4d 65 74 61  rraySize(setMeta
23802 36 29 2c 20 73 65 74 4d 65 74 61 36 29 3b 0a 20  6), setMeta6);. 
23803 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
23804 56 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20  VdbeChangeP1(v, 
23805 69 41 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20  iAddr, iDb);.   
23806 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23807 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 69 41  beChangeP1(v, iA
23808 64 64 72 2b 31 2c 20 69 44 62 29 3b 0a 20 20 20  ddr+1, iDb);.   
23809 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
2380a 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20 69 41  beChangeP2(v, iA
2380b 64 64 72 2b 32 2c 20 69 41 64 64 72 2b 34 29 3b  ddr+2, iAddr+4);
2380c 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
2380d 65 33 56 64 62 65 43 68 61 6e 67 65 50 31 28 76  e3VdbeChangeP1(v
2380e 2c 20 69 41 64 64 72 2b 34 2c 20 65 41 75 74 6f  , iAddr+4, eAuto
2380f 2d 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  -1);.          s
23810 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23811 50 31 28 76 2c 20 69 41 64 64 72 2b 35 2c 20 69  P1(v, iAddr+5, i
23812 44 62 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  Db);.          s
23813 71 6c 69 74 65 33 56 64 62 65 55 73 65 73 42 74  qlite3VdbeUsesBt
23814 72 65 65 28 76 2c 20 69 44 62 29 3b 0a 20 20 20  ree(v, iDb);.   
23815 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
23816 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e     }.  }else.#en
23817 64 69 66 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20  dif..  /*.  **  
23818 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
23819 2e 5d 69 6e 63 72 65 6d 65 6e 74 61 6c 5f 76 61  .]incremental_va
2381a 63 75 75 6d 28 4e 29 0a 20 20 2a 2a 0a 20 20 2a  cuum(N).  **.  *
2381b 2a 20 44 6f 20 4e 20 73 74 65 70 73 20 6f 66 20  * Do N steps of 
2381c 69 6e 63 72 65 6d 65 6e 74 61 6c 20 76 61 63 75  incremental vacu
2381d 75 6d 69 6e 67 20 6f 6e 20 61 20 64 61 74 61 62  uming on a datab
2381e 61 73 65 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65  ase..  */.#ifnde
2381f 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
23820 54 4f 56 41 43 55 55 4d 0a 20 20 69 66 28 20 73  TOVACUUM.  if( s
23821 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c  qlite3StrICmp(zL
23822 65 66 74 2c 22 69 6e 63 72 65 6d 65 6e 74 61 6c  eft,"incremental
23823 5f 76 61 63 75 75 6d 22 29 3d 3d 30 20 29 7b 0a  _vacuum")==0 ){.
23824 20 20 20 20 69 6e 74 20 69 4c 69 6d 69 74 2c 20      int iLimit, 
23825 61 64 64 72 3b 0a 20 20 20 20 69 66 28 20 73 71  addr;.    if( sq
23826 6c 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28  lite3ReadSchema(
23827 70 50 61 72 73 65 29 20 29 7b 0a 20 20 20 20 20  pParse) ){.     
23828 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
23829 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2382a 7a 52 69 67 68 74 3d 3d 30 20 7c 7c 20 21 73 71  zRight==0 || !sq
2382b 6c 69 74 65 33 47 65 74 49 6e 74 33 32 28 7a 52  lite3GetInt32(zR
2382c 69 67 68 74 2c 20 26 69 4c 69 6d 69 74 29 20 7c  ight, &iLimit) |
2382d 7c 20 69 4c 69 6d 69 74 3c 3d 30 20 29 7b 0a 20  | iLimit<=0 ){. 
2382e 20 20 20 20 20 69 4c 69 6d 69 74 20 3d 20 30 78       iLimit = 0x
2382f 37 66 66 66 66 66 66 66 3b 0a 20 20 20 20 7d 0a  7fffffff;.    }.
23830 20 20 20 20 73 71 6c 69 74 65 33 42 65 67 69 6e      sqlite3Begin
23831 57 72 69 74 65 4f 70 65 72 61 74 69 6f 6e 28 70  WriteOperation(p
23832 50 61 72 73 65 2c 20 30 2c 20 69 44 62 29 3b 0a  Parse, 0, iDb);.
23833 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23834 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
23835 67 65 72 2c 20 69 4c 69 6d 69 74 2c 20 31 29 3b  ger, iLimit, 1);
23836 0a 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69  .    addr = sqli
23837 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
23838 20 4f 50 5f 49 6e 63 72 56 61 63 75 75 6d 2c 20   OP_IncrVacuum, 
23839 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  iDb);.    sqlite
2383a 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
2383b 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 31 29 3b  P_ResultRow, 1);
2383c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2383d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
2383e 49 6d 6d 2c 20 31 2c 20 2d 31 29 3b 0a 20 20 20  Imm, 1, -1);.   
2383f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23840 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p2(v, OP_IfPos, 
23841 31 2c 20 61 64 64 72 29 3b 0a 20 20 20 20 73 71  1, addr);.    sq
23842 6c 69 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72  lite3VdbeJumpHer
23843 65 28 76 2c 20 61 64 64 72 29 3b 0a 20 20 7d 65  e(v, addr);.  }e
23844 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  lse.#endif..#ifn
23845 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
23846 50 41 47 45 52 5f 50 52 41 47 4d 41 53 0a 20 20  PAGER_PRAGMAS.  
23847 2f 2a 0a 20 20 2a 2a 20 20 50 52 41 47 4d 41 20  /*.  **  PRAGMA 
23848 5b 64 61 74 61 62 61 73 65 2e 5d 63 61 63 68 65  [database.]cache
23849 5f 73 69 7a 65 0a 20 20 2a 2a 20 20 50 52 41 47  _size.  **  PRAG
2384a 4d 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 63 61  MA [database.]ca
2384b 63 68 65 5f 73 69 7a 65 3d 4e 0a 20 20 2a 2a 0a  che_size=N.  **.
2384c 20 20 2a 2a 20 54 68 65 20 66 69 72 73 74 20 66    ** The first f
2384d 6f 72 6d 20 72 65 70 6f 72 74 73 20 74 68 65 20  orm reports the 
2384e 63 75 72 72 65 6e 74 20 6c 6f 63 61 6c 20 73 65  current local se
2384f 74 74 69 6e 67 20 66 6f 72 20 74 68 65 0a 20 20  tting for the.  
23850 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69  ** page cache si
23851 7a 65 2e 20 20 54 68 65 20 6c 6f 63 61 6c 20 73  ze.  The local s
23852 65 74 74 69 6e 67 20 63 61 6e 20 62 65 20 64 69  etting can be di
23853 66 66 65 72 65 6e 74 20 66 72 6f 6d 0a 20 20 2a  fferent from.  *
23854 2a 20 74 68 65 20 70 65 72 73 69 73 74 65 6e 74  * the persistent
23855 20 63 61 63 68 65 20 73 69 7a 65 20 76 61 6c 75   cache size valu
23856 65 20 74 68 61 74 20 69 73 20 73 74 6f 72 65 64  e that is stored
23857 20 69 6e 20 74 68 65 20 64 61 74 61 62 61 73 65   in the database
23858 0a 20 20 2a 2a 20 66 69 6c 65 20 69 74 73 65 6c  .  ** file itsel
23859 66 2e 20 20 54 68 65 20 76 61 6c 75 65 20 72 65  f.  The value re
2385a 74 75 72 6e 65 64 20 69 73 20 74 68 65 20 6d 61  turned is the ma
2385b 78 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 0a  ximum number of.
2385c 20 20 2a 2a 20 70 61 67 65 73 20 69 6e 20 74 68    ** pages in th
2385d 65 20 70 61 67 65 20 63 61 63 68 65 2e 20 20 54  e page cache.  T
2385e 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d 20 73  he second form s
2385f 65 74 73 20 74 68 65 20 6c 6f 63 61 6c 0a 20 20  ets the local.  
23860 2a 2a 20 70 61 67 65 20 63 61 63 68 65 20 73 69  ** page cache si
23861 7a 65 20 76 61 6c 75 65 2e 20 20 49 74 20 64 6f  ze value.  It do
23862 65 73 20 6e 6f 74 20 63 68 61 6e 67 65 20 74 68  es not change th
23863 65 20 70 65 72 73 69 73 74 65 6e 74 0a 20 20 2a  e persistent.  *
23864 2a 20 63 61 63 68 65 20 73 69 7a 65 20 73 74 6f  * cache size sto
23865 72 65 64 20 6f 6e 20 74 68 65 20 64 69 73 6b 20  red on the disk 
23866 73 6f 20 74 68 65 20 63 61 63 68 65 20 73 69 7a  so the cache siz
23867 65 20 77 69 6c 6c 20 72 65 76 65 72 74 0a 20 20  e will revert.  
23868 2a 2a 20 74 6f 20 69 74 73 20 64 65 66 61 75 6c  ** to its defaul
23869 74 20 76 61 6c 75 65 20 77 68 65 6e 20 74 68 65  t value when the
2386a 20 64 61 74 61 62 61 73 65 20 69 73 20 63 6c 6f   database is clo
2386b 73 65 64 20 61 6e 64 20 72 65 6f 70 65 6e 65 64  sed and reopened
2386c 2e 0a 20 20 2a 2a 20 4e 20 73 68 6f 75 6c 64 20  ..  ** N should 
2386d 62 65 20 61 20 70 6f 73 69 74 69 76 65 20 69 6e  be a positive in
2386e 74 65 67 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 66  teger..  */.  if
2386f 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
23870 28 7a 4c 65 66 74 2c 22 63 61 63 68 65 5f 73 69  (zLeft,"cache_si
23871 7a 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  ze")==0 ){.    i
23872 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
23873 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
23874 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
23875 20 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20      if( !zRight 
23876 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 53  ){.      returnS
23877 69 6e 67 6c 65 49 6e 74 28 70 50 61 72 73 65 2c  ingleInt(pParse,
23878 20 22 63 61 63 68 65 5f 73 69 7a 65 22 2c 20 70   "cache_size", p
23879 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63 61 63  Db->pSchema->cac
2387a 68 65 5f 73 69 7a 65 29 3b 0a 20 20 20 20 7d 65  he_size);.    }e
2387b 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74 20 73  lse{.      int s
2387c 69 7a 65 20 3d 20 61 74 6f 69 28 7a 52 69 67 68  ize = atoi(zRigh
2387d 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20 73 69  t);.      if( si
2387e 7a 65 3c 30 20 29 20 73 69 7a 65 20 3d 20 2d 73  ze<0 ) size = -s
2387f 69 7a 65 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e  ize;.      pDb->
23880 70 53 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73  pSchema->cache_s
23881 69 7a 65 20 3d 20 73 69 7a 65 3b 0a 20 20 20 20  ize = size;.    
23882 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 65    sqlite3BtreeSe
23883 74 43 61 63 68 65 53 69 7a 65 28 70 44 62 2d 3e  tCacheSize(pDb->
23884 70 42 74 2c 20 70 44 62 2d 3e 70 53 63 68 65 6d  pBt, pDb->pSchem
23885 61 2d 3e 63 61 63 68 65 5f 73 69 7a 65 29 3b 0a  a->cache_size);.
23886 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
23887 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
23888 41 20 74 65 6d 70 5f 73 74 6f 72 65 0a 20 20 2a  A temp_store.  *
23889 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
2388a 73 74 6f 72 65 20 3d 20 22 64 65 66 61 75 6c 74  store = "default
2388b 22 7c 22 6d 65 6d 6f 72 79 22 7c 22 66 69 6c 65  "|"memory"|"file
2388c 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75  ".  **.  ** Retu
2388d 72 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f  rn or set the lo
2388e 63 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65  cal value of the
2388f 20 74 65 6d 70 5f 73 74 6f 72 65 20 66 6c 61 67   temp_store flag
23890 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a  .  Changing.  **
23891 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
23892 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63   does not make c
23893 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69  hanges to the di
23894 73 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65 20  sk file and the 
23895 64 65 66 61 75 6c 74 0a 20 20 2a 2a 20 76 61 6c  default.  ** val
23896 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f  ue will be resto
23897 72 65 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d  red the next tim
23898 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
23899 73 20 6f 70 65 6e 65 64 2e 0a 20 20 2a 2a 0a 20  s opened..  **. 
2389a 20 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 69 74   ** Note that it
2389b 20 69 73 20 70 6f 73 73 69 62 6c 65 20 66 6f 72   is possible for
2389c 20 74 68 65 20 6c 69 62 72 61 72 79 20 63 6f 6d   the library com
2389d 70 69 6c 65 2d 74 69 6d 65 20 6f 70 74 69 6f 6e  pile-time option
2389e 73 20 74 6f 0a 20 20 2a 2a 20 6f 76 65 72 72 69  s to.  ** overri
2389f 64 65 20 74 68 69 73 20 73 65 74 74 69 6e 67 0a  de this setting.
238a0 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
238a1 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
238a2 20 22 74 65 6d 70 5f 73 74 6f 72 65 22 29 3d 3d   "temp_store")==
238a3 30 20 29 7b 0a 20 20 20 20 69 66 28 20 21 7a 52  0 ){.    if( !zR
238a4 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 72 65  ight ){.      re
238a5 74 75 72 6e 53 69 6e 67 6c 65 49 6e 74 28 70 50  turnSingleInt(pP
238a6 61 72 73 65 2c 20 22 74 65 6d 70 5f 73 74 6f 72  arse, "temp_stor
238a7 65 22 2c 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f  e", db->temp_sto
238a8 72 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  re);.    }else{.
238a9 20 20 20 20 20 20 63 68 61 6e 67 65 54 65 6d 70        changeTemp
238aa 53 74 6f 72 61 67 65 28 70 50 61 72 73 65 2c 20  Storage(pParse, 
238ab 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20  zRight);.    }. 
238ac 20 7d 65 6c 73 65 0a 0a 20 20 2f 2a 0a 20 20 2a   }else..  /*.  *
238ad 2a 20 20 20 50 52 41 47 4d 41 20 74 65 6d 70 5f  *   PRAGMA temp_
238ae 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 0a  store_directory.
238af 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74 65    **   PRAGMA te
238b0 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63 74 6f  mp_store_directo
238b1 72 79 20 3d 20 22 22 7c 22 64 69 72 65 63 74 6f  ry = ""|"directo
238b2 72 79 5f 6e 61 6d 65 22 0a 20 20 2a 2a 0a 20 20  ry_name".  **.  
238b3 2a 2a 20 52 65 74 75 72 6e 20 6f 72 20 73 65 74  ** Return or set
238b4 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
238b5 20 6f 66 20 74 68 65 20 74 65 6d 70 5f 73 74 6f   of the temp_sto
238b6 72 65 5f 64 69 72 65 63 74 6f 72 79 20 66 6c 61  re_directory fla
238b7 67 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a  g.  Changing.  *
238b8 2a 20 74 68 65 20 76 61 6c 75 65 20 73 65 74 73  * the value sets
238b9 20 61 20 73 70 65 63 69 66 69 63 20 64 69 72 65   a specific dire
238ba 63 74 6f 72 79 20 74 6f 20 62 65 20 75 73 65 64  ctory to be used
238bb 20 66 6f 72 20 74 65 6d 70 6f 72 61 72 79 20 66   for temporary f
238bc 69 6c 65 73 2e 0a 20 20 2a 2a 20 53 65 74 74 69  iles..  ** Setti
238bd 6e 67 20 74 6f 20 61 20 6e 75 6c 6c 20 73 74 72  ng to a null str
238be 69 6e 67 20 72 65 76 65 72 74 73 20 74 6f 20 74  ing reverts to t
238bf 68 65 20 64 65 66 61 75 6c 74 20 74 65 6d 70 6f  he default tempo
238c0 72 61 72 79 20 64 69 72 65 63 74 6f 72 79 20 73  rary directory s
238c1 65 61 72 63 68 2e 0a 20 20 2a 2a 20 49 66 20 74  earch..  ** If t
238c2 65 6d 70 6f 72 61 72 79 20 64 69 72 65 63 74 6f  emporary directo
238c3 72 79 20 69 73 20 63 68 61 6e 67 65 64 2c 20 74  ry is changed, t
238c4 68 65 6e 20 69 6e 76 61 6c 69 64 61 74 65 54 65  hen invalidateTe
238c5 6d 70 53 74 6f 72 61 67 65 2e 0a 20 20 2a 2a 0a  mpStorage..  **.
238c6 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74    */.  if( sqlit
238c7 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c  e3StrICmp(zLeft,
238c8 20 22 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72   "temp_store_dir
238c9 65 63 74 6f 72 79 22 29 3d 3d 30 20 29 7b 0a 20  ectory")==0 ){. 
238ca 20 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29     if( !zRight )
238cb 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c 69  {.      if( sqli
238cc 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74 6f  te3_temp_directo
238cd 72 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ry ){.        sq
238ce 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
238cf 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
238d0 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
238d1 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43  tColName(v, 0, C
238d2 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a 20 20  OLNAME_NAME, .  
238d3 20 20 20 20 20 20 20 20 20 20 22 74 65 6d 70 5f            "temp_
238d4 73 74 6f 72 65 5f 64 69 72 65 63 74 6f 72 79 22  store_directory"
238d5 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
238d6 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
238d7 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
238d8 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 2c  P_String8, 0, 1,
238d9 20 30 2c 20 73 71 6c 69 74 65 33 5f 74 65 6d 70   0, sqlite3_temp
238da 5f 64 69 72 65 63 74 6f 72 79 2c 20 30 29 3b 0a  _directory, 0);.
238db 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
238dc 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
238dd 52 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 31 29  ResultRow, 1, 1)
238de 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 65  ;.      }.    }e
238df 6c 73 65 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  lse{.#ifndef SQL
238e0 49 54 45 5f 4f 4d 49 54 5f 57 53 44 0a 20 20 20  ITE_OMIT_WSD.   
238e1 20 20 20 69 66 28 20 7a 52 69 67 68 74 5b 30 5d     if( zRight[0]
238e2 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20   ){.        int 
238e3 72 63 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  rc;.        int 
238e4 72 65 73 3b 0a 20 20 20 20 20 20 20 20 72 63 20  res;.        rc 
238e5 3d 20 73 71 6c 69 74 65 33 4f 73 41 63 63 65 73  = sqlite3OsAcces
238e6 73 28 64 62 2d 3e 70 56 66 73 2c 20 7a 52 69 67  s(db->pVfs, zRig
238e7 68 74 2c 20 53 51 4c 49 54 45 5f 41 43 43 45 53  ht, SQLITE_ACCES
238e8 53 5f 52 45 41 44 57 52 49 54 45 2c 20 26 72 65  S_READWRITE, &re
238e9 73 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  s);.        if( 
238ea 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c  rc!=SQLITE_OK ||
238eb 20 72 65 73 3d 3d 30 20 29 7b 0a 20 20 20 20 20   res==0 ){.     
238ec 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
238ed 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f  rMsg(pParse, "no
238ee 74 20 61 20 77 72 69 74 61 62 6c 65 20 64 69 72  t a writable dir
238ef 65 63 74 6f 72 79 22 29 3b 0a 20 20 20 20 20 20  ectory");.      
238f0 20 20 20 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f      goto pragma_
238f1 6f 75 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  out;.        }. 
238f2 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
238f3 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
238f4 52 45 3d 3d 30 0a 20 20 20 20 20 20 20 7c 7c 20  RE==0.       || 
238f5 28 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f  (SQLITE_TEMP_STO
238f6 52 45 3d 3d 31 20 26 26 20 64 62 2d 3e 74 65 6d  RE==1 && db->tem
238f7 70 5f 73 74 6f 72 65 3c 3d 31 29 0a 20 20 20 20  p_store<=1).    
238f8 20 20 20 7c 7c 20 28 53 51 4c 49 54 45 5f 54 45     || (SQLITE_TE
238f9 4d 50 5f 53 54 4f 52 45 3d 3d 32 20 26 26 20 64  MP_STORE==2 && d
238fa 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 3d 3d 31  b->temp_store==1
238fb 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20  ).      ){.     
238fc 20 20 20 69 6e 76 61 6c 69 64 61 74 65 54 65 6d     invalidateTem
238fd 70 53 74 6f 72 61 67 65 28 70 50 61 72 73 65 29  pStorage(pParse)
238fe 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
238ff 73 71 6c 69 74 65 33 5f 66 72 65 65 28 73 71 6c  sqlite3_free(sql
23900 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65 63 74  ite3_temp_direct
23901 6f 72 79 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ory);.      if( 
23902 7a 52 69 67 68 74 5b 30 5d 20 29 7b 0a 20 20 20  zRight[0] ){.   
23903 20 20 20 20 20 73 71 6c 69 74 65 33 5f 74 65 6d       sqlite3_tem
23904 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20 73 71  p_directory = sq
23905 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 30 2c  lite3DbStrDup(0,
23906 20 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20   zRight);.      
23907 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
23908 71 6c 69 74 65 33 5f 74 65 6d 70 5f 64 69 72 65  qlite3_temp_dire
23909 63 74 6f 72 79 20 3d 20 30 3b 0a 20 20 20 20 20  ctory = 0;.     
2390a 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
2390b 49 54 45 5f 4f 4d 49 54 5f 57 53 44 20 2a 2f 0a  ITE_OMIT_WSD */.
2390c 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20      }.  }else.. 
2390d 20 2f 2a 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d   /*.  **   PRAGM
2390e 41 20 5b 64 61 74 61 62 61 73 65 2e 5d 73 79 6e  A [database.]syn
2390f 63 68 72 6f 6e 6f 75 73 0a 20 20 2a 2a 20 20 20  chronous.  **   
23910 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
23911 2e 5d 73 79 6e 63 68 72 6f 6e 6f 75 73 3d 4f 46  .]synchronous=OF
23912 46 7c 4f 4e 7c 4e 4f 52 4d 41 4c 7c 46 55 4c 4c  F|ON|NORMAL|FULL
23913 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74 75 72  .  **.  ** Retur
23914 6e 20 6f 72 20 73 65 74 20 74 68 65 20 6c 6f 63  n or set the loc
23915 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  al value of the 
23916 73 79 6e 63 68 72 6f 6e 6f 75 73 20 66 6c 61 67  synchronous flag
23917 2e 20 20 43 68 61 6e 67 69 6e 67 0a 20 20 2a 2a  .  Changing.  **
23918 20 74 68 65 20 6c 6f 63 61 6c 20 76 61 6c 75 65   the local value
23919 20 64 6f 65 73 20 6e 6f 74 20 6d 61 6b 65 20 63   does not make c
2391a 68 61 6e 67 65 73 20 74 6f 20 74 68 65 20 64 69  hanges to the di
2391b 73 6b 20 66 69 6c 65 20 61 6e 64 20 74 68 65 0a  sk file and the.
2391c 20 20 2a 2a 20 64 65 66 61 75 6c 74 20 76 61 6c    ** default val
2391d 75 65 20 77 69 6c 6c 20 62 65 20 72 65 73 74 6f  ue will be resto
2391e 72 65 64 20 74 68 65 20 6e 65 78 74 20 74 69 6d  red the next tim
2391f 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69  e the database i
23920 73 0a 20 20 2a 2a 20 6f 70 65 6e 65 64 2e 0a 20  s.  ** opened.. 
23921 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
23922 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 22  3StrICmp(zLeft,"
23923 73 79 6e 63 68 72 6f 6e 6f 75 73 22 29 3d 3d 30  synchronous")==0
23924 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   ){.    if( sqli
23925 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
23926 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
23927 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 69 66 28  gma_out;.    if(
23928 20 21 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20   !zRight ){.    
23929 20 20 72 65 74 75 72 6e 53 69 6e 67 6c 65 49 6e    returnSingleIn
2392a 74 28 70 50 61 72 73 65 2c 20 22 73 79 6e 63 68  t(pParse, "synch
2392b 72 6f 6e 6f 75 73 22 2c 20 70 44 62 2d 3e 73 61  ronous", pDb->sa
2392c 66 65 74 79 5f 6c 65 76 65 6c 2d 31 29 3b 0a 20  fety_level-1);. 
2392d 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2392e 69 66 28 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d  if( !db->autoCom
2392f 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  mit ){.        s
23930 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
23931 50 61 72 73 65 2c 20 0a 20 20 20 20 20 20 20 20  Parse, .        
23932 20 20 20 20 22 53 61 66 65 74 79 20 6c 65 76 65      "Safety leve
23933 6c 20 6d 61 79 20 6e 6f 74 20 62 65 20 63 68 61  l may not be cha
23934 6e 67 65 64 20 69 6e 73 69 64 65 20 61 20 74 72  nged inside a tr
23935 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20 20  ansaction");.   
23936 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
23937 20 20 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65    pDb->safety_le
23938 76 65 6c 20 3d 20 67 65 74 53 61 66 65 74 79 4c  vel = getSafetyL
23939 65 76 65 6c 28 7a 52 69 67 68 74 29 2b 31 3b 0a  evel(zRight)+1;.
2393a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2393b 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
2393c 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47 45  SQLITE_OMIT_PAGE
2393d 52 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  R_PRAGMAS */..#i
2393e 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
2393f 54 5f 46 4c 41 47 5f 50 52 41 47 4d 41 53 0a 20  T_FLAG_PRAGMAS. 
23940 20 69 66 28 20 66 6c 61 67 50 72 61 67 6d 61 28   if( flagPragma(
23941 70 50 61 72 73 65 2c 20 7a 4c 65 66 74 2c 20 7a  pParse, zLeft, z
23942 52 69 67 68 74 29 20 29 7b 0a 20 20 20 20 2f 2a  Right) ){.    /*
23943 20 54 68 65 20 66 6c 61 67 50 72 61 67 6d 61 28   The flagPragma(
23944 29 20 73 75 62 72 6f 75 74 69 6e 65 20 61 6c 73  ) subroutine als
23945 6f 20 67 65 6e 65 72 61 74 65 73 20 61 6e 79 20  o generates any 
23946 6e 65 63 65 73 73 61 72 79 20 63 6f 64 65 0a 20  necessary code. 
23947 20 20 20 2a 2a 20 74 68 65 72 65 20 69 73 20 6e     ** there is n
23948 6f 74 68 69 6e 67 20 6d 6f 72 65 20 74 6f 20 64  othing more to d
23949 6f 20 68 65 72 65 20 2a 2f 0a 20 20 7d 65 6c 73  o here */.  }els
2394a 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  e.#endif /* SQLI
2394b 54 45 5f 4f 4d 49 54 5f 46 4c 41 47 5f 50 52 41  TE_OMIT_FLAG_PRA
2394c 47 4d 41 53 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  GMAS */..#ifndef
2394d 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48   SQLITE_OMIT_SCH
2394e 45 4d 41 5f 50 52 41 47 4d 41 53 0a 20 20 2f 2a  EMA_PRAGMAS.  /*
2394f 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 74  .  **   PRAGMA t
23950 61 62 6c 65 5f 69 6e 66 6f 28 3c 74 61 62 6c 65  able_info(<table
23951 3e 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 52 65 74  >).  **.  ** Ret
23952 75 72 6e 20 61 20 73 69 6e 67 6c 65 20 72 6f 77  urn a single row
23953 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d 6e   for each column
23954 20 6f 66 20 74 68 65 20 6e 61 6d 65 64 20 74 61   of the named ta
23955 62 6c 65 2e 20 54 68 65 20 63 6f 6c 75 6d 6e 73  ble. The columns
23956 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 72 65 74   of.  ** the ret
23957 75 72 6e 65 64 20 64 61 74 61 20 73 65 74 20 61  urned data set a
23958 72 65 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 63 69  re:.  **.  ** ci
23959 64 3a 20 20 20 20 20 20 20 20 43 6f 6c 75 6d 6e  d:        Column
2395a 20 69 64 20 28 6e 75 6d 62 65 72 65 64 20 66 72   id (numbered fr
2395b 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67 68 74  om left to right
2395c 2c 20 73 74 61 72 74 69 6e 67 20 61 74 20 30 29  , starting at 0)
2395d 0a 20 20 2a 2a 20 6e 61 6d 65 3a 20 20 20 20 20  .  ** name:     
2395e 20 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 0a 20 20    Column name.  
2395f 2a 2a 20 74 79 70 65 3a 20 20 20 20 20 20 20 43  ** type:       C
23960 6f 6c 75 6d 6e 20 64 65 63 6c 61 72 61 74 69 6f  olumn declaratio
23961 6e 20 74 79 70 65 2e 0a 20 20 2a 2a 20 6e 6f 74  n type..  ** not
23962 6e 75 6c 6c 3a 20 20 20 20 54 72 75 65 20 69 66  null:    True if
23963 20 27 4e 4f 54 20 4e 55 4c 4c 27 20 69 73 20 70   'NOT NULL' is p
23964 61 72 74 20 6f 66 20 63 6f 6c 75 6d 6e 20 64 65  art of column de
23965 63 6c 61 72 61 74 69 6f 6e 0a 20 20 2a 2a 20 64  claration.  ** d
23966 66 6c 74 5f 76 61 6c 75 65 3a 20 54 68 65 20 64  flt_value: The d
23967 65 66 61 75 6c 74 20 76 61 6c 75 65 20 66 6f 72  efault value for
23968 20 74 68 65 20 63 6f 6c 75 6d 6e 2c 20 69 66 20   the column, if 
23969 61 6e 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  any..  */.  if( 
2396a 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a  sqlite3StrICmp(z
2396b 4c 65 66 74 2c 20 22 74 61 62 6c 65 5f 69 6e 66  Left, "table_inf
2396c 6f 22 29 3d 3d 30 20 26 26 20 7a 52 69 67 68 74  o")==0 && zRight
2396d 20 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70   ){.    Table *p
2396e 54 61 62 3b 0a 20 20 20 20 69 66 28 20 73 71 6c  Tab;.    if( sql
2396f 69 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70  ite3ReadSchema(p
23970 50 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72  Parse) ) goto pr
23971 61 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 70 54  agma_out;.    pT
23972 61 62 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64  ab = sqlite3Find
23973 54 61 62 6c 65 28 64 62 2c 20 7a 52 69 67 68 74  Table(db, zRight
23974 2c 20 7a 44 62 29 3b 0a 20 20 20 20 69 66 28 20  , zDb);.    if( 
23975 70 54 61 62 20 29 7b 0a 20 20 20 20 20 20 69 6e  pTab ){.      in
23976 74 20 69 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t i;.      int n
23977 48 69 64 64 65 6e 20 3d 20 30 3b 0a 20 20 20 20  Hidden = 0;.    
23978 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 3b 0a    Column *pCol;.
23979 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2397a 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20 36  eSetNumCols(v, 6
2397b 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  );.      pParse-
2397c 3e 6e 4d 65 6d 20 3d 20 36 3b 0a 20 20 20 20 20  >nMem = 6;.     
2397d 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
2397e 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
2397f 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22  NAME_NAME, "cid"
23980 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
23981 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
23982 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
23983 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
23984 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45  , "name", SQLITE
23985 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
23986 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
23987 6c 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e  lName(v, 2, COLN
23988 41 4d 45 5f 4e 41 4d 45 2c 20 22 74 79 70 65 22  AME_NAME, "type"
23989 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
2398a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2398b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
2398c 20 33 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   3, COLNAME_NAME
2398d 2c 20 22 6e 6f 74 6e 75 6c 6c 22 2c 20 53 51 4c  , "notnull", SQL
2398e 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
2398f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
23990 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 34 2c 20 43  tColName(v, 4, C
23991 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 64 66  OLNAME_NAME, "df
23992 6c 74 5f 76 61 6c 75 65 22 2c 20 53 51 4c 49 54  lt_value", SQLIT
23993 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
23994 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
23995 6f 6c 4e 61 6d 65 28 76 2c 20 35 2c 20 43 4f 4c  olName(v, 5, COL
23996 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 6b 22 2c  NAME_NAME, "pk",
23997 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
23998 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 69  .      sqlite3Vi
23999 65 77 47 65 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73  ewGetColumnNames
2399a 28 70 50 61 72 73 65 2c 20 70 54 61 62 29 3b 0a  (pParse, pTab);.
2399b 20 20 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70        for(i=0, p
2399c 43 6f 6c 3d 70 54 61 62 2d 3e 61 43 6f 6c 3b 20  Col=pTab->aCol; 
2399d 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
2399e 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20  +, pCol++){.    
2399f 20 20 20 20 63 6f 6e 73 74 20 54 6f 6b 65 6e 20      const Token 
239a0 2a 70 44 66 6c 74 3b 0a 20 20 20 20 20 20 20 20  *pDflt;.        
239a1 69 66 28 20 49 73 48 69 64 64 65 6e 43 6f 6c 75  if( IsHiddenColu
239a2 6d 6e 28 70 43 6f 6c 29 20 29 7b 0a 20 20 20 20  mn(pCol) ){.    
239a3 20 20 20 20 20 20 6e 48 69 64 64 65 6e 2b 2b 3b        nHidden++;
239a4 0a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 74 69  .          conti
239a5 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  nue;.        }. 
239a6 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
239a7 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
239a8 6e 74 65 67 65 72 2c 20 69 2d 6e 48 69 64 64 65  nteger, i-nHidde
239a9 6e 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20 73  n, 1);.        s
239aa 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
239ab 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20  (v, OP_String8, 
239ac 30 2c 20 32 2c 20 30 2c 20 70 43 6f 6c 2d 3e 7a  0, 2, 0, pCol->z
239ad 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20  Name, 0);.      
239ae 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
239af 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
239b0 38 2c 20 30 2c 20 33 2c 20 30 2c 0a 20 20 20 20  8, 0, 3, 0,.    
239b1 20 20 20 20 20 20 20 70 43 6f 6c 2d 3e 7a 54 79         pCol->zTy
239b2 70 65 20 3f 20 70 43 6f 6c 2d 3e 7a 54 79 70 65  pe ? pCol->zType
239b3 20 3a 20 22 22 2c 20 30 29 3b 0a 20 20 20 20 20   : "", 0);.     
239b4 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
239b5 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
239b6 65 72 2c 20 28 70 43 6f 6c 2d 3e 6e 6f 74 4e 75  er, (pCol->notNu
239b7 6c 6c 20 3f 20 31 20 3a 20 30 29 2c 20 34 29 3b  ll ? 1 : 0), 4);
239b8 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 43 6f  .        if( pCo
239b9 6c 2d 3e 70 44 66 6c 74 20 26 26 20 28 70 44 66  l->pDflt && (pDf
239ba 6c 74 20 3d 20 26 70 43 6f 6c 2d 3e 70 44 66 6c  lt = &pCol->pDfl
239bb 74 2d 3e 73 70 61 6e 29 2d 3e 7a 20 29 7b 0a 20  t->span)->z ){. 
239bc 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
239bd 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
239be 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 35 2c 20  _String8, 0, 5, 
239bf 30 2c 20 28 63 68 61 72 2a 29 70 44 66 6c 74 2d  0, (char*)pDflt-
239c0 3e 7a 2c 20 70 44 66 6c 74 2d 3e 6e 29 3b 0a 20  >z, pDflt->n);. 
239c1 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20         }else{.  
239c2 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
239c3 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
239c4 4e 75 6c 6c 2c 20 30 2c 20 35 29 3b 0a 20 20 20  Null, 0, 5);.   
239c5 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 73       }.        s
239c6 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
239c7 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
239c8 70 43 6f 6c 2d 3e 69 73 50 72 69 6d 4b 65 79 2c  pCol->isPrimKey,
239c9 20 36 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c   6);.        sql
239ca 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
239cb 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
239cc 31 2c 20 36 29 3b 0a 20 20 20 20 20 20 7d 0a 20  1, 6);.      }. 
239cd 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 0a 20 20     }.  }else..  
239ce 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
239cf 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e 64 65 78  mp(zLeft, "index
239d0 5f 69 6e 66 6f 22 29 3d 3d 30 20 26 26 20 7a 52  _info")==0 && zR
239d1 69 67 68 74 20 29 7b 0a 20 20 20 20 49 6e 64 65  ight ){.    Inde
239d2 78 20 2a 70 49 64 78 3b 0a 20 20 20 20 54 61 62  x *pIdx;.    Tab
239d3 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 69 66  le *pTab;.    if
239d4 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63 68  ( sqlite3ReadSch
239d5 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67 6f  ema(pParse) ) go
239d6 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a 20  to pragma_out;. 
239d7 20 20 20 70 49 64 78 20 3d 20 73 71 6c 69 74 65     pIdx = sqlite
239d8 33 46 69 6e 64 49 6e 64 65 78 28 64 62 2c 20 7a  3FindIndex(db, z
239d9 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a 20 20 20  Right, zDb);.   
239da 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20 20 20   if( pIdx ){.   
239db 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 20 20     int i;.      
239dc 70 54 61 62 20 3d 20 70 49 64 78 2d 3e 70 54 61  pTab = pIdx->pTa
239dd 62 6c 65 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ble;.      sqlit
239de 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
239df 28 76 2c 20 33 29 3b 0a 20 20 20 20 20 20 70 50  (v, 3);.      pP
239e0 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a  arse->nMem = 3;.
239e1 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
239e2 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
239e3 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
239e4 22 73 65 71 6e 6f 22 2c 20 53 51 4c 49 54 45 5f  "seqno", SQLITE_
239e5 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73  STATIC);.      s
239e6 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
239e7 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e 41  Name(v, 1, COLNA
239e8 4d 45 5f 4e 41 4d 45 2c 20 22 63 69 64 22 2c 20  ME_NAME, "cid", 
239e9 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
239ea 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
239eb 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 32  eSetColName(v, 2
239ec 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
239ed 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53  "name", SQLITE_S
239ee 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 66 6f  TATIC);.      fo
239ef 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 2d 3e 6e  r(i=0; i<pIdx->n
239f0 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20  Column; i++){.  
239f1 20 20 20 20 20 20 69 6e 74 20 63 6e 75 6d 20 3d        int cnum =
239f2 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b   pIdx->aiColumn[
239f3 69 5d 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  i];.        sqli
239f4 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
239f5 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2c 20   OP_Integer, i, 
239f6 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  1);.        sqli
239f7 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
239f8 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 63 6e 75   OP_Integer, cnu
239f9 6d 2c 20 32 29 3b 0a 20 20 20 20 20 20 20 20 61  m, 2);.        a
239fa 73 73 65 72 74 28 20 70 54 61 62 2d 3e 6e 43 6f  ssert( pTab->nCo
239fb 6c 3e 63 6e 75 6d 20 29 3b 0a 20 20 20 20 20 20  l>cnum );.      
239fc 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
239fd 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
239fe 38 2c 20 30 2c 20 33 2c 20 30 2c 20 70 54 61 62  8, 0, 3, 0, pTab
239ff 2d 3e 61 43 6f 6c 5b 63 6e 75 6d 5d 2e 7a 4e 61  ->aCol[cnum].zNa
23a00 6d 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20  me, 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 52 65 73 75 6c 74 52 6f  2(v, OP_ResultRo
23a03 77 2c 20 31 2c 20 33 29 3b 0a 20 20 20 20 20 20  w, 1, 3);.      
23a04 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 0a  }.    }.  }else.
23a05 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74  .  if( sqlite3St
23a06 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 69 6e  rICmp(zLeft, "in
23a07 64 65 78 5f 6c 69 73 74 22 29 3d 3d 30 20 26 26  dex_list")==0 &&
23a08 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 49   zRight ){.    I
23a09 6e 64 65 78 20 2a 70 49 64 78 3b 0a 20 20 20 20  ndex *pIdx;.    
23a0a 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20  Table *pTab;.   
23a0b 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61 64   if( sqlite3Read
23a0c 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20 29  Schema(pParse) )
23a0d 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74   goto pragma_out
23a0e 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71 6c  ;.    pTab = sql
23a0f 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64 62  ite3FindTable(db
23a10 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b 0a  , zRight, zDb);.
23a11 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b 0a      if( pTab ){.
23a12 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65        v = sqlite
23a13 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
23a14 3b 0a 20 20 20 20 20 20 70 49 64 78 20 3d 20 70  ;.      pIdx = p
23a15 54 61 62 2d 3e 70 49 6e 64 65 78 3b 0a 20 20 20  Tab->pIndex;.   
23a16 20 20 20 69 66 28 20 70 49 64 78 20 29 7b 0a 20     if( pIdx ){. 
23a17 20 20 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30         int i = 0
23a18 3b 20 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ; .        sqlit
23a19 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
23a1a 28 76 2c 20 33 29 3b 0a 20 20 20 20 20 20 20 20  (v, 3);.        
23a1b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33  pParse->nMem = 3
23a1c 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
23a1d 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
23a1e 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41  v, 0, COLNAME_NA
23a1f 4d 45 2c 20 22 73 65 71 22 2c 20 53 51 4c 49 54  ME, "seq", SQLIT
23a20 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
23a21 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65     sqlite3VdbeSe
23a22 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43  tColName(v, 1, C
23a23 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6e 61  OLNAME_NAME, "na
23a24 6d 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  me", SQLITE_STAT
23a25 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  IC);.        sql
23a26 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
23a27 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45  me(v, 2, COLNAME
23a28 5f 4e 41 4d 45 2c 20 22 75 6e 69 71 75 65 22 2c  _NAME, "unique",
23a29 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
23a2a 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28 70  .        while(p
23a2b 49 64 78 29 7b 0a 20 20 20 20 20 20 20 20 20 20  Idx){.          
23a2c 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a2d 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
23a2e 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20   i, 1);.        
23a2f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23a30 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67  Op4(v, OP_String
23a31 38 2c 20 30 2c 20 32 2c 20 30 2c 20 70 49 64 78  8, 0, 2, 0, pIdx
23a32 2d 3e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20  ->zName, 0);.   
23a33 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23a34 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
23a35 6e 74 65 67 65 72 2c 20 70 49 64 78 2d 3e 6f 6e  nteger, pIdx->on
23a36 45 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 2c 20  Error!=OE_None, 
23a37 33 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  3);.          sq
23a38 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23a39 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
23a3a 20 31 2c 20 33 29 3b 0a 20 20 20 20 20 20 20 20   1, 3);.        
23a3b 20 20 2b 2b 69 3b 0a 20 20 20 20 20 20 20 20 20    ++i;.         
23a3c 20 70 49 64 78 20 3d 20 70 49 64 78 2d 3e 70 4e   pIdx = pIdx->pN
23a3d 65 78 74 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ext;.        }. 
23a3e 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
23a3f 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c 69  else..  if( sqli
23a40 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66 74  te3StrICmp(zLeft
23a41 2c 20 22 64 61 74 61 62 61 73 65 5f 6c 69 73 74  , "database_list
23a42 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 6e 74  ")==0 ){.    int
23a43 20 69 3b 0a 20 20 20 20 69 66 28 20 73 71 6c 69   i;.    if( sqli
23a44 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 70 50  te3ReadSchema(pP
23a45 61 72 73 65 29 20 29 20 67 6f 74 6f 20 70 72 61  arse) ) goto pra
23a46 67 6d 61 5f 6f 75 74 3b 0a 20 20 20 20 73 71 6c  gma_out;.    sql
23a47 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
23a48 6c 73 28 76 2c 20 33 29 3b 0a 20 20 20 20 70 50  ls(v, 3);.    pP
23a49 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 33 3b 0a  arse->nMem = 3;.
23a4a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
23a4b 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20  etColName(v, 0, 
23a4c 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 73  COLNAME_NAME, "s
23a4d 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  eq", SQLITE_STAT
23a4e 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  IC);.    sqlite3
23a4f 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
23a50 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 1, COLNAME_NAM
23a51 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c 49 54  E, "name", SQLIT
23a52 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 73  E_STATIC);.    s
23a53 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c  qlite3VdbeSetCol
23a54 4e 61 6d 65 28 76 2c 20 32 2c 20 43 4f 4c 4e 41  Name(v, 2, COLNA
23a55 4d 45 5f 4e 41 4d 45 2c 20 22 66 69 6c 65 22 2c  ME_NAME, "file",
23a56 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
23a57 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
23a58 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
23a59 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62       if( db->aDb
23a5a 5b 69 5d 2e 70 42 74 3d 3d 30 20 29 20 63 6f 6e  [i].pBt==0 ) con
23a5b 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 61 73 73  tinue;.      ass
23a5c 65 72 74 28 20 64 62 2d 3e 61 44 62 5b 69 5d 2e  ert( db->aDb[i].
23a5d 7a 4e 61 6d 65 21 3d 30 20 29 3b 0a 20 20 20 20  zName!=0 );.    
23a5e 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23a5f 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
23a60 72 2c 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20  r, i, 1);.      
23a61 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23a62 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
23a63 20 30 2c 20 32 2c 20 30 2c 20 64 62 2d 3e 61 44   0, 2, 0, db->aD
23a64 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a  b[i].zName, 0);.
23a65 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23a66 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
23a67 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a  ring8, 0, 3, 0,.
23a68 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
23a69 65 33 42 74 72 65 65 47 65 74 46 69 6c 65 6e 61  e3BtreeGetFilena
23a6a 6d 65 28 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 42  me(db->aDb[i].pB
23a6b 74 29 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71  t), 0);.      sq
23a6c 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23a6d 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
23a6e 20 31 2c 20 33 29 3b 0a 20 20 20 20 7d 0a 20 20   1, 3);.    }.  
23a6f 7d 65 6c 73 65 0a 0a 20 20 69 66 28 20 73 71 6c  }else..  if( sql
23a70 69 74 65 33 53 74 72 49 43 6d 70 28 7a 4c 65 66  ite3StrICmp(zLef
23a71 74 2c 20 22 63 6f 6c 6c 61 74 69 6f 6e 5f 6c 69  t, "collation_li
23a72 73 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69  st")==0 ){.    i
23a73 6e 74 20 69 20 3d 20 30 3b 0a 20 20 20 20 48 61  nt i = 0;.    Ha
23a74 73 68 45 6c 65 6d 20 2a 70 3b 0a 20 20 20 20 73  shElem *p;.    s
23a75 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d  qlite3VdbeSetNum
23a76 43 6f 6c 73 28 76 2c 20 32 29 3b 0a 20 20 20 20  Cols(v, 2);.    
23a77 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 32  pParse->nMem = 2
23a78 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23a79 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 30  eSetColName(v, 0
23a7a 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
23a7b 22 73 65 71 22 2c 20 53 51 4c 49 54 45 5f 53 54  "seq", SQLITE_ST
23a7c 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69 74  ATIC);.    sqlit
23a7d 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
23a7e 28 76 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 1, COLNAME_N
23a7f 41 4d 45 2c 20 22 6e 61 6d 65 22 2c 20 53 51 4c  AME, "name", SQL
23a80 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
23a81 20 66 6f 72 28 70 3d 73 71 6c 69 74 65 48 61 73   for(p=sqliteHas
23a82 68 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c  hFirst(&db->aCol
23a83 6c 53 65 71 29 3b 20 70 3b 20 70 3d 73 71 6c 69  lSeq); p; p=sqli
23a84 74 65 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a  teHashNext(p)){.
23a85 20 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70        CollSeq *p
23a86 43 6f 6c 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20  Coll = (CollSeq 
23a87 2a 29 73 71 6c 69 74 65 48 61 73 68 44 61 74 61  *)sqliteHashData
23a88 28 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  (p);.      sqlit
23a89 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
23a8a 4f 50 5f 49 6e 74 65 67 65 72 2c 20 69 2b 2b 2c  OP_Integer, i++,
23a8b 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74   1);.      sqlit
23a8c 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
23a8d 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 32  OP_String8, 0, 2
23a8e 2c 20 30 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d  , 0, pColl->zNam
23a8f 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c  e, 0);.      sql
23a90 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
23a91 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20  , OP_ResultRow, 
23a92 31 2c 20 32 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  1, 2);.    }.  }
23a93 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53  else.#endif /* S
23a94 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d  QLITE_OMIT_SCHEM
23a95 41 5f 50 52 41 47 4d 41 53 20 2a 2f 0a 0a 23 69  A_PRAGMAS */..#i
23a96 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  fndef SQLITE_OMI
23a97 54 5f 46 4f 52 45 49 47 4e 5f 4b 45 59 0a 20 20  T_FOREIGN_KEY.  
23a98 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
23a99 6d 70 28 7a 4c 65 66 74 2c 20 22 66 6f 72 65 69  mp(zLeft, "forei
23a9a 67 6e 5f 6b 65 79 5f 6c 69 73 74 22 29 3d 3d 30  gn_key_list")==0
23a9b 20 26 26 20 7a 52 69 67 68 74 20 29 7b 0a 20 20   && zRight ){.  
23a9c 20 20 46 4b 65 79 20 2a 70 46 4b 3b 0a 20 20 20    FKey *pFK;.   
23a9d 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
23a9e 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
23a9f 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
23aa0 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
23aa1 74 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73 71  t;.    pTab = sq
23aa2 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
23aa3 62 2c 20 7a 52 69 67 68 74 2c 20 7a 44 62 29 3b  b, zRight, zDb);
23aa4 0a 20 20 20 20 69 66 28 20 70 54 61 62 20 29 7b  .    if( pTab ){
23aa5 0a 20 20 20 20 20 20 76 20 3d 20 73 71 6c 69 74  .      v = sqlit
23aa6 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
23aa7 29 3b 0a 20 20 20 20 20 20 70 46 4b 20 3d 20 70  );.      pFK = p
23aa8 54 61 62 2d 3e 70 46 4b 65 79 3b 0a 20 20 20 20  Tab->pFKey;.    
23aa9 20 20 69 66 28 20 70 46 4b 20 29 7b 0a 20 20 20    if( pFK ){.   
23aaa 20 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 20       int i = 0; 
23aab 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23aac 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
23aad 2c 20 38 29 3b 0a 20 20 20 20 20 20 20 20 70 50  , 8);.        pP
23aae 61 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 38 3b 0a  arse->nMem = 8;.
23aaf 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23ab0 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
23ab1 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   0, COLNAME_NAME
23ab2 2c 20 22 69 64 22 2c 20 53 51 4c 49 54 45 5f 53  , "id", SQLITE_S
23ab3 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20  TATIC);.        
23ab4 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
23ab5 6c 4e 61 6d 65 28 76 2c 20 31 2c 20 43 4f 4c 4e  lName(v, 1, COLN
23ab6 41 4d 45 5f 4e 41 4d 45 2c 20 22 73 65 71 22 2c  AME_NAME, "seq",
23ab7 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
23ab8 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23ab9 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
23aba 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 2, COLNAME_NAM
23abb 45 2c 20 22 74 61 62 6c 65 22 2c 20 53 51 4c 49  E, "table", SQLI
23abc 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23abd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
23abe 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 33 2c 20  etColName(v, 3, 
23abf 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 66  COLNAME_NAME, "f
23ac0 72 6f 6d 22 2c 20 53 51 4c 49 54 45 5f 53 54 41  rom", SQLITE_STA
23ac1 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 73 71  TIC);.        sq
23ac2 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
23ac3 61 6d 65 28 76 2c 20 34 2c 20 43 4f 4c 4e 41 4d  ame(v, 4, COLNAM
23ac4 45 5f 4e 41 4d 45 2c 20 22 74 6f 22 2c 20 53 51  E_NAME, "to", SQ
23ac5 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20  LITE_STATIC);.  
23ac6 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23ac7 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 35  eSetColName(v, 5
23ac8 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
23ac9 22 6f 6e 5f 75 70 64 61 74 65 22 2c 20 53 51 4c  "on_update", SQL
23aca 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
23acb 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23acc 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 36 2c  SetColName(v, 6,
23acd 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
23ace 6f 6e 5f 64 65 6c 65 74 65 22 2c 20 53 51 4c 49  on_delete", SQLI
23acf 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23ad0 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
23ad1 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 37 2c 20  etColName(v, 7, 
23ad2 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6d  COLNAME_NAME, "m
23ad3 61 74 63 68 22 2c 20 53 51 4c 49 54 45 5f 53 54  atch", SQLITE_ST
23ad4 41 54 49 43 29 3b 0a 20 20 20 20 20 20 20 20 77  ATIC);.        w
23ad5 68 69 6c 65 28 70 46 4b 29 7b 0a 20 20 20 20 20  hile(pFK){.     
23ad6 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
23ad7 20 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a        for(j=0; j
23ad8 3c 70 46 4b 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29  <pFK->nCol; j++)
23ad9 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68  {.            ch
23ada 61 72 20 2a 7a 43 6f 6c 20 3d 20 70 46 4b 2d 3e  ar *zCol = pFK->
23adb 61 43 6f 6c 5b 6a 5d 2e 7a 43 6f 6c 3b 0a 20 20  aCol[j].zCol;.  
23adc 20 20 20 20 20 20 20 20 20 20 63 68 61 72 20 2a            char *
23add 7a 4f 6e 55 70 64 61 74 65 20 3d 20 28 63 68 61  zOnUpdate = (cha
23ade 72 20 2a 29 61 63 74 69 6f 6e 4e 61 6d 65 28 70  r *)actionName(p
23adf 46 4b 2d 3e 75 70 64 61 74 65 43 6f 6e 66 29 3b  FK->updateConf);
23ae0 0a 20 20 20 20 20 20 20 20 20 20 20 20 63 68 61  .            cha
23ae1 72 20 2a 7a 4f 6e 44 65 6c 65 74 65 20 3d 20 28  r *zOnDelete = (
23ae2 63 68 61 72 20 2a 29 61 63 74 69 6f 6e 4e 61 6d  char *)actionNam
23ae3 65 28 70 46 4b 2d 3e 64 65 6c 65 74 65 43 6f 6e  e(pFK->deleteCon
23ae4 66 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  f);.            
23ae5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23ae6 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
23ae7 20 69 2c 20 31 29 3b 0a 20 20 20 20 20 20 20 20   i, 1);.        
23ae8 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23ae9 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
23aea 67 65 72 2c 20 6a 2c 20 32 29 3b 0a 20 20 20 20  ger, j, 2);.    
23aeb 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23aec 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
23aed 53 74 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30  String8, 0, 3, 0
23aee 2c 20 70 46 4b 2d 3e 7a 54 6f 2c 20 30 29 3b 0a  , pFK->zTo, 0);.
23aef 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
23af0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
23af1 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20   OP_String8, 0, 
23af2 34 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  4, 0,.          
23af3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23af4 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 70      pTab->aCol[p
23af5 46 4b 2d 3e 61 43 6f 6c 5b 6a 5d 2e 69 46 72 6f  FK->aCol[j].iFro
23af6 6d 5d 2e 7a 4e 61 6d 65 2c 20 30 29 3b 0a 20 20  m].zName, 0);.  
23af7 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
23af8 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 7a  3VdbeAddOp4(v, z
23af9 43 6f 6c 20 3f 20 4f 50 5f 53 74 72 69 6e 67 38  Col ? OP_String8
23afa 20 3a 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 35   : OP_Null, 0, 5
23afb 2c 20 30 2c 20 7a 43 6f 6c 2c 20 30 29 3b 0a 20  , 0, zCol, 0);. 
23afc 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
23afd 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20  e3VdbeAddOp4(v, 
23afe 4f 50 5f 53 74 72 69 6e 67 38 2c 20 30 2c 20 36  OP_String8, 0, 6
23aff 2c 20 30 2c 20 7a 4f 6e 55 70 64 61 74 65 2c 20  , 0, zOnUpdate, 
23b00 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  0);.            
23b01 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
23b02 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38 2c  4(v, OP_String8,
23b03 20 30 2c 20 37 2c 20 30 2c 20 7a 4f 6e 44 65 6c   0, 7, 0, zOnDel
23b04 65 74 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ete, 0);.       
23b05 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23b06 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72  AddOp4(v, OP_Str
23b07 69 6e 67 38 2c 20 30 2c 20 38 2c 20 30 2c 20 22  ing8, 0, 8, 0, "
23b08 4e 4f 4e 45 22 2c 20 30 29 3b 0a 20 20 20 20 20  NONE", 0);.     
23b09 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23b0a 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
23b0b 65 73 75 6c 74 52 6f 77 2c 20 31 2c 20 38 29 3b  esultRow, 1, 8);
23b0c 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
23b0d 20 20 20 20 20 20 20 2b 2b 69 3b 0a 20 20 20 20         ++i;.    
23b0e 20 20 20 20 20 20 70 46 4b 20 3d 20 70 46 4b 2d        pFK = pFK-
23b0f 3e 70 4e 65 78 74 46 72 6f 6d 3b 0a 20 20 20 20  >pNextFrom;.    
23b10 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
23b11 20 20 7d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64    }.  }else.#end
23b12 69 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53  if /* !defined(S
23b13 51 4c 49 54 45 5f 4f 4d 49 54 5f 46 4f 52 45 49  QLITE_OMIT_FOREI
23b14 47 4e 5f 4b 45 59 29 20 2a 2f 0a 0a 23 69 66 6e  GN_KEY) */..#ifn
23b15 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69 66 28  def NDEBUG.  if(
23b16 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23b17 7a 4c 65 66 74 2c 20 22 70 61 72 73 65 72 5f 74  zLeft, "parser_t
23b18 72 61 63 65 22 29 3d 3d 30 20 29 7b 0a 20 20 20  race")==0 ){.   
23b19 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20   if( zRight ){. 
23b1a 20 20 20 20 20 69 66 28 20 67 65 74 42 6f 6f 6c       if( getBool
23b1b 65 61 6e 28 7a 52 69 67 68 74 29 20 29 7b 0a 20  ean(zRight) ){. 
23b1c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 50 61         sqlite3Pa
23b1d 72 73 65 72 54 72 61 63 65 28 73 74 64 65 72 72  rserTrace(stderr
23b1e 2c 20 22 70 61 72 73 65 72 3a 20 22 29 3b 0a 20  , "parser: ");. 
23b1f 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
23b20 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
23b21 72 54 72 61 63 65 28 30 2c 20 30 29 3b 0a 20 20  rTrace(0, 0);.  
23b22 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65      }.    }.  }e
23b23 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a  lse.#endif..  /*
23b24 20 52 65 69 6e 73 74 61 6c 6c 20 74 68 65 20 4c   Reinstall the L
23b25 49 4b 45 20 61 6e 64 20 47 4c 4f 42 20 66 75 6e  IKE and GLOB fun
23b26 63 74 69 6f 6e 73 2e 20 20 54 68 65 20 76 61 72  ctions.  The var
23b27 69 61 6e 74 20 6f 66 20 4c 49 4b 45 0a 20 20 2a  iant of LIKE.  *
23b28 2a 20 75 73 65 64 20 77 69 6c 6c 20 62 65 20 63  * used will be c
23b29 61 73 65 20 73 65 6e 73 69 74 69 76 65 20 6f 72  ase sensitive or
23b2a 20 6e 6f 74 20 64 65 70 65 6e 64 69 6e 67 20 6f   not depending o
23b2b 6e 20 74 68 65 20 52 48 53 2e 0a 20 20 2a 2f 0a  n the RHS..  */.
23b2c 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
23b2d 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 63 61 73  ICmp(zLeft, "cas
23b2e 65 5f 73 65 6e 73 69 74 69 76 65 5f 6c 69 6b 65  e_sensitive_like
23b2f 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  ")==0 ){.    if(
23b30 20 7a 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20   zRight ){.     
23b31 20 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72   sqlite3Register
23b32 4c 69 6b 65 46 75 6e 63 74 69 6f 6e 73 28 64 62  LikeFunctions(db
23b33 2c 20 67 65 74 42 6f 6f 6c 65 61 6e 28 7a 52 69  , getBoolean(zRi
23b34 67 68 74 29 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  ght));.    }.  }
23b35 65 6c 73 65 0a 0a 23 69 66 6e 64 65 66 20 53 51  else..#ifndef SQ
23b36 4c 49 54 45 5f 49 4e 54 45 47 52 49 54 59 5f 43  LITE_INTEGRITY_C
23b37 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41 58 0a 23  HECK_ERROR_MAX.#
23b38 20 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 49   define SQLITE_I
23b39 4e 54 45 47 52 49 54 59 5f 43 48 45 43 4b 5f 45  NTEGRITY_CHECK_E
23b3a 52 52 4f 52 5f 4d 41 58 20 31 30 30 0a 23 65 6e  RROR_MAX 100.#en
23b3b 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
23b3c 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
23b3d 54 59 5f 43 48 45 43 4b 0a 20 20 2f 2a 20 50 72  TY_CHECK.  /* Pr
23b3e 61 67 6d 61 20 22 71 75 69 63 6b 5f 63 68 65 63  agma "quick_chec
23b3f 6b 22 20 69 73 20 61 6e 20 65 78 70 65 72 69 6d  k" is an experim
23b40 65 6e 74 61 6c 20 72 65 64 75 63 65 64 20 76 65  ental reduced ve
23b41 72 73 69 6f 6e 20 6f 66 20 0a 20 20 2a 2a 20 69  rsion of .  ** i
23b42 6e 74 65 67 72 69 74 79 5f 63 68 65 63 6b 20 64  ntegrity_check d
23b43 65 73 69 67 6e 65 64 20 74 6f 20 64 65 74 65 63  esigned to detec
23b44 74 20 6d 6f 73 74 20 64 61 74 61 62 61 73 65 20  t most database 
23b45 63 6f 72 72 75 70 74 69 6f 6e 0a 20 20 2a 2a 20  corruption.  ** 
23b46 77 69 74 68 6f 75 74 20 6d 6f 73 74 20 6f 66 20  without most of 
23b47 74 68 65 20 6f 76 65 72 68 65 61 64 20 6f 66 20  the overhead of 
23b48 61 20 66 75 6c 6c 20 69 6e 74 65 67 72 69 74 79  a full integrity
23b49 2d 63 68 65 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69  -check..  */.  i
23b4a 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
23b4b 70 28 7a 4c 65 66 74 2c 20 22 69 6e 74 65 67 72  p(zLeft, "integr
23b4c 69 74 79 5f 63 68 65 63 6b 22 29 3d 3d 30 0a 20  ity_check")==0. 
23b4d 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72 49    || sqlite3StrI
23b4e 43 6d 70 28 7a 4c 65 66 74 2c 20 22 71 75 69 63  Cmp(zLeft, "quic
23b4f 6b 5f 63 68 65 63 6b 22 29 3d 3d 30 20 0a 20 20  k_check")==0 .  
23b50 29 7b 0a 20 20 20 20 69 6e 74 20 69 2c 20 6a 2c  ){.    int i, j,
23b51 20 61 64 64 72 2c 20 6d 78 45 72 72 3b 0a 0a 20   addr, mxErr;.. 
23b52 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 61 74 20     /* Code that 
23b53 61 70 70 65 61 72 73 20 61 74 20 74 68 65 20 65  appears at the e
23b54 6e 64 20 6f 66 20 74 68 65 20 69 6e 74 65 67 72  nd of the integr
23b55 69 74 79 20 63 68 65 63 6b 2e 20 20 49 66 20 6e  ity check.  If n
23b56 6f 20 65 72 72 6f 72 0a 20 20 20 20 2a 2a 20 6d  o error.    ** m
23b57 65 73 73 61 67 65 73 20 68 61 76 65 20 62 65 65  essages have bee
23b58 6e 20 67 65 6e 65 72 61 74 65 64 2c 20 6f 75 74  n generated, out
23b59 70 75 74 20 4f 4b 2e 20 20 4f 74 68 65 72 77 69  put OK.  Otherwi
23b5a 73 65 20 6f 75 74 70 75 74 20 74 68 65 0a 20 20  se output the.  
23b5b 20 20 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61    ** error messa
23b5c 67 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 74  ge.    */.    st
23b5d 61 74 69 63 20 63 6f 6e 73 74 20 56 64 62 65 4f  atic const VdbeO
23b5e 70 4c 69 73 74 20 65 6e 64 43 6f 64 65 5b 5d 20  pList endCode[] 
23b5f 3d 20 7b 0a 20 20 20 20 20 20 7b 20 4f 50 5f 41  = {.      { OP_A
23b60 64 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c 20 30  ddImm,      1, 0
23b61 2c 20 20 20 20 20 20 20 20 30 7d 2c 20 20 20 20  ,        0},    
23b62 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 20 20 7b 20  /* 0 */.      { 
23b63 4f 50 5f 49 66 4e 65 67 2c 20 20 20 20 20 20 20  OP_IfNeg,       
23b64 31 2c 20 30 2c 20 20 20 20 20 20 20 20 30 7d 2c  1, 0,        0},
23b65 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20      /* 1 */.    
23b66 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20    { OP_String8, 
23b67 20 20 20 20 30 2c 20 33 2c 20 20 20 20 20 20 20      0, 3,       
23b68 20 30 7d 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a   0},    /* 2 */.
23b69 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 73 75 6c        { OP_Resul
23b6a 74 52 6f 77 2c 20 20 20 33 2c 20 31 2c 20 20 20  tRow,   3, 1,   
23b6b 20 20 20 20 20 30 7d 2c 0a 20 20 20 20 7d 3b 0a       0},.    };.
23b6c 0a 20 20 20 20 69 6e 74 20 69 73 51 75 69 63 6b  .    int isQuick
23b6d 20 3d 20 28 7a 4c 65 66 74 5b 30 5d 3d 3d 27 71   = (zLeft[0]=='q
23b6e 27 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74  ');..    /* Init
23b6f 69 61 6c 69 7a 65 20 74 68 65 20 56 44 42 45 20  ialize the VDBE 
23b70 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 20 20 69  program */.    i
23b71 66 28 20 73 71 6c 69 74 65 33 52 65 61 64 53 63  f( sqlite3ReadSc
23b72 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 20 67  hema(pParse) ) g
23b73 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75 74 3b 0a  oto pragma_out;.
23b74 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
23b75 20 3d 20 36 3b 0a 20 20 20 20 73 71 6c 69 74 65   = 6;.    sqlite
23b76 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28  3VdbeSetNumCols(
23b77 76 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c 69 74  v, 1);.    sqlit
23b78 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
23b79 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  (v, 0, COLNAME_N
23b7a 41 4d 45 2c 20 22 69 6e 74 65 67 72 69 74 79 5f  AME, "integrity_
23b7b 63 68 65 63 6b 22 2c 20 53 51 4c 49 54 45 5f 53  check", SQLITE_S
23b7c 54 41 54 49 43 29 3b 0a 0a 20 20 20 20 2f 2a 20  TATIC);..    /* 
23b7d 53 65 74 20 74 68 65 20 6d 61 78 69 6d 75 6d 20  Set the maximum 
23b7e 65 72 72 6f 72 20 63 6f 75 6e 74 20 2a 2f 0a 20  error count */. 
23b7f 20 20 20 6d 78 45 72 72 20 3d 20 53 51 4c 49 54     mxErr = SQLIT
23b80 45 5f 49 4e 54 45 47 52 49 54 59 5f 43 48 45 43  E_INTEGRITY_CHEC
23b81 4b 5f 45 52 52 4f 52 5f 4d 41 58 3b 0a 20 20 20  K_ERROR_MAX;.   
23b82 20 69 66 28 20 7a 52 69 67 68 74 20 29 7b 0a 20   if( zRight ){. 
23b83 20 20 20 20 20 6d 78 45 72 72 20 3d 20 61 74 6f       mxErr = ato
23b84 69 28 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20  i(zRight);.     
23b85 20 69 66 28 20 6d 78 45 72 72 3c 3d 30 20 29 7b   if( mxErr<=0 ){
23b86 0a 20 20 20 20 20 20 20 20 6d 78 45 72 72 20 3d  .        mxErr =
23b87 20 53 51 4c 49 54 45 5f 49 4e 54 45 47 52 49 54   SQLITE_INTEGRIT
23b88 59 5f 43 48 45 43 4b 5f 45 52 52 4f 52 5f 4d 41  Y_CHECK_ERROR_MA
23b89 58 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  X;.      }.    }
23b8a 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
23b8b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74  AddOp2(v, OP_Int
23b8c 65 67 65 72 2c 20 6d 78 45 72 72 2c 20 31 29 3b  eger, mxErr, 1);
23b8d 20 20 2f 2a 20 72 65 67 5b 31 5d 20 68 6f 6c 64    /* reg[1] hold
23b8e 73 20 65 72 72 6f 72 73 20 6c 65 66 74 20 2a 2f  s errors left */
23b8f 0a 0a 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20 69  ..    /* Do an i
23b90 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 20 6f  ntegrity check o
23b91 6e 20 65 61 63 68 20 64 61 74 61 62 61 73 65 20  n each database 
23b92 66 69 6c 65 20 2a 2f 0a 20 20 20 20 66 6f 72 28  file */.    for(
23b93 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
23b94 69 2b 2b 29 7b 0a 20 20 20 20 20 20 48 61 73 68  i++){.      Hash
23b95 45 6c 65 6d 20 2a 78 3b 0a 20 20 20 20 20 20 48  Elem *x;.      H
23b96 61 73 68 20 2a 70 54 62 6c 73 3b 0a 20 20 20 20  ash *pTbls;.    
23b97 20 20 69 6e 74 20 63 6e 74 20 3d 20 30 3b 0a 0a    int cnt = 0;..
23b98 20 20 20 20 20 20 69 66 28 20 4f 4d 49 54 5f 54        if( OMIT_T
23b99 45 4d 50 44 42 20 26 26 20 69 3d 3d 31 20 29 20  EMPDB && i==1 ) 
23b9a 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20  continue;..     
23b9b 20 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69   sqlite3CodeVeri
23b9c 66 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c  fySchema(pParse,
23b9d 20 69 29 3b 0a 20 20 20 20 20 20 61 64 64 72 20   i);.      addr 
23b9e 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
23b9f 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c  Op1(v, OP_IfPos,
23ba0 20 31 29 3b 20 2f 2a 20 48 61 6c 74 20 69 66 20   1); /* Halt if 
23ba1 6f 75 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f  out of errors */
23ba2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23ba3 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
23ba4 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  alt, 0, 0);.    
23ba5 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
23ba6 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
23ba7 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 61 6e 20  .      /* Do an 
23ba8 69 6e 74 65 67 72 69 74 79 20 63 68 65 63 6b 20  integrity check 
23ba9 6f 66 20 74 68 65 20 42 2d 54 72 65 65 0a 20 20  of the B-Tree.  
23baa 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
23bab 42 65 67 69 6e 20 62 79 20 66 69 6c 6c 69 6e 67  Begin by filling
23bac 20 72 65 67 69 73 74 65 72 73 20 32 2c 20 33 2c   registers 2, 3,
23bad 20 2e 2e 2e 20 77 69 74 68 20 74 68 65 20 72 6f   ... with the ro
23bae 6f 74 20 70 61 67 65 73 20 6e 75 6d 62 65 72 73  ot pages numbers
23baf 0a 20 20 20 20 20 20 2a 2a 20 66 6f 72 20 61 6c  .      ** for al
23bb0 6c 20 74 61 62 6c 65 73 20 61 6e 64 20 69 6e 64  l tables and ind
23bb1 69 63 65 73 20 69 6e 20 74 68 65 20 64 61 74 61  ices in the data
23bb2 62 61 73 65 2e 0a 20 20 20 20 20 20 2a 2f 0a 20  base..      */. 
23bb3 20 20 20 20 20 70 54 62 6c 73 20 3d 20 26 64 62       pTbls = &db
23bb4 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65 6d 61  ->aDb[i].pSchema
23bb5 2d 3e 74 62 6c 48 61 73 68 3b 0a 20 20 20 20 20  ->tblHash;.     
23bb6 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73   for(x=sqliteHas
23bb7 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78  hFirst(pTbls); x
23bb8 3b 20 78 3d 73 71 6c 69 74 65 48 61 73 68 4e 65  ; x=sqliteHashNe
23bb9 78 74 28 78 29 29 7b 0a 20 20 20 20 20 20 20 20  xt(x)){.        
23bba 54 61 62 6c 65 20 2a 70 54 61 62 20 3d 20 73 71  Table *pTab = sq
23bbb 6c 69 74 65 48 61 73 68 44 61 74 61 28 78 29 3b  liteHashData(x);
23bbc 0a 20 20 20 20 20 20 20 20 49 6e 64 65 78 20 2a  .        Index *
23bbd 70 49 64 78 3b 0a 20 20 20 20 20 20 20 20 73 71  pIdx;.        sq
23bbe 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
23bbf 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70  v, OP_Integer, p
23bc0 54 61 62 2d 3e 74 6e 75 6d 2c 20 32 2b 63 6e 74  Tab->tnum, 2+cnt
23bc1 29 3b 0a 20 20 20 20 20 20 20 20 63 6e 74 2b 2b  );.        cnt++
23bc2 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 70 49  ;.        for(pI
23bc3 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b  dx=pTab->pIndex;
23bc4 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78   pIdx; pIdx=pIdx
23bc5 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20 20 20  ->pNext){.      
23bc6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23bc7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
23bc8 67 65 72 2c 20 70 49 64 78 2d 3e 74 6e 75 6d 2c  ger, pIdx->tnum,
23bc9 20 32 2b 63 6e 74 29 3b 0a 20 20 20 20 20 20 20   2+cnt);.       
23bca 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 20 20     cnt++;.      
23bcb 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20    }.      }.    
23bcc 20 20 69 66 28 20 63 6e 74 3d 3d 30 20 29 20 63    if( cnt==0 ) c
23bcd 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20 20 20  ontinue;..      
23bce 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 73 75 66  /* Make sure suf
23bcf 66 69 63 69 65 6e 74 20 6e 75 6d 62 65 72 20 6f  ficient number o
23bd0 66 20 72 65 67 69 73 74 65 72 73 20 68 61 76 65  f registers have
23bd1 20 62 65 65 6e 20 61 6c 6c 6f 63 61 74 65 64 20   been allocated 
23bd2 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 50 61  */.      if( pPa
23bd3 72 73 65 2d 3e 6e 4d 65 6d 20 3c 20 63 6e 74 2b  rse->nMem < cnt+
23bd4 34 20 29 7b 0a 20 20 20 20 20 20 20 20 70 50 61  4 ){.        pPa
23bd5 72 73 65 2d 3e 6e 4d 65 6d 20 3d 20 63 6e 74 2b  rse->nMem = cnt+
23bd6 34 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  4;.      }..    
23bd7 20 20 2f 2a 20 44 6f 20 74 68 65 20 62 2d 74 72    /* Do the b-tr
23bd8 65 65 20 69 6e 74 65 67 72 69 74 79 20 63 68 65  ee integrity che
23bd9 63 6b 73 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  cks */.      sql
23bda 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
23bdb 2c 20 4f 50 5f 49 6e 74 65 67 72 69 74 79 43 6b  , OP_IntegrityCk
23bdc 2c 20 32 2c 20 63 6e 74 2c 20 31 29 3b 0a 20 20  , 2, cnt, 1);.  
23bdd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
23bde 68 61 6e 67 65 50 35 28 76 2c 20 69 29 3b 0a 20  hangeP5(v, i);. 
23bdf 20 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69       addr = sqli
23be0 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
23be1 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 32 29 3b 0a   OP_IsNull, 2);.
23be2 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23be3 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74  eAddOp4(v, OP_St
23be4 72 69 6e 67 38 2c 20 30 2c 20 33 2c 20 30 2c 0a  ring8, 0, 3, 0,.
23be5 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
23be6 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 2a 2a 2a  MPrintf(db, "***
23be7 20 69 6e 20 64 61 74 61 62 61 73 65 20 25 73 20   in database %s 
23be8 2a 2a 2a 5c 6e 22 2c 20 64 62 2d 3e 61 44 62 5b  ***\n", db->aDb[
23be9 69 5d 2e 7a 4e 61 6d 65 29 2c 0a 20 20 20 20 20  i].zName),.     
23bea 20 20 20 20 50 34 5f 44 59 4e 41 4d 49 43 29 3b      P4_DYNAMIC);
23beb 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23bec 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d  beAddOp3(v, OP_M
23bed 6f 76 65 2c 20 32 2c 20 34 2c 20 31 29 3b 0a 20  ove, 2, 4, 1);. 
23bee 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23bef 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6e  AddOp3(v, OP_Con
23bf0 63 61 74 2c 20 34 2c 20 33 2c 20 32 29 3b 0a 20  cat, 4, 3, 2);. 
23bf1 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23bf2 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
23bf3 75 6c 74 52 6f 77 2c 20 32 2c 20 31 29 3b 0a 20  ultRow, 2, 1);. 
23bf4 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23bf5 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
23bf6 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4d 61 6b  );..      /* Mak
23bf7 65 20 73 75 72 65 20 61 6c 6c 20 74 68 65 20 69  e sure all the i
23bf8 6e 64 69 63 65 73 20 61 72 65 20 63 6f 6e 73 74  ndices are const
23bf9 72 75 63 74 65 64 20 63 6f 72 72 65 63 74 6c 79  ructed correctly
23bfa 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
23bfb 20 66 6f 72 28 78 3d 73 71 6c 69 74 65 48 61 73   for(x=sqliteHas
23bfc 68 46 69 72 73 74 28 70 54 62 6c 73 29 3b 20 78  hFirst(pTbls); x
23bfd 20 26 26 20 21 69 73 51 75 69 63 6b 3b 20 78 3d   && !isQuick; x=
23bfe 73 71 6c 69 74 65 48 61 73 68 4e 65 78 74 28 78  sqliteHashNext(x
23bff 29 29 7b 0a 20 20 20 20 20 20 20 20 54 61 62 6c  )){.        Tabl
23c00 65 20 2a 70 54 61 62 20 3d 20 73 71 6c 69 74 65  e *pTab = sqlite
23c01 48 61 73 68 44 61 74 61 28 78 29 3b 0a 20 20 20  HashData(x);.   
23c02 20 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64 78       Index *pIdx
23c03 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6c 6f  ;.        int lo
23c04 6f 70 54 6f 70 3b 0a 0a 20 20 20 20 20 20 20 20  opTop;..        
23c05 69 66 28 20 70 54 61 62 2d 3e 70 49 6e 64 65 78  if( pTab->pIndex
23c06 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
23c07 20 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73          addr = s
23c08 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
23c09 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20 31 29  (v, OP_IfPos, 1)
23c0a 3b 20 20 2f 2a 20 53 74 6f 70 20 69 66 20 6f 75  ;  /* Stop if ou
23c0b 74 20 6f 66 20 65 72 72 6f 72 73 20 2a 2f 0a 20  t of errors */. 
23c0c 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23c0d 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 48  beAddOp2(v, OP_H
23c0e 61 6c 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  alt, 0, 0);.    
23c0f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
23c10 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 29  umpHere(v, addr)
23c11 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
23c12 33 4f 70 65 6e 54 61 62 6c 65 41 6e 64 49 6e 64  3OpenTableAndInd
23c13 69 63 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ices(pParse, pTa
23c14 62 2c 20 31 2c 20 4f 50 5f 4f 70 65 6e 52 65 61  b, 1, OP_OpenRea
23c15 64 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  d);.        sqli
23c16 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
23c17 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20   OP_Integer, 0, 
23c18 32 29 3b 20 20 2f 2a 20 72 65 67 28 32 29 20 77  2);  /* reg(2) w
23c19 69 6c 6c 20 63 6f 75 6e 74 20 65 6e 74 72 69 65  ill count entrie
23c1a 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 6c 6f 6f  s */.        loo
23c1b 70 54 6f 70 20 3d 20 73 71 6c 69 74 65 33 56 64  pTop = sqlite3Vd
23c1c 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
23c1d 65 77 69 6e 64 2c 20 31 2c 20 30 29 3b 0a 20 20  ewind, 1, 0);.  
23c1e 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23c1f 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
23c20 64 49 6d 6d 2c 20 32 2c 20 31 29 3b 20 20 20 2f  dImm, 2, 1);   /
23c21 2a 20 69 6e 63 72 65 6d 65 6e 74 20 65 6e 74 72  * increment entr
23c22 79 20 63 6f 75 6e 74 20 2a 2f 0a 20 20 20 20 20  y count */.     
23c23 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78     for(j=0, pIdx
23c24 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
23c25 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
23c26 70 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20  pNext, j++){.   
23c27 20 20 20 20 20 20 20 69 6e 74 20 6a 6d 70 32 3b         int jmp2;
23c28 0a 20 20 20 20 20 20 20 20 20 20 73 74 61 74 69  .          stati
23c29 63 20 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69  c const VdbeOpLi
23c2a 73 74 20 69 64 78 45 72 72 5b 5d 20 3d 20 7b 0a  st idxErr[] = {.
23c2b 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
23c2c 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20 20 31 2c  _AddImm,      1,
23c2d 20 2d 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20   -1,  0},.      
23c2e 20 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e        { OP_Strin
23c2f 67 38 2c 20 20 20 20 20 30 2c 20 20 33 2c 20 20  g8,     0,  3,  
23c30 30 7d 2c 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20  0},    /* 1 */. 
23c31 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
23c32 52 6f 77 69 64 2c 20 20 20 20 20 20 20 31 2c 20  Rowid,       1, 
23c33 20 34 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20   4,  0},.       
23c34 20 20 20 20 20 7b 20 4f 50 5f 53 74 72 69 6e 67       { OP_String
23c35 38 2c 20 20 20 20 20 30 2c 20 20 35 2c 20 20 30  8,     0,  5,  0
23c36 7d 2c 20 20 20 20 2f 2a 20 33 20 2a 2f 0a 20 20  },    /* 3 */.  
23c37 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 53            { OP_S
23c38 74 72 69 6e 67 38 2c 20 20 20 20 20 30 2c 20 20  tring8,     0,  
23c39 36 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 34 20  6,  0},    /* 4 
23c3a 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b  */.            {
23c3b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20 20 20 20 20   OP_Concat,     
23c3c 20 34 2c 20 20 33 2c 20 20 33 7d 2c 0a 20 20 20   4,  3,  3},.   
23c3d 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 43 6f           { OP_Co
23c3e 6e 63 61 74 2c 20 20 20 20 20 20 35 2c 20 20 33  ncat,      5,  3
23c3f 2c 20 20 33 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  3},.         
23c40 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74 2c 20     { OP_Concat, 
23c41 20 20 20 20 20 36 2c 20 20 33 2c 20 20 33 7d 2c       6,  3,  3},
23c42 0a 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f  .            { O
23c43 50 5f 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 33  P_ResultRow,   3
23c44 2c 20 20 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20  ,  1,  0},.     
23c45 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 66 50 6f         { OP_IfPo
23c46 73 2c 20 20 20 20 20 20 20 31 2c 20 20 30 2c 20  s,       1,  0, 
23c47 20 30 7d 2c 20 20 20 20 2f 2a 20 39 20 2a 2f 0a   0},    /* 9 */.
23c48 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50              { OP
23c49 5f 48 61 6c 74 2c 20 20 20 20 20 20 20 20 30 2c  _Halt,        0,
23c4a 20 20 30 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20    0,  0},.      
23c4b 20 20 20 20 7d 3b 0a 20 20 20 20 20 20 20 20 20      };.         
23c4c 20 73 71 6c 69 74 65 33 47 65 6e 65 72 61 74 65   sqlite3Generate
23c4d 49 6e 64 65 78 4b 65 79 28 70 50 61 72 73 65 2c  IndexKey(pParse,
23c4e 20 70 49 64 78 2c 20 31 2c 20 33 2c 20 31 29 3b   pIdx, 1, 3, 1);
23c4f 0a 20 20 20 20 20 20 20 20 20 20 6a 6d 70 32 20  .          jmp2 
23c50 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
23c51 4f 70 33 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c  Op3(v, OP_Found,
23c52 20 6a 2b 32 2c 20 30 2c 20 33 29 3b 0a 20 20 20   j+2, 0, 3);.   
23c53 20 20 20 20 20 20 20 61 64 64 72 20 3d 20 73 71         addr = sq
23c54 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 4c 69  lite3VdbeAddOpLi
23c55 73 74 28 76 2c 20 41 72 72 61 79 53 69 7a 65 28  st(v, ArraySize(
23c56 69 64 78 45 72 72 29 2c 20 69 64 78 45 72 72 29  idxErr), idxErr)
23c57 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  ;.          sqli
23c58 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28  te3VdbeChangeP4(
23c59 76 2c 20 61 64 64 72 2b 31 2c 20 22 72 6f 77 69  v, addr+1, "rowi
23c5a 64 20 22 2c 20 50 34 5f 53 54 41 54 49 43 29 3b  d ", P4_STATIC);
23c5b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23c5c 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
23c5d 2c 20 61 64 64 72 2b 33 2c 20 22 20 6d 69 73 73  , addr+3, " miss
23c5e 69 6e 67 20 66 72 6f 6d 20 69 6e 64 65 78 20 22  ing from index "
23c5f 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
23c60 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
23c61 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61  dbeChangeP4(v, a
23c62 64 64 72 2b 34 2c 20 70 49 64 78 2d 3e 7a 4e 61  ddr+4, pIdx->zNa
23c63 6d 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a  me, P4_STATIC);.
23c64 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
23c65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
23c66 20 61 64 64 72 2b 39 29 3b 0a 20 20 20 20 20 20   addr+9);.      
23c67 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a      sqlite3VdbeJ
23c68 75 6d 70 48 65 72 65 28 76 2c 20 6a 6d 70 32 29  umpHere(v, jmp2)
23c69 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
23c6a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23c6b 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74  ddOp2(v, OP_Next
23c6c 2c 20 31 2c 20 6c 6f 6f 70 54 6f 70 2b 31 29 3b  , 1, loopTop+1);
23c6d 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
23c6e 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20  VdbeJumpHere(v, 
23c6f 6c 6f 6f 70 54 6f 70 29 3b 0a 20 20 20 20 20 20  loopTop);.      
23c70 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d    for(j=0, pIdx=
23c71 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
23c72 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
23c73 4e 65 78 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20  Next, j++){.    
23c74 20 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e        static con
23c75 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 63 6e  st VdbeOpList cn
23c76 74 49 64 78 5b 5d 20 3d 20 7b 0a 20 20 20 20 20  tIdx[] = {.     
23c77 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 49 6e 74          { OP_Int
23c78 65 67 65 72 2c 20 20 20 20 20 20 30 2c 20 20 33  eger,      0,  3
23c79 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20 20  ,  0},.         
23c7a 20 20 20 20 7b 20 4f 50 5f 52 65 77 69 6e 64 2c      { OP_Rewind,
23c7b 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 30         0,  0,  0
23c7c 7d 2c 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20  },  /* 1 */.    
23c7d 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 41 64           { OP_Ad
23c7e 64 49 6d 6d 2c 20 20 20 20 20 20 20 33 2c 20 20  dImm,       3,  
23c7f 31 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 20 20  1,  0},.        
23c80 20 20 20 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20       { OP_Next, 
23c81 20 20 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20          0,  0,  
23c82 30 7d 2c 20 20 2f 2a 20 33 20 2a 2f 0a 20 20 20  0},  /* 3 */.   
23c83 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 45            { OP_E
23c84 71 2c 20 20 20 20 20 20 20 20 20 20 20 32 2c 20  q,           2, 
23c85 20 30 2c 20 20 33 7d 2c 20 20 2f 2a 20 34 20 2a   0,  3},  /* 4 *
23c86 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 7b  /.             {
23c87 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 20 20 20 20   OP_AddImm,     
23c88 20 20 31 2c 20 2d 31 2c 20 20 30 7d 2c 0a 20 20    1, -1,  0},.  
23c89 20 20 20 20 20 20 20 20 20 20 20 7b 20 4f 50 5f             { OP_
23c8a 53 74 72 69 6e 67 38 2c 20 20 20 20 20 20 30 2c  String8,      0,
23c8b 20 20 32 2c 20 20 30 7d 2c 20 20 2f 2a 20 36 20    2,  0},  /* 6 
23c8c 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
23c8d 7b 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20  { OP_String8,   
23c8e 20 20 20 30 2c 20 20 33 2c 20 20 30 7d 2c 20 20     0,  3,  0},  
23c8f 2f 2a 20 37 20 2a 2f 0a 20 20 20 20 20 20 20 20  /* 7 */.        
23c90 20 20 20 20 20 7b 20 4f 50 5f 43 6f 6e 63 61 74       { OP_Concat
23c91 2c 20 20 20 20 20 20 20 33 2c 20 20 32 2c 20 20  ,       3,  2,  
23c92 32 7d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  2},.            
23c93 20 7b 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c   { OP_ResultRow,
23c94 20 20 20 20 32 2c 20 20 31 2c 20 20 30 7d 2c 0a      2,  1,  0},.
23c95 20 20 20 20 20 20 20 20 20 20 7d 3b 0a 20 20 20            };.   
23c96 20 20 20 20 20 20 20 69 66 28 20 70 49 64 78 2d         if( pIdx-
23c97 3e 74 6e 75 6d 3d 3d 30 20 29 20 63 6f 6e 74 69  >tnum==0 ) conti
23c98 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 20 20 61  nue;.          a
23c99 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
23c9a 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66  eAddOp1(v, OP_If
23c9b 50 6f 73 2c 20 31 29 3b 0a 20 20 20 20 20 20 20  Pos, 1);.       
23c9c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
23c9d 64 4f 70 32 28 76 2c 20 4f 50 5f 48 61 6c 74 2c  dOp2(v, OP_Halt,
23c9e 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20   0, 0);.        
23c9f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
23ca0 70 48 65 72 65 28 76 2c 20 61 64 64 72 29 3b 0a  pHere(v, addr);.
23ca1 20 20 20 20 20 20 20 20 20 20 61 64 64 72 20 3d            addr =
23ca2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23ca3 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53 69  pList(v, ArraySi
23ca4 7a 65 28 63 6e 74 49 64 78 29 2c 20 63 6e 74 49  ze(cntIdx), cntI
23ca5 64 78 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  dx);.          s
23ca6 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23ca7 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 6a 2b  P1(v, addr+1, j+
23ca8 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71  2);.          sq
23ca9 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
23caa 32 28 76 2c 20 61 64 64 72 2b 31 2c 20 61 64 64  2(v, addr+1, add
23cab 72 2b 34 29 3b 0a 20 20 20 20 20 20 20 20 20 20  r+4);.          
23cac 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
23cad 65 50 31 28 76 2c 20 61 64 64 72 2b 33 2c 20 6a  eP1(v, addr+3, j
23cae 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  +2);.          s
23caf 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23cb0 50 32 28 76 2c 20 61 64 64 72 2b 33 2c 20 61 64  P2(v, addr+3, ad
23cb1 64 72 2b 32 29 3b 0a 20 20 20 20 20 20 20 20 20  dr+2);.         
23cb2 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d 70   sqlite3VdbeJump
23cb3 48 65 72 65 28 76 2c 20 61 64 64 72 2b 34 29 3b  Here(v, addr+4);
23cb4 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
23cb5 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
23cb6 2c 20 61 64 64 72 2b 36 2c 20 0a 20 20 20 20 20  , addr+6, .     
23cb7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23cb8 22 77 72 6f 6e 67 20 23 20 6f 66 20 65 6e 74 72  "wrong # of entr
23cb9 69 65 73 20 69 6e 20 69 6e 64 65 78 20 22 2c 20  ies in index ", 
23cba 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
23cbb 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
23cbc 65 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64  eChangeP4(v, add
23cbd 72 2b 37 2c 20 70 49 64 78 2d 3e 7a 4e 61 6d 65  r+7, pIdx->zName
23cbe 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20  , P4_STATIC);.  
23cbf 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 20        }.      } 
23cc0 0a 20 20 20 20 7d 0a 20 20 20 20 61 64 64 72 20  .    }.    addr 
23cc1 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
23cc2 4f 70 4c 69 73 74 28 76 2c 20 41 72 72 61 79 53  OpList(v, ArrayS
23cc3 69 7a 65 28 65 6e 64 43 6f 64 65 29 2c 20 65 6e  ize(endCode), en
23cc4 64 43 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69  dCode);.    sqli
23cc5 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
23cc6 76 2c 20 61 64 64 72 2c 20 2d 6d 78 45 72 72 29  v, addr, -mxErr)
23cc7 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
23cc8 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64  eJumpHere(v, add
23cc9 72 2b 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  r+1);.    sqlite
23cca 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76 2c  3VdbeChangeP4(v,
23ccb 20 61 64 64 72 2b 32 2c 20 22 6f 6b 22 2c 20 50   addr+2, "ok", P
23ccc 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 7d 65 6c  4_STATIC);.  }el
23ccd 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
23cce 49 54 45 5f 4f 4d 49 54 5f 49 4e 54 45 47 52 49  ITE_OMIT_INTEGRI
23ccf 54 59 5f 43 48 45 43 4b 20 2a 2f 0a 0a 23 69 66  TY_CHECK */..#if
23cd0 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
23cd1 5f 55 54 46 31 36 0a 20 20 2f 2a 0a 20 20 2a 2a  _UTF16.  /*.  **
23cd2 20 20 20 50 52 41 47 4d 41 20 65 6e 63 6f 64 69     PRAGMA encodi
23cd3 6e 67 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41  ng.  **   PRAGMA
23cd4 20 65 6e 63 6f 64 69 6e 67 20 3d 20 22 75 74 66   encoding = "utf
23cd5 2d 38 22 7c 22 75 74 66 2d 31 36 22 7c 22 75 74  -8"|"utf-16"|"ut
23cd6 66 2d 31 36 6c 65 22 7c 22 75 74 66 2d 31 36 62  f-16le"|"utf-16b
23cd7 65 22 0a 20 20 2a 2a 0a 20 20 2a 2a 20 49 6e 20  e".  **.  ** In 
23cd8 69 74 73 20 66 69 72 73 74 20 66 6f 72 6d 2c 20  its first form, 
23cd9 74 68 69 73 20 70 72 61 67 6d 61 20 72 65 74 75  this pragma retu
23cda 72 6e 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67  rns the encoding
23cdb 20 6f 66 20 74 68 65 20 6d 61 69 6e 0a 20 20 2a   of the main.  *
23cdc 2a 20 64 61 74 61 62 61 73 65 2e 20 49 66 20 74  * database. If t
23cdd 68 65 20 64 61 74 61 62 61 73 65 20 69 73 20 6e  he database is n
23cde 6f 74 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20  ot initialized, 
23cdf 69 74 20 69 73 20 69 6e 69 74 69 61 6c 69 7a 65  it is initialize
23ce0 64 20 6e 6f 77 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  d now..  **.  **
23ce1 20 54 68 65 20 73 65 63 6f 6e 64 20 66 6f 72 6d   The second form
23ce2 20 6f 66 20 74 68 69 73 20 70 72 61 67 6d 61 20   of this pragma 
23ce3 69 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68  is a no-op if th
23ce4 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
23ce5 66 69 6c 65 0a 20 20 2a 2a 20 68 61 73 20 6e 6f  file.  ** has no
23ce6 74 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 69  t already been i
23ce7 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49 6e 20 74  nitialized. In t
23ce8 68 69 73 20 63 61 73 65 20 69 74 20 73 65 74 73  his case it sets
23ce9 20 74 68 65 20 64 65 66 61 75 6c 74 0a 20 20 2a   the default.  *
23cea 2a 20 65 6e 63 6f 64 69 6e 67 20 74 68 61 74 20  * encoding that 
23ceb 77 69 6c 6c 20 62 65 20 75 73 65 64 20 66 6f 72  will be used for
23cec 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
23ced 73 65 20 66 69 6c 65 20 69 66 20 61 20 6e 65 77  se file if a new
23cee 20 66 69 6c 65 0a 20 20 2a 2a 20 69 73 20 63 72   file.  ** is cr
23cef 65 61 74 65 64 2e 20 49 66 20 61 6e 20 65 78 69  eated. If an exi
23cf0 73 74 69 6e 67 20 6d 61 69 6e 20 64 61 74 61 62  sting main datab
23cf1 61 73 65 20 66 69 6c 65 20 69 73 20 6f 70 65 6e  ase file is open
23cf2 65 64 2c 20 74 68 65 6e 20 74 68 65 0a 20 20 2a  ed, then the.  *
23cf3 2a 20 64 65 66 61 75 6c 74 20 74 65 78 74 20 65  * default text e
23cf4 6e 63 6f 64 69 6e 67 20 66 6f 72 20 74 68 65 20  ncoding for the 
23cf5 65 78 69 73 74 69 6e 67 20 64 61 74 61 62 61 73  existing databas
23cf6 65 20 69 73 20 75 73 65 64 2e 0a 20 20 2a 2a 20  e is used..  ** 
23cf7 0a 20 20 2a 2a 20 49 6e 20 61 6c 6c 20 63 61 73  .  ** In all cas
23cf8 65 73 20 6e 65 77 20 64 61 74 61 62 61 73 65 73  es new databases
23cf9 20 63 72 65 61 74 65 64 20 75 73 69 6e 67 20 74   created using t
23cfa 68 65 20 41 54 54 41 43 48 20 63 6f 6d 6d 61 6e  he ATTACH comman
23cfb 64 20 61 72 65 0a 20 20 2a 2a 20 63 72 65 61 74  d are.  ** creat
23cfc 65 64 20 74 6f 20 75 73 65 20 74 68 65 20 73 61  ed to use the sa
23cfd 6d 65 20 64 65 66 61 75 6c 74 20 74 65 78 74 20  me default text 
23cfe 65 6e 63 6f 64 69 6e 67 20 61 73 20 74 68 65 20  encoding as the 
23cff 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2e 20 49  main database. I
23d00 66 0a 20 20 2a 2a 20 74 68 65 20 6d 61 69 6e 20  f.  ** the main 
23d01 64 61 74 61 62 61 73 65 20 68 61 73 20 6e 6f 74  database has not
23d02 20 62 65 65 6e 20 69 6e 69 74 69 61 6c 69 7a 65   been initialize
23d03 64 20 61 6e 64 2f 6f 72 20 63 72 65 61 74 65 64  d and/or created
23d04 20 77 68 65 6e 20 41 54 54 41 43 48 0a 20 20 2a   when ATTACH.  *
23d05 2a 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74  * is executed, t
23d06 68 69 73 20 69 73 20 64 6f 6e 65 20 62 65 66 6f  his is done befo
23d07 72 65 20 74 68 65 20 41 54 54 41 43 48 20 6f 70  re the ATTACH op
23d08 65 72 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20  eration..  **.  
23d09 2a 2a 20 49 6e 20 74 68 65 20 73 65 63 6f 6e 64  ** In the second
23d0a 20 66 6f 72 6d 20 74 68 69 73 20 70 72 61 67 6d   form this pragm
23d0b 61 20 73 65 74 73 20 74 68 65 20 74 65 78 74 20  a sets the text 
23d0c 65 6e 63 6f 64 69 6e 67 20 74 6f 20 62 65 20 75  encoding to be u
23d0d 73 65 64 20 69 6e 0a 20 20 2a 2a 20 6e 65 77 20  sed in.  ** new 
23d0e 64 61 74 61 62 61 73 65 20 66 69 6c 65 73 20 63  database files c
23d0f 72 65 61 74 65 64 20 75 73 69 6e 67 20 74 68 69  reated using thi
23d10 73 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  s database handl
23d11 65 2e 20 49 74 20 69 73 20 6f 6e 6c 79 0a 20 20  e. It is only.  
23d12 2a 2a 20 75 73 65 66 75 6c 20 69 66 20 69 6e 76  ** useful if inv
23d13 6f 6b 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  oked immediately
23d14 20 61 66 74 65 72 20 74 68 65 20 6d 61 69 6e 20   after the main 
23d15 64 61 74 61 62 61 73 65 20 69 0a 20 20 2a 2f 0a  database i.  */.
23d16 20 20 69 66 28 20 73 71 6c 69 74 65 33 53 74 72    if( sqlite3Str
23d17 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 65 6e 63  ICmp(zLeft, "enc
23d18 6f 64 69 6e 67 22 29 3d 3d 30 20 29 7b 0a 20 20  oding")==0 ){.  
23d19 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73    static const s
23d1a 74 72 75 63 74 20 45 6e 63 4e 61 6d 65 20 7b 0a  truct EncName {.
23d1b 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d        char *zNam
23d1c 65 3b 0a 20 20 20 20 20 20 75 38 20 65 6e 63 3b  e;.      u8 enc;
23d1d 0a 20 20 20 20 7d 20 65 6e 63 6e 61 6d 65 73 5b  .    } encnames[
23d1e 5d 20 3d 20 7b 0a 20 20 20 20 20 20 7b 20 22 55  ] = {.      { "U
23d1f 54 46 2d 38 22 2c 20 20 20 20 53 51 4c 49 54 45  TF-8",    SQLITE
23d20 5f 55 54 46 38 20 20 20 20 20 20 20 20 7d 2c 0a  _UTF8        },.
23d21 20 20 20 20 20 20 7b 20 22 55 54 46 38 22 2c 20        { "UTF8", 
23d22 20 20 20 20 53 51 4c 49 54 45 5f 55 54 46 38 20      SQLITE_UTF8 
23d23 20 20 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20         },.      
23d24 7b 20 22 55 54 46 2d 31 36 6c 65 22 2c 20 53 51  { "UTF-16le", SQ
23d25 4c 49 54 45 5f 55 54 46 31 36 4c 45 20 20 20 20  LITE_UTF16LE    
23d26 20 7d 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46   },.      { "UTF
23d27 31 36 6c 65 22 2c 20 20 53 51 4c 49 54 45 5f 55  16le",  SQLITE_U
23d28 54 46 31 36 4c 45 20 20 20 20 20 7d 2c 0a 20 20  TF16LE     },.  
23d29 20 20 20 20 7b 20 22 55 54 46 2d 31 36 62 65 22      { "UTF-16be"
23d2a 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 42 45  , SQLITE_UTF16BE
23d2b 20 20 20 20 20 7d 2c 0a 20 20 20 20 20 20 7b 20       },.      { 
23d2c 22 55 54 46 31 36 62 65 22 2c 20 20 53 51 4c 49  "UTF16be",  SQLI
23d2d 54 45 5f 55 54 46 31 36 42 45 20 20 20 20 20 7d  TE_UTF16BE     }
23d2e 2c 0a 20 20 20 20 20 20 7b 20 22 55 54 46 2d 31  ,.      { "UTF-1
23d2f 36 22 2c 20 20 20 30 20 20 20 20 20 20 20 20 20  6",   0         
23d30 20 20 20 20 20 20 20 20 20 7d 2c 20 2f 2a 20 53           }, /* S
23d31 51 4c 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56  QLITE_UTF16NATIV
23d32 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20 22 55 54  E */.      { "UT
23d33 46 31 36 22 2c 20 20 20 20 30 20 20 20 20 20 20  F16",    0      
23d34 20 20 20 20 20 20 20 20 20 20 20 20 7d 2c 20 2f              }, /
23d35 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4e 41  * SQLITE_UTF16NA
23d36 54 49 56 45 20 2a 2f 0a 20 20 20 20 20 20 7b 20  TIVE */.      { 
23d37 30 2c 20 30 20 7d 0a 20 20 20 20 7d 3b 0a 20 20  0, 0 }.    };.  
23d38 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20 45    const struct E
23d39 6e 63 4e 61 6d 65 20 2a 70 45 6e 63 3b 0a 20 20  ncName *pEnc;.  
23d3a 20 20 69 66 28 20 21 7a 52 69 67 68 74 20 29 7b    if( !zRight ){
23d3b 20 20 20 20 2f 2a 20 22 50 52 41 47 4d 41 20 65      /* "PRAGMA e
23d3c 6e 63 6f 64 69 6e 67 22 20 2a 2f 0a 20 20 20 20  ncoding" */.    
23d3d 20 20 69 66 28 20 73 71 6c 69 74 65 33 52 65 61    if( sqlite3Rea
23d3e 64 53 63 68 65 6d 61 28 70 50 61 72 73 65 29 20  dSchema(pParse) 
23d3f 29 20 67 6f 74 6f 20 70 72 61 67 6d 61 5f 6f 75  ) goto pragma_ou
23d40 74 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  t;.      sqlite3
23d41 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76  VdbeSetNumCols(v
23d42 2c 20 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 1);.      sqli
23d43 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d  te3VdbeSetColNam
23d44 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f  e(v, 0, COLNAME_
23d45 4e 41 4d 45 2c 20 22 65 6e 63 6f 64 69 6e 67 22  NAME, "encoding"
23d46 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
23d47 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
23d48 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
23d49 53 74 72 69 6e 67 38 2c 20 30 2c 20 31 29 3b 0a  String8, 0, 1);.
23d4a 20 20 20 20 20 20 66 6f 72 28 70 45 6e 63 3d 26        for(pEnc=&
23d4b 65 6e 63 6e 61 6d 65 73 5b 30 5d 3b 20 70 45 6e  encnames[0]; pEn
23d4c 63 2d 3e 7a 4e 61 6d 65 3b 20 70 45 6e 63 2b 2b  c->zName; pEnc++
23d4d 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
23d4e 45 6e 63 2d 3e 65 6e 63 3d 3d 45 4e 43 28 70 50  Enc->enc==ENC(pP
23d4f 61 72 73 65 2d 3e 64 62 29 20 29 7b 0a 20 20 20  arse->db) ){.   
23d50 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
23d51 62 65 43 68 61 6e 67 65 50 34 28 76 2c 20 2d 31  beChangeP4(v, -1
23d52 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 2c 20 50  , pEnc->zName, P
23d53 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  4_STATIC);.     
23d54 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
23d55 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
23d56 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23d57 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
23d58 6c 74 52 6f 77 2c 20 31 2c 20 31 29 3b 0a 20 20  ltRow, 1, 1);.  
23d59 20 20 7d 65 6c 73 65 7b 20 20 20 20 20 20 20 20    }else{        
23d5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23d5b 2f 2a 20 22 50 52 41 47 4d 41 20 65 6e 63 6f 64  /* "PRAGMA encod
23d5c 69 6e 67 20 3d 20 58 58 58 22 20 2a 2f 0a 20 20  ing = XXX" */.  
23d5d 20 20 20 20 2f 2a 20 4f 6e 6c 79 20 63 68 61 6e      /* Only chan
23d5e 67 65 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  ge the value of 
23d5f 73 71 6c 69 74 65 2e 65 6e 63 20 69 66 20 74 68  sqlite.enc if th
23d60 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c  e database handl
23d61 65 20 69 73 20 6e 6f 74 0a 20 20 20 20 20 20 2a  e is not.      *
23d62 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20 49  * initialized. I
23d63 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
23d64 61 73 65 20 65 78 69 73 74 73 2c 20 74 68 65 20  ase exists, the 
23d65 6e 65 77 20 73 71 6c 69 74 65 2e 65 6e 63 20 76  new sqlite.enc v
23d66 61 6c 75 65 0a 20 20 20 20 20 20 2a 2a 20 77 69  alue.      ** wi
23d67 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74 65  ll be overwritte
23d68 6e 20 77 68 65 6e 20 74 68 65 20 73 63 68 65 6d  n when the schem
23d69 61 20 69 73 20 6e 65 78 74 20 6c 6f 61 64 65 64  a is next loaded
23d6a 2e 20 49 66 20 69 74 20 64 6f 65 73 20 6e 6f 74  . If it does not
23d6b 0a 20 20 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  .      ** alread
23d6c 79 20 65 78 69 73 74 73 2c 20 69 74 20 77 69 6c  y exists, it wil
23d6d 6c 20 62 65 20 63 72 65 61 74 65 64 20 74 6f 20  l be created to 
23d6e 75 73 65 20 74 68 65 20 6e 65 77 20 65 6e 63 6f  use the new enco
23d6f 64 69 6e 67 20 76 61 6c 75 65 2e 0a 20 20 20 20  ding value..    
23d70 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 0a    */.      if( .
23d71 20 20 20 20 20 20 20 20 21 28 44 62 48 61 73 50          !(DbHasP
23d72 72 6f 70 65 72 74 79 28 64 62 2c 20 30 2c 20 44  roperty(db, 0, D
23d73 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29 29  B_SchemaLoaded))
23d74 20 7c 7c 20 0a 20 20 20 20 20 20 20 20 44 62 48   || .        DbH
23d75 61 73 50 72 6f 70 65 72 74 79 28 64 62 2c 20 30  asProperty(db, 0
23d76 2c 20 44 42 5f 45 6d 70 74 79 29 20 0a 20 20 20  , DB_Empty) .   
23d77 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f     ){.        fo
23d78 72 28 70 45 6e 63 3d 26 65 6e 63 6e 61 6d 65 73  r(pEnc=&encnames
23d79 5b 30 5d 3b 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65  [0]; pEnc->zName
23d7a 3b 20 70 45 6e 63 2b 2b 29 7b 0a 20 20 20 20 20  ; pEnc++){.     
23d7b 20 20 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69       if( 0==sqli
23d7c 74 65 33 53 74 72 49 43 6d 70 28 7a 52 69 67 68  te3StrICmp(zRigh
23d7d 74 2c 20 70 45 6e 63 2d 3e 7a 4e 61 6d 65 29 20  t, pEnc->zName) 
23d7e 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 45  ){.            E
23d7f 4e 43 28 70 50 61 72 73 65 2d 3e 64 62 29 20 3d  NC(pParse->db) =
23d80 20 70 45 6e 63 2d 3e 65 6e 63 20 3f 20 70 45 6e   pEnc->enc ? pEn
23d81 63 2d 3e 65 6e 63 20 3a 20 53 51 4c 49 54 45 5f  c->enc : SQLITE_
23d82 55 54 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20  UTF16NATIVE;.   
23d83 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
23d84 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
23d85 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66      }.        if
23d86 28 20 21 70 45 6e 63 2d 3e 7a 4e 61 6d 65 20 29  ( !pEnc->zName )
23d87 7b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69  {.          sqli
23d88 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
23d89 73 65 2c 20 22 75 6e 73 75 70 70 6f 72 74 65 64  se, "unsupported
23d8a 20 65 6e 63 6f 64 69 6e 67 3a 20 25 73 22 2c 20   encoding: %s", 
23d8b 7a 52 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20  zRight);.       
23d8c 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
23d8d 0a 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20  .  }else.#endif 
23d8e 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55  /* SQLITE_OMIT_U
23d8f 54 46 31 36 20 2a 2f 0a 0a 23 69 66 6e 64 65 66  TF16 */..#ifndef
23d90 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 43 48   SQLITE_OMIT_SCH
23d91 45 4d 41 5f 56 45 52 53 49 4f 4e 5f 50 52 41 47  EMA_VERSION_PRAG
23d92 4d 41 53 0a 20 20 2f 2a 0a 20 20 2a 2a 20 20 20  MAS.  /*.  **   
23d93 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73 65  PRAGMA [database
23d94 2e 5d 73 63 68 65 6d 61 5f 76 65 72 73 69 6f 6e  .]schema_version
23d95 0a 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b  .  **   PRAGMA [
23d96 64 61 74 61 62 61 73 65 2e 5d 73 63 68 65 6d 61  database.]schema
23d97 5f 76 65 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65  _version = <inte
23d98 67 65 72 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20  ger>.  **.  **  
23d99 20 50 52 41 47 4d 41 20 5b 64 61 74 61 62 61 73   PRAGMA [databas
23d9a 65 2e 5d 75 73 65 72 5f 76 65 72 73 69 6f 6e 0a  e.]user_version.
23d9b 20 20 2a 2a 20 20 20 50 52 41 47 4d 41 20 5b 64    **   PRAGMA [d
23d9c 61 74 61 62 61 73 65 2e 5d 75 73 65 72 5f 76 65  atabase.]user_ve
23d9d 72 73 69 6f 6e 20 3d 20 3c 69 6e 74 65 67 65 72  rsion = <integer
23d9e 3e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  >.  **.  ** The 
23d9f 70 72 61 67 6d 61 27 73 20 73 63 68 65 6d 61 5f  pragma's schema_
23da0 76 65 72 73 69 6f 6e 20 61 6e 64 20 75 73 65 72  version and user
23da1 5f 76 65 72 73 69 6f 6e 20 61 72 65 20 75 73 65  _version are use
23da2 64 20 74 6f 20 73 65 74 20 6f 72 20 67 65 74 0a  d to set or get.
23da3 20 20 2a 2a 20 74 68 65 20 76 61 6c 75 65 20 6f    ** the value o
23da4 66 20 74 68 65 20 73 63 68 65 6d 61 2d 76 65 72  f the schema-ver
23da5 73 69 6f 6e 20 61 6e 64 20 75 73 65 72 2d 76 65  sion and user-ve
23da6 72 73 69 6f 6e 2c 20 72 65 73 70 65 63 74 69 76  rsion, respectiv
23da7 65 6c 79 2e 20 42 6f 74 68 0a 20 20 2a 2a 20 74  ely. Both.  ** t
23da8 68 65 20 73 63 68 65 6d 61 2d 76 65 72 73 69 6f  he schema-versio
23da9 6e 20 61 6e 64 20 74 68 65 20 75 73 65 72 2d 76  n and the user-v
23daa 65 72 73 69 6f 6e 20 61 72 65 20 33 32 2d 62 69  ersion are 32-bi
23dab 74 20 73 69 67 6e 65 64 20 69 6e 74 65 67 65 72  t signed integer
23dac 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 69 6e  s.  ** stored in
23dad 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68 65   the database he
23dae 61 64 65 72 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  ader..  **.  ** 
23daf 54 68 65 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69  The schema-cooki
23db0 65 20 69 73 20 75 73 75 61 6c 6c 79 20 6f 6e 6c  e is usually onl
23db1 79 20 6d 61 6e 69 70 75 6c 61 74 65 64 20 69 6e  y manipulated in
23db2 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69  ternally by SQLi
23db3 74 65 2e 20 49 74 0a 20 20 2a 2a 20 69 73 20 69  te. It.  ** is i
23db4 6e 63 72 65 6d 65 6e 74 65 64 20 62 79 20 53 51  ncremented by SQ
23db5 4c 69 74 65 20 77 68 65 6e 65 76 65 72 20 74 68  Lite whenever th
23db6 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65 6d  e database schem
23db7 61 20 69 73 20 6d 6f 64 69 66 69 65 64 20 28 62  a is modified (b
23db8 79 0a 20 20 2a 2a 20 63 72 65 61 74 69 6e 67 20  y.  ** creating 
23db9 6f 72 20 64 72 6f 70 70 69 6e 67 20 61 20 74 61  or dropping a ta
23dba 62 6c 65 20 6f 72 20 69 6e 64 65 78 29 2e 20 54  ble or index). T
23dbb 68 65 20 73 63 68 65 6d 61 20 76 65 72 73 69 6f  he schema versio
23dbc 6e 20 69 73 20 75 73 65 64 20 62 79 0a 20 20 2a  n is used by.  *
23dbd 2a 20 53 51 4c 69 74 65 20 65 61 63 68 20 74 69  * SQLite each ti
23dbe 6d 65 20 61 20 71 75 65 72 79 20 69 73 20 65 78  me a query is ex
23dbf 65 63 75 74 65 64 20 74 6f 20 65 6e 73 75 72 65  ecuted to ensure
23dc0 20 74 68 61 74 20 74 68 65 20 69 6e 74 65 72 6e   that the intern
23dc1 61 6c 20 63 61 63 68 65 0a 20 20 2a 2a 20 6f 66  al cache.  ** of
23dc2 20 74 68 65 20 73 63 68 65 6d 61 20 75 73 65 64   the schema used
23dc3 20 77 68 65 6e 20 63 6f 6d 70 69 6c 69 6e 67 20   when compiling 
23dc4 74 68 65 20 53 51 4c 20 71 75 65 72 79 20 6d 61  the SQL query ma
23dc5 74 63 68 65 73 20 74 68 65 20 73 63 68 65 6d 61  tches the schema
23dc6 20 6f 66 0a 20 20 2a 2a 20 74 68 65 20 64 61 74   of.  ** the dat
23dc7 61 62 61 73 65 20 61 67 61 69 6e 73 74 20 77 68  abase against wh
23dc8 69 63 68 20 74 68 65 20 63 6f 6d 70 69 6c 65 64  ich the compiled
23dc9 20 71 75 65 72 79 20 69 73 20 61 63 74 75 61 6c   query is actual
23dca 6c 79 20 65 78 65 63 75 74 65 64 2e 0a 20 20 2a  ly executed..  *
23dcb 2a 20 53 75 62 76 65 72 74 69 6e 67 20 74 68 69  * Subverting thi
23dcc 73 20 6d 65 63 68 61 6e 69 73 6d 20 62 79 20 75  s mechanism by u
23dcd 73 69 6e 67 20 22 50 52 41 47 4d 41 20 73 63 68  sing "PRAGMA sch
23dce 65 6d 61 5f 76 65 72 73 69 6f 6e 22 20 74 6f 20  ema_version" to 
23dcf 6d 6f 64 69 66 79 0a 20 20 2a 2a 20 74 68 65 20  modify.  ** the 
23dd0 73 63 68 65 6d 61 2d 76 65 72 73 69 6f 6e 20 69  schema-version i
23dd1 73 20 70 6f 74 65 6e 74 69 61 6c 6c 79 20 64 61  s potentially da
23dd2 6e 67 65 72 6f 75 73 20 61 6e 64 20 6d 61 79 20  ngerous and may 
23dd3 6c 65 61 64 20 74 6f 20 70 72 6f 67 72 61 6d 0a  lead to program.
23dd4 20 20 2a 2a 20 63 72 61 73 68 65 73 20 6f 72 20    ** crashes or 
23dd5 64 61 74 61 62 61 73 65 20 63 6f 72 72 75 70 74  database corrupt
23dd6 69 6f 6e 2e 20 55 73 65 20 77 69 74 68 20 63 61  ion. Use with ca
23dd7 75 74 69 6f 6e 21 0a 20 20 2a 2a 0a 20 20 2a 2a  ution!.  **.  **
23dd8 20 54 68 65 20 75 73 65 72 2d 76 65 72 73 69 6f   The user-versio
23dd9 6e 20 69 73 20 6e 6f 74 20 75 73 65 64 20 69 6e  n is not used in
23dda 74 65 72 6e 61 6c 6c 79 20 62 79 20 53 51 4c 69  ternally by SQLi
23ddb 74 65 2e 20 49 74 20 6d 61 79 20 62 65 20 75 73  te. It may be us
23ddc 65 64 20 62 79 0a 20 20 2a 2a 20 61 70 70 6c 69  ed by.  ** appli
23ddd 63 61 74 69 6f 6e 73 20 66 6f 72 20 61 6e 79 20  cations for any 
23dde 70 75 72 70 6f 73 65 2e 0a 20 20 2a 2f 0a 20 20  purpose..  */.  
23ddf 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
23de0 6d 70 28 7a 4c 65 66 74 2c 20 22 73 63 68 65 6d  mp(zLeft, "schem
23de1 61 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a  a_version")==0 .
23de2 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
23de3 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 75 73 65  ICmp(zLeft, "use
23de4 72 5f 76 65 72 73 69 6f 6e 22 29 3d 3d 30 20 0a  r_version")==0 .
23de5 20 20 20 7c 7c 20 73 71 6c 69 74 65 33 53 74 72     || sqlite3Str
23de6 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 66 72 65  ICmp(zLeft, "fre
23de7 65 6c 69 73 74 5f 63 6f 75 6e 74 22 29 3d 3d 30  elist_count")==0
23de8 20 0a 20 20 29 7b 0a 20 20 20 20 69 6e 74 20 69   .  ){.    int i
23de9 43 6f 6f 6b 69 65 3b 20 20 20 2f 2a 20 43 6f 6f  Cookie;   /* Coo
23dea 6b 69 65 20 69 6e 64 65 78 2e 20 30 20 66 6f 72  kie index. 0 for
23deb 20 73 63 68 65 6d 61 2d 63 6f 6f 6b 69 65 2c 20   schema-cookie, 
23dec 36 20 66 6f 72 20 75 73 65 72 2d 63 6f 6f 6b 69  6 for user-cooki
23ded 65 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  e. */.    sqlite
23dee 33 56 64 62 65 55 73 65 73 42 74 72 65 65 28 76  3VdbeUsesBtree(v
23def 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 77 69 74  , iDb);.    swit
23df0 63 68 28 20 7a 4c 65 66 74 5b 30 5d 20 29 7b 0a  ch( zLeft[0] ){.
23df1 20 20 20 20 20 20 63 61 73 65 20 27 73 27 3a 20        case 's': 
23df2 63 61 73 65 20 27 53 27 3a 0a 20 20 20 20 20 20  case 'S':.      
23df3 20 20 69 43 6f 6f 6b 69 65 20 3d 20 30 3b 0a 20    iCookie = 0;. 
23df4 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
23df5 20 20 20 20 63 61 73 65 20 27 66 27 3a 20 63 61      case 'f': ca
23df6 73 65 20 27 46 27 3a 0a 20 20 20 20 20 20 20 20  se 'F':.        
23df7 69 43 6f 6f 6b 69 65 20 3d 20 31 3b 0a 20 20 20  iCookie = 1;.   
23df8 20 20 20 20 20 69 44 62 20 3d 20 28 2d 31 2a 28       iDb = (-1*(
23df9 69 44 62 2b 31 29 29 3b 0a 20 20 20 20 20 20 20  iDb+1));.       
23dfa 20 61 73 73 65 72 74 28 69 44 62 3c 3d 30 29 3b   assert(iDb<=0);
23dfb 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
23dfc 20 20 20 20 20 20 64 65 66 61 75 6c 74 3a 0a 20        default:. 
23dfd 20 20 20 20 20 20 20 69 43 6f 6f 6b 69 65 20 3d         iCookie =
23dfe 20 35 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61   5;.        brea
23dff 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  k;.    }..    if
23e00 28 20 7a 52 69 67 68 74 20 26 26 20 69 44 62 3e  ( zRight && iDb>
23e01 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 57  =0 ){.      /* W
23e02 72 69 74 65 20 74 68 65 20 73 70 65 63 69 66 69  rite the specifi
23e03 65 64 20 63 6f 6f 6b 69 65 20 76 61 6c 75 65 20  ed cookie value 
23e04 2a 2f 0a 20 20 20 20 20 20 73 74 61 74 69 63 20  */.      static 
23e05 63 6f 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74  const VdbeOpList
23e06 20 73 65 74 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b   setCookie[] = {
23e07 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f 54 72  .        { OP_Tr
23e08 61 6e 73 61 63 74 69 6f 6e 2c 20 20 20 20 30 2c  ansaction,    0,
23e09 20 20 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20    1,  0},    /* 
23e0a 30 20 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f  0 */.        { O
23e0b 50 5f 49 6e 74 65 67 65 72 2c 20 20 20 20 20 20  P_Integer,      
23e0c 20 20 30 2c 20 20 31 2c 20 20 30 7d 2c 20 20 20    0,  1,  0},   
23e0d 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 20   /* 1 */.       
23e0e 20 7b 20 4f 50 5f 53 65 74 43 6f 6f 6b 69 65 2c   { OP_SetCookie,
23e0f 20 20 20 20 20 20 30 2c 20 20 30 2c 20 20 31 7d        0,  0,  1}
23e10 2c 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20 20 20  ,    /* 2 */.   
23e11 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20     };.      int 
23e12 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64  addr = sqlite3Vd
23e13 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41  beAddOpList(v, A
23e14 72 72 61 79 53 69 7a 65 28 73 65 74 43 6f 6f 6b  rraySize(setCook
23e15 69 65 29 2c 20 73 65 74 43 6f 6f 6b 69 65 29 3b  ie), setCookie);
23e16 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
23e17 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61 64  beChangeP1(v, ad
23e18 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20  dr, iDb);.      
23e19 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
23e1a 65 50 31 28 76 2c 20 61 64 64 72 2b 31 2c 20 61  eP1(v, addr+1, a
23e1b 74 6f 69 28 7a 52 69 67 68 74 29 29 3b 0a 20 20  toi(zRight));.  
23e1c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43      sqlite3VdbeC
23e1d 68 61 6e 67 65 50 31 28 76 2c 20 61 64 64 72 2b  hangeP1(v, addr+
23e1e 32 2c 20 69 44 62 29 3b 0a 20 20 20 20 20 20 73  2, iDb);.      s
23e1f 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65  qlite3VdbeChange
23e20 50 32 28 76 2c 20 61 64 64 72 2b 32 2c 20 69 43  P2(v, addr+2, iC
23e21 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 7d 65 6c 73  ookie);.    }els
23e22 65 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65 61 64  e{.      /* Read
23e23 20 74 68 65 20 73 70 65 63 69 66 69 65 64 20 63   the specified c
23e24 6f 6f 6b 69 65 20 76 61 6c 75 65 20 2a 2f 0a 20  ookie value */. 
23e25 20 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73       static cons
23e26 74 20 56 64 62 65 4f 70 4c 69 73 74 20 72 65 61  t VdbeOpList rea
23e27 64 43 6f 6f 6b 69 65 5b 5d 20 3d 20 7b 0a 20 20  dCookie[] = {.  
23e28 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 61 64 43        { OP_ReadC
23e29 6f 6f 6b 69 65 2c 20 20 20 20 20 20 30 2c 20 20  ookie,      0,  
23e2a 31 2c 20 20 30 7d 2c 20 20 20 20 2f 2a 20 30 20  1,  0},    /* 0 
23e2b 2a 2f 0a 20 20 20 20 20 20 20 20 7b 20 4f 50 5f  */.        { OP_
23e2c 52 65 73 75 6c 74 52 6f 77 2c 20 20 20 20 20 20  ResultRow,      
23e2d 20 31 2c 20 20 31 2c 20 20 30 7d 0a 20 20 20 20   1,  1,  0}.    
23e2e 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20 61    };.      int a
23e2f 64 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ddr = sqlite3Vdb
23e30 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20 41 72  eAddOpList(v, Ar
23e31 72 61 79 53 69 7a 65 28 72 65 61 64 43 6f 6f 6b  raySize(readCook
23e32 69 65 29 2c 20 72 65 61 64 43 6f 6f 6b 69 65 29  ie), readCookie)
23e33 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
23e34 64 62 65 43 68 61 6e 67 65 50 31 28 76 2c 20 61  dbeChangeP1(v, a
23e35 64 64 72 2c 20 69 44 62 29 3b 0a 20 20 20 20 20  ddr, iDb);.     
23e36 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
23e37 67 65 50 33 28 76 2c 20 61 64 64 72 2c 20 69 43  geP3(v, addr, iC
23e38 6f 6f 6b 69 65 29 3b 0a 20 20 20 20 20 20 73 71  ookie);.      sq
23e39 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43  lite3VdbeSetNumC
23e3a 6f 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 20  ols(v, 1);.     
23e3b 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
23e3c 6f 6c 4e 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c  olName(v, 0, COL
23e3d 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4c 65 66 74  NAME_NAME, zLeft
23e3e 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
23e3f 4e 54 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c  NT);.    }.  }el
23e40 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  se.#endif /* SQL
23e41 49 54 45 5f 4f 4d 49 54 5f 53 43 48 45 4d 41 5f  ITE_OMIT_SCHEMA_
23e42 56 45 52 53 49 4f 4e 5f 50 52 41 47 4d 41 53 20  VERSION_PRAGMAS 
23e43 2a 2f 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  */..#if defined(
23e44 53 51 4c 49 54 45 5f 44 45 42 55 47 29 20 7c 7c  SQLITE_DEBUG) ||
23e45 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
23e46 54 45 53 54 29 0a 20 20 2f 2a 0a 20 20 2a 2a 20  TEST).  /*.  ** 
23e47 52 65 70 6f 72 74 20 74 68 65 20 63 75 72 72 65  Report the curre
23e48 6e 74 20 73 74 61 74 65 20 6f 66 20 66 69 6c 65  nt state of file
23e49 20 6c 6f 67 73 20 66 6f 72 20 61 6c 6c 20 64 61   logs for all da
23e4a 74 61 62 61 73 65 73 0a 20 20 2a 2f 0a 20 20 69  tabases.  */.  i
23e4b 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
23e4c 70 28 7a 4c 65 66 74 2c 20 22 6c 6f 63 6b 5f 73  p(zLeft, "lock_s
23e4d 74 61 74 75 73 22 29 3d 3d 30 20 29 7b 0a 20 20  tatus")==0 ){.  
23e4e 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
23e4f 68 61 72 20 2a 63 6f 6e 73 74 20 61 7a 4c 6f 63  har *const azLoc
23e50 6b 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20 20 20 20  kName[] = {.    
23e51 20 20 22 75 6e 6c 6f 63 6b 65 64 22 2c 20 22 73    "unlocked", "s
23e52 68 61 72 65 64 22 2c 20 22 72 65 73 65 72 76 65  hared", "reserve
23e53 64 22 2c 20 22 70 65 6e 64 69 6e 67 22 2c 20 22  d", "pending", "
23e54 65 78 63 6c 75 73 69 76 65 22 0a 20 20 20 20 7d  exclusive".    }
23e55 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20  ;.    int i;.   
23e56 20 56 64 62 65 20 2a 76 20 3d 20 73 71 6c 69 74   Vdbe *v = sqlit
23e57 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
23e58 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  );.    sqlite3Vd
23e59 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c 20  beSetNumCols(v, 
23e5a 32 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  2);.    pParse->
23e5b 6e 4d 65 6d 20 3d 20 32 3b 0a 20 20 20 20 73 71  nMem = 2;.    sq
23e5c 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
23e5d 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
23e5e 45 5f 4e 41 4d 45 2c 20 22 64 61 74 61 62 61 73  E_NAME, "databas
23e5f 65 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  e", SQLITE_STATI
23e60 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  C);.    sqlite3V
23e61 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
23e62 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
23e63 2c 20 22 73 74 61 74 75 73 22 2c 20 53 51 4c 49  , "status", SQLI
23e64 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  TE_STATIC);.    
23e65 66 6f 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e  for(i=0; i<db->n
23e66 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  Db; i++){.      
23e67 42 74 72 65 65 20 2a 70 42 74 3b 0a 20 20 20 20  Btree *pBt;.    
23e68 20 20 50 61 67 65 72 20 2a 70 50 61 67 65 72 3b    Pager *pPager;
23e69 0a 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61  .      const cha
23e6a 72 20 2a 7a 53 74 61 74 65 20 3d 20 22 75 6e 6b  r *zState = "unk
23e6b 6e 6f 77 6e 22 3b 0a 20 20 20 20 20 20 69 6e 74  nown";.      int
23e6c 20 6a 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62   j;.      if( db
23e6d 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3d 3d  ->aDb[i].zName==
23e6e 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
23e6f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
23e70 64 64 4f 70 34 28 76 2c 20 4f 50 5f 53 74 72 69  ddOp4(v, OP_Stri
23e71 6e 67 38 2c 20 30 2c 20 31 2c 20 30 2c 20 64 62  ng8, 0, 1, 0, db
23e72 2d 3e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 2c 20  ->aDb[i].zName, 
23e73 50 34 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20  P4_STATIC);.    
23e74 20 20 70 42 74 20 3d 20 64 62 2d 3e 61 44 62 5b    pBt = db->aDb[
23e75 69 5d 2e 70 42 74 3b 0a 20 20 20 20 20 20 69 66  i].pBt;.      if
23e76 28 20 70 42 74 3d 3d 30 20 7c 7c 20 28 70 50 61  ( pBt==0 || (pPa
23e77 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
23e78 65 65 50 61 67 65 72 28 70 42 74 29 29 3d 3d 30  eePager(pBt))==0
23e79 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 53 74 61   ){.        zSta
23e7a 74 65 20 3d 20 22 63 6c 6f 73 65 64 22 3b 0a 20  te = "closed";. 
23e7b 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 73       }else if( s
23e7c 71 6c 69 74 65 33 5f 66 69 6c 65 5f 63 6f 6e 74  qlite3_file_cont
23e7d 72 6f 6c 28 64 62 2c 20 69 20 3f 20 64 62 2d 3e  rol(db, i ? db->
23e7e 61 44 62 5b 69 5d 2e 7a 4e 61 6d 65 20 3a 20 30  aDb[i].zName : 0
23e7f 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
23e80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23e81 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 46          SQLITE_F
23e82 43 4e 54 4c 5f 4c 4f 43 4b 53 54 41 54 45 2c 20  CNTL_LOCKSTATE, 
23e83 26 6a 29 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29  &j)==SQLITE_OK )
23e84 7b 0a 20 20 20 20 20 20 20 20 20 7a 53 74 61 74  {.         zStat
23e85 65 20 3d 20 61 7a 4c 6f 63 6b 4e 61 6d 65 5b 6a  e = azLockName[j
23e86 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  ];.      }.     
23e87 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
23e88 70 34 28 76 2c 20 4f 50 5f 53 74 72 69 6e 67 38  p4(v, OP_String8
23e89 2c 20 30 2c 20 32 2c 20 30 2c 20 7a 53 74 61 74  , 0, 2, 0, zStat
23e8a 65 2c 20 50 34 5f 53 54 41 54 49 43 29 3b 0a 20  e, P4_STATIC);. 
23e8b 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
23e8c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
23e8d 75 6c 74 52 6f 77 2c 20 31 2c 20 32 29 3b 0a 20  ultRow, 1, 2);. 
23e8e 20 20 20 7d 0a 0a 20 20 7d 65 6c 73 65 0a 23 65     }..  }else.#e
23e8f 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
23e90 49 54 45 5f 53 53 45 0a 20 20 2f 2a 0a 20 20 2a  ITE_SSE.  /*.  *
23e91 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20 69  * Check to see i
23e92 66 20 74 68 65 20 73 71 6c 69 74 65 5f 73 74 61  f the sqlite_sta
23e93 74 65 6d 65 6e 74 73 20 74 61 62 6c 65 20 65 78  tements table ex
23e94 69 73 74 73 2e 20 20 43 72 65 61 74 65 20 69 74  ists.  Create it
23e95 0a 20 20 2a 2a 20 69 66 20 69 74 20 64 6f 65 73  .  ** if it does
23e96 20 6e 6f 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28   not..  */.  if(
23e97 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
23e98 7a 4c 65 66 74 2c 20 22 63 72 65 61 74 65 5f 73  zLeft, "create_s
23e99 71 6c 69 74 65 5f 73 74 61 74 65 6d 65 6e 74 5f  qlite_statement_
23e9a 74 61 62 6c 65 22 29 3d 3d 30 20 29 7b 0a 20 20  table")==0 ){.  
23e9b 20 20 65 78 74 65 72 6e 20 69 6e 74 20 73 71 6c    extern int sql
23e9c 69 74 65 33 43 72 65 61 74 65 53 74 61 74 65 6d  ite3CreateStatem
23e9d 65 6e 74 73 54 61 62 6c 65 28 50 61 72 73 65 2a  entsTable(Parse*
23e9e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 72  );.    sqlite3Cr
23e9f 65 61 74 65 53 74 61 74 65 6d 65 6e 74 73 54 61  eateStatementsTa
23ea0 62 6c 65 28 70 50 61 72 73 65 29 3b 0a 20 20 7d  ble(pParse);.  }
23ea1 65 6c 73 65 0a 23 65 6e 64 69 66 0a 0a 23 69 66  else.#endif..#if
23ea2 20 53 51 4c 49 54 45 5f 48 41 53 5f 43 4f 44 45   SQLITE_HAS_CODE
23ea3 43 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 53  C.  if( sqlite3S
23ea4 74 72 49 43 6d 70 28 7a 4c 65 66 74 2c 20 22 6b  trICmp(zLeft, "k
23ea5 65 79 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 73  ey")==0 ){.    s
23ea6 71 6c 69 74 65 33 5f 6b 65 79 28 64 62 2c 20 7a  qlite3_key(db, z
23ea7 52 69 67 68 74 2c 20 73 74 72 6c 65 6e 28 7a 52  Right, strlen(zR
23ea8 69 67 68 74 29 29 3b 0a 20 20 7d 65 6c 73 65 0a  ight));.  }else.
23ea9 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54  #endif.#if SQLIT
23eaa 45 5f 48 41 53 5f 43 4f 44 45 43 20 7c 7c 20 64  E_HAS_CODEC || d
23eab 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
23eac 41 42 4c 45 5f 43 45 52 4f 44 29 0a 20 20 69 66  ABLE_CEROD).  if
23ead 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70  ( sqlite3StrICmp
23eae 28 7a 4c 65 66 74 2c 20 22 61 63 74 69 76 61 74  (zLeft, "activat
23eaf 65 5f 65 78 74 65 6e 73 69 6f 6e 73 22 29 3d 3d  e_extensions")==
23eb0 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  0 ){.#if SQLITE_
23eb1 48 41 53 5f 43 4f 44 45 43 0a 20 20 20 20 69 66  HAS_CODEC.    if
23eb2 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  ( sqlite3StrNICm
23eb3 70 28 7a 52 69 67 68 74 2c 20 22 73 65 65 2d 22  p(zRight, "see-"
23eb4 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20  , 4)==0 ){.     
23eb5 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
23eb6 69 74 65 33 5f 61 63 74 69 76 61 74 65 5f 73 65  ite3_activate_se
23eb7 65 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a  e(const char*);.
23eb8 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 61 63        sqlite3_ac
23eb9 74 69 76 61 74 65 5f 73 65 65 28 26 7a 52 69 67  tivate_see(&zRig
23eba 68 74 5b 34 5d 29 3b 0a 20 20 20 20 7d 0a 23 65  ht[4]);.    }.#e
23ebb 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
23ebc 54 45 5f 45 4e 41 42 4c 45 5f 43 45 52 4f 44 0a  TE_ENABLE_CEROD.
23ebd 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
23ebe 74 72 4e 49 43 6d 70 28 7a 52 69 67 68 74 2c 20  trNICmp(zRight, 
23ebf 22 63 65 72 6f 64 2d 22 2c 20 36 29 3d 3d 30 20  "cerod-", 6)==0 
23ec0 29 7b 0a 20 20 20 20 20 20 65 78 74 65 72 6e 20  ){.      extern 
23ec1 76 6f 69 64 20 73 71 6c 69 74 65 33 5f 61 63 74  void sqlite3_act
23ec2 69 76 61 74 65 5f 63 65 72 6f 64 28 63 6f 6e 73  ivate_cerod(cons
23ec3 74 20 63 68 61 72 2a 29 3b 0a 20 20 20 20 20 20  t char*);.      
23ec4 73 71 6c 69 74 65 33 5f 61 63 74 69 76 61 74 65  sqlite3_activate
23ec5 5f 63 65 72 6f 64 28 26 7a 52 69 67 68 74 5b 36  _cerod(&zRight[6
23ec6 5d 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ]);.    }.#endif
23ec7 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 7b  .  }.#endif..  {
23ec8 7d 0a 0a 20 20 69 66 28 20 76 20 29 7b 0a 20 20  }..  if( v ){.  
23ec9 20 20 2f 2a 20 43 6f 64 65 20 61 6e 20 4f 50 5f    /* Code an OP_
23eca 45 78 70 69 72 65 20 61 74 20 74 68 65 20 65 6e  Expire at the en
23ecb 64 20 6f 66 20 65 61 63 68 20 50 52 41 47 4d 41  d of each PRAGMA
23ecc 20 70 72 6f 67 72 61 6d 20 74 6f 20 63 61 75 73   program to caus
23ecd 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 56 44 42  e.    ** the VDB
23ece 45 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74  E implementing t
23ecf 68 65 20 70 72 61 67 6d 61 20 74 6f 20 65 78 70  he pragma to exp
23ed0 69 72 65 2e 20 4d 6f 73 74 20 28 61 6c 6c 3f 29  ire. Most (all?)
23ed1 20 70 72 61 67 6d 61 73 0a 20 20 20 20 2a 2a 20   pragmas.    ** 
23ed2 61 72 65 20 6f 6e 6c 79 20 76 61 6c 69 64 20 66  are only valid f
23ed3 6f 72 20 61 20 73 69 6e 67 6c 65 20 65 78 65 63  or a single exec
23ed4 75 74 69 6f 6e 2e 0a 20 20 20 20 2a 2f 0a 20 20  ution..    */.  
23ed5 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
23ed6 4f 70 32 28 76 2c 20 4f 50 5f 45 78 70 69 72 65  Op2(v, OP_Expire
23ed7 2c 20 31 2c 20 30 29 3b 0a 0a 20 20 20 20 2f 2a  , 1, 0);..    /*
23ed8 0a 20 20 20 20 2a 2a 20 52 65 73 65 74 20 74 68  .    ** Reset th
23ed9 65 20 73 61 66 65 74 79 20 6c 65 76 65 6c 2c 20  e safety level, 
23eda 69 6e 20 63 61 73 65 20 74 68 65 20 66 75 6c 6c  in case the full
23edb 66 73 79 6e 63 20 66 6c 61 67 20 6f 72 20 73 79  fsync flag or sy
23edc 6e 63 68 72 6f 6e 6f 75 73 0a 20 20 20 20 2a 2a  nchronous.    **
23edd 20 73 65 74 74 69 6e 67 20 63 68 61 6e 67 65 64   setting changed
23ede 2e 0a 20 20 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ..    */.#ifndef
23edf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 47   SQLITE_OMIT_PAG
23ee0 45 52 5f 50 52 41 47 4d 41 53 0a 20 20 20 20 69  ER_PRAGMAS.    i
23ee1 66 28 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  f( db->autoCommi
23ee2 74 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  t ){.      sqlit
23ee3 65 33 42 74 72 65 65 53 65 74 53 61 66 65 74 79  e3BtreeSetSafety
23ee4 4c 65 76 65 6c 28 70 44 62 2d 3e 70 42 74 2c 20  Level(pDb->pBt, 
23ee5 70 44 62 2d 3e 73 61 66 65 74 79 5f 6c 65 76 65  pDb->safety_leve
23ee6 6c 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  l,.             
23ee7 20 20 20 20 28 64 62 2d 3e 66 6c 61 67 73 26 53      (db->flags&S
23ee8 51 4c 49 54 45 5f 46 75 6c 6c 46 53 79 6e 63 29  QLITE_FullFSync)
23ee9 21 3d 30 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64  !=0);.    }.#end
23eea 69 66 0a 20 20 7d 0a 70 72 61 67 6d 61 5f 6f 75  if.  }.pragma_ou
23eeb 74 3a 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72  t:.  sqlite3DbFr
23eec 65 65 28 64 62 2c 20 7a 4c 65 66 74 29 3b 0a 20  ee(db, zLeft);. 
23eed 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
23eee 62 2c 20 7a 52 69 67 68 74 29 3b 0a 7d 0a 0a 23  b, zRight);.}..#
23eef 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
23ef0 4f 4d 49 54 5f 50 52 41 47 4d 41 20 7c 7c 20 53  OMIT_PRAGMA || S
23ef1 51 4c 49 54 45 5f 4f 4d 49 54 5f 50 41 52 53 45  QLITE_OMIT_PARSE
23ef2 52 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  R */../*********
23ef3 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70 72 61  ***** End of pra
23ef4 67 6d 61 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  gma.c **********
23ef5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ef6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23ef7 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
23ef8 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
23ef9 20 70 72 65 70 61 72 65 2e 63 20 2a 2a 2a 2a 2a   prepare.c *****
23efa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23efb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23efc 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 35  ****/./*.** 2005
23efd 20 4d 61 79 20 32 35 0a 2a 2a 0a 2a 2a 20 54 68   May 25.**.** Th
23efe 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69  e author disclai
23eff 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20  ms copyright to 
23f00 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65  this source code
23f01 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a  .  In place of.*
23f02 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65  * a legal notice
23f03 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73  , here is a bles
23f04 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d  sing:.**.**    M
23f05 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61  ay you do good a
23f06 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20  nd not evil..** 
23f07 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20     May you find 
23f08 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20  forgiveness for 
23f09 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72  yourself and for
23f0a 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20  give others..** 
23f0b 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65     May you share
23f0c 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74   freely, never t
23f0d 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20  aking more than 
23f0e 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a  you give..**.***
23f0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23f10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23f11 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23f12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
23f13 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66  ******.** This f
23f14 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65  ile contains the
23f15 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20   implementation 
23f16 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 70  of the sqlite3_p
23f17 72 65 70 61 72 65 28 29 0a 2a 2a 20 69 6e 74 65  repare().** inte
23f18 72 66 61 63 65 2c 20 61 6e 64 20 72 6f 75 74 69  rface, and routi
23f19 6e 65 73 20 74 68 61 74 20 63 6f 6e 74 72 69 62  nes that contrib
23f1a 75 74 65 20 74 6f 20 6c 6f 61 64 69 6e 67 20 74  ute to loading t
23f1b 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68 65  he database sche
23f1c 6d 61 0a 2a 2a 20 66 72 6f 6d 20 64 69 73 6b 2e  ma.** from disk.
23f1d 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 70 72 65 70  .**.** $Id: prep
23f1e 61 72 65 2e 63 2c 76 20 31 2e 31 30 31 20 32 30  are.c,v 1.101 20
23f1f 30 38 2f 31 31 2f 31 39 20 31 36 3a 35 32 3a 34  08/11/19 16:52:4
23f20 34 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78  4 danielk1977 Ex
23f21 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69  p $.*/../*.** Fi
23f22 6c 6c 20 74 68 65 20 49 6e 69 74 44 61 74 61 20  ll the InitData 
23f23 73 74 72 75 63 74 75 72 65 20 77 69 74 68 20 61  structure with a
23f24 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
23f25 74 68 61 74 20 69 6e 64 69 63 61 74 65 73 0a 2a  that indicates.*
23f26 2a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 62  * that the datab
23f27 61 73 65 20 69 73 20 63 6f 72 72 75 70 74 2e 0a  ase is corrupt..
23f28 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63  */.static void c
23f29 6f 72 72 75 70 74 53 63 68 65 6d 61 28 0a 20 20  orruptSchema(.  
23f2a 49 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61 2c  InitData *pData,
23f2b 20 20 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69       /* Initiali
23f2c 7a 61 74 69 6f 6e 20 63 6f 6e 74 65 78 74 20 2a  zation context *
23f2d 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
23f2e 7a 4f 62 6a 2c 20 20 20 20 2f 2a 20 4f 62 6a 65  zObj,    /* Obje
23f2f 63 74 20 62 65 69 6e 67 20 70 61 72 73 65 64 20  ct being parsed 
23f30 61 74 20 74 68 65 20 70 6f 69 6e 74 20 6f 66 20  at the point of 
23f31 65 72 72 6f 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  error */.  const
23f32 20 63 68 61 72 20 2a 7a 45 78 74 72 61 20 20 20   char *zExtra   
23f33 2f 2a 20 45 72 72 6f 72 20 69 6e 66 6f 72 6d 61  /* Error informa
23f34 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c  tion */.){.  sql
23f35 69 74 65 33 20 2a 64 62 20 3d 20 70 44 61 74 61  ite3 *db = pData
23f36 2d 3e 64 62 3b 0a 20 20 69 66 28 20 21 64 62 2d  ->db;.  if( !db-
23f37 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26  >mallocFailed &&
23f38 20 28 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51   (db->flags & SQ
23f39 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d 6f 64  LITE_RecoveryMod
23f3a 65 29 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28  e)==0 ){.    if(
23f3b 20 7a 4f 62 6a 3d 3d 30 20 29 20 7a 4f 62 6a 20   zObj==0 ) zObj 
23f3c 3d 20 22 3f 22 3b 0a 20 20 20 20 73 71 6c 69 74  = "?";.    sqlit
23f3d 65 33 53 65 74 53 74 72 69 6e 67 28 70 44 61 74  e3SetString(pDat
23f3e 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 70 44 61  a->pzErrMsg, pDa
23f3f 74 61 2d 3e 64 62 2c 0a 20 20 20 20 20 20 20 22  ta->db,.       "
23f40 6d 61 6c 66 6f 72 6d 65 64 20 64 61 74 61 62 61  malformed databa
23f41 73 65 20 73 63 68 65 6d 61 20 28 25 73 29 22 2c  se schema (%s)",
23f42 20 7a 4f 62 6a 29 3b 0a 20 20 20 20 69 66 28 20   zObj);.    if( 
23f43 7a 45 78 74 72 61 20 26 26 20 7a 45 78 74 72 61  zExtra && zExtra
23f44 5b 30 5d 20 29 7b 0a 20 20 20 20 20 20 2a 70 44  [0] ){.      *pD
23f45 61 74 61 2d 3e 70 7a 45 72 72 4d 73 67 20 3d 20  ata->pzErrMsg = 
23f46 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28  sqlite3MAppendf(
23f47 70 44 61 74 61 2d 3e 64 62 2c 20 2a 70 44 61 74  pData->db, *pDat
23f48 61 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 22 25 73  a->pzErrMsg, "%s
23f49 20 2d 20 25 73 22 2c 0a 20 20 20 20 20 20 20 20   - %s",.        
23f4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
23f4b 20 20 20 20 20 20 20 20 20 20 2a 70 44 61 74 61            *pData
23f4c 2d 3e 70 7a 45 72 72 4d 73 67 2c 20 7a 45 78 74  ->pzErrMsg, zExt
23f4d 72 61 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  ra);.    }.  }. 
23f4e 20 70 44 61 74 61 2d 3e 72 63 20 3d 20 53 51 4c   pData->rc = SQL
23f4f 49 54 45 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 0a  ITE_CORRUPT;.}..
23f50 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
23f51 65 20 63 61 6c 6c 62 61 63 6b 20 72 6f 75 74 69  e callback routi
23f52 6e 65 20 66 6f 72 20 74 68 65 20 63 6f 64 65 20  ne for the code 
23f53 74 68 61 74 20 69 6e 69 74 69 61 6c 69 7a 65 73  that initializes
23f54 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73 65   the.** database
23f55 2e 20 20 53 65 65 20 73 71 6c 69 74 65 33 49 6e  .  See sqlite3In
23f56 69 74 28 29 20 62 65 6c 6f 77 20 66 6f 72 20 61  it() below for a
23f57 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
23f58 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 69 73 20 72  ation..** This r
23f59 6f 75 74 69 6e 65 20 69 73 20 61 6c 73 6f 20 63  outine is also c
23f5a 61 6c 6c 65 64 20 66 72 6f 6d 20 74 68 65 20 4f  alled from the O
23f5b 50 5f 50 61 72 73 65 53 63 68 65 6d 61 20 6f 70  P_ParseSchema op
23f5c 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44 42 45  code of the VDBE
23f5d 2e 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 63 61 6c  ..**.** Each cal
23f5e 6c 62 61 63 6b 20 63 6f 6e 74 61 69 6e 73 20 74  lback contains t
23f5f 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 6e 66  he following inf
23f60 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20  ormation:.**.** 
23f61 20 20 20 20 61 72 67 76 5b 30 5d 20 3d 20 6e 61      argv[0] = na
23f62 6d 65 20 6f 66 20 74 68 69 6e 67 20 62 65 69 6e  me of thing bein
23f63 67 20 63 72 65 61 74 65 64 0a 2a 2a 20 20 20 20  g created.**    
23f64 20 61 72 67 76 5b 31 5d 20 3d 20 72 6f 6f 74 20   argv[1] = root 
23f65 70 61 67 65 20 6e 75 6d 62 65 72 20 66 6f 72 20  page number for 
23f66 74 61 62 6c 65 20 6f 72 20 69 6e 64 65 78 2e 20  table or index. 
23f67 30 20 66 6f 72 20 74 72 69 67 67 65 72 20 6f 72  0 for trigger or
23f68 20 76 69 65 77 2e 0a 2a 2a 20 20 20 20 20 61 72   view..**     ar
23f69 67 76 5b 32 5d 20 3d 20 53 51 4c 20 74 65 78 74  gv[2] = SQL text
23f6a 20 66 6f 72 20 74 68 65 20 43 52 45 41 54 45 20   for the CREATE 
23f6b 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2f  statement..**.*/
23f6c 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
23f6d 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 43  int sqlite3InitC
23f6e 61 6c 6c 62 61 63 6b 28 76 6f 69 64 20 2a 70 49  allback(void *pI
23f6f 6e 69 74 2c 20 69 6e 74 20 61 72 67 63 2c 20 63  nit, int argc, c
23f70 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72  har **argv, char
23f71 20 2a 2a 4e 6f 74 55 73 65 64 29 7b 0a 20 20 49   **NotUsed){.  I
23f72 6e 69 74 44 61 74 61 20 2a 70 44 61 74 61 20 3d  nitData *pData =
23f73 20 28 49 6e 69 74 44 61 74 61 2a 29 70 49 6e 69   (InitData*)pIni
23f74 74 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t;.  sqlite3 *db
23f75 20 3d 20 70 44 61 74 61 2d 3e 64 62 3b 0a 20 20   = pData->db;.  
23f76 69 6e 74 20 69 44 62 20 3d 20 70 44 61 74 61 2d  int iDb = pData-
23f77 3e 69 44 62 3b 0a 0a 20 20 61 73 73 65 72 74 28  >iDb;..  assert(
23f78 20 61 72 67 63 3d 3d 33 20 29 3b 0a 20 20 55 4e   argc==3 );.  UN
23f79 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32 28  USED_PARAMETER2(
23f7a 4e 6f 74 55 73 65 64 2c 20 61 72 67 63 29 3b 0a  NotUsed, argc);.
23f7b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
23f7c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
23f7d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 44 62 43  >mutex) );.  DbC
23f7e 6c 65 61 72 50 72 6f 70 65 72 74 79 28 64 62 2c  learProperty(db,
23f7f 20 69 44 62 2c 20 44 42 5f 45 6d 70 74 79 29 3b   iDb, DB_Empty);
23f80 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f  .  if( db->mallo
23f81 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 63  cFailed ){.    c
23f82 6f 72 72 75 70 74 53 63 68 65 6d 61 28 70 44 61  orruptSchema(pDa
23f83 74 61 2c 20 61 72 67 76 5b 30 5d 2c 20 30 29 3b  ta, argv[0], 0);
23f84 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
23f85 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20  TE_NOMEM;.  }.. 
23f86 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d 30 20   assert( iDb>=0 
23f87 26 26 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29  && iDb<db->nDb )
23f88 3b 0a 20 20 69 66 28 20 61 72 67 76 3d 3d 30 20  ;.  if( argv==0 
23f89 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 2f 2a  ) return 0;   /*
23f8a 20 4d 69 67 68 74 20 68 61 70 70 65 6e 20 69 66   Might happen if
23f8b 20 45 4d 50 54 59 5f 52 45 53 55 4c 54 5f 43 41   EMPTY_RESULT_CA
23f8c 4c 4c 42 41 43 4b 53 20 61 72 65 20 6f 6e 20 2a  LLBACKS are on *
23f8d 2f 0a 20 20 69 66 28 20 61 72 67 76 5b 31 5d 3d  /.  if( argv[1]=
23f8e 3d 30 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70  =0 ){.    corrup
23f8f 74 53 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61  tSchema(pData, a
23f90 72 67 76 5b 30 5d 2c 20 30 29 3b 0a 20 20 7d 65  rgv[0], 0);.  }e
23f91 6c 73 65 20 69 66 28 20 61 72 67 76 5b 32 5d 20  lse if( argv[2] 
23f92 26 26 20 61 72 67 76 5b 32 5d 5b 30 5d 20 29 7b  && argv[2][0] ){
23f93 0a 20 20 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65  .    /* Call the
23f94 20 70 61 72 73 65 72 20 74 6f 20 70 72 6f 63 65   parser to proce
23f95 73 73 20 61 20 43 52 45 41 54 45 20 54 41 42 4c  ss a CREATE TABL
23f96 45 2c 20 49 4e 44 45 58 20 6f 72 20 56 49 45 57  E, INDEX or VIEW
23f97 2e 0a 20 20 20 20 2a 2a 20 42 75 74 20 62 65 63  ..    ** But bec
23f98 61 75 73 65 20 64 62 2d 3e 69 6e 69 74 2e 62 75  ause db->init.bu
23f99 73 79 20 69 73 20 73 65 74 20 74 6f 20 31 2c 20  sy is set to 1, 
23f9a 6e 6f 20 56 44 42 45 20 63 6f 64 65 20 69 73 20  no VDBE code is 
23f9b 67 65 6e 65 72 61 74 65 64 0a 20 20 20 20 2a 2a  generated.    **
23f9c 20 6f 72 20 65 78 65 63 75 74 65 64 2e 20 20 41   or executed.  A
23f9d 6c 6c 20 74 68 65 20 70 61 72 73 65 72 20 64 6f  ll the parser do
23f9e 65 73 20 69 73 20 62 75 69 6c 64 20 74 68 65 20  es is build the 
23f9f 69 6e 74 65 72 6e 61 6c 20 64 61 74 61 0a 20 20  internal data.  
23fa0 20 20 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20    ** structures 
23fa1 74 68 61 74 20 64 65 73 63 72 69 62 65 20 74 68  that describe th
23fa2 65 20 74 61 62 6c 65 2c 20 69 6e 64 65 78 2c 20  e table, index, 
23fa3 6f 72 20 76 69 65 77 2e 0a 20 20 20 20 2a 2f 0a  or view..    */.
23fa4 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 3b 0a      char *zErr;.
23fa5 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20 20 20      int rc;.    
23fa6 75 38 20 6c 6f 6f 6b 61 73 69 64 65 45 6e 61 62  u8 lookasideEnab
23fa7 6c 65 64 3b 0a 20 20 20 20 61 73 73 65 72 74 28  led;.    assert(
23fa8 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 29   db->init.busy )
23fa9 3b 0a 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69  ;.    db->init.i
23faa 44 62 20 3d 20 69 44 62 3b 0a 20 20 20 20 64 62  Db = iDb;.    db
23fab 2d 3e 69 6e 69 74 2e 6e 65 77 54 6e 75 6d 20 3d  ->init.newTnum =
23fac 20 61 74 6f 69 28 61 72 67 76 5b 31 5d 29 3b 0a   atoi(argv[1]);.
23fad 20 20 20 20 6c 6f 6f 6b 61 73 69 64 65 45 6e 61      lookasideEna
23fae 62 6c 65 64 20 3d 20 64 62 2d 3e 6c 6f 6f 6b 61  bled = db->looka
23faf 73 69 64 65 2e 62 45 6e 61 62 6c 65 64 3b 0a 20  side.bEnabled;. 
23fb0 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65     db->lookaside
23fb1 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b 0a 20  .bEnabled = 0;. 
23fb2 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
23fb3 65 78 65 63 28 64 62 2c 20 61 72 67 76 5b 32 5d  exec(db, argv[2]
23fb4 2c 20 30 2c 20 30 2c 20 26 7a 45 72 72 29 3b 0a  , 0, 0, &zErr);.
23fb5 20 20 20 20 64 62 2d 3e 69 6e 69 74 2e 69 44 62      db->init.iDb
23fb6 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 6c 6f   = 0;.    db->lo
23fb7 6f 6b 61 73 69 64 65 2e 62 45 6e 61 62 6c 65 64  okaside.bEnabled
23fb8 20 3d 20 6c 6f 6f 6b 61 73 69 64 65 45 6e 61 62   = lookasideEnab
23fb9 6c 65 64 3b 0a 20 20 20 20 61 73 73 65 72 74 28  led;.    assert(
23fba 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c   rc!=SQLITE_OK |
23fbb 7c 20 7a 45 72 72 3d 3d 30 20 29 3b 0a 20 20 20  | zErr==0 );.   
23fbc 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d   if( SQLITE_OK!=
23fbd 72 63 20 29 7b 0a 20 20 20 20 20 20 70 44 61 74  rc ){.      pDat
23fbe 61 2d 3e 72 63 20 3d 20 72 63 3b 0a 20 20 20 20  a->rc = rc;.    
23fbf 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
23fc0 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
23fc1 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
23fc2 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 65  ed = 1;.      }e
23fc3 6c 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49  lse if( rc!=SQLI
23fc4 54 45 5f 49 4e 54 45 52 52 55 50 54 20 29 7b 0a  TE_INTERRUPT ){.
23fc5 20 20 20 20 20 20 20 20 63 6f 72 72 75 70 74 53          corruptS
23fc6 63 68 65 6d 61 28 70 44 61 74 61 2c 20 61 72 67  chema(pData, arg
23fc7 76 5b 30 5d 2c 20 7a 45 72 72 29 3b 0a 20 20 20  v[0], zErr);.   
23fc8 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74     }.      sqlit
23fc9 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72  e3DbFree(db, zEr
23fca 72 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  r);.    }.  }els
23fcb 65 20 69 66 28 20 61 72 67 76 5b 30 5d 3d 3d 30  e if( argv[0]==0
23fcc 20 29 7b 0a 20 20 20 20 63 6f 72 72 75 70 74 53   ){.    corruptS
23fcd 63 68 65 6d 61 28 70 44 61 74 61 2c 20 30 2c 20  chema(pData, 0, 
23fce 30 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  0);.  }else{.   
23fcf 20 2f 2a 20 49 66 20 74 68 65 20 53 51 4c 20 63   /* If the SQL c
23fd0 6f 6c 75 6d 6e 20 69 73 20 62 6c 61 6e 6b 20 69  olumn is blank i
23fd1 74 20 6d 65 61 6e 73 20 74 68 69 73 20 69 73 20  t means this is 
23fd2 61 6e 20 69 6e 64 65 78 20 74 68 61 74 0a 20 20  an index that.  
23fd3 20 20 2a 2a 20 77 61 73 20 63 72 65 61 74 65 64    ** was created
23fd4 20 74 6f 20 62 65 20 74 68 65 20 50 52 49 4d 41   to be the PRIMA
23fd5 52 59 20 4b 45 59 20 6f 72 20 74 6f 20 66 75 6c  RY KEY or to ful
23fd6 66 69 6c 6c 20 61 20 55 4e 49 51 55 45 0a 20 20  fill a UNIQUE.  
23fd7 20 20 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20    ** constraint 
23fd8 66 6f 72 20 61 20 43 52 45 41 54 45 20 54 41 42  for a CREATE TAB
23fd9 4c 45 2e 20 20 54 68 65 20 69 6e 64 65 78 20 73  LE.  The index s
23fda 68 6f 75 6c 64 20 68 61 76 65 20 61 6c 72 65 61  hould have alrea
23fdb 64 79 0a 20 20 20 20 2a 2a 20 62 65 65 6e 20 63  dy.    ** been c
23fdc 72 65 61 74 65 64 20 77 68 65 6e 20 77 65 20 70  reated when we p
23fdd 72 6f 63 65 73 73 65 64 20 74 68 65 20 43 52 45  rocessed the CRE
23fde 41 54 45 20 54 41 42 4c 45 2e 20 20 41 6c 6c 20  ATE TABLE.  All 
23fdf 77 65 20 68 61 76 65 0a 20 20 20 20 2a 2a 20 74  we have.    ** t
23fe0 6f 20 64 6f 20 68 65 72 65 20 69 73 20 72 65 63  o do here is rec
23fe1 6f 72 64 20 74 68 65 20 72 6f 6f 74 20 70 61 67  ord the root pag
23fe2 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 61  e number for tha
23fe3 74 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a  t index..    */.
23fe4 20 20 20 20 49 6e 64 65 78 20 2a 70 49 6e 64 65      Index *pInde
23fe5 78 3b 0a 20 20 20 20 70 49 6e 64 65 78 20 3d 20  x;.    pIndex = 
23fe6 73 71 6c 69 74 65 33 46 69 6e 64 49 6e 64 65 78  sqlite3FindIndex
23fe7 28 64 62 2c 20 61 72 67 76 5b 30 5d 2c 20 64 62  (db, argv[0], db
23fe8 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
23fe9 29 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 64 65  );.    if( pInde
23fea 78 3d 3d 30 20 7c 7c 20 70 49 6e 64 65 78 2d 3e  x==0 || pIndex->
23feb 74 6e 75 6d 21 3d 30 20 29 7b 0a 20 20 20 20 20  tnum!=0 ){.     
23fec 20 2f 2a 20 54 68 69 73 20 63 61 6e 20 6f 63 63   /* This can occ
23fed 75 72 20 69 66 20 74 68 65 72 65 20 65 78 69 73  ur if there exis
23fee 74 73 20 61 6e 20 69 6e 64 65 78 20 6f 6e 20 61  ts an index on a
23fef 20 54 45 4d 50 20 74 61 62 6c 65 20 77 68 69 63   TEMP table whic
23ff0 68 0a 20 20 20 20 20 20 2a 2a 20 68 61 73 20 74  h.      ** has t
23ff1 68 65 20 73 61 6d 65 20 6e 61 6d 65 20 61 73 20  he same name as 
23ff2 61 6e 6f 74 68 65 72 20 69 6e 64 65 78 20 6f 6e  another index on
23ff3 20 61 20 70 65 72 6d 61 6e 65 6e 74 20 69 6e 64   a permanent ind
23ff4 65 78 2e 20 20 53 69 6e 63 65 0a 20 20 20 20 20  ex.  Since.     
23ff5 20 2a 2a 20 74 68 65 20 70 65 72 6d 61 6e 65 6e   ** the permanen
23ff6 74 20 74 61 62 6c 65 20 69 73 20 68 69 64 64 65  t table is hidde
23ff7 6e 20 62 79 20 74 68 65 20 54 45 4d 50 20 74 61  n by the TEMP ta
23ff8 62 6c 65 2c 20 77 65 20 63 61 6e 20 61 6c 73 6f  ble, we can also
23ff9 0a 20 20 20 20 20 20 2a 2a 20 73 61 66 65 6c 79  .      ** safely
23ffa 20 69 67 6e 6f 72 65 20 74 68 65 20 69 6e 64 65   ignore the inde
23ffb 78 20 6f 6e 20 74 68 65 20 70 65 72 6d 61 6e 65  x on the permane
23ffc 6e 74 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20  nt table..      
23ffd 2a 2f 0a 20 20 20 20 20 20 2f 2a 20 44 6f 20 4e  */.      /* Do N
23ffe 6f 74 68 69 6e 67 20 2a 2f 3b 0a 20 20 20 20 7d  othing */;.    }
23fff 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 49 6e 64  else{.      pInd
24000 65 78 2d 3e 74 6e 75 6d 20 3d 20 61 74 6f 69 28  ex->tnum = atoi(
24001 61 72 67 76 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a  argv[1]);.    }.
24002 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a    }.  return 0;.
24003 7d 0a 0a 2f 2a 0a 2a 2a 20 41 74 74 65 6d 70 74  }../*.** Attempt
24004 20 74 6f 20 72 65 61 64 20 74 68 65 20 64 61 74   to read the dat
24005 61 62 61 73 65 20 73 63 68 65 6d 61 20 61 6e 64  abase schema and
24006 20 69 6e 69 74 69 61 6c 69 7a 65 20 69 6e 74 65   initialize inte
24007 72 6e 61 6c 0a 2a 2a 20 64 61 74 61 20 73 74 72  rnal.** data str
24008 75 63 74 75 72 65 73 20 66 6f 72 20 61 20 73 69  uctures for a si
24009 6e 67 6c 65 20 64 61 74 61 62 61 73 65 20 66 69  ngle database fi
2400a 6c 65 2e 20 20 54 68 65 20 69 6e 64 65 78 20 6f  le.  The index o
2400b 66 20 74 68 65 0a 2a 2a 20 64 61 74 61 62 61 73  f the.** databas
2400c 65 20 66 69 6c 65 20 69 73 20 67 69 76 65 6e 20  e file is given 
2400d 62 79 20 69 44 62 2e 20 20 69 44 62 3d 3d 30 20  by iDb.  iDb==0 
2400e 69 73 20 75 73 65 64 20 66 6f 72 20 74 68 65 20  is used for the 
2400f 6d 61 69 6e 0a 2a 2a 20 64 61 74 61 62 61 73 65  main.** database
24010 2e 20 20 69 44 62 3d 3d 31 20 73 68 6f 75 6c 64  .  iDb==1 should
24011 20 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20   never be used. 
24012 20 69 44 62 3e 3d 32 20 69 73 20 75 73 65 64 20   iDb>=2 is used 
24013 66 6f 72 0a 2a 2a 20 61 75 78 69 6c 69 61 72 79  for.** auxiliary
24014 20 64 61 74 61 62 61 73 65 73 2e 20 20 52 65 74   databases.  Ret
24015 75 72 6e 20 6f 6e 65 20 6f 66 20 74 68 65 20 53  urn one of the S
24016 51 4c 49 54 45 5f 20 65 72 72 6f 72 20 63 6f 64  QLITE_ error cod
24017 65 73 20 74 6f 0a 2a 2a 20 69 6e 64 69 63 61 74  es to.** indicat
24018 65 20 73 75 63 63 65 73 73 20 6f 72 20 66 61 69  e success or fai
24019 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lure..*/.static 
2401a 69 6e 74 20 73 71 6c 69 74 65 33 49 6e 69 74 4f  int sqlite3InitO
2401b 6e 65 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ne(sqlite3 *db, 
2401c 69 6e 74 20 69 44 62 2c 20 63 68 61 72 20 2a 2a  int iDb, char **
2401d 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e 74  pzErrMsg){.  int
2401e 20 72 63 3b 0a 20 20 42 74 43 75 72 73 6f 72 20   rc;.  BtCursor 
2401f 2a 63 75 72 4d 61 69 6e 3b 0a 20 20 69 6e 74 20  *curMain;.  int 
24020 73 69 7a 65 3b 0a 20 20 54 61 62 6c 65 20 2a 70  size;.  Table *p
24021 54 61 62 3b 0a 20 20 44 62 20 2a 70 44 62 3b 0a  Tab;.  Db *pDb;.
24022 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 61 7a    char const *az
24023 41 72 67 5b 34 5d 3b 0a 20 20 69 6e 74 20 6d 65  Arg[4];.  int me
24024 74 61 5b 31 30 5d 3b 0a 20 20 49 6e 69 74 44 61  ta[10];.  InitDa
24025 74 61 20 69 6e 69 74 44 61 74 61 3b 0a 20 20 63  ta initData;.  c
24026 68 61 72 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74  har const *zMast
24027 65 72 53 63 68 65 6d 61 3b 0a 20 20 63 68 61 72  erSchema;.  char
24028 20 63 6f 6e 73 74 20 2a 7a 4d 61 73 74 65 72 4e   const *zMasterN
24029 61 6d 65 20 3d 20 53 43 48 45 4d 41 5f 54 41 42  ame = SCHEMA_TAB
2402a 4c 45 28 69 44 62 29 3b 0a 0a 20 20 2f 2a 0a 20  LE(iDb);..  /*. 
2402b 20 2a 2a 20 54 68 65 20 6d 61 73 74 65 72 20 64   ** The master d
2402c 61 74 61 62 61 73 65 20 74 61 62 6c 65 20 68 61  atabase table ha
2402d 73 20 61 20 73 74 72 75 63 74 75 72 65 20 6c 69  s a structure li
2402e 6b 65 20 74 68 69 73 0a 20 20 2a 2f 0a 20 20 73  ke this.  */.  s
2402f 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
24030 20 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d   master_schema[]
24031 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45   = .     "CREATE
24032 20 54 41 42 4c 45 20 73 71 6c 69 74 65 5f 6d 61   TABLE sqlite_ma
24033 73 74 65 72 28 5c 6e 22 0a 20 20 20 20 20 22 20  ster(\n".     " 
24034 20 74 79 70 65 20 74 65 78 74 2c 5c 6e 22 0a 20   type text,\n". 
24035 20 20 20 20 22 20 20 6e 61 6d 65 20 74 65 78 74      "  name text
24036 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 74 62 6c  ,\n".     "  tbl
24037 5f 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20  _name text,\n". 
24038 20 20 20 20 22 20 20 72 6f 6f 74 70 61 67 65 20      "  rootpage 
24039 69 6e 74 65 67 65 72 2c 5c 6e 22 0a 20 20 20 20  integer,\n".    
2403a 20 22 20 20 73 71 6c 20 74 65 78 74 5c 6e 22 0a   "  sql text\n".
2403b 20 20 20 20 20 22 29 22 0a 20 20 3b 0a 23 69 66       ")".  ;.#if
2403c 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2403d 5f 54 45 4d 50 44 42 0a 20 20 73 74 61 74 69 63  _TEMPDB.  static
2403e 20 63 6f 6e 73 74 20 63 68 61 72 20 74 65 6d 70   const char temp
2403f 5f 6d 61 73 74 65 72 5f 73 63 68 65 6d 61 5b 5d  _master_schema[]
24040 20 3d 20 0a 20 20 20 20 20 22 43 52 45 41 54 45   = .     "CREATE
24041 20 54 45 4d 50 20 54 41 42 4c 45 20 73 71 6c 69   TEMP TABLE sqli
24042 74 65 5f 74 65 6d 70 5f 6d 61 73 74 65 72 28 5c  te_temp_master(\
24043 6e 22 0a 20 20 20 20 20 22 20 20 74 79 70 65 20  n".     "  type 
24044 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20  text,\n".     " 
24045 20 6e 61 6d 65 20 74 65 78 74 2c 5c 6e 22 0a 20   name text,\n". 
24046 20 20 20 20 22 20 20 74 62 6c 5f 6e 61 6d 65 20      "  tbl_name 
24047 74 65 78 74 2c 5c 6e 22 0a 20 20 20 20 20 22 20  text,\n".     " 
24048 20 72 6f 6f 74 70 61 67 65 20 69 6e 74 65 67 65   rootpage intege
24049 72 2c 5c 6e 22 0a 20 20 20 20 20 22 20 20 73 71  r,\n".     "  sq
2404a 6c 20 74 65 78 74 5c 6e 22 0a 20 20 20 20 20 22  l text\n".     "
2404b 29 22 0a 20 20 3b 0a 23 65 6c 73 65 0a 20 20 23  )".  ;.#else.  #
2404c 64 65 66 69 6e 65 20 74 65 6d 70 5f 6d 61 73 74  define temp_mast
2404d 65 72 5f 73 63 68 65 6d 61 20 30 0a 23 65 6e 64  er_schema 0.#end
2404e 69 66 0a 0a 20 20 61 73 73 65 72 74 28 20 69 44  if..  assert( iD
2404f 62 3e 3d 30 20 26 26 20 69 44 62 3c 64 62 2d 3e  b>=0 && iDb<db->
24050 6e 44 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28  nDb );.  assert(
24051 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53   db->aDb[iDb].pS
24052 63 68 65 6d 61 20 29 3b 0a 20 20 61 73 73 65 72  chema );.  asser
24053 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
24054 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
24055 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69 44   );.  assert( iD
24056 62 3d 3d 31 20 7c 7c 20 73 71 6c 69 74 65 33 42  b==1 || sqlite3B
24057 74 72 65 65 48 6f 6c 64 73 4d 75 74 65 78 28 64  treeHoldsMutex(d
24058 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 29  b->aDb[iDb].pBt)
24059 20 29 3b 0a 0a 20 20 2f 2a 20 7a 4d 61 73 74 65   );..  /* zMaste
2405a 72 53 63 68 65 6d 61 20 61 6e 64 20 7a 49 6e 69  rSchema and zIni
2405b 74 53 63 72 69 70 74 20 61 72 65 20 73 65 74 20  tScript are set 
2405c 74 6f 20 70 6f 69 6e 74 20 61 74 20 74 68 65 20  to point at the 
2405d 6d 61 73 74 65 72 20 73 63 68 65 6d 61 0a 20 20  master schema.  
2405e 2a 2a 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73  ** and initialis
2405f 61 74 69 6f 6e 20 73 63 72 69 70 74 20 61 70 70  ation script app
24060 72 6f 70 72 69 61 74 65 20 66 6f 72 20 74 68 65  ropriate for the
24061 20 64 61 74 61 62 61 73 65 20 62 65 69 6e 67 0a   database being.
24062 20 20 2a 2a 20 69 6e 69 74 69 61 6c 69 73 65 64    ** initialised
24063 2e 20 7a 4d 61 73 74 65 72 4e 61 6d 65 20 69 73  . zMasterName is
24064 20 74 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65   the name of the
24065 20 6d 61 73 74 65 72 20 74 61 62 6c 65 2e 0a 20   master table.. 
24066 20 2a 2f 0a 20 20 69 66 28 20 21 4f 4d 49 54 5f   */.  if( !OMIT_
24067 54 45 4d 50 44 42 20 26 26 20 69 44 62 3d 3d 31  TEMPDB && iDb==1
24068 20 29 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72 53   ){.    zMasterS
24069 63 68 65 6d 61 20 3d 20 74 65 6d 70 5f 6d 61 73  chema = temp_mas
2406a 74 65 72 5f 73 63 68 65 6d 61 3b 0a 20 20 7d 65  ter_schema;.  }e
2406b 6c 73 65 7b 0a 20 20 20 20 7a 4d 61 73 74 65 72  lse{.    zMaster
2406c 53 63 68 65 6d 61 20 3d 20 6d 61 73 74 65 72 5f  Schema = master_
2406d 73 63 68 65 6d 61 3b 0a 20 20 7d 0a 20 20 7a 4d  schema;.  }.  zM
2406e 61 73 74 65 72 4e 61 6d 65 20 3d 20 53 43 48 45  asterName = SCHE
2406f 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 0a  MA_TABLE(iDb);..
24070 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74    /* Construct t
24071 68 65 20 73 63 68 65 6d 61 20 74 61 62 6c 65 73  he schema tables
24072 2e 20 20 2a 2f 0a 20 20 61 7a 41 72 67 5b 30 5d  .  */.  azArg[0]
24073 20 3d 20 7a 4d 61 73 74 65 72 4e 61 6d 65 3b 0a   = zMasterName;.
24074 20 20 61 7a 41 72 67 5b 31 5d 20 3d 20 22 31 22    azArg[1] = "1"
24075 3b 0a 20 20 61 7a 41 72 67 5b 32 5d 20 3d 20 7a  ;.  azArg[2] = z
24076 4d 61 73 74 65 72 53 63 68 65 6d 61 3b 0a 20 20  MasterSchema;.  
24077 61 7a 41 72 67 5b 33 5d 20 3d 20 30 3b 0a 20 20  azArg[3] = 0;.  
24078 69 6e 69 74 44 61 74 61 2e 64 62 20 3d 20 64 62  initData.db = db
24079 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 69 44 62  ;.  initData.iDb
2407a 20 3d 20 69 44 62 3b 0a 20 20 69 6e 69 74 44 61   = iDb;.  initDa
2407b 74 61 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  ta.rc = SQLITE_O
2407c 4b 3b 0a 20 20 69 6e 69 74 44 61 74 61 2e 70 7a  K;.  initData.pz
2407d 45 72 72 4d 73 67 20 3d 20 70 7a 45 72 72 4d 73  ErrMsg = pzErrMs
2407e 67 3b 0a 20 20 28 76 6f 69 64 29 73 71 6c 69 74  g;.  (void)sqlit
2407f 65 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b  e3SafetyOff(db);
24080 0a 20 20 73 71 6c 69 74 65 33 49 6e 69 74 43 61  .  sqlite3InitCa
24081 6c 6c 62 61 63 6b 28 26 69 6e 69 74 44 61 74 61  llback(&initData
24082 2c 20 33 2c 20 28 63 68 61 72 20 2a 2a 29 61 7a  , 3, (char **)az
24083 41 72 67 2c 20 30 29 3b 0a 20 20 28 76 6f 69 64  Arg, 0);.  (void
24084 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
24085 28 64 62 29 3b 0a 20 20 69 66 28 20 69 6e 69 74  (db);.  if( init
24086 44 61 74 61 2e 72 63 20 29 7b 0a 20 20 20 20 72  Data.rc ){.    r
24087 63 20 3d 20 69 6e 69 74 44 61 74 61 2e 72 63 3b  c = initData.rc;
24088 0a 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f  .    goto error_
24089 6f 75 74 3b 0a 20 20 7d 0a 20 20 70 54 61 62 20  out;.  }.  pTab 
2408a 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 54 61 62  = sqlite3FindTab
2408b 6c 65 28 64 62 2c 20 7a 4d 61 73 74 65 72 4e 61  le(db, zMasterNa
2408c 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  me, db->aDb[iDb]
2408d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 70  .zName);.  if( p
2408e 54 61 62 20 29 7b 0a 20 20 20 20 70 54 61 62 2d  Tab ){.    pTab-
2408f 3e 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f  >tabFlags |= TF_
24090 52 65 61 64 6f 6e 6c 79 3b 0a 20 20 7d 0a 0a 20  Readonly;.  }.. 
24091 20 2f 2a 20 43 72 65 61 74 65 20 61 20 63 75 72   /* Create a cur
24092 73 6f 72 20 74 6f 20 68 6f 6c 64 20 74 68 65 20  sor to hold the 
24093 64 61 74 61 62 61 73 65 20 6f 70 65 6e 0a 20 20  database open.  
24094 2a 2f 0a 20 20 70 44 62 20 3d 20 26 64 62 2d 3e  */.  pDb = &db->
24095 61 44 62 5b 69 44 62 5d 3b 0a 20 20 69 66 28 20  aDb[iDb];.  if( 
24096 70 44 62 2d 3e 70 42 74 3d 3d 30 20 29 7b 0a 20  pDb->pBt==0 ){. 
24097 20 20 20 69 66 28 20 21 4f 4d 49 54 5f 54 45 4d     if( !OMIT_TEM
24098 50 44 42 20 26 26 20 69 44 62 3d 3d 31 20 29 7b  PDB && iDb==1 ){
24099 0a 20 20 20 20 20 20 44 62 53 65 74 50 72 6f 70  .      DbSetProp
2409a 65 72 74 79 28 64 62 2c 20 31 2c 20 44 42 5f 53  erty(db, 1, DB_S
2409b 63 68 65 6d 61 4c 6f 61 64 65 64 29 3b 0a 20 20  chemaLoaded);.  
2409c 20 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 53    }.    return S
2409d 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20  QLITE_OK;.  }.  
2409e 63 75 72 4d 61 69 6e 20 3d 20 73 71 6c 69 74 65  curMain = sqlite
2409f 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 73 71 6c 69  3MallocZero(sqli
240a0 74 65 33 42 74 72 65 65 43 75 72 73 6f 72 53 69  te3BtreeCursorSi
240a1 7a 65 28 29 29 3b 0a 20 20 69 66 28 20 21 63 75  ze());.  if( !cu
240a2 72 4d 61 69 6e 20 29 7b 0a 20 20 20 20 72 63 20  rMain ){.    rc 
240a3 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
240a4 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f 6f      goto error_o
240a5 75 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ut;.  }.  sqlite
240a6 33 42 74 72 65 65 45 6e 74 65 72 28 70 44 62 2d  3BtreeEnter(pDb-
240a7 3e 70 42 74 29 3b 0a 20 20 72 63 20 3d 20 73 71  >pBt);.  rc = sq
240a8 6c 69 74 65 33 42 74 72 65 65 43 75 72 73 6f 72  lite3BtreeCursor
240a9 28 70 44 62 2d 3e 70 42 74 2c 20 4d 41 53 54 45  (pDb->pBt, MASTE
240aa 52 5f 52 4f 4f 54 2c 20 30 2c 20 30 2c 20 63 75  R_ROOT, 0, 0, cu
240ab 72 4d 61 69 6e 29 3b 0a 20 20 69 66 28 20 72 63  rMain);.  if( rc
240ac 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 72  !=SQLITE_OK && r
240ad 63 21 3d 53 51 4c 49 54 45 5f 45 4d 50 54 59 20  c!=SQLITE_EMPTY 
240ae 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65  ){.    sqlite3Se
240af 74 53 74 72 69 6e 67 28 70 7a 45 72 72 4d 73 67  tString(pzErrMsg
240b0 2c 20 64 62 2c 20 22 25 73 22 2c 20 73 71 6c 69  , db, "%s", sqli
240b1 74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a  te3ErrStr(rc));.
240b2 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
240b3 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a  _error_out;.  }.
240b4 0a 20 20 2f 2a 20 47 65 74 20 74 68 65 20 64 61  .  /* Get the da
240b5 74 61 62 61 73 65 20 6d 65 74 61 20 69 6e 66 6f  tabase meta info
240b6 72 6d 61 74 69 6f 6e 2e 0a 20 20 2a 2a 0a 20 20  rmation..  **.  
240b7 2a 2a 20 4d 65 74 61 20 76 61 6c 75 65 73 20 61  ** Meta values a
240b8 72 65 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 20  re as follows:. 
240b9 20 2a 2a 20 20 20 20 6d 65 74 61 5b 30 5d 20 20   **    meta[0]  
240ba 20 53 63 68 65 6d 61 20 63 6f 6f 6b 69 65 2e 20   Schema cookie. 
240bb 20 43 68 61 6e 67 65 73 20 77 69 74 68 20 65 61   Changes with ea
240bc 63 68 20 73 63 68 65 6d 61 20 63 68 61 6e 67 65  ch schema change
240bd 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 31  ..  **    meta[1
240be 5d 20 20 20 46 69 6c 65 20 66 6f 72 6d 61 74 20  ]   File format 
240bf 6f 66 20 73 63 68 65 6d 61 20 6c 61 79 65 72 2e  of schema layer.
240c0 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 32 5d  .  **    meta[2]
240c1 20 20 20 53 69 7a 65 20 6f 66 20 74 68 65 20 70     Size of the p
240c2 61 67 65 20 63 61 63 68 65 2e 0a 20 20 2a 2a 20  age cache..  ** 
240c3 20 20 20 6d 65 74 61 5b 33 5d 20 20 20 55 73 65     meta[3]   Use
240c4 20 66 72 65 65 6c 69 73 74 20 69 66 20 30 2e 20   freelist if 0. 
240c5 20 41 75 74 6f 76 61 63 75 75 6d 20 69 66 20 67   Autovacuum if g
240c6 72 65 61 74 65 72 20 74 68 61 6e 20 7a 65 72 6f  reater than zero
240c7 2e 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 34  ..  **    meta[4
240c8 5d 20 20 20 44 62 20 74 65 78 74 20 65 6e 63 6f  ]   Db text enco
240c9 64 69 6e 67 2e 20 31 3a 55 54 46 2d 38 20 32 3a  ding. 1:UTF-8 2:
240ca 55 54 46 2d 31 36 4c 45 20 33 3a 55 54 46 2d 31  UTF-16LE 3:UTF-1
240cb 36 42 45 0a 20 20 2a 2a 20 20 20 20 6d 65 74 61  6BE.  **    meta
240cc 5b 35 5d 20 20 20 54 68 65 20 75 73 65 72 20 63  [5]   The user c
240cd 6f 6f 6b 69 65 2e 20 55 73 65 64 20 62 79 20 74  ookie. Used by t
240ce 68 65 20 61 70 70 6c 69 63 61 74 69 6f 6e 2e 0a  he application..
240cf 20 20 2a 2a 20 20 20 20 6d 65 74 61 5b 36 5d 20    **    meta[6] 
240d0 20 20 49 6e 63 72 65 6d 65 6e 74 61 6c 2d 76 61    Incremental-va
240d1 63 75 75 6d 20 66 6c 61 67 2e 0a 20 20 2a 2a 20  cuum flag..  ** 
240d2 20 20 20 6d 65 74 61 5b 37 5d 0a 20 20 2a 2a 20     meta[7].  ** 
240d3 20 20 20 6d 65 74 61 5b 38 5d 0a 20 20 2a 2a 20     meta[8].  ** 
240d4 20 20 20 6d 65 74 61 5b 39 5d 0a 20 20 2a 2a 0a     meta[9].  **.
240d5 20 20 2a 2a 20 4e 6f 74 65 3a 20 54 68 65 20 23    ** Note: The #
240d6 64 65 66 69 6e 65 64 20 53 51 4c 49 54 45 5f 55  defined SQLITE_U
240d7 54 46 2a 20 73 79 6d 62 6f 6c 73 20 69 6e 20 73  TF* symbols in s
240d8 71 6c 69 74 65 49 6e 74 2e 68 20 63 6f 72 72 65  qliteInt.h corre
240d9 73 70 6f 6e 64 20 74 6f 0a 20 20 2a 2a 20 74 68  spond to.  ** th
240da 65 20 70 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  e possible value
240db 73 20 6f 66 20 6d 65 74 61 5b 34 5d 2e 0a 20 20  s of meta[4]..  
240dc 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
240dd 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e  ITE_OK ){.    in
240de 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  t i;.    for(i=0
240df 3b 20 69 3c 41 72 72 61 79 53 69 7a 65 28 6d 65  ; i<ArraySize(me
240e0 74 61 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ta); i++){.     
240e1 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74 72   rc = sqlite3Btr
240e2 65 65 47 65 74 4d 65 74 61 28 70 44 62 2d 3e 70  eeGetMeta(pDb->p
240e3 42 74 2c 20 69 2b 31 2c 20 28 75 33 32 20 2a 29  Bt, i+1, (u32 *)
240e4 26 6d 65 74 61 5b 69 5d 29 3b 0a 20 20 20 20 20  &meta[i]);.     
240e5 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20   if( rc ){.     
240e6 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
240e7 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
240e8 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
240e9 72 72 53 74 72 28 72 63 29 29 3b 0a 20 20 20 20  rrStr(rc));.    
240ea 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e 65      goto initone
240eb 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20 20  _error_out;.    
240ec 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73    }.    }.  }els
240ed 65 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 6d 65  e{.    memset(me
240ee 74 61 2c 20 30 2c 20 73 69 7a 65 6f 66 28 6d 65  ta, 0, sizeof(me
240ef 74 61 29 29 3b 0a 20 20 7d 0a 20 20 70 44 62 2d  ta));.  }.  pDb-
240f0 3e 70 53 63 68 65 6d 61 2d 3e 73 63 68 65 6d 61  >pSchema->schema
240f1 5f 63 6f 6f 6b 69 65 20 3d 20 6d 65 74 61 5b 30  _cookie = meta[0
240f2 5d 3b 0a 0a 20 20 2f 2a 20 49 66 20 6f 70 65 6e  ];..  /* If open
240f3 69 6e 67 20 61 20 6e 6f 6e 2d 65 6d 70 74 79 20  ing a non-empty 
240f4 64 61 74 61 62 61 73 65 2c 20 63 68 65 63 6b 20  database, check 
240f5 74 68 65 20 74 65 78 74 20 65 6e 63 6f 64 69 6e  the text encodin
240f6 67 2e 20 46 6f 72 20 74 68 65 0a 20 20 2a 2a 20  g. For the.  ** 
240f7 6d 61 69 6e 20 64 61 74 61 62 61 73 65 2c 20 73  main database, s
240f8 65 74 20 73 71 6c 69 74 65 33 2e 65 6e 63 20 74  et sqlite3.enc t
240f9 6f 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 6f  o the encoding o
240fa 66 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62  f the main datab
240fb 61 73 65 2e 0a 20 20 2a 2a 20 46 6f 72 20 61 6e  ase..  ** For an
240fc 20 61 74 74 61 63 68 65 64 20 64 62 2c 20 69 74   attached db, it
240fd 20 69 73 20 61 6e 20 65 72 72 6f 72 20 69 66 20   is an error if 
240fe 74 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20  the encoding is 
240ff 6e 6f 74 20 74 68 65 20 73 61 6d 65 0a 20 20 2a  not the same.  *
24100 2a 20 61 73 20 73 71 6c 69 74 65 33 2e 65 6e 63  * as sqlite3.enc
24101 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6d 65 74  ..  */.  if( met
24102 61 5b 34 5d 20 29 7b 20 20 2f 2a 20 74 65 78 74  a[4] ){  /* text
24103 20 65 6e 63 6f 64 69 6e 67 20 2a 2f 0a 20 20 20   encoding */.   
24104 20 69 66 28 20 69 44 62 3d 3d 30 20 29 7b 0a 20   if( iDb==0 ){. 
24105 20 20 20 20 20 2f 2a 20 49 66 20 6f 70 65 6e 69       /* If openi
24106 6e 67 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  ng the main data
24107 62 61 73 65 2c 20 73 65 74 20 45 4e 43 28 64 62  base, set ENC(db
24108 29 2e 20 2a 2f 0a 20 20 20 20 20 20 45 4e 43 28  ). */.      ENC(
24109 64 62 29 20 3d 20 28 75 38 29 6d 65 74 61 5b 34  db) = (u8)meta[4
2410a 5d 3b 0a 20 20 20 20 20 20 64 62 2d 3e 70 44 66  ];.      db->pDf
2410b 6c 74 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33  ltColl = sqlite3
2410c 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
2410d 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 42 49  SQLITE_UTF8, "BI
2410e 4e 41 52 59 22 2c 20 36 2c 20 30 29 3b 0a 20 20  NARY", 6, 0);.  
2410f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f    }else{.      /
24110 2a 20 49 66 20 6f 70 65 6e 69 6e 67 20 61 6e 20  * If opening an 
24111 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61 73  attached databas
24112 65 2c 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  e, the encoding 
24113 6d 75 63 68 20 6d 61 74 63 68 20 45 4e 43 28 64  much match ENC(d
24114 62 29 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20  b) */.      if( 
24115 6d 65 74 61 5b 34 5d 21 3d 45 4e 43 28 64 62 29  meta[4]!=ENC(db)
24116 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
24117 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a 45  te3SetString(pzE
24118 72 72 4d 73 67 2c 20 64 62 2c 20 22 61 74 74 61  rrMsg, db, "atta
24119 63 68 65 64 20 64 61 74 61 62 61 73 65 73 20 6d  ched databases m
2411a 75 73 74 20 75 73 65 20 74 68 65 20 73 61 6d 65  ust use the same
2411b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 22 20  ".            " 
2411c 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20 61 73  text encoding as
2411d 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 22 29   main database")
2411e 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  ;.        rc = S
2411f 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20  QLITE_ERROR;.   
24120 20 20 20 20 20 67 6f 74 6f 20 69 6e 69 74 6f 6e       goto initon
24121 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 20  e_error_out;.   
24122 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 65 6c     }.    }.  }el
24123 73 65 7b 0a 20 20 20 20 44 62 53 65 74 50 72 6f  se{.    DbSetPro
24124 70 65 72 74 79 28 64 62 2c 20 69 44 62 2c 20 44  perty(db, iDb, D
24125 42 5f 45 6d 70 74 79 29 3b 0a 20 20 7d 0a 20 20  B_Empty);.  }.  
24126 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 65 6e  pDb->pSchema->en
24127 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a 0a 20 20  c = ENC(db);..  
24128 69 66 28 20 70 44 62 2d 3e 70 53 63 68 65 6d 61  if( pDb->pSchema
24129 2d 3e 63 61 63 68 65 5f 73 69 7a 65 3d 3d 30 20  ->cache_size==0 
2412a 29 7b 0a 20 20 20 20 73 69 7a 65 20 3d 20 6d 65  ){.    size = me
2412b 74 61 5b 32 5d 3b 0a 20 20 20 20 69 66 28 20 73  ta[2];.    if( s
2412c 69 7a 65 3d 3d 30 20 29 7b 20 73 69 7a 65 20 3d  ize==0 ){ size =
2412d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54 5f   SQLITE_DEFAULT_
2412e 43 41 43 48 45 5f 53 49 5a 45 3b 20 7d 0a 20 20  CACHE_SIZE; }.  
2412f 20 20 69 66 28 20 73 69 7a 65 3c 30 20 29 20 73    if( size<0 ) s
24130 69 7a 65 20 3d 20 2d 73 69 7a 65 3b 0a 20 20 20  ize = -size;.   
24131 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 63   pDb->pSchema->c
24132 61 63 68 65 5f 73 69 7a 65 20 3d 20 73 69 7a 65  ache_size = size
24133 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72  ;.    sqlite3Btr
24134 65 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 70  eeSetCacheSize(p
24135 44 62 2d 3e 70 42 74 2c 20 70 44 62 2d 3e 70 53  Db->pBt, pDb->pS
24136 63 68 65 6d 61 2d 3e 63 61 63 68 65 5f 73 69 7a  chema->cache_siz
24137 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20  e);.  }..  /*.  
24138 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74 3d 3d  ** file_format==
24139 31 20 20 20 20 56 65 72 73 69 6f 6e 20 33 2e 30  1    Version 3.0
2413a 2e 30 2e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66 6f  .0..  ** file_fo
2413b 72 6d 61 74 3d 3d 32 20 20 20 20 56 65 72 73 69  rmat==2    Versi
2413c 6f 6e 20 33 2e 31 2e 33 2e 20 20 2f 2f 20 41 4c  on 3.1.3.  // AL
2413d 54 45 52 20 54 41 42 4c 45 20 41 44 44 20 43 4f  TER TABLE ADD CO
2413e 4c 55 4d 4e 0a 20 20 2a 2a 20 66 69 6c 65 5f 66  LUMN.  ** file_f
2413f 6f 72 6d 61 74 3d 3d 33 20 20 20 20 56 65 72 73  ormat==3    Vers
24140 69 6f 6e 20 33 2e 31 2e 34 2e 20 20 2f 2f 20 64  ion 3.1.4.  // d
24141 69 74 74 6f 20 62 75 74 20 77 69 74 68 20 6e 6f  itto but with no
24142 6e 2d 4e 55 4c 4c 20 64 65 66 61 75 6c 74 73 0a  n-NULL defaults.
24143 20 20 2a 2a 20 66 69 6c 65 5f 66 6f 72 6d 61 74    ** file_format
24144 3d 3d 34 20 20 20 20 56 65 72 73 69 6f 6e 20 33  ==4    Version 3
24145 2e 33 2e 30 2e 20 20 2f 2f 20 44 45 53 43 20 69  .3.0.  // DESC i
24146 6e 64 69 63 65 73 2e 20 20 42 6f 6f 6c 65 61 6e  ndices.  Boolean
24147 20 63 6f 6e 73 74 61 6e 74 73 0a 20 20 2a 2f 0a   constants.  */.
24148 20 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e    pDb->pSchema->
24149 66 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 6d 65  file_format = me
2414a 74 61 5b 31 5d 3b 0a 20 20 69 66 28 20 70 44 62  ta[1];.  if( pDb
2414b 2d 3e 70 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f  ->pSchema->file_
2414c 66 6f 72 6d 61 74 3d 3d 30 20 29 7b 0a 20 20 20  format==0 ){.   
2414d 20 70 44 62 2d 3e 70 53 63 68 65 6d 61 2d 3e 66   pDb->pSchema->f
2414e 69 6c 65 5f 66 6f 72 6d 61 74 20 3d 20 31 3b 0a  ile_format = 1;.
2414f 20 20 7d 0a 20 20 69 66 28 20 70 44 62 2d 3e 70    }.  if( pDb->p
24150 53 63 68 65 6d 61 2d 3e 66 69 6c 65 5f 66 6f 72  Schema->file_for
24151 6d 61 74 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46  mat>SQLITE_MAX_F
24152 49 4c 45 5f 46 4f 52 4d 41 54 20 29 7b 0a 20 20  ILE_FORMAT ){.  
24153 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72 69    sqlite3SetStri
24154 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62 2c  ng(pzErrMsg, db,
24155 20 22 75 6e 73 75 70 70 6f 72 74 65 64 20 66 69   "unsupported fi
24156 6c 65 20 66 6f 72 6d 61 74 22 29 3b 0a 20 20 20  le format");.   
24157 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
24158 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 69 6e 69  OR;.    goto ini
24159 74 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3b 0a  tone_error_out;.
2415a 20 20 7d 0a 0a 20 20 2f 2a 20 54 69 63 6b 65 74    }..  /* Ticket
2415b 20 23 32 38 30 34 3a 20 20 57 68 65 6e 20 77 65   #2804:  When we
2415c 20 6f 70 65 6e 20 61 20 64 61 74 61 62 61 73 65   open a database
2415d 20 69 6e 20 74 68 65 20 6e 65 77 65 72 20 66 69   in the newer fi
2415e 6c 65 20 66 6f 72 6d 61 74 2c 0a 20 20 2a 2a 20  le format,.  ** 
2415f 63 6c 65 61 72 20 74 68 65 20 6c 65 67 61 63 79  clear the legacy
24160 5f 66 69 6c 65 5f 66 6f 72 6d 61 74 20 70 72 61  _file_format pra
24161 67 6d 61 20 66 6c 61 67 20 73 6f 20 74 68 61 74  gma flag so that
24162 20 61 20 56 41 43 55 55 4d 20 77 69 6c 6c 0a 20   a VACUUM will. 
24163 20 2a 2a 20 6e 6f 74 20 64 6f 77 6e 67 72 61 64   ** not downgrad
24164 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20 61  e the database a
24165 6e 64 20 74 68 75 73 20 69 6e 76 61 6c 69 64 61  nd thus invalida
24166 74 65 20 61 6e 79 20 64 65 73 63 65 6e 64 69 6e  te any descendin
24167 67 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 74  g.  ** indices t
24168 68 61 74 20 74 68 65 20 75 73 65 72 20 6d 69 67  hat the user mig
24169 68 74 20 68 61 76 65 20 63 72 65 61 74 65 64 2e  ht have created.
2416a 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69 44 62 3d  .  */.  if( iDb=
2416b 3d 30 20 26 26 20 6d 65 74 61 5b 31 5d 3e 3d 34  =0 && meta[1]>=4
2416c 20 29 7b 0a 20 20 20 20 64 62 2d 3e 66 6c 61 67   ){.    db->flag
2416d 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 4c 65 67  s &= ~SQLITE_Leg
2416e 61 63 79 46 69 6c 65 46 6d 74 3b 0a 20 20 7d 0a  acyFileFmt;.  }.
2416f 0a 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 73  .  /* Read the s
24170 63 68 65 6d 61 20 69 6e 66 6f 72 6d 61 74 69 6f  chema informatio
24171 6e 20 6f 75 74 20 6f 66 20 74 68 65 20 73 63 68  n out of the sch
24172 65 6d 61 20 74 61 62 6c 65 73 0a 20 20 2a 2f 0a  ema tables.  */.
24173 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 69 6e    assert( db->in
24174 69 74 2e 62 75 73 79 20 29 3b 0a 20 20 69 66 28  it.busy );.  if(
24175 20 72 63 3d 3d 53 51 4c 49 54 45 5f 45 4d 50 54   rc==SQLITE_EMPT
24176 59 20 29 7b 0a 20 20 20 20 2f 2a 20 46 6f 72 20  Y ){.    /* For 
24177 61 6e 20 65 6d 70 74 79 20 64 61 74 61 62 61 73  an empty databas
24178 65 2c 20 74 68 65 72 65 20 69 73 20 6e 6f 74 68  e, there is noth
24179 69 6e 67 20 74 6f 20 72 65 61 64 20 2a 2f 0a 20  ing to read */. 
2417a 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f     rc = SQLITE_O
2417b 4b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  K;.  }else{.    
2417c 63 68 61 72 20 2a 7a 53 71 6c 3b 0a 20 20 20 20  char *zSql;.    
2417d 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 4d 50  zSql = sqlite3MP
2417e 72 69 6e 74 66 28 64 62 2c 20 0a 20 20 20 20 20  rintf(db, .     
2417f 20 20 20 22 53 45 4c 45 43 54 20 6e 61 6d 65 2c     "SELECT name,
24180 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c 20 46   rootpage, sql F
24181 52 4f 4d 20 27 25 71 27 2e 25 73 22 2c 0a 20 20  ROM '%q'.%s",.  
24182 20 20 20 20 20 20 64 62 2d 3e 61 44 62 5b 69 44        db->aDb[iD
24183 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 4d 61 73 74 65  b].zName, zMaste
24184 72 4e 61 6d 65 29 3b 0a 20 20 20 20 28 76 6f 69  rName);.    (voi
24185 64 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f  d)sqlite3SafetyO
24186 66 66 28 64 62 29 3b 0a 23 69 66 6e 64 65 66 20  ff(db);.#ifndef 
24187 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55 54 48  SQLITE_OMIT_AUTH
24188 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20 7b 0a  ORIZATION.    {.
24189 20 20 20 20 20 20 69 6e 74 20 28 2a 78 41 75 74        int (*xAut
2418a 68 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e  h)(void*,int,con
2418b 73 74 20 63 68 61 72 2a 2c 63 6f 6e 73 74 20 63  st char*,const c
2418c 68 61 72 2a 2c 63 6f 6e 73 74 20 63 68 61 72 2a  har*,const char*
2418d 2c 63 6f 6e 73 74 20 63 68 61 72 2a 29 3b 0a 20  ,const char*);. 
2418e 20 20 20 20 20 78 41 75 74 68 20 3d 20 64 62 2d       xAuth = db-
2418f 3e 78 41 75 74 68 3b 0a 20 20 20 20 20 20 64 62  >xAuth;.      db
24190 2d 3e 78 41 75 74 68 20 3d 20 30 3b 0a 23 65 6e  ->xAuth = 0;.#en
24191 64 69 66 0a 20 20 20 20 20 20 72 63 20 3d 20 73  dif.      rc = s
24192 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c 20  qlite3_exec(db, 
24193 7a 53 71 6c 2c 20 73 71 6c 69 74 65 33 49 6e 69  zSql, sqlite3Ini
24194 74 43 61 6c 6c 62 61 63 6b 2c 20 26 69 6e 69 74  tCallback, &init
24195 44 61 74 61 2c 20 30 29 3b 0a 23 69 66 6e 64 65  Data, 0);.#ifnde
24196 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 55  f SQLITE_OMIT_AU
24197 54 48 4f 52 49 5a 41 54 49 4f 4e 0a 20 20 20 20  THORIZATION.    
24198 20 20 64 62 2d 3e 78 41 75 74 68 20 3d 20 78 41    db->xAuth = xA
24199 75 74 68 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  uth;.    }.#endi
2419a 66 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  f.    if( rc==SQ
2419b 4c 49 54 45 5f 4f 4b 20 29 20 72 63 20 3d 20 69  LITE_OK ) rc = i
2419c 6e 69 74 44 61 74 61 2e 72 63 3b 0a 20 20 20 20  nitData.rc;.    
2419d 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
2419e 65 74 79 4f 6e 28 64 62 29 3b 0a 20 20 20 20 73  etyOn(db);.    s
2419f 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
241a0 20 7a 53 71 6c 29 3b 0a 23 69 66 6e 64 65 66 20   zSql);.#ifndef 
241a1 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 4e 41 4c  SQLITE_OMIT_ANAL
241a2 59 5a 45 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  YZE.    if( rc==
241a3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
241a4 20 20 20 73 71 6c 69 74 65 33 41 6e 61 6c 79 73     sqlite3Analys
241a5 69 73 4c 6f 61 64 28 64 62 2c 20 69 44 62 29 3b  isLoad(db, iDb);
241a6 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
241a7 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c  }.  if( db->mall
241a8 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
241a9 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
241aa 4d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  M;.    sqlite3Re
241ab 73 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d  setInternalSchem
241ac 61 28 64 62 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  a(db, 0);.  }.  
241ad 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
241ae 4b 20 7c 7c 20 28 64 62 2d 3e 66 6c 61 67 73 26  K || (db->flags&
241af 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72 79 4d  SQLITE_RecoveryM
241b0 6f 64 65 29 29 7b 0a 20 20 20 20 2f 2a 20 42 6c  ode)){.    /* Bl
241b1 61 63 6b 20 6d 61 67 69 63 3a 20 49 66 20 74 68  ack magic: If th
241b2 65 20 53 51 4c 49 54 45 5f 52 65 63 6f 76 65 72  e SQLITE_Recover
241b3 79 4d 6f 64 65 20 66 6c 61 67 20 69 73 20 73 65  yMode flag is se
241b4 74 2c 20 74 68 65 6e 20 63 6f 6e 73 69 64 65 72  t, then consider
241b5 0a 20 20 20 20 2a 2a 20 74 68 65 20 73 63 68 65  .    ** the sche
241b6 6d 61 20 6c 6f 61 64 65 64 2c 20 65 76 65 6e 20  ma loaded, even 
241b7 69 66 20 65 72 72 6f 72 73 20 6f 63 63 75 72 65  if errors occure
241b8 64 2e 20 49 6e 20 74 68 69 73 20 73 69 74 75 61  d. In this situa
241b9 74 69 6f 6e 20 74 68 65 20 0a 20 20 20 20 2a 2a  tion the .    **
241ba 20 63 75 72 72 65 6e 74 20 73 71 6c 69 74 65 33   current sqlite3
241bb 5f 70 72 65 70 61 72 65 28 29 20 6f 70 65 72 61  _prepare() opera
241bc 74 69 6f 6e 20 77 69 6c 6c 20 66 61 69 6c 2c 20  tion will fail, 
241bd 62 75 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  but the followin
241be 67 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 77 69 6c  g one.    ** wil
241bf 6c 20 61 74 74 65 6d 70 74 20 74 6f 20 63 6f 6d  l attempt to com
241c0 70 69 6c 65 20 74 68 65 20 73 75 70 70 6c 69 65  pile the supplie
241c1 64 20 73 74 61 74 65 6d 65 6e 74 20 61 67 61 69  d statement agai
241c2 6e 73 74 20 77 68 61 74 65 76 65 72 20 73 75 62  nst whatever sub
241c3 73 65 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74 68  set.    ** of th
241c4 65 20 73 63 68 65 6d 61 20 77 61 73 20 6c 6f 61  e schema was loa
241c5 64 65 64 20 62 65 66 6f 72 65 20 74 68 65 20 65  ded before the e
241c6 72 72 6f 72 20 6f 63 63 75 72 65 64 2e 20 54 68  rror occured. Th
241c7 65 20 70 72 69 6d 61 72 79 0a 20 20 20 20 2a 2a  e primary.    **
241c8 20 70 75 72 70 6f 73 65 20 6f 66 20 74 68 69 73   purpose of this
241c9 20 69 73 20 74 6f 20 61 6c 6c 6f 77 20 61 63 63   is to allow acc
241ca 65 73 73 20 74 6f 20 74 68 65 20 73 71 6c 69 74  ess to the sqlit
241cb 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 0a 20  e_master table. 
241cc 20 20 20 2a 2a 20 65 76 65 6e 20 77 68 65 6e 20     ** even when 
241cd 69 74 73 20 63 6f 6e 74 65 6e 74 73 20 68 61 76  its contents hav
241ce 65 20 62 65 65 6e 20 63 6f 72 72 75 70 74 65 64  e been corrupted
241cf 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 44 62 53  ..    */.    DbS
241d0 65 74 50 72 6f 70 65 72 74 79 28 64 62 2c 20 69  etProperty(db, i
241d1 44 62 2c 20 44 42 5f 53 63 68 65 6d 61 4c 6f 61  Db, DB_SchemaLoa
241d2 64 65 64 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ded);.    rc = S
241d3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
241d4 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 66 6f   /* Jump here fo
241d5 72 20 61 6e 20 65 72 72 6f 72 20 74 68 61 74 20  r an error that 
241d6 6f 63 63 75 72 73 20 61 66 74 65 72 20 73 75 63  occurs after suc
241d7 63 65 73 73 66 75 6c 6c 79 20 61 6c 6c 6f 63 61  cessfully alloca
241d8 74 69 6e 67 0a 20 20 2a 2a 20 63 75 72 4d 61 69  ting.  ** curMai
241d9 6e 20 61 6e 64 20 63 61 6c 6c 69 6e 67 20 73 71  n and calling sq
241da 6c 69 74 65 33 42 74 72 65 65 45 6e 74 65 72 28  lite3BtreeEnter(
241db 29 2e 20 46 6f 72 20 61 6e 20 65 72 72 6f 72 20  ). For an error 
241dc 74 68 61 74 20 6f 63 63 75 72 73 0a 20 20 2a 2a  that occurs.  **
241dd 20 62 65 66 6f 72 65 20 74 68 61 74 20 70 6f 69   before that poi
241de 6e 74 2c 20 6a 75 6d 70 20 74 6f 20 65 72 72 6f  nt, jump to erro
241df 72 5f 6f 75 74 2e 0a 20 20 2a 2f 0a 69 6e 69 74  r_out..  */.init
241e0 6f 6e 65 5f 65 72 72 6f 72 5f 6f 75 74 3a 0a 20  one_error_out:. 
241e1 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6c 6f   sqlite3BtreeClo
241e2 73 65 43 75 72 73 6f 72 28 63 75 72 4d 61 69 6e  seCursor(curMain
241e3 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
241e4 65 28 63 75 72 4d 61 69 6e 29 3b 0a 20 20 73 71  e(curMain);.  sq
241e5 6c 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 28  lite3BtreeLeave(
241e6 70 44 62 2d 3e 70 42 74 29 3b 0a 0a 65 72 72 6f  pDb->pBt);..erro
241e7 72 5f 6f 75 74 3a 0a 20 20 69 66 28 20 72 63 3d  r_out:.  if( rc=
241e8 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c  =SQLITE_NOMEM ||
241e9 20 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52   rc==SQLITE_IOER
241ea 52 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 64  R_NOMEM ){.    d
241eb 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
241ec 3d 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  = 1;.  }.  retur
241ed 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
241ee 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c 20 64 61  nitialize all da
241ef 74 61 62 61 73 65 20 66 69 6c 65 73 20 2d 20 74  tabase files - t
241f0 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65  he main database
241f1 20 66 69 6c 65 2c 20 74 68 65 20 66 69 6c 65 0a   file, the file.
241f2 2a 2a 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  ** used to store
241f3 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65   temporary table
241f4 73 2c 20 61 6e 64 20 61 6e 79 20 61 64 64 69 74  s, and any addit
241f5 69 6f 6e 61 6c 20 64 61 74 61 62 61 73 65 20 66  ional database f
241f6 69 6c 65 73 0a 2a 2a 20 63 72 65 61 74 65 64 20  iles.** created 
241f7 75 73 69 6e 67 20 41 54 54 41 43 48 20 73 74 61  using ATTACH sta
241f8 74 65 6d 65 6e 74 73 2e 20 20 52 65 74 75 72 6e  tements.  Return
241f9 20 61 20 73 75 63 63 65 73 73 20 63 6f 64 65 2e   a success code.
241fa 20 20 49 66 20 61 6e 0a 2a 2a 20 65 72 72 6f 72    If an.** error
241fb 20 6f 63 63 75 72 73 2c 20 77 72 69 74 65 20 61   occurs, write a
241fc 6e 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  n error message 
241fd 69 6e 74 6f 20 2a 70 7a 45 72 72 4d 73 67 2e 0a  into *pzErrMsg..
241fe 2a 2a 0a 2a 2a 20 41 66 74 65 72 20 61 20 64 61  **.** After a da
241ff 74 61 62 61 73 65 20 69 73 20 69 6e 69 74 69 61  tabase is initia
24200 6c 69 7a 65 64 2c 20 74 68 65 20 44 42 5f 53 63  lized, the DB_Sc
24201 68 65 6d 61 4c 6f 61 64 65 64 20 62 69 74 20 69  hemaLoaded bit i
24202 73 20 73 65 74 0a 2a 2a 20 62 69 74 20 69 73 20  s set.** bit is 
24203 73 65 74 20 69 6e 20 74 68 65 20 66 6c 61 67 73  set in the flags
24204 20 66 69 65 6c 64 20 6f 66 20 74 68 65 20 44 62   field of the Db
24205 20 73 74 72 75 63 74 75 72 65 2e 20 49 66 20 74   structure. If t
24206 68 65 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 66  he database.** f
24207 69 6c 65 20 77 61 73 20 6f 66 20 7a 65 72 6f 2d  ile was of zero-
24208 6c 65 6e 67 74 68 2c 20 74 68 65 6e 20 74 68 65  length, then the
24209 20 44 42 5f 45 6d 70 74 79 20 66 6c 61 67 20 69   DB_Empty flag i
2420a 73 20 61 6c 73 6f 20 73 65 74 2e 0a 2a 2f 0a 53  s also set..*/.S
2420b 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e  QLITE_PRIVATE in
2420c 74 20 73 71 6c 69 74 65 33 49 6e 69 74 28 73 71  t sqlite3Init(sq
2420d 6c 69 74 65 33 20 2a 64 62 2c 20 63 68 61 72 20  lite3 *db, char 
2420e 2a 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69  **pzErrMsg){.  i
2420f 6e 74 20 69 2c 20 72 63 3b 0a 20 20 69 6e 74 20  nt i, rc;.  int 
24210 63 6f 6d 6d 69 74 5f 69 6e 74 65 72 6e 61 6c 20  commit_internal 
24211 3d 20 21 28 64 62 2d 3e 66 6c 61 67 73 26 53 51  = !(db->flags&SQ
24212 4c 49 54 45 5f 49 6e 74 65 72 6e 43 68 61 6e 67  LITE_InternChang
24213 65 73 29 3b 0a 20 20 0a 20 20 61 73 73 65 72 74  es);.  .  assert
24214 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  ( sqlite3_mutex_
24215 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20  held(db->mutex) 
24216 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 69 6e 69  );.  if( db->ini
24217 74 2e 62 75 73 79 20 29 20 72 65 74 75 72 6e 20  t.busy ) return 
24218 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 72 63 20  SQLITE_OK;.  rc 
24219 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 64  = SQLITE_OK;.  d
2421a 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d 20 31  b->init.busy = 1
2421b 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d  ;.  for(i=0; rc=
2421c 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c  =SQLITE_OK && i<
2421d 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20  db->nDb; i++){. 
2421e 20 20 20 69 66 28 20 44 62 48 61 73 50 72 6f 70     if( DbHasProp
2421f 65 72 74 79 28 64 62 2c 20 69 2c 20 44 42 5f 53  erty(db, i, DB_S
24220 63 68 65 6d 61 4c 6f 61 64 65 64 29 20 7c 7c 20  chemaLoaded) || 
24221 69 3d 3d 31 20 29 20 63 6f 6e 74 69 6e 75 65 3b  i==1 ) continue;
24222 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
24223 33 49 6e 69 74 4f 6e 65 28 64 62 2c 20 69 2c 20  3InitOne(db, i, 
24224 70 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 69  pzErrMsg);.    i
24225 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 73  f( rc ){.      s
24226 71 6c 69 74 65 33 52 65 73 65 74 49 6e 74 65 72  qlite3ResetInter
24227 6e 61 6c 53 63 68 65 6d 61 28 64 62 2c 20 69 29  nalSchema(db, i)
24228 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
24229 2a 20 4f 6e 63 65 20 61 6c 6c 20 74 68 65 20 6f  * Once all the o
2422a 74 68 65 72 20 64 61 74 61 62 61 73 65 73 20 68  ther databases h
2422b 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61 6c  ave been initial
2422c 69 73 65 64 2c 20 6c 6f 61 64 20 74 68 65 20 73  ised, load the s
2422d 63 68 65 6d 61 0a 20 20 2a 2a 20 66 6f 72 20 74  chema.  ** for t
2422e 68 65 20 54 45 4d 50 20 64 61 74 61 62 61 73 65  he TEMP database
2422f 2e 20 54 68 69 73 20 69 73 20 6c 6f 61 64 65 64  . This is loaded
24230 20 6c 61 73 74 2c 20 61 73 20 74 68 65 20 54 45   last, as the TE
24231 4d 50 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  MP database.  **
24232 20 73 63 68 65 6d 61 20 6d 61 79 20 63 6f 6e 74   schema may cont
24233 61 69 6e 20 72 65 66 65 72 65 6e 63 65 73 20 74  ain references t
24234 6f 20 6f 62 6a 65 63 74 73 20 69 6e 20 6f 74 68  o objects in oth
24235 65 72 20 64 61 74 61 62 61 73 65 73 2e 0a 20 20  er databases..  
24236 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  */.#ifndef SQLIT
24237 45 5f 4f 4d 49 54 5f 54 45 4d 50 44 42 0a 20 20  E_OMIT_TEMPDB.  
24238 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
24239 4b 20 26 26 20 64 62 2d 3e 6e 44 62 3e 31 20 26  K && db->nDb>1 &
2423a 26 20 21 44 62 48 61 73 50 72 6f 70 65 72 74 79  & !DbHasProperty
2423b 28 64 62 2c 20 31 2c 20 44 42 5f 53 63 68 65 6d  (db, 1, DB_Schem
2423c 61 4c 6f 61 64 65 64 29 20 29 7b 0a 20 20 20 20  aLoaded) ){.    
2423d 72 63 20 3d 20 73 71 6c 69 74 65 33 49 6e 69 74  rc = sqlite3Init
2423e 4f 6e 65 28 64 62 2c 20 31 2c 20 70 7a 45 72 72  One(db, 1, pzErr
2423f 4d 73 67 29 3b 0a 20 20 20 20 69 66 28 20 72 63  Msg);.    if( rc
24240 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
24241 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53 63  3ResetInternalSc
24242 68 65 6d 61 28 64 62 2c 20 31 29 3b 0a 20 20 20  hema(db, 1);.   
24243 20 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20   }.  }.#endif.. 
24244 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20 3d   db->init.busy =
24245 20 30 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   0;.  if( rc==SQ
24246 4c 49 54 45 5f 4f 4b 20 26 26 20 63 6f 6d 6d 69  LITE_OK && commi
24247 74 5f 69 6e 74 65 72 6e 61 6c 20 29 7b 0a 20 20  t_internal ){.  
24248 20 20 73 71 6c 69 74 65 33 43 6f 6d 6d 69 74 49    sqlite3CommitI
24249 6e 74 65 72 6e 61 6c 43 68 61 6e 67 65 73 28 64  nternalChanges(d
2424a 62 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  b);.  }..  retur
2424b 6e 20 72 63 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  n rc; .}../*.** 
2424c 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2424d 61 20 6e 6f 2d 6f 70 20 69 66 20 74 68 65 20 64  a no-op if the d
2424e 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
2424f 73 20 61 6c 72 65 61 64 79 20 69 6e 69 74 69 61  s already initia
24250 6c 69 73 65 64 2e 0a 2a 2a 20 4f 74 68 65 72 77  lised..** Otherw
24251 69 73 65 2c 20 74 68 65 20 73 63 68 65 6d 61 20  ise, the schema 
24252 69 73 20 6c 6f 61 64 65 64 2e 20 41 6e 20 65 72  is loaded. An er
24253 72 6f 72 20 63 6f 64 65 20 69 73 20 72 65 74 75  ror code is retu
24254 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  rned..*/.SQLITE_
24255 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
24256 74 65 33 52 65 61 64 53 63 68 65 6d 61 28 50 61  te3ReadSchema(Pa
24257 72 73 65 20 2a 70 50 61 72 73 65 29 7b 0a 20 20  rse *pParse){.  
24258 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
24259 4f 4b 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  OK;.  sqlite3 *d
2425a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
2425b 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2425c 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d  3_mutex_held(db-
2425d 3e 6d 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28  >mutex) );.  if(
2425e 20 21 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79 20   !db->init.busy 
2425f 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
24260 74 65 33 49 6e 69 74 28 64 62 2c 20 26 70 50 61  te3Init(db, &pPa
24261 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  rse->zErrMsg);. 
24262 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
24263 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 70 50  ITE_OK ){.    pP
24264 61 72 73 65 2d 3e 72 63 20 3d 20 72 63 3b 0a 20  arse->rc = rc;. 
24265 20 20 20 70 50 61 72 73 65 2d 3e 6e 45 72 72 2b     pParse->nErr+
24266 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
24267 72 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 68  rc;.}.../*.** Ch
24268 65 63 6b 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69  eck schema cooki
24269 65 73 20 69 6e 20 61 6c 6c 20 64 61 74 61 62 61  es in all databa
2426a 73 65 73 2e 20 20 49 66 20 61 6e 79 20 63 6f 6f  ses.  If any coo
2426b 6b 69 65 20 69 73 20 6f 75 74 0a 2a 2a 20 6f 66  kie is out.** of
2426c 20 64 61 74 65 2c 20 72 65 74 75 72 6e 20 30 2e   date, return 0.
2426d 20 20 49 66 20 61 6c 6c 20 73 63 68 65 6d 61 20    If all schema 
2426e 63 6f 6f 6b 69 65 73 20 61 72 65 20 63 75 72 72  cookies are curr
2426f 65 6e 74 2c 20 72 65 74 75 72 6e 20 31 2e 0a 2a  ent, return 1..*
24270 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 63 68  /.static int sch
24271 65 6d 61 49 73 56 61 6c 69 64 28 73 71 6c 69 74  emaIsValid(sqlit
24272 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20 69  e3 *db){.  int i
24273 44 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20  Db;.  int rc;.  
24274 42 74 43 75 72 73 6f 72 20 2a 63 75 72 54 65 6d  BtCursor *curTem
24275 70 3b 0a 20 20 69 6e 74 20 63 6f 6f 6b 69 65 3b  p;.  int cookie;
24276 0a 20 20 69 6e 74 20 61 6c 6c 4f 6b 20 3d 20 31  .  int allOk = 1
24277 3b 0a 0a 20 20 63 75 72 54 65 6d 70 20 3d 20 28  ;..  curTemp = (
24278 42 74 43 75 72 73 6f 72 20 2a 29 73 71 6c 69 74  BtCursor *)sqlit
24279 65 33 4d 61 6c 6c 6f 63 28 73 71 6c 69 74 65 33  e3Malloc(sqlite3
2427a 42 74 72 65 65 43 75 72 73 6f 72 53 69 7a 65 28  BtreeCursorSize(
2427b 29 29 3b 0a 20 20 69 66 28 20 63 75 72 54 65 6d  ));.  if( curTem
2427c 70 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  p ){.    assert(
2427d 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68   sqlite3_mutex_h
2427e 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29 20 29  eld(db->mutex) )
2427f 3b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d 30 3b  ;.    for(iDb=0;
24280 20 61 6c 6c 4f 6b 20 26 26 20 69 44 62 3c 64 62   allOk && iDb<db
24281 2d 3e 6e 44 62 3b 20 69 44 62 2b 2b 29 7b 0a 20  ->nDb; iDb++){. 
24282 20 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 3b       Btree *pBt;
24283 0a 20 20 20 20 20 20 70 42 74 20 3d 20 64 62 2d  .      pBt = db-
24284 3e 61 44 62 5b 69 44 62 5d 2e 70 42 74 3b 0a 20  >aDb[iDb].pBt;. 
24285 20 20 20 20 20 69 66 28 20 70 42 74 3d 3d 30 20       if( pBt==0 
24286 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
24287 20 20 6d 65 6d 73 65 74 28 63 75 72 54 65 6d 70    memset(curTemp
24288 2c 20 30 2c 20 73 71 6c 69 74 65 33 42 74 72 65  , 0, sqlite3Btre
24289 65 43 75 72 73 6f 72 53 69 7a 65 28 29 29 3b 0a  eCursorSize());.
2428a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
2428b 65 33 42 74 72 65 65 43 75 72 73 6f 72 28 70 42  e3BtreeCursor(pB
2428c 74 2c 20 4d 41 53 54 45 52 5f 52 4f 4f 54 2c 20  t, MASTER_ROOT, 
2428d 30 2c 20 30 2c 20 63 75 72 54 65 6d 70 29 3b 0a  0, 0, curTemp);.
2428e 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
2428f 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
24290 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
24291 74 72 65 65 47 65 74 4d 65 74 61 28 70 42 74 2c  treeGetMeta(pBt,
24292 20 31 2c 20 28 75 33 32 20 2a 29 26 63 6f 6f 6b   1, (u32 *)&cook
24293 69 65 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  ie);.        if(
24294 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26   rc==SQLITE_OK &
24295 26 20 63 6f 6f 6b 69 65 21 3d 64 62 2d 3e 61 44  & cookie!=db->aD
24296 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d 61 2d 3e  b[iDb].pSchema->
24297 73 63 68 65 6d 61 5f 63 6f 6f 6b 69 65 20 29 7b  schema_cookie ){
24298 0a 20 20 20 20 20 20 20 20 20 20 61 6c 6c 4f 6b  .          allOk
24299 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a   = 0;.        }.
2429a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 42          sqlite3B
2429b 74 72 65 65 43 6c 6f 73 65 43 75 72 73 6f 72 28  treeCloseCursor(
2429c 63 75 72 54 65 6d 70 29 3b 0a 20 20 20 20 20 20  curTemp);.      
2429d 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  }.      if( rc==
2429e 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 7c 7c 20  SQLITE_NOMEM || 
2429f 72 63 3d 3d 53 51 4c 49 54 45 5f 49 4f 45 52 52  rc==SQLITE_IOERR
242a0 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20  _NOMEM ){.      
242a1 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
242a2 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a  ed = 1;.      }.
242a3 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
242a4 33 5f 66 72 65 65 28 63 75 72 54 65 6d 70 29 3b  3_free(curTemp);
242a5 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 6c  .  }else{.    al
242a6 6c 4f 6b 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  lOk = 0;.    db-
242a7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20  >mallocFailed = 
242a8 31 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  1;.  }..  return
242a9 20 61 6c 6c 4f 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a   allOk;.}../*.**
242aa 20 43 6f 6e 76 65 72 74 20 61 20 73 63 68 65 6d   Convert a schem
242ab 61 20 70 6f 69 6e 74 65 72 20 69 6e 74 6f 20 74  a pointer into t
242ac 68 65 20 69 44 62 20 69 6e 64 65 78 20 74 68 61  he iDb index tha
242ad 74 20 69 6e 64 69 63 61 74 65 73 0a 2a 2a 20 77  t indicates.** w
242ae 68 69 63 68 20 64 61 74 61 62 61 73 65 20 66 69  hich database fi
242af 6c 65 20 69 6e 20 64 62 2d 3e 61 44 62 5b 5d 20  le in db->aDb[] 
242b0 74 68 65 20 73 63 68 65 6d 61 20 72 65 66 65 72  the schema refer
242b1 73 20 74 6f 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  s to..**.** If t
242b2 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73 65  he same database
242b3 20 69 73 20 61 74 74 61 63 68 65 64 20 6d 6f 72   is attached mor
242b4 65 20 74 68 61 6e 20 6f 6e 63 65 2c 20 74 68 65  e than once, the
242b5 20 66 69 72 73 74 0a 2a 2a 20 61 74 74 61 63 68   first.** attach
242b6 65 64 20 64 61 74 61 62 61 73 65 20 69 73 20 72  ed database is r
242b7 65 74 75 72 6e 65 64 2e 0a 2a 2f 0a 53 51 4c 49  eturned..*/.SQLI
242b8 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
242b9 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
242ba 64 65 78 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  dex(sqlite3 *db,
242bb 20 53 63 68 65 6d 61 20 2a 70 53 63 68 65 6d 61   Schema *pSchema
242bc 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20 2d 31 30  ){.  int i = -10
242bd 30 30 30 30 30 3b 0a 0a 20 20 2f 2a 20 49 66 20  00000;..  /* If 
242be 70 53 63 68 65 6d 61 20 69 73 20 4e 55 4c 4c 2c  pSchema is NULL,
242bf 20 74 68 65 6e 20 72 65 74 75 72 6e 20 2d 31 30   then return -10
242c0 30 30 30 30 30 2e 20 54 68 69 73 20 68 61 70 70  00000. This happ
242c1 65 6e 73 20 77 68 65 6e 20 63 6f 64 65 20 69 6e  ens when code in
242c2 20 0a 20 20 2a 2a 20 65 78 70 72 2e 63 20 69 73   .  ** expr.c is
242c3 20 74 72 79 69 6e 67 20 74 6f 20 72 65 73 6f 6c   trying to resol
242c4 76 65 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  ve a reference t
242c5 6f 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61  o a transient ta
242c6 62 6c 65 20 28 69 2e 65 2e 20 6f 6e 65 0a 20 20  ble (i.e. one.  
242c7 2a 2a 20 63 72 65 61 74 65 64 20 62 79 20 61 20  ** created by a 
242c8 73 75 62 2d 73 65 6c 65 63 74 29 2e 20 49 6e 20  sub-select). In 
242c9 74 68 69 73 20 63 61 73 65 20 74 68 65 20 72 65  this case the re
242ca 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20 74 68  turn value of th
242cb 69 73 20 0a 20 20 2a 2a 20 66 75 6e 63 74 69 6f  is .  ** functio
242cc 6e 20 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62  n should never b
242cd 65 20 75 73 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a  e used..  **.  *
242ce 2a 20 57 65 20 72 65 74 75 72 6e 20 2d 31 30 30  * We return -100
242cf 30 30 30 30 20 69 6e 73 74 65 61 64 20 6f 66 20  0000 instead of 
242d0 74 68 65 20 6d 6f 72 65 20 75 73 75 61 6c 20 2d  the more usual -
242d1 31 20 73 69 6d 70 6c 79 20 62 65 63 61 75 73 65  1 simply because
242d2 20 75 73 69 6e 67 0a 20 20 2a 2a 20 2d 31 30 30   using.  ** -100
242d3 30 30 30 30 20 61 73 20 69 6e 63 6f 72 72 65 63  0000 as incorrec
242d4 74 6c 79 20 75 73 69 6e 67 20 2d 31 30 30 30 30  tly using -10000
242d5 30 30 20 69 6e 64 65 78 20 69 6e 74 6f 20 64 62  00 index into db
242d6 2d 3e 61 44 62 5b 5d 20 69 73 20 6d 75 63 68 20  ->aDb[] is much 
242d7 0a 20 20 2a 2a 20 6d 6f 72 65 20 6c 69 6b 65 6c  .  ** more likel
242d8 79 20 74 6f 20 63 61 75 73 65 20 61 20 73 65 67  y to cause a seg
242d9 66 61 75 6c 74 20 74 68 61 6e 20 2d 31 20 28 6f  fault than -1 (o
242da 66 20 63 6f 75 72 73 65 20 74 68 65 72 65 20 61  f course there a
242db 72 65 20 61 73 73 65 72 74 28 29 0a 20 20 2a 2a  re assert().  **
242dc 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 6f 2c   statements too,
242dd 20 62 75 74 20 69 74 20 6e 65 76 65 72 20 68 75   but it never hu
242de 72 74 73 20 74 6f 20 70 6c 61 79 20 74 68 65 20  rts to play the 
242df 6f 64 64 73 29 2e 0a 20 20 2a 2f 0a 20 20 61 73  odds)..  */.  as
242e0 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75  sert( sqlite3_mu
242e1 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74  tex_held(db->mut
242e2 65 78 29 20 29 3b 0a 20 20 69 66 28 20 70 53 63  ex) );.  if( pSc
242e3 68 65 6d 61 20 29 7b 0a 20 20 20 20 66 6f 72 28  hema ){.    for(
242e4 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62 3b 20  i=0; i<db->nDb; 
242e5 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20  i++){.      if( 
242e6 64 62 2d 3e 61 44 62 5b 69 5d 2e 70 53 63 68 65  db->aDb[i].pSche
242e7 6d 61 3d 3d 70 53 63 68 65 6d 61 20 29 7b 0a 20  ma==pSchema ){. 
242e8 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
242e9 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
242ea 61 73 73 65 72 74 28 20 69 3e 3d 30 20 26 26 69  assert( i>=0 &&i
242eb 3e 3d 30 20 26 26 20 20 69 3c 64 62 2d 3e 6e 44  >=0 &&  i<db->nD
242ec 62 20 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  b );.  }.  retur
242ed 6e 20 69 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f  n i;.}../*.** Co
242ee 6d 70 69 6c 65 20 74 68 65 20 55 54 46 2d 38 20  mpile the UTF-8 
242ef 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74  encoded SQL stat
242f0 65 6d 65 6e 74 20 7a 53 71 6c 20 69 6e 74 6f 20  ement zSql into 
242f1 61 20 73 74 61 74 65 6d 65 6e 74 20 68 61 6e 64  a statement hand
242f2 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le..*/.static in
242f3 74 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65  t sqlite3Prepare
242f4 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
242f5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
242f6 20 44 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   Database handle
242f7 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  . */.  const cha
242f8 72 20 2a 7a 53 71 6c 2c 20 20 20 20 20 20 20 20  r *zSql,        
242f9 20 2f 2a 20 55 54 46 2d 38 20 65 6e 63 6f 64 65   /* UTF-8 encode
242fa 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
242fb 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74 65 73   */.  int nBytes
242fc 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
242fd 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 53 71  /* Length of zSq
242fe 6c 20 69 6e 20 62 79 74 65 73 2e 20 2a 2f 0a 20  l in bytes. */. 
242ff 20 69 6e 74 20 73 61 76 65 53 71 6c 46 6c 61 67   int saveSqlFlag
24300 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72  ,          /* Tr
24301 75 65 20 74 6f 20 63 6f 70 79 20 53 51 4c 20 74  ue to copy SQL t
24302 65 78 74 20 69 6e 74 6f 20 74 68 65 20 73 71 6c  ext into the sql
24303 69 74 65 33 5f 73 74 6d 74 20 2a 2f 0a 20 20 73  ite3_stmt */.  s
24304 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70  qlite3_stmt **pp
24305 53 74 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a  Stmt,    /* OUT:
24306 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   A pointer to th
24307 65 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65  e prepared state
24308 6d 65 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  ment */.  const 
24309 63 68 61 72 20 2a 2a 70 7a 54 61 69 6c 20 20 20  char **pzTail   
2430a 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20      /* OUT: End 
2430b 6f 66 20 70 61 72 73 65 64 20 73 74 72 69 6e 67  of parsed string
2430c 20 2a 2f 0a 29 7b 0a 20 20 50 61 72 73 65 20 73   */.){.  Parse s
2430d 50 61 72 73 65 3b 0a 20 20 63 68 61 72 20 2a 7a  Parse;.  char *z
2430e 45 72 72 4d 73 67 20 3d 20 30 3b 0a 20 20 69 6e  ErrMsg = 0;.  in
2430f 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
24310 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 61 73  ;.  int i;..  as
24311 73 65 72 74 28 20 70 70 53 74 6d 74 20 29 3b 0a  sert( ppStmt );.
24312 20 20 2a 70 70 53 74 6d 74 20 3d 20 30 3b 0a 20    *ppStmt = 0;. 
24313 20 69 66 28 20 73 71 6c 69 74 65 33 53 61 66 65   if( sqlite3Safe
24314 74 79 4f 6e 28 64 62 29 20 29 7b 0a 20 20 20 20  tyOn(db) ){.    
24315 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49  return SQLITE_MI
24316 53 55 53 45 3b 0a 20 20 7d 0a 20 20 61 73 73 65  SUSE;.  }.  asse
24317 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
24318 61 69 6c 65 64 20 29 3b 0a 20 20 61 73 73 65 72  ailed );.  asser
24319 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
2431a 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
2431b 20 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61 6e 79   );..  /* If any
2431c 20 61 74 74 61 63 68 65 64 20 64 61 74 61 62 61   attached databa
2431d 73 65 20 73 63 68 65 6d 61 73 20 61 72 65 20 6c  se schemas are l
2431e 6f 63 6b 65 64 2c 20 64 6f 20 6e 6f 74 20 70 72  ocked, do not pr
2431f 6f 63 65 65 64 20 77 69 74 68 0a 20 20 2a 2a 20  oceed with.  ** 
24320 63 6f 6d 70 69 6c 61 74 69 6f 6e 2e 20 49 6e 73  compilation. Ins
24321 74 65 61 64 20 72 65 74 75 72 6e 20 53 51 4c 49  tead return SQLI
24322 54 45 5f 4c 4f 43 4b 45 44 20 69 6d 6d 65 64 69  TE_LOCKED immedi
24323 61 74 65 6c 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f  ately..  */.  fo
24324 72 28 69 3d 30 3b 20 69 3c 64 62 2d 3e 6e 44 62  r(i=0; i<db->nDb
24325 3b 20 69 2b 2b 29 20 7b 0a 20 20 20 20 42 74 72  ; i++) {.    Btr
24326 65 65 20 2a 70 42 74 20 3d 20 64 62 2d 3e 61 44  ee *pBt = db->aD
24327 62 5b 69 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66  b[i].pBt;.    if
24328 28 20 70 42 74 20 29 7b 0a 20 20 20 20 20 20 69  ( pBt ){.      i
24329 6e 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20  nt rc;.      rc 
2432a 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 53 63  = sqlite3BtreeSc
2432b 68 65 6d 61 4c 6f 63 6b 65 64 28 70 42 74 29 3b  hemaLocked(pBt);
2432c 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2432d 0a 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63  .        const c
2432e 68 61 72 20 2a 7a 44 62 20 3d 20 64 62 2d 3e 61  har *zDb = db->a
2432f 44 62 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20  Db[i].zName;.   
24330 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f       sqlite3Erro
24331 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4c 4f 43  r(db, SQLITE_LOC
24332 4b 45 44 2c 20 22 64 61 74 61 62 61 73 65 20 73  KED, "database s
24333 63 68 65 6d 61 20 69 73 20 6c 6f 63 6b 65 64 3a  chema is locked:
24334 20 25 73 22 2c 20 7a 44 62 29 3b 0a 20 20 20 20   %s", zDb);.    
24335 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
24336 33 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a  3SafetyOff(db);.
24337 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 73          return s
24338 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62  qlite3ApiExit(db
24339 2c 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45 44 29  , SQLITE_LOCKED)
2433a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2433b 20 20 7d 0a 20 20 0a 20 20 6d 65 6d 73 65 74 28    }.  .  memset(
2433c 26 73 50 61 72 73 65 2c 20 30 2c 20 73 69 7a 65  &sParse, 0, size
2433d 6f 66 28 73 50 61 72 73 65 29 29 3b 0a 20 20 73  of(sParse));.  s
2433e 50 61 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 20  Parse.db = db;. 
2433f 20 69 66 28 20 6e 42 79 74 65 73 3e 3d 30 20 26   if( nBytes>=0 &
24340 26 20 28 6e 42 79 74 65 73 3d 3d 30 20 7c 7c 20  & (nBytes==0 || 
24341 7a 53 71 6c 5b 6e 42 79 74 65 73 2d 31 5d 21 3d  zSql[nBytes-1]!=
24342 30 29 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a  0) ){.    char *
24343 7a 53 71 6c 43 6f 70 79 3b 0a 20 20 20 20 69 6e  zSqlCopy;.    in
24344 74 20 6d 78 4c 65 6e 20 3d 20 64 62 2d 3e 61 4c  t mxLen = db->aL
24345 69 6d 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49  imit[SQLITE_LIMI
24346 54 5f 53 51 4c 5f 4c 45 4e 47 54 48 5d 3b 0a 20  T_SQL_LENGTH];. 
24347 20 20 20 69 66 28 20 6e 42 79 74 65 73 3e 6d 78     if( nBytes>mx
24348 4c 65 6e 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Len ){.      sql
24349 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2434a 4c 49 54 45 5f 54 4f 4f 42 49 47 2c 20 22 73 74  LITE_TOOBIG, "st
2434b 61 74 65 6d 65 6e 74 20 74 6f 6f 20 6c 6f 6e 67  atement too long
2434c 22 29 3b 0a 20 20 20 20 20 20 28 76 6f 69 64 29  ");.      (void)
2434d 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66  sqlite3SafetyOff
2434e 28 64 62 29 3b 0a 20 20 20 20 20 20 72 65 74 75  (db);.      retu
2434f 72 6e 20 73 71 6c 69 74 65 33 41 70 69 45 78 69  rn sqlite3ApiExi
24350 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 54 4f 4f  t(db, SQLITE_TOO
24351 42 49 47 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  BIG);.    }.    
24352 7a 53 71 6c 43 6f 70 79 20 3d 20 73 71 6c 69 74  zSqlCopy = sqlit
24353 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20  e3DbStrNDup(db, 
24354 7a 53 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20  zSql, nBytes);. 
24355 20 20 20 69 66 28 20 7a 53 71 6c 43 6f 70 79 20     if( zSqlCopy 
24356 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
24357 52 75 6e 50 61 72 73 65 72 28 26 73 50 61 72 73  RunParser(&sPars
24358 65 2c 20 7a 53 71 6c 43 6f 70 79 2c 20 26 7a 45  e, zSqlCopy, &zE
24359 72 72 4d 73 67 29 3b 0a 20 20 20 20 20 20 73 71  rrMsg);.      sq
2435a 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2435b 7a 53 71 6c 43 6f 70 79 29 3b 0a 20 20 20 20 20  zSqlCopy);.     
2435c 20 73 50 61 72 73 65 2e 7a 54 61 69 6c 20 3d 20   sParse.zTail = 
2435d 26 7a 53 71 6c 5b 73 50 61 72 73 65 2e 7a 54 61  &zSql[sParse.zTa
2435e 69 6c 2d 7a 53 71 6c 43 6f 70 79 5d 3b 0a 20 20  il-zSqlCopy];.  
2435f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
24360 50 61 72 73 65 2e 7a 54 61 69 6c 20 3d 20 26 7a  Parse.zTail = &z
24361 53 71 6c 5b 6e 42 79 74 65 73 5d 3b 0a 20 20 20  Sql[nBytes];.   
24362 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20   }.  }else{.    
24363 73 71 6c 69 74 65 33 52 75 6e 50 61 72 73 65 72  sqlite3RunParser
24364 28 26 73 50 61 72 73 65 2c 20 7a 53 71 6c 2c 20  (&sParse, zSql, 
24365 26 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 0a 0a  &zErrMsg);.  }..
24366 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
24367 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 73 50  Failed ){.    sP
24368 61 72 73 65 2e 72 63 20 3d 20 53 51 4c 49 54 45  arse.rc = SQLITE
24369 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 69 66  _NOMEM;.  }.  if
2436a 28 20 73 50 61 72 73 65 2e 72 63 3d 3d 53 51 4c  ( sParse.rc==SQL
2436b 49 54 45 5f 44 4f 4e 45 20 29 20 73 50 61 72 73  ITE_DONE ) sPars
2436c 65 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  e.rc = SQLITE_OK
2436d 3b 0a 20 20 69 66 28 20 73 50 61 72 73 65 2e 63  ;.  if( sParse.c
2436e 68 65 63 6b 53 63 68 65 6d 61 20 26 26 20 21 73  heckSchema && !s
2436f 63 68 65 6d 61 49 73 56 61 6c 69 64 28 64 62 29  chemaIsValid(db)
24370 20 29 7b 0a 20 20 20 20 73 50 61 72 73 65 2e 72   ){.    sParse.r
24371 63 20 3d 20 53 51 4c 49 54 45 5f 53 43 48 45 4d  c = SQLITE_SCHEM
24372 41 3b 0a 20 20 7d 0a 20 20 69 66 28 20 73 50 61  A;.  }.  if( sPa
24373 72 73 65 2e 72 63 3d 3d 53 51 4c 49 54 45 5f 53  rse.rc==SQLITE_S
24374 43 48 45 4d 41 20 29 7b 0a 20 20 20 20 73 71 6c  CHEMA ){.    sql
24375 69 74 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61  ite3ResetInterna
24376 6c 53 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a  lSchema(db, 0);.
24377 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
24378 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
24379 20 20 73 50 61 72 73 65 2e 72 63 20 3d 20 53 51    sParse.rc = SQ
2437a 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a  LITE_NOMEM;.  }.
2437b 20 20 69 66 28 20 70 7a 54 61 69 6c 20 29 7b 0a    if( pzTail ){.
2437c 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 73 50      *pzTail = sP
2437d 61 72 73 65 2e 7a 54 61 69 6c 3b 0a 20 20 7d 0a  arse.zTail;.  }.
2437e 20 20 72 63 20 3d 20 73 50 61 72 73 65 2e 72 63    rc = sParse.rc
2437f 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
24380 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
24381 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
24382 4f 4b 20 26 26 20 73 50 61 72 73 65 2e 70 56 64  OK && sParse.pVd
24383 62 65 20 26 26 20 73 50 61 72 73 65 2e 65 78 70  be && sParse.exp
24384 6c 61 69 6e 20 29 7b 0a 20 20 20 20 69 66 28 20  lain ){.    if( 
24385 73 50 61 72 73 65 2e 65 78 70 6c 61 69 6e 3d 3d  sParse.explain==
24386 32 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  2 ){.      sqlit
24387 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73  e3VdbeSetNumCols
24388 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 33  (sParse.pVdbe, 3
24389 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2438a 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
2438b 50 61 72 73 65 2e 70 56 64 62 65 2c 20 30 2c 20  Parse.pVdbe, 0, 
2438c 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 6f  COLNAME_NAME, "o
2438d 72 64 65 72 22 2c 20 53 51 4c 49 54 45 5f 53 54  rder", SQLITE_ST
2438e 41 54 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c  ATIC);.      sql
2438f 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61  ite3VdbeSetColNa
24390 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  me(sParse.pVdbe,
24391 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   1, COLNAME_NAME
24392 2c 20 22 66 72 6f 6d 22 2c 20 53 51 4c 49 54 45  , "from", SQLITE
24393 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20 20  _STATIC);.      
24394 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f  sqlite3VdbeSetCo
24395 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64  lName(sParse.pVd
24396 62 65 2c 20 32 2c 20 43 4f 4c 4e 41 4d 45 5f 4e  be, 2, COLNAME_N
24397 41 4d 45 2c 20 22 64 65 74 61 69 6c 22 2c 20 53  AME, "detail", S
24398 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
24399 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2439a 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 4e 75  sqlite3VdbeSetNu
2439b 6d 43 6f 6c 73 28 73 50 61 72 73 65 2e 70 56 64  mCols(sParse.pVd
2439c 62 65 2c 20 38 29 3b 0a 20 20 20 20 20 20 73 71  be, 8);.      sq
2439d 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
2439e 61 6d 65 28 73 50 61 72 73 65 2e 70 56 64 62 65  ame(sParse.pVdbe
2439f 2c 20 30 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , 0, COLNAME_NAM
243a0 45 2c 20 22 61 64 64 72 22 2c 20 53 51 4c 49 54  E, "addr", SQLIT
243a1 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 20  E_STATIC);.     
243a2 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
243a3 6f 6c 4e 61 6d 65 28 73 50 61 72 73 65 2e 70 56  olName(sParse.pV
243a4 64 62 65 2c 20 31 2c 20 43 4f 4c 4e 41 4d 45 5f  dbe, 1, COLNAME_
243a5 4e 41 4d 45 2c 20 22 6f 70 63 6f 64 65 22 2c 20  NAME, "opcode", 
243a6 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
243a7 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
243a8 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61 72  eSetColName(sPar
243a9 73 65 2e 70 56 64 62 65 2c 20 32 2c 20 43 4f 4c  se.pVdbe, 2, COL
243aa 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 31 22 2c  NAME_NAME, "p1",
243ab 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b   SQLITE_STATIC);
243ac 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
243ad 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50 61  beSetColName(sPa
243ae 72 73 65 2e 70 56 64 62 65 2c 20 33 2c 20 43 4f  rse.pVdbe, 3, CO
243af 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 32 22  LNAME_NAME, "p2"
243b0 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29  , SQLITE_STATIC)
243b1 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
243b2 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73 50  dbeSetColName(sP
243b3 61 72 73 65 2e 70 56 64 62 65 2c 20 34 2c 20 43  arse.pVdbe, 4, C
243b4 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70 33  OLNAME_NAME, "p3
243b5 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ", SQLITE_STATIC
243b6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
243b7 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 73  VdbeSetColName(s
243b8 50 61 72 73 65 2e 70 56 64 62 65 2c 20 35 2c 20  Parse.pVdbe, 5, 
243b9 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22 70  COLNAME_NAME, "p
243ba 34 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  4", SQLITE_STATI
243bb 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  C);.      sqlite
243bc 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28  3VdbeSetColName(
243bd 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 36 2c  sParse.pVdbe, 6,
243be 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 22   COLNAME_NAME, "
243bf 70 35 22 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  p5", SQLITE_STAT
243c0 49 43 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  IC);.      sqlit
243c1 65 33 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65  e3VdbeSetColName
243c2 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c 20 37  (sParse.pVdbe, 7
243c3 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20  , COLNAME_NAME, 
243c4 22 63 6f 6d 6d 65 6e 74 22 2c 20 53 51 4c 49 54  "comment", SQLIT
243c5 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20 20 7d  E_STATIC);.    }
243c6 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
243c7 66 28 20 73 71 6c 69 74 65 33 53 61 66 65 74 79  f( sqlite3Safety
243c8 4f 66 66 28 64 62 29 20 29 7b 0a 20 20 20 20 72  Off(db) ){.    r
243c9 63 20 3d 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  c = SQLITE_MISUS
243ca 45 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 73 61  E;.  }..  if( sa
243cb 76 65 53 71 6c 46 6c 61 67 20 29 7b 0a 20 20 20  veSqlFlag ){.   
243cc 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 53   sqlite3VdbeSetS
243cd 71 6c 28 73 50 61 72 73 65 2e 70 56 64 62 65 2c  ql(sParse.pVdbe,
243ce 20 7a 53 71 6c 2c 20 73 50 61 72 73 65 2e 7a 54   zSql, sParse.zT
243cf 61 69 6c 20 2d 20 7a 53 71 6c 29 3b 0a 20 20 7d  ail - zSql);.  }
243d0 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
243d1 45 5f 4f 4b 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  E_OK || db->mall
243d2 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20  ocFailed ){.    
243d3 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
243d4 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29  ((sqlite3_stmt*)
243d5 73 50 61 72 73 65 2e 70 56 64 62 65 29 3b 0a 20  sParse.pVdbe);. 
243d6 20 20 20 61 73 73 65 72 74 28 21 28 2a 70 70 53     assert(!(*ppS
243d7 74 6d 74 29 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  tmt));.  }else{.
243d8 20 20 20 20 2a 70 70 53 74 6d 74 20 3d 20 28 73      *ppStmt = (s
243d9 71 6c 69 74 65 33 5f 73 74 6d 74 2a 29 73 50 61  qlite3_stmt*)sPa
243da 72 73 65 2e 70 56 64 62 65 3b 0a 20 20 7d 0a 0a  rse.pVdbe;.  }..
243db 20 20 69 66 28 20 7a 45 72 72 4d 73 67 20 29 7b    if( zErrMsg ){
243dc 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
243dd 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22 2c 20  r(db, rc, "%s", 
243de 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20 73 71  zErrMsg);.    sq
243df 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
243e0 7a 45 72 72 4d 73 67 29 3b 0a 20 20 7d 65 6c 73  zErrMsg);.  }els
243e1 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  e{.    sqlite3Er
243e2 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a  ror(db, rc, 0);.
243e3 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69    }..  rc = sqli
243e4 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72  te3ApiExit(db, r
243e5 63 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 72  c);.  assert( (r
243e6 63 26 64 62 2d 3e 65 72 72 4d 61 73 6b 29 3d 3d  c&db->errMask)==
243e7 72 63 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  rc );.  return r
243e8 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  c;.}.static int 
243e9 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72  sqlite3LockAndPr
243ea 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74 65 33  epare(.  sqlite3
243eb 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
243ec 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68     /* Database h
243ed 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73  andle. */.  cons
243ee 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20  t char *zSql,   
243ef 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65        /* UTF-8 e
243f0 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65  ncoded SQL state
243f1 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e  ment. */.  int n
243f2 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
243f3 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f       /* Length o
243f4 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e  f zSql in bytes.
243f5 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76 65 53 71   */.  int saveSq
243f6 6c 46 6c 61 67 2c 20 20 20 20 20 20 20 20 20 20  lFlag,          
243f7 2f 2a 20 54 72 75 65 20 74 6f 20 63 6f 70 79 20  /* True to copy 
243f8 53 51 4c 20 74 65 78 74 20 69 6e 74 6f 20 74 68  SQL text into th
243f9 65 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a  e sqlite3_stmt *
243fa 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74  /.  sqlite3_stmt
243fb 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f 2a   **ppStmt,    /*
243fc 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72 20   OUT: A pointer 
243fd 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64 20  to the prepared 
243fe 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 63  statement */.  c
243ff 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 61  onst char **pzTa
24400 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a  il       /* OUT:
24401 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20 73   End of parsed s
24402 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69 6e  tring */.){.  in
24403 74 20 72 63 3b 0a 20 20 69 66 28 20 21 73 71 6c  t rc;.  if( !sql
24404 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b 4f  ite3SafetyCheckO
24405 6b 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  k(db) ){.    ret
24406 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53  urn SQLITE_MISUS
24407 45 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  E;.  }.  sqlite3
24408 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
24409 3e 6d 75 74 65 78 29 3b 0a 20 20 73 71 6c 69 74  >mutex);.  sqlit
2440a 65 33 42 74 72 65 65 45 6e 74 65 72 41 6c 6c 28  e3BtreeEnterAll(
2440b 64 62 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69  db);.  rc = sqli
2440c 74 65 33 50 72 65 70 61 72 65 28 64 62 2c 20 7a  te3Prepare(db, z
2440d 53 71 6c 2c 20 6e 42 79 74 65 73 2c 20 73 61 76  Sql, nBytes, sav
2440e 65 53 71 6c 46 6c 61 67 2c 20 70 70 53 74 6d 74  eSqlFlag, ppStmt
2440f 2c 20 70 7a 54 61 69 6c 29 3b 0a 20 20 73 71 6c  , pzTail);.  sql
24410 69 74 65 33 42 74 72 65 65 4c 65 61 76 65 41 6c  ite3BtreeLeaveAl
24411 6c 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33  l(db);.  sqlite3
24412 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
24413 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72  >mutex);.  retur
24414 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n rc;.}../*.** R
24415 65 72 75 6e 20 74 68 65 20 63 6f 6d 70 69 6c 61  erun the compila
24416 74 69 6f 6e 20 6f 66 20 61 20 73 74 61 74 65 6d  tion of a statem
24417 65 6e 74 20 61 66 74 65 72 20 61 20 73 63 68 65  ent after a sche
24418 6d 61 20 63 68 61 6e 67 65 2e 0a 2a 2a 20 52 65  ma change..** Re
24419 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
2441a 20 73 74 61 74 65 6d 65 6e 74 20 77 61 73 20 72   statement was r
2441b 65 63 6f 6d 70 69 6c 65 64 20 73 75 63 63 65 73  ecompiled succes
2441c 73 66 75 6c 6c 79 2e 0a 2a 2a 20 52 65 74 75 72  sfully..** Retur
2441d 6e 20 66 61 6c 73 65 20 69 66 20 74 68 65 72 65  n false if there
2441e 20 69 73 20 61 6e 20 65 72 72 6f 72 20 6f 66 20   is an error of 
2441f 73 6f 6d 65 20 6b 69 6e 64 2e 0a 2a 2f 0a 53 51  some kind..*/.SQ
24420 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
24421 20 73 71 6c 69 74 65 33 52 65 70 72 65 70 61 72   sqlite3Reprepar
24422 65 28 56 64 62 65 20 2a 70 29 7b 0a 20 20 69 6e  e(Vdbe *p){.  in
24423 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f  t rc;.  sqlite3_
24424 73 74 6d 74 20 2a 70 4e 65 77 3b 0a 20 20 63 6f  stmt *pNew;.  co
24425 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 3b 0a  nst char *zSql;.
24426 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 0a    sqlite3 *db;..
24427 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
24428 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 73 71 6c  3_mutex_held(sql
24429 69 74 65 33 56 64 62 65 44 62 28 70 29 2d 3e 6d  ite3VdbeDb(p)->m
2442a 75 74 65 78 29 20 29 3b 0a 20 20 7a 53 71 6c 20  utex) );.  zSql 
2442b 3d 20 73 71 6c 69 74 65 33 5f 73 71 6c 28 28 73  = sqlite3_sql((s
2442c 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 29 70 29  qlite3_stmt *)p)
2442d 3b 0a 20 20 61 73 73 65 72 74 28 20 7a 53 71 6c  ;.  assert( zSql
2442e 21 3d 30 20 29 3b 20 20 2f 2a 20 52 65 70 72 65  !=0 );  /* Repre
2442f 70 61 72 65 20 6f 6e 6c 79 20 63 61 6c 6c 65 64  pare only called
24430 20 66 6f 72 20 70 72 65 70 61 72 65 5f 76 32 28   for prepare_v2(
24431 29 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a  ) statements */.
24432 20 20 64 62 20 3d 20 73 71 6c 69 74 65 33 56 64    db = sqlite3Vd
24433 62 65 44 62 28 70 29 3b 0a 20 20 61 73 73 65 72  beDb(p);.  asser
24434 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  t( sqlite3_mutex
24435 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65 78 29  _held(db->mutex)
24436 20 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   );.  rc = sqlit
24437 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70 61 72 65  e3LockAndPrepare
24438 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 30  (db, zSql, -1, 0
24439 2c 20 26 70 4e 65 77 2c 20 30 29 3b 0a 20 20 69  , &pNew, 0);.  i
2443a 66 28 20 72 63 20 29 7b 0a 20 20 20 20 69 66 28  f( rc ){.    if(
2443b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45   rc==SQLITE_NOME
2443c 4d 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  M ){.      db->m
2443d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
2443e 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65 72  .    }.    asser
2443f 74 28 20 70 4e 65 77 3d 3d 30 20 29 3b 0a 20 20  t( pNew==0 );.  
24440 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 65    return 0;.  }e
24441 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72 74 28  lse{.    assert(
24442 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 7d 0a   pNew!=0 );.  }.
24443 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 77 61    sqlite3VdbeSwa
24444 70 28 28 56 64 62 65 2a 29 70 4e 65 77 2c 20 70  p((Vdbe*)pNew, p
24445 29 3b 0a 20 20 73 71 6c 69 74 65 33 54 72 61 6e  );.  sqlite3Tran
24446 73 66 65 72 42 69 6e 64 69 6e 67 73 28 70 4e 65  sferBindings(pNe
24447 77 2c 20 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  w, (sqlite3_stmt
24448 2a 29 70 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  *)p);.  sqlite3V
24449 64 62 65 52 65 73 65 74 53 74 65 70 52 65 73 75  dbeResetStepResu
2444a 6c 74 28 28 56 64 62 65 2a 29 70 4e 65 77 29 3b  lt((Vdbe*)pNew);
2444b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 46 69  .  sqlite3VdbeFi
2444c 6e 61 6c 69 7a 65 28 28 56 64 62 65 2a 29 70 4e  nalize((Vdbe*)pN
2444d 65 77 29 3b 0a 20 20 72 65 74 75 72 6e 20 31 3b  ew);.  return 1;
2444e 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f 20 76  .}.../*.** Two v
2444f 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65 20 6f  ersions of the o
24450 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20 4c 65  fficial API.  Le
24451 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75 73 65  gacy and new use
24452 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61 63 79  .  In the legacy
24453 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74 68 65  .** version, the
24454 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20 74 65   original SQL te
24455 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65 64 20  xt is not saved 
24456 69 6e 20 74 68 65 20 70 72 65 70 61 72 65 64 20  in the prepared 
24457 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61 6e 64  statement.** and
24458 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d 61 20   so if a schema 
24459 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c 20 53  change occurs, S
2445a 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69 73 20  QLITE_SCHEMA is 
2445b 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a 20 73  returned by.** s
2445c 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e 20 20  qlite3_step().  
2445d 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72 73 69  In the new versi
2445e 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  on, the original
2445f 20 53 51 4c 20 74 65 78 74 20 69 73 20 72 65 74   SQL text is ret
24460 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74 68 65  ained.** and the
24461 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20 61 75   statement is au
24462 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65 63 6f  tomatically reco
24463 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73 63 68  mpiled if an sch
24464 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20 6f 63  ema change.** oc
24465 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  curs..*/.SQLITE_
24466 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
24467 70 72 65 70 61 72 65 28 0a 20 20 73 71 6c 69 74  prepare(.  sqlit
24468 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
24469 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
2446a 20 68 61 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f   handle. */.  co
2446b 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20  nst char *zSql, 
2446c 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
2446d 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
2446e 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
2446f 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
24470 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
24471 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
24472 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
24473 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
24474 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
24475 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
24476 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
24477 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
24478 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
24479 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
2447a 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
2447b 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
2447c 20 73 71 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50   sqlite3LockAndP
2447d 72 65 70 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e  repare(db,zSql,n
2447e 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70  Bytes,0,ppStmt,p
2447f 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74  zTail);.  assert
24480 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
24481 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20  || ppStmt==0 || 
24482 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f  *ppStmt==0 );  /
24483 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31  * VERIFY: F13021
24484 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
24485 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
24486 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
24487 65 5f 76 32 28 0a 20 20 73 71 6c 69 74 65 33 20  e_v2(.  sqlite3 
24488 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
24489 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 68 61    /* Database ha
2448a 6e 64 6c 65 2e 20 2a 2f 0a 20 20 63 6f 6e 73 74  ndle. */.  const
2448b 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20 20 20   char *zSql,    
2448c 20 20 20 20 20 2f 2a 20 55 54 46 2d 38 20 65 6e       /* UTF-8 en
2448d 63 6f 64 65 64 20 53 51 4c 20 73 74 61 74 65 6d  coded SQL statem
2448e 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74 20 6e 42  ent. */.  int nB
2448f 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20 20  ytes,           
24490 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66      /* Length of
24491 20 7a 53 71 6c 20 69 6e 20 62 79 74 65 73 2e 20   zSql in bytes. 
24492 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  */.  sqlite3_stm
24493 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20 20 2f  t **ppStmt,    /
24494 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74 65 72  * OUT: A pointer
24495 20 74 6f 20 74 68 65 20 70 72 65 70 61 72 65 64   to the prepared
24496 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
24497 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a 70 7a 54  const char **pzT
24498 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f 55 54  ail       /* OUT
24499 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65 64 20  : End of parsed 
2449a 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20 20 69  string */.){.  i
2449b 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d 20 73 71  nt rc;.  rc = sq
2449c 6c 69 74 65 33 4c 6f 63 6b 41 6e 64 50 72 65 70  lite3LockAndPrep
2449d 61 72 65 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74  are(db,zSql,nByt
2449e 65 73 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61  es,1,ppStmt,pzTa
2449f 69 6c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72  il);.  assert( r
244a0 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20  c==SQLITE_OK || 
244a1 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70  ppStmt==0 || *pp
244a2 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56  Stmt==0 );  /* V
244a3 45 52 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f  ERIFY: F13021 */
244a4 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
244a5 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
244a6 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
244a7 2a 20 43 6f 6d 70 69 6c 65 20 74 68 65 20 55 54  * Compile the UT
244a8 46 2d 31 36 20 65 6e 63 6f 64 65 64 20 53 51 4c  F-16 encoded SQL
244a9 20 73 74 61 74 65 6d 65 6e 74 20 7a 53 71 6c 20   statement zSql 
244aa 69 6e 74 6f 20 61 20 73 74 61 74 65 6d 65 6e 74  into a statement
244ab 20 68 61 6e 64 6c 65 2e 0a 2a 2f 0a 73 74 61 74   handle..*/.stat
244ac 69 63 20 69 6e 74 20 73 71 6c 69 74 65 33 50 72  ic int sqlite3Pr
244ad 65 70 61 72 65 31 36 28 0a 20 20 73 71 6c 69 74  epare16(.  sqlit
244ae 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20  e3 *db,         
244af 20 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65       /* Database
244b0 20 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63   handle. */ .  c
244b1 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c  onst void *zSql,
244b2 20 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d           /* UTF-
244b3 38 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74  8 encoded SQL st
244b4 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e  atement. */.  in
244b5 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20  t nBytes,       
244b6 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
244b7 68 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74  h of zSql in byt
244b8 65 73 2e 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  es. */.  int sav
244b9 65 53 71 6c 46 6c 61 67 2c 20 20 20 20 20 20 20  eSqlFlag,       
244ba 20 20 20 2f 2a 20 54 72 75 65 20 74 6f 20 73 61     /* True to sa
244bb 76 65 20 53 51 4c 20 74 65 78 74 20 69 6e 74 6f  ve SQL text into
244bc 20 74 68 65 20 73 71 6c 69 74 65 33 5f 73 74 6d   the sqlite3_stm
244bd 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73  t */.  sqlite3_s
244be 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20 20  tmt **ppStmt,   
244bf 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e 74   /* OUT: A point
244c0 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61 72  er to the prepar
244c1 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a  ed statement */.
244c2 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a 70    const void **p
244c3 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20 4f  zTail       /* O
244c4 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73 65  UT: End of parse
244c5 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a 20  d string */.){. 
244c6 20 2f 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f   /* This functio
244c7 6e 20 63 75 72 72 65 6e 74 6c 79 20 77 6f 72 6b  n currently work
244c8 73 20 62 79 20 66 69 72 73 74 20 74 72 61 6e 73  s by first trans
244c9 66 6f 72 6d 69 6e 67 20 74 68 65 20 55 54 46 2d  forming the UTF-
244ca 31 36 0a 20 20 2a 2a 20 65 6e 63 6f 64 65 64 20  16.  ** encoded 
244cb 73 74 72 69 6e 67 20 74 6f 20 55 54 46 2d 38 2c  string to UTF-8,
244cc 20 74 68 65 6e 20 69 6e 76 6f 6b 69 6e 67 20 73   then invoking s
244cd 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 28 29  qlite3_prepare()
244ce 2e 20 54 68 65 0a 20 20 2a 2a 20 74 72 69 63 6b  . The.  ** trick
244cf 79 20 62 69 74 20 69 73 20 66 69 67 75 72 69 6e  y bit is figurin
244d0 67 20 6f 75 74 20 74 68 65 20 70 6f 69 6e 74 65  g out the pointe
244d1 72 20 74 6f 20 72 65 74 75 72 6e 20 69 6e 20 2a  r to return in *
244d2 70 7a 54 61 69 6c 2e 0a 20 20 2a 2f 0a 20 20 63  pzTail..  */.  c
244d3 68 61 72 20 2a 7a 53 71 6c 38 3b 0a 20 20 63 6f  har *zSql8;.  co
244d4 6e 73 74 20 63 68 61 72 20 2a 7a 54 61 69 6c 38  nst char *zTail8
244d5 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 20 3d   = 0;.  int rc =
244d6 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 69   SQLITE_OK;..  i
244d7 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
244d8 79 43 68 65 63 6b 4f 6b 28 64 62 29 20 29 7b 0a  yCheckOk(db) ){.
244d9 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
244da 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
244db 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
244dc 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
244dd 20 20 7a 53 71 6c 38 20 3d 20 73 71 6c 69 74 65    zSql8 = sqlite
244de 33 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 53  3Utf16to8(db, zS
244df 71 6c 2c 20 6e 42 79 74 65 73 29 3b 0a 20 20 69  ql, nBytes);.  i
244e0 66 28 20 7a 53 71 6c 38 20 29 7b 0a 20 20 20 20  f( zSql8 ){.    
244e1 72 63 20 3d 20 73 71 6c 69 74 65 33 4c 6f 63 6b  rc = sqlite3Lock
244e2 41 6e 64 50 72 65 70 61 72 65 28 64 62 2c 20 7a  AndPrepare(db, z
244e3 53 71 6c 38 2c 20 2d 31 2c 20 73 61 76 65 53 71  Sql8, -1, saveSq
244e4 6c 46 6c 61 67 2c 20 70 70 53 74 6d 74 2c 20 26  lFlag, ppStmt, &
244e5 7a 54 61 69 6c 38 29 3b 0a 20 20 7d 0a 0a 20 20  zTail8);.  }..  
244e6 69 66 28 20 7a 54 61 69 6c 38 20 26 26 20 70 7a  if( zTail8 && pz
244e7 54 61 69 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 49  Tail ){.    /* I
244e8 66 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  f sqlite3_prepar
244e9 65 20 72 65 74 75 72 6e 73 20 61 20 74 61 69 6c  e returns a tail
244ea 20 70 6f 69 6e 74 65 72 2c 20 77 65 20 63 61 6c   pointer, we cal
244eb 63 75 6c 61 74 65 20 74 68 65 0a 20 20 20 20 2a  culate the.    *
244ec 2a 20 65 71 75 69 76 61 6c 65 6e 74 20 70 6f 69  * equivalent poi
244ed 6e 74 65 72 20 69 6e 74 6f 20 74 68 65 20 55 54  nter into the UT
244ee 46 2d 31 36 20 73 74 72 69 6e 67 20 62 79 20 63  F-16 string by c
244ef 6f 75 6e 74 69 6e 67 20 74 68 65 20 75 6e 69 63  ounting the unic
244f0 6f 64 65 0a 20 20 20 20 2a 2a 20 63 68 61 72 61  ode.    ** chara
244f1 63 74 65 72 73 20 62 65 74 77 65 65 6e 20 7a 53  cters between zS
244f2 71 6c 38 20 61 6e 64 20 7a 54 61 69 6c 38 2c 20  ql8 and zTail8, 
244f3 61 6e 64 20 74 68 65 6e 20 72 65 74 75 72 6e 69  and then returni
244f4 6e 67 20 61 20 70 6f 69 6e 74 65 72 0a 20 20 20  ng a pointer.   
244f5 20 2a 2a 20 74 68 65 20 73 61 6d 65 20 6e 75 6d   ** the same num
244f6 62 65 72 20 6f 66 20 63 68 61 72 61 63 74 65 72  ber of character
244f7 73 20 69 6e 74 6f 20 74 68 65 20 55 54 46 2d 31  s into the UTF-1
244f8 36 20 73 74 72 69 6e 67 2e 0a 20 20 20 20 2a 2f  6 string..    */
244f9 0a 20 20 20 20 69 6e 74 20 63 68 61 72 73 5f 70  .    int chars_p
244fa 61 72 73 65 64 20 3d 20 73 71 6c 69 74 65 33 55  arsed = sqlite3U
244fb 74 66 38 43 68 61 72 4c 65 6e 28 7a 53 71 6c 38  tf8CharLen(zSql8
244fc 2c 20 7a 54 61 69 6c 38 2d 7a 53 71 6c 38 29 3b  , zTail8-zSql8);
244fd 0a 20 20 20 20 2a 70 7a 54 61 69 6c 20 3d 20 28  .    *pzTail = (
244fe 75 38 20 2a 29 7a 53 71 6c 20 2b 20 73 71 6c 69  u8 *)zSql + sqli
244ff 74 65 33 55 74 66 31 36 42 79 74 65 4c 65 6e 28  te3Utf16ByteLen(
24500 7a 53 71 6c 2c 20 63 68 61 72 73 5f 70 61 72 73  zSql, chars_pars
24501 65 64 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  ed);.  }.  sqlit
24502 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 53 71  e3DbFree(db, zSq
24503 6c 38 29 3b 20 0a 20 20 72 63 20 3d 20 73 71 6c  l8); .  rc = sql
24504 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c 20  ite3ApiExit(db, 
24505 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  rc);.  sqlite3_m
24506 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d  utex_leave(db->m
24507 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20  utex);.  return 
24508 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77 6f  rc;.}../*.** Two
24509 20 76 65 72 73 69 6f 6e 73 20 6f 66 20 74 68 65   versions of the
2450a 20 6f 66 66 69 63 69 61 6c 20 41 50 49 2e 20 20   official API.  
2450b 4c 65 67 61 63 79 20 61 6e 64 20 6e 65 77 20 75  Legacy and new u
2450c 73 65 2e 20 20 49 6e 20 74 68 65 20 6c 65 67 61  se.  In the lega
2450d 63 79 0a 2a 2a 20 76 65 72 73 69 6f 6e 2c 20 74  cy.** version, t
2450e 68 65 20 6f 72 69 67 69 6e 61 6c 20 53 51 4c 20  he original SQL 
2450f 74 65 78 74 20 69 73 20 6e 6f 74 20 73 61 76 65  text is not save
24510 64 20 69 6e 20 74 68 65 20 70 72 65 70 61 72 65  d in the prepare
24511 64 20 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 61  d statement.** a
24512 6e 64 20 73 6f 20 69 66 20 61 20 73 63 68 65 6d  nd so if a schem
24513 61 20 63 68 61 6e 67 65 20 6f 63 63 75 72 73 2c  a change occurs,
24514 20 53 51 4c 49 54 45 5f 53 43 48 45 4d 41 20 69   SQLITE_SCHEMA i
24515 73 20 72 65 74 75 72 6e 65 64 20 62 79 0a 2a 2a  s returned by.**
24516 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 2e   sqlite3_step().
24517 20 20 49 6e 20 74 68 65 20 6e 65 77 20 76 65 72    In the new ver
24518 73 69 6f 6e 2c 20 74 68 65 20 6f 72 69 67 69 6e  sion, the origin
24519 61 6c 20 53 51 4c 20 74 65 78 74 20 69 73 20 72  al SQL text is r
2451a 65 74 61 69 6e 65 64 0a 2a 2a 20 61 6e 64 20 74  etained.** and t
2451b 68 65 20 73 74 61 74 65 6d 65 6e 74 20 69 73 20  he statement is 
2451c 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 72 65  automatically re
2451d 63 6f 6d 70 69 6c 65 64 20 69 66 20 61 6e 20 73  compiled if an s
2451e 63 68 65 6d 61 20 63 68 61 6e 67 65 0a 2a 2a 20  chema change.** 
2451f 6f 63 63 75 72 73 2e 0a 2a 2f 0a 53 51 4c 49 54  occurs..*/.SQLIT
24520 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
24521 33 5f 70 72 65 70 61 72 65 31 36 28 0a 20 20 73  3_prepare16(.  s
24522 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20  qlite3 *db,     
24523 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
24524 62 61 73 65 20 68 61 6e 64 6c 65 2e 20 2a 2f 20  base handle. */ 
24525 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
24526 53 71 6c 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  Sql,         /* 
24527 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20 53 51  UTF-8 encoded SQ
24528 4c 20 73 74 61 74 65 6d 65 6e 74 2e 20 2a 2f 0a  L statement. */.
24529 20 20 69 6e 74 20 6e 42 79 74 65 73 2c 20 20 20    int nBytes,   
2452a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2452b 65 6e 67 74 68 20 6f 66 20 7a 53 71 6c 20 69 6e  ength of zSql in
2452c 20 62 79 74 65 73 2e 20 2a 2f 0a 20 20 73 71 6c   bytes. */.  sql
2452d 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
2452e 6d 74 2c 20 20 20 20 2f 2a 20 4f 55 54 3a 20 41  mt,    /* OUT: A
2452f 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
24530 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d 65  prepared stateme
24531 6e 74 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f  nt */.  const vo
24532 69 64 20 2a 2a 70 7a 54 61 69 6c 20 20 20 20 20  id **pzTail     
24533 20 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 20 6f 66    /* OUT: End of
24534 20 70 61 72 73 65 64 20 73 74 72 69 6e 67 20 2a   parsed string *
24535 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  /.){.  int rc;. 
24536 20 72 63 20 3d 20 73 71 6c 69 74 65 33 50 72 65   rc = sqlite3Pre
24537 70 61 72 65 31 36 28 64 62 2c 7a 53 71 6c 2c 6e  pare16(db,zSql,n
24538 42 79 74 65 73 2c 30 2c 70 70 53 74 6d 74 2c 70  Bytes,0,ppStmt,p
24539 7a 54 61 69 6c 29 3b 0a 20 20 61 73 73 65 72 74  zTail);.  assert
2453a 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
2453b 7c 7c 20 70 70 53 74 6d 74 3d 3d 30 20 7c 7c 20  || ppStmt==0 || 
2453c 2a 70 70 53 74 6d 74 3d 3d 30 20 29 3b 20 20 2f  *ppStmt==0 );  /
2453d 2a 20 56 45 52 49 46 59 3a 20 46 31 33 30 32 31  * VERIFY: F13021
2453e 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
2453f 0a 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  .}.SQLITE_API in
24540 74 20 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72  t sqlite3_prepar
24541 65 31 36 5f 76 32 28 0a 20 20 73 71 6c 69 74 65  e16_v2(.  sqlite
24542 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20  3 *db,          
24543 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
24544 68 61 6e 64 6c 65 2e 20 2a 2f 20 0a 20 20 63 6f  handle. */ .  co
24545 6e 73 74 20 76 6f 69 64 20 2a 7a 53 71 6c 2c 20  nst void *zSql, 
24546 20 20 20 20 20 20 20 20 2f 2a 20 55 54 46 2d 38          /* UTF-8
24547 20 65 6e 63 6f 64 65 64 20 53 51 4c 20 73 74 61   encoded SQL sta
24548 74 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 69 6e 74  tement. */.  int
24549 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 20 20   nBytes,        
2454a 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74 68         /* Length
2454b 20 6f 66 20 7a 53 71 6c 20 69 6e 20 62 79 74 65   of zSql in byte
2454c 73 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  s. */.  sqlite3_
2454d 73 74 6d 74 20 2a 2a 70 70 53 74 6d 74 2c 20 20  stmt **ppStmt,  
2454e 20 20 2f 2a 20 4f 55 54 3a 20 41 20 70 6f 69 6e    /* OUT: A poin
2454f 74 65 72 20 74 6f 20 74 68 65 20 70 72 65 70 61  ter to the prepa
24550 72 65 64 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f  red statement */
24551 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 2a  .  const void **
24552 70 7a 54 61 69 6c 20 20 20 20 20 20 20 2f 2a 20  pzTail       /* 
24553 4f 55 54 3a 20 45 6e 64 20 6f 66 20 70 61 72 73  OUT: End of pars
24554 65 64 20 73 74 72 69 6e 67 20 2a 2f 0a 29 7b 0a  ed string */.){.
24555 20 20 69 6e 74 20 72 63 3b 0a 20 20 72 63 20 3d    int rc;.  rc =
24556 20 73 71 6c 69 74 65 33 50 72 65 70 61 72 65 31   sqlite3Prepare1
24557 36 28 64 62 2c 7a 53 71 6c 2c 6e 42 79 74 65 73  6(db,zSql,nBytes
24558 2c 31 2c 70 70 53 74 6d 74 2c 70 7a 54 61 69 6c  ,1,ppStmt,pzTail
24559 29 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d  );.  assert( rc=
2455a 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 70 70  =SQLITE_OK || pp
2455b 53 74 6d 74 3d 3d 30 20 7c 7c 20 2a 70 70 53 74  Stmt==0 || *ppSt
2455c 6d 74 3d 3d 30 20 29 3b 20 20 2f 2a 20 56 45 52  mt==0 );  /* VER
2455d 49 46 59 3a 20 46 31 33 30 32 31 20 2a 2f 0a 20  IFY: F13021 */. 
2455e 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23   return rc;.}..#
2455f 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
24560 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
24561 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45  ************** E
24562 6e 64 20 6f 66 20 70 72 65 70 61 72 65 2e 63 20  nd of prepare.c 
24563 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24564 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24565 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
24566 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42  ************** B
24567 65 67 69 6e 20 66 69 6c 65 20 73 65 6c 65 63 74  egin file select
24568 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
24569 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2456a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2456b 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d  *.** 2001 Septem
2456c 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65  ber 15.**.** The
2456d 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d   author disclaim
2456e 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74  s copyright to t
2456f 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e  his source code.
24570 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a    In place of.**
24571 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c   a legal notice,
24572 20 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73   here is a bless
24573 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61  ing:.**.**    Ma
24574 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e  y you do good an
24575 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20  d not evil..**  
24576 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66    May you find f
24577 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79  orgiveness for y
24578 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67  ourself and forg
24579 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20  ive others..**  
2457a 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20    May you share 
2457b 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61  freely, never ta
2457c 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79  king more than y
2457d 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a  ou give..**.****
2457e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2457f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24581 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
24582 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69  *****.** This fi
24583 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63 6f  le contains C co
24584 64 65 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74  de routines that
24585 20 61 72 65 20 63 61 6c 6c 65 64 20 62 79 20 74   are called by t
24586 68 65 20 70 61 72 73 65 72 0a 2a 2a 20 74 6f 20  he parser.** to 
24587 68 61 6e 64 6c 65 20 53 45 4c 45 43 54 20 73 74  handle SELECT st
24588 61 74 65 6d 65 6e 74 73 20 69 6e 20 53 51 4c 69  atements in SQLi
24589 74 65 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 73  te..**.** $Id: s
2458a 65 6c 65 63 74 2e 63 2c 76 20 31 2e 34 38 36 20  elect.c,v 1.486 
2458b 32 30 30 38 2f 31 31 2f 31 39 20 30 39 3a 30 35  2008/11/19 09:05
2458c 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :27 danielk1977 
2458d 45 78 70 20 24 0a 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a  Exp $.*/.../*.**
2458e 20 44 65 6c 65 74 65 20 61 6c 6c 20 74 68 65 20   Delete all the 
2458f 63 6f 6e 74 65 6e 74 20 6f 66 20 61 20 53 65 6c  content of a Sel
24590 65 63 74 20 73 74 72 75 63 74 75 72 65 20 62 75  ect structure bu
24591 74 20 64 6f 20 6e 6f 74 20 64 65 61 6c 6c 6f 63  t do not dealloc
24592 61 74 65 0a 2a 2a 20 74 68 65 20 73 65 6c 65 63  ate.** the selec
24593 74 20 73 74 72 75 63 74 75 72 65 20 69 74 73 65  t structure itse
24594 6c 66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  lf..*/.static vo
24595 69 64 20 63 6c 65 61 72 53 65 6c 65 63 74 28 73  id clearSelect(s
24596 71 6c 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65  qlite3 *db, Sele
24597 63 74 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74 65  ct *p){.  sqlite
24598 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
24599 64 62 2c 20 70 2d 3e 70 45 4c 69 73 74 29 3b 0a  db, p->pEList);.
2459a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
2459b 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 53  Delete(db, p->pS
2459c 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  rc);.  sqlite3Ex
2459d 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e  prDelete(db, p->
2459e 70 57 68 65 72 65 29 3b 0a 20 20 73 71 6c 69 74  pWhere);.  sqlit
2459f 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65  e3ExprListDelete
245a0 28 64 62 2c 20 70 2d 3e 70 47 72 6f 75 70 42 79  (db, p->pGroupBy
245a1 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
245a2 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 48  Delete(db, p->pH
245a3 61 76 69 6e 67 29 3b 0a 20 20 73 71 6c 69 74 65  aving);.  sqlite
245a4 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28  3ExprListDelete(
245a5 64 62 2c 20 70 2d 3e 70 4f 72 64 65 72 42 79 29  db, p->pOrderBy)
245a6 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
245a7 74 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70  tDelete(db, p->p
245a8 50 72 69 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65  Prior);.  sqlite
245a9 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
245aa 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20 20 73 71  p->pLimit);.  sq
245ab 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28  lite3ExprDelete(
245ac 64 62 2c 20 70 2d 3e 70 4f 66 66 73 65 74 29 3b  db, p->pOffset);
245ad 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  .}../*.** Initia
245ae 6c 69 7a 65 20 61 20 53 65 6c 65 63 74 44 65 73  lize a SelectDes
245af 74 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  t structure..*/.
245b0 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
245b1 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
245b2 74 44 65 73 74 49 6e 69 74 28 53 65 6c 65 63 74  tDestInit(Select
245b3 44 65 73 74 20 2a 70 44 65 73 74 2c 20 69 6e 74  Dest *pDest, int
245b4 20 65 44 65 73 74 2c 20 69 6e 74 20 69 50 61 72   eDest, int iPar
245b5 6d 29 7b 0a 20 20 70 44 65 73 74 2d 3e 65 44 65  m){.  pDest->eDe
245b6 73 74 20 3d 20 65 44 65 73 74 3b 0a 20 20 70 44  st = eDest;.  pD
245b7 65 73 74 2d 3e 69 50 61 72 6d 20 3d 20 69 50 61  est->iParm = iPa
245b8 72 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 61 66 66  rm;.  pDest->aff
245b9 69 6e 69 74 79 20 3d 20 30 3b 0a 20 20 70 44 65  inity = 0;.  pDe
245ba 73 74 2d 3e 69 4d 65 6d 20 3d 20 30 3b 0a 20 20  st->iMem = 0;.  
245bb 70 44 65 73 74 2d 3e 6e 4d 65 6d 20 3d 20 30 3b  pDest->nMem = 0;
245bc 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63  .}.../*.** Alloc
245bd 61 74 65 20 61 20 6e 65 77 20 53 65 6c 65 63 74  ate a new Select
245be 20 73 74 72 75 63 74 75 72 65 20 61 6e 64 20 72   structure and r
245bf 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20  eturn a pointer 
245c0 74 6f 20 74 68 61 74 0a 2a 2a 20 73 74 72 75 63  to that.** struc
245c1 74 75 72 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ture..*/.SQLITE_
245c2 50 52 49 56 41 54 45 20 53 65 6c 65 63 74 20 2a  PRIVATE Select *
245c3 73 71 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77  sqlite3SelectNew
245c4 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
245c5 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
245c6 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
245c7 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
245c8 73 74 2c 20 20 20 20 20 2f 2a 20 77 68 69 63 68  st,     /* which
245c9 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 69 6e 63 6c   columns to incl
245ca 75 64 65 20 69 6e 20 74 68 65 20 72 65 73 75 6c  ude in the resul
245cb 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
245cc 70 53 72 63 2c 20 20 20 20 20 20 20 20 2f 2a 20  pSrc,        /* 
245cd 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
245ce 2d 2d 20 77 68 69 63 68 20 74 61 62 6c 65 73 20  -- which tables 
245cf 74 6f 20 73 63 61 6e 20 2a 2f 0a 20 20 45 78 70  to scan */.  Exp
245d0 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
245d1 20 20 20 2f 2a 20 74 68 65 20 57 48 45 52 45 20     /* the WHERE 
245d2 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
245d3 4c 69 73 74 20 2a 70 47 72 6f 75 70 42 79 2c 20  List *pGroupBy, 
245d4 20 20 2f 2a 20 74 68 65 20 47 52 4f 55 50 20 42    /* the GROUP B
245d5 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  Y clause */.  Ex
245d6 70 72 20 2a 70 48 61 76 69 6e 67 2c 20 20 20 20  pr *pHaving,    
245d7 20 20 20 20 2f 2a 20 74 68 65 20 48 41 56 49 4e      /* the HAVIN
245d8 47 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78  G clause */.  Ex
245d9 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
245da 2c 20 20 20 2f 2a 20 74 68 65 20 4f 52 44 45 52  ,   /* the ORDER
245db 20 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20   BY clause */.  
245dc 69 6e 74 20 69 73 44 69 73 74 69 6e 63 74 2c 20  int isDistinct, 
245dd 20 20 20 20 20 20 2f 2a 20 74 72 75 65 20 69 66        /* true if
245de 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65   the DISTINCT ke
245df 79 77 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74  yword is present
245e0 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4c 69 6d   */.  Expr *pLim
245e1 69 74 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 4c  it,         /* L
245e2 49 4d 49 54 20 76 61 6c 75 65 2e 20 20 4e 55 4c  IMIT value.  NUL
245e3 4c 20 6d 65 61 6e 73 20 6e 6f 74 20 75 73 65 64  L means not used
245e4 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 4f 66 66   */.  Expr *pOff
245e5 73 65 74 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  set         /* O
245e6 46 46 53 45 54 20 76 61 6c 75 65 2e 20 20 4e 55  FFSET value.  NU
245e7 4c 4c 20 6d 65 61 6e 73 20 6e 6f 20 6f 66 66 73  LL means no offs
245e8 65 74 20 2a 2f 0a 29 7b 0a 20 20 53 65 6c 65 63  et */.){.  Selec
245e9 74 20 2a 70 4e 65 77 3b 0a 20 20 53 65 6c 65 63  t *pNew;.  Selec
245ea 74 20 73 74 61 6e 64 69 6e 3b 0a 20 20 73 71 6c  t standin;.  sql
245eb 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
245ec 65 2d 3e 64 62 3b 0a 20 20 70 4e 65 77 20 3d 20  e->db;.  pNew = 
245ed 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a  sqlite3DbMallocZ
245ee 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 2a  ero(db, sizeof(*
245ef 70 4e 65 77 29 20 29 3b 0a 20 20 61 73 73 65 72  pNew) );.  asser
245f0 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  t( db->mallocFai
245f1 6c 65 64 20 7c 7c 20 21 70 4f 66 66 73 65 74 20  led || !pOffset 
245f2 7c 7c 20 70 4c 69 6d 69 74 20 29 3b 20 2f 2a 20  || pLimit ); /* 
245f3 4f 46 46 53 45 54 20 69 6d 70 6c 69 65 73 20 4c  OFFSET implies L
245f4 49 4d 49 54 20 2a 2f 0a 20 20 69 66 28 20 70 4e  IMIT */.  if( pN
245f5 65 77 3d 3d 30 20 29 7b 0a 20 20 20 20 70 4e 65  ew==0 ){.    pNe
245f6 77 20 3d 20 26 73 74 61 6e 64 69 6e 3b 0a 20 20  w = &standin;.  
245f7 20 20 6d 65 6d 73 65 74 28 70 4e 65 77 2c 20 30    memset(pNew, 0
245f8 2c 20 73 69 7a 65 6f 66 28 2a 70 4e 65 77 29 29  , sizeof(*pNew))
245f9 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 4c 69  ;.  }.  if( pELi
245fa 73 74 3d 3d 30 20 29 7b 0a 20 20 20 20 70 45 4c  st==0 ){.    pEL
245fb 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78 70  ist = sqlite3Exp
245fc 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
245fd 73 65 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78  se, 0, sqlite3Ex
245fe 70 72 28 64 62 2c 54 4b 5f 41 4c 4c 2c 30 2c 30  pr(db,TK_ALL,0,0
245ff 2c 30 29 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70  ,0), 0);.  }.  p
24600 4e 65 77 2d 3e 70 45 4c 69 73 74 20 3d 20 70 45  New->pEList = pE
24601 4c 69 73 74 3b 0a 20 20 70 4e 65 77 2d 3e 70 53  List;.  pNew->pS
24602 72 63 20 3d 20 70 53 72 63 3b 0a 20 20 70 4e 65  rc = pSrc;.  pNe
24603 77 2d 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65  w->pWhere = pWhe
24604 72 65 3b 0a 20 20 70 4e 65 77 2d 3e 70 47 72 6f  re;.  pNew->pGro
24605 75 70 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b  upBy = pGroupBy;
24606 0a 20 20 70 4e 65 77 2d 3e 70 48 61 76 69 6e 67  .  pNew->pHaving
24607 20 3d 20 70 48 61 76 69 6e 67 3b 0a 20 20 70 4e   = pHaving;.  pN
24608 65 77 2d 3e 70 4f 72 64 65 72 42 79 20 3d 20 70  ew->pOrderBy = p
24609 4f 72 64 65 72 42 79 3b 0a 20 20 70 4e 65 77 2d  OrderBy;.  pNew-
2460a 3e 73 65 6c 46 6c 61 67 73 20 3d 20 69 73 44 69  >selFlags = isDi
2460b 73 74 69 6e 63 74 20 3f 20 53 46 5f 44 69 73 74  stinct ? SF_Dist
2460c 69 6e 63 74 20 3a 20 30 3b 0a 20 20 70 4e 65 77  inct : 0;.  pNew
2460d 2d 3e 6f 70 20 3d 20 54 4b 5f 53 45 4c 45 43 54  ->op = TK_SELECT
2460e 3b 0a 20 20 70 4e 65 77 2d 3e 70 4c 69 6d 69 74  ;.  pNew->pLimit
2460f 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 70 4e 65   = pLimit;.  pNe
24610 77 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f 66  w->pOffset = pOf
24611 66 73 65 74 3b 0a 20 20 70 4e 65 77 2d 3e 61 64  fset;.  pNew->ad
24612 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d 20 3d 20  drOpenEphm[0] = 
24613 2d 31 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72  -1;.  pNew->addr
24614 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 20 2d 31  OpenEphm[1] = -1
24615 3b 0a 20 20 70 4e 65 77 2d 3e 61 64 64 72 4f 70  ;.  pNew->addrOp
24616 65 6e 45 70 68 6d 5b 32 5d 20 3d 20 2d 31 3b 0a  enEphm[2] = -1;.
24617 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63    if( db->malloc
24618 46 61 69 6c 65 64 20 29 20 7b 0a 20 20 20 20 63  Failed ) {.    c
24619 6c 65 61 72 53 65 6c 65 63 74 28 64 62 2c 20 70  learSelect(db, p
2461a 4e 65 77 29 3b 0a 20 20 20 20 69 66 28 20 70 4e  New);.    if( pN
2461b 65 77 21 3d 26 73 74 61 6e 64 69 6e 20 29 20 73  ew!=&standin ) s
2461c 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2461d 20 70 4e 65 77 29 3b 0a 20 20 20 20 70 4e 65 77   pNew);.    pNew
2461e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75   = 0;.  }.  retu
2461f 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
24620 2a 20 44 65 6c 65 74 65 20 74 68 65 20 67 69 76  * Delete the giv
24621 65 6e 20 53 65 6c 65 63 74 20 73 74 72 75 63 74  en Select struct
24622 75 72 65 20 61 6e 64 20 61 6c 6c 20 6f 66 20 69  ure and all of i
24623 74 73 20 73 75 62 73 74 72 75 63 74 75 72 65 73  ts substructures
24624 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
24625 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
24626 53 65 6c 65 63 74 44 65 6c 65 74 65 28 73 71 6c  SelectDelete(sql
24627 69 74 65 33 20 2a 64 62 2c 20 53 65 6c 65 63 74  ite3 *db, Select
24628 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
24629 0a 20 20 20 20 63 6c 65 61 72 53 65 6c 65 63 74  .    clearSelect
2462a 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 73 71 6c  (db, p);.    sql
2462b 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2462c 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
2462d 47 69 76 65 6e 20 31 20 74 6f 20 33 20 69 64 65  Given 1 to 3 ide
2462e 6e 74 69 66 69 65 72 73 20 70 72 65 63 65 65 64  ntifiers preceed
2462f 69 6e 67 20 74 68 65 20 4a 4f 49 4e 20 6b 65 79  ing the JOIN key
24630 77 6f 72 64 2c 20 64 65 74 65 72 6d 69 6e 65 20  word, determine 
24631 74 68 65 0a 2a 2a 20 74 79 70 65 20 6f 66 20 6a  the.** type of j
24632 6f 69 6e 2e 20 20 52 65 74 75 72 6e 20 61 6e 20  oin.  Return an 
24633 69 6e 74 65 67 65 72 20 63 6f 6e 73 74 61 6e 74  integer constant
24634 20 74 68 61 74 20 65 78 70 72 65 73 73 65 73 20   that expresses 
24635 74 68 61 74 20 74 79 70 65 0a 2a 2a 20 69 6e 20  that type.** in 
24636 74 65 72 6d 73 20 6f 66 20 74 68 65 20 66 6f 6c  terms of the fol
24637 6c 6f 77 69 6e 67 20 62 69 74 20 76 61 6c 75 65  lowing bit value
24638 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 4a 54 5f  s:.**.**     JT_
24639 49 4e 4e 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f  INNER.**     JT_
2463a 43 52 4f 53 53 0a 2a 2a 20 20 20 20 20 4a 54 5f  CROSS.**     JT_
2463b 4f 55 54 45 52 0a 2a 2a 20 20 20 20 20 4a 54 5f  OUTER.**     JT_
2463c 4e 41 54 55 52 41 4c 0a 2a 2a 20 20 20 20 20 4a  NATURAL.**     J
2463d 54 5f 4c 45 46 54 0a 2a 2a 20 20 20 20 20 4a 54  T_LEFT.**     JT
2463e 5f 52 49 47 48 54 0a 2a 2a 0a 2a 2a 20 41 20 66  _RIGHT.**.** A f
2463f 75 6c 6c 20 6f 75 74 65 72 20 6a 6f 69 6e 20 69  ull outer join i
24640 73 20 74 68 65 20 63 6f 6d 62 69 6e 61 74 69 6f  s the combinatio
24641 6e 20 6f 66 20 4a 54 5f 4c 45 46 54 20 61 6e 64  n of JT_LEFT and
24642 20 4a 54 5f 52 49 47 48 54 2e 0a 2a 2a 0a 2a 2a   JT_RIGHT..**.**
24643 20 49 66 20 61 6e 20 69 6c 6c 65 67 61 6c 20 6f   If an illegal o
24644 72 20 75 6e 73 75 70 70 6f 72 74 65 64 20 6a 6f  r unsupported jo
24645 69 6e 20 74 79 70 65 20 69 73 20 73 65 65 6e 2c  in type is seen,
24646 20 74 68 65 6e 20 73 74 69 6c 6c 20 72 65 74 75   then still retu
24647 72 6e 0a 2a 2a 20 61 20 6a 6f 69 6e 20 74 79 70  rn.** a join typ
24648 65 2c 20 62 75 74 20 70 75 74 20 61 6e 20 65 72  e, but put an er
24649 72 6f 72 20 69 6e 20 74 68 65 20 70 50 61 72 73  ror in the pPars
2464a 65 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  e structure..*/.
2464b 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2464c 6e 74 20 73 71 6c 69 74 65 33 4a 6f 69 6e 54 79  nt sqlite3JoinTy
2464d 70 65 28 50 61 72 73 65 20 2a 70 50 61 72 73 65  pe(Parse *pParse
2464e 2c 20 54 6f 6b 65 6e 20 2a 70 41 2c 20 54 6f 6b  , Token *pA, Tok
2464f 65 6e 20 2a 70 42 2c 20 54 6f 6b 65 6e 20 2a 70  en *pB, Token *p
24650 43 29 7b 0a 20 20 69 6e 74 20 6a 6f 69 6e 74 79  C){.  int jointy
24651 70 65 20 3d 20 30 3b 0a 20 20 54 6f 6b 65 6e 20  pe = 0;.  Token 
24652 2a 61 70 41 6c 6c 5b 33 5d 3b 0a 20 20 54 6f 6b  *apAll[3];.  Tok
24653 65 6e 20 2a 70 3b 0a 20 20 73 74 61 74 69 63 20  en *p;.  static 
24654 63 6f 6e 73 74 20 73 74 72 75 63 74 20 7b 0a 20  const struct {. 
24655 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 7a 4b     const char zK
24656 65 79 77 6f 72 64 5b 38 5d 3b 0a 20 20 20 20 75  eyword[8];.    u
24657 38 20 6e 43 68 61 72 3b 0a 20 20 20 20 75 38 20  8 nChar;.    u8 
24658 63 6f 64 65 3b 0a 20 20 7d 20 6b 65 79 77 6f 72  code;.  } keywor
24659 64 73 5b 5d 20 3d 20 7b 0a 20 20 20 20 7b 20 22  ds[] = {.    { "
2465a 6e 61 74 75 72 61 6c 22 2c 20 37 2c 20 4a 54 5f  natural", 7, JT_
2465b 4e 41 54 55 52 41 4c 20 7d 2c 0a 20 20 20 20 7b  NATURAL },.    {
2465c 20 22 6c 65 66 74 22 2c 20 20 20 20 34 2c 20 4a   "left",    4, J
2465d 54 5f 4c 45 46 54 7c 4a 54 5f 4f 55 54 45 52 20  T_LEFT|JT_OUTER 
2465e 7d 2c 0a 20 20 20 20 7b 20 22 72 69 67 68 74 22  },.    { "right"
2465f 2c 20 20 20 35 2c 20 4a 54 5f 52 49 47 48 54 7c  ,   5, JT_RIGHT|
24660 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20 20  JT_OUTER },.    
24661 7b 20 22 66 75 6c 6c 22 2c 20 20 20 20 34 2c 20  { "full",    4, 
24662 4a 54 5f 4c 45 46 54 7c 4a 54 5f 52 49 47 48 54  JT_LEFT|JT_RIGHT
24663 7c 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20  |JT_OUTER },.   
24664 20 7b 20 22 6f 75 74 65 72 22 2c 20 20 20 35 2c   { "outer",   5,
24665 20 4a 54 5f 4f 55 54 45 52 20 7d 2c 0a 20 20 20   JT_OUTER },.   
24666 20 7b 20 22 69 6e 6e 65 72 22 2c 20 20 20 35 2c   { "inner",   5,
24667 20 4a 54 5f 49 4e 4e 45 52 20 7d 2c 0a 20 20 20   JT_INNER },.   
24668 20 7b 20 22 63 72 6f 73 73 22 2c 20 20 20 35 2c   { "cross",   5,
24669 20 4a 54 5f 49 4e 4e 45 52 7c 4a 54 5f 43 52 4f   JT_INNER|JT_CRO
2466a 53 53 20 7d 2c 0a 20 20 7d 3b 0a 20 20 69 6e 74  SS },.  };.  int
2466b 20 69 2c 20 6a 3b 0a 20 20 61 70 41 6c 6c 5b 30   i, j;.  apAll[0
2466c 5d 20 3d 20 70 41 3b 0a 20 20 61 70 41 6c 6c 5b  ] = pA;.  apAll[
2466d 31 5d 20 3d 20 70 42 3b 0a 20 20 61 70 41 6c 6c  1] = pB;.  apAll
2466e 5b 32 5d 20 3d 20 70 43 3b 0a 20 20 66 6f 72 28  [2] = pC;.  for(
2466f 69 3d 30 3b 20 69 3c 33 20 26 26 20 61 70 41 6c  i=0; i<3 && apAl
24670 6c 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  l[i]; i++){.    
24671 70 20 3d 20 61 70 41 6c 6c 5b 69 5d 3b 0a 20 20  p = apAll[i];.  
24672 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 41 72 72    for(j=0; j<Arr
24673 61 79 53 69 7a 65 28 6b 65 79 77 6f 72 64 73 29  aySize(keywords)
24674 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
24675 28 20 70 2d 3e 6e 3d 3d 6b 65 79 77 6f 72 64 73  ( p->n==keywords
24676 5b 6a 5d 2e 6e 43 68 61 72 20 0a 20 20 20 20 20  [j].nChar .     
24677 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33 53       && sqlite3S
24678 74 72 4e 49 43 6d 70 28 28 63 68 61 72 2a 29 70  trNICmp((char*)p
24679 2d 3e 7a 2c 20 6b 65 79 77 6f 72 64 73 5b 6a 5d  ->z, keywords[j]
2467a 2e 7a 4b 65 79 77 6f 72 64 2c 20 70 2d 3e 6e 29  .zKeyword, p->n)
2467b 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 6a  ==0 ){.        j
2467c 6f 69 6e 74 79 70 65 20 7c 3d 20 6b 65 79 77 6f  ointype |= keywo
2467d 72 64 73 5b 6a 5d 2e 63 6f 64 65 3b 0a 20 20 20  rds[j].code;.   
2467e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2467f 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
24680 28 20 6a 3e 3d 41 72 72 61 79 53 69 7a 65 28 6b  ( j>=ArraySize(k
24681 65 79 77 6f 72 64 73 29 20 29 7b 0a 20 20 20 20  eywords) ){.    
24682 20 20 6a 6f 69 6e 74 79 70 65 20 7c 3d 20 4a 54    jointype |= JT
24683 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72  _ERROR;.      br
24684 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  eak;.    }.  }. 
24685 20 69 66 28 0a 20 20 20 20 20 28 6a 6f 69 6e 74   if(.     (joint
24686 79 70 65 20 26 20 28 4a 54 5f 49 4e 4e 45 52 7c  ype & (JT_INNER|
24687 4a 54 5f 4f 55 54 45 52 29 29 3d 3d 28 4a 54 5f  JT_OUTER))==(JT_
24688 49 4e 4e 45 52 7c 4a 54 5f 4f 55 54 45 52 29 20  INNER|JT_OUTER) 
24689 7c 7c 0a 20 20 20 20 20 28 6a 6f 69 6e 74 79 70  ||.     (jointyp
2468a 65 20 26 20 4a 54 5f 45 52 52 4f 52 29 21 3d 30  e & JT_ERROR)!=0
2468b 0a 20 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20  .  ){.    const 
2468c 63 68 61 72 20 2a 7a 53 70 20 3d 20 22 20 22 3b  char *zSp = " ";
2468d 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 42 21  .    assert( pB!
2468e 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20 70 43  =0 );.    if( pC
2468f 3d 3d 30 20 29 7b 20 7a 53 70 2b 2b 3b 20 7d 0a  ==0 ){ zSp++; }.
24690 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
24691 4d 73 67 28 70 50 61 72 73 65 2c 20 22 75 6e 6b  Msg(pParse, "unk
24692 6e 6f 77 6e 20 6f 72 20 75 6e 73 75 70 70 6f 72  nown or unsuppor
24693 74 65 64 20 6a 6f 69 6e 20 74 79 70 65 3a 20 22  ted join type: "
24694 0a 20 20 20 20 20 20 20 22 25 54 20 25 54 25 73  .       "%T %T%s
24695 25 54 22 2c 20 70 41 2c 20 70 42 2c 20 7a 53 70  %T", pA, pB, zSp
24696 2c 20 70 43 29 3b 0a 20 20 20 20 6a 6f 69 6e 74  , pC);.    joint
24697 79 70 65 20 3d 20 4a 54 5f 49 4e 4e 45 52 3b 0a  ype = JT_INNER;.
24698 20 20 7d 65 6c 73 65 20 69 66 28 20 6a 6f 69 6e    }else if( join
24699 74 79 70 65 20 26 20 4a 54 5f 52 49 47 48 54 20  type & JT_RIGHT 
2469a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2469b 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 0a  rorMsg(pParse, .
2469c 20 20 20 20 20 20 22 52 49 47 48 54 20 61 6e 64        "RIGHT and
2469d 20 46 55 4c 4c 20 4f 55 54 45 52 20 4a 4f 49 4e   FULL OUTER JOIN
2469e 73 20 61 72 65 20 6e 6f 74 20 63 75 72 72 65 6e  s are not curren
2469f 74 6c 79 20 73 75 70 70 6f 72 74 65 64 22 29 3b  tly supported");
246a0 0a 20 20 20 20 6a 6f 69 6e 74 79 70 65 20 3d 20  .    jointype = 
246a1 4a 54 5f 49 4e 4e 45 52 3b 0a 20 20 7d 0a 20 20  JT_INNER;.  }.  
246a2 72 65 74 75 72 6e 20 6a 6f 69 6e 74 79 70 65 3b  return jointype;
246a3 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
246a4 20 74 68 65 20 69 6e 64 65 78 20 6f 66 20 61 20   the index of a 
246a5 63 6f 6c 75 6d 6e 20 69 6e 20 61 20 74 61 62 6c  column in a tabl
246a6 65 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 69 66  e.  Return -1 if
246a7 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69   the column.** i
246a8 73 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 65 64 20  s not contained 
246a9 69 6e 20 74 68 65 20 74 61 62 6c 65 2e 0a 2a 2f  in the table..*/
246aa 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6c 75  .static int colu
246ab 6d 6e 49 6e 64 65 78 28 54 61 62 6c 65 20 2a 70  mnIndex(Table *p
246ac 54 61 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  Tab, const char 
246ad 2a 7a 43 6f 6c 29 7b 0a 20 20 69 6e 74 20 69 3b  *zCol){.  int i;
246ae 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
246af 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a  ab->nCol; i++){.
246b0 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 53      if( sqlite3S
246b1 74 72 49 43 6d 70 28 70 54 61 62 2d 3e 61 43 6f  trICmp(pTab->aCo
246b2 6c 5b 69 5d 2e 7a 4e 61 6d 65 2c 20 7a 43 6f 6c  l[i].zName, zCol
246b3 29 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 69 3b  )==0 ) return i;
246b4 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 2d 31  .  }.  return -1
246b5 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74  ;.}../*.** Set t
246b6 68 65 20 76 61 6c 75 65 20 6f 66 20 61 20 74 6f  he value of a to
246b7 6b 65 6e 20 74 6f 20 61 20 27 5c 30 30 30 27 2d  ken to a '\000'-
246b8 74 65 72 6d 69 6e 61 74 65 64 20 73 74 72 69 6e  terminated strin
246b9 67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  g..*/.static voi
246ba 64 20 73 65 74 54 6f 6b 65 6e 28 54 6f 6b 65 6e  d setToken(Token
246bb 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
246bc 2a 7a 29 7b 0a 20 20 70 2d 3e 7a 20 3d 20 28 75  *z){.  p->z = (u
246bd 38 2a 29 7a 3b 0a 20 20 70 2d 3e 6e 20 3d 20 7a  8*)z;.  p->n = z
246be 20 3f 20 73 74 72 6c 65 6e 28 7a 29 20 3a 20 30   ? strlen(z) : 0
246bf 3b 0a 20 20 70 2d 3e 64 79 6e 20 3d 20 30 3b 0a  ;.  p->dyn = 0;.
246c0 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68 65  }../*.** Set the
246c1 20 74 6f 6b 65 6e 20 74 6f 20 74 68 65 20 64 6f   token to the do
246c2 75 62 6c 65 2d 71 75 6f 74 65 64 20 61 6e 64 20  uble-quoted and 
246c3 65 73 63 61 70 65 64 20 76 65 72 73 69 6f 6e 20  escaped version 
246c4 6f 66 20 74 68 65 20 73 74 72 69 6e 67 20 70 6f  of the string po
246c5 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62 79 20 7a  inted.** to by z
246c6 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 3b 0a 2a  . For example;.*
246c7 2a 0a 2a 2a 20 20 20 20 7b 61 22 62 63 7d 20 20  *.**    {a"bc}  
246c8 2d 3e 20 20 7b 22 61 22 22 62 63 22 7d 0a 2a 2f  ->  {"a""bc"}.*/
246c9 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 74  .static void set
246ca 51 75 6f 74 65 64 54 6f 6b 65 6e 28 50 61 72 73  QuotedToken(Pars
246cb 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65 6e  e *pParse, Token
246cc 20 2a 70 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *p, const char 
246cd 2a 7a 29 7b 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  *z){..  /* Check
246ce 20 69 66 20 74 68 65 20 73 74 72 69 6e 67 20 61   if the string a
246cf 70 70 65 61 72 73 20 74 6f 20 62 65 20 71 75 6f  ppears to be quo
246d0 74 65 64 20 75 73 69 6e 67 20 22 2e 2e 2e 22 20  ted using "..." 
246d1 6f 72 20 60 2e 2e 2e 60 0a 20 20 2a 2a 20 6f 72  or `...`.  ** or
246d2 20 5b 2e 2e 2e 5d 20 6f 72 20 27 2e 2e 2e 27 20   [...] or '...' 
246d3 6f 72 20 69 66 20 74 68 65 20 73 74 72 69 6e 67  or if the string
246d4 20 63 6f 6e 74 61 69 6e 73 20 61 6e 79 20 22 20   contains any " 
246d5 63 68 61 72 61 63 74 65 72 73 2e 20 20 0a 20 20  characters.  .  
246d6 2a 2a 20 49 66 20 69 74 20 64 6f 65 73 2c 20 74  ** If it does, t
246d7 68 65 6e 20 72 65 63 6f 72 64 20 61 20 76 65 72  hen record a ver
246d8 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 74 72 69  sion of the stri
246d9 6e 67 20 77 69 74 68 20 74 68 65 20 73 70 65 63  ng with the spec
246da 69 61 6c 0a 20 20 2a 2a 20 63 68 61 72 61 63 74  ial.  ** charact
246db 65 72 73 20 65 73 63 61 70 65 64 2e 0a 20 20 2a  ers escaped..  *
246dc 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  /.  const char *
246dd 7a 32 20 3d 20 7a 3b 0a 20 20 69 66 28 20 2a 7a  z2 = z;.  if( *z
246de 32 21 3d 27 5b 27 20 26 26 20 2a 7a 32 21 3d 27  2!='[' && *z2!='
246df 60 27 20 26 26 20 2a 7a 32 21 3d 27 5c 27 27 20  `' && *z2!='\'' 
246e0 29 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 2a 7a  ){.    while( *z
246e1 32 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 2a  2 ){.      if( *
246e2 7a 32 3d 3d 27 22 27 20 29 20 62 72 65 61 6b 3b  z2=='"' ) break;
246e3 0a 20 20 20 20 20 20 7a 32 2b 2b 3b 0a 20 20 20  .      z2++;.   
246e4 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 2a 7a   }.  }..  if( *z
246e5 32 20 29 7b 0a 20 20 20 20 2f 2a 20 53 74 72 69  2 ){.    /* Stri
246e6 6e 67 20 63 6f 6e 74 61 69 6e 73 20 22 20 63 68  ng contains " ch
246e7 61 72 61 63 74 65 72 73 20 2d 20 63 6f 70 79 20  aracters - copy 
246e8 61 6e 64 20 71 75 6f 74 65 20 74 68 65 20 73 74  and quote the st
246e9 72 69 6e 67 2e 20 2a 2f 0a 20 20 20 20 70 2d 3e  ring. */.    p->
246ea 7a 20 3d 20 28 75 38 20 2a 29 73 71 6c 69 74 65  z = (u8 *)sqlite
246eb 33 4d 50 72 69 6e 74 66 28 70 50 61 72 73 65 2d  3MPrintf(pParse-
246ec 3e 64 62 2c 20 22 5c 22 25 77 5c 22 22 2c 20 7a  >db, "\"%w\"", z
246ed 29 3b 0a 20 20 20 20 69 66 28 20 70 2d 3e 7a 20  );.    if( p->z 
246ee 29 7b 0a 20 20 20 20 20 20 70 2d 3e 6e 20 3d 20  ){.      p->n = 
246ef 73 74 72 6c 65 6e 28 28 63 68 61 72 20 2a 29 70  strlen((char *)p
246f0 2d 3e 7a 29 3b 0a 20 20 20 20 20 20 70 2d 3e 64  ->z);.      p->d
246f1 79 6e 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  yn = 1;.    }.  
246f2 7d 65 6c 73 65 7b 0a 20 20 20 20 2f 2a 20 53 74  }else{.    /* St
246f3 72 69 6e 67 20 63 6f 6e 74 61 69 6e 73 20 6e 6f  ring contains no
246f4 20 22 20 63 68 61 72 61 63 74 65 72 73 20 2d 20   " characters - 
246f5 63 6f 70 79 20 74 68 65 20 70 6f 69 6e 74 65 72  copy the pointer
246f6 2e 20 2a 2f 0a 20 20 20 20 70 2d 3e 7a 20 3d 20  . */.    p->z = 
246f7 28 75 38 2a 29 7a 3b 0a 20 20 20 20 70 2d 3e 6e  (u8*)z;.    p->n
246f8 20 3d 20 28 7a 32 20 2d 20 7a 29 3b 0a 20 20 20   = (z2 - z);.   
246f9 20 70 2d 3e 64 79 6e 20 3d 20 30 3b 0a 20 20 7d   p->dyn = 0;.  }
246fa 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
246fb 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6e   an expression n
246fc 6f 64 65 20 66 6f 72 20 61 6e 20 69 64 65 6e 74  ode for an ident
246fd 69 66 69 65 72 20 77 69 74 68 20 74 68 65 20 6e  ifier with the n
246fe 61 6d 65 20 6f 66 20 7a 4e 61 6d 65 0a 2a 2f 0a  ame of zName.*/.
246ff 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 45  SQLITE_PRIVATE E
24700 78 70 72 20 2a 73 71 6c 69 74 65 33 43 72 65 61  xpr *sqlite3Crea
24701 74 65 49 64 45 78 70 72 28 50 61 72 73 65 20 2a  teIdExpr(Parse *
24702 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
24703 61 72 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 54 6f  ar *zName){.  To
24704 6b 65 6e 20 64 75 6d 6d 79 3b 0a 20 20 73 65 74  ken dummy;.  set
24705 54 6f 6b 65 6e 28 26 64 75 6d 6d 79 2c 20 7a 4e  Token(&dummy, zN
24706 61 6d 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ame);.  return s
24707 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
24708 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c  se, TK_ID, 0, 0,
24709 20 26 64 75 6d 6d 79 29 3b 0a 7d 0a 0a 2f 2a 0a   &dummy);.}../*.
2470a 2a 2a 20 41 64 64 20 61 20 74 65 72 6d 20 74 6f  ** Add a term to
2470b 20 74 68 65 20 57 48 45 52 45 20 65 78 70 72 65   the WHERE expre
2470c 73 73 69 6f 6e 20 69 6e 20 2a 70 70 45 78 70 72  ssion in *ppExpr
2470d 20 74 68 61 74 20 72 65 71 75 69 72 65 73 20 74   that requires t
2470e 68 65 0a 2a 2a 20 7a 43 6f 6c 20 63 6f 6c 75 6d  he.** zCol colum
2470f 6e 20 74 6f 20 62 65 20 65 71 75 61 6c 20 69 6e  n to be equal in
24710 20 74 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20   the two tables 
24711 70 54 61 62 31 20 61 6e 64 20 70 54 61 62 32 2e  pTab1 and pTab2.
24712 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
24713 61 64 64 57 68 65 72 65 54 65 72 6d 28 0a 20 20  addWhereTerm(.  
24714 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
24715 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
24716 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
24717 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 43 6f   const char *zCo
24718 6c 2c 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d  l,        /* Nam
24719 65 20 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 20  e of the column 
2471a 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 61 62 6c 65  */.  const Table
2471b 20 2a 70 54 61 62 31 2c 20 20 20 20 20 20 2f 2a   *pTab1,      /*
2471c 20 46 69 72 73 74 20 74 61 62 6c 65 20 2a 2f 0a   First table */.
2471d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 41    const char *zA
2471e 6c 69 61 73 31 2c 20 20 20 20 20 2f 2a 20 41 6c  lias1,     /* Al
2471f 69 61 73 20 66 6f 72 20 66 69 72 73 74 20 74 61  ias for first ta
24720 62 6c 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  ble.  May be NUL
24721 4c 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 54 61 62  L */.  const Tab
24722 6c 65 20 2a 70 54 61 62 32 2c 20 20 20 20 20 20  le *pTab2,      
24723 2f 2a 20 53 65 63 6f 6e 64 20 74 61 62 6c 65 20  /* Second table 
24724 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
24725 2a 7a 41 6c 69 61 73 32 2c 20 20 20 20 20 2f 2a  *zAlias2,     /*
24726 20 41 6c 69 61 73 20 66 6f 72 20 73 65 63 6f 6e   Alias for secon
24727 64 20 74 61 62 6c 65 2e 20 20 4d 61 79 20 62 65  d table.  May be
24728 20 4e 55 4c 4c 20 2a 2f 0a 20 20 69 6e 74 20 69   NULL */.  int i
24729 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 2c 20  RightJoinTable, 
2472a 20 20 20 20 2f 2a 20 56 44 42 45 20 63 75 72 73      /* VDBE curs
2472b 6f 72 20 66 6f 72 20 74 68 65 20 72 69 67 68 74  or for the right
2472c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72   table */.  Expr
2472d 20 2a 2a 70 70 45 78 70 72 2c 20 20 20 20 20 20   **ppExpr,      
2472e 20 20 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20       /* Add the 
2472f 65 71 75 61 6c 69 74 79 20 74 65 72 6d 20 74 6f  equality term to
24730 20 74 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   this expression
24731 20 2a 2f 0a 20 20 69 6e 74 20 69 73 4f 75 74 65   */.  int isOute
24732 72 4a 6f 69 6e 20 20 20 20 20 20 20 20 20 20 2f  rJoin          /
24733 2a 20 54 72 75 65 20 69 66 20 64 65 61 6c 69 6e  * True if dealin
24734 67 20 77 69 74 68 20 61 6e 20 4f 55 54 45 52 20  g with an OUTER 
24735 6a 6f 69 6e 20 2a 2f 0a 29 7b 0a 20 20 45 78 70  join */.){.  Exp
24736 72 20 2a 70 45 31 61 2c 20 2a 70 45 31 62 2c 20  r *pE1a, *pE1b, 
24737 2a 70 45 31 63 3b 0a 20 20 45 78 70 72 20 2a 70  *pE1c;.  Expr *p
24738 45 32 61 2c 20 2a 70 45 32 62 2c 20 2a 70 45 32  E2a, *pE2b, *pE2
24739 63 3b 0a 20 20 45 78 70 72 20 2a 70 45 3b 0a 0a  c;.  Expr *pE;..
2473a 20 20 70 45 31 61 20 3d 20 73 71 6c 69 74 65 33    pE1a = sqlite3
2473b 43 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61  CreateIdExpr(pPa
2473c 72 73 65 2c 20 7a 43 6f 6c 29 3b 0a 20 20 70 45  rse, zCol);.  pE
2473d 32 61 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  2a = sqlite3Crea
2473e 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c  teIdExpr(pParse,
2473f 20 7a 43 6f 6c 29 3b 0a 20 20 69 66 28 20 7a 41   zCol);.  if( zA
24740 6c 69 61 73 31 3d 3d 30 20 29 7b 0a 20 20 20 20  lias1==0 ){.    
24741 7a 41 6c 69 61 73 31 20 3d 20 70 54 61 62 31 2d  zAlias1 = pTab1-
24742 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20 20 70 45  >zName;.  }.  pE
24743 31 62 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61  1b = sqlite3Crea
24744 74 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c  teIdExpr(pParse,
24745 20 7a 41 6c 69 61 73 31 29 3b 0a 20 20 69 66 28   zAlias1);.  if(
24746 20 7a 41 6c 69 61 73 32 3d 3d 30 20 29 7b 0a 20   zAlias2==0 ){. 
24747 20 20 20 7a 41 6c 69 61 73 32 20 3d 20 70 54 61     zAlias2 = pTa
24748 62 32 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 7d 0a 20  b2->zName;.  }. 
24749 20 70 45 32 62 20 3d 20 73 71 6c 69 74 65 33 43   pE2b = sqlite3C
2474a 72 65 61 74 65 49 64 45 78 70 72 28 70 50 61 72  reateIdExpr(pPar
2474b 73 65 2c 20 7a 41 6c 69 61 73 32 29 3b 0a 20 20  se, zAlias2);.  
2474c 70 45 31 63 20 3d 20 73 71 6c 69 74 65 33 50 45  pE1c = sqlite3PE
2474d 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 44  xpr(pParse, TK_D
2474e 4f 54 2c 20 70 45 31 62 2c 20 70 45 31 61 2c 20  OT, pE1b, pE1a, 
2474f 30 29 3b 0a 20 20 70 45 32 63 20 3d 20 73 71 6c  0);.  pE2c = sql
24750 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
24751 2c 20 54 4b 5f 44 4f 54 2c 20 70 45 32 62 2c 20  , TK_DOT, pE2b, 
24752 70 45 32 61 2c 20 30 29 3b 0a 20 20 70 45 20 3d  pE2a, 0);.  pE =
24753 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
24754 61 72 73 65 2c 20 54 4b 5f 45 51 2c 20 70 45 31  arse, TK_EQ, pE1
24755 63 2c 20 70 45 32 63 2c 20 30 29 3b 0a 20 20 69  c, pE2c, 0);.  i
24756 66 28 20 70 45 20 26 26 20 69 73 4f 75 74 65 72  f( pE && isOuter
24757 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 45 78 70 72  Join ){.    Expr
24758 53 65 74 50 72 6f 70 65 72 74 79 28 70 45 2c 20  SetProperty(pE, 
24759 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 3b 0a 20 20  EP_FromJoin);.  
2475a 20 20 70 45 2d 3e 69 52 69 67 68 74 4a 6f 69 6e    pE->iRightJoin
2475b 54 61 62 6c 65 20 3d 20 69 52 69 67 68 74 4a 6f  Table = iRightJo
2475c 69 6e 54 61 62 6c 65 3b 0a 20 20 7d 0a 20 20 2a  inTable;.  }.  *
2475d 70 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33  ppExpr = sqlite3
2475e 45 78 70 72 41 6e 64 28 70 50 61 72 73 65 2d 3e  ExprAnd(pParse->
2475f 64 62 2c 2a 70 70 45 78 70 72 2c 20 70 45 29 3b  db,*ppExpr, pE);
24760 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 74 20 74 68  .}../*.** Set th
24761 65 20 45 50 5f 46 72 6f 6d 4a 6f 69 6e 20 70 72  e EP_FromJoin pr
24762 6f 70 65 72 74 79 20 6f 6e 20 61 6c 6c 20 74 65  operty on all te
24763 72 6d 73 20 6f 66 20 74 68 65 20 67 69 76 65 6e  rms of the given
24764 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a 2a 2a 20   expression..** 
24765 41 6e 64 20 73 65 74 20 74 68 65 20 45 78 70 72  And set the Expr
24766 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65  .iRightJoinTable
24767 20 74 6f 20 69 54 61 62 6c 65 20 66 6f 72 20 65   to iTable for e
24768 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
24769 0a 2a 2a 20 65 78 70 72 65 73 73 69 6f 6e 2e 0a  .** expression..
2476a 2a 2a 0a 2a 2a 20 54 68 65 20 45 50 5f 46 72 6f  **.** The EP_Fro
2476b 6d 4a 6f 69 6e 20 70 72 6f 70 65 72 74 79 20 69  mJoin property i
2476c 73 20 75 73 65 64 20 6f 6e 20 74 65 72 6d 73 20  s used on terms 
2476d 6f 66 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e  of an expression
2476e 20 74 6f 20 74 65 6c 6c 0a 2a 2a 20 74 68 65 20   to tell.** the 
2476f 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20  LEFT OUTER JOIN 
24770 70 72 6f 63 65 73 73 69 6e 67 20 6c 6f 67 69 63  processing logic
24771 20 74 68 61 74 20 74 68 69 73 20 74 65 72 6d 20   that this term 
24772 69 73 20 70 61 72 74 20 6f 66 20 74 68 65 0a 2a  is part of the.*
24773 2a 20 6a 6f 69 6e 20 72 65 73 74 72 69 63 74 69  * join restricti
24774 6f 6e 20 73 70 65 63 69 66 69 65 64 20 69 6e 20  on specified in 
24775 74 68 65 20 4f 4e 20 6f 72 20 55 53 49 4e 47 20  the ON or USING 
24776 63 6c 61 75 73 65 20 61 6e 64 20 6e 6f 74 20 61  clause and not a
24777 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20   part.** of the 
24778 6d 6f 72 65 20 67 65 6e 65 72 61 6c 20 57 48 45  more general WHE
24779 52 45 20 63 6c 61 75 73 65 2e 20 20 54 68 65 73  RE clause.  Thes
2477a 65 20 74 65 72 6d 73 20 61 72 65 20 6d 6f 76 65  e terms are move
2477b 64 20 6f 76 65 72 20 74 6f 20 74 68 65 0a 2a 2a  d over to the.**
2477c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 64 75   WHERE clause du
2477d 72 69 6e 67 20 6a 6f 69 6e 20 70 72 6f 63 65 73  ring join proces
2477e 73 69 6e 67 20 62 75 74 20 77 65 20 6e 65 65 64  sing but we need
2477f 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 74 68 61   to remember tha
24780 74 20 74 68 65 79 0a 2a 2a 20 6f 72 69 67 69 6e  t they.** origin
24781 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f  ated in the ON o
24782 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 2e 0a  r USING clause..
24783 2a 2a 0a 2a 2a 20 54 68 65 20 45 78 70 72 2e 69  **.** The Expr.i
24784 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 74  RightJoinTable t
24785 65 6c 6c 73 20 74 68 65 20 57 48 45 52 45 20 63  ells the WHERE c
24786 6c 61 75 73 65 20 70 72 6f 63 65 73 73 69 6e 67  lause processing
24787 20 74 68 61 74 20 74 68 65 0a 2a 2a 20 65 78 70   that the.** exp
24788 72 65 73 73 69 6f 6e 20 64 65 70 65 6e 64 73 20  ression depends 
24789 6f 6e 20 74 61 62 6c 65 20 69 52 69 67 68 74 4a  on table iRightJ
2478a 6f 69 6e 54 61 62 6c 65 20 65 76 65 6e 20 69 66  oinTable even if
2478b 20 74 68 61 74 20 74 61 62 6c 65 20 69 73 20 6e   that table is n
2478c 6f 74 0a 2a 2a 20 65 78 70 6c 69 63 69 74 6c 79  ot.** explicitly
2478d 20 6d 65 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68   mentioned in th
2478e 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20 20 54  e expression.  T
2478f 68 61 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  hat information 
24790 69 73 20 6e 65 65 64 65 64 0a 2a 2a 20 66 6f 72  is needed.** for
24791 20 63 61 73 65 73 20 6c 69 6b 65 20 74 68 69 73   cases like this
24792 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 53 45 4c 45 43  :.**.**    SELEC
24793 54 20 2a 20 46 52 4f 4d 20 74 31 20 4c 45 46 54  T * FROM t1 LEFT
24794 20 4a 4f 49 4e 20 74 32 20 4f 4e 20 74 31 2e 61   JOIN t2 ON t1.a
24795 3d 74 32 2e 62 20 41 4e 44 20 74 31 2e 78 3d 35  =t2.b AND t1.x=5
24796 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 68 65 72 65  .**.** The where
24797 20 63 6c 61 75 73 65 20 6e 65 65 64 73 20 74 6f   clause needs to
24798 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64 6c   defer the handl
24799 69 6e 67 20 6f 66 20 74 68 65 20 74 31 2e 78 3d  ing of the t1.x=
2479a 35 0a 2a 2a 20 74 65 72 6d 20 75 6e 74 69 6c 20  5.** term until 
2479b 61 66 74 65 72 20 74 68 65 20 74 32 20 6c 6f 6f  after the t2 loo
2479c 70 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20  p of the join.  
2479d 49 6e 20 74 68 61 74 20 77 61 79 2c 20 61 0a 2a  In that way, a.*
2479e 2a 20 4e 55 4c 4c 20 74 32 20 72 6f 77 20 77 69  * NULL t2 row wi
2479f 6c 6c 20 62 65 20 69 6e 73 65 72 74 65 64 20 77  ll be inserted w
247a0 68 65 6e 65 76 65 72 20 74 31 2e 78 21 3d 35 2e  henever t1.x!=5.
247a1 20 20 49 66 20 77 65 20 64 6f 20 6e 6f 74 0a 2a    If we do not.*
247a2 2a 20 64 65 66 65 72 20 74 68 65 20 68 61 6e 64  * defer the hand
247a3 6c 69 6e 67 20 6f 66 20 74 31 2e 78 3d 35 2c 20  ling of t1.x=5, 
247a4 69 74 20 77 69 6c 6c 20 62 65 20 70 72 6f 63 65  it will be proce
247a5 73 73 65 64 20 69 6d 6d 65 64 69 61 74 65 6c 79  ssed immediately
247a6 0a 2a 2a 20 61 66 74 65 72 20 74 68 65 20 74 31  .** after the t1
247a7 20 6c 6f 6f 70 20 61 6e 64 20 72 6f 77 73 20 77   loop and rows w
247a8 69 74 68 20 74 31 2e 78 21 3d 35 20 77 69 6c 6c  ith t1.x!=5 will
247a9 20 6e 65 76 65 72 20 61 70 70 65 61 72 20 69 6e   never appear in
247aa 0a 2a 2a 20 74 68 65 20 6f 75 74 70 75 74 2c 20  .** the output, 
247ab 77 68 69 63 68 20 69 73 20 69 6e 63 6f 72 72 65  which is incorre
247ac 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ct..*/.static vo
247ad 69 64 20 73 65 74 4a 6f 69 6e 45 78 70 72 28 45  id setJoinExpr(E
247ae 78 70 72 20 2a 70 2c 20 69 6e 74 20 69 54 61 62  xpr *p, int iTab
247af 6c 65 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 20  le){.  while( p 
247b0 29 7b 0a 20 20 20 20 45 78 70 72 53 65 74 50 72  ){.    ExprSetPr
247b1 6f 70 65 72 74 79 28 70 2c 20 45 50 5f 46 72 6f  operty(p, EP_Fro
247b2 6d 4a 6f 69 6e 29 3b 0a 20 20 20 20 70 2d 3e 69  mJoin);.    p->i
247b3 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c 65 20 3d  RightJoinTable =
247b4 20 69 54 61 62 6c 65 3b 0a 20 20 20 20 73 65 74   iTable;.    set
247b5 4a 6f 69 6e 45 78 70 72 28 70 2d 3e 70 4c 65 66  JoinExpr(p->pLef
247b6 74 2c 20 69 54 61 62 6c 65 29 3b 0a 20 20 20 20  t, iTable);.    
247b7 70 20 3d 20 70 2d 3e 70 52 69 67 68 74 3b 0a 20  p = p->pRight;. 
247b8 20 7d 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69   } .}../*.** Thi
247b9 73 20 72 6f 75 74 69 6e 65 20 70 72 6f 63 65 73  s routine proces
247ba 73 65 73 20 74 68 65 20 6a 6f 69 6e 20 69 6e 66  ses the join inf
247bb 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 61 20 53  ormation for a S
247bc 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
247bd 0a 2a 2a 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47  .** ON and USING
247be 20 63 6c 61 75 73 65 73 20 61 72 65 20 63 6f 6e   clauses are con
247bf 76 65 72 74 65 64 20 69 6e 74 6f 20 65 78 74 72  verted into extr
247c0 61 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20 57  a terms of the W
247c1 48 45 52 45 20 63 6c 61 75 73 65 2e 0a 2a 2a 20  HERE clause..** 
247c2 4e 41 54 55 52 41 4c 20 6a 6f 69 6e 73 20 61 6c  NATURAL joins al
247c3 73 6f 20 63 72 65 61 74 65 20 65 78 74 72 61 20  so create extra 
247c4 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72  WHERE clause ter
247c5 6d 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74 65  ms..**.** The te
247c6 72 6d 73 20 6f 66 20 61 20 46 52 4f 4d 20 63 6c  rms of a FROM cl
247c7 61 75 73 65 20 61 72 65 20 63 6f 6e 74 61 69 6e  ause are contain
247c8 65 64 20 69 6e 20 74 68 65 20 53 65 6c 65 63 74  ed in the Select
247c9 2e 70 53 72 63 20 73 74 72 75 63 74 75 72 65 2e  .pSrc structure.
247ca 0a 2a 2a 20 54 68 65 20 6c 65 66 74 20 6d 6f 73  .** The left mos
247cb 74 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 66  t table is the f
247cc 69 72 73 74 20 65 6e 74 72 79 20 69 6e 20 53 65  irst entry in Se
247cd 6c 65 63 74 2e 70 53 72 63 2e 20 20 54 68 65 20  lect.pSrc.  The 
247ce 72 69 67 68 74 2d 6d 6f 73 74 0a 2a 2a 20 74 61  right-most.** ta
247cf 62 6c 65 20 69 73 20 74 68 65 20 6c 61 73 74 20  ble is the last 
247d0 65 6e 74 72 79 2e 20 20 54 68 65 20 6a 6f 69 6e  entry.  The join
247d1 20 6f 70 65 72 61 74 6f 72 20 69 73 20 68 65 6c   operator is hel
247d2 64 20 69 6e 20 74 68 65 20 65 6e 74 72 79 20 74  d in the entry t
247d3 6f 0a 2a 2a 20 74 68 65 20 6c 65 66 74 2e 20 20  o.** the left.  
247d4 54 68 75 73 20 65 6e 74 72 79 20 30 20 63 6f 6e  Thus entry 0 con
247d5 74 61 69 6e 73 20 74 68 65 20 6a 6f 69 6e 20 6f  tains the join o
247d6 70 65 72 61 74 6f 72 20 66 6f 72 20 74 68 65 20  perator for the 
247d7 6a 6f 69 6e 20 62 65 74 77 65 65 6e 0a 2a 2a 20  join between.** 
247d8 65 6e 74 72 69 65 73 20 30 20 61 6e 64 20 31 2e  entries 0 and 1.
247d9 20 20 41 6e 79 20 4f 4e 20 6f 72 20 55 53 49 4e    Any ON or USIN
247da 47 20 63 6c 61 75 73 65 73 20 61 73 73 6f 63 69  G clauses associ
247db 61 74 65 64 20 77 69 74 68 20 74 68 65 20 6a 6f  ated with the jo
247dc 69 6e 20 61 72 65 0a 2a 2a 20 61 6c 73 6f 20 61  in are.** also a
247dd 74 74 61 63 68 65 64 20 74 6f 20 74 68 65 20 6c  ttached to the l
247de 65 66 74 20 65 6e 74 72 79 2e 0a 2a 2a 0a 2a 2a  eft entry..**.**
247df 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   This routine re
247e0 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72  turns the number
247e1 20 6f 66 20 65 72 72 6f 72 73 20 65 6e 63 6f 75   of errors encou
247e2 6e 74 65 72 65 64 2e 0a 2a 2f 0a 73 74 61 74 69  ntered..*/.stati
247e3 63 20 69 6e 74 20 73 71 6c 69 74 65 50 72 6f 63  c int sqliteProc
247e4 65 73 73 4a 6f 69 6e 28 50 61 72 73 65 20 2a 70  essJoin(Parse *p
247e5 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70  Parse, Select *p
247e6 29 7b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  ){.  SrcList *pS
247e7 72 63 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  rc;             
247e8 20 20 20 20 20 2f 2a 20 41 6c 6c 20 74 61 62 6c       /* All tabl
247e9 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
247ea 6c 61 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 69  lause */.  int i
247eb 2c 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  , j;            
247ec 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
247ed 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
247ee 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
247ef 69 74 65 6d 20 2a 70 4c 65 66 74 3b 20 20 20 20  item *pLeft;    
247f0 20 2f 2a 20 4c 65 66 74 20 74 61 62 6c 65 20 62   /* Left table b
247f1 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a 20  eing joined */. 
247f2 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
247f3 69 74 65 6d 20 2a 70 52 69 67 68 74 3b 20 20 20  item *pRight;   
247f4 20 2f 2a 20 52 69 67 68 74 20 74 61 62 6c 65 20   /* Right table 
247f5 62 65 69 6e 67 20 6a 6f 69 6e 65 64 20 2a 2f 0a  being joined */.
247f6 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72  .  pSrc = p->pSr
247f7 63 3b 0a 20 20 70 4c 65 66 74 20 3d 20 26 70 53  c;.  pLeft = &pS
247f8 72 63 2d 3e 61 5b 30 5d 3b 0a 20 20 70 52 69 67  rc->a[0];.  pRig
247f9 68 74 20 3d 20 26 70 4c 65 66 74 5b 31 5d 3b 0a  ht = &pLeft[1];.
247fa 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 53 72    for(i=0; i<pSr
247fb 63 2d 3e 6e 53 72 63 2d 31 3b 20 69 2b 2b 2c 20  c->nSrc-1; i++, 
247fc 70 52 69 67 68 74 2b 2b 2c 20 70 4c 65 66 74 2b  pRight++, pLeft+
247fd 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  +){.    Table *p
247fe 4c 65 66 74 54 61 62 20 3d 20 70 4c 65 66 74 2d  LeftTab = pLeft-
247ff 3e 70 54 61 62 3b 0a 20 20 20 20 54 61 62 6c 65  >pTab;.    Table
24800 20 2a 70 52 69 67 68 74 54 61 62 20 3d 20 70 52   *pRightTab = pR
24801 69 67 68 74 2d 3e 70 54 61 62 3b 0a 20 20 20 20  ight->pTab;.    
24802 69 6e 74 20 69 73 4f 75 74 65 72 3b 0a 0a 20 20  int isOuter;..  
24803 20 20 69 66 28 20 70 4c 65 66 74 54 61 62 3d 3d    if( pLeftTab==
24804 30 20 7c 7c 20 70 52 69 67 68 74 54 61 62 3d 3d  0 || pRightTab==
24805 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20  0 ) continue;.  
24806 20 20 69 73 4f 75 74 65 72 20 3d 20 28 70 52 69    isOuter = (pRi
24807 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  ght->jointype & 
24808 4a 54 5f 4f 55 54 45 52 29 21 3d 30 3b 0a 0a 20  JT_OUTER)!=0;.. 
24809 20 20 20 2f 2a 20 57 68 65 6e 20 74 68 65 20 4e     /* When the N
2480a 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 20 69  ATURAL keyword i
2480b 73 20 70 72 65 73 65 6e 74 2c 20 61 64 64 20 57  s present, add W
2480c 48 45 52 45 20 63 6c 61 75 73 65 20 74 65 72 6d  HERE clause term
2480d 73 20 66 6f 72 0a 20 20 20 20 2a 2a 20 65 76 65  s for.    ** eve
2480e 72 79 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 74  ry column that t
2480f 68 65 20 74 77 6f 20 74 61 62 6c 65 73 20 68 61  he two tables ha
24810 76 65 20 69 6e 20 63 6f 6d 6d 6f 6e 2e 0a 20 20  ve in common..  
24811 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69    */.    if( pRi
24812 67 68 74 2d 3e 6a 6f 69 6e 74 79 70 65 20 26 20  ght->jointype & 
24813 4a 54 5f 4e 41 54 55 52 41 4c 20 29 7b 0a 20 20  JT_NATURAL ){.  
24814 20 20 20 20 69 66 28 20 70 52 69 67 68 74 2d 3e      if( pRight->
24815 70 4f 6e 20 7c 7c 20 70 52 69 67 68 74 2d 3e 70  pOn || pRight->p
24816 55 73 69 6e 67 20 29 7b 0a 20 20 20 20 20 20 20  Using ){.       
24817 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
24818 28 70 50 61 72 73 65 2c 20 22 61 20 4e 41 54 55  (pParse, "a NATU
24819 52 41 4c 20 6a 6f 69 6e 20 6d 61 79 20 6e 6f 74  RAL join may not
2481a 20 68 61 76 65 20 22 0a 20 20 20 20 20 20 20 20   have ".        
2481b 20 20 20 22 61 6e 20 4f 4e 20 6f 72 20 55 53 49     "an ON or USI
2481c 4e 47 20 63 6c 61 75 73 65 22 2c 20 30 29 3b 0a  NG clause", 0);.
2481d 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
2481e 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2481f 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 4c 65 66 74  for(j=0; j<pLeft
24820 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b 29 7b  Tab->nCol; j++){
24821 0a 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a  .        char *z
24822 4e 61 6d 65 20 3d 20 70 4c 65 66 74 54 61 62 2d  Name = pLeftTab-
24823 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 3b 0a  >aCol[j].zName;.
24824 20 20 20 20 20 20 20 20 69 66 28 20 63 6f 6c 75          if( colu
24825 6d 6e 49 6e 64 65 78 28 70 52 69 67 68 74 54 61  mnIndex(pRightTa
24826 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a  b, zName)>=0 ){.
24827 20 20 20 20 20 20 20 20 20 20 61 64 64 57 68 65            addWhe
24828 72 65 54 65 72 6d 28 70 50 61 72 73 65 2c 20 7a  reTerm(pParse, z
24829 4e 61 6d 65 2c 20 70 4c 65 66 74 54 61 62 2c 20  Name, pLeftTab, 
2482a 70 4c 65 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a  pLeft->zAlias, .
2482b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2482c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 52                pR
2482d 69 67 68 74 54 61 62 2c 20 70 52 69 67 68 74 2d  ightTab, pRight-
2482e 3e 7a 41 6c 69 61 73 2c 0a 20 20 20 20 20 20 20  >zAlias,.       
2482f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24830 20 20 20 20 20 20 20 70 52 69 67 68 74 2d 3e 69         pRight->i
24831 43 75 72 73 6f 72 2c 20 26 70 2d 3e 70 57 68 65  Cursor, &p->pWhe
24832 72 65 2c 20 69 73 4f 75 74 65 72 29 3b 0a 20 20  re, isOuter);.  
24833 20 20 20 20 20 20 20 20 0a 20 20 20 20 20 20 20          .       
24834 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
24835 0a 0a 20 20 20 20 2f 2a 20 44 69 73 61 6c 6c 6f  ..    /* Disallo
24836 77 20 62 6f 74 68 20 4f 4e 20 61 6e 64 20 55 53  w both ON and US
24837 49 4e 47 20 63 6c 61 75 73 65 73 20 69 6e 20 74  ING clauses in t
24838 68 65 20 73 61 6d 65 20 6a 6f 69 6e 0a 20 20 20  he same join.   
24839 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 52 69 67   */.    if( pRig
2483a 68 74 2d 3e 70 4f 6e 20 26 26 20 70 52 69 67 68  ht->pOn && pRigh
2483b 74 2d 3e 70 55 73 69 6e 67 20 29 7b 0a 20 20 20  t->pUsing ){.   
2483c 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
2483d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e  sg(pParse, "cann
2483e 6f 74 20 68 61 76 65 20 62 6f 74 68 20 4f 4e 20  ot have both ON 
2483f 61 6e 64 20 55 53 49 4e 47 20 22 0a 20 20 20 20  and USING ".    
24840 20 20 20 20 22 63 6c 61 75 73 65 73 20 69 6e 20      "clauses in 
24841 74 68 65 20 73 61 6d 65 20 6a 6f 69 6e 22 29 3b  the same join");
24842 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
24843 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41  .    }..    /* A
24844 64 64 20 74 68 65 20 4f 4e 20 63 6c 61 75 73 65  dd the ON clause
24845 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
24846 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2c  he WHERE clause,
24847 20 63 6f 6e 6e 65 63 74 65 64 20 62 79 0a 20 20   connected by.  
24848 20 20 2a 2a 20 61 6e 20 41 4e 44 20 6f 70 65 72    ** an AND oper
24849 61 74 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  ator..    */.   
2484a 20 69 66 28 20 70 52 69 67 68 74 2d 3e 70 4f 6e   if( pRight->pOn
2484b 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 73   ){.      if( is
2484c 4f 75 74 65 72 20 29 20 73 65 74 4a 6f 69 6e 45  Outer ) setJoinE
2484d 78 70 72 28 70 52 69 67 68 74 2d 3e 70 4f 6e 2c  xpr(pRight->pOn,
2484e 20 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72   pRight->iCursor
2484f 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 57 68 65  );.      p->pWhe
24850 72 65 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  re = sqlite3Expr
24851 41 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  And(pParse->db, 
24852 70 2d 3e 70 57 68 65 72 65 2c 20 70 52 69 67 68  p->pWhere, pRigh
24853 74 2d 3e 70 4f 6e 29 3b 0a 20 20 20 20 20 20 70  t->pOn);.      p
24854 52 69 67 68 74 2d 3e 70 4f 6e 20 3d 20 30 3b 0a  Right->pOn = 0;.
24855 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 72      }..    /* Cr
24856 65 61 74 65 20 65 78 74 72 61 20 74 65 72 6d 73  eate extra terms
24857 20 6f 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c   on the WHERE cl
24858 61 75 73 65 20 66 6f 72 20 65 61 63 68 20 63 6f  ause for each co
24859 6c 75 6d 6e 20 6e 61 6d 65 64 0a 20 20 20 20 2a  lumn named.    *
2485a 2a 20 69 6e 20 74 68 65 20 55 53 49 4e 47 20 63  * in the USING c
2485b 6c 61 75 73 65 2e 20 20 45 78 61 6d 70 6c 65 3a  lause.  Example:
2485c 20 49 66 20 74 68 65 20 74 77 6f 20 74 61 62 6c   If the two tabl
2485d 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65 64 20  es to be joined 
2485e 61 72 65 20 0a 20 20 20 20 2a 2a 20 41 20 61 6e  are .    ** A an
2485f 64 20 42 20 61 6e 64 20 74 68 65 20 55 53 49 4e  d B and the USIN
24860 47 20 63 6c 61 75 73 65 20 6e 61 6d 65 73 20 58  G clause names X
24861 2c 20 59 2c 20 61 6e 64 20 5a 2c 20 74 68 65 6e  , Y, and Z, then
24862 20 61 64 64 20 74 68 69 73 0a 20 20 20 20 2a 2a   add this.    **
24863 20 74 6f 20 74 68 65 20 57 48 45 52 45 20 63 6c   to the WHERE cl
24864 61 75 73 65 3a 20 20 20 20 41 2e 58 3d 42 2e 58  ause:    A.X=B.X
24865 20 41 4e 44 20 41 2e 59 3d 42 2e 59 20 41 4e 44   AND A.Y=B.Y AND
24866 20 41 2e 5a 3d 42 2e 5a 0a 20 20 20 20 2a 2a 20   A.Z=B.Z.    ** 
24867 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72 20  Report an error 
24868 69 66 20 61 6e 79 20 63 6f 6c 75 6d 6e 20 6d 65  if any column me
24869 6e 74 69 6f 6e 65 64 20 69 6e 20 74 68 65 20 55  ntioned in the U
2486a 53 49 4e 47 20 63 6c 61 75 73 65 20 69 73 0a 20  SING clause is. 
2486b 20 20 20 2a 2a 20 6e 6f 74 20 63 6f 6e 74 61 69     ** not contai
2486c 6e 65 64 20 69 6e 20 62 6f 74 68 20 74 61 62 6c  ned in both tabl
2486d 65 73 20 74 6f 20 62 65 20 6a 6f 69 6e 65 64 2e  es to be joined.
2486e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20  .    */.    if( 
2486f 70 52 69 67 68 74 2d 3e 70 55 73 69 6e 67 20 29  pRight->pUsing )
24870 7b 0a 20 20 20 20 20 20 49 64 4c 69 73 74 20 2a  {.      IdList *
24871 70 4c 69 73 74 20 3d 20 70 52 69 67 68 74 2d 3e  pList = pRight->
24872 70 55 73 69 6e 67 3b 0a 20 20 20 20 20 20 66 6f  pUsing;.      fo
24873 72 28 6a 3d 30 3b 20 6a 3c 70 4c 69 73 74 2d 3e  r(j=0; j<pList->
24874 6e 49 64 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  nId; j++){.     
24875 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d     char *zName =
24876 20 70 4c 69 73 74 2d 3e 61 5b 6a 5d 2e 7a 4e 61   pList->a[j].zNa
24877 6d 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  me;.        if( 
24878 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66  columnIndex(pLef
24879 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c 30 20 7c  tTab, zName)<0 |
2487a 7c 20 63 6f 6c 75 6d 6e 49 6e 64 65 78 28 70 52  | columnIndex(pR
2487b 69 67 68 74 54 61 62 2c 20 7a 4e 61 6d 65 29 3c  ightTab, zName)<
2487c 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 73  0 ){.          s
2487d 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2487e 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 6a  Parse, "cannot j
2487f 6f 69 6e 20 75 73 69 6e 67 20 63 6f 6c 75 6d 6e  oin using column
24880 20 25 73 20 2d 20 63 6f 6c 75 6d 6e 20 22 0a 20   %s - column ". 
24881 20 20 20 20 20 20 20 20 20 20 20 22 6e 6f 74 20             "not 
24882 70 72 65 73 65 6e 74 20 69 6e 20 62 6f 74 68 20  present in both 
24883 74 61 62 6c 65 73 22 2c 20 7a 4e 61 6d 65 29 3b  tables", zName);
24884 0a 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72  .          retur
24885 6e 20 31 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  n 1;.        }. 
24886 20 20 20 20 20 20 20 61 64 64 57 68 65 72 65 54         addWhereT
24887 65 72 6d 28 70 50 61 72 73 65 2c 20 7a 4e 61 6d  erm(pParse, zNam
24888 65 2c 20 70 4c 65 66 74 54 61 62 2c 20 70 4c 65  e, pLeftTab, pLe
24889 66 74 2d 3e 7a 41 6c 69 61 73 2c 20 0a 20 20 20  ft->zAlias, .   
2488a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2488b 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74 54           pRightT
2488c 61 62 2c 20 70 52 69 67 68 74 2d 3e 7a 41 6c 69  ab, pRight->zAli
2488d 61 73 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  as,.            
2488e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2488f 70 52 69 67 68 74 2d 3e 69 43 75 72 73 6f 72 2c  pRight->iCursor,
24890 20 26 70 2d 3e 70 57 68 65 72 65 2c 20 69 73 4f   &p->pWhere, isO
24891 75 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  uter);.      }. 
24892 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72     }.  }.  retur
24893 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  n 0;.}../*.** In
24894 73 65 72 74 20 63 6f 64 65 20 69 6e 74 6f 20 22  sert code into "
24895 76 22 20 74 68 61 74 20 77 69 6c 6c 20 70 75 73  v" that will pus
24896 68 20 74 68 65 20 72 65 63 6f 72 64 20 6f 6e 20  h the record on 
24897 74 68 65 20 74 6f 70 20 6f 66 20 74 68 65 0a 2a  the top of the.*
24898 2a 20 73 74 61 63 6b 20 69 6e 74 6f 20 74 68 65  * stack into the
24899 20 73 6f 72 74 65 72 2e 0a 2a 2f 0a 73 74 61 74   sorter..*/.stat
2489a 69 63 20 76 6f 69 64 20 70 75 73 68 4f 6e 74 6f  ic void pushOnto
2489b 53 6f 72 74 65 72 28 0a 20 20 50 61 72 73 65 20  Sorter(.  Parse 
2489c 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
2489d 20 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65   /* Parser conte
2489e 78 74 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74  xt */.  ExprList
2489f 20 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 2f   *pOrderBy,    /
248a0 2a 20 54 68 65 20 4f 52 44 45 52 20 42 59 20 63  * The ORDER BY c
248a1 6c 61 75 73 65 20 2a 2f 0a 20 20 53 65 6c 65 63  lause */.  Selec
248a2 74 20 2a 70 53 65 6c 65 63 74 2c 20 20 20 20 20  t *pSelect,     
248a3 20 20 2f 2a 20 54 68 65 20 77 68 6f 6c 65 20 53    /* The whole S
248a4 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
248a5 2a 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61  */.  int regData
248a6 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
248a7 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
248a8 64 61 74 61 20 74 6f 20 62 65 20 73 6f 72 74 65  data to be sorte
248a9 64 20 2a 2f 0a 29 7b 0a 20 20 56 64 62 65 20 2a  d */.){.  Vdbe *
248aa 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
248ab 65 3b 0a 20 20 69 6e 74 20 6e 45 78 70 72 20 3d  e;.  int nExpr =
248ac 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
248ad 3b 0a 20 20 69 6e 74 20 72 65 67 42 61 73 65 20  ;.  int regBase 
248ae 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
248af 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20 6e 45  Range(pParse, nE
248b0 78 70 72 2b 32 29 3b 0a 20 20 69 6e 74 20 72 65  xpr+2);.  int re
248b1 67 52 65 63 6f 72 64 20 3d 20 73 71 6c 69 74 65  gRecord = sqlite
248b2 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72  3GetTempReg(pPar
248b3 73 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78  se);.  sqlite3Ex
248b4 70 72 43 6f 64 65 45 78 70 72 4c 69 73 74 28 70  prCodeExprList(p
248b5 50 61 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c  Parse, pOrderBy,
248b6 20 72 65 67 42 61 73 65 2c 20 30 29 3b 0a 20 20   regBase, 0);.  
248b7 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
248b8 32 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65  2(v, OP_Sequence
248b9 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  , pOrderBy->iECu
248ba 72 73 6f 72 2c 20 72 65 67 42 61 73 65 2b 6e 45  rsor, regBase+nE
248bb 78 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  xpr);.  sqlite3E
248bc 78 70 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72  xprCodeMove(pPar
248bd 73 65 2c 20 72 65 67 44 61 74 61 2c 20 72 65 67  se, regData, reg
248be 42 61 73 65 2b 6e 45 78 70 72 2b 31 2c 20 31 29  Base+nExpr+1, 1)
248bf 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
248c0 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65  ddOp3(v, OP_Make
248c1 52 65 63 6f 72 64 2c 20 72 65 67 42 61 73 65 2c  Record, regBase,
248c2 20 6e 45 78 70 72 20 2b 20 32 2c 20 72 65 67 52   nExpr + 2, regR
248c3 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74 65  ecord);.  sqlite
248c4 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
248c5 50 5f 49 64 78 49 6e 73 65 72 74 2c 20 70 4f 72  P_IdxInsert, pOr
248c6 64 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 2c  derBy->iECursor,
248c7 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 73   regRecord);.  s
248c8 71 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d  qlite3ReleaseTem
248c9 70 52 65 67 28 70 50 61 72 73 65 2c 20 72 65 67  pReg(pParse, reg
248ca 52 65 63 6f 72 64 29 3b 0a 20 20 73 71 6c 69 74  Record);.  sqlit
248cb 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e  e3ReleaseTempRan
248cc 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 42 61  ge(pParse, regBa
248cd 73 65 2c 20 6e 45 78 70 72 2b 32 29 3b 0a 20 20  se, nExpr+2);.  
248ce 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4c 69  if( pSelect->iLi
248cf 6d 69 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 61  mit ){.    int a
248d0 64 64 72 31 2c 20 61 64 64 72 32 3b 0a 20 20 20  ddr1, addr2;.   
248d1 20 69 6e 74 20 69 4c 69 6d 69 74 3b 0a 20 20 20   int iLimit;.   
248d2 20 69 66 28 20 70 53 65 6c 65 63 74 2d 3e 69 4f   if( pSelect->iO
248d3 66 66 73 65 74 20 29 7b 0a 20 20 20 20 20 20 69  ffset ){.      i
248d4 4c 69 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d  Limit = pSelect-
248d5 3e 69 4f 66 66 73 65 74 2b 31 3b 0a 20 20 20 20  >iOffset+1;.    
248d6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 4c 69  }else{.      iLi
248d7 6d 69 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 69  mit = pSelect->i
248d8 4c 69 6d 69 74 3b 0a 20 20 20 20 7d 0a 20 20 20  Limit;.    }.   
248d9 20 61 64 64 72 31 20 3d 20 73 71 6c 69 74 65 33   addr1 = sqlite3
248da 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
248db 5f 49 66 5a 65 72 6f 2c 20 69 4c 69 6d 69 74 29  _IfZero, iLimit)
248dc 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
248dd 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64  eAddOp2(v, OP_Ad
248de 64 49 6d 6d 2c 20 69 4c 69 6d 69 74 2c 20 2d 31  dImm, iLimit, -1
248df 29 3b 0a 20 20 20 20 61 64 64 72 32 20 3d 20 73  );.    addr2 = s
248e0 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 30  qlite3VdbeAddOp0
248e1 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b 0a 20 20  (v, OP_Goto);.  
248e2 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
248e3 70 48 65 72 65 28 76 2c 20 61 64 64 72 31 29 3b  pHere(v, addr1);
248e4 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
248e5 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4c 61 73  AddOp1(v, OP_Las
248e6 74 2c 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43  t, pOrderBy->iEC
248e7 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 71 6c 69  ursor);.    sqli
248e8 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
248e9 20 4f 50 5f 44 65 6c 65 74 65 2c 20 70 4f 72 64   OP_Delete, pOrd
248ea 65 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 29 3b  erBy->iECursor);
248eb 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
248ec 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72  JumpHere(v, addr
248ed 32 29 3b 0a 20 20 20 20 70 53 65 6c 65 63 74 2d  2);.    pSelect-
248ee 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 7d  >iLimit = 0;.  }
248ef 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 63 6f  .}../*.** Add co
248f0 64 65 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20  de to implement 
248f1 74 68 65 20 4f 46 46 53 45 54 0a 2a 2f 0a 73 74  the OFFSET.*/.st
248f2 61 74 69 63 20 76 6f 69 64 20 63 6f 64 65 4f 66  atic void codeOf
248f3 66 73 65 74 28 0a 20 20 56 64 62 65 20 2a 76 2c  fset(.  Vdbe *v,
248f4 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
248f5 65 72 61 74 65 20 63 6f 64 65 20 69 6e 74 6f 20  erate code into 
248f6 74 68 69 73 20 56 4d 20 2a 2f 0a 20 20 53 65 6c  this VM */.  Sel
248f7 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20 2f  ect *p,        /
248f8 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61  * The SELECT sta
248f9 74 65 6d 65 6e 74 20 62 65 69 6e 67 20 63 6f 64  tement being cod
248fa 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6e  ed */.  int iCon
248fb 74 69 6e 75 65 20 20 20 20 20 2f 2a 20 4a 75 6d  tinue     /* Jum
248fc 70 20 68 65 72 65 20 74 6f 20 73 6b 69 70 20 74  p here to skip t
248fd 68 65 20 63 75 72 72 65 6e 74 20 72 65 63 6f 72  he current recor
248fe 64 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20 70 2d  d */.){.  if( p-
248ff 3e 69 4f 66 66 73 65 74 20 26 26 20 69 43 6f 6e  >iOffset && iCon
24900 74 69 6e 75 65 21 3d 30 20 29 7b 0a 20 20 20 20  tinue!=0 ){.    
24901 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20 73 71  int addr;.    sq
24902 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24903 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d  v, OP_AddImm, p-
24904 3e 69 4f 66 66 73 65 74 2c 20 2d 31 29 3b 0a 20  >iOffset, -1);. 
24905 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65     addr = sqlite
24906 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
24907 50 5f 49 66 4e 65 67 2c 20 70 2d 3e 69 4f 66 66  P_IfNeg, p->iOff
24908 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  set);.    sqlite
24909 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2490a 50 5f 47 6f 74 6f 2c 20 30 2c 20 69 43 6f 6e 74  P_Goto, 0, iCont
2490b 69 6e 75 65 29 3b 0a 20 20 20 20 56 64 62 65 43  inue);.    VdbeC
2490c 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 73 6b 69 70  omment((v, "skip
2490d 20 4f 46 46 53 45 54 20 72 65 63 6f 72 64 73 22   OFFSET records"
2490e 29 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56  ));.    sqlite3V
2490f 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 61  dbeJumpHere(v, a
24910 64 64 72 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ddr);.  }.}../*.
24911 2a 2a 20 41 64 64 20 63 6f 64 65 20 74 68 61 74  ** Add code that
24912 20 77 69 6c 6c 20 63 68 65 63 6b 20 74 6f 20 6d   will check to m
24913 61 6b 65 20 73 75 72 65 20 74 68 65 20 4e 20 72  ake sure the N r
24914 65 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e  egisters startin
24915 67 20 61 74 20 69 4d 65 6d 0a 2a 2a 20 66 6f 72  g at iMem.** for
24916 6d 20 61 20 64 69 73 74 69 6e 63 74 20 65 6e 74  m a distinct ent
24917 72 79 2e 20 20 69 54 61 62 20 69 73 20 61 20 73  ry.  iTab is a s
24918 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61  orting index tha
24919 74 20 68 6f 6c 64 73 20 70 72 65 76 69 6f 75 73  t holds previous
2491a 6c 79 0a 2a 2a 20 73 65 65 6e 20 63 6f 6d 62 69  ly.** seen combi
2491b 6e 61 74 69 6f 6e 73 20 6f 66 20 74 68 65 20 4e  nations of the N
2491c 20 76 61 6c 75 65 73 2e 20 20 41 20 6e 65 77 20   values.  A new 
2491d 65 6e 74 72 79 20 69 73 20 6d 61 64 65 20 69 6e  entry is made in
2491e 20 69 54 61 62 0a 2a 2a 20 69 66 20 74 68 65 20   iTab.** if the 
2491f 63 75 72 72 65 6e 74 20 4e 20 76 61 6c 75 65 73  current N values
24920 20 61 72 65 20 6e 65 77 2e 0a 2a 2a 0a 2a 2a 20   are new..**.** 
24921 41 20 6a 75 6d 70 20 74 6f 20 61 64 64 72 52 65  A jump to addrRe
24922 70 65 61 74 20 69 73 20 6d 61 64 65 20 61 6e 64  peat is made and
24923 20 74 68 65 20 4e 2b 31 20 76 61 6c 75 65 73 20   the N+1 values 
24924 61 72 65 20 70 6f 70 70 65 64 20 66 72 6f 6d 20  are popped from 
24925 74 68 65 0a 2a 2a 20 73 74 61 63 6b 20 69 66 20  the.** stack if 
24926 74 68 65 20 74 6f 70 20 4e 20 65 6c 65 6d 65 6e  the top N elemen
24927 74 73 20 61 72 65 20 6e 6f 74 20 64 69 73 74 69  ts are not disti
24928 6e 63 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  nct..*/.static v
24929 6f 69 64 20 63 6f 64 65 44 69 73 74 69 6e 63 74  oid codeDistinct
2492a 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
2492b 65 2c 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e  e,     /* Parsin
2492c 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65 72  g and code gener
2492d 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f  ating context */
2492e 0a 20 20 69 6e 74 20 69 54 61 62 2c 20 20 20 20  .  int iTab,    
2492f 20 20 20 20 20 20 2f 2a 20 41 20 73 6f 72 74 69        /* A sorti
24930 6e 67 20 69 6e 64 65 78 20 75 73 65 64 20 74 6f  ng index used to
24931 20 74 65 73 74 20 66 6f 72 20 64 69 73 74 69 6e   test for distin
24932 63 74 6e 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20  ctness */.  int 
24933 61 64 64 72 52 65 70 65 61 74 2c 20 20 20 20 2f  addrRepeat,    /
24934 2a 20 4a 75 6d 70 20 74 6f 20 68 65 72 65 20 69  * Jump to here i
24935 66 20 6e 6f 74 20 64 69 73 74 69 6e 63 74 20 2a  f not distinct *
24936 2f 0a 20 20 69 6e 74 20 4e 2c 20 20 20 20 20 20  /.  int N,      
24937 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
24938 20 6f 66 20 65 6c 65 6d 65 6e 74 73 20 2a 2f 0a   of elements */.
24939 20 20 69 6e 74 20 69 4d 65 6d 20 20 20 20 20 20    int iMem      
2493a 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 65 6c       /* First el
2493b 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 56 64  ement */.){.  Vd
2493c 62 65 20 2a 76 3b 0a 20 20 69 6e 74 20 72 31 3b  be *v;.  int r1;
2493d 0a 0a 20 20 76 20 3d 20 70 50 61 72 73 65 2d 3e  ..  v = pParse->
2493e 70 56 64 62 65 3b 0a 20 20 72 31 20 3d 20 73 71  pVdbe;.  r1 = sq
2493f 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
24940 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c 69 74  pParse);.  sqlit
24941 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
24942 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 69  OP_MakeRecord, i
24943 4d 65 6d 2c 20 4e 2c 20 72 31 29 3b 0a 20 20 73  Mem, N, r1);.  s
24944 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33  qlite3VdbeAddOp3
24945 28 76 2c 20 4f 50 5f 46 6f 75 6e 64 2c 20 69 54  (v, OP_Found, iT
24946 61 62 2c 20 61 64 64 72 52 65 70 65 61 74 2c 20  ab, addrRepeat, 
24947 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  r1);.  sqlite3Vd
24948 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
24949 64 78 49 6e 73 65 72 74 2c 20 69 54 61 62 2c 20  dxInsert, iTab, 
2494a 72 31 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65  r1);.  sqlite3Re
2494b 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
2494c 72 73 65 2c 20 72 31 29 3b 0a 7d 0a 0a 2f 2a 0a  rse, r1);.}../*.
2494d 2a 2a 20 47 65 6e 65 72 61 74 65 20 61 6e 20 65  ** Generate an e
2494e 72 72 6f 72 20 6d 65 73 73 61 67 65 20 77 68 65  rror message whe
2494f 6e 20 61 20 53 45 4c 45 43 54 20 69 73 20 75 73  n a SELECT is us
24950 65 64 20 77 69 74 68 69 6e 20 61 20 73 75 62 65  ed within a sube
24951 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 28 65 78  xpression.** (ex
24952 61 6d 70 6c 65 3a 20 20 22 61 20 49 4e 20 28 53  ample:  "a IN (S
24953 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 61 62  ELECT * FROM tab
24954 6c 65 29 22 29 20 62 75 74 20 69 74 20 68 61 73  le)") but it has
24955 20 6d 6f 72 65 20 74 68 61 6e 20 31 20 72 65 73   more than 1 res
24956 75 6c 74 0a 2a 2a 20 63 6f 6c 75 6d 6e 2e 20 20  ult.** column.  
24957 57 65 20 64 6f 20 74 68 69 73 20 69 6e 20 61 20  We do this in a 
24958 73 75 62 72 6f 75 74 69 6e 65 20 62 65 63 61 75  subroutine becau
24959 73 65 20 74 68 65 20 65 72 72 6f 72 20 6f 63 63  se the error occ
2495a 75 72 73 20 69 6e 20 6d 75 6c 74 69 70 6c 65 0a  urs in multiple.
2495b 2a 2a 20 70 6c 61 63 65 73 2e 0a 2a 2f 0a 73 74  ** places..*/.st
2495c 61 74 69 63 20 69 6e 74 20 63 68 65 63 6b 46 6f  atic int checkFo
2495d 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c 65  rMultiColumnSele
2495e 63 74 45 72 72 6f 72 28 0a 20 20 50 61 72 73 65  ctError(.  Parse
2495f 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
24960 2f 2a 20 50 61 72 73 65 20 63 6f 6e 74 65 78 74  /* Parse context
24961 2e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73  . */.  SelectDes
24962 74 20 2a 70 44 65 73 74 2c 20 20 20 2f 2a 20 44  t *pDest,   /* D
24963 65 73 74 69 6e 61 74 69 6f 6e 20 6f 66 20 53 45  estination of SE
24964 4c 45 43 54 20 72 65 73 75 6c 74 73 20 2a 2f 0a  LECT results */.
24965 20 20 69 6e 74 20 6e 45 78 70 72 20 20 20 20 20    int nExpr     
24966 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
24967 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d   of result colum
24968 6e 73 20 72 65 74 75 72 6e 65 64 20 62 79 20 53  ns returned by S
24969 45 4c 45 43 54 20 2a 2f 0a 29 7b 0a 20 20 69 6e  ELECT */.){.  in
2496a 74 20 65 44 65 73 74 20 3d 20 70 44 65 73 74 2d  t eDest = pDest-
2496b 3e 65 44 65 73 74 3b 0a 20 20 69 66 28 20 6e 45  >eDest;.  if( nE
2496c 78 70 72 3e 31 20 26 26 20 28 65 44 65 73 74 3d  xpr>1 && (eDest=
2496d 3d 53 52 54 5f 4d 65 6d 20 7c 7c 20 65 44 65 73  =SRT_Mem || eDes
2496e 74 3d 3d 53 52 54 5f 53 65 74 29 20 29 7b 0a 20  t==SRT_Set) ){. 
2496f 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
24970 73 67 28 70 50 61 72 73 65 2c 20 22 6f 6e 6c 79  sg(pParse, "only
24971 20 61 20 73 69 6e 67 6c 65 20 72 65 73 75 6c 74   a single result
24972 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 22 0a 20   allowed for ". 
24973 20 20 20 20 20 20 22 61 20 53 45 4c 45 43 54 20        "a SELECT 
24974 74 68 61 74 20 69 73 20 70 61 72 74 20 6f 66 20  that is part of 
24975 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b  an expression");
24976 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20  .    return 1;. 
24977 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
24978 72 6e 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  rn 0;.  }.}../*.
24979 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
2497a 67 65 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f  generates the co
2497b 64 65 20 66 6f 72 20 74 68 65 20 69 6e 73 69 64  de for the insid
2497c 65 20 6f 66 20 74 68 65 20 69 6e 6e 65 72 20 6c  e of the inner l
2497d 6f 6f 70 0a 2a 2a 20 6f 66 20 61 20 53 45 4c 45  oop.** of a SELE
2497e 43 54 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 73 72 63  CT..**.** If src
2497f 54 61 62 20 61 6e 64 20 6e 43 6f 6c 75 6d 6e 20  Tab and nColumn 
24980 61 72 65 20 62 6f 74 68 20 7a 65 72 6f 2c 20 74  are both zero, t
24981 68 65 6e 20 74 68 65 20 70 45 4c 69 73 74 20 65  hen the pEList e
24982 78 70 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 61 72  xpressions.** ar
24983 65 20 65 76 61 6c 75 61 74 65 64 20 69 6e 20 6f  e evaluated in o
24984 72 64 65 72 20 74 6f 20 67 65 74 20 74 68 65 20  rder to get the 
24985 64 61 74 61 20 66 6f 72 20 74 68 69 73 20 72 6f  data for this ro
24986 77 2e 20 20 49 66 20 6e 43 6f 6c 75 6d 6e 3e 30  w.  If nColumn>0
24987 0a 2a 2a 20 74 68 65 6e 20 64 61 74 61 20 69 73  .** then data is
24988 20 70 75 6c 6c 65 64 20 66 72 6f 6d 20 73 72 63   pulled from src
24989 54 61 62 20 61 6e 64 20 70 45 4c 69 73 74 20 69  Tab and pEList i
2498a 73 20 75 73 65 64 20 6f 6e 6c 79 20 74 6f 20 67  s used only to g
2498b 65 74 20 74 68 65 0a 2a 2a 20 64 61 74 61 74 79  et the.** dataty
2498c 70 65 73 20 66 6f 72 20 65 61 63 68 20 63 6f 6c  pes for each col
2498d 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  umn..*/.static v
2498e 6f 69 64 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c  oid selectInnerL
2498f 6f 6f 70 28 0a 20 20 50 61 72 73 65 20 2a 70 50  oop(.  Parse *pP
24990 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f  arse,          /
24991 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 6f 6e  * The parser con
24992 74 65 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74  text */.  Select
24993 20 2a 70 2c 20 20 20 20 20 20 20 20 20 20 20 20   *p,            
24994 20 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74    /* The complet
24995 65 20 73 65 6c 65 63 74 20 73 74 61 74 65 6d 65  e select stateme
24996 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 20 2a  nt being coded *
24997 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
24998 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 4c  List,       /* L
24999 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 62 65  ist of values be
2499a 69 6e 67 20 65 78 74 72 61 63 74 65 64 20 2a 2f  ing extracted */
2499b 0a 20 20 69 6e 74 20 73 72 63 54 61 62 2c 20 20  .  int srcTab,  
2499c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 75             /* Pu
2499d 6c 6c 20 64 61 74 61 20 66 72 6f 6d 20 74 68 69  ll data from thi
2499e 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74  s table */.  int
2499f 20 6e 43 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20   nColumn,       
249a0 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
249a1 66 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 74 68 65  f columns in the
249a2 20 73 6f 75 72 63 65 20 74 61 62 6c 65 20 2a 2f   source table */
249a3 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72  .  ExprList *pOr
249a4 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 49 66  derBy,     /* If
249a5 20 6e 6f 74 20 4e 55 4c 4c 2c 20 73 6f 72 74 20   not NULL, sort 
249a6 72 65 73 75 6c 74 73 20 75 73 69 6e 67 20 74 68  results using th
249a7 69 73 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20  is key */.  int 
249a8 64 69 73 74 69 6e 63 74 2c 20 20 20 20 20 20 20  distinct,       
249a9 20 20 20 20 2f 2a 20 49 66 20 3e 3d 30 2c 20 6d      /* If >=0, m
249aa 61 6b 65 20 73 75 72 65 20 72 65 73 75 6c 74 73  ake sure results
249ab 20 61 72 65 20 64 69 73 74 69 6e 63 74 20 2a 2f   are distinct */
249ac 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
249ad 44 65 73 74 2c 20 20 20 20 20 20 2f 2a 20 48 6f  Dest,      /* Ho
249ae 77 20 74 6f 20 64 69 73 70 6f 73 65 20 6f 66 20  w to dispose of 
249af 74 68 65 20 72 65 73 75 6c 74 73 20 2a 2f 0a 20  the results */. 
249b0 20 69 6e 74 20 69 43 6f 6e 74 69 6e 75 65 2c 20   int iContinue, 
249b1 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70           /* Jump
249b2 20 68 65 72 65 20 74 6f 20 63 6f 6e 74 69 6e 75   here to continu
249b3 65 20 77 69 74 68 20 6e 65 78 74 20 72 6f 77 20  e with next row 
249b4 2a 2f 0a 20 20 69 6e 74 20 69 42 72 65 61 6b 20  */.  int iBreak 
249b5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
249b6 4a 75 6d 70 20 68 65 72 65 20 74 6f 20 62 72 65  Jump here to bre
249b7 61 6b 20 6f 75 74 20 6f 66 20 74 68 65 20 69 6e  ak out of the in
249b8 6e 65 72 20 6c 6f 6f 70 20 2a 2f 0a 29 7b 0a 20  ner loop */.){. 
249b9 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
249ba 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e 74 20  e->pVdbe;.  int 
249bb 69 3b 0a 20 20 69 6e 74 20 68 61 73 44 69 73 74  i;.  int hasDist
249bc 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  inct;        /* 
249bd 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53 54  True if the DIST
249be 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73 20  INCT keyword is 
249bf 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e 74  present */.  int
249c0 20 72 65 67 52 65 73 75 6c 74 3b 20 20 20 20 20   regResult;     
249c1 20 20 20 20 20 20 20 20 20 2f 2a 20 53 74 61 72           /* Star
249c2 74 20 6f 66 20 6d 65 6d 6f 72 79 20 68 6f 6c 64  t of memory hold
249c3 69 6e 67 20 72 65 73 75 6c 74 20 73 65 74 20 2a  ing result set *
249c4 2f 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20  /.  int eDest = 
249c5 70 44 65 73 74 2d 3e 65 44 65 73 74 3b 20 20 20  pDest->eDest;   
249c6 2f 2a 20 48 6f 77 20 74 6f 20 64 69 73 70 6f 73  /* How to dispos
249c7 65 20 6f 66 20 72 65 73 75 6c 74 73 20 2a 2f 0a  e of results */.
249c8 20 20 69 6e 74 20 69 50 61 72 6d 20 3d 20 70 44    int iParm = pD
249c9 65 73 74 2d 3e 69 50 61 72 6d 3b 20 20 20 2f 2a  est->iParm;   /*
249ca 20 46 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20   First argument 
249cb 74 6f 20 64 69 73 70 6f 73 61 6c 20 6d 65 74 68  to disposal meth
249cc 6f 64 20 2a 2f 0a 20 20 69 6e 74 20 6e 52 65 73  od */.  int nRes
249cd 75 6c 74 43 6f 6c 3b 20 20 20 20 20 20 20 20 20  ultCol;         
249ce 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
249cf 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73 20   result columns 
249d0 2a 2f 0a 0a 20 20 69 66 28 20 76 3d 3d 30 20 29  */..  if( v==0 )
249d1 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73 65 72   return;.  asser
249d2 74 28 20 70 45 4c 69 73 74 21 3d 30 20 29 3b 0a  t( pEList!=0 );.
249d3 20 20 68 61 73 44 69 73 74 69 6e 63 74 20 3d 20    hasDistinct = 
249d4 64 69 73 74 69 6e 63 74 3e 3d 30 3b 0a 20 20 69  distinct>=0;.  i
249d5 66 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 26  f( pOrderBy==0 &
249d6 26 20 21 68 61 73 44 69 73 74 69 6e 63 74 20 29  & !hasDistinct )
249d7 7b 0a 20 20 20 20 63 6f 64 65 4f 66 66 73 65 74  {.    codeOffset
249d8 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69 6e 75 65  (v, p, iContinue
249d9 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 75 6c  );.  }..  /* Pul
249da 6c 20 74 68 65 20 72 65 71 75 65 73 74 65 64 20  l the requested 
249db 63 6f 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20  columns..  */.  
249dc 69 66 28 20 6e 43 6f 6c 75 6d 6e 3e 30 20 29 7b  if( nColumn>0 ){
249dd 0a 20 20 20 20 6e 52 65 73 75 6c 74 43 6f 6c 20  .    nResultCol 
249de 3d 20 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 7d 65 6c  = nColumn;.  }el
249df 73 65 7b 0a 20 20 20 20 6e 52 65 73 75 6c 74 43  se{.    nResultC
249e0 6f 6c 20 3d 20 70 45 4c 69 73 74 2d 3e 6e 45 78  ol = pEList->nEx
249e1 70 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 44  pr;.  }.  if( pD
249e2 65 73 74 2d 3e 69 4d 65 6d 3d 3d 30 20 29 7b 0a  est->iMem==0 ){.
249e3 20 20 20 20 70 44 65 73 74 2d 3e 69 4d 65 6d 20      pDest->iMem 
249e4 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 31  = pParse->nMem+1
249e5 3b 0a 20 20 20 20 70 44 65 73 74 2d 3e 6e 4d 65  ;.    pDest->nMe
249e6 6d 20 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a  m = nResultCol;.
249e7 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
249e8 20 2b 3d 20 6e 52 65 73 75 6c 74 43 6f 6c 3b 0a   += nResultCol;.
249e9 20 20 7d 65 6c 73 65 20 69 66 28 20 70 44 65 73    }else if( pDes
249ea 74 2d 3e 6e 4d 65 6d 21 3d 6e 52 65 73 75 6c 74  t->nMem!=nResult
249eb 43 6f 6c 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68  Col ){.    /* Th
249ec 69 73 20 68 61 70 70 65 6e 73 20 77 68 65 6e 20  is happens when 
249ed 74 77 6f 20 53 45 4c 45 43 54 73 20 6f 66 20 61  two SELECTs of a
249ee 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54   compound SELECT
249ef 20 68 61 76 65 20 64 69 66 66 65 72 69 6e 67 0a   have differing.
249f0 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 73 20 6f      ** numbers o
249f1 66 20 72 65 73 75 6c 74 20 63 6f 6c 75 6d 6e 73  f result columns
249f2 2e 20 20 54 68 65 20 65 72 72 6f 72 20 6d 65 73  .  The error mes
249f3 73 61 67 65 20 77 69 6c 6c 20 62 65 20 67 65 6e  sage will be gen
249f4 65 72 61 74 65 64 20 62 79 0a 20 20 20 20 2a 2a  erated by.    **
249f5 20 61 20 68 69 67 68 65 72 2d 6c 65 76 65 6c 20   a higher-level 
249f6 72 6f 75 74 69 6e 65 2e 20 2a 2f 0a 20 20 20 20  routine. */.    
249f7 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 72 65  return;.  }.  re
249f8 67 52 65 73 75 6c 74 20 3d 20 70 44 65 73 74 2d  gResult = pDest-
249f9 3e 69 4d 65 6d 3b 0a 20 20 69 66 28 20 6e 43 6f  >iMem;.  if( nCo
249fa 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20 20 66 6f  lumn>0 ){.    fo
249fb 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e  r(i=0; i<nColumn
249fc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
249fd 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
249fe 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 73 72  v, OP_Column, sr
249ff 63 54 61 62 2c 20 69 2c 20 72 65 67 52 65 73 75  cTab, i, regResu
24a00 6c 74 2b 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  lt+i);.    }.  }
24a01 65 6c 73 65 20 69 66 28 20 65 44 65 73 74 21 3d  else if( eDest!=
24a02 53 52 54 5f 45 78 69 73 74 73 20 29 7b 0a 20 20  SRT_Exists ){.  
24a03 20 20 2f 2a 20 49 66 20 74 68 65 20 64 65 73 74    /* If the dest
24a04 69 6e 61 74 69 6f 6e 20 69 73 20 61 6e 20 45 58  ination is an EX
24a05 49 53 54 53 28 2e 2e 2e 29 20 65 78 70 72 65 73  ISTS(...) expres
24a06 73 69 6f 6e 2c 20 74 68 65 20 61 63 74 75 61 6c  sion, the actual
24a07 0a 20 20 20 20 2a 2a 20 76 61 6c 75 65 73 20 72  .    ** values r
24a08 65 74 75 72 6e 65 64 20 62 79 20 74 68 65 20 53  eturned by the S
24a09 45 4c 45 43 54 20 61 72 65 20 6e 6f 74 20 72 65  ELECT are not re
24a0a 71 75 69 72 65 64 2e 0a 20 20 20 20 2a 2f 0a 20  quired..    */. 
24a0b 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
24a0c 64 65 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  deExprList(pPars
24a0d 65 2c 20 70 45 4c 69 73 74 2c 20 72 65 67 52 65  e, pEList, regRe
24a0e 73 75 6c 74 2c 20 65 44 65 73 74 3d 3d 53 52 54  sult, eDest==SRT
24a0f 5f 4f 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20  _Output);.  }.  
24a10 6e 43 6f 6c 75 6d 6e 20 3d 20 6e 52 65 73 75 6c  nColumn = nResul
24a11 74 43 6f 6c 3b 0a 0a 20 20 2f 2a 20 49 66 20 74  tCol;..  /* If t
24a12 68 65 20 44 49 53 54 49 4e 43 54 20 6b 65 79 77  he DISTINCT keyw
24a13 6f 72 64 20 77 61 73 20 70 72 65 73 65 6e 74 20  ord was present 
24a14 6f 6e 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  on the SELECT st
24a15 61 74 65 6d 65 6e 74 0a 20 20 2a 2a 20 61 6e 64  atement.  ** and
24a16 20 74 68 69 73 20 72 6f 77 20 68 61 73 20 62 65   this row has be
24a17 65 6e 20 73 65 65 6e 20 62 65 66 6f 72 65 2c 20  en seen before, 
24a18 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6d 61 6b 65  then do not make
24a19 20 74 68 69 73 20 72 6f 77 0a 20 20 2a 2a 20 70   this row.  ** p
24a1a 61 72 74 20 6f 66 20 74 68 65 20 72 65 73 75 6c  art of the resul
24a1b 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 68 61  t..  */.  if( ha
24a1c 73 44 69 73 74 69 6e 63 74 20 29 7b 0a 20 20 20  sDistinct ){.   
24a1d 20 61 73 73 65 72 74 28 20 70 45 4c 69 73 74 21   assert( pEList!
24a1e 3d 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  =0 );.    assert
24a1f 28 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 3d  ( pEList->nExpr=
24a20 3d 6e 43 6f 6c 75 6d 6e 20 29 3b 0a 20 20 20 20  =nColumn );.    
24a21 63 6f 64 65 44 69 73 74 69 6e 63 74 28 70 50 61  codeDistinct(pPa
24a22 72 73 65 2c 20 64 69 73 74 69 6e 63 74 2c 20 69  rse, distinct, i
24a23 43 6f 6e 74 69 6e 75 65 2c 20 6e 43 6f 6c 75 6d  Continue, nColum
24a24 6e 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20  n, regResult);. 
24a25 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 3d     if( pOrderBy=
24a26 3d 30 20 29 7b 0a 20 20 20 20 20 20 63 6f 64 65  =0 ){.      code
24a27 4f 66 66 73 65 74 28 76 2c 20 70 2c 20 69 43 6f  Offset(v, p, iCo
24a28 6e 74 69 6e 75 65 29 3b 0a 20 20 20 20 7d 0a 20  ntinue);.    }. 
24a29 20 7d 0a 0a 20 20 69 66 28 20 63 68 65 63 6b 46   }..  if( checkF
24a2a 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53 65 6c  orMultiColumnSel
24a2b 65 63 74 45 72 72 6f 72 28 70 50 61 72 73 65 2c  ectError(pParse,
24a2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74 2d 3e   pDest, pEList->
24a2d 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20 72 65  nExpr) ){.    re
24a2e 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  turn;.  }..  swi
24a2f 74 63 68 28 20 65 44 65 73 74 20 29 7b 0a 20 20  tch( eDest ){.  
24a30 20 20 2f 2a 20 49 6e 20 74 68 69 73 20 6d 6f 64    /* In this mod
24a31 65 2c 20 77 72 69 74 65 20 65 61 63 68 20 71 75  e, write each qu
24a32 65 72 79 20 72 65 73 75 6c 74 20 74 6f 20 74 68  ery result to th
24a33 65 20 6b 65 79 20 6f 66 20 74 68 65 20 74 65 6d  e key of the tem
24a34 70 6f 72 61 72 79 0a 20 20 20 20 2a 2a 20 74 61  porary.    ** ta
24a35 62 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a  ble iParm..    *
24a36 2f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  /.#ifndef SQLITE
24a37 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53  _OMIT_COMPOUND_S
24a38 45 4c 45 43 54 0a 20 20 20 20 63 61 73 65 20 53  ELECT.    case S
24a39 52 54 5f 55 6e 69 6f 6e 3a 20 7b 0a 20 20 20 20  RT_Union: {.    
24a3a 20 20 69 6e 74 20 72 31 3b 0a 20 20 20 20 20 20    int r1;.      
24a3b 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
24a3c 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
24a3d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
24a3e 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61  eAddOp3(v, OP_Ma
24a3f 6b 65 52 65 63 6f 72 64 2c 20 72 65 67 52 65 73  keRecord, regRes
24a40 75 6c 74 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31  ult, nColumn, r1
24a41 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
24a42 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24a43 5f 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72  _IdxInsert, iPar
24a44 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
24a45 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24a46 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
24a47 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
24a48 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6e 73    }..    /* Cons
24a49 74 72 75 63 74 20 61 20 72 65 63 6f 72 64 20 66  truct a record f
24a4a 72 6f 6d 20 74 68 65 20 71 75 65 72 79 20 72 65  rom the query re
24a4b 73 75 6c 74 2c 20 62 75 74 20 69 6e 73 74 65 61  sult, but instea
24a4c 64 20 6f 66 0a 20 20 20 20 2a 2a 20 73 61 76 69  d of.    ** savi
24a4d 6e 67 20 74 68 61 74 20 72 65 63 6f 72 64 2c 20  ng that record, 
24a4e 75 73 65 20 69 74 20 61 73 20 61 20 6b 65 79 20  use it as a key 
24a4f 74 6f 20 64 65 6c 65 74 65 20 65 6c 65 6d 65 6e  to delete elemen
24a50 74 73 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20 74  ts from.    ** t
24a51 68 65 20 74 65 6d 70 6f 72 61 72 79 20 74 61 62  he temporary tab
24a52 6c 65 20 69 50 61 72 6d 2e 0a 20 20 20 20 2a 2f  le iParm..    */
24a53 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 78  .    case SRT_Ex
24a54 63 65 70 74 3a 20 7b 0a 20 20 20 20 20 20 73 71  cept: {.      sq
24a55 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
24a56 76 2c 20 4f 50 5f 49 64 78 44 65 6c 65 74 65 2c  v, OP_IdxDelete,
24a57 20 69 50 61 72 6d 2c 20 72 65 67 52 65 73 75 6c   iParm, regResul
24a58 74 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20  t, nColumn);.   
24a59 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
24a5a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20 53  #endif..    /* S
24a5b 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c 74 20  tore the result 
24a5c 61 73 20 64 61 74 61 20 75 73 69 6e 67 20 61 20  as data using a 
24a5d 75 6e 69 71 75 65 20 6b 65 79 2e 0a 20 20 20 20  unique key..    
24a5e 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f  */.    case SRT_
24a5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65 20  Table:.    case 
24a60 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b 0a  SRT_EphemTab: {.
24a61 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73        int r1 = s
24a62 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67  qlite3GetTempReg
24a63 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20  (pParse);.      
24a64 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
24a65 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f  3(v, OP_MakeReco
24a66 72 64 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e  rd, regResult, n
24a67 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a 20 20 20  Column, r1);.   
24a68 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20     if( pOrderBy 
24a69 29 7b 0a 20 20 20 20 20 20 20 20 70 75 73 68 4f  ){.        pushO
24a6a 6e 74 6f 53 6f 72 74 65 72 28 70 50 61 72 73 65  ntoSorter(pParse
24a6b 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 2c 20 72  , pOrderBy, p, r
24a6c 31 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  1);.      }else{
24a6d 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 20  .        int r2 
24a6e 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
24a6f 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
24a70 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24a71 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 77  AddOp2(v, OP_New
24a72 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20 72 32  Rowid, iParm, r2
24a73 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
24a74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c 20  e3VdbeAddOp3(v, 
24a75 4f 50 5f 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  OP_Insert, iParm
24a76 2c 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20  , r1, r2);.     
24a77 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
24a78 61 6e 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47  angeP5(v, OPFLAG
24a79 5f 41 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20  _APPEND);.      
24a7a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73 65    sqlite3Release
24a7b 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c 20  TempReg(pParse, 
24a7c 72 32 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  r2);.      }.   
24a7d 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61 73     sqlite3Releas
24a7e 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 2c  eTempReg(pParse,
24a7f 20 72 31 29 3b 0a 20 20 20 20 20 20 62 72 65 61   r1);.      brea
24a80 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64 65  k;.    }..#ifnde
24a81 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
24a82 42 51 55 45 52 59 0a 20 20 20 20 2f 2a 20 49 66  BQUERY.    /* If
24a83 20 77 65 20 61 72 65 20 63 72 65 61 74 69 6e 67   we are creating
24a84 20 61 20 73 65 74 20 66 6f 72 20 61 6e 20 22 65   a set for an "e
24a85 78 70 72 20 49 4e 20 28 53 45 4c 45 43 54 20 2e  xpr IN (SELECT .
24a86 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63 74 2c 0a  ..)" construct,.
24a87 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 72      ** then ther
24a88 65 20 73 68 6f 75 6c 64 20 62 65 20 61 20 73 69  e should be a si
24a89 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20 74 68 65  ngle item on the
24a8a 20 73 74 61 63 6b 2e 20 20 57 72 69 74 65 20 74   stack.  Write t
24a8b 68 69 73 0a 20 20 20 20 2a 2a 20 69 74 65 6d 20  his.    ** item 
24a8c 69 6e 74 6f 20 74 68 65 20 73 65 74 20 74 61 62  into the set tab
24a8d 6c 65 20 77 69 74 68 20 62 6f 67 75 73 20 64 61  le with bogus da
24a8e 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  ta..    */.    c
24a8f 61 73 65 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20  ase SRT_Set: {. 
24a90 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 43 6f       assert( nCo
24a91 6c 75 6d 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20  lumn==1 );.     
24a92 20 70 2d 3e 61 66 66 69 6e 69 74 79 20 3d 20 73   p->affinity = s
24a93 71 6c 69 74 65 33 43 6f 6d 70 61 72 65 41 66 66  qlite3CompareAff
24a94 69 6e 69 74 79 28 70 45 4c 69 73 74 2d 3e 61 5b  inity(pEList->a[
24a95 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
24a96 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
24a97 20 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29    if( pOrderBy )
24a98 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 74 20  {.        /* At 
24a99 66 69 72 73 74 20 67 6c 61 6e 63 65 20 79 6f 75  first glance you
24a9a 20 77 6f 75 6c 64 20 74 68 69 6e 6b 20 77 65 20   would think we 
24a9b 63 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 6f  could optimize o
24a9c 75 74 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a  ut the.        *
24a9d 2a 20 4f 52 44 45 52 20 42 59 20 69 6e 20 74 68  * ORDER BY in th
24a9e 69 73 20 63 61 73 65 20 73 69 6e 63 65 20 74 68  is case since th
24a9f 65 20 6f 72 64 65 72 20 6f 66 20 65 6e 74 72 69  e order of entri
24aa0 65 73 20 69 6e 20 74 68 65 20 73 65 74 0a 20 20  es in the set.  
24aa1 20 20 20 20 20 20 2a 2a 20 64 6f 65 73 20 6e 6f        ** does no
24aa2 74 20 6d 61 74 74 65 72 2e 20 20 42 75 74 20 74  t matter.  But t
24aa3 68 65 72 65 20 6d 69 67 68 74 20 62 65 20 61 20  here might be a 
24aa4 4c 49 4d 49 54 20 63 6c 61 75 73 65 2c 20 69 6e  LIMIT clause, in
24aa5 20 77 68 69 63 68 0a 20 20 20 20 20 20 20 20 2a   which.        *
24aa6 2a 20 63 61 73 65 20 74 68 65 20 6f 72 64 65 72  * case the order
24aa7 20 64 6f 65 73 20 6d 61 74 74 65 72 20 2a 2f 0a   does matter */.
24aa8 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
24aa9 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
24aaa 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 65 67 52  OrderBy, p, regR
24aab 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20 7d 65  esult);.      }e
24aac 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  lse{.        int
24aad 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74   r1 = sqlite3Get
24aae 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b  TempReg(pParse);
24aaf 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24ab0 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
24ab1 5f 4d 61 6b 65 52 65 63 6f 72 64 2c 20 72 65 67  _MakeRecord, reg
24ab2 52 65 73 75 6c 74 2c 20 31 2c 20 72 31 2c 20 26  Result, 1, r1, &
24ab3 70 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b  p->affinity, 1);
24ab4 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24ab5 45 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74  ExprCacheAffinit
24ab6 79 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20  yChange(pParse, 
24ab7 72 65 67 52 65 73 75 6c 74 2c 20 31 29 3b 0a 20  regResult, 1);. 
24ab8 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
24ab9 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49  beAddOp2(v, OP_I
24aba 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d 2c  dxInsert, iParm,
24abb 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
24abc 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
24abd 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
24abe 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
24abf 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
24ac0 20 2f 2a 20 49 66 20 61 6e 79 20 72 6f 77 20 65   /* If any row e
24ac1 78 69 73 74 20 69 6e 20 74 68 65 20 72 65 73 75  xist in the resu
24ac2 6c 74 20 73 65 74 2c 20 72 65 63 6f 72 64 20 74  lt set, record t
24ac3 68 61 74 20 66 61 63 74 20 61 6e 64 20 61 62 6f  hat fact and abo
24ac4 72 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63  rt..    */.    c
24ac5 61 73 65 20 53 52 54 5f 45 78 69 73 74 73 3a 20  ase SRT_Exists: 
24ac6 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
24ac7 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24ac8 49 6e 74 65 67 65 72 2c 20 31 2c 20 69 50 61 72  Integer, 1, iPar
24ac9 6d 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  m);.      /* The
24aca 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69   LIMIT clause wi
24acb 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65  ll terminate the
24acc 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a   loop for us */.
24acd 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
24ace 20 7d 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74 68   }..    /* If th
24acf 69 73 20 69 73 20 61 20 73 63 61 6c 61 72 20 73  is is a scalar s
24ad0 65 6c 65 63 74 20 74 68 61 74 20 69 73 20 70 61  elect that is pa
24ad1 72 74 20 6f 66 20 61 6e 20 65 78 70 72 65 73 73  rt of an express
24ad2 69 6f 6e 2c 20 74 68 65 6e 0a 20 20 20 20 2a 2a  ion, then.    **
24ad3 20 73 74 6f 72 65 20 74 68 65 20 72 65 73 75 6c   store the resul
24ad4 74 73 20 69 6e 20 74 68 65 20 61 70 70 72 6f 70  ts in the approp
24ad5 72 69 61 74 65 20 6d 65 6d 6f 72 79 20 63 65 6c  riate memory cel
24ad6 6c 20 61 6e 64 20 62 72 65 61 6b 20 6f 75 74 0a  l and break out.
24ad7 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 73 63      ** of the sc
24ad8 61 6e 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2f 0a  an loop..    */.
24ad9 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d      case SRT_Mem
24ada 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
24adb 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
24adc 20 20 20 20 20 20 69 66 28 20 70 4f 72 64 65 72        if( pOrder
24add 42 79 20 29 7b 0a 20 20 20 20 20 20 20 20 70 75  By ){.        pu
24ade 73 68 4f 6e 74 6f 53 6f 72 74 65 72 28 70 50 61  shOntoSorter(pPa
24adf 72 73 65 2c 20 70 4f 72 64 65 72 42 79 2c 20 70  rse, pOrderBy, p
24ae0 2c 20 72 65 67 52 65 73 75 6c 74 29 3b 0a 20 20  , regResult);.  
24ae1 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24ae2 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f     sqlite3ExprCo
24ae3 64 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 72  deMove(pParse, r
24ae4 65 67 52 65 73 75 6c 74 2c 20 69 50 61 72 6d 2c  egResult, iParm,
24ae5 20 31 29 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20   1);.        /* 
24ae6 54 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  The LIMIT clause
24ae7 20 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f   will jump out o
24ae8 66 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75  f the loop for u
24ae9 73 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20  s */.      }.   
24aea 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
24aeb 23 65 6e 64 69 66 20 2f 2a 20 23 69 66 6e 64 65  #endif /* #ifnde
24aec 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
24aed 42 51 55 45 52 59 20 2a 2f 0a 0a 20 20 20 20 2f  BQUERY */..    /
24aee 2a 20 53 65 6e 64 20 74 68 65 20 64 61 74 61 20  * Send the data 
24aef 74 6f 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20  to the callback 
24af0 66 75 6e 63 74 69 6f 6e 20 6f 72 20 74 6f 20 61  function or to a
24af1 20 73 75 62 72 6f 75 74 69 6e 65 2e 20 20 49 6e   subroutine.  In
24af2 20 74 68 65 0a 20 20 20 20 2a 2a 20 63 61 73 65   the.    ** case
24af3 20 6f 66 20 61 20 73 75 62 72 6f 75 74 69 6e 65   of a subroutine
24af4 2c 20 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65  , the subroutine
24af5 20 69 74 73 65 6c 66 20 69 73 20 72 65 73 70 6f   itself is respo
24af6 6e 73 69 62 6c 65 20 66 6f 72 0a 20 20 20 20 2a  nsible for.    *
24af7 2a 20 70 6f 70 70 69 6e 67 20 74 68 65 20 64 61  * popping the da
24af8 74 61 20 66 72 6f 6d 20 74 68 65 20 73 74 61 63  ta from the stac
24af9 6b 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61  k..    */.    ca
24afa 73 65 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  se SRT_Coroutine
24afb 3a 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 4f  :.    case SRT_O
24afc 75 74 70 75 74 3a 20 7b 0a 20 20 20 20 20 20 69  utput: {.      i
24afd 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20  f( pOrderBy ){. 
24afe 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20         int r1 = 
24aff 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
24b00 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20  g(pParse);.     
24b01 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24b02 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
24b03 65 63 6f 72 64 2c 20 72 65 67 52 65 73 75 6c 74  ecord, regResult
24b04 2c 20 6e 43 6f 6c 75 6d 6e 2c 20 72 31 29 3b 0a  , nColumn, r1);.
24b05 20 20 20 20 20 20 20 20 70 75 73 68 4f 6e 74 6f          pushOnto
24b06 53 6f 72 74 65 72 28 70 50 61 72 73 65 2c 20 70  Sorter(pParse, p
24b07 4f 72 64 65 72 42 79 2c 20 70 2c 20 72 31 29 3b  OrderBy, p, r1);
24b08 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24b09 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70  ReleaseTempReg(p
24b0a 50 61 72 73 65 2c 20 72 31 29 3b 0a 20 20 20 20  Parse, r1);.    
24b0b 20 20 7d 65 6c 73 65 20 69 66 28 20 65 44 65 73    }else if( eDes
24b0c 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65  t==SRT_Coroutine
24b0d 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
24b0e 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
24b0f 20 4f 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74   OP_Yield, pDest
24b10 2d 3e 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20  ->iParm);.      
24b11 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 73  }else{.        s
24b12 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
24b13 28 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77  (v, OP_ResultRow
24b14 2c 20 72 65 67 52 65 73 75 6c 74 2c 20 6e 43 6f  , regResult, nCo
24b15 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20 73  lumn);.        s
24b16 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
24b17 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
24b18 61 72 73 65 2c 20 72 65 67 52 65 73 75 6c 74 2c  arse, regResult,
24b19 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20   nColumn);.     
24b1a 20 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a   }.      break;.
24b1b 20 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69      }..#if !defi
24b1c 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  ned(SQLITE_OMIT_
24b1d 54 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a 20  TRIGGER).    /* 
24b1e 44 69 73 63 61 72 64 20 74 68 65 20 72 65 73 75  Discard the resu
24b1f 6c 74 73 2e 20 20 54 68 69 73 20 69 73 20 75 73  lts.  This is us
24b20 65 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73 74  ed for SELECT st
24b21 61 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a  atements inside.
24b22 20 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79 20      ** the body 
24b23 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 54  of a TRIGGER.  T
24b24 68 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73 75  he purpose of su
24b25 63 68 20 73 65 6c 65 63 74 73 20 69 73 20 74 6f  ch selects is to
24b26 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65   call.    ** use
24b27 72 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69  r-defined functi
24b28 6f 6e 73 20 74 68 61 74 20 68 61 76 65 20 73 69  ons that have si
24b29 64 65 20 65 66 66 65 63 74 73 2e 20 20 57 65 20  de effects.  We 
24b2a 64 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20 20  do not care.    
24b2b 2a 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63 74  ** about the act
24b2c 75 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20 74  ual results of t
24b2d 68 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a  he select..    *
24b2e 2f 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  /.    default: {
24b2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 65  .      assert( e
24b30 44 65 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72  Dest==SRT_Discar
24b31 64 20 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  d );.      break
24b32 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20  ;.    }.#endif. 
24b33 20 7d 0a 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f   }..  /* Jump to
24b34 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20   the end of the 
24b35 6c 6f 6f 70 20 69 66 20 74 68 65 20 4c 49 4d 49  loop if the LIMI
24b36 54 20 69 73 20 72 65 61 63 68 65 64 2e 0a 20 20  T is reached..  
24b37 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69 6d  */.  if( p->iLim
24b38 69 74 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  it ){.    assert
24b39 28 20 70 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b  ( pOrderBy==0 );
24b3a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73    /* If there is
24b3b 20 61 6e 20 4f 52 44 45 52 20 42 59 2c 20 74 68   an ORDER BY, th
24b3c 65 20 63 61 6c 6c 20 74 6f 0a 20 20 20 20 20 20  e call to.      
24b3d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
24b3e 20 20 20 20 20 20 2a 2a 20 70 75 73 68 4f 6e 74        ** pushOnt
24b3f 6f 53 6f 72 74 65 72 28 29 20 77 6f 75 6c 64 20  oSorter() would 
24b40 68 61 76 65 20 63 6c 65 61 72 65 64 20 70 2d 3e  have cleared p->
24b41 69 4c 69 6d 69 74 20 2a 2f 0a 20 20 20 20 73 71  iLimit */.    sq
24b42 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24b43 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 70 2d  v, OP_AddImm, p-
24b44 3e 69 4c 69 6d 69 74 2c 20 2d 31 29 3b 0a 20 20  >iLimit, -1);.  
24b45 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24b46 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a 65 72 6f  Op2(v, OP_IfZero
24b47 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20 69 42 72  , p->iLimit, iBr
24b48 65 61 6b 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  eak);.  }.}../*.
24b49 2a 2a 20 47 69 76 65 6e 20 61 6e 20 65 78 70 72  ** Given an expr
24b4a 65 73 73 69 6f 6e 20 6c 69 73 74 2c 20 67 65 6e  ession list, gen
24b4b 65 72 61 74 65 20 61 20 4b 65 79 49 6e 66 6f 20  erate a KeyInfo 
24b4c 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 72  structure that r
24b4d 65 63 6f 72 64 73 0a 2a 2a 20 74 68 65 20 63 6f  ecords.** the co
24b4e 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65  llating sequence
24b4f 20 66 6f 72 20 65 61 63 68 20 65 78 70 72 65 73   for each expres
24b50 73 69 6f 6e 20 69 6e 20 74 68 61 74 20 65 78 70  sion in that exp
24b51 72 65 73 73 69 6f 6e 20 6c 69 73 74 2e 0a 2a 2a  ression list..**
24b52 0a 2a 2a 20 49 66 20 74 68 65 20 45 78 70 72 4c  .** If the ExprL
24b53 69 73 74 20 69 73 20 61 6e 20 4f 52 44 45 52 20  ist is an ORDER 
24b54 42 59 20 6f 72 20 47 52 4f 55 50 20 42 59 20 63  BY or GROUP BY c
24b55 6c 61 75 73 65 20 74 68 65 6e 20 74 68 65 20 72  lause then the r
24b56 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 4b 65 79 49  esulting.** KeyI
24b57 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69 73  nfo structure is
24b58 20 61 70 70 72 6f 70 72 69 61 74 65 20 66 6f 72   appropriate for
24b59 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 20 61 20   initializing a 
24b5a 76 69 72 74 75 61 6c 20 69 6e 64 65 78 20 74 6f  virtual index to
24b5b 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  .** implement th
24b5c 61 74 20 63 6c 61 75 73 65 2e 20 20 49 66 20 74  at clause.  If t
24b5d 68 65 20 45 78 70 72 4c 69 73 74 20 69 73 20 74  he ExprList is t
24b5e 68 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66  he result set of
24b5f 20 61 20 53 45 4c 45 43 54 0a 2a 2a 20 74 68 65   a SELECT.** the
24b60 6e 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  n the KeyInfo st
24b61 72 75 63 74 75 72 65 20 69 73 20 61 70 70 72 6f  ructure is appro
24b62 70 72 69 61 74 65 20 66 6f 72 20 69 6e 69 74 69  priate for initi
24b63 61 6c 69 7a 69 6e 67 20 61 20 76 69 72 74 75 61  alizing a virtua
24b64 6c 0a 2a 2a 20 69 6e 64 65 78 20 74 6f 20 69 6d  l.** index to im
24b65 70 6c 65 6d 65 6e 74 20 61 20 44 49 53 54 49 4e  plement a DISTIN
24b66 43 54 20 74 65 73 74 2e 0a 2a 2a 0a 2a 2a 20 53  CT test..**.** S
24b67 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74 68 65  pace to hold the
24b68 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74 75   KeyInfo structu
24b69 72 65 20 69 73 20 6f 62 74 61 69 6e 20 66 72 6f  re is obtain fro
24b6a 6d 20 6d 61 6c 6c 6f 63 2e 20 20 54 68 65 20 63  m malloc.  The c
24b6b 61 6c 6c 69 6e 67 0a 2a 2a 20 66 75 6e 63 74 69  alling.** functi
24b6c 6f 6e 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  on is responsibl
24b6d 65 20 66 6f 72 20 73 65 65 69 6e 67 20 74 68 61  e for seeing tha
24b6e 74 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65  t this structure
24b6f 20 69 73 20 65 76 65 6e 74 75 61 6c 6c 79 0a 2a   is eventually.*
24b70 2a 20 66 72 65 65 64 2e 20 20 41 64 64 20 74 68  * freed.  Add th
24b71 65 20 4b 65 79 49 6e 66 6f 20 73 74 72 75 63 74  e KeyInfo struct
24b72 75 72 65 20 74 6f 20 74 68 65 20 50 34 20 66 69  ure to the P4 fi
24b73 65 6c 64 20 6f 66 20 61 6e 20 6f 70 63 6f 64 65  eld of an opcode
24b74 20 75 73 69 6e 67 0a 2a 2a 20 50 34 5f 4b 45 59   using.** P4_KEY
24b75 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 20 69 73 20  INFO_HANDOFF is 
24b76 74 68 65 20 75 73 75 61 6c 20 77 61 79 20 6f 66  the usual way of
24b77 20 64 65 61 6c 69 6e 67 20 77 69 74 68 20 74 68   dealing with th
24b78 69 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 4b 65  is..*/.static Ke
24b79 79 49 6e 66 6f 20 2a 6b 65 79 49 6e 66 6f 46 72  yInfo *keyInfoFr
24b7a 6f 6d 45 78 70 72 4c 69 73 74 28 50 61 72 73 65  omExprList(Parse
24b7b 20 2a 70 50 61 72 73 65 2c 20 45 78 70 72 4c 69   *pParse, ExprLi
24b7c 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 73 71  st *pList){.  sq
24b7d 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
24b7e 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 6e 45  se->db;.  int nE
24b7f 78 70 72 3b 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  xpr;.  KeyInfo *
24b80 70 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20  pInfo;.  struct 
24b81 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20 2a 70  ExprList_item *p
24b82 49 74 65 6d 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a  Item;.  int i;..
24b83 20 20 6e 45 78 70 72 20 3d 20 70 4c 69 73 74 2d    nExpr = pList-
24b84 3e 6e 45 78 70 72 3b 0a 20 20 70 49 6e 66 6f 20  >nExpr;.  pInfo 
24b85 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
24b86 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
24b87 28 2a 70 49 6e 66 6f 29 20 2b 20 6e 45 78 70 72  (*pInfo) + nExpr
24b88 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
24b89 2a 29 2b 31 29 20 29 3b 0a 20 20 69 66 28 20 70  *)+1) );.  if( p
24b8a 49 6e 66 6f 20 29 7b 0a 20 20 20 20 70 49 6e 66  Info ){.    pInf
24b8b 6f 2d 3e 61 53 6f 72 74 4f 72 64 65 72 20 3d 20  o->aSortOrder = 
24b8c 28 75 38 2a 29 26 70 49 6e 66 6f 2d 3e 61 43 6f  (u8*)&pInfo->aCo
24b8d 6c 6c 5b 6e 45 78 70 72 5d 3b 0a 20 20 20 20 70  ll[nExpr];.    p
24b8e 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e  Info->nField = n
24b8f 45 78 70 72 3b 0a 20 20 20 20 70 49 6e 66 6f 2d  Expr;.    pInfo-
24b90 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29 3b 0a  >enc = ENC(db);.
24b91 20 20 20 20 66 6f 72 28 69 3d 30 2c 20 70 49 74      for(i=0, pIt
24b92 65 6d 3d 70 4c 69 73 74 2d 3e 61 3b 20 69 3c 6e  em=pList->a; i<n
24b93 45 78 70 72 3b 20 69 2b 2b 2c 20 70 49 74 65 6d  Expr; i++, pItem
24b94 2b 2b 29 7b 0a 20 20 20 20 20 20 43 6f 6c 6c 53  ++){.      CollS
24b95 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 20 20 20  eq *pColl;.     
24b96 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
24b97 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
24b98 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  se, pItem->pExpr
24b99 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 70 43  );.      if( !pC
24b9a 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 70  oll ){.        p
24b9b 43 6f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74  Coll = db->pDflt
24b9c 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
24b9d 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43 6f 6c 6c      pInfo->aColl
24b9e 5b 69 5d 20 3d 20 70 43 6f 6c 6c 3b 0a 20 20 20  [i] = pColl;.   
24b9f 20 20 20 70 49 6e 66 6f 2d 3e 61 53 6f 72 74 4f     pInfo->aSortO
24ba0 72 64 65 72 5b 69 5d 20 3d 20 70 49 74 65 6d 2d  rder[i] = pItem-
24ba1 3e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20  >sortOrder;.    
24ba2 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 70  }.  }.  return p
24ba3 49 6e 66 6f 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  Info;.}.../*.** 
24ba4 49 66 20 74 68 65 20 69 6e 6e 65 72 20 6c 6f 6f  If the inner loo
24ba5 70 20 77 61 73 20 67 65 6e 65 72 61 74 65 64 20  p was generated 
24ba6 75 73 69 6e 67 20 61 20 6e 6f 6e 2d 6e 75 6c 6c  using a non-null
24ba7 20 70 4f 72 64 65 72 42 79 20 61 72 67 75 6d 65   pOrderBy argume
24ba8 6e 74 2c 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20  nt,.** then the 
24ba9 72 65 73 75 6c 74 73 20 77 65 72 65 20 70 6c 61  results were pla
24baa 63 65 64 20 69 6e 20 61 20 73 6f 72 74 65 72 2e  ced in a sorter.
24bab 20 20 41 66 74 65 72 20 74 68 65 20 6c 6f 6f 70    After the loop
24bac 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64 0a 2a   is terminated.*
24bad 2a 20 77 65 20 6e 65 65 64 20 74 6f 20 72 75 6e  * we need to run
24bae 20 74 68 65 20 73 6f 72 74 65 72 20 61 6e 64 20   the sorter and 
24baf 6f 75 74 70 75 74 20 74 68 65 20 72 65 73 75 6c  output the resul
24bb0 74 73 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ts.  The followi
24bb1 6e 67 0a 2a 2a 20 72 6f 75 74 69 6e 65 20 67 65  ng.** routine ge
24bb2 6e 65 72 61 74 65 73 20 74 68 65 20 63 6f 64 65  nerates the code
24bb3 20 6e 65 65 64 65 64 20 74 6f 20 64 6f 20 74 68   needed to do th
24bb4 61 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  at..*/.static vo
24bb5 69 64 20 67 65 6e 65 72 61 74 65 53 6f 72 74 54  id generateSortT
24bb6 61 69 6c 28 0a 20 20 50 61 72 73 65 20 2a 70 50  ail(.  Parse *pP
24bb7 61 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73  arse,    /* Pars
24bb8 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
24bb9 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20   Select *p,     
24bba 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45 43 54     /* The SELECT
24bbb 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
24bbc 56 64 62 65 20 2a 76 2c 20 20 20 20 20 20 20 20  Vdbe *v,        
24bbd 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
24bbe 64 65 20 69 6e 74 6f 20 74 68 69 73 20 56 44 42  de into this VDB
24bbf 45 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  E */.  int nColu
24bc0 6d 6e 2c 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62  mn,      /* Numb
24bc1 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 6f 66  er of columns of
24bc2 20 64 61 74 61 20 2a 2f 0a 20 20 53 65 6c 65 63   data */.  Selec
24bc3 74 44 65 73 74 20 2a 70 44 65 73 74 20 2f 2a 20  tDest *pDest /* 
24bc4 57 72 69 74 65 20 74 68 65 20 73 6f 72 74 65 64  Write the sorted
24bc5 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
24bc6 0a 29 7b 0a 20 20 69 6e 74 20 62 72 6b 20 3d 20  .){.  int brk = 
24bc7 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
24bc8 61 62 65 6c 28 76 29 3b 0a 20 20 69 6e 74 20 63  abel(v);.  int c
24bc9 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ont = sqlite3Vdb
24bca 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20  eMakeLabel(v);. 
24bcb 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 69 6e 74   int addr;.  int
24bcc 20 69 54 61 62 3b 0a 20 20 69 6e 74 20 70 73 65   iTab;.  int pse
24bcd 75 64 6f 54 61 62 20 3d 20 30 3b 0a 20 20 45 78  udoTab = 0;.  Ex
24bce 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79  prList *pOrderBy
24bcf 20 3d 20 70 2d 3e 70 4f 72 64 65 72 42 79 3b 0a   = p->pOrderBy;.
24bd0 0a 20 20 69 6e 74 20 65 44 65 73 74 20 3d 20 70  .  int eDest = p
24bd1 44 65 73 74 2d 3e 65 44 65 73 74 3b 0a 20 20 69  Dest->eDest;.  i
24bd2 6e 74 20 69 50 61 72 6d 20 3d 20 70 44 65 73 74  nt iParm = pDest
24bd3 2d 3e 69 50 61 72 6d 3b 0a 0a 20 20 69 6e 74 20  ->iParm;..  int 
24bd4 72 65 67 52 6f 77 3b 0a 20 20 69 6e 74 20 72 65  regRow;.  int re
24bd5 67 52 6f 77 69 64 3b 0a 0a 20 20 69 54 61 62 20  gRowid;..  iTab 
24bd6 3d 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75  = pOrderBy->iECu
24bd7 72 73 6f 72 3b 0a 20 20 69 66 28 20 65 44 65 73  rsor;.  if( eDes
24bd8 74 3d 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c  t==SRT_Output ||
24bd9 20 65 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f   eDest==SRT_Coro
24bda 75 74 69 6e 65 20 29 7b 0a 20 20 20 20 70 73 65  utine ){.    pse
24bdb 75 64 6f 54 61 62 20 3d 20 70 50 61 72 73 65 2d  udoTab = pParse-
24bdc 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 73 71 6c  >nTab++;.    sql
24bdd 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24bde 2c 20 4f 50 5f 53 65 74 4e 75 6d 43 6f 6c 75 6d  , OP_SetNumColum
24bdf 6e 73 2c 20 30 2c 20 6e 43 6f 6c 75 6d 6e 29 3b  ns, 0, nColumn);
24be0 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
24be1 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65  AddOp2(v, OP_Ope
24be2 6e 50 73 65 75 64 6f 2c 20 70 73 65 75 64 6f 54  nPseudo, pseudoT
24be3 61 62 2c 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f  ab, eDest==SRT_O
24be4 75 74 70 75 74 29 3b 0a 20 20 7d 0a 20 20 61 64  utput);.  }.  ad
24be5 64 72 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33  dr = 1 + sqlite3
24be6 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
24be7 5f 53 6f 72 74 2c 20 69 54 61 62 2c 20 62 72 6b  _Sort, iTab, brk
24be8 29 3b 0a 20 20 63 6f 64 65 4f 66 66 73 65 74 28  );.  codeOffset(
24be9 76 2c 20 70 2c 20 63 6f 6e 74 29 3b 0a 20 20 72  v, p, cont);.  r
24bea 65 67 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 47  egRow = sqlite3G
24beb 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
24bec 29 3b 0a 20 20 72 65 67 52 6f 77 69 64 20 3d 20  );.  regRowid = 
24bed 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65  sqlite3GetTempRe
24bee 67 28 70 50 61 72 73 65 29 3b 0a 20 20 73 71 6c  g(pParse);.  sql
24bef 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
24bf0 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 54 61  , OP_Column, iTa
24bf1 62 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78  b, pOrderBy->nEx
24bf2 70 72 20 2b 20 31 2c 20 72 65 67 52 6f 77 29 3b  pr + 1, regRow);
24bf3 0a 20 20 73 77 69 74 63 68 28 20 65 44 65 73 74  .  switch( eDest
24bf4 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 52 54   ){.    case SRT
24bf5 5f 54 61 62 6c 65 3a 0a 20 20 20 20 63 61 73 65  _Table:.    case
24bf6 20 53 52 54 5f 45 70 68 65 6d 54 61 62 3a 20 7b   SRT_EphemTab: {
24bf7 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
24bf8 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
24bf9 65 77 52 6f 77 69 64 2c 20 69 50 61 72 6d 2c 20  ewRowid, iParm, 
24bfa 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
24bfb 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24bfc 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74 2c  p3(v, OP_Insert,
24bfd 20 69 50 61 72 6d 2c 20 72 65 67 52 6f 77 2c 20   iParm, regRow, 
24bfe 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 20  regRowid);.     
24bff 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e   sqlite3VdbeChan
24c00 67 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41  geP5(v, OPFLAG_A
24c01 50 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 62 72  PPEND);.      br
24c02 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64  eak;.    }.#ifnd
24c03 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ef SQLITE_OMIT_S
24c04 55 42 51 55 45 52 59 0a 20 20 20 20 63 61 73 65  UBQUERY.    case
24c05 20 53 52 54 5f 53 65 74 3a 20 7b 0a 20 20 20 20   SRT_Set: {.    
24c06 20 20 61 73 73 65 72 74 28 20 6e 43 6f 6c 75 6d    assert( nColum
24c07 6e 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71  n==1 );.      sq
24c08 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34 28  lite3VdbeAddOp4(
24c09 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63 6f 72 64  v, OP_MakeRecord
24c0a 2c 20 72 65 67 52 6f 77 2c 20 31 2c 20 72 65 67  , regRow, 1, reg
24c0b 52 6f 77 69 64 2c 20 26 70 2d 3e 61 66 66 69 6e  Rowid, &p->affin
24c0c 69 74 79 2c 20 31 29 3b 0a 20 20 20 20 20 20 73  ity, 1);.      s
24c0d 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65 41  qlite3ExprCacheA
24c0e 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70 50  ffinityChange(pP
24c0f 61 72 73 65 2c 20 72 65 67 52 6f 77 2c 20 31 29  arse, regRow, 1)
24c10 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
24c11 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
24c12 49 64 78 49 6e 73 65 72 74 2c 20 69 50 61 72 6d  IdxInsert, iParm
24c13 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20  , regRowid);.   
24c14 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
24c15 20 20 20 20 63 61 73 65 20 53 52 54 5f 4d 65 6d      case SRT_Mem
24c16 3a 20 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  : {.      assert
24c17 28 20 6e 43 6f 6c 75 6d 6e 3d 3d 31 20 29 3b 0a  ( nColumn==1 );.
24c18 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
24c19 72 43 6f 64 65 4d 6f 76 65 28 70 50 61 72 73 65  rCodeMove(pParse
24c1a 2c 20 72 65 67 52 6f 77 2c 20 69 50 61 72 6d 2c  , regRow, iParm,
24c1b 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68   1);.      /* Th
24c1c 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 77  e LIMIT clause w
24c1d 69 6c 6c 20 74 65 72 6d 69 6e 61 74 65 20 74 68  ill terminate th
24c1e 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f  e loop for us */
24c1f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
24c20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 63    }.#endif.    c
24c21 61 73 65 20 53 52 54 5f 4f 75 74 70 75 74 3a 0a  ase SRT_Output:.
24c22 20 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72      case SRT_Cor
24c23 6f 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20  outine: {.      
24c24 69 6e 74 20 69 3b 0a 20 20 20 20 20 20 73 71 6c  int i;.      sql
24c25 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
24c26 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c  , OP_Integer, 1,
24c27 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
24c28 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
24c29 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72 74  Op3(v, OP_Insert
24c2a 2c 20 70 73 65 75 64 6f 54 61 62 2c 20 72 65 67  , pseudoTab, reg
24c2b 52 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Row, regRowid);.
24c2c 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69        for(i=0; i
24c2d 3c 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a  <nColumn; i++){.
24c2e 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
24c2f 72 65 67 52 6f 77 21 3d 70 44 65 73 74 2d 3e 69  regRow!=pDest->i
24c30 4d 65 6d 2b 69 20 29 3b 0a 20 20 20 20 20 20 20  Mem+i );.       
24c31 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
24c32 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
24c33 20 70 73 65 75 64 6f 54 61 62 2c 20 69 2c 20 70   pseudoTab, i, p
24c34 44 65 73 74 2d 3e 69 4d 65 6d 2b 69 29 3b 0a 20  Dest->iMem+i);. 
24c35 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
24c36 20 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70   eDest==SRT_Outp
24c37 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ut ){.        sq
24c38 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
24c39 76 2c 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 2c  v, OP_ResultRow,
24c3a 20 70 44 65 73 74 2d 3e 69 4d 65 6d 2c 20 6e 43   pDest->iMem, nC
24c3b 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20 20  olumn);.        
24c3c 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68 65  sqlite3ExprCache
24c3d 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28 70  AffinityChange(p
24c3e 50 61 72 73 65 2c 20 70 44 65 73 74 2d 3e 69 4d  Parse, pDest->iM
24c3f 65 6d 2c 20 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20  em, nColumn);.  
24c40 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24c41 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
24c42 64 4f 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64  dOp1(v, OP_Yield
24c43 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29 3b  , pDest->iParm);
24c44 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 62  .      }.      b
24c45 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20  reak;.    }.    
24c46 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20  default: {.     
24c47 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e 67 20 2a   /* Do nothing *
24c48 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  /.      break;. 
24c49 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
24c4a 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67  e3ReleaseTempReg
24c4b 28 70 50 61 72 73 65 2c 20 72 65 67 52 6f 77 29  (pParse, regRow)
24c4c 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61  ;.  sqlite3Relea
24c4d 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
24c4e 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a 20 20  , regRowid);..  
24c4f 2f 2a 20 4c 49 4d 49 54 20 68 61 73 20 62 65 65  /* LIMIT has bee
24c50 6e 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 62 79  n implemented by
24c51 20 74 68 65 20 70 75 73 68 4f 6e 74 6f 53 6f 72   the pushOntoSor
24c52 74 65 72 28 29 20 72 6f 75 74 69 6e 65 2e 0a 20  ter() routine.. 
24c53 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d   */.  assert( p-
24c54 3e 69 4c 69 6d 69 74 3d 3d 30 20 29 3b 0a 0a 20  >iLimit==0 );.. 
24c55 20 2f 2a 20 54 68 65 20 62 6f 74 74 6f 6d 20 6f   /* The bottom o
24c56 66 20 74 68 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a  f the loop.  */.
24c57 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
24c58 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 63 6f 6e  olveLabel(v, con
24c59 74 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  t);.  sqlite3Vdb
24c5a 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 65  eAddOp2(v, OP_Ne
24c5b 78 74 2c 20 69 54 61 62 2c 20 61 64 64 72 29 3b  xt, iTab, addr);
24c5c 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65  .  sqlite3VdbeRe
24c5d 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 62 72  solveLabel(v, br
24c5e 6b 29 3b 0a 20 20 69 66 28 20 65 44 65 73 74 3d  k);.  if( eDest=
24c5f 3d 53 52 54 5f 4f 75 74 70 75 74 20 7c 7c 20 65  =SRT_Output || e
24c60 44 65 73 74 3d 3d 53 52 54 5f 43 6f 72 6f 75 74  Dest==SRT_Corout
24c61 69 6e 65 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ine ){.    sqlit
24c62 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
24c63 4f 50 5f 43 6c 6f 73 65 2c 20 70 73 65 75 64 6f  OP_Close, pseudo
24c64 54 61 62 2c 20 30 29 3b 0a 20 20 7d 0a 0a 7d 0a  Tab, 0);.  }..}.
24c65 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
24c66 70 6f 69 6e 74 65 72 20 74 6f 20 61 20 73 74 72  pointer to a str
24c67 69 6e 67 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74  ing containing t
24c68 68 65 20 27 64 65 63 6c 61 72 61 74 69 6f 6e 20  he 'declaration 
24c69 74 79 70 65 27 20 6f 66 20 74 68 65 0a 2a 2a 20  type' of the.** 
24c6a 65 78 70 72 65 73 73 69 6f 6e 20 70 45 78 70 72  expression pExpr
24c6b 2e 20 54 68 65 20 73 74 72 69 6e 67 20 6d 61 79  . The string may
24c6c 20 62 65 20 74 72 65 61 74 65 64 20 61 73 20 73   be treated as s
24c6d 74 61 74 69 63 20 62 79 20 74 68 65 20 63 61 6c  tatic by the cal
24c6e 6c 65 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 64  ler..**.** The d
24c6f 65 63 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20  eclaration type 
24c70 69 73 20 74 68 65 20 65 78 61 63 74 20 64 61 74  is the exact dat
24c71 61 74 79 70 65 20 64 65 66 69 6e 69 74 69 6f 6e  atype definition
24c72 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20   extracted from 
24c73 74 68 65 0a 2a 2a 20 6f 72 69 67 69 6e 61 6c 20  the.** original 
24c74 43 52 45 41 54 45 20 54 41 42 4c 45 20 73 74 61  CREATE TABLE sta
24c75 74 65 6d 65 6e 74 20 69 66 20 74 68 65 20 65 78  tement if the ex
24c76 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f  pression is a co
24c77 6c 75 6d 6e 2e 20 54 68 65 0a 2a 2a 20 64 65 63  lumn. The.** dec
24c78 6c 61 72 61 74 69 6f 6e 20 74 79 70 65 20 66 6f  laration type fo
24c79 72 20 61 20 52 4f 57 49 44 20 66 69 65 6c 64 20  r a ROWID field 
24c7a 69 73 20 49 4e 54 45 47 45 52 2e 20 45 78 61 63  is INTEGER. Exac
24c7b 74 6c 79 20 77 68 65 6e 20 61 6e 20 65 78 70 72  tly when an expr
24c7c 65 73 73 69 6f 6e 0a 2a 2a 20 69 73 20 63 6f 6e  ession.** is con
24c7d 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e  sidered a column
24c7e 20 63 61 6e 20 62 65 20 63 6f 6d 70 6c 65 78 20   can be complex 
24c7f 69 6e 20 74 68 65 20 70 72 65 73 65 6e 63 65 20  in the presence 
24c80 6f 66 20 73 75 62 71 75 65 72 69 65 73 2e 20 54  of subqueries. T
24c81 68 65 0a 2a 2a 20 72 65 73 75 6c 74 2d 73 65 74  he.** result-set
24c82 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61   expression in a
24c83 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ll of the follow
24c84 69 6e 67 20 53 45 4c 45 43 54 20 73 74 61 74 65  ing SELECT state
24c85 6d 65 6e 74 73 20 69 73 20 0a 2a 2a 20 63 6f 6e  ments is .** con
24c86 73 69 64 65 72 65 64 20 61 20 63 6f 6c 75 6d 6e  sidered a column
24c87 20 62 79 20 74 68 69 73 20 66 75 6e 63 74 69 6f   by this functio
24c88 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43  n..**.**   SELEC
24c89 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c 3b 0a  T col FROM tbl;.
24c8a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53 45 4c  **   SELECT (SEL
24c8b 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74 62 6c  ECT col FROM tbl
24c8c 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 28 53  ;.**   SELECT (S
24c8d 45 4c 45 43 54 20 63 6f 6c 20 46 52 4f 4d 20 74  ELECT col FROM t
24c8e 62 6c 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  bl);.**   SELECT
24c8f 20 61 62 63 20 46 52 4f 4d 20 28 53 45 4c 45 43   abc FROM (SELEC
24c90 54 20 63 6f 6c 20 41 53 20 61 62 63 20 46 52 4f  T col AS abc FRO
24c91 4d 20 74 62 6c 29 3b 0a 2a 2a 20 0a 2a 2a 20 54  M tbl);.** .** T
24c92 68 65 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 74  he declaration t
24c93 79 70 65 20 66 6f 72 20 61 6e 79 20 65 78 70 72  ype for any expr
24c94 65 73 73 69 6f 6e 20 6f 74 68 65 72 20 74 68 61  ession other tha
24c95 6e 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 4e 55  n a column is NU
24c96 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  LL..*/.static co
24c97 6e 73 74 20 63 68 61 72 20 2a 63 6f 6c 75 6d 6e  nst char *column
24c98 54 79 70 65 28 0a 20 20 4e 61 6d 65 43 6f 6e 74  Type(.  NameCont
24c99 65 78 74 20 2a 70 4e 43 2c 20 0a 20 20 45 78 70  ext *pNC, .  Exp
24c9a 72 20 2a 70 45 78 70 72 2c 0a 20 20 63 6f 6e 73  r *pExpr,.  cons
24c9b 74 20 63 68 61 72 20 2a 2a 70 7a 4f 72 69 67 69  t char **pzOrigi
24c9c 6e 44 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  nDb,.  const cha
24c9d 72 20 2a 2a 70 7a 4f 72 69 67 69 6e 54 61 62 2c  r **pzOriginTab,
24c9e 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
24c9f 70 7a 4f 72 69 67 69 6e 43 6f 6c 0a 29 7b 0a 20  pzOriginCol.){. 
24ca0 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 54 79   char const *zTy
24ca1 70 65 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 63  pe = 0;.  char c
24ca2 6f 6e 73 74 20 2a 7a 4f 72 69 67 69 6e 44 62 20  onst *zOriginDb 
24ca3 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73  = 0;.  char cons
24ca4 74 20 2a 7a 4f 72 69 67 69 6e 54 61 62 20 3d 20  t *zOriginTab = 
24ca5 30 3b 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  0;.  char const 
24ca6 2a 7a 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 30 3b  *zOriginCol = 0;
24ca7 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 69 66 28 20  .  int j;.  if( 
24ca8 70 45 78 70 72 3d 3d 30 20 7c 7c 20 70 4e 43 2d  pExpr==0 || pNC-
24ca9 3e 70 53 72 63 4c 69 73 74 3d 3d 30 20 29 20 72  >pSrcList==0 ) r
24caa 65 74 75 72 6e 20 30 3b 0a 0a 20 20 73 77 69 74  eturn 0;..  swit
24cab 63 68 28 20 70 45 78 70 72 2d 3e 6f 70 20 29 7b  ch( pExpr->op ){
24cac 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 41 47 47  .    case TK_AGG
24cad 5f 43 4f 4c 55 4d 4e 3a 0a 20 20 20 20 63 61 73  _COLUMN:.    cas
24cae 65 20 54 4b 5f 43 4f 4c 55 4d 4e 3a 20 7b 0a 20  e TK_COLUMN: {. 
24caf 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70 72       /* The expr
24cb0 65 73 73 69 6f 6e 20 69 73 20 61 20 63 6f 6c 75  ession is a colu
24cb1 6d 6e 2e 20 4c 6f 63 61 74 65 20 74 68 65 20 74  mn. Locate the t
24cb2 61 62 6c 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20  able the column 
24cb3 69 73 20 62 65 69 6e 67 0a 20 20 20 20 20 20 2a  is being.      *
24cb4 2a 20 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d  * extracted from
24cb5 20 69 6e 20 4e 61 6d 65 43 6f 6e 74 65 78 74 2e   in NameContext.
24cb6 70 53 72 63 4c 69 73 74 2e 20 54 68 69 73 20 74  pSrcList. This t
24cb7 61 62 6c 65 20 6d 61 79 20 62 65 20 72 65 61 6c  able may be real
24cb8 0a 20 20 20 20 20 20 2a 2a 20 64 61 74 61 62 61  .      ** databa
24cb9 73 65 20 74 61 62 6c 65 20 6f 72 20 61 20 73 75  se table or a su
24cba 62 71 75 65 72 79 2e 0a 20 20 20 20 20 20 2a 2f  bquery..      */
24cbb 0a 20 20 20 20 20 20 54 61 62 6c 65 20 2a 70 54  .      Table *pT
24cbc 61 62 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  ab = 0;         
24cbd 20 20 20 2f 2a 20 54 61 62 6c 65 20 73 74 72 75     /* Table stru
24cbe 63 74 75 72 65 20 63 6f 6c 75 6d 6e 20 69 73 20  cture column is 
24cbf 65 78 74 72 61 63 74 65 64 20 66 72 6f 6d 20 2a  extracted from *
24cc0 2f 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  /.      Select *
24cc1 70 53 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20  pS = 0;         
24cc2 20 20 20 20 2f 2a 20 53 65 6c 65 63 74 20 74 68      /* Select th
24cc3 65 20 63 6f 6c 75 6d 6e 20 69 73 20 65 78 74 72  e column is extr
24cc4 61 63 74 65 64 20 66 72 6f 6d 20 2a 2f 0a 20 20  acted from */.  
24cc5 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20 70      int iCol = p
24cc6 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e 3b 20 20  Expr->iColumn;  
24cc7 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 6f 6c 75  /* Index of colu
24cc8 6d 6e 20 69 6e 20 70 54 61 62 20 2a 2f 0a 20 20  mn in pTab */.  
24cc9 20 20 20 20 77 68 69 6c 65 28 20 70 4e 43 20 26      while( pNC &
24cca 26 20 21 70 54 61 62 20 29 7b 0a 20 20 20 20 20  & !pTab ){.     
24ccb 20 20 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62     SrcList *pTab
24ccc 4c 69 73 74 20 3d 20 70 4e 43 2d 3e 70 53 72 63  List = pNC->pSrc
24ccd 4c 69 73 74 3b 0a 20 20 20 20 20 20 20 20 66 6f  List;.        fo
24cce 72 28 6a 3d 30 3b 6a 3c 70 54 61 62 4c 69 73 74  r(j=0;j<pTabList
24ccf 2d 3e 6e 53 72 63 20 26 26 20 70 54 61 62 4c 69  ->nSrc && pTabLi
24cd0 73 74 2d 3e 61 5b 6a 5d 2e 69 43 75 72 73 6f 72  st->a[j].iCursor
24cd1 21 3d 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3b  !=pExpr->iTable;
24cd2 6a 2b 2b 29 3b 0a 20 20 20 20 20 20 20 20 69 66  j++);.        if
24cd3 28 20 6a 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ( j<pTabList->nS
24cd4 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  rc ){.          
24cd5 70 54 61 62 20 3d 20 70 54 61 62 4c 69 73 74 2d  pTab = pTabList-
24cd6 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a 20 20 20 20  >a[j].pTab;.    
24cd7 20 20 20 20 20 20 70 53 20 3d 20 70 54 61 62 4c        pS = pTabL
24cd8 69 73 74 2d 3e 61 5b 6a 5d 2e 70 53 65 6c 65 63  ist->a[j].pSelec
24cd9 74 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65  t;.        }else
24cda 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4e 43 20  {.          pNC 
24cdb 3d 20 70 4e 43 2d 3e 70 4e 65 78 74 3b 0a 20 20  = pNC->pNext;.  
24cdc 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
24cdd 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62 3d  .      if( pTab=
24cde 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a  =0 ){.        /*
24cdf 20 46 49 58 20 4d 45 3a 0a 20 20 20 20 20 20 20   FIX ME:.       
24ce0 20 2a 2a 20 54 68 69 73 20 63 61 6e 20 6f 63 63   ** This can occ
24ce1 75 72 73 20 69 66 20 79 6f 75 20 68 61 76 65 20  urs if you have 
24ce2 73 6f 6d 65 74 68 69 6e 67 20 6c 69 6b 65 20 22  something like "
24ce3 53 45 4c 45 43 54 20 6e 65 77 2e 78 3b 22 20 69  SELECT new.x;" i
24ce4 6e 73 69 64 65 0a 20 20 20 20 20 20 20 20 2a 2a  nside.        **
24ce5 20 61 20 74 72 69 67 67 65 72 2e 20 20 49 6e 20   a trigger.  In 
24ce6 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 69 66 20  other words, if 
24ce7 79 6f 75 20 72 65 66 65 72 65 6e 63 65 20 74 68  you reference th
24ce8 65 20 73 70 65 63 69 61 6c 20 22 6e 65 77 22 0a  e special "new".
24ce9 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62 6c 65          ** table
24cea 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
24ceb 65 74 20 6f 66 20 61 20 73 65 6c 65 63 74 2e 20  et of a select. 
24cec 20 57 65 20 64 6f 20 6e 6f 74 20 68 61 76 65 20   We do not have 
24ced 61 20 67 6f 6f 64 20 77 61 79 0a 20 20 20 20 20  a good way.     
24cee 20 20 20 2a 2a 20 74 6f 20 66 69 6e 64 20 74 68     ** to find th
24cef 65 20 61 63 74 75 61 6c 20 74 61 62 6c 65 20 74  e actual table t
24cf0 79 70 65 2c 20 73 6f 20 63 61 6c 6c 20 69 74 20  ype, so call it 
24cf1 22 54 45 58 54 22 2e 20 20 54 68 69 73 20 69 73  "TEXT".  This is
24cf2 20 72 65 61 6c 6c 79 0a 20 20 20 20 20 20 20 20   really.        
24cf3 2a 2a 20 73 6f 6d 65 74 68 69 6e 67 20 6f 66 20  ** something of 
24cf4 61 20 62 75 67 2c 20 62 75 74 20 49 20 64 6f 20  a bug, but I do 
24cf5 6e 6f 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f 20  not know how to 
24cf6 66 69 78 20 69 74 2e 0a 20 20 20 20 20 20 20 20  fix it..        
24cf7 2a 2a 0a 20 20 20 20 20 20 20 20 2a 2a 20 54 68  **.        ** Th
24cf8 69 73 20 63 6f 64 65 20 64 6f 65 73 20 6e 6f 74  is code does not
24cf9 20 70 72 6f 64 75 63 65 20 74 68 65 20 63 6f 72   produce the cor
24cfa 72 65 63 74 20 61 6e 73 77 65 72 20 2d 20 69 74  rect answer - it
24cfb 20 6a 75 73 74 20 70 72 65 76 65 6e 74 73 0a 20   just prevents. 
24cfc 20 20 20 20 20 20 20 2a 2a 20 61 20 73 65 67 66         ** a segf
24cfd 61 75 6c 74 2e 20 20 53 65 65 20 74 69 63 6b 65  ault.  See ticke
24cfe 74 20 23 31 32 32 39 2e 0a 20 20 20 20 20 20 20  t #1229..       
24cff 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 54 79 70   */.        zTyp
24d00 65 20 3d 20 22 54 45 58 54 22 3b 0a 20 20 20 20  e = "TEXT";.    
24d01 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
24d02 20 7d 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74   }..      assert
24d03 28 20 70 54 61 62 20 29 3b 0a 20 20 20 20 20 20  ( pTab );.      
24d04 69 66 28 20 70 53 20 29 7b 0a 20 20 20 20 20 20  if( pS ){.      
24d05 20 20 2f 2a 20 54 68 65 20 22 74 61 62 6c 65 22    /* The "table"
24d06 20 69 73 20 61 63 74 75 61 6c 6c 79 20 61 20 73   is actually a s
24d07 75 62 2d 73 65 6c 65 63 74 20 6f 72 20 61 20 76  ub-select or a v
24d08 69 65 77 20 69 6e 20 74 68 65 20 46 52 4f 4d 20  iew in the FROM 
24d09 63 6c 61 75 73 65 0a 20 20 20 20 20 20 20 20 2a  clause.        *
24d0a 2a 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  * of the SELECT 
24d0b 73 74 61 74 65 6d 65 6e 74 2e 20 52 65 74 75 72  statement. Retur
24d0c 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74 69 6f  n the declaratio
24d0d 6e 20 74 79 70 65 20 61 6e 64 20 6f 72 69 67 69  n type and origi
24d0e 6e 0a 20 20 20 20 20 20 20 20 2a 2a 20 64 61 74  n.        ** dat
24d0f 61 20 66 6f 72 20 74 68 65 20 72 65 73 75 6c 74  a for the result
24d10 2d 73 65 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74  -set column of t
24d11 68 65 20 73 75 62 2d 73 65 6c 65 63 74 2e 0a 20  he sub-select.. 
24d12 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
24d13 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30 20 26 26    if( iCol>=0 &&
24d14 20 69 43 6f 6c 3c 70 53 2d 3e 70 45 4c 69 73 74   iCol<pS->pEList
24d15 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20 20 20  ->nExpr ){.     
24d16 20 20 20 20 20 2f 2a 20 49 66 20 69 43 6f 6c 20       /* If iCol 
24d17 69 73 20 6c 65 73 73 20 74 68 61 6e 20 7a 65 72  is less than zer
24d18 6f 2c 20 74 68 65 6e 20 74 68 65 20 65 78 70 72  o, then the expr
24d19 65 73 73 69 6f 6e 20 72 65 71 75 65 73 74 73 20  ession requests 
24d1a 74 68 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a  the.          **
24d1b 20 72 6f 77 69 64 20 6f 66 20 74 68 65 20 73 75   rowid of the su
24d1c 62 2d 73 65 6c 65 63 74 20 6f 72 20 76 69 65 77  b-select or view
24d1d 2e 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f  . This expressio
24d1e 6e 20 69 73 20 6c 65 67 61 6c 20 28 73 65 65 20  n is legal (see 
24d1f 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 65  .          ** te
24d20 73 74 20 63 61 73 65 20 6d 69 73 63 32 2e 32 2e  st case misc2.2.
24d21 32 29 20 2d 20 69 74 20 61 6c 77 61 79 73 20 65  2) - it always e
24d22 76 61 6c 75 61 74 65 73 20 74 6f 20 4e 55 4c 4c  valuates to NULL
24d23 2e 0a 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 20  ..          */. 
24d24 20 20 20 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e           NameCon
24d25 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20 20 20  text sNC;.      
24d26 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 53      Expr *p = pS
24d27 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c  ->pEList->a[iCol
24d28 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 20  ].pExpr;.       
24d29 20 20 20 73 4e 43 2e 70 53 72 63 4c 69 73 74 20     sNC.pSrcList 
24d2a 3d 20 70 53 2d 3e 70 53 72 63 3b 0a 20 20 20 20  = pS->pSrc;.    
24d2b 20 20 20 20 20 20 73 4e 43 2e 70 4e 65 78 74 20        sNC.pNext 
24d2c 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 73  = 0;.          s
24d2d 4e 43 2e 70 50 61 72 73 65 20 3d 20 70 4e 43 2d  NC.pParse = pNC-
24d2e 3e 70 50 61 72 73 65 3b 0a 20 20 20 20 20 20 20  >pParse;.       
24d2f 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d     zType = colum
24d30 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 26  nType(&sNC, p, &
24d31 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69  zOriginDb, &zOri
24d32 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e  ginTab, &zOrigin
24d33 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 20 20 7d  Col); .        }
24d34 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
24d35 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29   pTab->pSchema )
24d36 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 41 20 72  {.        /* A r
24d37 65 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  eal table */.   
24d38 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70 53       assert( !pS
24d39 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20   );.        if( 
24d3a 69 43 6f 6c 3c 30 20 29 20 69 43 6f 6c 20 3d 20  iCol<0 ) iCol = 
24d3b 70 54 61 62 2d 3e 69 50 4b 65 79 3b 0a 20 20 20  pTab->iPKey;.   
24d3c 20 20 20 20 20 61 73 73 65 72 74 28 20 69 43 6f       assert( iCo
24d3d 6c 3d 3d 2d 31 20 7c 7c 20 28 69 43 6f 6c 3e 3d  l==-1 || (iCol>=
24d3e 30 20 26 26 20 69 43 6f 6c 3c 70 54 61 62 2d 3e  0 && iCol<pTab->
24d3f 6e 43 6f 6c 29 20 29 3b 0a 20 20 20 20 20 20 20  nCol) );.       
24d40 20 69 66 28 20 69 43 6f 6c 3c 30 20 29 7b 0a 20   if( iCol<0 ){. 
24d41 20 20 20 20 20 20 20 20 20 7a 54 79 70 65 20 3d           zType =
24d42 20 22 49 4e 54 45 47 45 52 22 3b 0a 20 20 20 20   "INTEGER";.    
24d43 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 43 6f 6c        zOriginCol
24d44 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20 20   = "rowid";.    
24d45 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
24d46 20 20 20 20 20 7a 54 79 70 65 20 3d 20 70 54 61       zType = pTa
24d47 62 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 54  b->aCol[iCol].zT
24d48 79 70 65 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ype;.          z
24d49 4f 72 69 67 69 6e 43 6f 6c 20 3d 20 70 54 61 62  OriginCol = pTab
24d4a 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
24d4b 6d 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  me;.        }.  
24d4c 20 20 20 20 20 20 7a 4f 72 69 67 69 6e 54 61 62        zOriginTab
24d4d 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b 0a   = pTab->zName;.
24d4e 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 43 2d          if( pNC-
24d4f 3e 70 50 61 72 73 65 20 29 7b 0a 20 20 20 20 20  >pParse ){.     
24d50 20 20 20 20 20 69 6e 74 20 69 44 62 20 3d 20 73       int iDb = s
24d51 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49 6e  qlite3SchemaToIn
24d52 64 65 78 28 70 4e 43 2d 3e 70 50 61 72 73 65 2d  dex(pNC->pParse-
24d53 3e 64 62 2c 20 70 54 61 62 2d 3e 70 53 63 68 65  >db, pTab->pSche
24d54 6d 61 29 3b 0a 20 20 20 20 20 20 20 20 20 20 7a  ma);.          z
24d55 4f 72 69 67 69 6e 44 62 20 3d 20 70 4e 43 2d 3e  OriginDb = pNC->
24d56 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62 5b  pParse->db->aDb[
24d57 69 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20  iDb].zName;.    
24d58 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
24d59 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
24d5a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
24d5b 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a 20 20  OMIT_SUBQUERY.  
24d5c 20 20 63 61 73 65 20 54 4b 5f 53 45 4c 45 43 54    case TK_SELECT
24d5d 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65  : {.      /* The
24d5e 20 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61   expression is a
24d5f 20 73 75 62 2d 73 65 6c 65 63 74 2e 20 52 65 74   sub-select. Ret
24d60 75 72 6e 20 74 68 65 20 64 65 63 6c 61 72 61 74  urn the declarat
24d61 69 6f 6e 20 74 79 70 65 20 61 6e 64 0a 20 20 20  ion type and.   
24d62 20 20 20 2a 2a 20 6f 72 69 67 69 6e 20 69 6e 66     ** origin inf
24d63 6f 20 66 6f 72 20 74 68 65 20 73 69 6e 67 6c 65  o for the single
24d64 20 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72   column in the r
24d65 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74 68 65  esult set of the
24d66 20 53 45 4c 45 43 54 0a 20 20 20 20 20 20 2a 2a   SELECT.      **
24d67 20 73 74 61 74 65 6d 65 6e 74 2e 0a 20 20 20 20   statement..    
24d68 20 20 2a 2f 0a 20 20 20 20 20 20 4e 61 6d 65 43    */.      NameC
24d69 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 20 20  ontext sNC;.    
24d6a 20 20 53 65 6c 65 63 74 20 2a 70 53 20 3d 20 70    Select *pS = p
24d6b 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 3b 0a 20  Expr->pSelect;. 
24d6c 20 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70       Expr *p = p
24d6d 53 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e  S->pEList->a[0].
24d6e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 73 4e 43  pExpr;.      sNC
24d6f 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 53 2d 3e  .pSrcList = pS->
24d70 70 53 72 63 3b 0a 20 20 20 20 20 20 73 4e 43 2e  pSrc;.      sNC.
24d71 70 4e 65 78 74 20 3d 20 70 4e 43 3b 0a 20 20 20  pNext = pNC;.   
24d72 20 20 20 73 4e 43 2e 70 50 61 72 73 65 20 3d 20     sNC.pParse = 
24d73 70 4e 43 2d 3e 70 50 61 72 73 65 3b 0a 20 20 20  pNC->pParse;.   
24d74 20 20 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d     zType = colum
24d75 6e 54 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 26  nType(&sNC, p, &
24d76 7a 4f 72 69 67 69 6e 44 62 2c 20 26 7a 4f 72 69  zOriginDb, &zOri
24d77 67 69 6e 54 61 62 2c 20 26 7a 4f 72 69 67 69 6e  ginTab, &zOrigin
24d78 43 6f 6c 29 3b 20 0a 20 20 20 20 20 20 62 72 65  Col); .      bre
24d79 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
24d7a 0a 20 20 7d 0a 20 20 0a 20 20 69 66 28 20 70 7a  .  }.  .  if( pz
24d7b 4f 72 69 67 69 6e 44 62 20 29 7b 0a 20 20 20 20  OriginDb ){.    
24d7c 61 73 73 65 72 74 28 20 70 7a 4f 72 69 67 69 6e  assert( pzOrigin
24d7d 54 61 62 20 26 26 20 70 7a 4f 72 69 67 69 6e 43  Tab && pzOriginC
24d7e 6f 6c 20 29 3b 0a 20 20 20 20 2a 70 7a 4f 72 69  ol );.    *pzOri
24d7f 67 69 6e 44 62 20 3d 20 7a 4f 72 69 67 69 6e 44  ginDb = zOriginD
24d80 62 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e  b;.    *pzOrigin
24d81 54 61 62 20 3d 20 7a 4f 72 69 67 69 6e 54 61 62  Tab = zOriginTab
24d82 3b 0a 20 20 20 20 2a 70 7a 4f 72 69 67 69 6e 43  ;.    *pzOriginC
24d83 6f 6c 20 3d 20 7a 4f 72 69 67 69 6e 43 6f 6c 3b  ol = zOriginCol;
24d84 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 54  .  }.  return zT
24d85 79 70 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  ype;.}../*.** Ge
24d86 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68 61 74  nerate code that
24d87 20 77 69 6c 6c 20 74 65 6c 6c 20 74 68 65 20 56   will tell the V
24d88 44 42 45 20 74 68 65 20 64 65 63 6c 61 72 61 74  DBE the declarat
24d89 69 6f 6e 20 74 79 70 65 73 20 6f 66 20 63 6f 6c  ion types of col
24d8a 75 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72  umns.** in the r
24d8b 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2f 0a 73 74  esult set..*/.st
24d8c 61 74 69 63 20 76 6f 69 64 20 67 65 6e 65 72 61  atic void genera
24d8d 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 0a 20  teColumnTypes(. 
24d8e 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
24d8f 20 20 20 20 20 2f 2a 20 50 61 72 73 65 72 20 63       /* Parser c
24d90 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
24d91 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
24d92 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62 6c 65  /* List of table
24d93 73 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  s */.  ExprList 
24d94 2a 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 45 78  *pEList    /* Ex
24d95 70 72 65 73 73 69 6f 6e 73 20 64 65 66 69 6e 69  pressions defini
24d96 6e 67 20 74 68 65 20 72 65 73 75 6c 74 20 73 65  ng the result se
24d97 74 20 2a 2f 0a 29 7b 0a 23 69 66 6e 64 65 66 20  t */.){.#ifndef 
24d98 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 43 4c  SQLITE_OMIT_DECL
24d99 54 59 50 45 0a 20 20 56 64 62 65 20 2a 76 20 3d  TYPE.  Vdbe *v =
24d9a 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a   pParse->pVdbe;.
24d9b 20 20 69 6e 74 20 69 3b 0a 20 20 4e 61 6d 65 43    int i;.  NameC
24d9c 6f 6e 74 65 78 74 20 73 4e 43 3b 0a 20 20 73 4e  ontext sNC;.  sN
24d9d 43 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61  C.pSrcList = pTa
24d9e 62 4c 69 73 74 3b 0a 20 20 73 4e 43 2e 70 50 61  bList;.  sNC.pPa
24d9f 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 20 20  rse = pParse;.  
24da0 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45 4c 69 73  for(i=0; i<pELis
24da1 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a  t->nExpr; i++){.
24da2 20 20 20 20 45 78 70 72 20 2a 70 20 3d 20 70 45      Expr *p = pE
24da3 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72  List->a[i].pExpr
24da4 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  ;.    const char
24da5 20 2a 7a 54 79 70 65 3b 0a 23 69 66 64 65 66 20   *zType;.#ifdef 
24da6 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 43 4f  SQLITE_ENABLE_CO
24da7 4c 55 4d 4e 5f 4d 45 54 41 44 41 54 41 0a 20 20  LUMN_METADATA.  
24da8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f    const char *zO
24da9 72 69 67 44 62 20 3d 20 30 3b 0a 20 20 20 20 63  rigDb = 0;.    c
24daa 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67  onst char *zOrig
24dab 54 61 62 20 3d 20 30 3b 0a 20 20 20 20 63 6f 6e  Tab = 0;.    con
24dac 73 74 20 63 68 61 72 20 2a 7a 4f 72 69 67 43 6f  st char *zOrigCo
24dad 6c 20 3d 20 30 3b 0a 20 20 20 20 7a 54 79 70 65  l = 0;.    zType
24dae 20 3d 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26 73   = columnType(&s
24daf 4e 43 2c 20 70 2c 20 26 7a 4f 72 69 67 44 62 2c  NC, p, &zOrigDb,
24db0 20 26 7a 4f 72 69 67 54 61 62 2c 20 26 7a 4f 72   &zOrigTab, &zOr
24db1 69 67 43 6f 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20  igCol);..    /* 
24db2 54 68 65 20 76 64 62 65 20 6d 75 73 74 20 6d 61  The vdbe must ma
24db3 6b 65 20 69 74 73 20 6f 77 6e 20 63 6f 70 79 20  ke its own copy 
24db4 6f 66 20 74 68 65 20 63 6f 6c 75 6d 6e 2d 74 79  of the column-ty
24db5 70 65 20 61 6e 64 20 6f 74 68 65 72 20 0a 20 20  pe and other .  
24db6 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 73 70 65 63    ** column spec
24db7 69 66 69 63 20 73 74 72 69 6e 67 73 2c 20 69 6e  ific strings, in
24db8 20 63 61 73 65 20 74 68 65 20 73 63 68 65 6d 61   case the schema
24db9 20 69 73 20 72 65 73 65 74 20 62 65 66 6f 72 65   is reset before
24dba 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 76 69 72   this.    ** vir
24dbb 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 69 73 20  tual machine is 
24dbc 64 65 6c 65 74 65 64 2e 0a 20 20 20 20 2a 2f 0a  deleted..    */.
24dbd 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 53      sqlite3VdbeS
24dbe 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20  etColName(v, i, 
24dbf 43 4f 4c 4e 41 4d 45 5f 44 41 54 41 42 41 53 45  COLNAME_DATABASE
24dc0 2c 20 7a 4f 72 69 67 44 62 2c 20 53 51 4c 49 54  , zOrigDb, SQLIT
24dc1 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20  E_TRANSIENT);.  
24dc2 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
24dc3 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
24dc4 4c 4e 41 4d 45 5f 54 41 42 4c 45 2c 20 7a 4f 72  LNAME_TABLE, zOr
24dc5 69 67 54 61 62 2c 20 53 51 4c 49 54 45 5f 54 52  igTab, SQLITE_TR
24dc6 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20 73 71  ANSIENT);.    sq
24dc7 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
24dc8 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c 4e 41 4d  ame(v, i, COLNAM
24dc9 45 5f 43 4f 4c 55 4d 4e 2c 20 7a 4f 72 69 67 43  E_COLUMN, zOrigC
24dca 6f 6c 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53  ol, SQLITE_TRANS
24dcb 49 45 4e 54 29 3b 0a 23 65 6c 73 65 0a 20 20 20  IENT);.#else.   
24dcc 20 7a 54 79 70 65 20 3d 20 63 6f 6c 75 6d 6e 54   zType = columnT
24dcd 79 70 65 28 26 73 4e 43 2c 20 70 2c 20 30 2c 20  ype(&sNC, p, 0, 
24dce 30 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 20 20  0, 0);.#endif.  
24dcf 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
24dd0 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
24dd1 4c 4e 41 4d 45 5f 44 45 43 4c 54 59 50 45 2c 20  LNAME_DECLTYPE, 
24dd2 7a 54 79 70 65 2c 20 53 51 4c 49 54 45 5f 54 52  zType, SQLITE_TR
24dd3 41 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 0a 23 65  ANSIENT);.  }.#e
24dd4 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
24dd5 4d 49 54 5f 44 45 43 4c 54 59 50 45 20 2a 2f 0a  MIT_DECLTYPE */.
24dd6 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74  }../*.** Generat
24dd7 65 20 63 6f 64 65 20 74 68 61 74 20 77 69 6c 6c  e code that will
24dd8 20 74 65 6c 6c 20 74 68 65 20 56 44 42 45 20 74   tell the VDBE t
24dd9 68 65 20 6e 61 6d 65 73 20 6f 66 20 63 6f 6c 75  he names of colu
24dda 6d 6e 73 0a 2a 2a 20 69 6e 20 74 68 65 20 72 65  mns.** in the re
24ddb 73 75 6c 74 20 73 65 74 2e 20 20 54 68 69 73 20  sult set.  This 
24ddc 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 73 20 75  information is u
24ddd 73 65 64 20 74 6f 20 70 72 6f 76 69 64 65 20 74  sed to provide t
24dde 68 65 0a 2a 2a 20 61 7a 43 6f 6c 5b 5d 20 76 61  he.** azCol[] va
24ddf 6c 75 65 73 20 69 6e 20 74 68 65 20 63 61 6c 6c  lues in the call
24de0 62 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  back..*/.static 
24de1 76 6f 69 64 20 67 65 6e 65 72 61 74 65 43 6f 6c  void generateCol
24de2 75 6d 6e 4e 61 6d 65 73 28 0a 20 20 50 61 72 73  umnNames(.  Pars
24de3 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
24de4 2f 2a 20 50 61 72 73 65 72 20 63 6f 6e 74 65 78  /* Parser contex
24de5 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
24de6 70 54 61 62 4c 69 73 74 2c 20 20 2f 2a 20 4c 69  pTabList,  /* Li
24de7 73 74 20 6f 66 20 74 61 62 6c 65 73 20 2a 2f 0a  st of tables */.
24de8 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69    ExprList *pELi
24de9 73 74 20 20 20 20 2f 2a 20 45 78 70 72 65 73 73  st    /* Express
24dea 69 6f 6e 73 20 64 65 66 69 6e 69 6e 67 20 74 68  ions defining th
24deb 65 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a  e result set */.
24dec 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
24ded 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
24dee 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 73 71 6c 69  int i, j;.  sqli
24def 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
24df0 2d 3e 64 62 3b 0a 20 20 69 6e 74 20 66 75 6c 6c  ->db;.  int full
24df1 4e 61 6d 65 73 2c 20 73 68 6f 72 74 4e 61 6d 65  Names, shortName
24df2 73 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  s;..#ifndef SQLI
24df3 54 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a  TE_OMIT_EXPLAIN.
24df4 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 73 20    /* If this is 
24df5 61 6e 20 45 58 50 4c 41 49 4e 2c 20 73 6b 69 70  an EXPLAIN, skip
24df6 20 74 68 69 73 20 73 74 65 70 20 2a 2f 0a 20 20   this step */.  
24df7 69 66 28 20 70 50 61 72 73 65 2d 3e 65 78 70 6c  if( pParse->expl
24df8 61 69 6e 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ain ){.    retur
24df9 6e 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20  n;.  }.#endif.. 
24dfa 20 61 73 73 65 72 74 28 20 76 21 3d 30 20 29 3b   assert( v!=0 );
24dfb 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 63  .  if( pParse->c
24dfc 6f 6c 4e 61 6d 65 73 53 65 74 20 7c 7c 20 76 3d  olNamesSet || v=
24dfd 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
24dfe 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b  Failed ) return;
24dff 0a 20 20 70 50 61 72 73 65 2d 3e 63 6f 6c 4e 61  .  pParse->colNa
24e00 6d 65 73 53 65 74 20 3d 20 31 3b 0a 20 20 66 75  mesSet = 1;.  fu
24e01 6c 6c 4e 61 6d 65 73 20 3d 20 28 64 62 2d 3e 66  llNames = (db->f
24e02 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46 75  lags & SQLITE_Fu
24e03 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 3b 0a  llColNames)!=0;.
24e04 20 20 73 68 6f 72 74 4e 61 6d 65 73 20 3d 20 28    shortNames = (
24e05 64 62 2d 3e 66 6c 61 67 73 20 26 20 53 51 4c 49  db->flags & SQLI
24e06 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73  TE_ShortColNames
24e07 29 21 3d 30 3b 0a 20 20 73 71 6c 69 74 65 33 56  )!=0;.  sqlite3V
24e08 64 62 65 53 65 74 4e 75 6d 43 6f 6c 73 28 76 2c  dbeSetNumCols(v,
24e09 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 29 3b   pEList->nExpr);
24e0a 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 45  .  for(i=0; i<pE
24e0b 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b  List->nExpr; i++
24e0c 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70 3b 0a  ){.    Expr *p;.
24e0d 20 20 20 20 70 20 3d 20 70 45 4c 69 73 74 2d 3e      p = pEList->
24e0e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20  a[i].pExpr;.    
24e0f 69 66 28 20 70 3d 3d 30 20 29 20 63 6f 6e 74 69  if( p==0 ) conti
24e10 6e 75 65 3b 0a 20 20 20 20 69 66 28 20 70 45 4c  nue;.    if( pEL
24e11 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
24e12 29 7b 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a  ){.      char *z
24e13 4e 61 6d 65 20 3d 20 70 45 4c 69 73 74 2d 3e 61  Name = pEList->a
24e14 5b 69 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 20  [i].zName;.     
24e15 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74 43   sqlite3VdbeSetC
24e16 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f 4c  olName(v, i, COL
24e17 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d 65  NAME_NAME, zName
24e18 2c 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45  , SQLITE_TRANSIE
24e19 4e 54 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  NT);.    }else i
24e1a 66 28 20 28 70 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f  f( (p->op==TK_CO
24e1b 4c 55 4d 4e 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 54  LUMN || p->op==T
24e1c 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 29 20 26 26  K_AGG_COLUMN) &&
24e1d 20 70 54 61 62 4c 69 73 74 20 29 7b 0a 20 20 20   pTabList ){.   
24e1e 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
24e1f 20 20 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 6c        char *zCol
24e20 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6c  ;.      int iCol
24e21 20 3d 20 70 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20   = p->iColumn;. 
24e22 20 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c       for(j=0; j<
24e23 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 20 26  pTabList->nSrc &
24e24 26 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a 5d  & pTabList->a[j]
24e25 2e 69 43 75 72 73 6f 72 21 3d 70 2d 3e 69 54 61  .iCursor!=p->iTa
24e26 62 6c 65 3b 20 6a 2b 2b 29 7b 7d 0a 20 20 20 20  ble; j++){}.    
24e27 20 20 61 73 73 65 72 74 28 20 6a 3c 70 54 61 62    assert( j<pTab
24e28 4c 69 73 74 2d 3e 6e 53 72 63 20 29 3b 0a 20 20  List->nSrc );.  
24e29 20 20 20 20 70 54 61 62 20 3d 20 70 54 61 62 4c      pTab = pTabL
24e2a 69 73 74 2d 3e 61 5b 6a 5d 2e 70 54 61 62 3b 0a  ist->a[j].pTab;.
24e2b 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c 30        if( iCol<0
24e2c 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d 3e   ) iCol = pTab->
24e2d 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 61 73 73  iPKey;.      ass
24e2e 65 72 74 28 20 69 43 6f 6c 3d 3d 2d 31 20 7c 7c  ert( iCol==-1 ||
24e2f 20 28 69 43 6f 6c 3e 3d 30 20 26 26 20 69 43 6f   (iCol>=0 && iCo
24e30 6c 3c 70 54 61 62 2d 3e 6e 43 6f 6c 29 20 29 3b  l<pTab->nCol) );
24e31 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c  .      if( iCol<
24e32 30 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 43 6f  0 ){.        zCo
24e33 6c 20 3d 20 22 72 6f 77 69 64 22 3b 0a 20 20 20  l = "rowid";.   
24e34 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
24e35 20 20 7a 43 6f 6c 20 3d 20 70 54 61 62 2d 3e 61    zCol = pTab->a
24e36 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61 6d 65 3b  Col[iCol].zName;
24e37 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69  .      }.      i
24e38 66 28 20 21 73 68 6f 72 74 4e 61 6d 65 73 20 26  f( !shortNames &
24e39 26 20 21 66 75 6c 6c 4e 61 6d 65 73 20 29 7b 0a  & !fullNames ){.
24e3a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
24e3b 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c  dbeSetColName(v,
24e3c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45   i, COLNAME_NAME
24e3d 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  , .            s
24e3e 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
24e3f 64 62 2c 20 28 63 68 61 72 2a 29 70 2d 3e 73 70  db, (char*)p->sp
24e40 61 6e 2e 7a 2c 20 70 2d 3e 73 70 61 6e 2e 6e 29  an.z, p->span.n)
24e41 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49 43  , SQLITE_DYNAMIC
24e42 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69  );.      }else i
24e43 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20 7c 7c 20  f( fullNames || 
24e44 28 21 73 68 6f 72 74 4e 61 6d 65 73 20 26 26 20  (!shortNames && 
24e45 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3e 31  pTabList->nSrc>1
24e46 29 20 29 7b 0a 20 20 20 20 20 20 20 20 63 68 61  ) ){.        cha
24e47 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 0a 20 20  r *zName = 0;.  
24e48 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 61 62        char *zTab
24e49 3b 0a 20 0a 20 20 20 20 20 20 20 20 7a 54 61 62  ;. .        zTab
24e4a 20 3d 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 6a   = pTabList->a[j
24e4b 5d 2e 7a 41 6c 69 61 73 3b 0a 20 20 20 20 20 20  ].zAlias;.      
24e4c 20 20 69 66 28 20 66 75 6c 6c 4e 61 6d 65 73 20    if( fullNames 
24e4d 7c 7c 20 7a 54 61 62 3d 3d 30 20 29 20 7a 54 61  || zTab==0 ) zTa
24e4e 62 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 3b  b = pTab->zName;
24e4f 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d  .        zName =
24e50 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
24e51 64 62 2c 20 22 25 73 2e 25 73 22 2c 20 7a 54 61  db, "%s.%s", zTa
24e52 62 2c 20 7a 43 6f 6c 29 3b 0a 20 20 20 20 20 20  b, zCol);.      
24e53 20 20 73 71 6c 69 74 65 33 56 64 62 65 53 65 74    sqlite3VdbeSet
24e54 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c 20 43 4f  ColName(v, i, CO
24e55 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 7a 4e 61 6d  LNAME_NAME, zNam
24e56 65 2c 20 53 51 4c 49 54 45 5f 44 59 4e 41 4d 49  e, SQLITE_DYNAMI
24e57 43 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b  C);.      }else{
24e58 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
24e59 56 64 62 65 53 65 74 43 6f 6c 4e 61 6d 65 28 76  VdbeSetColName(v
24e5a 2c 20 69 2c 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d  , i, COLNAME_NAM
24e5b 45 2c 20 7a 43 6f 6c 2c 20 53 51 4c 49 54 45 5f  E, zCol, SQLITE_
24e5c 54 52 41 4e 53 49 45 4e 54 29 3b 0a 20 20 20 20  TRANSIENT);.    
24e5d 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20    }.    }else{. 
24e5e 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24e5f 53 65 74 43 6f 6c 4e 61 6d 65 28 76 2c 20 69 2c  SetColName(v, i,
24e60 20 43 4f 4c 4e 41 4d 45 5f 4e 41 4d 45 2c 20 0a   COLNAME_NAME, .
24e61 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
24e62 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28  3DbStrNDup(db, (
24e63 63 68 61 72 2a 29 70 2d 3e 73 70 61 6e 2e 7a 2c  char*)p->span.z,
24e64 20 70 2d 3e 73 70 61 6e 2e 6e 29 2c 20 53 51 4c   p->span.n), SQL
24e65 49 54 45 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20  ITE_DYNAMIC);.  
24e66 20 20 7d 0a 20 20 7d 0a 20 20 67 65 6e 65 72 61    }.  }.  genera
24e67 74 65 43 6f 6c 75 6d 6e 54 79 70 65 73 28 70 50  teColumnTypes(pP
24e68 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
24e69 70 45 4c 69 73 74 29 3b 0a 7d 0a 0a 23 69 66 6e  pEList);.}..#ifn
24e6a 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
24e6b 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45 43 54 0a  COMPOUND_SELECT.
24e6c 2f 2a 0a 2a 2a 20 4e 61 6d 65 20 6f 66 20 74 68  /*.** Name of th
24e6d 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 6f 70 65  e connection ope
24e6e 72 61 74 6f 72 2c 20 75 73 65 64 20 66 6f 72 20  rator, used for 
24e6f 65 72 72 6f 72 20 6d 65 73 73 61 67 65 73 2e 0a  error messages..
24e70 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20  */.static const 
24e71 63 68 61 72 20 2a 73 65 6c 65 63 74 4f 70 4e 61  char *selectOpNa
24e72 6d 65 28 69 6e 74 20 69 64 29 7b 0a 20 20 63 68  me(int id){.  ch
24e73 61 72 20 2a 7a 3b 0a 20 20 73 77 69 74 63 68 28  ar *z;.  switch(
24e74 20 69 64 20 29 7b 0a 20 20 20 20 63 61 73 65 20   id ){.    case 
24e75 54 4b 5f 41 4c 4c 3a 20 20 20 20 20 20 20 7a 20  TK_ALL:       z 
24e76 3d 20 22 55 4e 49 4f 4e 20 41 4c 4c 22 3b 20 20  = "UNION ALL";  
24e77 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65   break;.    case
24e78 20 54 4b 5f 49 4e 54 45 52 53 45 43 54 3a 20 7a   TK_INTERSECT: z
24e79 20 3d 20 22 49 4e 54 45 52 53 45 43 54 22 3b 20   = "INTERSECT"; 
24e7a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73    break;.    cas
24e7b 65 20 54 4b 5f 45 58 43 45 50 54 3a 20 20 20 20  e TK_EXCEPT:    
24e7c 7a 20 3d 20 22 45 58 43 45 50 54 22 3b 20 20 20  z = "EXCEPT";   
24e7d 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64 65     break;.    de
24e7e 66 61 75 6c 74 3a 20 20 20 20 20 20 20 20 20 20  fault:          
24e7f 20 7a 20 3d 20 22 55 4e 49 4f 4e 22 3b 20 20 20   z = "UNION";   
24e80 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 7d 0a 20      break;.  }. 
24e81 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23 65 6e   return z;.}.#en
24e82 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
24e83 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
24e84 43 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 47 69 76  CT */../*.** Giv
24e85 65 6e 20 61 20 61 6e 20 65 78 70 72 65 73 73 69  en a an expressi
24e86 6f 6e 20 6c 69 73 74 20 28 77 68 69 63 68 20 69  on list (which i
24e87 73 20 72 65 61 6c 6c 79 20 74 68 65 20 6c 69 73  s really the lis
24e88 74 20 6f 66 20 65 78 70 72 65 73 73 69 6f 6e 73  t of expressions
24e89 0a 2a 2a 20 74 68 61 74 20 66 6f 72 6d 20 74 68  .** that form th
24e8a 65 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20  e result set of 
24e8b 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  a SELECT stateme
24e8c 6e 74 29 20 63 6f 6d 70 75 74 65 20 61 70 70 72  nt) compute appr
24e8d 6f 70 72 69 61 74 65 0a 2a 2a 20 63 6f 6c 75 6d  opriate.** colum
24e8e 6e 20 6e 61 6d 65 73 20 66 6f 72 20 61 20 74 61  n names for a ta
24e8f 62 6c 65 20 74 68 61 74 20 77 6f 75 6c 64 20 68  ble that would h
24e90 6f 6c 64 20 74 68 65 20 65 78 70 72 65 73 73 69  old the expressi
24e91 6f 6e 20 6c 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 41  on list..**.** A
24e92 6c 6c 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20  ll column names 
24e93 77 69 6c 6c 20 62 65 20 75 6e 69 71 75 65 2e 0a  will be unique..
24e94 2a 2a 0a 2a 2a 20 4f 6e 6c 79 20 74 68 65 20 63  **.** Only the c
24e95 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 61 72 65 20  olumn names are 
24e96 63 6f 6d 70 75 74 65 64 2e 20 20 43 6f 6c 75 6d  computed.  Colum
24e97 6e 2e 7a 54 79 70 65 2c 20 43 6f 6c 75 6d 6e 2e  n.zType, Column.
24e98 7a 43 6f 6c 6c 2c 0a 2a 2a 20 61 6e 64 20 6f 74  zColl,.** and ot
24e99 68 65 72 20 66 69 65 6c 64 73 20 6f 66 20 43 6f  her fields of Co
24e9a 6c 75 6d 6e 20 61 72 65 20 7a 65 72 6f 65 64 2e  lumn are zeroed.
24e9b 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 53 51  .**.** Return SQ
24e9c 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
24e9d 73 73 2e 20 20 49 66 20 61 20 6d 65 6d 6f 72 79  ss.  If a memory
24e9e 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
24e9f 72 20 6f 63 63 75 72 73 2c 0a 2a 2a 20 73 74 6f  r occurs,.** sto
24ea0 72 65 20 4e 55 4c 4c 20 69 6e 20 2a 70 61 43 6f  re NULL in *paCo
24ea1 6c 20 61 6e 64 20 30 20 69 6e 20 2a 70 6e 43 6f  l and 0 in *pnCo
24ea2 6c 20 61 6e 64 20 72 65 74 75 72 6e 20 53 51 4c  l and return SQL
24ea3 49 54 45 5f 4e 4f 4d 45 4d 2e 0a 2a 2f 0a 73 74  ITE_NOMEM..*/.st
24ea4 61 74 69 63 20 69 6e 74 20 73 65 6c 65 63 74 43  atic int selectC
24ea5 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
24ea6 73 74 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  st(.  Parse *pPa
24ea7 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  rse,          /*
24ea8 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65 78 74   Parsing context
24ea9 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
24eaa 70 45 4c 69 73 74 2c 20 20 20 20 20 20 20 2f 2a  pEList,       /*
24eab 20 45 78 70 72 20 6c 69 73 74 20 66 72 6f 6d 20   Expr list from 
24eac 77 68 69 63 68 20 74 6f 20 64 65 72 69 76 65 20  which to derive 
24ead 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 20 2a 2f 0a  column names */.
24eae 20 20 69 6e 74 20 2a 70 6e 43 6f 6c 2c 20 20 20    int *pnCol,   
24eaf 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72 69            /* Wri
24eb0 74 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  te the number of
24eb1 20 63 6f 6c 75 6d 6e 73 20 68 65 72 65 20 2a 2f   columns here */
24eb2 0a 20 20 43 6f 6c 75 6d 6e 20 2a 2a 70 61 43 6f  .  Column **paCo
24eb3 6c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 57 72  l          /* Wr
24eb4 69 74 65 20 74 68 65 20 6e 65 77 20 63 6f 6c 75  ite the new colu
24eb5 6d 6e 20 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  mn list here */.
24eb6 29 7b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  ){.  sqlite3 *db
24eb7 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
24eb8 20 69 6e 74 20 69 2c 20 6a 2c 20 63 6e 74 3b 0a   int i, j, cnt;.
24eb9 20 20 43 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20    Column *aCol, 
24eba 2a 70 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e 43 6f  *pCol;.  int nCo
24ebb 6c 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20 20  l;.  Expr *p;.  
24ebc 63 68 61 72 20 2a 7a 4e 61 6d 65 3b 0a 20 20 69  char *zName;.  i
24ebd 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20 2a 70 6e  nt nName;..  *pn
24ebe 43 6f 6c 20 3d 20 6e 43 6f 6c 20 3d 20 70 45 4c  Col = nCol = pEL
24ebf 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 61 43  ist->nExpr;.  aC
24ec0 6f 6c 20 3d 20 2a 70 61 43 6f 6c 20 3d 20 73 71  ol = *paCol = sq
24ec1 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
24ec2 6f 28 64 62 2c 20 73 69 7a 65 6f 66 28 61 43 6f  o(db, sizeof(aCo
24ec3 6c 5b 30 5d 29 2a 6e 43 6f 6c 29 3b 0a 20 20 69  l[0])*nCol);.  i
24ec4 66 28 20 61 43 6f 6c 3d 3d 30 20 29 20 72 65 74  f( aCol==0 ) ret
24ec5 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
24ec6 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 43 6f  ;.  for(i=0, pCo
24ec7 6c 3d 61 43 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20  l=aCol; i<nCol; 
24ec8 69 2b 2b 2c 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20  i++, pCol++){.  
24ec9 20 20 2f 2a 20 47 65 74 20 61 6e 20 61 70 70 72    /* Get an appr
24eca 6f 70 72 69 61 74 65 20 6e 61 6d 65 20 66 6f 72  opriate name for
24ecb 20 74 68 65 20 63 6f 6c 75 6d 6e 0a 20 20 20 20   the column.    
24ecc 2a 2f 0a 20 20 20 20 70 20 3d 20 70 45 4c 69 73  */.    p = pELis
24ecd 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20  t->a[i].pExpr;. 
24ece 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70 52     assert( p->pR
24ecf 69 67 68 74 3d 3d 30 20 7c 7c 20 70 2d 3e 70 52  ight==0 || p->pR
24ed0 69 67 68 74 2d 3e 74 6f 6b 65 6e 2e 7a 3d 3d 30  ight->token.z==0
24ed1 20 7c 7c 20 70 2d 3e 70 52 69 67 68 74 2d 3e 74   || p->pRight->t
24ed2 6f 6b 65 6e 2e 7a 5b 30 5d 21 3d 30 20 29 3b 0a  oken.z[0]!=0 );.
24ed3 20 20 20 20 69 66 28 20 28 7a 4e 61 6d 65 20 3d      if( (zName =
24ed4 20 70 45 4c 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e   pEList->a[i].zN
24ed5 61 6d 65 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  ame)!=0 ){.     
24ed6 20 2f 2a 20 49 66 20 74 68 65 20 63 6f 6c 75 6d   /* If the colum
24ed7 6e 20 63 6f 6e 74 61 69 6e 73 20 61 6e 20 22 41  n contains an "A
24ed8 53 20 3c 6e 61 6d 65 3e 22 20 70 68 72 61 73 65  S <name>" phrase
24ed9 2c 20 75 73 65 20 3c 6e 61 6d 65 3e 20 61 73 20  , use <name> as 
24eda 74 68 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20 20  the name */.    
24edb 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
24edc 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 7a 4e  3DbStrDup(db, zN
24edd 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  ame);.    }else{
24ede 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 43 6f  .      Expr *pCo
24edf 6c 20 3d 20 70 3b 0a 20 20 20 20 20 20 54 61 62  l = p;.      Tab
24ee0 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20 20 20  le *pTab;.      
24ee1 77 68 69 6c 65 28 20 70 43 6f 6c 2d 3e 6f 70 3d  while( pCol->op=
24ee2 3d 54 4b 5f 44 4f 54 20 29 20 70 43 6f 6c 20 3d  =TK_DOT ) pCol =
24ee3 20 70 43 6f 6c 2d 3e 70 52 69 67 68 74 3b 0a 20   pCol->pRight;. 
24ee4 20 20 20 20 20 69 66 28 20 70 43 6f 6c 2d 3e 6f       if( pCol->o
24ee5 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20  p==TK_COLUMN && 
24ee6 28 70 54 61 62 20 3d 20 70 43 6f 6c 2d 3e 70 54  (pTab = pCol->pT
24ee7 61 62 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  ab)!=0 ){.      
24ee8 20 20 2f 2a 20 46 6f 72 20 63 6f 6c 75 6d 6e 73    /* For columns
24ee9 20 75 73 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20   use the column 
24eea 6e 61 6d 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 20  name name */.   
24eeb 20 20 20 20 20 69 6e 74 20 69 43 6f 6c 20 3d 20       int iCol = 
24eec 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20  pCol->iColumn;. 
24eed 20 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c 3c         if( iCol<
24eee 30 20 29 20 69 43 6f 6c 20 3d 20 70 54 61 62 2d  0 ) iCol = pTab-
24eef 3e 69 50 4b 65 79 3b 0a 20 20 20 20 20 20 20 20  >iPKey;.        
24ef0 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d  zName = sqlite3M
24ef1 50 72 69 6e 74 66 28 64 62 2c 20 22 25 73 22 2c  Printf(db, "%s",
24ef2 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
24ef3 20 20 69 43 6f 6c 3e 3d 30 20 3f 20 70 54 61 62    iCol>=0 ? pTab
24ef4 2d 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 2e 7a 4e 61  ->aCol[iCol].zNa
24ef5 6d 65 20 3a 20 22 72 6f 77 69 64 22 29 3b 0a 20  me : "rowid");. 
24ef6 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
24ef7 20 20 20 20 2f 2a 20 55 73 65 20 74 68 65 20 6f      /* Use the o
24ef8 72 69 67 69 6e 61 6c 20 74 65 78 74 20 6f 66 20  riginal text of 
24ef9 74 68 65 20 63 6f 6c 75 6d 6e 20 65 78 70 72 65  the column expre
24efa 73 73 69 6f 6e 20 61 73 20 69 74 73 20 6e 61 6d  ssion as its nam
24efb 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 7a 4e 61  e */.        zNa
24efc 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69  me = sqlite3MPri
24efd 6e 74 66 28 64 62 2c 20 22 25 54 22 2c 20 26 70  ntf(db, "%T", &p
24efe 43 6f 6c 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20  Col->span);.    
24eff 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66    }.    }.    if
24f00 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
24f01 65 64 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ed ){.      sqli
24f02 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
24f03 61 6d 65 29 3b 0a 20 20 20 20 20 20 62 72 65 61  ame);.      brea
24f04 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c  k;.    }.    sql
24f05 69 74 65 33 44 65 71 75 6f 74 65 28 7a 4e 61 6d  ite3Dequote(zNam
24f06 65 29 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b 65  e);..    /* Make
24f07 20 73 75 72 65 20 74 68 65 20 63 6f 6c 75 6d 6e   sure the column
24f08 20 6e 61 6d 65 20 69 73 20 75 6e 69 71 75 65 2e   name is unique.
24f09 20 20 49 66 20 74 68 65 20 6e 61 6d 65 20 69 73    If the name is
24f0a 20 6e 6f 74 20 75 6e 69 71 75 65 2c 0a 20 20 20   not unique,.   
24f0b 20 2a 2a 20 61 70 70 65 6e 64 20 61 20 69 6e 74   ** append a int
24f0c 65 67 65 72 20 74 6f 20 74 68 65 20 6e 61 6d 65  eger to the name
24f0d 20 73 6f 20 74 68 61 74 20 69 74 20 62 65 63 6f   so that it beco
24f0e 6d 65 73 20 75 6e 69 71 75 65 2e 0a 20 20 20 20  mes unique..    
24f0f 2a 2f 0a 20 20 20 20 6e 4e 61 6d 65 20 3d 20 73  */.    nName = s
24f10 74 72 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20  trlen(zName);.  
24f11 20 20 66 6f 72 28 6a 3d 63 6e 74 3d 30 3b 20 6a    for(j=cnt=0; j
24f12 3c 69 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20  <i; j++){.      
24f13 69 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43  if( sqlite3StrIC
24f14 6d 70 28 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65  mp(aCol[j].zName
24f15 2c 20 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20  , zName)==0 ){. 
24f16 20 20 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 65         char *zNe
24f17 77 4e 61 6d 65 3b 0a 20 20 20 20 20 20 20 20 7a  wName;.        z
24f18 4e 61 6d 65 5b 6e 4e 61 6d 65 5d 20 3d 20 30 3b  Name[nName] = 0;
24f19 0a 20 20 20 20 20 20 20 20 7a 4e 65 77 4e 61 6d  .        zNewNam
24f1a 65 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e  e = sqlite3MPrin
24f1b 74 66 28 64 62 2c 20 22 25 73 3a 25 64 22 2c 20  tf(db, "%s:%d", 
24f1c 7a 4e 61 6d 65 2c 20 2b 2b 63 6e 74 29 3b 0a 20  zName, ++cnt);. 
24f1d 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62         sqlite3Db
24f1e 46 72 65 65 28 64 62 2c 20 7a 4e 61 6d 65 29 3b  Free(db, zName);
24f1f 0a 20 20 20 20 20 20 20 20 7a 4e 61 6d 65 20 3d  .        zName =
24f20 20 7a 4e 65 77 4e 61 6d 65 3b 0a 20 20 20 20 20   zNewName;.     
24f21 20 20 20 6a 20 3d 20 2d 31 3b 0a 20 20 20 20 20     j = -1;.     
24f22 20 20 20 69 66 28 20 7a 4e 61 6d 65 3d 3d 30 20     if( zName==0 
24f23 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d  ) break;.      }
24f24 0a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f 6c 2d  .    }.    pCol-
24f25 3e 7a 4e 61 6d 65 20 3d 20 7a 4e 61 6d 65 3b 0a  >zName = zName;.
24f26 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61    }.  if( db->ma
24f27 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
24f28 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72    int j;.    for
24f29 28 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b  (j=0; j<i; j++){
24f2a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62  .      sqlite3Db
24f2b 46 72 65 65 28 64 62 2c 20 61 43 6f 6c 5b 6a 5d  Free(db, aCol[j]
24f2c 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  .zName);.    }. 
24f2d 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
24f2e 28 64 62 2c 20 61 43 6f 6c 29 3b 0a 20 20 20 20  (db, aCol);.    
24f2f 2a 70 61 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  *paCol = 0;.    
24f30 2a 70 6e 43 6f 6c 20 3d 20 30 3b 0a 20 20 20 20  *pnCol = 0;.    
24f31 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
24f32 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  MEM;.  }.  retur
24f33 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
24f34 2f 2a 0a 2a 2a 20 41 64 64 20 74 79 70 65 20 61  /*.** Add type a
24f35 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69 6e 66  nd collation inf
24f36 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 61 20 63 6f  ormation to a co
24f37 6c 75 6d 6e 20 6c 69 73 74 20 62 61 73 65 64 20  lumn list based 
24f38 6f 6e 0a 2a 2a 20 61 20 53 45 4c 45 43 54 20 73  on.** a SELECT s
24f39 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 20 0a 2a 2a  tatement..** .**
24f3a 20 54 68 65 20 63 6f 6c 75 6d 6e 20 6c 69 73 74   The column list
24f3b 20 70 72 65 73 75 6d 61 62 6c 79 20 63 61 6d 65   presumably came
24f3c 20 66 72 6f 6d 20 73 65 6c 65 63 74 43 6f 6c 75   from selectColu
24f3d 6d 6e 4e 61 6d 65 73 46 72 6f 6d 45 78 70 72 4c  mnNamesFromExprL
24f3e 69 73 74 28 29 2e 0a 2a 2a 20 54 68 65 20 63 6f  ist()..** The co
24f3f 6c 75 6d 6e 20 6c 69 73 74 20 68 61 73 20 6f 6e  lumn list has on
24f40 6c 79 20 6e 61 6d 65 73 2c 20 6e 6f 74 20 74 79  ly names, not ty
24f41 70 65 73 20 6f 72 20 63 6f 6c 6c 61 74 69 6f 6e  pes or collation
24f42 73 2e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74  s.  This.** rout
24f43 69 6e 65 20 67 6f 65 73 20 74 68 72 6f 75 67 68  ine goes through
24f44 20 61 6e 64 20 61 64 64 73 20 74 68 65 20 74 79   and adds the ty
24f45 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f  pes and collatio
24f46 6e 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  ns..**.** This r
24f47 6f 75 74 69 6e 65 20 72 65 71 75 69 72 65 73 20  outine requires 
24f48 74 68 61 74 20 61 6c 6c 20 69 6e 64 65 6e 74 69  that all indenti
24f49 66 69 65 72 73 20 69 6e 20 74 68 65 20 53 45 4c  fiers in the SEL
24f4a 45 43 54 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  ECT.** statement
24f4b 20 62 65 20 72 65 73 6f 6c 76 65 64 2e 0a 2a 2f   be resolved..*/
24f4c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 65 6c  .static void sel
24f4d 65 63 74 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65  ectAddColumnType
24f4e 41 6e 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20 20  AndCollation(.  
24f4f 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
24f50 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
24f51 20 63 6f 6e 74 65 78 74 73 20 2a 2f 0a 20 20 69   contexts */.  i
24f52 6e 74 20 6e 43 6f 6c 2c 20 20 20 20 20 20 20 20  nt nCol,        
24f53 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
24f54 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 43  f columns */.  C
24f55 6f 6c 75 6d 6e 20 2a 61 43 6f 6c 2c 20 20 20 20  olumn *aCol,    
24f56 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20       /* List of 
24f57 63 6f 6c 75 6d 6e 73 20 2a 2f 0a 20 20 53 65 6c  columns */.  Sel
24f58 65 63 74 20 2a 70 53 65 6c 65 63 74 20 20 20 20  ect *pSelect    
24f59 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 75 73 65     /* SELECT use
24f5a 64 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 74  d to determine t
24f5b 79 70 65 73 20 61 6e 64 20 63 6f 6c 6c 61 74 69  ypes and collati
24f5c 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ons */.){.  sqli
24f5d 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65  te3 *db = pParse
24f5e 2d 3e 64 62 3b 0a 20 20 4e 61 6d 65 43 6f 6e 74  ->db;.  NameCont
24f5f 65 78 74 20 73 4e 43 3b 0a 20 20 43 6f 6c 75 6d  ext sNC;.  Colum
24f60 6e 20 2a 70 43 6f 6c 3b 0a 20 20 43 6f 6c 6c 53  n *pCol;.  CollS
24f61 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74  eq *pColl;.  int
24f62 20 69 3b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 20   i;.  Expr *p;. 
24f63 20 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74   struct ExprList
24f64 5f 69 74 65 6d 20 2a 61 3b 0a 0a 20 20 61 73 73  _item *a;..  ass
24f65 65 72 74 28 20 70 53 65 6c 65 63 74 21 3d 30 20  ert( pSelect!=0 
24f66 29 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 53  );.  assert( (pS
24f67 65 6c 65 63 74 2d 3e 73 65 6c 46 6c 61 67 73 20  elect->selFlags 
24f68 26 20 53 46 5f 52 65 73 6f 6c 76 65 64 29 21 3d  & SF_Resolved)!=
24f69 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
24f6a 43 6f 6c 3d 3d 70 53 65 6c 65 63 74 2d 3e 70 45  Col==pSelect->pE
24f6b 4c 69 73 74 2d 3e 6e 45 78 70 72 20 7c 7c 20 64  List->nExpr || d
24f6c 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
24f6d 29 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c  );.  if( db->mal
24f6e 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74 75  locFailed ) retu
24f6f 72 6e 3b 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e  rn;.  memset(&sN
24f70 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
24f71 29 29 3b 0a 20 20 73 4e 43 2e 70 53 72 63 4c 69  ));.  sNC.pSrcLi
24f72 73 74 20 3d 20 70 53 65 6c 65 63 74 2d 3e 70 53  st = pSelect->pS
24f73 72 63 3b 0a 20 20 61 20 3d 20 70 53 65 6c 65 63  rc;.  a = pSelec
24f74 74 2d 3e 70 45 4c 69 73 74 2d 3e 61 3b 0a 20 20  t->pEList->a;.  
24f75 66 6f 72 28 69 3d 30 2c 20 70 43 6f 6c 3d 61 43  for(i=0, pCol=aC
24f76 6f 6c 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 2c  ol; i<nCol; i++,
24f77 20 70 43 6f 6c 2b 2b 29 7b 0a 20 20 20 20 70 20   pCol++){.    p 
24f78 3d 20 61 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20  = a[i].pExpr;.  
24f79 20 20 70 43 6f 6c 2d 3e 7a 54 79 70 65 20 3d 20    pCol->zType = 
24f7a 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
24f7b 64 62 2c 20 63 6f 6c 75 6d 6e 54 79 70 65 28 26  db, columnType(&
24f7c 73 4e 43 2c 20 70 2c 20 30 2c 20 30 2c 20 30 29  sNC, p, 0, 0, 0)
24f7d 29 3b 0a 20 20 20 20 70 43 6f 6c 2d 3e 61 66 66  );.    pCol->aff
24f7e 69 6e 69 74 79 20 3d 20 73 71 6c 69 74 65 33 45  inity = sqlite3E
24f7f 78 70 72 41 66 66 69 6e 69 74 79 28 70 29 3b 0a  xprAffinity(p);.
24f80 20 20 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69      pColl = sqli
24f81 74 65 33 45 78 70 72 43 6f 6c 6c 53 65 71 28 70  te3ExprCollSeq(p
24f82 50 61 72 73 65 2c 20 70 29 3b 0a 20 20 20 20 69  Parse, p);.    i
24f83 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20 20  f( pColl ){.    
24f84 20 20 70 43 6f 6c 2d 3e 7a 43 6f 6c 6c 20 3d 20    pCol->zColl = 
24f85 73 71 6c 69 74 65 33 44 62 53 74 72 44 75 70 28  sqlite3DbStrDup(
24f86 64 62 2c 20 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65  db, pColl->zName
24f87 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a 0a  );.    }.  }.}..
24f88 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20 53 45  /*.** Given a SE
24f89 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2c 20  LECT statement, 
24f8a 67 65 6e 65 72 61 74 65 20 61 20 54 61 62 6c 65  generate a Table
24f8b 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
24f8c 64 65 73 63 72 69 62 65 73 0a 2a 2a 20 74 68 65  describes.** the
24f8d 20 72 65 73 75 6c 74 20 73 65 74 20 6f 66 20 74   result set of t
24f8e 68 61 74 20 53 45 4c 45 43 54 2e 0a 2a 2f 0a 53  hat SELECT..*/.S
24f8f 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 54 61  QLITE_PRIVATE Ta
24f90 62 6c 65 20 2a 73 71 6c 69 74 65 33 52 65 73 75  ble *sqlite3Resu
24f91 6c 74 53 65 74 4f 66 53 65 6c 65 63 74 28 50 61  ltSetOfSelect(Pa
24f92 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 65 6c  rse *pParse, Sel
24f93 65 63 74 20 2a 70 53 65 6c 65 63 74 29 7b 0a 20  ect *pSelect){. 
24f94 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20   Table *pTab;.  
24f95 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50  sqlite3 *db = pP
24f96 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e 74 20  arse->db;.  int 
24f97 73 61 76 65 64 46 6c 61 67 73 3b 0a 0a 20 20 73  savedFlags;..  s
24f98 61 76 65 64 46 6c 61 67 73 20 3d 20 64 62 2d 3e  avedFlags = db->
24f99 66 6c 61 67 73 3b 0a 20 20 64 62 2d 3e 66 6c 61  flags;.  db->fla
24f9a 67 73 20 26 3d 20 7e 53 51 4c 49 54 45 5f 46 75  gs &= ~SQLITE_Fu
24f9b 6c 6c 43 6f 6c 4e 61 6d 65 73 3b 0a 20 20 64 62  llColNames;.  db
24f9c 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
24f9d 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65 73 3b  E_ShortColNames;
24f9e 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
24f9f 50 72 65 70 28 70 50 61 72 73 65 2c 20 70 53 65  Prep(pParse, pSe
24fa0 6c 65 63 74 2c 20 30 29 3b 0a 20 20 69 66 28 20  lect, 0);.  if( 
24fa1 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 20 72  pParse->nErr ) r
24fa2 65 74 75 72 6e 20 30 3b 0a 20 20 77 68 69 6c 65  eturn 0;.  while
24fa3 28 20 70 53 65 6c 65 63 74 2d 3e 70 50 72 69 6f  ( pSelect->pPrio
24fa4 72 20 29 20 70 53 65 6c 65 63 74 20 3d 20 70 53  r ) pSelect = pS
24fa5 65 6c 65 63 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  elect->pPrior;. 
24fa6 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73 61 76   db->flags = sav
24fa7 65 64 46 6c 61 67 73 3b 0a 20 20 70 54 61 62 20  edFlags;.  pTab 
24fa8 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
24fa9 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
24faa 28 54 61 62 6c 65 29 20 29 3b 0a 20 20 69 66 28  (Table) );.  if(
24fab 20 70 54 61 62 3d 3d 30 20 29 7b 0a 20 20 20 20   pTab==0 ){.    
24fac 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
24fad 70 54 61 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20  pTab->db = db;. 
24fae 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b   pTab->nRef = 1;
24faf 0a 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 20 3d  .  pTab->zName =
24fb0 20 30 3b 0a 20 20 73 65 6c 65 63 74 43 6f 6c 75   0;.  selectColu
24fb1 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69 73 74 28  mnsFromExprList(
24fb2 70 50 61 72 73 65 2c 20 70 53 65 6c 65 63 74 2d  pParse, pSelect-
24fb3 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e  >pEList, &pTab->
24fb4 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f  nCol, &pTab->aCo
24fb5 6c 29 3b 0a 20 20 73 65 6c 65 63 74 41 64 64 43  l);.  selectAddC
24fb6 6f 6c 75 6d 6e 54 79 70 65 41 6e 64 43 6f 6c 6c  olumnTypeAndColl
24fb7 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 54  ation(pParse, pT
24fb8 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54 61 62 2d 3e  ab->nCol, pTab->
24fb9 61 43 6f 6c 2c 20 70 53 65 6c 65 63 74 29 3b 0a  aCol, pSelect);.
24fba 20 20 70 54 61 62 2d 3e 69 50 4b 65 79 20 3d 20    pTab->iPKey = 
24fbb 2d 31 3b 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61  -1;.  if( db->ma
24fbc 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20 20  llocFailed ){.  
24fbd 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54    sqlite3DeleteT
24fbe 61 62 6c 65 28 70 54 61 62 29 3b 0a 20 20 20 20  able(pTab);.    
24fbf 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
24fc0 72 65 74 75 72 6e 20 70 54 61 62 3b 0a 7d 0a 0a  return pTab;.}..
24fc1 2f 2a 0a 2a 2a 20 47 65 74 20 61 20 56 44 42 45  /*.** Get a VDBE
24fc2 20 66 6f 72 20 74 68 65 20 67 69 76 65 6e 20 70   for the given p
24fc3 61 72 73 65 72 20 63 6f 6e 74 65 78 74 2e 20 20  arser context.  
24fc4 43 72 65 61 74 65 20 61 20 6e 65 77 20 6f 6e 65  Create a new one
24fc5 20 69 66 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   if necessary..*
24fc6 2a 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63  * If an error oc
24fc7 63 75 72 73 2c 20 72 65 74 75 72 6e 20 4e 55 4c  curs, return NUL
24fc8 4c 20 61 6e 64 20 6c 65 61 76 65 20 61 20 6d 65  L and leave a me
24fc9 73 73 61 67 65 20 69 6e 20 70 50 61 72 73 65 2e  ssage in pParse.
24fca 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
24fcb 54 45 20 56 64 62 65 20 2a 73 71 6c 69 74 65 33  TE Vdbe *sqlite3
24fcc 47 65 74 56 64 62 65 28 50 61 72 73 65 20 2a 70  GetVdbe(Parse *p
24fcd 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20 2a  Parse){.  Vdbe *
24fce 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
24fcf 65 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 7b  e;.  if( v==0 ){
24fd0 0a 20 20 20 20 76 20 3d 20 70 50 61 72 73 65 2d  .    v = pParse-
24fd1 3e 70 56 64 62 65 20 3d 20 73 71 6c 69 74 65 33  >pVdbe = sqlite3
24fd2 56 64 62 65 43 72 65 61 74 65 28 70 50 61 72 73  VdbeCreate(pPars
24fd3 65 2d 3e 64 62 29 3b 0a 23 69 66 6e 64 65 66 20  e->db);.#ifndef 
24fd4 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41 43  SQLITE_OMIT_TRAC
24fd5 45 0a 20 20 20 20 69 66 28 20 76 20 29 7b 0a 20  E.    if( v ){. 
24fd6 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
24fd7 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 54 72 61  AddOp0(v, OP_Tra
24fd8 63 65 29 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69  ce);.    }.#endi
24fd9 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 76  f.  }.  return v
24fda 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70  ;.}.../*.** Comp
24fdb 75 74 65 20 74 68 65 20 69 4c 69 6d 69 74 20 61  ute the iLimit a
24fdc 6e 64 20 69 4f 66 66 73 65 74 20 66 69 65 6c 64  nd iOffset field
24fdd 73 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  s of the SELECT 
24fde 62 61 73 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20  based on the.** 
24fdf 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66 73  pLimit and pOffs
24fe0 65 74 20 65 78 70 72 65 73 73 69 6f 6e 73 2e 20  et expressions. 
24fe1 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66 66   pLimit and pOff
24fe2 73 65 74 20 68 6f 6c 64 20 74 68 65 20 65 78 70  set hold the exp
24fe3 72 65 73 73 69 6f 6e 73 0a 2a 2a 20 74 68 61 74  ressions.** that
24fe4 20 61 70 70 65 61 72 20 69 6e 20 74 68 65 20 6f   appear in the o
24fe5 72 69 67 69 6e 61 6c 20 53 51 4c 20 73 74 61 74  riginal SQL stat
24fe6 65 6d 65 6e 74 20 61 66 74 65 72 20 74 68 65 20  ement after the 
24fe7 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45 54  LIMIT and OFFSET
24fe8 0a 2a 2a 20 6b 65 79 77 6f 72 64 73 2e 20 20 4f  .** keywords.  O
24fe9 72 20 4e 55 4c 4c 20 69 66 20 74 68 6f 73 65 20  r NULL if those 
24fea 6b 65 79 77 6f 72 64 73 20 61 72 65 20 6f 6d 69  keywords are omi
24feb 74 74 65 64 2e 20 69 4c 69 6d 69 74 20 61 6e 64  tted. iLimit and
24fec 20 69 4f 66 66 73 65 74 20 0a 2a 2a 20 61 72 65   iOffset .** are
24fed 20 74 68 65 20 69 6e 74 65 67 65 72 20 6d 65 6d   the integer mem
24fee 6f 72 79 20 72 65 67 69 73 74 65 72 20 6e 75 6d  ory register num
24fef 62 65 72 73 20 66 6f 72 20 63 6f 75 6e 74 65 72  bers for counter
24ff0 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70 75 74  s used to comput
24ff1 65 20 0a 2a 2a 20 74 68 65 20 6c 69 6d 69 74 20  e .** the limit 
24ff2 61 6e 64 20 6f 66 66 73 65 74 2e 20 20 49 66 20  and offset.  If 
24ff3 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69 6d 69  there is no limi
24ff4 74 20 61 6e 64 2f 6f 72 20 6f 66 66 73 65 74 2c  t and/or offset,
24ff5 20 74 68 65 6e 20 0a 2a 2a 20 69 4c 69 6d 69 74   then .** iLimit
24ff6 20 61 6e 64 20 69 4f 66 66 73 65 74 20 61 72 65   and iOffset are
24ff7 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a 2a 2a   negative..**.**
24ff8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 63 68   This routine ch
24ff9 61 6e 67 65 73 20 74 68 65 20 76 61 6c 75 65 73  anges the values
24ffa 20 6f 66 20 69 4c 69 6d 69 74 20 61 6e 64 20 69   of iLimit and i
24ffb 4f 66 66 73 65 74 20 6f 6e 6c 79 20 69 66 0a 2a  Offset only if.*
24ffc 2a 20 61 20 6c 69 6d 69 74 20 6f 72 20 6f 66 66  * a limit or off
24ffd 73 65 74 20 69 73 20 64 65 66 69 6e 65 64 20 62  set is defined b
24ffe 79 20 70 4c 69 6d 69 74 20 61 6e 64 20 70 4f 66  y pLimit and pOf
24fff 66 73 65 74 2e 20 20 69 4c 69 6d 69 74 20 61 6e  fset.  iLimit an
25000 64 0a 2a 2a 20 69 4f 66 66 73 65 74 20 73 68 6f  d.** iOffset sho
25001 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 70 72  uld have been pr
25002 65 73 65 74 20 74 6f 20 61 70 70 72 6f 70 72 69  eset to appropri
25003 61 74 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  ate default valu
25004 65 73 0a 2a 2a 20 28 75 73 75 61 6c 6c 79 20 62  es.** (usually b
25005 75 74 20 6e 6f 74 20 61 6c 77 61 79 73 20 2d 31  ut not always -1
25006 29 20 70 72 69 6f 72 20 74 6f 20 63 61 6c 6c 69  ) prior to calli
25007 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65 2e  ng this routine.
25008 0a 2a 2a 20 4f 6e 6c 79 20 69 66 20 70 4c 69 6d  .** Only if pLim
25009 69 74 21 3d 30 20 6f 72 20 70 4f 66 66 73 65 74  it!=0 or pOffset
2500a 21 3d 30 20 64 6f 20 74 68 65 20 6c 69 6d 69 74  !=0 do the limit
2500b 20 72 65 67 69 73 74 65 72 73 20 67 65 74 0a 2a   registers get.*
2500c 2a 20 72 65 64 65 66 69 6e 65 64 2e 20 20 54 68  * redefined.  Th
2500d 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72  e UNION ALL oper
2500e 61 74 6f 72 20 75 73 65 73 20 74 68 69 73 20 70  ator uses this p
2500f 72 6f 70 65 72 74 79 20 74 6f 20 66 6f 72 63 65  roperty to force
25010 0a 2a 2a 20 74 68 65 20 72 65 75 73 65 20 6f 66  .** the reuse of
25011 20 74 68 65 20 73 61 6d 65 20 6c 69 6d 69 74 20   the same limit 
25012 61 6e 64 20 6f 66 66 73 65 74 20 72 65 67 69 73  and offset regis
25013 74 65 72 73 20 61 63 72 6f 73 73 20 6d 75 6c 74  ters across mult
25014 69 70 6c 65 0a 2a 2a 20 53 45 4c 45 43 54 20 73  iple.** SELECT s
25015 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74  tatements..*/.st
25016 61 74 69 63 20 76 6f 69 64 20 63 6f 6d 70 75 74  atic void comput
25017 65 4c 69 6d 69 74 52 65 67 69 73 74 65 72 73 28  eLimitRegisters(
25018 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53  Parse *pParse, S
25019 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 42  elect *p, int iB
2501a 72 65 61 6b 29 7b 0a 20 20 56 64 62 65 20 2a 76  reak){.  Vdbe *v
2501b 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4c 69 6d   = 0;.  int iLim
2501c 69 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 4f  it = 0;.  int iO
2501d 66 66 73 65 74 3b 0a 20 20 69 6e 74 20 61 64 64  ffset;.  int add
2501e 72 31 3b 0a 20 20 69 66 28 20 70 2d 3e 69 4c 69  r1;.  if( p->iLi
2501f 6d 69 74 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20  mit ) return;.. 
25020 20 2f 2a 20 0a 20 20 2a 2a 20 22 4c 49 4d 49 54   /* .  ** "LIMIT
25021 20 2d 31 22 20 61 6c 77 61 79 73 20 73 68 6f 77   -1" always show
25022 73 20 61 6c 6c 20 72 6f 77 73 2e 20 20 54 68 65  s all rows.  The
25023 72 65 20 69 73 20 73 6f 6d 65 0a 20 20 2a 2a 20  re is some.  ** 
25024 63 6f 6e 74 72 61 76 65 72 73 79 20 61 62 6f 75  contraversy abou
25025 74 20 77 68 61 74 20 74 68 65 20 63 6f 72 72 65  t what the corre
25026 63 74 20 62 65 68 61 76 69 6f 72 20 73 68 6f 75  ct behavior shou
25027 6c 64 20 62 65 2e 0a 20 20 2a 2a 20 54 68 65 20  ld be..  ** The 
25028 63 75 72 72 65 6e 74 20 69 6d 70 6c 65 6d 65 6e  current implemen
25029 74 61 74 69 6f 6e 20 69 6e 74 65 72 70 72 65 74  tation interpret
2502a 73 20 22 4c 49 4d 49 54 20 30 22 20 74 6f 20 6d  s "LIMIT 0" to m
2502b 65 61 6e 0a 20 20 2a 2a 20 6e 6f 20 72 6f 77 73  ean.  ** no rows
2502c 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 2d 3e  ..  */.  if( p->
2502d 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 70 2d  pLimit ){.    p-
2502e 3e 69 4c 69 6d 69 74 20 3d 20 69 4c 69 6d 69 74  >iLimit = iLimit
2502f 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65   = ++pParse->nMe
25030 6d 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74  m;.    v = sqlit
25031 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
25032 29 3b 0a 20 20 20 20 69 66 28 20 76 3d 3d 30 20  );.    if( v==0 
25033 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 73 71  ) return;.    sq
25034 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
25035 61 72 73 65 2c 20 70 2d 3e 70 4c 69 6d 69 74 2c  arse, p->pLimit,
25036 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 73 71   iLimit);.    sq
25037 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
25038 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
25039 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20 20 56 64   iLimit);.    Vd
2503a 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4c  beComment((v, "L
2503b 49 4d 49 54 20 63 6f 75 6e 74 65 72 22 29 29 3b  IMIT counter"));
2503c 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
2503d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 5a  AddOp2(v, OP_IfZ
2503e 65 72 6f 2c 20 69 4c 69 6d 69 74 2c 20 69 42 72  ero, iLimit, iBr
2503f 65 61 6b 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  eak);.  }.  if( 
25040 70 2d 3e 70 4f 66 66 73 65 74 20 29 7b 0a 20 20  p->pOffset ){.  
25041 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 69    p->iOffset = i
25042 4f 66 66 73 65 74 20 3d 20 2b 2b 70 50 61 72 73  Offset = ++pPars
25043 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 69 66 28  e->nMem;.    if(
25044 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20   p->pLimit ){.  
25045 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d      pParse->nMem
25046 2b 2b 3b 20 20 20 2f 2a 20 41 6c 6c 6f 63 61 74  ++;   /* Allocat
25047 65 20 61 6e 20 65 78 74 72 61 20 72 65 67 69 73  e an extra regis
25048 74 65 72 20 66 6f 72 20 6c 69 6d 69 74 2b 6f 66  ter for limit+of
25049 66 73 65 74 20 2a 2f 0a 20 20 20 20 7d 0a 20 20  fset */.    }.  
2504a 20 20 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74    v = sqlite3Get
2504b 56 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20  Vdbe(pParse);.  
2504c 20 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74    if( v==0 ) ret
2504d 75 72 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  urn;.    sqlite3
2504e 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
2504f 20 70 2d 3e 70 4f 66 66 73 65 74 2c 20 69 4f 66   p->pOffset, iOf
25050 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  fset);.    sqlit
25051 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
25052 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20 69 4f  OP_MustBeInt, iO
25053 66 66 73 65 74 29 3b 0a 20 20 20 20 56 64 62 65  ffset);.    Vdbe
25054 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 46 46  Comment((v, "OFF
25055 53 45 54 20 63 6f 75 6e 74 65 72 22 29 29 3b 0a  SET counter"));.
25056 20 20 20 20 61 64 64 72 31 20 3d 20 73 71 6c 69      addr1 = sqli
25057 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c  te3VdbeAddOp1(v,
25058 20 4f 50 5f 49 66 50 6f 73 2c 20 69 4f 66 66 73   OP_IfPos, iOffs
25059 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  et);.    sqlite3
2505a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2505b 5f 49 6e 74 65 67 65 72 2c 20 30 2c 20 69 4f 66  _Integer, 0, iOf
2505c 66 73 65 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  fset);.    sqlit
2505d 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
2505e 2c 20 61 64 64 72 31 29 3b 0a 20 20 20 20 69 66  , addr1);.    if
2505f 28 20 70 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20  ( p->pLimit ){. 
25060 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25061 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 41 64 64  AddOp3(v, OP_Add
25062 2c 20 69 4c 69 6d 69 74 2c 20 69 4f 66 66 73 65  , iLimit, iOffse
25063 74 2c 20 69 4f 66 66 73 65 74 2b 31 29 3b 0a 20  t, iOffset+1);. 
25064 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
25065 28 28 76 2c 20 22 4c 49 4d 49 54 2b 4f 46 46 53  ((v, "LIMIT+OFFS
25066 45 54 22 29 29 3b 0a 20 20 20 20 20 20 61 64 64  ET"));.      add
25067 72 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  r1 = sqlite3Vdbe
25068 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 49 66 50  AddOp1(v, OP_IfP
25069 6f 73 2c 20 69 4c 69 6d 69 74 29 3b 0a 20 20 20  os, iLimit);.   
2506a 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2506b 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
2506c 65 72 2c 20 2d 31 2c 20 69 4f 66 66 73 65 74 2b  er, -1, iOffset+
2506d 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
2506e 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
2506f 20 61 64 64 72 31 29 3b 0a 20 20 20 20 7d 0a 20   addr1);.    }. 
25070 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51   }.}..#ifndef SQ
25071 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
25072 4e 44 5f 53 45 4c 45 43 54 0a 2f 2a 0a 2a 2a 20  ND_SELECT./*.** 
25073 52 65 74 75 72 6e 20 74 68 65 20 61 70 70 72 6f  Return the appro
25074 70 72 69 61 74 65 20 63 6f 6c 6c 61 74 69 6e 67  priate collating
25075 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74 68   sequence for th
25076 65 20 69 43 6f 6c 2d 74 68 20 63 6f 6c 75 6d 6e  e iCol-th column
25077 20 6f 66 0a 2a 2a 20 74 68 65 20 72 65 73 75 6c   of.** the resul
25078 74 20 73 65 74 20 66 6f 72 20 74 68 65 20 63 6f  t set for the co
25079 6d 70 6f 75 6e 64 2d 73 65 6c 65 63 74 20 73 74  mpound-select st
2507a 61 74 65 6d 65 6e 74 20 22 70 22 2e 20 20 52 65  atement "p".  Re
2507b 74 75 72 6e 20 4e 55 4c 4c 20 69 66 0a 2a 2a 20  turn NULL if.** 
2507c 74 68 65 20 63 6f 6c 75 6d 6e 20 68 61 73 20 6e  the column has n
2507d 6f 20 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74  o default collat
2507e 69 6e 67 20 73 65 71 75 65 6e 63 65 2e 0a 2a 2a  ing sequence..**
2507f 0a 2a 2a 20 54 68 65 20 63 6f 6c 6c 61 74 69 6e  .** The collatin
25080 67 20 73 65 71 75 65 6e 63 65 20 66 6f 72 20 74  g sequence for t
25081 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65  he compound sele
25082 63 74 20 69 73 20 74 61 6b 65 6e 20 66 72 6f 6d  ct is taken from
25083 20 74 68 65 0a 2a 2a 20 6c 65 66 74 2d 6d 6f 73   the.** left-mos
25084 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20 73 65  t term of the se
25085 6c 65 63 74 20 74 68 61 74 20 68 61 73 20 61 20  lect that has a 
25086 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
25087 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 43 6f  ce..*/.static Co
25088 6c 6c 53 65 71 20 2a 6d 75 6c 74 69 53 65 6c 65  llSeq *multiSele
25089 63 74 43 6f 6c 6c 53 65 71 28 50 61 72 73 65 20  ctCollSeq(Parse 
2508a 2a 70 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20  *pParse, Select 
2508b 2a 70 2c 20 69 6e 74 20 69 43 6f 6c 29 7b 0a 20  *p, int iCol){. 
2508c 20 43 6f 6c 6c 53 65 71 20 2a 70 52 65 74 3b 0a   CollSeq *pRet;.
2508d 20 20 69 66 28 20 70 2d 3e 70 50 72 69 6f 72 20    if( p->pPrior 
2508e 29 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 6d 75  ){.    pRet = mu
2508f 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71  ltiSelectCollSeq
25090 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 50 72 69  (pParse, p->pPri
25091 6f 72 2c 20 69 43 6f 6c 29 3b 0a 20 20 7d 65 6c  or, iCol);.  }el
25092 73 65 7b 0a 20 20 20 20 70 52 65 74 20 3d 20 30  se{.    pRet = 0
25093 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 52 65 74  ;.  }.  if( pRet
25094 3d 3d 30 20 29 7b 0a 20 20 20 20 70 52 65 74 20  ==0 ){.    pRet 
25095 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
25096 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 2d 3e  lSeq(pParse, p->
25097 70 45 4c 69 73 74 2d 3e 61 5b 69 43 6f 6c 5d 2e  pEList->a[iCol].
25098 70 45 78 70 72 29 3b 0a 20 20 7d 0a 20 20 72 65  pExpr);.  }.  re
25099 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 23 65 6e  turn pRet;.}.#en
2509a 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2509b 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
2509c 43 54 20 2a 2f 0a 0a 2f 2a 20 46 6f 72 77 61 72  CT */../* Forwar
2509d 64 20 72 65 66 65 72 65 6e 63 65 20 2a 2f 0a 73  d reference */.s
2509e 74 61 74 69 63 20 69 6e 74 20 6d 75 6c 74 69 53  tatic int multiS
2509f 65 6c 65 63 74 4f 72 64 65 72 42 79 28 0a 20 20  electOrderBy(.  
250a0 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
250a1 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
250a2 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
250a3 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
250a4 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68 74      /* The right
250a5 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54 73  -most of SELECTs
250a6 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f 0a   to be coded */.
250a7 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70 44    SelectDest *pD
250a8 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74 20  est     /* What 
250a9 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72 79  to do with query
250aa 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 3b 0a 0a   results */.);..
250ab 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
250ac 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45  OMIT_COMPOUND_SE
250ad 4c 45 43 54 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  LECT./*.** This 
250ae 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
250af 64 20 74 6f 20 70 72 6f 63 65 73 73 20 61 20 63  d to process a c
250b0 6f 6d 70 6f 75 6e 64 20 71 75 65 72 79 20 66 6f  ompound query fo
250b1 72 6d 20 66 72 6f 6d 0a 2a 2a 20 74 77 6f 20 6f  rm from.** two o
250b2 72 20 6d 6f 72 65 20 73 65 70 61 72 61 74 65 20  r more separate 
250b3 71 75 65 72 69 65 73 20 75 73 69 6e 67 20 55 4e  queries using UN
250b4 49 4f 4e 2c 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20  ION, UNION ALL, 
250b5 45 58 43 45 50 54 2c 20 6f 72 0a 2a 2a 20 49 4e  EXCEPT, or.** IN
250b6 54 45 52 53 45 43 54 0a 2a 2a 0a 2a 2a 20 22 70  TERSECT.**.** "p
250b7 22 20 70 6f 69 6e 74 73 20 74 6f 20 74 68 65 20  " points to the 
250b8 72 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 74 68  right-most of th
250b9 65 20 74 77 6f 20 71 75 65 72 69 65 73 2e 20 20  e two queries.  
250ba 74 68 65 20 71 75 65 72 79 20 6f 6e 20 74 68 65  the query on the
250bb 0a 2a 2a 20 6c 65 66 74 20 69 73 20 70 2d 3e 70  .** left is p->p
250bc 50 72 69 6f 72 2e 20 20 54 68 65 20 6c 65 66 74  Prior.  The left
250bd 20 71 75 65 72 79 20 63 6f 75 6c 64 20 61 6c 73   query could als
250be 6f 20 62 65 20 61 20 63 6f 6d 70 6f 75 6e 64 20  o be a compound 
250bf 71 75 65 72 79 0a 2a 2a 20 69 6e 20 77 68 69 63  query.** in whic
250c0 68 20 63 61 73 65 20 74 68 69 73 20 72 6f 75 74  h case this rout
250c1 69 6e 65 20 77 69 6c 6c 20 62 65 20 63 61 6c 6c  ine will be call
250c2 65 64 20 72 65 63 75 72 73 69 76 65 6c 79 2e 20  ed recursively. 
250c3 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c  .**.** The resul
250c4 74 73 20 6f 66 20 74 68 65 20 74 6f 74 61 6c 20  ts of the total 
250c5 71 75 65 72 79 20 61 72 65 20 74 6f 20 62 65 20  query are to be 
250c6 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 61 20 64  written into a d
250c7 65 73 74 69 6e 61 74 69 6f 6e 0a 2a 2a 20 6f 66  estination.** of
250c8 20 74 79 70 65 20 65 44 65 73 74 20 77 69 74 68   type eDest with
250c9 20 70 61 72 61 6d 65 74 65 72 20 69 50 61 72 6d   parameter iParm
250ca 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c 65 20  ..**.** Example 
250cb 31 3a 20 20 43 6f 6e 73 69 64 65 72 20 61 20 74  1:  Consider a t
250cc 68 72 65 65 2d 77 61 79 20 63 6f 6d 70 6f 75 6e  hree-way compoun
250cd 64 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 2e  d SQL statement.
250ce 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43  .**.**     SELEC
250cf 54 20 61 20 46 52 4f 4d 20 74 31 20 55 4e 49 4f  T a FROM t1 UNIO
250d0 4e 20 53 45 4c 45 43 54 20 62 20 46 52 4f 4d 20  N SELECT b FROM 
250d1 74 32 20 55 4e 49 4f 4e 20 53 45 4c 45 43 54 20  t2 UNION SELECT 
250d2 63 20 46 52 4f 4d 20 74 33 0a 2a 2a 0a 2a 2a 20  c FROM t3.**.** 
250d3 54 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 69  This statement i
250d4 73 20 70 61 72 73 65 64 20 75 70 20 61 73 20 66  s parsed up as f
250d5 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  ollows:.**.**   
250d6 20 20 53 45 4c 45 43 54 20 63 20 46 52 4f 4d 20    SELECT c FROM 
250d7 74 33 0a 2a 2a 20 20 20 20 20 20 7c 0a 2a 2a 20  t3.**      |.** 
250d8 20 20 20 20 20 60 2d 2d 2d 2d 2d 3e 20 20 53 45       `----->  SE
250d9 4c 45 43 54 20 62 20 46 52 4f 4d 20 74 32 0a 2a  LECT b FROM t2.*
250da 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  *               
250db 20 7c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20   |.**           
250dc 20 20 20 20 20 60 2d 2d 2d 2d 2d 2d 3e 20 20 53       `------>  S
250dd 45 4c 45 43 54 20 61 20 46 52 4f 4d 20 74 31 0a  ELECT a FROM t1.
250de 2a 2a 0a 2a 2a 20 54 68 65 20 61 72 72 6f 77 73  **.** The arrows
250df 20 69 6e 20 74 68 65 20 64 69 61 67 72 61 6d 20   in the diagram 
250e0 61 62 6f 76 65 20 72 65 70 72 65 73 65 6e 74 20  above represent 
250e1 74 68 65 20 53 65 6c 65 63 74 2e 70 50 72 69 6f  the Select.pPrio
250e2 72 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 20 53 6f  r pointer..** So
250e3 20 69 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   if this routine
250e4 20 69 73 20 63 61 6c 6c 65 64 20 77 69 74 68 20   is called with 
250e5 70 20 65 71 75 61 6c 20 74 6f 20 74 68 65 20 74  p equal to the t
250e6 33 20 71 75 65 72 79 2c 20 74 68 65 6e 0a 2a 2a  3 query, then.**
250e7 20 70 50 72 69 6f 72 20 77 69 6c 6c 20 62 65 20   pPrior will be 
250e8 74 68 65 20 74 32 20 71 75 65 72 79 2e 20 20 70  the t2 query.  p
250e9 2d 3e 6f 70 20 77 69 6c 6c 20 62 65 20 54 4b 5f  ->op will be TK_
250ea 55 4e 49 4f 4e 20 69 6e 20 74 68 69 73 20 63 61  UNION in this ca
250eb 73 65 2e 0a 2a 2a 0a 2a 2a 20 4e 6f 74 69 63 65  se..**.** Notice
250ec 20 74 68 61 74 20 62 65 63 61 75 73 65 20 6f 66   that because of
250ed 20 74 68 65 20 77 61 79 20 53 51 4c 69 74 65 20   the way SQLite 
250ee 70 61 72 73 65 73 20 63 6f 6d 70 6f 75 6e 64 20  parses compound 
250ef 53 45 4c 45 43 54 73 2c 20 74 68 65 0a 2a 2a 20  SELECTs, the.** 
250f0 69 6e 64 69 76 69 64 75 61 6c 20 73 65 6c 65 63  individual selec
250f1 74 73 20 61 6c 77 61 79 73 20 67 72 6f 75 70 20  ts always group 
250f2 66 72 6f 6d 20 6c 65 66 74 20 74 6f 20 72 69 67  from left to rig
250f3 68 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ht..*/.static in
250f4 74 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 0a 20  t multiSelect(. 
250f5 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
250f6 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e         /* Parsin
250f7 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
250f8 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
250f9 20 20 20 20 20 2f 2a 20 54 68 65 20 72 69 67 68       /* The righ
250fa 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c 45 43 54  t-most of SELECT
250fb 73 20 74 6f 20 62 65 20 63 6f 64 65 64 20 2a 2f  s to be coded */
250fc 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20 2a 70  .  SelectDest *p
250fd 44 65 73 74 20 20 20 20 20 2f 2a 20 57 68 61 74  Dest     /* What
250fe 20 74 6f 20 64 6f 20 77 69 74 68 20 71 75 65 72   to do with quer
250ff 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a 29 7b 0a  y results */.){.
25100 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
25101 45 5f 4f 4b 3b 20 20 20 2f 2a 20 53 75 63 63 65  E_OK;   /* Succe
25102 73 73 20 63 6f 64 65 20 66 72 6f 6d 20 61 20 73  ss code from a s
25103 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 53  ubroutine */.  S
25104 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b 20 20  elect *pPrior;  
25105 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65 72 20       /* Another 
25106 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61 74 65  SELECT immediate
25107 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74 20 2a  ly to our left *
25108 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20 20  /.  Vdbe *v;    
25109 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47 65 6e            /* Gen
2510a 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20 74 68  erate code to th
2510b 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53 65 6c  is VDBE */.  Sel
2510c 65 63 74 44 65 73 74 20 64 65 73 74 3b 20 20 20  ectDest dest;   
2510d 20 20 20 2f 2a 20 41 6c 74 65 72 6e 61 74 69 76     /* Alternativ
2510e 65 20 64 61 74 61 20 64 65 73 74 69 6e 61 74 69  e data destinati
2510f 6f 6e 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  on */.  Select *
25110 70 44 65 6c 65 74 65 20 3d 20 30 3b 20 20 2f 2a  pDelete = 0;  /*
25111 20 43 68 61 69 6e 20 6f 66 20 73 69 6d 70 6c 65   Chain of simple
25112 20 73 65 6c 65 63 74 73 20 74 6f 20 64 65 6c 65   selects to dele
25113 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 20  te */.  sqlite3 
25114 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  *db;          /*
25115 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63   Database connec
25116 74 69 6f 6e 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61  tion */..  /* Ma
25117 6b 65 20 73 75 72 65 20 74 68 65 72 65 20 69 73  ke sure there is
25118 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 6f 72 20   no ORDER BY or 
25119 4c 49 4d 49 54 20 63 6c 61 75 73 65 20 6f 6e 20  LIMIT clause on 
2511a 70 72 69 6f 72 20 53 45 4c 45 43 54 73 2e 20 20  prior SELECTs.  
2511b 4f 6e 6c 79 0a 20 20 2a 2a 20 74 68 65 20 6c 61  Only.  ** the la
2511c 73 74 20 28 72 69 67 68 74 2d 6d 6f 73 74 29 20  st (right-most) 
2511d 53 45 4c 45 43 54 20 69 6e 20 74 68 65 20 73 65  SELECT in the se
2511e 72 69 65 73 20 6d 61 79 20 68 61 76 65 20 61 6e  ries may have an
2511f 20 4f 52 44 45 52 20 42 59 20 6f 72 20 4c 49 4d   ORDER BY or LIM
25120 49 54 2e 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72  IT..  */.  asser
25121 74 28 20 70 20 26 26 20 70 2d 3e 70 50 72 69 6f  t( p && p->pPrio
25122 72 20 29 3b 20 20 2f 2a 20 43 61 6c 6c 69 6e 67  r );  /* Calling
25123 20 66 75 6e 63 74 69 6f 6e 20 67 75 61 72 61 6e   function guaran
25124 74 65 65 73 20 74 68 69 73 20 6d 75 63 68 20 2a  tees this much *
25125 2f 0a 20 20 64 62 20 3d 20 70 50 61 72 73 65 2d  /.  db = pParse-
25126 3e 64 62 3b 0a 20 20 70 50 72 69 6f 72 20 3d 20  >db;.  pPrior = 
25127 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61 73 73  p->pPrior;.  ass
25128 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70 52 69  ert( pPrior->pRi
25129 67 68 74 6d 6f 73 74 21 3d 70 50 72 69 6f 72 20  ghtmost!=pPrior 
2512a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 72  );.  assert( pPr
2512b 69 6f 72 2d 3e 70 52 69 67 68 74 6d 6f 73 74 3d  ior->pRightmost=
2512c 3d 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 29  =p->pRightmost )
2512d 3b 0a 20 20 64 65 73 74 20 3d 20 2a 70 44 65 73  ;.  dest = *pDes
2512e 74 3b 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d  t;.  if( pPrior-
2512f 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20  >pOrderBy ){.   
25130 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
25131 28 70 50 61 72 73 65 2c 22 4f 52 44 45 52 20 42  (pParse,"ORDER B
25132 59 20 63 6c 61 75 73 65 20 73 68 6f 75 6c 64 20  Y clause should 
25133 63 6f 6d 65 20 61 66 74 65 72 20 25 73 20 6e 6f  come after %s no
25134 74 20 62 65 66 6f 72 65 22 2c 0a 20 20 20 20 20  t before",.     
25135 20 73 65 6c 65 63 74 4f 70 4e 61 6d 65 28 70 2d   selectOpName(p-
25136 3e 6f 70 29 29 3b 0a 20 20 20 20 72 63 20 3d 20  >op));.    rc = 
25137 31 3b 0a 20 20 20 20 67 6f 74 6f 20 6d 75 6c 74  1;.    goto mult
25138 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20 20  i_select_end;.  
25139 7d 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e  }.  if( pPrior->
2513a 70 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20 73 71  pLimit ){.    sq
2513b 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
2513c 61 72 73 65 2c 22 4c 49 4d 49 54 20 63 6c 61 75  arse,"LIMIT clau
2513d 73 65 20 73 68 6f 75 6c 64 20 63 6f 6d 65 20 61  se should come a
2513e 66 74 65 72 20 25 73 20 6e 6f 74 20 62 65 66 6f  fter %s not befo
2513f 72 65 22 2c 0a 20 20 20 20 20 20 73 65 6c 65 63  re",.      selec
25140 74 4f 70 4e 61 6d 65 28 70 2d 3e 6f 70 29 29 3b  tOpName(p->op));
25141 0a 20 20 20 20 72 63 20 3d 20 31 3b 0a 20 20 20  .    rc = 1;.   
25142 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
25143 63 74 5f 65 6e 64 3b 0a 20 20 7d 0a 0a 20 20 76  ct_end;.  }..  v
25144 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64 62   = sqlite3GetVdb
25145 65 28 70 50 61 72 73 65 29 3b 0a 20 20 61 73 73  e(pParse);.  ass
25146 65 72 74 28 20 76 21 3d 30 20 29 3b 20 20 2f 2a  ert( v!=0 );  /*
25147 20 54 68 65 20 56 44 42 45 20 61 6c 72 65 61 64   The VDBE alread
25148 79 20 63 72 65 61 74 65 64 20 62 79 20 63 61 6c  y created by cal
25149 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 2a 2f  ling function */
2514a 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 20 74 68  ..  /* Create th
2514b 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 74 65  e destination te
2514c 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 66  mporary table if
2514d 20 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2f 0a   necessary.  */.
2514e 20 20 69 66 28 20 64 65 73 74 2e 65 44 65 73 74    if( dest.eDest
2514f 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29  ==SRT_EphemTab )
25150 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 2d  {.    assert( p-
25151 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 73  >pEList );.    s
25152 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25153 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
25154 65 72 61 6c 2c 20 64 65 73 74 2e 69 50 61 72 6d  eral, dest.iParm
25155 2c 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  , p->pEList->nEx
25156 70 72 29 3b 0a 20 20 20 20 64 65 73 74 2e 65 44  pr);.    dest.eD
25157 65 73 74 20 3d 20 53 52 54 5f 54 61 62 6c 65 3b  est = SRT_Table;
25158 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20  .  }..  /* Make 
25159 73 75 72 65 20 61 6c 6c 20 53 45 4c 45 43 54 73  sure all SELECTs
2515a 20 69 6e 20 74 68 65 20 73 74 61 74 65 6d 65 6e   in the statemen
2515b 74 20 68 61 76 65 20 74 68 65 20 73 61 6d 65 20  t have the same 
2515c 6e 75 6d 62 65 72 20 6f 66 20 65 6c 65 6d 65 6e  number of elemen
2515d 74 73 0a 20 20 2a 2a 20 69 6e 20 74 68 65 69 72  ts.  ** in their
2515e 20 72 65 73 75 6c 74 20 73 65 74 73 2e 0a 20 20   result sets..  
2515f 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  */.  assert( p->
25160 70 45 4c 69 73 74 20 26 26 20 70 50 72 69 6f 72  pEList && pPrior
25161 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20 20 69 66  ->pEList );.  if
25162 28 20 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  ( p->pEList->nEx
25163 70 72 21 3d 70 50 72 69 6f 72 2d 3e 70 45 4c 69  pr!=pPrior->pELi
25164 73 74 2d 3e 6e 45 78 70 72 20 29 7b 0a 20 20 20  st->nExpr ){.   
25165 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
25166 28 70 50 61 72 73 65 2c 20 22 53 45 4c 45 43 54  (pParse, "SELECT
25167 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 61 6e  s to the left an
25168 64 20 72 69 67 68 74 20 6f 66 20 25 73 22 0a 20  d right of %s". 
25169 20 20 20 20 20 22 20 64 6f 20 6e 6f 74 20 68 61       " do not ha
2516a 76 65 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62  ve the same numb
2516b 65 72 20 6f 66 20 72 65 73 75 6c 74 20 63 6f 6c  er of result col
2516c 75 6d 6e 73 22 2c 20 73 65 6c 65 63 74 4f 70 4e  umns", selectOpN
2516d 61 6d 65 28 70 2d 3e 6f 70 29 29 3b 0a 20 20 20  ame(p->op));.   
2516e 20 72 63 20 3d 20 31 3b 0a 20 20 20 20 67 6f 74   rc = 1;.    got
2516f 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65  o multi_select_e
25170 6e 64 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f  nd;.  }..  /* Co
25171 6d 70 6f 75 6e 64 20 53 45 4c 45 43 54 73 20 74  mpound SELECTs t
25172 68 61 74 20 68 61 76 65 20 61 6e 20 4f 52 44 45  hat have an ORDE
25173 52 20 42 59 20 63 6c 61 75 73 65 20 61 72 65 20  R BY clause are 
25174 68 61 6e 64 6c 65 64 20 73 65 70 61 72 61 74 65  handled separate
25175 6c 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ly..  */.  if( p
25176 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
25177 20 20 72 65 74 75 72 6e 20 6d 75 6c 74 69 53 65    return multiSe
25178 6c 65 63 74 4f 72 64 65 72 42 79 28 70 50 61 72  lectOrderBy(pPar
25179 73 65 2c 20 70 2c 20 70 44 65 73 74 29 3b 0a 20  se, p, pDest);. 
2517a 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74   }..  /* Generat
2517b 65 20 63 6f 64 65 20 66 6f 72 20 74 68 65 20 6c  e code for the l
2517c 65 66 74 20 61 6e 64 20 72 69 67 68 74 20 53 45  eft and right SE
2517d 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e  LECT statements.
2517e 0a 20 20 2a 2f 0a 20 20 73 77 69 74 63 68 28 20  .  */.  switch( 
2517f 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 63 61 73  p->op ){.    cas
25180 65 20 54 4b 5f 41 4c 4c 3a 20 7b 0a 20 20 20 20  e TK_ALL: {.    
25181 20 20 69 6e 74 20 61 64 64 72 20 3d 20 30 3b 0a    int addr = 0;.
25182 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21 70        assert( !p
25183 50 72 69 6f 72 2d 3e 70 4c 69 6d 69 74 20 29 3b  Prior->pLimit );
25184 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d 3e 70  .      pPrior->p
25185 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c 69 6d 69  Limit = p->pLimi
25186 74 3b 0a 20 20 20 20 20 20 70 50 72 69 6f 72 2d  t;.      pPrior-
25187 3e 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f  >pOffset = p->pO
25188 66 66 73 65 74 3b 0a 20 20 20 20 20 20 72 63 20  ffset;.      rc 
25189 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28  = sqlite3Select(
2518a 70 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20  pParse, pPrior, 
2518b 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20 70 2d  &dest);.      p-
2518c 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20  >pLimit = 0;.   
2518d 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
2518e 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 20  0;.      if( rc 
2518f 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f 20  ){.        goto 
25190 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64  multi_select_end
25191 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
25192 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20  p->pPrior = 0;. 
25193 20 20 20 20 20 70 2d 3e 69 4c 69 6d 69 74 20 3d       p->iLimit =
25194 20 70 50 72 69 6f 72 2d 3e 69 4c 69 6d 69 74 3b   pPrior->iLimit;
25195 0a 20 20 20 20 20 20 70 2d 3e 69 4f 66 66 73 65  .      p->iOffse
25196 74 20 3d 20 70 50 72 69 6f 72 2d 3e 69 4f 66 66  t = pPrior->iOff
25197 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28 20 70  set;.      if( p
25198 2d 3e 69 4c 69 6d 69 74 20 29 7b 0a 20 20 20 20  ->iLimit ){.    
25199 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
2519a 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
2519b 4f 50 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c  OP_IfZero, p->iL
2519c 69 6d 69 74 29 3b 0a 20 20 20 20 20 20 20 20 56  imit);.        V
2519d 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
2519e 4a 75 6d 70 20 61 68 65 61 64 20 69 66 20 4c 49  Jump ahead if LI
2519f 4d 49 54 20 72 65 61 63 68 65 64 22 29 29 3b 0a  MIT reached"));.
251a0 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63        }.      rc
251a1 20 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74   = sqlite3Select
251a2 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64 65 73  (pParse, p, &des
251a3 74 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74  t);.      pDelet
251a4 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  e = p->pPrior;. 
251a5 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
251a6 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 69   pPrior;.      i
251a7 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20  f( rc ){.       
251a8 20 67 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65   goto multi_sele
251a9 63 74 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a  ct_end;.      }.
251aa 20 20 20 20 20 20 69 66 28 20 61 64 64 72 20 29        if( addr )
251ab 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
251ac 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
251ad 20 61 64 64 72 29 3b 0a 20 20 20 20 20 20 7d 0a   addr);.      }.
251ae 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
251af 20 7d 0a 20 20 20 20 63 61 73 65 20 54 4b 5f 45   }.    case TK_E
251b0 58 43 45 50 54 3a 0a 20 20 20 20 63 61 73 65 20  XCEPT:.    case 
251b1 54 4b 5f 55 4e 49 4f 4e 3a 20 7b 0a 20 20 20 20  TK_UNION: {.    
251b2 20 20 69 6e 74 20 75 6e 69 6f 6e 54 61 62 3b 20    int unionTab; 
251b3 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75 6d     /* Cursor num
251b4 62 65 72 20 6f 66 20 74 68 65 20 74 65 6d 70 6f  ber of the tempo
251b5 72 61 72 79 20 74 61 62 6c 65 20 68 6f 6c 64 69  rary table holdi
251b6 6e 67 20 72 65 73 75 6c 74 20 2a 2f 0a 20 20 20  ng result */.   
251b7 20 20 20 69 6e 74 20 6f 70 20 3d 20 30 3b 20 20     int op = 0;  
251b8 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 74 68      /* One of th
251b9 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69 6f 6e  e SRT_ operation
251ba 73 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 73 65  s to apply to se
251bb 6c 66 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  lf */.      int 
251bc 70 72 69 6f 72 4f 70 3b 20 20 20 20 20 2f 2a 20  priorOp;     /* 
251bd 54 68 65 20 53 52 54 5f 20 6f 70 65 72 61 74 69  The SRT_ operati
251be 6f 6e 20 74 6f 20 61 70 70 6c 79 20 74 6f 20 70  on to apply to p
251bf 72 69 6f 72 20 73 65 6c 65 63 74 73 20 2a 2f 0a  rior selects */.
251c0 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d        Expr *pLim
251c1 69 74 2c 20 2a 70 4f 66 66 73 65 74 3b 20 2f 2a  it, *pOffset; /*
251c2 20 53 61 76 65 64 20 76 61 6c 75 65 73 20 6f 66   Saved values of
251c3 20 70 2d 3e 6e 4c 69 6d 69 74 20 61 6e 64 20 70   p->nLimit and p
251c4 2d 3e 6e 4f 66 66 73 65 74 20 2a 2f 0a 20 20 20  ->nOffset */.   
251c5 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
251c6 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 75 6e     SelectDest un
251c7 69 6f 6e 64 65 73 74 3b 0a 0a 20 20 20 20 20 20  iondest;..      
251c8 70 72 69 6f 72 4f 70 20 3d 20 53 52 54 5f 55 6e  priorOp = SRT_Un
251c9 69 6f 6e 3b 0a 20 20 20 20 20 20 69 66 28 20 64  ion;.      if( d
251ca 65 73 74 2e 65 44 65 73 74 3d 3d 70 72 69 6f 72  est.eDest==prior
251cb 4f 70 20 26 26 20 21 70 2d 3e 70 4c 69 6d 69 74  Op && !p->pLimit
251cc 20 26 26 20 21 70 2d 3e 70 4f 66 66 73 65 74 20   && !p->pOffset 
251cd 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65  ){.        /* We
251ce 20 63 61 6e 20 72 65 75 73 65 20 61 20 74 65 6d   can reuse a tem
251cf 70 6f 72 61 72 79 20 74 61 62 6c 65 20 67 65 6e  porary table gen
251d0 65 72 61 74 65 64 20 62 79 20 61 20 53 45 4c 45  erated by a SELE
251d1 43 54 20 74 6f 20 6f 75 72 0a 20 20 20 20 20 20  CT to our.      
251d2 20 20 2a 2a 20 72 69 67 68 74 2e 0a 20 20 20 20    ** right..    
251d3 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75      */.        u
251d4 6e 69 6f 6e 54 61 62 20 3d 20 64 65 73 74 2e 69  nionTab = dest.i
251d5 50 61 72 6d 3b 0a 20 20 20 20 20 20 7d 65 6c 73  Parm;.      }els
251d6 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 57 65  e{.        /* We
251d7 20 77 69 6c 6c 20 6e 65 65 64 20 74 6f 20 63 72   will need to cr
251d8 65 61 74 65 20 6f 75 72 20 6f 77 6e 20 74 65 6d  eate our own tem
251d9 70 6f 72 61 72 79 20 74 61 62 6c 65 20 74 6f 20  porary table to 
251da 68 6f 6c 64 20 74 68 65 0a 20 20 20 20 20 20 20  hold the.       
251db 20 2a 2a 20 69 6e 74 65 72 6d 65 64 69 61 74 65   ** intermediate
251dc 20 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 20 20   results..      
251dd 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 75 6e 69    */.        uni
251de 6f 6e 54 61 62 20 3d 20 70 50 61 72 73 65 2d 3e  onTab = pParse->
251df 6e 54 61 62 2b 2b 3b 0a 20 20 20 20 20 20 20 20  nTab++;.        
251e0 61 73 73 65 72 74 28 20 70 2d 3e 70 4f 72 64 65  assert( p->pOrde
251e1 72 42 79 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20  rBy==0 );.      
251e2 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
251e3 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
251e4 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20  _OpenEphemeral, 
251e5 75 6e 69 6f 6e 54 61 62 2c 20 30 29 3b 0a 20 20  unionTab, 0);.  
251e6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
251e7 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 30 5d  >addrOpenEphm[0]
251e8 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20 20 20   == -1 );.      
251e9 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
251ea 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  m[0] = addr;.   
251eb 20 20 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f       p->pRightmo
251ec 73 74 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20  st->selFlags |= 
251ed 53 46 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c  SF_UsesEphemeral
251ee 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
251ef 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a 20  ( p->pEList );. 
251f0 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
251f1 20 43 6f 64 65 20 74 68 65 20 53 45 4c 45 43 54   Code the SELECT
251f2 20 73 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 6f   statements to o
251f3 75 72 20 6c 65 66 74 0a 20 20 20 20 20 20 2a 2f  ur left.      */
251f4 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 21  .      assert( !
251f5 70 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79  pPrior->pOrderBy
251f6 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
251f7 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74 28  3SelectDestInit(
251f8 26 75 6e 69 6f 6e 64 65 73 74 2c 20 70 72 69 6f  &uniondest, prio
251f9 72 4f 70 2c 20 75 6e 69 6f 6e 54 61 62 29 3b 0a  rOp, unionTab);.
251fa 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
251fb 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c  e3Select(pParse,
251fc 20 70 50 72 69 6f 72 2c 20 26 75 6e 69 6f 6e 64   pPrior, &uniond
251fd 65 73 74 29 3b 0a 20 20 20 20 20 20 69 66 28 20  est);.      if( 
251fe 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f  rc ){.        go
251ff 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f  to multi_select_
25200 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20  end;.      }..  
25201 20 20 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20      /* Code the 
25202 63 75 72 72 65 6e 74 20 53 45 4c 45 43 54 20 73  current SELECT s
25203 74 61 74 65 6d 65 6e 74 0a 20 20 20 20 20 20 2a  tatement.      *
25204 2f 0a 20 20 20 20 20 20 69 66 28 20 70 2d 3e 6f  /.      if( p->o
25205 70 3d 3d 54 4b 5f 45 58 43 45 50 54 20 29 7b 0a  p==TK_EXCEPT ){.
25206 20 20 20 20 20 20 20 20 6f 70 20 3d 20 53 52 54          op = SRT
25207 5f 45 78 63 65 70 74 3b 0a 20 20 20 20 20 20 7d  _Except;.      }
25208 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 61 73  else{.        as
25209 73 65 72 74 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f  sert( p->op==TK_
2520a 55 4e 49 4f 4e 20 29 3b 0a 20 20 20 20 20 20 20  UNION );.       
2520b 20 6f 70 20 3d 20 53 52 54 5f 55 6e 69 6f 6e 3b   op = SRT_Union;
2520c 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70  .      }.      p
2520d 2d 3e 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20  ->pPrior = 0;.  
2520e 20 20 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e      pLimit = p->
2520f 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d  pLimit;.      p-
25210 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20  >pLimit = 0;.   
25211 20 20 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e     pOffset = p->
25212 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
25213 2d 3e 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20  ->pOffset = 0;. 
25214 20 20 20 20 20 75 6e 69 6f 6e 64 65 73 74 2e 65       uniondest.e
25215 44 65 73 74 20 3d 20 6f 70 3b 0a 20 20 20 20 20  Dest = op;.     
25216 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53 65 6c   rc = sqlite3Sel
25217 65 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26  ect(pParse, p, &
25218 75 6e 69 6f 6e 64 65 73 74 29 3b 0a 20 20 20 20  uniondest);.    
25219 20 20 2f 2a 20 51 75 65 72 79 20 66 6c 61 74 74    /* Query flatt
2521a 65 6e 69 6e 67 20 69 6e 20 73 71 6c 69 74 65 33  ening in sqlite3
2521b 53 65 6c 65 63 74 28 29 20 6d 69 67 68 74 20 72  Select() might r
2521c 65 66 69 6c 6c 20 70 2d 3e 70 4f 72 64 65 72 42  efill p->pOrderB
2521d 79 2e 0a 20 20 20 20 20 20 2a 2a 20 42 65 20 73  y..      ** Be s
2521e 75 72 65 20 74 6f 20 64 65 6c 65 74 65 20 70 2d  ure to delete p-
2521f 3e 70 4f 72 64 65 72 42 79 2c 20 74 68 65 72 65  >pOrderBy, there
25220 66 6f 72 65 2c 20 74 6f 20 61 76 6f 69 64 20 61  fore, to avoid a
25221 20 6d 65 6d 6f 72 79 20 6c 65 61 6b 2e 20 2a 2f   memory leak. */
25222 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
25223 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
25224 20 70 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20   p->pOrderBy);. 
25225 20 20 20 20 20 70 44 65 6c 65 74 65 20 3d 20 70       pDelete = p
25226 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20  ->pPrior;.      
25227 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 50 72 69  p->pPrior = pPri
25228 6f 72 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4f 72  or;.      p->pOr
25229 64 65 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 20  derBy = 0;.     
2522a 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2522b 74 65 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74  te(db, p->pLimit
2522c 29 3b 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d  );.      p->pLim
2522d 69 74 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20  it = pLimit;.   
2522e 20 20 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20     p->pOffset = 
2522f 70 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70  pOffset;.      p
25230 2d 3e 69 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20  ->iLimit = 0;.  
25231 20 20 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d      p->iOffset =
25232 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63   0;.      if( rc
25233 20 29 7b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f   ){.        goto
25234 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74 5f 65 6e   multi_select_en
25235 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 0a 20 20 20  d;.      }...   
25236 20 20 20 2f 2a 20 43 6f 6e 76 65 72 74 20 74 68     /* Convert th
25237 65 20 64 61 74 61 20 69 6e 20 74 68 65 20 74 65  e data in the te
25238 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 69 6e  mporary table in
25239 74 6f 20 77 68 61 74 65 76 65 72 20 66 6f 72 6d  to whatever form
2523a 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69 73 20  .      ** it is 
2523b 74 68 61 74 20 77 65 20 63 75 72 72 65 6e 74 6c  that we currentl
2523c 79 20 6e 65 65 64 2e 0a 20 20 20 20 20 20 2a 2f  y need..      */
2523d 20 20 20 20 20 20 0a 20 20 20 20 20 20 69 66 28        .      if(
2523e 20 64 65 73 74 2e 65 44 65 73 74 21 3d 70 72 69   dest.eDest!=pri
2523f 6f 72 4f 70 20 7c 7c 20 75 6e 69 6f 6e 54 61 62  orOp || unionTab
25240 21 3d 64 65 73 74 2e 69 50 61 72 6d 20 29 7b 0a  !=dest.iParm ){.
25241 20 20 20 20 20 20 20 20 69 6e 74 20 69 43 6f 6e          int iCon
25242 74 2c 20 69 42 72 65 61 6b 2c 20 69 53 74 61 72  t, iBreak, iStar
25243 74 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  t;.        asser
25244 74 28 20 70 2d 3e 70 45 4c 69 73 74 20 29 3b 0a  t( p->pEList );.
25245 20 20 20 20 20 20 20 20 69 66 28 20 64 65 73 74          if( dest
25246 2e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70  .eDest==SRT_Outp
25247 75 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ut ){.          
25248 53 65 6c 65 63 74 20 2a 70 46 69 72 73 74 20 3d  Select *pFirst =
25249 20 70 3b 0a 20 20 20 20 20 20 20 20 20 20 77 68   p;.          wh
2524a 69 6c 65 28 20 70 46 69 72 73 74 2d 3e 70 50 72  ile( pFirst->pPr
2524b 69 6f 72 20 29 20 70 46 69 72 73 74 20 3d 20 70  ior ) pFirst = p
2524c 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 3b 0a 20  First->pPrior;. 
2524d 20 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74           generat
2524e 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50 61  eColumnNames(pPa
2524f 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d 3e  rse, 0, pFirst->
25250 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 20  pEList);.       
25251 20 7d 0a 20 20 20 20 20 20 20 20 69 42 72 65 61   }.        iBrea
25252 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  k = sqlite3VdbeM
25253 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20  akeLabel(v);.   
25254 20 20 20 20 20 69 43 6f 6e 74 20 3d 20 73 71 6c       iCont = sql
25255 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65  ite3VdbeMakeLabe
25256 6c 28 76 29 3b 0a 20 20 20 20 20 20 20 20 63 6f  l(v);.        co
25257 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73 74  mputeLimitRegist
25258 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20 69  ers(pParse, p, i
25259 42 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 20 20  Break);.        
2525a 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2525b 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c 20  2(v, OP_Rewind, 
2525c 75 6e 69 6f 6e 54 61 62 2c 20 69 42 72 65 61 6b  unionTab, iBreak
2525d 29 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72  );.        iStar
2525e 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  t = sqlite3VdbeC
2525f 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
25260 20 20 20 20 20 20 20 73 65 6c 65 63 74 49 6e 6e         selectInn
25261 65 72 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70  erLoop(pParse, p
25262 2c 20 70 2d 3e 70 45 4c 69 73 74 2c 20 75 6e 69  , p->pEList, uni
25263 6f 6e 54 61 62 2c 20 70 2d 3e 70 45 4c 69 73 74  onTab, p->pEList
25264 2d 3e 6e 45 78 70 72 2c 0a 20 20 20 20 20 20 20  ->nExpr,.       
25265 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25266 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c 20 69   0, -1, &dest, i
25267 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20  Cont, iBreak);. 
25268 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
25269 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2526a 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20  , iCont);.      
2526b 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2526c 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
2526d 75 6e 69 6f 6e 54 61 62 2c 20 69 53 74 61 72 74  unionTab, iStart
2526e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2526f 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
25270 65 6c 28 76 2c 20 69 42 72 65 61 6b 29 3b 0a 20  el(v, iBreak);. 
25271 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
25272 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
25273 6c 6f 73 65 2c 20 75 6e 69 6f 6e 54 61 62 2c 20  lose, unionTab, 
25274 30 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  0);.      }.    
25275 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
25276 20 20 20 63 61 73 65 20 54 4b 5f 49 4e 54 45 52     case TK_INTER
25277 53 45 43 54 3a 20 7b 0a 20 20 20 20 20 20 69 6e  SECT: {.      in
25278 74 20 74 61 62 31 2c 20 74 61 62 32 3b 0a 20 20  t tab1, tab2;.  
25279 20 20 20 20 69 6e 74 20 69 43 6f 6e 74 2c 20 69      int iCont, i
2527a 42 72 65 61 6b 2c 20 69 53 74 61 72 74 3b 0a 20  Break, iStart;. 
2527b 20 20 20 20 20 45 78 70 72 20 2a 70 4c 69 6d 69       Expr *pLimi
2527c 74 2c 20 2a 70 4f 66 66 73 65 74 3b 0a 20 20 20  t, *pOffset;.   
2527d 20 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20     int addr;.   
2527e 20 20 20 53 65 6c 65 63 74 44 65 73 74 20 69 6e     SelectDest in
2527f 74 65 72 73 65 63 74 64 65 73 74 3b 0a 20 20 20  tersectdest;.   
25280 20 20 20 69 6e 74 20 72 31 3b 0a 0a 20 20 20 20     int r1;..    
25281 20 20 2f 2a 20 49 4e 54 45 52 53 45 43 54 20 69    /* INTERSECT i
25282 73 20 64 69 66 66 65 72 65 6e 74 20 66 72 6f 6d  s different from
25283 20 74 68 65 20 6f 74 68 65 72 73 20 73 69 6e 63   the others sinc
25284 65 20 69 74 20 72 65 71 75 69 72 65 73 0a 20 20  e it requires.  
25285 20 20 20 20 2a 2a 20 74 77 6f 20 74 65 6d 70 6f      ** two tempo
25286 72 61 72 79 20 74 61 62 6c 65 73 2e 20 20 48 65  rary tables.  He
25287 6e 63 65 20 69 74 20 68 61 73 20 69 74 73 20 6f  nce it has its o
25288 77 6e 20 63 61 73 65 2e 20 20 42 65 67 69 6e 0a  wn case.  Begin.
25289 20 20 20 20 20 20 2a 2a 20 62 79 20 61 6c 6c 6f        ** by allo
2528a 63 61 74 69 6e 67 20 74 68 65 20 74 61 62 6c 65  cating the table
2528b 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64 2e 0a  s we will need..
2528c 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 74        */.      t
2528d 61 62 31 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54  ab1 = pParse->nT
2528e 61 62 2b 2b 3b 0a 20 20 20 20 20 20 74 61 62 32  ab++;.      tab2
2528f 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b   = pParse->nTab+
25290 2b 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  +;.      assert(
25291 20 70 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20   p->pOrderBy==0 
25292 29 3b 0a 0a 20 20 20 20 20 20 61 64 64 72 20 3d  );..      addr =
25293 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25294 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68  p2(v, OP_OpenEph
25295 65 6d 65 72 61 6c 2c 20 74 61 62 31 2c 20 30 29  emeral, tab1, 0)
25296 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
25297 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b  p->addrOpenEphm[
25298 30 5d 20 3d 3d 20 2d 31 20 29 3b 0a 20 20 20 20  0] == -1 );.    
25299 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45 70 68    p->addrOpenEph
2529a 6d 5b 30 5d 20 3d 20 61 64 64 72 3b 0a 20 20 20  m[0] = addr;.   
2529b 20 20 20 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74     p->pRightmost
2529c 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
2529d 5f 55 73 65 73 45 70 68 65 6d 65 72 61 6c 3b 0a  _UsesEphemeral;.
2529e 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 2d        assert( p-
2529f 3e 70 45 4c 69 73 74 20 29 3b 0a 0a 20 20 20 20  >pEList );..    
252a0 20 20 2f 2a 20 43 6f 64 65 20 74 68 65 20 53 45    /* Code the SE
252a1 4c 45 43 54 73 20 74 6f 20 6f 75 72 20 6c 65 66  LECTs to our lef
252a2 74 20 69 6e 74 6f 20 74 65 6d 70 6f 72 61 72 79  t into temporary
252a3 20 74 61 62 6c 65 20 22 74 61 62 31 22 2e 0a 20   table "tab1".. 
252a4 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 73 71       */.      sq
252a5 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
252a6 6e 69 74 28 26 69 6e 74 65 72 73 65 63 74 64 65  nit(&intersectde
252a7 73 74 2c 20 53 52 54 5f 55 6e 69 6f 6e 2c 20 74  st, SRT_Union, t
252a8 61 62 31 29 3b 0a 20 20 20 20 20 20 72 63 20 3d  ab1);.      rc =
252a9 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 70   sqlite3Select(p
252aa 50 61 72 73 65 2c 20 70 50 72 69 6f 72 2c 20 26  Parse, pPrior, &
252ab 69 6e 74 65 72 73 65 63 74 64 65 73 74 29 3b 0a  intersectdest);.
252ac 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b 0a        if( rc ){.
252ad 20 20 20 20 20 20 20 20 67 6f 74 6f 20 6d 75 6c          goto mul
252ae 74 69 5f 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  ti_select_end;. 
252af 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a       }..      /*
252b0 20 43 6f 64 65 20 74 68 65 20 63 75 72 72 65 6e   Code the curren
252b1 74 20 53 45 4c 45 43 54 20 69 6e 74 6f 20 74 65  t SELECT into te
252b2 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 22 74  mporary table "t
252b3 61 62 32 22 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ab2".      */.  
252b4 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
252b5 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
252b6 4f 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c  OP_OpenEphemeral
252b7 2c 20 74 61 62 32 2c 20 30 29 3b 0a 20 20 20 20  , tab2, 0);.    
252b8 20 20 61 73 73 65 72 74 28 20 70 2d 3e 61 64 64    assert( p->add
252b9 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d 3d 20  rOpenEphm[1] == 
252ba 2d 31 20 29 3b 0a 20 20 20 20 20 20 70 2d 3e 61  -1 );.      p->a
252bb 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d 20 3d  ddrOpenEphm[1] =
252bc 20 61 64 64 72 3b 0a 20 20 20 20 20 20 70 2d 3e   addr;.      p->
252bd 70 50 72 69 6f 72 20 3d 20 30 3b 0a 20 20 20 20  pPrior = 0;.    
252be 20 20 70 4c 69 6d 69 74 20 3d 20 70 2d 3e 70 4c    pLimit = p->pL
252bf 69 6d 69 74 3b 0a 20 20 20 20 20 20 70 2d 3e 70  imit;.      p->p
252c0 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20 20  Limit = 0;.     
252c1 20 70 4f 66 66 73 65 74 20 3d 20 70 2d 3e 70 4f   pOffset = p->pO
252c2 66 66 73 65 74 3b 0a 20 20 20 20 20 20 70 2d 3e  ffset;.      p->
252c3 70 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20 20  pOffset = 0;.   
252c4 20 20 20 69 6e 74 65 72 73 65 63 74 64 65 73 74     intersectdest
252c5 2e 69 50 61 72 6d 20 3d 20 74 61 62 32 3b 0a 20  .iParm = tab2;. 
252c6 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
252c7 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
252c8 70 2c 20 26 69 6e 74 65 72 73 65 63 74 64 65 73  p, &intersectdes
252c9 74 29 3b 0a 20 20 20 20 20 20 70 44 65 6c 65 74  t);.      pDelet
252ca 65 20 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20  e = p->pPrior;. 
252cb 20 20 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d       p->pPrior =
252cc 20 70 50 72 69 6f 72 3b 0a 20 20 20 20 20 20 73   pPrior;.      s
252cd 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
252ce 28 64 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b  (db, p->pLimit);
252cf 0a 20 20 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74  .      p->pLimit
252d0 20 3d 20 70 4c 69 6d 69 74 3b 0a 20 20 20 20 20   = pLimit;.     
252d1 20 70 2d 3e 70 4f 66 66 73 65 74 20 3d 20 70 4f   p->pOffset = pO
252d2 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28  ffset;.      if(
252d3 20 72 63 20 29 7b 0a 20 20 20 20 20 20 20 20 67   rc ){.        g
252d4 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
252d5 5f 65 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  _end;.      }.. 
252d6 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
252d7 20 63 6f 64 65 20 74 6f 20 74 61 6b 65 20 74 68   code to take th
252d8 65 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6f  e intersection o
252d9 66 20 74 68 65 20 74 77 6f 20 74 65 6d 70 6f 72  f the two tempor
252da 61 72 79 0a 20 20 20 20 20 20 2a 2a 20 74 61 62  ary.      ** tab
252db 6c 65 73 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  les..      */.  
252dc 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
252dd 45 4c 69 73 74 20 29 3b 0a 20 20 20 20 20 20 69  EList );.      i
252de 66 28 20 64 65 73 74 2e 65 44 65 73 74 3d 3d 53  f( dest.eDest==S
252df 52 54 5f 4f 75 74 70 75 74 20 29 7b 0a 20 20 20  RT_Output ){.   
252e0 20 20 20 20 20 53 65 6c 65 63 74 20 2a 70 46 69       Select *pFi
252e1 72 73 74 20 3d 20 70 3b 0a 20 20 20 20 20 20 20  rst = p;.       
252e2 20 77 68 69 6c 65 28 20 70 46 69 72 73 74 2d 3e   while( pFirst->
252e3 70 50 72 69 6f 72 20 29 20 70 46 69 72 73 74 20  pPrior ) pFirst 
252e4 3d 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72  = pFirst->pPrior
252e5 3b 0a 20 20 20 20 20 20 20 20 67 65 6e 65 72 61  ;.        genera
252e6 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
252e7 61 72 73 65 2c 20 30 2c 20 70 46 69 72 73 74 2d  arse, 0, pFirst-
252e8 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20  >pEList);.      
252e9 7d 0a 20 20 20 20 20 20 69 42 72 65 61 6b 20 3d  }.      iBreak =
252ea 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65   sqlite3VdbeMake
252eb 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20  Label(v);.      
252ec 69 43 6f 6e 74 20 3d 20 73 71 6c 69 74 65 33 56  iCont = sqlite3V
252ed 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
252ee 0a 20 20 20 20 20 20 63 6f 6d 70 75 74 65 4c 69  .      computeLi
252ef 6d 69 74 52 65 67 69 73 74 65 72 73 28 70 50 61  mitRegisters(pPa
252f0 72 73 65 2c 20 70 2c 20 69 42 72 65 61 6b 29 3b  rse, p, iBreak);
252f1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
252f2 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
252f3 65 77 69 6e 64 2c 20 74 61 62 31 2c 20 69 42 72  ewind, tab1, iBr
252f4 65 61 6b 29 3b 0a 20 20 20 20 20 20 72 31 20 3d  eak);.      r1 =
252f5 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
252f6 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
252f7 20 20 69 53 74 61 72 74 20 3d 20 73 71 6c 69 74    iStart = sqlit
252f8 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
252f9 4f 50 5f 52 6f 77 4b 65 79 2c 20 74 61 62 31 2c  OP_RowKey, tab1,
252fa 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69   r1);.      sqli
252fb 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
252fc 20 4f 50 5f 4e 6f 74 46 6f 75 6e 64 2c 20 74 61   OP_NotFound, ta
252fd 62 32 2c 20 69 43 6f 6e 74 2c 20 72 31 29 3b 0a  b2, iCont, r1);.
252fe 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
252ff 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
25300 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73  se, r1);.      s
25301 65 6c 65 63 74 49 6e 6e 65 72 4c 6f 6f 70 28 70  electInnerLoop(p
25302 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 45 4c  Parse, p, p->pEL
25303 69 73 74 2c 20 74 61 62 31 2c 20 70 2d 3e 70 45  ist, tab1, p->pE
25304 4c 69 73 74 2d 3e 6e 45 78 70 72 2c 0a 20 20 20  List->nExpr,.   
25305 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25306 20 20 20 30 2c 20 2d 31 2c 20 26 64 65 73 74 2c     0, -1, &dest,
25307 20 69 43 6f 6e 74 2c 20 69 42 72 65 61 6b 29 3b   iCont, iBreak);
25308 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
25309 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76  beResolveLabel(v
2530a 2c 20 69 43 6f 6e 74 29 3b 0a 20 20 20 20 20 20  , iCont);.      
2530b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2530c 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 74 61  2(v, OP_Next, ta
2530d 62 31 2c 20 69 53 74 61 72 74 29 3b 0a 20 20 20  b1, iStart);.   
2530e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2530f 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 69 42  solveLabel(v, iB
25310 72 65 61 6b 29 3b 0a 20 20 20 20 20 20 73 71 6c  reak);.      sql
25311 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
25312 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 32  , OP_Close, tab2
25313 2c 20 30 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  , 0);.      sqli
25314 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25315 20 4f 50 5f 43 6c 6f 73 65 2c 20 74 61 62 31 2c   OP_Close, tab1,
25316 20 30 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b   0);.      break
25317 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25318 2a 20 43 6f 6d 70 75 74 65 20 63 6f 6c 6c 61 74  * Compute collat
25319 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20 75 73  ing sequences us
2531a 65 64 20 62 79 20 0a 20 20 2a 2a 20 74 65 6d 70  ed by .  ** temp
2531b 6f 72 61 72 79 20 74 61 62 6c 65 73 20 6e 65 65  orary tables nee
2531c 64 65 64 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74  ded to implement
2531d 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 73 65   the compound se
2531e 6c 65 63 74 2e 0a 20 20 2a 2a 20 41 74 74 61 63  lect..  ** Attac
2531f 68 20 74 68 65 20 4b 65 79 49 6e 66 6f 20 73 74  h the KeyInfo st
25320 72 75 63 74 75 72 65 20 74 6f 20 61 6c 6c 20 74  ructure to all t
25321 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
25322 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69 73 20  .  **.  ** This 
25323 73 65 63 74 69 6f 6e 20 69 73 20 72 75 6e 20 62  section is run b
25324 79 20 74 68 65 20 72 69 67 68 74 2d 6d 6f 73 74  y the right-most
25325 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e   SELECT statemen
25326 74 20 6f 6e 6c 79 2e 0a 20 20 2a 2a 20 53 45 4c  t only..  ** SEL
25327 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 20 74  ECT statements t
25328 6f 20 74 68 65 20 6c 65 66 74 20 61 6c 77 61 79  o the left alway
25329 73 20 73 6b 69 70 20 74 68 69 73 20 70 61 72 74  s skip this part
2532a 2e 20 20 54 68 65 20 72 69 67 68 74 2d 6d 6f 73  .  The right-mos
2532b 74 0a 20 20 2a 2a 20 53 45 4c 45 43 54 20 6d 69  t.  ** SELECT mi
2532c 67 68 74 20 61 6c 73 6f 20 73 6b 69 70 20 74 68  ght also skip th
2532d 69 73 20 70 61 72 74 20 69 66 20 69 74 20 68 61  is part if it ha
2532e 73 20 6e 6f 20 4f 52 44 45 52 20 42 59 20 63 6c  s no ORDER BY cl
2532f 61 75 73 65 20 61 6e 64 0a 20 20 2a 2a 20 6e 6f  ause and.  ** no
25330 20 74 65 6d 70 20 74 61 62 6c 65 73 20 61 72 65   temp tables are
25331 20 72 65 71 75 69 72 65 64 2e 0a 20 20 2a 2f 0a   required..  */.
25332 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61 67    if( p->selFlag
25333 73 20 26 20 53 46 5f 55 73 65 73 45 70 68 65 6d  s & SF_UsesEphem
25334 65 72 61 6c 20 29 7b 0a 20 20 20 20 69 6e 74 20  eral ){.    int 
25335 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  i;              
25336 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
25337 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
25338 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e   KeyInfo *pKeyIn
25339 66 6f 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  fo;            /
2533a 2a 20 43 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75  * Collating sequ
2533b 65 6e 63 65 20 66 6f 72 20 74 68 65 20 72 65 73  ence for the res
2533c 75 6c 74 20 73 65 74 20 2a 2f 0a 20 20 20 20 53  ult set */.    S
2533d 65 6c 65 63 74 20 2a 70 4c 6f 6f 70 3b 20 20 20  elect *pLoop;   
2533e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2533f 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f  For looping thro
25340 75 67 68 20 53 45 4c 45 43 54 20 73 74 61 74 65  ugh SELECT state
25341 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 43 6f 6c  ments */.    Col
25342 6c 53 65 71 20 2a 2a 61 70 43 6f 6c 6c 3b 20 20  lSeq **apColl;  
25343 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f             /* Fo
25344 72 20 6c 6f 6f 70 69 6e 67 20 74 68 72 6f 75 67  r looping throug
25345 68 20 70 4b 65 79 49 6e 66 6f 2d 3e 61 43 6f 6c  h pKeyInfo->aCol
25346 6c 5b 5d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e  l[] */.    int n
25347 43 6f 6c 3b 20 20 20 20 20 20 20 20 20 20 20 20  Col;            
25348 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
25349 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 69 6e  er of columns in
2534a 20 72 65 73 75 6c 74 20 73 65 74 20 2a 2f 0a 0a   result set */..
2534b 20 20 20 20 61 73 73 65 72 74 28 20 70 2d 3e 70      assert( p->p
2534c 52 69 67 68 74 6d 6f 73 74 3d 3d 70 20 29 3b 0a  Rightmost==p );.
2534d 20 20 20 20 6e 43 6f 6c 20 3d 20 70 2d 3e 70 45      nCol = p->pE
2534e 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 0a 20 20 20  List->nExpr;.   
2534f 20 70 4b 65 79 49 6e 66 6f 20 3d 20 73 71 6c 69   pKeyInfo = sqli
25350 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
25351 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  db,.            
25352 20 20 20 20 20 20 20 20 20 20 20 73 69 7a 65 6f             sizeo
25353 66 28 2a 70 4b 65 79 49 6e 66 6f 29 2b 6e 43 6f  f(*pKeyInfo)+nCo
25354 6c 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65  l*(sizeof(CollSe
25355 71 2a 29 20 2b 20 31 29 29 3b 0a 20 20 20 20 69  q*) + 1));.    i
25356 66 28 20 21 70 4b 65 79 49 6e 66 6f 20 29 7b 0a  f( !pKeyInfo ){.
25357 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
25358 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 67  E_NOMEM;.      g
25359 6f 74 6f 20 6d 75 6c 74 69 5f 73 65 6c 65 63 74  oto multi_select
2535a 5f 65 6e 64 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  _end;.    }..   
2535b 20 70 4b 65 79 49 6e 66 6f 2d 3e 65 6e 63 20 3d   pKeyInfo->enc =
2535c 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20 70 4b   ENC(db);.    pK
2535d 65 79 49 6e 66 6f 2d 3e 6e 46 69 65 6c 64 20 3d  eyInfo->nField =
2535e 20 6e 43 6f 6c 3b 0a 0a 20 20 20 20 66 6f 72 28   nCol;..    for(
2535f 69 3d 30 2c 20 61 70 43 6f 6c 6c 3d 70 4b 65 79  i=0, apColl=pKey
25360 49 6e 66 6f 2d 3e 61 43 6f 6c 6c 3b 20 69 3c 6e  Info->aColl; i<n
25361 43 6f 6c 3b 20 69 2b 2b 2c 20 61 70 43 6f 6c 6c  Col; i++, apColl
25362 2b 2b 29 7b 0a 20 20 20 20 20 20 2a 61 70 43 6f  ++){.      *apCo
25363 6c 6c 20 3d 20 6d 75 6c 74 69 53 65 6c 65 63 74  ll = multiSelect
25364 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20  CollSeq(pParse, 
25365 70 2c 20 69 29 3b 0a 20 20 20 20 20 20 69 66 28  p, i);.      if(
25366 20 30 3d 3d 2a 61 70 43 6f 6c 6c 20 29 7b 0a 20   0==*apColl ){. 
25367 20 20 20 20 20 20 20 2a 61 70 43 6f 6c 6c 20 3d         *apColl =
25368 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
25369 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2536a 20 20 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20     for(pLoop=p; 
2536b 70 4c 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f  pLoop; pLoop=pLo
2536c 6f 70 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20  op->pPrior){.   
2536d 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 32 3b     for(i=0; i<2;
2536e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
2536f 6e 74 20 61 64 64 72 20 3d 20 70 4c 6f 6f 70 2d  nt addr = pLoop-
25370 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d  >addrOpenEphm[i]
25371 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 61 64  ;.        if( ad
25372 64 72 3c 30 20 29 7b 0a 20 20 20 20 20 20 20 20  dr<0 ){.        
25373 20 20 2f 2a 20 49 66 20 5b 30 5d 20 69 73 20 75    /* If [0] is u
25374 6e 75 73 65 64 20 74 68 65 6e 20 5b 31 5d 20 69  nused then [1] i
25375 73 20 61 6c 73 6f 20 75 6e 75 73 65 64 2e 20 20  s also unused.  
25376 53 6f 20 77 65 20 63 61 6e 0a 20 20 20 20 20 20  So we can.      
25377 20 20 20 20 2a 2a 20 61 6c 77 61 79 73 20 73 61      ** always sa
25378 66 65 6c 79 20 61 62 6f 72 74 20 61 73 20 73 6f  fely abort as so
25379 6f 6e 20 61 73 20 74 68 65 20 66 69 72 73 74 20  on as the first 
2537a 75 6e 75 73 65 64 20 73 6c 6f 74 20 69 73 20 66  unused slot is f
2537b 6f 75 6e 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ound */.        
2537c 20 20 61 73 73 65 72 74 28 20 70 4c 6f 6f 70 2d    assert( pLoop-
2537d 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 31 5d  >addrOpenEphm[1]
2537e 3c 30 20 29 3b 0a 20 20 20 20 20 20 20 20 20 20  <0 );.          
2537f 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
25380 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
25381 56 64 62 65 43 68 61 6e 67 65 50 32 28 76 2c 20  VdbeChangeP2(v, 
25382 61 64 64 72 2c 20 6e 43 6f 6c 29 3b 0a 20 20 20  addr, nCol);.   
25383 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
25384 43 68 61 6e 67 65 50 34 28 76 2c 20 61 64 64 72  ChangeP4(v, addr
25385 2c 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66  , (char*)pKeyInf
25386 6f 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a  o, P4_KEYINFO);.
25387 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e 61          pLoop->a
25388 64 64 72 4f 70 65 6e 45 70 68 6d 5b 69 5d 20 3d  ddrOpenEphm[i] =
25389 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   -1;.      }.   
2538a 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62   }.    sqlite3Db
2538b 46 72 65 65 28 64 62 2c 20 70 4b 65 79 49 6e 66  Free(db, pKeyInf
2538c 6f 29 3b 0a 20 20 7d 0a 0a 6d 75 6c 74 69 5f 73  o);.  }..multi_s
2538d 65 6c 65 63 74 5f 65 6e 64 3a 0a 20 20 70 44 65  elect_end:.  pDe
2538e 73 74 2d 3e 69 4d 65 6d 20 3d 20 64 65 73 74 2e  st->iMem = dest.
2538f 69 4d 65 6d 3b 0a 20 20 70 44 65 73 74 2d 3e 6e  iMem;.  pDest->n
25390 4d 65 6d 20 3d 20 64 65 73 74 2e 6e 4d 65 6d 3b  Mem = dest.nMem;
25391 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74  .  sqlite3Select
25392 44 65 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 65  Delete(db, pDele
25393 74 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  te);.  return rc
25394 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  ;.}.#endif /* SQ
25395 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55  LITE_OMIT_COMPOU
25396 4e 44 5f 53 45 4c 45 43 54 20 2a 2f 0a 0a 2f 2a  ND_SELECT */../*
25397 0a 2a 2a 20 43 6f 64 65 20 61 6e 20 6f 75 74 70  .** Code an outp
25398 75 74 20 73 75 62 72 6f 75 74 69 6e 65 20 66 6f  ut subroutine fo
25399 72 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 69 6d  r a coroutine im
2539a 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2539b 61 0a 2a 2a 20 53 45 4c 45 43 54 20 73 74 61 74  a.** SELECT stat
2539c 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ment..**.** The 
2539d 64 61 74 61 20 74 6f 20 62 65 20 6f 75 74 70 75  data to be outpu
2539e 74 20 69 73 20 63 6f 6e 74 61 69 6e 65 64 20 69  t is contained i
2539f 6e 20 70 49 6e 2d 3e 69 4d 65 6d 2e 20 20 54 68  n pIn->iMem.  Th
253a0 65 72 65 20 61 72 65 0a 2a 2a 20 70 49 6e 2d 3e  ere are.** pIn->
253a1 6e 4d 65 6d 20 63 6f 6c 75 6d 6e 73 20 74 6f 20  nMem columns to 
253a2 62 65 20 6f 75 74 70 75 74 2e 20 20 70 44 65 73  be output.  pDes
253a3 74 20 69 73 20 77 68 65 72 65 20 74 68 65 20 6f  t is where the o
253a4 75 74 70 75 74 20 73 68 6f 75 6c 64 0a 2a 2a 20  utput should.** 
253a5 62 65 20 73 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 72  be sent..**.** r
253a6 65 67 52 65 74 75 72 6e 20 69 73 20 74 68 65 20  egReturn is the 
253a7 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20 72 65  number of the re
253a8 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 74  gister holding t
253a9 68 65 20 73 75 62 72 6f 75 74 69 6e 65 0a 2a 2a  he subroutine.**
253aa 20 72 65 74 75 72 6e 20 61 64 64 72 65 73 73 2e   return address.
253ab 0a 2a 2a 0a 2a 2a 20 49 66 20 72 65 67 50 72 65  .**.** If regPre
253ac 76 3e 30 20 74 68 65 6e 20 69 74 20 69 73 20 61  v>0 then it is a
253ad 20 74 68 65 20 66 69 72 73 74 20 72 65 67 69 73   the first regis
253ae 74 65 72 20 69 6e 20 61 20 76 65 63 74 6f 72 20  ter in a vector 
253af 74 68 61 74 0a 2a 2a 20 72 65 63 6f 72 64 73 20  that.** records 
253b0 74 68 65 20 70 72 65 76 69 6f 75 73 20 6f 75 74  the previous out
253b1 70 75 74 2e 20 20 6d 65 6d 5b 72 65 67 50 72 65  put.  mem[regPre
253b2 76 5d 20 69 73 20 61 20 66 6c 61 67 20 74 68 61  v] is a flag tha
253b3 74 20 69 73 20 66 61 6c 73 65 0a 2a 2a 20 69 66  t is false.** if
253b4 20 74 68 65 72 65 20 68 61 73 20 62 65 65 6e 20   there has been 
253b5 6e 6f 20 70 72 65 76 69 6f 75 73 20 6f 75 74 70  no previous outp
253b6 75 74 2e 20 20 49 66 20 72 65 67 50 72 65 76 3e  ut.  If regPrev>
253b7 30 20 74 68 65 6e 20 63 6f 64 65 20 69 73 0a 2a  0 then code is.*
253b8 2a 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 73  * generated to s
253b9 75 70 70 72 65 73 73 20 64 75 70 6c 69 63 61 74  uppress duplicat
253ba 65 73 2e 20 20 70 4b 65 79 49 6e 66 6f 20 69 73  es.  pKeyInfo is
253bb 20 75 73 65 64 20 66 6f 72 20 63 6f 6d 70 61 72   used for compar
253bc 69 6e 67 0a 2a 2a 20 6b 65 79 73 2e 0a 2a 2a 0a  ing.** keys..**.
253bd 2a 2a 20 49 66 20 74 68 65 20 4c 49 4d 49 54 20  ** If the LIMIT 
253be 66 6f 75 6e 64 20 69 6e 20 70 2d 3e 69 4c 69 6d  found in p->iLim
253bf 69 74 20 69 73 20 72 65 61 63 68 65 64 2c 20 6a  it is reached, j
253c0 75 6d 70 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  ump immediately 
253c1 74 6f 0a 2a 2a 20 69 42 72 65 61 6b 2e 0a 2a 2f  to.** iBreak..*/
253c2 0a 73 74 61 74 69 63 20 69 6e 74 20 67 65 6e 65  .static int gene
253c3 72 61 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75  rateOutputSubrou
253c4 74 69 6e 65 28 0a 20 20 50 61 72 73 65 20 2a 70  tine(.  Parse *p
253c5 50 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20  Parse,          
253c6 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
253c7 78 74 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a  xt */.  Select *
253c8 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
253c9 2f 2a 20 54 68 65 20 53 45 4c 45 43 54 20 73 74  /* The SELECT st
253ca 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53 65 6c  atement */.  Sel
253cb 65 63 74 44 65 73 74 20 2a 70 49 6e 2c 20 20 20  ectDest *pIn,   
253cc 20 20 20 20 20 2f 2a 20 43 6f 72 6f 75 74 69 6e       /* Coroutin
253cd 65 20 73 75 70 70 6c 79 69 6e 67 20 64 61 74 61  e supplying data
253ce 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
253cf 20 2a 70 44 65 73 74 2c 20 20 20 20 20 20 2f 2a   *pDest,      /*
253d0 20 57 68 65 72 65 20 74 6f 20 73 65 6e 64 20 74   Where to send t
253d1 68 65 20 64 61 74 61 20 2a 2f 0a 20 20 69 6e 74  he data */.  int
253d2 20 72 65 67 52 65 74 75 72 6e 2c 20 20 20 20 20   regReturn,     
253d3 20 20 20 20 20 2f 2a 20 54 68 65 20 72 65 74 75       /* The retu
253d4 72 6e 20 61 64 64 72 65 73 73 20 72 65 67 69 73  rn address regis
253d5 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ter */.  int reg
253d6 50 72 65 76 2c 20 20 20 20 20 20 20 20 20 20 20  Prev,           
253d7 20 2f 2a 20 50 72 65 76 69 6f 75 73 20 72 65 73   /* Previous res
253d8 75 6c 74 20 72 65 67 69 73 74 65 72 2e 20 20 4e  ult register.  N
253d9 6f 20 75 6e 69 71 75 65 6e 65 73 73 20 69 66 20  o uniqueness if 
253da 30 20 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a  0 */.  KeyInfo *
253db 70 4b 65 79 49 6e 66 6f 2c 20 20 20 20 20 20 2f  pKeyInfo,      /
253dc 2a 20 46 6f 72 20 63 6f 6d 70 61 72 69 6e 67 20  * For comparing 
253dd 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 65 6e  with previous en
253de 74 72 79 20 2a 2f 0a 20 20 69 6e 74 20 70 34 74  try */.  int p4t
253df 79 70 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ype,            
253e0 20 2f 2a 20 54 68 65 20 70 34 20 74 79 70 65 20   /* The p4 type 
253e1 66 6f 72 20 70 4b 65 79 49 6e 66 6f 20 2a 2f 0a  for pKeyInfo */.
253e2 20 20 69 6e 74 20 69 42 72 65 61 6b 20 20 20 20    int iBreak    
253e3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a 75 6d            /* Jum
253e4 70 20 68 65 72 65 20 69 66 20 77 65 20 68 69 74  p here if we hit
253e5 20 74 68 65 20 4c 49 4d 49 54 20 2a 2f 0a 29 7b   the LIMIT */.){
253e6 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61  .  Vdbe *v = pPa
253e7 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20 69 6e  rse->pVdbe;.  in
253e8 74 20 69 43 6f 6e 74 69 6e 75 65 3b 0a 20 20 69  t iContinue;.  i
253e9 6e 74 20 61 64 64 72 3b 0a 0a 20 20 61 64 64 72  nt addr;..  addr
253ea 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
253eb 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20  rrentAddr(v);.  
253ec 69 43 6f 6e 74 69 6e 75 65 20 3d 20 73 71 6c 69  iContinue = sqli
253ed 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c  te3VdbeMakeLabel
253ee 28 76 29 3b 0a 0a 20 20 2f 2a 20 53 75 70 70 72  (v);..  /* Suppr
253ef 65 73 73 20 64 75 70 6c 69 63 61 74 65 73 20 66  ess duplicates f
253f0 6f 72 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54  or UNION, EXCEPT
253f1 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43 54 20  , and INTERSECT 
253f2 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 65 67 50  .  */.  if( regP
253f3 72 65 76 20 29 7b 0a 20 20 20 20 69 6e 74 20 6a  rev ){.    int j
253f4 31 2c 20 6a 32 3b 0a 20 20 20 20 6a 31 20 3d 20  1, j2;.    j1 = 
253f5 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
253f6 31 28 76 2c 20 4f 50 5f 49 66 4e 6f 74 2c 20 72  1(v, OP_IfNot, r
253f7 65 67 50 72 65 76 29 3b 0a 20 20 20 20 6a 32 20  egPrev);.    j2 
253f8 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
253f9 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70 61 72  Op4(v, OP_Compar
253fa 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c 20 72 65  e, pIn->iMem, re
253fb 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d 3e 6e 4d  gPrev+1, pIn->nM
253fc 65 6d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  em,.            
253fd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
253fe 20 20 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66    (char*)pKeyInf
253ff 6f 2c 20 70 34 74 79 70 65 29 3b 0a 20 20 20 20  o, p4type);.    
25400 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
25401 33 28 76 2c 20 4f 50 5f 4a 75 6d 70 2c 20 6a 32  3(v, OP_Jump, j2
25402 2b 32 2c 20 69 43 6f 6e 74 69 6e 75 65 2c 20 6a  +2, iContinue, j
25403 32 2b 32 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  2+2);.    sqlite
25404 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
25405 20 6a 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65   j1);.    sqlite
25406 33 45 78 70 72 43 6f 64 65 43 6f 70 79 28 70 50  3ExprCodeCopy(pP
25407 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d 65 6d 2c  arse, pIn->iMem,
25408 20 72 65 67 50 72 65 76 2b 31 2c 20 70 49 6e 2d   regPrev+1, pIn-
25409 3e 6e 4d 65 6d 29 3b 0a 20 20 20 20 73 71 6c 69  >nMem);.    sqli
2540a 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
2540b 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 31 2c 20   OP_Integer, 1, 
2540c 72 65 67 50 72 65 76 29 3b 0a 20 20 7d 0a 20 20  regPrev);.  }.  
2540d 69 66 28 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e  if( pParse->db->
2540e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
2540f 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f 2a 20 53  eturn 0;..  /* S
25410 75 70 70 72 65 73 73 20 74 68 65 20 74 68 65 20  uppress the the 
25411 66 69 72 73 74 20 4f 46 46 53 45 54 20 65 6e 74  first OFFSET ent
25412 72 69 65 73 20 69 66 20 74 68 65 72 65 20 69 73  ries if there is
25413 20 61 6e 20 4f 46 46 53 45 54 20 63 6c 61 75 73   an OFFSET claus
25414 65 0a 20 20 2a 2f 0a 20 20 63 6f 64 65 4f 66 66  e.  */.  codeOff
25415 73 65 74 28 76 2c 20 70 2c 20 69 43 6f 6e 74 69  set(v, p, iConti
25416 6e 75 65 29 3b 0a 0a 20 20 73 77 69 74 63 68 28  nue);..  switch(
25417 20 70 44 65 73 74 2d 3e 65 44 65 73 74 20 29 7b   pDest->eDest ){
25418 0a 20 20 20 20 2f 2a 20 53 74 6f 72 65 20 74 68  .    /* Store th
25419 65 20 72 65 73 75 6c 74 20 61 73 20 64 61 74 61  e result as data
2541a 20 75 73 69 6e 67 20 61 20 75 6e 69 71 75 65 20   using a unique 
2541b 6b 65 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  key..    */.    
2541c 63 61 73 65 20 53 52 54 5f 54 61 62 6c 65 3a 0a  case SRT_Table:.
2541d 20 20 20 20 63 61 73 65 20 53 52 54 5f 45 70 68      case SRT_Eph
2541e 65 6d 54 61 62 3a 20 7b 0a 20 20 20 20 20 20 69  emTab: {.      i
2541f 6e 74 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47  nt r1 = sqlite3G
25420 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  etTempReg(pParse
25421 29 3b 0a 20 20 20 20 20 20 69 6e 74 20 72 32 20  );.      int r2 
25422 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70  = sqlite3GetTemp
25423 52 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20  Reg(pParse);.   
25424 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25425 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52  dOp3(v, OP_MakeR
25426 65 63 6f 72 64 2c 20 70 49 6e 2d 3e 69 4d 65 6d  ecord, pIn->iMem
25427 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 2c 20 72 31 29  , pIn->nMem, r1)
25428 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25429 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2542a 4e 65 77 52 6f 77 69 64 2c 20 70 44 65 73 74 2d  NewRowid, pDest-
2542b 3e 69 50 61 72 6d 2c 20 72 32 29 3b 0a 20 20 20  >iParm, r2);.   
2542c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2542d 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65 72  dOp3(v, OP_Inser
2542e 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 2c  t, pDest->iParm,
2542f 20 72 31 2c 20 72 32 29 3b 0a 20 20 20 20 20 20   r1, r2);.      
25430 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
25431 65 50 35 28 76 2c 20 4f 50 46 4c 41 47 5f 41 50  eP5(v, OPFLAG_AP
25432 50 45 4e 44 29 3b 0a 20 20 20 20 20 20 73 71 6c  PEND);.      sql
25433 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
25434 65 67 28 70 50 61 72 73 65 2c 20 72 32 29 3b 0a  eg(pParse, r2);.
25435 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
25436 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
25437 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 62  se, r1);.      b
25438 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 23 69 66  reak;.    }..#if
25439 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2543a 5f 53 55 42 51 55 45 52 59 0a 20 20 20 20 2f 2a  _SUBQUERY.    /*
2543b 20 49 66 20 77 65 20 61 72 65 20 63 72 65 61 74   If we are creat
2543c 69 6e 67 20 61 20 73 65 74 20 66 6f 72 20 61 6e  ing a set for an
2543d 20 22 65 78 70 72 20 49 4e 20 28 53 45 4c 45 43   "expr IN (SELEC
2543e 54 20 2e 2e 2e 29 22 20 63 6f 6e 73 74 72 75 63  T ...)" construc
2543f 74 2c 0a 20 20 20 20 2a 2a 20 74 68 65 6e 20 74  t,.    ** then t
25440 68 65 72 65 20 73 68 6f 75 6c 64 20 62 65 20 61  here should be a
25441 20 73 69 6e 67 6c 65 20 69 74 65 6d 20 6f 6e 20   single item on 
25442 74 68 65 20 73 74 61 63 6b 2e 20 20 57 72 69 74  the stack.  Writ
25443 65 20 74 68 69 73 0a 20 20 20 20 2a 2a 20 69 74  e this.    ** it
25444 65 6d 20 69 6e 74 6f 20 74 68 65 20 73 65 74 20  em into the set 
25445 74 61 62 6c 65 20 77 69 74 68 20 62 6f 67 75 73  table with bogus
25446 20 64 61 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20   data..    */.  
25447 20 20 63 61 73 65 20 53 52 54 5f 53 65 74 3a 20    case SRT_Set: 
25448 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 31 3b 0a  {.      int r1;.
25449 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 49        assert( pI
2544a 6e 2d 3e 6e 4d 65 6d 3d 3d 31 20 29 3b 0a 20 20  n->nMem==1 );.  
2544b 20 20 20 20 70 2d 3e 61 66 66 69 6e 69 74 79 20      p->affinity 
2544c 3d 20 0a 20 20 20 20 20 20 20 20 20 73 71 6c 69  = .         sqli
2544d 74 65 33 43 6f 6d 70 61 72 65 41 66 66 69 6e 69  te3CompareAffini
2544e 74 79 28 70 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b  ty(p->pEList->a[
2544f 30 5d 2e 70 45 78 70 72 2c 20 70 44 65 73 74 2d  0].pExpr, pDest-
25450 3e 61 66 66 69 6e 69 74 79 29 3b 0a 20 20 20 20  >affinity);.    
25451 20 20 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65    r1 = sqlite3Ge
25452 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
25453 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
25454 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25455 4d 61 6b 65 52 65 63 6f 72 64 2c 20 70 49 6e 2d  MakeRecord, pIn-
25456 3e 69 4d 65 6d 2c 20 31 2c 20 72 31 2c 20 26 70  >iMem, 1, r1, &p
25457 2d 3e 61 66 66 69 6e 69 74 79 2c 20 31 29 3b 0a  ->affinity, 1);.
25458 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
25459 72 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68  rCacheAffinityCh
2545a 61 6e 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e  ange(pParse, pIn
2545b 2d 3e 69 4d 65 6d 2c 20 31 29 3b 0a 20 20 20 20  ->iMem, 1);.    
2545c 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
2545d 4f 70 32 28 76 2c 20 4f 50 5f 49 64 78 49 6e 73  Op2(v, OP_IdxIns
2545e 65 72 74 2c 20 70 44 65 73 74 2d 3e 69 50 61 72  ert, pDest->iPar
2545f 6d 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 73 71  m, r1);.      sq
25460 6c 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70  lite3ReleaseTemp
25461 52 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b  Reg(pParse, r1);
25462 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
25463 20 20 7d 0a 0a 23 69 66 20 30 20 20 2f 2a 20 4e    }..#if 0  /* N
25464 65 76 65 72 20 6f 63 63 75 72 73 20 6f 6e 20 61  ever occurs on a
25465 6e 20 4f 52 44 45 52 20 42 59 20 71 75 65 72 79  n ORDER BY query
25466 20 2a 2f 0a 20 20 20 20 2f 2a 20 49 66 20 61 6e   */.    /* If an
25467 79 20 72 6f 77 20 65 78 69 73 74 20 69 6e 20 74  y row exist in t
25468 68 65 20 72 65 73 75 6c 74 20 73 65 74 2c 20 72  he result set, r
25469 65 63 6f 72 64 20 74 68 61 74 20 66 61 63 74 20  ecord that fact 
2546a 61 6e 64 20 61 62 6f 72 74 2e 0a 20 20 20 20 2a  and abort..    *
2546b 2f 0a 20 20 20 20 63 61 73 65 20 53 52 54 5f 45  /.    case SRT_E
2546c 78 69 73 74 73 3a 20 7b 0a 20 20 20 20 20 20 73  xists: {.      s
2546d 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2546e 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
2546f 31 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d 29  1, pDest->iParm)
25470 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68 65 20 4c  ;.      /* The L
25471 49 4d 49 54 20 63 6c 61 75 73 65 20 77 69 6c 6c  IMIT clause will
25472 20 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c   terminate the l
25473 6f 6f 70 20 66 6f 72 20 75 73 20 2a 2f 0a 20 20  oop for us */.  
25474 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
25475 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
25476 49 66 20 74 68 69 73 20 69 73 20 61 20 73 63 61  If this is a sca
25477 6c 61 72 20 73 65 6c 65 63 74 20 74 68 61 74 20  lar select that 
25478 69 73 20 70 61 72 74 20 6f 66 20 61 6e 20 65 78  is part of an ex
25479 70 72 65 73 73 69 6f 6e 2c 20 74 68 65 6e 0a 20  pression, then. 
2547a 20 20 20 2a 2a 20 73 74 6f 72 65 20 74 68 65 20     ** store the 
2547b 72 65 73 75 6c 74 73 20 69 6e 20 74 68 65 20 61  results in the a
2547c 70 70 72 6f 70 72 69 61 74 65 20 6d 65 6d 6f 72  ppropriate memor
2547d 79 20 63 65 6c 6c 20 61 6e 64 20 62 72 65 61 6b  y cell and break
2547e 20 6f 75 74 0a 20 20 20 20 2a 2a 20 6f 66 20 74   out.    ** of t
2547f 68 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20 20  he scan loop..  
25480 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 52    */.    case SR
25481 54 5f 4d 65 6d 3a 20 7b 0a 20 20 20 20 20 20 61  T_Mem: {.      a
25482 73 73 65 72 74 28 20 70 49 6e 2d 3e 6e 4d 65 6d  ssert( pIn->nMem
25483 3d 3d 31 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==1 );.      sql
25484 69 74 65 33 45 78 70 72 43 6f 64 65 4d 6f 76 65  ite3ExprCodeMove
25485 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e 69 4d  (pParse, pIn->iM
25486 65 6d 2c 20 70 44 65 73 74 2d 3e 69 50 61 72 6d  em, pDest->iParm
25487 2c 20 31 29 3b 0a 20 20 20 20 20 20 2f 2a 20 54  , 1);.      /* T
25488 68 65 20 4c 49 4d 49 54 20 63 6c 61 75 73 65 20  he LIMIT clause 
25489 77 69 6c 6c 20 6a 75 6d 70 20 6f 75 74 20 6f 66  will jump out of
2548a 20 74 68 65 20 6c 6f 6f 70 20 66 6f 72 20 75 73   the loop for us
2548b 20 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b   */.      break;
2548c 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  .    }.#endif /*
2548d 20 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f   #ifndef SQLITE_
2548e 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2548f 0a 0a 20 20 20 20 2f 2a 20 54 68 65 20 72 65 73  ..    /* The res
25490 75 6c 74 73 20 61 72 65 20 73 74 6f 72 65 64 20  ults are stored 
25491 69 6e 20 61 20 73 65 71 75 65 6e 63 65 20 6f 66  in a sequence of
25492 20 72 65 67 69 73 74 65 72 73 0a 20 20 20 20 2a   registers.    *
25493 2a 20 73 74 61 72 74 69 6e 67 20 61 74 20 70 44  * starting at pD
25494 65 73 74 2d 3e 69 4d 65 6d 2e 20 20 54 68 65 6e  est->iMem.  Then
25495 20 74 68 65 20 63 6f 2d 72 6f 75 74 69 6e 65 20   the co-routine 
25496 79 69 65 6c 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  yields..    */. 
25497 20 20 20 63 61 73 65 20 53 52 54 5f 43 6f 72 6f     case SRT_Coro
25498 75 74 69 6e 65 3a 20 7b 0a 20 20 20 20 20 20 69  utine: {.      i
25499 66 28 20 70 44 65 73 74 2d 3e 69 4d 65 6d 3d 3d  f( pDest->iMem==
2549a 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 44 65  0 ){.        pDe
2549b 73 74 2d 3e 69 4d 65 6d 20 3d 20 73 71 6c 69 74  st->iMem = sqlit
2549c 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2549d 50 61 72 73 65 2c 20 70 49 6e 2d 3e 6e 4d 65 6d  Parse, pIn->nMem
2549e 29 3b 0a 20 20 20 20 20 20 20 20 70 44 65 73 74  );.        pDest
2549f 2d 3e 6e 4d 65 6d 20 3d 20 70 49 6e 2d 3e 6e 4d  ->nMem = pIn->nM
254a0 65 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  em;.      }.    
254a1 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
254a2 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 70 49  eMove(pParse, pI
254a3 6e 2d 3e 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e  n->iMem, pDest->
254a4 69 4d 65 6d 2c 20 70 44 65 73 74 2d 3e 6e 4d 65  iMem, pDest->nMe
254a5 6d 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  m);.      sqlite
254a6 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f  3VdbeAddOp1(v, O
254a7 50 5f 59 69 65 6c 64 2c 20 70 44 65 73 74 2d 3e  P_Yield, pDest->
254a8 69 50 61 72 6d 29 3b 0a 20 20 20 20 20 20 62 72  iParm);.      br
254a9 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  eak;.    }..    
254aa 2f 2a 20 52 65 73 75 6c 74 73 20 61 72 65 20 73  /* Results are s
254ab 74 6f 72 65 64 20 69 6e 20 61 20 73 65 71 75 65  tored in a seque
254ac 6e 63 65 20 6f 66 20 72 65 67 69 73 74 65 72 73  nce of registers
254ad 2e 20 20 54 68 65 6e 20 74 68 65 0a 20 20 20 20  .  Then the.    
254ae 2a 2a 20 4f 50 5f 52 65 73 75 6c 74 52 6f 77 20  ** OP_ResultRow 
254af 6f 70 63 6f 64 65 20 69 73 20 75 73 65 64 20 74  opcode is used t
254b0 6f 20 63 61 75 73 65 20 73 71 6c 69 74 65 33 5f  o cause sqlite3_
254b1 73 74 65 70 28 29 20 74 6f 20 72 65 74 75 72 6e  step() to return
254b2 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78 74  .    ** the next
254b3 20 72 6f 77 20 6f 66 20 72 65 73 75 6c 74 2e 0a   row of result..
254b4 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20      */.    case 
254b5 53 52 54 5f 4f 75 74 70 75 74 3a 20 7b 0a 20 20  SRT_Output: {.  
254b6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
254b7 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 75  ddOp2(v, OP_Resu
254b8 6c 74 52 6f 77 2c 20 70 49 6e 2d 3e 69 4d 65 6d  ltRow, pIn->iMem
254b9 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29 3b 0a 20 20  , pIn->nMem);.  
254ba 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
254bb 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61 6e  acheAffinityChan
254bc 67 65 28 70 50 61 72 73 65 2c 20 70 49 6e 2d 3e  ge(pParse, pIn->
254bd 69 4d 65 6d 2c 20 70 49 6e 2d 3e 6e 4d 65 6d 29  iMem, pIn->nMem)
254be 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
254bf 20 20 20 7d 0a 0a 23 69 66 20 21 64 65 66 69 6e     }..#if !defin
254c0 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ed(SQLITE_OMIT_T
254c1 52 49 47 47 45 52 29 0a 20 20 20 20 2f 2a 20 44  RIGGER).    /* D
254c2 69 73 63 61 72 64 20 74 68 65 20 72 65 73 75 6c  iscard the resul
254c3 74 73 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ts.  This is use
254c4 64 20 66 6f 72 20 53 45 4c 45 43 54 20 73 74 61  d for SELECT sta
254c5 74 65 6d 65 6e 74 73 20 69 6e 73 69 64 65 0a 20  tements inside. 
254c6 20 20 20 2a 2a 20 74 68 65 20 62 6f 64 79 20 6f     ** the body o
254c7 66 20 61 20 54 52 49 47 47 45 52 2e 20 20 54 68  f a TRIGGER.  Th
254c8 65 20 70 75 72 70 6f 73 65 20 6f 66 20 73 75 63  e purpose of suc
254c9 68 20 73 65 6c 65 63 74 73 20 69 73 20 74 6f 20  h selects is to 
254ca 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 75 73 65 72  call.    ** user
254cb 2d 64 65 66 69 6e 65 64 20 66 75 6e 63 74 69 6f  -defined functio
254cc 6e 73 20 74 68 61 74 20 68 61 76 65 20 73 69 64  ns that have sid
254cd 65 20 65 66 66 65 63 74 73 2e 20 20 57 65 20 64  e effects.  We d
254ce 6f 20 6e 6f 74 20 63 61 72 65 0a 20 20 20 20 2a  o not care.    *
254cf 2a 20 61 62 6f 75 74 20 74 68 65 20 61 63 74 75  * about the actu
254d0 61 6c 20 72 65 73 75 6c 74 73 20 6f 66 20 74 68  al results of th
254d1 65 20 73 65 6c 65 63 74 2e 0a 20 20 20 20 2a 2f  e select..    */
254d2 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a  .    default: {.
254d3 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
254d4 20 7d 0a 23 65 6e 64 69 66 0a 20 20 7d 0a 0a 20   }.#endif.  }.. 
254d5 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68 65 20   /* Jump to the 
254d6 65 6e 64 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20  end of the loop 
254d7 69 66 20 74 68 65 20 4c 49 4d 49 54 20 69 73 20  if the LIMIT is 
254d8 72 65 61 63 68 65 64 2e 0a 20 20 2a 2f 0a 20 20  reached..  */.  
254d9 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 29 7b  if( p->iLimit ){
254da 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
254db 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 41 64 64  AddOp2(v, OP_Add
254dc 49 6d 6d 2c 20 70 2d 3e 69 4c 69 6d 69 74 2c 20  Imm, p->iLimit, 
254dd 2d 31 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  -1);.    sqlite3
254de 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
254df 5f 49 66 5a 65 72 6f 2c 20 70 2d 3e 69 4c 69 6d  _IfZero, p->iLim
254e0 69 74 2c 20 69 42 72 65 61 6b 29 3b 0a 20 20 7d  it, iBreak);.  }
254e1 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20  ..  /* Generate 
254e2 74 68 65 20 73 75 62 72 6f 75 74 69 6e 65 20 72  the subroutine r
254e3 65 74 75 72 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c  eturn.  */.  sql
254e4 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c  ite3VdbeResolveL
254e5 61 62 65 6c 28 76 2c 20 69 43 6f 6e 74 69 6e 75  abel(v, iContinu
254e6 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62  e);.  sqlite3Vdb
254e7 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65  eAddOp1(v, OP_Re
254e8 74 75 72 6e 2c 20 72 65 67 52 65 74 75 72 6e 29  turn, regReturn)
254e9 3b 0a 0a 20 20 72 65 74 75 72 6e 20 61 64 64 72  ;..  return addr
254ea 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 6c 74 65 72  ;.}../*.** Alter
254eb 6e 61 74 69 76 65 20 63 6f 6d 70 6f 75 6e 64 20  native compound 
254ec 73 65 6c 65 63 74 20 63 6f 64 65 20 67 65 6e 65  select code gene
254ed 72 61 74 6f 72 20 66 6f 72 20 63 61 73 65 73 20  rator for cases 
254ee 77 68 65 6e 20 74 68 65 72 65 0a 2a 2a 20 69 73  when there.** is
254ef 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61   an ORDER BY cla
254f0 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 57 65 20 61 73  use..**.** We as
254f1 73 75 6d 65 20 61 20 71 75 65 72 79 20 6f 66 20  sume a query of 
254f2 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 6f  the following fo
254f3 72 6d 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 3c  rm:.**.**      <
254f4 73 65 6c 65 63 74 41 3e 20 20 3c 6f 70 65 72 61  selectA>  <opera
254f5 74 6f 72 3e 20 20 3c 73 65 6c 65 63 74 42 3e 20  tor>  <selectB> 
254f6 20 4f 52 44 45 52 20 42 59 20 3c 6f 72 64 65 72   ORDER BY <order
254f7 62 79 6c 69 73 74 3e 0a 2a 2a 0a 2a 2a 20 3c 6f  bylist>.**.** <o
254f8 70 65 72 61 74 6f 72 3e 20 69 73 20 6f 6e 65 20  perator> is one 
254f9 6f 66 20 55 4e 49 4f 4e 20 41 4c 4c 2c 20 55 4e  of UNION ALL, UN
254fa 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20 6f 72 20  ION, EXCEPT, or 
254fb 49 4e 54 45 52 53 45 43 54 2e 20 20 54 68 65 20  INTERSECT.  The 
254fc 69 64 65 61 0a 2a 2a 20 69 73 20 74 6f 20 63 6f  idea.** is to co
254fd 64 65 20 62 6f 74 68 20 3c 73 65 6c 65 63 74 41  de both <selectA
254fe 3e 20 61 6e 64 20 3c 73 65 6c 65 63 74 42 3e 20  > and <selectB> 
254ff 77 69 74 68 20 74 68 65 20 4f 52 44 45 52 20 42  with the ORDER B
25500 59 20 63 6c 61 75 73 65 20 61 73 0a 2a 2a 20 63  Y clause as.** c
25501 6f 2d 72 6f 75 74 69 6e 65 73 2e 20 20 54 68 65  o-routines.  The
25502 6e 20 72 75 6e 20 74 68 65 20 63 6f 2d 72 6f 75  n run the co-rou
25503 74 69 6e 65 73 20 69 6e 20 70 61 72 61 6c 6c 65  tines in paralle
25504 6c 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20  l and merge the 
25505 72 65 73 75 6c 74 73 0a 2a 2a 20 69 6e 74 6f 20  results.** into 
25506 74 68 65 20 6f 75 74 70 75 74 2e 20 20 49 6e 20  the output.  In 
25507 61 64 64 69 74 69 6f 6e 20 74 6f 20 74 68 65 20  addition to the 
25508 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65 73 20 28  two coroutines (
25509 63 61 6c 6c 65 64 20 73 65 6c 65 63 74 41 20 61  called selectA a
2550a 6e 64 0a 2a 2a 20 73 65 6c 65 63 74 42 29 20 74  nd.** selectB) t
2550b 68 65 72 65 20 61 72 65 20 37 20 73 75 62 72 6f  here are 7 subro
2550c 75 74 69 6e 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  utines:.**.**   
2550d 20 6f 75 74 41 3a 20 20 20 20 4d 6f 76 65 20 74   outA:    Move t
2550e 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
2550f 20 73 65 6c 65 63 74 41 20 63 6f 72 6f 75 74 69   selectA corouti
25510 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70  ne into the outp
25511 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ut.**           
25512 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75 6e    of the compoun
25513 64 20 71 75 65 72 79 2e 0a 2a 2a 0a 2a 2a 20 20  d query..**.**  
25514 20 20 6f 75 74 42 3a 20 20 20 20 4d 6f 76 65 20    outB:    Move 
25515 74 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68  the output of th
25516 65 20 73 65 6c 65 63 74 42 20 63 6f 72 6f 75 74  e selectB corout
25517 69 6e 65 20 69 6e 74 6f 20 74 68 65 20 6f 75 74  ine into the out
25518 70 75 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  put.**          
25519 20 20 20 6f 66 20 74 68 65 20 63 6f 6d 70 6f 75     of the compou
2551a 6e 64 20 71 75 65 72 79 2e 20 20 28 4f 6e 6c 79  nd query.  (Only
2551b 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20 55   generated for U
2551c 4e 49 4f 4e 20 61 6e 64 0a 2a 2a 20 20 20 20 20  NION and.**     
2551d 20 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c          UNION AL
2551e 4c 2e 20 20 45 58 43 45 50 54 20 61 6e 64 20 49  L.  EXCEPT and I
2551f 4e 53 45 52 54 53 45 43 54 20 6e 65 76 65 72 20  NSERTSECT never 
25520 6f 75 74 70 75 74 20 61 20 72 6f 77 20 74 68 61  output a row tha
25521 74 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  t.**            
25522 20 61 70 70 65 61 72 73 20 6f 6e 6c 79 20 69 6e   appears only in
25523 20 42 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 20 41 6c   B.).**.**    Al
25524 74 42 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68  tB:    Called wh
25525 65 6e 20 74 68 65 72 65 20 69 73 20 64 61 74 61  en there is data
25526 20 66 72 6f 6d 20 62 6f 74 68 20 63 6f 72 6f 75   from both corou
25527 74 69 6e 65 73 20 61 6e 64 20 41 3c 42 2e 0a 2a  tines and A<B..*
25528 2a 0a 2a 2a 20 20 20 20 41 65 71 42 3a 20 20 20  *.**    AeqB:   
25529 20 43 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65   Called when the
2552a 72 65 20 69 73 20 64 61 74 61 20 66 72 6f 6d 20  re is data from 
2552b 62 6f 74 68 20 63 6f 72 6f 75 74 69 6e 65 73 20  both coroutines 
2552c 61 6e 64 20 41 3d 3d 42 2e 0a 2a 2a 0a 2a 2a 20  and A==B..**.** 
2552d 20 20 20 41 67 74 42 3a 20 20 20 20 43 61 6c 6c     AgtB:    Call
2552e 65 64 20 77 68 65 6e 20 74 68 65 72 65 20 69 73  ed when there is
2552f 20 64 61 74 61 20 66 72 6f 6d 20 62 6f 74 68 20   data from both 
25530 63 6f 72 6f 75 74 69 6e 65 73 20 61 6e 64 20 41  coroutines and A
25531 3e 42 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66  >B..**.**    Eof
25532 41 3a 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65  A:    Called whe
25533 6e 20 64 61 74 61 20 69 73 20 65 78 68 61 75 73  n data is exhaus
25534 74 65 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 41  ted from selectA
25535 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 45 6f 66 42 3a  ..**.**    EofB:
25536 20 20 20 20 43 61 6c 6c 65 64 20 77 68 65 6e 20      Called when 
25537 64 61 74 61 20 69 73 20 65 78 68 61 75 73 74 65  data is exhauste
25538 64 20 66 72 6f 6d 20 73 65 6c 65 63 74 42 2e 0a  d from selectB..
25539 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d  **.** The implem
2553a 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
2553b 6c 61 74 74 65 72 20 66 69 76 65 20 73 75 62 72  latter five subr
2553c 6f 75 74 69 6e 65 73 20 64 65 70 65 6e 64 20 6f  outines depend o
2553d 6e 20 77 68 69 63 68 20 0a 2a 2a 20 3c 6f 70 65  n which .** <ope
2553e 72 61 74 6f 72 3e 20 69 73 20 75 73 65 64 3a 0a  rator> is used:.
2553f 2a 2a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  **.**.**        
25540 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 20 20       UNION ALL  
25541 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 20 20 20         UNION    
25542 20 20 20 20 20 20 20 20 45 58 43 45 50 54 20 20          EXCEPT  
25543 20 20 20 20 20 20 20 20 49 4e 54 45 52 53 45 43          INTERSEC
25544 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2d 2d  T.**          --
25545 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 2d 2d 2d  -----------  ---
25546 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
25547 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20  --------------  
25548 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
25549 2d 0a 2a 2a 20 20 20 41 6c 74 42 3a 20 20 20 6f  -.**   AltB:   o
2554a 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20 20  utA, nextA      
2554b 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20 20 20  outA, nextA     
2554c 20 20 6f 75 74 41 2c 20 6e 65 78 74 41 20 20 20    outA, nextA   
2554d 20 20 20 20 20 20 6e 65 78 74 41 0a 2a 2a 0a 2a        nextA.**.*
2554e 2a 20 20 20 41 65 71 42 3a 20 20 20 6f 75 74 41  *   AeqB:   outA
2554f 2c 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20  , nextA         
25550 6e 65 78 74 41 20 20 20 20 20 20 20 20 20 20 20  nextA           
25551 20 20 6e 65 78 74 41 20 20 20 20 20 20 20 20 20    nextA         
25552 6f 75 74 41 2c 20 6e 65 78 74 41 0a 2a 2a 0a 2a  outA, nextA.**.*
25553 2a 20 20 20 41 67 74 42 3a 20 20 20 6f 75 74 42  *   AgtB:   outB
25554 2c 20 6e 65 78 74 42 20 20 20 20 20 20 6f 75 74  , nextB      out
25555 42 2c 20 6e 65 78 74 42 20 20 20 20 20 20 20 20  B, nextB        
25556 20 20 6e 65 78 74 42 20 20 20 20 20 20 20 20 20    nextB         
25557 20 20 20 6e 65 78 74 42 0a 2a 2a 0a 2a 2a 20 20     nextB.**.**  
25558 20 45 6f 66 41 3a 20 20 20 6f 75 74 42 2c 20 6e   EofA:   outB, n
25559 65 78 74 42 20 20 20 20 20 20 6f 75 74 42 2c 20  extB      outB, 
2555a 6e 65 78 74 42 20 20 20 20 20 20 20 20 20 20 68  nextB          h
2555b 61 6c 74 20 20 20 20 20 20 20 20 20 20 20 20 20  alt             
2555c 68 61 6c 74 0a 2a 2a 0a 2a 2a 20 20 20 45 6f 66  halt.**.**   Eof
2555d 42 3a 20 20 20 6f 75 74 41 2c 20 6e 65 78 74 41  B:   outA, nextA
2555e 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65 78 74        outA, next
2555f 41 20 20 20 20 20 20 20 6f 75 74 41 2c 20 6e 65  A       outA, ne
25560 78 74 41 20 20 20 20 20 20 20 20 20 68 61 6c 74  xtA         halt
25561 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20 41 6c  .**.** In the Al
25562 74 42 2c 20 41 65 71 42 2c 20 61 6e 64 20 41 67  tB, AeqB, and Ag
25563 74 42 20 73 75 62 72 6f 75 74 69 6e 65 73 2c 20  tB subroutines, 
25564 61 6e 20 45 4f 46 20 6f 6e 20 41 20 66 6f 6c 6c  an EOF on A foll
25565 6f 77 69 6e 67 20 6e 65 78 74 41 0a 2a 2a 20 63  owing nextA.** c
25566 61 75 73 65 73 20 61 6e 20 69 6d 6d 65 64 69 61  auses an immedia
25567 74 65 20 6a 75 6d 70 20 74 6f 20 45 6f 66 41 20  te jump to EofA 
25568 61 6e 64 20 61 6e 20 45 4f 46 20 6f 6e 20 42 20  and an EOF on B 
25569 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78 74 42 20  following nextB 
2556a 63 61 75 73 65 73 0a 2a 2a 20 61 6e 20 69 6d 6d  causes.** an imm
2556b 65 64 69 61 74 65 20 6a 75 6d 70 20 74 6f 20 45  ediate jump to E
2556c 6f 66 42 2e 20 20 57 69 74 68 69 6e 20 45 6f 66  ofB.  Within Eof
2556d 41 20 61 6e 64 20 45 6f 66 42 2c 20 61 6e 64 20  A and EofB, and 
2556e 45 4f 46 20 6f 6e 20 65 6e 74 72 79 20 6f 72 0a  EOF on entry or.
2556f 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 6e 65 78  ** following nex
25570 74 58 20 63 61 75 73 65 73 20 61 20 6a 75 6d 70  tX causes a jump
25571 20 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 74   to the end of t
25572 68 65 20 73 65 6c 65 63 74 20 70 72 6f 63 65 73  he select proces
25573 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 44 75 70 6c  sing..**.** Dupl
25574 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20 69 6e  icate removal in
25575 20 74 68 65 20 55 4e 49 4f 4e 2c 20 45 58 43 45   the UNION, EXCE
25576 50 54 2c 20 61 6e 64 20 49 4e 54 45 52 53 45 43  PT, and INTERSEC
25577 54 20 63 61 73 65 73 20 69 73 20 68 61 6e 64 6c  T cases is handl
25578 65 64 0a 2a 2a 20 77 69 74 68 69 6e 20 74 68 65  ed.** within the
25579 20 6f 75 74 70 75 74 20 73 75 62 72 6f 75 74 69   output subrouti
2557a 6e 65 2e 20 20 54 68 65 20 72 65 67 50 72 65 76  ne.  The regPrev
2557b 20 72 65 67 69 73 74 65 72 20 73 65 74 20 68 6f   register set ho
2557c 6c 64 73 20 74 68 65 20 70 72 65 76 69 6f 75 73  lds the previous
2557d 6c 79 0a 2a 2a 20 6f 75 74 70 75 74 20 76 61 6c  ly.** output val
2557e 75 65 2e 20 20 41 20 63 6f 6d 70 61 72 69 73 6f  ue.  A compariso
2557f 6e 20 69 73 20 6d 61 64 65 20 61 67 61 69 6e 73  n is made agains
25580 74 20 74 68 69 73 20 76 61 6c 75 65 20 61 6e 64  t this value and
25581 20 74 68 65 20 6f 75 74 70 75 74 0a 2a 2a 20 69   the output.** i
25582 73 20 73 6b 69 70 70 65 64 20 69 66 20 74 68 65  s skipped if the
25583 20 6e 65 78 74 20 72 65 73 75 6c 74 73 20 77 6f   next results wo
25584 75 6c 64 20 62 65 20 74 68 65 20 73 61 6d 65 20  uld be the same 
25585 61 73 20 74 68 65 20 70 72 65 76 69 6f 75 73 2e  as the previous.
25586 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6d 70 6c 65  .**.** The imple
25587 6d 65 6e 74 61 74 69 6f 6e 20 70 6c 61 6e 20 69  mentation plan i
25588 73 20 74 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74  s to implement t
25589 68 65 20 74 77 6f 20 63 6f 72 6f 75 74 69 6e 65  he two coroutine
2558a 73 20 61 6e 64 20 73 65 76 65 6e 0a 2a 2a 20 73  s and seven.** s
2558b 75 62 72 6f 75 74 69 6e 65 73 20 66 69 72 73 74  ubroutines first
2558c 2c 20 74 68 65 6e 20 70 75 74 20 74 68 65 20 63  , then put the c
2558d 6f 6e 74 72 6f 6c 20 6c 6f 67 69 63 20 61 74 20  ontrol logic at 
2558e 74 68 65 20 62 6f 74 74 6f 6d 2e 20 20 4c 69 6b  the bottom.  Lik
2558f 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20  e this:.**.**   
25590 20 20 20 20 20 20 20 67 6f 74 6f 20 49 6e 69 74         goto Init
25591 0a 2a 2a 20 20 20 20 20 63 6f 41 3a 20 63 6f 72  .**     coA: cor
25592 6f 75 74 69 6e 65 20 66 6f 72 20 6c 65 66 74 20  outine for left 
25593 71 75 65 72 79 20 28 41 29 0a 2a 2a 20 20 20 20  query (A).**    
25594 20 63 6f 42 3a 20 63 6f 72 6f 75 74 69 6e 65 20   coB: coroutine 
25595 66 6f 72 20 72 69 67 68 74 20 71 75 65 72 79 20  for right query 
25596 28 42 29 0a 2a 2a 20 20 20 20 6f 75 74 41 3a 20  (B).**    outA: 
25597 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f  output one row o
25598 66 20 41 0a 2a 2a 20 20 20 20 6f 75 74 42 3a 20  f A.**    outB: 
25599 6f 75 74 70 75 74 20 6f 6e 65 20 72 6f 77 20 6f  output one row o
2559a 66 20 42 20 28 55 4e 49 4f 4e 20 61 6e 64 20 55  f B (UNION and U
2559b 4e 49 4f 4e 20 41 4c 4c 20 6f 6e 6c 79 29 0a 2a  NION ALL only).*
2559c 2a 20 20 20 20 45 6f 66 41 3a 20 2e 2e 2e 0a 2a  *    EofA: ....*
2559d 2a 20 20 20 20 45 6f 66 42 3a 20 2e 2e 2e 0a 2a  *    EofB: ....*
2559e 2a 20 20 20 20 41 6c 74 42 3a 20 2e 2e 2e 0a 2a  *    AltB: ....*
2559f 2a 20 20 20 20 41 65 71 42 3a 20 2e 2e 2e 0a 2a  *    AeqB: ....*
255a0 2a 20 20 20 20 41 67 74 42 3a 20 2e 2e 2e 0a 2a  *    AgtB: ....*
255a1 2a 20 20 20 20 49 6e 69 74 3a 20 69 6e 69 74 69  *    Init: initi
255a2 61 6c 69 7a 65 20 63 6f 72 6f 75 74 69 6e 65 20  alize coroutine 
255a3 72 65 67 69 73 74 65 72 73 0a 2a 2a 20 20 20 20  registers.**    
255a4 20 20 20 20 20 20 79 69 65 6c 64 20 63 6f 41 0a        yield coA.
255a5 2a 2a 20 20 20 20 20 20 20 20 20 20 69 66 20 65  **          if e
255a6 6f 66 28 41 29 20 67 6f 74 6f 20 45 6f 66 41 0a  of(A) goto EofA.
255a7 2a 2a 20 20 20 20 20 20 20 20 20 20 79 69 65 6c  **          yiel
255a8 64 20 63 6f 42 0a 2a 2a 20 20 20 20 20 20 20 20  d coB.**        
255a9 20 20 69 66 20 65 6f 66 28 42 29 20 67 6f 74 6f    if eof(B) goto
255aa 20 45 6f 66 42 0a 2a 2a 20 20 20 20 43 6d 70 72   EofB.**    Cmpr
255ab 3a 20 43 6f 6d 70 61 72 65 20 41 2c 20 42 0a 2a  : Compare A, B.*
255ac 2a 20 20 20 20 20 20 20 20 20 20 4a 75 6d 70 20  *          Jump 
255ad 41 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42  AltB, AeqB, AgtB
255ae 0a 2a 2a 20 20 20 20 20 45 6e 64 3a 20 2e 2e 2e  .**     End: ...
255af 0a 2a 2a 0a 2a 2a 20 57 65 20 63 61 6c 6c 20 41  .**.** We call A
255b0 6c 74 42 2c 20 41 65 71 42 2c 20 41 67 74 42 2c  ltB, AeqB, AgtB,
255b1 20 45 6f 66 41 2c 20 61 6e 64 20 45 6f 66 42 20   EofA, and EofB 
255b2 22 73 75 62 72 6f 75 74 69 6e 65 73 22 20 62 75  "subroutines" bu
255b3 74 20 74 68 65 79 20 61 72 65 20 6e 6f 74 0a 2a  t they are not.*
255b4 2a 20 61 63 74 75 61 6c 6c 79 20 63 61 6c 6c 65  * actually calle
255b5 64 20 75 73 69 6e 67 20 47 6f 73 75 62 20 61 6e  d using Gosub an
255b6 64 20 74 68 65 79 20 64 6f 20 6e 6f 74 20 52 65  d they do not Re
255b7 74 75 72 6e 2e 20 20 45 6f 66 41 20 61 6e 64 20  turn.  EofA and 
255b8 45 6f 66 42 20 6c 6f 6f 70 0a 2a 2a 20 75 6e 74  EofB loop.** unt
255b9 69 6c 20 61 6c 6c 20 64 61 74 61 20 69 73 20 65  il all data is e
255ba 78 68 61 75 73 74 65 64 20 74 68 65 6e 20 6a 75  xhausted then ju
255bb 6d 70 20 74 6f 20 74 68 65 20 22 65 6e 64 22 20  mp to the "end" 
255bc 6c 61 62 65 2e 20 20 41 6c 74 42 2c 20 41 65 71  labe.  AltB, Aeq
255bd 42 2c 0a 2a 2a 20 61 6e 64 20 41 67 74 42 20 6a  B,.** and AgtB j
255be 75 6d 70 20 74 6f 20 65 69 74 68 65 72 20 4c 32  ump to either L2
255bf 20 6f 72 20 74 6f 20 6f 6e 65 20 6f 66 20 45 6f   or to one of Eo
255c0 66 41 20 6f 72 20 45 6f 66 42 2e 0a 2a 2f 0a 23  fA or EofB..*/.#
255c1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
255c2 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  IT_COMPOUND_SELE
255c3 43 54 0a 73 74 61 74 69 63 20 69 6e 74 20 6d 75  CT.static int mu
255c4 6c 74 69 53 65 6c 65 63 74 4f 72 64 65 72 42 79  ltiSelectOrderBy
255c5 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  (.  Parse *pPars
255c6 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72  e,        /* Par
255c7 73 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a  sing context */.
255c8 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20 20 20    Select *p,    
255c9 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72          /* The r
255ca 69 67 68 74 2d 6d 6f 73 74 20 6f 66 20 53 45 4c  ight-most of SEL
255cb 45 43 54 73 20 74 6f 20 62 65 20 63 6f 64 65 64  ECTs to be coded
255cc 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74   */.  SelectDest
255cd 20 2a 70 44 65 73 74 20 20 20 20 20 2f 2a 20 57   *pDest     /* W
255ce 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 71  hat to do with q
255cf 75 65 72 79 20 72 65 73 75 6c 74 73 20 2a 2f 0a  uery results */.
255d0 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
255d1 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f             /* Lo
255d2 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  op counters */. 
255d3 20 53 65 6c 65 63 74 20 2a 70 50 72 69 6f 72 3b   Select *pPrior;
255d4 20 20 20 20 20 20 20 2f 2a 20 41 6e 6f 74 68 65         /* Anothe
255d5 72 20 53 45 4c 45 43 54 20 69 6d 6d 65 64 69 61  r SELECT immedia
255d6 74 65 6c 79 20 74 6f 20 6f 75 72 20 6c 65 66 74  tely to our left
255d7 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20   */.  Vdbe *v;  
255d8 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 47              /* G
255d9 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f 20  enerate code to 
255da 74 68 69 73 20 56 44 42 45 20 2a 2f 0a 20 20 53  this VDBE */.  S
255db 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 41 3b  electDest destA;
255dc 20 20 20 20 20 2f 2a 20 44 65 73 74 69 6e 61 74       /* Destinat
255dd 69 6f 6e 20 66 6f 72 20 63 6f 72 6f 75 74 69 6e  ion for coroutin
255de 65 20 41 20 2a 2f 0a 20 20 53 65 6c 65 63 74 44  e A */.  SelectD
255df 65 73 74 20 64 65 73 74 42 3b 20 20 20 20 20 2f  est destB;     /
255e0 2a 20 44 65 73 74 69 6e 61 74 69 6f 6e 20 66 6f  * Destination fo
255e1 72 20 63 6f 72 6f 75 74 69 6e 65 20 42 20 2a 2f  r coroutine B */
255e2 0a 20 20 69 6e 74 20 72 65 67 41 64 64 72 41 3b  .  int regAddrA;
255e3 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72           /* Addr
255e4 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
255e5 20 73 65 6c 65 63 74 2d 41 20 63 6f 72 6f 75 74   select-A corout
255e6 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67  ine */.  int reg
255e7 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20 20 2f  EofA;          /
255e8 2a 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61  * Flag to indica
255e9 74 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 41  te when select-A
255ea 20 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a   is complete */.
255eb 20 20 69 6e 74 20 72 65 67 41 64 64 72 42 3b 20    int regAddrB; 
255ec 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65          /* Addre
255ed 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  ss register for 
255ee 73 65 6c 65 63 74 2d 42 20 63 6f 72 6f 75 74 69  select-B corouti
255ef 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 45  ne */.  int regE
255f0 6f 66 42 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ofB;          /*
255f1 20 46 6c 61 67 20 74 6f 20 69 6e 64 69 63 61 74   Flag to indicat
255f2 65 20 77 68 65 6e 20 73 65 6c 65 63 74 2d 42 20  e when select-B 
255f3 69 73 20 63 6f 6d 70 6c 65 74 65 20 2a 2f 0a 20  is complete */. 
255f4 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 41   int addrSelectA
255f5 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
255f6 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d  s of the select-
255f7 41 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20  A coroutine */. 
255f8 20 69 6e 74 20 61 64 64 72 53 65 6c 65 63 74 42   int addrSelectB
255f9 3b 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73  ;      /* Addres
255fa 73 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2d  s of the select-
255fb 42 20 63 6f 72 6f 75 74 69 6e 65 20 2a 2f 0a 20  B coroutine */. 
255fc 20 69 6e 74 20 72 65 67 4f 75 74 41 3b 20 20 20   int regOutA;   
255fd 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
255fe 73 20 72 65 67 69 73 74 65 72 20 66 6f 72 20 74  s register for t
255ff 68 65 20 6f 75 74 70 75 74 2d 41 20 73 75 62 72  he output-A subr
25600 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20  outine */.  int 
25601 72 65 67 4f 75 74 42 3b 20 20 20 20 20 20 20 20  regOutB;        
25602 20 20 2f 2a 20 41 64 64 72 65 73 73 20 72 65 67    /* Address reg
25603 69 73 74 65 72 20 66 6f 72 20 74 68 65 20 6f 75  ister for the ou
25604 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74 69 6e  tput-B subroutin
25605 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 4f  e */.  int addrO
25606 75 74 41 3b 20 20 20 20 20 20 20 20 20 2f 2a 20  utA;         /* 
25607 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20 6f  Address of the o
25608 75 74 70 75 74 2d 41 20 73 75 62 72 6f 75 74 69  utput-A subrouti
25609 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2560a 4f 75 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  OutB;         /*
2560b 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
2560c 6f 75 74 70 75 74 2d 42 20 73 75 62 72 6f 75 74  output-B subrout
2560d 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ine */.  int add
2560e 72 45 6f 66 41 3b 20 20 20 20 20 20 20 20 20 2f  rEofA;         /
2560f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65  * Address of the
25610 20 73 65 6c 65 63 74 2d 41 2d 65 78 68 61 75 73   select-A-exhaus
25611 74 65 64 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  ted subroutine *
25612 2f 0a 20 20 69 6e 74 20 61 64 64 72 45 6f 66 42  /.  int addrEofB
25613 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 41 64 64  ;         /* Add
25614 72 65 73 73 20 6f 66 20 74 68 65 20 73 65 6c 65  ress of the sele
25615 63 74 2d 42 2d 65 78 68 61 75 73 74 65 64 20 73  ct-B-exhausted s
25616 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a 20 20 69  ubroutine */.  i
25617 6e 74 20 61 64 64 72 41 6c 74 42 3b 20 20 20 20  nt addrAltB;    
25618 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20       /* Address 
25619 6f 66 20 74 68 65 20 41 3c 42 20 73 75 62 72 6f  of the A<B subro
2561a 75 74 69 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61  utine */.  int a
2561b 64 64 72 41 65 71 42 3b 20 20 20 20 20 20 20 20  ddrAeqB;        
2561c 20 2f 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74   /* Address of t
2561d 68 65 20 41 3d 3d 42 20 73 75 62 72 6f 75 74 69  he A==B subrouti
2561e 6e 65 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72  ne */.  int addr
2561f 41 67 74 42 3b 20 20 20 20 20 20 20 20 20 2f 2a  AgtB;         /*
25620 20 41 64 64 72 65 73 73 20 6f 66 20 74 68 65 20   Address of the 
25621 41 3e 42 20 73 75 62 72 6f 75 74 69 6e 65 20 2a  A>B subroutine *
25622 2f 0a 20 20 69 6e 74 20 72 65 67 4c 69 6d 69 74  /.  int regLimit
25623 41 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c 69 6d  A;        /* Lim
25624 69 74 20 72 65 67 69 73 74 65 72 20 66 6f 72 20  it register for 
25625 73 65 6c 65 63 74 2d 41 20 2a 2f 0a 20 20 69 6e  select-A */.  in
25626 74 20 72 65 67 4c 69 6d 69 74 42 3b 20 20 20 20  t regLimitB;    
25627 20 20 20 20 2f 2a 20 4c 69 6d 69 74 20 72 65 67      /* Limit reg
25628 69 73 74 65 72 20 66 6f 72 20 73 65 6c 65 63 74  ister for select
25629 2d 41 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 50  -A */.  int regP
2562a 72 65 76 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  rev;          /*
2562b 20 41 20 72 61 6e 67 65 20 6f 66 20 72 65 67 69   A range of regi
2562c 73 74 65 72 73 20 74 6f 20 68 6f 6c 64 20 70 72  sters to hold pr
2562d 65 76 69 6f 75 73 20 6f 75 74 70 75 74 20 2a 2f  evious output */
2562e 0a 20 20 69 6e 74 20 73 61 76 65 64 4c 69 6d 69  .  int savedLimi
2562f 74 3b 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65  t;       /* Save
25630 64 20 76 61 6c 75 65 20 6f 66 20 70 2d 3e 69 4c  d value of p->iL
25631 69 6d 69 74 20 2a 2f 0a 20 20 69 6e 74 20 73 61  imit */.  int sa
25632 76 65 64 4f 66 66 73 65 74 3b 20 20 20 20 20 20  vedOffset;      
25633 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20 6f  /* Saved value o
25634 66 20 70 2d 3e 69 4f 66 66 73 65 74 20 2a 2f 0a  f p->iOffset */.
25635 20 20 69 6e 74 20 6c 61 62 65 6c 43 6d 70 72 3b    int labelCmpr;
25636 20 20 20 20 20 20 20 20 2f 2a 20 4c 61 62 65 6c          /* Label
25637 20 66 6f 72 20 74 68 65 20 73 74 61 72 74 20 6f   for the start o
25638 66 20 74 68 65 20 6d 65 72 67 65 20 61 6c 67 6f  f the merge algo
25639 72 69 74 68 6d 20 2a 2f 0a 20 20 69 6e 74 20 6c  rithm */.  int l
2563a 61 62 65 6c 45 6e 64 3b 20 20 20 20 20 20 20 20  abelEnd;        
2563b 20 2f 2a 20 4c 61 62 65 6c 20 66 6f 72 20 74 68   /* Label for th
2563c 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 76 65  e end of the ove
2563d 72 61 6c 6c 20 53 45 4c 45 43 54 20 73 74 6d 74  rall SELECT stmt
2563e 20 2a 2f 0a 20 20 69 6e 74 20 6a 31 3b 20 20 20   */.  int j1;   
2563f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4a              /* J
25640 75 6d 70 20 69 6e 73 74 72 75 63 74 69 6f 6e 73  ump instructions
25641 20 74 68 61 74 20 67 65 74 20 72 65 74 61 72 67   that get retarg
25642 65 74 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 6f  etted */.  int o
25643 70 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p;              
25644 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b 5f 41 4c   /* One of TK_AL
25645 4c 2c 20 54 4b 5f 55 4e 49 4f 4e 2c 20 54 4b 5f  L, TK_UNION, TK_
25646 45 58 43 45 50 54 2c 20 54 4b 5f 49 4e 54 45 52  EXCEPT, TK_INTER
25647 53 45 43 54 20 2a 2f 0a 20 20 4b 65 79 49 6e 66  SECT */.  KeyInf
25648 6f 20 2a 70 4b 65 79 44 75 70 20 3d 20 30 3b 20  o *pKeyDup = 0; 
25649 2f 2a 20 43 6f 6d 70 61 72 69 73 6f 6e 20 69 6e  /* Comparison in
2564a 66 6f 72 6d 61 74 69 6f 6e 20 66 6f 72 20 64 75  formation for du
2564b 70 6c 69 63 61 74 65 20 72 65 6d 6f 76 61 6c 20  plicate removal 
2564c 2a 2f 0a 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b  */.  KeyInfo *pK
2564d 65 79 4d 65 72 67 65 3b 20 20 20 2f 2a 20 43 6f  eyMerge;   /* Co
2564e 6d 70 61 72 69 73 6f 6e 20 69 6e 66 6f 72 6d 61  mparison informa
2564f 74 69 6f 6e 20 66 6f 72 20 6d 65 72 67 69 6e 67  tion for merging
25650 20 72 6f 77 73 20 2a 2f 0a 20 20 73 71 6c 69 74   rows */.  sqlit
25651 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20  e3 *db;         
25652 20 2f 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e   /* Database con
25653 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 45 78 70  nection */.  Exp
25654 72 4c 69 73 74 20 2a 70 4f 72 64 65 72 42 79 3b  rList *pOrderBy;
25655 20 20 20 2f 2a 20 54 68 65 20 4f 52 44 45 52 20     /* The ORDER 
25656 42 59 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 69  BY clause */.  i
25657 6e 74 20 6e 4f 72 64 65 72 42 79 3b 20 20 20 20  nt nOrderBy;    
25658 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
25659 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 4f  f terms in the O
2565a 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a  RDER BY clause *
2565b 2f 0a 20 20 69 6e 74 20 2a 61 50 65 72 6d 75 74  /.  int *aPermut
2565c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 70  e;        /* Map
2565d 70 69 6e 67 20 66 72 6f 6d 20 4f 52 44 45 52 20  ping from ORDER 
2565e 42 59 20 74 65 72 6d 73 20 74 6f 20 72 65 73 75  BY terms to resu
2565f 6c 74 20 73 65 74 20 63 6f 6c 75 6d 6e 73 20 2a  lt set columns *
25660 2f 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e  /..  assert( p->
25661 70 4f 72 64 65 72 42 79 21 3d 30 20 29 3b 0a 20  pOrderBy!=0 );. 
25662 20 61 73 73 65 72 74 28 20 70 4b 65 79 44 75 70   assert( pKeyDup
25663 3d 3d 30 20 29 3b 20 2f 2a 20 22 4d 61 6e 61 67  ==0 ); /* "Manag
25664 65 64 22 20 63 6f 64 65 20 6e 65 65 64 73 20 74  ed" code needs t
25665 68 69 73 2e 20 20 54 69 63 6b 65 74 20 23 33 33  his.  Ticket #33
25666 38 32 2e 20 2a 2f 0a 20 20 64 62 20 3d 20 70 50  82. */.  db = pP
25667 61 72 73 65 2d 3e 64 62 3b 0a 20 20 76 20 3d 20  arse->db;.  v = 
25668 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
25669 20 69 66 28 20 76 3d 3d 30 20 29 20 72 65 74 75   if( v==0 ) retu
2566a 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2566b 0a 20 20 6c 61 62 65 6c 45 6e 64 20 3d 20 73 71  .  labelEnd = sq
2566c 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61 62  lite3VdbeMakeLab
2566d 65 6c 28 76 29 3b 0a 20 20 6c 61 62 65 6c 43 6d  el(v);.  labelCm
2566e 70 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  pr = sqlite3Vdbe
2566f 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 0a  MakeLabel(v);...
25670 20 20 2f 2a 20 50 61 74 63 68 20 75 70 20 74 68    /* Patch up th
25671 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
25672 65 0a 20 20 2a 2f 0a 20 20 6f 70 20 3d 20 70 2d  e.  */.  op = p-
25673 3e 6f 70 3b 20 20 0a 20 20 70 50 72 69 6f 72 20  >op;  .  pPrior 
25674 3d 20 70 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 61  = p->pPrior;.  a
25675 73 73 65 72 74 28 20 70 50 72 69 6f 72 2d 3e 70  ssert( pPrior->p
25676 4f 72 64 65 72 42 79 3d 3d 30 20 29 3b 0a 20 20  OrderBy==0 );.  
25677 70 4f 72 64 65 72 42 79 20 3d 20 70 2d 3e 70 4f  pOrderBy = p->pO
25678 72 64 65 72 42 79 3b 0a 20 20 61 73 73 65 72 74  rderBy;.  assert
25679 28 20 70 4f 72 64 65 72 42 79 20 29 3b 0a 20 20  ( pOrderBy );.  
2567a 6e 4f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65  nOrderBy = pOrde
2567b 72 42 79 2d 3e 6e 45 78 70 72 3b 0a 0a 20 20 2f  rBy->nExpr;..  /
2567c 2a 20 46 6f 72 20 6f 70 65 72 61 74 6f 72 73 20  * For operators 
2567d 6f 74 68 65 72 20 74 68 61 6e 20 55 4e 49 4f 4e  other than UNION
2567e 20 41 4c 4c 20 77 65 20 68 61 76 65 20 74 6f 20   ALL we have to 
2567f 6d 61 6b 65 20 73 75 72 65 20 74 68 61 74 0a 20  make sure that. 
25680 20 2a 2a 20 74 68 65 20 4f 52 44 45 52 20 42 59   ** the ORDER BY
25681 20 63 6c 61 75 73 65 20 63 6f 76 65 72 73 20 65   clause covers e
25682 76 65 72 79 20 74 65 72 6d 20 6f 66 20 74 68 65  very term of the
25683 20 72 65 73 75 6c 74 20 73 65 74 2e 20 20 41 64   result set.  Ad
25684 64 0a 20 20 2a 2a 20 74 65 72 6d 73 20 74 6f 20  d.  ** terms to 
25685 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
25686 75 73 65 20 61 73 20 6e 65 63 65 73 73 61 72 79  use as necessary
25687 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 21  ..  */.  if( op!
25688 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 66  =TK_ALL ){.    f
25689 6f 72 28 69 3d 31 3b 20 64 62 2d 3e 6d 61 6c 6c  or(i=1; db->mall
2568a 6f 63 46 61 69 6c 65 64 3d 3d 30 20 26 26 20 69  ocFailed==0 && i
2568b 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e 45 78  <=p->pEList->nEx
2568c 70 72 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  pr; i++){.      
2568d 73 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f  struct ExprList_
2568e 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20  item *pItem;.   
2568f 20 20 20 66 6f 72 28 6a 3d 30 2c 20 70 49 74 65     for(j=0, pIte
25690 6d 3d 70 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a  m=pOrderBy->a; j
25691 3c 6e 4f 72 64 65 72 42 79 3b 20 6a 2b 2b 2c 20  <nOrderBy; j++, 
25692 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20  pItem++){.      
25693 20 20 61 73 73 65 72 74 28 20 70 49 74 65 6d 2d    assert( pItem-
25694 3e 69 43 6f 6c 3e 30 20 29 3b 0a 20 20 20 20 20  >iCol>0 );.     
25695 20 20 20 69 66 28 20 70 49 74 65 6d 2d 3e 69 43     if( pItem->iC
25696 6f 6c 3d 3d 69 20 29 20 62 72 65 61 6b 3b 0a 20  ol==i ) break;. 
25697 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
25698 20 6a 3d 3d 6e 4f 72 64 65 72 42 79 20 29 7b 0a   j==nOrderBy ){.
25699 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e          Expr *pN
2569a 65 77 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  ew = sqlite3PExp
2569b 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 4e 54  r(pParse, TK_INT
2569c 45 47 45 52 2c 20 30 2c 20 30 2c 20 30 29 3b 0a  EGER, 0, 0, 0);.
2569d 20 20 20 20 20 20 20 20 69 66 28 20 70 4e 65 77          if( pNew
2569e 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ==0 ) return SQL
2569f 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20  ITE_NOMEM;.     
256a0 20 20 20 70 4e 65 77 2d 3e 66 6c 61 67 73 20 7c     pNew->flags |
256a1 3d 20 45 50 5f 49 6e 74 56 61 6c 75 65 3b 0a 20  = EP_IntValue;. 
256a2 20 20 20 20 20 20 20 70 4e 65 77 2d 3e 69 54 61         pNew->iTa
256a3 62 6c 65 20 3d 20 69 3b 0a 20 20 20 20 20 20 20  ble = i;.       
256a4 20 70 4f 72 64 65 72 42 79 20 3d 20 73 71 6c 69   pOrderBy = sqli
256a5 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e  te3ExprListAppen
256a6 64 28 70 50 61 72 73 65 2c 20 70 4f 72 64 65 72  d(pParse, pOrder
256a7 42 79 2c 20 70 4e 65 77 2c 20 30 29 3b 0a 20 20  By, pNew, 0);.  
256a8 20 20 20 20 20 20 70 4f 72 64 65 72 42 79 2d 3e        pOrderBy->
256a9 61 5b 6e 4f 72 64 65 72 42 79 2b 2b 5d 2e 69 43  a[nOrderBy++].iC
256aa 6f 6c 20 3d 20 69 3b 0a 20 20 20 20 20 20 7d 0a  ol = i;.      }.
256ab 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
256ac 43 6f 6d 70 75 74 65 20 74 68 65 20 63 6f 6d 70  Compute the comp
256ad 61 72 69 73 6f 6e 20 70 65 72 6d 75 74 61 74 69  arison permutati
256ae 6f 6e 20 61 6e 64 20 6b 65 79 69 6e 66 6f 20 74  on and keyinfo t
256af 68 61 74 20 69 73 20 75 73 65 64 20 77 69 74 68  hat is used with
256b0 0a 20 20 2a 2a 20 74 68 65 20 70 65 72 6d 75 74  .  ** the permut
256b1 61 74 69 6f 6e 20 69 6e 20 6f 72 64 65 72 20 74  ation in order t
256b2 6f 20 63 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f  o comparisons to
256b3 20 64 65 74 65 72 6d 69 6e 65 20 69 66 20 74 68   determine if th
256b4 65 20 6e 65 78 74 0a 20 20 2a 2a 20 72 6f 77 20  e next.  ** row 
256b5 6f 66 20 72 65 73 75 6c 74 73 20 63 6f 6d 65 73  of results comes
256b6 20 66 72 6f 6d 20 73 65 6c 65 63 74 41 20 6f 72   from selectA or
256b7 20 73 65 6c 65 63 74 42 2e 20 20 41 6c 73 6f 20   selectB.  Also 
256b8 61 64 64 20 65 78 70 6c 69 63 69 74 0a 20 20 2a  add explicit.  *
256b9 2a 20 63 6f 6c 6c 61 74 69 6f 6e 73 20 74 6f 20  * collations to 
256ba 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  the ORDER BY cla
256bb 75 73 65 20 74 65 72 6d 73 20 73 6f 20 74 68 61  use terms so tha
256bc 74 20 77 68 65 6e 20 74 68 65 20 73 75 62 71 75  t when the subqu
256bd 65 72 69 65 73 0a 20 20 2a 2a 20 74 6f 20 74 68  eries.  ** to th
256be 65 20 72 69 67 68 74 20 61 6e 64 20 74 68 65 20  e right and the 
256bf 6c 65 66 74 20 61 72 65 20 65 76 61 6c 75 61 74  left are evaluat
256c0 65 64 2c 20 74 68 65 79 20 75 73 65 20 74 68 65  ed, they use the
256c1 20 63 6f 72 72 65 63 74 0a 20 20 2a 2a 20 63 6f   correct.  ** co
256c2 6c 6c 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  llation..  */.  
256c3 61 50 65 72 6d 75 74 65 20 3d 20 73 71 6c 69 74  aPermute = sqlit
256c4 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62  e3DbMallocRaw(db
256c5 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4f  , sizeof(int)*nO
256c6 72 64 65 72 42 79 29 3b 0a 20 20 69 66 28 20 61  rderBy);.  if( a
256c7 50 65 72 6d 75 74 65 20 29 7b 0a 20 20 20 20 73  Permute ){.    s
256c8 74 72 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69  truct ExprList_i
256c9 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 20 20  tem *pItem;.    
256ca 66 6f 72 28 69 3d 30 2c 20 70 49 74 65 6d 3d 70  for(i=0, pItem=p
256cb 4f 72 64 65 72 42 79 2d 3e 61 3b 20 69 3c 6e 4f  OrderBy->a; i<nO
256cc 72 64 65 72 42 79 3b 20 69 2b 2b 2c 20 70 49 74  rderBy; i++, pIt
256cd 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 61 73 73  em++){.      ass
256ce 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 43 6f 6c  ert( pItem->iCol
256cf 3e 30 20 20 26 26 20 70 49 74 65 6d 2d 3e 69 43  >0  && pItem->iC
256d0 6f 6c 3c 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  ol<=p->pEList->n
256d1 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61 50  Expr );.      aP
256d2 65 72 6d 75 74 65 5b 69 5d 20 3d 20 70 49 74 65  ermute[i] = pIte
256d3 6d 2d 3e 69 43 6f 6c 20 2d 20 31 3b 0a 20 20 20  m->iCol - 1;.   
256d4 20 7d 0a 20 20 20 20 70 4b 65 79 4d 65 72 67 65   }.    pKeyMerge
256d5 20 3d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   =.      sqlite3
256d6 44 62 4d 61 6c 6c 6f 63 52 61 77 28 64 62 2c 20  DbMallocRaw(db, 
256d7 73 69 7a 65 6f 66 28 2a 70 4b 65 79 4d 65 72 67  sizeof(*pKeyMerg
256d8 65 29 2b 6e 4f 72 64 65 72 42 79 2a 28 73 69 7a  e)+nOrderBy*(siz
256d9 65 6f 66 28 43 6f 6c 6c 53 65 71 2a 29 2b 31 29  eof(CollSeq*)+1)
256da 29 3b 0a 20 20 20 20 69 66 28 20 70 4b 65 79 4d  );.    if( pKeyM
256db 65 72 67 65 20 29 7b 0a 20 20 20 20 20 20 70 4b  erge ){.      pK
256dc 65 79 4d 65 72 67 65 2d 3e 61 53 6f 72 74 4f 72  eyMerge->aSortOr
256dd 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b 65 79  der = (u8*)&pKey
256de 4d 65 72 67 65 2d 3e 61 43 6f 6c 6c 5b 6e 4f 72  Merge->aColl[nOr
256df 64 65 72 42 79 5d 3b 0a 20 20 20 20 20 20 70 4b  derBy];.      pK
256e0 65 79 4d 65 72 67 65 2d 3e 6e 46 69 65 6c 64 20  eyMerge->nField 
256e1 3d 20 6e 4f 72 64 65 72 42 79 3b 0a 20 20 20 20  = nOrderBy;.    
256e2 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e 65 6e 63    pKeyMerge->enc
256e3 20 3d 20 45 4e 43 28 64 62 29 3b 0a 20 20 20 20   = ENC(db);.    
256e4 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 4f 72    for(i=0; i<nOr
256e5 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20 20 20  derBy; i++){.   
256e6 20 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43       CollSeq *pC
256e7 6f 6c 6c 3b 0a 20 20 20 20 20 20 20 20 45 78 70  oll;.        Exp
256e8 72 20 2a 70 54 65 72 6d 20 3d 20 70 4f 72 64 65  r *pTerm = pOrde
256e9 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
256ea 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 54 65  .        if( pTe
256eb 72 6d 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 45  rm->flags & EP_E
256ec 78 70 43 6f 6c 6c 61 74 65 20 29 7b 0a 20 20 20  xpCollate ){.   
256ed 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70         pColl = p
256ee 54 65 72 6d 2d 3e 70 43 6f 6c 6c 3b 0a 20 20 20  Term->pColl;.   
256ef 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
256f0 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 6d 75        pColl = mu
256f1 6c 74 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71  ltiSelectCollSeq
256f2 28 70 50 61 72 73 65 2c 20 70 2c 20 61 50 65 72  (pParse, p, aPer
256f3 6d 75 74 65 5b 69 5d 29 3b 0a 20 20 20 20 20 20  mute[i]);.      
256f4 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73      pTerm->flags
256f5 20 7c 3d 20 45 50 5f 45 78 70 43 6f 6c 6c 61 74   |= EP_ExpCollat
256f6 65 3b 0a 20 20 20 20 20 20 20 20 20 20 70 54 65  e;.          pTe
256f7 72 6d 2d 3e 70 43 6f 6c 6c 20 3d 20 70 43 6f 6c  rm->pColl = pCol
256f8 6c 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  l;.        }.   
256f9 20 20 20 20 20 70 4b 65 79 4d 65 72 67 65 2d 3e       pKeyMerge->
256fa 61 43 6f 6c 6c 5b 69 5d 20 3d 20 70 43 6f 6c 6c  aColl[i] = pColl
256fb 3b 0a 20 20 20 20 20 20 20 20 70 4b 65 79 4d 65  ;.        pKeyMe
256fc 72 67 65 2d 3e 61 53 6f 72 74 4f 72 64 65 72 5b  rge->aSortOrder[
256fd 69 5d 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61  i] = pOrderBy->a
256fe 5b 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20  [i].sortOrder;. 
256ff 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
25700 65 6c 73 65 7b 0a 20 20 20 20 70 4b 65 79 4d 65  else{.    pKeyMe
25701 72 67 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rge = 0;.  }..  
25702 2f 2a 20 52 65 61 74 74 61 63 68 20 74 68 65 20  /* Reattach the 
25703 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
25704 74 6f 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20  to the query..  
25705 2a 2f 0a 20 20 70 2d 3e 70 4f 72 64 65 72 42 79  */.  p->pOrderBy
25706 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 70   = pOrderBy;.  p
25707 50 72 69 6f 72 2d 3e 70 4f 72 64 65 72 42 79 20  Prior->pOrderBy 
25708 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
25709 74 44 75 70 28 70 50 61 72 73 65 2d 3e 64 62 2c  tDup(pParse->db,
2570a 20 70 4f 72 64 65 72 42 79 29 3b 0a 0a 20 20 2f   pOrderBy);..  /
2570b 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 72 61 6e  * Allocate a ran
2570c 67 65 20 6f 66 20 74 65 6d 70 6f 72 61 72 79 20  ge of temporary 
2570d 72 65 67 69 73 74 65 72 73 20 61 6e 64 20 74 68  registers and th
2570e 65 20 4b 65 79 49 6e 66 6f 20 6e 65 65 64 65 64  e KeyInfo needed
2570f 0a 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6c 6f  .  ** for the lo
25710 67 69 63 20 74 68 61 74 20 72 65 6d 6f 76 65 73  gic that removes
25711 20 64 75 70 6c 69 63 61 74 65 20 72 65 73 75 6c   duplicate resul
25712 74 20 72 6f 77 73 20 77 68 65 6e 20 74 68 65 0a  t rows when the.
25713 20 20 2a 2a 20 6f 70 65 72 61 74 6f 72 20 69 73    ** operator is
25714 20 55 4e 49 4f 4e 2c 20 45 58 43 45 50 54 2c 20   UNION, EXCEPT, 
25715 6f 72 20 49 4e 54 45 52 53 45 43 54 20 28 62 75  or INTERSECT (bu
25716 74 20 6e 6f 74 20 55 4e 49 4f 4e 20 41 4c 4c 29  t not UNION ALL)
25717 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d  ..  */.  if( op=
25718 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20 20 20 20 72  =TK_ALL ){.    r
25719 65 67 50 72 65 76 20 3d 20 30 3b 0a 20 20 7d 65  egPrev = 0;.  }e
2571a 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 6e 45 78  lse{.    int nEx
2571b 70 72 20 3d 20 70 2d 3e 70 45 4c 69 73 74 2d 3e  pr = p->pEList->
2571c 6e 45 78 70 72 3b 0a 20 20 20 20 61 73 73 65 72  nExpr;.    asser
2571d 74 28 20 6e 4f 72 64 65 72 42 79 3e 3d 6e 45 78  t( nOrderBy>=nEx
2571e 70 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  pr || db->malloc
2571f 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20 72 65  Failed );.    re
25720 67 50 72 65 76 20 3d 20 73 71 6c 69 74 65 33 47  gPrev = sqlite3G
25721 65 74 54 65 6d 70 52 61 6e 67 65 28 70 50 61 72  etTempRange(pPar
25722 73 65 2c 20 6e 45 78 70 72 2b 31 29 3b 0a 20 20  se, nExpr+1);.  
25723 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
25724 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65  Op2(v, OP_Intege
25725 72 2c 20 30 2c 20 72 65 67 50 72 65 76 29 3b 0a  r, 0, regPrev);.
25726 20 20 20 20 70 4b 65 79 44 75 70 20 3d 20 73 71      pKeyDup = sq
25727 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72  lite3DbMallocZer
25728 6f 28 64 62 2c 0a 20 20 20 20 20 20 20 20 20 20  o(db,.          
25729 20 20 20 20 20 20 20 20 73 69 7a 65 6f 66 28 2a          sizeof(*
2572a 70 4b 65 79 44 75 70 29 20 2b 20 6e 45 78 70 72  pKeyDup) + nExpr
2572b 2a 28 73 69 7a 65 6f 66 28 43 6f 6c 6c 53 65 71  *(sizeof(CollSeq
2572c 2a 29 2b 31 29 20 29 3b 0a 20 20 20 20 69 66 28  *)+1) );.    if(
2572d 20 70 4b 65 79 44 75 70 20 29 7b 0a 20 20 20 20   pKeyDup ){.    
2572e 20 20 70 4b 65 79 44 75 70 2d 3e 61 53 6f 72 74    pKeyDup->aSort
2572f 4f 72 64 65 72 20 3d 20 28 75 38 2a 29 26 70 4b  Order = (u8*)&pK
25730 65 79 44 75 70 2d 3e 61 43 6f 6c 6c 5b 6e 45 78  eyDup->aColl[nEx
25731 70 72 5d 3b 0a 20 20 20 20 20 20 70 4b 65 79 44  pr];.      pKeyD
25732 75 70 2d 3e 6e 46 69 65 6c 64 20 3d 20 6e 45 78  up->nField = nEx
25733 70 72 3b 0a 20 20 20 20 20 20 70 4b 65 79 44 75  pr;.      pKeyDu
25734 70 2d 3e 65 6e 63 20 3d 20 45 4e 43 28 64 62 29  p->enc = ENC(db)
25735 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b  ;.      for(i=0;
25736 20 69 3c 6e 45 78 70 72 3b 20 69 2b 2b 29 7b 0a   i<nExpr; i++){.
25737 20 20 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d          pKeyDup-
25738 3e 61 43 6f 6c 6c 5b 69 5d 20 3d 20 6d 75 6c 74  >aColl[i] = mult
25739 69 53 65 6c 65 63 74 43 6f 6c 6c 53 65 71 28 70  iSelectCollSeq(p
2573a 50 61 72 73 65 2c 20 70 2c 20 69 29 3b 0a 20 20  Parse, p, i);.  
2573b 20 20 20 20 20 20 70 4b 65 79 44 75 70 2d 3e 61        pKeyDup->a
2573c 53 6f 72 74 4f 72 64 65 72 5b 69 5d 20 3d 20 30  SortOrder[i] = 0
2573d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2573e 20 20 7d 0a 20 0a 20 20 2f 2a 20 53 65 70 61 72    }. .  /* Separ
2573f 61 74 65 20 74 68 65 20 6c 65 66 74 20 61 6e 64  ate the left and
25740 20 74 68 65 20 72 69 67 68 74 20 71 75 65 72 79   the right query
25741 20 66 72 6f 6d 20 6f 6e 65 20 61 6e 6f 74 68 65   from one anothe
25742 72 0a 20 20 2a 2f 0a 20 20 70 2d 3e 70 50 72 69  r.  */.  p->pPri
25743 6f 72 20 3d 20 30 3b 0a 20 20 70 50 72 69 6f 72  or = 0;.  pPrior
25744 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 30  ->pRightmost = 0
25745 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  ;.  sqlite3Resol
25746 76 65 4f 72 64 65 72 47 72 6f 75 70 42 79 28 70  veOrderGroupBy(p
25747 50 61 72 73 65 2c 20 70 2c 20 70 2d 3e 70 4f 72  Parse, p, p->pOr
25748 64 65 72 42 79 2c 20 22 4f 52 44 45 52 22 29 3b  derBy, "ORDER");
25749 0a 20 20 69 66 28 20 70 50 72 69 6f 72 2d 3e 70  .  if( pPrior->p
2574a 50 72 69 6f 72 3d 3d 30 20 29 7b 0a 20 20 20 20  Prior==0 ){.    
2574b 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 4f 72  sqlite3ResolveOr
2574c 64 65 72 47 72 6f 75 70 42 79 28 70 50 61 72 73  derGroupBy(pPars
2574d 65 2c 20 70 50 72 69 6f 72 2c 20 70 50 72 69 6f  e, pPrior, pPrio
2574e 72 2d 3e 70 4f 72 64 65 72 42 79 2c 20 22 4f 52  r->pOrderBy, "OR
2574f 44 45 52 22 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  DER");.  }..  /*
25750 20 43 6f 6d 70 75 74 65 20 74 68 65 20 6c 69 6d   Compute the lim
25751 69 74 20 72 65 67 69 73 74 65 72 73 20 2a 2f 0a  it registers */.
25752 20 20 63 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65    computeLimitRe
25753 67 69 73 74 65 72 73 28 70 50 61 72 73 65 2c 20  gisters(pParse, 
25754 70 2c 20 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20  p, labelEnd);.  
25755 69 66 28 20 70 2d 3e 69 4c 69 6d 69 74 20 26 26  if( p->iLimit &&
25756 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 29 7b 0a 20   op==TK_ALL ){. 
25757 20 20 20 72 65 67 4c 69 6d 69 74 41 20 3d 20 2b     regLimitA = +
25758 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
25759 20 20 20 72 65 67 4c 69 6d 69 74 42 20 3d 20 2b     regLimitB = +
2575a 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20  +pParse->nMem;. 
2575b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2575c 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 70 79 2c  dOp2(v, OP_Copy,
2575d 20 70 2d 3e 69 4f 66 66 73 65 74 20 3f 20 70 2d   p->iOffset ? p-
2575e 3e 69 4f 66 66 73 65 74 2b 31 20 3a 20 70 2d 3e  >iOffset+1 : p->
2575f 69 4c 69 6d 69 74 2c 0a 20 20 20 20 20 20 20 20  iLimit,.        
25760 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25761 20 20 20 20 20 20 20 20 20 20 72 65 67 4c 69 6d            regLim
25762 69 74 41 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  itA);.    sqlite
25763 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25764 50 5f 43 6f 70 79 2c 20 72 65 67 4c 69 6d 69 74  P_Copy, regLimit
25765 41 2c 20 72 65 67 4c 69 6d 69 74 42 29 3b 0a 20  A, regLimitB);. 
25766 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 67 4c   }else{.    regL
25767 69 6d 69 74 41 20 3d 20 72 65 67 4c 69 6d 69 74  imitA = regLimit
25768 42 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  B = 0;.  }.  sql
25769 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
2576a 62 2c 20 70 2d 3e 70 4c 69 6d 69 74 29 3b 0a 20  b, p->pLimit);. 
2576b 20 70 2d 3e 70 4c 69 6d 69 74 20 3d 20 30 3b 0a   p->pLimit = 0;.
2576c 20 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c    sqlite3ExprDel
2576d 65 74 65 28 64 62 2c 20 70 2d 3e 70 4f 66 66 73  ete(db, p->pOffs
2576e 65 74 29 3b 0a 20 20 70 2d 3e 70 4f 66 66 73 65  et);.  p->pOffse
2576f 74 20 3d 20 30 3b 0a 0a 20 20 72 65 67 41 64 64  t = 0;..  regAdd
25770 72 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e  rA = ++pParse->n
25771 4d 65 6d 3b 0a 20 20 72 65 67 45 6f 66 41 20 3d  Mem;.  regEofA =
25772 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
25773 0a 20 20 72 65 67 41 64 64 72 42 20 3d 20 2b 2b  .  regAddrB = ++
25774 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
25775 72 65 67 45 6f 66 42 20 3d 20 2b 2b 70 50 61 72  regEofB = ++pPar
25776 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f  se->nMem;.  regO
25777 75 74 41 20 3d 20 2b 2b 70 50 61 72 73 65 2d 3e  utA = ++pParse->
25778 6e 4d 65 6d 3b 0a 20 20 72 65 67 4f 75 74 42 20  nMem;.  regOutB 
25779 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
2577a 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63  ;.  sqlite3Selec
2577b 74 44 65 73 74 49 6e 69 74 28 26 64 65 73 74 41  tDestInit(&destA
2577c 2c 20 53 52 54 5f 43 6f 72 6f 75 74 69 6e 65 2c  , SRT_Coroutine,
2577d 20 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71   regAddrA);.  sq
2577e 6c 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49  lite3SelectDestI
2577f 6e 69 74 28 26 64 65 73 74 42 2c 20 53 52 54 5f  nit(&destB, SRT_
25780 43 6f 72 6f 75 74 69 6e 65 2c 20 72 65 67 41 64  Coroutine, regAd
25781 64 72 42 29 3b 0a 0a 20 20 2f 2a 20 4a 75 6d 70  drB);..  /* Jump
25782 20 70 61 73 74 20 74 68 65 20 76 61 72 69 6f 75   past the variou
25783 73 20 73 75 62 72 6f 75 74 69 6e 65 73 20 61 6e  s subroutines an
25784 64 20 63 6f 72 6f 75 74 69 6e 65 73 20 74 6f 20  d coroutines to 
25785 74 68 65 20 6d 61 69 6e 0a 20 20 2a 2a 20 6d 65  the main.  ** me
25786 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20  rge loop.  */.  
25787 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  j1 = sqlite3Vdbe
25788 41 64 64 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74  AddOp0(v, OP_Got
25789 6f 29 3b 0a 20 20 61 64 64 72 53 65 6c 65 63 74  o);.  addrSelect
2578a 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  A = sqlite3VdbeC
2578b 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a  urrentAddr(v);..
2578c 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61  .  /* Generate a
2578d 20 63 6f 72 6f 75 74 69 6e 65 20 74 6f 20 65 76   coroutine to ev
2578e 61 6c 75 61 74 65 20 74 68 65 20 53 45 4c 45 43  aluate the SELEC
2578f 54 20 73 74 61 74 65 6d 65 6e 74 20 74 6f 20 74  T statement to t
25790 68 65 0a 20 20 2a 2a 20 6c 65 66 74 20 6f 66 20  he.  ** left of 
25791 74 68 65 20 63 6f 6d 70 6f 75 6e 64 20 6f 70 65  the compound ope
25792 72 61 74 6f 72 20 2d 20 74 68 65 20 22 41 22 20  rator - the "A" 
25793 73 65 6c 65 63 74 2e 0a 20 20 2a 2f 0a 20 20 56  select..  */.  V
25794 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28  dbeNoopComment((
25795 76 2c 20 22 42 65 67 69 6e 20 63 6f 72 6f 75 74  v, "Begin corout
25796 69 6e 65 20 66 6f 72 20 6c 65 66 74 20 53 45 4c  ine for left SEL
25797 45 43 54 22 29 29 3b 0a 20 20 70 50 72 69 6f 72  ECT"));.  pPrior
25798 2d 3e 69 4c 69 6d 69 74 20 3d 20 72 65 67 4c 69  ->iLimit = regLi
25799 6d 69 74 41 3b 0a 20 20 73 71 6c 69 74 65 33 53  mitA;.  sqlite3S
2579a 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20 70 50  elect(pParse, pP
2579b 72 69 6f 72 2c 20 26 64 65 73 74 41 29 3b 0a 20  rior, &destA);. 
2579c 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2579d 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2579e 2c 20 31 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20  , 1, regEofA);. 
2579f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
257a0 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
257a1 72 65 67 41 64 64 72 41 29 3b 0a 20 20 56 64 62  regAddrA);.  Vdb
257a2 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
257a3 20 22 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20   "End coroutine 
257a4 66 6f 72 20 6c 65 66 74 20 53 45 4c 45 43 54 22  for left SELECT"
257a5 29 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61  ));..  /* Genera
257a6 74 65 20 61 20 63 6f 72 6f 75 74 69 6e 65 20 74  te a coroutine t
257a7 6f 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 53  o evaluate the S
257a8 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
257a9 6f 6e 20 0a 20 20 2a 2a 20 74 68 65 20 72 69 67  on .  ** the rig
257aa 68 74 20 2d 20 74 68 65 20 22 42 22 20 73 65 6c  ht - the "B" sel
257ab 65 63 74 0a 20 20 2a 2f 0a 20 20 61 64 64 72 53  ect.  */.  addrS
257ac 65 6c 65 63 74 42 20 3d 20 73 71 6c 69 74 65 33  electB = sqlite3
257ad 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
257ae 76 29 3b 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f  v);.  VdbeNoopCo
257af 6d 6d 65 6e 74 28 28 76 2c 20 22 42 65 67 69 6e  mment((v, "Begin
257b0 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f 72 20 72   coroutine for r
257b1 69 67 68 74 20 53 45 4c 45 43 54 22 29 29 3b 0a  ight SELECT"));.
257b2 20 20 73 61 76 65 64 4c 69 6d 69 74 20 3d 20 70    savedLimit = p
257b3 2d 3e 69 4c 69 6d 69 74 3b 0a 20 20 73 61 76 65  ->iLimit;.  save
257b4 64 4f 66 66 73 65 74 20 3d 20 70 2d 3e 69 4f 66  dOffset = p->iOf
257b5 66 73 65 74 3b 0a 20 20 70 2d 3e 69 4c 69 6d 69  fset;.  p->iLimi
257b6 74 20 3d 20 72 65 67 4c 69 6d 69 74 42 3b 0a 20  t = regLimitB;. 
257b7 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b   p->iOffset = 0;
257b8 20 20 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65    .  sqlite3Sele
257b9 63 74 28 70 50 61 72 73 65 2c 20 70 2c 20 26 64  ct(pParse, p, &d
257ba 65 73 74 42 29 3b 0a 20 20 70 2d 3e 69 4c 69 6d  estB);.  p->iLim
257bb 69 74 20 3d 20 73 61 76 65 64 4c 69 6d 69 74 3b  it = savedLimit;
257bc 0a 20 20 70 2d 3e 69 4f 66 66 73 65 74 20 3d 20  .  p->iOffset = 
257bd 73 61 76 65 64 4f 66 66 73 65 74 3b 0a 20 20 73  savedOffset;.  s
257be 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
257bf 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
257c0 31 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20 20 73  1, regEofB);.  s
257c1 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
257c2 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65  (v, OP_Yield, re
257c3 67 41 64 64 72 42 29 3b 0a 20 20 56 64 62 65 4e  gAddrB);.  VdbeN
257c4 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
257c5 45 6e 64 20 63 6f 72 6f 75 74 69 6e 65 20 66 6f  End coroutine fo
257c6 72 20 72 69 67 68 74 20 53 45 4c 45 43 54 22 29  r right SELECT")
257c7 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
257c8 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74  e a subroutine t
257c9 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65 20  hat outputs the 
257ca 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
257cb 68 65 20 41 0a 20 20 2a 2a 20 73 65 6c 65 63 74  he A.  ** select
257cc 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75 74   as the next out
257cd 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20 63  put row of the c
257ce 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a  ompound select..
257cf 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43    */.  VdbeNoopC
257d0 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 4f 75 74 70  omment((v, "Outp
257d1 75 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 41  ut routine for A
257d2 22 29 29 3b 0a 20 20 61 64 64 72 4f 75 74 41 20  "));.  addrOutA 
257d3 3d 20 67 65 6e 65 72 61 74 65 4f 75 74 70 75 74  = generateOutput
257d4 53 75 62 72 6f 75 74 69 6e 65 28 70 50 61 72 73  Subroutine(pPars
257d5 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  e,.             
257d6 20 20 20 20 70 2c 20 26 64 65 73 74 41 2c 20 70      p, &destA, p
257d7 44 65 73 74 2c 20 72 65 67 4f 75 74 41 2c 0a 20  Dest, regOutA,. 
257d8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
257d9 72 65 67 50 72 65 76 2c 20 70 4b 65 79 44 75 70  regPrev, pKeyDup
257da 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e  , P4_KEYINFO_HAN
257db 44 4f 46 46 2c 20 6c 61 62 65 6c 45 6e 64 29 3b  DOFF, labelEnd);
257dc 0a 20 20 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  .  .  /* Generat
257dd 65 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74  e a subroutine t
257de 68 61 74 20 6f 75 74 70 75 74 73 20 74 68 65 20  hat outputs the 
257df 63 75 72 72 65 6e 74 20 72 6f 77 20 6f 66 20 74  current row of t
257e0 68 65 20 42 0a 20 20 2a 2a 20 73 65 6c 65 63 74  he B.  ** select
257e1 20 61 73 20 74 68 65 20 6e 65 78 74 20 6f 75 74   as the next out
257e2 70 75 74 20 72 6f 77 20 6f 66 20 74 68 65 20 63  put row of the c
257e3 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2e 0a  ompound select..
257e4 20 20 2a 2f 0a 20 20 69 66 28 20 6f 70 3d 3d 54    */.  if( op==T
257e5 4b 5f 41 4c 4c 20 7c 7c 20 6f 70 3d 3d 54 4b 5f  K_ALL || op==TK_
257e6 55 4e 49 4f 4e 20 29 7b 0a 20 20 20 20 56 64 62  UNION ){.    Vdb
257e7 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
257e8 20 22 4f 75 74 70 75 74 20 72 6f 75 74 69 6e 65   "Output routine
257e9 20 66 6f 72 20 42 22 29 29 3b 0a 20 20 20 20 61   for B"));.    a
257ea 64 64 72 4f 75 74 42 20 3d 20 67 65 6e 65 72 61  ddrOutB = genera
257eb 74 65 4f 75 74 70 75 74 53 75 62 72 6f 75 74 69  teOutputSubrouti
257ec 6e 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20  ne(pParse,.     
257ed 20 20 20 20 20 20 20 20 20 20 20 20 70 2c 20 26              p, &
257ee 64 65 73 74 42 2c 20 70 44 65 73 74 2c 20 72 65  destB, pDest, re
257ef 67 4f 75 74 42 2c 0a 20 20 20 20 20 20 20 20 20  gOutB,.         
257f0 20 20 20 20 20 20 20 20 72 65 67 50 72 65 76 2c          regPrev,
257f1 20 70 4b 65 79 44 75 70 2c 20 50 34 5f 4b 45 59   pKeyDup, P4_KEY
257f2 49 4e 46 4f 5f 53 54 41 54 49 43 2c 20 6c 61 62  INFO_STATIC, lab
257f3 65 6c 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f  elEnd);.  }..  /
257f4 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73 75 62  * Generate a sub
257f5 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e 20 77  routine to run w
257f6 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74 73 20  hen the results 
257f7 66 72 6f 6d 20 73 65 6c 65 63 74 20 41 0a 20 20  from select A.  
257f8 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74 65 64  ** are exhausted
257f9 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61 20 69   and only data i
257fa 6e 20 73 65 6c 65 63 74 20 42 20 72 65 6d 61 69  n select B remai
257fb 6e 73 2e 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e  ns..  */.  VdbeN
257fc 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  oopComment((v, "
257fd 65 6f 66 2d 41 20 73 75 62 72 6f 75 74 69 6e 65  eof-A subroutine
257fe 22 29 29 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54  "));.  if( op==T
257ff 4b 5f 45 58 43 45 50 54 20 7c 7c 20 6f 70 3d 3d  K_EXCEPT || op==
25800 54 4b 5f 49 4e 54 45 52 53 45 43 54 20 29 7b 0a  TK_INTERSECT ){.
25801 20 20 20 20 61 64 64 72 45 6f 66 41 20 3d 20 73      addrEofA = s
25802 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25803 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
25804 6c 61 62 65 6c 45 6e 64 29 3b 0a 20 20 7d 65 6c  labelEnd);.  }el
25805 73 65 7b 20 20 0a 20 20 20 20 61 64 64 72 45 6f  se{  .    addrEo
25806 66 41 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  fA = sqlite3Vdbe
25807 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
25808 20 72 65 67 45 6f 66 42 2c 20 6c 61 62 65 6c 45   regEofB, labelE
25809 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  nd);.    sqlite3
2580a 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2580b 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 42 2c  _Gosub, regOutB,
2580c 20 61 64 64 72 4f 75 74 42 29 3b 0a 20 20 20 20   addrOutB);.    
2580d 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2580e 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72  1(v, OP_Yield, r
2580f 65 67 41 64 64 72 42 29 3b 0a 20 20 20 20 73 71  egAddrB);.    sq
25810 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
25811 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 61  v, OP_Goto, 0, a
25812 64 64 72 45 6f 66 41 29 3b 0a 20 20 7d 0a 0a 20  ddrEofA);.  }.. 
25813 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20 73   /* Generate a s
25814 75 62 72 6f 75 74 69 6e 65 20 74 6f 20 72 75 6e  ubroutine to run
25815 20 77 68 65 6e 20 74 68 65 20 72 65 73 75 6c 74   when the result
25816 73 20 66 72 6f 6d 20 73 65 6c 65 63 74 20 42 0a  s from select B.
25817 20 20 2a 2a 20 61 72 65 20 65 78 68 61 75 73 74    ** are exhaust
25818 65 64 20 61 6e 64 20 6f 6e 6c 79 20 64 61 74 61  ed and only data
25819 20 69 6e 20 73 65 6c 65 63 74 20 41 20 72 65 6d   in select A rem
2581a 61 69 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ains..  */.  if(
2581b 20 6f 70 3d 3d 54 4b 5f 49 4e 54 45 52 53 45 43   op==TK_INTERSEC
2581c 54 20 29 7b 0a 20 20 20 20 61 64 64 72 45 6f 66  T ){.    addrEof
2581d 42 20 3d 20 61 64 64 72 45 6f 66 41 3b 0a 20 20  B = addrEofA;.  
2581e 7d 65 6c 73 65 7b 20 20 0a 20 20 20 20 56 64 62  }else{  .    Vdb
2581f 65 4e 6f 6f 70 43 6f 6d 6d 65 6e 74 28 28 76 2c  eNoopComment((v,
25820 20 22 65 6f 66 2d 42 20 73 75 62 72 6f 75 74 69   "eof-B subrouti
25821 6e 65 22 29 29 3b 0a 20 20 20 20 61 64 64 72 45  ne"));.    addrE
25822 6f 66 42 20 3d 20 73 71 6c 69 74 65 33 56 64 62  ofB = sqlite3Vdb
25823 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66  eAddOp2(v, OP_If
25824 2c 20 72 65 67 45 6f 66 41 2c 20 6c 61 62 65 6c  , regEofA, label
25825 45 6e 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  End);.    sqlite
25826 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25827 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75 74 41  P_Gosub, regOutA
25828 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20 20 20  , addrOutA);.   
25829 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2582a 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2582b 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73  regAddrA);.    s
2582c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2582d 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
2582e 61 64 64 72 45 6f 66 42 29 3b 0a 20 20 7d 0a 0a  addrEofB);.  }..
2582f 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f    /* Generate co
25830 64 65 20 74 6f 20 68 61 6e 64 6c 65 20 74 68 65  de to handle the
25831 20 63 61 73 65 20 6f 66 20 41 3c 42 0a 20 20 2a   case of A<B.  *
25832 2f 0a 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d  /.  VdbeNoopComm
25833 65 6e 74 28 28 76 2c 20 22 41 2d 6c 74 2d 42 20  ent((v, "A-lt-B 
25834 73 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20  subroutine"));. 
25835 20 61 64 64 72 41 6c 74 42 20 3d 20 73 71 6c 69   addrAltB = sqli
25836 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
25837 20 4f 50 5f 47 6f 73 75 62 2c 20 72 65 67 4f 75   OP_Gosub, regOu
25838 74 41 2c 20 61 64 64 72 4f 75 74 41 29 3b 0a 20  tA, addrOutA);. 
25839 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2583a 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
2583b 72 65 67 41 64 64 72 41 29 3b 0a 20 20 73 71 6c  regAddrA);.  sql
2583c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2583d 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f 66 41  , OP_If, regEofA
2583e 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20 20 73  , addrEofA);.  s
2583f 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25840 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20  (v, OP_Goto, 0, 
25841 6c 61 62 65 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f  labelCmpr);..  /
25842 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20  * Generate code 
25843 74 6f 20 68 61 6e 64 6c 65 20 74 68 65 20 63 61  to handle the ca
25844 73 65 20 6f 66 20 41 3d 3d 42 0a 20 20 2a 2f 0a  se of A==B.  */.
25845 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c    if( op==TK_ALL
25846 20 29 7b 0a 20 20 20 20 61 64 64 72 41 65 71 42   ){.    addrAeqB
25847 20 3d 20 61 64 64 72 41 6c 74 42 3b 0a 20 20 7d   = addrAltB;.  }
25848 65 6c 73 65 20 69 66 28 20 6f 70 3d 3d 54 4b 5f  else if( op==TK_
25849 49 4e 54 45 52 53 45 43 54 20 29 7b 0a 20 20 20  INTERSECT ){.   
2584a 20 61 64 64 72 41 65 71 42 20 3d 20 61 64 64 72   addrAeqB = addr
2584b 41 6c 74 42 3b 0a 20 20 20 20 61 64 64 72 41 6c  AltB;.    addrAl
2584c 74 42 2b 2b 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  tB++;.  }else{. 
2584d 20 20 20 56 64 62 65 4e 6f 6f 70 43 6f 6d 6d 65     VdbeNoopComme
2584e 6e 74 28 28 76 2c 20 22 41 2d 65 71 2d 42 20 73  nt((v, "A-eq-B s
2584f 75 62 72 6f 75 74 69 6e 65 22 29 29 3b 0a 20 20  ubroutine"));.  
25850 20 20 61 64 64 72 41 65 71 42 20 3d 0a 20 20 20    addrAeqB =.   
25851 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25852 70 31 28 76 2c 20 4f 50 5f 59 69 65 6c 64 2c 20  p1(v, OP_Yield, 
25853 72 65 67 41 64 64 72 41 29 3b 0a 20 20 20 20 73  regAddrA);.    s
25854 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
25855 28 76 2c 20 4f 50 5f 49 66 2c 20 72 65 67 45 6f  (v, OP_If, regEo
25856 66 41 2c 20 61 64 64 72 45 6f 66 41 29 3b 0a 20  fA, addrEofA);. 
25857 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
25858 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
25859 20 30 2c 20 6c 61 62 65 6c 43 6d 70 72 29 3b 0a   0, labelCmpr);.
2585a 20 20 7d 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61    }..  /* Genera
2585b 74 65 20 63 6f 64 65 20 74 6f 20 68 61 6e 64 6c  te code to handl
2585c 65 20 74 68 65 20 63 61 73 65 20 6f 66 20 41 3e  e the case of A>
2585d 42 0a 20 20 2a 2f 0a 20 20 56 64 62 65 4e 6f 6f  B.  */.  VdbeNoo
2585e 70 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 41 2d  pComment((v, "A-
2585f 67 74 2d 42 20 73 75 62 72 6f 75 74 69 6e 65 22  gt-B subroutine"
25860 29 29 3b 0a 20 20 61 64 64 72 41 67 74 42 20 3d  ));.  addrAgtB =
25861 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
25862 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 69 66  entAddr(v);.  if
25863 28 20 6f 70 3d 3d 54 4b 5f 41 4c 4c 20 7c 7c 20  ( op==TK_ALL || 
25864 6f 70 3d 3d 54 4b 5f 55 4e 49 4f 4e 20 29 7b 0a  op==TK_UNION ){.
25865 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
25866 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
25867 62 2c 20 72 65 67 4f 75 74 42 2c 20 61 64 64 72  b, regOutB, addr
25868 4f 75 74 42 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  OutB);.  }.  sql
25869 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2586a 2c 20 4f 50 5f 59 69 65 6c 64 2c 20 72 65 67 41  , OP_Yield, regA
2586b 64 64 72 42 29 3b 0a 20 20 73 71 6c 69 74 65 33  ddrB);.  sqlite3
2586c 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2586d 5f 49 66 2c 20 72 65 67 45 6f 66 42 2c 20 61 64  _If, regEofB, ad
2586e 64 72 45 6f 66 42 29 3b 0a 20 20 73 71 6c 69 74  drEofB);.  sqlit
2586f 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
25870 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 6c 61 62 65  OP_Goto, 0, labe
25871 6c 43 6d 70 72 29 3b 0a 0a 20 20 2f 2a 20 54 68  lCmpr);..  /* Th
25872 69 73 20 63 6f 64 65 20 72 75 6e 73 20 6f 6e 63  is code runs onc
25873 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20  e to initialize 
25874 65 76 65 72 79 74 68 69 6e 67 2e 0a 20 20 2a 2f  everything..  */
25875 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
25876 6d 70 48 65 72 65 28 76 2c 20 6a 31 29 3b 0a 20  mpHere(v, j1);. 
25877 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
25878 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
25879 2c 20 30 2c 20 72 65 67 45 6f 66 41 29 3b 0a 20  , 0, regEofA);. 
2587a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2587b 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72  p2(v, OP_Integer
2587c 2c 20 30 2c 20 72 65 67 45 6f 66 42 29 3b 0a 20  , 0, regEofB);. 
2587d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2587e 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75 62 2c 20  p2(v, OP_Gosub, 
2587f 72 65 67 41 64 64 72 41 2c 20 61 64 64 72 53 65  regAddrA, addrSe
25880 6c 65 63 74 41 29 3b 0a 20 20 73 71 6c 69 74 65  lectA);.  sqlite
25881 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
25882 50 5f 47 6f 73 75 62 2c 20 72 65 67 41 64 64 72  P_Gosub, regAddr
25883 42 2c 20 61 64 64 72 53 65 6c 65 63 74 42 29 3b  B, addrSelectB);
25884 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
25885 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c 20 72  dOp2(v, OP_If, r
25886 65 67 45 6f 66 41 2c 20 61 64 64 72 45 6f 66 41  egEofA, addrEofA
25887 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
25888 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 66 2c  AddOp2(v, OP_If,
25889 20 72 65 67 45 6f 66 42 2c 20 61 64 64 72 45 6f   regEofB, addrEo
2588a 66 42 29 3b 0a 0a 20 20 2f 2a 20 49 6d 70 6c 65  fB);..  /* Imple
2588b 6d 65 6e 74 20 74 68 65 20 6d 61 69 6e 20 6d 65  ment the main me
2588c 72 67 65 20 6c 6f 6f 70 0a 20 20 2a 2f 0a 20 20  rge loop.  */.  
2588d 73 71 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c  sqlite3VdbeResol
2588e 76 65 4c 61 62 65 6c 28 76 2c 20 6c 61 62 65 6c  veLabel(v, label
2588f 43 6d 70 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  Cmpr);.  sqlite3
25890 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50  VdbeAddOp4(v, OP
25891 5f 50 65 72 6d 75 74 61 74 69 6f 6e 2c 20 30 2c  _Permutation, 0,
25892 20 30 2c 20 30 2c 20 28 63 68 61 72 2a 29 61 50   0, 0, (char*)aP
25893 65 72 6d 75 74 65 2c 20 50 34 5f 49 4e 54 41 52  ermute, P4_INTAR
25894 52 41 59 29 3b 0a 20 20 73 71 6c 69 74 65 33 56  RAY);.  sqlite3V
25895 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f  dbeAddOp4(v, OP_
25896 43 6f 6d 70 61 72 65 2c 20 64 65 73 74 41 2e 69  Compare, destA.i
25897 4d 65 6d 2c 20 64 65 73 74 42 2e 69 4d 65 6d 2c  Mem, destB.iMem,
25898 20 6e 4f 72 64 65 72 42 79 2c 0a 20 20 20 20 20   nOrderBy,.     
25899 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2589a 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65 79 4d      (char*)pKeyM
2589b 65 72 67 65 2c 20 50 34 5f 4b 45 59 49 4e 46 4f  erge, P4_KEYINFO
2589c 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 73 71 6c  _HANDOFF);.  sql
2589d 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2589e 2c 20 4f 50 5f 4a 75 6d 70 2c 20 61 64 64 72 41  , OP_Jump, addrA
2589f 6c 74 42 2c 20 61 64 64 72 41 65 71 42 2c 20 61  ltB, addrAeqB, a
258a0 64 64 72 41 67 74 42 29 3b 0a 0a 20 20 2f 2a 20  ddrAgtB);..  /* 
258a1 52 65 6c 65 61 73 65 20 74 65 6d 70 6f 72 61 72  Release temporar
258a2 79 20 72 65 67 69 73 74 65 72 73 0a 20 20 2a 2f  y registers.  */
258a3 0a 20 20 69 66 28 20 72 65 67 50 72 65 76 20 29  .  if( regPrev )
258a4 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c  {.    sqlite3Rel
258a5 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
258a6 61 72 73 65 2c 20 72 65 67 50 72 65 76 2c 20 6e  arse, regPrev, n
258a7 4f 72 64 65 72 42 79 2b 31 29 3b 0a 20 20 7d 0a  OrderBy+1);.  }.
258a8 0a 20 20 2f 2a 20 4a 75 6d 70 20 74 6f 20 74 68  .  /* Jump to th
258a9 65 20 74 68 69 73 20 70 6f 69 6e 74 20 69 6e 20  e this point in 
258aa 6f 72 64 65 72 20 74 6f 20 74 65 72 6d 69 6e 61  order to termina
258ab 74 65 20 74 68 65 20 71 75 65 72 79 2e 0a 20 20  te the query..  
258ac 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  */.  sqlite3Vdbe
258ad 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20  ResolveLabel(v, 
258ae 6c 61 62 65 6c 45 6e 64 29 3b 0a 0a 20 20 2f 2a  labelEnd);..  /*
258af 20 53 65 74 20 74 68 65 20 6e 75 6d 62 65 72 20   Set the number 
258b0 6f 66 20 6f 75 74 70 75 74 20 63 6f 6c 75 6d 6e  of output column
258b1 73 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 44 65  s.  */.  if( pDe
258b2 73 74 2d 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f  st->eDest==SRT_O
258b3 75 74 70 75 74 20 29 7b 0a 20 20 20 20 53 65 6c  utput ){.    Sel
258b4 65 63 74 20 2a 70 46 69 72 73 74 20 3d 20 70 50  ect *pFirst = pP
258b5 72 69 6f 72 3b 0a 20 20 20 20 77 68 69 6c 65 28  rior;.    while(
258b6 20 70 46 69 72 73 74 2d 3e 70 50 72 69 6f 72 20   pFirst->pPrior 
258b7 29 20 70 46 69 72 73 74 20 3d 20 70 46 69 72 73  ) pFirst = pFirs
258b8 74 2d 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 67  t->pPrior;.    g
258b9 65 6e 65 72 61 74 65 43 6f 6c 75 6d 6e 4e 61 6d  enerateColumnNam
258ba 65 73 28 70 50 61 72 73 65 2c 20 30 2c 20 70 46  es(pParse, 0, pF
258bb 69 72 73 74 2d 3e 70 45 4c 69 73 74 29 3b 0a 20  irst->pEList);. 
258bc 20 7d 0a 0a 20 20 2f 2a 20 52 65 61 73 73 65 6d   }..  /* Reassem
258bd 62 6c 79 20 74 68 65 20 63 6f 6d 70 6f 75 6e 64  bly the compound
258be 20 71 75 65 72 79 20 73 6f 20 74 68 61 74 20 69   query so that i
258bf 74 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  t will be freed 
258c0 63 6f 72 72 65 63 74 6c 79 0a 20 20 2a 2a 20 62  correctly.  ** b
258c1 79 20 74 68 65 20 63 61 6c 6c 69 6e 67 20 66 75  y the calling fu
258c2 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 28 20  nction */.  if( 
258c3 70 2d 3e 70 50 72 69 6f 72 20 29 7b 0a 20 20 20  p->pPrior ){.   
258c4 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65   sqlite3SelectDe
258c5 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 50 72 69  lete(db, p->pPri
258c6 6f 72 29 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 50  or);.  }.  p->pP
258c7 72 69 6f 72 20 3d 20 70 50 72 69 6f 72 3b 0a 0a  rior = pPrior;..
258c8 20 20 2f 2a 2a 2a 20 54 42 44 3a 20 20 49 6e 73    /*** TBD:  Ins
258c9 65 72 74 20 73 75 62 72 6f 75 74 69 6e 65 20 63  ert subroutine c
258ca 61 6c 6c 73 20 74 6f 20 63 6c 6f 73 65 20 63 75  alls to close cu
258cb 72 73 6f 72 73 20 6f 6e 20 69 6e 63 6f 6d 70 6c  rsors on incompl
258cc 65 74 65 0a 20 20 2a 2a 2a 2a 20 73 75 62 71 75  ete.  **** subqu
258cd 65 72 69 65 73 20 2a 2a 2a 2a 2f 0a 20 20 72 65  eries ****/.  re
258ce 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
258cf 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 20 21 64  }.#endif..#if !d
258d0 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
258d1 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20  IT_SUBQUERY) || 
258d2 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
258d3 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 20 46 6f  OMIT_VIEW)./* Fo
258d4 72 77 61 72 64 20 44 65 63 6c 61 72 61 74 69 6f  rward Declaratio
258d5 6e 73 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69  ns */.static voi
258d6 64 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28  d substExprList(
258d7 73 71 6c 69 74 65 33 2a 2c 20 45 78 70 72 4c 69  sqlite3*, ExprLi
258d8 73 74 2a 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69  st*, int, ExprLi
258d9 73 74 2a 29 3b 0a 73 74 61 74 69 63 20 76 6f 69  st*);.static voi
258da 64 20 73 75 62 73 74 53 65 6c 65 63 74 28 73 71  d substSelect(sq
258db 6c 69 74 65 33 2a 2c 20 53 65 6c 65 63 74 20 2a  lite3*, Select *
258dc 2c 20 69 6e 74 2c 20 45 78 70 72 4c 69 73 74 20  , int, ExprList 
258dd 2a 29 3b 0a 0a 2f 2a 0a 2a 2a 20 53 63 61 6e 20  *);../*.** Scan 
258de 74 68 72 6f 75 67 68 20 74 68 65 20 65 78 70 72  through the expr
258df 65 73 73 69 6f 6e 20 70 45 78 70 72 2e 20 20 52  ession pExpr.  R
258e0 65 70 6c 61 63 65 20 65 76 65 72 79 20 72 65 66  eplace every ref
258e1 65 72 65 6e 63 65 20 74 6f 0a 2a 2a 20 61 20 63  erence to.** a c
258e2 6f 6c 75 6d 6e 20 69 6e 20 74 61 62 6c 65 20 6e  olumn in table n
258e3 75 6d 62 65 72 20 69 54 61 62 6c 65 20 77 69 74  umber iTable wit
258e4 68 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20  h a copy of the 
258e5 69 43 6f 6c 75 6d 6e 2d 74 68 0a 2a 2a 20 65 6e  iColumn-th.** en
258e6 74 72 79 20 69 6e 20 70 45 4c 69 73 74 2e 20 20  try in pEList.  
258e7 28 42 75 74 20 6c 65 61 76 65 20 72 65 66 65 72  (But leave refer
258e8 65 6e 63 65 73 20 74 6f 20 74 68 65 20 52 4f 57  ences to the ROW
258e9 49 44 20 63 6f 6c 75 6d 6e 20 0a 2a 2a 20 75 6e  ID column .** un
258ea 63 68 61 6e 67 65 64 2e 29 0a 2a 2a 0a 2a 2a 20  changed.).**.** 
258eb 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
258ec 70 61 72 74 20 6f 66 20 74 68 65 20 66 6c 61 74  part of the flat
258ed 74 65 6e 69 6e 67 20 70 72 6f 63 65 64 75 72 65  tening procedure
258ee 2e 20 20 41 20 73 75 62 71 75 65 72 79 0a 2a 2a  .  A subquery.**
258ef 20 77 68 6f 73 65 20 72 65 73 75 6c 74 20 73 65   whose result se
258f0 74 20 69 73 20 64 65 66 69 6e 65 64 20 62 79 20  t is defined by 
258f1 70 45 4c 69 73 74 20 61 70 70 65 61 72 73 20 61  pEList appears a
258f2 73 20 65 6e 74 72 79 20 69 6e 20 74 68 65 0a 2a  s entry in the.*
258f3 2a 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66  * FROM clause of
258f4 20 61 20 53 45 4c 45 43 54 20 73 75 63 68 20 74   a SELECT such t
258f5 68 61 74 20 74 68 65 20 56 44 42 45 20 63 75 72  hat the VDBE cur
258f6 73 6f 72 20 61 73 73 69 67 6e 65 64 20 74 6f 20  sor assigned to 
258f7 74 68 61 74 0a 2a 2a 20 46 4f 52 4d 20 63 6c 61  that.** FORM cla
258f8 75 73 65 20 65 6e 74 72 79 20 69 73 20 69 54 61  use entry is iTa
258f9 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75 74 69  ble.  This routi
258fa 6e 65 20 6d 61 6b 65 20 74 68 65 20 6e 65 63 65  ne make the nece
258fb 73 73 61 72 79 20 0a 2a 2a 20 63 68 61 6e 67 65  ssary .** change
258fc 73 20 74 6f 20 70 45 78 70 72 20 73 6f 20 74 68  s to pExpr so th
258fd 61 74 20 69 74 20 72 65 66 65 72 73 20 64 69 72  at it refers dir
258fe 65 63 74 6c 79 20 74 6f 20 74 68 65 20 73 6f 75  ectly to the sou
258ff 72 63 65 20 74 61 62 6c 65 0a 2a 2a 20 6f 66 20  rce table.** of 
25900 74 68 65 20 73 75 62 71 75 65 72 79 20 72 61 74  the subquery rat
25901 68 65 72 20 74 68 65 20 72 65 73 75 6c 74 20 73  her the result s
25902 65 74 20 6f 66 20 74 68 65 20 73 75 62 71 75 65  et of the subque
25903 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ry..*/.static vo
25904 69 64 20 73 75 62 73 74 45 78 70 72 28 0a 20 20  id substExpr(.  
25905 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20  sqlite3 *db,    
25906 20 20 20 20 2f 2a 20 52 65 70 6f 72 74 20 6d 61      /* Report ma
25907 6c 6c 6f 63 20 65 72 72 6f 72 73 20 74 6f 20 74  lloc errors to t
25908 68 69 73 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a  his connection *
25909 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
2590a 20 20 20 20 20 20 20 20 2f 2a 20 45 78 70 72 20          /* Expr 
2590b 69 6e 20 77 68 69 63 68 20 73 75 62 73 74 69 74  in which substit
2590c 75 74 69 6f 6e 20 6f 63 63 75 72 73 20 2a 2f 0a  ution occurs */.
2590d 20 20 69 6e 74 20 69 54 61 62 6c 65 2c 20 20 20    int iTable,   
2590e 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74        /* Table t
2590f 6f 20 62 65 20 73 75 62 73 74 69 74 75 74 65 64  o be substituted
25910 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
25911 70 45 4c 69 73 74 20 20 20 20 2f 2a 20 53 75 62  pEList    /* Sub
25912 73 74 69 74 75 74 65 20 65 78 70 72 65 73 73 69  stitute expressi
25913 6f 6e 73 20 2a 2f 0a 29 7b 0a 20 20 69 66 28 20  ons */.){.  if( 
25914 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
25915 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  n;.  if( pExpr->
25916 6f 70 3d 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26  op==TK_COLUMN &&
25917 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 3d 3d   pExpr->iTable==
25918 69 54 61 62 6c 65 20 29 7b 0a 20 20 20 20 69 66  iTable ){.    if
25919 28 20 70 45 78 70 72 2d 3e 69 43 6f 6c 75 6d 6e  ( pExpr->iColumn
2591a 3c 30 20 29 7b 0a 20 20 20 20 20 20 70 45 78 70  <0 ){.      pExp
2591b 72 2d 3e 6f 70 20 3d 20 54 4b 5f 4e 55 4c 4c 3b  r->op = TK_NULL;
2591c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
2591d 20 20 45 78 70 72 20 2a 70 4e 65 77 3b 0a 20 20    Expr *pNew;.  
2591e 20 20 20 20 61 73 73 65 72 74 28 20 70 45 4c 69      assert( pELi
2591f 73 74 21 3d 30 20 26 26 20 70 45 78 70 72 2d 3e  st!=0 && pExpr->
25920 69 43 6f 6c 75 6d 6e 3c 70 45 4c 69 73 74 2d 3e  iColumn<pEList->
25921 6e 45 78 70 72 20 29 3b 0a 20 20 20 20 20 20 61  nExpr );.      a
25922 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e 70 4c  ssert( pExpr->pL
25923 65 66 74 3d 3d 30 20 26 26 20 70 45 78 70 72 2d  eft==0 && pExpr-
25924 3e 70 52 69 67 68 74 3d 3d 30 20 26 26 20 70 45  >pRight==0 && pE
25925 78 70 72 2d 3e 70 4c 69 73 74 3d 3d 30 20 29 3b  xpr->pList==0 );
25926 0a 20 20 20 20 20 20 70 4e 65 77 20 3d 20 70 45  .      pNew = pE
25927 4c 69 73 74 2d 3e 61 5b 70 45 78 70 72 2d 3e 69  List->a[pExpr->i
25928 43 6f 6c 75 6d 6e 5d 2e 70 45 78 70 72 3b 0a 20  Column].pExpr;. 
25929 20 20 20 20 20 61 73 73 65 72 74 28 20 70 4e 65       assert( pNe
2592a 77 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45  w!=0 );.      pE
2592b 78 70 72 2d 3e 6f 70 20 3d 20 70 4e 65 77 2d 3e  xpr->op = pNew->
2592c 6f 70 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74  op;.      assert
2592d 28 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 3d 3d  ( pExpr->pLeft==
2592e 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70 72  0 );.      pExpr
2592f 2d 3e 70 4c 65 66 74 20 3d 20 73 71 6c 69 74 65  ->pLeft = sqlite
25930 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4e 65  3ExprDup(db, pNe
25931 77 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 20 20 20  w->pLeft);.     
25932 20 61 73 73 65 72 74 28 20 70 45 78 70 72 2d 3e   assert( pExpr->
25933 70 52 69 67 68 74 3d 3d 30 20 29 3b 0a 20 20 20  pRight==0 );.   
25934 20 20 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74     pExpr->pRight
25935 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75   = sqlite3ExprDu
25936 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 52 69 67  p(db, pNew->pRig
25937 68 74 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  ht);.      asser
25938 74 28 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 3d  t( pExpr->pList=
25939 3d 30 20 29 3b 0a 20 20 20 20 20 20 70 45 78 70  =0 );.      pExp
2593a 72 2d 3e 70 4c 69 73 74 20 3d 20 73 71 6c 69 74  r->pList = sqlit
2593b 65 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62  e3ExprListDup(db
2593c 2c 20 70 4e 65 77 2d 3e 70 4c 69 73 74 29 3b 0a  , pNew->pList);.
2593d 20 20 20 20 20 20 70 45 78 70 72 2d 3e 69 54 61        pExpr->iTa
2593e 62 6c 65 20 3d 20 70 4e 65 77 2d 3e 69 54 61 62  ble = pNew->iTab
2593f 6c 65 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  le;.      pExpr-
25940 3e 70 54 61 62 20 3d 20 70 4e 65 77 2d 3e 70 54  >pTab = pNew->pT
25941 61 62 3b 0a 20 20 20 20 20 20 70 45 78 70 72 2d  ab;.      pExpr-
25942 3e 69 43 6f 6c 75 6d 6e 20 3d 20 70 4e 65 77 2d  >iColumn = pNew-
25943 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
25944 70 45 78 70 72 2d 3e 69 41 67 67 20 3d 20 70 4e  pExpr->iAgg = pN
25945 65 77 2d 3e 69 41 67 67 3b 0a 20 20 20 20 20 20  ew->iAgg;.      
25946 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79  sqlite3TokenCopy
25947 28 64 62 2c 20 26 70 45 78 70 72 2d 3e 74 6f 6b  (db, &pExpr->tok
25948 65 6e 2c 20 26 70 4e 65 77 2d 3e 74 6f 6b 65 6e  en, &pNew->token
25949 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2594a 54 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70  TokenCopy(db, &p
2594b 45 78 70 72 2d 3e 73 70 61 6e 2c 20 26 70 4e 65  Expr->span, &pNe
2594c 77 2d 3e 73 70 61 6e 29 3b 0a 20 20 20 20 20 20  w->span);.      
2594d 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74 20 3d  pExpr->pSelect =
2594e 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75   sqlite3SelectDu
2594f 70 28 64 62 2c 20 70 4e 65 77 2d 3e 70 53 65 6c  p(db, pNew->pSel
25950 65 63 74 29 3b 0a 20 20 20 20 20 20 70 45 78 70  ect);.      pExp
25951 72 2d 3e 66 6c 61 67 73 20 3d 20 70 4e 65 77 2d  r->flags = pNew-
25952 3e 66 6c 61 67 73 3b 0a 20 20 20 20 7d 0a 20 20  >flags;.    }.  
25953 7d 65 6c 73 65 7b 0a 20 20 20 20 73 75 62 73 74  }else{.    subst
25954 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d 3e  Expr(db, pExpr->
25955 70 4c 65 66 74 2c 20 69 54 61 62 6c 65 2c 20 70  pLeft, iTable, p
25956 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 75 62 73  EList);.    subs
25957 74 45 78 70 72 28 64 62 2c 20 70 45 78 70 72 2d  tExpr(db, pExpr-
25958 3e 70 52 69 67 68 74 2c 20 69 54 61 62 6c 65 2c  >pRight, iTable,
25959 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 73 75   pEList);.    su
2595a 62 73 74 53 65 6c 65 63 74 28 64 62 2c 20 70 45  bstSelect(db, pE
2595b 78 70 72 2d 3e 70 53 65 6c 65 63 74 2c 20 69 54  xpr->pSelect, iT
2595c 61 62 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20  able, pEList);. 
2595d 20 20 20 73 75 62 73 74 45 78 70 72 4c 69 73 74     substExprList
2595e 28 64 62 2c 20 70 45 78 70 72 2d 3e 70 4c 69 73  (db, pExpr->pLis
2595f 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  t, iTable, pELis
25960 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63  t);.  }.}.static
25961 20 76 6f 69 64 20 73 75 62 73 74 45 78 70 72 4c   void substExprL
25962 69 73 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ist(.  sqlite3 *
25963 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 52  db,         /* R
25964 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72 72  eport malloc err
25965 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 45 78  ors here */.  Ex
25966 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 2c 20 20  prList *pList,  
25967 20 20 20 2f 2a 20 4c 69 73 74 20 74 6f 20 73 63     /* List to sc
25968 61 6e 20 61 6e 64 20 69 6e 20 77 68 69 63 68 20  an and in which 
25969 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74 75  to make substitu
2596a 74 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 54 61  tes */.  int iTa
2596b 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  ble,          /*
2596c 20 54 61 62 6c 65 20 74 6f 20 62 65 20 73 75 62   Table to be sub
2596d 73 74 69 74 75 74 65 64 20 2a 2f 0a 20 20 45 78  stituted */.  Ex
2596e 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
2596f 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
25970 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20   values */.){.  
25971 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 70 4c 69  int i;.  if( pLi
25972 73 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a  st==0 ) return;.
25973 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4c 69    for(i=0; i<pLi
25974 73 74 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29 7b  st->nExpr; i++){
25975 0a 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64  .    substExpr(d
25976 62 2c 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e 70  b, pList->a[i].p
25977 45 78 70 72 2c 20 69 54 61 62 6c 65 2c 20 70 45  Expr, iTable, pE
25978 4c 69 73 74 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61  List);.  }.}.sta
25979 74 69 63 20 76 6f 69 64 20 73 75 62 73 74 53 65  tic void substSe
2597a 6c 65 63 74 28 0a 20 20 73 71 6c 69 74 65 33 20  lect(.  sqlite3 
2597b 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f 2a 20  *db,         /* 
2597c 52 65 70 6f 72 74 20 6d 61 6c 6c 6f 63 20 65 72  Report malloc er
2597d 72 6f 72 73 20 68 65 72 65 20 2a 2f 0a 20 20 53  rors here */.  S
2597e 65 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20  elect *p,       
2597f 20 20 20 20 2f 2a 20 53 45 4c 45 43 54 20 73 74      /* SELECT st
25980 61 74 65 6d 65 6e 74 20 69 6e 20 77 68 69 63 68  atement in which
25981 20 74 6f 20 6d 61 6b 65 20 73 75 62 73 74 69 74   to make substit
25982 75 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74 20  utions */.  int 
25983 69 54 61 62 6c 65 2c 20 20 20 20 20 20 20 20 20  iTable,         
25984 20 2f 2a 20 54 61 62 6c 65 20 74 6f 20 62 65 20   /* Table to be 
25985 72 65 70 6c 61 63 65 64 20 2a 2f 0a 20 20 45 78  replaced */.  Ex
25986 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74 20 20  prList *pEList  
25987 20 20 20 2f 2a 20 53 75 62 73 74 69 74 75 74 65     /* Substitute
25988 20 76 61 6c 75 65 73 20 2a 2f 0a 29 7b 0a 20 20   values */.){.  
25989 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20  SrcList *pSrc;. 
2598a 20 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f   struct SrcList_
2598b 69 74 65 6d 20 2a 70 49 74 65 6d 3b 0a 20 20 69  item *pItem;.  i
2598c 6e 74 20 69 3b 0a 20 20 69 66 28 20 21 70 20 29  nt i;.  if( !p )
2598d 20 72 65 74 75 72 6e 3b 0a 20 20 73 75 62 73 74   return;.  subst
2598e 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e  ExprList(db, p->
2598f 70 45 4c 69 73 74 2c 20 69 54 61 62 6c 65 2c 20  pEList, iTable, 
25990 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73 74  pEList);.  subst
25991 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70 2d 3e  ExprList(db, p->
25992 70 47 72 6f 75 70 42 79 2c 20 69 54 61 62 6c 65  pGroupBy, iTable
25993 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62  , pEList);.  sub
25994 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c 20 70  stExprList(db, p
25995 2d 3e 70 4f 72 64 65 72 42 79 2c 20 69 54 61 62  ->pOrderBy, iTab
25996 6c 65 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 73  le, pEList);.  s
25997 75 62 73 74 45 78 70 72 28 64 62 2c 20 70 2d 3e  ubstExpr(db, p->
25998 70 48 61 76 69 6e 67 2c 20 69 54 61 62 6c 65 2c  pHaving, iTable,
25999 20 70 45 4c 69 73 74 29 3b 0a 20 20 73 75 62 73   pEList);.  subs
2599a 74 45 78 70 72 28 64 62 2c 20 70 2d 3e 70 57 68  tExpr(db, p->pWh
2599b 65 72 65 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c  ere, iTable, pEL
2599c 69 73 74 29 3b 0a 20 20 73 75 62 73 74 53 65 6c  ist);.  substSel
2599d 65 63 74 28 64 62 2c 20 70 2d 3e 70 50 72 69 6f  ect(db, p->pPrio
2599e 72 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  r, iTable, pELis
2599f 74 29 3b 0a 20 20 70 53 72 63 20 3d 20 70 2d 3e  t);.  pSrc = p->
259a0 70 53 72 63 3b 0a 20 20 69 66 28 20 70 53 72 63  pSrc;.  if( pSrc
259a1 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 70 53   ){.    for(i=pS
259a2 72 63 2d 3e 6e 53 72 63 2c 20 70 49 74 65 6d 3d  rc->nSrc, pItem=
259a3 70 53 72 63 2d 3e 61 3b 20 69 3e 30 3b 20 69 2d  pSrc->a; i>0; i-
259a4 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20 20  -, pItem++){.   
259a5 20 20 20 73 75 62 73 74 53 65 6c 65 63 74 28 64     substSelect(d
259a6 62 2c 20 70 49 74 65 6d 2d 3e 70 53 65 6c 65 63  b, pItem->pSelec
259a7 74 2c 20 69 54 61 62 6c 65 2c 20 70 45 4c 69 73  t, iTable, pELis
259a8 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a  t);.    }.  }.}.
259a9 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e  #endif /* !defin
259aa 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53  ed(SQLITE_OMIT_S
259ab 55 42 51 55 45 52 59 29 20 7c 7c 20 21 64 65 66  UBQUERY) || !def
259ac 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
259ad 5f 56 49 45 57 29 20 2a 2f 0a 0a 23 69 66 20 21  _VIEW) */..#if !
259ae 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f  defined(SQLITE_O
259af 4d 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c  MIT_SUBQUERY) ||
259b0 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45   !defined(SQLITE
259b1 5f 4f 4d 49 54 5f 56 49 45 57 29 0a 2f 2a 0a 2a  _OMIT_VIEW)./*.*
259b2 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 61  * This routine a
259b3 74 74 65 6d 70 74 73 20 74 6f 20 66 6c 61 74 74  ttempts to flatt
259b4 65 6e 20 73 75 62 71 75 65 72 69 65 73 20 69 6e  en subqueries in
259b5 20 6f 72 64 65 72 20 74 6f 20 73 70 65 65 64 0a   order to speed.
259b6 2a 2a 20 65 78 65 63 75 74 69 6f 6e 2e 20 20 49  ** execution.  I
259b7 74 20 72 65 74 75 72 6e 73 20 31 20 69 66 20 69  t returns 1 if i
259b8 74 20 6d 61 6b 65 73 20 63 68 61 6e 67 65 73 20  t makes changes 
259b9 61 6e 64 20 30 20 69 66 20 6e 6f 20 66 6c 61 74  and 0 if no flat
259ba 74 65 6e 69 6e 67 0a 2a 2a 20 6f 63 63 75 72 73  tening.** occurs
259bb 2e 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 6e 64 65 72  ..**.** To under
259bc 73 74 61 6e 64 20 74 68 65 20 63 6f 6e 63 65 70  stand the concep
259bd 74 20 6f 66 20 66 6c 61 74 74 65 6e 69 6e 67 2c  t of flattening,
259be 20 63 6f 6e 73 69 64 65 72 20 74 68 65 20 66 6f   consider the fo
259bf 6c 6c 6f 77 69 6e 67 0a 2a 2a 20 71 75 65 72 79  llowing.** query
259c0 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45  :.**.**     SELE
259c1 43 54 20 61 20 46 52 4f 4d 20 28 53 45 4c 45 43  CT a FROM (SELEC
259c2 54 20 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20  T x+y AS a FROM 
259c3 74 31 20 57 48 45 52 45 20 7a 3c 31 30 30 29 20  t1 WHERE z<100) 
259c4 57 48 45 52 45 20 61 3e 35 0a 2a 2a 0a 2a 2a 20  WHERE a>5.**.** 
259c5 54 68 65 20 64 65 66 61 75 6c 74 20 77 61 79 20  The default way 
259c6 6f 66 20 69 6d 70 6c 65 6d 65 6e 74 69 6e 67 20  of implementing 
259c7 74 68 69 73 20 71 75 65 72 79 20 69 73 20 74 6f  this query is to
259c8 20 65 78 65 63 75 74 65 20 74 68 65 0a 2a 2a 20   execute the.** 
259c9 73 75 62 71 75 65 72 79 20 66 69 72 73 74 20 61  subquery first a
259ca 6e 64 20 73 74 6f 72 65 20 74 68 65 20 72 65 73  nd store the res
259cb 75 6c 74 73 20 69 6e 20 61 20 74 65 6d 70 6f 72  ults in a tempor
259cc 61 72 79 20 74 61 62 6c 65 2c 20 74 68 65 6e 0a  ary table, then.
259cd 2a 2a 20 72 75 6e 20 74 68 65 20 6f 75 74 65 72  ** run the outer
259ce 20 71 75 65 72 79 20 6f 6e 20 74 68 61 74 20 74   query on that t
259cf 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 2e 20  emporary table. 
259d0 20 54 68 69 73 20 72 65 71 75 69 72 65 73 20 74   This requires t
259d1 77 6f 0a 2a 2a 20 70 61 73 73 65 73 20 6f 76 65  wo.** passes ove
259d2 72 20 74 68 65 20 64 61 74 61 2e 20 20 46 75 72  r the data.  Fur
259d3 74 68 65 72 6d 6f 72 65 2c 20 62 65 63 61 75 73  thermore, becaus
259d4 65 20 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20  e the temporary 
259d5 74 61 62 6c 65 0a 2a 2a 20 68 61 73 20 6e 6f 20  table.** has no 
259d6 69 6e 64 69 63 65 73 2c 20 74 68 65 20 57 48 45  indices, the WHE
259d7 52 45 20 63 6c 61 75 73 65 20 6f 6e 20 74 68 65  RE clause on the
259d8 20 6f 75 74 65 72 20 71 75 65 72 79 20 63 61 6e   outer query can
259d9 6e 6f 74 20 62 65 0a 2a 2a 20 6f 70 74 69 6d 69  not be.** optimi
259da 7a 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  zed..**.** This 
259db 72 6f 75 74 69 6e 65 20 61 74 74 65 6d 70 74 73  routine attempts
259dc 20 74 6f 20 72 65 77 72 69 74 65 20 71 75 65 72   to rewrite quer
259dd 69 65 73 20 73 75 63 68 20 61 73 20 74 68 65 20  ies such as the 
259de 61 62 6f 76 65 20 69 6e 74 6f 0a 2a 2a 20 61 20  above into.** a 
259df 73 69 6e 67 6c 65 20 66 6c 61 74 20 73 65 6c 65  single flat sele
259e0 63 74 2c 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a  ct, like this:.*
259e1 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
259e2 78 2b 79 20 41 53 20 61 20 46 52 4f 4d 20 74 31  x+y AS a FROM t1
259e3 20 57 48 45 52 45 20 7a 3c 31 30 30 20 41 4e 44   WHERE z<100 AND
259e4 20 61 3e 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63   a>5.**.** The c
259e5 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20 66 6f  ode generated fo
259e6 72 20 74 68 69 73 20 73 69 6d 70 69 66 69 63 61  r this simpifica
259e7 74 69 6f 6e 20 67 69 76 65 73 20 74 68 65 20 73  tion gives the s
259e8 61 6d 65 20 72 65 73 75 6c 74 0a 2a 2a 20 62 75  ame result.** bu
259e9 74 20 6f 6e 6c 79 20 68 61 73 20 74 6f 20 73 63  t only has to sc
259ea 61 6e 20 74 68 65 20 64 61 74 61 20 6f 6e 63 65  an the data once
259eb 2e 20 20 41 6e 64 20 62 65 63 61 75 73 65 20 69  .  And because i
259ec 6e 64 69 63 65 73 20 6d 69 67 68 74 20 0a 2a 2a  ndices might .**
259ed 20 65 78 69 73 74 20 6f 6e 20 74 68 65 20 74 61   exist on the ta
259ee 62 6c 65 20 74 31 2c 20 61 20 63 6f 6d 70 6c 65  ble t1, a comple
259ef 74 65 20 73 63 61 6e 20 6f 66 20 74 68 65 20 64  te scan of the d
259f0 61 74 61 20 6d 69 67 68 74 20 62 65 0a 2a 2a 20  ata might be.** 
259f1 61 76 6f 69 64 65 64 2e 0a 2a 2a 0a 2a 2a 20 46  avoided..**.** F
259f2 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6f 6e 6c  lattening is onl
259f3 79 20 61 74 74 65 6d 70 74 65 64 20 69 66 20 61  y attempted if a
259f4 6c 6c 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77  ll of the follow
259f5 69 6e 67 20 61 72 65 20 74 72 75 65 3a 0a 2a 2a  ing are true:.**
259f6 0a 2a 2a 20 20 20 28 31 29 20 20 54 68 65 20 73  .**   (1)  The s
259f7 75 62 71 75 65 72 79 20 61 6e 64 20 74 68 65 20  ubquery and the 
259f8 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f 20 6e  outer query do n
259f9 6f 74 20 62 6f 74 68 20 75 73 65 20 61 67 67 72  ot both use aggr
259fa 65 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 20 20  egates..**.**   
259fb 28 32 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (2)  The subquer
259fc 79 20 69 73 20 6e 6f 74 20 61 6e 20 61 67 67 72  y is not an aggr
259fd 65 67 61 74 65 20 6f 72 20 74 68 65 20 6f 75 74  egate or the out
259fe 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f 74 20  er query is not 
259ff 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20  a join..**.**   
25a00 28 33 29 20 20 54 68 65 20 73 75 62 71 75 65 72  (3)  The subquer
25a01 79 20 69 73 20 6e 6f 74 20 74 68 65 20 72 69 67  y is not the rig
25a02 68 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 20  ht operand of a 
25a03 6c 65 66 74 20 6f 75 74 65 72 20 6a 6f 69 6e 0a  left outer join.
25a04 2a 2a 20 20 20 20 20 20 20 20 28 4f 72 69 67 69  **        (Origi
25a05 6e 61 6c 6c 79 20 74 69 63 6b 65 74 20 23 33 30  nally ticket #30
25a06 36 2e 20 20 53 74 72 65 6e 67 68 74 65 6e 65 64  6.  Strenghtened
25a07 20 62 79 20 74 69 63 6b 65 74 20 23 33 33 30 30   by ticket #3300
25a08 29 0a 2a 2a 0a 2a 2a 20 20 20 28 34 29 20 20 54  ).**.**   (4)  T
25a09 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20 6e  he subquery is n
25a0a 6f 74 20 44 49 53 54 49 4e 43 54 20 6f 72 20 74  ot DISTINCT or t
25a0b 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69  he outer query i
25a0c 73 20 6e 6f 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a  s not a join..**
25a0d 0a 2a 2a 20 20 20 28 35 29 20 20 54 68 65 20 73  .**   (5)  The s
25a0e 75 62 71 75 65 72 79 20 69 73 20 6e 6f 74 20 44  ubquery is not D
25a0f 49 53 54 49 4e 43 54 20 6f 72 20 74 68 65 20 6f  ISTINCT or the o
25a10 75 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20  uter query does 
25a11 6e 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20  not use.**      
25a12 20 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a    aggregates..**
25a13 0a 2a 2a 20 20 20 28 36 29 20 20 54 68 65 20 73  .**   (6)  The s
25a14 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74  ubquery does not
25a15 20 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20   use aggregates 
25a16 6f 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  or the outer que
25a17 72 79 20 69 73 20 6e 6f 74 0a 2a 2a 20 20 20 20  ry is not.**    
25a18 20 20 20 20 44 49 53 54 49 4e 43 54 2e 0a 2a 2a      DISTINCT..**
25a19 0a 2a 2a 20 20 20 28 37 29 20 20 54 68 65 20 73  .**   (7)  The s
25a1a 75 62 71 75 65 72 79 20 68 61 73 20 61 20 46 52  ubquery has a FR
25a1b 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a  OM clause..**.**
25a1c 20 20 20 28 38 29 20 20 54 68 65 20 73 75 62 71     (8)  The subq
25a1d 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
25a1e 65 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f  e LIMIT or the o
25a1f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 6e 6f  uter query is no
25a20 74 20 61 20 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20  t a join..**.** 
25a21 20 20 28 39 29 20 20 54 68 65 20 73 75 62 71 75    (9)  The subqu
25a22 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73 65  ery does not use
25a23 20 4c 49 4d 49 54 20 6f 72 20 74 68 65 20 6f 75   LIMIT or the ou
25a24 74 65 72 20 71 75 65 72 79 20 64 6f 65 73 20 6e  ter query does n
25a25 6f 74 20 75 73 65 0a 2a 2a 20 20 20 20 20 20 20  ot use.**       
25a26 20 61 67 67 72 65 67 61 74 65 73 2e 0a 2a 2a 0a   aggregates..**.
25a27 2a 2a 20 20 28 31 30 29 20 20 54 68 65 20 73 75  **  (10)  The su
25a28 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20  bquery does not 
25a29 75 73 65 20 61 67 67 72 65 67 61 74 65 73 20 6f  use aggregates o
25a2a 72 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  r the outer quer
25a2b 79 20 64 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20  y does not.**   
25a2c 20 20 20 20 20 75 73 65 20 4c 49 4d 49 54 2e 0a       use LIMIT..
25a2d 2a 2a 0a 2a 2a 20 20 28 31 31 29 20 20 54 68 65  **.**  (11)  The
25a2e 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 74 68   subquery and th
25a2f 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64 6f  e outer query do
25a30 20 6e 6f 74 20 62 6f 74 68 20 68 61 76 65 20 4f   not both have O
25a31 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 73 2e  RDER BY clauses.
25a32 0a 2a 2a 0a 2a 2a 20 20 28 31 32 29 20 20 4e 6f  .**.**  (12)  No
25a33 74 20 69 6d 70 6c 65 6d 65 6e 74 65 64 2e 20 20  t implemented.  
25a34 53 75 62 73 75 6d 65 64 20 69 6e 74 6f 20 72 65  Subsumed into re
25a35 73 74 72 69 63 74 69 6f 6e 20 28 33 29 2e 20 20  striction (3).  
25a36 57 61 73 20 70 72 65 76 69 6f 75 73 6c 79 0a 2a  Was previously.*
25a37 2a 20 20 20 20 20 20 20 20 61 20 73 65 70 61 72  *        a separ
25a38 61 74 65 20 72 65 73 74 72 69 63 74 69 6f 6e 20  ate restriction 
25a39 64 65 72 69 76 69 6e 67 20 66 72 6f 6d 20 74 69  deriving from ti
25a3a 63 6b 65 74 20 23 33 35 30 2e 0a 2a 2a 0a 2a 2a  cket #350..**.**
25a3b 20 20 28 31 33 29 20 20 54 68 65 20 73 75 62 71    (13)  The subq
25a3c 75 65 72 79 20 61 6e 64 20 6f 75 74 65 72 20 71  uery and outer q
25a3d 75 65 72 79 20 64 6f 20 6e 6f 74 20 62 6f 74 68  uery do not both
25a3e 20 75 73 65 20 4c 49 4d 49 54 0a 2a 2a 0a 2a 2a   use LIMIT.**.**
25a3f 20 20 28 31 34 29 20 20 54 68 65 20 73 75 62 71    (14)  The subq
25a40 75 65 72 79 20 64 6f 65 73 20 6e 6f 74 20 75 73  uery does not us
25a41 65 20 4f 46 46 53 45 54 0a 2a 2a 0a 2a 2a 20 20  e OFFSET.**.**  
25a42 28 31 35 29 20 20 54 68 65 20 6f 75 74 65 72 20  (15)  The outer 
25a43 71 75 65 72 79 20 69 73 20 6e 6f 74 20 70 61 72  query is not par
25a44 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t of a compound 
25a45 73 65 6c 65 63 74 20 6f 72 20 74 68 65 0a 2a 2a  select or the.**
25a46 20 20 20 20 20 20 20 20 73 75 62 71 75 65 72 79          subquery
25a47 20 64 6f 65 73 20 6e 6f 74 20 68 61 76 65 20 62   does not have b
25a48 6f 74 68 20 61 6e 20 4f 52 44 45 52 20 42 59 20  oth an ORDER BY 
25a49 61 6e 64 20 61 20 4c 49 4d 49 54 20 63 6c 61 75  and a LIMIT clau
25a4a 73 65 2e 0a 2a 2a 20 20 20 20 20 20 20 20 28 53  se..**        (S
25a4b 65 65 20 74 69 63 6b 65 74 20 23 32 33 33 39 29  ee ticket #2339)
25a4c 0a 2a 2a 0a 2a 2a 20 20 28 31 36 29 20 20 54 68  .**.**  (16)  Th
25a4d 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 69 73  e outer query is
25a4e 20 6e 6f 74 20 61 6e 20 61 67 67 72 65 67 61 74   not an aggregat
25a4f 65 20 6f 72 20 74 68 65 20 73 75 62 71 75 65 72  e or the subquer
25a50 79 20 64 6f 65 73 0a 2a 2a 20 20 20 20 20 20 20  y does.**       
25a51 20 6e 6f 74 20 63 6f 6e 74 61 69 6e 20 4f 52 44   not contain ORD
25a52 45 52 20 42 59 2e 20 20 28 54 69 63 6b 65 74 20  ER BY.  (Ticket 
25a53 23 32 39 34 32 29 20 20 54 68 69 73 20 75 73 65  #2942)  This use
25a54 64 20 74 6f 20 6e 6f 74 20 6d 61 74 74 65 72 0a  d to not matter.
25a55 2a 2a 20 20 20 20 20 20 20 20 75 6e 74 69 6c 20  **        until 
25a56 77 65 20 69 6e 74 72 6f 64 75 63 65 64 20 74 68  we introduced th
25a57 65 20 67 72 6f 75 70 5f 63 6f 6e 63 61 74 28 29  e group_concat()
25a58 20 66 75 6e 63 74 69 6f 6e 2e 20 20 0a 2a 2a 0a   function.  .**.
25a59 2a 2a 20 20 28 31 37 29 20 20 54 68 65 20 73 75  **  (17)  The su
25a5a 62 2d 71 75 65 72 79 20 69 73 20 6e 6f 74 20 61  b-query is not a
25a5b 20 63 6f 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74   compound select
25a5c 2c 20 6f 72 20 69 74 20 69 73 20 61 20 55 4e 49  , or it is a UNI
25a5d 4f 4e 20 41 4c 4c 20 0a 2a 2a 20 20 20 20 20 20  ON ALL .**      
25a5e 20 20 63 6f 6d 70 6f 75 6e 64 20 63 6c 61 75 73    compound claus
25a5f 65 20 6d 61 64 65 20 75 70 20 65 6e 74 69 72 65  e made up entire
25a60 6c 79 20 6f 66 20 6e 6f 6e 2d 61 67 67 72 65 67  ly of non-aggreg
25a61 61 74 65 20 71 75 65 72 69 65 73 2c 20 61 6e 64  ate queries, and
25a62 20 0a 2a 2a 20 20 20 20 20 20 20 20 74 68 65 20   .**        the 
25a63 70 61 72 65 6e 74 20 71 75 65 72 79 3a 0a 2a 2a  parent query:.**
25a64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 69  .**          * i
25a65 73 20 6e 6f 74 20 69 74 73 65 6c 66 20 70 61 72  s not itself par
25a66 74 20 6f 66 20 61 20 63 6f 6d 70 6f 75 6e 64 20  t of a compound 
25a67 73 65 6c 65 63 74 2c 0a 2a 2a 20 20 20 20 20 20  select,.**      
25a68 20 20 20 20 2a 20 69 73 20 6e 6f 74 20 61 6e 20      * is not an 
25a69 61 67 67 72 65 67 61 74 65 20 6f 72 20 44 49 53  aggregate or DIS
25a6a 54 49 4e 43 54 20 71 75 65 72 79 2c 20 61 6e 64  TINCT query, and
25a6b 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 2a 20 68  .**          * h
25a6c 61 73 20 6e 6f 20 6f 74 68 65 72 20 74 61 62 6c  as no other tabl
25a6d 65 73 20 6f 72 20 73 75 62 2d 73 65 6c 65 63 74  es or sub-select
25a6e 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
25a6f 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ause..**.**     
25a70 20 20 20 54 68 65 20 70 61 72 65 6e 74 20 61 6e     The parent an
25a71 64 20 73 75 62 2d 71 75 65 72 79 20 6d 61 79 20  d sub-query may 
25a72 63 6f 6e 74 61 69 6e 20 57 48 45 52 45 20 63 6c  contain WHERE cl
25a73 61 75 73 65 73 2e 20 53 75 62 6a 65 63 74 20 74  auses. Subject t
25a74 6f 0a 2a 2a 20 20 20 20 20 20 20 20 72 75 6c 65  o.**        rule
25a75 73 20 28 31 31 29 2c 20 28 31 33 29 20 61 6e 64  s (11), (13) and
25a76 20 28 31 34 29 2c 20 74 68 65 79 20 6d 61 79 20   (14), they may 
25a77 61 6c 73 6f 20 63 6f 6e 74 61 69 6e 20 4f 52 44  also contain ORD
25a78 45 52 20 42 59 2c 0a 2a 2a 20 20 20 20 20 20 20  ER BY,.**       
25a79 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
25a7a 54 20 63 6c 61 75 73 65 73 2e 0a 2a 2a 0a 2a 2a  T clauses..**.**
25a7b 20 20 28 31 38 29 20 20 49 66 20 74 68 65 20 73    (18)  If the s
25a7c 75 62 2d 71 75 65 72 79 20 69 73 20 61 20 63 6f  ub-query is a co
25a7d 6d 70 6f 75 6e 64 20 73 65 6c 65 63 74 2c 20 74  mpound select, t
25a7e 68 65 6e 20 61 6c 6c 20 74 65 72 6d 73 20 6f 66  hen all terms of
25a7f 20 74 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 4f   the.**        O
25a80 52 44 45 52 20 62 79 20 63 6c 61 75 73 65 20 6f  RDER by clause o
25a81 66 20 74 68 65 20 70 61 72 65 6e 74 20 6d 75 73  f the parent mus
25a82 74 20 62 65 20 73 69 6d 70 6c 65 20 72 65 66 65  t be simple refe
25a83 72 65 6e 63 65 73 20 74 6f 20 0a 2a 2a 20 20 20  rences to .**   
25a84 20 20 20 20 20 63 6f 6c 75 6d 6e 73 20 6f 66 20       columns of 
25a85 74 68 65 20 73 75 62 2d 71 75 65 72 79 2e 0a 2a  the sub-query..*
25a86 2a 0a 2a 2a 20 20 28 31 39 29 20 20 54 68 65 20  *.**  (19)  The 
25a87 73 75 62 71 75 65 72 79 20 64 6f 65 73 20 6e 6f  subquery does no
25a88 74 20 75 73 65 20 4c 49 4d 49 54 20 6f 72 20 74  t use LIMIT or t
25a89 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 64  he outer query d
25a8a 6f 65 73 20 6e 6f 74 0a 2a 2a 20 20 20 20 20 20  oes not.**      
25a8b 20 20 68 61 76 65 20 61 20 57 48 45 52 45 20 63    have a WHERE c
25a8c 6c 61 75 73 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  lause..**.** In 
25a8d 74 68 69 73 20 72 6f 75 74 69 6e 65 2c 20 74 68  this routine, th
25a8e 65 20 22 70 22 20 70 61 72 61 6d 65 74 65 72 20  e "p" parameter 
25a8f 69 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  is a pointer to 
25a90 74 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 2e  the outer query.
25a91 0a 2a 2a 20 54 68 65 20 73 75 62 71 75 65 72 79  .** The subquery
25a92 20 69 73 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 69   is p->pSrc->a[i
25a93 46 72 6f 6d 5d 2e 20 20 69 73 41 67 67 20 69 73  From].  isAgg is
25a94 20 74 72 75 65 20 69 66 20 74 68 65 20 6f 75 74   true if the out
25a95 65 72 20 71 75 65 72 79 0a 2a 2a 20 75 73 65 73  er query.** uses
25a96 20 61 67 67 72 65 67 61 74 65 73 20 61 6e 64 20   aggregates and 
25a97 73 75 62 71 75 65 72 79 49 73 41 67 67 20 69 73  subqueryIsAgg is
25a98 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75 62   true if the sub
25a99 71 75 65 72 79 20 75 73 65 73 20 61 67 67 72 65  query uses aggre
25a9a 67 61 74 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  gates..**.** If 
25a9b 66 6c 61 74 74 65 6e 69 6e 67 20 69 73 20 6e 6f  flattening is no
25a9c 74 20 61 74 74 65 6d 70 74 65 64 2c 20 74 68 69  t attempted, thi
25a9d 73 20 72 6f 75 74 69 6e 65 20 69 73 20 61 20 6e  s routine is a n
25a9e 6f 2d 6f 70 20 61 6e 64 20 72 65 74 75 72 6e 73  o-op and returns
25a9f 20 30 2e 0a 2a 2a 20 49 66 20 66 6c 61 74 74 65   0..** If flatte
25aa0 6e 69 6e 67 20 69 73 20 61 74 74 65 6d 70 74 65  ning is attempte
25aa1 64 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72  d this routine r
25aa2 65 74 75 72 6e 73 20 31 2e 0a 2a 2a 0a 2a 2a 20  eturns 1..**.** 
25aa3 41 6c 6c 20 6f 66 20 74 68 65 20 65 78 70 72 65  All of the expre
25aa4 73 73 69 6f 6e 20 61 6e 61 6c 79 73 69 73 20 6d  ssion analysis m
25aa5 75 73 74 20 6f 63 63 75 72 20 6f 6e 20 62 6f 74  ust occur on bot
25aa6 68 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  h the outer quer
25aa7 79 20 61 6e 64 0a 2a 2a 20 74 68 65 20 73 75 62  y and.** the sub
25aa8 71 75 65 72 79 20 62 65 66 6f 72 65 20 74 68 69  query before thi
25aa9 73 20 72 6f 75 74 69 6e 65 20 72 75 6e 73 2e 0a  s routine runs..
25aaa 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c  */.static int fl
25aab 61 74 74 65 6e 53 75 62 71 75 65 72 79 28 0a 20  attenSubquery(. 
25aac 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
25aad 20 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67        /* Parsing
25aae 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
25aaf 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
25ab0 20 20 20 2f 2a 20 54 68 65 20 70 61 72 65 6e 74     /* The parent
25ab1 20 6f 72 20 6f 75 74 65 72 20 53 45 4c 45 43 54   or outer SELECT
25ab2 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20   statement */.  
25ab3 69 6e 74 20 69 46 72 6f 6d 2c 20 20 20 20 20 20  int iFrom,      
25ab4 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 69 6e       /* Index in
25ab5 20 70 2d 3e 70 53 72 63 2d 3e 61 5b 5d 20 6f 66   p->pSrc->a[] of
25ab6 20 74 68 65 20 69 6e 6e 65 72 20 73 75 62 71 75   the inner subqu
25ab7 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 73 41  ery */.  int isA
25ab8 67 67 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a  gg,           /*
25ab9 20 54 72 75 65 20 69 66 20 6f 75 74 65 72 20 53   True if outer S
25aba 45 4c 45 43 54 20 75 73 65 73 20 61 67 67 72 65  ELECT uses aggre
25abb 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 20 2a  gate functions *
25abc 2f 0a 20 20 69 6e 74 20 73 75 62 71 75 65 72 79  /.  int subquery
25abd 49 73 41 67 67 20 20 20 20 2f 2a 20 54 72 75 65  IsAgg    /* True
25abe 20 69 66 20 74 68 65 20 73 75 62 71 75 65 72 79   if the subquery
25abf 20 75 73 65 73 20 61 67 67 72 65 67 61 74 65 20   uses aggregate 
25ac0 66 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 29 7b 0a  functions */.){.
25ac1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
25ac2 61 76 65 64 41 75 74 68 43 6f 6e 74 65 78 74 20  avedAuthContext 
25ac3 3d 20 70 50 61 72 73 65 2d 3e 7a 41 75 74 68 43  = pParse->zAuthC
25ac4 6f 6e 74 65 78 74 3b 0a 20 20 53 65 6c 65 63 74  ontext;.  Select
25ac5 20 2a 70 50 61 72 65 6e 74 3b 0a 20 20 53 65 6c   *pParent;.  Sel
25ac6 65 63 74 20 2a 70 53 75 62 3b 20 20 20 20 20 20  ect *pSub;      
25ac7 20 2f 2a 20 54 68 65 20 69 6e 6e 65 72 20 71 75   /* The inner qu
25ac8 65 72 79 20 6f 72 20 22 73 75 62 71 75 65 72 79  ery or "subquery
25ac9 22 20 2a 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70  " */.  Select *p
25aca 53 75 62 31 3b 20 20 20 20 20 20 2f 2a 20 50 6f  Sub1;      /* Po
25acb 69 6e 74 65 72 20 74 6f 20 74 68 65 20 72 69 67  inter to the rig
25acc 68 74 6d 6f 73 74 20 73 65 6c 65 63 74 20 69 6e  htmost select in
25acd 20 73 75 62 2d 71 75 65 72 79 20 2a 2f 0a 20 20   sub-query */.  
25ace 53 72 63 4c 69 73 74 20 2a 70 53 72 63 3b 20 20  SrcList *pSrc;  
25acf 20 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20      /* The FROM 
25ad0 63 6c 61 75 73 65 20 6f 66 20 74 68 65 20 6f 75  clause of the ou
25ad1 74 65 72 20 71 75 65 72 79 20 2a 2f 0a 20 20 53  ter query */.  S
25ad2 72 63 4c 69 73 74 20 2a 70 53 75 62 53 72 63 3b  rcList *pSubSrc;
25ad3 20 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63     /* The FROM c
25ad4 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75 62  lause of the sub
25ad5 71 75 65 72 79 20 2a 2f 0a 20 20 45 78 70 72 4c  query */.  ExprL
25ad6 69 73 74 20 2a 70 4c 69 73 74 3b 20 20 20 20 2f  ist *pList;    /
25ad7 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65 74  * The result set
25ad8 20 6f 66 20 74 68 65 20 6f 75 74 65 72 20 71 75   of the outer qu
25ad9 65 72 79 20 2a 2f 0a 20 20 69 6e 74 20 69 50 61  ery */.  int iPa
25ada 72 65 6e 74 3b 20 20 20 20 20 20 20 20 2f 2a 20  rent;        /* 
25adb 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75 6d 62  VDBE cursor numb
25adc 65 72 20 6f 66 20 74 68 65 20 70 53 75 62 20 72  er of the pSub r
25add 65 73 75 6c 74 20 73 65 74 20 74 65 6d 70 20 74  esult set temp t
25ade 61 62 6c 65 20 2a 2f 0a 20 20 69 6e 74 20 69 3b  able */.  int i;
25adf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
25ae0 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20 2a 2f   Loop counter */
25ae1 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72 65 3b  .  Expr *pWhere;
25ae2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25ae3 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45      /* The WHERE
25ae4 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 73 74 72   clause */.  str
25ae5 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
25ae6 20 2a 70 53 75 62 69 74 65 6d 3b 20 20 20 2f 2a   *pSubitem;   /*
25ae7 20 54 68 65 20 73 75 62 71 75 65 72 79 20 2a 2f   The subquery */
25ae8 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
25ae9 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
25aea 2f 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  /* Check to see 
25aeb 69 66 20 66 6c 61 74 74 65 6e 69 6e 67 20 69 73  if flattening is
25aec 20 70 65 72 6d 69 74 74 65 64 2e 20 20 52 65 74   permitted.  Ret
25aed 75 72 6e 20 30 20 69 66 20 6e 6f 74 2e 0a 20 20  urn 0 if not..  
25aee 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 21 3d  */.  assert( p!=
25aef 30 20 29 3b 0a 20 20 69 66 28 20 70 3d 3d 30 20  0 );.  if( p==0 
25af0 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73  ) return 0;.  as
25af1 73 65 72 74 28 20 70 2d 3e 70 50 72 69 6f 72 3d  sert( p->pPrior=
25af2 3d 30 20 29 3b 20 20 2f 2a 20 55 6e 61 62 6c 65  =0 );  /* Unable
25af3 20 74 6f 20 66 6c 61 74 74 65 6e 20 63 6f 6d 70   to flatten comp
25af4 6f 75 6e 64 20 71 75 65 72 69 65 73 20 2a 2f 0a  ound queries */.
25af5 20 20 70 53 72 63 20 3d 20 70 2d 3e 70 53 72 63    pSrc = p->pSrc
25af6 3b 0a 20 20 61 73 73 65 72 74 28 20 70 53 72 63  ;.  assert( pSrc
25af7 20 26 26 20 69 46 72 6f 6d 3e 3d 30 20 26 26 20   && iFrom>=0 && 
25af8 69 46 72 6f 6d 3c 70 53 72 63 2d 3e 6e 53 72 63  iFrom<pSrc->nSrc
25af9 20 29 3b 0a 20 20 70 53 75 62 69 74 65 6d 20 3d   );.  pSubitem =
25afa 20 26 70 53 72 63 2d 3e 61 5b 69 46 72 6f 6d 5d   &pSrc->a[iFrom]
25afb 3b 0a 20 20 69 50 61 72 65 6e 74 20 3d 20 70 53  ;.  iParent = pS
25afc 75 62 69 74 65 6d 2d 3e 69 43 75 72 73 6f 72 3b  ubitem->iCursor;
25afd 0a 20 20 70 53 75 62 20 3d 20 70 53 75 62 69 74  .  pSub = pSubit
25afe 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 20 20 61  em->pSelect;.  a
25aff 73 73 65 72 74 28 20 70 53 75 62 21 3d 30 20 29  ssert( pSub!=0 )
25b00 3b 0a 20 20 69 66 28 20 69 73 41 67 67 20 26 26  ;.  if( isAgg &&
25b01 20 73 75 62 71 75 65 72 79 49 73 41 67 67 20 29   subqueryIsAgg )
25b02 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20   return 0;      
25b03 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
25b04 73 74 72 69 63 74 69 6f 6e 20 28 31 29 20 20 2a  striction (1)  *
25b05 2f 0a 20 20 69 66 28 20 73 75 62 71 75 65 72 79  /.  if( subquery
25b06 49 73 41 67 67 20 26 26 20 70 53 72 63 2d 3e 6e  IsAgg && pSrc->n
25b07 53 72 63 3e 31 20 29 20 72 65 74 75 72 6e 20 30  Src>1 ) return 0
25b08 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65  ;          /* Re
25b09 73 74 72 69 63 74 69 6f 6e 20 28 32 29 20 20 2a  striction (2)  *
25b0a 2f 0a 20 20 70 53 75 62 53 72 63 20 3d 20 70 53  /.  pSubSrc = pS
25b0b 75 62 2d 3e 70 53 72 63 3b 0a 20 20 61 73 73 65  ub->pSrc;.  asse
25b0c 72 74 28 20 70 53 75 62 53 72 63 20 29 3b 0a 20  rt( pSubSrc );. 
25b0d 20 2f 2a 20 50 72 69 6f 72 20 74 6f 20 76 65 72   /* Prior to ver
25b0e 73 69 6f 6e 20 33 2e 31 2e 32 2c 20 77 68 65 6e  sion 3.1.2, when
25b0f 20 4c 49 4d 49 54 20 61 6e 64 20 4f 46 46 53 45   LIMIT and OFFSE
25b10 54 20 68 61 64 20 74 6f 20 62 65 20 73 69 6d 70  T had to be simp
25b11 6c 65 20 63 6f 6e 73 74 61 6e 74 73 2c 0a 20 20  le constants,.  
25b12 2a 2a 20 6e 6f 74 20 61 72 62 69 74 72 61 72 79  ** not arbitrary
25b13 20 65 78 70 72 65 73 73 73 69 6f 6e 73 2c 20 77   expresssions, w
25b14 65 20 61 6c 6c 6f 77 65 64 20 73 6f 6d 65 20 63  e allowed some c
25b15 6f 6d 62 69 6e 69 6e 67 20 6f 66 20 4c 49 4d 49  ombining of LIMI
25b16 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 20 20 2a  T and OFFSET.  *
25b17 2a 20 62 65 63 61 75 73 65 20 74 68 65 79 20 63  * because they c
25b18 6f 75 6c 64 20 62 65 20 63 6f 6d 70 75 74 65 64  ould be computed
25b19 20 61 74 20 63 6f 6d 70 69 6c 65 2d 74 69 6d 65   at compile-time
25b1a 2e 20 20 42 75 74 20 77 68 65 6e 20 4c 49 4d 49  .  But when LIMI
25b1b 54 20 61 6e 64 20 4f 46 46 53 45 54 0a 20 20 2a  T and OFFSET.  *
25b1c 2a 20 62 65 63 61 6d 65 20 61 72 62 69 74 72 61  * became arbitra
25b1d 72 79 20 65 78 70 72 65 73 73 69 6f 6e 73 2c 20  ry expressions, 
25b1e 77 65 20 77 65 72 65 20 66 6f 72 63 65 64 20 74  we were forced t
25b1f 6f 20 61 64 64 20 72 65 73 74 72 69 63 74 69 6f  o add restrictio
25b20 6e 73 20 28 31 33 29 0a 20 20 2a 2a 20 61 6e 64  ns (13).  ** and
25b21 20 28 31 34 29 2e 20 2a 2f 0a 20 20 69 66 28 20   (14). */.  if( 
25b22 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26 20  pSub->pLimit && 
25b23 70 2d 3e 70 4c 69 6d 69 74 20 29 20 72 65 74 75  p->pLimit ) retu
25b24 72 6e 20 30 3b 20 20 20 20 20 20 20 20 20 20 20  rn 0;           
25b25 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
25b26 6e 20 28 31 33 29 20 2a 2f 0a 20 20 69 66 28 20  n (13) */.  if( 
25b27 70 53 75 62 2d 3e 70 4f 66 66 73 65 74 20 29 20  pSub->pOffset ) 
25b28 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
25b29 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b2a 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f     /* Restrictio
25b2b 6e 20 28 31 34 29 20 2a 2f 0a 20 20 69 66 28 20  n (14) */.  if( 
25b2c 70 2d 3e 70 52 69 67 68 74 6d 6f 73 74 20 26 26  p->pRightmost &&
25b2d 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 20 26 26   pSub->pLimit &&
25b2e 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20   pSub->pOrderBy 
25b2f 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
25b30 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b32 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
25b33 65 73 74 72 69 63 74 69 6f 6e 20 28 31 35 29 20  estriction (15) 
25b34 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 75  */.  }.  if( pSu
25b35 62 53 72 63 2d 3e 6e 53 72 63 3d 3d 30 20 29 20  bSrc->nSrc==0 ) 
25b36 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20 20 20  return 0;       
25b37 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b38 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e 20 28  /* Restriction (
25b39 37 29 20 20 2a 2f 0a 20 20 69 66 28 20 28 28 70  7)  */.  if( ((p
25b3a 53 75 62 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20  Sub->selFlags & 
25b3b 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20  SF_Distinct)!=0 
25b3c 7c 7c 20 70 53 75 62 2d 3e 70 4c 69 6d 69 74 29  || pSub->pLimit)
25b3d 20 0a 20 20 20 20 20 20 20 20 20 26 26 20 28 70   .         && (p
25b3e 53 72 63 2d 3e 6e 53 72 63 3e 31 20 7c 7c 20 69  Src->nSrc>1 || i
25b3f 73 41 67 67 29 20 29 7b 20 20 20 20 20 20 20 20  sAgg) ){        
25b40 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69 6f 6e    /* Restriction
25b41 73 20 28 34 29 28 35 29 28 38 29 28 39 29 20 2a  s (4)(5)(8)(9) *
25b42 2f 0a 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b  /.     return 0;
25b43 20 20 20 20 20 20 20 0a 20 20 7d 0a 20 20 69 66         .  }.  if
25b44 28 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  ( (p->selFlags &
25b45 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30   SF_Distinct)!=0
25b46 20 26 26 20 73 75 62 71 75 65 72 79 49 73 41 67   && subqueryIsAg
25b47 67 20 29 7b 0a 20 20 20 20 20 72 65 74 75 72 6e  g ){.     return
25b48 20 30 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 52   0;         /* R
25b49 65 73 74 72 69 63 74 69 6f 6e 20 28 36 29 20 20  estriction (6)  
25b4a 2a 2f 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  */.  }.  if( p->
25b4b 70 4f 72 64 65 72 42 79 20 26 26 20 70 53 75 62  pOrderBy && pSub
25b4c 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20  ->pOrderBy ){.  
25b4d 20 20 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20     return 0;    
25b4e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25b50 20 20 20 20 20 20 20 2f 2a 20 52 65 73 74 72 69         /* Restri
25b51 63 74 69 6f 6e 20 28 31 31 29 20 2a 2f 0a 20 20  ction (11) */.  
25b52 7d 0a 20 20 69 66 28 20 69 73 41 67 67 20 26 26  }.  if( isAgg &&
25b53 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79 20   pSub->pOrderBy 
25b54 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20 20 20  ) return 0;     
25b55 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
25b56 73 74 72 69 63 74 69 6f 6e 20 28 31 36 29 20 2a  striction (16) *
25b57 2f 0a 20 20 69 66 28 20 70 53 75 62 2d 3e 70 4c  /.  if( pSub->pL
25b58 69 6d 69 74 20 26 26 20 70 2d 3e 70 57 68 65 72  imit && p->pWher
25b59 65 20 29 20 72 65 74 75 72 6e 20 30 3b 20 20 20  e ) return 0;   
25b5a 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52 65             /* Re
25b5b 73 74 72 69 63 74 69 6f 6e 20 28 31 39 29 20 2a  striction (19) *
25b5c 2f 0a 0a 20 20 2f 2a 20 4f 42 53 4f 4c 45 54 45  /..  /* OBSOLETE
25b5d 20 43 4f 4d 4d 45 4e 54 20 31 3a 0a 20 20 2a 2a   COMMENT 1:.  **
25b5e 20 52 65 73 74 72 69 63 74 69 6f 6e 20 33 3a 20   Restriction 3: 
25b5f 20 49 66 20 74 68 65 20 73 75 62 71 75 65 72 79   If the subquery
25b60 20 69 73 20 61 20 6a 6f 69 6e 2c 20 6d 61 6b 65   is a join, make
25b61 20 73 75 72 65 20 74 68 65 20 73 75 62 71 75 65   sure the subque
25b62 72 79 20 69 73 20 0a 20 20 2a 2a 20 6e 6f 74 20  ry is .  ** not 
25b63 75 73 65 64 20 61 73 20 74 68 65 20 72 69 67 68  used as the righ
25b64 74 20 6f 70 65 72 61 6e 64 20 6f 66 20 61 6e 20  t operand of an 
25b65 6f 75 74 65 72 20 6a 6f 69 6e 2e 20 20 45 78 61  outer join.  Exa
25b66 6d 70 6c 65 73 20 6f 66 20 77 68 79 20 74 68 69  mples of why thi
25b67 73 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20 61 6c  s.  ** is not al
25b68 6c 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a  lowed:.  **.  **
25b69 20 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54           t1 LEFT
25b6a 20 4f 55 54 45 52 20 4a 4f 49 4e 20 28 74 32 20   OUTER JOIN (t2 
25b6b 4a 4f 49 4e 20 74 33 29 0a 20 20 2a 2a 0a 20 20  JOIN t3).  **.  
25b6c 2a 2a 20 49 66 20 77 65 20 66 6c 61 74 74 65 6e  ** If we flatten
25b6d 20 74 68 65 20 61 62 6f 76 65 2c 20 77 65 20 77   the above, we w
25b6e 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a 20 20  ould get.  **.  
25b6f 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31 20 4c  **         (t1 L
25b70 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 20 74  EFT OUTER JOIN t
25b71 32 29 20 4a 4f 49 4e 20 74 33 0a 20 20 2a 2a 0a  2) JOIN t3.  **.
25b72 20 20 2a 2a 20 77 68 69 63 68 20 69 73 20 6e 6f    ** which is no
25b73 74 20 61 74 20 61 6c 6c 20 74 68 65 20 73 61 6d  t at all the sam
25b74 65 20 74 68 69 6e 67 2e 0a 20 20 2a 2a 0a 20 20  e thing..  **.  
25b75 2a 2a 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d  ** OBSOLETE COMM
25b76 45 4e 54 20 32 3a 0a 20 20 2a 2a 20 52 65 73 74  ENT 2:.  ** Rest
25b77 72 69 63 74 69 6f 6e 20 31 32 3a 20 20 49 66 20  riction 12:  If 
25b78 74 68 65 20 73 75 62 71 75 65 72 79 20 69 73 20  the subquery is 
25b79 74 68 65 20 72 69 67 68 74 20 6f 70 65 72 61 6e  the right operan
25b7a 64 20 6f 66 20 61 20 6c 65 66 74 20 6f 75 74 65  d of a left oute
25b7b 72 0a 20 20 2a 2a 20 6a 6f 69 6e 2c 20 6d 61 6b  r.  ** join, mak
25b7c 65 20 73 75 72 65 20 74 68 65 20 73 75 62 71 75  e sure the subqu
25b7d 65 72 79 20 68 61 73 20 6e 6f 20 57 48 45 52 45  ery has no WHERE
25b7e 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 20 41 6e   clause..  ** An
25b7f 20 65 78 61 6d 70 6c 65 73 20 6f 66 20 77 68 79   examples of why
25b80 20 74 68 69 73 20 69 73 20 6e 6f 74 20 61 6c 6c   this is not all
25b81 6f 77 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  owed:.  **.  ** 
25b82 20 20 20 20 20 20 20 20 74 31 20 4c 45 46 54 20          t1 LEFT 
25b83 4f 55 54 45 52 20 4a 4f 49 4e 20 28 53 45 4c 45  OUTER JOIN (SELE
25b84 43 54 20 2a 20 46 52 4f 4d 20 74 32 20 57 48 45  CT * FROM t2 WHE
25b85 52 45 20 74 32 2e 78 3e 30 29 0a 20 20 2a 2a 0a  RE t2.x>0).  **.
25b86 20 20 2a 2a 20 49 66 20 77 65 20 66 6c 61 74 74    ** If we flatt
25b87 65 6e 20 74 68 65 20 61 62 6f 76 65 2c 20 77 65  en the above, we
25b88 20 77 6f 75 6c 64 20 67 65 74 0a 20 20 2a 2a 0a   would get.  **.
25b89 20 20 2a 2a 20 20 20 20 20 20 20 20 20 28 74 31    **         (t1
25b8a 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e   LEFT OUTER JOIN
25b8b 20 74 32 29 20 57 48 45 52 45 20 74 32 2e 78 3e   t2) WHERE t2.x>
25b8c 30 0a 20 20 2a 2a 0a 20 20 2a 2a 20 42 75 74 20  0.  **.  ** But 
25b8d 74 68 65 20 74 32 2e 78 3e 30 20 74 65 73 74 20  the t2.x>0 test 
25b8e 77 69 6c 6c 20 61 6c 77 61 79 73 20 66 61 69 6c  will always fail
25b8f 20 6f 6e 20 61 20 4e 55 4c 4c 20 72 6f 77 20 6f   on a NULL row o
25b90 66 20 74 32 2c 20 77 68 69 63 68 0a 20 20 2a 2a  f t2, which.  **
25b91 20 65 66 66 65 63 74 69 76 65 6c 79 20 63 6f 6e   effectively con
25b92 76 65 72 74 73 20 74 68 65 20 4f 55 54 45 52 20  verts the OUTER 
25b93 4a 4f 49 4e 20 69 6e 74 6f 20 61 6e 20 49 4e 4e  JOIN into an INN
25b94 45 52 20 4a 4f 49 4e 2e 0a 20 20 2a 2a 0a 20 20  ER JOIN..  **.  
25b95 2a 2a 20 54 48 49 53 20 4f 56 45 52 52 49 44 45  ** THIS OVERRIDE
25b96 53 20 4f 42 53 4f 4c 45 54 45 20 43 4f 4d 4d 45  S OBSOLETE COMME
25b97 4e 54 53 20 31 20 41 4e 44 20 32 20 41 42 4f 56  NTS 1 AND 2 ABOV
25b98 45 3a 0a 20 20 2a 2a 20 54 69 63 6b 65 74 20 23  E:.  ** Ticket #
25b99 33 33 30 30 20 73 68 6f 77 73 20 74 68 61 74 20  3300 shows that 
25b9a 66 6c 61 74 74 65 6e 69 6e 67 20 74 68 65 20 72  flattening the r
25b9b 69 67 68 74 20 74 65 72 6d 20 6f 66 20 61 20 4c  ight term of a L
25b9c 45 46 54 20 4a 4f 49 4e 0a 20 20 2a 2a 20 69 73  EFT JOIN.  ** is
25b9d 20 66 72 61 75 67 68 74 20 77 69 74 68 20 64 61   fraught with da
25b9e 6e 67 65 72 2e 20 20 42 65 73 74 20 74 6f 20 61  nger.  Best to a
25b9f 76 6f 69 64 20 74 68 65 20 77 68 6f 6c 65 20 74  void the whole t
25ba0 68 69 6e 67 2e 20 20 49 66 20 74 68 65 0a 20 20  hing.  If the.  
25ba1 2a 2a 20 73 75 62 71 75 65 72 79 20 69 73 20 74  ** subquery is t
25ba2 68 65 20 72 69 67 68 74 20 74 65 72 6d 20 6f 66  he right term of
25ba3 20 61 20 4c 45 46 54 20 4a 4f 49 4e 2c 20 74 68   a LEFT JOIN, th
25ba4 65 6e 20 64 6f 20 6e 6f 74 20 66 6c 61 74 74 65  en do not flatte
25ba5 6e 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 70  n..  */.  if( (p
25ba6 53 75 62 69 74 65 6d 2d 3e 6a 6f 69 6e 74 79 70  Subitem->jointyp
25ba7 65 20 26 20 4a 54 5f 4f 55 54 45 52 29 21 3d 30  e & JT_OUTER)!=0
25ba8 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
25ba9 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 73 74  ;.  }..  /* Rest
25baa 72 69 63 74 69 6f 6e 20 31 37 3a 20 49 66 20 74  riction 17: If t
25bab 68 65 20 73 75 62 2d 71 75 65 72 79 20 69 73 20  he sub-query is 
25bac 61 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  a compound SELEC
25bad 54 2c 20 74 68 65 6e 20 69 74 20 6d 75 73 74 0a  T, then it must.
25bae 20 20 2a 2a 20 75 73 65 20 6f 6e 6c 79 20 74 68    ** use only th
25baf 65 20 55 4e 49 4f 4e 20 41 4c 4c 20 6f 70 65 72  e UNION ALL oper
25bb0 61 74 6f 72 2e 20 41 6e 64 20 6e 6f 6e 65 20 6f  ator. And none o
25bb1 66 20 74 68 65 20 73 69 6d 70 6c 65 20 73 65 6c  f the simple sel
25bb2 65 63 74 20 71 75 65 72 69 65 73 0a 20 20 2a 2a  ect queries.  **
25bb3 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 74 68   that make up th
25bb4 65 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c 45 43  e compound SELEC
25bb5 54 20 61 72 65 20 61 6c 6c 6f 77 65 64 20 74 6f  T are allowed to
25bb6 20 62 65 20 61 67 67 72 65 67 61 74 65 20 6f 72   be aggregate or
25bb7 20 64 69 73 74 69 6e 63 74 0a 20 20 2a 2a 20 71   distinct.  ** q
25bb8 75 65 72 69 65 73 2e 0a 20 20 2a 2f 0a 20 20 69  ueries..  */.  i
25bb9 66 28 20 70 53 75 62 2d 3e 70 50 72 69 6f 72 20  f( pSub->pPrior 
25bba 29 7b 0a 20 20 20 20 69 66 28 20 70 2d 3e 70 50  ){.    if( p->pP
25bbb 72 69 6f 72 20 7c 7c 20 69 73 41 67 67 20 7c 7c  rior || isAgg ||
25bbc 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
25bbd 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30 20  SF_Distinct)!=0 
25bbe 7c 7c 20 70 53 72 63 2d 3e 6e 53 72 63 21 3d 31  || pSrc->nSrc!=1
25bbf 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
25bc0 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 66 6f   0;.    }.    fo
25bc1 72 28 70 53 75 62 31 3d 70 53 75 62 3b 20 70 53  r(pSub1=pSub; pS
25bc2 75 62 31 3b 20 70 53 75 62 31 3d 70 53 75 62 31  ub1; pSub1=pSub1
25bc3 2d 3e 70 50 72 69 6f 72 29 7b 0a 20 20 20 20 20  ->pPrior){.     
25bc4 20 69 66 28 20 28 70 53 75 62 31 2d 3e 73 65 6c   if( (pSub1->sel
25bc5 46 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74  Flags & (SF_Dist
25bc6 69 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74  inct|SF_Aggregat
25bc7 65 29 29 21 3d 30 0a 20 20 20 20 20 20 20 7c 7c  e))!=0.       ||
25bc8 20 28 70 53 75 62 31 2d 3e 70 50 72 69 6f 72 20   (pSub1->pPrior 
25bc9 26 26 20 70 53 75 62 31 2d 3e 6f 70 21 3d 54 4b  && pSub1->op!=TK
25bca 5f 41 4c 4c 29 20 0a 20 20 20 20 20 20 20 7c 7c  _ALL) .       ||
25bcb 20 21 70 53 75 62 31 2d 3e 70 53 72 63 20 7c 7c   !pSub1->pSrc ||
25bcc 20 70 53 75 62 31 2d 3e 70 53 72 63 2d 3e 6e 53   pSub1->pSrc->nS
25bcd 72 63 21 3d 31 0a 20 20 20 20 20 20 29 7b 0a 20  rc!=1.      ){. 
25bce 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b         return 0;
25bcf 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
25bd0 20 20 20 20 2f 2a 20 52 65 73 74 72 69 63 74 69      /* Restricti
25bd1 6f 6e 20 31 38 2e 20 2a 2f 0a 20 20 20 20 69 66  on 18. */.    if
25bd2 28 20 70 2d 3e 70 4f 72 64 65 72 42 79 20 29 7b  ( p->pOrderBy ){
25bd3 0a 20 20 20 20 20 20 69 6e 74 20 69 69 3b 0a 20  .      int ii;. 
25bd4 20 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69       for(ii=0; i
25bd5 69 3c 70 2d 3e 70 4f 72 64 65 72 42 79 2d 3e 6e  i<p->pOrderBy->n
25bd6 45 78 70 72 3b 20 69 69 2b 2b 29 7b 0a 20 20 20  Expr; ii++){.   
25bd7 20 20 20 20 20 69 66 28 20 70 2d 3e 70 4f 72 64       if( p->pOrd
25bd8 65 72 42 79 2d 3e 61 5b 69 69 5d 2e 69 43 6f 6c  erBy->a[ii].iCol
25bd9 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ==0 ) return 0;.
25bda 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
25bdb 7d 0a 0a 20 20 2f 2a 2a 2a 2a 2a 20 49 66 20 77  }..  /***** If w
25bdc 65 20 72 65 61 63 68 20 74 68 69 73 20 70 6f 69  e reach this poi
25bdd 6e 74 2c 20 66 6c 61 74 74 65 6e 69 6e 67 20 69  nt, flattening i
25bde 73 20 70 65 72 6d 69 74 74 65 64 2e 20 2a 2a 2a  s permitted. ***
25bdf 2a 2a 2f 0a 0a 20 20 2f 2a 20 41 75 74 68 6f 72  **/..  /* Author
25be0 69 7a 65 20 74 68 65 20 73 75 62 71 75 65 72 79  ize the subquery
25be1 20 2a 2f 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41   */.  pParse->zA
25be2 75 74 68 43 6f 6e 74 65 78 74 20 3d 20 70 53 75  uthContext = pSu
25be3 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 3b 0a 20 20  bitem->zName;.  
25be4 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b  sqlite3AuthCheck
25be5 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f  (pParse, SQLITE_
25be6 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20 30 29  SELECT, 0, 0, 0)
25be7 3b 0a 20 20 70 50 61 72 73 65 2d 3e 7a 41 75 74  ;.  pParse->zAut
25be8 68 43 6f 6e 74 65 78 74 20 3d 20 7a 53 61 76 65  hContext = zSave
25be9 64 41 75 74 68 43 6f 6e 74 65 78 74 3b 0a 0a 20  dAuthContext;.. 
25bea 20 2f 2a 20 49 66 20 74 68 65 20 73 75 62 2d 71   /* If the sub-q
25beb 75 65 72 79 20 69 73 20 61 20 63 6f 6d 70 6f 75  uery is a compou
25bec 6e 64 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d  nd SELECT statem
25bed 65 6e 74 2c 20 74 68 65 6e 20 28 62 79 20 72 65  ent, then (by re
25bee 73 74 72 69 63 74 69 6f 6e 73 0a 20 20 2a 2a 20  strictions.  ** 
25bef 31 37 20 61 6e 64 20 31 38 20 61 62 6f 76 65 29  17 and 18 above)
25bf0 20 69 74 20 6d 75 73 74 20 62 65 20 61 20 55 4e   it must be a UN
25bf1 49 4f 4e 20 41 4c 4c 20 61 6e 64 20 74 68 65 20  ION ALL and the 
25bf2 70 61 72 65 6e 74 20 71 75 65 72 79 20 6d 75 73  parent query mus
25bf3 74 20 0a 20 20 2a 2a 20 62 65 20 6f 66 20 74 68  t .  ** be of th
25bf4 65 20 66 6f 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a  e form:.  **.  *
25bf5 2a 20 20 20 20 20 53 45 4c 45 43 54 20 3c 65 78  *     SELECT <ex
25bf6 70 72 2d 6c 69 73 74 3e 20 46 52 4f 4d 20 28 3c  pr-list> FROM (<
25bf7 73 75 62 2d 71 75 65 72 79 3e 29 20 3c 77 68 65  sub-query>) <whe
25bf8 72 65 2d 63 6c 61 75 73 65 3e 20 0a 20 20 2a 2a  re-clause> .  **
25bf9 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 65 64 20 62  .  ** followed b
25bfa 79 20 61 6e 79 20 4f 52 44 45 52 20 42 59 2c 20  y any ORDER BY, 
25bfb 4c 49 4d 49 54 20 61 6e 64 2f 6f 72 20 4f 46 46  LIMIT and/or OFF
25bfc 53 45 54 20 63 6c 61 75 73 65 73 2e 20 54 68 69  SET clauses. Thi
25bfd 73 20 62 6c 6f 63 6b 0a 20 20 2a 2a 20 63 72 65  s block.  ** cre
25bfe 61 74 65 73 20 4e 2d 31 20 63 6f 70 69 65 73 20  ates N-1 copies 
25bff 6f 66 20 74 68 65 20 70 61 72 65 6e 74 20 71 75  of the parent qu
25c00 65 72 79 20 77 69 74 68 6f 75 74 20 61 6e 79 20  ery without any 
25c01 4f 52 44 45 52 20 42 59 2c 20 4c 49 4d 49 54 20  ORDER BY, LIMIT 
25c02 6f 72 20 0a 20 20 2a 2a 20 4f 46 46 53 45 54 20  or .  ** OFFSET 
25c03 63 6c 61 75 73 65 73 20 61 6e 64 20 6a 6f 69 6e  clauses and join
25c04 73 20 74 68 65 6d 20 74 6f 20 74 68 65 20 6c 65  s them to the le
25c05 66 74 2d 68 61 6e 64 2d 73 69 64 65 20 6f 66 20  ft-hand-side of 
25c06 74 68 65 20 6f 72 69 67 69 6e 61 6c 0a 20 20 2a  the original.  *
25c07 2a 20 75 73 69 6e 67 20 55 4e 49 4f 4e 20 41 4c  * using UNION AL
25c08 4c 20 6f 70 65 72 61 74 6f 72 73 2e 20 49 6e 20  L operators. In 
25c09 74 68 69 73 20 63 61 73 65 20 4e 20 69 73 20 74  this case N is t
25c0a 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 69 6d  he number of sim
25c0b 70 6c 65 0a 20 20 2a 2a 20 73 65 6c 65 63 74 20  ple.  ** select 
25c0c 73 74 61 74 65 6d 65 6e 74 73 20 69 6e 20 74 68  statements in th
25c0d 65 20 63 6f 6d 70 6f 75 6e 64 20 73 75 62 2d 71  e compound sub-q
25c0e 75 65 72 79 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uery..  **.  ** 
25c0f 45 78 61 6d 70 6c 65 3a 0a 20 20 2a 2a 0a 20 20  Example:.  **.  
25c10 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 61 2b  **     SELECT a+
25c11 31 20 46 52 4f 4d 20 28 0a 20 20 2a 2a 20 20 20  1 FROM (.  **   
25c12 20 20 20 20 20 53 45 4c 45 43 54 20 78 20 46 52       SELECT x FR
25c13 4f 4d 20 74 61 62 0a 20 20 2a 2a 20 20 20 20 20  OM tab.  **     
25c14 20 20 20 55 4e 49 4f 4e 20 41 4c 4c 0a 20 20 2a     UNION ALL.  *
25c15 2a 20 20 20 20 20 20 20 20 53 45 4c 45 43 54 20  *        SELECT 
25c16 79 20 46 52 4f 4d 20 74 61 62 0a 20 20 2a 2a 20  y FROM tab.  ** 
25c17 20 20 20 20 20 20 20 55 4e 49 4f 4e 20 41 4c 4c         UNION ALL
25c18 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 53 45 4c  .  **        SEL
25c19 45 43 54 20 61 62 73 28 7a 2a 32 29 20 46 52 4f  ECT abs(z*2) FRO
25c1a 4d 20 74 61 62 32 0a 20 20 2a 2a 20 20 20 20 20  M tab2.  **     
25c1b 29 20 57 48 45 52 45 20 61 21 3d 35 20 4f 52 44  ) WHERE a!=5 ORD
25c1c 45 52 20 42 59 20 31 0a 20 20 2a 2a 0a 20 20 2a  ER BY 1.  **.  *
25c1d 2a 20 54 72 61 6e 73 66 6f 72 6d 65 64 20 69 6e  * Transformed in
25c1e 74 6f 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  to:.  **.  **   
25c1f 20 20 53 45 4c 45 43 54 20 78 2b 31 20 46 52 4f    SELECT x+1 FRO
25c20 4d 20 74 61 62 20 57 48 45 52 45 20 78 2b 31 21  M tab WHERE x+1!
25c21 3d 35 0a 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f  =5.  **     UNIO
25c22 4e 20 41 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53  N ALL.  **     S
25c23 45 4c 45 43 54 20 79 2b 31 20 46 52 4f 4d 20 74  ELECT y+1 FROM t
25c24 61 62 20 57 48 45 52 45 20 79 2b 31 21 3d 35 0a  ab WHERE y+1!=5.
25c25 20 20 2a 2a 20 20 20 20 20 55 4e 49 4f 4e 20 41    **     UNION A
25c26 4c 4c 0a 20 20 2a 2a 20 20 20 20 20 53 45 4c 45  LL.  **     SELE
25c27 43 54 20 61 62 73 28 7a 2a 32 29 2b 31 20 46 52  CT abs(z*2)+1 FR
25c28 4f 4d 20 74 61 62 32 20 57 48 45 52 45 20 61 62  OM tab2 WHERE ab
25c29 73 28 7a 2a 32 29 2b 31 21 3d 35 0a 20 20 2a 2a  s(z*2)+1!=5.  **
25c2a 20 20 20 20 20 4f 52 44 45 52 20 42 59 20 31 0a       ORDER BY 1.
25c2b 20 20 2a 2a 0a 20 20 2a 2a 20 57 65 20 63 61 6c    **.  ** We cal
25c2c 6c 20 74 68 69 73 20 74 68 65 20 22 63 6f 6d 70  l this the "comp
25c2d 6f 75 6e 64 2d 73 75 62 71 75 65 72 79 20 66 6c  ound-subquery fl
25c2e 61 74 74 65 6e 69 6e 67 22 2e 0a 20 20 2a 2f 0a  attening"..  */.
25c2f 20 20 66 6f 72 28 70 53 75 62 3d 70 53 75 62 2d    for(pSub=pSub-
25c30 3e 70 50 72 69 6f 72 3b 20 70 53 75 62 3b 20 70  >pPrior; pSub; p
25c31 53 75 62 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72  Sub=pSub->pPrior
25c32 29 7b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  ){.    Select *p
25c33 4e 65 77 3b 0a 20 20 20 20 45 78 70 72 4c 69 73  New;.    ExprLis
25c34 74 20 2a 70 4f 72 64 65 72 42 79 20 3d 20 70 2d  t *pOrderBy = p-
25c35 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 45  >pOrderBy;.    E
25c36 78 70 72 20 2a 70 4c 69 6d 69 74 20 3d 20 70 2d  xpr *pLimit = p-
25c37 3e 70 4c 69 6d 69 74 3b 0a 20 20 20 20 53 65 6c  >pLimit;.    Sel
25c38 65 63 74 20 2a 70 50 72 69 6f 72 20 3d 20 70 2d  ect *pPrior = p-
25c39 3e 70 50 72 69 6f 72 3b 0a 20 20 20 20 70 2d 3e  >pPrior;.    p->
25c3a 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20 20  pOrderBy = 0;.  
25c3b 20 20 70 2d 3e 70 53 72 63 20 3d 20 30 3b 0a 20    p->pSrc = 0;. 
25c3c 20 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 30     p->pPrior = 0
25c3d 3b 0a 20 20 20 20 70 2d 3e 70 4c 69 6d 69 74 20  ;.    p->pLimit 
25c3e 3d 20 30 3b 0a 20 20 20 20 70 4e 65 77 20 3d 20  = 0;.    pNew = 
25c3f 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44 75 70  sqlite3SelectDup
25c40 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 70 2d 3e  (db, p);.    p->
25c41 70 4c 69 6d 69 74 20 3d 20 70 4c 69 6d 69 74 3b  pLimit = pLimit;
25c42 0a 20 20 20 20 70 2d 3e 70 4f 72 64 65 72 42 79  .    p->pOrderBy
25c43 20 3d 20 70 4f 72 64 65 72 42 79 3b 0a 20 20 20   = pOrderBy;.   
25c44 20 70 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 3b   p->pSrc = pSrc;
25c45 0a 20 20 20 20 70 2d 3e 6f 70 20 3d 20 54 4b 5f  .    p->op = TK_
25c46 41 4c 4c 3b 0a 20 20 20 20 70 2d 3e 70 52 69 67  ALL;.    p->pRig
25c47 68 74 6d 6f 73 74 20 3d 20 30 3b 0a 20 20 20 20  htmost = 0;.    
25c48 69 66 28 20 70 4e 65 77 3d 3d 30 20 29 7b 0a 20  if( pNew==0 ){. 
25c49 20 20 20 20 20 70 4e 65 77 20 3d 20 70 50 72 69       pNew = pPri
25c4a 6f 72 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  or;.    }else{. 
25c4b 20 20 20 20 20 70 4e 65 77 2d 3e 70 50 72 69 6f       pNew->pPrio
25c4c 72 20 3d 20 70 50 72 69 6f 72 3b 0a 20 20 20 20  r = pPrior;.    
25c4d 20 20 70 4e 65 77 2d 3e 70 52 69 67 68 74 6d 6f    pNew->pRightmo
25c4e 73 74 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  st = 0;.    }.  
25c4f 20 20 70 2d 3e 70 50 72 69 6f 72 20 3d 20 70 4e    p->pPrior = pN
25c50 65 77 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e  ew;.    if( db->
25c51 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72  mallocFailed ) r
25c52 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 0a 20 20  eturn 1;.  }..  
25c53 2f 2a 20 42 65 67 69 6e 20 66 6c 61 74 74 65 6e  /* Begin flatten
25c54 69 6e 67 20 74 68 65 20 69 46 72 6f 6d 2d 74 68  ing the iFrom-th
25c55 20 65 6e 74 72 79 20 6f 66 20 74 68 65 20 46 52   entry of the FR
25c56 4f 4d 20 63 6c 61 75 73 65 20 0a 20 20 2a 2a 20  OM clause .  ** 
25c57 69 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65  in the outer que
25c58 72 79 2e 0a 20 20 2a 2f 0a 20 20 70 53 75 62 20  ry..  */.  pSub 
25c59 3d 20 70 53 75 62 31 20 3d 20 70 53 75 62 69 74  = pSub1 = pSubit
25c5a 65 6d 2d 3e 70 53 65 6c 65 63 74 3b 0a 0a 20 20  em->pSelect;..  
25c5b 2f 2a 20 44 65 6c 65 74 65 20 74 68 65 20 74 72  /* Delete the tr
25c5c 61 6e 73 69 65 6e 74 20 74 61 62 6c 65 20 73 74  ansient table st
25c5d 72 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74  ructure associat
25c5e 65 64 20 77 69 74 68 20 74 68 65 0a 20 20 2a 2a  ed with the.  **
25c5f 20 73 75 62 71 75 65 72 79 0a 20 20 2a 2f 0a 20   subquery.  */. 
25c60 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
25c61 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 44 61  b, pSubitem->zDa
25c62 74 61 62 61 73 65 29 3b 0a 20 20 73 71 6c 69 74  tabase);.  sqlit
25c63 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 53 75  e3DbFree(db, pSu
25c64 62 69 74 65 6d 2d 3e 7a 4e 61 6d 65 29 3b 0a 20  bitem->zName);. 
25c65 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
25c66 62 2c 20 70 53 75 62 69 74 65 6d 2d 3e 7a 41 6c  b, pSubitem->zAl
25c67 69 61 73 29 3b 0a 20 20 70 53 75 62 69 74 65 6d  ias);.  pSubitem
25c68 2d 3e 7a 44 61 74 61 62 61 73 65 20 3d 20 30 3b  ->zDatabase = 0;
25c69 0a 20 20 70 53 75 62 69 74 65 6d 2d 3e 7a 4e 61  .  pSubitem->zNa
25c6a 6d 65 20 3d 20 30 3b 0a 20 20 70 53 75 62 69 74  me = 0;.  pSubit
25c6b 65 6d 2d 3e 7a 41 6c 69 61 73 20 3d 20 30 3b 0a  em->zAlias = 0;.
25c6c 20 20 70 53 75 62 69 74 65 6d 2d 3e 70 53 65 6c    pSubitem->pSel
25c6d 65 63 74 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 44  ect = 0;..  /* D
25c6e 65 66 65 72 20 64 65 6c 65 74 69 6e 67 20 74 68  efer deleting th
25c6f 65 20 54 61 62 6c 65 20 6f 62 6a 65 63 74 20 61  e Table object a
25c70 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
25c71 68 65 0a 20 20 2a 2a 20 73 75 62 71 75 65 72 79  he.  ** subquery
25c72 20 75 6e 74 69 6c 20 63 6f 64 65 20 67 65 6e 65   until code gene
25c73 72 61 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20 63  ration is.  ** c
25c74 6f 6d 70 6c 65 74 65 2c 20 73 69 6e 63 65 20 74  omplete, since t
25c75 68 65 72 65 20 6d 61 79 20 73 74 69 6c 6c 20 65  here may still e
25c76 78 69 73 74 20 45 78 70 72 2e 70 54 61 62 20 65  xist Expr.pTab e
25c77 6e 74 72 69 65 73 20 74 68 61 74 0a 20 20 2a 2a  ntries that.  **
25c78 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 73 75   refer to the su
25c79 62 71 75 65 72 79 20 65 76 65 6e 20 61 66 74 65  bquery even afte
25c7a 72 20 66 6c 61 74 74 65 6e 69 6e 67 2e 20 20 54  r flattening.  T
25c7b 69 63 6b 65 74 20 23 33 33 34 36 2e 0a 20 20 2a  icket #3346..  *
25c7c 2f 0a 20 20 69 66 28 20 70 53 75 62 69 74 65 6d  /.  if( pSubitem
25c7d 2d 3e 70 54 61 62 21 3d 30 20 29 7b 0a 20 20 20  ->pTab!=0 ){.   
25c7e 20 54 61 62 6c 65 20 2a 70 54 61 62 54 6f 44 65   Table *pTabToDe
25c7f 6c 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 70 54  l = pSubitem->pT
25c80 61 62 3b 0a 20 20 20 20 69 66 28 20 70 54 61 62  ab;.    if( pTab
25c81 54 6f 44 65 6c 2d 3e 6e 52 65 66 3d 3d 31 20 29  ToDel->nRef==1 )
25c82 7b 0a 20 20 20 20 20 20 70 54 61 62 54 6f 44 65  {.      pTabToDe
25c83 6c 2d 3e 70 4e 65 78 74 5a 6f 6d 62 69 65 20 3d  l->pNextZombie =
25c84 20 70 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65   pParse->pZombie
25c85 54 61 62 3b 0a 20 20 20 20 20 20 70 50 61 72 73  Tab;.      pPars
25c86 65 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 3d 20  e->pZombieTab = 
25c87 70 54 61 62 54 6f 44 65 6c 3b 0a 20 20 20 20 7d  pTabToDel;.    }
25c88 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 54 61 62  else{.      pTab
25c89 54 6f 44 65 6c 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  ToDel->nRef--;. 
25c8a 20 20 20 7d 0a 20 20 20 20 70 53 75 62 69 74 65     }.    pSubite
25c8b 6d 2d 3e 70 54 61 62 20 3d 20 30 3b 0a 20 20 7d  m->pTab = 0;.  }
25c8c 0a 0a 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f  ..  /* The follo
25c8d 77 69 6e 67 20 6c 6f 6f 70 20 72 75 6e 73 20 6f  wing loop runs o
25c8e 6e 63 65 20 66 6f 72 20 65 61 63 68 20 74 65 72  nce for each ter
25c8f 6d 20 69 6e 20 61 20 63 6f 6d 70 6f 75 6e 64 2d  m in a compound-
25c90 73 75 62 71 75 65 72 79 0a 20 20 2a 2a 20 66 6c  subquery.  ** fl
25c91 61 74 74 65 6e 69 6e 67 20 28 61 73 20 64 65 73  attening (as des
25c92 63 72 69 62 65 64 20 61 62 6f 76 65 29 2e 20 20  cribed above).  
25c93 49 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20  If we are doing 
25c94 61 20 64 69 66 66 65 72 65 6e 74 20 6b 69 6e 64  a different kind
25c95 0a 20 20 2a 2a 20 6f 66 20 66 6c 61 74 74 65 6e  .  ** of flatten
25c96 69 6e 67 20 2d 20 61 20 66 6c 61 74 74 65 6e 69  ing - a flatteni
25c97 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 61 20  ng other than a 
25c98 63 6f 6d 70 6f 75 6e 64 2d 73 75 62 71 75 65 72  compound-subquer
25c99 79 20 66 6c 61 74 74 65 6e 69 6e 67 20 2d 0a 20  y flattening -. 
25c9a 20 2a 2a 20 74 68 65 6e 20 74 68 69 73 20 6c 6f   ** then this lo
25c9b 6f 70 20 6f 6e 6c 79 20 72 75 6e 73 20 6f 6e 63  op only runs onc
25c9c 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  e..  **.  ** Thi
25c9d 73 20 6c 6f 6f 70 20 6d 6f 76 65 73 20 61 6c 6c  s loop moves all
25c9e 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 65 6c 65   of the FROM ele
25c9f 6d 65 6e 74 73 20 6f 66 20 74 68 65 20 73 75 62  ments of the sub
25ca0 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65 0a 20  query into the. 
25ca1 20 2a 2a 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   ** the FROM cla
25ca2 75 73 65 20 6f 66 20 74 68 65 20 6f 75 74 65 72  use of the outer
25ca3 20 71 75 65 72 79 2e 20 20 42 65 66 6f 72 65 20   query.  Before 
25ca4 64 6f 69 6e 67 20 74 68 69 73 2c 20 72 65 6d 65  doing this, reme
25ca5 6d 62 65 72 0a 20 20 2a 2a 20 74 68 65 20 63 75  mber.  ** the cu
25ca6 72 73 6f 72 20 6e 75 6d 62 65 72 20 66 6f 72 20  rsor number for 
25ca7 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 6f 75 74  the original out
25ca8 65 72 20 71 75 65 72 79 20 46 52 4f 4d 20 65 6c  er query FROM el
25ca9 65 6d 65 6e 74 20 69 6e 0a 20 20 2a 2a 20 69 50  ement in.  ** iP
25caa 61 72 65 6e 74 2e 20 20 54 68 65 20 69 50 61 72  arent.  The iPar
25cab 65 6e 74 20 63 75 72 73 6f 72 20 77 69 6c 6c 20  ent cursor will 
25cac 6e 65 76 65 72 20 62 65 20 75 73 65 64 2e 20 20  never be used.  
25cad 53 75 62 73 65 71 75 65 6e 74 20 63 6f 64 65 0a  Subsequent code.
25cae 20 20 2a 2a 20 77 69 6c 6c 20 73 63 61 6e 20 65    ** will scan e
25caf 78 70 72 65 73 73 69 6f 6e 73 20 6c 6f 6f 6b 69  xpressions looki
25cb0 6e 67 20 66 6f 72 20 69 50 61 72 65 6e 74 20 72  ng for iParent r
25cb1 65 66 65 72 65 6e 63 65 73 20 61 6e 64 20 72 65  eferences and re
25cb2 70 6c 61 63 65 0a 20 20 2a 2a 20 74 68 6f 73 65  place.  ** those
25cb3 20 72 65 66 65 72 65 6e 63 65 73 20 77 69 74 68   references with
25cb4 20 65 78 70 72 65 73 73 69 6f 6e 73 20 74 68 61   expressions tha
25cb5 74 20 72 65 73 6f 6c 76 65 20 74 6f 20 74 68 65  t resolve to the
25cb6 20 73 75 62 71 75 65 72 79 20 46 52 4f 4d 0a 20   subquery FROM. 
25cb7 20 2a 2a 20 65 6c 65 6d 65 6e 74 73 20 77 65 20   ** elements we 
25cb8 61 72 65 20 6e 6f 77 20 63 6f 70 79 69 6e 67 20  are now copying 
25cb9 69 6e 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 70  in..  */.  for(p
25cba 50 61 72 65 6e 74 3d 70 3b 20 70 50 61 72 65 6e  Parent=p; pParen
25cbb 74 3b 20 70 50 61 72 65 6e 74 3d 70 50 61 72 65  t; pParent=pPare
25cbc 6e 74 2d 3e 70 50 72 69 6f 72 2c 20 70 53 75 62  nt->pPrior, pSub
25cbd 3d 70 53 75 62 2d 3e 70 50 72 69 6f 72 29 7b 0a  =pSub->pPrior){.
25cbe 20 20 20 20 69 6e 74 20 6e 53 75 62 53 72 63 3b      int nSubSrc;
25cbf 0a 20 20 20 20 69 6e 74 20 6a 6f 69 6e 74 79 70  .    int jointyp
25cc0 65 20 3d 20 30 3b 0a 20 20 20 20 70 53 75 62 53  e = 0;.    pSubS
25cc1 72 63 20 3d 20 70 53 75 62 2d 3e 70 53 72 63 3b  rc = pSub->pSrc;
25cc2 20 20 20 20 20 2f 2a 20 46 52 4f 4d 20 63 6c 61       /* FROM cla
25cc3 75 73 65 20 6f 66 20 73 75 62 71 75 65 72 79 20  use of subquery 
25cc4 2a 2f 0a 20 20 20 20 6e 53 75 62 53 72 63 20 3d  */.    nSubSrc =
25cc5 20 70 53 75 62 53 72 63 2d 3e 6e 53 72 63 3b 20   pSubSrc->nSrc; 
25cc6 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 65   /* Number of te
25cc7 72 6d 73 20 69 6e 20 73 75 62 71 75 65 72 79 20  rms in subquery 
25cc8 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
25cc9 20 20 20 70 53 72 63 20 3d 20 70 50 61 72 65 6e     pSrc = pParen
25cca 74 2d 3e 70 53 72 63 3b 20 20 20 20 20 2f 2a 20  t->pSrc;     /* 
25ccb 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f 66 20 74  FROM clause of t
25ccc 68 65 20 6f 75 74 65 72 20 71 75 65 72 79 20 2a  he outer query *
25ccd 2f 0a 0a 20 20 20 20 69 66 28 20 70 53 72 63 20  /..    if( pSrc 
25cce 29 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ){.      assert(
25ccf 20 70 50 61 72 65 6e 74 3d 3d 70 20 29 3b 20 20   pParent==p );  
25cd0 2f 2a 20 46 69 72 73 74 20 74 69 6d 65 20 74 68  /* First time th
25cd1 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a  rough the loop *
25cd2 2f 0a 20 20 20 20 20 20 6a 6f 69 6e 74 79 70 65  /.      jointype
25cd3 20 3d 20 70 53 75 62 69 74 65 6d 2d 3e 6a 6f 69   = pSubitem->joi
25cd4 6e 74 79 70 65 3b 0a 20 20 20 20 7d 65 6c 73 65  ntype;.    }else
25cd5 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
25cd6 70 50 61 72 65 6e 74 21 3d 70 20 29 3b 20 20 2f  pParent!=p );  /
25cd7 2a 20 32 6e 64 20 61 6e 64 20 73 75 62 73 65 71  * 2nd and subseq
25cd8 75 65 6e 74 20 74 69 6d 65 73 20 74 68 72 6f 75  uent times throu
25cd9 67 68 20 74 68 65 20 6c 6f 6f 70 20 2a 2f 0a 20  gh the loop */. 
25cda 20 20 20 20 20 70 53 72 63 20 3d 20 70 50 61 72       pSrc = pPar
25cdb 65 6e 74 2d 3e 70 53 72 63 20 3d 20 73 71 6c 69  ent->pSrc = sqli
25cdc 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
25cdd 28 64 62 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20  (db, 0, 0, 0);. 
25cde 20 20 20 20 20 69 66 28 20 70 53 72 63 3d 3d 30       if( pSrc==0
25cdf 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73 65   ){.        asse
25ce0 72 74 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  rt( db->mallocFa
25ce1 69 6c 65 64 20 29 3b 0a 20 20 20 20 20 20 20 20  iled );.        
25ce2 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
25ce3 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 54 68 65     }..    /* The
25ce4 20 73 75 62 71 75 65 72 79 20 75 73 65 73 20 61   subquery uses a
25ce5 20 73 69 6e 67 6c 65 20 73 6c 6f 74 20 6f 66 20   single slot of 
25ce6 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
25ce7 6f 66 20 74 68 65 20 6f 75 74 65 72 0a 20 20 20  of the outer.   
25ce8 20 2a 2a 20 71 75 65 72 79 2e 20 20 49 66 20 74   ** query.  If t
25ce9 68 65 20 73 75 62 71 75 65 72 79 20 68 61 73 20  he subquery has 
25cea 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 65 6c  more than one el
25ceb 65 6d 65 6e 74 20 69 6e 20 69 74 73 20 46 52 4f  ement in its FRO
25cec 4d 20 63 6c 61 75 73 65 2c 0a 20 20 20 20 2a 2a  M clause,.    **
25ced 20 74 68 65 6e 20 65 78 70 61 6e 64 20 74 68 65   then expand the
25cee 20 6f 75 74 65 72 20 71 75 65 72 79 20 74 6f 20   outer query to 
25cef 6d 61 6b 65 20 73 70 61 63 65 20 66 6f 72 20 69  make space for i
25cf0 74 20 74 6f 20 68 6f 6c 64 20 61 6c 6c 20 65 6c  t to hold all el
25cf1 65 6d 65 6e 74 73 0a 20 20 20 20 2a 2a 20 6f 66  ements.    ** of
25cf2 20 74 68 65 20 73 75 62 71 75 65 72 79 2e 0a 20   the subquery.. 
25cf3 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 45 78 61     **.    ** Exa
25cf4 6d 70 6c 65 3a 0a 20 20 20 20 2a 2a 0a 20 20 20  mple:.    **.   
25cf5 20 2a 2a 20 20 20 20 53 45 4c 45 43 54 20 2a 20   **    SELECT * 
25cf6 46 52 4f 4d 20 74 61 62 41 2c 20 28 53 45 4c 45  FROM tabA, (SELE
25cf7 43 54 20 2a 20 46 52 4f 4d 20 73 75 62 31 2c 20  CT * FROM sub1, 
25cf8 73 75 62 32 29 2c 20 74 61 62 42 3b 0a 20 20 20  sub2), tabB;.   
25cf9 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 65 20 6f   **.    ** The o
25cfa 75 74 65 72 20 71 75 65 72 79 20 68 61 73 20 33  uter query has 3
25cfb 20 73 6c 6f 74 73 20 69 6e 20 69 74 73 20 46 52   slots in its FR
25cfc 4f 4d 20 63 6c 61 75 73 65 2e 20 20 4f 6e 65 20  OM clause.  One 
25cfd 73 6c 6f 74 20 6f 66 20 74 68 65 0a 20 20 20 20  slot of the.    
25cfe 2a 2a 20 6f 75 74 65 72 20 71 75 65 72 79 20 28  ** outer query (
25cff 74 68 65 20 6d 69 64 64 6c 65 20 73 6c 6f 74 29  the middle slot)
25d00 20 69 73 20 75 73 65 64 20 62 79 20 74 68 65 20   is used by the 
25d01 73 75 62 71 75 65 72 79 2e 20 20 54 68 65 20 6e  subquery.  The n
25d02 65 78 74 0a 20 20 20 20 2a 2a 20 62 6c 6f 63 6b  ext.    ** block
25d03 20 6f 66 20 63 6f 64 65 20 77 69 6c 6c 20 65 78   of code will ex
25d04 70 61 6e 64 20 74 68 65 20 6f 75 74 20 71 75 65  pand the out que
25d05 72 79 20 74 6f 20 34 20 73 6c 6f 74 73 2e 20 20  ry to 4 slots.  
25d06 54 68 65 20 6d 69 64 64 6c 65 0a 20 20 20 20 2a  The middle.    *
25d07 2a 20 73 6c 6f 74 20 69 73 20 65 78 70 61 6e 64  * slot is expand
25d08 65 64 20 74 6f 20 74 77 6f 20 73 6c 6f 74 73 20  ed to two slots 
25d09 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 61 6b 65  in order to make
25d0a 20 73 70 61 63 65 20 66 6f 72 20 74 68 65 0a 20   space for the. 
25d0b 20 20 20 2a 2a 20 74 77 6f 20 65 6c 65 6d 65 6e     ** two elemen
25d0c 74 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  ts in the FROM c
25d0d 6c 61 75 73 65 20 6f 66 20 74 68 65 20 73 75 62  lause of the sub
25d0e 71 75 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20  query..    */.  
25d0f 20 20 69 66 28 20 6e 53 75 62 53 72 63 3e 31 20    if( nSubSrc>1 
25d10 29 7b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74  ){.      pParent
25d11 2d 3e 70 53 72 63 20 3d 20 70 53 72 63 20 3d 20  ->pSrc = pSrc = 
25d12 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 45 6e  sqlite3SrcListEn
25d13 6c 61 72 67 65 28 64 62 2c 20 70 53 72 63 2c 20  large(db, pSrc, 
25d14 6e 53 75 62 53 72 63 2d 31 2c 69 46 72 6f 6d 2b  nSubSrc-1,iFrom+
25d15 31 29 3b 0a 20 20 20 20 20 20 69 66 28 20 64 62  1);.      if( db
25d16 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
25d17 7b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  {.        break;
25d18 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
25d19 20 20 20 20 2f 2a 20 54 72 61 6e 73 66 65 72 20      /* Transfer 
25d1a 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
25d1b 74 65 72 6d 73 20 66 72 6f 6d 20 74 68 65 20 73  terms from the s
25d1c 75 62 71 75 65 72 79 20 69 6e 74 6f 20 74 68 65  ubquery into the
25d1d 0a 20 20 20 20 2a 2a 20 6f 75 74 65 72 20 71 75  .    ** outer qu
25d1e 65 72 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ery..    */.    
25d1f 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 53 75 62 53  for(i=0; i<nSubS
25d20 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  rc; i++){.      
25d21 70 53 72 63 2d 3e 61 5b 69 2b 69 46 72 6f 6d 5d  pSrc->a[i+iFrom]
25d22 20 3d 20 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d   = pSubSrc->a[i]
25d23 3b 0a 20 20 20 20 20 20 6d 65 6d 73 65 74 28 26  ;.      memset(&
25d24 70 53 75 62 53 72 63 2d 3e 61 5b 69 5d 2c 20 30  pSubSrc->a[i], 0
25d25 2c 20 73 69 7a 65 6f 66 28 70 53 75 62 53 72 63  , sizeof(pSubSrc
25d26 2d 3e 61 5b 69 5d 29 29 3b 0a 20 20 20 20 7d 0a  ->a[i]));.    }.
25d27 20 20 20 20 70 53 72 63 2d 3e 61 5b 69 46 72 6f      pSrc->a[iFro
25d28 6d 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 6a 6f  m].jointype = jo
25d29 69 6e 74 79 70 65 3b 0a 20 20 0a 20 20 20 20 2f  intype;.  .    /
25d2a 2a 20 4e 6f 77 20 62 65 67 69 6e 20 73 75 62 73  * Now begin subs
25d2b 74 69 74 75 74 69 6e 67 20 73 75 62 71 75 65 72  tituting subquer
25d2c 79 20 72 65 73 75 6c 74 20 73 65 74 20 65 78 70  y result set exp
25d2d 72 65 73 73 69 6f 6e 73 20 66 6f 72 20 0a 20 20  ressions for .  
25d2e 20 20 2a 2a 20 72 65 66 65 72 65 6e 63 65 73 20    ** references 
25d2f 74 6f 20 74 68 65 20 69 50 61 72 65 6e 74 20 69  to the iParent i
25d30 6e 20 74 68 65 20 6f 75 74 65 72 20 71 75 65 72  n the outer quer
25d31 79 2e 0a 20 20 20 20 2a 2a 20 0a 20 20 20 20 2a  y..    ** .    *
25d32 2a 20 45 78 61 6d 70 6c 65 3a 0a 20 20 20 20 2a  * Example:.    *
25d33 2a 0a 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43  *.    **   SELEC
25d34 54 20 61 2b 35 2c 20 62 2a 31 30 20 46 52 4f 4d  T a+5, b*10 FROM
25d35 20 28 53 45 4c 45 43 54 20 78 2a 33 20 41 53 20   (SELECT x*3 AS 
25d36 61 2c 20 79 2b 31 30 20 41 53 20 62 20 46 52 4f  a, y+10 AS b FRO
25d37 4d 20 74 31 29 20 57 48 45 52 45 20 61 3e 62 3b  M t1) WHERE a>b;
25d38 0a 20 20 20 20 2a 2a 20 20 20 5c 20 20 20 20 20  .    **   \     
25d39 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d3a 5c 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20 73  \_____________ s
25d3b 75 62 71 75 65 72 79 20 5f 5f 5f 5f 5f 5f 5f 5f  ubquery ________
25d3c 5f 5f 2f 20 20 20 20 20 20 20 20 20 20 2f 0a 20  __/          /. 
25d3d 20 20 20 2a 2a 20 20 20 20 5c 5f 5f 5f 5f 5f 5f     **    \______
25d3e 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 20  _______________ 
25d3f 6f 75 74 65 72 20 71 75 65 72 79 20 5f 5f 5f 5f  outer query ____
25d40 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f  ________________
25d41 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 20 20 20 20  __________/.    
25d42 2a 2a 0a 20 20 20 20 2a 2a 20 57 65 20 6c 6f 6f  **.    ** We loo
25d43 6b 20 61 74 20 65 76 65 72 79 20 65 78 70 72 65  k at every expre
25d44 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 6f 75 74  ssion in the out
25d45 65 72 20 71 75 65 72 79 20 61 6e 64 20 65 76 65  er query and eve
25d46 72 79 20 70 6c 61 63 65 20 77 65 20 73 65 65 0a  ry place we see.
25d47 20 20 20 20 2a 2a 20 22 61 22 20 77 65 20 73 75      ** "a" we su
25d48 62 73 74 69 74 75 74 65 20 22 78 2a 33 22 20 61  bstitute "x*3" a
25d49 6e 64 20 65 76 65 72 79 20 70 6c 61 63 65 20 77  nd every place w
25d4a 65 20 73 65 65 20 22 62 22 20 77 65 20 73 75 62  e see "b" we sub
25d4b 73 74 69 74 75 74 65 20 22 79 2b 31 30 22 2e 0a  stitute "y+10"..
25d4c 20 20 20 20 2a 2f 0a 20 20 20 20 70 4c 69 73 74      */.    pList
25d4d 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69   = pParent->pELi
25d4e 73 74 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b  st;.    for(i=0;
25d4f 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
25d50 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70   i++){.      Exp
25d51 72 20 2a 70 45 78 70 72 3b 0a 20 20 20 20 20 20  r *pExpr;.      
25d52 69 66 28 20 70 4c 69 73 74 2d 3e 61 5b 69 5d 2e  if( pList->a[i].
25d53 7a 4e 61 6d 65 3d 3d 30 20 26 26 20 28 70 45 78  zName==0 && (pEx
25d54 70 72 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 69 5d  pr = pList->a[i]
25d55 2e 70 45 78 70 72 29 2d 3e 73 70 61 6e 2e 7a 21  .pExpr)->span.z!
25d56 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 70 4c  =0 ){.        pL
25d57 69 73 74 2d 3e 61 5b 69 5d 2e 7a 4e 61 6d 65 20  ist->a[i].zName 
25d58 3d 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  = .             
25d59 20 20 73 71 6c 69 74 65 33 44 62 53 74 72 4e 44    sqlite3DbStrND
25d5a 75 70 28 64 62 2c 20 28 63 68 61 72 2a 29 70 45  up(db, (char*)pE
25d5b 78 70 72 2d 3e 73 70 61 6e 2e 7a 2c 20 70 45 78  xpr->span.z, pEx
25d5c 70 72 2d 3e 73 70 61 6e 2e 6e 29 3b 0a 20 20 20  pr->span.n);.   
25d5d 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73     }.    }.    s
25d5e 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
25d5f 20 70 50 61 72 65 6e 74 2d 3e 70 45 4c 69 73 74   pParent->pEList
25d60 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d  , iParent, pSub-
25d61 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 69 66  >pEList);.    if
25d62 28 20 69 73 41 67 67 20 29 7b 0a 20 20 20 20 20  ( isAgg ){.     
25d63 20 73 75 62 73 74 45 78 70 72 4c 69 73 74 28 64   substExprList(d
25d64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 47 72 6f  b, pParent->pGro
25d65 75 70 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70  upBy, iParent, p
25d66 53 75 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20  Sub->pEList);.  
25d67 20 20 20 20 73 75 62 73 74 45 78 70 72 28 64 62      substExpr(db
25d68 2c 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69  , pParent->pHavi
25d69 6e 67 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  ng, iParent, pSu
25d6a 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b->pEList);.    
25d6b 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e  }.    if( pSub->
25d6c 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20  pOrderBy ){.    
25d6d 20 20 61 73 73 65 72 74 28 20 70 50 61 72 65 6e    assert( pParen
25d6e 74 2d 3e 70 4f 72 64 65 72 42 79 3d 3d 30 20 29  t->pOrderBy==0 )
25d6f 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e 74 2d  ;.      pParent-
25d70 3e 70 4f 72 64 65 72 42 79 20 3d 20 70 53 75 62  >pOrderBy = pSub
25d71 2d 3e 70 4f 72 64 65 72 42 79 3b 0a 20 20 20 20  ->pOrderBy;.    
25d72 20 20 70 53 75 62 2d 3e 70 4f 72 64 65 72 42 79    pSub->pOrderBy
25d73 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 0;.    }else 
25d74 69 66 28 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72  if( pParent->pOr
25d75 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 73  derBy ){.      s
25d76 75 62 73 74 45 78 70 72 4c 69 73 74 28 64 62 2c  ubstExprList(db,
25d77 20 70 50 61 72 65 6e 74 2d 3e 70 4f 72 64 65 72   pParent->pOrder
25d78 42 79 2c 20 69 50 61 72 65 6e 74 2c 20 70 53 75  By, iParent, pSu
25d79 62 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  b->pEList);.    
25d7a 7d 0a 20 20 20 20 69 66 28 20 70 53 75 62 2d 3e  }.    if( pSub->
25d7b 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 20 20  pWhere ){.      
25d7c 70 57 68 65 72 65 20 3d 20 73 71 6c 69 74 65 33  pWhere = sqlite3
25d7d 45 78 70 72 44 75 70 28 64 62 2c 20 70 53 75 62  ExprDup(db, pSub
25d7e 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 7d  ->pWhere);.    }
25d7f 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 57 68 65  else{.      pWhe
25d80 72 65 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20  re = 0;.    }.  
25d81 20 20 69 66 28 20 73 75 62 71 75 65 72 79 49 73    if( subqueryIs
25d82 41 67 67 20 29 7b 0a 20 20 20 20 20 20 61 73 73  Agg ){.      ass
25d83 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e 70 48  ert( pParent->pH
25d84 61 76 69 6e 67 3d 3d 30 20 29 3b 0a 20 20 20 20  aving==0 );.    
25d85 20 20 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69    pParent->pHavi
25d86 6e 67 20 3d 20 70 50 61 72 65 6e 74 2d 3e 70 57  ng = pParent->pW
25d87 68 65 72 65 3b 0a 20 20 20 20 20 20 70 50 61 72  here;.      pPar
25d88 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d 20 70 57  ent->pWhere = pW
25d89 68 65 72 65 3b 0a 20 20 20 20 20 20 73 75 62 73  here;.      subs
25d8a 74 45 78 70 72 28 64 62 2c 20 70 50 61 72 65 6e  tExpr(db, pParen
25d8b 74 2d 3e 70 48 61 76 69 6e 67 2c 20 69 50 61 72  t->pHaving, iPar
25d8c 65 6e 74 2c 20 70 53 75 62 2d 3e 70 45 4c 69 73  ent, pSub->pELis
25d8d 74 29 3b 0a 20 20 20 20 20 20 70 50 61 72 65 6e  t);.      pParen
25d8e 74 2d 3e 70 48 61 76 69 6e 67 20 3d 20 73 71 6c  t->pHaving = sql
25d8f 69 74 65 33 45 78 70 72 41 6e 64 28 64 62 2c 20  ite3ExprAnd(db, 
25d90 70 50 61 72 65 6e 74 2d 3e 70 48 61 76 69 6e 67  pParent->pHaving
25d91 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
25d92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25d93 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
25d94 44 75 70 28 64 62 2c 20 70 53 75 62 2d 3e 70 48  Dup(db, pSub->pH
25d95 61 76 69 6e 67 29 29 3b 0a 20 20 20 20 20 20 61  aving));.      a
25d96 73 73 65 72 74 28 20 70 50 61 72 65 6e 74 2d 3e  ssert( pParent->
25d97 70 47 72 6f 75 70 42 79 3d 3d 30 20 29 3b 0a 20  pGroupBy==0 );. 
25d98 20 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 47       pParent->pG
25d99 72 6f 75 70 42 79 20 3d 20 73 71 6c 69 74 65 33  roupBy = sqlite3
25d9a 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20  ExprListDup(db, 
25d9b 70 53 75 62 2d 3e 70 47 72 6f 75 70 42 79 29 3b  pSub->pGroupBy);
25d9c 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
25d9d 20 20 73 75 62 73 74 45 78 70 72 28 64 62 2c 20    substExpr(db, 
25d9e 70 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 2c  pParent->pWhere,
25d9f 20 69 50 61 72 65 6e 74 2c 20 70 53 75 62 2d 3e   iParent, pSub->
25da0 70 45 4c 69 73 74 29 3b 0a 20 20 20 20 20 20 70  pEList);.      p
25da1 50 61 72 65 6e 74 2d 3e 70 57 68 65 72 65 20 3d  Parent->pWhere =
25da2 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e 64 28   sqlite3ExprAnd(
25da3 64 62 2c 20 70 50 61 72 65 6e 74 2d 3e 70 57 68  db, pParent->pWh
25da4 65 72 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  ere, pWhere);.  
25da5 20 20 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 54 68    }.  .    /* Th
25da6 65 20 66 6c 61 74 74 65 6e 65 64 20 71 75 65 72  e flattened quer
25da7 79 20 69 73 20 64 69 73 74 69 6e 63 74 20 69 66  y is distinct if
25da8 20 65 69 74 68 65 72 20 74 68 65 20 69 6e 6e 65   either the inne
25da9 72 20 6f 72 20 74 68 65 0a 20 20 20 20 2a 2a 20  r or the.    ** 
25daa 6f 75 74 65 72 20 71 75 65 72 79 20 69 73 20 64  outer query is d
25dab 69 73 74 69 6e 63 74 2e 20 0a 20 20 20 20 2a 2f  istinct. .    */
25dac 0a 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 73 65  .    pParent->se
25dad 6c 46 6c 61 67 73 20 7c 3d 20 70 53 75 62 2d 3e  lFlags |= pSub->
25dae 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 44 69  selFlags & SF_Di
25daf 73 74 69 6e 63 74 3b 0a 20 20 0a 20 20 20 20 2f  stinct;.  .    /
25db0 2a 0a 20 20 20 20 2a 2a 20 53 45 4c 45 43 54 20  *.    ** SELECT 
25db1 2e 2e 2e 20 46 52 4f 4d 20 28 53 45 4c 45 43 54  ... FROM (SELECT
25db2 20 2e 2e 2e 20 4c 49 4d 49 54 20 61 20 4f 46 46   ... LIMIT a OFF
25db3 53 45 54 20 62 29 20 4c 49 4d 49 54 20 78 20 4f  SET b) LIMIT x O
25db4 46 46 53 45 54 20 79 3b 0a 20 20 20 20 2a 2a 0a  FFSET y;.    **.
25db5 20 20 20 20 2a 2a 20 4f 6e 65 20 69 73 20 74 65      ** One is te
25db6 6d 70 74 65 64 20 74 6f 20 74 72 79 20 74 6f 20  mpted to try to 
25db7 61 64 64 20 61 20 61 6e 64 20 62 20 74 6f 20 63  add a and b to c
25db8 6f 6d 62 69 6e 65 20 74 68 65 20 6c 69 6d 69 74  ombine the limit
25db9 73 2e 20 20 42 75 74 20 74 68 69 73 0a 20 20 20  s.  But this.   
25dba 20 2a 2a 20 64 6f 65 73 20 6e 6f 74 20 77 6f 72   ** does not wor
25dbb 6b 20 69 66 20 65 69 74 68 65 72 20 6c 69 6d 69  k if either limi
25dbc 74 20 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 20  t is negative.. 
25dbd 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 53     */.    if( pS
25dbe 75 62 2d 3e 70 4c 69 6d 69 74 20 29 7b 0a 20 20  ub->pLimit ){.  
25dbf 20 20 20 20 70 50 61 72 65 6e 74 2d 3e 70 4c 69      pParent->pLi
25dc0 6d 69 74 20 3d 20 70 53 75 62 2d 3e 70 4c 69 6d  mit = pSub->pLim
25dc1 69 74 3b 0a 20 20 20 20 20 20 70 53 75 62 2d 3e  it;.      pSub->
25dc2 70 4c 69 6d 69 74 20 3d 20 30 3b 0a 20 20 20 20  pLimit = 0;.    
25dc3 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69 6e 69  }.  }..  /* Fini
25dc4 61 6c 6c 79 2c 20 64 65 6c 65 74 65 20 77 68 61  ally, delete wha
25dc5 74 20 69 73 20 6c 65 66 74 20 6f 66 20 74 68 65  t is left of the
25dc6 20 73 75 62 71 75 65 72 79 20 61 6e 64 20 72 65   subquery and re
25dc7 74 75 72 6e 0a 20 20 2a 2a 20 73 75 63 63 65 73  turn.  ** succes
25dc8 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
25dc9 33 53 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62  3SelectDelete(db
25dca 2c 20 70 53 75 62 31 29 3b 0a 0a 20 20 72 65 74  , pSub1);..  ret
25dcb 75 72 6e 20 31 3b 0a 7d 0a 23 65 6e 64 69 66 20  urn 1;.}.#endif 
25dcc 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  /* !defined(SQLI
25dcd 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59  TE_OMIT_SUBQUERY
25dce 29 20 7c 7c 20 21 64 65 66 69 6e 65 64 28 53 51  ) || !defined(SQ
25dcf 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29 20  LITE_OMIT_VIEW) 
25dd0 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 41 6e 61 6c 79 7a  */../*.** Analyz
25dd1 65 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  e the SELECT sta
25dd2 74 65 6d 65 6e 74 20 70 61 73 73 65 64 20 61 73  tement passed as
25dd3 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f 20   an argument to 
25dd4 73 65 65 20 69 66 20 69 74 0a 2a 2a 20 69 73 20  see if it.** is 
25dd5 61 20 6d 69 6e 28 29 20 6f 72 20 6d 61 78 28 29  a min() or max()
25dd6 20 71 75 65 72 79 2e 20 52 65 74 75 72 6e 20 57   query. Return W
25dd7 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e  HERE_ORDERBY_MIN
25dd8 20 6f 72 20 57 48 45 52 45 5f 4f 52 44 45 52 42   or WHERE_ORDERB
25dd9 59 5f 4d 41 58 20 69 66 20 0a 2a 2a 20 69 74 20  Y_MAX if .** it 
25dda 69 73 2c 20 6f 72 20 30 20 6f 74 68 65 72 77 69  is, or 0 otherwi
25ddb 73 65 2e 20 41 74 20 70 72 65 73 65 6e 74 2c 20  se. At present, 
25ddc 61 20 71 75 65 72 79 20 69 73 20 63 6f 6e 73 69  a query is consi
25ddd 64 65 72 65 64 20 74 6f 20 62 65 0a 2a 2a 20 61  dered to be.** a
25dde 20 6d 69 6e 28 29 2f 6d 61 78 28 29 20 71 75 65   min()/max() que
25ddf 72 79 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 31  ry if:.**.**   1
25de0 2e 20 54 68 65 72 65 20 69 73 20 61 20 73 69 6e  . There is a sin
25de1 67 6c 65 20 6f 62 6a 65 63 74 20 69 6e 20 74 68  gle object in th
25de2 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 2a  e FROM clause..*
25de3 2a 0a 2a 2a 20 20 20 32 2e 20 54 68 65 72 65 20  *.**   2. There 
25de4 69 73 20 61 20 73 69 6e 67 6c 65 20 65 78 70 72  is a single expr
25de5 65 73 73 69 6f 6e 20 69 6e 20 74 68 65 20 72 65  ession in the re
25de6 73 75 6c 74 20 73 65 74 2c 20 61 6e 64 20 69 74  sult set, and it
25de7 20 69 73 0a 2a 2a 20 20 20 20 20 20 65 69 74 68   is.**      eith
25de8 65 72 20 6d 69 6e 28 78 29 20 6f 72 20 6d 61 78  er min(x) or max
25de9 28 78 29 2c 20 77 68 65 72 65 20 78 20 69 73 20  (x), where x is 
25dea 61 20 63 6f 6c 75 6d 6e 20 72 65 66 65 72 65 6e  a column referen
25deb 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
25dec 74 20 6d 69 6e 4d 61 78 51 75 65 72 79 28 53 65  t minMaxQuery(Se
25ded 6c 65 63 74 20 2a 70 29 7b 0a 20 20 45 78 70 72  lect *p){.  Expr
25dee 20 2a 70 45 78 70 72 3b 0a 20 20 45 78 70 72 4c   *pExpr;.  ExprL
25def 69 73 74 20 2a 70 45 4c 69 73 74 20 3d 20 70 2d  ist *pEList = p-
25df0 3e 70 45 4c 69 73 74 3b 0a 0a 20 20 69 66 28 20  >pEList;..  if( 
25df1 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d 31  pEList->nExpr!=1
25df2 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
25df3 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
25df4 20 20 70 45 78 70 72 20 3d 20 70 45 4c 69 73 74    pExpr = pEList
25df5 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20  ->a[0].pExpr;.  
25df6 70 45 4c 69 73 74 20 3d 20 70 45 78 70 72 2d 3e  pEList = pExpr->
25df7 70 4c 69 73 74 3b 0a 20 20 69 66 28 20 70 45 78  pList;.  if( pEx
25df8 70 72 2d 3e 6f 70 21 3d 54 4b 5f 41 47 47 5f 46  pr->op!=TK_AGG_F
25df9 55 4e 43 54 49 4f 4e 20 7c 7c 20 70 45 4c 69 73  UNCTION || pELis
25dfa 74 3d 3d 30 20 7c 7c 20 70 45 4c 69 73 74 2d 3e  t==0 || pEList->
25dfb 6e 45 78 70 72 21 3d 31 20 29 20 72 65 74 75 72  nExpr!=1 ) retur
25dfc 6e 20 30 3b 0a 20 20 69 66 28 20 70 45 4c 69 73  n 0;.  if( pELis
25dfd 74 2d 3e 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 6f  t->a[0].pExpr->o
25dfe 70 21 3d 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e  p!=TK_AGG_COLUMN
25dff 20 29 20 72 65 74 75 72 6e 20 57 48 45 52 45 5f   ) return WHERE_
25e00 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c 3b 0a  ORDERBY_NORMAL;.
25e01 20 20 69 66 28 20 70 45 78 70 72 2d 3e 74 6f 6b    if( pExpr->tok
25e02 65 6e 2e 6e 21 3d 33 20 29 20 72 65 74 75 72 6e  en.n!=3 ) return
25e03 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 5f 4e   WHERE_ORDERBY_N
25e04 4f 52 4d 41 4c 3b 0a 20 20 69 66 28 20 73 71 6c  ORMAL;.  if( sql
25e05 69 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68  ite3StrNICmp((ch
25e06 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
25e07 2e 7a 2c 22 6d 69 6e 22 2c 33 29 3d 3d 30 20 29  .z,"min",3)==0 )
25e08 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48 45  {.    return WHE
25e09 52 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3b 0a  RE_ORDERBY_MIN;.
25e0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69    }else if( sqli
25e0b 74 65 33 53 74 72 4e 49 43 6d 70 28 28 63 68 61  te3StrNICmp((cha
25e0c 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e  r*)pExpr->token.
25e0d 7a 2c 22 6d 61 78 22 2c 33 29 3d 3d 30 20 29 7b  z,"max",3)==0 ){
25e0e 0a 20 20 20 20 72 65 74 75 72 6e 20 57 48 45 52  .    return WHER
25e0f 45 5f 4f 52 44 45 52 42 59 5f 4d 41 58 3b 0a 20  E_ORDERBY_MAX;. 
25e10 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 48 45 52   }.  return WHER
25e11 45 5f 4f 52 44 45 52 42 59 5f 4e 4f 52 4d 41 4c  E_ORDERBY_NORMAL
25e12 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
25e13 65 20 73 6f 75 72 63 65 2d 6c 69 73 74 20 69 74  e source-list it
25e14 65 6d 20 70 61 73 73 65 64 20 61 73 20 61 6e 20  em passed as an 
25e15 61 72 67 75 6d 65 6e 74 20 77 61 73 20 61 75 67  argument was aug
25e16 6d 65 6e 74 65 64 20 77 69 74 68 20 61 6e 0a 2a  mented with an.*
25e17 2a 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61  * INDEXED BY cla
25e18 75 73 65 2c 20 74 68 65 6e 20 74 72 79 20 74 6f  use, then try to
25e19 20 6c 6f 63 61 74 65 20 74 68 65 20 73 70 65 63   locate the spec
25e1a 69 66 69 65 64 20 69 6e 64 65 78 2e 20 49 66 20  ified index. If 
25e1b 74 68 65 72 65 0a 2a 2a 20 77 61 73 20 73 75 63  there.** was suc
25e1c 68 20 61 20 63 6c 61 75 73 65 20 61 6e 64 20 74  h a clause and t
25e1d 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 20 63  he named index c
25e1e 61 6e 6e 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20  annot be found, 
25e1f 72 65 74 75 72 6e 20 0a 2a 2a 20 53 51 4c 49 54  return .** SQLIT
25e20 45 5f 45 52 52 4f 52 20 61 6e 64 20 6c 65 61 76  E_ERROR and leav
25e21 65 20 61 6e 20 65 72 72 6f 72 20 69 6e 20 70 50  e an error in pP
25e22 61 72 73 65 2e 20 4f 74 68 65 72 77 69 73 65 2c  arse. Otherwise,
25e23 20 70 6f 70 75 6c 61 74 65 20 0a 2a 2a 20 70 46   populate .** pF
25e24 72 6f 6d 2d 3e 70 49 6e 64 65 78 20 61 6e 64 20  rom->pIndex and 
25e25 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
25e26 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
25e27 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 49  ATE int sqlite3I
25e28 6e 64 65 78 65 64 42 79 4c 6f 6f 6b 75 70 28 50  ndexedByLookup(P
25e29 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 73 74  arse *pParse, st
25e2a 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65  ruct SrcList_ite
25e2b 6d 20 2a 70 46 72 6f 6d 29 7b 0a 20 20 69 66 28  m *pFrom){.  if(
25e2c 20 70 46 72 6f 6d 2d 3e 70 54 61 62 20 26 26 20   pFrom->pTab && 
25e2d 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78 20 29 7b  pFrom->zIndex ){
25e2e 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
25e2f 20 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a   = pFrom->pTab;.
25e30 20 20 20 20 63 68 61 72 20 2a 7a 49 6e 64 65 78      char *zIndex
25e31 20 3d 20 70 46 72 6f 6d 2d 3e 7a 49 6e 64 65 78   = pFrom->zIndex
25e32 3b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70 49 64  ;.    Index *pId
25e33 78 3b 0a 20 20 20 20 66 6f 72 28 70 49 64 78 3d  x;.    for(pIdx=
25e34 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 0a 20  pTab->pIndex; . 
25e35 20 20 20 20 20 20 20 70 49 64 78 20 26 26 20 73         pIdx && s
25e36 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28 70 49  qlite3StrICmp(pI
25e37 64 78 2d 3e 7a 4e 61 6d 65 2c 20 7a 49 6e 64 65  dx->zName, zInde
25e38 78 29 3b 20 0a 20 20 20 20 20 20 20 20 70 49 64  x); .        pId
25e39 78 3d 70 49 64 78 2d 3e 70 4e 65 78 74 0a 20 20  x=pIdx->pNext.  
25e3a 20 20 29 3b 0a 20 20 20 20 69 66 28 20 21 70 49    );.    if( !pI
25e3b 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  dx ){.      sqli
25e3c 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
25e3d 73 65 2c 20 22 6e 6f 20 73 75 63 68 20 69 6e 64  se, "no such ind
25e3e 65 78 3a 20 25 73 22 2c 20 7a 49 6e 64 65 78 2c  ex: %s", zIndex,
25e3f 20 30 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72   0);.      retur
25e40 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  n SQLITE_ERROR;.
25e41 20 20 20 20 7d 0a 20 20 20 20 70 46 72 6f 6d 2d      }.    pFrom-
25e42 3e 70 49 6e 64 65 78 20 3d 20 70 49 64 78 3b 0a  >pIndex = pIdx;.
25e43 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
25e44 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
25e45 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
25e46 20 61 20 57 61 6c 6b 65 72 20 63 61 6c 6c 62 61   a Walker callba
25e47 63 6b 20 66 6f 72 20 22 65 78 70 61 6e 64 69 6e  ck for "expandin
25e48 67 22 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  g" a SELECT stat
25e49 65 6d 65 6e 74 2e 0a 2a 2a 20 22 45 78 70 61 6e  ement..** "Expan
25e4a 64 69 6e 67 22 20 6d 65 61 6e 73 20 74 6f 20 64  ding" means to d
25e4b 6f 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a  o the following:
25e4c 0a 2a 2a 0a 2a 2a 20 20 20 20 28 31 29 20 20 4d  .**.**    (1)  M
25e4d 61 6b 65 20 73 75 72 65 20 56 44 42 45 20 63 75  ake sure VDBE cu
25e4e 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 68 61 76  rsor numbers hav
25e4f 65 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20  e been assigned 
25e50 74 6f 20 65 76 65 72 79 0a 2a 2a 20 20 20 20 20  to every.**     
25e51 20 20 20 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74      element of t
25e52 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a  he FROM clause..
25e53 2a 2a 0a 2a 2a 20 20 20 20 28 32 29 20 20 46 69  **.**    (2)  Fi
25e54 6c 6c 20 69 6e 20 74 68 65 20 70 54 61 62 4c 69  ll in the pTabLi
25e55 73 74 2d 3e 61 5b 5d 2e 70 54 61 62 20 66 69 65  st->a[].pTab fie
25e56 6c 64 73 20 69 6e 20 74 68 65 20 53 72 63 4c 69  lds in the SrcLi
25e57 73 74 20 74 68 61 74 20 0a 2a 2a 20 20 20 20 20  st that .**     
25e58 20 20 20 20 64 65 66 69 6e 65 73 20 46 52 4f 4d      defines FROM
25e59 20 63 6c 61 75 73 65 2e 20 20 57 68 65 6e 20 76   clause.  When v
25e5a 69 65 77 73 20 61 70 70 65 61 72 20 69 6e 20 74  iews appear in t
25e5b 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 2c 0a  he FROM clause,.
25e5c 2a 2a 20 20 20 20 20 20 20 20 20 66 69 6c 6c 20  **         fill 
25e5d 70 54 61 62 4c 69 73 74 2d 3e 61 5b 5d 2e 70 53  pTabList->a[].pS
25e5e 65 6c 65 63 74 20 77 69 74 68 20 61 20 63 6f 70  elect with a cop
25e5f 79 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  y of the SELECT 
25e60 73 74 61 74 65 6d 65 6e 74 0a 2a 2a 20 20 20 20  statement.**    
25e61 20 20 20 20 20 74 68 61 74 20 69 6d 70 6c 65 6d       that implem
25e62 65 6e 74 73 20 74 68 65 20 76 69 65 77 2e 20 20  ents the view.  
25e63 41 20 63 6f 70 79 20 69 73 20 6d 61 64 65 20 6f  A copy is made o
25e64 66 20 74 68 65 20 76 69 65 77 27 73 20 53 45 4c  f the view's SEL
25e65 45 43 54 0a 2a 2a 20 20 20 20 20 20 20 20 20 73  ECT.**         s
25e66 74 61 74 65 6d 65 6e 74 20 73 6f 20 74 68 61 74  tatement so that
25e67 20 77 65 20 63 61 6e 20 66 72 65 65 6c 79 20 6d   we can freely m
25e68 6f 64 69 66 79 20 6f 72 20 64 65 6c 65 74 65 20  odify or delete 
25e69 74 68 61 74 20 73 74 61 74 65 6d 65 6e 74 0a 2a  that statement.*
25e6a 2a 20 20 20 20 20 20 20 20 20 77 69 74 68 6f 75  *         withou
25e6b 74 20 77 6f 72 72 79 69 6e 67 20 61 62 6f 75 74  t worrying about
25e6c 20 6d 65 73 73 69 6e 67 20 75 70 20 74 68 65 20   messing up the 
25e6d 70 72 65 73 69 73 74 65 6e 74 20 72 65 70 72 65  presistent repre
25e6e 73 65 6e 74 61 74 69 6f 6e 0a 2a 2a 20 20 20 20  sentation.**    
25e6f 20 20 20 20 20 6f 66 20 74 68 65 20 76 69 65 77       of the view
25e70 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 28 33 29 20 20  ..**.**    (3)  
25e71 41 64 64 20 74 65 72 6d 73 20 74 6f 20 74 68 65  Add terms to the
25e72 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
25e73 20 61 63 63 6f 6d 6f 64 61 74 65 20 74 68 65 20   accomodate the 
25e74 4e 41 54 55 52 41 4c 20 6b 65 79 77 6f 72 64 0a  NATURAL keyword.
25e75 2a 2a 20 20 20 20 20 20 20 20 20 6f 6e 20 6a 6f  **         on jo
25e76 69 6e 73 20 61 6e 64 20 74 68 65 20 4f 4e 20 61  ins and the ON a
25e77 6e 64 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20  nd USING clause 
25e78 6f 66 20 6a 6f 69 6e 73 2e 0a 2a 2a 0a 2a 2a 20  of joins..**.** 
25e79 20 20 20 28 34 29 20 20 53 63 61 6e 20 74 68 65     (4)  Scan the
25e7a 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e 73   list of columns
25e7b 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 73   in the result s
25e7c 65 74 20 28 70 45 4c 69 73 74 29 20 6c 6f 6f 6b  et (pEList) look
25e7d 69 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 66  ing.**         f
25e7e 6f 72 20 69 6e 73 74 61 6e 63 65 73 20 6f 66 20  or instances of 
25e7f 74 68 65 20 22 2a 22 20 6f 70 65 72 61 74 6f 72  the "*" operator
25e80 20 6f 72 20 74 68 65 20 54 41 42 4c 45 2e 2a 20   or the TABLE.* 
25e81 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 20 20 20 20  operator..**    
25e82 20 20 20 20 20 49 66 20 66 6f 75 6e 64 2c 20 65       If found, e
25e83 78 70 61 6e 64 20 65 61 63 68 20 22 2a 22 20 74  xpand each "*" t
25e84 6f 20 62 65 20 65 76 65 72 79 20 63 6f 6c 75 6d  o be every colum
25e85 6e 20 69 6e 20 65 76 65 72 79 20 74 61 62 6c 65  n in every table
25e86 0a 2a 2a 20 20 20 20 20 20 20 20 20 61 6e 64 20  .**         and 
25e87 54 41 42 4c 45 2e 2a 20 74 6f 20 62 65 20 65 76  TABLE.* to be ev
25e88 65 72 79 20 63 6f 6c 75 6d 6e 20 69 6e 20 54 41  ery column in TA
25e89 42 4c 45 2e 0a 2a 2a 0a 2a 2f 0a 73 74 61 74 69  BLE..**.*/.stati
25e8a 63 20 69 6e 74 20 73 65 6c 65 63 74 45 78 70 61  c int selectExpa
25e8b 6e 64 65 72 28 57 61 6c 6b 65 72 20 2a 70 57 61  nder(Walker *pWa
25e8c 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a 70 29  lker, Select *p)
25e8d 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
25e8e 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61  e = pWalker->pPa
25e8f 72 73 65 3b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c  rse;.  int i, j,
25e90 20 6b 3b 0a 20 20 53 72 63 4c 69 73 74 20 2a 70   k;.  SrcList *p
25e91 54 61 62 4c 69 73 74 3b 0a 20 20 45 78 70 72 4c  TabList;.  ExprL
25e92 69 73 74 20 2a 70 45 4c 69 73 74 3b 0a 20 20 73  ist *pEList;.  s
25e93 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74  truct SrcList_it
25e94 65 6d 20 2a 70 46 72 6f 6d 3b 0a 20 20 73 71 6c  em *pFrom;.  sql
25e95 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73  ite3 *db = pPars
25e96 65 2d 3e 64 62 3b 0a 0a 20 20 69 66 28 20 64 62  e->db;..  if( db
25e97 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 20  ->mallocFailed  
25e98 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  ){.    return WR
25e99 43 5f 41 62 6f 72 74 3b 0a 20 20 7d 0a 20 20 69  C_Abort;.  }.  i
25e9a 66 28 20 70 2d 3e 70 53 72 63 3d 3d 30 20 7c 7c  f( p->pSrc==0 ||
25e9b 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20   (p->selFlags & 
25e9c 53 46 5f 45 78 70 61 6e 64 65 64 29 21 3d 30 20  SF_Expanded)!=0 
25e9d 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 57 52  ){.    return WR
25e9e 43 5f 50 72 75 6e 65 3b 0a 20 20 7d 0a 20 20 70  C_Prune;.  }.  p
25e9f 2d 3e 73 65 6c 46 6c 61 67 73 20 7c 3d 20 53 46  ->selFlags |= SF
25ea0 5f 45 78 70 61 6e 64 65 64 3b 0a 20 20 70 54 61  _Expanded;.  pTa
25ea1 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63 3b  bList = p->pSrc;
25ea2 0a 20 20 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70  .  pEList = p->p
25ea3 45 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 4d 61 6b  EList;..  /* Mak
25ea4 65 20 73 75 72 65 20 63 75 72 73 6f 72 20 6e 75  e sure cursor nu
25ea5 6d 62 65 72 73 20 68 61 76 65 20 62 65 65 6e 20  mbers have been 
25ea6 61 73 73 69 67 6e 65 64 20 74 6f 20 61 6c 6c 20  assigned to all 
25ea7 65 6e 74 72 69 65 73 20 69 6e 0a 20 20 2a 2a 20  entries in.  ** 
25ea8 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
25ea9 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20 73 74  of the SELECT st
25eaa 61 74 65 6d 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20  atement..  */.  
25eab 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 73  sqlite3SrcListAs
25eac 73 69 67 6e 43 75 72 73 6f 72 73 28 70 50 61 72  signCursors(pPar
25ead 73 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 0a  se, pTabList);..
25eae 20 20 2f 2a 20 4c 6f 6f 6b 20 75 70 20 65 76 65    /* Look up eve
25eaf 72 79 20 74 61 62 6c 65 20 6e 61 6d 65 64 20 69  ry table named i
25eb0 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
25eb1 65 20 6f 66 20 74 68 65 20 73 65 6c 65 63 74 2e  e of the select.
25eb2 20 20 49 66 0a 20 20 2a 2a 20 61 6e 20 65 6e 74    If.  ** an ent
25eb3 72 79 20 6f 66 20 74 68 65 20 46 52 4f 4d 20 63  ry of the FROM c
25eb4 6c 61 75 73 65 20 69 73 20 61 20 73 75 62 71 75  lause is a subqu
25eb5 65 72 79 20 69 6e 73 74 65 61 64 20 6f 66 20 61  ery instead of a
25eb6 20 74 61 62 6c 65 20 6f 72 20 76 69 65 77 2c 0a   table or view,.
25eb7 20 20 2a 2a 20 74 68 65 6e 20 63 72 65 61 74 65    ** then create
25eb8 20 61 20 74 72 61 6e 73 69 65 6e 74 20 74 61 62   a transient tab
25eb9 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 6f 20  le structure to 
25eba 64 65 73 63 72 69 62 65 20 74 68 65 20 73 75 62  describe the sub
25ebb 71 75 65 72 79 2e 0a 20 20 2a 2f 0a 20 20 66 6f  query..  */.  fo
25ebc 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d 70 54 61  r(i=0, pFrom=pTa
25ebd 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70 54 61 62  bList->a; i<pTab
25ebe 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c  List->nSrc; i++,
25ebf 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20 20 20 54   pFrom++){.    T
25ec0 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 20 20  able *pTab;.    
25ec1 69 66 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62 21  if( pFrom->pTab!
25ec2 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20 54  =0 ){.      /* T
25ec3 68 69 73 20 73 74 61 74 65 6d 65 6e 74 20 68 61  his statement ha
25ec4 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 70  s already been p
25ec5 72 65 70 61 72 65 64 2e 20 20 54 68 65 72 65 20  repared.  There 
25ec6 69 73 20 6e 6f 20 6e 65 65 64 0a 20 20 20 20 20  is no need.     
25ec7 20 2a 2a 20 74 6f 20 67 6f 20 66 75 72 74 68 65   ** to go furthe
25ec8 72 2e 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65  r. */.      asse
25ec9 72 74 28 20 69 3d 3d 30 20 29 3b 0a 20 20 20 20  rt( i==0 );.    
25eca 20 20 72 65 74 75 72 6e 20 57 52 43 5f 50 72 75    return WRC_Pru
25ecb 6e 65 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ne;.    }.    if
25ecc 28 20 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 3d 3d  ( pFrom->zName==
25ecd 30 20 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c  0 ){.#ifndef SQL
25ece 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52  ITE_OMIT_SUBQUER
25ecf 59 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  Y.      Select *
25ed0 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53  pSel = pFrom->pS
25ed1 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 2f 2a 20  elect;.      /* 
25ed2 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e 20 74  A sub-query in t
25ed3 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 6f  he FROM clause o
25ed4 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a 20 20  f a SELECT */.  
25ed5 20 20 20 20 61 73 73 65 72 74 28 20 70 53 65 6c      assert( pSel
25ed6 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  !=0 );.      ass
25ed7 65 72 74 28 20 70 46 72 6f 6d 2d 3e 70 54 61 62  ert( pFrom->pTab
25ed8 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 73 71 6c  ==0 );.      sql
25ed9 69 74 65 33 57 61 6c 6b 53 65 6c 65 63 74 28 70  ite3WalkSelect(p
25eda 57 61 6c 6b 65 72 2c 20 70 53 65 6c 29 3b 0a 20  Walker, pSel);. 
25edb 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70 54 61 62       pFrom->pTab
25edc 20 3d 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65   = pTab = sqlite
25edd 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62  3DbMallocZero(db
25ede 2c 20 73 69 7a 65 6f 66 28 54 61 62 6c 65 29 29  , sizeof(Table))
25edf 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
25ee0 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
25ee1 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Abort;.      pT
25ee2 61 62 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20 20  ab->db = db;.   
25ee3 20 20 20 70 54 61 62 2d 3e 6e 52 65 66 20 3d 20     pTab->nRef = 
25ee4 31 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  1;.      pTab->z
25ee5 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 4d 50  Name = sqlite3MP
25ee6 72 69 6e 74 66 28 64 62 2c 20 22 73 71 6c 69 74  rintf(db, "sqlit
25ee7 65 5f 73 75 62 71 75 65 72 79 5f 25 70 5f 22 2c  e_subquery_%p_",
25ee8 20 28 76 6f 69 64 2a 29 70 54 61 62 29 3b 0a 20   (void*)pTab);. 
25ee9 20 20 20 20 20 77 68 69 6c 65 28 20 70 53 65 6c       while( pSel
25eea 2d 3e 70 50 72 69 6f 72 20 29 7b 20 70 53 65 6c  ->pPrior ){ pSel
25eeb 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72 3b   = pSel->pPrior;
25eec 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63 74 43   }.      selectC
25eed 6f 6c 75 6d 6e 73 46 72 6f 6d 45 78 70 72 4c 69  olumnsFromExprLi
25eee 73 74 28 70 50 61 72 73 65 2c 20 70 53 65 6c 2d  st(pParse, pSel-
25eef 3e 70 45 4c 69 73 74 2c 20 26 70 54 61 62 2d 3e  >pEList, &pTab->
25ef0 6e 43 6f 6c 2c 20 26 70 54 61 62 2d 3e 61 43 6f  nCol, &pTab->aCo
25ef1 6c 29 3b 0a 20 20 20 20 20 20 70 54 61 62 2d 3e  l);.      pTab->
25ef2 69 50 4b 65 79 20 3d 20 2d 31 3b 0a 20 20 20 20  iPKey = -1;.    
25ef3 20 20 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73    pTab->tabFlags
25ef4 20 7c 3d 20 54 46 5f 45 70 68 65 6d 65 72 61 6c   |= TF_Ephemeral
25ef5 3b 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d 65 6c  ;.#endif.    }el
25ef6 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20 41 6e 20  se{.      /* An 
25ef7 6f 72 64 69 6e 61 72 79 20 74 61 62 6c 65 20 6f  ordinary table o
25ef8 72 20 76 69 65 77 20 6e 61 6d 65 20 69 6e 20 74  r view name in t
25ef9 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a  he FROM clause *
25efa 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  /.      assert( 
25efb 70 46 72 6f 6d 2d 3e 70 54 61 62 3d 3d 30 20 29  pFrom->pTab==0 )
25efc 3b 0a 20 20 20 20 20 20 70 46 72 6f 6d 2d 3e 70  ;.      pFrom->p
25efd 54 61 62 20 3d 20 70 54 61 62 20 3d 20 0a 20 20  Tab = pTab = .  
25efe 20 20 20 20 20 20 73 71 6c 69 74 65 33 4c 6f 63        sqlite3Loc
25eff 61 74 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ateTable(pParse,
25f00 30 2c 70 46 72 6f 6d 2d 3e 7a 4e 61 6d 65 2c 70  0,pFrom->zName,p
25f01 46 72 6f 6d 2d 3e 7a 44 61 74 61 62 61 73 65 29  From->zDatabase)
25f02 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ;.      if( pTab
25f03 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 57 52 43  ==0 ) return WRC
25f04 5f 41 62 6f 72 74 3b 0a 20 20 20 20 20 20 70 54  _Abort;.      pT
25f05 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a 23 69 66 20  ab->nRef++;.#if 
25f06 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
25f07 4f 4d 49 54 5f 56 49 45 57 29 20 7c 7c 20 21 64  OMIT_VIEW) || !d
25f08 65 66 69 6e 65 64 20 28 53 51 4c 49 54 45 5f 4f  efined (SQLITE_O
25f09 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
25f0a 29 0a 20 20 20 20 20 20 69 66 28 20 70 54 61 62  ).      if( pTab
25f0b 2d 3e 70 53 65 6c 65 63 74 20 7c 7c 20 49 73 56  ->pSelect || IsV
25f0c 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
25f0d 20 20 20 20 20 20 20 20 2f 2a 20 57 65 20 72 65          /* We re
25f0e 61 63 68 20 68 65 72 65 20 69 66 20 74 68 65 20  ach here if the 
25f0f 6e 61 6d 65 64 20 74 61 62 6c 65 20 69 73 20 61  named table is a
25f10 20 72 65 61 6c 6c 79 20 61 20 76 69 65 77 20 2a   really a view *
25f11 2f 0a 20 20 20 20 20 20 20 20 69 66 28 20 73 71  /.        if( sq
25f12 6c 69 74 65 33 56 69 65 77 47 65 74 43 6f 6c 75  lite3ViewGetColu
25f13 6d 6e 4e 61 6d 65 73 28 70 50 61 72 73 65 2c 20  mnNames(pParse, 
25f14 70 54 61 62 29 20 29 20 72 65 74 75 72 6e 20 57  pTab) ) return W
25f15 52 43 5f 41 62 6f 72 74 3b 0a 0a 20 20 20 20 20  RC_Abort;..     
25f16 20 20 20 2f 2a 20 49 66 20 70 46 72 6f 6d 2d 3e     /* If pFrom->
25f17 70 53 65 6c 65 63 74 21 3d 30 20 69 74 20 6d 65  pSelect!=0 it me
25f18 61 6e 73 20 77 65 20 61 72 65 20 64 65 61 6c 69  ans we are deali
25f19 6e 67 20 77 69 74 68 20 61 0a 20 20 20 20 20 20  ng with a.      
25f1a 20 20 2a 2a 20 76 69 65 77 20 77 69 74 68 69 6e    ** view within
25f1b 20 61 20 76 69 65 77 2e 20 20 54 68 65 20 53 45   a view.  The SE
25f1c 4c 45 43 54 20 73 74 72 75 63 74 75 72 65 20 68  LECT structure h
25f1d 61 73 20 61 6c 72 65 61 64 79 20 62 65 65 6e 0a  as already been.
25f1e 20 20 20 20 20 20 20 20 2a 2a 20 63 6f 70 69 65          ** copie
25f1f 64 20 62 79 20 74 68 65 20 6f 75 74 65 72 20 76  d by the outer v
25f20 69 65 77 20 73 6f 20 77 65 20 63 61 6e 20 73 6b  iew so we can sk
25f21 69 70 20 74 68 65 20 63 6f 70 79 20 73 74 65 70  ip the copy step
25f22 20 68 65 72 65 0a 20 20 20 20 20 20 20 20 2a 2a   here.        **
25f23 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 20 76 69   in the inner vi
25f24 65 77 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20  ew..        */. 
25f25 20 20 20 20 20 20 20 69 66 28 20 70 46 72 6f 6d         if( pFrom
25f26 2d 3e 70 53 65 6c 65 63 74 3d 3d 30 20 29 7b 0a  ->pSelect==0 ){.
25f27 20 20 20 20 20 20 20 20 20 20 70 46 72 6f 6d 2d            pFrom-
25f28 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  >pSelect = sqlit
25f29 65 33 53 65 6c 65 63 74 44 75 70 28 64 62 2c 20  e3SelectDup(db, 
25f2a 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 29 3b 0a  pTab->pSelect);.
25f2b 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
25f2c 33 57 61 6c 6b 53 65 6c 65 63 74 28 70 57 61 6c  3WalkSelect(pWal
25f2d 6b 65 72 2c 20 70 46 72 6f 6d 2d 3e 70 53 65 6c  ker, pFrom->pSel
25f2e 65 63 74 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  ect);.        }.
25f2f 20 20 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20        }.#endif. 
25f30 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 4c 6f 63     }..    /* Loc
25f31 61 74 65 20 74 68 65 20 69 6e 64 65 78 20 6e 61  ate the index na
25f32 6d 65 64 20 62 79 20 74 68 65 20 49 4e 44 45 58  med by the INDEX
25f33 45 44 20 42 59 20 63 6c 61 75 73 65 2c 20 69 66  ED BY clause, if
25f34 20 61 6e 79 2e 20 2a 2f 0a 20 20 20 20 69 66 28   any. */.    if(
25f35 20 73 71 6c 69 74 65 33 49 6e 64 65 78 65 64 42   sqlite3IndexedB
25f36 79 4c 6f 6f 6b 75 70 28 70 50 61 72 73 65 2c 20  yLookup(pParse, 
25f37 70 46 72 6f 6d 29 20 29 7b 0a 20 20 20 20 20 20  pFrom) ){.      
25f38 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f 72 74  return WRC_Abort
25f39 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f  ;.    }.  }..  /
25f3a 2a 20 50 72 6f 63 65 73 73 20 4e 41 54 55 52 41  * Process NATURA
25f3b 4c 20 6b 65 79 77 6f 72 64 73 2c 20 61 6e 64 20  L keywords, and 
25f3c 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63 6c 61  ON and USING cla
25f3d 75 73 65 73 20 6f 66 20 6a 6f 69 6e 73 2e 0a 20  uses of joins.. 
25f3e 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 6d 61   */.  if( db->ma
25f3f 6c 6c 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73 71  llocFailed || sq
25f40 6c 69 74 65 50 72 6f 63 65 73 73 4a 6f 69 6e 28  liteProcessJoin(
25f41 70 50 61 72 73 65 2c 20 70 29 20 29 7b 0a 20 20  pParse, p) ){.  
25f42 20 20 72 65 74 75 72 6e 20 57 52 43 5f 41 62 6f    return WRC_Abo
25f43 72 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 6f  rt;.  }..  /* Fo
25f44 72 20 65 76 65 72 79 20 22 2a 22 20 74 68 61 74  r every "*" that
25f45 20 6f 63 63 75 72 73 20 69 6e 20 74 68 65 20 63   occurs in the c
25f46 6f 6c 75 6d 6e 20 6c 69 73 74 2c 20 69 6e 73 65  olumn list, inse
25f47 72 74 20 74 68 65 20 6e 61 6d 65 73 20 6f 66 0a  rt the names of.
25f48 20 20 2a 2a 20 61 6c 6c 20 63 6f 6c 75 6d 6e 73    ** all columns
25f49 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73 2e 20   in all tables. 
25f4a 20 41 6e 64 20 66 6f 72 20 65 76 65 72 79 20 54   And for every T
25f4b 41 42 4c 45 2e 2a 20 69 6e 73 65 72 74 20 74 68  ABLE.* insert th
25f4c 65 20 6e 61 6d 65 73 0a 20 20 2a 2a 20 6f 66 20  e names.  ** of 
25f4d 61 6c 6c 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 54  all columns in T
25f4e 41 42 4c 45 2e 20 20 54 68 65 20 70 61 72 73 65  ABLE.  The parse
25f4f 72 20 69 6e 73 65 72 74 65 64 20 61 20 73 70 65  r inserted a spe
25f50 63 69 61 6c 20 65 78 70 72 65 73 73 69 6f 6e 0a  cial expression.
25f51 20 20 2a 2a 20 77 69 74 68 20 74 68 65 20 54 4b    ** with the TK
25f52 5f 41 4c 4c 20 6f 70 65 72 61 74 6f 72 20 66 6f  _ALL operator fo
25f53 72 20 65 61 63 68 20 22 2a 22 20 74 68 61 74 20  r each "*" that 
25f54 69 74 20 66 6f 75 6e 64 20 69 6e 20 74 68 65 20  it found in the 
25f55 63 6f 6c 75 6d 6e 20 6c 69 73 74 2e 0a 20 20 2a  column list..  *
25f56 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
25f57 63 6f 64 65 20 6a 75 73 74 20 68 61 73 20 74 6f  code just has to
25f58 20 6c 6f 63 61 74 65 20 74 68 65 20 54 4b 5f 41   locate the TK_A
25f59 4c 4c 20 65 78 70 72 65 73 73 69 6f 6e 73 20 61  LL expressions a
25f5a 6e 64 20 65 78 70 61 6e 64 0a 20 20 2a 2a 20 65  nd expand.  ** e
25f5b 61 63 68 20 6f 6e 65 20 74 6f 20 74 68 65 20 6c  ach one to the l
25f5c 69 73 74 20 6f 66 20 61 6c 6c 20 63 6f 6c 75 6d  ist of all colum
25f5d 6e 73 20 69 6e 20 61 6c 6c 20 74 61 62 6c 65 73  ns in all tables
25f5e 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20  ..  **.  ** The 
25f5f 66 69 72 73 74 20 6c 6f 6f 70 20 6a 75 73 74 20  first loop just 
25f60 63 68 65 63 6b 73 20 74 6f 20 73 65 65 20 69 66  checks to see if
25f61 20 74 68 65 72 65 20 61 72 65 20 61 6e 79 20 22   there are any "
25f62 2a 22 20 6f 70 65 72 61 74 6f 72 73 0a 20 20 2a  *" operators.  *
25f63 2a 20 74 68 61 74 20 6e 65 65 64 20 65 78 70 61  * that need expa
25f64 6e 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 66 6f  nding..  */.  fo
25f65 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69 73 74 2d  r(k=0; k<pEList-
25f66 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b 0a 20 20  >nExpr; k++){.  
25f67 20 20 45 78 70 72 20 2a 70 45 20 3d 20 70 45 4c    Expr *pE = pEL
25f68 69 73 74 2d 3e 61 5b 6b 5d 2e 70 45 78 70 72 3b  ist->a[k].pExpr;
25f69 0a 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d  .    if( pE->op=
25f6a 3d 54 4b 5f 41 4c 4c 20 29 20 62 72 65 61 6b 3b  =TK_ALL ) break;
25f6b 0a 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 3d  .    if( pE->op=
25f6c 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45 2d 3e 70  =TK_DOT && pE->p
25f6d 52 69 67 68 74 20 26 26 20 70 45 2d 3e 70 52 69  Right && pE->pRi
25f6e 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 41 4c 4c 0a  ght->op==TK_ALL.
25f6f 20 20 20 20 20 20 20 20 20 26 26 20 70 45 2d 3e           && pE->
25f70 70 4c 65 66 74 20 26 26 20 70 45 2d 3e 70 4c 65  pLeft && pE->pLe
25f71 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 49 44 20 29 20  ft->op==TK_ID ) 
25f72 62 72 65 61 6b 3b 0a 20 20 7d 0a 20 20 69 66 28  break;.  }.  if(
25f73 20 6b 3c 70 45 4c 69 73 74 2d 3e 6e 45 78 70 72   k<pEList->nExpr
25f74 20 29 7b 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a   ){.    /*.    *
25f75 2a 20 49 66 20 77 65 20 67 65 74 20 68 65 72 65  * If we get here
25f76 20 69 74 20 6d 65 61 6e 73 20 74 68 65 20 72 65   it means the re
25f77 73 75 6c 74 20 73 65 74 20 63 6f 6e 74 61 69 6e  sult set contain
25f78 73 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 22 2a  s one or more "*
25f79 22 0a 20 20 20 20 2a 2a 20 6f 70 65 72 61 74 6f  ".    ** operato
25f7a 72 73 20 74 68 61 74 20 6e 65 65 64 20 74 6f 20  rs that need to 
25f7b 62 65 20 65 78 70 61 6e 64 65 64 2e 20 20 4c 6f  be expanded.  Lo
25f7c 6f 70 20 74 68 72 6f 75 67 68 20 65 61 63 68 20  op through each 
25f7d 65 78 70 72 65 73 73 69 6f 6e 0a 20 20 20 20 2a  expression.    *
25f7e 2a 20 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20  * in the result 
25f7f 73 65 74 20 61 6e 64 20 65 78 70 61 6e 64 20 74  set and expand t
25f80 68 65 6d 20 6f 6e 65 20 62 79 20 6f 6e 65 2e 0a  hem one by one..
25f81 20 20 20 20 2a 2f 0a 20 20 20 20 73 74 72 75 63      */.    struc
25f82 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
25f83 2a 61 20 3d 20 70 45 4c 69 73 74 2d 3e 61 3b 0a  *a = pEList->a;.
25f84 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4e      ExprList *pN
25f85 65 77 20 3d 20 30 3b 0a 20 20 20 20 69 6e 74 20  ew = 0;.    int 
25f86 66 6c 61 67 73 20 3d 20 70 50 61 72 73 65 2d 3e  flags = pParse->
25f87 64 62 2d 3e 66 6c 61 67 73 3b 0a 20 20 20 20 69  db->flags;.    i
25f88 6e 74 20 6c 6f 6e 67 4e 61 6d 65 73 20 3d 20 28  nt longNames = (
25f89 66 6c 61 67 73 20 26 20 53 51 4c 49 54 45 5f 46  flags & SQLITE_F
25f8a 75 6c 6c 43 6f 6c 4e 61 6d 65 73 29 21 3d 30 0a  ullColNames)!=0.
25f8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
25f8c 20 20 20 20 20 20 26 26 20 28 66 6c 61 67 73 20        && (flags 
25f8d 26 20 53 51 4c 49 54 45 5f 53 68 6f 72 74 43 6f  & SQLITE_ShortCo
25f8e 6c 4e 61 6d 65 73 29 3d 3d 30 3b 0a 0a 20 20 20  lNames)==0;..   
25f8f 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 70 45 4c 69   for(k=0; k<pELi
25f90 73 74 2d 3e 6e 45 78 70 72 3b 20 6b 2b 2b 29 7b  st->nExpr; k++){
25f91 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 45 20  .      Expr *pE 
25f92 3d 20 61 5b 6b 5d 2e 70 45 78 70 72 3b 0a 20 20  = a[k].pExpr;.  
25f93 20 20 20 20 69 66 28 20 70 45 2d 3e 6f 70 21 3d      if( pE->op!=
25f94 54 4b 5f 41 4c 4c 20 26 26 0a 20 20 20 20 20 20  TK_ALL &&.      
25f95 20 20 20 20 20 28 70 45 2d 3e 6f 70 21 3d 54 4b       (pE->op!=TK
25f96 5f 44 4f 54 20 7c 7c 20 70 45 2d 3e 70 52 69 67  _DOT || pE->pRig
25f97 68 74 3d 3d 30 20 7c 7c 20 70 45 2d 3e 70 52 69  ht==0 || pE->pRi
25f98 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 41 4c 4c 29  ght->op!=TK_ALL)
25f99 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54   ){.        /* T
25f9a 68 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 65  his particular e
25f9b 78 70 72 65 73 73 69 6f 6e 20 64 6f 65 73 20 6e  xpression does n
25f9c 6f 74 20 6e 65 65 64 20 74 6f 20 62 65 20 65 78  ot need to be ex
25f9d 70 61 6e 64 65 64 2e 0a 20 20 20 20 20 20 20 20  panded..        
25f9e 2a 2f 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20  */.        pNew 
25f9f 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
25fa0 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
25fa1 70 4e 65 77 2c 20 61 5b 6b 5d 2e 70 45 78 70 72  pNew, a[k].pExpr
25fa2 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 69 66  , 0);.        if
25fa3 28 20 70 4e 65 77 20 29 7b 0a 20 20 20 20 20 20  ( pNew ){.      
25fa4 20 20 20 20 70 4e 65 77 2d 3e 61 5b 70 4e 65 77      pNew->a[pNew
25fa5 2d 3e 6e 45 78 70 72 2d 31 5d 2e 7a 4e 61 6d 65  ->nExpr-1].zName
25fa6 20 3d 20 61 5b 6b 5d 2e 7a 4e 61 6d 65 3b 0a 20   = a[k].zName;. 
25fa7 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
25fa8 20 61 5b 6b 5d 2e 70 45 78 70 72 20 3d 20 30 3b   a[k].pExpr = 0;
25fa9 0a 20 20 20 20 20 20 20 20 61 5b 6b 5d 2e 7a 4e  .        a[k].zN
25faa 61 6d 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ame = 0;.      }
25fab 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a  else{.        /*
25fac 20 54 68 69 73 20 65 78 70 72 65 73 73 69 6f 6e   This expression
25fad 20 69 73 20 61 20 22 2a 22 20 6f 72 20 61 20 22   is a "*" or a "
25fae 54 41 42 4c 45 2e 2a 22 20 61 6e 64 20 6e 65 65  TABLE.*" and nee
25faf 64 73 20 74 6f 20 62 65 0a 20 20 20 20 20 20 20  ds to be.       
25fb0 20 2a 2a 20 65 78 70 61 6e 64 65 64 2e 20 2a 2f   ** expanded. */
25fb1 0a 20 20 20 20 20 20 20 20 69 6e 74 20 74 61 62  .        int tab
25fb2 6c 65 53 65 65 6e 20 3d 20 30 3b 20 20 20 20 20  leSeen = 0;     
25fb3 20 2f 2a 20 53 65 74 20 74 6f 20 31 20 77 68 65   /* Set to 1 whe
25fb4 6e 20 54 41 42 4c 45 20 6d 61 74 63 68 65 73 20  n TABLE matches 
25fb5 2a 2f 0a 20 20 20 20 20 20 20 20 63 68 61 72 20  */.        char 
25fb6 2a 7a 54 4e 61 6d 65 3b 20 20 20 20 20 20 20 20  *zTName;        
25fb7 20 20 20 20 2f 2a 20 74 65 78 74 20 6f 66 20 6e      /* text of n
25fb8 61 6d 65 20 6f 66 20 54 41 42 4c 45 20 2a 2f 0a  ame of TABLE */.
25fb9 20 20 20 20 20 20 20 20 69 66 28 20 70 45 2d 3e          if( pE->
25fba 6f 70 3d 3d 54 4b 5f 44 4f 54 20 26 26 20 70 45  op==TK_DOT && pE
25fbb 2d 3e 70 4c 65 66 74 20 29 7b 0a 20 20 20 20 20  ->pLeft ){.     
25fbc 20 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 73 71       zTName = sq
25fbd 6c 69 74 65 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b  lite3NameFromTok
25fbe 65 6e 28 64 62 2c 20 26 70 45 2d 3e 70 4c 65 66  en(db, &pE->pLef
25fbf 74 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20 20 20  t->token);.     
25fc0 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
25fc1 20 20 20 20 7a 54 4e 61 6d 65 20 3d 20 30 3b 0a      zTName = 0;.
25fc2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
25fc3 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d    for(i=0, pFrom
25fc4 3d 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c  =pTabList->a; i<
25fc5 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20  pTabList->nSrc; 
25fc6 69 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20  i++, pFrom++){. 
25fc7 20 20 20 20 20 20 20 20 20 54 61 62 6c 65 20 2a           Table *
25fc8 70 54 61 62 20 3d 20 70 46 72 6f 6d 2d 3e 70 54  pTab = pFrom->pT
25fc9 61 62 3b 0a 20 20 20 20 20 20 20 20 20 20 63 68  ab;.          ch
25fca 61 72 20 2a 7a 54 61 62 4e 61 6d 65 20 3d 20 70  ar *zTabName = p
25fcb 46 72 6f 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  From->zAlias;.  
25fcc 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 61 62          if( zTab
25fcd 4e 61 6d 65 3d 3d 30 20 7c 7c 20 7a 54 61 62 4e  Name==0 || zTabN
25fce 61 6d 65 5b 30 5d 3d 3d 30 20 29 7b 20 0a 20 20  ame[0]==0 ){ .  
25fcf 20 20 20 20 20 20 20 20 20 20 7a 54 61 62 4e 61            zTabNa
25fd0 6d 65 20 3d 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  me = pTab->zName
25fd1 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20  ;.          }.  
25fd2 20 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e          if( db->
25fd3 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 62  mallocFailed ) b
25fd4 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20  reak;.          
25fd5 69 66 28 20 7a 54 4e 61 6d 65 20 26 26 20 73 71  if( zTName && sq
25fd6 6c 69 74 65 33 53 74 72 49 43 6d 70 28 7a 54 4e  lite3StrICmp(zTN
25fd7 61 6d 65 2c 20 7a 54 61 62 4e 61 6d 65 29 21 3d  ame, zTabName)!=
25fd8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  0 ){.           
25fd9 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
25fda 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20       }.         
25fdb 20 74 61 62 6c 65 53 65 65 6e 20 3d 20 31 3b 0a   tableSeen = 1;.
25fdc 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 3d            for(j=
25fdd 30 3b 20 6a 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b  0; j<pTab->nCol;
25fde 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20   j++){.         
25fdf 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c 20     Expr *pExpr, 
25fe0 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 20 20 20  *pRight;.       
25fe1 20 20 20 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65       char *zName
25fe2 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d   = pTab->aCol[j]
25fe3 2e 7a 4e 61 6d 65 3b 0a 0a 20 20 20 20 20 20 20  .zName;..       
25fe4 20 20 20 20 20 2f 2a 20 49 66 20 61 20 63 6f 6c       /* If a col
25fe5 75 6d 6e 20 69 73 20 6d 61 72 6b 65 64 20 61 73  umn is marked as
25fe6 20 27 68 69 64 64 65 6e 27 20 28 63 75 72 72 65   'hidden' (curre
25fe7 6e 74 6c 79 20 6f 6e 6c 79 20 70 6f 73 73 69 62  ntly only possib
25fe8 6c 65 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  le.            *
25fe9 2a 20 66 6f 72 20 76 69 72 74 75 61 6c 20 74 61  * for virtual ta
25fea 62 6c 65 73 29 2c 20 64 6f 20 6e 6f 74 20 69 6e  bles), do not in
25feb 63 6c 75 64 65 20 69 74 20 69 6e 20 74 68 65 20  clude it in the 
25fec 65 78 70 61 6e 64 65 64 0a 20 20 20 20 20 20 20  expanded.       
25fed 20 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 2d 73       ** result-s
25fee 65 74 20 6c 69 73 74 2e 0a 20 20 20 20 20 20 20  et list..       
25fef 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
25ff0 20 20 20 20 69 66 28 20 49 73 48 69 64 64 65 6e      if( IsHidden
25ff1 43 6f 6c 75 6d 6e 28 26 70 54 61 62 2d 3e 61 43  Column(&pTab->aC
25ff2 6f 6c 5b 6a 5d 29 20 29 7b 0a 20 20 20 20 20 20  ol[j]) ){.      
25ff3 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 49          assert(I
25ff4 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 29 3b  sVirtual(pTab));
25ff5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63  .              c
25ff6 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 20  ontinue;.       
25ff7 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
25ff8 20 20 20 20 69 66 28 20 69 3e 30 20 29 7b 0a 20      if( i>0 ){. 
25ff9 20 20 20 20 20 20 20 20 20 20 20 20 20 73 74 72               str
25ffa 75 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d  uct SrcList_item
25ffb 20 2a 70 4c 65 66 74 20 3d 20 26 70 54 61 62 4c   *pLeft = &pTabL
25ffc 69 73 74 2d 3e 61 5b 69 2d 31 5d 3b 0a 20 20 20  ist->a[i-1];.   
25ffd 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 28             if( (
25ffe 70 4c 65 66 74 5b 31 5d 2e 6a 6f 69 6e 74 79 70  pLeft[1].jointyp
25fff 65 20 26 20 4a 54 5f 4e 41 54 55 52 41 4c 29 21  e & JT_NATURAL)!
26000 3d 30 20 26 26 0a 20 20 20 20 20 20 20 20 20 20  =0 &&.          
26001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
26002 6c 75 6d 6e 49 6e 64 65 78 28 70 4c 65 66 74 2d  lumnIndex(pLeft-
26003 3e 70 54 61 62 2c 20 7a 4e 61 6d 65 29 3e 3d 30  >pTab, zName)>=0
26004 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
26005 20 20 20 20 2f 2a 20 49 6e 20 61 20 4e 41 54 55      /* In a NATU
26006 52 41 4c 20 6a 6f 69 6e 2c 20 6f 6d 69 74 20 74  RAL join, omit t
26007 68 65 20 6a 6f 69 6e 20 63 6f 6c 75 6d 6e 73 20  he join columns 
26008 66 72 6f 6d 20 74 68 65 20 0a 20 20 20 20 20 20  from the .      
26009 20 20 20 20 20 20 20 20 20 20 2a 2a 20 74 61 62            ** tab
2600a 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  le on the right 
2600b 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  */.             
2600c 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2600d 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20             }.   
2600e 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 73             if( s
2600f 71 6c 69 74 65 33 49 64 4c 69 73 74 49 6e 64 65  qlite3IdListInde
26010 78 28 70 4c 65 66 74 5b 31 5d 2e 70 55 73 69 6e  x(pLeft[1].pUsin
26011 67 2c 20 7a 4e 61 6d 65 29 3e 3d 30 20 29 7b 0a  g, zName)>=0 ){.
26012 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26013 2f 2a 20 49 6e 20 61 20 6a 6f 69 6e 20 77 69 74  /* In a join wit
26014 68 20 61 20 55 53 49 4e 47 20 63 6c 61 75 73 65  h a USING clause
26015 2c 20 6f 6d 69 74 20 63 6f 6c 75 6d 6e 73 20 69  , omit columns i
26016 6e 20 74 68 65 0a 20 20 20 20 20 20 20 20 20 20  n the.          
26017 20 20 20 20 20 20 2a 2a 20 75 73 69 6e 67 20 63        ** using c
26018 6c 61 75 73 65 20 66 72 6f 6d 20 74 68 65 20 74  lause from the t
26019 61 62 6c 65 20 6f 6e 20 74 68 65 20 72 69 67 68  able on the righ
2601a 74 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20  t. */.          
2601b 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2601c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
2601d 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20              }.  
2601e 20 20 20 20 20 20 20 20 20 20 70 52 69 67 68 74            pRight
2601f 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
26020 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30  pParse, TK_ID, 0
26021 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  , 0, 0);.       
26022 20 20 20 20 20 69 66 28 20 70 52 69 67 68 74 3d       if( pRight=
26023 3d 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  =0 ) break;.    
26024 20 20 20 20 20 20 20 20 73 65 74 51 75 6f 74 65          setQuote
26025 64 54 6f 6b 65 6e 28 70 50 61 72 73 65 2c 20 26  dToken(pParse, &
26026 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 2c 20 7a  pRight->token, z
26027 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 20  Name);.         
26028 20 20 20 69 66 28 20 6c 6f 6e 67 4e 61 6d 65 73     if( longNames
26029 20 7c 7c 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53   || pTabList->nS
2602a 72 63 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20  rc>1 ){.        
2602b 20 20 20 20 20 20 45 78 70 72 20 2a 70 4c 65 66        Expr *pLef
2602c 74 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  t = sqlite3PExpr
2602d 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20  (pParse, TK_ID, 
2602e 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 20 20  0, 0, 0);.      
2602f 20 20 20 20 20 20 20 20 70 45 78 70 72 20 3d 20          pExpr = 
26030 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
26031 72 73 65 2c 20 54 4b 5f 44 4f 54 2c 20 70 4c 65  rse, TK_DOT, pLe
26032 66 74 2c 20 70 52 69 67 68 74 2c 20 30 29 3b 0a  ft, pRight, 0);.
26033 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 66                if
26034 28 20 70 45 78 70 72 3d 3d 30 20 29 20 62 72 65  ( pExpr==0 ) bre
26035 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  ak;.            
26036 20 20 73 65 74 51 75 6f 74 65 64 54 6f 6b 65 6e    setQuotedToken
26037 28 70 50 61 72 73 65 2c 20 26 70 4c 65 66 74 2d  (pParse, &pLeft-
26038 3e 74 6f 6b 65 6e 2c 20 7a 54 61 62 4e 61 6d 65  >token, zTabName
26039 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2603a 20 73 65 74 54 6f 6b 65 6e 28 26 70 45 78 70 72   setToken(&pExpr
2603b 2d 3e 73 70 61 6e 2c 20 0a 20 20 20 20 20 20 20  ->span, .       
2603c 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2603d 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 25  e3MPrintf(db, "%
2603e 73 2e 25 73 22 2c 20 7a 54 61 62 4e 61 6d 65 2c  s.%s", zTabName,
2603f 20 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 20 20   zName));.      
26040 20 20 20 20 20 20 20 20 70 45 78 70 72 2d 3e 73          pExpr->s
26041 70 61 6e 2e 64 79 6e 20 3d 20 31 3b 0a 20 20 20  pan.dyn = 1;.   
26042 20 20 20 20 20 20 20 20 20 20 20 70 45 78 70 72             pExpr
26043 2d 3e 74 6f 6b 65 6e 2e 7a 20 3d 20 30 3b 0a 20  ->token.z = 0;. 
26044 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45 78               pEx
26045 70 72 2d 3e 74 6f 6b 65 6e 2e 6e 20 3d 20 30 3b  pr->token.n = 0;
26046 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70  .              p
26047 45 78 70 72 2d 3e 74 6f 6b 65 6e 2e 64 79 6e 20  Expr->token.dyn 
26048 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20  = 0;.           
26049 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2604a 20 20 20 20 20 20 70 45 78 70 72 20 3d 20 70 52        pExpr = pR
2604b 69 67 68 74 3b 0a 20 20 20 20 20 20 20 20 20 20  ight;.          
2604c 20 20 20 20 70 45 78 70 72 2d 3e 73 70 61 6e 20      pExpr->span 
2604d 3d 20 70 45 78 70 72 2d 3e 74 6f 6b 65 6e 3b 0a  = pExpr->token;.
2604e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 45                pE
2604f 78 70 72 2d 3e 73 70 61 6e 2e 64 79 6e 20 3d 20  xpr->span.dyn = 
26050 30 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d  0;.            }
26051 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 66 28  .            if(
26052 20 6c 6f 6e 67 4e 61 6d 65 73 20 29 7b 0a 20 20   longNames ){.  
26053 20 20 20 20 20 20 20 20 20 20 20 20 70 4e 65 77              pNew
26054 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
26055 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
26056 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20 26 70   pNew, pExpr, &p
26057 45 78 70 72 2d 3e 73 70 61 6e 29 3b 0a 20 20 20  Expr->span);.   
26058 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a           }else{.
26059 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 4e                pN
2605a 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  ew = sqlite3Expr
2605b 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2605c 65 2c 20 70 4e 65 77 2c 20 70 45 78 70 72 2c 20  e, pNew, pExpr, 
2605d 26 70 52 69 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b  &pRight->token);
2605e 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2605f 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20           }.     
26060 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
26061 20 21 74 61 62 6c 65 53 65 65 6e 20 29 7b 0a 20   !tableSeen ){. 
26062 20 20 20 20 20 20 20 20 20 69 66 28 20 7a 54 4e           if( zTN
26063 61 6d 65 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ame ){.         
26064 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d     sqlite3ErrorM
26065 73 67 28 70 50 61 72 73 65 2c 20 22 6e 6f 20 73  sg(pParse, "no s
26066 75 63 68 20 74 61 62 6c 65 3a 20 25 73 22 2c 20  uch table: %s", 
26067 7a 54 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20 20  zTName);.       
26068 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
26069 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
2606a 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
2606b 6f 20 74 61 62 6c 65 73 20 73 70 65 63 69 66 69  o tables specifi
2606c 65 64 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ed");.          
2606d 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
2606e 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2606f 65 28 64 62 2c 20 7a 54 4e 61 6d 65 29 3b 0a 20  e(db, zTName);. 
26070 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
26071 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
26072 44 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73  Delete(db, pELis
26073 74 29 3b 0a 20 20 20 20 70 2d 3e 70 45 4c 69 73  t);.    p->pELis
26074 74 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 23 69  t = pNew;.  }.#i
26075 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 43 4f 4c  f SQLITE_MAX_COL
26076 55 4d 4e 0a 20 20 69 66 28 20 70 2d 3e 70 45 4c  UMN.  if( p->pEL
26077 69 73 74 20 26 26 20 70 2d 3e 70 45 4c 69 73 74  ist && p->pEList
26078 2d 3e 6e 45 78 70 72 3e 64 62 2d 3e 61 4c 69 6d  ->nExpr>db->aLim
26079 69 74 5b 53 51 4c 49 54 45 5f 4c 49 4d 49 54 5f  it[SQLITE_LIMIT_
2607a 43 4f 4c 55 4d 4e 5d 20 29 7b 0a 20 20 20 20 73  COLUMN] ){.    s
2607b 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2607c 50 61 72 73 65 2c 20 22 74 6f 6f 20 6d 61 6e 79  Parse, "too many
2607d 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 72 65 73 75   columns in resu
2607e 6c 74 20 73 65 74 22 29 3b 0a 20 20 7d 0a 23 65  lt set");.  }.#e
2607f 6e 64 69 66 0a 20 20 72 65 74 75 72 6e 20 57 52  ndif.  return WR
26080 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
26081 2a 0a 2a 2a 20 4e 6f 2d 6f 70 20 72 6f 75 74 69  *.** No-op routi
26082 6e 65 20 66 6f 72 20 74 68 65 20 70 61 72 73 65  ne for the parse
26083 2d 74 72 65 65 20 77 61 6c 6b 65 72 2e 0a 2a 2a  -tree walker..**
26084 0a 2a 2a 20 57 68 65 6e 20 74 68 69 73 20 72 6f  .** When this ro
26085 75 74 69 6e 65 20 69 73 20 74 68 65 20 57 61 6c  utine is the Wal
26086 6b 65 72 2e 78 45 78 70 72 43 61 6c 6c 62 61 63  ker.xExprCallbac
26087 6b 20 74 68 65 6e 20 65 78 70 72 65 73 73 69 6f  k then expressio
26088 6e 20 74 72 65 65 73 0a 2a 2a 20 61 72 65 20 77  n trees.** are w
26089 61 6c 6b 65 64 20 77 69 74 68 6f 75 74 20 61 6e  alked without an
2608a 79 20 61 63 74 69 6f 6e 73 20 62 65 69 6e 67 20  y actions being 
2608b 74 61 6b 65 6e 20 61 74 20 65 61 63 68 20 6e 6f  taken at each no
2608c 64 65 2e 20 20 50 72 65 73 75 6d 61 62 6c 79 2c  de.  Presumably,
2608d 0a 2a 2a 20 77 68 65 6e 20 74 68 69 73 20 72 6f  .** when this ro
2608e 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 66 6f  utine is used fo
2608f 72 20 57 61 6c 6b 65 72 2e 78 45 78 70 72 43 61  r Walker.xExprCa
26090 6c 6c 62 61 63 6b 20 74 68 65 6e 20 0a 2a 2a 20  llback then .** 
26091 57 61 6c 6b 65 72 2e 78 53 65 6c 65 63 74 43 61  Walker.xSelectCa
26092 6c 6c 62 61 63 6b 20 69 73 20 73 65 74 20 74 6f  llback is set to
26093 20 64 6f 20 73 6f 6d 65 74 68 69 6e 67 20 75 73   do something us
26094 65 66 75 6c 20 66 6f 72 20 65 76 65 72 79 20 0a  eful for every .
26095 2a 2a 20 73 75 62 71 75 65 72 79 20 69 6e 20 74  ** subquery in t
26096 68 65 20 70 61 72 73 65 72 20 74 72 65 65 2e 0a  he parser tree..
26097 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 65 78  */.static int ex
26098 70 72 57 61 6c 6b 4e 6f 6f 70 28 57 61 6c 6b 65  prWalkNoop(Walke
26099 72 20 2a 4e 6f 74 55 73 65 64 2c 20 45 78 70 72  r *NotUsed, Expr
2609a 20 2a 4e 6f 74 55 73 65 64 32 29 7b 0a 20 20 55   *NotUsed2){.  U
2609b 4e 55 53 45 44 5f 50 41 52 41 4d 45 54 45 52 32  NUSED_PARAMETER2
2609c 28 4e 6f 74 55 73 65 64 2c 20 4e 6f 74 55 73 65  (NotUsed, NotUse
2609d 64 32 29 3b 0a 20 20 72 65 74 75 72 6e 20 57 52  d2);.  return WR
2609e 43 5f 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 0a 2f  C_Continue;.}../
2609f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
260a0 65 20 22 65 78 70 61 6e 64 73 22 20 61 20 53 45  e "expands" a SE
260a1 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 61  LECT statement a
260a2 6e 64 20 61 6c 6c 20 6f 66 20 69 74 73 20 73 75  nd all of its su
260a3 62 71 75 65 72 69 65 73 2e 0a 2a 2a 20 46 6f 72  bqueries..** For
260a4 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f   additional info
260a5 72 6d 61 74 69 6f 6e 20 6f 6e 20 77 68 61 74 20  rmation on what 
260a6 69 74 20 6d 65 61 6e 73 20 74 6f 20 22 65 78 70  it means to "exp
260a7 61 6e 64 22 20 61 20 53 45 4c 45 43 54 0a 2a 2a  and" a SELECT.**
260a8 20 73 74 61 74 65 6d 65 6e 74 2c 20 73 65 65 20   statement, see 
260a9 74 68 65 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74  the comment on t
260aa 68 65 20 73 65 6c 65 63 74 45 78 70 61 6e 64 20  he selectExpand 
260ab 77 6f 72 6b 65 72 20 63 61 6c 6c 62 61 63 6b 20  worker callback 
260ac 61 62 6f 76 65 2e 0a 2a 2a 0a 2a 2a 20 45 78 70  above..**.** Exp
260ad 61 6e 64 69 6e 67 20 61 20 53 45 4c 45 43 54 20  anding a SELECT 
260ae 73 74 61 74 65 6d 65 6e 74 20 69 73 20 74 68 65  statement is the
260af 20 66 69 72 73 74 20 73 74 65 70 20 69 6e 20 70   first step in p
260b0 72 6f 63 65 73 73 69 6e 67 20 61 0a 2a 2a 20 53  rocessing a.** S
260b1 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e  ELECT statement.
260b2 20 20 54 68 65 20 53 45 4c 45 43 54 20 73 74 61    The SELECT sta
260b3 74 65 6d 65 6e 74 20 6d 75 73 74 20 62 65 20 65  tement must be e
260b4 78 70 61 6e 64 65 64 20 62 65 66 6f 72 65 0a 2a  xpanded before.*
260b5 2a 20 6e 61 6d 65 20 72 65 73 6f 6c 75 74 69 6f  * name resolutio
260b6 6e 20 69 73 20 70 65 72 66 6f 72 6d 65 64 2e 0a  n is performed..
260b7 2a 2a 0a 2a 2a 20 49 66 20 61 6e 79 74 68 69 6e  **.** If anythin
260b8 67 20 67 6f 65 73 20 77 72 6f 6e 67 2c 20 61 6e  g goes wrong, an
260b9 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69   error message i
260ba 73 20 77 72 69 74 74 65 6e 20 69 6e 74 6f 20 70  s written into p
260bb 50 61 72 73 65 2e 0a 2a 2a 20 54 68 65 20 63 61  Parse..** The ca
260bc 6c 6c 69 6e 67 20 66 75 6e 63 74 69 6f 6e 20 63  lling function c
260bd 61 6e 20 64 65 74 65 63 74 20 74 68 65 20 70 72  an detect the pr
260be 6f 62 6c 65 6d 20 62 79 20 6c 6f 6f 6b 69 6e 67  oblem by looking
260bf 20 61 74 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   at pParse->nErr
260c0 0a 2a 2a 20 61 6e 64 2f 6f 72 20 70 50 61 72 73  .** and/or pPars
260c1 65 2d 3e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  e->db->mallocFai
260c2 6c 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  led..*/.static v
260c3 6f 69 64 20 73 71 6c 69 74 65 33 53 65 6c 65 63  oid sqlite3Selec
260c4 74 45 78 70 61 6e 64 28 50 61 72 73 65 20 2a 70  tExpand(Parse *p
260c5 50 61 72 73 65 2c 20 53 65 6c 65 63 74 20 2a 70  Parse, Select *p
260c6 53 65 6c 65 63 74 29 7b 0a 20 20 57 61 6c 6b 65  Select){.  Walke
260c7 72 20 77 3b 0a 20 20 77 2e 78 53 65 6c 65 63 74  r w;.  w.xSelect
260c8 43 61 6c 6c 62 61 63 6b 20 3d 20 73 65 6c 65 63  Callback = selec
260c9 74 45 78 70 61 6e 64 65 72 3b 0a 20 20 77 2e 78  tExpander;.  w.x
260ca 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d 20 65  ExprCallback = e
260cb 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20 20 77  xprWalkNoop;.  w
260cc 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
260cd 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c 6b 53  ;.  sqlite3WalkS
260ce 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c 65 63  elect(&w, pSelec
260cf 74 29 3b 0a 7d 0a 0a 0a 23 69 66 6e 64 65 66 20  t);.}...#ifndef 
260d0 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42 51  SQLITE_OMIT_SUBQ
260d1 55 45 52 59 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  UERY./*.** This 
260d2 69 73 20 61 20 57 61 6c 6b 65 72 2e 78 53 65 6c  is a Walker.xSel
260d3 65 63 74 43 61 6c 6c 62 61 63 6b 20 63 61 6c 6c  ectCallback call
260d4 62 61 63 6b 20 66 6f 72 20 74 68 65 20 73 71 6c  back for the sql
260d5 69 74 65 33 53 65 6c 65 63 74 54 79 70 65 49 6e  ite3SelectTypeIn
260d6 66 6f 28 29 0a 2a 2a 20 69 6e 74 65 72 66 61 63  fo().** interfac
260d7 65 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 61 63  e..**.** For eac
260d8 68 20 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75  h FROM-clause su
260d9 62 71 75 65 72 79 2c 20 61 64 64 20 43 6f 6c 75  bquery, add Colu
260da 6d 6e 2e 7a 54 79 70 65 20 61 6e 64 20 43 6f 6c  mn.zType and Col
260db 75 6d 6e 2e 7a 43 6f 6c 6c 0a 2a 2a 20 69 6e 66  umn.zColl.** inf
260dc 6f 72 6d 61 74 69 6f 6e 20 74 6f 20 74 68 65 20  ormation to the 
260dd 54 61 62 6c 65 20 73 74 72 75 63 74 75 72 65 20  Table structure 
260de 74 68 61 74 20 72 65 70 72 65 73 65 6e 74 73 20  that represents 
260df 74 68 65 20 72 65 73 75 6c 74 20 73 65 74 0a 2a  the result set.*
260e0 2a 20 6f 66 20 74 68 61 74 20 73 75 62 71 75 65  * of that subque
260e1 72 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 54 61  ry..**.** The Ta
260e2 62 6c 65 20 73 74 72 75 63 74 75 72 65 20 74 68  ble structure th
260e3 61 74 20 72 65 70 72 65 73 65 6e 74 73 20 74 68  at represents th
260e4 65 20 72 65 73 75 6c 74 20 73 65 74 20 77 61 73  e result set was
260e5 20 63 6f 6e 73 74 72 75 63 74 65 64 0a 2a 2a 20   constructed.** 
260e6 62 79 20 73 65 6c 65 63 74 45 78 70 61 6e 64 65  by selectExpande
260e7 72 28 29 20 62 75 74 20 74 68 65 20 74 79 70 65  r() but the type
260e8 20 61 6e 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 69   and collation i
260e9 6e 66 6f 72 6d 61 74 69 6f 6e 20 77 61 73 20 6f  nformation was o
260ea 6d 69 74 74 65 64 0a 2a 2a 20 61 74 20 74 68 61  mitted.** at tha
260eb 74 20 70 6f 69 6e 74 20 62 65 63 61 75 73 65 20  t point because 
260ec 69 64 65 6e 74 69 66 69 65 72 73 20 68 61 64 20  identifiers had 
260ed 6e 6f 74 20 79 65 74 20 62 65 65 6e 20 72 65 73  not yet been res
260ee 6f 6c 76 65 64 2e 20 20 54 68 69 73 0a 2a 2a 20  olved.  This.** 
260ef 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c 65  routine is calle
260f0 64 20 61 66 74 65 72 20 69 64 65 6e 74 69 66 69  d after identifi
260f1 65 72 20 72 65 73 6f 6c 75 74 69 6f 6e 2e 0a 2a  er resolution..*
260f2 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65 6c  /.static int sel
260f3 65 63 74 41 64 64 53 75 62 71 75 65 72 79 54 79  ectAddSubqueryTy
260f4 70 65 49 6e 66 6f 28 57 61 6c 6b 65 72 20 2a 70  peInfo(Walker *p
260f5 57 61 6c 6b 65 72 2c 20 53 65 6c 65 63 74 20 2a  Walker, Select *
260f6 70 29 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61  p){.  Parse *pPa
260f7 72 73 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20  rse;.  int i;.  
260f8 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
260f9 74 3b 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c  t;.  struct SrcL
260fa 69 73 74 5f 69 74 65 6d 20 2a 70 46 72 6f 6d 3b  ist_item *pFrom;
260fb 0a 0a 20 20 61 73 73 65 72 74 28 20 70 2d 3e 73  ..  assert( p->s
260fc 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 52 65 73  elFlags & SF_Res
260fd 6f 6c 76 65 64 20 29 3b 0a 20 20 69 66 28 20 28  olved );.  if( (
260fe 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46  p->selFlags & SF
260ff 5f 48 61 73 54 79 70 65 49 6e 66 6f 29 3d 3d 30  _HasTypeInfo)==0
26100 20 29 7b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c   ){.    p->selFl
26101 61 67 73 20 7c 3d 20 53 46 5f 48 61 73 54 79 70  ags |= SF_HasTyp
26102 65 49 6e 66 6f 3b 0a 20 20 20 20 70 50 61 72 73  eInfo;.    pPars
26103 65 20 3d 20 70 57 61 6c 6b 65 72 2d 3e 70 50 61  e = pWalker->pPa
26104 72 73 65 3b 0a 20 20 20 20 70 54 61 62 4c 69 73  rse;.    pTabLis
26105 74 20 3d 20 70 2d 3e 70 53 72 63 3b 0a 20 20 20  t = p->pSrc;.   
26106 20 66 6f 72 28 69 3d 30 2c 20 70 46 72 6f 6d 3d   for(i=0, pFrom=
26107 70 54 61 62 4c 69 73 74 2d 3e 61 3b 20 69 3c 70  pTabList->a; i<p
26108 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
26109 2b 2b 2c 20 70 46 72 6f 6d 2b 2b 29 7b 0a 20 20  ++, pFrom++){.  
2610a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 20      Table *pTab 
2610b 3d 20 70 46 72 6f 6d 2d 3e 70 54 61 62 3b 0a 20  = pFrom->pTab;. 
2610c 20 20 20 20 20 69 66 28 20 70 54 61 62 20 26 26       if( pTab &&
2610d 20 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73   (pTab->tabFlags
2610e 20 26 20 54 46 5f 45 70 68 65 6d 65 72 61 6c 29   & TF_Ephemeral)
2610f 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  !=0 ){.        /
26110 2a 20 41 20 73 75 62 2d 71 75 65 72 79 20 69 6e  * A sub-query in
26111 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
26112 20 6f 66 20 61 20 53 45 4c 45 43 54 20 2a 2f 0a   of a SELECT */.
26113 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a          Select *
26114 70 53 65 6c 20 3d 20 70 46 72 6f 6d 2d 3e 70 53  pSel = pFrom->pS
26115 65 6c 65 63 74 3b 0a 20 20 20 20 20 20 20 20 61  elect;.        a
26116 73 73 65 72 74 28 20 70 53 65 6c 20 29 3b 0a 20  ssert( pSel );. 
26117 20 20 20 20 20 20 20 77 68 69 6c 65 28 20 70 53         while( pS
26118 65 6c 2d 3e 70 50 72 69 6f 72 20 29 20 70 53 65  el->pPrior ) pSe
26119 6c 20 3d 20 70 53 65 6c 2d 3e 70 50 72 69 6f 72  l = pSel->pPrior
2611a 3b 0a 20 20 20 20 20 20 20 20 73 65 6c 65 63 74  ;.        select
2611b 41 64 64 43 6f 6c 75 6d 6e 54 79 70 65 41 6e 64  AddColumnTypeAnd
2611c 43 6f 6c 6c 61 74 69 6f 6e 28 70 50 61 72 73 65  Collation(pParse
2611d 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2c 20 70 54  , pTab->nCol, pT
2611e 61 62 2d 3e 61 43 6f 6c 2c 20 70 53 65 6c 29 3b  ab->aCol, pSel);
2611f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
26120 20 7d 0a 20 20 72 65 74 75 72 6e 20 57 52 43 5f   }.  return WRC_
26121 43 6f 6e 74 69 6e 75 65 3b 0a 7d 0a 23 65 6e 64  Continue;.}.#end
26122 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20  if.../*.** This 
26123 72 6f 75 74 69 6e 65 20 61 64 64 73 20 64 61 74  routine adds dat
26124 61 74 79 70 65 20 61 6e 64 20 63 6f 6c 6c 61 74  atype and collat
26125 69 6e 67 20 73 65 71 75 65 6e 63 65 20 69 6e 66  ing sequence inf
26126 6f 72 6d 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 74  ormation to.** t
26127 68 65 20 54 61 62 6c 65 20 73 74 72 75 63 74 75  he Table structu
26128 72 65 73 20 6f 66 20 61 6c 6c 20 46 52 4f 4d 2d  res of all FROM-
26129 63 6c 61 75 73 65 20 73 75 62 71 75 65 72 69 65  clause subquerie
2612a 73 20 69 6e 20 61 0a 2a 2a 20 53 45 4c 45 43 54  s in a.** SELECT
2612b 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a   statement..**.*
2612c 2a 20 55 73 65 20 74 68 69 73 20 72 6f 75 74 69  * Use this routi
2612d 6e 65 20 61 66 74 65 72 20 6e 61 6d 65 20 72 65  ne after name re
2612e 73 6f 6c 75 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61  solution..*/.sta
2612f 74 69 63 20 76 6f 69 64 20 73 71 6c 69 74 65 33  tic void sqlite3
26130 53 65 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66  SelectAddTypeInf
26131 6f 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  o(Parse *pParse,
26132 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74   Select *pSelect
26133 29 7b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ){.#ifndef SQLIT
26134 45 5f 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 0a  E_OMIT_SUBQUERY.
26135 20 20 57 61 6c 6b 65 72 20 77 3b 0a 20 20 77 2e    Walker w;.  w.
26136 78 53 65 6c 65 63 74 43 61 6c 6c 62 61 63 6b 20  xSelectCallback 
26137 3d 20 73 65 6c 65 63 74 41 64 64 53 75 62 71 75  = selectAddSubqu
26138 65 72 79 54 79 70 65 49 6e 66 6f 3b 0a 20 20 77  eryTypeInfo;.  w
26139 2e 78 45 78 70 72 43 61 6c 6c 62 61 63 6b 20 3d  .xExprCallback =
2613a 20 65 78 70 72 57 61 6c 6b 4e 6f 6f 70 3b 0a 20   exprWalkNoop;. 
2613b 20 77 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72   w.pParse = pPar
2613c 73 65 3b 0a 20 20 73 71 6c 69 74 65 33 57 61 6c  se;.  sqlite3Wal
2613d 6b 53 65 6c 65 63 74 28 26 77 2c 20 70 53 65 6c  kSelect(&w, pSel
2613e 65 63 74 29 3b 0a 23 65 6e 64 69 66 0a 7d 0a 0a  ect);.#endif.}..
2613f 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74  ./*.** This rout
26140 69 6e 65 20 73 65 74 73 20 6f 66 20 61 20 53 45  ine sets of a SE
26141 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 66  LECT statement f
26142 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 20 20  or processing.  
26143 54 68 65 0a 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67  The.** following
26144 20 69 73 20 61 63 63 6f 6d 70 6c 69 73 68 65 64   is accomplished
26145 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 20 56  :.**.**     *  V
26146 44 42 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65  DBE Cursor numbe
26147 72 73 20 61 72 65 20 61 73 73 69 67 6e 65 64 20  rs are assigned 
26148 74 6f 20 61 6c 6c 20 46 52 4f 4d 2d 63 6c 61 75  to all FROM-clau
26149 73 65 20 74 65 72 6d 73 2e 0a 2a 2a 20 20 20 20  se terms..**    
2614a 20 2a 20 20 45 70 68 65 6d 65 72 61 6c 20 54 61   *  Ephemeral Ta
2614b 62 6c 65 20 6f 62 6a 65 63 74 73 20 61 72 65 20  ble objects are 
2614c 63 72 65 61 74 65 64 20 66 6f 72 20 61 6c 6c 20  created for all 
2614d 46 52 4f 4d 2d 63 6c 61 75 73 65 20 73 75 62 71  FROM-clause subq
2614e 75 65 72 69 65 73 2e 0a 2a 2a 20 20 20 20 20 2a  ueries..**     *
2614f 20 20 4f 4e 20 61 6e 64 20 55 53 49 4e 47 20 63    ON and USING c
26150 6c 61 75 73 65 73 20 61 72 65 20 73 68 69 66 74  lauses are shift
26151 65 64 20 69 6e 74 6f 20 57 48 45 52 45 20 73 74  ed into WHERE st
26152 61 74 65 6d 65 6e 74 73 0a 2a 2a 20 20 20 20 20  atements.**     
26153 2a 20 20 57 69 6c 64 63 61 72 64 73 20 22 2a 22  *  Wildcards "*"
26154 20 61 6e 64 20 22 54 41 42 4c 45 2e 2a 22 20 69   and "TABLE.*" i
26155 6e 20 72 65 73 75 6c 74 20 73 65 74 73 20 61 72  n result sets ar
26156 65 20 65 78 70 61 6e 64 65 64 2e 0a 2a 2a 20 20  e expanded..**  
26157 20 20 20 2a 20 20 49 64 65 6e 74 69 66 69 65 72     *  Identifier
26158 73 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 20  s in expression 
26159 61 72 65 20 6d 61 74 63 68 65 64 20 74 6f 20 74  are matched to t
2615a 61 62 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ables..**.** Thi
2615b 73 20 72 6f 75 74 69 6e 65 20 61 63 74 73 20 72  s routine acts r
2615c 65 63 75 72 73 69 76 65 6c 79 20 6f 6e 20 61 6c  ecursively on al
2615d 6c 20 73 75 62 71 75 65 72 69 65 73 20 77 69 74  l subqueries wit
2615e 68 69 6e 20 74 68 65 20 53 45 4c 45 43 54 2e 0a  hin the SELECT..
2615f 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
26160 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 53 65  E void sqlite3Se
26161 6c 65 63 74 50 72 65 70 28 0a 20 20 50 61 72 73  lectPrep(.  Pars
26162 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
26163 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
26164 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 65   context */.  Se
26165 6c 65 63 74 20 2a 70 2c 20 20 20 20 20 20 20 20  lect *p,        
26166 20 20 20 20 20 2f 2a 20 54 68 65 20 53 45 4c 45       /* The SELE
26167 43 54 20 73 74 61 74 65 6d 65 6e 74 20 62 65 69  CT statement bei
26168 6e 67 20 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 4e  ng coded. */.  N
26169 61 6d 65 43 6f 6e 74 65 78 74 20 2a 70 4f 75 74  ameContext *pOut
2616a 65 72 4e 43 20 20 2f 2a 20 4e 61 6d 65 20 63 6f  erNC  /* Name co
2616b 6e 74 65 78 74 20 66 6f 72 20 63 6f 6e 74 61 69  ntext for contai
2616c 6e 65 72 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69  ner */.){.  sqli
2616d 74 65 33 20 2a 64 62 3b 0a 20 20 69 66 28 20 70  te3 *db;.  if( p
2616e 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
2616f 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
26170 0a 20 20 69 66 28 20 70 2d 3e 73 65 6c 46 6c 61  .  if( p->selFla
26171 67 73 20 26 20 53 46 5f 48 61 73 54 79 70 65 49  gs & SF_HasTypeI
26172 6e 66 6f 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  nfo ) return;.  
26173 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45 72 72  if( pParse->nErr
26174 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
26175 69 6c 65 64 20 29 20 72 65 74 75 72 6e 3b 0a 20  iled ) return;. 
26176 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 45 78   sqlite3SelectEx
26177 70 61 6e 64 28 70 50 61 72 73 65 2c 20 70 29 3b  pand(pParse, p);
26178 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
26179 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
2617a 63 46 61 69 6c 65 64 20 29 20 72 65 74 75 72 6e  cFailed ) return
2617b 3b 0a 20 20 73 71 6c 69 74 65 33 52 65 73 6f 6c  ;.  sqlite3Resol
2617c 76 65 53 65 6c 65 63 74 4e 61 6d 65 73 28 70 50  veSelectNames(pP
2617d 61 72 73 65 2c 20 70 2c 20 70 4f 75 74 65 72 4e  arse, p, pOuterN
2617e 43 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73 65  C);.  if( pParse
2617f 2d 3e 6e 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61  ->nErr || db->ma
26180 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 72 65 74  llocFailed ) ret
26181 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33 53 65  urn;.  sqlite3Se
26182 6c 65 63 74 41 64 64 54 79 70 65 49 6e 66 6f 28  lectAddTypeInfo(
26183 70 50 61 72 73 65 2c 20 70 29 3b 0a 7d 0a 0a 2f  pParse, p);.}../
26184 2a 0a 2a 2a 20 52 65 73 65 74 20 74 68 65 20 61  *.** Reset the a
26185 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c  ggregate accumul
26186 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ator..**.** The 
26187 61 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75  aggregate accumu
26188 6c 61 74 6f 72 20 69 73 20 61 20 73 65 74 20 6f  lator is a set o
26189 66 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73 20 74  f memory cells t
2618a 68 61 74 20 68 6f 6c 64 0a 2a 2a 20 69 6e 74 65  hat hold.** inte
2618b 72 6d 65 64 69 61 74 65 20 72 65 73 75 6c 74 73  rmediate results
2618c 20 77 68 69 6c 65 20 63 61 6c 63 75 6c 61 74 69   while calculati
2618d 6e 67 20 61 6e 20 61 67 67 72 65 67 61 74 65 2e  ng an aggregate.
2618e 20 20 54 68 69 73 0a 2a 2a 20 72 6f 75 74 69 6e    This.** routin
2618f 65 20 73 69 6d 70 6c 79 20 73 74 6f 72 65 73 20  e simply stores 
26190 4e 55 4c 4c 73 20 69 6e 20 61 6c 6c 20 6f 66 20  NULLs in all of 
26191 74 68 6f 73 65 20 6d 65 6d 6f 72 79 20 63 65 6c  those memory cel
26192 6c 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ls..*/.static vo
26193 69 64 20 72 65 73 65 74 41 63 63 75 6d 75 6c 61  id resetAccumula
26194 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61 72 73  tor(Parse *pPars
26195 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41 67 67  e, AggInfo *pAgg
26196 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20 2a 76  Info){.  Vdbe *v
26197 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65   = pParse->pVdbe
26198 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73 74 72  ;.  int i;.  str
26199 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75 6e 63  uct AggInfo_func
2619a 20 2a 70 46 75 6e 63 3b 0a 20 20 69 66 28 20 70   *pFunc;.  if( p
2619b 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 2b 70  AggInfo->nFunc+p
2619c 41 67 67 49 6e 66 6f 2d 3e 6e 43 6f 6c 75 6d 6e  AggInfo->nColumn
2619d 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ==0 ){.    retur
2619e 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d 30  n;.  }.  for(i=0
2619f 3b 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 43  ; i<pAggInfo->nC
261a0 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20  olumn; i++){.   
261a1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
261a2 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
261a3 2c 20 70 41 67 67 49 6e 66 6f 2d 3e 61 43 6f 6c  , pAggInfo->aCol
261a4 5b 69 5d 2e 69 4d 65 6d 29 3b 0a 20 20 7d 0a 20  [i].iMem);.  }. 
261a5 20 66 6f 72 28 70 46 75 6e 63 3d 70 41 67 67 49   for(pFunc=pAggI
261a6 6e 66 6f 2d 3e 61 46 75 6e 63 2c 20 69 3d 30 3b  nfo->aFunc, i=0;
261a7 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
261a8 6e 63 3b 20 69 2b 2b 2c 20 70 46 75 6e 63 2b 2b  nc; i++, pFunc++
261a9 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
261aa 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4e  beAddOp2(v, OP_N
261ab 75 6c 6c 2c 20 30 2c 20 70 46 75 6e 63 2d 3e 69  ull, 0, pFunc->i
261ac 4d 65 6d 29 3b 0a 20 20 20 20 69 66 28 20 70 46  Mem);.    if( pF
261ad 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 3e 3d  unc->iDistinct>=
261ae 30 20 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  0 ){.      Expr 
261af 2a 70 45 20 3d 20 70 46 75 6e 63 2d 3e 70 45 78  *pE = pFunc->pEx
261b0 70 72 3b 0a 20 20 20 20 20 20 69 66 28 20 70 45  pr;.      if( pE
261b1 2d 3e 70 4c 69 73 74 3d 3d 30 20 7c 7c 20 70 45  ->pList==0 || pE
261b2 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70 72 21 3d  ->pList->nExpr!=
261b3 31 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  1 ){.        sql
261b4 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
261b5 72 73 65 2c 20 22 44 49 53 54 49 4e 43 54 20 69  rse, "DISTINCT i
261b6 6e 20 61 67 67 72 65 67 61 74 65 20 6d 75 73 74  n aggregate must
261b7 20 62 65 20 66 6f 6c 6c 6f 77 65 64 20 22 0a 20   be followed ". 
261b8 20 20 20 20 20 20 20 20 20 20 22 62 79 20 61 6e            "by an
261b9 20 65 78 70 72 65 73 73 69 6f 6e 22 29 3b 0a 20   expression");. 
261ba 20 20 20 20 20 20 20 70 46 75 6e 63 2d 3e 69 44         pFunc->iD
261bb 69 73 74 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20  istinct = -1;.  
261bc 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
261bd 20 20 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79     KeyInfo *pKey
261be 49 6e 66 6f 20 3d 20 6b 65 79 49 6e 66 6f 46 72  Info = keyInfoFr
261bf 6f 6d 45 78 70 72 4c 69 73 74 28 70 50 61 72 73  omExprList(pPars
261c0 65 2c 20 70 45 2d 3e 70 4c 69 73 74 29 3b 0a 20  e, pE->pList);. 
261c1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64         sqlite3Vd
261c2 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
261c3 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 46  penEphemeral, pF
261c4 75 6e 63 2d 3e 69 44 69 73 74 69 6e 63 74 2c 20  unc->iDistinct, 
261c5 30 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20  0, 0,.          
261c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
261c7 28 63 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c  (char*)pKeyInfo,
261c8 20 50 34 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44   P4_KEYINFO_HAND
261c9 4f 46 46 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  OFF);.      }.  
261ca 20 20 7d 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a    }.  }.}../*.**
261cb 20 49 6e 76 6f 6b 65 20 74 68 65 20 4f 50 5f 41   Invoke the OP_A
261cc 67 67 46 69 6e 61 6c 69 7a 65 20 6f 70 63 6f 64  ggFinalize opcod
261cd 65 20 66 6f 72 20 65 76 65 72 79 20 61 67 67 72  e for every aggr
261ce 65 67 61 74 65 20 66 75 6e 63 74 69 6f 6e 0a 2a  egate function.*
261cf 2a 20 69 6e 20 74 68 65 20 41 67 67 49 6e 66 6f  * in the AggInfo
261d0 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a 73   structure..*/.s
261d1 74 61 74 69 63 20 76 6f 69 64 20 66 69 6e 61 6c  tatic void final
261d2 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
261d3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 41  Parse *pParse, A
261d4 67 67 49 6e 66 6f 20 2a 70 41 67 67 49 6e 66 6f  ggInfo *pAggInfo
261d5 29 7b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  ){.  Vdbe *v = p
261d6 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20 20  Parse->pVdbe;.  
261d7 69 6e 74 20 69 3b 0a 20 20 73 74 72 75 63 74 20  int i;.  struct 
261d8 41 67 67 49 6e 66 6f 5f 66 75 6e 63 20 2a 70 46  AggInfo_func *pF
261d9 3b 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d  ;.  for(i=0, pF=
261da 70 41 67 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b  pAggInfo->aFunc;
261db 20 69 3c 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75   i<pAggInfo->nFu
261dc 6e 63 3b 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a  nc; i++, pF++){.
261dd 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
261de 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72 2d  ist = pF->pExpr-
261df 3e 70 4c 69 73 74 3b 0a 20 20 20 20 73 71 6c 69  >pList;.    sqli
261e0 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
261e1 20 4f 50 5f 41 67 67 46 69 6e 61 6c 2c 20 70 46   OP_AggFinal, pF
261e2 2d 3e 69 4d 65 6d 2c 20 70 4c 69 73 74 20 3f 20  ->iMem, pList ? 
261e3 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 3a 20 30  pList->nExpr : 0
261e4 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
261e5 20 20 20 20 20 20 20 20 20 20 20 28 76 6f 69 64             (void
261e6 2a 29 70 46 2d 3e 70 46 75 6e 63 2c 20 50 34 5f  *)pF->pFunc, P4_
261e7 46 55 4e 43 44 45 46 29 3b 0a 20 20 7d 0a 7d 0a  FUNCDEF);.  }.}.
261e8 0a 2f 2a 0a 2a 2a 20 55 70 64 61 74 65 20 74 68  ./*.** Update th
261e9 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 6d 65  e accumulator me
261ea 6d 6f 72 79 20 63 65 6c 6c 73 20 66 6f 72 20 61  mory cells for a
261eb 6e 20 61 67 67 72 65 67 61 74 65 20 62 61 73 65  n aggregate base
261ec 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 63 75 72 72  d on.** the curr
261ed 65 6e 74 20 63 75 72 73 6f 72 20 70 6f 73 69 74  ent cursor posit
261ee 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ion..*/.static v
261ef 6f 69 64 20 75 70 64 61 74 65 41 63 63 75 6d 75  oid updateAccumu
261f0 6c 61 74 6f 72 28 50 61 72 73 65 20 2a 70 50 61  lator(Parse *pPa
261f1 72 73 65 2c 20 41 67 67 49 6e 66 6f 20 2a 70 41  rse, AggInfo *pA
261f2 67 67 49 6e 66 6f 29 7b 0a 20 20 56 64 62 65 20  ggInfo){.  Vdbe 
261f3 2a 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64  *v = pParse->pVd
261f4 62 65 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 73  be;.  int i;.  s
261f5 74 72 75 63 74 20 41 67 67 49 6e 66 6f 5f 66 75  truct AggInfo_fu
261f6 6e 63 20 2a 70 46 3b 0a 20 20 73 74 72 75 63 74  nc *pF;.  struct
261f7 20 41 67 67 49 6e 66 6f 5f 63 6f 6c 20 2a 70 43   AggInfo_col *pC
261f8 3b 0a 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64  ;..  pAggInfo->d
261f9 69 72 65 63 74 4d 6f 64 65 20 3d 20 31 3b 0a 20  irectMode = 1;. 
261fa 20 66 6f 72 28 69 3d 30 2c 20 70 46 3d 70 41 67   for(i=0, pF=pAg
261fb 67 49 6e 66 6f 2d 3e 61 46 75 6e 63 3b 20 69 3c  gInfo->aFunc; i<
261fc 70 41 67 67 49 6e 66 6f 2d 3e 6e 46 75 6e 63 3b  pAggInfo->nFunc;
261fd 20 69 2b 2b 2c 20 70 46 2b 2b 29 7b 0a 20 20 20   i++, pF++){.   
261fe 20 69 6e 74 20 6e 41 72 67 3b 0a 20 20 20 20 69   int nArg;.    i
261ff 6e 74 20 61 64 64 72 4e 65 78 74 20 3d 20 30 3b  nt addrNext = 0;
26200 0a 20 20 20 20 69 6e 74 20 72 65 67 41 67 67 3b  .    int regAgg;
26201 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .    ExprList *p
26202 4c 69 73 74 20 3d 20 70 46 2d 3e 70 45 78 70 72  List = pF->pExpr
26203 2d 3e 70 4c 69 73 74 3b 0a 20 20 20 20 69 66 28  ->pList;.    if(
26204 20 70 4c 69 73 74 20 29 7b 0a 20 20 20 20 20 20   pList ){.      
26205 6e 41 72 67 20 3d 20 70 4c 69 73 74 2d 3e 6e 45  nArg = pList->nE
26206 78 70 72 3b 0a 20 20 20 20 20 20 72 65 67 41 67  xpr;.      regAg
26207 67 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54 65  g = sqlite3GetTe
26208 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c 20  mpRange(pParse, 
26209 6e 41 72 67 29 3b 0a 20 20 20 20 20 20 73 71 6c  nArg);.      sql
2620a 69 74 65 33 45 78 70 72 43 6f 64 65 45 78 70 72  ite3ExprCodeExpr
2620b 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 4c 69  List(pParse, pLi
2620c 73 74 2c 20 72 65 67 41 67 67 2c 20 30 29 3b 0a  st, regAgg, 0);.
2620d 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2620e 20 6e 41 72 67 20 3d 20 30 3b 0a 20 20 20 20 20   nArg = 0;.     
2620f 20 72 65 67 41 67 67 20 3d 20 30 3b 0a 20 20 20   regAgg = 0;.   
26210 20 7d 0a 20 20 20 20 69 66 28 20 70 46 2d 3e 69   }.    if( pF->i
26211 44 69 73 74 69 6e 63 74 3e 3d 30 20 29 7b 0a 20  Distinct>=0 ){. 
26212 20 20 20 20 20 61 64 64 72 4e 65 78 74 20 3d 20       addrNext = 
26213 73 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c  sqlite3VdbeMakeL
26214 61 62 65 6c 28 76 29 3b 0a 20 20 20 20 20 20 61  abel(v);.      a
26215 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 31 20 29  ssert( nArg==1 )
26216 3b 0a 20 20 20 20 20 20 63 6f 64 65 44 69 73 74  ;.      codeDist
26217 69 6e 63 74 28 70 50 61 72 73 65 2c 20 70 46 2d  inct(pParse, pF-
26218 3e 69 44 69 73 74 69 6e 63 74 2c 20 61 64 64 72  >iDistinct, addr
26219 4e 65 78 74 2c 20 31 2c 20 72 65 67 41 67 67 29  Next, 1, regAgg)
2621a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
2621b 70 46 2d 3e 70 46 75 6e 63 2d 3e 66 6c 61 67 73  pF->pFunc->flags
2621c 20 26 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 4e   & SQLITE_FUNC_N
2621d 45 45 44 43 4f 4c 4c 20 29 7b 0a 20 20 20 20 20  EEDCOLL ){.     
2621e 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 20   CollSeq *pColl 
2621f 3d 20 30 3b 0a 20 20 20 20 20 20 73 74 72 75 63  = 0;.      struc
26220 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65 6d 20  t ExprList_item 
26221 2a 70 49 74 65 6d 3b 0a 20 20 20 20 20 20 69 6e  *pItem;.      in
26222 74 20 6a 3b 0a 20 20 20 20 20 20 61 73 73 65 72  t j;.      asser
26223 74 28 20 70 4c 69 73 74 21 3d 30 20 29 3b 20 20  t( pList!=0 );  
26224 2f 2a 20 70 4c 69 73 74 21 3d 30 20 69 66 20 70  /* pList!=0 if p
26225 46 2d 3e 70 46 75 6e 63 20 68 61 73 20 4e 45 45  F->pFunc has NEE
26226 44 43 4f 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 66  DCOLL */.      f
26227 6f 72 28 6a 3d 30 2c 20 70 49 74 65 6d 3d 70 4c  or(j=0, pItem=pL
26228 69 73 74 2d 3e 61 3b 20 21 70 43 6f 6c 6c 20 26  ist->a; !pColl &
26229 26 20 6a 3c 6e 41 72 67 3b 20 6a 2b 2b 2c 20 70  & j<nArg; j++, p
2622a 49 74 65 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 20  Item++){.       
2622b 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2622c 45 78 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72  ExprCollSeq(pPar
2622d 73 65 2c 20 70 49 74 65 6d 2d 3e 70 45 78 70 72  se, pItem->pExpr
2622e 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2622f 20 69 66 28 20 21 70 43 6f 6c 6c 20 29 7b 0a 20   if( !pColl ){. 
26230 20 20 20 20 20 20 20 70 43 6f 6c 6c 20 3d 20 70         pColl = p
26231 50 61 72 73 65 2d 3e 64 62 2d 3e 70 44 66 6c 74  Parse->db->pDflt
26232 43 6f 6c 6c 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Coll;.      }.  
26233 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26234 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6c 6c  ddOp4(v, OP_Coll
26235 53 65 71 2c 20 30 2c 20 30 2c 20 30 2c 20 28 63  Seq, 0, 0, 0, (c
26236 68 61 72 20 2a 29 70 43 6f 6c 6c 2c 20 50 34 5f  har *)pColl, P4_
26237 43 4f 4c 4c 53 45 51 29 3b 0a 20 20 20 20 7d 0a  COLLSEQ);.    }.
26238 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26239 64 64 4f 70 34 28 76 2c 20 4f 50 5f 41 67 67 53  ddOp4(v, OP_AggS
2623a 74 65 70 2c 20 30 2c 20 72 65 67 41 67 67 2c 20  tep, 0, regAgg, 
2623b 70 46 2d 3e 69 4d 65 6d 2c 0a 20 20 20 20 20 20  pF->iMem,.      
2623c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2623d 28 76 6f 69 64 2a 29 70 46 2d 3e 70 46 75 6e 63  (void*)pF->pFunc
2623e 2c 20 50 34 5f 46 55 4e 43 44 45 46 29 3b 0a 20  , P4_FUNCDEF);. 
2623f 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68     sqlite3VdbeCh
26240 61 6e 67 65 50 35 28 76 2c 20 6e 41 72 67 29 3b  angeP5(v, nArg);
26241 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65  .    sqlite3Rele
26242 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50 61  aseTempRange(pPa
26243 72 73 65 2c 20 72 65 67 41 67 67 2c 20 6e 41 72  rse, regAgg, nAr
26244 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45  g);.    sqlite3E
26245 78 70 72 43 61 63 68 65 41 66 66 69 6e 69 74 79  xprCacheAffinity
26246 43 68 61 6e 67 65 28 70 50 61 72 73 65 2c 20 72  Change(pParse, r
26247 65 67 41 67 67 2c 20 6e 41 72 67 29 3b 0a 20 20  egAgg, nArg);.  
26248 20 20 69 66 28 20 61 64 64 72 4e 65 78 74 20 29    if( addrNext )
26249 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  {.      sqlite3V
2624a 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
2624b 76 2c 20 61 64 64 72 4e 65 78 74 29 3b 0a 20 20  v, addrNext);.  
2624c 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d    }.  }.  for(i=
2624d 30 2c 20 70 43 3d 70 41 67 67 49 6e 66 6f 2d 3e  0, pC=pAggInfo->
2624e 61 43 6f 6c 3b 20 69 3c 70 41 67 67 49 6e 66 6f  aCol; i<pAggInfo
2624f 2d 3e 6e 41 63 63 75 6d 75 6c 61 74 6f 72 3b 20  ->nAccumulator; 
26250 69 2b 2b 2c 20 70 43 2b 2b 29 7b 0a 20 20 20 20  i++, pC++){.    
26251 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
26252 70 50 61 72 73 65 2c 20 70 43 2d 3e 70 45 78 70  pParse, pC->pExp
26253 72 2c 20 70 43 2d 3e 69 4d 65 6d 29 3b 0a 20 20  r, pC->iMem);.  
26254 7d 0a 20 20 70 41 67 67 49 6e 66 6f 2d 3e 64 69  }.  pAggInfo->di
26255 72 65 63 74 4d 6f 64 65 20 3d 20 30 3b 0a 7d 0a  rectMode = 0;.}.
26256 0a 2f 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20  ./*.** Generate 
26257 63 6f 64 65 20 66 6f 72 20 74 68 65 20 53 45 4c  code for the SEL
26258 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20 67 69  ECT statement gi
26259 76 65 6e 20 69 6e 20 74 68 65 20 70 20 61 72 67  ven in the p arg
2625a 75 6d 65 6e 74 2e 20 20 0a 2a 2a 0a 2a 2a 20 54  ument.  .**.** T
2625b 68 65 20 72 65 73 75 6c 74 73 20 61 72 65 20 64  he results are d
2625c 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 76 61  istributed in va
2625d 72 69 6f 75 73 20 77 61 79 73 20 64 65 70 65 6e  rious ways depen
2625e 64 69 6e 67 20 6f 6e 20 74 68 65 0a 2a 2a 20 63  ding on the.** c
2625f 6f 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 53  ontents of the S
26260 65 6c 65 63 74 44 65 73 74 20 73 74 72 75 63 74  electDest struct
26261 75 72 65 20 70 6f 69 6e 74 65 64 20 74 6f 20 62  ure pointed to b
26262 79 20 61 72 67 75 6d 65 6e 74 20 70 44 65 73 74  y argument pDest
26263 0a 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  .** as follows:.
26264 2a 2a 0a 2a 2a 20 20 20 20 20 70 44 65 73 74 2d  **.**     pDest-
26265 3e 65 44 65 73 74 20 20 20 20 52 65 73 75 6c 74  >eDest    Result
26266 0a 2a 2a 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  .**     --------
26267 2d 2d 2d 2d 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  ----    --------
26268 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
26269 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2626a 2d 2d 2d 0a 2a 2a 20 20 20 20 20 53 52 54 5f 4f  ---.**     SRT_O
2626b 75 74 70 75 74 20 20 20 20 20 20 47 65 6e 65 72  utput      Gener
2626c 61 74 65 20 61 20 72 6f 77 20 6f 66 20 6f 75 74  ate a row of out
2626d 70 75 74 20 28 75 73 69 6e 67 20 74 68 65 20 4f  put (using the O
2626e 50 5f 52 65 73 75 6c 74 52 6f 77 0a 2a 2a 20 20  P_ResultRow.**  
2626f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26270 20 20 20 6f 70 63 6f 64 65 29 20 66 6f 72 20 65     opcode) for e
26271 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20 72  ach row in the r
26272 65 73 75 6c 74 20 73 65 74 2e 0a 2a 2a 0a 2a 2a  esult set..**.**
26273 20 20 20 20 20 53 52 54 5f 4d 65 6d 20 20 20 20       SRT_Mem    
26274 20 20 20 20 20 4f 6e 6c 79 20 76 61 6c 69 64 20       Only valid 
26275 69 66 20 74 68 65 20 72 65 73 75 6c 74 20 69 73  if the result is
26276 20 61 20 73 69 6e 67 6c 65 20 63 6f 6c 75 6d 6e   a single column
26277 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
26278 20 20 20 20 20 20 20 20 20 53 74 6f 72 65 20 74           Store t
26279 68 65 20 66 69 72 73 74 20 63 6f 6c 75 6d 6e 20  he first column 
2627a 6f 66 20 74 68 65 20 66 69 72 73 74 20 72 65 73  of the first res
2627b 75 6c 74 20 72 6f 77 0a 2a 2a 20 20 20 20 20 20  ult row.**      
2627c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69                 i
2627d 6e 20 72 65 67 69 73 74 65 72 20 70 44 65 73 74  n register pDest
2627e 2d 3e 69 50 61 72 6d 20 74 68 65 6e 20 61 62 61  ->iParm then aba
2627f 6e 64 6f 6e 20 74 68 65 20 72 65 73 74 0a 2a 2a  ndon the rest.**
26280 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26281 20 20 20 20 20 6f 66 20 74 68 65 20 71 75 65 72       of the quer
26282 79 2e 20 20 54 68 69 73 20 64 65 73 74 69 6e 61  y.  This destina
26283 74 69 6f 6e 20 69 6d 70 6c 69 65 73 20 22 4c 49  tion implies "LI
26284 4d 49 54 20 31 22 2e 0a 2a 2a 0a 2a 2a 20 20 20  MIT 1"..**.**   
26285 20 20 53 52 54 5f 53 65 74 20 20 20 20 20 20 20    SRT_Set       
26286 20 20 54 68 65 20 72 65 73 75 6c 74 20 6d 75 73    The result mus
26287 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20 63 6f  t be a single co
26288 6c 75 6d 6e 2e 20 20 53 74 6f 72 65 20 65 61 63  lumn.  Store eac
26289 68 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  h.**            
2628a 20 20 20 20 20 20 20 20 20 72 6f 77 20 6f 66 20           row of 
2628b 72 65 73 75 6c 74 20 61 73 20 74 68 65 20 6b 65  result as the ke
2628c 79 20 69 6e 20 74 61 62 6c 65 20 70 44 65 73 74  y in table pDest
2628d 2d 3e 69 50 61 72 6d 2e 20 0a 2a 2a 20 20 20 20  ->iParm. .**    
2628e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2628f 20 41 70 70 6c 79 20 74 68 65 20 61 66 66 69 6e   Apply the affin
26290 69 74 79 20 70 44 65 73 74 2d 3e 61 66 66 69 6e  ity pDest->affin
26291 69 74 79 20 62 65 66 6f 72 65 20 73 74 6f 72 69  ity before stori
26292 6e 67 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  ng.**           
26293 20 20 20 20 20 20 20 20 20 20 72 65 73 75 6c 74            result
26294 73 2e 20 20 55 73 65 64 20 74 6f 20 69 6d 70 6c  s.  Used to impl
26295 65 6d 65 6e 74 20 22 49 4e 20 28 53 45 4c 45 43  ement "IN (SELEC
26296 54 20 2e 2e 2e 29 22 2e 0a 2a 2a 0a 2a 2a 20 20  T ...)"..**.**  
26297 20 20 20 53 52 54 5f 55 6e 69 6f 6e 20 20 20 20     SRT_Union    
26298 20 20 20 53 74 6f 72 65 20 72 65 73 75 6c 74 73     Store results
26299 20 61 73 20 61 20 6b 65 79 20 69 6e 20 61 20 74   as a key in a t
2629a 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
2629b 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a  Dest->iParm..**.
2629c 2a 2a 20 20 20 20 20 53 52 54 5f 45 78 63 65 70  **     SRT_Excep
2629d 74 20 20 20 20 20 20 52 65 6d 6f 76 65 20 72 65  t      Remove re
2629e 73 75 6c 74 73 20 66 72 6f 6d 20 74 68 65 20 74  sults from the t
2629f 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 20 70  emporary table p
262a0 44 65 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a  Dest->iParm..**.
262a1 2a 2a 20 20 20 20 20 53 52 54 5f 54 61 62 6c 65  **     SRT_Table
262a2 20 20 20 20 20 20 20 53 74 6f 72 65 20 72 65 73         Store res
262a3 75 6c 74 73 20 69 6e 20 74 65 6d 70 6f 72 61 72  ults in temporar
262a4 79 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69  y table pDest->i
262a5 50 61 72 6d 2e 0a 2a 2a 20 20 20 20 20 20 20 20  Parm..**        
262a6 20 20 20 20 20 20 20 20 20 20 20 20 20 54 68 69               Thi
262a7 73 20 69 73 20 6c 69 6b 65 20 53 52 54 5f 45 70  s is like SRT_Ep
262a8 68 65 6d 54 61 62 20 65 78 63 65 70 74 20 74 68  hemTab except th
262a9 61 74 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a 20  at the table.** 
262aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262ab 20 20 20 20 69 73 20 61 73 73 75 6d 65 64 20 74      is assumed t
262ac 6f 20 61 6c 72 65 61 64 79 20 62 65 20 6f 70 65  o already be ope
262ad 6e 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54  n..**.**     SRT
262ae 5f 45 70 68 65 6d 54 61 62 20 20 20 20 43 72 65  _EphemTab    Cre
262af 61 74 65 20 61 6e 20 74 65 6d 70 6f 72 61 72 79  ate an temporary
262b0 20 74 61 62 6c 65 20 70 44 65 73 74 2d 3e 69 50   table pDest->iP
262b1 61 72 6d 20 61 6e 64 20 73 74 6f 72 65 0a 2a 2a  arm and store.**
262b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262b3 20 20 20 20 20 74 68 65 20 72 65 73 75 6c 74 20       the result 
262b4 74 68 65 72 65 2e 20 54 68 65 20 63 75 72 73 6f  there. The curso
262b5 72 20 69 73 20 6c 65 66 74 20 6f 70 65 6e 20 61  r is left open a
262b6 66 74 65 72 0a 2a 2a 20 20 20 20 20 20 20 20 20  fter.**         
262b7 20 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75              retu
262b8 72 6e 69 6e 67 2e 20 20 54 68 69 73 20 69 73 20  rning.  This is 
262b9 6c 69 6b 65 20 53 52 54 5f 54 61 62 6c 65 20 65  like SRT_Table e
262ba 78 63 65 70 74 20 74 68 61 74 0a 2a 2a 20 20 20  xcept that.**   
262bb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262bc 20 20 74 68 69 73 20 64 65 73 74 69 6e 61 74 69    this destinati
262bd 6f 6e 20 75 73 65 73 20 4f 50 5f 4f 70 65 6e 45  on uses OP_OpenE
262be 70 68 65 6d 65 72 61 6c 20 74 6f 20 63 72 65 61  phemeral to crea
262bf 74 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  te.**           
262c0 20 20 20 20 20 20 20 20 20 20 74 68 65 20 74 61            the ta
262c1 62 6c 65 20 66 69 72 73 74 2e 0a 2a 2a 0a 2a 2a  ble first..**.**
262c2 20 20 20 20 20 53 52 54 5f 43 6f 72 6f 75 74 69       SRT_Corouti
262c3 6e 65 20 20 20 47 65 6e 65 72 61 74 65 20 61 20  ne   Generate a 
262c4 63 6f 2d 72 6f 75 74 69 6e 65 20 74 68 61 74 20  co-routine that 
262c5 72 65 74 75 72 6e 73 20 61 20 6e 65 77 20 72 6f  returns a new ro
262c6 77 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 20 20  w of.**         
262c7 20 20 20 20 20 20 20 20 20 20 20 20 72 65 73 75              resu
262c8 6c 74 73 20 65 61 63 68 20 74 69 6d 65 20 69 74  lts each time it
262c9 20 69 73 20 69 6e 76 6f 6b 65 64 2e 20 20 54 68   is invoked.  Th
262ca 65 20 65 6e 74 72 79 20 70 6f 69 6e 74 0a 2a 2a  e entry point.**
262cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262cc 20 20 20 20 20 6f 66 20 74 68 65 20 63 6f 2d 72       of the co-r
262cd 6f 75 74 69 6e 65 20 69 73 20 73 74 6f 72 65 64  outine is stored
262ce 20 69 6e 20 72 65 67 69 73 74 65 72 20 70 44 65   in register pDe
262cf 73 74 2d 3e 69 50 61 72 6d 2e 0a 2a 2a 0a 2a 2a  st->iParm..**.**
262d0 20 20 20 20 20 53 52 54 5f 45 78 69 73 74 73 20       SRT_Exists 
262d1 20 20 20 20 20 53 74 6f 72 65 20 61 20 31 20 69       Store a 1 i
262d2 6e 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 70 44  n memory cell pD
262d3 65 73 74 2d 3e 69 50 61 72 6d 20 69 66 20 74 68  est->iParm if th
262d4 65 20 72 65 73 75 6c 74 0a 2a 2a 20 20 20 20 20  e result.**     
262d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262d6 73 65 74 20 69 73 20 6e 6f 74 20 65 6d 70 74 79  set is not empty
262d7 2e 0a 2a 2a 0a 2a 2a 20 20 20 20 20 53 52 54 5f  ..**.**     SRT_
262d8 44 69 73 63 61 72 64 20 20 20 20 20 54 68 72 6f  Discard     Thro
262d9 77 20 74 68 65 20 72 65 73 75 6c 74 73 20 61 77  w the results aw
262da 61 79 2e 20 20 54 68 69 73 20 69 73 20 75 73 65  ay.  This is use
262db 64 20 62 79 20 53 45 4c 45 43 54 0a 2a 2a 20 20  d by SELECT.**  
262dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
262dd 20 20 20 73 74 61 74 65 6d 65 6e 74 73 20 77 69     statements wi
262de 74 68 69 6e 20 74 72 69 67 67 65 72 73 20 77 68  thin triggers wh
262df 6f 73 65 20 6f 6e 6c 79 20 70 75 72 70 6f 73 65  ose only purpose
262e0 20 69 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20   is.**          
262e1 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20 73             the s
262e2 69 64 65 2d 65 66 66 65 63 74 73 20 6f 66 20 66  ide-effects of f
262e3 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20  unctions..**.** 
262e4 54 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65 74  This routine ret
262e5 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 72 20  urns the number 
262e6 6f 66 20 65 72 72 6f 72 73 2e 20 20 49 66 20 61  of errors.  If a
262e7 6e 79 20 65 72 72 6f 72 73 20 61 72 65 0a 2a 2a  ny errors are.**
262e8 20 65 6e 63 6f 75 6e 74 65 72 65 64 2c 20 74 68   encountered, th
262e9 65 6e 20 61 6e 20 61 70 70 72 6f 70 72 69 61 74  en an appropriat
262ea 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20  e error message 
262eb 69 73 20 6c 65 66 74 20 69 6e 0a 2a 2a 20 70 50  is left in.** pP
262ec 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2e 0a 2a  arse->zErrMsg..*
262ed 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
262ee 65 20 64 6f 65 73 20 4e 4f 54 20 66 72 65 65 20  e does NOT free 
262ef 74 68 65 20 53 65 6c 65 63 74 20 73 74 72 75 63  the Select struc
262f0 74 75 72 65 20 70 61 73 73 65 64 20 69 6e 2e 20  ture passed in. 
262f1 20 54 68 65 0a 2a 2a 20 63 61 6c 6c 69 6e 67 20   The.** calling 
262f2 66 75 6e 63 74 69 6f 6e 20 6e 65 65 64 73 20 74  function needs t
262f3 6f 20 64 6f 20 74 68 61 74 2e 0a 2a 2f 0a 53 51  o do that..*/.SQ
262f4 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74  LITE_PRIVATE int
262f5 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 28 0a   sqlite3Select(.
262f6 20 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c    Parse *pParse,
262f7 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
262f8 70 61 72 73 65 72 20 63 6f 6e 74 65 78 74 20 2a  parser context *
262f9 2f 0a 20 20 53 65 6c 65 63 74 20 2a 70 2c 20 20  /.  Select *p,  
262fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
262fb 65 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65  e SELECT stateme
262fc 6e 74 20 62 65 69 6e 67 20 63 6f 64 65 64 2e 20  nt being coded. 
262fd 2a 2f 0a 20 20 53 65 6c 65 63 74 44 65 73 74 20  */.  SelectDest 
262fe 2a 70 44 65 73 74 20 20 20 20 20 20 2f 2a 20 57  *pDest      /* W
262ff 68 61 74 20 74 6f 20 64 6f 20 77 69 74 68 20 74  hat to do with t
26300 68 65 20 71 75 65 72 79 20 72 65 73 75 6c 74 73  he query results
26301 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20   */.){.  int i, 
26302 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  j;              
26303 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 73  /* Loop counters
26304 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f 20   */.  WhereInfo 
26305 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 2f 2a 20  *pWInfo;     /* 
26306 52 65 74 75 72 6e 20 66 72 6f 6d 20 73 71 6c 69  Return from sqli
26307 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 20  te3WhereBegin() 
26308 2a 2f 0a 20 20 56 64 62 65 20 2a 76 3b 20 20 20  */.  Vdbe *v;   
26309 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2630a 68 65 20 76 69 72 74 75 61 6c 20 6d 61 63 68 69  he virtual machi
2630b 6e 65 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ne under constru
2630c 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  ction */.  int i
2630d 73 41 67 67 3b 20 20 20 20 20 20 20 20 20 20 20  sAgg;           
2630e 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20 73 65    /* True for se
2630f 6c 65 63 74 20 6c 69 73 74 73 20 6c 69 6b 65 20  lect lists like 
26310 22 63 6f 75 6e 74 28 2a 29 22 20 2a 2f 0a 20 20  "count(*)" */.  
26311 45 78 70 72 4c 69 73 74 20 2a 70 45 4c 69 73 74  ExprList *pEList
26312 3b 20 20 20 20 20 20 2f 2a 20 4c 69 73 74 20 6f  ;      /* List o
26313 66 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 65 78 74  f columns to ext
26314 72 61 63 74 2e 20 2a 2f 0a 20 20 53 72 63 4c 69  ract. */.  SrcLi
26315 73 74 20 2a 70 54 61 62 4c 69 73 74 3b 20 20 20  st *pTabList;   
26316 20 20 2f 2a 20 4c 69 73 74 20 6f 66 20 74 61 62    /* List of tab
26317 6c 65 73 20 74 6f 20 73 65 6c 65 63 74 20 66 72  les to select fr
26318 6f 6d 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57  om */.  Expr *pW
26319 68 65 72 65 3b 20 20 20 20 20 20 20 20 20 20 2f  here;          /
2631a 2a 20 54 68 65 20 57 48 45 52 45 20 63 6c 61 75  * The WHERE clau
2631b 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c 4c  se.  May be NULL
2631c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
2631d 70 4f 72 64 65 72 42 79 3b 20 20 20 20 2f 2a 20  pOrderBy;    /* 
2631e 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
2631f 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
26320 4c 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  L */.  ExprList 
26321 2a 70 47 72 6f 75 70 42 79 3b 20 20 20 20 2f 2a  *pGroupBy;    /*
26322 20 54 68 65 20 47 52 4f 55 50 20 42 59 20 63 6c   The GROUP BY cl
26323 61 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55  ause.  May be NU
26324 4c 4c 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 48  LL */.  Expr *pH
26325 61 76 69 6e 67 3b 20 20 20 20 20 20 20 20 20 2f  aving;         /
26326 2a 20 54 68 65 20 48 41 56 49 4e 47 20 63 6c 61  * The HAVING cla
26327 75 73 65 2e 20 20 4d 61 79 20 62 65 20 4e 55 4c  use.  May be NUL
26328 4c 20 2a 2f 0a 20 20 69 6e 74 20 69 73 44 69 73  L */.  int isDis
26329 74 69 6e 63 74 3b 20 20 20 20 20 20 20 20 2f 2a  tinct;        /*
2632a 20 54 72 75 65 20 69 66 20 74 68 65 20 44 49 53   True if the DIS
2632b 54 49 4e 43 54 20 6b 65 79 77 6f 72 64 20 69 73  TINCT keyword is
2632c 20 70 72 65 73 65 6e 74 20 2a 2f 0a 20 20 69 6e   present */.  in
2632d 74 20 64 69 73 74 69 6e 63 74 3b 20 20 20 20 20  t distinct;     
2632e 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20 74 6f       /* Table to
2632f 20 75 73 65 20 66 6f 72 20 74 68 65 20 64 69 73   use for the dis
26330 74 69 6e 63 74 20 73 65 74 20 2a 2f 0a 20 20 69  tinct set */.  i
26331 6e 74 20 72 63 20 3d 20 31 3b 20 20 20 20 20 20  nt rc = 1;      
26332 20 20 20 20 20 20 2f 2a 20 56 61 6c 75 65 20 74        /* Value t
26333 6f 20 72 65 74 75 72 6e 20 66 72 6f 6d 20 74 68  o return from th
26334 69 73 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  is function */. 
26335 20 69 6e 74 20 61 64 64 72 53 6f 72 74 49 6e 64   int addrSortInd
26336 65 78 3b 20 20 20 20 20 2f 2a 20 41 64 64 72 65  ex;     /* Addre
26337 73 73 20 6f 66 20 61 6e 20 4f 50 5f 4f 70 65 6e  ss of an OP_Open
26338 45 70 68 65 6d 65 72 61 6c 20 69 6e 73 74 72 75  Ephemeral instru
26339 63 74 69 6f 6e 20 2a 2f 0a 20 20 41 67 67 49 6e  ction */.  AggIn
2633a 66 6f 20 73 41 67 67 49 6e 66 6f 3b 20 20 20 20  fo sAggInfo;    
2633b 20 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e    /* Information
2633c 20 75 73 65 64 20 62 79 20 61 67 67 72 65 67 61   used by aggrega
2633d 74 65 20 71 75 65 72 69 65 73 20 2a 2f 0a 20 20  te queries */.  
2633e 69 6e 74 20 69 45 6e 64 3b 20 20 20 20 20 20 20  int iEnd;       
2633f 20 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73         /* Addres
26340 73 20 6f 66 20 74 68 65 20 65 6e 64 20 6f 66 20  s of the end of 
26341 74 68 65 20 71 75 65 72 79 20 2a 2f 0a 20 20 73  the query */.  s
26342 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20  qlite3 *db;     
26343 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
26344 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
26345 20 2a 2f 0a 0a 20 20 64 62 20 3d 20 70 50 61 72   */..  db = pPar
26346 73 65 2d 3e 64 62 3b 0a 20 20 69 66 28 20 70 3d  se->db;.  if( p=
26347 3d 30 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  =0 || db->malloc
26348 46 61 69 6c 65 64 20 7c 7c 20 70 50 61 72 73 65  Failed || pParse
26349 2d 3e 6e 45 72 72 20 29 7b 0a 20 20 20 20 72 65  ->nErr ){.    re
2634a 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66  turn 1;.  }.  if
2634b 28 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65  ( sqlite3AuthChe
2634c 63 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54  ck(pParse, SQLIT
2634d 45 5f 53 45 4c 45 43 54 2c 20 30 2c 20 30 2c 20  E_SELECT, 0, 0, 
2634e 30 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 20  0) ) return 1;. 
2634f 20 6d 65 6d 73 65 74 28 26 73 41 67 67 49 6e 66   memset(&sAggInf
26350 6f 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 41 67  o, 0, sizeof(sAg
26351 67 49 6e 66 6f 29 29 3b 0a 0a 20 20 70 4f 72 64  gInfo));..  pOrd
26352 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
26353 42 79 3b 0a 20 20 69 66 28 20 49 67 6e 6f 72 61  By;.  if( Ignora
26354 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73 74  bleOrderby(pDest
26355 29 20 29 7b 0a 20 20 20 20 70 2d 3e 70 4f 72 64  ) ){.    p->pOrd
26356 65 72 42 79 20 3d 20 30 3b 0a 0a 20 20 20 20 2f  erBy = 0;..    /
26357 2a 20 49 6e 20 74 68 65 73 65 20 63 61 73 65 73  * In these cases
26358 20 74 68 65 20 44 49 53 54 49 4e 43 54 20 6f 70   the DISTINCT op
26359 65 72 61 74 6f 72 20 6d 61 6b 65 73 20 6e 6f 20  erator makes no 
2635a 64 69 66 66 65 72 65 6e 63 65 20 74 6f 20 74 68  difference to th
2635b 65 0a 20 20 20 20 2a 2a 20 72 65 73 75 6c 74 73  e.    ** results
2635c 2c 20 73 6f 20 72 65 6d 6f 76 65 20 69 74 20 69  , so remove it i
2635d 66 20 69 74 20 77 65 72 65 20 73 70 65 63 69 66  f it were specif
2635e 69 65 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ied..    */.    
2635f 61 73 73 65 72 74 28 70 44 65 73 74 2d 3e 65 44  assert(pDest->eD
26360 65 73 74 3d 3d 53 52 54 5f 45 78 69 73 74 73 20  est==SRT_Exists 
26361 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65 73 74 3d  || pDest->eDest=
26362 3d 53 52 54 5f 55 6e 69 6f 6e 20 7c 7c 20 0a 20  =SRT_Union || . 
26363 20 20 20 20 20 20 20 20 20 20 70 44 65 73 74 2d            pDest-
26364 3e 65 44 65 73 74 3d 3d 53 52 54 5f 45 78 63 65  >eDest==SRT_Exce
26365 70 74 20 7c 7c 20 70 44 65 73 74 2d 3e 65 44 65  pt || pDest->eDe
26366 73 74 3d 3d 53 52 54 5f 44 69 73 63 61 72 64 29  st==SRT_Discard)
26367 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c 61 67  ;.    p->selFlag
26368 73 20 26 3d 20 7e 53 46 5f 44 69 73 74 69 6e 63  s &= ~SF_Distinc
26369 74 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  t;.  }.  sqlite3
2636a 53 65 6c 65 63 74 50 72 65 70 28 70 50 61 72 73  SelectPrep(pPars
2636b 65 2c 20 70 2c 20 30 29 3b 0a 20 20 69 66 28 20  e, p, 0);.  if( 
2636c 70 50 61 72 73 65 2d 3e 6e 45 72 72 20 29 7b 0a  pParse->nErr ){.
2636d 20 20 20 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f      goto select_
2636e 65 6e 64 3b 0a 20 20 7d 0a 20 20 70 2d 3e 70 4f  end;.  }.  p->pO
2636f 72 64 65 72 42 79 20 3d 20 70 4f 72 64 65 72 42  rderBy = pOrderB
26370 79 3b 0a 0a 0a 20 20 2f 2a 20 4d 61 6b 65 20 6c  y;...  /* Make l
26371 6f 63 61 6c 20 63 6f 70 69 65 73 20 6f 66 20 74  ocal copies of t
26372 68 65 20 70 61 72 61 6d 65 74 65 72 73 20 66 6f  he parameters fo
26373 72 20 74 68 69 73 20 71 75 65 72 79 2e 0a 20 20  r this query..  
26374 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74 20 3d 20  */.  pTabList = 
26375 70 2d 3e 70 53 72 63 3b 0a 20 20 69 73 41 67 67  p->pSrc;.  isAgg
26376 20 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20   = (p->selFlags 
26377 26 20 53 46 5f 41 67 67 72 65 67 61 74 65 29 21  & SF_Aggregate)!
26378 3d 30 3b 0a 20 20 70 45 4c 69 73 74 20 3d 20 70  =0;.  pEList = p
26379 2d 3e 70 45 4c 69 73 74 3b 0a 20 20 69 66 28 20  ->pEList;.  if( 
2637a 70 45 4c 69 73 74 3d 3d 30 20 29 20 67 6f 74 6f  pEList==0 ) goto
2637b 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20   select_end;..  
2637c 2f 2a 20 0a 20 20 2a 2a 20 44 6f 20 6e 6f 74 20  /* .  ** Do not 
2637d 65 76 65 6e 20 61 74 74 65 6d 70 74 20 74 6f 20  even attempt to 
2637e 67 65 6e 65 72 61 74 65 20 61 6e 79 20 63 6f 64  generate any cod
2637f 65 20 69 66 20 77 65 20 68 61 76 65 20 61 6c 72  e if we have alr
26380 65 61 64 79 20 73 65 65 6e 0a 20 20 2a 2a 20 65  eady seen.  ** e
26381 72 72 6f 72 73 20 62 65 66 6f 72 65 20 74 68 69  rrors before thi
26382 73 20 72 6f 75 74 69 6e 65 20 73 74 61 72 74 73  s routine starts
26383 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 50 61  ..  */.  if( pPa
26384 72 73 65 2d 3e 6e 45 72 72 3e 30 20 29 20 67 6f  rse->nErr>0 ) go
26385 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 0a  to select_end;..
26386 20 20 2f 2a 20 4f 52 44 45 52 20 42 59 20 69 73    /* ORDER BY is
26387 20 69 67 6e 6f 72 65 64 20 66 6f 72 20 73 6f 6d   ignored for som
26388 65 20 64 65 73 74 69 6e 61 74 69 6f 6e 73 2e 0a  e destinations..
26389 20 20 2a 2f 0a 20 20 69 66 28 20 49 67 6e 6f 72    */.  if( Ignor
2638a 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73  ableOrderby(pDes
2638b 74 29 20 29 7b 0a 20 20 20 20 70 4f 72 64 65 72  t) ){.    pOrder
2638c 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f  By = 0;.  }..  /
2638d 2a 20 42 65 67 69 6e 20 67 65 6e 65 72 61 74 69  * Begin generati
2638e 6e 67 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20  ng code..  */.  
2638f 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56 64  v = sqlite3GetVd
26390 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69 66  be(pParse);.  if
26391 28 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 73 65  ( v==0 ) goto se
26392 6c 65 63 74 5f 65 6e 64 3b 0a 0a 20 20 2f 2a 20  lect_end;..  /* 
26393 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
26394 72 20 61 6c 6c 20 73 75 62 2d 71 75 65 72 69 65  r all sub-querie
26395 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c  s in the FROM cl
26396 61 75 73 65 0a 20 20 2a 2f 0a 23 69 66 20 21 64  ause.  */.#if !d
26397 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
26398 49 54 5f 53 55 42 51 55 45 52 59 29 20 7c 7c 20  IT_SUBQUERY) || 
26399 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
2639a 4f 4d 49 54 5f 56 49 45 57 29 0a 20 20 66 6f 72  OMIT_VIEW).  for
2639b 28 69 3d 30 3b 20 21 70 2d 3e 70 50 72 69 6f 72  (i=0; !p->pPrior
2639c 20 26 26 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e   && i<pTabList->
2639d 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  nSrc; i++){.    
2639e 73 74 72 75 63 74 20 53 72 63 4c 69 73 74 5f 69  struct SrcList_i
2639f 74 65 6d 20 2a 70 49 74 65 6d 20 3d 20 26 70 54  tem *pItem = &pT
263a0 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 3b 0a 20 20  abList->a[i];.  
263a1 20 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73    SelectDest des
263a2 74 3b 0a 20 20 20 20 53 65 6c 65 63 74 20 2a 70  t;.    Select *p
263a3 53 75 62 20 3d 20 70 49 74 65 6d 2d 3e 70 53 65  Sub = pItem->pSe
263a4 6c 65 63 74 3b 0a 20 20 20 20 69 6e 74 20 69 73  lect;.    int is
263a5 41 67 67 53 75 62 3b 0a 0a 20 20 20 20 69 66 28  AggSub;..    if(
263a6 20 70 53 75 62 3d 3d 30 20 7c 7c 20 70 49 74 65   pSub==0 || pIte
263a7 6d 2d 3e 69 73 50 6f 70 75 6c 61 74 65 64 20 29  m->isPopulated )
263a8 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20   continue;..    
263a9 2f 2a 20 49 6e 63 72 65 6d 65 6e 74 20 50 61 72  /* Increment Par
263aa 73 65 2e 6e 48 65 69 67 68 74 20 62 79 20 74 68  se.nHeight by th
263ab 65 20 68 65 69 67 68 74 20 6f 66 20 74 68 65 20  e height of the 
263ac 6c 61 72 67 65 73 74 20 65 78 70 72 65 73 73 69  largest expressi
263ad 6f 6e 0a 20 20 20 20 2a 2a 20 74 72 65 65 20 72  on.    ** tree r
263ae 65 66 65 72 65 64 20 74 6f 20 62 79 20 74 68 69  efered to by thi
263af 73 2c 20 74 68 65 20 70 61 72 65 6e 74 20 73 65  s, the parent se
263b0 6c 65 63 74 2e 20 54 68 65 20 63 68 69 6c 64 20  lect. The child 
263b1 73 65 6c 65 63 74 0a 20 20 20 20 2a 2a 20 6d 61  select.    ** ma
263b2 79 20 63 6f 6e 74 61 69 6e 20 65 78 70 72 65 73  y contain expres
263b3 73 69 6f 6e 20 74 72 65 65 73 20 6f 66 20 61 74  sion trees of at
263b4 20 6d 6f 73 74 0a 20 20 20 20 2a 2a 20 28 53 51   most.    ** (SQ
263b5 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44 45  LITE_MAX_EXPR_DE
263b6 50 54 48 2d 50 61 72 73 65 2e 6e 48 65 69 67 68  PTH-Parse.nHeigh
263b7 74 29 20 68 65 69 67 68 74 2e 20 54 68 69 73 20  t) height. This 
263b8 69 73 20 61 20 62 69 74 0a 20 20 20 20 2a 2a 20  is a bit.    ** 
263b9 6d 6f 72 65 20 63 6f 6e 73 65 72 76 61 74 69 76  more conservativ
263ba 65 20 74 68 61 6e 20 6e 65 63 65 73 73 61 72 79  e than necessary
263bb 2c 20 62 75 74 20 6d 75 63 68 20 65 61 73 69 65  , but much easie
263bc 72 20 74 68 61 6e 20 65 6e 66 6f 72 63 69 6e 67  r than enforcing
263bd 0a 20 20 20 20 2a 2a 20 61 6e 20 65 78 61 63 74  .    ** an exact
263be 20 6c 69 6d 69 74 2e 0a 20 20 20 20 2a 2f 0a 20   limit..    */. 
263bf 20 20 20 70 50 61 72 73 65 2d 3e 6e 48 65 69 67     pParse->nHeig
263c0 68 74 20 2b 3d 20 73 71 6c 69 74 65 33 53 65 6c  ht += sqlite3Sel
263c1 65 63 74 45 78 70 72 48 65 69 67 68 74 28 70 29  ectExprHeight(p)
263c2 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20  ;..    /* Check 
263c3 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 75  to see if the su
263c4 62 71 75 65 72 79 20 63 61 6e 20 62 65 20 61 62  bquery can be ab
263c5 73 6f 72 62 65 64 20 69 6e 74 6f 20 74 68 65 20  sorbed into the 
263c6 70 61 72 65 6e 74 2e 20 2a 2f 0a 20 20 20 20 69  parent. */.    i
263c7 73 41 67 67 53 75 62 20 3d 20 28 70 53 75 62 2d  sAggSub = (pSub-
263c8 3e 73 65 6c 46 6c 61 67 73 20 26 20 53 46 5f 41  >selFlags & SF_A
263c9 67 67 72 65 67 61 74 65 29 21 3d 30 3b 0a 20 20  ggregate)!=0;.  
263ca 20 20 69 66 28 20 66 6c 61 74 74 65 6e 53 75 62    if( flattenSub
263cb 71 75 65 72 79 28 70 50 61 72 73 65 2c 20 70 2c  query(pParse, p,
263cc 20 69 2c 20 69 73 41 67 67 2c 20 69 73 41 67 67   i, isAgg, isAgg
263cd 53 75 62 29 20 29 7b 0a 20 20 20 20 20 20 69 66  Sub) ){.      if
263ce 28 20 69 73 41 67 67 53 75 62 20 29 7b 0a 20 20  ( isAggSub ){.  
263cf 20 20 20 20 20 20 69 73 41 67 67 20 3d 20 31 3b        isAgg = 1;
263d0 0a 20 20 20 20 20 20 20 20 70 2d 3e 73 65 6c 46  .        p->selF
263d1 6c 61 67 73 20 7c 3d 20 53 46 5f 41 67 67 72 65  lags |= SF_Aggre
263d2 67 61 74 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  gate;.      }.  
263d3 20 20 20 20 69 20 3d 20 2d 31 3b 0a 20 20 20 20      i = -1;.    
263d4 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
263d5 69 74 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e  ite3SelectDestIn
263d6 69 74 28 26 64 65 73 74 2c 20 53 52 54 5f 45 70  it(&dest, SRT_Ep
263d7 68 65 6d 54 61 62 2c 20 70 49 74 65 6d 2d 3e 69  hemTab, pItem->i
263d8 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 61  Cursor);.      a
263d9 73 73 65 72 74 28 20 70 49 74 65 6d 2d 3e 69 73  ssert( pItem->is
263da 50 6f 70 75 6c 61 74 65 64 3d 3d 30 20 29 3b 0a  Populated==0 );.
263db 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
263dc 65 63 74 28 70 50 61 72 73 65 2c 20 70 53 75 62  ect(pParse, pSub
263dd 2c 20 26 64 65 73 74 29 3b 0a 20 20 20 20 20 20  , &dest);.      
263de 70 49 74 65 6d 2d 3e 69 73 50 6f 70 75 6c 61 74  pItem->isPopulat
263df 65 64 20 3d 20 31 3b 0a 20 20 20 20 7d 0a 20 20  ed = 1;.    }.  
263e0 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 45    if( pParse->nE
263e1 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63  rr || db->malloc
263e2 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
263e3 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
263e4 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
263e5 65 2d 3e 6e 48 65 69 67 68 74 20 2d 3d 20 73 71  e->nHeight -= sq
263e6 6c 69 74 65 33 53 65 6c 65 63 74 45 78 70 72 48  lite3SelectExprH
263e7 65 69 67 68 74 28 70 29 3b 0a 20 20 20 20 70 54  eight(p);.    pT
263e8 61 62 4c 69 73 74 20 3d 20 70 2d 3e 70 53 72 63  abList = p->pSrc
263e9 3b 0a 20 20 20 20 69 66 28 20 21 49 67 6e 6f 72  ;.    if( !Ignor
263ea 61 62 6c 65 4f 72 64 65 72 62 79 28 70 44 65 73  ableOrderby(pDes
263eb 74 29 20 29 7b 0a 20 20 20 20 20 20 70 4f 72 64  t) ){.      pOrd
263ec 65 72 42 79 20 3d 20 70 2d 3e 70 4f 72 64 65 72  erBy = p->pOrder
263ed 42 79 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  By;.    }.  }.  
263ee 70 45 4c 69 73 74 20 3d 20 70 2d 3e 70 45 4c 69  pEList = p->pELi
263ef 73 74 3b 0a 23 65 6e 64 69 66 0a 20 20 70 57 68  st;.#endif.  pWh
263f0 65 72 65 20 3d 20 70 2d 3e 70 57 68 65 72 65 3b  ere = p->pWhere;
263f1 0a 20 20 70 47 72 6f 75 70 42 79 20 3d 20 70 2d  .  pGroupBy = p-
263f2 3e 70 47 72 6f 75 70 42 79 3b 0a 20 20 70 48 61  >pGroupBy;.  pHa
263f3 76 69 6e 67 20 3d 20 70 2d 3e 70 48 61 76 69 6e  ving = p->pHavin
263f4 67 3b 0a 20 20 69 73 44 69 73 74 69 6e 63 74 20  g;.  isDistinct 
263f5 3d 20 28 70 2d 3e 73 65 6c 46 6c 61 67 73 20 26  = (p->selFlags &
263f6 20 53 46 5f 44 69 73 74 69 6e 63 74 29 21 3d 30   SF_Distinct)!=0
263f7 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
263f8 45 5f 4f 4d 49 54 5f 43 4f 4d 50 4f 55 4e 44 5f  E_OMIT_COMPOUND_
263f9 53 45 4c 45 43 54 0a 20 20 2f 2a 20 49 66 20 74  SELECT.  /* If t
263fa 68 65 72 65 20 69 73 20 61 72 65 20 61 20 73 65  here is are a se
263fb 71 75 65 6e 63 65 20 6f 66 20 71 75 65 72 69 65  quence of querie
263fc 73 2c 20 64 6f 20 74 68 65 20 65 61 72 6c 69 65  s, do the earlie
263fd 72 20 6f 6e 65 73 20 66 69 72 73 74 2e 0a 20 20  r ones first..  
263fe 2a 2f 0a 20 20 69 66 28 20 70 2d 3e 70 50 72 69  */.  if( p->pPri
263ff 6f 72 20 29 7b 0a 20 20 20 20 69 66 28 20 70 2d  or ){.    if( p-
26400 3e 70 52 69 67 68 74 6d 6f 73 74 3d 3d 30 20 29  >pRightmost==0 )
26401 7b 0a 20 20 20 20 20 20 53 65 6c 65 63 74 20 2a  {.      Select *
26402 70 4c 6f 6f 70 2c 20 2a 70 52 69 67 68 74 20 3d  pLoop, *pRight =
26403 20 30 3b 0a 20 20 20 20 20 20 69 6e 74 20 63 6e   0;.      int cn
26404 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 6e 74  t = 0;.      int
26405 20 6d 78 53 65 6c 65 63 74 3b 0a 20 20 20 20 20   mxSelect;.     
26406 20 66 6f 72 28 70 4c 6f 6f 70 3d 70 3b 20 70 4c   for(pLoop=p; pL
26407 6f 6f 70 3b 20 70 4c 6f 6f 70 3d 70 4c 6f 6f 70  oop; pLoop=pLoop
26408 2d 3e 70 50 72 69 6f 72 2c 20 63 6e 74 2b 2b 29  ->pPrior, cnt++)
26409 7b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d  {.        pLoop-
2640a 3e 70 52 69 67 68 74 6d 6f 73 74 20 3d 20 70 3b  >pRightmost = p;
2640b 0a 20 20 20 20 20 20 20 20 70 4c 6f 6f 70 2d 3e  .        pLoop->
2640c 70 4e 65 78 74 20 3d 20 70 52 69 67 68 74 3b 0a  pNext = pRight;.
2640d 20 20 20 20 20 20 20 20 70 52 69 67 68 74 20 3d          pRight =
2640e 20 70 4c 6f 6f 70 3b 0a 20 20 20 20 20 20 7d 0a   pLoop;.      }.
2640f 20 20 20 20 20 20 6d 78 53 65 6c 65 63 74 20 3d        mxSelect =
26410 20 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49   db->aLimit[SQLI
26411 54 45 5f 4c 49 4d 49 54 5f 43 4f 4d 50 4f 55 4e  TE_LIMIT_COMPOUN
26412 44 5f 53 45 4c 45 43 54 5d 3b 0a 20 20 20 20 20  D_SELECT];.     
26413 20 69 66 28 20 6d 78 53 65 6c 65 63 74 20 26 26   if( mxSelect &&
26414 20 63 6e 74 3e 6d 78 53 65 6c 65 63 74 20 29 7b   cnt>mxSelect ){
26415 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
26416 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c  ErrorMsg(pParse,
26417 20 22 74 6f 6f 20 6d 61 6e 79 20 74 65 72 6d 73   "too many terms
26418 20 69 6e 20 63 6f 6d 70 6f 75 6e 64 20 53 45 4c   in compound SEL
26419 45 43 54 22 29 3b 0a 20 20 20 20 20 20 20 20 72  ECT");.        r
2641a 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d  eturn 1;.      }
2641b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74 75 72  .    }.    retur
2641c 6e 20 6d 75 6c 74 69 53 65 6c 65 63 74 28 70 50  n multiSelect(pP
2641d 61 72 73 65 2c 20 70 2c 20 70 44 65 73 74 29 3b  arse, p, pDest);
2641e 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2641f 2a 20 49 66 20 77 72 69 74 69 6e 67 20 74 6f 20  * If writing to 
26420 6d 65 6d 6f 72 79 20 6f 72 20 67 65 6e 65 72 61  memory or genera
26421 74 69 6e 67 20 61 20 73 65 74 0a 20 20 2a 2a 20  ting a set.  ** 
26422 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20 63 6f  only a single co
26423 6c 75 6d 6e 20 6d 61 79 20 62 65 20 6f 75 74 70  lumn may be outp
26424 75 74 2e 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66  ut..  */.#ifndef
26425 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55 42   SQLITE_OMIT_SUB
26426 51 55 45 52 59 0a 20 20 69 66 28 20 63 68 65 63  QUERY.  if( chec
26427 6b 46 6f 72 4d 75 6c 74 69 43 6f 6c 75 6d 6e 53  kForMultiColumnS
26428 65 6c 65 63 74 45 72 72 6f 72 28 70 50 61 72 73  electError(pPars
26429 65 2c 20 70 44 65 73 74 2c 20 70 45 4c 69 73 74  e, pDest, pEList
2642a 2d 3e 6e 45 78 70 72 29 20 29 7b 0a 20 20 20 20  ->nExpr) ){.    
2642b 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
2642c 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  .  }.#endif..  /
2642d 2a 20 49 66 20 70 6f 73 73 69 62 6c 65 2c 20 72  * If possible, r
2642e 65 77 72 69 74 65 20 74 68 65 20 71 75 65 72 79  ewrite the query
2642f 20 74 6f 20 75 73 65 20 47 52 4f 55 50 20 42 59   to use GROUP BY
26430 20 69 6e 73 74 65 61 64 20 6f 66 20 44 49 53 54   instead of DIST
26431 49 4e 43 54 2e 0a 20 20 2a 2a 20 47 52 4f 55 50  INCT..  ** GROUP
26432 20 42 59 20 6d 69 67 68 74 20 75 73 65 20 61 6e   BY might use an
26433 20 69 6e 64 65 78 2c 20 44 49 53 54 49 4e 43 54   index, DISTINCT
26434 20 6e 65 76 65 72 20 64 6f 65 73 2e 0a 20 20 2a   never does..  *
26435 2f 0a 20 20 69 66 28 20 28 70 2d 3e 73 65 6c 46  /.  if( (p->selF
26436 6c 61 67 73 20 26 20 28 53 46 5f 44 69 73 74 69  lags & (SF_Disti
26437 6e 63 74 7c 53 46 5f 41 67 67 72 65 67 61 74 65  nct|SF_Aggregate
26438 29 29 3d 3d 53 46 5f 44 69 73 74 69 6e 63 74 20  ))==SF_Distinct 
26439 26 26 20 21 70 2d 3e 70 47 72 6f 75 70 42 79 20  && !p->pGroupBy 
2643a 29 7b 0a 20 20 20 20 70 2d 3e 70 47 72 6f 75 70  ){.    p->pGroup
2643b 42 79 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  By = sqlite3Expr
2643c 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e 70  ListDup(db, p->p
2643d 45 4c 69 73 74 29 3b 0a 20 20 20 20 70 47 72 6f  EList);.    pGro
2643e 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75 70  upBy = p->pGroup
2643f 42 79 3b 0a 20 20 20 20 70 2d 3e 73 65 6c 46 6c  By;.    p->selFl
26440 61 67 73 20 26 3d 20 7e 53 46 5f 44 69 73 74 69  ags &= ~SF_Disti
26441 6e 63 74 3b 0a 20 20 20 20 69 73 44 69 73 74 69  nct;.    isDisti
26442 6e 63 74 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  nct = 0;.  }..  
26443 2f 2a 20 49 66 20 74 68 65 72 65 20 69 73 20 61  /* If there is a
26444 6e 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  n ORDER BY claus
26445 65 2c 20 74 68 65 6e 20 74 68 69 73 20 73 6f 72  e, then this sor
26446 74 69 6e 67 0a 20 20 2a 2a 20 69 6e 64 65 78 20  ting.  ** index 
26447 6d 69 67 68 74 20 65 6e 64 20 75 70 20 62 65 69  might end up bei
26448 6e 67 20 75 6e 75 73 65 64 20 69 66 20 74 68 65  ng unused if the
26449 20 64 61 74 61 20 63 61 6e 20 62 65 20 0a 20 20   data can be .  
2644a 2a 2a 20 65 78 74 72 61 63 74 65 64 20 69 6e 20  ** extracted in 
2644b 70 72 65 2d 73 6f 72 74 65 64 20 6f 72 64 65 72  pre-sorted order
2644c 2e 20 20 49 66 20 74 68 61 74 20 69 73 20 74 68  .  If that is th
2644d 65 20 63 61 73 65 2c 20 74 68 65 6e 20 74 68 65  e case, then the
2644e 0a 20 20 2a 2a 20 4f 50 5f 4f 70 65 6e 45 70 68  .  ** OP_OpenEph
2644f 65 6d 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69  emeral instructi
26450 6f 6e 20 77 69 6c 6c 20 62 65 20 63 68 61 6e 67  on will be chang
26451 65 64 20 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70  ed to an OP_Noop
26452 20 6f 6e 63 65 0a 20 20 2a 2a 20 77 65 20 66 69   once.  ** we fi
26453 67 75 72 65 20 6f 75 74 20 74 68 61 74 20 74 68  gure out that th
26454 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65 78 20  e sorting index 
26455 69 73 20 6e 6f 74 20 6e 65 65 64 65 64 2e 20 20  is not needed.  
26456 54 68 65 20 61 64 64 72 53 6f 72 74 49 6e 64 65  The addrSortInde
26457 78 0a 20 20 2a 2a 20 76 61 72 69 61 62 6c 65 20  x.  ** variable 
26458 69 73 20 75 73 65 64 20 74 6f 20 66 61 63 69 6c  is used to facil
26459 69 74 61 74 65 20 74 68 61 74 20 63 68 61 6e 67  itate that chang
2645a 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 4f  e..  */.  if( pO
2645b 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 4b 65  rderBy ){.    Ke
2645c 79 49 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b  yInfo *pKeyInfo;
2645d 0a 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20  .    pKeyInfo = 
2645e 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c  keyInfoFromExprL
2645f 69 73 74 28 70 50 61 72 73 65 2c 20 70 4f 72 64  ist(pParse, pOrd
26460 65 72 42 79 29 3b 0a 20 20 20 20 70 4f 72 64 65  erBy);.    pOrde
26461 72 42 79 2d 3e 69 45 43 75 72 73 6f 72 20 3d 20  rBy->iECursor = 
26462 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
26463 20 20 20 20 70 2d 3e 61 64 64 72 4f 70 65 6e 45      p->addrOpenE
26464 70 68 6d 5b 32 5d 20 3d 20 61 64 64 72 53 6f 72  phm[2] = addrSor
26465 74 49 6e 64 65 78 20 3d 0a 20 20 20 20 20 20 73  tIndex =.      s
26466 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
26467 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
26468 65 72 61 6c 2c 0a 20 20 20 20 20 20 20 20 20 20  eral,.          
26469 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2646a 20 70 4f 72 64 65 72 42 79 2d 3e 69 45 43 75 72   pOrderBy->iECur
2646b 73 6f 72 2c 20 70 4f 72 64 65 72 42 79 2d 3e 6e  sor, pOrderBy->n
2646c 45 78 70 72 2b 32 2c 20 30 2c 0a 20 20 20 20 20  Expr+2, 0,.     
2646d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2646e 20 20 20 20 20 20 28 63 68 61 72 2a 29 70 4b 65        (char*)pKe
2646f 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59 49 4e 46  yInfo, P4_KEYINF
26470 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 7d 65  O_HANDOFF);.  }e
26471 6c 73 65 7b 0a 20 20 20 20 61 64 64 72 53 6f 72  lse{.    addrSor
26472 74 49 6e 64 65 78 20 3d 20 2d 31 3b 0a 20 20 7d  tIndex = -1;.  }
26473 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6f 75  ..  /* If the ou
26474 74 70 75 74 20 69 73 20 64 65 73 74 69 6e 65 64  tput is destined
26475 20 66 6f 72 20 61 20 74 65 6d 70 6f 72 61 72 79   for a temporary
26476 20 74 61 62 6c 65 2c 20 6f 70 65 6e 20 74 68 61   table, open tha
26477 74 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20  t table..  */.  
26478 69 66 28 20 70 44 65 73 74 2d 3e 65 44 65 73 74  if( pDest->eDest
26479 3d 3d 53 52 54 5f 45 70 68 65 6d 54 61 62 20 29  ==SRT_EphemTab )
2647a 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  {.    sqlite3Vdb
2647b 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70  eAddOp2(v, OP_Op
2647c 65 6e 45 70 68 65 6d 65 72 61 6c 2c 20 70 44 65  enEphemeral, pDe
2647d 73 74 2d 3e 69 50 61 72 6d 2c 20 70 45 4c 69 73  st->iParm, pELis
2647e 74 2d 3e 6e 45 78 70 72 29 3b 0a 20 20 7d 0a 0a  t->nExpr);.  }..
2647f 20 20 2f 2a 20 53 65 74 20 74 68 65 20 6c 69 6d    /* Set the lim
26480 69 74 65 72 2e 0a 20 20 2a 2f 0a 20 20 69 45 6e  iter..  */.  iEn
26481 64 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d  d = sqlite3VdbeM
26482 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 63  akeLabel(v);.  c
26483 6f 6d 70 75 74 65 4c 69 6d 69 74 52 65 67 69 73  omputeLimitRegis
26484 74 65 72 73 28 70 50 61 72 73 65 2c 20 70 2c 20  ters(pParse, p, 
26485 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a 20 4f 70 65  iEnd);..  /* Ope
26486 6e 20 61 20 76 69 72 74 75 61 6c 20 69 6e 64 65  n a virtual inde
26487 78 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68 65  x to use for the
26488 20 64 69 73 74 69 6e 63 74 20 73 65 74 2e 0a 20   distinct set.. 
26489 20 2a 2f 0a 20 20 69 66 28 20 69 73 44 69 73 74   */.  if( isDist
2648a 69 6e 63 74 20 29 7b 0a 20 20 20 20 4b 65 79 49  inct ){.    KeyI
2648b 6e 66 6f 20 2a 70 4b 65 79 49 6e 66 6f 3b 0a 20  nfo *pKeyInfo;. 
2648c 20 20 20 61 73 73 65 72 74 28 20 69 73 41 67 67     assert( isAgg
2648d 20 7c 7c 20 70 47 72 6f 75 70 42 79 20 29 3b 0a   || pGroupBy );.
2648e 20 20 20 20 64 69 73 74 69 6e 63 74 20 3d 20 70      distinct = p
2648f 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20  Parse->nTab++;. 
26490 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d 20 6b 65     pKeyInfo = ke
26491 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72 4c 69 73  yInfoFromExprLis
26492 74 28 70 50 61 72 73 65 2c 20 70 2d 3e 70 45 4c  t(pParse, p->pEL
26493 69 73 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ist);.    sqlite
26494 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c 20 4f  3VdbeAddOp4(v, O
26495 50 5f 4f 70 65 6e 45 70 68 65 6d 65 72 61 6c 2c  P_OpenEphemeral,
26496 20 64 69 73 74 69 6e 63 74 2c 20 30 2c 20 30 2c   distinct, 0, 0,
26497 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
26498 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29           (char*)
26499 70 4b 65 79 49 6e 66 6f 2c 20 50 34 5f 4b 45 59  pKeyInfo, P4_KEY
2649a 49 4e 46 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20  INFO_HANDOFF);. 
2649b 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 69 73 74   }else{.    dist
2649c 69 6e 63 74 20 3d 20 2d 31 3b 0a 20 20 7d 0a 0a  inct = -1;.  }..
2649d 20 20 2f 2a 20 41 67 67 72 65 67 61 74 65 20 61    /* Aggregate a
2649e 6e 64 20 6e 6f 6e 2d 61 67 67 72 65 67 61 74 65  nd non-aggregate
2649f 20 71 75 65 72 69 65 73 20 61 72 65 20 68 61 6e   queries are han
264a0 64 6c 65 64 20 64 69 66 66 65 72 65 6e 74 6c 79  dled differently
264a1 20 2a 2f 0a 20 20 69 66 28 20 21 69 73 41 67 67   */.  if( !isAgg
264a2 20 26 26 20 70 47 72 6f 75 70 42 79 3d 3d 30 20   && pGroupBy==0 
264a3 29 7b 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 63  ){.    /* This c
264a4 61 73 65 20 69 73 20 66 6f 72 20 6e 6f 6e 2d 61  ase is for non-a
264a5 67 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73  ggregate queries
264a6 0a 20 20 20 20 2a 2a 20 42 65 67 69 6e 20 74 68  .    ** Begin th
264a7 65 20 64 61 74 61 62 61 73 65 20 73 63 61 6e 0a  e database scan.
264a8 20 20 20 20 2a 2f 0a 20 20 20 20 70 57 49 6e 66      */.    pWInf
264a9 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72 65  o = sqlite3Where
264aa 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70 54  Begin(pParse, pT
264ab 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c 20  abList, pWhere, 
264ac 26 70 4f 72 64 65 72 42 79 2c 20 30 29 3b 0a 20  &pOrderBy, 0);. 
264ad 20 20 20 69 66 28 20 70 57 49 6e 66 6f 3d 3d 30     if( pWInfo==0
264ae 20 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65   ) goto select_e
264af 6e 64 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 73  nd;..    /* If s
264b0 6f 72 74 69 6e 67 20 69 6e 64 65 78 20 74 68 61  orting index tha
264b1 74 20 77 61 73 20 63 72 65 61 74 65 64 20 62 79  t was created by
264b2 20 61 20 70 72 69 6f 72 20 4f 50 5f 4f 70 65 6e   a prior OP_Open
264b3 45 70 68 65 6d 65 72 61 6c 20 0a 20 20 20 20 2a  Ephemeral .    *
264b4 2a 20 69 6e 73 74 72 75 63 74 69 6f 6e 20 65 6e  * instruction en
264b5 64 65 64 20 75 70 20 6e 6f 74 20 62 65 69 6e 67  ded up not being
264b6 20 6e 65 65 64 65 64 2c 20 74 68 65 6e 20 63 68   needed, then ch
264b7 61 6e 67 65 20 74 68 65 20 4f 50 5f 4f 70 65 6e  ange the OP_Open
264b8 45 70 68 65 6d 65 72 61 6c 0a 20 20 20 20 2a 2a  Ephemeral.    **
264b9 20 69 6e 74 6f 20 61 6e 20 4f 50 5f 4e 6f 6f 70   into an OP_Noop
264ba 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28  ..    */.    if(
264bb 20 61 64 64 72 53 6f 72 74 49 6e 64 65 78 3e 3d   addrSortIndex>=
264bc 30 20 26 26 20 70 4f 72 64 65 72 42 79 3d 3d 30  0 && pOrderBy==0
264bd 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   ){.      sqlite
264be 33 56 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f  3VdbeChangeToNoo
264bf 70 28 76 2c 20 61 64 64 72 53 6f 72 74 49 6e 64  p(v, addrSortInd
264c0 65 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 70 2d  ex, 1);.      p-
264c1 3e 61 64 64 72 4f 70 65 6e 45 70 68 6d 5b 32 5d  >addrOpenEphm[2]
264c2 20 3d 20 2d 31 3b 0a 20 20 20 20 7d 0a 0a 20 20   = -1;.    }..  
264c3 20 20 2f 2a 20 55 73 65 20 74 68 65 20 73 74 61    /* Use the sta
264c4 6e 64 61 72 64 20 69 6e 6e 65 72 20 6c 6f 6f 70  ndard inner loop
264c5 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61 73 73 65  .    */.    asse
264c6 72 74 28 21 69 73 44 69 73 74 69 6e 63 74 29 3b  rt(!isDistinct);
264c7 0a 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72  .    selectInner
264c8 4c 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20  Loop(pParse, p, 
264c9 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20 70 4f  pEList, 0, 0, pO
264ca 72 64 65 72 42 79 2c 20 2d 31 2c 20 70 44 65 73  rderBy, -1, pDes
264cb 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
264cc 20 20 20 20 20 20 20 70 57 49 6e 66 6f 2d 3e 69         pWInfo->i
264cd 43 6f 6e 74 69 6e 75 65 2c 20 70 57 49 6e 66 6f  Continue, pWInfo
264ce 2d 3e 69 42 72 65 61 6b 29 3b 0a 0a 20 20 20 20  ->iBreak);..    
264cf 2f 2a 20 45 6e 64 20 74 68 65 20 64 61 74 61 62  /* End the datab
264d0 61 73 65 20 73 63 61 6e 20 6c 6f 6f 70 2e 0a 20  ase scan loop.. 
264d1 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
264d2 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66 6f  3WhereEnd(pWInfo
264d3 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
264d4 2f 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 70  /* This is the p
264d5 72 6f 63 65 73 73 69 6e 67 20 66 6f 72 20 61 67  rocessing for ag
264d6 67 72 65 67 61 74 65 20 71 75 65 72 69 65 73 20  gregate queries 
264d7 2a 2f 0a 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65  */.    NameConte
264d8 78 74 20 73 4e 43 3b 20 20 20 20 2f 2a 20 4e 61  xt sNC;    /* Na
264d9 6d 65 20 63 6f 6e 74 65 78 74 20 66 6f 72 20 70  me context for p
264da 72 6f 63 65 73 73 69 6e 67 20 61 67 67 72 65 67  rocessing aggreg
264db 61 74 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20  ate information 
264dc 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41 4d 65 6d  */.    int iAMem
264dd 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 69  ;          /* Fi
264de 72 73 74 20 4d 65 6d 20 61 64 64 72 65 73 73 20  rst Mem address 
264df 66 6f 72 20 73 74 6f 72 69 6e 67 20 63 75 72 72  for storing curr
264e0 65 6e 74 20 47 52 4f 55 50 20 42 59 20 2a 2f 0a  ent GROUP BY */.
264e1 20 20 20 20 69 6e 74 20 69 42 4d 65 6d 3b 20 20      int iBMem;  
264e2 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
264e3 20 4d 65 6d 20 61 64 64 72 65 73 73 20 66 6f 72   Mem address for
264e4 20 70 72 65 76 69 6f 75 73 20 47 52 4f 55 50 20   previous GROUP 
264e5 42 59 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 55  BY */.    int iU
264e6 73 65 46 6c 61 67 3b 20 20 20 20 20 20 20 2f 2a  seFlag;       /*
264e7 20 4d 65 6d 20 61 64 64 72 65 73 73 20 68 6f 6c   Mem address hol
264e8 64 69 6e 67 20 66 6c 61 67 20 69 6e 64 69 63 61  ding flag indica
264e9 74 69 6e 67 20 74 68 61 74 20 61 74 20 6c 65 61  ting that at lea
264ea 73 74 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  st.             
264eb 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e             ** on
264ec 65 20 72 6f 77 20 6f 66 20 74 68 65 20 69 6e 70  e row of the inp
264ed 75 74 20 74 6f 20 74 68 65 20 61 67 67 72 65 67  ut to the aggreg
264ee 61 74 6f 72 20 68 61 73 20 62 65 65 6e 0a 20 20  ator has been.  
264ef 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
264f0 20 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73        ** process
264f1 65 64 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 41  ed */.    int iA
264f2 62 6f 72 74 46 6c 61 67 3b 20 20 20 20 20 2f 2a  bortFlag;     /*
264f3 20 4d 65 6d 20 61 64 64 72 65 73 73 20 77 68 69   Mem address whi
264f4 63 68 20 63 61 75 73 65 73 20 71 75 65 72 79 20  ch causes query 
264f5 61 62 6f 72 74 20 69 66 20 70 6f 73 69 74 69 76  abort if positiv
264f6 65 20 2a 2f 0a 20 20 20 20 69 6e 74 20 67 72 6f  e */.    int gro
264f7 75 70 42 79 53 6f 72 74 3b 20 20 20 20 2f 2a 20  upBySort;    /* 
264f8 52 6f 77 73 20 63 6f 6d 65 20 66 72 6f 6d 20 73  Rows come from s
264f9 6f 75 72 63 65 20 69 6e 20 47 52 4f 55 50 20 42  ource in GROUP B
264fa 59 20 6f 72 64 65 72 20 2a 2f 0a 20 20 20 20 69  Y order */.    i
264fb 6e 74 20 61 64 64 72 45 6e 64 3b 20 20 20 20 20  nt addrEnd;     
264fc 20 20 20 2f 2a 20 45 6e 64 20 6f 66 20 70 72 6f     /* End of pro
264fd 63 65 73 73 69 6e 67 20 66 6f 72 20 74 68 69 73  cessing for this
264fe 20 53 45 4c 45 43 54 20 2a 2f 0a 0a 20 20 20 20   SELECT */..    
264ff 2f 2a 20 52 65 6d 6f 76 65 20 61 6e 79 20 61 6e  /* Remove any an
26500 64 20 61 6c 6c 20 61 6c 69 61 73 65 73 20 62 65  d all aliases be
26501 74 77 65 65 6e 20 74 68 65 20 72 65 73 75 6c 74  tween the result
26502 20 73 65 74 20 61 6e 64 20 74 68 65 0a 20 20 20   set and the.   
26503 20 2a 2a 20 47 52 4f 55 50 20 42 59 20 63 6c 61   ** GROUP BY cla
26504 75 73 65 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20  use..    */.    
26505 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b 0a  if( pGroupBy ){.
26506 20 20 20 20 20 20 69 6e 74 20 69 3b 20 20 20 20        int i;    
26507 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26508 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
26509 74 65 72 20 2a 2f 0a 20 20 20 20 20 20 73 74 72  ter */.      str
2650a 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
2650b 6d 20 2a 70 49 74 65 6d 3b 20 20 2f 2a 20 46 6f  m *pItem;  /* Fo
2650c 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65  r looping over e
2650d 78 70 72 65 73 73 69 6f 6e 20 69 6e 20 61 20 6c  xpression in a l
2650e 69 73 74 20 2a 2f 0a 0a 20 20 20 20 20 20 66 6f  ist */..      fo
2650f 72 28 69 3d 70 2d 3e 70 45 4c 69 73 74 2d 3e 6e  r(i=p->pEList->n
26510 45 78 70 72 2c 20 70 49 74 65 6d 3d 70 2d 3e 70  Expr, pItem=p->p
26511 45 4c 69 73 74 2d 3e 61 3b 20 69 3e 30 3b 20 69  EList->a; i>0; i
26512 2d 2d 2c 20 70 49 74 65 6d 2b 2b 29 7b 0a 20 20  --, pItem++){.  
26513 20 20 20 20 20 20 70 49 74 65 6d 2d 3e 69 41 6c        pItem->iAl
26514 69 61 73 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d  ias = 0;.      }
26515 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 70 47 72  .      for(i=pGr
26516 6f 75 70 42 79 2d 3e 6e 45 78 70 72 2c 20 70 49  oupBy->nExpr, pI
26517 74 65 6d 3d 70 47 72 6f 75 70 42 79 2d 3e 61 3b  tem=pGroupBy->a;
26518 20 69 3e 30 3b 20 69 2d 2d 2c 20 70 49 74 65 6d   i>0; i--, pItem
26519 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 70 49 74  ++){.        pIt
2651a 65 6d 2d 3e 69 41 6c 69 61 73 20 3d 20 30 3b 0a  em->iAlias = 0;.
2651b 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
2651c 0a 20 20 20 20 2f 2a 20 43 72 65 61 74 65 20 61  .    /* Create a
2651d 20 6c 61 62 65 6c 20 74 6f 20 6a 75 6d 70 20 74   label to jump t
2651e 6f 20 77 68 65 6e 20 77 65 20 77 61 6e 74 20 74  o when we want t
2651f 6f 20 61 62 6f 72 74 20 74 68 65 20 71 75 65 72  o abort the quer
26520 79 20 2a 2f 0a 20 20 20 20 61 64 64 72 45 6e 64  y */.    addrEnd
26521 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
26522 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a 20 20 20  keLabel(v);..   
26523 20 2f 2a 20 43 6f 6e 76 65 72 74 20 54 4b 5f 43   /* Convert TK_C
26524 4f 4c 55 4d 4e 20 6e 6f 64 65 73 20 69 6e 74 6f  OLUMN nodes into
26525 20 54 4b 5f 41 47 47 5f 43 4f 4c 55 4d 4e 20 61   TK_AGG_COLUMN a
26526 6e 64 20 6d 61 6b 65 20 65 6e 74 72 69 65 73 20  nd make entries 
26527 69 6e 0a 20 20 20 20 2a 2a 20 73 41 67 67 49 6e  in.    ** sAggIn
26528 66 6f 20 66 6f 72 20 61 6c 6c 20 54 4b 5f 41 47  fo for all TK_AG
26529 47 5f 46 55 4e 43 54 49 4f 4e 20 6e 6f 64 65 73  G_FUNCTION nodes
2652a 20 69 6e 20 65 78 70 72 65 73 73 69 6f 6e 73 20   in expressions 
2652b 6f 66 20 74 68 65 0a 20 20 20 20 2a 2a 20 53 45  of the.    ** SE
2652c 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 0a  LECT statement..
2652d 20 20 20 20 2a 2f 0a 20 20 20 20 6d 65 6d 73 65      */.    memse
2652e 74 28 26 73 4e 43 2c 20 30 2c 20 73 69 7a 65 6f  t(&sNC, 0, sizeo
2652f 66 28 73 4e 43 29 29 3b 0a 20 20 20 20 73 4e 43  f(sNC));.    sNC
26530 2e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  .pParse = pParse
26531 3b 0a 20 20 20 20 73 4e 43 2e 70 53 72 63 4c 69  ;.    sNC.pSrcLi
26532 73 74 20 3d 20 70 54 61 62 4c 69 73 74 3b 0a 20  st = pTabList;. 
26533 20 20 20 73 4e 43 2e 70 41 67 67 49 6e 66 6f 20     sNC.pAggInfo 
26534 3d 20 26 73 41 67 67 49 6e 66 6f 3b 0a 20 20 20  = &sAggInfo;.   
26535 20 73 41 67 67 49 6e 66 6f 2e 6e 53 6f 72 74 69   sAggInfo.nSorti
26536 6e 67 43 6f 6c 75 6d 6e 20 3d 20 70 47 72 6f 75  ngColumn = pGrou
26537 70 42 79 20 3f 20 70 47 72 6f 75 70 42 79 2d 3e  pBy ? pGroupBy->
26538 6e 45 78 70 72 2b 31 20 3a 20 30 3b 0a 20 20 20  nExpr+1 : 0;.   
26539 20 73 41 67 67 49 6e 66 6f 2e 70 47 72 6f 75 70   sAggInfo.pGroup
2653a 42 79 20 3d 20 70 47 72 6f 75 70 42 79 3b 0a 20  By = pGroupBy;. 
2653b 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41 6e     sqlite3ExprAn
2653c 61 6c 79 7a 65 41 67 67 4c 69 73 74 28 26 73 4e  alyzeAggList(&sN
2653d 43 2c 20 70 45 4c 69 73 74 29 3b 0a 20 20 20 20  C, pEList);.    
2653e 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
2653f 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20  zeAggList(&sNC, 
26540 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20 20 69  pOrderBy);.    i
26541 66 28 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20  f( pHaving ){.  
26542 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 41      sqlite3ExprA
26543 6e 61 6c 79 7a 65 41 67 67 72 65 67 61 74 65 73  nalyzeAggregates
26544 28 26 73 4e 43 2c 20 70 48 61 76 69 6e 67 29 3b  (&sNC, pHaving);
26545 0a 20 20 20 20 7d 0a 20 20 20 20 73 41 67 67 49  .    }.    sAggI
26546 6e 66 6f 2e 6e 41 63 63 75 6d 75 6c 61 74 6f 72  nfo.nAccumulator
26547 20 3d 20 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c   = sAggInfo.nCol
26548 75 6d 6e 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30  umn;.    for(i=0
26549 3b 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 46 75  ; i<sAggInfo.nFu
2654a 6e 63 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  nc; i++){.      
2654b 73 71 6c 69 74 65 33 45 78 70 72 41 6e 61 6c 79  sqlite3ExprAnaly
2654c 7a 65 41 67 67 4c 69 73 74 28 26 73 4e 43 2c 20  zeAggList(&sNC, 
2654d 73 41 67 67 49 6e 66 6f 2e 61 46 75 6e 63 5b 69  sAggInfo.aFunc[i
2654e 5d 2e 70 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b  ].pExpr->pList);
2654f 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 64  .    }.    if( d
26550 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20  b->mallocFailed 
26551 29 20 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e  ) goto select_en
26552 64 3b 0a 0a 20 20 20 20 2f 2a 20 50 72 6f 63 65  d;..    /* Proce
26553 73 73 69 6e 67 20 66 6f 72 20 61 67 67 72 65 67  ssing for aggreg
26554 61 74 65 73 20 77 69 74 68 20 47 52 4f 55 50 20  ates with GROUP 
26555 42 59 20 69 73 20 76 65 72 79 20 64 69 66 66 65  BY is very diffe
26556 72 65 6e 74 20 61 6e 64 0a 20 20 20 20 2a 2a 20  rent and.    ** 
26557 6d 75 63 68 20 6d 6f 72 65 20 63 6f 6d 70 6c 65  much more comple
26558 78 20 74 68 61 6e 20 61 67 67 72 65 67 61 74 65  x than aggregate
26559 73 20 77 69 74 68 6f 75 74 20 61 20 47 52 4f 55  s without a GROU
2655a 50 20 42 59 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  P BY..    */.   
2655b 20 69 66 28 20 70 47 72 6f 75 70 42 79 20 29 7b   if( pGroupBy ){
2655c 0a 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20 2a  .      KeyInfo *
2655d 70 4b 65 79 49 6e 66 6f 3b 20 20 2f 2a 20 4b 65  pKeyInfo;  /* Ke
2655e 79 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  ying information
2655f 20 66 6f 72 20 74 68 65 20 67 72 6f 75 70 20 62   for the group b
26560 79 20 63 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20  y clause */.    
26561 20 20 69 6e 74 20 6a 31 3b 20 20 20 20 20 20 20    int j1;       
26562 20 20 20 20 20 20 2f 2a 20 41 2d 76 73 2d 42 20        /* A-vs-B 
26563 63 6f 6d 70 61 72 69 73 69 6f 6e 20 6a 75 6d 70  comparision jump
26564 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
26565 64 72 4f 75 74 70 75 74 52 6f 77 3b 20 20 2f 2a  drOutputRow;  /*
26566 20 53 74 61 72 74 20 6f 66 20 73 75 62 72 6f 75   Start of subrou
26567 74 69 6e 65 20 74 68 61 74 20 6f 75 74 70 75 74  tine that output
26568 73 20 61 20 72 65 73 75 6c 74 20 72 6f 77 20 2a  s a result row *
26569 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65 67 4f  /.      int regO
2656a 75 74 70 75 74 52 6f 77 3b 20 20 20 2f 2a 20 52  utputRow;   /* R
2656b 65 74 75 72 6e 20 61 64 64 72 65 73 73 20 72 65  eturn address re
2656c 67 69 73 74 65 72 20 66 6f 72 20 6f 75 74 70 75  gister for outpu
2656d 74 20 73 75 62 72 6f 75 74 69 6e 65 20 2a 2f 0a  t subroutine */.
2656e 20 20 20 20 20 20 69 6e 74 20 61 64 64 72 53 65        int addrSe
2656f 74 41 62 6f 72 74 3b 20 20 20 2f 2a 20 53 65 74  tAbort;   /* Set
26570 20 74 68 65 20 61 62 6f 72 74 20 66 6c 61 67 20   the abort flag 
26571 61 6e 64 20 72 65 74 75 72 6e 20 2a 2f 0a 20 20  and return */.  
26572 20 20 20 20 69 6e 74 20 61 64 64 72 54 6f 70 4f      int addrTopO
26573 66 4c 6f 6f 70 3b 20 20 2f 2a 20 54 6f 70 20 6f  fLoop;  /* Top o
26574 66 20 74 68 65 20 69 6e 70 75 74 20 6c 6f 6f 70  f the input loop
26575 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 61 64   */.      int ad
26576 64 72 53 6f 72 74 69 6e 67 49 64 78 3b 20 2f 2a  drSortingIdx; /*
26577 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68 65   The OP_OpenEphe
26578 6d 65 72 61 6c 20 66 6f 72 20 74 68 65 20 73 6f  meral for the so
26579 72 74 69 6e 67 20 69 6e 64 65 78 20 2a 2f 0a 20  rting index */. 
2657a 20 20 20 20 20 69 6e 74 20 61 64 64 72 52 65 73       int addrRes
2657b 65 74 3b 20 20 20 20 20 20 2f 2a 20 53 75 62 72  et;      /* Subr
2657c 6f 75 74 69 6e 65 20 66 6f 72 20 72 65 73 65 74  outine for reset
2657d 74 69 6e 67 20 74 68 65 20 61 63 63 75 6d 75 6c  ting the accumul
2657e 61 74 6f 72 20 2a 2f 0a 20 20 20 20 20 20 69 6e  ator */.      in
2657f 74 20 72 65 67 52 65 73 65 74 3b 20 20 20 20 20  t regReset;     
26580 20 20 2f 2a 20 52 65 74 75 72 6e 20 61 64 64 72    /* Return addr
26581 65 73 73 20 72 65 67 69 73 74 65 72 20 66 6f 72  ess register for
26582 20 72 65 73 65 74 20 73 75 62 72 6f 75 74 69 6e   reset subroutin
26583 65 20 2a 2f 0a 0a 20 20 20 20 20 20 2f 2a 20 49  e */..      /* I
26584 66 20 74 68 65 72 65 20 69 73 20 61 20 47 52 4f  f there is a GRO
26585 55 50 20 42 59 20 63 6c 61 75 73 65 20 77 65 20  UP BY clause we 
26586 6d 69 67 68 74 20 6e 65 65 64 20 61 20 73 6f 72  might need a sor
26587 74 69 6e 67 20 69 6e 64 65 78 20 74 6f 0a 20 20  ting index to.  
26588 20 20 20 20 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74      ** implement
26589 20 69 74 2e 20 20 41 6c 6c 6f 63 61 74 65 20 74   it.  Allocate t
2658a 68 61 74 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  hat sorting inde
2658b 78 20 6e 6f 77 2e 20 20 49 66 20 69 74 20 74 75  x now.  If it tu
2658c 72 6e 73 20 6f 75 74 0a 20 20 20 20 20 20 2a 2a  rns out.      **
2658d 20 74 68 61 74 20 77 65 20 64 6f 20 6e 6f 74 20   that we do not 
2658e 6e 65 65 64 20 69 74 20 61 66 74 65 72 20 61 6c  need it after al
2658f 6c 2c 20 74 68 65 20 4f 70 65 6e 45 70 68 65 6d  l, the OpenEphem
26590 65 72 61 6c 20 69 6e 73 74 72 75 63 74 69 6f 6e  eral instruction
26591 0a 20 20 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62  .      ** will b
26592 65 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f  e converted into
26593 20 61 20 4e 6f 6f 70 2e 20 20 0a 20 20 20 20 20   a Noop.  .     
26594 20 2a 2f 0a 20 20 20 20 20 20 73 41 67 67 49 6e   */.      sAggIn
26595 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 20 3d 20  fo.sortingIdx = 
26596 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
26597 20 20 20 20 20 20 70 4b 65 79 49 6e 66 6f 20 3d        pKeyInfo =
26598 20 6b 65 79 49 6e 66 6f 46 72 6f 6d 45 78 70 72   keyInfoFromExpr
26599 4c 69 73 74 28 70 50 61 72 73 65 2c 20 70 47 72  List(pParse, pGr
2659a 6f 75 70 42 79 29 3b 0a 20 20 20 20 20 20 61 64  oupBy);.      ad
2659b 64 72 53 6f 72 74 69 6e 67 49 64 78 20 3d 20 73  drSortingIdx = s
2659c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
2659d 28 76 2c 20 4f 50 5f 4f 70 65 6e 45 70 68 65 6d  (v, OP_OpenEphem
2659e 65 72 61 6c 2c 20 0a 20 20 20 20 20 20 20 20 20  eral, .         
2659f 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
265a0 67 49 64 78 2c 20 73 41 67 67 49 6e 66 6f 2e 6e  gIdx, sAggInfo.n
265a1 53 6f 72 74 69 6e 67 43 6f 6c 75 6d 6e 2c 20 0a  SortingColumn, .
265a2 20 20 20 20 20 20 20 20 20 20 30 2c 20 28 63 68            0, (ch
265a3 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50 34  ar*)pKeyInfo, P4
265a4 5f 4b 45 59 49 4e 46 4f 5f 48 41 4e 44 4f 46 46  _KEYINFO_HANDOFF
265a5 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 49 6e 69  );..      /* Ini
265a6 74 69 61 6c 69 7a 65 20 6d 65 6d 6f 72 79 20 6c  tialize memory l
265a7 6f 63 61 74 69 6f 6e 73 20 75 73 65 64 20 62 79  ocations used by
265a8 20 47 52 4f 55 50 20 42 59 20 61 67 67 72 65 67   GROUP BY aggreg
265a9 61 74 65 20 70 72 6f 63 65 73 73 69 6e 67 0a 20  ate processing. 
265aa 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 55       */.      iU
265ab 73 65 46 6c 61 67 20 3d 20 2b 2b 70 50 61 72 73  seFlag = ++pPars
265ac 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 69  e->nMem;.      i
265ad 41 62 6f 72 74 46 6c 61 67 20 3d 20 2b 2b 70 50  AbortFlag = ++pP
265ae 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20 20 20  arse->nMem;.    
265af 20 20 72 65 67 4f 75 74 70 75 74 52 6f 77 20 3d    regOutputRow =
265b0 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
265b1 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75  .      addrOutpu
265b2 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64  tRow = sqlite3Vd
265b3 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a  beMakeLabel(v);.
265b4 20 20 20 20 20 20 72 65 67 52 65 73 65 74 20 3d        regReset =
265b5 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
265b6 0a 20 20 20 20 20 20 61 64 64 72 52 65 73 65 74  .      addrReset
265b7 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 4d 61   = sqlite3VdbeMa
265b8 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 20 20 20 20  keLabel(v);.    
265b9 20 20 69 41 4d 65 6d 20 3d 20 70 50 61 72 73 65    iAMem = pParse
265ba 2d 3e 6e 4d 65 6d 20 2b 20 31 3b 0a 20 20 20 20  ->nMem + 1;.    
265bb 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b    pParse->nMem +
265bc 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78 70  = pGroupBy->nExp
265bd 72 3b 0a 20 20 20 20 20 20 69 42 4d 65 6d 20 3d  r;.      iBMem =
265be 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20 2b 20   pParse->nMem + 
265bf 31 3b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d  1;.      pParse-
265c0 3e 6e 4d 65 6d 20 2b 3d 20 70 47 72 6f 75 70 42  >nMem += pGroupB
265c1 79 2d 3e 6e 45 78 70 72 3b 0a 20 20 20 20 20 20  y->nExpr;.      
265c2 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
265c3 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
265c4 20 30 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b   0, iAbortFlag);
265c5 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
265c6 6e 74 28 28 76 2c 20 22 63 6c 65 61 72 20 61 62  nt((v, "clear ab
265c7 6f 72 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20  ort flag"));.   
265c8 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
265c9 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67  dOp2(v, OP_Integ
265ca 65 72 2c 20 30 2c 20 69 55 73 65 46 6c 61 67 29  er, 0, iUseFlag)
265cb 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d  ;.      VdbeComm
265cc 65 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61 74  ent((v, "indicat
265cd 65 20 61 63 63 75 6d 75 6c 61 74 6f 72 20 65 6d  e accumulator em
265ce 70 74 79 22 29 29 3b 0a 0a 20 20 20 20 20 20 2f  pty"));..      /
265cf 2a 20 42 65 67 69 6e 20 61 20 6c 6f 6f 70 20 74  * Begin a loop t
265d0 68 61 74 20 77 69 6c 6c 20 65 78 74 72 61 63 74  hat will extract
265d1 20 61 6c 6c 20 73 6f 75 72 63 65 20 72 6f 77 73   all source rows
265d2 20 69 6e 20 47 52 4f 55 50 20 42 59 20 6f 72 64   in GROUP BY ord
265d3 65 72 2e 0a 20 20 20 20 20 20 2a 2a 20 54 68 69  er..      ** Thi
265d4 73 20 6d 69 67 68 74 20 69 6e 76 6f 6c 76 65 20  s might involve 
265d5 74 77 6f 20 73 65 70 61 72 61 74 65 20 6c 6f 6f  two separate loo
265d6 70 73 20 77 69 74 68 20 61 6e 20 4f 50 5f 53 6f  ps with an OP_So
265d7 72 74 20 69 6e 20 62 65 74 77 65 65 6e 2c 20 6f  rt in between, o
265d8 72 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 6d 69  r.      ** it mi
265d9 67 68 74 20 62 65 20 61 20 73 69 6e 67 6c 65 20  ght be a single 
265da 6c 6f 6f 70 20 74 68 61 74 20 75 73 65 73 20 61  loop that uses a
265db 6e 20 69 6e 64 65 78 20 74 6f 20 65 78 74 72 61  n index to extra
265dc 63 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 0a 20  ct information. 
265dd 20 20 20 20 20 2a 2a 20 69 6e 20 74 68 65 20 72       ** in the r
265de 69 67 68 74 20 6f 72 64 65 72 20 74 6f 20 62 65  ight order to be
265df 67 69 6e 20 77 69 74 68 2e 0a 20 20 20 20 20 20  gin with..      
265e0 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  */.      sqlite3
265e1 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
265e2 5f 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74  _Gosub, regReset
265e3 2c 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20  , addrReset);.  
265e4 20 20 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c      pWInfo = sql
265e5 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 70  ite3WhereBegin(p
265e6 50 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c  Parse, pTabList,
265e7 20 70 57 68 65 72 65 2c 20 26 70 47 72 6f 75 70   pWhere, &pGroup
265e8 42 79 2c 20 30 29 3b 0a 20 20 20 20 20 20 69 66  By, 0);.      if
265e9 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f  ( pWInfo==0 ) go
265ea 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b 0a 20  to select_end;. 
265eb 20 20 20 20 20 69 66 28 20 70 47 72 6f 75 70 42       if( pGroupB
265ec 79 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  y==0 ){.        
265ed 2f 2a 20 54 68 65 20 6f 70 74 69 6d 69 7a 65 72  /* The optimizer
265ee 20 69 73 20 61 62 6c 65 20 74 6f 20 64 65 6c 69   is able to deli
265ef 76 65 72 20 72 6f 77 73 20 69 6e 20 67 72 6f 75  ver rows in grou
265f0 70 20 62 79 20 6f 72 64 65 72 20 73 6f 0a 20 20  p by order so.  
265f1 20 20 20 20 20 20 2a 2a 20 77 65 20 64 6f 20 6e        ** we do n
265f2 6f 74 20 68 61 76 65 20 74 6f 20 73 6f 72 74 2e  ot have to sort.
265f3 20 20 54 68 65 20 4f 50 5f 4f 70 65 6e 45 70 68    The OP_OpenEph
265f4 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 77 69 6c  emeral table wil
265f5 6c 20 62 65 0a 20 20 20 20 20 20 20 20 2a 2a 20  l be.        ** 
265f6 63 61 6e 63 65 6c 6c 65 64 20 6c 61 74 65 72 20  cancelled later 
265f7 62 65 63 61 75 73 65 20 77 65 20 73 74 69 6c 6c  because we still
265f8 20 6e 65 65 64 20 74 6f 20 75 73 65 20 74 68 65   need to use the
265f9 20 70 4b 65 79 49 6e 66 6f 0a 20 20 20 20 20 20   pKeyInfo.      
265fa 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 47 72    */.        pGr
265fb 6f 75 70 42 79 20 3d 20 70 2d 3e 70 47 72 6f 75  oupBy = p->pGrou
265fc 70 42 79 3b 0a 20 20 20 20 20 20 20 20 67 72 6f  pBy;.        gro
265fd 75 70 42 79 53 6f 72 74 20 3d 20 30 3b 0a 20 20  upBySort = 0;.  
265fe 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
265ff 20 20 20 2f 2a 20 52 6f 77 73 20 61 72 65 20 63     /* Rows are c
26600 6f 6d 69 6e 67 20 6f 75 74 20 69 6e 20 75 6e 64  oming out in und
26601 65 74 65 72 6d 69 6e 65 64 20 6f 72 64 65 72 2e  etermined order.
26602 20 20 57 65 20 68 61 76 65 20 74 6f 20 70 75 73    We have to pus
26603 68 0a 20 20 20 20 20 20 20 20 2a 2a 20 65 61 63  h.        ** eac
26604 68 20 72 6f 77 20 69 6e 74 6f 20 61 20 73 6f 72  h row into a sor
26605 74 69 6e 67 20 69 6e 64 65 78 2c 20 74 65 72 6d  ting index, term
26606 69 6e 61 74 65 20 74 68 65 20 66 69 72 73 74 20  inate the first 
26607 6c 6f 6f 70 2c 0a 20 20 20 20 20 20 20 20 2a 2a  loop,.        **
26608 20 74 68 65 6e 20 6c 6f 6f 70 20 6f 76 65 72 20   then loop over 
26609 74 68 65 20 73 6f 72 74 69 6e 67 20 69 6e 64 65  the sorting inde
2660a 78 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 67 65  x in order to ge
2660b 74 20 74 68 65 20 6f 75 74 70 75 74 0a 20 20 20  t the output.   
2660c 20 20 20 20 20 2a 2a 20 69 6e 20 73 6f 72 74 65       ** in sorte
2660d 64 20 6f 72 64 65 72 0a 20 20 20 20 20 20 20 20  d order.        
2660e 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74 20 72  */.        int r
2660f 65 67 42 61 73 65 3b 0a 20 20 20 20 20 20 20 20  egBase;.        
26610 69 6e 74 20 72 65 67 52 65 63 6f 72 64 3b 0a 20  int regRecord;. 
26611 20 20 20 20 20 20 20 69 6e 74 20 6e 43 6f 6c 3b         int nCol;
26612 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 47 72  .        int nGr
26613 6f 75 70 42 79 3b 0a 0a 20 20 20 20 20 20 20 20  oupBy;..        
26614 67 72 6f 75 70 42 79 53 6f 72 74 20 3d 20 31 3b  groupBySort = 1;
26615 0a 20 20 20 20 20 20 20 20 6e 47 72 6f 75 70 42  .        nGroupB
26616 79 20 3d 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45  y = pGroupBy->nE
26617 78 70 72 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f  xpr;.        nCo
26618 6c 20 3d 20 6e 47 72 6f 75 70 42 79 20 2b 20 31  l = nGroupBy + 1
26619 3b 0a 20 20 20 20 20 20 20 20 6a 20 3d 20 6e 47  ;.        j = nG
2661a 72 6f 75 70 42 79 2b 31 3b 0a 20 20 20 20 20 20  roupBy+1;.      
2661b 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 73 41 67    for(i=0; i<sAg
2661c 67 49 6e 66 6f 2e 6e 43 6f 6c 75 6d 6e 3b 20 69  gInfo.nColumn; i
2661d 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  ++){.          i
2661e 66 28 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c  f( sAggInfo.aCol
2661f 5b 69 5d 2e 69 53 6f 72 74 65 72 43 6f 6c 75 6d  [i].iSorterColum
26620 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20 20 20 20  n>=j ){.        
26621 20 20 20 20 6e 43 6f 6c 2b 2b 3b 0a 20 20 20 20      nCol++;.    
26622 20 20 20 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20          j++;.   
26623 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
26624 20 7d 0a 20 20 20 20 20 20 20 20 72 65 67 42 61   }.        regBa
26625 73 65 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  se = sqlite3GetT
26626 65 6d 70 52 61 6e 67 65 28 70 50 61 72 73 65 2c  empRange(pParse,
26627 20 6e 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20   nCol);.        
26628 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 45  sqlite3ExprCodeE
26629 78 70 72 4c 69 73 74 28 70 50 61 72 73 65 2c 20  xprList(pParse, 
2662a 70 47 72 6f 75 70 42 79 2c 20 72 65 67 42 61 73  pGroupBy, regBas
2662b 65 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 73  e, 0);.        s
2662c 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2662d 28 76 2c 20 4f 50 5f 53 65 71 75 65 6e 63 65 2c  (v, OP_Sequence,
2662e 20 73 41 67 67 49 6e 66 6f 2e 73 6f 72 74 69 6e   sAggInfo.sortin
2662f 67 49 64 78 2c 72 65 67 42 61 73 65 2b 6e 47 72  gIdx,regBase+nGr
26630 6f 75 70 42 79 29 3b 0a 20 20 20 20 20 20 20 20  oupBy);.        
26631 6a 20 3d 20 6e 47 72 6f 75 70 42 79 2b 31 3b 0a  j = nGroupBy+1;.
26632 20 20 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b          for(i=0;
26633 20 69 3c 73 41 67 67 49 6e 66 6f 2e 6e 43 6f 6c   i<sAggInfo.nCol
26634 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20  umn; i++){.     
26635 20 20 20 20 20 73 74 72 75 63 74 20 41 67 67 49       struct AggI
26636 6e 66 6f 5f 63 6f 6c 20 2a 70 43 6f 6c 20 3d 20  nfo_col *pCol = 
26637 26 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 5b 69  &sAggInfo.aCol[i
26638 5d 3b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ];.          if(
26639 20 70 43 6f 6c 2d 3e 69 53 6f 72 74 65 72 43 6f   pCol->iSorterCo
2663a 6c 75 6d 6e 3e 3d 6a 20 29 7b 0a 20 20 20 20 20  lumn>=j ){.     
2663b 20 20 20 20 20 20 20 69 6e 74 20 72 31 20 3d 20         int r1 = 
2663c 6a 20 2b 20 72 65 67 42 61 73 65 3b 0a 20 20 20  j + regBase;.   
2663d 20 20 20 20 20 20 20 20 20 69 6e 74 20 72 32 3b           int r2;
2663e 0a 0a 20 20 20 20 20 20 20 20 20 20 20 20 72 32  ..            r2
2663f 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f   = sqlite3ExprCo
26640 64 65 47 65 74 43 6f 6c 75 6d 6e 28 70 50 61 72  deGetColumn(pPar
26641 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  se, .           
26642 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26643 20 20 20 20 70 43 6f 6c 2d 3e 70 54 61 62 2c 20      pCol->pTab, 
26644 70 43 6f 6c 2d 3e 69 43 6f 6c 75 6d 6e 2c 20 70  pCol->iColumn, p
26645 43 6f 6c 2d 3e 69 54 61 62 6c 65 2c 20 72 31 2c  Col->iTable, r1,
26646 20 30 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20   0);.           
26647 20 69 66 28 20 72 31 21 3d 72 32 20 29 7b 0a 20   if( r1!=r2 ){. 
26648 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
26649 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2664a 2c 20 4f 50 5f 53 43 6f 70 79 2c 20 72 32 2c 20  , OP_SCopy, r2, 
2664b 72 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  r1);.           
2664c 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 6a   }.            j
2664d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 0a  ++;.          }.
2664e 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2664f 20 20 72 65 67 52 65 63 6f 72 64 20 3d 20 73 71    regRecord = sq
26650 6c 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28  lite3GetTempReg(
26651 70 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20  pParse);.       
26652 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
26653 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65 63  p3(v, OP_MakeRec
26654 6f 72 64 2c 20 72 65 67 42 61 73 65 2c 20 6e 43  ord, regBase, nC
26655 6f 6c 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a  ol, regRecord);.
26656 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
26657 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26658 49 64 78 49 6e 73 65 72 74 2c 20 73 41 67 67 49  IdxInsert, sAggI
26659 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78 2c 20  nfo.sortingIdx, 
2665a 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20 20 20  regRecord);.    
2665b 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
2665c 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
2665d 2c 20 72 65 67 52 65 63 6f 72 64 29 3b 0a 20 20  , regRecord);.  
2665e 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
2665f 65 61 73 65 54 65 6d 70 52 61 6e 67 65 28 70 50  easeTempRange(pP
26660 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c 20 6e  arse, regBase, n
26661 43 6f 6c 29 3b 0a 20 20 20 20 20 20 20 20 73 71  Col);.        sq
26662 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 70 57  lite3WhereEnd(pW
26663 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20 20 73  Info);.        s
26664 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
26665 28 76 2c 20 4f 50 5f 53 6f 72 74 2c 20 73 41 67  (v, OP_Sort, sAg
26666 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
26667 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20 20 20 20  , addrEnd);.    
26668 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28      VdbeComment(
26669 28 76 2c 20 22 47 52 4f 55 50 20 42 59 20 73 6f  (v, "GROUP BY so
2666a 72 74 22 29 29 3b 0a 20 20 20 20 20 20 20 20 73  rt"));.        s
2666b 41 67 67 49 6e 66 6f 2e 75 73 65 53 6f 72 74 69  AggInfo.useSorti
2666c 6e 67 49 64 78 20 3d 20 31 3b 0a 20 20 20 20 20  ngIdx = 1;.     
2666d 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 45 76 61   }..      /* Eva
2666e 6c 75 61 74 65 20 74 68 65 20 63 75 72 72 65 6e  luate the curren
2666f 74 20 47 52 4f 55 50 20 42 59 20 74 65 72 6d 73  t GROUP BY terms
26670 20 61 6e 64 20 73 74 6f 72 65 20 69 6e 20 62 30   and store in b0
26671 2c 20 62 31 2c 20 62 32 2e 2e 2e 0a 20 20 20 20  , b1, b2....    
26672 20 20 2a 2a 20 28 62 30 20 69 73 20 6d 65 6d 6f    ** (b0 is memo
26673 72 79 20 6c 6f 63 61 74 69 6f 6e 20 69 42 4d 65  ry location iBMe
26674 6d 2b 30 2c 20 62 31 20 69 73 20 69 42 4d 65 6d  m+0, b1 is iBMem
26675 2b 31 2c 20 61 6e 64 20 73 6f 20 66 6f 72 74 68  +1, and so forth
26676 29 0a 20 20 20 20 20 20 2a 2a 20 54 68 65 6e 20  ).      ** Then 
26677 63 6f 6d 70 61 72 65 20 74 68 65 20 63 75 72 72  compare the curr
26678 65 6e 74 20 47 52 4f 55 50 20 42 59 20 74 65 72  ent GROUP BY ter
26679 6d 73 20 61 67 61 69 6e 73 74 20 74 68 65 20 47  ms against the G
2667a 52 4f 55 50 20 42 59 20 74 65 72 6d 73 0a 20 20  ROUP BY terms.  
2667b 20 20 20 20 2a 2a 20 66 72 6f 6d 20 74 68 65 20      ** from the 
2667c 70 72 65 76 69 6f 75 73 20 72 6f 77 20 63 75 72  previous row cur
2667d 72 65 6e 74 6c 79 20 73 74 6f 72 65 64 20 69 6e  rently stored in
2667e 20 61 30 2c 20 61 31 2c 20 61 32 2e 2e 2e 0a 20   a0, a1, a2.... 
2667f 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 61 64       */.      ad
26680 64 72 54 6f 70 4f 66 4c 6f 6f 70 20 3d 20 73 71  drTopOfLoop = sq
26681 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
26682 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 66  Addr(v);.      f
26683 6f 72 28 6a 3d 30 3b 20 6a 3c 70 47 72 6f 75 70  or(j=0; j<pGroup
26684 42 79 2d 3e 6e 45 78 70 72 3b 20 6a 2b 2b 29 7b  By->nExpr; j++){
26685 0a 20 20 20 20 20 20 20 20 69 66 28 20 67 72 6f  .        if( gro
26686 75 70 42 79 53 6f 72 74 20 29 7b 0a 20 20 20 20  upBySort ){.    
26687 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
26688 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43 6f  eAddOp3(v, OP_Co
26689 6c 75 6d 6e 2c 20 73 41 67 67 49 6e 66 6f 2e 73  lumn, sAggInfo.s
2668a 6f 72 74 69 6e 67 49 64 78 2c 20 6a 2c 20 69 42  ortingIdx, j, iB
2668b 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20 20 20 20 20  Mem+j);.        
2668c 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20  }else{.         
2668d 20 73 41 67 67 49 6e 66 6f 2e 64 69 72 65 63 74   sAggInfo.direct
2668e 4d 6f 64 65 20 3d 20 31 3b 0a 20 20 20 20 20 20  Mode = 1;.      
2668f 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 43      sqlite3ExprC
26690 6f 64 65 28 70 50 61 72 73 65 2c 20 70 47 72 6f  ode(pParse, pGro
26691 75 70 42 79 2d 3e 61 5b 6a 5d 2e 70 45 78 70 72  upBy->a[j].pExpr
26692 2c 20 69 42 4d 65 6d 2b 6a 29 3b 0a 20 20 20 20  , iBMem+j);.    
26693 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
26694 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26695 64 64 4f 70 34 28 76 2c 20 4f 50 5f 43 6f 6d 70  ddOp4(v, OP_Comp
26696 61 72 65 2c 20 69 41 4d 65 6d 2c 20 69 42 4d 65  are, iAMem, iBMe
26697 6d 2c 20 70 47 72 6f 75 70 42 79 2d 3e 6e 45 78  m, pGroupBy->nEx
26698 70 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  pr,.            
26699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 63                (c
2669a 68 61 72 2a 29 70 4b 65 79 49 6e 66 6f 2c 20 50  har*)pKeyInfo, P
2669b 34 5f 4b 45 59 49 4e 46 4f 29 3b 0a 20 20 20 20  4_KEYINFO);.    
2669c 20 20 6a 31 20 3d 20 73 71 6c 69 74 65 33 56 64    j1 = sqlite3Vd
2669d 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
2669e 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
2669f 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
266a0 4a 75 6d 70 2c 20 6a 31 2b 31 2c 20 30 2c 20 6a  Jump, j1+1, 0, j
266a1 31 2b 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  1+1);..      /* 
266a2 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
266a3 61 74 20 72 75 6e 73 20 77 68 65 6e 65 76 65 72  at runs whenever
266a4 20 74 68 65 20 47 52 4f 55 50 20 42 59 20 63 68   the GROUP BY ch
266a5 61 6e 67 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20  anges..      ** 
266a6 43 68 61 6e 67 65 73 20 69 6e 20 74 68 65 20 47  Changes in the G
266a7 52 4f 55 50 20 42 59 20 61 72 65 20 64 65 74 65  ROUP BY are dete
266a8 63 74 65 64 20 62 79 20 74 68 65 20 70 72 65 76  cted by the prev
266a9 69 6f 75 73 20 63 6f 64 65 0a 20 20 20 20 20 20  ious code.      
266aa 2a 2a 20 62 6c 6f 63 6b 2e 20 20 49 66 20 74 68  ** block.  If th
266ab 65 72 65 20 77 65 72 65 20 6e 6f 20 63 68 61 6e  ere were no chan
266ac 67 65 73 2c 20 74 68 69 73 20 62 6c 6f 63 6b 20  ges, this block 
266ad 69 73 20 73 6b 69 70 70 65 64 2e 0a 20 20 20 20  is skipped..    
266ae 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54 68    **.      ** Th
266af 69 73 20 63 6f 64 65 20 63 6f 70 69 65 73 20 63  is code copies c
266b0 75 72 72 65 6e 74 20 67 72 6f 75 70 20 62 79 20  urrent group by 
266b1 74 65 72 6d 73 20 69 6e 20 62 30 2c 62 31 2c 62  terms in b0,b1,b
266b2 32 2c 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 20 6f  2,....      ** o
266b3 76 65 72 20 74 6f 20 61 30 2c 61 31 2c 61 32 2e  ver to a0,a1,a2.
266b4 20 20 49 74 20 74 68 65 6e 20 63 61 6c 6c 73 20    It then calls 
266b5 74 68 65 20 6f 75 74 70 75 74 20 73 75 62 72 6f  the output subro
266b6 75 74 69 6e 65 0a 20 20 20 20 20 20 2a 2a 20 61  utine.      ** a
266b7 6e 64 20 72 65 73 65 74 73 20 74 68 65 20 61 67  nd resets the ag
266b8 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c 61  gregate accumula
266b9 74 6f 72 20 72 65 67 69 73 74 65 72 73 20 69 6e  tor registers in
266ba 20 70 72 65 70 61 72 61 74 69 6f 6e 0a 20 20 20   preparation.   
266bb 20 20 20 2a 2a 20 66 6f 72 20 74 68 65 20 6e 65     ** for the ne
266bc 78 74 20 47 52 4f 55 50 20 42 59 20 62 61 74 63  xt GROUP BY batc
266bd 68 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  h..      */.    
266be 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
266bf 65 4d 6f 76 65 28 70 50 61 72 73 65 2c 20 69 42  eMove(pParse, iB
266c0 4d 65 6d 2c 20 69 41 4d 65 6d 2c 20 70 47 72 6f  Mem, iAMem, pGro
266c1 75 70 42 79 2d 3e 6e 45 78 70 72 29 3b 0a 20 20  upBy->nExpr);.  
266c2 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
266c3 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 73 75  ddOp2(v, OP_Gosu
266c4 62 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 2c  b, regOutputRow,
266c5 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b   addrOutputRow);
266c6 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
266c7 6e 74 28 28 76 2c 20 22 6f 75 74 70 75 74 20 6f  nt((v, "output o
266c8 6e 65 20 72 6f 77 22 29 29 3b 0a 20 20 20 20 20  ne row"));.     
266c9 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
266ca 70 32 28 76 2c 20 4f 50 5f 49 66 50 6f 73 2c 20  p2(v, OP_IfPos, 
266cb 69 41 62 6f 72 74 46 6c 61 67 2c 20 61 64 64 72  iAbortFlag, addr
266cc 45 6e 64 29 3b 0a 20 20 20 20 20 20 56 64 62 65  End);.      Vdbe
266cd 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 63 68 65  Comment((v, "che
266ce 63 6b 20 61 62 6f 72 74 20 66 6c 61 67 22 29 29  ck abort flag"))
266cf 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
266d0 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
266d1 47 6f 73 75 62 2c 20 72 65 67 52 65 73 65 74 2c  Gosub, regReset,
266d2 20 61 64 64 72 52 65 73 65 74 29 3b 0a 20 20 20   addrReset);.   
266d3 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28     VdbeComment((
266d4 76 2c 20 22 72 65 73 65 74 20 61 63 63 75 6d 75  v, "reset accumu
266d5 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20 20  lator"));..     
266d6 20 2f 2a 20 55 70 64 61 74 65 20 74 68 65 20 61   /* Update the a
266d7 67 67 72 65 67 61 74 65 20 61 63 63 75 6d 75 6c  ggregate accumul
266d8 61 74 6f 72 73 20 62 61 73 65 64 20 6f 6e 20 74  ators based on t
266d9 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 0a 20 20  he content of.  
266da 20 20 20 20 2a 2a 20 74 68 65 20 63 75 72 72 65      ** the curre
266db 6e 74 20 72 6f 77 0a 20 20 20 20 20 20 2a 2f 0a  nt row.      */.
266dc 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
266dd 65 4a 75 6d 70 48 65 72 65 28 76 2c 20 6a 31 29  eJumpHere(v, j1)
266de 3b 0a 20 20 20 20 20 20 75 70 64 61 74 65 41 63  ;.      updateAc
266df 63 75 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65  cumulator(pParse
266e0 2c 20 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20  , &sAggInfo);.  
266e1 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
266e2 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
266e3 67 65 72 2c 20 31 2c 20 69 55 73 65 46 6c 61 67  ger, 1, iUseFlag
266e4 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
266e5 6d 65 6e 74 28 28 76 2c 20 22 69 6e 64 69 63 61  ment((v, "indica
266e6 74 65 20 64 61 74 61 20 69 6e 20 61 63 63 75 6d  te data in accum
266e7 75 6c 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20  ulator"));..    
266e8 20 20 2f 2a 20 45 6e 64 20 6f 66 20 74 68 65 20    /* End of the 
266e9 6c 6f 6f 70 0a 20 20 20 20 20 20 2a 2f 0a 20 20  loop.      */.  
266ea 20 20 20 20 69 66 28 20 67 72 6f 75 70 42 79 53      if( groupByS
266eb 6f 72 74 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ort ){.        s
266ec 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
266ed 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20 73 41 67  (v, OP_Next, sAg
266ee 67 49 6e 66 6f 2e 73 6f 72 74 69 6e 67 49 64 78  gInfo.sortingIdx
266ef 2c 20 61 64 64 72 54 6f 70 4f 66 4c 6f 6f 70 29  , addrTopOfLoop)
266f0 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
266f1 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 57 68         sqlite3Wh
266f2 65 72 65 45 6e 64 28 70 57 49 6e 66 6f 29 3b 0a  ereEnd(pWInfo);.
266f3 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
266f4 64 62 65 43 68 61 6e 67 65 54 6f 4e 6f 6f 70 28  dbeChangeToNoop(
266f5 76 2c 20 61 64 64 72 53 6f 72 74 69 6e 67 49 64  v, addrSortingId
266f6 78 2c 20 31 29 3b 0a 20 20 20 20 20 20 7d 0a 0a  x, 1);.      }..
266f7 20 20 20 20 20 20 2f 2a 20 4f 75 74 70 75 74 20        /* Output 
266f8 74 68 65 20 66 69 6e 61 6c 20 72 6f 77 20 6f 66  the final row of
266f9 20 72 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2f   result.      */
266fa 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
266fb 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47  beAddOp2(v, OP_G
266fc 6f 73 75 62 2c 20 72 65 67 4f 75 74 70 75 74 52  osub, regOutputR
266fd 6f 77 2c 20 61 64 64 72 4f 75 74 70 75 74 52 6f  ow, addrOutputRo
266fe 77 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f  w);.      VdbeCo
266ff 6d 6d 65 6e 74 28 28 76 2c 20 22 6f 75 74 70 75  mment((v, "outpu
26700 74 20 66 69 6e 61 6c 20 72 6f 77 22 29 29 3b 0a  t final row"));.
26701 0a 20 20 20 20 20 20 2f 2a 20 4a 75 6d 70 20 6f  .      /* Jump o
26702 76 65 72 20 74 68 65 20 73 75 62 72 6f 75 74 69  ver the subrouti
26703 6e 65 73 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  nes.      */.   
26704 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
26705 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c  dOp2(v, OP_Goto,
26706 20 30 2c 20 61 64 64 72 45 6e 64 29 3b 0a 0a 20   0, addrEnd);.. 
26707 20 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65       /* Generate
26708 20 61 20 73 75 62 72 6f 75 74 69 6e 65 20 74 68   a subroutine th
26709 61 74 20 6f 75 74 70 75 74 73 20 61 20 73 69 6e  at outputs a sin
2670a 67 6c 65 20 72 6f 77 20 6f 66 20 74 68 65 20 72  gle row of the r
2670b 65 73 75 6c 74 0a 20 20 20 20 20 20 2a 2a 20 73  esult.      ** s
2670c 65 74 2e 20 20 54 68 69 73 20 73 75 62 72 6f 75  et.  This subrou
2670d 74 69 6e 65 20 66 69 72 73 74 20 6c 6f 6f 6b 73  tine first looks
2670e 20 61 74 20 74 68 65 20 69 55 73 65 46 6c 61 67   at the iUseFlag
2670f 2e 20 20 49 66 20 69 55 73 65 46 6c 61 67 0a 20  .  If iUseFlag. 
26710 20 20 20 20 20 2a 2a 20 69 73 20 6c 65 73 73 20       ** is less 
26711 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74 6f  than or equal to
26712 20 7a 65 72 6f 2c 20 74 68 65 20 73 75 62 72 6f   zero, the subro
26713 75 74 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70  utine is a no-op
26714 2e 20 20 49 66 0a 20 20 20 20 20 20 2a 2a 20 74  .  If.      ** t
26715 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 63 61  he processing ca
26716 6c 6c 73 20 66 6f 72 20 74 68 65 20 71 75 65 72  lls for the quer
26717 79 20 74 6f 20 61 62 6f 72 74 2c 20 74 68 69 73  y to abort, this
26718 20 73 75 62 72 6f 75 74 69 6e 65 0a 20 20 20 20   subroutine.    
26719 20 20 2a 2a 20 69 6e 63 72 65 6d 65 6e 74 73 20    ** increments 
2671a 74 68 65 20 69 41 62 6f 72 74 46 6c 61 67 20 6d  the iAbortFlag m
2671b 65 6d 6f 72 79 20 6c 6f 63 61 74 69 6f 6e 20 62  emory location b
2671c 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20  efore returning 
2671d 69 6e 0a 20 20 20 20 20 20 2a 2a 20 6f 72 64 65  in.      ** orde
2671e 72 20 74 6f 20 73 69 67 6e 61 6c 20 74 68 65 20  r to signal the 
2671f 63 61 6c 6c 65 72 20 74 6f 20 61 62 6f 72 74 2e  caller to abort.
26720 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
26721 61 64 64 72 53 65 74 41 62 6f 72 74 20 3d 20 73  addrSetAbort = s
26722 71 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e  qlite3VdbeCurren
26723 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20  tAddr(v);.      
26724 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
26725 32 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c  2(v, OP_Integer,
26726 20 31 2c 20 69 41 62 6f 72 74 46 6c 61 67 29 3b   1, iAbortFlag);
26727 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65  .      VdbeComme
26728 6e 74 28 28 76 2c 20 22 73 65 74 20 61 62 6f 72  nt((v, "set abor
26729 74 20 66 6c 61 67 22 29 29 3b 0a 20 20 20 20 20  t flag"));.     
2672a 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
2672b 70 31 28 76 2c 20 4f 50 5f 52 65 74 75 72 6e 2c  p1(v, OP_Return,
2672c 20 72 65 67 4f 75 74 70 75 74 52 6f 77 29 3b 0a   regOutputRow);.
2672d 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
2672e 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
2672f 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 29 3b   addrOutputRow);
26730 0a 20 20 20 20 20 20 61 64 64 72 4f 75 74 70 75  .      addrOutpu
26731 74 52 6f 77 20 3d 20 73 71 6c 69 74 65 33 56 64  tRow = sqlite3Vd
26732 62 65 43 75 72 72 65 6e 74 41 64 64 72 28 76 29  beCurrentAddr(v)
26733 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ;.      sqlite3V
26734 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
26735 49 66 50 6f 73 2c 20 69 55 73 65 46 6c 61 67 2c  IfPos, iUseFlag,
26736 20 61 64 64 72 4f 75 74 70 75 74 52 6f 77 2b 32   addrOutputRow+2
26737 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
26738 6d 65 6e 74 28 28 76 2c 20 22 47 72 6f 75 70 62  ment((v, "Groupb
26739 79 20 72 65 73 75 6c 74 20 67 65 6e 65 72 61 74  y result generat
2673a 6f 72 20 65 6e 74 72 79 20 70 6f 69 6e 74 22 29  or entry point")
2673b 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
2673c 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
2673d 5f 52 65 74 75 72 6e 2c 20 72 65 67 4f 75 74 70  _Return, regOutp
2673e 75 74 52 6f 77 29 3b 0a 20 20 20 20 20 20 66 69  utRow);.      fi
2673f 6e 61 6c 69 7a 65 41 67 67 46 75 6e 63 74 69 6f  nalizeAggFunctio
26740 6e 73 28 70 50 61 72 73 65 2c 20 26 73 41 67 67  ns(pParse, &sAgg
26741 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28  Info);.      if(
26742 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20   pHaving ){.    
26743 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 49      sqlite3ExprI
26744 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20 70  fFalse(pParse, p
26745 48 61 76 69 6e 67 2c 20 61 64 64 72 4f 75 74 70  Having, addrOutp
26746 75 74 52 6f 77 2b 31 2c 20 53 51 4c 49 54 45 5f  utRow+1, SQLITE_
26747 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20  JUMPIFNULL);.   
26748 20 20 20 7d 0a 20 20 20 20 20 20 73 65 6c 65 63     }.      selec
26749 74 49 6e 6e 65 72 4c 6f 6f 70 28 70 50 61 72 73  tInnerLoop(pPars
2674a 65 2c 20 70 2c 20 70 2d 3e 70 45 4c 69 73 74 2c  e, p, p->pEList,
2674b 20 30 2c 20 30 2c 20 70 4f 72 64 65 72 42 79 2c   0, 0, pOrderBy,
2674c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2674d 20 20 20 20 20 20 20 64 69 73 74 69 6e 63 74 2c         distinct,
2674e 20 70 44 65 73 74 2c 0a 20 20 20 20 20 20 20 20   pDest,.        
2674f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61 64                ad
26750 64 72 4f 75 74 70 75 74 52 6f 77 2b 31 2c 20 61  drOutputRow+1, a
26751 64 64 72 53 65 74 41 62 6f 72 74 29 3b 0a 20 20  ddrSetAbort);.  
26752 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
26753 64 64 4f 70 31 28 76 2c 20 4f 50 5f 52 65 74 75  ddOp1(v, OP_Retu
26754 72 6e 2c 20 72 65 67 4f 75 74 70 75 74 52 6f 77  rn, regOutputRow
26755 29 3b 0a 20 20 20 20 20 20 56 64 62 65 43 6f 6d  );.      VdbeCom
26756 6d 65 6e 74 28 28 76 2c 20 22 65 6e 64 20 67 72  ment((v, "end gr
26757 6f 75 70 62 79 20 72 65 73 75 6c 74 20 67 65 6e  oupby result gen
26758 65 72 61 74 6f 72 22 29 29 3b 0a 0a 20 20 20 20  erator"));..    
26759 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20 61 20    /* Generate a 
2675a 73 75 62 72 6f 75 74 69 6e 65 20 74 68 61 74 20  subroutine that 
2675b 77 69 6c 6c 20 72 65 73 65 74 20 74 68 65 20 67  will reset the g
2675c 72 6f 75 70 2d 62 79 20 61 63 63 75 6d 75 6c 61  roup-by accumula
2675d 74 6f 72 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20  tor.      */.   
2675e 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65     sqlite3VdbeRe
2675f 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64  solveLabel(v, ad
26760 64 72 52 65 73 65 74 29 3b 0a 20 20 20 20 20 20  drReset);.      
26761 72 65 73 65 74 41 63 63 75 6d 75 6c 61 74 6f 72  resetAccumulator
26762 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
26763 66 6f 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  fo);.      sqlit
26764 65 33 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20  e3VdbeAddOp1(v, 
26765 4f 50 5f 52 65 74 75 72 6e 2c 20 72 65 67 52 65  OP_Return, regRe
26766 73 65 74 29 3b 0a 20 20 20 20 20 0a 20 20 20 20  set);.     .    
26767 7d 20 2f 2a 20 65 6e 64 69 66 20 70 47 72 6f 75  } /* endif pGrou
26768 70 42 79 20 2a 2f 0a 20 20 20 20 65 6c 73 65 20  pBy */.    else 
26769 7b 0a 20 20 20 20 20 20 45 78 70 72 4c 69 73 74  {.      ExprList
2676a 20 2a 70 4d 69 6e 4d 61 78 20 3d 20 30 3b 0a 20   *pMinMax = 0;. 
2676b 20 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70       ExprList *p
2676c 44 65 6c 20 3d 20 30 3b 0a 20 20 20 20 20 20 75  Del = 0;.      u
2676d 38 20 66 6c 61 67 3b 0a 0a 20 20 20 20 20 20 2f  8 flag;..      /
2676e 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 71  * Check if the q
2676f 75 65 72 79 20 69 73 20 6f 66 20 6f 6e 65 20 6f  uery is of one o
26770 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
26771 66 6f 72 6d 73 3a 0a 20 20 20 20 20 20 2a 2a 0a  forms:.      **.
26772 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c 45 43        **   SELEC
26773 54 20 6d 69 6e 28 78 29 20 46 52 4f 4d 20 2e 2e  T min(x) FROM ..
26774 2e 0a 20 20 20 20 20 20 2a 2a 20 20 20 53 45 4c  ..      **   SEL
26775 45 43 54 20 6d 61 78 28 78 29 20 46 52 4f 4d 20  ECT max(x) FROM 
26776 2e 2e 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  ....      **.   
26777 20 20 20 2a 2a 20 49 66 20 69 74 20 69 73 2c 20     ** If it is, 
26778 74 68 65 6e 20 61 73 6b 20 74 68 65 20 63 6f 64  then ask the cod
26779 65 20 69 6e 20 77 68 65 72 65 2e 63 20 74 6f 20  e in where.c to 
2677a 61 74 74 65 6d 70 74 20 74 6f 20 73 6f 72 74 20  attempt to sort 
2677b 72 65 73 75 6c 74 73 0a 20 20 20 20 20 20 2a 2a  results.      **
2677c 20 61 73 20 69 66 20 74 68 65 72 65 20 77 61 73   as if there was
2677d 20 61 6e 20 22 4f 52 44 45 52 20 4f 4e 20 78 22   an "ORDER ON x"
2677e 20 6f 72 20 22 4f 52 44 45 52 20 4f 4e 20 78 20   or "ORDER ON x 
2677f 44 45 53 43 22 20 63 6c 61 75 73 65 2e 20 0a 20  DESC" clause. . 
26780 20 20 20 20 20 2a 2a 20 49 66 20 77 68 65 72 65       ** If where
26781 2e 63 20 69 73 20 61 62 6c 65 20 74 6f 20 70 72  .c is able to pr
26782 6f 64 75 63 65 20 72 65 73 75 6c 74 73 20 73 6f  oduce results so
26783 72 74 65 64 20 69 6e 20 74 68 69 73 20 6f 72 64  rted in this ord
26784 65 72 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 2a  er, then.      *
26785 2a 20 61 64 64 20 76 64 62 65 20 63 6f 64 65 20  * add vdbe code 
26786 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f 66 20  to break out of 
26787 74 68 65 20 70 72 6f 63 65 73 73 69 6e 67 20 6c  the processing l
26788 6f 6f 70 20 61 66 74 65 72 20 74 68 65 20 0a 20  oop after the . 
26789 20 20 20 20 20 2a 2a 20 66 69 72 73 74 20 69 74       ** first it
2678a 65 72 61 74 69 6f 6e 20 28 73 69 6e 63 65 20 74  eration (since t
2678b 68 65 20 66 69 72 73 74 20 69 74 65 72 61 74 69  he first iterati
2678c 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 20 69  on of the loop i
2678d 73 20 0a 20 20 20 20 20 20 2a 2a 20 67 75 61 72  s .      ** guar
2678e 61 6e 74 65 65 64 20 74 6f 20 6f 70 65 72 61 74  anteed to operat
2678f 65 20 6f 6e 20 74 68 65 20 72 6f 77 20 77 69 74  e on the row wit
26790 68 20 74 68 65 20 6d 69 6e 69 6d 75 6d 20 6f 72  h the minimum or
26791 20 6d 61 78 69 6d 75 6d 20 0a 20 20 20 20 20 20   maximum .      
26792 2a 2a 20 76 61 6c 75 65 20 6f 66 20 78 2c 20 74  ** value of x, t
26793 68 65 20 6f 6e 6c 79 20 72 6f 77 20 72 65 71 75  he only row requ
26794 69 72 65 64 29 2e 0a 20 20 20 20 20 20 2a 2a 0a  ired)..      **.
26795 20 20 20 20 20 20 2a 2a 20 41 20 73 70 65 63 69        ** A speci
26796 61 6c 20 66 6c 61 67 20 6d 75 73 74 20 62 65 20  al flag must be 
26797 70 61 73 73 65 64 20 74 6f 20 73 71 6c 69 74 65  passed to sqlite
26798 33 57 68 65 72 65 42 65 67 69 6e 28 29 20 74 6f  3WhereBegin() to
26799 20 73 6c 69 67 68 74 6c 79 0a 20 20 20 20 20 20   slightly.      
2679a 2a 2a 20 6d 6f 64 69 66 79 20 62 65 68 61 76 69  ** modify behavi
2679b 6f 75 72 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a  our as follows:.
2679c 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2679d 2a 20 20 20 2b 20 49 66 20 74 68 65 20 71 75 65  *   + If the que
2679e 72 79 20 69 73 20 61 20 22 53 45 4c 45 43 54 20  ry is a "SELECT 
2679f 6d 69 6e 28 78 29 22 2c 20 74 68 65 6e 20 74 68  min(x)", then th
267a0 65 20 6c 6f 6f 70 20 63 6f 64 65 64 20 62 79 0a  e loop coded by.
267a1 20 20 20 20 20 20 2a 2a 20 20 20 20 20 77 68 65        **     whe
267a2 72 65 2e 63 20 73 68 6f 75 6c 64 20 6e 6f 74 20  re.c should not 
267a3 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6e 79  iterate over any
267a4 20 76 61 6c 75 65 73 20 77 69 74 68 20 61 20 4e   values with a N
267a5 55 4c 4c 20 76 61 6c 75 65 0a 20 20 20 20 20 20  ULL value.      
267a6 2a 2a 20 20 20 20 20 66 6f 72 20 78 2e 0a 20 20  **     for x..  
267a7 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20      **.      ** 
267a8 20 20 2b 20 54 68 65 20 6f 70 74 69 6d 69 7a 65    + The optimize
267a9 72 20 63 6f 64 65 20 69 6e 20 77 68 65 72 65 2e  r code in where.
267aa 63 20 28 74 68 65 20 74 68 69 6e 67 20 74 68 61  c (the thing tha
267ab 74 20 64 65 63 69 64 65 73 20 77 68 69 63 68 0a  t decides which.
267ac 20 20 20 20 20 20 2a 2a 20 20 20 20 20 69 6e 64        **     ind
267ad 65 78 20 6f 72 20 69 6e 64 69 63 65 73 20 74 6f  ex or indices to
267ae 20 75 73 65 29 20 73 68 6f 75 6c 64 20 70 6c 61   use) should pla
267af 63 65 20 61 20 64 69 66 66 65 72 65 6e 74 20 70  ce a different p
267b0 72 69 6f 72 69 74 79 20 6f 6e 20 0a 20 20 20 20  riority on .    
267b1 20 20 2a 2a 20 20 20 20 20 73 61 74 69 73 66 79    **     satisfy
267b2 69 6e 67 20 74 68 65 20 27 4f 52 44 45 52 20 42  ing the 'ORDER B
267b3 59 27 20 63 6c 61 75 73 65 20 74 68 61 6e 20 69  Y' clause than i
267b4 74 20 64 6f 65 73 20 69 6e 20 6f 74 68 65 72 20  t does in other 
267b5 63 61 73 65 73 2e 0a 20 20 20 20 20 20 2a 2a 20  cases..      ** 
267b6 20 20 20 20 52 65 66 65 72 20 74 6f 20 63 6f 64      Refer to cod
267b7 65 20 61 6e 64 20 63 6f 6d 6d 65 6e 74 73 20 69  e and comments i
267b8 6e 20 77 68 65 72 65 2e 63 20 66 6f 72 20 64 65  n where.c for de
267b9 74 61 69 6c 73 2e 0a 20 20 20 20 20 20 2a 2f 0a  tails..      */.
267ba 20 20 20 20 20 20 66 6c 61 67 20 3d 20 6d 69 6e        flag = min
267bb 4d 61 78 51 75 65 72 79 28 70 29 3b 0a 20 20 20  MaxQuery(p);.   
267bc 20 20 20 69 66 28 20 66 6c 61 67 20 29 7b 0a 20     if( flag ){. 
267bd 20 20 20 20 20 20 20 70 44 65 6c 20 3d 20 70 4d         pDel = pM
267be 69 6e 4d 61 78 20 3d 20 73 71 6c 69 74 65 33 45  inMax = sqlite3E
267bf 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70  xprListDup(db, p
267c0 2d 3e 70 45 4c 69 73 74 2d 3e 61 5b 30 5d 2e 70  ->pEList->a[0].p
267c1 45 78 70 72 2d 3e 70 4c 69 73 74 29 3b 0a 20 20  Expr->pList);.  
267c2 20 20 20 20 20 20 69 66 28 20 70 4d 69 6e 4d 61        if( pMinMa
267c3 78 20 26 26 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  x && !db->malloc
267c4 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
267c5 20 20 20 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30      pMinMax->a[0
267c6 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d 20 66 6c  ].sortOrder = fl
267c7 61 67 21 3d 57 48 45 52 45 5f 4f 52 44 45 52 42  ag!=WHERE_ORDERB
267c8 59 5f 4d 49 4e 3b 0a 20 20 20 20 20 20 20 20 20  Y_MIN;.         
267c9 20 70 4d 69 6e 4d 61 78 2d 3e 61 5b 30 5d 2e 70   pMinMax->a[0].p
267ca 45 78 70 72 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f  Expr->op = TK_CO
267cb 4c 55 4d 4e 3b 0a 20 20 20 20 20 20 20 20 7d 0a  LUMN;.        }.
267cc 20 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f        }..      /
267cd 2a 20 54 68 69 73 20 63 61 73 65 20 72 75 6e 73  * This case runs
267ce 20 69 66 20 74 68 65 20 61 67 67 72 65 67 61 74   if the aggregat
267cf 65 20 68 61 73 20 6e 6f 20 47 52 4f 55 50 20 42  e has no GROUP B
267d0 59 20 63 6c 61 75 73 65 2e 20 20 54 68 65 0a 20  Y clause.  The. 
267d1 20 20 20 20 20 2a 2a 20 70 72 6f 63 65 73 73 69       ** processi
267d2 6e 67 20 69 73 20 6d 75 63 68 20 73 69 6d 70 6c  ng is much simpl
267d3 65 72 20 73 69 6e 63 65 20 74 68 65 72 65 20 69  er since there i
267d4 73 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c 65 20  s only a single 
267d5 72 6f 77 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20  row.      ** of 
267d6 6f 75 74 70 75 74 2e 0a 20 20 20 20 20 20 2a 2f  output..      */
267d7 0a 20 20 20 20 20 20 72 65 73 65 74 41 63 63 75  .      resetAccu
267d8 6d 75 6c 61 74 6f 72 28 70 50 61 72 73 65 2c 20  mulator(pParse, 
267d9 26 73 41 67 67 49 6e 66 6f 29 3b 0a 20 20 20 20  &sAggInfo);.    
267da 20 20 70 57 49 6e 66 6f 20 3d 20 73 71 6c 69 74    pWInfo = sqlit
267db 65 33 57 68 65 72 65 42 65 67 69 6e 28 70 50 61  e3WhereBegin(pPa
267dc 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
267dd 57 68 65 72 65 2c 20 26 70 4d 69 6e 4d 61 78 2c  Where, &pMinMax,
267de 20 66 6c 61 67 29 3b 0a 20 20 20 20 20 20 69 66   flag);.      if
267df 28 20 70 57 49 6e 66 6f 3d 3d 30 20 29 7b 0a 20  ( pWInfo==0 ){. 
267e0 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
267e1 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  prListDelete(db,
267e2 20 70 44 65 6c 29 3b 0a 20 20 20 20 20 20 20 20   pDel);.        
267e3 67 6f 74 6f 20 73 65 6c 65 63 74 5f 65 6e 64 3b  goto select_end;
267e4 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 75  .      }.      u
267e5 70 64 61 74 65 41 63 63 75 6d 75 6c 61 74 6f 72  pdateAccumulator
267e6 28 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e  (pParse, &sAggIn
267e7 66 6f 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21  fo);.      if( !
267e8 70 4d 69 6e 4d 61 78 20 26 26 20 66 6c 61 67 20  pMinMax && flag 
267e9 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
267ea 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
267eb 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 70 57 49 6e  OP_Goto, 0, pWIn
267ec 66 6f 2d 3e 69 42 72 65 61 6b 29 3b 0a 20 20 20  fo->iBreak);.   
267ed 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
267ee 28 28 76 2c 20 22 25 73 28 29 20 62 79 20 69 6e  ((v, "%s() by in
267ef 64 65 78 22 2c 28 66 6c 61 67 3d 3d 57 48 45 52  dex",(flag==WHER
267f0 45 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 3f 22 6d  E_ORDERBY_MIN?"m
267f1 69 6e 22 3a 22 6d 61 78 22 29 29 29 3b 0a 20 20  in":"max")));.  
267f2 20 20 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69      }.      sqli
267f3 74 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e  te3WhereEnd(pWIn
267f4 66 6f 29 3b 0a 20 20 20 20 20 20 66 69 6e 61 6c  fo);.      final
267f5 69 7a 65 41 67 67 46 75 6e 63 74 69 6f 6e 73 28  izeAggFunctions(
267f6 70 50 61 72 73 65 2c 20 26 73 41 67 67 49 6e 66  pParse, &sAggInf
267f7 6f 29 3b 0a 20 20 20 20 20 20 70 4f 72 64 65 72  o);.      pOrder
267f8 42 79 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66  By = 0;.      if
267f9 28 20 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20  ( pHaving ){.   
267fa 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
267fb 49 66 46 61 6c 73 65 28 70 50 61 72 73 65 2c 20  IfFalse(pParse, 
267fc 70 48 61 76 69 6e 67 2c 20 61 64 64 72 45 6e 64  pHaving, addrEnd
267fd 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e  , SQLITE_JUMPIFN
267fe 55 4c 4c 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ULL);.      }.  
267ff 20 20 20 20 73 65 6c 65 63 74 49 6e 6e 65 72 4c      selectInnerL
26800 6f 6f 70 28 70 50 61 72 73 65 2c 20 70 2c 20 70  oop(pParse, p, p
26801 2d 3e 70 45 4c 69 73 74 2c 20 30 2c 20 30 2c 20  ->pEList, 0, 0, 
26802 30 2c 20 2d 31 2c 20 0a 20 20 20 20 20 20 20 20  0, -1, .        
26803 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44                pD
26804 65 73 74 2c 20 61 64 64 72 45 6e 64 2c 20 61 64  est, addrEnd, ad
26805 64 72 45 6e 64 29 3b 0a 0a 20 20 20 20 20 20 73  drEnd);..      s
26806 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
26807 6c 65 74 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a  lete(db, pDel);.
26808 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
26809 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65  3VdbeResolveLabe
2680a 6c 28 76 2c 20 61 64 64 72 45 6e 64 29 3b 0a 20  l(v, addrEnd);. 
2680b 20 20 20 0a 20 20 7d 20 2f 2a 20 65 6e 64 69 66     .  } /* endif
2680c 20 61 67 67 72 65 67 61 74 65 20 71 75 65 72 79   aggregate query
2680d 20 2a 2f 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65   */..  /* If the
2680e 72 65 20 69 73 20 61 6e 20 4f 52 44 45 52 20 42  re is an ORDER B
2680f 59 20 63 6c 61 75 73 65 2c 20 74 68 65 6e 20 77  Y clause, then w
26810 65 20 6e 65 65 64 20 74 6f 20 73 6f 72 74 20 74  e need to sort t
26811 68 65 20 72 65 73 75 6c 74 73 0a 20 20 2a 2a 20  he results.  ** 
26812 61 6e 64 20 73 65 6e 64 20 74 68 65 6d 20 74 6f  and send them to
26813 20 74 68 65 20 63 61 6c 6c 62 61 63 6b 20 6f 6e   the callback on
26814 65 20 62 79 20 6f 6e 65 2e 0a 20 20 2a 2f 0a 20  e by one..  */. 
26815 20 69 66 28 20 70 4f 72 64 65 72 42 79 20 29 7b   if( pOrderBy ){
26816 0a 20 20 20 20 67 65 6e 65 72 61 74 65 53 6f 72  .    generateSor
26817 74 54 61 69 6c 28 70 50 61 72 73 65 2c 20 70 2c  tTail(pParse, p,
26818 20 76 2c 20 70 45 4c 69 73 74 2d 3e 6e 45 78 70   v, pEList->nExp
26819 72 2c 20 70 44 65 73 74 29 3b 0a 20 20 7d 0a 0a  r, pDest);.  }..
2681a 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 74    /* Jump here t
2681b 6f 20 73 6b 69 70 20 74 68 69 73 20 71 75 65 72  o skip this quer
2681c 79 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  y.  */.  sqlite3
2681d 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c  VdbeResolveLabel
2681e 28 76 2c 20 69 45 6e 64 29 3b 0a 0a 20 20 2f 2a  (v, iEnd);..  /*
2681f 20 54 68 65 20 53 45 4c 45 43 54 20 77 61 73 20   The SELECT was 
26820 73 75 63 63 65 73 73 66 75 6c 6c 79 20 63 6f 64  successfully cod
26821 65 64 2e 20 20 20 53 65 74 20 74 68 65 20 72 65  ed.   Set the re
26822 74 75 72 6e 20 63 6f 64 65 20 74 6f 20 30 0a 20  turn code to 0. 
26823 20 2a 2a 20 74 6f 20 69 6e 64 69 63 61 74 65 20   ** to indicate 
26824 6e 6f 20 65 72 72 6f 72 73 2e 0a 20 20 2a 2f 0a  no errors..  */.
26825 20 20 72 63 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20    rc = 0;..  /* 
26826 43 6f 6e 74 72 6f 6c 20 6a 75 6d 70 73 20 74 6f  Control jumps to
26827 20 68 65 72 65 20 69 66 20 61 6e 20 65 72 72 6f   here if an erro
26828 72 20 69 73 20 65 6e 63 6f 75 6e 74 65 72 65 64  r is encountered
26829 20 61 62 6f 76 65 2c 20 6f 72 20 75 70 6f 6e 0a   above, or upon.
2682a 20 20 2a 2a 20 73 75 63 63 65 73 73 66 75 6c 20    ** successful 
2682b 63 6f 64 69 6e 67 20 6f 66 20 74 68 65 20 53 45  coding of the SE
2682c 4c 45 43 54 2e 0a 20 20 2a 2f 0a 73 65 6c 65 63  LECT..  */.selec
2682d 74 5f 65 6e 64 3a 0a 0a 20 20 2f 2a 20 49 64 65  t_end:..  /* Ide
2682e 6e 74 69 66 79 20 63 6f 6c 75 6d 6e 20 6e 61 6d  ntify column nam
2682f 65 73 20 69 66 20 72 65 73 75 6c 74 73 20 6f 66  es if results of
26830 20 74 68 65 20 53 45 4c 45 43 54 20 61 72 65 20   the SELECT are 
26831 74 6f 20 62 65 20 6f 75 74 70 75 74 2e 0a 20 20  to be output..  
26832 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
26833 49 54 45 5f 4f 4b 20 26 26 20 70 44 65 73 74 2d  ITE_OK && pDest-
26834 3e 65 44 65 73 74 3d 3d 53 52 54 5f 4f 75 74 70  >eDest==SRT_Outp
26835 75 74 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61  ut ){.    genera
26836 74 65 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 70 50  teColumnNames(pP
26837 61 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20  arse, pTabList, 
26838 70 45 4c 69 73 74 29 3b 0a 20 20 7d 0a 0a 20 20  pEList);.  }..  
26839 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
2683a 2c 20 73 41 67 67 49 6e 66 6f 2e 61 43 6f 6c 29  , sAggInfo.aCol)
2683b 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  ;.  sqlite3DbFre
2683c 65 28 64 62 2c 20 73 41 67 67 49 6e 66 6f 2e 61  e(db, sAggInfo.a
2683d 46 75 6e 63 29 3b 0a 20 20 72 65 74 75 72 6e 20  Func);.  return 
2683e 72 63 3b 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e  rc;.}..#if defin
2683f 65 64 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29  ed(SQLITE_DEBUG)
26840 0a 2f 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./*.************
26841 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26842 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26843 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26844 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26845 2a 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f  ***.** The follo
26846 77 69 6e 67 20 63 6f 64 65 20 69 73 20 75 73 65  wing code is use
26847 64 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e  d for testing an
26848 64 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79  d debugging only
26849 2e 20 20 54 68 65 20 63 6f 64 65 0a 2a 2a 20 74  .  The code.** t
2684a 68 61 74 20 66 6f 6c 6c 6f 77 73 20 64 6f 65 73  hat follows does
2684b 20 6e 6f 74 20 61 70 70 65 61 72 20 69 6e 20 6e   not appear in n
2684c 6f 72 6d 61 6c 20 62 75 69 6c 64 73 2e 0a 2a 2a  ormal builds..**
2684d 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74 69 6e  .** These routin
2684e 65 73 20 61 72 65 20 75 73 65 64 20 74 6f 20 70  es are used to p
2684f 72 69 6e 74 20 6f 75 74 20 74 68 65 20 63 6f 6e  rint out the con
26850 74 65 6e 74 20 6f 66 20 61 6c 6c 20 6f 72 20 70  tent of all or p
26851 61 72 74 20 6f 66 20 61 20 0a 2a 2a 20 70 61 72  art of a .** par
26852 73 65 20 73 74 72 75 63 74 75 72 65 73 20 73 75  se structures su
26853 63 68 20 61 73 20 53 65 6c 65 63 74 20 6f 72 20  ch as Select or 
26854 45 78 70 72 2e 20 20 53 75 63 68 20 70 72 69 6e  Expr.  Such prin
26855 74 6f 75 74 73 20 61 72 65 20 75 73 65 66 75 6c  touts are useful
26856 0a 2a 2a 20 66 6f 72 20 68 65 6c 70 69 6e 67 20  .** for helping 
26857 74 6f 20 75 6e 64 65 72 73 74 61 6e 64 20 77 68  to understand wh
26858 61 74 20 69 73 20 68 61 70 70 65 6e 69 6e 67 20  at is happening 
26859 69 6e 73 69 64 65 20 74 68 65 20 63 6f 64 65 20  inside the code 
2685a 67 65 6e 65 72 61 74 6f 72 0a 2a 2a 20 64 75 72  generator.** dur
2685b 69 6e 67 20 74 68 65 20 65 78 65 63 75 74 69 6f  ing the executio
2685c 6e 20 6f 66 20 63 6f 6d 70 6c 65 78 20 53 45 4c  n of complex SEL
2685d 45 43 54 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  ECT statements..
2685e 2a 2a 0a 2a 2a 20 54 68 65 73 65 20 72 6f 75 74  **.** These rout
2685f 69 6e 65 20 61 72 65 20 6e 6f 74 20 63 61 6c 6c  ine are not call
26860 65 64 20 61 6e 79 77 68 65 72 65 20 66 72 6f 6d  ed anywhere from
26861 20 77 69 74 68 69 6e 20 74 68 65 20 6e 6f 72 6d   within the norm
26862 61 6c 0a 2a 2a 20 63 6f 64 65 20 62 61 73 65 2e  al.** code base.
26863 20 20 54 68 65 6e 20 61 72 65 20 69 6e 74 65 6e    Then are inten
26864 64 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64  ded to be called
26865 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65   from within the
26866 20 64 65 62 75 67 67 65 72 0a 2a 2a 20 6f 72 20   debugger.** or 
26867 66 72 6f 6d 20 74 65 6d 70 6f 72 61 72 79 20 22  from temporary "
26868 70 72 69 6e 74 66 22 20 73 74 61 74 65 6d 65 6e  printf" statemen
26869 74 73 20 69 6e 73 65 72 74 65 64 20 66 6f 72 20  ts inserted for 
2686a 64 65 62 75 67 67 69 6e 67 2e 0a 2a 2f 0a 53 51  debugging..*/.SQ
2686b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2686c 64 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78  d sqlite3PrintEx
2686d 70 72 28 45 78 70 72 20 2a 70 29 7b 0a 20 20 69  pr(Expr *p){.  i
2686e 66 28 20 70 2d 3e 74 6f 6b 65 6e 2e 7a 20 26 26  f( p->token.z &&
2686f 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 3e 30 20 29 7b   p->token.n>0 ){
26870 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75  .    sqlite3Debu
26871 67 50 72 69 6e 74 66 28 22 28 25 2e 2a 73 22 2c  gPrintf("(%.*s",
26872 20 70 2d 3e 74 6f 6b 65 6e 2e 6e 2c 20 70 2d 3e   p->token.n, p->
26873 74 6f 6b 65 6e 2e 7a 29 3b 0a 20 20 7d 65 6c 73  token.z);.  }els
26874 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  e{.    sqlite3De
26875 62 75 67 50 72 69 6e 74 66 28 22 28 25 64 22 2c  bugPrintf("(%d",
26876 20 70 2d 3e 6f 70 29 3b 0a 20 20 7d 0a 20 20 69   p->op);.  }.  i
26877 66 28 20 70 2d 3e 70 4c 65 66 74 20 29 7b 0a 20  f( p->pLeft ){. 
26878 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
26879 72 69 6e 74 66 28 22 20 22 29 3b 0a 20 20 20 20  rintf(" ");.    
2687a 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
2687b 28 70 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 7d 0a  (p->pLeft);.  }.
2687c 20 20 69 66 28 20 70 2d 3e 70 52 69 67 68 74 20    if( p->pRight 
2687d 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
2687e 62 75 67 50 72 69 6e 74 66 28 22 20 22 29 3b 0a  bugPrintf(" ");.
2687f 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74      sqlite3Print
26880 45 78 70 72 28 70 2d 3e 70 52 69 67 68 74 29 3b  Expr(p->pRight);
26881 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65  .  }.  sqlite3De
26882 62 75 67 50 72 69 6e 74 66 28 22 29 22 29 3b 0a  bugPrintf(")");.
26883 7d 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  }.SQLITE_PRIVATE
26884 20 76 6f 69 64 20 73 71 6c 69 74 65 33 50 72 69   void sqlite3Pri
26885 6e 74 45 78 70 72 4c 69 73 74 28 45 78 70 72 4c  ntExprList(ExprL
26886 69 73 74 20 2a 70 4c 69 73 74 29 7b 0a 20 20 69  ist *pList){.  i
26887 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
26888 20 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b   i<pList->nExpr;
26889 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69 74   i++){.    sqlit
2688a 65 33 50 72 69 6e 74 45 78 70 72 28 70 4c 69 73  e3PrintExpr(pLis
2688b 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b 0a  t->a[i].pExpr);.
2688c 20 20 20 20 69 66 28 20 69 3c 70 4c 69 73 74 2d      if( i<pList-
2688d 3e 6e 45 78 70 72 2d 31 20 29 7b 0a 20 20 20 20  >nExpr-1 ){.    
2688e 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
2688f 69 6e 74 66 28 22 2c 20 22 29 3b 0a 20 20 20 20  intf(", ");.    
26890 7d 0a 20 20 7d 0a 7d 0a 53 51 4c 49 54 45 5f 50  }.  }.}.SQLITE_P
26891 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
26892 74 65 33 50 72 69 6e 74 53 65 6c 65 63 74 28 53  te3PrintSelect(S
26893 65 6c 65 63 74 20 2a 70 2c 20 69 6e 74 20 69 6e  elect *p, int in
26894 64 65 6e 74 29 7b 0a 20 20 73 71 6c 69 74 65 33  dent){.  sqlite3
26895 44 65 62 75 67 50 72 69 6e 74 66 28 22 25 2a 73  DebugPrintf("%*s
26896 53 45 4c 45 43 54 28 25 70 29 20 22 2c 20 69 6e  SELECT(%p) ", in
26897 64 65 6e 74 2c 20 22 22 2c 20 70 29 3b 0a 20 20  dent, "", p);.  
26898 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
26899 4c 69 73 74 28 70 2d 3e 70 45 4c 69 73 74 29 3b  List(p->pEList);
2689a 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50  .  sqlite3DebugP
2689b 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 69  rintf("\n");.  i
2689c 66 28 20 70 2d 3e 70 53 72 63 20 29 7b 0a 20 20  f( p->pSrc ){.  
2689d 20 20 63 68 61 72 20 2a 7a 50 72 65 66 69 78 3b    char *zPrefix;
2689e 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20 20 20 20  .    int i;.    
2689f 7a 50 72 65 66 69 78 20 3d 20 22 46 52 4f 4d 22  zPrefix = "FROM"
268a0 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
268a1 3c 70 2d 3e 70 53 72 63 2d 3e 6e 53 72 63 3b 20  <p->pSrc->nSrc; 
268a2 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 74 72 75  i++){.      stru
268a3 63 74 20 53 72 63 4c 69 73 74 5f 69 74 65 6d 20  ct SrcList_item 
268a4 2a 70 49 74 65 6d 20 3d 20 26 70 2d 3e 70 53 72  *pItem = &p->pSr
268a5 63 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 20 20 73  c->a[i];.      s
268a6 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
268a7 66 28 22 25 2a 73 20 22 2c 20 69 6e 64 65 6e 74  f("%*s ", indent
268a8 2b 36 2c 20 7a 50 72 65 66 69 78 29 3b 0a 20 20  +6, zPrefix);.  
268a9 20 20 20 20 7a 50 72 65 66 69 78 20 3d 20 22 22      zPrefix = ""
268aa 3b 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  ;.      if( pIte
268ab 6d 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20 20  m->pSelect ){.  
268ac 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62        sqlite3Deb
268ad 75 67 50 72 69 6e 74 66 28 22 28 5c 6e 22 29 3b  ugPrintf("(\n");
268ae 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
268af 50 72 69 6e 74 53 65 6c 65 63 74 28 70 49 74 65  PrintSelect(pIte
268b0 6d 2d 3e 70 53 65 6c 65 63 74 2c 20 69 6e 64 65  m->pSelect, inde
268b1 6e 74 2b 31 30 29 3b 0a 20 20 20 20 20 20 20 20  nt+10);.        
268b2 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e  sqlite3DebugPrin
268b3 74 66 28 22 25 2a 73 29 22 2c 20 69 6e 64 65 6e  tf("%*s)", inden
268b4 74 2b 38 2c 20 22 22 29 3b 0a 20 20 20 20 20 20  t+8, "");.      
268b5 7d 65 6c 73 65 20 69 66 28 20 70 49 74 65 6d 2d  }else if( pItem-
268b6 3e 7a 4e 61 6d 65 20 29 7b 0a 20 20 20 20 20 20  >zName ){.      
268b7 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72    sqlite3DebugPr
268b8 69 6e 74 66 28 22 25 73 22 2c 20 70 49 74 65 6d  intf("%s", pItem
268b9 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
268ba 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  }.      if( pIte
268bb 6d 2d 3e 70 54 61 62 20 29 7b 0a 20 20 20 20 20  m->pTab ){.     
268bc 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
268bd 72 69 6e 74 66 28 22 28 74 61 62 6c 65 3a 20 25  rintf("(table: %
268be 73 29 22 2c 20 70 49 74 65 6d 2d 3e 70 54 61 62  s)", pItem->pTab
268bf 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
268c0 7d 0a 20 20 20 20 20 20 69 66 28 20 70 49 74 65  }.      if( pIte
268c1 6d 2d 3e 7a 41 6c 69 61 73 20 29 7b 0a 20 20 20  m->zAlias ){.   
268c2 20 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75       sqlite3Debu
268c3 67 50 72 69 6e 74 66 28 22 20 41 53 20 25 73 22  gPrintf(" AS %s"
268c4 2c 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29  , pItem->zAlias)
268c5 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
268c6 69 66 28 20 69 3c 70 2d 3e 70 53 72 63 2d 3e 6e  if( i<p->pSrc->n
268c7 53 72 63 2d 31 20 29 7b 0a 20 20 20 20 20 20 20  Src-1 ){.       
268c8 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
268c9 6e 74 66 28 22 2c 22 29 3b 0a 20 20 20 20 20 20  ntf(",");.      
268ca 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  }.      sqlite3D
268cb 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29  ebugPrintf("\n")
268cc 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
268cd 28 20 70 2d 3e 70 57 68 65 72 65 20 29 7b 0a 20  ( p->pWhere ){. 
268ce 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67 50     sqlite3DebugP
268cf 72 69 6e 74 66 28 22 25 2a 73 20 57 48 45 52 45  rintf("%*s WHERE
268d0 20 22 2c 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b   ", indent, "");
268d1 0a 20 20 20 20 73 71 6c 69 74 65 33 50 72 69 6e  .    sqlite3Prin
268d2 74 45 78 70 72 28 70 2d 3e 70 57 68 65 72 65 29  tExpr(p->pWhere)
268d3 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 62  ;.    sqlite3Deb
268d4 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a  ugPrintf("\n");.
268d5 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70 47 72    }.  if( p->pGr
268d6 6f 75 70 42 79 20 29 7b 0a 20 20 20 20 73 71 6c  oupBy ){.    sql
268d7 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
268d8 22 25 2a 73 20 47 52 4f 55 50 20 42 59 20 22 2c  "%*s GROUP BY ",
268d9 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20   indent, "");.  
268da 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
268db 70 72 4c 69 73 74 28 70 2d 3e 70 47 72 6f 75 70  prList(p->pGroup
268dc 42 79 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  By);.    sqlite3
268dd 44 65 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22  DebugPrintf("\n"
268de 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e  );.  }.  if( p->
268df 70 48 61 76 69 6e 67 20 29 7b 0a 20 20 20 20 73  pHaving ){.    s
268e0 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
268e1 66 28 22 25 2a 73 20 48 41 56 49 4e 47 20 22 2c  f("%*s HAVING ",
268e2 20 69 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20   indent, "");.  
268e3 20 20 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78    sqlite3PrintEx
268e4 70 72 28 70 2d 3e 70 48 61 76 69 6e 67 29 3b 0a  pr(p->pHaving);.
268e5 20 20 20 20 73 71 6c 69 74 65 33 44 65 62 75 67      sqlite3Debug
268e6 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20  Printf("\n");.  
268e7 7d 0a 20 20 69 66 28 20 70 2d 3e 70 4f 72 64 65  }.  if( p->pOrde
268e8 72 42 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rBy ){.    sqlit
268e9 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22 25  e3DebugPrintf("%
268ea 2a 73 20 4f 52 44 45 52 20 42 59 20 22 2c 20 69  *s ORDER BY ", i
268eb 6e 64 65 6e 74 2c 20 22 22 29 3b 0a 20 20 20 20  ndent, "");.    
268ec 73 71 6c 69 74 65 33 50 72 69 6e 74 45 78 70 72  sqlite3PrintExpr
268ed 4c 69 73 74 28 70 2d 3e 70 4f 72 64 65 72 42 79  List(p->pOrderBy
268ee 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  );.    sqlite3De
268ef 62 75 67 50 72 69 6e 74 66 28 22 5c 6e 22 29 3b  bugPrintf("\n");
268f0 0a 20 20 7d 0a 7d 0a 2f 2a 20 45 6e 64 20 6f 66  .  }.}./* End of
268f1 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 64   the structure d
268f2 65 62 75 67 20 70 72 69 6e 74 69 6e 67 20 63 6f  ebug printing co
268f3 64 65 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  de.*************
268f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
268f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
268f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
268f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
268f8 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 64 65 66 69  /.#endif /* defi
268f9 6e 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29  ned(SQLITE_TEST)
268fa 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49   || defined(SQLI
268fb 54 45 5f 44 45 42 55 47 29 20 2a 2f 0a 0a 2f 2a  TE_DEBUG) */../*
268fc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
268fd 64 20 6f 66 20 73 65 6c 65 63 74 2e 63 20 2a 2a  d of select.c **
268fe 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
268ff 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26900 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
26901 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
26902 67 69 6e 20 66 69 6c 65 20 74 61 62 6c 65 2e 63  gin file table.c
26903 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
26904 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26905 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
26906 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
26907 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
26908 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
26909 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2690a 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2690b 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2690c 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2690d 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2690e 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2690f 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
26910 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
26911 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
26912 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
26913 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
26914 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
26915 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
26916 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
26917 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
26918 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
26919 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2691d 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
2691e 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73  e contains the s
2691f 71 6c 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65  qlite3_get_table
26920 28 29 20 61 6e 64 20 73 71 6c 69 74 65 33 5f 66  () and sqlite3_f
26921 72 65 65 5f 74 61 62 6c 65 28 29 0a 2a 2a 20 69  ree_table().** i
26922 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
26923 73 2e 20 20 54 68 65 73 65 20 61 72 65 20 6a 75  s.  These are ju
26924 73 74 20 77 72 61 70 70 65 72 73 20 61 72 6f 75  st wrappers arou
26925 6e 64 20 74 68 65 20 6d 61 69 6e 0a 2a 2a 20 69  nd the main.** i
26926 6e 74 65 72 66 61 63 65 20 72 6f 75 74 69 6e 65  nterface routine
26927 20 6f 66 20 73 71 6c 69 74 65 33 5f 65 78 65 63   of sqlite3_exec
26928 28 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 73 65 20  ()..**.** These 
26929 72 6f 75 74 69 6e 65 73 20 61 72 65 20 69 6e 20  routines are in 
2692a 61 20 73 65 70 61 72 61 74 65 20 66 69 6c 65 73  a separate files
2692b 20 73 6f 20 74 68 61 74 20 74 68 65 79 20 77 69   so that they wi
2692c 6c 6c 20 6e 6f 74 20 62 65 20 6c 69 6e 6b 65 64  ll not be linked
2692d 0a 2a 2a 20 69 66 20 74 68 65 79 20 61 72 65 20  .** if they are 
2692e 6e 6f 74 20 75 73 65 64 2e 0a 2a 2a 0a 2a 2a 20  not used..**.** 
2692f 24 49 64 3a 20 74 61 62 6c 65 2e 63 2c 76 20 31  $Id: table.c,v 1
26930 2e 33 36 20 32 30 30 38 2f 30 37 2f 30 38 20 32  .36 2008/07/08 2
26931 32 3a 32 38 3a 34 39 20 73 68 61 6e 65 20 45 78  2:28:49 shane Ex
26932 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  p $.*/..#ifndef 
26933 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47 45 54 5f  SQLITE_OMIT_GET_
26934 54 41 42 4c 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  TABLE../*.** Thi
26935 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20 75  s structure is u
26936 73 65 64 20 74 6f 20 70 61 73 73 20 64 61 74 61  sed to pass data
26937 20 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 67 65   from sqlite3_ge
26938 74 5f 74 61 62 6c 65 28 29 20 74 68 72 6f 75 67  t_table() throug
26939 68 0a 2a 2a 20 74 6f 20 74 68 65 20 63 61 6c 6c  h.** to the call
2693a 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20 69 73  back function is
2693b 20 75 73 65 73 20 74 6f 20 62 75 69 6c 64 20 74   uses to build t
2693c 68 65 20 72 65 73 75 6c 74 2e 0a 2a 2f 0a 74 79  he result..*/.ty
2693d 70 65 64 65 66 20 73 74 72 75 63 74 20 54 61 62  pedef struct Tab
2693e 52 65 73 75 6c 74 20 7b 0a 20 20 63 68 61 72 20  Result {.  char 
2693f 2a 2a 61 7a 52 65 73 75 6c 74 3b 0a 20 20 63 68  **azResult;.  ch
26940 61 72 20 2a 7a 45 72 72 4d 73 67 3b 0a 20 20 69  ar *zErrMsg;.  i
26941 6e 74 20 6e 52 65 73 75 6c 74 3b 0a 20 20 69 6e  nt nResult;.  in
26942 74 20 6e 41 6c 6c 6f 63 3b 0a 20 20 69 6e 74 20  t nAlloc;.  int 
26943 6e 52 6f 77 3b 0a 20 20 69 6e 74 20 6e 43 6f 6c  nRow;.  int nCol
26944 75 6d 6e 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61  umn;.  int nData
26945 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 7d 20 54 61  ;.  int rc;.} Ta
26946 62 52 65 73 75 6c 74 3b 0a 0a 2f 2a 0a 2a 2a 20  bResult;../*.** 
26947 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
26948 63 61 6c 6c 65 64 20 6f 6e 63 65 20 66 6f 72 20  called once for 
26949 65 61 63 68 20 72 6f 77 20 69 6e 20 74 68 65 20  each row in the 
2694a 72 65 73 75 6c 74 20 74 61 62 6c 65 2e 20 20 49  result table.  I
2694b 74 73 20 6a 6f 62 0a 2a 2a 20 69 73 20 74 6f 20  ts job.** is to 
2694c 66 69 6c 6c 20 69 6e 20 74 68 65 20 54 61 62 52  fill in the TabR
2694d 65 73 75 6c 74 20 73 74 72 75 63 74 75 72 65 20  esult structure 
2694e 61 70 70 72 6f 70 72 69 61 74 65 6c 79 2c 20 61  appropriately, a
2694f 6c 6c 6f 63 61 74 69 6e 67 20 6e 65 77 0a 2a 2a  llocating new.**
26950 20 6d 65 6d 6f 72 79 20 61 73 20 6e 65 63 65 73   memory as neces
26951 73 61 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  sary..*/.static 
26952 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 65 74 5f  int sqlite3_get_
26953 74 61 62 6c 65 5f 63 62 28 76 6f 69 64 20 2a 70  table_cb(void *p
26954 41 72 67 2c 20 69 6e 74 20 6e 43 6f 6c 2c 20 63  Arg, int nCol, c
26955 68 61 72 20 2a 2a 61 72 67 76 2c 20 63 68 61 72  har **argv, char
26956 20 2a 2a 63 6f 6c 76 29 7b 0a 20 20 54 61 62 52   **colv){.  TabR
26957 65 73 75 6c 74 20 2a 70 20 3d 20 28 54 61 62 52  esult *p = (TabR
26958 65 73 75 6c 74 2a 29 70 41 72 67 3b 0a 20 20 69  esult*)pArg;.  i
26959 6e 74 20 6e 65 65 64 3b 0a 20 20 69 6e 74 20 69  nt need;.  int i
2695a 3b 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 0a 20 20  ;.  char *z;..  
2695b 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
2695c 72 65 20 69 73 20 65 6e 6f 75 67 68 20 73 70 61  re is enough spa
2695d 63 65 20 69 6e 20 70 2d 3e 61 7a 52 65 73 75 6c  ce in p->azResul
2695e 74 20 74 6f 20 68 6f 6c 64 20 65 76 65 72 79 74  t to hold everyt
2695f 68 69 6e 67 0a 20 20 2a 2a 20 77 65 20 6e 65 65  hing.  ** we nee
26960 64 20 74 6f 20 72 65 6d 65 6d 62 65 72 20 66 72  d to remember fr
26961 6f 6d 20 74 68 69 73 20 69 6e 76 6f 63 61 74 69  om this invocati
26962 6f 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 62 61  on of the callba
26963 63 6b 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70  ck..  */.  if( p
26964 2d 3e 6e 52 6f 77 3d 3d 30 20 26 26 20 61 72 67  ->nRow==0 && arg
26965 76 21 3d 30 20 29 7b 0a 20 20 20 20 6e 65 65 64  v!=0 ){.    need
26966 20 3d 20 6e 43 6f 6c 2a 32 3b 0a 20 20 7d 65 6c   = nCol*2;.  }el
26967 73 65 7b 0a 20 20 20 20 6e 65 65 64 20 3d 20 6e  se{.    need = n
26968 43 6f 6c 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  Col;.  }.  if( p
26969 2d 3e 6e 44 61 74 61 20 2b 20 6e 65 65 64 20 3e  ->nData + need >
2696a 3d 20 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a 20  = p->nAlloc ){. 
2696b 20 20 20 63 68 61 72 20 2a 2a 61 7a 4e 65 77 3b     char **azNew;
2696c 0a 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d  .    p->nAlloc =
2696d 20 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 6e   p->nAlloc*2 + n
2696e 65 65 64 20 2b 20 31 3b 0a 20 20 20 20 61 7a 4e  eed + 1;.    azN
2696f 65 77 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61  ew = sqlite3_rea
26970 6c 6c 6f 63 28 20 70 2d 3e 61 7a 52 65 73 75 6c  lloc( p->azResul
26971 74 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29  t, sizeof(char*)
26972 2a 70 2d 3e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20  *p->nAlloc );.  
26973 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d 30 20 29    if( azNew==0 )
26974 20 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69   goto malloc_fai
26975 6c 65 64 3b 0a 20 20 20 20 70 2d 3e 61 7a 52 65  led;.    p->azRe
26976 73 75 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20  sult = azNew;.  
26977 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 69 73 20  }..  /* If this 
26978 69 73 20 74 68 65 20 66 69 72 73 74 20 72 6f 77  is the first row
26979 2c 20 74 68 65 6e 20 67 65 6e 65 72 61 74 65 20  , then generate 
2697a 61 6e 20 65 78 74 72 61 20 72 6f 77 20 63 6f 6e  an extra row con
2697b 74 61 69 6e 69 6e 67 0a 20 20 2a 2a 20 74 68 65  taining.  ** the
2697c 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 63 6f   names of all co
2697d 6c 75 6d 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  lumns..  */.  if
2697e 28 20 70 2d 3e 6e 52 6f 77 3d 3d 30 20 29 7b 0a  ( p->nRow==0 ){.
2697f 20 20 20 20 70 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d      p->nColumn =
26980 20 6e 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69   nCol;.    for(i
26981 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69 2b 2b 29  =0; i<nCol; i++)
26982 7b 0a 20 20 20 20 20 20 7a 20 3d 20 73 71 6c 69  {.      z = sqli
26983 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 22  te3_mprintf("%s"
26984 2c 20 63 6f 6c 76 5b 69 5d 29 3b 0a 20 20 20 20  , colv[i]);.    
26985 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 67 6f 74    if( z==0 ) got
26986 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65 64 3b  o malloc_failed;
26987 0a 20 20 20 20 20 20 70 2d 3e 61 7a 52 65 73 75  .      p->azResu
26988 6c 74 5b 70 2d 3e 6e 44 61 74 61 2b 2b 5d 20 3d  lt[p->nData++] =
26989 20 7a 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73   z;.    }.  }els
2698a 65 20 69 66 28 20 70 2d 3e 6e 43 6f 6c 75 6d 6e  e if( p->nColumn
2698b 21 3d 6e 43 6f 6c 20 29 7b 0a 20 20 20 20 73 71  !=nCol ){.    sq
2698c 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 45  lite3_free(p->zE
2698d 72 72 4d 73 67 29 3b 0a 20 20 20 20 70 2d 3e 7a  rrMsg);.    p->z
2698e 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
2698f 5f 6d 70 72 69 6e 74 66 28 0a 20 20 20 20 20 20  _mprintf(.      
26990 20 22 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61   "sqlite3_get_ta
26991 62 6c 65 28 29 20 63 61 6c 6c 65 64 20 77 69 74  ble() called wit
26992 68 20 74 77 6f 20 6f 72 20 6d 6f 72 65 20 69 6e  h two or more in
26993 63 6f 6d 70 61 74 69 62 6c 65 20 71 75 65 72 69  compatible queri
26994 65 73 22 0a 20 20 20 20 29 3b 0a 20 20 20 20 70  es".    );.    p
26995 2d 3e 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52  ->rc = SQLITE_ER
26996 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  ROR;.    return 
26997 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 6f 70  1;.  }..  /* Cop
26998 79 20 6f 76 65 72 20 74 68 65 20 72 6f 77 20 64  y over the row d
26999 61 74 61 0a 20 20 2a 2f 0a 20 20 69 66 28 20 61  ata.  */.  if( a
2699a 72 67 76 21 3d 30 20 29 7b 0a 20 20 20 20 66 6f  rgv!=0 ){.    fo
2699b 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 3b 20 69  r(i=0; i<nCol; i
2699c 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
2699d 72 67 76 5b 69 5d 3d 3d 30 20 29 7b 0a 20 20 20  rgv[i]==0 ){.   
2699e 20 20 20 20 20 7a 20 3d 20 30 3b 0a 20 20 20 20       z = 0;.    
2699f 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
269a0 20 69 6e 74 20 6e 20 3d 20 73 74 72 6c 65 6e 28   int n = strlen(
269a1 61 72 67 76 5b 69 5d 29 2b 31 3b 0a 20 20 20 20  argv[i])+1;.    
269a2 20 20 20 20 7a 20 3d 20 73 71 6c 69 74 65 33 5f      z = sqlite3_
269a3 6d 61 6c 6c 6f 63 28 20 6e 20 29 3b 0a 20 20 20  malloc( n );.   
269a4 20 20 20 20 20 69 66 28 20 7a 3d 3d 30 20 29 20       if( z==0 ) 
269a5 67 6f 74 6f 20 6d 61 6c 6c 6f 63 5f 66 61 69 6c  goto malloc_fail
269a6 65 64 3b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63  ed;.        memc
269a7 70 79 28 7a 2c 20 61 72 67 76 5b 69 5d 2c 20 6e  py(z, argv[i], n
269a8 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
269a9 20 70 2d 3e 61 7a 52 65 73 75 6c 74 5b 70 2d 3e   p->azResult[p->
269aa 6e 44 61 74 61 2b 2b 5d 20 3d 20 7a 3b 0a 20 20  nData++] = z;.  
269ab 20 20 7d 0a 20 20 20 20 70 2d 3e 6e 52 6f 77 2b    }.    p->nRow+
269ac 2b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  +;.  }.  return 
269ad 30 3b 0a 0a 6d 61 6c 6c 6f 63 5f 66 61 69 6c 65  0;..malloc_faile
269ae 64 3a 0a 20 20 70 2d 3e 72 63 20 3d 20 53 51 4c  d:.  p->rc = SQL
269af 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 72 65 74  ITE_NOMEM;.  ret
269b0 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  urn 1;.}../*.** 
269b1 51 75 65 72 79 20 74 68 65 20 64 61 74 61 62 61  Query the databa
269b2 73 65 2e 20 20 42 75 74 20 69 6e 73 74 65 61 64  se.  But instead
269b3 20 6f 66 20 69 6e 76 6f 6b 69 6e 67 20 61 20 63   of invoking a c
269b4 61 6c 6c 62 61 63 6b 20 66 6f 72 20 65 61 63 68  allback for each
269b5 20 72 6f 77 2c 0a 2a 2a 20 6d 61 6c 6c 6f 63 28   row,.** malloc(
269b6 29 20 66 6f 72 20 73 70 61 63 65 20 74 6f 20 68  ) for space to h
269b7 6f 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 61  old the result a
269b8 6e 64 20 72 65 74 75 72 6e 20 74 68 65 20 65 6e  nd return the en
269b9 74 69 72 65 20 72 65 73 75 6c 74 73 0a 2a 2a 20  tire results.** 
269ba 61 74 20 74 68 65 20 63 6f 6e 63 6c 75 73 69 6f  at the conclusio
269bb 6e 20 6f 66 20 74 68 65 20 63 61 6c 6c 2e 0a 2a  n of the call..*
269bc 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74 20  *.** The result 
269bd 74 68 61 74 20 69 73 20 77 72 69 74 74 65 6e 20  that is written 
269be 74 6f 20 2a 2a 2a 70 61 7a 52 65 73 75 6c 74 20  to ***pazResult 
269bf 69 73 20 68 65 6c 64 20 69 6e 20 6d 65 6d 6f 72  is held in memor
269c0 79 20 6f 62 74 61 69 6e 65 64 0a 2a 2a 20 66 72  y obtained.** fr
269c1 6f 6d 20 6d 61 6c 6c 6f 63 28 29 2e 20 20 42 75  om malloc().  Bu
269c2 74 20 74 68 65 20 63 61 6c 6c 65 72 20 63 61 6e  t the caller can
269c3 6e 6f 74 20 66 72 65 65 20 74 68 69 73 20 6d 65  not free this me
269c4 6d 6f 72 79 20 64 69 72 65 63 74 6c 79 2e 20 20  mory directly.  
269c5 0a 2a 2a 20 49 6e 73 74 65 61 64 2c 20 74 68 65  .** Instead, the
269c6 20 65 6e 74 69 72 65 20 74 61 62 6c 65 20 73 68   entire table sh
269c7 6f 75 6c 64 20 62 65 20 70 61 73 73 65 64 20 74  ould be passed t
269c8 6f 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74  o sqlite3_free_t
269c9 61 62 6c 65 28 29 20 77 68 65 6e 0a 2a 2a 20 74  able() when.** t
269ca 68 65 20 63 61 6c 6c 69 6e 67 20 70 72 6f 63 65  he calling proce
269cb 64 75 72 65 20 69 73 20 66 69 6e 69 73 68 65 64  dure is finished
269cc 20 75 73 69 6e 67 20 69 74 2e 0a 2a 2f 0a 53 51   using it..*/.SQ
269cd 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
269ce 69 74 65 33 5f 67 65 74 5f 74 61 62 6c 65 28 0a  ite3_get_table(.
269cf 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20    sqlite3 *db,  
269d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
269d1 20 54 68 65 20 64 61 74 61 62 61 73 65 20 6f 6e   The database on
269d2 20 77 68 69 63 68 20 74 68 65 20 53 51 4c 20 65   which the SQL e
269d3 78 65 63 75 74 65 73 20 2a 2f 0a 20 20 63 6f 6e  xecutes */.  con
269d4 73 74 20 63 68 61 72 20 2a 7a 53 71 6c 2c 20 20  st char *zSql,  
269d5 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
269d6 53 51 4c 20 74 6f 20 62 65 20 65 78 65 63 75 74  SQL to be execut
269d7 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 2a  ed */.  char ***
269d8 70 61 7a 52 65 73 75 6c 74 2c 20 20 20 20 20 20  pazResult,      
269d9 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
269da 20 72 65 73 75 6c 74 20 74 61 62 6c 65 20 68 65   result table he
269db 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 52  re */.  int *pnR
269dc 6f 77 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ow,             
269dd 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
269de 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f 77 73 20   number of rows 
269df 69 6e 20 74 68 65 20 72 65 73 75 6c 74 20 68 65  in the result he
269e0 72 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 43  re */.  int *pnC
269e1 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
269e2 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68 65      /* Write the
269e3 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
269e4 6e 73 20 6f 66 20 72 65 73 75 6c 74 20 68 65 72  ns of result her
269e5 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 70 7a  e */.  char **pz
269e6 45 72 72 4d 73 67 20 20 20 20 20 20 20 20 20 20  ErrMsg          
269e7 20 20 20 2f 2a 20 57 72 69 74 65 20 65 72 72 6f     /* Write erro
269e8 72 20 6d 65 73 73 61 67 65 73 20 68 65 72 65 20  r messages here 
269e9 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  */.){.  int rc;.
269ea 20 20 54 61 62 52 65 73 75 6c 74 20 72 65 73 3b    TabResult res;
269eb 0a 0a 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d  ..  *pazResult =
269ec 20 30 3b 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75   0;.  if( pnColu
269ed 6d 6e 20 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d  mn ) *pnColumn =
269ee 20 30 3b 0a 20 20 69 66 28 20 70 6e 52 6f 77 20   0;.  if( pnRow 
269ef 29 20 2a 70 6e 52 6f 77 20 3d 20 30 3b 0a 20 20  ) *pnRow = 0;.  
269f0 72 65 73 2e 7a 45 72 72 4d 73 67 20 3d 20 30 3b  res.zErrMsg = 0;
269f1 0a 20 20 72 65 73 2e 6e 52 65 73 75 6c 74 20 3d  .  res.nResult =
269f2 20 30 3b 0a 20 20 72 65 73 2e 6e 52 6f 77 20 3d   0;.  res.nRow =
269f3 20 30 3b 0a 20 20 72 65 73 2e 6e 43 6f 6c 75 6d   0;.  res.nColum
269f4 6e 20 3d 20 30 3b 0a 20 20 72 65 73 2e 6e 44 61  n = 0;.  res.nDa
269f5 74 61 20 3d 20 31 3b 0a 20 20 72 65 73 2e 6e 41  ta = 1;.  res.nA
269f6 6c 6c 6f 63 20 3d 20 32 30 3b 0a 20 20 72 65 73  lloc = 20;.  res
269f7 2e 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b  .rc = SQLITE_OK;
269f8 0a 20 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 20  .  res.azResult 
269f9 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
269fa 28 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a 72  (sizeof(char*)*r
269fb 65 73 2e 6e 41 6c 6c 6f 63 20 29 3b 0a 20 20 69  es.nAlloc );.  i
269fc 66 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74 3d  f( res.azResult=
269fd 3d 30 20 29 7b 0a 20 20 20 20 20 64 62 2d 3e 65  =0 ){.     db->e
269fe 72 72 43 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f  rrCode = SQLITE_
269ff 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 72 65 74 75  NOMEM;.     retu
26a00 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
26a01 0a 20 20 7d 0a 20 20 72 65 73 2e 61 7a 52 65 73  .  }.  res.azRes
26a02 75 6c 74 5b 30 5d 20 3d 20 30 3b 0a 20 20 72 63  ult[0] = 0;.  rc
26a03 20 3d 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28   = sqlite3_exec(
26a04 64 62 2c 20 7a 53 71 6c 2c 20 73 71 6c 69 74 65  db, zSql, sqlite
26a05 33 5f 67 65 74 5f 74 61 62 6c 65 5f 63 62 2c 20  3_get_table_cb, 
26a06 26 72 65 73 2c 20 70 7a 45 72 72 4d 73 67 29 3b  &res, pzErrMsg);
26a07 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
26a08 66 28 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 30  f(res.azResult[0
26a09 5d 29 3e 3d 20 73 69 7a 65 6f 66 28 72 65 73 2e  ])>= sizeof(res.
26a0a 6e 44 61 74 61 29 20 29 3b 0a 20 20 72 65 73 2e  nData) );.  res.
26a0b 61 7a 52 65 73 75 6c 74 5b 30 5d 20 3d 20 53 51  azResult[0] = SQ
26a0c 4c 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28  LITE_INT_TO_PTR(
26a0d 72 65 73 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66  res.nData);.  if
26a0e 28 20 28 72 63 26 30 78 66 66 29 3d 3d 53 51 4c  ( (rc&0xff)==SQL
26a0f 49 54 45 5f 41 42 4f 52 54 20 29 7b 0a 20 20 20  ITE_ABORT ){.   
26a10 20 73 71 6c 69 74 65 33 5f 66 72 65 65 5f 74 61   sqlite3_free_ta
26a11 62 6c 65 28 26 72 65 73 2e 61 7a 52 65 73 75 6c  ble(&res.azResul
26a12 74 5b 31 5d 29 3b 0a 20 20 20 20 69 66 28 20 72  t[1]);.    if( r
26a13 65 73 2e 7a 45 72 72 4d 73 67 20 29 7b 0a 20 20  es.zErrMsg ){.  
26a14 20 20 20 20 69 66 28 20 70 7a 45 72 72 4d 73 67      if( pzErrMsg
26a15 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69   ){.        sqli
26a16 74 65 33 5f 66 72 65 65 28 2a 70 7a 45 72 72 4d  te3_free(*pzErrM
26a17 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70 7a  sg);.        *pz
26a18 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65 33  ErrMsg = sqlite3
26a19 5f 6d 70 72 69 6e 74 66 28 22 25 73 22 2c 72 65  _mprintf("%s",re
26a1a 73 2e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  s.zErrMsg);.    
26a1b 20 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65    }.      sqlite
26a1c 33 5f 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d  3_free(res.zErrM
26a1d 73 67 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  sg);.    }.    d
26a1e 62 2d 3e 65 72 72 43 6f 64 65 20 3d 20 72 65 73  b->errCode = res
26a1f 2e 72 63 3b 20 20 2f 2a 20 41 73 73 75 6d 65 20  .rc;  /* Assume 
26a20 33 32 2d 62 69 74 20 61 73 73 69 67 6e 6d 65 6e  32-bit assignmen
26a21 74 20 69 73 20 61 74 6f 6d 69 63 20 2a 2f 0a 20  t is atomic */. 
26a22 20 20 20 72 65 74 75 72 6e 20 72 65 73 2e 72 63     return res.rc
26a23 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  ;.  }.  sqlite3_
26a24 66 72 65 65 28 72 65 73 2e 7a 45 72 72 4d 73 67  free(res.zErrMsg
26a25 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
26a26 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73 71  ITE_OK ){.    sq
26a27 6c 69 74 65 33 5f 66 72 65 65 5f 74 61 62 6c 65  lite3_free_table
26a28 28 26 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31  (&res.azResult[1
26a29 5d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72  ]);.    return r
26a2a 63 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 65 73  c;.  }.  if( res
26a2b 2e 6e 41 6c 6c 6f 63 3e 72 65 73 2e 6e 44 61 74  .nAlloc>res.nDat
26a2c 61 20 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 2a  a ){.    char **
26a2d 61 7a 4e 65 77 3b 0a 20 20 20 20 61 7a 4e 65 77  azNew;.    azNew
26a2e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c   = sqlite3_reall
26a2f 6f 63 28 20 72 65 73 2e 61 7a 52 65 73 75 6c 74  oc( res.azResult
26a30 2c 20 73 69 7a 65 6f 66 28 63 68 61 72 2a 29 2a  , sizeof(char*)*
26a31 28 72 65 73 2e 6e 44 61 74 61 2b 31 29 20 29 3b  (res.nData+1) );
26a32 0a 20 20 20 20 69 66 28 20 61 7a 4e 65 77 3d 3d  .    if( azNew==
26a33 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  0 ){.      sqlit
26a34 65 33 5f 66 72 65 65 5f 74 61 62 6c 65 28 26 72  e3_free_table(&r
26a35 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 29 3b  es.azResult[1]);
26a36 0a 20 20 20 20 20 20 64 62 2d 3e 65 72 72 43 6f  .      db->errCo
26a37 64 65 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  de = SQLITE_NOME
26a38 4d 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  M;.      return 
26a39 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20  SQLITE_NOMEM;.  
26a3a 20 20 7d 0a 20 20 20 20 72 65 73 2e 6e 41 6c 6c    }.    res.nAll
26a3b 6f 63 20 3d 20 72 65 73 2e 6e 44 61 74 61 2b 31  oc = res.nData+1
26a3c 3b 0a 20 20 20 20 72 65 73 2e 61 7a 52 65 73 75  ;.    res.azResu
26a3d 6c 74 20 3d 20 61 7a 4e 65 77 3b 0a 20 20 7d 0a  lt = azNew;.  }.
26a3e 20 20 2a 70 61 7a 52 65 73 75 6c 74 20 3d 20 26    *pazResult = &
26a3f 72 65 73 2e 61 7a 52 65 73 75 6c 74 5b 31 5d 3b  res.azResult[1];
26a40 0a 20 20 69 66 28 20 70 6e 43 6f 6c 75 6d 6e 20  .  if( pnColumn 
26a41 29 20 2a 70 6e 43 6f 6c 75 6d 6e 20 3d 20 72 65  ) *pnColumn = re
26a42 73 2e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 69 66 28  s.nColumn;.  if(
26a43 20 70 6e 52 6f 77 20 29 20 2a 70 6e 52 6f 77 20   pnRow ) *pnRow 
26a44 3d 20 72 65 73 2e 6e 52 6f 77 3b 0a 20 20 72 65  = res.nRow;.  re
26a45 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
26a46 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 66  * This routine f
26a47 72 65 65 73 20 74 68 65 20 73 70 61 63 65 20 74  rees the space t
26a48 68 65 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74  he sqlite3_get_t
26a49 61 62 6c 65 28 29 20 6d 61 6c 6c 6f 63 65 64 2e  able() malloced.
26a4a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
26a4b 6f 69 64 20 73 71 6c 69 74 65 33 5f 66 72 65 65  oid sqlite3_free
26a4c 5f 74 61 62 6c 65 28 0a 20 20 63 68 61 72 20 2a  _table(.  char *
26a4d 2a 61 7a 52 65 73 75 6c 74 20 20 20 20 20 20 20  *azResult       
26a4e 20 20 20 20 20 2f 2a 20 52 65 73 75 6c 74 20 72       /* Result r
26a4f 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 66 72 6f  eturned from fro
26a50 6d 20 73 71 6c 69 74 65 33 5f 67 65 74 5f 74 61  m sqlite3_get_ta
26a51 62 6c 65 28 29 20 2a 2f 0a 29 7b 0a 20 20 69 66  ble() */.){.  if
26a52 28 20 61 7a 52 65 73 75 6c 74 20 29 7b 0a 20 20  ( azResult ){.  
26a53 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20 20 20    int i, n;.    
26a54 61 7a 52 65 73 75 6c 74 2d 2d 3b 0a 20 20 20 20  azResult--;.    
26a55 61 73 73 65 72 74 28 20 61 7a 52 65 73 75 6c 74  assert( azResult
26a56 21 3d 30 20 29 3b 0a 20 20 20 20 6e 20 3d 20 53  !=0 );.    n = S
26a57 51 4c 49 54 45 5f 50 54 52 5f 54 4f 5f 49 4e 54  QLITE_PTR_TO_INT
26a58 28 61 7a 52 65 73 75 6c 74 5b 30 5d 29 3b 0a 20  (azResult[0]);. 
26a59 20 20 20 66 6f 72 28 69 3d 31 3b 20 69 3c 6e 3b     for(i=1; i<n;
26a5a 20 69 2b 2b 29 7b 20 69 66 28 20 61 7a 52 65 73   i++){ if( azRes
26a5b 75 6c 74 5b 69 5d 20 29 20 73 71 6c 69 74 65 33  ult[i] ) sqlite3
26a5c 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 5b 69  _free(azResult[i
26a5d 5d 29 3b 20 7d 0a 20 20 20 20 73 71 6c 69 74 65  ]); }.    sqlite
26a5e 33 5f 66 72 65 65 28 61 7a 52 65 73 75 6c 74 29  3_free(azResult)
26a5f 3b 0a 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20  ;.  }.}..#endif 
26a60 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 47  /* SQLITE_OMIT_G
26a61 45 54 5f 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 2a  ET_TABLE */../**
26a62 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
26a63 20 6f 66 20 74 61 62 6c 65 2e 63 20 2a 2a 2a 2a   of table.c ****
26a64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
26a67 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
26a68 69 6e 20 66 69 6c 65 20 74 72 69 67 67 65 72 2e  in file trigger.
26a69 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
26a6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a6b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
26a6c 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
26a6d 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
26a6e 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
26a6f 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
26a70 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
26a71 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
26a72 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
26a73 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
26a74 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
26a75 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
26a76 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
26a77 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
26a78 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
26a79 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
26a7a 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
26a7b 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
26a7c 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
26a7d 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
26a7e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a7f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a80 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
26a81 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
26a82 2a 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 74 72 69  *.**.** $Id: tri
26a83 67 67 65 72 2e 63 2c 76 20 31 2e 31 33 30 20 32  gger.c,v 1.130 2
26a84 30 30 38 2f 31 31 2f 31 39 20 30 39 3a 30 35 3a  008/11/19 09:05:
26a85 32 37 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45  27 danielk1977 E
26a86 78 70 20 24 0a 2a 2f 0a 0a 23 69 66 6e 64 65 66  xp $.*/..#ifndef
26a87 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49   SQLITE_OMIT_TRI
26a88 47 47 45 52 0a 2f 2a 0a 2a 2a 20 44 65 6c 65 74  GGER./*.** Delet
26a89 65 20 61 20 6c 69 6e 6b 65 64 20 6c 69 73 74 20  e a linked list 
26a8a 6f 66 20 54 72 69 67 67 65 72 53 74 65 70 20 73  of TriggerStep s
26a8b 74 72 75 63 74 75 72 65 73 2e 0a 2a 2f 0a 53 51  tructures..*/.SQ
26a8c 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
26a8d 64 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54  d sqlite3DeleteT
26a8e 72 69 67 67 65 72 53 74 65 70 28 73 71 6c 69 74  riggerStep(sqlit
26a8f 65 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72 53  e3 *db, TriggerS
26a90 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
26a91 70 29 7b 0a 20 20 77 68 69 6c 65 28 20 70 54 72  p){.  while( pTr
26a92 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
26a93 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 20 70   TriggerStep * p
26a94 54 6d 70 20 3d 20 70 54 72 69 67 67 65 72 53 74  Tmp = pTriggerSt
26a95 65 70 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  ep;.    pTrigger
26a96 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72 53  Step = pTriggerS
26a97 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 0a 20 20 20  tep->pNext;..   
26a98 20 69 66 28 20 70 54 6d 70 2d 3e 74 61 72 67 65   if( pTmp->targe
26a99 74 2e 64 79 6e 20 29 20 73 71 6c 69 74 65 33 44  t.dyn ) sqlite3D
26a9a 62 46 72 65 65 28 64 62 2c 20 28 63 68 61 72 2a  bFree(db, (char*
26a9b 29 70 54 6d 70 2d 3e 74 61 72 67 65 74 2e 7a 29  )pTmp->target.z)
26a9c 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  ;.    sqlite3Exp
26a9d 72 44 65 6c 65 74 65 28 64 62 2c 20 70 54 6d 70  rDelete(db, pTmp
26a9e 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73  ->pWhere);.    s
26a9f 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65  qlite3ExprListDe
26aa0 6c 65 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70  lete(db, pTmp->p
26aa1 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 73  ExprList);.    s
26aa2 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
26aa3 74 65 28 64 62 2c 20 70 54 6d 70 2d 3e 70 53 65  te(db, pTmp->pSe
26aa4 6c 65 63 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  lect);.    sqlit
26aa5 65 33 49 64 4c 69 73 74 44 65 6c 65 74 65 28 64  e3IdListDelete(d
26aa6 62 2c 20 70 54 6d 70 2d 3e 70 49 64 4c 69 73 74  b, pTmp->pIdList
26aa7 29 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 44  );..    sqlite3D
26aa8 62 46 72 65 65 28 64 62 2c 20 70 54 6d 70 29 3b  bFree(db, pTmp);
26aa9 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  .  }.}../*.** Th
26aaa 69 73 20 69 73 20 63 61 6c 6c 65 64 20 62 79 20  is is called by 
26aab 74 68 65 20 70 61 72 73 65 72 20 77 68 65 6e 20  the parser when 
26aac 69 74 20 73 65 65 73 20 61 20 43 52 45 41 54 45  it sees a CREATE
26aad 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
26aae 6e 74 0a 2a 2a 20 75 70 20 74 6f 20 74 68 65 20  nt.** up to the 
26aaf 70 6f 69 6e 74 20 6f 66 20 74 68 65 20 42 45 47  point of the BEG
26ab0 49 4e 20 62 65 66 6f 72 65 20 74 68 65 20 74 72  IN before the tr
26ab1 69 67 67 65 72 20 61 63 74 69 6f 6e 73 2e 20 20  igger actions.  
26ab2 41 20 54 72 69 67 67 65 72 0a 2a 2a 20 73 74 72  A Trigger.** str
26ab3 75 63 74 75 72 65 20 69 73 20 67 65 6e 65 72 61  ucture is genera
26ab4 74 65 64 20 62 61 73 65 64 20 6f 6e 20 74 68 65  ted based on the
26ab5 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 76 61   information ava
26ab6 69 6c 61 62 6c 65 20 61 6e 64 20 73 74 6f 72 65  ilable and store
26ab7 64 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65 2d 3e  d.** in pParse->
26ab8 70 4e 65 77 54 72 69 67 67 65 72 2e 20 20 41 66  pNewTrigger.  Af
26ab9 74 65 72 20 74 68 65 20 74 72 69 67 67 65 72 20  ter the trigger 
26aba 61 63 74 69 6f 6e 73 20 68 61 76 65 20 62 65 65  actions have bee
26abb 6e 20 70 61 72 73 65 64 2c 20 74 68 65 0a 2a 2a  n parsed, the.**
26abc 20 73 71 6c 69 74 65 33 46 69 6e 69 73 68 54 72   sqlite3FinishTr
26abd 69 67 67 65 72 28 29 20 66 75 6e 63 74 69 6f 6e  igger() function
26abe 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 63 6f   is called to co
26abf 6d 70 6c 65 74 65 20 74 68 65 20 74 72 69 67 67  mplete the trigg
26ac0 65 72 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 69  er.** constructi
26ac1 6f 6e 20 70 72 6f 63 65 73 73 2e 0a 2a 2f 0a 53  on process..*/.S
26ac2 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f  QLITE_PRIVATE vo
26ac3 69 64 20 73 71 6c 69 74 65 33 42 65 67 69 6e 54  id sqlite3BeginT
26ac4 72 69 67 67 65 72 28 0a 20 20 50 61 72 73 65 20  rigger(.  Parse 
26ac5 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 2f 2a  *pParse,      /*
26ac6 20 54 68 65 20 70 61 72 73 65 20 63 6f 6e 74 65   The parse conte
26ac7 78 74 20 6f 66 20 74 68 65 20 43 52 45 41 54 45  xt of the CREATE
26ac8 20 54 52 49 47 47 45 52 20 73 74 61 74 65 6d 65   TRIGGER stateme
26ac9 6e 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  nt */.  Token *p
26aca 4e 61 6d 65 31 2c 20 20 20 20 20 20 2f 2a 20 54  Name1,      /* T
26acb 68 65 20 6e 61 6d 65 20 6f 66 20 74 68 65 20 74  he name of the t
26acc 72 69 67 67 65 72 20 2a 2f 0a 20 20 54 6f 6b 65  rigger */.  Toke
26acd 6e 20 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20  n *pName2,      
26ace 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66 20 74  /* The name of t
26acf 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20  he trigger */.  
26ad0 69 6e 74 20 74 72 5f 74 6d 2c 20 20 20 20 20 20  int tr_tm,      
26ad1 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20 54 4b      /* One of TK
26ad2 5f 42 45 46 4f 52 45 2c 20 54 4b 5f 41 46 54 45  _BEFORE, TK_AFTE
26ad3 52 2c 20 54 4b 5f 49 4e 53 54 45 41 44 20 2a 2f  R, TK_INSTEAD */
26ad4 0a 20 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20  .  int op,      
26ad5 20 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66         /* One of
26ad6 20 54 4b 5f 49 4e 53 45 52 54 2c 20 54 4b 5f 55   TK_INSERT, TK_U
26ad7 50 44 41 54 45 2c 20 54 4b 5f 44 45 4c 45 54 45  PDATE, TK_DELETE
26ad8 20 2a 2f 0a 20 20 49 64 4c 69 73 74 20 2a 70 43   */.  IdList *pC
26ad9 6f 6c 75 6d 6e 73 2c 20 20 20 2f 2a 20 63 6f 6c  olumns,   /* col
26ada 75 6d 6e 20 6c 69 73 74 20 69 66 20 74 68 69 73  umn list if this
26adb 20 69 73 20 61 6e 20 55 50 44 41 54 45 20 4f 46   is an UPDATE OF
26adc 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 53 72   trigger */.  Sr
26add 63 4c 69 73 74 20 2a 70 54 61 62 6c 65 4e 61 6d  cList *pTableNam
26ade 65 2c 2f 2a 20 54 68 65 20 6e 61 6d 65 20 6f 66  e,/* The name of
26adf 20 74 68 65 20 74 61 62 6c 65 2f 76 69 65 77 20   the table/view 
26ae0 74 68 65 20 74 72 69 67 67 65 72 20 61 70 70 6c  the trigger appl
26ae1 69 65 73 20 74 6f 20 2a 2f 0a 20 20 45 78 70 72  ies to */.  Expr
26ae2 20 2a 70 57 68 65 6e 2c 20 20 20 20 20 20 20 20   *pWhen,        
26ae3 2f 2a 20 57 48 45 4e 20 63 6c 61 75 73 65 20 2a  /* WHEN clause *
26ae4 2f 0a 20 20 69 6e 74 20 69 73 54 65 6d 70 2c 20  /.  int isTemp, 
26ae5 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
26ae6 69 66 20 74 68 65 20 54 45 4d 50 4f 52 41 52 59  if the TEMPORARY
26ae7 20 6b 65 79 77 6f 72 64 20 69 73 20 70 72 65 73   keyword is pres
26ae8 65 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 45  ent */.  int noE
26ae9 72 72 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  rr           /* 
26aea 53 75 70 70 72 65 73 73 20 65 72 72 6f 72 73 20  Suppress errors 
26aeb 69 66 20 74 68 65 20 74 72 69 67 67 65 72 20 61  if the trigger a
26aec 6c 72 65 61 64 79 20 65 78 69 73 74 73 20 2a 2f  lready exists */
26aed 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 20 2a 70  .){.  Trigger *p
26aee 54 72 69 67 67 65 72 20 3d 20 30 3b 0a 20 20 54  Trigger = 0;.  T
26aef 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68  able *pTab;.  ch
26af0 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 30 3b 20 20  ar *zName = 0;  
26af1 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
26af2 20 74 68 65 20 74 72 69 67 67 65 72 20 2a 2f 0a   the trigger */.
26af3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
26af4 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 69 6e  pParse->db;.  in
26af5 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20 20  t iDb;          
26af6 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
26af7 61 62 61 73 65 20 74 6f 20 73 74 6f 72 65 20 74  abase to store t
26af8 68 65 20 74 72 69 67 67 65 72 20 69 6e 20 2a 2f  he trigger in */
26af9 0a 20 20 54 6f 6b 65 6e 20 2a 70 4e 61 6d 65 3b  .  Token *pName;
26afa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
26afb 65 20 75 6e 71 75 61 6c 69 66 69 65 64 20 64 62  e unqualified db
26afc 20 6e 61 6d 65 20 2a 2f 0a 20 20 44 62 46 69 78   name */.  DbFix
26afd 65 72 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69  er sFix;.  int i
26afe 54 61 62 44 62 3b 0a 0a 20 20 61 73 73 65 72 74  TabDb;..  assert
26aff 28 20 70 4e 61 6d 65 31 21 3d 30 20 29 3b 20 20  ( pName1!=0 );  
26b00 20 2f 2a 20 70 4e 61 6d 65 31 2d 3e 7a 20 6d 69   /* pName1->z mi
26b01 67 68 74 20 62 65 20 4e 55 4c 4c 2c 20 62 75 74  ght be NULL, but
26b02 20 6e 6f 74 20 70 4e 61 6d 65 31 20 69 74 73 65   not pName1 itse
26b03 6c 66 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  lf */.  assert( 
26b04 70 4e 61 6d 65 32 21 3d 30 20 29 3b 0a 20 20 69  pName2!=0 );.  i
26b05 66 28 20 69 73 54 65 6d 70 20 29 7b 0a 20 20 20  f( isTemp ){.   
26b06 20 2f 2a 20 49 66 20 54 45 4d 50 20 77 61 73 20   /* If TEMP was 
26b07 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e 20  specified, then 
26b08 74 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65  the trigger name
26b09 20 6d 61 79 20 6e 6f 74 20 62 65 20 71 75 61 6c   may not be qual
26b0a 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20 20 69 66  ified. */.    if
26b0b 28 20 70 4e 61 6d 65 32 2d 3e 6e 3e 30 20 29 7b  ( pName2->n>0 ){
26b0c 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
26b0d 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
26b0e 74 65 6d 70 6f 72 61 72 79 20 74 72 69 67 67 65  temporary trigge
26b0f 72 20 6d 61 79 20 6e 6f 74 20 68 61 76 65 20 71  r may not have q
26b10 75 61 6c 69 66 69 65 64 20 6e 61 6d 65 22 29 3b  ualified name");
26b11 0a 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67  .      goto trig
26b12 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20  ger_cleanup;.   
26b13 20 7d 0a 20 20 20 20 69 44 62 20 3d 20 31 3b 0a   }.    iDb = 1;.
26b14 20 20 20 20 70 4e 61 6d 65 20 3d 20 70 4e 61 6d      pName = pNam
26b15 65 31 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  e1;.  }else{.   
26b16 20 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 74   /* Figure out t
26b17 68 65 20 64 62 20 74 68 61 74 20 74 68 65 20 74  he db that the t
26b18 68 65 20 74 72 69 67 67 65 72 20 77 69 6c 6c 20  he trigger will 
26b19 62 65 20 63 72 65 61 74 65 64 20 69 6e 20 2a 2f  be created in */
26b1a 0a 20 20 20 20 69 44 62 20 3d 20 73 71 6c 69 74  .    iDb = sqlit
26b1b 65 33 54 77 6f 50 61 72 74 4e 61 6d 65 28 70 50  e3TwoPartName(pP
26b1c 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
26b1d 61 6d 65 32 2c 20 26 70 4e 61 6d 65 29 3b 0a 20  ame2, &pName);. 
26b1e 20 20 20 69 66 28 20 69 44 62 3c 30 20 29 7b 0a     if( iDb<0 ){.
26b1f 20 20 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67        goto trigg
26b20 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  er_cleanup;.    
26b21 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74  }.  }..  /* If t
26b22 68 65 20 74 72 69 67 67 65 72 20 6e 61 6d 65 20  he trigger name 
26b23 77 61 73 20 75 6e 71 75 61 6c 69 66 69 65 64 2c  was unqualified,
26b24 20 61 6e 64 20 74 68 65 20 74 61 62 6c 65 20 69   and the table i
26b25 73 20 61 20 74 65 6d 70 20 74 61 62 6c 65 2c 0a  s a temp table,.
26b26 20 20 2a 2a 20 74 68 65 6e 20 73 65 74 20 69 44    ** then set iD
26b27 62 20 74 6f 20 31 20 74 6f 20 63 72 65 61 74 65  b to 1 to create
26b28 20 74 68 65 20 74 72 69 67 67 65 72 20 69 6e 20   the trigger in 
26b29 74 68 65 20 74 65 6d 70 6f 72 61 72 79 20 64 61  the temporary da
26b2a 74 61 62 61 73 65 2e 0a 20 20 2a 2a 20 49 66 20  tabase..  ** If 
26b2b 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 4c 6f  sqlite3SrcListLo
26b2c 6f 6b 75 70 28 29 20 72 65 74 75 72 6e 73 20 30  okup() returns 0
26b2d 2c 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 65  , indicating the
26b2e 20 74 61 62 6c 65 20 64 6f 65 73 20 6e 6f 74 0a   table does not.
26b2f 20 20 2a 2a 20 65 78 69 73 74 2c 20 74 68 65 20    ** exist, the 
26b30 65 72 72 6f 72 20 69 73 20 63 61 75 67 68 74 20  error is caught 
26b31 62 79 20 74 68 65 20 62 6c 6f 63 6b 20 62 65 6c  by the block bel
26b32 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  ow..  */.  if( !
26b33 70 54 61 62 6c 65 4e 61 6d 65 20 7c 7c 20 64 62  pTableName || db
26b34 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
26b35 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67  {.    goto trigg
26b36 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a  er_cleanup;.  }.
26b37 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
26b38 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50  SrcListLookup(pP
26b39 61 72 73 65 2c 20 70 54 61 62 6c 65 4e 61 6d 65  arse, pTableName
26b3a 29 3b 0a 20 20 69 66 28 20 70 4e 61 6d 65 32 2d  );.  if( pName2-
26b3b 3e 6e 3d 3d 30 20 26 26 20 70 54 61 62 20 26 26  >n==0 && pTab &&
26b3c 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 3d 3d   pTab->pSchema==
26b3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53 63 68 65  db->aDb[1].pSche
26b3e 6d 61 20 29 7b 0a 20 20 20 20 69 44 62 20 3d 20  ma ){.    iDb = 
26b3f 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 6e 73  1;.  }..  /* Ens
26b40 75 72 65 20 74 68 65 20 74 61 62 6c 65 20 6e 61  ure the table na
26b41 6d 65 20 6d 61 74 63 68 65 73 20 64 61 74 61 62  me matches datab
26b42 61 73 65 20 6e 61 6d 65 20 61 6e 64 20 74 68 61  ase name and tha
26b43 74 20 74 68 65 20 74 61 62 6c 65 20 65 78 69 73  t the table exis
26b44 74 73 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e  ts */.  if( db->
26b45 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 20 67  mallocFailed ) g
26b46 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
26b47 6e 75 70 3b 0a 20 20 61 73 73 65 72 74 28 20 70  nup;.  assert( p
26b48 54 61 62 6c 65 4e 61 6d 65 2d 3e 6e 53 72 63 3d  TableName->nSrc=
26b49 3d 31 20 29 3b 0a 20 20 69 66 28 20 73 71 6c 69  =1 );.  if( sqli
26b4a 74 65 33 46 69 78 49 6e 69 74 28 26 73 46 69 78  te3FixInit(&sFix
26b4b 2c 20 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22  , pParse, iDb, "
26b4c 74 72 69 67 67 65 72 22 2c 20 70 4e 61 6d 65 29  trigger", pName)
26b4d 20 26 26 20 0a 20 20 20 20 20 20 73 71 6c 69 74   && .      sqlit
26b4e 65 33 46 69 78 53 72 63 4c 69 73 74 28 26 73 46  e3FixSrcList(&sF
26b4f 69 78 2c 20 70 54 61 62 6c 65 4e 61 6d 65 29 20  ix, pTableName) 
26b50 29 7b 0a 20 20 20 20 67 6f 74 6f 20 74 72 69 67  ){.    goto trig
26b51 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ger_cleanup;.  }
26b52 0a 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65  .  pTab = sqlite
26b53 33 53 72 63 4c 69 73 74 4c 6f 6f 6b 75 70 28 70  3SrcListLookup(p
26b54 50 61 72 73 65 2c 20 70 54 61 62 6c 65 4e 61 6d  Parse, pTableNam
26b55 65 29 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20  e);.  if( !pTab 
26b56 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65 20 74 61  ){.    /* The ta
26b57 62 6c 65 20 64 6f 65 73 20 6e 6f 74 20 65 78 69  ble does not exi
26b58 73 74 2e 20 2a 2f 0a 20 20 20 20 67 6f 74 6f 20  st. */.    goto 
26b59 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
26b5a 0a 20 20 7d 0a 20 20 69 66 28 20 49 73 56 69 72  .  }.  if( IsVir
26b5b 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a 20 20  tual(pTab) ){.  
26b5c 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
26b5d 67 28 70 50 61 72 73 65 2c 20 22 63 61 6e 6e 6f  g(pParse, "canno
26b5e 74 20 63 72 65 61 74 65 20 74 72 69 67 67 65 72  t create trigger
26b5f 73 20 6f 6e 20 76 69 72 74 75 61 6c 20 74 61 62  s on virtual tab
26b60 6c 65 73 22 29 3b 0a 20 20 20 20 67 6f 74 6f 20  les");.    goto 
26b61 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b  trigger_cleanup;
26b62 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b  .  }..  /* Check
26b63 20 74 68 61 74 20 74 68 65 20 74 72 69 67 67 65   that the trigge
26b64 72 20 6e 61 6d 65 20 69 73 20 6e 6f 74 20 72 65  r name is not re
26b65 73 65 72 76 65 64 20 61 6e 64 20 74 68 61 74 20  served and that 
26b66 6e 6f 20 74 72 69 67 67 65 72 20 6f 66 20 74 68  no trigger of th
26b67 65 0a 20 20 2a 2a 20 73 70 65 63 69 66 69 65 64  e.  ** specified
26b68 20 6e 61 6d 65 20 65 78 69 73 74 73 20 2a 2f 0a   name exists */.
26b69 20 20 7a 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65    zName = sqlite
26b6a 33 4e 61 6d 65 46 72 6f 6d 54 6f 6b 65 6e 28 64  3NameFromToken(d
26b6b 62 2c 20 70 4e 61 6d 65 29 3b 0a 20 20 69 66 28  b, pName);.  if(
26b6c 20 21 7a 4e 61 6d 65 20 7c 7c 20 53 51 4c 49 54   !zName || SQLIT
26b6d 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 43 68 65  E_OK!=sqlite3Che
26b6e 63 6b 4f 62 6a 65 63 74 4e 61 6d 65 28 70 50 61  ckObjectName(pPa
26b6f 72 73 65 2c 20 7a 4e 61 6d 65 29 20 29 7b 0a 20  rse, zName) ){. 
26b70 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
26b71 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
26b72 66 28 20 73 71 6c 69 74 65 33 48 61 73 68 46 69  f( sqlite3HashFi
26b73 6e 64 28 26 28 64 62 2d 3e 61 44 62 5b 69 44 62  nd(&(db->aDb[iDb
26b74 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69 67 48  ].pSchema->trigH
26b75 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 73 74 72 6c  ash), zName,strl
26b76 65 6e 28 7a 4e 61 6d 65 29 29 20 29 7b 0a 20 20  en(zName)) ){.  
26b77 20 20 69 66 28 20 21 6e 6f 45 72 72 20 29 7b 0a    if( !noErr ){.
26b78 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72 72        sqlite3Err
26b79 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 74  orMsg(pParse, "t
26b7a 72 69 67 67 65 72 20 25 54 20 61 6c 72 65 61 64  rigger %T alread
26b7b 79 20 65 78 69 73 74 73 22 2c 20 70 4e 61 6d 65  y exists", pName
26b7c 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 67 6f 74  );.    }.    got
26b7d 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75  o trigger_cleanu
26b7e 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20  p;.  }..  /* Do 
26b7f 6e 6f 74 20 63 72 65 61 74 65 20 61 20 74 72 69  not create a tri
26b80 67 67 65 72 20 6f 6e 20 61 20 73 79 73 74 65 6d  gger on a system
26b81 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
26b82 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28  sqlite3StrNICmp(
26b83 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 22 73 71  pTab->zName, "sq
26b84 6c 69 74 65 5f 22 2c 20 37 29 3d 3d 30 20 29 7b  lite_", 7)==0 ){
26b85 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f  .    sqlite3Erro
26b86 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 63 61  rMsg(pParse, "ca
26b87 6e 6e 6f 74 20 63 72 65 61 74 65 20 74 72 69 67  nnot create trig
26b88 67 65 72 20 6f 6e 20 73 79 73 74 65 6d 20 74 61  ger on system ta
26b89 62 6c 65 22 29 3b 0a 20 20 20 20 70 50 61 72 73  ble");.    pPars
26b8a 65 2d 3e 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 67  e->nErr++;.    g
26b8b 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65 61  oto trigger_clea
26b8c 6e 75 70 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49  nup;.  }..  /* I
26b8d 4e 53 54 45 41 44 20 6f 66 20 74 72 69 67 67 65  NSTEAD of trigge
26b8e 72 73 20 61 72 65 20 6f 6e 6c 79 20 66 6f 72 20  rs are only for 
26b8f 76 69 65 77 73 20 61 6e 64 20 76 69 65 77 73 20  views and views 
26b90 6f 6e 6c 79 20 73 75 70 70 6f 72 74 20 49 4e 53  only support INS
26b91 54 45 41 44 0a 20 20 2a 2a 20 6f 66 20 74 72 69  TEAD.  ** of tri
26b92 67 67 65 72 73 2e 0a 20 20 2a 2f 0a 20 20 69 66  ggers..  */.  if
26b93 28 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20  ( pTab->pSelect 
26b94 26 26 20 74 72 5f 74 6d 21 3d 54 4b 5f 49 4e 53  && tr_tm!=TK_INS
26b95 54 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69  TEAD ){.    sqli
26b96 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72  te3ErrorMsg(pPar
26b97 73 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61  se, "cannot crea
26b98 74 65 20 25 73 20 74 72 69 67 67 65 72 20 6f 6e  te %s trigger on
26b99 20 76 69 65 77 3a 20 25 53 22 2c 20 0a 20 20 20   view: %S", .   
26b9a 20 20 20 20 20 28 74 72 5f 74 6d 20 3d 3d 20 54       (tr_tm == T
26b9b 4b 5f 42 45 46 4f 52 45 29 3f 22 42 45 46 4f 52  K_BEFORE)?"BEFOR
26b9c 45 22 3a 22 41 46 54 45 52 22 2c 20 70 54 61 62  E":"AFTER", pTab
26b9d 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20  leName, 0);.    
26b9e 67 6f 74 6f 20 74 72 69 67 67 65 72 5f 63 6c 65  goto trigger_cle
26b9f 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69 66 28 20  anup;.  }.  if( 
26ba0 21 70 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 26  !pTab->pSelect &
26ba1 26 20 74 72 5f 74 6d 3d 3d 54 4b 5f 49 4e 53 54  & tr_tm==TK_INST
26ba2 45 41 44 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  EAD ){.    sqlit
26ba3 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
26ba4 65 2c 20 22 63 61 6e 6e 6f 74 20 63 72 65 61 74  e, "cannot creat
26ba5 65 20 49 4e 53 54 45 41 44 20 4f 46 22 0a 20 20  e INSTEAD OF".  
26ba6 20 20 20 20 20 20 22 20 74 72 69 67 67 65 72 20        " trigger 
26ba7 6f 6e 20 74 61 62 6c 65 3a 20 25 53 22 2c 20 70  on table: %S", p
26ba8 54 61 62 6c 65 4e 61 6d 65 2c 20 30 29 3b 0a 20  TableName, 0);. 
26ba9 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72 5f     goto trigger_
26baa 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 69  cleanup;.  }.  i
26bab 54 61 62 44 62 20 3d 20 73 71 6c 69 74 65 33 53  TabDb = sqlite3S
26bac 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c  chemaToIndex(db,
26bad 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
26bae 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
26baf 5f 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54  _OMIT_AUTHORIZAT
26bb0 49 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20  ION.  {.    int 
26bb1 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 43 52  code = SQLITE_CR
26bb2 45 41 54 45 5f 54 52 49 47 47 45 52 3b 0a 20 20  EATE_TRIGGER;.  
26bb3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
26bb4 62 20 3d 20 64 62 2d 3e 61 44 62 5b 69 54 61 62  b = db->aDb[iTab
26bb5 44 62 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 20 20 63  Db].zName;.    c
26bb6 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 54 72  onst char *zDbTr
26bb7 69 67 20 3d 20 69 73 54 65 6d 70 20 3f 20 64 62  ig = isTemp ? db
26bb8 2d 3e 61 44 62 5b 31 5d 2e 7a 4e 61 6d 65 20 3a  ->aDb[1].zName :
26bb9 20 7a 44 62 3b 0a 20 20 20 20 69 66 28 20 69 54   zDb;.    if( iT
26bba 61 62 44 62 3d 3d 31 20 7c 7c 20 69 73 54 65 6d  abDb==1 || isTem
26bbb 70 20 29 20 63 6f 64 65 20 3d 20 53 51 4c 49 54  p ) code = SQLIT
26bbc 45 5f 43 52 45 41 54 45 5f 54 45 4d 50 5f 54 52  E_CREATE_TEMP_TR
26bbd 49 47 47 45 52 3b 0a 20 20 20 20 69 66 28 20 73  IGGER;.    if( s
26bbe 71 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28  qlite3AuthCheck(
26bbf 70 50 61 72 73 65 2c 20 63 6f 64 65 2c 20 7a 4e  pParse, code, zN
26bc0 61 6d 65 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  ame, pTab->zName
26bc1 2c 20 7a 44 62 54 72 69 67 29 20 29 7b 0a 20 20  , zDbTrig) ){.  
26bc2 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
26bc3 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
26bc4 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 41      if( sqlite3A
26bc5 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65 2c  uthCheck(pParse,
26bc6 20 53 51 4c 49 54 45 5f 49 4e 53 45 52 54 2c 20   SQLITE_INSERT, 
26bc7 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 54 61  SCHEMA_TABLE(iTa
26bc8 62 44 62 29 2c 30 2c 7a 44 62 29 29 7b 0a 20 20  bDb),0,zDb)){.  
26bc9 20 20 20 20 67 6f 74 6f 20 74 72 69 67 67 65 72      goto trigger
26bca 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20 7d 0a  _cleanup;.    }.
26bcb 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a    }.#endif..  /*
26bcc 20 49 4e 53 54 45 41 44 20 4f 46 20 74 72 69 67   INSTEAD OF trig
26bcd 67 65 72 73 20 63 61 6e 20 6f 6e 6c 79 20 61 70  gers can only ap
26bce 70 65 61 72 20 6f 6e 20 76 69 65 77 73 20 61 6e  pear on views an
26bcf 64 20 42 45 46 4f 52 45 20 74 72 69 67 67 65 72  d BEFORE trigger
26bd0 73 0a 20 20 2a 2a 20 63 61 6e 6e 6f 74 20 61 70  s.  ** cannot ap
26bd1 70 65 61 72 20 6f 6e 20 76 69 65 77 73 2e 20 20  pear on views.  
26bd2 53 6f 20 77 65 20 6d 69 67 68 74 20 61 73 20 77  So we might as w
26bd3 65 6c 6c 20 74 72 61 6e 73 6c 61 74 65 20 65 76  ell translate ev
26bd4 65 72 79 0a 20 20 2a 2a 20 49 4e 53 54 45 41 44  ery.  ** INSTEAD
26bd5 20 4f 46 20 74 72 69 67 67 65 72 20 69 6e 74 6f   OF trigger into
26bd6 20 61 20 42 45 46 4f 52 45 20 74 72 69 67 67 65   a BEFORE trigge
26bd7 72 2e 20 20 49 74 20 73 69 6d 70 6c 69 66 69 65  r.  It simplifie
26bd8 73 20 63 6f 64 65 0a 20 20 2a 2a 20 65 6c 73 65  s code.  ** else
26bd9 77 68 65 72 65 2e 0a 20 20 2a 2f 0a 20 20 69 66  where..  */.  if
26bda 20 28 74 72 5f 74 6d 20 3d 3d 20 54 4b 5f 49 4e   (tr_tm == TK_IN
26bdb 53 54 45 41 44 29 7b 0a 20 20 20 20 74 72 5f 74  STEAD){.    tr_t
26bdc 6d 20 3d 20 54 4b 5f 42 45 46 4f 52 45 3b 0a 20  m = TK_BEFORE;. 
26bdd 20 7d 0a 0a 20 20 2f 2a 20 42 75 69 6c 64 20 74   }..  /* Build t
26bde 68 65 20 54 72 69 67 67 65 72 20 6f 62 6a 65 63  he Trigger objec
26bdf 74 20 2a 2f 0a 20 20 70 54 72 69 67 67 65 72 20  t */.  pTrigger 
26be0 3d 20 28 54 72 69 67 67 65 72 2a 29 73 71 6c 69  = (Trigger*)sqli
26be1 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28  te3DbMallocZero(
26be2 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67 67  db, sizeof(Trigg
26be3 65 72 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69  er));.  if( pTri
26be4 67 67 65 72 3d 3d 30 20 29 20 67 6f 74 6f 20 74  gger==0 ) goto t
26be5 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a  rigger_cleanup;.
26be6 20 20 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65    pTrigger->name
26be7 20 3d 20 7a 4e 61 6d 65 3b 0a 20 20 7a 4e 61 6d   = zName;.  zNam
26be8 65 20 3d 20 30 3b 0a 20 20 70 54 72 69 67 67 65  e = 0;.  pTrigge
26be9 72 2d 3e 74 61 62 6c 65 20 3d 20 73 71 6c 69 74  r->table = sqlit
26bea 65 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 70  e3DbStrDup(db, p
26beb 54 61 62 6c 65 4e 61 6d 65 2d 3e 61 5b 30 5d 2e  TableName->a[0].
26bec 7a 4e 61 6d 65 29 3b 0a 20 20 70 54 72 69 67 67  zName);.  pTrigg
26bed 65 72 2d 3e 70 53 63 68 65 6d 61 20 3d 20 64 62  er->pSchema = db
26bee 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65  ->aDb[iDb].pSche
26bef 6d 61 3b 0a 20 20 70 54 72 69 67 67 65 72 2d 3e  ma;.  pTrigger->
26bf0 70 54 61 62 53 63 68 65 6d 61 20 3d 20 70 54 61  pTabSchema = pTa
26bf1 62 2d 3e 70 53 63 68 65 6d 61 3b 0a 20 20 70 54  b->pSchema;.  pT
26bf2 72 69 67 67 65 72 2d 3e 6f 70 20 3d 20 6f 70 3b  rigger->op = op;
26bf3 0a 20 20 70 54 72 69 67 67 65 72 2d 3e 74 72 5f  .  pTrigger->tr_
26bf4 74 6d 20 3d 20 74 72 5f 74 6d 3d 3d 54 4b 5f 42  tm = tr_tm==TK_B
26bf5 45 46 4f 52 45 20 3f 20 54 52 49 47 47 45 52 5f  EFORE ? TRIGGER_
26bf6 42 45 46 4f 52 45 20 3a 20 54 52 49 47 47 45 52  BEFORE : TRIGGER
26bf7 5f 41 46 54 45 52 3b 0a 20 20 70 54 72 69 67 67  _AFTER;.  pTrigg
26bf8 65 72 2d 3e 70 57 68 65 6e 20 3d 20 73 71 6c 69  er->pWhen = sqli
26bf9 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
26bfa 57 68 65 6e 29 3b 0a 20 20 70 54 72 69 67 67 65  When);.  pTrigge
26bfb 72 2d 3e 70 43 6f 6c 75 6d 6e 73 20 3d 20 73 71  r->pColumns = sq
26bfc 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 64  lite3IdListDup(d
26bfd 62 2c 20 70 43 6f 6c 75 6d 6e 73 29 3b 0a 20 20  b, pColumns);.  
26bfe 73 71 6c 69 74 65 33 54 6f 6b 65 6e 43 6f 70 79  sqlite3TokenCopy
26bff 28 64 62 2c 20 26 70 54 72 69 67 67 65 72 2d 3e  (db, &pTrigger->
26c00 6e 61 6d 65 54 6f 6b 65 6e 2c 70 4e 61 6d 65 29  nameToken,pName)
26c01 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
26c02 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 3d  se->pNewTrigger=
26c03 3d 30 20 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  =0 );.  pParse->
26c04 70 4e 65 77 54 72 69 67 67 65 72 20 3d 20 70 54  pNewTrigger = pT
26c05 72 69 67 67 65 72 3b 0a 0a 74 72 69 67 67 65 72  rigger;..trigger
26c06 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73 71 6c 69  _cleanup:.  sqli
26c07 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4e  te3DbFree(db, zN
26c08 61 6d 65 29 3b 0a 20 20 73 71 6c 69 74 65 33 53  ame);.  sqlite3S
26c09 72 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c  rcListDelete(db,
26c0a 20 70 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20 20   pTableName);.  
26c0b 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65 6c  sqlite3IdListDel
26c0c 65 74 65 28 64 62 2c 20 70 43 6f 6c 75 6d 6e 73  ete(db, pColumns
26c0d 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
26c0e 44 65 6c 65 74 65 28 64 62 2c 20 70 57 68 65 6e  Delete(db, pWhen
26c0f 29 3b 0a 20 20 69 66 28 20 21 70 50 61 72 73 65  );.  if( !pParse
26c10 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20 29 7b  ->pNewTrigger ){
26c11 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65  .    sqlite3Dele
26c12 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 54  teTrigger(db, pT
26c13 72 69 67 67 65 72 29 3b 0a 20 20 7d 65 6c 73 65  rigger);.  }else
26c14 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  {.    assert( pP
26c15 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
26c16 72 3d 3d 70 54 72 69 67 67 65 72 20 29 3b 0a 20  r==pTrigger );. 
26c17 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73   }.}../*.** This
26c18 20 72 6f 75 74 69 6e 65 20 69 73 20 63 61 6c 6c   routine is call
26c19 65 64 20 61 66 74 65 72 20 61 6c 6c 20 6f 66 20  ed after all of 
26c1a 74 68 65 20 74 72 69 67 67 65 72 20 61 63 74 69  the trigger acti
26c1b 6f 6e 73 20 68 61 76 65 20 62 65 65 6e 20 70 61  ons have been pa
26c1c 72 73 65 64 0a 2a 2a 20 69 6e 20 6f 72 64 65 72  rsed.** in order
26c1d 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65   to complete the
26c1e 20 70 72 6f 63 65 73 73 20 6f 66 20 62 75 69 6c   process of buil
26c1f 64 69 6e 67 20 74 68 65 20 74 72 69 67 67 65 72  ding the trigger
26c20 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
26c21 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
26c22 46 69 6e 69 73 68 54 72 69 67 67 65 72 28 0a 20  FinishTrigger(. 
26c23 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
26c24 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61 72 73           /* Pars
26c25 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
26c26 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
26c27 65 70 4c 69 73 74 2c 20 2f 2a 20 54 68 65 20 74  epList, /* The t
26c28 72 69 67 67 65 72 65 64 20 70 72 6f 67 72 61 6d  riggered program
26c29 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 41 6c   */.  Token *pAl
26c2a 6c 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  l             /*
26c2b 20 54 6f 6b 65 6e 20 74 68 61 74 20 64 65 73 63   Token that desc
26c2c 72 69 62 65 73 20 74 68 65 20 63 6f 6d 70 6c 65  ribes the comple
26c2d 74 65 20 43 52 45 41 54 45 20 54 52 49 47 47 45  te CREATE TRIGGE
26c2e 52 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65  R */.){.  Trigge
26c2f 72 20 2a 70 54 72 69 67 20 3d 20 30 3b 20 20 20  r *pTrig = 0;   
26c30 20 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72    /* The trigger
26c31 20 77 68 6f 73 65 20 63 6f 6e 73 74 72 75 63 74   whose construct
26c32 69 6f 6e 20 69 73 20 66 69 6e 69 73 68 69 6e 67  ion is finishing
26c33 20 75 70 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33   up */.  sqlite3
26c34 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
26c35 62 3b 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62  b;  /* The datab
26c36 61 73 65 20 2a 2f 0a 20 20 44 62 46 69 78 65 72  ase */.  DbFixer
26c37 20 73 46 69 78 3b 0a 20 20 69 6e 74 20 69 44 62   sFix;.  int iDb
26c38 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
26c39 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
26c3a 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
26c3b 72 69 67 67 65 72 20 2a 2f 0a 0a 20 20 70 54 72  rigger */..  pTr
26c3c 69 67 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65  ig = pParse->pNe
26c3d 77 54 72 69 67 67 65 72 3b 0a 20 20 70 50 61 72  wTrigger;.  pPar
26c3e 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72 20  se->pNewTrigger 
26c3f 3d 20 30 3b 0a 20 20 69 66 28 20 70 50 61 72 73  = 0;.  if( pPars
26c40 65 2d 3e 6e 45 72 72 20 7c 7c 20 21 70 54 72 69  e->nErr || !pTri
26c41 67 20 29 20 67 6f 74 6f 20 74 72 69 67 67 65 72  g ) goto trigger
26c42 66 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3b 0a  finish_cleanup;.
26c43 20 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53    iDb = sqlite3S
26c44 63 68 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61  chemaToIndex(pPa
26c45 72 73 65 2d 3e 64 62 2c 20 70 54 72 69 67 2d 3e  rse->db, pTrig->
26c46 70 53 63 68 65 6d 61 29 3b 0a 20 20 70 54 72 69  pSchema);.  pTri
26c47 67 2d 3e 73 74 65 70 5f 6c 69 73 74 20 3d 20 70  g->step_list = p
26c48 53 74 65 70 4c 69 73 74 3b 0a 20 20 77 68 69 6c  StepList;.  whil
26c49 65 28 20 70 53 74 65 70 4c 69 73 74 20 29 7b 0a  e( pStepList ){.
26c4a 20 20 20 20 70 53 74 65 70 4c 69 73 74 2d 3e 70      pStepList->p
26c4b 54 72 69 67 20 3d 20 70 54 72 69 67 3b 0a 20 20  Trig = pTrig;.  
26c4c 20 20 70 53 74 65 70 4c 69 73 74 20 3d 20 70 53    pStepList = pS
26c4d 74 65 70 4c 69 73 74 2d 3e 70 4e 65 78 74 3b 0a  tepList->pNext;.
26c4e 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74 65    }.  if( sqlite
26c4f 33 46 69 78 49 6e 69 74 28 26 73 46 69 78 2c 20  3FixInit(&sFix, 
26c50 70 50 61 72 73 65 2c 20 69 44 62 2c 20 22 74 72  pParse, iDb, "tr
26c51 69 67 67 65 72 22 2c 20 26 70 54 72 69 67 2d 3e  igger", &pTrig->
26c52 6e 61 6d 65 54 6f 6b 65 6e 29 20 0a 20 20 20 20  nameToken) .    
26c53 20 20 20 20 20 20 26 26 20 73 71 6c 69 74 65 33        && sqlite3
26c54 46 69 78 54 72 69 67 67 65 72 53 74 65 70 28 26  FixTriggerStep(&
26c55 73 46 69 78 2c 20 70 54 72 69 67 2d 3e 73 74 65  sFix, pTrig->ste
26c56 70 5f 6c 69 73 74 29 20 29 7b 0a 20 20 20 20 67  p_list) ){.    g
26c57 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69 73  oto triggerfinis
26c58 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 0a  h_cleanup;.  }..
26c59 20 20 2f 2a 20 69 66 20 77 65 20 61 72 65 20 6e    /* if we are n
26c5a 6f 74 20 69 6e 69 74 69 61 6c 69 7a 69 6e 67 2c  ot initializing,
26c5b 20 61 6e 64 20 74 68 69 73 20 74 72 69 67 67 65   and this trigge
26c5c 72 20 69 73 20 6e 6f 74 20 6f 6e 20 61 20 54 45  r is not on a TE
26c5d 4d 50 20 74 61 62 6c 65 2c 20 0a 20 20 2a 2a 20  MP table, .  ** 
26c5e 62 75 69 6c 64 20 74 68 65 20 73 71 6c 69 74 65  build the sqlite
26c5f 5f 6d 61 73 74 65 72 20 65 6e 74 72 79 0a 20 20  _master entry.  
26c60 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d 3e 69 6e  */.  if( !db->in
26c61 69 74 2e 62 75 73 79 20 29 7b 0a 20 20 20 20 56  it.busy ){.    V
26c62 64 62 65 20 2a 76 3b 0a 20 20 20 20 63 68 61 72  dbe *v;.    char
26c63 20 2a 7a 3b 0a 0a 20 20 20 20 2f 2a 20 4d 61 6b   *z;..    /* Mak
26c64 65 20 61 6e 20 65 6e 74 72 79 20 69 6e 20 74 68  e an entry in th
26c65 65 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 20  e sqlite_master 
26c66 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 76 20 3d  table */.    v =
26c67 20 73 71 6c 69 74 65 33 47 65 74 56 64 62 65 28   sqlite3GetVdbe(
26c68 70 50 61 72 73 65 29 3b 0a 20 20 20 20 69 66 28  pParse);.    if(
26c69 20 76 3d 3d 30 20 29 20 67 6f 74 6f 20 74 72 69   v==0 ) goto tri
26c6a 67 67 65 72 66 69 6e 69 73 68 5f 63 6c 65 61 6e  ggerfinish_clean
26c6b 75 70 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42  up;.    sqlite3B
26c6c 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
26c6d 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
26c6e 62 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c 69  b);.    z = sqli
26c6f 74 65 33 44 62 53 74 72 4e 44 75 70 28 64 62 2c  te3DbStrNDup(db,
26c70 20 28 63 68 61 72 2a 29 70 41 6c 6c 2d 3e 7a 2c   (char*)pAll->z,
26c71 20 70 41 6c 6c 2d 3e 6e 29 3b 0a 20 20 20 20 73   pAll->n);.    s
26c72 71 6c 69 74 65 33 4e 65 73 74 65 64 50 61 72 73  qlite3NestedPars
26c73 65 28 70 50 61 72 73 65 2c 0a 20 20 20 20 20 20  e(pParse,.      
26c74 20 22 49 4e 53 45 52 54 20 49 4e 54 4f 20 25 51   "INSERT INTO %Q
26c75 2e 25 73 20 56 41 4c 55 45 53 28 27 74 72 69 67  .%s VALUES('trig
26c76 67 65 72 27 2c 25 51 2c 25 51 2c 30 2c 27 43 52  ger',%Q,%Q,0,'CR
26c77 45 41 54 45 20 54 52 49 47 47 45 52 20 25 71 27  EATE TRIGGER %q'
26c78 29 22 2c 0a 20 20 20 20 20 20 20 64 62 2d 3e 61  )",.       db->a
26c79 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 53  Db[iDb].zName, S
26c7a 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62 29  CHEMA_TABLE(iDb)
26c7b 2c 20 70 54 72 69 67 2d 3e 6e 61 6d 65 2c 0a 20  , pTrig->name,. 
26c7c 20 20 20 20 20 20 70 54 72 69 67 2d 3e 74 61 62        pTrig->tab
26c7d 6c 65 2c 20 7a 29 3b 0a 20 20 20 20 73 71 6c 69  le, z);.    sqli
26c7e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 29  te3DbFree(db, z)
26c7f 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
26c80 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
26c81 2c 20 69 44 62 29 3b 0a 20 20 20 20 73 71 6c 69  , iDb);.    sqli
26c82 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
26c83 20 4f 50 5f 50 61 72 73 65 53 63 68 65 6d 61 2c   OP_ParseSchema,
26c84 20 69 44 62 2c 20 30 2c 20 30 2c 20 73 71 6c 69   iDb, 0, 0, sqli
26c85 74 65 33 4d 50 72 69 6e 74 66 28 0a 20 20 20 20  te3MPrintf(.    
26c86 20 20 20 20 64 62 2c 20 22 74 79 70 65 3d 27 74      db, "type='t
26c87 72 69 67 67 65 72 27 20 41 4e 44 20 6e 61 6d 65  rigger' AND name
26c88 3d 27 25 71 27 22 2c 20 70 54 72 69 67 2d 3e 6e  ='%q'", pTrig->n
26c89 61 6d 65 29 2c 20 50 34 5f 44 59 4e 41 4d 49 43  ame), P4_DYNAMIC
26c8a 0a 20 20 20 20 29 3b 0a 20 20 7d 0a 0a 20 20 69  .    );.  }..  i
26c8b 66 28 20 64 62 2d 3e 69 6e 69 74 2e 62 75 73 79  f( db->init.busy
26c8c 20 29 7b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20   ){.    int n;. 
26c8d 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a     Table *pTab;.
26c8e 20 20 20 20 54 72 69 67 67 65 72 20 2a 70 44 65      Trigger *pDe
26c8f 6c 3b 0a 20 20 20 20 70 44 65 6c 20 3d 20 73 71  l;.    pDel = sq
26c90 6c 69 74 65 33 48 61 73 68 49 6e 73 65 72 74 28  lite3HashInsert(
26c91 26 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 70 53  &db->aDb[iDb].pS
26c92 63 68 65 6d 61 2d 3e 74 72 69 67 48 61 73 68 2c  chema->trigHash,
26c93 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
26c94 20 20 20 20 20 20 20 70 54 72 69 67 2d 3e 6e 61         pTrig->na
26c95 6d 65 2c 20 73 74 72 6c 65 6e 28 70 54 72 69 67  me, strlen(pTrig
26c96 2d 3e 6e 61 6d 65 29 2c 20 70 54 72 69 67 29 3b  ->name), pTrig);
26c97 0a 20 20 20 20 69 66 28 20 70 44 65 6c 20 29 7b  .    if( pDel ){
26c98 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
26c99 44 65 6c 3d 3d 70 54 72 69 67 20 29 3b 0a 20 20  Del==pTrig );.  
26c9a 20 20 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61      db->mallocFa
26c9b 69 6c 65 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  iled = 1;.      
26c9c 67 6f 74 6f 20 74 72 69 67 67 65 72 66 69 6e 69  goto triggerfini
26c9d 73 68 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  sh_cleanup;.    
26c9e 7d 0a 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e  }.    n = strlen
26c9f 28 70 54 72 69 67 2d 3e 74 61 62 6c 65 29 20 2b  (pTrig->table) +
26ca0 20 31 3b 0a 20 20 20 20 70 54 61 62 20 3d 20 73   1;.    pTab = s
26ca1 71 6c 69 74 65 33 48 61 73 68 46 69 6e 64 28 26  qlite3HashFind(&
26ca2 70 54 72 69 67 2d 3e 70 54 61 62 53 63 68 65 6d  pTrig->pTabSchem
26ca3 61 2d 3e 74 62 6c 48 61 73 68 2c 20 70 54 72 69  a->tblHash, pTri
26ca4 67 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 20 20  g->table, n);.  
26ca5 20 20 61 73 73 65 72 74 28 20 70 54 61 62 21 3d    assert( pTab!=
26ca6 30 20 29 3b 0a 20 20 20 20 70 54 72 69 67 2d 3e  0 );.    pTrig->
26ca7 70 4e 65 78 74 20 3d 20 70 54 61 62 2d 3e 70 54  pNext = pTab->pT
26ca8 72 69 67 67 65 72 3b 0a 20 20 20 20 70 54 61 62  rigger;.    pTab
26ca9 2d 3e 70 54 72 69 67 67 65 72 20 3d 20 70 54 72  ->pTrigger = pTr
26caa 69 67 3b 0a 20 20 20 20 70 54 72 69 67 20 3d 20  ig;.    pTrig = 
26cab 30 3b 0a 20 20 7d 0a 0a 74 72 69 67 67 65 72 66  0;.  }..triggerf
26cac 69 6e 69 73 68 5f 63 6c 65 61 6e 75 70 3a 0a 20  inish_cleanup:. 
26cad 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 72   sqlite3DeleteTr
26cae 69 67 67 65 72 28 64 62 2c 20 70 54 72 69 67 29  igger(db, pTrig)
26caf 3b 0a 20 20 61 73 73 65 72 74 28 20 21 70 50 61  ;.  assert( !pPa
26cb0 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65 72  rse->pNewTrigger
26cb1 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 6c   );.  sqlite3Del
26cb2 65 74 65 54 72 69 67 67 65 72 53 74 65 70 28 64  eteTriggerStep(d
26cb3 62 2c 20 70 53 74 65 70 4c 69 73 74 29 3b 0a 7d  b, pStepList);.}
26cb4 0a 0a 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 61 20 63  ../*.** Make a c
26cb5 6f 70 79 20 6f 66 20 61 6c 6c 20 63 6f 6d 70 6f  opy of all compo
26cb6 6e 65 6e 74 73 20 6f 66 20 74 68 65 20 67 69 76  nents of the giv
26cb7 65 6e 20 74 72 69 67 67 65 72 20 73 74 65 70 2e  en trigger step.
26cb8 20 20 54 68 69 73 20 68 61 73 0a 2a 2a 20 74 68    This has.** th
26cb9 65 20 65 66 66 65 63 74 20 6f 66 20 63 6f 70 79  e effect of copy
26cba 69 6e 67 20 61 6c 6c 20 45 78 70 72 2e 74 6f 6b  ing all Expr.tok
26cbb 65 6e 2e 7a 20 76 61 6c 75 65 73 20 69 6e 74 6f  en.z values into
26cbc 20 6d 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64   memory obtained
26cbd 0a 2a 2a 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  .** from sqlite3
26cbe 5f 6d 61 6c 6c 6f 63 28 29 2e 20 20 41 73 20 69  _malloc().  As i
26cbf 6e 69 74 69 61 6c 6c 79 20 63 72 65 61 74 65 64  nitially created
26cc0 2c 20 74 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e  , the Expr.token
26cc1 2e 7a 20 76 61 6c 75 65 73 0a 2a 2a 20 61 6c 6c  .z values.** all
26cc2 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 69 6e   point to the in
26cc3 70 75 74 20 73 74 72 69 6e 67 20 74 68 61 74 20  put string that 
26cc4 77 61 73 20 66 65 64 20 74 6f 20 74 68 65 20 70  was fed to the p
26cc5 61 72 73 65 72 2e 20 20 42 75 74 20 74 68 61 74  arser.  But that
26cc6 0a 2a 2a 20 73 74 72 69 6e 67 20 69 73 20 65 70  .** string is ep
26cc7 68 65 6d 65 72 61 6c 20 2d 20 69 74 20 77 69 6c  hemeral - it wil
26cc8 6c 20 67 6f 20 61 77 61 79 20 61 73 20 73 6f 6f  l go away as soo
26cc9 6e 20 61 73 20 74 68 65 20 73 71 6c 69 74 65 33  n as the sqlite3
26cca 5f 65 78 65 63 28 29 0a 2a 2a 20 63 61 6c 6c 20  _exec().** call 
26ccb 74 68 61 74 20 73 74 61 72 74 65 64 20 74 68 65  that started the
26ccc 20 70 61 72 73 65 72 20 65 78 69 74 73 2e 20 20   parser exits.  
26ccd 54 68 69 73 20 72 6f 75 74 69 6e 65 20 6d 61 6b  This routine mak
26cce 65 73 20 61 20 70 65 72 73 69 73 74 65 6e 74 0a  es a persistent.
26ccf 2a 2a 20 63 6f 70 79 20 6f 66 20 61 6c 6c 20 74  ** copy of all t
26cd0 68 65 20 45 78 70 72 2e 74 6f 6b 65 6e 2e 7a 20  he Expr.token.z 
26cd1 73 74 72 69 6e 67 73 20 73 6f 20 74 68 61 74 20  strings so that 
26cd2 74 68 65 20 54 72 69 67 67 65 72 53 74 65 70 20  the TriggerStep 
26cd3 73 74 72 75 63 74 75 72 65 0a 2a 2a 20 77 69 6c  structure.** wil
26cd4 6c 20 62 65 20 76 61 6c 69 64 20 65 76 65 6e 20  l be valid even 
26cd5 61 66 74 65 72 20 74 68 65 20 73 71 6c 69 74 65  after the sqlite
26cd6 33 5f 65 78 65 63 28 29 20 63 61 6c 6c 20 72 65  3_exec() call re
26cd7 74 75 72 6e 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  turns..*/.static
26cd8 20 76 6f 69 64 20 73 71 6c 69 74 65 50 65 72 73   void sqlitePers
26cd9 69 73 74 54 72 69 67 67 65 72 53 74 65 70 28 73  istTriggerStep(s
26cda 71 6c 69 74 65 33 20 2a 64 62 2c 20 54 72 69 67  qlite3 *db, Trig
26cdb 67 65 72 53 74 65 70 20 2a 70 29 7b 0a 20 20 69  gerStep *p){.  i
26cdc 66 28 20 70 2d 3e 74 61 72 67 65 74 2e 7a 20 29  f( p->target.z )
26cdd 7b 0a 20 20 20 20 70 2d 3e 74 61 72 67 65 74 2e  {.    p->target.
26cde 7a 20 3d 20 28 75 38 2a 29 73 71 6c 69 74 65 33  z = (u8*)sqlite3
26cdf 44 62 53 74 72 4e 44 75 70 28 64 62 2c 20 28 63  DbStrNDup(db, (c
26ce0 68 61 72 2a 29 70 2d 3e 74 61 72 67 65 74 2e 7a  har*)p->target.z
26ce1 2c 20 70 2d 3e 74 61 72 67 65 74 2e 6e 29 3b 0a  , p->target.n);.
26ce2 20 20 20 20 70 2d 3e 74 61 72 67 65 74 2e 64 79      p->target.dy
26ce3 6e 20 3d 20 31 3b 0a 20 20 7d 0a 20 20 69 66 28  n = 1;.  }.  if(
26ce4 20 70 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a 20   p->pSelect ){. 
26ce5 20 20 20 53 65 6c 65 63 74 20 2a 70 4e 65 77 20     Select *pNew 
26ce6 3d 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44  = sqlite3SelectD
26ce7 75 70 28 64 62 2c 20 70 2d 3e 70 53 65 6c 65 63  up(db, p->pSelec
26ce8 74 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 53  t);.    sqlite3S
26ce9 65 6c 65 63 74 44 65 6c 65 74 65 28 64 62 2c 20  electDelete(db, 
26cea 70 2d 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 20  p->pSelect);.   
26ceb 20 70 2d 3e 70 53 65 6c 65 63 74 20 3d 20 70 4e   p->pSelect = pN
26cec 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d  ew;.  }.  if( p-
26ced 3e 70 57 68 65 72 65 20 29 7b 0a 20 20 20 20 45  >pWhere ){.    E
26cee 78 70 72 20 2a 70 4e 65 77 20 3d 20 73 71 6c 69  xpr *pNew = sqli
26cef 74 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70  te3ExprDup(db, p
26cf0 2d 3e 70 57 68 65 72 65 29 3b 0a 20 20 20 20 73  ->pWhere);.    s
26cf1 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65 74 65  qlite3ExprDelete
26cf2 28 64 62 2c 20 70 2d 3e 70 57 68 65 72 65 29 3b  (db, p->pWhere);
26cf3 0a 20 20 20 20 70 2d 3e 70 57 68 65 72 65 20 3d  .    p->pWhere =
26cf4 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 69 66 28   pNew;.  }.  if(
26cf5 20 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 29 7b   p->pExprList ){
26cf6 0a 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70  .    ExprList *p
26cf7 4e 65 77 20 3d 20 73 71 6c 69 74 65 33 45 78 70  New = sqlite3Exp
26cf8 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 2d 3e  rListDup(db, p->
26cf9 70 45 78 70 72 4c 69 73 74 29 3b 0a 20 20 20 20  pExprList);.    
26cfa 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
26cfb 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 45 78  elete(db, p->pEx
26cfc 70 72 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e  prList);.    p->
26cfd 70 45 78 70 72 4c 69 73 74 20 3d 20 70 4e 65 77  pExprList = pNew
26cfe 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 2d 3e 70  ;.  }.  if( p->p
26cff 49 64 4c 69 73 74 20 29 7b 0a 20 20 20 20 49 64  IdList ){.    Id
26d00 4c 69 73 74 20 2a 70 4e 65 77 20 3d 20 73 71 6c  List *pNew = sql
26d01 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 64 62  ite3IdListDup(db
26d02 2c 20 70 2d 3e 70 49 64 4c 69 73 74 29 3b 0a 20  , p->pIdList);. 
26d03 20 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74     sqlite3IdList
26d04 44 65 6c 65 74 65 28 64 62 2c 20 70 2d 3e 70 49  Delete(db, p->pI
26d05 64 4c 69 73 74 29 3b 0a 20 20 20 20 70 2d 3e 70  dList);.    p->p
26d06 49 64 4c 69 73 74 20 3d 20 70 4e 65 77 3b 0a 20  IdList = pNew;. 
26d07 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 75 72 6e   }.}../*.** Turn
26d08 20 61 20 53 45 4c 45 43 54 20 73 74 61 74 65 6d   a SELECT statem
26d09 65 6e 74 20 28 74 68 61 74 20 74 68 65 20 70 53  ent (that the pS
26d0a 65 6c 65 63 74 20 70 61 72 61 6d 65 74 65 72 20  elect parameter 
26d0b 70 6f 69 6e 74 73 20 74 6f 29 20 69 6e 74 6f 0a  points to) into.
26d0c 2a 2a 20 61 20 74 72 69 67 67 65 72 20 73 74 65  ** a trigger ste
26d0d 70 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  p.  Return a poi
26d0e 6e 74 65 72 20 74 6f 20 61 20 54 72 69 67 67 65  nter to a Trigge
26d0f 72 53 74 65 70 20 73 74 72 75 63 74 75 72 65 2e  rStep structure.
26d10 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73 65  .**.** The parse
26d11 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  r calls this rou
26d12 74 69 6e 65 20 77 68 65 6e 20 69 74 20 66 69 6e  tine when it fin
26d13 64 73 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  ds a SELECT stat
26d14 65 6d 65 6e 74 20 69 6e 0a 2a 2a 20 62 6f 64 79  ement in.** body
26d15 20 6f 66 20 61 20 54 52 49 47 47 45 52 2e 20 20   of a TRIGGER.  
26d16 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
26d17 54 45 20 54 72 69 67 67 65 72 53 74 65 70 20 2a  TE TriggerStep *
26d18 73 71 6c 69 74 65 33 54 72 69 67 67 65 72 53 65  sqlite3TriggerSe
26d19 6c 65 63 74 53 74 65 70 28 73 71 6c 69 74 65 33  lectStep(sqlite3
26d1a 20 2a 64 62 2c 20 53 65 6c 65 63 74 20 2a 70 53   *db, Select *pS
26d1b 65 6c 65 63 74 29 7b 0a 20 20 54 72 69 67 67 65  elect){.  Trigge
26d1c 72 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53  rStep *pTriggerS
26d1d 74 65 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  tep = sqlite3DbM
26d1e 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69  allocZero(db, si
26d1f 7a 65 6f 66 28 54 72 69 67 67 65 72 53 74 65 70  zeof(TriggerStep
26d20 29 29 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67  ));.  if( pTrigg
26d21 65 72 53 74 65 70 3d 3d 30 20 29 20 7b 0a 20 20  erStep==0 ) {.  
26d22 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74 44    sqlite3SelectD
26d23 65 6c 65 74 65 28 64 62 2c 20 70 53 65 6c 65 63  elete(db, pSelec
26d24 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 30  t);.    return 0
26d25 3b 0a 20 20 7d 0a 0a 20 20 70 54 72 69 67 67 65  ;.  }..  pTrigge
26d26 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54 4b 5f 53  rStep->op = TK_S
26d27 45 4c 45 43 54 3b 0a 20 20 70 54 72 69 67 67 65  ELECT;.  pTrigge
26d28 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74 20 3d  rStep->pSelect =
26d29 20 70 53 65 6c 65 63 74 3b 0a 20 20 70 54 72 69   pSelect;.  pTri
26d2a 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
26d2b 20 3d 20 4f 45 5f 44 65 66 61 75 6c 74 3b 0a 20   = OE_Default;. 
26d2c 20 73 71 6c 69 74 65 50 65 72 73 69 73 74 54 72   sqlitePersistTr
26d2d 69 67 67 65 72 53 74 65 70 28 64 62 2c 20 70 54  iggerStep(db, pT
26d2e 72 69 67 67 65 72 53 74 65 70 29 3b 0a 0a 20 20  riggerStep);..  
26d2f 72 65 74 75 72 6e 20 70 54 72 69 67 67 65 72 53  return pTriggerS
26d30 74 65 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 42 75  tep;.}../*.** Bu
26d31 69 6c 64 20 61 20 74 72 69 67 67 65 72 20 73 74  ild a trigger st
26d32 65 70 20 6f 75 74 20 6f 66 20 61 6e 20 49 4e 53  ep out of an INS
26d33 45 52 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ERT statement.  
26d34 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  Return a pointer
26d35 0a 2a 2a 20 74 6f 20 74 68 65 20 6e 65 77 20 74  .** to the new t
26d36 72 69 67 67 65 72 20 73 74 65 70 2e 0a 2a 2a 0a  rigger step..**.
26d37 2a 2a 20 54 68 65 20 70 61 72 73 65 72 20 63 61  ** The parser ca
26d38 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65  lls this routine
26d39 20 77 68 65 6e 20 69 74 20 73 65 65 73 20 61 6e   when it sees an
26d3a 20 49 4e 53 45 52 54 20 69 6e 73 69 64 65 20 74   INSERT inside t
26d3b 68 65 0a 2a 2a 20 62 6f 64 79 20 6f 66 20 61 20  he.** body of a 
26d3c 74 72 69 67 67 65 72 2e 0a 2a 2f 0a 53 51 4c 49  trigger..*/.SQLI
26d3d 54 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67  TE_PRIVATE Trigg
26d3e 65 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54  erStep *sqlite3T
26d3f 72 69 67 67 65 72 49 6e 73 65 72 74 53 74 65 70  riggerInsertStep
26d40 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c  (.  sqlite3 *db,
26d41 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
26d42 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
26d43 6f 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  on */.  Token *p
26d44 54 61 62 6c 65 4e 61 6d 65 2c 20 20 2f 2a 20 4e  TableName,  /* N
26d45 61 6d 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65  ame of the table
26d46 20 69 6e 74 6f 20 77 68 69 63 68 20 77 65 20 69   into which we i
26d47 6e 73 65 72 74 20 2a 2f 0a 20 20 49 64 4c 69 73  nsert */.  IdLis
26d48 74 20 2a 70 43 6f 6c 75 6d 6e 2c 20 20 20 20 2f  t *pColumn,    /
26d49 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d 6e  * List of column
26d4a 73 20 69 6e 20 70 54 61 62 6c 65 4e 61 6d 65 20  s in pTableName 
26d4b 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 2a  to insert into *
26d4c 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70 45  /.  ExprList *pE
26d4d 4c 69 73 74 2c 20 20 20 2f 2a 20 54 68 65 20 56  List,   /* The V
26d4e 41 4c 55 45 20 63 6c 61 75 73 65 3a 20 61 20 6c  ALUE clause: a l
26d4f 69 73 74 20 6f 66 20 76 61 6c 75 65 73 20 74 6f  ist of values to
26d50 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a 2f 0a   be inserted */.
26d51 20 20 53 65 6c 65 63 74 20 2a 70 53 65 6c 65 63    Select *pSelec
26d52 74 2c 20 20 20 20 2f 2a 20 41 20 53 45 4c 45 43  t,    /* A SELEC
26d53 54 20 73 74 61 74 65 6d 65 6e 74 20 74 68 61 74  T statement that
26d54 20 73 75 70 70 6c 69 65 73 20 76 61 6c 75 65 73   supplies values
26d55 20 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66   */.  int orconf
26d56 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
26d57 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69   conflict algori
26d58 74 68 6d 20 28 4f 45 5f 41 62 6f 72 74 2c 20 4f  thm (OE_Abort, O
26d59 45 5f 52 65 70 6c 61 63 65 2c 20 65 74 63 2e 29  E_Replace, etc.)
26d5a 20 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72   */.){.  Trigger
26d5b 53 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74  Step *pTriggerSt
26d5c 65 70 3b 0a 0a 20 20 61 73 73 65 72 74 28 70 45  ep;..  assert(pE
26d5d 4c 69 73 74 20 3d 3d 20 30 20 7c 7c 20 70 53 65  List == 0 || pSe
26d5e 6c 65 63 74 20 3d 3d 20 30 29 3b 0a 20 20 61 73  lect == 0);.  as
26d5f 73 65 72 74 28 70 45 4c 69 73 74 20 21 3d 20 30  sert(pEList != 0
26d60 20 7c 7c 20 70 53 65 6c 65 63 74 20 21 3d 20 30   || pSelect != 0
26d61 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61   || db->mallocFa
26d62 69 6c 65 64 29 3b 0a 0a 20 20 70 54 72 69 67 67  iled);..  pTrigg
26d63 65 72 53 74 65 70 20 3d 20 73 71 6c 69 74 65 33  erStep = sqlite3
26d64 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c  DbMallocZero(db,
26d65 20 73 69 7a 65 6f 66 28 54 72 69 67 67 65 72 53   sizeof(TriggerS
26d66 74 65 70 29 29 3b 0a 20 20 69 66 28 20 70 54 72  tep));.  if( pTr
26d67 69 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20  iggerStep ){.   
26d68 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 6f   pTriggerStep->o
26d69 70 20 3d 20 54 4b 5f 49 4e 53 45 52 54 3b 0a 20  p = TK_INSERT;. 
26d6a 20 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d     pTriggerStep-
26d6b 3e 70 53 65 6c 65 63 74 20 3d 20 70 53 65 6c 65  >pSelect = pSele
26d6c 63 74 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72  ct;.    pTrigger
26d6d 53 74 65 70 2d 3e 74 61 72 67 65 74 20 20 3d 20  Step->target  = 
26d6e 2a 70 54 61 62 6c 65 4e 61 6d 65 3b 0a 20 20 20  *pTableName;.   
26d6f 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
26d70 49 64 4c 69 73 74 20 3d 20 70 43 6f 6c 75 6d 6e  IdList = pColumn
26d71 3b 0a 20 20 20 20 70 54 72 69 67 67 65 72 53 74  ;.    pTriggerSt
26d72 65 70 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20  ep->pExprList = 
26d73 70 45 4c 69 73 74 3b 0a 20 20 20 20 70 54 72 69  pEList;.    pTri
26d74 67 67 65 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66  ggerStep->orconf
26d75 20 3d 20 6f 72 63 6f 6e 66 3b 0a 20 20 20 20 73   = orconf;.    s
26d76 71 6c 69 74 65 50 65 72 73 69 73 74 54 72 69 67  qlitePersistTrig
26d77 67 65 72 53 74 65 70 28 64 62 2c 20 70 54 72 69  gerStep(db, pTri
26d78 67 67 65 72 53 74 65 70 29 3b 0a 20 20 7d 65 6c  ggerStep);.  }el
26d79 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 49  se{.    sqlite3I
26d7a 64 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  dListDelete(db, 
26d7b 70 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 73 71  pColumn);.    sq
26d7c 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
26d7d 65 74 65 28 64 62 2c 20 70 45 4c 69 73 74 29 3b  ete(db, pEList);
26d7e 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65  .    sqlite3Sele
26d7f 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
26d80 6c 65 63 74 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  lect);.  }..  re
26d81 74 75 72 6e 20 70 54 72 69 67 67 65 72 53 74 65  turn pTriggerSte
26d82 70 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73  p;.}../*.** Cons
26d83 74 72 75 63 74 20 61 20 74 72 69 67 67 65 72 20  truct a trigger 
26d84 73 74 65 70 20 74 68 61 74 20 69 6d 70 6c 65 6d  step that implem
26d85 65 6e 74 73 20 61 6e 20 55 50 44 41 54 45 20 73  ents an UPDATE s
26d86 74 61 74 65 6d 65 6e 74 20 61 6e 64 20 72 65 74  tatement and ret
26d87 75 72 6e 0a 2a 2a 20 61 20 70 6f 69 6e 74 65 72  urn.** a pointer
26d88 20 74 6f 20 74 68 61 74 20 74 72 69 67 67 65 72   to that trigger
26d89 20 73 74 65 70 2e 20 20 54 68 65 20 70 61 72 73   step.  The pars
26d8a 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
26d8b 75 74 69 6e 65 20 77 68 65 6e 20 69 74 0a 2a 2a  utine when it.**
26d8c 20 73 65 65 73 20 61 6e 20 55 50 44 41 54 45 20   sees an UPDATE 
26d8d 73 74 61 74 65 6d 65 6e 74 20 69 6e 73 69 64 65  statement inside
26d8e 20 74 68 65 20 62 6f 64 79 20 6f 66 20 61 20 43   the body of a C
26d8f 52 45 41 54 45 20 54 52 49 47 47 45 52 2e 0a 2a  REATE TRIGGER..*
26d90 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
26d91 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 73 71   TriggerStep *sq
26d92 6c 69 74 65 33 54 72 69 67 67 65 72 55 70 64 61  lite3TriggerUpda
26d93 74 65 53 74 65 70 28 0a 20 20 73 71 6c 69 74 65  teStep(.  sqlite
26d94 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 2f  3 *db,         /
26d95 2a 20 54 68 65 20 64 61 74 61 62 61 73 65 20 63  * The database c
26d96 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 54  onnection */.  T
26d97 6f 6b 65 6e 20 2a 70 54 61 62 6c 65 4e 61 6d 65  oken *pTableName
26d98 2c 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74  ,   /* Name of t
26d99 68 65 20 74 61 62 6c 65 20 74 6f 20 62 65 20 75  he table to be u
26d9a 70 64 61 74 65 64 20 2a 2f 0a 20 20 45 78 70 72  pdated */.  Expr
26d9b 4c 69 73 74 20 2a 70 45 4c 69 73 74 2c 20 20 20  List *pEList,   
26d9c 20 2f 2a 20 54 68 65 20 53 45 54 20 63 6c 61 75   /* The SET clau
26d9d 73 65 3a 20 6c 69 73 74 20 6f 66 20 63 6f 6c 75  se: list of colu
26d9e 6d 6e 20 61 6e 64 20 6e 65 77 20 76 61 6c 75 65  mn and new value
26d9f 73 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68  s */.  Expr *pWh
26da0 65 72 65 2c 20 20 20 20 20 20 20 20 2f 2a 20 54  ere,        /* T
26da1 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  he WHERE clause 
26da2 2a 2f 0a 20 20 69 6e 74 20 6f 72 63 6f 6e 66 20  */.  int orconf 
26da3 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
26da4 20 63 6f 6e 66 6c 69 63 74 20 61 6c 67 6f 72 69   conflict algori
26da5 74 68 6d 2e 20 28 4f 45 5f 41 62 6f 72 74 2c 20  thm. (OE_Abort, 
26da6 4f 45 5f 49 67 6e 6f 72 65 2c 20 65 74 63 29 20  OE_Ignore, etc) 
26da7 2a 2f 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53  */.){.  TriggerS
26da8 74 65 70 20 2a 70 54 72 69 67 67 65 72 53 74 65  tep *pTriggerSte
26da9 70 20 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c  p = sqlite3DbMal
26daa 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65  locZero(db, size
26dab 6f 66 28 54 72 69 67 67 65 72 53 74 65 70 29 29  of(TriggerStep))
26dac 3b 0a 20 20 69 66 28 20 70 54 72 69 67 67 65 72  ;.  if( pTrigger
26dad 53 74 65 70 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Step==0 ){.     
26dae 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
26daf 65 6c 65 74 65 28 64 62 2c 20 70 45 4c 69 73 74  elete(db, pEList
26db0 29 3b 0a 20 20 20 20 20 73 71 6c 69 74 65 33 45  );.     sqlite3E
26db1 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
26db2 68 65 72 65 29 3b 0a 20 20 20 20 20 72 65 74 75  here);.     retu
26db3 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72  rn 0;.  }..  pTr
26db4 69 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20  iggerStep->op = 
26db5 54 4b 5f 55 50 44 41 54 45 3b 0a 20 20 70 54 72  TK_UPDATE;.  pTr
26db6 69 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65  iggerStep->targe
26db7 74 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65  t  = *pTableName
26db8 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
26db9 2d 3e 70 45 78 70 72 4c 69 73 74 20 3d 20 70 45  ->pExprList = pE
26dba 4c 69 73 74 3b 0a 20 20 70 54 72 69 67 67 65 72  List;.  pTrigger
26dbb 53 74 65 70 2d 3e 70 57 68 65 72 65 20 3d 20 70  Step->pWhere = p
26dbc 57 68 65 72 65 3b 0a 20 20 70 54 72 69 67 67 65  Where;.  pTrigge
26dbd 72 53 74 65 70 2d 3e 6f 72 63 6f 6e 66 20 3d 20  rStep->orconf = 
26dbe 6f 72 63 6f 6e 66 3b 0a 20 20 73 71 6c 69 74 65  orconf;.  sqlite
26dbf 50 65 72 73 69 73 74 54 72 69 67 67 65 72 53 74  PersistTriggerSt
26dc0 65 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53  ep(db, pTriggerS
26dc1 74 65 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  tep);..  return 
26dc2 70 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a  pTriggerStep;.}.
26dc3 0a 2f 2a 0a 2a 2a 20 43 6f 6e 73 74 72 75 63 74  ./*.** Construct
26dc4 20 61 20 74 72 69 67 67 65 72 20 73 74 65 70 20   a trigger step 
26dc5 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 73 20  that implements 
26dc6 61 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  a DELETE stateme
26dc7 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 0a 2a 2a  nt and return.**
26dc8 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
26dc9 61 74 20 74 72 69 67 67 65 72 20 73 74 65 70 2e  at trigger step.
26dca 20 20 54 68 65 20 70 61 72 73 65 72 20 63 61 6c    The parser cal
26dcb 6c 73 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ls this routine 
26dcc 77 68 65 6e 20 69 74 0a 2a 2a 20 73 65 65 73 20  when it.** sees 
26dcd 61 20 44 45 4c 45 54 45 20 73 74 61 74 65 6d 65  a DELETE stateme
26dce 6e 74 20 69 6e 73 69 64 65 20 74 68 65 20 62 6f  nt inside the bo
26dcf 64 79 20 6f 66 20 61 20 43 52 45 41 54 45 20 54  dy of a CREATE T
26dd0 52 49 47 47 45 52 2e 0a 2a 2f 0a 53 51 4c 49 54  RIGGER..*/.SQLIT
26dd1 45 5f 50 52 49 56 41 54 45 20 54 72 69 67 67 65  E_PRIVATE Trigge
26dd2 72 53 74 65 70 20 2a 73 71 6c 69 74 65 33 54 72  rStep *sqlite3Tr
26dd3 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
26dd4 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
26dd5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
26dd6 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f  tabase connectio
26dd7 6e 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70 54  n */.  Token *pT
26dd8 61 62 6c 65 4e 61 6d 65 2c 20 20 20 20 20 20 2f  ableName,      /
26dd9 2a 20 54 68 65 20 74 61 62 6c 65 20 66 72 6f 6d  * The table from
26dda 20 77 68 69 63 68 20 72 6f 77 73 20 61 72 65 20   which rows are 
26ddb 64 65 6c 65 74 65 64 20 2a 2f 0a 20 20 45 78 70  deleted */.  Exp
26ddc 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20  r *pWhere       
26ddd 20 20 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52       /* The WHER
26dde 45 20 63 6c 61 75 73 65 20 2a 2f 0a 29 7b 0a 20  E clause */.){. 
26ddf 20 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 54   TriggerStep *pT
26de0 72 69 67 67 65 72 53 74 65 70 20 3d 20 73 71 6c  riggerStep = sql
26de1 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
26de2 28 64 62 2c 20 73 69 7a 65 6f 66 28 54 72 69 67  (db, sizeof(Trig
26de3 67 65 72 53 74 65 70 29 29 3b 0a 20 20 69 66 28  gerStep));.  if(
26de4 20 70 54 72 69 67 67 65 72 53 74 65 70 3d 3d 30   pTriggerStep==0
26de5 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45   ){.    sqlite3E
26de6 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 57  xprDelete(db, pW
26de7 68 65 72 65 29 3b 0a 20 20 20 20 72 65 74 75 72  here);.    retur
26de8 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 70 54 72 69  n 0;.  }..  pTri
26de9 67 67 65 72 53 74 65 70 2d 3e 6f 70 20 3d 20 54  ggerStep->op = T
26dea 4b 5f 44 45 4c 45 54 45 3b 0a 20 20 70 54 72 69  K_DELETE;.  pTri
26deb 67 67 65 72 53 74 65 70 2d 3e 74 61 72 67 65 74  ggerStep->target
26dec 20 20 3d 20 2a 70 54 61 62 6c 65 4e 61 6d 65 3b    = *pTableName;
26ded 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  .  pTriggerStep-
26dee 3e 70 57 68 65 72 65 20 3d 20 70 57 68 65 72 65  >pWhere = pWhere
26def 3b 0a 20 20 70 54 72 69 67 67 65 72 53 74 65 70  ;.  pTriggerStep
26df0 2d 3e 6f 72 63 6f 6e 66 20 3d 20 4f 45 5f 44 65  ->orconf = OE_De
26df1 66 61 75 6c 74 3b 0a 20 20 73 71 6c 69 74 65 50  fault;.  sqliteP
26df2 65 72 73 69 73 74 54 72 69 67 67 65 72 53 74 65  ersistTriggerSte
26df3 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 53 74  p(db, pTriggerSt
26df4 65 70 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 70  ep);..  return p
26df5 54 72 69 67 67 65 72 53 74 65 70 3b 0a 7d 0a 0a  TriggerStep;.}..
26df6 2f 2a 20 0a 2a 2a 20 52 65 63 75 72 73 69 76 65  /* .** Recursive
26df7 6c 79 20 64 65 6c 65 74 65 20 61 20 54 72 69 67  ly delete a Trig
26df8 67 65 72 20 73 74 72 75 63 74 75 72 65 0a 2a 2f  ger structure.*/
26df9 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
26dfa 76 6f 69 64 20 73 71 6c 69 74 65 33 44 65 6c 65  void sqlite3Dele
26dfb 74 65 54 72 69 67 67 65 72 28 73 71 6c 69 74 65  teTrigger(sqlite
26dfc 33 20 2a 64 62 2c 20 54 72 69 67 67 65 72 20 2a  3 *db, Trigger *
26dfd 70 54 72 69 67 67 65 72 29 7b 0a 20 20 69 66 28  pTrigger){.  if(
26dfe 20 70 54 72 69 67 67 65 72 3d 3d 30 20 29 20 72   pTrigger==0 ) r
26dff 65 74 75 72 6e 3b 0a 20 20 73 71 6c 69 74 65 33  eturn;.  sqlite3
26e00 44 65 6c 65 74 65 54 72 69 67 67 65 72 53 74 65  DeleteTriggerSte
26e01 70 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e  p(db, pTrigger->
26e02 73 74 65 70 5f 6c 69 73 74 29 3b 0a 20 20 73 71  step_list);.  sq
26e03 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
26e04 70 54 72 69 67 67 65 72 2d 3e 6e 61 6d 65 29 3b  pTrigger->name);
26e05 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
26e06 28 64 62 2c 20 70 54 72 69 67 67 65 72 2d 3e 74  (db, pTrigger->t
26e07 61 62 6c 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  able);.  sqlite3
26e08 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
26e09 54 72 69 67 67 65 72 2d 3e 70 57 68 65 6e 29 3b  Trigger->pWhen);
26e0a 0a 20 20 73 71 6c 69 74 65 33 49 64 4c 69 73 74  .  sqlite3IdList
26e0b 44 65 6c 65 74 65 28 64 62 2c 20 70 54 72 69 67  Delete(db, pTrig
26e0c 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e 73 29 3b 0a  ger->pColumns);.
26e0d 20 20 69 66 28 20 70 54 72 69 67 67 65 72 2d 3e    if( pTrigger->
26e0e 6e 61 6d 65 54 6f 6b 65 6e 2e 64 79 6e 20 29 20  nameToken.dyn ) 
26e0f 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62  sqlite3DbFree(db
26e10 2c 20 28 63 68 61 72 2a 29 70 54 72 69 67 67 65  , (char*)pTrigge
26e11 72 2d 3e 6e 61 6d 65 54 6f 6b 65 6e 2e 7a 29 3b  r->nameToken.z);
26e12 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
26e13 28 64 62 2c 20 70 54 72 69 67 67 65 72 29 3b 0a  (db, pTrigger);.
26e14 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
26e15 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c 6c 65 64  nction is called
26e16 20 74 6f 20 64 72 6f 70 20 61 20 74 72 69 67 67   to drop a trigg
26e17 65 72 20 66 72 6f 6d 20 74 68 65 20 64 61 74 61  er from the data
26e18 62 61 73 65 20 73 63 68 65 6d 61 2e 20 0a 2a 2a  base schema. .**
26e19 0a 2a 2a 20 54 68 69 73 20 6d 61 79 20 62 65 20  .** This may be 
26e1a 63 61 6c 6c 65 64 20 64 69 72 65 63 74 6c 79 20  called directly 
26e1b 66 72 6f 6d 20 74 68 65 20 70 61 72 73 65 72 20  from the parser 
26e1c 61 6e 64 20 74 68 65 72 65 66 6f 72 65 20 69 64  and therefore id
26e1d 65 6e 74 69 66 69 65 73 0a 2a 2a 20 74 68 65 20  entifies.** the 
26e1e 74 72 69 67 67 65 72 20 62 79 20 6e 61 6d 65 2e  trigger by name.
26e1f 20 20 54 68 65 20 73 71 6c 69 74 65 33 44 72 6f    The sqlite3Dro
26e20 70 54 72 69 67 67 65 72 50 74 72 28 29 20 72 6f  pTriggerPtr() ro
26e21 75 74 69 6e 65 20 64 6f 65 73 20 74 68 65 0a 2a  utine does the.*
26e22 2a 20 73 61 6d 65 20 6a 6f 62 20 61 73 20 74 68  * same job as th
26e23 69 73 20 72 6f 75 74 69 6e 65 20 65 78 63 65 70  is routine excep
26e24 74 20 69 74 20 74 61 6b 65 73 20 61 20 70 6f 69  t it takes a poi
26e25 6e 74 65 72 20 74 6f 20 74 68 65 20 74 72 69 67  nter to the trig
26e26 67 65 72 0a 2a 2a 20 69 6e 73 74 65 61 64 20 6f  ger.** instead o
26e27 66 20 74 68 65 20 74 72 69 67 67 65 72 20 6e 61  f the trigger na
26e28 6d 65 2e 0a 2a 2a 2f 0a 53 51 4c 49 54 45 5f 50  me..**/.SQLITE_P
26e29 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
26e2a 74 65 33 44 72 6f 70 54 72 69 67 67 65 72 28 50  te3DropTrigger(P
26e2b 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 53 72  arse *pParse, Sr
26e2c 63 4c 69 73 74 20 2a 70 4e 61 6d 65 2c 20 69 6e  cList *pName, in
26e2d 74 20 6e 6f 45 72 72 29 7b 0a 20 20 54 72 69 67  t noErr){.  Trig
26e2e 67 65 72 20 2a 70 54 72 69 67 67 65 72 20 3d 20  ger *pTrigger = 
26e2f 30 3b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 6f  0;.  int i;.  co
26e30 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 0a 20  nst char *zDb;. 
26e31 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
26e32 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  me;.  int nName;
26e33 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
26e34 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
26e35 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
26e36 69 6c 65 64 20 29 20 67 6f 74 6f 20 64 72 6f 70  iled ) goto drop
26e37 5f 74 72 69 67 67 65 72 5f 63 6c 65 61 6e 75 70  _trigger_cleanup
26e38 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f  ;.  if( SQLITE_O
26e39 4b 21 3d 73 71 6c 69 74 65 33 52 65 61 64 53 63  K!=sqlite3ReadSc
26e3a 68 65 6d 61 28 70 50 61 72 73 65 29 20 29 7b 0a  hema(pParse) ){.
26e3b 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72      goto drop_tr
26e3c 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
26e3d 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 70 4e   }..  assert( pN
26e3e 61 6d 65 2d 3e 6e 53 72 63 3d 3d 31 20 29 3b 0a  ame->nSrc==1 );.
26e3f 20 20 7a 44 62 20 3d 20 70 4e 61 6d 65 2d 3e 61    zDb = pName->a
26e40 5b 30 5d 2e 7a 44 61 74 61 62 61 73 65 3b 0a 20  [0].zDatabase;. 
26e41 20 7a 4e 61 6d 65 20 3d 20 70 4e 61 6d 65 2d 3e   zName = pName->
26e42 61 5b 30 5d 2e 7a 4e 61 6d 65 3b 0a 20 20 6e 4e  a[0].zName;.  nN
26e43 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e 61  ame = strlen(zNa
26e44 6d 65 29 3b 0a 20 20 66 6f 72 28 69 3d 4f 4d 49  me);.  for(i=OMI
26e45 54 5f 54 45 4d 50 44 42 3b 20 69 3c 64 62 2d 3e  T_TEMPDB; i<db->
26e46 6e 44 62 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69  nDb; i++){.    i
26e47 6e 74 20 6a 20 3d 20 28 69 3c 32 29 20 3f 20 69  nt j = (i<2) ? i
26e48 5e 31 20 3a 20 69 3b 20 20 2f 2a 20 53 65 61 72  ^1 : i;  /* Sear
26e49 63 68 20 54 45 4d 50 20 62 65 66 6f 72 65 20 4d  ch TEMP before M
26e4a 41 49 4e 20 2a 2f 0a 20 20 20 20 69 66 28 20 7a  AIN */.    if( z
26e4b 44 62 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  Db && sqlite3Str
26e4c 49 43 6d 70 28 64 62 2d 3e 61 44 62 5b 6a 5d 2e  ICmp(db->aDb[j].
26e4d 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 29 20 63 6f  zName, zDb) ) co
26e4e 6e 74 69 6e 75 65 3b 0a 20 20 20 20 70 54 72 69  ntinue;.    pTri
26e4f 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33 48 61  gger = sqlite3Ha
26e50 73 68 46 69 6e 64 28 26 28 64 62 2d 3e 61 44 62  shFind(&(db->aDb
26e51 5b 6a 5d 2e 70 53 63 68 65 6d 61 2d 3e 74 72 69  [j].pSchema->tri
26e52 67 48 61 73 68 29 2c 20 7a 4e 61 6d 65 2c 20 6e  gHash), zName, n
26e53 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
26e54 54 72 69 67 67 65 72 20 29 20 62 72 65 61 6b 3b  Trigger ) break;
26e55 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 54 72 69  .  }.  if( !pTri
26e56 67 67 65 72 20 29 7b 0a 20 20 20 20 69 66 28 20  gger ){.    if( 
26e57 21 6e 6f 45 72 72 20 29 7b 0a 20 20 20 20 20 20  !noErr ){.      
26e58 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
26e59 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
26e5a 20 74 72 69 67 67 65 72 3a 20 25 53 22 2c 20 70   trigger: %S", p
26e5b 4e 61 6d 65 2c 20 30 29 3b 0a 20 20 20 20 7d 0a  Name, 0);.    }.
26e5c 20 20 20 20 67 6f 74 6f 20 64 72 6f 70 5f 74 72      goto drop_tr
26e5d 69 67 67 65 72 5f 63 6c 65 61 6e 75 70 3b 0a 20  igger_cleanup;. 
26e5e 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 72 6f 70   }.  sqlite3Drop
26e5f 54 72 69 67 67 65 72 50 74 72 28 70 50 61 72 73  TriggerPtr(pPars
26e60 65 2c 20 70 54 72 69 67 67 65 72 29 3b 0a 0a 64  e, pTrigger);..d
26e61 72 6f 70 5f 74 72 69 67 67 65 72 5f 63 6c 65 61  rop_trigger_clea
26e62 6e 75 70 3a 0a 20 20 73 71 6c 69 74 65 33 53 72  nup:.  sqlite3Sr
26e63 63 4c 69 73 74 44 65 6c 65 74 65 28 64 62 2c 20  cListDelete(db, 
26e64 70 4e 61 6d 65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  pName);.}../*.**
26e65 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65   Return a pointe
26e66 72 20 74 6f 20 74 68 65 20 54 61 62 6c 65 20 73  r to the Table s
26e67 74 72 75 63 74 75 72 65 20 66 6f 72 20 74 68 65  tructure for the
26e68 20 74 61 62 6c 65 20 74 68 61 74 20 61 20 74 72   table that a tr
26e69 69 67 67 65 72 0a 2a 2a 20 69 73 20 73 65 74 20  igger.** is set 
26e6a 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 54 61  on..*/.static Ta
26e6b 62 6c 65 20 2a 74 61 62 6c 65 4f 66 54 72 69 67  ble *tableOfTrig
26e6c 67 65 72 28 54 72 69 67 67 65 72 20 2a 70 54 72  ger(Trigger *pTr
26e6d 69 67 67 65 72 29 7b 0a 20 20 69 6e 74 20 6e 20  igger){.  int n 
26e6e 3d 20 73 74 72 6c 65 6e 28 70 54 72 69 67 67 65  = strlen(pTrigge
26e6f 72 2d 3e 74 61 62 6c 65 29 20 2b 20 31 3b 0a 20  r->table) + 1;. 
26e70 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 48   return sqlite3H
26e71 61 73 68 46 69 6e 64 28 26 70 54 72 69 67 67 65  ashFind(&pTrigge
26e72 72 2d 3e 70 54 61 62 53 63 68 65 6d 61 2d 3e 74  r->pTabSchema->t
26e73 62 6c 48 61 73 68 2c 20 70 54 72 69 67 67 65 72  blHash, pTrigger
26e74 2d 3e 74 61 62 6c 65 2c 20 6e 29 3b 0a 7d 0a 0a  ->table, n);.}..
26e75 0a 2f 2a 0a 2a 2a 20 44 72 6f 70 20 61 20 74 72  ./*.** Drop a tr
26e76 69 67 67 65 72 20 67 69 76 65 6e 20 61 20 70 6f  igger given a po
26e77 69 6e 74 65 72 20 74 6f 20 74 68 61 74 20 74 72  inter to that tr
26e78 69 67 67 65 72 2e 20 0a 2a 2f 0a 53 51 4c 49 54  igger. .*/.SQLIT
26e79 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
26e7a 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
26e7b 72 50 74 72 28 50 61 72 73 65 20 2a 70 50 61 72  rPtr(Parse *pPar
26e7c 73 65 2c 20 54 72 69 67 67 65 72 20 2a 70 54 72  se, Trigger *pTr
26e7d 69 67 67 65 72 29 7b 0a 20 20 54 61 62 6c 65 20  igger){.  Table 
26e7e 20 20 2a 70 54 61 62 6c 65 3b 0a 20 20 56 64 62    *pTable;.  Vdb
26e7f 65 20 2a 76 3b 0a 20 20 73 71 6c 69 74 65 33 20  e *v;.  sqlite3 
26e80 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
26e81 3b 0a 20 20 69 6e 74 20 69 44 62 3b 0a 0a 20 20  ;.  int iDb;..  
26e82 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
26e83 65 6d 61 54 6f 49 6e 64 65 78 28 70 50 61 72 73  emaToIndex(pPars
26e84 65 2d 3e 64 62 2c 20 70 54 72 69 67 67 65 72 2d  e->db, pTrigger-
26e85 3e 70 53 63 68 65 6d 61 29 3b 0a 20 20 61 73 73  >pSchema);.  ass
26e86 65 72 74 28 20 69 44 62 3e 3d 30 20 26 26 20 69  ert( iDb>=0 && i
26e87 44 62 3c 64 62 2d 3e 6e 44 62 20 29 3b 0a 20 20  Db<db->nDb );.  
26e88 70 54 61 62 6c 65 20 3d 20 74 61 62 6c 65 4f 66  pTable = tableOf
26e89 54 72 69 67 67 65 72 28 70 54 72 69 67 67 65 72  Trigger(pTrigger
26e8a 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54 61  );.  assert( pTa
26e8b 62 6c 65 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ble );.  assert(
26e8c 20 70 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61   pTable->pSchema
26e8d 3d 3d 70 54 72 69 67 67 65 72 2d 3e 70 53 63 68  ==pTrigger->pSch
26e8e 65 6d 61 20 7c 7c 20 69 44 62 3d 3d 31 20 29 3b  ema || iDb==1 );
26e8f 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
26e90 4f 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49  OMIT_AUTHORIZATI
26e91 4f 4e 0a 20 20 7b 0a 20 20 20 20 69 6e 74 20 63  ON.  {.    int c
26e92 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52 4f  ode = SQLITE_DRO
26e93 50 5f 54 52 49 47 47 45 52 3b 0a 20 20 20 20 63  P_TRIGGER;.    c
26e94 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 20 3d  onst char *zDb =
26e95 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e   db->aDb[iDb].zN
26e96 61 6d 65 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  ame;.    const c
26e97 68 61 72 20 2a 7a 54 61 62 20 3d 20 53 43 48 45  har *zTab = SCHE
26e98 4d 41 5f 54 41 42 4c 45 28 69 44 62 29 3b 0a 20  MA_TABLE(iDb);. 
26e99 20 20 20 69 66 28 20 69 44 62 3d 3d 31 20 29 20     if( iDb==1 ) 
26e9a 63 6f 64 65 20 3d 20 53 51 4c 49 54 45 5f 44 52  code = SQLITE_DR
26e9b 4f 50 5f 54 45 4d 50 5f 54 52 49 47 47 45 52 3b  OP_TEMP_TRIGGER;
26e9c 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
26e9d 41 75 74 68 43 68 65 63 6b 28 70 50 61 72 73 65  AuthCheck(pParse
26e9e 2c 20 63 6f 64 65 2c 20 70 54 72 69 67 67 65 72  , code, pTrigger
26e9f 2d 3e 6e 61 6d 65 2c 20 70 54 61 62 6c 65 2d 3e  ->name, pTable->
26ea0 7a 4e 61 6d 65 2c 20 7a 44 62 29 20 7c 7c 0a 20  zName, zDb) ||. 
26ea1 20 20 20 20 20 73 71 6c 69 74 65 33 41 75 74 68       sqlite3Auth
26ea2 43 68 65 63 6b 28 70 50 61 72 73 65 2c 20 53 51  Check(pParse, SQ
26ea3 4c 49 54 45 5f 44 45 4c 45 54 45 2c 20 7a 54 61  LITE_DELETE, zTa
26ea4 62 2c 20 30 2c 20 7a 44 62 29 20 29 7b 0a 20 20  b, 0, zDb) ){.  
26ea5 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
26ea6 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  }.  }.#endif..  
26ea7 2f 2a 20 47 65 6e 65 72 61 74 65 20 63 6f 64 65  /* Generate code
26ea8 20 74 6f 20 64 65 73 74 72 6f 79 20 74 68 65 20   to destroy the 
26ea9 64 61 74 61 62 61 73 65 20 72 65 63 6f 72 64 20  database record 
26eaa 6f 66 20 74 68 65 20 74 72 69 67 67 65 72 2e 0a  of the trigger..
26eab 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
26eac 54 61 62 6c 65 21 3d 30 20 29 3b 0a 20 20 69 66  Table!=0 );.  if
26ead 28 20 28 76 20 3d 20 73 71 6c 69 74 65 33 47 65  ( (v = sqlite3Ge
26eae 74 56 64 62 65 28 70 50 61 72 73 65 29 29 21 3d  tVdbe(pParse))!=
26eaf 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 62 61 73  0 ){.    int bas
26eb0 65 3b 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f  e;.    static co
26eb1 6e 73 74 20 56 64 62 65 4f 70 4c 69 73 74 20 64  nst VdbeOpList d
26eb2 72 6f 70 54 72 69 67 67 65 72 5b 5d 20 3d 20 7b  ropTrigger[] = {
26eb3 0a 20 20 20 20 20 20 7b 20 4f 50 5f 52 65 77 69  .      { OP_Rewi
26eb4 6e 64 2c 20 20 20 20 20 30 2c 20 41 44 44 52 28  nd,     0, ADDR(
26eb5 39 29 2c 20 20 30 7d 2c 0a 20 20 20 20 20 20 7b  9),  0},.      {
26eb6 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
26eb7 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 1,        0},
26eb8 20 2f 2a 20 31 20 2a 2f 0a 20 20 20 20 20 20 7b   /* 1 */.      {
26eb9 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 20 20 20 20   OP_Column,     
26eba 30 2c 20 31 2c 20 20 20 20 20 20 20 20 32 7d 2c  0, 1,        2},
26ebb 0a 20 20 20 20 20 20 7b 20 4f 50 5f 4e 65 2c 20  .      { OP_Ne, 
26ebc 20 20 20 20 20 20 20 20 32 2c 20 41 44 44 52 28          2, ADDR(
26ebd 38 29 2c 20 20 31 7d 2c 0a 20 20 20 20 20 20 7b  8),  1},.      {
26ebe 20 4f 50 5f 53 74 72 69 6e 67 38 2c 20 20 20 20   OP_String8,    
26ebf 30 2c 20 31 2c 20 20 20 20 20 20 20 20 30 7d 2c  0, 1,        0},
26ec0 20 2f 2a 20 34 3a 20 22 74 72 69 67 67 65 72 22   /* 4: "trigger"
26ec1 20 2a 2f 0a 20 20 20 20 20 20 7b 20 4f 50 5f 43   */.      { OP_C
26ec2 6f 6c 75 6d 6e 2c 20 20 20 20 20 30 2c 20 30 2c  olumn,     0, 0,
26ec3 20 20 20 20 20 20 20 20 32 7d 2c 0a 20 20 20 20          2},.    
26ec4 20 20 7b 20 4f 50 5f 4e 65 2c 20 20 20 20 20 20    { OP_Ne,      
26ec5 20 20 20 32 2c 20 41 44 44 52 28 38 29 2c 20 20     2, ADDR(8),  
26ec6 31 7d 2c 0a 20 20 20 20 20 20 7b 20 4f 50 5f 44  1},.      { OP_D
26ec7 65 6c 65 74 65 2c 20 20 20 20 20 30 2c 20 30 2c  elete,     0, 0,
26ec8 20 20 20 20 20 20 20 20 30 7d 2c 0a 20 20 20 20          0},.    
26ec9 20 20 7b 20 4f 50 5f 4e 65 78 74 2c 20 20 20 20    { OP_Next,    
26eca 20 20 20 30 2c 20 41 44 44 52 28 31 29 2c 20 20     0, ADDR(1),  
26ecb 30 7d 2c 20 2f 2a 20 38 20 2a 2f 0a 20 20 20 20  0}, /* 8 */.    
26ecc 7d 3b 0a 0a 20 20 20 20 73 71 6c 69 74 65 33 42  };..    sqlite3B
26ecd 65 67 69 6e 57 72 69 74 65 4f 70 65 72 61 74 69  eginWriteOperati
26ece 6f 6e 28 70 50 61 72 73 65 2c 20 30 2c 20 69 44  on(pParse, 0, iD
26ecf 62 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 4f  b);.    sqlite3O
26ed0 70 65 6e 4d 61 73 74 65 72 54 61 62 6c 65 28 70  penMasterTable(p
26ed1 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20 20  Parse, iDb);.   
26ed2 20 62 61 73 65 20 3d 20 73 71 6c 69 74 65 33 56   base = sqlite3V
26ed3 64 62 65 41 64 64 4f 70 4c 69 73 74 28 76 2c 20  dbeAddOpList(v, 
26ed4 20 41 72 72 61 79 53 69 7a 65 28 64 72 6f 70 54   ArraySize(dropT
26ed5 72 69 67 67 65 72 29 2c 20 64 72 6f 70 54 72 69  rigger), dropTri
26ed6 67 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  gger);.    sqlit
26ed7 65 33 56 64 62 65 43 68 61 6e 67 65 50 34 28 76  e3VdbeChangeP4(v
26ed8 2c 20 62 61 73 65 2b 31 2c 20 70 54 72 69 67 67  , base+1, pTrigg
26ed9 65 72 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a 20 20  er->name, 0);.  
26eda 20 20 73 71 6c 69 74 65 33 56 64 62 65 43 68 61    sqlite3VdbeCha
26edb 6e 67 65 50 34 28 76 2c 20 62 61 73 65 2b 34 2c  ngeP4(v, base+4,
26edc 20 22 74 72 69 67 67 65 72 22 2c 20 50 34 5f 53   "trigger", P4_S
26edd 54 41 54 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  TATIC);.    sqli
26ede 74 65 33 43 68 61 6e 67 65 43 6f 6f 6b 69 65 28  te3ChangeCookie(
26edf 70 50 61 72 73 65 2c 20 69 44 62 29 3b 0a 20 20  pParse, iDb);.  
26ee0 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
26ee1 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
26ee2 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
26ee3 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
26ee4 20 4f 50 5f 44 72 6f 70 54 72 69 67 67 65 72 2c   OP_DropTrigger,
26ee5 20 69 44 62 2c 20 30 2c 20 30 2c 20 70 54 72 69   iDb, 0, 0, pTri
26ee6 67 67 65 72 2d 3e 6e 61 6d 65 2c 20 30 29 3b 0a  gger->name, 0);.
26ee7 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d    }.}../*.** Rem
26ee8 6f 76 65 20 61 20 74 72 69 67 67 65 72 20 66 72  ove a trigger fr
26ee9 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62 6c  om the hash tabl
26eea 65 73 20 6f 66 20 74 68 65 20 73 71 6c 69 74 65  es of the sqlite
26eeb 2a 20 70 6f 69 6e 74 65 72 2e 0a 2a 2f 0a 53 51  * pointer..*/.SQ
26eec 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
26eed 64 20 73 71 6c 69 74 65 33 55 6e 6c 69 6e 6b 41  d sqlite3UnlinkA
26eee 6e 64 44 65 6c 65 74 65 54 72 69 67 67 65 72 28  ndDeleteTrigger(
26eef 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
26ef0 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
26ef1 20 2a 7a 4e 61 6d 65 29 7b 0a 20 20 54 72 69 67   *zName){.  Trig
26ef2 67 65 72 20 2a 70 54 72 69 67 67 65 72 3b 0a 20  ger *pTrigger;. 
26ef3 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72   int nName = str
26ef4 6c 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 54  len(zName);.  pT
26ef5 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
26ef6 48 61 73 68 49 6e 73 65 72 74 28 26 28 64 62 2d  HashInsert(&(db-
26ef7 3e 61 44 62 5b 69 44 62 5d 2e 70 53 63 68 65 6d  >aDb[iDb].pSchem
26ef8 61 2d 3e 74 72 69 67 48 61 73 68 29 2c 0a 20 20  a->trigHash),.  
26ef9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
26efa 20 20 20 20 20 20 20 20 20 20 20 20 20 7a 4e 61               zNa
26efb 6d 65 2c 20 6e 4e 61 6d 65 2c 20 30 29 3b 0a 20  me, nName, 0);. 
26efc 20 69 66 28 20 70 54 72 69 67 67 65 72 20 29 7b   if( pTrigger ){
26efd 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54 61 62  .    Table *pTab
26efe 6c 65 20 3d 20 74 61 62 6c 65 4f 66 54 72 69 67  le = tableOfTrig
26eff 67 65 72 28 70 54 72 69 67 67 65 72 29 3b 0a 20  ger(pTrigger);. 
26f00 20 20 20 61 73 73 65 72 74 28 20 70 54 61 62 6c     assert( pTabl
26f01 65 21 3d 30 20 29 3b 0a 20 20 20 20 69 66 28 20  e!=0 );.    if( 
26f02 70 54 61 62 6c 65 2d 3e 70 54 72 69 67 67 65 72  pTable->pTrigger
26f03 20 3d 3d 20 70 54 72 69 67 67 65 72 20 29 7b 0a   == pTrigger ){.
26f04 20 20 20 20 20 20 70 54 61 62 6c 65 2d 3e 70 54        pTable->pT
26f05 72 69 67 67 65 72 20 3d 20 70 54 72 69 67 67 65  rigger = pTrigge
26f06 72 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 7d 65  r->pNext;.    }e
26f07 6c 73 65 7b 0a 20 20 20 20 20 20 54 72 69 67 67  lse{.      Trigg
26f08 65 72 20 2a 63 63 20 3d 20 70 54 61 62 6c 65 2d  er *cc = pTable-
26f09 3e 70 54 72 69 67 67 65 72 3b 0a 20 20 20 20 20  >pTrigger;.     
26f0a 20 77 68 69 6c 65 28 20 63 63 20 29 7b 20 0a 20   while( cc ){ . 
26f0b 20 20 20 20 20 20 20 69 66 28 20 63 63 2d 3e 70         if( cc->p
26f0c 4e 65 78 74 20 3d 3d 20 70 54 72 69 67 67 65 72  Next == pTrigger
26f0d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 63 63   ){.          cc
26f0e 2d 3e 70 4e 65 78 74 20 3d 20 63 63 2d 3e 70 4e  ->pNext = cc->pN
26f0f 65 78 74 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20  ext->pNext;.    
26f10 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
26f11 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 63       }.        c
26f12 63 20 3d 20 63 63 2d 3e 70 4e 65 78 74 3b 0a 20  c = cc->pNext;. 
26f13 20 20 20 20 20 7d 0a 20 20 20 20 20 20 61 73 73       }.      ass
26f14 65 72 74 28 63 63 29 3b 0a 20 20 20 20 7d 0a 20  ert(cc);.    }. 
26f15 20 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65     sqlite3Delete
26f16 54 72 69 67 67 65 72 28 64 62 2c 20 70 54 72 69  Trigger(db, pTri
26f17 67 67 65 72 29 3b 0a 20 20 20 20 64 62 2d 3e 66  gger);.    db->f
26f18 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 49  lags |= SQLITE_I
26f19 6e 74 65 72 6e 43 68 61 6e 67 65 73 3b 0a 20 20  nternChanges;.  
26f1a 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 70 45 4c 69 73  }.}../*.** pELis
26f1b 74 20 69 73 20 74 68 65 20 53 45 54 20 63 6c 61  t is the SET cla
26f1c 75 73 65 20 6f 66 20 61 6e 20 55 50 44 41 54 45  use of an UPDATE
26f1d 20 73 74 61 74 65 6d 65 6e 74 2e 20 20 45 61 63   statement.  Eac
26f1e 68 20 65 6e 74 72 79 0a 2a 2a 20 69 6e 20 70 45  h entry.** in pE
26f1f 4c 69 73 74 20 69 73 20 6f 66 20 74 68 65 20 66  List is of the f
26f20 6f 72 6d 61 74 20 3c 69 64 3e 3d 3c 65 78 70 72  ormat <id>=<expr
26f21 3e 2e 20 20 49 66 20 61 6e 79 20 6f 66 20 74 68  >.  If any of th
26f22 65 20 65 6e 74 72 69 65 73 0a 2a 2a 20 69 6e 20  e entries.** in 
26f23 70 45 4c 69 73 74 20 68 61 76 65 20 61 6e 20 3c  pEList have an <
26f24 69 64 3e 20 77 68 69 63 68 20 6d 61 74 63 68 65  id> which matche
26f25 73 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72 20  s an identifier 
26f26 69 6e 20 70 49 64 4c 69 73 74 2c 0a 2a 2a 20 74  in pIdList,.** t
26f27 68 65 6e 20 72 65 74 75 72 6e 20 54 52 55 45 2e  hen return TRUE.
26f28 20 20 49 66 20 70 49 64 4c 69 73 74 3d 3d 4e 55    If pIdList==NU
26f29 4c 4c 2c 20 74 68 65 6e 20 69 74 20 69 73 20 63  LL, then it is c
26f2a 6f 6e 73 69 64 65 72 65 64 20 61 0a 2a 2a 20 77  onsidered a.** w
26f2b 69 6c 64 63 61 72 64 20 74 68 61 74 20 6d 61 74  ildcard that mat
26f2c 63 68 65 73 20 61 6e 79 74 68 69 6e 67 2e 20 20  ches anything.  
26f2d 4c 69 6b 65 77 69 73 65 20 69 66 20 70 45 4c 69  Likewise if pELi
26f2e 73 74 3d 3d 4e 55 4c 4c 20 74 68 65 6e 0a 2a 2a  st==NULL then.**
26f2f 20 69 74 20 6d 61 74 63 68 65 73 20 61 6e 79 74   it matches anyt
26f30 68 69 6e 67 20 73 6f 20 61 6c 77 61 79 73 20 72  hing so always r
26f31 65 74 75 72 6e 20 74 72 75 65 2e 20 20 52 65 74  eturn true.  Ret
26f32 75 72 6e 20 66 61 6c 73 65 20 6f 6e 6c 79 0a 2a  urn false only.*
26f33 2a 20 69 66 20 74 68 65 72 65 20 69 73 20 6e 6f  * if there is no
26f34 20 6d 61 74 63 68 2e 0a 2a 2f 0a 73 74 61 74 69   match..*/.stati
26f35 63 20 69 6e 74 20 63 68 65 63 6b 43 6f 6c 75 6d  c int checkColum
26f36 6e 4f 76 65 72 4c 61 70 28 49 64 4c 69 73 74 20  nOverLap(IdList 
26f37 2a 70 49 64 4c 69 73 74 2c 20 45 78 70 72 4c 69  *pIdList, ExprLi
26f38 73 74 20 2a 70 45 4c 69 73 74 29 7b 0a 20 20 69  st *pEList){.  i
26f39 6e 74 20 65 3b 0a 20 20 69 66 28 20 21 70 49 64  nt e;.  if( !pId
26f3a 4c 69 73 74 20 7c 7c 20 21 70 45 4c 69 73 74 20  List || !pEList 
26f3b 29 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 66 6f  ) return 1;.  fo
26f3c 72 28 65 3d 30 3b 20 65 3c 70 45 4c 69 73 74 2d  r(e=0; e<pEList-
26f3d 3e 6e 45 78 70 72 3b 20 65 2b 2b 29 7b 0a 20 20  >nExpr; e++){.  
26f3e 20 20 69 66 28 20 73 71 6c 69 74 65 33 49 64 4c    if( sqlite3IdL
26f3f 69 73 74 49 6e 64 65 78 28 70 49 64 4c 69 73 74  istIndex(pIdList
26f40 2c 20 70 45 4c 69 73 74 2d 3e 61 5b 65 5d 2e 7a  , pEList->a[e].z
26f41 4e 61 6d 65 29 3e 3d 30 20 29 20 72 65 74 75 72  Name)>=0 ) retur
26f42 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  n 1;.  }.  retur
26f43 6e 20 30 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  n 0; .}../*.** R
26f44 65 74 75 72 6e 20 61 20 62 69 74 20 76 65 63 74  eturn a bit vect
26f45 6f 72 20 74 6f 20 69 6e 64 69 63 61 74 65 20 77  or to indicate w
26f46 68 61 74 20 6b 69 6e 64 20 6f 66 20 74 72 69 67  hat kind of trig
26f47 67 65 72 73 20 65 78 69 73 74 20 66 6f 72 20 6f  gers exist for o
26f48 70 65 72 61 74 69 6f 6e 0a 2a 2a 20 22 6f 70 22  peration.** "op"
26f49 20 6f 6e 20 74 61 62 6c 65 20 70 54 61 62 2e 20   on table pTab. 
26f4a 20 49 66 20 70 43 68 61 6e 67 65 73 20 69 73 20   If pChanges is 
26f4b 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69 74  not NULL then it
26f4c 20 69 73 20 61 20 6c 69 73 74 20 6f 66 20 63 6f   is a list of co
26f4d 6c 75 6d 6e 73 0a 2a 2a 20 74 68 61 74 20 61 72  lumns.** that ar
26f4e 65 20 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e  e being updated.
26f4f 20 20 54 72 69 67 67 65 72 73 20 6f 6e 6c 79 20    Triggers only 
26f50 6d 61 74 63 68 20 69 66 20 74 68 65 20 4f 4e 20  match if the ON 
26f51 63 6c 61 75 73 65 20 6f 66 20 74 68 65 0a 2a 2a  clause of the.**
26f52 20 74 72 69 67 67 65 72 20 64 65 66 69 6e 69 74   trigger definit
26f53 69 6f 6e 20 6f 76 65 72 6c 61 70 73 20 74 68 65  ion overlaps the
26f54 20 73 65 74 20 6f 66 20 63 6f 6c 75 6d 6e 73 20   set of columns 
26f55 62 65 69 6e 67 20 75 70 64 61 74 65 64 2e 0a 2a  being updated..*
26f56 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75 72 6e 65  *.** The returne
26f57 64 20 62 69 74 20 76 65 63 74 6f 72 20 69 73 20  d bit vector is 
26f58 73 6f 6d 65 20 63 6f 6d 62 69 6e 61 74 69 6f 6e  some combination
26f59 20 6f 66 20 54 52 49 47 47 45 52 5f 42 45 46 4f   of TRIGGER_BEFO
26f5a 52 45 20 61 6e 64 0a 2a 2a 20 54 52 49 47 47 45  RE and.** TRIGGE
26f5b 52 5f 41 46 54 45 52 2e 0a 2a 2f 0a 53 51 4c 49  R_AFTER..*/.SQLI
26f5c 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73  TE_PRIVATE int s
26f5d 71 6c 69 74 65 33 54 72 69 67 67 65 72 73 45 78  qlite3TriggersEx
26f5e 69 73 74 28 0a 20 20 54 61 62 6c 65 20 2a 70 54  ist(.  Table *pT
26f5f 61 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f  ab,            /
26f60 2a 20 54 68 65 20 74 61 62 6c 65 20 74 68 65 20  * The table the 
26f61 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 74 72 69  contains the tri
26f62 67 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 6f  ggers */.  int o
26f63 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
26f64 20 20 20 2f 2a 20 6f 6e 65 20 6f 66 20 54 4b 5f     /* one of TK_
26f65 44 45 4c 45 54 45 2c 20 54 4b 5f 49 4e 53 45 52  DELETE, TK_INSER
26f66 54 2c 20 54 4b 5f 55 50 44 41 54 45 20 2a 2f 0a  T, TK_UPDATE */.
26f67 20 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61    ExprList *pCha
26f68 6e 67 65 73 20 20 20 20 20 20 2f 2a 20 43 6f 6c  nges      /* Col
26f69 75 6d 6e 73 20 74 68 61 74 20 63 68 61 6e 67 65  umns that change
26f6a 20 69 6e 20 61 6e 20 55 50 44 41 54 45 20 73 74   in an UPDATE st
26f6b 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20  atement */.){.  
26f6c 54 72 69 67 67 65 72 20 2a 70 54 72 69 67 67 65  Trigger *pTrigge
26f6d 72 3b 0a 20 20 69 6e 74 20 6d 61 73 6b 20 3d 20  r;.  int mask = 
26f6e 30 3b 0a 0a 20 20 70 54 72 69 67 67 65 72 20 3d  0;..  pTrigger =
26f6f 20 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29   IsVirtual(pTab)
26f70 20 3f 20 30 20 3a 20 70 54 61 62 2d 3e 70 54 72   ? 0 : pTab->pTr
26f71 69 67 67 65 72 3b 0a 20 20 77 68 69 6c 65 28 20  igger;.  while( 
26f72 70 54 72 69 67 67 65 72 20 29 7b 0a 20 20 20 20  pTrigger ){.    
26f73 69 66 28 20 70 54 72 69 67 67 65 72 2d 3e 6f 70  if( pTrigger->op
26f74 3d 3d 6f 70 20 26 26 20 63 68 65 63 6b 43 6f 6c  ==op && checkCol
26f75 75 6d 6e 4f 76 65 72 4c 61 70 28 70 54 72 69 67  umnOverLap(pTrig
26f76 67 65 72 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 20 70  ger->pColumns, p
26f77 43 68 61 6e 67 65 73 29 20 29 7b 0a 20 20 20 20  Changes) ){.    
26f78 20 20 6d 61 73 6b 20 7c 3d 20 70 54 72 69 67 67    mask |= pTrigg
26f79 65 72 2d 3e 74 72 5f 74 6d 3b 0a 20 20 20 20 7d  er->tr_tm;.    }
26f7a 0a 20 20 20 20 70 54 72 69 67 67 65 72 20 3d 20  .    pTrigger = 
26f7b 70 54 72 69 67 67 65 72 2d 3e 70 4e 65 78 74 3b  pTrigger->pNext;
26f7c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6d 61  .  }.  return ma
26f7d 73 6b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e  sk;.}../*.** Con
26f7e 76 65 72 74 20 74 68 65 20 70 53 74 65 70 2d 3e  vert the pStep->
26f7f 74 61 72 67 65 74 20 74 6f 6b 65 6e 20 69 6e 74  target token int
26f80 6f 20 61 20 53 72 63 4c 69 73 74 20 61 6e 64 20  o a SrcList and 
26f81 72 65 74 75 72 6e 20 61 20 70 6f 69 6e 74 65 72  return a pointer
26f82 0a 2a 2a 20 74 6f 20 74 68 61 74 20 53 72 63 4c  .** to that SrcL
26f83 69 73 74 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  ist..**.** This 
26f84 72 6f 75 74 69 6e 65 20 61 64 64 73 20 61 20 73  routine adds a s
26f85 70 65 63 69 66 69 63 20 64 61 74 61 62 61 73 65  pecific database
26f86 20 6e 61 6d 65 2c 20 69 66 20 6e 65 65 64 65 64   name, if needed
26f87 2c 20 74 6f 20 74 68 65 20 74 61 72 67 65 74 20  , to the target 
26f88 77 68 65 6e 0a 2a 2a 20 66 6f 72 6d 69 6e 67 20  when.** forming 
26f89 74 68 65 20 53 72 63 4c 69 73 74 2e 20 20 54 68  the SrcList.  Th
26f8a 69 73 20 70 72 65 76 65 6e 74 73 20 61 20 74 72  is prevents a tr
26f8b 69 67 67 65 72 20 69 6e 20 6f 6e 65 20 64 61 74  igger in one dat
26f8c 61 62 61 73 65 20 66 72 6f 6d 0a 2a 2a 20 72 65  abase from.** re
26f8d 66 65 72 72 69 6e 67 20 74 6f 20 61 20 74 61 72  ferring to a tar
26f8e 67 65 74 20 69 6e 20 61 6e 6f 74 68 65 72 20 64  get in another d
26f8f 61 74 61 62 61 73 65 2e 20 20 41 6e 20 65 78 63  atabase.  An exc
26f90 65 70 74 69 6f 6e 20 69 73 20 77 68 65 6e 20 74  eption is when t
26f91 68 65 0a 2a 2a 20 74 72 69 67 67 65 72 20 69 73  he.** trigger is
26f92 20 69 6e 20 54 45 4d 50 20 69 6e 20 77 68 69 63   in TEMP in whic
26f93 68 20 63 61 73 65 20 69 74 20 63 61 6e 20 72 65  h case it can re
26f94 66 65 72 20 74 6f 20 61 6e 79 20 6f 74 68 65 72  fer to any other
26f95 20 64 61 74 61 62 61 73 65 20 69 74 0a 2a 2a 20   database it.** 
26f96 77 61 6e 74 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  wants..*/.static
26f97 20 53 72 63 4c 69 73 74 20 2a 74 61 72 67 65 74   SrcList *target
26f98 53 72 63 4c 69 73 74 28 0a 20 20 50 61 72 73 65  SrcList(.  Parse
26f99 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26f9a 2f 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63  /* The parsing c
26f9b 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 54 72 69 67  ontext */.  Trig
26f9c 67 65 72 53 74 65 70 20 2a 70 53 74 65 70 20 20  gerStep *pStep  
26f9d 20 2f 2a 20 54 68 65 20 74 72 69 67 67 65 72 20   /* The trigger 
26f9e 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 74  containing the t
26f9f 61 72 67 65 74 20 74 6f 6b 65 6e 20 2a 2f 0a 29  arget token */.)
26fa0 7b 0a 20 20 54 6f 6b 65 6e 20 73 44 62 3b 20 20  {.  Token sDb;  
26fa1 20 20 20 20 20 20 20 20 20 2f 2a 20 44 75 6d 6d           /* Dumm
26fa2 79 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  y database name 
26fa3 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 69  token */.  int i
26fa4 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  Db;             
26fa5 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20  /* Index of the 
26fa6 64 61 74 61 62 61 73 65 20 74 6f 20 75 73 65 20  database to use 
26fa7 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a 70 53  */.  SrcList *pS
26fa8 72 63 3b 20 20 20 20 20 20 20 2f 2a 20 53 72 63  rc;       /* Src
26fa9 4c 69 73 74 20 74 6f 20 62 65 20 72 65 74 75 72  List to be retur
26faa 6e 65 64 20 2a 2f 0a 0a 20 20 69 44 62 20 3d 20  ned */..  iDb = 
26fab 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
26fac 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
26fad 20 70 53 74 65 70 2d 3e 70 54 72 69 67 2d 3e 70   pStep->pTrig->p
26fae 53 63 68 65 6d 61 29 3b 0a 20 20 69 66 28 20 69  Schema);.  if( i
26faf 44 62 3d 3d 30 20 7c 7c 20 69 44 62 3e 3d 32 20  Db==0 || iDb>=2 
26fb0 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 69  ){.    assert( i
26fb1 44 62 3c 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6e  Db<pParse->db->n
26fb2 44 62 20 29 3b 0a 20 20 20 20 73 44 62 2e 7a 20  Db );.    sDb.z 
26fb3 3d 20 28 75 38 2a 29 70 50 61 72 73 65 2d 3e 64  = (u8*)pParse->d
26fb4 62 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d  b->aDb[iDb].zNam
26fb5 65 3b 0a 20 20 20 20 73 44 62 2e 6e 20 3d 20 73  e;.    sDb.n = s
26fb6 74 72 6c 65 6e 28 28 63 68 61 72 2a 29 73 44 62  trlen((char*)sDb
26fb7 2e 7a 29 3b 0a 20 20 20 20 70 53 72 63 20 3d 20  .z);.    pSrc = 
26fb8 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70  sqlite3SrcListAp
26fb9 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c  pend(pParse->db,
26fba 20 30 2c 20 26 73 44 62 2c 20 26 70 53 74 65 70   0, &sDb, &pStep
26fbb 2d 3e 74 61 72 67 65 74 29 3b 0a 20 20 7d 20 65  ->target);.  } e
26fbc 6c 73 65 20 7b 0a 20 20 20 20 70 53 72 63 20 3d  lse {.    pSrc =
26fbd 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 41   sqlite3SrcListA
26fbe 70 70 65 6e 64 28 70 50 61 72 73 65 2d 3e 64 62  ppend(pParse->db
26fbf 2c 20 30 2c 20 26 70 53 74 65 70 2d 3e 74 61 72  , 0, &pStep->tar
26fc0 67 65 74 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72  get, 0);.  }.  r
26fc1 65 74 75 72 6e 20 70 53 72 63 3b 0a 7d 0a 0a 2f  eturn pSrc;.}../
26fc2 2a 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 56 44  *.** Generate VD
26fc3 42 45 20 63 6f 64 65 20 66 6f 72 20 7a 65 72 6f  BE code for zero
26fc4 20 6f 72 20 6d 6f 72 65 20 73 74 61 74 65 6d 65   or more stateme
26fc5 6e 74 73 20 69 6e 73 69 64 65 20 74 68 65 20 62  nts inside the b
26fc6 6f 64 79 20 6f 66 20 61 0a 2a 2a 20 74 72 69 67  ody of a.** trig
26fc7 67 65 72 2e 20 20 0a 2a 2f 0a 73 74 61 74 69 63  ger.  .*/.static
26fc8 20 69 6e 74 20 63 6f 64 65 54 72 69 67 67 65 72   int codeTrigger
26fc9 50 72 6f 67 72 61 6d 28 0a 20 20 50 61 72 73 65  Program(.  Parse
26fca 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
26fcb 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
26fcc 65 72 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20  er context */.  
26fcd 54 72 69 67 67 65 72 53 74 65 70 20 2a 70 53 74  TriggerStep *pSt
26fce 65 70 4c 69 73 74 2c 20 20 20 2f 2a 20 4c 69 73  epList,   /* Lis
26fcf 74 20 6f 66 20 73 74 61 74 65 6d 65 6e 74 73 20  t of statements 
26fd0 69 6e 73 69 64 65 20 74 68 65 20 74 72 69 67 67  inside the trigg
26fd1 65 72 20 62 6f 64 79 20 2a 2f 0a 20 20 69 6e 74  er body */.  int
26fd2 20 6f 72 63 6f 6e 66 69 6e 20 20 20 20 20 20 20   orconfin       
26fd3 20 20 20 20 20 20 20 2f 2a 20 43 6f 6e 66 6c 69         /* Confli
26fd4 63 74 20 61 6c 67 6f 72 69 74 68 6d 2e 20 28 4f  ct algorithm. (O
26fd5 45 5f 41 62 6f 72 74 2c 20 65 74 63 29 20 2a 2f  E_Abort, etc) */
26fd6 20 20 0a 29 7b 0a 20 20 54 72 69 67 67 65 72 53    .){.  TriggerS
26fd7 74 65 70 20 2a 20 70 54 72 69 67 67 65 72 53 74  tep * pTriggerSt
26fd8 65 70 20 3d 20 70 53 74 65 70 4c 69 73 74 3b 0a  ep = pStepList;.
26fd9 20 20 69 6e 74 20 6f 72 63 6f 6e 66 3b 0a 20 20    int orconf;.  
26fda 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73 65  Vdbe *v = pParse
26fdb 2d 3e 70 56 64 62 65 3b 0a 20 20 73 71 6c 69 74  ->pVdbe;.  sqlit
26fdc 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
26fdd 3e 64 62 3b 0a 0a 20 20 61 73 73 65 72 74 28 20  >db;..  assert( 
26fde 70 54 72 69 67 67 65 72 53 74 65 70 21 3d 30 20  pTriggerStep!=0 
26fdf 29 3b 0a 20 20 61 73 73 65 72 74 28 20 76 21 3d  );.  assert( v!=
26fe0 30 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  0 );.  sqlite3Vd
26fe1 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
26fe2 6f 6e 74 65 78 74 50 75 73 68 2c 20 30 2c 20 30  ontextPush, 0, 0
26fe3 29 3b 0a 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74  );.  VdbeComment
26fe4 28 28 76 2c 20 22 62 65 67 69 6e 20 74 72 69 67  ((v, "begin trig
26fe5 67 65 72 20 25 73 22 2c 20 70 53 74 65 70 4c 69  ger %s", pStepLi
26fe6 73 74 2d 3e 70 54 72 69 67 2d 3e 6e 61 6d 65 29  st->pTrig->name)
26fe7 29 3b 0a 20 20 77 68 69 6c 65 28 20 70 54 72 69  );.  while( pTri
26fe8 67 67 65 72 53 74 65 70 20 29 7b 0a 20 20 20 20  ggerStep ){.    
26fe9 6f 72 63 6f 6e 66 20 3d 20 28 6f 72 63 6f 6e 66  orconf = (orconf
26fea 69 6e 20 3d 3d 20 4f 45 5f 44 65 66 61 75 6c 74  in == OE_Default
26feb 29 3f 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e  )?pTriggerStep->
26fec 6f 72 63 6f 6e 66 3a 6f 72 63 6f 6e 66 69 6e 3b  orconf:orconfin;
26fed 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 74 72 69  .    pParse->tri
26fee 67 53 74 61 63 6b 2d 3e 6f 72 63 6f 6e 66 20 3d  gStack->orconf =
26fef 20 6f 72 63 6f 6e 66 3b 0a 20 20 20 20 73 77 69   orconf;.    swi
26ff0 74 63 68 28 20 70 54 72 69 67 67 65 72 53 74 65  tch( pTriggerSte
26ff1 70 2d 3e 6f 70 20 29 7b 0a 20 20 20 20 20 20 63  p->op ){.      c
26ff2 61 73 65 20 54 4b 5f 53 45 4c 45 43 54 3a 20 7b  ase TK_SELECT: {
26ff3 0a 20 20 20 20 20 20 20 20 53 65 6c 65 63 74 20  .        Select 
26ff4 2a 73 73 20 3d 20 73 71 6c 69 74 65 33 53 65 6c  *ss = sqlite3Sel
26ff5 65 63 74 44 75 70 28 64 62 2c 20 70 54 72 69 67  ectDup(db, pTrig
26ff6 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  gerStep->pSelect
26ff7 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 73  );.        if( s
26ff8 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 53  s ){.          S
26ff9 65 6c 65 63 74 44 65 73 74 20 64 65 73 74 3b 0a  electDest dest;.
26ffa 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74  .          sqlit
26ffb 65 33 53 65 6c 65 63 74 44 65 73 74 49 6e 69 74  e3SelectDestInit
26ffc 28 26 64 65 73 74 2c 20 53 52 54 5f 44 69 73 63  (&dest, SRT_Disc
26ffd 61 72 64 2c 20 30 29 3b 0a 20 20 20 20 20 20 20  ard, 0);.       
26ffe 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63 74     sqlite3Select
26fff 28 70 50 61 72 73 65 2c 20 73 73 2c 20 26 64 65  (pParse, ss, &de
27000 73 74 29 3b 0a 20 20 20 20 20 20 20 20 20 20 73  st);.          s
27001 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
27002 74 65 28 64 62 2c 20 73 73 29 3b 0a 20 20 20 20  te(db, ss);.    
27003 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72      }.        br
27004 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
27005 20 20 20 63 61 73 65 20 54 4b 5f 55 50 44 41 54     case TK_UPDAT
27006 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72 63  E: {.        Src
27007 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 20 20  List *pSrc;.    
27008 20 20 20 20 70 53 72 63 20 3d 20 74 61 72 67 65      pSrc = targe
27009 74 53 72 63 4c 69 73 74 28 70 50 61 72 73 65 2c  tSrcList(pParse,
2700a 20 70 54 72 69 67 67 65 72 53 74 65 70 29 3b 0a   pTriggerStep);.
2700b 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
2700c 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
2700d 52 65 73 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30  ResetCount, 0, 0
2700e 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  );.        sqlit
2700f 65 33 55 70 64 61 74 65 28 70 50 61 72 73 65 2c  e3Update(pParse,
27010 20 70 53 72 63 2c 0a 20 20 20 20 20 20 20 20 20   pSrc,.         
27011 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
27012 70 72 4c 69 73 74 44 75 70 28 64 62 2c 20 70 54  prListDup(db, pT
27013 72 69 67 67 65 72 53 74 65 70 2d 3e 70 45 78 70  riggerStep->pExp
27014 72 4c 69 73 74 29 2c 20 0a 20 20 20 20 20 20 20  rList), .       
27015 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
27016 45 78 70 72 44 75 70 28 64 62 2c 20 70 54 72 69  ExprDup(db, pTri
27017 67 67 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65  ggerStep->pWhere
27018 29 2c 20 6f 72 63 6f 6e 66 29 3b 0a 20 20 20 20  ), orconf);.    
27019 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2701a 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73 65  ddOp2(v, OP_Rese
2701b 74 43 6f 75 6e 74 2c 20 31 2c 20 30 29 3b 0a 20  tCount, 1, 0);. 
2701c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2701d 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73 65      }.      case
2701e 20 54 4b 5f 49 4e 53 45 52 54 3a 20 7b 0a 20 20   TK_INSERT: {.  
2701f 20 20 20 20 20 20 53 72 63 4c 69 73 74 20 2a 70        SrcList *p
27020 53 72 63 3b 0a 20 20 20 20 20 20 20 20 70 53 72  Src;.        pSr
27021 63 20 3d 20 74 61 72 67 65 74 53 72 63 4c 69 73  c = targetSrcLis
27022 74 28 70 50 61 72 73 65 2c 20 70 54 72 69 67 67  t(pParse, pTrigg
27023 65 72 53 74 65 70 29 3b 0a 20 20 20 20 20 20 20  erStep);.       
27024 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
27025 70 32 28 76 2c 20 4f 50 5f 52 65 73 65 74 43 6f  p2(v, OP_ResetCo
27026 75 6e 74 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  unt, 0, 0);.    
27027 20 20 20 20 73 71 6c 69 74 65 33 49 6e 73 65 72      sqlite3Inser
27028 74 28 70 50 61 72 73 65 2c 20 70 53 72 63 2c 0a  t(pParse, pSrc,.
27029 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2702a 33 45 78 70 72 4c 69 73 74 44 75 70 28 64 62 2c  3ExprListDup(db,
2702b 20 70 54 72 69 67 67 65 72 53 74 65 70 2d 3e 70   pTriggerStep->p
2702c 45 78 70 72 4c 69 73 74 29 2c 20 0a 20 20 20 20  ExprList), .    
2702d 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c        sqlite3Sel
2702e 65 63 74 44 75 70 28 64 62 2c 20 70 54 72 69 67  ectDup(db, pTrig
2702f 67 65 72 53 74 65 70 2d 3e 70 53 65 6c 65 63 74  gerStep->pSelect
27030 29 2c 20 0a 20 20 20 20 20 20 20 20 20 20 73 71  ), .          sq
27031 6c 69 74 65 33 49 64 4c 69 73 74 44 75 70 28 64  lite3IdListDup(d
27032 62 2c 20 70 54 72 69 67 67 65 72 53 74 65 70 2d  b, pTriggerStep-
27033 3e 70 49 64 4c 69 73 74 29 2c 20 6f 72 63 6f 6e  >pIdList), orcon
27034 66 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  f);.        sqli
27035 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27036 20 4f 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20   OP_ResetCount, 
27037 31 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62  1, 0);.        b
27038 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
27039 20 20 20 20 63 61 73 65 20 54 4b 5f 44 45 4c 45      case TK_DELE
2703a 54 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 53 72  TE: {.        Sr
2703b 63 4c 69 73 74 20 2a 70 53 72 63 3b 0a 20 20 20  cList *pSrc;.   
2703c 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
2703d 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
2703e 65 74 43 6f 75 6e 74 2c 20 30 2c 20 30 29 3b 0a  etCount, 0, 0);.
2703f 20 20 20 20 20 20 20 20 70 53 72 63 20 3d 20 74          pSrc = t
27040 61 72 67 65 74 53 72 63 4c 69 73 74 28 70 50 61  argetSrcList(pPa
27041 72 73 65 2c 20 70 54 72 69 67 67 65 72 53 74 65  rse, pTriggerSte
27042 70 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69  p);.        sqli
27043 74 65 33 44 65 6c 65 74 65 46 72 6f 6d 28 70 50  te3DeleteFrom(pP
27044 61 72 73 65 2c 20 70 53 72 63 2c 20 0a 20 20 20  arse, pSrc, .   
27045 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27046 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
27047 70 72 44 75 70 28 64 62 2c 20 70 54 72 69 67 67  prDup(db, pTrigg
27048 65 72 53 74 65 70 2d 3e 70 57 68 65 72 65 29 29  erStep->pWhere))
27049 3b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  ;.        sqlite
2704a 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2704b 50 5f 52 65 73 65 74 43 6f 75 6e 74 2c 20 31 2c  P_ResetCount, 1,
2704c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65   0);.        bre
2704d 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2704e 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
2704f 20 20 20 61 73 73 65 72 74 28 30 29 3b 0a 20 20     assert(0);.  
27050 20 20 7d 20 0a 20 20 20 20 70 54 72 69 67 67 65    } .    pTrigge
27051 72 53 74 65 70 20 3d 20 70 54 72 69 67 67 65 72  rStep = pTrigger
27052 53 74 65 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 7d  Step->pNext;.  }
27053 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27054 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6f 6e 74 65  dOp2(v, OP_Conte
27055 78 74 50 6f 70 2c 20 30 2c 20 30 29 3b 0a 20 20  xtPop, 0, 0);.  
27056 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20  VdbeComment((v, 
27057 22 65 6e 64 20 74 72 69 67 67 65 72 20 25 73 22  "end trigger %s"
27058 2c 20 70 53 74 65 70 4c 69 73 74 2d 3e 70 54 72  , pStepList->pTr
27059 69 67 2d 3e 6e 61 6d 65 29 29 3b 0a 0a 20 20 72  ig->name));..  r
2705a 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a  eturn 0;.}../*.*
2705b 2a 20 54 68 69 73 20 69 73 20 63 61 6c 6c 65 64  * This is called
2705c 20 74 6f 20 63 6f 64 65 20 46 4f 52 20 45 41 43   to code FOR EAC
2705d 48 20 52 4f 57 20 74 72 69 67 67 65 72 73 2e 0a  H ROW triggers..
2705e 2a 2a 0a 2a 2a 20 57 68 65 6e 20 74 68 65 20 63  **.** When the c
2705f 6f 64 65 20 74 68 61 74 20 74 68 69 73 20 66 75  ode that this fu
27060 6e 63 74 69 6f 6e 20 67 65 6e 65 72 61 74 65 73  nction generates
27061 20 69 73 20 65 78 65 63 75 74 65 64 2c 20 74 68   is executed, th
27062 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20  e following .** 
27063 6d 75 73 74 20 62 65 20 74 72 75 65 3a 0a 2a 2a  must be true:.**
27064 0a 2a 2a 20 31 2e 20 4e 6f 20 63 75 72 73 6f 72  .** 1. No cursor
27065 73 20 6d 61 79 20 62 65 20 6f 70 65 6e 20 69 6e  s may be open in
27066 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61 62 61   the main databa
27067 73 65 2e 20 20 28 42 75 74 20 6e 65 77 49 64 78  se.  (But newIdx
27068 20 61 6e 64 20 6f 6c 64 49 64 78 0a 2a 2a 20 20   and oldIdx.**  
27069 20 20 63 61 6e 20 62 65 20 69 6e 64 69 63 65 73    can be indices
2706a 20 6f 66 20 63 75 72 73 6f 72 73 20 69 6e 20 74   of cursors in t
2706b 65 6d 70 6f 72 61 72 79 20 74 61 62 6c 65 73 2e  emporary tables.
2706c 20 20 53 65 65 20 62 65 6c 6f 77 2e 29 0a 2a 2a    See below.).**
2706d 0a 2a 2a 20 32 2e 20 49 66 20 74 68 65 20 74 72  .** 2. If the tr
2706e 69 67 67 65 72 73 20 62 65 69 6e 67 20 63 6f 64  iggers being cod
2706f 65 64 20 61 72 65 20 4f 4e 20 49 4e 53 45 52 54  ed are ON INSERT
27070 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20 74 72   or ON UPDATE tr
27071 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a 20  iggers, then.** 
27072 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79 20 76     a temporary v
27073 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e 64 65  dbe cursor (inde
27074 78 20 6e 65 77 49 64 78 29 20 6d 75 73 74 20 62  x newIdx) must b
27075 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e 74  e open and point
27076 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61 20 72  ing at.**    a r
27077 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61  ow containing va
27078 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62 73 74  lues to be subst
27079 69 74 75 74 65 64 20 66 6f 72 20 6e 65 77 2e 2a  ituted for new.*
2707a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
2707b 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67 67 65  the.**    trigge
2707c 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a 2a  r program(s)..**
2707d 0a 2a 2a 20 33 2e 20 49 66 20 74 68 65 20 74 72  .** 3. If the tr
2707e 69 67 67 65 72 73 20 62 65 69 6e 67 20 63 6f 64  iggers being cod
2707f 65 64 20 61 72 65 20 4f 4e 20 44 45 4c 45 54 45  ed are ON DELETE
27080 20 6f 72 20 4f 4e 20 55 50 44 41 54 45 20 74 72   or ON UPDATE tr
27081 69 67 67 65 72 73 2c 20 74 68 65 6e 0a 2a 2a 20  iggers, then.** 
27082 20 20 20 61 20 74 65 6d 70 6f 72 61 72 79 20 76     a temporary v
27083 64 62 65 20 63 75 72 73 6f 72 20 28 69 6e 64 65  dbe cursor (inde
27084 78 20 6f 6c 64 49 64 78 29 20 6d 75 73 74 20 62  x oldIdx) must b
27085 65 20 6f 70 65 6e 20 61 6e 64 20 70 6f 69 6e 74  e open and point
27086 69 6e 67 20 61 74 0a 2a 2a 20 20 20 20 61 20 72  ing at.**    a r
27087 6f 77 20 63 6f 6e 74 61 69 6e 69 6e 67 20 76 61  ow containing va
27088 6c 75 65 73 20 74 6f 20 62 65 20 73 75 62 73 74  lues to be subst
27089 69 74 75 74 65 64 20 66 6f 72 20 6f 6c 64 2e 2a  ituted for old.*
2708a 20 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20   expressions in 
2708b 74 68 65 0a 2a 2a 20 20 20 20 74 72 69 67 67 65  the.**    trigge
2708c 72 20 70 72 6f 67 72 61 6d 28 73 29 2e 0a 2a 2a  r program(s)..**
2708d 0a 2a 2a 20 49 66 20 74 68 65 79 20 61 72 65 20  .** If they are 
2708e 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65 20 70 69  not NULL, the pi
2708f 4f 6c 64 43 6f 6c 4d 61 73 6b 20 61 6e 64 20 70  OldColMask and p
27090 69 4e 65 77 43 6f 6c 4d 61 73 6b 20 6f 75 74 70  iNewColMask outp
27091 75 74 20 76 61 72 69 61 62 6c 65 73 0a 2a 2a 20  ut variables.** 
27092 61 72 65 20 73 65 74 20 74 6f 20 76 61 6c 75 65  are set to value
27093 73 20 74 68 61 74 20 64 65 73 63 72 69 62 65 20  s that describe 
27094 74 68 65 20 63 6f 6c 75 6d 6e 73 20 75 73 65 64  the columns used
27095 20 62 79 20 74 68 65 20 74 72 69 67 67 65 72 20   by the trigger 
27096 70 72 6f 67 72 61 6d 0a 2a 2a 20 69 6e 20 74 68  program.** in th
27097 65 20 4f 4c 44 2e 2a 20 61 6e 64 20 4e 45 57 2e  e OLD.* and NEW.
27098 2a 20 74 61 62 6c 65 73 20 72 65 73 70 65 63 74  * tables respect
27099 69 76 65 6c 79 2e 20 49 66 20 63 6f 6c 75 6d 6e  ively. If column
2709a 20 4e 20 6f 66 20 74 68 65 20 0a 2a 2a 20 70 73   N of the .** ps
2709b 65 75 64 6f 2d 74 61 62 6c 65 20 69 73 20 72 65  eudo-table is re
2709c 61 64 20 61 74 20 6c 65 61 73 74 20 6f 6e 63 65  ad at least once
2709d 2c 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64  , the correspond
2709e 69 6e 67 20 62 69 74 20 6f 66 20 74 68 65 20 6f  ing bit of the o
2709f 75 74 70 75 74 0a 2a 2a 20 6d 61 73 6b 20 69 73  utput.** mask is
270a0 20 73 65 74 2e 20 49 66 20 61 20 63 6f 6c 75 6d   set. If a colum
270a1 6e 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 20  n with an index 
270a2 67 72 65 61 74 65 72 20 74 68 61 6e 20 33 32 20  greater than 32 
270a3 69 73 20 72 65 61 64 2c 20 74 68 65 0a 2a 2a 20  is read, the.** 
270a4 6f 75 74 70 75 74 20 6d 61 73 6b 20 69 73 20 73  output mask is s
270a5 65 74 20 74 6f 20 74 68 65 20 73 70 65 63 69 61  et to the specia
270a6 6c 20 76 61 6c 75 65 20 30 78 66 66 66 66 66 66  l value 0xffffff
270a7 66 66 2e 0a 2a 2a 0a 2a 2f 0a 53 51 4c 49 54 45  ff..**.*/.SQLITE
270a8 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
270a9 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67 67  ite3CodeRowTrigg
270aa 65 72 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  er(.  Parse *pPa
270ab 72 73 65 2c 20 20 20 20 20 20 20 2f 2a 20 50 61  rse,       /* Pa
270ac 72 73 65 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  rse context */. 
270ad 20 69 6e 74 20 6f 70 2c 20 20 20 20 20 20 20 20   int op,        
270ae 20 20 20 20 20 20 2f 2a 20 4f 6e 65 20 6f 66 20        /* One of 
270af 54 4b 5f 55 50 44 41 54 45 2c 20 54 4b 5f 49 4e  TK_UPDATE, TK_IN
270b0 53 45 52 54 2c 20 54 4b 5f 44 45 4c 45 54 45 20  SERT, TK_DELETE 
270b1 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
270b2 43 68 61 6e 67 65 73 2c 20 20 2f 2a 20 43 68 61  Changes,  /* Cha
270b3 6e 67 65 73 20 6c 69 73 74 20 66 6f 72 20 61 6e  nges list for an
270b4 79 20 55 50 44 41 54 45 20 4f 46 20 74 72 69 67  y UPDATE OF trig
270b5 67 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 74 72  gers */.  int tr
270b6 5f 74 6d 2c 20 20 20 20 20 20 20 20 20 20 20 2f  _tm,           /
270b7 2a 20 4f 6e 65 20 6f 66 20 54 52 49 47 47 45 52  * One of TRIGGER
270b8 5f 42 45 46 4f 52 45 2c 20 54 52 49 47 47 45 52  _BEFORE, TRIGGER
270b9 5f 41 46 54 45 52 20 2a 2f 0a 20 20 54 61 62 6c  _AFTER */.  Tabl
270ba 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20 20  e *pTab,        
270bb 20 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 74 6f   /* The table to
270bc 20 63 6f 64 65 20 74 72 69 67 67 65 72 73 20 66   code triggers f
270bd 72 6f 6d 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 77  rom */.  int new
270be 49 64 78 2c 20 20 20 20 20 20 20 20 20 20 2f 2a  Idx,          /*
270bf 20 54 68 65 20 69 6e 64 69 63 65 20 6f 66 20 74   The indice of t
270c0 68 65 20 22 6e 65 77 22 20 72 6f 77 20 74 6f 20  he "new" row to 
270c1 61 63 63 65 73 73 20 2a 2f 0a 20 20 69 6e 74 20  access */.  int 
270c2 6f 6c 64 49 64 78 2c 20 20 20 20 20 20 20 20 20  oldIdx,         
270c3 20 2f 2a 20 54 68 65 20 69 6e 64 69 63 65 20 6f   /* The indice o
270c4 66 20 74 68 65 20 22 6f 6c 64 22 20 72 6f 77 20  f the "old" row 
270c5 74 6f 20 61 63 63 65 73 73 20 2a 2f 0a 20 20 69  to access */.  i
270c6 6e 74 20 6f 72 63 6f 6e 66 2c 20 20 20 20 20 20  nt orconf,      
270c7 20 20 20 20 2f 2a 20 4f 4e 20 43 4f 4e 46 4c 49      /* ON CONFLI
270c8 43 54 20 70 6f 6c 69 63 79 20 2a 2f 0a 20 20 69  CT policy */.  i
270c9 6e 74 20 69 67 6e 6f 72 65 4a 75 6d 70 2c 20 20  nt ignoreJump,  
270ca 20 20 20 20 2f 2a 20 49 6e 73 74 72 75 63 74 69      /* Instructi
270cb 6f 6e 20 74 6f 20 6a 75 6d 70 20 74 6f 20 66 6f  on to jump to fo
270cc 72 20 52 41 49 53 45 28 49 47 4e 4f 52 45 29 20  r RAISE(IGNORE) 
270cd 2a 2f 0a 20 20 75 33 32 20 2a 70 69 4f 6c 64 43  */.  u32 *piOldC
270ce 6f 6c 4d 61 73 6b 2c 20 20 20 2f 2a 20 4f 55 54  olMask,   /* OUT
270cf 3a 20 4d 61 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e  : Mask of column
270d0 73 20 75 73 65 64 20 66 72 6f 6d 20 74 68 65 20  s used from the 
270d1 4f 4c 44 2e 2a 20 74 61 62 6c 65 20 2a 2f 0a 20  OLD.* table */. 
270d2 20 75 33 32 20 2a 70 69 4e 65 77 43 6f 6c 4d 61   u32 *piNewColMa
270d3 73 6b 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4d 61  sk    /* OUT: Ma
270d4 73 6b 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 75 73  sk of columns us
270d5 65 64 20 66 72 6f 6d 20 74 68 65 20 4e 45 57 2e  ed from the NEW.
270d6 2a 20 74 61 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20  * table */.){.  
270d7 54 72 69 67 67 65 72 20 2a 70 3b 0a 20 20 73 71  Trigger *p;.  sq
270d8 6c 69 74 65 33 20 2a 64 62 20 3d 20 70 50 61 72  lite3 *db = pPar
270d9 73 65 2d 3e 64 62 3b 0a 20 20 54 72 69 67 67 65  se->db;.  Trigge
270da 72 53 74 61 63 6b 20 74 72 69 67 53 74 61 63 6b  rStack trigStack
270db 45 6e 74 72 79 3b 0a 0a 20 20 74 72 69 67 53 74  Entry;..  trigSt
270dc 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 43 6f 6c 4d  ackEntry.oldColM
270dd 61 73 6b 20 3d 20 30 3b 0a 20 20 74 72 69 67 53  ask = 0;.  trigS
270de 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77 43 6f 6c  tackEntry.newCol
270df 4d 61 73 6b 20 3d 20 30 3b 0a 0a 20 20 61 73 73  Mask = 0;..  ass
270e0 65 72 74 28 6f 70 20 3d 3d 20 54 4b 5f 55 50 44  ert(op == TK_UPD
270e1 41 54 45 20 7c 7c 20 6f 70 20 3d 3d 20 54 4b 5f  ATE || op == TK_
270e2 49 4e 53 45 52 54 20 7c 7c 20 6f 70 20 3d 3d 20  INSERT || op == 
270e3 54 4b 5f 44 45 4c 45 54 45 29 3b 0a 20 20 61 73  TK_DELETE);.  as
270e4 73 65 72 74 28 74 72 5f 74 6d 20 3d 3d 20 54 52  sert(tr_tm == TR
270e5 49 47 47 45 52 5f 42 45 46 4f 52 45 20 7c 7c 20  IGGER_BEFORE || 
270e6 74 72 5f 74 6d 20 3d 3d 20 54 52 49 47 47 45 52  tr_tm == TRIGGER
270e7 5f 41 46 54 45 52 20 29 3b 0a 0a 20 20 61 73 73  _AFTER );..  ass
270e8 65 72 74 28 6e 65 77 49 64 78 20 21 3d 20 2d 31  ert(newIdx != -1
270e9 20 7c 7c 20 6f 6c 64 49 64 78 20 21 3d 20 2d 31   || oldIdx != -1
270ea 29 3b 0a 0a 20 20 66 6f 72 28 70 3d 70 54 61 62  );..  for(p=pTab
270eb 2d 3e 70 54 72 69 67 67 65 72 3b 20 70 3b 20 70  ->pTrigger; p; p
270ec 3d 70 2d 3e 70 4e 65 78 74 29 7b 0a 20 20 20 20  =p->pNext){.    
270ed 69 6e 74 20 66 69 72 65 5f 74 68 69 73 20 3d 20  int fire_this = 
270ee 30 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 74 65 72  0;..    /* Deter
270ef 6d 69 6e 65 20 77 68 65 74 68 65 72 20 77 65 20  mine whether we 
270f0 73 68 6f 75 6c 64 20 63 6f 64 65 20 74 68 69 73  should code this
270f1 20 74 72 69 67 67 65 72 20 2a 2f 0a 20 20 20 20   trigger */.    
270f2 69 66 28 20 0a 20 20 20 20 20 20 70 2d 3e 6f 70  if( .      p->op
270f3 3d 3d 6f 70 20 26 26 20 0a 20 20 20 20 20 20 70  ==op && .      p
270f4 2d 3e 74 72 5f 74 6d 3d 3d 74 72 5f 74 6d 20 26  ->tr_tm==tr_tm &
270f5 26 20 0a 20 20 20 20 20 20 28 70 2d 3e 70 53 63  & .      (p->pSc
270f6 68 65 6d 61 3d 3d 70 2d 3e 70 54 61 62 53 63 68  hema==p->pTabSch
270f7 65 6d 61 20 7c 7c 20 70 2d 3e 70 53 63 68 65 6d  ema || p->pSchem
270f8 61 3d 3d 64 62 2d 3e 61 44 62 5b 31 5d 2e 70 53  a==db->aDb[1].pS
270f9 63 68 65 6d 61 29 20 26 26 0a 20 20 20 20 20 20  chema) &&.      
270fa 28 6f 70 21 3d 54 4b 5f 55 50 44 41 54 45 7c 7c  (op!=TK_UPDATE||
270fb 21 70 2d 3e 70 43 6f 6c 75 6d 6e 73 7c 7c 63 68  !p->pColumns||ch
270fc 65 63 6b 43 6f 6c 75 6d 6e 4f 76 65 72 4c 61 70  eckColumnOverLap
270fd 28 70 2d 3e 70 43 6f 6c 75 6d 6e 73 2c 70 43 68  (p->pColumns,pCh
270fe 61 6e 67 65 73 29 29 0a 20 20 20 20 29 7b 0a 20  anges)).    ){. 
270ff 20 20 20 20 20 54 72 69 67 67 65 72 53 74 61 63       TriggerStac
27100 6b 20 2a 70 53 3b 20 20 20 20 20 20 2f 2a 20 50  k *pS;      /* P
27101 6f 69 6e 74 65 72 20 74 6f 20 74 72 69 67 67 65  ointer to trigge
27102 72 2d 73 74 61 63 6b 20 65 6e 74 72 79 20 2a 2f  r-stack entry */
27103 0a 20 20 20 20 20 20 66 6f 72 28 70 53 3d 70 50  .      for(pS=pP
27104 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b 3b  arse->trigStack;
27105 20 70 53 20 26 26 20 70 21 3d 70 53 2d 3e 70 54   pS && p!=pS->pT
27106 72 69 67 67 65 72 3b 20 70 53 3d 70 53 2d 3e 70  rigger; pS=pS->p
27107 4e 65 78 74 29 7b 7d 0a 20 20 20 20 20 20 69 66  Next){}.      if
27108 28 20 21 70 53 20 29 7b 0a 20 20 20 20 20 20 20  ( !pS ){.       
27109 20 66 69 72 65 5f 74 68 69 73 20 3d 20 31 3b 0a   fire_this = 1;.
2710a 20 20 20 20 20 20 7d 0a 23 69 66 20 30 20 20 20        }.#if 0   
2710b 20 2f 2a 20 47 69 76 65 20 6e 6f 20 77 61 72 6e   /* Give no warn
2710c 69 6e 67 20 66 6f 72 20 72 65 63 75 72 73 69 76  ing for recursiv
2710d 65 20 74 72 69 67 67 65 72 73 2e 20 20 4a 75 73  e triggers.  Jus
2710e 74 20 64 6f 20 6e 6f 74 20 64 6f 20 74 68 65 6d  t do not do them
2710f 20 2a 2f 0a 20 20 20 20 20 20 65 6c 73 65 7b 0a   */.      else{.
27110 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
27111 72 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20  rrorMsg(pParse, 
27112 22 72 65 63 75 72 73 69 76 65 20 74 72 69 67 67  "recursive trigg
27113 65 72 73 20 6e 6f 74 20 73 75 70 70 6f 72 74 65  ers not supporte
27114 64 20 28 25 73 29 22 2c 0a 20 20 20 20 20 20 20  d (%s)",.       
27115 20 20 20 20 20 70 2d 3e 6e 61 6d 65 29 3b 0a 20       p->name);. 
27116 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51         return SQ
27117 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20  LITE_ERROR;.    
27118 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 7d    }.#endif.    }
27119 0a 20 0a 20 20 20 20 69 66 28 20 66 69 72 65 5f  . .    if( fire_
2711a 74 68 69 73 20 29 7b 0a 20 20 20 20 20 20 69 6e  this ){.      in
2711b 74 20 65 6e 64 54 72 69 67 67 65 72 3b 0a 20 20  t endTrigger;.  
2711c 20 20 20 20 45 78 70 72 20 2a 20 77 68 65 6e 45      Expr * whenE
2711d 78 70 72 3b 0a 20 20 20 20 20 20 41 75 74 68 43  xpr;.      AuthC
2711e 6f 6e 74 65 78 74 20 73 43 6f 6e 74 65 78 74 3b  ontext sContext;
2711f 0a 20 20 20 20 20 20 4e 61 6d 65 43 6f 6e 74 65  .      NameConte
27120 78 74 20 73 4e 43 3b 0a 0a 23 69 66 6e 64 65 66  xt sNC;..#ifndef
27121 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
27122 43 45 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  CE.      sqlite3
27123 56 64 62 65 41 64 64 4f 70 34 28 70 50 61 72 73  VdbeAddOp4(pPars
27124 65 2d 3e 70 56 64 62 65 2c 20 4f 50 5f 54 72 61  e->pVdbe, OP_Tra
27125 63 65 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 20  ce, 0, 0, 0,.   
27126 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27127 20 20 20 20 20 73 71 6c 69 74 65 33 4d 50 72 69       sqlite3MPri
27128 6e 74 66 28 64 62 2c 20 22 2d 2d 20 54 52 49 47  ntf(db, "-- TRIG
27129 47 45 52 20 25 73 22 2c 20 70 2d 3e 6e 61 6d 65  GER %s", p->name
2712a 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
2712b 20 20 20 20 20 20 20 20 20 20 20 50 34 5f 44 59             P4_DY
2712c 4e 41 4d 49 43 29 3b 0a 23 65 6e 64 69 66 0a 20  NAMIC);.#endif. 
2712d 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73 4e 43       memset(&sNC
2712e 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43 29  , 0, sizeof(sNC)
2712f 29 3b 0a 20 20 20 20 20 20 73 4e 43 2e 70 50 61  );.      sNC.pPa
27130 72 73 65 20 3d 20 70 50 61 72 73 65 3b 0a 0a 20  rse = pParse;.. 
27131 20 20 20 20 20 2f 2a 20 50 75 73 68 20 61 6e 20       /* Push an 
27132 65 6e 74 72 79 20 6f 6e 20 74 6f 20 74 68 65 20  entry on to the 
27133 74 72 69 67 67 65 72 20 73 74 61 63 6b 20 2a 2f  trigger stack */
27134 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b  .      trigStack
27135 45 6e 74 72 79 2e 70 54 72 69 67 67 65 72 20 3d  Entry.pTrigger =
27136 20 70 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74   p;.      trigSt
27137 61 63 6b 45 6e 74 72 79 2e 6e 65 77 49 64 78 20  ackEntry.newIdx 
27138 3d 20 6e 65 77 49 64 78 3b 0a 20 20 20 20 20 20  = newIdx;.      
27139 74 72 69 67 53 74 61 63 6b 45 6e 74 72 79 2e 6f  trigStackEntry.o
2713a 6c 64 49 64 78 20 3d 20 6f 6c 64 49 64 78 3b 0a  ldIdx = oldIdx;.
2713b 20 20 20 20 20 20 74 72 69 67 53 74 61 63 6b 45        trigStackE
2713c 6e 74 72 79 2e 70 54 61 62 20 3d 20 70 54 61 62  ntry.pTab = pTab
2713d 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63  ;.      trigStac
2713e 6b 45 6e 74 72 79 2e 70 4e 65 78 74 20 3d 20 70  kEntry.pNext = p
2713f 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
27140 3b 0a 20 20 20 20 20 20 74 72 69 67 53 74 61 63  ;.      trigStac
27141 6b 45 6e 74 72 79 2e 69 67 6e 6f 72 65 4a 75 6d  kEntry.ignoreJum
27142 70 20 3d 20 69 67 6e 6f 72 65 4a 75 6d 70 3b 0a  p = ignoreJump;.
27143 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 74 72        pParse->tr
27144 69 67 53 74 61 63 6b 20 3d 20 26 74 72 69 67 53  igStack = &trigS
27145 74 61 63 6b 45 6e 74 72 79 3b 0a 20 20 20 20 20  tackEntry;.     
27146 20 73 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74   sqlite3AuthCont
27147 65 78 74 50 75 73 68 28 70 50 61 72 73 65 2c 20  extPush(pParse, 
27148 26 73 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 6e 61  &sContext, p->na
27149 6d 65 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 63  me);..      /* c
2714a 6f 64 65 20 74 68 65 20 57 48 45 4e 20 63 6c 61  ode the WHEN cla
2714b 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 65 6e 64  use */.      end
2714c 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65  Trigger = sqlite
2714d 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 70  3VdbeMakeLabel(p
2714e 50 61 72 73 65 2d 3e 70 56 64 62 65 29 3b 0a 20  Parse->pVdbe);. 
2714f 20 20 20 20 20 77 68 65 6e 45 78 70 72 20 3d 20       whenExpr = 
27150 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28 64  sqlite3ExprDup(d
27151 62 2c 20 70 2d 3e 70 57 68 65 6e 29 3b 0a 20 20  b, p->pWhen);.  
27152 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c      if( db->mall
27153 6f 63 46 61 69 6c 65 64 20 7c 7c 20 73 71 6c 69  ocFailed || sqli
27154 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61  te3ResolveExprNa
27155 6d 65 73 28 26 73 4e 43 2c 20 77 68 65 6e 45 78  mes(&sNC, whenEx
27156 70 72 29 20 29 7b 0a 20 20 20 20 20 20 20 20 70  pr) ){.        p
27157 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
27158 20 3d 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72   = trigStackEntr
27159 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 20  y.pNext;.       
2715a 20 73 71 6c 69 74 65 33 45 78 70 72 44 65 6c 65   sqlite3ExprDele
2715b 74 65 28 64 62 2c 20 77 68 65 6e 45 78 70 72 29  te(db, whenExpr)
2715c 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2715d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   1;.      }.    
2715e 20 20 73 71 6c 69 74 65 33 45 78 70 72 49 66 46    sqlite3ExprIfF
2715f 61 6c 73 65 28 70 50 61 72 73 65 2c 20 77 68 65  alse(pParse, whe
27160 6e 45 78 70 72 2c 20 65 6e 64 54 72 69 67 67 65  nExpr, endTrigge
27161 72 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46  r, SQLITE_JUMPIF
27162 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 73 71 6c  NULL);.      sql
27163 69 74 65 33 45 78 70 72 44 65 6c 65 74 65 28 64  ite3ExprDelete(d
27164 62 2c 20 77 68 65 6e 45 78 70 72 29 3b 0a 0a 20  b, whenExpr);.. 
27165 20 20 20 20 20 63 6f 64 65 54 72 69 67 67 65 72       codeTrigger
27166 50 72 6f 67 72 61 6d 28 70 50 61 72 73 65 2c 20  Program(pParse, 
27167 70 2d 3e 73 74 65 70 5f 6c 69 73 74 2c 20 6f 72  p->step_list, or
27168 63 6f 6e 66 29 3b 20 0a 0a 20 20 20 20 20 20 2f  conf); ..      /
27169 2a 20 50 6f 70 20 74 68 65 20 65 6e 74 72 79 20  * Pop the entry 
2716a 6f 66 66 20 74 68 65 20 74 72 69 67 67 65 72 20  off the trigger 
2716b 73 74 61 63 6b 20 2a 2f 0a 20 20 20 20 20 20 70  stack */.      p
2716c 50 61 72 73 65 2d 3e 74 72 69 67 53 74 61 63 6b  Parse->trigStack
2716d 20 3d 20 74 72 69 67 53 74 61 63 6b 45 6e 74 72   = trigStackEntr
2716e 79 2e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20 73  y.pNext;.      s
2716f 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
27170 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b  tPop(&sContext);
27171 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56  ..      sqlite3V
27172 64 62 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28  dbeResolveLabel(
27173 70 50 61 72 73 65 2d 3e 70 56 64 62 65 2c 20 65  pParse->pVdbe, e
27174 6e 64 54 72 69 67 67 65 72 29 3b 0a 20 20 20 20  ndTrigger);.    
27175 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 69 4f 6c  }.  }.  if( piOl
27176 64 43 6f 6c 4d 61 73 6b 20 29 20 2a 70 69 4f 6c  dColMask ) *piOl
27177 64 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74 72 69 67  dColMask |= trig
27178 53 74 61 63 6b 45 6e 74 72 79 2e 6f 6c 64 43 6f  StackEntry.oldCo
27179 6c 4d 61 73 6b 3b 0a 20 20 69 66 28 20 70 69 4e  lMask;.  if( piN
2717a 65 77 43 6f 6c 4d 61 73 6b 20 29 20 2a 70 69 4e  ewColMask ) *piN
2717b 65 77 43 6f 6c 4d 61 73 6b 20 7c 3d 20 74 72 69  ewColMask |= tri
2717c 67 53 74 61 63 6b 45 6e 74 72 79 2e 6e 65 77 43  gStackEntry.newC
2717d 6f 6c 4d 61 73 6b 3b 0a 20 20 72 65 74 75 72 6e  olMask;.  return
2717e 20 30 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20   0;.}.#endif /* 
2717f 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
27180 4f 4d 49 54 5f 54 52 49 47 47 45 52 29 20 2a 2f  OMIT_TRIGGER) */
27181 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
27182 2a 20 45 6e 64 20 6f 66 20 74 72 69 67 67 65 72  * End of trigger
27183 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
27184 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27185 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27186 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
27187 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 75 70 64  * Begin file upd
27188 61 74 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ate.c **********
27189 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2718a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2718b 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31 20 53 65 70  /./*.** 2001 Sep
2718c 74 65 6d 62 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20  tember 15.**.** 
2718d 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
2718e 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
2718f 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
27190 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
27191 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
27192 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
27193 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
27194 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
27195 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
27196 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
27197 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
27198 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
27199 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
2719a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
2719b 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
2719c 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
2719d 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
2719e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2719f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271a1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
271a2 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73  ********.** This
271a3 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43   file contains C
271a4 20 63 6f 64 65 20 72 6f 75 74 69 6e 65 73 20 74   code routines t
271a5 68 61 74 20 61 72 65 20 63 61 6c 6c 65 64 20 62  hat are called b
271a6 79 20 74 68 65 20 70 61 72 73 65 72 0a 2a 2a 20  y the parser.** 
271a7 74 6f 20 68 61 6e 64 6c 65 20 55 50 44 41 54 45  to handle UPDATE
271a8 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a 2a 0a   statements..**.
271a9 2a 2a 20 24 49 64 3a 20 75 70 64 61 74 65 2e 63  ** $Id: update.c
271aa 2c 76 20 31 2e 31 38 37 20 32 30 30 38 2f 31 31  ,v 1.187 2008/11
271ab 2f 31 39 20 30 39 3a 30 35 3a 32 37 20 64 61 6e  /19 09:05:27 dan
271ac 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
271ad 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  /..#ifndef SQLIT
271ae 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
271af 42 4c 45 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64  BLE./* Forward d
271b0 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 73 74  eclaration */.st
271b1 61 74 69 63 20 76 6f 69 64 20 75 70 64 61 74 65  atic void update
271b2 56 69 72 74 75 61 6c 54 61 62 6c 65 28 0a 20 20  VirtualTable(.  
271b3 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
271b4 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
271b5 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
271b6 20 53 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20   SrcList *pSrc, 
271b7 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72        /* The vir
271b8 74 75 61 6c 20 74 61 62 6c 65 20 74 6f 20 62 65  tual table to be
271b9 20 6d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20 54   modified */.  T
271ba 61 62 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20  able *pTab,     
271bb 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
271bc 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78  al table */.  Ex
271bd 70 72 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73  prList *pChanges
271be 2c 20 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e  ,  /* The column
271bf 73 20 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 74  s to change in t
271c0 68 65 20 55 50 44 41 54 45 20 73 74 61 74 65 6d  he UPDATE statem
271c1 65 6e 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  ent */.  Expr *p
271c2 52 6f 77 69 64 45 78 70 72 2c 20 20 20 20 2f 2a  RowidExpr,    /*
271c3 20 45 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64   Expression used
271c4 20 74 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68   to recompute th
271c5 65 20 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74  e rowid */.  int
271c6 20 2a 61 58 52 65 66 2c 20 20 20 20 20 20 20 20   *aXRef,        
271c7 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
271c8 6d 20 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61  m columns of pTa
271c9 62 20 74 6f 20 65 6e 74 72 69 65 73 20 69 6e 20  b to entries in 
271ca 70 43 68 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78  pChanges */.  Ex
271cb 70 72 20 2a 70 57 68 65 72 65 20 20 20 20 20 20  pr *pWhere      
271cc 20 20 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75     /* WHERE clau
271cd 73 65 20 6f 66 20 74 68 65 20 55 50 44 41 54 45  se of the UPDATE
271ce 20 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 3b   statement */.);
271cf 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54  .#endif /* SQLIT
271d0 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
271d1 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  BLE */../*.** Th
271d2 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 6c 79 20  e most recently 
271d3 63 6f 64 65 64 20 69 6e 73 74 72 75 63 74 69 6f  coded instructio
271d4 6e 20 77 61 73 20 61 6e 20 4f 50 5f 43 6f 6c 75  n was an OP_Colu
271d5 6d 6e 20 74 6f 20 72 65 74 72 69 65 76 65 20 74  mn to retrieve t
271d6 68 65 0a 2a 2a 20 69 2d 74 68 20 63 6f 6c 75 6d  he.** i-th colum
271d7 6e 20 6f 66 20 74 61 62 6c 65 20 70 54 61 62 2e  n of table pTab.
271d8 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 73 65   This routine se
271d9 74 73 20 74 68 65 20 50 34 20 70 61 72 61 6d 65  ts the P4 parame
271da 74 65 72 20 6f 66 20 74 68 65 20 0a 2a 2a 20 4f  ter of the .** O
271db 50 5f 43 6f 6c 75 6d 6e 20 74 6f 20 74 68 65 20  P_Column to the 
271dc 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2c 20 69  default value, i
271dd 66 20 61 6e 79 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  f any..**.** The
271de 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 20 6f   default value o
271df 66 20 61 20 63 6f 6c 75 6d 6e 20 69 73 20 73 70  f a column is sp
271e0 65 63 69 66 69 65 64 20 62 79 20 61 20 44 45 46  ecified by a DEF
271e1 41 55 4c 54 20 63 6c 61 75 73 65 20 69 6e 20 74  AULT clause in t
271e2 68 65 20 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 64 65  he .** column de
271e3 66 69 6e 69 74 69 6f 6e 2e 20 54 68 69 73 20 77  finition. This w
271e4 61 73 20 65 69 74 68 65 72 20 73 75 70 70 6c 69  as either suppli
271e5 65 64 20 62 79 20 74 68 65 20 75 73 65 72 20 77  ed by the user w
271e6 68 65 6e 20 74 68 65 20 74 61 62 6c 65 0a 2a 2a  hen the table.**
271e7 20 77 61 73 20 63 72 65 61 74 65 64 2c 20 6f 72   was created, or
271e8 20 61 64 64 65 64 20 6c 61 74 65 72 20 74 6f 20   added later to 
271e9 74 68 65 20 74 61 62 6c 65 20 64 65 66 69 6e 69  the table defini
271ea 74 69 6f 6e 20 62 79 20 61 6e 20 41 4c 54 45 52  tion by an ALTER
271eb 20 54 41 42 4c 45 0a 2a 2a 20 63 6f 6d 6d 61 6e   TABLE.** comman
271ec 64 2e 20 49 66 20 74 68 65 20 6c 61 74 74 65 72  d. If the latter
271ed 2c 20 74 68 65 6e 20 74 68 65 20 72 6f 77 2d 72  , then the row-r
271ee 65 63 6f 72 64 73 20 69 6e 20 74 68 65 20 74 61  ecords in the ta
271ef 62 6c 65 20 62 74 72 65 65 20 6f 6e 20 64 69 73  ble btree on dis
271f0 6b 0a 2a 2a 20 6d 61 79 20 6e 6f 74 20 63 6f 6e  k.** may not con
271f1 74 61 69 6e 20 61 20 76 61 6c 75 65 20 66 6f 72  tain a value for
271f2 20 74 68 65 20 63 6f 6c 75 6d 6e 20 61 6e 64 20   the column and 
271f3 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c 75  the default valu
271f4 65 2c 20 74 61 6b 65 6e 0a 2a 2a 20 66 72 6f 6d  e, taken.** from
271f5 20 74 68 65 20 50 34 20 70 61 72 61 6d 65 74 65   the P4 paramete
271f6 72 20 6f 66 20 74 68 65 20 4f 50 5f 43 6f 6c 75  r of the OP_Colu
271f7 6d 6e 20 69 6e 73 74 72 75 63 74 69 6f 6e 2c 20  mn instruction, 
271f8 69 73 20 72 65 74 75 72 6e 65 64 20 69 6e 73 74  is returned inst
271f9 65 61 64 2e 0a 2a 2a 20 49 66 20 74 68 65 20 66  ead..** If the f
271fa 6f 72 6d 65 72 2c 20 74 68 65 6e 20 61 6c 6c 20  ormer, then all 
271fb 72 6f 77 2d 72 65 63 6f 72 64 73 20 61 72 65 20  row-records are 
271fc 67 75 61 72 61 6e 74 65 65 64 20 74 6f 20 69 6e  guaranteed to in
271fd 63 6c 75 64 65 20 61 20 76 61 6c 75 65 0a 2a 2a  clude a value.**
271fe 20 66 6f 72 20 74 68 65 20 63 6f 6c 75 6d 6e 20   for the column 
271ff 61 6e 64 20 74 68 65 20 50 34 20 76 61 6c 75 65  and the P4 value
27200 20 69 73 20 6e 6f 74 20 72 65 71 75 69 72 65 64   is not required
27201 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6c 75 6d 6e 20 64  ..**.** Column d
27202 65 66 69 6e 69 74 69 6f 6e 73 20 63 72 65 61 74  efinitions creat
27203 65 64 20 62 79 20 61 6e 20 41 4c 54 45 52 20 54  ed by an ALTER T
27204 41 42 4c 45 20 63 6f 6d 6d 61 6e 64 20 6d 61 79  ABLE command may
27205 20 6f 6e 6c 79 20 68 61 76 65 20 0a 2a 2a 20 6c   only have .** l
27206 69 74 65 72 61 6c 20 64 65 66 61 75 6c 74 20 76  iteral default v
27207 61 6c 75 65 73 20 73 70 65 63 69 66 69 65 64 3a  alues specified:
27208 20 61 20 6e 75 6d 62 65 72 2c 20 6e 75 6c 6c 20   a number, null 
27209 6f 72 20 61 20 73 74 72 69 6e 67 2e 20 28 49 66  or a string. (If
2720a 20 61 20 6d 6f 72 65 0a 2a 2a 20 63 6f 6d 70 6c   a more.** compl
2720b 69 63 61 74 65 64 20 64 65 66 61 75 6c 74 20 65  icated default e
2720c 78 70 72 65 73 73 69 6f 6e 20 76 61 6c 75 65 20  xpression value 
2720d 77 61 73 20 70 72 6f 76 69 64 65 64 2c 20 69 74  was provided, it
2720e 20 69 73 20 65 76 61 6c 75 61 74 65 64 20 0a 2a   is evaluated .*
2720f 2a 20 77 68 65 6e 20 74 68 65 20 41 4c 54 45 52  * when the ALTER
27210 20 54 41 42 4c 45 20 69 73 20 65 78 65 63 75 74   TABLE is execut
27211 65 64 20 61 6e 64 20 6f 6e 65 20 6f 66 20 74 68  ed and one of th
27212 65 20 6c 69 74 65 72 61 6c 20 76 61 6c 75 65 73  e literal values
27213 20 77 72 69 74 74 65 6e 0a 2a 2a 20 69 6e 74 6f   written.** into
27214 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
27215 65 72 20 74 61 62 6c 65 2e 29 0a 2a 2a 0a 2a 2a  er table.).**.**
27216 20 54 68 65 72 65 66 6f 72 65 2c 20 74 68 65 20   Therefore, the 
27217 50 34 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  P4 parameter is 
27218 6f 6e 6c 79 20 72 65 71 75 69 72 65 64 20 69 66  only required if
27219 20 74 68 65 20 64 65 66 61 75 6c 74 20 76 61 6c   the default val
2721a 75 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 63 6f  ue for.** the co
2721b 6c 75 6d 6e 20 69 73 20 61 20 6c 69 74 65 72 61  lumn is a litera
2721c 6c 20 6e 75 6d 62 65 72 2c 20 73 74 72 69 6e 67  l number, string
2721d 20 6f 72 20 6e 75 6c 6c 2e 20 54 68 65 20 73 71   or null. The sq
2721e 6c 69 74 65 33 56 61 6c 75 65 46 72 6f 6d 45 78  lite3ValueFromEx
2721f 70 72 28 29 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  pr().** function
27220 20 69 73 20 63 61 70 61 62 6c 65 20 6f 66 20 74   is capable of t
27221 72 61 6e 73 66 6f 72 6d 69 6e 67 20 74 68 65 73  ransforming thes
27222 65 20 74 79 70 65 73 20 6f 66 20 65 78 70 72 65  e types of expre
27223 73 73 69 6f 6e 73 20 69 6e 74 6f 0a 2a 2a 20 73  ssions into.** s
27224 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 6f 62 6a  qlite3_value obj
27225 65 63 74 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ects..*/.SQLITE_
27226 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
27227 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75 6c  ite3ColumnDefaul
27228 74 28 56 64 62 65 20 2a 76 2c 20 54 61 62 6c 65  t(Vdbe *v, Table
27229 20 2a 70 54 61 62 2c 20 69 6e 74 20 69 29 7b 0a   *pTab, int i){.
2722a 20 20 69 66 28 20 70 54 61 62 20 26 26 20 21 70    if( pTab && !p
2722b 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 7b 0a  Tab->pSelect ){.
2722c 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75      sqlite3_valu
2722d 65 20 2a 70 56 61 6c 75 65 3b 0a 20 20 20 20 75  e *pValue;.    u
2722e 38 20 65 6e 63 20 3d 20 45 4e 43 28 73 71 6c 69  8 enc = ENC(sqli
2722f 74 65 33 56 64 62 65 44 62 28 76 29 29 3b 0a 20  te3VdbeDb(v));. 
27230 20 20 20 43 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20     Column *pCol 
27231 3d 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d  = &pTab->aCol[i]
27232 3b 0a 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e  ;.    VdbeCommen
27233 74 28 28 76 2c 20 22 25 73 2e 25 73 22 2c 20 70  t((v, "%s.%s", p
27234 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 70 43 6f 6c  Tab->zName, pCol
27235 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 20 20 20 20 61  ->zName));.    a
27236 73 73 65 72 74 28 20 69 3c 70 54 61 62 2d 3e 6e  ssert( i<pTab->n
27237 43 6f 6c 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  Col );.    sqlit
27238 65 33 56 61 6c 75 65 46 72 6f 6d 45 78 70 72 28  e3ValueFromExpr(
27239 73 71 6c 69 74 65 33 56 64 62 65 44 62 28 76 29  sqlite3VdbeDb(v)
2723a 2c 20 70 43 6f 6c 2d 3e 70 44 66 6c 74 2c 20 65  , pCol->pDflt, e
2723b 6e 63 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  nc, .           
2723c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43                pC
2723d 6f 6c 2d 3e 61 66 66 69 6e 69 74 79 2c 20 26 70  ol->affinity, &p
2723e 56 61 6c 75 65 29 3b 0a 20 20 20 20 69 66 28 20  Value);.    if( 
2723f 70 56 61 6c 75 65 20 29 7b 0a 20 20 20 20 20 20  pValue ){.      
27240 73 71 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67  sqlite3VdbeChang
27241 65 50 34 28 76 2c 20 2d 31 2c 20 28 63 6f 6e 73  eP4(v, -1, (cons
27242 74 20 63 68 61 72 20 2a 29 70 56 61 6c 75 65 2c  t char *)pValue,
27243 20 50 34 5f 4d 45 4d 29 3b 0a 20 20 20 20 7d 0a   P4_MEM);.    }.
27244 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 6f    }.}../*.** Pro
27245 63 65 73 73 20 61 6e 20 55 50 44 41 54 45 20 73  cess an UPDATE s
27246 74 61 74 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20  tatement..**.** 
27247 20 20 55 50 44 41 54 45 20 4f 52 20 49 47 4e 4f    UPDATE OR IGNO
27248 52 45 20 74 61 62 6c 65 5f 77 78 79 7a 20 53 45  RE table_wxyz SE
27249 54 20 61 3d 62 2c 20 63 3d 64 20 57 48 45 52 45  T a=b, c=d WHERE
2724a 20 65 3c 35 20 41 4e 44 20 66 20 4e 4f 54 20 4e   e<5 AND f NOT N
2724b 55 4c 4c 3b 0a 2a 2a 20 20 20 20 20 20 20 20 20  ULL;.**         
2724c 20 5c 5f 5f 5f 5f 5f 5f 5f 2f 20 5c 5f 5f 5f 5f   \_______/ \____
2724d 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f  ____/     \_____
2724e 5f 2f 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f  _/       \______
2724f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f 0a 2a 20 20 20  __________/.*   
27250 20 20 20 20 20 20 20 20 20 6f 6e 45 72 72 6f 72           onError
27251 20 20 20 70 54 61 62 4c 69 73 74 20 20 20 20 20     pTabList     
27252 20 70 43 68 61 6e 67 65 73 20 20 20 20 20 20 20   pChanges       
27253 20 20 20 20 20 20 70 57 68 65 72 65 0a 2a 2f 0a        pWhere.*/.
27254 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
27255 6f 69 64 20 73 71 6c 69 74 65 33 55 70 64 61 74  oid sqlite3Updat
27256 65 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  e(.  Parse *pPar
27257 73 65 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 54  se,         /* T
27258 68 65 20 70 61 72 73 65 72 20 63 6f 6e 74 65 78  he parser contex
27259 74 20 2a 2f 0a 20 20 53 72 63 4c 69 73 74 20 2a  t */.  SrcList *
2725a 70 54 61 62 4c 69 73 74 2c 20 20 20 20 20 2f 2a  pTabList,     /*
2725b 20 54 68 65 20 74 61 62 6c 65 20 69 6e 20 77 68   The table in wh
2725c 69 63 68 20 77 65 20 73 68 6f 75 6c 64 20 63 68  ich we should ch
2725d 61 6e 67 65 20 74 68 69 6e 67 73 20 2a 2f 0a 20  ange things */. 
2725e 20 45 78 70 72 4c 69 73 74 20 2a 70 43 68 61 6e   ExprList *pChan
2725f 67 65 73 2c 20 20 20 20 2f 2a 20 54 68 69 6e 67  ges,    /* Thing
27260 73 20 74 6f 20 62 65 20 63 68 61 6e 67 65 64 20  s to be changed 
27261 2a 2f 0a 20 20 45 78 70 72 20 2a 70 57 68 65 72  */.  Expr *pWher
27262 65 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  e,          /* T
27263 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 2e  he WHERE clause.
27264 20 20 4d 61 79 20 62 65 20 6e 75 6c 6c 20 2a 2f    May be null */
27265 0a 20 20 69 6e 74 20 6f 6e 45 72 72 6f 72 20 20  .  int onError  
27266 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48 6f 77            /* How
27267 20 74 6f 20 68 61 6e 64 6c 65 20 63 6f 6e 73 74   to handle const
27268 72 61 69 6e 74 20 65 72 72 6f 72 73 20 2a 2f 0a  raint errors */.
27269 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
2726a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2726b 6f 6f 70 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a  oop counters */.
2726c 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 20 20    Table *pTab;  
2726d 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2726e 74 61 62 6c 65 20 74 6f 20 62 65 20 75 70 64 61  table to be upda
2726f 74 65 64 20 2a 2f 0a 20 20 69 6e 74 20 61 64 64  ted */.  int add
27270 72 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20  r = 0;          
27271 2f 2a 20 56 44 42 45 20 69 6e 73 74 72 75 63 74  /* VDBE instruct
27272 69 6f 6e 20 61 64 64 72 65 73 73 20 6f 66 20 74  ion address of t
27273 68 65 20 73 74 61 72 74 20 6f 66 20 74 68 65 20  he start of the 
27274 6c 6f 6f 70 20 2a 2f 0a 20 20 57 68 65 72 65 49  loop */.  WhereI
27275 6e 66 6f 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20  nfo *pWInfo;    
27276 20 2f 2a 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20   /* Information 
27277 61 62 6f 75 74 20 74 68 65 20 57 48 45 52 45 20  about the WHERE 
27278 63 6c 61 75 73 65 20 2a 2f 0a 20 20 56 64 62 65  clause */.  Vdbe
27279 20 2a 76 3b 20 20 20 20 20 20 20 20 20 20 20 20   *v;            
2727a 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2727b 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
2727c 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20 2a 70 49  e */.  Index *pI
2727d 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a  dx;           /*
2727e 20 46 6f 72 20 6c 6f 6f 70 69 6e 67 20 6f 76 65   For looping ove
2727f 72 20 69 6e 64 69 63 65 73 20 2a 2f 0a 20 20 69  r indices */.  i
27280 6e 74 20 6e 49 64 78 3b 20 20 20 20 20 20 20 20  nt nIdx;        
27281 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
27282 6f 66 20 69 6e 64 69 63 65 73 20 74 68 61 74 20  of indices that 
27283 6e 65 65 64 20 75 70 64 61 74 69 6e 67 20 2a 2f  need updating */
27284 0a 20 20 69 6e 74 20 69 43 75 72 3b 20 20 20 20  .  int iCur;    
27285 20 20 20 20 20 20 20 20 20 20 2f 2a 20 56 44 42            /* VDB
27286 45 20 43 75 72 73 6f 72 20 6e 75 6d 62 65 72 20  E Cursor number 
27287 6f 66 20 70 54 61 62 20 2a 2f 0a 20 20 73 71 6c  of pTab */.  sql
27288 69 74 65 33 20 2a 64 62 3b 20 20 20 20 20 20 20  ite3 *db;       
27289 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62      /* The datab
2728a 61 73 65 20 73 74 72 75 63 74 75 72 65 20 2a 2f  ase structure */
2728b 0a 20 20 69 6e 74 20 2a 61 52 65 67 49 64 78 20  .  int *aRegIdx 
2728c 3d 20 30 3b 20 20 20 20 20 20 2f 2a 20 4f 6e 65  = 0;      /* One
2728d 20 72 65 67 69 73 74 65 72 20 61 73 73 69 67 6e   register assign
2728e 65 64 20 74 6f 20 65 61 63 68 20 69 6e 64 65 78  ed to each index
2728f 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20 2a   to be updated *
27290 2f 0a 20 20 69 6e 74 20 2a 61 58 52 65 66 20 3d  /.  int *aXRef =
27291 20 30 3b 20 20 20 20 20 20 20 20 2f 2a 20 61 58   0;        /* aX
27292 52 65 66 5b 69 5d 20 69 73 20 74 68 65 20 69 6e  Ref[i] is the in
27293 64 65 78 20 69 6e 20 70 43 68 61 6e 67 65 73 2d  dex in pChanges-
27294 3e 61 5b 5d 20 6f 66 20 74 68 65 0a 20 20 20 20  >a[] of the.    
27295 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27296 20 20 20 20 20 2a 2a 20 61 6e 20 65 78 70 72 65       ** an expre
27297 73 73 69 6f 6e 20 66 6f 72 20 74 68 65 20 69 2d  ssion for the i-
27298 74 68 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65  th column of the
27299 20 74 61 62 6c 65 2e 0a 20 20 20 20 20 20 20 20   table..        
2729a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2729b 20 2a 2a 20 61 58 52 65 66 5b 69 5d 3d 3d 2d 31   ** aXRef[i]==-1
2729c 20 69 66 20 74 68 65 20 69 2d 74 68 20 63 6f 6c   if the i-th col
2729d 75 6d 6e 20 69 73 20 6e 6f 74 20 63 68 61 6e 67  umn is not chang
2729e 65 64 2e 20 2a 2f 0a 20 20 69 6e 74 20 63 68 6e  ed. */.  int chn
2729f 67 52 6f 77 69 64 3b 20 20 20 20 20 20 20 20 20  gRowid;         
272a0 2f 2a 20 54 72 75 65 20 69 66 20 74 68 65 20 72  /* True if the r
272a1 65 63 6f 72 64 20 6e 75 6d 62 65 72 20 69 73 20  ecord number is 
272a2 62 65 69 6e 67 20 63 68 61 6e 67 65 64 20 2a 2f  being changed */
272a3 0a 20 20 45 78 70 72 20 2a 70 52 6f 77 69 64 45  .  Expr *pRowidE
272a4 78 70 72 20 3d 20 30 3b 20 20 2f 2a 20 45 78 70  xpr = 0;  /* Exp
272a5 72 65 73 73 69 6f 6e 20 64 65 66 69 6e 69 6e 67  ression defining
272a6 20 74 68 65 20 6e 65 77 20 72 65 63 6f 72 64 20   the new record 
272a7 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 69 6e 74 20  number */.  int 
272a8 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 20 20 20 20  openAll = 0;    
272a9 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20 61 6c     /* True if al
272aa 6c 20 69 6e 64 69 63 65 73 20 6e 65 65 64 20 74  l indices need t
272ab 6f 20 62 65 20 6f 70 65 6e 65 64 20 2a 2f 0a 20  o be opened */. 
272ac 20 41 75 74 68 43 6f 6e 74 65 78 74 20 73 43 6f   AuthContext sCo
272ad 6e 74 65 78 74 3b 20 20 2f 2a 20 54 68 65 20 61  ntext;  /* The a
272ae 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 63 6f 6e  uthorization con
272af 74 65 78 74 20 2a 2f 0a 20 20 4e 61 6d 65 43 6f  text */.  NameCo
272b0 6e 74 65 78 74 20 73 4e 43 3b 20 20 20 20 20 20  ntext sNC;      
272b1 20 2f 2a 20 54 68 65 20 6e 61 6d 65 2d 63 6f 6e   /* The name-con
272b2 74 65 78 74 20 74 6f 20 72 65 73 6f 6c 76 65 20  text to resolve 
272b3 65 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 2a  expressions in *
272b4 2f 0a 20 20 69 6e 74 20 69 44 62 3b 20 20 20 20  /.  int iDb;    
272b5 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61             /* Da
272b6 74 61 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e  tabase containin
272b7 67 20 74 68 65 20 74 61 62 6c 65 20 62 65 69 6e  g the table bein
272b8 67 20 75 70 64 61 74 65 64 20 2a 2f 0a 20 20 69  g updated */.  i
272b9 6e 74 20 6a 31 3b 20 20 20 20 20 20 20 20 20 20  nt j1;          
272ba 20 20 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73        /* Address
272bb 65 73 20 6f 66 20 6a 75 6d 70 20 69 6e 73 74 72  es of jump instr
272bc 75 63 74 69 6f 6e 73 20 2a 2f 0a 20 20 69 6e 74  uctions */.  int
272bd 20 6f 6b 4f 6e 65 50 61 73 73 3b 20 20 20 20 20   okOnePass;     
272be 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f 72 20      /* True for 
272bf 6f 6e 65 2d 70 61 73 73 20 61 6c 67 6f 72 69 74  one-pass algorit
272c0 68 6d 20 77 69 74 68 6f 75 74 20 74 68 65 20 46  hm without the F
272c1 49 46 4f 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20  IFO */..#ifndef 
272c2 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
272c3 47 45 52 0a 20 20 69 6e 74 20 69 73 56 69 65 77  GER.  int isView
272c4 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
272c5 20 20 20 2f 2a 20 54 72 79 69 6e 67 20 74 6f 20     /* Trying to 
272c6 75 70 64 61 74 65 20 61 20 76 69 65 77 20 2a 2f  update a view */
272c7 0a 20 20 69 6e 74 20 74 72 69 67 67 65 72 73 5f  .  int triggers_
272c8 65 78 69 73 74 20 3d 20 30 3b 20 20 20 20 20 20  exist = 0;      
272c9 2f 2a 20 54 72 75 65 20 69 66 20 61 6e 79 20 72  /* True if any r
272ca 6f 77 20 74 72 69 67 67 65 72 73 20 65 78 69 73  ow triggers exis
272cb 74 20 2a 2f 0a 23 65 6e 64 69 66 0a 20 20 69 6e  t */.#endif.  in
272cc 74 20 69 42 65 67 69 6e 41 66 74 65 72 54 72 69  t iBeginAfterTri
272cd 67 67 65 72 3b 20 20 20 20 20 20 2f 2a 20 41 64  gger;      /* Ad
272ce 64 72 65 73 73 20 6f 66 20 61 66 74 65 72 20 74  dress of after t
272cf 72 69 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a  rigger program *
272d0 2f 0a 20 20 69 6e 74 20 69 45 6e 64 41 66 74 65  /.  int iEndAfte
272d1 72 54 72 69 67 67 65 72 3b 20 20 20 20 20 20 20  rTrigger;       
272d2 20 2f 2a 20 45 78 69 74 20 6f 66 20 61 66 74 65   /* Exit of afte
272d3 72 20 74 72 69 67 67 65 72 20 70 72 6f 67 72 61  r trigger progra
272d4 6d 20 2a 2f 0a 20 20 69 6e 74 20 69 42 65 67 69  m */.  int iBegi
272d5 6e 42 65 66 6f 72 65 54 72 69 67 67 65 72 3b 20  nBeforeTrigger; 
272d6 20 20 20 20 2f 2a 20 41 64 64 72 65 73 73 20 6f      /* Address o
272d7 66 20 62 65 66 6f 72 65 20 74 72 69 67 67 65 72  f before trigger
272d8 20 70 72 6f 67 72 61 6d 20 2a 2f 0a 20 20 69 6e   program */.  in
272d9 74 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67  t iEndBeforeTrig
272da 67 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 45 78  ger;       /* Ex
272db 69 74 20 6f 66 20 62 65 66 6f 72 65 20 74 72 69  it of before tri
272dc 67 67 65 72 20 70 72 6f 67 72 61 6d 20 2a 2f 0a  gger program */.
272dd 20 20 75 33 32 20 6f 6c 64 5f 63 6f 6c 5f 6d 61    u32 old_col_ma
272de 73 6b 20 3d 20 30 3b 20 20 20 20 20 20 20 20 2f  sk = 0;        /
272df 2a 20 4d 61 73 6b 20 6f 66 20 4f 4c 44 2e 2a 20  * Mask of OLD.* 
272e0 63 6f 6c 75 6d 6e 73 20 69 6e 20 75 73 65 20 2a  columns in use *
272e1 2f 0a 20 20 75 33 32 20 6e 65 77 5f 63 6f 6c 5f  /.  u32 new_col_
272e2 6d 61 73 6b 20 3d 20 30 3b 20 20 20 20 20 20 20  mask = 0;       
272e3 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 4e 45 57 2e   /* Mask of NEW.
272e4 2a 20 63 6f 6c 75 6d 6e 73 20 69 6e 20 75 73 65  * columns in use
272e5 20 2a 2f 0a 0a 20 20 69 6e 74 20 6e 65 77 49 64   */..  int newId
272e6 78 20 20 20 20 20 20 3d 20 2d 31 3b 20 20 2f 2a  x      = -1;  /*
272e7 20 69 6e 64 65 78 20 6f 66 20 74 72 69 67 67 65   index of trigge
272e8 72 20 22 6e 65 77 22 20 74 65 6d 70 20 74 61 62  r "new" temp tab
272e9 6c 65 20 20 20 20 20 20 20 2a 2f 0a 20 20 69 6e  le       */.  in
272ea 74 20 6f 6c 64 49 64 78 20 20 20 20 20 20 3d 20  t oldIdx      = 
272eb 2d 31 3b 20 20 2f 2a 20 69 6e 64 65 78 20 6f 66  -1;  /* index of
272ec 20 74 72 69 67 67 65 72 20 22 6f 6c 64 22 20 74   trigger "old" t
272ed 65 6d 70 20 74 61 62 6c 65 20 20 20 20 20 20 20  emp table       
272ee 2a 2f 0a 0a 20 20 2f 2a 20 52 65 67 69 73 74 65  */..  /* Registe
272ef 72 20 41 6c 6c 6f 63 61 74 69 6f 6e 73 20 2a 2f  r Allocations */
272f0 0a 20 20 69 6e 74 20 72 65 67 52 6f 77 43 6f 75  .  int regRowCou
272f1 6e 74 20 3d 20 30 3b 20 20 20 2f 2a 20 41 20 63  nt = 0;   /* A c
272f2 6f 75 6e 74 20 6f 66 20 72 6f 77 73 20 63 68 61  ount of rows cha
272f3 6e 67 65 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65  nged */.  int re
272f4 67 4f 6c 64 52 6f 77 69 64 3b 20 20 20 20 20 20  gOldRowid;      
272f5 20 2f 2a 20 54 68 65 20 6f 6c 64 20 72 6f 77 69   /* The old rowi
272f6 64 20 2a 2f 0a 20 20 69 6e 74 20 72 65 67 4e 65  d */.  int regNe
272f7 77 52 6f 77 69 64 3b 20 20 20 20 20 20 20 2f 2a  wRowid;       /*
272f8 20 54 68 65 20 6e 65 77 20 72 6f 77 69 64 20 2a   The new rowid *
272f9 2f 0a 20 20 69 6e 74 20 72 65 67 44 61 74 61 3b  /.  int regData;
272fa 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 65             /* Ne
272fb 77 20 64 61 74 61 20 66 6f 72 20 74 68 65 20 72  w data for the r
272fc 6f 77 20 2a 2f 0a 0a 20 20 73 43 6f 6e 74 65 78  ow */..  sContex
272fd 74 2e 70 50 61 72 73 65 20 3d 20 30 3b 0a 20 20  t.pParse = 0;.  
272fe 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
272ff 0a 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e  .  if( pParse->n
27300 45 72 72 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c 6f  Err || db->mallo
27301 63 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 67  cFailed ){.    g
27302 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e  oto update_clean
27303 75 70 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  up;.  }.  assert
27304 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  ( pTabList->nSrc
27305 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 4c 6f 63  ==1 );..  /* Loc
27306 61 74 65 20 74 68 65 20 74 61 62 6c 65 20 77 68  ate the table wh
27307 69 63 68 20 77 65 20 77 61 6e 74 20 74 6f 20 75  ich we want to u
27308 70 64 61 74 65 2e 20 0a 20 20 2a 2f 0a 20 20 70  pdate. .  */.  p
27309 54 61 62 20 3d 20 73 71 6c 69 74 65 33 53 72 63  Tab = sqlite3Src
2730a 4c 69 73 74 4c 6f 6f 6b 75 70 28 70 50 61 72 73  ListLookup(pPars
2730b 65 2c 20 70 54 61 62 4c 69 73 74 29 3b 0a 20 20  e, pTabList);.  
2730c 69 66 28 20 70 54 61 62 3d 3d 30 20 29 20 67 6f  if( pTab==0 ) go
2730d 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75  to update_cleanu
2730e 70 3b 0a 20 20 69 44 62 20 3d 20 73 71 6c 69 74  p;.  iDb = sqlit
2730f 65 33 53 63 68 65 6d 61 54 6f 49 6e 64 65 78 28  e3SchemaToIndex(
27310 70 50 61 72 73 65 2d 3e 64 62 2c 20 70 54 61 62  pParse->db, pTab
27311 2d 3e 70 53 63 68 65 6d 61 29 3b 0a 0a 20 20 2f  ->pSchema);..  /
27312 2a 20 46 69 67 75 72 65 20 6f 75 74 20 69 66 20  * Figure out if 
27313 77 65 20 68 61 76 65 20 61 6e 79 20 74 72 69 67  we have any trig
27314 67 65 72 73 20 61 6e 64 20 69 66 20 74 68 65 20  gers and if the 
27315 74 61 62 6c 65 20 62 65 69 6e 67 0a 20 20 2a 2a  table being.  **
27316 20 75 70 64 61 74 65 64 20 69 73 20 61 20 76 69   updated is a vi
27317 65 77 0a 20 20 2a 2f 0a 23 69 66 6e 64 65 66 20  ew.  */.#ifndef 
27318 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47  SQLITE_OMIT_TRIG
27319 47 45 52 0a 20 20 74 72 69 67 67 65 72 73 5f 65  GER.  triggers_e
2731a 78 69 73 74 20 3d 20 73 71 6c 69 74 65 33 54 72  xist = sqlite3Tr
2731b 69 67 67 65 72 73 45 78 69 73 74 28 70 54 61 62  iggersExist(pTab
2731c 2c 20 54 4b 5f 55 50 44 41 54 45 2c 20 70 43 68  , TK_UPDATE, pCh
2731d 61 6e 67 65 73 29 3b 0a 20 20 69 73 56 69 65 77  anges);.  isView
2731e 20 3d 20 70 54 61 62 2d 3e 70 53 65 6c 65 63 74   = pTab->pSelect
2731f 21 3d 30 3b 0a 23 65 6c 73 65 0a 23 20 64 65 66  !=0;.#else.# def
27320 69 6e 65 20 74 72 69 67 67 65 72 73 5f 65 78 69  ine triggers_exi
27321 73 74 20 30 0a 23 20 64 65 66 69 6e 65 20 69 73  st 0.# define is
27322 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 23 69  View 0.#endif.#i
27323 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  fdef SQLITE_OMIT
27324 5f 56 49 45 57 0a 23 20 75 6e 64 65 66 20 69 73  _VIEW.# undef is
27325 56 69 65 77 0a 23 20 64 65 66 69 6e 65 20 69 73  View.# define is
27326 56 69 65 77 20 30 0a 23 65 6e 64 69 66 0a 0a 20  View 0.#endif.. 
27327 20 69 66 28 20 73 71 6c 69 74 65 33 49 73 52 65   if( sqlite3IsRe
27328 61 64 4f 6e 6c 79 28 70 50 61 72 73 65 2c 20 70  adOnly(pParse, p
27329 54 61 62 2c 20 74 72 69 67 67 65 72 73 5f 65 78  Tab, triggers_ex
2732a 69 73 74 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f  ist) ){.    goto
2732b 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2732c 0a 20 20 7d 0a 20 20 69 66 28 20 73 71 6c 69 74  .  }.  if( sqlit
2732d 65 33 56 69 65 77 47 65 74 43 6f 6c 75 6d 6e 4e  e3ViewGetColumnN
2732e 61 6d 65 73 28 70 50 61 72 73 65 2c 20 70 54 61  ames(pParse, pTa
2732f 62 29 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 75  b) ){.    goto u
27330 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
27331 20 7d 0a 20 20 61 58 52 65 66 20 3d 20 73 71 6c   }.  aXRef = sql
27332 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77 28  ite3DbMallocRaw(
27333 64 62 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 20  db, sizeof(int) 
27334 2a 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 3b 0a  * pTab->nCol );.
27335 20 20 69 66 28 20 61 58 52 65 66 3d 3d 30 20 29    if( aXRef==0 )
27336 20 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65   goto update_cle
27337 61 6e 75 70 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  anup;.  for(i=0;
27338 20 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69   i<pTab->nCol; i
27339 2b 2b 29 20 61 58 52 65 66 5b 69 5d 20 3d 20 2d  ++) aXRef[i] = -
2733a 31 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72  1;..  /* If ther
2733b 65 20 61 72 65 20 46 4f 52 20 45 41 43 48 20 52  e are FOR EACH R
2733c 4f 57 20 74 72 69 67 67 65 72 73 2c 20 61 6c 6c  OW triggers, all
2733d 6f 63 61 74 65 20 63 75 72 73 6f 72 73 20 66 6f  ocate cursors fo
2733e 72 20 74 68 65 0a 20 20 2a 2a 20 73 70 65 63 69  r the.  ** speci
2733f 61 6c 20 4f 4c 44 20 61 6e 64 20 4e 45 57 20 74  al OLD and NEW t
27340 61 62 6c 65 73 0a 20 20 2a 2f 0a 20 20 69 66 28  ables.  */.  if(
27341 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
27342 29 7b 0a 20 20 20 20 6e 65 77 49 64 78 20 3d 20  ){.    newIdx = 
27343 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a  pParse->nTab++;.
27344 20 20 20 20 6f 6c 64 49 64 78 20 3d 20 70 50 61      oldIdx = pPa
27345 72 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d  rse->nTab++;.  }
27346 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20  ..  /* Allocate 
27347 61 20 63 75 72 73 6f 72 73 20 66 6f 72 20 74 68  a cursors for th
27348 65 20 6d 61 69 6e 20 64 61 74 61 62 61 73 65 20  e main database 
27349 74 61 62 6c 65 20 61 6e 64 20 66 6f 72 20 61 6c  table and for al
2734a 6c 20 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2a 20  l indices..  ** 
2734b 54 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f 72  The index cursor
2734c 73 20 6d 69 67 68 74 20 6e 6f 74 20 62 65 20 75  s might not be u
2734d 73 65 64 2c 20 62 75 74 20 69 66 20 74 68 65 79  sed, but if they
2734e 20 61 72 65 20 75 73 65 64 20 74 68 65 79 0a 20   are used they. 
2734f 20 2a 2a 20 6e 65 65 64 20 74 6f 20 6f 63 63 75   ** need to occu
27350 72 20 72 69 67 68 74 20 61 66 74 65 72 20 74 68  r right after th
27351 65 20 64 61 74 61 62 61 73 65 20 63 75 72 73 6f  e database curso
27352 72 2e 20 20 53 6f 20 67 6f 20 61 68 65 61 64 20  r.  So go ahead 
27353 61 6e 64 0a 20 20 2a 2a 20 61 6c 6c 6f 63 61 74  and.  ** allocat
27354 65 20 65 6e 6f 75 67 68 20 73 70 61 63 65 2c 20  e enough space, 
27355 6a 75 73 74 20 69 6e 20 63 61 73 65 2e 0a 20 20  just in case..  
27356 2a 2f 0a 20 20 70 54 61 62 4c 69 73 74 2d 3e 61  */.  pTabList->a
27357 5b 30 5d 2e 69 43 75 72 73 6f 72 20 3d 20 69 43  [0].iCursor = iC
27358 75 72 20 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61  ur = pParse->nTa
27359 62 2b 2b 3b 0a 20 20 66 6f 72 28 70 49 64 78 3d  b++;.  for(pIdx=
2735a 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49  pTab->pIndex; pI
2735b 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70  dx; pIdx=pIdx->p
2735c 4e 65 78 74 29 7b 0a 20 20 20 20 70 50 61 72 73  Next){.    pPars
2735d 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 7d 0a 0a  e->nTab++;.  }..
2735e 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2735f 74 68 65 20 6e 61 6d 65 2d 63 6f 6e 74 65 78 74  the name-context
27360 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28 26 73 4e   */.  memset(&sN
27361 43 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 4e 43  C, 0, sizeof(sNC
27362 29 29 3b 0a 20 20 73 4e 43 2e 70 50 61 72 73 65  ));.  sNC.pParse
27363 20 3d 20 70 50 61 72 73 65 3b 0a 20 20 73 4e 43   = pParse;.  sNC
27364 2e 70 53 72 63 4c 69 73 74 20 3d 20 70 54 61 62  .pSrcList = pTab
27365 4c 69 73 74 3b 0a 0a 20 20 2f 2a 20 52 65 73 6f  List;..  /* Reso
27366 6c 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  lve the column n
27367 61 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20  ames in all the 
27368 65 78 70 72 65 73 73 69 6f 6e 73 20 6f 66 20 74  expressions of t
27369 68 65 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 55  he.  ** of the U
2736a 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e 74 2e  PDATE statement.
2736b 20 20 41 6c 73 6f 20 66 69 6e 64 20 74 68 65 20    Also find the 
2736c 63 6f 6c 75 6d 6e 20 69 6e 64 65 78 0a 20 20 2a  column index.  *
2736d 2a 20 66 6f 72 20 65 61 63 68 20 63 6f 6c 75 6d  * for each colum
2736e 6e 20 74 6f 20 62 65 20 75 70 64 61 74 65 64 20  n to be updated 
2736f 69 6e 20 74 68 65 20 70 43 68 61 6e 67 65 73 20  in the pChanges 
27370 61 72 72 61 79 2e 20 20 46 6f 72 20 65 61 63 68  array.  For each
27371 0a 20 20 2a 2a 20 63 6f 6c 75 6d 6e 20 74 6f 20  .  ** column to 
27372 62 65 20 75 70 64 61 74 65 64 2c 20 6d 61 6b 65  be updated, make
27373 20 73 75 72 65 20 77 65 20 68 61 76 65 20 61 75   sure we have au
27374 74 68 6f 72 69 7a 61 74 69 6f 6e 20 74 6f 20 63  thorization to c
27375 68 61 6e 67 65 0a 20 20 2a 2a 20 74 68 61 74 20  hange.  ** that 
27376 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2f 0a 20 20 63  column..  */.  c
27377 68 6e 67 52 6f 77 69 64 20 3d 20 30 3b 0a 20 20  hngRowid = 0;.  
27378 66 6f 72 28 69 3d 30 3b 20 69 3c 70 43 68 61 6e  for(i=0; i<pChan
27379 67 65 73 2d 3e 6e 45 78 70 72 3b 20 69 2b 2b 29  ges->nExpr; i++)
2737a 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
2737b 33 52 65 73 6f 6c 76 65 45 78 70 72 4e 61 6d 65  3ResolveExprName
2737c 73 28 26 73 4e 43 2c 20 70 43 68 61 6e 67 65 73  s(&sNC, pChanges
2737d 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 20 29 7b  ->a[i].pExpr) ){
2737e 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61  .      goto upda
2737f 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
27380 7d 0a 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a  }.    for(j=0; j
27381 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 6a 2b 2b  <pTab->nCol; j++
27382 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 71 6c  ){.      if( sql
27383 69 74 65 33 53 74 72 49 43 6d 70 28 70 54 61 62  ite3StrICmp(pTab
27384 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a 4e 61 6d 65 2c  ->aCol[j].zName,
27385 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e   pChanges->a[i].
27386 7a 4e 61 6d 65 29 3d 3d 30 20 29 7b 0a 20 20 20  zName)==0 ){.   
27387 20 20 20 20 20 69 66 28 20 6a 3d 3d 70 54 61 62       if( j==pTab
27388 2d 3e 69 50 4b 65 79 20 29 7b 0a 20 20 20 20 20  ->iPKey ){.     
27389 20 20 20 20 20 63 68 6e 67 52 6f 77 69 64 20 3d       chngRowid =
2738a 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 70 52   1;.          pR
2738b 6f 77 69 64 45 78 70 72 20 3d 20 70 43 68 61 6e  owidExpr = pChan
2738c 67 65 73 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  ges->a[i].pExpr;
2738d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2738e 20 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 69 3b     aXRef[j] = i;
2738f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
27390 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
27391 20 20 69 66 28 20 6a 3e 3d 70 54 61 62 2d 3e 6e    if( j>=pTab->n
27392 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20 69 66 28  Col ){.      if(
27393 20 73 71 6c 69 74 65 33 49 73 52 6f 77 69 64 28   sqlite3IsRowid(
27394 70 43 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a  pChanges->a[i].z
27395 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 20 20 20  Name) ){.       
27396 20 63 68 6e 67 52 6f 77 69 64 20 3d 20 31 3b 0a   chngRowid = 1;.
27397 20 20 20 20 20 20 20 20 70 52 6f 77 69 64 45 78          pRowidEx
27398 70 72 20 3d 20 70 43 68 61 6e 67 65 73 2d 3e 61  pr = pChanges->a
27399 5b 69 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20  [i].pExpr;.     
2739a 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2739b 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
2739c 70 50 61 72 73 65 2c 20 22 6e 6f 20 73 75 63 68  pParse, "no such
2739d 20 63 6f 6c 75 6d 6e 3a 20 25 73 22 2c 20 70 43   column: %s", pC
2739e 68 61 6e 67 65 73 2d 3e 61 5b 69 5d 2e 7a 4e 61  hanges->a[i].zNa
2739f 6d 65 29 3b 0a 20 20 20 20 20 20 20 20 67 6f 74  me);.        got
273a0 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
273a1 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
273a2 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
273a3 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
273a4 4e 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 69 6e  N.    {.      in
273a5 74 20 72 63 3b 0a 20 20 20 20 20 20 72 63 20 3d  t rc;.      rc =
273a6 20 73 71 6c 69 74 65 33 41 75 74 68 43 68 65 63   sqlite3AuthChec
273a7 6b 28 70 50 61 72 73 65 2c 20 53 51 4c 49 54 45  k(pParse, SQLITE
273a8 5f 55 50 44 41 54 45 2c 20 70 54 61 62 2d 3e 7a  _UPDATE, pTab->z
273a9 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20  Name,.          
273aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
273ab 20 70 54 61 62 2d 3e 61 43 6f 6c 5b 6a 5d 2e 7a   pTab->aCol[j].z
273ac 4e 61 6d 65 2c 20 64 62 2d 3e 61 44 62 5b 69 44  Name, db->aDb[iD
273ad 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20  b].zName);.     
273ae 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
273af 44 45 4e 59 20 29 7b 0a 20 20 20 20 20 20 20 20  DENY ){.        
273b0 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
273b1 6e 75 70 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  nup;.      }else
273b2 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
273b3 49 47 4e 4f 52 45 20 29 7b 0a 20 20 20 20 20 20  IGNORE ){.      
273b4 20 20 61 58 52 65 66 5b 6a 5d 20 3d 20 2d 31 3b    aXRef[j] = -1;
273b5 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 23  .      }.    }.#
273b6 65 6e 64 69 66 0a 20 20 7d 0a 0a 20 20 2f 2a 20  endif.  }..  /* 
273b7 41 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79 20  Allocate memory 
273b8 66 6f 72 20 74 68 65 20 61 72 72 61 79 20 61 52  for the array aR
273b9 65 67 49 64 78 5b 5d 2e 20 20 54 68 65 72 65 20  egIdx[].  There 
273ba 69 73 20 6f 6e 65 20 65 6e 74 72 79 20 69 6e 20  is one entry in 
273bb 74 68 65 0a 20 20 2a 2a 20 61 72 72 61 79 20 66  the.  ** array f
273bc 6f 72 20 65 61 63 68 20 69 6e 64 65 78 20 61 73  or each index as
273bd 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74 61  sociated with ta
273be 62 6c 65 20 62 65 69 6e 67 20 75 70 64 61 74 65  ble being update
273bf 64 2e 20 20 46 69 6c 6c 20 69 6e 0a 20 20 2a 2a  d.  Fill in.  **
273c0 20 74 68 65 20 76 61 6c 75 65 20 77 69 74 68 20   the value with 
273c1 61 20 72 65 67 69 73 74 65 72 20 6e 75 6d 62 65  a register numbe
273c2 72 20 66 6f 72 20 69 6e 64 69 63 65 73 20 74 68  r for indices th
273c3 61 74 20 61 72 65 20 74 6f 20 62 65 20 75 73 65  at are to be use
273c4 64 0a 20 20 2a 2a 20 61 6e 64 20 77 69 74 68 20  d.  ** and with 
273c5 7a 65 72 6f 20 66 6f 72 20 75 6e 75 73 65 64 20  zero for unused 
273c6 69 6e 64 69 63 65 73 2e 0a 20 20 2a 2f 0a 20 20  indices..  */.  
273c7 66 6f 72 28 6e 49 64 78 3d 30 2c 20 70 49 64 78  for(nIdx=0, pIdx
273c8 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70  =pTab->pIndex; p
273c9 49 64 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e  Idx; pIdx=pIdx->
273ca 70 4e 65 78 74 2c 20 6e 49 64 78 2b 2b 29 7b 7d  pNext, nIdx++){}
273cb 0a 20 20 69 66 28 20 6e 49 64 78 3e 30 20 29 7b  .  if( nIdx>0 ){
273cc 0a 20 20 20 20 61 52 65 67 49 64 78 20 3d 20 73  .    aRegIdx = s
273cd 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61  qlite3DbMallocRa
273ce 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 49 6e 64  w(db, sizeof(Ind
273cf 65 78 2a 29 20 2a 20 6e 49 64 78 20 29 3b 0a 20  ex*) * nIdx );. 
273d0 20 20 20 69 66 28 20 61 52 65 67 49 64 78 3d 3d     if( aRegIdx==
273d1 30 20 29 20 67 6f 74 6f 20 75 70 64 61 74 65 5f  0 ) goto update_
273d2 63 6c 65 61 6e 75 70 3b 0a 20 20 7d 0a 20 20 66  cleanup;.  }.  f
273d3 6f 72 28 6a 3d 30 2c 20 70 49 64 78 3d 70 54 61  or(j=0, pIdx=pTa
273d4 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
273d5 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
273d6 74 2c 20 6a 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  t, j++){.    int
273d7 20 72 65 67 3b 0a 20 20 20 20 69 66 28 20 63 68   reg;.    if( ch
273d8 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20  ngRowid ){.     
273d9 20 72 65 67 20 3d 20 2b 2b 70 50 61 72 73 65 2d   reg = ++pParse-
273da 3e 6e 4d 65 6d 3b 0a 20 20 20 20 7d 65 6c 73 65  >nMem;.    }else
273db 7b 0a 20 20 20 20 20 20 72 65 67 20 3d 20 30 3b  {.      reg = 0;
273dc 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  .      for(i=0; 
273dd 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b  i<pIdx->nColumn;
273de 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 69   i++){.        i
273df 66 28 20 61 58 52 65 66 5b 70 49 64 78 2d 3e 61  f( aXRef[pIdx->a
273e0 69 43 6f 6c 75 6d 6e 5b 69 5d 5d 3e 3d 30 20 29  iColumn[i]]>=0 )
273e1 7b 0a 20 20 20 20 20 20 20 20 20 20 72 65 67 20  {.          reg 
273e2 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
273e3 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72 65 61  ;.          brea
273e4 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  k;.        }.   
273e5 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61     }.    }.    a
273e6 52 65 67 49 64 78 5b 6a 5d 20 3d 20 72 65 67 3b  RegIdx[j] = reg;
273e7 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .  }..  /* Alloc
273e8 61 74 65 20 61 20 62 6c 6f 63 6b 20 6f 66 20 72  ate a block of r
273e9 65 67 69 73 74 65 72 20 75 73 65 64 20 74 6f 20  egister used to 
273ea 73 74 6f 72 65 20 74 68 65 20 63 68 61 6e 67 65  store the change
273eb 20 72 65 63 6f 72 64 0a 20 20 2a 2a 20 73 65 6e   record.  ** sen
273ec 74 20 74 6f 20 73 71 6c 69 74 65 33 47 65 6e 65  t to sqlite3Gene
273ed 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
273ee 65 63 6b 73 28 29 2e 20 20 54 68 65 72 65 20 61  ecks().  There a
273ef 72 65 20 65 69 74 68 65 72 0a 20 20 2a 2a 20 6f  re either.  ** o
273f0 6e 65 20 6f 72 20 74 77 6f 20 72 65 67 69 73 74  ne or two regist
273f1 65 72 73 20 66 6f 72 20 68 6f 6c 64 69 6e 67 20  ers for holding 
273f2 74 68 65 20 72 6f 77 69 64 2e 20 20 4f 6e 65 20  the rowid.  One 
273f3 72 6f 77 69 64 20 72 65 67 69 73 74 65 72 0a 20  rowid register. 
273f4 20 2a 2a 20 69 73 20 75 73 65 64 20 69 66 20 63   ** is used if c
273f5 68 6e 67 52 6f 77 69 64 20 69 73 20 66 61 6c 73  hngRowid is fals
273f6 65 20 61 6e 64 20 74 77 6f 20 61 72 65 20 75 73  e and two are us
273f7 65 64 20 69 66 20 63 68 6e 67 52 6f 77 69 64 20  ed if chngRowid 
273f8 69 73 0a 20 20 2a 2a 20 74 72 75 65 2e 20 20 46  is.  ** true.  F
273f9 6f 6c 6c 6f 77 69 6e 67 20 74 68 65 73 65 20 61  ollowing these a
273fa 72 65 20 70 54 61 62 2d 3e 6e 43 6f 6c 20 72 65  re pTab->nCol re
273fb 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20 63  gister holding c
273fc 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 64 61 74 61 2e  olumn.  ** data.
273fd 0a 20 20 2a 2f 0a 20 20 72 65 67 4f 6c 64 52 6f  .  */.  regOldRo
273fe 77 69 64 20 3d 20 72 65 67 4e 65 77 52 6f 77 69  wid = regNewRowi
273ff 64 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  d = pParse->nMem
27400 20 2b 20 31 3b 0a 20 20 70 50 61 72 73 65 2d 3e   + 1;.  pParse->
27401 6e 4d 65 6d 20 2b 3d 20 70 54 61 62 2d 3e 6e 43  nMem += pTab->nC
27402 6f 6c 20 2b 20 31 3b 0a 20 20 69 66 28 20 63 68  ol + 1;.  if( ch
27403 6e 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 72  ngRowid ){.    r
27404 65 67 4e 65 77 52 6f 77 69 64 2b 2b 3b 0a 20 20  egNewRowid++;.  
27405 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 2b 2b    pParse->nMem++
27406 3b 0a 20 20 7d 0a 20 20 72 65 67 44 61 74 61 20  ;.  }.  regData 
27407 3d 20 72 65 67 4e 65 77 52 6f 77 69 64 2b 31 3b  = regNewRowid+1;
27408 0a 20 0a 0a 20 20 2f 2a 20 42 65 67 69 6e 20 67  . ..  /* Begin g
27409 65 6e 65 72 61 74 69 6e 67 20 63 6f 64 65 2e 0a  enerating code..
2740a 20 20 2a 2f 0a 20 20 76 20 3d 20 73 71 6c 69 74    */.  v = sqlit
2740b 65 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65  e3GetVdbe(pParse
2740c 29 3b 0a 20 20 69 66 28 20 76 3d 3d 30 20 29 20  );.  if( v==0 ) 
2740d 67 6f 74 6f 20 75 70 64 61 74 65 5f 63 6c 65 61  goto update_clea
2740e 6e 75 70 3b 0a 20 20 69 66 28 20 70 50 61 72 73  nup;.  if( pPars
2740f 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 20 73  e->nested==0 ) s
27410 71 6c 69 74 65 33 56 64 62 65 43 6f 75 6e 74 43  qlite3VdbeCountC
27411 68 61 6e 67 65 73 28 76 29 3b 0a 20 20 73 71 6c  hanges(v);.  sql
27412 69 74 65 33 42 65 67 69 6e 57 72 69 74 65 4f 70  ite3BeginWriteOp
27413 65 72 61 74 69 6f 6e 28 70 50 61 72 73 65 2c 20  eration(pParse, 
27414 31 2c 20 69 44 62 29 3b 0a 0a 23 69 66 6e 64 65  1, iDb);..#ifnde
27415 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49  f SQLITE_OMIT_VI
27416 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 2f 2a 20  RTUALTABLE.  /* 
27417 56 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 6d  Virtual tables m
27418 75 73 74 20 62 65 20 68 61 6e 64 6c 65 64 20 73  ust be handled s
27419 65 70 61 72 61 74 65 6c 79 20 2a 2f 0a 20 20 69  eparately */.  i
2741a 66 28 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  f( IsVirtual(pTa
2741b 62 29 20 29 7b 0a 20 20 20 20 75 70 64 61 74 65  b) ){.    update
2741c 56 69 72 74 75 61 6c 54 61 62 6c 65 28 70 50 61  VirtualTable(pPa
2741d 72 73 65 2c 20 70 54 61 62 4c 69 73 74 2c 20 70  rse, pTabList, p
2741e 54 61 62 2c 20 70 43 68 61 6e 67 65 73 2c 20 70  Tab, pChanges, p
2741f 52 6f 77 69 64 45 78 70 72 2c 20 61 58 52 65 66  RowidExpr, aXRef
27420 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
27421 20 20 20 20 20 20 20 20 20 70 57 68 65 72 65 29           pWhere)
27422 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d 20 30  ;.    pWhere = 0
27423 3b 0a 20 20 20 20 70 54 61 62 4c 69 73 74 20 3d  ;.    pTabList =
27424 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20 75 70 64   0;.    goto upd
27425 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 7d  ate_cleanup;.  }
27426 0a 23 65 6e 64 69 66 0a 0a 20 20 2f 2a 20 53 74  .#endif..  /* St
27427 61 72 74 20 74 68 65 20 76 69 65 77 20 63 6f 6e  art the view con
27428 74 65 78 74 0a 20 20 2a 2f 0a 20 20 69 66 28 20  text.  */.  if( 
27429 69 73 56 69 65 77 20 29 7b 0a 20 20 20 20 73 71  isView ){.    sq
2742a 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78 74  lite3AuthContext
2742b 50 75 73 68 28 70 50 61 72 73 65 2c 20 26 73 43  Push(pParse, &sC
2742c 6f 6e 74 65 78 74 2c 20 70 54 61 62 2d 3e 7a 4e  ontext, pTab->zN
2742d 61 6d 65 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ame);.  }..  /* 
2742e 47 65 6e 65 72 61 74 65 20 74 68 65 20 63 6f 64  Generate the cod
2742f 65 20 66 6f 72 20 74 72 69 67 67 65 72 73 2e 0a  e for triggers..
27430 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67 67    */.  if( trigg
27431 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20 20  ers_exist ){.   
27432 20 69 6e 74 20 69 47 6f 74 6f 3b 0a 0a 20 20 20   int iGoto;..   
27433 20 2f 2a 20 43 72 65 61 74 65 20 70 73 65 75 64   /* Create pseud
27434 6f 2d 74 61 62 6c 65 73 20 66 6f 72 20 4e 45 57  o-tables for NEW
27435 20 61 6e 64 20 4f 4c 44 0a 20 20 20 20 2a 2f 0a   and OLD.    */.
27436 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27437 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e  ddOp2(v, OP_SetN
27438 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 54  umColumns, 0, pT
27439 61 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73  ab->nCol);.    s
2743a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2743b 28 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64  (v, OP_OpenPseud
2743c 6f 2c 20 6f 6c 64 49 64 78 2c 20 30 29 3b 0a 20  o, oldIdx, 0);. 
2743d 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2743e 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65 74 4e 75  dOp2(v, OP_SetNu
2743f 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20 70 54 61  mColumns, 0, pTa
27440 62 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 73 71  b->nCol);.    sq
27441 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
27442 76 2c 20 4f 50 5f 4f 70 65 6e 50 73 65 75 64 6f  v, OP_OpenPseudo
27443 2c 20 6e 65 77 49 64 78 2c 20 30 29 3b 0a 0a 20  , newIdx, 0);.. 
27444 20 20 20 69 47 6f 74 6f 20 3d 20 73 71 6c 69 74     iGoto = sqlit
27445 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
27446 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a  OP_Goto, 0, 0);.
27447 20 20 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74      addr = sqlit
27448 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
27449 76 29 3b 0a 20 20 20 20 69 42 65 67 69 6e 42 65  v);.    iBeginBe
2744a 66 6f 72 65 54 72 69 67 67 65 72 20 3d 20 73 71  foreTrigger = sq
2744b 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
2744c 41 64 64 72 28 76 29 3b 0a 20 20 20 20 69 66 28  Addr(v);.    if(
2744d 20 73 71 6c 69 74 65 33 43 6f 64 65 52 6f 77 54   sqlite3CodeRowT
2744e 72 69 67 67 65 72 28 70 50 61 72 73 65 2c 20 54  rigger(pParse, T
2744f 4b 5f 55 50 44 41 54 45 2c 20 70 43 68 61 6e 67  K_UPDATE, pChang
27450 65 73 2c 20 54 52 49 47 47 45 52 5f 42 45 46 4f  es, TRIGGER_BEFO
27451 52 45 2c 20 70 54 61 62 2c 0a 20 20 20 20 20 20  RE, pTab,.      
27452 20 20 20 20 6e 65 77 49 64 78 2c 20 6f 6c 64 49      newIdx, oldI
27453 64 78 2c 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64  dx, onError, add
27454 72 2c 20 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b  r, &old_col_mask
27455 2c 20 26 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 29  , &new_col_mask)
27456 20 29 7b 0a 20 20 20 20 20 20 67 6f 74 6f 20 75   ){.      goto u
27457 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20  pdate_cleanup;. 
27458 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 42 65 66     }.    iEndBef
27459 6f 72 65 54 72 69 67 67 65 72 20 3d 20 73 71 6c  oreTrigger = sql
2745a 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
2745b 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c 20 30 29  , OP_Goto, 0, 0)
2745c 3b 0a 20 20 20 20 69 42 65 67 69 6e 41 66 74 65  ;.    iBeginAfte
2745d 72 54 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74  rTrigger = sqlit
2745e 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
2745f 72 28 76 29 3b 0a 20 20 20 20 69 66 28 20 73 71  r(v);.    if( sq
27460 6c 69 74 65 33 43 6f 64 65 52 6f 77 54 72 69 67  lite3CodeRowTrig
27461 67 65 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 55  ger(pParse, TK_U
27462 50 44 41 54 45 2c 20 70 43 68 61 6e 67 65 73 2c  PDATE, pChanges,
27463 20 54 52 49 47 47 45 52 5f 41 46 54 45 52 2c 20   TRIGGER_AFTER, 
27464 70 54 61 62 2c 20 0a 20 20 20 20 20 20 20 20 20  pTab, .         
27465 20 6e 65 77 49 64 78 2c 20 6f 6c 64 49 64 78 2c   newIdx, oldIdx,
27466 20 6f 6e 45 72 72 6f 72 2c 20 61 64 64 72 2c 20   onError, addr, 
27467 26 6f 6c 64 5f 63 6f 6c 5f 6d 61 73 6b 2c 20 26  &old_col_mask, &
27468 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 29 20 29 7b  new_col_mask) ){
27469 0a 20 20 20 20 20 20 67 6f 74 6f 20 75 70 64 61  .      goto upda
2746a 74 65 5f 63 6c 65 61 6e 75 70 3b 0a 20 20 20 20  te_cleanup;.    
2746b 7d 0a 20 20 20 20 69 45 6e 64 41 66 74 65 72 54  }.    iEndAfterT
2746c 72 69 67 67 65 72 20 3d 20 73 71 6c 69 74 65 33  rigger = sqlite3
2746d 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
2746e 5f 47 6f 74 6f 2c 20 30 2c 20 30 29 3b 0a 20 20  _Goto, 0, 0);.  
2746f 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
27470 70 48 65 72 65 28 76 2c 20 69 47 6f 74 6f 29 3b  pHere(v, iGoto);
27471 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 77 65  .  }..  /* If we
27472 20 61 72 65 20 74 72 79 69 6e 67 20 74 6f 20 75   are trying to u
27473 70 64 61 74 65 20 61 20 76 69 65 77 2c 20 72 65  pdate a view, re
27474 61 6c 69 7a 65 20 74 68 61 74 20 76 69 65 77 20  alize that view 
27475 69 6e 74 6f 0a 20 20 2a 2a 20 61 20 65 70 68 65  into.  ** a ephe
27476 6d 65 72 61 6c 20 74 61 62 6c 65 2e 0a 20 20 2a  meral table..  *
27477 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
27478 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 45 57 29  QLITE_OMIT_VIEW)
27479 20 26 26 20 21 64 65 66 69 6e 65 64 28 53 51 4c   && !defined(SQL
2747a 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45 52  ITE_OMIT_TRIGGER
2747b 29 0a 20 20 69 66 28 20 69 73 56 69 65 77 20 29  ).  if( isView )
2747c 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 4d 61 74  {.    sqlite3Mat
2747d 65 72 69 61 6c 69 7a 65 56 69 65 77 28 70 50 61  erializeView(pPa
2747e 72 73 65 2c 20 70 54 61 62 2c 20 70 57 68 65 72  rse, pTab, pWher
2747f 65 2c 20 69 43 75 72 29 3b 0a 20 20 7d 0a 23 65  e, iCur);.  }.#e
27480 6e 64 69 66 0a 0a 20 20 2f 2a 20 52 65 73 6f 6c  ndif..  /* Resol
27481 76 65 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e 61  ve the column na
27482 6d 65 73 20 69 6e 20 61 6c 6c 20 74 68 65 20 65  mes in all the e
27483 78 70 72 65 73 73 69 6f 6e 73 20 69 6e 20 74 68  xpressions in th
27484 65 0a 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61  e.  ** WHERE cla
27485 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  use..  */.  if( 
27486 73 71 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78  sqlite3ResolveEx
27487 70 72 4e 61 6d 65 73 28 26 73 4e 43 2c 20 70 57  prNames(&sNC, pW
27488 68 65 72 65 29 20 29 7b 0a 20 20 20 20 67 6f 74  here) ){.    got
27489 6f 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70  o update_cleanup
2748a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 42 65 67 69  ;.  }..  /* Begi
2748b 6e 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  n the database s
2748c 63 61 6e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  can.  */.  sqlit
2748d 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
2748e 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65 67 4f  OP_Null, 0, regO
2748f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 70 57 49 6e  ldRowid);.  pWIn
27490 66 6f 20 3d 20 73 71 6c 69 74 65 33 57 68 65 72  fo = sqlite3Wher
27491 65 42 65 67 69 6e 28 70 50 61 72 73 65 2c 20 70  eBegin(pParse, p
27492 54 61 62 4c 69 73 74 2c 20 70 57 68 65 72 65 2c  TabList, pWhere,
27493 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20   0,.            
27494 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27495 20 57 48 45 52 45 5f 4f 4e 45 50 41 53 53 5f 44   WHERE_ONEPASS_D
27496 45 53 49 52 45 44 29 3b 0a 20 20 69 66 28 20 70  ESIRED);.  if( p
27497 57 49 6e 66 6f 3d 3d 30 20 29 20 67 6f 74 6f 20  WInfo==0 ) goto 
27498 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b 0a  update_cleanup;.
27499 20 20 6f 6b 4f 6e 65 50 61 73 73 20 3d 20 70 57    okOnePass = pW
2749a 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 3b  Info->okOnePass;
2749b 0a 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20  ..  /* Remember 
2749c 74 68 65 20 72 6f 77 69 64 20 6f 66 20 65 76 65  the rowid of eve
2749d 72 79 20 69 74 65 6d 20 74 6f 20 62 65 20 75 70  ry item to be up
2749e 64 61 74 65 64 2e 0a 20 20 2a 2f 0a 20 20 73 71  dated..  */.  sq
2749f 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
274a0 76 2c 20 49 73 56 69 72 74 75 61 6c 28 70 54 61  v, IsVirtual(pTa
274a1 62 29 3f 4f 50 5f 56 52 6f 77 69 64 3a 4f 50 5f  b)?OP_VRowid:OP_
274a2 52 6f 77 69 64 2c 20 69 43 75 72 2c 20 72 65 67  Rowid, iCur, reg
274a3 4f 6c 64 52 6f 77 69 64 29 3b 0a 20 20 69 66 28  OldRowid);.  if(
274a4 20 21 6f 6b 4f 6e 65 50 61 73 73 20 29 20 73 71   !okOnePass ) sq
274a5 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
274a6 76 2c 20 4f 50 5f 46 69 66 6f 57 72 69 74 65 2c  v, OP_FifoWrite,
274a7 20 72 65 67 4f 6c 64 52 6f 77 69 64 2c 20 30 29   regOldRowid, 0)
274a8 3b 0a 0a 20 20 2f 2a 20 45 6e 64 20 74 68 65 20  ;..  /* End the 
274a9 64 61 74 61 62 61 73 65 20 73 63 61 6e 20 6c 6f  database scan lo
274aa 6f 70 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74  op..  */.  sqlit
274ab 65 33 57 68 65 72 65 45 6e 64 28 70 57 49 6e 66  e3WhereEnd(pWInf
274ac 6f 29 3b 0a 0a 20 20 2f 2a 20 49 6e 69 74 69 61  o);..  /* Initia
274ad 6c 69 7a 65 20 74 68 65 20 63 6f 75 6e 74 20 6f  lize the count o
274ae 66 20 75 70 64 61 74 65 64 20 72 6f 77 73 0a 20  f updated rows. 
274af 20 2a 2f 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c   */.  if( db->fl
274b0 61 67 73 20 26 20 53 51 4c 49 54 45 5f 43 6f 75  ags & SQLITE_Cou
274b1 6e 74 52 6f 77 73 20 26 26 20 21 70 50 61 72 73  ntRows && !pPars
274b2 65 2d 3e 74 72 69 67 53 74 61 63 6b 20 29 7b 0a  e->trigStack ){.
274b3 20 20 20 20 72 65 67 52 6f 77 43 6f 75 6e 74 20      regRowCount 
274b4 3d 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d  = ++pParse->nMem
274b5 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
274b6 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e  eAddOp2(v, OP_In
274b7 74 65 67 65 72 2c 20 30 2c 20 72 65 67 52 6f 77  teger, 0, regRow
274b8 43 6f 75 6e 74 29 3b 0a 20 20 7d 0a 0a 20 20 69  Count);.  }..  i
274b9 66 28 20 21 69 73 56 69 65 77 20 26 26 20 21 49  f( !isView && !I
274ba 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20 29  sVirtual(pTab) )
274bb 7b 0a 20 20 20 20 2f 2a 20 0a 20 20 20 20 2a 2a  {.    /* .    **
274bc 20 4f 70 65 6e 20 65 76 65 72 79 20 69 6e 64 65   Open every inde
274bd 78 20 74 68 61 74 20 6e 65 65 64 73 20 75 70 64  x that needs upd
274be 61 74 69 6e 67 2e 20 20 4e 6f 74 65 20 74 68 61  ating.  Note tha
274bf 74 20 69 66 20 61 6e 79 0a 20 20 20 20 2a 2a 20  t if any.    ** 
274c0 69 6e 64 65 78 20 63 6f 75 6c 64 20 70 6f 74 65  index could pote
274c1 6e 74 69 61 6c 6c 79 20 69 6e 76 6f 6b 65 20 61  ntially invoke a
274c2 20 52 45 50 4c 41 43 45 20 63 6f 6e 66 6c 69 63   REPLACE conflic
274c3 74 20 72 65 73 6f 6c 75 74 69 6f 6e 20 0a 20 20  t resolution .  
274c4 20 20 2a 2a 20 61 63 74 69 6f 6e 2c 20 74 68 65    ** action, the
274c5 6e 20 77 65 20 6e 65 65 64 20 74 6f 20 6f 70 65  n we need to ope
274c6 6e 20 61 6c 6c 20 69 6e 64 69 63 65 73 20 62 65  n all indices be
274c7 63 61 75 73 65 20 77 65 20 6d 69 67 68 74 20 6e  cause we might n
274c8 65 65 64 0a 20 20 20 20 2a 2a 20 74 6f 20 62 65  eed.    ** to be
274c9 20 64 65 6c 65 74 69 6e 67 20 73 6f 6d 65 20 72   deleting some r
274ca 65 63 6f 72 64 73 2e 0a 20 20 20 20 2a 2f 0a 20  ecords..    */. 
274cb 20 20 20 69 66 28 20 21 6f 6b 4f 6e 65 50 61 73     if( !okOnePas
274cc 73 20 29 20 73 71 6c 69 74 65 33 4f 70 65 6e 54  s ) sqlite3OpenT
274cd 61 62 6c 65 28 70 50 61 72 73 65 2c 20 69 43 75  able(pParse, iCu
274ce 72 2c 20 69 44 62 2c 20 70 54 61 62 2c 20 4f 50  r, iDb, pTab, OP
274cf 5f 4f 70 65 6e 57 72 69 74 65 29 3b 20 0a 20 20  _OpenWrite); .  
274d0 20 20 69 66 28 20 6f 6e 45 72 72 6f 72 3d 3d 4f    if( onError==O
274d1 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20  E_Replace ){.   
274d2 20 20 20 6f 70 65 6e 41 6c 6c 20 3d 20 31 3b 0a     openAll = 1;.
274d3 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
274d4 20 6f 70 65 6e 41 6c 6c 20 3d 20 30 3b 0a 20 20   openAll = 0;.  
274d5 20 20 20 20 66 6f 72 28 70 49 64 78 3d 70 54 61      for(pIdx=pTa
274d6 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64 78 3b  b->pIndex; pIdx;
274d7 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e 65 78   pIdx=pIdx->pNex
274d8 74 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  t){.        if( 
274d9 70 49 64 78 2d 3e 6f 6e 45 72 72 6f 72 3d 3d 4f  pIdx->onError==O
274da 45 5f 52 65 70 6c 61 63 65 20 29 7b 0a 20 20 20  E_Replace ){.   
274db 20 20 20 20 20 20 20 6f 70 65 6e 41 6c 6c 20 3d         openAll =
274dc 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 62 72   1;.          br
274dd 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  eak;.        }. 
274de 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
274df 20 66 6f 72 28 69 3d 30 2c 20 70 49 64 78 3d 70   for(i=0, pIdx=p
274e0 54 61 62 2d 3e 70 49 6e 64 65 78 3b 20 70 49 64  Tab->pIndex; pId
274e1 78 3b 20 70 49 64 78 3d 70 49 64 78 2d 3e 70 4e  x; pIdx=pIdx->pN
274e2 65 78 74 2c 20 69 2b 2b 29 7b 0a 20 20 20 20 20  ext, i++){.     
274e3 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20 7c 7c 20   if( openAll || 
274e4 61 52 65 67 49 64 78 5b 69 5d 3e 30 20 29 7b 0a  aRegIdx[i]>0 ){.
274e5 20 20 20 20 20 20 20 20 4b 65 79 49 6e 66 6f 20          KeyInfo 
274e6 2a 70 4b 65 79 20 3d 20 73 71 6c 69 74 65 33 49  *pKey = sqlite3I
274e7 6e 64 65 78 4b 65 79 69 6e 66 6f 28 70 50 61 72  ndexKeyinfo(pPar
274e8 73 65 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  se, pIdx);.     
274e9 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
274ea 64 4f 70 34 28 76 2c 20 4f 50 5f 4f 70 65 6e 57  dOp4(v, OP_OpenW
274eb 72 69 74 65 2c 20 69 43 75 72 2b 69 2b 31 2c 20  rite, iCur+i+1, 
274ec 70 49 64 78 2d 3e 74 6e 75 6d 2c 20 69 44 62 2c  pIdx->tnum, iDb,
274ed 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
274ee 20 20 20 20 20 20 20 20 28 63 68 61 72 2a 29 70          (char*)p
274ef 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46 4f 5f  Key, P4_KEYINFO_
274f0 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20 20 20  HANDOFF);.      
274f1 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
274f2 2d 3e 6e 54 61 62 3e 69 43 75 72 2b 69 2b 31 20  ->nTab>iCur+i+1 
274f3 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
274f4 0a 20 20 7d 0a 20 20 0a 20 20 2f 2a 20 4a 75 6d  .  }.  .  /* Jum
274f5 70 20 62 61 63 6b 20 74 6f 20 74 68 69 73 20 70  p back to this p
274f6 6f 69 6e 74 20 69 66 20 61 20 74 72 69 67 67 65  oint if a trigge
274f7 72 20 65 6e 63 6f 75 6e 74 65 72 73 20 61 6e 20  r encounters an 
274f8 49 47 4e 4f 52 45 20 63 6f 6e 73 74 72 61 69 6e  IGNORE constrain
274f9 74 2e 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67  t. */.  if( trig
274fa 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20  gers_exist ){.  
274fb 20 20 73 71 6c 69 74 65 33 56 64 62 65 52 65 73    sqlite3VdbeRes
274fc 6f 6c 76 65 4c 61 62 65 6c 28 76 2c 20 61 64 64  olveLabel(v, add
274fd 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 54 6f  r);.  }..  /* To
274fe 70 20 6f 66 20 74 68 65 20 75 70 64 61 74 65 20  p of the update 
274ff 6c 6f 6f 70 20 2a 2f 0a 20 20 69 66 28 20 6f 6b  loop */.  if( ok
27500 4f 6e 65 50 61 73 73 20 29 7b 0a 20 20 20 20 69  OnePass ){.    i
27501 6e 74 20 61 31 20 3d 20 73 71 6c 69 74 65 33 56  nt a1 = sqlite3V
27502 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50 5f  dbeAddOp1(v, OP_
27503 4e 6f 74 4e 75 6c 6c 2c 20 72 65 67 4f 6c 64 52  NotNull, regOldR
27504 6f 77 69 64 29 3b 0a 20 20 20 20 61 64 64 72 20  owid);.    addr 
27505 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
27506 4f 70 30 28 76 2c 20 4f 50 5f 47 6f 74 6f 29 3b  Op0(v, OP_Goto);
27507 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27508 4a 75 6d 70 48 65 72 65 28 76 2c 20 61 31 29 3b  JumpHere(v, a1);
27509 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 64  .  }else{.    ad
2750a 64 72 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65  dr = sqlite3Vdbe
2750b 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 46 69 66  AddOp2(v, OP_Fif
2750c 6f 52 65 61 64 2c 20 72 65 67 4f 6c 64 52 6f 77  oRead, regOldRow
2750d 69 64 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 69  id, 0);.  }..  i
2750e 66 28 20 74 72 69 67 67 65 72 73 5f 65 78 69 73  f( triggers_exis
2750f 74 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 65 67  t ){.    int reg
27510 52 6f 77 69 64 3b 0a 20 20 20 20 69 6e 74 20 72  Rowid;.    int r
27511 65 67 52 6f 77 3b 0a 20 20 20 20 69 6e 74 20 72  egRow;.    int r
27512 65 67 43 6f 6c 73 3b 0a 0a 20 20 20 20 2f 2a 20  egCols;..    /* 
27513 4d 61 6b 65 20 63 75 72 73 6f 72 20 69 43 75 72  Make cursor iCur
27514 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 20 72 65   point to the re
27515 63 6f 72 64 20 74 68 61 74 20 69 73 20 62 65 69  cord that is bei
27516 6e 67 20 75 70 64 61 74 65 64 2e 0a 20 20 20 20  ng updated..    
27517 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  */.    sqlite3Vd
27518 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 4e  beAddOp3(v, OP_N
27519 6f 74 45 78 69 73 74 73 2c 20 69 43 75 72 2c 20  otExists, iCur, 
2751a 61 64 64 72 2c 20 72 65 67 4f 6c 64 52 6f 77 69  addr, regOldRowi
2751b 64 29 3b 0a 0a 20 20 20 20 2f 2a 20 47 65 6e 65  d);..    /* Gene
2751c 72 61 74 65 20 74 68 65 20 4f 4c 44 20 74 61 62  rate the OLD tab
2751d 6c 65 0a 20 20 20 20 2a 2f 0a 20 20 20 20 72 65  le.    */.    re
2751e 67 52 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33  gRowid = sqlite3
2751f 47 65 74 54 65 6d 70 52 65 67 28 70 50 61 72 73  GetTempReg(pPars
27520 65 29 3b 0a 20 20 20 20 72 65 67 52 6f 77 20 3d  e);.    regRow =
27521 20 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52   sqlite3GetTempR
27522 65 67 28 70 50 61 72 73 65 29 3b 0a 20 20 20 20  eg(pParse);.    
27523 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27524 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
27525 43 75 72 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  Cur, regRowid);.
27526 20 20 20 20 69 66 28 20 21 6f 6c 64 5f 63 6f 6c      if( !old_col
27527 5f 6d 61 73 6b 20 29 7b 0a 20 20 20 20 20 20 73  _mask ){.      s
27528 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
27529 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20  (v, OP_Null, 0, 
2752a 72 65 67 52 6f 77 29 3b 0a 20 20 20 20 7d 65 6c  regRow);.    }el
2752b 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  se{.      sqlite
2752c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f  3VdbeAddOp2(v, O
2752d 50 5f 52 6f 77 44 61 74 61 2c 20 69 43 75 72 2c  P_RowData, iCur,
2752e 20 72 65 67 52 6f 77 29 3b 0a 20 20 20 20 7d 0a   regRow);.    }.
2752f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
27530 64 64 4f 70 33 28 76 2c 20 4f 50 5f 49 6e 73 65  ddOp3(v, OP_Inse
27531 72 74 2c 20 6f 6c 64 49 64 78 2c 20 72 65 67 52  rt, oldIdx, regR
27532 6f 77 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a 0a  ow, regRowid);..
27533 20 20 20 20 2f 2a 20 47 65 6e 65 72 61 74 65 20      /* Generate 
27534 74 68 65 20 4e 45 57 20 74 61 62 6c 65 0a 20 20  the NEW table.  
27535 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 63 68 6e    */.    if( chn
27536 67 52 6f 77 69 64 20 29 7b 0a 20 20 20 20 20 20  gRowid ){.      
27537 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 41  sqlite3ExprCodeA
27538 6e 64 43 61 63 68 65 28 70 50 61 72 73 65 2c 20  ndCache(pParse, 
27539 70 52 6f 77 69 64 45 78 70 72 2c 20 72 65 67 52  pRowidExpr, regR
2753a 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71 6c  owid);.      sql
2753b 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28 76  ite3VdbeAddOp1(v
2753c 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c 20  , OP_MustBeInt, 
2753d 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  regRowid);.    }
2753e 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c 69  else{.      sqli
2753f 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
27540 20 4f 50 5f 52 6f 77 69 64 2c 20 69 43 75 72 2c   OP_Rowid, iCur,
27541 20 72 65 67 52 6f 77 69 64 29 3b 0a 20 20 20 20   regRowid);.    
27542 7d 0a 20 20 20 20 72 65 67 43 6f 6c 73 20 3d 20  }.    regCols = 
27543 73 71 6c 69 74 65 33 47 65 74 54 65 6d 70 52 61  sqlite3GetTempRa
27544 6e 67 65 28 70 50 61 72 73 65 2c 20 70 54 61 62  nge(pParse, pTab
27545 2d 3e 6e 43 6f 6c 29 3b 0a 20 20 20 20 66 6f 72  ->nCol);.    for
27546 28 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43  (i=0; i<pTab->nC
27547 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ol; i++){.      
27548 69 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b  if( i==pTab->iPK
27549 65 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71  ey ){.        sq
2754a 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
2754b 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72  v, OP_Null, 0, r
2754c 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20 20 20  egCols+i);.     
2754d 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2754e 20 20 20 7d 0a 20 20 20 20 20 20 6a 20 3d 20 61     }.      j = a
2754f 58 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20 69  XRef[i];.      i
27550 66 28 20 6e 65 77 5f 63 6f 6c 5f 6d 61 73 6b 26  f( new_col_mask&
27551 28 28 75 33 32 29 31 3c 3c 69 29 20 7c 7c 20 6e  ((u32)1<<i) || n
27552 65 77 5f 63 6f 6c 5f 6d 61 73 6b 3d 3d 30 78 66  ew_col_mask==0xf
27553 66 66 66 66 66 66 66 20 29 7b 0a 20 20 20 20 20  fffffff ){.     
27554 20 20 20 69 66 28 20 6a 3c 30 20 29 7b 0a 20 20     if( j<0 ){.  
27555 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
27556 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
27557 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 2c 20 69 2c  Column, iCur, i,
27558 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a 20 20 20   regCols+i);.   
27559 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 43 6f         sqlite3Co
2755a 6c 75 6d 6e 44 65 66 61 75 6c 74 28 76 2c 20 70  lumnDefault(v, p
2755b 54 61 62 2c 20 69 29 3b 0a 20 20 20 20 20 20 20  Tab, i);.       
2755c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
2755d 20 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64    sqlite3ExprCod
2755e 65 41 6e 64 43 61 63 68 65 28 70 50 61 72 73 65  eAndCache(pParse
2755f 2c 20 70 43 68 61 6e 67 65 73 2d 3e 61 5b 6a 5d  , pChanges->a[j]
27560 2e 70 45 78 70 72 2c 20 72 65 67 43 6f 6c 73 2b  .pExpr, regCols+
27561 69 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  i);.        }.  
27562 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
27563 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27564 64 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c  dOp2(v, OP_Null,
27565 20 30 2c 20 72 65 67 43 6f 6c 73 2b 69 29 3b 0a   0, regCols+i);.
27566 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
27567 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27568 4f 70 33 28 76 2c 20 4f 50 5f 4d 61 6b 65 52 65  Op3(v, OP_MakeRe
27569 63 6f 72 64 2c 20 72 65 67 43 6f 6c 73 2c 20 70  cord, regCols, p
2756a 54 61 62 2d 3e 6e 43 6f 6c 2c 20 72 65 67 52 6f  Tab->nCol, regRo
2756b 77 29 3b 0a 20 20 20 20 69 66 28 20 21 69 73 56  w);.    if( !isV
2756c 69 65 77 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  iew ){.      sql
2756d 69 74 65 33 54 61 62 6c 65 41 66 66 69 6e 69 74  ite3TableAffinit
2756e 79 53 74 72 28 76 2c 20 70 54 61 62 29 3b 0a 20  yStr(v, pTab);. 
2756f 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
27570 43 61 63 68 65 41 66 66 69 6e 69 74 79 43 68 61  CacheAffinityCha
27571 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43  nge(pParse, regC
27572 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ols, pTab->nCol)
27573 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  ;.    }.    sqli
27574 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52 61  te3ReleaseTempRa
27575 6e 67 65 28 70 50 61 72 73 65 2c 20 72 65 67 43  nge(pParse, regC
27576 6f 6c 73 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 29  ols, pTab->nCol)
27577 3b 0a 20 20 20 20 2f 2a 20 69 66 28 20 70 50 61  ;.    /* if( pPa
27578 72 73 65 2d 3e 6e 45 72 72 20 29 20 67 6f 74 6f  rse->nErr ) goto
27579 20 75 70 64 61 74 65 5f 63 6c 65 61 6e 75 70 3b   update_cleanup;
2757a 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 56   */.    sqlite3V
2757b 64 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f  dbeAddOp3(v, OP_
2757c 49 6e 73 65 72 74 2c 20 6e 65 77 49 64 78 2c 20  Insert, newIdx, 
2757d 72 65 67 52 6f 77 2c 20 72 65 67 52 6f 77 69 64  regRow, regRowid
2757e 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 52 65  );.    sqlite3Re
2757f 6c 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61  leaseTempReg(pPa
27580 72 73 65 2c 20 72 65 67 52 6f 77 69 64 29 3b 0a  rse, regRowid);.
27581 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
27582 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
27583 2c 20 72 65 67 52 6f 77 29 3b 0a 0a 20 20 20 20  , regRow);..    
27584 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
27585 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20 30 2c  2(v, OP_Goto, 0,
27586 20 69 42 65 67 69 6e 42 65 66 6f 72 65 54 72 69   iBeginBeforeTri
27587 67 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69 74  gger);.    sqlit
27588 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76  e3VdbeJumpHere(v
27589 2c 20 69 45 6e 64 42 65 66 6f 72 65 54 72 69 67  , iEndBeforeTrig
2758a 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  ger);.  }..  if(
2758b 20 21 69 73 56 69 65 77 20 26 26 20 21 49 73 56   !isView && !IsV
2758c 69 72 74 75 61 6c 28 70 54 61 62 29 20 29 7b 0a  irtual(pTab) ){.
2758d 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 6f 76 65 72      /* Loop over
2758e 20 65 76 65 72 79 20 72 65 63 6f 72 64 20 74 68   every record th
2758f 61 74 20 6e 65 65 64 73 20 75 70 64 61 74 69 6e  at needs updatin
27590 67 2e 20 20 57 65 20 68 61 76 65 20 74 6f 20 6c  g.  We have to l
27591 6f 61 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6f  oad.    ** the o
27592 6c 64 20 64 61 74 61 20 66 6f 72 20 65 61 63 68  ld data for each
27593 20 72 65 63 6f 72 64 20 74 6f 20 62 65 20 75 70   record to be up
27594 64 61 74 65 64 20 62 65 63 61 75 73 65 20 73 6f  dated because so
27595 6d 65 20 63 6f 6c 75 6d 6e 73 0a 20 20 20 20 2a  me columns.    *
27596 2a 20 6d 69 67 68 74 20 6e 6f 74 20 63 68 61 6e  * might not chan
27597 67 65 20 61 6e 64 20 77 65 20 77 69 6c 6c 20 6e  ge and we will n
27598 65 65 64 20 74 6f 20 63 6f 70 79 20 74 68 65 20  eed to copy the 
27599 6f 6c 64 20 76 61 6c 75 65 2e 0a 20 20 20 20 2a  old value..    *
2759a 2a 20 41 6c 73 6f 2c 20 74 68 65 20 6f 6c 64 20  * Also, the old 
2759b 64 61 74 61 20 69 73 20 6e 65 65 64 65 64 20 74  data is needed t
2759c 6f 20 64 65 6c 65 74 65 20 74 68 65 20 6f 6c 64  o delete the old
2759d 20 69 6e 64 65 78 20 65 6e 74 72 69 65 73 2e 0a   index entries..
2759e 20 20 20 20 2a 2a 20 53 6f 20 6d 61 6b 65 20 74      ** So make t
2759f 68 65 20 63 75 72 73 6f 72 20 70 6f 69 6e 74 20  he cursor point 
275a0 61 74 20 74 68 65 20 6f 6c 64 20 72 65 63 6f 72  at the old recor
275a1 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 73 71  d..    */.    sq
275a2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
275a3 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c  v, OP_NotExists,
275a4 20 69 43 75 72 2c 20 61 64 64 72 2c 20 72 65 67   iCur, addr, reg
275a5 4f 6c 64 52 6f 77 69 64 29 3b 0a 0a 20 20 20 20  OldRowid);..    
275a6 2f 2a 20 49 66 20 74 68 65 20 72 65 63 6f 72 64  /* If the record
275a7 20 6e 75 6d 62 65 72 20 77 69 6c 6c 20 63 68 61   number will cha
275a8 6e 67 65 2c 20 70 75 73 68 20 74 68 65 20 72 65  nge, push the re
275a9 63 6f 72 64 20 6e 75 6d 62 65 72 20 61 73 20 69  cord number as i
275aa 74 0a 20 20 20 20 2a 2a 20 77 69 6c 6c 20 62 65  t.    ** will be
275ab 20 61 66 74 65 72 20 74 68 65 20 75 70 64 61 74   after the updat
275ac 65 2e 20 28 54 68 65 20 6f 6c 64 20 72 65 63 6f  e. (The old reco
275ad 72 64 20 6e 75 6d 62 65 72 20 69 73 20 63 75 72  rd number is cur
275ae 72 65 6e 74 6c 79 0a 20 20 20 20 2a 2a 20 6f 6e  rently.    ** on
275af 20 74 6f 70 20 6f 66 20 74 68 65 20 73 74 61 63   top of the stac
275b0 6b 2e 29 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  k.).    */.    i
275b1 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a  f( chngRowid ){.
275b2 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
275b3 72 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 52  rCode(pParse, pR
275b4 6f 77 69 64 45 78 70 72 2c 20 72 65 67 4e 65 77  owidExpr, regNew
275b5 52 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 73 71  Rowid);.      sq
275b6 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31 28  lite3VdbeAddOp1(
275b7 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
275b8 20 72 65 67 4e 65 77 52 6f 77 69 64 29 3b 0a 20   regNewRowid);. 
275b9 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43 6f 6d     }..    /* Com
275ba 70 75 74 65 20 6e 65 77 20 64 61 74 61 20 66 6f  pute new data fo
275bb 72 20 74 68 69 73 20 72 65 63 6f 72 64 2e 20 20  r this record.  
275bc 0a 20 20 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28  .    */.    for(
275bd 69 3d 30 3b 20 69 3c 70 54 61 62 2d 3e 6e 43 6f  i=0; i<pTab->nCo
275be 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69  l; i++){.      i
275bf 66 28 20 69 3d 3d 70 54 61 62 2d 3e 69 50 4b 65  f( i==pTab->iPKe
275c0 79 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  y ){.        sql
275c1 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
275c2 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30 2c 20 72 65  , OP_Null, 0, re
275c3 67 44 61 74 61 2b 69 29 3b 0a 20 20 20 20 20 20  gData+i);.      
275c4 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20    continue;.    
275c5 20 20 7d 0a 20 20 20 20 20 20 6a 20 3d 20 61 58    }.      j = aX
275c6 52 65 66 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  Ref[i];.      if
275c7 28 20 6a 3c 30 20 29 7b 0a 20 20 20 20 20 20 20  ( j<0 ){.       
275c8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
275c9 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c  p3(v, OP_Column,
275ca 20 69 43 75 72 2c 20 69 2c 20 72 65 67 44 61 74   iCur, i, regDat
275cb 61 2b 69 29 3b 0a 20 20 20 20 20 20 20 20 73 71  a+i);.        sq
275cc 6c 69 74 65 33 43 6f 6c 75 6d 6e 44 65 66 61 75  lite3ColumnDefau
275cd 6c 74 28 76 2c 20 70 54 61 62 2c 20 69 29 3b 0a  lt(v, pTab, i);.
275ce 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
275cf 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72       sqlite3Expr
275d0 43 6f 64 65 28 70 50 61 72 73 65 2c 20 70 43 68  Code(pParse, pCh
275d1 61 6e 67 65 73 2d 3e 61 5b 6a 5d 2e 70 45 78 70  anges->a[j].pExp
275d2 72 2c 20 72 65 67 44 61 74 61 2b 69 29 3b 0a 20  r, regData+i);. 
275d3 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
275d4 20 20 2f 2a 20 44 6f 20 63 6f 6e 73 74 72 61 69    /* Do constrai
275d5 6e 74 20 63 68 65 63 6b 73 0a 20 20 20 20 2a 2f  nt checks.    */
275d6 0a 20 20 20 20 73 71 6c 69 74 65 33 47 65 6e 65  .    sqlite3Gene
275d7 72 61 74 65 43 6f 6e 73 74 72 61 69 6e 74 43 68  rateConstraintCh
275d8 65 63 6b 73 28 70 50 61 72 73 65 2c 20 70 54 61  ecks(pParse, pTa
275d9 62 2c 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52  b, iCur, regNewR
275da 6f 77 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  owid,.          
275db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275dc 20 20 20 20 20 20 20 20 20 20 61 52 65 67 49 64            aRegId
275dd 78 2c 20 63 68 6e 67 52 6f 77 69 64 2c 20 31 2c  x, chngRowid, 1,
275de 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
275df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
275e0 20 20 20 20 20 6f 6e 45 72 72 6f 72 2c 20 61 64       onError, ad
275e1 64 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44 65 6c  dr);..    /* Del
275e2 65 74 65 20 74 68 65 20 6f 6c 64 20 69 6e 64 69  ete the old indi
275e3 63 65 73 20 66 6f 72 20 74 68 65 20 63 75 72 72  ces for the curr
275e4 65 6e 74 20 72 65 63 6f 72 64 2e 0a 20 20 20 20  ent record..    
275e5 2a 2f 0a 20 20 20 20 6a 31 20 3d 20 73 71 6c 69  */.    j1 = sqli
275e6 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76 2c  te3VdbeAddOp3(v,
275e7 20 4f 50 5f 4e 6f 74 45 78 69 73 74 73 2c 20 69   OP_NotExists, i
275e8 43 75 72 2c 20 30 2c 20 72 65 67 4f 6c 64 52 6f  Cur, 0, regOldRo
275e9 77 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  wid);.    sqlite
275ea 33 47 65 6e 65 72 61 74 65 52 6f 77 49 6e 64 65  3GenerateRowInde
275eb 78 44 65 6c 65 74 65 28 70 50 61 72 73 65 2c 20  xDelete(pParse, 
275ec 70 54 61 62 2c 20 69 43 75 72 2c 20 61 52 65 67  pTab, iCur, aReg
275ed 49 64 78 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66  Idx);..    /* If
275ee 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20 72 65   changing the re
275ef 63 6f 72 64 20 6e 75 6d 62 65 72 2c 20 64 65 6c  cord number, del
275f0 65 74 65 20 74 68 65 20 6f 6c 64 20 72 65 63 6f  ete the old reco
275f1 72 64 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  rd..    */.    i
275f2 66 28 20 63 68 6e 67 52 6f 77 69 64 20 29 7b 0a  f( chngRowid ){.
275f3 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
275f4 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 44 65  eAddOp2(v, OP_De
275f5 6c 65 74 65 2c 20 69 43 75 72 2c 20 30 29 3b 0a  lete, iCur, 0);.
275f6 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
275f7 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
275f8 20 6a 31 29 3b 0a 0a 20 20 20 20 2f 2a 20 43 72   j1);..    /* Cr
275f9 65 61 74 65 20 74 68 65 20 6e 65 77 20 69 6e 64  eate the new ind
275fa 65 78 20 65 6e 74 72 69 65 73 20 61 6e 64 20 74  ex entries and t
275fb 68 65 20 6e 65 77 20 72 65 63 6f 72 64 2e 0a 20  he new record.. 
275fc 20 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65     */.    sqlite
275fd 33 43 6f 6d 70 6c 65 74 65 49 6e 73 65 72 74 69  3CompleteInserti
275fe 6f 6e 28 70 50 61 72 73 65 2c 20 70 54 61 62 2c  on(pParse, pTab,
275ff 20 69 43 75 72 2c 20 72 65 67 4e 65 77 52 6f 77   iCur, regNewRow
27600 69 64 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  id, .           
27601 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27602 20 20 61 52 65 67 49 64 78 2c 20 31 2c 20 2d 31    aRegIdx, 1, -1
27603 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  , 0);.  }..  /* 
27604 49 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 6f  Increment the ro
27605 77 20 63 6f 75 6e 74 65 72 20 0a 20 20 2a 2f 0a  w counter .  */.
27606 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
27607 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
27608 77 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74  ws && !pParse->t
27609 72 69 67 53 74 61 63 6b 29 7b 0a 20 20 20 20 73  rigStack){.    s
2760a 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
2760b 28 76 2c 20 4f 50 5f 41 64 64 49 6d 6d 2c 20 72  (v, OP_AddImm, r
2760c 65 67 52 6f 77 43 6f 75 6e 74 2c 20 31 29 3b 0a  egRowCount, 1);.
2760d 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65    }..  /* If the
2760e 72 65 20 61 72 65 20 74 72 69 67 67 65 72 73 2c  re are triggers,
2760f 20 63 6c 6f 73 65 20 61 6c 6c 20 74 68 65 20 63   close all the c
27610 75 72 73 6f 72 73 20 61 66 74 65 72 20 65 61 63  ursors after eac
27611 68 20 69 74 65 72 61 74 69 6f 6e 0a 20 20 2a 2a  h iteration.  **
27612 20 74 68 72 6f 75 67 68 20 74 68 65 20 6c 6f 6f   through the loo
27613 70 2e 20 20 54 68 65 20 66 69 72 65 20 74 68 65  p.  The fire the
27614 20 61 66 74 65 72 20 74 72 69 67 67 65 72 73 2e   after triggers.
27615 0a 20 20 2a 2f 0a 20 20 69 66 28 20 74 72 69 67  .  */.  if( trig
27616 67 65 72 73 5f 65 78 69 73 74 20 29 7b 0a 20 20  gers_exist ){.  
27617 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27618 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
27619 30 2c 20 69 42 65 67 69 6e 41 66 74 65 72 54 72  0, iBeginAfterTr
2761a 69 67 67 65 72 29 3b 0a 20 20 20 20 73 71 6c 69  igger);.    sqli
2761b 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2761c 76 2c 20 69 45 6e 64 41 66 74 65 72 54 72 69 67  v, iEndAfterTrig
2761d 67 65 72 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  ger);.  }..  /* 
2761e 52 65 70 65 61 74 20 74 68 65 20 61 62 6f 76 65  Repeat the above
2761f 20 77 69 74 68 20 74 68 65 20 6e 65 78 74 20 72   with the next r
27620 65 63 6f 72 64 20 74 6f 20 62 65 20 75 70 64 61  ecord to be upda
27621 74 65 64 2c 20 75 6e 74 69 6c 0a 20 20 2a 2a 20  ted, until.  ** 
27622 61 6c 6c 20 72 65 63 6f 72 64 20 73 65 6c 65 63  all record selec
27623 74 65 64 20 62 79 20 74 68 65 20 57 48 45 52 45  ted by the WHERE
27624 20 63 6c 61 75 73 65 20 68 61 76 65 20 62 65 65   clause have bee
27625 6e 20 75 70 64 61 74 65 64 2e 0a 20 20 2a 2f 0a  n updated..  */.
27626 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27627 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74 6f 2c 20  Op2(v, OP_Goto, 
27628 30 2c 20 61 64 64 72 29 3b 0a 20 20 73 71 6c 69  0, addr);.  sqli
27629 74 65 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28  te3VdbeJumpHere(
2762a 76 2c 20 61 64 64 72 29 3b 0a 0a 20 20 2f 2a 20  v, addr);..  /* 
2762b 43 6c 6f 73 65 20 61 6c 6c 20 74 61 62 6c 65 73  Close all tables
2762c 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70   */.  for(i=0, p
2762d 49 64 78 3d 70 54 61 62 2d 3e 70 49 6e 64 65 78  Idx=pTab->pIndex
2762e 3b 20 70 49 64 78 3b 20 70 49 64 78 3d 70 49 64  ; pIdx; pIdx=pId
2762f 78 2d 3e 70 4e 65 78 74 2c 20 69 2b 2b 29 7b 0a  x->pNext, i++){.
27630 20 20 20 20 69 66 28 20 6f 70 65 6e 41 6c 6c 20      if( openAll 
27631 7c 7c 20 61 52 65 67 49 64 78 5b 69 5d 3e 30 20  || aRegIdx[i]>0 
27632 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
27633 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50  VdbeAddOp2(v, OP
27634 5f 43 6c 6f 73 65 2c 20 69 43 75 72 2b 69 2b 31  _Close, iCur+i+1
27635 2c 20 30 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  , 0);.    }.  }.
27636 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27637 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c  Op2(v, OP_Close,
27638 20 69 43 75 72 2c 20 30 29 3b 0a 20 20 69 66 28   iCur, 0);.  if(
27639 20 74 72 69 67 67 65 72 73 5f 65 78 69 73 74 20   triggers_exist 
2763a 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64  ){.    sqlite3Vd
2763b 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43  beAddOp2(v, OP_C
2763c 6c 6f 73 65 2c 20 6e 65 77 49 64 78 2c 20 30 29  lose, newIdx, 0)
2763d 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  ;.    sqlite3Vdb
2763e 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c  eAddOp2(v, OP_Cl
2763f 6f 73 65 2c 20 6f 6c 64 49 64 78 2c 20 30 29 3b  ose, oldIdx, 0);
27640 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20  .  }..  /*.  ** 
27641 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
27642 72 20 6f 66 20 72 6f 77 73 20 74 68 61 74 20 77  r of rows that w
27643 65 72 65 20 63 68 61 6e 67 65 64 2e 20 49 66 20  ere changed. If 
27644 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  this routine is 
27645 0a 20 20 2a 2a 20 67 65 6e 65 72 61 74 69 6e 67  .  ** generating
27646 20 63 6f 64 65 20 62 65 63 61 75 73 65 20 6f 66   code because of
27647 20 61 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74   a call to sqlit
27648 65 33 4e 65 73 74 65 64 50 61 72 73 65 28 29 2c  e3NestedParse(),
27649 20 64 6f 20 6e 6f 74 0a 20 20 2a 2a 20 69 6e 76   do not.  ** inv
2764a 6f 6b 65 20 74 68 65 20 63 61 6c 6c 62 61 63 6b  oke the callback
2764b 20 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 2a 2f 0a   function..  */.
2764c 20 20 69 66 28 20 64 62 2d 3e 66 6c 61 67 73 20    if( db->flags 
2764d 26 20 53 51 4c 49 54 45 5f 43 6f 75 6e 74 52 6f  & SQLITE_CountRo
2764e 77 73 20 26 26 20 21 70 50 61 72 73 65 2d 3e 74  ws && !pParse->t
2764f 72 69 67 53 74 61 63 6b 20 26 26 20 70 50 61 72  rigStack && pPar
27650 73 65 2d 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b  se->nested==0 ){
27651 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65  .    sqlite3Vdbe
27652 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 73  AddOp2(v, OP_Res
27653 75 6c 74 52 6f 77 2c 20 72 65 67 52 6f 77 43 6f  ultRow, regRowCo
27654 75 6e 74 2c 20 31 29 3b 0a 20 20 20 20 73 71 6c  unt, 1);.    sql
27655 69 74 65 33 56 64 62 65 53 65 74 4e 75 6d 43 6f  ite3VdbeSetNumCo
27656 6c 73 28 76 2c 20 31 29 3b 0a 20 20 20 20 73 71  ls(v, 1);.    sq
27657 6c 69 74 65 33 56 64 62 65 53 65 74 43 6f 6c 4e  lite3VdbeSetColN
27658 61 6d 65 28 76 2c 20 30 2c 20 43 4f 4c 4e 41 4d  ame(v, 0, COLNAM
27659 45 5f 4e 41 4d 45 2c 20 22 72 6f 77 73 20 75 70  E_NAME, "rows up
2765a 64 61 74 65 64 22 2c 20 53 51 4c 49 54 45 5f 53  dated", SQLITE_S
2765b 54 41 54 49 43 29 3b 0a 20 20 7d 0a 0a 75 70 64  TATIC);.  }..upd
2765c 61 74 65 5f 63 6c 65 61 6e 75 70 3a 0a 20 20 73  ate_cleanup:.  s
2765d 71 6c 69 74 65 33 41 75 74 68 43 6f 6e 74 65 78  qlite3AuthContex
2765e 74 50 6f 70 28 26 73 43 6f 6e 74 65 78 74 29 3b  tPop(&sContext);
2765f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65  .  sqlite3DbFree
27660 28 64 62 2c 20 61 52 65 67 49 64 78 29 3b 0a 20  (db, aRegIdx);. 
27661 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
27662 62 2c 20 61 58 52 65 66 29 3b 0a 20 20 73 71 6c  b, aXRef);.  sql
27663 69 74 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74  ite3SrcListDelet
27664 65 28 64 62 2c 20 70 54 61 62 4c 69 73 74 29 3b  e(db, pTabList);
27665 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69  .  sqlite3ExprLi
27666 73 74 44 65 6c 65 74 65 28 64 62 2c 20 70 43 68  stDelete(db, pCh
27667 61 6e 67 65 73 29 3b 0a 20 20 73 71 6c 69 74 65  anges);.  sqlite
27668 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20  3ExprDelete(db, 
27669 70 57 68 65 72 65 29 3b 0a 20 20 72 65 74 75 72  pWhere);.  retur
2766a 6e 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51  n;.}..#ifndef SQ
2766b 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
2766c 4c 54 41 42 4c 45 0a 2f 2a 0a 2a 2a 20 47 65 6e  LTABLE./*.** Gen
2766d 65 72 61 74 65 20 63 6f 64 65 20 66 6f 72 20 61  erate code for a
2766e 6e 20 55 50 44 41 54 45 20 6f 66 20 61 20 76 69  n UPDATE of a vi
2766f 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
27670 2a 2a 20 54 68 65 20 73 74 72 61 74 65 67 79 20  ** The strategy 
27671 69 73 20 74 68 61 74 20 77 65 20 63 72 65 61 74  is that we creat
27672 65 20 61 6e 20 65 70 68 65 6d 65 72 69 61 6c 20  e an ephemerial 
27673 74 61 62 6c 65 20 74 68 61 74 20 63 6f 6e 74 61  table that conta
27674 69 6e 73 0a 2a 2a 20 66 6f 72 20 65 61 63 68 20  ins.** for each 
27675 72 6f 77 20 74 6f 20 62 65 20 63 68 61 6e 67 65  row to be change
27676 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 41 29 20 20  d:.**.**   (A)  
27677 54 68 65 20 6f 72 69 67 69 6e 61 6c 20 72 6f 77  The original row
27678 69 64 20 6f 66 20 74 68 61 74 20 72 6f 77 2e 0a  id of that row..
27679 2a 2a 20 20 20 28 42 29 20 20 54 68 65 20 72 65  **   (B)  The re
2767a 76 69 73 65 64 20 72 6f 77 69 64 20 66 6f 72 20  vised rowid for 
2767b 74 68 65 20 72 6f 77 2e 20 28 6e 6f 74 65 31 29  the row. (note1)
2767c 0a 2a 2a 20 20 20 28 43 29 20 20 54 68 65 20 63  .**   (C)  The c
2767d 6f 6e 74 65 6e 74 20 6f 66 20 65 76 65 72 79 20  ontent of every 
2767e 63 6f 6c 75 6d 6e 20 69 6e 20 74 68 65 20 72 6f  column in the ro
2767f 77 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 77 65  w..**.** Then we
27680 20 6c 6f 6f 70 20 6f 76 65 72 20 74 68 69 73 20   loop over this 
27681 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20  ephemeral table 
27682 61 6e 64 20 66 6f 72 20 65 61 63 68 20 72 6f 77  and for each row
27683 20 69 6e 0a 2a 2a 20 74 68 65 20 65 70 68 65 72   in.** the epher
27684 6d 65 72 61 6c 20 74 61 62 6c 65 20 63 61 6c 6c  meral table call
27685 20 56 55 70 64 61 74 65 2e 0a 2a 2a 0a 2a 2a 20   VUpdate..**.** 
27686 57 68 65 6e 20 66 69 6e 69 73 68 65 64 2c 20 64  When finished, d
27687 72 6f 70 20 74 68 65 20 65 70 68 65 6d 65 72 61  rop the ephemera
27688 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 28  l table..**.** (
27689 6e 6f 74 65 31 29 20 41 63 74 75 61 6c 6c 79 2c  note1) Actually,
2768a 20 69 66 20 77 65 20 6b 6e 6f 77 20 69 6e 20 61   if we know in a
2768b 64 76 61 6e 63 65 20 74 68 61 74 20 28 41 29 20  dvance that (A) 
2768c 69 73 20 61 6c 77 61 79 73 20 74 68 65 20 73 61  is always the sa
2768d 6d 65 0a 2a 2a 20 61 73 20 28 42 29 20 77 65 20  me.** as (B) we 
2768e 6f 6e 6c 79 20 73 74 6f 72 65 20 28 41 29 2c 20  only store (A), 
2768f 74 68 65 6e 20 64 75 70 6c 69 63 61 74 65 20 28  then duplicate (
27690 41 29 20 77 68 65 6e 20 70 75 6c 6c 69 6e 67 0a  A) when pulling.
27691 2a 2a 20 69 74 20 6f 75 74 20 6f 66 20 74 68 65  ** it out of the
27692 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65   ephemeral table
27693 20 62 65 66 6f 72 65 20 63 61 6c 6c 69 6e 67 20   before calling 
27694 56 55 70 64 61 74 65 2e 0a 2a 2f 0a 73 74 61 74  VUpdate..*/.stat
27695 69 63 20 76 6f 69 64 20 75 70 64 61 74 65 56 69  ic void updateVi
27696 72 74 75 61 6c 54 61 62 6c 65 28 0a 20 20 50 61  rtualTable(.  Pa
27697 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20  rse *pParse,    
27698 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
27699 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53  g context */.  S
2769a 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
2769b 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75      /* The virtu
2769c 61 6c 20 74 61 62 6c 65 20 74 6f 20 62 65 20 6d  al table to be m
2769d 6f 64 69 66 69 65 64 20 2a 2f 0a 20 20 54 61 62  odified */.  Tab
2769e 6c 65 20 2a 70 54 61 62 2c 20 20 20 20 20 20 20  le *pTab,       
2769f 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61 6c    /* The virtual
276a0 20 74 61 62 6c 65 20 2a 2f 0a 20 20 45 78 70 72   table */.  Expr
276a1 4c 69 73 74 20 2a 70 43 68 61 6e 67 65 73 2c 20  List *pChanges, 
276a2 20 2f 2a 20 54 68 65 20 63 6f 6c 75 6d 6e 73 20   /* The columns 
276a3 74 6f 20 63 68 61 6e 67 65 20 69 6e 20 74 68 65  to change in the
276a4 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
276a5 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 52 6f  t */.  Expr *pRo
276a6 77 69 64 2c 20 20 20 20 20 20 20 20 2f 2a 20 45  wid,        /* E
276a7 78 70 72 65 73 73 69 6f 6e 20 75 73 65 64 20 74  xpression used t
276a8 6f 20 72 65 63 6f 6d 70 75 74 65 20 74 68 65 20  o recompute the 
276a9 72 6f 77 69 64 20 2a 2f 0a 20 20 69 6e 74 20 2a  rowid */.  int *
276aa 61 58 52 65 66 2c 20 20 20 20 20 20 20 20 20 20  aXRef,          
276ab 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20  /* Mapping from 
276ac 63 6f 6c 75 6d 6e 73 20 6f 66 20 70 54 61 62 20  columns of pTab 
276ad 74 6f 20 65 6e 74 72 69 65 73 20 69 6e 20 70 43  to entries in pC
276ae 68 61 6e 67 65 73 20 2a 2f 0a 20 20 45 78 70 72  hanges */.  Expr
276af 20 2a 70 57 68 65 72 65 20 20 20 20 20 20 20 20   *pWhere        
276b0 20 2f 2a 20 57 48 45 52 45 20 63 6c 61 75 73 65   /* WHERE clause
276b1 20 6f 66 20 74 68 65 20 55 50 44 41 54 45 20 73   of the UPDATE s
276b2 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20  tatement */.){. 
276b3 20 56 64 62 65 20 2a 76 20 3d 20 70 50 61 72 73   Vdbe *v = pPars
276b4 65 2d 3e 70 56 64 62 65 3b 20 20 2f 2a 20 56 69  e->pVdbe;  /* Vi
276b5 72 74 75 61 6c 20 6d 61 63 68 69 6e 65 20 75 6e  rtual machine un
276b6 64 65 72 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e  der construction
276b7 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a   */.  ExprList *
276b8 70 45 4c 69 73 74 20 3d 20 30 3b 20 20 20 20 20  pEList = 0;     
276b9 2f 2a 20 54 68 65 20 72 65 73 75 6c 74 20 73 65  /* The result se
276ba 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54 20  t of the SELECT 
276bb 73 74 61 74 65 6d 65 6e 74 20 2a 2f 0a 20 20 53  statement */.  S
276bc 65 6c 65 63 74 20 2a 70 53 65 6c 65 63 74 20 3d  elect *pSelect =
276bd 20 30 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20   0;      /* The 
276be 53 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74  SELECT statement
276bf 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70   */.  Expr *pExp
276c0 72 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r;              
276c1 2f 2a 20 54 65 6d 70 6f 72 61 72 79 20 65 78 70  /* Temporary exp
276c2 72 65 73 73 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74  ression */.  int
276c3 20 65 70 68 65 6d 54 61 62 3b 20 20 20 20 20 20   ephemTab;      
276c4 20 20 20 20 20 20 20 2f 2a 20 54 61 62 6c 65 20         /* Table 
276c5 68 6f 6c 64 69 6e 67 20 74 68 65 20 72 65 73 75  holding the resu
276c6 6c 74 20 6f 66 20 74 68 65 20 53 45 4c 45 43 54  lt of the SELECT
276c7 20 2a 2f 0a 20 20 69 6e 74 20 69 3b 20 20 20 20   */.  int i;    
276c8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276c9 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65 72 20  /* Loop counter 
276ca 2a 2f 0a 20 20 69 6e 74 20 61 64 64 72 3b 20 20  */.  int addr;  
276cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
276cc 2a 20 41 64 64 72 65 73 73 20 6f 66 20 74 6f 70  * Address of top
276cd 20 6f 66 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e   of loop */.  in
276ce 74 20 69 52 65 67 3b 20 20 20 20 20 20 20 20 20  t iReg;         
276cf 20 20 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74          /* First
276d0 20 72 65 67 69 73 74 65 72 20 69 6e 20 73 65 74   register in set
276d1 20 70 61 73 73 65 64 20 74 6f 20 4f 50 5f 56 55   passed to OP_VU
276d2 70 64 61 74 65 20 2a 2f 0a 20 20 73 71 6c 69 74  pdate */.  sqlit
276d3 65 33 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d  e3 *db = pParse-
276d4 3e 64 62 3b 20 2f 2a 20 44 61 74 61 62 61 73 65  >db; /* Database
276d5 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20   connection */. 
276d6 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 56 74   const char *pVt
276d7 61 62 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ab = (const char
276d8 2a 29 70 54 61 62 2d 3e 70 56 74 61 62 3b 0a 20  *)pTab->pVtab;. 
276d9 20 53 65 6c 65 63 74 44 65 73 74 20 64 65 73 74   SelectDest dest
276da 3b 0a 0a 20 20 2f 2a 20 43 6f 6e 73 74 72 75 63  ;..  /* Construc
276db 74 20 74 68 65 20 53 45 4c 45 43 54 20 73 74 61  t the SELECT sta
276dc 74 65 6d 65 6e 74 20 74 68 61 74 20 77 69 6c 6c  tement that will
276dd 20 66 69 6e 64 20 74 68 65 20 6e 65 77 20 76 61   find the new va
276de 6c 75 65 73 20 66 6f 72 0a 20 20 2a 2a 20 61 6c  lues for.  ** al
276df 6c 20 75 70 64 61 74 65 64 20 72 6f 77 73 2e 20  l updated rows. 
276e0 0a 20 20 2a 2f 0a 20 20 70 45 4c 69 73 74 20 3d  .  */.  pEList =
276e1 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
276e2 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20 30  Append(pParse, 0
276e3 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
276e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276e5 20 20 20 20 73 71 6c 69 74 65 33 43 72 65 61 74      sqlite3Creat
276e6 65 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20  eIdExpr(pParse, 
276e7 22 5f 72 6f 77 69 64 5f 22 29 2c 20 30 29 3b 0a  "_rowid_"), 0);.
276e8 20 20 69 66 28 20 70 52 6f 77 69 64 20 29 7b 0a    if( pRowid ){.
276e9 20 20 20 20 70 45 4c 69 73 74 20 3d 20 73 71 6c      pEList = sql
276ea 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
276eb 6e 64 28 70 50 61 72 73 65 2c 20 70 45 4c 69 73  nd(pParse, pELis
276ec 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  t,.             
276ed 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
276ee 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78 70        sqlite3Exp
276ef 72 44 75 70 28 64 62 2c 20 70 52 6f 77 69 64 29  rDup(db, pRowid)
276f0 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65  , 0);.  }.  asse
276f1 72 74 28 20 70 54 61 62 2d 3e 69 50 4b 65 79 3c  rt( pTab->iPKey<
276f2 30 20 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20  0 );.  for(i=0; 
276f3 69 3c 70 54 61 62 2d 3e 6e 43 6f 6c 3b 20 69 2b  i<pTab->nCol; i+
276f4 2b 29 7b 0a 20 20 20 20 69 66 28 20 61 58 52 65  +){.    if( aXRe
276f5 66 5b 69 5d 3e 3d 30 20 29 7b 0a 20 20 20 20 20  f[i]>=0 ){.     
276f6 20 70 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33   pExpr = sqlite3
276f7 45 78 70 72 44 75 70 28 64 62 2c 20 70 43 68 61  ExprDup(db, pCha
276f8 6e 67 65 73 2d 3e 61 5b 61 58 52 65 66 5b 69 5d  nges->a[aXRef[i]
276f9 5d 2e 70 45 78 70 72 29 3b 0a 20 20 20 20 7d 65  ].pExpr);.    }e
276fa 6c 73 65 7b 0a 20 20 20 20 20 20 70 45 78 70 72  lse{.      pExpr
276fb 20 3d 20 73 71 6c 69 74 65 33 43 72 65 61 74 65   = sqlite3Create
276fc 49 64 45 78 70 72 28 70 50 61 72 73 65 2c 20 70  IdExpr(pParse, p
276fd 54 61 62 2d 3e 61 43 6f 6c 5b 69 5d 2e 7a 4e 61  Tab->aCol[i].zNa
276fe 6d 65 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70  me);.    }.    p
276ff 45 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  EList = sqlite3E
27700 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
27701 61 72 73 65 2c 20 70 45 4c 69 73 74 2c 20 70 45  arse, pEList, pE
27702 78 70 72 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 70  xpr, 0);.  }.  p
27703 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74 65 33  Select = sqlite3
27704 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72 73 65  SelectNew(pParse
27705 2c 20 70 45 4c 69 73 74 2c 20 70 53 72 63 2c 20  , pEList, pSrc, 
27706 70 57 68 65 72 65 2c 20 30 2c 20 30 2c 20 30 2c  pWhere, 0, 0, 0,
27707 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 0a 20 20   0, 0, 0);.  .  
27708 2f 2a 20 43 72 65 61 74 65 20 74 68 65 20 65 70  /* Create the ep
27709 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 69 6e  hemeral table in
2770a 74 6f 20 77 68 69 63 68 20 74 68 65 20 75 70 64  to which the upd
2770b 61 74 65 20 72 65 73 75 6c 74 73 20 77 69 6c 6c  ate results will
2770c 0a 20 20 2a 2a 20 62 65 20 73 74 6f 72 65 64 2e  .  ** be stored.
2770d 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
2770e 76 20 29 3b 0a 20 20 65 70 68 65 6d 54 61 62 20  v );.  ephemTab 
2770f 3d 20 70 50 61 72 73 65 2d 3e 6e 54 61 62 2b 2b  = pParse->nTab++
27710 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41  ;.  sqlite3VdbeA
27711 64 64 4f 70 32 28 76 2c 20 4f 50 5f 4f 70 65 6e  ddOp2(v, OP_Open
27712 45 70 68 65 6d 65 72 61 6c 2c 20 65 70 68 65 6d  Ephemeral, ephem
27713 54 61 62 2c 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b  Tab, pTab->nCol+
27714 31 2b 28 70 52 6f 77 69 64 21 3d 30 29 29 3b 0a  1+(pRowid!=0));.
27715 0a 20 20 2f 2a 20 66 69 6c 6c 20 74 68 65 20 65  .  /* fill the e
27716 70 68 65 6d 65 72 61 6c 20 74 61 62 6c 65 20 0a  phemeral table .
27717 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65    */.  sqlite3Se
27718 6c 65 63 74 44 65 73 74 49 6e 69 74 28 26 64 65  lectDestInit(&de
27719 73 74 2c 20 53 52 54 5f 54 61 62 6c 65 2c 20 65  st, SRT_Table, e
2771a 70 68 65 6d 54 61 62 29 3b 0a 20 20 73 71 6c 69  phemTab);.  sqli
2771b 74 65 33 53 65 6c 65 63 74 28 70 50 61 72 73 65  te3Select(pParse
2771c 2c 20 70 53 65 6c 65 63 74 2c 20 26 64 65 73 74  , pSelect, &dest
2771d 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  );..  /* Generat
2771e 65 20 63 6f 64 65 20 74 6f 20 73 63 61 6e 20 74  e code to scan t
2771f 68 65 20 65 70 68 65 6d 65 72 61 6c 20 74 61 62  he ephemeral tab
27720 6c 65 20 61 6e 64 20 63 61 6c 6c 20 56 55 70 64  le and call VUpd
27721 61 74 65 2e 20 2a 2f 0a 20 20 69 52 65 67 20 3d  ate. */.  iReg =
27722 20 2b 2b 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b   ++pParse->nMem;
27723 0a 20 20 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 20  .  pParse->nMem 
27724 2b 3d 20 70 54 61 62 2d 3e 6e 43 6f 6c 2b 31 3b  += pTab->nCol+1;
27725 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64  .  sqlite3VdbeAd
27726 64 4f 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e  dOp2(v, OP_Rewin
27727 64 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29 3b  d, ephemTab, 0);
27728 0a 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65  .  addr = sqlite
27729 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72  3VdbeCurrentAddr
2772a 28 76 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  (v);.  sqlite3Vd
2772b 62 65 41 64 64 4f 70 33 28 76 2c 20 4f 50 5f 43  beAddOp3(v, OP_C
2772c 6f 6c 75 6d 6e 2c 20 20 65 70 68 65 6d 54 61 62  olumn,  ephemTab
2772d 2c 20 30 2c 20 69 52 65 67 29 3b 0a 20 20 73 71  , 0, iReg);.  sq
2772e 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28  lite3VdbeAddOp3(
2772f 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 65 70  v, OP_Column, ep
27730 68 65 6d 54 61 62 2c 20 28 70 52 6f 77 69 64 3f  hemTab, (pRowid?
27731 31 3a 30 29 2c 20 69 52 65 67 2b 31 29 3b 0a 20  1:0), iReg+1);. 
27732 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54 61 62   for(i=0; i<pTab
27733 2d 3e 6e 43 6f 6c 3b 20 69 2b 2b 29 7b 0a 20 20  ->nCol; i++){.  
27734 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27735 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
27736 2c 20 65 70 68 65 6d 54 61 62 2c 20 69 2b 31 2b  , ephemTab, i+1+
27737 28 70 52 6f 77 69 64 21 3d 30 29 2c 20 69 52 65  (pRowid!=0), iRe
27738 67 2b 32 2b 69 29 3b 0a 20 20 7d 0a 20 20 73 71  g+2+i);.  }.  sq
27739 6c 69 74 65 33 56 74 61 62 4d 61 6b 65 57 72 69  lite3VtabMakeWri
2773a 74 61 62 6c 65 28 70 50 61 72 73 65 2c 20 70 54  table(pParse, pT
2773b 61 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64  ab);.  sqlite3Vd
2773c 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 56  beAddOp4(v, OP_V
2773d 55 70 64 61 74 65 2c 20 30 2c 20 70 54 61 62 2d  Update, 0, pTab-
2773e 3e 6e 43 6f 6c 2b 32 2c 20 69 52 65 67 2c 20 70  >nCol+2, iReg, p
2773f 56 74 61 62 2c 20 50 34 5f 56 54 41 42 29 3b 0a  Vtab, P4_VTAB);.
27740 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
27741 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
27742 65 70 68 65 6d 54 61 62 2c 20 61 64 64 72 29 3b  ephemTab, addr);
27743 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75  .  sqlite3VdbeJu
27744 6d 70 48 65 72 65 28 76 2c 20 61 64 64 72 2d 31  mpHere(v, addr-1
27745 29 3b 0a 20 20 73 71 6c 69 74 65 33 56 64 62 65  );.  sqlite3Vdbe
27746 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 43 6c 6f  AddOp2(v, OP_Clo
27747 73 65 2c 20 65 70 68 65 6d 54 61 62 2c 20 30 29  se, ephemTab, 0)
27748 3b 0a 0a 20 20 2f 2a 20 43 6c 65 61 6e 75 70 20  ;..  /* Cleanup 
27749 2a 2f 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  */.  sqlite3Sele
2774a 63 74 44 65 6c 65 74 65 28 64 62 2c 20 70 53 65  ctDelete(db, pSe
2774b 6c 65 63 74 29 3b 20 20 0a 7d 0a 23 65 6e 64 69  lect);  .}.#endi
2774c 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54  f /* SQLITE_OMIT
2774d 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20 2a 2f  _VIRTUALTABLE */
2774e 0a 0a 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 22  ../* Make sure "
2774f 69 73 56 69 65 77 22 20 67 65 74 73 20 75 6e 64  isView" gets und
27750 65 66 69 6e 65 64 20 69 6e 20 63 61 73 65 20 74  efined in case t
27751 68 69 73 20 66 69 6c 65 20 62 65 63 6f 6d 65 73  his file becomes
27752 20 70 61 72 74 20 6f 66 0a 2a 2a 20 74 68 65 20   part of.** the 
27753 61 6d 61 6c 67 61 6d 61 74 69 6f 6e 20 2d 20 73  amalgamation - s
27754 6f 20 74 68 61 74 20 73 75 62 73 65 71 75 65 6e  o that subsequen
27755 74 20 66 69 6c 65 73 20 64 6f 20 6e 6f 74 20 73  t files do not s
27756 65 65 20 69 73 56 69 65 77 20 61 73 20 61 0a 2a  ee isView as a.*
27757 2a 20 6d 61 63 72 6f 2e 20 2a 2f 0a 23 75 6e 64  * macro. */.#und
27758 65 66 20 69 73 56 69 65 77 0a 0a 2f 2a 2a 2a 2a  ef isView../****
27759 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
2775a 66 20 75 70 64 61 74 65 2e 63 20 2a 2a 2a 2a 2a  f update.c *****
2775b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2775c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2775d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2775e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2775f 20 66 69 6c 65 20 76 61 63 75 75 6d 2e 63 20 2a   file vacuum.c *
27760 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27761 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27762 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
27763 20 32 30 30 33 20 41 70 72 69 6c 20 36 0a 2a 2a   2003 April 6.**
27764 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
27765 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
27766 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
27767 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
27768 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
27769 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2776a 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2776b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2776c 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2776d 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2776e 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2776f 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
27770 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
27771 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
27772 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
27773 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
27774 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
27775 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
27776 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27777 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27778 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27779 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2777a 54 68 69 73 20 66 69 6c 65 20 63 6f 6e 74 61 69  This file contai
2777b 6e 73 20 63 6f 64 65 20 75 73 65 64 20 74 6f 20  ns code used to 
2777c 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 56 41  implement the VA
2777d 43 55 55 4d 20 63 6f 6d 6d 61 6e 64 2e 0a 2a 2a  CUUM command..**
2777e 0a 2a 2a 20 4d 6f 73 74 20 6f 66 20 74 68 65 20  .** Most of the 
2777f 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
27780 65 20 6d 61 79 20 62 65 20 6f 6d 69 74 74 65 64  e may be omitted
27781 20 62 79 20 64 65 66 69 6e 69 6e 67 20 74 68 65   by defining the
27782 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  .** SQLITE_OMIT_
27783 56 41 43 55 55 4d 20 6d 61 63 72 6f 2e 0a 2a 2a  VACUUM macro..**
27784 0a 2a 2a 20 24 49 64 3a 20 76 61 63 75 75 6d 2e  .** $Id: vacuum.
27785 63 2c 76 20 31 2e 38 34 20 32 30 30 38 2f 31 31  c,v 1.84 2008/11
27786 2f 31 37 20 31 39 3a 31 38 3a 35 35 20 64 61 6e  /17 19:18:55 dan
27787 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
27788 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
27789 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 41 43 55  SQLITE_OMIT_VACU
2778a 55 4d 29 20 26 26 20 21 64 65 66 69 6e 65 64 28  UM) && !defined(
2778b 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41 54 54 41  SQLITE_OMIT_ATTA
2778c 43 48 29 0a 2f 2a 0a 2a 2a 20 45 78 65 63 75 74  CH)./*.** Execut
2778d 65 20 7a 53 71 6c 20 6f 6e 20 64 61 74 61 62 61  e zSql on databa
2778e 73 65 20 64 62 2e 20 52 65 74 75 72 6e 20 61 6e  se db. Return an
2778f 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a 2a 2f 0a   error code..*/.
27790 73 74 61 74 69 63 20 69 6e 74 20 65 78 65 63 53  static int execS
27791 71 6c 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ql(sqlite3 *db, 
27792 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53 71 6c  const char *zSql
27793 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  ){.  sqlite3_stm
27794 74 20 2a 70 53 74 6d 74 3b 0a 20 20 69 66 28 20  t *pStmt;.  if( 
27795 21 7a 53 71 6c 20 29 7b 0a 20 20 20 20 72 65 74  !zSql ){.    ret
27796 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
27797 3b 0a 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49  ;.  }.  if( SQLI
27798 54 45 5f 4f 4b 21 3d 73 71 6c 69 74 65 33 5f 70  TE_OK!=sqlite3_p
27799 72 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c  repare(db, zSql,
2779a 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 20   -1, &pStmt, 0) 
2779b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71  ){.    return sq
2779c 6c 69 74 65 33 5f 65 72 72 63 6f 64 65 28 64 62  lite3_errcode(db
2779d 29 3b 0a 20 20 7d 0a 20 20 77 68 69 6c 65 28 20  );.  }.  while( 
2779e 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69  SQLITE_ROW==sqli
2779f 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74 29 20  te3_step(pStmt) 
277a0 29 7b 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  ){}.  return sql
277a1 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
277a2 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45  tmt);.}../*.** E
277a3 78 65 63 75 74 65 20 7a 53 71 6c 20 6f 6e 20 64  xecute zSql on d
277a4 61 74 61 62 61 73 65 20 64 62 2e 20 54 68 65 20  atabase db. The 
277a5 73 74 61 74 65 6d 65 6e 74 20 72 65 74 75 72 6e  statement return
277a6 73 20 65 78 61 63 74 6c 79 0a 2a 2a 20 6f 6e 65  s exactly.** one
277a7 20 63 6f 6c 75 6d 6e 2e 20 45 78 65 63 75 74 65   column. Execute
277a8 20 74 68 69 73 20 61 73 20 53 51 4c 20 6f 6e 20   this as SQL on 
277a9 74 68 65 20 73 61 6d 65 20 64 61 74 61 62 61 73  the same databas
277aa 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
277ab 20 65 78 65 63 45 78 65 63 53 71 6c 28 73 71 6c   execExecSql(sql
277ac 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
277ad 63 68 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 73  char *zSql){.  s
277ae 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 53 74  qlite3_stmt *pSt
277af 6d 74 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  mt;.  int rc;.. 
277b0 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72   rc = sqlite3_pr
277b1 65 70 61 72 65 28 64 62 2c 20 7a 53 71 6c 2c 20  epare(db, zSql, 
277b2 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29 3b 0a  -1, &pStmt, 0);.
277b3 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
277b4 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
277b5 0a 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54  ..  while( SQLIT
277b6 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73  E_ROW==sqlite3_s
277b7 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a 20 20  tep(pStmt) ){.  
277b8 20 20 72 63 20 3d 20 65 78 65 63 53 71 6c 28 64    rc = execSql(d
277b9 62 2c 20 28 63 68 61 72 2a 29 73 71 6c 69 74 65  b, (char*)sqlite
277ba 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 70 53  3_column_text(pS
277bb 74 6d 74 2c 20 30 29 29 3b 0a 20 20 20 20 69 66  tmt, 0));.    if
277bc 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
277bd 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
277be 5f 66 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29  _finalize(pStmt)
277bf 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
277c0 63 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  c;.    }.  }..  
277c1 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 66  return sqlite3_f
277c2 69 6e 61 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a  inalize(pStmt);.
277c3 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 6e 6f 6e  }../*.** The non
277c4 2d 73 74 61 6e 64 61 72 64 20 56 41 43 55 55 4d  -standard VACUUM
277c5 20 63 6f 6d 6d 61 6e 64 20 69 73 20 75 73 65 64   command is used
277c6 20 74 6f 20 63 6c 65 61 6e 20 75 70 20 74 68 65   to clean up the
277c7 20 64 61 74 61 62 61 73 65 2c 0a 2a 2a 20 63 6f   database,.** co
277c8 6c 6c 61 70 73 65 20 66 72 65 65 20 73 70 61 63  llapse free spac
277c9 65 2c 20 65 74 63 2e 20 20 49 74 20 69 73 20 6d  e, etc.  It is m
277ca 6f 64 65 6c 6c 65 64 20 61 66 74 65 72 20 74 68  odelled after th
277cb 65 20 56 41 43 55 55 4d 20 63 6f 6d 6d 61 6e 64  e VACUUM command
277cc 0a 2a 2a 20 69 6e 20 50 6f 73 74 67 72 65 53 51  .** in PostgreSQ
277cd 4c 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 76 65 72 73  L..**.** In vers
277ce 69 6f 6e 20 31 2e 30 2e 78 20 6f 66 20 53 51 4c  ion 1.0.x of SQL
277cf 69 74 65 2c 20 74 68 65 20 56 41 43 55 55 4d 20  ite, the VACUUM 
277d0 63 6f 6d 6d 61 6e 64 20 77 6f 75 6c 64 20 63 61  command would ca
277d1 6c 6c 0a 2a 2a 20 67 64 62 6d 5f 72 65 6f 72 67  ll.** gdbm_reorg
277d2 61 6e 69 7a 65 28 29 20 6f 6e 20 61 6c 6c 20 74  anize() on all t
277d3 68 65 20 64 61 74 61 62 61 73 65 20 74 61 62 6c  he database tabl
277d4 65 73 2e 20 20 42 75 74 20 62 65 67 69 6e 6e 69  es.  But beginni
277d5 6e 67 0a 2a 2a 20 77 69 74 68 20 32 2e 30 2e 30  ng.** with 2.0.0
277d6 2c 20 53 51 4c 69 74 65 20 6e 6f 20 6c 6f 6e 67  , SQLite no long
277d7 65 72 20 75 73 65 73 20 47 44 42 4d 20 73 6f 20  er uses GDBM so 
277d8 74 68 69 73 20 63 6f 6d 6d 61 6e 64 20 68 61 73  this command has
277d9 0a 2a 2a 20 62 65 63 6f 6d 65 20 61 20 6e 6f 2d  .** become a no-
277da 6f 70 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  op..*/.SQLITE_PR
277db 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74  IVATE void sqlit
277dc 65 33 56 61 63 75 75 6d 28 50 61 72 73 65 20 2a  e3Vacuum(Parse *
277dd 70 50 61 72 73 65 29 7b 0a 20 20 56 64 62 65 20  pParse){.  Vdbe 
277de 2a 76 20 3d 20 73 71 6c 69 74 65 33 47 65 74 56  *v = sqlite3GetV
277df 64 62 65 28 70 50 61 72 73 65 29 3b 0a 20 20 69  dbe(pParse);.  i
277e0 66 28 20 76 20 29 7b 0a 20 20 20 20 73 71 6c 69  f( v ){.    sqli
277e1 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
277e2 20 4f 50 5f 56 61 63 75 75 6d 2c 20 30 2c 20 30   OP_Vacuum, 0, 0
277e3 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 3b  );.  }.  return;
277e4 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  .}../*.** This r
277e5 6f 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74  outine implement
277e6 73 20 74 68 65 20 4f 50 5f 56 61 63 75 75 6d 20  s the OP_Vacuum 
277e7 6f 70 63 6f 64 65 20 6f 66 20 74 68 65 20 56 44  opcode of the VD
277e8 42 45 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  BE..*/.SQLITE_PR
277e9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
277ea 33 52 75 6e 56 61 63 75 75 6d 28 63 68 61 72 20  3RunVacuum(char 
277eb 2a 2a 70 7a 45 72 72 4d 73 67 2c 20 73 71 6c 69  **pzErrMsg, sqli
277ec 74 65 33 20 2a 64 62 29 7b 0a 20 20 69 6e 74 20  te3 *db){.  int 
277ed 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 20  rc = SQLITE_OK; 
277ee 20 20 20 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f      /* Return co
277ef 64 65 20 66 72 6f 6d 20 73 65 72 76 69 63 65 20  de from service 
277f0 72 6f 75 74 69 6e 65 73 20 2a 2f 0a 20 20 42 74  routines */.  Bt
277f1 72 65 65 20 2a 70 4d 61 69 6e 3b 20 20 20 20 20  ree *pMain;     
277f2 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64 61 74        /* The dat
277f3 61 62 61 73 65 20 62 65 69 6e 67 20 76 61 63 75  abase being vacu
277f4 75 6d 65 64 20 2a 2f 0a 20 20 50 61 67 65 72 20  umed */.  Pager 
277f5 2a 70 4d 61 69 6e 50 61 67 65 72 3b 20 20 20 20  *pMainPager;    
277f6 20 20 2f 2a 20 50 61 67 65 72 20 66 6f 72 20 64    /* Pager for d
277f7 61 74 61 62 61 73 65 20 62 65 69 6e 67 20 76 61  atabase being va
277f8 63 75 75 6d 65 64 20 2a 2f 0a 20 20 42 74 72 65  cuumed */.  Btre
277f9 65 20 2a 70 54 65 6d 70 3b 20 20 20 20 20 20 20  e *pTemp;       
277fa 20 20 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 6f      /* The tempo
277fb 72 61 72 79 20 64 61 74 61 62 61 73 65 20 77 65  rary database we
277fc 20 76 61 63 75 75 6d 20 69 6e 74 6f 20 2a 2f 0a   vacuum into */.
277fd 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 30    char *zSql = 0
277fe 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 53 51 4c  ;         /* SQL
277ff 20 73 74 61 74 65 6d 65 6e 74 73 20 2a 2f 0a 20   statements */. 
27800 20 69 6e 74 20 73 61 76 65 64 5f 66 6c 61 67 73   int saved_flags
27801 3b 20 20 20 20 20 20 20 20 2f 2a 20 53 61 76 65  ;        /* Save
27802 64 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 64  d value of the d
27803 62 2d 3e 66 6c 61 67 73 20 2a 2f 0a 20 20 69 6e  b->flags */.  in
27804 74 20 73 61 76 65 64 5f 6e 43 68 61 6e 67 65 3b  t saved_nChange;
27805 20 20 20 20 20 20 2f 2a 20 53 61 76 65 64 20 76        /* Saved v
27806 61 6c 75 65 20 6f 66 20 64 62 2d 3e 6e 43 68 61  alue of db->nCha
27807 6e 67 65 20 2a 2f 0a 20 20 69 6e 74 20 73 61 76  nge */.  int sav
27808 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e 67 65 3b  ed_nTotalChange;
27809 20 2f 2a 20 53 61 76 65 64 20 76 61 6c 75 65 20   /* Saved value 
2780a 6f 66 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61  of db->nTotalCha
2780b 6e 67 65 20 2a 2f 0a 20 20 44 62 20 2a 70 44 62  nge */.  Db *pDb
2780c 20 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20   = 0;           
2780d 20 2f 2a 20 44 61 74 61 62 61 73 65 20 74 6f 20   /* Database to 
2780e 64 65 74 61 63 68 20 61 74 20 65 6e 64 20 6f 66  detach at end of
2780f 20 76 61 63 75 75 6d 20 2a 2f 0a 20 20 69 6e 74   vacuum */.  int
27810 20 69 73 4d 65 6d 44 62 3b 20 20 20 20 20 20 20   isMemDb;       
27811 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 73 20       /* True is 
27812 76 61 63 75 75 6d 69 6e 67 20 61 20 3a 6d 65 6d  vacuuming a :mem
27813 6f 72 79 3a 20 64 61 74 61 62 61 73 65 20 2a 2f  ory: database */
27814 0a 20 20 69 6e 74 20 6e 52 65 73 3b 0a 0a 20 20  .  int nRes;..  
27815 2f 2a 20 53 61 76 65 20 74 68 65 20 63 75 72 72  /* Save the curr
27816 65 6e 74 20 76 61 6c 75 65 20 6f 66 20 74 68 65  ent value of the
27817 20 77 72 69 74 65 2d 73 63 68 65 6d 61 20 66 6c   write-schema fl
27818 61 67 20 62 65 66 6f 72 65 20 73 65 74 74 69 6e  ag before settin
27819 67 20 69 74 2e 20 2a 2f 0a 20 20 73 61 76 65 64  g it. */.  saved
2781a 5f 66 6c 61 67 73 20 3d 20 64 62 2d 3e 66 6c 61  _flags = db->fla
2781b 67 73 3b 0a 20 20 73 61 76 65 64 5f 6e 43 68 61  gs;.  saved_nCha
2781c 6e 67 65 20 3d 20 64 62 2d 3e 6e 43 68 61 6e 67  nge = db->nChang
2781d 65 3b 0a 20 20 73 61 76 65 64 5f 6e 54 6f 74 61  e;.  saved_nTota
2781e 6c 43 68 61 6e 67 65 20 3d 20 64 62 2d 3e 6e 54  lChange = db->nT
2781f 6f 74 61 6c 43 68 61 6e 67 65 3b 0a 20 20 64 62  otalChange;.  db
27820 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c 49 54  ->flags |= SQLIT
27821 45 5f 57 72 69 74 65 53 63 68 65 6d 61 20 7c 20  E_WriteSchema | 
27822 53 51 4c 49 54 45 5f 49 67 6e 6f 72 65 43 68 65  SQLITE_IgnoreChe
27823 63 6b 73 3b 0a 0a 20 20 69 66 28 20 21 64 62 2d  cks;..  if( !db-
27824 3e 61 75 74 6f 43 6f 6d 6d 69 74 20 29 7b 0a 20  >autoCommit ){. 
27825 20 20 20 73 71 6c 69 74 65 33 53 65 74 53 74 72     sqlite3SetStr
27826 69 6e 67 28 70 7a 45 72 72 4d 73 67 2c 20 64 62  ing(pzErrMsg, db
27827 2c 20 22 63 61 6e 6e 6f 74 20 56 41 43 55 55 4d  , "cannot VACUUM
27828 20 66 72 6f 6d 20 77 69 74 68 69 6e 20 61 20 74   from within a t
27829 72 61 6e 73 61 63 74 69 6f 6e 22 29 3b 0a 20 20  ransaction");.  
2782a 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
2782b 52 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 6e  ROR;.    goto en
2782c 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d  d_of_vacuum;.  }
2782d 0a 20 20 70 4d 61 69 6e 20 3d 20 64 62 2d 3e 61  .  pMain = db->a
2782e 44 62 5b 30 5d 2e 70 42 74 3b 0a 20 20 70 4d 61  Db[0].pBt;.  pMa
2782f 69 6e 50 61 67 65 72 20 3d 20 73 71 6c 69 74 65  inPager = sqlite
27830 33 42 74 72 65 65 50 61 67 65 72 28 70 4d 61 69  3BtreePager(pMai
27831 6e 29 3b 0a 20 20 69 73 4d 65 6d 44 62 20 3d 20  n);.  isMemDb = 
27832 73 71 6c 69 74 65 33 50 61 67 65 72 46 69 6c 65  sqlite3PagerFile
27833 28 70 4d 61 69 6e 50 61 67 65 72 29 2d 3e 70 4d  (pMainPager)->pM
27834 65 74 68 6f 64 73 3d 3d 30 3b 0a 0a 20 20 2f 2a  ethods==0;..  /*
27835 20 41 74 74 61 63 68 20 74 68 65 20 74 65 6d 70   Attach the temp
27836 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 20 61  orary database a
27837 73 20 27 76 61 63 75 75 6d 5f 64 62 27 2e 20 54  s 'vacuum_db'. T
27838 68 65 20 73 79 6e 63 68 72 6f 6e 6f 75 73 20 70  he synchronous p
27839 72 61 67 6d 61 0a 20 20 2a 2a 20 63 61 6e 20 62  ragma.  ** can b
2783a 65 20 73 65 74 20 74 6f 20 27 6f 66 66 27 20 66  e set to 'off' f
2783b 6f 72 20 74 68 69 73 20 66 69 6c 65 2c 20 61 73  or this file, as
2783c 20 69 74 20 69 73 20 6e 6f 74 20 72 65 63 6f 76   it is not recov
2783d 65 72 65 64 20 69 66 20 61 20 63 72 61 73 68 0a  ered if a crash.
2783e 20 20 2a 2a 20 6f 63 63 75 72 73 20 61 6e 79 77    ** occurs anyw
2783f 61 79 2e 20 54 68 65 20 69 6e 74 65 67 72 69 74  ay. The integrit
27840 79 20 6f 66 20 74 68 65 20 64 61 74 61 62 61 73  y of the databas
27841 65 20 69 73 20 6d 61 69 6e 74 61 69 6e 65 64 20  e is maintained 
27842 62 79 20 61 0a 20 20 2a 2a 20 28 70 6f 73 73 69  by a.  ** (possi
27843 62 6c 79 20 73 79 6e 63 68 72 6f 6e 6f 75 73 29  bly synchronous)
27844 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 6f 70 65   transaction ope
27845 6e 65 64 20 6f 6e 20 74 68 65 20 6d 61 69 6e 20  ned on the main 
27846 64 61 74 61 62 61 73 65 20 62 65 66 6f 72 65 0a  database before.
27847 20 20 2a 2a 20 73 71 6c 69 74 65 33 42 74 72 65    ** sqlite3Btre
27848 65 43 6f 70 79 46 69 6c 65 28 29 20 69 73 20 63  eCopyFile() is c
27849 61 6c 6c 65 64 2e 0a 20 20 2a 2a 0a 20 20 2a 2a  alled..  **.  **
2784a 20 41 6e 20 6f 70 74 69 6d 69 73 61 74 69 6f 6e   An optimisation
2784b 20 77 6f 75 6c 64 20 62 65 20 74 6f 20 75 73 65   would be to use
2784c 20 61 20 6e 6f 6e 2d 6a 6f 75 72 6e 61 6c 65 64   a non-journaled
2784d 20 70 61 67 65 72 2e 0a 20 20 2a 2a 20 28 4c 61   pager..  ** (La
2784e 74 65 72 3a 29 20 49 20 74 72 69 65 64 20 73 65  ter:) I tried se
2784f 74 74 69 6e 67 20 22 50 52 41 47 4d 41 20 76 61  tting "PRAGMA va
27850 63 75 75 6d 5f 64 62 2e 6a 6f 75 72 6e 61 6c 5f  cuum_db.journal_
27851 6d 6f 64 65 3d 4f 46 46 22 20 62 75 74 0a 20 20  mode=OFF" but.  
27852 2a 2a 20 74 68 61 74 20 61 63 74 75 61 6c 6c 79  ** that actually
27853 20 6d 61 64 65 20 74 68 65 20 56 41 43 55 55 4d   made the VACUUM
27854 20 72 75 6e 20 73 6c 6f 77 65 72 2e 20 20 56 65   run slower.  Ve
27855 72 79 20 6c 69 74 74 6c 65 20 6a 6f 75 72 6e 61  ry little journa
27856 6c 6c 69 6e 67 0a 20 20 2a 2a 20 61 63 74 75 61  lling.  ** actua
27857 6c 6c 79 20 6f 63 63 75 72 73 20 77 68 65 6e 20  lly occurs when 
27858 64 6f 69 6e 67 20 61 20 76 61 63 75 75 6d 20 73  doing a vacuum s
27859 69 6e 63 65 20 74 68 65 20 76 61 63 75 75 6d 5f  ince the vacuum_
2785a 64 62 20 69 73 20 69 6e 69 74 69 61 6c 6c 79 0a  db is initially.
2785b 20 20 2a 2a 20 65 6d 70 74 79 2e 20 20 4f 6e 6c    ** empty.  Onl
2785c 79 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20 68 65  y the journal he
2785d 61 64 65 72 20 69 73 20 77 72 69 74 74 65 6e 2e  ader is written.
2785e 20 20 41 70 70 61 72 65 6e 74 6c 79 20 69 74 20    Apparently it 
2785f 74 61 6b 65 73 20 6d 6f 72 65 0a 20 20 2a 2a 20  takes more.  ** 
27860 74 69 6d 65 20 74 6f 20 70 61 72 73 65 20 61 6e  time to parse an
27861 64 20 72 75 6e 20 74 68 65 20 50 52 41 47 4d 41  d run the PRAGMA
27862 20 74 6f 20 74 75 72 6e 20 6a 6f 75 72 6e 61 6c   to turn journal
27863 6c 69 6e 67 20 6f 66 66 20 74 68 61 6e 20 69 74  ling off than it
27864 20 64 6f 65 73 0a 20 20 2a 2a 20 74 6f 20 77 72   does.  ** to wr
27865 69 74 65 20 74 68 65 20 6a 6f 75 72 6e 61 6c 20  ite the journal 
27866 68 65 61 64 65 72 20 66 69 6c 65 2e 0a 20 20 2a  header file..  *
27867 2f 0a 20 20 7a 53 71 6c 20 3d 20 22 41 54 54 41  /.  zSql = "ATTA
27868 43 48 20 27 27 20 41 53 20 76 61 63 75 75 6d 5f  CH '' AS vacuum_
27869 64 62 3b 22 3b 0a 20 20 72 63 20 3d 20 65 78 65  db;";.  rc = exe
2786a 63 53 71 6c 28 64 62 2c 20 7a 53 71 6c 29 3b 0a  cSql(db, zSql);.
2786b 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2786c 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
2786d 66 5f 76 61 63 75 75 6d 3b 0a 20 20 70 44 62 20  f_vacuum;.  pDb 
2786e 3d 20 26 64 62 2d 3e 61 44 62 5b 64 62 2d 3e 6e  = &db->aDb[db->n
2786f 44 62 2d 31 5d 3b 0a 20 20 61 73 73 65 72 74 28  Db-1];.  assert(
27870 20 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62 5b   strcmp(db->aDb[
27871 64 62 2d 3e 6e 44 62 2d 31 5d 2e 7a 4e 61 6d 65  db->nDb-1].zName
27872 2c 22 76 61 63 75 75 6d 5f 64 62 22 29 3d 3d 30  ,"vacuum_db")==0
27873 20 29 3b 0a 20 20 70 54 65 6d 70 20 3d 20 64 62   );.  pTemp = db
27874 2d 3e 61 44 62 5b 64 62 2d 3e 6e 44 62 2d 31 5d  ->aDb[db->nDb-1]
27875 2e 70 42 74 3b 0a 0a 20 20 6e 52 65 73 20 3d 20  .pBt;..  nRes = 
27876 73 71 6c 69 74 65 33 42 74 72 65 65 47 65 74 52  sqlite3BtreeGetR
27877 65 73 65 72 76 65 28 70 4d 61 69 6e 29 3b 0a 0a  eserve(pMain);..
27878 20 20 2f 2a 20 41 20 56 41 43 55 55 4d 20 63 61    /* A VACUUM ca
27879 6e 6e 6f 74 20 63 68 61 6e 67 65 20 74 68 65 20  nnot change the 
2787a 70 61 67 65 73 69 7a 65 20 6f 66 20 61 6e 20 65  pagesize of an e
2787b 6e 63 72 79 70 74 65 64 20 64 61 74 61 62 61 73  ncrypted databas
2787c 65 2e 20 2a 2f 0a 23 69 66 64 65 66 20 53 51 4c  e. */.#ifdef SQL
2787d 49 54 45 5f 48 41 53 5f 43 4f 44 45 43 0a 20 20  ITE_HAS_CODEC.  
2787e 69 66 28 20 64 62 2d 3e 6e 65 78 74 50 61 67 65  if( db->nextPage
2787f 73 69 7a 65 20 29 7b 0a 20 20 20 20 65 78 74 65  size ){.    exte
27880 72 6e 20 76 6f 69 64 20 73 71 6c 69 74 65 33 43  rn void sqlite3C
27881 6f 64 65 63 47 65 74 4b 65 79 28 73 71 6c 69 74  odecGetKey(sqlit
27882 65 33 2a 2c 20 69 6e 74 2c 20 76 6f 69 64 2a 2a  e3*, int, void**
27883 2c 20 69 6e 74 2a 29 3b 0a 20 20 20 20 69 6e 74  , int*);.    int
27884 20 6e 4b 65 79 3b 0a 20 20 20 20 63 68 61 72 20   nKey;.    char 
27885 2a 7a 4b 65 79 3b 0a 20 20 20 20 73 71 6c 69 74  *zKey;.    sqlit
27886 65 33 43 6f 64 65 63 47 65 74 4b 65 79 28 64 62  e3CodecGetKey(db
27887 2c 20 30 2c 20 28 76 6f 69 64 2a 2a 29 26 7a 4b  , 0, (void**)&zK
27888 65 79 2c 20 26 6e 4b 65 79 29 3b 0a 20 20 20 20  ey, &nKey);.    
27889 69 66 28 20 6e 4b 65 79 20 29 20 64 62 2d 3e 6e  if( nKey ) db->n
2788a 65 78 74 50 61 67 65 73 69 7a 65 20 3d 20 30 3b  extPagesize = 0;
2788b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 69  .  }.#endif..  i
2788c 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65 53  f( sqlite3BtreeS
2788d 65 74 50 61 67 65 53 69 7a 65 28 70 54 65 6d 70  etPageSize(pTemp
2788e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  , sqlite3BtreeGe
2788f 74 50 61 67 65 53 69 7a 65 28 70 4d 61 69 6e 29  tPageSize(pMain)
27890 2c 20 6e 52 65 73 29 0a 20 20 20 7c 7c 20 28 21  , nRes).   || (!
27891 69 73 4d 65 6d 44 62 20 26 26 20 73 71 6c 69 74  isMemDb && sqlit
27892 65 33 42 74 72 65 65 53 65 74 50 61 67 65 53 69  e3BtreeSetPageSi
27893 7a 65 28 70 54 65 6d 70 2c 20 64 62 2d 3e 6e 65  ze(pTemp, db->ne
27894 78 74 50 61 67 65 73 69 7a 65 2c 20 6e 52 65 73  xtPagesize, nRes
27895 29 29 0a 20 20 20 7c 7c 20 64 62 2d 3e 6d 61 6c  )).   || db->mal
27896 6c 6f 63 46 61 69 6c 65 64 20 0a 20 20 29 7b 0a  locFailed .  ){.
27897 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f      rc = SQLITE_
27898 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f 20  NOMEM;.    goto 
27899 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20  end_of_vacuum;. 
2789a 20 7d 0a 20 20 72 63 20 3d 20 65 78 65 63 53 71   }.  rc = execSq
2789b 6c 28 64 62 2c 20 22 50 52 41 47 4d 41 20 76 61  l(db, "PRAGMA va
2789c 63 75 75 6d 5f 64 62 2e 73 79 6e 63 68 72 6f 6e  cuum_db.synchron
2789d 6f 75 73 3d 4f 46 46 22 29 3b 0a 20 20 69 66 28  ous=OFF");.  if(
2789e 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2789f 7b 0a 20 20 20 20 67 6f 74 6f 20 65 6e 64 5f 6f  {.    goto end_o
278a0 66 5f 76 61 63 75 75 6d 3b 0a 20 20 7d 0a 0a 23  f_vacuum;.  }..#
278a1 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
278a2 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20 20  IT_AUTOVACUUM.  
278a3 73 71 6c 69 74 65 33 42 74 72 65 65 53 65 74 41  sqlite3BtreeSetA
278a4 75 74 6f 56 61 63 75 75 6d 28 70 54 65 6d 70 2c  utoVacuum(pTemp,
278a5 20 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63   db->nextAutovac
278a6 3e 3d 30 20 3f 20 64 62 2d 3e 6e 65 78 74 41 75  >=0 ? db->nextAu
278a7 74 6f 76 61 63 20 3a 0a 20 20 20 20 20 20 20 20  tovac :.        
278a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
278a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
278aa 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 47     sqlite3BtreeG
278ab 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61  etAutoVacuum(pMa
278ac 69 6e 29 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20  in));.#endif..  
278ad 2f 2a 20 42 65 67 69 6e 20 61 20 74 72 61 6e 73  /* Begin a trans
278ae 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d  action */.  rc =
278af 20 65 78 65 63 53 71 6c 28 64 62 2c 20 22 42 45   execSql(db, "BE
278b0 47 49 4e 20 45 58 43 4c 55 53 49 56 45 3b 22 29  GIN EXCLUSIVE;")
278b1 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
278b2 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64  TE_OK ) goto end
278b3 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f  _of_vacuum;..  /
278b4 2a 20 51 75 65 72 79 20 74 68 65 20 73 63 68 65  * Query the sche
278b5 6d 61 20 6f 66 20 74 68 65 20 6d 61 69 6e 20 64  ma of the main d
278b6 61 74 61 62 61 73 65 2e 20 43 72 65 61 74 65 20  atabase. Create 
278b7 61 20 6d 69 72 72 6f 72 20 73 63 68 65 6d 61 0a  a mirror schema.
278b8 20 20 2a 2a 20 69 6e 20 74 68 65 20 74 65 6d 70    ** in the temp
278b9 6f 72 61 72 79 20 64 61 74 61 62 61 73 65 2e 0a  orary database..
278ba 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65 78 65 63    */.  rc = exec
278bb 45 78 65 63 53 71 6c 28 64 62 2c 20 0a 20 20 20  ExecSql(db, .   
278bc 20 20 20 22 53 45 4c 45 43 54 20 27 43 52 45 41     "SELECT 'CREA
278bd 54 45 20 54 41 42 4c 45 20 76 61 63 75 75 6d 5f  TE TABLE vacuum_
278be 64 62 2e 27 20 7c 7c 20 73 75 62 73 74 72 28 73  db.' || substr(s
278bf 71 6c 2c 31 34 29 20 22 0a 20 20 20 20 20 20 22  ql,14) ".      "
278c0 20 20 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61    FROM sqlite_ma
278c1 73 74 65 72 20 57 48 45 52 45 20 74 79 70 65 3d  ster WHERE type=
278c2 27 74 61 62 6c 65 27 20 41 4e 44 20 6e 61 6d 65  'table' AND name
278c3 21 3d 27 73 71 6c 69 74 65 5f 73 65 71 75 65 6e  !='sqlite_sequen
278c4 63 65 27 22 0a 20 20 20 20 20 20 22 20 20 20 41  ce'".      "   A
278c5 4e 44 20 72 6f 6f 74 70 61 67 65 3e 30 22 0a 20  ND rootpage>0". 
278c6 20 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51   );.  if( rc!=SQ
278c7 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
278c8 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20  nd_of_vacuum;.  
278c9 72 63 20 3d 20 65 78 65 63 45 78 65 63 53 71 6c  rc = execExecSql
278ca 28 64 62 2c 20 0a 20 20 20 20 20 20 22 53 45 4c  (db, .      "SEL
278cb 45 43 54 20 27 43 52 45 41 54 45 20 49 4e 44 45  ECT 'CREATE INDE
278cc 58 20 76 61 63 75 75 6d 5f 64 62 2e 27 20 7c 7c  X vacuum_db.' ||
278cd 20 73 75 62 73 74 72 28 73 71 6c 2c 31 34 29 22   substr(sql,14)"
278ce 0a 20 20 20 20 20 20 22 20 20 46 52 4f 4d 20 73  .      "  FROM s
278cf 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
278d0 52 45 20 73 71 6c 20 4c 49 4b 45 20 27 43 52 45  RE sql LIKE 'CRE
278d1 41 54 45 20 49 4e 44 45 58 20 25 27 20 22 29 3b  ATE INDEX %' ");
278d2 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
278d3 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
278d4 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20  of_vacuum;.  rc 
278d5 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
278d6 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
278d7 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20   'CREATE UNIQUE 
278d8 49 4e 44 45 58 20 76 61 63 75 75 6d 5f 64 62 2e  INDEX vacuum_db.
278d9 27 20 7c 7c 20 73 75 62 73 74 72 28 73 71 6c 2c  ' || substr(sql,
278da 32 31 29 20 22 0a 20 20 20 20 20 20 22 20 20 46  21) ".      "  F
278db 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65  ROM sqlite_maste
278dc 72 20 57 48 45 52 45 20 73 71 6c 20 4c 49 4b 45  r WHERE sql LIKE
278dd 20 27 43 52 45 41 54 45 20 55 4e 49 51 55 45 20   'CREATE UNIQUE 
278de 49 4e 44 45 58 20 25 27 22 29 3b 0a 20 20 69 66  INDEX %'");.  if
278df 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
278e0 29 20 67 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61  ) goto end_of_va
278e1 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20 4c 6f 6f 70  cuum;..  /* Loop
278e2 20 74 68 72 6f 75 67 68 20 74 68 65 20 74 61 62   through the tab
278e3 6c 65 73 20 69 6e 20 74 68 65 20 6d 61 69 6e 20  les in the main 
278e4 64 61 74 61 62 61 73 65 2e 20 46 6f 72 20 65 61  database. For ea
278e5 63 68 2c 20 64 6f 0a 20 20 2a 2a 20 61 6e 20 22  ch, do.  ** an "
278e6 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75  INSERT INTO vacu
278e7 75 6d 5f 64 62 2e 78 78 78 20 53 45 4c 45 43 54  um_db.xxx SELECT
278e8 20 2a 20 46 52 4f 4d 20 78 78 78 3b 22 20 74 6f   * FROM xxx;" to
278e9 20 63 6f 70 79 0a 20 20 2a 2a 20 74 68 65 20 63   copy.  ** the c
278ea 6f 6e 74 65 6e 74 73 20 74 6f 20 74 68 65 20 74  ontents to the t
278eb 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73  emporary databas
278ec 65 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 65  e..  */.  rc = e
278ed 78 65 63 45 78 65 63 53 71 6c 28 64 62 2c 20 0a  xecExecSql(db, .
278ee 20 20 20 20 20 20 22 53 45 4c 45 43 54 20 27 49        "SELECT 'I
278ef 4e 53 45 52 54 20 49 4e 54 4f 20 76 61 63 75 75  NSERT INTO vacuu
278f0 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f 74 65 28  m_db.' || quote(
278f1 6e 61 6d 65 29 20 22 0a 20 20 20 20 20 20 22 7c  name) ".      "|
278f2 7c 20 27 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  | ' SELECT * FRO
278f3 4d 20 27 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d  M ' || quote(nam
278f4 65 29 20 7c 7c 20 27 3b 27 22 0a 20 20 20 20 20  e) || ';'".     
278f5 20 22 46 52 4f 4d 20 73 71 6c 69 74 65 5f 6d 61   "FROM sqlite_ma
278f6 73 74 65 72 20 22 0a 20 20 20 20 20 20 22 57 48  ster ".      "WH
278f7 45 52 45 20 74 79 70 65 20 3d 20 27 74 61 62 6c  ERE type = 'tabl
278f8 65 27 20 41 4e 44 20 6e 61 6d 65 21 3d 27 73 71  e' AND name!='sq
278f9 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27 20 22  lite_sequence' "
278fa 0a 20 20 20 20 20 20 22 20 20 41 4e 44 20 72 6f  .      "  AND ro
278fb 6f 74 70 61 67 65 3e 30 22 0a 0a 20 20 29 3b 0a  otpage>0"..  );.
278fc 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
278fd 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f 6f  _OK ) goto end_o
278fe 66 5f 76 61 63 75 75 6d 3b 0a 0a 20 20 2f 2a 20  f_vacuum;..  /* 
278ff 43 6f 70 79 20 6f 76 65 72 20 74 68 65 20 73 65  Copy over the se
27900 71 75 65 6e 63 65 20 74 61 62 6c 65 0a 20 20 2a  quence table.  *
27901 2f 0a 20 20 72 63 20 3d 20 65 78 65 63 45 78 65  /.  rc = execExe
27902 63 53 71 6c 28 64 62 2c 20 0a 20 20 20 20 20 20  cSql(db, .      
27903 22 53 45 4c 45 43 54 20 27 44 45 4c 45 54 45 20  "SELECT 'DELETE 
27904 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 27  FROM vacuum_db.'
27905 20 7c 7c 20 71 75 6f 74 65 28 6e 61 6d 65 29 20   || quote(name) 
27906 7c 7c 20 27 3b 27 20 22 0a 20 20 20 20 20 20 22  || ';' ".      "
27907 46 52 4f 4d 20 76 61 63 75 75 6d 5f 64 62 2e 73  FROM vacuum_db.s
27908 71 6c 69 74 65 5f 6d 61 73 74 65 72 20 57 48 45  qlite_master WHE
27909 52 45 20 6e 61 6d 65 3d 27 73 71 6c 69 74 65 5f  RE name='sqlite_
2790a 73 65 71 75 65 6e 63 65 27 20 22 0a 20 20 29 3b  sequence' ".  );
2790b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2790c 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e 64 5f  E_OK ) goto end_
2790d 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 72 63 20  of_vacuum;.  rc 
2790e 3d 20 65 78 65 63 45 78 65 63 53 71 6c 28 64 62  = execExecSql(db
2790f 2c 20 0a 20 20 20 20 20 20 22 53 45 4c 45 43 54  , .      "SELECT
27910 20 27 49 4e 53 45 52 54 20 49 4e 54 4f 20 76 61   'INSERT INTO va
27911 63 75 75 6d 5f 64 62 2e 27 20 7c 7c 20 71 75 6f  cuum_db.' || quo
27912 74 65 28 6e 61 6d 65 29 20 22 0a 20 20 20 20 20  te(name) ".     
27913 20 22 7c 7c 20 27 20 53 45 4c 45 43 54 20 2a 20   "|| ' SELECT * 
27914 46 52 4f 4d 20 27 20 7c 7c 20 71 75 6f 74 65 28  FROM ' || quote(
27915 6e 61 6d 65 29 20 7c 7c 20 27 3b 27 20 22 0a 20  name) || ';' ". 
27916 20 20 20 20 20 22 46 52 4f 4d 20 76 61 63 75 75       "FROM vacuu
27917 6d 5f 64 62 2e 73 71 6c 69 74 65 5f 6d 61 73 74  m_db.sqlite_mast
27918 65 72 20 57 48 45 52 45 20 6e 61 6d 65 3d 3d 27  er WHERE name=='
27919 73 71 6c 69 74 65 5f 73 65 71 75 65 6e 63 65 27  sqlite_sequence'
2791a 3b 22 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63  ;".  );.  if( rc
2791b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
2791c 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d  to end_of_vacuum
2791d 3b 0a 0a 0a 20 20 2f 2a 20 43 6f 70 79 20 74 68  ;...  /* Copy th
2791e 65 20 74 72 69 67 67 65 72 73 2c 20 76 69 65 77  e triggers, view
2791f 73 2c 20 61 6e 64 20 76 69 72 74 75 61 6c 20 74  s, and virtual t
27920 61 62 6c 65 73 20 66 72 6f 6d 20 74 68 65 20 6d  ables from the m
27921 61 69 6e 20 64 61 74 61 62 61 73 65 0a 20 20 2a  ain database.  *
27922 2a 20 6f 76 65 72 20 74 6f 20 74 68 65 20 74 65  * over to the te
27923 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
27924 2e 20 20 4e 6f 6e 65 20 6f 66 20 74 68 65 73 65  .  None of these
27925 20 6f 62 6a 65 63 74 73 20 68 61 73 20 61 6e 79   objects has any
27926 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  .  ** associated
27927 20 73 74 6f 72 61 67 65 2c 20 73 6f 20 61 6c 6c   storage, so all
27928 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 20 69   we have to do i
27929 73 20 63 6f 70 79 20 74 68 65 69 72 20 65 6e 74  s copy their ent
2792a 72 69 65 73 0a 20 20 2a 2a 20 66 72 6f 6d 20 74  ries.  ** from t
2792b 68 65 20 53 51 4c 49 54 45 5f 4d 41 53 54 45 52  he SQLITE_MASTER
2792c 20 74 61 62 6c 65 2e 0a 20 20 2a 2f 0a 20 20 72   table..  */.  r
2792d 63 20 3d 20 65 78 65 63 53 71 6c 28 64 62 2c 0a  c = execSql(db,.
2792e 20 20 20 20 20 20 22 49 4e 53 45 52 54 20 49 4e        "INSERT IN
2792f 54 4f 20 76 61 63 75 75 6d 5f 64 62 2e 73 71 6c  TO vacuum_db.sql
27930 69 74 65 5f 6d 61 73 74 65 72 20 22 0a 20 20 20  ite_master ".   
27931 20 20 20 22 20 20 53 45 4c 45 43 54 20 74 79 70     "  SELECT typ
27932 65 2c 20 6e 61 6d 65 2c 20 74 62 6c 5f 6e 61 6d  e, name, tbl_nam
27933 65 2c 20 72 6f 6f 74 70 61 67 65 2c 20 73 71 6c  e, rootpage, sql
27934 22 0a 20 20 20 20 20 20 22 20 20 20 20 46 52 4f  ".      "    FRO
27935 4d 20 73 71 6c 69 74 65 5f 6d 61 73 74 65 72 22  M sqlite_master"
27936 0a 20 20 20 20 20 20 22 20 20 20 57 48 45 52 45  .      "   WHERE
27937 20 74 79 70 65 3d 27 76 69 65 77 27 20 4f 52 20   type='view' OR 
27938 74 79 70 65 3d 27 74 72 69 67 67 65 72 27 22 0a  type='trigger'".
27939 20 20 20 20 20 20 22 20 20 20 20 20 20 4f 52 20        "      OR 
2793a 28 74 79 70 65 3d 27 74 61 62 6c 65 27 20 41 4e  (type='table' AN
2793b 44 20 72 6f 6f 74 70 61 67 65 3d 30 29 22 0a 20  D rootpage=0)". 
2793c 20 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20 67   );.  if( rc ) g
2793d 6f 74 6f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75  oto end_of_vacuu
2793e 6d 3b 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69 73  m;..  /* At this
2793f 20 70 6f 69 6e 74 2c 20 75 6e 6c 65 73 73 20 74   point, unless t
27940 68 65 20 6d 61 69 6e 20 64 62 20 77 61 73 20 63  he main db was c
27941 6f 6d 70 6c 65 74 65 6c 79 20 65 6d 70 74 79 2c  ompletely empty,
27942 20 74 68 65 72 65 20 69 73 20 6e 6f 77 20 61 0a   there is now a.
27943 20 20 2a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e    ** transaction
27944 20 6f 70 65 6e 20 6f 6e 20 74 68 65 20 76 61 63   open on the vac
27945 75 75 6d 20 64 61 74 61 62 61 73 65 2c 20 62 75  uum database, bu
27946 74 20 6e 6f 74 20 6f 6e 20 74 68 65 20 6d 61 69  t not on the mai
27947 6e 20 64 61 74 61 62 61 73 65 2e 0a 20 20 2a 2a  n database..  **
27948 20 4f 70 65 6e 20 61 20 62 74 72 65 65 20 6c 65   Open a btree le
27949 76 65 6c 20 74 72 61 6e 73 61 63 74 69 6f 6e 20  vel transaction 
2794a 6f 6e 20 74 68 65 20 6d 61 69 6e 20 64 61 74 61  on the main data
2794b 62 61 73 65 2e 20 54 68 69 73 20 61 6c 6c 6f 77  base. This allow
2794c 73 20 61 0a 20 20 2a 2a 20 63 61 6c 6c 20 74 6f  s a.  ** call to
2794d 20 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70   sqlite3BtreeCop
2794e 79 46 69 6c 65 28 29 2e 20 54 68 65 20 6d 61 69  yFile(). The mai
2794f 6e 20 64 61 74 61 62 61 73 65 20 62 74 72 65 65  n database btree
27950 20 6c 65 76 65 6c 0a 20 20 2a 2a 20 74 72 61 6e   level.  ** tran
27951 73 61 63 74 69 6f 6e 20 69 73 20 74 68 65 6e 20  saction is then 
27952 63 6f 6d 6d 69 74 74 65 64 2c 20 73 6f 20 74 68  committed, so th
27953 65 20 53 51 4c 20 6c 65 76 65 6c 20 6e 65 76 65  e SQL level neve
27954 72 20 6b 6e 6f 77 73 20 69 74 20 77 61 73 0a 20  r knows it was. 
27955 20 2a 2a 20 6f 70 65 6e 65 64 20 66 6f 72 20 77   ** opened for w
27956 72 69 74 69 6e 67 2e 20 54 68 69 73 20 77 61 79  riting. This way
27957 2c 20 74 68 65 20 53 51 4c 20 74 72 61 6e 73 61  , the SQL transa
27958 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20 63 72  ction used to cr
27959 65 61 74 65 20 74 68 65 0a 20 20 2a 2a 20 74 65  eate the.  ** te
2795a 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61 73 65  mporary database
2795b 20 6e 65 76 65 72 20 6e 65 65 64 73 20 74 6f 20   never needs to 
2795c 62 65 20 63 6f 6d 6d 69 74 74 65 64 2e 0a 20 20  be committed..  
2795d 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  */.  if( rc==SQL
2795e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 75 33  ITE_OK ){.    u3
2795f 32 20 6d 65 74 61 3b 0a 20 20 20 20 69 6e 74 20  2 meta;.    int 
27960 69 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 69 73 20  i;..    /* This 
27961 61 72 72 61 79 20 64 65 74 65 72 6d 69 6e 65 73  array determines
27962 20 77 68 69 63 68 20 6d 65 74 61 20 6d 65 74 61   which meta meta
27963 20 76 61 6c 75 65 73 20 61 72 65 20 70 72 65 73   values are pres
27964 65 72 76 65 64 20 69 6e 20 74 68 65 0a 20 20 20  erved in the.   
27965 20 2a 2a 20 76 61 63 75 75 6d 2e 20 20 45 76 65   ** vacuum.  Eve
27966 6e 20 65 6e 74 72 69 65 73 20 61 72 65 20 74 68  n entries are th
27967 65 20 6d 65 74 61 20 76 61 6c 75 65 20 6e 75 6d  e meta value num
27968 62 65 72 20 61 6e 64 20 6f 64 64 20 65 6e 74 72  ber and odd entr
27969 69 65 73 0a 20 20 20 20 2a 2a 20 61 72 65 20 61  ies.    ** are a
2796a 6e 20 69 6e 63 72 65 6d 65 6e 74 20 74 6f 20 61  n increment to a
2796b 70 70 6c 79 20 74 6f 20 74 68 65 20 6d 65 74 61  pply to the meta
2796c 20 76 61 6c 75 65 20 61 66 74 65 72 20 74 68 65   value after the
2796d 20 76 61 63 75 75 6d 2e 0a 20 20 20 20 2a 2a 20   vacuum..    ** 
2796e 54 68 65 20 69 6e 63 72 65 6d 65 6e 74 20 69 73  The increment is
2796f 20 75 73 65 64 20 74 6f 20 69 6e 63 72 65 61 73   used to increas
27970 65 20 74 68 65 20 73 63 68 65 6d 61 20 63 6f 6f  e the schema coo
27971 6b 69 65 20 73 6f 20 74 68 61 74 20 6f 74 68 65  kie so that othe
27972 72 0a 20 20 20 20 2a 2a 20 63 6f 6e 6e 65 63 74  r.    ** connect
27973 69 6f 6e 73 20 74 6f 20 74 68 65 20 73 61 6d 65  ions to the same
27974 20 64 61 74 61 62 61 73 65 20 77 69 6c 6c 20 6b   database will k
27975 6e 6f 77 20 74 6f 20 72 65 72 65 61 64 20 74 68  now to reread th
27976 65 20 73 63 68 65 6d 61 2e 0a 20 20 20 20 2a 2f  e schema..    */
27977 0a 20 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73  .    static cons
27978 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20  t unsigned char 
27979 61 43 6f 70 79 5b 5d 20 3d 20 7b 0a 20 20 20 20  aCopy[] = {.    
2797a 20 20 20 31 2c 20 31 2c 20 20 20 20 2f 2a 20 41     1, 1,    /* A
2797b 64 64 20 6f 6e 65 20 74 6f 20 74 68 65 20 6f 6c  dd one to the ol
2797c 64 20 73 63 68 65 6d 61 20 63 6f 6f 6b 69 65 20  d schema cookie 
2797d 2a 2f 0a 20 20 20 20 20 20 20 33 2c 20 30 2c 20  */.       3, 0, 
2797e 20 20 20 2f 2a 20 50 72 65 73 65 72 76 65 20 74     /* Preserve t
2797f 68 65 20 64 65 66 61 75 6c 74 20 70 61 67 65 20  he default page 
27980 63 61 63 68 65 20 73 69 7a 65 20 2a 2f 0a 20 20  cache size */.  
27981 20 20 20 20 20 35 2c 20 30 2c 20 20 20 20 2f 2a       5, 0,    /*
27982 20 50 72 65 73 65 72 76 65 20 74 68 65 20 64 65   Preserve the de
27983 66 61 75 6c 74 20 74 65 78 74 20 65 6e 63 6f 64  fault text encod
27984 69 6e 67 20 2a 2f 0a 20 20 20 20 20 20 20 36 2c  ing */.       6,
27985 20 30 2c 20 20 20 20 2f 2a 20 50 72 65 73 65 72   0,    /* Preser
27986 76 65 20 74 68 65 20 75 73 65 72 20 76 65 72 73  ve the user vers
27987 69 6f 6e 20 2a 2f 0a 20 20 20 20 7d 3b 0a 0a 20  ion */.    };.. 
27988 20 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71     assert( 1==sq
27989 6c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72  lite3BtreeIsInTr
2798a 61 6e 73 28 70 54 65 6d 70 29 20 29 3b 0a 20 20  ans(pTemp) );.  
2798b 20 20 61 73 73 65 72 74 28 20 31 3d 3d 73 71 6c    assert( 1==sql
2798c 69 74 65 33 42 74 72 65 65 49 73 49 6e 54 72 61  ite3BtreeIsInTra
2798d 6e 73 28 70 4d 61 69 6e 29 20 29 3b 0a 0a 20 20  ns(pMain) );..  
2798e 20 20 2f 2a 20 43 6f 70 79 20 42 74 72 65 65 20    /* Copy Btree 
2798f 6d 65 74 61 20 76 61 6c 75 65 73 20 2a 2f 0a 20  meta values */. 
27990 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 41 72     for(i=0; i<Ar
27991 72 61 79 53 69 7a 65 28 61 43 6f 70 79 29 3b 20  raySize(aCopy); 
27992 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 72 63 20  i+=2){.      rc 
27993 3d 20 73 71 6c 69 74 65 33 42 74 72 65 65 47 65  = sqlite3BtreeGe
27994 74 4d 65 74 61 28 70 4d 61 69 6e 2c 20 61 43 6f  tMeta(pMain, aCo
27995 70 79 5b 69 5d 2c 20 26 6d 65 74 61 29 3b 0a 20  py[i], &meta);. 
27996 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c       if( rc!=SQL
27997 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 6e  ITE_OK ) goto en
27998 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a 20 20 20  d_of_vacuum;.   
27999 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42     rc = sqlite3B
2799a 74 72 65 65 55 70 64 61 74 65 4d 65 74 61 28 70  treeUpdateMeta(p
2799b 54 65 6d 70 2c 20 61 43 6f 70 79 5b 69 5d 2c 20  Temp, aCopy[i], 
2799c 6d 65 74 61 2b 61 43 6f 70 79 5b 69 2b 31 5d 29  meta+aCopy[i+1])
2799d 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 21 3d  ;.      if( rc!=
2799e 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2799f 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
279a0 20 20 20 20 7d 0a 0a 20 20 20 20 72 63 20 3d 20      }..    rc = 
279a1 73 71 6c 69 74 65 33 42 74 72 65 65 43 6f 70 79  sqlite3BtreeCopy
279a2 46 69 6c 65 28 70 4d 61 69 6e 2c 20 70 54 65 6d  File(pMain, pTem
279a3 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  p);.    if( rc!=
279a4 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
279a5 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
279a6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
279a7 42 74 72 65 65 43 6f 6d 6d 69 74 28 70 54 65 6d  BtreeCommit(pTem
279a8 70 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  p);.    if( rc!=
279a9 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
279aa 20 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3b 0a   end_of_vacuum;.
279ab 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
279ac 4d 49 54 5f 41 55 54 4f 56 41 43 55 55 4d 0a 20  MIT_AUTOVACUUM. 
279ad 20 20 20 73 71 6c 69 74 65 33 42 74 72 65 65 53     sqlite3BtreeS
279ae 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 4d 61  etAutoVacuum(pMa
279af 69 6e 2c 20 73 71 6c 69 74 65 33 42 74 72 65 65  in, sqlite3Btree
279b0 47 65 74 41 75 74 6f 56 61 63 75 75 6d 28 70 54  GetAutoVacuum(pT
279b1 65 6d 70 29 29 3b 0a 23 65 6e 64 69 66 0a 20 20  emp));.#endif.  
279b2 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
279b3 72 65 65 43 6f 6d 6d 69 74 28 70 4d 61 69 6e 29  reeCommit(pMain)
279b4 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d  ;.  }..  if( rc=
279b5 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
279b6 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
279b7 72 65 65 53 65 74 50 61 67 65 53 69 7a 65 28 70  reeSetPageSize(p
279b8 4d 61 69 6e 2c 20 73 71 6c 69 74 65 33 42 74 72  Main, sqlite3Btr
279b9 65 65 47 65 74 50 61 67 65 53 69 7a 65 28 70 54  eeGetPageSize(pT
279ba 65 6d 70 29 2c 20 6e 52 65 73 29 3b 0a 20 20 7d  emp), nRes);.  }
279bb 0a 0a 65 6e 64 5f 6f 66 5f 76 61 63 75 75 6d 3a  ..end_of_vacuum:
279bc 0a 20 20 2f 2a 20 52 65 73 74 6f 72 65 20 74 68  .  /* Restore th
279bd 65 20 6f 72 69 67 69 6e 61 6c 20 76 61 6c 75 65  e original value
279be 20 6f 66 20 64 62 2d 3e 66 6c 61 67 73 20 2a 2f   of db->flags */
279bf 0a 20 20 64 62 2d 3e 66 6c 61 67 73 20 3d 20 73  .  db->flags = s
279c0 61 76 65 64 5f 66 6c 61 67 73 3b 0a 20 20 64 62  aved_flags;.  db
279c1 2d 3e 6e 43 68 61 6e 67 65 20 3d 20 73 61 76 65  ->nChange = save
279c2 64 5f 6e 43 68 61 6e 67 65 3b 0a 20 20 64 62 2d  d_nChange;.  db-
279c3 3e 6e 54 6f 74 61 6c 43 68 61 6e 67 65 20 3d 20  >nTotalChange = 
279c4 73 61 76 65 64 5f 6e 54 6f 74 61 6c 43 68 61 6e  saved_nTotalChan
279c5 67 65 3b 0a 0a 20 20 2f 2a 20 43 75 72 72 65 6e  ge;..  /* Curren
279c6 74 6c 79 20 74 68 65 72 65 20 69 73 20 61 6e 20  tly there is an 
279c7 53 51 4c 20 6c 65 76 65 6c 20 74 72 61 6e 73 61  SQL level transa
279c8 63 74 69 6f 6e 20 6f 70 65 6e 20 6f 6e 20 74 68  ction open on th
279c9 65 20 76 61 63 75 75 6d 0a 20 20 2a 2a 20 64 61  e vacuum.  ** da
279ca 74 61 62 61 73 65 2e 20 4e 6f 20 6c 6f 63 6b 73  tabase. No locks
279cb 20 61 72 65 20 68 65 6c 64 20 6f 6e 20 61 6e 79   are held on any
279cc 20 6f 74 68 65 72 20 66 69 6c 65 73 20 28 73 69   other files (si
279cd 6e 63 65 20 74 68 65 20 6d 61 69 6e 20 66 69 6c  nce the main fil
279ce 65 0a 20 20 2a 2a 20 77 61 73 20 63 6f 6d 6d 69  e.  ** was commi
279cf 74 74 65 64 20 61 74 20 74 68 65 20 62 74 72 65  tted at the btre
279d0 65 20 6c 65 76 65 6c 29 2e 20 53 6f 20 69 74 20  e level). So it 
279d1 73 61 66 65 20 74 6f 20 65 6e 64 20 74 68 65 20  safe to end the 
279d2 74 72 61 6e 73 61 63 74 69 6f 6e 0a 20 20 2a 2a  transaction.  **
279d3 20 62 79 20 6d 61 6e 75 61 6c 6c 79 20 73 65 74   by manually set
279d4 74 69 6e 67 20 74 68 65 20 61 75 74 6f 43 6f 6d  ting the autoCom
279d5 6d 69 74 20 66 6c 61 67 20 74 6f 20 74 72 75 65  mit flag to true
279d6 20 61 6e 64 20 64 65 74 61 63 68 69 6e 67 20 74   and detaching t
279d7 68 65 0a 20 20 2a 2a 20 76 61 63 75 75 6d 20 64  he.  ** vacuum d
279d8 61 74 61 62 61 73 65 2e 20 54 68 65 20 76 61 63  atabase. The vac
279d9 75 75 6d 5f 64 62 20 6a 6f 75 72 6e 61 6c 20 66  uum_db journal f
279da 69 6c 65 20 69 73 20 64 65 6c 65 74 65 64 20 77  ile is deleted w
279db 68 65 6e 20 74 68 65 20 70 61 67 65 72 0a 20 20  hen the pager.  
279dc 2a 2a 20 69 73 20 63 6c 6f 73 65 64 20 62 79 20  ** is closed by 
279dd 74 68 65 20 44 45 54 41 43 48 2e 0a 20 20 2a 2f  the DETACH..  */
279de 0a 20 20 64 62 2d 3e 61 75 74 6f 43 6f 6d 6d 69  .  db->autoCommi
279df 74 20 3d 20 31 3b 0a 0a 20 20 69 66 28 20 70 44  t = 1;..  if( pD
279e0 62 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33  b ){.    sqlite3
279e1 42 74 72 65 65 43 6c 6f 73 65 28 70 44 62 2d 3e  BtreeClose(pDb->
279e2 70 42 74 29 3b 0a 20 20 20 20 70 44 62 2d 3e 70  pBt);.    pDb->p
279e3 42 74 20 3d 20 30 3b 0a 20 20 20 20 70 44 62 2d  Bt = 0;.    pDb-
279e4 3e 70 53 63 68 65 6d 61 20 3d 20 30 3b 0a 20 20  >pSchema = 0;.  
279e5 7d 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65  }..  sqlite3Rese
279e6 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28  tInternalSchema(
279e7 64 62 2c 20 30 29 3b 0a 0a 20 20 72 65 74 75 72  db, 0);..  retur
279e8 6e 20 72 63 3b 0a 7d 0a 23 65 6e 64 69 66 20 20  n rc;.}.#endif  
279e9 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  /* SQLITE_OMIT_V
279ea 41 43 55 55 4d 20 26 26 20 53 51 4c 49 54 45 5f  ACUUM && SQLITE_
279eb 4f 4d 49 54 5f 41 54 54 41 43 48 20 2a 2f 0a 0a  OMIT_ATTACH */..
279ec 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
279ed 45 6e 64 20 6f 66 20 76 61 63 75 75 6d 2e 63 20  End of vacuum.c 
279ee 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
279ef 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
279f0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
279f1 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
279f2 42 65 67 69 6e 20 66 69 6c 65 20 76 74 61 62 2e  Begin file vtab.
279f3 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
279f4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
279f5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
279f6 2f 2a 0a 2a 2a 20 32 30 30 36 20 4a 75 6e 65 20  /*.** 2006 June 
279f7 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74  10.**.** The aut
279f8 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f  hor disclaims co
279f9 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20  pyright to this 
279fa 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e  source code.  In
279fb 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c   place of.** a l
279fc 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72  egal notice, her
279fd 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a  e is a blessing:
279fe 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  .**.**    May yo
279ff 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f  u do good and no
27a00 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61  t evil..**    Ma
27a01 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69  y you find forgi
27a02 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73  veness for yours
27a03 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20  elf and forgive 
27a04 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61  others..**    Ma
27a05 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65  y you share free
27a06 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67  ly, never taking
27a07 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67   more than you g
27a08 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a  ive..**.********
27a09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a0c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
27a0d 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
27a0e 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 75 73 65  ontains code use
27a0f 64 20 74 6f 20 68 65 6c 70 20 69 6d 70 6c 65 6d  d to help implem
27a10 65 6e 74 20 76 69 72 74 75 61 6c 20 74 61 62 6c  ent virtual tabl
27a11 65 73 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20 76  es..**.** $Id: v
27a12 74 61 62 2e 63 2c 76 20 31 2e 37 38 20 32 30 30  tab.c,v 1.78 200
27a13 38 2f 31 31 2f 31 33 20 31 39 3a 31 32 3a 33 36  8/11/13 19:12:36
27a14 20 64 61 6e 69 65 6c 6b 31 39 37 37 20 45 78 70   danielk1977 Exp
27a15 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53 51   $.*/.#ifndef SQ
27a16 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41  LITE_OMIT_VIRTUA
27a17 4c 54 41 42 4c 45 0a 0a 73 74 61 74 69 63 20 69  LTABLE..static i
27a18 6e 74 20 63 72 65 61 74 65 4d 6f 64 75 6c 65 28  nt createModule(
27a19 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
27a1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a1b 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20 69     /* Database i
27a1c 6e 20 77 68 69 63 68 20 6d 6f 64 75 6c 65 20 69  n which module i
27a1d 73 20 72 65 67 69 73 74 65 72 65 64 20 2a 2f 0a  s registered */.
27a1e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e    const char *zN
27a1f 61 6d 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ame,            
27a20 20 20 2f 2a 20 4e 61 6d 65 20 61 73 73 69 67 6e    /* Name assign
27a21 65 64 20 74 6f 20 74 68 69 73 20 6d 6f 64 75 6c  ed to this modul
27a22 65 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 73 71 6c  e */.  const sql
27a23 69 74 65 33 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f  ite3_module *pMo
27a24 64 75 6c 65 2c 20 20 2f 2a 20 54 68 65 20 64 65  dule,  /* The de
27a25 66 69 6e 69 74 69 6f 6e 20 6f 66 20 74 68 65 20  finition of the 
27a26 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 76 6f 69 64  module */.  void
27a27 20 2a 70 41 75 78 2c 20 20 20 20 20 20 20 20 20   *pAux,         
27a28 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43              /* C
27a29 6f 6e 74 65 78 74 20 70 6f 69 6e 74 65 72 20 66  ontext pointer f
27a2a 6f 72 20 78 43 72 65 61 74 65 2f 78 43 6f 6e 6e  or xCreate/xConn
27a2b 65 63 74 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a  ect */.  void (*
27a2c 78 44 65 73 74 72 6f 79 29 28 76 6f 69 64 20 2a  xDestroy)(void *
27a2d 29 20 20 20 20 20 20 20 20 2f 2a 20 4d 6f 64 75  )        /* Modu
27a2e 6c 65 20 64 65 73 74 72 75 63 74 6f 72 20 66 75  le destructor fu
27a2f 6e 63 74 69 6f 6e 20 2a 2f 0a 29 20 7b 0a 20 20  nction */.) {.  
27a30 69 6e 74 20 72 63 2c 20 6e 4e 61 6d 65 3b 0a 20  int rc, nName;. 
27a31 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 0a   Module *pMod;..
27a32 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
27a33 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29  enter(db->mutex)
27a34 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c  ;.  nName = strl
27a35 65 6e 28 7a 4e 61 6d 65 29 3b 0a 20 20 70 4d 6f  en(zName);.  pMo
27a36 64 20 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71  d = (Module *)sq
27a37 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52 61 77  lite3DbMallocRaw
27a38 28 64 62 2c 20 73 69 7a 65 6f 66 28 4d 6f 64 75  (db, sizeof(Modu
27a39 6c 65 29 20 2b 20 6e 4e 61 6d 65 20 2b 20 31 29  le) + nName + 1)
27a3a 3b 0a 20 20 69 66 28 20 70 4d 6f 64 20 29 7b 0a  ;.  if( pMod ){.
27a3b 20 20 20 20 4d 6f 64 75 6c 65 20 2a 70 44 65 6c      Module *pDel
27a3c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 43 6f 70  ;.    char *zCop
27a3d 79 20 3d 20 28 63 68 61 72 20 2a 29 28 26 70 4d  y = (char *)(&pM
27a3e 6f 64 5b 31 5d 29 3b 0a 20 20 20 20 6d 65 6d 63  od[1]);.    memc
27a3f 70 79 28 7a 43 6f 70 79 2c 20 7a 4e 61 6d 65 2c  py(zCopy, zName,
27a40 20 6e 4e 61 6d 65 2b 31 29 3b 0a 20 20 20 20 70   nName+1);.    p
27a41 4d 6f 64 2d 3e 7a 4e 61 6d 65 20 3d 20 7a 43 6f  Mod->zName = zCo
27a42 70 79 3b 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 4d  py;.    pMod->pM
27a43 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 75 6c 65 3b  odule = pModule;
27a44 0a 20 20 20 20 70 4d 6f 64 2d 3e 70 41 75 78 20  .    pMod->pAux 
27a45 3d 20 70 41 75 78 3b 0a 20 20 20 20 70 4d 6f 64  = pAux;.    pMod
27a46 2d 3e 78 44 65 73 74 72 6f 79 20 3d 20 78 44 65  ->xDestroy = xDe
27a47 73 74 72 6f 79 3b 0a 20 20 20 20 70 44 65 6c 20  stroy;.    pDel 
27a48 3d 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  = (Module *)sqli
27a49 74 65 33 48 61 73 68 49 6e 73 65 72 74 28 26 64  te3HashInsert(&d
27a4a 62 2d 3e 61 4d 6f 64 75 6c 65 2c 20 7a 43 6f 70  b->aModule, zCop
27a4b 79 2c 20 6e 4e 61 6d 65 2c 20 28 76 6f 69 64 2a  y, nName, (void*
27a4c 29 70 4d 6f 64 29 3b 0a 20 20 20 20 69 66 28 20  )pMod);.    if( 
27a4d 70 44 65 6c 20 26 26 20 70 44 65 6c 2d 3e 78 44  pDel && pDel->xD
27a4e 65 73 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20  estroy ){.      
27a4f 70 44 65 6c 2d 3e 78 44 65 73 74 72 6f 79 28 70  pDel->xDestroy(p
27a50 44 65 6c 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20  Del->pAux);.    
27a51 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 44 62 46  }.    sqlite3DbF
27a52 72 65 65 28 64 62 2c 20 70 44 65 6c 29 3b 0a 20  ree(db, pDel);. 
27a53 20 20 20 69 66 28 20 70 44 65 6c 3d 3d 70 4d 6f     if( pDel==pMo
27a54 64 20 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d  d ){.      db->m
27a55 61 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b  allocFailed = 1;
27a56 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
27a57 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
27a58 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
27a59 7d 65 6c 73 65 20 69 66 28 20 78 44 65 73 74 72  }else if( xDestr
27a5a 6f 79 20 29 7b 0a 20 20 20 20 78 44 65 73 74 72  oy ){.    xDestr
27a5b 6f 79 28 70 41 75 78 29 3b 0a 20 20 7d 0a 20 20  oy(pAux);.  }.  
27a5c 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
27a5d 78 69 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f  xit(db, SQLITE_O
27a5e 4b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  K);.  sqlite3_mu
27a5f 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
27a60 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  tex);.  return r
27a61 63 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  c;.}.../*.** Ext
27a62 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69  ernal API functi
27a63 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  on used to creat
27a64 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d  e a new virtual-
27a65 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  table module..*/
27a66 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
27a67 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
27a68 6f 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33  odule(.  sqlite3
27a69 20 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20   *db,           
27a6a 20 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61           /* Data
27a6b 62 61 73 65 20 69 6e 20 77 68 69 63 68 20 6d 6f  base in which mo
27a6c 64 75 6c 65 20 69 73 20 72 65 67 69 73 74 65 72  dule is register
27a6d 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ed */.  const ch
27a6e 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20 20 20 20  ar *zName,      
27a6f 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20          /* Name 
27a70 61 73 73 69 67 6e 65 64 20 74 6f 20 74 68 69 73  assigned to this
27a71 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20 63 6f 6e   module */.  con
27a72 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
27a73 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20 2f 2a 20  e *pModule,  /* 
27a74 54 68 65 20 64 65 66 69 6e 69 74 69 6f 6e 20 6f  The definition o
27a75 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 2a 2f 0a  f the module */.
27a76 20 20 76 6f 69 64 20 2a 70 41 75 78 20 20 20 20    void *pAux    
27a77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a78 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20 70 6f 69    /* Context poi
27a79 6e 74 65 72 20 66 6f 72 20 78 43 72 65 61 74 65  nter for xCreate
27a7a 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a 29 7b 0a  /xConnect */.){.
27a7b 20 20 72 65 74 75 72 6e 20 63 72 65 61 74 65 4d    return createM
27a7c 6f 64 75 6c 65 28 64 62 2c 20 7a 4e 61 6d 65 2c  odule(db, zName,
27a7d 20 70 4d 6f 64 75 6c 65 2c 20 70 41 75 78 2c 20   pModule, pAux, 
27a7e 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  0);.}../*.** Ext
27a7f 65 72 6e 61 6c 20 41 50 49 20 66 75 6e 63 74 69  ernal API functi
27a80 6f 6e 20 75 73 65 64 20 74 6f 20 63 72 65 61 74  on used to creat
27a81 65 20 61 20 6e 65 77 20 76 69 72 74 75 61 6c 2d  e a new virtual-
27a82 74 61 62 6c 65 20 6d 6f 64 75 6c 65 2e 0a 2a 2f  table module..*/
27a83 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
27a84 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d  sqlite3_create_m
27a85 6f 64 75 6c 65 5f 76 32 28 0a 20 20 73 71 6c 69  odule_v2(.  sqli
27a86 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20 20 20  te3 *db,        
27a87 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
27a88 61 74 61 62 61 73 65 20 69 6e 20 77 68 69 63 68  atabase in which
27a89 20 6d 6f 64 75 6c 65 20 69 73 20 72 65 67 69 73   module is regis
27a8a 74 65 72 65 64 20 2a 2f 0a 20 20 63 6f 6e 73 74  tered */.  const
27a8b 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 20 20   char *zName,   
27a8c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61             /* Na
27a8d 6d 65 20 61 73 73 69 67 6e 65 64 20 74 6f 20 74  me assigned to t
27a8e 68 69 73 20 6d 6f 64 75 6c 65 20 2a 2f 0a 20 20  his module */.  
27a8f 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
27a90 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 2c 20 20  dule *pModule,  
27a91 2f 2a 20 54 68 65 20 64 65 66 69 6e 69 74 69 6f  /* The definitio
27a92 6e 20 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20  n of the module 
27a93 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 75 78 2c  */.  void *pAux,
27a94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27a95 20 20 20 20 20 2f 2a 20 43 6f 6e 74 65 78 74 20       /* Context 
27a96 70 6f 69 6e 74 65 72 20 66 6f 72 20 78 43 72 65  pointer for xCre
27a97 61 74 65 2f 78 43 6f 6e 6e 65 63 74 20 2a 2f 0a  ate/xConnect */.
27a98 20 20 76 6f 69 64 20 28 2a 78 44 65 73 74 72 6f    void (*xDestro
27a99 79 29 28 76 6f 69 64 20 2a 29 20 20 20 20 20 20  y)(void *)      
27a9a 20 20 2f 2a 20 4d 6f 64 75 6c 65 20 64 65 73 74    /* Module dest
27a9b 72 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 20  ructor function 
27a9c 2a 2f 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 63  */.){.  return c
27a9d 72 65 61 74 65 4d 6f 64 75 6c 65 28 64 62 2c 20  reateModule(db, 
27a9e 7a 4e 61 6d 65 2c 20 70 4d 6f 64 75 6c 65 2c 20  zName, pModule, 
27a9f 70 41 75 78 2c 20 78 44 65 73 74 72 6f 79 29 3b  pAux, xDestroy);
27aa0 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 6f 63 6b 20 74  .}../*.** Lock t
27aa1 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
27aa2 20 73 6f 20 74 68 61 74 20 69 74 20 63 61 6e 6e   so that it cann
27aa3 6f 74 20 62 65 20 64 69 73 63 6f 6e 6e 65 63 74  ot be disconnect
27aa4 65 64 2e 0a 2a 2a 20 4c 6f 63 6b 73 20 6e 65 73  ed..** Locks nes
27aa5 74 2e 20 20 45 76 65 72 79 20 6c 6f 63 6b 20 73  t.  Every lock s
27aa6 68 6f 75 6c 64 20 68 61 76 65 20 61 20 63 6f 72  hould have a cor
27aa7 72 65 73 70 6f 6e 64 69 6e 67 20 75 6e 6c 6f 63  responding unloc
27aa8 6b 2e 0a 2a 2a 20 49 66 20 61 6e 20 75 6e 6c 6f  k..** If an unlo
27aa9 63 6b 20 69 73 20 6f 6d 69 74 74 65 64 2c 20 72  ck is omitted, r
27aaa 65 73 6f 75 72 63 65 73 20 6c 65 61 6b 73 20 77  esources leaks w
27aab 69 6c 6c 20 6f 63 63 75 72 2e 20 20 0a 2a 2a 0a  ill occur.  .**.
27aac 2a 2a 20 49 66 20 61 20 64 69 73 63 6f 6e 6e 65  ** If a disconne
27aad 63 74 20 69 73 20 61 74 74 65 6d 70 74 65 64 20  ct is attempted 
27aae 77 68 69 6c 65 20 61 20 76 69 72 74 75 61 6c 20  while a virtual 
27aaf 74 61 62 6c 65 20 69 73 20 6c 6f 63 6b 65 64 2c  table is locked,
27ab0 0a 2a 2a 20 74 68 65 20 64 69 73 63 6f 6e 6e 65  .** the disconne
27ab1 63 74 20 69 73 20 64 65 66 65 72 72 65 64 20 75  ct is deferred u
27ab2 6e 74 69 6c 20 61 6c 6c 20 6c 6f 63 6b 73 20 68  ntil all locks h
27ab3 61 76 65 20 62 65 65 6e 20 72 65 6d 6f 76 65 64  ave been removed
27ab4 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
27ab5 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
27ab6 56 74 61 62 4c 6f 63 6b 28 73 71 6c 69 74 65 33  VtabLock(sqlite3
27ab7 5f 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20  _vtab *pVtab){. 
27ab8 20 70 56 74 61 62 2d 3e 6e 52 65 66 2b 2b 3b 0a   pVtab->nRef++;.
27ab9 7d 0a 0a 2f 2a 0a 2a 2a 20 55 6e 6c 6f 63 6b 20  }../*.** Unlock 
27aba 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e  a virtual table.
27abb 20 20 57 68 65 6e 20 74 68 65 20 6c 61 73 74 20    When the last 
27abc 6c 6f 63 6b 20 69 73 20 72 65 6d 6f 76 65 64 2c  lock is removed,
27abd 0a 2a 2a 20 64 69 73 63 6f 6e 6e 65 63 74 20 74  .** disconnect t
27abe 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
27abf 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
27ac0 41 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33  ATE void sqlite3
27ac1 56 74 61 62 55 6e 6c 6f 63 6b 28 73 71 6c 69 74  VtabUnlock(sqlit
27ac2 65 33 20 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f  e3 *db, sqlite3_
27ac3 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20  vtab *pVtab){.  
27ac4 70 56 74 61 62 2d 3e 6e 52 65 66 2d 2d 3b 0a 20  pVtab->nRef--;. 
27ac5 20 61 73 73 65 72 74 28 64 62 29 3b 0a 20 20 61   assert(db);.  a
27ac6 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 53 61  ssert( sqlite3Sa
27ac7 66 65 74 79 43 68 65 63 6b 4f 6b 28 64 62 29 20  fetyCheckOk(db) 
27ac8 29 3b 0a 20 20 69 66 28 20 70 56 74 61 62 2d 3e  );.  if( pVtab->
27ac9 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20 69  nRef==0 ){.    i
27aca 66 28 20 64 62 2d 3e 6d 61 67 69 63 3d 3d 53 51  f( db->magic==SQ
27acb 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59 20  LITE_MAGIC_BUSY 
27acc 29 7b 0a 20 20 20 20 20 20 28 76 6f 69 64 29 73  ){.      (void)s
27acd 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66 66 28  qlite3SafetyOff(
27ace 64 62 29 3b 0a 20 20 20 20 20 20 70 56 74 61 62  db);.      pVtab
27acf 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44 69 73 63  ->pModule->xDisc
27ad0 6f 6e 6e 65 63 74 28 70 56 74 61 62 29 3b 0a 20  onnect(pVtab);. 
27ad1 20 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74       (void)sqlit
27ad2 65 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a  e3SafetyOn(db);.
27ad3 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20      } else {.   
27ad4 20 20 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c     pVtab->pModul
27ad5 65 2d 3e 78 44 69 73 63 6f 6e 6e 65 63 74 28 70  e->xDisconnect(p
27ad6 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  Vtab);.    }.  }
27ad7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72 20  .}../*.** Clear 
27ad8 61 6e 79 20 61 6e 64 20 61 6c 6c 20 76 69 72 74  any and all virt
27ad9 75 61 6c 2d 74 61 62 6c 65 20 69 6e 66 6f 72 6d  ual-table inform
27ada 61 74 69 6f 6e 20 66 72 6f 6d 20 74 68 65 20 54  ation from the T
27adb 61 62 6c 65 20 72 65 63 6f 72 64 2e 0a 2a 2a 20  able record..** 
27adc 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
27add 63 61 6c 6c 65 64 2c 20 66 6f 72 20 65 78 61 6d  called, for exam
27ade 70 6c 65 2c 20 6a 75 73 74 20 62 65 66 6f 72 65  ple, just before
27adf 20 64 65 6c 65 74 69 6e 67 20 74 68 65 20 54 61   deleting the Ta
27ae0 62 6c 65 0a 2a 2a 20 72 65 63 6f 72 64 2e 0a 2a  ble.** record..*
27ae1 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
27ae2 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
27ae3 62 43 6c 65 61 72 28 54 61 62 6c 65 20 2a 70 29  bClear(Table *p)
27ae4 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  {.  sqlite3_vtab
27ae5 20 2a 70 56 74 61 62 20 3d 20 70 2d 3e 70 56 74   *pVtab = p->pVt
27ae6 61 62 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ab;.  sqlite3 *d
27ae7 62 20 3d 20 70 2d 3e 64 62 3b 0a 20 20 69 66 28  b = p->db;.  if(
27ae8 20 70 56 74 61 62 20 29 7b 0a 20 20 20 20 61 73   pVtab ){.    as
27ae9 73 65 72 74 28 20 70 2d 3e 70 4d 6f 64 20 26 26  sert( p->pMod &&
27aea 20 70 2d 3e 70 4d 6f 64 2d 3e 70 4d 6f 64 75 6c   p->pMod->pModul
27aeb 65 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  e );.    sqlite3
27aec 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20 70  VtabUnlock(db, p
27aed 56 74 61 62 29 3b 0a 20 20 20 20 70 2d 3e 70 56  Vtab);.    p->pV
27aee 74 61 62 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69  tab = 0;.  }.  i
27aef 66 28 20 70 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  f( p->azModuleAr
27af0 67 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 3b 0a  g ){.    int i;.
27af1 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70      for(i=0; i<p
27af2 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 3b 20 69 2b  ->nModuleArg; i+
27af3 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  +){.      sqlite
27af4 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
27af5 7a 4d 6f 64 75 6c 65 41 72 67 5b 69 5d 29 3b 0a  zModuleArg[i]);.
27af6 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
27af7 33 44 62 46 72 65 65 28 64 62 2c 20 70 2d 3e 61  3DbFree(db, p->a
27af8 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 7d  zModuleArg);.  }
27af9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 61 20  .}../*.** Add a 
27afa 6e 65 77 20 6d 6f 64 75 6c 65 20 61 72 67 75 6d  new module argum
27afb 65 6e 74 20 74 6f 20 70 54 61 62 6c 65 2d 3e 61  ent to pTable->a
27afc 7a 4d 6f 64 75 6c 65 41 72 67 5b 5d 2e 0a 2a 2a  zModuleArg[]..**
27afd 20 54 68 65 20 73 74 72 69 6e 67 20 69 73 20 6e   The string is n
27afe 6f 74 20 63 6f 70 69 65 64 20 2d 20 74 68 65 20  ot copied - the 
27aff 70 6f 69 6e 74 65 72 20 69 73 20 73 74 6f 72 65  pointer is store
27b00 64 2e 20 20 54 68 65 0a 2a 2a 20 73 74 72 69 6e  d.  The.** strin
27b01 67 20 77 69 6c 6c 20 62 65 20 66 72 65 65 64 20  g will be freed 
27b02 61 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 77 68  automatically wh
27b03 65 6e 20 74 68 65 20 74 61 62 6c 65 20 69 73 0a  en the table is.
27b04 2a 2a 20 64 65 6c 65 74 65 64 2e 0a 2a 2f 0a 73  ** deleted..*/.s
27b05 74 61 74 69 63 20 76 6f 69 64 20 61 64 64 4d 6f  tatic void addMo
27b06 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 73 71 6c  duleArgument(sql
27b07 69 74 65 33 20 2a 64 62 2c 20 54 61 62 6c 65 20  ite3 *db, Table 
27b08 2a 70 54 61 62 6c 65 2c 20 63 68 61 72 20 2a 7a  *pTable, char *z
27b09 41 72 67 29 7b 0a 20 20 69 6e 74 20 69 20 3d 20  Arg){.  int i = 
27b0a 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64 75 6c 65 41  pTable->nModuleA
27b0b 72 67 2b 2b 3b 0a 20 20 69 6e 74 20 6e 42 79 74  rg++;.  int nByt
27b0c 65 73 20 3d 20 73 69 7a 65 6f 66 28 63 68 61 72  es = sizeof(char
27b0d 20 2a 29 2a 28 31 2b 70 54 61 62 6c 65 2d 3e 6e   *)*(1+pTable->n
27b0e 4d 6f 64 75 6c 65 41 72 67 29 3b 0a 20 20 63 68  ModuleArg);.  ch
27b0f 61 72 20 2a 2a 61 7a 4d 6f 64 75 6c 65 41 72 67  ar **azModuleArg
27b10 3b 0a 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 20  ;.  azModuleArg 
27b11 3d 20 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c  = sqlite3DbReall
27b12 6f 63 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e 61  oc(db, pTable->a
27b13 7a 4d 6f 64 75 6c 65 41 72 67 2c 20 6e 42 79 74  zModuleArg, nByt
27b14 65 73 29 3b 0a 20 20 69 66 28 20 61 7a 4d 6f 64  es);.  if( azMod
27b15 75 6c 65 41 72 67 3d 3d 30 20 29 7b 0a 20 20 20  uleArg==0 ){.   
27b16 20 69 6e 74 20 6a 3b 0a 20 20 20 20 66 6f 72 28   int j;.    for(
27b17 6a 3d 30 3b 20 6a 3c 69 3b 20 6a 2b 2b 29 7b 0a  j=0; j<i; j++){.
27b18 20 20 20 20 20 20 73 71 6c 69 74 65 33 44 62 46        sqlite3DbF
27b19 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  ree(db, pTable->
27b1a 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 6a 5d 29 3b  azModuleArg[j]);
27b1b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
27b1c 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 41 72  e3DbFree(db, zAr
27b1d 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  g);.    sqlite3D
27b1e 62 46 72 65 65 28 64 62 2c 20 70 54 61 62 6c 65  bFree(db, pTable
27b1f 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 29 3b 0a  ->azModuleArg);.
27b20 20 20 20 20 70 54 61 62 6c 65 2d 3e 6e 4d 6f 64      pTable->nMod
27b21 75 6c 65 41 72 67 20 3d 20 30 3b 0a 20 20 7d 65  uleArg = 0;.  }e
27b22 6c 73 65 7b 0a 20 20 20 20 61 7a 4d 6f 64 75 6c  lse{.    azModul
27b23 65 41 72 67 5b 69 5d 20 3d 20 7a 41 72 67 3b 0a  eArg[i] = zArg;.
27b24 20 20 20 20 61 7a 4d 6f 64 75 6c 65 41 72 67 5b      azModuleArg[
27b25 69 2b 31 5d 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  i+1] = 0;.  }.  
27b26 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64 75 6c 65  pTable->azModule
27b27 41 72 67 20 3d 20 61 7a 4d 6f 64 75 6c 65 41 72  Arg = azModuleAr
27b28 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  g;.}../*.** The 
27b29 70 61 72 73 65 72 20 63 61 6c 6c 73 20 74 68 69  parser calls thi
27b2a 73 20 72 6f 75 74 69 6e 65 20 77 68 65 6e 20 69  s routine when i
27b2b 74 20 66 69 72 73 74 20 73 65 65 73 20 61 20 43  t first sees a C
27b2c 52 45 41 54 45 20 56 49 52 54 55 41 4c 20 54 41  REATE VIRTUAL TA
27b2d 42 4c 45 0a 2a 2a 20 73 74 61 74 65 6d 65 6e 74  BLE.** statement
27b2e 2e 20 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e 61  .  The module na
27b2f 6d 65 20 68 61 73 20 62 65 65 6e 20 70 61 72 73  me has been pars
27b30 65 64 2c 20 62 75 74 20 74 68 65 20 6f 70 74 69  ed, but the opti
27b31 6f 6e 61 6c 20 6c 69 73 74 0a 2a 2a 20 6f 66 20  onal list.** of 
27b32 70 61 72 61 6d 65 74 65 72 73 20 74 68 61 74 20  parameters that 
27b33 66 6f 6c 6c 6f 77 20 74 68 65 20 6d 6f 64 75 6c  follow the modul
27b34 65 20 6e 61 6d 65 20 61 72 65 20 73 74 69 6c 6c  e name are still
27b35 20 70 65 6e 64 69 6e 67 2e 0a 2a 2f 0a 53 51 4c   pending..*/.SQL
27b36 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
27b37 20 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69   sqlite3VtabBegi
27b38 6e 50 61 72 73 65 28 0a 20 20 50 61 72 73 65 20  nParse(.  Parse 
27b39 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20 20  *pParse,        
27b3a 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e 74 65  /* Parsing conte
27b3b 78 74 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20 2a 70  xt */.  Token *p
27b3c 4e 61 6d 65 31 2c 20 20 20 20 20 20 20 20 2f 2a  Name1,        /*
27b3d 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74 61 62   Name of new tab
27b3e 6c 65 2c 20 6f 72 20 64 61 74 61 62 61 73 65 20  le, or database 
27b3f 6e 61 6d 65 20 2a 2f 0a 20 20 54 6f 6b 65 6e 20  name */.  Token 
27b40 2a 70 4e 61 6d 65 32 2c 20 20 20 20 20 20 20 20  *pName2,        
27b41 2f 2a 20 4e 61 6d 65 20 6f 66 20 6e 65 77 20 74  /* Name of new t
27b42 61 62 6c 65 20 6f 72 20 4e 55 4c 4c 20 2a 2f 0a  able or NULL */.
27b43 20 20 54 6f 6b 65 6e 20 2a 70 4d 6f 64 75 6c 65    Token *pModule
27b44 4e 61 6d 65 20 20 20 20 2f 2a 20 4e 61 6d 65 20  Name    /* Name 
27b45 6f 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 66 6f  of the module fo
27b46 72 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  r the virtual ta
27b47 62 6c 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ble */.){.  int 
27b48 69 44 62 3b 20 20 20 20 20 20 20 20 20 20 20 20  iDb;            
27b49 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
27b4a 65 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 62  e the table is b
27b4b 65 69 6e 67 20 63 72 65 61 74 65 64 20 69 6e 20  eing created in 
27b4c 2a 2f 0a 20 20 54 61 62 6c 65 20 2a 70 54 61 62  */.  Table *pTab
27b4d 6c 65 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 68  le;        /* Th
27b4e 65 20 6e 65 77 20 76 69 72 74 75 61 6c 20 74 61  e new virtual ta
27b4f 62 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ble */.  sqlite3
27b50 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 2f   *db;          /
27b51 2a 20 44 61 74 61 62 61 73 65 20 63 6f 6e 6e 65  * Database conne
27b52 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66 28 20  ction */..  if( 
27b53 70 50 61 72 73 65 2d 3e 64 62 2d 3e 66 6c 61 67  pParse->db->flag
27b54 73 20 26 20 53 51 4c 49 54 45 5f 53 68 61 72 65  s & SQLITE_Share
27b55 64 43 61 63 68 65 20 29 7b 0a 20 20 20 20 73 71  dCache ){.    sq
27b56 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
27b57 61 72 73 65 2c 20 22 43 61 6e 6e 6f 74 20 75 73  arse, "Cannot us
27b58 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  e virtual tables
27b59 20 69 6e 20 73 68 61 72 65 64 2d 63 61 63 68 65   in shared-cache
27b5a 20 6d 6f 64 65 22 29 3b 0a 20 20 20 20 72 65 74   mode");.    ret
27b5b 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  urn;.  }..  sqli
27b5c 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70 50  te3StartTable(pP
27b5d 61 72 73 65 2c 20 70 4e 61 6d 65 31 2c 20 70 4e  arse, pName1, pN
27b5e 61 6d 65 32 2c 20 30 2c 20 30 2c 20 31 2c 20 30  ame2, 0, 0, 1, 0
27b5f 29 3b 0a 20 20 70 54 61 62 6c 65 20 3d 20 70 50  );.  pTable = pP
27b60 61 72 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 3b  arse->pNewTable;
27b61 0a 20 20 69 66 28 20 70 54 61 62 6c 65 3d 3d 30  .  if( pTable==0
27b62 20 7c 7c 20 70 50 61 72 73 65 2d 3e 6e 45 72 72   || pParse->nErr
27b63 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 61 73 73   ) return;.  ass
27b64 65 72 74 28 20 30 3d 3d 70 54 61 62 6c 65 2d 3e  ert( 0==pTable->
27b65 70 49 6e 64 65 78 20 29 3b 0a 0a 20 20 64 62 20  pIndex );..  db 
27b66 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20  = pParse->db;.  
27b67 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63 68  iDb = sqlite3Sch
27b68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20 70  emaToIndex(db, p
27b69 54 61 62 6c 65 2d 3e 70 53 63 68 65 6d 61 29 3b  Table->pSchema);
27b6a 0a 20 20 61 73 73 65 72 74 28 20 69 44 62 3e 3d  .  assert( iDb>=
27b6b 30 20 29 3b 0a 0a 20 20 70 54 61 62 6c 65 2d 3e  0 );..  pTable->
27b6c 74 61 62 46 6c 61 67 73 20 7c 3d 20 54 46 5f 56  tabFlags |= TF_V
27b6d 69 72 74 75 61 6c 3b 0a 20 20 70 54 61 62 6c 65  irtual;.  pTable
27b6e 2d 3e 6e 4d 6f 64 75 6c 65 41 72 67 20 3d 20 30  ->nModuleArg = 0
27b6f 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41 72 67  ;.  addModuleArg
27b70 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62 6c 65  ument(db, pTable
27b71 2c 20 73 71 6c 69 74 65 33 4e 61 6d 65 46 72 6f  , sqlite3NameFro
27b72 6d 54 6f 6b 65 6e 28 64 62 2c 20 70 4d 6f 64 75  mToken(db, pModu
27b73 6c 65 4e 61 6d 65 29 29 3b 0a 20 20 61 64 64 4d  leName));.  addM
27b74 6f 64 75 6c 65 41 72 67 75 6d 65 6e 74 28 64 62  oduleArgument(db
27b75 2c 20 70 54 61 62 6c 65 2c 20 73 71 6c 69 74 65  , pTable, sqlite
27b76 33 44 62 53 74 72 44 75 70 28 64 62 2c 20 64 62  3DbStrDup(db, db
27b77 2d 3e 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65  ->aDb[iDb].zName
27b78 29 29 3b 0a 20 20 61 64 64 4d 6f 64 75 6c 65 41  ));.  addModuleA
27b79 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 54 61 62  rgument(db, pTab
27b7a 6c 65 2c 20 73 71 6c 69 74 65 33 44 62 53 74 72  le, sqlite3DbStr
27b7b 44 75 70 28 64 62 2c 20 70 54 61 62 6c 65 2d 3e  Dup(db, pTable->
27b7c 7a 4e 61 6d 65 29 29 3b 0a 20 20 70 50 61 72 73  zName));.  pPars
27b7d 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e 20  e->sNameToken.n 
27b7e 3d 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e 7a  = pModuleName->z
27b7f 20 2b 20 70 4d 6f 64 75 6c 65 4e 61 6d 65 2d 3e   + pModuleName->
27b80 6e 20 2d 20 70 4e 61 6d 65 31 2d 3e 7a 3b 0a 0a  n - pName1->z;..
27b81 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
27b82 4d 49 54 5f 41 55 54 48 4f 52 49 5a 41 54 49 4f  MIT_AUTHORIZATIO
27b83 4e 0a 20 20 2f 2a 20 43 72 65 61 74 69 6e 67 20  N.  /* Creating 
27b84 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  a virtual table 
27b85 69 6e 76 6f 6b 65 73 20 74 68 65 20 61 75 74 68  invokes the auth
27b86 6f 72 69 7a 61 74 69 6f 6e 20 63 61 6c 6c 62 61  orization callba
27b87 63 6b 20 74 77 69 63 65 2e 0a 20 20 2a 2a 20 54  ck twice..  ** T
27b88 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61 74  he first invocat
27b89 69 6f 6e 2c 20 74 6f 20 6f 62 74 61 69 6e 20 70  ion, to obtain p
27b8a 65 72 6d 69 73 73 69 6f 6e 20 74 6f 20 49 4e 53  ermission to INS
27b8b 45 52 54 20 61 20 72 6f 77 20 69 6e 74 6f 20 74  ERT a row into t
27b8c 68 65 0a 20 20 2a 2a 20 73 71 6c 69 74 65 5f 6d  he.  ** sqlite_m
27b8d 61 73 74 65 72 20 74 61 62 6c 65 2c 20 68 61 73  aster table, has
27b8e 20 61 6c 72 65 61 64 79 20 62 65 65 6e 20 6d 61   already been ma
27b8f 64 65 20 62 79 20 73 71 6c 69 74 65 33 53 74 61  de by sqlite3Sta
27b90 72 74 54 61 62 6c 65 28 29 2e 0a 20 20 2a 2a 20  rtTable()..  ** 
27b91 54 68 65 20 73 65 63 6f 6e 64 20 63 61 6c 6c 2c  The second call,
27b92 20 74 6f 20 6f 62 74 61 69 6e 20 70 65 72 6d 69   to obtain permi
27b93 73 73 69 6f 6e 20 74 6f 20 63 72 65 61 74 65 20  ssion to create 
27b94 74 68 65 20 74 61 62 6c 65 2c 20 69 73 20 6d 61  the table, is ma
27b95 64 65 20 6e 6f 77 2e 0a 20 20 2a 2f 0a 20 20 69  de now..  */.  i
27b96 66 28 20 70 54 61 62 6c 65 2d 3e 61 7a 4d 6f 64  f( pTable->azMod
27b97 75 6c 65 41 72 67 20 29 7b 0a 20 20 20 20 73 71  uleArg ){.    sq
27b98 6c 69 74 65 33 41 75 74 68 43 68 65 63 6b 28 70  lite3AuthCheck(p
27b99 50 61 72 73 65 2c 20 53 51 4c 49 54 45 5f 43 52  Parse, SQLITE_CR
27b9a 45 41 54 45 5f 56 54 41 42 4c 45 2c 20 70 54 61  EATE_VTABLE, pTa
27b9b 62 6c 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20  ble->zName, .   
27b9c 20 20 20 20 20 20 20 20 20 70 54 61 62 6c 65 2d           pTable-
27b9d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d 2c  >azModuleArg[0],
27b9e 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 61 44 62   pParse->db->aDb
27b9f 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
27ba0 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 0a 2a  }.#endif.}../*.*
27ba1 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 74  * This routine t
27ba2 61 6b 65 73 20 74 68 65 20 6d 6f 64 75 6c 65 20  akes the module 
27ba3 61 72 67 75 6d 65 6e 74 20 74 68 61 74 20 68 61  argument that ha
27ba4 73 20 62 65 65 6e 20 61 63 63 75 6d 75 6c 61 74  s been accumulat
27ba5 69 6e 67 0a 2a 2a 20 69 6e 20 70 50 61 72 73 65  ing.** in pParse
27ba6 2d 3e 7a 41 72 67 5b 5d 20 61 6e 64 20 61 70 70  ->zArg[] and app
27ba7 65 6e 64 73 20 69 74 20 74 6f 20 74 68 65 20 6c  ends it to the l
27ba8 69 73 74 20 6f 66 20 61 72 67 75 6d 65 6e 74 73  ist of arguments
27ba9 20 6f 6e 20 74 68 65 0a 2a 2a 20 76 69 72 74 75   on the.** virtu
27baa 61 6c 20 74 61 62 6c 65 20 63 75 72 72 65 6e 74  al table current
27bab 6c 79 20 75 6e 64 65 72 20 63 6f 6e 73 74 72 75  ly under constru
27bac 63 74 69 6f 6e 20 69 6e 20 70 50 61 72 73 65 2d  ction in pParse-
27bad 3e 70 54 61 62 6c 65 2e 0a 2a 2f 0a 73 74 61 74  >pTable..*/.stat
27bae 69 63 20 76 6f 69 64 20 61 64 64 41 72 67 75 6d  ic void addArgum
27baf 65 6e 74 54 6f 56 74 61 62 28 50 61 72 73 65 20  entToVtab(Parse 
27bb0 2a 70 50 61 72 73 65 29 7b 0a 20 20 69 66 28 20  *pParse){.  if( 
27bb1 70 50 61 72 73 65 2d 3e 73 41 72 67 2e 7a 20 26  pParse->sArg.z &
27bb2 26 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54 61  & pParse->pNewTa
27bb3 62 6c 65 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  ble ){.    const
27bb4 20 63 68 61 72 20 2a 7a 20 3d 20 28 63 6f 6e 73   char *z = (cons
27bb5 74 20 63 68 61 72 2a 29 70 50 61 72 73 65 2d 3e  t char*)pParse->
27bb6 73 41 72 67 2e 7a 3b 0a 20 20 20 20 69 6e 74 20  sArg.z;.    int 
27bb7 6e 20 3d 20 70 50 61 72 73 65 2d 3e 73 41 72 67  n = pParse->sArg
27bb8 2e 6e 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20  .n;.    sqlite3 
27bb9 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
27bba 3b 0a 20 20 20 20 61 64 64 4d 6f 64 75 6c 65 41  ;.    addModuleA
27bbb 72 67 75 6d 65 6e 74 28 64 62 2c 20 70 50 61 72  rgument(db, pPar
27bbc 73 65 2d 3e 70 4e 65 77 54 61 62 6c 65 2c 20 73  se->pNewTable, s
27bbd 71 6c 69 74 65 33 44 62 53 74 72 4e 44 75 70 28  qlite3DbStrNDup(
27bbe 64 62 2c 20 7a 2c 20 6e 29 29 3b 0a 20 20 7d 0a  db, z, n));.  }.
27bbf 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
27bc0 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
27bc1 6f 75 74 69 6e 65 20 61 66 74 65 72 20 74 68 65  outine after the
27bc2 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
27bc3 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 0a  TABLE statement.
27bc4 2a 2a 20 68 61 73 20 62 65 65 6e 20 63 6f 6d 70  ** has been comp
27bc5 6c 65 74 65 6c 79 20 70 61 72 73 65 64 2e 0a 2a  letely parsed..*
27bc6 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  /.SQLITE_PRIVATE
27bc7 20 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61   void sqlite3Vta
27bc8 62 46 69 6e 69 73 68 50 61 72 73 65 28 50 61 72  bFinishParse(Par
27bc9 73 65 20 2a 70 50 61 72 73 65 2c 20 54 6f 6b 65  se *pParse, Toke
27bca 6e 20 2a 70 45 6e 64 29 7b 0a 20 20 54 61 62 6c  n *pEnd){.  Tabl
27bcb 65 20 2a 70 54 61 62 3b 20 20 20 20 20 20 20 20  e *pTab;        
27bcc 2f 2a 20 54 68 65 20 74 61 62 6c 65 20 62 65 69  /* The table bei
27bcd 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 20 2a  ng constructed *
27bce 2f 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b  /.  sqlite3 *db;
27bcf 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
27bd0 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
27bd1 6f 6e 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 4d  on */.  char *zM
27bd2 6f 64 75 6c 65 3b 20 20 20 20 20 20 2f 2a 20 54  odule;      /* T
27bd3 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 20 6f  he module name o
27bd4 66 20 74 68 65 20 74 61 62 6c 65 3a 20 55 53 49  f the table: USI
27bd5 4e 47 20 6d 6f 64 75 6c 65 6e 61 6d 65 20 2a 2f  NG modulename */
27bd6 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20  .  Module *pMod 
27bd7 3d 20 30 3b 0a 0a 20 20 61 64 64 41 72 67 75 6d  = 0;..  addArgum
27bd8 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73 65  entToVtab(pParse
27bd9 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41 72  );.  pParse->sAr
27bda 67 2e 7a 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c  g.z = 0;..  /* L
27bdb 6f 6f 6b 75 70 20 74 68 65 20 6d 6f 64 75 6c 65  ookup the module
27bdc 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 70 54 61 62   name. */.  pTab
27bdd 20 3d 20 70 50 61 72 73 65 2d 3e 70 4e 65 77 54   = pParse->pNewT
27bde 61 62 6c 65 3b 0a 20 20 69 66 28 20 70 54 61 62  able;.  if( pTab
27bdf 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20  ==0 ) return;.  
27be0 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b  db = pParse->db;
27be1 0a 20 20 69 66 28 20 70 54 61 62 2d 3e 6e 4d 6f  .  if( pTab->nMo
27be2 64 75 6c 65 41 72 67 3c 31 20 29 20 72 65 74 75  duleArg<1 ) retu
27be3 72 6e 3b 0a 20 20 7a 4d 6f 64 75 6c 65 20 3d 20  rn;.  zModule = 
27be4 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72  pTab->azModuleAr
27be5 67 5b 30 5d 3b 0a 20 20 70 4d 6f 64 20 3d 20 28  g[0];.  pMod = (
27be6 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74 65 33  Module *)sqlite3
27be7 48 61 73 68 46 69 6e 64 28 26 64 62 2d 3e 61 4d  HashFind(&db->aM
27be8 6f 64 75 6c 65 2c 20 7a 4d 6f 64 75 6c 65 2c 20  odule, zModule, 
27be9 73 74 72 6c 65 6e 28 7a 4d 6f 64 75 6c 65 29 29  strlen(zModule))
27bea 3b 0a 20 20 70 54 61 62 2d 3e 70 4d 6f 64 20 3d  ;.  pTab->pMod =
27beb 20 70 4d 6f 64 3b 0a 20 20 0a 20 20 2f 2a 20 49   pMod;.  .  /* I
27bec 66 20 74 68 65 20 43 52 45 41 54 45 20 56 49 52  f the CREATE VIR
27bed 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65  TUAL TABLE state
27bee 6d 65 6e 74 20 69 73 20 62 65 69 6e 67 20 65 6e  ment is being en
27bef 74 65 72 65 64 20 66 6f 72 20 74 68 65 0a 20 20  tered for the.  
27bf0 2a 2a 20 66 69 72 73 74 20 74 69 6d 65 20 28 69  ** first time (i
27bf1 6e 20 6f 74 68 65 72 20 77 6f 72 64 73 20 69 66  n other words if
27bf2 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
27bf3 6c 65 20 69 73 20 61 63 74 75 61 6c 6c 79 20 62  le is actually b
27bf4 65 69 6e 67 0a 20 20 2a 2a 20 63 72 65 61 74 65  eing.  ** create
27bf5 64 20 6e 6f 77 20 69 6e 73 74 65 61 64 20 6f 66  d now instead of
27bf6 20 6a 75 73 74 20 62 65 69 6e 67 20 72 65 61 64   just being read
27bf7 20 6f 75 74 20 6f 66 20 73 71 6c 69 74 65 5f 6d   out of sqlite_m
27bf8 61 73 74 65 72 29 20 74 68 65 6e 0a 20 20 2a 2a  aster) then.  **
27bf9 20 64 6f 20 61 64 64 69 74 69 6f 6e 61 6c 20 69   do additional i
27bfa 6e 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 77 6f  nitialization wo
27bfb 72 6b 20 61 6e 64 20 73 74 6f 72 65 20 74 68 65  rk and store the
27bfc 20 73 74 61 74 65 6d 65 6e 74 20 74 65 78 74 0a   statement text.
27bfd 20 20 2a 2a 20 69 6e 20 74 68 65 20 73 71 6c 69    ** in the sqli
27bfe 74 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 2e  te_master table.
27bff 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 64 62 2d  .  */.  if( !db-
27c00 3e 69 6e 69 74 2e 62 75 73 79 20 29 7b 0a 20 20  >init.busy ){.  
27c01 20 20 63 68 61 72 20 2a 7a 53 74 6d 74 3b 0a 20    char *zStmt;. 
27c02 20 20 20 63 68 61 72 20 2a 7a 57 68 65 72 65 3b     char *zWhere;
27c03 0a 20 20 20 20 69 6e 74 20 69 44 62 3b 0a 20 20  .    int iDb;.  
27c04 20 20 56 64 62 65 20 2a 76 3b 0a 0a 20 20 20 20    Vdbe *v;..    
27c05 2f 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 63  /* Compute the c
27c06 6f 6d 70 6c 65 74 65 20 74 65 78 74 20 6f 66 20  omplete text of 
27c07 74 68 65 20 43 52 45 41 54 45 20 56 49 52 54 55  the CREATE VIRTU
27c08 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  AL TABLE stateme
27c09 6e 74 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 45  nt */.    if( pE
27c0a 6e 64 20 29 7b 0a 20 20 20 20 20 20 70 50 61 72  nd ){.      pPar
27c0b 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e 6e  se->sNameToken.n
27c0c 20 3d 20 70 45 6e 64 2d 3e 7a 20 2d 20 70 50 61   = pEnd->z - pPa
27c0d 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e 2e  rse->sNameToken.
27c0e 7a 20 2b 20 70 45 6e 64 2d 3e 6e 3b 0a 20 20 20  z + pEnd->n;.   
27c0f 20 7d 0a 20 20 20 20 7a 53 74 6d 74 20 3d 20 73   }.    zStmt = s
27c10 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28 64 62  qlite3MPrintf(db
27c11 2c 20 22 43 52 45 41 54 45 20 56 49 52 54 55 41  , "CREATE VIRTUA
27c12 4c 20 54 41 42 4c 45 20 25 54 22 2c 20 26 70 50  L TABLE %T", &pP
27c13 61 72 73 65 2d 3e 73 4e 61 6d 65 54 6f 6b 65 6e  arse->sNameToken
27c14 29 3b 0a 0a 20 20 20 20 2f 2a 20 41 20 73 6c 6f  );..    /* A slo
27c15 74 20 66 6f 72 20 74 68 65 20 72 65 63 6f 72 64  t for the record
27c16 20 68 61 73 20 61 6c 72 65 61 64 79 20 62 65 65   has already bee
27c17 6e 20 61 6c 6c 6f 63 61 74 65 64 20 69 6e 20 74  n allocated in t
27c18 68 65 20 0a 20 20 20 20 2a 2a 20 53 51 4c 49 54  he .    ** SQLIT
27c19 45 5f 4d 41 53 54 45 52 20 74 61 62 6c 65 2e 20  E_MASTER table. 
27c1a 20 57 65 20 6a 75 73 74 20 6e 65 65 64 20 74 6f   We just need to
27c1b 20 75 70 64 61 74 65 20 74 68 61 74 20 73 6c 6f   update that slo
27c1c 74 20 77 69 74 68 20 61 6c 6c 0a 20 20 20 20 2a  t with all.    *
27c1d 2a 20 74 68 65 20 69 6e 66 6f 72 6d 61 74 69 6f  * the informatio
27c1e 6e 20 77 65 27 76 65 20 63 6f 6c 6c 65 63 74 65  n we've collecte
27c1f 64 2e 20 20 0a 20 20 20 20 2a 2a 0a 20 20 20 20  d.  .    **.    
27c20 2a 2a 20 54 68 65 20 56 4d 20 72 65 67 69 73 74  ** The VM regist
27c21 65 72 20 6e 75 6d 62 65 72 20 70 50 61 72 73 65  er number pParse
27c22 2d 3e 72 65 67 52 6f 77 69 64 20 68 6f 6c 64 73  ->regRowid holds
27c23 20 74 68 65 20 72 6f 77 69 64 20 6f 66 20 61 6e   the rowid of an
27c24 0a 20 20 20 20 2a 2a 20 65 6e 74 72 79 20 69 6e  .    ** entry in
27c25 20 74 68 65 20 73 71 6c 69 74 65 5f 6d 61 73 74   the sqlite_mast
27c26 65 72 20 74 61 62 6c 65 20 74 68 74 20 77 61 73  er table tht was
27c27 20 63 72 65 61 74 65 64 20 66 6f 72 20 74 68 69   created for thi
27c28 73 20 76 74 61 62 0a 20 20 20 20 2a 2a 20 62 79  s vtab.    ** by
27c29 20 73 71 6c 69 74 65 33 53 74 61 72 74 54 61 62   sqlite3StartTab
27c2a 6c 65 28 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  le()..    */.   
27c2b 20 69 44 62 20 3d 20 73 71 6c 69 74 65 33 53 63   iDb = sqlite3Sc
27c2c 68 65 6d 61 54 6f 49 6e 64 65 78 28 64 62 2c 20  hemaToIndex(db, 
27c2d 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b 0a  pTab->pSchema);.
27c2e 20 20 20 20 73 71 6c 69 74 65 33 4e 65 73 74 65      sqlite3Neste
27c2f 64 50 61 72 73 65 28 70 50 61 72 73 65 2c 0a 20  dParse(pParse,. 
27c30 20 20 20 20 20 22 55 50 44 41 54 45 20 25 51 2e       "UPDATE %Q.
27c31 25 73 20 22 0a 20 20 20 20 20 20 20 20 20 22 53  %s ".         "S
27c32 45 54 20 74 79 70 65 3d 27 74 61 62 6c 65 27 2c  ET type='table',
27c33 20 6e 61 6d 65 3d 25 51 2c 20 74 62 6c 5f 6e 61   name=%Q, tbl_na
27c34 6d 65 3d 25 51 2c 20 72 6f 6f 74 70 61 67 65 3d  me=%Q, rootpage=
27c35 30 2c 20 73 71 6c 3d 25 51 20 22 0a 20 20 20 20  0, sql=%Q ".    
27c36 20 20 20 22 57 48 45 52 45 20 72 6f 77 69 64 3d     "WHERE rowid=
27c37 23 25 64 22 2c 0a 20 20 20 20 20 20 64 62 2d 3e  #%d",.      db->
27c38 61 44 62 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20  aDb[iDb].zName, 
27c39 53 43 48 45 4d 41 5f 54 41 42 4c 45 28 69 44 62  SCHEMA_TABLE(iDb
27c3a 29 2c 0a 20 20 20 20 20 20 70 54 61 62 2d 3e 7a  ),.      pTab->z
27c3b 4e 61 6d 65 2c 0a 20 20 20 20 20 20 70 54 61 62  Name,.      pTab
27c3c 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 7a  ->zName,.      z
27c3d 53 74 6d 74 2c 0a 20 20 20 20 20 20 70 50 61 72  Stmt,.      pPar
27c3e 73 65 2d 3e 72 65 67 52 6f 77 69 64 0a 20 20 20  se->regRowid.   
27c3f 20 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   );.    sqlite3D
27c40 62 46 72 65 65 28 64 62 2c 20 7a 53 74 6d 74 29  bFree(db, zStmt)
27c41 3b 0a 20 20 20 20 76 20 3d 20 73 71 6c 69 74 65  ;.    v = sqlite
27c42 33 47 65 74 56 64 62 65 28 70 50 61 72 73 65 29  3GetVdbe(pParse)
27c43 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 43 68 61  ;.    sqlite3Cha
27c44 6e 67 65 43 6f 6f 6b 69 65 28 70 50 61 72 73 65  ngeCookie(pParse
27c45 2c 20 69 44 62 29 3b 0a 0a 20 20 20 20 73 71 6c  , iDb);..    sql
27c46 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
27c47 2c 20 4f 50 5f 45 78 70 69 72 65 2c 20 30 2c 20  , OP_Expire, 0, 
27c48 30 29 3b 0a 20 20 20 20 7a 57 68 65 72 65 20 3d  0);.    zWhere =
27c49 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
27c4a 64 62 2c 20 22 6e 61 6d 65 3d 27 25 71 27 22 2c  db, "name='%q'",
27c4b 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 29 3b 0a 20   pTab->zName);. 
27c4c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
27c4d 64 4f 70 34 28 76 2c 20 4f 50 5f 50 61 72 73 65  dOp4(v, OP_Parse
27c4e 53 63 68 65 6d 61 2c 20 69 44 62 2c 20 31 2c 20  Schema, iDb, 1, 
27c4f 30 2c 20 7a 57 68 65 72 65 2c 20 50 34 5f 44 59  0, zWhere, P4_DY
27c50 4e 41 4d 49 43 29 3b 0a 20 20 20 20 73 71 6c 69  NAMIC);.    sqli
27c51 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
27c52 20 4f 50 5f 56 43 72 65 61 74 65 2c 20 69 44 62   OP_VCreate, iDb
27c53 2c 20 30 2c 20 30 2c 20 0a 20 20 20 20 20 20 20  , 0, 0, .       
27c54 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
27c55 20 20 70 54 61 62 2d 3e 7a 4e 61 6d 65 2c 20 73    pTab->zName, s
27c56 74 72 6c 65 6e 28 70 54 61 62 2d 3e 7a 4e 61 6d  trlen(pTab->zNam
27c57 65 29 20 2b 20 31 29 3b 0a 20 20 7d 0a 0a 20 20  e) + 1);.  }..  
27c58 2f 2a 20 49 66 20 77 65 20 61 72 65 20 72 65 72  /* If we are rer
27c59 65 61 64 69 6e 67 20 74 68 65 20 73 71 6c 69 74  eading the sqlit
27c5a 65 5f 6d 61 73 74 65 72 20 74 61 62 6c 65 20 63  e_master table c
27c5b 72 65 61 74 65 20 74 68 65 20 69 6e 2d 6d 65 6d  reate the in-mem
27c5c 6f 72 79 0a 20 20 2a 2a 20 72 65 63 6f 72 64 20  ory.  ** record 
27c5d 6f 66 20 74 68 65 20 74 61 62 6c 65 2e 20 49 66  of the table. If
27c5e 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73 20   the module has 
27c5f 61 6c 72 65 61 64 79 20 62 65 65 6e 20 72 65 67  already been reg
27c60 69 73 74 65 72 65 64 2c 0a 20 20 2a 2a 20 61 6c  istered,.  ** al
27c61 73 6f 20 63 61 6c 6c 20 74 68 65 20 78 43 6f 6e  so call the xCon
27c62 6e 65 63 74 20 6d 65 74 68 6f 64 20 68 65 72 65  nect method here
27c63 2e 0a 20 20 2a 2f 0a 20 20 65 6c 73 65 20 7b 0a  ..  */.  else {.
27c64 20 20 20 20 54 61 62 6c 65 20 2a 70 4f 6c 64 3b      Table *pOld;
27c65 0a 20 20 20 20 53 63 68 65 6d 61 20 2a 70 53 63  .    Schema *pSc
27c66 68 65 6d 61 20 3d 20 70 54 61 62 2d 3e 70 53 63  hema = pTab->pSc
27c67 68 65 6d 61 3b 0a 20 20 20 20 63 6f 6e 73 74 20  hema;.    const 
27c68 63 68 61 72 20 2a 7a 4e 61 6d 65 20 3d 20 70 54  char *zName = pT
27c69 61 62 2d 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 69  ab->zName;.    i
27c6a 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 74 72 6c 65  nt nName = strle
27c6b 6e 28 7a 4e 61 6d 65 29 20 2b 20 31 3b 0a 20 20  n(zName) + 1;.  
27c6c 20 20 70 4f 6c 64 20 3d 20 73 71 6c 69 74 65 33    pOld = sqlite3
27c6d 48 61 73 68 49 6e 73 65 72 74 28 26 70 53 63 68  HashInsert(&pSch
27c6e 65 6d 61 2d 3e 74 62 6c 48 61 73 68 2c 20 7a 4e  ema->tblHash, zN
27c6f 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 70 54 61 62  ame, nName, pTab
27c70 29 3b 0a 20 20 20 20 69 66 28 20 70 4f 6c 64 20  );.    if( pOld 
27c71 29 7b 0a 20 20 20 20 20 20 64 62 2d 3e 6d 61 6c  ){.      db->mal
27c72 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a 20  locFailed = 1;. 
27c73 20 20 20 20 20 61 73 73 65 72 74 28 20 70 54 61       assert( pTa
27c74 62 3d 3d 70 4f 6c 64 20 29 3b 20 20 2f 2a 20 4d  b==pOld );  /* M
27c75 61 6c 6c 6f 63 20 6d 75 73 74 20 68 61 76 65 20  alloc must have 
27c76 66 61 69 6c 65 64 20 69 6e 73 69 64 65 20 48 61  failed inside Ha
27c77 73 68 49 6e 73 65 72 74 28 29 20 2a 2f 0a 20 20  shInsert() */.  
27c78 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
27c79 7d 0a 20 20 20 20 70 53 63 68 65 6d 61 2d 3e 64  }.    pSchema->d
27c7a 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a  b = pParse->db;.
27c7b 20 20 20 20 70 50 61 72 73 65 2d 3e 70 4e 65 77      pParse->pNew
27c7c 54 61 62 6c 65 20 3d 20 30 3b 0a 20 20 7d 0a 7d  Table = 0;.  }.}
27c7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72 73  ../*.** The pars
27c7e 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f  er calls this ro
27c7f 75 74 69 6e 65 20 77 68 65 6e 20 69 74 20 73 65  utine when it se
27c80 65 73 20 74 68 65 20 66 69 72 73 74 20 74 6f 6b  es the first tok
27c81 65 6e 0a 2a 2a 20 6f 66 20 61 6e 20 61 72 67 75  en.** of an argu
27c82 6d 65 6e 74 20 74 6f 20 74 68 65 20 6d 6f 64 75  ment to the modu
27c83 6c 65 20 6e 61 6d 65 20 69 6e 20 61 20 43 52 45  le name in a CRE
27c84 41 54 45 20 56 49 52 54 55 41 4c 20 54 41 42 4c  ATE VIRTUAL TABL
27c85 45 20 73 74 61 74 65 6d 65 6e 74 2e 0a 2a 2f 0a  E statement..*/.
27c86 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76  SQLITE_PRIVATE v
27c87 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62 41  oid sqlite3VtabA
27c88 72 67 49 6e 69 74 28 50 61 72 73 65 20 2a 70 50  rgInit(Parse *pP
27c89 61 72 73 65 29 7b 0a 20 20 61 64 64 41 72 67 75  arse){.  addArgu
27c8a 6d 65 6e 74 54 6f 56 74 61 62 28 70 50 61 72 73  mentToVtab(pPars
27c8b 65 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 73 41  e);.  pParse->sA
27c8c 72 67 2e 7a 20 3d 20 30 3b 0a 20 20 70 50 61 72  rg.z = 0;.  pPar
27c8d 73 65 2d 3e 73 41 72 67 2e 6e 20 3d 20 30 3b 0a  se->sArg.n = 0;.
27c8e 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 70 61 72  }../*.** The par
27c8f 73 65 72 20 63 61 6c 6c 73 20 74 68 69 73 20 72  ser calls this r
27c90 6f 75 74 69 6e 65 20 66 6f 72 20 65 61 63 68 20  outine for each 
27c91 74 6f 6b 65 6e 20 61 66 74 65 72 20 74 68 65 20  token after the 
27c92 66 69 72 73 74 20 74 6f 6b 65 6e 0a 2a 2a 20 69  first token.** i
27c93 6e 20 61 6e 20 61 72 67 75 6d 65 6e 74 20 74 6f  n an argument to
27c94 20 74 68 65 20 6d 6f 64 75 6c 65 20 6e 61 6d 65   the module name
27c95 20 69 6e 20 61 20 43 52 45 41 54 45 20 56 49 52   in a CREATE VIR
27c96 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61 74 65  TUAL TABLE state
27c97 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
27c98 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
27c99 69 74 65 33 56 74 61 62 41 72 67 45 78 74 65 6e  ite3VtabArgExten
27c9a 64 28 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c  d(Parse *pParse,
27c9b 20 54 6f 6b 65 6e 20 2a 70 29 7b 0a 20 20 54 6f   Token *p){.  To
27c9c 6b 65 6e 20 2a 70 41 72 67 20 3d 20 26 70 50 61  ken *pArg = &pPa
27c9d 72 73 65 2d 3e 73 41 72 67 3b 0a 20 20 69 66 28  rse->sArg;.  if(
27c9e 20 70 41 72 67 2d 3e 7a 3d 3d 30 20 29 7b 0a 20   pArg->z==0 ){. 
27c9f 20 20 20 70 41 72 67 2d 3e 7a 20 3d 20 70 2d 3e     pArg->z = p->
27ca0 7a 3b 0a 20 20 20 20 70 41 72 67 2d 3e 6e 20 3d  z;.    pArg->n =
27ca1 20 70 2d 3e 6e 3b 0a 20 20 7d 65 6c 73 65 7b 0a   p->n;.  }else{.
27ca2 20 20 20 20 61 73 73 65 72 74 28 70 41 72 67 2d      assert(pArg-
27ca3 3e 7a 20 3c 20 70 2d 3e 7a 29 3b 0a 20 20 20 20  >z < p->z);.    
27ca4 70 41 72 67 2d 3e 6e 20 3d 20 28 70 2d 3e 7a 20  pArg->n = (p->z 
27ca5 2b 20 70 2d 3e 6e 20 2d 20 70 41 72 67 2d 3e 7a  + p->n - pArg->z
27ca6 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
27ca7 49 6e 76 6f 6b 65 20 61 20 76 69 72 74 75 61 6c  Invoke a virtual
27ca8 20 74 61 62 6c 65 20 63 6f 6e 73 74 72 75 63 74   table construct
27ca9 6f 72 20 28 65 69 74 68 65 72 20 78 43 72 65 61  or (either xCrea
27caa 74 65 20 6f 72 20 78 43 6f 6e 6e 65 63 74 29 2e  te or xConnect).
27cab 20 54 68 65 0a 2a 2a 20 70 6f 69 6e 74 65 72 20   The.** pointer 
27cac 74 6f 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20  to the function 
27cad 74 6f 20 69 6e 76 6f 6b 65 20 69 73 20 70 61 73  to invoke is pas
27cae 73 65 64 20 61 73 20 74 68 65 20 66 6f 75 72 74  sed as the fourt
27caf 68 20 70 61 72 61 6d 65 74 65 72 0a 2a 2a 20 74  h parameter.** t
27cb0 6f 20 74 68 69 73 20 70 72 6f 63 65 64 75 72 65  o this procedure
27cb1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
27cb2 76 74 61 62 43 61 6c 6c 43 6f 6e 73 74 72 75 63  vtabCallConstruc
27cb3 74 6f 72 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  tor(.  sqlite3 *
27cb4 64 62 2c 20 0a 20 20 54 61 62 6c 65 20 2a 70 54  db, .  Table *pT
27cb5 61 62 2c 0a 20 20 4d 6f 64 75 6c 65 20 2a 70 4d  ab,.  Module *pM
27cb6 6f 64 2c 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e  od,.  int (*xCon
27cb7 73 74 72 75 63 74 29 28 73 71 6c 69 74 65 33 2a  struct)(sqlite3*
27cb8 2c 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  ,void*,int,const
27cb9 20 63 68 61 72 2a 63 6f 6e 73 74 2a 2c 73 71 6c   char*const*,sql
27cba 69 74 65 33 5f 76 74 61 62 2a 2a 2c 63 68 61 72  ite3_vtab**,char
27cbb 2a 2a 29 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a  **),.  char **pz
27cbc 45 72 72 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b  Err.){.  int rc;
27cbd 0a 20 20 69 6e 74 20 72 63 32 3b 0a 20 20 73 71  .  int rc2;.  sq
27cbe 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
27cbf 62 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 63  b = 0;.  const c
27cc0 68 61 72 20 2a 63 6f 6e 73 74 2a 61 7a 41 72 67  har *const*azArg
27cc1 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a   = (const char *
27cc2 63 6f 6e 73 74 2a 29 70 54 61 62 2d 3e 61 7a 4d  const*)pTab->azM
27cc3 6f 64 75 6c 65 41 72 67 3b 0a 20 20 69 6e 74 20  oduleArg;.  int 
27cc4 6e 41 72 67 20 3d 20 70 54 61 62 2d 3e 6e 4d 6f  nArg = pTab->nMo
27cc5 64 75 6c 65 41 72 67 3b 0a 20 20 63 68 61 72 20  duleArg;.  char 
27cc6 2a 7a 45 72 72 20 3d 20 30 3b 0a 20 20 63 68 61  *zErr = 0;.  cha
27cc7 72 20 2a 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 3d  r *zModuleName =
27cc8 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66 28   sqlite3MPrintf(
27cc9 64 62 2c 20 22 25 73 22 2c 20 70 54 61 62 2d 3e  db, "%s", pTab->
27cca 7a 4e 61 6d 65 29 3b 0a 0a 20 20 69 66 28 20 21  zName);..  if( !
27ccb 7a 4d 6f 64 75 6c 65 4e 61 6d 65 20 29 7b 0a 20  zModuleName ){. 
27ccc 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
27ccd 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 61  _NOMEM;.  }..  a
27cce 73 73 65 72 74 28 20 21 64 62 2d 3e 70 56 54 61  ssert( !db->pVTa
27ccf 62 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 78  b );.  assert( x
27cd0 43 6f 6e 73 74 72 75 63 74 20 29 3b 0a 0a 20 20  Construct );..  
27cd1 64 62 2d 3e 70 56 54 61 62 20 3d 20 70 54 61 62  db->pVTab = pTab
27cd2 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
27cd3 53 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20  SafetyOff(db);. 
27cd4 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51 4c   assert( rc==SQL
27cd5 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d  ITE_OK );.  rc =
27cd6 20 78 43 6f 6e 73 74 72 75 63 74 28 64 62 2c 20   xConstruct(db, 
27cd7 70 4d 6f 64 2d 3e 70 41 75 78 2c 20 6e 41 72 67  pMod->pAux, nArg
27cd8 2c 20 61 7a 41 72 67 2c 20 26 70 56 74 61 62 2c  , azArg, &pVtab,
27cd9 20 26 7a 45 72 72 29 3b 0a 20 20 72 63 32 20 3d   &zErr);.  rc2 =
27cda 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e   sqlite3SafetyOn
27cdb 28 64 62 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  (db);.  if( rc==
27cdc 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 56 74  SQLITE_OK && pVt
27cdd 61 62 20 29 7b 0a 20 20 20 20 70 56 74 61 62 2d  ab ){.    pVtab-
27cde 3e 70 4d 6f 64 75 6c 65 20 3d 20 70 4d 6f 64 2d  >pModule = pMod-
27cdf 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 20 20 70 56  >pModule;.    pV
27ce0 74 61 62 2d 3e 6e 52 65 66 20 3d 20 31 3b 0a 20  tab->nRef = 1;. 
27ce1 20 20 20 70 54 61 62 2d 3e 70 56 74 61 62 20 3d     pTab->pVtab =
27ce2 20 70 56 74 61 62 3b 0a 20 20 7d 0a 0a 20 20 69   pVtab;.  }..  i
27ce3 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 72 63  f( SQLITE_OK!=rc
27ce4 20 29 7b 0a 20 20 20 20 69 66 28 20 7a 45 72 72   ){.    if( zErr
27ce5 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a 70 7a  ==0 ){.      *pz
27ce6 45 72 72 20 3d 20 73 71 6c 69 74 65 33 4d 50 72  Err = sqlite3MPr
27ce7 69 6e 74 66 28 64 62 2c 20 22 76 74 61 62 6c 65  intf(db, "vtable
27ce8 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 66 61 69   constructor fai
27ce9 6c 65 64 3a 20 25 73 22 2c 20 7a 4d 6f 64 75 6c  led: %s", zModul
27cea 65 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 65 6c 73  eName);.    }els
27ceb 65 20 7b 0a 20 20 20 20 20 20 2a 70 7a 45 72 72  e {.      *pzErr
27cec 20 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74   = sqlite3MPrint
27ced 66 28 64 62 2c 20 22 25 73 22 2c 20 7a 45 72 72  f(db, "%s", zErr
27cee 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27cef 44 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 29  DbFree(db, zErr)
27cf0 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 20  ;.    }.  }else 
27cf1 69 66 28 20 64 62 2d 3e 70 56 54 61 62 20 29 7b  if( db->pVTab ){
27cf2 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20  .    const char 
27cf3 2a 7a 46 6f 72 6d 61 74 20 3d 20 22 76 74 61 62  *zFormat = "vtab
27cf4 6c 65 20 63 6f 6e 73 74 72 75 63 74 6f 72 20 64  le constructor d
27cf5 69 64 20 6e 6f 74 20 64 65 63 6c 61 72 65 20 73  id not declare s
27cf6 63 68 65 6d 61 3a 20 25 73 22 3b 0a 20 20 20 20  chema: %s";.    
27cf7 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65 33  *pzErr = sqlite3
27cf8 4d 50 72 69 6e 74 66 28 64 62 2c 20 7a 46 6f 72  MPrintf(db, zFor
27cf9 6d 61 74 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d 65  mat, pTab->zName
27cfa 29 3b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  );.    rc = SQLI
27cfb 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 20 0a 20  TE_ERROR;.  } . 
27cfc 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
27cfd 4f 4b 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 72  OK ){.    rc = r
27cfe 63 32 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 70 56  c2;.  }.  db->pV
27cff 54 61 62 20 3d 20 30 3b 0a 20 20 73 71 6c 69 74  Tab = 0;.  sqlit
27d00 65 33 44 62 46 72 65 65 28 64 62 2c 20 7a 4d 6f  e3DbFree(db, zMo
27d01 64 75 6c 65 4e 61 6d 65 29 3b 0a 0a 20 20 2f 2a  duleName);..  /*
27d02 20 49 66 20 65 76 65 72 79 74 68 69 6e 67 20 77   If everything w
27d03 65 6e 74 20 61 63 63 6f 72 64 69 6e 67 20 74 6f  ent according to
27d04 20 70 6c 61 6e 2c 20 6c 6f 6f 70 20 74 68 72 6f   plan, loop thro
27d05 75 67 68 20 74 68 65 20 63 6f 6c 75 6d 6e 73 0a  ugh the columns.
27d06 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61 62 6c    ** of the tabl
27d07 65 20 74 6f 20 73 65 65 20 69 66 20 61 6e 79 20  e to see if any 
27d08 6f 66 20 74 68 65 6d 20 63 6f 6e 74 61 69 6e 20  of them contain 
27d09 74 68 65 20 74 6f 6b 65 6e 20 22 68 69 64 64 65  the token "hidde
27d0a 6e 22 2e 0a 20 20 2a 2a 20 49 66 20 73 6f 2c 20  n"..  ** If so, 
27d0b 73 65 74 20 74 68 65 20 43 6f 6c 75 6d 6e 2e 69  set the Column.i
27d0c 73 48 69 64 64 65 6e 20 66 6c 61 67 20 61 6e 64  sHidden flag and
27d0d 20 72 65 6d 6f 76 65 20 74 68 65 20 74 6f 6b 65   remove the toke
27d0e 6e 20 66 72 6f 6d 0a 20 20 2a 2a 20 74 68 65 20  n from.  ** the 
27d0f 74 79 70 65 20 73 74 72 69 6e 67 2e 0a 20 20 2a  type string..  *
27d10 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  /.  if( rc==SQLI
27d11 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 6e 74  TE_OK ){.    int
27d12 20 69 43 6f 6c 3b 0a 20 20 20 20 66 6f 72 28 69   iCol;.    for(i
27d13 43 6f 6c 3d 30 3b 20 69 43 6f 6c 3c 70 54 61 62  Col=0; iCol<pTab
27d14 2d 3e 6e 43 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b  ->nCol; iCol++){
27d15 0a 20 20 20 20 20 20 63 68 61 72 20 2a 7a 54 79  .      char *zTy
27d16 70 65 20 3d 20 70 54 61 62 2d 3e 61 43 6f 6c 5b  pe = pTab->aCol[
27d17 69 43 6f 6c 5d 2e 7a 54 79 70 65 3b 0a 20 20 20  iCol].zType;.   
27d18 20 20 20 69 6e 74 20 6e 54 79 70 65 3b 0a 20 20     int nType;.  
27d19 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b 0a 20      int i = 0;. 
27d1a 20 20 20 20 20 69 66 28 20 21 7a 54 79 70 65 20       if( !zType 
27d1b 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
27d1c 20 20 6e 54 79 70 65 20 3d 20 73 74 72 6c 65 6e    nType = strlen
27d1d 28 7a 54 79 70 65 29 3b 0a 20 20 20 20 20 20 69  (zType);.      i
27d1e 66 28 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  f( sqlite3StrNIC
27d1f 6d 70 28 22 68 69 64 64 65 6e 22 2c 20 7a 54 79  mp("hidden", zTy
27d20 70 65 2c 20 36 29 20 7c 7c 20 28 7a 54 79 70 65  pe, 6) || (zType
27d21 5b 36 5d 20 26 26 20 7a 54 79 70 65 5b 36 5d 21  [6] && zType[6]!
27d22 3d 27 20 27 29 20 29 7b 0a 20 20 20 20 20 20 20  =' ') ){.       
27d23 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 54 79 70   for(i=0; i<nTyp
27d24 65 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  e; i++){.       
27d25 20 20 20 69 66 28 20 28 30 3d 3d 73 71 6c 69 74     if( (0==sqlit
27d26 65 33 53 74 72 4e 49 43 6d 70 28 22 20 68 69 64  e3StrNICmp(" hid
27d27 64 65 6e 22 2c 20 26 7a 54 79 70 65 5b 69 5d 2c  den", &zType[i],
27d28 20 37 29 29 0a 20 20 20 20 20 20 20 20 20 20 20   7)).           
27d29 26 26 20 28 7a 54 79 70 65 5b 69 2b 37 5d 3d 3d  && (zType[i+7]==
27d2a 27 5c 30 27 20 7c 7c 20 7a 54 79 70 65 5b 69 2b  '\0' || zType[i+
27d2b 37 5d 3d 3d 27 20 27 29 0a 20 20 20 20 20 20 20  7]==' ').       
27d2c 20 20 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20     ){.          
27d2d 20 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20    i++;.         
27d2e 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
27d2f 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
27d30 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
27d31 28 20 69 3c 6e 54 79 70 65 20 29 7b 0a 20 20 20  ( i<nType ){.   
27d32 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20       int j;.    
27d33 20 20 20 20 69 6e 74 20 6e 44 65 6c 20 3d 20 36      int nDel = 6
27d34 20 2b 20 28 7a 54 79 70 65 5b 69 2b 36 5d 20 3f   + (zType[i+6] ?
27d35 20 31 20 3a 20 30 29 3b 0a 20 20 20 20 20 20 20   1 : 0);.       
27d36 20 66 6f 72 28 6a 3d 69 3b 20 28 6a 2b 6e 44 65   for(j=i; (j+nDe
27d37 6c 29 3c 3d 6e 54 79 70 65 3b 20 6a 2b 2b 29 7b  l)<=nType; j++){
27d38 0a 20 20 20 20 20 20 20 20 20 20 7a 54 79 70 65  .          zType
27d39 5b 6a 5d 20 3d 20 7a 54 79 70 65 5b 6a 2b 6e 44  [j] = zType[j+nD
27d3a 65 6c 5d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  el];.        }. 
27d3b 20 20 20 20 20 20 20 69 66 28 20 7a 54 79 70 65         if( zType
27d3c 5b 69 5d 3d 3d 27 5c 30 27 20 26 26 20 69 3e 30  [i]=='\0' && i>0
27d3d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73   ){.          as
27d3e 73 65 72 74 28 7a 54 79 70 65 5b 69 2d 31 5d 3d  sert(zType[i-1]=
27d3f 3d 27 20 27 29 3b 0a 20 20 20 20 20 20 20 20 20  =' ');.         
27d40 20 7a 54 79 70 65 5b 69 2d 31 5d 20 3d 20 27 5c   zType[i-1] = '\
27d41 30 27 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  0';.        }.  
27d42 20 20 20 20 20 20 70 54 61 62 2d 3e 61 43 6f 6c        pTab->aCol
27d43 5b 69 43 6f 6c 5d 2e 69 73 48 69 64 64 65 6e 20  [iCol].isHidden 
27d44 3d 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  = 1;.      }.   
27d45 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
27d46 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  rc;.}../*.** Thi
27d47 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69 6e  s function is in
27d48 76 6f 6b 65 64 20 62 79 20 74 68 65 20 70 61 72  voked by the par
27d49 73 65 72 20 74 6f 20 63 61 6c 6c 20 74 68 65 20  ser to call the 
27d4a 78 43 6f 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f  xConnect() metho
27d4b 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76 69 72 74  d.** of the virt
27d4c 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 2e 20  ual table pTab. 
27d4d 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63 75  If an error occu
27d4e 72 73 2c 20 61 6e 20 65 72 72 6f 72 20 63 6f 64  rs, an error cod
27d4f 65 20 69 73 20 72 65 74 75 72 6e 65 64 20 0a 2a  e is returned .*
27d50 2a 20 61 6e 64 20 61 6e 20 65 72 72 6f 72 20 6c  * and an error l
27d51 65 66 74 20 69 6e 20 70 50 61 72 73 65 2e 0a 2a  eft in pParse..*
27d52 2a 0a 2a 2a 20 54 68 69 73 20 63 61 6c 6c 20 69  *.** This call i
27d53 73 20 61 20 6e 6f 2d 6f 70 20 69 66 20 74 61 62  s a no-op if tab
27d54 6c 65 20 70 54 61 62 20 69 73 20 6e 6f 74 20 61  le pTab is not a
27d55 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a   virtual table..
27d56 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
27d57 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
27d58 62 43 61 6c 6c 43 6f 6e 6e 65 63 74 28 50 61 72  bCallConnect(Par
27d59 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
27d5a 65 20 2a 70 54 61 62 29 7b 0a 20 20 4d 6f 64 75  e *pTab){.  Modu
27d5b 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 69 6e 74 20  le *pMod;.  int 
27d5c 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
27d5d 0a 20 20 69 66 28 20 21 70 54 61 62 20 7c 7c 20  .  if( !pTab || 
27d5e 28 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20  (pTab->tabFlags 
27d5f 26 20 54 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30  & TF_Virtual)==0
27d60 20 7c 7c 20 70 54 61 62 2d 3e 70 56 74 61 62 20   || pTab->pVtab 
27d61 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
27d62 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20  LITE_OK;.  }..  
27d63 70 4d 6f 64 20 3d 20 70 54 61 62 2d 3e 70 4d 6f  pMod = pTab->pMo
27d64 64 3b 0a 20 20 69 66 28 20 21 70 4d 6f 64 20 29  d;.  if( !pMod )
27d65 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  {.    const char
27d66 20 2a 7a 4d 6f 64 75 6c 65 20 3d 20 70 54 61 62   *zModule = pTab
27d67 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67 5b 30 5d  ->azModuleArg[0]
27d68 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72 72  ;.    sqlite3Err
27d69 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22 6e  orMsg(pParse, "n
27d6a 6f 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25  o such module: %
27d6b 73 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20  s", zModule);.  
27d6c 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52    rc = SQLITE_ER
27d6d 52 4f 52 3b 0a 20 20 7d 20 65 6c 73 65 20 7b 0a  ROR;.  } else {.
27d6e 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d      char *zErr =
27d6f 20 30 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 20   0;.    sqlite3 
27d70 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
27d71 3b 0a 20 20 20 20 72 63 20 3d 20 76 74 61 62 43  ;.    rc = vtabC
27d72 61 6c 6c 43 6f 6e 73 74 72 75 63 74 6f 72 28 64  allConstructor(d
27d73 62 2c 20 70 54 61 62 2c 20 70 4d 6f 64 2c 20 70  b, pTab, pMod, p
27d74 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 43  Mod->pModule->xC
27d75 6f 6e 6e 65 63 74 2c 20 26 7a 45 72 72 29 3b 0a  onnect, &zErr);.
27d76 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
27d77 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 73  TE_OK ){.      s
27d78 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
27d79 50 61 72 73 65 2c 20 22 25 73 22 2c 20 7a 45 72  Parse, "%s", zEr
27d7a 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 73 71  r);.    }.    sq
27d7b 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
27d7c 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20 20 72 65  zErr);.  }..  re
27d7d 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
27d7e 2a 20 41 64 64 20 74 68 65 20 76 69 72 74 75 61  * Add the virtua
27d7f 6c 20 74 61 62 6c 65 20 70 56 74 61 62 20 74 6f  l table pVtab to
27d80 20 74 68 65 20 61 72 72 61 79 20 73 71 6c 69 74   the array sqlit
27d81 65 33 2e 61 56 54 72 61 6e 73 5b 5d 2e 0a 2a 2f  e3.aVTrans[]..*/
27d82 0a 73 74 61 74 69 63 20 69 6e 74 20 61 64 64 54  .static int addT
27d83 6f 56 54 72 61 6e 73 28 73 71 6c 69 74 65 33 20  oVTrans(sqlite3 
27d84 2a 64 62 2c 20 73 71 6c 69 74 65 33 5f 76 74 61  *db, sqlite3_vta
27d85 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 63 6f 6e  b *pVtab){.  con
27d86 73 74 20 69 6e 74 20 41 52 52 41 59 5f 49 4e 43  st int ARRAY_INC
27d87 52 20 3d 20 35 3b 0a 0a 20 20 2f 2a 20 47 72 6f  R = 5;..  /* Gro
27d88 77 20 74 68 65 20 73 71 6c 69 74 65 33 2e 61 56  w the sqlite3.aV
27d89 54 72 61 6e 73 20 61 72 72 61 79 20 69 66 20 72  Trans array if r
27d8a 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 69 66 28  equired */.  if(
27d8b 20 28 64 62 2d 3e 6e 56 54 72 61 6e 73 25 41 52   (db->nVTrans%AR
27d8c 52 41 59 5f 49 4e 43 52 29 3d 3d 30 20 29 7b 0a  RAY_INCR)==0 ){.
27d8d 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
27d8e 20 2a 2a 61 56 54 72 61 6e 73 3b 0a 20 20 20 20   **aVTrans;.    
27d8f 69 6e 74 20 6e 42 79 74 65 73 20 3d 20 73 69 7a  int nBytes = siz
27d90 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  eof(sqlite3_vtab
27d91 20 2a 29 20 2a 20 28 64 62 2d 3e 6e 56 54 72 61   *) * (db->nVTra
27d92 6e 73 20 2b 20 41 52 52 41 59 5f 49 4e 43 52 29  ns + ARRAY_INCR)
27d93 3b 0a 20 20 20 20 61 56 54 72 61 6e 73 20 3d 20  ;.    aVTrans = 
27d94 73 71 6c 69 74 65 33 44 62 52 65 61 6c 6c 6f 63  sqlite3DbRealloc
27d95 28 64 62 2c 20 28 76 6f 69 64 20 2a 29 64 62 2d  (db, (void *)db-
27d96 3e 61 56 54 72 61 6e 73 2c 20 6e 42 79 74 65 73  >aVTrans, nBytes
27d97 29 3b 0a 20 20 20 20 69 66 28 20 21 61 56 54 72  );.    if( !aVTr
27d98 61 6e 73 20 29 7b 0a 20 20 20 20 20 20 72 65 74  ans ){.      ret
27d99 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  urn SQLITE_NOMEM
27d9a 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6d 65 6d 73  ;.    }.    mems
27d9b 65 74 28 26 61 56 54 72 61 6e 73 5b 64 62 2d 3e  et(&aVTrans[db->
27d9c 6e 56 54 72 61 6e 73 5d 2c 20 30 2c 20 73 69 7a  nVTrans], 0, siz
27d9d 65 6f 66 28 73 71 6c 69 74 65 33 5f 76 74 61 62  eof(sqlite3_vtab
27d9e 20 2a 29 2a 41 52 52 41 59 5f 49 4e 43 52 29 3b   *)*ARRAY_INCR);
27d9f 0a 20 20 20 20 64 62 2d 3e 61 56 54 72 61 6e 73  .    db->aVTrans
27da0 20 3d 20 61 56 54 72 61 6e 73 3b 0a 20 20 7d 0a   = aVTrans;.  }.
27da1 0a 20 20 2f 2a 20 41 64 64 20 70 56 74 61 62 20  .  /* Add pVtab 
27da2 74 6f 20 74 68 65 20 65 6e 64 20 6f 66 20 73 71  to the end of sq
27da3 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 20 2a 2f  lite3.aVTrans */
27da4 0a 20 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 64  .  db->aVTrans[d
27da5 62 2d 3e 6e 56 54 72 61 6e 73 2b 2b 5d 20 3d 20  b->nVTrans++] = 
27da6 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVtab;.  sqlite3
27da7 56 74 61 62 4c 6f 63 6b 28 70 56 74 61 62 29 3b  VtabLock(pVtab);
27da8 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
27da9 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68  _OK;.}../*.** Th
27daa 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 69  is function is i
27dab 6e 76 6f 6b 65 64 20 62 79 20 74 68 65 20 76 64  nvoked by the vd
27dac 62 65 20 74 6f 20 63 61 6c 6c 20 74 68 65 20 78  be to call the x
27dad 43 72 65 61 74 65 20 6d 65 74 68 6f 64 0a 2a 2a  Create method.**
27dae 20 6f 66 20 74 68 65 20 76 69 72 74 75 61 6c 20   of the virtual 
27daf 74 61 62 6c 65 20 6e 61 6d 65 64 20 7a 54 61 62  table named zTab
27db0 20 69 6e 20 64 61 74 61 62 61 73 65 20 69 44 62   in database iDb
27db1 2e 20 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 20 65  . .**.** If an e
27db2 72 72 6f 72 20 6f 63 63 75 72 73 2c 20 2a 70 7a  rror occurs, *pz
27db3 45 72 72 20 69 73 20 73 65 74 20 74 6f 20 70 6f  Err is set to po
27db4 69 6e 74 20 61 6e 20 61 6e 20 45 6e 67 6c 69 73  int an an Englis
27db5 68 20 6c 61 6e 67 75 61 67 65 0a 2a 2a 20 64 65  h language.** de
27db6 73 63 72 69 70 74 69 6f 6e 20 6f 66 20 74 68 65  scription of the
27db7 20 65 72 72 6f 72 20 61 6e 64 20 61 6e 20 53 51   error and an SQ
27db8 4c 49 54 45 5f 58 58 58 20 65 72 72 6f 72 20 63  LITE_XXX error c
27db9 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e  ode is returned.
27dba 0a 2a 2a 20 49 6e 20 74 68 69 73 20 63 61 73 65  .** In this case
27dbb 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75 73 74   the caller must
27dbc 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 44 62 46   call sqlite3DbF
27dbd 72 65 65 28 64 62 2c 20 29 20 6f 6e 20 2a 70 7a  ree(db, ) on *pz
27dbe 45 72 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  Err..*/.SQLITE_P
27dbf 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
27dc0 65 33 56 74 61 62 43 61 6c 6c 43 72 65 61 74 65  e3VtabCallCreate
27dc1 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e  (sqlite3 *db, in
27dc2 74 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61  t iDb, const cha
27dc3 72 20 2a 7a 54 61 62 2c 20 63 68 61 72 20 2a 2a  r *zTab, char **
27dc4 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63  pzErr){.  int rc
27dc5 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
27dc6 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 4d  Table *pTab;.  M
27dc7 6f 64 75 6c 65 20 2a 70 4d 6f 64 3b 0a 20 20 63  odule *pMod;.  c
27dc8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4d 6f 64 75  onst char *zModu
27dc9 6c 65 3b 0a 0a 20 20 70 54 61 62 20 3d 20 73 71  le;..  pTab = sq
27dca 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
27dcb 62 2c 20 7a 54 61 62 2c 20 64 62 2d 3e 61 44 62  b, zTab, db->aDb
27dcc 5b 69 44 62 5d 2e 7a 4e 61 6d 65 29 3b 0a 20 20  [iDb].zName);.  
27dcd 61 73 73 65 72 74 28 70 54 61 62 20 26 26 20 28  assert(pTab && (
27dce 70 54 61 62 2d 3e 74 61 62 46 6c 61 67 73 20 26  pTab->tabFlags &
27dcf 20 54 46 5f 56 69 72 74 75 61 6c 29 21 3d 30 20   TF_Virtual)!=0 
27dd0 26 26 20 21 70 54 61 62 2d 3e 70 56 74 61 62 29  && !pTab->pVtab)
27dd1 3b 0a 20 20 70 4d 6f 64 20 3d 20 70 54 61 62 2d  ;.  pMod = pTab-
27dd2 3e 70 4d 6f 64 3b 0a 20 20 7a 4d 6f 64 75 6c 65  >pMod;.  zModule
27dd3 20 3d 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c   = pTab->azModul
27dd4 65 41 72 67 5b 30 5d 3b 0a 0a 20 20 2f 2a 20 49  eArg[0];..  /* I
27dd5 66 20 74 68 65 20 6d 6f 64 75 6c 65 20 68 61 73  f the module has
27dd6 20 62 65 65 6e 20 72 65 67 69 73 74 65 72 65 64   been registered
27dd7 20 61 6e 64 20 69 6e 63 6c 75 64 65 73 20 61 20   and includes a 
27dd8 43 72 65 61 74 65 20 6d 65 74 68 6f 64 2c 20 0a  Create method, .
27dd9 20 20 2a 2a 20 69 6e 76 6f 6b 65 20 69 74 20 6e    ** invoke it n
27dda 6f 77 2e 20 49 66 20 74 68 65 20 6d 6f 64 75 6c  ow. If the modul
27ddb 65 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 20 72  e has not been r
27ddc 65 67 69 73 74 65 72 65 64 2c 20 72 65 74 75 72  egistered, retur
27ddd 6e 20 61 6e 20 0a 20 20 2a 2a 20 65 72 72 6f 72  n an .  ** error
27dde 2e 20 4f 74 68 65 72 77 69 73 65 2c 20 64 6f 20  . Otherwise, do 
27ddf 6e 6f 74 68 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20  nothing..  */.  
27de0 69 66 28 20 21 70 4d 6f 64 20 29 7b 0a 20 20 20  if( !pMod ){.   
27de1 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c 69 74 65   *pzErr = sqlite
27de2 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 6e 6f  3MPrintf(db, "no
27de3 20 73 75 63 68 20 6d 6f 64 75 6c 65 3a 20 25 73   such module: %s
27de4 22 2c 20 7a 4d 6f 64 75 6c 65 29 3b 0a 20 20 20  ", zModule);.   
27de5 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
27de6 4f 52 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  OR;.  }else{.   
27de7 20 72 63 20 3d 20 76 74 61 62 43 61 6c 6c 43 6f   rc = vtabCallCo
27de8 6e 73 74 72 75 63 74 6f 72 28 64 62 2c 20 70 54  nstructor(db, pT
27de9 61 62 2c 20 70 4d 6f 64 2c 20 70 4d 6f 64 2d 3e  ab, pMod, pMod->
27dea 70 4d 6f 64 75 6c 65 2d 3e 78 43 72 65 61 74 65  pModule->xCreate
27deb 2c 20 70 7a 45 72 72 29 3b 0a 20 20 7d 0a 0a 20  , pzErr);.  }.. 
27dec 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
27ded 4f 4b 20 26 26 20 70 54 61 62 2d 3e 70 56 74 61  OK && pTab->pVta
27dee 62 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  b ){.      rc = 
27def 61 64 64 54 6f 56 54 72 61 6e 73 28 64 62 2c 20  addToVTrans(db, 
27df0 70 54 61 62 2d 3e 70 56 74 61 62 29 3b 0a 20 20  pTab->pVtab);.  
27df1 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  }..  return rc;.
27df2 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75  }../*.** This fu
27df3 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 74  nction is used t
27df4 6f 20 73 65 74 20 74 68 65 20 73 63 68 65 6d 61  o set the schema
27df5 20 6f 66 20 61 20 76 69 72 74 75 61 6c 20 74 61   of a virtual ta
27df6 62 6c 65 2e 20 20 49 74 20 69 73 20 6f 6e 6c 79  ble.  It is only
27df7 0a 2a 2a 20 76 61 6c 69 64 20 74 6f 20 63 61 6c  .** valid to cal
27df8 6c 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  l this function 
27df9 66 72 6f 6d 20 77 69 74 68 69 6e 20 74 68 65 20  from within the 
27dfa 78 43 72 65 61 74 65 28 29 20 6f 72 20 78 43 6f  xCreate() or xCo
27dfb 6e 6e 65 63 74 28 29 20 6f 66 20 61 0a 2a 2a 20  nnect() of a.** 
27dfc 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
27dfd 64 75 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  dule..*/.SQLITE_
27dfe 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
27dff 64 65 63 6c 61 72 65 5f 76 74 61 62 28 73 71 6c  declare_vtab(sql
27e00 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
27e01 63 68 61 72 20 2a 7a 43 72 65 61 74 65 54 61 62  char *zCreateTab
27e02 6c 65 29 7b 0a 20 20 50 61 72 73 65 20 73 50 61  le){.  Parse sPa
27e03 72 73 65 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d  rse;..  int rc =
27e04 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 54 61   SQLITE_OK;.  Ta
27e05 62 6c 65 20 2a 70 54 61 62 3b 0a 20 20 63 68 61  ble *pTab;.  cha
27e06 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a 0a 20 20  r *zErr = 0;..  
27e07 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e  sqlite3_mutex_en
27e08 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ter(db->mutex);.
27e09 20 20 70 54 61 62 20 3d 20 64 62 2d 3e 70 56 54    pTab = db->pVT
27e0a 61 62 3b 0a 20 20 69 66 28 20 21 70 54 61 62 20  ab;.  if( !pTab 
27e0b 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
27e0c 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4d  ror(db, SQLITE_M
27e0d 49 53 55 53 45 2c 20 30 29 3b 0a 20 20 20 20 73  ISUSE, 0);.    s
27e0e 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
27e0f 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
27e10 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
27e11 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20 61  _MISUSE;.  }.  a
27e12 73 73 65 72 74 28 28 70 54 61 62 2d 3e 74 61 62  ssert((pTab->tab
27e13 46 6c 61 67 73 20 26 20 54 46 5f 56 69 72 74 75  Flags & TF_Virtu
27e14 61 6c 29 21 3d 30 20 26 26 20 70 54 61 62 2d 3e  al)!=0 && pTab->
27e15 6e 43 6f 6c 3d 3d 30 20 26 26 20 70 54 61 62 2d  nCol==0 && pTab-
27e16 3e 61 43 6f 6c 3d 3d 30 29 3b 0a 0a 20 20 6d 65  >aCol==0);..  me
27e17 6d 73 65 74 28 26 73 50 61 72 73 65 2c 20 30 2c  mset(&sParse, 0,
27e18 20 73 69 7a 65 6f 66 28 50 61 72 73 65 29 29 3b   sizeof(Parse));
27e19 0a 20 20 73 50 61 72 73 65 2e 64 65 63 6c 61 72  .  sParse.declar
27e1a 65 56 74 61 62 20 3d 20 31 3b 0a 20 20 73 50 61  eVtab = 1;.  sPa
27e1b 72 73 65 2e 64 62 20 3d 20 64 62 3b 0a 0a 20 20  rse.db = db;..  
27e1c 69 66 28 20 0a 20 20 20 20 20 20 53 51 4c 49 54  if( .      SQLIT
27e1d 45 5f 4f 4b 20 3d 3d 20 73 71 6c 69 74 65 33 52  E_OK == sqlite3R
27e1e 75 6e 50 61 72 73 65 72 28 26 73 50 61 72 73 65  unParser(&sParse
27e1f 2c 20 7a 43 72 65 61 74 65 54 61 62 6c 65 2c 20  , zCreateTable, 
27e20 26 7a 45 72 72 29 20 26 26 20 0a 20 20 20 20 20  &zErr) && .     
27e21 20 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62 6c   sParse.pNewTabl
27e22 65 20 26 26 20 0a 20 20 20 20 20 20 21 73 50 61  e && .      !sPa
27e23 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 70  rse.pNewTable->p
27e24 53 65 6c 65 63 74 20 26 26 20 0a 20 20 20 20 20  Select && .     
27e25 20 28 73 50 61 72 73 65 2e 70 4e 65 77 54 61 62   (sParse.pNewTab
27e26 6c 65 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54  le->tabFlags & T
27e27 46 5f 56 69 72 74 75 61 6c 29 3d 3d 30 0a 20 20  F_Virtual)==0.  
27e28 29 7b 0a 20 20 20 20 70 54 61 62 2d 3e 61 43 6f  ){.    pTab->aCo
27e29 6c 20 3d 20 73 50 61 72 73 65 2e 70 4e 65 77 54  l = sParse.pNewT
27e2a 61 62 6c 65 2d 3e 61 43 6f 6c 3b 0a 20 20 20 20  able->aCol;.    
27e2b 70 54 61 62 2d 3e 6e 43 6f 6c 20 3d 20 73 50 61  pTab->nCol = sPa
27e2c 72 73 65 2e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e  rse.pNewTable->n
27e2d 43 6f 6c 3b 0a 20 20 20 20 73 50 61 72 73 65 2e  Col;.    sParse.
27e2e 70 4e 65 77 54 61 62 6c 65 2d 3e 6e 43 6f 6c 20  pNewTable->nCol 
27e2f 3d 20 30 3b 0a 20 20 20 20 73 50 61 72 73 65 2e  = 0;.    sParse.
27e30 70 4e 65 77 54 61 62 6c 65 2d 3e 61 43 6f 6c 20  pNewTable->aCol 
27e31 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 70 56 54  = 0;.    db->pVT
27e32 61 62 20 3d 20 30 3b 0a 20 20 7d 20 65 6c 73 65  ab = 0;.  } else
27e33 20 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72   {.    sqlite3Er
27e34 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 45  ror(db, SQLITE_E
27e35 52 52 4f 52 2c 20 7a 45 72 72 29 3b 0a 20 20 20  RROR, zErr);.   
27e36 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
27e37 62 2c 20 7a 45 72 72 29 3b 0a 20 20 20 20 72 63  b, zErr);.    rc
27e38 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
27e39 0a 20 20 7d 0a 20 20 73 50 61 72 73 65 2e 64 65  .  }.  sParse.de
27e3a 63 6c 61 72 65 56 74 61 62 20 3d 20 30 3b 0a 0a  clareVtab = 0;..
27e3b 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69    sqlite3_finali
27e3c 7a 65 28 28 73 71 6c 69 74 65 33 5f 73 74 6d 74  ze((sqlite3_stmt
27e3d 2a 29 73 50 61 72 73 65 2e 70 56 64 62 65 29 3b  *)sParse.pVdbe);
27e3e 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65 74 65  .  sqlite3Delete
27e3f 54 61 62 6c 65 28 73 50 61 72 73 65 2e 70 4e 65  Table(sParse.pNe
27e40 77 54 61 62 6c 65 29 3b 0a 20 20 73 50 61 72 73  wTable);.  sPars
27e41 65 2e 70 4e 65 77 54 61 62 6c 65 20 3d 20 30 3b  e.pNewTable = 0;
27e42 0a 0a 20 20 61 73 73 65 72 74 28 20 28 72 63 26  ..  assert( (rc&
27e43 30 78 66 66 29 3d 3d 72 63 20 29 3b 0a 20 20 72  0xff)==rc );.  r
27e44 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
27e45 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
27e46 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
27e47 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
27e48 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
27e49 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f  .** This functio
27e4a 6e 20 69 73 20 69 6e 76 6f 6b 65 64 20 62 79 20  n is invoked by 
27e4b 74 68 65 20 76 64 62 65 20 74 6f 20 63 61 6c 6c  the vdbe to call
27e4c 20 74 68 65 20 78 44 65 73 74 72 6f 79 20 6d 65   the xDestroy me
27e4d 74 68 6f 64 0a 2a 2a 20 6f 66 20 74 68 65 20 76  thod.** of the v
27e4e 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6e 61 6d  irtual table nam
27e4f 65 64 20 7a 54 61 62 20 69 6e 20 64 61 74 61 62  ed zTab in datab
27e50 61 73 65 20 69 44 62 2e 20 54 68 69 73 20 6f 63  ase iDb. This oc
27e51 63 75 72 73 0a 2a 2a 20 77 68 65 6e 20 61 20 44  curs.** when a D
27e52 52 4f 50 20 54 41 42 4c 45 20 69 73 20 6d 65 6e  ROP TABLE is men
27e53 74 69 6f 6e 65 64 2e 0a 2a 2a 0a 2a 2a 20 54 68  tioned..**.** Th
27e54 69 73 20 63 61 6c 6c 20 69 73 20 61 20 6e 6f 2d  is call is a no-
27e55 6f 70 20 69 66 20 7a 54 61 62 20 69 73 20 6e 6f  op if zTab is no
27e56 74 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  t a virtual tabl
27e57 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  e..*/.SQLITE_PRI
27e58 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
27e59 56 74 61 62 43 61 6c 6c 44 65 73 74 72 6f 79 28  VtabCallDestroy(
27e5a 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 69 6e 74  sqlite3 *db, int
27e5b 20 69 44 62 2c 20 63 6f 6e 73 74 20 63 68 61 72   iDb, const char
27e5c 20 2a 7a 54 61 62 29 0a 7b 0a 20 20 69 6e 74 20   *zTab).{.  int 
27e5d 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
27e5e 20 20 54 61 62 6c 65 20 2a 70 54 61 62 3b 0a 0a    Table *pTab;..
27e5f 20 20 70 54 61 62 20 3d 20 73 71 6c 69 74 65 33    pTab = sqlite3
27e60 46 69 6e 64 54 61 62 6c 65 28 64 62 2c 20 7a 54  FindTable(db, zT
27e61 61 62 2c 20 64 62 2d 3e 61 44 62 5b 69 44 62 5d  ab, db->aDb[iDb]
27e62 2e 7a 4e 61 6d 65 29 3b 0a 20 20 61 73 73 65 72  .zName);.  asser
27e63 74 28 70 54 61 62 29 3b 0a 20 20 69 66 28 20 70  t(pTab);.  if( p
27e64 54 61 62 2d 3e 70 56 74 61 62 20 29 7b 0a 20 20  Tab->pVtab ){.  
27e65 20 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79    int (*xDestroy
27e66 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  )(sqlite3_vtab *
27e67 70 56 54 61 62 29 20 3d 20 70 54 61 62 2d 3e 70  pVTab) = pTab->p
27e68 4d 6f 64 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 44  Mod->pModule->xD
27e69 65 73 74 72 6f 79 3b 0a 20 20 20 20 72 63 20 3d  estroy;.    rc =
27e6a 20 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 66   sqlite3SafetyOf
27e6b 66 28 64 62 29 3b 0a 20 20 20 20 61 73 73 65 72  f(db);.    asser
27e6c 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  t( rc==SQLITE_OK
27e6d 20 29 3b 0a 20 20 20 20 69 66 28 20 78 44 65 73   );.    if( xDes
27e6e 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 72 63  troy ){.      rc
27e6f 20 3d 20 78 44 65 73 74 72 6f 79 28 70 54 61 62   = xDestroy(pTab
27e70 2d 3e 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a  ->pVtab);.    }.
27e71 20 20 20 20 28 76 6f 69 64 29 73 71 6c 69 74 65      (void)sqlite
27e72 33 53 61 66 65 74 79 4f 6e 28 64 62 29 3b 0a 20  3SafetyOn(db);. 
27e73 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
27e74 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 69 6e  E_OK ){.      in
27e75 74 20 69 3b 0a 20 20 20 20 20 20 66 6f 72 28 69  t i;.      for(i
27e76 3d 30 3b 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e  =0; i<db->nVTran
27e77 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20  s; i++){.       
27e78 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73   if( db->aVTrans
27e79 5b 69 5d 3d 3d 70 54 61 62 2d 3e 70 56 74 61 62  [i]==pTab->pVtab
27e7a 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64 62   ){.          db
27e7b 2d 3e 61 56 54 72 61 6e 73 5b 69 5d 20 3d 20 64  ->aVTrans[i] = d
27e7c 62 2d 3e 61 56 54 72 61 6e 73 5b 2d 2d 64 62 2d  b->aVTrans[--db-
27e7d 3e 6e 56 54 72 61 6e 73 5d 3b 0a 20 20 20 20 20  >nVTrans];.     
27e7e 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
27e7f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
27e80 20 20 20 20 70 54 61 62 2d 3e 70 56 74 61 62 20      pTab->pVtab 
27e81 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  = 0;.    }.  }..
27e82 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
27e83 2f 2a 0a 2a 2a 20 54 68 69 73 20 66 75 6e 63 74  /*.** This funct
27e84 69 6f 6e 20 69 6e 76 6f 6b 65 73 20 65 69 74 68  ion invokes eith
27e85 65 72 20 74 68 65 20 78 52 6f 6c 6c 62 61 63 6b  er the xRollback
27e86 20 6f 72 20 78 43 6f 6d 6d 69 74 20 6d 65 74 68   or xCommit meth
27e87 6f 64 0a 2a 2a 20 6f 66 20 65 61 63 68 20 6f 66  od.** of each of
27e88 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62   the virtual tab
27e89 6c 65 73 20 69 6e 20 74 68 65 20 73 71 6c 69 74  les in the sqlit
27e8a 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79  e3.aVTrans array
27e8b 2e 20 54 68 65 20 6d 65 74 68 6f 64 0a 2a 2a 20  . The method.** 
27e8c 63 61 6c 6c 65 64 20 69 73 20 69 64 65 6e 74 69  called is identi
27e8d 66 69 65 64 20 62 79 20 74 68 65 20 73 65 63 6f  fied by the seco
27e8e 6e 64 20 61 72 67 75 6d 65 6e 74 2c 20 22 6f 66  nd argument, "of
27e8f 66 73 65 74 22 2c 20 77 68 69 63 68 20 69 73 0a  fset", which is.
27e90 2a 2a 20 74 68 65 20 6f 66 66 73 65 74 20 6f 66  ** the offset of
27e91 20 74 68 65 20 6d 65 74 68 6f 64 20 74 6f 20 63   the method to c
27e92 61 6c 6c 20 69 6e 20 74 68 65 20 73 71 6c 69 74  all in the sqlit
27e93 65 33 5f 6d 6f 64 75 6c 65 20 73 74 72 75 63 74  e3_module struct
27e94 75 72 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61  ure..**.** The a
27e95 72 72 61 79 20 69 73 20 63 6c 65 61 72 65 64 20  rray is cleared 
27e96 61 66 74 65 72 20 69 6e 76 6f 6b 69 6e 67 20 74  after invoking t
27e97 68 65 20 63 61 6c 6c 62 61 63 6b 73 2e 20 0a 2a  he callbacks. .*
27e98 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 61  /.static void ca
27e99 6c 6c 46 69 6e 61 6c 69 73 65 72 28 73 71 6c 69  llFinaliser(sqli
27e9a 74 65 33 20 2a 64 62 2c 20 69 6e 74 20 6f 66 66  te3 *db, int off
27e9b 73 65 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20  set){.  int i;. 
27e9c 20 69 66 28 20 64 62 2d 3e 61 56 54 72 61 6e 73   if( db->aVTrans
27e9d 20 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b   ){.    for(i=0;
27e9e 20 69 3c 64 62 2d 3e 6e 56 54 72 61 6e 73 20 26   i<db->nVTrans &
27e9f 26 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 69 5d  & db->aVTrans[i]
27ea0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71  ; i++){.      sq
27ea1 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
27ea2 62 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b  b = db->aVTrans[
27ea3 69 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 28 2a  i];.      int (*
27ea4 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  x)(sqlite3_vtab 
27ea5 2a 29 3b 0a 20 20 20 20 20 20 78 20 3d 20 2a 28  *);.      x = *(
27ea6 69 6e 74 20 28 2a 2a 29 28 73 71 6c 69 74 65 33  int (**)(sqlite3
27ea7 5f 76 74 61 62 20 2a 29 29 28 28 63 68 61 72 20  _vtab *))((char 
27ea8 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  *)pVtab->pModule
27ea9 20 2b 20 6f 66 66 73 65 74 29 3b 0a 20 20 20 20   + offset);.    
27eaa 20 20 69 66 28 20 78 20 29 20 78 28 70 56 74 61    if( x ) x(pVta
27eab 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  b);.      sqlite
27eac 33 56 74 61 62 55 6e 6c 6f 63 6b 28 64 62 2c 20  3VtabUnlock(db, 
27ead 70 56 74 61 62 29 3b 0a 20 20 20 20 7d 0a 20 20  pVtab);.    }.  
27eae 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
27eaf 64 62 2c 20 64 62 2d 3e 61 56 54 72 61 6e 73 29  db, db->aVTrans)
27eb0 3b 0a 20 20 20 20 64 62 2d 3e 6e 56 54 72 61 6e  ;.    db->nVTran
27eb1 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d 3e 61  s = 0;.    db->a
27eb2 56 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 7d 0a  VTrans = 0;.  }.
27eb3 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f 6b 65 20  }../*.** Invoke 
27eb4 74 68 65 20 78 53 79 6e 63 20 6d 65 74 68 6f 64  the xSync method
27eb5 20 6f 66 20 61 6c 6c 20 76 69 72 74 75 61 6c 20   of all virtual 
27eb6 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 73 71  tables in the sq
27eb7 6c 69 74 65 33 2e 61 56 54 72 61 6e 73 0a 2a 2a  lite3.aVTrans.**
27eb8 20 61 72 72 61 79 2e 20 52 65 74 75 72 6e 20 74   array. Return t
27eb9 68 65 20 65 72 72 6f 72 20 63 6f 64 65 20 66 6f  he error code fo
27eba 72 20 74 68 65 20 66 69 72 73 74 20 65 72 72 6f  r the first erro
27ebb 72 20 74 68 61 74 20 6f 63 63 75 72 73 2c 20 6f  r that occurs, o
27ebc 72 0a 2a 2a 20 53 51 4c 49 54 45 5f 4f 4b 20 69  r.** SQLITE_OK i
27ebd 66 20 61 6c 6c 20 78 53 79 6e 63 20 6f 70 65 72  f all xSync oper
27ebe 61 74 69 6f 6e 73 20 61 72 65 20 73 75 63 63 65  ations are succe
27ebf 73 73 66 75 6c 2e 0a 2a 2a 0a 2a 2a 20 53 65 74  ssful..**.** Set
27ec0 20 2a 70 7a 45 72 72 6d 73 67 20 74 6f 20 70 6f   *pzErrmsg to po
27ec1 69 6e 74 20 74 6f 20 61 20 62 75 66 66 65 72 20  int to a buffer 
27ec2 74 68 61 74 20 73 68 6f 75 6c 64 20 62 65 20 72  that should be r
27ec3 65 6c 65 61 73 65 64 20 75 73 69 6e 67 20 0a 2a  eleased using .*
27ec4 2a 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28  * sqlite3DbFree(
27ec5 29 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20  ) containing an 
27ec6 65 72 72 6f 72 20 6d 65 73 73 61 67 65 2c 20 69  error message, i
27ec7 66 20 6f 6e 65 20 69 73 20 61 76 61 69 6c 61 62  f one is availab
27ec8 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52  le..*/.SQLITE_PR
27ec9 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
27eca 33 56 74 61 62 53 79 6e 63 28 73 71 6c 69 74 65  3VtabSync(sqlite
27ecb 33 20 2a 64 62 2c 20 63 68 61 72 20 2a 2a 70 7a  3 *db, char **pz
27ecc 45 72 72 6d 73 67 29 7b 0a 20 20 69 6e 74 20 69  Errmsg){.  int i
27ecd 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c  ;.  int rc = SQL
27ece 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 72 63  ITE_OK;.  int rc
27ecf 73 61 66 65 74 79 3b 0a 20 20 73 71 6c 69 74 65  safety;.  sqlite
27ed0 33 5f 76 74 61 62 20 2a 2a 61 56 54 72 61 6e 73  3_vtab **aVTrans
27ed1 20 3d 20 64 62 2d 3e 61 56 54 72 61 6e 73 3b 0a   = db->aVTrans;.
27ed2 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 53  .  rc = sqlite3S
27ed3 61 66 65 74 79 4f 66 66 28 64 62 29 3b 0a 20 20  afetyOff(db);.  
27ed4 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 30 3b  db->aVTrans = 0;
27ed5 0a 20 20 66 6f 72 28 69 3d 30 3b 20 72 63 3d 3d  .  for(i=0; rc==
27ed6 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 3c 64  SQLITE_OK && i<d
27ed7 62 2d 3e 6e 56 54 72 61 6e 73 20 26 26 20 61 56  b->nVTrans && aV
27ed8 54 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a  Trans[i]; i++){.
27ed9 20 20 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62      sqlite3_vtab
27eda 20 2a 70 56 74 61 62 20 3d 20 61 56 54 72 61 6e   *pVtab = aVTran
27edb 73 5b 69 5d 3b 0a 20 20 20 20 69 6e 74 20 28 2a  s[i];.    int (*
27edc 78 29 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  x)(sqlite3_vtab 
27edd 2a 29 3b 0a 20 20 20 20 78 20 3d 20 70 56 74 61  *);.    x = pVta
27ede 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 53 79 6e  b->pModule->xSyn
27edf 63 3b 0a 20 20 20 20 69 66 28 20 78 20 29 7b 0a  c;.    if( x ){.
27ee0 20 20 20 20 20 20 72 63 20 3d 20 78 28 70 56 74        rc = x(pVt
27ee1 61 62 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  ab);.      sqlit
27ee2 65 33 44 62 46 72 65 65 28 64 62 2c 20 2a 70 7a  e3DbFree(db, *pz
27ee3 45 72 72 6d 73 67 29 3b 0a 20 20 20 20 20 20 2a  Errmsg);.      *
27ee4 70 7a 45 72 72 6d 73 67 20 3d 20 70 56 74 61 62  pzErrmsg = pVtab
27ee5 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20 20  ->zErrMsg;.     
27ee6 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20   pVtab->zErrMsg 
27ee7 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  = 0;.    }.  }. 
27ee8 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 3d 20 61   db->aVTrans = a
27ee9 56 54 72 61 6e 73 3b 0a 20 20 72 63 73 61 66 65  VTrans;.  rcsafe
27eea 74 79 20 3d 20 73 71 6c 69 74 65 33 53 61 66 65  ty = sqlite3Safe
27eeb 74 79 4f 6e 28 64 62 29 3b 0a 0a 20 20 69 66 28  tyOn(db);..  if(
27eec 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
27eed 7b 0a 20 20 20 20 72 63 20 3d 20 72 63 73 61 66  {.    rc = rcsaf
27eee 65 74 79 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ety;.  }.  retur
27eef 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  n rc;.}../*.** I
27ef0 6e 76 6f 6b 65 20 74 68 65 20 78 52 6f 6c 6c 62  nvoke the xRollb
27ef1 61 63 6b 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c  ack method of al
27ef2 6c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73  l virtual tables
27ef3 20 69 6e 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69   in the .** sqli
27ef4 74 65 33 2e 61 56 54 72 61 6e 73 20 61 72 72 61  te3.aVTrans arra
27ef5 79 2e 20 54 68 65 6e 20 63 6c 65 61 72 20 74 68  y. Then clear th
27ef6 65 20 61 72 72 61 79 20 69 74 73 65 6c 66 2e 0a  e array itself..
27ef7 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
27ef8 45 20 69 6e 74 20 73 71 6c 69 74 65 33 56 74 61  E int sqlite3Vta
27ef9 62 52 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65  bRollback(sqlite
27efa 33 20 2a 64 62 29 7b 0a 20 20 63 61 6c 6c 46 69  3 *db){.  callFi
27efb 6e 61 6c 69 73 65 72 28 64 62 2c 20 6f 66 66 73  naliser(db, offs
27efc 65 74 6f 66 28 73 71 6c 69 74 65 33 5f 6d 6f 64  etof(sqlite3_mod
27efd 75 6c 65 2c 78 52 6f 6c 6c 62 61 63 6b 29 29 3b  ule,xRollback));
27efe 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
27eff 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e  _OK;.}../*.** In
27f00 76 6f 6b 65 20 74 68 65 20 78 43 6f 6d 6d 69 74  voke the xCommit
27f01 20 6d 65 74 68 6f 64 20 6f 66 20 61 6c 6c 20 76   method of all v
27f02 69 72 74 75 61 6c 20 74 61 62 6c 65 73 20 69 6e  irtual tables in
27f03 20 74 68 65 20 0a 2a 2a 20 73 71 6c 69 74 65 33   the .** sqlite3
27f04 2e 61 56 54 72 61 6e 73 20 61 72 72 61 79 2e 20  .aVTrans array. 
27f05 54 68 65 6e 20 63 6c 65 61 72 20 74 68 65 20 61  Then clear the a
27f06 72 72 61 79 20 69 74 73 65 6c 66 2e 0a 2a 2f 0a  rray itself..*/.
27f07 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
27f08 6e 74 20 73 71 6c 69 74 65 33 56 74 61 62 43 6f  nt sqlite3VtabCo
27f09 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  mmit(sqlite3 *db
27f0a 29 7b 0a 20 20 63 61 6c 6c 46 69 6e 61 6c 69 73  ){.  callFinalis
27f0b 65 72 28 64 62 2c 20 6f 66 66 73 65 74 6f 66 28  er(db, offsetof(
27f0c 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 2c 78  sqlite3_module,x
27f0d 43 6f 6d 6d 69 74 29 29 3b 0a 20 20 72 65 74 75  Commit));.  retu
27f0e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
27f0f 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68 65 20 76 69  ./*.** If the vi
27f10 72 74 75 61 6c 20 74 61 62 6c 65 20 70 56 74 61  rtual table pVta
27f11 62 20 73 75 70 70 6f 72 74 73 20 74 68 65 20 74  b supports the t
27f12 72 61 6e 73 61 63 74 69 6f 6e 20 69 6e 74 65 72  ransaction inter
27f13 66 61 63 65 0a 2a 2a 20 28 78 42 65 67 69 6e 2f  face.** (xBegin/
27f14 78 52 6f 6c 6c 62 61 63 6b 2f 78 43 6f 6d 6d 69  xRollback/xCommi
27f15 74 20 61 6e 64 20 6f 70 74 69 6f 6e 61 6c 6c 79  t and optionally
27f16 20 78 53 79 6e 63 29 20 61 6e 64 20 61 20 74 72   xSync) and a tr
27f17 61 6e 73 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20  ansaction is.** 
27f18 6e 6f 74 20 63 75 72 72 65 6e 74 6c 79 20 6f 70  not currently op
27f19 65 6e 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 78  en, invoke the x
27f1a 42 65 67 69 6e 20 6d 65 74 68 6f 64 20 6e 6f 77  Begin method now
27f1b 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20 78  ..**.** If the x
27f1c 42 65 67 69 6e 20 63 61 6c 6c 20 69 73 20 73 75  Begin call is su
27f1d 63 63 65 73 73 66 75 6c 2c 20 70 6c 61 63 65 20  ccessful, place 
27f1e 74 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62  the sqlite3_vtab
27f1f 20 70 6f 69 6e 74 65 72 0a 2a 2a 20 69 6e 20 74   pointer.** in t
27f20 68 65 20 73 71 6c 69 74 65 33 2e 61 56 54 72 61  he sqlite3.aVTra
27f21 6e 73 20 61 72 72 61 79 2e 0a 2a 2f 0a 53 51 4c  ns array..*/.SQL
27f22 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
27f23 73 71 6c 69 74 65 33 56 74 61 62 42 65 67 69 6e  sqlite3VtabBegin
27f24 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 73 71  (sqlite3 *db, sq
27f25 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
27f26 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  b){.  int rc = S
27f27 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 63 6f 6e 73  QLITE_OK;.  cons
27f28 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65  t sqlite3_module
27f29 20 2a 70 4d 6f 64 75 6c 65 3b 0a 0a 20 20 2f 2a   *pModule;..  /*
27f2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 3a 20 49   Special case: I
27f2b 66 20 64 62 2d 3e 61 56 54 72 61 6e 73 20 69 73  f db->aVTrans is
27f2c 20 4e 55 4c 4c 20 61 6e 64 20 64 62 2d 3e 6e 56   NULL and db->nV
27f2d 54 72 61 6e 73 20 69 73 20 67 72 65 61 74 65 72  Trans is greater
27f2e 0a 20 20 2a 2a 20 74 68 61 6e 20 7a 65 72 6f 2c  .  ** than zero,
27f2f 20 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74   then this funct
27f30 69 6f 6e 20 69 73 20 62 65 69 6e 67 20 63 61 6c  ion is being cal
27f31 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69 6e 20  led from within 
27f32 61 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20 6d  a.  ** virtual m
27f33 6f 64 75 6c 65 20 78 53 79 6e 63 28 29 20 63 61  odule xSync() ca
27f34 6c 6c 62 61 63 6b 2e 20 49 74 20 69 73 20 69 6c  llback. It is il
27f35 6c 65 67 61 6c 20 74 6f 20 77 72 69 74 65 20 74  legal to write t
27f36 6f 20 0a 20 20 2a 2a 20 76 69 72 74 75 61 6c 20  o .  ** virtual 
27f37 6d 6f 64 75 6c 65 20 74 61 62 6c 65 73 20 69 6e  module tables in
27f38 20 74 68 69 73 20 63 61 73 65 2c 20 73 6f 20 72   this case, so r
27f39 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43  eturn SQLITE_LOC
27f3a 4b 45 44 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  KED..  */.  if( 
27f3b 73 71 6c 69 74 65 33 56 74 61 62 49 6e 53 79 6e  sqlite3VtabInSyn
27f3c 63 28 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74  c(db) ){.    ret
27f3d 75 72 6e 20 53 51 4c 49 54 45 5f 4c 4f 43 4b 45  urn SQLITE_LOCKE
27f3e 44 3b 0a 20 20 7d 0a 20 20 69 66 28 20 21 70 56  D;.  }.  if( !pV
27f3f 74 61 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72  tab ){.    retur
27f40 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d  n SQLITE_OK;.  }
27f41 20 0a 20 20 70 4d 6f 64 75 6c 65 20 3d 20 70 56   .  pModule = pV
27f42 74 61 62 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 0a 20  tab->pModule;.. 
27f43 20 69 66 28 20 70 4d 6f 64 75 6c 65 2d 3e 78 42   if( pModule->xB
27f44 65 67 69 6e 20 29 7b 0a 20 20 20 20 69 6e 74 20  egin ){.    int 
27f45 69 3b 0a 0a 0a 20 20 20 20 2f 2a 20 49 66 20 70  i;...    /* If p
27f46 56 74 61 62 20 69 73 20 61 6c 72 65 61 64 79 20  Vtab is already 
27f47 69 6e 20 74 68 65 20 61 56 54 72 61 6e 73 20 61  in the aVTrans a
27f48 72 72 61 79 2c 20 72 65 74 75 72 6e 20 65 61 72  rray, return ear
27f49 6c 79 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  ly */.    for(i=
27f4a 30 3b 20 28 69 3c 64 62 2d 3e 6e 56 54 72 61 6e  0; (i<db->nVTran
27f4b 73 29 20 26 26 20 30 21 3d 64 62 2d 3e 61 56 54  s) && 0!=db->aVT
27f4c 72 61 6e 73 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20  rans[i]; i++){. 
27f4d 20 20 20 20 20 69 66 28 20 64 62 2d 3e 61 56 54       if( db->aVT
27f4e 72 61 6e 73 5b 69 5d 3d 3d 70 56 74 61 62 20 29  rans[i]==pVtab )
27f4f 7b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  {.        return
27f50 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
27f51 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f    }.    }..    /
27f52 2a 20 49 6e 76 6f 6b 65 20 74 68 65 20 78 42 65  * Invoke the xBe
27f53 67 69 6e 20 6d 65 74 68 6f 64 20 2a 2f 0a 20 20  gin method */.  
27f54 20 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d 3e    rc = pModule->
27f55 78 42 65 67 69 6e 28 70 56 74 61 62 29 3b 0a 20  xBegin(pVtab);. 
27f56 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
27f57 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63  E_OK ){.      rc
27f58 20 3d 20 61 64 64 54 6f 56 54 72 61 6e 73 28 64   = addToVTrans(d
27f59 62 2c 20 70 56 74 61 62 29 3b 0a 20 20 20 20 7d  b, pVtab);.    }
27f5a 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
27f5b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  ;.}../*.** The f
27f5c 69 72 73 74 20 70 61 72 61 6d 65 74 65 72 20 28  irst parameter (
27f5d 70 44 65 66 29 20 69 73 20 61 20 66 75 6e 63 74  pDef) is a funct
27f5e 69 6f 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  ion implementati
27f5f 6f 6e 2e 20 20 54 68 65 0a 2a 2a 20 73 65 63 6f  on.  The.** seco
27f60 6e 64 20 70 61 72 61 6d 65 74 65 72 20 28 70 45  nd parameter (pE
27f61 78 70 72 29 20 69 73 20 74 68 65 20 66 69 72 73  xpr) is the firs
27f62 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  t argument to th
27f63 69 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a 2a 20  is function..** 
27f64 49 66 20 70 45 78 70 72 20 69 73 20 61 20 63 6f  If pExpr is a co
27f65 6c 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61  lumn in a virtua
27f66 6c 20 74 61 62 6c 65 2c 20 74 68 65 6e 20 6c 65  l table, then le
27f67 74 20 74 68 65 20 76 69 72 74 75 61 6c 0a 2a 2a  t the virtual.**
27f68 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74   table implement
27f69 61 74 69 6f 6e 20 68 61 76 65 20 61 6e 20 6f 70  ation have an op
27f6a 70 6f 72 74 75 6e 69 74 79 20 74 6f 20 6f 76 65  portunity to ove
27f6b 72 6c 6f 61 64 20 74 68 65 20 66 75 6e 63 74 69  rload the functi
27f6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 72  on..**.** This r
27f6d 6f 75 74 69 6e 65 20 69 73 20 75 73 65 64 20 74  outine is used t
27f6e 6f 20 61 6c 6c 6f 77 20 76 69 72 74 75 61 6c 20  o allow virtual 
27f6f 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e 74 61  table implementa
27f70 74 69 6f 6e 73 20 74 6f 0a 2a 2a 20 6f 76 65 72  tions to.** over
27f71 6c 6f 61 64 20 4d 41 54 43 48 2c 20 4c 49 4b 45  load MATCH, LIKE
27f72 2c 20 47 4c 4f 42 2c 20 61 6e 64 20 52 45 47 45  , GLOB, and REGE
27f73 58 50 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a  XP operators..**
27f74 0a 2a 2a 20 52 65 74 75 72 6e 20 65 69 74 68 65  .** Return eithe
27f75 72 20 74 68 65 20 70 44 65 66 20 61 72 67 75 6d  r the pDef argum
27f76 65 6e 74 20 28 69 6e 64 69 63 61 74 69 6e 67 20  ent (indicating 
27f77 6e 6f 20 63 68 61 6e 67 65 29 20 6f 72 20 61 20  no change) or a 
27f78 0a 2a 2a 20 6e 65 77 20 46 75 6e 63 44 65 66 20  .** new FuncDef 
27f79 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20 69  structure that i
27f7a 73 20 6d 61 72 6b 65 64 20 61 73 20 65 70 68 65  s marked as ephe
27f7b 6d 65 72 61 6c 20 75 73 69 6e 67 20 74 68 65 0a  meral using the.
27f7c 2a 2a 20 53 51 4c 49 54 45 5f 46 55 4e 43 5f 45  ** SQLITE_FUNC_E
27f7d 50 48 45 4d 20 66 6c 61 67 2e 0a 2a 2f 0a 53 51  PHEM flag..*/.SQ
27f7e 4c 49 54 45 5f 50 52 49 56 41 54 45 20 46 75 6e  LITE_PRIVATE Fun
27f7f 63 44 65 66 20 2a 73 71 6c 69 74 65 33 56 74 61  cDef *sqlite3Vta
27f80 62 4f 76 65 72 6c 6f 61 64 46 75 6e 63 74 69 6f  bOverloadFunctio
27f81 6e 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  n(.  sqlite3 *db
27f82 2c 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65  ,    /* Database
27f83 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 66 6f 72 20   connection for 
27f84 72 65 70 6f 72 74 69 6e 67 20 6d 61 6c 6c 6f 63  reporting malloc
27f85 20 70 72 6f 62 6c 65 6d 73 20 2a 2f 0a 20 20 46   problems */.  F
27f86 75 6e 63 44 65 66 20 2a 70 44 65 66 2c 20 20 2f  uncDef *pDef,  /
27f87 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 70 6f  * Function to po
27f88 73 73 69 62 6c 79 20 6f 76 65 72 6c 6f 61 64 20  ssibly overload 
27f89 2a 2f 0a 20 20 69 6e 74 20 6e 41 72 67 2c 20 20  */.  int nArg,  
27f8a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
27f8b 66 20 61 72 67 75 6d 65 6e 74 73 20 74 6f 20 74  f arguments to t
27f8c 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20  he function */. 
27f8d 20 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20   Expr *pExpr    
27f8e 20 2f 2a 20 46 69 72 73 74 20 61 72 67 75 6d 65   /* First argume
27f8f 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
27f90 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65  on */.){.  Table
27f91 20 2a 70 54 61 62 3b 0a 20 20 73 71 6c 69 74 65   *pTab;.  sqlite
27f92 33 5f 76 74 61 62 20 2a 70 56 74 61 62 3b 0a 20  3_vtab *pVtab;. 
27f93 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20   sqlite3_module 
27f94 2a 70 4d 6f 64 3b 0a 20 20 76 6f 69 64 20 28 2a  *pMod;.  void (*
27f95 78 46 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63  xFunc)(sqlite3_c
27f96 6f 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69  ontext*,int,sqli
27f97 74 65 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20  te3_value**);.  
27f98 76 6f 69 64 20 2a 70 41 72 67 3b 0a 20 20 46 75  void *pArg;.  Fu
27f99 6e 63 44 65 66 20 2a 70 4e 65 77 3b 0a 20 20 69  ncDef *pNew;.  i
27f9a 6e 74 20 72 63 20 3d 20 30 3b 0a 20 20 63 68 61  nt rc = 0;.  cha
27f9b 72 20 2a 7a 4c 6f 77 65 72 4e 61 6d 65 3b 0a 20  r *zLowerName;. 
27f9c 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
27f9d 7a 3b 0a 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20  z;...  /* Check 
27f9e 74 6f 20 73 65 65 20 74 68 65 20 6c 65 66 74 20  to see the left 
27f9f 6f 70 65 72 61 6e 64 20 69 73 20 61 20 63 6f 6c  operand is a col
27fa0 75 6d 6e 20 69 6e 20 61 20 76 69 72 74 75 61 6c  umn in a virtual
27fa1 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28 20   table */.  if( 
27fa2 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75 72  pExpr==0 ) retur
27fa3 6e 20 70 44 65 66 3b 0a 20 20 69 66 28 20 70 45  n pDef;.  if( pE
27fa4 78 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55  xpr->op!=TK_COLU
27fa5 4d 4e 20 29 20 72 65 74 75 72 6e 20 70 44 65 66  MN ) return pDef
27fa6 3b 0a 20 20 70 54 61 62 20 3d 20 70 45 78 70 72  ;.  pTab = pExpr
27fa7 2d 3e 70 54 61 62 3b 0a 20 20 69 66 28 20 70 54  ->pTab;.  if( pT
27fa8 61 62 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 70  ab==0 ) return p
27fa9 44 65 66 3b 0a 20 20 69 66 28 20 28 70 54 61 62  Def;.  if( (pTab
27faa 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f  ->tabFlags & TF_
27fab 56 69 72 74 75 61 6c 29 3d 3d 30 20 29 20 72 65  Virtual)==0 ) re
27fac 74 75 72 6e 20 70 44 65 66 3b 0a 20 20 70 56 74  turn pDef;.  pVt
27fad 61 62 20 3d 20 70 54 61 62 2d 3e 70 56 74 61 62  ab = pTab->pVtab
27fae 3b 0a 20 20 61 73 73 65 72 74 28 20 70 56 74 61  ;.  assert( pVta
27faf 62 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  b!=0 );.  assert
27fb0 28 20 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  ( pVtab->pModule
27fb1 21 3d 30 20 29 3b 0a 20 20 70 4d 6f 64 20 3d 20  !=0 );.  pMod = 
27fb2 28 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c 65 20  (sqlite3_module 
27fb3 2a 29 70 56 74 61 62 2d 3e 70 4d 6f 64 75 6c 65  *)pVtab->pModule
27fb4 3b 0a 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 46  ;.  if( pMod->xF
27fb5 69 6e 64 46 75 6e 63 74 69 6f 6e 3d 3d 30 20 29  indFunction==0 )
27fb6 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a 20 0a   return pDef;. .
27fb7 20 20 2f 2a 20 43 61 6c 6c 20 74 68 65 20 78 46    /* Call the xF
27fb8 69 6e 64 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68  indFunction meth
27fb9 6f 64 20 6f 6e 20 74 68 65 20 76 69 72 74 75 61  od on the virtua
27fba 6c 20 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65 6e  l table implemen
27fbb 74 61 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 73  tation.  ** to s
27fbc 65 65 20 69 66 20 74 68 65 20 69 6d 70 6c 65 6d  ee if the implem
27fbd 65 6e 74 61 74 69 6f 6e 20 77 61 6e 74 73 20 74  entation wants t
27fbe 6f 20 6f 76 65 72 6c 6f 61 64 20 74 68 69 73 20  o overload this 
27fbf 66 75 6e 63 74 69 6f 6e 20 0a 20 20 2a 2f 0a 20  function .  */. 
27fc0 20 7a 4c 6f 77 65 72 4e 61 6d 65 20 3d 20 73 71   zLowerName = sq
27fc1 6c 69 74 65 33 44 62 53 74 72 44 75 70 28 64 62  lite3DbStrDup(db
27fc2 2c 20 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 3b 0a  , pDef->zName);.
27fc3 20 20 69 66 28 20 7a 4c 6f 77 65 72 4e 61 6d 65    if( zLowerName
27fc4 20 29 7b 0a 20 20 20 20 66 6f 72 28 7a 3d 28 75   ){.    for(z=(u
27fc5 6e 73 69 67 6e 65 64 20 63 68 61 72 2a 29 7a 4c  nsigned char*)zL
27fc6 6f 77 65 72 4e 61 6d 65 3b 20 2a 7a 3b 20 7a 2b  owerName; *z; z+
27fc7 2b 29 7b 0a 20 20 20 20 20 20 2a 7a 20 3d 20 73  +){.      *z = s
27fc8 71 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77  qlite3UpperToLow
27fc9 65 72 5b 2a 7a 5d 3b 0a 20 20 20 20 7d 0a 20 20  er[*z];.    }.  
27fca 20 20 72 63 20 3d 20 70 4d 6f 64 2d 3e 78 46 69    rc = pMod->xFi
27fcb 6e 64 46 75 6e 63 74 69 6f 6e 28 70 56 74 61 62  ndFunction(pVtab
27fcc 2c 20 6e 41 72 67 2c 20 7a 4c 6f 77 65 72 4e 61  , nArg, zLowerNa
27fcd 6d 65 2c 20 26 78 46 75 6e 63 2c 20 26 70 41 72  me, &xFunc, &pAr
27fce 67 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 44  g);.    sqlite3D
27fcf 62 46 72 65 65 28 64 62 2c 20 7a 4c 6f 77 65 72  bFree(db, zLower
27fd0 4e 61 6d 65 29 3b 0a 20 20 20 20 69 66 28 20 70  Name);.    if( p
27fd1 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67 20 29 7b  Vtab->zErrMsg ){
27fd2 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
27fd3 72 6f 72 28 64 62 2c 20 72 63 2c 20 22 25 73 22  ror(db, rc, "%s"
27fd4 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67  , pVtab->zErrMsg
27fd5 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
27fd6 44 62 46 72 65 65 28 64 62 2c 20 70 56 74 61 62  DbFree(db, pVtab
27fd7 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20 20 20 20  ->zErrMsg);.    
27fd8 20 20 70 56 74 61 62 2d 3e 7a 45 72 72 4d 73 67    pVtab->zErrMsg
27fd9 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   = 0;.    }.  }.
27fda 20 20 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20    if( rc==0 ){. 
27fdb 20 20 20 72 65 74 75 72 6e 20 70 44 65 66 3b 0a     return pDef;.
27fdc 20 20 7d 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65    }..  /* Create
27fdd 20 61 20 6e 65 77 20 65 70 68 65 6d 65 72 61 6c   a new ephemeral
27fde 20 66 75 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69   function defini
27fdf 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 6f 76 65  tion for the ove
27fe0 72 6c 6f 61 64 65 64 0a 20 20 2a 2a 20 66 75 6e  rloaded.  ** fun
27fe1 63 74 69 6f 6e 20 2a 2f 0a 20 20 70 4e 65 77 20  ction */.  pNew 
27fe2 3d 20 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f  = sqlite3DbMallo
27fe3 63 5a 65 72 6f 28 64 62 2c 20 73 69 7a 65 6f 66  cZero(db, sizeof
27fe4 28 2a 70 4e 65 77 29 20 2b 20 73 74 72 6c 65 6e  (*pNew) + strlen
27fe5 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29 20 29 3b  (pDef->zName) );
27fe6 0a 20 20 69 66 28 20 70 4e 65 77 3d 3d 30 20 29  .  if( pNew==0 )
27fe7 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 70 44 65  {.    return pDe
27fe8 66 3b 0a 20 20 7d 0a 20 20 2a 70 4e 65 77 20 3d  f;.  }.  *pNew =
27fe9 20 2a 70 44 65 66 3b 0a 20 20 70 4e 65 77 2d 3e   *pDef;.  pNew->
27fea 7a 4e 61 6d 65 20 3d 20 28 63 68 61 72 20 2a 29  zName = (char *)
27feb 26 70 4e 65 77 5b 31 5d 3b 0a 20 20 6d 65 6d 63  &pNew[1];.  memc
27fec 70 79 28 70 4e 65 77 2d 3e 7a 4e 61 6d 65 2c 20  py(pNew->zName, 
27fed 70 44 65 66 2d 3e 7a 4e 61 6d 65 2c 20 73 74 72  pDef->zName, str
27fee 6c 65 6e 28 70 44 65 66 2d 3e 7a 4e 61 6d 65 29  len(pDef->zName)
27fef 2b 31 29 3b 0a 20 20 70 4e 65 77 2d 3e 78 46 75  +1);.  pNew->xFu
27ff0 6e 63 20 3d 20 78 46 75 6e 63 3b 0a 20 20 70 4e  nc = xFunc;.  pN
27ff1 65 77 2d 3e 70 55 73 65 72 44 61 74 61 20 3d 20  ew->pUserData = 
27ff2 70 41 72 67 3b 0a 20 20 70 4e 65 77 2d 3e 66 6c  pArg;.  pNew->fl
27ff3 61 67 73 20 7c 3d 20 53 51 4c 49 54 45 5f 46 55  ags |= SQLITE_FU
27ff4 4e 43 5f 45 50 48 45 4d 3b 0a 20 20 72 65 74 75  NC_EPHEM;.  retu
27ff5 72 6e 20 70 4e 65 77 3b 0a 7d 0a 0a 2f 2a 0a 2a  rn pNew;.}../*.*
27ff6 2a 20 4d 61 6b 65 20 73 75 72 65 20 76 69 72 74  * Make sure virt
27ff7 75 61 6c 20 74 61 62 6c 65 20 70 54 61 62 20 69  ual table pTab i
27ff8 73 20 63 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74  s contained in t
27ff9 68 65 20 70 50 61 72 73 65 2d 3e 61 70 56 69 72  he pParse->apVir
27ffa 74 75 61 6c 4c 6f 63 6b 5b 5d 0a 2a 2a 20 61 72  tualLock[].** ar
27ffb 72 61 79 20 73 6f 20 74 68 61 74 20 61 6e 20 4f  ray so that an O
27ffc 50 5f 56 42 65 67 69 6e 20 77 69 6c 6c 20 67 65  P_VBegin will ge
27ffd 74 20 67 65 6e 65 72 61 74 65 64 20 66 6f 72 20  t generated for 
27ffe 69 74 2e 20 20 41 64 64 20 70 54 61 62 20 74 6f  it.  Add pTab to
27fff 20 74 68 65 0a 2a 2a 20 61 72 72 61 79 20 69 66   the.** array if
28000 20 69 74 20 69 73 20 6d 69 73 73 69 6e 67 2e 20   it is missing. 
28001 20 49 66 20 70 54 61 62 20 69 73 20 61 6c 72 65   If pTab is alre
28002 61 64 79 20 69 6e 20 74 68 65 20 61 72 72 61 79  ady in the array
28003 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a  , this routine.*
28004 2a 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a 2f  * is a no-op..*/
28005 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
28006 76 6f 69 64 20 73 71 6c 69 74 65 33 56 74 61 62  void sqlite3Vtab
28007 4d 61 6b 65 57 72 69 74 61 62 6c 65 28 50 61 72  MakeWritable(Par
28008 73 65 20 2a 70 50 61 72 73 65 2c 20 54 61 62 6c  se *pParse, Tabl
28009 65 20 2a 70 54 61 62 29 7b 0a 20 20 69 6e 74 20  e *pTab){.  int 
2800a 69 2c 20 6e 3b 0a 20 20 61 73 73 65 72 74 28 20  i, n;.  assert( 
2800b 49 73 56 69 72 74 75 61 6c 28 70 54 61 62 29 20  IsVirtual(pTab) 
2800c 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
2800d 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c 6f 63  pParse->nVtabLoc
2800e 6b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28  k; i++){.    if(
2800f 20 70 54 61 62 3d 3d 70 50 61 72 73 65 2d 3e 61   pTab==pParse->a
28010 70 56 74 61 62 4c 6f 63 6b 5b 69 5d 20 29 20 72  pVtabLock[i] ) r
28011 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 6e 20 3d  eturn;.  }.  n =
28012 20 28 70 50 61 72 73 65 2d 3e 6e 56 74 61 62 4c   (pParse->nVtabL
28013 6f 63 6b 2b 31 29 2a 73 69 7a 65 6f 66 28 70 50  ock+1)*sizeof(pP
28014 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  arse->apVtabLock
28015 5b 30 5d 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  [0]);.  pParse->
28016 61 70 56 74 61 62 4c 6f 63 6b 20 3d 20 73 71 6c  apVtabLock = sql
28017 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 50 61  ite3_realloc(pPa
28018 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 2c  rse->apVtabLock,
28019 20 6e 29 3b 0a 20 20 69 66 28 20 70 50 61 72 73   n);.  if( pPars
2801a 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b 20 29 7b  e->apVtabLock ){
2801b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 61 70 56  .    pParse->apV
2801c 74 61 62 4c 6f 63 6b 5b 70 50 61 72 73 65 2d 3e  tabLock[pParse->
2801d 6e 56 74 61 62 4c 6f 63 6b 2b 2b 5d 20 3d 20 70  nVtabLock++] = p
2801e 54 61 62 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  Tab;.  }else{.  
2801f 20 20 70 50 61 72 73 65 2d 3e 64 62 2d 3e 6d 61    pParse->db->ma
28020 6c 6c 6f 63 46 61 69 6c 65 64 20 3d 20 31 3b 0a  llocFailed = 1;.
28021 20 20 7d 0a 7d 0a 0a 23 65 6e 64 69 66 20 2f 2a    }.}..#endif /*
28022 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
28023 54 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a  TUALTABLE */../*
28024 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
28025 64 20 6f 66 20 76 74 61 62 2e 63 20 2a 2a 2a 2a  d of vtab.c ****
28026 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28027 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28028 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
28029 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
2802a 67 69 6e 20 66 69 6c 65 20 77 68 65 72 65 2e 63  gin file where.c
2802b 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2802c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2802d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2802e 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
2802f 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
28030 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
28031 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
28032 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
28033 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
28034 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
28035 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
28036 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
28037 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
28038 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
28039 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2803a 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2803b 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2803c 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2803d 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2803e 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2803f 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
28040 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
28041 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28042 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28043 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28044 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
28045 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 6d 6f 64  ****.** This mod
28046 75 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20 63  ule contains C c
28047 6f 64 65 20 74 68 61 74 20 67 65 6e 65 72 61 74  ode that generat
28048 65 73 20 56 44 42 45 20 63 6f 64 65 20 75 73 65  es VDBE code use
28049 64 20 74 6f 20 70 72 6f 63 65 73 73 0a 2a 2a 20  d to process.** 
2804a 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
2804b 20 6f 66 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   of SQL statemen
2804c 74 73 2e 20 20 54 68 69 73 20 6d 6f 64 75 6c 65  ts.  This module
2804d 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 20   is responsible 
2804e 66 6f 72 0a 2a 2a 20 67 65 6e 65 72 61 74 69 6e  for.** generatin
2804f 67 20 74 68 65 20 63 6f 64 65 20 74 68 61 74 20  g the code that 
28050 6c 6f 6f 70 73 20 74 68 72 6f 75 67 68 20 61 20  loops through a 
28051 74 61 62 6c 65 20 6c 6f 6f 6b 69 6e 67 20 66 6f  table looking fo
28052 72 20 61 70 70 6c 69 63 61 62 6c 65 0a 2a 2a 20  r applicable.** 
28053 72 6f 77 73 2e 20 20 49 6e 64 69 63 65 73 20 61  rows.  Indices a
28054 72 65 20 73 65 6c 65 63 74 65 64 20 61 6e 64 20  re selected and 
28055 75 73 65 64 20 74 6f 20 73 70 65 65 64 20 74 68  used to speed th
28056 65 20 73 65 61 72 63 68 20 77 68 65 6e 20 64 6f  e search when do
28057 69 6e 67 0a 2a 2a 20 73 6f 20 69 73 20 61 70 70  ing.** so is app
28058 6c 69 63 61 62 6c 65 2e 20 20 42 65 63 61 75 73  licable.  Becaus
28059 65 20 74 68 69 73 20 6d 6f 64 75 6c 65 20 69 73  e this module is
2805a 20 72 65 73 70 6f 6e 73 69 62 6c 65 20 66 6f 72   responsible for
2805b 20 73 65 6c 65 63 74 69 6e 67 0a 2a 2a 20 69 6e   selecting.** in
2805c 64 69 63 65 73 2c 20 79 6f 75 20 6d 69 67 68 74  dices, you might
2805d 20 61 6c 73 6f 20 74 68 69 6e 6b 20 6f 66 20 74   also think of t
2805e 68 69 73 20 6d 6f 64 75 6c 65 20 61 73 20 74 68  his module as th
2805f 65 20 22 71 75 65 72 79 20 6f 70 74 69 6d 69 7a  e "query optimiz
28060 65 72 22 2e 0a 2a 2a 0a 2a 2a 20 24 49 64 3a 20  er"..**.** $Id: 
28061 77 68 65 72 65 2e 63 2c 76 20 31 2e 33 33 30 20  where.c,v 1.330 
28062 32 30 30 38 2f 31 31 2f 31 37 20 31 39 3a 31 38  2008/11/17 19:18
28063 3a 35 35 20 64 61 6e 69 65 6c 6b 31 39 37 37 20  :55 danielk1977 
28064 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
28065 54 72 61 63 65 20 6f 75 74 70 75 74 20 6d 61 63  Trace output mac
28066 72 6f 73 0a 2a 2f 0a 23 69 66 20 64 65 66 69 6e  ros.*/.#if defin
28067 65 64 28 53 51 4c 49 54 45 5f 54 45 53 54 29 20  ed(SQLITE_TEST) 
28068 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54  || defined(SQLIT
28069 45 5f 44 45 42 55 47 29 0a 53 51 4c 49 54 45 5f  E_DEBUG).SQLITE_
2806a 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2806b 74 65 33 57 68 65 72 65 54 72 61 63 65 20 3d 20  te3WhereTrace = 
2806c 30 3b 0a 23 65 6e 64 69 66 0a 23 69 66 20 30 0a  0;.#endif.#if 0.
2806d 23 20 64 65 66 69 6e 65 20 57 48 45 52 45 54 52  # define WHERETR
2806e 41 43 45 28 58 29 20 20 69 66 28 73 71 6c 69 74  ACE(X)  if(sqlit
2806f 65 33 57 68 65 72 65 54 72 61 63 65 29 20 73 71  e3WhereTrace) sq
28070 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66  lite3DebugPrintf
28071 20 58 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e   X.#else.# defin
28072 65 20 57 48 45 52 45 54 52 41 43 45 28 58 29 0a  e WHERETRACE(X).
28073 23 65 6e 64 69 66 0a 0a 2f 2a 20 46 6f 72 77 61  #endif../* Forwa
28074 72 64 20 72 65 66 65 72 65 6e 63 65 0a 2a 2f 0a  rd reference.*/.
28075 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 57  typedef struct W
28076 68 65 72 65 43 6c 61 75 73 65 20 57 68 65 72 65  hereClause Where
28077 43 6c 61 75 73 65 3b 0a 74 79 70 65 64 65 66 20  Clause;.typedef 
28078 73 74 72 75 63 74 20 45 78 70 72 4d 61 73 6b 53  struct ExprMaskS
28079 65 74 20 45 78 70 72 4d 61 73 6b 53 65 74 3b 0a  et ExprMaskSet;.
2807a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 71 75 65 72 79  ./*.** The query
2807b 20 67 65 6e 65 72 61 74 6f 72 20 75 73 65 73 20   generator uses 
2807c 61 6e 20 61 72 72 61 79 20 6f 66 20 69 6e 73 74  an array of inst
2807d 61 6e 63 65 73 20 6f 66 20 74 68 69 73 20 73 74  ances of this st
2807e 72 75 63 74 75 72 65 20 74 6f 0a 2a 2a 20 68 65  ructure to.** he
2807f 6c 70 20 69 74 20 61 6e 61 6c 79 7a 65 20 74 68  lp it analyze th
28080 65 20 73 75 62 65 78 70 72 65 73 73 69 6f 6e 73  e subexpressions
28081 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
28082 61 75 73 65 2e 20 20 45 61 63 68 20 57 48 45 52  ause.  Each WHER
28083 45 0a 2a 2a 20 63 6c 61 75 73 65 20 73 75 62 65  E.** clause sube
28084 78 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70  xpression is sep
28085 61 72 61 74 65 64 20 66 72 6f 6d 20 74 68 65 20  arated from the 
28086 6f 74 68 65 72 73 20 62 79 20 61 6e 20 41 4e 44  others by an AND
28087 20 6f 70 65 72 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a   operator..**.**
28088 20 41 6c 6c 20 57 68 65 72 65 54 65 72 6d 73 20   All WhereTerms 
28089 61 72 65 20 63 6f 6c 6c 65 63 74 65 64 20 69 6e  are collected in
2808a 74 6f 20 61 20 73 69 6e 67 6c 65 20 57 68 65 72  to a single Wher
2808b 65 43 6c 61 75 73 65 20 73 74 72 75 63 74 75 72  eClause structur
2808c 65 2e 20 20 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c  e.  .** The foll
2808d 6f 77 69 6e 67 20 69 64 65 6e 74 69 74 79 20 68  owing identity h
2808e 6f 6c 64 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  olds:.**.**     
2808f 20 20 20 57 68 65 72 65 54 65 72 6d 2e 70 57 43     WhereTerm.pWC
28090 2d 3e 61 5b 57 68 65 72 65 54 65 72 6d 2e 69 64  ->a[WhereTerm.id
28091 78 5d 20 3d 3d 20 57 68 65 72 65 54 65 72 6d 0a  x] == WhereTerm.
28092 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 20 74 65 72  **.** When a ter
28093 6d 20 69 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  m is of the form
28094 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
28095 20 20 20 20 20 58 20 3c 6f 70 3e 20 3c 65 78 70       X <op> <exp
28096 72 3e 0a 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 58  r>.**.** where X
28097 20 69 73 20 61 20 63 6f 6c 75 6d 6e 20 6e 61 6d   is a column nam
28098 65 20 61 6e 64 20 3c 6f 70 3e 20 69 73 20 6f 6e  e and <op> is on
28099 65 20 6f 66 20 63 65 72 74 61 69 6e 20 6f 70 65  e of certain ope
2809a 72 61 74 6f 72 73 2c 0a 2a 2a 20 74 68 65 6e 20  rators,.** then 
2809b 57 68 65 72 65 54 65 72 6d 2e 6c 65 66 74 43 75  WhereTerm.leftCu
2809c 72 73 6f 72 20 61 6e 64 20 57 68 65 72 65 54 65  rsor and WhereTe
2809d 72 6d 2e 6c 65 66 74 43 6f 6c 75 6d 6e 20 72 65  rm.leftColumn re
2809e 63 6f 72 64 20 74 68 65 0a 2a 2a 20 63 75 72 73  cord the.** curs
2809f 6f 72 20 6e 75 6d 62 65 72 20 61 6e 64 20 63 6f  or number and co
280a0 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f 72 20  lumn number for 
280a1 58 2e 20 20 57 68 65 72 65 54 65 72 6d 2e 6f 70  X.  WhereTerm.op
280a2 65 72 61 74 6f 72 20 72 65 63 6f 72 64 73 0a 2a  erator records.*
280a3 2a 20 74 68 65 20 3c 6f 70 3e 20 75 73 69 6e 67  * the <op> using
280a4 20 61 20 62 69 74 6d 61 73 6b 20 65 6e 63 6f 64   a bitmask encod
280a5 69 6e 67 20 64 65 66 69 6e 65 64 20 62 79 20 57  ing defined by W
280a6 4f 5f 78 78 78 20 62 65 6c 6f 77 2e 20 20 54 68  O_xxx below.  Th
280a7 65 0a 2a 2a 20 75 73 65 20 6f 66 20 61 20 62 69  e.** use of a bi
280a8 74 6d 61 73 6b 20 65 6e 63 6f 64 69 6e 67 20 66  tmask encoding f
280a9 6f 72 20 74 68 65 20 6f 70 65 72 61 74 6f 72 20  or the operator 
280aa 61 6c 6c 6f 77 73 20 75 73 20 74 6f 20 73 65 61  allows us to sea
280ab 72 63 68 0a 2a 2a 20 71 75 69 63 6b 6c 79 20 66  rch.** quickly f
280ac 6f 72 20 74 65 72 6d 73 20 74 68 61 74 20 6d 61  or terms that ma
280ad 74 63 68 20 61 6e 79 20 6f 66 20 73 65 76 65 72  tch any of sever
280ae 61 6c 20 64 69 66 66 65 72 65 6e 74 20 6f 70 65  al different ope
280af 72 61 74 6f 72 73 2e 0a 2a 2a 0a 2a 2a 20 70 72  rators..**.** pr
280b0 65 72 65 71 52 69 67 68 74 20 61 6e 64 20 70 72  ereqRight and pr
280b1 65 72 65 71 41 6c 6c 20 72 65 63 6f 72 64 20 73  ereqAll record s
280b2 65 74 73 20 6f 66 20 63 75 72 73 6f 72 20 6e 75  ets of cursor nu
280b3 6d 62 65 72 73 2c 0a 2a 2a 20 62 75 74 20 74 68  mbers,.** but th
280b4 65 79 20 64 6f 20 73 6f 20 69 6e 64 69 72 65 63  ey do so indirec
280b5 74 6c 79 2e 20 20 41 20 73 69 6e 67 6c 65 20 45  tly.  A single E
280b6 78 70 72 4d 61 73 6b 53 65 74 20 73 74 72 75 63  xprMaskSet struc
280b7 74 75 72 65 20 74 72 61 6e 73 6c 61 74 65 73 0a  ture translates.
280b8 2a 2a 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  ** cursor number
280b9 20 69 6e 74 6f 20 62 69 74 73 20 61 6e 64 20 74   into bits and t
280ba 68 65 20 74 72 61 6e 73 6c 61 74 65 64 20 62 69  he translated bi
280bb 74 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20 74  t is stored in t
280bc 68 65 20 70 72 65 72 65 71 0a 2a 2a 20 66 69 65  he prereq.** fie
280bd 6c 64 73 2e 20 20 54 68 65 20 74 72 61 6e 73 6c  lds.  The transl
280be 61 74 69 6f 6e 20 69 73 20 75 73 65 64 20 69 6e  ation is used in
280bf 20 6f 72 64 65 72 20 74 6f 20 6d 61 78 69 6d 69   order to maximi
280c0 7a 65 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66  ze the number of
280c1 0a 2a 2a 20 62 69 74 73 20 74 68 61 74 20 77 69  .** bits that wi
280c2 6c 6c 20 66 69 74 20 69 6e 20 61 20 42 69 74 6d  ll fit in a Bitm
280c3 61 73 6b 2e 20 20 54 68 65 20 56 44 42 45 20 63  ask.  The VDBE c
280c4 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69  ursor numbers mi
280c5 67 68 74 20 62 65 0a 2a 2a 20 73 70 72 65 61 64  ght be.** spread
280c6 20 6f 75 74 20 6f 76 65 72 20 74 68 65 20 6e 6f   out over the no
280c7 6e 2d 6e 65 67 61 74 69 76 65 20 69 6e 74 65 67  n-negative integ
280c8 65 72 73 2e 20 20 46 6f 72 20 65 78 61 6d 70 6c  ers.  For exampl
280c9 65 2c 20 74 68 65 20 63 75 72 73 6f 72 0a 2a 2a  e, the cursor.**
280ca 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74 20 62   numbers might b
280cb 65 20 33 2c 20 38 2c 20 39 2c 20 31 30 2c 20 32  e 3, 8, 9, 10, 2
280cc 30 2c 20 32 33 2c 20 34 31 2c 20 61 6e 64 20 34  0, 23, 41, and 4
280cd 35 2e 20 20 54 68 65 20 45 78 70 72 4d 61 73 6b  5.  The ExprMask
280ce 53 65 74 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65  Set.** translate
280cf 73 20 74 68 65 73 65 20 73 70 61 72 73 65 20 63  s these sparse c
280d0 75 72 73 6f 72 20 6e 75 6d 62 65 72 73 20 69 6e  ursor numbers in
280d1 74 6f 20 63 6f 6e 73 65 63 75 74 69 76 65 20 69  to consecutive i
280d2 6e 74 65 67 65 72 73 0a 2a 2a 20 62 65 67 69 6e  ntegers.** begin
280d3 6e 69 6e 67 20 77 69 74 68 20 30 20 69 6e 20 6f  ning with 0 in o
280d4 72 64 65 72 20 74 6f 20 6d 61 6b 65 20 74 68 65  rder to make the
280d5 20 62 65 73 74 20 70 6f 73 73 69 62 6c 65 20 75   best possible u
280d6 73 65 20 6f 66 20 74 68 65 20 61 76 61 69 6c 61  se of the availa
280d7 62 6c 65 0a 2a 2a 20 62 69 74 73 20 69 6e 20 74  ble.** bits in t
280d8 68 65 20 42 69 74 6d 61 73 6b 2e 20 20 53 6f 2c  he Bitmask.  So,
280d9 20 69 6e 20 74 68 65 20 65 78 61 6d 70 6c 65 20   in the example 
280da 61 62 6f 76 65 2c 20 74 68 65 20 63 75 72 73 6f  above, the curso
280db 72 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 6f 75  r numbers.** wou
280dc 6c 64 20 62 65 20 6d 61 70 70 65 64 20 69 6e 74  ld be mapped int
280dd 6f 20 69 6e 74 65 67 65 72 73 20 30 20 74 68 72  o integers 0 thr
280de 6f 75 67 68 20 37 2e 0a 2a 2f 0a 74 79 70 65 64  ough 7..*/.typed
280df 65 66 20 73 74 72 75 63 74 20 57 68 65 72 65 54  ef struct WhereT
280e0 65 72 6d 20 57 68 65 72 65 54 65 72 6d 3b 0a 73  erm WhereTerm;.s
280e1 74 72 75 63 74 20 57 68 65 72 65 54 65 72 6d 20  truct WhereTerm 
280e2 7b 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b  {.  Expr *pExpr;
280e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50              /* P
280e4 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 73 75  ointer to the su
280e5 62 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 20  bexpression */. 
280e6 20 69 31 36 20 69 50 61 72 65 6e 74 3b 20 20 20   i16 iParent;   
280e7 20 20 20 20 20 20 20 20 20 2f 2a 20 44 69 73 61           /* Disa
280e8 62 6c 65 20 70 57 43 2d 3e 61 5b 69 50 61 72 65  ble pWC->a[iPare
280e9 6e 74 5d 20 77 68 65 6e 20 74 68 69 73 20 74 65  nt] when this te
280ea 72 6d 20 64 69 73 61 62 6c 65 64 20 2a 2f 0a 20  rm disabled */. 
280eb 20 69 31 36 20 6c 65 66 74 43 75 72 73 6f 72 3b   i16 leftCursor;
280ec 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73           /* Curs
280ed 6f 72 20 6e 75 6d 62 65 72 20 6f 66 20 58 20 69  or number of X i
280ee 6e 20 22 58 20 3c 6f 70 3e 20 3c 65 78 70 72 3e  n "X <op> <expr>
280ef 22 20 2a 2f 0a 20 20 69 31 36 20 6c 65 66 74 43  " */.  i16 leftC
280f0 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f  olumn;         /
280f1 2a 20 43 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  * Column number 
280f2 6f 66 20 58 20 69 6e 20 22 58 20 3c 6f 70 3e 20  of X in "X <op> 
280f3 3c 65 78 70 72 3e 22 20 2a 2f 0a 20 20 75 31 36  <expr>" */.  u16
280f4 20 65 4f 70 65 72 61 74 6f 72 3b 20 20 20 20 20   eOperator;     
280f5 20 20 20 20 20 2f 2a 20 41 20 57 4f 5f 78 78 20       /* A WO_xx 
280f6 76 61 6c 75 65 20 64 65 73 63 72 69 62 69 6e 67  value describing
280f7 20 3c 6f 70 3e 20 2a 2f 0a 20 20 75 38 20 66 6c   <op> */.  u8 fl
280f8 61 67 73 3b 20 20 20 20 20 20 20 20 20 20 20 20  ags;            
280f9 20 20 20 2f 2a 20 42 69 74 20 66 6c 61 67 73 2e     /* Bit flags.
280fa 20 20 53 65 65 20 62 65 6c 6f 77 20 2a 2f 0a 20    See below */. 
280fb 20 75 38 20 6e 43 68 69 6c 64 3b 20 20 20 20 20   u8 nChild;     
280fc 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62           /* Numb
280fd 65 72 20 6f 66 20 63 68 69 6c 64 72 65 6e 20 74  er of children t
280fe 68 61 74 20 6d 75 73 74 20 64 69 73 61 62 6c 65  hat must disable
280ff 20 75 73 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c   us */.  WhereCl
28100 61 75 73 65 20 2a 70 57 43 3b 20 20 20 20 20 20  ause *pWC;      
28101 20 2f 2a 20 54 68 65 20 63 6c 61 75 73 65 20 74   /* The clause t
28102 68 69 73 20 74 65 72 6d 20 69 73 20 70 61 72 74  his term is part
28103 20 6f 66 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b   of */.  Bitmask
28104 20 70 72 65 72 65 71 52 69 67 68 74 3b 20 20 20   prereqRight;   
28105 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20 74   /* Bitmask of t
28106 61 62 6c 65 73 20 75 73 65 64 20 62 79 20 70 52  ables used by pR
28107 69 67 68 74 20 2a 2f 0a 20 20 42 69 74 6d 61 73  ight */.  Bitmas
28108 6b 20 70 72 65 72 65 71 41 6c 6c 3b 20 20 20 20  k prereqAll;    
28109 20 20 2f 2a 20 42 69 74 6d 61 73 6b 20 6f 66 20    /* Bitmask of 
2810a 74 61 62 6c 65 73 20 72 65 66 65 72 65 6e 63 65  tables reference
2810b 64 20 62 79 20 70 20 2a 2f 0a 7d 3b 0a 0a 2f 2a  d by p */.};../*
2810c 0a 2a 2a 20 41 6c 6c 6f 77 65 64 20 76 61 6c 75  .** Allowed valu
2810d 65 73 20 6f 66 20 57 68 65 72 65 54 65 72 6d 2e  es of WhereTerm.
2810e 66 6c 61 67 73 0a 2a 2f 0a 23 64 65 66 69 6e 65  flags.*/.#define
2810f 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 20 20   TERM_DYNAMIC   
28110 20 30 78 30 31 20 20 20 2f 2a 20 4e 65 65 64 20   0x01   /* Need 
28111 74 6f 20 63 61 6c 6c 20 73 71 6c 69 74 65 33 45  to call sqlite3E
28112 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 45  xprDelete(db, pE
28113 78 70 72 29 20 2a 2f 0a 23 64 65 66 69 6e 65 20  xpr) */.#define 
28114 54 45 52 4d 5f 56 49 52 54 55 41 4c 20 20 20 20  TERM_VIRTUAL    
28115 30 78 30 32 20 20 20 2f 2a 20 41 64 64 65 64 20  0x02   /* Added 
28116 62 79 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 72  by the optimizer
28117 2e 20 20 44 6f 20 6e 6f 74 20 63 6f 64 65 20 2a  .  Do not code *
28118 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 43  /.#define TERM_C
28119 4f 44 45 44 20 20 20 20 20 20 30 78 30 34 20 20  ODED      0x04  
2811a 20 2f 2a 20 54 68 69 73 20 74 65 72 6d 20 69 73   /* This term is
2811b 20 61 6c 72 65 61 64 79 20 63 6f 64 65 64 20 2a   already coded *
2811c 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f 43  /.#define TERM_C
2811d 4f 50 49 45 44 20 20 20 20 20 30 78 30 38 20 20  OPIED     0x08  
2811e 20 2f 2a 20 48 61 73 20 61 20 63 68 69 6c 64 20   /* Has a child 
2811f 2a 2f 0a 23 64 65 66 69 6e 65 20 54 45 52 4d 5f  */.#define TERM_
28120 4f 52 5f 4f 4b 20 20 20 20 20 20 30 78 31 30 20  OR_OK      0x10 
28121 20 20 2f 2a 20 55 73 65 64 20 64 75 72 69 6e 67    /* Used during
28122 20 4f 52 2d 63 6c 61 75 73 65 20 70 72 6f 63 65   OR-clause proce
28123 73 73 69 6e 67 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ssing */../*.** 
28124 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20 74  An instance of t
28125 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73 74 72  he following str
28126 75 63 74 75 72 65 20 68 6f 6c 64 73 20 61 6c 6c  ucture holds all
28127 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f   information abo
28128 75 74 20 61 0a 2a 2a 20 57 48 45 52 45 20 63 6c  ut a.** WHERE cl
28129 61 75 73 65 2e 20 20 4d 6f 73 74 6c 79 20 74 68  ause.  Mostly th
2812a 69 73 20 69 73 20 61 20 63 6f 6e 74 61 69 6e 65  is is a containe
2812b 72 20 66 6f 72 20 6f 6e 65 20 6f 72 20 6d 6f 72  r for one or mor
2812c 65 20 57 68 65 72 65 54 65 72 6d 73 2e 0a 2a 2f  e WhereTerms..*/
2812d 0a 73 74 72 75 63 74 20 57 68 65 72 65 43 6c 61  .struct WhereCla
2812e 75 73 65 20 7b 0a 20 20 50 61 72 73 65 20 2a 70  use {.  Parse *p
2812f 50 61 72 73 65 3b 20 20 20 20 20 20 20 20 20 20  Parse;          
28130 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
28131 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70 72  ontext */.  Expr
28132 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53 65  MaskSet *pMaskSe
28133 74 3b 20 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20  t;   /* Mapping 
28134 6f 66 20 74 61 62 6c 65 20 69 6e 64 69 63 65 73  of table indices
28135 20 74 6f 20 62 69 74 6d 61 73 6b 73 20 2a 2f 0a   to bitmasks */.
28136 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20 20    int nTerm;    
28137 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
28138 6d 62 65 72 20 6f 66 20 74 65 72 6d 73 20 2a 2f  mber of terms */
28139 0a 20 20 69 6e 74 20 6e 53 6c 6f 74 3b 20 20 20  .  int nSlot;   
2813a 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2813b 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73  umber of entries
2813c 20 69 6e 20 61 5b 5d 20 2a 2f 0a 20 20 57 68 65   in a[] */.  Whe
2813d 72 65 54 65 72 6d 20 2a 61 3b 20 20 20 20 20 20  reTerm *a;      
2813e 20 20 20 20 20 20 2f 2a 20 45 61 63 68 20 61 5b        /* Each a[
2813f 5d 20 64 65 73 63 72 69 62 65 73 20 61 20 74 65  ] describes a te
28140 72 6d 20 6f 66 20 74 68 65 20 57 48 45 52 45 20  rm of the WHERE 
28141 63 6c 75 61 73 65 20 2a 2f 0a 20 20 57 68 65 72  cluase */.  Wher
28142 65 54 65 72 6d 20 61 53 74 61 74 69 63 5b 31 30  eTerm aStatic[10
28143 5d 3b 20 20 20 2f 2a 20 49 6e 69 74 69 61 6c 20  ];   /* Initial 
28144 73 74 61 74 69 63 20 73 70 61 63 65 20 66 6f 72  static space for
28145 20 61 5b 5d 20 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a   a[] */.};../*.*
28146 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
28147 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
28148 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74  tructure keeps t
28149 72 61 63 6b 20 6f 66 20 61 20 6d 61 70 70 69 6e  rack of a mappin
2814a 67 0a 2a 2a 20 62 65 74 77 65 65 6e 20 56 44 42  g.** between VDB
2814b 45 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  E cursor numbers
2814c 20 61 6e 64 20 62 69 74 73 20 6f 66 20 74 68 65   and bits of the
2814d 20 62 69 74 6d 61 73 6b 73 20 69 6e 20 57 68 65   bitmasks in Whe
2814e 72 65 54 65 72 6d 2e 0a 2a 2a 0a 2a 2a 20 54 68  reTerm..**.** Th
2814f 65 20 56 44 42 45 20 63 75 72 73 6f 72 20 6e 75  e VDBE cursor nu
28150 6d 62 65 72 73 20 61 72 65 20 73 6d 61 6c 6c 20  mbers are small 
28151 69 6e 74 65 67 65 72 73 20 63 6f 6e 74 61 69 6e  integers contain
28152 65 64 20 69 6e 20 0a 2a 2a 20 53 72 63 4c 69 73  ed in .** SrcLis
28153 74 5f 69 74 65 6d 2e 69 43 75 72 73 6f 72 20 61  t_item.iCursor a
28154 6e 64 20 45 78 70 72 2e 69 54 61 62 6c 65 20 66  nd Expr.iTable f
28155 69 65 6c 64 73 2e 20 20 46 6f 72 20 61 6e 79 20  ields.  For any 
28156 67 69 76 65 6e 20 57 48 45 52 45 20 0a 2a 2a 20  given WHERE .** 
28157 63 6c 61 75 73 65 2c 20 74 68 65 20 63 75 72 73  clause, the curs
28158 6f 72 20 6e 75 6d 62 65 72 73 20 6d 69 67 68 74  or numbers might
28159 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20   not begin with 
2815a 30 20 61 6e 64 20 74 68 65 79 20 6d 69 67 68 74  0 and they might
2815b 0a 2a 2a 20 63 6f 6e 74 61 69 6e 20 67 61 70 73  .** contain gaps
2815c 20 69 6e 20 74 68 65 20 6e 75 6d 62 65 72 69 6e   in the numberin
2815d 67 20 73 65 71 75 65 6e 63 65 2e 20 20 42 75 74  g sequence.  But
2815e 20 77 65 20 77 61 6e 74 20 74 6f 20 6d 61 6b 65   we want to make
2815f 20 6d 61 78 69 6d 75 6d 0a 2a 2a 20 75 73 65 20   maximum.** use 
28160 6f 66 20 74 68 65 20 62 69 74 73 20 69 6e 20 6f  of the bits in o
28161 75 72 20 62 69 74 6d 61 73 6b 73 2e 20 20 54 68  ur bitmasks.  Th
28162 69 73 20 73 74 72 75 63 74 75 72 65 20 70 72 6f  is structure pro
28163 76 69 64 65 73 20 61 20 6d 61 70 70 69 6e 67 0a  vides a mapping.
28164 2a 2a 20 66 72 6f 6d 20 74 68 65 20 73 70 61 72  ** from the spar
28165 73 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72  se cursor number
28166 73 20 69 6e 74 6f 20 63 6f 6e 73 65 63 75 74 69  s into consecuti
28167 76 65 20 69 6e 74 65 67 65 72 73 20 62 65 67 69  ve integers begi
28168 6e 6e 69 6e 67 0a 2a 2a 20 77 69 74 68 20 30 2e  nning.** with 0.
28169 0a 2a 2a 0a 2a 2a 20 49 66 20 45 78 70 72 4d 61  .**.** If ExprMa
2816a 73 6b 53 65 74 2e 69 78 5b 41 5d 3d 3d 42 20 69  skSet.ix[A]==B i
2816b 74 20 6d 65 61 6e 73 20 74 68 61 74 20 54 68 65  t means that The
2816c 20 41 2d 74 68 20 62 69 74 20 6f 66 20 61 20 42   A-th bit of a B
2816d 69 74 6d 61 73 6b 0a 2a 2a 20 63 6f 72 72 65 73  itmask.** corres
2816e 70 6f 6e 64 73 20 56 44 42 45 20 63 75 72 73 6f  ponds VDBE curso
2816f 72 20 6e 75 6d 62 65 72 20 42 2e 20 20 54 68 65  r number B.  The
28170 20 41 2d 74 68 20 62 69 74 20 6f 66 20 61 20 62   A-th bit of a b
28171 69 74 6d 61 73 6b 20 69 73 20 31 3c 3c 41 2e 0a  itmask is 1<<A..
28172 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c  **.** For exampl
28173 65 2c 20 69 66 20 74 68 65 20 57 48 45 52 45 20  e, if the WHERE 
28174 63 6c 61 75 73 65 20 65 78 70 72 65 73 73 69 6f  clause expressio
28175 6e 20 75 73 65 64 20 74 68 65 73 65 20 56 44 42  n used these VDB
28176 45 0a 2a 2a 20 63 75 72 73 6f 72 73 3a 20 20 34  E.** cursors:  4
28177 2c 20 35 2c 20 38 2c 20 32 39 2c 20 35 37 2c 20  , 5, 8, 29, 57, 
28178 37 33 2e 20 20 54 68 65 6e 20 74 68 65 20 20 45  73.  Then the  E
28179 78 70 72 4d 61 73 6b 53 65 74 20 73 74 72 75 63  xprMaskSet struc
2817a 74 75 72 65 0a 2a 2a 20 77 6f 75 6c 64 20 6d 61  ture.** would ma
2817b 70 20 74 68 6f 73 65 20 63 75 72 73 6f 72 20 6e  p those cursor n
2817c 75 6d 62 65 72 73 20 69 6e 74 6f 20 62 69 74 73  umbers into bits
2817d 20 30 20 74 68 72 6f 75 67 68 20 35 2e 0a 2a 2a   0 through 5..**
2817e 0a 2a 2a 20 4e 6f 74 65 20 74 68 61 74 20 74 68  .** Note that th
2817f 65 20 6d 61 70 70 69 6e 67 20 69 73 20 6e 6f 74  e mapping is not
28180 20 6e 65 63 65 73 73 61 72 69 6c 79 20 6f 72 64   necessarily ord
28181 65 72 65 64 2e 20 20 49 6e 20 74 68 65 20 65 78  ered.  In the ex
28182 61 6d 70 6c 65 0a 2a 2a 20 61 62 6f 76 65 2c 20  ample.** above, 
28183 74 68 65 20 6d 61 70 70 69 6e 67 20 6d 69 67 68  the mapping migh
28184 74 20 67 6f 20 6c 69 6b 65 20 74 68 69 73 3a 20  t go like this: 
28185 20 34 2d 3e 33 2c 20 35 2d 3e 31 2c 20 38 2d 3e   4->3, 5->1, 8->
28186 32 2c 20 32 39 2d 3e 30 2c 0a 2a 2a 20 35 37 2d  2, 29->0,.** 57-
28187 3e 35 2c 20 37 33 2d 3e 34 2e 20 20 4f 72 20 6f  >5, 73->4.  Or o
28188 6e 65 20 6f 66 20 37 31 39 20 6f 74 68 65 72 20  ne of 719 other 
28189 63 6f 6d 62 69 6e 61 74 69 6f 6e 73 20 6d 69 67  combinations mig
2818a 68 74 20 62 65 20 75 73 65 64 2e 20 49 74 0a 2a  ht be used. It.*
2818b 2a 20 64 6f 65 73 20 6e 6f 74 20 72 65 61 6c 6c  * does not reall
2818c 79 20 6d 61 74 74 65 72 2e 20 20 57 68 61 74 20  y matter.  What 
2818d 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 69 73 20  is important is 
2818e 74 68 61 74 20 73 70 61 72 73 65 20 63 75 72 73  that sparse curs
2818f 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72 73 20 61 6c  or.** numbers al
28190 6c 20 67 65 74 20 6d 61 70 70 65 64 20 69 6e 74  l get mapped int
28191 6f 20 62 69 74 20 6e 75 6d 62 65 72 73 20 74 68  o bit numbers th
28192 61 74 20 62 65 67 69 6e 20 77 69 74 68 20 30 20  at begin with 0 
28193 61 6e 64 20 63 6f 6e 74 61 69 6e 0a 2a 2a 20 6e  and contain.** n
28194 6f 20 67 61 70 73 2e 0a 2a 2f 0a 73 74 72 75 63  o gaps..*/.struc
28195 74 20 45 78 70 72 4d 61 73 6b 53 65 74 20 7b 0a  t ExprMaskSet {.
28196 20 20 69 6e 74 20 6e 3b 20 20 20 20 20 20 20 20    int n;        
28197 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28198 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 73 73  /* Number of ass
28199 69 67 6e 65 64 20 63 75 72 73 6f 72 20 76 61 6c  igned cursor val
2819a 75 65 73 20 2a 2f 0a 20 20 69 6e 74 20 69 78 5b  ues */.  int ix[
2819b 42 4d 53 5d 3b 20 20 20 20 20 20 20 20 20 20 20  BMS];           
2819c 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72         /* Cursor
2819d 20 61 73 73 69 67 6e 65 64 20 74 6f 20 65 61 63   assigned to eac
2819e 68 20 62 69 74 20 2a 2f 0a 7d 3b 0a 0a 0a 2f 2a  h bit */.};.../*
2819f 0a 2a 2a 20 42 69 74 6d 61 73 6b 73 20 66 6f 72  .** Bitmasks for
281a0 20 74 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74   the operators t
281a1 68 61 74 20 69 6e 64 69 63 65 73 20 61 72 65 20  hat indices are 
281a2 61 62 6c 65 20 74 6f 20 65 78 70 6c 6f 69 74 2e  able to exploit.
281a3 20 20 41 6e 0a 2a 2a 20 4f 52 2d 65 64 20 63 6f    An.** OR-ed co
281a4 6d 62 69 6e 61 74 69 6f 6e 20 6f 66 20 74 68 65  mbination of the
281a5 73 65 20 76 61 6c 75 65 73 20 63 61 6e 20 62 65  se values can be
281a6 20 75 73 65 64 20 77 68 65 6e 20 73 65 61 72 63   used when searc
281a7 68 69 6e 67 20 66 6f 72 0a 2a 2a 20 74 65 72 6d  hing for.** term
281a8 73 20 69 6e 20 74 68 65 20 77 68 65 72 65 20 63  s in the where c
281a9 6c 61 75 73 65 2e 0a 2a 2f 0a 23 64 65 66 69 6e  lause..*/.#defin
281aa 65 20 57 4f 5f 49 4e 20 20 20 20 20 31 0a 23 64  e WO_IN     1.#d
281ab 65 66 69 6e 65 20 57 4f 5f 45 51 20 20 20 20 20  efine WO_EQ     
281ac 32 0a 23 64 65 66 69 6e 65 20 57 4f 5f 4c 54 20  2.#define WO_LT 
281ad 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54 4b 5f      (WO_EQ<<(TK_
281ae 4c 54 2d 54 4b 5f 45 51 29 29 0a 23 64 65 66 69  LT-TK_EQ)).#defi
281af 6e 65 20 57 4f 5f 4c 45 20 20 20 20 20 28 57 4f  ne WO_LE     (WO
281b0 5f 45 51 3c 3c 28 54 4b 5f 4c 45 2d 54 4b 5f 45  _EQ<<(TK_LE-TK_E
281b1 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f 5f 47  Q)).#define WO_G
281b2 54 20 20 20 20 20 28 57 4f 5f 45 51 3c 3c 28 54  T     (WO_EQ<<(T
281b3 4b 5f 47 54 2d 54 4b 5f 45 51 29 29 0a 23 64 65  K_GT-TK_EQ)).#de
281b4 66 69 6e 65 20 57 4f 5f 47 45 20 20 20 20 20 28  fine WO_GE     (
281b5 57 4f 5f 45 51 3c 3c 28 54 4b 5f 47 45 2d 54 4b  WO_EQ<<(TK_GE-TK
281b6 5f 45 51 29 29 0a 23 64 65 66 69 6e 65 20 57 4f  _EQ)).#define WO
281b7 5f 4d 41 54 43 48 20 20 36 34 0a 23 64 65 66 69  _MATCH  64.#defi
281b8 6e 65 20 57 4f 5f 49 53 4e 55 4c 4c 20 31 32 38  ne WO_ISNULL 128
281b9 0a 0a 2f 2a 0a 2a 2a 20 56 61 6c 75 65 20 66 6f  ../*.** Value fo
281ba 72 20 66 6c 61 67 73 20 72 65 74 75 72 6e 65 64  r flags returned
281bb 20 62 79 20 62 65 73 74 49 6e 64 65 78 28 29 2e   by bestIndex().
281bc 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 6c 65 61    .**.** The lea
281bd 73 74 20 73 69 67 6e 69 66 69 63 61 6e 74 20 62  st significant b
281be 79 74 65 20 69 73 20 72 65 73 65 72 76 65 64 20  yte is reserved 
281bf 61 73 20 61 20 6d 61 73 6b 20 66 6f 72 20 57 4f  as a mask for WO
281c0 5f 20 76 61 6c 75 65 73 20 61 62 6f 76 65 2e 0a  _ values above..
281c1 2a 2a 20 54 68 65 20 57 68 65 72 65 4c 65 76 65  ** The WhereLeve
281c2 6c 2e 66 6c 61 67 73 20 66 69 65 6c 64 20 69 73  l.flags field is
281c3 20 75 73 75 61 6c 6c 79 20 73 65 74 20 74 6f 20   usually set to 
281c4 57 4f 5f 49 4e 7c 57 4f 5f 45 51 7c 57 4f 5f 49  WO_IN|WO_EQ|WO_I
281c5 53 4e 55 4c 4c 2e 0a 2a 2a 20 42 75 74 20 69 66  SNULL..** But if
281c6 20 74 68 65 20 74 61 62 6c 65 20 69 73 20 74 68   the table is th
281c7 65 20 72 69 67 68 74 20 74 61 62 6c 65 20 6f 66  e right table of
281c8 20 61 20 6c 65 66 74 20 6a 6f 69 6e 2c 20 57 68   a left join, Wh
281c9 65 72 65 4c 65 76 65 6c 2e 66 6c 61 67 73 0a 2a  ereLevel.flags.*
281ca 2a 20 69 73 20 73 65 74 20 74 6f 20 57 4f 5f 49  * is set to WO_I
281cb 4e 7c 57 4f 5f 45 51 2e 20 20 54 68 65 20 57 68  N|WO_EQ.  The Wh
281cc 65 72 65 4c 65 76 65 6c 2e 66 6c 61 67 73 20 66  ereLevel.flags f
281cd 69 65 6c 64 20 63 61 6e 20 74 68 65 6e 20 62 65  ield can then be
281ce 20 75 73 65 64 20 61 73 0a 2a 2a 20 74 68 65 20   used as.** the 
281cf 22 6f 70 22 20 70 61 72 61 6d 65 74 65 72 20 74  "op" parameter t
281d0 6f 20 66 69 6e 64 54 65 72 6d 20 77 68 65 6e 20  o findTerm when 
281d1 77 65 20 61 72 65 20 72 65 73 6f 6c 76 69 6e 67  we are resolving
281d2 20 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72   equality constr
281d3 61 69 6e 74 73 2e 0a 2a 2a 20 49 53 4e 55 4c 4c  aints..** ISNULL
281d4 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 77 69 6c   constraints wil
281d5 6c 20 74 68 65 6e 20 6e 6f 74 20 62 65 20 75 73  l then not be us
281d6 65 64 20 6f 6e 20 74 68 65 20 72 69 67 68 74 20  ed on the right 
281d7 74 61 62 6c 65 20 6f 66 20 61 20 6c 65 66 74 0a  table of a left.
281d8 2a 2a 20 6a 6f 69 6e 2e 20 20 54 69 63 6b 65 74  ** join.  Ticket
281d9 73 20 23 32 31 37 37 20 61 6e 64 20 23 32 31 38  s #2177 and #218
281da 39 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  9..*/.#define WH
281db 45 52 45 5f 52 4f 57 49 44 5f 45 51 20 20 20 20  ERE_ROWID_EQ    
281dc 20 30 78 30 30 30 31 30 30 20 20 20 2f 2a 20 72   0x000100   /* r
281dd 6f 77 69 64 3d 45 58 50 52 20 6f 72 20 72 6f 77  owid=EXPR or row
281de 69 64 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f 0a 23  id IN (...) */.#
281df 64 65 66 69 6e 65 20 57 48 45 52 45 5f 52 4f 57  define WHERE_ROW
281e0 49 44 5f 52 41 4e 47 45 20 20 30 78 30 30 30 32  ID_RANGE  0x0002
281e1 30 30 20 20 20 2f 2a 20 72 6f 77 69 64 3c 45 58  00   /* rowid<EX
281e2 50 52 20 61 6e 64 2f 6f 72 20 72 6f 77 69 64 3e  PR and/or rowid>
281e3 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e 65 20  EXPR */.#define 
281e4 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 45 51 20  WHERE_COLUMN_EQ 
281e5 20 20 20 30 78 30 30 31 30 30 30 20 20 20 2f 2a     0x001000   /*
281e6 20 78 3d 45 58 50 52 20 6f 72 20 78 20 49 4e 20   x=EXPR or x IN 
281e7 28 2e 2e 2e 29 20 2a 2f 0a 23 64 65 66 69 6e 65  (...) */.#define
281e8 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41   WHERE_COLUMN_RA
281e9 4e 47 45 20 30 78 30 30 32 30 30 30 20 20 20 2f  NGE 0x002000   /
281ea 2a 20 78 3c 45 58 50 52 20 61 6e 64 2f 6f 72 20  * x<EXPR and/or 
281eb 78 3e 45 58 50 52 20 2a 2f 0a 23 64 65 66 69 6e  x>EXPR */.#defin
281ec 65 20 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 49  e WHERE_COLUMN_I
281ed 4e 20 20 20 20 30 78 30 30 34 30 30 30 20 20 20  N    0x004000   
281ee 2f 2a 20 78 20 49 4e 20 28 2e 2e 2e 29 20 2a 2f  /* x IN (...) */
281ef 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45 5f 54  .#define WHERE_T
281f0 4f 50 5f 4c 49 4d 49 54 20 20 20 20 30 78 30 31  OP_LIMIT    0x01
281f1 30 30 30 30 20 20 20 2f 2a 20 78 3c 45 58 50 52  0000   /* x<EXPR
281f2 20 6f 72 20 78 3c 3d 45 58 50 52 20 63 6f 6e 73   or x<=EXPR cons
281f3 74 72 61 69 6e 74 20 2a 2f 0a 23 64 65 66 69 6e  traint */.#defin
281f4 65 20 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49  e WHERE_BTM_LIMI
281f5 54 20 20 20 20 30 78 30 32 30 30 30 30 20 20 20  T    0x020000   
281f6 2f 2a 20 78 3e 45 58 50 52 20 6f 72 20 78 3e 3d  /* x>EXPR or x>=
281f7 45 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 20  EXPR constraint 
281f8 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48 45 52 45  */.#define WHERE
281f9 5f 49 44 58 5f 4f 4e 4c 59 20 20 20 20 20 30 78  _IDX_ONLY     0x
281fa 30 38 30 30 30 30 20 20 20 2f 2a 20 55 73 65 20  080000   /* Use 
281fb 69 6e 64 65 78 20 6f 6e 6c 79 20 2d 20 6f 6d 69  index only - omi
281fc 74 20 74 61 62 6c 65 20 2a 2f 0a 23 64 65 66 69  t table */.#defi
281fd 6e 65 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59  ne WHERE_ORDERBY
281fe 20 20 20 20 20 20 30 78 31 30 30 30 30 30 20 20        0x100000  
281ff 20 2f 2a 20 4f 75 74 70 75 74 20 77 69 6c 6c 20   /* Output will 
28200 61 70 70 65 61 72 20 69 6e 20 63 6f 72 72 65 63  appear in correc
28201 74 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65 66 69  t order */.#defi
28202 6e 65 20 57 48 45 52 45 5f 52 45 56 45 52 53 45  ne WHERE_REVERSE
28203 20 20 20 20 20 20 30 78 32 30 30 30 30 30 20 20        0x200000  
28204 20 2f 2a 20 53 63 61 6e 20 69 6e 20 72 65 76 65   /* Scan in reve
28205 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 23 64 65  rse order */.#de
28206 66 69 6e 65 20 57 48 45 52 45 5f 55 4e 49 51 55  fine WHERE_UNIQU
28207 45 20 20 20 20 20 20 20 30 78 34 30 30 30 30 30  E       0x400000
28208 20 20 20 2f 2a 20 53 65 6c 65 63 74 73 20 6e 6f     /* Selects no
28209 20 6d 6f 72 65 20 74 68 61 6e 20 6f 6e 65 20 72   more than one r
2820a 6f 77 20 2a 2f 0a 23 64 65 66 69 6e 65 20 57 48  ow */.#define WH
2820b 45 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  ERE_VIRTUALTABLE
2820c 20 30 78 38 30 30 30 30 30 20 20 20 2f 2a 20 55   0x800000   /* U
2820d 73 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65  se virtual-table
2820e 20 70 72 6f 63 65 73 73 69 6e 67 20 2a 2f 0a 0a   processing */..
2820f 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
28210 20 61 20 70 72 65 61 6c 6c 6f 63 61 74 65 64 20   a preallocated 
28211 57 68 65 72 65 43 6c 61 75 73 65 20 73 74 72 75  WhereClause stru
28212 63 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  cture..*/.static
28213 20 76 6f 69 64 20 77 68 65 72 65 43 6c 61 75 73   void whereClaus
28214 65 49 6e 69 74 28 0a 20 20 57 68 65 72 65 43 6c  eInit(.  WhereCl
28215 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20 20 20  ause *pWC,      
28216 20 20 2f 2a 20 54 68 65 20 57 68 65 72 65 43 6c    /* The WhereCl
28217 61 75 73 65 20 74 6f 20 62 65 20 69 6e 69 74 69  ause to be initi
28218 61 6c 69 7a 65 64 20 2a 2f 0a 20 20 50 61 72 73  alized */.  Pars
28219 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
2821a 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
2821b 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
2821c 20 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d   ExprMaskSet *pM
2821d 61 73 6b 53 65 74 20 20 20 20 2f 2a 20 4d 61 70  askSet    /* Map
2821e 70 69 6e 67 20 66 72 6f 6d 20 74 61 62 6c 65 20  ping from table 
2821f 69 6e 64 69 63 65 73 20 74 6f 20 62 69 74 6d 61  indices to bitma
28220 73 6b 73 20 2a 2f 0a 29 7b 0a 20 20 70 57 43 2d  sks */.){.  pWC-
28221 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72 73 65  >pParse = pParse
28222 3b 0a 20 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65  ;.  pWC->pMaskSe
28223 74 20 3d 20 70 4d 61 73 6b 53 65 74 3b 0a 20 20  t = pMaskSet;.  
28224 70 57 43 2d 3e 6e 54 65 72 6d 20 3d 20 30 3b 0a  pWC->nTerm = 0;.
28225 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 3d 20 41    pWC->nSlot = A
28226 72 72 61 79 53 69 7a 65 28 70 57 43 2d 3e 61 53  rraySize(pWC->aS
28227 74 61 74 69 63 29 3b 0a 20 20 70 57 43 2d 3e 61  tatic);.  pWC->a
28228 20 3d 20 70 57 43 2d 3e 61 53 74 61 74 69 63 3b   = pWC->aStatic;
28229 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 61 6c 6c 6f  .}../*.** Deallo
2822a 63 61 74 65 20 61 20 57 68 65 72 65 43 6c 61 75  cate a WhereClau
2822b 73 65 20 73 74 72 75 63 74 75 72 65 2e 20 20 54  se structure.  T
2822c 68 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73  he WhereClause s
2822d 74 72 75 63 74 75 72 65 0a 2a 2a 20 69 74 73 65  tructure.** itse
2822e 6c 66 20 69 73 20 6e 6f 74 20 66 72 65 65 64 2e  lf is not freed.
2822f 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69    This routine i
28230 73 20 74 68 65 20 69 6e 76 65 72 73 65 20 6f 66  s the inverse of
28231 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e 69 74   whereClauseInit
28232 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ()..*/.static vo
28233 69 64 20 77 68 65 72 65 43 6c 61 75 73 65 43 6c  id whereClauseCl
28234 65 61 72 28 57 68 65 72 65 43 6c 61 75 73 65 20  ear(WhereClause 
28235 2a 70 57 43 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  *pWC){.  int i;.
28236 20 20 57 68 65 72 65 54 65 72 6d 20 2a 61 3b 0a    WhereTerm *a;.
28237 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20    sqlite3 *db = 
28238 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64 62 3b  pWC->pParse->db;
28239 0a 20 20 66 6f 72 28 69 3d 70 57 43 2d 3e 6e 54  .  for(i=pWC->nT
2823a 65 72 6d 2d 31 2c 20 61 3d 70 57 43 2d 3e 61 3b  erm-1, a=pWC->a;
2823b 20 69 3e 3d 30 3b 20 69 2d 2d 2c 20 61 2b 2b 29   i>=0; i--, a++)
2823c 7b 0a 20 20 20 20 69 66 28 20 61 2d 3e 66 6c 61  {.    if( a->fla
2823d 67 73 20 26 20 54 45 52 4d 5f 44 59 4e 41 4d 49  gs & TERM_DYNAMI
2823e 43 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  C ){.      sqlit
2823f 65 33 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c  e3ExprDelete(db,
28240 20 61 2d 3e 70 45 78 70 72 29 3b 0a 20 20 20 20   a->pExpr);.    
28241 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 57 43 2d  }.  }.  if( pWC-
28242 3e 61 21 3d 70 57 43 2d 3e 61 53 74 61 74 69 63  >a!=pWC->aStatic
28243 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
28244 62 46 72 65 65 28 64 62 2c 20 70 57 43 2d 3e 61  bFree(db, pWC->a
28245 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
28246 41 64 64 20 61 20 6e 65 77 20 65 6e 74 72 69 65  Add a new entrie
28247 73 20 74 6f 20 74 68 65 20 57 68 65 72 65 43 6c  s to the WhereCl
28248 61 75 73 65 20 73 74 72 75 63 74 75 72 65 2e 20  ause structure. 
28249 20 49 6e 63 72 65 61 73 65 20 74 68 65 20 61 6c   Increase the al
2824a 6c 6f 63 61 74 65 64 0a 2a 2a 20 73 70 61 63 65  located.** space
2824b 20 61 73 20 6e 65 63 65 73 73 61 72 79 2e 0a 2a   as necessary..*
2824c 2a 0a 2a 2a 20 49 66 20 74 68 65 20 66 6c 61 67  *.** If the flag
2824d 73 20 61 72 67 75 6d 65 6e 74 20 69 6e 63 6c 75  s argument inclu
2824e 64 65 73 20 54 45 52 4d 5f 44 59 4e 41 4d 49 43  des TERM_DYNAMIC
2824f 2c 20 74 68 65 6e 20 72 65 73 70 6f 6e 73 69 62  , then responsib
28250 69 6c 69 74 79 0a 2a 2a 20 66 6f 72 20 66 72 65  ility.** for fre
28251 65 69 6e 67 20 74 68 65 20 65 78 70 72 65 73 73  eing the express
28252 69 6f 6e 20 70 20 69 73 20 61 73 73 75 6d 65 64  ion p is assumed
28253 20 62 79 20 74 68 65 20 57 68 65 72 65 43 6c 61   by the WhereCla
28254 75 73 65 20 6f 62 6a 65 63 74 2e 0a 2a 2a 0a 2a  use object..**.*
28255 2a 20 57 41 52 4e 49 4e 47 3a 20 20 54 68 69 73  * WARNING:  This
28256 20 72 6f 75 74 69 6e 65 20 6d 69 67 68 74 20 72   routine might r
28257 65 61 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 70  eallocate the sp
28258 61 63 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72  ace used to stor
28259 65 0a 2a 2a 20 57 68 65 72 65 54 65 72 6d 73 2e  e.** WhereTerms.
2825a 20 20 41 6c 6c 20 70 6f 69 6e 74 65 72 73 20 74    All pointers t
2825b 6f 20 57 68 65 72 65 54 65 72 6d 73 20 73 68 6f  o WhereTerms sho
2825c 75 6c 64 20 62 65 20 69 6e 76 61 6c 69 64 61 74  uld be invalidat
2825d 65 64 20 61 66 74 65 72 0a 2a 2a 20 63 61 6c 6c  ed after.** call
2825e 69 6e 67 20 74 68 69 73 20 72 6f 75 74 69 6e 65  ing this routine
2825f 2e 20 20 53 75 63 68 20 70 6f 69 6e 74 65 72 73  .  Such pointers
28260 20 6d 61 79 20 62 65 20 72 65 69 6e 69 74 69 61   may be reinitia
28261 6c 69 7a 65 64 20 62 79 20 72 65 66 65 72 65 6e  lized by referen
28262 63 69 6e 67 0a 2a 2a 20 74 68 65 20 70 57 43 2d  cing.** the pWC-
28263 3e 61 5b 5d 20 61 72 72 61 79 2e 0a 2a 2f 0a 73  >a[] array..*/.s
28264 74 61 74 69 63 20 69 6e 74 20 77 68 65 72 65 43  tatic int whereC
28265 6c 61 75 73 65 49 6e 73 65 72 74 28 57 68 65 72  lauseInsert(Wher
28266 65 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 45 78  eClause *pWC, Ex
28267 70 72 20 2a 70 2c 20 69 6e 74 20 66 6c 61 67 73  pr *p, int flags
28268 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a  ){.  WhereTerm *
28269 70 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 64 78  pTerm;.  int idx
2826a 3b 0a 20 20 69 66 28 20 70 57 43 2d 3e 6e 54 65  ;.  if( pWC->nTe
2826b 72 6d 3e 3d 70 57 43 2d 3e 6e 53 6c 6f 74 20 29  rm>=pWC->nSlot )
2826c 7b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20  {.    WhereTerm 
2826d 2a 70 4f 6c 64 20 3d 20 70 57 43 2d 3e 61 3b 0a  *pOld = pWC->a;.
2826e 20 20 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20      sqlite3 *db 
2826f 3d 20 70 57 43 2d 3e 70 50 61 72 73 65 2d 3e 64  = pWC->pParse->d
28270 62 3b 0a 20 20 20 20 70 57 43 2d 3e 61 20 3d 20  b;.    pWC->a = 
28271 73 71 6c 69 74 65 33 44 62 4d 61 6c 6c 6f 63 52  sqlite3DbMallocR
28272 61 77 28 64 62 2c 20 73 69 7a 65 6f 66 28 70 57  aw(db, sizeof(pW
28273 43 2d 3e 61 5b 30 5d 29 2a 70 57 43 2d 3e 6e 53  C->a[0])*pWC->nS
28274 6c 6f 74 2a 32 20 29 3b 0a 20 20 20 20 69 66 28  lot*2 );.    if(
28275 20 70 57 43 2d 3e 61 3d 3d 30 20 29 7b 0a 20 20   pWC->a==0 ){.  
28276 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 20      if( flags & 
28277 54 45 52 4d 5f 44 59 4e 41 4d 49 43 20 29 7b 0a  TERM_DYNAMIC ){.
28278 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45          sqlite3E
28279 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70 29  xprDelete(db, p)
2827a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2827b 70 57 43 2d 3e 61 20 3d 20 70 4f 6c 64 3b 0a 20  pWC->a = pOld;. 
2827c 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
2827d 20 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28     }.    memcpy(
2827e 70 57 43 2d 3e 61 2c 20 70 4f 6c 64 2c 20 73 69  pWC->a, pOld, si
2827f 7a 65 6f 66 28 70 57 43 2d 3e 61 5b 30 5d 29 2a  zeof(pWC->a[0])*
28280 70 57 43 2d 3e 6e 54 65 72 6d 29 3b 0a 20 20 20  pWC->nTerm);.   
28281 20 69 66 28 20 70 4f 6c 64 21 3d 70 57 43 2d 3e   if( pOld!=pWC->
28282 61 53 74 61 74 69 63 20 29 7b 0a 20 20 20 20 20  aStatic ){.     
28283 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28 64   sqlite3DbFree(d
28284 62 2c 20 70 4f 6c 64 29 3b 0a 20 20 20 20 7d 0a  b, pOld);.    }.
28285 20 20 20 20 70 57 43 2d 3e 6e 53 6c 6f 74 20 2a      pWC->nSlot *
28286 3d 20 32 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d  = 2;.  }.  pTerm
28287 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 20 3d   = &pWC->a[idx =
28288 20 70 57 43 2d 3e 6e 54 65 72 6d 5d 3b 0a 20 20   pWC->nTerm];.  
28289 70 57 43 2d 3e 6e 54 65 72 6d 2b 2b 3b 0a 20 20  pWC->nTerm++;.  
2828a 70 54 65 72 6d 2d 3e 70 45 78 70 72 20 3d 20 70  pTerm->pExpr = p
2828b 3b 0a 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73  ;.  pTerm->flags
2828c 20 3d 20 66 6c 61 67 73 3b 0a 20 20 70 54 65 72   = flags;.  pTer
2828d 6d 2d 3e 70 57 43 20 3d 20 70 57 43 3b 0a 20 20  m->pWC = pWC;.  
2828e 70 54 65 72 6d 2d 3e 69 50 61 72 65 6e 74 20 3d  pTerm->iParent =
2828f 20 2d 31 3b 0a 20 20 72 65 74 75 72 6e 20 69 64   -1;.  return id
28290 78 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  x;.}../*.** This
28291 20 72 6f 75 74 69 6e 65 20 69 64 65 6e 74 69 66   routine identif
28292 69 65 73 20 73 75 62 65 78 70 72 65 73 73 69 6f  ies subexpressio
28293 6e 73 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  ns in the WHERE 
28294 63 6c 61 75 73 65 20 77 68 65 72 65 0a 2a 2a 20  clause where.** 
28295 65 61 63 68 20 73 75 62 65 78 70 72 65 73 73 69  each subexpressi
28296 6f 6e 20 69 73 20 73 65 70 61 72 61 74 65 64 20  on is separated 
28297 62 79 20 74 68 65 20 41 4e 44 20 6f 70 65 72 61  by the AND opera
28298 74 6f 72 20 6f 72 20 73 6f 6d 65 20 6f 74 68 65  tor or some othe
28299 72 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 73 70  r.** operator sp
2829a 65 63 69 66 69 65 64 20 69 6e 20 74 68 65 20 6f  ecified in the o
2829b 70 20 70 61 72 61 6d 65 74 65 72 2e 20 20 54 68  p parameter.  Th
2829c 65 20 57 68 65 72 65 43 6c 61 75 73 65 20 73 74  e WhereClause st
2829d 72 75 63 74 75 72 65 0a 2a 2a 20 69 73 20 66 69  ructure.** is fi
2829e 6c 6c 65 64 20 77 69 74 68 20 70 6f 69 6e 74 65  lled with pointe
2829f 72 73 20 74 6f 20 73 75 62 65 78 70 72 65 73 73  rs to subexpress
282a0 69 6f 6e 73 2e 20 20 46 6f 72 20 65 78 61 6d 70  ions.  For examp
282a1 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 57 48 45  le:.**.**    WHE
282a2 52 45 20 20 61 3d 3d 27 68 65 6c 6c 6f 27 20 41  RE  a=='hello' A
282a3 4e 44 20 63 6f 61 6c 65 73 63 65 28 62 2c 31 31  ND coalesce(b,11
282a4 29 3c 31 30 20 41 4e 44 20 28 63 2b 31 32 21 3d  )<10 AND (c+12!=
282a5 64 20 4f 52 20 63 3d 3d 32 32 29 0a 2a 2a 20 20  d OR c==22).**  
282a6 20 20 20 20 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f           \______
282a7 5f 5f 2f 20 20 20 20 20 5c 5f 5f 5f 5f 5f 5f 5f  __/     \_______
282a8 5f 5f 5f 5f 5f 5f 5f 5f 2f 20 20 20 20 20 5c 5f  ________/     \_
282a9 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 5f 2f  _______________/
282aa 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 73  .**            s
282ab 6c 6f 74 5b 30 5d 20 20 20 20 20 20 20 20 20 20  lot[0]          
282ac 20 20 73 6c 6f 74 5b 31 5d 20 20 20 20 20 20 20    slot[1]       
282ad 20 20 20 20 20 20 20 20 73 6c 6f 74 5b 32 5d 0a          slot[2].
282ae 2a 2a 0a 2a 2a 20 54 68 65 20 6f 72 69 67 69 6e  **.** The origin
282af 61 6c 20 57 48 45 52 45 20 63 6c 61 75 73 65 20  al WHERE clause 
282b0 69 6e 20 70 45 78 70 72 20 69 73 20 75 6e 61 6c  in pExpr is unal
282b1 74 65 72 65 64 2e 20 20 41 6c 6c 20 74 68 69 73  tered.  All this
282b2 20 72 6f 75 74 69 6e 65 0a 2a 2a 20 64 6f 65 73   routine.** does
282b3 20 69 73 20 6d 61 6b 65 20 73 6c 6f 74 5b 5d 20   is make slot[] 
282b4 65 6e 74 72 69 65 73 20 70 6f 69 6e 74 20 74 6f  entries point to
282b5 20 73 75 62 73 74 72 75 63 74 75 72 65 20 77 69   substructure wi
282b6 74 68 69 6e 20 70 45 78 70 72 2e 0a 2a 2a 0a 2a  thin pExpr..**.*
282b7 2a 20 49 6e 20 74 68 65 20 70 72 65 76 69 6f 75  * In the previou
282b8 73 20 73 65 6e 74 65 6e 63 65 20 61 6e 64 20 69  s sentence and i
282b9 6e 20 74 68 65 20 64 69 61 67 72 61 6d 2c 20 22  n the diagram, "
282ba 73 6c 6f 74 5b 5d 22 20 72 65 66 65 72 73 20 74  slot[]" refers t
282bb 6f 0a 2a 2a 20 74 68 65 20 57 68 65 72 65 43 6c  o.** the WhereCl
282bc 61 75 73 65 2e 61 5b 5d 20 61 72 72 61 79 2e 20  ause.a[] array. 
282bd 20 54 68 69 73 20 61 72 72 61 79 20 67 72 6f 77   This array grow
282be 73 20 61 73 20 6e 65 65 64 65 64 20 74 6f 20 63  s as needed to c
282bf 6f 6e 74 61 69 6e 0a 2a 2a 20 61 6c 6c 20 74 65  ontain.** all te
282c0 72 6d 73 20 6f 66 20 74 68 65 20 57 48 45 52 45  rms of the WHERE
282c1 20 63 6c 61 75 73 65 2e 0a 2a 2f 0a 73 74 61 74   clause..*/.stat
282c2 69 63 20 76 6f 69 64 20 77 68 65 72 65 53 70 6c  ic void whereSpl
282c3 69 74 28 57 68 65 72 65 43 6c 61 75 73 65 20 2a  it(WhereClause *
282c4 70 57 43 2c 20 45 78 70 72 20 2a 70 45 78 70 72  pWC, Expr *pExpr
282c5 2c 20 69 6e 74 20 6f 70 29 7b 0a 20 20 69 66 28  , int op){.  if(
282c6 20 70 45 78 70 72 3d 3d 30 20 29 20 72 65 74 75   pExpr==0 ) retu
282c7 72 6e 3b 0a 20 20 69 66 28 20 70 45 78 70 72 2d  rn;.  if( pExpr-
282c8 3e 6f 70 21 3d 6f 70 20 29 7b 0a 20 20 20 20 77  >op!=op ){.    w
282c9 68 65 72 65 43 6c 61 75 73 65 49 6e 73 65 72 74  hereClauseInsert
282ca 28 70 57 43 2c 20 70 45 78 70 72 2c 20 30 29 3b  (pWC, pExpr, 0);
282cb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 77 68  .  }else{.    wh
282cc 65 72 65 53 70 6c 69 74 28 70 57 43 2c 20 70 45  ereSplit(pWC, pE
282cd 78 70 72 2d 3e 70 4c 65 66 74 2c 20 6f 70 29 3b  xpr->pLeft, op);
282ce 0a 20 20 20 20 77 68 65 72 65 53 70 6c 69 74 28  .    whereSplit(
282cf 70 57 43 2c 20 70 45 78 70 72 2d 3e 70 52 69 67  pWC, pExpr->pRig
282d0 68 74 2c 20 6f 70 29 3b 0a 20 20 7d 0a 7d 0a 0a  ht, op);.  }.}..
282d1 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 7a 65  /*.** Initialize
282d2 20 61 6e 20 65 78 70 72 65 73 73 69 6f 6e 20 6d   an expression m
282d3 61 73 6b 20 73 65 74 0a 2a 2f 0a 23 64 65 66 69  ask set.*/.#defi
282d4 6e 65 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 50  ne initMaskSet(P
282d5 29 20 20 6d 65 6d 73 65 74 28 50 2c 20 30 2c 20  )  memset(P, 0, 
282d6 73 69 7a 65 6f 66 28 2a 50 29 29 0a 0a 2f 2a 0a  sizeof(*P))../*.
282d7 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 62 69  ** Return the bi
282d8 74 6d 61 73 6b 20 66 6f 72 20 74 68 65 20 67 69  tmask for the gi
282d9 76 65 6e 20 63 75 72 73 6f 72 20 6e 75 6d 62 65  ven cursor numbe
282da 72 2e 20 20 52 65 74 75 72 6e 20 30 20 69 66 0a  r.  Return 0 if.
282db 2a 2a 20 69 43 75 72 73 6f 72 20 69 73 20 6e 6f  ** iCursor is no
282dc 74 20 69 6e 20 74 68 65 20 73 65 74 2e 0a 2a 2f  t in the set..*/
282dd 0a 73 74 61 74 69 63 20 42 69 74 6d 61 73 6b 20  .static Bitmask 
282de 67 65 74 4d 61 73 6b 28 45 78 70 72 4d 61 73 6b  getMask(ExprMask
282df 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 69  Set *pMaskSet, i
282e0 6e 74 20 69 43 75 72 73 6f 72 29 7b 0a 20 20 69  nt iCursor){.  i
282e1 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
282e2 20 69 3c 70 4d 61 73 6b 53 65 74 2d 3e 6e 3b 20   i<pMaskSet->n; 
282e3 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 4d  i++){.    if( pM
282e4 61 73 6b 53 65 74 2d 3e 69 78 5b 69 5d 3d 3d 69  askSet->ix[i]==i
282e5 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 20 20  Cursor ){.      
282e6 72 65 74 75 72 6e 20 28 28 42 69 74 6d 61 73 6b  return ((Bitmask
282e7 29 31 29 3c 3c 69 3b 0a 20 20 20 20 7d 0a 20 20  )1)<<i;.    }.  
282e8 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a  }.  return 0;.}.
282e9 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20  ./*.** Create a 
282ea 6e 65 77 20 6d 61 73 6b 20 66 6f 72 20 63 75 72  new mask for cur
282eb 73 6f 72 20 69 43 75 72 73 6f 72 2e 0a 2a 2a 0a  sor iCursor..**.
282ec 2a 2a 20 54 68 65 72 65 20 69 73 20 6f 6e 65 20  ** There is one 
282ed 63 75 72 73 6f 72 20 70 65 72 20 74 61 62 6c 65  cursor per table
282ee 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61   in the FROM cla
282ef 75 73 65 2e 20 20 54 68 65 20 6e 75 6d 62 65 72  use.  The number
282f0 20 6f 66 0a 2a 2a 20 74 61 62 6c 65 73 20 69 6e   of.** tables in
282f1 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65   the FROM clause
282f2 20 69 73 20 6c 69 6d 69 74 65 64 20 62 79 20 61   is limited by a
282f3 20 74 65 73 74 20 65 61 72 6c 79 20 69 6e 20 74   test early in t
282f4 68 65 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68 65  he.** sqlite3Whe
282f5 72 65 42 65 67 69 6e 28 29 20 72 6f 75 74 69 6e  reBegin() routin
282f6 65 2e 20 20 53 6f 20 77 65 20 6b 6e 6f 77 20 74  e.  So we know t
282f7 68 61 74 20 74 68 65 20 70 4d 61 73 6b 53 65 74  hat the pMaskSet
282f8 2d 3e 69 78 5b 5d 0a 2a 2a 20 61 72 72 61 79 20  ->ix[].** array 
282f9 77 69 6c 6c 20 6e 65 76 65 72 20 6f 76 65 72 66  will never overf
282fa 6c 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  low..*/.static v
282fb 6f 69 64 20 63 72 65 61 74 65 4d 61 73 6b 28 45  oid createMask(E
282fc 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73  xprMaskSet *pMas
282fd 6b 53 65 74 2c 20 69 6e 74 20 69 43 75 72 73 6f  kSet, int iCurso
282fe 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4d  r){.  assert( pM
282ff 61 73 6b 53 65 74 2d 3e 6e 20 3c 20 41 72 72 61  askSet->n < Arra
28300 79 53 69 7a 65 28 70 4d 61 73 6b 53 65 74 2d 3e  ySize(pMaskSet->
28301 69 78 29 20 29 3b 0a 20 20 70 4d 61 73 6b 53 65  ix) );.  pMaskSe
28302 74 2d 3e 69 78 5b 70 4d 61 73 6b 53 65 74 2d 3e  t->ix[pMaskSet->
28303 6e 2b 2b 5d 20 3d 20 69 43 75 72 73 6f 72 3b 0a  n++] = iCursor;.
28304 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
28305 75 74 69 6e 65 20 77 61 6c 6b 73 20 28 72 65 63  utine walks (rec
28306 75 72 73 69 76 65 6c 79 29 20 61 6e 20 65 78 70  ursively) an exp
28307 72 65 73 73 69 6f 6e 20 74 72 65 65 20 61 6e 64  ression tree and
28308 20 67 65 6e 65 72 61 74 65 73 0a 2a 2a 20 61 20   generates.** a 
28309 62 69 74 6d 61 73 6b 20 69 6e 64 69 63 61 74 69  bitmask indicati
2830a 6e 67 20 77 68 69 63 68 20 74 61 62 6c 65 73 20  ng which tables 
2830b 61 72 65 20 75 73 65 64 20 69 6e 20 74 68 61 74  are used in that
2830c 20 65 78 70 72 65 73 73 69 6f 6e 0a 2a 2a 20 74   expression.** t
2830d 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 6f 72  ree..**.** In or
2830e 64 65 72 20 66 6f 72 20 74 68 69 73 20 72 6f 75  der for this rou
2830f 74 69 6e 65 20 74 6f 20 77 6f 72 6b 2c 20 74 68  tine to work, th
28310 65 20 63 61 6c 6c 69 6e 67 20 66 75 6e 63 74 69  e calling functi
28311 6f 6e 20 6d 75 73 74 20 68 61 76 65 0a 2a 2a 20  on must have.** 
28312 70 72 65 76 69 6f 75 73 6c 79 20 69 6e 76 6f 6b  previously invok
28313 65 64 20 73 71 6c 69 74 65 33 52 65 73 6f 6c 76  ed sqlite3Resolv
28314 65 45 78 70 72 4e 61 6d 65 73 28 29 20 6f 6e 20  eExprNames() on 
28315 74 68 65 20 65 78 70 72 65 73 73 69 6f 6e 2e 20  the expression. 
28316 20 53 65 65 0a 2a 2a 20 74 68 65 20 68 65 61 64   See.** the head
28317 65 72 20 63 6f 6d 6d 65 6e 74 20 6f 6e 20 74 68  er comment on th
28318 61 74 20 72 6f 75 74 69 6e 65 20 66 6f 72 20 61  at routine for a
28319 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
2831a 61 74 69 6f 6e 2e 0a 2a 2a 20 54 68 65 20 73 71  ation..** The sq
2831b 6c 69 74 65 33 52 65 73 6f 6c 76 65 45 78 70 72  lite3ResolveExpr
2831c 4e 61 6d 65 73 28 29 20 72 6f 75 74 69 6e 65 73  Names() routines
2831d 20 6c 6f 6f 6b 73 20 66 6f 72 20 63 6f 6c 75 6d   looks for colum
2831e 6e 20 6e 61 6d 65 73 20 61 6e 64 0a 2a 2a 20 73  n names and.** s
2831f 65 74 73 20 74 68 65 69 72 20 6f 70 63 6f 64 65  ets their opcode
28320 73 20 74 6f 20 54 4b 5f 43 4f 4c 55 4d 4e 20 61  s to TK_COLUMN a
28321 6e 64 20 74 68 65 69 72 20 45 78 70 72 2e 69 54  nd their Expr.iT
28322 61 62 6c 65 20 66 69 65 6c 64 73 20 74 6f 0a 2a  able fields to.*
28323 2a 20 74 68 65 20 56 44 42 45 20 63 75 72 73 6f  * the VDBE curso
28324 72 20 6e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  r number of the 
28325 74 61 62 6c 65 2e 20 20 54 68 69 73 20 72 6f 75  table.  This rou
28326 74 69 6e 65 20 6a 75 73 74 20 68 61 73 20 74 6f  tine just has to
28327 0a 2a 2a 20 74 72 61 6e 73 6c 61 74 65 20 74 68  .** translate th
28328 65 20 63 75 72 73 6f 72 20 6e 75 6d 62 65 72 73  e cursor numbers
28329 20 69 6e 74 6f 20 62 69 74 6d 61 73 6b 20 76 61   into bitmask va
2832a 6c 75 65 73 20 61 6e 64 20 4f 52 20 61 6c 6c 0a  lues and OR all.
2832b 2a 2a 20 74 68 65 20 62 69 74 6d 61 73 6b 73 20  ** the bitmasks 
2832c 74 6f 67 65 74 68 65 72 2e 0a 2a 2f 0a 73 74 61  together..*/.sta
2832d 74 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72  tic Bitmask expr
2832e 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 45  ListTableUsage(E
2832f 78 70 72 4d 61 73 6b 53 65 74 2a 2c 20 45 78 70  xprMaskSet*, Exp
28330 72 4c 69 73 74 2a 29 3b 0a 73 74 61 74 69 63 20  rList*);.static 
28331 42 69 74 6d 61 73 6b 20 65 78 70 72 53 65 6c 65  Bitmask exprSele
28332 63 74 54 61 62 6c 65 55 73 61 67 65 28 45 78 70  ctTableUsage(Exp
28333 72 4d 61 73 6b 53 65 74 2a 2c 20 53 65 6c 65 63  rMaskSet*, Selec
28334 74 2a 29 3b 0a 73 74 61 74 69 63 20 42 69 74 6d  t*);.static Bitm
28335 61 73 6b 20 65 78 70 72 54 61 62 6c 65 55 73 61  ask exprTableUsa
28336 67 65 28 45 78 70 72 4d 61 73 6b 53 65 74 20 2a  ge(ExprMaskSet *
28337 70 4d 61 73 6b 53 65 74 2c 20 45 78 70 72 20 2a  pMaskSet, Expr *
28338 70 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61  p){.  Bitmask ma
28339 73 6b 20 3d 20 30 3b 0a 20 20 69 66 28 20 70 3d  sk = 0;.  if( p=
2833a 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
2833b 20 69 66 28 20 70 2d 3e 6f 70 3d 3d 54 4b 5f 43   if( p->op==TK_C
2833c 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6d 61 73  OLUMN ){.    mas
2833d 6b 20 3d 20 67 65 74 4d 61 73 6b 28 70 4d 61 73  k = getMask(pMas
2833e 6b 53 65 74 2c 20 70 2d 3e 69 54 61 62 6c 65 29  kSet, p->iTable)
2833f 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 6d 61 73  ;.    return mas
28340 6b 3b 0a 20 20 7d 0a 20 20 6d 61 73 6b 20 3d 20  k;.  }.  mask = 
28341 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
28342 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 52 69 67  MaskSet, p->pRig
28343 68 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20 65  ht);.  mask |= e
28344 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70 4d  xprTableUsage(pM
28345 61 73 6b 53 65 74 2c 20 70 2d 3e 70 4c 65 66 74  askSet, p->pLeft
28346 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70  );.  mask |= exp
28347 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65 28  rListTableUsage(
28348 70 4d 61 73 6b 53 65 74 2c 20 70 2d 3e 70 4c 69  pMaskSet, p->pLi
28349 73 74 29 3b 0a 20 20 6d 61 73 6b 20 7c 3d 20 65  st);.  mask |= e
2834a 78 70 72 53 65 6c 65 63 74 54 61 62 6c 65 55 73  xprSelectTableUs
2834b 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 2d  age(pMaskSet, p-
2834c 3e 70 53 65 6c 65 63 74 29 3b 0a 20 20 72 65 74  >pSelect);.  ret
2834d 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74  urn mask;.}.stat
2834e 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 4c  ic Bitmask exprL
2834f 69 73 74 54 61 62 6c 65 55 73 61 67 65 28 45 78  istTableUsage(Ex
28350 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  prMaskSet *pMask
28351 53 65 74 2c 20 45 78 70 72 4c 69 73 74 20 2a 70  Set, ExprList *p
28352 4c 69 73 74 29 7b 0a 20 20 69 6e 74 20 69 3b 0a  List){.  int i;.
28353 20 20 42 69 74 6d 61 73 6b 20 6d 61 73 6b 20 3d    Bitmask mask =
28354 20 30 3b 0a 20 20 69 66 28 20 70 4c 69 73 74 20   0;.  if( pList 
28355 29 7b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20  ){.    for(i=0; 
28356 69 3c 70 4c 69 73 74 2d 3e 6e 45 78 70 72 3b 20  i<pList->nExpr; 
28357 69 2b 2b 29 7b 0a 20 20 20 20 20 20 6d 61 73 6b  i++){.      mask
28358 20 7c 3d 20 65 78 70 72 54 61 62 6c 65 55 73 61   |= exprTableUsa
28359 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69  ge(pMaskSet, pLi
2835a 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 3b  st->a[i].pExpr);
2835b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74  .    }.  }.  ret
2835c 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 73 74 61 74  urn mask;.}.stat
2835d 69 63 20 42 69 74 6d 61 73 6b 20 65 78 70 72 53  ic Bitmask exprS
2835e 65 6c 65 63 74 54 61 62 6c 65 55 73 61 67 65 28  electTableUsage(
2835f 45 78 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61  ExprMaskSet *pMa
28360 73 6b 53 65 74 2c 20 53 65 6c 65 63 74 20 2a 70  skSet, Select *p
28361 53 29 7b 0a 20 20 42 69 74 6d 61 73 6b 20 6d 61  S){.  Bitmask ma
28362 73 6b 20 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28  sk = 0;.  while(
28363 20 70 53 20 29 7b 0a 20 20 20 20 6d 61 73 6b 20   pS ){.    mask 
28364 7c 3d 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65  |= exprListTable
28365 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20  Usage(pMaskSet, 
28366 70 53 2d 3e 70 45 4c 69 73 74 29 3b 0a 20 20 20  pS->pEList);.   
28367 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 4c 69 73   mask |= exprLis
28368 74 54 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73  tTableUsage(pMas
28369 6b 53 65 74 2c 20 70 53 2d 3e 70 47 72 6f 75 70  kSet, pS->pGroup
2836a 42 79 29 3b 0a 20 20 20 20 6d 61 73 6b 20 7c 3d  By);.    mask |=
2836b 20 65 78 70 72 4c 69 73 74 54 61 62 6c 65 55 73   exprListTableUs
2836c 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70 53  age(pMaskSet, pS
2836d 2d 3e 70 4f 72 64 65 72 42 79 29 3b 0a 20 20 20  ->pOrderBy);.   
2836e 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54 61 62   mask |= exprTab
2836f 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74  leUsage(pMaskSet
28370 2c 20 70 53 2d 3e 70 57 68 65 72 65 29 3b 0a 20  , pS->pWhere);. 
28371 20 20 20 6d 61 73 6b 20 7c 3d 20 65 78 70 72 54     mask |= exprT
28372 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
28373 65 74 2c 20 70 53 2d 3e 70 48 61 76 69 6e 67 29  et, pS->pHaving)
28374 3b 0a 20 20 20 20 70 53 20 3d 20 70 53 2d 3e 70  ;.    pS = pS->p
28375 50 72 69 6f 72 3b 0a 20 20 7d 0a 20 20 72 65 74  Prior;.  }.  ret
28376 75 72 6e 20 6d 61 73 6b 3b 0a 7d 0a 0a 2f 2a 0a  urn mask;.}../*.
28377 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
28378 66 20 74 68 65 20 67 69 76 65 6e 20 6f 70 65 72  f the given oper
28379 61 74 6f 72 20 69 73 20 6f 6e 65 20 6f 66 20 74  ator is one of t
2837a 68 65 20 6f 70 65 72 61 74 6f 72 73 20 74 68 61  he operators tha
2837b 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65 64 20  t is.** allowed 
2837c 66 6f 72 20 61 6e 20 69 6e 64 65 78 61 62 6c 65  for an indexable
2837d 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 65   WHERE clause te
2837e 72 6d 2e 20 20 54 68 65 20 61 6c 6c 6f 77 65 64  rm.  The allowed
2837f 20 6f 70 65 72 61 74 6f 72 73 20 61 72 65 0a 2a   operators are.*
28380 2a 20 22 3d 22 2c 20 22 3c 22 2c 20 22 3e 22 2c  * "=", "<", ">",
28381 20 22 3c 3d 22 2c 20 22 3e 3d 22 2c 20 61 6e 64   "<=", ">=", and
28382 20 22 49 4e 22 2e 0a 2a 2f 0a 73 74 61 74 69 63   "IN"..*/.static
28383 20 69 6e 74 20 61 6c 6c 6f 77 65 64 4f 70 28 69   int allowedOp(i
28384 6e 74 20 6f 70 29 7b 0a 20 20 61 73 73 65 72 74  nt op){.  assert
28385 28 20 54 4b 5f 47 54 3e 54 4b 5f 45 51 20 26 26  ( TK_GT>TK_EQ &&
28386 20 54 4b 5f 47 54 3c 54 4b 5f 47 45 20 29 3b 0a   TK_GT<TK_GE );.
28387 20 20 61 73 73 65 72 74 28 20 54 4b 5f 4c 54 3e    assert( TK_LT>
28388 54 4b 5f 45 51 20 26 26 20 54 4b 5f 4c 54 3c 54  TK_EQ && TK_LT<T
28389 4b 5f 47 45 20 29 3b 0a 20 20 61 73 73 65 72 74  K_GE );.  assert
2838a 28 20 54 4b 5f 4c 45 3e 54 4b 5f 45 51 20 26 26  ( TK_LE>TK_EQ &&
2838b 20 54 4b 5f 4c 45 3c 54 4b 5f 47 45 20 29 3b 0a   TK_LE<TK_GE );.
2838c 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 45 3d    assert( TK_GE=
2838d 3d 54 4b 5f 45 51 2b 34 20 29 3b 0a 20 20 72 65  =TK_EQ+4 );.  re
2838e 74 75 72 6e 20 6f 70 3d 3d 54 4b 5f 49 4e 20 7c  turn op==TK_IN |
2838f 7c 20 28 6f 70 3e 3d 54 4b 5f 45 51 20 26 26 20  | (op>=TK_EQ && 
28390 6f 70 3c 3d 54 4b 5f 47 45 29 20 7c 7c 20 6f 70  op<=TK_GE) || op
28391 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 3b 0a 7d 0a 0a  ==TK_ISNULL;.}..
28392 2f 2a 0a 2a 2a 20 53 77 61 70 20 74 77 6f 20 6f  /*.** Swap two o
28393 62 6a 65 63 74 73 20 6f 66 20 74 79 70 65 20 54  bjects of type T
28394 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 53 57 41  ..*/.#define SWA
28395 50 28 54 59 50 45 2c 41 2c 42 29 20 7b 54 59 50  P(TYPE,A,B) {TYP
28396 45 20 74 3d 41 3b 20 41 3d 42 3b 20 42 3d 74 3b  E t=A; A=B; B=t;
28397 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 6d 75 74 65  }../*.** Commute
28398 20 61 20 63 6f 6d 70 61 72 69 73 6f 6e 20 6f 70   a comparison op
28399 65 72 61 74 6f 72 2e 20 20 45 78 70 72 65 73 73  erator.  Express
2839a 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 6f 72 6d  ions of the form
2839b 20 22 58 20 6f 70 20 59 22 0a 2a 2a 20 61 72 65   "X op Y".** are
2839c 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74 6f 20   converted into 
2839d 22 59 20 6f 70 20 58 22 2e 0a 2a 2a 0a 2a 2a 20  "Y op X"..**.** 
2839e 49 66 20 61 20 63 6f 6c 6c 61 74 69 6f 6e 20 73  If a collation s
2839f 65 71 75 65 6e 63 65 20 69 73 20 61 73 73 6f 63  equence is assoc
283a0 69 61 74 65 64 20 77 69 74 68 20 65 69 74 68 65  iated with eithe
283a1 72 20 74 68 65 20 6c 65 66 74 20 6f 72 20 72 69  r the left or ri
283a2 67 68 74 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74  ght.** side of t
283a3 68 65 20 63 6f 6d 70 61 72 69 73 6f 6e 2c 20 69  he comparison, i
283a4 74 20 72 65 6d 61 69 6e 73 20 61 73 73 6f 63 69  t remains associ
283a5 61 74 65 64 20 77 69 74 68 20 74 68 65 20 73 61  ated with the sa
283a6 6d 65 20 73 69 64 65 20 61 66 74 65 72 0a 2a 2a  me side after.**
283a7 20 74 68 65 20 63 6f 6d 6d 75 74 61 74 69 6f 6e   the commutation
283a8 2e 20 53 6f 20 22 59 20 63 6f 6c 6c 61 74 65 20  . So "Y collate 
283a9 4e 4f 43 41 53 45 20 6f 70 20 58 22 20 62 65 63  NOCASE op X" bec
283aa 6f 6d 65 73 20 0a 2a 2a 20 22 58 20 63 6f 6c 6c  omes .** "X coll
283ab 61 74 65 20 4e 4f 43 41 53 45 20 6f 70 20 59 22  ate NOCASE op Y"
283ac 2e 20 54 68 69 73 20 69 73 20 62 65 63 61 75 73  . This is becaus
283ad 65 20 61 6e 79 20 63 6f 6c 6c 61 74 69 6f 6e 20  e any collation 
283ae 73 65 71 75 65 6e 63 65 20 6f 6e 0a 2a 2a 20 74  sequence on.** t
283af 68 65 20 6c 65 66 74 20 68 61 6e 64 20 73 69 64  he left hand sid
283b0 65 20 6f 66 20 61 20 63 6f 6d 70 61 72 69 73 6f  e of a compariso
283b1 6e 20 6f 76 65 72 72 69 64 65 73 20 61 6e 79 20  n overrides any 
283b2 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
283b3 63 65 20 0a 2a 2a 20 61 74 74 61 63 68 65 64 20  ce .** attached 
283b4 74 6f 20 74 68 65 20 72 69 67 68 74 2e 20 46 6f  to the right. Fo
283b5 72 20 74 68 65 20 73 61 6d 65 20 72 65 61 73 6f  r the same reaso
283b6 6e 20 74 68 65 20 45 50 5f 45 78 70 43 6f 6c 6c  n the EP_ExpColl
283b7 61 74 65 20 66 6c 61 67 0a 2a 2a 20 69 73 20 6e  ate flag.** is n
283b8 6f 74 20 63 6f 6d 6d 75 74 65 64 2e 0a 2a 2f 0a  ot commuted..*/.
283b9 73 74 61 74 69 63 20 76 6f 69 64 20 65 78 70 72  static void expr
283ba 43 6f 6d 6d 75 74 65 28 50 61 72 73 65 20 2a 70  Commute(Parse *p
283bb 50 61 72 73 65 2c 20 45 78 70 72 20 2a 70 45 78  Parse, Expr *pEx
283bc 70 72 29 7b 0a 20 20 75 31 36 20 65 78 70 52 69  pr){.  u16 expRi
283bd 67 68 74 20 3d 20 28 70 45 78 70 72 2d 3e 70 52  ght = (pExpr->pR
283be 69 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 45 50  ight->flags & EP
283bf 5f 45 78 70 43 6f 6c 6c 61 74 65 29 3b 0a 20 20  _ExpCollate);.  
283c0 75 31 36 20 65 78 70 4c 65 66 74 20 3d 20 28 70  u16 expLeft = (p
283c1 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66 6c 61  Expr->pLeft->fla
283c2 67 73 20 26 20 45 50 5f 45 78 70 43 6f 6c 6c 61  gs & EP_ExpColla
283c3 74 65 29 3b 0a 20 20 61 73 73 65 72 74 28 20 61  te);.  assert( a
283c4 6c 6c 6f 77 65 64 4f 70 28 70 45 78 70 72 2d 3e  llowedOp(pExpr->
283c5 6f 70 29 20 26 26 20 70 45 78 70 72 2d 3e 6f 70  op) && pExpr->op
283c6 21 3d 54 4b 5f 49 4e 20 29 3b 0a 20 20 70 45 78  !=TK_IN );.  pEx
283c7 70 72 2d 3e 70 52 69 67 68 74 2d 3e 70 43 6f 6c  pr->pRight->pCol
283c8 6c 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43  l = sqlite3ExprC
283c9 6f 6c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70  ollSeq(pParse, p
283ca 45 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20  Expr->pRight);. 
283cb 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 70   pExpr->pLeft->p
283cc 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33 45 78  Coll = sqlite3Ex
283cd 70 72 43 6f 6c 6c 53 65 71 28 70 50 61 72 73 65  prCollSeq(pParse
283ce 2c 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b  , pExpr->pLeft);
283cf 0a 20 20 53 57 41 50 28 43 6f 6c 6c 53 65 71 2a  .  SWAP(CollSeq*
283d0 2c 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e  ,pExpr->pRight->
283d1 70 43 6f 6c 6c 2c 70 45 78 70 72 2d 3e 70 4c 65  pColl,pExpr->pLe
283d2 66 74 2d 3e 70 43 6f 6c 6c 29 3b 0a 20 20 70 45  ft->pColl);.  pE
283d3 78 70 72 2d 3e 70 52 69 67 68 74 2d 3e 66 6c 61  xpr->pRight->fla
283d4 67 73 20 3d 20 28 70 45 78 70 72 2d 3e 70 52 69  gs = (pExpr->pRi
283d5 67 68 74 2d 3e 66 6c 61 67 73 20 26 20 7e 45 50  ght->flags & ~EP
283d6 5f 45 78 70 43 6f 6c 6c 61 74 65 29 20 7c 20 65  _ExpCollate) | e
283d7 78 70 4c 65 66 74 3b 0a 20 20 70 45 78 70 72 2d  xpLeft;.  pExpr-
283d8 3e 70 4c 65 66 74 2d 3e 66 6c 61 67 73 20 3d 20  >pLeft->flags = 
283d9 28 70 45 78 70 72 2d 3e 70 4c 65 66 74 2d 3e 66  (pExpr->pLeft->f
283da 6c 61 67 73 20 26 20 7e 45 50 5f 45 78 70 43 6f  lags & ~EP_ExpCo
283db 6c 6c 61 74 65 29 20 7c 20 65 78 70 52 69 67 68  llate) | expRigh
283dc 74 3b 0a 20 20 53 57 41 50 28 45 78 70 72 2a 2c  t;.  SWAP(Expr*,
283dd 70 45 78 70 72 2d 3e 70 52 69 67 68 74 2c 70 45  pExpr->pRight,pE
283de 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69  xpr->pLeft);.  i
283df 66 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d 54 4b  f( pExpr->op>=TK
283e0 5f 47 54 20 29 7b 0a 20 20 20 20 61 73 73 65 72  _GT ){.    asser
283e1 74 28 20 54 4b 5f 4c 54 3d 3d 54 4b 5f 47 54 2b  t( TK_LT==TK_GT+
283e2 32 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  2 );.    assert(
283e3 20 54 4b 5f 47 45 3d 3d 54 4b 5f 4c 45 2b 32 20   TK_GE==TK_LE+2 
283e4 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 54  );.    assert( T
283e5 4b 5f 47 54 3e 54 4b 5f 45 51 20 29 3b 0a 20 20  K_GT>TK_EQ );.  
283e6 20 20 61 73 73 65 72 74 28 20 54 4b 5f 47 54 3c    assert( TK_GT<
283e7 54 4b 5f 4c 45 20 29 3b 0a 20 20 20 20 61 73 73  TK_LE );.    ass
283e8 65 72 74 28 20 70 45 78 70 72 2d 3e 6f 70 3e 3d  ert( pExpr->op>=
283e9 54 4b 5f 47 54 20 26 26 20 70 45 78 70 72 2d 3e  TK_GT && pExpr->
283ea 6f 70 3c 3d 54 4b 5f 47 45 20 29 3b 0a 20 20 20  op<=TK_GE );.   
283eb 20 70 45 78 70 72 2d 3e 6f 70 20 3d 20 28 28 70   pExpr->op = ((p
283ec 45 78 70 72 2d 3e 6f 70 2d 54 4b 5f 47 54 29 5e  Expr->op-TK_GT)^
283ed 32 29 2b 54 4b 5f 47 54 3b 0a 20 20 7d 0a 7d 0a  2)+TK_GT;.  }.}.
283ee 0a 2f 2a 0a 2a 2a 20 54 72 61 6e 73 6c 61 74 65  ./*.** Translate
283ef 20 66 72 6f 6d 20 54 4b 5f 78 78 20 6f 70 65 72   from TK_xx oper
283f0 61 74 6f 72 20 74 6f 20 57 4f 5f 78 78 20 62 69  ator to WO_xx bi
283f1 74 6d 61 73 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63  tmask..*/.static
283f2 20 69 6e 74 20 6f 70 65 72 61 74 6f 72 4d 61 73   int operatorMas
283f3 6b 28 69 6e 74 20 6f 70 29 7b 0a 20 20 69 6e 74  k(int op){.  int
283f4 20 63 3b 0a 20 20 61 73 73 65 72 74 28 20 61 6c   c;.  assert( al
283f5 6c 6f 77 65 64 4f 70 28 6f 70 29 20 29 3b 0a 20  lowedOp(op) );. 
283f6 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49 4e 20 29   if( op==TK_IN )
283f7 7b 0a 20 20 20 20 63 20 3d 20 57 4f 5f 49 4e 3b  {.    c = WO_IN;
283f8 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6f 70 3d  .  }else if( op=
283f9 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29 7b 0a 20 20  =TK_ISNULL ){.  
283fa 20 20 63 20 3d 20 57 4f 5f 49 53 4e 55 4c 4c 3b    c = WO_ISNULL;
283fb 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 63 20  .  }else{.    c 
283fc 3d 20 57 4f 5f 45 51 3c 3c 28 6f 70 2d 54 4b 5f  = WO_EQ<<(op-TK_
283fd 45 51 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  EQ);.  }.  asser
283fe 74 28 20 6f 70 21 3d 54 4b 5f 49 53 4e 55 4c 4c  t( op!=TK_ISNULL
283ff 20 7c 7c 20 63 3d 3d 57 4f 5f 49 53 4e 55 4c 4c   || c==WO_ISNULL
28400 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70   );.  assert( op
28401 21 3d 54 4b 5f 49 4e 20 7c 7c 20 63 3d 3d 57 4f  !=TK_IN || c==WO
28402 5f 49 4e 20 29 3b 0a 20 20 61 73 73 65 72 74 28  _IN );.  assert(
28403 20 6f 70 21 3d 54 4b 5f 45 51 20 7c 7c 20 63 3d   op!=TK_EQ || c=
28404 3d 57 4f 5f 45 51 20 29 3b 0a 20 20 61 73 73 65  =WO_EQ );.  asse
28405 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 54 20 7c 7c  rt( op!=TK_LT ||
28406 20 63 3d 3d 57 4f 5f 4c 54 20 29 3b 0a 20 20 61   c==WO_LT );.  a
28407 73 73 65 72 74 28 20 6f 70 21 3d 54 4b 5f 4c 45  ssert( op!=TK_LE
28408 20 7c 7c 20 63 3d 3d 57 4f 5f 4c 45 20 29 3b 0a   || c==WO_LE );.
28409 20 20 61 73 73 65 72 74 28 20 6f 70 21 3d 54 4b    assert( op!=TK
2840a 5f 47 54 20 7c 7c 20 63 3d 3d 57 4f 5f 47 54 20  _GT || c==WO_GT 
2840b 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6f 70 21  );.  assert( op!
2840c 3d 54 4b 5f 47 45 20 7c 7c 20 63 3d 3d 57 4f 5f  =TK_GE || c==WO_
2840d 47 45 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 63  GE );.  return c
2840e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65 61 72 63  ;.}../*.** Searc
2840f 68 20 66 6f 72 20 61 20 74 65 72 6d 20 69 6e 20  h for a term in 
28410 74 68 65 20 57 48 45 52 45 20 63 6c 61 75 73 65  the WHERE clause
28411 20 74 68 61 74 20 69 73 20 6f 66 20 74 68 65 20   that is of the 
28412 66 6f 72 6d 20 22 58 20 3c 6f 70 3e 20 3c 65 78  form "X <op> <ex
28413 70 72 3e 22 0a 2a 2a 20 77 68 65 72 65 20 58 20  pr>".** where X 
28414 69 73 20 61 20 72 65 66 65 72 65 6e 63 65 20 74  is a reference t
28415 6f 20 74 68 65 20 69 43 6f 6c 75 6d 6e 20 6f 66  o the iColumn of
28416 20 74 61 62 6c 65 20 69 43 75 72 20 61 6e 64 20   table iCur and 
28417 3c 6f 70 3e 20 69 73 20 6f 6e 65 20 6f 66 0a 2a  <op> is one of.*
28418 2a 20 74 68 65 20 57 4f 5f 78 78 20 6f 70 65 72  * the WO_xx oper
28419 61 74 6f 72 20 63 6f 64 65 73 20 73 70 65 63 69  ator codes speci
2841a 66 69 65 64 20 62 79 20 74 68 65 20 6f 70 20 70  fied by the op p
2841b 61 72 61 6d 65 74 65 72 2e 0a 2a 2a 20 52 65 74  arameter..** Ret
2841c 75 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  urn a pointer to
2841d 20 74 68 65 20 74 65 72 6d 2e 20 20 52 65 74 75   the term.  Retu
2841e 72 6e 20 30 20 69 66 20 6e 6f 74 20 66 6f 75 6e  rn 0 if not foun
2841f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 57 68 65  d..*/.static Whe
28420 72 65 54 65 72 6d 20 2a 66 69 6e 64 54 65 72 6d  reTerm *findTerm
28421 28 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  (.  WhereClause 
28422 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68 65  *pWC,     /* The
28423 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74 6f   WHERE clause to
28424 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a   be searched */.
28425 20 20 69 6e 74 20 69 43 75 72 2c 20 20 20 20 20    int iCur,     
28426 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 73 6f          /* Curso
28427 72 20 6e 75 6d 62 65 72 20 6f 66 20 4c 48 53 20  r number of LHS 
28428 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
28429 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f  ,          /* Co
2842a 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 6f 66 20 4c  lumn number of L
2842b 48 53 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20  HS */.  Bitmask 
2842c 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 2f 2a  notReady,     /*
2842d 20 52 48 53 20 6d 75 73 74 20 6e 6f 74 20 6f 76   RHS must not ov
2842e 65 72 6c 61 70 20 77 69 74 68 20 74 68 69 73 20  erlap with this 
2842f 6d 61 73 6b 20 2a 2f 0a 20 20 75 31 36 20 6f 70  mask */.  u16 op
28430 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
28431 2f 2a 20 4d 61 73 6b 20 6f 66 20 57 4f 5f 78 78  /* Mask of WO_xx
28432 20 76 61 6c 75 65 73 20 64 65 73 63 72 69 62 69   values describi
28433 6e 67 20 6f 70 65 72 61 74 6f 72 20 2a 2f 0a 20  ng operator */. 
28434 20 49 6e 64 65 78 20 2a 70 49 64 78 20 20 20 20   Index *pIdx    
28435 20 20 20 20 20 20 20 2f 2a 20 4d 75 73 74 20 62         /* Must b
28436 65 20 63 6f 6d 70 61 74 69 62 6c 65 20 77 69 74  e compatible wit
28437 68 20 74 68 69 73 20 69 6e 64 65 78 2c 20 69 66  h this index, if
28438 20 6e 6f 74 20 4e 55 4c 4c 20 2a 2f 0a 29 7b 0a   not NULL */.){.
28439 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65    WhereTerm *pTe
2843a 72 6d 3b 0a 20 20 69 6e 74 20 6b 3b 0a 20 20 61  rm;.  int k;.  a
2843b 73 73 65 72 74 28 20 69 43 75 72 3e 3d 30 20 29  ssert( iCur>=0 )
2843c 3b 0a 20 20 66 6f 72 28 70 54 65 72 6d 3d 70 57  ;.  for(pTerm=pW
2843d 43 2d 3e 61 2c 20 6b 3d 70 57 43 2d 3e 6e 54 65  C->a, k=pWC->nTe
2843e 72 6d 3b 20 6b 3b 20 6b 2d 2d 2c 20 70 54 65 72  rm; k; k--, pTer
2843f 6d 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 70 54  m++){.    if( pT
28440 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d  erm->leftCursor=
28441 3d 69 43 75 72 0a 20 20 20 20 20 20 20 26 26 20  =iCur.       && 
28442 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69  (pTerm->prereqRi
28443 67 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d  ght & notReady)=
28444 3d 30 0a 20 20 20 20 20 20 20 26 26 20 70 54 65  =0.       && pTe
28445 72 6d 2d 3e 6c 65 66 74 43 6f 6c 75 6d 6e 3d 3d  rm->leftColumn==
28446 69 43 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 20 26  iColumn.       &
28447 26 20 28 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61  & (pTerm->eOpera
28448 74 6f 72 20 26 20 6f 70 29 21 3d 30 0a 20 20 20  tor & op)!=0.   
28449 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 49   ){.      if( pI
2844a 64 78 20 26 26 20 70 54 65 72 6d 2d 3e 65 4f 70  dx && pTerm->eOp
2844b 65 72 61 74 6f 72 21 3d 57 4f 5f 49 53 4e 55 4c  erator!=WO_ISNUL
2844c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  L ){.        Exp
2844d 72 20 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70  r *pX = pTerm->p
2844e 45 78 70 72 3b 0a 20 20 20 20 20 20 20 20 43 6f  Expr;.        Co
2844f 6c 6c 53 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20  llSeq *pColl;.  
28450 20 20 20 20 20 20 63 68 61 72 20 69 64 78 61 66        char idxaf
28451 66 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6a  f;.        int j
28452 3b 0a 20 20 20 20 20 20 20 20 50 61 72 73 65 20  ;.        Parse 
28453 2a 70 50 61 72 73 65 20 3d 20 70 57 43 2d 3e 70  *pParse = pWC->p
28454 50 61 72 73 65 3b 0a 0a 20 20 20 20 20 20 20 20  Parse;..        
28455 69 64 78 61 66 66 20 3d 20 70 49 64 78 2d 3e 70  idxaff = pIdx->p
28456 54 61 62 6c 65 2d 3e 61 43 6f 6c 5b 69 43 6f 6c  Table->aCol[iCol
28457 75 6d 6e 5d 2e 61 66 66 69 6e 69 74 79 3b 0a 20  umn].affinity;. 
28458 20 20 20 20 20 20 20 69 66 28 20 21 73 71 6c 69         if( !sqli
28459 74 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79  te3IndexAffinity
2845a 4f 6b 28 70 58 2c 20 69 64 78 61 66 66 29 20 29  Ok(pX, idxaff) )
2845b 20 63 6f 6e 74 69 6e 75 65 3b 0a 0a 20 20 20 20   continue;..    
2845c 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20 6f 75      /* Figure ou
2845d 74 20 74 68 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  t the collation 
2845e 73 65 71 75 65 6e 63 65 20 72 65 71 75 69 72 65  sequence require
2845f 64 20 66 72 6f 6d 20 61 6e 20 69 6e 64 65 78 20  d from an index 
28460 66 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  for.        ** i
28461 74 20 74 6f 20 62 65 20 75 73 65 66 75 6c 20 66  t to be useful f
28462 6f 72 20 6f 70 74 69 6d 69 73 69 6e 67 20 65 78  or optimising ex
28463 70 72 65 73 73 69 6f 6e 20 70 58 2e 20 53 74 6f  pression pX. Sto
28464 72 65 20 74 68 69 73 0a 20 20 20 20 20 20 20 20  re this.        
28465 2a 2a 20 76 61 6c 75 65 20 69 6e 20 76 61 72 69  ** value in vari
28466 61 62 6c 65 20 70 43 6f 6c 6c 2e 0a 20 20 20 20  able pColl..    
28467 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 61      */.        a
28468 73 73 65 72 74 28 70 58 2d 3e 70 4c 65 66 74 29  ssert(pX->pLeft)
28469 3b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c 20  ;.        pColl 
2846a 3d 20 73 71 6c 69 74 65 33 42 69 6e 61 72 79 43  = sqlite3BinaryC
2846b 6f 6d 70 61 72 65 43 6f 6c 6c 53 65 71 28 70 50  ompareCollSeq(pP
2846c 61 72 73 65 2c 20 70 58 2d 3e 70 4c 65 66 74 2c  arse, pX->pLeft,
2846d 20 70 58 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20   pX->pRight);.  
2846e 20 20 20 20 20 20 69 66 28 20 21 70 43 6f 6c 6c        if( !pColl
2846f 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 43   ){.          pC
28470 6f 6c 6c 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  oll = pParse->db
28471 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a 20 20 20  ->pDfltColl;.   
28472 20 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20       }..        
28473 66 6f 72 28 6a 3d 30 3b 20 70 49 64 78 2d 3e 61  for(j=0; pIdx->a
28474 69 43 6f 6c 75 6d 6e 5b 6a 5d 21 3d 69 43 6f 6c  iColumn[j]!=iCol
28475 75 6d 6e 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20  umn; j++){.     
28476 20 20 20 20 20 69 66 28 20 4e 45 56 45 52 28 6a       if( NEVER(j
28477 3e 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 29  >=pIdx->nColumn)
28478 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20   ) return 0;.   
28479 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69       }.        i
2847a 66 28 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d  f( sqlite3StrICm
2847b 70 28 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20  p(pColl->zName, 
2847c 70 49 64 78 2d 3e 61 7a 43 6f 6c 6c 5b 6a 5d 29  pIdx->azColl[j])
2847d 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2847e 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
2847f 6e 20 70 54 65 72 6d 3b 0a 20 20 20 20 7d 0a 20  n pTerm;.    }. 
28480 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
28481 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
28482 65 72 65 6e 63 65 20 2a 2f 0a 73 74 61 74 69 63  erence */.static
28483 20 76 6f 69 64 20 65 78 70 72 41 6e 61 6c 79 7a   void exprAnalyz
28484 65 28 53 72 63 4c 69 73 74 2a 2c 20 57 68 65 72  e(SrcList*, Wher
28485 65 43 6c 61 75 73 65 2a 2c 20 69 6e 74 29 3b 0a  eClause*, int);.
28486 0a 2f 2a 0a 2a 2a 20 43 61 6c 6c 20 65 78 70 72  ./*.** Call expr
28487 41 6e 61 6c 79 7a 65 20 6f 6e 20 61 6c 6c 20 74  Analyze on all t
28488 65 72 6d 73 20 69 6e 20 61 20 57 48 45 52 45 20  erms in a WHERE 
28489 63 6c 61 75 73 65 2e 20 20 0a 2a 2a 0a 2a 2a 0a  clause.  .**.**.
2848a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 65  */.static void e
2848b 78 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 0a 20  xprAnalyzeAll(. 
2848c 20 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69   SrcList *pTabLi
2848d 73 74 2c 20 20 20 20 20 20 20 2f 2a 20 74 68 65  st,       /* the
2848e 20 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a   FROM clause */.
2848f 20 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70    WhereClause *p
28490 57 43 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68  WC         /* th
28491 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 74  e WHERE clause t
28492 6f 20 62 65 20 61 6e 61 6c 79 7a 65 64 20 2a 2f  o be analyzed */
28493 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66  .){.  int i;.  f
28494 6f 72 28 69 3d 70 57 43 2d 3e 6e 54 65 72 6d 2d  or(i=pWC->nTerm-
28495 31 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20  1; i>=0; i--){. 
28496 20 20 20 65 78 70 72 41 6e 61 6c 79 7a 65 28 70     exprAnalyze(p
28497 54 61 62 4c 69 73 74 2c 20 70 57 43 2c 20 69 29  TabList, pWC, i)
28498 3b 0a 20 20 7d 0a 7d 0a 0a 23 69 66 6e 64 65 66  ;.  }.}..#ifndef
28499 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b   SQLITE_OMIT_LIK
2849a 45 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 0a 2f  E_OPTIMIZATION./
2849b 2a 0a 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65  *.** Check to se
2849c 65 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 65  e if the given e
2849d 78 70 72 65 73 73 69 6f 6e 20 69 73 20 61 20 4c  xpression is a L
2849e 49 4b 45 20 6f 72 20 47 4c 4f 42 20 6f 70 65 72  IKE or GLOB oper
2849f 61 74 6f 72 20 74 68 61 74 0a 2a 2a 20 63 61 6e  ator that.** can
284a0 20 62 65 20 6f 70 74 69 6d 69 7a 65 64 20 75 73   be optimized us
284a1 69 6e 67 20 69 6e 65 71 75 61 6c 69 74 79 20 63  ing inequality c
284a2 6f 6e 73 74 72 61 69 6e 74 73 2e 20 20 52 65 74  onstraints.  Ret
284a3 75 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69  urn TRUE if it i
284a4 73 0a 2a 2a 20 73 6f 20 61 6e 64 20 66 61 6c 73  s.** so and fals
284a5 65 20 69 66 20 6e 6f 74 2e 0a 2a 2a 0a 2a 2a 20  e if not..**.** 
284a6 49 6e 20 6f 72 64 65 72 20 66 6f 72 20 74 68 65  In order for the
284a7 20 6f 70 65 72 61 74 6f 72 20 74 6f 20 62 65 20   operator to be 
284a8 6f 70 74 69 6d 69 7a 69 62 6c 65 2c 20 74 68 65  optimizible, the
284a9 20 52 48 53 20 6d 75 73 74 20 62 65 20 61 20 73   RHS must be a s
284aa 74 72 69 6e 67 0a 2a 2a 20 6c 69 74 65 72 61 6c  tring.** literal
284ab 20 74 68 61 74 20 64 6f 65 73 20 6e 6f 74 20 62   that does not b
284ac 65 67 69 6e 20 77 69 74 68 20 61 20 77 69 6c 64  egin with a wild
284ad 63 61 72 64 2e 20 20 0a 2a 2f 0a 73 74 61 74 69  card.  .*/.stati
284ae 63 20 69 6e 74 20 69 73 4c 69 6b 65 4f 72 47 6c  c int isLikeOrGl
284af 6f 62 28 0a 20 20 50 61 72 73 65 20 2a 70 50 61  ob(.  Parse *pPa
284b0 72 73 65 2c 20 20 20 20 2f 2a 20 50 61 72 73 69  rse,    /* Parsi
284b1 6e 67 20 61 6e 64 20 63 6f 64 65 20 67 65 6e 65  ng and code gene
284b2 72 61 74 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a  rating context *
284b3 2f 0a 20 20 45 78 70 72 20 2a 70 45 78 70 72 2c  /.  Expr *pExpr,
284b4 20 20 20 20 20 20 2f 2a 20 54 65 73 74 20 74 68        /* Test th
284b5 69 73 20 65 78 70 72 65 73 73 69 6f 6e 20 2a 2f  is expression */
284b6 0a 20 20 69 6e 74 20 2a 70 6e 50 61 74 74 65 72  .  int *pnPatter
284b7 6e 2c 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f  n,   /* Number o
284b8 66 20 6e 6f 6e 2d 77 69 6c 64 63 61 72 64 20 70  f non-wildcard p
284b9 72 65 66 69 78 20 63 68 61 72 61 63 74 65 72 73  refix characters
284ba 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 73 43 6f   */.  int *pisCo
284bb 6d 70 6c 65 74 65 2c 20 2f 2a 20 54 72 75 65 20  mplete, /* True 
284bc 69 66 20 74 68 65 20 6f 6e 6c 79 20 77 69 6c 64  if the only wild
284bd 63 61 72 64 20 69 73 20 25 20 69 6e 20 74 68 65  card is % in the
284be 20 6c 61 73 74 20 63 68 61 72 61 63 74 65 72 20   last character 
284bf 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e 6f 43 61 73  */.  int *pnoCas
284c0 65 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  e      /* True i
284c1 66 20 75 70 70 65 72 63 61 73 65 20 69 73 20 65  f uppercase is e
284c2 71 75 69 76 61 6c 65 6e 74 20 74 6f 20 6c 6f 77  quivalent to low
284c3 65 72 63 61 73 65 20 2a 2f 0a 29 7b 0a 20 20 63  ercase */.){.  c
284c4 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20  onst char *z;.  
284c5 45 78 70 72 20 2a 70 52 69 67 68 74 2c 20 2a 70  Expr *pRight, *p
284c6 4c 65 66 74 3b 0a 20 20 45 78 70 72 4c 69 73 74  Left;.  ExprList
284c7 20 2a 70 4c 69 73 74 3b 0a 20 20 69 6e 74 20 63   *pList;.  int c
284c8 2c 20 63 6e 74 3b 0a 20 20 63 68 61 72 20 77 63  , cnt;.  char wc
284c9 5b 33 5d 3b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a  [3];.  CollSeq *
284ca 70 43 6f 6c 6c 3b 0a 20 20 73 71 6c 69 74 65 33  pColl;.  sqlite3
284cb 20 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64   *db = pParse->d
284cc 62 3b 0a 0a 20 20 69 66 28 20 21 73 71 6c 69 74  b;..  if( !sqlit
284cd 65 33 49 73 4c 69 6b 65 46 75 6e 63 74 69 6f 6e  e3IsLikeFunction
284ce 28 64 62 2c 20 70 45 78 70 72 2c 20 70 6e 6f 43  (db, pExpr, pnoC
284cf 61 73 65 2c 20 77 63 29 20 29 7b 0a 20 20 20 20  ase, wc) ){.    
284d0 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 23 69  return 0;.  }.#i
284d1 66 64 65 66 20 53 51 4c 49 54 45 5f 45 42 43 44  fdef SQLITE_EBCD
284d2 49 43 0a 20 20 69 66 28 20 2a 70 6e 6f 43 61 73  IC.  if( *pnoCas
284d3 65 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 23 65  e ) return 0;.#e
284d4 6e 64 69 66 0a 20 20 70 4c 69 73 74 20 3d 20 70  ndif.  pList = p
284d5 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20 70  Expr->pList;.  p
284d6 52 69 67 68 74 20 3d 20 70 4c 69 73 74 2d 3e 61  Right = pList->a
284d7 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 69 66 28  [0].pExpr;.  if(
284d8 20 70 52 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f   pRight->op!=TK_
284d9 53 54 52 49 4e 47 0a 20 20 20 26 26 20 28 70 52  STRING.   && (pR
284da 69 67 68 74 2d 3e 6f 70 21 3d 54 4b 5f 52 45 47  ight->op!=TK_REG
284db 49 53 54 45 52 20 7c 7c 20 70 52 69 67 68 74 2d  ISTER || pRight-
284dc 3e 69 43 6f 6c 75 6d 6e 21 3d 54 4b 5f 53 54 52  >iColumn!=TK_STR
284dd 49 4e 47 29 20 29 7b 0a 20 20 20 20 72 65 74 75  ING) ){.    retu
284de 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 70 4c 65 66  rn 0;.  }.  pLef
284df 74 20 3d 20 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e  t = pList->a[1].
284e0 70 45 78 70 72 3b 0a 20 20 69 66 28 20 70 4c 65  pExpr;.  if( pLe
284e1 66 74 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  ft->op!=TK_COLUM
284e2 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  N ){.    return 
284e3 30 3b 0a 20 20 7d 0a 20 20 70 43 6f 6c 6c 20 3d  0;.  }.  pColl =
284e4 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c 6c   sqlite3ExprColl
284e5 53 65 71 28 70 50 61 72 73 65 2c 20 70 4c 65 66  Seq(pParse, pLef
284e6 74 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 43  t);.  assert( pC
284e7 6f 6c 6c 21 3d 30 20 7c 7c 20 70 4c 65 66 74 2d  oll!=0 || pLeft-
284e8 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 20 29 3b 0a  >iColumn==-1 );.
284e9 20 20 69 66 28 20 70 43 6f 6c 6c 3d 3d 30 20 29    if( pColl==0 )
284ea 7b 0a 20 20 20 20 2f 2a 20 4e 6f 20 63 6f 6c 6c  {.    /* No coll
284eb 61 74 69 6f 6e 20 69 73 20 64 65 66 69 6e 65 64  ation is defined
284ec 20 66 6f 72 20 74 68 65 20 52 4f 57 49 44 2e 20   for the ROWID. 
284ed 20 55 73 65 20 74 68 65 20 64 65 66 61 75 6c 74   Use the default
284ee 2e 20 2a 2f 0a 20 20 20 20 70 43 6f 6c 6c 20 3d  . */.    pColl =
284ef 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 3b 0a   db->pDfltColl;.
284f0 20 20 7d 0a 20 20 69 66 28 20 28 70 43 6f 6c 6c    }.  if( (pColl
284f1 2d 3e 74 79 70 65 21 3d 53 51 4c 49 54 45 5f 43  ->type!=SQLITE_C
284f2 4f 4c 4c 5f 42 49 4e 41 52 59 20 7c 7c 20 2a 70  OLL_BINARY || *p
284f3 6e 6f 43 61 73 65 29 20 26 26 0a 20 20 20 20 20  noCase) &&.     
284f4 20 28 70 43 6f 6c 6c 2d 3e 74 79 70 65 21 3d 53   (pColl->type!=S
284f5 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53  QLITE_COLL_NOCAS
284f6 45 20 7c 7c 20 21 2a 70 6e 6f 43 61 73 65 29 20  E || !*pnoCase) 
284f7 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
284f8 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 44 65  .  }.  sqlite3De
284f9 71 75 6f 74 65 45 78 70 72 28 64 62 2c 20 70 52  quoteExpr(db, pR
284fa 69 67 68 74 29 3b 0a 20 20 7a 20 3d 20 28 63 68  ight);.  z = (ch
284fb 61 72 20 2a 29 70 52 69 67 68 74 2d 3e 74 6f 6b  ar *)pRight->tok
284fc 65 6e 2e 7a 3b 0a 20 20 63 6e 74 20 3d 20 30 3b  en.z;.  cnt = 0;
284fd 0a 20 20 69 66 28 20 7a 20 29 7b 0a 20 20 20 20  .  if( z ){.    
284fe 77 68 69 6c 65 28 20 28 63 3d 7a 5b 63 6e 74 5d  while( (c=z[cnt]
284ff 29 21 3d 30 20 26 26 20 63 21 3d 77 63 5b 30 5d  )!=0 && c!=wc[0]
28500 20 26 26 20 63 21 3d 77 63 5b 31 5d 20 26 26 20   && c!=wc[1] && 
28501 63 21 3d 77 63 5b 32 5d 20 29 7b 20 63 6e 74 2b  c!=wc[2] ){ cnt+
28502 2b 3b 20 7d 0a 20 20 7d 0a 20 20 69 66 28 20 63  +; }.  }.  if( c
28503 6e 74 3d 3d 30 20 7c 7c 20 32 35 35 3d 3d 28 75  nt==0 || 255==(u
28504 38 29 7a 5b 63 6e 74 5d 20 29 7b 0a 20 20 20 20  8)z[cnt] ){.    
28505 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
28506 2a 70 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 7a  *pisComplete = z
28507 5b 63 6e 74 5d 3d 3d 77 63 5b 30 5d 20 26 26 20  [cnt]==wc[0] && 
28508 7a 5b 63 6e 74 2b 31 5d 3d 3d 30 3b 0a 20 20 2a  z[cnt+1]==0;.  *
28509 70 6e 50 61 74 74 65 72 6e 20 3d 20 63 6e 74 3b  pnPattern = cnt;
2850a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23  .  return 1;.}.#
2850b 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2850c 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54 49 4d 49  OMIT_LIKE_OPTIMI
2850d 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 0a 23 69 66 6e  ZATION */...#ifn
2850e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2850f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a  VIRTUALTABLE./*.
28510 2a 2a 20 43 68 65 63 6b 20 74 6f 20 73 65 65 20  ** Check to see 
28511 69 66 20 74 68 65 20 67 69 76 65 6e 20 65 78 70  if the given exp
28512 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74 68  ression is of th
28513 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
28514 20 20 20 20 20 63 6f 6c 75 6d 6e 20 4d 41 54 43       column MATC
28515 48 20 65 78 70 72 0a 2a 2a 0a 2a 2a 20 49 66 20  H expr.**.** If 
28516 69 74 20 69 73 20 74 68 65 6e 20 72 65 74 75 72  it is then retur
28517 6e 20 54 52 55 45 2e 20 20 49 66 20 6e 6f 74 2c  n TRUE.  If not,
28518 20 72 65 74 75 72 6e 20 46 41 4c 53 45 2e 0a 2a   return FALSE..*
28519 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 73 4d  /.static int isM
2851a 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 0a 20 20  atchOfColumn(.  
2851b 45 78 70 72 20 2a 70 45 78 70 72 20 20 20 20 20  Expr *pExpr     
2851c 20 2f 2a 20 54 65 73 74 20 74 68 69 73 20 65 78   /* Test this ex
2851d 70 72 65 73 73 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  pression */.){. 
2851e 20 45 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74   ExprList *pList
2851f 3b 0a 0a 20 20 69 66 28 20 70 45 78 70 72 2d 3e  ;..  if( pExpr->
28520 6f 70 21 3d 54 4b 5f 46 55 4e 43 54 49 4f 4e 20  op!=TK_FUNCTION 
28521 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b  ){.    return 0;
28522 0a 20 20 7d 0a 20 20 69 66 28 20 70 45 78 70 72  .  }.  if( pExpr
28523 2d 3e 74 6f 6b 65 6e 2e 6e 21 3d 35 20 7c 7c 0a  ->token.n!=5 ||.
28524 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 53 74         sqlite3St
28525 72 4e 49 43 6d 70 28 28 63 6f 6e 73 74 20 63 68  rNICmp((const ch
28526 61 72 2a 29 70 45 78 70 72 2d 3e 74 6f 6b 65 6e  ar*)pExpr->token
28527 2e 7a 2c 22 6d 61 74 63 68 22 2c 35 29 21 3d 30  .z,"match",5)!=0
28528 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30   ){.    return 0
28529 3b 0a 20 20 7d 0a 20 20 70 4c 69 73 74 20 3d 20  ;.  }.  pList = 
2852a 70 45 78 70 72 2d 3e 70 4c 69 73 74 3b 0a 20 20  pExpr->pList;.  
2852b 69 66 28 20 70 4c 69 73 74 2d 3e 6e 45 78 70 72  if( pList->nExpr
2852c 21 3d 32 20 29 7b 0a 20 20 20 20 72 65 74 75 72  !=2 ){.    retur
2852d 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70  n 0;.  }.  if( p
2852e 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72  List->a[1].pExpr
2852f 2d 3e 6f 70 20 21 3d 20 54 4b 5f 43 4f 4c 55 4d  ->op != TK_COLUM
28530 4e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  N ){.    return 
28531 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  0;.  }.  return 
28532 31 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  1;.}.#endif /* S
28533 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55  QLITE_OMIT_VIRTU
28534 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 2f 2a 0a 2a  ALTABLE */../*.*
28535 2a 20 49 66 20 74 68 65 20 70 42 61 73 65 20 65  * If the pBase e
28536 78 70 72 65 73 73 69 6f 6e 20 6f 72 69 67 69 6e  xpression origin
28537 61 74 65 64 20 69 6e 20 74 68 65 20 4f 4e 20 6f  ated in the ON o
28538 72 20 55 53 49 4e 47 20 63 6c 61 75 73 65 20 6f  r USING clause o
28539 66 0a 2a 2a 20 61 20 6a 6f 69 6e 2c 20 74 68 65  f.** a join, the
2853a 6e 20 74 72 61 6e 73 66 65 72 20 74 68 65 20 61  n transfer the a
2853b 70 70 72 6f 70 72 69 61 74 65 20 6d 61 72 6b 69  ppropriate marki
2853c 6e 67 73 20 6f 76 65 72 20 74 6f 20 64 65 72 69  ngs over to deri
2853d 76 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ved..*/.static v
2853e 6f 69 64 20 74 72 61 6e 73 66 65 72 4a 6f 69 6e  oid transferJoin
2853f 4d 61 72 6b 69 6e 67 73 28 45 78 70 72 20 2a 70  Markings(Expr *p
28540 44 65 72 69 76 65 64 2c 20 45 78 70 72 20 2a 70  Derived, Expr *p
28541 42 61 73 65 29 7b 0a 20 20 70 44 65 72 69 76 65  Base){.  pDerive
28542 64 2d 3e 66 6c 61 67 73 20 7c 3d 20 70 42 61 73  d->flags |= pBas
28543 65 2d 3e 66 6c 61 67 73 20 26 20 45 50 5f 46 72  e->flags & EP_Fr
28544 6f 6d 4a 6f 69 6e 3b 0a 20 20 70 44 65 72 69 76  omJoin;.  pDeriv
28545 65 64 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61  ed->iRightJoinTa
28546 62 6c 65 20 3d 20 70 42 61 73 65 2d 3e 69 52 69  ble = pBase->iRi
28547 67 68 74 4a 6f 69 6e 54 61 62 6c 65 3b 0a 7d 0a  ghtJoinTable;.}.
28548 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53 51  .#if !defined(SQ
28549 4c 49 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54  LITE_OMIT_OR_OPT
2854a 49 4d 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64  IMIZATION) && !d
2854b 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d  efined(SQLITE_OM
2854c 49 54 5f 53 55 42 51 55 45 52 59 29 0a 2f 2a 0a  IT_SUBQUERY)./*.
2854d 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
2854e 66 20 74 68 65 20 67 69 76 65 6e 20 74 65 72 6d  f the given term
2854f 20 6f 66 20 61 6e 20 4f 52 20 63 6c 61 75 73 65   of an OR clause
28550 20 63 61 6e 20 62 65 20 63 6f 6e 76 65 72 74 65   can be converte
28551 64 0a 2a 2a 20 69 6e 74 6f 20 61 6e 20 49 4e 20  d.** into an IN 
28552 63 6c 61 75 73 65 2e 20 20 54 68 65 20 69 43 75  clause.  The iCu
28553 72 73 6f 72 20 61 6e 64 20 69 43 6f 6c 75 6d 6e  rsor and iColumn
28554 20 64 65 66 69 6e 65 20 74 68 65 20 6c 65 66 74   define the left
28555 2d 68 61 6e 64 0a 2a 2a 20 73 69 64 65 20 6f 66  -hand.** side of
28556 20 74 68 65 20 49 4e 20 63 6c 61 75 73 65 2e 0a   the IN clause..
28557 2a 2a 0a 2a 2a 20 54 68 65 20 63 6f 6e 74 65 78  **.** The contex
28558 74 20 69 73 20 74 68 61 74 20 77 65 20 68 61 76  t is that we hav
28559 65 20 6d 75 6c 74 69 70 6c 65 20 4f 52 2d 63 6f  e multiple OR-co
2855a 6e 6e 65 63 74 65 64 20 65 71 75 61 6c 69 74 79  nnected equality
2855b 20 74 65 72 6d 73 0a 2a 2a 20 6c 69 6b 65 20 74   terms.** like t
2855c 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20  his:.**.**      
2855d 20 20 20 20 20 61 3d 3c 65 78 70 72 31 3e 20 4f       a=<expr1> O
2855e 52 20 20 61 3d 3c 65 78 70 72 32 3e 20 4f 52 20  R  a=<expr2> OR 
2855f 62 3d 3c 65 78 70 72 33 3e 20 20 4f 52 20 2e 2e  b=<expr3>  OR ..
28560 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 4f 72 54  ..**.** The pOrT
28561 65 72 6d 20 69 6e 70 75 74 20 74 6f 20 74 68 69  erm input to thi
28562 73 20 72 6f 75 74 69 6e 65 20 63 6f 72 72 65 73  s routine corres
28563 70 6f 6e 64 73 20 74 6f 20 61 20 73 69 6e 67 6c  ponds to a singl
28564 65 20 74 65 72 6d 20 6f 66 0a 2a 2a 20 74 68 69  e term of.** thi
28565 73 20 4f 52 20 63 6c 61 75 73 65 2e 20 20 49 6e  s OR clause.  In
28566 20 6f 72 64 65 72 20 66 6f 72 20 74 68 65 20 74   order for the t
28567 65 72 6d 20 74 6f 20 62 65 20 61 20 63 61 6e 64  erm to be a cand
28568 69 64 61 74 65 20 66 6f 72 0a 2a 2a 20 63 6f 6e  idate for.** con
28569 76 65 72 73 69 6f 6e 20 74 6f 20 61 6e 20 49 4e  version to an IN
2856a 20 6f 70 65 72 61 74 6f 72 2c 20 74 68 65 20 66   operator, the f
2856b 6f 6c 6c 6f 77 69 6e 67 20 6d 75 73 74 20 62 65  ollowing must be
2856c 20 74 72 75 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 20   true:.**.**    
2856d 20 2a 20 20 54 68 65 20 6c 65 66 74 2d 68 61 6e   *  The left-han
2856e 64 20 73 69 64 65 20 6f 66 20 74 68 65 20 74 65  d side of the te
2856f 72 6d 20 6d 75 73 74 20 62 65 20 74 68 65 20 63  rm must be the c
28570 6f 6c 75 6d 6e 20 77 68 69 63 68 0a 2a 2a 20 20  olumn which.**  
28571 20 20 20 20 20 20 69 73 20 69 64 65 6e 74 69 66        is identif
28572 69 65 64 20 62 79 20 69 43 75 72 73 6f 72 20 61  ied by iCursor a
28573 6e 64 20 69 43 6f 6c 75 6d 6e 2e 0a 2a 2a 0a 2a  nd iColumn..**.*
28574 2a 20 20 20 20 20 2a 20 20 49 66 20 74 68 65 20  *     *  If the 
28575 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64 65 20  right-hand side 
28576 69 73 20 61 6c 73 6f 20 61 20 63 6f 6c 75 6d 6e  is also a column
28577 2c 20 74 68 65 6e 20 74 68 65 20 61 66 66 69 6e  , then the affin
28578 69 74 69 65 73 0a 2a 2a 20 20 20 20 20 20 20 20  ities.**        
28579 6f 66 20 62 6f 74 68 20 72 69 67 68 74 20 61 6e  of both right an
2857a 64 20 6c 65 66 74 20 73 69 64 65 73 20 6d 75 73  d left sides mus
2857b 74 20 62 65 20 73 75 63 68 20 74 68 61 74 20 6e  t be such that n
2857c 6f 20 74 79 70 65 0a 2a 2a 20 20 20 20 20 20 20  o type.**       
2857d 20 63 6f 6e 76 65 72 73 69 6f 6e 73 20 61 72 65   conversions are
2857e 20 72 65 71 75 69 72 65 64 20 6f 6e 20 74 68 65   required on the
2857f 20 72 69 67 68 74 2e 20 20 28 54 69 63 6b 65 74   right.  (Ticket
28580 20 23 32 32 34 39 29 0a 2a 2a 0a 2a 2a 20 49 66   #2249).**.** If
28581 20 62 6f 74 68 20 6f 66 20 74 68 65 73 65 20 63   both of these c
28582 6f 6e 64 69 74 69 6f 6e 73 20 61 72 65 20 74 72  onditions are tr
28583 75 65 2c 20 74 68 65 6e 20 72 65 74 75 72 6e 20  ue, then return 
28584 74 72 75 65 2e 20 20 4f 74 68 65 72 77 69 73 65  true.  Otherwise
28585 0a 2a 2a 20 72 65 74 75 72 6e 20 66 61 6c 73 65  .** return false
28586 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
28587 6f 72 54 65 72 6d 49 73 4f 70 74 43 61 6e 64 69  orTermIsOptCandi
28588 64 61 74 65 28 57 68 65 72 65 54 65 72 6d 20 2a  date(WhereTerm *
28589 70 4f 72 54 65 72 6d 2c 20 69 6e 74 20 69 43 75  pOrTerm, int iCu
2858a 72 73 6f 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d  rsor, int iColum
2858b 6e 29 7b 0a 20 20 69 6e 74 20 61 66 66 4c 65 66  n){.  int affLef
2858c 74 2c 20 61 66 66 52 69 67 68 74 3b 0a 20 20 61  t, affRight;.  a
2858d 73 73 65 72 74 28 20 70 4f 72 54 65 72 6d 2d 3e  ssert( pOrTerm->
2858e 65 4f 70 65 72 61 74 6f 72 3d 3d 57 4f 5f 45 51  eOperator==WO_EQ
2858f 20 29 3b 0a 20 20 69 66 28 20 70 4f 72 54 65 72   );.  if( pOrTer
28590 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 21 3d 69  m->leftCursor!=i
28591 43 75 72 73 6f 72 20 29 7b 0a 20 20 20 20 72 65  Cursor ){.    re
28592 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 69 66  turn 0;.  }.  if
28593 28 20 70 4f 72 54 65 72 6d 2d 3e 6c 65 66 74 43  ( pOrTerm->leftC
28594 6f 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 29  olumn!=iColumn )
28595 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a  {.    return 0;.
28596 20 20 7d 0a 20 20 61 66 66 52 69 67 68 74 20 3d    }.  affRight =
28597 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
28598 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45  nity(pOrTerm->pE
28599 78 70 72 2d 3e 70 52 69 67 68 74 29 3b 0a 20 20  xpr->pRight);.  
2859a 69 66 28 20 61 66 66 52 69 67 68 74 3d 3d 30 20  if( affRight==0 
2859b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b  ){.    return 1;
2859c 0a 20 20 7d 0a 20 20 61 66 66 4c 65 66 74 20 3d  .  }.  affLeft =
2859d 20 73 71 6c 69 74 65 33 45 78 70 72 41 66 66 69   sqlite3ExprAffi
2859e 6e 69 74 79 28 70 4f 72 54 65 72 6d 2d 3e 70 45  nity(pOrTerm->pE
2859f 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a 20 20 69  xpr->pLeft);.  i
285a0 66 28 20 61 66 66 52 69 67 68 74 21 3d 61 66 66  f( affRight!=aff
285a1 4c 65 66 74 20 29 7b 0a 20 20 20 20 72 65 74 75  Left ){.    retu
285a2 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 0;.  }.  retu
285a3 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52  rn 1;.}../*.** R
285a4 65 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68  eturn true if th
285a5 65 20 67 69 76 65 6e 20 74 65 72 6d 20 6f 66 20  e given term of 
285a6 61 6e 20 4f 52 20 63 6c 61 75 73 65 20 63 61 6e  an OR clause can
285a7 20 62 65 20 69 67 6e 6f 72 65 64 20 64 75 72 69   be ignored duri
285a8 6e 67 0a 2a 2a 20 61 20 63 68 65 63 6b 20 74 6f  ng.** a check to
285a9 20 6d 61 6b 65 20 73 75 72 65 20 61 6c 6c 20 4f   make sure all O
285aa 52 20 74 65 72 6d 73 20 61 72 65 20 63 61 6e 64  R terms are cand
285ab 69 64 61 74 65 73 20 66 6f 72 20 6f 70 74 69 6d  idates for optim
285ac 69 7a 61 74 69 6f 6e 2e 0a 2a 2a 20 49 6e 20 6f  ization..** In o
285ad 74 68 65 72 20 77 6f 72 64 73 2c 20 72 65 74 75  ther words, retu
285ae 72 6e 20 74 72 75 65 20 69 66 20 61 20 63 61 6c  rn true if a cal
285af 6c 20 74 6f 20 74 68 65 20 6f 72 54 65 72 6d 49  l to the orTermI
285b0 73 4f 70 74 43 61 6e 64 69 64 61 74 65 28 29 0a  sOptCandidate().
285b1 2a 2a 20 61 62 6f 76 65 20 72 65 74 75 72 6e 65  ** above returne
285b2 64 20 66 61 6c 73 65 20 62 75 74 20 69 74 20 69  d false but it i
285b3 73 20 6e 6f 74 20 6e 65 63 65 73 73 61 72 79 20  s not necessary 
285b4 74 6f 20 64 69 73 71 75 61 6c 69 66 79 20 74 68  to disqualify th
285b5 65 0a 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f  e.** optimizatio
285b6 6e 2e 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65  n..**.** Suppose
285b7 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 4f 52   the original OR
285b8 20 70 68 72 61 73 65 20 77 61 73 20 74 68 69 73   phrase was this
285b9 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20 20  :.**.**         
285ba 20 20 61 3d 34 20 20 4f 52 20 20 61 3d 31 31 20    a=4  OR  a=11 
285bb 20 4f 52 20 20 61 3d 62 0a 2a 2a 0a 2a 2a 20 44   OR  a=b.**.** D
285bc 75 72 69 6e 67 20 61 6e 61 6c 79 73 69 73 2c 20  uring analysis, 
285bd 74 68 65 20 74 68 69 72 64 20 74 65 72 6d 20 67  the third term g
285be 65 74 73 20 66 6c 69 70 70 65 64 20 61 72 6f 75  ets flipped arou
285bf 6e 64 20 61 6e 64 20 64 75 70 6c 69 63 61 74 65  nd and duplicate
285c0 0a 2a 2a 20 73 6f 20 74 68 61 74 20 77 65 20 61  .** so that we a
285c1 72 65 20 6c 65 66 74 20 77 69 74 68 20 74 68 69  re left with thi
285c2 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 20  s:.**.**        
285c3 20 20 20 61 3d 34 20 20 4f 52 20 20 61 3d 31 31     a=4  OR  a=11
285c4 20 20 4f 52 20 20 61 3d 62 20 20 4f 52 20 20 62    OR  a=b  OR  b
285c5 3d 61 0a 2a 2a 0a 2a 2a 20 53 69 6e 63 65 20 74  =a.**.** Since t
285c6 68 65 20 6c 61 73 74 20 74 77 6f 20 74 65 72 6d  he last two term
285c7 73 20 61 72 65 20 64 75 70 6c 69 63 61 74 65 73  s are duplicates
285c8 2c 20 6f 6e 6c 79 20 6f 6e 65 20 6f 66 20 74 68  , only one of th
285c9 65 6d 0a 2a 2a 20 68 61 73 20 74 6f 20 71 75 61  em.** has to qua
285ca 6c 69 66 79 20 69 6e 20 6f 72 64 65 72 20 66 6f  lify in order fo
285cb 72 20 74 68 65 20 77 68 6f 6c 65 20 70 68 72 61  r the whole phra
285cc 73 65 20 74 6f 20 71 75 61 6c 69 66 79 2e 20 20  se to qualify.  
285cd 57 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75  When.** this rou
285ce 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 2c 20  tine is called, 
285cf 77 65 20 6b 6e 6f 77 20 74 68 61 74 20 70 4f 72  we know that pOr
285d0 54 65 72 6d 20 64 69 64 20 6e 6f 74 20 71 75 61  Term did not qua
285d1 6c 69 66 79 2e 0a 2a 2a 20 54 68 69 73 20 72 6f  lify..** This ro
285d2 75 74 69 6e 65 20 6d 65 72 65 6c 79 20 63 68 65  utine merely che
285d3 63 6b 73 20 74 6f 20 73 65 65 20 69 66 20 70 4f  cks to see if pO
285d4 72 54 65 72 6d 20 68 61 73 20 61 20 64 75 70 6c  rTerm has a dupl
285d5 69 63 61 74 65 20 74 68 61 74 0a 2a 2a 20 6d 69  icate that.** mi
285d6 67 68 74 20 71 75 61 6c 69 66 79 2e 20 20 49 66  ght qualify.  If
285d7 20 74 68 65 72 65 20 69 73 20 61 20 64 75 70 6c   there is a dupl
285d8 69 63 61 74 65 20 74 68 61 74 20 68 61 73 20 6e  icate that has n
285d9 6f 74 20 79 65 74 20 62 65 65 6e 0a 2a 2a 20 64  ot yet been.** d
285da 69 73 71 75 61 6c 69 66 69 65 64 2c 20 74 68 65  isqualified, the
285db 6e 20 72 65 74 75 72 6e 20 74 72 75 65 2e 20 20  n return true.  
285dc 49 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20  If there are no 
285dd 64 75 70 6c 69 63 61 74 65 73 2c 20 6f 72 0a 2a  duplicates, or.*
285de 2a 20 74 68 65 20 64 75 70 6c 69 63 61 74 65 20  * the duplicate 
285df 68 61 73 20 61 6c 73 6f 20 62 65 65 6e 20 64 69  has also been di
285e0 73 71 75 61 6c 69 66 69 65 64 2c 20 72 65 74 75  squalified, retu
285e1 72 6e 20 66 61 6c 73 65 2e 0a 2a 2f 0a 73 74 61  rn false..*/.sta
285e2 74 69 63 20 69 6e 74 20 6f 72 54 65 72 6d 48 61  tic int orTermHa
285e3 73 4f 6b 44 75 70 6c 69 63 61 74 65 28 57 68 65  sOkDuplicate(Whe
285e4 72 65 43 6c 61 75 73 65 20 2a 70 4f 72 2c 20 57  reClause *pOr, W
285e5 68 65 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72  hereTerm *pOrTer
285e6 6d 29 7b 0a 20 20 69 66 28 20 70 4f 72 54 65 72  m){.  if( pOrTer
285e7 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45 52 4d 5f  m->flags & TERM_
285e8 43 4f 50 49 45 44 20 29 7b 0a 20 20 20 20 2f 2a  COPIED ){.    /*
285e9 20 54 68 69 73 20 69 73 20 74 68 65 20 6f 72 69   This is the ori
285ea 67 69 6e 61 6c 20 74 65 72 6d 2e 20 20 54 68 65  ginal term.  The
285eb 20 64 75 70 6c 69 63 61 74 65 20 69 73 20 74 6f   duplicate is to
285ec 20 74 68 65 20 6c 65 66 74 20 68 61 64 0a 20 20   the left had.  
285ed 20 20 2a 2a 20 68 61 73 20 6e 6f 74 20 79 65 74    ** has not yet
285ee 20 62 65 65 6e 20 61 6e 61 6c 79 7a 65 64 20 61   been analyzed a
285ef 6e 64 20 74 68 75 73 20 68 61 73 20 6e 6f 74 20  nd thus has not 
285f0 79 65 74 20 62 65 65 6e 20 64 69 73 71 75 61 6c  yet been disqual
285f1 69 66 69 65 64 2e 20 2a 2f 0a 20 20 20 20 72 65  ified. */.    re
285f2 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 69 66  turn 1;.  }.  if
285f3 28 20 28 70 4f 72 54 65 72 6d 2d 3e 66 6c 61 67  ( (pOrTerm->flag
285f4 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  s & TERM_VIRTUAL
285f5 29 21 3d 30 0a 20 20 20 20 20 26 26 20 28 70 4f  )!=0.     && (pO
285f6 72 2d 3e 61 5b 70 4f 72 54 65 72 6d 2d 3e 69 50  r->a[pOrTerm->iP
285f7 61 72 65 6e 74 5d 2e 66 6c 61 67 73 20 26 20 54  arent].flags & T
285f8 45 52 4d 5f 4f 52 5f 4f 4b 29 21 3d 30 20 29 7b  ERM_OR_OK)!=0 ){
285f9 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 69 73 20  .    /* This is 
285fa 61 20 64 75 70 6c 69 63 61 74 65 20 74 65 72 6d  a duplicate term
285fb 2e 20 20 54 68 65 20 6f 72 69 67 69 6e 61 6c 20  .  The original 
285fc 71 75 61 6c 69 66 69 65 64 20 73 6f 20 74 68 69  qualified so thi
285fd 73 20 6f 6e 65 0a 20 20 20 20 2a 2a 20 64 6f 65  s one.    ** doe
285fe 73 20 6e 6f 74 20 68 61 76 65 20 74 6f 2e 20 2a  s not have to. *
285ff 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a  /.    return 1;.
28600 20 20 7d 0a 20 20 2f 2a 20 54 68 69 73 20 69 73    }.  /* This is
28601 20 65 69 74 68 65 72 20 61 20 73 69 6e 67 6c 65   either a single
28602 74 6f 6e 20 74 65 72 6d 20 6f 72 20 65 6c 73 65  ton term or else
28603 20 69 74 20 69 73 20 61 20 64 75 70 6c 69 63 61   it is a duplica
28604 74 65 20 66 6f 72 0a 20 20 2a 2a 20 77 68 69 63  te for.  ** whic
28605 68 20 74 68 65 20 6f 72 69 67 69 6e 61 6c 20 64  h the original d
28606 69 64 20 6e 6f 74 20 71 75 61 6c 69 66 79 2e 20  id not qualify. 
28607 20 45 69 74 68 65 72 20 77 61 79 20 77 65 20 61   Either way we a
28608 72 65 20 64 6f 6e 65 20 66 6f 72 2e 20 2a 2f 0a  re done for. */.
28609 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 23 65    return 0;.}.#e
2860a 6e 64 69 66 20 2f 2a 20 21 53 51 4c 49 54 45 5f  ndif /* !SQLITE_
2860b 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d 49 5a 41  OMIT_OR_OPTIMIZA
2860c 54 49 4f 4e 20 26 26 20 21 53 51 4c 49 54 45 5f  TION && !SQLITE_
2860d 4f 4d 49 54 5f 53 55 42 51 55 45 52 59 20 2a 2f  OMIT_SUBQUERY */
2860e 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ../*.** The inpu
2860f 74 20 74 6f 20 74 68 69 73 20 72 6f 75 74 69 6e  t to this routin
28610 65 20 69 73 20 61 6e 20 57 68 65 72 65 54 65 72  e is an WhereTer
28611 6d 20 73 74 72 75 63 74 75 72 65 20 77 69 74 68  m structure with
28612 20 6f 6e 6c 79 20 74 68 65 0a 2a 2a 20 22 70 45   only the.** "pE
28613 78 70 72 22 20 66 69 65 6c 64 20 66 69 6c 6c 65  xpr" field fille
28614 64 20 69 6e 2e 20 20 54 68 65 20 6a 6f 62 20 6f  d in.  The job o
28615 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  f this routine i
28616 73 20 74 6f 20 61 6e 61 6c 79 7a 65 20 74 68 65  s to analyze the
28617 0a 2a 2a 20 73 75 62 65 78 70 72 65 73 73 69 6f  .** subexpressio
28618 6e 20 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 61  n and populate a
28619 6c 6c 20 74 68 65 20 6f 74 68 65 72 20 66 69 65  ll the other fie
2861a 6c 64 73 20 6f 66 20 74 68 65 20 57 68 65 72 65  lds of the Where
2861b 54 65 72 6d 0a 2a 2a 20 73 74 72 75 63 74 75 72  Term.** structur
2861c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  e..**.** If the 
2861d 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66  expression is of
2861e 20 74 68 65 20 66 6f 72 6d 20 22 3c 65 78 70 72   the form "<expr
2861f 3e 20 3c 6f 70 3e 20 58 22 20 69 74 20 67 65 74  > <op> X" it get
28620 73 20 63 6f 6d 6d 75 74 65 64 0a 2a 2a 20 74 6f  s commuted.** to
28621 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 66 6f   the standard fo
28622 72 6d 20 6f 66 20 22 58 20 3c 6f 70 3e 20 3c 65  rm of "X <op> <e
28623 78 70 72 3e 22 2e 20 20 49 66 20 74 68 65 20 65  xpr>".  If the e
28624 78 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 0a  xpression is of.
28625 2a 2a 20 74 68 65 20 66 6f 72 6d 20 22 58 20 3c  ** the form "X <
28626 6f 70 3e 20 59 22 20 77 68 65 72 65 20 62 6f 74  op> Y" where bot
28627 68 20 58 20 61 6e 64 20 59 20 61 72 65 20 63 6f  h X and Y are co
28628 6c 75 6d 6e 73 2c 20 74 68 65 6e 20 74 68 65 20  lumns, then the 
28629 6f 72 69 67 69 6e 61 6c 0a 2a 2a 20 65 78 70 72  original.** expr
2862a 65 73 73 69 6f 6e 20 69 73 20 75 6e 63 68 61 6e  ession is unchan
2862b 67 65 64 20 61 6e 64 20 61 20 6e 65 77 20 76 69  ged and a new vi
2862c 72 74 75 61 6c 20 65 78 70 72 65 73 73 69 6f 6e  rtual expression
2862d 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 2a 2a 20   of the form.** 
2862e 22 59 20 3c 6f 70 3e 20 58 22 20 69 73 20 61 64  "Y <op> X" is ad
2862f 64 65 64 20 74 6f 20 74 68 65 20 57 48 45 52 45  ded to the WHERE
28630 20 63 6c 61 75 73 65 20 61 6e 64 20 61 6e 61 6c   clause and anal
28631 79 7a 65 64 20 73 65 70 61 72 61 74 65 6c 79 2e  yzed separately.
28632 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
28633 65 78 70 72 41 6e 61 6c 79 7a 65 28 0a 20 20 53  exprAnalyze(.  S
28634 72 63 4c 69 73 74 20 2a 70 53 72 63 2c 20 20 20  rcList *pSrc,   
28635 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20           /* the 
28636 46 52 4f 4d 20 63 6c 61 75 73 65 20 2a 2f 0a 20  FROM clause */. 
28637 20 57 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57   WhereClause *pW
28638 43 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68  C,         /* th
28639 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
2863a 2f 0a 20 20 69 6e 74 20 69 64 78 54 65 72 6d 20  /.  int idxTerm 
2863b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2863c 20 49 6e 64 65 78 20 6f 66 20 74 68 65 20 74 65   Index of the te
2863d 72 6d 20 74 6f 20 62 65 20 61 6e 61 6c 79 7a 65  rm to be analyze
2863e 64 20 2a 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54  d */.){.  WhereT
2863f 65 72 6d 20 2a 70 54 65 72 6d 3b 0a 20 20 45 78  erm *pTerm;.  Ex
28640 70 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b  prMaskSet *pMask
28641 53 65 74 3b 0a 20 20 45 78 70 72 20 2a 70 45 78  Set;.  Expr *pEx
28642 70 72 3b 0a 20 20 42 69 74 6d 61 73 6b 20 70 72  pr;.  Bitmask pr
28643 65 72 65 71 4c 65 66 74 3b 0a 20 20 42 69 74 6d  ereqLeft;.  Bitm
28644 61 73 6b 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20  ask prereqAll;. 
28645 20 42 69 74 6d 61 73 6b 20 65 78 74 72 61 52 69   Bitmask extraRi
28646 67 68 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e  ght = 0;.  int n
28647 50 61 74 74 65 72 6e 3b 0a 20 20 69 6e 74 20 69  Pattern;.  int i
28648 73 43 6f 6d 70 6c 65 74 65 3b 0a 20 20 69 6e 74  sComplete;.  int
28649 20 6e 6f 43 61 73 65 3b 0a 20 20 69 6e 74 20 6f   noCase;.  int o
2864a 70 3b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72  p;.  Parse *pPar
2864b 73 65 20 3d 20 70 57 43 2d 3e 70 50 61 72 73 65  se = pWC->pParse
2864c 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20  ;.  sqlite3 *db 
2864d 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20  = pParse->db;.. 
2864e 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   if( db->mallocF
2864f 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 72 65 74  ailed ){.    ret
28650 75 72 6e 3b 0a 20 20 7d 0a 20 20 70 54 65 72 6d  urn;.  }.  pTerm
28651 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 54 65   = &pWC->a[idxTe
28652 72 6d 5d 3b 0a 20 20 70 4d 61 73 6b 53 65 74 20  rm];.  pMaskSet 
28653 3d 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65 74 3b  = pWC->pMaskSet;
28654 0a 20 20 70 45 78 70 72 20 3d 20 70 54 65 72 6d  .  pExpr = pTerm
28655 2d 3e 70 45 78 70 72 3b 0a 20 20 70 72 65 72 65  ->pExpr;.  prere
28656 71 4c 65 66 74 20 3d 20 65 78 70 72 54 61 62 6c  qLeft = exprTabl
28657 65 55 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c  eUsage(pMaskSet,
28658 20 70 45 78 70 72 2d 3e 70 4c 65 66 74 29 3b 0a   pExpr->pLeft);.
28659 20 20 6f 70 20 3d 20 70 45 78 70 72 2d 3e 6f 70    op = pExpr->op
2865a 3b 0a 20 20 69 66 28 20 6f 70 3d 3d 54 4b 5f 49  ;.  if( op==TK_I
2865b 4e 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  N ){.    assert(
2865c 20 70 45 78 70 72 2d 3e 70 52 69 67 68 74 3d 3d   pExpr->pRight==
2865d 30 20 29 3b 0a 20 20 20 20 70 54 65 72 6d 2d 3e  0 );.    pTerm->
2865e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20 65 78  prereqRight = ex
2865f 70 72 4c 69 73 74 54 61 62 6c 65 55 73 61 67 65  prListTableUsage
28660 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
28661 2d 3e 70 4c 69 73 74 29 0a 20 20 20 20 20 20 20  ->pList).       
28662 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28663 20 20 20 7c 20 65 78 70 72 53 65 6c 65 63 74 54     | exprSelectT
28664 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
28665 65 74 2c 20 70 45 78 70 72 2d 3e 70 53 65 6c 65  et, pExpr->pSele
28666 63 74 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28  ct);.  }else if(
28667 20 6f 70 3d 3d 54 4b 5f 49 53 4e 55 4c 4c 20 29   op==TK_ISNULL )
28668 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 70 72 65  {.    pTerm->pre
28669 72 65 71 52 69 67 68 74 20 3d 20 30 3b 0a 20 20  reqRight = 0;.  
2866a 7d 65 6c 73 65 7b 0a 20 20 20 20 70 54 65 72 6d  }else{.    pTerm
2866b 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d 20  ->prereqRight = 
2866c 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28 70  exprTableUsage(p
2866d 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72 2d 3e  MaskSet, pExpr->
2866e 70 52 69 67 68 74 29 3b 0a 20 20 7d 0a 20 20 70  pRight);.  }.  p
2866f 72 65 72 65 71 41 6c 6c 20 3d 20 65 78 70 72 54  rereqAll = exprT
28670 61 62 6c 65 55 73 61 67 65 28 70 4d 61 73 6b 53  ableUsage(pMaskS
28671 65 74 2c 20 70 45 78 70 72 29 3b 0a 20 20 69 66  et, pExpr);.  if
28672 28 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  ( ExprHasPropert
28673 79 28 70 45 78 70 72 2c 20 45 50 5f 46 72 6f 6d  y(pExpr, EP_From
28674 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 42 69 74  Join) ){.    Bit
28675 6d 61 73 6b 20 78 20 3d 20 67 65 74 4d 61 73 6b  mask x = getMask
28676 28 70 4d 61 73 6b 53 65 74 2c 20 70 45 78 70 72  (pMaskSet, pExpr
28677 2d 3e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  ->iRightJoinTabl
28678 65 29 3b 0a 20 20 20 20 70 72 65 72 65 71 41 6c  e);.    prereqAl
28679 6c 20 7c 3d 20 78 3b 0a 20 20 20 20 65 78 74 72  l |= x;.    extr
2867a 61 52 69 67 68 74 20 3d 20 78 2d 31 3b 20 20 2f  aRight = x-1;  /
2867b 2a 20 4f 4e 20 63 6c 61 75 73 65 20 74 65 72 6d  * ON clause term
2867c 73 20 6d 61 79 20 6e 6f 74 20 62 65 20 75 73 65  s may not be use
2867d 64 20 77 69 74 68 20 61 6e 20 69 6e 64 65 78 0a  d with an index.
2867e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2867f 20 20 20 20 20 20 20 2a 2a 20 6f 6e 20 6c 65 66         ** on lef
28680 74 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46  t table of a LEF
28681 54 20 4a 4f 49 4e 2e 20 20 54 69 63 6b 65 74 20  T JOIN.  Ticket 
28682 23 33 30 31 35 20 2a 2f 0a 20 20 7d 0a 20 20 70  #3015 */.  }.  p
28683 54 65 72 6d 2d 3e 70 72 65 72 65 71 41 6c 6c 20  Term->prereqAll 
28684 3d 20 70 72 65 72 65 71 41 6c 6c 3b 0a 20 20 70  = prereqAll;.  p
28685 54 65 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72  Term->leftCursor
28686 20 3d 20 2d 31 3b 0a 20 20 70 54 65 72 6d 2d 3e   = -1;.  pTerm->
28687 69 50 61 72 65 6e 74 20 3d 20 2d 31 3b 0a 20 20  iParent = -1;.  
28688 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
28689 20 3d 20 30 3b 0a 20 20 69 66 28 20 61 6c 6c 6f   = 0;.  if( allo
2868a 77 65 64 4f 70 28 6f 70 29 20 26 26 20 28 70 54  wedOp(op) && (pT
2868b 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74  erm->prereqRight
2868c 20 26 20 70 72 65 72 65 71 4c 65 66 74 29 3d 3d   & prereqLeft)==
2868d 30 20 29 7b 0a 20 20 20 20 45 78 70 72 20 2a 70  0 ){.    Expr *p
2868e 4c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c  Left = pExpr->pL
2868f 65 66 74 3b 0a 20 20 20 20 45 78 70 72 20 2a 70  eft;.    Expr *p
28690 52 69 67 68 74 20 3d 20 70 45 78 70 72 2d 3e 70  Right = pExpr->p
28691 52 69 67 68 74 3b 0a 20 20 20 20 69 66 28 20 70  Right;.    if( p
28692 4c 65 66 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c  Left->op==TK_COL
28693 55 4d 4e 20 29 7b 0a 20 20 20 20 20 20 70 54 65  UMN ){.      pTe
28694 72 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 3d  rm->leftCursor =
28695 20 70 4c 65 66 74 2d 3e 69 54 61 62 6c 65 3b 0a   pLeft->iTable;.
28696 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6c 65 66        pTerm->lef
28697 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65 66 74 2d  tColumn = pLeft-
28698 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  >iColumn;.      
28699 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
2869a 20 3d 20 6f 70 65 72 61 74 6f 72 4d 61 73 6b 28   = operatorMask(
2869b 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69  op);.    }.    i
2869c 66 28 20 70 52 69 67 68 74 20 26 26 20 70 52 69  f( pRight && pRi
2869d 67 68 74 2d 3e 6f 70 3d 3d 54 4b 5f 43 4f 4c 55  ght->op==TK_COLU
2869e 4d 4e 20 29 7b 0a 20 20 20 20 20 20 57 68 65 72  MN ){.      Wher
2869f 65 54 65 72 6d 20 2a 70 4e 65 77 3b 0a 20 20 20  eTerm *pNew;.   
286a0 20 20 20 45 78 70 72 20 2a 70 44 75 70 3b 0a 20     Expr *pDup;. 
286a1 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
286a2 6c 65 66 74 43 75 72 73 6f 72 3e 3d 30 20 29 7b  leftCursor>=0 ){
286a3 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 64 78  .        int idx
286a4 4e 65 77 3b 0a 20 20 20 20 20 20 20 20 70 44 75  New;.        pDu
286a5 70 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44  p = sqlite3ExprD
286a6 75 70 28 64 62 2c 20 70 45 78 70 72 29 3b 0a 20  up(db, pExpr);. 
286a7 20 20 20 20 20 20 20 69 66 28 20 64 62 2d 3e 6d         if( db->m
286a8 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
286a9 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
286aa 45 78 70 72 44 65 6c 65 74 65 28 64 62 2c 20 70  ExprDelete(db, p
286ab 44 75 70 29 3b 0a 20 20 20 20 20 20 20 20 20 20  Dup);.          
286ac 72 65 74 75 72 6e 3b 0a 20 20 20 20 20 20 20 20  return;.        
286ad 7d 0a 20 20 20 20 20 20 20 20 69 64 78 4e 65 77  }.        idxNew
286ae 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49 6e   = whereClauseIn
286af 73 65 72 74 28 70 57 43 2c 20 70 44 75 70 2c 20  sert(pWC, pDup, 
286b0 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45 52  TERM_VIRTUAL|TER
286b1 4d 5f 44 59 4e 41 4d 49 43 29 3b 0a 20 20 20 20  M_DYNAMIC);.    
286b2 20 20 20 20 69 66 28 20 69 64 78 4e 65 77 3d 3d      if( idxNew==
286b3 30 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 20 20  0 ) return;.    
286b4 20 20 20 20 70 4e 65 77 20 3d 20 26 70 57 43 2d      pNew = &pWC-
286b5 3e 61 5b 69 64 78 4e 65 77 5d 3b 0a 20 20 20 20  >a[idxNew];.    
286b6 20 20 20 20 70 4e 65 77 2d 3e 69 50 61 72 65 6e      pNew->iParen
286b7 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20 20 20  t = idxTerm;.   
286b8 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
286b9 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
286ba 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43         pTerm->nC
286bb 68 69 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20  hild = 1;.      
286bc 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c    pTerm->flags |
286bd 3d 20 54 45 52 4d 5f 43 4f 50 49 45 44 3b 0a 20  = TERM_COPIED;. 
286be 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
286bf 20 20 20 20 70 44 75 70 20 3d 20 70 45 78 70 72      pDup = pExpr
286c0 3b 0a 20 20 20 20 20 20 20 20 70 4e 65 77 20 3d  ;.        pNew =
286c1 20 70 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a   pTerm;.      }.
286c2 20 20 20 20 20 20 65 78 70 72 43 6f 6d 6d 75 74        exprCommut
286c3 65 28 70 50 61 72 73 65 2c 20 70 44 75 70 29 3b  e(pParse, pDup);
286c4 0a 20 20 20 20 20 20 70 4c 65 66 74 20 3d 20 70  .      pLeft = p
286c5 44 75 70 2d 3e 70 4c 65 66 74 3b 0a 20 20 20 20  Dup->pLeft;.    
286c6 20 20 70 4e 65 77 2d 3e 6c 65 66 74 43 75 72 73    pNew->leftCurs
286c7 6f 72 20 3d 20 70 4c 65 66 74 2d 3e 69 54 61 62  or = pLeft->iTab
286c8 6c 65 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e  le;.      pNew->
286c9 6c 65 66 74 43 6f 6c 75 6d 6e 20 3d 20 70 4c 65  leftColumn = pLe
286ca 66 74 2d 3e 69 43 6f 6c 75 6d 6e 3b 0a 20 20 20  ft->iColumn;.   
286cb 20 20 20 70 4e 65 77 2d 3e 70 72 65 72 65 71 52     pNew->prereqR
286cc 69 67 68 74 20 3d 20 70 72 65 72 65 71 4c 65 66  ight = prereqLef
286cd 74 3b 0a 20 20 20 20 20 20 70 4e 65 77 2d 3e 70  t;.      pNew->p
286ce 72 65 72 65 71 41 6c 6c 20 3d 20 70 72 65 72 65  rereqAll = prere
286cf 71 41 6c 6c 3b 0a 20 20 20 20 20 20 70 4e 65 77  qAll;.      pNew
286d0 2d 3e 65 4f 70 65 72 61 74 6f 72 20 3d 20 6f 70  ->eOperator = op
286d1 65 72 61 74 6f 72 4d 61 73 6b 28 70 44 75 70 2d  eratorMask(pDup-
286d2 3e 6f 70 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  >op);.    }.  }.
286d3 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
286d4 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50 54  OMIT_BETWEEN_OPT
286d5 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 49  IMIZATION.  /* I
286d6 66 20 61 20 74 65 72 6d 20 69 73 20 74 68 65 20  f a term is the 
286d7 42 45 54 57 45 45 4e 20 6f 70 65 72 61 74 6f 72  BETWEEN operator
286d8 2c 20 63 72 65 61 74 65 20 74 77 6f 20 6e 65 77  , create two new
286d9 20 76 69 72 74 75 61 6c 20 74 65 72 6d 73 0a 20   virtual terms. 
286da 20 2a 2a 20 74 68 61 74 20 64 65 66 69 6e 65 20   ** that define 
286db 74 68 65 20 72 61 6e 67 65 20 74 68 61 74 20 74  the range that t
286dc 68 65 20 42 45 54 57 45 45 4e 20 69 6d 70 6c 65  he BETWEEN imple
286dd 6d 65 6e 74 73 2e 0a 20 20 2a 2f 0a 20 20 65 6c  ments..  */.  el
286de 73 65 20 69 66 28 20 70 45 78 70 72 2d 3e 6f 70  se if( pExpr->op
286df 3d 3d 54 4b 5f 42 45 54 57 45 45 4e 20 29 7b 0a  ==TK_BETWEEN ){.
286e0 20 20 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4c      ExprList *pL
286e1 69 73 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69  ist = pExpr->pLi
286e2 73 74 3b 0a 20 20 20 20 69 6e 74 20 69 3b 0a 20  st;.    int i;. 
286e3 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
286e4 75 38 20 6f 70 73 5b 5d 20 3d 20 7b 54 4b 5f 47  u8 ops[] = {TK_G
286e5 45 2c 20 54 4b 5f 4c 45 7d 3b 0a 20 20 20 20 61  E, TK_LE};.    a
286e6 73 73 65 72 74 28 20 70 4c 69 73 74 21 3d 30 20  ssert( pList!=0 
286e7 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70  );.    assert( p
286e8 4c 69 73 74 2d 3e 6e 45 78 70 72 3d 3d 32 20 29  List->nExpr==2 )
286e9 3b 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69  ;.    for(i=0; i
286ea 3c 32 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  <2; i++){.      
286eb 45 78 70 72 20 2a 70 4e 65 77 45 78 70 72 3b 0a  Expr *pNewExpr;.
286ec 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65 77        int idxNew
286ed 3b 0a 20 20 20 20 20 20 70 4e 65 77 45 78 70 72  ;.      pNewExpr
286ee 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 28 64   = sqlite3Expr(d
286ef 62 2c 20 6f 70 73 5b 69 5d 2c 20 73 71 6c 69 74  b, ops[i], sqlit
286f0 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 45  e3ExprDup(db, pE
286f1 78 70 72 2d 3e 70 4c 65 66 74 29 2c 0a 20 20 20  xpr->pLeft),.   
286f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
286f3 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
286f4 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4c 69  3ExprDup(db, pLi
286f5 73 74 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 29 2c  st->a[i].pExpr),
286f6 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65   0);.      idxNe
286f7 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  w = whereClauseI
286f8 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45  nsert(pWC, pNewE
286f9 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  xpr, TERM_VIRTUA
286fa 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
286fb 0a 20 20 20 20 20 20 65 78 70 72 41 6e 61 6c 79  .      exprAnaly
286fc 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69 64  ze(pSrc, pWC, id
286fd 78 4e 65 77 29 3b 0a 20 20 20 20 20 20 70 54 65  xNew);.      pTe
286fe 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78  rm = &pWC->a[idx
286ff 54 65 72 6d 5d 3b 0a 20 20 20 20 20 20 70 57 43  Term];.      pWC
28700 2d 3e 61 5b 69 64 78 4e 65 77 5d 2e 69 50 61 72  ->a[idxNew].iPar
28701 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20  ent = idxTerm;. 
28702 20 20 20 7d 0a 20 20 20 20 70 54 65 72 6d 2d 3e     }.    pTerm->
28703 6e 43 68 69 6c 64 20 3d 20 32 3b 0a 20 20 7d 0a  nChild = 2;.  }.
28704 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45  #endif /* SQLITE
28705 5f 4f 4d 49 54 5f 42 45 54 57 45 45 4e 5f 4f 50  _OMIT_BETWEEN_OP
28706 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a 23  TIMIZATION */..#
28707 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
28708 54 45 5f 4f 4d 49 54 5f 4f 52 5f 4f 50 54 49 4d  TE_OMIT_OR_OPTIM
28709 49 5a 41 54 49 4f 4e 29 20 26 26 20 21 64 65 66  IZATION) && !def
2870a 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49 54  ined(SQLITE_OMIT
2870b 5f 53 55 42 51 55 45 52 59 29 0a 20 20 2f 2a 20  _SUBQUERY).  /* 
2870c 41 74 74 65 6d 70 74 20 74 6f 20 63 6f 6e 76 65  Attempt to conve
2870d 72 74 20 4f 52 2d 63 6f 6e 6e 65 63 74 65 64 20  rt OR-connected 
2870e 74 65 72 6d 73 20 69 6e 74 6f 20 61 6e 20 49 4e  terms into an IN
2870f 20 6f 70 65 72 61 74 6f 72 20 73 6f 20 74 68 61   operator so tha
28710 74 0a 20 20 2a 2a 20 74 68 65 79 20 63 61 6e 20  t.  ** they can 
28711 6d 61 6b 65 20 75 73 65 20 6f 66 20 69 6e 64 69  make use of indi
28712 63 65 73 2e 20 20 45 78 61 6d 70 6c 65 3a 0a 20  ces.  Example:. 
28713 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 20 78 20   **.  **      x 
28714 3d 20 65 78 70 72 31 20 20 4f 52 20 20 65 78 70  = expr1  OR  exp
28715 72 32 20 3d 20 78 20 20 4f 52 20 20 78 20 3d 20  r2 = x  OR  x = 
28716 65 78 70 72 33 0a 20 20 2a 2a 0a 20 20 2a 2a 20  expr3.  **.  ** 
28717 69 73 20 63 6f 6e 76 65 72 74 65 64 20 69 6e 74  is converted int
28718 6f 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20  o.  **.  **     
28719 20 78 20 49 4e 20 28 65 78 70 72 31 2c 65 78 70   x IN (expr1,exp
2871a 72 32 2c 65 78 70 72 33 29 0a 20 20 2a 2a 0a 20  r2,expr3).  **. 
2871b 20 2a 2a 20 54 68 69 73 20 6f 70 74 69 6d 69 7a   ** This optimiz
2871c 61 74 69 6f 6e 20 6d 75 73 74 20 62 65 20 6f 6d  ation must be om
2871d 69 74 74 65 64 20 69 66 20 4f 4d 49 54 5f 53 55  itted if OMIT_SU
2871e 42 51 55 45 52 59 20 69 73 20 64 65 66 69 6e 65  BQUERY is define
2871f 64 20 62 65 63 61 75 73 65 0a 20 20 2a 2a 20 74  d because.  ** t
28720 68 65 20 63 6f 6d 70 69 6c 65 72 20 66 6f 72 20  he compiler for 
28721 74 68 65 20 74 68 65 20 49 4e 20 6f 70 65 72 61  the the IN opera
28722 74 6f 72 20 69 73 20 70 61 72 74 20 6f 66 20 73  tor is part of s
28723 75 62 2d 71 75 65 72 69 65 73 2e 0a 20 20 2a 2f  ub-queries..  */
28724 0a 20 20 65 6c 73 65 20 69 66 28 20 70 45 78 70  .  else if( pExp
28725 72 2d 3e 6f 70 3d 3d 54 4b 5f 4f 52 20 29 7b 0a  r->op==TK_OR ){.
28726 20 20 20 20 69 6e 74 20 6f 6b 3b 0a 20 20 20 20      int ok;.    
28727 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 20 20 69 6e  int i, j;.    in
28728 74 20 69 43 6f 6c 75 6d 6e 2c 20 69 43 75 72 73  t iColumn, iCurs
28729 6f 72 3b 0a 20 20 20 20 57 68 65 72 65 43 6c 61  or;.    WhereCla
2872a 75 73 65 20 73 4f 72 3b 0a 20 20 20 20 57 68 65  use sOr;.    Whe
2872b 72 65 54 65 72 6d 20 2a 70 4f 72 54 65 72 6d 3b  reTerm *pOrTerm;
2872c 0a 0a 20 20 20 20 61 73 73 65 72 74 28 20 28 70  ..    assert( (p
2872d 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20 54 45  Term->flags & TE
2872e 52 4d 5f 44 59 4e 41 4d 49 43 29 3d 3d 30 20 29  RM_DYNAMIC)==0 )
2872f 3b 0a 20 20 20 20 77 68 65 72 65 43 6c 61 75 73  ;.    whereClaus
28730 65 49 6e 69 74 28 26 73 4f 72 2c 20 70 57 43 2d  eInit(&sOr, pWC-
28731 3e 70 50 61 72 73 65 2c 20 70 4d 61 73 6b 53 65  >pParse, pMaskSe
28732 74 29 3b 0a 20 20 20 20 77 68 65 72 65 53 70 6c  t);.    whereSpl
28733 69 74 28 26 73 4f 72 2c 20 70 45 78 70 72 2c 20  it(&sOr, pExpr, 
28734 54 4b 5f 4f 52 29 3b 0a 20 20 20 20 65 78 70 72  TK_OR);.    expr
28735 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 53 72 63 2c  AnalyzeAll(pSrc,
28736 20 26 73 4f 72 29 3b 0a 20 20 20 20 61 73 73 65   &sOr);.    asse
28737 72 74 28 20 73 4f 72 2e 6e 54 65 72 6d 3e 3d 32  rt( sOr.nTerm>=2
28738 20 29 3b 0a 20 20 20 20 6a 20 3d 20 30 3b 0a 20   );.    j = 0;. 
28739 20 20 20 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f     if( db->mallo
2873a 63 46 61 69 6c 65 64 20 29 20 67 6f 74 6f 20 6f  cFailed ) goto o
2873b 72 5f 6e 6f 74 5f 70 6f 73 73 69 62 6c 65 3b 0a  r_not_possible;.
2873c 20 20 20 20 64 6f 7b 0a 20 20 20 20 20 20 61 73      do{.      as
2873d 73 65 72 74 28 20 6a 3c 73 4f 72 2e 6e 54 65 72  sert( j<sOr.nTer
2873e 6d 20 29 3b 0a 20 20 20 20 20 20 69 43 6f 6c 75  m );.      iColu
2873f 6d 6e 20 3d 20 73 4f 72 2e 61 5b 6a 5d 2e 6c 65  mn = sOr.a[j].le
28740 66 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20  ftColumn;.      
28741 69 43 75 72 73 6f 72 20 3d 20 73 4f 72 2e 61 5b  iCursor = sOr.a[
28742 6a 5d 2e 6c 65 66 74 43 75 72 73 6f 72 3b 0a 20  j].leftCursor;. 
28743 20 20 20 20 20 6f 6b 20 3d 20 69 43 75 72 73 6f       ok = iCurso
28744 72 3e 3d 30 3b 0a 20 20 20 20 20 20 66 6f 72 28  r>=0;.      for(
28745 69 3d 73 4f 72 2e 6e 54 65 72 6d 2d 31 2c 20 70  i=sOr.nTerm-1, p
28746 4f 72 54 65 72 6d 3d 73 4f 72 2e 61 3b 20 69 3e  OrTerm=sOr.a; i>
28747 3d 30 20 26 26 20 6f 6b 3b 20 69 2d 2d 2c 20 70  =0 && ok; i--, p
28748 4f 72 54 65 72 6d 2b 2b 29 7b 0a 20 20 20 20 20  OrTerm++){.     
28749 20 20 20 69 66 28 20 70 4f 72 54 65 72 6d 2d 3e     if( pOrTerm->
2874a 65 4f 70 65 72 61 74 6f 72 21 3d 57 4f 5f 45 51  eOperator!=WO_EQ
2874b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 67 6f   ){.          go
2874c 74 6f 20 6f 72 5f 6e 6f 74 5f 70 6f 73 73 69 62  to or_not_possib
2874d 6c 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  le;.        }.  
2874e 20 20 20 20 20 20 69 66 28 20 6f 72 54 65 72 6d        if( orTerm
2874f 49 73 4f 70 74 43 61 6e 64 69 64 61 74 65 28 70  IsOptCandidate(p
28750 4f 72 54 65 72 6d 2c 20 69 43 75 72 73 6f 72 2c  OrTerm, iCursor,
28751 20 69 43 6f 6c 75 6d 6e 29 20 29 7b 0a 20 20 20   iColumn) ){.   
28752 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d 3e         pOrTerm->
28753 66 6c 61 67 73 20 7c 3d 20 54 45 52 4d 5f 4f 52  flags |= TERM_OR
28754 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  _OK;.        }el
28755 73 65 20 69 66 28 20 6f 72 54 65 72 6d 48 61 73  se if( orTermHas
28756 4f 6b 44 75 70 6c 69 63 61 74 65 28 26 73 4f 72  OkDuplicate(&sOr
28757 2c 20 70 4f 72 54 65 72 6d 29 20 29 7b 0a 20 20  , pOrTerm) ){.  
28758 20 20 20 20 20 20 20 20 70 4f 72 54 65 72 6d 2d          pOrTerm-
28759 3e 66 6c 61 67 73 20 26 3d 20 7e 54 45 52 4d 5f  >flags &= ~TERM_
2875a 4f 52 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 7d  OR_OK;.        }
2875b 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2875c 6f 6b 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20  ok = 0;.        
2875d 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 77  }.      }.    }w
2875e 68 69 6c 65 28 20 21 6f 6b 20 26 26 20 28 73 4f  hile( !ok && (sO
2875f 72 2e 61 5b 6a 2b 2b 5d 2e 66 6c 61 67 73 20 26  r.a[j++].flags &
28760 20 54 45 52 4d 5f 43 4f 50 49 45 44 29 21 3d 30   TERM_COPIED)!=0
28761 20 26 26 20 6a 3c 32 20 29 3b 0a 20 20 20 20 69   && j<2 );.    i
28762 66 28 20 6f 6b 20 29 7b 0a 20 20 20 20 20 20 45  f( ok ){.      E
28763 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 20 3d  xprList *pList =
28764 20 30 3b 0a 20 20 20 20 20 20 45 78 70 72 20 2a   0;.      Expr *
28765 70 4e 65 77 2c 20 2a 70 44 75 70 3b 0a 20 20 20  pNew, *pDup;.   
28766 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d     Expr *pLeft =
28767 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d   0;.      for(i=
28768 73 4f 72 2e 6e 54 65 72 6d 2d 31 2c 20 70 4f 72  sOr.nTerm-1, pOr
28769 54 65 72 6d 3d 73 4f 72 2e 61 3b 20 69 3e 3d 30  Term=sOr.a; i>=0
2876a 3b 20 69 2d 2d 2c 20 70 4f 72 54 65 72 6d 2b 2b  ; i--, pOrTerm++
2876b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 28  ){.        if( (
2876c 70 4f 72 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26  pOrTerm->flags &
2876d 20 54 45 52 4d 5f 4f 52 5f 4f 4b 29 3d 3d 30 20   TERM_OR_OK)==0 
2876e 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2876f 20 20 20 20 70 44 75 70 20 3d 20 73 71 6c 69 74      pDup = sqlit
28770 65 33 45 78 70 72 44 75 70 28 64 62 2c 20 70 4f  e3ExprDup(db, pO
28771 72 54 65 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 52  rTerm->pExpr->pR
28772 69 67 68 74 29 3b 0a 20 20 20 20 20 20 20 20 70  ight);.        p
28773 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45 78  List = sqlite3Ex
28774 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 57 43  prListAppend(pWC
28775 2d 3e 70 50 61 72 73 65 2c 20 70 4c 69 73 74 2c  ->pParse, pList,
28776 20 70 44 75 70 2c 20 30 29 3b 0a 20 20 20 20 20   pDup, 0);.     
28777 20 20 20 70 4c 65 66 74 20 3d 20 70 4f 72 54 65     pLeft = pOrTe
28778 72 6d 2d 3e 70 45 78 70 72 2d 3e 70 4c 65 66 74  rm->pExpr->pLeft
28779 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2877a 61 73 73 65 72 74 28 20 70 4c 65 66 74 21 3d 30  assert( pLeft!=0
2877b 20 29 3b 0a 20 20 20 20 20 20 70 44 75 70 20 3d   );.      pDup =
2877c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70 28   sqlite3ExprDup(
2877d 64 62 2c 20 70 4c 65 66 74 29 3b 0a 20 20 20 20  db, pLeft);.    
2877e 20 20 70 4e 65 77 20 3d 20 73 71 6c 69 74 65 33    pNew = sqlite3
2877f 45 78 70 72 28 64 62 2c 20 54 4b 5f 49 4e 2c 20  Expr(db, TK_IN, 
28780 70 44 75 70 2c 20 30 2c 20 30 29 3b 0a 20 20 20  pDup, 0, 0);.   
28781 20 20 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20     if( pNew ){. 
28782 20 20 20 20 20 20 20 69 6e 74 20 69 64 78 4e 65         int idxNe
28783 77 3b 0a 20 20 20 20 20 20 20 20 74 72 61 6e 73  w;.        trans
28784 66 65 72 4a 6f 69 6e 4d 61 72 6b 69 6e 67 73 28  ferJoinMarkings(
28785 70 4e 65 77 2c 20 70 45 78 70 72 29 3b 0a 20 20  pNew, pExpr);.  
28786 20 20 20 20 20 20 70 4e 65 77 2d 3e 70 4c 69 73        pNew->pLis
28787 74 20 3d 20 70 4c 69 73 74 3b 0a 20 20 20 20 20  t = pList;.     
28788 20 20 20 69 64 78 4e 65 77 20 3d 20 77 68 65 72     idxNew = wher
28789 65 43 6c 61 75 73 65 49 6e 73 65 72 74 28 70 57  eClauseInsert(pW
2878a 43 2c 20 70 4e 65 77 2c 20 54 45 52 4d 5f 56 49  C, pNew, TERM_VI
2878b 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d  RTUAL|TERM_DYNAM
2878c 49 43 29 3b 0a 20 20 20 20 20 20 20 20 65 78 70  IC);.        exp
2878d 72 41 6e 61 6c 79 7a 65 28 70 53 72 63 2c 20 70  rAnalyze(pSrc, p
2878e 57 43 2c 20 69 64 78 4e 65 77 29 3b 0a 20 20 20  WC, idxNew);.   
2878f 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
28790 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
28791 20 20 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64         pWC->a[id
28792 78 4e 65 77 5d 2e 69 50 61 72 65 6e 74 20 3d 20  xNew].iParent = 
28793 69 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 20  idxTerm;.       
28794 20 70 54 65 72 6d 2d 3e 6e 43 68 69 6c 64 20 3d   pTerm->nChild =
28795 20 31 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b   1;.      }else{
28796 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
28797 45 78 70 72 4c 69 73 74 44 65 6c 65 74 65 28 64  ExprListDelete(d
28798 62 2c 20 70 4c 69 73 74 29 3b 0a 20 20 20 20 20  b, pList);.     
28799 20 7d 0a 20 20 20 20 7d 0a 6f 72 5f 6e 6f 74 5f   }.    }.or_not_
2879a 70 6f 73 73 69 62 6c 65 3a 0a 20 20 20 20 77 68  possible:.    wh
2879b 65 72 65 43 6c 61 75 73 65 43 6c 65 61 72 28 26  ereClauseClear(&
2879c 73 4f 72 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  sOr);.  }.#endif
2879d 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f   /* SQLITE_OMIT_
2879e 4f 52 5f 4f 50 54 49 4d 49 5a 41 54 49 4f 4e 20  OR_OPTIMIZATION 
2879f 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  */..#ifndef SQLI
287a0 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f 50 54  TE_OMIT_LIKE_OPT
287a1 49 4d 49 5a 41 54 49 4f 4e 0a 20 20 2f 2a 20 41  IMIZATION.  /* A
287a2 64 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 74  dd constraints t
287a3 6f 20 72 65 64 75 63 65 20 74 68 65 20 73 65 61  o reduce the sea
287a4 72 63 68 20 73 70 61 63 65 20 6f 6e 20 61 20 4c  rch space on a L
287a5 49 4b 45 20 6f 72 20 47 4c 4f 42 0a 20 20 2a 2a  IKE or GLOB.  **
287a6 20 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2a 0a   operator..  **.
287a7 20 20 2a 2a 20 41 20 6c 69 6b 65 20 70 61 74 74    ** A like patt
287a8 65 72 6e 20 6f 66 20 74 68 65 20 66 6f 72 6d 20  ern of the form 
287a9 22 78 20 4c 49 4b 45 20 27 61 62 63 25 27 22 20  "x LIKE 'abc%'" 
287aa 69 73 20 63 68 61 6e 67 65 64 20 69 6e 74 6f 20  is changed into 
287ab 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20 2a 2a  constraints.  **
287ac 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 78  .  **          x
287ad 3e 3d 27 61 62 63 27 20 41 4e 44 20 78 3c 27 61  >='abc' AND x<'a
287ae 62 64 27 20 41 4e 44 20 78 20 4c 49 4b 45 20 27  bd' AND x LIKE '
287af 61 62 63 25 27 0a 20 20 2a 2a 0a 20 20 2a 2a 20  abc%'.  **.  ** 
287b0 54 68 65 20 6c 61 73 74 20 63 68 61 72 61 63 74  The last charact
287b1 65 72 20 6f 66 20 74 68 65 20 70 72 65 66 69 78  er of the prefix
287b2 20 22 61 62 63 22 20 69 73 20 69 6e 63 72 65 6d   "abc" is increm
287b3 65 6e 74 65 64 20 74 6f 20 66 6f 72 6d 20 74 68  ented to form th
287b4 65 0a 20 20 2a 2a 20 74 65 72 6d 69 6e 61 74 69  e.  ** terminati
287b5 6f 6e 20 63 6f 6e 64 69 74 69 6f 6e 20 22 61 62  on condition "ab
287b6 64 22 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  d"..  */.  if( i
287b7 73 4c 69 6b 65 4f 72 47 6c 6f 62 28 70 50 61 72  sLikeOrGlob(pPar
287b8 73 65 2c 20 70 45 78 70 72 2c 20 26 6e 50 61 74  se, pExpr, &nPat
287b9 74 65 72 6e 2c 20 26 69 73 43 6f 6d 70 6c 65 74  tern, &isComplet
287ba 65 2c 20 26 6e 6f 43 61 73 65 29 20 29 7b 0a 20  e, &noCase) ){. 
287bb 20 20 20 45 78 70 72 20 2a 70 4c 65 66 74 2c 20     Expr *pLeft, 
287bc 2a 70 52 69 67 68 74 3b 0a 20 20 20 20 45 78 70  *pRight;.    Exp
287bd 72 20 2a 70 53 74 72 31 2c 20 2a 70 53 74 72 32  r *pStr1, *pStr2
287be 3b 0a 20 20 20 20 45 78 70 72 20 2a 70 4e 65 77  ;.    Expr *pNew
287bf 45 78 70 72 31 2c 20 2a 70 4e 65 77 45 78 70 72  Expr1, *pNewExpr
287c0 32 3b 0a 20 20 20 20 69 6e 74 20 69 64 78 4e 65  2;.    int idxNe
287c1 77 31 2c 20 69 64 78 4e 65 77 32 3b 0a 0a 20 20  w1, idxNew2;..  
287c2 20 20 70 4c 65 66 74 20 3d 20 70 45 78 70 72 2d    pLeft = pExpr-
287c3 3e 70 4c 69 73 74 2d 3e 61 5b 31 5d 2e 70 45 78  >pList->a[1].pEx
287c4 70 72 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d  pr;.    pRight =
287c5 20 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61   pExpr->pList->a
287c6 5b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70  [0].pExpr;.    p
287c7 53 74 72 31 20 3d 20 73 71 6c 69 74 65 33 50 45  Str1 = sqlite3PE
287c8 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 53  xpr(pParse, TK_S
287c9 54 52 49 4e 47 2c 20 30 2c 20 30 2c 20 30 29 3b  TRING, 0, 0, 0);
287ca 0a 20 20 20 20 69 66 28 20 70 53 74 72 31 20 29  .    if( pStr1 )
287cb 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 54  {.      sqlite3T
287cc 6f 6b 65 6e 43 6f 70 79 28 64 62 2c 20 26 70 53  okenCopy(db, &pS
287cd 74 72 31 2d 3e 74 6f 6b 65 6e 2c 20 26 70 52 69  tr1->token, &pRi
287ce 67 68 74 2d 3e 74 6f 6b 65 6e 29 3b 0a 20 20 20  ght->token);.   
287cf 20 20 20 70 53 74 72 31 2d 3e 74 6f 6b 65 6e 2e     pStr1->token.
287d0 6e 20 3d 20 6e 50 61 74 74 65 72 6e 3b 0a 20 20  n = nPattern;.  
287d1 20 20 20 20 70 53 74 72 31 2d 3e 66 6c 61 67 73      pStr1->flags
287d2 20 3d 20 45 50 5f 44 65 71 75 6f 74 65 64 3b 0a   = EP_Dequoted;.
287d3 20 20 20 20 7d 0a 20 20 20 20 70 53 74 72 32 20      }.    pStr2 
287d4 3d 20 73 71 6c 69 74 65 33 45 78 70 72 44 75 70  = sqlite3ExprDup
287d5 28 64 62 2c 20 70 53 74 72 31 29 3b 0a 20 20 20  (db, pStr1);.   
287d6 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
287d7 46 61 69 6c 65 64 20 29 7b 0a 20 20 20 20 20 20  Failed ){.      
287d8 75 38 20 63 2c 20 2a 70 43 3b 0a 20 20 20 20 20  u8 c, *pC;.     
287d9 20 61 73 73 65 72 74 28 20 70 53 74 72 32 2d 3e   assert( pStr2->
287da 74 6f 6b 65 6e 2e 64 79 6e 20 29 3b 0a 20 20 20  token.dyn );.   
287db 20 20 20 70 43 20 3d 20 28 75 38 2a 29 26 70 53     pC = (u8*)&pS
287dc 74 72 32 2d 3e 74 6f 6b 65 6e 2e 7a 5b 6e 50 61  tr2->token.z[nPa
287dd 74 74 65 72 6e 2d 31 5d 3b 0a 20 20 20 20 20 20  ttern-1];.      
287de 63 20 3d 20 2a 70 43 3b 0a 20 20 20 20 20 20 69  c = *pC;.      i
287df 66 28 20 6e 6f 43 61 73 65 20 29 7b 0a 20 20 20  f( noCase ){.   
287e0 20 20 20 20 20 69 66 28 20 63 3d 3d 27 40 27 20       if( c=='@' 
287e1 29 20 69 73 43 6f 6d 70 6c 65 74 65 20 3d 20 30  ) isComplete = 0
287e2 3b 0a 20 20 20 20 20 20 20 20 63 20 3d 20 73 71  ;.        c = sq
287e3 6c 69 74 65 33 55 70 70 65 72 54 6f 4c 6f 77 65  lite3UpperToLowe
287e4 72 5b 63 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  r[c];.      }.  
287e5 20 20 20 20 2a 70 43 20 3d 20 63 20 2b 20 31 3b      *pC = c + 1;
287e6 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 65 77 45  .    }.    pNewE
287e7 78 70 72 31 20 3d 20 73 71 6c 69 74 65 33 50 45  xpr1 = sqlite3PE
287e8 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 47  xpr(pParse, TK_G
287e9 45 2c 20 73 71 6c 69 74 65 33 45 78 70 72 44 75  E, sqlite3ExprDu
287ea 70 28 64 62 2c 70 4c 65 66 74 29 2c 20 70 53 74  p(db,pLeft), pSt
287eb 72 31 2c 20 30 29 3b 0a 20 20 20 20 69 64 78 4e  r1, 0);.    idxN
287ec 65 77 31 20 3d 20 77 68 65 72 65 43 6c 61 75 73  ew1 = whereClaus
287ed 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65  eInsert(pWC, pNe
287ee 77 45 78 70 72 31 2c 20 54 45 52 4d 5f 56 49 52  wExpr1, TERM_VIR
287ef 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49  TUAL|TERM_DYNAMI
287f0 43 29 3b 0a 20 20 20 20 65 78 70 72 41 6e 61 6c  C);.    exprAnal
287f1 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c 20 69  yze(pSrc, pWC, i
287f2 64 78 4e 65 77 31 29 3b 0a 20 20 20 20 70 4e 65  dxNew1);.    pNe
287f3 77 45 78 70 72 32 20 3d 20 73 71 6c 69 74 65 33  wExpr2 = sqlite3
287f4 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
287f5 5f 4c 54 2c 20 73 71 6c 69 74 65 33 45 78 70 72  _LT, sqlite3Expr
287f6 44 75 70 28 64 62 2c 70 4c 65 66 74 29 2c 20 70  Dup(db,pLeft), p
287f7 53 74 72 32 2c 20 30 29 3b 0a 20 20 20 20 69 64  Str2, 0);.    id
287f8 78 4e 65 77 32 20 3d 20 77 68 65 72 65 43 6c 61  xNew2 = whereCla
287f9 75 73 65 49 6e 73 65 72 74 28 70 57 43 2c 20 70  useInsert(pWC, p
287fa 4e 65 77 45 78 70 72 32 2c 20 54 45 52 4d 5f 56  NewExpr2, TERM_V
287fb 49 52 54 55 41 4c 7c 54 45 52 4d 5f 44 59 4e 41  IRTUAL|TERM_DYNA
287fc 4d 49 43 29 3b 0a 20 20 20 20 65 78 70 72 41 6e  MIC);.    exprAn
287fd 61 6c 79 7a 65 28 70 53 72 63 2c 20 70 57 43 2c  alyze(pSrc, pWC,
287fe 20 69 64 78 4e 65 77 32 29 3b 0a 20 20 20 20 70   idxNew2);.    p
287ff 54 65 72 6d 20 3d 20 26 70 57 43 2d 3e 61 5b 69  Term = &pWC->a[i
28800 64 78 54 65 72 6d 5d 3b 0a 20 20 20 20 69 66 28  dxTerm];.    if(
28801 20 69 73 43 6f 6d 70 6c 65 74 65 20 29 7b 0a 20   isComplete ){. 
28802 20 20 20 20 20 70 57 43 2d 3e 61 5b 69 64 78 4e       pWC->a[idxN
28803 65 77 31 5d 2e 69 50 61 72 65 6e 74 20 3d 20 69  ew1].iParent = i
28804 64 78 54 65 72 6d 3b 0a 20 20 20 20 20 20 70 57  dxTerm;.      pW
28805 43 2d 3e 61 5b 69 64 78 4e 65 77 32 5d 2e 69 50  C->a[idxNew2].iP
28806 61 72 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b  arent = idxTerm;
28807 0a 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43  .      pTerm->nC
28808 68 69 6c 64 20 3d 20 32 3b 0a 20 20 20 20 7d 0a  hild = 2;.    }.
28809 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
2880a 4c 49 54 45 5f 4f 4d 49 54 5f 4c 49 4b 45 5f 4f  LITE_OMIT_LIKE_O
2880b 50 54 49 4d 49 5a 41 54 49 4f 4e 20 2a 2f 0a 0a  PTIMIZATION */..
2880c 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2880d 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
2880e 0a 20 20 2f 2a 20 41 64 64 20 61 20 57 4f 5f 4d  .  /* Add a WO_M
2880f 41 54 43 48 20 61 75 78 69 6c 69 61 72 79 20 74  ATCH auxiliary t
28810 65 72 6d 20 74 6f 20 74 68 65 20 63 6f 6e 73 74  erm to the const
28811 72 61 69 6e 74 20 73 65 74 20 69 66 20 74 68 65  raint set if the
28812 0a 20 20 2a 2a 20 63 75 72 72 65 6e 74 20 65 78  .  ** current ex
28813 70 72 65 73 73 69 6f 6e 20 69 73 20 6f 66 20 74  pression is of t
28814 68 65 20 66 6f 72 6d 3a 20 20 63 6f 6c 75 6d 6e  he form:  column
28815 20 4d 41 54 43 48 20 65 78 70 72 2e 0a 20 20 2a   MATCH expr..  *
28816 2a 20 54 68 69 73 20 69 6e 66 6f 72 6d 61 74 69  * This informati
28817 6f 6e 20 69 73 20 75 73 65 64 20 62 79 20 74 68  on is used by th
28818 65 20 78 42 65 73 74 49 6e 64 65 78 20 6d 65 74  e xBestIndex met
28819 68 6f 64 73 20 6f 66 0a 20 20 2a 2a 20 76 69 72  hods of.  ** vir
2881a 74 75 61 6c 20 74 61 62 6c 65 73 2e 20 20 54 68  tual tables.  Th
2881b 65 20 6e 61 74 69 76 65 20 71 75 65 72 79 20 6f  e native query o
2881c 70 74 69 6d 69 7a 65 72 20 64 6f 65 73 20 6e 6f  ptimizer does no
2881d 74 20 61 74 74 65 6d 70 74 0a 20 20 2a 2a 20 74  t attempt.  ** t
2881e 6f 20 64 6f 20 61 6e 79 74 68 69 6e 67 20 77 69  o do anything wi
2881f 74 68 20 4d 41 54 43 48 20 66 75 6e 63 74 69 6f  th MATCH functio
28820 6e 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 69  ns..  */.  if( i
28821 73 4d 61 74 63 68 4f 66 43 6f 6c 75 6d 6e 28 70  sMatchOfColumn(p
28822 45 78 70 72 29 20 29 7b 0a 20 20 20 20 69 6e 74  Expr) ){.    int
28823 20 69 64 78 4e 65 77 3b 0a 20 20 20 20 45 78 70   idxNew;.    Exp
28824 72 20 2a 70 52 69 67 68 74 2c 20 2a 70 4c 65 66  r *pRight, *pLef
28825 74 3b 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d  t;.    WhereTerm
28826 20 2a 70 4e 65 77 54 65 72 6d 3b 0a 20 20 20 20   *pNewTerm;.    
28827 42 69 74 6d 61 73 6b 20 70 72 65 72 65 71 43 6f  Bitmask prereqCo
28828 6c 75 6d 6e 2c 20 70 72 65 72 65 71 45 78 70 72  lumn, prereqExpr
28829 3b 0a 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20  ;..    pRight = 
2882a 70 45 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 61 5b  pExpr->pList->a[
2882b 30 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 70 4c  0].pExpr;.    pL
2882c 65 66 74 20 3d 20 70 45 78 70 72 2d 3e 70 4c 69  eft = pExpr->pLi
2882d 73 74 2d 3e 61 5b 31 5d 2e 70 45 78 70 72 3b 0a  st->a[1].pExpr;.
2882e 20 20 20 20 70 72 65 72 65 71 45 78 70 72 20 3d      prereqExpr =
2882f 20 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28   exprTableUsage(
28830 70 4d 61 73 6b 53 65 74 2c 20 70 52 69 67 68 74  pMaskSet, pRight
28831 29 3b 0a 20 20 20 20 70 72 65 72 65 71 43 6f 6c  );.    prereqCol
28832 75 6d 6e 20 3d 20 65 78 70 72 54 61 62 6c 65 55  umn = exprTableU
28833 73 61 67 65 28 70 4d 61 73 6b 53 65 74 2c 20 70  sage(pMaskSet, p
28834 4c 65 66 74 29 3b 0a 20 20 20 20 69 66 28 20 28  Left);.    if( (
28835 70 72 65 72 65 71 45 78 70 72 20 26 20 70 72 65  prereqExpr & pre
28836 72 65 71 43 6f 6c 75 6d 6e 29 3d 3d 30 20 29 7b  reqColumn)==0 ){
28837 0a 20 20 20 20 20 20 45 78 70 72 20 2a 70 4e 65  .      Expr *pNe
28838 77 45 78 70 72 3b 0a 20 20 20 20 20 20 70 4e 65  wExpr;.      pNe
28839 77 45 78 70 72 20 3d 20 73 71 6c 69 74 65 33 45  wExpr = sqlite3E
2883a 78 70 72 28 64 62 2c 20 54 4b 5f 4d 41 54 43 48  xpr(db, TK_MATCH
2883b 2c 20 30 2c 20 73 71 6c 69 74 65 33 45 78 70 72  , 0, sqlite3Expr
2883c 44 75 70 28 64 62 2c 20 70 52 69 67 68 74 29 2c  Dup(db, pRight),
2883d 20 30 29 3b 0a 20 20 20 20 20 20 69 64 78 4e 65   0);.      idxNe
2883e 77 20 3d 20 77 68 65 72 65 43 6c 61 75 73 65 49  w = whereClauseI
2883f 6e 73 65 72 74 28 70 57 43 2c 20 70 4e 65 77 45  nsert(pWC, pNewE
28840 78 70 72 2c 20 54 45 52 4d 5f 56 49 52 54 55 41  xpr, TERM_VIRTUA
28841 4c 7c 54 45 52 4d 5f 44 59 4e 41 4d 49 43 29 3b  L|TERM_DYNAMIC);
28842 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72 6d 20  .      pNewTerm 
28843 3d 20 26 70 57 43 2d 3e 61 5b 69 64 78 4e 65 77  = &pWC->a[idxNew
28844 5d 3b 0a 20 20 20 20 20 20 70 4e 65 77 54 65 72  ];.      pNewTer
28845 6d 2d 3e 70 72 65 72 65 71 52 69 67 68 74 20 3d  m->prereqRight =
28846 20 70 72 65 72 65 71 45 78 70 72 3b 0a 20 20 20   prereqExpr;.   
28847 20 20 20 70 4e 65 77 54 65 72 6d 2d 3e 6c 65 66     pNewTerm->lef
28848 74 43 75 72 73 6f 72 20 3d 20 70 4c 65 66 74 2d  tCursor = pLeft-
28849 3e 69 54 61 62 6c 65 3b 0a 20 20 20 20 20 20 70  >iTable;.      p
2884a 4e 65 77 54 65 72 6d 2d 3e 6c 65 66 74 43 6f 6c  NewTerm->leftCol
2884b 75 6d 6e 20 3d 20 70 4c 65 66 74 2d 3e 69 43 6f  umn = pLeft->iCo
2884c 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 4e 65 77  lumn;.      pNew
2884d 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20  Term->eOperator 
2884e 3d 20 57 4f 5f 4d 41 54 43 48 3b 0a 20 20 20 20  = WO_MATCH;.    
2884f 20 20 70 4e 65 77 54 65 72 6d 2d 3e 69 50 61 72    pNewTerm->iPar
28850 65 6e 74 20 3d 20 69 64 78 54 65 72 6d 3b 0a 20  ent = idxTerm;. 
28851 20 20 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57       pTerm = &pW
28852 43 2d 3e 61 5b 69 64 78 54 65 72 6d 5d 3b 0a 20  C->a[idxTerm];. 
28853 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 43 68 69       pTerm->nChi
28854 6c 64 20 3d 20 31 3b 0a 20 20 20 20 20 20 70 54  ld = 1;.      pT
28855 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 54 45  erm->flags |= TE
28856 52 4d 5f 43 4f 50 49 45 44 3b 0a 20 20 20 20 20  RM_COPIED;.     
28857 20 70 4e 65 77 54 65 72 6d 2d 3e 70 72 65 72 65   pNewTerm->prere
28858 71 41 6c 6c 20 3d 20 70 54 65 72 6d 2d 3e 70 72  qAll = pTerm->pr
28859 65 72 65 71 41 6c 6c 3b 0a 20 20 20 20 7d 0a 20  ereqAll;.    }. 
2885a 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c   }.#endif /* SQL
2885b 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c  ITE_OMIT_VIRTUAL
2885c 54 41 42 4c 45 20 2a 2f 0a 0a 20 20 2f 2a 20 50  TABLE */..  /* P
2885d 72 65 76 65 6e 74 20 4f 4e 20 63 6c 61 75 73 65  revent ON clause
2885e 20 74 65 72 6d 73 20 6f 66 20 61 20 4c 45 46 54   terms of a LEFT
2885f 20 4a 4f 49 4e 20 66 72 6f 6d 20 62 65 69 6e 67   JOIN from being
28860 20 75 73 65 64 20 74 6f 20 64 72 69 76 65 0a 20   used to drive. 
28861 20 2a 2a 20 61 6e 20 69 6e 64 65 78 20 66 6f 72   ** an index for
28862 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65 20 6c   tables to the l
28863 65 66 74 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e  eft of the join.
28864 0a 20 20 2a 2f 0a 20 20 70 54 65 72 6d 2d 3e 70  .  */.  pTerm->p
28865 72 65 72 65 71 52 69 67 68 74 20 7c 3d 20 65 78  rereqRight |= ex
28866 74 72 61 52 69 67 68 74 3b 0a 7d 0a 0a 2f 2a 0a  traRight;.}../*.
28867 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
28868 66 20 61 6e 79 20 6f 66 20 74 68 65 20 65 78 70  f any of the exp
28869 72 65 73 73 69 6f 6e 73 20 69 6e 20 70 4c 69 73  ressions in pLis
2886a 74 2d 3e 61 5b 69 46 69 72 73 74 2e 2e 2e 5d 20  t->a[iFirst...] 
2886b 63 6f 6e 74 61 69 6e 0a 2a 2a 20 61 20 72 65 66  contain.** a ref
2886c 65 72 65 6e 63 65 20 74 6f 20 61 6e 79 20 74 61  erence to any ta
2886d 62 6c 65 20 6f 74 68 65 72 20 74 68 61 6e 20 74  ble other than t
2886e 68 65 20 69 42 61 73 65 20 74 61 62 6c 65 2e 0a  he iBase table..
2886f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 65  */.static int re
28870 66 65 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62  ferencesOtherTab
28871 6c 65 73 28 0a 20 20 45 78 70 72 4c 69 73 74 20  les(.  ExprList 
28872 2a 70 4c 69 73 74 2c 20 20 20 20 20 20 20 20 20  *pList,         
28873 20 2f 2a 20 53 65 61 72 63 68 20 65 78 70 72 65   /* Search expre
28874 73 73 69 6f 6e 73 20 69 6e 20 74 68 73 20 6c 69  ssions in ths li
28875 73 74 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b  st */.  ExprMask
28876 53 65 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20  Set *pMaskSet,  
28877 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20 66 72 6f    /* Mapping fro
28878 6d 20 74 61 62 6c 65 73 20 74 6f 20 62 69 74 6d  m tables to bitm
28879 61 70 73 20 2a 2f 0a 20 20 69 6e 74 20 69 46 69  aps */.  int iFi
2887a 72 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  rst,            
2887b 20 20 20 2f 2a 20 42 65 20 73 65 61 72 63 68 69     /* Be searchi
2887c 6e 67 20 77 69 74 68 20 74 68 65 20 69 46 69 72  ng with the iFir
2887d 73 74 2d 74 68 20 65 78 70 72 65 73 73 69 6f 6e  st-th expression
2887e 20 2a 2f 0a 20 20 69 6e 74 20 69 42 61 73 65 20   */.  int iBase 
2887f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28880 2f 2a 20 49 67 6e 6f 72 65 20 72 65 66 65 72 65  /* Ignore refere
28881 6e 63 65 73 20 74 6f 20 74 68 69 73 20 74 61 62  nces to this tab
28882 6c 65 20 2a 2f 0a 29 7b 0a 20 20 42 69 74 6d 61  le */.){.  Bitma
28883 73 6b 20 61 6c 6c 6f 77 65 64 20 3d 20 7e 67 65  sk allowed = ~ge
28884 74 4d 61 73 6b 28 70 4d 61 73 6b 53 65 74 2c 20  tMask(pMaskSet, 
28885 69 42 61 73 65 29 3b 0a 20 20 77 68 69 6c 65 28  iBase);.  while(
28886 20 69 46 69 72 73 74 3c 70 4c 69 73 74 2d 3e 6e   iFirst<pList->n
28887 45 78 70 72 20 29 7b 0a 20 20 20 20 69 66 28 20  Expr ){.    if( 
28888 28 65 78 70 72 54 61 62 6c 65 55 73 61 67 65 28  (exprTableUsage(
28889 70 4d 61 73 6b 53 65 74 2c 20 70 4c 69 73 74 2d  pMaskSet, pList-
2888a 3e 61 5b 69 46 69 72 73 74 2b 2b 5d 2e 70 45 78  >a[iFirst++].pEx
2888b 70 72 29 26 61 6c 6c 6f 77 65 64 29 21 3d 30 20  pr)&allowed)!=0 
2888c 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  ){.      return 
2888d 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 72  1;.    }.  }.  r
2888e 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 0a 2f 2a 0a  eturn 0;.}.../*.
2888f 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20  ** This routine 
28890 64 65 63 69 64 65 73 20 69 66 20 70 49 64 78 20  decides if pIdx 
28891 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73  can be used to s
28892 61 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52  atisfy the ORDER
28893 20 42 59 0a 2a 2a 20 63 6c 61 75 73 65 2e 20 20   BY.** clause.  
28894 49 66 20 69 74 20 63 61 6e 2c 20 69 74 20 72 65  If it can, it re
28895 74 75 72 6e 73 20 31 2e 20 20 49 66 20 70 49 64  turns 1.  If pId
28896 78 20 63 61 6e 6e 6f 74 20 73 61 74 69 73 66 79  x cannot satisfy
28897 20 74 68 65 0a 2a 2a 20 4f 52 44 45 52 20 42 59   the.** ORDER BY
28898 20 63 6c 61 75 73 65 2c 20 74 68 69 73 20 72 6f   clause, this ro
28899 75 74 69 6e 65 20 72 65 74 75 72 6e 73 20 30 2e  utine returns 0.
2889a 0a 2a 2a 0a 2a 2a 20 70 4f 72 64 65 72 42 79 20  .**.** pOrderBy 
2889b 69 73 20 61 6e 20 4f 52 44 45 52 20 42 59 20 63  is an ORDER BY c
2889c 6c 61 75 73 65 20 66 72 6f 6d 20 61 20 53 45 4c  lause from a SEL
2889d 45 43 54 20 73 74 61 74 65 6d 65 6e 74 2e 20 20  ECT statement.  
2889e 70 54 61 62 20 69 73 20 74 68 65 0a 2a 2a 20 6c  pTab is the.** l
2889f 65 66 74 2d 6d 6f 73 74 20 74 61 62 6c 65 20 69  eft-most table i
288a0 6e 20 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73  n the FROM claus
288a1 65 20 6f 66 20 74 68 61 74 20 73 61 6d 65 20 53  e of that same S
288a2 45 4c 45 43 54 20 73 74 61 74 65 6d 65 6e 74 20  ELECT statement 
288a3 61 6e 64 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65  and.** the table
288a4 20 68 61 73 20 61 20 63 75 72 73 6f 72 20 6e 75   has a cursor nu
288a5 6d 62 65 72 20 6f 66 20 22 62 61 73 65 22 2e 20  mber of "base". 
288a6 20 70 49 64 78 20 69 73 20 61 6e 20 69 6e 64 65   pIdx is an inde
288a7 78 20 6f 6e 20 70 54 61 62 2e 0a 2a 2a 0a 2a 2a  x on pTab..**.**
288a8 20 6e 45 71 43 6f 6c 20 69 73 20 74 68 65 20 6e   nEqCol is the n
288a9 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
288aa 20 6f 66 20 70 49 64 78 20 74 68 61 74 20 61 72   of pIdx that ar
288ab 65 20 75 73 65 64 20 61 73 20 65 71 75 61 6c 69  e used as equali
288ac 74 79 0a 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74  ty.** constraint
288ad 73 2e 20 20 41 6e 79 20 6f 66 20 74 68 65 73 65  s.  Any of these
288ae 20 63 6f 6c 75 6d 6e 73 20 6d 61 79 20 62 65 20   columns may be 
288af 6d 69 73 73 69 6e 67 20 66 72 6f 6d 20 74 68 65  missing from the
288b0 20 4f 52 44 45 52 20 42 59 0a 2a 2a 20 63 6c 61   ORDER BY.** cla
288b1 75 73 65 20 61 6e 64 20 74 68 65 20 6d 61 74 63  use and the matc
288b2 68 20 63 61 6e 20 73 74 69 6c 6c 20 62 65 20 61  h can still be a
288b3 20 73 75 63 63 65 73 73 2e 0a 2a 2a 0a 2a 2a 20   success..**.** 
288b4 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68 65  All terms of the
288b5 20 4f 52 44 45 52 20 42 59 20 74 68 61 74 20 6d   ORDER BY that m
288b6 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 65  atch against the
288b7 20 69 6e 64 65 78 20 6d 75 73 74 20 62 65 20 65   index must be e
288b8 69 74 68 65 72 0a 2a 2a 20 41 53 43 20 6f 72 20  ither.** ASC or 
288b9 44 45 53 43 2e 20 20 28 54 65 72 6d 73 20 6f 66  DESC.  (Terms of
288ba 20 74 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c   the ORDER BY cl
288bb 61 75 73 65 20 70 61 73 74 20 74 68 65 20 65 6e  ause past the en
288bc 64 20 6f 66 20 61 20 55 4e 49 51 55 45 0a 2a 2a  d of a UNIQUE.**
288bd 20 69 6e 64 65 78 20 64 6f 20 6e 6f 74 20 6e 65   index do not ne
288be 65 64 20 74 6f 20 73 61 74 69 73 66 79 20 74 68  ed to satisfy th
288bf 69 73 20 63 6f 6e 73 74 72 61 69 6e 74 2e 29 20  is constraint.) 
288c0 20 54 68 65 20 2a 70 62 52 65 76 20 76 61 6c 75   The *pbRev valu
288c1 65 20 69 73 0a 2a 2a 20 73 65 74 20 74 6f 20 31  e is.** set to 1
288c2 20 69 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   if the ORDER BY
288c3 20 63 6c 61 75 73 65 20 69 73 20 61 6c 6c 20 44   clause is all D
288c4 45 53 43 20 61 6e 64 20 69 74 20 69 73 20 73 65  ESC and it is se
288c5 74 20 74 6f 20 30 20 69 66 0a 2a 2a 20 74 68 65  t to 0 if.** the
288c6 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
288c7 20 69 73 20 61 6c 6c 20 41 53 43 2e 0a 2a 2f 0a   is all ASC..*/.
288c8 73 74 61 74 69 63 20 69 6e 74 20 69 73 53 6f 72  static int isSor
288c9 74 69 6e 67 49 6e 64 65 78 28 0a 20 20 50 61 72  tingIndex(.  Par
288ca 73 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20  se *pParse,     
288cb 20 20 20 20 20 2f 2a 20 50 61 72 73 69 6e 67 20       /* Parsing 
288cc 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 45 78 70  context */.  Exp
288cd 72 4d 61 73 6b 53 65 74 20 2a 70 4d 61 73 6b 53  rMaskSet *pMaskS
288ce 65 74 2c 20 20 2f 2a 20 4d 61 70 70 69 6e 67 20  et,  /* Mapping 
288cf 66 72 6f 6d 20 74 61 62 6c 65 20 69 6e 64 69 63  from table indic
288d0 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20 2a 2f  es to bitmaps */
288d1 0a 20 20 49 6e 64 65 78 20 2a 70 49 64 78 2c 20  .  Index *pIdx, 
288d2 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
288d3 65 20 69 6e 64 65 78 20 77 65 20 61 72 65 20 74  e index we are t
288d4 65 73 74 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  esting */.  int 
288d5 62 61 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  base,           
288d6 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e 75      /* Cursor nu
288d7 6d 62 65 72 20 66 6f 72 20 74 68 65 20 74 61 62  mber for the tab
288d8 6c 65 20 74 6f 20 62 65 20 73 6f 72 74 65 64 20  le to be sorted 
288d9 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20 2a 70  */.  ExprList *p
288da 4f 72 64 65 72 42 79 2c 20 20 20 20 20 2f 2a 20  OrderBy,     /* 
288db 54 68 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61  The ORDER BY cla
288dc 75 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6e 45 71  use */.  int nEq
288dd 43 6f 6c 2c 20 20 20 20 20 20 20 20 20 20 20 20  Col,            
288de 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 69 6e   /* Number of in
288df 64 65 78 20 63 6f 6c 75 6d 6e 73 20 77 69 74 68  dex columns with
288e0 20 3d 3d 20 63 6f 6e 73 74 72 61 69 6e 74 73 20   == constraints 
288e1 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76 20  */.  int *pbRev 
288e2 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
288e3 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44 45  Set to 1 if ORDE
288e4 52 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f 0a  R BY is DESC */.
288e5 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 20 20  ){.  int i, j;  
288e6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
288e7 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75       /* Loop cou
288e8 6e 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 73  nters */.  int s
288e9 6f 72 74 4f 72 64 65 72 20 3d 20 30 3b 20 20 20  ortOrder = 0;   
288ea 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 58 4f             /* XO
288eb 52 20 6f 66 20 69 6e 64 65 78 20 61 6e 64 20 4f  R of index and O
288ec 52 44 45 52 20 42 59 20 73 6f 72 74 20 64 69 72  RDER BY sort dir
288ed 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e 74 20  ection */.  int 
288ee 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20  nTerm;          
288ef 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
288f0 75 6d 62 65 72 20 6f 66 20 4f 52 44 45 52 20 42  umber of ORDER B
288f1 59 20 74 65 72 6d 73 20 2a 2f 0a 20 20 73 74 72  Y terms */.  str
288f2 75 63 74 20 45 78 70 72 4c 69 73 74 5f 69 74 65  uct ExprList_ite
288f3 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 2f 2a 20  m *pTerm;    /* 
288f4 41 20 74 65 72 6d 20 6f 66 20 74 68 65 20 4f 52  A term of the OR
288f5 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 2a 2f  DER BY clause */
288f6 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
288f7 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 0a 20 20   pParse->db;..  
288f8 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42 79  assert( pOrderBy
288f9 21 3d 30 20 29 3b 0a 20 20 6e 54 65 72 6d 20 3d  !=0 );.  nTerm =
288fa 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72   pOrderBy->nExpr
288fb 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 54 65 72  ;.  assert( nTer
288fc 6d 3e 30 20 29 3b 0a 0a 20 20 2f 2a 20 4d 61 74  m>0 );..  /* Mat
288fd 63 68 20 74 65 72 6d 73 20 6f 66 20 74 68 65 20  ch terms of the 
288fe 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
288ff 61 67 61 69 6e 73 74 20 63 6f 6c 75 6d 6e 73 20  against columns 
28900 6f 66 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65  of.  ** the inde
28901 78 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 4e 6f 74  x..  **.  ** Not
28902 65 20 74 68 61 74 20 69 6e 64 69 63 65 73 20 68  e that indices h
28903 61 76 65 20 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ave pIdx->nColum
28904 6e 20 72 65 67 75 6c 61 72 20 63 6f 6c 75 6d 6e  n regular column
28905 73 20 70 6c 75 73 0a 20 20 2a 2a 20 6f 6e 65 20  s plus.  ** one 
28906 61 64 64 69 74 69 6f 6e 61 6c 20 63 6f 6c 75 6d  additional colum
28907 6e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65  n containing the
28908 20 72 6f 77 69 64 2e 20 20 54 68 65 20 72 6f 77   rowid.  The row
28909 69 64 20 63 6f 6c 75 6d 6e 0a 20 20 2a 2a 20 6f  id column.  ** o
2890a 66 20 74 68 65 20 69 6e 64 65 78 20 69 73 20 61  f the index is a
2890b 6c 73 6f 20 61 6c 6c 6f 77 65 64 20 74 6f 20 6d  lso allowed to m
2890c 61 74 63 68 20 61 67 61 69 6e 73 74 20 74 68 65  atch against the
2890d 20 4f 52 44 45 52 20 42 59 0a 20 20 2a 2a 20 63   ORDER BY.  ** c
2890e 6c 61 75 73 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f  lause..  */.  fo
2890f 72 28 69 3d 6a 3d 30 2c 20 70 54 65 72 6d 3d 70  r(i=j=0, pTerm=p
28910 4f 72 64 65 72 42 79 2d 3e 61 3b 20 6a 3c 6e 54  OrderBy->a; j<nT
28911 65 72 6d 20 26 26 20 69 3c 3d 70 49 64 78 2d 3e  erm && i<=pIdx->
28912 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20  nColumn; i++){. 
28913 20 20 20 45 78 70 72 20 2a 70 45 78 70 72 3b 20     Expr *pExpr; 
28914 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78 70        /* The exp
28915 72 65 73 73 69 6f 6e 20 6f 66 20 74 68 65 20 4f  ression of the O
28916 52 44 45 52 20 42 59 20 70 54 65 72 6d 20 2a 2f  RDER BY pTerm */
28917 0a 20 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43  .    CollSeq *pC
28918 6f 6c 6c 3b 20 20 20 20 2f 2a 20 54 68 65 20 63  oll;    /* The c
28919 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63  ollating sequenc
2891a 65 20 6f 66 20 70 45 78 70 72 20 2a 2f 0a 20 20  e of pExpr */.  
2891b 20 20 69 6e 74 20 74 65 72 6d 53 6f 72 74 4f 72    int termSortOr
2891c 64 65 72 3b 20 2f 2a 20 53 6f 72 74 20 6f 72 64  der; /* Sort ord
2891d 65 72 20 66 6f 72 20 74 68 69 73 20 74 65 72 6d  er for this term
2891e 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 43 6f 6c   */.    int iCol
2891f 75 6d 6e 3b 20 20 20 20 20 20 20 2f 2a 20 54 68  umn;       /* Th
28920 65 20 69 2d 74 68 20 63 6f 6c 75 6d 6e 20 6f 66  e i-th column of
28921 20 74 68 65 20 69 6e 64 65 78 2e 20 20 2d 31 20   the index.  -1 
28922 66 6f 72 20 72 6f 77 69 64 20 2a 2f 0a 20 20 20  for rowid */.   
28923 20 69 6e 74 20 69 53 6f 72 74 4f 72 64 65 72 3b   int iSortOrder;
28924 20 20 20 20 2f 2a 20 31 20 66 6f 72 20 44 45 53      /* 1 for DES
28925 43 2c 20 30 20 66 6f 72 20 41 53 43 20 6f 6e 20  C, 0 for ASC on 
28926 74 68 65 20 69 2d 74 68 20 69 6e 64 65 78 20 74  the i-th index t
28927 65 72 6d 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74  erm */.    const
28928 20 63 68 61 72 20 2a 7a 43 6f 6c 6c 3b 20 2f 2a   char *zColl; /*
28929 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 63 6f 6c   Name of the col
2892a 6c 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 20  lating sequence 
2892b 66 6f 72 20 69 2d 74 68 20 69 6e 64 65 78 20 74  for i-th index t
2892c 65 72 6d 20 2a 2f 0a 0a 20 20 20 20 70 45 78 70  erm */..    pExp
2892d 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72  r = pTerm->pExpr
2892e 3b 0a 20 20 20 20 69 66 28 20 70 45 78 70 72 2d  ;.    if( pExpr-
2892f 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d 4e 20 7c  >op!=TK_COLUMN |
28930 7c 20 70 45 78 70 72 2d 3e 69 54 61 62 6c 65 21  | pExpr->iTable!
28931 3d 62 61 73 65 20 29 7b 0a 20 20 20 20 20 20 2f  =base ){.      /
28932 2a 20 43 61 6e 20 6e 6f 74 20 75 73 65 20 61 6e  * Can not use an
28933 20 69 6e 64 65 78 20 73 6f 72 74 20 6f 6e 20 61   index sort on a
28934 6e 79 74 68 69 6e 67 20 74 68 61 74 20 69 73 20  nything that is 
28935 6e 6f 74 20 61 20 63 6f 6c 75 6d 6e 20 69 6e 20  not a column in 
28936 74 68 65 0a 20 20 20 20 20 20 2a 2a 20 6c 65 66  the.      ** lef
28937 74 2d 6d 6f 73 74 20 74 61 62 6c 65 20 6f 66 20  t-most table of 
28938 74 68 65 20 46 52 4f 4d 20 63 6c 61 75 73 65 20  the FROM clause 
28939 2a 2f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  */.      break;.
2893a 20 20 20 20 7d 0a 20 20 20 20 70 43 6f 6c 6c 20      }.    pColl 
2893b 3d 20 73 71 6c 69 74 65 33 45 78 70 72 43 6f 6c  = sqlite3ExprCol
2893c 6c 53 65 71 28 70 50 61 72 73 65 2c 20 70 45 78  lSeq(pParse, pEx
2893d 70 72 29 3b 0a 20 20 20 20 69 66 28 20 21 70 43  pr);.    if( !pC
2893e 6f 6c 6c 20 29 7b 0a 20 20 20 20 20 20 70 43 6f  oll ){.      pCo
2893f 6c 6c 20 3d 20 64 62 2d 3e 70 44 66 6c 74 43 6f  ll = db->pDfltCo
28940 6c 6c 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  ll;.    }.    if
28941 28 20 69 3c 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  ( i<pIdx->nColum
28942 6e 20 29 7b 0a 20 20 20 20 20 20 69 43 6f 6c 75  n ){.      iColu
28943 6d 6e 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c  mn = pIdx->aiCol
28944 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20 69 66  umn[i];.      if
28945 28 20 69 43 6f 6c 75 6d 6e 3d 3d 70 49 64 78 2d  ( iColumn==pIdx-
28946 3e 70 54 61 62 6c 65 2d 3e 69 50 4b 65 79 20 29  >pTable->iPKey )
28947 7b 0a 20 20 20 20 20 20 20 20 69 43 6f 6c 75 6d  {.        iColum
28948 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a  n = -1;.      }.
28949 20 20 20 20 20 20 69 53 6f 72 74 4f 72 64 65 72        iSortOrder
2894a 20 3d 20 70 49 64 78 2d 3e 61 53 6f 72 74 4f 72   = pIdx->aSortOr
2894b 64 65 72 5b 69 5d 3b 0a 20 20 20 20 20 20 7a 43  der[i];.      zC
2894c 6f 6c 6c 20 3d 20 70 49 64 78 2d 3e 61 7a 43 6f  oll = pIdx->azCo
2894d 6c 6c 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73 65  ll[i];.    }else
2894e 7b 0a 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 20  {.      iColumn 
2894f 3d 20 2d 31 3b 0a 20 20 20 20 20 20 69 53 6f 72  = -1;.      iSor
28950 74 4f 72 64 65 72 20 3d 20 30 3b 0a 20 20 20 20  tOrder = 0;.    
28951 20 20 7a 43 6f 6c 6c 20 3d 20 70 43 6f 6c 6c 2d    zColl = pColl-
28952 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 7d 0a 20 20  >zName;.    }.  
28953 20 20 69 66 28 20 70 45 78 70 72 2d 3e 69 43 6f    if( pExpr->iCo
28954 6c 75 6d 6e 21 3d 69 43 6f 6c 75 6d 6e 20 7c 7c  lumn!=iColumn ||
28955 20 73 71 6c 69 74 65 33 53 74 72 49 43 6d 70 28   sqlite3StrICmp(
28956 70 43 6f 6c 6c 2d 3e 7a 4e 61 6d 65 2c 20 7a 43  pColl->zName, zC
28957 6f 6c 6c 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a  oll) ){.      /*
28958 20 54 65 72 6d 20 6a 20 6f 66 20 74 68 65 20 4f   Term j of the O
28959 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 64  RDER BY clause d
2895a 6f 65 73 20 6e 6f 74 20 6d 61 74 63 68 20 63 6f  oes not match co
2895b 6c 75 6d 6e 20 69 20 6f 66 20 74 68 65 20 69 6e  lumn i of the in
2895c 64 65 78 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  dex */.      if(
2895d 20 69 3c 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20   i<nEqCol ){.   
2895e 20 20 20 20 20 2f 2a 20 49 66 20 61 6e 20 69 6e       /* If an in
2895f 64 65 78 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  dex column that 
28960 69 73 20 63 6f 6e 73 74 72 61 69 6e 65 64 20 62  is constrained b
28961 79 20 3d 3d 20 66 61 69 6c 73 20 74 6f 20 6d 61  y == fails to ma
28962 74 63 68 20 61 6e 0a 20 20 20 20 20 20 20 20 2a  tch an.        *
28963 2a 20 4f 52 44 45 52 20 42 59 20 74 65 72 6d 2c  * ORDER BY term,
28964 20 74 68 61 74 20 69 73 20 4f 4b 2e 20 20 4a 75   that is OK.  Ju
28965 73 74 20 69 67 6e 6f 72 65 20 74 68 61 74 20 63  st ignore that c
28966 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20 69 6e 64  olumn of the ind
28967 65 78 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  ex.        */.  
28968 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
28969 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2896a 69 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e  i==pIdx->nColumn
2896b 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49   ){.        /* I
2896c 6e 64 65 78 20 63 6f 6c 75 6d 6e 20 69 20 69 73  ndex column i is
2896d 20 74 68 65 20 72 6f 77 69 64 2e 20 20 41 6c 6c   the rowid.  All
2896e 20 6f 74 68 65 72 20 74 65 72 6d 73 20 6d 61 74   other terms mat
2896f 63 68 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 62  ch. */.        b
28970 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 65 6c 73  reak;.      }els
28971 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 66  e{.        /* If
28972 20 61 6e 20 69 6e 64 65 78 20 63 6f 6c 75 6d 6e   an index column
28973 20 66 61 69 6c 73 20 74 6f 20 6d 61 74 63 68 20   fails to match 
28974 61 6e 64 20 69 73 20 6e 6f 74 20 63 6f 6e 73 74  and is not const
28975 72 61 69 6e 65 64 20 62 79 20 3d 3d 0a 20 20 20  rained by ==.   
28976 20 20 20 20 20 2a 2a 20 74 68 65 6e 20 74 68 65       ** then the
28977 20 69 6e 64 65 78 20 63 61 6e 6e 6f 74 20 73 61   index cannot sa
28978 74 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20  tisfy the ORDER 
28979 42 59 20 63 6f 6e 73 74 72 61 69 6e 74 2e 0a 20  BY constraint.. 
2897a 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20         */.      
2897b 20 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20    return 0;.    
2897c 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73    }.    }.    as
2897d 73 65 72 74 28 20 70 49 64 78 2d 3e 61 53 6f 72  sert( pIdx->aSor
2897e 74 4f 72 64 65 72 21 3d 30 20 29 3b 0a 20 20 20  tOrder!=0 );.   
2897f 20 61 73 73 65 72 74 28 20 70 54 65 72 6d 2d 3e   assert( pTerm->
28980 73 6f 72 74 4f 72 64 65 72 3d 3d 30 20 7c 7c 20  sortOrder==0 || 
28981 70 54 65 72 6d 2d 3e 73 6f 72 74 4f 72 64 65 72  pTerm->sortOrder
28982 3d 3d 31 20 29 3b 0a 20 20 20 20 61 73 73 65 72  ==1 );.    asser
28983 74 28 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d 30  t( iSortOrder==0
28984 20 7c 7c 20 69 53 6f 72 74 4f 72 64 65 72 3d 3d   || iSortOrder==
28985 31 20 29 3b 0a 20 20 20 20 74 65 72 6d 53 6f 72  1 );.    termSor
28986 74 4f 72 64 65 72 20 3d 20 69 53 6f 72 74 4f 72  tOrder = iSortOr
28987 64 65 72 20 5e 20 70 54 65 72 6d 2d 3e 73 6f 72  der ^ pTerm->sor
28988 74 4f 72 64 65 72 3b 0a 20 20 20 20 69 66 28 20  tOrder;.    if( 
28989 69 3e 6e 45 71 43 6f 6c 20 29 7b 0a 20 20 20 20  i>nEqCol ){.    
2898a 20 20 69 66 28 20 74 65 72 6d 53 6f 72 74 4f 72    if( termSortOr
2898b 64 65 72 21 3d 73 6f 72 74 4f 72 64 65 72 20 29  der!=sortOrder )
2898c 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64  {.        /* Ind
2898d 69 63 65 73 20 63 61 6e 20 6f 6e 6c 79 20 62 65  ices can only be
2898e 20 75 73 65 64 20 69 66 20 61 6c 6c 20 4f 52 44   used if all ORD
2898f 45 52 20 42 59 20 74 65 72 6d 73 20 70 61 73 74  ER BY terms past
28990 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
28991 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
28992 69 6e 74 73 20 61 72 65 20 61 6c 6c 20 65 69 74  ints are all eit
28993 68 65 72 20 44 45 53 43 20 6f 72 20 41 53 43 2e  her DESC or ASC.
28994 20 2a 2f 0a 20 20 20 20 20 20 20 20 72 65 74 75   */.        retu
28995 72 6e 20 30 3b 0a 20 20 20 20 20 20 7d 0a 20 20  rn 0;.      }.  
28996 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
28997 6f 72 74 4f 72 64 65 72 20 3d 20 74 65 72 6d 53  ortOrder = termS
28998 6f 72 74 4f 72 64 65 72 3b 0a 20 20 20 20 7d 0a  ortOrder;.    }.
28999 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 70 54 65      j++;.    pTe
2899a 72 6d 2b 2b 3b 0a 20 20 20 20 69 66 28 20 69 43  rm++;.    if( iC
2899b 6f 6c 75 6d 6e 3c 30 20 26 26 20 21 72 65 66 65  olumn<0 && !refe
2899c 72 65 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65  rencesOtherTable
2899d 73 28 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73  s(pOrderBy, pMas
2899e 6b 53 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29  kSet, j, base) )
2899f 7b 0a 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68  {.      /* If th
289a0 65 20 69 6e 64 65 78 65 64 20 63 6f 6c 75 6d 6e  e indexed column
289a1 20 69 73 20 74 68 65 20 70 72 69 6d 61 72 79 20   is the primary 
289a2 6b 65 79 20 61 6e 64 20 65 76 65 72 79 74 68 69  key and everythi
289a3 6e 67 20 6d 61 74 63 68 65 73 0a 20 20 20 20 20  ng matches.     
289a4 20 2a 2a 20 73 6f 20 66 61 72 20 61 6e 64 20 6e   ** so far and n
289a5 6f 6e 65 20 6f 66 20 74 68 65 20 4f 52 44 45 52  one of the ORDER
289a6 20 42 59 20 74 65 72 6d 73 20 74 6f 20 74 68 65   BY terms to the
289a7 20 72 69 67 68 74 20 72 65 66 65 72 65 6e 63 65   right reference
289a8 20 6f 74 68 65 72 0a 20 20 20 20 20 20 2a 2a 20   other.      ** 
289a9 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20 6a 6f  tables in the jo
289aa 69 6e 2c 20 74 68 65 6e 20 77 65 20 61 72 65 20  in, then we are 
289ab 61 73 73 75 72 65 64 20 74 68 61 74 20 74 68 65  assured that the
289ac 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20 75 73   index can be us
289ad 65 64 20 0a 20 20 20 20 20 20 2a 2a 20 74 6f 20  ed .      ** to 
289ae 73 6f 72 74 20 62 65 63 61 75 73 65 20 74 68 65  sort because the
289af 20 70 72 69 6d 61 72 79 20 6b 65 79 20 69 73 20   primary key is 
289b0 75 6e 69 71 75 65 20 61 6e 64 20 73 6f 20 6e 6f  unique and so no
289b1 6e 65 20 6f 66 20 74 68 65 20 6f 74 68 65 72 0a  ne of the other.
289b2 20 20 20 20 20 20 2a 2a 20 63 6f 6c 75 6d 6e 73        ** columns
289b3 20 77 69 6c 6c 20 6d 61 6b 65 20 61 6e 79 20 64   will make any d
289b4 69 66 66 65 72 65 6e 63 65 0a 20 20 20 20 20 20  ifference.      
289b5 2a 2f 0a 20 20 20 20 20 20 6a 20 3d 20 6e 54 65  */.      j = nTe
289b6 72 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rm;.    }.  }.. 
289b7 20 2a 70 62 52 65 76 20 3d 20 73 6f 72 74 4f 72   *pbRev = sortOr
289b8 64 65 72 21 3d 30 3b 0a 20 20 69 66 28 20 6a 3e  der!=0;.  if( j>
289b9 3d 6e 54 65 72 6d 20 29 7b 0a 20 20 20 20 2f 2a  =nTerm ){.    /*
289ba 20 41 6c 6c 20 74 65 72 6d 73 20 6f 66 20 74 68   All terms of th
289bb 65 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73  e ORDER BY claus
289bc 65 20 61 72 65 20 63 6f 76 65 72 65 64 20 62 79  e are covered by
289bd 20 74 68 69 73 20 69 6e 64 65 78 20 73 6f 0a 20   this index so. 
289be 20 20 20 2a 2a 20 74 68 69 73 20 69 6e 64 65 78     ** this index
289bf 20 63 61 6e 20 62 65 20 75 73 65 64 20 66 6f 72   can be used for
289c0 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a 20 20 20   sorting. */.   
289c1 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20   return 1;.  }. 
289c2 20 69 66 28 20 70 49 64 78 2d 3e 6f 6e 45 72 72   if( pIdx->onErr
289c3 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26 20 69  or!=OE_None && i
289c4 3d 3d 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 0a  ==pIdx->nColumn.
289c5 20 20 20 20 20 20 26 26 20 21 72 65 66 65 72 65        && !refere
289c6 6e 63 65 73 4f 74 68 65 72 54 61 62 6c 65 73 28  ncesOtherTables(
289c7 70 4f 72 64 65 72 42 79 2c 20 70 4d 61 73 6b 53  pOrderBy, pMaskS
289c8 65 74 2c 20 6a 2c 20 62 61 73 65 29 20 29 7b 0a  et, j, base) ){.
289c9 20 20 20 20 2f 2a 20 41 6c 6c 20 74 65 72 6d 73      /* All terms
289ca 20 6f 66 20 74 68 69 73 20 69 6e 64 65 78 20 6d   of this index m
289cb 61 74 63 68 20 73 6f 6d 65 20 70 72 65 66 69 78  atch some prefix
289cc 20 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59   of the ORDER BY
289cd 20 63 6c 61 75 73 65 0a 20 20 20 20 2a 2a 20 61   clause.    ** a
289ce 6e 64 20 74 68 65 20 69 6e 64 65 78 20 69 73 20  nd the index is 
289cf 55 4e 49 51 55 45 20 61 6e 64 20 6e 6f 20 74 65  UNIQUE and no te
289d0 72 6d 73 20 6f 6e 20 74 68 65 20 74 61 69 6c 20  rms on the tail 
289d1 6f 66 20 74 68 65 20 4f 52 44 45 52 20 42 59 0a  of the ORDER BY.
289d2 20 20 20 20 2a 2a 20 63 6c 61 75 73 65 20 72 65      ** clause re
289d3 66 65 72 65 6e 63 65 20 6f 74 68 65 72 20 74 61  ference other ta
289d4 62 6c 65 73 20 69 6e 20 61 20 6a 6f 69 6e 2e 20  bles in a join. 
289d5 20 49 66 20 74 68 69 73 20 69 73 20 61 6c 6c 20   If this is all 
289d6 74 72 75 65 20 74 68 65 6e 0a 20 20 20 20 2a 2a  true then.    **
289d7 20 74 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c   the order by cl
289d8 61 75 73 65 20 69 73 20 73 75 70 65 72 66 6c 75  ause is superflu
289d9 6f 75 73 2e 20 2a 2f 0a 20 20 20 20 72 65 74 75  ous. */.    retu
289da 72 6e 20 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75  rn 1;.  }.  retu
289db 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43  rn 0;.}../*.** C
289dc 68 65 63 6b 20 74 61 62 6c 65 20 74 6f 20 73 65  heck table to se
289dd 65 20 69 66 20 74 68 65 20 4f 52 44 45 52 20 42  e if the ORDER B
289de 59 20 63 6c 61 75 73 65 20 69 6e 20 70 4f 72 64  Y clause in pOrd
289df 65 72 42 79 20 63 61 6e 20 62 65 20 73 61 74 69  erBy can be sati
289e0 73 66 69 65 64 0a 2a 2a 20 62 79 20 73 6f 72 74  sfied.** by sort
289e1 69 6e 67 20 69 6e 20 6f 72 64 65 72 20 6f 66 20  ing in order of 
289e2 52 4f 57 49 44 2e 20 20 52 65 74 75 72 6e 20 74  ROWID.  Return t
289e3 72 75 65 20 69 66 20 73 6f 20 61 6e 64 20 73 65  rue if so and se
289e4 74 20 2a 70 62 52 65 76 20 74 6f 20 62 65 0a 2a  t *pbRev to be.*
289e5 2a 20 74 72 75 65 20 66 6f 72 20 72 65 76 65 72  * true for rever
289e6 73 65 20 52 4f 57 49 44 20 61 6e 64 20 66 61 6c  se ROWID and fal
289e7 73 65 20 66 6f 72 20 66 6f 72 77 61 72 64 20 52  se for forward R
289e8 4f 57 49 44 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73  OWID order..*/.s
289e9 74 61 74 69 63 20 69 6e 74 20 73 6f 72 74 61 62  tatic int sortab
289ea 6c 65 42 79 52 6f 77 69 64 28 0a 20 20 69 6e 74  leByRowid(.  int
289eb 20 62 61 73 65 2c 20 20 20 20 20 20 20 20 20 20   base,          
289ec 20 20 20 20 20 2f 2a 20 43 75 72 73 6f 72 20 6e       /* Cursor n
289ed 75 6d 62 65 72 20 66 6f 72 20 74 61 62 6c 65 20  umber for table 
289ee 74 6f 20 62 65 20 73 6f 72 74 65 64 20 2a 2f 0a  to be sorted */.
289ef 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
289f0 65 72 42 79 2c 20 20 20 20 20 2f 2a 20 54 68 65  erBy,     /* The
289f1 20 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65   ORDER BY clause
289f2 20 2a 2f 0a 20 20 45 78 70 72 4d 61 73 6b 53 65   */.  ExprMaskSe
289f3 74 20 2a 70 4d 61 73 6b 53 65 74 2c 20 20 2f 2a  t *pMaskSet,  /*
289f4 20 4d 61 70 70 69 6e 67 20 66 72 6f 6d 20 74 61   Mapping from ta
289f5 62 6c 65 73 20 74 6f 20 62 69 74 6d 61 70 73 20  bles to bitmaps 
289f6 2a 2f 0a 20 20 69 6e 74 20 2a 70 62 52 65 76 20  */.  int *pbRev 
289f7 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
289f8 53 65 74 20 74 6f 20 31 20 69 66 20 4f 52 44 45  Set to 1 if ORDE
289f9 52 20 42 59 20 69 73 20 44 45 53 43 20 2a 2f 0a  R BY is DESC */.
289fa 29 7b 0a 20 20 45 78 70 72 20 2a 70 3b 0a 0a 20  ){.  Expr *p;.. 
289fb 20 61 73 73 65 72 74 28 20 70 4f 72 64 65 72 42   assert( pOrderB
289fc 79 21 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74  y!=0 );.  assert
289fd 28 20 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70  ( pOrderBy->nExp
289fe 72 3e 30 20 29 3b 0a 20 20 70 20 3d 20 70 4f 72  r>0 );.  p = pOr
289ff 64 65 72 42 79 2d 3e 61 5b 30 5d 2e 70 45 78 70  derBy->a[0].pExp
28a00 72 3b 0a 20 20 69 66 28 20 70 2d 3e 6f 70 3d 3d  r;.  if( p->op==
28a01 54 4b 5f 43 4f 4c 55 4d 4e 20 26 26 20 70 2d 3e  TK_COLUMN && p->
28a02 69 54 61 62 6c 65 3d 3d 62 61 73 65 20 26 26 20  iTable==base && 
28a03 70 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 2d 31 0a 20  p->iColumn==-1. 
28a04 20 20 20 26 26 20 21 72 65 66 65 72 65 6e 63 65     && !reference
28a05 73 4f 74 68 65 72 54 61 62 6c 65 73 28 70 4f 72  sOtherTables(pOr
28a06 64 65 72 42 79 2c 20 70 4d 61 73 6b 53 65 74 2c  derBy, pMaskSet,
28a07 20 31 2c 20 62 61 73 65 29 20 29 7b 0a 20 20 20   1, base) ){.   
28a08 20 2a 70 62 52 65 76 20 3d 20 70 4f 72 64 65 72   *pbRev = pOrder
28a09 42 79 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  By->a[0].sortOrd
28a0a 65 72 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  er;.    return 1
28a0b 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 30  ;.  }.  return 0
28a0c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65 70 61  ;.}../*.** Prepa
28a0d 72 65 20 61 20 63 72 75 64 65 20 65 73 74 69 6d  re a crude estim
28a0e 61 74 65 20 6f 66 20 74 68 65 20 6c 6f 67 61 72  ate of the logar
28a0f 69 74 68 6d 20 6f 66 20 74 68 65 20 69 6e 70 75  ithm of the inpu
28a10 74 20 76 61 6c 75 65 2e 0a 2a 2a 20 54 68 65 20  t value..** The 
28a11 72 65 73 75 6c 74 73 20 6e 65 65 64 20 6e 6f 74  results need not
28a12 20 62 65 20 65 78 61 63 74 2e 20 20 54 68 69 73   be exact.  This
28a13 20 69 73 20 6f 6e 6c 79 20 75 73 65 64 20 66 6f   is only used fo
28a14 72 20 65 73 74 69 6d 61 74 69 6e 67 0a 2a 2a 20  r estimating.** 
28a15 74 68 65 20 74 6f 74 61 6c 20 63 6f 73 74 20 6f  the total cost o
28a16 66 20 70 65 72 66 6f 72 6d 69 6e 67 20 6f 70 65  f performing ope
28a17 72 61 74 69 6f 6e 73 20 77 69 74 68 20 4f 28 6c  rations with O(l
28a18 6f 67 4e 29 20 6f 72 20 4f 28 4e 6c 6f 67 4e 29  ogN) or O(NlogN)
28a19 0a 2a 2a 20 63 6f 6d 70 6c 65 78 69 74 79 2e 20  .** complexity. 
28a1a 20 42 65 63 61 75 73 65 20 4e 20 69 73 20 6a 75   Because N is ju
28a1b 73 74 20 61 20 67 75 65 73 73 2c 20 69 74 20 69  st a guess, it i
28a1c 73 20 6e 6f 20 67 72 65 61 74 20 74 72 61 67 65  s no great trage
28a1d 64 79 20 69 66 0a 2a 2a 20 6c 6f 67 4e 20 69 73  dy if.** logN is
28a1e 20 61 20 6c 69 74 74 6c 65 20 6f 66 66 2e 0a 2a   a little off..*
28a1f 2f 0a 73 74 61 74 69 63 20 64 6f 75 62 6c 65 20  /.static double 
28a20 65 73 74 4c 6f 67 28 64 6f 75 62 6c 65 20 4e 29  estLog(double N)
28a21 7b 0a 20 20 64 6f 75 62 6c 65 20 6c 6f 67 4e 20  {.  double logN 
28a22 3d 20 31 3b 0a 20 20 64 6f 75 62 6c 65 20 78 20  = 1;.  double x 
28a23 3d 20 31 30 3b 0a 20 20 77 68 69 6c 65 28 20 4e  = 10;.  while( N
28a24 3e 78 20 29 7b 0a 20 20 20 20 6c 6f 67 4e 20 2b  >x ){.    logN +
28a25 3d 20 31 3b 0a 20 20 20 20 78 20 2a 3d 20 31 30  = 1;.    x *= 10
28a26 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6c  ;.  }.  return l
28a27 6f 67 4e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 77  ogN;.}../*.** Tw
28a28 6f 20 72 6f 75 74 69 6e 65 73 20 66 6f 72 20 70  o routines for p
28a29 72 69 6e 74 69 6e 67 20 74 68 65 20 63 6f 6e 74  rinting the cont
28a2a 65 6e 74 20 6f 66 20 61 6e 20 73 71 6c 69 74 65  ent of an sqlite
28a2b 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 0a 2a 2a 20  3_index_info.** 
28a2c 73 74 72 75 63 74 75 72 65 2e 20 20 55 73 65 64  structure.  Used
28a2d 20 66 6f 72 20 74 65 73 74 69 6e 67 20 61 6e 64   for testing and
28a2e 20 64 65 62 75 67 67 69 6e 67 20 6f 6e 6c 79 2e   debugging only.
28a2f 20 20 49 66 20 6e 65 69 74 68 65 72 0a 2a 2a 20    If neither.** 
28a30 53 51 4c 49 54 45 5f 54 45 53 54 20 6f 72 20 53  SQLITE_TEST or S
28a31 51 4c 49 54 45 5f 44 45 42 55 47 20 61 72 65 20  QLITE_DEBUG are 
28a32 64 65 66 69 6e 65 64 2c 20 74 68 65 6e 20 74 68  defined, then th
28a33 65 73 65 20 72 6f 75 74 69 6e 65 73 0a 2a 2a 20  ese routines.** 
28a34 61 72 65 20 6e 6f 2d 6f 70 73 2e 0a 2a 2f 0a 23  are no-ops..*/.#
28a35 69 66 20 21 64 65 66 69 6e 65 64 28 53 51 4c 49  if !defined(SQLI
28a36 54 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54  TE_OMIT_VIRTUALT
28a37 41 42 4c 45 29 20 26 26 20 64 65 66 69 6e 65 64  ABLE) && defined
28a38 28 53 51 4c 49 54 45 5f 44 45 42 55 47 29 0a 73  (SQLITE_DEBUG).s
28a39 74 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45  tatic void TRACE
28a3a 5f 49 44 58 5f 49 4e 50 55 54 53 28 73 71 6c 69  _IDX_INPUTS(sqli
28a3b 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
28a3c 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
28a3d 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
28a3e 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
28a3f 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
28a40 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
28a41 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
28a42 62 75 67 50 72 69 6e 74 66 28 22 20 20 63 6f 6e  bugPrintf("  con
28a43 73 74 72 61 69 6e 74 5b 25 64 5d 3a 20 63 6f 6c  straint[%d]: col
28a44 3d 25 64 20 74 65 72 6d 69 64 3d 25 64 20 6f 70  =%d termid=%d op
28a45 3d 25 64 20 75 73 61 62 6c 65 64 3d 25 64 5c 6e  =%d usabled=%d\n
28a46 22 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20  ",.       i,.   
28a47 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72 61 69      p->aConstrai
28a48 6e 74 5b 69 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20  nt[i].iColumn,. 
28a49 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e 73 74 72        p->aConstr
28a4a 61 69 6e 74 5b 69 5d 2e 69 54 65 72 6d 4f 66 66  aint[i].iTermOff
28a4b 73 65 74 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61  set,.       p->a
28a4c 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 2e 6f 70  Constraint[i].op
28a4d 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
28a4e 73 74 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c  straint[i].usabl
28a4f 65 29 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  e);.  }.  for(i=
28a50 30 3b 20 69 3c 70 2d 3e 6e 4f 72 64 65 72 42 79  0; i<p->nOrderBy
28a51 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 71 6c 69  ; i++){.    sqli
28a52 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28 22  te3DebugPrintf("
28a53 20 20 6f 72 64 65 72 62 79 5b 25 64 5d 3a 20 63    orderby[%d]: c
28a54 6f 6c 3d 25 64 20 64 65 73 63 3d 25 64 5c 6e 22  ol=%d desc=%d\n"
28a55 2c 0a 20 20 20 20 20 20 20 69 2c 0a 20 20 20 20  ,.       i,.    
28a56 20 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69     p->aOrderBy[i
28a57 5d 2e 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  ].iColumn,.     
28a58 20 20 70 2d 3e 61 4f 72 64 65 72 42 79 5b 69 5d    p->aOrderBy[i]
28a59 2e 64 65 73 63 29 3b 0a 20 20 7d 0a 7d 0a 73 74  .desc);.  }.}.st
28a5a 61 74 69 63 20 76 6f 69 64 20 54 52 41 43 45 5f  atic void TRACE_
28a5b 49 44 58 5f 4f 55 54 50 55 54 53 28 73 71 6c 69  IDX_OUTPUTS(sqli
28a5c 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a  te3_index_info *
28a5d 70 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 69  p){.  int i;.  i
28a5e 66 28 20 21 73 71 6c 69 74 65 33 57 68 65 72 65  f( !sqlite3Where
28a5f 54 72 61 63 65 20 29 20 72 65 74 75 72 6e 3b 0a  Trace ) return;.
28a60 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 2d 3e    for(i=0; i<p->
28a61 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b 2b  nConstraint; i++
28a62 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  ){.    sqlite3De
28a63 62 75 67 50 72 69 6e 74 66 28 22 20 20 75 73 61  bugPrintf("  usa
28a64 67 65 5b 25 64 5d 3a 20 61 72 67 76 49 64 78 3d  ge[%d]: argvIdx=
28a65 25 64 20 6f 6d 69 74 3d 25 64 5c 6e 22 2c 0a 20  %d omit=%d\n",. 
28a66 20 20 20 20 20 20 69 2c 0a 20 20 20 20 20 20 20        i,.       
28a67 70 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  p->aConstraintUs
28a68 61 67 65 5b 69 5d 2e 61 72 67 76 49 6e 64 65 78  age[i].argvIndex
28a69 2c 0a 20 20 20 20 20 20 20 70 2d 3e 61 43 6f 6e  ,.       p->aCon
28a6a 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 5d 2e  straintUsage[i].
28a6b 6f 6d 69 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c  omit);.  }.  sql
28a6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74 66 28  ite3DebugPrintf(
28a6d 22 20 20 69 64 78 4e 75 6d 3d 25 64 5c 6e 22 2c  "  idxNum=%d\n",
28a6e 20 70 2d 3e 69 64 78 4e 75 6d 29 3b 0a 20 20 73   p->idxNum);.  s
28a6f 71 6c 69 74 65 33 44 65 62 75 67 50 72 69 6e 74  qlite3DebugPrint
28a70 66 28 22 20 20 69 64 78 53 74 72 3d 25 73 5c 6e  f("  idxStr=%s\n
28a71 22 2c 20 70 2d 3e 69 64 78 53 74 72 29 3b 0a 20  ", p->idxStr);. 
28a72 20 73 71 6c 69 74 65 33 44 65 62 75 67 50 72 69   sqlite3DebugPri
28a73 6e 74 66 28 22 20 20 6f 72 64 65 72 42 79 43 6f  ntf("  orderByCo
28a74 6e 73 75 6d 65 64 3d 25 64 5c 6e 22 2c 20 70 2d  nsumed=%d\n", p-
28a75 3e 6f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64  >orderByConsumed
28a76 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 65 62 75  );.  sqlite3Debu
28a77 67 50 72 69 6e 74 66 28 22 20 20 65 73 74 69 6d  gPrintf("  estim
28a78 61 74 65 64 43 6f 73 74 3d 25 67 5c 6e 22 2c 20  atedCost=%g\n", 
28a79 70 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  p->estimatedCost
28a7a 29 3b 0a 7d 0a 23 65 6c 73 65 0a 23 64 65 66 69  );.}.#else.#defi
28a7b 6e 65 20 54 52 41 43 45 5f 49 44 58 5f 49 4e 50  ne TRACE_IDX_INP
28a7c 55 54 53 28 41 29 0a 23 64 65 66 69 6e 65 20 54  UTS(A).#define T
28a7d 52 41 43 45 5f 49 44 58 5f 4f 55 54 50 55 54 53  RACE_IDX_OUTPUTS
28a7e 28 41 29 0a 23 65 6e 64 69 66 0a 0a 23 69 66 6e  (A).#endif..#ifn
28a7f 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
28a80 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 2f 2a 0a  VIRTUALTABLE./*.
28a81 2a 2a 20 43 6f 6d 70 75 74 65 20 74 68 65 20 62  ** Compute the b
28a82 65 73 74 20 69 6e 64 65 78 20 66 6f 72 20 61 20  est index for a 
28a83 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
28a84 2a 0a 2a 2a 20 54 68 65 20 62 65 73 74 20 69 6e  *.** The best in
28a85 64 65 78 20 69 73 20 63 6f 6d 70 75 74 65 64 20  dex is computed 
28a86 62 79 20 74 68 65 20 78 42 65 73 74 49 6e 64 65  by the xBestInde
28a87 78 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20  x method of the 
28a88 76 69 72 74 75 61 6c 0a 2a 2a 20 74 61 62 6c 65  virtual.** table
28a89 20 6d 6f 64 75 6c 65 2e 20 20 54 68 69 73 20 72   module.  This r
28a8a 6f 75 74 69 6e 65 20 69 73 20 72 65 61 6c 6c 79  outine is really
28a8b 20 6a 75 73 74 20 61 20 77 72 61 70 70 65 72 20   just a wrapper 
28a8c 74 68 61 74 20 73 65 74 73 20 75 70 0a 2a 2a 20  that sets up.** 
28a8d 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65  the sqlite3_inde
28a8e 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65  x_info structure
28a8f 20 74 68 61 74 20 69 73 20 75 73 65 64 20 74 6f   that is used to
28a90 20 63 6f 6d 6d 75 6e 69 63 61 74 65 20 77 69 74   communicate wit
28a91 68 0a 2a 2a 20 78 42 65 73 74 49 6e 64 65 78 2e  h.** xBestIndex.
28a92 0a 2a 2a 0a 2a 2a 20 49 6e 20 61 20 6a 6f 69 6e  .**.** In a join
28a93 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 6d  , this routine m
28a94 69 67 68 74 20 62 65 20 63 61 6c 6c 65 64 20 6d  ight be called m
28a95 75 6c 74 69 70 6c 65 20 74 69 6d 65 73 20 66 6f  ultiple times fo
28a96 72 20 74 68 65 0a 2a 2a 20 73 61 6d 65 20 76 69  r the.** same vi
28a97 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68  rtual table.  Th
28a98 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
28a99 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 69  info structure i
28a9a 73 20 63 72 65 61 74 65 64 0a 2a 2a 20 61 6e 64  s created.** and
28a9b 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 6f 6e 20   initialized on 
28a9c 74 68 65 20 66 69 72 73 74 20 69 6e 76 6f 63 61  the first invoca
28a9d 74 69 6f 6e 20 61 6e 64 20 72 65 75 73 65 64 20  tion and reused 
28a9e 6f 6e 20 61 6c 6c 20 73 75 62 73 65 71 75 65 6e  on all subsequen
28a9f 74 0a 2a 2a 20 69 6e 76 6f 63 61 74 69 6f 6e 73  t.** invocations
28aa0 2e 20 20 54 68 65 20 73 71 6c 69 74 65 33 5f 69  .  The sqlite3_i
28aa1 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74  ndex_info struct
28aa2 75 72 65 20 69 73 20 61 6c 73 6f 20 75 73 65 64  ure is also used
28aa3 20 77 68 65 6e 0a 2a 2a 20 63 6f 64 65 20 69 73   when.** code is
28aa4 20 67 65 6e 65 72 61 74 65 64 20 74 6f 20 61 63   generated to ac
28aa5 63 65 73 73 20 74 68 65 20 76 69 72 74 75 61 6c  cess the virtual
28aa6 20 74 61 62 6c 65 2e 20 20 54 68 65 20 77 68 65   table.  The whe
28aa7 72 65 49 6e 66 6f 44 65 6c 65 74 65 28 29 20 0a  reInfoDelete() .
28aa8 2a 2a 20 72 6f 75 74 69 6e 65 20 74 61 6b 65 73  ** routine takes
28aa9 20 63 61 72 65 20 6f 66 20 66 72 65 65 69 6e 67   care of freeing
28aaa 20 74 68 65 20 73 71 6c 69 74 65 33 5f 69 6e 64   the sqlite3_ind
28aab 65 78 5f 69 6e 66 6f 20 73 74 72 75 63 74 75 72  ex_info structur
28aac 65 20 61 66 74 65 72 0a 2a 2a 20 65 76 65 72 79  e after.** every
28aad 62 6f 64 79 20 68 61 73 20 66 69 6e 69 73 68 65  body has finishe
28aae 64 20 77 69 74 68 20 69 74 2e 0a 2a 2f 0a 73 74  d with it..*/.st
28aaf 61 74 69 63 20 64 6f 75 62 6c 65 20 62 65 73 74  atic double best
28ab0 56 69 72 74 75 61 6c 49 6e 64 65 78 28 0a 20 20  VirtualIndex(.  
28ab1 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20 20  Parse *pParse,  
28ab2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28ab3 2a 20 54 68 65 20 70 61 72 73 69 6e 67 20 63 6f  * The parsing co
28ab4 6e 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65  ntext */.  Where
28ab5 43 6c 61 75 73 65 20 2a 70 57 43 2c 20 20 20 20  Clause *pWC,    
28ab6 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65            /* The
28ab7 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
28ab8 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69 73  .  struct SrcLis
28ab9 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20 20  t_item *pSrc,   
28aba 20 20 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c    /* The FROM cl
28abb 61 75 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61  ause term to sea
28abc 72 63 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b  rch */.  Bitmask
28abd 20 6e 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20   notReady,      
28abe 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 73 6b 20          /* Mask 
28abf 6f 66 20 63 75 72 73 6f 72 73 20 74 68 61 74 20  of cursors that 
28ac0 61 72 65 20 6e 6f 74 20 61 76 61 69 6c 61 62 6c  are not availabl
28ac1 65 20 2a 2f 0a 20 20 45 78 70 72 4c 69 73 74 20  e */.  ExprList 
28ac2 2a 70 4f 72 64 65 72 42 79 2c 20 20 20 20 20 20  *pOrderBy,      
28ac3 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6f 72 64        /* The ord
28ac4 65 72 20 62 79 20 63 6c 61 75 73 65 20 2a 2f 0a  er by clause */.
28ac5 20 20 69 6e 74 20 6f 72 64 65 72 42 79 55 73 61    int orderByUsa
28ac6 62 6c 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ble,            
28ac7 20 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 63   /* True if we c
28ac8 61 6e 20 70 6f 74 65 6e 74 69 61 6c 20 73 6f 72  an potential sor
28ac9 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 69  t */.  sqlite3_i
28aca 6e 64 65 78 5f 69 6e 66 6f 20 2a 2a 70 70 49 64  ndex_info **ppId
28acb 78 49 6e 66 6f 20 2f 2a 20 49 6e 64 65 78 20 69  xInfo /* Index i
28acc 6e 66 6f 72 6d 61 74 69 6f 6e 20 70 61 73 73 65  nformation passe
28acd 64 20 74 6f 20 78 42 65 73 74 49 6e 64 65 78 20  d to xBestIndex 
28ace 2a 2f 0a 29 7b 0a 20 20 54 61 62 6c 65 20 2a 70  */.){.  Table *p
28acf 54 61 62 20 3d 20 70 53 72 63 2d 3e 70 54 61 62  Tab = pSrc->pTab
28ad0 3b 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62  ;.  sqlite3_vtab
28ad1 20 2a 70 56 74 61 62 20 3d 20 70 54 61 62 2d 3e   *pVtab = pTab->
28ad2 70 56 74 61 62 3b 0a 20 20 73 71 6c 69 74 65 33  pVtab;.  sqlite3
28ad3 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 64  _index_info *pId
28ad4 78 49 6e 66 6f 3b 0a 20 20 73 74 72 75 63 74 20  xInfo;.  struct 
28ad5 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
28ad6 6e 73 74 72 61 69 6e 74 20 2a 70 49 64 78 43 6f  nstraint *pIdxCo
28ad7 6e 73 3b 0a 20 20 73 74 72 75 63 74 20 73 71 6c  ns;.  struct sql
28ad8 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
28ad9 62 79 20 2a 70 49 64 78 4f 72 64 65 72 42 79 3b  by *pIdxOrderBy;
28ada 0a 20 20 73 74 72 75 63 74 20 73 71 6c 69 74 65  .  struct sqlite
28adb 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69  3_index_constrai
28adc 6e 74 5f 75 73 61 67 65 20 2a 70 55 73 61 67 65  nt_usage *pUsage
28add 3b 0a 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70  ;.  WhereTerm *p
28ade 54 65 72 6d 3b 0a 20 20 69 6e 74 20 69 2c 20 6a  Term;.  int i, j
28adf 3b 0a 20 20 69 6e 74 20 6e 4f 72 64 65 72 42 79  ;.  int nOrderBy
28ae0 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2f  ;.  int rc;..  /
28ae1 2a 20 49 66 20 74 68 65 20 73 71 6c 69 74 65 33  * If the sqlite3
28ae2 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72 75  _index_info stru
28ae3 63 74 75 72 65 20 68 61 73 20 6e 6f 74 20 62 65  cture has not be
28ae4 65 6e 20 70 72 65 76 69 6f 75 73 6c 79 0a 20 20  en previously.  
28ae5 2a 2a 20 61 6c 6c 6f 63 61 74 65 64 20 61 6e 64  ** allocated and
28ae6 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 66 6f 72   initialized for
28ae7 20 74 68 69 73 20 76 69 72 74 75 61 6c 20 74 61   this virtual ta
28ae8 62 6c 65 2c 20 74 68 65 6e 20 61 6c 6c 6f 63 61  ble, then alloca
28ae9 74 65 0a 20 20 2a 2a 20 61 6e 64 20 69 6e 69 74  te.  ** and init
28aea 69 61 6c 69 7a 65 20 69 74 20 6e 6f 77 0a 20 20  ialize it now.  
28aeb 2a 2f 0a 20 20 70 49 64 78 49 6e 66 6f 20 3d 20  */.  pIdxInfo = 
28aec 2a 70 70 49 64 78 49 6e 66 6f 3b 0a 20 20 69 66  *ppIdxInfo;.  if
28aed 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b  ( pIdxInfo==0 ){
28aee 0a 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a  .    WhereTerm *
28aef 70 54 65 72 6d 3b 0a 20 20 20 20 69 6e 74 20 6e  pTerm;.    int n
28af0 54 65 72 6d 3b 0a 20 20 20 20 57 48 45 52 45 54  Term;.    WHERET
28af1 52 41 43 45 28 28 22 52 65 63 6f 6d 70 75 74 69  RACE(("Recomputi
28af2 6e 67 20 69 6e 64 65 78 20 69 6e 66 6f 20 66 6f  ng index info fo
28af3 72 20 25 73 2e 2e 2e 5c 6e 22 2c 20 70 54 61 62  r %s...\n", pTab
28af4 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a 20 20 20 20  ->zName));..    
28af5 2f 2a 20 43 6f 75 6e 74 20 74 68 65 20 6e 75 6d  /* Count the num
28af6 62 65 72 20 6f 66 20 70 6f 73 73 69 62 6c 65 20  ber of possible 
28af7 57 48 45 52 45 20 63 6c 61 75 73 65 20 63 6f 6e  WHERE clause con
28af8 73 74 72 61 69 6e 74 73 20 72 65 66 65 72 72 69  straints referri
28af9 6e 67 0a 20 20 20 20 2a 2a 20 74 6f 20 74 68 69  ng.    ** to thi
28afa 73 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  s virtual table 
28afb 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 6e 54 65  */.    for(i=nTe
28afc 72 6d 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d  rm=0, pTerm=pWC-
28afd 3e 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d  >a; i<pWC->nTerm
28afe 3b 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b  ; i++, pTerm++){
28aff 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
28b00 2d 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20  ->leftCursor != 
28b01 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20  pSrc->iCursor ) 
28b02 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
28b03 61 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e  assert( (pTerm->
28b04 65 4f 70 65 72 61 74 6f 72 26 28 70 54 65 72 6d  eOperator&(pTerm
28b05 2d 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d  ->eOperator-1))=
28b06 3d 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  =0 );.      test
28b07 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70  case( pTerm->eOp
28b08 65 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b  erator==WO_IN );
28b09 0a 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28  .      testcase(
28b0a 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f   pTerm->eOperato
28b0b 72 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a  r==WO_ISNULL );.
28b0c 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
28b0d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
28b0e 5f 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29  _IN|WO_ISNULL) )
28b0f 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
28b10 20 6e 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 7d 0a   nTerm++;.    }.
28b11 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 65 20 4f  .    /* If the O
28b12 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20 63  RDER BY clause c
28b13 6f 6e 74 61 69 6e 73 20 6f 6e 6c 79 20 63 6f 6c  ontains only col
28b14 75 6d 6e 73 20 69 6e 20 74 68 65 20 63 75 72 72  umns in the curr
28b15 65 6e 74 20 0a 20 20 20 20 2a 2a 20 76 69 72 74  ent .    ** virt
28b16 75 61 6c 20 74 61 62 6c 65 20 74 68 65 6e 20 61  ual table then a
28b17 6c 6c 6f 63 61 74 65 20 73 70 61 63 65 20 66 6f  llocate space fo
28b18 72 20 74 68 65 20 61 4f 72 64 65 72 42 79 20 70  r the aOrderBy p
28b19 61 72 74 20 6f 66 0a 20 20 20 20 2a 2a 20 74 68  art of.    ** th
28b1a 65 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  e sqlite3_index_
28b1b 69 6e 66 6f 20 73 74 72 75 63 74 75 72 65 2e 0a  info structure..
28b1c 20 20 20 20 2a 2f 0a 20 20 20 20 6e 4f 72 64 65      */.    nOrde
28b1d 72 42 79 20 3d 20 30 3b 0a 20 20 20 20 69 66 28  rBy = 0;.    if(
28b1e 20 70 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20   pOrderBy ){.   
28b1f 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 4f     for(i=0; i<pO
28b20 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3b 20 69  rderBy->nExpr; i
28b21 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 45 78 70  ++){.        Exp
28b22 72 20 2a 70 45 78 70 72 20 3d 20 70 4f 72 64 65  r *pExpr = pOrde
28b23 72 42 79 2d 3e 61 5b 69 5d 2e 70 45 78 70 72 3b  rBy->a[i].pExpr;
28b24 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 45 78  .        if( pEx
28b25 70 72 2d 3e 6f 70 21 3d 54 4b 5f 43 4f 4c 55 4d  pr->op!=TK_COLUM
28b26 4e 20 7c 7c 20 70 45 78 70 72 2d 3e 69 54 61 62  N || pExpr->iTab
28b27 6c 65 21 3d 70 53 72 63 2d 3e 69 43 75 72 73 6f  le!=pSrc->iCurso
28b28 72 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20  r ) break;.     
28b29 20 7d 0a 20 20 20 20 20 20 69 66 28 20 69 3d 3d   }.      if( i==
28b2a 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 20  pOrderBy->nExpr 
28b2b 29 7b 0a 20 20 20 20 20 20 20 20 6e 4f 72 64 65  ){.        nOrde
28b2c 72 42 79 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e  rBy = pOrderBy->
28b2d 6e 45 78 70 72 3b 0a 20 20 20 20 20 20 7d 0a 20  nExpr;.      }. 
28b2e 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 41 6c 6c     }..    /* All
28b2f 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69 74 65  ocate the sqlite
28b30 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74 72  3_index_info str
28b31 75 63 74 75 72 65 0a 20 20 20 20 2a 2f 0a 20 20  ucture.    */.  
28b32 20 20 70 49 64 78 49 6e 66 6f 20 3d 20 73 71 6c    pIdxInfo = sql
28b33 69 74 65 33 44 62 4d 61 6c 6c 6f 63 5a 65 72 6f  ite3DbMallocZero
28b34 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 73 69 7a  (pParse->db, siz
28b35 65 6f 66 28 2a 70 49 64 78 49 6e 66 6f 29 0a 20  eof(*pIdxInfo). 
28b36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b37 20 20 20 20 20 20 20 20 20 20 20 20 2b 20 28 73              + (s
28b38 69 7a 65 6f 66 28 2a 70 49 64 78 43 6f 6e 73 29  izeof(*pIdxCons)
28b39 20 2b 20 73 69 7a 65 6f 66 28 2a 70 55 73 61 67   + sizeof(*pUsag
28b3a 65 29 29 2a 6e 54 65 72 6d 0a 20 20 20 20 20 20  e))*nTerm.      
28b3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b3c 20 20 20 20 20 20 20 2b 20 73 69 7a 65 6f 66 28         + sizeof(
28b3d 2a 70 49 64 78 4f 72 64 65 72 42 79 29 2a 6e 4f  *pIdxOrderBy)*nO
28b3e 72 64 65 72 42 79 20 29 3b 0a 20 20 20 20 69 66  rderBy );.    if
28b3f 28 20 70 49 64 78 49 6e 66 6f 3d 3d 30 20 29 7b  ( pIdxInfo==0 ){
28b40 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
28b41 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
28b42 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b  out of memory");
28b43 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 30 2e  .      return 0.
28b44 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 70  0;.    }.    *pp
28b45 49 64 78 49 6e 66 6f 20 3d 20 70 49 64 78 49 6e  IdxInfo = pIdxIn
28b46 66 6f 3b 0a 0a 20 20 20 20 2f 2a 20 49 6e 69 74  fo;..    /* Init
28b47 69 61 6c 69 7a 65 20 74 68 65 20 73 74 72 75 63  ialize the struc
28b48 74 75 72 65 2e 20 20 54 68 65 20 73 71 6c 69 74  ture.  The sqlit
28b49 65 33 5f 69 6e 64 65 78 5f 69 6e 66 6f 20 73 74  e3_index_info st
28b4a 72 75 63 74 75 72 65 20 63 6f 6e 74 61 69 6e 73  ructure contains
28b4b 0a 20 20 20 20 2a 2a 20 6d 61 6e 79 20 66 69 65  .    ** many fie
28b4c 6c 64 73 20 74 68 61 74 20 61 72 65 20 64 65 63  lds that are dec
28b4d 6c 61 72 65 64 20 22 63 6f 6e 73 74 22 20 74 6f  lared "const" to
28b4e 20 70 72 65 76 65 6e 74 20 78 42 65 73 74 49 6e   prevent xBestIn
28b4f 64 65 78 20 66 72 6f 6d 0a 20 20 20 20 2a 2a 20  dex from.    ** 
28b50 63 68 61 6e 67 69 6e 67 20 74 68 65 6d 2e 20 20  changing them.  
28b51 57 65 20 68 61 76 65 20 74 6f 20 64 6f 20 73 6f  We have to do so
28b52 6d 65 20 66 75 6e 6b 79 20 63 61 73 74 69 6e 67  me funky casting
28b53 20 69 6e 20 6f 72 64 65 72 20 74 6f 0a 20 20 20   in order to.   
28b54 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74   ** initialize t
28b55 68 6f 73 65 20 66 69 65 6c 64 73 2e 0a 20 20 20  hose fields..   
28b56 20 2a 2f 0a 20 20 20 20 70 49 64 78 43 6f 6e 73   */.    pIdxCons
28b57 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
28b58 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
28b59 69 6e 74 2a 29 26 70 49 64 78 49 6e 66 6f 5b 31  int*)&pIdxInfo[1
28b5a 5d 3b 0a 20 20 20 20 70 49 64 78 4f 72 64 65 72  ];.    pIdxOrder
28b5b 42 79 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c  By = (struct sql
28b5c 69 74 65 33 5f 69 6e 64 65 78 5f 6f 72 64 65 72  ite3_index_order
28b5d 62 79 2a 29 26 70 49 64 78 43 6f 6e 73 5b 6e 54  by*)&pIdxCons[nT
28b5e 65 72 6d 5d 3b 0a 20 20 20 20 70 55 73 61 67 65  erm];.    pUsage
28b5f 20 3d 20 28 73 74 72 75 63 74 20 73 71 6c 69 74   = (struct sqlit
28b60 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61  e3_index_constra
28b61 69 6e 74 5f 75 73 61 67 65 2a 29 26 70 49 64 78  int_usage*)&pIdx
28b62 4f 72 64 65 72 42 79 5b 6e 4f 72 64 65 72 42 79  OrderBy[nOrderBy
28b63 5d 3b 0a 20 20 20 20 2a 28 69 6e 74 2a 29 26 70  ];.    *(int*)&p
28b64 49 64 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72  IdxInfo->nConstr
28b65 61 69 6e 74 20 3d 20 6e 54 65 72 6d 3b 0a 20 20  aint = nTerm;.  
28b66 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49 6e    *(int*)&pIdxIn
28b67 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20 6e  fo->nOrderBy = n
28b68 4f 72 64 65 72 42 79 3b 0a 20 20 20 20 2a 28 73  OrderBy;.    *(s
28b69 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
28b6a 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 2a 2a  dex_constraint**
28b6b 29 26 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e  )&pIdxInfo->aCon
28b6c 73 74 72 61 69 6e 74 20 3d 20 70 49 64 78 43 6f  straint = pIdxCo
28b6d 6e 73 3b 0a 20 20 20 20 2a 28 73 74 72 75 63 74  ns;.    *(struct
28b6e 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 6f   sqlite3_index_o
28b6f 72 64 65 72 62 79 2a 2a 29 26 70 49 64 78 49 6e  rderby**)&pIdxIn
28b70 66 6f 2d 3e 61 4f 72 64 65 72 42 79 20 3d 20 70  fo->aOrderBy = p
28b71 49 64 78 4f 72 64 65 72 42 79 3b 0a 20 20 20 20  IdxOrderBy;.    
28b72 2a 28 73 74 72 75 63 74 20 73 71 6c 69 74 65 33  *(struct sqlite3
28b73 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e  _index_constrain
28b74 74 5f 75 73 61 67 65 2a 2a 29 26 70 49 64 78 49  t_usage**)&pIdxI
28b75 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
28b76 55 73 61 67 65 20 3d 0a 20 20 20 20 20 20 20 20  Usage =.        
28b77 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b78 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b79 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28b7a 20 20 20 20 20 20 20 20 20 20 20 20 20 70 55 73               pUs
28b7b 61 67 65 3b 0a 0a 20 20 20 20 66 6f 72 28 69 3d  age;..    for(i=
28b7c 6a 3d 30 2c 20 70 54 65 72 6d 3d 70 57 43 2d 3e  j=0, pTerm=pWC->
28b7d 61 3b 20 69 3c 70 57 43 2d 3e 6e 54 65 72 6d 3b  a; i<pWC->nTerm;
28b7e 20 69 2b 2b 2c 20 70 54 65 72 6d 2b 2b 29 7b 0a   i++, pTerm++){.
28b7f 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d        if( pTerm-
28b80 3e 6c 65 66 74 43 75 72 73 6f 72 20 21 3d 20 70  >leftCursor != p
28b81 53 72 63 2d 3e 69 43 75 72 73 6f 72 20 29 20 63  Src->iCursor ) c
28b82 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 61  ontinue;.      a
28b83 73 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 65  ssert( (pTerm->e
28b84 4f 70 65 72 61 74 6f 72 26 28 70 54 65 72 6d 2d  Operator&(pTerm-
28b85 3e 65 4f 70 65 72 61 74 6f 72 2d 31 29 29 3d 3d  >eOperator-1))==
28b86 30 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63  0 );.      testc
28b87 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
28b88 72 61 74 6f 72 3d 3d 57 4f 5f 49 4e 20 29 3b 0a  rator==WO_IN );.
28b89 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
28b8a 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
28b8b 3d 3d 57 4f 5f 49 53 4e 55 4c 4c 20 29 3b 0a 20  ==WO_ISNULL );. 
28b8c 20 20 20 20 20 69 66 28 20 70 54 65 72 6d 2d 3e       if( pTerm->
28b8d 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f 5f  eOperator & (WO_
28b8e 49 4e 7c 57 4f 5f 49 53 4e 55 4c 4c 29 20 29 20  IN|WO_ISNULL) ) 
28b8f 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20  continue;.      
28b90 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 43 6f 6c  pIdxCons[j].iCol
28b91 75 6d 6e 20 3d 20 70 54 65 72 6d 2d 3e 6c 65 66  umn = pTerm->lef
28b92 74 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70  tColumn;.      p
28b93 49 64 78 43 6f 6e 73 5b 6a 5d 2e 69 54 65 72 6d  IdxCons[j].iTerm
28b94 4f 66 66 73 65 74 20 3d 20 69 3b 0a 20 20 20 20  Offset = i;.    
28b95 20 20 70 49 64 78 43 6f 6e 73 5b 6a 5d 2e 6f 70    pIdxCons[j].op
28b96 20 3d 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61   = pTerm->eOpera
28b97 74 6f 72 3b 0a 20 20 20 20 20 20 2f 2a 20 54 68  tor;.      /* Th
28b98 65 20 64 69 72 65 63 74 20 61 73 73 69 67 6e 6d  e direct assignm
28b99 65 6e 74 20 69 6e 20 74 68 65 20 70 72 65 76 69  ent in the previ
28b9a 6f 75 73 20 6c 69 6e 65 20 69 73 20 70 6f 73 73  ous line is poss
28b9b 69 62 6c 65 20 6f 6e 6c 79 20 62 65 63 61 75 73  ible only becaus
28b9c 65 0a 20 20 20 20 20 20 2a 2a 20 74 68 65 20 57  e.      ** the W
28b9d 4f 5f 20 61 6e 64 20 53 51 4c 49 54 45 5f 49 4e  O_ and SQLITE_IN
28b9e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 20  DEX_CONSTRAINT_ 
28b9f 63 6f 64 65 73 20 61 72 65 20 69 64 65 6e 74 69  codes are identi
28ba0 63 61 6c 2e 20 20 54 68 65 0a 20 20 20 20 20 20  cal.  The.      
28ba1 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20 61 73 73  ** following ass
28ba2 65 72 74 73 20 76 65 72 69 66 79 20 74 68 69 73  erts verify this
28ba3 20 66 61 63 74 2e 20 2a 2f 0a 20 20 20 20 20 20   fact. */.      
28ba4 61 73 73 65 72 74 28 20 57 4f 5f 45 51 3d 3d 53  assert( WO_EQ==S
28ba5 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
28ba6 54 52 41 49 4e 54 5f 45 51 20 29 3b 0a 20 20 20  TRAINT_EQ );.   
28ba7 20 20 20 61 73 73 65 72 74 28 20 57 4f 5f 4c 54     assert( WO_LT
28ba8 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  ==SQLITE_INDEX_C
28ba9 4f 4e 53 54 52 41 49 4e 54 5f 4c 54 20 29 3b 0a  ONSTRAINT_LT );.
28baa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 57 4f        assert( WO
28bab 5f 4c 45 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45  _LE==SQLITE_INDE
28bac 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4c 45 20  X_CONSTRAINT_LE 
28bad 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
28bae 20 57 4f 5f 47 54 3d 3d 53 51 4c 49 54 45 5f 49   WO_GT==SQLITE_I
28baf 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e 54 5f  NDEX_CONSTRAINT_
28bb0 47 54 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65  GT );.      asse
28bb1 72 74 28 20 57 4f 5f 47 45 3d 3d 53 51 4c 49 54  rt( WO_GE==SQLIT
28bb2 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49  E_INDEX_CONSTRAI
28bb3 4e 54 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 61  NT_GE );.      a
28bb4 73 73 65 72 74 28 20 57 4f 5f 4d 41 54 43 48 3d  ssert( WO_MATCH=
28bb5 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f  =SQLITE_INDEX_CO
28bb6 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43 48 20 29  NSTRAINT_MATCH )
28bb7 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
28bb8 70 54 65 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72  pTerm->eOperator
28bb9 20 26 20 28 57 4f 5f 45 51 7c 57 4f 5f 4c 54 7c   & (WO_EQ|WO_LT|
28bba 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47  WO_LE|WO_GT|WO_G
28bbb 45 7c 57 4f 5f 4d 41 54 43 48 29 20 29 3b 0a 20  E|WO_MATCH) );. 
28bbc 20 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 7d 0a       j++;.    }.
28bbd 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
28bbe 4f 72 64 65 72 42 79 3b 20 69 2b 2b 29 7b 0a 20  OrderBy; i++){. 
28bbf 20 20 20 20 20 45 78 70 72 20 2a 70 45 78 70 72       Expr *pExpr
28bc0 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b 69   = pOrderBy->a[i
28bc1 5d 2e 70 45 78 70 72 3b 0a 20 20 20 20 20 20 70  ].pExpr;.      p
28bc2 49 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 69 43  IdxOrderBy[i].iC
28bc3 6f 6c 75 6d 6e 20 3d 20 70 45 78 70 72 2d 3e 69  olumn = pExpr->i
28bc4 43 6f 6c 75 6d 6e 3b 0a 20 20 20 20 20 20 70 49  Column;.      pI
28bc5 64 78 4f 72 64 65 72 42 79 5b 69 5d 2e 64 65 73  dxOrderBy[i].des
28bc6 63 20 3d 20 70 4f 72 64 65 72 42 79 2d 3e 61 5b  c = pOrderBy->a[
28bc7 69 5d 2e 73 6f 72 74 4f 72 64 65 72 3b 0a 20 20  i].sortOrder;.  
28bc8 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 74    }.  }..  /* At
28bc9 20 74 68 69 73 20 70 6f 69 6e 74 2c 20 74 68 65   this point, the
28bca 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
28bcb 6e 66 6f 20 73 74 72 75 63 74 75 72 65 20 74 68  nfo structure th
28bcc 61 74 20 70 49 64 78 49 6e 66 6f 20 70 6f 69 6e  at pIdxInfo poin
28bcd 74 73 0a 20 20 2a 2a 20 74 6f 20 77 69 6c 6c 20  ts.  ** to will 
28bce 68 61 76 65 20 62 65 65 6e 20 69 6e 69 74 69 61  have been initia
28bcf 6c 69 7a 65 64 2c 20 65 69 74 68 65 72 20 64 75  lized, either du
28bd0 72 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74  ring the current
28bd1 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 72 0a 20   invocation or. 
28bd2 20 2a 2a 20 64 75 72 69 6e 67 20 73 6f 6d 65 20   ** during some 
28bd3 70 72 69 6f 72 20 69 6e 76 6f 63 61 74 69 6f 6e  prior invocation
28bd4 2e 20 20 4e 6f 77 20 77 65 20 6a 75 73 74 20 68  .  Now we just h
28bd5 61 76 65 20 74 6f 20 63 75 73 74 6f 6d 69 7a 65  ave to customize
28bd6 20 74 68 65 0a 20 20 2a 2a 20 64 65 74 61 69 6c   the.  ** detail
28bd7 73 20 6f 66 20 70 49 64 78 49 6e 66 6f 20 66 6f  s of pIdxInfo fo
28bd8 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 69 6e  r the current in
28bd9 76 6f 63 61 74 69 6f 6e 20 61 6e 64 20 70 61 73  vocation and pas
28bda 73 20 69 74 20 74 6f 0a 20 20 2a 2a 20 78 42 65  s it to.  ** xBe
28bdb 73 74 49 6e 64 65 78 2e 0a 20 20 2a 2f 0a 0a 20  stIndex..  */.. 
28bdc 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c 65 20 6e   /* The module n
28bdd 61 6d 65 20 6d 75 73 74 20 62 65 20 64 65 66 69  ame must be defi
28bde 6e 65 64 2e 20 41 6c 73 6f 2c 20 62 79 20 74 68  ned. Also, by th
28bdf 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20 6d  is point there m
28be0 75 73 74 0a 20 20 2a 2a 20 62 65 20 61 20 70 6f  ust.  ** be a po
28be1 69 6e 74 65 72 20 74 6f 20 61 6e 20 73 71 6c 69  inter to an sqli
28be2 74 65 33 5f 76 74 61 62 20 73 74 72 75 63 74 75  te3_vtab structu
28be3 72 65 2e 20 4f 74 68 65 72 77 69 73 65 0a 20 20  re. Otherwise.  
28be4 2a 2a 20 73 71 6c 69 74 65 33 56 69 65 77 47 65  ** sqlite3ViewGe
28be5 74 43 6f 6c 75 6d 6e 4e 61 6d 65 73 28 29 20 77  tColumnNames() w
28be6 6f 75 6c 64 20 68 61 76 65 20 70 69 63 6b 65 64  ould have picked
28be7 20 75 70 20 74 68 65 20 65 72 72 6f 72 2e 20 0a   up the error. .
28be8 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70    */.  assert( p
28be9 54 61 62 2d 3e 61 7a 4d 6f 64 75 6c 65 41 72 67  Tab->azModuleArg
28bea 20 26 26 20 70 54 61 62 2d 3e 61 7a 4d 6f 64 75   && pTab->azModu
28beb 6c 65 41 72 67 5b 30 5d 20 29 3b 0a 20 20 61 73  leArg[0] );.  as
28bec 73 65 72 74 28 20 70 56 74 61 62 20 29 3b 0a 23  sert( pVtab );.#
28bed 69 66 20 30 0a 20 20 69 66 28 20 70 54 61 62 2d  if 0.  if( pTab-
28bee 3e 70 56 74 61 62 3d 3d 30 20 29 7b 0a 20 20 20  >pVtab==0 ){.   
28bef 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67   sqlite3ErrorMsg
28bf0 28 70 50 61 72 73 65 2c 20 22 75 6e 64 65 66 69  (pParse, "undefi
28bf1 6e 65 64 20 6d 6f 64 75 6c 65 20 25 73 20 66 6f  ned module %s fo
28bf2 72 20 74 61 62 6c 65 20 25 73 22 2c 0a 20 20 20  r table %s",.   
28bf3 20 20 20 20 20 70 54 61 62 2d 3e 61 7a 4d 6f 64       pTab->azMod
28bf4 75 6c 65 41 72 67 5b 30 5d 2c 20 70 54 61 62 2d  uleArg[0], pTab-
28bf5 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 72 65 74  >zName);.    ret
28bf6 75 72 6e 20 30 2e 30 3b 0a 20 20 7d 0a 23 65 6e  urn 0.0;.  }.#en
28bf7 64 69 66 0a 0a 20 20 2f 2a 20 53 65 74 20 74 68  dif..  /* Set th
28bf8 65 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 5d 2e  e aConstraint[].
28bf9 75 73 61 62 6c 65 20 66 69 65 6c 64 73 20 61 6e  usable fields an
28bfa 64 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 6c 6c  d initialize all
28bfb 20 0a 20 20 2a 2a 20 6f 75 74 70 75 74 20 76 61   .  ** output va
28bfc 72 69 61 62 6c 65 73 20 74 6f 20 7a 65 72 6f 2e  riables to zero.
28bfd 0a 20 20 2a 2a 0a 20 20 2a 2a 20 61 43 6f 6e 73  .  **.  ** aCons
28bfe 74 72 61 69 6e 74 5b 5d 2e 75 73 61 62 6c 65 20  traint[].usable 
28bff 69 73 20 74 72 75 65 20 66 6f 72 20 63 6f 6e 73  is true for cons
28c00 74 72 61 69 6e 74 73 20 77 68 65 72 65 20 74 68  traints where th
28c01 65 20 72 69 67 68 74 2d 68 61 6e 64 0a 20 20 2a  e right-hand.  *
28c02 2a 20 73 69 64 65 20 63 6f 6e 74 61 69 6e 73 20  * side contains 
28c03 6f 6e 6c 79 20 72 65 66 65 72 65 6e 63 65 73 20  only references 
28c04 74 6f 20 74 61 62 6c 65 73 20 74 6f 20 74 68 65  to tables to the
28c05 20 6c 65 66 74 20 6f 66 20 74 68 65 20 63 75 72   left of the cur
28c06 72 65 6e 74 0a 20 20 2a 2a 20 74 61 62 6c 65 2e  rent.  ** table.
28c07 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64 73    In other words
28c08 2c 20 69 66 20 74 68 65 20 63 6f 6e 73 74 72 61  , if the constra
28c09 69 6e 74 20 69 73 20 6f 66 20 74 68 65 20 66 6f  int is of the fo
28c0a 72 6d 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20 20 20  rm:.  **.  **   
28c0b 20 20 20 20 20 20 20 20 63 6f 6c 75 6d 6e 20 3d          column =
28c0c 20 65 78 70 72 0a 20 20 2a 2a 0a 20 20 2a 2a 20   expr.  **.  ** 
28c0d 61 6e 64 20 77 65 20 61 72 65 20 65 76 61 6c 75  and we are evalu
28c0e 61 74 69 6e 67 20 61 20 6a 6f 69 6e 2c 20 74 68  ating a join, th
28c0f 65 6e 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  en the constrain
28c10 74 20 6f 6e 20 63 6f 6c 75 6d 6e 20 69 73 20 0a  t on column is .
28c11 20 20 2a 2a 20 6f 6e 6c 79 20 76 61 6c 69 64 20    ** only valid 
28c12 69 66 20 61 6c 6c 20 74 61 62 6c 65 73 20 72 65  if all tables re
28c13 66 65 72 65 6e 63 65 64 20 69 6e 20 65 78 70 72  ferenced in expr
28c14 20 6f 63 63 75 72 20 74 6f 20 74 68 65 20 6c 65   occur to the le
28c15 66 74 0a 20 20 2a 2a 20 6f 66 20 74 68 65 20 74  ft.  ** of the t
28c16 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  able containing 
28c17 63 6f 6c 75 6d 6e 2e 0a 20 20 2a 2a 0a 20 20 2a  column..  **.  *
28c18 2a 20 54 68 65 20 61 43 6f 6e 73 74 72 61 69 6e  * The aConstrain
28c19 74 73 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61  ts[] array conta
28c1a 69 6e 73 20 65 6e 74 72 69 65 73 20 66 6f 72 20  ins entries for 
28c1b 61 6c 6c 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a  all constraints.
28c1c 20 20 2a 2a 20 6f 6e 20 74 68 65 20 63 75 72 72    ** on the curr
28c1d 65 6e 74 20 74 61 62 6c 65 2e 20 20 54 68 61 74  ent table.  That
28c1e 20 77 61 79 20 77 65 20 6f 6e 6c 79 20 68 61 76   way we only hav
28c1f 65 20 74 6f 20 63 6f 6d 70 75 74 65 20 69 74 20  e to compute it 
28c20 6f 6e 63 65 0a 20 20 2a 2a 20 65 76 65 6e 20 74  once.  ** even t
28c21 68 6f 75 67 68 20 77 65 20 6d 69 67 68 74 20 74  hough we might t
28c22 72 79 20 74 6f 20 70 69 63 6b 20 74 68 65 20 62  ry to pick the b
28c23 65 73 74 20 69 6e 64 65 78 20 6d 75 6c 74 69 70  est index multip
28c24 6c 65 20 74 69 6d 65 73 2e 0a 20 20 2a 2a 20 46  le times..  ** F
28c25 6f 72 20 65 61 63 68 20 61 74 74 65 6d 70 74 20  or each attempt 
28c26 61 74 20 70 69 63 6b 69 6e 67 20 61 6e 20 69 6e  at picking an in
28c27 64 65 78 2c 20 74 68 65 20 6f 72 64 65 72 20 6f  dex, the order o
28c28 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 0a  f tables in the.
28c29 20 20 2a 2a 20 6a 6f 69 6e 20 6d 69 67 68 74 20    ** join might 
28c2a 62 65 20 64 69 66 66 65 72 65 6e 74 20 73 6f 20  be different so 
28c2b 77 65 20 68 61 76 65 20 74 6f 20 72 65 63 6f 6d  we have to recom
28c2c 70 75 74 65 20 74 68 65 20 75 73 61 62 6c 65 20  pute the usable 
28c2d 66 6c 61 67 0a 20 20 2a 2a 20 65 61 63 68 20 74  flag.  ** each t
28c2e 69 6d 65 2e 0a 20 20 2a 2f 0a 20 20 70 49 64 78  ime..  */.  pIdx
28c2f 43 6f 6e 73 20 3d 20 2a 28 73 74 72 75 63 74 20  Cons = *(struct 
28c30 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
28c31 6e 73 74 72 61 69 6e 74 2a 2a 29 26 70 49 64 78  nstraint**)&pIdx
28c32 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  Info->aConstrain
28c33 74 3b 0a 20 20 70 55 73 61 67 65 20 3d 20 70 49  t;.  pUsage = pI
28c34 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61  dxInfo->aConstra
28c35 69 6e 74 55 73 61 67 65 3b 0a 20 20 66 6f 72 28  intUsage;.  for(
28c36 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e 66 6f 2d  i=0; i<pIdxInfo-
28c37 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b 20 69 2b  >nConstraint; i+
28c38 2b 2c 20 70 49 64 78 43 6f 6e 73 2b 2b 29 7b 0a  +, pIdxCons++){.
28c39 20 20 20 20 6a 20 3d 20 70 49 64 78 43 6f 6e 73      j = pIdxCons
28c3a 2d 3e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20  ->iTermOffset;. 
28c3b 20 20 20 70 54 65 72 6d 20 3d 20 26 70 57 43 2d     pTerm = &pWC-
28c3c 3e 61 5b 6a 5d 3b 0a 20 20 20 20 70 49 64 78 43  >a[j];.    pIdxC
28c3d 6f 6e 73 2d 3e 75 73 61 62 6c 65 20 3d 20 20 28  ons->usable =  (
28c3e 70 54 65 72 6d 2d 3e 70 72 65 72 65 71 52 69 67  pTerm->prereqRig
28c3f 68 74 20 26 20 6e 6f 74 52 65 61 64 79 29 3d 3d  ht & notReady)==
28c40 30 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28  0;.  }.  memset(
28c41 70 55 73 61 67 65 2c 20 30 2c 20 73 69 7a 65 6f  pUsage, 0, sizeo
28c42 66 28 70 55 73 61 67 65 5b 30 5d 29 2a 70 49 64  f(pUsage[0])*pId
28c43 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
28c44 6e 74 29 3b 0a 20 20 69 66 28 20 70 49 64 78 49  nt);.  if( pIdxI
28c45 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72 65 65 49  nfo->needToFreeI
28c46 64 78 53 74 72 20 29 7b 0a 20 20 20 20 73 71 6c  dxStr ){.    sql
28c47 69 74 65 33 5f 66 72 65 65 28 70 49 64 78 49 6e  ite3_free(pIdxIn
28c48 66 6f 2d 3e 69 64 78 53 74 72 29 3b 0a 20 20 7d  fo->idxStr);.  }
28c49 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78  .  pIdxInfo->idx
28c4a 53 74 72 20 3d 20 30 3b 0a 20 20 70 49 64 78 49  Str = 0;.  pIdxI
28c4b 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 30 3b  nfo->idxNum = 0;
28c4c 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 65 65  .  pIdxInfo->nee
28c4d 64 54 6f 46 72 65 65 49 64 78 53 74 72 20 3d 20  dToFreeIdxStr = 
28c4e 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e 6f  0;.  pIdxInfo->o
28c4f 72 64 65 72 42 79 43 6f 6e 73 75 6d 65 64 20 3d  rderByConsumed =
28c50 20 30 3b 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   0;.  pIdxInfo->
28c51 65 73 74 69 6d 61 74 65 64 43 6f 73 74 20 3d 20  estimatedCost = 
28c52 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 2f  SQLITE_BIG_DBL /
28c53 20 32 2e 30 3b 0a 20 20 6e 4f 72 64 65 72 42 79   2.0;.  nOrderBy
28c54 20 3d 20 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72   = pIdxInfo->nOr
28c55 64 65 72 42 79 3b 0a 20 20 69 66 28 20 70 49 64  derBy;.  if( pId
28c56 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20  xInfo->nOrderBy 
28c57 26 26 20 21 6f 72 64 65 72 42 79 55 73 61 62 6c  && !orderByUsabl
28c58 65 20 29 7b 0a 20 20 20 20 2a 28 69 6e 74 2a 29  e ){.    *(int*)
28c59 26 70 49 64 78 49 6e 66 6f 2d 3e 6e 4f 72 64 65  &pIdxInfo->nOrde
28c5a 72 42 79 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20  rBy = 0;.  }..  
28c5b 28 76 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66  (void)sqlite3Saf
28c5c 65 74 79 4f 66 66 28 70 50 61 72 73 65 2d 3e 64  etyOff(pParse->d
28c5d 62 29 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45  b);.  WHERETRACE
28c5e 28 28 22 78 42 65 73 74 49 6e 64 65 78 20 66 6f  (("xBestIndex fo
28c5f 72 20 25 73 5c 6e 22 2c 20 70 54 61 62 2d 3e 7a  r %s\n", pTab->z
28c60 4e 61 6d 65 29 29 3b 0a 20 20 54 52 41 43 45 5f  Name));.  TRACE_
28c61 49 44 58 5f 49 4e 50 55 54 53 28 70 49 64 78 49  IDX_INPUTS(pIdxI
28c62 6e 66 6f 29 3b 0a 20 20 72 63 20 3d 20 70 56 74  nfo);.  rc = pVt
28c63 61 62 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 42 65  ab->pModule->xBe
28c64 73 74 49 6e 64 65 78 28 70 56 74 61 62 2c 20 70  stIndex(pVtab, p
28c65 49 64 78 49 6e 66 6f 29 3b 0a 20 20 54 52 41 43  IdxInfo);.  TRAC
28c66 45 5f 49 44 58 5f 4f 55 54 50 55 54 53 28 70 49  E_IDX_OUTPUTS(pI
28c67 64 78 49 6e 66 6f 29 3b 0a 20 20 28 76 6f 69 64  dxInfo);.  (void
28c68 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
28c69 28 70 50 61 72 73 65 2d 3e 64 62 29 3b 0a 0a 20  (pParse->db);.. 
28c6a 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
28c6b 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 72 63  OK ){.    if( rc
28c6c 3d 3d 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29  ==SQLITE_NOMEM )
28c6d 7b 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e  {.      pParse->
28c6e 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
28c6f 20 3d 20 31 3b 0a 20 20 20 20 7d 65 6c 73 65 20   = 1;.    }else 
28c70 69 66 28 20 21 70 56 74 61 62 2d 3e 7a 45 72 72  if( !pVtab->zErr
28c71 4d 73 67 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  Msg ){.      sql
28c72 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61  ite3ErrorMsg(pPa
28c73 72 73 65 2c 20 22 25 73 22 2c 20 73 71 6c 69 74  rse, "%s", sqlit
28c74 65 33 45 72 72 53 74 72 28 72 63 29 29 3b 0a 20  e3ErrStr(rc));. 
28c75 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28c76 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28  sqlite3ErrorMsg(
28c77 70 50 61 72 73 65 2c 20 22 25 73 22 2c 20 70 56  pParse, "%s", pV
28c78 74 61 62 2d 3e 7a 45 72 72 4d 73 67 29 3b 0a 20  tab->zErrMsg);. 
28c79 20 20 20 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74     }.  }.  sqlit
28c7a 65 33 44 62 46 72 65 65 28 70 50 61 72 73 65 2d  e3DbFree(pParse-
28c7b 3e 64 62 2c 20 70 56 74 61 62 2d 3e 7a 45 72 72  >db, pVtab->zErr
28c7c 4d 73 67 29 3b 0a 20 20 70 56 74 61 62 2d 3e 7a  Msg);.  pVtab->z
28c7d 45 72 72 4d 73 67 20 3d 20 30 3b 0a 0a 20 20 66  ErrMsg = 0;..  f
28c7e 6f 72 28 69 3d 30 3b 20 69 3c 70 49 64 78 49 6e  or(i=0; i<pIdxIn
28c7f 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  fo->nConstraint;
28c80 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 21   i++){.    if( !
28c81 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74  pIdxInfo->aConst
28c82 72 61 69 6e 74 5b 69 5d 2e 75 73 61 62 6c 65 20  raint[i].usable 
28c83 26 26 20 70 55 73 61 67 65 5b 69 5d 2e 61 72 67  && pUsage[i].arg
28c84 76 49 6e 64 65 78 3e 30 20 29 7b 0a 20 20 20 20  vIndex>0 ){.    
28c85 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 4d 73    sqlite3ErrorMs
28c86 67 28 70 50 61 72 73 65 2c 20 0a 20 20 20 20 20  g(pParse, .     
28c87 20 20 20 20 20 22 74 61 62 6c 65 20 25 73 3a 20       "table %s: 
28c88 78 42 65 73 74 49 6e 64 65 78 20 72 65 74 75 72  xBestIndex retur
28c89 6e 65 64 20 61 6e 20 69 6e 76 61 6c 69 64 20 70  ned an invalid p
28c8a 6c 61 6e 22 2c 20 70 54 61 62 2d 3e 7a 4e 61 6d  lan", pTab->zNam
28c8b 65 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  e);.      return
28c8c 20 30 2e 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a   0.0;.    }.  }.
28c8d 0a 20 20 2a 28 69 6e 74 2a 29 26 70 49 64 78 49  .  *(int*)&pIdxI
28c8e 6e 66 6f 2d 3e 6e 4f 72 64 65 72 42 79 20 3d 20  nfo->nOrderBy = 
28c8f 6e 4f 72 64 65 72 42 79 3b 0a 20 20 72 65 74 75  nOrderBy;.  retu
28c90 72 6e 20 70 49 64 78 49 6e 66 6f 2d 3e 65 73 74  rn pIdxInfo->est
28c91 69 6d 61 74 65 64 43 6f 73 74 3b 0a 7d 0a 23 65  imatedCost;.}.#e
28c92 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
28c93 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c 45  MIT_VIRTUALTABLE
28c94 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20   */../*.** Find 
28c95 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 66  the best index f
28c96 6f 72 20 61 63 63 65 73 73 69 6e 67 20 61 20 70  or accessing a p
28c97 61 72 74 69 63 75 6c 61 72 20 74 61 62 6c 65 2e  articular table.
28c98 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69 6e 74    Return a point
28c99 65 72 0a 2a 2a 20 74 6f 20 74 68 65 20 69 6e 64  er.** to the ind
28c9a 65 78 2c 20 66 6c 61 67 73 20 74 68 61 74 20 64  ex, flags that d
28c9b 65 73 63 72 69 62 65 20 68 6f 77 20 74 68 65 20  escribe how the 
28c9c 69 6e 64 65 78 20 73 68 6f 75 6c 64 20 62 65 20  index should be 
28c9d 75 73 65 64 2c 20 74 68 65 0a 2a 2a 20 6e 75 6d  used, the.** num
28c9e 62 65 72 20 6f 66 20 65 71 75 61 6c 69 74 79 20  ber of equality 
28c9f 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 61 6e 64  constraints, and
28ca0 20 74 68 65 20 22 63 6f 73 74 22 20 66 6f 72 20   the "cost" for 
28ca1 74 68 69 73 20 69 6e 64 65 78 2e 0a 2a 2a 0a 2a  this index..**.*
28ca2 2a 20 54 68 65 20 6c 6f 77 65 73 74 20 63 6f 73  * The lowest cos
28ca3 74 20 69 6e 64 65 78 20 77 69 6e 73 2e 20 20 54  t index wins.  T
28ca4 68 65 20 63 6f 73 74 20 69 73 20 61 6e 20 65 73  he cost is an es
28ca5 74 69 6d 61 74 65 20 6f 66 20 74 68 65 20 61 6d  timate of the am
28ca6 6f 75 6e 74 20 6f 66 0a 2a 2a 20 43 50 55 20 61  ount of.** CPU a
28ca7 6e 64 20 64 69 73 6b 20 49 2f 4f 20 6e 65 65 64  nd disk I/O need
28ca8 20 74 6f 20 70 72 6f 63 65 73 73 20 74 68 65 20   to process the 
28ca9 72 65 71 75 65 73 74 20 75 73 69 6e 67 20 74 68  request using th
28caa 65 20 73 65 6c 65 63 74 65 64 20 69 6e 64 65 78  e selected index
28cab 2e 0a 2a 2a 20 46 61 63 74 6f 72 73 20 74 68 61  ..** Factors tha
28cac 74 20 69 6e 66 6c 75 65 6e 63 65 20 63 6f 73 74  t influence cost
28cad 20 69 6e 63 6c 75 64 65 3a 0a 2a 2a 0a 2a 2a 20   include:.**.** 
28cae 20 20 20 2a 20 20 54 68 65 20 65 73 74 69 6d 61     *  The estima
28caf 74 65 64 20 6e 75 6d 62 65 72 20 6f 66 20 72 6f  ted number of ro
28cb0 77 73 20 74 68 61 74 20 77 69 6c 6c 20 62 65 20  ws that will be 
28cb1 72 65 74 72 69 65 76 65 64 2e 20 20 28 54 68 65  retrieved.  (The
28cb2 0a 2a 2a 20 20 20 20 20 20 20 66 65 77 65 72 20  .**       fewer 
28cb3 74 68 65 20 62 65 74 74 65 72 2e 29 0a 2a 2a 0a  the better.).**.
28cb4 2a 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65 72  **    *  Whether
28cb5 20 6f 72 20 6e 6f 74 20 73 6f 72 74 69 6e 67 20   or not sorting 
28cb6 6d 75 73 74 20 6f 63 63 75 72 2e 0a 2a 2a 0a 2a  must occur..**.*
28cb7 2a 20 20 20 20 2a 20 20 57 68 65 74 68 65 72 20  *    *  Whether 
28cb8 6f 72 20 6e 6f 74 20 74 68 65 72 65 20 6d 75 73  or not there mus
28cb9 74 20 62 65 20 73 65 70 61 72 61 74 65 20 6c 6f  t be separate lo
28cba 6f 6b 75 70 73 20 69 6e 20 74 68 65 0a 2a 2a 20  okups in the.** 
28cbb 20 20 20 20 20 20 69 6e 64 65 78 20 61 6e 64 20        index and 
28cbc 69 6e 20 74 68 65 20 6d 61 69 6e 20 74 61 62 6c  in the main tabl
28cbd 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72  e..**.** If ther
28cbe 65 20 77 61 73 20 61 6e 20 49 4e 44 45 58 45 44  e was an INDEXED
28cbf 20 42 59 20 63 6c 61 75 73 65 20 61 74 74 61 63   BY clause attac
28cc0 68 65 64 20 74 6f 20 74 68 65 20 74 61 62 6c 65  hed to the table
28cc1 20 69 6e 20 74 68 65 20 53 45 4c 45 43 54 0a 2a   in the SELECT.*
28cc2 2a 20 73 74 61 74 65 6d 65 6e 74 2c 20 74 68 65  * statement, the
28cc3 6e 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  n this function 
28cc4 6f 6e 6c 79 20 63 6f 6e 73 69 64 65 72 73 20 73  only considers s
28cc5 74 72 61 74 65 67 69 65 73 20 75 73 69 6e 67 20  trategies using 
28cc6 74 68 65 20 0a 2a 2a 20 6e 61 6d 65 64 20 69 6e  the .** named in
28cc7 64 65 78 2e 20 49 66 20 6f 6e 65 20 63 61 6e 6e  dex. If one cann
28cc8 6f 74 20 62 65 20 66 6f 75 6e 64 2c 20 74 68 65  ot be found, the
28cc9 6e 20 74 68 65 20 72 65 74 75 72 6e 65 64 20 63  n the returned c
28cca 6f 73 74 20 69 73 0a 2a 2a 20 53 51 4c 49 54 45  ost is.** SQLITE
28ccb 5f 42 49 47 5f 44 42 4c 2e 20 49 66 20 61 20 73  _BIG_DBL. If a s
28ccc 74 72 61 74 65 67 79 20 63 61 6e 20 62 65 20 66  trategy can be f
28ccd 6f 75 6e 64 20 74 68 61 74 20 75 73 65 73 20 74  ound that uses t
28cce 68 65 20 6e 61 6d 65 64 20 69 6e 64 65 78 2c 20  he named index, 
28ccf 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 63 6f 73  .** then the cos
28cd0 74 20 69 73 20 63 61 6c 63 75 6c 61 74 65 64 20  t is calculated 
28cd1 69 6e 20 74 68 65 20 75 73 75 61 6c 20 77 61 79  in the usual way
28cd2 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 20 4e 4f 54  ..**.** If a NOT
28cd3 20 49 4e 44 45 58 45 44 20 63 6c 61 75 73 65 20   INDEXED clause 
28cd4 77 61 73 20 61 74 74 61 63 68 65 64 20 74 6f 20  was attached to 
28cd5 74 68 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65  the table in the
28cd6 20 53 45 4c 45 43 54 20 0a 2a 2a 20 73 74 61 74   SELECT .** stat
28cd7 65 6d 65 6e 74 2c 20 74 68 65 6e 20 6e 6f 20 69  ement, then no i
28cd8 6e 64 65 78 65 73 20 61 72 65 20 63 6f 6e 73 69  ndexes are consi
28cd9 64 65 72 65 64 2e 20 48 6f 77 65 76 65 72 2c 20  dered. However, 
28cda 74 68 65 20 73 65 6c 65 63 74 65 64 20 0a 2a 2a  the selected .**
28cdb 20 73 74 61 74 65 67 79 20 6d 61 79 20 73 74 69   stategy may sti
28cdc 6c 6c 20 74 61 6b 65 20 61 64 76 61 6e 74 61 67  ll take advantag
28cdd 65 20 6f 66 20 74 68 65 20 74 61 62 6c 65 73 20  e of the tables 
28cde 62 75 69 6c 74 2d 69 6e 20 72 6f 77 69 64 0a 2a  built-in rowid.*
28cdf 2a 20 69 6e 64 65 78 2e 0a 2a 2f 0a 73 74 61 74  * index..*/.stat
28ce0 69 63 20 64 6f 75 62 6c 65 20 62 65 73 74 49 6e  ic double bestIn
28ce1 64 65 78 28 0a 20 20 50 61 72 73 65 20 2a 70 50  dex(.  Parse *pP
28ce2 61 72 73 65 2c 20 20 20 20 20 20 20 20 20 20 20  arse,           
28ce3 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 69 6e     /* The parsin
28ce4 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 57  g context */.  W
28ce5 68 65 72 65 43 6c 61 75 73 65 20 2a 70 57 43 2c  hereClause *pWC,
28ce6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
28ce7 65 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a  e WHERE clause *
28ce8 2f 0a 20 20 73 74 72 75 63 74 20 53 72 63 4c 69  /.  struct SrcLi
28ce9 73 74 5f 69 74 65 6d 20 2a 70 53 72 63 2c 20 20  st_item *pSrc,  
28cea 2f 2a 20 54 68 65 20 46 52 4f 4d 20 63 6c 61 75  /* The FROM clau
28ceb 73 65 20 74 65 72 6d 20 74 6f 20 73 65 61 72 63  se term to searc
28cec 68 20 2a 2f 0a 20 20 42 69 74 6d 61 73 6b 20 6e  h */.  Bitmask n
28ced 6f 74 52 65 61 64 79 2c 20 20 20 20 20 20 20 20  otReady,        
28cee 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 63 75     /* Mask of cu
28cef 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
28cf0 6f 74 20 61 76 61 69 6c 61 62 6c 65 20 2a 2f 0a  ot available */.
28cf1 20 20 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64    ExprList *pOrd
28cf2 65 72 42 79 2c 20 20 20 20 20 20 20 20 20 2f 2a  erBy,         /*
28cf3 20 54 68 65 20 6f 72 64 65 72 20 62 79 20 63 6c   The order by cl
28cf4 61 75 73 65 20 2a 2f 0a 20 20 49 6e 64 65 78 20  ause */.  Index 
28cf5 2a 2a 70 70 49 6e 64 65 78 2c 20 20 20 20 20 20  **ppIndex,      
28cf6 20 20 20 20 20 20 2f 2a 20 4d 61 6b 65 20 2a 70        /* Make *p
28cf7 70 49 6e 64 65 78 20 70 6f 69 6e 74 20 74 6f 20  pIndex point to 
28cf8 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 2a  the best index *
28cf9 2f 0a 20 20 69 6e 74 20 2a 70 46 6c 61 67 73 2c  /.  int *pFlags,
28cfa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28cfb 2f 2a 20 50 75 74 20 66 6c 61 67 73 20 64 65 73  /* Put flags des
28cfc 63 72 69 62 69 6e 67 20 74 68 69 73 20 63 68 6f  cribing this cho
28cfd 69 63 65 20 69 6e 20 2a 70 46 6c 61 67 73 20 2a  ice in *pFlags *
28cfe 2f 0a 20 20 69 6e 74 20 2a 70 6e 45 71 20 20 20  /.  int *pnEq   
28cff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d00 2f 2a 20 50 75 74 20 74 68 65 20 6e 75 6d 62 65  /* Put the numbe
28d01 72 20 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f  r of == or IN co
28d02 6e 73 74 72 61 69 6e 74 73 20 68 65 72 65 20 2a  nstraints here *
28d03 2f 0a 29 7b 0a 20 20 57 68 65 72 65 54 65 72 6d  /.){.  WhereTerm
28d04 20 2a 70 54 65 72 6d 3b 0a 20 20 49 6e 64 65 78   *pTerm;.  Index
28d05 20 2a 62 65 73 74 49 64 78 20 3d 20 30 3b 20 20   *bestIdx = 0;  
28d06 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78 20         /* Index 
28d07 74 68 61 74 20 67 69 76 65 73 20 74 68 65 20 6c  that gives the l
28d08 6f 77 65 73 74 20 63 6f 73 74 20 2a 2f 0a 20 20  owest cost */.  
28d09 64 6f 75 62 6c 65 20 6c 6f 77 65 73 74 43 6f 73  double lowestCos
28d0a 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
28d0b 68 65 20 63 6f 73 74 20 6f 66 20 75 73 69 6e 67  he cost of using
28d0c 20 62 65 73 74 49 64 78 20 2a 2f 0a 20 20 69 6e   bestIdx */.  in
28d0d 74 20 62 65 73 74 46 6c 61 67 73 20 3d 20 30 3b  t bestFlags = 0;
28d0e 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
28d0f 67 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  gs associated wi
28d10 74 68 20 62 65 73 74 49 64 78 20 2a 2f 0a 20 20  th bestIdx */.  
28d11 69 6e 74 20 62 65 73 74 4e 45 71 20 3d 20 30 3b  int bestNEq = 0;
28d12 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42              /* B
28d13 65 73 74 20 76 61 6c 75 65 20 66 6f 72 20 6e 45  est value for nE
28d14 71 20 2a 2f 0a 20 20 69 6e 74 20 69 43 75 72 20  q */.  int iCur 
28d15 3d 20 70 53 72 63 2d 3e 69 43 75 72 73 6f 72 3b  = pSrc->iCursor;
28d16 20 20 20 2f 2a 20 54 68 65 20 63 75 72 73 6f 72     /* The cursor
28d17 20 6f 66 20 74 68 65 20 74 61 62 6c 65 20 74 6f   of the table to
28d18 20 62 65 20 61 63 63 65 73 73 65 64 20 2a 2f 0a   be accessed */.
28d19 20 20 49 6e 64 65 78 20 2a 70 50 72 6f 62 65 3b    Index *pProbe;
28d1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
28d1b 20 41 6e 20 69 6e 64 65 78 20 77 65 20 61 72 65   An index we are
28d1c 20 65 76 61 6c 75 61 74 69 6e 67 20 2a 2f 0a 20   evaluating */. 
28d1d 20 69 6e 74 20 72 65 76 3b 20 20 20 20 20 20 20   int rev;       
28d1e 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
28d1f 54 72 75 65 20 74 6f 20 73 63 61 6e 20 69 6e 20  True to scan in 
28d20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f  reverse order */
28d21 0a 20 20 69 6e 74 20 66 6c 61 67 73 3b 20 20 20  .  int flags;   
28d22 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
28d23 2a 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74  * Flags associat
28d24 65 64 20 77 69 74 68 20 70 50 72 6f 62 65 20 2a  ed with pProbe *
28d25 2f 0a 20 20 69 6e 74 20 6e 45 71 3b 20 20 20 20  /.  int nEq;    
28d26 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28d27 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20  /* Number of == 
28d28 6f 72 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74  or IN constraint
28d29 73 20 2a 2f 0a 20 20 69 6e 74 20 65 71 54 65 72  s */.  int eqTer
28d2a 6d 4d 61 73 6b 3b 20 20 20 20 20 20 20 20 20 20  mMask;          
28d2b 20 20 20 2f 2a 20 4d 61 73 6b 20 6f 66 20 76 61     /* Mask of va
28d2c 6c 69 64 20 65 71 75 61 6c 69 74 79 20 6f 70 65  lid equality ope
28d2d 72 61 74 6f 72 73 20 2a 2f 0a 20 20 64 6f 75 62  rators */.  doub
28d2e 6c 65 20 63 6f 73 74 3b 20 20 20 20 20 20 20 20  le cost;        
28d2f 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 73 74 20          /* Cost 
28d30 6f 66 20 75 73 69 6e 67 20 70 50 72 6f 62 65 20  of using pProbe 
28d31 2a 2f 0a 0a 20 20 57 48 45 52 45 54 52 41 43 45  */..  WHERETRACE
28d32 28 28 22 62 65 73 74 49 6e 64 65 78 3a 20 74 62  (("bestIndex: tb
28d33 6c 3d 25 73 20 6e 6f 74 52 65 61 64 79 3d 25 6c  l=%s notReady=%l
28d34 6c 78 5c 6e 22 2c 20 70 53 72 63 2d 3e 70 54 61  lx\n", pSrc->pTa
28d35 62 2d 3e 7a 4e 61 6d 65 2c 20 6e 6f 74 52 65 61  b->zName, notRea
28d36 64 79 29 29 3b 0a 20 20 6c 6f 77 65 73 74 43 6f  dy));.  lowestCo
28d37 73 74 20 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f  st = SQLITE_BIG_
28d38 44 42 4c 3b 0a 20 20 70 50 72 6f 62 65 20 3d 20  DBL;.  pProbe = 
28d39 70 53 72 63 2d 3e 70 54 61 62 2d 3e 70 49 6e 64  pSrc->pTab->pInd
28d3a 65 78 3b 0a 20 20 69 66 28 20 70 53 72 63 2d 3e  ex;.  if( pSrc->
28d3b 6e 6f 74 49 6e 64 65 78 65 64 20 29 7b 0a 20 20  notIndexed ){.  
28d3c 20 20 70 50 72 6f 62 65 20 3d 20 30 3b 0a 20 20    pProbe = 0;.  
28d3d 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  }..  /* If the t
28d3e 61 62 6c 65 20 68 61 73 20 6e 6f 20 69 6e 64 69  able has no indi
28d3f 63 65 73 20 61 6e 64 20 74 68 65 72 65 20 61 72  ces and there ar
28d40 65 20 6e 6f 20 74 65 72 6d 73 20 69 6e 20 74 68  e no terms in th
28d41 65 20 77 68 65 72 65 0a 20 20 2a 2a 20 63 6c 61  e where.  ** cla
28d42 75 73 65 20 74 68 61 74 20 72 65 66 65 72 20 74  use that refer t
28d43 6f 20 74 68 65 20 52 4f 57 49 44 2c 20 74 68 65  o the ROWID, the
28d44 6e 20 77 65 20 77 69 6c 6c 20 6e 65 76 65 72 20  n we will never 
28d45 62 65 20 61 62 6c 65 20 74 6f 20 64 6f 0a 20 20  be able to do.  
28d46 2a 2a 20 61 6e 79 74 68 69 6e 67 20 6f 74 68 65  ** anything othe
28d47 72 20 74 68 61 6e 20 61 20 66 75 6c 6c 20 74 61  r than a full ta
28d48 62 6c 65 20 73 63 61 6e 20 6f 6e 20 74 68 69 73  ble scan on this
28d49 20 74 61 62 6c 65 2e 20 20 57 65 20 6d 69 67 68   table.  We migh
28d4a 74 20 61 73 0a 20 20 2a 2a 20 77 65 6c 6c 20 70  t as.  ** well p
28d4b 75 74 20 69 74 20 66 69 72 73 74 20 69 6e 20 74  ut it first in t
28d4c 68 65 20 6a 6f 69 6e 20 6f 72 64 65 72 2e 20 20  he join order.  
28d4d 54 68 61 74 20 77 61 79 2c 20 70 65 72 68 61 70  That way, perhap
28d4e 73 20 69 74 20 63 61 6e 20 62 65 0a 20 20 2a 2a  s it can be.  **
28d4f 20 72 65 66 65 72 65 6e 63 65 64 20 62 79 20 6f   referenced by o
28d50 74 68 65 72 20 74 61 62 6c 65 73 20 69 6e 20 74  ther tables in t
28d51 68 65 20 6a 6f 69 6e 2e 0a 20 20 2a 2f 0a 20 20  he join..  */.  
28d52 69 66 28 20 70 50 72 6f 62 65 3d 3d 30 20 26 26  if( pProbe==0 &&
28d53 0a 20 20 20 20 20 66 69 6e 64 54 65 72 6d 28 70  .     findTerm(p
28d54 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 30 2c  WC, iCur, -1, 0,
28d55 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f   WO_EQ|WO_IN|WO_
28d56 4c 54 7c 57 4f 5f 4c 45 7c 57 4f 5f 47 54 7c 57  LT|WO_LE|WO_GT|W
28d57 4f 5f 47 45 2c 30 29 3d 3d 30 20 26 26 0a 20 20  O_GE,0)==0 &&.  
28d58 20 20 20 28 70 4f 72 64 65 72 42 79 3d 3d 30 20     (pOrderBy==0 
28d59 7c 7c 20 21 73 6f 72 74 61 62 6c 65 42 79 52 6f  || !sortableByRo
28d5a 77 69 64 28 69 43 75 72 2c 20 70 4f 72 64 65 72  wid(iCur, pOrder
28d5b 42 79 2c 20 70 57 43 2d 3e 70 4d 61 73 6b 53 65  By, pWC->pMaskSe
28d5c 74 2c 20 26 72 65 76 29 29 20 29 7b 0a 20 20 20  t, &rev)) ){.   
28d5d 20 2a 70 46 6c 61 67 73 20 3d 20 30 3b 0a 20 20   *pFlags = 0;.  
28d5e 20 20 2a 70 70 49 6e 64 65 78 20 3d 20 30 3b 0a    *ppIndex = 0;.
28d5f 20 20 20 20 2a 70 6e 45 71 20 3d 20 30 3b 0a 20      *pnEq = 0;. 
28d60 20 20 20 72 65 74 75 72 6e 20 30 2e 30 3b 0a 20     return 0.0;. 
28d61 20 7d 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 66   }..  /* Check f
28d62 6f 72 20 61 20 72 6f 77 69 64 3d 45 58 50 52 20  or a rowid=EXPR 
28d63 6f 72 20 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e  or rowid IN (...
28d64 29 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 49  ) constraints. I
28d65 66 20 74 68 65 72 65 20 77 61 73 0a 20 20 2a 2a  f there was.  **
28d66 20 61 6e 20 49 4e 44 45 58 45 44 20 42 59 20 63   an INDEXED BY c
28d67 6c 61 75 73 65 20 61 74 74 61 63 68 65 64 20 74  lause attached t
28d68 6f 20 74 68 69 73 20 74 61 62 6c 65 2c 20 73 6b  o this table, sk
28d69 69 70 20 74 68 69 73 20 73 74 65 70 2e 0a 20 20  ip this step..  
28d6a 2a 2f 0a 20 20 69 66 28 20 21 70 53 72 63 2d 3e  */.  if( !pSrc->
28d6b 70 49 6e 64 65 78 20 29 7b 0a 20 20 20 20 70 54  pIndex ){.    pT
28d6c 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70  erm = findTerm(p
28d6d 57 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f  WC, iCur, -1, no
28d6e 74 52 65 61 64 79 2c 20 57 4f 5f 45 51 7c 57 4f  tReady, WO_EQ|WO
28d6f 5f 49 4e 2c 20 30 29 3b 0a 20 20 20 20 69 66 28  _IN, 0);.    if(
28d70 20 70 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20   pTerm ){.      
28d71 45 78 70 72 20 2a 70 45 78 70 72 3b 0a 20 20 20  Expr *pExpr;.   
28d72 20 20 20 2a 70 70 49 6e 64 65 78 20 3d 20 30 3b     *ppIndex = 0;
28d73 0a 20 20 20 20 20 20 62 65 73 74 46 6c 61 67 73  .      bestFlags
28d74 20 3d 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45   = WHERE_ROWID_E
28d75 51 3b 0a 20 20 20 20 20 20 69 66 28 20 70 54 65  Q;.      if( pTe
28d76 72 6d 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  rm->eOperator & 
28d77 57 4f 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20  WO_EQ ){.       
28d78 20 2f 2a 20 52 6f 77 69 64 3d 3d 20 69 73 20 61   /* Rowid== is a
28d79 6c 77 61 79 73 20 74 68 65 20 62 65 73 74 20 70  lways the best p
28d7a 69 63 6b 2e 20 20 4c 6f 6f 6b 20 6e 6f 20 66 75  ick.  Look no fu
28d7b 72 74 68 65 72 2e 20 20 42 65 63 61 75 73 65 20  rther.  Because 
28d7c 6f 6e 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20  only.        ** 
28d7d 61 20 73 69 6e 67 6c 65 20 72 6f 77 20 69 73 20  a single row is 
28d7e 67 65 6e 65 72 61 74 65 64 2c 20 6f 75 74 70 75  generated, outpu
28d7f 74 20 69 73 20 61 6c 77 61 79 73 20 69 6e 20 73  t is always in s
28d80 6f 72 74 65 64 20 6f 72 64 65 72 20 2a 2f 0a 20  orted order */. 
28d81 20 20 20 20 20 20 20 2a 70 46 6c 61 67 73 20 3d         *pFlags =
28d82 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20   WHERE_ROWID_EQ 
28d83 7c 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a  | WHERE_UNIQUE;.
28d84 20 20 20 20 20 20 20 20 2a 70 6e 45 71 20 3d 20          *pnEq = 
28d85 31 3b 0a 20 20 20 20 20 20 20 20 57 48 45 52 45  1;.        WHERE
28d86 54 52 41 43 45 28 28 22 2e 2e 2e 20 62 65 73 74  TRACE(("... best
28d87 20 69 73 20 72 6f 77 69 64 5c 6e 22 29 29 3b 0a   is rowid\n"));.
28d88 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 30          return 0
28d89 2e 30 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20  .0;.      }else 
28d8a 69 66 28 20 28 70 45 78 70 72 20 3d 20 70 54 65  if( (pExpr = pTe
28d8b 72 6d 2d 3e 70 45 78 70 72 29 2d 3e 70 4c 69 73  rm->pExpr)->pLis
28d8c 74 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  t!=0 ){.        
28d8d 2f 2a 20 52 6f 77 69 64 20 49 4e 20 28 4c 49 53  /* Rowid IN (LIS
28d8e 54 29 3a 20 63 6f 73 74 20 69 73 20 4e 6c 6f 67  T): cost is Nlog
28d8f 4e 20 77 68 65 72 65 20 4e 20 69 73 20 74 68 65  N where N is the
28d90 20 6e 75 6d 62 65 72 20 6f 66 20 6c 69 73 74 0a   number of list.
28d91 20 20 20 20 20 20 20 20 2a 2a 20 65 6c 65 6d 65          ** eleme
28d92 6e 74 73 2e 20 20 2a 2f 0a 20 20 20 20 20 20 20  nts.  */.       
28d93 20 6c 6f 77 65 73 74 43 6f 73 74 20 3d 20 70 45   lowestCost = pE
28d94 78 70 72 2d 3e 70 4c 69 73 74 2d 3e 6e 45 78 70  xpr->pList->nExp
28d95 72 3b 0a 20 20 20 20 20 20 20 20 6c 6f 77 65 73  r;.        lowes
28d96 74 43 6f 73 74 20 2a 3d 20 65 73 74 4c 6f 67 28  tCost *= estLog(
28d97 6c 6f 77 65 73 74 43 6f 73 74 29 3b 0a 20 20 20  lowestCost);.   
28d98 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
28d99 20 20 2f 2a 20 52 6f 77 69 64 20 49 4e 20 28 53    /* Rowid IN (S
28d9a 45 4c 45 43 54 29 3a 20 63 6f 73 74 20 69 73 20  ELECT): cost is 
28d9b 4e 6c 6f 67 4e 20 77 68 65 72 65 20 4e 20 69 73  NlogN where N is
28d9c 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 72   the number of r
28d9d 6f 77 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 69  ows.        ** i
28d9e 6e 20 74 68 65 20 72 65 73 75 6c 74 20 6f 66 20  n the result of 
28d9f 74 68 65 20 69 6e 6e 65 72 20 73 65 6c 65 63 74  the inner select
28da0 2e 20 20 57 65 20 68 61 76 65 20 6e 6f 20 77 61  .  We have no wa
28da1 79 20 74 6f 20 65 73 74 69 6d 61 74 65 0a 20 20  y to estimate.  
28da2 20 20 20 20 20 20 2a 2a 20 74 68 61 74 20 76 61        ** that va
28da3 6c 75 65 20 73 6f 20 6d 61 6b 65 20 61 20 77 69  lue so make a wi
28da4 6c 64 20 67 75 65 73 73 2e 20 2a 2f 0a 20 20 20  ld guess. */.   
28da5 20 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20       lowestCost 
28da6 3d 20 32 30 30 3b 0a 20 20 20 20 20 20 7d 0a 20  = 200;.      }. 
28da7 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
28da8 28 22 2e 2e 2e 20 72 6f 77 69 64 20 49 4e 20 63  ("... rowid IN c
28da9 6f 73 74 3a 20 25 2e 39 67 5c 6e 22 2c 20 6c 6f  ost: %.9g\n", lo
28daa 77 65 73 74 43 6f 73 74 29 29 3b 0a 20 20 20 20  westCost));.    
28dab 7d 0a 20 20 0a 20 20 20 20 2f 2a 20 45 73 74 69  }.  .    /* Esti
28dac 6d 61 74 65 20 74 68 65 20 63 6f 73 74 20 6f 66  mate the cost of
28dad 20 61 20 74 61 62 6c 65 20 73 63 61 6e 2e 20 20   a table scan.  
28dae 49 66 20 77 65 20 64 6f 20 6e 6f 74 20 6b 6e 6f  If we do not kno
28daf 77 20 68 6f 77 20 6d 61 6e 79 0a 20 20 20 20 2a  w how many.    *
28db0 2a 20 65 6e 74 72 69 65 73 20 61 72 65 20 69 6e  * entries are in
28db1 20 74 68 65 20 74 61 62 6c 65 2c 20 75 73 65 20   the table, use 
28db2 31 20 6d 69 6c 6c 69 6f 6e 20 61 73 20 61 20 67  1 million as a g
28db3 75 65 73 73 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  uess..    */.   
28db4 20 63 6f 73 74 20 3d 20 70 50 72 6f 62 65 20 3f   cost = pProbe ?
28db5 20 70 50 72 6f 62 65 2d 3e 61 69 52 6f 77 45 73   pProbe->aiRowEs
28db6 74 5b 30 5d 20 3a 20 31 30 30 30 30 30 30 3b 0a  t[0] : 1000000;.
28db7 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28      WHERETRACE((
28db8 22 2e 2e 2e 20 74 61 62 6c 65 20 73 63 61 6e 20  "... table scan 
28db9 62 61 73 65 20 63 6f 73 74 3a 20 25 2e 39 67 5c  base cost: %.9g\
28dba 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20  n", cost));.    
28dbb 66 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 52 4f  flags = WHERE_RO
28dbc 57 49 44 5f 52 41 4e 47 45 3b 0a 20 20 0a 20 20  WID_RANGE;.  .  
28dbd 20 20 2f 2a 20 43 68 65 63 6b 20 66 6f 72 20 63    /* Check for c
28dbe 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 61 20  onstraints on a 
28dbf 72 61 6e 67 65 20 6f 66 20 72 6f 77 69 64 73 20  range of rowids 
28dc0 69 6e 20 61 20 74 61 62 6c 65 20 73 63 61 6e 2e  in a table scan.
28dc1 0a 20 20 20 20 2a 2f 0a 20 20 20 20 70 54 65 72  .    */.    pTer
28dc2 6d 20 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43  m = findTerm(pWC
28dc3 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74 52  , iCur, -1, notR
28dc4 65 61 64 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c  eady, WO_LT|WO_L
28dc5 45 7c 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 30  E|WO_GT|WO_GE, 0
28dc6 29 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  );.    if( pTerm
28dc7 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 66 69   ){.      if( fi
28dc8 6e 64 54 65 72 6d 28 70 57 43 2c 20 69 43 75 72  ndTerm(pWC, iCur
28dc9 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20  , -1, notReady, 
28dca 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 20  WO_LT|WO_LE, 0) 
28dcb 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61 67 73  ){.        flags
28dcc 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49   |= WHERE_TOP_LI
28dcd 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 63 6f 73  MIT;.        cos
28dce 74 20 2f 3d 20 33 3b 20 20 2f 2a 20 47 75 65 73  t /= 3;  /* Gues
28dcf 73 20 74 68 61 74 20 72 6f 77 69 64 3c 45 58 50  s that rowid<EXP
28dd0 52 20 65 6c 69 6d 69 6e 61 74 65 73 20 74 77 6f  R eliminates two
28dd1 2d 74 68 69 72 64 73 20 6f 72 20 72 6f 77 73 20  -thirds or rows 
28dd2 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  */.      }.     
28dd3 20 69 66 28 20 66 69 6e 64 54 65 72 6d 28 70 57   if( findTerm(pW
28dd4 43 2c 20 69 43 75 72 2c 20 2d 31 2c 20 6e 6f 74  C, iCur, -1, not
28dd5 52 65 61 64 79 2c 20 57 4f 5f 47 54 7c 57 4f 5f  Ready, WO_GT|WO_
28dd6 47 45 2c 20 30 29 20 29 7b 0a 20 20 20 20 20 20  GE, 0) ){.      
28dd7 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45    flags |= WHERE
28dd8 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20 20 20  _BTM_LIMIT;.    
28dd9 20 20 20 20 63 6f 73 74 20 2f 3d 20 33 3b 20 20      cost /= 3;  
28dda 2f 2a 20 47 75 65 73 73 20 74 68 61 74 20 72 6f  /* Guess that ro
28ddb 77 69 64 3e 45 58 50 52 20 65 6c 69 6d 69 6e 61  wid>EXPR elimina
28ddc 74 65 73 20 74 77 6f 2d 74 68 69 72 64 73 20 6f  tes two-thirds o
28ddd 66 20 72 6f 77 73 20 2a 2f 0a 20 20 20 20 20 20  f rows */.      
28dde 7d 0a 20 20 20 20 20 20 57 48 45 52 45 54 52 41  }.      WHERETRA
28ddf 43 45 28 28 22 2e 2e 2e 20 72 6f 77 69 64 20 72  CE(("... rowid r
28de0 61 6e 67 65 20 72 65 64 75 63 65 73 20 63 6f 73  ange reduces cos
28de1 74 20 74 6f 20 25 2e 39 67 5c 6e 22 2c 20 63 6f  t to %.9g\n", co
28de2 73 74 29 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  st));.    }else{
28de3 0a 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 30  .      flags = 0
28de4 3b 0a 20 20 20 20 7d 0a 20 20 0a 20 20 20 20 2f  ;.    }.  .    /
28de5 2a 20 49 66 20 74 68 65 20 74 61 62 6c 65 20 73  * If the table s
28de6 63 61 6e 20 64 6f 65 73 20 6e 6f 74 20 73 61 74  can does not sat
28de7 69 73 66 79 20 74 68 65 20 4f 52 44 45 52 20 42  isfy the ORDER B
28de8 59 20 63 6c 61 75 73 65 2c 20 69 6e 63 72 65 61  Y clause, increa
28de9 73 65 0a 20 20 20 20 2a 2a 20 74 68 65 20 63 6f  se.    ** the co
28dea 73 74 20 62 79 20 4e 6c 6f 67 4e 20 74 6f 20 63  st by NlogN to c
28deb 6f 76 65 72 20 74 68 65 20 65 78 70 65 6e 73 65  over the expense
28dec 20 6f 66 20 73 6f 72 74 69 6e 67 2e 20 2a 2f 0a   of sorting. */.
28ded 20 20 20 20 69 66 28 20 70 4f 72 64 65 72 42 79      if( pOrderBy
28dee 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 73 6f   ){.      if( so
28def 72 74 61 62 6c 65 42 79 52 6f 77 69 64 28 69 43  rtableByRowid(iC
28df0 75 72 2c 20 70 4f 72 64 65 72 42 79 2c 20 70 57  ur, pOrderBy, pW
28df1 43 2d 3e 70 4d 61 73 6b 53 65 74 2c 20 26 72 65  C->pMaskSet, &re
28df2 76 29 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c  v) ){.        fl
28df3 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 4f 52 44  ags |= WHERE_ORD
28df4 45 52 42 59 7c 57 48 45 52 45 5f 52 4f 57 49 44  ERBY|WHERE_ROWID
28df5 5f 52 41 4e 47 45 3b 0a 20 20 20 20 20 20 20 20  _RANGE;.        
28df6 69 66 28 20 72 65 76 20 29 7b 0a 20 20 20 20 20  if( rev ){.     
28df7 20 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48       flags |= WH
28df8 45 52 45 5f 52 45 56 45 52 53 45 3b 0a 20 20 20  ERE_REVERSE;.   
28df9 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c       }.      }el
28dfa 73 65 7b 0a 20 20 20 20 20 20 20 20 63 6f 73 74  se{.        cost
28dfb 20 2b 3d 20 63 6f 73 74 2a 65 73 74 4c 6f 67 28   += cost*estLog(
28dfc 63 6f 73 74 29 3b 0a 20 20 20 20 20 20 20 20 57  cost);.        W
28dfd 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
28dfe 73 6f 72 74 69 6e 67 20 69 6e 63 72 65 61 73 65  sorting increase
28dff 73 20 63 6f 73 74 20 74 6f 20 25 2e 39 67 5c 6e  s cost to %.9g\n
28e00 22 2c 20 63 6f 73 74 29 29 3b 0a 20 20 20 20 20  ", cost));.     
28e01 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28   }.    }.    if(
28e02 20 63 6f 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74   cost<lowestCost
28e03 20 29 7b 0a 20 20 20 20 20 20 6c 6f 77 65 73 74   ){.      lowest
28e04 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20 20  Cost = cost;.   
28e05 20 20 20 62 65 73 74 46 6c 61 67 73 20 3d 20 66     bestFlags = f
28e06 6c 61 67 73 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  lags;.    }.  }.
28e07 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 70 53 72  .  /* If the pSr
28e08 63 20 74 61 62 6c 65 20 69 73 20 74 68 65 20 72  c table is the r
28e09 69 67 68 74 20 74 61 62 6c 65 20 6f 66 20 61 20  ight table of a 
28e0a 4c 45 46 54 20 4a 4f 49 4e 20 74 68 65 6e 20 77  LEFT JOIN then w
28e0b 65 20 6d 61 79 20 6e 6f 74 0a 20 20 2a 2a 20 75  e may not.  ** u
28e0c 73 65 20 61 6e 20 69 6e 64 65 78 20 74 6f 20 73  se an index to s
28e0d 61 74 69 73 66 79 20 49 53 20 4e 55 4c 4c 20 63  atisfy IS NULL c
28e0e 6f 6e 73 74 72 61 69 6e 74 73 20 6f 6e 20 74 68  onstraints on th
28e0f 61 74 20 74 61 62 6c 65 2e 20 20 54 68 69 73 20  at table.  This 
28e10 69 73 0a 20 20 2a 2a 20 62 65 63 61 75 73 65 20  is.  ** because 
28e11 63 6f 6c 75 6d 6e 73 20 6d 69 67 68 74 20 65 6e  columns might en
28e12 64 20 75 70 20 62 65 69 6e 67 20 4e 55 4c 4c 20  d up being NULL 
28e13 69 66 20 74 68 65 20 74 61 62 6c 65 20 64 6f 65  if the table doe
28e14 73 20 6e 6f 74 20 6d 61 74 63 68 20 2d 0a 20 20  s not match -.  
28e15 2a 2a 20 61 20 63 69 72 63 75 6d 73 74 61 6e 63  ** a circumstanc
28e16 65 20 77 68 69 63 68 20 74 68 65 20 69 6e 64 65  e which the inde
28e17 78 20 63 61 6e 6e 6f 74 20 68 65 6c 70 20 75 73  x cannot help us
28e18 20 64 69 73 63 6f 76 65 72 2e 20 20 54 69 63 6b   discover.  Tick
28e19 65 74 20 23 32 31 37 37 2e 0a 20 20 2a 2f 0a 20  et #2177..  */. 
28e1a 20 69 66 28 20 28 70 53 72 63 2d 3e 6a 6f 69 6e   if( (pSrc->join
28e1b 74 79 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21  type & JT_LEFT)!
28e1c 3d 30 20 29 7b 0a 20 20 20 20 65 71 54 65 72 6d  =0 ){.    eqTerm
28e1d 4d 61 73 6b 20 3d 20 57 4f 5f 45 51 7c 57 4f 5f  Mask = WO_EQ|WO_
28e1e 49 4e 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  IN;.  }else{.   
28e1f 20 65 71 54 65 72 6d 4d 61 73 6b 20 3d 20 57 4f   eqTermMask = WO
28e20 5f 45 51 7c 57 4f 5f 49 4e 7c 57 4f 5f 49 53 4e  _EQ|WO_IN|WO_ISN
28e21 55 4c 4c 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4c  ULL;.  }..  /* L
28e22 6f 6f 6b 20 61 74 20 65 61 63 68 20 69 6e 64 65  ook at each inde
28e23 78 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 53  x..  */.  if( pS
28e24 72 63 2d 3e 70 49 6e 64 65 78 20 29 7b 0a 20 20  rc->pIndex ){.  
28e25 20 20 70 50 72 6f 62 65 20 3d 20 70 53 72 63 2d    pProbe = pSrc-
28e26 3e 70 49 6e 64 65 78 3b 0a 20 20 7d 0a 20 20 66  >pIndex;.  }.  f
28e27 6f 72 28 3b 20 70 50 72 6f 62 65 3b 20 70 50 72  or(; pProbe; pPr
28e28 6f 62 65 3d 28 70 53 72 63 2d 3e 70 49 6e 64 65  obe=(pSrc->pInde
28e29 78 20 3f 20 30 20 3a 20 70 50 72 6f 62 65 2d 3e  x ? 0 : pProbe->
28e2a 70 4e 65 78 74 29 29 7b 0a 20 20 20 20 69 6e 74  pNext)){.    int
28e2b 20 69 3b 20 20 20 20 20 20 20 20 20 20 20 20 20   i;             
28e2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 6f 6f            /* Loo
28e2d 70 20 63 6f 75 6e 74 65 72 20 2a 2f 0a 20 20 20  p counter */.   
28e2e 20 64 6f 75 62 6c 65 20 69 6e 4d 75 6c 74 69 70   double inMultip
28e2f 6c 69 65 72 20 3d 20 31 3b 0a 0a 20 20 20 20 57  lier = 1;..    W
28e30 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e 2e 20  HERETRACE(("... 
28e31 69 6e 64 65 78 20 25 73 3a 5c 6e 22 2c 20 70 50  index %s:\n", pP
28e32 72 6f 62 65 2d 3e 7a 4e 61 6d 65 29 29 3b 0a 0a  robe->zName));..
28e33 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 74 68 65      /* Count the
28e34 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
28e35 6e 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20  ns in the index 
28e36 74 68 61 74 20 61 72 65 20 73 61 74 69 73 66 69  that are satisfi
28e37 65 64 0a 20 20 20 20 2a 2a 20 62 79 20 78 3d 45  ed.    ** by x=E
28e38 58 50 52 20 63 6f 6e 73 74 72 61 69 6e 74 73 20  XPR constraints 
28e39 6f 72 20 78 20 49 4e 20 28 2e 2e 2e 29 20 63 6f  or x IN (...) co
28e3a 6e 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 2a  nstraints..    *
28e3b 2f 0a 20 20 20 20 66 6c 61 67 73 20 3d 20 30 3b  /.    flags = 0;
28e3c 0a 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  .    for(i=0; i<
28e3d 70 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b  pProbe->nColumn;
28e3e 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74   i++){.      int
28e3f 20 6a 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43   j = pProbe->aiC
28e40 6f 6c 75 6d 6e 5b 69 5d 3b 0a 20 20 20 20 20 20  olumn[i];.      
28e41 70 54 65 72 6d 20 3d 20 66 69 6e 64 54 65 72 6d  pTerm = findTerm
28e42 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20 6e  (pWC, iCur, j, n
28e43 6f 74 52 65 61 64 79 2c 20 65 71 54 65 72 6d 4d  otReady, eqTermM
28e44 61 73 6b 2c 20 70 50 72 6f 62 65 29 3b 0a 20 20  ask, pProbe);.  
28e45 20 20 20 20 69 66 28 20 70 54 65 72 6d 3d 3d 30      if( pTerm==0
28e46 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
28e47 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  flags |= WHERE_C
28e48 4f 4c 55 4d 4e 5f 45 51 3b 0a 20 20 20 20 20 20  OLUMN_EQ;.      
28e49 69 66 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65 72  if( pTerm->eOper
28e4a 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 7b 0a  ator & WO_IN ){.
28e4b 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70 45          Expr *pE
28e4c 78 70 72 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  xpr = pTerm->pEx
28e4d 70 72 3b 0a 20 20 20 20 20 20 20 20 66 6c 61 67  pr;.        flag
28e4e 73 20 7c 3d 20 57 48 45 52 45 5f 43 4f 4c 55 4d  s |= WHERE_COLUM
28e4f 4e 5f 49 4e 3b 0a 20 20 20 20 20 20 20 20 69 66  N_IN;.        if
28e50 28 20 70 45 78 70 72 2d 3e 70 53 65 6c 65 63 74  ( pExpr->pSelect
28e51 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  !=0 ){.         
28e52 20 69 6e 4d 75 6c 74 69 70 6c 69 65 72 20 2a 3d   inMultiplier *=
28e53 20 32 35 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c   25;.        }el
28e54 73 65 20 69 66 28 20 41 4c 57 41 59 53 28 70 45  se if( ALWAYS(pE
28e55 78 70 72 2d 3e 70 4c 69 73 74 29 20 29 7b 0a 20  xpr->pList) ){. 
28e56 20 20 20 20 20 20 20 20 20 69 6e 4d 75 6c 74 69           inMulti
28e57 70 6c 69 65 72 20 2a 3d 20 70 45 78 70 72 2d 3e  plier *= pExpr->
28e58 70 4c 69 73 74 2d 3e 6e 45 78 70 72 20 2b 20 31  pList->nExpr + 1
28e59 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
28e5a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63 6f    }.    }.    co
28e5b 73 74 20 3d 20 70 50 72 6f 62 65 2d 3e 61 69 52  st = pProbe->aiR
28e5c 6f 77 45 73 74 5b 69 5d 20 2a 20 69 6e 4d 75 6c  owEst[i] * inMul
28e5d 74 69 70 6c 69 65 72 20 2a 20 65 73 74 4c 6f 67  tiplier * estLog
28e5e 28 69 6e 4d 75 6c 74 69 70 6c 69 65 72 29 3b 0a  (inMultiplier);.
28e5f 20 20 20 20 6e 45 71 20 3d 20 69 3b 0a 20 20 20      nEq = i;.   
28e60 20 69 66 28 20 70 50 72 6f 62 65 2d 3e 6f 6e 45   if( pProbe->onE
28e61 72 72 6f 72 21 3d 4f 45 5f 4e 6f 6e 65 20 26 26  rror!=OE_None &&
28e62 20 28 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f   (flags & WHERE_
28e63 43 4f 4c 55 4d 4e 5f 49 4e 29 3d 3d 30 0a 20 20  COLUMN_IN)==0.  
28e64 20 20 20 20 20 20 20 26 26 20 6e 45 71 3d 3d 70         && nEq==p
28e65 50 72 6f 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29  Probe->nColumn )
28e66 7b 0a 20 20 20 20 20 20 66 6c 61 67 73 20 7c 3d  {.      flags |=
28e67 20 57 48 45 52 45 5f 55 4e 49 51 55 45 3b 0a 20   WHERE_UNIQUE;. 
28e68 20 20 20 7d 0a 20 20 20 20 57 48 45 52 45 54 52     }.    WHERETR
28e69 41 43 45 28 28 22 2e 2e 2e 2e 2e 2e 20 6e 45 71  ACE(("...... nEq
28e6a 3d 25 64 20 69 6e 4d 75 6c 74 3d 25 2e 39 67 20  =%d inMult=%.9g 
28e6b 63 6f 73 74 3d 25 2e 39 67 5c 6e 22 2c 6e 45 71  cost=%.9g\n",nEq
28e6c 2c 69 6e 4d 75 6c 74 69 70 6c 69 65 72 2c 63 6f  ,inMultiplier,co
28e6d 73 74 29 29 3b 0a 0a 20 20 20 20 2f 2a 20 4c 6f  st));..    /* Lo
28e6e 6f 6b 20 66 6f 72 20 72 61 6e 67 65 20 63 6f 6e  ok for range con
28e6f 73 74 72 61 69 6e 74 73 0a 20 20 20 20 2a 2f 0a  straints.    */.
28e70 20 20 20 20 69 66 28 20 6e 45 71 3c 70 50 72 6f      if( nEq<pPro
28e71 62 65 2d 3e 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20  be->nColumn ){. 
28e72 20 20 20 20 20 69 6e 74 20 6a 20 3d 20 70 50 72       int j = pPr
28e73 6f 62 65 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6e 45  obe->aiColumn[nE
28e74 71 5d 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20  q];.      pTerm 
28e75 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
28e76 69 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64  iCur, j, notRead
28e77 79 2c 20 57 4f 5f 4c 54 7c 57 4f 5f 4c 45 7c 57  y, WO_LT|WO_LE|W
28e78 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70 50 72 6f  O_GT|WO_GE, pPro
28e79 62 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20 70  be);.      if( p
28e7a 54 65 72 6d 20 29 7b 0a 20 20 20 20 20 20 20 20  Term ){.        
28e7b 66 6c 61 67 73 20 7c 3d 20 57 48 45 52 45 5f 43  flags |= WHERE_C
28e7c 4f 4c 55 4d 4e 5f 52 41 4e 47 45 3b 0a 20 20 20  OLUMN_RANGE;.   
28e7d 20 20 20 20 20 69 66 28 20 66 69 6e 64 54 65 72       if( findTer
28e7e 6d 28 70 57 43 2c 20 69 43 75 72 2c 20 6a 2c 20  m(pWC, iCur, j, 
28e7f 6e 6f 74 52 65 61 64 79 2c 20 57 4f 5f 4c 54 7c  notReady, WO_LT|
28e80 57 4f 5f 4c 45 2c 20 70 50 72 6f 62 65 29 20 29  WO_LE, pProbe) )
28e81 7b 0a 20 20 20 20 20 20 20 20 20 20 66 6c 61 67  {.          flag
28e82 73 20 7c 3d 20 57 48 45 52 45 5f 54 4f 50 5f 4c  s |= WHERE_TOP_L
28e83 49 4d 49 54 3b 0a 20 20 20 20 20 20 20 20 20 20  IMIT;.          
28e84 63 6f 73 74 20 2f 3d 20 33 3b 0a 20 20 20 20 20  cost /= 3;.     
28e85 20 20 20 7d 0a 20 20 20 20 20 20 20 20 69 66 28     }.        if(
28e86 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20 69   findTerm(pWC, i
28e87 43 75 72 2c 20 6a 2c 20 6e 6f 74 52 65 61 64 79  Cur, j, notReady
28e88 2c 20 57 4f 5f 47 54 7c 57 4f 5f 47 45 2c 20 70  , WO_GT|WO_GE, p
28e89 50 72 6f 62 65 29 20 29 7b 0a 20 20 20 20 20 20  Probe) ){.      
28e8a 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45      flags |= WHE
28e8b 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 3b 0a 20 20  RE_BTM_LIMIT;.  
28e8c 20 20 20 20 20 20 20 20 63 6f 73 74 20 2f 3d 20          cost /= 
28e8d 33 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  3;.        }.   
28e8e 20 20 20 20 20 57 48 45 52 45 54 52 41 43 45 28       WHERETRACE(
28e8f 28 22 2e 2e 2e 2e 2e 2e 20 72 61 6e 67 65 20 72  ("...... range r
28e90 65 64 75 63 65 73 20 63 6f 73 74 20 74 6f 20 25  educes cost to %
28e91 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a  .9g\n", cost));.
28e92 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20        }.    }.. 
28e93 20 20 20 2f 2a 20 41 64 64 20 74 68 65 20 61 64     /* Add the ad
28e94 64 69 74 69 6f 6e 61 6c 20 63 6f 73 74 20 6f 66  ditional cost of
28e95 20 73 6f 72 74 69 6e 67 20 69 66 20 74 68 61 74   sorting if that
28e96 20 69 73 20 61 20 66 61 63 74 6f 72 2e 0a 20 20   is a factor..  
28e97 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70 4f 72    */.    if( pOr
28e98 64 65 72 42 79 20 29 7b 0a 20 20 20 20 20 20 69  derBy ){.      i
28e99 66 28 20 28 66 6c 61 67 73 20 26 20 57 48 45 52  f( (flags & WHER
28e9a 45 5f 43 4f 4c 55 4d 4e 5f 49 4e 29 3d 3d 30 20  E_COLUMN_IN)==0 
28e9b 26 26 0a 20 20 20 20 20 20 20 20 20 20 20 69 73  &&.           is
28e9c 53 6f 72 74 69 6e 67 49 6e 64 65 78 28 70 50 61  SortingIndex(pPa
28e9d 72 73 65 2c 70 57 43 2d 3e 70 4d 61 73 6b 53 65  rse,pWC->pMaskSe
28e9e 74 2c 70 50 72 6f 62 65 2c 69 43 75 72 2c 70 4f  t,pProbe,iCur,pO
28e9f 72 64 65 72 42 79 2c 6e 45 71 2c 26 72 65 76 29  rderBy,nEq,&rev)
28ea0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
28ea1 66 6c 61 67 73 3d 3d 30 20 29 7b 0a 20 20 20 20  flags==0 ){.    
28ea2 20 20 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48        flags = WH
28ea3 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e 47 45  ERE_COLUMN_RANGE
28ea4 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
28ea5 20 20 20 20 66 6c 61 67 73 20 7c 3d 20 57 48 45      flags |= WHE
28ea6 52 45 5f 4f 52 44 45 52 42 59 3b 0a 20 20 20 20  RE_ORDERBY;.    
28ea7 20 20 20 20 69 66 28 20 72 65 76 20 29 7b 0a 20      if( rev ){. 
28ea8 20 20 20 20 20 20 20 20 20 66 6c 61 67 73 20 7c           flags |
28ea9 3d 20 57 48 45 52 45 5f 52 45 56 45 52 53 45 3b  = WHERE_REVERSE;
28eaa 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
28eab 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
28eac 63 6f 73 74 20 2b 3d 20 63 6f 73 74 2a 65 73 74  cost += cost*est
28ead 4c 6f 67 28 63 6f 73 74 29 3b 0a 20 20 20 20 20  Log(cost);.     
28eae 20 20 20 57 48 45 52 45 54 52 41 43 45 28 28 22     WHERETRACE(("
28eaf 2e 2e 2e 2e 2e 2e 20 6f 72 64 65 72 62 79 20 69  ...... orderby i
28eb0 6e 63 72 65 61 73 65 73 20 63 6f 73 74 20 74 6f  ncreases cost to
28eb1 20 25 2e 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29   %.9g\n", cost))
28eb2 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
28eb3 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 74 6f  .    /* Check to
28eb4 20 73 65 65 20 69 66 20 77 65 20 63 61 6e 20 67   see if we can g
28eb5 65 74 20 61 77 61 79 20 77 69 74 68 20 75 73 69  et away with usi
28eb6 6e 67 20 6a 75 73 74 20 74 68 65 20 69 6e 64 65  ng just the inde
28eb7 78 20 77 69 74 68 6f 75 74 0a 20 20 20 20 2a 2a  x without.    **
28eb8 20 65 76 65 72 20 72 65 61 64 69 6e 67 20 74 68   ever reading th
28eb9 65 20 74 61 62 6c 65 2e 20 20 49 66 20 74 68 61  e table.  If tha
28eba 74 20 69 73 20 74 68 65 20 63 61 73 65 2c 20 74  t is the case, t
28ebb 68 65 6e 20 68 61 6c 76 65 20 74 68 65 0a 20 20  hen halve the.  
28ebc 20 20 2a 2a 20 63 6f 73 74 20 6f 66 20 74 68 69    ** cost of thi
28ebd 73 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2f 0a  s index..    */.
28ebe 20 20 20 20 69 66 28 20 66 6c 61 67 73 20 26 26      if( flags &&
28ebf 20 70 53 72 63 2d 3e 63 6f 6c 55 73 65 64 20 3c   pSrc->colUsed <
28ec0 20 28 28 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c   (((Bitmask)1)<<
28ec1 28 42 4d 53 2d 31 29 29 20 29 7b 0a 20 20 20 20  (BMS-1)) ){.    
28ec2 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20 70 53    Bitmask m = pS
28ec3 72 63 2d 3e 63 6f 6c 55 73 65 64 3b 0a 20 20 20  rc->colUsed;.   
28ec4 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20 20 20 20     int j;.      
28ec5 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 50 72 6f 62  for(j=0; j<pProb
28ec6 65 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b 2b 29  e->nColumn; j++)
28ec7 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 78 20  {.        int x 
28ec8 3d 20 70 50 72 6f 62 65 2d 3e 61 69 43 6f 6c 75  = pProbe->aiColu
28ec9 6d 6e 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69  mn[j];.        i
28eca 66 28 20 78 3c 42 4d 53 2d 31 20 29 7b 0a 20 20  f( x<BMS-1 ){.  
28ecb 20 20 20 20 20 20 20 20 6d 20 26 3d 20 7e 28 28          m &= ~((
28ecc 28 42 69 74 6d 61 73 6b 29 31 29 3c 3c 78 29 3b  (Bitmask)1)<<x);
28ecd 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
28ece 20 7d 0a 20 20 20 20 20 20 69 66 28 20 6d 3d 3d   }.      if( m==
28ecf 30 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 61  0 ){.        fla
28ed0 67 73 20 7c 3d 20 57 48 45 52 45 5f 49 44 58 5f  gs |= WHERE_IDX_
28ed1 4f 4e 4c 59 3b 0a 20 20 20 20 20 20 20 20 63 6f  ONLY;.        co
28ed2 73 74 20 2f 3d 20 32 3b 0a 20 20 20 20 20 20 20  st /= 2;.       
28ed3 20 57 48 45 52 45 54 52 41 43 45 28 28 22 2e 2e   WHERETRACE(("..
28ed4 2e 2e 2e 2e 20 69 64 78 2d 6f 6e 6c 79 20 72 65  .... idx-only re
28ed5 64 75 63 65 73 20 63 6f 73 74 20 74 6f 20 25 2e  duces cost to %.
28ed6 39 67 5c 6e 22 2c 20 63 6f 73 74 29 29 3b 0a 20  9g\n", cost));. 
28ed7 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20       }.    }..  
28ed8 20 20 2f 2a 20 49 66 20 74 68 69 73 20 69 6e 64    /* If this ind
28ed9 65 78 20 68 61 73 20 61 63 68 69 65 76 65 64 20  ex has achieved 
28eda 74 68 65 20 6c 6f 77 65 73 74 20 63 6f 73 74 20  the lowest cost 
28edb 73 6f 20 66 61 72 2c 20 74 68 65 6e 20 75 73 65  so far, then use
28edc 20 69 74 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20   it..    */.    
28edd 69 66 28 20 66 6c 61 67 73 20 26 26 20 63 6f 73  if( flags && cos
28ede 74 20 3c 20 6c 6f 77 65 73 74 43 6f 73 74 20 29  t < lowestCost )
28edf 7b 0a 20 20 20 20 20 20 62 65 73 74 49 64 78 20  {.      bestIdx 
28ee0 3d 20 70 50 72 6f 62 65 3b 0a 20 20 20 20 20 20  = pProbe;.      
28ee1 6c 6f 77 65 73 74 43 6f 73 74 20 3d 20 63 6f 73  lowestCost = cos
28ee2 74 3b 0a 20 20 20 20 20 20 62 65 73 74 46 6c 61  t;.      bestFla
28ee3 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20 20 20 20  gs = flags;.    
28ee4 20 20 62 65 73 74 4e 45 71 20 3d 20 6e 45 71 3b    bestNEq = nEq;
28ee5 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
28ee6 20 52 65 70 6f 72 74 20 74 68 65 20 62 65 73 74   Report the best
28ee7 20 72 65 73 75 6c 74 0a 20 20 2a 2f 0a 20 20 2a   result.  */.  *
28ee8 70 70 49 6e 64 65 78 20 3d 20 62 65 73 74 49 64  ppIndex = bestId
28ee9 78 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28  x;.  WHERETRACE(
28eea 28 22 62 65 73 74 20 69 6e 64 65 78 20 69 73 20  ("best index is 
28eeb 25 73 2c 20 63 6f 73 74 3d 25 2e 39 67 2c 20 66  %s, cost=%.9g, f
28eec 6c 61 67 73 3d 25 78 2c 20 6e 45 71 3d 25 64 5c  lags=%x, nEq=%d\
28eed 6e 22 2c 0a 20 20 20 20 20 20 20 20 62 65 73 74  n",.        best
28eee 49 64 78 20 3f 20 62 65 73 74 49 64 78 2d 3e 7a  Idx ? bestIdx->z
28eef 4e 61 6d 65 20 3a 20 22 28 6e 6f 6e 65 29 22 2c  Name : "(none)",
28ef0 20 6c 6f 77 65 73 74 43 6f 73 74 2c 20 62 65 73   lowestCost, bes
28ef1 74 46 6c 61 67 73 2c 20 62 65 73 74 4e 45 71 29  tFlags, bestNEq)
28ef2 29 3b 0a 20 20 2a 70 46 6c 61 67 73 20 3d 20 62  );.  *pFlags = b
28ef3 65 73 74 46 6c 61 67 73 20 7c 20 65 71 54 65 72  estFlags | eqTer
28ef4 6d 4d 61 73 6b 3b 0a 20 20 2a 70 6e 45 71 20 3d  mMask;.  *pnEq =
28ef5 20 62 65 73 74 4e 45 71 3b 0a 20 20 72 65 74 75   bestNEq;.  retu
28ef6 72 6e 20 6c 6f 77 65 73 74 43 6f 73 74 3b 0a 7d  rn lowestCost;.}
28ef7 0a 0a 0a 2f 2a 0a 2a 2a 20 44 69 73 61 62 6c 65  .../*.** Disable
28ef8 20 61 20 74 65 72 6d 20 69 6e 20 74 68 65 20 57   a term in the W
28ef9 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 45 78  HERE clause.  Ex
28efa 63 65 70 74 2c 20 64 6f 20 6e 6f 74 20 64 69 73  cept, do not dis
28efb 61 62 6c 65 20 74 68 65 20 74 65 72 6d 0a 2a 2a  able the term.**
28efc 20 69 66 20 69 74 20 63 6f 6e 74 72 6f 6c 73 20   if it controls 
28efd 61 20 4c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49  a LEFT OUTER JOI
28efe 4e 20 61 6e 64 20 69 74 20 64 69 64 20 6e 6f 74  N and it did not
28eff 20 6f 72 69 67 69 6e 61 74 65 20 69 6e 20 74 68   originate in th
28f00 65 20 4f 4e 0a 2a 2a 20 6f 72 20 55 53 49 4e 47  e ON.** or USING
28f01 20 63 6c 61 75 73 65 20 6f 66 20 74 68 61 74 20   clause of that 
28f02 6a 6f 69 6e 2e 0a 2a 2a 0a 2a 2a 20 43 6f 6e 73  join..**.** Cons
28f03 69 64 65 72 20 74 68 65 20 74 65 72 6d 20 74 32  ider the term t2
28f04 2e 7a 3d 27 6f 6b 27 20 69 6e 20 74 68 65 20 66  .z='ok' in the f
28f05 6f 6c 6c 6f 77 69 6e 67 20 71 75 65 72 69 65 73  ollowing queries
28f06 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 31 29 20 20 53  :.**.**   (1)  S
28f07 45 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 20  ELECT * FROM t1 
28f08 4c 45 46 54 20 4a 4f 49 4e 20 74 32 20 4f 4e 20  LEFT JOIN t2 ON 
28f09 74 31 2e 61 3d 74 32 2e 78 20 57 48 45 52 45 20  t1.a=t2.x WHERE 
28f0a 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20 20 20 28  t2.z='ok'.**   (
28f0b 32 29 20 20 53 45 4c 45 43 54 20 2a 20 46 52 4f  2)  SELECT * FRO
28f0c 4d 20 74 31 20 4c 45 46 54 20 4a 4f 49 4e 20 74  M t1 LEFT JOIN t
28f0d 32 20 4f 4e 20 74 31 2e 61 3d 74 32 2e 78 20 41  2 ON t1.a=t2.x A
28f0e 4e 44 20 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 20  ND t2.z='ok'.** 
28f0f 20 20 28 33 29 20 20 53 45 4c 45 43 54 20 2a 20    (3)  SELECT * 
28f10 46 52 4f 4d 20 74 31 2c 20 74 32 20 57 48 45 52  FROM t1, t2 WHER
28f11 45 20 74 31 2e 61 3d 74 32 2e 78 20 41 4e 44 20  E t1.a=t2.x AND 
28f12 74 32 2e 7a 3d 27 6f 6b 27 0a 2a 2a 0a 2a 2a 20  t2.z='ok'.**.** 
28f13 54 68 65 20 74 32 2e 7a 3d 27 6f 6b 27 20 69 73  The t2.z='ok' is
28f14 20 64 69 73 61 62 6c 65 64 20 69 6e 20 74 68 65   disabled in the
28f15 20 69 6e 20 28 32 29 20 62 65 63 61 75 73 65 20   in (2) because 
28f16 69 74 20 6f 72 69 67 69 6e 61 74 65 73 0a 2a 2a  it originates.**
28f17 20 69 6e 20 74 68 65 20 4f 4e 20 63 6c 61 75 73   in the ON claus
28f18 65 2e 20 20 54 68 65 20 74 65 72 6d 20 69 73 20  e.  The term is 
28f19 64 69 73 61 62 6c 65 64 20 69 6e 20 28 33 29 20  disabled in (3) 
28f1a 62 65 63 61 75 73 65 20 69 74 20 69 73 20 6e 6f  because it is no
28f1b 74 20 70 61 72 74 0a 2a 2a 20 6f 66 20 61 20 4c  t part.** of a L
28f1c 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2e 20  EFT OUTER JOIN. 
28f1d 20 49 6e 20 28 31 29 2c 20 74 68 65 20 74 65 72   In (1), the ter
28f1e 6d 20 69 73 20 6e 6f 74 20 64 69 73 61 62 6c 65  m is not disable
28f1f 64 2e 0a 2a 2a 0a 2a 2a 20 44 69 73 61 62 6c 69  d..**.** Disabli
28f20 6e 67 20 61 20 74 65 72 6d 20 63 61 75 73 65 73  ng a term causes
28f21 20 74 68 61 74 20 74 65 72 6d 20 74 6f 20 6e 6f   that term to no
28f22 74 20 62 65 20 74 65 73 74 65 64 20 69 6e 20 74  t be tested in t
28f23 68 65 20 69 6e 6e 65 72 20 6c 6f 6f 70 0a 2a 2a  he inner loop.**
28f24 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 44   of the join.  D
28f25 69 73 61 62 6c 69 6e 67 20 69 73 20 61 6e 20 6f  isabling is an o
28f26 70 74 69 6d 69 7a 61 74 69 6f 6e 2e 20 20 57 68  ptimization.  Wh
28f27 65 6e 20 74 65 72 6d 73 20 61 72 65 20 73 61 74  en terms are sat
28f28 69 73 66 69 65 64 0a 2a 2a 20 62 79 20 69 6e 64  isfied.** by ind
28f29 69 63 65 73 2c 20 77 65 20 64 69 73 61 62 6c 65  ices, we disable
28f2a 20 74 68 65 6d 20 74 6f 20 70 72 65 76 65 6e 74   them to prevent
28f2b 20 72 65 64 75 6e 64 61 6e 74 20 74 65 73 74 73   redundant tests
28f2c 20 69 6e 20 74 68 65 20 69 6e 6e 65 72 0a 2a 2a   in the inner.**
28f2d 20 6c 6f 6f 70 2e 20 20 57 65 20 77 6f 75 6c 64   loop.  We would
28f2e 20 67 65 74 20 74 68 65 20 63 6f 72 72 65 63 74   get the correct
28f2f 20 72 65 73 75 6c 74 73 20 69 66 20 6e 6f 74 68   results if noth
28f30 69 6e 67 20 77 65 72 65 20 65 76 65 72 20 64 69  ing were ever di
28f31 73 61 62 6c 65 64 2c 0a 2a 2a 20 62 75 74 20 6a  sabled,.** but j
28f32 6f 69 6e 73 20 6d 69 67 68 74 20 72 75 6e 20 61  oins might run a
28f33 20 6c 69 74 74 6c 65 20 73 6c 6f 77 65 72 2e 20   little slower. 
28f34 20 54 68 65 20 74 72 69 63 6b 20 69 73 20 74 6f   The trick is to
28f35 20 64 69 73 61 62 6c 65 20 61 73 20 6d 75 63 68   disable as much
28f36 0a 2a 2a 20 61 73 20 77 65 20 63 61 6e 20 77 69  .** as we can wi
28f37 74 68 6f 75 74 20 64 69 73 61 62 6c 69 6e 67 20  thout disabling 
28f38 74 6f 6f 20 6d 75 63 68 2e 20 20 49 66 20 77 65  too much.  If we
28f39 20 64 69 73 61 62 6c 65 64 20 69 6e 20 28 31 29   disabled in (1)
28f3a 2c 20 77 65 27 64 20 67 65 74 0a 2a 2a 20 74 68  , we'd get.** th
28f3b 65 20 77 72 6f 6e 67 20 61 6e 73 77 65 72 2e 20  e wrong answer. 
28f3c 20 53 65 65 20 74 69 63 6b 65 74 20 23 38 31 33   See ticket #813
28f3d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
28f3e 20 64 69 73 61 62 6c 65 54 65 72 6d 28 57 68 65   disableTerm(Whe
28f3f 72 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c  reLevel *pLevel,
28f40 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
28f41 6d 29 7b 0a 20 20 69 66 28 20 70 54 65 72 6d 0a  m){.  if( pTerm.
28f42 20 20 20 20 20 20 26 26 20 41 4c 57 41 59 53 28        && ALWAYS(
28f43 28 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20  (pTerm->flags & 
28f44 54 45 52 4d 5f 43 4f 44 45 44 29 3d 3d 30 29 0a  TERM_CODED)==0).
28f45 20 20 20 20 20 20 26 26 20 28 70 4c 65 76 65 6c        && (pLevel
28f46 2d 3e 69 4c 65 66 74 4a 6f 69 6e 3d 3d 30 20 7c  ->iLeftJoin==0 |
28f47 7c 20 45 78 70 72 48 61 73 50 72 6f 70 65 72 74  | ExprHasPropert
28f48 79 28 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  y(pTerm->pExpr, 
28f49 45 50 5f 46 72 6f 6d 4a 6f 69 6e 29 29 0a 20 20  EP_FromJoin)).  
28f4a 29 7b 0a 20 20 20 20 70 54 65 72 6d 2d 3e 66 6c  ){.    pTerm->fl
28f4b 61 67 73 20 7c 3d 20 54 45 52 4d 5f 43 4f 44 45  ags |= TERM_CODE
28f4c 44 3b 0a 20 20 20 20 69 66 28 20 70 54 65 72 6d  D;.    if( pTerm
28f4d 2d 3e 69 50 61 72 65 6e 74 3e 3d 30 20 29 7b 0a  ->iParent>=0 ){.
28f4e 20 20 20 20 20 20 57 68 65 72 65 54 65 72 6d 20        WhereTerm 
28f4f 2a 70 4f 74 68 65 72 20 3d 20 26 70 54 65 72 6d  *pOther = &pTerm
28f50 2d 3e 70 57 43 2d 3e 61 5b 70 54 65 72 6d 2d 3e  ->pWC->a[pTerm->
28f51 69 50 61 72 65 6e 74 5d 3b 0a 20 20 20 20 20 20  iParent];.      
28f52 69 66 28 20 28 2d 2d 70 4f 74 68 65 72 2d 3e 6e  if( (--pOther->n
28f53 43 68 69 6c 64 29 3d 3d 30 20 29 7b 0a 20 20 20  Child)==0 ){.   
28f54 20 20 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d       disableTerm
28f55 28 70 4c 65 76 65 6c 2c 20 70 4f 74 68 65 72 29  (pLevel, pOther)
28f56 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
28f57 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 70 70    }.}../*.** App
28f58 6c 79 20 74 68 65 20 61 66 66 69 6e 69 74 69 65  ly the affinitie
28f59 73 20 61 73 73 6f 63 69 61 74 65 64 20 77 69 74  s associated wit
28f5a 68 20 74 68 65 20 66 69 72 73 74 20 6e 20 63 6f  h the first n co
28f5b 6c 75 6d 6e 73 20 6f 66 20 69 6e 64 65 78 0a 2a  lumns of index.*
28f5c 2a 20 70 49 64 78 20 74 6f 20 74 68 65 20 76 61  * pIdx to the va
28f5d 6c 75 65 73 20 69 6e 20 74 68 65 20 6e 20 72 65  lues in the n re
28f5e 67 69 73 74 65 72 73 20 73 74 61 72 74 69 6e 67  gisters starting
28f5f 20 61 74 20 62 61 73 65 2e 0a 2a 2f 0a 73 74 61   at base..*/.sta
28f60 74 69 63 20 76 6f 69 64 20 63 6f 64 65 41 70 70  tic void codeApp
28f61 6c 79 41 66 66 69 6e 69 74 79 28 50 61 72 73 65  lyAffinity(Parse
28f62 20 2a 70 50 61 72 73 65 2c 20 69 6e 74 20 62 61   *pParse, int ba
28f63 73 65 2c 20 69 6e 74 20 6e 2c 20 49 6e 64 65 78  se, int n, Index
28f64 20 2a 70 49 64 78 29 7b 0a 20 20 69 66 28 20 6e   *pIdx){.  if( n
28f65 3e 30 20 29 7b 0a 20 20 20 20 56 64 62 65 20 2a  >0 ){.    Vdbe *
28f66 76 20 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62  v = pParse->pVdb
28f67 65 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 76  e;.    assert( v
28f68 21 3d 30 20 29 3b 0a 20 20 20 20 73 71 6c 69 74  !=0 );.    sqlit
28f69 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
28f6a 4f 50 5f 41 66 66 69 6e 69 74 79 2c 20 62 61 73  OP_Affinity, bas
28f6b 65 2c 20 6e 29 3b 0a 20 20 20 20 73 71 6c 69 74  e, n);.    sqlit
28f6c 65 33 49 6e 64 65 78 41 66 66 69 6e 69 74 79 53  e3IndexAffinityS
28f6d 74 72 28 76 2c 20 70 49 64 78 29 3b 0a 20 20 20  tr(v, pIdx);.   
28f6e 20 73 71 6c 69 74 65 33 45 78 70 72 43 61 63 68   sqlite3ExprCach
28f6f 65 41 66 66 69 6e 69 74 79 43 68 61 6e 67 65 28  eAffinityChange(
28f70 70 50 61 72 73 65 2c 20 62 61 73 65 2c 20 6e 29  pParse, base, n)
28f71 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  ;.  }.}.../*.** 
28f72 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 66 6f  Generate code fo
28f73 72 20 61 20 73 69 6e 67 6c 65 20 65 71 75 61 6c  r a single equal
28f74 69 74 79 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ity term of the 
28f75 57 48 45 52 45 20 63 6c 61 75 73 65 2e 20 20 41  WHERE clause.  A
28f76 6e 20 65 71 75 61 6c 69 74 79 0a 2a 2a 20 74 65  n equality.** te
28f77 72 6d 20 63 61 6e 20 62 65 20 65 69 74 68 65 72  rm can be either
28f78 20 58 3d 65 78 70 72 20 6f 72 20 58 20 49 4e 20   X=expr or X IN 
28f79 28 2e 2e 2e 29 2e 20 20 20 70 54 65 72 6d 20 69  (...).   pTerm i
28f7a 73 20 74 68 65 20 74 65 72 6d 20 74 6f 20 62 65  s the term to be
28f7b 20 0a 2a 2a 20 63 6f 64 65 64 2e 0a 2a 2a 0a 2a   .** coded..**.*
28f7c 2a 20 54 68 65 20 63 75 72 72 65 6e 74 20 76 61  * The current va
28f7d 6c 75 65 20 66 6f 72 20 74 68 65 20 63 6f 6e 73  lue for the cons
28f7e 74 72 61 69 6e 74 20 69 73 20 6c 65 66 74 20 69  traint is left i
28f7f 6e 20 72 65 67 69 73 74 65 72 20 69 52 65 67 2e  n register iReg.
28f80 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 61 20 63 6f 6e  .**.** For a con
28f81 73 74 72 61 69 6e 74 20 6f 66 20 74 68 65 20 66  straint of the f
28f82 6f 72 6d 20 58 3d 65 78 70 72 2c 20 74 68 65 20  orm X=expr, the 
28f83 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 65 76  expression is ev
28f84 61 6c 75 61 74 65 64 20 61 6e 64 20 69 74 73 0a  aluated and its.
28f85 2a 2a 20 72 65 73 75 6c 74 20 69 73 20 6c 65 66  ** result is lef
28f86 74 20 6f 6e 20 74 68 65 20 73 74 61 63 6b 2e 20  t on the stack. 
28f87 20 46 6f 72 20 63 6f 6e 73 74 72 61 69 6e 74 73   For constraints
28f88 20 6f 66 20 74 68 65 20 66 6f 72 6d 20 58 20 49   of the form X I
28f89 4e 20 28 2e 2e 2e 29 0a 2a 2a 20 74 68 69 73 20  N (...).** this 
28f8a 72 6f 75 74 69 6e 65 20 73 65 74 73 20 75 70 20  routine sets up 
28f8b 61 20 6c 6f 6f 70 20 74 68 61 74 20 77 69 6c 6c  a loop that will
28f8c 20 69 74 65 72 61 74 65 20 6f 76 65 72 20 61 6c   iterate over al
28f8d 6c 20 76 61 6c 75 65 73 20 6f 66 20 58 2e 0a 2a  l values of X..*
28f8e 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 64  /.static int cod
28f8f 65 45 71 75 61 6c 69 74 79 54 65 72 6d 28 0a 20  eEqualityTerm(. 
28f90 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 2c 20   Parse *pParse, 
28f91 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73       /* The pars
28f92 69 6e 67 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20  ing context */. 
28f93 20 57 68 65 72 65 54 65 72 6d 20 2a 70 54 65 72   WhereTerm *pTer
28f94 6d 2c 20 20 20 2f 2a 20 54 68 65 20 74 65 72 6d  m,   /* The term
28f95 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
28f96 61 75 73 65 20 74 6f 20 62 65 20 63 6f 64 65 64  ause to be coded
28f97 20 2a 2f 0a 20 20 57 68 65 72 65 4c 65 76 65 6c   */.  WhereLevel
28f98 20 2a 70 4c 65 76 65 6c 2c 20 2f 2a 20 57 68 65   *pLevel, /* Whe
28f99 6e 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 46  n level of the F
28f9a 52 4f 4d 20 63 6c 61 75 73 65 20 77 65 20 61 72  ROM clause we ar
28f9b 65 20 77 6f 72 6b 69 6e 67 20 6f 6e 20 2a 2f 0a  e working on */.
28f9c 20 20 69 6e 74 20 69 54 61 72 67 65 74 20 20 20    int iTarget   
28f9d 20 20 20 20 20 20 2f 2a 20 41 74 74 65 6d 70 74        /* Attempt
28f9e 20 74 6f 20 6c 65 61 76 65 20 72 65 73 75 6c 74   to leave result
28f9f 73 20 69 6e 20 74 68 69 73 20 72 65 67 69 73 74  s in this regist
28fa0 65 72 20 2a 2f 0a 29 7b 0a 20 20 45 78 70 72 20  er */.){.  Expr 
28fa1 2a 70 58 20 3d 20 70 54 65 72 6d 2d 3e 70 45 78  *pX = pTerm->pEx
28fa2 70 72 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  pr;.  Vdbe *v = 
28fa3 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
28fa4 20 69 6e 74 20 69 52 65 67 3b 20 20 20 20 20 20   int iReg;      
28fa5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 52              /* R
28fa6 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
28fa7 72 65 73 75 6c 74 73 20 2a 2f 0a 0a 20 20 61 73  results */..  as
28fa8 73 65 72 74 28 20 69 54 61 72 67 65 74 3e 30 20  sert( iTarget>0 
28fa9 29 3b 0a 20 20 69 66 28 20 70 58 2d 3e 6f 70 3d  );.  if( pX->op=
28faa 3d 54 4b 5f 45 51 20 29 7b 0a 20 20 20 20 69 52  =TK_EQ ){.    iR
28fab 65 67 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72  eg = sqlite3Expr
28fac 43 6f 64 65 54 61 72 67 65 74 28 70 50 61 72 73  CodeTarget(pPars
28fad 65 2c 20 70 58 2d 3e 70 52 69 67 68 74 2c 20 69  e, pX->pRight, i
28fae 54 61 72 67 65 74 29 3b 0a 20 20 7d 65 6c 73 65  Target);.  }else
28faf 20 69 66 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f   if( pX->op==TK_
28fb0 49 53 4e 55 4c 4c 20 29 7b 0a 20 20 20 20 69 52  ISNULL ){.    iR
28fb1 65 67 20 3d 20 69 54 61 72 67 65 74 3b 0a 20 20  eg = iTarget;.  
28fb2 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
28fb3 4f 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20  Op2(v, OP_Null, 
28fb4 30 2c 20 69 52 65 67 29 3b 0a 23 69 66 6e 64 65  0, iReg);.#ifnde
28fb5 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 53 55  f SQLITE_OMIT_SU
28fb6 42 51 55 45 52 59 0a 20 20 7d 65 6c 73 65 7b 0a  BQUERY.  }else{.
28fb7 20 20 20 20 69 6e 74 20 65 54 79 70 65 3b 0a 20      int eType;. 
28fb8 20 20 20 69 6e 74 20 69 54 61 62 3b 0a 20 20 20     int iTab;.   
28fb9 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f 70 20 2a   struct InLoop *
28fba 70 49 6e 3b 0a 0a 20 20 20 20 61 73 73 65 72 74  pIn;..    assert
28fbb 28 20 70 58 2d 3e 6f 70 3d 3d 54 4b 5f 49 4e 20  ( pX->op==TK_IN 
28fbc 29 3b 0a 20 20 20 20 69 52 65 67 20 3d 20 69 54  );.    iReg = iT
28fbd 61 72 67 65 74 3b 0a 20 20 20 20 65 54 79 70 65  arget;.    eType
28fbe 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 49 6e   = sqlite3FindIn
28fbf 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20 70 58  Index(pParse, pX
28fc0 2c 20 30 29 3b 0a 20 20 20 20 69 54 61 62 20 3d  , 0);.    iTab =
28fc1 20 70 58 2d 3e 69 54 61 62 6c 65 3b 0a 20 20 20   pX->iTable;.   
28fc2 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
28fc3 70 32 28 76 2c 20 4f 50 5f 52 65 77 69 6e 64 2c  p2(v, OP_Rewind,
28fc4 20 69 54 61 62 2c 20 30 29 3b 0a 20 20 20 20 56   iTab, 0);.    V
28fc5 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22  dbeComment((v, "
28fc6 25 2e 2a 73 22 2c 20 70 58 2d 3e 73 70 61 6e 2e  %.*s", pX->span.
28fc7 6e 2c 20 70 58 2d 3e 73 70 61 6e 2e 7a 29 29 3b  n, pX->span.z));
28fc8 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d  .    if( pLevel-
28fc9 3e 6e 49 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 20  >nIn==0 ){.     
28fca 20 70 4c 65 76 65 6c 2d 3e 6e 78 74 20 3d 20 73   pLevel->nxt = s
28fcb 71 6c 69 74 65 33 56 64 62 65 4d 61 6b 65 4c 61  qlite3VdbeMakeLa
28fcc 62 65 6c 28 76 29 3b 0a 20 20 20 20 7d 0a 20 20  bel(v);.    }.  
28fcd 20 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 2b 2b 3b    pLevel->nIn++;
28fce 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 61 49 6e  .    pLevel->aIn
28fcf 4c 6f 6f 70 20 3d 20 73 71 6c 69 74 65 33 44 62  Loop = sqlite3Db
28fd0 52 65 61 6c 6c 6f 63 4f 72 46 72 65 65 28 70 50  ReallocOrFree(pP
28fd1 61 72 73 65 2d 3e 64 62 2c 20 70 4c 65 76 65 6c  arse->db, pLevel
28fd2 2d 3e 61 49 6e 4c 6f 6f 70 2c 0a 20 20 20 20 20  ->aInLoop,.     
28fd3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
28fd4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
28fd5 69 7a 65 6f 66 28 70 4c 65 76 65 6c 2d 3e 61 49  izeof(pLevel->aI
28fd6 6e 4c 6f 6f 70 5b 30 5d 29 2a 70 4c 65 76 65 6c  nLoop[0])*pLevel
28fd7 2d 3e 6e 49 6e 29 3b 0a 20 20 20 20 70 49 6e 20  ->nIn);.    pIn 
28fd8 3d 20 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f  = pLevel->aInLoo
28fd9 70 3b 0a 20 20 20 20 69 66 28 20 70 49 6e 20 29  p;.    if( pIn )
28fda 7b 0a 20 20 20 20 20 20 70 49 6e 20 2b 3d 20 70  {.      pIn += p
28fdb 4c 65 76 65 6c 2d 3e 6e 49 6e 20 2d 20 31 3b 0a  Level->nIn - 1;.
28fdc 20 20 20 20 20 20 70 49 6e 2d 3e 69 43 75 72 20        pIn->iCur 
28fdd 3d 20 69 54 61 62 3b 0a 20 20 20 20 20 20 69 66  = iTab;.      if
28fde 28 20 65 54 79 70 65 3d 3d 49 4e 5f 49 4e 44 45  ( eType==IN_INDE
28fdf 58 5f 52 4f 57 49 44 20 29 7b 0a 20 20 20 20 20  X_ROWID ){.     
28fe0 20 20 20 70 49 6e 2d 3e 74 6f 70 41 64 64 72 20     pIn->topAddr 
28fe1 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
28fe2 4f 70 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c  Op2(v, OP_Rowid,
28fe3 20 69 54 61 62 2c 20 69 52 65 67 29 3b 0a 20 20   iTab, iReg);.  
28fe4 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
28fe5 20 20 20 70 49 6e 2d 3e 74 6f 70 41 64 64 72 20     pIn->topAddr 
28fe6 3d 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64  = sqlite3VdbeAdd
28fe7 4f 70 33 28 76 2c 20 4f 50 5f 43 6f 6c 75 6d 6e  Op3(v, OP_Column
28fe8 2c 20 69 54 61 62 2c 20 30 2c 20 69 52 65 67 29  , iTab, 0, iReg)
28fe9 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
28fea 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
28feb 31 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20  1(v, OP_IsNull, 
28fec 69 52 65 67 29 3b 0a 20 20 20 20 7d 65 6c 73 65  iReg);.    }else
28fed 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e  {.      pLevel->
28fee 6e 49 6e 20 3d 20 30 3b 0a 20 20 20 20 7d 0a 23  nIn = 0;.    }.#
28fef 65 6e 64 69 66 0a 20 20 7d 0a 20 20 64 69 73 61  endif.  }.  disa
28ff0 62 6c 65 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20  bleTerm(pLevel, 
28ff1 70 54 65 72 6d 29 3b 0a 20 20 72 65 74 75 72 6e  pTerm);.  return
28ff2 20 69 52 65 67 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20   iReg;.}../*.** 
28ff3 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  Generate code th
28ff4 61 74 20 77 69 6c 6c 20 65 76 61 6c 75 61 74 65  at will evaluate
28ff5 20 61 6c 6c 20 3d 3d 20 61 6e 64 20 49 4e 20 63   all == and IN c
28ff6 6f 6e 73 74 72 61 69 6e 74 73 20 66 6f 72 20 61  onstraints for a
28ff7 6e 0a 2a 2a 20 69 6e 64 65 78 2e 20 20 54 68 65  n.** index.  The
28ff8 20 76 61 6c 75 65 73 20 66 6f 72 20 61 6c 6c 20   values for all 
28ff9 63 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20  constraints are 
28ffa 6c 65 66 74 20 6f 6e 20 74 68 65 20 73 74 61 63  left on the stac
28ffb 6b 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 65 78 61  k..**.** For exa
28ffc 6d 70 6c 65 2c 20 63 6f 6e 73 69 64 65 72 20 74  mple, consider t
28ffd 61 62 6c 65 20 74 31 28 61 2c 62 2c 63 2c 64 2c  able t1(a,b,c,d,
28ffe 65 2c 66 29 20 77 69 74 68 20 69 6e 64 65 78 20  e,f) with index 
28fff 69 31 28 61 2c 62 2c 63 29 2e 0a 2a 2a 20 53 75  i1(a,b,c)..** Su
29000 70 70 6f 73 65 20 74 68 65 20 57 48 45 52 45 20  ppose the WHERE 
29001 63 6c 61 75 73 65 20 69 73 20 74 68 69 73 3a 20  clause is this: 
29002 20 61 3d 3d 35 20 41 4e 44 20 62 20 49 4e 20 28   a==5 AND b IN (
29003 31 2c 32 2c 33 29 20 41 4e 44 20 63 3e 35 20 41  1,2,3) AND c>5 A
29004 4e 44 20 63 3c 31 30 0a 2a 2a 20 54 68 65 20 69  ND c<10.** The i
29005 6e 64 65 78 20 68 61 73 20 61 73 20 6d 61 6e 79  ndex has as many
29006 20 61 73 20 74 68 72 65 65 20 65 71 75 61 6c 69   as three equali
29007 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20  ty constraints, 
29008 62 75 74 20 69 6e 20 74 68 69 73 0a 2a 2a 20 65  but in this.** e
29009 78 61 6d 70 6c 65 2c 20 74 68 65 20 74 68 69 72  xample, the thir
2900a 64 20 22 63 22 20 76 61 6c 75 65 20 69 73 20 61  d "c" value is a
2900b 6e 20 69 6e 65 71 75 61 6c 69 74 79 2e 20 20 53  n inequality.  S
2900c 6f 20 6f 6e 6c 79 20 74 77 6f 20 0a 2a 2a 20 63  o only two .** c
2900d 6f 6e 73 74 72 61 69 6e 74 73 20 61 72 65 20 63  onstraints are c
2900e 6f 64 65 64 2e 20 20 54 68 69 73 20 72 6f 75 74  oded.  This rout
2900f 69 6e 65 20 77 69 6c 6c 20 67 65 6e 65 72 61 74  ine will generat
29010 65 20 63 6f 64 65 20 74 6f 20 65 76 61 6c 75 61  e code to evalua
29011 74 65 0a 2a 2a 20 61 3d 3d 35 20 61 6e 64 20 62  te.** a==5 and b
29012 20 49 4e 20 28 31 2c 32 2c 33 29 2e 20 20 54 68   IN (1,2,3).  Th
29013 65 20 63 75 72 72 65 6e 74 20 76 61 6c 75 65 73  e current values
29014 20 66 6f 72 20 61 20 61 6e 64 20 62 20 77 69 6c   for a and b wil
29015 6c 20 62 65 20 6c 65 66 74 0a 2a 2a 20 6f 6e 20  l be left.** on 
29016 74 68 65 20 73 74 61 63 6b 20 2d 20 61 20 69 73  the stack - a is
29017 20 74 68 65 20 64 65 65 70 65 73 74 20 61 6e 64   the deepest and
29018 20 62 20 74 68 65 20 73 68 61 6c 6c 6f 77 65 73   b the shallowes
29019 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20 74 68 65 20  t..**.** In the 
2901a 65 78 61 6d 70 6c 65 20 61 62 6f 76 65 20 6e 45  example above nE
2901b 71 3d 3d 32 2e 20 20 42 75 74 20 74 68 69 73 20  q==2.  But this 
2901c 73 75 62 72 6f 75 74 69 6e 65 20 77 6f 72 6b 73  subroutine works
2901d 20 66 6f 72 20 61 6e 79 20 76 61 6c 75 65 0a 2a   for any value.*
2901e 2a 20 6f 66 20 6e 45 71 20 69 6e 63 6c 75 64 69  * of nEq includi
2901f 6e 67 20 30 2e 20 20 49 66 20 6e 45 71 3d 3d 30  ng 0.  If nEq==0
29020 2c 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 69  , this routine i
29021 73 20 6e 65 61 72 6c 79 20 61 20 6e 6f 2d 6f 70  s nearly a no-op
29022 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 74 68  ..** The only th
29023 69 6e 67 20 69 74 20 64 6f 65 73 20 69 73 20 61  ing it does is a
29024 6c 6c 6f 63 61 74 65 20 74 68 65 20 70 4c 65 76  llocate the pLev
29025 65 6c 2d 3e 69 4d 65 6d 20 6d 65 6d 6f 72 79 20  el->iMem memory 
29026 63 65 6c 6c 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  cell..**.** This
29027 20 72 6f 75 74 69 6e 65 20 61 6c 77 61 79 73 20   routine always 
29028 61 6c 6c 6f 63 61 74 65 73 20 61 74 20 6c 65 61  allocates at lea
29029 73 74 20 6f 6e 65 20 6d 65 6d 6f 72 79 20 63 65  st one memory ce
2902a 6c 6c 20 61 6e 64 20 70 75 74 73 0a 2a 2a 20 74  ll and puts.** t
2902b 68 65 20 61 64 64 72 65 73 73 20 6f 66 20 74 68  he address of th
2902c 61 74 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 20 69  at memory cell i
2902d 6e 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 2e 20  n pLevel->iMem. 
2902e 20 54 68 65 20 63 6f 64 65 20 74 68 61 74 0a 2a   The code that.*
2902f 2a 20 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75  * calls this rou
29030 74 69 6e 65 20 77 69 6c 6c 20 75 73 65 20 70 4c  tine will use pL
29031 65 76 65 6c 2d 3e 69 4d 65 6d 20 74 6f 20 73 74  evel->iMem to st
29032 6f 72 65 20 74 68 65 20 74 65 72 6d 69 6e 61 74  ore the terminat
29033 69 6f 6e 0a 2a 2a 20 6b 65 79 20 76 61 6c 75 65  ion.** key value
29034 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 20 20 49   of the loop.  I
29035 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 49 4e  f one or more IN
29036 20 6f 70 65 72 61 74 6f 72 73 20 61 70 70 65 61   operators appea
29037 72 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 69 73 20  r, then.** this 
29038 72 6f 75 74 69 6e 65 20 61 6c 6c 6f 63 61 74 65  routine allocate
29039 73 20 61 6e 20 61 64 64 69 74 69 6f 6e 61 6c 20  s an additional 
2903a 6e 45 71 20 6d 65 6d 6f 72 79 20 63 65 6c 6c 73  nEq memory cells
2903b 20 66 6f 72 20 69 6e 74 65 72 6e 61 6c 0a 2a 2a   for internal.**
2903c 20 75 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   use..*/.static 
2903d 69 6e 74 20 63 6f 64 65 41 6c 6c 45 71 75 61 6c  int codeAllEqual
2903e 69 74 79 54 65 72 6d 73 28 0a 20 20 50 61 72 73  ityTerms(.  Pars
2903f 65 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20  e *pParse,      
29040 20 20 2f 2a 20 50 61 72 73 69 6e 67 20 63 6f 6e    /* Parsing con
29041 74 65 78 74 20 2a 2f 0a 20 20 57 68 65 72 65 4c  text */.  WhereL
29042 65 76 65 6c 20 2a 70 4c 65 76 65 6c 2c 20 20 20  evel *pLevel,   
29043 2f 2a 20 57 68 69 63 68 20 6e 65 73 74 65 64 20  /* Which nested 
29044 6c 6f 6f 70 20 6f 66 20 74 68 65 20 46 52 4f 4d  loop of the FROM
29045 20 77 65 20 61 72 65 20 63 6f 64 69 6e 67 20 2a   we are coding *
29046 2f 0a 20 20 57 68 65 72 65 43 6c 61 75 73 65 20  /.  WhereClause 
29047 2a 70 57 43 2c 20 20 20 20 20 2f 2a 20 54 68 65  *pWC,     /* The
29048 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 2a 2f   WHERE clause */
29049 0a 20 20 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65  .  Bitmask notRe
2904a 61 64 79 2c 20 20 20 20 20 2f 2a 20 57 68 69 63  ady,     /* Whic
2904b 68 20 70 61 72 74 73 20 6f 66 20 46 52 4f 4d 20  h parts of FROM 
2904c 68 61 76 65 20 6e 6f 74 20 79 65 74 20 62 65 65  have not yet bee
2904d 6e 20 63 6f 64 65 64 20 2a 2f 0a 20 20 69 6e 74  n coded */.  int
2904e 20 6e 45 78 74 72 61 52 65 67 20 20 20 20 20 20   nExtraReg      
2904f 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20     /* Number of 
29050 65 78 74 72 61 20 72 65 67 69 73 74 65 72 73 20  extra registers 
29051 74 6f 20 61 6c 6c 6f 63 61 74 65 20 2a 2f 0a 29  to allocate */.)
29052 7b 0a 20 20 69 6e 74 20 6e 45 71 20 3d 20 70 4c  {.  int nEq = pL
29053 65 76 65 6c 2d 3e 6e 45 71 3b 20 20 20 20 20 20  evel->nEq;      
29054 20 20 2f 2a 20 54 68 65 20 6e 75 6d 62 65 72 20    /* The number 
29055 6f 66 20 3d 3d 20 6f 72 20 49 4e 20 63 6f 6e 73  of == or IN cons
29056 74 72 61 69 6e 74 73 20 74 6f 20 63 6f 64 65 20  traints to code 
29057 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20 3d 20 70  */.  Vdbe *v = p
29058 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 20 20 20  Parse->pVdbe;   
29059 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
2905a 6c 20 6d 61 63 68 69 6e 65 20 75 6e 64 65 72 20  l machine under 
2905b 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 2a 2f 0a  construction */.
2905c 20 20 49 6e 64 65 78 20 2a 70 49 64 78 20 3d 20    Index *pIdx = 
2905d 70 4c 65 76 65 6c 2d 3e 70 49 64 78 3b 20 20 20  pLevel->pIdx;   
2905e 2f 2a 20 54 68 65 20 69 6e 64 65 78 20 62 65 69  /* The index bei
2905f 6e 67 20 75 73 65 64 20 66 6f 72 20 74 68 69 73  ng used for this
29060 20 6c 6f 6f 70 20 2a 2f 0a 20 20 69 6e 74 20 69   loop */.  int i
29061 43 75 72 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 54  Cur = pLevel->iT
29062 61 62 43 75 72 3b 20 20 20 2f 2a 20 54 68 65 20  abCur;   /* The 
29063 63 75 72 73 6f 72 20 6f 66 20 74 68 65 20 74 61  cursor of the ta
29064 62 6c 65 20 2a 2f 0a 20 20 57 68 65 72 65 54 65  ble */.  WhereTe
29065 72 6d 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20  rm *pTerm;      
29066 20 20 20 20 20 20 20 2f 2a 20 41 20 73 69 6e 67         /* A sing
29067 6c 65 20 63 6f 6e 73 74 72 61 69 6e 74 20 74 65  le constraint te
29068 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 6a 3b 20 20  rm */.  int j;  
29069 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2906a 20 20 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f        /* Loop co
2906b 75 6e 74 65 72 20 2a 2f 0a 20 20 69 6e 74 20 72  unter */.  int r
2906c 65 67 42 61 73 65 3b 20 20 20 20 20 20 20 20 20  egBase;         
2906d 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65           /* Base
2906e 20 72 65 67 69 73 74 65 72 20 2a 2f 0a 0a 20 20   register */..  
2906f 2f 2a 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f  /* Figure out ho
29070 77 20 6d 61 6e 79 20 6d 65 6d 6f 72 79 20 63 65  w many memory ce
29071 6c 6c 73 20 77 65 20 77 69 6c 6c 20 6e 65 65 64  lls we will need
29072 20 74 68 65 6e 20 61 6c 6c 6f 63 61 74 65 20 74   then allocate t
29073 68 65 6d 2e 0a 20 20 2a 2a 20 57 65 20 61 6c 77  hem..  ** We alw
29074 61 79 73 20 6e 65 65 64 20 61 74 20 6c 65 61 73  ays need at leas
29075 74 20 6f 6e 65 20 75 73 65 64 20 74 6f 20 73 74  t one used to st
29076 6f 72 65 20 74 68 65 20 6c 6f 6f 70 20 74 65 72  ore the loop ter
29077 6d 69 6e 61 74 6f 72 0a 20 20 2a 2a 20 76 61 6c  minator.  ** val
29078 75 65 2e 20 20 49 66 20 74 68 65 72 65 20 61 72  ue.  If there ar
29079 65 20 49 4e 20 6f 70 65 72 61 74 6f 72 73 20 77  e IN operators w
2907a 65 27 6c 6c 20 6e 65 65 64 20 6f 6e 65 20 66 6f  e'll need one fo
2907b 72 20 65 61 63 68 20 3d 3d 20 6f 72 0a 20 20 2a  r each == or.  *
2907c 2a 20 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 2e  * IN constraint.
2907d 0a 20 20 2a 2f 0a 20 20 70 4c 65 76 65 6c 2d 3e  .  */.  pLevel->
2907e 69 4d 65 6d 20 3d 20 70 50 61 72 73 65 2d 3e 6e  iMem = pParse->n
2907f 4d 65 6d 20 2b 20 31 3b 0a 20 20 72 65 67 42 61  Mem + 1;.  regBa
29080 73 65 20 3d 20 70 50 61 72 73 65 2d 3e 6e 4d 65  se = pParse->nMe
29081 6d 20 2b 20 32 3b 0a 20 20 70 50 61 72 73 65 2d  m + 2;.  pParse-
29082 3e 6e 4d 65 6d 20 2b 3d 20 70 4c 65 76 65 6c 2d  >nMem += pLevel-
29083 3e 6e 45 71 20 2b 20 32 20 2b 20 6e 45 78 74 72  >nEq + 2 + nExtr
29084 61 52 65 67 3b 0a 0a 20 20 2f 2a 20 45 76 61 6c  aReg;..  /* Eval
29085 75 61 74 65 20 74 68 65 20 65 71 75 61 6c 69 74  uate the equalit
29086 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20 20  y constraints.  
29087 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 49 64  */.  assert( pId
29088 78 2d 3e 6e 43 6f 6c 75 6d 6e 3e 3d 6e 45 71 20  x->nColumn>=nEq 
29089 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c  );.  for(j=0; j<
2908a 6e 45 71 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 69  nEq; j++){.    i
2908b 6e 74 20 72 31 3b 0a 20 20 20 20 69 6e 74 20 6b  nt r1;.    int k
2908c 20 3d 20 70 49 64 78 2d 3e 61 69 43 6f 6c 75 6d   = pIdx->aiColum
2908d 6e 5b 6a 5d 3b 0a 20 20 20 20 70 54 65 72 6d 20  n[j];.    pTerm 
2908e 3d 20 66 69 6e 64 54 65 72 6d 28 70 57 43 2c 20  = findTerm(pWC, 
2908f 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65 61 64  iCur, k, notRead
29090 79 2c 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  y, pLevel->flags
29091 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 69 66 28  , pIdx);.    if(
29092 20 4e 45 56 45 52 28 70 54 65 72 6d 3d 3d 30 29   NEVER(pTerm==0)
29093 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 61 73   ) break;.    as
29094 73 65 72 74 28 20 28 70 54 65 72 6d 2d 3e 66 6c  sert( (pTerm->fl
29095 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45 44  ags & TERM_CODED
29096 29 3d 3d 30 20 29 3b 0a 20 20 20 20 72 31 20 3d  )==0 );.    r1 =
29097 20 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72   codeEqualityTer
29098 6d 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c  m(pParse, pTerm,
29099 20 70 4c 65 76 65 6c 2c 20 72 65 67 42 61 73 65   pLevel, regBase
2909a 2b 6a 29 3b 0a 20 20 20 20 69 66 28 20 72 31 21  +j);.    if( r1!
2909b 3d 72 65 67 42 61 73 65 2b 6a 20 29 7b 0a 20 20  =regBase+j ){.  
2909c 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
2909d 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f 70  ddOp2(v, OP_SCop
2909e 79 2c 20 72 31 2c 20 72 65 67 42 61 73 65 2b 6a  y, r1, regBase+j
2909f 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 74 65 73  );.    }.    tes
290a0 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f  tcase( pTerm->eO
290a1 70 65 72 61 74 6f 72 20 26 20 57 4f 5f 49 53 4e  perator & WO_ISN
290a2 55 4c 4c 20 29 3b 0a 20 20 20 20 74 65 73 74 63  ULL );.    testc
290a3 61 73 65 28 20 70 54 65 72 6d 2d 3e 65 4f 70 65  ase( pTerm->eOpe
290a4 72 61 74 6f 72 20 26 20 57 4f 5f 49 4e 20 29 3b  rator & WO_IN );
290a5 0a 20 20 20 20 69 66 28 20 28 70 54 65 72 6d 2d  .    if( (pTerm-
290a6 3e 65 4f 70 65 72 61 74 6f 72 20 26 20 28 57 4f  >eOperator & (WO
290a7 5f 49 53 4e 55 4c 4c 7c 57 4f 5f 49 4e 29 29 3d  _ISNULL|WO_IN))=
290a8 3d 30 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  =0 ){.      sqli
290a9 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
290aa 20 4f 50 5f 49 73 4e 75 6c 6c 2c 20 72 65 67 42   OP_IsNull, regB
290ab 61 73 65 2b 6a 2c 20 70 4c 65 76 65 6c 2d 3e 62  ase+j, pLevel->b
290ac 72 6b 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  rk);.    }.  }. 
290ad 20 72 65 74 75 72 6e 20 72 65 67 42 61 73 65 3b   return regBase;
290ae 0a 7d 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28  .}..#if defined(
290af 53 51 4c 49 54 45 5f 54 45 53 54 29 0a 2f 2a 0a  SQLITE_TEST)./*.
290b0 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67  ** The following
290b1 20 76 61 72 69 61 62 6c 65 20 68 6f 6c 64 73 20   variable holds 
290b2 61 20 74 65 78 74 20 64 65 73 63 72 69 70 74 69  a text descripti
290b3 6f 6e 20 6f 66 20 71 75 65 72 79 20 70 6c 61 6e  on of query plan
290b4 20 67 65 6e 65 72 61 74 65 64 0a 2a 2a 20 62 79   generated.** by
290b5 20 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74   the most recent
290b6 20 63 61 6c 6c 20 74 6f 20 73 71 6c 69 74 65 33   call to sqlite3
290b7 57 68 65 72 65 42 65 67 69 6e 28 29 2e 20 20 45  WhereBegin().  E
290b8 61 63 68 20 63 61 6c 6c 20 74 6f 20 57 68 65 72  ach call to Wher
290b9 65 42 65 67 69 6e 0a 2a 2a 20 6f 76 65 72 77 72  eBegin.** overwr
290ba 69 74 65 73 20 74 68 65 20 70 72 65 76 69 6f 75  ites the previou
290bb 73 2e 20 20 54 68 69 73 20 69 6e 66 6f 72 6d 61  s.  This informa
290bc 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66 6f 72  tion is used for
290bd 20 74 65 73 74 69 6e 67 20 61 6e 64 0a 2a 2a 20   testing and.** 
290be 61 6e 61 6c 79 73 69 73 20 6f 6e 6c 79 2e 0a 2a  analysis only..*
290bf 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 63 68 61  /.SQLITE_API cha
290c0 72 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  r sqlite3_query_
290c1 70 6c 61 6e 5b 42 4d 53 2a 32 2a 34 30 5d 3b 20  plan[BMS*2*40]; 
290c2 20 2f 2a 20 54 65 78 74 20 6f 66 20 74 68 65 20   /* Text of the 
290c3 6a 6f 69 6e 20 2a 2f 0a 73 74 61 74 69 63 20 69  join */.static i
290c4 6e 74 20 6e 51 50 6c 61 6e 20 3d 20 30 3b 20 20  nt nQPlan = 0;  
290c5 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
290c6 65 78 74 20 66 72 65 65 20 73 6c 6f 77 20 69 6e  ext free slow in
290c7 20 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 5d 20 2a   _query_plan[] *
290c8 2f 0a 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c  /..#endif /* SQL
290c9 49 54 45 5f 54 45 53 54 20 2a 2f 0a 0a 0a 2f 2a  ITE_TEST */.../*
290ca 0a 2a 2a 20 46 72 65 65 20 61 20 57 68 65 72 65  .** Free a Where
290cb 49 6e 66 6f 20 73 74 72 75 63 74 75 72 65 0a 2a  Info structure.*
290cc 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 77 68  /.static void wh
290cd 65 72 65 49 6e 66 6f 46 72 65 65 28 73 71 6c 69  ereInfoFree(sqli
290ce 74 65 33 20 2a 64 62 2c 20 57 68 65 72 65 49 6e  te3 *db, WhereIn
290cf 66 6f 20 2a 70 57 49 6e 66 6f 29 7b 0a 20 20 69  fo *pWInfo){.  i
290d0 66 28 20 70 57 49 6e 66 6f 20 29 7b 0a 20 20 20  f( pWInfo ){.   
290d1 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28   int i;.    for(
290d2 69 3d 30 3b 20 69 3c 70 57 49 6e 66 6f 2d 3e 6e  i=0; i<pWInfo->n
290d3 4c 65 76 65 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  Level; i++){.   
290d4 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78     sqlite3_index
290d5 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 20 3d 20 70  _info *pInfo = p
290d6 57 49 6e 66 6f 2d 3e 61 5b 69 5d 2e 70 49 64 78  WInfo->a[i].pIdx
290d7 49 6e 66 6f 3b 0a 20 20 20 20 20 20 69 66 28 20  Info;.      if( 
290d8 70 49 6e 66 6f 20 29 7b 0a 20 20 20 20 20 20 20  pInfo ){.       
290d9 20 61 73 73 65 72 74 28 20 70 49 6e 66 6f 2d 3e   assert( pInfo->
290da 6e 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72  needToFreeIdxStr
290db 3d 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 73  ==0 );.        s
290dc 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
290dd 20 70 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 7d   pInfo);.      }
290de 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
290df 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 57 49  e3DbFree(db, pWI
290e0 6e 66 6f 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  nfo);.  }.}.../*
290e1 0a 2a 2a 20 47 65 6e 65 72 61 74 65 20 74 68 65  .** Generate the
290e2 20 62 65 67 69 6e 6e 69 6e 67 20 6f 66 20 74 68   beginning of th
290e3 65 20 6c 6f 6f 70 20 75 73 65 64 20 66 6f 72 20  e loop used for 
290e4 57 48 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f  WHERE clause pro
290e5 63 65 73 73 69 6e 67 2e 0a 2a 2a 20 54 68 65 20  cessing..** The 
290e6 72 65 74 75 72 6e 20 76 61 6c 75 65 20 69 73 20  return value is 
290e7 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61 6e 20  a pointer to an 
290e8 6f 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65  opaque structure
290e9 20 74 68 61 74 20 63 6f 6e 74 61 69 6e 73 0a 2a   that contains.*
290ea 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 6e 65  * information ne
290eb 65 64 65 64 20 74 6f 20 74 65 72 6d 69 6e 61 74  eded to terminat
290ec 65 20 74 68 65 20 6c 6f 6f 70 2e 20 20 4c 61 74  e the loop.  Lat
290ed 65 72 2c 20 74 68 65 20 63 61 6c 6c 69 6e 67 20  er, the calling 
290ee 72 6f 75 74 69 6e 65 0a 2a 2a 20 73 68 6f 75 6c  routine.** shoul
290ef 64 20 69 6e 76 6f 6b 65 20 73 71 6c 69 74 65 33  d invoke sqlite3
290f0 57 68 65 72 65 45 6e 64 28 29 20 77 69 74 68 20  WhereEnd() with 
290f1 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75 65  the return value
290f2 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69 6f   of this functio
290f3 6e 0a 2a 2a 20 69 6e 20 6f 72 64 65 72 20 74 6f  n.** in order to
290f4 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 57 48   complete the WH
290f5 45 52 45 20 63 6c 61 75 73 65 20 70 72 6f 63 65  ERE clause proce
290f6 73 73 69 6e 67 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  ssing..**.** If 
290f7 61 6e 20 65 72 72 6f 72 20 6f 63 63 75 72 73 2c  an error occurs,
290f8 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 65   this routine re
290f9 74 75 72 6e 73 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a  turns NULL..**.*
290fa 2a 20 54 68 65 20 62 61 73 69 63 20 69 64 65 61  * The basic idea
290fb 20 69 73 20 74 6f 20 64 6f 20 61 20 6e 65 73 74   is to do a nest
290fc 65 64 20 6c 6f 6f 70 2c 20 6f 6e 65 20 6c 6f 6f  ed loop, one loo
290fd 70 20 66 6f 72 20 65 61 63 68 20 74 61 62 6c 65  p for each table
290fe 20 69 6e 0a 2a 2a 20 74 68 65 20 46 52 4f 4d 20   in.** the FROM 
290ff 63 6c 61 75 73 65 20 6f 66 20 61 20 73 65 6c 65  clause of a sele
29100 63 74 2e 20 20 28 49 4e 53 45 52 54 20 61 6e 64  ct.  (INSERT and
29101 20 55 50 44 41 54 45 20 73 74 61 74 65 6d 65 6e   UPDATE statemen
29102 74 73 20 61 72 65 20 74 68 65 0a 2a 2a 20 73 61  ts are the.** sa
29103 6d 65 20 61 73 20 61 20 53 45 4c 45 43 54 20 77  me as a SELECT w
29104 69 74 68 20 6f 6e 6c 79 20 61 20 73 69 6e 67 6c  ith only a singl
29105 65 20 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46  e table in the F
29106 52 4f 4d 20 63 6c 61 75 73 65 2e 29 20 20 46 6f  ROM clause.)  Fo
29107 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 69 66  r.** example, if
29108 20 74 68 65 20 53 51 4c 20 69 73 20 74 68 69 73   the SQL is this
29109 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20 53 45  :.**.**       SE
2910a 4c 45 43 54 20 2a 20 46 52 4f 4d 20 74 31 2c 20  LECT * FROM t1, 
2910b 74 32 2c 20 74 33 20 57 48 45 52 45 20 2e 2e 2e  t2, t3 WHERE ...
2910c 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74 68 65  ;.**.** Then the
2910d 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64 20   code generated 
2910e 69 73 20 63 6f 6e 63 65 70 74 75 61 6c 6c 79 20  is conceptually 
2910f 6c 69 6b 65 20 74 68 65 20 66 6f 6c 6c 6f 77 69  like the followi
29110 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 66  ng:.**.**      f
29111 6f 72 65 61 63 68 20 72 6f 77 31 20 69 6e 20 74  oreach row1 in t
29112 31 20 64 6f 20 20 20 20 20 20 20 5c 20 20 20 20  1 do       \    
29113 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a  Code generated.*
29114 2a 20 20 20 20 20 20 20 20 66 6f 72 65 61 63 68  *        foreach
29115 20 72 6f 77 32 20 69 6e 20 74 32 20 64 6f 20 20   row2 in t2 do  
29116 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74      |-- by sqlit
29117 65 33 57 68 65 72 65 42 65 67 69 6e 28 29 0a 2a  e3WhereBegin().*
29118 2a 20 20 20 20 20 20 20 20 20 20 66 6f 72 65 61  *          forea
29119 63 68 20 72 6f 77 33 20 69 6e 20 74 33 20 64 6f  ch row3 in t3 do
2911a 20 20 20 2f 0a 2a 2a 20 20 20 20 20 20 20 20 20     /.**         
2911b 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20 20 20 20     ....**       
2911c 20 20 20 65 6e 64 20 20 20 20 20 20 20 20 20 20     end          
2911d 20 20 20 20 20 20 20 20 20 20 20 5c 20 20 20 20             \    
2911e 43 6f 64 65 20 67 65 6e 65 72 61 74 65 64 0a 2a  Code generated.*
2911f 2a 20 20 20 20 20 20 20 20 65 6e 64 20 20 20 20  *        end    
29120 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29121 20 20 20 20 7c 2d 2d 20 62 79 20 73 71 6c 69 74      |-- by sqlit
29122 65 33 57 68 65 72 65 45 6e 64 28 29 0a 2a 2a 20  e3WhereEnd().** 
29123 20 20 20 20 20 65 6e 64 20 20 20 20 20 20 20 20       end        
29124 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29125 20 2f 0a 2a 2a 0a 2a 2a 20 4e 6f 74 65 20 74 68   /.**.** Note th
29126 61 74 20 74 68 65 20 6c 6f 6f 70 73 20 6d 69 67  at the loops mig
29127 68 74 20 6e 6f 74 20 62 65 20 6e 65 73 74 65 64  ht not be nested
29128 20 69 6e 20 74 68 65 20 6f 72 64 65 72 20 69 6e   in the order in
29129 20 77 68 69 63 68 20 74 68 65 79 0a 2a 2a 20 61   which they.** a
2912a 70 70 65 61 72 20 69 6e 20 74 68 65 20 46 52 4f  ppear in the FRO
2912b 4d 20 63 6c 61 75 73 65 20 69 66 20 61 20 64 69  M clause if a di
2912c 66 66 65 72 65 6e 74 20 6f 72 64 65 72 20 69 73  fferent order is
2912d 20 62 65 74 74 65 72 20 61 62 6c 65 20 74 6f 20   better able to 
2912e 6d 61 6b 65 0a 2a 2a 20 75 73 65 20 6f 66 20 69  make.** use of i
2912f 6e 64 69 63 65 73 2e 20 20 4e 6f 74 65 20 61 6c  ndices.  Note al
29130 73 6f 20 74 68 61 74 20 77 68 65 6e 20 74 68 65  so that when the
29131 20 49 4e 20 6f 70 65 72 61 74 6f 72 20 61 70 70   IN operator app
29132 65 61 72 73 20 69 6e 0a 2a 2a 20 74 68 65 20 57  ears in.** the W
29133 48 45 52 45 20 63 6c 61 75 73 65 2c 20 69 74 20  HERE clause, it 
29134 6d 69 67 68 74 20 72 65 73 75 6c 74 20 69 6e 20  might result in 
29135 61 64 64 69 74 69 6f 6e 61 6c 20 6e 65 73 74 65  additional neste
29136 64 20 6c 6f 6f 70 73 20 66 6f 72 0a 2a 2a 20 73  d loops for.** s
29137 63 61 6e 6e 69 6e 67 20 74 68 72 6f 75 67 68 20  canning through 
29138 61 6c 6c 20 76 61 6c 75 65 73 20 6f 6e 20 74 68  all values on th
29139 65 20 72 69 67 68 74 2d 68 61 6e 64 20 73 69 64  e right-hand sid
2913a 65 20 6f 66 20 74 68 65 20 49 4e 2e 0a 2a 2a 0a  e of the IN..**.
2913b 2a 2a 20 54 68 65 72 65 20 61 72 65 20 42 74 72  ** There are Btr
2913c 65 65 20 63 75 72 73 6f 72 73 20 61 73 73 6f 63  ee cursors assoc
2913d 69 61 74 65 64 20 77 69 74 68 20 65 61 63 68 20  iated with each 
2913e 74 61 62 6c 65 2e 20 20 74 31 20 75 73 65 73 20  table.  t1 uses 
2913f 63 75 72 73 6f 72 0a 2a 2a 20 6e 75 6d 62 65 72  cursor.** number
29140 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 30 5d 2e   pTabList->a[0].
29141 69 43 75 72 73 6f 72 2e 20 20 74 32 20 75 73 65  iCursor.  t2 use
29142 73 20 74 68 65 20 63 75 72 73 6f 72 20 70 54 61  s the cursor pTa
29143 62 4c 69 73 74 2d 3e 61 5b 31 5d 2e 69 43 75 72  bList->a[1].iCur
29144 73 6f 72 2e 0a 2a 2a 20 41 6e 64 20 73 6f 20 66  sor..** And so f
29145 6f 72 74 68 2e 20 20 54 68 69 73 20 72 6f 75 74  orth.  This rout
29146 69 6e 65 20 67 65 6e 65 72 61 74 65 73 20 63 6f  ine generates co
29147 64 65 20 74 6f 20 6f 70 65 6e 20 74 68 6f 73 65  de to open those
29148 20 56 44 42 45 20 63 75 72 73 6f 72 73 0a 2a 2a   VDBE cursors.**
29149 20 61 6e 64 20 73 71 6c 69 74 65 33 57 68 65 72   and sqlite3Wher
2914a 65 45 6e 64 28 29 20 67 65 6e 65 72 61 74 65 73  eEnd() generates
2914b 20 74 68 65 20 63 6f 64 65 20 74 6f 20 63 6c 6f   the code to clo
2914c 73 65 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20 54  se them..**.** T
2914d 68 65 20 63 6f 64 65 20 74 68 61 74 20 73 71 6c  he code that sql
2914e 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 28 29  ite3WhereBegin()
2914f 20 67 65 6e 65 72 61 74 65 73 20 6c 65 61 76 65   generates leave
29150 73 20 74 68 65 20 63 75 72 73 6f 72 73 20 6e 61  s the cursors na
29151 6d 65 64 0a 2a 2a 20 69 6e 20 70 54 61 62 4c 69  med.** in pTabLi
29152 73 74 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 74  st pointing at t
29153 68 65 69 72 20 61 70 70 72 6f 70 72 69 61 74 65  heir appropriate
29154 20 65 6e 74 72 69 65 73 2e 20 20 54 68 65 20 5b   entries.  The [
29155 2e 2e 2e 5d 20 63 6f 64 65 0a 2a 2a 20 63 61 6e  ...] code.** can
29156 20 75 73 65 20 4f 50 5f 43 6f 6c 75 6d 6e 20 61   use OP_Column a
29157 6e 64 20 4f 50 5f 52 6f 77 69 64 20 6f 70 63 6f  nd OP_Rowid opco
29158 64 65 73 20 6f 6e 20 74 68 65 73 65 20 63 75 72  des on these cur
29159 73 6f 72 73 20 74 6f 20 65 78 74 72 61 63 74 0a  sors to extract.
2915a 2a 2a 20 64 61 74 61 20 66 72 6f 6d 20 74 68 65  ** data from the
2915b 20 76 61 72 69 6f 75 73 20 74 61 62 6c 65 73 20   various tables 
2915c 6f 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 2a 2a 0a  of the loop..**.
2915d 2a 2a 20 49 66 20 74 68 65 20 57 48 45 52 45 20  ** If the WHERE 
2915e 63 6c 61 75 73 65 20 69 73 20 65 6d 70 74 79 2c  clause is empty,
2915f 20 74 68 65 20 66 6f 72 65 61 63 68 20 6c 6f 6f   the foreach loo
29160 70 73 20 6d 75 73 74 20 65 61 63 68 20 73 63 61  ps must each sca
29161 6e 20 74 68 65 69 72 0a 2a 2a 20 65 6e 74 69 72  n their.** entir
29162 65 20 74 61 62 6c 65 73 2e 20 20 54 68 75 73 20  e tables.  Thus 
29163 61 20 74 68 72 65 65 2d 77 61 79 20 6a 6f 69 6e  a three-way join
29164 20 69 73 20 61 6e 20 4f 28 4e 5e 33 29 20 6f 70   is an O(N^3) op
29165 65 72 61 74 69 6f 6e 2e 20 20 42 75 74 20 69 66  eration.  But if
29166 0a 2a 2a 20 74 68 65 20 74 61 62 6c 65 73 20 68  .** the tables h
29167 61 76 65 20 69 6e 64 69 63 65 73 20 61 6e 64 20  ave indices and 
29168 74 68 65 72 65 20 61 72 65 20 74 65 72 6d 73 20  there are terms 
29169 69 6e 20 74 68 65 20 57 48 45 52 45 20 63 6c 61  in the WHERE cla
2916a 75 73 65 20 74 68 61 74 0a 2a 2a 20 72 65 66 65  use that.** refe
2916b 72 20 74 6f 20 74 68 6f 73 65 20 69 6e 64 69 63  r to those indic
2916c 65 73 2c 20 61 20 63 6f 6d 70 6c 65 74 65 20 74  es, a complete t
2916d 61 62 6c 65 20 73 63 61 6e 20 63 61 6e 20 62 65  able scan can be
2916e 20 61 76 6f 69 64 65 64 20 61 6e 64 20 74 68 65   avoided and the
2916f 0a 2a 2a 20 63 6f 64 65 20 77 69 6c 6c 20 72 75  .** code will ru
29170 6e 20 6d 75 63 68 20 66 61 73 74 65 72 2e 20 20  n much faster.  
29171 4d 6f 73 74 20 6f 66 20 74 68 65 20 77 6f 72 6b  Most of the work
29172 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
29173 20 69 73 20 63 68 65 63 6b 69 6e 67 0a 2a 2a 20   is checking.** 
29174 74 6f 20 73 65 65 20 69 66 20 74 68 65 72 65 20  to see if there 
29175 61 72 65 20 69 6e 64 69 63 65 73 20 74 68 61 74  are indices that
29176 20 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20   can be used to 
29177 73 70 65 65 64 20 75 70 20 74 68 65 20 6c 6f 6f  speed up the loo
29178 70 2e 0a 2a 2a 0a 2a 2a 20 54 65 72 6d 73 20 6f  p..**.** Terms o
29179 66 20 74 68 65 20 57 48 45 52 45 20 63 6c 61 75  f the WHERE clau
2917a 73 65 20 61 72 65 20 61 6c 73 6f 20 75 73 65 64  se are also used
2917b 20 74 6f 20 6c 69 6d 69 74 20 77 68 69 63 68 20   to limit which 
2917c 72 6f 77 73 20 61 63 74 75 61 6c 6c 79 0a 2a 2a  rows actually.**
2917d 20 6d 61 6b 65 20 69 74 20 74 6f 20 74 68 65 20   make it to the 
2917e 22 2e 2e 2e 22 20 69 6e 20 74 68 65 20 6d 69 64  "..." in the mid
2917f 64 6c 65 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  dle of the loop.
29180 20 20 41 66 74 65 72 20 65 61 63 68 20 22 66 6f    After each "fo
29181 72 65 61 63 68 22 2c 0a 2a 2a 20 74 65 72 6d 73  reach",.** terms
29182 20 6f 66 20 74 68 65 20 57 48 45 52 45 20 63 6c   of the WHERE cl
29183 61 75 73 65 20 74 68 61 74 20 75 73 65 20 6f 6e  ause that use on
29184 6c 79 20 74 65 72 6d 73 20 69 6e 20 74 68 61 74  ly terms in that
29185 20 6c 6f 6f 70 20 61 6e 64 20 6f 75 74 65 72 0a   loop and outer.
29186 2a 2a 20 6c 6f 6f 70 73 20 61 72 65 20 65 76 61  ** loops are eva
29187 6c 75 61 74 65 64 20 61 6e 64 20 69 66 20 66 61  luated and if fa
29188 6c 73 65 20 61 20 6a 75 6d 70 20 69 73 20 6d 61  lse a jump is ma
29189 64 65 20 61 72 6f 75 6e 64 20 61 6c 6c 20 73 75  de around all su
2918a 62 73 65 71 75 65 6e 74 0a 2a 2a 20 69 6e 6e 65  bsequent.** inne
2918b 72 20 6c 6f 6f 70 73 20 28 6f 72 20 61 72 6f 75  r loops (or arou
2918c 6e 64 20 74 68 65 20 22 2e 2e 2e 22 20 69 66 20  nd the "..." if 
2918d 74 68 65 20 74 65 73 74 20 6f 63 63 75 72 73 20  the test occurs 
2918e 77 69 74 68 69 6e 20 74 68 65 20 69 6e 6e 65 72  within the inner
2918f 2d 0a 2a 2a 20 6d 6f 73 74 20 6c 6f 6f 70 29 0a  -.** most loop).
29190 2a 2a 0a 2a 2a 20 4f 55 54 45 52 20 4a 4f 49 4e  **.** OUTER JOIN
29191 53 0a 2a 2a 0a 2a 2a 20 41 6e 20 6f 75 74 65 72  S.**.** An outer
29192 20 6a 6f 69 6e 20 6f 66 20 74 61 62 6c 65 73 20   join of tables 
29193 74 31 20 61 6e 64 20 74 32 20 69 73 20 63 6f 6e  t1 and t2 is con
29194 63 65 70 74 61 6c 6c 79 20 63 6f 64 65 64 20 61  ceptally coded a
29195 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a  s follows:.**.**
29196 20 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 31      foreach row1
29197 20 69 6e 20 74 31 20 64 6f 0a 2a 2a 20 20 20 20   in t1 do.**    
29198 20 20 66 6c 61 67 20 3d 20 30 0a 2a 2a 20 20 20    flag = 0.**   
29199 20 20 20 66 6f 72 65 61 63 68 20 72 6f 77 32 20     foreach row2 
2919a 69 6e 20 74 32 20 64 6f 0a 2a 2a 20 20 20 20 20  in t2 do.**     
2919b 20 20 20 73 74 61 72 74 3a 0a 2a 2a 20 20 20 20     start:.**    
2919c 20 20 20 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 20        ....**    
2919d 20 20 20 20 20 20 66 6c 61 67 20 3d 20 31 0a 2a        flag = 1.*
2919e 2a 20 20 20 20 20 20 65 6e 64 0a 2a 2a 20 20 20  *      end.**   
2919f 20 20 20 69 66 20 66 6c 61 67 3d 3d 30 20 74 68     if flag==0 th
291a0 65 6e 0a 2a 2a 20 20 20 20 20 20 20 20 6d 6f 76  en.**        mov
291a1 65 20 74 68 65 20 72 6f 77 32 20 63 75 72 73 6f  e the row2 curso
291a2 72 20 74 6f 20 61 20 6e 75 6c 6c 20 72 6f 77 0a  r to a null row.
291a3 2a 2a 20 20 20 20 20 20 20 20 67 6f 74 6f 20 73  **        goto s
291a4 74 61 72 74 0a 2a 2a 20 20 20 20 20 20 66 69 0a  tart.**      fi.
291a5 2a 2a 20 20 20 20 65 6e 64 0a 2a 2a 0a 2a 2a 20  **    end.**.** 
291a6 4f 52 44 45 52 20 42 59 20 43 4c 41 55 53 45 20  ORDER BY CLAUSE 
291a7 50 52 4f 43 45 53 53 49 4e 47 0a 2a 2a 0a 2a 2a  PROCESSING.**.**
291a8 20 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 61   *ppOrderBy is a
291a9 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
291aa 4f 52 44 45 52 20 42 59 20 63 6c 61 75 73 65 20  ORDER BY clause 
291ab 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61 74  of a SELECT stat
291ac 65 6d 65 6e 74 2c 0a 2a 2a 20 69 66 20 74 68 65  ement,.** if the
291ad 72 65 20 69 73 20 6f 6e 65 2e 20 20 49 66 20 74  re is one.  If t
291ae 68 65 72 65 20 69 73 20 6e 6f 20 4f 52 44 45 52  here is no ORDER
291af 20 42 59 20 63 6c 61 75 73 65 20 6f 72 20 69 66   BY clause or if
291b0 20 74 68 69 73 20 72 6f 75 74 69 6e 65 0a 2a 2a   this routine.**
291b1 20 69 73 20 63 61 6c 6c 65 64 20 66 72 6f 6d 20   is called from 
291b2 61 6e 20 55 50 44 41 54 45 20 6f 72 20 44 45 4c  an UPDATE or DEL
291b3 45 54 45 20 73 74 61 74 65 6d 65 6e 74 2c 20 74  ETE statement, t
291b4 68 65 6e 20 70 70 4f 72 64 65 72 42 79 20 69 73  hen ppOrderBy is
291b5 20 4e 55 4c 4c 2e 0a 2a 2a 0a 2a 2a 20 49 66 20   NULL..**.** If 
291b6 61 6e 20 69 6e 64 65 78 20 63 61 6e 20 62 65 20  an index can be 
291b7 75 73 65 64 20 73 6f 20 74 68 61 74 20 74 68 65  used so that the
291b8 20 6e 61 74 75 72 61 6c 20 6f 75 74 70 75 74 20   natural output 
291b9 6f 72 64 65 72 20 6f 66 20 74 68 65 20 74 61 62  order of the tab
291ba 6c 65 0a 2a 2a 20 73 63 61 6e 20 69 73 20 63 6f  le.** scan is co
291bb 72 72 65 63 74 20 66 6f 72 20 74 68 65 20 4f 52  rrect for the OR
291bc 44 45 52 20 42 59 20 63 6c 61 75 73 65 2c 20 74  DER BY clause, t
291bd 68 65 6e 20 74 68 61 74 20 69 6e 64 65 78 20 69  hen that index i
291be 73 20 75 73 65 64 20 61 6e 64 0a 2a 2a 20 2a 70  s used and.** *p
291bf 70 4f 72 64 65 72 42 79 20 69 73 20 73 65 74 20  pOrderBy is set 
291c0 74 6f 20 4e 55 4c 4c 2e 20 20 54 68 69 73 20 69  to NULL.  This i
291c1 73 20 61 6e 20 6f 70 74 69 6d 69 7a 61 74 69 6f  s an optimizatio
291c2 6e 20 74 68 61 74 20 70 72 65 76 65 6e 74 73 20  n that prevents 
291c3 61 6e 0a 2a 2a 20 75 6e 6e 65 63 65 73 73 61 72  an.** unnecessar
291c4 79 20 73 6f 72 74 20 6f 66 20 74 68 65 20 72 65  y sort of the re
291c5 73 75 6c 74 20 73 65 74 20 69 66 20 61 6e 20 69  sult set if an i
291c6 6e 64 65 78 20 61 70 70 72 6f 70 72 69 61 74 65  ndex appropriate
291c7 20 66 6f 72 20 74 68 65 0a 2a 2a 20 4f 52 44 45   for the.** ORDE
291c8 52 20 42 59 20 63 6c 61 75 73 65 20 61 6c 72 65  R BY clause alre
291c9 61 64 79 20 65 78 69 73 74 73 2e 0a 2a 2a 0a 2a  ady exists..**.*
291ca 2a 20 49 66 20 74 68 65 20 77 68 65 72 65 20 63  * If the where c
291cb 6c 61 75 73 65 20 6c 6f 6f 70 73 20 63 61 6e 6e  lause loops cann
291cc 6f 74 20 62 65 20 61 72 72 61 6e 67 65 64 20 74  ot be arranged t
291cd 6f 20 70 72 6f 76 69 64 65 20 74 68 65 20 63 6f  o provide the co
291ce 72 72 65 63 74 0a 2a 2a 20 6f 75 74 70 75 74 20  rrect.** output 
291cf 6f 72 64 65 72 2c 20 74 68 65 6e 20 74 68 65 20  order, then the 
291d0 2a 70 70 4f 72 64 65 72 42 79 20 69 73 20 75 6e  *ppOrderBy is un
291d1 63 68 61 6e 67 65 64 2e 0a 2a 2f 0a 53 51 4c 49  changed..*/.SQLI
291d2 54 45 5f 50 52 49 56 41 54 45 20 57 68 65 72 65  TE_PRIVATE Where
291d3 49 6e 66 6f 20 2a 73 71 6c 69 74 65 33 57 68 65  Info *sqlite3Whe
291d4 72 65 42 65 67 69 6e 28 0a 20 20 50 61 72 73 65  reBegin(.  Parse
291d5 20 2a 70 50 61 72 73 65 2c 20 20 20 20 20 20 20   *pParse,       
291d6 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 63   /* The parser c
291d7 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 53 72 63 4c  ontext */.  SrcL
291d8 69 73 74 20 2a 70 54 61 62 4c 69 73 74 2c 20 20  ist *pTabList,  
291d9 20 20 2f 2a 20 41 20 6c 69 73 74 20 6f 66 20 61    /* A list of a
291da 6c 6c 20 74 61 62 6c 65 73 20 74 6f 20 62 65 20  ll tables to be 
291db 73 63 61 6e 6e 65 64 20 2a 2f 0a 20 20 45 78 70  scanned */.  Exp
291dc 72 20 2a 70 57 68 65 72 65 2c 20 20 20 20 20 20  r *pWhere,      
291dd 20 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20     /* The WHERE 
291de 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
291df 4c 69 73 74 20 2a 2a 70 70 4f 72 64 65 72 42 79  List **ppOrderBy
291e0 2c 20 2f 2a 20 41 6e 20 4f 52 44 45 52 20 42 59  , /* An ORDER BY
291e1 20 63 6c 61 75 73 65 2c 20 6f 72 20 4e 55 4c 4c   clause, or NULL
291e2 20 2a 2f 0a 20 20 75 38 20 77 66 6c 61 67 73 20   */.  u8 wflags 
291e3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
291e4 6e 65 20 6f 66 20 74 68 65 20 57 48 45 52 45 5f  ne of the WHERE_
291e5 2a 20 66 6c 61 67 73 20 64 65 66 69 6e 65 64 20  * flags defined 
291e6 69 6e 20 73 71 6c 69 74 65 49 6e 74 2e 68 20 2a  in sqliteInt.h *
291e7 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 20 20 20  /.){.  int i;   
291e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
291e9 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e 74 65    /* Loop counte
291ea 72 20 2a 2f 0a 20 20 57 68 65 72 65 49 6e 66 6f  r */.  WhereInfo
291eb 20 2a 70 57 49 6e 66 6f 3b 20 20 20 20 20 20 20   *pWInfo;       
291ec 20 20 2f 2a 20 57 69 6c 6c 20 62 65 63 6f 6d 65    /* Will become
291ed 20 74 68 65 20 72 65 74 75 72 6e 20 76 61 6c 75   the return valu
291ee 65 20 6f 66 20 74 68 69 73 20 66 75 6e 63 74 69  e of this functi
291ef 6f 6e 20 2a 2f 0a 20 20 56 64 62 65 20 2a 76 20  on */.  Vdbe *v 
291f0 3d 20 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b  = pParse->pVdbe;
291f1 20 20 20 2f 2a 20 54 68 65 20 76 69 72 74 75 61     /* The virtua
291f2 6c 20 64 61 74 61 62 61 73 65 20 65 6e 67 69 6e  l database engin
291f3 65 20 2a 2f 0a 20 20 69 6e 74 20 62 72 6b 2c 20  e */.  int brk, 
291f4 63 6f 6e 74 20 3d 20 30 3b 20 20 20 20 20 20 20  cont = 0;       
291f5 20 20 2f 2a 20 41 64 64 72 65 73 73 65 73 20 75    /* Addresses u
291f6 73 65 64 20 64 75 72 69 6e 67 20 63 6f 64 65 20  sed during code 
291f7 67 65 6e 65 72 61 74 69 6f 6e 20 2a 2f 0a 20 20  generation */.  
291f8 42 69 74 6d 61 73 6b 20 6e 6f 74 52 65 61 64 79  Bitmask notReady
291f9 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43 75  ;          /* Cu
291fa 72 73 6f 72 73 20 74 68 61 74 20 61 72 65 20 6e  rsors that are n
291fb 6f 74 20 79 65 74 20 70 6f 73 69 74 69 6f 6e 65  ot yet positione
291fc 64 20 2a 2f 0a 20 20 57 68 65 72 65 54 65 72 6d  d */.  WhereTerm
291fd 20 2a 70 54 65 72 6d 3b 20 20 20 20 20 20 20 20   *pTerm;        
291fe 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65 20 74 65    /* A single te
291ff 72 6d 20 69 6e 20 74 68 65 20 57 48 45 52 45 20  rm in the WHERE 
29200 63 6c 61 75 73 65 20 2a 2f 0a 20 20 45 78 70 72  clause */.  Expr
29201 4d 61 73 6b 53 65 74 20 6d 61 73 6b 53 65 74 3b  MaskSet maskSet;
29202 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 78         /* The ex
29203 70 72 65 73 73 69 6f 6e 20 6d 61 73 6b 20 73 65  pression mask se
29204 74 20 2a 2f 0a 20 20 57 68 65 72 65 43 6c 61 75  t */.  WhereClau
29205 73 65 20 77 63 3b 20 20 20 20 20 20 20 20 20 20  se wc;          
29206 20 20 2f 2a 20 54 68 65 20 57 48 45 52 45 20 63    /* The WHERE c
29207 6c 61 75 73 65 20 69 73 20 64 69 76 69 64 65 64  lause is divided
29208 20 69 6e 74 6f 20 74 68 65 73 65 20 74 65 72 6d   into these term
29209 73 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 53 72  s */.  struct Sr
2920a 63 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62  cList_item *pTab
2920b 49 74 65 6d 3b 20 20 2f 2a 20 41 20 73 69 6e 67  Item;  /* A sing
2920c 6c 65 20 65 6e 74 72 79 20 66 72 6f 6d 20 70 54  le entry from pT
2920d 61 62 4c 69 73 74 20 2a 2f 0a 20 20 57 68 65 72  abList */.  Wher
2920e 65 4c 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 20  eLevel *pLevel; 
2920f 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41              /* A
29210 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 69 6e   single level in
29211 20 74 68 65 20 70 57 49 6e 66 6f 20 6c 69 73 74   the pWInfo list
29212 20 2a 2f 0a 20 20 69 6e 74 20 69 46 72 6f 6d 3b   */.  int iFrom;
29213 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29214 20 20 20 20 20 20 2f 2a 20 46 69 72 73 74 20 75        /* First u
29215 6e 75 73 65 64 20 46 52 4f 4d 20 63 6c 61 75 73  nused FROM claus
29216 65 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20 20 69  e element */.  i
29217 6e 74 20 61 6e 64 46 6c 61 67 73 3b 20 20 20 20  nt andFlags;    
29218 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 4e 44            /* AND
29219 2d 65 64 20 63 6f 6d 62 69 6e 61 74 69 6f 6e 20  -ed combination 
2921a 6f 66 20 61 6c 6c 20 77 63 2e 61 5b 5d 2e 66 6c  of all wc.a[].fl
2921b 61 67 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ags */.  sqlite3
2921c 20 2a 64 62 3b 20 20 20 20 20 20 20 20 20 20 20   *db;           
2921d 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2921e 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20  connection */.  
2921f 45 78 70 72 4c 69 73 74 20 2a 70 4f 72 64 65 72  ExprList *pOrder
29220 42 79 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 54 68  By = 0;..  /* Th
29221 65 20 6e 75 6d 62 65 72 20 6f 66 20 74 61 62 6c  e number of tabl
29222 65 73 20 69 6e 20 74 68 65 20 46 52 4f 4d 20 63  es in the FROM c
29223 6c 61 75 73 65 20 69 73 20 6c 69 6d 69 74 65 64  lause is limited
29224 20 62 79 20 74 68 65 20 6e 75 6d 62 65 72 20 6f   by the number o
29225 66 0a 20 20 2a 2a 20 62 69 74 73 20 69 6e 20 61  f.  ** bits in a
29226 20 42 69 74 6d 61 73 6b 20 0a 20 20 2a 2f 0a 20   Bitmask .  */. 
29227 20 69 66 28 20 70 54 61 62 4c 69 73 74 2d 3e 6e   if( pTabList->n
29228 53 72 63 3e 42 4d 53 20 29 7b 0a 20 20 20 20 73  Src>BMS ){.    s
29229 71 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70  qlite3ErrorMsg(p
2922a 50 61 72 73 65 2c 20 22 61 74 20 6d 6f 73 74 20  Parse, "at most 
2922b 25 64 20 74 61 62 6c 65 73 20 69 6e 20 61 20 6a  %d tables in a j
2922c 6f 69 6e 22 2c 20 42 4d 53 29 3b 0a 20 20 20 20  oin", BMS);.    
2922d 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20  return 0;.  }.. 
2922e 20 69 66 28 20 70 70 4f 72 64 65 72 42 79 20 29   if( ppOrderBy )
2922f 7b 0a 20 20 20 20 70 4f 72 64 65 72 42 79 20 3d  {.    pOrderBy =
29230 20 2a 70 70 4f 72 64 65 72 42 79 3b 0a 20 20 7d   *ppOrderBy;.  }
29231 0a 0a 20 20 2f 2a 20 53 70 6c 69 74 20 74 68 65  ..  /* Split the
29232 20 57 48 45 52 45 20 63 6c 61 75 73 65 20 69 6e   WHERE clause in
29233 74 6f 20 73 65 70 61 72 61 74 65 20 73 75 62 65  to separate sube
29234 78 70 72 65 73 73 69 6f 6e 73 20 77 68 65 72 65  xpressions where
29235 20 65 61 63 68 0a 20 20 2a 2a 20 73 75 62 65 78   each.  ** subex
29236 70 72 65 73 73 69 6f 6e 20 69 73 20 73 65 70 61  pression is sepa
29237 72 61 74 65 64 20 62 79 20 61 6e 20 41 4e 44 20  rated by an AND 
29238 6f 70 65 72 61 74 6f 72 2e 0a 20 20 2a 2f 0a 20  operator..  */. 
29239 20 69 6e 69 74 4d 61 73 6b 53 65 74 28 26 6d 61   initMaskSet(&ma
2923a 73 6b 53 65 74 29 3b 0a 20 20 77 68 65 72 65 43  skSet);.  whereC
2923b 6c 61 75 73 65 49 6e 69 74 28 26 77 63 2c 20 70  lauseInit(&wc, p
2923c 50 61 72 73 65 2c 20 26 6d 61 73 6b 53 65 74 29  Parse, &maskSet)
2923d 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 43  ;.  sqlite3ExprC
2923e 6f 64 65 43 6f 6e 73 74 61 6e 74 73 28 70 50 61  odeConstants(pPa
2923f 72 73 65 2c 20 70 57 68 65 72 65 29 3b 0a 20 20  rse, pWhere);.  
29240 77 68 65 72 65 53 70 6c 69 74 28 26 77 63 2c 20  whereSplit(&wc, 
29241 70 57 68 65 72 65 2c 20 54 4b 5f 41 4e 44 29 3b  pWhere, TK_AND);
29242 0a 20 20 20 20 0a 20 20 2f 2a 20 41 6c 6c 6f 63  .    .  /* Alloc
29243 61 74 65 20 61 6e 64 20 69 6e 69 74 69 61 6c 69  ate and initiali
29244 7a 65 20 74 68 65 20 57 68 65 72 65 49 6e 66 6f  ze the WhereInfo
29245 20 73 74 72 75 63 74 75 72 65 20 74 68 61 74 20   structure that 
29246 77 69 6c 6c 20 62 65 63 6f 6d 65 20 74 68 65 0a  will become the.
29247 20 20 2a 2a 20 72 65 74 75 72 6e 20 76 61 6c 75    ** return valu
29248 65 2e 0a 20 20 2a 2f 0a 20 20 64 62 20 3d 20 70  e..  */.  db = p
29249 50 61 72 73 65 2d 3e 64 62 3b 0a 20 20 70 57 49  Parse->db;.  pWI
2924a 6e 66 6f 20 3d 20 73 71 6c 69 74 65 33 44 62 4d  nfo = sqlite3DbM
2924b 61 6c 6c 6f 63 5a 65 72 6f 28 64 62 2c 20 20 0a  allocZero(db,  .
2924c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2924d 20 20 20 20 20 20 73 69 7a 65 6f 66 28 57 68 65        sizeof(Whe
2924e 72 65 49 6e 66 6f 29 20 2b 20 70 54 61 62 4c 69  reInfo) + pTabLi
2924f 73 74 2d 3e 6e 53 72 63 2a 73 69 7a 65 6f 66 28  st->nSrc*sizeof(
29250 57 68 65 72 65 4c 65 76 65 6c 29 29 3b 0a 20 20  WhereLevel));.  
29251 69 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  if( db->mallocFa
29252 69 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f  iled ){.    goto
29253 20 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72   whereBeginError
29254 3b 0a 20 20 7d 0a 20 20 70 57 49 6e 66 6f 2d 3e  ;.  }.  pWInfo->
29255 6e 4c 65 76 65 6c 20 3d 20 70 54 61 62 4c 69 73  nLevel = pTabLis
29256 74 2d 3e 6e 53 72 63 3b 0a 20 20 70 57 49 6e 66  t->nSrc;.  pWInf
29257 6f 2d 3e 70 50 61 72 73 65 20 3d 20 70 50 61 72  o->pParse = pPar
29258 73 65 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 70 54  se;.  pWInfo->pT
29259 61 62 4c 69 73 74 20 3d 20 70 54 61 62 4c 69 73  abList = pTabLis
2925a 74 3b 0a 20 20 70 57 49 6e 66 6f 2d 3e 69 42 72  t;.  pWInfo->iBr
2925b 65 61 6b 20 3d 20 73 71 6c 69 74 65 33 56 64 62  eak = sqlite3Vdb
2925c 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b 0a 0a  eMakeLabel(v);..
2925d 20 20 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73    /* Special cas
2925e 65 3a 20 61 20 57 48 45 52 45 20 63 6c 61 75 73  e: a WHERE claus
2925f 65 20 74 68 61 74 20 69 73 20 63 6f 6e 73 74 61  e that is consta
29260 6e 74 2e 20 20 45 76 61 6c 75 61 74 65 20 74 68  nt.  Evaluate th
29261 65 0a 20 20 2a 2a 20 65 78 70 72 65 73 73 69 6f  e.  ** expressio
29262 6e 20 61 6e 64 20 65 69 74 68 65 72 20 6a 75 6d  n and either jum
29263 70 20 6f 76 65 72 20 61 6c 6c 20 6f 66 20 74 68  p over all of th
29264 65 20 63 6f 64 65 20 6f 72 20 66 61 6c 6c 20 74  e code or fall t
29265 68 72 75 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20  hru..  */.  if( 
29266 70 57 68 65 72 65 20 26 26 20 28 70 54 61 62 4c  pWhere && (pTabL
29267 69 73 74 2d 3e 6e 53 72 63 3d 3d 30 20 7c 7c 20  ist->nSrc==0 || 
29268 73 71 6c 69 74 65 33 45 78 70 72 49 73 43 6f 6e  sqlite3ExprIsCon
29269 73 74 61 6e 74 4e 6f 74 4a 6f 69 6e 28 70 57 68  stantNotJoin(pWh
2926a 65 72 65 29 29 20 29 7b 0a 20 20 20 20 73 71 6c  ere)) ){.    sql
2926b 69 74 65 33 45 78 70 72 49 66 46 61 6c 73 65 28  ite3ExprIfFalse(
2926c 70 50 61 72 73 65 2c 20 70 57 68 65 72 65 2c 20  pParse, pWhere, 
2926d 70 57 49 6e 66 6f 2d 3e 69 42 72 65 61 6b 2c 20  pWInfo->iBreak, 
2926e 53 51 4c 49 54 45 5f 4a 55 4d 50 49 46 4e 55 4c  SQLITE_JUMPIFNUL
2926f 4c 29 3b 0a 20 20 20 20 70 57 68 65 72 65 20 3d  L);.    pWhere =
29270 20 30 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 73   0;.  }..  /* As
29271 73 69 67 6e 20 61 20 62 69 74 20 66 72 6f 6d 20  sign a bit from 
29272 74 68 65 20 62 69 74 6d 61 73 6b 20 74 6f 20 65  the bitmask to e
29273 76 65 72 79 20 74 65 72 6d 20 69 6e 20 74 68 65  very term in the
29274 20 46 52 4f 4d 20 63 6c 61 75 73 65 2e 0a 20 20   FROM clause..  
29275 2a 2a 0a 20 20 2a 2a 20 57 68 65 6e 20 61 73 73  **.  ** When ass
29276 69 67 6e 69 6e 67 20 62 69 74 6d 61 73 6b 20 76  igning bitmask v
29277 61 6c 75 65 73 20 74 6f 20 46 52 4f 4d 20 63 6c  alues to FROM cl
29278 61 75 73 65 20 63 75 72 73 6f 72 73 2c 20 69 74  ause cursors, it
29279 20 6d 75 73 74 20 62 65 0a 20 20 2a 2a 20 74 68   must be.  ** th
2927a 65 20 63 61 73 65 20 74 68 61 74 20 69 66 20 58  e case that if X
2927b 20 69 73 20 74 68 65 20 62 69 74 6d 61 73 6b 20   is the bitmask 
2927c 66 6f 72 20 74 68 65 20 4e 2d 74 68 20 46 52 4f  for the N-th FRO
2927d 4d 20 63 6c 61 75 73 65 20 74 65 72 6d 20 74 68  M clause term th
2927e 65 6e 0a 20 20 2a 2a 20 74 68 65 20 62 69 74 6d  en.  ** the bitm
2927f 61 73 6b 20 66 6f 72 20 61 6c 6c 20 46 52 4f 4d  ask for all FROM
29280 20 63 6c 61 75 73 65 20 74 65 72 6d 73 20 74 6f   clause terms to
29281 20 74 68 65 20 6c 65 66 74 20 6f 66 20 74 68 65   the left of the
29282 20 4e 2d 74 68 20 74 65 72 6d 0a 20 20 2a 2a 20   N-th term.  ** 
29283 69 73 20 28 58 2d 31 29 2e 20 20 20 41 6e 20 65  is (X-1).   An e
29284 78 70 72 65 73 73 69 6f 6e 20 66 72 6f 6d 20 74  xpression from t
29285 68 65 20 4f 4e 20 63 6c 61 75 73 65 20 6f 66 20  he ON clause of 
29286 61 20 4c 45 46 54 20 4a 4f 49 4e 20 63 61 6e 20  a LEFT JOIN can 
29287 75 73 65 0a 20 20 2a 2a 20 69 74 73 20 45 78 70  use.  ** its Exp
29288 72 2e 69 52 69 67 68 74 4a 6f 69 6e 54 61 62 6c  r.iRightJoinTabl
29289 65 20 76 61 6c 75 65 20 74 6f 20 66 69 6e 64 20  e value to find 
2928a 74 68 65 20 62 69 74 6d 61 73 6b 20 6f 66 20 74  the bitmask of t
2928b 68 65 20 72 69 67 68 74 20 74 61 62 6c 65 0a 20  he right table. 
2928c 20 2a 2a 20 6f 66 20 74 68 65 20 6a 6f 69 6e 2e   ** of the join.
2928d 20 20 53 75 62 74 72 61 63 74 69 6e 67 20 6f 6e    Subtracting on
2928e 65 20 66 72 6f 6d 20 74 68 65 20 72 69 67 68 74  e from the right
2928f 20 74 61 62 6c 65 20 62 69 74 6d 61 73 6b 20 67   table bitmask g
29290 69 76 65 73 20 61 0a 20 20 2a 2a 20 62 69 74 6d  ives a.  ** bitm
29291 61 73 6b 20 66 6f 72 20 61 6c 6c 20 74 61 62 6c  ask for all tabl
29292 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74 20 6f  es to the left o
29293 66 20 74 68 65 20 6a 6f 69 6e 2e 20 20 4b 6e 6f  f the join.  Kno
29294 77 69 6e 67 20 74 68 65 20 62 69 74 6d 61 73 6b  wing the bitmask
29295 0a 20 20 2a 2a 20 66 6f 72 20 61 6c 6c 20 74 61  .  ** for all ta
29296 62 6c 65 73 20 74 6f 20 74 68 65 20 6c 65 66 74  bles to the left
29297 20 6f 66 20 61 20 6c 65 66 74 20 6a 6f 69 6e 20   of a left join 
29298 69 73 20 69 6d 70 6f 72 74 61 6e 74 2e 20 20 54  is important.  T
29299 69 63 6b 65 74 20 23 33 30 31 35 2e 0a 20 20 2a  icket #3015..  *
2929a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  /.  for(i=0; i<p
2929b 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69  TabList->nSrc; i
2929c 2b 2b 29 7b 0a 20 20 20 20 63 72 65 61 74 65 4d  ++){.    createM
2929d 61 73 6b 28 26 6d 61 73 6b 53 65 74 2c 20 70 54  ask(&maskSet, pT
2929e 61 62 4c 69 73 74 2d 3e 61 5b 69 5d 2e 69 43 75  abList->a[i].iCu
2929f 72 73 6f 72 29 3b 0a 20 20 7d 0a 23 69 66 6e 64  rsor);.  }.#ifnd
292a0 65 66 20 4e 44 45 42 55 47 0a 20 20 7b 0a 20 20  ef NDEBUG.  {.  
292a1 20 20 42 69 74 6d 61 73 6b 20 74 6f 54 68 65 4c    Bitmask toTheL
292a2 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72  eft = 0;.    for
292a3 28 69 3d 30 3b 20 69 3c 70 54 61 62 4c 69 73 74  (i=0; i<pTabList
292a4 2d 3e 6e 53 72 63 3b 20 69 2b 2b 29 7b 0a 20 20  ->nSrc; i++){.  
292a5 20 20 20 20 42 69 74 6d 61 73 6b 20 6d 20 3d 20      Bitmask m = 
292a6 67 65 74 4d 61 73 6b 28 26 6d 61 73 6b 53 65 74  getMask(&maskSet
292a7 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69 5d  , pTabList->a[i]
292a8 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 20  .iCursor);.     
292a9 20 61 73 73 65 72 74 28 20 28 6d 2d 31 29 3d 3d   assert( (m-1)==
292aa 74 6f 54 68 65 4c 65 66 74 20 29 3b 0a 20 20 20  toTheLeft );.   
292ab 20 20 20 74 6f 54 68 65 4c 65 66 74 20 7c 3d 20     toTheLeft |= 
292ac 6d 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 23 65 6e  m;.    }.  }.#en
292ad 64 69 66 0a 0a 20 20 2f 2a 20 41 6e 61 6c 79 7a  dif..  /* Analyz
292ae 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 73 75 62  e all of the sub
292af 65 78 70 72 65 73 73 69 6f 6e 73 2e 20 20 4e 6f  expressions.  No
292b0 74 65 20 74 68 61 74 20 65 78 70 72 41 6e 61 6c  te that exprAnal
292b1 79 7a 65 28 29 20 6d 69 67 68 74 0a 20 20 2a 2a  yze() might.  **
292b2 20 61 64 64 20 6e 65 77 20 76 69 72 74 75 61 6c   add new virtual
292b3 20 74 65 72 6d 73 20 6f 6e 74 6f 20 74 68 65 20   terms onto the 
292b4 65 6e 64 20 6f 66 20 74 68 65 20 57 48 45 52 45  end of the WHERE
292b5 20 63 6c 61 75 73 65 2e 20 20 57 65 20 64 6f 20   clause.  We do 
292b6 6e 6f 74 0a 20 20 2a 2a 20 77 61 6e 74 20 74 6f  not.  ** want to
292b7 20 61 6e 61 6c 79 7a 65 20 74 68 65 73 65 20 76   analyze these v
292b8 69 72 74 75 61 6c 20 74 65 72 6d 73 2c 20 73 6f  irtual terms, so
292b9 20 73 74 61 72 74 20 61 6e 61 6c 79 7a 69 6e 67   start analyzing
292ba 20 61 74 20 74 68 65 20 65 6e 64 0a 20 20 2a 2a   at the end.  **
292bb 20 61 6e 64 20 77 6f 72 6b 20 66 6f 72 77 61 72   and work forwar
292bc 64 20 73 6f 20 74 68 61 74 20 74 68 65 20 61 64  d so that the ad
292bd 64 65 64 20 76 69 72 74 75 61 6c 20 74 65 72 6d  ded virtual term
292be 73 20 61 72 65 20 6e 65 76 65 72 20 70 72 6f 63  s are never proc
292bf 65 73 73 65 64 2e 0a 20 20 2a 2f 0a 20 20 65 78  essed..  */.  ex
292c0 70 72 41 6e 61 6c 79 7a 65 41 6c 6c 28 70 54 61  prAnalyzeAll(pTa
292c1 62 4c 69 73 74 2c 20 26 77 63 29 3b 0a 20 20 69  bList, &wc);.  i
292c2 66 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69  f( db->mallocFai
292c3 6c 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  led ){.    goto 
292c4 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f 72 3b  whereBeginError;
292c5 0a 20 20 7d 0a 0a 20 20 2f 2a 20 43 68 6f 73 65  .  }..  /* Chose
292c6 20 74 68 65 20 62 65 73 74 20 69 6e 64 65 78 20   the best index 
292c7 74 6f 20 75 73 65 20 66 6f 72 20 65 61 63 68 20  to use for each 
292c8 74 61 62 6c 65 20 69 6e 20 74 68 65 20 46 52 4f  table in the FRO
292c9 4d 20 63 6c 61 75 73 65 2e 0a 20 20 2a 2a 0a 20  M clause..  **. 
292ca 20 2a 2a 20 54 68 69 73 20 6c 6f 6f 70 20 66 69   ** This loop fi
292cb 6c 6c 73 20 69 6e 20 74 68 65 20 66 6f 6c 6c 6f  lls in the follo
292cc 77 69 6e 67 20 66 69 65 6c 64 73 3a 0a 20 20 2a  wing fields:.  *
292cd 2a 0a 20 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d  *.  **   pWInfo-
292ce 3e 61 5b 5d 2e 70 49 64 78 20 20 20 20 20 20 54  >a[].pIdx      T
292cf 68 65 20 69 6e 64 65 78 20 74 6f 20 75 73 65 20  he index to use 
292d0 66 6f 72 20 74 68 69 73 20 6c 65 76 65 6c 20 6f  for this level o
292d1 66 20 74 68 65 20 6c 6f 6f 70 2e 0a 20 20 2a 2a  f the loop..  **
292d2 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 66     pWInfo->a[].f
292d3 6c 61 67 73 20 20 20 20 20 57 48 45 52 45 5f 78  lags     WHERE_x
292d4 78 78 20 66 6c 61 67 73 20 61 73 73 6f 63 69 61  xx flags associa
292d5 74 65 64 20 77 69 74 68 20 70 49 64 78 0a 20 20  ted with pIdx.  
292d6 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d  **   pWInfo->a[]
292d7 2e 6e 45 71 20 20 20 20 20 20 20 54 68 65 20 6e  .nEq       The n
292d8 75 6d 62 65 72 20 6f 66 20 3d 3d 20 61 6e 64 20  umber of == and 
292d9 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 0a 20  IN constraints. 
292da 20 2a 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b   **   pWInfo->a[
292db 5d 2e 69 46 72 6f 6d 20 20 20 20 20 57 68 69 63  ].iFrom     Whic
292dc 68 20 74 65 72 6d 20 6f 66 20 74 68 65 20 46 52  h term of the FR
292dd 4f 4d 20 63 6c 61 75 73 65 20 69 73 20 62 65 69  OM clause is bei
292de 6e 67 20 63 6f 64 65 64 0a 20 20 2a 2a 20 20 20  ng coded.  **   
292df 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e 69 54 61 62  pWInfo->a[].iTab
292e0 43 75 72 20 20 20 54 68 65 20 56 44 42 45 20 63  Cur   The VDBE c
292e1 75 72 73 6f 72 20 66 6f 72 20 74 68 65 20 64 61  ursor for the da
292e2 74 61 62 61 73 65 20 74 61 62 6c 65 0a 20 20 2a  tabase table.  *
292e3 2a 20 20 20 70 57 49 6e 66 6f 2d 3e 61 5b 5d 2e  *   pWInfo->a[].
292e4 69 49 64 78 43 75 72 20 20 20 54 68 65 20 56 44  iIdxCur   The VD
292e5 42 45 20 63 75 72 73 6f 72 20 66 6f 72 20 74 68  BE cursor for th
292e6 65 20 69 6e 64 65 78 0a 20 20 2a 2a 0a 20 20 2a  e index.  **.  *
292e7 2a 20 54 68 69 73 20 6c 6f 6f 70 20 61 6c 73 6f  * This loop also
292e8 20 66 69 67 75 72 65 73 20 6f 75 74 20 74 68 65   figures out the
292e9 20 6e 65 73 74 69 6e 67 20 6f 72 64 65 72 20 6f   nesting order o
292ea 66 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  f tables in the 
292eb 46 52 4f 4d 0a 20 20 2a 2a 20 63 6c 61 75 73 65  FROM.  ** clause
292ec 2e 0a 20 20 2a 2f 0a 20 20 6e 6f 74 52 65 61 64  ..  */.  notRead
292ed 79 20 3d 20 7e 28 42 69 74 6d 61 73 6b 29 30 3b  y = ~(Bitmask)0;
292ee 0a 20 20 70 54 61 62 49 74 65 6d 20 3d 20 70 54  .  pTabItem = pT
292ef 61 62 4c 69 73 74 2d 3e 61 3b 0a 20 20 70 4c 65  abList->a;.  pLe
292f0 76 65 6c 20 3d 20 70 57 49 6e 66 6f 2d 3e 61 3b  vel = pWInfo->a;
292f1 0a 20 20 61 6e 64 46 6c 61 67 73 20 3d 20 7e 30  .  andFlags = ~0
292f2 3b 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  ;.  WHERETRACE((
292f3 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 53  "*** Optimizer S
292f4 74 61 72 74 20 2a 2a 2a 5c 6e 22 29 29 3b 0a 20  tart ***\n"));. 
292f5 20 66 6f 72 28 69 3d 69 46 72 6f 6d 3d 30 2c 20   for(i=iFrom=0, 
292f6 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61  pLevel=pWInfo->a
292f7 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
292f8 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b  rc; i++, pLevel+
292f9 2b 29 7b 0a 20 20 20 20 49 6e 64 65 78 20 2a 70  +){.    Index *p
292fa 49 64 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  Idx;            
292fb 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 66 6f 72      /* Index for
292fc 20 46 52 4f 4d 20 74 61 62 6c 65 20 61 74 20 70   FROM table at p
292fd 54 61 62 49 74 65 6d 20 2a 2f 0a 20 20 20 20 69  TabItem */.    i
292fe 6e 74 20 66 6c 61 67 73 3b 20 20 20 20 20 20 20  nt flags;       
292ff 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c             /* Fl
29300 61 67 73 20 61 73 73 73 6f 63 69 61 74 65 64 20  ags asssociated 
29301 77 69 74 68 20 70 49 64 78 20 2a 2f 0a 20 20 20  with pIdx */.   
29302 20 69 6e 74 20 6e 45 71 3b 20 20 20 20 20 20 20   int nEq;       
29303 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29304 4e 75 6d 62 65 72 20 6f 66 20 3d 3d 20 6f 72 20  Number of == or 
29305 49 4e 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 2a  IN constraints *
29306 2f 0a 20 20 20 20 64 6f 75 62 6c 65 20 63 6f 73  /.    double cos
29307 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
29308 20 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 66 6f    /* The cost fo
29309 72 20 70 49 64 78 20 2a 2f 0a 20 20 20 20 69 6e  r pIdx */.    in
2930a 74 20 6a 3b 20 20 20 20 20 20 20 20 20 20 20 20  t j;            
2930b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72            /* For
2930c 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 46 52   looping over FR
2930d 4f 4d 20 74 61 62 6c 65 73 20 2a 2f 0a 20 20 20  OM tables */.   
2930e 20 49 6e 64 65 78 20 2a 70 42 65 73 74 20 3d 20   Index *pBest = 
2930f 30 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  0;           /* 
29310 54 68 65 20 62 65 73 74 20 69 6e 64 65 78 20 73  The best index s
29311 65 65 6e 20 73 6f 20 66 61 72 20 2a 2f 0a 20 20  een so far */.  
29312 20 20 69 6e 74 20 62 65 73 74 46 6c 61 67 73 20    int bestFlags 
29313 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  = 0;          /*
29314 20 46 6c 61 67 73 20 61 73 73 6f 63 69 61 74 65   Flags associate
29315 64 20 77 69 74 68 20 70 42 65 73 74 20 2a 2f 0a  d with pBest */.
29316 20 20 20 20 69 6e 74 20 62 65 73 74 4e 45 71 20      int bestNEq 
29317 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
29318 2f 2a 20 6e 45 71 20 61 73 73 6f 63 69 61 74 65  /* nEq associate
29319 64 20 77 69 74 68 20 70 42 65 73 74 20 2a 2f 0a  d with pBest */.
2931a 20 20 20 20 64 6f 75 62 6c 65 20 6c 6f 77 65 73      double lowes
2931b 74 43 6f 73 74 3b 20 20 20 20 20 20 20 20 20 20  tCost;          
2931c 2f 2a 20 43 6f 73 74 20 6f 66 20 74 68 65 20 70  /* Cost of the p
2931d 42 65 73 74 20 2a 2f 0a 20 20 20 20 69 6e 74 20  Best */.    int 
2931e 62 65 73 74 4a 20 3d 20 30 3b 20 20 20 20 20 20  bestJ = 0;      
2931f 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 76          /* The v
29320 61 6c 75 65 20 6f 66 20 6a 20 2a 2f 0a 20 20 20  alue of j */.   
29321 20 42 69 74 6d 61 73 6b 20 6d 3b 20 20 20 20 20   Bitmask m;     
29322 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
29323 42 69 74 6d 61 73 6b 20 76 61 6c 75 65 20 66 6f  Bitmask value fo
29324 72 20 6a 20 6f 72 20 62 65 73 74 4a 20 2a 2f 0a  r j or bestJ */.
29325 20 20 20 20 69 6e 74 20 6f 6e 63 65 20 3d 20 30      int once = 0
29326 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
29327 2f 2a 20 54 72 75 65 20 77 68 65 6e 20 66 69 72  /* True when fir
29328 73 74 20 74 61 62 6c 65 20 69 73 20 73 65 65 6e  st table is seen
29329 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   */.    sqlite3_
2932a 69 6e 64 65 78 5f 69 6e 66 6f 20 2a 70 49 6e 64  index_info *pInd
2932b 65 78 3b 20 2f 2a 20 43 75 72 72 65 6e 74 20 76  ex; /* Current v
2932c 69 72 74 75 61 6c 20 69 6e 64 65 78 20 2a 2f 0a  irtual index */.
2932d 0a 20 20 20 20 6c 6f 77 65 73 74 43 6f 73 74 20  .    lowestCost 
2932e 3d 20 53 51 4c 49 54 45 5f 42 49 47 5f 44 42 4c  = SQLITE_BIG_DBL
2932f 3b 0a 20 20 20 20 66 6f 72 28 6a 3d 69 46 72 6f  ;.    for(j=iFro
29330 6d 2c 20 70 54 61 62 49 74 65 6d 3d 26 70 54 61  m, pTabItem=&pTa
29331 62 4c 69 73 74 2d 3e 61 5b 6a 5d 3b 20 6a 3c 70  bList->a[j]; j<p
29332 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 6a  TabList->nSrc; j
29333 2b 2b 2c 20 70 54 61 62 49 74 65 6d 2b 2b 29 7b  ++, pTabItem++){
29334 0a 20 20 20 20 20 20 69 6e 74 20 64 6f 4e 6f 74  .      int doNot
29335 52 65 6f 72 64 65 72 3b 20 20 2f 2a 20 54 72 75  Reorder;  /* Tru
29336 65 20 69 66 20 74 68 69 73 20 74 61 62 6c 65 20  e if this table 
29337 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20 72 65  should not be re
29338 6f 72 64 65 72 65 64 20 2a 2f 0a 0a 20 20 20 20  ordered */..    
29339 20 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 3d    doNotReorder =
2933a 20 20 28 70 54 61 62 49 74 65 6d 2d 3e 6a 6f 69    (pTabItem->joi
2933b 6e 74 79 70 65 20 26 20 28 4a 54 5f 4c 45 46 54  ntype & (JT_LEFT
2933c 7c 4a 54 5f 43 52 4f 53 53 29 29 21 3d 30 3b 0a  |JT_CROSS))!=0;.
2933d 20 20 20 20 20 20 69 66 28 20 6f 6e 63 65 20 26        if( once &
2933e 26 20 64 6f 4e 6f 74 52 65 6f 72 64 65 72 20 29  & doNotReorder )
2933f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 6d 20   break;.      m 
29340 3d 20 67 65 74 4d 61 73 6b 28 26 6d 61 73 6b 53  = getMask(&maskS
29341 65 74 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69 43  et, pTabItem->iC
29342 75 72 73 6f 72 29 3b 0a 20 20 20 20 20 20 69 66  ursor);.      if
29343 28 20 28 6d 20 26 20 6e 6f 74 52 65 61 64 79 29  ( (m & notReady)
29344 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ==0 ){.        i
29345 66 28 20 6a 3d 3d 69 46 72 6f 6d 20 29 20 69 46  f( j==iFrom ) iF
29346 72 6f 6d 2b 2b 3b 0a 20 20 20 20 20 20 20 20 63  rom++;.        c
29347 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d  ontinue;.      }
29348 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
29349 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 20 29 3b  TabItem->pTab );
2934a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2934b 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42 4c  OMIT_VIRTUALTABL
2934c 45 0a 20 20 20 20 20 20 69 66 28 20 49 73 56 69  E.      if( IsVi
2934d 72 74 75 61 6c 28 70 54 61 62 49 74 65 6d 2d 3e  rtual(pTabItem->
2934e 70 54 61 62 29 20 29 7b 0a 20 20 20 20 20 20 20  pTab) ){.       
2934f 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69   sqlite3_index_i
29350 6e 66 6f 20 2a 2a 70 70 49 64 78 49 6e 66 6f 20  nfo **ppIdxInfo 
29351 3d 20 26 70 57 49 6e 66 6f 2d 3e 61 5b 6a 5d 2e  = &pWInfo->a[j].
29352 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pIdxInfo;.      
29353 20 20 63 6f 73 74 20 3d 20 62 65 73 74 56 69 72    cost = bestVir
29354 74 75 61 6c 49 6e 64 65 78 28 70 50 61 72 73 65  tualIndex(pParse
29355 2c 20 26 77 63 2c 20 70 54 61 62 49 74 65 6d 2c  , &wc, pTabItem,
29356 20 6e 6f 74 52 65 61 64 79 2c 0a 20 20 20 20 20   notReady,.     
29357 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29358 20 20 20 20 20 20 20 20 20 20 20 70 70 4f 72 64             ppOrd
29359 65 72 42 79 20 3f 20 2a 70 70 4f 72 64 65 72 42  erBy ? *ppOrderB
2935a 79 20 3a 20 30 2c 20 69 3d 3d 30 2c 0a 20 20 20  y : 0, i==0,.   
2935b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2935c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70 49               ppI
2935d 64 78 49 6e 66 6f 29 3b 0a 20 20 20 20 20 20 20  dxInfo);.       
2935e 20 66 6c 61 67 73 20 3d 20 57 48 45 52 45 5f 56   flags = WHERE_V
2935f 49 52 54 55 41 4c 54 41 42 4c 45 3b 0a 20 20 20  IRTUALTABLE;.   
29360 20 20 20 20 20 70 49 6e 64 65 78 20 3d 20 2a 70       pIndex = *p
29361 70 49 64 78 49 6e 66 6f 3b 0a 20 20 20 20 20 20  pIdxInfo;.      
29362 20 20 69 66 28 20 70 49 6e 64 65 78 20 26 26 20    if( pIndex && 
29363 70 49 6e 64 65 78 2d 3e 6f 72 64 65 72 42 79 43  pIndex->orderByC
29364 6f 6e 73 75 6d 65 64 20 29 7b 0a 20 20 20 20 20  onsumed ){.     
29365 20 20 20 20 20 66 6c 61 67 73 20 3d 20 57 48 45       flags = WHE
29366 52 45 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 20  RE_VIRTUALTABLE 
29367 7c 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 3b  | WHERE_ORDERBY;
29368 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29369 20 20 20 70 49 64 78 20 3d 20 30 3b 0a 20 20 20     pIdx = 0;.   
2936a 20 20 20 20 20 6e 45 71 20 3d 20 30 3b 0a 20 20       nEq = 0;.  
2936b 20 20 20 20 20 20 69 66 28 20 28 53 51 4c 49 54        if( (SQLIT
2936c 45 5f 42 49 47 5f 44 42 4c 2f 32 2e 30 29 3c 63  E_BIG_DBL/2.0)<c
2936d 6f 73 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ost ){.         
2936e 20 2f 2a 20 54 68 65 20 63 6f 73 74 20 69 73 20   /* The cost is 
2936f 6e 6f 74 20 61 6c 6c 6f 77 65 64 20 74 6f 20 62  not allowed to b
29370 65 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 51  e larger than SQ
29371 4c 49 54 45 5f 42 49 47 5f 44 42 4c 20 28 74 68  LITE_BIG_DBL (th
29372 65 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 69  e.          ** i
29373 6e 69 74 61 6c 20 76 61 6c 75 65 20 6f 66 20 6c  nital value of l
29374 6f 77 65 73 74 43 6f 73 74 20 69 6e 20 74 68 69  owestCost in thi
29375 73 20 6c 6f 6f 70 2e 20 49 66 20 69 74 20 69 73  s loop. If it is
29376 2c 20 74 68 65 6e 0a 20 20 20 20 20 20 20 20 20  , then.         
29377 20 2a 2a 20 74 68 65 20 28 63 6f 73 74 3c 6c 6f   ** the (cost<lo
29378 77 65 73 74 43 6f 73 74 29 20 74 65 73 74 20 62  westCost) test b
29379 65 6c 6f 77 20 77 69 6c 6c 20 6e 65 76 65 72 20  elow will never 
2937a 62 65 20 74 72 75 65 20 61 6e 64 0a 20 20 20 20  be true and.    
2937b 20 20 20 20 20 20 2a 2a 20 70 4c 65 76 65 6c 2d        ** pLevel-
2937c 3e 70 42 65 73 74 49 64 78 20 6e 65 76 65 72 20  >pBestIdx never 
2937d 73 65 74 2e 0a 20 20 20 20 20 20 20 20 20 20 2a  set..          *
2937e 2f 20 0a 20 20 20 20 20 20 20 20 20 20 63 6f 73  / .          cos
2937f 74 20 3d 20 28 53 51 4c 49 54 45 5f 42 49 47 5f  t = (SQLITE_BIG_
29380 44 42 4c 2f 32 2e 30 29 3b 0a 20 20 20 20 20 20  DBL/2.0);.      
29381 20 20 7d 0a 20 20 20 20 20 20 7d 65 6c 73 65 20    }.      }else 
29382 0a 23 65 6e 64 69 66 0a 20 20 20 20 20 20 7b 0a  .#endif.      {.
29383 20 20 20 20 20 20 20 20 63 6f 73 74 20 3d 20 62          cost = b
29384 65 73 74 49 6e 64 65 78 28 70 50 61 72 73 65 2c  estIndex(pParse,
29385 20 26 77 63 2c 20 70 54 61 62 49 74 65 6d 2c 20   &wc, pTabItem, 
29386 6e 6f 74 52 65 61 64 79 2c 0a 20 20 20 20 20 20  notReady,.      
29387 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29388 20 20 20 28 69 3d 3d 30 20 26 26 20 70 70 4f 72     (i==0 && ppOr
29389 64 65 72 42 79 29 20 3f 20 2a 70 70 4f 72 64 65  derBy) ? *ppOrde
2938a 72 42 79 20 3a 20 30 2c 0a 20 20 20 20 20 20 20  rBy : 0,.       
2938b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2938c 20 20 26 70 49 64 78 2c 20 26 66 6c 61 67 73 2c    &pIdx, &flags,
2938d 20 26 6e 45 71 29 3b 0a 20 20 20 20 20 20 20 20   &nEq);.        
2938e 70 49 6e 64 65 78 20 3d 20 30 3b 0a 20 20 20 20  pIndex = 0;.    
2938f 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 63 6f    }.      if( co
29390 73 74 3c 6c 6f 77 65 73 74 43 6f 73 74 20 29 7b  st<lowestCost ){
29391 0a 20 20 20 20 20 20 20 20 6f 6e 63 65 20 3d 20  .        once = 
29392 31 3b 0a 20 20 20 20 20 20 20 20 6c 6f 77 65 73  1;.        lowes
29393 74 43 6f 73 74 20 3d 20 63 6f 73 74 3b 0a 20 20  tCost = cost;.  
29394 20 20 20 20 20 20 70 42 65 73 74 20 3d 20 70 49        pBest = pI
29395 64 78 3b 0a 20 20 20 20 20 20 20 20 62 65 73 74  dx;.        best
29396 46 6c 61 67 73 20 3d 20 66 6c 61 67 73 3b 0a 20  Flags = flags;. 
29397 20 20 20 20 20 20 20 62 65 73 74 4e 45 71 20 3d         bestNEq =
29398 20 6e 45 71 3b 0a 20 20 20 20 20 20 20 20 62 65   nEq;.        be
29399 73 74 4a 20 3d 20 6a 3b 0a 20 20 20 20 20 20 20  stJ = j;.       
2939a 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49 64   pLevel->pBestId
2939b 78 20 3d 20 70 49 6e 64 65 78 3b 0a 20 20 20 20  x = pIndex;.    
2939c 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 64 6f    }.      if( do
2939d 4e 6f 74 52 65 6f 72 64 65 72 20 29 20 62 72 65  NotReorder ) bre
2939e 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 57 48  ak;.    }.    WH
2939f 45 52 45 54 52 41 43 45 28 28 22 2a 2a 2a 20 4f  ERETRACE(("*** O
293a0 70 74 69 6d 69 7a 65 72 20 73 65 6c 65 63 74 73  ptimizer selects
293a1 20 74 61 62 6c 65 20 25 64 20 66 6f 72 20 6c 6f   table %d for lo
293a2 6f 70 20 25 64 5c 6e 22 2c 20 62 65 73 74 4a 2c  op %d\n", bestJ,
293a3 0a 20 20 20 20 20 20 20 20 20 20 20 70 4c 65 76  .           pLev
293a4 65 6c 2d 70 57 49 6e 66 6f 2d 3e 61 29 29 3b 0a  el-pWInfo->a));.
293a5 20 20 20 20 69 66 28 20 28 62 65 73 74 46 6c 61      if( (bestFla
293a6 67 73 20 26 20 57 48 45 52 45 5f 4f 52 44 45 52  gs & WHERE_ORDER
293a7 42 59 29 21 3d 30 20 29 7b 0a 20 20 20 20 20 20  BY)!=0 ){.      
293a8 2a 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a  *ppOrderBy = 0;.
293a9 20 20 20 20 7d 0a 20 20 20 20 61 6e 64 46 6c 61      }.    andFla
293aa 67 73 20 26 3d 20 62 65 73 74 46 6c 61 67 73 3b  gs &= bestFlags;
293ab 0a 20 20 20 20 70 4c 65 76 65 6c 2d 3e 66 6c 61  .    pLevel->fla
293ac 67 73 20 3d 20 62 65 73 74 46 6c 61 67 73 3b 0a  gs = bestFlags;.
293ad 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 49 64 78      pLevel->pIdx
293ae 20 3d 20 70 42 65 73 74 3b 0a 20 20 20 20 70 4c   = pBest;.    pL
293af 65 76 65 6c 2d 3e 6e 45 71 20 3d 20 62 65 73 74  evel->nEq = best
293b0 4e 45 71 3b 0a 20 20 20 20 70 4c 65 76 65 6c 2d  NEq;.    pLevel-
293b1 3e 61 49 6e 4c 6f 6f 70 20 3d 20 30 3b 0a 20 20  >aInLoop = 0;.  
293b2 20 20 70 4c 65 76 65 6c 2d 3e 6e 49 6e 20 3d 20    pLevel->nIn = 
293b3 30 3b 0a 20 20 20 20 69 66 28 20 70 42 65 73 74  0;.    if( pBest
293b4 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c   ){.      pLevel
293b5 2d 3e 69 49 64 78 43 75 72 20 3d 20 70 50 61 72  ->iIdxCur = pPar
293b6 73 65 2d 3e 6e 54 61 62 2b 2b 3b 0a 20 20 20 20  se->nTab++;.    
293b7 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70 4c 65  }else{.      pLe
293b8 76 65 6c 2d 3e 69 49 64 78 43 75 72 20 3d 20 2d  vel->iIdxCur = -
293b9 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 6e 6f 74  1;.    }.    not
293ba 52 65 61 64 79 20 26 3d 20 7e 67 65 74 4d 61 73  Ready &= ~getMas
293bb 6b 28 26 6d 61 73 6b 53 65 74 2c 20 70 54 61 62  k(&maskSet, pTab
293bc 4c 69 73 74 2d 3e 61 5b 62 65 73 74 4a 5d 2e 69  List->a[bestJ].i
293bd 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 70 4c 65  Cursor);.    pLe
293be 76 65 6c 2d 3e 69 46 72 6f 6d 20 3d 20 62 65 73  vel->iFrom = bes
293bf 74 4a 3b 0a 0a 20 20 20 20 2f 2a 20 43 68 65 63  tJ;..    /* Chec
293c0 6b 20 74 68 61 74 20 69 66 20 74 68 65 20 74 61  k that if the ta
293c1 62 6c 65 20 73 63 61 6e 6e 65 64 20 62 79 20 74  ble scanned by t
293c2 68 69 73 20 6c 6f 6f 70 20 69 74 65 72 61 74 69  his loop iterati
293c3 6f 6e 20 68 61 64 20 61 6e 0a 20 20 20 20 2a 2a  on had an.    **
293c4 20 49 4e 44 45 58 45 44 20 42 59 20 63 6c 61 75   INDEXED BY clau
293c5 73 65 20 61 74 74 61 63 68 65 64 20 74 6f 20 69  se attached to i
293c6 74 2c 20 74 68 61 74 20 74 68 65 20 6e 61 6d 65  t, that the name
293c7 64 20 69 6e 64 65 78 20 69 73 20 62 65 69 6e 67  d index is being
293c8 0a 20 20 20 20 2a 2a 20 75 73 65 64 20 66 6f 72  .    ** used for
293c9 20 74 68 65 20 73 63 61 6e 2e 20 49 66 20 6e 6f   the scan. If no
293ca 74 2c 20 74 68 65 6e 20 71 75 65 72 79 20 63 6f  t, then query co
293cb 6d 70 69 6c 61 74 69 6f 6e 20 68 61 73 20 66 61  mpilation has fa
293cc 69 6c 65 64 2e 0a 20 20 20 20 2a 2a 20 52 65 74  iled..    ** Ret
293cd 75 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20  urn an error..  
293ce 20 20 2a 2f 0a 20 20 20 20 70 49 64 78 20 3d 20    */.    pIdx = 
293cf 70 54 61 62 4c 69 73 74 2d 3e 61 5b 62 65 73 74  pTabList->a[best
293d0 4a 5d 2e 70 49 6e 64 65 78 3b 0a 20 20 20 20 61  J].pIndex;.    a
293d1 73 73 65 72 74 28 20 21 70 49 64 78 20 7c 7c 20  ssert( !pIdx || 
293d2 21 70 42 65 73 74 20 7c 7c 20 70 49 64 78 3d 3d  !pBest || pIdx==
293d3 70 42 65 73 74 20 29 3b 0a 20 20 20 20 69 66 28  pBest );.    if(
293d4 20 70 49 64 78 20 26 26 20 70 42 65 73 74 21 3d   pIdx && pBest!=
293d5 70 49 64 78 20 29 7b 0a 20 20 20 20 20 20 73 71  pIdx ){.      sq
293d6 6c 69 74 65 33 45 72 72 6f 72 4d 73 67 28 70 50  lite3ErrorMsg(pP
293d7 61 72 73 65 2c 20 22 63 61 6e 6e 6f 74 20 75 73  arse, "cannot us
293d8 65 20 69 6e 64 65 78 3a 20 25 73 22 2c 20 70 49  e index: %s", pI
293d9 64 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20  dx->zName);.    
293da 20 20 67 6f 74 6f 20 77 68 65 72 65 42 65 67 69    goto whereBegi
293db 6e 45 72 72 6f 72 3b 0a 20 20 20 20 7d 0a 20 20  nError;.    }.  
293dc 7d 0a 20 20 57 48 45 52 45 54 52 41 43 45 28 28  }.  WHERETRACE((
293dd 22 2a 2a 2a 20 4f 70 74 69 6d 69 7a 65 72 20 46  "*** Optimizer F
293de 69 6e 69 73 68 65 64 20 2a 2a 2a 5c 6e 22 29 29  inished ***\n"))
293df 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 74  ;..  /* If the t
293e0 6f 74 61 6c 20 71 75 65 72 79 20 6f 6e 6c 79 20  otal query only 
293e1 73 65 6c 65 63 74 73 20 61 20 73 69 6e 67 6c 65  selects a single
293e2 20 72 6f 77 2c 20 74 68 65 6e 20 74 68 65 20 4f   row, then the O
293e3 52 44 45 52 20 42 59 0a 20 20 2a 2a 20 63 6c 61  RDER BY.  ** cla
293e4 75 73 65 20 69 73 20 69 72 72 65 6c 65 76 61 6e  use is irrelevan
293e5 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28 61  t..  */.  if( (a
293e6 6e 64 46 6c 61 67 73 20 26 20 57 48 45 52 45 5f  ndFlags & WHERE_
293e7 55 4e 49 51 55 45 29 21 3d 30 20 26 26 20 70 70  UNIQUE)!=0 && pp
293e8 4f 72 64 65 72 42 79 20 29 7b 0a 20 20 20 20 2a  OrderBy ){.    *
293e9 70 70 4f 72 64 65 72 42 79 20 3d 20 30 3b 0a 20  ppOrderBy = 0;. 
293ea 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20   }..  /* If the 
293eb 63 61 6c 6c 65 72 20 69 73 20 61 6e 20 55 50 44  caller is an UPD
293ec 41 54 45 20 6f 72 20 44 45 4c 45 54 45 20 73 74  ATE or DELETE st
293ed 61 74 65 6d 65 6e 74 20 74 68 61 74 20 69 73 20  atement that is 
293ee 72 65 71 75 65 73 74 69 6e 67 0a 20 20 2a 2a 20  requesting.  ** 
293ef 74 6f 20 75 73 65 20 61 20 6f 6e 65 2d 70 61 73  to use a one-pas
293f0 73 20 61 6c 67 6f 72 69 74 68 6d 2c 20 64 65 74  s algorithm, det
293f1 65 72 6d 69 6e 65 20 69 66 20 74 68 69 73 20 69  ermine if this i
293f2 73 20 61 70 70 72 6f 70 72 69 61 74 65 2e 0a 20  s appropriate.. 
293f3 20 2a 2a 20 54 68 65 20 6f 6e 65 2d 70 61 73 73   ** The one-pass
293f4 20 61 6c 67 6f 72 69 74 68 6d 20 6f 6e 6c 79 20   algorithm only 
293f5 77 6f 72 6b 73 20 69 66 20 74 68 65 20 57 48 45  works if the WHE
293f6 52 45 20 63 6c 61 75 73 65 20 63 6f 6e 73 74 72  RE clause constr
293f7 61 69 6e 74 73 0a 20 20 2a 2a 20 74 68 65 20 73  aints.  ** the s
293f8 74 61 74 65 6d 65 6e 74 20 74 6f 20 75 70 64 61  tatement to upda
293f9 74 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 2e  te a single row.
293fa 0a 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  .  */.  assert( 
293fb 28 77 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f  (wflags & WHERE_
293fc 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45 44 29  ONEPASS_DESIRED)
293fd 3d 3d 30 20 7c 7c 20 70 57 49 6e 66 6f 2d 3e 6e  ==0 || pWInfo->n
293fe 4c 65 76 65 6c 3d 3d 31 20 29 3b 0a 20 20 69 66  Level==1 );.  if
293ff 28 20 28 77 66 6c 61 67 73 20 26 20 57 48 45 52  ( (wflags & WHER
29400 45 5f 4f 4e 45 50 41 53 53 5f 44 45 53 49 52 45  E_ONEPASS_DESIRE
29401 44 29 21 3d 30 20 26 26 20 28 61 6e 64 46 6c 61  D)!=0 && (andFla
29402 67 73 20 26 20 57 48 45 52 45 5f 55 4e 49 51 55  gs & WHERE_UNIQU
29403 45 29 21 3d 30 20 29 7b 0a 20 20 20 20 70 57 49  E)!=0 ){.    pWI
29404 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 3d  nfo->okOnePass =
29405 20 31 3b 0a 20 20 20 20 70 57 49 6e 66 6f 2d 3e   1;.    pWInfo->
29406 61 5b 30 5d 2e 66 6c 61 67 73 20 26 3d 20 7e 57  a[0].flags &= ~W
29407 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 3b 0a 20  HERE_IDX_ONLY;. 
29408 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65 6e 20 61 6c   }..  /* Open al
29409 6c 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65 20  l tables in the 
2940a 70 54 61 62 4c 69 73 74 20 61 6e 64 20 61 6e 79  pTabList and any
2940b 20 69 6e 64 69 63 65 73 20 73 65 6c 65 63 74 65   indices selecte
2940c 64 20 66 6f 72 0a 20 20 2a 2a 20 73 65 61 72 63  d for.  ** searc
2940d 68 69 6e 67 20 74 68 6f 73 65 20 74 61 62 6c 65  hing those table
2940e 73 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65  s..  */.  sqlite
2940f 33 43 6f 64 65 56 65 72 69 66 79 53 63 68 65 6d  3CodeVerifySchem
29410 61 28 70 50 61 72 73 65 2c 20 2d 31 29 3b 20 2f  a(pParse, -1); /
29411 2a 20 49 6e 73 65 72 74 20 74 68 65 20 63 6f 6f  * Insert the coo
29412 6b 69 65 20 76 65 72 69 66 69 65 72 20 47 6f 74  kie verifier Got
29413 6f 20 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20  o */.  for(i=0, 
29414 70 4c 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61  pLevel=pWInfo->a
29415 3b 20 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53  ; i<pTabList->nS
29416 72 63 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b  rc; i++, pLevel+
29417 2b 29 7b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70  +){.    Table *p
29418 54 61 62 3b 20 20 20 20 20 2f 2a 20 54 61 62 6c  Tab;     /* Tabl
29419 65 20 74 6f 20 6f 70 65 6e 20 2a 2f 0a 20 20 20  e to open */.   
2941a 20 49 6e 64 65 78 20 2a 70 49 78 3b 20 20 20 20   Index *pIx;    
2941b 20 20 2f 2a 20 49 6e 64 65 78 20 75 73 65 64 20    /* Index used 
2941c 74 6f 20 61 63 63 65 73 73 20 70 54 61 62 20 28  to access pTab (
2941d 69 66 20 61 6e 79 29 20 2a 2f 0a 20 20 20 20 69  if any) */.    i
2941e 6e 74 20 69 44 62 3b 20 20 20 20 20 20 20 20 20  nt iDb;         
2941f 2f 2a 20 49 6e 64 65 78 20 6f 66 20 64 61 74 61  /* Index of data
29420 62 61 73 65 20 63 6f 6e 74 61 69 6e 69 6e 67 20  base containing 
29421 74 61 62 6c 65 2f 69 6e 64 65 78 20 2a 2f 0a 20  table/index */. 
29422 20 20 20 69 6e 74 20 69 49 64 78 43 75 72 20 3d     int iIdxCur =
29423 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
29424 3b 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  ;..#ifndef SQLIT
29425 45 5f 4f 4d 49 54 5f 45 58 50 4c 41 49 4e 0a 20  E_OMIT_EXPLAIN. 
29426 20 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 65     if( pParse->e
29427 78 70 6c 61 69 6e 3d 3d 32 20 29 7b 0a 20 20 20  xplain==2 ){.   
29428 20 20 20 63 68 61 72 20 2a 7a 4d 73 67 3b 0a 20     char *zMsg;. 
29429 20 20 20 20 20 73 74 72 75 63 74 20 53 72 63 4c       struct SrcL
2942a 69 73 74 5f 69 74 65 6d 20 2a 70 49 74 65 6d 20  ist_item *pItem 
2942b 3d 20 26 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70  = &pTabList->a[p
2942c 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20  Level->iFrom];. 
2942d 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
2942e 74 65 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22  te3MPrintf(db, "
2942f 54 41 42 4c 45 20 25 73 22 2c 20 70 49 74 65 6d  TABLE %s", pItem
29430 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 20 20  ->zName);.      
29431 69 66 28 20 70 49 74 65 6d 2d 3e 7a 41 6c 69 61  if( pItem->zAlia
29432 73 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73  s ){.        zMs
29433 67 20 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65  g = sqlite3MAppe
29434 6e 64 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25  ndf(db, zMsg, "%
29435 73 20 41 53 20 25 73 22 2c 20 7a 4d 73 67 2c 20  s AS %s", zMsg, 
29436 70 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 29 3b 0a  pItem->zAlias);.
29437 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
29438 28 20 28 70 49 78 20 3d 20 70 4c 65 76 65 6c 2d  ( (pIx = pLevel-
29439 3e 70 49 64 78 29 21 3d 30 20 29 7b 0a 20 20 20  >pIdx)!=0 ){.   
2943a 20 20 20 20 20 7a 4d 73 67 20 3d 20 73 71 6c 69       zMsg = sqli
2943b 74 65 33 4d 41 70 70 65 6e 64 66 28 64 62 2c 20  te3MAppendf(db, 
2943c 7a 4d 73 67 2c 20 22 25 73 20 57 49 54 48 20 49  zMsg, "%s WITH I
2943d 4e 44 45 58 20 25 73 22 2c 20 7a 4d 73 67 2c 20  NDEX %s", zMsg, 
2943e 70 49 78 2d 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20  pIx->zName);.   
2943f 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70 4c 65     }else if( pLe
29440 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28 57 48  vel->flags & (WH
29441 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48 45  ERE_ROWID_EQ|WHE
29442 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29 20  RE_ROWID_RANGE) 
29443 29 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20  ){.        zMsg 
29444 3d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64  = sqlite3MAppend
29445 66 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20  f(db, zMsg, "%s 
29446 55 53 49 4e 47 20 50 52 49 4d 41 52 59 20 4b 45  USING PRIMARY KE
29447 59 22 2c 20 7a 4d 73 67 29 3b 0a 20 20 20 20 20  Y", zMsg);.     
29448 20 7d 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54   }.#ifndef SQLIT
29449 45 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41  E_OMIT_VIRTUALTA
2944a 42 4c 45 0a 20 20 20 20 20 20 65 6c 73 65 20 69  BLE.      else i
2944b 66 28 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74  f( pLevel->pBest
2944c 49 64 78 20 29 7b 0a 20 20 20 20 20 20 20 20 73  Idx ){.        s
2944d 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 69 6e 66  qlite3_index_inf
2944e 6f 20 2a 70 42 65 73 74 49 64 78 20 3d 20 70 4c  o *pBestIdx = pL
2944f 65 76 65 6c 2d 3e 70 42 65 73 74 49 64 78 3b 0a  evel->pBestIdx;.
29450 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d 20 73          zMsg = s
29451 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66 28 64  qlite3MAppendf(d
29452 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 56 49 52  b, zMsg, "%s VIR
29453 54 55 41 4c 20 54 41 42 4c 45 20 49 4e 44 45 58  TUAL TABLE INDEX
29454 20 25 64 3a 25 73 22 2c 20 7a 4d 73 67 2c 0a 20   %d:%s", zMsg,. 
29455 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29456 20 20 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78     pBestIdx->idx
29457 4e 75 6d 2c 20 70 42 65 73 74 49 64 78 2d 3e 69  Num, pBestIdx->i
29458 64 78 53 74 72 29 3b 0a 20 20 20 20 20 20 7d 0a  dxStr);.      }.
29459 23 65 6e 64 69 66 0a 20 20 20 20 20 20 69 66 28  #endif.      if(
2945a 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
2945b 20 57 48 45 52 45 5f 4f 52 44 45 52 42 59 20 29   WHERE_ORDERBY )
2945c 7b 0a 20 20 20 20 20 20 20 20 7a 4d 73 67 20 3d  {.        zMsg =
2945d 20 73 71 6c 69 74 65 33 4d 41 70 70 65 6e 64 66   sqlite3MAppendf
2945e 28 64 62 2c 20 7a 4d 73 67 2c 20 22 25 73 20 4f  (db, zMsg, "%s O
2945f 52 44 45 52 20 42 59 22 2c 20 7a 4d 73 67 29 3b  RDER BY", zMsg);
29460 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 73  .      }.      s
29461 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 34  qlite3VdbeAddOp4
29462 28 76 2c 20 4f 50 5f 45 78 70 6c 61 69 6e 2c 20  (v, OP_Explain, 
29463 69 2c 20 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  i, pLevel->iFrom
29464 2c 20 30 2c 20 7a 4d 73 67 2c 20 50 34 5f 44 59  , 0, zMsg, P4_DY
29465 4e 41 4d 49 43 29 3b 0a 20 20 20 20 7d 0a 23 65  NAMIC);.    }.#e
29466 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
29467 4d 49 54 5f 45 58 50 4c 41 49 4e 20 2a 2f 0a 20  MIT_EXPLAIN */. 
29468 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26 70     pTabItem = &p
29469 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76 65  TabList->a[pLeve
2946a 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20 70  l->iFrom];.    p
2946b 54 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Tab = pTabItem->
2946c 70 54 61 62 3b 0a 20 20 20 20 69 44 62 20 3d 20  pTab;.    iDb = 
2946d 73 71 6c 69 74 65 33 53 63 68 65 6d 61 54 6f 49  sqlite3SchemaToI
2946e 6e 64 65 78 28 70 50 61 72 73 65 2d 3e 64 62 2c  ndex(pParse->db,
2946f 20 70 54 61 62 2d 3e 70 53 63 68 65 6d 61 29 3b   pTab->pSchema);
29470 0a 20 20 20 20 69 66 28 20 28 70 54 61 62 2d 3e  .    if( (pTab->
29471 74 61 62 46 6c 61 67 73 20 26 20 54 46 5f 45 70  tabFlags & TF_Ep
29472 68 65 6d 65 72 61 6c 29 21 3d 30 20 7c 7c 20 70  hemeral)!=0 || p
29473 54 61 62 2d 3e 70 53 65 6c 65 63 74 20 29 20 63  Tab->pSelect ) c
29474 6f 6e 74 69 6e 75 65 3b 0a 23 69 66 6e 64 65 66  ontinue;.#ifndef
29475 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
29476 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20 69 66  TUALTABLE.    if
29477 28 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73 74 49  ( pLevel->pBestI
29478 64 78 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  dx ){.      int 
29479 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  iCur = pTabItem-
2947a 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 20 20  >iCursor;.      
2947b 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
2947c 34 28 76 2c 20 4f 50 5f 56 4f 70 65 6e 2c 20 69  4(v, OP_VOpen, i
2947d 43 75 72 2c 20 30 2c 20 30 2c 0a 20 20 20 20 20  Cur, 0, 0,.     
2947e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2947f 20 20 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29     (const char*)
29480 70 54 61 62 2d 3e 70 56 74 61 62 2c 20 50 34 5f  pTab->pVtab, P4_
29481 56 54 41 42 29 3b 0a 20 20 20 20 7d 65 6c 73 65  VTAB);.    }else
29482 0a 23 65 6e 64 69 66 0a 20 20 20 20 69 66 28 20  .#endif.    if( 
29483 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26  (pLevel->flags &
29484 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59 29   WHERE_IDX_ONLY)
29485 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 69 6e 74  ==0 ){.      int
29486 20 6f 70 20 3d 20 70 57 49 6e 66 6f 2d 3e 6f 6b   op = pWInfo->ok
29487 4f 6e 65 50 61 73 73 20 3f 20 4f 50 5f 4f 70 65  OnePass ? OP_Ope
29488 6e 57 72 69 74 65 20 3a 20 4f 50 5f 4f 70 65 6e  nWrite : OP_Open
29489 52 65 61 64 3b 0a 20 20 20 20 20 20 73 71 6c 69  Read;.      sqli
2948a 74 65 33 4f 70 65 6e 54 61 62 6c 65 28 70 50 61  te3OpenTable(pPa
2948b 72 73 65 2c 20 70 54 61 62 49 74 65 6d 2d 3e 69  rse, pTabItem->i
2948c 43 75 72 73 6f 72 2c 20 69 44 62 2c 20 70 54 61  Cursor, iDb, pTa
2948d 62 2c 20 6f 70 29 3b 0a 20 20 20 20 20 20 69 66  b, op);.      if
2948e 28 20 21 70 57 49 6e 66 6f 2d 3e 6f 6b 4f 6e 65  ( !pWInfo->okOne
2948f 50 61 73 73 20 26 26 20 70 54 61 62 2d 3e 6e 43  Pass && pTab->nC
29490 6f 6c 3c 42 4d 53 20 29 7b 0a 20 20 20 20 20 20  ol<BMS ){.      
29491 20 20 42 69 74 6d 61 73 6b 20 62 20 3d 20 70 54    Bitmask b = pT
29492 61 62 49 74 65 6d 2d 3e 63 6f 6c 55 73 65 64 3b  abItem->colUsed;
29493 0a 20 20 20 20 20 20 20 20 69 6e 74 20 6e 20 3d  .        int n =
29494 20 30 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28   0;.        for(
29495 3b 20 62 3b 20 62 3d 62 3e 3e 31 2c 20 6e 2b 2b  ; b; b=b>>1, n++
29496 29 7b 7d 0a 20 20 20 20 20 20 20 20 73 71 6c 69  ){}.        sqli
29497 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 32 28  te3VdbeChangeP2(
29498 76 2c 20 73 71 6c 69 74 65 33 56 64 62 65 43 75  v, sqlite3VdbeCu
29499 72 72 65 6e 74 41 64 64 72 28 76 29 2d 32 2c 20  rrentAddr(v)-2, 
2949a 6e 29 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65  n);.        asse
2949b 72 74 28 20 6e 3c 3d 70 54 61 62 2d 3e 6e 43 6f  rt( n<=pTab->nCo
2949c 6c 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  l );.      }.   
2949d 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71   }else{.      sq
2949e 6c 69 74 65 33 54 61 62 6c 65 4c 6f 63 6b 28 70  lite3TableLock(p
2949f 50 61 72 73 65 2c 20 69 44 62 2c 20 70 54 61 62  Parse, iDb, pTab
294a0 2d 3e 74 6e 75 6d 2c 20 30 2c 20 70 54 61 62 2d  ->tnum, 0, pTab-
294a1 3e 7a 4e 61 6d 65 29 3b 0a 20 20 20 20 7d 0a 20  >zName);.    }. 
294a2 20 20 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43     pLevel->iTabC
294a3 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 69  ur = pTabItem->i
294a4 43 75 72 73 6f 72 3b 0a 20 20 20 20 69 66 28 20  Cursor;.    if( 
294a5 28 70 49 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70  (pIx = pLevel->p
294a6 49 64 78 29 21 3d 30 20 29 7b 0a 20 20 20 20 20  Idx)!=0 ){.     
294a7 20 4b 65 79 49 6e 66 6f 20 2a 70 4b 65 79 20 3d   KeyInfo *pKey =
294a8 20 73 71 6c 69 74 65 33 49 6e 64 65 78 4b 65 79   sqlite3IndexKey
294a9 69 6e 66 6f 28 70 50 61 72 73 65 2c 20 70 49 78  info(pParse, pIx
294aa 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
294ab 20 70 49 78 2d 3e 70 53 63 68 65 6d 61 3d 3d 70   pIx->pSchema==p
294ac 54 61 62 2d 3e 70 53 63 68 65 6d 61 20 29 3b 0a  Tab->pSchema );.
294ad 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62        sqlite3Vdb
294ae 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 65  eAddOp2(v, OP_Se
294af 74 4e 75 6d 43 6f 6c 75 6d 6e 73 2c 20 30 2c 20  tNumColumns, 0, 
294b0 70 49 78 2d 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 3b  pIx->nColumn+1);
294b1 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 56 64  .      sqlite3Vd
294b2 62 65 41 64 64 4f 70 34 28 76 2c 20 4f 50 5f 4f  beAddOp4(v, OP_O
294b3 70 65 6e 52 65 61 64 2c 20 69 49 64 78 43 75 72  penRead, iIdxCur
294b4 2c 20 70 49 78 2d 3e 74 6e 75 6d 2c 20 69 44 62  , pIx->tnum, iDb
294b5 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
294b6 20 20 20 20 20 20 20 20 20 20 28 63 68 61 72 2a            (char*
294b7 29 70 4b 65 79 2c 20 50 34 5f 4b 45 59 49 4e 46  )pKey, P4_KEYINF
294b8 4f 5f 48 41 4e 44 4f 46 46 29 3b 0a 20 20 20 20  O_HANDOFF);.    
294b9 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
294ba 2c 20 22 25 73 22 2c 20 70 49 78 2d 3e 7a 4e 61  , "%s", pIx->zNa
294bb 6d 65 29 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  me));.    }.    
294bc 73 71 6c 69 74 65 33 43 6f 64 65 56 65 72 69 66  sqlite3CodeVerif
294bd 79 53 63 68 65 6d 61 28 70 50 61 72 73 65 2c 20  ySchema(pParse, 
294be 69 44 62 29 3b 0a 20 20 7d 0a 20 20 70 57 49 6e  iDb);.  }.  pWIn
294bf 66 6f 2d 3e 69 54 6f 70 20 3d 20 73 71 6c 69 74  fo->iTop = sqlit
294c0 65 33 56 64 62 65 43 75 72 72 65 6e 74 41 64 64  e3VdbeCurrentAdd
294c1 72 28 76 29 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65  r(v);..  /* Gene
294c2 72 61 74 65 20 74 68 65 20 63 6f 64 65 20 74 6f  rate the code to
294c3 20 64 6f 20 74 68 65 20 73 65 61 72 63 68 2e 20   do the search. 
294c4 20 45 61 63 68 20 69 74 65 72 61 74 69 6f 6e 20   Each iteration 
294c5 6f 66 20 74 68 65 20 66 6f 72 0a 20 20 2a 2a 20  of the for.  ** 
294c6 6c 6f 6f 70 20 62 65 6c 6f 77 20 67 65 6e 65 72  loop below gener
294c7 61 74 65 73 20 63 6f 64 65 20 66 6f 72 20 61 20  ates code for a 
294c8 73 69 6e 67 6c 65 20 6e 65 73 74 65 64 20 6c 6f  single nested lo
294c9 6f 70 20 6f 66 20 74 68 65 20 56 4d 0a 20 20 2a  op of the VM.  *
294ca 2a 20 70 72 6f 67 72 61 6d 2e 0a 20 20 2a 2f 0a  * program..  */.
294cb 20 20 6e 6f 74 52 65 61 64 79 20 3d 20 7e 28 42    notReady = ~(B
294cc 69 74 6d 61 73 6b 29 30 3b 0a 20 20 66 6f 72 28  itmask)0;.  for(
294cd 69 3d 30 2c 20 70 4c 65 76 65 6c 3d 70 57 49 6e  i=0, pLevel=pWIn
294ce 66 6f 2d 3e 61 3b 20 69 3c 70 54 61 62 4c 69 73  fo->a; i<pTabLis
294cf 74 2d 3e 6e 53 72 63 3b 20 69 2b 2b 2c 20 70 4c  t->nSrc; i++, pL
294d0 65 76 65 6c 2b 2b 29 7b 0a 20 20 20 20 69 6e 74  evel++){.    int
294d1 20 6a 2c 20 6b 3b 0a 20 20 20 20 69 6e 74 20 69   j, k;.    int i
294d2 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e  Cur = pTabItem->
294d3 69 43 75 72 73 6f 72 3b 20 20 2f 2a 20 54 68 65  iCursor;  /* The
294d4 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
294d5 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20   the table */.  
294d6 20 20 49 6e 64 65 78 20 2a 70 49 64 78 3b 20 20    Index *pIdx;  
294d7 20 20 20 20 20 2f 2a 20 54 68 65 20 69 6e 64 65       /* The inde
294d8 78 20 77 65 20 77 69 6c 6c 20 62 65 20 75 73 69  x we will be usi
294d9 6e 67 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 78  ng */.    int nx
294da 74 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  t;           /* 
294db 57 68 65 72 65 20 74 6f 20 6a 75 6d 70 20 74 6f  Where to jump to
294dc 20 63 6f 6e 74 69 6e 75 65 20 77 69 74 68 20 74   continue with t
294dd 68 65 20 6e 65 78 74 20 49 4e 20 63 61 73 65 20  he next IN case 
294de 2a 2f 0a 20 20 20 20 69 6e 74 20 69 49 64 78 43  */.    int iIdxC
294df 75 72 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65  ur;       /* The
294e0 20 56 44 42 45 20 63 75 72 73 6f 72 20 66 6f 72   VDBE cursor for
294e1 20 74 68 65 20 69 6e 64 65 78 20 2a 2f 0a 20 20   the index */.  
294e2 20 20 69 6e 74 20 6f 6d 69 74 54 61 62 6c 65 3b    int omitTable;
294e3 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20       /* True if 
294e4 77 65 20 75 73 65 20 74 68 65 20 69 6e 64 65 78  we use the index
294e5 20 6f 6e 6c 79 20 2a 2f 0a 20 20 20 20 69 6e 74   only */.    int
294e6 20 62 52 65 76 3b 20 20 20 20 20 20 20 20 20 20   bRev;          
294e7 2f 2a 20 54 72 75 65 20 69 66 20 77 65 20 6e 65  /* True if we ne
294e8 65 64 20 74 6f 20 73 63 61 6e 20 69 6e 20 72 65  ed to scan in re
294e9 76 65 72 73 65 20 6f 72 64 65 72 20 2a 2f 0a 0a  verse order */..
294ea 20 20 20 20 70 54 61 62 49 74 65 6d 20 3d 20 26      pTabItem = &
294eb 70 54 61 62 4c 69 73 74 2d 3e 61 5b 70 4c 65 76  pTabList->a[pLev
294ec 65 6c 2d 3e 69 46 72 6f 6d 5d 3b 0a 20 20 20 20  el->iFrom];.    
294ed 69 43 75 72 20 3d 20 70 54 61 62 49 74 65 6d 2d  iCur = pTabItem-
294ee 3e 69 43 75 72 73 6f 72 3b 0a 20 20 20 20 70 49  >iCursor;.    pI
294ef 64 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 49 64  dx = pLevel->pId
294f0 78 3b 0a 20 20 20 20 69 49 64 78 43 75 72 20 3d  x;.    iIdxCur =
294f1 20 70 4c 65 76 65 6c 2d 3e 69 49 64 78 43 75 72   pLevel->iIdxCur
294f2 3b 0a 20 20 20 20 62 52 65 76 20 3d 20 28 70 4c  ;.    bRev = (pL
294f3 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48  evel->flags & WH
294f4 45 52 45 5f 52 45 56 45 52 53 45 29 21 3d 30 3b  ERE_REVERSE)!=0;
294f5 0a 20 20 20 20 6f 6d 69 74 54 61 62 6c 65 20 3d  .    omitTable =
294f6 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20   (pLevel->flags 
294f7 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c 59  & WHERE_IDX_ONLY
294f8 29 21 3d 30 3b 0a 0a 20 20 20 20 2f 2a 20 43 72  )!=0;..    /* Cr
294f9 65 61 74 65 20 6c 61 62 65 6c 73 20 66 6f 72 20  eate labels for 
294fa 74 68 65 20 22 62 72 65 61 6b 22 20 61 6e 64 20  the "break" and 
294fb 22 63 6f 6e 74 69 6e 75 65 22 20 69 6e 73 74 72  "continue" instr
294fc 75 63 74 69 6f 6e 73 0a 20 20 20 20 2a 2a 20 66  uctions.    ** f
294fd 6f 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c  or the current l
294fe 6f 6f 70 2e 20 20 4a 75 6d 70 20 74 6f 20 62 72  oop.  Jump to br
294ff 6b 20 74 6f 20 62 72 65 61 6b 20 6f 75 74 20 6f  k to break out o
29500 66 20 61 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a  f a loop..    **
29501 20 4a 75 6d 70 20 74 6f 20 63 6f 6e 74 20 74 6f   Jump to cont to
29502 20 67 6f 20 69 6d 6d 65 64 69 61 74 65 6c 79 20   go immediately 
29503 74 6f 20 74 68 65 20 6e 65 78 74 20 69 74 65 72  to the next iter
29504 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a 20 20 20  ation of the.   
29505 20 2a 2a 20 6c 6f 6f 70 2e 0a 20 20 20 20 2a 2a   ** loop..    **
29506 0a 20 20 20 20 2a 2a 20 57 68 65 6e 20 74 68 65  .    ** When the
29507 72 65 20 69 73 20 61 6e 20 49 4e 20 6f 70 65 72  re is an IN oper
29508 61 74 6f 72 2c 20 77 65 20 61 6c 73 6f 20 68 61  ator, we also ha
29509 76 65 20 61 20 22 6e 78 74 22 20 6c 61 62 65 6c  ve a "nxt" label
2950a 20 74 68 61 74 0a 20 20 20 20 2a 2a 20 6d 65 61   that.    ** mea
2950b 6e 73 20 74 6f 20 63 6f 6e 74 69 6e 75 65 20 77  ns to continue w
2950c 69 74 68 20 74 68 65 20 6e 65 78 74 20 49 4e 20  ith the next IN 
2950d 76 61 6c 75 65 20 63 6f 6d 62 69 6e 61 74 69 6f  value combinatio
2950e 6e 2e 20 20 57 68 65 6e 0a 20 20 20 20 2a 2a 20  n.  When.    ** 
2950f 74 68 65 72 65 20 61 72 65 20 6e 6f 20 49 4e 20  there are no IN 
29510 6f 70 65 72 61 74 6f 72 73 20 69 6e 20 74 68 65  operators in the
29511 20 63 6f 6e 73 74 72 61 69 6e 74 73 2c 20 74 68   constraints, th
29512 65 20 22 6e 78 74 22 20 6c 61 62 65 6c 0a 20 20  e "nxt" label.  
29513 20 20 2a 2a 20 69 73 20 74 68 65 20 73 61 6d 65    ** is the same
29514 20 61 73 20 22 62 72 6b 22 2e 0a 20 20 20 20 2a   as "brk"..    *
29515 2f 0a 20 20 20 20 62 72 6b 20 3d 20 70 4c 65 76  /.    brk = pLev
29516 65 6c 2d 3e 62 72 6b 20 3d 20 70 4c 65 76 65 6c  el->brk = pLevel
29517 2d 3e 6e 78 74 20 3d 20 73 71 6c 69 74 65 33 56  ->nxt = sqlite3V
29518 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28 76 29 3b  dbeMakeLabel(v);
29519 0a 20 20 20 20 63 6f 6e 74 20 3d 20 70 4c 65 76  .    cont = pLev
2951a 65 6c 2d 3e 63 6f 6e 74 20 3d 20 73 71 6c 69 74  el->cont = sqlit
2951b 65 33 56 64 62 65 4d 61 6b 65 4c 61 62 65 6c 28  e3VdbeMakeLabel(
2951c 76 29 3b 0a 0a 20 20 20 20 2f 2a 20 49 66 20 74  v);..    /* If t
2951d 68 69 73 20 69 73 20 74 68 65 20 72 69 67 68 74  his is the right
2951e 20 74 61 62 6c 65 20 6f 66 20 61 20 4c 45 46 54   table of a LEFT
2951f 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20 61 6c 6c   OUTER JOIN, all
29520 6f 63 61 74 65 20 61 6e 64 0a 20 20 20 20 2a 2a  ocate and.    **
29521 20 69 6e 69 74 69 61 6c 69 7a 65 20 61 20 6d 65   initialize a me
29522 6d 6f 72 79 20 63 65 6c 6c 20 74 68 61 74 20 72  mory cell that r
29523 65 63 6f 72 64 73 20 69 66 20 74 68 69 73 20 74  ecords if this t
29524 61 62 6c 65 20 6d 61 74 63 68 65 73 20 61 6e 79  able matches any
29525 0a 20 20 20 20 2a 2a 20 72 6f 77 20 6f 66 20 74  .    ** row of t
29526 68 65 20 6c 65 66 74 20 74 61 62 6c 65 20 6f 66  he left table of
29527 20 74 68 65 20 6a 6f 69 6e 2e 0a 20 20 20 20 2a   the join..    *
29528 2f 0a 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c  /.    if( pLevel
29529 2d 3e 69 46 72 6f 6d 3e 30 20 26 26 20 28 70 54  ->iFrom>0 && (pT
2952a 61 62 49 74 65 6d 5b 30 5d 2e 6a 6f 69 6e 74 79  abItem[0].jointy
2952b 70 65 20 26 20 4a 54 5f 4c 45 46 54 29 21 3d 30  pe & JT_LEFT)!=0
2952c 20 29 7b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c   ){.      pLevel
2952d 2d 3e 69 4c 65 66 74 4a 6f 69 6e 20 3d 20 2b 2b  ->iLeftJoin = ++
2952e 70 50 61 72 73 65 2d 3e 6e 4d 65 6d 3b 0a 20 20  pParse->nMem;.  
2952f 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
29530 64 64 4f 70 32 28 76 2c 20 4f 50 5f 49 6e 74 65  ddOp2(v, OP_Inte
29531 67 65 72 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e  ger, 0, pLevel->
29532 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20  iLeftJoin);.    
29533 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74 28 28 76    VdbeComment((v
29534 2c 20 22 69 6e 69 74 20 4c 45 46 54 20 4a 4f 49  , "init LEFT JOI
29535 4e 20 6e 6f 2d 6d 61 74 63 68 20 66 6c 61 67 22  N no-match flag"
29536 29 29 3b 0a 20 20 20 20 7d 0a 0a 23 69 66 6e 64  ));.    }..#ifnd
29537 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56  ef SQLITE_OMIT_V
29538 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20 20 20  IRTUALTABLE.    
29539 69 66 28 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73  if( pLevel->pBes
2953a 74 49 64 78 20 29 7b 0a 20 20 20 20 20 20 2f 2a  tIdx ){.      /*
2953b 20 43 61 73 65 20 30 3a 20 20 54 68 65 20 74 61   Case 0:  The ta
2953c 62 6c 65 20 69 73 20 61 20 76 69 72 74 75 61 6c  ble is a virtual
2953d 2d 74 61 62 6c 65 2e 20 20 55 73 65 20 74 68 65  -table.  Use the
2953e 20 56 46 69 6c 74 65 72 20 61 6e 64 20 56 4e 65   VFilter and VNe
2953f 78 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  xt.      **     
29540 20 20 20 20 20 74 6f 20 61 63 63 65 73 73 20 74       to access t
29541 68 65 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a  he data..      *
29542 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20  /.      int j;. 
29543 20 20 20 20 20 69 6e 74 20 69 52 65 67 3b 20 20       int iReg;  
29544 20 2f 2a 20 50 33 20 56 61 6c 75 65 20 66 6f 72   /* P3 Value for
29545 20 4f 50 5f 56 46 69 6c 74 65 72 20 2a 2f 0a 20   OP_VFilter */. 
29546 20 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e 64       sqlite3_ind
29547 65 78 5f 69 6e 66 6f 20 2a 70 42 65 73 74 49 64  ex_info *pBestId
29548 78 20 3d 20 70 4c 65 76 65 6c 2d 3e 70 42 65 73  x = pLevel->pBes
29549 74 49 64 78 3b 0a 20 20 20 20 20 20 69 6e 74 20  tIdx;.      int 
2954a 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20 70 42  nConstraint = pB
2954b 65 73 74 49 64 78 2d 3e 6e 43 6f 6e 73 74 72 61  estIdx->nConstra
2954c 69 6e 74 3b 0a 20 20 20 20 20 20 73 74 72 75 63  int;.      struc
2954d 74 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f  t sqlite3_index_
2954e 63 6f 6e 73 74 72 61 69 6e 74 5f 75 73 61 67 65  constraint_usage
2954f 20 2a 61 55 73 61 67 65 20 3d 0a 20 20 20 20 20   *aUsage =.     
29550 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29551 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29552 20 20 20 20 20 20 20 20 20 20 20 20 20 70 42 65               pBe
29553 73 74 49 64 78 2d 3e 61 43 6f 6e 73 74 72 61 69  stIdx->aConstrai
29554 6e 74 55 73 61 67 65 3b 0a 20 20 20 20 20 20 63  ntUsage;.      c
29555 6f 6e 73 74 20 73 74 72 75 63 74 20 73 71 6c 69  onst struct sqli
29556 74 65 33 5f 69 6e 64 65 78 5f 63 6f 6e 73 74 72  te3_index_constr
29557 61 69 6e 74 20 2a 61 43 6f 6e 73 74 72 61 69 6e  aint *aConstrain
29558 74 20 3d 0a 20 20 20 20 20 20 20 20 20 20 20 20  t =.            
29559 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2955a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2955b 20 20 20 20 20 20 70 42 65 73 74 49 64 78 2d 3e        pBestIdx->
2955c 61 43 6f 6e 73 74 72 61 69 6e 74 3b 0a 0a 20 20  aConstraint;..  
2955d 20 20 20 20 69 52 65 67 20 3d 20 73 71 6c 69 74      iReg = sqlit
2955e 65 33 47 65 74 54 65 6d 70 52 61 6e 67 65 28 70  e3GetTempRange(p
2955f 50 61 72 73 65 2c 20 6e 43 6f 6e 73 74 72 61 69  Parse, nConstrai
29560 6e 74 2b 32 29 3b 0a 20 20 20 20 20 20 70 50 61  nt+2);.      pPa
29561 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43  rse->disableColC
29562 61 63 68 65 2b 2b 3b 0a 20 20 20 20 20 20 66 6f  ache++;.      fo
29563 72 28 6a 3d 31 3b 20 6a 3c 3d 6e 43 6f 6e 73 74  r(j=1; j<=nConst
29564 72 61 69 6e 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20  raint; j++){.   
29565 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20 20 20 20       int k;.    
29566 20 20 20 20 66 6f 72 28 6b 3d 30 3b 20 6b 3c 6e      for(k=0; k<n
29567 43 6f 6e 73 74 72 61 69 6e 74 3b 20 6b 2b 2b 29  Constraint; k++)
29568 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
29569 61 55 73 61 67 65 5b 6b 5d 2e 61 72 67 76 49 6e  aUsage[k].argvIn
2956a 64 65 78 3d 3d 6a 20 29 7b 0a 20 20 20 20 20 20  dex==j ){.      
2956b 20 20 20 20 20 20 69 6e 74 20 69 54 65 72 6d 20        int iTerm 
2956c 3d 20 61 43 6f 6e 73 74 72 61 69 6e 74 5b 6b 5d  = aConstraint[k]
2956d 2e 69 54 65 72 6d 4f 66 66 73 65 74 3b 0a 20 20  .iTermOffset;.  
2956e 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
2956f 28 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62 6c  ( pParse->disabl
29570 65 43 6f 6c 43 61 63 68 65 20 29 3b 0a 20 20 20  eColCache );.   
29571 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33           sqlite3
29572 45 78 70 72 43 6f 64 65 28 70 50 61 72 73 65 2c  ExprCode(pParse,
29573 20 77 63 2e 61 5b 69 54 65 72 6d 5d 2e 70 45 78   wc.a[iTerm].pEx
29574 70 72 2d 3e 70 52 69 67 68 74 2c 20 69 52 65 67  pr->pRight, iReg
29575 2b 6a 2b 31 29 3b 0a 20 20 20 20 20 20 20 20 20  +j+1);.         
29576 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
29577 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a      }.        }.
29578 20 20 20 20 20 20 20 20 69 66 28 20 6b 3d 3d 6e          if( k==n
29579 43 6f 6e 73 74 72 61 69 6e 74 20 29 20 62 72 65  Constraint ) bre
2957a 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
2957b 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2957c 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
2957d 65 20 29 3b 0a 20 20 20 20 20 20 70 50 61 72 73  e );.      pPars
2957e 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
2957f 68 65 2d 2d 3b 0a 20 20 20 20 20 20 73 71 6c 69  he--;.      sqli
29580 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29581 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 70 42 65   OP_Integer, pBe
29582 73 74 49 64 78 2d 3e 69 64 78 4e 75 6d 2c 20 69  stIdx->idxNum, i
29583 52 65 67 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  Reg);.      sqli
29584 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c  te3VdbeAddOp2(v,
29585 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20 6a 2d 31   OP_Integer, j-1
29586 2c 20 69 52 65 67 2b 31 29 3b 0a 20 20 20 20 20  , iReg+1);.     
29587 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29588 70 34 28 76 2c 20 4f 50 5f 56 46 69 6c 74 65 72  p4(v, OP_VFilter
29589 2c 20 69 43 75 72 2c 20 62 72 6b 2c 20 69 52 65  , iCur, brk, iRe
2958a 67 2c 20 70 42 65 73 74 49 64 78 2d 3e 69 64 78  g, pBestIdx->idx
2958b 53 74 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  Str,.           
2958c 20 20 20 20 20 20 20 20 20 20 20 20 20 70 42 65               pBe
2958d 73 74 49 64 78 2d 3e 6e 65 65 64 54 6f 46 72 65  stIdx->needToFre
2958e 65 49 64 78 53 74 72 20 3f 20 50 34 5f 4d 50 52  eIdxStr ? P4_MPR
2958f 49 4e 54 46 20 3a 20 50 34 5f 53 54 41 54 49 43  INTF : P4_STATIC
29590 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
29591 52 65 6c 65 61 73 65 54 65 6d 70 52 61 6e 67 65  ReleaseTempRange
29592 28 70 50 61 72 73 65 2c 20 69 52 65 67 2c 20 6e  (pParse, iReg, n
29593 43 6f 6e 73 74 72 61 69 6e 74 2b 32 29 3b 0a 20  Constraint+2);. 
29594 20 20 20 20 20 70 42 65 73 74 49 64 78 2d 3e 6e       pBestIdx->n
29595 65 65 64 54 6f 46 72 65 65 49 64 78 53 74 72 20  eedToFreeIdxStr 
29596 3d 20 30 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a  = 0;.      for(j
29597 3d 30 3b 20 6a 3c 6e 43 6f 6e 73 74 72 61 69 6e  =0; j<nConstrain
29598 74 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20  t; j++){.       
29599 20 69 66 28 20 61 55 73 61 67 65 5b 6a 5d 2e 6f   if( aUsage[j].o
2959a 6d 69 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  mit ){.         
2959b 20 69 6e 74 20 69 54 65 72 6d 20 3d 20 61 43 6f   int iTerm = aCo
2959c 6e 73 74 72 61 69 6e 74 5b 6a 5d 2e 69 54 65 72  nstraint[j].iTer
2959d 6d 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 20  mOffset;.       
2959e 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
2959f 4c 65 76 65 6c 2c 20 26 77 63 2e 61 5b 69 54 65  Level, &wc.a[iTe
295a0 72 6d 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  rm]);.        }.
295a1 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 4c        }.      pL
295a2 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50 5f 56 4e  evel->op = OP_VN
295a3 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65 76 65  ext;.      pLeve
295a4 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a 20 20  l->p1 = iCur;.  
295a5 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32 20 3d      pLevel->p2 =
295a6 20 73 71 6c 69 74 65 33 56 64 62 65 43 75 72 72   sqlite3VdbeCurr
295a7 65 6e 74 41 64 64 72 28 76 29 3b 0a 20 20 20 20  entAddr(v);.    
295a8 7d 65 6c 73 65 0a 23 65 6e 64 69 66 20 2f 2a 20  }else.#endif /* 
295a9 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52 54  SQLITE_OMIT_VIRT
295aa 55 41 4c 54 41 42 4c 45 20 2a 2f 0a 0a 20 20 20  UALTABLE */..   
295ab 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61   if( pLevel->fla
295ac 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57 49 44  gs & WHERE_ROWID
295ad 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  _EQ ){.      /* 
295ae 43 61 73 65 20 31 3a 20 20 57 65 20 63 61 6e 20  Case 1:  We can 
295af 64 69 72 65 63 74 6c 79 20 72 65 66 65 72 65 6e  directly referen
295b0 63 65 20 61 20 73 69 6e 67 6c 65 20 72 6f 77 20  ce a single row 
295b1 75 73 69 6e 67 20 61 6e 0a 20 20 20 20 20 20 2a  using an.      *
295b2 2a 20 20 20 20 20 20 20 20 20 20 65 71 75 61 6c  *          equal
295b3 69 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61  ity comparison a
295b4 67 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44  gainst the ROWID
295b5 20 66 69 65 6c 64 2e 20 20 4f 72 0a 20 20 20 20   field.  Or.    
295b6 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 77 65    **          we
295b7 20 72 65 66 65 72 65 6e 63 65 20 6d 75 6c 74 69   reference multi
295b8 70 6c 65 20 72 6f 77 73 20 75 73 69 6e 67 20 61  ple rows using a
295b9 20 22 72 6f 77 69 64 20 49 4e 20 28 2e 2e 2e 29   "rowid IN (...)
295ba 22 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  ".      **      
295bb 20 20 20 20 63 6f 6e 73 74 72 75 63 74 2e 0a 20      construct.. 
295bc 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 6e       */.      in
295bd 74 20 72 31 3b 0a 20 20 20 20 20 20 69 6e 74 20  t r1;.      int 
295be 72 74 6d 70 20 3d 20 73 71 6c 69 74 65 33 47 65  rtmp = sqlite3Ge
295bf 74 54 65 6d 70 52 65 67 28 70 50 61 72 73 65 29  tTempReg(pParse)
295c0 3b 0a 20 20 20 20 20 20 70 54 65 72 6d 20 3d 20  ;.      pTerm = 
295c1 66 69 6e 64 54 65 72 6d 28 26 77 63 2c 20 69 43  findTerm(&wc, iC
295c2 75 72 2c 20 2d 31 2c 20 6e 6f 74 52 65 61 64 79  ur, -1, notReady
295c3 2c 20 57 4f 5f 45 51 7c 57 4f 5f 49 4e 2c 20 30  , WO_EQ|WO_IN, 0
295c4 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  );.      assert(
295c5 20 70 54 65 72 6d 21 3d 30 20 29 3b 0a 20 20 20   pTerm!=0 );.   
295c6 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72 6d     assert( pTerm
295c7 2d 3e 70 45 78 70 72 21 3d 30 20 29 3b 0a 20 20  ->pExpr!=0 );.  
295c8 20 20 20 20 61 73 73 65 72 74 28 20 70 54 65 72      assert( pTer
295c9 6d 2d 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69  m->leftCursor==i
295ca 43 75 72 20 29 3b 0a 20 20 20 20 20 20 61 73 73  Cur );.      ass
295cb 65 72 74 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d  ert( omitTable==
295cc 30 20 29 3b 0a 20 20 20 20 20 20 72 31 20 3d 20  0 );.      r1 = 
295cd 63 6f 64 65 45 71 75 61 6c 69 74 79 54 65 72 6d  codeEqualityTerm
295ce 28 70 50 61 72 73 65 2c 20 70 54 65 72 6d 2c 20  (pParse, pTerm, 
295cf 70 4c 65 76 65 6c 2c 20 72 74 6d 70 29 3b 0a 20  pLevel, rtmp);. 
295d0 20 20 20 20 20 6e 78 74 20 3d 20 70 4c 65 76 65       nxt = pLeve
295d1 6c 2d 3e 6e 78 74 3b 0a 20 20 20 20 20 20 73 71  l->nxt;.      sq
295d2 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28  lite3VdbeAddOp2(
295d3 76 2c 20 4f 50 5f 4d 75 73 74 42 65 49 6e 74 2c  v, OP_MustBeInt,
295d4 20 72 31 2c 20 6e 78 74 29 3b 0a 20 20 20 20 20   r1, nxt);.     
295d5 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
295d6 70 33 28 76 2c 20 4f 50 5f 4e 6f 74 45 78 69 73  p3(v, OP_NotExis
295d7 74 73 2c 20 69 43 75 72 2c 20 6e 78 74 2c 20 72  ts, iCur, nxt, r
295d8 31 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  1);.      sqlite
295d9 33 52 65 6c 65 61 73 65 54 65 6d 70 52 65 67 28  3ReleaseTempReg(
295da 70 50 61 72 73 65 2c 20 72 74 6d 70 29 3b 0a 20  pParse, rtmp);. 
295db 20 20 20 20 20 56 64 62 65 43 6f 6d 6d 65 6e 74       VdbeComment
295dc 28 28 76 2c 20 22 70 6b 22 29 29 3b 0a 20 20 20  ((v, "pk"));.   
295dd 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20     pLevel->op = 
295de 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20 7d 65 6c  OP_Noop;.    }el
295df 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 66  se if( pLevel->f
295e0 6c 61 67 73 20 26 20 57 48 45 52 45 5f 52 4f 57  lags & WHERE_ROW
295e1 49 44 5f 52 41 4e 47 45 20 29 7b 0a 20 20 20 20  ID_RANGE ){.    
295e2 20 20 2f 2a 20 43 61 73 65 20 32 3a 20 20 57 65    /* Case 2:  We
295e3 20 68 61 76 65 20 61 6e 20 69 6e 65 71 75 61 6c   have an inequal
295e4 69 74 79 20 63 6f 6d 70 61 72 69 73 6f 6e 20 61  ity comparison a
295e5 67 61 69 6e 73 74 20 74 68 65 20 52 4f 57 49 44  gainst the ROWID
295e6 20 66 69 65 6c 64 2e 0a 20 20 20 20 20 20 2a 2f   field..      */
295e7 0a 20 20 20 20 20 20 69 6e 74 20 74 65 73 74 4f  .      int testO
295e8 70 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20  p = OP_Noop;.   
295e9 20 20 20 69 6e 74 20 73 74 61 72 74 3b 0a 20 20     int start;.  
295ea 20 20 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70      WhereTerm *p
295eb 53 74 61 72 74 2c 20 2a 70 45 6e 64 3b 0a 0a 20  Start, *pEnd;.. 
295ec 20 20 20 20 20 61 73 73 65 72 74 28 20 6f 6d 69       assert( omi
295ed 74 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20 20  tTable==0 );.   
295ee 20 20 20 70 53 74 61 72 74 20 3d 20 66 69 6e 64     pStart = find
295ef 54 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20  Term(&wc, iCur, 
295f0 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f  -1, notReady, WO
295f1 5f 47 54 7c 57 4f 5f 47 45 2c 20 30 29 3b 0a 20  _GT|WO_GE, 0);. 
295f2 20 20 20 20 20 70 45 6e 64 20 3d 20 66 69 6e 64       pEnd = find
295f3 54 65 72 6d 28 26 77 63 2c 20 69 43 75 72 2c 20  Term(&wc, iCur, 
295f4 2d 31 2c 20 6e 6f 74 52 65 61 64 79 2c 20 57 4f  -1, notReady, WO
295f5 5f 4c 54 7c 57 4f 5f 4c 45 2c 20 30 29 3b 0a 20  _LT|WO_LE, 0);. 
295f6 20 20 20 20 20 69 66 28 20 62 52 65 76 20 29 7b       if( bRev ){
295f7 0a 20 20 20 20 20 20 20 20 70 54 65 72 6d 20 3d  .        pTerm =
295f8 20 70 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20   pStart;.       
295f9 20 70 53 74 61 72 74 20 3d 20 70 45 6e 64 3b 0a   pStart = pEnd;.
295fa 20 20 20 20 20 20 20 20 70 45 6e 64 20 3d 20 70          pEnd = p
295fb 54 65 72 6d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  Term;.      }.  
295fc 20 20 20 20 69 66 28 20 70 53 74 61 72 74 20 29      if( pStart )
295fd 7b 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a  {.        Expr *
295fe 70 58 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20  pX;.        int 
295ff 72 31 3b 0a 20 20 20 20 20 20 20 20 70 58 20 3d  r1;.        pX =
29600 20 70 53 74 61 72 74 2d 3e 70 45 78 70 72 3b 0a   pStart->pExpr;.
29601 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
29602 70 58 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 20  pX!=0 );.       
29603 20 61 73 73 65 72 74 28 20 70 53 74 61 72 74 2d   assert( pStart-
29604 3e 6c 65 66 74 43 75 72 73 6f 72 3d 3d 69 43 75  >leftCursor==iCu
29605 72 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 2f 2a  r );..        /*
29606 20 54 68 65 20 46 6f 72 63 65 49 6e 74 20 69 6e   The ForceInt in
29607 73 74 72 75 63 74 69 6f 6e 20 6d 61 79 20 6d 6f  struction may mo
29608 64 69 66 79 20 74 68 65 20 72 65 67 69 73 74 65  dify the registe
29609 72 20 74 68 61 74 20 69 74 20 6f 70 65 72 61 74  r that it operat
2960a 65 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 6f 6e  es.        ** on
2960b 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 20 69 74  . For example it
2960c 20 6d 61 79 20 72 65 70 6c 61 63 65 20 61 20 72   may replace a r
2960d 65 61 6c 20 76 61 6c 75 65 20 77 69 74 68 20 61  eal value with a
2960e 6e 20 69 6e 74 65 67 65 72 20 6f 6e 65 2c 0a 20  n integer one,. 
2960f 20 20 20 20 20 20 20 2a 2a 20 6f 72 20 69 66 20         ** or if 
29610 70 33 20 69 73 20 74 72 75 65 20 69 74 20 6d 61  p3 is true it ma
29611 79 20 69 6e 63 72 65 6d 65 6e 74 20 74 68 65 20  y increment the 
29612 72 65 67 69 73 74 65 72 20 76 61 6c 75 65 2e 20  register value. 
29613 46 6f 72 20 74 68 69 73 0a 20 20 20 20 20 20 20  For this.       
29614 20 2a 2a 20 72 65 61 73 6f 6e 20 77 65 20 6e 65   ** reason we ne
29615 65 64 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20  ed to make sure 
29616 74 68 61 74 20 72 65 67 69 73 74 65 72 20 72 31  that register r1
29617 20 69 73 20 72 65 61 6c 6c 79 20 61 20 6e 65 77   is really a new
29618 6c 79 0a 20 20 20 20 20 20 20 20 2a 2a 20 61 6c  ly.        ** al
29619 6c 6f 63 61 74 65 64 20 74 65 6d 70 6f 72 61 72  located temporar
2961a 79 20 72 65 67 69 73 74 65 72 2c 20 61 6e 64 20  y register, and 
2961b 6e 6f 74 20 70 61 72 74 20 6f 66 20 74 68 65 20  not part of the 
2961c 63 6f 6c 75 6d 6e 2d 63 61 63 68 65 2e 0a 20 20  column-cache..  
2961d 20 20 20 20 20 20 2a 2a 20 46 6f 72 20 74 68 69        ** For thi
2961e 73 20 72 65 61 73 6f 6e 20 77 65 20 63 61 6e 6e  s reason we cann
2961f 6f 74 20 75 73 65 20 73 71 6c 69 74 65 33 45 78  ot use sqlite3Ex
29620 70 72 43 6f 64 65 54 65 6d 70 28 29 20 68 65 72  prCodeTemp() her
29621 65 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20  e..        */.  
29622 20 20 20 20 20 20 72 31 20 3d 20 73 71 6c 69 74        r1 = sqlit
29623 65 33 47 65 74 54 65 6d 70 52 65 67 28 70 50 61  e3GetTempReg(pPa
29624 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20 73 71  rse);.        sq
29625 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70 50  lite3ExprCode(pP
29626 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68 74  arse, pX->pRight
29627 2c 20 72 31 29 3b 0a 0a 20 20 20 20 20 20 20 20  , r1);..        
29628 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29629 33 28 76 2c 20 4f 50 5f 46 6f 72 63 65 49 6e 74  3(v, OP_ForceInt
2962a 2c 20 72 31 2c 20 62 72 6b 2c 20 0a 20 20 20 20  , r1, brk, .    
2962b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2962c 20 20 20 20 20 20 20 20 20 70 58 2d 3e 6f 70 3d           pX->op=
2962d 3d 54 4b 5f 4c 45 20 7c 7c 20 70 58 2d 3e 6f 70  =TK_LE || pX->op
2962e 3d 3d 54 4b 5f 47 54 29 3b 0a 20 20 20 20 20 20  ==TK_GT);.      
2962f 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
29630 4f 70 33 28 76 2c 20 62 52 65 76 20 3f 20 4f 50  Op3(v, bRev ? OP
29631 5f 4d 6f 76 65 4c 74 20 3a 20 4f 50 5f 4d 6f 76  _MoveLt : OP_Mov
29632 65 47 65 2c 20 69 43 75 72 2c 20 62 72 6b 2c 20  eGe, iCur, brk, 
29633 72 31 29 3b 0a 20 20 20 20 20 20 20 20 56 64 62  r1);.        Vdb
29634 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 70 6b  eComment((v, "pk
29635 22 29 29 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  "));.        sql
29636 69 74 65 33 52 65 6c 65 61 73 65 54 65 6d 70 52  ite3ReleaseTempR
29637 65 67 28 70 50 61 72 73 65 2c 20 72 31 29 3b 0a  eg(pParse, r1);.
29638 20 20 20 20 20 20 20 20 64 69 73 61 62 6c 65 54          disableT
29639 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 53 74 61  erm(pLevel, pSta
2963a 72 74 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65  rt);.      }else
2963b 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  {.        sqlite
2963c 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20 62  3VdbeAddOp2(v, b
2963d 52 65 76 20 3f 20 4f 50 5f 4c 61 73 74 20 3a 20  Rev ? OP_Last : 
2963e 4f 50 5f 52 65 77 69 6e 64 2c 20 69 43 75 72 2c  OP_Rewind, iCur,
2963f 20 62 72 6b 29 3b 0a 20 20 20 20 20 20 7d 0a 20   brk);.      }. 
29640 20 20 20 20 20 69 66 28 20 70 45 6e 64 20 29 7b       if( pEnd ){
29641 0a 20 20 20 20 20 20 20 20 45 78 70 72 20 2a 70  .        Expr *p
29642 58 3b 0a 20 20 20 20 20 20 20 20 70 58 20 3d 20  X;.        pX = 
29643 70 45 6e 64 2d 3e 70 45 78 70 72 3b 0a 20 20 20  pEnd->pExpr;.   
29644 20 20 20 20 20 61 73 73 65 72 74 28 20 70 58 21       assert( pX!
29645 3d 30 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =0 );.        as
29646 73 65 72 74 28 20 70 45 6e 64 2d 3e 6c 65 66 74  sert( pEnd->left
29647 43 75 72 73 6f 72 3d 3d 69 43 75 72 20 29 3b 0a  Cursor==iCur );.
29648 20 20 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e          pLevel->
29649 69 4d 65 6d 20 3d 20 2b 2b 70 50 61 72 73 65 2d  iMem = ++pParse-
2964a 3e 6e 4d 65 6d 3b 0a 20 20 20 20 20 20 20 20 73  >nMem;.        s
2964b 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28 70  qlite3ExprCode(p
2964c 50 61 72 73 65 2c 20 70 58 2d 3e 70 52 69 67 68  Parse, pX->pRigh
2964d 74 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d 29  t, pLevel->iMem)
2964e 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70 58  ;.        if( pX
2964f 2d 3e 6f 70 3d 3d 54 4b 5f 4c 54 20 7c 7c 20 70  ->op==TK_LT || p
29650 58 2d 3e 6f 70 3d 3d 54 4b 5f 47 54 20 29 7b 0a  X->op==TK_GT ){.
29651 20 20 20 20 20 20 20 20 20 20 74 65 73 74 4f 70            testOp
29652 20 3d 20 62 52 65 76 20 3f 20 4f 50 5f 4c 65 20   = bRev ? OP_Le 
29653 3a 20 4f 50 5f 47 65 3b 0a 20 20 20 20 20 20 20  : OP_Ge;.       
29654 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
29655 20 20 74 65 73 74 4f 70 20 3d 20 62 52 65 76 20    testOp = bRev 
29656 3f 20 4f 50 5f 4c 74 20 3a 20 4f 50 5f 47 74 3b  ? OP_Lt : OP_Gt;
29657 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
29658 20 20 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70     disableTerm(p
29659 4c 65 76 65 6c 2c 20 70 45 6e 64 29 3b 0a 20 20  Level, pEnd);.  
2965a 20 20 20 20 7d 0a 20 20 20 20 20 20 73 74 61 72      }.      star
2965b 74 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43  t = sqlite3VdbeC
2965c 75 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 20  urrentAddr(v);. 
2965d 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 6f 70 20       pLevel->op 
2965e 3d 20 62 52 65 76 20 3f 20 4f 50 5f 50 72 65 76  = bRev ? OP_Prev
2965f 20 3a 20 4f 50 5f 4e 65 78 74 3b 0a 20 20 20 20   : OP_Next;.    
29660 20 20 70 4c 65 76 65 6c 2d 3e 70 31 20 3d 20 69    pLevel->p1 = i
29661 43 75 72 3b 0a 20 20 20 20 20 20 70 4c 65 76 65  Cur;.      pLeve
29662 6c 2d 3e 70 32 20 3d 20 73 74 61 72 74 3b 0a 20  l->p2 = start;. 
29663 20 20 20 20 20 69 66 28 20 74 65 73 74 4f 70 21       if( testOp!
29664 3d 4f 50 5f 4e 6f 6f 70 20 29 7b 0a 20 20 20 20  =OP_Noop ){.    
29665 20 20 20 20 69 6e 74 20 72 31 20 3d 20 73 71 6c      int r1 = sql
29666 69 74 65 33 47 65 74 54 65 6d 70 52 65 67 28 70  ite3GetTempReg(p
29667 50 61 72 73 65 29 3b 0a 20 20 20 20 20 20 20 20  Parse);.        
29668 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70  sqlite3VdbeAddOp
29669 32 28 76 2c 20 4f 50 5f 52 6f 77 69 64 2c 20 69  2(v, OP_Rowid, i
2966a 43 75 72 2c 20 72 31 29 3b 0a 20 20 20 20 20 20  Cur, r1);.      
2966b 20 20 2f 2a 20 73 71 6c 69 74 65 33 56 64 62 65    /* sqlite3Vdbe
2966c 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 53 43 6f  AddOp2(v, OP_SCo
2966d 70 79 2c 20 70 4c 65 76 65 6c 2d 3e 69 4d 65 6d  py, pLevel->iMem
2966e 2c 20 30 29 3b 20 2a 2f 0a 20 20 20 20 20 20 20  , 0); */.       
2966f 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
29670 70 33 28 76 2c 20 74 65 73 74 4f 70 2c 20 70 4c  p3(v, testOp, pL
29671 65 76 65 6c 2d 3e 69 4d 65 6d 2c 20 62 72 6b 2c  evel->iMem, brk,
29672 20 72 31 29 3b 0a 20 20 20 20 20 20 20 20 73 71   r1);.        sq
29673 6c 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50  lite3VdbeChangeP
29674 35 28 76 2c 20 53 51 4c 49 54 45 5f 41 46 46 5f  5(v, SQLITE_AFF_
29675 4e 55 4d 45 52 49 43 20 7c 20 53 51 4c 49 54 45  NUMERIC | SQLITE
29676 5f 4a 55 4d 50 49 46 4e 55 4c 4c 29 3b 0a 20 20  _JUMPIFNULL);.  
29677 20 20 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c        sqlite3Rel
29678 65 61 73 65 54 65 6d 70 52 65 67 28 70 50 61 72  easeTempReg(pPar
29679 73 65 2c 20 72 31 29 3b 0a 20 20 20 20 20 20 7d  se, r1);.      }
2967a 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 70  .    }else if( p
2967b 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28  Level->flags & (
2967c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f 52 41 4e  WHERE_COLUMN_RAN
2967d 47 45 7c 57 48 45 52 45 5f 43 4f 4c 55 4d 4e 5f  GE|WHERE_COLUMN_
2967e 45 51 29 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  EQ) ){.      /* 
2967f 43 61 73 65 20 33 3a 20 41 20 73 63 61 6e 20 75  Case 3: A scan u
29680 73 69 6e 67 20 61 6e 20 69 6e 64 65 78 2e 0a 20  sing an index.. 
29681 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a       **.      **
29682 20 20 20 20 20 20 20 20 20 54 68 65 20 57 48 45           The WHE
29683 52 45 20 63 6c 61 75 73 65 20 6d 61 79 20 63 6f  RE clause may co
29684 6e 74 61 69 6e 20 7a 65 72 6f 20 6f 72 20 6d 6f  ntain zero or mo
29685 72 65 20 65 71 75 61 6c 69 74 79 20 0a 20 20 20  re equality .   
29686 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 74 65     **         te
29687 72 6d 73 20 28 22 3d 3d 22 20 6f 72 20 22 49 4e  rms ("==" or "IN
29688 22 20 6f 70 65 72 61 74 6f 72 73 29 20 74 68 61  " operators) tha
29689 74 20 72 65 66 65 72 20 74 6f 20 74 68 65 20 4e  t refer to the N
2968a 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
2968b 20 20 6c 65 66 74 2d 6d 6f 73 74 20 63 6f 6c 75    left-most colu
2968c 6d 6e 73 20 6f 66 20 74 68 65 20 69 6e 64 65 78  mns of the index
2968d 2e 20 49 74 20 6d 61 79 20 61 6c 73 6f 20 63 6f  . It may also co
2968e 6e 74 61 69 6e 0a 20 20 20 20 20 20 2a 2a 20 20  ntain.      **  
2968f 20 20 20 20 20 20 20 69 6e 65 71 75 61 6c 69 74         inequalit
29690 79 20 63 6f 6e 73 74 72 61 69 6e 74 73 20 28 3e  y constraints (>
29691 2c 20 3c 2c 20 3e 3d 20 6f 72 20 3c 3d 29 20 6f  , <, >= or <=) o
29692 6e 20 74 68 65 20 69 6e 64 65 78 65 64 0a 20 20  n the indexed.  
29693 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63      **         c
29694 6f 6c 75 6d 6e 20 74 68 61 74 20 69 6d 6d 65 64  olumn that immed
29695 69 61 74 65 6c 79 20 66 6f 6c 6c 6f 77 73 20 74  iately follows t
29696 68 65 20 4e 20 65 71 75 61 6c 69 74 69 65 73 2e  he N equalities.
29697 20 4f 6e 6c 79 20 0a 20 20 20 20 20 20 2a 2a 20   Only .      ** 
29698 20 20 20 20 20 20 20 20 74 68 65 20 72 69 67 68          the righ
29699 74 2d 6d 6f 73 74 20 63 6f 6c 75 6d 6e 20 63 61  t-most column ca
2969a 6e 20 62 65 20 61 6e 20 69 6e 65 71 75 61 6c 69  n be an inequali
2969b 74 79 20 2d 20 74 68 65 20 72 65 73 74 20 6d 75  ty - the rest mu
2969c 73 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  st.      **     
2969d 20 20 20 20 75 73 65 20 74 68 65 20 22 3d 3d 22      use the "=="
2969e 20 61 6e 64 20 22 49 4e 22 20 6f 70 65 72 61 74   and "IN" operat
2969f 6f 72 73 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  ors. For example
296a0 2c 20 69 66 20 74 68 65 20 0a 20 20 20 20 20 20  , if the .      
296a1 2a 2a 20 20 20 20 20 20 20 20 20 69 6e 64 65 78  **         index
296a2 20 69 73 20 6f 6e 20 28 78 2c 79 2c 7a 29 2c 20   is on (x,y,z), 
296a3 74 68 65 6e 20 74 68 65 20 66 6f 6c 6c 6f 77 69  then the followi
296a4 6e 67 20 63 6c 61 75 73 65 73 20 61 72 65 20 61  ng clauses are a
296a5 6c 6c 20 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ll .      **    
296a6 20 20 20 20 20 6f 70 74 69 6d 69 7a 65 64 3a 0a       optimized:.
296a7 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
296a8 2a 20 20 20 20 20 20 20 20 20 20 20 20 78 3d 35  *            x=5
296a9 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20  .      **       
296aa 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3d 31       x=5 AND y=1
296ab 30 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20  0.      **      
296ac 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79 3c        x=5 AND y<
296ad 31 30 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  10.      **     
296ae 20 20 20 20 20 20 20 78 3d 35 20 41 4e 44 20 79         x=5 AND y
296af 3e 35 20 41 4e 44 20 79 3c 31 30 0a 20 20 20 20  >5 AND y<10.    
296b0 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
296b1 78 3d 35 20 41 4e 44 20 79 3d 35 20 41 4e 44 20  x=5 AND y=5 AND 
296b2 7a 3c 3d 31 30 0a 20 20 20 20 20 20 2a 2a 0a 20  z<=10.      **. 
296b3 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20       **         
296b4 54 68 65 20 7a 3c 31 30 20 74 65 72 6d 20 6f 66  The z<10 term of
296b5 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63   the following c
296b6 61 6e 6e 6f 74 20 62 65 20 75 73 65 64 2c 20 6f  annot be used, o
296b7 6e 6c 79 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  nly.      **    
296b8 20 20 20 20 20 74 68 65 20 78 3d 35 20 74 65 72       the x=5 ter
296b9 6d 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  m:.      **.    
296ba 20 20 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20    **            
296bb 78 3d 35 20 41 4e 44 20 7a 3c 31 30 0a 20 20 20  x=5 AND z<10.   
296bc 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20     **.      **  
296bd 20 20 20 20 20 20 20 4e 20 6d 61 79 20 62 65 20         N may be 
296be 7a 65 72 6f 20 69 66 20 74 68 65 72 65 20 61 72  zero if there ar
296bf 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  e inequality con
296c0 73 74 72 61 69 6e 74 73 2e 0a 20 20 20 20 20 20  straints..      
296c1 2a 2a 20 20 20 20 20 20 20 20 20 49 66 20 74 68  **         If th
296c2 65 72 65 20 61 72 65 20 6e 6f 20 69 6e 65 71 75  ere are no inequ
296c3 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74  ality constraint
296c4 73 2c 20 74 68 65 6e 20 4e 20 69 73 20 61 74 0a  s, then N is at.
296c5 20 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20        **        
296c6 20 6c 65 61 73 74 20 6f 6e 65 2e 0a 20 20 20 20   least one..    
296c7 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 20    **.      **   
296c8 20 20 20 20 20 20 54 68 69 73 20 63 61 73 65 20        This case 
296c9 69 73 20 61 6c 73 6f 20 75 73 65 64 20 77 68 65  is also used whe
296ca 6e 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 57  n there are no W
296cb 48 45 52 45 20 63 6c 61 75 73 65 0a 20 20 20 20  HERE clause.    
296cc 20 20 2a 2a 20 20 20 20 20 20 20 20 20 63 6f 6e    **         con
296cd 73 74 72 61 69 6e 74 73 20 62 75 74 20 61 6e 20  straints but an 
296ce 69 6e 64 65 78 20 69 73 20 73 65 6c 65 63 74 65  index is selecte
296cf 64 20 61 6e 79 77 61 79 2c 20 69 6e 20 6f 72 64  d anyway, in ord
296d0 65 72 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 20  er.      **     
296d1 20 20 20 20 74 6f 20 66 6f 72 63 65 20 74 68 65      to force the
296d2 20 6f 75 74 70 75 74 20 6f 72 64 65 72 20 74 6f   output order to
296d3 20 63 6f 6e 66 6f 72 6d 20 74 6f 20 61 6e 20 4f   conform to an O
296d4 52 44 45 52 20 42 59 2e 0a 20 20 20 20 20 20 2a  RDER BY..      *
296d5 2f 20 20 0a 20 20 20 20 20 20 69 6e 74 20 61 53  /  .      int aS
296d6 74 61 72 74 4f 70 5b 5d 20 3d 20 7b 0a 20 20 20  tartOp[] = {.   
296d7 20 20 20 20 20 30 2c 0a 20 20 20 20 20 20 20 20       0,.        
296d8 30 2c 0a 20 20 20 20 20 20 20 20 4f 50 5f 52 65  0,.        OP_Re
296d9 77 69 6e 64 2c 20 20 20 20 20 20 20 20 20 20 20  wind,           
296da 2f 2a 20 32 3a 20 28 21 73 74 61 72 74 5f 63 6f  /* 2: (!start_co
296db 6e 73 74 72 61 69 6e 74 73 20 26 26 20 73 74 61  nstraints && sta
296dc 72 74 45 71 20 26 26 20 20 21 62 52 65 76 29 20  rtEq &&  !bRev) 
296dd 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f 4c 61  */.        OP_La
296de 73 74 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  st,             
296df 2f 2a 20 33 3a 20 28 21 73 74 61 72 74 5f 63 6f  /* 3: (!start_co
296e0 6e 73 74 72 61 69 6e 74 73 20 26 26 20 73 74 61  nstraints && sta
296e1 72 74 45 71 20 26 26 20 20 20 62 52 65 76 29 20  rtEq &&   bRev) 
296e2 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f 4d 6f  */.        OP_Mo
296e3 76 65 47 74 2c 20 20 20 20 20 20 20 20 20 20 20  veGt,           
296e4 2f 2a 20 34 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 4: (start_con
296e5 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74  straints  && !st
296e6 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29 20  artEq && !bRev) 
296e7 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f 4d 6f  */.        OP_Mo
296e8 76 65 4c 74 2c 20 20 20 20 20 20 20 20 20 20 20  veLt,           
296e9 2f 2a 20 35 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 5: (start_con
296ea 73 74 72 61 69 6e 74 73 20 20 26 26 20 21 73 74  straints  && !st
296eb 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20  artEq &&  bRev) 
296ec 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f 4d 6f  */.        OP_Mo
296ed 76 65 47 65 2c 20 20 20 20 20 20 20 20 20 20 20  veGe,           
296ee 2f 2a 20 36 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 6: (start_con
296ef 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73 74  straints  &&  st
296f0 61 72 74 45 71 20 26 26 20 21 62 52 65 76 29 20  artEq && !bRev) 
296f1 2a 2f 0a 20 20 20 20 20 20 20 20 4f 50 5f 4d 6f  */.        OP_Mo
296f2 76 65 4c 65 20 20 20 20 20 20 20 20 20 20 20 20  veLe            
296f3 2f 2a 20 37 3a 20 28 73 74 61 72 74 5f 63 6f 6e  /* 7: (start_con
296f4 73 74 72 61 69 6e 74 73 20 20 26 26 20 20 73 74  straints  &&  st
296f5 61 72 74 45 71 20 26 26 20 20 62 52 65 76 29 20  artEq &&  bRev) 
296f6 2a 2f 0a 20 20 20 20 20 20 7d 3b 0a 20 20 20 20  */.      };.    
296f7 20 20 69 6e 74 20 61 45 6e 64 4f 70 5b 5d 20 3d    int aEndOp[] =
296f8 20 7b 0a 20 20 20 20 20 20 20 20 4f 50 5f 4e 6f   {.        OP_No
296f9 6f 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  op,             
296fa 2f 2a 20 30 3a 20 28 21 65 6e 64 5f 63 6f 6e 73  /* 0: (!end_cons
296fb 74 72 61 69 6e 74 73 29 20 2a 2f 0a 20 20 20 20  traints) */.    
296fc 20 20 20 20 4f 50 5f 49 64 78 47 45 2c 20 20 20      OP_IdxGE,   
296fd 20 20 20 20 20 20 20 20 20 2f 2a 20 31 3a 20 28           /* 1: (
296fe 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20  end_constraints 
296ff 26 26 20 21 62 52 65 76 29 20 2a 2f 0a 20 20 20  && !bRev) */.   
29700 20 20 20 20 20 4f 50 5f 49 64 78 4c 54 20 20 20       OP_IdxLT   
29701 20 20 20 20 20 20 20 20 20 20 2f 2a 20 32 3a 20            /* 2: 
29702 28 65 6e 64 5f 63 6f 6e 73 74 72 61 69 6e 74 73  (end_constraints
29703 20 26 26 20 62 52 65 76 29 20 2a 2f 0a 20 20 20   && bRev) */.   
29704 20 20 20 7d 3b 0a 20 20 20 20 20 20 69 6e 74 20     };.      int 
29705 6e 45 71 20 3d 20 70 4c 65 76 65 6c 2d 3e 6e 45  nEq = pLevel->nE
29706 71 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 4d  q;.      int isM
29707 69 6e 51 75 65 72 79 20 3d 20 30 3b 20 20 20 20  inQuery = 0;    
29708 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73        /* If this
29709 20 69 73 20 61 6e 20 6f 70 74 69 6d 69 7a 65 64   is an optimized
2970a 20 53 45 4c 45 43 54 20 6d 69 6e 28 78 29 2e 2e   SELECT min(x)..
2970b 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72 65   */.      int re
2970c 67 42 61 73 65 3b 20 20 20 20 20 20 20 20 20 20  gBase;          
2970d 20 20 20 20 20 20 20 2f 2a 20 42 61 73 65 20 72         /* Base r
2970e 65 67 69 73 74 65 72 20 68 6f 6c 64 69 6e 67 20  egister holding 
2970f 63 6f 6e 73 74 72 61 69 6e 74 20 76 61 6c 75 65  constraint value
29710 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 72  s */.      int r
29711 31 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1;              
29712 20 20 20 20 20 20 20 20 2f 2a 20 54 65 6d 70 20          /* Temp 
29713 72 65 67 69 73 74 65 72 20 2a 2f 0a 20 20 20 20  register */.    
29714 20 20 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61    WhereTerm *pRa
29715 6e 67 65 53 74 61 72 74 20 3d 20 30 3b 20 20 2f  ngeStart = 0;  /
29716 2a 20 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e  * Inequality con
29717 73 74 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65  straint at range
29718 20 73 74 61 72 74 20 2a 2f 0a 20 20 20 20 20 20   start */.      
29719 57 68 65 72 65 54 65 72 6d 20 2a 70 52 61 6e 67  WhereTerm *pRang
2971a 65 45 6e 64 20 3d 20 30 3b 20 20 20 20 2f 2a 20  eEnd = 0;    /* 
2971b 49 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74  Inequality const
2971c 72 61 69 6e 74 20 61 74 20 72 61 6e 67 65 20 65  raint at range e
2971d 6e 64 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20  nd */.      int 
2971e 73 74 61 72 74 45 71 3b 20 20 20 20 20 20 20 20  startEq;        
2971f 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
29720 20 69 66 20 72 61 6e 67 65 20 73 74 61 72 74 20   if range start 
29721 75 73 65 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c  uses ==, >= or <
29722 3d 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 65  = */.      int e
29723 6e 64 45 71 3b 20 20 20 20 20 20 20 20 20 20 20  ndEq;           
29724 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
29725 69 66 20 72 61 6e 67 65 20 65 6e 64 20 75 73 65  if range end use
29726 73 20 3d 3d 2c 20 3e 3d 20 6f 72 20 3c 3d 20 2a  s ==, >= or <= *
29727 2f 0a 20 20 20 20 20 20 69 6e 74 20 73 74 61 72  /.      int star
29728 74 5f 63 6f 6e 73 74 72 61 69 6e 74 73 3b 20 20  t_constraints;  
29729 20 20 20 20 20 2f 2a 20 53 74 61 72 74 20 6f 66       /* Start of
2972a 20 72 61 6e 67 65 20 69 73 20 63 6f 6e 73 74 72   range is constr
2972b 61 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20 69  ained */.      i
2972c 6e 74 20 6b 20 3d 20 70 49 64 78 2d 3e 61 69 43  nt k = pIdx->aiC
2972d 6f 6c 75 6d 6e 5b 6e 45 71 5d 3b 20 2f 2a 20 43  olumn[nEq]; /* C
2972e 6f 6c 75 6d 6e 20 66 6f 72 20 69 6e 65 71 75 61  olumn for inequa
2972f 6c 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 73  lity constraints
29730 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6e 43   */.      int nC
29731 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20 20 20 20  onstraint;      
29732 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
29733 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 74 20 74   of constraint t
29734 65 72 6d 73 20 2a 2f 0a 20 20 20 20 20 20 69 6e  erms */.      in
29735 74 20 6f 70 3b 0a 0a 20 20 20 20 20 20 2f 2a 20  t op;..      /* 
29736 47 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 6f  Generate code to
29737 20 65 76 61 6c 75 61 74 65 20 61 6c 6c 20 63 6f   evaluate all co
29738 6e 73 74 72 61 69 6e 74 20 74 65 72 6d 73 20 75  nstraint terms u
29739 73 69 6e 67 20 3d 3d 20 6f 72 20 49 4e 0a 20 20  sing == or IN.  
2973a 20 20 20 20 2a 2a 20 61 6e 64 20 73 74 6f 72 65      ** and store
2973b 20 74 68 65 20 76 61 6c 75 65 73 20 6f 66 20 74   the values of t
2973c 68 6f 73 65 20 74 65 72 6d 73 20 69 6e 20 61 6e  hose terms in an
2973d 20 61 72 72 61 79 20 6f 66 20 72 65 67 69 73 74   array of regist
2973e 65 72 73 0a 20 20 20 20 20 20 2a 2a 20 73 74 61  ers.      ** sta
2973f 72 74 69 6e 67 20 61 74 20 72 65 67 42 61 73 65  rting at regBase
29740 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20  ..      */.     
29741 20 72 65 67 42 61 73 65 20 3d 20 63 6f 64 65 41   regBase = codeA
29742 6c 6c 45 71 75 61 6c 69 74 79 54 65 72 6d 73 28  llEqualityTerms(
29743 70 50 61 72 73 65 2c 20 70 4c 65 76 65 6c 2c 20  pParse, pLevel, 
29744 26 77 63 2c 20 6e 6f 74 52 65 61 64 79 2c 20 32  &wc, notReady, 2
29745 29 3b 0a 20 20 20 20 20 20 6e 78 74 20 3d 20 70  );.      nxt = p
29746 4c 65 76 65 6c 2d 3e 6e 78 74 3b 0a 0a 20 20 20  Level->nxt;..   
29747 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20 6c 6f     /* If this lo
29748 6f 70 20 73 61 74 69 73 66 69 65 73 20 61 20 73  op satisfies a s
29749 6f 72 74 20 6f 72 64 65 72 20 28 70 4f 72 64 65  ort order (pOrde
2974a 72 42 79 29 20 72 65 71 75 65 73 74 20 74 68 61  rBy) request tha
2974b 74 20 0a 20 20 20 20 20 20 2a 2a 20 77 61 73 20  t .      ** was 
2974c 70 61 73 73 65 64 20 74 6f 20 74 68 69 73 20 66  passed to this f
2974d 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6d 70 6c 65  unction to imple
2974e 6d 65 6e 74 20 61 20 22 53 45 4c 45 43 54 20 6d  ment a "SELECT m
2974f 69 6e 28 78 29 20 2e 2e 2e 22 20 0a 20 20 20 20  in(x) ..." .    
29750 20 20 2a 2a 20 71 75 65 72 79 2c 20 74 68 65 6e    ** query, then
29751 20 74 68 65 20 63 61 6c 6c 65 72 20 77 69 6c 6c   the caller will
29752 20 6f 6e 6c 79 20 61 6c 6c 6f 77 20 74 68 65 20   only allow the 
29753 6c 6f 6f 70 20 74 6f 20 72 75 6e 20 66 6f 72 0a  loop to run for.
29754 20 20 20 20 20 20 2a 2a 20 61 20 73 69 6e 67 6c        ** a singl
29755 65 20 69 74 65 72 61 74 69 6f 6e 2e 20 54 68 69  e iteration. Thi
29756 73 20 6d 65 61 6e 73 20 74 68 61 74 20 74 68 65  s means that the
29757 20 66 69 72 73 74 20 72 6f 77 20 72 65 74 75 72   first row retur
29758 6e 65 64 0a 20 20 20 20 20 20 2a 2a 20 73 68 6f  ned.      ** sho
29759 75 6c 64 20 6e 6f 74 20 68 61 76 65 20 61 20 4e  uld not have a N
2975a 55 4c 4c 20 76 61 6c 75 65 20 73 74 6f 72 65 64  ULL value stored
2975b 20 69 6e 20 27 78 27 2e 20 49 66 20 63 6f 6c 75   in 'x'. If colu
2975c 6d 6e 20 27 78 27 20 69 73 0a 20 20 20 20 20 20  mn 'x' is.      
2975d 2a 2a 20 74 68 65 20 66 69 72 73 74 20 6f 6e 65  ** the first one
2975e 20 61 66 74 65 72 20 74 68 65 20 6e 45 71 20 65   after the nEq e
2975f 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61 69  quality constrai
29760 6e 74 73 20 69 6e 20 74 68 65 20 69 6e 64 65 78  nts in the index
29761 2c 0a 20 20 20 20 20 20 2a 2a 20 74 68 69 73 20  ,.      ** this 
29762 72 65 71 75 69 72 65 73 20 73 6f 6d 65 20 73 70  requires some sp
29763 65 63 69 61 6c 20 68 61 6e 64 6c 69 6e 67 2e 0a  ecial handling..
29764 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69        */.      i
29765 66 28 20 28 77 66 6c 61 67 73 26 57 48 45 52 45  f( (wflags&WHERE
29766 5f 4f 52 44 45 52 42 59 5f 4d 49 4e 29 21 3d 30  _ORDERBY_MIN)!=0
29767 0a 20 20 20 20 20 20 20 26 26 20 28 70 4c 65 76  .       && (pLev
29768 65 6c 2d 3e 66 6c 61 67 73 26 57 48 45 52 45 5f  el->flags&WHERE_
29769 4f 52 44 45 52 42 59 29 0a 20 20 20 20 20 20 20  ORDERBY).       
2976a 26 26 20 28 70 49 64 78 2d 3e 6e 43 6f 6c 75 6d  && (pIdx->nColum
2976b 6e 3e 6e 45 71 29 0a 20 20 20 20 20 20 29 7b 0a  n>nEq).      ){.
2976c 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
2976d 70 4f 72 64 65 72 42 79 2d 3e 6e 45 78 70 72 3d  pOrderBy->nExpr=
2976e 3d 31 20 29 3b 0a 20 20 20 20 20 20 20 20 61 73  =1 );.        as
2976f 73 65 72 74 28 20 70 4f 72 64 65 72 42 79 2d 3e  sert( pOrderBy->
29770 61 5b 30 5d 2e 70 45 78 70 72 2d 3e 69 43 6f 6c  a[0].pExpr->iCol
29771 75 6d 6e 3d 3d 70 49 64 78 2d 3e 61 69 43 6f 6c  umn==pIdx->aiCol
29772 75 6d 6e 5b 6e 45 71 5d 20 29 3b 0a 20 20 20 20  umn[nEq] );.    
29773 20 20 20 20 69 73 4d 69 6e 51 75 65 72 79 20 3d      isMinQuery =
29774 20 31 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20   1;.      }..   
29775 20 20 20 2f 2a 20 46 69 6e 64 20 61 6e 79 20 69     /* Find any i
29776 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72  nequality constr
29777 61 69 6e 74 20 74 65 72 6d 73 20 66 6f 72 20 74  aint terms for t
29778 68 65 20 73 74 61 72 74 20 61 6e 64 20 65 6e 64  he start and end
29779 20 0a 20 20 20 20 20 20 2a 2a 20 6f 66 20 74 68   .      ** of th
2977a 65 20 72 61 6e 67 65 2e 20 0a 20 20 20 20 20 20  e range. .      
2977b 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65  */.      if( pLe
2977c 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45  vel->flags & WHE
2977d 52 45 5f 54 4f 50 5f 4c 49 4d 49 54 20 29 7b 0a  RE_TOP_LIMIT ){.
2977e 20 20 20 20 20 20 20 20 70 52 61 6e 67 65 45 6e          pRangeEn
2977f 64 20 3d 20 66 69 6e 64 54 65 72 6d 28 26 77 63  d = findTerm(&wc
29780 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74 52 65  , iCur, k, notRe
29781 61 64 79 2c 20 28 57 4f 5f 4c 54 7c 57 4f 5f 4c  ady, (WO_LT|WO_L
29782 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20 20 20  E), pIdx);.     
29783 20 7d 0a 20 20 20 20 20 20 69 66 28 20 70 4c 65   }.      if( pLe
29784 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 57 48 45  vel->flags & WHE
29785 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20 29 7b 0a  RE_BTM_LIMIT ){.
29786 20 20 20 20 20 20 20 20 70 52 61 6e 67 65 53 74          pRangeSt
29787 61 72 74 20 3d 20 66 69 6e 64 54 65 72 6d 28 26  art = findTerm(&
29788 77 63 2c 20 69 43 75 72 2c 20 6b 2c 20 6e 6f 74  wc, iCur, k, not
29789 52 65 61 64 79 2c 20 28 57 4f 5f 47 54 7c 57 4f  Ready, (WO_GT|WO
2978a 5f 47 45 29 2c 20 70 49 64 78 29 3b 0a 20 20 20  _GE), pIdx);.   
2978b 20 20 20 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 49     }..      /* I
2978c 66 20 77 65 20 61 72 65 20 64 6f 69 6e 67 20 61  f we are doing a
2978d 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 20 73   reverse order s
2978e 63 61 6e 20 6f 6e 20 61 6e 20 61 73 63 65 6e 64  can on an ascend
2978f 69 6e 67 20 69 6e 64 65 78 2c 20 6f 72 0a 20 20  ing index, or.  
29790 20 20 20 20 2a 2a 20 61 20 66 6f 72 77 61 72 64      ** a forward
29791 20 6f 72 64 65 72 20 73 63 61 6e 20 6f 6e 20 61   order scan on a
29792 20 64 65 73 63 65 6e 64 69 6e 67 20 69 6e 64 65   descending inde
29793 78 2c 20 69 6e 74 65 72 63 68 61 6e 67 65 20 74  x, interchange t
29794 68 65 20 0a 20 20 20 20 20 20 2a 2a 20 73 74 61  he .      ** sta
29795 72 74 20 61 6e 64 20 65 6e 64 20 74 65 72 6d 73  rt and end terms
29796 20 28 70 52 61 6e 67 65 53 74 61 72 74 20 61 6e   (pRangeStart an
29797 64 20 70 52 61 6e 67 65 45 6e 64 29 2e 0a 20 20  d pRangeEnd)..  
29798 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 69 66 28      */.      if(
29799 20 62 52 65 76 3d 3d 28 70 49 64 78 2d 3e 61 53   bRev==(pIdx->aS
2979a 6f 72 74 4f 72 64 65 72 5b 6e 45 71 5d 3d 3d 53  ortOrder[nEq]==S
2979b 51 4c 49 54 45 5f 53 4f 5f 41 53 43 29 20 29 7b  QLITE_SO_ASC) ){
2979c 0a 20 20 20 20 20 20 20 20 53 57 41 50 28 57 68  .        SWAP(Wh
2979d 65 72 65 54 65 72 6d 20 2a 2c 20 70 52 61 6e 67  ereTerm *, pRang
2979e 65 45 6e 64 2c 20 70 52 61 6e 67 65 53 74 61 72  eEnd, pRangeStar
2979f 74 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  t);.      }..   
297a0 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52 61     testcase( pRa
297a1 6e 67 65 53 74 61 72 74 20 26 26 20 70 52 61 6e  ngeStart && pRan
297a2 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61 74  geStart->eOperat
297a3 6f 72 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20  or & WO_LE );.  
297a4 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70 52      testcase( pR
297a5 61 6e 67 65 53 74 61 72 74 20 26 26 20 70 52 61  angeStart && pRa
297a6 6e 67 65 53 74 61 72 74 2d 3e 65 4f 70 65 72 61  ngeStart->eOpera
297a7 74 6f 72 20 26 20 57 4f 5f 47 45 20 29 3b 0a 20  tor & WO_GE );. 
297a8 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20 70       testcase( p
297a9 52 61 6e 67 65 45 6e 64 20 26 26 20 70 52 61 6e  RangeEnd && pRan
297aa 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72  geEnd->eOperator
297ab 20 26 20 57 4f 5f 4c 45 20 29 3b 0a 20 20 20 20   & WO_LE );.    
297ac 20 20 74 65 73 74 63 61 73 65 28 20 70 52 61 6e    testcase( pRan
297ad 67 65 45 6e 64 20 26 26 20 70 52 61 6e 67 65 45  geEnd && pRangeE
297ae 6e 64 2d 3e 65 4f 70 65 72 61 74 6f 72 20 26 20  nd->eOperator & 
297af 57 4f 5f 47 45 20 29 3b 0a 20 20 20 20 20 20 73  WO_GE );.      s
297b0 74 61 72 74 45 71 20 3d 20 21 70 52 61 6e 67 65  tartEq = !pRange
297b1 53 74 61 72 74 20 7c 7c 20 70 52 61 6e 67 65 53  Start || pRangeS
297b2 74 61 72 74 2d 3e 65 4f 70 65 72 61 74 6f 72 20  tart->eOperator 
297b3 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f 47 45 29 3b  & (WO_LE|WO_GE);
297b4 0a 20 20 20 20 20 20 65 6e 64 45 71 20 3d 20 20  .      endEq =  
297b5 20 21 70 52 61 6e 67 65 45 6e 64 20 7c 7c 20 70   !pRangeEnd || p
297b6 52 61 6e 67 65 45 6e 64 2d 3e 65 4f 70 65 72 61  RangeEnd->eOpera
297b7 74 6f 72 20 26 20 28 57 4f 5f 4c 45 7c 57 4f 5f  tor & (WO_LE|WO_
297b8 47 45 29 3b 0a 20 20 20 20 20 20 73 74 61 72 74  GE);.      start
297b9 5f 63 6f 6e 73 74 72 61 69 6e 74 73 20 3d 20 70  _constraints = p
297ba 52 61 6e 67 65 53 74 61 72 74 20 7c 7c 20 6e 45  RangeStart || nE
297bb 71 3e 30 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 53  q>0;..      /* S
297bc 65 65 6b 20 74 68 65 20 69 6e 64 65 78 20 63 75  eek the index cu
297bd 72 73 6f 72 20 74 6f 20 74 68 65 20 73 74 61 72  rsor to the star
297be 74 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20  t of the range. 
297bf 2a 2f 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72  */.      nConstr
297c0 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20 20  aint = nEq;.    
297c1 20 20 69 66 28 20 70 52 61 6e 67 65 53 74 61 72    if( pRangeStar
297c2 74 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e 74  t ){.        int
297c3 20 64 63 63 20 3d 20 70 50 61 72 73 65 2d 3e 64   dcc = pParse->d
297c4 69 73 61 62 6c 65 43 6f 6c 43 61 63 68 65 3b 0a  isableColCache;.
297c5 20 20 20 20 20 20 20 20 69 66 28 20 70 52 61 6e          if( pRan
297c6 67 65 45 6e 64 20 29 7b 0a 20 20 20 20 20 20 20  geEnd ){.       
297c7 20 20 20 70 50 61 72 73 65 2d 3e 64 69 73 61 62     pParse->disab
297c8 6c 65 43 6f 6c 43 61 63 68 65 2b 2b 3b 0a 20 20  leColCache++;.  
297c9 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
297ca 73 71 6c 69 74 65 33 45 78 70 72 43 6f 64 65 28  sqlite3ExprCode(
297cb 70 50 61 72 73 65 2c 20 70 52 61 6e 67 65 53 74  pParse, pRangeSt
297cc 61 72 74 2d 3e 70 45 78 70 72 2d 3e 70 52 69 67  art->pExpr->pRig
297cd 68 74 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29  ht, regBase+nEq)
297ce 3b 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65  ;.        pParse
297cf 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63 68  ->disableColCach
297d0 65 20 3d 20 64 63 63 3b 0a 20 20 20 20 20 20 20  e = dcc;.       
297d1 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
297d2 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c 6c 2c  p2(v, OP_IsNull,
297d3 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20 6e 78   regBase+nEq, nx
297d4 74 29 3b 0a 20 20 20 20 20 20 20 20 6e 43 6f 6e  t);.        nCon
297d5 73 74 72 61 69 6e 74 2b 2b 3b 0a 20 20 20 20 20  straint++;.     
297d6 20 7d 65 6c 73 65 20 69 66 28 20 69 73 4d 69 6e   }else if( isMin
297d7 51 75 65 72 79 20 29 7b 0a 20 20 20 20 20 20 20  Query ){.       
297d8 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f   sqlite3VdbeAddO
297d9 70 32 28 76 2c 20 4f 50 5f 4e 75 6c 6c 2c 20 30  p2(v, OP_Null, 0
297da 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 29 3b 0a  , regBase+nEq);.
297db 20 20 20 20 20 20 20 20 6e 43 6f 6e 73 74 72 61          nConstra
297dc 69 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 20 20 73  int++;.        s
297dd 74 61 72 74 45 71 20 3d 20 30 3b 0a 20 20 20 20  tartEq = 0;.    
297de 20 20 20 20 73 74 61 72 74 5f 63 6f 6e 73 74 72      start_constr
297df 61 69 6e 74 73 20 3d 20 31 3b 0a 20 20 20 20 20  aints = 1;.     
297e0 20 7d 0a 20 20 20 20 20 20 63 6f 64 65 41 70 70   }.      codeApp
297e1 6c 79 41 66 66 69 6e 69 74 79 28 70 50 61 72 73  lyAffinity(pPars
297e2 65 2c 20 72 65 67 42 61 73 65 2c 20 6e 43 6f 6e  e, regBase, nCon
297e3 73 74 72 61 69 6e 74 2c 20 70 49 64 78 29 3b 0a  straint, pIdx);.
297e4 20 20 20 20 20 20 6f 70 20 3d 20 61 53 74 61 72        op = aStar
297e5 74 4f 70 5b 28 73 74 61 72 74 5f 63 6f 6e 73 74  tOp[(start_const
297e6 72 61 69 6e 74 73 3c 3c 32 29 20 2b 20 28 73 74  raints<<2) + (st
297e7 61 72 74 45 71 3c 3c 31 29 20 2b 20 62 52 65 76  artEq<<1) + bRev
297e8 5d 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ];.      assert(
297e9 20 6f 70 21 3d 30 20 29 3b 0a 20 20 20 20 20 20   op!=0 );.      
297ea 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
297eb 5f 52 65 77 69 6e 64 20 29 3b 0a 20 20 20 20 20  _Rewind );.     
297ec 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
297ed 50 5f 4c 61 73 74 20 29 3b 0a 20 20 20 20 20 20  P_Last );.      
297ee 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50  testcase( op==OP
297ef 5f 4d 6f 76 65 47 74 20 29 3b 0a 20 20 20 20 20  _MoveGt );.     
297f0 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d 4f   testcase( op==O
297f1 50 5f 4d 6f 76 65 47 65 20 29 3b 0a 20 20 20 20  P_MoveGe );.    
297f2 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d 3d    testcase( op==
297f3 4f 50 5f 4d 6f 76 65 4c 65 20 29 3b 0a 20 20 20  OP_MoveLe );.   
297f4 20 20 20 74 65 73 74 63 61 73 65 28 20 6f 70 3d     testcase( op=
297f5 3d 4f 50 5f 4d 6f 76 65 4c 74 20 29 3b 0a 20 20  =OP_MoveLt );.  
297f6 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
297f7 64 64 4f 70 34 28 76 2c 20 6f 70 2c 20 69 49 64  ddOp4(v, op, iId
297f8 78 43 75 72 2c 20 6e 78 74 2c 20 72 65 67 42 61  xCur, nxt, regBa
297f9 73 65 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20  se, .           
297fa 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
297fb 49 54 45 5f 49 4e 54 5f 54 4f 5f 50 54 52 28 6e  ITE_INT_TO_PTR(n
297fc 43 6f 6e 73 74 72 61 69 6e 74 29 2c 20 50 34 5f  Constraint), P4_
297fd 49 4e 54 33 32 29 3b 0a 0a 20 20 20 20 20 20 2f  INT32);..      /
297fe 2a 20 4c 6f 61 64 20 74 68 65 20 76 61 6c 75 65  * Load the value
297ff 20 66 6f 72 20 74 68 65 20 69 6e 65 71 75 61 6c   for the inequal
29800 69 74 79 20 63 6f 6e 73 74 72 61 69 6e 74 20 61  ity constraint a
29801 74 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  t the end of the
29802 0a 20 20 20 20 20 20 2a 2a 20 72 61 6e 67 65 20  .      ** range 
29803 28 69 66 20 61 6e 79 29 2e 0a 20 20 20 20 20 20  (if any)..      
29804 2a 2f 0a 20 20 20 20 20 20 6e 43 6f 6e 73 74 72  */.      nConstr
29805 61 69 6e 74 20 3d 20 6e 45 71 3b 0a 20 20 20 20  aint = nEq;.    
29806 20 20 69 66 28 20 70 52 61 6e 67 65 45 6e 64 20    if( pRangeEnd 
29807 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
29808 65 33 45 78 70 72 43 6f 64 65 28 70 50 61 72 73  e3ExprCode(pPars
29809 65 2c 20 70 52 61 6e 67 65 45 6e 64 2d 3e 70 45  e, pRangeEnd->pE
2980a 78 70 72 2d 3e 70 52 69 67 68 74 2c 20 72 65 67  xpr->pRight, reg
2980b 42 61 73 65 2b 6e 45 71 29 3b 0a 20 20 20 20 20  Base+nEq);.     
2980c 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2980d 64 4f 70 32 28 76 2c 20 4f 50 5f 49 73 4e 75 6c  dOp2(v, OP_IsNul
2980e 6c 2c 20 72 65 67 42 61 73 65 2b 6e 45 71 2c 20  l, regBase+nEq, 
2980f 6e 78 74 29 3b 0a 20 20 20 20 20 20 20 20 63 6f  nxt);.        co
29810 64 65 41 70 70 6c 79 41 66 66 69 6e 69 74 79 28  deApplyAffinity(
29811 70 50 61 72 73 65 2c 20 72 65 67 42 61 73 65 2c  pParse, regBase,
29812 20 6e 45 71 2b 31 2c 20 70 49 64 78 29 3b 0a 20   nEq+1, pIdx);. 
29813 20 20 20 20 20 20 20 6e 43 6f 6e 73 74 72 61 69         nConstrai
29814 6e 74 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20  nt++;.      }.. 
29815 20 20 20 20 20 2f 2a 20 54 6f 70 20 6f 66 20 74       /* Top of t
29816 68 65 20 6c 6f 6f 70 20 62 6f 64 79 20 2a 2f 0a  he loop body */.
29817 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32        pLevel->p2
29818 20 3d 20 73 71 6c 69 74 65 33 56 64 62 65 43 75   = sqlite3VdbeCu
29819 72 72 65 6e 74 41 64 64 72 28 76 29 3b 0a 0a 20  rrentAddr(v);.. 
2981a 20 20 20 20 20 2f 2a 20 43 68 65 63 6b 20 69 66       /* Check if
2981b 20 74 68 65 20 69 6e 64 65 78 20 63 75 72 73 6f   the index curso
2981c 72 20 69 73 20 70 61 73 74 20 74 68 65 20 65 6e  r is past the en
2981d 64 20 6f 66 20 74 68 65 20 72 61 6e 67 65 2e 20  d of the range. 
2981e 2a 2f 0a 20 20 20 20 20 20 6f 70 20 3d 20 61 45  */.      op = aE
2981f 6e 64 4f 70 5b 28 70 52 61 6e 67 65 45 6e 64 20  ndOp[(pRangeEnd 
29820 7c 7c 20 6e 45 71 29 20 2a 20 28 31 20 2b 20 62  || nEq) * (1 + b
29821 52 65 76 29 5d 3b 0a 20 20 20 20 20 20 74 65 73  Rev)];.      tes
29822 74 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 4e 6f  tcase( op==OP_No
29823 6f 70 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  op );.      test
29824 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78  case( op==OP_Idx
29825 47 45 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74  GE );.      test
29826 63 61 73 65 28 20 6f 70 3d 3d 4f 50 5f 49 64 78  case( op==OP_Idx
29827 4c 54 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  LT );.      sqli
29828 74 65 33 56 64 62 65 41 64 64 4f 70 34 28 76 2c  te3VdbeAddOp4(v,
29829 20 6f 70 2c 20 69 49 64 78 43 75 72 2c 20 6e 78   op, iIdxCur, nx
2982a 74 2c 20 72 65 67 42 61 73 65 2c 0a 20 20 20 20  t, regBase,.    
2982b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2982c 20 20 20 20 53 51 4c 49 54 45 5f 49 4e 54 5f 54      SQLITE_INT_T
2982d 4f 5f 50 54 52 28 6e 43 6f 6e 73 74 72 61 69 6e  O_PTR(nConstrain
2982e 74 29 2c 20 50 34 5f 49 4e 54 33 32 29 3b 0a 20  t), P4_INT32);. 
2982f 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
29830 43 68 61 6e 67 65 50 35 28 76 2c 20 65 6e 64 45  ChangeP5(v, endE
29831 71 21 3d 62 52 65 76 29 3b 0a 0a 20 20 20 20 20  q!=bRev);..     
29832 20 2f 2a 20 49 66 20 74 68 65 72 65 20 61 72 65   /* If there are
29833 20 69 6e 65 71 75 61 6c 69 74 79 20 63 6f 6e 73   inequality cons
29834 74 72 61 69 6e 74 73 2c 20 63 68 65 63 6b 20 74  traints, check t
29835 68 61 74 20 74 68 65 20 76 61 6c 75 65 0a 20 20  hat the value.  
29836 20 20 20 20 2a 2a 20 6f 66 20 74 68 65 20 74 61      ** of the ta
29837 62 6c 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20  ble column that 
29838 74 68 65 20 69 6e 65 71 75 61 6c 69 74 79 20 63  the inequality c
29839 6f 6e 74 72 61 69 6e 73 20 69 73 20 6e 6f 74 20  ontrains is not 
2983a 4e 55 4c 4c 2e 0a 20 20 20 20 20 20 2a 2a 20 49  NULL..      ** I
2983b 66 20 69 74 20 69 73 2c 20 6a 75 6d 70 20 74 6f  f it is, jump to
2983c 20 74 68 65 20 6e 65 78 74 20 69 74 65 72 61 74   the next iterat
2983d 69 6f 6e 20 6f 66 20 74 68 65 20 6c 6f 6f 70 2e  ion of the loop.
2983e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2983f 72 31 20 3d 20 73 71 6c 69 74 65 33 47 65 74 54  r1 = sqlite3GetT
29840 65 6d 70 52 65 67 28 70 50 61 72 73 65 29 3b 0a  empReg(pParse);.
29841 20 20 20 20 20 20 74 65 73 74 63 61 73 65 28 20        testcase( 
29842 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20  pLevel->flags & 
29843 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54 20  WHERE_BTM_LIMIT 
29844 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61 73  );.      testcas
29845 65 28 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  e( pLevel->flags
29846 20 26 20 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d   & WHERE_TOP_LIM
29847 49 54 20 29 3b 0a 20 20 20 20 20 20 69 66 28 20  IT );.      if( 
29848 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20  pLevel->flags & 
29849 28 57 48 45 52 45 5f 42 54 4d 5f 4c 49 4d 49 54  (WHERE_BTM_LIMIT
2984a 7c 57 48 45 52 45 5f 54 4f 50 5f 4c 49 4d 49 54  |WHERE_TOP_LIMIT
2984b 29 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  ) ){.        sql
2984c 69 74 65 33 56 64 62 65 41 64 64 4f 70 33 28 76  ite3VdbeAddOp3(v
2984d 2c 20 4f 50 5f 43 6f 6c 75 6d 6e 2c 20 69 49 64  , OP_Column, iId
2984e 78 43 75 72 2c 20 6e 45 71 2c 20 72 31 29 3b 0a  xCur, nEq, r1);.
2984f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 56          sqlite3V
29850 64 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f  dbeAddOp2(v, OP_
29851 49 73 4e 75 6c 6c 2c 20 72 31 2c 20 63 6f 6e 74  IsNull, r1, cont
29852 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
29853 20 20 2f 2a 20 53 65 65 6b 20 74 68 65 20 74 61    /* Seek the ta
29854 62 6c 65 20 63 75 72 73 6f 72 2c 20 69 66 20 72  ble cursor, if r
29855 65 71 75 69 72 65 64 20 2a 2f 0a 20 20 20 20 20  equired */.     
29856 20 69 66 28 20 21 6f 6d 69 74 54 61 62 6c 65 20   if( !omitTable 
29857 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74  ){.        sqlit
29858 65 33 56 64 62 65 41 64 64 4f 70 32 28 76 2c 20  e3VdbeAddOp2(v, 
29859 4f 50 5f 49 64 78 52 6f 77 69 64 2c 20 69 49 64  OP_IdxRowid, iId
2985a 78 43 75 72 2c 20 72 31 29 3b 0a 20 20 20 20 20  xCur, r1);.     
2985b 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64     sqlite3VdbeAd
2985c 64 4f 70 33 28 76 2c 20 4f 50 5f 4d 6f 76 65 47  dOp3(v, OP_MoveG
2985d 65 2c 20 69 43 75 72 2c 20 30 2c 20 72 31 29 3b  e, iCur, 0, r1);
2985e 20 20 2f 2a 20 44 65 66 65 72 72 65 64 20 73 65    /* Deferred se
2985f 65 6b 20 2a 2f 0a 20 20 20 20 20 20 7d 0a 20 20  ek */.      }.  
29860 20 20 20 20 73 71 6c 69 74 65 33 52 65 6c 65 61      sqlite3Relea
29861 73 65 54 65 6d 70 52 65 67 28 70 50 61 72 73 65  seTempReg(pParse
29862 2c 20 72 31 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  , r1);..      /*
29863 20 52 65 63 6f 72 64 20 74 68 65 20 69 6e 73 74   Record the inst
29864 72 75 63 74 69 6f 6e 20 75 73 65 64 20 74 6f 20  ruction used to 
29865 74 65 72 6d 69 6e 61 74 65 20 74 68 65 20 6c 6f  terminate the lo
29866 6f 70 2e 20 44 69 73 61 62 6c 65 20 0a 20 20 20  op. Disable .   
29867 20 20 20 2a 2a 20 57 48 45 52 45 20 63 6c 61 75     ** WHERE clau
29868 73 65 20 74 65 72 6d 73 20 6d 61 64 65 20 72 65  se terms made re
29869 64 75 6e 64 61 6e 74 20 62 79 20 74 68 65 20 69  dundant by the i
2986a 6e 64 65 78 20 72 61 6e 67 65 20 73 63 61 6e 2e  ndex range scan.
2986b 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20  .      */.      
2986c 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 62 52 65  pLevel->op = bRe
2986d 76 20 3f 20 4f 50 5f 50 72 65 76 20 3a 20 4f 50  v ? OP_Prev : OP
2986e 5f 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65  _Next;.      pLe
2986f 76 65 6c 2d 3e 70 31 20 3d 20 69 49 64 78 43 75  vel->p1 = iIdxCu
29870 72 3b 0a 20 20 20 20 20 20 64 69 73 61 62 6c 65  r;.      disable
29871 54 65 72 6d 28 70 4c 65 76 65 6c 2c 20 70 52 61  Term(pLevel, pRa
29872 6e 67 65 53 74 61 72 74 29 3b 0a 20 20 20 20 20  ngeStart);.     
29873 20 64 69 73 61 62 6c 65 54 65 72 6d 28 70 4c 65   disableTerm(pLe
29874 76 65 6c 2c 20 70 52 61 6e 67 65 45 6e 64 29 3b  vel, pRangeEnd);
29875 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
29876 20 20 2f 2a 20 43 61 73 65 20 34 3a 20 20 54 68    /* Case 4:  Th
29877 65 72 65 20 69 73 20 6e 6f 20 75 73 61 62 6c 65  ere is no usable
29878 20 69 6e 64 65 78 2e 20 20 57 65 20 6d 75 73 74   index.  We must
29879 20 64 6f 20 61 20 63 6f 6d 70 6c 65 74 65 0a 20   do a complete. 
2987a 20 20 20 20 20 2a 2a 20 20 20 20 20 20 20 20 20       **         
2987b 20 73 63 61 6e 20 6f 66 20 74 68 65 20 65 6e 74   scan of the ent
2987c 69 72 65 20 74 61 62 6c 65 2e 0a 20 20 20 20 20  ire table..     
2987d 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
2987e 28 20 6f 6d 69 74 54 61 62 6c 65 3d 3d 30 20 29  ( omitTable==0 )
2987f 3b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  ;.      assert( 
29880 62 52 65 76 3d 3d 30 20 29 3b 0a 20 20 20 20 20  bRev==0 );.     
29881 20 70 4c 65 76 65 6c 2d 3e 6f 70 20 3d 20 4f 50   pLevel->op = OP
29882 5f 4e 65 78 74 3b 0a 20 20 20 20 20 20 70 4c 65  _Next;.      pLe
29883 76 65 6c 2d 3e 70 31 20 3d 20 69 43 75 72 3b 0a  vel->p1 = iCur;.
29884 20 20 20 20 20 20 70 4c 65 76 65 6c 2d 3e 70 32        pLevel->p2
29885 20 3d 20 31 20 2b 20 73 71 6c 69 74 65 33 56 64   = 1 + sqlite3Vd
29886 62 65 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 52  beAddOp2(v, OP_R
29887 65 77 69 6e 64 2c 20 69 43 75 72 2c 20 62 72 6b  ewind, iCur, brk
29888 29 3b 0a 20 20 20 20 20 20 70 4c 65 76 65 6c 2d  );.      pLevel-
29889 3e 70 35 20 3d 20 53 51 4c 49 54 45 5f 53 54 4d  >p5 = SQLITE_STM
2988a 54 53 54 41 54 55 53 5f 46 55 4c 4c 53 43 41 4e  TSTATUS_FULLSCAN
2988b 5f 53 54 45 50 3b 0a 20 20 20 20 7d 0a 20 20 20  _STEP;.    }.   
2988c 20 6e 6f 74 52 65 61 64 79 20 26 3d 20 7e 67 65   notReady &= ~ge
2988d 74 4d 61 73 6b 28 26 6d 61 73 6b 53 65 74 2c 20  tMask(&maskSet, 
2988e 69 43 75 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  iCur);..    /* I
2988f 6e 73 65 72 74 20 63 6f 64 65 20 74 6f 20 74 65  nsert code to te
29890 73 74 20 65 76 65 72 79 20 73 75 62 65 78 70 72  st every subexpr
29891 65 73 73 69 6f 6e 20 74 68 61 74 20 63 61 6e 20  ession that can 
29892 62 65 20 63 6f 6d 70 6c 65 74 65 6c 79 0a 20 20  be completely.  
29893 20 20 2a 2a 20 63 6f 6d 70 75 74 65 64 20 75 73    ** computed us
29894 69 6e 67 20 74 68 65 20 63 75 72 72 65 6e 74 20  ing the current 
29895 73 65 74 20 6f 66 20 74 61 62 6c 65 73 2e 0a 20  set of tables.. 
29896 20 20 20 2a 2f 0a 20 20 20 20 6b 20 3d 20 30 3b     */.    k = 0;
29897 0a 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d 77  .    for(pTerm=w
29898 63 2e 61 2c 20 6a 3d 77 63 2e 6e 54 65 72 6d 3b  c.a, j=wc.nTerm;
29899 20 6a 3e 30 3b 20 6a 2d 2d 2c 20 70 54 65 72 6d   j>0; j--, pTerm
2989a 2b 2b 29 7b 0a 20 20 20 20 20 20 45 78 70 72 20  ++){.      Expr 
2989b 2a 70 45 3b 0a 20 20 20 20 20 20 74 65 73 74 63  *pE;.      testc
2989c 61 73 65 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67  ase( pTerm->flag
2989d 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55 41 4c  s & TERM_VIRTUAL
2989e 20 29 3b 0a 20 20 20 20 20 20 74 65 73 74 63 61   );.      testca
2989f 73 65 28 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73  se( pTerm->flags
298a0 20 26 20 54 45 52 4d 5f 43 4f 44 45 44 20 29 3b   & TERM_CODED );
298a1 0a 20 20 20 20 20 20 69 66 28 20 70 54 65 72 6d  .      if( pTerm
298a2 2d 3e 66 6c 61 67 73 20 26 20 28 54 45 52 4d 5f  ->flags & (TERM_
298a3 56 49 52 54 55 41 4c 7c 54 45 52 4d 5f 43 4f 44  VIRTUAL|TERM_COD
298a4 45 44 29 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ED) ) continue;.
298a5 20 20 20 20 20 20 69 66 28 20 28 70 54 65 72 6d        if( (pTerm
298a6 2d 3e 70 72 65 72 65 71 41 6c 6c 20 26 20 6e 6f  ->prereqAll & no
298a7 74 52 65 61 64 79 29 21 3d 30 20 29 20 63 6f 6e  tReady)!=0 ) con
298a8 74 69 6e 75 65 3b 0a 20 20 20 20 20 20 70 45 20  tinue;.      pE 
298a9 3d 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 3b 0a  = pTerm->pExpr;.
298aa 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70 45        assert( pE
298ab 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66 28  !=0 );.      if(
298ac 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74 4a 6f   pLevel->iLeftJo
298ad 69 6e 20 26 26 20 21 45 78 70 72 48 61 73 50 72  in && !ExprHasPr
298ae 6f 70 65 72 74 79 28 70 45 2c 20 45 50 5f 46 72  operty(pE, EP_Fr
298af 6f 6d 4a 6f 69 6e 29 20 29 7b 0a 20 20 20 20 20  omJoin) ){.     
298b0 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
298b1 20 20 20 7d 0a 20 20 20 20 20 20 70 50 61 72 73     }.      pPars
298b2 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c 43 61 63  e->disableColCac
298b3 68 65 20 2b 3d 20 6b 3b 0a 20 20 20 20 20 20 73  he += k;.      s
298b4 71 6c 69 74 65 33 45 78 70 72 49 66 46 61 6c 73  qlite3ExprIfFals
298b5 65 28 70 50 61 72 73 65 2c 20 70 45 2c 20 63 6f  e(pParse, pE, co
298b6 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d 50 49  nt, SQLITE_JUMPI
298b7 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20 70 50  FNULL);.      pP
298b8 61 72 73 65 2d 3e 64 69 73 61 62 6c 65 43 6f 6c  arse->disableCol
298b9 43 61 63 68 65 20 2d 3d 20 6b 3b 0a 20 20 20 20  Cache -= k;.    
298ba 20 20 6b 20 3d 20 31 3b 0a 20 20 20 20 20 20 70    k = 1;.      p
298bb 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c 3d 20 54  Term->flags |= T
298bc 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20 20 20 7d  ERM_CODED;.    }
298bd 0a 0a 20 20 20 20 2f 2a 20 46 6f 72 20 61 20 4c  ..    /* For a L
298be 45 46 54 20 4f 55 54 45 52 20 4a 4f 49 4e 2c 20  EFT OUTER JOIN, 
298bf 67 65 6e 65 72 61 74 65 20 63 6f 64 65 20 74 68  generate code th
298c0 61 74 20 77 69 6c 6c 20 72 65 63 6f 72 64 20 74  at will record t
298c1 68 65 20 66 61 63 74 20 74 68 61 74 0a 20 20 20  he fact that.   
298c2 20 2a 2a 20 61 74 20 6c 65 61 73 74 20 6f 6e 65   ** at least one
298c3 20 72 6f 77 20 6f 66 20 74 68 65 20 72 69 67 68   row of the righ
298c4 74 20 74 61 62 6c 65 20 68 61 73 20 6d 61 74 63  t table has matc
298c5 68 65 64 20 74 68 65 20 6c 65 66 74 20 74 61 62  hed the left tab
298c6 6c 65 2e 20 20 0a 20 20 20 20 2a 2f 0a 20 20 20  le.  .    */.   
298c7 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 4c 65   if( pLevel->iLe
298c8 66 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20 20 20  ftJoin ){.      
298c9 70 4c 65 76 65 6c 2d 3e 74 6f 70 20 3d 20 73 71  pLevel->top = sq
298ca 6c 69 74 65 33 56 64 62 65 43 75 72 72 65 6e 74  lite3VdbeCurrent
298cb 41 64 64 72 28 76 29 3b 0a 20 20 20 20 20 20 73  Addr(v);.      s
298cc 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 32  qlite3VdbeAddOp2
298cd 28 76 2c 20 4f 50 5f 49 6e 74 65 67 65 72 2c 20  (v, OP_Integer, 
298ce 31 2c 20 70 4c 65 76 65 6c 2d 3e 69 4c 65 66 74  1, pLevel->iLeft
298cf 4a 6f 69 6e 29 3b 0a 20 20 20 20 20 20 56 64 62  Join);.      Vdb
298d0 65 43 6f 6d 6d 65 6e 74 28 28 76 2c 20 22 72 65  eComment((v, "re
298d1 63 6f 72 64 20 4c 45 46 54 20 4a 4f 49 4e 20 68  cord LEFT JOIN h
298d2 69 74 22 29 29 3b 0a 20 20 20 20 20 20 73 71 6c  it"));.      sql
298d3 69 74 65 33 45 78 70 72 43 6c 65 61 72 43 6f 6c  ite3ExprClearCol
298d4 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73 65 2c  umnCache(pParse,
298d5 20 70 4c 65 76 65 6c 2d 3e 69 54 61 62 43 75 72   pLevel->iTabCur
298d6 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
298d7 45 78 70 72 43 6c 65 61 72 43 6f 6c 75 6d 6e 43  ExprClearColumnC
298d8 61 63 68 65 28 70 50 61 72 73 65 2c 20 70 4c 65  ache(pParse, pLe
298d9 76 65 6c 2d 3e 69 49 64 78 43 75 72 29 3b 0a 20  vel->iIdxCur);. 
298da 20 20 20 20 20 66 6f 72 28 70 54 65 72 6d 3d 77       for(pTerm=w
298db 63 2e 61 2c 20 6a 3d 30 3b 20 6a 3c 77 63 2e 6e  c.a, j=0; j<wc.n
298dc 54 65 72 6d 3b 20 6a 2b 2b 2c 20 70 54 65 72 6d  Term; j++, pTerm
298dd 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 74 65 73  ++){.        tes
298de 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 66 6c  tcase( pTerm->fl
298df 61 67 73 20 26 20 54 45 52 4d 5f 56 49 52 54 55  ags & TERM_VIRTU
298e0 41 4c 20 29 3b 0a 20 20 20 20 20 20 20 20 74 65  AL );.        te
298e1 73 74 63 61 73 65 28 20 70 54 65 72 6d 2d 3e 66  stcase( pTerm->f
298e2 6c 61 67 73 20 26 20 54 45 52 4d 5f 43 4f 44 45  lags & TERM_CODE
298e3 44 20 29 3b 0a 20 20 20 20 20 20 20 20 69 66 28  D );.        if(
298e4 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 26 20   pTerm->flags & 
298e5 28 54 45 52 4d 5f 56 49 52 54 55 41 4c 7c 54 45  (TERM_VIRTUAL|TE
298e6 52 4d 5f 43 4f 44 45 44 29 20 29 20 63 6f 6e 74  RM_CODED) ) cont
298e7 69 6e 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66  inue;.        if
298e8 28 20 28 70 54 65 72 6d 2d 3e 70 72 65 72 65 71  ( (pTerm->prereq
298e9 41 6c 6c 20 26 20 6e 6f 74 52 65 61 64 79 29 21  All & notReady)!
298ea 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  =0 ) continue;. 
298eb 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70         assert( p
298ec 54 65 72 6d 2d 3e 70 45 78 70 72 20 29 3b 0a 20  Term->pExpr );. 
298ed 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78         sqlite3Ex
298ee 70 72 49 66 46 61 6c 73 65 28 70 50 61 72 73 65  prIfFalse(pParse
298ef 2c 20 70 54 65 72 6d 2d 3e 70 45 78 70 72 2c 20  , pTerm->pExpr, 
298f0 63 6f 6e 74 2c 20 53 51 4c 49 54 45 5f 4a 55 4d  cont, SQLITE_JUM
298f1 50 49 46 4e 55 4c 4c 29 3b 0a 20 20 20 20 20 20  PIFNULL);.      
298f2 20 20 70 54 65 72 6d 2d 3e 66 6c 61 67 73 20 7c    pTerm->flags |
298f3 3d 20 54 45 52 4d 5f 43 4f 44 45 44 3b 0a 20 20  = TERM_CODED;.  
298f4 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
298f5 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 54  .#ifdef SQLITE_T
298f6 45 53 54 20 20 2f 2a 20 46 6f 72 20 74 65 73 74  EST  /* For test
298f7 69 6e 67 20 61 6e 64 20 64 65 62 75 67 67 69 6e  ing and debuggin
298f8 67 20 75 73 65 20 6f 6e 6c 79 20 2a 2f 0a 20 20  g use only */.  
298f9 2f 2a 20 52 65 63 6f 72 64 20 69 6e 20 74 68 65  /* Record in the
298fa 20 71 75 65 72 79 20 70 6c 61 6e 20 69 6e 66 6f   query plan info
298fb 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68  rmation about th
298fc 65 20 63 75 72 72 65 6e 74 20 74 61 62 6c 65 0a  e current table.
298fd 20 20 2a 2a 20 61 6e 64 20 74 68 65 20 69 6e 64    ** and the ind
298fe 65 78 20 75 73 65 64 20 74 6f 20 61 63 63 65 73  ex used to acces
298ff 73 20 69 74 20 28 69 66 20 61 6e 79 29 2e 20 20  s it (if any).  
29900 49 66 20 74 68 65 20 74 61 62 6c 65 20 69 74 73  If the table its
29901 65 6c 66 0a 20 20 2a 2a 20 69 73 20 6e 6f 74 20  elf.  ** is not 
29902 75 73 65 64 2c 20 69 74 73 20 6e 61 6d 65 20 69  used, its name i
29903 73 20 6a 75 73 74 20 27 7b 7d 27 2e 20 20 49 66  s just '{}'.  If
29904 20 6e 6f 20 69 6e 64 65 78 20 69 73 20 75 73 65   no index is use
29905 64 0a 20 20 2a 2a 20 74 68 65 20 69 6e 64 65 78  d.  ** the index
29906 20 69 73 20 6c 69 73 74 65 64 20 61 73 20 22 7b   is listed as "{
29907 7d 22 2e 20 20 49 66 20 74 68 65 20 70 72 69 6d  }".  If the prim
29908 61 72 79 20 6b 65 79 20 69 73 20 75 73 65 64 20  ary key is used 
29909 74 68 65 0a 20 20 2a 2a 20 69 6e 64 65 78 20 6e  the.  ** index n
2990a 61 6d 65 20 69 73 20 27 2a 27 2e 0a 20 20 2a 2f  ame is '*'..  */
2990b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 54  .  for(i=0; i<pT
2990c 61 62 4c 69 73 74 2d 3e 6e 53 72 63 3b 20 69 2b  abList->nSrc; i+
2990d 2b 29 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 3b  +){.    char *z;
2990e 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20 20 20 20  .    int n;.    
2990f 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e 66 6f  pLevel = &pWInfo
29910 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 70 54 61 62  ->a[i];.    pTab
29911 49 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74  Item = &pTabList
29912 2d 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f  ->a[pLevel->iFro
29913 6d 5d 3b 0a 20 20 20 20 7a 20 3d 20 70 54 61 62  m];.    z = pTab
29914 49 74 65 6d 2d 3e 7a 41 6c 69 61 73 3b 0a 20 20  Item->zAlias;.  
29915 20 20 69 66 28 20 7a 3d 3d 30 20 29 20 7a 20 3d    if( z==0 ) z =
29916 20 70 54 61 62 49 74 65 6d 2d 3e 70 54 61 62 2d   pTabItem->pTab-
29917 3e 7a 4e 61 6d 65 3b 0a 20 20 20 20 6e 20 3d 20  >zName;.    n = 
29918 73 74 72 6c 65 6e 28 7a 29 3b 0a 20 20 20 20 69  strlen(z);.    i
29919 66 28 20 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69  f( n+nQPlan < si
2991a 7a 65 6f 66 28 73 71 6c 69 74 65 33 5f 71 75 65  zeof(sqlite3_que
2991b 72 79 5f 70 6c 61 6e 29 2d 31 30 20 29 7b 0a 20  ry_plan)-10 ){. 
2991c 20 20 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d       if( pLevel-
2991d 3e 66 6c 61 67 73 20 26 20 57 48 45 52 45 5f 49  >flags & WHERE_I
2991e 44 58 5f 4f 4e 4c 59 20 29 7b 0a 20 20 20 20 20  DX_ONLY ){.     
2991f 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
29920 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
29921 50 6c 61 6e 5d 2c 20 22 7b 7d 22 2c 20 32 29 3b  Plan], "{}", 2);
29922 0a 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20  .        nQPlan 
29923 2b 3d 20 32 3b 0a 20 20 20 20 20 20 7d 65 6c 73  += 2;.      }els
29924 65 7b 0a 20 20 20 20 20 20 20 20 6d 65 6d 63 70  e{.        memcp
29925 79 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79  y(&sqlite3_query
29926 5f 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 7a  _plan[nQPlan], z
29927 2c 20 6e 29 3b 0a 20 20 20 20 20 20 20 20 6e 51  , n);.        nQ
29928 50 6c 61 6e 20 2b 3d 20 6e 3b 0a 20 20 20 20 20  Plan += n;.     
29929 20 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33   }.      sqlite3
2992a 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
2992b 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b 0a 20 20 20  an++] = ' ';.   
2992c 20 7d 0a 20 20 20 20 74 65 73 74 63 61 73 65 28   }.    testcase(
2992d 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
2992e 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 20   WHERE_ROWID_EQ 
2992f 29 3b 0a 20 20 20 20 74 65 73 74 63 61 73 65 28  );.    testcase(
29930 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26   pLevel->flags &
29931 20 57 48 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e   WHERE_ROWID_RAN
29932 47 45 20 29 3b 0a 20 20 20 20 69 66 28 20 70 4c  GE );.    if( pL
29933 65 76 65 6c 2d 3e 66 6c 61 67 73 20 26 20 28 57  evel->flags & (W
29934 48 45 52 45 5f 52 4f 57 49 44 5f 45 51 7c 57 48  HERE_ROWID_EQ|WH
29935 45 52 45 5f 52 4f 57 49 44 5f 52 41 4e 47 45 29  ERE_ROWID_RANGE)
29936 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79   ){.      memcpy
29937 28 26 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  (&sqlite3_query_
29938 70 6c 61 6e 5b 6e 51 50 6c 61 6e 5d 2c 20 22 2a  plan[nQPlan], "*
29939 20 22 2c 20 32 29 3b 0a 20 20 20 20 20 20 6e 51   ", 2);.      nQ
2993a 50 6c 61 6e 20 2b 3d 20 32 3b 0a 20 20 20 20 7d  Plan += 2;.    }
2993b 65 6c 73 65 20 69 66 28 20 70 4c 65 76 65 6c 2d  else if( pLevel-
2993c 3e 70 49 64 78 3d 3d 30 20 29 7b 0a 20 20 20 20  >pIdx==0 ){.    
2993d 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74 65    memcpy(&sqlite
2993e 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50  3_query_plan[nQP
2993f 6c 61 6e 5d 2c 20 22 7b 7d 20 22 2c 20 33 29 3b  lan], "{} ", 3);
29940 0a 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b 3d  .      nQPlan +=
29941 20 33 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20   3;.    }else{. 
29942 20 20 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28       n = strlen(
29943 70 4c 65 76 65 6c 2d 3e 70 49 64 78 2d 3e 7a 4e  pLevel->pIdx->zN
29944 61 6d 65 29 3b 0a 20 20 20 20 20 20 69 66 28 20  ame);.      if( 
29945 6e 2b 6e 51 50 6c 61 6e 20 3c 20 73 69 7a 65 6f  n+nQPlan < sizeo
29946 66 28 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f  f(sqlite3_query_
29947 70 6c 61 6e 29 2d 32 20 29 7b 0a 20 20 20 20 20  plan)-2 ){.     
29948 20 20 20 6d 65 6d 63 70 79 28 26 73 71 6c 69 74     memcpy(&sqlit
29949 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51  e3_query_plan[nQ
2994a 50 6c 61 6e 5d 2c 20 70 4c 65 76 65 6c 2d 3e 70  Plan], pLevel->p
2994b 49 64 78 2d 3e 7a 4e 61 6d 65 2c 20 6e 29 3b 0a  Idx->zName, n);.
2994c 20 20 20 20 20 20 20 20 6e 51 50 6c 61 6e 20 2b          nQPlan +
2994d 3d 20 6e 3b 0a 20 20 20 20 20 20 20 20 73 71 6c  = n;.        sql
2994e 69 74 65 33 5f 71 75 65 72 79 5f 70 6c 61 6e 5b  ite3_query_plan[
2994f 6e 51 50 6c 61 6e 2b 2b 5d 20 3d 20 27 20 27 3b  nQPlan++] = ' ';
29950 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
29951 20 7d 0a 20 20 77 68 69 6c 65 28 20 6e 51 50 6c   }.  while( nQPl
29952 61 6e 3e 30 20 26 26 20 73 71 6c 69 74 65 33 5f  an>0 && sqlite3_
29953 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c 61  query_plan[nQPla
29954 6e 2d 31 5d 3d 3d 27 20 27 20 29 7b 0a 20 20 20  n-1]==' ' ){.   
29955 20 73 71 6c 69 74 65 33 5f 71 75 65 72 79 5f 70   sqlite3_query_p
29956 6c 61 6e 5b 2d 2d 6e 51 50 6c 61 6e 5d 20 3d 20  lan[--nQPlan] = 
29957 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
29958 5f 71 75 65 72 79 5f 70 6c 61 6e 5b 6e 51 50 6c  _query_plan[nQPl
29959 61 6e 5d 20 3d 20 30 3b 0a 20 20 6e 51 50 6c 61  an] = 0;.  nQPla
2995a 6e 20 3d 20 30 3b 0a 23 65 6e 64 69 66 20 2f 2a  n = 0;.#endif /*
2995b 20 53 51 4c 49 54 45 5f 54 45 53 54 20 2f 2f 20   SQLITE_TEST // 
2995c 54 65 73 74 69 6e 67 20 61 6e 64 20 64 65 62 75  Testing and debu
2995d 67 67 69 6e 67 20 75 73 65 20 6f 6e 6c 79 20 2a  gging use only *
2995e 2f 0a 0a 20 20 2f 2a 20 52 65 63 6f 72 64 20 74  /..  /* Record t
2995f 68 65 20 63 6f 6e 74 69 6e 75 61 74 69 6f 6e 20  he continuation 
29960 61 64 64 72 65 73 73 20 69 6e 20 74 68 65 20 57  address in the W
29961 68 65 72 65 49 6e 66 6f 20 73 74 72 75 63 74 75  hereInfo structu
29962 72 65 2e 20 20 54 68 65 6e 0a 20 20 2a 2a 20 63  re.  Then.  ** c
29963 6c 65 61 6e 20 75 70 20 61 6e 64 20 72 65 74 75  lean up and retu
29964 72 6e 2e 0a 20 20 2a 2f 0a 20 20 70 57 49 6e 66  rn..  */.  pWInf
29965 6f 2d 3e 69 43 6f 6e 74 69 6e 75 65 20 3d 20 63  o->iContinue = c
29966 6f 6e 74 3b 0a 20 20 77 68 65 72 65 43 6c 61 75  ont;.  whereClau
29967 73 65 43 6c 65 61 72 28 26 77 63 29 3b 0a 20 20  seClear(&wc);.  
29968 72 65 74 75 72 6e 20 70 57 49 6e 66 6f 3b 0a 0a  return pWInfo;..
29969 20 20 2f 2a 20 4a 75 6d 70 20 68 65 72 65 20 69    /* Jump here i
2996a 66 20 6d 61 6c 6c 6f 63 20 66 61 69 6c 73 20 2a  f malloc fails *
2996b 2f 0a 77 68 65 72 65 42 65 67 69 6e 45 72 72 6f  /.whereBeginErro
2996c 72 3a 0a 20 20 77 68 65 72 65 43 6c 61 75 73 65  r:.  whereClause
2996d 43 6c 65 61 72 28 26 77 63 29 3b 0a 20 20 77 68  Clear(&wc);.  wh
2996e 65 72 65 49 6e 66 6f 46 72 65 65 28 64 62 2c 20  ereInfoFree(db, 
2996f 70 57 49 6e 66 6f 29 3b 0a 20 20 72 65 74 75 72  pWInfo);.  retur
29970 6e 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  n 0;.}../*.** Ge
29971 6e 65 72 61 74 65 20 74 68 65 20 65 6e 64 20 6f  nerate the end o
29972 66 20 74 68 65 20 57 48 45 52 45 20 6c 6f 6f 70  f the WHERE loop
29973 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 73 20  .  See comments 
29974 6f 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33 57 68  on .** sqlite3Wh
29975 65 72 65 42 65 67 69 6e 28 29 20 66 6f 72 20 61  ereBegin() for a
29976 64 64 69 74 69 6f 6e 61 6c 20 69 6e 66 6f 72 6d  dditional inform
29977 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54 45  ation..*/.SQLITE
29978 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
29979 6c 69 74 65 33 57 68 65 72 65 45 6e 64 28 57 68  lite3WhereEnd(Wh
2997a 65 72 65 49 6e 66 6f 20 2a 70 57 49 6e 66 6f 29  ereInfo *pWInfo)
2997b 7b 0a 20 20 50 61 72 73 65 20 2a 70 50 61 72 73  {.  Parse *pPars
2997c 65 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 50 61 72  e = pWInfo->pPar
2997d 73 65 3b 0a 20 20 56 64 62 65 20 2a 76 20 3d 20  se;.  Vdbe *v = 
2997e 70 50 61 72 73 65 2d 3e 70 56 64 62 65 3b 0a 20  pParse->pVdbe;. 
2997f 20 69 6e 74 20 69 3b 0a 20 20 57 68 65 72 65 4c   int i;.  WhereL
29980 65 76 65 6c 20 2a 70 4c 65 76 65 6c 3b 0a 20 20  evel *pLevel;.  
29981 53 72 63 4c 69 73 74 20 2a 70 54 61 62 4c 69 73  SrcList *pTabLis
29982 74 20 3d 20 70 57 49 6e 66 6f 2d 3e 70 54 61 62  t = pWInfo->pTab
29983 4c 69 73 74 3b 0a 20 20 73 71 6c 69 74 65 33 20  List;.  sqlite3 
29984 2a 64 62 20 3d 20 70 50 61 72 73 65 2d 3e 64 62  *db = pParse->db
29985 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74 65  ;..  /* Generate
29986 20 6c 6f 6f 70 20 74 65 72 6d 69 6e 61 74 69 6f   loop terminatio
29987 6e 20 63 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 73  n code..  */.  s
29988 71 6c 69 74 65 33 45 78 70 72 43 6c 65 61 72 43  qlite3ExprClearC
29989 6f 6c 75 6d 6e 43 61 63 68 65 28 70 50 61 72 73  olumnCache(pPars
2998a 65 2c 20 2d 31 29 3b 0a 20 20 66 6f 72 28 69 3d  e, -1);.  for(i=
2998b 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63 2d 31  pTabList->nSrc-1
2998c 3b 20 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20  ; i>=0; i--){.  
2998d 20 20 70 4c 65 76 65 6c 20 3d 20 26 70 57 49 6e    pLevel = &pWIn
2998e 66 6f 2d 3e 61 5b 69 5d 3b 0a 20 20 20 20 73 71  fo->a[i];.    sq
2998f 6c 69 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65  lite3VdbeResolve
29990 4c 61 62 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d  Label(v, pLevel-
29991 3e 63 6f 6e 74 29 3b 0a 20 20 20 20 69 66 28 20  >cont);.    if( 
29992 70 4c 65 76 65 6c 2d 3e 6f 70 21 3d 4f 50 5f 4e  pLevel->op!=OP_N
29993 6f 6f 70 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  oop ){.      sql
29994 69 74 65 33 56 64 62 65 41 64 64 4f 70 32 28 76  ite3VdbeAddOp2(v
29995 2c 20 70 4c 65 76 65 6c 2d 3e 6f 70 2c 20 70 4c  , pLevel->op, pL
29996 65 76 65 6c 2d 3e 70 31 2c 20 70 4c 65 76 65 6c  evel->p1, pLevel
29997 2d 3e 70 32 29 3b 0a 20 20 20 20 20 20 73 71 6c  ->p2);.      sql
29998 69 74 65 33 56 64 62 65 43 68 61 6e 67 65 50 35  ite3VdbeChangeP5
29999 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 70 35 29 3b  (v, pLevel->p5);
2999a 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 70  .    }.    if( p
2999b 4c 65 76 65 6c 2d 3e 6e 49 6e 20 29 7b 0a 20 20  Level->nIn ){.  
2999c 20 20 20 20 73 74 72 75 63 74 20 49 6e 4c 6f 6f      struct InLoo
2999d 70 20 2a 70 49 6e 3b 0a 20 20 20 20 20 20 69 6e  p *pIn;.      in
2999e 74 20 6a 3b 0a 20 20 20 20 20 20 73 71 6c 69 74  t j;.      sqlit
2999f 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61 62  e3VdbeResolveLab
299a0 65 6c 28 76 2c 20 70 4c 65 76 65 6c 2d 3e 6e 78  el(v, pLevel->nx
299a1 74 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 3d  t);.      for(j=
299a2 70 4c 65 76 65 6c 2d 3e 6e 49 6e 2c 20 70 49 6e  pLevel->nIn, pIn
299a3 3d 26 70 4c 65 76 65 6c 2d 3e 61 49 6e 4c 6f 6f  =&pLevel->aInLoo
299a4 70 5b 6a 2d 31 5d 3b 20 6a 3e 30 3b 20 6a 2d 2d  p[j-1]; j>0; j--
299a5 2c 20 70 49 6e 2d 2d 29 7b 0a 20 20 20 20 20 20  , pIn--){.      
299a6 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
299a7 70 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f  pHere(v, pIn->to
299a8 70 41 64 64 72 2b 31 29 3b 0a 20 20 20 20 20 20  pAddr+1);.      
299a9 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
299aa 4f 70 32 28 76 2c 20 4f 50 5f 4e 65 78 74 2c 20  Op2(v, OP_Next, 
299ab 70 49 6e 2d 3e 69 43 75 72 2c 20 70 49 6e 2d 3e  pIn->iCur, pIn->
299ac 74 6f 70 41 64 64 72 29 3b 0a 20 20 20 20 20 20  topAddr);.      
299ad 20 20 73 71 6c 69 74 65 33 56 64 62 65 4a 75 6d    sqlite3VdbeJum
299ae 70 48 65 72 65 28 76 2c 20 70 49 6e 2d 3e 74 6f  pHere(v, pIn->to
299af 70 41 64 64 72 2d 31 29 3b 0a 20 20 20 20 20 20  pAddr-1);.      
299b0 7d 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 44  }.      sqlite3D
299b1 62 46 72 65 65 28 64 62 2c 20 70 4c 65 76 65 6c  bFree(db, pLevel
299b2 2d 3e 61 49 6e 4c 6f 6f 70 29 3b 0a 20 20 20 20  ->aInLoop);.    
299b3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 56 64 62  }.    sqlite3Vdb
299b4 65 52 65 73 6f 6c 76 65 4c 61 62 65 6c 28 76 2c  eResolveLabel(v,
299b5 20 70 4c 65 76 65 6c 2d 3e 62 72 6b 29 3b 0a 20   pLevel->brk);. 
299b6 20 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69     if( pLevel->i
299b7 4c 65 66 74 4a 6f 69 6e 20 29 7b 0a 20 20 20 20  LeftJoin ){.    
299b8 20 20 69 6e 74 20 61 64 64 72 3b 0a 20 20 20 20    int addr;.    
299b9 20 20 61 64 64 72 20 3d 20 73 71 6c 69 74 65 33    addr = sqlite3
299ba 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
299bb 5f 49 66 50 6f 73 2c 20 70 4c 65 76 65 6c 2d 3e  _IfPos, pLevel->
299bc 69 4c 65 66 74 4a 6f 69 6e 29 3b 0a 20 20 20 20  iLeftJoin);.    
299bd 20 20 73 71 6c 69 74 65 33 56 64 62 65 41 64 64    sqlite3VdbeAdd
299be 4f 70 31 28 76 2c 20 4f 50 5f 4e 75 6c 6c 52 6f  Op1(v, OP_NullRo
299bf 77 2c 20 70 54 61 62 4c 69 73 74 2d 3e 61 5b 69  w, pTabList->a[i
299c0 5d 2e 69 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  ].iCursor);.    
299c1 20 20 69 66 28 20 70 4c 65 76 65 6c 2d 3e 69 49    if( pLevel->iI
299c2 64 78 43 75 72 3e 3d 30 20 29 7b 0a 20 20 20 20  dxCur>=0 ){.    
299c3 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65 41      sqlite3VdbeA
299c4 64 64 4f 70 31 28 76 2c 20 4f 50 5f 4e 75 6c 6c  ddOp1(v, OP_Null
299c5 52 6f 77 2c 20 70 4c 65 76 65 6c 2d 3e 69 49 64  Row, pLevel->iId
299c6 78 43 75 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20  xCur);.      }. 
299c7 20 20 20 20 20 73 71 6c 69 74 65 33 56 64 62 65       sqlite3Vdbe
299c8 41 64 64 4f 70 32 28 76 2c 20 4f 50 5f 47 6f 74  AddOp2(v, OP_Got
299c9 6f 2c 20 30 2c 20 70 4c 65 76 65 6c 2d 3e 74 6f  o, 0, pLevel->to
299ca 70 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65  p);.      sqlite
299cb 33 56 64 62 65 4a 75 6d 70 48 65 72 65 28 76 2c  3VdbeJumpHere(v,
299cc 20 61 64 64 72 29 3b 0a 20 20 20 20 7d 0a 20 20   addr);.    }.  
299cd 7d 0a 0a 20 20 2f 2a 20 54 68 65 20 22 62 72 65  }..  /* The "bre
299ce 61 6b 22 20 70 6f 69 6e 74 20 69 73 20 68 65 72  ak" point is her
299cf 65 2c 20 6a 75 73 74 20 70 61 73 74 20 74 68 65  e, just past the
299d0 20 65 6e 64 20 6f 66 20 74 68 65 20 6f 75 74 65   end of the oute
299d1 72 20 6c 6f 6f 70 2e 0a 20 20 2a 2a 20 53 65 74  r loop..  ** Set
299d2 20 69 74 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69   it..  */.  sqli
299d3 74 65 33 56 64 62 65 52 65 73 6f 6c 76 65 4c 61  te3VdbeResolveLa
299d4 62 65 6c 28 76 2c 20 70 57 49 6e 66 6f 2d 3e 69  bel(v, pWInfo->i
299d5 42 72 65 61 6b 29 3b 0a 0a 20 20 2f 2a 20 43 6c  Break);..  /* Cl
299d6 6f 73 65 20 61 6c 6c 20 6f 66 20 74 68 65 20 63  ose all of the c
299d7 75 72 73 6f 72 73 20 74 68 61 74 20 77 65 72 65  ursors that were
299d8 20 6f 70 65 6e 65 64 20 62 79 20 73 71 6c 69 74   opened by sqlit
299d9 65 33 57 68 65 72 65 42 65 67 69 6e 2e 0a 20 20  e3WhereBegin..  
299da 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 2c 20 70 4c  */.  for(i=0, pL
299db 65 76 65 6c 3d 70 57 49 6e 66 6f 2d 3e 61 3b 20  evel=pWInfo->a; 
299dc 69 3c 70 54 61 62 4c 69 73 74 2d 3e 6e 53 72 63  i<pTabList->nSrc
299dd 3b 20 69 2b 2b 2c 20 70 4c 65 76 65 6c 2b 2b 29  ; i++, pLevel++)
299de 7b 0a 20 20 20 20 73 74 72 75 63 74 20 53 72 63  {.    struct Src
299df 4c 69 73 74 5f 69 74 65 6d 20 2a 70 54 61 62 49  List_item *pTabI
299e0 74 65 6d 20 3d 20 26 70 54 61 62 4c 69 73 74 2d  tem = &pTabList-
299e1 3e 61 5b 70 4c 65 76 65 6c 2d 3e 69 46 72 6f 6d  >a[pLevel->iFrom
299e2 5d 3b 0a 20 20 20 20 54 61 62 6c 65 20 2a 70 54  ];.    Table *pT
299e3 61 62 20 3d 20 70 54 61 62 49 74 65 6d 2d 3e 70  ab = pTabItem->p
299e4 54 61 62 3b 0a 20 20 20 20 61 73 73 65 72 74 28  Tab;.    assert(
299e5 20 70 54 61 62 21 3d 30 20 29 3b 0a 20 20 20 20   pTab!=0 );.    
299e6 69 66 28 20 28 70 54 61 62 2d 3e 74 61 62 46 6c  if( (pTab->tabFl
299e7 61 67 73 20 26 20 54 46 5f 45 70 68 65 6d 65 72  ags & TF_Ephemer
299e8 61 6c 29 21 3d 30 20 7c 7c 20 70 54 61 62 2d 3e  al)!=0 || pTab->
299e9 70 53 65 6c 65 63 74 20 29 20 63 6f 6e 74 69 6e  pSelect ) contin
299ea 75 65 3b 0a 20 20 20 20 69 66 28 20 21 70 57 49  ue;.    if( !pWI
299eb 6e 66 6f 2d 3e 6f 6b 4f 6e 65 50 61 73 73 20 26  nfo->okOnePass &
299ec 26 20 28 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73  & (pLevel->flags
299ed 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c   & WHERE_IDX_ONL
299ee 59 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 73  Y)==0 ){.      s
299ef 71 6c 69 74 65 33 56 64 62 65 41 64 64 4f 70 31  qlite3VdbeAddOp1
299f0 28 76 2c 20 4f 50 5f 43 6c 6f 73 65 2c 20 70 54  (v, OP_Close, pT
299f1 61 62 49 74 65 6d 2d 3e 69 43 75 72 73 6f 72 29  abItem->iCursor)
299f2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20  ;.    }.    if( 
299f3 70 4c 65 76 65 6c 2d 3e 70 49 64 78 21 3d 30 20  pLevel->pIdx!=0 
299f4 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
299f5 56 64 62 65 41 64 64 4f 70 31 28 76 2c 20 4f 50  VdbeAddOp1(v, OP
299f6 5f 43 6c 6f 73 65 2c 20 70 4c 65 76 65 6c 2d 3e  _Close, pLevel->
299f7 69 49 64 78 43 75 72 29 3b 0a 20 20 20 20 7d 0a  iIdxCur);.    }.
299f8 0a 20 20 20 20 2f 2a 20 49 66 20 74 68 69 73 20  .    /* If this 
299f9 73 63 61 6e 20 75 73 65 73 20 61 6e 20 69 6e 64  scan uses an ind
299fa 65 78 2c 20 6d 61 6b 65 20 63 6f 64 65 20 73 75  ex, make code su
299fb 62 73 74 69 74 75 74 69 6f 6e 73 20 74 6f 20 72  bstitutions to r
299fc 65 61 64 20 64 61 74 61 0a 20 20 20 20 2a 2a 20  ead data.    ** 
299fd 66 72 6f 6d 20 74 68 65 20 69 6e 64 65 78 20 69  from the index i
299fe 6e 20 70 72 65 66 65 72 65 6e 63 65 20 74 6f 20  n preference to 
299ff 74 68 65 20 74 61 62 6c 65 2e 20 53 6f 6d 65 74  the table. Somet
29a00 69 6d 65 73 2c 20 74 68 69 73 20 6d 65 61 6e 73  imes, this means
29a01 0a 20 20 20 20 2a 2a 20 74 68 65 20 74 61 62 6c  .    ** the tabl
29a02 65 20 6e 65 65 64 20 6e 65 76 65 72 20 62 65 20  e need never be 
29a03 72 65 61 64 20 66 72 6f 6d 2e 20 54 68 69 73 20  read from. This 
29a04 69 73 20 61 20 70 65 72 66 6f 72 6d 61 6e 63 65  is a performance
29a05 20 62 6f 6f 73 74 2c 0a 20 20 20 20 2a 2a 20 61   boost,.    ** a
29a06 73 20 74 68 65 20 76 64 62 65 20 6c 65 76 65 6c  s the vdbe level
29a07 20 77 61 69 74 73 20 75 6e 74 69 6c 20 74 68 65   waits until the
29a08 20 74 61 62 6c 65 20 69 73 20 72 65 61 64 20 62   table is read b
29a09 65 66 6f 72 65 20 61 63 74 75 61 6c 6c 79 0a 20  efore actually. 
29a0a 20 20 20 2a 2a 20 73 65 65 6b 69 6e 67 20 74 68     ** seeking th
29a0b 65 20 74 61 62 6c 65 20 63 75 72 73 6f 72 20 74  e table cursor t
29a0c 6f 20 74 68 65 20 72 65 63 6f 72 64 20 63 6f 72  o the record cor
29a0d 72 65 73 70 6f 6e 64 69 6e 67 20 74 6f 20 74 68  responding to th
29a0e 65 20 63 75 72 72 65 6e 74 0a 20 20 20 20 2a 2a  e current.    **
29a0f 20 70 6f 73 69 74 69 6f 6e 20 69 6e 20 74 68 65   position in the
29a10 20 69 6e 64 65 78 2e 0a 20 20 20 20 2a 2a 20 0a   index..    ** .
29a11 20 20 20 20 2a 2a 20 43 61 6c 6c 73 20 74 6f 20      ** Calls to 
29a12 74 68 65 20 63 6f 64 65 20 67 65 6e 65 72 61 74  the code generat
29a13 6f 72 20 69 6e 20 62 65 74 77 65 65 6e 20 73 71  or in between sq
29a14 6c 69 74 65 33 57 68 65 72 65 42 65 67 69 6e 20  lite3WhereBegin 
29a15 61 6e 64 0a 20 20 20 20 2a 2a 20 73 71 6c 69 74  and.    ** sqlit
29a16 65 33 57 68 65 72 65 45 6e 64 20 77 69 6c 6c 20  e3WhereEnd will 
29a17 68 61 76 65 20 63 72 65 61 74 65 64 20 63 6f 64  have created cod
29a18 65 20 74 68 61 74 20 72 65 66 65 72 65 6e 63 65  e that reference
29a19 73 20 74 68 65 20 74 61 62 6c 65 0a 20 20 20 20  s the table.    
29a1a 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 54 68  ** directly.  Th
29a1b 69 73 20 6c 6f 6f 70 20 73 63 61 6e 73 20 61 6c  is loop scans al
29a1c 6c 20 74 68 61 74 20 63 6f 64 65 20 6c 6f 6f 6b  l that code look
29a1d 69 6e 67 20 66 6f 72 20 6f 70 63 6f 64 65 73 0a  ing for opcodes.
29a1e 20 20 20 20 2a 2a 20 74 68 61 74 20 72 65 66 65      ** that refe
29a1f 72 65 6e 63 65 20 74 68 65 20 74 61 62 6c 65 20  rence the table 
29a20 61 6e 64 20 63 6f 6e 76 65 72 74 73 20 74 68 65  and converts the
29a21 6d 20 69 6e 74 6f 20 6f 70 63 6f 64 65 73 20 74  m into opcodes t
29a22 68 61 74 0a 20 20 20 20 2a 2a 20 72 65 66 65 72  hat.    ** refer
29a23 65 6e 63 65 20 74 68 65 20 69 6e 64 65 78 2e 0a  ence the index..
29a24 20 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 70      */.    if( p
29a25 4c 65 76 65 6c 2d 3e 70 49 64 78 20 29 7b 0a 20  Level->pIdx ){. 
29a26 20 20 20 20 20 69 6e 74 20 6b 2c 20 6a 2c 20 6c       int k, j, l
29a27 61 73 74 3b 0a 20 20 20 20 20 20 56 64 62 65 4f  ast;.      VdbeO
29a28 70 20 2a 70 4f 70 3b 0a 20 20 20 20 20 20 49 6e  p *pOp;.      In
29a29 64 65 78 20 2a 70 49 64 78 20 3d 20 70 4c 65 76  dex *pIdx = pLev
29a2a 65 6c 2d 3e 70 49 64 78 3b 0a 20 20 20 20 20 20  el->pIdx;.      
29a2b 69 6e 74 20 75 73 65 49 6e 64 65 78 4f 6e 6c 79  int useIndexOnly
29a2c 20 3d 20 70 4c 65 76 65 6c 2d 3e 66 6c 61 67 73   = pLevel->flags
29a2d 20 26 20 57 48 45 52 45 5f 49 44 58 5f 4f 4e 4c   & WHERE_IDX_ONL
29a2e 59 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74  Y;..      assert
29a2f 28 20 70 49 64 78 21 3d 30 20 29 3b 0a 20 20 20  ( pIdx!=0 );.   
29a30 20 20 20 70 4f 70 20 3d 20 73 71 6c 69 74 65 33     pOp = sqlite3
29a31 56 64 62 65 47 65 74 4f 70 28 76 2c 20 70 57 49  VdbeGetOp(v, pWI
29a32 6e 66 6f 2d 3e 69 54 6f 70 29 3b 0a 20 20 20 20  nfo->iTop);.    
29a33 20 20 6c 61 73 74 20 3d 20 73 71 6c 69 74 65 33    last = sqlite3
29a34 56 64 62 65 43 75 72 72 65 6e 74 41 64 64 72 28  VdbeCurrentAddr(
29a35 76 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 6b 3d  v);.      for(k=
29a36 70 57 49 6e 66 6f 2d 3e 69 54 6f 70 3b 20 6b 3c  pWInfo->iTop; k<
29a37 6c 61 73 74 3b 20 6b 2b 2b 2c 20 70 4f 70 2b 2b  last; k++, pOp++
29a38 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 70  ){.        if( p
29a39 4f 70 2d 3e 70 31 21 3d 70 4c 65 76 65 6c 2d 3e  Op->p1!=pLevel->
29a3a 69 54 61 62 43 75 72 20 29 20 63 6f 6e 74 69 6e  iTabCur ) contin
29a3b 75 65 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ue;.        if( 
29a3c 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
29a3d 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 20 20  Column ){.      
29a3e 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70      for(j=0; j<p
29a3f 49 64 78 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 6a 2b  Idx->nColumn; j+
29a40 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  +){.            
29a41 69 66 28 20 70 4f 70 2d 3e 70 32 3d 3d 70 49 64  if( pOp->p2==pId
29a42 78 2d 3e 61 69 43 6f 6c 75 6d 6e 5b 6a 5d 20 29  x->aiColumn[j] )
29a43 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
29a44 70 4f 70 2d 3e 70 32 20 3d 20 6a 3b 0a 20 20 20  pOp->p2 = j;.   
29a45 20 20 20 20 20 20 20 20 20 20 20 70 4f 70 2d 3e             pOp->
29a46 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e 69 49 64  p1 = pLevel->iId
29a47 78 43 75 72 3b 0a 20 20 20 20 20 20 20 20 20 20  xCur;.          
29a48 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
29a49 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
29a4a 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 61     }.          a
29a4b 73 73 65 72 74 28 21 75 73 65 49 6e 64 65 78 4f  ssert(!useIndexO
29a4c 6e 6c 79 20 7c 7c 20 6a 3c 70 49 64 78 2d 3e 6e  nly || j<pIdx->n
29a4d 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 20 20 20  Column);.       
29a4e 20 7d 65 6c 73 65 20 69 66 28 20 70 4f 70 2d 3e   }else if( pOp->
29a4f 6f 70 63 6f 64 65 3d 3d 4f 50 5f 52 6f 77 69 64  opcode==OP_Rowid
29a50 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 4f   ){.          pO
29a51 70 2d 3e 70 31 20 3d 20 70 4c 65 76 65 6c 2d 3e  p->p1 = pLevel->
29a52 69 49 64 78 43 75 72 3b 0a 20 20 20 20 20 20 20  iIdxCur;.       
29a53 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65 20 3d     pOp->opcode =
29a54 20 4f 50 5f 49 64 78 52 6f 77 69 64 3b 0a 20 20   OP_IdxRowid;.  
29a55 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
29a56 70 4f 70 2d 3e 6f 70 63 6f 64 65 3d 3d 4f 50 5f  pOp->opcode==OP_
29a57 4e 75 6c 6c 52 6f 77 20 26 26 20 75 73 65 49 6e  NullRow && useIn
29a58 64 65 78 4f 6e 6c 79 20 29 7b 0a 20 20 20 20 20  dexOnly ){.     
29a59 20 20 20 20 20 70 4f 70 2d 3e 6f 70 63 6f 64 65       pOp->opcode
29a5a 20 3d 20 4f 50 5f 4e 6f 6f 70 3b 0a 20 20 20 20   = OP_Noop;.    
29a5b 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
29a5c 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 46 69    }.  }..  /* Fi
29a5d 6e 61 6c 20 63 6c 65 61 6e 75 70 0a 20 20 2a 2f  nal cleanup.  */
29a5e 0a 20 20 77 68 65 72 65 49 6e 66 6f 46 72 65 65  .  whereInfoFree
29a5f 28 64 62 2c 20 70 57 49 6e 66 6f 29 3b 0a 20 20  (db, pWInfo);.  
29a60 72 65 74 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a  return;.}../****
29a61 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
29a62 66 20 77 68 65 72 65 2e 63 20 2a 2a 2a 2a 2a 2a  f where.c ******
29a63 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a65 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
29a66 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
29a67 20 66 69 6c 65 20 70 61 72 73 65 2e 63 20 2a 2a   file parse.c **
29a68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
29a6a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 44 72  *********/./* Dr
29a6b 69 76 65 72 20 74 65 6d 70 6c 61 74 65 20 66 6f  iver template fo
29a6c 72 20 74 68 65 20 4c 45 4d 4f 4e 20 70 61 72 73  r the LEMON pars
29a6d 65 72 20 67 65 6e 65 72 61 74 6f 72 2e 0a 2a 2a  er generator..**
29a6e 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63   The author disc
29a6f 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20  laims copyright 
29a70 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63  to this source c
29a71 6f 64 65 2e 0a 2a 2f 0a 2f 2a 20 46 69 72 73 74  ode..*/./* First
29a72 20 6f 66 66 2c 20 63 6f 64 65 20 69 73 20 69 6e   off, code is in
29a73 63 6c 75 64 65 64 20 74 68 61 74 20 66 6f 6c 6c  cluded that foll
29a74 6f 77 73 20 74 68 65 20 22 69 6e 63 6c 75 64 65  ows the "include
29a75 22 20 64 65 63 6c 61 72 61 74 69 6f 6e 0a 2a 2a  " declaration.**
29a76 20 69 6e 20 74 68 65 20 69 6e 70 75 74 20 67 72   in the input gr
29a77 61 6d 6d 61 72 20 66 69 6c 65 2e 20 2a 2f 0a 0a  ammar file. */..
29a78 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61 6e  ./*.** An instan
29a79 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75 63  ce of this struc
29a7a 74 75 72 65 20 68 6f 6c 64 73 20 69 6e 66 6f 72  ture holds infor
29a7b 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 74 68 65  mation about the
29a7c 0a 2a 2a 20 4c 49 4d 49 54 20 63 6c 61 75 73 65  .** LIMIT clause
29a7d 20 6f 66 20 61 20 53 45 4c 45 43 54 20 73 74 61   of a SELECT sta
29a7e 74 65 6d 65 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63  tement..*/.struc
29a7f 74 20 4c 69 6d 69 74 56 61 6c 20 7b 0a 20 20 45  t LimitVal {.  E
29a80 78 70 72 20 2a 70 4c 69 6d 69 74 3b 20 20 20 20  xpr *pLimit;    
29a81 2f 2a 20 54 68 65 20 4c 49 4d 49 54 20 65 78 70  /* The LIMIT exp
29a82 72 65 73 73 69 6f 6e 2e 20 20 4e 55 4c 4c 20 69  ression.  NULL i
29a83 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20 6c 69  f there is no li
29a84 6d 69 74 20 2a 2f 0a 20 20 45 78 70 72 20 2a 70  mit */.  Expr *p
29a85 4f 66 66 73 65 74 3b 20 20 20 2f 2a 20 54 68 65  Offset;   /* The
29a86 20 4f 46 46 53 45 54 20 65 78 70 72 65 73 73 69   OFFSET expressi
29a87 6f 6e 2e 20 20 4e 55 4c 4c 20 69 66 20 74 68 65  on.  NULL if the
29a88 72 65 20 69 73 20 6e 6f 6e 65 20 2a 2f 0a 7d 3b  re is none */.};
29a89 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
29a8a 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
29a8b 63 74 75 72 65 20 69 73 20 75 73 65 64 20 74 6f  cture is used to
29a8c 20 73 74 6f 72 65 20 74 68 65 20 4c 49 4b 45 2c   store the LIKE,
29a8d 0a 2a 2a 20 47 4c 4f 42 2c 20 4e 4f 54 20 4c 49  .** GLOB, NOT LI
29a8e 4b 45 2c 20 61 6e 64 20 4e 4f 54 20 47 4c 4f 42  KE, and NOT GLOB
29a8f 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a 73   operators..*/.s
29a90 74 72 75 63 74 20 4c 69 6b 65 4f 70 20 7b 0a 20  truct LikeOp {. 
29a91 20 54 6f 6b 65 6e 20 65 4f 70 65 72 61 74 6f 72   Token eOperator
29a92 3b 20 20 2f 2a 20 22 6c 69 6b 65 22 20 6f 72 20  ;  /* "like" or 
29a93 22 67 6c 6f 62 22 20 6f 72 20 22 72 65 67 65 78  "glob" or "regex
29a94 70 22 20 2a 2f 0a 20 20 69 6e 74 20 6e 6f 74 3b  p" */.  int not;
29a95 20 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65           /* True
29a96 20 69 66 20 74 68 65 20 4e 4f 54 20 6b 65 79 77   if the NOT keyw
29a97 6f 72 64 20 69 73 20 70 72 65 73 65 6e 74 20 2a  ord is present *
29a98 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69  /.};../*.** An i
29a99 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
29a9a 6f 6c 6c 6f 77 69 6e 67 20 73 74 72 75 63 74 75  ollowing structu
29a9b 72 65 20 64 65 73 63 72 69 62 65 73 20 74 68 65  re describes the
29a9c 20 65 76 65 6e 74 20 6f 66 20 61 0a 2a 2a 20 54   event of a.** T
29a9d 52 49 47 47 45 52 2e 20 20 22 61 22 20 69 73 20  RIGGER.  "a" is 
29a9e 74 68 65 20 65 76 65 6e 74 20 74 79 70 65 2c 20  the event type, 
29a9f 6f 6e 65 20 6f 66 20 54 4b 5f 55 50 44 41 54 45  one of TK_UPDATE
29aa0 2c 20 54 4b 5f 49 4e 53 45 52 54 2c 0a 2a 2a 20  , TK_INSERT,.** 
29aa1 54 4b 5f 44 45 4c 45 54 45 2c 20 6f 72 20 54 4b  TK_DELETE, or TK
29aa2 5f 49 4e 53 54 45 41 44 2e 20 20 49 66 20 74 68  _INSTEAD.  If th
29aa3 65 20 65 76 65 6e 74 20 69 73 20 6f 66 20 74 68  e event is of th
29aa4 65 20 66 6f 72 6d 0a 2a 2a 0a 2a 2a 20 20 20 20  e form.**.**    
29aa5 20 20 55 50 44 41 54 45 20 4f 4e 20 28 61 2c 62    UPDATE ON (a,b
29aa6 2c 63 29 0a 2a 2a 0a 2a 2a 20 54 68 65 6e 20 74  ,c).**.** Then t
29aa7 68 65 20 22 62 22 20 49 64 4c 69 73 74 20 72 65  he "b" IdList re
29aa8 63 6f 72 64 73 20 74 68 65 20 6c 69 73 74 20 22  cords the list "
29aa9 61 2c 62 2c 63 22 2e 0a 2a 2f 0a 73 74 72 75 63  a,b,c"..*/.struc
29aaa 74 20 54 72 69 67 45 76 65 6e 74 20 7b 20 69 6e  t TrigEvent { in
29aab 74 20 61 3b 20 49 64 4c 69 73 74 20 2a 20 62 3b  t a; IdList * b;
29aac 20 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e   };../*.** An in
29aad 73 74 61 6e 63 65 20 6f 66 20 74 68 69 73 20 73  stance of this s
29aae 74 72 75 63 74 75 72 65 20 68 6f 6c 64 73 20 74  tructure holds t
29aaf 68 65 20 41 54 54 41 43 48 20 6b 65 79 20 61 6e  he ATTACH key an
29ab0 64 20 74 68 65 20 6b 65 79 20 74 79 70 65 2e 0a  d the key type..
29ab1 2a 2f 0a 73 74 72 75 63 74 20 41 74 74 61 63 68  */.struct Attach
29ab2 4b 65 79 20 7b 20 69 6e 74 20 74 79 70 65 3b 20  Key { int type; 
29ab3 20 54 6f 6b 65 6e 20 6b 65 79 3b 20 7d 3b 0a 0a   Token key; };..
29ab4 2f 2a 20 4e 65 78 74 20 69 73 20 61 6c 6c 20 74  /* Next is all t
29ab5 6f 6b 65 6e 20 76 61 6c 75 65 73 2c 20 69 6e 20  oken values, in 
29ab6 61 20 66 6f 72 6d 20 73 75 69 74 61 62 6c 65 20  a form suitable 
29ab7 66 6f 72 20 75 73 65 20 62 79 20 6d 61 6b 65 68  for use by makeh
29ab8 65 61 64 65 72 73 2e 0a 2a 2a 20 54 68 69 73 20  eaders..** This 
29ab9 73 65 63 74 69 6f 6e 20 77 69 6c 6c 20 62 65 20  section will be 
29aba 6e 75 6c 6c 20 75 6e 6c 65 73 73 20 6c 65 6d 6f  null unless lemo
29abb 6e 20 69 73 20 72 75 6e 20 77 69 74 68 20 74 68  n is run with th
29abc 65 20 2d 6d 20 73 77 69 74 63 68 2e 0a 2a 2f 0a  e -m switch..*/.
29abd 2f 2a 20 0a 2a 2a 20 54 68 65 73 65 20 63 6f 6e  /* .** These con
29abe 73 74 61 6e 74 73 20 28 61 6c 6c 20 67 65 6e 65  stants (all gene
29abf 72 61 74 65 64 20 61 75 74 6f 6d 61 74 69 63 61  rated automatica
29ac0 6c 6c 79 20 62 79 20 74 68 65 20 70 61 72 73 65  lly by the parse
29ac1 72 20 67 65 6e 65 72 61 74 6f 72 29 0a 2a 2a 20  r generator).** 
29ac2 73 70 65 63 69 66 79 20 74 68 65 20 76 61 72 69  specify the vari
29ac3 6f 75 73 20 6b 69 6e 64 73 20 6f 66 20 74 6f 6b  ous kinds of tok
29ac4 65 6e 73 20 28 74 65 72 6d 69 6e 61 6c 73 29 20  ens (terminals) 
29ac5 74 68 61 74 20 74 68 65 20 70 61 72 73 65 72 0a  that the parser.
29ac6 2a 2a 20 75 6e 64 65 72 73 74 61 6e 64 73 2e 20  ** understands. 
29ac7 0a 2a 2a 0a 2a 2a 20 45 61 63 68 20 73 79 6d 62  .**.** Each symb
29ac8 6f 6c 20 68 65 72 65 20 69 73 20 61 20 74 65 72  ol here is a ter
29ac9 6d 69 6e 61 6c 20 73 79 6d 62 6f 6c 20 69 6e 20  minal symbol in 
29aca 74 68 65 20 67 72 61 6d 6d 61 72 2e 0a 2a 2f 0a  the grammar..*/.
29acb 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65  /* Make sure the
29acc 20 49 4e 54 45 52 46 41 43 45 20 6d 61 63 72 6f   INTERFACE macro
29acd 20 69 73 20 64 65 66 69 6e 65 64 2e 0a 2a 2f 0a   is defined..*/.
29ace 23 69 66 6e 64 65 66 20 49 4e 54 45 52 46 41 43  #ifndef INTERFAC
29acf 45 0a 23 20 64 65 66 69 6e 65 20 49 4e 54 45 52  E.# define INTER
29ad0 46 41 43 45 20 31 0a 23 65 6e 64 69 66 0a 2f 2a  FACE 1.#endif./*
29ad1 20 54 68 65 20 6e 65 78 74 20 74 68 69 6e 67 20   The next thing 
29ad2 69 6e 63 6c 75 64 65 64 20 69 73 20 73 65 72 69  included is seri
29ad3 65 73 20 6f 66 20 64 65 66 69 6e 65 73 20 77 68  es of defines wh
29ad4 69 63 68 20 63 6f 6e 74 72 6f 6c 0a 2a 2a 20 76  ich control.** v
29ad5 61 72 69 6f 75 73 20 61 73 70 65 63 74 73 20 6f  arious aspects o
29ad6 66 20 74 68 65 20 67 65 6e 65 72 61 74 65 64 20  f the generated 
29ad7 70 61 72 73 65 72 2e 0a 2a 2a 20 20 20 20 59 59  parser..**    YY
29ad8 43 4f 44 45 54 59 50 45 20 20 20 20 20 20 20 20  CODETYPE        
29ad9 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
29ada 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
29adb 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20  ng terminal.**  
29adc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29add 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d       and nonterm
29ade 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22  inal numbers.  "
29adf 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69  unsigned char" i
29ae0 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
29ae1 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
29ae2 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77  if there are few
29ae3 65 72 20 74 68 61 6e 20 32 35 30 20 74 65 72 6d  er than 250 term
29ae4 69 6e 61 6c 73 0a 2a 2a 20 20 20 20 20 20 20 20  inals.**        
29ae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 61                 a
29ae6 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 73 2e  nd nonterminals.
29ae7 20 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20    "int" is used 
29ae8 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20  otherwise..**   
29ae9 20 59 59 4e 4f 43 4f 44 45 20 20 20 20 20 20 20   YYNOCODE       
29aea 20 20 20 20 69 73 20 61 20 6e 75 6d 62 65 72 20      is a number 
29aeb 6f 66 20 74 79 70 65 20 59 59 43 4f 44 45 54 59  of type YYCODETY
29aec 50 45 20 77 68 69 63 68 20 63 6f 72 72 65 73 70  PE which corresp
29aed 6f 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20  onds.**         
29aee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
29aef 20 6e 6f 20 6c 65 67 61 6c 20 74 65 72 6d 69 6e   no legal termin
29af0 61 6c 20 6f 72 20 6e 6f 6e 74 65 72 6d 69 6e 61  al or nontermina
29af1 6c 20 6e 75 6d 62 65 72 2e 20 20 54 68 69 73 0a  l number.  This.
29af2 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
29af3 20 20 20 20 20 20 20 20 20 6e 75 6d 62 65 72 20           number 
29af4 69 73 20 75 73 65 64 20 74 6f 20 66 69 6c 6c 20  is used to fill 
29af5 69 6e 20 65 6d 70 74 79 20 73 6c 6f 74 73 20 6f  in empty slots o
29af6 66 20 74 68 65 20 68 61 73 68 20 0a 2a 2a 20 20  f the hash .**  
29af7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29af8 20 20 20 20 20 74 61 62 6c 65 2e 0a 2a 2a 20 20       table..**  
29af9 20 20 59 59 46 41 4c 4c 42 41 43 4b 20 20 20 20    YYFALLBACK    
29afa 20 20 20 20 20 49 66 20 64 65 66 69 6e 65 64 2c       If defined,
29afb 20 74 68 69 73 20 69 6e 64 69 63 61 74 65 73 20   this indicates 
29afc 74 68 61 74 20 6f 6e 65 20 6f 72 20 6d 6f 72 65  that one or more
29afd 20 74 6f 6b 65 6e 73 0a 2a 2a 20 20 20 20 20 20   tokens.**      
29afe 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29aff 20 68 61 76 65 20 66 61 6c 6c 2d 62 61 63 6b 20   have fall-back 
29b00 76 61 6c 75 65 73 20 77 68 69 63 68 20 73 68 6f  values which sho
29b01 75 6c 64 20 62 65 20 75 73 65 64 20 69 66 20 74  uld be used if t
29b02 68 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  he.**           
29b03 20 20 20 20 20 20 20 20 20 20 20 20 6f 72 69 67              orig
29b04 69 6e 61 6c 20 76 61 6c 75 65 20 6f 66 20 74 68  inal value of th
29b05 65 20 74 6f 6b 65 6e 20 77 69 6c 6c 20 6e 6f 74  e token will not
29b06 20 70 61 72 73 65 2e 0a 2a 2a 20 20 20 20 59 59   parse..**    YY
29b07 41 43 54 49 4f 4e 54 59 50 45 20 20 20 20 20 20  ACTIONTYPE      
29b08 20 69 73 20 74 68 65 20 64 61 74 61 20 74 79 70   is the data typ
29b09 65 20 75 73 65 64 20 66 6f 72 20 73 74 6f 72 69  e used for stori
29b0a 6e 67 20 74 65 72 6d 69 6e 61 6c 0a 2a 2a 20 20  ng terminal.**  
29b0b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b0c 20 20 20 20 20 61 6e 64 20 6e 6f 6e 74 65 72 6d       and nonterm
29b0d 69 6e 61 6c 20 6e 75 6d 62 65 72 73 2e 20 20 22  inal numbers.  "
29b0e 75 6e 73 69 67 6e 65 64 20 63 68 61 72 22 20 69  unsigned char" i
29b0f 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  s.**            
29b10 20 20 20 20 20 20 20 20 20 20 20 75 73 65 64 20             used 
29b11 69 66 20 74 68 65 72 65 20 61 72 65 20 66 65 77  if there are few
29b12 65 72 20 74 68 61 6e 20 32 35 30 20 72 75 6c 65  er than 250 rule
29b13 73 20 61 6e 64 0a 2a 2a 20 20 20 20 20 20 20 20  s and.**        
29b14 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
29b15 74 61 74 65 73 20 63 6f 6d 62 69 6e 65 64 2e 20  tates combined. 
29b16 20 22 69 6e 74 22 20 69 73 20 75 73 65 64 20 6f   "int" is used o
29b17 74 68 65 72 77 69 73 65 2e 0a 2a 2a 20 20 20 20  therwise..**    
29b18 73 71 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b  sqlite3ParserTOK
29b19 45 4e 54 59 50 45 20 20 20 20 20 69 73 20 74 68  ENTYPE     is th
29b1a 65 20 64 61 74 61 20 74 79 70 65 20 75 73 65 64  e data type used
29b1b 20 66 6f 72 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e   for minor token
29b1c 73 20 67 69 76 65 6e 20 0a 2a 2a 20 20 20 20 20  s given .**     
29b1d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b1e 20 20 64 69 72 65 63 74 6c 79 20 74 6f 20 74 68    directly to th
29b1f 65 20 70 61 72 73 65 72 20 66 72 6f 6d 20 74 68  e parser from th
29b20 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2a 20  e tokenizer..** 
29b21 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 20     YYMINORTYPE  
29b22 20 20 20 20 20 20 69 73 20 74 68 65 20 64 61 74        is the dat
29b23 61 20 74 79 70 65 20 75 73 65 64 20 66 6f 72 20  a type used for 
29b24 61 6c 6c 20 6d 69 6e 6f 72 20 74 6f 6b 65 6e 73  all minor tokens
29b25 2e 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  ..**            
29b26 20 20 20 20 20 20 20 20 20 20 20 54 68 69 73 20             This 
29b27 69 73 20 74 79 70 69 63 61 6c 6c 79 20 61 20 75  is typically a u
29b28 6e 69 6f 6e 20 6f 66 20 6d 61 6e 79 20 74 79 70  nion of many typ
29b29 65 73 2c 20 6f 6e 65 20 6f 66 0a 2a 2a 20 20 20  es, one of.**   
29b2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b2b 20 20 20 20 77 68 69 63 68 20 69 73 20 73 71 6c      which is sql
29b2c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
29b2d 59 50 45 2e 20 20 54 68 65 20 65 6e 74 72 79 20  YPE.  The entry 
29b2e 69 6e 20 74 68 65 20 75 6e 69 6f 6e 0a 2a 2a 20  in the union.** 
29b2f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29b30 20 20 20 20 20 20 66 6f 72 20 62 61 73 65 20 74        for base t
29b31 6f 6b 65 6e 73 20 69 73 20 63 61 6c 6c 65 64 20  okens is called 
29b32 22 79 79 30 22 2e 0a 2a 2a 20 20 20 20 59 59 53  "yy0"..**    YYS
29b33 54 41 43 4b 44 45 50 54 48 20 20 20 20 20 20 20  TACKDEPTH       
29b34 69 73 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 64  is the maximum d
29b35 65 70 74 68 20 6f 66 20 74 68 65 20 70 61 72 73  epth of the pars
29b36 65 72 27 73 20 73 74 61 63 6b 2e 20 20 49 66 0a  er's stack.  If.
29b37 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
29b38 20 20 20 20 20 20 20 20 20 7a 65 72 6f 20 74 68           zero th
29b39 65 20 73 74 61 63 6b 20 69 73 20 64 79 6e 61 6d  e stack is dynam
29b3a 69 63 61 6c 6c 79 20 73 69 7a 65 64 20 75 73 69  ically sized usi
29b3b 6e 67 20 72 65 61 6c 6c 6f 63 28 29 0a 2a 2a 20  ng realloc().** 
29b3c 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
29b3d 41 52 47 5f 53 44 45 43 4c 20 20 20 20 20 41 20  ARG_SDECL     A 
29b3e 73 74 61 74 69 63 20 76 61 72 69 61 62 6c 65 20  static variable 
29b3f 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72 20  declaration for 
29b40 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  the %extra_argum
29b41 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65  ent.**    sqlite
29b42 33 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c  3ParserARG_PDECL
29b43 20 20 20 20 20 41 20 70 61 72 61 6d 65 74 65 72       A parameter
29b44 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 66 6f 72   declaration for
29b45 20 74 68 65 20 25 65 78 74 72 61 5f 61 72 67 75   the %extra_argu
29b46 6d 65 6e 74 0a 2a 2a 20 20 20 20 73 71 6c 69 74  ment.**    sqlit
29b47 65 33 50 61 72 73 65 72 41 52 47 5f 53 54 4f 52  e3ParserARG_STOR
29b48 45 20 20 20 20 20 43 6f 64 65 20 74 6f 20 73 74  E     Code to st
29b49 6f 72 65 20 25 65 78 74 72 61 5f 61 72 67 75 6d  ore %extra_argum
29b4a 65 6e 74 20 69 6e 74 6f 20 79 79 70 50 61 72 73  ent into yypPars
29b4b 65 72 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  er.**    sqlite3
29b4c 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 20  ParserARG_FETCH 
29b4d 20 20 20 20 43 6f 64 65 20 74 6f 20 65 78 74 72      Code to extr
29b4e 61 63 74 20 25 65 78 74 72 61 5f 61 72 67 75 6d  act %extra_argum
29b4f 65 6e 74 20 66 72 6f 6d 20 79 79 70 50 61 72 73  ent from yypPars
29b50 65 72 0a 2a 2a 20 20 20 20 59 59 4e 53 54 41 54  er.**    YYNSTAT
29b51 45 20 20 20 20 20 20 20 20 20 20 20 74 68 65 20  E           the 
29b52 63 6f 6d 62 69 6e 65 64 20 6e 75 6d 62 65 72 20  combined number 
29b53 6f 66 20 73 74 61 74 65 73 2e 0a 2a 2a 20 20 20  of states..**   
29b54 20 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20   YYNRULE        
29b55 20 20 20 20 74 68 65 20 6e 75 6d 62 65 72 20 6f      the number o
29b56 66 20 72 75 6c 65 73 20 69 6e 20 74 68 65 20 67  f rules in the g
29b57 72 61 6d 6d 61 72 0a 2a 2a 20 20 20 20 59 59 45  rammar.**    YYE
29b58 52 52 4f 52 53 59 4d 42 4f 4c 20 20 20 20 20 20  RRORSYMBOL      
29b59 69 73 20 74 68 65 20 63 6f 64 65 20 6e 75 6d 62  is the code numb
29b5a 65 72 20 6f 66 20 74 68 65 20 65 72 72 6f 72 20  er of the error 
29b5b 73 79 6d 62 6f 6c 2e 20 20 49 66 20 6e 6f 74 0a  symbol.  If not.
29b5c 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  **              
29b5d 20 20 20 20 20 20 20 20 20 64 65 66 69 6e 65 64           defined
29b5e 2c 20 74 68 65 6e 20 64 6f 20 6e 6f 20 65 72 72  , then do no err
29b5f 6f 72 20 70 72 6f 63 65 73 73 69 6e 67 2e 0a 2a  or processing..*
29b60 2f 0a 23 64 65 66 69 6e 65 20 59 59 43 4f 44 45  /.#define YYCODE
29b61 54 59 50 45 20 75 6e 73 69 67 6e 65 64 20 63 68  TYPE unsigned ch
29b62 61 72 0a 23 64 65 66 69 6e 65 20 59 59 4e 4f 43  ar.#define YYNOC
29b63 4f 44 45 20 32 34 39 0a 23 64 65 66 69 6e 65 20  ODE 249.#define 
29b64 59 59 41 43 54 49 4f 4e 54 59 50 45 20 75 6e 73  YYACTIONTYPE uns
29b65 69 67 6e 65 64 20 73 68 6f 72 74 20 69 6e 74 0a  igned short int.
29b66 23 64 65 66 69 6e 65 20 59 59 57 49 4c 44 43 41  #define YYWILDCA
29b67 52 44 20 35 39 0a 23 64 65 66 69 6e 65 20 73 71  RD 59.#define sq
29b68 6c 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e  lite3ParserTOKEN
29b69 54 59 50 45 20 54 6f 6b 65 6e 0a 74 79 70 65 64  TYPE Token.typed
29b6a 65 66 20 75 6e 69 6f 6e 20 7b 0a 20 20 73 71 6c  ef union {.  sql
29b6b 69 74 65 33 50 61 72 73 65 72 54 4f 4b 45 4e 54  ite3ParserTOKENT
29b6c 59 50 45 20 79 79 30 3b 0a 20 20 53 65 6c 65 63  YPE yy0;.  Selec
29b6d 74 2a 20 79 79 34 33 3b 0a 20 20 54 72 69 67 67  t* yy43;.  Trigg
29b6e 65 72 53 74 65 70 2a 20 79 79 37 35 3b 0a 20 20  erStep* yy75;.  
29b6f 73 74 72 75 63 74 20 4c 69 6d 69 74 56 61 6c 20  struct LimitVal 
29b70 79 79 38 34 3b 0a 20 20 73 74 72 75 63 74 20 4c  yy84;.  struct L
29b71 69 6b 65 4f 70 20 79 79 38 36 3b 0a 20 20 73 74  ikeOp yy86;.  st
29b72 72 75 63 74 20 7b 69 6e 74 20 76 61 6c 75 65 3b  ruct {int value;
29b73 20 69 6e 74 20 6d 61 73 6b 3b 7d 20 79 79 32 30   int mask;} yy20
29b74 37 3b 0a 20 20 45 78 70 72 4c 69 73 74 2a 20 79  7;.  ExprList* y
29b75 79 32 34 32 3b 0a 20 20 69 6e 74 20 79 79 33 31  y242;.  int yy31
29b76 36 3b 0a 20 20 49 64 4c 69 73 74 2a 20 79 79 33  6;.  IdList* yy3
29b77 35 32 3b 0a 20 20 73 74 72 75 63 74 20 54 72 69  52;.  struct Tri
29b78 67 45 76 65 6e 74 20 79 79 33 35 34 3b 0a 20 20  gEvent yy354;.  
29b79 53 72 63 4c 69 73 74 2a 20 79 79 34 31 39 3b 0a  SrcList* yy419;.
29b7a 20 20 45 78 70 72 2a 20 79 79 34 35 30 3b 0a 7d    Expr* yy450;.}
29b7b 20 59 59 4d 49 4e 4f 52 54 59 50 45 3b 0a 23 69   YYMINORTYPE;.#i
29b7c 66 6e 64 65 66 20 59 59 53 54 41 43 4b 44 45 50  fndef YYSTACKDEP
29b7d 54 48 0a 23 64 65 66 69 6e 65 20 59 59 53 54 41  TH.#define YYSTA
29b7e 43 4b 44 45 50 54 48 20 31 30 30 0a 23 65 6e 64  CKDEPTH 100.#end
29b7f 69 66 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74  if.#define sqlit
29b80 65 33 50 61 72 73 65 72 41 52 47 5f 53 44 45 43  e3ParserARG_SDEC
29b81 4c 20 50 61 72 73 65 20 2a 70 50 61 72 73 65 3b  L Parse *pParse;
29b82 0a 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33  .#define sqlite3
29b83 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20  ParserARG_PDECL 
29b84 2c 50 61 72 73 65 20 2a 70 50 61 72 73 65 0a 23  ,Parse *pParse.#
29b85 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50 61  define sqlite3Pa
29b86 72 73 65 72 41 52 47 5f 46 45 54 43 48 20 50 61  rserARG_FETCH Pa
29b87 72 73 65 20 2a 70 50 61 72 73 65 20 3d 20 79 79  rse *pParse = yy
29b88 70 50 61 72 73 65 72 2d 3e 70 50 61 72 73 65 0a  pParser->pParse.
29b89 23 64 65 66 69 6e 65 20 73 71 6c 69 74 65 33 50  #define sqlite3P
29b8a 61 72 73 65 72 41 52 47 5f 53 54 4f 52 45 20 79  arserARG_STORE y
29b8b 79 70 50 61 72 73 65 72 2d 3e 70 50 61 72 73 65  ypParser->pParse
29b8c 20 3d 20 70 50 61 72 73 65 0a 23 64 65 66 69 6e   = pParse.#defin
29b8d 65 20 59 59 4e 53 54 41 54 45 20 35 39 38 0a 23  e YYNSTATE 598.#
29b8e 64 65 66 69 6e 65 20 59 59 4e 52 55 4c 45 20 33  define YYNRULE 3
29b8f 31 35 0a 23 64 65 66 69 6e 65 20 59 59 46 41 4c  15.#define YYFAL
29b90 4c 42 41 43 4b 20 31 0a 23 64 65 66 69 6e 65 20  LBACK 1.#define 
29b91 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 20 20 20 20  YY_NO_ACTION    
29b92 20 20 28 59 59 4e 53 54 41 54 45 2b 59 59 4e 52    (YYNSTATE+YYNR
29b93 55 4c 45 2b 32 29 0a 23 64 65 66 69 6e 65 20 59  ULE+2).#define Y
29b94 59 5f 41 43 43 45 50 54 5f 41 43 54 49 4f 4e 20  Y_ACCEPT_ACTION 
29b95 20 28 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55   (YYNSTATE+YYNRU
29b96 4c 45 2b 31 29 0a 23 64 65 66 69 6e 65 20 59 59  LE+1).#define YY
29b97 5f 45 52 52 4f 52 5f 41 43 54 49 4f 4e 20 20 20  _ERROR_ACTION   
29b98 28 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c  (YYNSTATE+YYNRUL
29b99 45 29 0a 0a 2f 2a 20 54 68 65 20 79 79 7a 65 72  E)../* The yyzer
29b9a 6f 6d 69 6e 6f 72 20 63 6f 6e 73 74 61 6e 74 20  ominor constant 
29b9b 69 73 20 75 73 65 64 20 74 6f 20 69 6e 69 74 69  is used to initi
29b9c 61 6c 69 7a 65 20 69 6e 73 74 61 6e 63 65 73 20  alize instances 
29b9d 6f 66 0a 2a 2a 20 59 59 4d 49 4e 4f 52 54 59 50  of.** YYMINORTYP
29b9e 45 20 6f 62 6a 65 63 74 73 20 74 6f 20 7a 65 72  E objects to zer
29b9f 6f 2e 20 2a 2f 0a 23 69 66 20 30 0a 73 74 61 74  o. */.#if 0.stat
29ba0 69 63 20 59 59 4d 49 4e 4f 52 54 59 50 45 20 79  ic YYMINORTYPE y
29ba1 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 23 65 6c 73  yzerominor;.#els
29ba2 65 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59  e.static const Y
29ba3 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 7a 65 72  YMINORTYPE yyzer
29ba4 6f 6d 69 6e 6f 72 3b 0a 23 65 6e 64 69 66 0a 0a  ominor;.#endif..
29ba5 2f 2a 20 4e 65 78 74 20 61 72 65 20 74 68 65 20  /* Next are the 
29ba6 74 61 62 6c 65 73 20 75 73 65 64 20 74 6f 20 64  tables used to d
29ba7 65 74 65 72 6d 69 6e 65 20 77 68 61 74 20 61 63  etermine what ac
29ba8 74 69 6f 6e 20 74 6f 20 74 61 6b 65 20 62 61 73  tion to take bas
29ba9 65 64 20 6f 6e 20 74 68 65 0a 2a 2a 20 63 75 72  ed on the.** cur
29baa 72 65 6e 74 20 73 74 61 74 65 20 61 6e 64 20 6c  rent state and l
29bab 6f 6f 6b 61 68 65 61 64 20 74 6f 6b 65 6e 2e 20  ookahead token. 
29bac 20 54 68 65 73 65 20 74 61 62 6c 65 73 20 61 72   These tables ar
29bad 65 20 75 73 65 64 20 74 6f 20 69 6d 70 6c 65 6d  e used to implem
29bae 65 6e 74 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 73  ent.** functions
29baf 20 74 68 61 74 20 74 61 6b 65 20 61 20 73 74 61   that take a sta
29bb0 74 65 20 6e 75 6d 62 65 72 20 61 6e 64 20 6c 6f  te number and lo
29bb1 6f 6b 61 68 65 61 64 20 76 61 6c 75 65 20 61 6e  okahead value an
29bb2 64 20 72 65 74 75 72 6e 20 61 6e 0a 2a 2a 20 61  d return an.** a
29bb3 63 74 69 6f 6e 20 69 6e 74 65 67 65 72 2e 20 20  ction integer.  
29bb4 0a 2a 2a 0a 2a 2a 20 53 75 70 70 6f 73 65 20 74  .**.** Suppose t
29bb5 68 65 20 61 63 74 69 6f 6e 20 69 6e 74 65 67 65  he action intege
29bb6 72 20 69 73 20 4e 2e 20 20 54 68 65 6e 20 74 68  r is N.  Then th
29bb7 65 20 61 63 74 69 6f 6e 20 69 73 20 64 65 74 65  e action is dete
29bb8 72 6d 69 6e 65 64 20 61 73 0a 2a 2a 20 66 6f 6c  rmined as.** fol
29bb9 6c 6f 77 73 0a 2a 2a 0a 2a 2a 20 20 20 30 20 3c  lows.**.**   0 <
29bba 3d 20 4e 20 3c 20 59 59 4e 53 54 41 54 45 20 20  = N < YYNSTATE  
29bbb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29bbc 53 68 69 66 74 20 4e 2e 20 20 54 68 61 74 20 69  Shift N.  That i
29bbd 73 2c 20 70 75 73 68 20 74 68 65 20 6c 6f 6f 6b  s, push the look
29bbe 61 68 65 61 64 0a 2a 2a 20 20 20 20 20 20 20 20  ahead.**        
29bbf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29bc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f                to
29bc1 6b 65 6e 20 6f 6e 74 6f 20 74 68 65 20 73 74 61  ken onto the sta
29bc2 63 6b 20 61 6e 64 20 67 6f 74 6f 20 73 74 61 74  ck and goto stat
29bc3 65 20 4e 2e 0a 2a 2a 0a 2a 2a 20 20 20 59 59 4e  e N..**.**   YYN
29bc4 53 54 41 54 45 20 3c 3d 20 4e 20 3c 20 59 59 4e  STATE <= N < YYN
29bc5 53 54 41 54 45 2b 59 59 4e 52 55 4c 45 20 20 20  STATE+YYNRULE   
29bc6 52 65 64 75 63 65 20 62 79 20 72 75 6c 65 20 4e  Reduce by rule N
29bc7 2d 59 59 4e 53 54 41 54 45 2e 0a 2a 2a 0a 2a 2a  -YYNSTATE..**.**
29bc8 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54 45     N == YYNSTATE
29bc9 2b 59 59 4e 52 55 4c 45 20 20 20 20 20 20 20 20  +YYNRULE        
29bca 20 20 20 20 20 20 41 20 73 79 6e 74 61 78 20 65        A syntax e
29bcb 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 72 65  rror has occurre
29bcc 64 2e 0a 2a 2a 0a 2a 2a 20 20 20 4e 20 3d 3d 20  d..**.**   N == 
29bcd 59 59 4e 53 54 41 54 45 2b 59 59 4e 52 55 4c 45  YYNSTATE+YYNRULE
29bce 2b 31 20 20 20 20 20 20 20 20 20 20 20 20 54 68  +1            Th
29bcf 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73  e parser accepts
29bd0 20 69 74 73 20 69 6e 70 75 74 2e 0a 2a 2a 0a 2a   its input..**.*
29bd1 2a 20 20 20 4e 20 3d 3d 20 59 59 4e 53 54 41 54  *   N == YYNSTAT
29bd2 45 2b 59 59 4e 52 55 4c 45 2b 32 20 20 20 20 20  E+YYNRULE+2     
29bd3 20 20 20 20 20 20 20 4e 6f 20 73 75 63 68 20 61         No such a
29bd4 63 74 69 6f 6e 2e 20 20 44 65 6e 6f 74 65 73 20  ction.  Denotes 
29bd5 75 6e 75 73 65 64 0a 2a 2a 20 20 20 20 20 20 20  unused.**       
29bd6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29bd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
29bd8 6c 6f 74 73 20 69 6e 20 74 68 65 20 79 79 5f 61  lots in the yy_a
29bd9 63 74 69 6f 6e 5b 5d 20 74 61 62 6c 65 2e 0a 2a  ction[] table..*
29bda 2a 0a 2a 2a 20 54 68 65 20 61 63 74 69 6f 6e 20  *.** The action 
29bdb 74 61 62 6c 65 20 69 73 20 63 6f 6e 73 74 72 75  table is constru
29bdc 63 74 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65  cted as a single
29bdd 20 6c 61 72 67 65 20 74 61 62 6c 65 20 6e 61 6d   large table nam
29bde 65 64 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 2e 0a  ed yy_action[]..
29bdf 2a 2a 20 47 69 76 65 6e 20 73 74 61 74 65 20 53  ** Given state S
29be0 20 61 6e 64 20 6c 6f 6f 6b 61 68 65 61 64 20 58   and lookahead X
29be1 2c 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  , the action is 
29be2 63 6f 6d 70 75 74 65 64 20 61 73 0a 2a 2a 0a 2a  computed as.**.*
29be3 2a 20 20 20 20 20 20 79 79 5f 61 63 74 69 6f 6e  *      yy_action
29be4 5b 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  [ yy_shift_ofst[
29be5 53 5d 20 2b 20 58 20 5d 0a 2a 2a 0a 2a 2a 20 49  S] + X ].**.** I
29be6 66 20 74 68 65 20 69 6e 64 65 78 20 76 61 6c 75  f the index valu
29be7 65 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b  e yy_shift_ofst[
29be8 53 5d 2b 58 20 69 73 20 6f 75 74 20 6f 66 20 72  S]+X is out of r
29be9 61 6e 67 65 20 6f 72 20 69 66 20 74 68 65 20 76  ange or if the v
29bea 61 6c 75 65 0a 2a 2a 20 79 79 5f 6c 6f 6f 6b 61  alue.** yy_looka
29beb 68 65 61 64 5b 79 79 5f 73 68 69 66 74 5f 6f 66  head[yy_shift_of
29bec 73 74 5b 53 5d 2b 58 5d 20 69 73 20 6e 6f 74 20  st[S]+X] is not 
29bed 65 71 75 61 6c 20 74 6f 20 58 20 6f 72 20 69 66  equal to X or if
29bee 20 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 53   yy_shift_ofst[S
29bef 5d 0a 2a 2a 20 69 73 20 65 71 75 61 6c 20 74 6f  ].** is equal to
29bf0 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46   YY_SHIFT_USE_DF
29bf1 4c 54 2c 20 69 74 20 6d 65 61 6e 73 20 74 68 61  LT, it means tha
29bf2 74 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73 20  t the action is 
29bf3 6e 6f 74 20 69 6e 20 74 68 65 20 74 61 62 6c 65  not in the table
29bf4 0a 2a 2a 20 61 6e 64 20 74 68 61 74 20 79 79 5f  .** and that yy_
29bf5 64 65 66 61 75 6c 74 5b 53 5d 20 73 68 6f 75 6c  default[S] shoul
29bf6 64 20 62 65 20 75 73 65 64 20 69 6e 73 74 65 61  d be used instea
29bf7 64 2e 20 20 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66  d.  .**.** The f
29bf8 6f 72 6d 75 6c 61 20 61 62 6f 76 65 20 69 73 20  ormula above is 
29bf9 66 6f 72 20 63 6f 6d 70 75 74 69 6e 67 20 74 68  for computing th
29bfa 65 20 61 63 74 69 6f 6e 20 77 68 65 6e 20 74 68  e action when th
29bfb 65 20 6c 6f 6f 6b 61 68 65 61 64 20 69 73 0a 2a  e lookahead is.*
29bfc 2a 20 61 20 74 65 72 6d 69 6e 61 6c 20 73 79 6d  * a terminal sym
29bfd 62 6f 6c 2e 20 20 49 66 20 74 68 65 20 6c 6f 6f  bol.  If the loo
29bfe 6b 61 68 65 61 64 20 69 73 20 61 20 6e 6f 6e 2d  kahead is a non-
29bff 74 65 72 6d 69 6e 61 6c 20 28 61 73 20 6f 63 63  terminal (as occ
29c00 75 72 73 20 61 66 74 65 72 0a 2a 2a 20 61 20 72  urs after.** a r
29c01 65 64 75 63 65 20 61 63 74 69 6f 6e 29 20 74 68  educe action) th
29c02 65 6e 20 74 68 65 20 79 79 5f 72 65 64 75 63 65  en the yy_reduce
29c03 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 69 73  _ofst[] array is
29c04 20 75 73 65 64 20 69 6e 20 70 6c 61 63 65 20 6f   used in place o
29c05 66 0a 2a 2a 20 74 68 65 20 79 79 5f 73 68 69 66  f.** the yy_shif
29c06 74 5f 6f 66 73 74 5b 5d 20 61 72 72 61 79 20 61  t_ofst[] array a
29c07 6e 64 20 59 59 5f 52 45 44 55 43 45 5f 55 53 45  nd YY_REDUCE_USE
29c08 5f 44 46 4c 54 20 69 73 20 75 73 65 64 20 69 6e  _DFLT is used in
29c09 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 59 59 5f   place of.** YY_
29c0a 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54 2e 0a  SHIFT_USE_DFLT..
29c0b 2a 2a 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77  **.** The follow
29c0c 69 6e 67 20 61 72 65 20 74 68 65 20 74 61 62 6c  ing are the tabl
29c0d 65 73 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20  es generated in 
29c0e 74 68 69 73 20 73 65 63 74 69 6f 6e 3a 0a 2a 2a  this section:.**
29c0f 0a 2a 2a 20 20 79 79 5f 61 63 74 69 6f 6e 5b 5d  .**  yy_action[]
29c10 20 20 20 20 20 20 20 20 41 20 73 69 6e 67 6c 65          A single
29c11 20 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 69 6e   table containin
29c12 67 20 61 6c 6c 20 61 63 74 69 6f 6e 73 2e 0a 2a  g all actions..*
29c13 2a 20 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b  *  yy_lookahead[
29c14 5d 20 20 20 20 20 41 20 74 61 62 6c 65 20 63 6f  ]     A table co
29c15 6e 74 61 69 6e 69 6e 67 20 74 68 65 20 6c 6f 6f  ntaining the loo
29c16 6b 61 68 65 61 64 20 66 6f 72 20 65 61 63 68 20  kahead for each 
29c17 65 6e 74 72 79 20 69 6e 0a 2a 2a 20 20 20 20 20  entry in.**     
29c18 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
29c19 79 79 5f 61 63 74 69 6f 6e 2e 20 20 55 73 65 64  yy_action.  Used
29c1a 20 74 6f 20 64 65 74 65 63 74 20 68 61 73 68 20   to detect hash 
29c1b 63 6f 6c 6c 69 73 69 6f 6e 73 2e 0a 2a 2a 20 20  collisions..**  
29c1c 79 79 5f 73 68 69 66 74 5f 6f 66 73 74 5b 5d 20  yy_shift_ofst[] 
29c1d 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61 74     For each stat
29c1e 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e  e, the offset in
29c1f 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72  to yy_action for
29c20 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
29c21 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e 67          shifting
29c22 20 74 65 72 6d 69 6e 61 6c 73 2e 0a 2a 2a 20 20   terminals..**  
29c23 79 79 5f 72 65 64 75 63 65 5f 6f 66 73 74 5b 5d  yy_reduce_ofst[]
29c24 20 20 20 46 6f 72 20 65 61 63 68 20 73 74 61 74     For each stat
29c25 65 2c 20 74 68 65 20 6f 66 66 73 65 74 20 69 6e  e, the offset in
29c26 74 6f 20 79 79 5f 61 63 74 69 6f 6e 20 66 6f 72  to yy_action for
29c27 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20 20  .**             
29c28 20 20 20 20 20 20 20 20 73 68 69 66 74 69 6e 67          shifting
29c29 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 73 20 61   non-terminals a
29c2a 66 74 65 72 20 61 20 72 65 64 75 63 65 2e 0a 2a  fter a reduce..*
29c2b 2a 20 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20  *  yy_default[] 
29c2c 20 20 20 20 20 20 44 65 66 61 75 6c 74 20 61 63        Default ac
29c2d 74 69 6f 6e 20 66 6f 72 20 65 61 63 68 20 73 74  tion for each st
29c2e 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  ate..*/.static c
29c2f 6f 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59 50  onst YYACTIONTYP
29c30 45 20 79 79 5f 61 63 74 69 6f 6e 5b 5d 20 3d 20  E yy_action[] = 
29c31 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20  {. /*     0 */  
29c32 20 32 39 36 2c 20 20 39 31 34 2c 20 20 31 32 30   296,  914,  120
29c33 2c 20 20 35 39 37 2c 20 20 20 20 32 2c 20 20 31  ,  597,    2,  1
29c34 37 32 2c 20 20 34 32 35 2c 20 20 34 32 35 2c 20  72,  425,  425, 
29c35 20 20 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20    62,   62,. /* 
29c36 20 20 20 31 30 20 2a 2f 20 20 20 20 36 32 2c 20     10 */    62, 
29c37 20 20 36 32 2c 20 20 32 31 30 2c 20 20 20 36 34    62,  210,   64
29c38 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
29c39 36 34 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20  64,   65,   65, 
29c3a 20 20 36 36 2c 0a 20 2f 2a 20 20 20 20 32 30 20    66,. /*    20 
29c3b 2a 2f 20 20 20 20 36 36 2c 20 20 20 36 36 2c 20  */    66,   66, 
29c3c 20 20 36 37 2c 20 20 32 31 32 2c 20 20 33 39 38    67,  212,  398
29c3d 2c 20 20 33 39 35 2c 20 20 34 33 32 2c 20 20 34  ,  395,  432,  4
29c3e 33 38 2c 20 20 20 36 39 2c 20 20 20 36 34 2c 0a  38,   69,   64,.
29c3f 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 20   /*    30 */    
29c40 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  64,   64,   64, 
29c41 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36    65,   65,   66
29c42 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20  ,   66,   66,   
29c43 36 37 2c 20 20 32 31 32 2c 0a 20 2f 2a 20 20 20  67,  212,. /*   
29c44 20 34 30 20 2a 2f 20 20 20 34 35 38 2c 20 20 34   40 */   458,  4
29c45 35 36 2c 20 20 33 32 37 2c 20 20 31 36 38 2c 20  56,  327,  168, 
29c46 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29c47 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29c48 33 39 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f  39,. /*    50 */
29c49 20 20 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20     439,   63,   
29c4a 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29c4b 20 20 36 32 2c 20 20 20 36 32 2c 20 20 32 35 36    62,   62,  256
29c4c 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f  ,   64,   64,. /
29c4d 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 20 36 34  *    60 */    64
29c4e 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29c4f 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29c50 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
29c51 2c 20 20 32 39 36 2c 0a 20 2f 2a 20 20 20 20 37  ,  296,. /*    7
29c52 30 20 2a 2f 20 20 20 34 39 38 2c 20 20 34 32 35  0 */   498,  425
29c53 2c 20 20 34 32 35 2c 20 20 32 31 32 2c 20 20 34  ,  425,  212,  4
29c54 32 37 2c 20 20 20 38 33 2c 20 20 20 36 38 2c 20  27,   83,   68, 
29c55 20 34 36 39 2c 20 20 20 37 30 2c 20 20 31 35 34   469,   70,  154
29c56 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20  ,. /*    80 */  
29c57 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34    64,   64,   64
29c58 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29c59 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29c5a 20 20 36 36 2c 20 20 20 36 37 2c 0a 20 2f 2a 20    66,   67,. /* 
29c5b 20 20 20 39 30 20 2a 2f 20 20 20 32 31 32 2c 20     90 */   212, 
29c5c 20 20 36 38 2c 20 20 33 30 37 2c 20 20 20 37 30    68,  307,   70
29c5d 2c 20 20 31 35 34 2c 20 20 34 33 32 2c 20 20 34  ,  154,  432,  4
29c5e 33 38 2c 20 20 34 35 34 2c 20 20 32 31 34 2c 20  38,  454,  214, 
29c5f 20 20 35 39 2c 0a 20 2f 2a 20 20 20 31 30 30 20    59,. /*   100 
29c60 2a 2f 20 20 20 20 36 35 2c 20 20 20 36 35 2c 20  */    65,   65, 
29c61 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36    66,   66,   66
29c62 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 20 20 34  ,   67,  212,  4
29c63 32 39 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 0a  29,  429,  429,.
29c64 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 34   /*   110 */   4
29c65 39 37 2c 20 20 35 38 33 2c 20 20 32 39 36 2c 20  97,  583,  296, 
29c66 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29c67 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29c68 33 39 2c 20 20 34 33 39 2c 0a 20 2f 2a 20 20 20  39,  439,. /*   
29c69 31 32 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20  120 */    63,   
29c6a 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29c6b 20 20 36 32 2c 20 20 20 36 32 2c 20 20 33 32 31    62,   62,  321
29c6c 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
29c6d 36 34 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f  64,. /*   130 */
29c6e 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20      64,   65,   
29c6f 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29c70 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
29c71 2c 20 20 34 33 32 2c 20 20 34 33 38 2c 0a 20 2f  ,  432,  438,. /
29c72 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 20 39 35  *   140 */    95
29c73 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20  ,   66,   66,   
29c74 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 20  66,   67,  212, 
29c75 20 34 30 33 2c 20 20 32 35 36 2c 20 20 34 32 31   403,  256,  421
29c76 2c 20 20 20 33 35 2c 0a 20 2f 2a 20 20 20 31 35  ,   35,. /*   15
29c77 30 20 2a 2f 20 20 20 20 35 37 2c 20 20 20 36 37  0 */    57,   67
29c78 2c 20 20 32 31 32 2c 20 20 31 37 35 2c 20 20 34  ,  212,  175,  4
29c79 31 37 2c 20 20 34 39 39 2c 20 20 20 36 31 2c 20  17,  499,   61, 
29c7a 20 20 36 30 2c 20 20 33 30 31 2c 20 20 34 34 32    60,  301,  442
29c7b 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20  ,. /*   160 */  
29c7c 20 34 34 33 2c 20 20 34 33 39 2c 20 20 34 33 39   443,  439,  439
29c7d 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20  ,   63,   63,   
29c7e 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
29c7f 20 20 36 32 2c 20 20 20 31 39 2c 0a 20 2f 2a 20    62,   19,. /* 
29c80 20 20 31 37 30 20 2a 2f 20 20 20 20 36 34 2c 20    170 */    64, 
29c81 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34    64,   64,   64
29c82 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
29c83 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  66,   66,   66, 
29c84 20 20 36 37 2c 0a 20 2f 2a 20 20 20 31 38 30 20    67,. /*   180 
29c85 2a 2f 20 20 20 32 31 32 2c 20 20 32 39 36 2c 20  */   212,  296, 
29c86 20 32 32 35 2c 20 20 35 33 32 2c 20 20 32 39 39   225,  532,  299
29c87 2c 20 20 35 38 31 2c 20 20 31 30 39 2c 20 20 34  ,  581,  109,  4
29c88 32 32 2c 20 20 32 34 32 2c 20 20 34 35 38 2c 0a  22,  242,  458,.
29c89 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 34   /*   190 */   4
29c8a 31 36 2c 20 20 33 33 35 2c 20 20 34 31 34 2c 20  16,  335,  414, 
29c8b 20 20 32 31 2c 20 20 35 30 32 2c 20 20 35 30 33    21,  502,  503
29c8c 2c 20 20 33 34 36 2c 20 20 34 30 33 2c 20 20 35  ,  346,  403,  5
29c8d 32 37 2c 20 20 31 37 36 2c 0a 20 2f 2a 20 20 20  27,  176,. /*   
29c8e 32 30 30 20 2a 2f 20 20 20 31 36 30 2c 20 20 34  200 */   160,  4
29c8f 35 34 2c 20 20 32 31 34 2c 20 20 35 38 30 2c 20  54,  214,  580, 
29c90 20 35 37 39 2c 20 20 33 34 34 2c 20 20 35 30 30   579,  344,  500
29c91 2c 20 20 34 33 32 2c 20 20 34 33 38 2c 20 20 31  ,  432,  438,  1
29c92 34 39 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f  49,. /*   210 */
29c93 20 20 20 31 35 30 2c 20 20 34 30 34 2c 20 20 34     150,  404,  4
29c94 30 35 2c 20 20 35 33 39 2c 20 20 35 31 34 2c 20  05,  539,  514, 
29c95 20 34 31 38 2c 20 20 31 35 31 2c 20 20 35 34 31   418,  151,  541
29c96 2c 20 20 20 20 38 2c 20 20 34 39 38 2c 0a 20 2f  ,    8,  498,. /
29c97 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 35 33 38  *   220 */   538
29c98 2c 20 20 35 37 37 2c 20 20 35 37 38 2c 20 20 34  ,  577,  578,  4
29c99 32 37 2c 20 20 32 39 36 2c 20 20 20 36 31 2c 20  27,  296,   61, 
29c9a 20 20 36 30 2c 20 20 33 30 31 2c 20 20 34 34 32    60,  301,  442
29c9b 2c 20 20 34 34 33 2c 0a 20 2f 2a 20 20 20 32 33  ,  443,. /*   23
29c9c 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 34 33 39  0 */   439,  439
29c9d 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20  ,   63,   63,   
29c9e 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
29c9f 20 20 36 32 2c 20 20 31 39 36 2c 20 20 20 36 34    62,  196,   64
29ca0 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20  ,. /*   240 */  
29ca1 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34    64,   64,   64
29ca2 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
29ca3 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  66,   66,   66, 
29ca4 20 20 36 37 2c 20 20 32 31 32 2c 0a 20 2f 2a 20    67,  212,. /* 
29ca5 20 20 32 35 30 20 2a 2f 20 20 20 34 33 32 2c 20    250 */   432, 
29ca6 20 34 33 38 2c 20 20 34 35 34 2c 20 20 35 39 38   438,  454,  598
29ca7 2c 20 20 33 39 38 2c 20 20 33 39 35 2c 20 20 34  ,  398,  395,  4
29ca8 32 39 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20  29,  429,  429, 
29ca9 20 33 36 39 2c 0a 20 2f 2a 20 20 20 32 36 30 20   369,. /*   260 
29caa 2a 2f 20 20 20 35 35 38 2c 20 20 34 38 31 2c 20  */   558,  481, 
29cab 20 34 30 34 2c 20 20 34 30 35 2c 20 20 33 37 32   404,  405,  372
29cac 2c 20 20 35 37 36 2c 20 20 32 31 33 2c 20 20 32  ,  576,  213,  2
29cad 39 36 2c 20 20 20 36 31 2c 20 20 20 36 30 2c 0a  96,   61,   60,.
29cae 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 33   /*   270 */   3
29caf 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20  01,  442,  443, 
29cb0 20 34 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33   439,  439,   63
29cb1 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
29cb2 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20 20  62,   62,. /*   
29cb3 32 38 30 20 2a 2f 20 20 20 20 36 32 2c 20 20 33  280 */    62,  3
29cb4 32 31 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  21,   64,   64, 
29cb5 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
29cb6 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
29cb7 36 36 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f  66,. /*   290 */
29cb8 20 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32      66,   67,  2
29cb9 31 32 2c 20 20 34 33 32 2c 20 20 34 33 38 2c 20  12,  432,  438, 
29cba 20 35 35 35 2c 20 20 35 30 33 2c 20 20 33 30 34   555,  503,  304
29cbb 2c 20 20 35 35 37 2c 20 20 35 33 32 2c 0a 20 2f  ,  557,  532,. /
29cbc 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 32 31 38  *   300 */   218
29cbd 2c 20 20 35 35 37 2c 20 20 35 35 32 2c 20 20 34  ,  557,  552,  4
29cbe 32 31 2c 20 20 20 33 36 2c 20 20 32 33 34 2c 20  21,   36,  234, 
29cbf 20 33 39 37 2c 20 20 20 20 32 2c 20 20 35 34 32   397,    2,  542
29cc0 2c 20 20 20 32 31 2c 0a 20 2f 2a 20 20 20 33 31  ,   21,. /*   31
29cc1 30 20 2a 2f 20 20 20 35 34 30 2c 20 20 20 36 31  0 */   540,   61
29cc2 2c 20 20 20 36 30 2c 20 20 33 30 31 2c 20 20 34  ,   60,  301,  4
29cc3 34 32 2c 20 20 34 34 33 2c 20 20 34 33 39 2c 20  42,  443,  439, 
29cc4 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20 36 33   439,   63,   63
29cc5 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20  ,. /*   320 */  
29cc6 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32    62,   62,   62
29cc7 2c 20 20 20 36 32 2c 20 20 33 38 38 2c 20 20 20  ,   62,  388,   
29cc8 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  64,   64,   64, 
29cc9 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f 2a 20    64,   65,. /* 
29cca 20 20 33 33 30 20 2a 2f 20 20 20 20 36 35 2c 20    330 */    65, 
29ccb 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36    66,   66,   66
29ccc 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 20 20 34  ,   67,  212,  4
29ccd 31 35 2c 20 20 35 33 30 2c 20 20 20 38 35 2c 20  15,  530,   85, 
29cce 20 33 38 31 2c 0a 20 2f 2a 20 20 20 33 34 30 20   381,. /*   340 
29ccf 2a 2f 20 20 20 20 37 38 2c 20 20 33 32 33 2c 20  */    78,  323, 
29cd0 20 32 39 36 2c 20 20 32 31 30 2c 20 20 33 30 34   296,  210,  304
29cd1 2c 20 20 35 32 37 2c 20 20 34 39 33 2c 20 20 34  ,  527,  493,  4
29cd2 39 32 2c 20 20 33 37 39 2c 20 20 32 37 34 2c 0a  92,  379,  274,.
29cd3 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 32   /*   350 */   2
29cd4 37 33 2c 20 20 33 37 39 2c 20 20 32 37 34 2c 20  73,  379,  274, 
29cd5 20 32 37 33 2c 20 20 33 34 37 2c 20 20 34 36 33   273,  347,  463
29cd6 2c 20 20 32 34 31 2c 20 20 33 38 37 2c 20 20 32  ,  241,  387,  2
29cd7 36 38 2c 20 20 32 31 30 2c 0a 20 2f 2a 20 20 20  68,  210,. /*   
29cd8 33 36 30 20 2a 2f 20 20 20 35 33 33 2c 20 20 35  360 */   533,  5
29cd9 38 31 2c 20 20 32 31 30 2c 20 20 34 30 33 2c 20  81,  210,  403, 
29cda 20 20 32 30 2c 20 20 32 32 34 2c 20 20 31 34 34    20,  224,  144
29cdb 2c 20 20 34 36 34 2c 20 20 34 33 32 2c 20 20 34  ,  464,  432,  4
29cdc 33 38 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f  38,. /*   370 */
29cdd 20 20 20 34 38 35 2c 20 20 31 36 34 2c 20 20 31     485,  164,  1
29cde 31 34 2c 20 20 32 34 38 2c 20 20 33 34 39 2c 20  14,  248,  349, 
29cdf 20 32 35 33 2c 20 20 33 35 30 2c 20 20 31 37 37   253,  350,  177
29ce0 2c 20 20 35 35 34 2c 20 20 35 38 30 2c 0a 20 2f  ,  554,  580,. /
29ce1 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 34 36 35  *   380 */   465
29ce2 2c 20 20 34 32 30 2c 20 20 33 33 31 2c 20 20 20  ,  420,  331,   
29ce3 38 31 2c 20 20 32 35 37 2c 20 20 34 31 39 2c 20  81,  257,  419, 
29ce4 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29ce5 2c 20 20 34 34 32 2c 0a 20 2f 2a 20 20 20 33 39  ,  442,. /*   39
29ce6 30 20 2a 2f 20 20 20 34 34 33 2c 20 20 34 33 39  0 */   443,  439
29ce7 2c 20 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20  ,  439,   63,   
29ce8 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29ce9 20 20 36 32 2c 20 20 20 36 32 2c 20 20 33 39 31    62,   62,  391
29cea 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20  ,. /*   400 */  
29ceb 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34    64,   64,   64
29cec 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29ced 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29cee 20 20 36 36 2c 20 20 20 36 37 2c 0a 20 2f 2a 20    66,   67,. /* 
29cef 20 20 34 31 30 20 2a 2f 20 20 20 32 31 32 2c 20    410 */   212, 
29cf0 20 32 39 36 2c 20 20 32 32 34 2c 20 20 32 30 33   296,  224,  203
29cf1 2c 20 20 32 34 39 2c 20 20 34 39 36 2c 20 20 34  ,  249,  496,  4
29cf2 30 33 2c 20 20 34 34 30 2c 20 20 38 33 37 2c 20  03,  440,  837, 
29cf3 20 31 31 34 2c 0a 20 2f 2a 20 20 20 34 32 30 20   114,. /*   420 
29cf4 2a 2f 20 20 20 32 34 38 2c 20 20 33 34 39 2c 20  */   248,  349, 
29cf5 20 32 35 33 2c 20 20 33 35 30 2c 20 20 31 37 37   253,  350,  177
29cf6 2c 20 20 32 35 30 2c 20 20 33 32 31 2c 20 20 31  ,  250,  321,  1
29cf7 35 32 2c 20 20 34 30 34 2c 20 20 34 30 35 2c 0a  52,  404,  405,.
29cf8 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 33   /*   430 */   3
29cf9 32 31 2c 20 20 32 35 37 2c 20 20 33 30 33 2c 20  21,  257,  303, 
29cfa 20 33 32 34 2c 20 20 31 35 35 2c 20 20 34 34 35   324,  155,  445
29cfb 2c 20 20 34 34 35 2c 20 20 34 33 32 2c 20 20 34  ,  445,  432,  4
29cfc 33 38 2c 20 20 33 31 37 2c 0a 20 2f 2a 20 20 20  38,  317,. /*   
29cfd 34 34 30 20 2a 2f 20 20 20 34 30 30 2c 20 20 33  440 */   400,  3
29cfe 38 39 2c 20 20 32 31 33 2c 20 20 20 36 38 2c 20  89,  213,   68, 
29cff 20 32 30 39 2c 20 20 20 37 30 2c 20 20 31 35 34   209,   70,  154
29d00 2c 20 20 34 32 32 2c 20 20 34 32 31 2c 20 20 20  ,  422,  421,   
29d01 33 35 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f  35,. /*   450 */
29d02 20 20 20 33 39 33 2c 20 20 32 30 32 2c 20 20 34     393,  202,  4
29d03 32 31 2c 20 20 20 34 32 2c 20 20 34 38 31 2c 20  21,   42,  481, 
29d04 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29d05 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 0a 20 2f  ,  442,  443,. /
29d06 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 34 33 39  *   460 */   439
29d07 2c 20 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20  ,  439,   63,   
29d08 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29d09 20 20 36 32 2c 20 20 20 36 32 2c 20 20 34 32 32    62,   62,  422
29d0a 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 34 37  ,   64,. /*   47
29d0b 30 20 2a 2f 20 20 20 20 36 34 2c 20 20 20 36 34  0 */    64,   64
29d0c 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29d0d 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29d0e 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
29d0f 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20  ,. /*   480 */  
29d10 20 32 39 36 2c 20 20 34 30 34 2c 20 20 34 30 35   296,  404,  405
29d11 2c 20 20 31 38 33 2c 20 20 35 31 33 2c 20 20 34  ,  183,  513,  4
29d12 32 32 2c 20 20 33 35 31 2c 20 20 33 35 34 2c 20  22,  351,  354, 
29d13 20 33 35 35 2c 20 20 34 30 33 2c 0a 20 2f 2a 20   355,  403,. /* 
29d14 20 20 34 39 30 20 2a 2f 20 20 20 20 37 37 2c 20    490 */    77, 
29d15 20 33 33 35 2c 20 20 20 37 39 2c 20 20 34 38 39   335,   79,  489
29d16 2c 20 20 32 31 36 2c 20 20 31 38 33 2c 20 20 33  ,  216,  183,  3
29d17 33 34 2c 20 20 33 35 36 2c 20 20 33 35 31 2c 20  34,  356,  351, 
29d18 20 33 35 34 2c 0a 20 2f 2a 20 20 20 35 30 30 20   354,. /*   500 
29d19 2a 2f 20 20 20 33 35 35 2c 20 20 34 33 33 2c 20  */   355,  433, 
29d1a 20 34 33 34 2c 20 20 34 30 36 2c 20 20 34 30 37   434,  406,  407
29d1b 2c 20 20 34 30 38 2c 20 20 34 33 32 2c 20 20 34  ,  408,  432,  4
29d1c 33 38 2c 20 20 32 33 35 2c 20 20 33 35 36 2c 0a  38,  235,  356,.
29d1d 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 33   /*   510 */   3
29d1e 38 36 2c 20 20 20 36 38 2c 20 20 32 39 31 2c 20  86,   68,  291, 
29d1f 20 20 37 30 2c 20 20 31 35 34 2c 20 20 34 35 36    70,  154,  456
29d20 2c 20 20 35 33 31 2c 20 20 31 36 38 2c 20 20 31  ,  531,  168,  1
29d21 39 38 2c 20 20 33 30 32 2c 0a 20 2f 2a 20 20 20  98,  302,. /*   
29d22 35 32 30 20 2a 2f 20 20 20 34 34 39 2c 20 20 34  520 */   449,  4
29d23 35 30 2c 20 20 34 33 36 2c 20 20 34 33 37 2c 20  50,  436,  437, 
29d24 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29d25 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29d26 33 39 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f  39,. /*   530 */
29d27 20 20 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20     439,   63,   
29d28 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29d29 20 20 36 32 2c 20 20 20 36 32 2c 20 20 33 39 34    62,   62,  394
29d2a 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 0a 20 2f  ,   64,   64,. /
29d2b 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 20 36 34  *   540 */    64
29d2c 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29d2d 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29d2e 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
29d2f 2c 20 20 32 39 36 2c 0a 20 2f 2a 20 20 20 35 35  ,  296,. /*   55
29d30 30 20 2a 2f 20 20 20 33 32 31 2c 20 20 34 33 35  0 */   321,  435
29d31 2c 20 20 34 32 32 2c 20 20 32 36 30 2c 20 20 34  ,  422,  260,  4
29d32 30 34 2c 20 20 34 30 35 2c 20 20 33 32 31 2c 20  04,  405,  321, 
29d33 20 31 38 33 2c 20 20 31 35 33 2c 20 20 33 32 31   183,  153,  321
29d34 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20  ,. /*   560 */  
29d35 20 33 35 31 2c 20 20 33 35 34 2c 20 20 33 35 35   351,  354,  355
29d36 2c 20 20 34 34 36 2c 20 20 33 33 32 2c 20 20 33  ,  446,  332,  3
29d37 32 31 2c 20 20 35 39 35 2c 20 20 39 30 35 2c 20  21,  595,  905, 
29d38 20 33 32 31 2c 20 20 39 30 35 2c 0a 20 2f 2a 20   321,  905,. /* 
29d39 20 20 35 37 30 20 2a 2f 20 20 20 20 20 31 2c 20    570 */     1, 
29d3a 20 33 35 36 2c 20 20 34 32 31 2c 20 20 20 32 38   356,  421,   28
29d3b 2c 20 20 34 30 33 2c 20 20 34 33 32 2c 20 20 34  ,  403,  432,  4
29d3c 33 38 2c 20 20 33 37 36 2c 20 20 34 32 31 2c 20  38,  376,  421, 
29d3d 20 20 34 32 2c 0a 20 2f 2a 20 20 20 35 38 30 20    42,. /*   580 
29d3e 2a 2f 20 20 20 34 37 37 2c 20 20 34 32 31 2c 20  */   477,  421, 
29d3f 20 20 33 35 2c 20 20 32 31 33 2c 20 20 35 34 38    35,  213,  548
29d40 2c 20 20 33 36 36 2c 20 20 35 34 38 2c 20 20 34  ,  366,  548,  4
29d41 32 31 2c 20 20 20 35 30 2c 20 20 31 35 39 2c 0a  21,   50,  159,.
29d42 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 34   /*   590 */   4
29d43 32 31 2c 20 20 20 35 30 2c 20 20 34 32 32 2c 20  21,   50,  422, 
29d44 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29d45 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29d46 33 39 2c 20 20 34 33 39 2c 0a 20 2f 2a 20 20 20  39,  439,. /*   
29d47 36 30 30 20 2a 2f 20 20 20 20 36 33 2c 20 20 20  600 */    63,   
29d48 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29d49 20 20 36 32 2c 20 20 20 36 32 2c 20 20 35 39 32    62,   62,  592
29d4a 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
29d4b 36 34 2c 0a 20 2f 2a 20 20 20 36 31 30 20 2a 2f  64,. /*   610 */
29d4c 20 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20      64,   65,   
29d4d 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29d4e 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
29d4f 2c 20 20 32 39 36 2c 20 20 33 33 37 2c 0a 20 2f  ,  296,  337,. /
29d50 2a 20 20 20 36 32 30 20 2a 2f 20 20 20 32 31 37  *   620 */   217
29d51 2c 20 20 34 36 33 2c 20 20 32 35 36 2c 20 20 20  ,  463,  256,   
29d52 39 34 2c 20 20 33 33 39 2c 20 20 33 32 36 2c 20  94,  339,  326, 
29d53 20 34 34 39 2c 20 20 34 35 30 2c 20 20 31 37 32   449,  450,  172
29d54 2c 20 20 33 34 30 2c 0a 20 2f 2a 20 20 20 36 33  ,  340,. /*   63
29d55 30 20 2a 2f 20 20 20 34 32 35 2c 20 20 33 34 35  0 */   425,  345
29d56 2c 20 20 35 33 32 2c 20 20 34 36 34 2c 20 20 33  ,  532,  464,  3
29d57 31 32 2c 20 20 35 39 35 2c 20 20 39 30 34 2c 20  12,  595,  904, 
29d58 20 33 31 33 2c 20 20 39 30 34 2c 20 20 34 30 34   313,  904,  404
29d59 2c 0a 20 2f 2a 20 20 20 36 34 30 20 2a 2f 20 20  ,. /*   640 */  
29d5a 20 34 30 35 2c 20 20 35 38 38 2c 20 20 20 32 31   405,  588,   21
29d5b 2c 20 20 32 32 36 2c 20 20 34 33 32 2c 20 20 34  ,  226,  432,  4
29d5c 33 38 2c 20 20 34 36 35 2c 20 20 32 34 33 2c 20  38,  465,  243, 
29d5d 20 35 30 34 2c 20 20 33 32 34 2c 0a 20 2f 2a 20   504,  324,. /* 
29d5e 20 20 36 35 30 20 2a 2f 20 20 20 33 32 32 2c 20    650 */   322, 
29d5f 20 34 34 35 2c 20 20 34 34 35 2c 20 20 34 32 31   445,  445,  421
29d60 2c 20 20 20 20 33 2c 20 20 34 35 39 2c 20 20 32  ,    3,  459,  2
29d61 33 30 2c 20 20 33 30 38 2c 20 20 35 30 35 2c 20  30,  308,  505, 
29d62 20 31 39 34 2c 0a 20 2f 2a 20 20 20 36 36 30 20   194,. /*   660 
29d63 2a 2f 20 20 20 32 37 38 2c 20 20 32 39 36 2c 20  */   278,  296, 
29d64 20 20 36 31 2c 20 20 20 36 30 2c 20 20 33 30 31    61,   60,  301
29d65 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20 20 34  ,  442,  443,  4
29d66 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33 2c 0a  39,  439,   63,.
29d67 20 2f 2a 20 20 20 36 37 30 20 2a 2f 20 20 20 20   /*   670 */    
29d68 36 33 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  63,   62,   62, 
29d69 20 20 36 32 2c 20 20 20 36 32 2c 20 20 35 39 32    62,   62,  592
29d6a 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20  ,   64,   64,   
29d6b 36 34 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20  64,   64,. /*   
29d6c 36 38 30 20 2a 2f 20 20 20 20 36 35 2c 20 20 20  680 */    65,   
29d6d 36 35 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  65,   66,   66, 
29d6e 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32 31 32    66,   67,  212
29d6f 2c 20 20 34 33 32 2c 20 20 34 33 38 2c 20 20 32  ,  432,  438,  2
29d70 31 33 2c 0a 20 2f 2a 20 20 20 36 39 30 20 2a 2f  13,. /*   690 */
29d71 20 20 20 31 37 39 2c 20 20 31 38 30 2c 20 20 31     179,  180,  1
29d72 38 31 2c 20 20 34 32 32 2c 20 20 33 32 34 2c 20  81,  422,  324, 
29d73 20 34 32 35 2c 20 20 34 34 35 2c 20 20 34 34 35   425,  445,  445
29d74 2c 20 20 32 38 31 2c 20 20 32 36 32 2c 0a 20 2f  ,  281,  262,. /
29d75 2a 20 20 20 37 30 30 20 2a 2f 20 20 20 32 37 39  *   700 */   279
29d76 2c 20 20 34 30 32 2c 20 20 31 39 34 2c 20 20 34  ,  402,  194,  4
29d77 38 31 2c 20 20 32 39 36 2c 20 20 20 36 31 2c 20  81,  296,   61, 
29d78 20 20 36 30 2c 20 20 33 30 31 2c 20 20 34 34 32    60,  301,  442
29d79 2c 20 20 34 34 33 2c 0a 20 2f 2a 20 20 20 37 31  ,  443,. /*   71
29d7a 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 34 33 39  0 */   439,  439
29d7b 2c 20 20 20 36 33 2c 20 20 20 36 33 2c 20 20 20  ,   63,   63,   
29d7c 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32 2c 20  62,   62,   62, 
29d7d 20 20 36 32 2c 20 20 33 37 37 2c 20 20 20 36 34    62,  377,   64
29d7e 2c 0a 20 2f 2a 20 20 20 37 32 30 20 2a 2f 20 20  ,. /*   720 */  
29d7f 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34    64,   64,   64
29d80 2c 20 20 20 36 35 2c 20 20 20 36 35 2c 20 20 20  ,   65,   65,   
29d81 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36 2c 20  66,   66,   66, 
29d82 20 20 36 37 2c 20 20 32 31 32 2c 0a 20 2f 2a 20    67,  212,. /* 
29d83 20 20 37 33 30 20 2a 2f 20 20 20 34 33 32 2c 20    730 */   432, 
29d84 20 34 33 38 2c 20 20 35 39 31 2c 20 20 32 39 35   438,  591,  295
29d85 2c 20 20 31 31 35 2c 20 20 32 36 38 2c 20 20 34  ,  115,  268,  4
29d86 32 32 2c 20 20 32 36 36 2c 20 20 32 31 31 2c 20  22,  266,  211, 
29d87 20 32 36 34 2c 0a 20 2f 2a 20 20 20 37 34 30 20   264,. /*   740 
29d88 2a 2f 20 20 20 33 37 33 2c 20 20 33 32 34 2c 20  */   373,  324, 
29d89 20 32 34 36 2c 20 20 34 34 35 2c 20 20 34 34 35   246,  445,  445
29d8a 2c 20 20 20 35 36 2c 20 20 32 35 36 2c 20 20 32  ,   56,  256,  2
29d8b 39 36 2c 20 20 20 36 31 2c 20 20 20 37 31 2c 0a  96,   61,   71,.
29d8c 20 2f 2a 20 20 20 37 35 30 20 2a 2f 20 20 20 33   /*   750 */   3
29d8d 30 31 2c 20 20 34 34 32 2c 20 20 34 34 33 2c 20  01,  442,  443, 
29d8e 20 34 33 39 2c 20 20 34 33 39 2c 20 20 20 36 33   439,  439,   63
29d8f 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20 20 20  ,   63,   62,   
29d90 36 32 2c 20 20 20 36 32 2c 0a 20 2f 2a 20 20 20  62,   62,. /*   
29d91 37 36 30 20 2a 2f 20 20 20 20 36 32 2c 20 20 33  760 */    62,  3
29d92 37 37 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  77,   64,   64, 
29d93 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20 36 35    64,   64,   65
29d94 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20  ,   65,   66,   
29d95 36 36 2c 0a 20 2f 2a 20 20 20 37 37 30 20 2a 2f  66,. /*   770 */
29d96 20 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 32      66,   67,  2
29d97 31 32 2c 20 20 34 33 32 2c 20 20 34 33 38 2c 20  12,  432,  438, 
29d98 20 35 35 30 2c 20 20 32 36 39 2c 20 20 34 37 34   550,  269,  474
29d99 2c 20 20 20 31 38 2c 20 20 35 34 39 2c 0a 20 2f  ,   18,  549,. /
29d9a 2a 20 20 20 37 38 30 20 2a 2f 20 20 20 32 38 30  *   780 */   280
29d9b 2c 20 20 33 30 39 2c 20 20 33 34 33 2c 20 20 33  ,  309,  343,  3
29d9c 38 30 2c 20 20 31 37 31 2c 20 20 31 36 30 2c 20  80,  171,  160, 
29d9d 20 32 35 36 2c 20 20 32 36 38 2c 20 20 20 20 35   256,  268,    5
29d9e 2c 20 20 32 36 38 2c 0a 20 2f 2a 20 20 20 37 39  ,  268,. /*   79
29d9f 30 20 2a 2f 20 20 20 32 39 36 2c 20 20 33 36 38  0 */   296,  368
29da0 2c 20 20 20 36 30 2c 20 20 33 30 31 2c 20 20 34  ,   60,  301,  4
29da1 34 32 2c 20 20 34 34 33 2c 20 20 34 33 39 2c 20  42,  443,  439, 
29da2 20 34 33 39 2c 20 20 20 36 33 2c 20 20 20 36 33   439,   63,   63
29da3 2c 0a 20 2f 2a 20 20 20 38 30 30 20 2a 2f 20 20  ,. /*   800 */  
29da4 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32    62,   62,   62
29da5 2c 20 20 20 36 32 2c 20 20 33 32 31 2c 20 20 20  ,   62,  321,   
29da6 36 34 2c 20 20 20 36 34 2c 20 20 20 36 34 2c 20  64,   64,   64, 
29da7 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f 2a 20    64,   65,. /* 
29da8 20 20 38 31 30 20 2a 2f 20 20 20 20 36 35 2c 20    810 */    65, 
29da9 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20 36 36    66,   66,   66
29daa 2c 20 20 20 36 37 2c 20 20 32 31 32 2c 20 20 34  ,   67,  212,  4
29dab 33 32 2c 20 20 34 33 38 2c 20 20 34 30 33 2c 20  32,  438,  403, 
29dac 20 20 31 30 2c 0a 20 2f 2a 20 20 20 38 32 30 20    10,. /*   820 
29dad 2a 2f 20 20 20 34 30 33 2c 20 20 33 31 30 2c 20  */   403,  310, 
29dae 20 32 36 38 2c 20 20 34 30 33 2c 20 20 32 36 38   268,  403,  268
29daf 2c 20 20 34 38 35 2c 20 20 34 32 31 2c 20 20 20  ,  485,  421,   
29db0 32 39 2c 20 20 35 36 36 2c 20 20 20 32 32 2c 0a  29,  566,   22,.
29db1 20 2f 2a 20 20 20 38 33 30 20 2a 2f 20 20 20 35   /*   830 */   5
29db2 36 38 2c 20 20 34 32 30 2c 20 20 34 32 38 2c 20  68,  420,  428, 
29db3 20 34 32 35 2c 20 20 33 37 36 2c 20 20 34 31 39   425,  376,  419
29db4 2c 20 20 33 30 31 2c 20 20 34 34 32 2c 20 20 34  ,  301,  442,  4
29db5 34 33 2c 20 20 34 33 39 2c 0a 20 2f 2a 20 20 20  43,  439,. /*   
29db6 38 34 30 20 2a 2f 20 20 20 34 33 39 2c 20 20 20  840 */   439,   
29db7 36 33 2c 20 20 20 36 33 2c 20 20 20 36 32 2c 20  63,   63,   62, 
29db8 20 20 36 32 2c 20 20 20 36 32 2c 20 20 20 36 32    62,   62,   62
29db9 2c 20 20 33 32 31 2c 20 20 20 36 34 2c 20 20 20  ,  321,   64,   
29dba 36 34 2c 0a 20 2f 2a 20 20 20 38 35 30 20 2a 2f  64,. /*   850 */
29dbb 20 20 20 20 36 34 2c 20 20 20 36 34 2c 20 20 20      64,   64,   
29dbc 36 35 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  65,   65,   66, 
29dbd 20 20 36 36 2c 20 20 20 36 36 2c 20 20 20 36 37    66,   66,   67
29dbe 2c 20 20 32 31 32 2c 20 20 20 37 33 2c 0a 20 2f  ,  212,   73,. /
29dbf 2a 20 20 20 38 36 30 20 2a 2f 20 20 20 33 32 38  *   860 */   328
29dc0 2c 20 20 34 38 35 2c 20 20 20 20 34 2c 20 20 35  ,  485,    4,  5
29dc1 36 39 2c 20 20 32 36 38 2c 20 20 35 37 30 2c 20  69,  268,  570, 
29dc2 20 33 30 30 2c 20 20 32 36 38 2c 20 20 31 34 37   300,  268,  147
29dc3 2c 20 20 34 32 31 2c 0a 20 2f 2a 20 20 20 38 37  ,  421,. /*   87
29dc4 30 20 2a 2f 20 20 20 20 32 34 2c 20 20 33 32 31  0 */    24,  321
29dc5 2c 20 20 33 35 39 2c 20 20 33 32 31 2c 20 20 33  ,  359,  321,  3
29dc6 32 35 2c 20 20 20 37 33 2c 20 20 33 32 38 2c 20  25,   73,  328, 
29dc7 20 34 39 31 2c 20 20 20 20 34 2c 20 20 34 35 35   491,    4,  455
29dc8 2c 0a 20 2f 2a 20 20 20 38 38 30 20 2a 2f 20 20  ,. /*   880 */  
29dc9 20 33 32 31 2c 20 20 33 34 32 2c 20 20 33 30 30   321,  342,  300
29dca 2c 20 20 34 30 34 2c 20 20 34 30 35 2c 20 20 34  ,  404,  405,  4
29dcb 30 34 2c 20 20 34 30 35 2c 20 20 33 36 37 2c 20  04,  405,  367, 
29dcc 20 34 30 34 2c 20 20 34 30 35 2c 0a 20 2f 2a 20   404,  405,. /* 
29dcd 20 20 38 39 30 20 2a 2f 20 20 20 33 32 35 2c 20    890 */   325, 
29dce 20 33 33 30 2c 20 20 33 32 31 2c 20 20 34 32 31   330,  321,  421
29dcf 2c 20 20 20 33 33 2c 20 20 34 32 31 2c 20 20 20  ,   33,  421,   
29dd0 35 34 2c 20 20 33 32 31 2c 20 20 34 32 35 2c 20  54,  321,  425, 
29dd1 20 31 37 38 2c 0a 20 2f 2a 20 20 20 39 30 30 20   178,. /*   900 
29dd2 2a 2f 20 20 20 32 32 39 2c 20 20 34 35 38 2c 20  */   229,  458, 
29dd3 20 34 32 31 2c 20 20 20 35 33 2c 20 20 33 32 31   421,   53,  321
29dd4 2c 20 20 32 32 37 2c 20 20 33 32 31 2c 20 20 33  ,  227,  321,  3
29dd5 33 30 2c 20 20 32 32 38 2c 20 20 34 37 38 2c 0a  30,  228,  478,.
29dd6 20 2f 2a 20 20 20 39 31 30 20 2a 2f 20 20 20 31   /*   910 */   1
29dd7 36 35 2c 20 20 33 32 31 2c 20 20 33 31 35 2c 20  65,  321,  315, 
29dd8 20 31 31 39 2c 20 20 34 32 31 2c 20 20 20 39 39   119,  421,   99
29dd9 2c 20 20 33 33 33 2c 20 20 34 35 38 2c 20 20 33  ,  333,  458,  3
29dda 32 31 2c 20 20 34 32 31 2c 0a 20 2f 2a 20 20 20  21,  421,. /*   
29ddb 39 32 30 20 2a 2f 20 20 20 20 39 37 2c 20 20 20  920 */    97,   
29ddc 37 36 2c 20 20 20 37 35 2c 20 20 33 31 31 2c 20  76,   75,  311, 
29ddd 20 32 36 38 2c 20 20 35 31 39 2c 20 20 34 32 31   268,  519,  421
29dde 2c 20 20 31 30 32 2c 20 20 34 32 31 2c 20 20 31  ,  102,  421,  1
29ddf 30 33 2c 0a 20 2f 2a 20 20 20 39 33 30 20 2a 2f  03,. /*   930 */
29de0 20 20 20 20 37 34 2c 20 20 33 31 39 2c 20 20 33      74,  319,  3
29de1 32 30 2c 20 20 34 32 31 2c 20 20 31 30 38 2c 20  20,  421,  108, 
29de2 20 34 32 37 2c 20 20 34 36 37 2c 20 20 20 37 36   427,  467,   76
29de3 2c 20 20 20 37 35 2c 20 20 34 39 30 2c 0a 20 2f  ,   75,  490,. /
29de4 2a 20 20 20 39 34 30 20 2a 2f 20 20 20 34 32 31  *   940 */   421
29de5 2c 20 20 31 31 30 2c 20 20 34 35 32 2c 20 20 34  ,  110,  452,  4
29de6 35 32 2c 20 20 33 32 31 2c 20 20 35 32 30 2c 20  52,  321,  520, 
29de7 20 20 37 34 2c 20 20 33 31 39 2c 20 20 33 32 30    74,  319,  320
29de8 2c 20 20 20 37 33 2c 0a 20 2f 2a 20 20 20 39 35  ,   73,. /*   95
29de9 30 20 2a 2f 20 20 20 33 32 38 2c 20 20 34 32 37  0 */   328,  427
29dea 2c 20 20 20 20 34 2c 20 20 32 31 30 2c 20 20 32  ,    4,  210,  2
29deb 39 38 2c 20 20 33 32 31 2c 20 20 33 30 30 2c 20  98,  321,  300, 
29dec 20 33 32 31 2c 20 20 31 35 36 2c 20 20 32 35 37   321,  156,  257
29ded 2c 0a 20 2f 2a 20 20 20 39 36 30 20 2a 2f 20 20  ,. /*   960 */  
29dee 20 33 32 31 2c 20 20 32 31 30 2c 20 20 31 38 35   321,  210,  185
29def 2c 20 20 31 38 32 2c 20 20 33 32 35 2c 20 20 32  ,  182,  325,  2
29df0 38 34 2c 20 20 34 32 31 2c 20 20 20 31 37 2c 20  84,  421,   17, 
29df1 20 34 32 39 2c 20 20 34 32 39 2c 0a 20 2f 2a 20   429,  429,. /* 
29df2 20 20 39 37 30 20 2a 2f 20 20 20 34 32 39 2c 20    970 */   429, 
29df3 20 34 33 30 2c 20 20 34 33 31 2c 20 20 20 31 32   430,  431,   12
29df4 2c 20 20 35 39 33 2c 20 20 33 37 38 2c 20 20 31  ,  593,  378,  1
29df5 38 38 2c 20 20 34 32 31 2c 20 20 31 30 30 2c 20  88,  421,  100, 
29df6 20 34 32 31 2c 0a 20 2f 2a 20 20 20 39 38 30 20   421,. /*   980 
29df7 2a 2f 20 20 20 20 33 34 2c 20 20 33 33 30 2c 20  */    34,  330, 
29df8 20 34 32 31 2c 20 20 20 39 38 2c 20 20 34 32 39   421,   98,  429
29df9 2c 20 20 34 32 39 2c 20 20 34 32 39 2c 20 20 34  ,  429,  429,  4
29dfa 33 30 2c 20 20 34 33 31 2c 20 20 20 31 32 2c 0a  30,  431,   12,.
29dfb 20 2f 2a 20 20 20 39 39 30 20 2a 2f 20 20 20 34   /*   990 */   4
29dfc 37 35 2c 20 20 34 35 38 2c 20 20 34 32 32 2c 20  75,  458,  422, 
29dfd 20 31 36 32 2c 20 20 34 38 30 2c 20 20 33 32 31   162,  480,  321
29dfe 2c 20 20 34 32 32 2c 20 20 33 30 36 2c 20 20 32  ,  422,  306,  2
29dff 33 31 2c 20 20 32 33 32 2c 0a 20 2f 2a 20 20 31  31,  232,. /*  1
29e00 30 30 30 20 2a 2f 20 20 20 32 33 33 2c 20 20 31  000 */   233,  1
29e01 30 35 2c 20 20 34 38 34 2c 20 20 36 33 32 2c 20  05,  484,  632, 
29e02 20 34 37 36 2c 20 20 33 32 31 2c 20 20 34 38 36   476,  321,  486
29e03 2c 20 20 34 34 37 2c 20 20 33 32 31 2c 20 20 20  ,  447,  321,   
29e04 32 33 2c 0a 20 2f 2a 20 20 31 30 31 30 20 2a 2f  23,. /*  1010 */
29e05 20 20 20 34 32 32 2c 20 20 20 37 36 2c 20 20 20     422,   76,   
29e06 37 35 2c 20 20 35 39 34 2c 20 20 32 30 37 2c 20  75,  594,  207, 
29e07 20 31 37 38 2c 20 20 32 38 36 2c 20 20 34 32 31   178,  286,  421
29e08 2c 20 20 20 32 35 2c 20 20 32 35 34 2c 0a 20 2f  ,   25,  254,. /
29e09 2a 20 20 31 30 32 30 20 2a 2f 20 20 20 20 37 34  *  1020 */    74
29e0a 2c 20 20 33 31 39 2c 20 20 33 32 30 2c 20 20 32  ,  319,  320,  2
29e0b 38 37 2c 20 20 33 32 31 2c 20 20 34 32 37 2c 20  87,  321,  427, 
29e0c 20 33 32 31 2c 20 20 34 32 31 2c 20 20 20 35 35   321,  421,   55
29e0d 2c 20 20 33 32 31 2c 0a 20 2f 2a 20 20 31 30 33  ,  321,. /*  103
29e0e 30 20 2a 2f 20 20 20 34 32 31 2c 20 20 31 31 31  0 */   421,  111
29e0f 2c 20 20 33 32 31 2c 20 20 34 37 31 2c 20 20 33  ,  321,  471,  3
29e10 32 31 2c 20 20 32 30 35 2c 20 20 35 31 35 2c 20  21,  205,  515, 
29e11 20 35 35 37 2c 20 20 35 31 31 2c 20 20 33 36 33   557,  511,  363
29e12 2c 0a 20 2f 2a 20 20 31 30 34 30 20 2a 2f 20 20  ,. /*  1040 */  
29e13 20 34 37 32 2c 20 20 32 30 34 2c 20 20 33 32 31   472,  204,  321
29e14 2c 20 20 35 31 36 2c 20 20 32 30 36 2c 20 20 33  ,  516,  206,  3
29e15 32 31 2c 20 20 34 32 31 2c 20 20 31 31 32 2c 20  21,  421,  112, 
29e16 20 34 32 31 2c 20 20 31 31 33 2c 0a 20 2f 2a 20   421,  113,. /* 
29e17 20 31 30 35 30 20 2a 2f 20 20 20 33 32 31 2c 20   1050 */   321, 
29e18 20 34 32 31 2c 20 20 20 32 36 2c 20 20 33 32 31   421,   26,  321
29e19 2c 20 20 34 32 31 2c 20 20 20 33 37 2c 20 20 34  ,  421,   37,  4
29e1a 32 31 2c 20 20 20 33 38 2c 20 20 34 32 39 2c 20  21,   38,  429, 
29e1b 20 34 32 39 2c 0a 20 2f 2a 20 20 31 30 36 30 20   429,. /*  1060 
29e1c 2a 2f 20 20 20 34 32 39 2c 20 20 34 33 30 2c 20  */   429,  430, 
29e1d 20 34 33 31 2c 20 20 20 31 32 2c 20 20 34 32 31   431,   12,  421
29e1e 2c 20 20 20 32 37 2c 20 20 35 32 31 2c 20 20 34  ,   27,  521,  4
29e1f 32 31 2c 20 20 20 33 39 2c 20 20 33 32 31 2c 0a  21,   39,  321,.
29e20 20 2f 2a 20 20 31 30 37 30 20 2a 2f 20 20 20 32   /*  1070 */   2
29e21 39 38 2c 20 20 31 35 38 2c 20 20 34 32 31 2c 20  98,  158,  421, 
29e22 20 20 34 30 2c 20 20 32 35 35 2c 20 20 34 32 31    40,  255,  421
29e23 2c 20 20 20 34 31 2c 20 20 33 32 31 2c 20 20 34  ,   41,  321,  4
29e24 38 33 2c 20 20 33 32 31 2c 0a 20 2f 2a 20 20 31  83,  321,. /*  1
29e25 30 38 30 20 2a 2f 20 20 20 31 37 33 2c 20 20 35  080 */   173,  5
29e26 32 33 2c 20 20 33 32 31 2c 20 20 31 38 32 2c 20  23,  321,  182, 
29e27 20 33 32 31 2c 20 20 35 32 32 2c 20 20 33 32 31   321,  522,  321
29e28 2c 20 20 33 38 34 2c 20 20 32 38 33 2c 20 20 32  ,  384,  283,  2
29e29 37 33 2c 0a 20 2f 2a 20 20 31 30 39 30 20 2a 2f  73,. /*  1090 */
29e2a 20 20 20 33 32 31 2c 20 20 34 32 31 2c 20 20 20     321,  421,   
29e2b 34 33 2c 20 20 32 39 37 2c 20 20 35 33 34 2c 20  43,  297,  534, 
29e2c 20 33 32 31 2c 20 20 34 37 36 2c 20 20 33 32 31   321,  476,  321
29e2d 2c 20 20 32 31 30 2c 20 20 34 32 31 2c 0a 20 2f  ,  210,  421,. /
29e2e 2a 20 20 31 31 30 30 20 2a 2f 20 20 20 20 34 34  *  1100 */    44
29e2f 2c 20 20 34 32 31 2c 20 20 20 34 35 2c 20 20 33  ,  421,   45,  3
29e30 32 31 2c 20 20 34 32 31 2c 20 20 20 33 30 2c 20  21,  421,   30, 
29e31 20 34 32 31 2c 20 20 20 33 31 2c 20 20 34 32 31   421,   31,  421
29e32 2c 20 20 20 34 36 2c 0a 20 2f 2a 20 20 31 31 31  ,   46,. /*  111
29e33 30 20 2a 2f 20 20 20 35 30 38 2c 20 20 35 30 39  0 */   508,  509
29e34 2c 20 20 34 32 31 2c 20 20 20 34 37 2c 20 20 32  ,  421,   47,  2
29e35 35 39 2c 20 20 33 32 31 2c 20 20 31 38 32 2c 20  59,  321,  182, 
29e36 20 34 32 31 2c 20 20 20 34 38 2c 20 20 34 32 31   421,   48,  421
29e37 2c 0a 20 2f 2a 20 20 31 31 32 30 20 2a 2f 20 20  ,. /*  1120 */  
29e38 20 20 34 39 2c 20 20 33 32 31 2c 20 20 33 35 38    49,  321,  358
29e39 2c 20 20 33 39 30 2c 20 20 31 38 32 2c 20 20 34  ,  390,  182,  4
29e3a 32 31 2c 20 20 20 33 32 2c 20 20 33 32 31 2c 20  21,   32,  321, 
29e3b 20 32 36 31 2c 20 20 35 31 38 2c 0a 20 2f 2a 20   261,  518,. /* 
29e3c 20 31 31 33 30 20 2a 2f 20 20 20 35 31 37 2c 20   1130 */   517, 
29e3d 20 35 35 33 2c 20 20 35 36 31 2c 20 20 31 38 32   553,  561,  182
29e3e 2c 20 20 31 37 33 2c 20 20 34 31 32 2c 20 20 31  ,  173,  412,  1
29e3f 39 31 2c 20 20 34 32 31 2c 20 20 20 31 31 2c 20  91,  421,   11, 
29e40 20 35 36 32 2c 0a 20 2f 2a 20 20 31 31 34 30 20   562,. /*  1140 
29e41 2a 2f 20 20 20 35 37 33 2c 20 20 20 39 32 2c 20  */   573,   92, 
29e42 20 20 39 32 2c 20 20 34 32 31 2c 20 20 20 35 31    92,  421,   51
29e43 2c 20 20 35 39 30 2c 20 20 32 36 33 2c 20 20 32  ,  590,  263,  2
29e44 39 34 2c 20 20 32 36 35 2c 20 20 34 32 31 2c 0a  94,  265,  421,.
29e45 20 2f 2a 20 20 31 31 35 30 20 2a 2f 20 20 20 20   /*  1150 */    
29e46 35 32 2c 20 20 32 36 37 2c 20 20 32 37 32 2c 20  52,  267,  272, 
29e47 20 33 37 31 2c 20 20 31 34 36 2c 20 20 33 37 34   371,  146,  374
29e48 2c 20 20 33 37 35 2c 20 20 32 37 35 2c 20 20 32  ,  375,  275,  2
29e49 37 36 2c 20 20 32 37 37 2c 0a 20 2f 2a 20 20 31  76,  277,. /*  1
29e4a 31 36 30 20 2a 2f 20 20 20 35 36 35 2c 20 20 35  160 */   565,  5
29e4b 37 35 2c 20 20 32 38 35 2c 20 20 32 38 38 2c 20  75,  285,  288, 
29e4c 20 32 38 39 2c 20 20 35 38 37 2c 20 20 34 37 30   289,  587,  470
29e4d 2c 20 20 34 35 31 2c 20 20 32 33 36 2c 20 20 34  ,  451,  236,  4
29e4e 35 33 2c 0a 20 2f 2a 20 20 31 31 37 30 20 2a 2f  53,. /*  1170 */
29e4f 20 20 20 33 32 39 2c 20 20 32 34 34 2c 20 20 34     329,  244,  4
29e50 37 33 2c 20 20 35 31 34 2c 20 20 32 35 31 2c 20  73,  514,  251, 
29e51 20 35 32 34 2c 20 20 35 36 30 2c 20 20 31 36 33   524,  560,  163
29e52 2c 20 20 34 30 31 2c 20 20 35 37 32 2c 0a 20 2f  ,  401,  572,. /
29e53 2a 20 20 31 31 38 30 20 2a 2f 20 20 20 34 32 36  *  1180 */   426
29e54 2c 20 20 35 32 35 2c 20 20 32 38 32 2c 20 20 35  ,  525,  282,  5
29e55 32 38 2c 20 20 34 30 39 2c 20 20 20 20 37 2c 20  28,  409,    7, 
29e56 20 34 31 30 2c 20 20 34 31 31 2c 20 20 33 38 35   410,  411,  385
29e57 2c 20 20 33 31 38 2c 0a 20 2f 2a 20 20 31 31 39  ,  318,. /*  119
29e58 30 20 2a 2f 20 20 20 20 38 35 2c 20 20 32 33 37  0 */    85,  237
29e59 2c 20 20 33 33 38 2c 20 20 35 32 36 2c 20 20 20  ,  338,  526,   
29e5a 38 34 2c 20 20 33 33 36 2c 20 20 33 35 33 2c 20  84,  336,  353, 
29e5b 20 20 35 38 2c 20 20 20 38 30 2c 20 20 32 31 35    58,   80,  215
29e5c 2c 0a 20 2f 2a 20 20 31 32 30 30 20 2a 2f 20 20  ,. /*  1200 */  
29e5d 20 31 37 30 2c 20 20 34 36 38 2c 20 20 31 32 31   170,  468,  121
29e5e 2c 20 20 20 38 36 2c 20 20 33 34 31 2c 20 20 33  ,   86,  341,  3
29e5f 34 38 2c 20 20 33 30 35 2c 20 20 35 30 31 2c 20  48,  305,  501, 
29e60 20 35 30 36 2c 20 20 31 32 34 2c 0a 20 2f 2a 20   506,  124,. /* 
29e61 20 31 32 31 30 20 2a 2f 20 20 20 35 31 31 2c 20   1210 */   511, 
29e62 20 32 32 32 2c 20 20 33 36 30 2c 20 20 34 32 33   222,  360,  423
29e63 2c 20 20 32 35 32 2c 20 20 31 38 36 2c 20 20 35  ,  252,  186,  5
29e64 31 32 2c 20 20 35 31 30 2c 20 20 32 32 31 2c 20  12,  510,  221, 
29e65 20 32 32 33 2c 0a 20 2f 2a 20 20 31 32 32 30 20   223,. /*  1220 
29e66 2a 2f 20 20 20 32 33 38 2c 20 20 35 30 37 2c 20  */   238,  507, 
29e67 20 32 33 39 2c 20 20 35 33 35 2c 20 20 32 34 30   239,  535,  240
29e68 2c 20 20 32 39 32 2c 20 20 34 32 34 2c 20 20 35  ,  292,  424,  5
29e69 32 39 2c 20 20 35 33 36 2c 20 20 35 33 37 2c 0a  29,  536,  537,.
29e6a 20 2f 2a 20 20 31 32 33 30 20 2a 2f 20 20 20 32   /*  1230 */   2
29e6b 39 33 2c 20 20 35 34 33 2c 20 20 31 38 37 2c 20  93,  543,  187, 
29e6c 20 31 38 39 2c 20 20 32 34 35 2c 20 20 33 36 32   189,  245,  362
29e6d 2c 20 20 34 38 32 2c 20 20 34 38 38 2c 20 20 32  ,  482,  488,  2
29e6e 34 37 2c 20 20 31 39 30 2c 0a 20 2f 2a 20 20 31  47,  190,. /*  1
29e6f 32 34 30 20 2a 2f 20 20 20 33 36 34 2c 20 20 20  240 */   364,   
29e70 38 39 2c 20 20 35 34 35 2c 20 20 31 39 32 2c 20  89,  545,  192, 
29e71 20 31 31 37 2c 20 20 33 37 30 2c 20 20 31 33 32   117,  370,  132
29e72 2c 20 20 35 35 36 2c 20 20 35 36 33 2c 20 20 31  ,  556,  563,  1
29e73 39 35 2c 0a 20 2f 2a 20 20 31 32 35 30 20 2a 2f  95,. /*  1250 */
29e74 20 20 20 33 38 32 2c 20 20 33 38 33 2c 20 20 33     382,  383,  3
29e75 31 34 2c 20 20 31 33 33 2c 20 20 31 33 34 2c 20  14,  133,  134, 
29e76 20 35 37 31 2c 20 20 31 33 38 2c 20 20 31 33 35   571,  138,  135
29e77 2c 20 20 31 33 36 2c 20 20 35 38 34 2c 0a 20 2f  ,  136,  584,. /
29e78 2a 20 20 31 32 36 30 20 2a 2f 20 20 20 35 38 39  *  1260 */   589
29e79 2c 20 20 35 38 35 2c 20 20 31 34 32 2c 20 20 33  ,  585,  142,  3
29e7a 39 39 2c 20 20 31 30 31 2c 20 20 34 31 33 2c 20  99,  101,  413, 
29e7b 20 32 32 30 2c 20 20 35 38 36 2c 20 20 32 37 30   220,  586,  270
29e7c 2c 20 20 31 30 34 2c 0a 20 2f 2a 20 20 31 32 37  ,  104,. /*  127
29e7d 30 20 2a 2f 20 20 20 31 34 31 2c 20 20 36 33 33  0 */   141,  633
29e7e 2c 20 20 36 33 34 2c 20 20 31 36 36 2c 20 20 31  ,  634,  166,  1
29e7f 36 37 2c 20 20 34 34 31 2c 20 20 34 34 34 2c 20  67,  441,  444, 
29e80 20 20 37 32 2c 20 20 34 36 30 2c 20 20 34 34 38    72,  460,  448
29e81 2c 0a 20 2f 2a 20 20 31 32 38 30 20 2a 2f 20 20  ,. /*  1280 */  
29e82 20 34 35 37 2c 20 20 35 34 36 2c 20 20 31 34 33   457,  546,  143
29e83 2c 20 20 31 35 37 2c 20 20 20 20 36 2c 20 20 34  ,  157,    6,  4
29e84 36 31 2c 20 20 20 31 34 2c 20 20 34 37 39 2c 20  61,   14,  479, 
29e85 20 31 36 39 2c 20 20 34 36 32 2c 0a 20 2f 2a 20   169,  462,. /* 
29e86 20 31 32 39 30 20 2a 2f 20 20 20 20 39 33 2c 20   1290 */    93, 
29e87 20 34 36 36 2c 20 20 20 38 32 2c 20 20 31 32 32   466,   82,  122
29e88 2c 20 20 20 31 33 2c 20 20 31 37 34 2c 20 20 34  ,   13,  174,  4
29e89 38 37 2c 20 20 20 39 36 2c 20 20 31 32 33 2c 20  87,   96,  123, 
29e8a 20 31 36 31 2c 0a 20 2f 2a 20 20 31 33 30 30 20   161,. /*  1300 
29e8b 2a 2f 20 20 20 34 39 34 2c 20 20 34 39 35 2c 20  */   494,  495, 
29e8c 20 20 38 37 2c 20 20 31 32 35 2c 20 20 31 32 36    87,  125,  126
29e8d 2c 20 20 31 31 36 2c 20 20 32 35 38 2c 20 20 20  ,  116,  258,   
29e8e 38 38 2c 20 20 31 32 37 2c 20 20 31 38 34 2c 0a  88,  127,  184,.
29e8f 20 2f 2a 20 20 31 33 31 30 20 2a 2f 20 20 20 32   /*  1310 */   2
29e90 35 30 2c 20 20 33 36 31 2c 20 20 32 31 39 2c 20  50,  361,  219, 
29e91 20 31 30 37 2c 20 20 35 34 34 2c 20 20 31 34 35   107,  544,  145
29e92 2c 20 20 31 32 38 2c 20 20 31 39 33 2c 20 20 33  ,  128,  193,  3
29e93 36 35 2c 20 20 31 31 38 2c 0a 20 2f 2a 20 20 31  65,  118,. /*  1
29e94 33 32 30 20 2a 2f 20 20 20 33 35 32 2c 20 20 33  320 */   352,  3
29e95 35 37 2c 20 20 31 37 33 2c 20 20 32 37 31 2c 20  57,  173,  271, 
29e96 20 31 33 30 2c 20 20 20 20 39 2c 20 20 33 31 36   130,    9,  316
29e97 2c 20 20 35 35 39 2c 20 20 31 39 37 2c 20 20 20  ,  559,  197,   
29e98 39 30 2c 0a 20 2f 2a 20 20 31 33 33 30 20 2a 2f  90,. /*  1330 */
29e99 20 20 20 35 34 37 2c 20 20 31 33 31 2c 20 20 31     547,  131,  1
29e9a 32 39 2c 20 20 20 31 35 2c 20 20 31 39 39 2c 20  29,   15,  199, 
29e9b 20 35 35 31 2c 20 20 35 36 34 2c 20 20 32 30 30   551,  564,  200
29e9c 2c 20 20 35 36 37 2c 20 20 32 30 31 2c 0a 20 2f  ,  567,  201,. /
29e9d 2a 20 20 31 33 34 30 20 2a 2f 20 20 20 31 33 39  *  1340 */   139
29e9e 2c 20 20 31 33 37 2c 20 20 35 38 32 2c 20 20 20  ,  137,  582,   
29e9f 39 31 2c 20 20 20 31 36 2c 20 20 31 30 36 2c 20  91,   16,  106, 
29ea0 20 31 34 30 2c 20 20 32 30 38 2c 20 20 35 37 34   140,  208,  574
29ea1 2c 20 20 33 39 32 2c 0a 20 2f 2a 20 20 31 33 35  ,  392,. /*  135
29ea2 30 20 2a 2f 20 20 20 33 39 36 2c 20 20 32 39 30  0 */   396,  290
29ea3 2c 20 20 31 34 38 2c 20 20 35 39 36 2c 0a 7d 3b  ,  148,  596,.};
29ea4 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 59 59  .static const YY
29ea5 43 4f 44 45 54 59 50 45 20 79 79 5f 6c 6f 6f 6b  CODETYPE yy_look
29ea6 61 68 65 61 64 5b 5d 20 3d 20 7b 0a 20 2f 2a 20  ahead[] = {. /* 
29ea7 20 20 20 20 30 20 2a 2f 20 20 20 20 31 36 2c 20      0 */    16, 
29ea8 20 31 34 30 2c 20 20 31 34 31 2c 20 20 31 34 32   140,  141,  142
29ea9 2c 20 20 31 34 33 2c 20 20 20 32 31 2c 20 20 20  ,  143,   21,   
29eaa 32 33 2c 20 20 20 32 33 2c 20 20 20 36 39 2c 20  23,   23,   69, 
29eab 20 20 37 30 2c 0a 20 2f 2a 20 20 20 20 31 30 20    70,. /*    10 
29eac 2a 2f 20 20 20 20 37 31 2c 20 20 20 37 32 2c 20  */    71,   72, 
29ead 20 31 31 30 2c 20 20 20 37 34 2c 20 20 20 37 35   110,   74,   75
29eae 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20  ,   76,   77,   
29eaf 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 0a  78,   79,   80,.
29eb0 20 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 20   /*    20 */    
29eb1 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
29eb2 20 20 38 34 2c 20 20 20 20 31 2c 20 20 20 20 32    84,    1,    2
29eb3 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20  ,   42,   43,   
29eb4 37 33 2c 20 20 20 37 34 2c 0a 20 2f 2a 20 20 20  73,   74,. /*   
29eb5 20 33 30 20 2a 2f 20 20 20 20 37 35 2c 20 20 20   30 */    75,   
29eb6 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20  76,   77,   78, 
29eb7 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31    79,   80,   81
29eb8 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20  ,   82,   83,   
29eb9 38 34 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f  84,. /*    40 */
29eba 20 20 20 20 35 38 2c 20 20 31 36 32 2c 20 20 31      58,  162,  1
29ebb 36 33 2c 20 20 31 36 34 2c 20 20 20 36 30 2c 20  63,  164,   60, 
29ebc 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29ebd 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f  ,   64,   65,. /
29ebe 2a 20 20 20 20 35 30 20 2a 2f 20 20 20 20 36 36  *    50 */    66
29ebf 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20  ,   67,   68,   
29ec0 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29ec1 20 20 37 32 2c 20 20 31 34 38 2c 20 20 20 37 34    72,  148,   74
29ec2 2c 20 20 20 37 35 2c 0a 20 2f 2a 20 20 20 20 36  ,   75,. /*    6
29ec3 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 20 37 37  0 */    76,   77
29ec4 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29ec5 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29ec6 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31 36    83,   84,   16
29ec7 2c 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20  ,. /*    70 */  
29ec8 20 20 38 38 2c 20 20 20 38 38 2c 20 20 20 38 38    88,   88,   88
29ec9 2c 20 20 20 38 34 2c 20 20 20 39 32 2c 20 20 20  ,   84,   92,   
29eca 32 32 2c 20 20 32 31 39 2c 20 20 32 32 30 2c 20  22,  219,  220, 
29ecb 20 32 32 31 2c 20 20 32 32 32 2c 0a 20 2f 2a 20   221,  222,. /* 
29ecc 20 20 20 38 30 20 2a 2f 20 20 20 20 37 34 2c 20     80 */    74, 
29ecd 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
29ece 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29ecf 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29ed0 20 20 38 33 2c 0a 20 2f 2a 20 20 20 20 39 30 20    83,. /*    90 
29ed1 2a 2f 20 20 20 20 38 34 2c 20 20 32 31 39 2c 20  */    84,  219, 
29ed2 20 31 38 33 2c 20 20 32 32 31 2c 20 20 32 32 32   183,  221,  222
29ed3 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20  ,   42,   43,   
29ed4 37 38 2c 20 20 20 37 39 2c 20 20 20 34 36 2c 0a  78,   79,   46,.
29ed5 20 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 20   /*   100 */    
29ed6 37 38 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20  78,   79,   80, 
29ed7 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
29ed8 2c 20 20 20 38 34 2c 20 20 31 32 35 2c 20 20 31  ,   84,  125,  1
29ed9 32 36 2c 20 20 31 32 37 2c 0a 20 2f 2a 20 20 20  26,  127,. /*   
29eda 31 31 30 20 2a 2f 20 20 20 31 37 30 2c 20 20 32  110 */   170,  2
29edb 33 39 2c 20 20 20 31 36 2c 20 20 20 36 30 2c 20  39,   16,   60, 
29edc 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29edd 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29ede 36 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f  66,. /*   120 */
29edf 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20      67,   68,   
29ee0 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29ee1 20 20 37 32 2c 20 20 31 34 38 2c 20 20 20 37 34    72,  148,   74
29ee2 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f  ,   75,   76,. /
29ee3 2a 20 20 20 31 33 30 20 2a 2f 20 20 20 20 37 37  *   130 */    77
29ee4 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29ee5 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29ee6 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34 32    83,   84,   42
29ee7 2c 20 20 20 34 33 2c 0a 20 2f 2a 20 20 20 31 34  ,   43,. /*   14
29ee8 30 20 2a 2f 20 20 20 20 34 34 2c 20 20 20 38 30  0 */    44,   80
29ee9 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20  ,   81,   82,   
29eea 38 33 2c 20 20 20 38 34 2c 20 20 20 32 33 2c 20  83,   84,   23, 
29eeb 20 31 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31   148,  170,  171
29eec 2c 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20  ,. /*   150 */  
29eed 20 20 31 39 2c 20 20 20 38 33 2c 20 20 20 38 34    19,   83,   84
29eee 2c 20 20 31 35 36 2c 20 20 20 32 33 2c 20 20 31  ,  156,   23,  1
29eef 37 30 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 20  70,   60,   61, 
29ef0 20 20 36 32 2c 20 20 20 36 33 2c 0a 20 2f 2a 20    62,   63,. /* 
29ef1 20 20 31 36 30 20 2a 2f 20 20 20 20 36 34 2c 20    160 */    64, 
29ef2 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37    65,   66,   67
29ef3 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
29ef4 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
29ef5 20 20 31 39 2c 0a 20 2f 2a 20 20 20 31 37 30 20    19,. /*   170 
29ef6 2a 2f 20 20 20 20 37 34 2c 20 20 20 37 35 2c 20  */    74,   75, 
29ef7 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
29ef8 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
29ef9 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 0a  81,   82,   83,.
29efa 20 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 20   /*   180 */    
29efb 38 34 2c 20 20 20 31 36 2c 20 20 31 38 33 2c 20  84,   16,  183, 
29efc 20 31 34 38 2c 20 20 31 35 31 2c 20 20 31 34 38   148,  151,  148
29efd 2c 20 20 20 32 31 2c 20 20 31 39 30 2c 20 20 31  ,   21,  190,  1
29efe 34 38 2c 20 20 20 35 38 2c 0a 20 2f 2a 20 20 20  48,   58,. /*   
29eff 31 39 30 20 2a 2f 20 20 20 31 36 39 2c 20 20 32  190 */   169,  2
29f00 31 33 2c 20 20 31 35 37 2c 20 20 31 35 38 2c 20  13,  157,  158, 
29f01 20 31 38 36 2c 20 20 31 38 37 2c 20 20 32 31 38   186,  187,  218
29f02 2c 20 20 20 32 33 2c 20 20 31 37 37 2c 20 20 32  ,   23,  177,  2
29f03 30 32 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f  02,. /*   200 */
29f04 20 20 20 32 30 33 2c 20 20 20 37 38 2c 20 20 20     203,   78,   
29f05 37 39 2c 20 20 31 36 36 2c 20 20 31 36 37 2c 20  79,  166,  167, 
29f06 20 32 30 38 2c 20 20 31 36 31 2c 20 20 20 34 32   208,  161,   42
29f07 2c 20 20 20 34 33 2c 20 20 20 37 38 2c 0a 20 2f  ,   43,   78,. /
29f08 2a 20 20 20 32 31 30 20 2a 2f 20 20 20 20 37 39  *   210 */    79
29f09 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 31  ,   88,   89,  1
29f0a 37 37 2c 20 20 31 37 38 2c 20 20 31 37 30 2c 20  77,  178,  170, 
29f0b 20 31 38 31 2c 20 20 31 38 32 2c 20 20 20 36 38   181,  182,   68
29f0c 2c 20 20 20 38 38 2c 0a 20 2f 2a 20 20 20 32 32  ,   88,. /*   22
29f0d 30 20 2a 2f 20 20 20 31 38 34 2c 20 20 20 39 38  0 */   184,   98
29f0e 2c 20 20 20 39 39 2c 20 20 20 39 32 2c 20 20 20  ,   99,   92,   
29f0f 31 36 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 20  16,   60,   61, 
29f10 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36 34    62,   63,   64
29f11 2c 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20  ,. /*   230 */  
29f12 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37    65,   66,   67
29f13 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
29f14 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
29f15 20 20 32 32 2c 20 20 20 37 34 2c 0a 20 2f 2a 20    22,   74,. /* 
29f16 20 20 32 34 30 20 2a 2f 20 20 20 20 37 35 2c 20    240 */    75, 
29f17 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
29f18 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
29f19 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
29f1a 20 20 38 34 2c 0a 20 2f 2a 20 20 20 32 35 30 20    84,. /*   250 
29f1b 2a 2f 20 20 20 20 34 32 2c 20 20 20 34 33 2c 20  */    42,   43, 
29f1c 20 20 37 38 2c 20 20 20 20 30 2c 20 20 20 20 31    78,    0,    1
29f1d 2c 20 20 20 20 32 2c 20 20 31 32 35 2c 20 20 31  ,    2,  125,  1
29f1e 32 36 2c 20 20 31 32 37 2c 20 20 32 32 36 2c 0a  26,  127,  226,.
29f1f 20 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 20 20   /*   260 */    
29f20 31 31 2c 20 20 31 36 32 2c 20 20 20 38 38 2c 20  11,  162,   88, 
29f21 20 20 38 39 2c 20 20 32 33 31 2c 20 20 32 32 38    89,  231,  228
29f22 2c 20 20 32 32 39 2c 20 20 20 31 36 2c 20 20 20  ,  229,   16,   
29f23 36 30 2c 20 20 20 36 31 2c 0a 20 2f 2a 20 20 20  60,   61,. /*   
29f24 32 37 30 20 2a 2f 20 20 20 20 36 32 2c 20 20 20  270 */    62,   
29f25 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20  63,   64,   65, 
29f26 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
29f27 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
29f28 37 31 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f  71,. /*   280 */
29f29 20 20 20 20 37 32 2c 20 20 31 34 38 2c 20 20 20      72,  148,   
29f2a 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
29f2b 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
29f2c 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 0a 20 2f  ,   80,   81,. /
29f2d 2a 20 20 20 32 39 30 20 2a 2f 20 20 20 20 38 32  *   290 */    82
29f2e 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
29f2f 34 32 2c 20 20 20 34 33 2c 20 20 31 38 36 2c 20  42,   43,  186, 
29f30 20 31 38 37 2c 20 20 20 31 36 2c 20 20 20 34 39   187,   16,   49
29f31 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20 20 33 30  ,  148,. /*   30
29f32 30 20 2a 2f 20 20 20 32 30 31 2c 20 20 20 34 39  0 */   201,   49
29f33 2c 20 20 20 31 38 2c 20 20 31 37 30 2c 20 20 31  ,   18,  170,  1
29f34 37 31 2c 20 20 31 35 34 2c 20 20 31 34 32 2c 20  71,  154,  142, 
29f35 20 31 34 33 2c 20 20 31 35 37 2c 20 20 31 35 38   143,  157,  158
29f36 2c 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20  ,. /*   310 */  
29f37 20 31 38 32 2c 20 20 20 36 30 2c 20 20 20 36 31   182,   60,   61
29f38 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20  ,   62,   63,   
29f39 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  64,   65,   66, 
29f3a 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20    67,   68,. /* 
29f3b 20 20 33 32 30 20 2a 2f 20 20 20 20 36 39 2c 20    320 */    69, 
29f3c 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
29f3d 2c 20 20 20 39 31 2c 20 20 20 37 34 2c 20 20 20  ,   91,   74,   
29f3e 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
29f3f 20 20 37 38 2c 0a 20 2f 2a 20 20 20 33 33 30 20    78,. /*   330 
29f40 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20  */    79,   80, 
29f41 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
29f42 2c 20 20 20 38 34 2c 20 20 31 36 38 2c 20 20 31  ,   84,  168,  1
29f43 36 39 2c 20 20 31 32 32 2c 20 20 20 35 35 2c 0a  69,  122,   55,.
29f44 20 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 20 31   /*   340 */   1
29f45 33 32 2c 20 20 20 31 36 2c 20 20 20 31 36 2c 20  32,   16,   16, 
29f46 20 31 31 30 2c 20 20 20 31 36 2c 20 20 31 37 37   110,   16,  177
29f47 2c 20 20 20 32 30 2c 20 20 20 32 30 2c 20 20 20  ,   20,   20,   
29f48 39 39 2c 20 20 31 30 30 2c 0a 20 2f 2a 20 20 20  99,  100,. /*   
29f49 33 35 30 20 2a 2f 20 20 20 31 30 31 2c 20 20 20  350 */   101,   
29f4a 39 39 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20  99,  100,  101, 
29f4b 20 20 38 30 2c 20 20 20 31 32 2c 20 20 32 32 33    80,   12,  223
29f4c 2c 20 20 31 32 34 2c 20 20 31 34 38 2c 20 20 31  ,  124,  148,  1
29f4d 31 30 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f  10,. /*   360 */
29f4e 20 20 20 31 38 32 2c 20 20 31 34 38 2c 20 20 31     182,  148,  1
29f4f 31 30 2c 20 20 20 32 33 2c 20 20 20 31 39 2c 20  10,   23,   19, 
29f50 20 20 38 34 2c 20 20 20 32 31 2c 20 20 20 32 34    84,   21,   24
29f51 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 0a 20 2f  ,   42,   43,. /
29f52 2a 20 20 20 33 37 30 20 2a 2f 20 20 20 31 34 38  *   370 */   148
29f53 2c 20 20 20 39 30 2c 20 20 20 39 31 2c 20 20 20  ,   90,   91,   
29f54 39 32 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20  92,   93,   94, 
29f55 20 20 39 35 2c 20 20 20 39 36 2c 20 20 20 39 34    95,   96,   94
29f56 2c 20 20 31 36 36 2c 0a 20 2f 2a 20 20 20 33 38  ,  166,. /*   38
29f57 30 20 2a 2f 20 20 20 20 33 37 2c 20 20 31 30 37  0 */    37,  107
29f58 2c 20 20 20 33 39 2c 20 20 31 33 32 2c 20 20 31  ,   39,  132,  1
29f59 30 33 2c 20 20 31 31 31 2c 20 20 20 36 30 2c 20  03,  111,   60, 
29f5a 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29f5b 2c 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20  ,. /*   390 */  
29f5c 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36    64,   65,   66
29f5d 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20  ,   67,   68,   
29f5e 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29f5f 20 20 37 32 2c 20 20 31 38 39 2c 0a 20 2f 2a 20    72,  189,. /* 
29f60 20 20 34 30 30 20 2a 2f 20 20 20 20 37 34 2c 20    400 */    74, 
29f61 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
29f62 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29f63 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29f64 20 20 38 33 2c 0a 20 2f 2a 20 20 20 34 31 30 20    83,. /*   410 
29f65 2a 2f 20 20 20 20 38 34 2c 20 20 20 31 36 2c 20  */    84,   16, 
29f66 20 20 38 34 2c 20 20 31 35 36 2c 20 20 20 39 32    84,  156,   92
29f67 2c 20 20 20 32 30 2c 20 20 20 32 33 2c 20 20 20  ,   20,   23,   
29f68 39 32 2c 20 20 31 33 34 2c 20 20 20 39 31 2c 0a  92,  134,   91,.
29f69 20 2f 2a 20 20 20 34 32 30 20 2a 2f 20 20 20 20   /*   420 */    
29f6a 39 32 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 20  92,   93,   94, 
29f6b 20 20 39 35 2c 20 20 20 39 36 2c 20 20 31 30 33    95,   96,  103
29f6c 2c 20 20 31 34 38 2c 20 20 20 32 32 2c 20 20 20  ,  148,   22,   
29f6d 38 38 2c 20 20 20 38 39 2c 0a 20 2f 2a 20 20 20  88,   89,. /*   
29f6e 34 33 30 20 2a 2f 20 20 20 31 34 38 2c 20 20 31  430 */   148,  1
29f6f 30 33 2c 20 20 32 31 30 2c 20 20 31 30 36 2c 20  03,  210,  106, 
29f70 20 31 35 36 2c 20 20 31 30 38 2c 20 20 31 30 39   156,  108,  109
29f71 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31  ,   42,   43,  1
29f72 34 34 2c 0a 20 2f 2a 20 20 20 34 34 30 20 2a 2f  44,. /*   440 */
29f73 20 20 20 31 34 35 2c 20 20 32 32 38 2c 20 20 32     145,  228,  2
29f74 32 39 2c 20 20 32 31 39 2c 20 20 31 34 39 2c 20  29,  219,  149, 
29f75 20 32 32 31 2c 20 20 32 32 32 2c 20 20 31 39 30   221,  222,  190
29f76 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 0a 20 2f  ,  170,  171,. /
29f77 2a 20 20 20 34 35 30 20 2a 2f 20 20 20 32 34 30  *   450 */   240
29f78 2c 20 20 31 35 36 2c 20 20 31 37 30 2c 20 20 31  ,  156,  170,  1
29f79 37 31 2c 20 20 31 36 32 2c 20 20 20 36 30 2c 20  71,  162,   60, 
29f7a 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29f7b 2c 20 20 20 36 34 2c 0a 20 2f 2a 20 20 20 34 36  ,   64,. /*   46
29f7c 30 20 2a 2f 20 20 20 20 36 35 2c 20 20 20 36 36  0 */    65,   66
29f7d 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20  ,   67,   68,   
29f7e 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29f7f 20 20 37 32 2c 20 20 31 39 30 2c 20 20 20 37 34    72,  190,   74
29f80 2c 0a 20 2f 2a 20 20 20 34 37 30 20 2a 2f 20 20  ,. /*   470 */  
29f81 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37    75,   76,   77
29f82 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29f83 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29f84 20 20 38 33 2c 20 20 20 38 34 2c 0a 20 2f 2a 20    83,   84,. /* 
29f85 20 20 34 38 30 20 2a 2f 20 20 20 20 31 36 2c 20    480 */    16, 
29f86 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 39 30    88,   89,   90
29f87 2c 20 20 20 32 30 2c 20 20 31 39 30 2c 20 20 20  ,   20,  190,   
29f88 39 33 2c 20 20 20 39 34 2c 20 20 20 39 35 2c 20  93,   94,   95, 
29f89 20 20 32 33 2c 0a 20 2f 2a 20 20 20 34 39 30 20    23,. /*   490 
29f8a 2a 2f 20 20 20 31 33 31 2c 20 20 32 31 33 2c 20  */   131,  213, 
29f8b 20 31 33 33 2c 20 20 32 30 31 2c 20 20 32 31 32   133,  201,  212
29f8c 2c 20 20 20 39 30 2c 20 20 32 31 38 2c 20 20 31  ,   90,  218,  1
29f8d 30 34 2c 20 20 20 39 33 2c 20 20 20 39 34 2c 0a  04,   93,   94,.
29f8e 20 2f 2a 20 20 20 35 30 30 20 2a 2f 20 20 20 20   /*   500 */    
29f8f 39 35 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20  95,   42,   43, 
29f90 20 20 20 37 2c 20 20 20 20 38 2c 20 20 20 20 39     7,    8,    9
29f91 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31  ,   42,   43,  1
29f92 39 31 2c 20 20 31 30 34 2c 0a 20 2f 2a 20 20 20  91,  104,. /*   
29f93 35 31 30 20 2a 2f 20 20 20 32 31 35 2c 20 20 32  510 */   215,  2
29f94 31 39 2c 20 20 31 35 39 2c 20 20 32 32 31 2c 20  19,  159,  221, 
29f95 20 32 32 32 2c 20 20 31 36 32 2c 20 20 31 36 33   222,  162,  163
29f96 2c 20 20 31 36 34 2c 20 20 31 35 36 2c 20 20 31  ,  164,  156,  1
29f97 36 35 2c 0a 20 2f 2a 20 20 20 35 32 30 20 2a 2f  65,. /*   520 */
29f98 20 20 20 31 36 36 2c 20 20 31 36 37 2c 20 20 20     166,  167,   
29f99 36 33 2c 20 20 20 36 34 2c 20 20 20 36 30 2c 20  63,   64,   60, 
29f9a 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29f9b 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 0a 20 2f  ,   64,   65,. /
29f9c 2a 20 20 20 35 33 30 20 2a 2f 20 20 20 20 36 36  *   530 */    66
29f9d 2c 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20  ,   67,   68,   
29f9e 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29f9f 20 20 37 32 2c 20 20 32 34 32 2c 20 20 20 37 34    72,  242,   74
29fa0 2c 20 20 20 37 35 2c 0a 20 2f 2a 20 20 20 35 34  ,   75,. /*   54
29fa1 30 20 2a 2f 20 20 20 20 37 36 2c 20 20 20 37 37  0 */    76,   77
29fa2 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29fa3 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29fa4 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31 36    83,   84,   16
29fa5 2c 0a 20 2f 2a 20 20 20 35 35 30 20 2a 2f 20 20  ,. /*   550 */  
29fa6 20 31 34 38 2c 20 20 20 39 32 2c 20 20 31 39 30   148,   92,  190
29fa7 2c 20 20 20 32 30 2c 20 20 20 38 38 2c 20 20 20  ,   20,   88,   
29fa8 38 39 2c 20 20 31 34 38 2c 20 20 20 39 30 2c 20  89,  148,   90, 
29fa9 20 31 35 36 2c 20 20 31 34 38 2c 0a 20 2f 2a 20   156,  148,. /* 
29faa 20 20 35 36 30 20 2a 2f 20 20 20 20 39 33 2c 20    560 */    93, 
29fab 20 20 39 34 2c 20 20 20 39 35 2c 20 20 20 32 30    94,   95,   20
29fac 2c 20 20 31 38 37 2c 20 20 31 34 38 2c 20 20 20  ,  187,  148,   
29fad 31 39 2c 20 20 20 32 30 2c 20 20 31 34 38 2c 20  19,   20,  148, 
29fae 20 20 32 32 2c 0a 20 2f 2a 20 20 20 35 37 30 20    22,. /*   570 
29faf 2a 2f 20 20 20 20 31 39 2c 20 20 31 30 34 2c 20  */    19,  104, 
29fb0 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 32 33   170,  171,   23
29fb1 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 31  ,   42,   43,  1
29fb2 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 0a  48,  170,  171,.
29fb3 20 2f 2a 20 20 20 35 38 30 20 2a 2f 20 20 20 31   /*   580 */   1
29fb4 31 34 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  14,  170,  171, 
29fb5 20 32 32 39 2c 20 20 20 39 39 2c 20 20 31 30 30   229,   99,  100
29fb6 2c 20 20 31 30 31 2c 20 20 31 37 30 2c 20 20 31  ,  101,  170,  1
29fb7 37 31 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20 20  71,  148,. /*   
29fb8 35 39 30 20 2a 2f 20 20 20 31 37 30 2c 20 20 31  590 */   170,  1
29fb9 37 31 2c 20 20 31 39 30 2c 20 20 20 36 30 2c 20  71,  190,   60, 
29fba 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29fbb 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29fbc 36 36 2c 0a 20 2f 2a 20 20 20 36 30 30 20 2a 2f  66,. /*   600 */
29fbd 20 20 20 20 36 37 2c 20 20 20 36 38 2c 20 20 20      67,   68,   
29fbe 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29fbf 20 20 37 32 2c 20 20 20 35 39 2c 20 20 20 37 34    72,   59,   74
29fc0 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 0a 20 2f  ,   75,   76,. /
29fc1 2a 20 20 20 36 31 30 20 2a 2f 20 20 20 20 37 37  *   610 */    77
29fc2 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
29fc3 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29fc4 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 31 36    83,   84,   16
29fc5 2c 20 20 32 31 31 2c 0a 20 2f 2a 20 20 20 36 32  ,  211,. /*   62
29fc6 30 20 2a 2f 20 20 20 32 31 32 2c 20 20 20 31 32  0 */   212,   12
29fc7 2c 20 20 31 34 38 2c 20 20 20 32 31 2c 20 20 32  ,  148,   21,  2
29fc8 31 33 2c 20 20 31 36 35 2c 20 20 31 36 36 2c 20  13,  165,  166, 
29fc9 20 31 36 37 2c 20 20 20 32 31 2c 20 20 31 34 38   167,   21,  148
29fca 2c 0a 20 2f 2a 20 20 20 36 33 30 20 2a 2f 20 20  ,. /*   630 */  
29fcb 20 20 32 33 2c 20 20 31 34 38 2c 20 20 31 34 38    23,  148,  148
29fcc 2c 20 20 20 32 34 2c 20 20 32 31 37 2c 20 20 20  ,   24,  217,   
29fcd 31 39 2c 20 20 20 32 30 2c 20 20 32 31 37 2c 20  19,   20,  217, 
29fce 20 20 32 32 2c 20 20 20 38 38 2c 0a 20 2f 2a 20    22,   88,. /* 
29fcf 20 20 36 34 30 20 2a 2f 20 20 20 20 38 39 2c 20    640 */    89, 
29fd0 20 31 35 37 2c 20 20 31 35 38 2c 20 20 32 31 34   157,  158,  214
29fd1 2c 20 20 20 34 32 2c 20 20 20 34 33 2c 20 20 20  ,   42,   43,   
29fd2 33 37 2c 20 20 31 34 38 2c 20 20 20 33 39 2c 20  37,  148,   39, 
29fd3 20 31 30 36 2c 0a 20 2f 2a 20 20 20 36 35 30 20   106,. /*   650 
29fd4 2a 2f 20 20 20 31 34 38 2c 20 20 31 30 38 2c 20  */   148,  108, 
29fd5 20 31 30 39 2c 20 20 31 37 30 2c 20 20 31 37 31   109,  170,  171
29fd6 2c 20 20 20 32 30 2c 20 20 31 34 36 2c 20 20 31  ,   20,  146,  1
29fd7 38 33 2c 20 20 20 34 39 2c 20 20 31 35 36 2c 0a  83,   49,  156,.
29fd8 20 2f 2a 20 20 20 36 36 30 20 2a 2f 20 20 20 20   /*   660 */    
29fd9 31 34 2c 20 20 20 31 36 2c 20 20 20 36 30 2c 20  14,   16,   60, 
29fda 20 20 36 31 2c 20 20 20 36 32 2c 20 20 20 36 33    61,   62,   63
29fdb 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20 20 20  ,   64,   65,   
29fdc 36 36 2c 20 20 20 36 37 2c 0a 20 2f 2a 20 20 20  66,   67,. /*   
29fdd 36 37 30 20 2a 2f 20 20 20 20 36 38 2c 20 20 20  670 */    68,   
29fde 36 39 2c 20 20 20 37 30 2c 20 20 20 37 31 2c 20  69,   70,   71, 
29fdf 20 20 37 32 2c 20 20 20 35 39 2c 20 20 20 37 34    72,   59,   74
29fe0 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20 20 20  ,   75,   76,   
29fe1 37 37 2c 0a 20 2f 2a 20 20 20 36 38 30 20 2a 2f  77,. /*   680 */
29fe2 20 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20      78,   79,   
29fe3 38 30 2c 20 20 20 38 31 2c 20 20 20 38 32 2c 20  80,   81,   82, 
29fe4 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20 34 32    83,   84,   42
29fe5 2c 20 20 20 34 33 2c 20 20 32 32 39 2c 0a 20 2f  ,   43,  229,. /
29fe6 2a 20 20 20 36 39 30 20 2a 2f 20 20 20 20 39 39  *   690 */    99
29fe7 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 20 20 31  ,  100,  101,  1
29fe8 39 30 2c 20 20 31 30 36 2c 20 20 20 38 38 2c 20  90,  106,   88, 
29fe9 20 31 30 38 2c 20 20 31 30 39 2c 20 20 20 35 32   108,  109,   52
29fea 2c 20 20 20 31 34 2c 0a 20 2f 2a 20 20 20 37 30  ,   14,. /*   70
29feb 30 20 2a 2f 20 20 20 20 35 34 2c 20 20 31 34 38  0 */    54,  148
29fec 2c 20 20 31 35 36 2c 20 20 31 36 32 2c 20 20 20  ,  156,  162,   
29fed 31 36 2c 20 20 20 36 30 2c 20 20 20 36 31 2c 20  16,   60,   61, 
29fee 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20 36 34    62,   63,   64
29fef 2c 0a 20 2f 2a 20 20 20 37 31 30 20 2a 2f 20 20  ,. /*   710 */  
29ff0 20 20 36 35 2c 20 20 20 36 36 2c 20 20 20 36 37    65,   66,   67
29ff1 2c 20 20 20 36 38 2c 20 20 20 36 39 2c 20 20 20  ,   68,   69,   
29ff2 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32 2c 20  70,   71,   72, 
29ff3 20 32 31 35 2c 20 20 20 37 34 2c 0a 20 2f 2a 20   215,   74,. /* 
29ff4 20 20 37 32 30 20 2a 2f 20 20 20 20 37 35 2c 20    720 */    75, 
29ff5 20 20 37 36 2c 20 20 20 37 37 2c 20 20 20 37 38    76,   77,   78
29ff6 2c 20 20 20 37 39 2c 20 20 20 38 30 2c 20 20 20  ,   79,   80,   
29ff7 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33 2c 20  81,   82,   83, 
29ff8 20 20 38 34 2c 0a 20 2f 2a 20 20 20 37 33 30 20    84,. /*   730 
29ff9 2a 2f 20 20 20 20 34 32 2c 20 20 20 34 33 2c 20  */    42,   43, 
29ffa 20 32 34 35 2c 20 20 32 34 36 2c 20 20 31 34 38   245,  246,  148
29ffb 2c 20 20 31 34 38 2c 20 20 31 39 30 2c 20 20 20  ,  148,  190,   
29ffc 35 32 2c 20 20 31 39 33 2c 20 20 20 35 34 2c 0a  52,  193,   54,.
29ffd 20 2f 2a 20 20 20 37 34 30 20 2a 2f 20 20 20 32   /*   740 */   2
29ffe 33 37 2c 20 20 31 30 36 2c 20 20 32 30 31 2c 20  37,  106,  201, 
29fff 20 31 30 38 2c 20 20 31 30 39 2c 20 20 32 30 30   108,  109,  200
2a000 2c 20 20 31 34 38 2c 20 20 20 31 36 2c 20 20 20  ,  148,   16,   
2a001 36 30 2c 20 20 20 36 31 2c 0a 20 2f 2a 20 20 20  60,   61,. /*   
2a002 37 35 30 20 2a 2f 20 20 20 20 36 32 2c 20 20 20  750 */    62,   
2a003 36 33 2c 20 20 20 36 34 2c 20 20 20 36 35 2c 20  63,   64,   65, 
2a004 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20 36 38    66,   67,   68
2a005 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20 20 20  ,   69,   70,   
2a006 37 31 2c 0a 20 2f 2a 20 20 20 37 36 30 20 2a 2f  71,. /*   760 */
2a007 20 20 20 20 37 32 2c 20 20 32 31 35 2c 20 20 20      72,  215,   
2a008 37 34 2c 20 20 20 37 35 2c 20 20 20 37 36 2c 20  74,   75,   76, 
2a009 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20 37 39    77,   78,   79
2a00a 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 0a 20 2f  ,   80,   81,. /
2a00b 2a 20 20 20 37 37 30 20 2a 2f 20 20 20 20 38 32  *   770 */    82
2a00c 2c 20 20 20 38 33 2c 20 20 20 38 34 2c 20 20 20  ,   83,   84,   
2a00d 34 32 2c 20 20 20 34 33 2c 20 20 20 32 35 2c 20  42,   43,   25, 
2a00e 20 31 38 39 2c 20 20 20 32 32 2c 20 20 32 33 32   189,   22,  232
2a00f 2c 20 20 20 32 39 2c 0a 20 2f 2a 20 20 20 37 38  ,   29,. /*   78
2a010 30 20 2a 2f 20 20 20 31 33 34 2c 20 20 31 38 33  0 */   134,  183
2a011 2c 20 20 20 31 36 2c 20 20 32 33 37 2c 20 20 32  ,   16,  237,  2
2a012 30 32 2c 20 20 32 30 33 2c 20 20 31 34 38 2c 20  02,  203,  148, 
2a013 20 31 34 38 2c 20 20 31 39 32 2c 20 20 31 34 38   148,  192,  148
2a014 2c 0a 20 2f 2a 20 20 20 37 39 30 20 2a 2f 20 20  ,. /*   790 */  
2a015 20 20 31 36 2c 20 20 20 34 31 2c 20 20 20 36 31    16,   41,   61
2a016 2c 20 20 20 36 32 2c 20 20 20 36 33 2c 20 20 20  ,   62,   63,   
2a017 36 34 2c 20 20 20 36 35 2c 20 20 20 36 36 2c 20  64,   65,   66, 
2a018 20 20 36 37 2c 20 20 20 36 38 2c 0a 20 2f 2a 20    67,   68,. /* 
2a019 20 20 38 30 30 20 2a 2f 20 20 20 20 36 39 2c 20    800 */    69, 
2a01a 20 20 37 30 2c 20 20 20 37 31 2c 20 20 20 37 32    70,   71,   72
2a01b 2c 20 20 31 34 38 2c 20 20 20 37 34 2c 20 20 20  ,  148,   74,   
2a01c 37 35 2c 20 20 20 37 36 2c 20 20 20 37 37 2c 20  75,   76,   77, 
2a01d 20 20 37 38 2c 0a 20 2f 2a 20 20 20 38 31 30 20    78,. /*   810 
2a01e 2a 2f 20 20 20 20 37 39 2c 20 20 20 38 30 2c 20  */    79,   80, 
2a01f 20 20 38 31 2c 20 20 20 38 32 2c 20 20 20 38 33    81,   82,   83
2a020 2c 20 20 20 38 34 2c 20 20 20 34 32 2c 20 20 20  ,   84,   42,   
2a021 34 33 2c 20 20 20 32 33 2c 20 20 20 31 39 2c 0a  43,   23,   19,.
2a022 20 2f 2a 20 20 20 38 32 30 20 2a 2f 20 20 20 20   /*   820 */    
2a023 32 33 2c 20 20 31 38 33 2c 20 20 31 34 38 2c 20  23,  183,  148, 
2a024 20 20 32 33 2c 20 20 31 34 38 2c 20 20 31 34 38    23,  148,  148
2a025 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
2a026 38 39 2c 20 20 20 31 39 2c 0a 20 2f 2a 20 20 20  89,   19,. /*   
2a027 38 33 30 20 2a 2f 20 20 20 31 38 39 2c 20 20 31  830 */   189,  1
2a028 30 37 2c 20 20 31 34 38 2c 20 20 20 32 33 2c 20  07,  148,   23, 
2a029 20 31 34 38 2c 20 20 31 31 31 2c 20 20 20 36 32   148,  111,   62
2a02a 2c 20 20 20 36 33 2c 20 20 20 36 34 2c 20 20 20  ,   63,   64,   
2a02b 36 35 2c 0a 20 2f 2a 20 20 20 38 34 30 20 2a 2f  65,. /*   840 */
2a02c 20 20 20 20 36 36 2c 20 20 20 36 37 2c 20 20 20      66,   67,   
2a02d 36 38 2c 20 20 20 36 39 2c 20 20 20 37 30 2c 20  68,   69,   70, 
2a02e 20 20 37 31 2c 20 20 20 37 32 2c 20 20 31 34 38    71,   72,  148
2a02f 2c 20 20 20 37 34 2c 20 20 20 37 35 2c 0a 20 2f  ,   74,   75,. /
2a030 2a 20 20 20 38 35 30 20 2a 2f 20 20 20 20 37 36  *   850 */    76
2a031 2c 20 20 20 37 37 2c 20 20 20 37 38 2c 20 20 20  ,   77,   78,   
2a032 37 39 2c 20 20 20 38 30 2c 20 20 20 38 31 2c 20  79,   80,   81, 
2a033 20 20 38 32 2c 20 20 20 38 33 2c 20 20 20 38 34    82,   83,   84
2a034 2c 20 20 20 31 36 2c 0a 20 2f 2a 20 20 20 38 36  ,   16,. /*   86
2a035 30 20 2a 2f 20 20 20 20 31 37 2c 20 20 31 34 38  0 */    17,  148
2a036 2c 20 20 20 31 39 2c 20 20 31 38 39 2c 20 20 31  ,   19,  189,  1
2a037 34 38 2c 20 20 31 38 39 2c 20 20 20 32 33 2c 20  48,  189,   23, 
2a038 20 31 34 38 2c 20 20 31 31 33 2c 20 20 31 37 30   148,  113,  170
2a039 2c 0a 20 2f 2a 20 20 20 38 37 30 20 2a 2f 20 20  ,. /*   870 */  
2a03a 20 31 37 31 2c 20 20 31 34 38 2c 20 20 20 31 36   171,  148,   16
2a03b 2c 20 20 31 34 38 2c 20 20 20 33 31 2c 20 20 20  ,  148,   31,   
2a03c 31 36 2c 20 20 20 31 37 2c 20 20 20 38 30 2c 20  16,   17,   80, 
2a03d 20 20 31 39 2c 20 20 31 36 32 2c 0a 20 2f 2a 20    19,  162,. /* 
2a03e 20 20 38 38 30 20 2a 2f 20 20 20 31 34 38 2c 20    880 */   148, 
2a03f 20 31 31 35 2c 20 20 20 32 33 2c 20 20 20 38 38   115,   23,   88
2a040 2c 20 20 20 38 39 2c 20 20 20 38 38 2c 20 20 20  ,   89,   88,   
2a041 38 39 2c 20 20 32 31 30 2c 20 20 20 38 38 2c 20  89,  210,   88, 
2a042 20 20 38 39 2c 0a 20 2f 2a 20 20 20 38 39 30 20    89,. /*   890 
2a043 2a 2f 20 20 20 20 33 31 2c 20 20 20 34 38 2c 20  */    31,   48, 
2a044 20 31 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31   148,  170,  171
2a045 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
2a046 34 38 2c 20 20 20 38 38 2c 20 20 20 34 33 2c 0a  48,   88,   43,.
2a047 20 2f 2a 20 20 20 39 30 30 20 2a 2f 20 20 20 32   /*   900 */   2
2a048 31 34 2c 20 20 20 35 38 2c 20 20 31 37 30 2c 20  14,   58,  170, 
2a049 20 31 37 31 2c 20 20 31 34 38 2c 20 20 31 38 39   171,  148,  189
2a04a 2c 20 20 31 34 38 2c 20 20 20 34 38 2c 20 20 31  ,  148,   48,  1
2a04b 38 39 2c 20 20 31 31 34 2c 0a 20 2f 2a 20 20 20  89,  114,. /*   
2a04c 39 31 30 20 2a 2f 20 20 20 20 31 39 2c 20 20 31  910 */    19,  1
2a04d 34 38 2c 20 20 32 34 33 2c 20 20 32 34 34 2c 20  48,  243,  244, 
2a04e 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 34 38   170,  171,  148
2a04f 2c 20 20 20 35 38 2c 20 20 31 34 38 2c 20 20 31  ,   58,  148,  1
2a050 37 30 2c 0a 20 2f 2a 20 20 20 39 32 30 20 2a 2f  70,. /*   920 */
2a051 20 20 20 31 37 31 2c 20 20 20 37 38 2c 20 20 20     171,   78,   
2a052 37 39 2c 20 20 32 31 30 2c 20 20 31 34 38 2c 20  79,  210,  148, 
2a053 20 20 33 30 2c 20 20 31 37 30 2c 20 20 31 37 31    30,  170,  171
2a054 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 0a 20 2f  ,  170,  171,. /
2a055 2a 20 20 20 39 33 30 20 2a 2f 20 20 20 20 38 37  *   930 */    87
2a056 2c 20 20 20 38 38 2c 20 20 20 38 39 2c 20 20 31  ,   88,   89,  1
2a057 37 30 2c 20 20 31 37 31 2c 20 20 20 39 32 2c 20  70,  171,   92, 
2a058 20 31 34 38 2c 20 20 20 37 38 2c 20 20 20 37 39   148,   78,   79
2a059 2c 20 20 20 38 30 2c 0a 20 2f 2a 20 20 20 39 34  ,   80,. /*   94
2a05a 30 20 2a 2f 20 20 20 31 37 30 2c 20 20 31 37 31  0 */   170,  171
2a05b 2c 20 20 31 32 35 2c 20 20 31 32 36 2c 20 20 31  ,  125,  126,  1
2a05c 34 38 2c 20 20 20 35 30 2c 20 20 20 38 37 2c 20  48,   50,   87, 
2a05d 20 20 38 38 2c 20 20 20 38 39 2c 20 20 20 31 36    88,   89,   16
2a05e 2c 0a 20 2f 2a 20 20 20 39 35 30 20 2a 2f 20 20  ,. /*   950 */  
2a05f 20 20 31 37 2c 20 20 20 39 32 2c 20 20 20 31 39    17,   92,   19
2a060 2c 20 20 31 31 30 2c 20 20 20 39 38 2c 20 20 31  ,  110,   98,  1
2a061 34 38 2c 20 20 20 32 33 2c 20 20 31 34 38 2c 20  48,   23,  148, 
2a062 20 31 35 36 2c 20 20 31 30 33 2c 0a 20 2f 2a 20   156,  103,. /* 
2a063 20 20 39 36 30 20 2a 2f 20 20 20 31 34 38 2c 20    960 */   148, 
2a064 20 31 31 30 2c 20 20 31 35 36 2c 20 20 20 32 32   110,  156,   22
2a065 2c 20 20 20 33 31 2c 20 20 31 38 39 2c 20 20 31  ,   31,  189,  1
2a066 37 30 2c 20 20 31 37 31 2c 20 20 31 32 35 2c 20  70,  171,  125, 
2a067 20 31 32 36 2c 0a 20 2f 2a 20 20 20 39 37 30 20   126,. /*   970 
2a068 2a 2f 20 20 20 31 32 37 2c 20 20 31 32 38 2c 20  */   127,  128, 
2a069 20 31 32 39 2c 20 20 31 33 30 2c 20 20 20 32 30   129,  130,   20
2a06a 2c 20 20 31 32 34 2c 20 20 31 35 36 2c 20 20 31  ,  124,  156,  1
2a06b 37 30 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 0a  70,  171,  170,.
2a06c 20 2f 2a 20 20 20 39 38 30 20 2a 2f 20 20 20 31   /*   980 */   1
2a06d 37 31 2c 20 20 20 34 38 2c 20 20 31 37 30 2c 20  71,   48,  170, 
2a06e 20 31 37 31 2c 20 20 31 32 35 2c 20 20 31 32 36   171,  125,  126
2a06f 2c 20 20 31 32 37 2c 20 20 31 32 38 2c 20 20 31  ,  127,  128,  1
2a070 32 39 2c 20 20 31 33 30 2c 0a 20 2f 2a 20 20 20  29,  130,. /*   
2a071 39 39 30 20 2a 2f 20 20 20 32 30 34 2c 20 20 20  990 */   204,   
2a072 35 38 2c 20 20 31 39 30 2c 20 20 20 20 35 2c 20  58,  190,    5, 
2a073 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31 39 30   148,  148,  190
2a074 2c 20 20 31 30 32 2c 20 20 20 31 30 2c 20 20 20  ,  102,   10,   
2a075 31 31 2c 0a 20 2f 2a 20 20 31 30 30 30 20 2a 2f  11,. /*  1000 */
2a076 20 20 20 20 31 32 2c 20 20 20 31 33 2c 20 20 31      12,   13,  1
2a077 34 38 2c 20 20 31 31 32 2c 20 20 20 32 32 2c 20  48,  112,   22, 
2a078 20 31 34 38 2c 20 20 31 34 38 2c 20 20 20 32 30   148,  148,   20
2a079 2c 20 20 31 34 38 2c 20 20 20 32 32 2c 0a 20 2f  ,  148,   22,. /
2a07a 2a 20 20 31 30 31 30 20 2a 2f 20 20 20 31 39 30  *  1010 */   190
2a07b 2c 20 20 20 37 38 2c 20 20 20 37 39 2c 20 20 20  ,   78,   79,   
2a07c 35 39 2c 20 20 20 32 36 2c 20 20 20 34 33 2c 20  59,   26,   43, 
2a07d 20 20 32 38 2c 20 20 31 37 30 2c 20 20 31 37 31    28,  170,  171
2a07e 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20 31 30 32  ,  148,. /*  102
2a07f 30 20 2a 2f 20 20 20 20 38 37 2c 20 20 20 38 38  0 */    87,   88
2a080 2c 20 20 20 38 39 2c 20 20 20 33 35 2c 20 20 31  ,   89,   35,  1
2a081 34 38 2c 20 20 20 39 32 2c 20 20 31 34 38 2c 20  48,   92,  148, 
2a082 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 34 38   170,  171,  148
2a083 2c 0a 20 2f 2a 20 20 31 30 33 30 20 2a 2f 20 20  ,. /*  1030 */  
2a084 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31 34 38   170,  171,  148
2a085 2c 20 20 20 32 37 2c 20 20 31 34 38 2c 20 20 20  ,   27,  148,   
2a086 34 37 2c 20 20 31 34 38 2c 20 20 20 34 39 2c 20  47,  148,   49, 
2a087 20 20 39 37 2c 20 20 32 33 34 2c 0a 20 2f 2a 20    97,  234,. /* 
2a088 20 31 30 34 30 20 2a 2f 20 20 20 20 33 34 2c 20   1040 */    34, 
2a089 20 20 35 33 2c 20 20 31 34 38 2c 20 20 31 37 39    53,  148,  179
2a08a 2c 20 20 20 35 36 2c 20 20 31 34 38 2c 20 20 31  ,   56,  148,  1
2a08b 37 30 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 20  70,  171,  170, 
2a08c 20 31 37 31 2c 0a 20 2f 2a 20 20 31 30 35 30 20   171,. /*  1050 
2a08d 2a 2f 20 20 20 31 34 38 2c 20 20 31 37 30 2c 20  */   148,  170, 
2a08e 20 31 37 31 2c 20 20 31 34 38 2c 20 20 31 37 30   171,  148,  170
2a08f 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 20 20 31  ,  171,  170,  1
2a090 37 31 2c 20 20 31 32 35 2c 20 20 31 32 36 2c 0a  71,  125,  126,.
2a091 20 2f 2a 20 20 31 30 36 30 20 2a 2f 20 20 20 31   /*  1060 */   1
2a092 32 37 2c 20 20 31 32 38 2c 20 20 31 32 39 2c 20  27,  128,  129, 
2a093 20 31 33 30 2c 20 20 31 37 30 2c 20 20 31 37 31   130,  170,  171
2a094 2c 20 20 31 37 39 2c 20 20 31 37 30 2c 20 20 31  ,  179,  170,  1
2a095 37 31 2c 20 20 31 34 38 2c 0a 20 2f 2a 20 20 31  71,  148,. /*  1
2a096 30 37 30 20 2a 2f 20 20 20 20 39 38 2c 20 20 20  070 */    98,   
2a097 38 39 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20  89,  170,  171, 
2a098 20 31 34 38 2c 20 20 31 37 30 2c 20 20 31 37 31   148,  170,  171
2a099 2c 20 20 31 34 38 2c 20 20 20 32 30 2c 20 20 31  ,  148,   20,  1
2a09a 34 38 2c 0a 20 2f 2a 20 20 31 30 38 30 20 2a 2f  48,. /*  1080 */
2a09b 20 20 20 20 32 32 2c 20 20 20 32 30 2c 20 20 31      22,   20,  1
2a09c 34 38 2c 20 20 20 32 32 2c 20 20 31 34 38 2c 20  48,   22,  148, 
2a09d 20 31 37 39 2c 20 20 31 34 38 2c 20 20 20 39 39   179,  148,   99
2a09e 2c 20 20 31 30 30 2c 20 20 31 30 31 2c 0a 20 2f  ,  100,  101,. /
2a09f 2a 20 20 31 30 39 30 20 2a 2f 20 20 20 31 34 38  *  1090 */   148
2a0a0 2c 20 20 31 37 30 2c 20 20 31 37 31 2c 20 20 31  ,  170,  171,  1
2a0a1 30 35 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20  05,  148,  148, 
2a0a2 20 31 31 34 2c 20 20 31 34 38 2c 20 20 31 31 30   114,  148,  110
2a0a3 2c 20 20 31 37 30 2c 0a 20 2f 2a 20 20 31 31 30  ,  170,. /*  110
2a0a4 30 20 2a 2f 20 20 20 31 37 31 2c 20 20 31 37 30  0 */   171,  170
2a0a5 2c 20 20 31 37 31 2c 20 20 31 34 38 2c 20 20 31  ,  171,  148,  1
2a0a6 37 30 2c 20 20 31 37 31 2c 20 20 31 37 30 2c 20  70,  171,  170, 
2a0a7 20 31 37 31 2c 20 20 31 37 30 2c 20 20 31 37 31   171,  170,  171
2a0a8 2c 0a 20 2f 2a 20 20 31 31 31 30 20 2a 2f 20 20  ,. /*  1110 */  
2a0a9 20 20 20 37 2c 20 20 20 20 38 2c 20 20 31 37 30     7,    8,  170
2a0aa 2c 20 20 31 37 31 2c 20 20 20 32 30 2c 20 20 31  ,  171,   20,  1
2a0ab 34 38 2c 20 20 20 32 32 2c 20 20 31 37 30 2c 20  48,   22,  170, 
2a0ac 20 31 37 31 2c 20 20 31 37 30 2c 0a 20 2f 2a 20   171,  170,. /* 
2a0ad 20 31 31 32 30 20 2a 2f 20 20 20 31 37 31 2c 20   1120 */   171, 
2a0ae 20 31 34 38 2c 20 20 20 32 30 2c 20 20 31 33 35   148,   20,  135
2a0af 2c 20 20 20 32 32 2c 20 20 31 37 30 2c 20 20 31  ,   22,  170,  1
2a0b0 37 31 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20  71,  148,  148, 
2a0b1 20 20 39 31 2c 0a 20 2f 2a 20 20 31 31 33 30 20    91,. /*  1130 
2a0b2 2a 2f 20 20 20 20 39 32 2c 20 20 20 32 30 2c 20  */    92,   20, 
2a0b3 20 20 32 30 2c 20 20 20 32 32 2c 20 20 20 32 32    20,   22,   22
2a0b4 2c 20 20 31 35 30 2c 20 20 32 33 33 2c 20 20 31  ,  150,  233,  1
2a0b5 37 30 2c 20 20 31 37 31 2c 20 20 20 32 30 2c 0a  70,  171,   20,.
2a0b6 20 2f 2a 20 20 31 31 34 30 20 2a 2f 20 20 20 20   /*  1140 */    
2a0b7 32 30 2c 20 20 20 32 32 2c 20 20 20 32 32 2c 20  20,   22,   22, 
2a0b8 20 31 37 30 2c 20 20 31 37 31 2c 20 20 20 32 30   170,  171,   20
2a0b9 2c 20 20 31 34 38 2c 20 20 20 32 32 2c 20 20 31  ,  148,   22,  1
2a0ba 34 38 2c 20 20 31 37 30 2c 0a 20 2f 2a 20 20 31  48,  170,. /*  1
2a0bb 31 35 30 20 2a 2f 20 20 20 31 37 31 2c 20 20 31  150 */   171,  1
2a0bc 34 38 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20  48,  148,  148, 
2a0bd 20 31 39 32 2c 20 20 31 34 38 2c 20 20 31 34 38   192,  148,  148
2a0be 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31  ,  148,  148,  1
2a0bf 34 38 2c 0a 20 2f 2a 20 20 31 31 36 30 20 2a 2f  48,. /*  1160 */
2a0c0 20 20 20 31 34 38 2c 20 20 31 34 38 2c 20 20 31     148,  148,  1
2a0c1 34 38 2c 20 20 31 34 38 2c 20 20 31 34 38 2c 20  48,  148,  148, 
2a0c2 20 31 34 38 2c 20 20 31 37 33 2c 20 20 32 33 30   148,  173,  230
2a0c3 2c 20 20 31 39 34 2c 20 20 32 33 30 2c 0a 20 2f  ,  194,  230,. /
2a0c4 2a 20 20 31 31 37 30 20 2a 2f 20 20 20 32 32 35  *  1170 */   225
2a0c5 2c 20 20 32 30 35 2c 20 20 31 37 33 2c 20 20 31  ,  205,  173,  1
2a0c6 37 38 2c 20 20 31 37 33 2c 20 20 31 37 33 2c 20  78,  173,  173, 
2a0c7 20 31 39 35 2c 20 20 20 20 36 2c 20 20 31 34 37   195,    6,  147
2a0c8 2c 20 20 31 39 35 2c 0a 20 2f 2a 20 20 31 31 38  ,  195,. /*  118
2a0c9 30 20 2a 2f 20 20 20 31 36 32 2c 20 20 31 36 32  0 */   162,  162
2a0ca 2c 20 20 32 30 35 2c 20 20 31 36 32 2c 20 20 31  ,  205,  162,  1
2a0cb 34 37 2c 20 20 20 32 32 2c 20 20 31 34 37 2c 20  47,   22,  147, 
2a0cc 20 31 34 37 2c 20 20 32 30 35 2c 20 20 31 35 35   147,  205,  155
2a0cd 2c 0a 20 2f 2a 20 20 31 31 39 30 20 2a 2f 20 20  ,. /*  1190 */  
2a0ce 20 31 32 32 2c 20 20 31 39 35 2c 20 20 31 31 39   122,  195,  119
2a0cf 2c 20 20 31 37 33 2c 20 20 31 32 30 2c 20 20 31  ,  173,  120,  1
2a0d0 31 38 2c 20 20 31 37 34 2c 20 20 31 32 31 2c 20  18,  174,  121, 
2a0d1 20 31 33 31 2c 20 20 32 32 34 2c 0a 20 2f 2a 20   131,  224,. /* 
2a0d2 20 31 32 30 30 20 2a 2f 20 20 20 31 31 32 2c 20   1200 */   112, 
2a0d3 20 31 35 33 2c 20 20 31 35 33 2c 20 20 20 39 38   153,  153,   98
2a0d4 2c 20 20 31 31 37 2c 20 20 20 39 38 2c 20 20 20  ,  117,   98,   
2a0d5 34 30 2c 20 20 31 37 32 2c 20 20 31 37 32 2c 20  40,  172,  172, 
2a0d6 20 20 31 39 2c 0a 20 2f 2a 20 20 31 32 31 30 20    19,. /*  1210 
2a0d7 2a 2f 20 20 20 20 39 37 2c 20 20 20 38 34 2c 20  */    97,   84, 
2a0d8 20 20 31 35 2c 20 20 31 39 30 2c 20 20 31 37 32    15,  190,  172
2a0d9 2c 20 20 31 35 32 2c 20 20 31 37 32 2c 20 20 31  ,  152,  172,  1
2a0da 37 34 2c 20 20 32 32 37 2c 20 20 32 32 37 2c 0a  74,  227,  227,.
2a0db 20 2f 2a 20 20 31 32 32 30 20 2a 2f 20 20 20 31   /*  1220 */   1
2a0dc 39 36 2c 20 20 31 38 30 2c 20 20 31 39 37 2c 20  96,  180,  197, 
2a0dd 20 31 37 32 2c 20 20 31 39 38 2c 20 20 31 37 35   172,  198,  175
2a0de 2c 20 20 31 39 39 2c 20 20 31 38 30 2c 20 20 31  ,  199,  180,  1
2a0df 37 32 2c 20 20 31 37 32 2c 0a 20 2f 2a 20 20 31  72,  172,. /*  1
2a0e0 32 33 30 20 2a 2f 20 20 20 31 37 35 2c 20 20 31  230 */   175,  1
2a0e1 35 33 2c 20 20 31 35 32 2c 20 20 31 35 32 2c 20  53,  152,  152, 
2a0e2 20 32 30 36 2c 20 20 31 35 33 2c 20 20 32 30 37   206,  153,  207
2a0e3 2c 20 20 32 30 37 2c 20 20 32 30 36 2c 20 20 31  ,  207,  206,  1
2a0e4 35 33 2c 0a 20 2f 2a 20 20 31 32 34 30 20 2a 2f  53,. /*  1240 */
2a0e5 20 20 20 20 33 38 2c 20 20 31 33 31 2c 20 20 31      38,  131,  1
2a0e6 35 33 2c 20 20 31 35 32 2c 20 20 20 36 30 2c 20  53,  152,   60, 
2a0e7 20 31 35 33 2c 20 20 20 31 39 2c 20 20 31 38 35   153,   19,  185
2a0e8 2c 20 20 31 39 35 2c 20 20 31 38 35 2c 0a 20 2f  ,  195,  185,. /
2a0e9 2a 20 20 31 32 35 30 20 2a 2f 20 20 20 31 35 33  *  1250 */   153
2a0ea 2c 20 20 20 31 35 2c 20 20 31 35 33 2c 20 20 31  ,   15,  153,  1
2a0eb 38 38 2c 20 20 31 38 38 2c 20 20 31 39 35 2c 20  88,  188,  195, 
2a0ec 20 31 38 35 2c 20 20 31 38 38 2c 20 20 31 38 38   185,  188,  188
2a0ed 2c 20 20 20 33 33 2c 0a 20 2f 2a 20 20 31 32 36  ,   33,. /*  126
2a0ee 30 20 2a 2f 20 20 20 31 33 38 2c 20 20 31 35 33  0 */   138,  153
2a0ef 2c 20 20 32 31 36 2c 20 20 20 20 31 2c 20 20 31  ,  216,    1,  1
2a0f0 36 30 2c 20 20 20 32 30 2c 20 20 31 37 36 2c 20  60,   20,  176, 
2a0f1 20 31 35 33 2c 20 20 32 33 35 2c 20 20 31 37 36   153,  235,  176
2a0f2 2c 0a 20 2f 2a 20 20 31 32 37 30 20 2a 2f 20 20  ,. /*  1270 */  
2a0f3 20 32 31 36 2c 20 20 31 31 32 2c 20 20 31 31 32   216,  112,  112
2a0f4 2c 20 20 31 31 32 2c 20 20 31 31 32 2c 20 20 20  ,  112,  112,   
2a0f5 39 32 2c 20 20 31 30 37 2c 20 20 20 31 39 2c 20  92,  107,   19, 
2a0f6 20 20 31 31 2c 20 20 20 32 30 2c 0a 20 2f 2a 20    11,   20,. /* 
2a0f7 20 31 32 38 30 20 2a 2f 20 20 20 20 32 30 2c 20   1280 */    20, 
2a0f8 20 32 33 36 2c 20 20 20 31 39 2c 20 20 20 31 39   236,   19,   19
2a0f9 2c 20 20 31 31 36 2c 20 20 20 32 30 2c 20 20 31  ,  116,   20,  1
2a0fa 31 36 2c 20 20 31 31 34 2c 20 20 20 32 32 2c 20  16,  114,   22, 
2a0fb 20 20 32 30 2c 0a 20 2f 2a 20 20 31 32 39 30 20    20,. /*  1290 
2a0fc 2a 2f 20 20 20 32 33 38 2c 20 20 20 32 30 2c 20  */   238,   20, 
2a0fd 20 20 32 32 2c 20 20 20 31 39 2c 20 20 20 32 32    22,   19,   22
2a0fe 2c 20 20 31 31 36 2c 20 20 31 31 35 2c 20 20 32  ,  116,  115,  2
2a0ff 33 38 2c 20 20 20 32 30 2c 20 20 31 31 32 2c 0a  38,   20,  112,.
2a100 20 2f 2a 20 20 31 33 30 30 20 2a 2f 20 20 20 20   /*  1300 */    
2a101 32 30 2c 20 20 20 32 30 2c 20 20 20 31 39 2c 20  20,   20,   19, 
2a102 20 20 31 39 2c 20 20 20 31 39 2c 20 20 20 33 32    19,   19,   32
2a103 2c 20 20 20 32 30 2c 20 20 20 31 39 2c 20 20 20  ,   20,   19,   
2a104 31 39 2c 20 20 20 39 36 2c 0a 20 2f 2a 20 20 31  19,   96,. /*  1
2a105 33 31 30 20 2a 2f 20 20 20 31 30 33 2c 20 20 20  310 */   103,   
2a106 31 36 2c 20 20 20 34 34 2c 20 20 32 34 31 2c 20  16,   44,  241, 
2a107 20 20 31 37 2c 20 20 20 32 31 2c 20 20 20 39 38    17,   21,   98
2a108 2c 20 20 20 39 38 2c 20 20 20 33 36 2c 20 20 32  ,   98,   36,  2
2a109 34 34 2c 0a 20 2f 2a 20 20 31 33 32 30 20 2a 2f  44,. /*  1320 */
2a10a 20 20 20 20 34 34 2c 20 20 20 34 34 2c 20 20 20      44,   44,   
2a10b 32 32 2c 20 20 31 33 34 2c 20 20 20 31 39 2c 20  22,  134,   19, 
2a10c 20 20 20 35 2c 20 20 32 34 37 2c 20 20 20 20 31     5,  247,    1
2a10d 2c 20 20 31 32 33 2c 20 20 20 36 38 2c 0a 20 2f  ,  123,   68,. /
2a10e 2a 20 20 31 33 33 30 20 2a 2f 20 20 20 20 35 31  *  1330 */    51
2a10f 2c 20 20 31 30 32 2c 20 20 20 34 35 2c 20 20 20  ,  102,   45,   
2a110 31 39 2c 20 20 31 31 33 2c 20 20 20 34 35 2c 20  19,  113,   45, 
2a111 20 20 20 31 2c 20 20 20 31 34 2c 20 20 20 31 37     1,   14,   17
2a112 2c 20 20 31 31 37 2c 0a 20 2f 2a 20 20 31 33 34  ,  117,. /*  134
2a113 30 20 2a 2f 20 20 20 31 30 32 2c 20 20 31 31 33  0 */   102,  113
2a114 2c 20 20 20 32 30 2c 20 20 20 36 38 2c 20 20 20  ,   20,   68,   
2a115 31 39 2c 20 20 20 31 34 2c 20 20 31 32 33 2c 20  19,   14,  123, 
2a116 20 31 33 36 2c 20 20 31 32 34 2c 20 20 20 35 37   136,  124,   57
2a117 2c 0a 20 2f 2a 20 20 31 33 35 30 20 2a 2f 20 20  ,. /*  1350 */  
2a118 20 20 20 33 2c 20 20 31 33 37 2c 20 20 20 31 39     3,  137,   19
2a119 2c 20 20 20 20 34 2c 0a 7d 3b 0a 23 64 65 66 69  ,    4,.};.#defi
2a11a 6e 65 20 59 59 5f 53 48 49 46 54 5f 55 53 45 5f  ne YY_SHIFT_USE_
2a11b 44 46 4c 54 20 28 2d 39 39 29 0a 23 64 65 66 69  DFLT (-99).#defi
2a11c 6e 65 20 59 59 5f 53 48 49 46 54 5f 4d 41 58 20  ne YY_SHIFT_MAX 
2a11d 33 39 36 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  396.static const
2a11e 20 73 68 6f 72 74 20 79 79 5f 73 68 69 66 74 5f   short yy_shift_
2a11f 6f 66 73 74 5b 5d 20 3d 20 7b 0a 20 2f 2a 20 20  ofst[] = {. /*  
2a120 20 20 20 30 20 2a 2f 20 20 20 20 32 33 2c 20 20     0 */    23,  
2a121 38 34 33 2c 20 20 39 38 38 2c 20 20 2d 31 36 2c  843,  988,  -16,
2a122 20 20 38 34 33 2c 20 20 39 33 33 2c 20 20 39 33    843,  933,  93
2a123 33 2c 20 20 33 39 33 2c 20 20 31 32 33 2c 20 20  3,  393,  123,  
2a124 32 35 32 2c 0a 20 2f 2a 20 20 20 20 31 30 20 2a  252,. /*    10 *
2a125 2f 20 20 20 2d 39 38 2c 20 20 20 39 36 2c 20 20  /   -98,   96,  
2a126 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
2a127 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 2d 34    933,  933,  -4
2a128 35 2c 20 20 32 34 39 2c 20 20 31 37 34 2c 0a 20  5,  249,  174,. 
2a129 2f 2a 20 20 20 20 32 30 20 2a 2f 20 20 20 33 34  /*    20 */   34
2a12a 30 2c 20 20 2d 31 37 2c 20 20 20 31 39 2c 20 20  0,  -17,   19,  
2a12b 20 31 39 2c 20 20 20 35 33 2c 20 20 31 36 35 2c   19,   53,  165,
2a12c 20 20 32 30 38 2c 20 20 32 35 31 2c 20 20 33 32    208,  251,  32
2a12d 36 2c 20 20 33 39 35 2c 0a 20 2f 2a 20 20 20 20  6,  395,. /*    
2a12e 33 30 20 2a 2f 20 20 20 34 36 34 2c 20 20 35 33  30 */   464,  53
2a12f 33 2c 20 20 36 30 32 2c 20 20 36 34 35 2c 20 20  3,  602,  645,  
2a130 36 38 38 2c 20 20 36 34 35 2c 20 20 36 34 35 2c  688,  645,  645,
2a131 20 20 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34    645,  645,  64
2a132 35 2c 0a 20 2f 2a 20 20 20 20 34 30 20 2a 2f 20  5,. /*    40 */ 
2a133 20 20 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34    645,  645,  64
2a134 35 2c 20 20 36 34 35 2c 20 20 36 34 35 2c 20 20  5,  645,  645,  
2a135 36 34 35 2c 20 20 36 34 35 2c 20 20 36 34 35 2c  645,  645,  645,
2a136 20 20 36 34 35 2c 20 20 36 34 35 2c 0a 20 2f 2a    645,  645,. /*
2a137 20 20 20 20 35 30 20 2a 2f 20 20 20 36 34 35 2c      50 */   645,
2a138 20 20 36 34 35 2c 20 20 36 34 35 2c 20 20 37 33    645,  645,  73
2a139 31 2c 20 20 37 37 34 2c 20 20 37 37 34 2c 20 20  1,  774,  774,  
2a13a 38 35 39 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  859,  933,  933,
2a13b 20 20 39 33 33 2c 0a 20 2f 2a 20 20 20 20 36 30    933,. /*    60
2a13c 20 2a 2f 20 20 20 39 33 33 2c 20 20 39 33 33 2c   */   933,  933,
2a13d 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33    933,  933,  93
2a13e 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  3,  933,  933,  
2a13f 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
2a140 0a 20 2f 2a 20 20 20 20 37 30 20 2a 2f 20 20 20  . /*    70 */   
2a141 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
2a142 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33    933,  933,  93
2a143 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  3,  933,  933,  
2a144 39 33 33 2c 20 20 39 33 33 2c 0a 20 2f 2a 20 20  933,  933,. /*  
2a145 20 20 38 30 20 2a 2f 20 20 20 39 33 33 2c 20 20    80 */   933,  
2a146 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
2a147 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33    933,  933,  93
2a148 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  3,  933,  933,  
2a149 39 33 33 2c 0a 20 2f 2a 20 20 20 20 39 30 20 2a  933,. /*    90 *
2a14a 2f 20 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20  /   933,  933,  
2a14b 39 33 33 2c 20 20 39 33 33 2c 20 20 39 33 33 2c  933,  933,  933,
2a14c 20 20 39 33 33 2c 20 20 39 33 33 2c 20 20 2d 36    933,  933,  -6
2a14d 31 2c 20 20 2d 36 31 2c 20 20 20 20 36 2c 0a 20  1,  -61,    6,. 
2a14e 2f 2a 20 20 20 31 30 30 20 2a 2f 20 20 20 20 20  /*   100 */     
2a14f 36 2c 20 20 32 38 31 2c 20 20 20 32 32 2c 20 20  6,  281,   22,  
2a150 20 36 31 2c 20 20 38 35 36 2c 20 20 32 38 34 2c   61,  856,  284,
2a151 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 20 36    340,  340,   6
2a152 38 2c 20 20 2d 31 37 2c 0a 20 2f 2a 20 20 20 31  8,  -17,. /*   1
2a153 31 30 20 2a 2f 20 20 20 2d 31 31 2c 20 20 2d 39  10 */   -11,  -9
2a154 39 2c 20 20 2d 39 39 2c 20 20 2d 39 39 2c 20 20  9,  -99,  -99,  
2a155 31 33 31 2c 20 20 33 32 38 2c 20 20 36 30 39 2c  131,  328,  609,
2a156 20 20 36 30 39 2c 20 20 35 34 37 2c 20 20 36 31    609,  547,  61
2a157 36 2c 0a 20 2f 2a 20 20 20 31 32 30 20 2a 2f 20  6,. /*   120 */ 
2a158 20 20 32 35 33 2c 20 20 36 30 37 2c 20 20 33 34    253,  607,  34
2a159 30 2c 20 20 36 30 37 2c 20 20 33 34 30 2c 20 20  0,  607,  340,  
2a15a 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  340,  340,  340,
2a15b 20 20 33 34 30 2c 20 20 33 34 30 2c 0a 20 2f 2a    340,  340,. /*
2a15c 20 20 20 31 33 30 20 2a 2f 20 20 20 33 34 30 2c     130 */   340,
2a15d 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34    340,  340,  34
2a15e 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  0,  340,  340,  
2a15f 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  340,  340,  340,
2a160 20 20 33 34 30 2c 0a 20 2f 2a 20 20 20 31 34 30    340,. /*   140
2a161 20 2a 2f 20 20 20 33 34 30 2c 20 20 32 33 33 2c   */   340,  233,
2a162 20 20 38 35 31 2c 20 20 2d 39 38 2c 20 20 2d 39    851,  -98,  -9
2a163 38 2c 20 20 2d 39 38 2c 20 20 2d 39 39 2c 20 20  8,  -98,  -99,  
2a164 2d 39 39 2c 20 20 2d 39 39 2c 20 20 2d 31 38 2c  -99,  -99,  -18,
2a165 0a 20 2f 2a 20 20 20 31 35 30 20 2a 2f 20 20 20  . /*   150 */   
2a166 2d 31 38 2c 20 20 34 30 35 2c 20 20 34 36 37 2c  -18,  405,  467,
2a167 20 20 33 32 37 2c 20 20 35 35 31 2c 20 20 35 34    327,  551,  54
2a168 33 2c 20 20 36 33 35 2c 20 20 33 34 33 2c 20 20  3,  635,  343,  
2a169 34 36 36 2c 20 20 37 39 35 2c 0a 20 2f 2a 20 20  466,  795,. /*  
2a16a 20 31 36 30 20 2a 2f 20 20 20 38 30 30 2c 20 20   160 */   800,  
2a16b 37 39 37 2c 20 20 34 39 36 2c 20 20 33 34 30 2c  797,  496,  340,
2a16c 20 20 33 34 30 2c 20 20 32 37 34 2c 20 20 33 34    340,  274,  34
2a16d 30 2c 20 20 33 34 30 2c 20 20 38 31 30 2c 20 20  0,  340,  810,  
2a16e 33 34 30 2c 0a 20 2f 2a 20 20 20 31 37 30 20 2a  340,. /*   170 *
2a16f 2f 20 20 20 33 34 30 2c 20 20 39 38 32 2c 20 20  /   340,  982,  
2a170 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  340,  340,  340,
2a171 20 20 35 38 38 2c 20 20 39 38 32 2c 20 20 33 34    588,  982,  34
2a172 30 2c 20 20 33 34 30 2c 20 20 38 39 35 2c 0a 20  0,  340,  895,. 
2a173 2f 2a 20 20 20 31 38 30 20 2a 2f 20 20 20 38 39  /*   180 */   89
2a174 35 2c 20 20 38 39 35 2c 20 20 33 34 30 2c 20 20  5,  895,  340,  
2a175 33 34 30 2c 20 20 33 34 30 2c 20 20 35 38 38 2c  340,  340,  588,
2a176 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 35 38    340,  340,  58
2a177 38 2c 20 20 33 34 30 2c 0a 20 2f 2a 20 20 20 31  8,  340,. /*   1
2a178 39 30 20 2a 2f 20 20 20 37 35 30 2c 20 20 34 38  90 */   750,  48
2a179 35 2c 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20  5,  340,  340,  
2a17a 35 38 38 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  588,  340,  340,
2a17b 20 20 33 34 30 2c 20 20 35 38 38 2c 20 20 33 34    340,  588,  34
2a17c 30 2c 0a 20 2f 2a 20 20 20 32 30 30 20 2a 2f 20  0,. /*   200 */ 
2a17d 20 20 33 34 30 2c 20 20 33 34 30 2c 20 20 35 38    340,  340,  58
2a17e 38 2c 20 20 35 38 38 2c 20 20 33 34 30 2c 20 20  8,  588,  340,  
2a17f 33 34 30 2c 20 20 33 34 30 2c 20 20 33 34 30 2c  340,  340,  340,
2a180 20 20 33 34 30 2c 20 20 33 34 35 2c 0a 20 2f 2a    340,  345,. /*
2a181 20 20 20 32 31 30 20 2a 2f 20 20 20 37 32 34 2c     210 */   724,
2a182 20 20 37 35 35 2c 20 20 2d 31 37 2c 20 20 38 31    755,  -17,  81
2a183 37 2c 20 20 38 31 37 2c 20 20 33 35 39 2c 20 31  7,  817,  359, 1
2a184 30 30 36 2c 20 31 30 30 36 2c 20 20 37 36 36 2c  006, 1006,  766,
2a185 20 31 30 30 36 2c 0a 20 2f 2a 20 20 20 32 32 30   1006,. /*   220
2a186 20 2a 2f 20 20 20 39 37 32 2c 20 31 30 30 36 2c   */   972, 1006,
2a187 20 20 2d 31 37 2c 20 31 30 30 36 2c 20 20 2d 31    -17, 1006,  -1
2a188 37 2c 20 20 39 34 31 2c 20 20 32 31 36 2c 20 20  7,  941,  216,  
2a189 37 36 36 2c 20 20 37 36 36 2c 20 20 32 31 36 2c  766,  766,  216,
2a18a 0a 20 2f 2a 20 20 20 32 33 30 20 2a 2f 20 20 31  . /*   230 */  1
2a18b 31 37 31 2c 20 31 31 37 31 2c 20 31 31 37 31 2c  171, 1171, 1171,
2a18c 20 31 31 37 31 2c 20 31 31 36 33 2c 20 20 2d 39   1171, 1163,  -9
2a18d 38 2c 20 31 30 36 38 2c 20 31 30 37 33 2c 20 31  8, 1068, 1073, 1
2a18e 30 37 34 2c 20 31 30 37 37 2c 0a 20 2f 2a 20 20  074, 1077,. /*  
2a18f 20 32 34 30 20 2a 2f 20 20 31 30 37 36 2c 20 31   240 */  1076, 1
2a190 30 36 37 2c 20 31 30 38 38 2c 20 31 30 38 38 2c  067, 1088, 1088,
2a191 20 31 31 30 35 2c 20 31 30 38 37 2c 20 31 31 30   1105, 1087, 110
2a192 35 2c 20 31 30 38 37 2c 20 31 31 30 37 2c 20 31  5, 1087, 1107, 1
2a193 31 30 37 2c 0a 20 2f 2a 20 20 20 32 35 30 20 2a  107,. /*   250 *
2a194 2f 20 20 31 31 36 36 2c 20 31 31 30 37 2c 20 31  /  1166, 1107, 1
2a195 31 31 33 2c 20 31 31 30 37 2c 20 31 31 39 30 2c  113, 1107, 1190,
2a196 20 31 31 32 37 2c 20 31 31 32 37 2c 20 31 31 36   1127, 1127, 116
2a197 36 2c 20 31 31 30 37 2c 20 31 31 30 37 2c 0a 20  6, 1107, 1107,. 
2a198 2f 2a 20 20 20 32 36 30 20 2a 2f 20 20 31 31 30  /*   260 */  110
2a199 37 2c 20 31 31 39 30 2c 20 31 31 39 37 2c 20 31  7, 1190, 1197, 1
2a19a 30 38 38 2c 20 31 31 39 37 2c 20 31 30 38 38 2c  088, 1197, 1088,
2a19b 20 31 31 39 37 2c 20 31 30 38 38 2c 20 31 30 38   1197, 1088, 108
2a19c 38 2c 20 31 32 30 32 2c 0a 20 2f 2a 20 20 20 32  8, 1202,. /*   2
2a19d 37 30 20 2a 2f 20 20 31 31 31 30 2c 20 31 31 39  70 */  1110, 119
2a19e 37 2c 20 31 30 38 38 2c 20 31 31 38 34 2c 20 31  7, 1088, 1184, 1
2a19f 31 38 34 2c 20 31 32 32 37 2c 20 31 30 36 38 2c  184, 1227, 1068,
2a1a0 20 31 30 38 38 2c 20 31 32 33 36 2c 20 31 32 33   1088, 1236, 123
2a1a1 36 2c 0a 20 2f 2a 20 20 20 32 38 30 20 2a 2f 20  6,. /*   280 */ 
2a1a2 20 31 32 33 36 2c 20 31 32 33 36 2c 20 31 30 36   1236, 1236, 106
2a1a3 38 2c 20 31 31 38 34 2c 20 31 32 32 37 2c 20 31  8, 1184, 1227, 1
2a1a4 30 38 38 2c 20 31 32 32 36 2c 20 31 32 32 36 2c  088, 1226, 1226,
2a1a5 20 31 30 38 38 2c 20 31 30 38 38 2c 0a 20 2f 2a   1088, 1088,. /*
2a1a6 20 20 20 32 39 30 20 2a 2f 20 20 31 31 32 32 2c     290 */  1122,
2a1a7 20 20 2d 39 39 2c 20 20 2d 39 39 2c 20 20 2d 39    -99,  -99,  -9
2a1a8 39 2c 20 20 2d 39 39 2c 20 20 2d 39 39 2c 20 20  9,  -99,  -99,  
2a1a9 34 35 39 2c 20 20 36 34 36 2c 20 20 35 39 31 2c  459,  646,  591,
2a1aa 20 20 36 38 35 2c 0a 20 2f 2a 20 20 20 33 30 30    685,. /*   300
2a1ab 20 2a 2f 20 20 20 38 39 31 2c 20 20 33 32 35 2c   */   891,  325,
2a1ac 20 20 39 38 37 2c 20 31 30 35 38 2c 20 20 33 32    987, 1058,  32
2a1ad 32 2c 20 31 31 30 33 2c 20 31 30 33 38 2c 20 31  2, 1103, 1038, 1
2a1ae 30 36 31 2c 20 31 30 39 34 2c 20 31 31 30 32 2c  061, 1094, 1102,
2a1af 0a 20 2f 2a 20 20 20 33 31 30 20 2a 2f 20 20 31  . /*   310 */  1
2a1b0 31 31 31 2c 20 31 31 31 32 2c 20 31 31 31 39 2c  111, 1112, 1119,
2a1b1 20 31 31 32 30 2c 20 20 31 35 30 2c 20 31 31 32   1120,  150, 112
2a1b2 35 2c 20 20 39 35 34 2c 20 31 32 36 32 2c 20 31  5,  954, 1262, 1
2a1b3 32 34 35 2c 20 31 31 35 39 2c 0a 20 2f 2a 20 20  245, 1159,. /*  
2a1b4 20 33 32 30 20 2a 2f 20 20 31 31 36 30 2c 20 31   320 */  1160, 1
2a1b5 31 36 31 2c 20 31 31 36 32 2c 20 31 31 38 33 2c  161, 1162, 1183,
2a1b6 20 31 31 36 39 2c 20 31 32 35 38 2c 20 31 32 35   1169, 1258, 125
2a1b7 39 2c 20 31 32 36 30 2c 20 31 32 36 33 2c 20 31  9, 1260, 1263, 1
2a1b8 32 36 37 2c 0a 20 2f 2a 20 20 20 33 33 30 20 2a  267,. /*   330 *
2a1b9 2f 20 20 31 32 36 34 2c 20 31 32 36 35 2c 20 31  /  1264, 1265, 1
2a1ba 32 36 36 2c 20 31 32 36 39 2c 20 31 32 37 31 2c  266, 1269, 1271,
2a1bb 20 31 32 37 30 2c 20 31 31 36 38 2c 20 31 32 37   1270, 1168, 127
2a1bc 32 2c 20 31 31 37 30 2c 20 31 32 37 30 2c 0a 20  2, 1170, 1270,. 
2a1bd 2f 2a 20 20 20 33 34 30 20 2a 2f 20 20 31 31 37  /*   340 */  117
2a1be 33 2c 20 31 32 37 34 2c 20 31 31 37 39 2c 20 31  3, 1274, 1179, 1
2a1bf 31 38 31 2c 20 31 32 37 38 2c 20 31 31 38 37 2c  181, 1278, 1187,
2a1c0 20 31 32 38 30 2c 20 31 32 38 31 2c 20 31 32 37   1280, 1281, 127
2a1c1 33 2c 20 31 32 36 38 2c 0a 20 2f 2a 20 20 20 33  3, 1268,. /*   3
2a1c2 35 30 20 2a 2f 20 20 31 32 38 33 2c 20 31 32 37  50 */  1283, 127
2a1c3 36 2c 20 31 32 38 34 2c 20 31 32 38 36 2c 20 31  6, 1284, 1286, 1
2a1c4 32 38 35 2c 20 31 32 38 38 2c 20 31 32 37 37 2c  285, 1288, 1277,
2a1c5 20 31 32 38 39 2c 20 31 32 31 33 2c 20 31 32 30   1289, 1213, 120
2a1c6 37 2c 0a 20 2f 2a 20 20 20 33 36 30 20 2a 2f 20  7,. /*   360 */ 
2a1c7 20 31 32 39 35 2c 20 31 32 39 37 2c 20 31 32 39   1295, 1297, 129
2a1c8 34 2c 20 31 32 31 38 2c 20 31 32 38 32 2c 20 31  4, 1218, 1282, 1
2a1c9 32 37 39 2c 20 31 32 38 37 2c 20 31 33 30 30 2c  279, 1287, 1300,
2a1ca 20 31 32 39 30 2c 20 31 31 38 39 2c 0a 20 2f 2a   1290, 1189,. /*
2a1cb 20 20 20 33 37 30 20 2a 2f 20 20 31 32 31 39 2c     370 */  1219,
2a1cc 20 31 33 30 35 2c 20 31 33 32 30 2c 20 31 33 32   1305, 1320, 132
2a1cd 36 2c 20 31 32 32 39 2c 20 31 32 36 31 2c 20 31  6, 1229, 1261, 1
2a1ce 32 37 35 2c 20 31 32 30 35 2c 20 31 33 31 34 2c  275, 1205, 1314,
2a1cf 20 31 32 32 31 2c 0a 20 2f 2a 20 20 20 33 38 30   1221,. /*   380
2a1d0 20 2a 2f 20 20 31 33 33 35 2c 20 31 33 32 33 2c   */  1335, 1323,
2a1d1 20 31 32 32 32 2c 20 31 33 32 31 2c 20 31 32 32   1222, 1321, 122
2a1d2 38 2c 20 31 32 33 38 2c 20 31 32 32 33 2c 20 31  8, 1238, 1223, 1
2a1d3 33 32 35 2c 20 31 32 32 34 2c 20 31 33 32 32 2c  325, 1224, 1322,
2a1d4 0a 20 2f 2a 20 20 20 33 39 30 20 2a 2f 20 20 31  . /*   390 */  1
2a1d5 33 33 31 2c 20 31 32 39 32 2c 20 31 32 31 31 2c  331, 1292, 1211,
2a1d6 20 31 32 31 34 2c 20 31 33 33 33 2c 20 31 33 34   1214, 1333, 134
2a1d7 37 2c 20 31 33 34 39 2c 0a 7d 3b 0a 23 64 65 66  7, 1349,.};.#def
2a1d8 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f 55 53  ine YY_REDUCE_US
2a1d9 45 5f 44 46 4c 54 20 28 2d 31 34 34 29 0a 23 64  E_DFLT (-144).#d
2a1da 65 66 69 6e 65 20 59 59 5f 52 45 44 55 43 45 5f  efine YY_REDUCE_
2a1db 4d 41 58 20 32 39 35 0a 73 74 61 74 69 63 20 63  MAX 295.static c
2a1dc 6f 6e 73 74 20 73 68 6f 72 74 20 79 79 5f 72 65  onst short yy_re
2a1dd 64 75 63 65 5f 6f 66 73 74 5b 5d 20 3d 20 7b 0a  duce_ofst[] = {.
2a1de 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20 2d 31   /*     0 */  -1
2a1df 33 39 2c 20 20 32 37 38 2c 20 20 32 39 35 2c 20  39,  278,  295, 
2a1e0 20 32 39 32 2c 20 20 34 30 32 2c 20 20 2d 32 32   292,  402,  -22
2a1e1 2c 20 20 34 30 38 2c 20 20 20 33 35 2c 20 20 20  ,  408,   35,   
2a1e2 33 37 2c 20 20 35 34 36 2c 0a 20 2f 2a 20 20 20  37,  546,. /*   
2a1e3 20 31 30 20 2a 2f 20 20 20 20 2d 33 2c 20 2d 31   10 */    -3, -1
2a1e4 32 38 2c 20 20 31 33 33 2c 20 20 32 38 32 2c 20  28,  133,  282, 
2a1e5 20 34 31 31 2c 20 20 34 31 37 2c 20 20 34 32 30   411,  417,  420
2a1e6 2c 20 2d 31 34 33 2c 20 20 35 30 33 2c 20 20 32  , -143,  503,  2
2a1e7 31 33 2c 0a 20 2f 2a 20 20 20 20 32 30 20 2a 2f  13,. /*    20 */
2a1e8 20 20 20 31 35 31 2c 20 20 33 35 33 2c 20 20 33     151,  353,  3
2a1e9 35 34 2c 20 20 34 36 30 2c 20 20 32 32 34 2c 20  54,  460,  224, 
2a1ea 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
2a1eb 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 0a 20 2f  ,  224,  224,. /
2a1ec 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 32 32 34  *    30 */   224
2a1ed 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
2a1ee 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20  24,  224,  224, 
2a1ef 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
2a1f0 2c 20 20 32 32 34 2c 0a 20 2f 2a 20 20 20 20 34  ,  224,. /*    4
2a1f1 30 20 2a 2f 20 20 20 32 32 34 2c 20 20 32 32 34  0 */   224,  224
2a1f2 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
2a1f3 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20  24,  224,  224, 
2a1f4 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
2a1f5 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f 20 20  ,. /*    50 */  
2a1f6 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32 32 34   224,  224,  224
2a1f7 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
2a1f8 32 34 2c 20 20 34 38 33 2c 20 20 36 35 36 2c 20  24,  483,  656, 
2a1f9 20 36 39 39 2c 20 20 37 32 33 2c 0a 20 2f 2a 20   699,  723,. /* 
2a1fa 20 20 20 36 30 20 2a 2f 20 20 20 37 32 35 2c 20     60 */   725, 
2a1fb 20 37 33 32 2c 20 20 37 34 34 2c 20 20 37 34 39   732,  744,  749
2a1fc 2c 20 20 37 35 36 2c 20 20 37 35 38 2c 20 20 37  ,  756,  758,  7
2a1fd 36 33 2c 20 20 37 37 30 2c 20 20 37 39 36 2c 20  63,  770,  796, 
2a1fe 20 38 30 37 2c 0a 20 2f 2a 20 20 20 20 37 30 20   807,. /*    70 
2a1ff 2a 2f 20 20 20 38 30 39 2c 20 20 38 31 32 2c 20  */   809,  812, 
2a200 20 38 34 37 2c 20 20 38 35 37 2c 20 20 38 36 30   847,  857,  860
2a201 2c 20 20 38 37 36 2c 20 20 38 37 38 2c 20 20 38  ,  876,  878,  8
2a202 38 31 2c 20 20 38 38 34 2c 20 20 38 38 36 2c 0a  81,  884,  886,.
2a203 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20 20 38   /*    80 */   8
2a204 39 34 2c 20 20 38 39 37 2c 20 20 39 30 32 2c 20  94,  897,  902, 
2a205 20 39 30 35 2c 20 20 39 32 31 2c 20 20 39 32 39   905,  921,  929
2a206 2c 20 20 39 33 31 2c 20 20 39 33 34 2c 20 20 39  ,  931,  934,  9
2a207 33 36 2c 20 20 39 33 38 2c 0a 20 2f 2a 20 20 20  36,  938,. /*   
2a208 20 39 30 20 2a 2f 20 20 20 39 34 32 2c 20 20 39   90 */   942,  9
2a209 34 37 2c 20 20 39 34 39 2c 20 20 39 35 35 2c 20  47,  949,  955, 
2a20a 20 39 36 37 2c 20 20 39 37 33 2c 20 20 39 37 39   967,  973,  979
2a20b 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
2a20c 32 34 2c 0a 20 2f 2a 20 20 20 31 30 30 20 2a 2f  24,. /*   100 */
2a20d 20 20 20 32 32 34 2c 20 20 31 36 38 2c 20 20 32     224,  168,  2
2a20e 32 34 2c 20 20 32 32 34 2c 20 20 20 33 36 2c 20  24,  224,   36, 
2a20f 20 20 33 33 2c 20 20 32 31 30 2c 20 20 34 38 34    33,  210,  484
2a210 2c 20 20 32 32 34 2c 20 2d 31 32 31 2c 0a 20 2f  ,  224, -121,. /
2a211 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 32 32 34  *   110 */   224
2a212 2c 20 20 32 32 34 2c 20 20 32 32 34 2c 20 20 32  ,  224,  224,  2
2a213 32 34 2c 20 20 20 34 35 2c 20 20 20 32 31 2c 20  24,   45,   21, 
2a214 20 20 20 38 2c 20 20 31 30 39 2c 20 20 34 38 37     8,  109,  487
2a215 2c 20 20 34 38 37 2c 0a 20 2f 2a 20 20 20 31 32  ,  487,. /*   12
2a216 30 20 2a 2f 20 20 20 31 36 34 2c 20 20 20 39 39  0 */   164,   99
2a217 2c 20 20 32 32 32 2c 20 20 35 34 31 2c 20 20 2d  ,  222,  541,  -
2a218 39 31 2c 20 20 20 2d 31 2c 20 20 34 37 34 2c 20  91,   -1,  474, 
2a219 20 35 39 38 2c 20 20 35 38 37 2c 20 20 36 37 37   598,  587,  677
2a21a 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f 20 20  ,. /*   130 */  
2a21b 20 36 33 38 2c 20 20 34 32 39 2c 20 20 37 31 33   638,  429,  713
2a21c 2c 20 20 36 33 39 2c 20 20 36 34 31 2c 20 20 36  ,  639,  641,  6
2a21d 37 34 2c 20 20 36 37 36 2c 20 20 37 31 36 2c 20  74,  676,  716, 
2a21e 20 37 31 39 2c 20 20 36 38 36 2c 0a 20 2f 2a 20   719,  686,. /* 
2a21f 20 20 31 34 30 20 2a 2f 20 20 20 37 37 36 2c 20    140 */   776, 
2a220 20 32 35 37 2c 20 20 33 36 32 2c 20 20 38 30 32   257,  362,  802
2a221 2c 20 20 38 30 36 2c 20 20 38 32 30 2c 20 20 35  ,  806,  820,  5
2a222 34 35 2c 20 20 35 38 32 2c 20 20 36 36 39 2c 20  45,  582,  669, 
2a223 20 2d 36 30 2c 0a 20 2f 2a 20 20 20 31 35 30 20   -60,. /*   150 
2a224 2a 2f 20 20 20 2d 31 35 2c 20 20 31 32 38 2c 20  */   -15,  128, 
2a225 20 31 37 38 2c 20 20 33 31 37 2c 20 20 20 34 30   178,  317,   40
2a226 2c 20 20 33 31 37 2c 20 20 33 31 37 2c 20 20 33  ,  317,  317,  3
2a227 37 37 2c 20 20 34 34 31 2c 20 20 34 38 31 2c 0a  77,  441,  481,.
2a228 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20 20 34   /*   160 */   4
2a229 39 39 2c 20 20 35 30 32 2c 20 20 35 31 30 2c 20  99,  502,  510, 
2a22a 20 35 35 33 2c 20 20 35 38 36 2c 20 20 35 39 36   553,  586,  596
2a22b 2c 20 20 35 30 32 2c 20 20 36 38 34 2c 20 20 37  ,  502,  684,  7
2a22c 31 37 2c 20 20 37 36 38 2c 0a 20 2f 2a 20 20 20  17,  768,. /*   
2a22d 31 37 30 20 2a 2f 20 20 20 37 38 38 2c 20 20 37  170 */   788,  7
2a22e 38 36 2c 20 20 38 34 36 2c 20 20 38 35 34 2c 20  86,  846,  854, 
2a22f 20 38 35 38 2c 20 20 33 31 37 2c 20 20 37 38 36   858,  317,  786
2a230 2c 20 20 38 37 31 2c 20 20 38 38 38 2c 20 20 38  ,  871,  888,  8
2a231 36 34 2c 0a 20 2f 2a 20 20 20 31 38 30 20 2a 2f  64,. /*   180 */
2a232 20 20 20 38 38 37 2c 20 20 39 30 36 2c 20 20 39     887,  906,  9
2a233 32 36 2c 20 20 39 34 36 2c 20 20 39 38 30 2c 20  26,  946,  980, 
2a234 20 33 31 37 2c 20 20 39 39 38 2c 20 31 30 30 30   317,  998, 1000
2a235 2c 20 20 33 31 37 2c 20 31 30 30 33 2c 0a 20 2f  ,  317, 1003,. /
2a236 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 39 30 33  *   190 */   903
2a237 2c 20 20 38 30 35 2c 20 31 30 30 34 2c 20 31 30  ,  805, 1004, 10
2a238 30 35 2c 20 20 33 31 37 2c 20 31 30 30 37 2c 20  05,  317, 1007, 
2a239 31 30 30 38 2c 20 31 30 30 39 2c 20 20 33 31 37  1008, 1009,  317
2a23a 2c 20 31 30 31 30 2c 0a 20 2f 2a 20 20 20 32 30  , 1010,. /*   20
2a23b 30 20 2a 2f 20 20 31 30 31 31 2c 20 31 30 31 32  0 */  1011, 1012
2a23c 2c 20 20 33 31 37 2c 20 20 33 31 37 2c 20 31 30  ,  317,  317, 10
2a23d 31 33 2c 20 31 30 31 34 2c 20 31 30 31 35 2c 20  13, 1014, 1015, 
2a23e 31 30 31 36 2c 20 31 30 31 37 2c 20 20 39 38 35  1016, 1017,  985
2a23f 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f 20 20  ,. /*   210 */  
2a240 20 39 36 32 2c 20 20 39 37 34 2c 20 31 30 31 38   962,  974, 1018
2a241 2c 20 20 39 33 37 2c 20 20 39 33 39 2c 20 20 39  ,  937,  939,  9
2a242 34 35 2c 20 20 39 39 33 2c 20 20 39 39 39 2c 20  45,  993,  999, 
2a243 20 39 36 36 2c 20 31 30 30 31 2c 0a 20 2f 2a 20   966, 1001,. /* 
2a244 20 20 32 32 30 20 2a 2f 20 20 20 39 39 35 2c 20    220 */   995, 
2a245 31 30 30 32 2c 20 31 30 31 39 2c 20 31 30 32 30  1002, 1019, 1020
2a246 2c 20 31 30 32 31 2c 20 31 30 32 32 2c 20 20 39  , 1021, 1022,  9
2a247 38 31 2c 20 20 39 37 37 2c 20 20 39 38 33 2c 20  81,  977,  983, 
2a248 20 39 38 34 2c 0a 20 2f 2a 20 20 20 32 33 30 20   984,. /*   230 
2a249 2a 2f 20 20 31 30 33 31 2c 20 31 30 33 37 2c 20  */  1031, 1037, 
2a24a 31 30 33 39 2c 20 31 30 34 30 2c 20 31 30 33 34  1039, 1040, 1034
2a24b 2c 20 31 30 32 33 2c 20 20 39 39 36 2c 20 31 30  , 1023,  996, 10
2a24c 32 34 2c 20 31 30 32 35 2c 20 31 30 32 36 2c 0a  24, 1025, 1026,.
2a24d 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20 31 30   /*   240 */  10
2a24e 32 37 2c 20 20 39 37 35 2c 20 31 30 34 38 2c 20  27,  975, 1048, 
2a24f 31 30 34 39 2c 20 31 30 32 38 2c 20 31 30 32 39  1049, 1028, 1029
2a250 2c 20 31 30 33 32 2c 20 31 30 33 30 2c 20 31 30  , 1032, 1030, 10
2a251 33 35 2c 20 31 30 33 36 2c 0a 20 2f 2a 20 20 20  35, 1036,. /*   
2a252 32 35 30 20 2a 2f 20 20 31 30 34 31 2c 20 31 30  250 */  1041, 10
2a253 34 32 2c 20 31 30 34 33 2c 20 31 30 34 34 2c 20  42, 1043, 1044, 
2a254 31 30 35 30 2c 20 20 39 39 31 2c 20 20 39 39 32  1050,  991,  992
2a255 2c 20 31 30 34 37 2c 20 31 30 35 31 2c 20 31 30  , 1047, 1051, 10
2a256 35 36 2c 0a 20 2f 2a 20 20 20 32 36 30 20 2a 2f  56,. /*   260 */
2a257 20 20 31 30 35 37 2c 20 31 30 35 35 2c 20 31 30    1057, 1055, 10
2a258 36 33 2c 20 31 30 37 38 2c 20 31 30 38 30 2c 20  63, 1078, 1080, 
2a259 31 30 38 32 2c 20 31 30 38 31 2c 20 31 30 38 36  1082, 1081, 1086
2a25a 2c 20 31 30 38 39 2c 20 31 30 33 33 2c 0a 20 2f  , 1089, 1033,. /
2a25b 2a 20 20 20 32 37 30 20 2a 2f 20 20 31 30 34 35  *   270 */  1045
2a25c 2c 20 31 30 39 31 2c 20 31 30 39 32 2c 20 31 30  , 1091, 1092, 10
2a25d 36 32 2c 20 31 30 36 34 2c 20 31 30 34 36 2c 20  62, 1064, 1046, 
2a25e 31 30 35 33 2c 20 31 30 39 37 2c 20 31 30 36 35  1053, 1097, 1065
2a25f 2c 20 31 30 36 36 2c 0a 20 2f 2a 20 20 20 32 38  , 1066,. /*   28
2a260 30 20 2a 2f 20 20 31 30 36 39 2c 20 31 30 37 30  0 */  1069, 1070
2a261 2c 20 31 30 36 30 2c 20 31 30 37 31 2c 20 31 30  , 1060, 1071, 10
2a262 35 34 2c 20 31 30 39 39 2c 20 31 30 35 32 2c 20  54, 1099, 1052, 
2a263 31 30 35 39 2c 20 31 31 30 38 2c 20 31 31 31 34  1059, 1108, 1114
2a264 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f 20 20  ,. /*   290 */  
2a265 31 30 37 32 2c 20 31 31 30 34 2c 20 31 30 39 30  1072, 1104, 1090
2a266 2c 20 31 30 39 33 2c 20 31 30 37 35 2c 20 31 30  , 1093, 1075, 10
2a267 37 39 2c 0a 7d 3b 0a 73 74 61 74 69 63 20 63 6f  79,.};.static co
2a268 6e 73 74 20 59 59 41 43 54 49 4f 4e 54 59 50 45  nst YYACTIONTYPE
2a269 20 79 79 5f 64 65 66 61 75 6c 74 5b 5d 20 3d 20   yy_default[] = 
2a26a 7b 0a 20 2f 2a 20 20 20 20 20 30 20 2a 2f 20 20  {. /*     0 */  
2a26b 20 36 30 33 2c 20 20 38 33 32 2c 20 20 39 31 33   603,  832,  913
2a26c 2c 20 20 37 31 39 2c 20 20 39 31 33 2c 20 20 38  ,  719,  913,  8
2a26d 33 32 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  32,  913,  913, 
2a26e 20 38 35 39 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   859,  913,. /* 
2a26f 20 20 20 31 30 20 2a 2f 20 20 20 37 32 33 2c 20     10 */   723, 
2a270 20 38 38 38 2c 20 20 38 33 30 2c 20 20 39 31 33   888,  830,  913
2a271 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a272 31 33 2c 20 20 38 30 34 2c 20 20 39 31 33 2c 20  13,  804,  913, 
2a273 20 38 35 39 2c 0a 20 2f 2a 20 20 20 20 32 30 20   859,. /*    20 
2a274 2a 2f 20 20 20 39 31 33 2c 20 20 36 33 35 2c 20  */   913,  635, 
2a275 20 38 35 39 2c 20 20 38 35 39 2c 20 20 37 35 35   859,  859,  755
2a276 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a277 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a  13,  913,  913,.
2a278 20 2f 2a 20 20 20 20 33 30 20 2a 2f 20 20 20 39   /*    30 */   9
2a279 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a27a 20 37 35 36 2c 20 20 39 31 33 2c 20 20 38 33 34   756,  913,  834
2a27b 2c 20 20 38 32 39 2c 20 20 38 32 35 2c 20 20 38  ,  829,  825,  8
2a27c 32 37 2c 20 20 38 32 36 2c 0a 20 2f 2a 20 20 20  27,  826,. /*   
2a27d 20 34 30 20 2a 2f 20 20 20 38 33 33 2c 20 20 37   40 */   833,  7
2a27e 35 37 2c 20 20 37 34 36 2c 20 20 37 35 33 2c 20  57,  746,  753, 
2a27f 20 37 36 30 2c 20 20 37 33 35 2c 20 20 38 37 32   760,  735,  872
2a280 2c 20 20 37 36 32 2c 20 20 37 36 33 2c 20 20 37  ,  762,  763,  7
2a281 36 39 2c 0a 20 2f 2a 20 20 20 20 35 30 20 2a 2f  69,. /*    50 */
2a282 20 20 20 37 37 30 2c 20 20 38 38 39 2c 20 20 38     770,  889,  8
2a283 38 37 2c 20 20 37 39 32 2c 20 20 37 39 31 2c 20  87,  792,  791, 
2a284 20 38 31 30 2c 20 20 39 31 33 2c 20 20 39 31 33   810,  913,  913
2a285 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f  ,  913,  913,. /
2a286 2a 20 20 20 20 36 30 20 2a 2f 20 20 20 39 31 33  *    60 */   913
2a287 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a288 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a289 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a28a 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 20 37  ,  913,. /*    7
2a28b 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33  0 */   913,  913
2a28c 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a28d 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a28e 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a28f 2c 0a 20 2f 2a 20 20 20 20 38 30 20 2a 2f 20 20  ,. /*    80 */  
2a290 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a291 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a292 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a293 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   913,  913,. /* 
2a294 20 20 20 39 30 20 2a 2f 20 20 20 39 31 33 2c 20     90 */   913, 
2a295 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a296 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a297 31 33 2c 20 20 37 39 34 2c 20 20 38 31 36 2c 20  13,  794,  816, 
2a298 20 37 39 33 2c 0a 20 2f 2a 20 20 20 31 30 30 20   793,. /*   100 
2a299 2a 2f 20 20 20 38 30 33 2c 20 20 36 32 38 2c 20  */   803,  628, 
2a29a 20 37 39 35 2c 20 20 37 39 36 2c 20 20 36 38 38   795,  796,  688
2a29b 2c 20 20 36 32 33 2c 20 20 39 31 33 2c 20 20 39  ,  623,  913,  9
2a29c 31 33 2c 20 20 37 39 37 2c 20 20 39 31 33 2c 0a  13,  797,  913,.
2a29d 20 2f 2a 20 20 20 31 31 30 20 2a 2f 20 20 20 37   /*   110 */   7
2a29e 39 38 2c 20 20 38 31 31 2c 20 20 38 31 32 2c 20  98,  811,  812, 
2a29f 20 38 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   813,  913,  913
2a2a0 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a2a1 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20  13,  913,. /*   
2a2a2 31 32 30 20 2a 2f 20 20 20 36 30 33 2c 20 20 37  120 */   603,  7
2a2a3 31 39 2c 20 20 39 31 33 2c 20 20 37 31 39 2c 20  19,  913,  719, 
2a2a4 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a2a5 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a2a6 31 33 2c 0a 20 2f 2a 20 20 20 31 33 30 20 2a 2f  13,. /*   130 */
2a2a7 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39     913,  913,  9
2a2a8 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a2a9 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a2aa 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f  ,  913,  913,. /
2a2ab 2a 20 20 20 31 34 30 20 2a 2f 20 20 20 39 31 33  *   140 */   913
2a2ac 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a2ad 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a2ae 20 37 31 33 2c 20 20 37 32 33 2c 20 20 39 30 36   713,  723,  906
2a2af 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 31 35  ,  913,. /*   15
2a2b0 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 36 37 39  0 */   913,  679
2a2b1 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a2b2 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a2b3 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a2b4 2c 0a 20 2f 2a 20 20 20 31 36 30 20 2a 2f 20 20  ,. /*   160 */  
2a2b5 20 39 31 33 2c 20 20 39 31 33 2c 20 20 36 31 31   913,  913,  611
2a2b6 2c 20 20 36 30 39 2c 20 20 39 31 33 2c 20 20 37  ,  609,  913,  7
2a2b7 31 31 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  11,  913,  913, 
2a2b8 20 36 33 37 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   637,  913,. /* 
2a2b9 20 20 31 37 30 20 2a 2f 20 20 20 39 31 33 2c 20    170 */   913, 
2a2ba 20 37 32 31 2c 20 20 39 31 33 2c 20 20 39 31 33   721,  913,  913
2a2bb 2c 20 20 39 31 33 2c 20 20 37 32 36 2c 20 20 37  ,  913,  726,  7
2a2bc 32 37 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  27,  913,  913, 
2a2bd 20 39 31 33 2c 0a 20 2f 2a 20 20 20 31 38 30 20   913,. /*   180 
2a2be 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20  */   913,  913, 
2a2bf 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a2c0 2c 20 20 36 32 35 2c 20 20 39 31 33 2c 20 20 39  ,  625,  913,  9
2a2c1 31 33 2c 20 20 37 30 30 2c 20 20 39 31 33 2c 0a  13,  700,  913,.
2a2c2 20 2f 2a 20 20 20 31 39 30 20 2a 2f 20 20 20 38   /*   190 */   8
2a2c3 36 35 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  65,  913,  913, 
2a2c4 20 39 31 33 2c 20 20 38 37 39 2c 20 20 39 31 33   913,  879,  913
2a2c5 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 38  ,  913,  913,  8
2a2c6 37 37 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20  77,  913,. /*   
2a2c7 32 30 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39  200 */   913,  9
2a2c8 31 33 2c 20 20 37 30 32 2c 20 20 37 36 35 2c 20  13,  702,  765, 
2a2c9 20 38 34 35 2c 20 20 39 31 33 2c 20 20 38 39 32   845,  913,  892
2a2ca 2c 20 20 38 39 34 2c 20 20 39 31 33 2c 20 20 39  ,  894,  913,  9
2a2cb 31 33 2c 0a 20 2f 2a 20 20 20 32 31 30 20 2a 2f  13,. /*   210 */
2a2cc 20 20 20 37 31 31 2c 20 20 37 32 30 2c 20 20 39     711,  720,  9
2a2cd 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a2ce 20 38 32 38 2c 20 20 37 34 39 2c 20 20 37 34 39   828,  749,  749
2a2cf 2c 20 20 37 33 37 2c 20 20 37 34 39 2c 0a 20 2f  ,  737,  749,. /
2a2d0 2a 20 20 20 32 32 30 20 2a 2f 20 20 20 36 35 38  *   220 */   658
2a2d1 2c 20 20 37 34 39 2c 20 20 39 31 33 2c 20 20 37  ,  749,  913,  7
2a2d2 34 39 2c 20 20 39 31 33 2c 20 20 36 36 31 2c 20  49,  913,  661, 
2a2d3 20 37 35 39 2c 20 20 37 33 37 2c 20 20 37 33 37   759,  737,  737
2a2d4 2c 20 20 37 35 39 2c 0a 20 2f 2a 20 20 20 32 33  ,  759,. /*   23
2a2d5 30 20 2a 2f 20 20 20 36 30 38 2c 20 20 36 30 38  0 */   608,  608
2a2d6 2c 20 20 36 30 38 2c 20 20 36 30 38 2c 20 20 36  ,  608,  608,  6
2a2d7 37 38 2c 20 20 39 31 33 2c 20 20 37 35 39 2c 20  78,  913,  759, 
2a2d8 20 37 35 30 2c 20 20 37 35 32 2c 20 20 37 34 32   750,  752,  742
2a2d9 2c 0a 20 2f 2a 20 20 20 32 34 30 20 2a 2f 20 20  ,. /*   240 */  
2a2da 20 37 35 34 2c 20 20 39 31 33 2c 20 20 37 32 38   754,  913,  728
2a2db 2c 20 20 37 32 38 2c 20 20 37 33 36 2c 20 20 37  ,  728,  736,  7
2a2dc 34 31 2c 20 20 37 33 36 2c 20 20 37 34 31 2c 20  41,  736,  741, 
2a2dd 20 36 39 30 2c 20 20 36 39 30 2c 0a 20 2f 2a 20   690,  690,. /* 
2a2de 20 20 32 35 30 20 2a 2f 20 20 20 36 37 35 2c 20    250 */   675, 
2a2df 20 36 39 30 2c 20 20 36 36 31 2c 20 20 36 39 30   690,  661,  690
2a2e0 2c 20 20 38 33 38 2c 20 20 38 34 32 2c 20 20 38  ,  838,  842,  8
2a2e1 34 32 2c 20 20 36 37 35 2c 20 20 36 39 30 2c 20  42,  675,  690, 
2a2e2 20 36 39 30 2c 0a 20 2f 2a 20 20 20 32 36 30 20   690,. /*   260 
2a2e3 2a 2f 20 20 20 36 39 30 2c 20 20 38 33 38 2c 20  */   690,  838, 
2a2e4 20 36 32 30 2c 20 20 37 32 38 2c 20 20 36 32 30   620,  728,  620
2a2e5 2c 20 20 37 32 38 2c 20 20 36 32 30 2c 20 20 37  ,  728,  620,  7
2a2e6 32 38 2c 20 20 37 32 38 2c 20 20 38 36 39 2c 0a  28,  728,  869,.
2a2e7 20 2f 2a 20 20 20 32 37 30 20 2a 2f 20 20 20 38   /*   270 */   8
2a2e8 37 31 2c 20 20 36 32 30 2c 20 20 37 32 38 2c 20  71,  620,  728, 
2a2e9 20 36 39 32 2c 20 20 36 39 32 2c 20 20 37 37 31   692,  692,  771
2a2ea 2c 20 20 37 35 39 2c 20 20 37 32 38 2c 20 20 36  ,  759,  728,  6
2a2eb 39 39 2c 20 20 36 39 39 2c 0a 20 2f 2a 20 20 20  99,  699,. /*   
2a2ec 32 38 30 20 2a 2f 20 20 20 36 39 39 2c 20 20 36  280 */   699,  6
2a2ed 39 39 2c 20 20 37 35 39 2c 20 20 36 39 32 2c 20  99,  759,  692, 
2a2ee 20 37 37 31 2c 20 20 37 32 38 2c 20 20 38 39 31   771,  728,  891
2a2ef 2c 20 20 38 39 31 2c 20 20 37 32 38 2c 20 20 37  ,  891,  728,  7
2a2f0 32 38 2c 0a 20 2f 2a 20 20 20 32 39 30 20 2a 2f  28,. /*   290 */
2a2f1 20 20 20 38 39 39 2c 20 20 36 34 35 2c 20 20 36     899,  645,  6
2a2f2 36 33 2c 20 20 36 36 33 2c 20 20 39 30 36 2c 20  63,  663,  906, 
2a2f3 20 39 31 31 2c 20 20 39 31 33 2c 20 20 39 31 33   911,  913,  913
2a2f4 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f  ,  913,  913,. /
2a2f5 2a 20 20 20 33 30 30 20 2a 2f 20 20 20 37 37 38  *   300 */   778
2a2f6 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a2f7 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a2f8 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a2f9 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 33 31  ,  913,. /*   31
2a2fa 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33  0 */   913,  913
2a2fb 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 38  ,  913,  913,  8
2a2fc 35 32 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  52,  913,  913, 
2a2fd 20 39 31 33 2c 20 20 39 31 33 2c 20 20 37 38 33   913,  913,  783
2a2fe 2c 0a 20 2f 2a 20 20 20 33 32 30 20 2a 2f 20 20  ,. /*   320 */  
2a2ff 20 37 37 39 2c 20 20 39 31 33 2c 20 20 37 38 30   779,  913,  780
2a300 2c 20 20 39 31 33 2c 20 20 37 30 35 2c 20 20 39  ,  913,  705,  9
2a301 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a302 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20   913,  913,. /* 
2a303 20 20 33 33 30 20 2a 2f 20 20 20 39 31 33 2c 20    330 */   913, 
2a304 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a305 2c 20 20 39 31 33 2c 20 20 38 33 31 2c 20 20 39  ,  913,  831,  9
2a306 31 33 2c 20 20 37 34 33 2c 20 20 39 31 33 2c 20  13,  743,  913, 
2a307 20 37 35 31 2c 0a 20 2f 2a 20 20 20 33 34 30 20   751,. /*   340 
2a308 2a 2f 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20  */   913,  913, 
2a309 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a30a 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a30b 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a  13,  913,  913,.
2a30c 20 2f 2a 20 20 20 33 35 30 20 2a 2f 20 20 20 39   /*   350 */   9
2a30d 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a30e 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a30f 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a310 31 33 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20  13,  913,. /*   
2a311 33 36 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 39  360 */   913,  9
2a312 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a313 20 39 31 33 2c 20 20 39 31 33 2c 20 20 38 36 37   913,  913,  867
2a314 2c 20 20 38 36 38 2c 20 20 39 31 33 2c 20 20 39  ,  868,  913,  9
2a315 31 33 2c 0a 20 2f 2a 20 20 20 33 37 30 20 2a 2f  13,. /*   370 */
2a316 20 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39     913,  913,  9
2a317 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a318 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a319 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 0a 20 2f  ,  913,  913,. /
2a31a 2a 20 20 20 33 38 30 20 2a 2f 20 20 20 39 31 33  *   380 */   913
2a31b 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a31c 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20  13,  913,  913, 
2a31d 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39 31 33   913,  913,  913
2a31e 2c 20 20 39 31 33 2c 0a 20 2f 2a 20 20 20 33 39  ,  913,. /*   39
2a31f 30 20 2a 2f 20 20 20 39 31 33 2c 20 20 38 39 38  0 */   913,  898
2a320 2c 20 20 39 31 33 2c 20 20 39 31 33 2c 20 20 39  ,  913,  913,  9
2a321 30 31 2c 20 20 36 30 34 2c 20 20 39 31 33 2c 20  01,  604,  913, 
2a322 20 35 39 39 2c 20 20 36 30 31 2c 20 20 36 30 32   599,  601,  602
2a323 2c 0a 20 2f 2a 20 20 20 34 30 30 20 2a 2f 20 20  ,. /*   400 */  
2a324 20 36 30 36 2c 20 20 36 30 37 2c 20 20 36 31 30   606,  607,  610
2a325 2c 20 20 36 33 32 2c 20 20 36 33 33 2c 20 20 36  ,  632,  633,  6
2a326 33 34 2c 20 20 36 31 32 2c 20 20 36 31 33 2c 20  34,  612,  613, 
2a327 20 36 31 34 2c 20 20 36 31 35 2c 0a 20 2f 2a 20   614,  615,. /* 
2a328 20 20 34 31 30 20 2a 2f 20 20 20 36 31 36 2c 20    410 */   616, 
2a329 20 36 31 37 2c 20 20 36 31 38 2c 20 20 36 32 34   617,  618,  624
2a32a 2c 20 20 36 32 36 2c 20 20 36 34 34 2c 20 20 36  ,  626,  644,  6
2a32b 34 36 2c 20 20 36 33 30 2c 20 20 36 34 38 2c 20  46,  630,  648, 
2a32c 20 37 30 39 2c 0a 20 2f 2a 20 20 20 34 32 30 20   709,. /*   420 
2a32d 2a 2f 20 20 20 37 31 30 2c 20 20 37 37 35 2c 20  */   710,  775, 
2a32e 20 37 30 33 2c 20 20 37 30 34 2c 20 20 37 30 38   703,  704,  708
2a32f 2c 20 20 36 33 31 2c 20 20 37 38 36 2c 20 20 37  ,  631,  786,  7
2a330 37 37 2c 20 20 37 38 31 2c 20 20 37 38 32 2c 0a  77,  781,  782,.
2a331 20 2f 2a 20 20 20 34 33 30 20 2a 2f 20 20 20 37   /*   430 */   7
2a332 38 34 2c 20 20 37 38 35 2c 20 20 37 39 39 2c 20  84,  785,  799, 
2a333 20 38 30 30 2c 20 20 38 30 32 2c 20 20 38 30 38   800,  802,  808
2a334 2c 20 20 38 31 35 2c 20 20 38 31 38 2c 20 20 38  ,  815,  818,  8
2a335 30 31 2c 20 20 38 30 36 2c 0a 20 2f 2a 20 20 20  01,  806,. /*   
2a336 34 34 30 20 2a 2f 20 20 20 38 30 37 2c 20 20 38  440 */   807,  8
2a337 30 39 2c 20 20 38 31 34 2c 20 20 38 31 37 2c 20  09,  814,  817, 
2a338 20 37 30 36 2c 20 20 37 30 37 2c 20 20 38 32 31   706,  707,  821
2a339 2c 20 20 36 33 38 2c 20 20 36 33 39 2c 20 20 36  ,  638,  639,  6
2a33a 34 32 2c 0a 20 2f 2a 20 20 20 34 35 30 20 2a 2f  42,. /*   450 */
2a33b 20 20 20 36 34 33 2c 20 20 38 35 35 2c 20 20 38     643,  855,  8
2a33c 35 37 2c 20 20 38 35 36 2c 20 20 38 35 38 2c 20  57,  856,  858, 
2a33d 20 36 34 31 2c 20 20 36 34 30 2c 20 20 37 38 37   641,  640,  787
2a33e 2c 20 20 37 39 30 2c 20 20 38 32 33 2c 0a 20 2f  ,  790,  823,. /
2a33f 2a 20 20 20 34 36 30 20 2a 2f 20 20 20 38 32 34  *   460 */   824
2a340 2c 20 20 38 38 30 2c 20 20 38 38 31 2c 20 20 38  ,  880,  881,  8
2a341 38 32 2c 20 20 38 38 33 2c 20 20 38 38 34 2c 20  82,  883,  884, 
2a342 20 38 31 39 2c 20 20 37 32 39 2c 20 20 38 32 32   819,  729,  822
2a343 2c 20 20 38 30 35 2c 0a 20 2f 2a 20 20 20 34 37  ,  805,. /*   47
2a344 30 20 2a 2f 20 20 20 37 34 34 2c 20 20 37 34 37  0 */   744,  747
2a345 2c 20 20 37 34 38 2c 20 20 37 34 35 2c 20 20 37  ,  748,  745,  7
2a346 31 32 2c 20 20 37 32 32 2c 20 20 37 33 31 2c 20  12,  722,  731, 
2a347 20 37 33 32 2c 20 20 37 33 33 2c 20 20 37 33 34   732,  733,  734
2a348 2c 0a 20 2f 2a 20 20 20 34 38 30 20 2a 2f 20 20  ,. /*   480 */  
2a349 20 37 31 37 2c 20 20 37 31 38 2c 20 20 37 32 34   717,  718,  724
2a34a 2c 20 20 37 34 30 2c 20 20 37 37 33 2c 20 20 37  ,  740,  773,  7
2a34b 37 34 2c 20 20 37 33 38 2c 20 20 37 33 39 2c 20  74,  738,  739, 
2a34c 20 37 32 35 2c 20 20 37 31 34 2c 0a 20 2f 2a 20   725,  714,. /* 
2a34d 20 20 34 39 30 20 2a 2f 20 20 20 37 31 35 2c 20    490 */   715, 
2a34e 20 37 31 36 2c 20 20 38 32 30 2c 20 20 37 37 36   716,  820,  776
2a34f 2c 20 20 37 38 38 2c 20 20 37 38 39 2c 20 20 36  ,  788,  789,  6
2a350 34 39 2c 20 20 36 35 30 2c 20 20 37 38 33 2c 20  49,  650,  783, 
2a351 20 36 35 31 2c 0a 20 2f 2a 20 20 20 35 30 30 20   651,. /*   500 
2a352 2a 2f 20 20 20 36 35 32 2c 20 20 36 35 33 2c 20  */   652,  653, 
2a353 20 36 39 31 2c 20 20 36 39 34 2c 20 20 36 39 35   691,  694,  695
2a354 2c 20 20 36 39 36 2c 20 20 36 35 34 2c 20 20 36  ,  696,  654,  6
2a355 37 33 2c 20 20 36 37 36 2c 20 20 36 37 37 2c 0a  73,  676,  677,.
2a356 20 2f 2a 20 20 20 35 31 30 20 2a 2f 20 20 20 36   /*   510 */   6
2a357 35 35 2c 20 20 36 36 32 2c 20 20 36 35 36 2c 20  55,  662,  656, 
2a358 20 36 35 37 2c 20 20 36 36 34 2c 20 20 36 36 35   657,  664,  665
2a359 2c 20 20 36 36 36 2c 20 20 36 36 39 2c 20 20 36  ,  666,  669,  6
2a35a 37 30 2c 20 20 36 37 31 2c 0a 20 2f 2a 20 20 20  70,  671,. /*   
2a35b 35 32 30 20 2a 2f 20 20 20 36 37 32 2c 20 20 36  520 */   672,  6
2a35c 36 37 2c 20 20 36 36 38 2c 20 20 38 33 39 2c 20  67,  668,  839, 
2a35d 20 38 34 30 2c 20 20 38 34 33 2c 20 20 38 34 31   840,  843,  841
2a35e 2c 20 20 36 35 39 2c 20 20 36 36 30 2c 20 20 36  ,  659,  660,  6
2a35f 37 34 2c 0a 20 2f 2a 20 20 20 35 33 30 20 2a 2f  74,. /*   530 */
2a360 20 20 20 36 34 37 2c 20 20 36 33 36 2c 20 20 36     647,  636,  6
2a361 32 39 2c 20 20 36 38 30 2c 20 20 36 38 33 2c 20  29,  680,  683, 
2a362 20 36 38 34 2c 20 20 36 38 35 2c 20 20 36 38 36   684,  685,  686
2a363 2c 20 20 36 38 37 2c 20 20 36 38 39 2c 0a 20 2f  ,  687,  689,. /
2a364 2a 20 20 20 35 34 30 20 2a 2f 20 20 20 36 38 31  *   540 */   681
2a365 2c 20 20 36 38 32 2c 20 20 36 32 37 2c 20 20 36  ,  682,  627,  6
2a366 31 39 2c 20 20 36 32 31 2c 20 20 37 33 30 2c 20  19,  621,  730, 
2a367 20 38 36 31 2c 20 20 38 37 30 2c 20 20 38 36 36   861,  870,  866
2a368 2c 20 20 38 36 32 2c 0a 20 2f 2a 20 20 20 35 35  ,  862,. /*   55
2a369 30 20 2a 2f 20 20 20 38 36 33 2c 20 20 38 36 34  0 */   863,  864
2a36a 2c 20 20 36 32 32 2c 20 20 38 33 35 2c 20 20 38  ,  622,  835,  8
2a36b 33 36 2c 20 20 36 39 33 2c 20 20 37 36 37 2c 20  36,  693,  767, 
2a36c 20 37 36 38 2c 20 20 38 36 30 2c 20 20 38 37 33   768,  860,  873
2a36d 2c 0a 20 2f 2a 20 20 20 35 36 30 20 2a 2f 20 20  ,. /*   560 */  
2a36e 20 38 37 35 2c 20 20 37 37 32 2c 20 20 38 37 36   875,  772,  876
2a36f 2c 20 20 38 37 38 2c 20 20 38 37 34 2c 20 20 39  ,  878,  874,  9
2a370 30 33 2c 20 20 36 39 37 2c 20 20 36 39 38 2c 20  03,  697,  698, 
2a371 20 37 30 31 2c 20 20 38 34 34 2c 0a 20 2f 2a 20   701,  844,. /* 
2a372 20 20 35 37 30 20 2a 2f 20 20 20 38 38 35 2c 20    570 */   885, 
2a373 20 37 35 38 2c 20 20 37 36 31 2c 20 20 37 36 34   758,  761,  764
2a374 2c 20 20 37 36 36 2c 20 20 38 34 36 2c 20 20 38  ,  766,  846,  8
2a375 34 37 2c 20 20 38 34 38 2c 20 20 38 34 39 2c 20  47,  848,  849, 
2a376 20 38 35 30 2c 0a 20 2f 2a 20 20 20 35 38 30 20   850,. /*   580 
2a377 2a 2f 20 20 20 38 35 33 2c 20 20 38 35 34 2c 20  */   853,  854, 
2a378 20 38 35 31 2c 20 20 38 38 36 2c 20 20 38 39 30   851,  886,  890
2a379 2c 20 20 38 39 33 2c 20 20 38 39 35 2c 20 20 38  ,  893,  895,  8
2a37a 39 36 2c 20 20 38 39 37 2c 20 20 39 30 30 2c 0a  96,  897,  900,.
2a37b 20 2f 2a 20 20 20 35 39 30 20 2a 2f 20 20 20 39   /*   590 */   9
2a37c 30 32 2c 20 20 39 30 37 2c 20 20 39 30 38 2c 20  02,  907,  908, 
2a37d 20 39 30 39 2c 20 20 39 31 32 2c 20 20 39 31 30   909,  912,  910
2a37e 2c 20 20 36 30 35 2c 20 20 36 30 30 2c 0a 7d 3b  ,  605,  600,.};
2a37f 0a 23 64 65 66 69 6e 65 20 59 59 5f 53 5a 5f 41  .#define YY_SZ_A
2a380 43 54 54 41 42 20 28 69 6e 74 29 28 73 69 7a 65  CTTAB (int)(size
2a381 6f 66 28 79 79 5f 61 63 74 69 6f 6e 29 2f 73 69  of(yy_action)/si
2a382 7a 65 6f 66 28 79 79 5f 61 63 74 69 6f 6e 5b 30  zeof(yy_action[0
2a383 5d 29 29 0a 0a 2f 2a 20 54 68 65 20 6e 65 78 74  ]))../* The next
2a384 20 74 61 62 6c 65 20 6d 61 70 73 20 74 6f 6b 65   table maps toke
2a385 6e 73 20 69 6e 74 6f 20 66 61 6c 6c 62 61 63 6b  ns into fallback
2a386 20 74 6f 6b 65 6e 73 2e 20 20 49 66 20 61 20 63   tokens.  If a c
2a387 6f 6e 73 74 72 75 63 74 0a 2a 2a 20 6c 69 6b 65  onstruct.** like
2a388 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 3a 0a   the following:.
2a389 2a 2a 20 0a 2a 2a 20 20 20 20 20 20 25 66 61 6c  ** .**      %fal
2a38a 6c 62 61 63 6b 20 49 44 20 58 20 59 20 5a 2e 0a  lback ID X Y Z..
2a38b 2a 2a 0a 2a 2a 20 61 70 70 65 61 72 73 20 69 6e  **.** appears in
2a38c 20 74 68 65 20 67 72 61 6d 6d 61 72 2c 20 74 68   the grammar, th
2a38d 65 6e 20 49 44 20 62 65 63 6f 6d 65 73 20 61 20  en ID becomes a 
2a38e 66 61 6c 6c 62 61 63 6b 20 74 6f 6b 65 6e 20 66  fallback token f
2a38f 6f 72 20 58 2c 20 59 2c 0a 2a 2a 20 61 6e 64 20  or X, Y,.** and 
2a390 5a 2e 20 20 57 68 65 6e 65 76 65 72 20 6f 6e 65  Z.  Whenever one
2a391 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 73 20 58   of the tokens X
2a392 2c 20 59 2c 20 6f 72 20 5a 20 69 73 20 69 6e 70  , Y, or Z is inp
2a393 75 74 20 74 6f 20 74 68 65 20 70 61 72 73 65 72  ut to the parser
2a394 0a 2a 2a 20 62 75 74 20 69 74 20 64 6f 65 73 20  .** but it does 
2a395 6e 6f 74 20 70 61 72 73 65 2c 20 74 68 65 20 74  not parse, the t
2a396 79 70 65 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e  ype of the token
2a397 20 69 73 20 63 68 61 6e 67 65 64 20 74 6f 20 49   is changed to I
2a398 44 20 61 6e 64 0a 2a 2a 20 74 68 65 20 70 61 72  D and.** the par
2a399 73 65 20 69 73 20 72 65 74 72 69 65 64 20 62 65  se is retried be
2a39a 66 6f 72 65 20 61 6e 20 65 72 72 6f 72 20 69 73  fore an error is
2a39b 20 74 68 72 6f 77 6e 2e 0a 2a 2f 0a 23 69 66 64   thrown..*/.#ifd
2a39c 65 66 20 59 59 46 41 4c 4c 42 41 43 4b 0a 73 74  ef YYFALLBACK.st
2a39d 61 74 69 63 20 63 6f 6e 73 74 20 59 59 43 4f 44  atic const YYCOD
2a39e 45 54 59 50 45 20 79 79 46 61 6c 6c 62 61 63 6b  ETYPE yyFallback
2a39f 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 20 2f  [] = {.    0,  /
2a3a0 2a 20 20 20 20 20 20 20 20 20 20 24 20 3d 3e 20  *          $ => 
2a3a1 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a3a2 2c 20 20 2f 2a 20 20 20 20 20 20 20 53 45 4d 49  ,  /*       SEMI
2a3a3 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a3a4 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 45 58 50    23,  /*    EXP
2a3a5 4c 41 49 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  LAIN => ID */.  
2a3a6 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 51 55   23,  /*      QU
2a3a7 45 52 59 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ERY => ID */.   
2a3a8 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 50 4c  23,  /*       PL
2a3a9 41 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  AN => ID */.   2
2a3aa 33 2c 20 20 2f 2a 20 20 20 20 20 20 42 45 47 49  3,  /*      BEGI
2a3ab 4e 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  N => ID */.    0
2a3ac 2c 20 20 2f 2a 20 54 52 41 4e 53 41 43 54 49 4f  ,  /* TRANSACTIO
2a3ad 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a  N => nothing */.
2a3ae 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 44 45 46     23,  /*   DEF
2a3af 45 52 52 45 44 20 3d 3e 20 49 44 20 2a 2f 0a 20  ERRED => ID */. 
2a3b0 20 20 32 33 2c 20 20 2f 2a 20 20 49 4d 4d 45 44    23,  /*  IMMED
2a3b1 49 41 54 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  IATE => ID */.  
2a3b2 20 32 33 2c 20 20 2f 2a 20 20 45 58 43 4c 55 53   23,  /*  EXCLUS
2a3b3 49 56 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  IVE => ID */.   
2a3b4 20 30 2c 20 20 2f 2a 20 20 20 20 20 43 4f 4d 4d   0,  /*     COMM
2a3b5 49 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f  IT => nothing */
2a3b6 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20  .   23,  /*     
2a3b7 20 20 20 45 4e 44 20 3d 3e 20 49 44 20 2a 2f 0a     END => ID */.
2a3b8 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 52 4f 4c      0,  /*   ROL
2a3b9 4c 42 41 43 4b 20 3d 3e 20 6e 6f 74 68 69 6e 67  LBACK => nothing
2a3ba 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20   */.    0,  /*  
2a3bb 20 20 20 43 52 45 41 54 45 20 3d 3e 20 6e 6f 74     CREATE => not
2a3bc 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20  hing */.    0,  
2a3bd 2f 2a 20 20 20 20 20 20 54 41 42 4c 45 20 3d 3e  /*      TABLE =>
2a3be 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 32   nothing */.   2
2a3bf 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 49  3,  /*         I
2a3c0 46 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 20 30  F => ID */.    0
2a3c1 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 4e 4f 54  ,  /*        NOT
2a3c2 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a3c3 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 45 58     0,  /*     EX
2a3c4 49 53 54 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  ISTS => nothing 
2a3c5 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
2a3c6 20 20 20 20 54 45 4d 50 20 3d 3e 20 49 44 20 2a      TEMP => ID *
2a3c7 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2a3c8 20 20 20 20 20 4c 50 20 3d 3e 20 6e 6f 74 68 69       LP => nothi
2a3c9 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a  ng */.    0,  /*
2a3ca 20 20 20 20 20 20 20 20 20 52 50 20 3d 3e 20 6e           RP => n
2a3cb 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c  othing */.    0,
2a3cc 20 20 2f 2a 20 20 20 20 20 20 20 20 20 41 53 20    /*         AS 
2a3cd 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20  => nothing */.  
2a3ce 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 43 4f    0,  /*      CO
2a3cf 4d 4d 41 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a  MMA => nothing *
2a3d0 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20 20  /.    0,  /*    
2a3d1 20 20 20 20 20 49 44 20 3d 3e 20 6e 6f 74 68 69       ID => nothi
2a3d2 6e 67 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ng */.   23,  /*
2a3d3 20 20 20 20 20 20 41 42 4f 52 54 20 3d 3e 20 49        ABORT => I
2a3d4 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
2a3d5 20 20 20 20 20 41 46 54 45 52 20 3d 3e 20 49 44       AFTER => ID
2a3d6 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
2a3d7 20 20 41 4e 41 4c 59 5a 45 20 3d 3e 20 49 44 20    ANALYZE => ID 
2a3d8 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
2a3d9 20 20 20 20 20 41 53 43 20 3d 3e 20 49 44 20 2a       ASC => ID *
2a3da 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
2a3db 20 41 54 54 41 43 48 20 3d 3e 20 49 44 20 2a 2f   ATTACH => ID */
2a3dc 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20  .   23,  /*     
2a3dd 42 45 46 4f 52 45 20 3d 3e 20 49 44 20 2a 2f 0a  BEFORE => ID */.
2a3de 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 43 41     23,  /*    CA
2a3df 53 43 41 44 45 20 3d 3e 20 49 44 20 2a 2f 0a 20  SCADE => ID */. 
2a3e0 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 20    23,  /*       
2a3e1 43 41 53 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  CAST => ID */.  
2a3e2 20 32 33 2c 20 20 2f 2a 20 20 20 43 4f 4e 46 4c   23,  /*   CONFL
2a3e3 49 43 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ICT => ID */.   
2a3e4 32 33 2c 20 20 2f 2a 20 20 20 44 41 54 41 42 41  23,  /*   DATABA
2a3e5 53 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  SE => ID */.   2
2a3e6 33 2c 20 20 2f 2a 20 20 20 20 20 20 20 44 45 53  3,  /*       DES
2a3e7 43 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  C => ID */.   23
2a3e8 2c 20 20 2f 2a 20 20 20 20 20 44 45 54 41 43 48  ,  /*     DETACH
2a3e9 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
2a3ea 20 20 2f 2a 20 20 20 20 20 20 20 45 41 43 48 20    /*       EACH 
2a3eb 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
2a3ec 20 2f 2a 20 20 20 20 20 20 20 46 41 49 4c 20 3d   /*       FAIL =
2a3ed 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2a3ee 2f 2a 20 20 20 20 20 20 20 20 46 4f 52 20 3d 3e  /*        FOR =>
2a3ef 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2a3f0 2a 20 20 20 20 20 49 47 4e 4f 52 45 20 3d 3e 20  *     IGNORE => 
2a3f1 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2a3f2 20 20 49 4e 49 54 49 41 4c 4c 59 20 3d 3e 20 49    INITIALLY => I
2a3f3 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
2a3f4 20 20 20 49 4e 53 54 45 41 44 20 3d 3e 20 49 44     INSTEAD => ID
2a3f5 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
2a3f6 20 20 4c 49 4b 45 5f 4b 57 20 3d 3e 20 49 44 20    LIKE_KW => ID 
2a3f7 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20  */.   23,  /*   
2a3f8 20 20 20 4d 41 54 43 48 20 3d 3e 20 49 44 20 2a     MATCH => ID *
2a3f9 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20  /.   23,  /*    
2a3fa 20 20 20 20 4b 45 59 20 3d 3e 20 49 44 20 2a 2f      KEY => ID */
2a3fb 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20  .   23,  /*     
2a3fc 20 20 20 20 4f 46 20 3d 3e 20 49 44 20 2a 2f 0a      OF => ID */.
2a3fd 20 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 4f     23,  /*     O
2a3fe 46 46 53 45 54 20 3d 3e 20 49 44 20 2a 2f 0a 20  FFSET => ID */. 
2a3ff 20 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 50 52    23,  /*     PR
2a400 41 47 4d 41 20 3d 3e 20 49 44 20 2a 2f 0a 20 20  AGMA => ID */.  
2a401 20 32 33 2c 20 20 2f 2a 20 20 20 20 20 20 52 41   23,  /*      RA
2a402 49 53 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20  ISE => ID */.   
2a403 32 33 2c 20 20 2f 2a 20 20 20 20 52 45 50 4c 41  23,  /*    REPLA
2a404 43 45 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32  CE => ID */.   2
2a405 33 2c 20 20 2f 2a 20 20 20 52 45 53 54 52 49 43  3,  /*   RESTRIC
2a406 54 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33  T => ID */.   23
2a407 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 52 4f 57  ,  /*        ROW
2a408 20 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c   => ID */.   23,
2a409 20 20 2f 2a 20 20 20 20 54 52 49 47 47 45 52 20    /*    TRIGGER 
2a40a 3d 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20  => ID */.   23, 
2a40b 20 2f 2a 20 20 20 20 20 56 41 43 55 55 4d 20 3d   /*     VACUUM =
2a40c 3e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20  > ID */.   23,  
2a40d 2f 2a 20 20 20 20 20 20 20 56 49 45 57 20 3d 3e  /*       VIEW =>
2a40e 20 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f   ID */.   23,  /
2a40f 2a 20 20 20 20 56 49 52 54 55 41 4c 20 3d 3e 20  *    VIRTUAL => 
2a410 49 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a  ID */.   23,  /*
2a411 20 20 20 20 52 45 49 4e 44 45 58 20 3d 3e 20 49      REINDEX => I
2a412 44 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20  D */.   23,  /* 
2a413 20 20 20 20 52 45 4e 41 4d 45 20 3d 3e 20 49 44      RENAME => ID
2a414 20 2a 2f 0a 20 20 20 32 33 2c 20 20 2f 2a 20 20   */.   23,  /*  
2a415 20 43 54 49 4d 45 5f 4b 57 20 3d 3e 20 49 44 20   CTIME_KW => ID 
2a416 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a417 20 20 20 20 20 41 4e 59 20 3d 3e 20 6e 6f 74 68       ANY => noth
2a418 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a419 2a 20 20 20 20 20 20 20 20 20 4f 52 20 3d 3e 20  *         OR => 
2a41a 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a41b 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 4e 44  ,  /*        AND
2a41c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a41d 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a41e 20 20 49 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    IS => nothing 
2a41f 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a420 20 42 45 54 57 45 45 4e 20 3d 3e 20 6e 6f 74 68   BETWEEN => noth
2a421 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a422 2a 20 20 20 20 20 20 20 20 20 49 4e 20 3d 3e 20  *         IN => 
2a423 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a424 2c 20 20 2f 2a 20 20 20 20 20 49 53 4e 55 4c 4c  ,  /*     ISNULL
2a425 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a426 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 4e 4f 54     0,  /*    NOT
2a427 4e 55 4c 4c 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NULL => nothing 
2a428 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a429 20 20 20 20 20 20 4e 45 20 3d 3e 20 6e 6f 74 68        NE => noth
2a42a 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a42b 2a 20 20 20 20 20 20 20 20 20 45 51 20 3d 3e 20  *         EQ => 
2a42c 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a42d 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 20 47 54  ,  /*         GT
2a42e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a42f 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a430 20 20 4c 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    LE => nothing 
2a431 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a432 20 20 20 20 20 20 4c 54 20 3d 3e 20 6e 6f 74 68        LT => noth
2a433 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a434 2a 20 20 20 20 20 20 20 20 20 47 45 20 3d 3e 20  *         GE => 
2a435 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a436 2c 20 20 2f 2a 20 20 20 20 20 45 53 43 41 50 45  ,  /*     ESCAPE
2a437 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a438 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 42 49     0,  /*     BI
2a439 54 41 4e 44 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  TAND => nothing 
2a43a 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a43b 20 20 20 42 49 54 4f 52 20 3d 3e 20 6e 6f 74 68     BITOR => noth
2a43c 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a43d 2a 20 20 20 20 20 4c 53 48 49 46 54 20 3d 3e 20  *     LSHIFT => 
2a43e 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a43f 2c 20 20 2f 2a 20 20 20 20 20 52 53 48 49 46 54  ,  /*     RSHIFT
2a440 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a441 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a442 50 4c 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  PLUS => nothing 
2a443 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a444 20 20 20 4d 49 4e 55 53 20 3d 3e 20 6e 6f 74 68     MINUS => noth
2a445 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a446 2a 20 20 20 20 20 20 20 53 54 41 52 20 3d 3e 20  *       STAR => 
2a447 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a448 2c 20 20 2f 2a 20 20 20 20 20 20 53 4c 41 53 48  ,  /*      SLASH
2a449 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a44a 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a44b 20 52 45 4d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   REM => nothing 
2a44c 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a44d 20 20 43 4f 4e 43 41 54 20 3d 3e 20 6e 6f 74 68    CONCAT => noth
2a44e 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a44f 2a 20 20 20 20 43 4f 4c 4c 41 54 45 20 3d 3e 20  *    COLLATE => 
2a450 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a451 2c 20 20 2f 2a 20 20 20 20 20 55 4d 49 4e 55 53  ,  /*     UMINUS
2a452 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a453 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 55     0,  /*      U
2a454 50 4c 55 53 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  PLUS => nothing 
2a455 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a456 20 20 42 49 54 4e 4f 54 20 3d 3e 20 6e 6f 74 68    BITNOT => noth
2a457 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a458 2a 20 20 20 20 20 53 54 52 49 4e 47 20 3d 3e 20  *     STRING => 
2a459 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a45a 2c 20 20 2f 2a 20 20 20 20 4a 4f 49 4e 5f 4b 57  ,  /*    JOIN_KW
2a45b 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a45c 20 20 20 30 2c 20 20 2f 2a 20 43 4f 4e 53 54 52     0,  /* CONSTR
2a45d 41 49 4e 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  AINT => nothing 
2a45e 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a45f 20 44 45 46 41 55 4c 54 20 3d 3e 20 6e 6f 74 68   DEFAULT => noth
2a460 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a461 2a 20 20 20 20 20 20 20 4e 55 4c 4c 20 3d 3e 20  *       NULL => 
2a462 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a463 2c 20 20 2f 2a 20 20 20 20 50 52 49 4d 41 52 59  ,  /*    PRIMARY
2a464 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a465 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 55 4e     0,  /*     UN
2a466 49 51 55 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  IQUE => nothing 
2a467 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a468 20 20 20 43 48 45 43 4b 20 3d 3e 20 6e 6f 74 68     CHECK => noth
2a469 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a46a 2a 20 52 45 46 45 52 45 4e 43 45 53 20 3d 3e 20  * REFERENCES => 
2a46b 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a46c 2c 20 20 2f 2a 20 20 20 41 55 54 4f 49 4e 43 52  ,  /*   AUTOINCR
2a46d 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a46e 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a46f 20 20 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20    ON => nothing 
2a470 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a471 20 20 44 45 4c 45 54 45 20 3d 3e 20 6e 6f 74 68    DELETE => noth
2a472 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a473 2a 20 20 20 20 20 55 50 44 41 54 45 20 3d 3e 20  *     UPDATE => 
2a474 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a475 2c 20 20 2f 2a 20 20 20 20 20 49 4e 53 45 52 54  ,  /*     INSERT
2a476 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a477 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a478 20 53 45 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20   SET => nothing 
2a479 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 44 45  */.    0,  /* DE
2a47a 46 45 52 52 41 42 4c 45 20 3d 3e 20 6e 6f 74 68  FERRABLE => noth
2a47b 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a47c 2a 20 20 20 20 46 4f 52 45 49 47 4e 20 3d 3e 20  *    FOREIGN => 
2a47d 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a47e 2c 20 20 2f 2a 20 20 20 20 20 20 20 44 52 4f 50  ,  /*       DROP
2a47f 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a480 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 55     0,  /*      U
2a481 4e 49 4f 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NION => nothing 
2a482 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a483 20 20 20 20 20 41 4c 4c 20 3d 3e 20 6e 6f 74 68       ALL => noth
2a484 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a485 2a 20 20 20 20 20 45 58 43 45 50 54 20 3d 3e 20  *     EXCEPT => 
2a486 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a487 2c 20 20 2f 2a 20 20 49 4e 54 45 52 53 45 43 54  ,  /*  INTERSECT
2a488 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a489 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 53 45     0,  /*     SE
2a48a 4c 45 43 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LECT => nothing 
2a48b 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a48c 44 49 53 54 49 4e 43 54 20 3d 3e 20 6e 6f 74 68  DISTINCT => noth
2a48d 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a48e 2a 20 20 20 20 20 20 20 20 44 4f 54 20 3d 3e 20  *        DOT => 
2a48f 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a490 2c 20 20 2f 2a 20 20 20 20 20 20 20 46 52 4f 4d  ,  /*       FROM
2a491 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a492 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a493 4a 4f 49 4e 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  JOIN => nothing 
2a494 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a495 20 49 4e 44 45 58 45 44 20 3d 3e 20 6e 6f 74 68   INDEXED => noth
2a496 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a497 2a 20 20 20 20 20 20 20 20 20 42 59 20 3d 3e 20  *         BY => 
2a498 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a499 2c 20 20 2f 2a 20 20 20 20 20 20 55 53 49 4e 47  ,  /*      USING
2a49a 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a49b 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 4f     0,  /*      O
2a49c 52 44 45 52 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  RDER => nothing 
2a49d 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a49e 20 20 20 47 52 4f 55 50 20 3d 3e 20 6e 6f 74 68     GROUP => noth
2a49f 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a4a0 2a 20 20 20 20 20 48 41 56 49 4e 47 20 3d 3e 20  *     HAVING => 
2a4a1 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a4a2 2c 20 20 2f 2a 20 20 20 20 20 20 4c 49 4d 49 54  ,  /*      LIMIT
2a4a3 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a4a4 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 57     0,  /*      W
2a4a5 48 45 52 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  HERE => nothing 
2a4a6 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a4a7 20 20 20 20 49 4e 54 4f 20 3d 3e 20 6e 6f 74 68      INTO => noth
2a4a8 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a4a9 2a 20 20 20 20 20 56 41 4c 55 45 53 20 3d 3e 20  *     VALUES => 
2a4aa 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a4ab 2c 20 20 2f 2a 20 20 20 20 49 4e 54 45 47 45 52  ,  /*    INTEGER
2a4ac 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a4ad 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 46     0,  /*      F
2a4ae 4c 4f 41 54 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  LOAT => nothing 
2a4af 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a4b0 20 20 20 20 42 4c 4f 42 20 3d 3e 20 6e 6f 74 68      BLOB => noth
2a4b1 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a4b2 2a 20 20 20 52 45 47 49 53 54 45 52 20 3d 3e 20  *   REGISTER => 
2a4b3 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a4b4 2c 20 20 2f 2a 20 20 20 56 41 52 49 41 42 4c 45  ,  /*   VARIABLE
2a4b5 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a4b6 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 20     0,  /*       
2a4b7 43 41 53 45 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  CASE => nothing 
2a4b8 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a4b9 20 20 20 20 57 48 45 4e 20 3d 3e 20 6e 6f 74 68      WHEN => noth
2a4ba 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a4bb 2a 20 20 20 20 20 20 20 54 48 45 4e 20 3d 3e 20  *       THEN => 
2a4bc 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a4bd 2c 20 20 2f 2a 20 20 20 20 20 20 20 45 4c 53 45  ,  /*       ELSE
2a4be 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a4bf 20 20 20 30 2c 20 20 2f 2a 20 20 20 20 20 20 49     0,  /*      I
2a4c0 4e 44 45 58 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  NDEX => nothing 
2a4c1 2a 2f 0a 20 20 20 20 30 2c 20 20 2f 2a 20 20 20  */.    0,  /*   
2a4c2 20 20 20 41 4c 54 45 52 20 3d 3e 20 6e 6f 74 68     ALTER => noth
2a4c3 69 6e 67 20 2a 2f 0a 20 20 20 20 30 2c 20 20 2f  ing */.    0,  /
2a4c4 2a 20 20 20 20 20 20 20 20 20 54 4f 20 3d 3e 20  *         TO => 
2a4c5 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20 20 20 20 30  nothing */.    0
2a4c6 2c 20 20 2f 2a 20 20 20 20 20 20 20 20 41 44 44  ,  /*        ADD
2a4c7 20 3d 3e 20 6e 6f 74 68 69 6e 67 20 2a 2f 0a 20   => nothing */. 
2a4c8 20 20 20 30 2c 20 20 2f 2a 20 20 20 43 4f 4c 55     0,  /*   COLU
2a4c9 4d 4e 4b 57 20 3d 3e 20 6e 6f 74 68 69 6e 67 20  MNKW => nothing 
2a4ca 2a 2f 0a 7d 3b 0a 23 65 6e 64 69 66 20 2f 2a 20  */.};.#endif /* 
2a4cb 59 59 46 41 4c 4c 42 41 43 4b 20 2a 2f 0a 0a 2f  YYFALLBACK */../
2a4cc 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  * The following 
2a4cd 73 74 72 75 63 74 75 72 65 20 72 65 70 72 65 73  structure repres
2a4ce 65 6e 74 73 20 61 20 73 69 6e 67 6c 65 20 65 6c  ents a single el
2a4cf 65 6d 65 6e 74 20 6f 66 20 74 68 65 0a 2a 2a 20  ement of the.** 
2a4d0 70 61 72 73 65 72 27 73 20 73 74 61 63 6b 2e 20  parser's stack. 
2a4d1 20 49 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 74 6f   Information sto
2a4d2 72 65 64 20 69 6e 63 6c 75 64 65 73 3a 0a 2a 2a  red includes:.**
2a4d3 0a 2a 2a 20 20 20 2b 20 20 54 68 65 20 73 74 61  .**   +  The sta
2a4d4 74 65 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68  te number for th
2a4d5 65 20 70 61 72 73 65 72 20 61 74 20 74 68 69 73  e parser at this
2a4d6 20 6c 65 76 65 6c 20 6f 66 20 74 68 65 20 73 74   level of the st
2a4d7 61 63 6b 2e 0a 2a 2a 0a 2a 2a 20 20 20 2b 20 20  ack..**.**   +  
2a4d8 54 68 65 20 76 61 6c 75 65 20 6f 66 20 74 68 65  The value of the
2a4d9 20 74 6f 6b 65 6e 20 73 74 6f 72 65 64 20 61 74   token stored at
2a4da 20 74 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74   this level of t
2a4db 68 65 20 73 74 61 63 6b 2e 0a 2a 2a 20 20 20 20  he stack..**    
2a4dc 20 20 28 49 6e 20 6f 74 68 65 72 20 77 6f 72 64    (In other word
2a4dd 73 2c 20 74 68 65 20 22 6d 61 6a 6f 72 22 20 74  s, the "major" t
2a4de 6f 6b 65 6e 2e 29 0a 2a 2a 0a 2a 2a 20 20 20 2b  oken.).**.**   +
2a4df 20 20 54 68 65 20 73 65 6d 61 6e 74 69 63 20 76    The semantic v
2a4e0 61 6c 75 65 20 73 74 6f 72 65 64 20 61 74 20 74  alue stored at t
2a4e1 68 69 73 20 6c 65 76 65 6c 20 6f 66 20 74 68 65  his level of the
2a4e2 20 73 74 61 63 6b 2e 20 20 54 68 69 73 20 69 73   stack.  This is
2a4e3 0a 2a 2a 20 20 20 20 20 20 74 68 65 20 69 6e 66  .**      the inf
2a4e4 6f 72 6d 61 74 69 6f 6e 20 75 73 65 64 20 62 79  ormation used by
2a4e5 20 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74   the action rout
2a4e6 69 6e 65 73 20 69 6e 20 74 68 65 20 67 72 61 6d  ines in the gram
2a4e7 6d 61 72 2e 0a 2a 2a 20 20 20 20 20 20 49 74 20  mar..**      It 
2a4e8 69 73 20 73 6f 6d 65 74 69 6d 65 73 20 63 61 6c  is sometimes cal
2a4e9 6c 65 64 20 74 68 65 20 22 6d 69 6e 6f 72 22 20  led the "minor" 
2a4ea 74 6f 6b 65 6e 2e 0a 2a 2f 0a 73 74 72 75 63 74  token..*/.struct
2a4eb 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 7b 0a   yyStackEntry {.
2a4ec 20 20 59 59 41 43 54 49 4f 4e 54 59 50 45 20 73    YYACTIONTYPE s
2a4ed 74 61 74 65 6e 6f 3b 20 20 2f 2a 20 54 68 65 20  tateno;  /* The 
2a4ee 73 74 61 74 65 2d 6e 75 6d 62 65 72 20 2a 2f 0a  state-number */.
2a4ef 20 20 59 59 43 4f 44 45 54 59 50 45 20 6d 61 6a    YYCODETYPE maj
2a4f0 6f 72 3b 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or;      /* The 
2a4f1 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 76 61 6c 75  major token valu
2a4f2 65 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  e.  This is the 
2a4f3 63 6f 64 65 0a 20 20 20 20 20 20 20 20 20 20 20  code.           
2a4f4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a                **
2a4f5 20 6e 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20   number for the 
2a4f6 74 6f 6b 65 6e 20 61 74 20 74 68 69 73 20 73 74  token at this st
2a4f7 61 63 6b 20 6c 65 76 65 6c 20 2a 2f 0a 20 20 59  ack level */.  Y
2a4f8 59 4d 49 4e 4f 52 54 59 50 45 20 6d 69 6e 6f 72  YMINORTYPE minor
2a4f9 3b 20 20 20 20 20 2f 2a 20 54 68 65 20 75 73 65  ;     /* The use
2a4fa 72 2d 73 75 70 70 6c 69 65 64 20 6d 69 6e 6f 72  r-supplied minor
2a4fb 20 74 6f 6b 65 6e 20 76 61 6c 75 65 2e 20 20 54   token value.  T
2a4fc 68 69 73 0a 20 20 20 20 20 20 20 20 20 20 20 20  his.            
2a4fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2a 2a 20               ** 
2a4fe 69 73 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20  is the value of 
2a4ff 74 68 65 20 74 6f 6b 65 6e 20 20 2a 2f 0a 7d 3b  the token  */.};
2a500 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2a501 79 79 53 74 61 63 6b 45 6e 74 72 79 20 79 79 53  yyStackEntry yyS
2a502 74 61 63 6b 45 6e 74 72 79 3b 0a 0a 2f 2a 20 54  tackEntry;../* T
2a503 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
2a504 70 61 72 73 65 72 20 69 73 20 63 6f 6d 70 6c 65  parser is comple
2a505 74 65 6c 79 20 63 6f 6e 74 61 69 6e 65 64 20 69  tely contained i
2a506 6e 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  n an instance of
2a507 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** the followin
2a508 67 20 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 73  g structure */.s
2a509 74 72 75 63 74 20 79 79 50 61 72 73 65 72 20 7b  truct yyParser {
2a50a 0a 20 20 69 6e 74 20 79 79 69 64 78 3b 20 20 20  .  int yyidx;   
2a50b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a50c 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 74 6f 70   /* Index of top
2a50d 20 65 6c 65 6d 65 6e 74 20 69 6e 20 73 74 61 63   element in stac
2a50e 6b 20 2a 2f 0a 23 69 66 64 65 66 20 59 59 54 52  k */.#ifdef YYTR
2a50f 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48  ACKMAXSTACKDEPTH
2a510 0a 20 20 69 6e 74 20 79 79 69 64 78 4d 61 78 3b  .  int yyidxMax;
2a511 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a512 20 2f 2a 20 4d 61 78 69 6d 75 6d 20 76 61 6c 75   /* Maximum valu
2a513 65 20 6f 66 20 79 79 69 64 78 20 2a 2f 0a 23 65  e of yyidx */.#e
2a514 6e 64 69 66 0a 20 20 69 6e 74 20 79 79 65 72 72  ndif.  int yyerr
2a515 63 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  cnt;            
2a516 20 20 20 20 20 2f 2a 20 53 68 69 66 74 73 20 6c       /* Shifts l
2a517 65 66 74 20 62 65 66 6f 72 65 20 6f 75 74 20 6f  eft before out o
2a518 66 20 74 68 65 20 65 72 72 6f 72 20 2a 2f 0a 20  f the error */. 
2a519 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2a51a 47 5f 53 44 45 43 4c 20 20 20 20 20 20 20 20 20  G_SDECL         
2a51b 20 20 20 20 20 20 20 2f 2a 20 41 20 70 6c 61 63         /* A plac
2a51c 65 20 74 6f 20 68 6f 6c 64 20 25 65 78 74 72 61  e to hold %extra
2a51d 5f 61 72 67 75 6d 65 6e 74 20 2a 2f 0a 23 69 66  _argument */.#if
2a51e 20 59 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30   YYSTACKDEPTH<=0
2a51f 0a 20 20 69 6e 74 20 79 79 73 74 6b 73 7a 3b 20  .  int yystksz; 
2a520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2a521 20 2f 2a 20 43 75 72 72 65 6e 74 20 73 69 64 65   /* Current side
2a522 20 6f 66 20 74 68 65 20 73 74 61 63 6b 20 2a 2f   of the stack */
2a523 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
2a524 2a 79 79 73 74 61 63 6b 3b 20 20 20 20 20 20 20  *yystack;       
2a525 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 27 73   /* The parser's
2a526 20 73 74 61 63 6b 20 2a 2f 0a 23 65 6c 73 65 0a   stack */.#else.
2a527 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20 79    yyStackEntry y
2a528 79 73 74 61 63 6b 5b 59 59 53 54 41 43 4b 44 45  ystack[YYSTACKDE
2a529 50 54 48 5d 3b 20 20 2f 2a 20 54 68 65 20 70 61  PTH];  /* The pa
2a52a 72 73 65 72 27 73 20 73 74 61 63 6b 20 2a 2f 0a  rser's stack */.
2a52b 23 65 6e 64 69 66 0a 7d 3b 0a 74 79 70 65 64 65  #endif.};.typede
2a52c 66 20 73 74 72 75 63 74 20 79 79 50 61 72 73 65  f struct yyParse
2a52d 72 20 79 79 50 61 72 73 65 72 3b 0a 0a 23 69 66  r yyParser;..#if
2a52e 6e 64 65 66 20 4e 44 45 42 55 47 0a 73 74 61 74  ndef NDEBUG.stat
2a52f 69 63 20 46 49 4c 45 20 2a 79 79 54 72 61 63 65  ic FILE *yyTrace
2a530 46 49 4c 45 20 3d 20 30 3b 0a 73 74 61 74 69 63  FILE = 0;.static
2a531 20 63 68 61 72 20 2a 79 79 54 72 61 63 65 50 72   char *yyTracePr
2a532 6f 6d 70 74 20 3d 20 30 3b 0a 23 65 6e 64 69 66  ompt = 0;.#endif
2a533 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23   /* NDEBUG */..#
2a534 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
2a535 20 0a 2a 2a 20 54 75 72 6e 20 70 61 72 73 65 72   .** Turn parser
2a536 20 74 72 61 63 69 6e 67 20 6f 6e 20 62 79 20 67   tracing on by g
2a537 69 76 69 6e 67 20 61 20 73 74 72 65 61 6d 20 74  iving a stream t
2a538 6f 20 77 68 69 63 68 20 74 6f 20 77 72 69 74 65  o which to write
2a539 20 74 68 65 20 74 72 61 63 65 0a 2a 2a 20 61 6e   the trace.** an
2a53a 64 20 61 20 70 72 6f 6d 70 74 20 74 6f 20 70 72  d a prompt to pr
2a53b 65 66 61 63 65 20 65 61 63 68 20 74 72 61 63 65  eface each trace
2a53c 20 6d 65 73 73 61 67 65 2e 20 20 54 72 61 63 69   message.  Traci
2a53d 6e 67 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66  ng is turned off
2a53e 0a 2a 2a 20 62 79 20 6d 61 6b 69 6e 67 20 65 69  .** by making ei
2a53f 74 68 65 72 20 61 72 67 75 6d 65 6e 74 20 4e 55  ther argument NU
2a540 4c 4c 20 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73  LL .**.** Inputs
2a541 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69  :.** <ul>.** <li
2a542 3e 20 41 20 46 49 4c 45 2a 20 74 6f 20 77 68 69  > A FILE* to whi
2a543 63 68 20 74 72 61 63 65 20 6f 75 74 70 75 74 20  ch trace output 
2a544 73 68 6f 75 6c 64 20 62 65 20 77 72 69 74 74 65  should be writte
2a545 6e 2e 0a 2a 2a 20 20 20 20 20 20 49 66 20 4e 55  n..**      If NU
2a546 4c 4c 2c 20 74 68 65 6e 20 74 72 61 63 69 6e 67  LL, then tracing
2a547 20 69 73 20 74 75 72 6e 65 64 20 6f 66 66 2e 0a   is turned off..
2a548 2a 2a 20 3c 6c 69 3e 20 41 20 70 72 65 66 69 78  ** <li> A prefix
2a549 20 73 74 72 69 6e 67 20 77 72 69 74 74 65 6e 20   string written 
2a54a 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
2a54b 20 6f 66 20 65 76 65 72 79 0a 2a 2a 20 20 20 20   of every.**    
2a54c 20 20 6c 69 6e 65 20 6f 66 20 74 72 61 63 65 20    line of trace 
2a54d 6f 75 74 70 75 74 2e 20 20 49 66 20 4e 55 4c 4c  output.  If NULL
2a54e 2c 20 74 68 65 6e 20 74 72 61 63 69 6e 67 20 69  , then tracing i
2a54f 73 0a 2a 2a 20 20 20 20 20 20 74 75 72 6e 65 64  s.**      turned
2a550 20 6f 66 66 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a   off..** </ul>.*
2a551 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a 0a 2a 2a  *.** Outputs:.**
2a552 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45   None..*/.SQLITE
2a553 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2a554 6c 69 74 65 33 50 61 72 73 65 72 54 72 61 63 65  lite3ParserTrace
2a555 28 46 49 4c 45 20 2a 54 72 61 63 65 46 49 4c 45  (FILE *TraceFILE
2a556 2c 20 63 68 61 72 20 2a 7a 54 72 61 63 65 50 72  , char *zTracePr
2a557 6f 6d 70 74 29 7b 0a 20 20 79 79 54 72 61 63 65  ompt){.  yyTrace
2a558 46 49 4c 45 20 3d 20 54 72 61 63 65 46 49 4c 45  FILE = TraceFILE
2a559 3b 0a 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70  ;.  yyTracePromp
2a55a 74 20 3d 20 7a 54 72 61 63 65 50 72 6f 6d 70 74  t = zTracePrompt
2a55b 3b 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  ;.  if( yyTraceF
2a55c 49 4c 45 3d 3d 30 20 29 20 79 79 54 72 61 63 65  ILE==0 ) yyTrace
2a55d 50 72 6f 6d 70 74 20 3d 20 30 3b 0a 20 20 65 6c  Prompt = 0;.  el
2a55e 73 65 20 69 66 28 20 79 79 54 72 61 63 65 50 72  se if( yyTracePr
2a55f 6f 6d 70 74 3d 3d 30 20 29 20 79 79 54 72 61 63  ompt==0 ) yyTrac
2a560 65 46 49 4c 45 20 3d 20 30 3b 0a 7d 0a 23 65 6e  eFILE = 0;.}.#en
2a561 64 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f  dif /* NDEBUG */
2a562 0a 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ..#ifndef NDEBUG
2a563 0a 2f 2a 20 46 6f 72 20 74 72 61 63 69 6e 67 20  ./* For tracing 
2a564 73 68 69 66 74 73 2c 20 74 68 65 20 6e 61 6d 65  shifts, the name
2a565 73 20 6f 66 20 61 6c 6c 20 74 65 72 6d 69 6e 61  s of all termina
2a566 6c 73 20 61 6e 64 20 6e 6f 6e 74 65 72 6d 69 6e  ls and nontermin
2a567 61 6c 73 0a 2a 2a 20 61 72 65 20 72 65 71 75 69  als.** are requi
2a568 72 65 64 2e 20 20 54 68 65 20 66 6f 6c 6c 6f 77  red.  The follow
2a569 69 6e 67 20 74 61 62 6c 65 20 73 75 70 70 6c 69  ing table suppli
2a56a 65 73 20 74 68 65 73 65 20 6e 61 6d 65 73 20 2a  es these names *
2a56b 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  /.static const c
2a56c 68 61 72 20 2a 63 6f 6e 73 74 20 79 79 54 6f 6b  har *const yyTok
2a56d 65 6e 4e 61 6d 65 5b 5d 20 3d 20 7b 20 0a 20 20  enName[] = { .  
2a56e 22 24 22 2c 20 20 20 20 20 20 20 20 20 20 20 20  "$",            
2a56f 20 22 53 45 4d 49 22 2c 20 20 20 20 20 20 20 20   "SEMI",        
2a570 20 20 22 45 58 50 4c 41 49 4e 22 2c 20 20 20 20    "EXPLAIN",    
2a571 20 20 20 22 51 55 45 52 59 22 2c 20 20 20 20 20     "QUERY",     
2a572 20 20 0a 20 20 22 50 4c 41 4e 22 2c 20 20 20 20    .  "PLAN",    
2a573 20 20 20 20 20 20 22 42 45 47 49 4e 22 2c 20 20        "BEGIN",  
2a574 20 20 20 20 20 20 20 22 54 52 41 4e 53 41 43 54         "TRANSACT
2a575 49 4f 4e 22 2c 20 20 20 22 44 45 46 45 52 52 45  ION",   "DEFERRE
2a576 44 22 2c 20 20 20 20 0a 20 20 22 49 4d 4d 45 44  D",    .  "IMMED
2a577 49 41 54 45 22 2c 20 20 20 20 20 22 45 58 43 4c  IATE",     "EXCL
2a578 55 53 49 56 45 22 2c 20 20 20 20 20 22 43 4f 4d  USIVE",     "COM
2a579 4d 49 54 22 2c 20 20 20 20 20 20 20 20 22 45 4e  MIT",        "EN
2a57a 44 22 2c 20 20 20 20 20 20 20 20 20 0a 20 20 22  D",         .  "
2a57b 52 4f 4c 4c 42 41 43 4b 22 2c 20 20 20 20 20 20  ROLLBACK",      
2a57c 22 43 52 45 41 54 45 22 2c 20 20 20 20 20 20 20  "CREATE",       
2a57d 20 22 54 41 42 4c 45 22 2c 20 20 20 20 20 20 20   "TABLE",       
2a57e 20 20 22 49 46 22 2c 20 20 20 20 20 20 20 20 20    "IF",         
2a57f 20 0a 20 20 22 4e 4f 54 22 2c 20 20 20 20 20 20   .  "NOT",      
2a580 20 20 20 20 20 22 45 58 49 53 54 53 22 2c 20 20       "EXISTS",  
2a581 20 20 20 20 20 20 22 54 45 4d 50 22 2c 20 20 20        "TEMP",   
2a582 20 20 20 20 20 20 20 22 4c 50 22 2c 20 20 20 20         "LP",    
2a583 20 20 20 20 20 20 0a 20 20 22 52 50 22 2c 20 20        .  "RP",  
2a584 20 20 20 20 20 20 20 20 20 20 22 41 53 22 2c 20            "AS", 
2a585 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4d 4d             "COMM
2a586 41 22 2c 20 20 20 20 20 20 20 20 20 22 49 44 22  A",         "ID"
2a587 2c 20 20 20 20 20 20 20 20 20 20 0a 20 20 22 41  ,          .  "A
2a588 42 4f 52 54 22 2c 20 20 20 20 20 20 20 20 20 22  BORT",         "
2a589 41 46 54 45 52 22 2c 20 20 20 20 20 20 20 20 20  AFTER",         
2a58a 22 41 4e 41 4c 59 5a 45 22 2c 20 20 20 20 20 20  "ANALYZE",      
2a58b 20 22 41 53 43 22 2c 20 20 20 20 20 20 20 20 20   "ASC",         
2a58c 0a 20 20 22 41 54 54 41 43 48 22 2c 20 20 20 20  .  "ATTACH",    
2a58d 20 20 20 20 22 42 45 46 4f 52 45 22 2c 20 20 20      "BEFORE",   
2a58e 20 20 20 20 20 22 43 41 53 43 41 44 45 22 2c 20       "CASCADE", 
2a58f 20 20 20 20 20 20 22 43 41 53 54 22 2c 20 20 20        "CAST",   
2a590 20 20 20 20 20 0a 20 20 22 43 4f 4e 46 4c 49 43       .  "CONFLIC
2a591 54 22 2c 20 20 20 20 20 20 22 44 41 54 41 42 41  T",      "DATABA
2a592 53 45 22 2c 20 20 20 20 20 20 22 44 45 53 43 22  SE",      "DESC"
2a593 2c 20 20 20 20 20 20 20 20 20 20 22 44 45 54 41  ,          "DETA
2a594 43 48 22 2c 20 20 20 20 20 20 0a 20 20 22 45 41  CH",      .  "EA
2a595 43 48 22 2c 20 20 20 20 20 20 20 20 20 20 22 46  CH",          "F
2a596 41 49 4c 22 2c 20 20 20 20 20 20 20 20 20 20 22  AIL",          "
2a597 46 4f 52 22 2c 20 20 20 20 20 20 20 20 20 20 20  FOR",           
2a598 22 49 47 4e 4f 52 45 22 2c 20 20 20 20 20 20 0a  "IGNORE",      .
2a599 20 20 22 49 4e 49 54 49 41 4c 4c 59 22 2c 20 20    "INITIALLY",  
2a59a 20 20 20 22 49 4e 53 54 45 41 44 22 2c 20 20 20     "INSTEAD",   
2a59b 20 20 20 20 22 4c 49 4b 45 5f 4b 57 22 2c 20 20      "LIKE_KW",  
2a59c 20 20 20 20 20 22 4d 41 54 43 48 22 2c 20 20 20       "MATCH",   
2a59d 20 20 20 20 0a 20 20 22 4b 45 59 22 2c 20 20 20      .  "KEY",   
2a59e 20 20 20 20 20 20 20 20 22 4f 46 22 2c 20 20 20          "OF",   
2a59f 20 20 20 20 20 20 20 20 20 22 4f 46 46 53 45 54           "OFFSET
2a5a0 22 2c 20 20 20 20 20 20 20 20 22 50 52 41 47 4d  ",        "PRAGM
2a5a1 41 22 2c 20 20 20 20 20 20 0a 20 20 22 52 41 49  A",      .  "RAI
2a5a2 53 45 22 2c 20 20 20 20 20 20 20 20 20 22 52 45  SE",         "RE
2a5a3 50 4c 41 43 45 22 2c 20 20 20 20 20 20 20 22 52  PLACE",       "R
2a5a4 45 53 54 52 49 43 54 22 2c 20 20 20 20 20 20 22  ESTRICT",      "
2a5a5 52 4f 57 22 2c 20 20 20 20 20 20 20 20 20 0a 20  ROW",         . 
2a5a6 20 22 54 52 49 47 47 45 52 22 2c 20 20 20 20 20   "TRIGGER",     
2a5a7 20 20 22 56 41 43 55 55 4d 22 2c 20 20 20 20 20    "VACUUM",     
2a5a8 20 20 20 22 56 49 45 57 22 2c 20 20 20 20 20 20     "VIEW",      
2a5a9 20 20 20 20 22 56 49 52 54 55 41 4c 22 2c 20 20      "VIRTUAL",  
2a5aa 20 20 20 0a 20 20 22 52 45 49 4e 44 45 58 22 2c     .  "REINDEX",
2a5ab 20 20 20 20 20 20 20 22 52 45 4e 41 4d 45 22 2c         "RENAME",
2a5ac 20 20 20 20 20 20 20 20 22 43 54 49 4d 45 5f 4b          "CTIME_K
2a5ad 57 22 2c 20 20 20 20 20 20 22 41 4e 59 22 2c 20  W",      "ANY", 
2a5ae 20 20 20 20 20 20 20 20 0a 20 20 22 4f 52 22 2c          .  "OR",
2a5af 20 20 20 20 20 20 20 20 20 20 20 20 22 41 4e 44              "AND
2a5b0 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 49 53  ",           "IS
2a5b1 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22 42  ",            "B
2a5b2 45 54 57 45 45 4e 22 2c 20 20 20 20 20 0a 20 20  ETWEEN",     .  
2a5b3 22 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 20  "IN",           
2a5b4 20 22 49 53 4e 55 4c 4c 22 2c 20 20 20 20 20 20   "ISNULL",      
2a5b5 20 20 22 4e 4f 54 4e 55 4c 4c 22 2c 20 20 20 20    "NOTNULL",    
2a5b6 20 20 20 22 4e 45 22 2c 20 20 20 20 20 20 20 20     "NE",        
2a5b7 20 20 0a 20 20 22 45 51 22 2c 20 20 20 20 20 20    .  "EQ",      
2a5b8 20 20 20 20 20 20 22 47 54 22 2c 20 20 20 20 20        "GT",     
2a5b9 20 20 20 20 20 20 20 22 4c 45 22 2c 20 20 20 20         "LE",    
2a5ba 20 20 20 20 20 20 20 20 22 4c 54 22 2c 20 20 20          "LT",   
2a5bb 20 20 20 20 20 20 20 0a 20 20 22 47 45 22 2c 20         .  "GE", 
2a5bc 20 20 20 20 20 20 20 20 20 20 20 22 45 53 43 41             "ESCA
2a5bd 50 45 22 2c 20 20 20 20 20 20 20 20 22 42 49 54  PE",        "BIT
2a5be 41 4e 44 22 2c 20 20 20 20 20 20 20 20 22 42 49  AND",        "BI
2a5bf 54 4f 52 22 2c 20 20 20 20 20 20 20 0a 20 20 22  TOR",       .  "
2a5c0 4c 53 48 49 46 54 22 2c 20 20 20 20 20 20 20 20  LSHIFT",        
2a5c1 22 52 53 48 49 46 54 22 2c 20 20 20 20 20 20 20  "RSHIFT",       
2a5c2 20 22 50 4c 55 53 22 2c 20 20 20 20 20 20 20 20   "PLUS",        
2a5c3 20 20 22 4d 49 4e 55 53 22 2c 20 20 20 20 20 20    "MINUS",      
2a5c4 20 0a 20 20 22 53 54 41 52 22 2c 20 20 20 20 20   .  "STAR",     
2a5c5 20 20 20 20 20 22 53 4c 41 53 48 22 2c 20 20 20       "SLASH",   
2a5c6 20 20 20 20 20 20 22 52 45 4d 22 2c 20 20 20 20        "REM",    
2a5c7 20 20 20 20 20 20 20 22 43 4f 4e 43 41 54 22 2c         "CONCAT",
2a5c8 20 20 20 20 20 20 0a 20 20 22 43 4f 4c 4c 41 54        .  "COLLAT
2a5c9 45 22 2c 20 20 20 20 20 20 20 22 55 4d 49 4e 55  E",       "UMINU
2a5ca 53 22 2c 20 20 20 20 20 20 20 20 22 55 50 4c 55  S",        "UPLU
2a5cb 53 22 2c 20 20 20 20 20 20 20 20 20 22 42 49 54  S",         "BIT
2a5cc 4e 4f 54 22 2c 20 20 20 20 20 20 0a 20 20 22 53  NOT",      .  "S
2a5cd 54 52 49 4e 47 22 2c 20 20 20 20 20 20 20 20 22  TRING",        "
2a5ce 4a 4f 49 4e 5f 4b 57 22 2c 20 20 20 20 20 20 20  JOIN_KW",       
2a5cf 22 43 4f 4e 53 54 52 41 49 4e 54 22 2c 20 20 20  "CONSTRAINT",   
2a5d0 20 22 44 45 46 41 55 4c 54 22 2c 20 20 20 20 20   "DEFAULT",     
2a5d1 0a 20 20 22 4e 55 4c 4c 22 2c 20 20 20 20 20 20  .  "NULL",      
2a5d2 20 20 20 20 22 50 52 49 4d 41 52 59 22 2c 20 20      "PRIMARY",  
2a5d3 20 20 20 20 20 22 55 4e 49 51 55 45 22 2c 20 20       "UNIQUE",  
2a5d4 20 20 20 20 20 20 22 43 48 45 43 4b 22 2c 20 20        "CHECK",  
2a5d5 20 20 20 20 20 0a 20 20 22 52 45 46 45 52 45 4e       .  "REFEREN
2a5d6 43 45 53 22 2c 20 20 20 20 22 41 55 54 4f 49 4e  CES",    "AUTOIN
2a5d7 43 52 22 2c 20 20 20 20 20 20 22 4f 4e 22 2c 20  CR",      "ON", 
2a5d8 20 20 20 20 20 20 20 20 20 20 20 22 44 45 4c 45             "DELE
2a5d9 54 45 22 2c 20 20 20 20 20 20 0a 20 20 22 55 50  TE",      .  "UP
2a5da 44 41 54 45 22 2c 20 20 20 20 20 20 20 20 22 49  DATE",        "I
2a5db 4e 53 45 52 54 22 2c 20 20 20 20 20 20 20 20 22  NSERT",        "
2a5dc 53 45 54 22 2c 20 20 20 20 20 20 20 20 20 20 20  SET",           
2a5dd 22 44 45 46 45 52 52 41 42 4c 45 22 2c 20 20 0a  "DEFERRABLE",  .
2a5de 20 20 22 46 4f 52 45 49 47 4e 22 2c 20 20 20 20    "FOREIGN",    
2a5df 20 20 20 22 44 52 4f 50 22 2c 20 20 20 20 20 20     "DROP",      
2a5e0 20 20 20 20 22 55 4e 49 4f 4e 22 2c 20 20 20 20      "UNION",    
2a5e1 20 20 20 20 20 22 41 4c 4c 22 2c 20 20 20 20 20       "ALL",     
2a5e2 20 20 20 20 0a 20 20 22 45 58 43 45 50 54 22 2c      .  "EXCEPT",
2a5e3 20 20 20 20 20 20 20 20 22 49 4e 54 45 52 53 45          "INTERSE
2a5e4 43 54 22 2c 20 20 20 20 20 22 53 45 4c 45 43 54  CT",     "SELECT
2a5e5 22 2c 20 20 20 20 20 20 20 20 22 44 49 53 54 49  ",        "DISTI
2a5e6 4e 43 54 22 2c 20 20 20 20 0a 20 20 22 44 4f 54  NCT",    .  "DOT
2a5e7 22 2c 20 20 20 20 20 20 20 20 20 20 20 22 46 52  ",           "FR
2a5e8 4f 4d 22 2c 20 20 20 20 20 20 20 20 20 20 22 4a  OM",          "J
2a5e9 4f 49 4e 22 2c 20 20 20 20 20 20 20 20 20 20 22  OIN",          "
2a5ea 49 4e 44 45 58 45 44 22 2c 20 20 20 20 20 0a 20  INDEXED",     . 
2a5eb 20 22 42 59 22 2c 20 20 20 20 20 20 20 20 20 20   "BY",          
2a5ec 20 20 22 55 53 49 4e 47 22 2c 20 20 20 20 20 20    "USING",      
2a5ed 20 20 20 22 4f 52 44 45 52 22 2c 20 20 20 20 20     "ORDER",     
2a5ee 20 20 20 20 22 47 52 4f 55 50 22 2c 20 20 20 20      "GROUP",    
2a5ef 20 20 20 0a 20 20 22 48 41 56 49 4e 47 22 2c 20     .  "HAVING", 
2a5f0 20 20 20 20 20 20 20 22 4c 49 4d 49 54 22 2c 20         "LIMIT", 
2a5f1 20 20 20 20 20 20 20 20 22 57 48 45 52 45 22 2c          "WHERE",
2a5f2 20 20 20 20 20 20 20 20 20 22 49 4e 54 4f 22 2c           "INTO",
2a5f3 20 20 20 20 20 20 20 20 0a 20 20 22 56 41 4c 55          .  "VALU
2a5f4 45 53 22 2c 20 20 20 20 20 20 20 20 22 49 4e 54  ES",        "INT
2a5f5 45 47 45 52 22 2c 20 20 20 20 20 20 20 22 46 4c  EGER",       "FL
2a5f6 4f 41 54 22 2c 20 20 20 20 20 20 20 20 20 22 42  OAT",         "B
2a5f7 4c 4f 42 22 2c 20 20 20 20 20 20 20 20 0a 20 20  LOB",        .  
2a5f8 22 52 45 47 49 53 54 45 52 22 2c 20 20 20 20 20  "REGISTER",     
2a5f9 20 22 56 41 52 49 41 42 4c 45 22 2c 20 20 20 20   "VARIABLE",    
2a5fa 20 20 22 43 41 53 45 22 2c 20 20 20 20 20 20 20    "CASE",       
2a5fb 20 20 20 22 57 48 45 4e 22 2c 20 20 20 20 20 20     "WHEN",      
2a5fc 20 20 0a 20 20 22 54 48 45 4e 22 2c 20 20 20 20    .  "THEN",    
2a5fd 20 20 20 20 20 20 22 45 4c 53 45 22 2c 20 20 20        "ELSE",   
2a5fe 20 20 20 20 20 20 20 22 49 4e 44 45 58 22 2c 20         "INDEX", 
2a5ff 20 20 20 20 20 20 20 20 22 41 4c 54 45 52 22 2c          "ALTER",
2a600 20 20 20 20 20 20 20 0a 20 20 22 54 4f 22 2c 20         .  "TO", 
2a601 20 20 20 20 20 20 20 20 20 20 20 22 41 44 44 22             "ADD"
2a602 2c 20 20 20 20 20 20 20 20 20 20 20 22 43 4f 4c  ,           "COL
2a603 55 4d 4e 4b 57 22 2c 20 20 20 20 20 20 22 65 72  UMNKW",      "er
2a604 72 6f 72 22 2c 20 20 20 20 20 20 20 0a 20 20 22  ror",       .  "
2a605 69 6e 70 75 74 22 2c 20 20 20 20 20 20 20 20 20  input",         
2a606 22 63 6d 64 6c 69 73 74 22 2c 20 20 20 20 20 20  "cmdlist",      
2a607 20 22 65 63 6d 64 22 2c 20 20 20 20 20 20 20 20   "ecmd",        
2a608 20 20 22 65 78 70 6c 61 69 6e 22 2c 20 20 20 20    "explain",    
2a609 20 0a 20 20 22 63 6d 64 78 22 2c 20 20 20 20 20   .  "cmdx",     
2a60a 20 20 20 20 20 22 63 6d 64 22 2c 20 20 20 20 20       "cmd",     
2a60b 20 20 20 20 20 20 22 74 72 61 6e 73 74 79 70 65        "transtype
2a60c 22 2c 20 20 20 20 20 22 74 72 61 6e 73 5f 6f 70  ",     "trans_op
2a60d 74 22 2c 20 20 20 0a 20 20 22 6e 6d 22 2c 20 20  t",   .  "nm",  
2a60e 20 20 20 20 20 20 20 20 20 20 22 63 72 65 61 74            "creat
2a60f 65 5f 74 61 62 6c 65 22 2c 20 20 22 63 72 65 61  e_table",  "crea
2a610 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 22 2c 20  te_table_args", 
2a611 20 22 74 65 6d 70 22 2c 20 20 20 20 20 20 20 20   "temp",        
2a612 0a 20 20 22 69 66 6e 6f 74 65 78 69 73 74 73 22  .  "ifnotexists"
2a613 2c 20 20 20 22 64 62 6e 6d 22 2c 20 20 20 20 20  ,   "dbnm",     
2a614 20 20 20 20 20 22 63 6f 6c 75 6d 6e 6c 69 73 74       "columnlist
2a615 22 2c 20 20 20 20 22 63 6f 6e 73 6c 69 73 74 5f  ",    "conslist_
2a616 6f 70 74 22 2c 0a 20 20 22 73 65 6c 65 63 74 22  opt",.  "select"
2a617 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d 6e  ,        "column
2a618 22 2c 20 20 20 20 20 20 20 20 22 63 6f 6c 75 6d  ",        "colum
2a619 6e 69 64 22 2c 20 20 20 20 20 20 22 74 79 70 65  nid",      "type
2a61a 22 2c 20 20 20 20 20 20 20 20 0a 20 20 22 63 61  ",        .  "ca
2a61b 72 67 6c 69 73 74 22 2c 20 20 20 20 20 20 22 69  rglist",      "i
2a61c 64 22 2c 20 20 20 20 20 20 20 20 20 20 20 20 22  d",            "
2a61d 69 64 73 22 2c 20 20 20 20 20 20 20 20 20 20 20  ids",           
2a61e 22 74 79 70 65 74 6f 6b 65 6e 22 2c 20 20 20 0a  "typetoken",   .
2a61f 20 20 22 74 79 70 65 6e 61 6d 65 22 2c 20 20 20    "typename",   
2a620 20 20 20 22 73 69 67 6e 65 64 22 2c 20 20 20 20     "signed",    
2a621 20 20 20 20 22 70 6c 75 73 5f 6e 75 6d 22 2c 20      "plus_num", 
2a622 20 20 20 20 20 22 6d 69 6e 75 73 5f 6e 75 6d 22       "minus_num"
2a623 2c 20 20 20 0a 20 20 22 63 61 72 67 22 2c 20 20  ,   .  "carg",  
2a624 20 20 20 20 20 20 20 20 22 63 63 6f 6e 73 22 2c          "ccons",
2a625 20 20 20 20 20 20 20 20 20 22 74 65 72 6d 22 2c           "term",
2a626 20 20 20 20 20 20 20 20 20 20 22 65 78 70 72 22            "expr"
2a627 2c 20 20 20 20 20 20 20 20 0a 20 20 22 6f 6e 63  ,        .  "onc
2a628 6f 6e 66 22 2c 20 20 20 20 20 20 20 20 22 73 6f  onf",        "so
2a629 72 74 6f 72 64 65 72 22 2c 20 20 20 20 20 22 61  rtorder",     "a
2a62a 75 74 6f 69 6e 63 22 2c 20 20 20 20 20 20 20 22  utoinc",       "
2a62b 69 64 78 6c 69 73 74 5f 6f 70 74 22 2c 20 0a 20  idxlist_opt", . 
2a62c 20 22 72 65 66 61 72 67 73 22 2c 20 20 20 20 20   "refargs",     
2a62d 20 20 22 64 65 66 65 72 5f 73 75 62 63 6c 61 75    "defer_subclau
2a62e 73 65 22 2c 20 20 22 72 65 66 61 72 67 22 2c 20  se",  "refarg", 
2a62f 20 20 20 20 20 20 20 22 72 65 66 61 63 74 22 2c         "refact",
2a630 20 20 20 20 20 20 0a 20 20 22 69 6e 69 74 5f 64        .  "init_d
2a631 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2a632 22 2c 20 20 22 63 6f 6e 73 6c 69 73 74 22 2c 20  ",  "conslist", 
2a633 20 20 20 20 20 22 74 63 6f 6e 73 22 2c 20 20 20       "tcons",   
2a634 20 20 20 20 20 20 22 69 64 78 6c 69 73 74 22 2c        "idxlist",
2a635 20 20 20 20 20 0a 20 20 22 64 65 66 65 72 5f 73       .  "defer_s
2a636 75 62 63 6c 61 75 73 65 5f 6f 70 74 22 2c 20 20  ubclause_opt",  
2a637 22 6f 72 63 6f 6e 66 22 2c 20 20 20 20 20 20 20  "orconf",       
2a638 20 22 72 65 73 6f 6c 76 65 74 79 70 65 22 2c 20   "resolvetype", 
2a639 20 20 22 72 61 69 73 65 74 79 70 65 22 2c 20 20    "raisetype",  
2a63a 20 0a 20 20 22 69 66 65 78 69 73 74 73 22 2c 20   .  "ifexists", 
2a63b 20 20 20 20 20 22 66 75 6c 6c 6e 61 6d 65 22 2c       "fullname",
2a63c 20 20 20 20 20 20 22 6f 6e 65 73 65 6c 65 63 74        "oneselect
2a63d 22 2c 20 20 20 20 20 22 6d 75 6c 74 69 73 65 6c  ",     "multisel
2a63e 65 63 74 5f 6f 70 22 2c 0a 20 20 22 64 69 73 74  ect_op",.  "dist
2a63f 69 6e 63 74 22 2c 20 20 20 20 20 20 22 73 65 6c  inct",      "sel
2a640 63 6f 6c 6c 69 73 74 22 2c 20 20 20 20 22 66 72  collist",    "fr
2a641 6f 6d 22 2c 20 20 20 20 20 20 20 20 20 20 22 77  om",          "w
2a642 68 65 72 65 5f 6f 70 74 22 2c 20 20 20 0a 20 20  here_opt",   .  
2a643 22 67 72 6f 75 70 62 79 5f 6f 70 74 22 2c 20 20  "groupby_opt",  
2a644 20 22 68 61 76 69 6e 67 5f 6f 70 74 22 2c 20 20   "having_opt",  
2a645 20 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 22 2c    "orderby_opt",
2a646 20 20 20 22 6c 69 6d 69 74 5f 6f 70 74 22 2c 20     "limit_opt", 
2a647 20 20 0a 20 20 22 73 63 6c 70 22 2c 20 20 20 20    .  "sclp",    
2a648 20 20 20 20 20 20 22 61 73 22 2c 20 20 20 20 20        "as",     
2a649 20 20 20 20 20 20 20 22 73 65 6c 74 61 62 6c 69         "seltabli
2a64a 73 74 22 2c 20 20 20 20 22 73 74 6c 5f 70 72 65  st",    "stl_pre
2a64b 66 69 78 22 2c 20 20 0a 20 20 22 6a 6f 69 6e 6f  fix",  .  "joino
2a64c 70 22 2c 20 20 20 20 20 20 20 20 22 69 6e 64 65  p",        "inde
2a64d 78 65 64 5f 6f 70 74 22 2c 20 20 20 22 6f 6e 5f  xed_opt",   "on_
2a64e 6f 70 74 22 2c 20 20 20 20 20 20 20 20 22 75 73  opt",        "us
2a64f 69 6e 67 5f 6f 70 74 22 2c 20 20 20 0a 20 20 22  ing_opt",   .  "
2a650 73 65 6c 74 61 62 6c 69 73 74 5f 70 61 72 65 6e  seltablist_paren
2a651 22 2c 20 20 22 6a 6f 69 6e 6f 70 32 22 2c 20 20  ",  "joinop2",  
2a652 20 20 20 20 20 22 69 6e 73 63 6f 6c 6c 69 73 74       "inscollist
2a653 22 2c 20 20 20 20 22 73 6f 72 74 6c 69 73 74 22  ",    "sortlist"
2a654 2c 20 20 20 20 0a 20 20 22 73 6f 72 74 69 74 65  ,    .  "sortite
2a655 6d 22 2c 20 20 20 20 20 20 22 6e 65 78 70 72 6c  m",      "nexprl
2a656 69 73 74 22 2c 20 20 20 20 20 22 73 65 74 6c 69  ist",     "setli
2a657 73 74 22 2c 20 20 20 20 20 20 20 22 69 6e 73 65  st",       "inse
2a658 72 74 5f 63 6d 64 22 2c 20 20 0a 20 20 22 69 6e  rt_cmd",  .  "in
2a659 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 22 2c 20 20  scollist_opt",  
2a65a 22 69 74 65 6d 6c 69 73 74 22 2c 20 20 20 20 20  "itemlist",     
2a65b 20 22 65 78 70 72 6c 69 73 74 22 2c 20 20 20 20   "exprlist",    
2a65c 20 20 22 6c 69 6b 65 6f 70 22 2c 20 20 20 20 20    "likeop",     
2a65d 20 0a 20 20 22 65 73 63 61 70 65 22 2c 20 20 20   .  "escape",   
2a65e 20 20 20 20 20 22 62 65 74 77 65 65 6e 5f 6f 70       "between_op
2a65f 22 2c 20 20 20 20 22 69 6e 5f 6f 70 22 2c 20 20  ",    "in_op",  
2a660 20 20 20 20 20 20 20 22 63 61 73 65 5f 6f 70 65         "case_ope
2a661 72 61 6e 64 22 2c 0a 20 20 22 63 61 73 65 5f 65  rand",.  "case_e
2a662 78 70 72 6c 69 73 74 22 2c 20 20 22 63 61 73 65  xprlist",  "case
2a663 5f 65 6c 73 65 22 2c 20 20 20 20 20 22 75 6e 69  _else",     "uni
2a664 71 75 65 66 6c 61 67 22 2c 20 20 20 20 22 63 6f  queflag",    "co
2a665 6c 6c 61 74 65 22 2c 20 20 20 20 20 0a 20 20 22  llate",     .  "
2a666 6e 6d 6e 75 6d 22 2c 20 20 20 20 20 20 20 20 20  nmnum",         
2a667 22 70 6c 75 73 5f 6f 70 74 22 2c 20 20 20 20 20  "plus_opt",     
2a668 20 22 6e 75 6d 62 65 72 22 2c 20 20 20 20 20 20   "number",      
2a669 20 20 22 74 72 69 67 67 65 72 5f 64 65 63 6c 22    "trigger_decl"
2a66a 2c 0a 20 20 22 74 72 69 67 67 65 72 5f 63 6d 64  ,.  "trigger_cmd
2a66b 5f 6c 69 73 74 22 2c 20 20 22 74 72 69 67 67 65  _list",  "trigge
2a66c 72 5f 74 69 6d 65 22 2c 20 20 22 74 72 69 67 67  r_time",  "trigg
2a66d 65 72 5f 65 76 65 6e 74 22 2c 20 20 22 66 6f 72  er_event",  "for
2a66e 65 61 63 68 5f 63 6c 61 75 73 65 22 2c 0a 20 20  each_clause",.  
2a66f 22 77 68 65 6e 5f 63 6c 61 75 73 65 22 2c 20 20  "when_clause",  
2a670 20 22 74 72 69 67 67 65 72 5f 63 6d 64 22 2c 20   "trigger_cmd", 
2a671 20 20 22 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f    "database_kw_o
2a672 70 74 22 2c 20 20 22 6b 65 79 5f 6f 70 74 22 2c  pt",  "key_opt",
2a673 20 20 20 20 20 0a 20 20 22 61 64 64 5f 63 6f 6c       .  "add_col
2a674 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 22 2c 20 20  umn_fullname",  
2a675 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 22 2c 20  "kwcolumn_opt", 
2a676 20 22 63 72 65 61 74 65 5f 76 74 61 62 22 2c 20   "create_vtab", 
2a677 20 20 22 76 74 61 62 61 72 67 6c 69 73 74 22 2c    "vtabarglist",
2a678 20 0a 20 20 22 76 74 61 62 61 72 67 22 2c 20 20   .  "vtabarg",  
2a679 20 20 20 20 20 22 76 74 61 62 61 72 67 74 6f 6b       "vtabargtok
2a67a 65 6e 22 2c 20 20 22 6c 70 22 2c 20 20 20 20 20  en",  "lp",     
2a67b 20 20 20 20 20 20 20 22 61 6e 79 6c 69 73 74 22         "anylist"
2a67c 2c 20 20 20 20 20 0a 7d 3b 0a 23 65 6e 64 69 66  ,     .};.#endif
2a67d 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 23   /* NDEBUG */..#
2a67e 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 2f 2a  ifndef NDEBUG./*
2a67f 20 46 6f 72 20 74 72 61 63 69 6e 67 20 72 65 64   For tracing red
2a680 75 63 65 20 61 63 74 69 6f 6e 73 2c 20 74 68 65  uce actions, the
2a681 20 6e 61 6d 65 73 20 6f 66 20 61 6c 6c 20 72 75   names of all ru
2a682 6c 65 73 20 61 72 65 20 72 65 71 75 69 72 65 64  les are required
2a683 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  ..*/.static cons
2a684 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 79 79  t char *const yy
2a685 52 75 6c 65 4e 61 6d 65 5b 5d 20 3d 20 7b 0a 20  RuleName[] = {. 
2a686 2f 2a 20 20 20 30 20 2a 2f 20 22 69 6e 70 75 74  /*   0 */ "input
2a687 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 22 2c 0a 20   ::= cmdlist",. 
2a688 2f 2a 20 20 20 31 20 2a 2f 20 22 63 6d 64 6c 69  /*   1 */ "cmdli
2a689 73 74 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 65  st ::= cmdlist e
2a68a 63 6d 64 22 2c 0a 20 2f 2a 20 20 20 32 20 2a 2f  cmd",. /*   2 */
2a68b 20 22 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 65 63   "cmdlist ::= ec
2a68c 6d 64 22 2c 0a 20 2f 2a 20 20 20 33 20 2a 2f 20  md",. /*   3 */ 
2a68d 22 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49 22 2c  "ecmd ::= SEMI",
2a68e 0a 20 2f 2a 20 20 20 34 20 2a 2f 20 22 65 63 6d  . /*   4 */ "ecm
2a68f 64 20 3a 3a 3d 20 65 78 70 6c 61 69 6e 20 63 6d  d ::= explain cm
2a690 64 78 20 53 45 4d 49 22 2c 0a 20 2f 2a 20 20 20  dx SEMI",. /*   
2a691 35 20 2a 2f 20 22 65 78 70 6c 61 69 6e 20 3a 3a  5 */ "explain ::
2a692 3d 22 2c 0a 20 2f 2a 20 20 20 36 20 2a 2f 20 22  =",. /*   6 */ "
2a693 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c  explain ::= EXPL
2a694 41 49 4e 22 2c 0a 20 2f 2a 20 20 20 37 20 2a 2f  AIN",. /*   7 */
2a695 20 22 65 78 70 6c 61 69 6e 20 3a 3a 3d 20 45 58   "explain ::= EX
2a696 50 4c 41 49 4e 20 51 55 45 52 59 20 50 4c 41 4e  PLAIN QUERY PLAN
2a697 22 2c 0a 20 2f 2a 20 20 20 38 20 2a 2f 20 22 63  ",. /*   8 */ "c
2a698 6d 64 78 20 3a 3a 3d 20 63 6d 64 22 2c 0a 20 2f  mdx ::= cmd",. /
2a699 2a 20 20 20 39 20 2a 2f 20 22 63 6d 64 20 3a 3a  *   9 */ "cmd ::
2a69a 3d 20 42 45 47 49 4e 20 74 72 61 6e 73 74 79 70  = BEGIN transtyp
2a69b 65 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f  e trans_opt",. /
2a69c 2a 20 20 31 30 20 2a 2f 20 22 74 72 61 6e 73 5f  *  10 */ "trans_
2a69d 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31  opt ::=",. /*  1
2a69e 31 20 2a 2f 20 22 74 72 61 6e 73 5f 6f 70 74 20  1 */ "trans_opt 
2a69f 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e 22  ::= TRANSACTION"
2a6a0 2c 0a 20 2f 2a 20 20 31 32 20 2a 2f 20 22 74 72  ,. /*  12 */ "tr
2a6a1 61 6e 73 5f 6f 70 74 20 3a 3a 3d 20 54 52 41 4e  ans_opt ::= TRAN
2a6a2 53 41 43 54 49 4f 4e 20 6e 6d 22 2c 0a 20 2f 2a  SACTION nm",. /*
2a6a3 20 20 31 33 20 2a 2f 20 22 74 72 61 6e 73 74 79    13 */ "transty
2a6a4 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 31 34  pe ::=",. /*  14
2a6a5 20 2a 2f 20 22 74 72 61 6e 73 74 79 70 65 20 3a   */ "transtype :
2a6a6 3a 3d 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f  := DEFERRED",. /
2a6a7 2a 20 20 31 35 20 2a 2f 20 22 74 72 61 6e 73 74  *  15 */ "transt
2a6a8 79 70 65 20 3a 3a 3d 20 49 4d 4d 45 44 49 41 54  ype ::= IMMEDIAT
2a6a9 45 22 2c 0a 20 2f 2a 20 20 31 36 20 2a 2f 20 22  E",. /*  16 */ "
2a6aa 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45 58  transtype ::= EX
2a6ab 43 4c 55 53 49 56 45 22 2c 0a 20 2f 2a 20 20 31  CLUSIVE",. /*  1
2a6ac 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 43 4f  7 */ "cmd ::= CO
2a6ad 4d 4d 49 54 20 74 72 61 6e 73 5f 6f 70 74 22 2c  MMIT trans_opt",
2a6ae 0a 20 2f 2a 20 20 31 38 20 2a 2f 20 22 63 6d 64  . /*  18 */ "cmd
2a6af 20 3a 3a 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f   ::= END trans_o
2a6b0 70 74 22 2c 0a 20 2f 2a 20 20 31 39 20 2a 2f 20  pt",. /*  19 */ 
2a6b1 22 63 6d 64 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43  "cmd ::= ROLLBAC
2a6b2 4b 20 74 72 61 6e 73 5f 6f 70 74 22 2c 0a 20 2f  K trans_opt",. /
2a6b3 2a 20 20 32 30 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  20 */ "cmd ::
2a6b4 3d 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63  = create_table c
2a6b5 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73  reate_table_args
2a6b6 22 2c 0a 20 2f 2a 20 20 32 31 20 2a 2f 20 22 63  ",. /*  21 */ "c
2a6b7 72 65 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20  reate_table ::= 
2a6b8 43 52 45 41 54 45 20 74 65 6d 70 20 54 41 42 4c  CREATE temp TABL
2a6b9 45 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d  E ifnotexists nm
2a6ba 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 20 32 32 20   dbnm",. /*  22 
2a6bb 2a 2f 20 22 69 66 6e 6f 74 65 78 69 73 74 73 20  */ "ifnotexists 
2a6bc 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 32 33 20 2a 2f  ::=",. /*  23 */
2a6bd 20 22 69 66 6e 6f 74 65 78 69 73 74 73 20 3a 3a   "ifnotexists ::
2a6be 3d 20 49 46 20 4e 4f 54 20 45 58 49 53 54 53 22  = IF NOT EXISTS"
2a6bf 2c 0a 20 2f 2a 20 20 32 34 20 2a 2f 20 22 74 65  ,. /*  24 */ "te
2a6c0 6d 70 20 3a 3a 3d 20 54 45 4d 50 22 2c 0a 20 2f  mp ::= TEMP",. /
2a6c1 2a 20 20 32 35 20 2a 2f 20 22 74 65 6d 70 20 3a  *  25 */ "temp :
2a6c2 3a 3d 22 2c 0a 20 2f 2a 20 20 32 36 20 2a 2f 20  :=",. /*  26 */ 
2a6c3 22 63 72 65 61 74 65 5f 74 61 62 6c 65 5f 61 72  "create_table_ar
2a6c4 67 73 20 3a 3a 3d 20 4c 50 20 63 6f 6c 75 6d 6e  gs ::= LP column
2a6c5 6c 69 73 74 20 63 6f 6e 73 6c 69 73 74 5f 6f 70  list conslist_op
2a6c6 74 20 52 50 22 2c 0a 20 2f 2a 20 20 32 37 20 2a  t RP",. /*  27 *
2a6c7 2f 20 22 63 72 65 61 74 65 5f 74 61 62 6c 65 5f  / "create_table_
2a6c8 61 72 67 73 20 3a 3a 3d 20 41 53 20 73 65 6c 65  args ::= AS sele
2a6c9 63 74 22 2c 0a 20 2f 2a 20 20 32 38 20 2a 2f 20  ct",. /*  28 */ 
2a6ca 22 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20  "columnlist ::= 
2a6cb 63 6f 6c 75 6d 6e 6c 69 73 74 20 43 4f 4d 4d 41  columnlist COMMA
2a6cc 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 20 32   column",. /*  2
2a6cd 39 20 2a 2f 20 22 63 6f 6c 75 6d 6e 6c 69 73 74  9 */ "columnlist
2a6ce 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 22 2c 0a 20 2f   ::= column",. /
2a6cf 2a 20 20 33 30 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  30 */ "column
2a6d0 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69 64 20 74 79   ::= columnid ty
2a6d1 70 65 20 63 61 72 67 6c 69 73 74 22 2c 0a 20 2f  pe carglist",. /
2a6d2 2a 20 20 33 31 20 2a 2f 20 22 63 6f 6c 75 6d 6e  *  31 */ "column
2a6d3 69 64 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20  id ::= nm",. /* 
2a6d4 20 33 32 20 2a 2f 20 22 69 64 20 3a 3a 3d 20 49   32 */ "id ::= I
2a6d5 44 22 2c 0a 20 2f 2a 20 20 33 33 20 2a 2f 20 22  D",. /*  33 */ "
2a6d6 69 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e  ids ::= ID|STRIN
2a6d7 47 22 2c 0a 20 2f 2a 20 20 33 34 20 2a 2f 20 22  G",. /*  34 */ "
2a6d8 6e 6d 20 3a 3a 3d 20 49 44 22 2c 0a 20 2f 2a 20  nm ::= ID",. /* 
2a6d9 20 33 35 20 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 53   35 */ "nm ::= S
2a6da 54 52 49 4e 47 22 2c 0a 20 2f 2a 20 20 33 36 20  TRING",. /*  36 
2a6db 2a 2f 20 22 6e 6d 20 3a 3a 3d 20 4a 4f 49 4e 5f  */ "nm ::= JOIN_
2a6dc 4b 57 22 2c 0a 20 2f 2a 20 20 33 37 20 2a 2f 20  KW",. /*  37 */ 
2a6dd 22 74 79 70 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  "type ::=",. /* 
2a6de 20 33 38 20 2a 2f 20 22 74 79 70 65 20 3a 3a 3d   38 */ "type ::=
2a6df 20 74 79 70 65 74 6f 6b 65 6e 22 2c 0a 20 2f 2a   typetoken",. /*
2a6e0 20 20 33 39 20 2a 2f 20 22 74 79 70 65 74 6f 6b    39 */ "typetok
2a6e1 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d 65 22  en ::= typename"
2a6e2 2c 0a 20 2f 2a 20 20 34 30 20 2a 2f 20 22 74 79  ,. /*  40 */ "ty
2a6e3 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65  petoken ::= type
2a6e4 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20 52  name LP signed R
2a6e5 50 22 2c 0a 20 2f 2a 20 20 34 31 20 2a 2f 20 22  P",. /*  41 */ "
2a6e6 74 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79  typetoken ::= ty
2a6e7 70 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64  pename LP signed
2a6e8 20 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50   COMMA signed RP
2a6e9 22 2c 0a 20 2f 2a 20 20 34 32 20 2a 2f 20 22 74  ",. /*  42 */ "t
2a6ea 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 69 64 73 22  ypename ::= ids"
2a6eb 2c 0a 20 2f 2a 20 20 34 33 20 2a 2f 20 22 74 79  ,. /*  43 */ "ty
2a6ec 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70 65 6e  pename ::= typen
2a6ed 61 6d 65 20 69 64 73 22 2c 0a 20 2f 2a 20 20 34  ame ids",. /*  4
2a6ee 34 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a 3a 3d  4 */ "signed ::=
2a6ef 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f 2a 20   plus_num",. /* 
2a6f0 20 34 35 20 2a 2f 20 22 73 69 67 6e 65 64 20 3a   45 */ "signed :
2a6f1 3a 3d 20 6d 69 6e 75 73 5f 6e 75 6d 22 2c 0a 20  := minus_num",. 
2a6f2 2f 2a 20 20 34 36 20 2a 2f 20 22 63 61 72 67 6c  /*  46 */ "cargl
2a6f3 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69 73 74  ist ::= carglist
2a6f4 20 63 61 72 67 22 2c 0a 20 2f 2a 20 20 34 37 20   carg",. /*  47 
2a6f5 2a 2f 20 22 63 61 72 67 6c 69 73 74 20 3a 3a 3d  */ "carglist ::=
2a6f6 22 2c 0a 20 2f 2a 20 20 34 38 20 2a 2f 20 22 63  ",. /*  48 */ "c
2a6f7 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49  arg ::= CONSTRAI
2a6f8 4e 54 20 6e 6d 20 63 63 6f 6e 73 22 2c 0a 20 2f  NT nm ccons",. /
2a6f9 2a 20 20 34 39 20 2a 2f 20 22 63 61 72 67 20 3a  *  49 */ "carg :
2a6fa 3a 3d 20 63 63 6f 6e 73 22 2c 0a 20 2f 2a 20 20  := ccons",. /*  
2a6fb 35 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  50 */ "ccons ::=
2a6fc 20 44 45 46 41 55 4c 54 20 74 65 72 6d 22 2c 0a   DEFAULT term",.
2a6fd 20 2f 2a 20 20 35 31 20 2a 2f 20 22 63 63 6f 6e   /*  51 */ "ccon
2a6fe 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4c 50  s ::= DEFAULT LP
2a6ff 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 20   expr RP",. /*  
2a700 35 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  52 */ "ccons ::=
2a701 20 44 45 46 41 55 4c 54 20 50 4c 55 53 20 74 65   DEFAULT PLUS te
2a702 72 6d 22 2c 0a 20 2f 2a 20 20 35 33 20 2a 2f 20  rm",. /*  53 */ 
2a703 22 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55  "ccons ::= DEFAU
2a704 4c 54 20 4d 49 4e 55 53 20 74 65 72 6d 22 2c 0a  LT MINUS term",.
2a705 20 2f 2a 20 20 35 34 20 2a 2f 20 22 63 63 6f 6e   /*  54 */ "ccon
2a706 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20 69 64  s ::= DEFAULT id
2a707 22 2c 0a 20 2f 2a 20 20 35 35 20 2a 2f 20 22 63  ",. /*  55 */ "c
2a708 63 6f 6e 73 20 3a 3a 3d 20 4e 55 4c 4c 20 6f 6e  cons ::= NULL on
2a709 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 35 36 20 2a  conf",. /*  56 *
2a70a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d 20 4e 4f 54  / "ccons ::= NOT
2a70b 20 4e 55 4c 4c 20 6f 6e 63 6f 6e 66 22 2c 0a 20   NULL onconf",. 
2a70c 2f 2a 20 20 35 37 20 2a 2f 20 22 63 63 6f 6e 73  /*  57 */ "ccons
2a70d 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b 45 59   ::= PRIMARY KEY
2a70e 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63 6f 6e   sortorder oncon
2a70f 66 20 61 75 74 6f 69 6e 63 22 2c 0a 20 2f 2a 20  f autoinc",. /* 
2a710 20 35 38 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a   58 */ "ccons ::
2a711 3d 20 55 4e 49 51 55 45 20 6f 6e 63 6f 6e 66 22  = UNIQUE onconf"
2a712 2c 0a 20 2f 2a 20 20 35 39 20 2a 2f 20 22 63 63  ,. /*  59 */ "cc
2a713 6f 6e 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50  ons ::= CHECK LP
2a714 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 20   expr RP",. /*  
2a715 36 30 20 2a 2f 20 22 63 63 6f 6e 73 20 3a 3a 3d  60 */ "ccons ::=
2a716 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69   REFERENCES nm i
2a717 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72  dxlist_opt refar
2a718 67 73 22 2c 0a 20 2f 2a 20 20 36 31 20 2a 2f 20  gs",. /*  61 */ 
2a719 22 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72  "ccons ::= defer
2a71a 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a 20 2f 2a  _subclause",. /*
2a71b 20 20 36 32 20 2a 2f 20 22 63 63 6f 6e 73 20 3a    62 */ "ccons :
2a71c 3a 3d 20 43 4f 4c 4c 41 54 45 20 69 64 73 22 2c  := COLLATE ids",
2a71d 0a 20 2f 2a 20 20 36 33 20 2a 2f 20 22 61 75 74  . /*  63 */ "aut
2a71e 6f 69 6e 63 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20  oinc ::=",. /*  
2a71f 36 34 20 2a 2f 20 22 61 75 74 6f 69 6e 63 20 3a  64 */ "autoinc :
2a720 3a 3d 20 41 55 54 4f 49 4e 43 52 22 2c 0a 20 2f  := AUTOINCR",. /
2a721 2a 20 20 36 35 20 2a 2f 20 22 72 65 66 61 72 67  *  65 */ "refarg
2a722 73 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 36 36 20  s ::=",. /*  66 
2a723 2a 2f 20 22 72 65 66 61 72 67 73 20 3a 3a 3d 20  */ "refargs ::= 
2a724 72 65 66 61 72 67 73 20 72 65 66 61 72 67 22 2c  refargs refarg",
2a725 0a 20 2f 2a 20 20 36 37 20 2a 2f 20 22 72 65 66  . /*  67 */ "ref
2a726 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48 20 6e 6d  arg ::= MATCH nm
2a727 22 2c 0a 20 2f 2a 20 20 36 38 20 2a 2f 20 22 72  ",. /*  68 */ "r
2a728 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45 4c  efarg ::= ON DEL
2a729 45 54 45 20 72 65 66 61 63 74 22 2c 0a 20 2f 2a  ETE refact",. /*
2a72a 20 20 36 39 20 2a 2f 20 22 72 65 66 61 72 67 20    69 */ "refarg 
2a72b 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20 72 65  ::= ON UPDATE re
2a72c 66 61 63 74 22 2c 0a 20 2f 2a 20 20 37 30 20 2a  fact",. /*  70 *
2a72d 2f 20 22 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e  / "refarg ::= ON
2a72e 20 49 4e 53 45 52 54 20 72 65 66 61 63 74 22 2c   INSERT refact",
2a72f 0a 20 2f 2a 20 20 37 31 20 2a 2f 20 22 72 65 66  . /*  71 */ "ref
2a730 61 63 74 20 3a 3a 3d 20 53 45 54 20 4e 55 4c 4c  act ::= SET NULL
2a731 22 2c 0a 20 2f 2a 20 20 37 32 20 2a 2f 20 22 72  ",. /*  72 */ "r
2a732 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20 44 45  efact ::= SET DE
2a733 46 41 55 4c 54 22 2c 0a 20 2f 2a 20 20 37 33 20  FAULT",. /*  73 
2a734 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20 43  */ "refact ::= C
2a735 41 53 43 41 44 45 22 2c 0a 20 2f 2a 20 20 37 34  ASCADE",. /*  74
2a736 20 2a 2f 20 22 72 65 66 61 63 74 20 3a 3a 3d 20   */ "refact ::= 
2a737 52 45 53 54 52 49 43 54 22 2c 0a 20 2f 2a 20 20  RESTRICT",. /*  
2a738 37 35 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62  75 */ "defer_sub
2a739 63 6c 61 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44  clause ::= NOT D
2a73a 45 46 45 52 52 41 42 4c 45 20 69 6e 69 74 5f 64  EFERRABLE init_d
2a73b 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74  eferred_pred_opt
2a73c 22 2c 0a 20 2f 2a 20 20 37 36 20 2a 2f 20 22 64  ",. /*  76 */ "d
2a73d 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 20 3a  efer_subclause :
2a73e 3a 3d 20 44 45 46 45 52 52 41 42 4c 45 20 69 6e  := DEFERRABLE in
2a73f 69 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64  it_deferred_pred
2a740 5f 6f 70 74 22 2c 0a 20 2f 2a 20 20 37 37 20 2a  _opt",. /*  77 *
2a741 2f 20 22 69 6e 69 74 5f 64 65 66 65 72 72 65 64  / "init_deferred
2a742 5f 70 72 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c 0a  _pred_opt ::=",.
2a743 20 2f 2a 20 20 37 38 20 2a 2f 20 22 69 6e 69 74   /*  78 */ "init
2a744 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f 6f  _deferred_pred_o
2a745 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c 59  pt ::= INITIALLY
2a746 20 44 45 46 45 52 52 45 44 22 2c 0a 20 2f 2a 20   DEFERRED",. /* 
2a747 20 37 39 20 2a 2f 20 22 69 6e 69 74 5f 64 65 66   79 */ "init_def
2a748 65 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a  erred_pred_opt :
2a749 3a 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d  := INITIALLY IMM
2a74a 45 44 49 41 54 45 22 2c 0a 20 2f 2a 20 20 38 30  EDIATE",. /*  80
2a74b 20 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70   */ "conslist_op
2a74c 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 38 31 20  t ::=",. /*  81 
2a74d 2a 2f 20 22 63 6f 6e 73 6c 69 73 74 5f 6f 70 74  */ "conslist_opt
2a74e 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c   ::= COMMA consl
2a74f 69 73 74 22 2c 0a 20 2f 2a 20 20 38 32 20 2a 2f  ist",. /*  82 */
2a750 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63   "conslist ::= c
2a751 6f 6e 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63  onslist COMMA tc
2a752 6f 6e 73 22 2c 0a 20 2f 2a 20 20 38 33 20 2a 2f  ons",. /*  83 */
2a753 20 22 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63   "conslist ::= c
2a754 6f 6e 73 6c 69 73 74 20 74 63 6f 6e 73 22 2c 0a  onslist tcons",.
2a755 20 2f 2a 20 20 38 34 20 2a 2f 20 22 63 6f 6e 73   /*  84 */ "cons
2a756 6c 69 73 74 20 3a 3a 3d 20 74 63 6f 6e 73 22 2c  list ::= tcons",
2a757 0a 20 2f 2a 20 20 38 35 20 2a 2f 20 22 74 63 6f  . /*  85 */ "tco
2a758 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49 4e  ns ::= CONSTRAIN
2a759 54 20 6e 6d 22 2c 0a 20 2f 2a 20 20 38 36 20 2a  T nm",. /*  86 *
2a75a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49  / "tcons ::= PRI
2a75b 4d 41 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c  MARY KEY LP idxl
2a75c 69 73 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f  ist autoinc RP o
2a75d 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20 20 38 37 20  nconf",. /*  87 
2a75e 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a 3d 20 55 4e  */ "tcons ::= UN
2a75f 49 51 55 45 20 4c 50 20 69 64 78 6c 69 73 74 20  IQUE LP idxlist 
2a760 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RP onconf",. /* 
2a761 20 38 38 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   88 */ "tcons ::
2a762 3d 20 43 48 45 43 4b 20 4c 50 20 65 78 70 72 20  = CHECK LP expr 
2a763 52 50 20 6f 6e 63 6f 6e 66 22 2c 0a 20 2f 2a 20  RP onconf",. /* 
2a764 20 38 39 20 2a 2f 20 22 74 63 6f 6e 73 20 3a 3a   89 */ "tcons ::
2a765 3d 20 46 4f 52 45 49 47 4e 20 4b 45 59 20 4c 50  = FOREIGN KEY LP
2a766 20 69 64 78 6c 69 73 74 20 52 50 20 52 45 46 45   idxlist RP REFE
2a767 52 45 4e 43 45 53 20 6e 6d 20 69 64 78 6c 69 73  RENCES nm idxlis
2a768 74 5f 6f 70 74 20 72 65 66 61 72 67 73 20 64 65  t_opt refargs de
2a769 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
2a76a 74 22 2c 0a 20 2f 2a 20 20 39 30 20 2a 2f 20 22  t",. /*  90 */ "
2a76b 64 65 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f  defer_subclause_
2a76c 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39  opt ::=",. /*  9
2a76d 31 20 2a 2f 20 22 64 65 66 65 72 5f 73 75 62 63  1 */ "defer_subc
2a76e 6c 61 75 73 65 5f 6f 70 74 20 3a 3a 3d 20 64 65  lause_opt ::= de
2a76f 66 65 72 5f 73 75 62 63 6c 61 75 73 65 22 2c 0a  fer_subclause",.
2a770 20 2f 2a 20 20 39 32 20 2a 2f 20 22 6f 6e 63 6f   /*  92 */ "onco
2a771 6e 66 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 20 39 33  nf ::=",. /*  93
2a772 20 2a 2f 20 22 6f 6e 63 6f 6e 66 20 3a 3a 3d 20   */ "onconf ::= 
2a773 4f 4e 20 43 4f 4e 46 4c 49 43 54 20 72 65 73 6f  ON CONFLICT reso
2a774 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 20 39  lvetype",. /*  9
2a775 34 20 2a 2f 20 22 6f 72 63 6f 6e 66 20 3a 3a 3d  4 */ "orconf ::=
2a776 22 2c 0a 20 2f 2a 20 20 39 35 20 2a 2f 20 22 6f  ",. /*  95 */ "o
2a777 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52 20 72 65 73  rconf ::= OR res
2a778 6f 6c 76 65 74 79 70 65 22 2c 0a 20 2f 2a 20 20  olvetype",. /*  
2a779 39 36 20 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79  96 */ "resolvety
2a77a 70 65 20 3a 3a 3d 20 72 61 69 73 65 74 79 70 65  pe ::= raisetype
2a77b 22 2c 0a 20 2f 2a 20 20 39 37 20 2a 2f 20 22 72  ",. /*  97 */ "r
2a77c 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a 3d 20 49  esolvetype ::= I
2a77d 47 4e 4f 52 45 22 2c 0a 20 2f 2a 20 20 39 38 20  GNORE",. /*  98 
2a77e 2a 2f 20 22 72 65 73 6f 6c 76 65 74 79 70 65 20  */ "resolvetype 
2a77f 3a 3a 3d 20 52 45 50 4c 41 43 45 22 2c 0a 20 2f  ::= REPLACE",. /
2a780 2a 20 20 39 39 20 2a 2f 20 22 63 6d 64 20 3a 3a  *  99 */ "cmd ::
2a781 3d 20 44 52 4f 50 20 54 41 42 4c 45 20 69 66 65  = DROP TABLE ife
2a782 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c  xists fullname",
2a783 0a 20 2f 2a 20 31 30 30 20 2a 2f 20 22 69 66 65  . /* 100 */ "ife
2a784 78 69 73 74 73 20 3a 3a 3d 20 49 46 20 45 58 49  xists ::= IF EXI
2a785 53 54 53 22 2c 0a 20 2f 2a 20 31 30 31 20 2a 2f  STS",. /* 101 */
2a786 20 22 69 66 65 78 69 73 74 73 20 3a 3a 3d 22 2c   "ifexists ::=",
2a787 0a 20 2f 2a 20 31 30 32 20 2a 2f 20 22 63 6d 64  . /* 102 */ "cmd
2a788 20 3a 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70   ::= CREATE temp
2a789 20 56 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74   VIEW ifnotexist
2a78a 73 20 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c  s nm dbnm AS sel
2a78b 65 63 74 22 2c 0a 20 2f 2a 20 31 30 33 20 2a 2f  ect",. /* 103 */
2a78c 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 56   "cmd ::= DROP V
2a78d 49 45 57 20 69 66 65 78 69 73 74 73 20 66 75 6c  IEW ifexists ful
2a78e 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 31 30 34 20  lname",. /* 104 
2a78f 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 73 65 6c 65  */ "cmd ::= sele
2a790 63 74 22 2c 0a 20 2f 2a 20 31 30 35 20 2a 2f 20  ct",. /* 105 */ 
2a791 22 73 65 6c 65 63 74 20 3a 3a 3d 20 6f 6e 65 73  "select ::= ones
2a792 65 6c 65 63 74 22 2c 0a 20 2f 2a 20 31 30 36 20  elect",. /* 106 
2a793 2a 2f 20 22 73 65 6c 65 63 74 20 3a 3a 3d 20 73  */ "select ::= s
2a794 65 6c 65 63 74 20 6d 75 6c 74 69 73 65 6c 65 63  elect multiselec
2a795 74 5f 6f 70 20 6f 6e 65 73 65 6c 65 63 74 22 2c  t_op oneselect",
2a796 0a 20 2f 2a 20 31 30 37 20 2a 2f 20 22 6d 75 6c  . /* 107 */ "mul
2a797 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d 20  tiselect_op ::= 
2a798 55 4e 49 4f 4e 22 2c 0a 20 2f 2a 20 31 30 38 20  UNION",. /* 108 
2a799 2a 2f 20 22 6d 75 6c 74 69 73 65 6c 65 63 74 5f  */ "multiselect_
2a79a 6f 70 20 3a 3a 3d 20 55 4e 49 4f 4e 20 41 4c 4c  op ::= UNION ALL
2a79b 22 2c 0a 20 2f 2a 20 31 30 39 20 2a 2f 20 22 6d  ",. /* 109 */ "m
2a79c 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
2a79d 3d 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53 45  = EXCEPT|INTERSE
2a79e 43 54 22 2c 0a 20 2f 2a 20 31 31 30 20 2a 2f 20  CT",. /* 110 */ 
2a79f 22 6f 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53  "oneselect ::= S
2a7a0 45 4c 45 43 54 20 64 69 73 74 69 6e 63 74 20 73  ELECT distinct s
2a7a1 65 6c 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77  elcollist from w
2a7a2 68 65 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79  here_opt groupby
2a7a3 5f 6f 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20  _opt having_opt 
2a7a4 6f 72 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69  orderby_opt limi
2a7a5 74 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 31 31 20  t_opt",. /* 111 
2a7a6 2a 2f 20 22 64 69 73 74 69 6e 63 74 20 3a 3a 3d  */ "distinct ::=
2a7a7 20 44 49 53 54 49 4e 43 54 22 2c 0a 20 2f 2a 20   DISTINCT",. /* 
2a7a8 31 31 32 20 2a 2f 20 22 64 69 73 74 69 6e 63 74  112 */ "distinct
2a7a9 20 3a 3a 3d 20 41 4c 4c 22 2c 0a 20 2f 2a 20 31   ::= ALL",. /* 1
2a7aa 31 33 20 2a 2f 20 22 64 69 73 74 69 6e 63 74 20  13 */ "distinct 
2a7ab 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 31 34 20 2a 2f  ::=",. /* 114 */
2a7ac 20 22 73 63 6c 70 20 3a 3a 3d 20 73 65 6c 63 6f   "sclp ::= selco
2a7ad 6c 6c 69 73 74 20 43 4f 4d 4d 41 22 2c 0a 20 2f  llist COMMA",. /
2a7ae 2a 20 31 31 35 20 2a 2f 20 22 73 63 6c 70 20 3a  * 115 */ "sclp :
2a7af 3a 3d 22 2c 0a 20 2f 2a 20 31 31 36 20 2a 2f 20  :=",. /* 116 */ 
2a7b0 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20  "selcollist ::= 
2a7b1 73 63 6c 70 20 65 78 70 72 20 61 73 22 2c 0a 20  sclp expr as",. 
2a7b2 2f 2a 20 31 31 37 20 2a 2f 20 22 73 65 6c 63 6f  /* 117 */ "selco
2a7b3 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53  llist ::= sclp S
2a7b4 54 41 52 22 2c 0a 20 2f 2a 20 31 31 38 20 2a 2f  TAR",. /* 118 */
2a7b5 20 22 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d   "selcollist ::=
2a7b6 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41   sclp nm DOT STA
2a7b7 52 22 2c 0a 20 2f 2a 20 31 31 39 20 2a 2f 20 22  R",. /* 119 */ "
2a7b8 61 73 20 3a 3a 3d 20 41 53 20 6e 6d 22 2c 0a 20  as ::= AS nm",. 
2a7b9 2f 2a 20 31 32 30 20 2a 2f 20 22 61 73 20 3a 3a  /* 120 */ "as ::
2a7ba 3d 20 69 64 73 22 2c 0a 20 2f 2a 20 31 32 31 20  = ids",. /* 121 
2a7bb 2a 2f 20 22 61 73 20 3a 3a 3d 22 2c 0a 20 2f 2a  */ "as ::=",. /*
2a7bc 20 31 32 32 20 2a 2f 20 22 66 72 6f 6d 20 3a 3a   122 */ "from ::
2a7bd 3d 22 2c 0a 20 2f 2a 20 31 32 33 20 2a 2f 20 22  =",. /* 123 */ "
2a7be 66 72 6f 6d 20 3a 3a 3d 20 46 52 4f 4d 20 73 65  from ::= FROM se
2a7bf 6c 74 61 62 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  ltablist",. /* 1
2a7c0 32 34 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69  24 */ "stl_prefi
2a7c1 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74  x ::= seltablist
2a7c2 20 6a 6f 69 6e 6f 70 22 2c 0a 20 2f 2a 20 31 32   joinop",. /* 12
2a7c3 35 20 2a 2f 20 22 73 74 6c 5f 70 72 65 66 69 78  5 */ "stl_prefix
2a7c4 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 32 36 20 2a   ::=",. /* 126 *
2a7c5 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  / "seltablist ::
2a7c6 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e 6d 20  = stl_prefix nm 
2a7c7 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65 64 5f  dbnm as indexed_
2a7c8 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69 6e 67  opt on_opt using
2a7c9 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 32 37 20 2a  _opt",. /* 127 *
2a7ca 2f 20 22 73 65 6c 74 61 62 6c 69 73 74 20 3a 3a  / "seltablist ::
2a7cb 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 4c 50 20  = stl_prefix LP 
2a7cc 73 65 6c 74 61 62 6c 69 73 74 5f 70 61 72 65 6e  seltablist_paren
2a7cd 20 52 50 20 61 73 20 6f 6e 5f 6f 70 74 20 75 73   RP as on_opt us
2a7ce 69 6e 67 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 32  ing_opt",. /* 12
2a7cf 38 20 2a 2f 20 22 73 65 6c 74 61 62 6c 69 73 74  8 */ "seltablist
2a7d0 5f 70 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 65 63  _paren ::= selec
2a7d1 74 22 2c 0a 20 2f 2a 20 31 32 39 20 2a 2f 20 22  t",. /* 129 */ "
2a7d2 73 65 6c 74 61 62 6c 69 73 74 5f 70 61 72 65 6e  seltablist_paren
2a7d3 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74 22   ::= seltablist"
2a7d4 2c 0a 20 2f 2a 20 31 33 30 20 2a 2f 20 22 64 62  ,. /* 130 */ "db
2a7d5 6e 6d 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 33 31  nm ::=",. /* 131
2a7d6 20 2a 2f 20 22 64 62 6e 6d 20 3a 3a 3d 20 44 4f   */ "dbnm ::= DO
2a7d7 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 33 32 20 2a  T nm",. /* 132 *
2a7d8 2f 20 22 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20  / "fullname ::= 
2a7d9 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a 20 31 33  nm dbnm",. /* 13
2a7da 33 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20 3a 3a 3d  3 */ "joinop ::=
2a7db 20 43 4f 4d 4d 41 7c 4a 4f 49 4e 22 2c 0a 20 2f   COMMA|JOIN",. /
2a7dc 2a 20 31 33 34 20 2a 2f 20 22 6a 6f 69 6e 6f 70  * 134 */ "joinop
2a7dd 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f 49   ::= JOIN_KW JOI
2a7de 4e 22 2c 0a 20 2f 2a 20 31 33 35 20 2a 2f 20 22  N",. /* 135 */ "
2a7df 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f  joinop ::= JOIN_
2a7e0 4b 57 20 6e 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a  KW nm JOIN",. /*
2a7e1 20 31 33 36 20 2a 2f 20 22 6a 6f 69 6e 6f 70 20   136 */ "joinop 
2a7e2 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 6e 6d 20 6e  ::= JOIN_KW nm n
2a7e3 6d 20 4a 4f 49 4e 22 2c 0a 20 2f 2a 20 31 33 37  m JOIN",. /* 137
2a7e4 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d 20   */ "on_opt ::= 
2a7e5 4f 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 33  ON expr",. /* 13
2a7e6 38 20 2a 2f 20 22 6f 6e 5f 6f 70 74 20 3a 3a 3d  8 */ "on_opt ::=
2a7e7 22 2c 0a 20 2f 2a 20 31 33 39 20 2a 2f 20 22 69  ",. /* 139 */ "i
2a7e8 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 22 2c  ndexed_opt ::=",
2a7e9 0a 20 2f 2a 20 31 34 30 20 2a 2f 20 22 69 6e 64  . /* 140 */ "ind
2a7ea 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44  exed_opt ::= IND
2a7eb 45 58 45 44 20 42 59 20 6e 6d 22 2c 0a 20 2f 2a  EXED BY nm",. /*
2a7ec 20 31 34 31 20 2a 2f 20 22 69 6e 64 65 78 65 64   141 */ "indexed
2a7ed 5f 6f 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44  _opt ::= NOT IND
2a7ee 45 58 45 44 22 2c 0a 20 2f 2a 20 31 34 32 20 2a  EXED",. /* 142 *
2a7ef 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20 3a 3a 3d  / "using_opt ::=
2a7f0 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f 6c   USING LP inscol
2a7f1 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 34  list RP",. /* 14
2a7f2 33 20 2a 2f 20 22 75 73 69 6e 67 5f 6f 70 74 20  3 */ "using_opt 
2a7f3 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 34 34 20 2a 2f  ::=",. /* 144 */
2a7f4 20 22 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a   "orderby_opt ::
2a7f5 3d 22 2c 0a 20 2f 2a 20 31 34 35 20 2a 2f 20 22  =",. /* 145 */ "
2a7f6 6f 72 64 65 72 62 79 5f 6f 70 74 20 3a 3a 3d 20  orderby_opt ::= 
2a7f7 4f 52 44 45 52 20 42 59 20 73 6f 72 74 6c 69 73  ORDER BY sortlis
2a7f8 74 22 2c 0a 20 2f 2a 20 31 34 36 20 2a 2f 20 22  t",. /* 146 */ "
2a7f9 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72  sortlist ::= sor
2a7fa 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73 6f 72 74  tlist COMMA sort
2a7fb 69 74 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c  item sortorder",
2a7fc 0a 20 2f 2a 20 31 34 37 20 2a 2f 20 22 73 6f 72  . /* 147 */ "sor
2a7fd 74 6c 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74  tlist ::= sortit
2a7fe 65 6d 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20  em sortorder",. 
2a7ff 2f 2a 20 31 34 38 20 2a 2f 20 22 73 6f 72 74 69  /* 148 */ "sorti
2a800 74 65 6d 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  tem ::= expr",. 
2a801 2f 2a 20 31 34 39 20 2a 2f 20 22 73 6f 72 74 6f  /* 149 */ "sorto
2a802 72 64 65 72 20 3a 3a 3d 20 41 53 43 22 2c 0a 20  rder ::= ASC",. 
2a803 2f 2a 20 31 35 30 20 2a 2f 20 22 73 6f 72 74 6f  /* 150 */ "sorto
2a804 72 64 65 72 20 3a 3a 3d 20 44 45 53 43 22 2c 0a  rder ::= DESC",.
2a805 20 2f 2a 20 31 35 31 20 2a 2f 20 22 73 6f 72 74   /* 151 */ "sort
2a806 6f 72 64 65 72 20 3a 3a 3d 22 2c 0a 20 2f 2a 20  order ::=",. /* 
2a807 31 35 32 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f  152 */ "groupby_
2a808 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35  opt ::=",. /* 15
2a809 33 20 2a 2f 20 22 67 72 6f 75 70 62 79 5f 6f 70  3 */ "groupby_op
2a80a 74 20 3a 3a 3d 20 47 52 4f 55 50 20 42 59 20 6e  t ::= GROUP BY n
2a80b 65 78 70 72 6c 69 73 74 22 2c 0a 20 2f 2a 20 31  exprlist",. /* 1
2a80c 35 34 20 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70  54 */ "having_op
2a80d 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 35 35 20  t ::=",. /* 155 
2a80e 2a 2f 20 22 68 61 76 69 6e 67 5f 6f 70 74 20 3a  */ "having_opt :
2a80f 3a 3d 20 48 41 56 49 4e 47 20 65 78 70 72 22 2c  := HAVING expr",
2a810 0a 20 2f 2a 20 31 35 36 20 2a 2f 20 22 6c 69 6d  . /* 156 */ "lim
2a811 69 74 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a  it_opt ::=",. /*
2a812 20 31 35 37 20 2a 2f 20 22 6c 69 6d 69 74 5f 6f   157 */ "limit_o
2a813 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
2a814 72 22 2c 0a 20 2f 2a 20 31 35 38 20 2a 2f 20 22  r",. /* 158 */ "
2a815 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49  limit_opt ::= LI
2a816 4d 49 54 20 65 78 70 72 20 4f 46 46 53 45 54 20  MIT expr OFFSET 
2a817 65 78 70 72 22 2c 0a 20 2f 2a 20 31 35 39 20 2a  expr",. /* 159 *
2a818 2f 20 22 6c 69 6d 69 74 5f 6f 70 74 20 3a 3a 3d  / "limit_opt ::=
2a819 20 4c 49 4d 49 54 20 65 78 70 72 20 43 4f 4d 4d   LIMIT expr COMM
2a81a 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 36 30  A expr",. /* 160
2a81b 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 45 4c   */ "cmd ::= DEL
2a81c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61 6d  ETE FROM fullnam
2a81d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77 68  e indexed_opt wh
2a81e 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 31 36  ere_opt",. /* 16
2a81f 31 20 2a 2f 20 22 77 68 65 72 65 5f 6f 70 74 20  1 */ "where_opt 
2a820 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 36 32 20 2a 2f  ::=",. /* 162 */
2a821 20 22 77 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20   "where_opt ::= 
2a822 57 48 45 52 45 20 65 78 70 72 22 2c 0a 20 2f 2a  WHERE expr",. /*
2a823 20 31 36 33 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   163 */ "cmd ::=
2a824 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20 66   UPDATE orconf f
2a825 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64 5f  ullname indexed_
2a826 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74 20  opt SET setlist 
2a827 77 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20  where_opt",. /* 
2a828 31 36 34 20 2a 2f 20 22 73 65 74 6c 69 73 74 20  164 */ "setlist 
2a829 3a 3a 3d 20 73 65 74 6c 69 73 74 20 43 4f 4d 4d  ::= setlist COMM
2a82a 41 20 6e 6d 20 45 51 20 65 78 70 72 22 2c 0a 20  A nm EQ expr",. 
2a82b 2f 2a 20 31 36 35 20 2a 2f 20 22 73 65 74 6c 69  /* 165 */ "setli
2a82c 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70  st ::= nm EQ exp
2a82d 72 22 2c 0a 20 2f 2a 20 31 36 36 20 2a 2f 20 22  r",. /* 166 */ "
2a82e 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63  cmd ::= insert_c
2a82f 6d 64 20 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65  md INTO fullname
2a830 20 69 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20   inscollist_opt 
2a831 56 41 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69  VALUES LP itemli
2a832 73 74 20 52 50 22 2c 0a 20 2f 2a 20 31 36 37 20  st RP",. /* 167 
2a833 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  */ "cmd ::= inse
2a834 72 74 5f 63 6d 64 20 49 4e 54 4f 20 66 75 6c 6c  rt_cmd INTO full
2a835 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  name inscollist_
2a836 6f 70 74 20 73 65 6c 65 63 74 22 2c 0a 20 2f 2a  opt select",. /*
2a837 20 31 36 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   168 */ "cmd ::=
2a838 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f   insert_cmd INTO
2a839 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c   fullname inscol
2a83a 6c 69 73 74 5f 6f 70 74 20 44 45 46 41 55 4c 54  list_opt DEFAULT
2a83b 20 56 41 4c 55 45 53 22 2c 0a 20 2f 2a 20 31 36   VALUES",. /* 16
2a83c 39 20 2a 2f 20 22 69 6e 73 65 72 74 5f 63 6d 64  9 */ "insert_cmd
2a83d 20 3a 3a 3d 20 49 4e 53 45 52 54 20 6f 72 63 6f   ::= INSERT orco
2a83e 6e 66 22 2c 0a 20 2f 2a 20 31 37 30 20 2a 2f 20  nf",. /* 170 */ 
2a83f 22 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d 20  "insert_cmd ::= 
2a840 52 45 50 4c 41 43 45 22 2c 0a 20 2f 2a 20 31 37  REPLACE",. /* 17
2a841 31 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a  1 */ "itemlist :
2a842 3a 3d 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d  := itemlist COMM
2a843 41 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 32  A expr",. /* 172
2a844 20 2a 2f 20 22 69 74 65 6d 6c 69 73 74 20 3a 3a   */ "itemlist ::
2a845 3d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 37 33  = expr",. /* 173
2a846 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74 5f   */ "inscollist_
2a847 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 31 37  opt ::=",. /* 17
2a848 34 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69 73 74  4 */ "inscollist
2a849 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20 69 6e 73 63  _opt ::= LP insc
2a84a 6f 6c 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a 20  ollist RP",. /* 
2a84b 31 37 35 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c 69  175 */ "inscolli
2a84c 73 74 20 3a 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73  st ::= inscollis
2a84d 74 20 43 4f 4d 4d 41 20 6e 6d 22 2c 0a 20 2f 2a  t COMMA nm",. /*
2a84e 20 31 37 36 20 2a 2f 20 22 69 6e 73 63 6f 6c 6c   176 */ "inscoll
2a84f 69 73 74 20 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a  ist ::= nm",. /*
2a850 20 31 37 37 20 2a 2f 20 22 65 78 70 72 20 3a 3a   177 */ "expr ::
2a851 3d 20 74 65 72 6d 22 2c 0a 20 2f 2a 20 31 37 38  = term",. /* 178
2a852 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50   */ "expr ::= LP
2a853 20 65 78 70 72 20 52 50 22 2c 0a 20 2f 2a 20 31   expr RP",. /* 1
2a854 37 39 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20  79 */ "term ::= 
2a855 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 31 38 30 20 2a  NULL",. /* 180 *
2a856 2f 20 22 65 78 70 72 20 3a 3a 3d 20 49 44 22 2c  / "expr ::= ID",
2a857 0a 20 2f 2a 20 31 38 31 20 2a 2f 20 22 65 78 70  . /* 181 */ "exp
2a858 72 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 22 2c 0a  r ::= JOIN_KW",.
2a859 20 2f 2a 20 31 38 32 20 2a 2f 20 22 65 78 70 72   /* 182 */ "expr
2a85a 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 22 2c   ::= nm DOT nm",
2a85b 0a 20 2f 2a 20 31 38 33 20 2a 2f 20 22 65 78 70  . /* 183 */ "exp
2a85c 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20  r ::= nm DOT nm 
2a85d 44 4f 54 20 6e 6d 22 2c 0a 20 2f 2a 20 31 38 34  DOT nm",. /* 184
2a85e 20 2a 2f 20 22 74 65 72 6d 20 3a 3a 3d 20 49 4e   */ "term ::= IN
2a85f 54 45 47 45 52 7c 46 4c 4f 41 54 7c 42 4c 4f 42  TEGER|FLOAT|BLOB
2a860 22 2c 0a 20 2f 2a 20 31 38 35 20 2a 2f 20 22 74  ",. /* 185 */ "t
2a861 65 72 6d 20 3a 3a 3d 20 53 54 52 49 4e 47 22 2c  erm ::= STRING",
2a862 0a 20 2f 2a 20 31 38 36 20 2a 2f 20 22 65 78 70  . /* 186 */ "exp
2a863 72 20 3a 3a 3d 20 52 45 47 49 53 54 45 52 22 2c  r ::= REGISTER",
2a864 0a 20 2f 2a 20 31 38 37 20 2a 2f 20 22 65 78 70  . /* 187 */ "exp
2a865 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45 22 2c  r ::= VARIABLE",
2a866 0a 20 2f 2a 20 31 38 38 20 2a 2f 20 22 65 78 70  . /* 188 */ "exp
2a867 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4c 4c 41  r ::= expr COLLA
2a868 54 45 20 69 64 73 22 2c 0a 20 2f 2a 20 31 38 39  TE ids",. /* 189
2a869 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 43 41   */ "expr ::= CA
2a86a 53 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79  ST LP expr AS ty
2a86b 70 65 74 6f 6b 65 6e 20 52 50 22 2c 0a 20 2f 2a  petoken RP",. /*
2a86c 20 31 39 30 20 2a 2f 20 22 65 78 70 72 20 3a 3a   190 */ "expr ::
2a86d 3d 20 49 44 20 4c 50 20 64 69 73 74 69 6e 63 74  = ID LP distinct
2a86e 20 65 78 70 72 6c 69 73 74 20 52 50 22 2c 0a 20   exprlist RP",. 
2a86f 2f 2a 20 31 39 31 20 2a 2f 20 22 65 78 70 72 20  /* 191 */ "expr 
2a870 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52  ::= ID LP STAR R
2a871 50 22 2c 0a 20 2f 2a 20 31 39 32 20 2a 2f 20 22  P",. /* 192 */ "
2a872 74 65 72 6d 20 3a 3a 3d 20 43 54 49 4d 45 5f 4b  term ::= CTIME_K
2a873 57 22 2c 0a 20 2f 2a 20 31 39 33 20 2a 2f 20 22  W",. /* 193 */ "
2a874 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 41 4e  expr ::= expr AN
2a875 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 34  D expr",. /* 194
2a876 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2a877 70 72 20 4f 52 20 65 78 70 72 22 2c 0a 20 2f 2a  pr OR expr",. /*
2a878 20 31 39 35 20 2a 2f 20 22 65 78 70 72 20 3a 3a   195 */ "expr ::
2a879 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47 45 7c  = expr LT|GT|GE|
2a87a 4c 45 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39  LE expr",. /* 19
2a87b 36 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  6 */ "expr ::= e
2a87c 78 70 72 20 45 51 7c 4e 45 20 65 78 70 72 22 2c  xpr EQ|NE expr",
2a87d 0a 20 2f 2a 20 31 39 37 20 2a 2f 20 22 65 78 70  . /* 197 */ "exp
2a87e 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54 41 4e  r ::= expr BITAN
2a87f 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54 7c 52  D|BITOR|LSHIFT|R
2a880 53 48 49 46 54 20 65 78 70 72 22 2c 0a 20 2f 2a  SHIFT expr",. /*
2a881 20 31 39 38 20 2a 2f 20 22 65 78 70 72 20 3a 3a   198 */ "expr ::
2a882 3d 20 65 78 70 72 20 50 4c 55 53 7c 4d 49 4e 55  = expr PLUS|MINU
2a883 53 20 65 78 70 72 22 2c 0a 20 2f 2a 20 31 39 39  S expr",. /* 199
2a884 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2a885 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c 52 45  pr STAR|SLASH|RE
2a886 4d 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 30 30  M expr",. /* 200
2a887 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2a888 70 72 20 43 4f 4e 43 41 54 20 65 78 70 72 22 2c  pr CONCAT expr",
2a889 0a 20 2f 2a 20 32 30 31 20 2a 2f 20 22 6c 69 6b  . /* 201 */ "lik
2a88a 65 6f 70 20 3a 3a 3d 20 4c 49 4b 45 5f 4b 57 22  eop ::= LIKE_KW"
2a88b 2c 0a 20 2f 2a 20 32 30 32 20 2a 2f 20 22 6c 69  ,. /* 202 */ "li
2a88c 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4c 49 4b  keop ::= NOT LIK
2a88d 45 5f 4b 57 22 2c 0a 20 2f 2a 20 32 30 33 20 2a  E_KW",. /* 203 *
2a88e 2f 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41  / "likeop ::= MA
2a88f 54 43 48 22 2c 0a 20 2f 2a 20 32 30 34 20 2a 2f  TCH",. /* 204 */
2a890 20 22 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54   "likeop ::= NOT
2a891 20 4d 41 54 43 48 22 2c 0a 20 2f 2a 20 32 30 35   MATCH",. /* 205
2a892 20 2a 2f 20 22 65 73 63 61 70 65 20 3a 3a 3d 20   */ "escape ::= 
2a893 45 53 43 41 50 45 20 65 78 70 72 22 2c 0a 20 2f  ESCAPE expr",. /
2a894 2a 20 32 30 36 20 2a 2f 20 22 65 73 63 61 70 65  * 206 */ "escape
2a895 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 30 37 20 2a   ::=",. /* 207 *
2a896 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  / "expr ::= expr
2a897 20 6c 69 6b 65 6f 70 20 65 78 70 72 20 65 73 63   likeop expr esc
2a898 61 70 65 22 2c 0a 20 2f 2a 20 32 30 38 20 2a 2f  ape",. /* 208 */
2a899 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
2a89a 49 53 4e 55 4c 4c 7c 4e 4f 54 4e 55 4c 4c 22 2c  ISNULL|NOTNULL",
2a89b 0a 20 2f 2a 20 32 30 39 20 2a 2f 20 22 65 78 70  . /* 209 */ "exp
2a89c 72 20 3a 3a 3d 20 65 78 70 72 20 49 53 20 4e 55  r ::= expr IS NU
2a89d 4c 4c 22 2c 0a 20 2f 2a 20 32 31 30 20 2a 2f 20  LL",. /* 210 */ 
2a89e 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4e  "expr ::= expr N
2a89f 4f 54 20 4e 55 4c 4c 22 2c 0a 20 2f 2a 20 32 31  OT NULL",. /* 21
2a8a0 31 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  1 */ "expr ::= e
2a8a1 78 70 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c 22  xpr IS NOT NULL"
2a8a2 2c 0a 20 2f 2a 20 32 31 32 20 2a 2f 20 22 65 78  ,. /* 212 */ "ex
2a8a3 70 72 20 3a 3a 3d 20 4e 4f 54 20 65 78 70 72 22  pr ::= NOT expr"
2a8a4 2c 0a 20 2f 2a 20 32 31 33 20 2a 2f 20 22 65 78  ,. /* 213 */ "ex
2a8a5 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78  pr ::= BITNOT ex
2a8a6 70 72 22 2c 0a 20 2f 2a 20 32 31 34 20 2a 2f 20  pr",. /* 214 */ 
2a8a7 22 65 78 70 72 20 3a 3a 3d 20 4d 49 4e 55 53 20  "expr ::= MINUS 
2a8a8 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 35 20 2a  expr",. /* 215 *
2a8a9 2f 20 22 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53  / "expr ::= PLUS
2a8aa 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 31 36 20   expr",. /* 216 
2a8ab 2a 2f 20 22 62 65 74 77 65 65 6e 5f 6f 70 20 3a  */ "between_op :
2a8ac 3a 3d 20 42 45 54 57 45 45 4e 22 2c 0a 20 2f 2a  := BETWEEN",. /*
2a8ad 20 32 31 37 20 2a 2f 20 22 62 65 74 77 65 65 6e   217 */ "between
2a8ae 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54 57  _op ::= NOT BETW
2a8af 45 45 4e 22 2c 0a 20 2f 2a 20 32 31 38 20 2a 2f  EEN",. /* 218 */
2a8b0 20 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20   "expr ::= expr 
2a8b1 62 65 74 77 65 65 6e 5f 6f 70 20 65 78 70 72 20  between_op expr 
2a8b2 41 4e 44 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32  AND expr",. /* 2
2a8b3 31 39 20 2a 2f 20 22 69 6e 5f 6f 70 20 3a 3a 3d  19 */ "in_op ::=
2a8b4 20 49 4e 22 2c 0a 20 2f 2a 20 32 32 30 20 2a 2f   IN",. /* 220 */
2a8b5 20 22 69 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20   "in_op ::= NOT 
2a8b6 49 4e 22 2c 0a 20 2f 2a 20 32 32 31 20 2a 2f 20  IN",. /* 221 */ 
2a8b7 22 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 69  "expr ::= expr i
2a8b8 6e 5f 6f 70 20 4c 50 20 65 78 70 72 6c 69 73 74  n_op LP exprlist
2a8b9 20 52 50 22 2c 0a 20 2f 2a 20 32 32 32 20 2a 2f   RP",. /* 222 */
2a8ba 20 22 65 78 70 72 20 3a 3a 3d 20 4c 50 20 73 65   "expr ::= LP se
2a8bb 6c 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32  lect RP",. /* 22
2a8bc 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65  3 */ "expr ::= e
2a8bd 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20 73 65 6c  xpr in_op LP sel
2a8be 65 63 74 20 52 50 22 2c 0a 20 2f 2a 20 32 32 34  ect RP",. /* 224
2a8bf 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 65 78   */ "expr ::= ex
2a8c0 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e 6d  pr in_op nm dbnm
2a8c1 22 2c 0a 20 2f 2a 20 32 32 35 20 2a 2f 20 22 65  ",. /* 225 */ "e
2a8c2 78 70 72 20 3a 3a 3d 20 45 58 49 53 54 53 20 4c  xpr ::= EXISTS L
2a8c3 50 20 73 65 6c 65 63 74 20 52 50 22 2c 0a 20 2f  P select RP",. /
2a8c4 2a 20 32 32 36 20 2a 2f 20 22 65 78 70 72 20 3a  * 226 */ "expr :
2a8c5 3a 3d 20 43 41 53 45 20 63 61 73 65 5f 6f 70 65  := CASE case_ope
2a8c6 72 61 6e 64 20 63 61 73 65 5f 65 78 70 72 6c 69  rand case_exprli
2a8c7 73 74 20 63 61 73 65 5f 65 6c 73 65 20 45 4e 44  st case_else END
2a8c8 22 2c 0a 20 2f 2a 20 32 32 37 20 2a 2f 20 22 63  ",. /* 227 */ "c
2a8c9 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
2a8ca 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
2a8cb 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
2a8cc 70 72 22 2c 0a 20 2f 2a 20 32 32 38 20 2a 2f 20  pr",. /* 228 */ 
2a8cd 22 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a  "case_exprlist :
2a8ce 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
2a8cf 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 32 39  N expr",. /* 229
2a8d0 20 2a 2f 20 22 63 61 73 65 5f 65 6c 73 65 20 3a   */ "case_else :
2a8d1 3a 3d 20 45 4c 53 45 20 65 78 70 72 22 2c 0a 20  := ELSE expr",. 
2a8d2 2f 2a 20 32 33 30 20 2a 2f 20 22 63 61 73 65 5f  /* 230 */ "case_
2a8d3 65 6c 73 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  else ::=",. /* 2
2a8d4 33 31 20 2a 2f 20 22 63 61 73 65 5f 6f 70 65 72  31 */ "case_oper
2a8d5 61 6e 64 20 3a 3a 3d 20 65 78 70 72 22 2c 0a 20  and ::= expr",. 
2a8d6 2f 2a 20 32 33 32 20 2a 2f 20 22 63 61 73 65 5f  /* 232 */ "case_
2a8d7 6f 70 65 72 61 6e 64 20 3a 3a 3d 22 2c 0a 20 2f  operand ::=",. /
2a8d8 2a 20 32 33 33 20 2a 2f 20 22 65 78 70 72 6c 69  * 233 */ "exprli
2a8d9 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73 74  st ::= nexprlist
2a8da 22 2c 0a 20 2f 2a 20 32 33 34 20 2a 2f 20 22 65  ",. /* 234 */ "e
2a8db 78 70 72 6c 69 73 74 20 3a 3a 3d 22 2c 0a 20 2f  xprlist ::=",. /
2a8dc 2a 20 32 33 35 20 2a 2f 20 22 6e 65 78 70 72 6c  * 235 */ "nexprl
2a8dd 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73  ist ::= nexprlis
2a8de 74 20 43 4f 4d 4d 41 20 65 78 70 72 22 2c 0a 20  t COMMA expr",. 
2a8df 2f 2a 20 32 33 36 20 2a 2f 20 22 6e 65 78 70 72  /* 236 */ "nexpr
2a8e0 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72 22 2c 0a  list ::= expr",.
2a8e1 20 2f 2a 20 32 33 37 20 2a 2f 20 22 63 6d 64 20   /* 237 */ "cmd 
2a8e2 3a 3a 3d 20 43 52 45 41 54 45 20 75 6e 69 71 75  ::= CREATE uniqu
2a8e3 65 66 6c 61 67 20 49 4e 44 45 58 20 69 66 6e 6f  eflag INDEX ifno
2a8e4 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e 6d 20  texists nm dbnm 
2a8e5 4f 4e 20 6e 6d 20 4c 50 20 69 64 78 6c 69 73 74  ON nm LP idxlist
2a8e6 20 52 50 22 2c 0a 20 2f 2a 20 32 33 38 20 2a 2f   RP",. /* 238 */
2a8e7 20 22 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a 3d   "uniqueflag ::=
2a8e8 20 55 4e 49 51 55 45 22 2c 0a 20 2f 2a 20 32 33   UNIQUE",. /* 23
2a8e9 39 20 2a 2f 20 22 75 6e 69 71 75 65 66 6c 61 67  9 */ "uniqueflag
2a8ea 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 30 20 2a   ::=",. /* 240 *
2a8eb 2f 20 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a  / "idxlist_opt :
2a8ec 3a 3d 22 2c 0a 20 2f 2a 20 32 34 31 20 2a 2f 20  :=",. /* 241 */ 
2a8ed 22 69 64 78 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d  "idxlist_opt ::=
2a8ee 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50 22 2c   LP idxlist RP",
2a8ef 0a 20 2f 2a 20 32 34 32 20 2a 2f 20 22 69 64 78  . /* 242 */ "idx
2a8f0 6c 69 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74  list ::= idxlist
2a8f1 20 43 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74   COMMA nm collat
2a8f2 65 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f  e sortorder",. /
2a8f3 2a 20 32 34 33 20 2a 2f 20 22 69 64 78 6c 69 73  * 243 */ "idxlis
2a8f4 74 20 3a 3a 3d 20 6e 6d 20 63 6f 6c 6c 61 74 65  t ::= nm collate
2a8f5 20 73 6f 72 74 6f 72 64 65 72 22 2c 0a 20 2f 2a   sortorder",. /*
2a8f6 20 32 34 34 20 2a 2f 20 22 63 6f 6c 6c 61 74 65   244 */ "collate
2a8f7 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 34 35 20 2a   ::=",. /* 245 *
2a8f8 2f 20 22 63 6f 6c 6c 61 74 65 20 3a 3a 3d 20 43  / "collate ::= C
2a8f9 4f 4c 4c 41 54 45 20 69 64 73 22 2c 0a 20 2f 2a  OLLATE ids",. /*
2a8fa 20 32 34 36 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   246 */ "cmd ::=
2a8fb 20 44 52 4f 50 20 49 4e 44 45 58 20 69 66 65 78   DROP INDEX ifex
2a8fc 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a  ists fullname",.
2a8fd 20 2f 2a 20 32 34 37 20 2a 2f 20 22 63 6d 64 20   /* 247 */ "cmd 
2a8fe 3a 3a 3d 20 56 41 43 55 55 4d 22 2c 0a 20 2f 2a  ::= VACUUM",. /*
2a8ff 20 32 34 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   248 */ "cmd ::=
2a900 20 56 41 43 55 55 4d 20 6e 6d 22 2c 0a 20 2f 2a   VACUUM nm",. /*
2a901 20 32 34 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   249 */ "cmd ::=
2a902 20 50 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20   PRAGMA nm dbnm 
2a903 45 51 20 6e 6d 6e 75 6d 22 2c 0a 20 2f 2a 20 32  EQ nmnum",. /* 2
2a904 35 30 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50  50 */ "cmd ::= P
2a905 52 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51  RAGMA nm dbnm EQ
2a906 20 4f 4e 22 2c 0a 20 2f 2a 20 32 35 31 20 2a 2f   ON",. /* 251 */
2a907 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41   "cmd ::= PRAGMA
2a908 20 6e 6d 20 64 62 6e 6d 20 45 51 20 44 45 4c 45   nm dbnm EQ DELE
2a909 54 45 22 2c 0a 20 2f 2a 20 32 35 32 20 2a 2f 20  TE",. /* 252 */ 
2a90a 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20  "cmd ::= PRAGMA 
2a90b 6e 6d 20 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73  nm dbnm EQ minus
2a90c 5f 6e 75 6d 22 2c 0a 20 2f 2a 20 32 35 33 20 2a  _num",. /* 253 *
2a90d 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47 4d  / "cmd ::= PRAGM
2a90e 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d 6e  A nm dbnm LP nmn
2a90f 75 6d 20 52 50 22 2c 0a 20 2f 2a 20 32 35 34 20  um RP",. /* 254 
2a910 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 50 52 41 47  */ "cmd ::= PRAG
2a911 4d 41 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20 2f 2a  MA nm dbnm",. /*
2a912 20 32 35 35 20 2a 2f 20 22 6e 6d 6e 75 6d 20 3a   255 */ "nmnum :
2a913 3a 3d 20 70 6c 75 73 5f 6e 75 6d 22 2c 0a 20 2f  := plus_num",. /
2a914 2a 20 32 35 36 20 2a 2f 20 22 6e 6d 6e 75 6d 20  * 256 */ "nmnum 
2a915 3a 3a 3d 20 6e 6d 22 2c 0a 20 2f 2a 20 32 35 37  ::= nm",. /* 257
2a916 20 2a 2f 20 22 70 6c 75 73 5f 6e 75 6d 20 3a 3a   */ "plus_num ::
2a917 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d 62 65  = plus_opt numbe
2a918 72 22 2c 0a 20 2f 2a 20 32 35 38 20 2a 2f 20 22  r",. /* 258 */ "
2a919 6d 69 6e 75 73 5f 6e 75 6d 20 3a 3a 3d 20 4d 49  minus_num ::= MI
2a91a 4e 55 53 20 6e 75 6d 62 65 72 22 2c 0a 20 2f 2a  NUS number",. /*
2a91b 20 32 35 39 20 2a 2f 20 22 6e 75 6d 62 65 72 20   259 */ "number 
2a91c 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41  ::= INTEGER|FLOA
2a91d 54 22 2c 0a 20 2f 2a 20 32 36 30 20 2a 2f 20 22  T",. /* 260 */ "
2a91e 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55  plus_opt ::= PLU
2a91f 53 22 2c 0a 20 2f 2a 20 32 36 31 20 2a 2f 20 22  S",. /* 261 */ "
2a920 70 6c 75 73 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20  plus_opt ::=",. 
2a921 2f 2a 20 32 36 32 20 2a 2f 20 22 63 6d 64 20 3a  /* 262 */ "cmd :
2a922 3a 3d 20 43 52 45 41 54 45 20 74 72 69 67 67 65  := CREATE trigge
2a923 72 5f 64 65 63 6c 20 42 45 47 49 4e 20 74 72 69  r_decl BEGIN tri
2a924 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e  gger_cmd_list EN
2a925 44 22 2c 0a 20 2f 2a 20 32 36 33 20 2a 2f 20 22  D",. /* 263 */ "
2a926 74 72 69 67 67 65 72 5f 64 65 63 6c 20 3a 3a 3d  trigger_decl ::=
2a927 20 74 65 6d 70 20 54 52 49 47 47 45 52 20 69 66   temp TRIGGER if
2a928 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64 62 6e  notexists nm dbn
2a929 6d 20 74 72 69 67 67 65 72 5f 74 69 6d 65 20 74  m trigger_time t
2a92a 72 69 67 67 65 72 5f 65 76 65 6e 74 20 4f 4e 20  rigger_event ON 
2a92b 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65 61 63 68  fullname foreach
2a92c 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f 63 6c 61  _clause when_cla
2a92d 75 73 65 22 2c 0a 20 2f 2a 20 32 36 34 20 2a 2f  use",. /* 264 */
2a92e 20 22 74 72 69 67 67 65 72 5f 74 69 6d 65 20 3a   "trigger_time :
2a92f 3a 3d 20 42 45 46 4f 52 45 22 2c 0a 20 2f 2a 20  := BEFORE",. /* 
2a930 32 36 35 20 2a 2f 20 22 74 72 69 67 67 65 72 5f  265 */ "trigger_
2a931 74 69 6d 65 20 3a 3a 3d 20 41 46 54 45 52 22 2c  time ::= AFTER",
2a932 0a 20 2f 2a 20 32 36 36 20 2a 2f 20 22 74 72 69  . /* 266 */ "tri
2a933 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49 4e  gger_time ::= IN
2a934 53 54 45 41 44 20 4f 46 22 2c 0a 20 2f 2a 20 32  STEAD OF",. /* 2
2a935 36 37 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 74  67 */ "trigger_t
2a936 69 6d 65 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32 36  ime ::=",. /* 26
2a937 38 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76  8 */ "trigger_ev
2a938 65 6e 74 20 3a 3a 3d 20 44 45 4c 45 54 45 7c 49  ent ::= DELETE|I
2a939 4e 53 45 52 54 22 2c 0a 20 2f 2a 20 32 36 39 20  NSERT",. /* 269 
2a93a 2a 2f 20 22 74 72 69 67 67 65 72 5f 65 76 65 6e  */ "trigger_even
2a93b 74 20 3a 3a 3d 20 55 50 44 41 54 45 22 2c 0a 20  t ::= UPDATE",. 
2a93c 2f 2a 20 32 37 30 20 2a 2f 20 22 74 72 69 67 67  /* 270 */ "trigg
2a93d 65 72 5f 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44  er_event ::= UPD
2a93e 41 54 45 20 4f 46 20 69 6e 73 63 6f 6c 6c 69 73  ATE OF inscollis
2a93f 74 22 2c 0a 20 2f 2a 20 32 37 31 20 2a 2f 20 22  t",. /* 271 */ "
2a940 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20 3a  foreach_clause :
2a941 3a 3d 22 2c 0a 20 2f 2a 20 32 37 32 20 2a 2f 20  :=",. /* 272 */ 
2a942 22 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65 20  "foreach_clause 
2a943 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f 57  ::= FOR EACH ROW
2a944 22 2c 0a 20 2f 2a 20 32 37 33 20 2a 2f 20 22 77  ",. /* 273 */ "w
2a945 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 22 2c  hen_clause ::=",
2a946 0a 20 2f 2a 20 32 37 34 20 2a 2f 20 22 77 68 65  . /* 274 */ "whe
2a947 6e 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 57 48 45  n_clause ::= WHE
2a948 4e 20 65 78 70 72 22 2c 0a 20 2f 2a 20 32 37 35  N expr",. /* 275
2a949 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64   */ "trigger_cmd
2a94a 5f 6c 69 73 74 20 3a 3a 3d 20 74 72 69 67 67 65  _list ::= trigge
2a94b 72 5f 63 6d 64 5f 6c 69 73 74 20 74 72 69 67 67  r_cmd_list trigg
2a94c 65 72 5f 63 6d 64 20 53 45 4d 49 22 2c 0a 20 2f  er_cmd SEMI",. /
2a94d 2a 20 32 37 36 20 2a 2f 20 22 74 72 69 67 67 65  * 276 */ "trigge
2a94e 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d 20 74  r_cmd_list ::= t
2a94f 72 69 67 67 65 72 5f 63 6d 64 20 53 45 4d 49 22  rigger_cmd SEMI"
2a950 2c 0a 20 2f 2a 20 32 37 37 20 2a 2f 20 22 74 72  ,. /* 277 */ "tr
2a951 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 55 50  igger_cmd ::= UP
2a952 44 41 54 45 20 6f 72 63 6f 6e 66 20 6e 6d 20 53  DATE orconf nm S
2a953 45 54 20 73 65 74 6c 69 73 74 20 77 68 65 72 65  ET setlist where
2a954 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32 37 38 20 2a  _opt",. /* 278 *
2a955 2f 20 22 74 72 69 67 67 65 72 5f 63 6d 64 20 3a  / "trigger_cmd :
2a956 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
2a957 54 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74  TO nm inscollist
2a958 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c 50 20 69  _opt VALUES LP i
2a959 74 65 6d 6c 69 73 74 20 52 50 22 2c 0a 20 2f 2a  temlist RP",. /*
2a95a 20 32 37 39 20 2a 2f 20 22 74 72 69 67 67 65 72   279 */ "trigger
2a95b 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65 72 74 5f  _cmd ::= insert_
2a95c 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69 6e 73 63  cmd INTO nm insc
2a95d 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
2a95e 74 22 2c 0a 20 2f 2a 20 32 38 30 20 2a 2f 20 22  t",. /* 280 */ "
2a95f 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a 3d 20  trigger_cmd ::= 
2a960 44 45 4c 45 54 45 20 46 52 4f 4d 20 6e 6d 20 77  DELETE FROM nm w
2a961 68 65 72 65 5f 6f 70 74 22 2c 0a 20 2f 2a 20 32  here_opt",. /* 2
2a962 38 31 20 2a 2f 20 22 74 72 69 67 67 65 72 5f 63  81 */ "trigger_c
2a963 6d 64 20 3a 3a 3d 20 73 65 6c 65 63 74 22 2c 0a  md ::= select",.
2a964 20 2f 2a 20 32 38 32 20 2a 2f 20 22 65 78 70 72   /* 282 */ "expr
2a965 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 49 47   ::= RAISE LP IG
2a966 4e 4f 52 45 20 52 50 22 2c 0a 20 2f 2a 20 32 38  NORE RP",. /* 28
2a967 33 20 2a 2f 20 22 65 78 70 72 20 3a 3a 3d 20 52  3 */ "expr ::= R
2a968 41 49 53 45 20 4c 50 20 72 61 69 73 65 74 79 70  AISE LP raisetyp
2a969 65 20 43 4f 4d 4d 41 20 6e 6d 20 52 50 22 2c 0a  e COMMA nm RP",.
2a96a 20 2f 2a 20 32 38 34 20 2a 2f 20 22 72 61 69 73   /* 284 */ "rais
2a96b 65 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41  etype ::= ROLLBA
2a96c 43 4b 22 2c 0a 20 2f 2a 20 32 38 35 20 2a 2f 20  CK",. /* 285 */ 
2a96d 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41  "raisetype ::= A
2a96e 42 4f 52 54 22 2c 0a 20 2f 2a 20 32 38 36 20 2a  BORT",. /* 286 *
2a96f 2f 20 22 72 61 69 73 65 74 79 70 65 20 3a 3a 3d  / "raisetype ::=
2a970 20 46 41 49 4c 22 2c 0a 20 2f 2a 20 32 38 37 20   FAIL",. /* 287 
2a971 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 44 52 4f 50  */ "cmd ::= DROP
2a972 20 54 52 49 47 47 45 52 20 69 66 65 78 69 73 74   TRIGGER ifexist
2a973 73 20 66 75 6c 6c 6e 61 6d 65 22 2c 0a 20 2f 2a  s fullname",. /*
2a974 20 32 38 38 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   288 */ "cmd ::=
2a975 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73 65   ATTACH database
2a976 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53 20  _kw_opt expr AS 
2a977 65 78 70 72 20 6b 65 79 5f 6f 70 74 22 2c 0a 20  expr key_opt",. 
2a978 2f 2a 20 32 38 39 20 2a 2f 20 22 63 6d 64 20 3a  /* 289 */ "cmd :
2a979 3a 3d 20 44 45 54 41 43 48 20 64 61 74 61 62 61  := DETACH databa
2a97a 73 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 22 2c  se_kw_opt expr",
2a97b 0a 20 2f 2a 20 32 39 30 20 2a 2f 20 22 6b 65 79  . /* 290 */ "key
2a97c 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
2a97d 39 31 20 2a 2f 20 22 6b 65 79 5f 6f 70 74 20 3a  91 */ "key_opt :
2a97e 3a 3d 20 4b 45 59 20 65 78 70 72 22 2c 0a 20 2f  := KEY expr",. /
2a97f 2a 20 32 39 32 20 2a 2f 20 22 64 61 74 61 62 61  * 292 */ "databa
2a980 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41  se_kw_opt ::= DA
2a981 54 41 42 41 53 45 22 2c 0a 20 2f 2a 20 32 39 33  TABASE",. /* 293
2a982 20 2a 2f 20 22 64 61 74 61 62 61 73 65 5f 6b 77   */ "database_kw
2a983 5f 6f 70 74 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 32  _opt ::=",. /* 2
2a984 39 34 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52  94 */ "cmd ::= R
2a985 45 49 4e 44 45 58 22 2c 0a 20 2f 2a 20 32 39 35  EINDEX",. /* 295
2a986 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 52 45 49   */ "cmd ::= REI
2a987 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 22 2c 0a 20  NDEX nm dbnm",. 
2a988 2f 2a 20 32 39 36 20 2a 2f 20 22 63 6d 64 20 3a  /* 296 */ "cmd :
2a989 3a 3d 20 41 4e 41 4c 59 5a 45 22 2c 0a 20 2f 2a  := ANALYZE",. /*
2a98a 20 32 39 37 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d   297 */ "cmd ::=
2a98b 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64 62 6e 6d   ANALYZE nm dbnm
2a98c 22 2c 0a 20 2f 2a 20 32 39 38 20 2a 2f 20 22 63  ",. /* 298 */ "c
2a98d 6d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42  md ::= ALTER TAB
2a98e 4c 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41  LE fullname RENA
2a98f 4d 45 20 54 4f 20 6e 6d 22 2c 0a 20 2f 2a 20 32  ME TO nm",. /* 2
2a990 39 39 20 2a 2f 20 22 63 6d 64 20 3a 3a 3d 20 41  99 */ "cmd ::= A
2a991 4c 54 45 52 20 54 41 42 4c 45 20 61 64 64 5f 63  LTER TABLE add_c
2a992 6f 6c 75 6d 6e 5f 66 75 6c 6c 6e 61 6d 65 20 41  olumn_fullname A
2a993 44 44 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  DD kwcolumn_opt 
2a994 63 6f 6c 75 6d 6e 22 2c 0a 20 2f 2a 20 33 30 30  column",. /* 300
2a995 20 2a 2f 20 22 61 64 64 5f 63 6f 6c 75 6d 6e 5f   */ "add_column_
2a996 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c  fullname ::= ful
2a997 6c 6e 61 6d 65 22 2c 0a 20 2f 2a 20 33 30 31 20  lname",. /* 301 
2a998 2a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74  */ "kwcolumn_opt
2a999 20 3a 3a 3d 22 2c 0a 20 2f 2a 20 33 30 32 20 2a   ::=",. /* 302 *
2a99a 2f 20 22 6b 77 63 6f 6c 75 6d 6e 5f 6f 70 74 20  / "kwcolumn_opt 
2a99b 3a 3a 3d 20 43 4f 4c 55 4d 4e 4b 57 22 2c 0a 20  ::= COLUMNKW",. 
2a99c 2f 2a 20 33 30 33 20 2a 2f 20 22 63 6d 64 20 3a  /* 303 */ "cmd :
2a99d 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62 22 2c  := create_vtab",
2a99e 0a 20 2f 2a 20 33 30 34 20 2a 2f 20 22 63 6d 64  . /* 304 */ "cmd
2a99f 20 3a 3a 3d 20 63 72 65 61 74 65 5f 76 74 61 62   ::= create_vtab
2a9a0 20 4c 50 20 76 74 61 62 61 72 67 6c 69 73 74 20   LP vtabarglist 
2a9a1 52 50 22 2c 0a 20 2f 2a 20 33 30 35 20 2a 2f 20  RP",. /* 305 */ 
2a9a2 22 63 72 65 61 74 65 5f 76 74 61 62 20 3a 3a 3d  "create_vtab ::=
2a9a3 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 20   CREATE VIRTUAL 
2a9a4 54 41 42 4c 45 20 6e 6d 20 64 62 6e 6d 20 55 53  TABLE nm dbnm US
2a9a5 49 4e 47 20 6e 6d 22 2c 0a 20 2f 2a 20 33 30 36  ING nm",. /* 306
2a9a6 20 2a 2f 20 22 76 74 61 62 61 72 67 6c 69 73 74   */ "vtabarglist
2a9a7 20 3a 3a 3d 20 76 74 61 62 61 72 67 22 2c 0a 20   ::= vtabarg",. 
2a9a8 2f 2a 20 33 30 37 20 2a 2f 20 22 76 74 61 62 61  /* 307 */ "vtaba
2a9a9 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61  rglist ::= vtaba
2a9aa 72 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61  rglist COMMA vta
2a9ab 62 61 72 67 22 2c 0a 20 2f 2a 20 33 30 38 20 2a  barg",. /* 308 *
2a9ac 2f 20 22 76 74 61 62 61 72 67 20 3a 3a 3d 22 2c  / "vtabarg ::=",
2a9ad 0a 20 2f 2a 20 33 30 39 20 2a 2f 20 22 76 74 61  . /* 309 */ "vta
2a9ae 62 61 72 67 20 3a 3a 3d 20 76 74 61 62 61 72 67  barg ::= vtabarg
2a9af 20 76 74 61 62 61 72 67 74 6f 6b 65 6e 22 2c 0a   vtabargtoken",.
2a9b0 20 2f 2a 20 33 31 30 20 2a 2f 20 22 76 74 61 62   /* 310 */ "vtab
2a9b1 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59  argtoken ::= ANY
2a9b2 22 2c 0a 20 2f 2a 20 33 31 31 20 2a 2f 20 22 76  ",. /* 311 */ "v
2a9b3 74 61 62 61 72 67 74 6f 6b 65 6e 20 3a 3a 3d 20  tabargtoken ::= 
2a9b4 6c 70 20 61 6e 79 6c 69 73 74 20 52 50 22 2c 0a  lp anylist RP",.
2a9b5 20 2f 2a 20 33 31 32 20 2a 2f 20 22 6c 70 20 3a   /* 312 */ "lp :
2a9b6 3a 3d 20 4c 50 22 2c 0a 20 2f 2a 20 33 31 33 20  := LP",. /* 313 
2a9b7 2a 2f 20 22 61 6e 79 6c 69 73 74 20 3a 3a 3d 22  */ "anylist ::="
2a9b8 2c 0a 20 2f 2a 20 33 31 34 20 2a 2f 20 22 61 6e  ,. /* 314 */ "an
2a9b9 79 6c 69 73 74 20 3a 3a 3d 20 61 6e 79 6c 69 73  ylist ::= anylis
2a9ba 74 20 41 4e 59 22 2c 0a 7d 3b 0a 23 65 6e 64 69  t ANY",.};.#endi
2a9bb 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a 0a  f /* NDEBUG */..
2a9bc 0a 23 69 66 20 59 59 53 54 41 43 4b 44 45 50 54  .#if YYSTACKDEPT
2a9bd 48 3c 3d 30 0a 2f 2a 0a 2a 2a 20 54 72 79 20 74  H<=0./*.** Try t
2a9be 6f 20 69 6e 63 72 65 61 73 65 20 74 68 65 20 73  o increase the s
2a9bf 69 7a 65 20 6f 66 20 74 68 65 20 70 61 72 73 65  ize of the parse
2a9c0 72 20 73 74 61 63 6b 2e 0a 2a 2f 0a 73 74 61 74  r stack..*/.stat
2a9c1 69 63 20 76 6f 69 64 20 79 79 47 72 6f 77 53 74  ic void yyGrowSt
2a9c2 61 63 6b 28 79 79 50 61 72 73 65 72 20 2a 70 29  ack(yyParser *p)
2a9c3 7b 0a 20 20 69 6e 74 20 6e 65 77 53 69 7a 65 3b  {.  int newSize;
2a9c4 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
2a9c5 2a 70 4e 65 77 3b 0a 0a 20 20 6e 65 77 53 69 7a  *pNew;..  newSiz
2a9c6 65 20 3d 20 70 2d 3e 79 79 73 74 6b 73 7a 2a 32  e = p->yystksz*2
2a9c7 20 2b 20 31 30 30 3b 0a 20 20 70 4e 65 77 20 3d   + 100;.  pNew =
2a9c8 20 72 65 61 6c 6c 6f 63 28 70 2d 3e 79 79 73 74   realloc(p->yyst
2a9c9 61 63 6b 2c 20 6e 65 77 53 69 7a 65 2a 73 69 7a  ack, newSize*siz
2a9ca 65 6f 66 28 70 4e 65 77 5b 30 5d 29 29 3b 0a 20  eof(pNew[0]));. 
2a9cb 20 69 66 28 20 70 4e 65 77 20 29 7b 0a 20 20 20   if( pNew ){.   
2a9cc 20 70 2d 3e 79 79 73 74 61 63 6b 20 3d 20 70 4e   p->yystack = pN
2a9cd 65 77 3b 0a 20 20 20 20 70 2d 3e 79 79 73 74 6b  ew;.    p->yystk
2a9ce 73 7a 20 3d 20 6e 65 77 53 69 7a 65 3b 0a 23 69  sz = newSize;.#i
2a9cf 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
2a9d0 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c 45   if( yyTraceFILE
2a9d1 20 29 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74   ){.      fprint
2a9d2 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 25  f(yyTraceFILE,"%
2a9d3 73 53 74 61 63 6b 20 67 72 6f 77 73 20 74 6f 20  sStack grows to 
2a9d4 25 64 20 65 6e 74 72 69 65 73 21 5c 6e 22 2c 0a  %d entries!\n",.
2a9d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79                yy
2a9d6 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 70 2d 3e  TracePrompt, p->
2a9d7 79 79 73 74 6b 73 7a 29 3b 0a 20 20 20 20 7d 0a  yystksz);.    }.
2a9d8 23 65 6e 64 69 66 0a 20 20 7d 0a 7d 0a 23 65 6e  #endif.  }.}.#en
2a9d9 64 69 66 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73  dif../* .** This
2a9da 20 66 75 6e 63 74 69 6f 6e 20 61 6c 6c 6f 63 61   function alloca
2a9db 74 65 73 20 61 20 6e 65 77 20 70 61 72 73 65 72  tes a new parser
2a9dc 2e 0a 2a 2a 20 54 68 65 20 6f 6e 6c 79 20 61 72  ..** The only ar
2a9dd 67 75 6d 65 6e 74 20 69 73 20 61 20 70 6f 69 6e  gument is a poin
2a9de 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74 69 6f  ter to a functio
2a9df 6e 20 77 68 69 63 68 20 77 6f 72 6b 73 20 6c 69  n which works li
2a9e0 6b 65 0a 2a 2a 20 6d 61 6c 6c 6f 63 2e 0a 2a 2a  ke.** malloc..**
2a9e1 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a 2a 20 41  .** Inputs:.** A
2a9e2 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   pointer to the 
2a9e3 66 75 6e 63 74 69 6f 6e 20 75 73 65 64 20 74 6f  function used to
2a9e4 20 61 6c 6c 6f 63 61 74 65 20 6d 65 6d 6f 72 79   allocate memory
2a9e5 2e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73 3a  ..**.** Outputs:
2a9e6 0a 2a 2a 20 41 20 70 6f 69 6e 74 65 72 20 74 6f  .** A pointer to
2a9e7 20 61 20 70 61 72 73 65 72 2e 20 20 54 68 69 73   a parser.  This
2a9e8 20 70 6f 69 6e 74 65 72 20 69 73 20 75 73 65 64   pointer is used
2a9e9 20 69 6e 20 73 75 62 73 65 71 75 65 6e 74 20 63   in subsequent c
2a9ea 61 6c 6c 73 0a 2a 2a 20 74 6f 20 73 71 6c 69 74  alls.** to sqlit
2a9eb 65 33 50 61 72 73 65 72 20 61 6e 64 20 73 71 6c  e3Parser and sql
2a9ec 69 74 65 33 50 61 72 73 65 72 46 72 65 65 2e 0a  ite3ParserFree..
2a9ed 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2a9ee 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65 33 50  E void *sqlite3P
2a9ef 61 72 73 65 72 41 6c 6c 6f 63 28 76 6f 69 64 20  arserAlloc(void 
2a9f0 2a 28 2a 6d 61 6c 6c 6f 63 50 72 6f 63 29 28 73  *(*mallocProc)(s
2a9f1 69 7a 65 5f 74 29 29 7b 0a 20 20 79 79 50 61 72  ize_t)){.  yyPar
2a9f2 73 65 72 20 2a 70 50 61 72 73 65 72 3b 0a 20 20  ser *pParser;.  
2a9f3 70 50 61 72 73 65 72 20 3d 20 28 79 79 50 61 72  pParser = (yyPar
2a9f4 73 65 72 2a 29 28 2a 6d 61 6c 6c 6f 63 50 72 6f  ser*)(*mallocPro
2a9f5 63 29 28 20 28 73 69 7a 65 5f 74 29 73 69 7a 65  c)( (size_t)size
2a9f6 6f 66 28 79 79 50 61 72 73 65 72 29 20 29 3b 0a  of(yyParser) );.
2a9f7 20 20 69 66 28 20 70 50 61 72 73 65 72 20 29 7b    if( pParser ){
2a9f8 0a 20 20 20 20 70 50 61 72 73 65 72 2d 3e 79 79  .    pParser->yy
2a9f9 69 64 78 20 3d 20 2d 31 3b 0a 23 69 66 64 65 66  idx = -1;.#ifdef
2a9fa 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43 4b   YYTRACKMAXSTACK
2a9fb 44 45 50 54 48 0a 20 20 20 20 70 50 61 72 73 65  DEPTH.    pParse
2a9fc 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 30 3b  r->yyidxMax = 0;
2a9fd 0a 23 65 6e 64 69 66 0a 23 69 66 20 59 59 53 54  .#endif.#if YYST
2a9fe 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20 20 20 20  ACKDEPTH<=0.    
2a9ff 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b  pParser->yystack
2aa00 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 70 50 61   = NULL;.    pPa
2aa01 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 3d 20  rser->yystksz = 
2aa02 30 3b 0a 20 20 20 20 79 79 47 72 6f 77 53 74 61  0;.    yyGrowSta
2aa03 63 6b 28 70 50 61 72 73 65 72 29 3b 0a 23 65 6e  ck(pParser);.#en
2aa04 64 69 66 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  dif.  }.  return
2aa05 20 70 50 61 72 73 65 72 3b 0a 7d 0a 0a 2f 2a 20   pParser;.}../* 
2aa06 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 66 75  The following fu
2aa07 6e 63 74 69 6f 6e 20 64 65 6c 65 74 65 73 20 74  nction deletes t
2aa08 68 65 20 76 61 6c 75 65 20 61 73 73 6f 63 69 61  he value associa
2aa09 74 65 64 20 77 69 74 68 20 61 0a 2a 2a 20 73 79  ted with a.** sy
2aa0a 6d 62 6f 6c 2e 20 20 54 68 65 20 73 79 6d 62 6f  mbol.  The symbo
2aa0b 6c 20 63 61 6e 20 62 65 20 65 69 74 68 65 72 20  l can be either 
2aa0c 61 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  a terminal or no
2aa0d 6e 74 65 72 6d 69 6e 61 6c 2e 0a 2a 2a 20 22 79  nterminal..** "y
2aa0e 79 6d 61 6a 6f 72 22 20 69 73 20 74 68 65 20 73  ymajor" is the s
2aa0f 79 6d 62 6f 6c 20 63 6f 64 65 2c 20 61 6e 64 20  ymbol code, and 
2aa10 22 79 79 70 6d 69 6e 6f 72 22 20 69 73 20 61 20  "yypminor" is a 
2aa11 70 6f 69 6e 74 65 72 20 74 6f 0a 2a 2a 20 74 68  pointer to.** th
2aa12 65 20 76 61 6c 75 65 2e 0a 2a 2f 0a 73 74 61 74  e value..*/.stat
2aa13 69 63 20 76 6f 69 64 20 79 79 5f 64 65 73 74 72  ic void yy_destr
2aa14 75 63 74 6f 72 28 0a 20 20 79 79 50 61 72 73 65  uctor(.  yyParse
2aa15 72 20 2a 79 79 70 50 61 72 73 65 72 2c 20 20 20  r *yypParser,   
2aa16 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a   /* The parser *
2aa17 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20 79  /.  YYCODETYPE y
2aa18 79 6d 61 6a 6f 72 2c 20 20 20 20 20 2f 2a 20 54  ymajor,     /* T
2aa19 79 70 65 20 63 6f 64 65 20 66 6f 72 20 6f 62 6a  ype code for obj
2aa1a 65 63 74 20 74 6f 20 64 65 73 74 72 6f 79 20 2a  ect to destroy *
2aa1b 2f 0a 20 20 59 59 4d 49 4e 4f 52 54 59 50 45 20  /.  YYMINORTYPE 
2aa1c 2a 79 79 70 6d 69 6e 6f 72 20 20 20 2f 2a 20 54  *yypminor   /* T
2aa1d 68 65 20 6f 62 6a 65 63 74 20 74 6f 20 62 65 20  he object to be 
2aa1e 64 65 73 74 72 6f 79 65 64 20 2a 2f 0a 29 7b 0a  destroyed */.){.
2aa1f 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41    sqlite3ParserA
2aa20 52 47 5f 46 45 54 43 48 3b 0a 20 20 73 77 69 74  RG_FETCH;.  swit
2aa21 63 68 28 20 79 79 6d 61 6a 6f 72 20 29 7b 0a 20  ch( yymajor ){. 
2aa22 20 20 20 2f 2a 20 48 65 72 65 20 69 73 20 69 6e     /* Here is in
2aa23 73 65 72 74 65 64 20 74 68 65 20 61 63 74 69 6f  serted the actio
2aa24 6e 73 20 77 68 69 63 68 20 74 61 6b 65 20 70 6c  ns which take pl
2aa25 61 63 65 20 77 68 65 6e 20 61 0a 20 20 20 20 2a  ace when a.    *
2aa26 2a 20 74 65 72 6d 69 6e 61 6c 20 6f 72 20 6e 6f  * terminal or no
2aa27 6e 2d 74 65 72 6d 69 6e 61 6c 20 69 73 20 64 65  n-terminal is de
2aa28 73 74 72 6f 79 65 64 2e 20 20 54 68 69 73 20 63  stroyed.  This c
2aa29 61 6e 20 68 61 70 70 65 6e 0a 20 20 20 20 2a 2a  an happen.    **
2aa2a 20 77 68 65 6e 20 74 68 65 20 73 79 6d 62 6f 6c   when the symbol
2aa2b 20 69 73 20 70 6f 70 70 65 64 20 66 72 6f 6d 20   is popped from 
2aa2c 74 68 65 20 73 74 61 63 6b 20 64 75 72 69 6e 67  the stack during
2aa2d 20 61 0a 20 20 20 20 2a 2a 20 72 65 64 75 63 65   a.    ** reduce
2aa2e 20 6f 72 20 64 75 72 69 6e 67 20 65 72 72 6f 72   or during error
2aa2f 20 70 72 6f 63 65 73 73 69 6e 67 20 6f 72 20 77   processing or w
2aa30 68 65 6e 20 61 20 70 61 72 73 65 72 20 69 73 20  hen a parser is 
2aa31 0a 20 20 20 20 2a 2a 20 62 65 69 6e 67 20 64 65  .    ** being de
2aa32 73 74 72 6f 79 65 64 20 62 65 66 6f 72 65 20 69  stroyed before i
2aa33 74 20 69 73 20 66 69 6e 69 73 68 65 64 20 70 61  t is finished pa
2aa34 72 73 69 6e 67 2e 0a 20 20 20 20 2a 2a 0a 20 20  rsing..    **.  
2aa35 20 20 2a 2a 20 4e 6f 74 65 3a 20 64 75 72 69 6e    ** Note: durin
2aa36 67 20 61 20 72 65 64 75 63 65 2c 20 74 68 65 20  g a reduce, the 
2aa37 6f 6e 6c 79 20 73 79 6d 62 6f 6c 73 20 64 65 73  only symbols des
2aa38 74 72 6f 79 65 64 20 61 72 65 20 74 68 6f 73 65  troyed are those
2aa39 0a 20 20 20 20 2a 2a 20 77 68 69 63 68 20 61 70  .    ** which ap
2aa3a 70 65 61 72 20 6f 6e 20 74 68 65 20 52 48 53 20  pear on the RHS 
2aa3b 6f 66 20 74 68 65 20 72 75 6c 65 2c 20 62 75 74  of the rule, but
2aa3c 20 77 68 69 63 68 20 61 72 65 20 6e 6f 74 20 75   which are not u
2aa3d 73 65 64 0a 20 20 20 20 2a 2a 20 69 6e 73 69 64  sed.    ** insid
2aa3e 65 20 74 68 65 20 43 20 63 6f 64 65 2e 0a 20 20  e the C code..  
2aa3f 20 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 35    */.    case 15
2aa40 36 3a 20 2f 2a 20 73 65 6c 65 63 74 20 2a 2f 0a  6: /* select */.
2aa41 20 20 20 20 63 61 73 65 20 31 39 30 3a 20 2f 2a      case 190: /*
2aa42 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 20 20   oneselect */.  
2aa43 20 20 63 61 73 65 20 32 30 38 3a 20 2f 2a 20 73    case 208: /* s
2aa44 65 6c 74 61 62 6c 69 73 74 5f 70 61 72 65 6e 20  eltablist_paren 
2aa45 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 53 65 6c 65  */.{.sqlite3Sele
2aa46 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  ctDelete(pParse-
2aa47 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e  >db, (yypminor->
2aa48 79 79 34 33 29 29 3b 0a 7d 0a 20 20 20 20 20 20  yy43));.}.      
2aa49 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
2aa4a 31 37 30 3a 20 2f 2a 20 74 65 72 6d 20 2a 2f 0a  170: /* term */.
2aa4b 20 20 20 20 63 61 73 65 20 31 37 31 3a 20 2f 2a      case 171: /*
2aa4c 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 63 61 73   expr */.    cas
2aa4d 65 20 31 39 35 3a 20 2f 2a 20 77 68 65 72 65 5f  e 195: /* where_
2aa4e 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
2aa4f 31 39 37 3a 20 2f 2a 20 68 61 76 69 6e 67 5f 6f  197: /* having_o
2aa50 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  pt */.    case 2
2aa51 30 36 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20 2a 2f  06: /* on_opt */
2aa52 0a 20 20 20 20 63 61 73 65 20 32 31 32 3a 20 2f  .    case 212: /
2aa53 2a 20 73 6f 72 74 69 74 65 6d 20 2a 2f 0a 20 20  * sortitem */.  
2aa54 20 20 63 61 73 65 20 32 32 30 3a 20 2f 2a 20 65    case 220: /* e
2aa55 73 63 61 70 65 20 2a 2f 0a 20 20 20 20 63 61 73  scape */.    cas
2aa56 65 20 32 32 33 3a 20 2f 2a 20 63 61 73 65 5f 6f  e 223: /* case_o
2aa57 70 65 72 61 6e 64 20 2a 2f 0a 20 20 20 20 63 61  perand */.    ca
2aa58 73 65 20 32 32 35 3a 20 2f 2a 20 63 61 73 65 5f  se 225: /* case_
2aa59 65 6c 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65  else */.    case
2aa5a 20 32 33 36 3a 20 2f 2a 20 77 68 65 6e 5f 63 6c   236: /* when_cl
2aa5b 61 75 73 65 20 2a 2f 0a 20 20 20 20 63 61 73 65  ause */.    case
2aa5c 20 32 33 39 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74   239: /* key_opt
2aa5d 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 45 78 70   */.{.sqlite3Exp
2aa5e 72 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  rDelete(pParse->
2aa5f 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79  db, (yypminor->y
2aa60 79 34 35 30 29 29 3b 0a 7d 0a 20 20 20 20 20 20  y450));.}.      
2aa61 62 72 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20  break;.    case 
2aa62 31 37 35 3a 20 2f 2a 20 69 64 78 6c 69 73 74 5f  175: /* idxlist_
2aa63 6f 70 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20  opt */.    case 
2aa64 31 38 33 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20  183: /* idxlist 
2aa65 2a 2f 0a 20 20 20 20 63 61 73 65 20 31 39 33 3a  */.    case 193:
2aa66 20 2f 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 2a   /* selcollist *
2aa67 2f 0a 20 20 20 20 63 61 73 65 20 31 39 36 3a 20  /.    case 196: 
2aa68 2f 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 2a  /* groupby_opt *
2aa69 2f 0a 20 20 20 20 63 61 73 65 20 31 39 38 3a 20  /.    case 198: 
2aa6a 2f 2a 20 6f 72 64 65 72 62 79 5f 6f 70 74 20 2a  /* orderby_opt *
2aa6b 2f 0a 20 20 20 20 63 61 73 65 20 32 30 30 3a 20  /.    case 200: 
2aa6c 2f 2a 20 73 63 6c 70 20 2a 2f 0a 20 20 20 20 63  /* sclp */.    c
2aa6d 61 73 65 20 32 31 31 3a 20 2f 2a 20 73 6f 72 74  ase 211: /* sort
2aa6e 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65  list */.    case
2aa6f 20 32 31 33 3a 20 2f 2a 20 6e 65 78 70 72 6c 69   213: /* nexprli
2aa70 73 74 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 32  st */.    case 2
2aa71 31 34 3a 20 2f 2a 20 73 65 74 6c 69 73 74 20 2a  14: /* setlist *
2aa72 2f 0a 20 20 20 20 63 61 73 65 20 32 31 37 3a 20  /.    case 217: 
2aa73 2f 2a 20 69 74 65 6d 6c 69 73 74 20 2a 2f 0a 20  /* itemlist */. 
2aa74 20 20 20 63 61 73 65 20 32 31 38 3a 20 2f 2a 20     case 218: /* 
2aa75 65 78 70 72 6c 69 73 74 20 2a 2f 0a 20 20 20 20  exprlist */.    
2aa76 63 61 73 65 20 32 32 34 3a 20 2f 2a 20 63 61 73  case 224: /* cas
2aa77 65 5f 65 78 70 72 6c 69 73 74 20 2a 2f 0a 7b 0a  e_exprlist */.{.
2aa78 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2aa79 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2aa7a 2c 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 32  , (yypminor->yy2
2aa7b 34 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72  42));.}.      br
2aa7c 65 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 31 38  eak;.    case 18
2aa7d 39 3a 20 2f 2a 20 66 75 6c 6c 6e 61 6d 65 20 2a  9: /* fullname *
2aa7e 2f 0a 20 20 20 20 63 61 73 65 20 31 39 34 3a 20  /.    case 194: 
2aa7f 2f 2a 20 66 72 6f 6d 20 2a 2f 0a 20 20 20 20 63  /* from */.    c
2aa80 61 73 65 20 32 30 32 3a 20 2f 2a 20 73 65 6c 74  ase 202: /* selt
2aa81 61 62 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61  ablist */.    ca
2aa82 73 65 20 32 30 33 3a 20 2f 2a 20 73 74 6c 5f 70  se 203: /* stl_p
2aa83 72 65 66 69 78 20 2a 2f 0a 7b 0a 73 71 6c 69 74  refix */.{.sqlit
2aa84 65 33 53 72 63 4c 69 73 74 44 65 6c 65 74 65 28  e3SrcListDelete(
2aa85 70 50 61 72 73 65 2d 3e 64 62 2c 20 28 79 79 70  pParse->db, (yyp
2aa86 6d 69 6e 6f 72 2d 3e 79 79 34 31 39 29 29 3b 0a  minor->yy419));.
2aa87 7d 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  }.      break;. 
2aa88 20 20 20 63 61 73 65 20 32 30 37 3a 20 2f 2a 20     case 207: /* 
2aa89 75 73 69 6e 67 5f 6f 70 74 20 2a 2f 0a 20 20 20  using_opt */.   
2aa8a 20 63 61 73 65 20 32 31 30 3a 20 2f 2a 20 69 6e   case 210: /* in
2aa8b 73 63 6f 6c 6c 69 73 74 20 2a 2f 0a 20 20 20 20  scollist */.    
2aa8c 63 61 73 65 20 32 31 36 3a 20 2f 2a 20 69 6e 73  case 216: /* ins
2aa8d 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 2a 2f 0a 7b  collist_opt */.{
2aa8e 0a 73 71 6c 69 74 65 33 49 64 4c 69 73 74 44 65  .sqlite3IdListDe
2aa8f 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c  lete(pParse->db,
2aa90 20 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 33 35   (yypminor->yy35
2aa91 32 29 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65  2));.}.      bre
2aa92 61 6b 3b 0a 20 20 20 20 63 61 73 65 20 32 33 32  ak;.    case 232
2aa93 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63 6d 64  : /* trigger_cmd
2aa94 5f 6c 69 73 74 20 2a 2f 0a 20 20 20 20 63 61 73  _list */.    cas
2aa95 65 20 32 33 37 3a 20 2f 2a 20 74 72 69 67 67 65  e 237: /* trigge
2aa96 72 5f 63 6d 64 20 2a 2f 0a 7b 0a 73 71 6c 69 74  r_cmd */.{.sqlit
2aa97 65 33 44 65 6c 65 74 65 54 72 69 67 67 65 72 53  e3DeleteTriggerS
2aa98 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  tep(pParse->db, 
2aa99 28 79 79 70 6d 69 6e 6f 72 2d 3e 79 79 37 35 29  (yypminor->yy75)
2aa9a 29 3b 0a 7d 0a 20 20 20 20 20 20 62 72 65 61 6b  );.}.      break
2aa9b 3b 0a 20 20 20 20 63 61 73 65 20 32 33 34 3a 20  ;.    case 234: 
2aa9c 2f 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74  /* trigger_event
2aa9d 20 2a 2f 0a 7b 0a 73 71 6c 69 74 65 33 49 64 4c   */.{.sqlite3IdL
2aa9e 69 73 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  istDelete(pParse
2aa9f 2d 3e 64 62 2c 20 28 79 79 70 6d 69 6e 6f 72 2d  ->db, (yypminor-
2aaa0 3e 79 79 33 35 34 29 2e 62 29 3b 0a 7d 0a 20 20  >yy354).b);.}.  
2aaa1 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 64      break;.    d
2aaa2 65 66 61 75 6c 74 3a 20 20 62 72 65 61 6b 3b 20  efault:  break; 
2aaa3 20 20 2f 2a 20 49 66 20 6e 6f 20 64 65 73 74 72    /* If no destr
2aaa4 75 63 74 6f 72 20 61 63 74 69 6f 6e 20 73 70 65  uctor action spe
2aaa5 63 69 66 69 65 64 3a 20 64 6f 20 6e 6f 74 68 69  cified: do nothi
2aaa6 6e 67 20 2a 2f 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a  ng */.  }.}../*.
2aaa7 2a 2a 20 50 6f 70 20 74 68 65 20 70 61 72 73 65  ** Pop the parse
2aaa8 72 27 73 20 73 74 61 63 6b 20 6f 6e 63 65 2e 0a  r's stack once..
2aaa9 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20 69  **.** If there i
2aaaa 73 20 61 20 64 65 73 74 72 75 63 74 6f 72 20 72  s a destructor r
2aaab 6f 75 74 69 6e 65 20 61 73 73 6f 63 69 61 74 65  outine associate
2aaac 64 20 77 69 74 68 20 74 68 65 20 74 6f 6b 65 6e  d with the token
2aaad 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 70 6f 70   which.** is pop
2aaae 70 65 64 20 66 72 6f 6d 20 74 68 65 20 73 74 61  ped from the sta
2aaaf 63 6b 2c 20 74 68 65 6e 20 63 61 6c 6c 20 69 74  ck, then call it
2aab0 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74  ..**.** Return t
2aab1 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e  he major token n
2aab2 75 6d 62 65 72 20 66 6f 72 20 74 68 65 20 73 79  umber for the sy
2aab3 6d 62 6f 6c 20 70 6f 70 70 65 64 2e 0a 2a 2f 0a  mbol popped..*/.
2aab4 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 70 6f  static int yy_po
2aab5 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b 28 79  p_parser_stack(y
2aab6 79 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72  yParser *pParser
2aab7 29 7b 0a 20 20 59 59 43 4f 44 45 54 59 50 45 20  ){.  YYCODETYPE 
2aab8 79 79 6d 61 6a 6f 72 3b 0a 20 20 79 79 53 74 61  yymajor;.  yySta
2aab9 63 6b 45 6e 74 72 79 20 2a 79 79 74 6f 73 20 3d  ckEntry *yytos =
2aaba 20 26 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61   &pParser->yysta
2aabb 63 6b 5b 70 50 61 72 73 65 72 2d 3e 79 79 69 64  ck[pParser->yyid
2aabc 78 5d 3b 0a 0a 20 20 69 66 28 20 70 50 61 72 73  x];..  if( pPars
2aabd 65 72 2d 3e 79 79 69 64 78 3c 30 20 29 20 72 65  er->yyidx<0 ) re
2aabe 74 75 72 6e 20 30 3b 0a 23 69 66 6e 64 65 66 20  turn 0;.#ifndef 
2aabf 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79 54  NDEBUG.  if( yyT
2aac0 72 61 63 65 46 49 4c 45 20 26 26 20 70 50 61 72  raceFILE && pPar
2aac1 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 7b  ser->yyidx>=0 ){
2aac2 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79 54  .    fprintf(yyT
2aac3 72 61 63 65 46 49 4c 45 2c 22 25 73 50 6f 70 70  raceFILE,"%sPopp
2aac4 69 6e 67 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20  ing %s\n",.     
2aac5 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 0a   yyTracePrompt,.
2aac6 20 20 20 20 20 20 79 79 54 6f 6b 65 6e 4e 61 6d        yyTokenNam
2aac7 65 5b 79 79 74 6f 73 2d 3e 6d 61 6a 6f 72 5d 29  e[yytos->major])
2aac8 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79  ;.  }.#endif.  y
2aac9 79 6d 61 6a 6f 72 20 3d 20 79 79 74 6f 73 2d 3e  ymajor = yytos->
2aaca 6d 61 6a 6f 72 3b 0a 20 20 79 79 5f 64 65 73 74  major;.  yy_dest
2aacb 72 75 63 74 6f 72 28 70 50 61 72 73 65 72 2c 20  ructor(pParser, 
2aacc 79 79 6d 61 6a 6f 72 2c 20 26 79 79 74 6f 73 2d  yymajor, &yytos-
2aacd 3e 6d 69 6e 6f 72 29 3b 0a 20 20 70 50 61 72 73  >minor);.  pPars
2aace 65 72 2d 3e 79 79 69 64 78 2d 2d 3b 0a 20 20 72  er->yyidx--;.  r
2aacf 65 74 75 72 6e 20 79 79 6d 61 6a 6f 72 3b 0a 7d  eturn yymajor;.}
2aad0 0a 0a 2f 2a 20 0a 2a 2a 20 44 65 61 6c 6c 6f 63  ../* .** Dealloc
2aad1 61 74 65 20 61 6e 64 20 64 65 73 74 72 6f 79 20  ate and destroy 
2aad2 61 20 70 61 72 73 65 72 2e 20 20 44 65 73 74 72  a parser.  Destr
2aad3 75 63 74 6f 72 73 20 61 72 65 20 61 6c 6c 20 63  uctors are all c
2aad4 61 6c 6c 65 64 20 66 6f 72 0a 2a 2a 20 61 6c 6c  alled for.** all
2aad5 20 73 74 61 63 6b 20 65 6c 65 6d 65 6e 74 73 20   stack elements 
2aad6 62 65 66 6f 72 65 20 73 68 75 74 74 69 6e 67 20  before shutting 
2aad7 74 68 65 20 70 61 72 73 65 72 20 64 6f 77 6e 2e  the parser down.
2aad8 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74 73 3a 0a 2a  .**.** Inputs:.*
2aad9 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c 69 3e 20 20  * <ul>.** <li>  
2aada 41 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65  A pointer to the
2aadb 20 70 61 72 73 65 72 2e 20 20 54 68 69 73 20 73   parser.  This s
2aadc 68 6f 75 6c 64 20 62 65 20 61 20 70 6f 69 6e 74  hould be a point
2aadd 65 72 0a 2a 2a 20 20 20 20 20 20 20 6f 62 74 61  er.**       obta
2aade 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65  ined from sqlite
2aadf 33 50 61 72 73 65 72 41 6c 6c 6f 63 2e 0a 2a 2a  3ParserAlloc..**
2aae0 20 3c 6c 69 3e 20 20 41 20 70 6f 69 6e 74 65 72   <li>  A pointer
2aae1 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 75   to a function u
2aae2 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  sed to reclaim m
2aae3 65 6d 6f 72 79 20 6f 62 74 61 69 6e 65 64 0a 2a  emory obtained.*
2aae4 2a 20 20 20 20 20 20 20 66 72 6f 6d 20 6d 61 6c  *       from mal
2aae5 6c 6f 63 2e 0a 2a 2a 20 3c 2f 75 6c 3e 0a 2a 2f  loc..** </ul>.*/
2aae6 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20  .SQLITE_PRIVATE 
2aae7 76 6f 69 64 20 73 71 6c 69 74 65 33 50 61 72 73  void sqlite3Pars
2aae8 65 72 46 72 65 65 28 0a 20 20 76 6f 69 64 20 2a  erFree(.  void *
2aae9 70 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  p,              
2aaea 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 61 72        /* The par
2aaeb 73 65 72 20 74 6f 20 62 65 20 64 65 6c 65 74 65  ser to be delete
2aaec 64 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 66 72  d */.  void (*fr
2aaed 65 65 50 72 6f 63 29 28 76 6f 69 64 2a 29 20 20  eeProc)(void*)  
2aaee 20 20 20 2f 2a 20 46 75 6e 63 74 69 6f 6e 20 75     /* Function u
2aaef 73 65 64 20 74 6f 20 72 65 63 6c 61 69 6d 20 6d  sed to reclaim m
2aaf0 65 6d 6f 72 79 20 2a 2f 0a 29 7b 0a 20 20 79 79  emory */.){.  yy
2aaf1 50 61 72 73 65 72 20 2a 70 50 61 72 73 65 72 20  Parser *pParser 
2aaf2 3d 20 28 79 79 50 61 72 73 65 72 2a 29 70 3b 0a  = (yyParser*)p;.
2aaf3 20 20 69 66 28 20 70 50 61 72 73 65 72 3d 3d 30    if( pParser==0
2aaf4 20 29 20 72 65 74 75 72 6e 3b 0a 20 20 77 68 69   ) return;.  whi
2aaf5 6c 65 28 20 70 50 61 72 73 65 72 2d 3e 79 79 69  le( pParser->yyi
2aaf6 64 78 3e 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70  dx>=0 ) yy_pop_p
2aaf7 61 72 73 65 72 5f 73 74 61 63 6b 28 70 50 61 72  arser_stack(pPar
2aaf8 73 65 72 29 3b 0a 23 69 66 20 59 59 53 54 41 43  ser);.#if YYSTAC
2aaf9 4b 44 45 50 54 48 3c 3d 30 0a 20 20 66 72 65 65  KDEPTH<=0.  free
2aafa 28 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61 63  (pParser->yystac
2aafb 6b 29 3b 0a 23 65 6e 64 69 66 0a 20 20 28 2a 66  k);.#endif.  (*f
2aafc 72 65 65 50 72 6f 63 29 28 28 76 6f 69 64 2a 29  reeProc)((void*)
2aafd 70 50 61 72 73 65 72 29 3b 0a 7d 0a 0a 2f 2a 0a  pParser);.}../*.
2aafe 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 70 65  ** Return the pe
2aaff 61 6b 20 64 65 70 74 68 20 6f 66 20 74 68 65 20  ak depth of the 
2ab00 73 74 61 63 6b 20 66 6f 72 20 61 20 70 61 72 73  stack for a pars
2ab01 65 72 2e 0a 2a 2f 0a 23 69 66 64 65 66 20 59 59  er..*/.#ifdef YY
2ab02 54 52 41 43 4b 4d 41 58 53 54 41 43 4b 44 45 50  TRACKMAXSTACKDEP
2ab03 54 48 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  TH.SQLITE_PRIVAT
2ab04 45 20 69 6e 74 20 73 71 6c 69 74 65 33 50 61 72  E int sqlite3Par
2ab05 73 65 72 53 74 61 63 6b 50 65 61 6b 28 76 6f 69  serStackPeak(voi
2ab06 64 20 2a 70 29 7b 0a 20 20 79 79 50 61 72 73 65  d *p){.  yyParse
2ab07 72 20 2a 70 50 61 72 73 65 72 20 3d 20 28 79 79  r *pParser = (yy
2ab08 50 61 72 73 65 72 2a 29 70 3b 0a 20 20 72 65 74  Parser*)p;.  ret
2ab09 75 72 6e 20 70 50 61 72 73 65 72 2d 3e 79 79 69  urn pParser->yyi
2ab0a 64 78 4d 61 78 3b 0a 7d 0a 23 65 6e 64 69 66 0a  dxMax;.}.#endif.
2ab0b 0a 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20  ./*.** Find the 
2ab0c 61 70 70 72 6f 70 72 69 61 74 65 20 61 63 74 69  appropriate acti
2ab0d 6f 6e 20 66 6f 72 20 61 20 70 61 72 73 65 72 20  on for a parser 
2ab0e 67 69 76 65 6e 20 74 68 65 20 74 65 72 6d 69 6e  given the termin
2ab0f 61 6c 0a 2a 2a 20 6c 6f 6f 6b 2d 61 68 65 61 64  al.** look-ahead
2ab10 20 74 6f 6b 65 6e 20 69 4c 6f 6f 6b 41 68 65 61   token iLookAhea
2ab11 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 20  d..**.** If the 
2ab12 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e  look-ahead token
2ab13 20 69 73 20 59 59 4e 4f 43 4f 44 45 2c 20 74 68   is YYNOCODE, th
2ab14 65 6e 20 63 68 65 63 6b 20 74 6f 20 73 65 65 20  en check to see 
2ab15 69 66 20 74 68 65 20 61 63 74 69 6f 6e 20 69 73  if the action is
2ab16 0a 2a 2a 20 69 6e 64 65 70 65 6e 64 65 6e 74 20  .** independent 
2ab17 6f 66 20 74 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  of the look-ahea
2ab18 64 2e 20 20 49 66 20 69 74 20 69 73 2c 20 72 65  d.  If it is, re
2ab19 74 75 72 6e 20 74 68 65 20 61 63 74 69 6f 6e 2c  turn the action,
2ab1a 20 6f 74 68 65 72 77 69 73 65 0a 2a 2a 20 72 65   otherwise.** re
2ab1b 74 75 72 6e 20 59 59 5f 4e 4f 5f 41 43 54 49 4f  turn YY_NO_ACTIO
2ab1c 4e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  N..*/.static int
2ab1d 20 79 79 5f 66 69 6e 64 5f 73 68 69 66 74 5f 61   yy_find_shift_a
2ab1e 63 74 69 6f 6e 28 0a 20 20 79 79 50 61 72 73 65  ction(.  yyParse
2ab1f 72 20 2a 70 50 61 72 73 65 72 2c 20 20 20 20 20  r *pParser,     
2ab20 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2ab21 20 2a 2f 0a 20 20 59 59 43 4f 44 45 54 59 50 45   */.  YYCODETYPE
2ab22 20 69 4c 6f 6f 6b 41 68 65 61 64 20 20 20 20 20   iLookAhead     
2ab23 2f 2a 20 54 68 65 20 6c 6f 6f 6b 2d 61 68 65 61  /* The look-ahea
2ab24 64 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b 0a 20 20  d token */.){.  
2ab25 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 73 74 61  int i;.  int sta
2ab26 74 65 6e 6f 20 3d 20 70 50 61 72 73 65 72 2d 3e  teno = pParser->
2ab27 79 79 73 74 61 63 6b 5b 70 50 61 72 73 65 72 2d  yystack[pParser-
2ab28 3e 79 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f 3b  >yyidx].stateno;
2ab29 0a 20 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f  . .  if( stateno
2ab2a 3e 59 59 5f 53 48 49 46 54 5f 4d 41 58 20 7c 7c  >YY_SHIFT_MAX ||
2ab2b 20 28 69 20 3d 20 79 79 5f 73 68 69 66 74 5f 6f   (i = yy_shift_o
2ab2c 66 73 74 5b 73 74 61 74 65 6e 6f 5d 29 3d 3d 59  fst[stateno])==Y
2ab2d 59 5f 53 48 49 46 54 5f 55 53 45 5f 44 46 4c 54  Y_SHIFT_USE_DFLT
2ab2e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 79   ){.    return y
2ab2f 79 5f 64 65 66 61 75 6c 74 5b 73 74 61 74 65 6e  y_default[staten
2ab30 6f 5d 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74  o];.  }.  assert
2ab31 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59  ( iLookAhead!=YY
2ab32 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d  NOCODE );.  i +=
2ab33 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 20 20 69   iLookAhead;.  i
2ab34 66 28 20 69 3c 30 20 7c 7c 20 69 3e 3d 59 59 5f  f( i<0 || i>=YY_
2ab35 53 5a 5f 41 43 54 54 41 42 20 7c 7c 20 79 79 5f  SZ_ACTTAB || yy_
2ab36 6c 6f 6f 6b 61 68 65 61 64 5b 69 5d 21 3d 69 4c  lookahead[i]!=iL
2ab37 6f 6f 6b 41 68 65 61 64 20 29 7b 0a 20 20 20 20  ookAhead ){.    
2ab38 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3e 30  if( iLookAhead>0
2ab39 20 29 7b 0a 23 69 66 64 65 66 20 59 59 46 41 4c   ){.#ifdef YYFAL
2ab3a 4c 42 41 43 4b 0a 20 20 20 20 20 20 69 6e 74 20  LBACK.      int 
2ab3b 69 46 61 6c 6c 62 61 63 6b 3b 20 20 20 20 20 20  iFallback;      
2ab3c 20 20 20 20 20 20 2f 2a 20 46 61 6c 6c 62 61 63        /* Fallbac
2ab3d 6b 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20 20 20  k token */.     
2ab3e 20 69 66 28 20 69 4c 6f 6f 6b 41 68 65 61 64 3c   if( iLookAhead<
2ab3f 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c 62 61 63  sizeof(yyFallbac
2ab40 6b 29 2f 73 69 7a 65 6f 66 28 79 79 46 61 6c 6c  k)/sizeof(yyFall
2ab41 62 61 63 6b 5b 30 5d 29 0a 20 20 20 20 20 20 20  back[0]).       
2ab42 20 20 20 20 20 20 26 26 20 28 69 46 61 6c 6c 62        && (iFallb
2ab43 61 63 6b 20 3d 20 79 79 46 61 6c 6c 62 61 63 6b  ack = yyFallback
2ab44 5b 69 4c 6f 6f 6b 41 68 65 61 64 5d 29 21 3d 30  [iLookAhead])!=0
2ab45 20 29 7b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42   ){.#ifndef NDEB
2ab46 55 47 0a 20 20 20 20 20 20 20 20 69 66 28 20 79  UG.        if( y
2ab47 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
2ab48 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28          fprintf(
2ab49 79 79 54 72 61 63 65 46 49 4c 45 2c 20 22 25 73  yyTraceFILE, "%s
2ab4a 46 41 4c 4c 42 41 43 4b 20 25 73 20 3d 3e 20 25  FALLBACK %s => %
2ab4b 73 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20  s\n",.          
2ab4c 20 20 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74     yyTracePrompt
2ab4d 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 69 4c  , yyTokenName[iL
2ab4e 6f 6f 6b 41 68 65 61 64 5d 2c 20 79 79 54 6f 6b  ookAhead], yyTok
2ab4f 65 6e 4e 61 6d 65 5b 69 46 61 6c 6c 62 61 63 6b  enName[iFallback
2ab50 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 23 65  ]);.        }.#e
2ab51 6e 64 69 66 0a 20 20 20 20 20 20 20 20 72 65 74  ndif.        ret
2ab52 75 72 6e 20 79 79 5f 66 69 6e 64 5f 73 68 69 66  urn yy_find_shif
2ab53 74 5f 61 63 74 69 6f 6e 28 70 50 61 72 73 65 72  t_action(pParser
2ab54 2c 20 69 46 61 6c 6c 62 61 63 6b 29 3b 0a 20 20  , iFallback);.  
2ab55 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66      }.#endif.#if
2ab56 64 65 66 20 59 59 57 49 4c 44 43 41 52 44 0a 20  def YYWILDCARD. 
2ab57 20 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 69       {.        i
2ab58 6e 74 20 6a 20 3d 20 69 20 2d 20 69 4c 6f 6f 6b  nt j = i - iLook
2ab59 41 68 65 61 64 20 2b 20 59 59 57 49 4c 44 43 41  Ahead + YYWILDCA
2ab5a 52 44 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  RD;.        if( 
2ab5b 6a 3e 3d 30 20 26 26 20 6a 3c 59 59 5f 53 5a 5f  j>=0 && j<YY_SZ_
2ab5c 41 43 54 54 41 42 20 26 26 20 79 79 5f 6c 6f 6f  ACTTAB && yy_loo
2ab5d 6b 61 68 65 61 64 5b 6a 5d 3d 3d 59 59 57 49 4c  kahead[j]==YYWIL
2ab5e 44 43 41 52 44 20 29 7b 0a 23 69 66 6e 64 65 66  DCARD ){.#ifndef
2ab5f 20 4e 44 45 42 55 47 0a 20 20 20 20 20 20 20 20   NDEBUG.        
2ab60 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49 4c    if( yyTraceFIL
2ab61 45 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20  E ){.           
2ab62 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2ab63 46 49 4c 45 2c 20 22 25 73 57 49 4c 44 43 41 52  FILE, "%sWILDCAR
2ab64 44 20 25 73 20 3d 3e 20 25 73 5c 6e 22 2c 0a 20  D %s => %s\n",. 
2ab65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 79 79                yy
2ab66 54 72 61 63 65 50 72 6f 6d 70 74 2c 20 79 79 54  TracePrompt, yyT
2ab67 6f 6b 65 6e 4e 61 6d 65 5b 69 4c 6f 6f 6b 41 68  okenName[iLookAh
2ab68 65 61 64 5d 2c 20 79 79 54 6f 6b 65 6e 4e 61 6d  ead], yyTokenNam
2ab69 65 5b 59 59 57 49 4c 44 43 41 52 44 5d 29 3b 0a  e[YYWILDCARD]);.
2ab6a 20 20 20 20 20 20 20 20 20 20 7d 0a 23 65 6e 64            }.#end
2ab6b 69 66 20 2f 2a 20 4e 44 45 42 55 47 20 2a 2f 0a  if /* NDEBUG */.
2ab6c 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
2ab6d 20 79 79 5f 61 63 74 69 6f 6e 5b 6a 5d 3b 0a 20   yy_action[j];. 
2ab6e 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
2ab6f 0a 23 65 6e 64 69 66 20 2f 2a 20 59 59 57 49 4c  .#endif /* YYWIL
2ab70 44 43 41 52 44 20 2a 2f 0a 20 20 20 20 7d 0a 20  DCARD */.    }. 
2ab71 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64 65 66     return yy_def
2ab72 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b 0a 20  ault[stateno];. 
2ab73 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 65 74 75   }else{.    retu
2ab74 72 6e 20 79 79 5f 61 63 74 69 6f 6e 5b 69 5d 3b  rn yy_action[i];
2ab75 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 69  .  }.}../*.** Fi
2ab76 6e 64 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  nd the appropria
2ab77 74 65 20 61 63 74 69 6f 6e 20 66 6f 72 20 61 20  te action for a 
2ab78 70 61 72 73 65 72 20 67 69 76 65 6e 20 74 68 65  parser given the
2ab79 20 6e 6f 6e 2d 74 65 72 6d 69 6e 61 6c 0a 2a 2a   non-terminal.**
2ab7a 20 6c 6f 6f 6b 2d 61 68 65 61 64 20 74 6f 6b 65   look-ahead toke
2ab7b 6e 20 69 4c 6f 6f 6b 41 68 65 61 64 2e 0a 2a 2a  n iLookAhead..**
2ab7c 0a 2a 2a 20 49 66 20 74 68 65 20 6c 6f 6f 6b 2d  .** If the look-
2ab7d 61 68 65 61 64 20 74 6f 6b 65 6e 20 69 73 20 59  ahead token is Y
2ab7e 59 4e 4f 43 4f 44 45 2c 20 74 68 65 6e 20 63 68  YNOCODE, then ch
2ab7f 65 63 6b 20 74 6f 20 73 65 65 20 69 66 20 74 68  eck to see if th
2ab80 65 20 61 63 74 69 6f 6e 20 69 73 0a 2a 2a 20 69  e action is.** i
2ab81 6e 64 65 70 65 6e 64 65 6e 74 20 6f 66 20 74 68  ndependent of th
2ab82 65 20 6c 6f 6f 6b 2d 61 68 65 61 64 2e 20 20 49  e look-ahead.  I
2ab83 66 20 69 74 20 69 73 2c 20 72 65 74 75 72 6e 20  f it is, return 
2ab84 74 68 65 20 61 63 74 69 6f 6e 2c 20 6f 74 68 65  the action, othe
2ab85 72 77 69 73 65 0a 2a 2a 20 72 65 74 75 72 6e 20  rwise.** return 
2ab86 59 59 5f 4e 4f 5f 41 43 54 49 4f 4e 2e 0a 2a 2f  YY_NO_ACTION..*/
2ab87 0a 73 74 61 74 69 63 20 69 6e 74 20 79 79 5f 66  .static int yy_f
2ab88 69 6e 64 5f 72 65 64 75 63 65 5f 61 63 74 69 6f  ind_reduce_actio
2ab89 6e 28 0a 20 20 69 6e 74 20 73 74 61 74 65 6e 6f  n(.  int stateno
2ab8a 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2ab8b 2a 20 43 75 72 72 65 6e 74 20 73 74 61 74 65 20  * Current state 
2ab8c 6e 75 6d 62 65 72 20 2a 2f 0a 20 20 59 59 43 4f  number */.  YYCO
2ab8d 44 45 54 59 50 45 20 69 4c 6f 6f 6b 41 68 65 61  DETYPE iLookAhea
2ab8e 64 20 20 20 20 20 2f 2a 20 54 68 65 20 6c 6f 6f  d     /* The loo
2ab8f 6b 2d 61 68 65 61 64 20 74 6f 6b 65 6e 20 2a 2f  k-ahead token */
2ab90 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a 23 69 66  .){.  int i;.#if
2ab91 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f  def YYERRORSYMBO
2ab92 4c 0a 20 20 69 66 28 20 73 74 61 74 65 6e 6f 3e  L.  if( stateno>
2ab93 59 59 5f 52 45 44 55 43 45 5f 4d 41 58 20 29 7b  YY_REDUCE_MAX ){
2ab94 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
2ab95 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
2ab96 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73  .  }.#else.  ass
2ab97 65 72 74 28 20 73 74 61 74 65 6e 6f 3c 3d 59 59  ert( stateno<=YY
2ab98 5f 52 45 44 55 43 45 5f 4d 41 58 20 29 3b 0a 23  _REDUCE_MAX );.#
2ab99 65 6e 64 69 66 0a 20 20 69 20 3d 20 79 79 5f 72  endif.  i = yy_r
2ab9a 65 64 75 63 65 5f 6f 66 73 74 5b 73 74 61 74 65  educe_ofst[state
2ab9b 6e 6f 5d 3b 0a 20 20 61 73 73 65 72 74 28 20 69  no];.  assert( i
2ab9c 21 3d 59 59 5f 52 45 44 55 43 45 5f 55 53 45 5f  !=YY_REDUCE_USE_
2ab9d 44 46 4c 54 20 29 3b 0a 20 20 61 73 73 65 72 74  DFLT );.  assert
2ab9e 28 20 69 4c 6f 6f 6b 41 68 65 61 64 21 3d 59 59  ( iLookAhead!=YY
2ab9f 4e 4f 43 4f 44 45 20 29 3b 0a 20 20 69 20 2b 3d  NOCODE );.  i +=
2aba0 20 69 4c 6f 6f 6b 41 68 65 61 64 3b 0a 23 69 66   iLookAhead;.#if
2aba1 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f  def YYERRORSYMBO
2aba2 4c 0a 20 20 69 66 28 20 69 3c 30 20 7c 7c 20 69  L.  if( i<0 || i
2aba3 3e 3d 59 59 5f 53 5a 5f 41 43 54 54 41 42 20 7c  >=YY_SZ_ACTTAB |
2aba4 7c 20 79 79 5f 6c 6f 6f 6b 61 68 65 61 64 5b 69  | yy_lookahead[i
2aba5 5d 21 3d 69 4c 6f 6f 6b 41 68 65 61 64 20 29 7b  ]!=iLookAhead ){
2aba6 0a 20 20 20 20 72 65 74 75 72 6e 20 79 79 5f 64  .    return yy_d
2aba7 65 66 61 75 6c 74 5b 73 74 61 74 65 6e 6f 5d 3b  efault[stateno];
2aba8 0a 20 20 7d 0a 23 65 6c 73 65 0a 20 20 61 73 73  .  }.#else.  ass
2aba9 65 72 74 28 20 69 3e 3d 30 20 26 26 20 69 3c 59  ert( i>=0 && i<Y
2abaa 59 5f 53 5a 5f 41 43 54 54 41 42 20 29 3b 0a 20  Y_SZ_ACTTAB );. 
2abab 20 61 73 73 65 72 74 28 20 79 79 5f 6c 6f 6f 6b   assert( yy_look
2abac 61 68 65 61 64 5b 69 5d 3d 3d 69 4c 6f 6f 6b 41  ahead[i]==iLookA
2abad 68 65 61 64 20 29 3b 0a 23 65 6e 64 69 66 0a 20  head );.#endif. 
2abae 20 72 65 74 75 72 6e 20 79 79 5f 61 63 74 69 6f   return yy_actio
2abaf 6e 5b 69 5d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n[i];.}../*.** T
2abb0 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75  he following rou
2abb1 74 69 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 69  tine is called i
2abb2 66 20 74 68 65 20 73 74 61 63 6b 20 6f 76 65 72  f the stack over
2abb3 66 6c 6f 77 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  flows..*/.static
2abb4 20 76 6f 69 64 20 79 79 53 74 61 63 6b 4f 76 65   void yyStackOve
2abb5 72 66 6c 6f 77 28 79 79 50 61 72 73 65 72 20 2a  rflow(yyParser *
2abb6 79 79 70 50 61 72 73 65 72 2c 20 59 59 4d 49 4e  yypParser, YYMIN
2abb7 4f 52 54 59 50 45 20 2a 79 79 70 4d 69 6e 6f 72  ORTYPE *yypMinor
2abb8 29 7b 0a 20 20 20 73 71 6c 69 74 65 33 50 61 72  ){.   sqlite3Par
2abb9 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 20 20  serARG_FETCH;.  
2abba 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64   yypParser->yyid
2abbb 78 2d 2d 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45  x--;.#ifndef NDE
2abbc 42 55 47 0a 20 20 20 69 66 28 20 79 79 54 72 61  BUG.   if( yyTra
2abbd 63 65 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 66  ceFILE ){.     f
2abbe 70 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49  printf(yyTraceFI
2abbf 4c 45 2c 22 25 73 53 74 61 63 6b 20 4f 76 65 72  LE,"%sStack Over
2abc0 66 6c 6f 77 21 5c 6e 22 2c 79 79 54 72 61 63 65  flow!\n",yyTrace
2abc1 50 72 6f 6d 70 74 29 3b 0a 20 20 20 7d 0a 23 65  Prompt);.   }.#e
2abc2 6e 64 69 66 0a 20 20 20 77 68 69 6c 65 28 20 79  ndif.   while( y
2abc3 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e  ypParser->yyidx>
2abc4 3d 30 20 29 20 79 79 5f 70 6f 70 5f 70 61 72 73  =0 ) yy_pop_pars
2abc5 65 72 5f 73 74 61 63 6b 28 79 79 70 50 61 72 73  er_stack(yypPars
2abc6 65 72 29 3b 0a 20 20 20 2f 2a 20 48 65 72 65 20  er);.   /* Here 
2abc7 63 6f 64 65 20 69 73 20 69 6e 73 65 72 74 65 64  code is inserted
2abc8 20 77 68 69 63 68 20 77 69 6c 6c 20 65 78 65 63   which will exec
2abc9 75 74 65 20 69 66 20 74 68 65 20 70 61 72 73 65  ute if the parse
2abca 72 0a 20 20 20 2a 2a 20 73 74 61 63 6b 20 65 76  r.   ** stack ev
2abcb 65 72 79 20 6f 76 65 72 66 6c 6f 77 73 20 2a 2f  ery overflows */
2abcc 0a 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72  ..  sqlite3Error
2abcd 4d 73 67 28 70 50 61 72 73 65 2c 20 22 70 61 72  Msg(pParse, "par
2abce 73 65 72 20 73 74 61 63 6b 20 6f 76 65 72 66 6c  ser stack overfl
2abcf 6f 77 22 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e  ow");.  pParse->
2abd0 70 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a  parseError = 1;.
2abd1 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72     sqlite3Parser
2abd2 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75  ARG_STORE; /* Su
2abd3 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61  ppress warning a
2abd4 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65 78 74  bout unused %ext
2abd5 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 20  ra_argument var 
2abd6 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 65 72 66  */.}../*.** Perf
2abd7 6f 72 6d 20 61 20 73 68 69 66 74 20 61 63 74 69  orm a shift acti
2abd8 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  on..*/.static vo
2abd9 69 64 20 79 79 5f 73 68 69 66 74 28 0a 20 20 79  id yy_shift(.  y
2abda 79 50 61 72 73 65 72 20 2a 79 79 70 50 61 72 73  yParser *yypPars
2abdb 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20  er,          /* 
2abdc 54 68 65 20 70 61 72 73 65 72 20 74 6f 20 62 65  The parser to be
2abdd 20 73 68 69 66 74 65 64 20 2a 2f 0a 20 20 69 6e   shifted */.  in
2abde 74 20 79 79 4e 65 77 53 74 61 74 65 2c 20 20 20  t yyNewState,   
2abdf 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2abe0 68 65 20 6e 65 77 20 73 74 61 74 65 20 74 6f 20  he new state to 
2abe1 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20 69 6e  shift in */.  in
2abe2 74 20 79 79 4d 61 6a 6f 72 2c 20 20 20 20 20 20  t yyMajor,      
2abe3 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2abe4 68 65 20 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 74  he major token t
2abe5 6f 20 73 68 69 66 74 20 69 6e 20 2a 2f 0a 20 20  o shift in */.  
2abe6 59 59 4d 49 4e 4f 52 54 59 50 45 20 2a 79 79 70  YYMINORTYPE *yyp
2abe7 4d 69 6e 6f 72 20 20 20 20 20 20 20 20 20 2f 2a  Minor         /*
2abe8 20 50 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20   Pointer to the 
2abe9 6d 69 6e 6f 72 20 74 6f 6b 65 6e 20 74 6f 20 73  minor token to s
2abea 68 69 66 74 20 69 6e 20 2a 2f 0a 29 7b 0a 20 20  hift in */.){.  
2abeb 79 79 53 74 61 63 6b 45 6e 74 72 79 20 2a 79 79  yyStackEntry *yy
2abec 74 6f 73 3b 0a 20 20 79 79 70 50 61 72 73 65 72  tos;.  yypParser
2abed 2d 3e 79 79 69 64 78 2b 2b 3b 0a 23 69 66 64 65  ->yyidx++;.#ifde
2abee 66 20 59 59 54 52 41 43 4b 4d 41 58 53 54 41 43  f YYTRACKMAXSTAC
2abef 4b 44 45 50 54 48 0a 20 20 69 66 28 20 79 79 70  KDEPTH.  if( yyp
2abf0 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 79 79  Parser->yyidx>yy
2abf1 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 4d 61  pParser->yyidxMa
2abf2 78 20 29 7b 0a 20 20 20 20 79 79 70 50 61 72 73  x ){.    yypPars
2abf3 65 72 2d 3e 79 79 69 64 78 4d 61 78 20 3d 20 79  er->yyidxMax = y
2abf4 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b  ypParser->yyidx;
2abf5 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66 20  .  }.#endif.#if 
2abf6 59 59 53 54 41 43 4b 44 45 50 54 48 3e 30 20 0a  YYSTACKDEPTH>0 .
2abf7 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d    if( yypParser-
2abf8 3e 79 79 69 64 78 3e 3d 59 59 53 54 41 43 4b 44  >yyidx>=YYSTACKD
2abf9 45 50 54 48 20 29 7b 0a 20 20 20 20 79 79 53 74  EPTH ){.    yySt
2abfa 61 63 6b 4f 76 65 72 66 6c 6f 77 28 79 79 70 50  ackOverflow(yypP
2abfb 61 72 73 65 72 2c 20 79 79 70 4d 69 6e 6f 72 29  arser, yypMinor)
2abfc 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20  ;.    return;.  
2abfd 7d 0a 23 65 6c 73 65 0a 20 20 69 66 28 20 79 79  }.#else.  if( yy
2abfe 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3e 3d  pParser->yyidx>=
2abff 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 6b  yypParser->yystk
2ac00 73 7a 20 29 7b 0a 20 20 20 20 79 79 47 72 6f 77  sz ){.    yyGrow
2ac01 53 74 61 63 6b 28 79 79 70 50 61 72 73 65 72 29  Stack(yypParser)
2ac02 3b 0a 20 20 20 20 69 66 28 20 79 79 70 50 61 72  ;.    if( yypPar
2ac03 73 65 72 2d 3e 79 79 69 64 78 3e 3d 79 79 70 50  ser->yyidx>=yypP
2ac04 61 72 73 65 72 2d 3e 79 79 73 74 6b 73 7a 20 29  arser->yystksz )
2ac05 7b 0a 20 20 20 20 20 20 79 79 53 74 61 63 6b 4f  {.      yyStackO
2ac06 76 65 72 66 6c 6f 77 28 79 79 70 50 61 72 73 65  verflow(yypParse
2ac07 72 2c 20 79 79 70 4d 69 6e 6f 72 29 3b 0a 20 20  r, yypMinor);.  
2ac08 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2ac09 7d 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 79  }.  }.#endif.  y
2ac0a 79 74 6f 73 20 3d 20 26 79 79 70 50 61 72 73 65  ytos = &yypParse
2ac0b 72 2d 3e 79 79 73 74 61 63 6b 5b 79 79 70 50 61  r->yystack[yypPa
2ac0c 72 73 65 72 2d 3e 79 79 69 64 78 5d 3b 0a 20 20  rser->yyidx];.  
2ac0d 79 79 74 6f 73 2d 3e 73 74 61 74 65 6e 6f 20 3d  yytos->stateno =
2ac0e 20 79 79 4e 65 77 53 74 61 74 65 3b 0a 20 20 79   yyNewState;.  y
2ac0f 79 74 6f 73 2d 3e 6d 61 6a 6f 72 20 3d 20 79 79  ytos->major = yy
2ac10 4d 61 6a 6f 72 3b 0a 20 20 79 79 74 6f 73 2d 3e  Major;.  yytos->
2ac11 6d 69 6e 6f 72 20 3d 20 2a 79 79 70 4d 69 6e 6f  minor = *yypMino
2ac12 72 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  r;.#ifndef NDEBU
2ac13 47 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46  G.  if( yyTraceF
2ac14 49 4c 45 20 26 26 20 79 79 70 50 61 72 73 65 72  ILE && yypParser
2ac15 2d 3e 79 79 69 64 78 3e 30 20 29 7b 0a 20 20 20  ->yyidx>0 ){.   
2ac16 20 69 6e 74 20 69 3b 0a 20 20 20 20 66 70 72 69   int i;.    fpri
2ac17 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c  ntf(yyTraceFILE,
2ac18 22 25 73 53 68 69 66 74 20 25 64 5c 6e 22 2c 79  "%sShift %d\n",y
2ac19 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79 79 4e  yTracePrompt,yyN
2ac1a 65 77 53 74 61 74 65 29 3b 0a 20 20 20 20 66 70  ewState);.    fp
2ac1b 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2ac1c 45 2c 22 25 73 53 74 61 63 6b 3a 22 2c 79 79 54  E,"%sStack:",yyT
2ac1d 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20 20  racePrompt);.   
2ac1e 20 66 6f 72 28 69 3d 31 3b 20 69 3c 3d 79 79 70   for(i=1; i<=yyp
2ac1f 50 61 72 73 65 72 2d 3e 79 79 69 64 78 3b 20 69  Parser->yyidx; i
2ac20 2b 2b 29 0a 20 20 20 20 20 20 66 70 72 69 6e 74  ++).      fprint
2ac21 66 28 79 79 54 72 61 63 65 46 49 4c 45 2c 22 20  f(yyTraceFILE," 
2ac22 25 73 22 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b  %s",yyTokenName[
2ac23 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74 61  yypParser->yysta
2ac24 63 6b 5b 69 5d 2e 6d 61 6a 6f 72 5d 29 3b 0a 20  ck[i].major]);. 
2ac25 20 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61     fprintf(yyTra
2ac26 63 65 46 49 4c 45 2c 22 5c 6e 22 29 3b 0a 20 20  ceFILE,"\n");.  
2ac27 7d 0a 23 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 20 54  }.#endif.}../* T
2ac28 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 61 62  he following tab
2ac29 6c 65 20 63 6f 6e 74 61 69 6e 73 20 69 6e 66 6f  le contains info
2ac2a 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20 65 76  rmation about ev
2ac2b 65 72 79 20 72 75 6c 65 20 74 68 61 74 0a 2a 2a  ery rule that.**
2ac2c 20 69 73 20 75 73 65 64 20 64 75 72 69 6e 67 20   is used during 
2ac2d 74 68 65 20 72 65 64 75 63 65 2e 0a 2a 2f 0a 73  the reduce..*/.s
2ac2e 74 61 74 69 63 20 63 6f 6e 73 74 20 73 74 72 75  tatic const stru
2ac2f 63 74 20 7b 0a 20 20 59 59 43 4f 44 45 54 59 50  ct {.  YYCODETYP
2ac30 45 20 6c 68 73 3b 20 20 20 20 20 20 20 20 20 2f  E lhs;         /
2ac31 2a 20 53 79 6d 62 6f 6c 20 6f 6e 20 74 68 65 20  * Symbol on the 
2ac32 6c 65 66 74 2d 68 61 6e 64 20 73 69 64 65 20 6f  left-hand side o
2ac33 66 20 74 68 65 20 72 75 6c 65 20 2a 2f 0a 20 20  f the rule */.  
2ac34 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 6e 72  unsigned char nr
2ac35 68 73 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65  hs;     /* Numbe
2ac36 72 20 6f 66 20 72 69 67 68 74 2d 68 61 6e 64 20  r of right-hand 
2ac37 73 69 64 65 20 73 79 6d 62 6f 6c 73 20 69 6e 20  side symbols in 
2ac38 74 68 65 20 72 75 6c 65 20 2a 2f 0a 7d 20 79 79  the rule */.} yy
2ac39 52 75 6c 65 49 6e 66 6f 5b 5d 20 3d 20 7b 0a 20  RuleInfo[] = {. 
2ac3a 20 7b 20 31 34 30 2c 20 31 20 7d 2c 0a 20 20 7b   { 140, 1 },.  {
2ac3b 20 31 34 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   141, 2 },.  { 1
2ac3c 34 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 32  41, 1 },.  { 142
2ac3d 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 32 2c 20  , 1 },.  { 142, 
2ac3e 33 20 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 30 20  3 },.  { 143, 0 
2ac3f 7d 2c 0a 20 20 7b 20 31 34 33 2c 20 31 20 7d 2c  },.  { 143, 1 },
2ac40 0a 20 20 7b 20 31 34 33 2c 20 33 20 7d 2c 0a 20  .  { 143, 3 },. 
2ac41 20 7b 20 31 34 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 144, 1 },.  {
2ac42 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20 7b 20 31   145, 3 },.  { 1
2ac43 34 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 34 37  47, 0 },.  { 147
2ac44 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 37 2c 20  , 1 },.  { 147, 
2ac45 32 20 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 30 20  2 },.  { 146, 0 
2ac46 7d 2c 0a 20 20 7b 20 31 34 36 2c 20 31 20 7d 2c  },.  { 146, 1 },
2ac47 0a 20 20 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20  .  { 146, 1 },. 
2ac48 20 7b 20 31 34 36 2c 20 31 20 7d 2c 0a 20 20 7b   { 146, 1 },.  {
2ac49 20 31 34 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   145, 2 },.  { 1
2ac4a 34 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 35  45, 2 },.  { 145
2ac4b 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20  , 2 },.  { 145, 
2ac4c 32 20 7d 2c 0a 20 20 7b 20 31 34 39 2c 20 36 20  2 },.  { 149, 6 
2ac4d 7d 2c 0a 20 20 7b 20 31 35 32 2c 20 30 20 7d 2c  },.  { 152, 0 },
2ac4e 0a 20 20 7b 20 31 35 32 2c 20 33 20 7d 2c 0a 20  .  { 152, 3 },. 
2ac4f 20 7b 20 31 35 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 151, 1 },.  {
2ac50 20 31 35 31 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   151, 0 },.  { 1
2ac51 35 30 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 35 30  50, 4 },.  { 150
2ac52 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20  , 2 },.  { 154, 
2ac53 33 20 7d 2c 0a 20 20 7b 20 31 35 34 2c 20 31 20  3 },.  { 154, 1 
2ac54 7d 2c 0a 20 20 7b 20 31 35 37 2c 20 33 20 7d 2c  },.  { 157, 3 },
2ac55 0a 20 20 7b 20 31 35 38 2c 20 31 20 7d 2c 0a 20  .  { 158, 1 },. 
2ac56 20 7b 20 31 36 31 2c 20 31 20 7d 2c 0a 20 20 7b   { 161, 1 },.  {
2ac57 20 31 36 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   162, 1 },.  { 1
2ac58 34 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38  48, 1 },.  { 148
2ac59 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34 38 2c 20  , 1 },.  { 148, 
2ac5a 31 20 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 30 20  1 },.  { 159, 0 
2ac5b 7d 2c 0a 20 20 7b 20 31 35 39 2c 20 31 20 7d 2c  },.  { 159, 1 },
2ac5c 0a 20 20 7b 20 31 36 33 2c 20 31 20 7d 2c 0a 20  .  { 163, 1 },. 
2ac5d 20 7b 20 31 36 33 2c 20 34 20 7d 2c 0a 20 20 7b   { 163, 4 },.  {
2ac5e 20 31 36 33 2c 20 36 20 7d 2c 0a 20 20 7b 20 31   163, 6 },.  { 1
2ac5f 36 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 36 34  64, 1 },.  { 164
2ac60 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20  , 2 },.  { 165, 
2ac61 31 20 7d 2c 0a 20 20 7b 20 31 36 35 2c 20 31 20  1 },.  { 165, 1 
2ac62 7d 2c 0a 20 20 7b 20 31 36 30 2c 20 32 20 7d 2c  },.  { 160, 2 },
2ac63 0a 20 20 7b 20 31 36 30 2c 20 30 20 7d 2c 0a 20  .  { 160, 0 },. 
2ac64 20 7b 20 31 36 38 2c 20 33 20 7d 2c 0a 20 20 7b   { 168, 3 },.  {
2ac65 20 31 36 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   168, 1 },.  { 1
2ac66 36 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39  69, 2 },.  { 169
2ac67 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20  , 4 },.  { 169, 
2ac68 33 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 33 20  3 },.  { 169, 3 
2ac69 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 32 20 7d 2c  },.  { 169, 2 },
2ac6a 0a 20 20 7b 20 31 36 39 2c 20 32 20 7d 2c 0a 20  .  { 169, 2 },. 
2ac6b 20 7b 20 31 36 39 2c 20 33 20 7d 2c 0a 20 20 7b   { 169, 3 },.  {
2ac6c 20 31 36 39 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   169, 5 },.  { 1
2ac6d 36 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 36 39  69, 2 },.  { 169
2ac6e 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20  , 4 },.  { 169, 
2ac6f 34 20 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 31 20  4 },.  { 169, 1 
2ac70 7d 2c 0a 20 20 7b 20 31 36 39 2c 20 32 20 7d 2c  },.  { 169, 2 },
2ac71 0a 20 20 7b 20 31 37 34 2c 20 30 20 7d 2c 0a 20  .  { 174, 0 },. 
2ac72 20 7b 20 31 37 34 2c 20 31 20 7d 2c 0a 20 20 7b   { 174, 1 },.  {
2ac73 20 31 37 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 31   176, 0 },.  { 1
2ac74 37 36 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 38  76, 2 },.  { 178
2ac75 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20  , 2 },.  { 178, 
2ac76 33 20 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 33 20  3 },.  { 178, 3 
2ac77 7d 2c 0a 20 20 7b 20 31 37 38 2c 20 33 20 7d 2c  },.  { 178, 3 },
2ac78 0a 20 20 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20  .  { 179, 2 },. 
2ac79 20 7b 20 31 37 39 2c 20 32 20 7d 2c 0a 20 20 7b   { 179, 2 },.  {
2ac7a 20 31 37 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31   179, 1 },.  { 1
2ac7b 37 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 37  79, 1 },.  { 177
2ac7c 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 37 2c 20  , 3 },.  { 177, 
2ac7d 32 20 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 30 20  2 },.  { 180, 0 
2ac7e 7d 2c 0a 20 20 7b 20 31 38 30 2c 20 32 20 7d 2c  },.  { 180, 2 },
2ac7f 0a 20 20 7b 20 31 38 30 2c 20 32 20 7d 2c 0a 20  .  { 180, 2 },. 
2ac80 20 7b 20 31 35 35 2c 20 30 20 7d 2c 0a 20 20 7b   { 155, 0 },.  {
2ac81 20 31 35 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31   155, 2 },.  { 1
2ac82 38 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 38 31  81, 3 },.  { 181
2ac83 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 31 2c 20  , 2 },.  { 181, 
2ac84 31 20 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 32 20  1 },.  { 182, 2 
2ac85 7d 2c 0a 20 20 7b 20 31 38 32 2c 20 37 20 7d 2c  },.  { 182, 7 },
2ac86 0a 20 20 7b 20 31 38 32 2c 20 35 20 7d 2c 0a 20  .  { 182, 5 },. 
2ac87 20 7b 20 31 38 32 2c 20 35 20 7d 2c 0a 20 20 7b   { 182, 5 },.  {
2ac88 20 31 38 32 2c 20 31 30 20 7d 2c 0a 20 20 7b 20   182, 10 },.  { 
2ac89 31 38 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 38  184, 0 },.  { 18
2ac8a 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 32 2c  4, 1 },.  { 172,
2ac8b 20 30 20 7d 2c 0a 20 20 7b 20 31 37 32 2c 20 33   0 },.  { 172, 3
2ac8c 20 7d 2c 0a 20 20 7b 20 31 38 35 2c 20 30 20 7d   },.  { 185, 0 }
2ac8d 2c 0a 20 20 7b 20 31 38 35 2c 20 32 20 7d 2c 0a  ,.  { 185, 2 },.
2ac8e 20 20 7b 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20    { 186, 1 },.  
2ac8f 7b 20 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 186, 1 },.  { 
2ac90 31 38 36 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 34  186, 1 },.  { 14
2ac91 35 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 38 38 2c  5, 4 },.  { 188,
2ac92 20 32 20 7d 2c 0a 20 20 7b 20 31 38 38 2c 20 30   2 },.  { 188, 0
2ac93 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 38 20 7d   },.  { 145, 8 }
2ac94 2c 0a 20 20 7b 20 31 34 35 2c 20 34 20 7d 2c 0a  ,.  { 145, 4 },.
2ac95 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a 20 20    { 145, 1 },.  
2ac96 7b 20 31 35 36 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 156, 1 },.  { 
2ac97 31 35 36 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 39  156, 3 },.  { 19
2ac98 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 39 31 2c  1, 1 },.  { 191,
2ac99 20 32 20 7d 2c 0a 20 20 7b 20 31 39 31 2c 20 31   2 },.  { 191, 1
2ac9a 20 7d 2c 0a 20 20 7b 20 31 39 30 2c 20 39 20 7d   },.  { 190, 9 }
2ac9b 2c 0a 20 20 7b 20 31 39 32 2c 20 31 20 7d 2c 0a  ,.  { 192, 1 },.
2ac9c 20 20 7b 20 31 39 32 2c 20 31 20 7d 2c 0a 20 20    { 192, 1 },.  
2ac9d 7b 20 31 39 32 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 192, 0 },.  { 
2ac9e 32 30 30 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30  200, 2 },.  { 20
2ac9f 30 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39 33 2c  0, 0 },.  { 193,
2aca0 20 33 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 32   3 },.  { 193, 2
2aca1 20 7d 2c 0a 20 20 7b 20 31 39 33 2c 20 34 20 7d   },.  { 193, 4 }
2aca2 2c 0a 20 20 7b 20 32 30 31 2c 20 32 20 7d 2c 0a  ,.  { 201, 2 },.
2aca3 20 20 7b 20 32 30 31 2c 20 31 20 7d 2c 0a 20 20    { 201, 1 },.  
2aca4 7b 20 32 30 31 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 201, 0 },.  { 
2aca5 31 39 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39  194, 0 },.  { 19
2aca6 34 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c  4, 2 },.  { 203,
2aca7 20 32 20 7d 2c 0a 20 20 7b 20 32 30 33 2c 20 30   2 },.  { 203, 0
2aca8 20 7d 2c 0a 20 20 7b 20 32 30 32 2c 20 37 20 7d   },.  { 202, 7 }
2aca9 2c 0a 20 20 7b 20 32 30 32 2c 20 37 20 7d 2c 0a  ,.  { 202, 7 },.
2acaa 20 20 7b 20 32 30 38 2c 20 31 20 7d 2c 0a 20 20    { 208, 1 },.  
2acab 7b 20 32 30 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 208, 1 },.  { 
2acac 31 35 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 35  153, 0 },.  { 15
2acad 33 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 38 39 2c  3, 2 },.  { 189,
2acae 20 32 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 31   2 },.  { 204, 1
2acaf 20 7d 2c 0a 20 20 7b 20 32 30 34 2c 20 32 20 7d   },.  { 204, 2 }
2acb0 2c 0a 20 20 7b 20 32 30 34 2c 20 33 20 7d 2c 0a  ,.  { 204, 3 },.
2acb1 20 20 7b 20 32 30 34 2c 20 34 20 7d 2c 0a 20 20    { 204, 4 },.  
2acb2 7b 20 32 30 36 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 206, 2 },.  { 
2acb3 32 30 36 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30  206, 0 },.  { 20
2acb4 35 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 30 35 2c  5, 0 },.  { 205,
2acb5 20 33 20 7d 2c 0a 20 20 7b 20 32 30 35 2c 20 32   3 },.  { 205, 2
2acb6 20 7d 2c 0a 20 20 7b 20 32 30 37 2c 20 34 20 7d   },.  { 207, 4 }
2acb7 2c 0a 20 20 7b 20 32 30 37 2c 20 30 20 7d 2c 0a  ,.  { 207, 0 },.
2acb8 20 20 7b 20 31 39 38 2c 20 30 20 7d 2c 0a 20 20    { 198, 0 },.  
2acb9 7b 20 31 39 38 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 198, 3 },.  { 
2acba 32 31 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 32 31  211, 4 },.  { 21
2acbb 31 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31 32 2c  1, 2 },.  { 212,
2acbc 20 31 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31   1 },.  { 173, 1
2acbd 20 7d 2c 0a 20 20 7b 20 31 37 33 2c 20 31 20 7d   },.  { 173, 1 }
2acbe 2c 0a 20 20 7b 20 31 37 33 2c 20 30 20 7d 2c 0a  ,.  { 173, 0 },.
2acbf 20 20 7b 20 31 39 36 2c 20 30 20 7d 2c 0a 20 20    { 196, 0 },.  
2acc0 7b 20 31 39 36 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 196, 3 },.  { 
2acc1 31 39 37 2c 20 30 20 7d 2c 0a 20 20 7b 20 31 39  197, 0 },.  { 19
2acc2 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 39 39 2c  7, 2 },.  { 199,
2acc3 20 30 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 32   0 },.  { 199, 2
2acc4 20 7d 2c 0a 20 20 7b 20 31 39 39 2c 20 34 20 7d   },.  { 199, 4 }
2acc5 2c 0a 20 20 7b 20 31 39 39 2c 20 34 20 7d 2c 0a  ,.  { 199, 4 },.
2acc6 20 20 7b 20 31 34 35 2c 20 35 20 7d 2c 0a 20 20    { 145, 5 },.  
2acc7 7b 20 31 39 35 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 195, 0 },.  { 
2acc8 31 39 35 2c 20 32 20 7d 2c 0a 20 20 7b 20 31 34  195, 2 },.  { 14
2acc9 35 2c 20 37 20 7d 2c 0a 20 20 7b 20 32 31 34 2c  5, 7 },.  { 214,
2acca 20 35 20 7d 2c 0a 20 20 7b 20 32 31 34 2c 20 33   5 },.  { 214, 3
2accb 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 38 20 7d   },.  { 145, 8 }
2accc 2c 0a 20 20 7b 20 31 34 35 2c 20 35 20 7d 2c 0a  ,.  { 145, 5 },.
2accd 20 20 7b 20 31 34 35 2c 20 36 20 7d 2c 0a 20 20    { 145, 6 },.  
2acce 7b 20 32 31 35 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 215, 2 },.  { 
2accf 32 31 35 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31  215, 1 },.  { 21
2acd0 37 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 37 2c  7, 3 },.  { 217,
2acd1 20 31 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 30   1 },.  { 216, 0
2acd2 20 7d 2c 0a 20 20 7b 20 32 31 36 2c 20 33 20 7d   },.  { 216, 3 }
2acd3 2c 0a 20 20 7b 20 32 31 30 2c 20 33 20 7d 2c 0a  ,.  { 210, 3 },.
2acd4 20 20 7b 20 32 31 30 2c 20 31 20 7d 2c 0a 20 20    { 210, 1 },.  
2acd5 7b 20 31 37 31 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 171, 1 },.  { 
2acd6 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  171, 3 },.  { 17
2acd7 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  0, 1 },.  { 171,
2acd8 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 31   1 },.  { 171, 1
2acd9 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d   },.  { 171, 3 }
2acda 2c 0a 20 20 7b 20 31 37 31 2c 20 35 20 7d 2c 0a  ,.  { 171, 5 },.
2acdb 20 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20    { 170, 1 },.  
2acdc 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 170, 1 },.  { 
2acdd 31 37 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37  171, 1 },.  { 17
2acde 31 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  1, 1 },.  { 171,
2acdf 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 36   3 },.  { 171, 6
2ace0 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 35 20 7d   },.  { 171, 5 }
2ace1 2c 0a 20 20 7b 20 31 37 31 2c 20 34 20 7d 2c 0a  ,.  { 171, 4 },.
2ace2 20 20 7b 20 31 37 30 2c 20 31 20 7d 2c 0a 20 20    { 170, 1 },.  
2ace3 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 171, 3 },.  { 
2ace4 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  171, 3 },.  { 17
2ace5 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  1, 3 },.  { 171,
2ace6 20 33 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33   3 },.  { 171, 3
2ace7 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d   },.  { 171, 3 }
2ace8 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c 0a  ,.  { 171, 3 },.
2ace9 20 20 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20 20    { 171, 3 },.  
2acea 7b 20 32 31 39 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 219, 1 },.  { 
2aceb 32 31 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 31  219, 2 },.  { 21
2acec 39 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 31 39 2c  9, 1 },.  { 219,
2aced 20 32 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 32   2 },.  { 220, 2
2acee 20 7d 2c 0a 20 20 7b 20 32 32 30 2c 20 30 20 7d   },.  { 220, 0 }
2acef 2c 0a 20 20 7b 20 31 37 31 2c 20 34 20 7d 2c 0a  ,.  { 171, 4 },.
2acf0 20 20 7b 20 31 37 31 2c 20 32 20 7d 2c 0a 20 20    { 171, 2 },.  
2acf1 7b 20 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20  { 171, 3 },.  { 
2acf2 31 37 31 2c 20 33 20 7d 2c 0a 20 20 7b 20 31 37  171, 3 },.  { 17
2acf3 31 2c 20 34 20 7d 2c 0a 20 20 7b 20 31 37 31 2c  1, 4 },.  { 171,
2acf4 20 32 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32   2 },.  { 171, 2
2acf5 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20 7d   },.  { 171, 2 }
2acf6 2c 0a 20 20 7b 20 31 37 31 2c 20 32 20 7d 2c 0a  ,.  { 171, 2 },.
2acf7 20 20 7b 20 32 32 31 2c 20 31 20 7d 2c 0a 20 20    { 221, 1 },.  
2acf8 7b 20 32 32 31 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 221, 2 },.  { 
2acf9 31 37 31 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32  171, 5 },.  { 22
2acfa 32 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 32 2c  2, 1 },.  { 222,
2acfb 20 32 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 35   2 },.  { 171, 5
2acfc 20 7d 2c 0a 20 20 7b 20 31 37 31 2c 20 33 20 7d   },.  { 171, 3 }
2acfd 2c 0a 20 20 7b 20 31 37 31 2c 20 35 20 7d 2c 0a  ,.  { 171, 5 },.
2acfe 20 20 7b 20 31 37 31 2c 20 34 20 7d 2c 0a 20 20    { 171, 4 },.  
2acff 7b 20 31 37 31 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 171, 4 },.  { 
2ad00 31 37 31 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32  171, 5 },.  { 22
2ad01 34 2c 20 35 20 7d 2c 0a 20 20 7b 20 32 32 34 2c  4, 5 },.  { 224,
2ad02 20 34 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 32   4 },.  { 225, 2
2ad03 20 7d 2c 0a 20 20 7b 20 32 32 35 2c 20 30 20 7d   },.  { 225, 0 }
2ad04 2c 0a 20 20 7b 20 32 32 33 2c 20 31 20 7d 2c 0a  ,.  { 223, 1 },.
2ad05 20 20 7b 20 32 32 33 2c 20 30 20 7d 2c 0a 20 20    { 223, 0 },.  
2ad06 7b 20 32 31 38 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 218, 1 },.  { 
2ad07 32 31 38 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 31  218, 0 },.  { 21
2ad08 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 31 33 2c  3, 3 },.  { 213,
2ad09 20 31 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31   1 },.  { 145, 1
2ad0a 31 20 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 31 20  1 },.  { 226, 1 
2ad0b 7d 2c 0a 20 20 7b 20 32 32 36 2c 20 30 20 7d 2c  },.  { 226, 0 },
2ad0c 0a 20 20 7b 20 31 37 35 2c 20 30 20 7d 2c 0a 20  .  { 175, 0 },. 
2ad0d 20 7b 20 31 37 35 2c 20 33 20 7d 2c 0a 20 20 7b   { 175, 3 },.  {
2ad0e 20 31 38 33 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   183, 5 },.  { 1
2ad0f 38 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 32 37  83, 3 },.  { 227
2ad10 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 32 37 2c 20  , 0 },.  { 227, 
2ad11 32 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 34 20  2 },.  { 145, 4 
2ad12 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c  },.  { 145, 1 },
2ad13 0a 20 20 7b 20 31 34 35 2c 20 32 20 7d 2c 0a 20  .  { 145, 2 },. 
2ad14 20 7b 20 31 34 35 2c 20 35 20 7d 2c 0a 20 20 7b   { 145, 5 },.  {
2ad15 20 31 34 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31   145, 5 },.  { 1
2ad16 34 35 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 35  45, 5 },.  { 145
2ad17 2c 20 35 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20  , 5 },.  { 145, 
2ad18 36 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 33 20  6 },.  { 145, 3 
2ad19 7d 2c 0a 20 20 7b 20 32 32 38 2c 20 31 20 7d 2c  },.  { 228, 1 },
2ad1a 0a 20 20 7b 20 32 32 38 2c 20 31 20 7d 2c 0a 20  .  { 228, 1 },. 
2ad1b 20 7b 20 31 36 36 2c 20 32 20 7d 2c 0a 20 20 7b   { 166, 2 },.  {
2ad1c 20 31 36 37 2c 20 32 20 7d 2c 0a 20 20 7b 20 32   167, 2 },.  { 2
2ad1d 33 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 39  30, 1 },.  { 229
2ad1e 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 32 39 2c 20  , 1 },.  { 229, 
2ad1f 30 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 35 20  0 },.  { 145, 5 
2ad20 7d 2c 0a 20 20 7b 20 32 33 31 2c 20 31 31 20 7d  },.  { 231, 11 }
2ad21 2c 0a 20 20 7b 20 32 33 33 2c 20 31 20 7d 2c 0a  ,.  { 233, 1 },.
2ad22 20 20 7b 20 32 33 33 2c 20 31 20 7d 2c 0a 20 20    { 233, 1 },.  
2ad23 7b 20 32 33 33 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 233, 2 },.  { 
2ad24 32 33 33 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 33  233, 0 },.  { 23
2ad25 34 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 34 2c  4, 1 },.  { 234,
2ad26 20 31 20 7d 2c 0a 20 20 7b 20 32 33 34 2c 20 33   1 },.  { 234, 3
2ad27 20 7d 2c 0a 20 20 7b 20 32 33 35 2c 20 30 20 7d   },.  { 235, 0 }
2ad28 2c 0a 20 20 7b 20 32 33 35 2c 20 33 20 7d 2c 0a  ,.  { 235, 3 },.
2ad29 20 20 7b 20 32 33 36 2c 20 30 20 7d 2c 0a 20 20    { 236, 0 },.  
2ad2a 7b 20 32 33 36 2c 20 32 20 7d 2c 0a 20 20 7b 20  { 236, 2 },.  { 
2ad2b 32 33 32 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 33  232, 3 },.  { 23
2ad2c 32 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33 37 2c  2, 2 },.  { 237,
2ad2d 20 36 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 38   6 },.  { 237, 8
2ad2e 20 7d 2c 0a 20 20 7b 20 32 33 37 2c 20 35 20 7d   },.  { 237, 5 }
2ad2f 2c 0a 20 20 7b 20 32 33 37 2c 20 34 20 7d 2c 0a  ,.  { 237, 4 },.
2ad30 20 20 7b 20 32 33 37 2c 20 31 20 7d 2c 0a 20 20    { 237, 1 },.  
2ad31 7b 20 31 37 31 2c 20 34 20 7d 2c 0a 20 20 7b 20  { 171, 4 },.  { 
2ad32 31 37 31 2c 20 36 20 7d 2c 0a 20 20 7b 20 31 38  171, 6 },.  { 18
2ad33 37 2c 20 31 20 7d 2c 0a 20 20 7b 20 31 38 37 2c  7, 1 },.  { 187,
2ad34 20 31 20 7d 2c 0a 20 20 7b 20 31 38 37 2c 20 31   1 },.  { 187, 1
2ad35 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 34 20 7d   },.  { 145, 4 }
2ad36 2c 0a 20 20 7b 20 31 34 35 2c 20 36 20 7d 2c 0a  ,.  { 145, 6 },.
2ad37 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20    { 145, 3 },.  
2ad38 7b 20 32 33 39 2c 20 30 20 7d 2c 0a 20 20 7b 20  { 239, 0 },.  { 
2ad39 32 33 39 2c 20 32 20 7d 2c 0a 20 20 7b 20 32 33  239, 2 },.  { 23
2ad3a 38 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 33 38 2c  8, 1 },.  { 238,
2ad3b 20 30 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31   0 },.  { 145, 1
2ad3c 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 33 20 7d   },.  { 145, 3 }
2ad3d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d 2c 0a  ,.  { 145, 1 },.
2ad3e 20 20 7b 20 31 34 35 2c 20 33 20 7d 2c 0a 20 20    { 145, 3 },.  
2ad3f 7b 20 31 34 35 2c 20 36 20 7d 2c 0a 20 20 7b 20  { 145, 6 },.  { 
2ad40 31 34 35 2c 20 36 20 7d 2c 0a 20 20 7b 20 32 34  145, 6 },.  { 24
2ad41 30 2c 20 31 20 7d 2c 0a 20 20 7b 20 32 34 31 2c  0, 1 },.  { 241,
2ad42 20 30 20 7d 2c 0a 20 20 7b 20 32 34 31 2c 20 31   0 },.  { 241, 1
2ad43 20 7d 2c 0a 20 20 7b 20 31 34 35 2c 20 31 20 7d   },.  { 145, 1 }
2ad44 2c 0a 20 20 7b 20 31 34 35 2c 20 34 20 7d 2c 0a  ,.  { 145, 4 },.
2ad45 20 20 7b 20 32 34 32 2c 20 37 20 7d 2c 0a 20 20    { 242, 7 },.  
2ad46 7b 20 32 34 33 2c 20 31 20 7d 2c 0a 20 20 7b 20  { 243, 1 },.  { 
2ad47 32 34 33 2c 20 33 20 7d 2c 0a 20 20 7b 20 32 34  243, 3 },.  { 24
2ad48 34 2c 20 30 20 7d 2c 0a 20 20 7b 20 32 34 34 2c  4, 0 },.  { 244,
2ad49 20 32 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 31   2 },.  { 245, 1
2ad4a 20 7d 2c 0a 20 20 7b 20 32 34 35 2c 20 33 20 7d   },.  { 245, 3 }
2ad4b 2c 0a 20 20 7b 20 32 34 36 2c 20 31 20 7d 2c 0a  ,.  { 246, 1 },.
2ad4c 20 20 7b 20 32 34 37 2c 20 30 20 7d 2c 0a 20 20    { 247, 0 },.  
2ad4d 7b 20 32 34 37 2c 20 32 20 7d 2c 0a 7d 3b 0a 0a  { 247, 2 },.};..
2ad4e 73 74 61 74 69 63 20 76 6f 69 64 20 79 79 5f 61  static void yy_a
2ad4f 63 63 65 70 74 28 79 79 50 61 72 73 65 72 2a 29  ccept(yyParser*)
2ad50 3b 20 20 2f 2a 20 46 6f 72 77 61 72 64 20 44 65  ;  /* Forward De
2ad51 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a  claration */../*
2ad52 0a 2a 2a 20 50 65 72 66 6f 72 6d 20 61 20 72 65  .** Perform a re
2ad53 64 75 63 65 20 61 63 74 69 6f 6e 20 61 6e 64 20  duce action and 
2ad54 74 68 65 20 73 68 69 66 74 20 74 68 61 74 20 6d  the shift that m
2ad55 75 73 74 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a  ust immediately.
2ad56 2a 2a 20 66 6f 6c 6c 6f 77 20 74 68 65 20 72 65  ** follow the re
2ad57 64 75 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  duce..*/.static 
2ad58 76 6f 69 64 20 79 79 5f 72 65 64 75 63 65 28 0a  void yy_reduce(.
2ad59 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
2ad5a 61 72 73 65 72 2c 20 20 20 20 20 20 20 20 20 2f  arser,         /
2ad5b 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
2ad5c 20 20 69 6e 74 20 79 79 72 75 6c 65 6e 6f 20 20    int yyruleno  
2ad5d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ad5e 2a 20 4e 75 6d 62 65 72 20 6f 66 20 74 68 65 20  * Number of the 
2ad5f 72 75 6c 65 20 62 79 20 77 68 69 63 68 20 74 6f  rule by which to
2ad60 20 72 65 64 75 63 65 20 2a 2f 0a 29 7b 0a 20 20   reduce */.){.  
2ad61 69 6e 74 20 79 79 67 6f 74 6f 3b 20 20 20 20 20  int yygoto;     
2ad62 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ad63 2f 2a 20 54 68 65 20 6e 65 78 74 20 73 74 61 74  /* The next stat
2ad64 65 20 2a 2f 0a 20 20 69 6e 74 20 79 79 61 63 74  e */.  int yyact
2ad65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ad66 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 6e 65         /* The ne
2ad67 78 74 20 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 59  xt action */.  Y
2ad68 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 67 6f 74  YMINORTYPE yygot
2ad69 6f 6d 69 6e 6f 72 3b 20 20 20 20 20 20 20 20 2f  ominor;        /
2ad6a 2a 20 54 68 65 20 4c 48 53 20 6f 66 20 74 68 65  * The LHS of the
2ad6b 20 72 75 6c 65 20 72 65 64 75 63 65 64 20 2a 2f   rule reduced */
2ad6c 0a 20 20 79 79 53 74 61 63 6b 45 6e 74 72 79 20  .  yyStackEntry 
2ad6d 2a 79 79 6d 73 70 3b 20 20 20 20 20 20 20 20 20  *yymsp;         
2ad6e 20 20 20 2f 2a 20 54 68 65 20 74 6f 70 20 6f 66     /* The top of
2ad6f 20 74 68 65 20 70 61 72 73 65 72 27 73 20 73 74   the parser's st
2ad70 61 63 6b 20 2a 2f 0a 20 20 69 6e 74 20 79 79 73  ack */.  int yys
2ad71 69 7a 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ize;            
2ad72 20 20 20 20 20 20 20 20 20 2f 2a 20 41 6d 6f 75           /* Amou
2ad73 6e 74 20 74 6f 20 70 6f 70 20 74 68 65 20 73 74  nt to pop the st
2ad74 61 63 6b 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ack */.  sqlite3
2ad75 50 61 72 73 65 72 41 52 47 5f 46 45 54 43 48 3b  ParserARG_FETCH;
2ad76 0a 20 20 79 79 6d 73 70 20 3d 20 26 79 79 70 50  .  yymsp = &yypP
2ad77 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 79  arser->yystack[y
2ad78 79 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 5d  ypParser->yyidx]
2ad79 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47  ;.#ifndef NDEBUG
2ad7a 0a 20 20 69 66 28 20 79 79 54 72 61 63 65 46 49  .  if( yyTraceFI
2ad7b 4c 45 20 26 26 20 79 79 72 75 6c 65 6e 6f 3e 3d  LE && yyruleno>=
2ad7c 30 20 0a 20 20 20 20 20 20 20 20 26 26 20 79 79  0 .        && yy
2ad7d 72 75 6c 65 6e 6f 3c 28 69 6e 74 29 28 73 69 7a  ruleno<(int)(siz
2ad7e 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d 65 29 2f  eof(yyRuleName)/
2ad7f 73 69 7a 65 6f 66 28 79 79 52 75 6c 65 4e 61 6d  sizeof(yyRuleNam
2ad80 65 5b 30 5d 29 29 20 29 7b 0a 20 20 20 20 66 70  e[0])) ){.    fp
2ad81 72 69 6e 74 66 28 79 79 54 72 61 63 65 46 49 4c  rintf(yyTraceFIL
2ad82 45 2c 20 22 25 73 52 65 64 75 63 65 20 5b 25 73  E, "%sReduce [%s
2ad83 5d 2e 5c 6e 22 2c 20 79 79 54 72 61 63 65 50 72  ].\n", yyTracePr
2ad84 6f 6d 70 74 2c 0a 20 20 20 20 20 20 79 79 52 75  ompt,.      yyRu
2ad85 6c 65 4e 61 6d 65 5b 79 79 72 75 6c 65 6e 6f 5d  leName[yyruleno]
2ad86 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a  );.  }.#endif /*
2ad87 20 4e 44 45 42 55 47 20 2a 2f 0a 0a 20 20 2f 2a   NDEBUG */..  /*
2ad88 20 53 69 6c 65 6e 63 65 20 63 6f 6d 70 6c 61 69   Silence complai
2ad89 6e 74 73 20 66 72 6f 6d 20 70 75 72 69 66 79 20  nts from purify 
2ad8a 61 62 6f 75 74 20 79 79 67 6f 74 6f 6d 69 6e 6f  about yygotomino
2ad8b 72 20 62 65 69 6e 67 20 75 6e 69 6e 69 74 69 61  r being uninitia
2ad8c 6c 69 7a 65 64 0a 20 20 2a 2a 20 69 6e 20 73 6f  lized.  ** in so
2ad8d 6d 65 20 63 61 73 65 73 20 77 68 65 6e 20 69 74  me cases when it
2ad8e 20 69 73 20 63 6f 70 69 65 64 20 69 6e 74 6f 20   is copied into 
2ad8f 74 68 65 20 73 74 61 63 6b 20 61 66 74 65 72 20  the stack after 
2ad90 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 20 20  the following.  
2ad91 2a 2a 20 73 77 69 74 63 68 2e 20 20 79 79 67 6f  ** switch.  yygo
2ad92 74 6f 6d 69 6e 6f 72 20 69 73 20 75 6e 69 6e 69  tominor is unini
2ad93 74 69 61 6c 69 7a 65 64 20 77 68 65 6e 20 61 20  tialized when a 
2ad94 72 75 6c 65 20 72 65 64 75 63 65 73 20 74 68 61  rule reduces tha
2ad95 74 20 64 6f 65 73 0a 20 20 2a 2a 20 6e 6f 74 20  t does.  ** not 
2ad96 73 65 74 20 74 68 65 20 76 61 6c 75 65 20 6f 66  set the value of
2ad97 20 69 74 73 20 6c 65 66 74 2d 68 61 6e 64 20 73   its left-hand s
2ad98 69 64 65 20 6e 6f 6e 74 65 72 6d 69 6e 61 6c 2e  ide nonterminal.
2ad99 20 20 4c 65 61 76 69 6e 67 20 74 68 65 0a 20 20    Leaving the.  
2ad9a 2a 2a 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20  ** value of the 
2ad9b 6e 6f 6e 74 65 72 6d 69 6e 61 6c 20 75 6e 69 6e  nonterminal unin
2ad9c 69 74 69 61 6c 69 7a 65 64 20 69 73 20 75 74 74  itialized is utt
2ad9d 65 72 6c 79 20 68 61 72 6d 6c 65 73 73 20 61 73  erly harmless as
2ad9e 20 6c 6f 6e 67 0a 20 20 2a 2a 20 61 73 20 74 68   long.  ** as th
2ad9f 65 20 76 61 6c 75 65 20 69 73 20 6e 65 76 65 72  e value is never
2ada0 20 75 73 65 64 2e 20 20 53 6f 20 72 65 61 6c 6c   used.  So reall
2ada1 79 20 74 68 65 20 6f 6e 6c 79 20 74 68 69 6e 67  y the only thing
2ada2 20 74 68 69 73 20 63 6f 64 65 0a 20 20 2a 2a 20   this code.  ** 
2ada3 61 63 63 6f 6d 70 6c 69 73 68 65 73 20 69 73 20  accomplishes is 
2ada4 74 6f 20 71 75 69 65 74 65 6e 20 70 75 72 69 66  to quieten purif
2ada5 79 2e 20 20 0a 20 20 2a 2a 0a 20 20 2a 2a 20 32  y.  .  **.  ** 2
2ada6 30 30 37 2d 30 31 2d 31 36 3a 20 20 54 68 65 20  007-01-16:  The 
2ada7 77 69 72 65 73 68 61 72 6b 20 70 72 6f 6a 65 63  wireshark projec
2ada8 74 20 28 77 77 77 2e 77 69 72 65 73 68 61 72 6b  t (www.wireshark
2ada9 2e 6f 72 67 29 20 72 65 70 6f 72 74 73 20 74 68  .org) reports th
2adaa 61 74 0a 20 20 2a 2a 20 77 69 74 68 6f 75 74 20  at.  ** without 
2adab 74 68 69 73 20 63 6f 64 65 2c 20 74 68 65 69 72  this code, their
2adac 20 70 61 72 73 65 72 20 73 65 67 66 61 75 6c 74   parser segfault
2adad 73 2e 20 20 49 27 6d 20 6e 6f 74 20 73 75 72 65  s.  I'm not sure
2adae 20 77 68 61 74 20 74 68 65 72 65 0a 20 20 2a 2a   what there.  **
2adaf 20 70 61 72 73 65 72 20 69 73 20 64 6f 69 6e 67   parser is doing
2adb0 20 74 6f 20 6d 61 6b 65 20 74 68 69 73 20 68 61   to make this ha
2adb1 70 70 65 6e 2e 20 20 54 68 69 73 20 69 73 20 74  ppen.  This is t
2adb2 68 65 20 73 65 63 6f 6e 64 20 62 75 67 20 72 65  he second bug re
2adb3 70 6f 72 74 0a 20 20 2a 2a 20 66 72 6f 6d 20 77  port.  ** from w
2adb4 69 72 65 73 68 61 72 6b 20 74 68 69 73 20 77 65  ireshark this we
2adb5 65 6b 2e 20 20 43 6c 65 61 72 6c 79 20 74 68 65  ek.  Clearly the
2adb6 79 20 61 72 65 20 73 74 72 65 73 73 69 6e 67 20  y are stressing 
2adb7 4c 65 6d 6f 6e 20 69 6e 20 77 61 79 73 0a 20 20  Lemon in ways.  
2adb8 2a 2a 20 74 68 61 74 20 69 74 20 68 61 73 20 6e  ** that it has n
2adb9 6f 74 20 62 65 65 6e 20 70 72 65 76 69 6f 75 73  ot been previous
2adba 6c 79 20 73 74 72 65 73 73 65 64 2e 2e 2e 20 20  ly stressed...  
2adbb 28 53 51 4c 69 74 65 20 74 69 63 6b 65 74 20 23  (SQLite ticket #
2adbc 32 31 37 32 29 0a 20 20 2a 2f 0a 20 20 2f 2a 6d  2172).  */.  /*m
2adbd 65 6d 73 65 74 28 26 79 79 67 6f 74 6f 6d 69 6e  emset(&yygotomin
2adbe 6f 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28 79 79  or, 0, sizeof(yy
2adbf 67 6f 74 6f 6d 69 6e 6f 72 29 29 3b 2a 2f 0a 20  gotominor));*/. 
2adc0 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 20 3d 20 79   yygotominor = y
2adc1 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 0a 0a 20 20  yzerominor;...  
2adc2 73 77 69 74 63 68 28 20 79 79 72 75 6c 65 6e 6f  switch( yyruleno
2adc3 20 29 7b 0a 20 20 2f 2a 20 42 65 67 69 6e 6e 69   ){.  /* Beginni
2adc4 6e 67 20 68 65 72 65 20 61 72 65 20 74 68 65 20  ng here are the 
2adc5 72 65 64 75 63 74 69 6f 6e 20 63 61 73 65 73 2e  reduction cases.
2adc6 20 20 41 20 74 79 70 69 63 61 6c 20 65 78 61 6d    A typical exam
2adc7 70 6c 65 0a 20 20 2a 2a 20 66 6f 6c 6c 6f 77 73  ple.  ** follows
2adc8 3a 0a 20 20 2a 2a 20 20 20 63 61 73 65 20 30 3a  :.  **   case 0:
2adc9 0a 20 20 2a 2a 20 20 23 6c 69 6e 65 20 3c 6c 69  .  **  #line <li
2adca 6e 65 6e 6f 3e 20 3c 67 72 61 6d 6d 61 72 66 69  neno> <grammarfi
2adcb 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 7b 20 2e  le>.  **     { .
2adcc 2e 2e 20 7d 20 20 20 20 20 20 20 20 20 20 20 2f  .. }           /
2adcd 2f 20 55 73 65 72 20 73 75 70 70 6c 69 65 64 20  / User supplied 
2adce 63 6f 64 65 0a 20 20 2a 2a 20 20 23 6c 69 6e 65  code.  **  #line
2adcf 20 3c 6c 69 6e 65 6e 6f 3e 20 3c 74 68 69 73 66   <lineno> <thisf
2add0 69 6c 65 3e 0a 20 20 2a 2a 20 20 20 20 20 62 72  ile>.  **     br
2add1 65 61 6b 3b 0a 20 20 2a 2f 0a 20 20 20 20 20 20  eak;.  */.      
2add2 63 61 73 65 20 30 3a 20 2f 2a 20 69 6e 70 75 74  case 0: /* input
2add3 20 3a 3a 3d 20 63 6d 64 6c 69 73 74 20 2a 2f 0a   ::= cmdlist */.
2add4 20 20 20 20 20 20 63 61 73 65 20 31 3a 20 2f 2a        case 1: /*
2add5 20 63 6d 64 6c 69 73 74 20 3a 3a 3d 20 63 6d 64   cmdlist ::= cmd
2add6 6c 69 73 74 20 65 63 6d 64 20 2a 2f 0a 20 20 20  list ecmd */.   
2add7 20 20 20 63 61 73 65 20 32 3a 20 2f 2a 20 63 6d     case 2: /* cm
2add8 64 6c 69 73 74 20 3a 3a 3d 20 65 63 6d 64 20 2a  dlist ::= ecmd *
2add9 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 3a 20  /.      case 3: 
2adda 2f 2a 20 65 63 6d 64 20 3a 3a 3d 20 53 45 4d 49  /* ecmd ::= SEMI
2addb 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 34   */.      case 4
2addc 3a 20 2f 2a 20 65 63 6d 64 20 3a 3a 3d 20 65 78  : /* ecmd ::= ex
2addd 70 6c 61 69 6e 20 63 6d 64 78 20 53 45 4d 49 20  plain cmdx SEMI 
2adde 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  */.      case 10
2addf 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f 70 74 20 3a  : /* trans_opt :
2ade0 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2ade1 20 31 31 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f 70   11: /* trans_op
2ade2 74 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f  t ::= TRANSACTIO
2ade3 4e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  N */.      case 
2ade4 31 32 3a 20 2f 2a 20 74 72 61 6e 73 5f 6f 70 74  12: /* trans_opt
2ade5 20 3a 3a 3d 20 54 52 41 4e 53 41 43 54 49 4f 4e   ::= TRANSACTION
2ade6 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73   nm */.      cas
2ade7 65 20 32 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d  e 20: /* cmd ::=
2ade8 20 63 72 65 61 74 65 5f 74 61 62 6c 65 20 63 72   create_table cr
2ade9 65 61 74 65 5f 74 61 62 6c 65 5f 61 72 67 73 20  eate_table_args 
2adea 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 38  */.      case 28
2adeb 3a 20 2f 2a 20 63 6f 6c 75 6d 6e 6c 69 73 74 20  : /* columnlist 
2adec 3a 3a 3d 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 43  ::= columnlist C
2aded 4f 4d 4d 41 20 63 6f 6c 75 6d 6e 20 2a 2f 0a 20  OMMA column */. 
2adee 20 20 20 20 20 63 61 73 65 20 32 39 3a 20 2f 2a       case 29: /*
2adef 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 3a 3a 3d 20   columnlist ::= 
2adf0 63 6f 6c 75 6d 6e 20 2a 2f 0a 20 20 20 20 20 20  column */.      
2adf1 63 61 73 65 20 33 37 3a 20 2f 2a 20 74 79 70 65  case 37: /* type
2adf2 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2adf3 73 65 20 34 34 3a 20 2f 2a 20 73 69 67 6e 65 64  se 44: /* signed
2adf4 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f   ::= plus_num */
2adf5 0a 20 20 20 20 20 20 63 61 73 65 20 34 35 3a 20  .      case 45: 
2adf6 2f 2a 20 73 69 67 6e 65 64 20 3a 3a 3d 20 6d 69  /* signed ::= mi
2adf7 6e 75 73 5f 6e 75 6d 20 2a 2f 0a 20 20 20 20 20  nus_num */.     
2adf8 20 63 61 73 65 20 34 36 3a 20 2f 2a 20 63 61 72   case 46: /* car
2adf9 67 6c 69 73 74 20 3a 3a 3d 20 63 61 72 67 6c 69  glist ::= cargli
2adfa 73 74 20 63 61 72 67 20 2a 2f 0a 20 20 20 20 20  st carg */.     
2adfb 20 63 61 73 65 20 34 37 3a 20 2f 2a 20 63 61 72   case 47: /* car
2adfc 67 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20  glist ::= */.   
2adfd 20 20 20 63 61 73 65 20 34 38 3a 20 2f 2a 20 63     case 48: /* c
2adfe 61 72 67 20 3a 3a 3d 20 43 4f 4e 53 54 52 41 49  arg ::= CONSTRAI
2adff 4e 54 20 6e 6d 20 63 63 6f 6e 73 20 2a 2f 0a 20  NT nm ccons */. 
2ae00 20 20 20 20 20 63 61 73 65 20 34 39 3a 20 2f 2a       case 49: /*
2ae01 20 63 61 72 67 20 3a 3a 3d 20 63 63 6f 6e 73 20   carg ::= ccons 
2ae02 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 35 35  */.      case 55
2ae03 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 4e  : /* ccons ::= N
2ae04 55 4c 4c 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 20 20  ULL onconf */.  
2ae05 20 20 20 20 63 61 73 65 20 38 32 3a 20 2f 2a 20      case 82: /* 
2ae06 63 6f 6e 73 6c 69 73 74 20 3a 3a 3d 20 63 6f 6e  conslist ::= con
2ae07 73 6c 69 73 74 20 43 4f 4d 4d 41 20 74 63 6f 6e  slist COMMA tcon
2ae08 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  s */.      case 
2ae09 38 33 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 20  83: /* conslist 
2ae0a 3a 3a 3d 20 63 6f 6e 73 6c 69 73 74 20 74 63 6f  ::= conslist tco
2ae0b 6e 73 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  ns */.      case
2ae0c 20 38 34 3a 20 2f 2a 20 63 6f 6e 73 6c 69 73 74   84: /* conslist
2ae0d 20 3a 3a 3d 20 74 63 6f 6e 73 20 2a 2f 0a 20 20   ::= tcons */.  
2ae0e 20 20 20 20 63 61 73 65 20 38 35 3a 20 2f 2a 20      case 85: /* 
2ae0f 74 63 6f 6e 73 20 3a 3a 3d 20 43 4f 4e 53 54 52  tcons ::= CONSTR
2ae10 41 49 4e 54 20 6e 6d 20 2a 2f 0a 20 20 20 20 20  AINT nm */.     
2ae11 20 63 61 73 65 20 32 36 30 3a 20 2f 2a 20 70 6c   case 260: /* pl
2ae12 75 73 5f 6f 70 74 20 3a 3a 3d 20 50 4c 55 53 20  us_opt ::= PLUS 
2ae13 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  */.      case 26
2ae14 31 3a 20 2f 2a 20 70 6c 75 73 5f 6f 70 74 20 3a  1: /* plus_opt :
2ae15 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2ae16 20 32 37 31 3a 20 2f 2a 20 66 6f 72 65 61 63 68   271: /* foreach
2ae17 5f 63 6c 61 75 73 65 20 3a 3a 3d 20 2a 2f 0a 20  _clause ::= */. 
2ae18 20 20 20 20 20 63 61 73 65 20 32 37 32 3a 20 2f       case 272: /
2ae19 2a 20 66 6f 72 65 61 63 68 5f 63 6c 61 75 73 65  * foreach_clause
2ae1a 20 3a 3a 3d 20 46 4f 52 20 45 41 43 48 20 52 4f   ::= FOR EACH RO
2ae1b 57 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  W */.      case 
2ae1c 32 39 32 3a 20 2f 2a 20 64 61 74 61 62 61 73 65  292: /* database
2ae1d 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 44 41 54 41  _kw_opt ::= DATA
2ae1e 42 41 53 45 20 2a 2f 0a 20 20 20 20 20 20 63 61  BASE */.      ca
2ae1f 73 65 20 32 39 33 3a 20 2f 2a 20 64 61 74 61 62  se 293: /* datab
2ae20 61 73 65 5f 6b 77 5f 6f 70 74 20 3a 3a 3d 20 2a  ase_kw_opt ::= *
2ae21 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 30 31  /.      case 301
2ae22 3a 20 2f 2a 20 6b 77 63 6f 6c 75 6d 6e 5f 6f 70  : /* kwcolumn_op
2ae23 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  t ::= */.      c
2ae24 61 73 65 20 33 30 32 3a 20 2f 2a 20 6b 77 63 6f  ase 302: /* kwco
2ae25 6c 75 6d 6e 5f 6f 70 74 20 3a 3a 3d 20 43 4f 4c  lumn_opt ::= COL
2ae26 55 4d 4e 4b 57 20 2a 2f 0a 20 20 20 20 20 20 63  UMNKW */.      c
2ae27 61 73 65 20 33 30 36 3a 20 2f 2a 20 76 74 61 62  ase 306: /* vtab
2ae28 61 72 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62  arglist ::= vtab
2ae29 61 72 67 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  arg */.      cas
2ae2a 65 20 33 30 37 3a 20 2f 2a 20 76 74 61 62 61 72  e 307: /* vtabar
2ae2b 67 6c 69 73 74 20 3a 3a 3d 20 76 74 61 62 61 72  glist ::= vtabar
2ae2c 67 6c 69 73 74 20 43 4f 4d 4d 41 20 76 74 61 62  glist COMMA vtab
2ae2d 61 72 67 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  arg */.      cas
2ae2e 65 20 33 30 39 3a 20 2f 2a 20 76 74 61 62 61 72  e 309: /* vtabar
2ae2f 67 20 3a 3a 3d 20 76 74 61 62 61 72 67 20 76 74  g ::= vtabarg vt
2ae30 61 62 61 72 67 74 6f 6b 65 6e 20 2a 2f 0a 20 20  abargtoken */.  
2ae31 20 20 20 20 63 61 73 65 20 33 31 33 3a 20 2f 2a      case 313: /*
2ae32 20 61 6e 79 6c 69 73 74 20 3a 3a 3d 20 2a 2f 0a   anylist ::= */.
2ae33 7b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  {.}.        brea
2ae34 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 35 3a  k;.      case 5:
2ae35 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d 20   /* explain ::= 
2ae36 2a 2f 0a 7b 20 73 71 6c 69 74 65 33 42 65 67 69  */.{ sqlite3Begi
2ae37 6e 50 61 72 73 65 28 70 50 61 72 73 65 2c 20 30  nParse(pParse, 0
2ae38 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ); }.        bre
2ae39 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
2ae3a 3a 20 2f 2a 20 65 78 70 6c 61 69 6e 20 3a 3a 3d  : /* explain ::=
2ae3b 20 45 58 50 4c 41 49 4e 20 2a 2f 0a 7b 20 73 71   EXPLAIN */.{ sq
2ae3c 6c 69 74 65 33 42 65 67 69 6e 50 61 72 73 65 28  lite3BeginParse(
2ae3d 70 50 61 72 73 65 2c 20 31 29 3b 20 7d 0a 20 20  pParse, 1); }.  
2ae3e 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2ae3f 20 20 20 63 61 73 65 20 37 3a 20 2f 2a 20 65 78     case 7: /* ex
2ae40 70 6c 61 69 6e 20 3a 3a 3d 20 45 58 50 4c 41 49  plain ::= EXPLAI
2ae41 4e 20 51 55 45 52 59 20 50 4c 41 4e 20 2a 2f 0a  N QUERY PLAN */.
2ae42 7b 20 73 71 6c 69 74 65 33 42 65 67 69 6e 50 61  { sqlite3BeginPa
2ae43 72 73 65 28 70 50 61 72 73 65 2c 20 32 29 3b 20  rse(pParse, 2); 
2ae44 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ae45 0a 20 20 20 20 20 20 63 61 73 65 20 38 3a 20 2f  .      case 8: /
2ae46 2a 20 63 6d 64 78 20 3a 3a 3d 20 63 6d 64 20 2a  * cmdx ::= cmd *
2ae47 2f 0a 7b 20 73 71 6c 69 74 65 33 46 69 6e 69 73  /.{ sqlite3Finis
2ae48 68 43 6f 64 69 6e 67 28 70 50 61 72 73 65 29 3b  hCoding(pParse);
2ae49 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2ae4a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39 3a 20  ;.      case 9: 
2ae4b 2f 2a 20 63 6d 64 20 3a 3a 3d 20 42 45 47 49 4e  /* cmd ::= BEGIN
2ae4c 20 74 72 61 6e 73 74 79 70 65 20 74 72 61 6e 73   transtype trans
2ae4d 5f 6f 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33  _opt */.{sqlite3
2ae4e 42 65 67 69 6e 54 72 61 6e 73 61 63 74 69 6f 6e  BeginTransaction
2ae4f 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d  (pParse, yymsp[-
2ae50 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b  1].minor.yy316);
2ae51 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ae52 0a 20 20 20 20 20 20 63 61 73 65 20 31 33 3a 20  .      case 13: 
2ae53 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d  /* transtype ::=
2ae54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2ae55 2e 79 79 33 31 36 20 3d 20 54 4b 5f 44 45 46 45  .yy316 = TK_DEFE
2ae56 52 52 45 44 3b 7d 0a 20 20 20 20 20 20 20 20 62  RRED;}.        b
2ae57 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ae58 20 31 34 3a 20 2f 2a 20 74 72 61 6e 73 74 79 70   14: /* transtyp
2ae59 65 20 3a 3a 3d 20 44 45 46 45 52 52 45 44 20 2a  e ::= DEFERRED *
2ae5a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31 35 3a  /.      case 15:
2ae5b 20 2f 2a 20 74 72 61 6e 73 74 79 70 65 20 3a 3a   /* transtype ::
2ae5c 3d 20 49 4d 4d 45 44 49 41 54 45 20 2a 2f 0a 20  = IMMEDIATE */. 
2ae5d 20 20 20 20 20 63 61 73 65 20 31 36 3a 20 2f 2a       case 16: /*
2ae5e 20 74 72 61 6e 73 74 79 70 65 20 3a 3a 3d 20 45   transtype ::= E
2ae5f 58 43 4c 55 53 49 56 45 20 2a 2f 0a 20 20 20 20  XCLUSIVE */.    
2ae60 20 20 63 61 73 65 20 31 30 37 3a 20 2f 2a 20 6d    case 107: /* m
2ae61 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
2ae62 3d 20 55 4e 49 4f 4e 20 2a 2f 0a 20 20 20 20 20  = UNION */.     
2ae63 20 63 61 73 65 20 31 30 39 3a 20 2f 2a 20 6d 75   case 109: /* mu
2ae64 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a 3d  ltiselect_op ::=
2ae65 20 45 58 43 45 50 54 7c 49 4e 54 45 52 53 45 43   EXCEPT|INTERSEC
2ae66 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  T */.{yygotomino
2ae67 72 2e 79 79 33 31 36 20 3d 20 79 79 6d 73 70 5b  r.yy316 = yymsp[
2ae68 30 5d 2e 6d 61 6a 6f 72 3b 7d 0a 20 20 20 20 20  0].major;}.     
2ae69 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2ae6a 63 61 73 65 20 31 37 3a 20 2f 2a 20 63 6d 64 20  case 17: /* cmd 
2ae6b 3a 3a 3d 20 43 4f 4d 4d 49 54 20 74 72 61 6e 73  ::= COMMIT trans
2ae6c 5f 6f 70 74 20 2a 2f 0a 20 20 20 20 20 20 63 61  _opt */.      ca
2ae6d 73 65 20 31 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a  se 18: /* cmd ::
2ae6e 3d 20 45 4e 44 20 74 72 61 6e 73 5f 6f 70 74 20  = END trans_opt 
2ae6f 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 6f 6d 6d 69  */.{sqlite3Commi
2ae70 74 54 72 61 6e 73 61 63 74 69 6f 6e 28 70 50 61  tTransaction(pPa
2ae71 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  rse);}.        b
2ae72 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2ae73 20 31 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20   19: /* cmd ::= 
2ae74 52 4f 4c 4c 42 41 43 4b 20 74 72 61 6e 73 5f 6f  ROLLBACK trans_o
2ae75 70 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 6f  pt */.{sqlite3Ro
2ae76 6c 6c 62 61 63 6b 54 72 61 6e 73 61 63 74 69 6f  llbackTransactio
2ae77 6e 28 70 50 61 72 73 65 29 3b 7d 0a 20 20 20 20  n(pParse);}.    
2ae78 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2ae79 20 63 61 73 65 20 32 31 3a 20 2f 2a 20 63 72 65   case 21: /* cre
2ae7a 61 74 65 5f 74 61 62 6c 65 20 3a 3a 3d 20 43 52  ate_table ::= CR
2ae7b 45 41 54 45 20 74 65 6d 70 20 54 41 42 4c 45 20  EATE temp TABLE 
2ae7c 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20 64  ifnotexists nm d
2ae7d 62 6e 6d 20 2a 2f 0a 7b 0a 20 20 20 73 71 6c 69  bnm */.{.   sqli
2ae7e 74 65 33 53 74 61 72 74 54 61 62 6c 65 28 70 50  te3StartTable(pP
2ae7f 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  arse,&yymsp[-1].
2ae80 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2ae81 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79 79  [0].minor.yy0,yy
2ae82 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2ae83 33 31 36 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32  316,0,0,yymsp[-2
2ae84 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 0a  ].minor.yy316);.
2ae85 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2ae86 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 3a 20  .      case 22: 
2ae87 2f 2a 20 69 66 6e 6f 74 65 78 69 73 74 73 20 3a  /* ifnotexists :
2ae88 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2ae89 20 32 35 3a 20 2f 2a 20 74 65 6d 70 20 3a 3a 3d   25: /* temp ::=
2ae8a 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 36   */.      case 6
2ae8b 33 3a 20 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a  3: /* autoinc ::
2ae8c 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2ae8d 37 37 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65  77: /* init_defe
2ae8e 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
2ae8f 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2ae90 37 39 3a 20 2f 2a 20 69 6e 69 74 5f 64 65 66 65  79: /* init_defe
2ae91 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 3a 3a  rred_pred_opt ::
2ae92 3d 20 49 4e 49 54 49 41 4c 4c 59 20 49 4d 4d 45  = INITIALLY IMME
2ae93 44 49 41 54 45 20 2a 2f 0a 20 20 20 20 20 20 63  DIATE */.      c
2ae94 61 73 65 20 39 30 3a 20 2f 2a 20 64 65 66 65 72  ase 90: /* defer
2ae95 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70 74 20 3a  _subclause_opt :
2ae96 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2ae97 20 31 30 31 3a 20 2f 2a 20 69 66 65 78 69 73 74   101: /* ifexist
2ae98 73 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  s ::= */.      c
2ae99 61 73 65 20 31 31 32 3a 20 2f 2a 20 64 69 73 74  ase 112: /* dist
2ae9a 69 6e 63 74 20 3a 3a 3d 20 41 4c 4c 20 2a 2f 0a  inct ::= ALL */.
2ae9b 20 20 20 20 20 20 63 61 73 65 20 31 31 33 3a 20        case 113: 
2ae9c 2f 2a 20 64 69 73 74 69 6e 63 74 20 3a 3a 3d 20  /* distinct ::= 
2ae9d 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  */.      case 21
2ae9e 36 3a 20 2f 2a 20 62 65 74 77 65 65 6e 5f 6f 70  6: /* between_op
2ae9f 20 3a 3a 3d 20 42 45 54 57 45 45 4e 20 2a 2f 0a   ::= BETWEEN */.
2aea0 20 20 20 20 20 20 63 61 73 65 20 32 31 39 3a 20        case 219: 
2aea1 2f 2a 20 69 6e 5f 6f 70 20 3a 3a 3d 20 49 4e 20  /* in_op ::= IN 
2aea2 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2aea3 79 79 33 31 36 20 3d 20 30 3b 7d 0a 20 20 20 20  yy316 = 0;}.    
2aea4 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2aea5 20 63 61 73 65 20 32 33 3a 20 2f 2a 20 69 66 6e   case 23: /* ifn
2aea6 6f 74 65 78 69 73 74 73 20 3a 3a 3d 20 49 46 20  otexists ::= IF 
2aea7 4e 4f 54 20 45 58 49 53 54 53 20 2a 2f 0a 20 20  NOT EXISTS */.  
2aea8 20 20 20 20 63 61 73 65 20 32 34 3a 20 2f 2a 20      case 24: /* 
2aea9 74 65 6d 70 20 3a 3a 3d 20 54 45 4d 50 20 2a 2f  temp ::= TEMP */
2aeaa 0a 20 20 20 20 20 20 63 61 73 65 20 36 34 3a 20  .      case 64: 
2aeab 2f 2a 20 61 75 74 6f 69 6e 63 20 3a 3a 3d 20 41  /* autoinc ::= A
2aeac 55 54 4f 49 4e 43 52 20 2a 2f 0a 20 20 20 20 20  UTOINCR */.     
2aead 20 63 61 73 65 20 37 38 3a 20 2f 2a 20 69 6e 69   case 78: /* ini
2aeae 74 5f 64 65 66 65 72 72 65 64 5f 70 72 65 64 5f  t_deferred_pred_
2aeaf 6f 70 74 20 3a 3a 3d 20 49 4e 49 54 49 41 4c 4c  opt ::= INITIALL
2aeb0 59 20 44 45 46 45 52 52 45 44 20 2a 2f 0a 20 20  Y DEFERRED */.  
2aeb1 20 20 20 20 63 61 73 65 20 31 30 30 3a 20 2f 2a      case 100: /*
2aeb2 20 69 66 65 78 69 73 74 73 20 3a 3a 3d 20 49 46   ifexists ::= IF
2aeb3 20 45 58 49 53 54 53 20 2a 2f 0a 20 20 20 20 20   EXISTS */.     
2aeb4 20 63 61 73 65 20 31 31 31 3a 20 2f 2a 20 64 69   case 111: /* di
2aeb5 73 74 69 6e 63 74 20 3a 3a 3d 20 44 49 53 54 49  stinct ::= DISTI
2aeb6 4e 43 54 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  NCT */.      cas
2aeb7 65 20 32 31 37 3a 20 2f 2a 20 62 65 74 77 65 65  e 217: /* betwee
2aeb8 6e 5f 6f 70 20 3a 3a 3d 20 4e 4f 54 20 42 45 54  n_op ::= NOT BET
2aeb9 57 45 45 4e 20 2a 2f 0a 20 20 20 20 20 20 63 61  WEEN */.      ca
2aeba 73 65 20 32 32 30 3a 20 2f 2a 20 69 6e 5f 6f 70  se 220: /* in_op
2aebb 20 3a 3a 3d 20 4e 4f 54 20 49 4e 20 2a 2f 0a 7b   ::= NOT IN */.{
2aebc 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31  yygotominor.yy31
2aebd 36 20 3d 20 31 3b 7d 0a 20 20 20 20 20 20 20 20  6 = 1;}.        
2aebe 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2aebf 65 20 32 36 3a 20 2f 2a 20 63 72 65 61 74 65 5f  e 26: /* create_
2aec0 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d 20 4c  table_args ::= L
2aec1 50 20 63 6f 6c 75 6d 6e 6c 69 73 74 20 63 6f 6e  P columnlist con
2aec2 73 6c 69 73 74 5f 6f 70 74 20 52 50 20 2a 2f 0a  slist_opt RP */.
2aec3 7b 0a 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61  {.  sqlite3EndTa
2aec4 62 6c 65 28 70 50 61 72 73 65 2c 26 79 79 6d 73  ble(pParse,&yyms
2aec5 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-1].minor.yy0,
2aec6 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2aec7 79 79 30 2c 30 29 3b 0a 7d 0a 20 20 20 20 20 20  yy0,0);.}.      
2aec8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2aec9 61 73 65 20 32 37 3a 20 2f 2a 20 63 72 65 61 74  ase 27: /* creat
2aeca 65 5f 74 61 62 6c 65 5f 61 72 67 73 20 3a 3a 3d  e_table_args ::=
2aecb 20 41 53 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a   AS select */.{.
2aecc 20 20 73 71 6c 69 74 65 33 45 6e 64 54 61 62 6c    sqlite3EndTabl
2aecd 65 28 70 50 61 72 73 65 2c 30 2c 30 2c 79 79 6d  e(pParse,0,0,yym
2aece 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 33  sp[0].minor.yy43
2aecf 29 3b 0a 20 20 73 71 6c 69 74 65 33 53 65 6c 65  );.  sqlite3Sele
2aed0 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  ctDelete(pParse-
2aed1 3e 64 62 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  >db, yymsp[0].mi
2aed2 6e 6f 72 2e 79 79 34 33 29 3b 0a 7d 0a 20 20 20  nor.yy43);.}.   
2aed3 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2aed4 20 20 63 61 73 65 20 33 30 3a 20 2f 2a 20 63 6f    case 30: /* co
2aed5 6c 75 6d 6e 20 3a 3a 3d 20 63 6f 6c 75 6d 6e 69  lumn ::= columni
2aed6 64 20 74 79 70 65 20 63 61 72 67 6c 69 73 74 20  d type carglist 
2aed7 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2aed8 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70  or.yy0.z = yymsp
2aed9 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-2].minor.yy0.z
2aeda 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2aedb 79 79 30 2e 6e 20 3d 20 28 70 50 61 72 73 65 2d  yy0.n = (pParse-
2aedc 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a 2d 79 79  >sLastToken.z-yy
2aedd 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2aede 30 2e 7a 29 20 2b 20 70 50 61 72 73 65 2d 3e 73  0.z) + pParse->s
2aedf 4c 61 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 7d 0a 20  LastToken.n;.}. 
2aee0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2aee1 20 20 20 20 63 61 73 65 20 33 31 3a 20 2f 2a 20      case 31: /* 
2aee2 63 6f 6c 75 6d 6e 69 64 20 3a 3a 3d 20 6e 6d 20  columnid ::= nm 
2aee3 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 64  */.{.  sqlite3Ad
2aee4 64 43 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 26  dColumn(pParse,&
2aee5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2aee6 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  y0);.  yygotomin
2aee7 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b 30  or.yy0 = yymsp[0
2aee8 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 7d 0a 20  ].minor.yy0;.}. 
2aee9 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2aeea 20 20 20 20 63 61 73 65 20 33 32 3a 20 2f 2a 20      case 32: /* 
2aeeb 69 64 20 3a 3a 3d 20 49 44 20 2a 2f 0a 20 20 20  id ::= ID */.   
2aeec 20 20 20 63 61 73 65 20 33 33 3a 20 2f 2a 20 69     case 33: /* i
2aeed 64 73 20 3a 3a 3d 20 49 44 7c 53 54 52 49 4e 47  ds ::= ID|STRING
2aeee 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33   */.      case 3
2aeef 34 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 49 44 20  4: /* nm ::= ID 
2aef0 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 33 35  */.      case 35
2aef1 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 53 54 52 49  : /* nm ::= STRI
2aef2 4e 47 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  NG */.      case
2aef3 20 33 36 3a 20 2f 2a 20 6e 6d 20 3a 3a 3d 20 4a   36: /* nm ::= J
2aef4 4f 49 4e 5f 4b 57 20 2a 2f 0a 20 20 20 20 20 20  OIN_KW */.      
2aef5 63 61 73 65 20 33 39 3a 20 2f 2a 20 74 79 70 65  case 39: /* type
2aef6 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61  token ::= typena
2aef7 6d 65 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  me */.      case
2aef8 20 34 32 3a 20 2f 2a 20 74 79 70 65 6e 61 6d 65   42: /* typename
2aef9 20 3a 3a 3d 20 69 64 73 20 2a 2f 0a 20 20 20 20   ::= ids */.    
2aefa 20 20 63 61 73 65 20 31 31 39 3a 20 2f 2a 20 61    case 119: /* a
2aefb 73 20 3a 3a 3d 20 41 53 20 6e 6d 20 2a 2f 0a 20  s ::= AS nm */. 
2aefc 20 20 20 20 20 63 61 73 65 20 31 32 30 3a 20 2f       case 120: /
2aefd 2a 20 61 73 20 3a 3a 3d 20 69 64 73 20 2a 2f 0a  * as ::= ids */.
2aefe 20 20 20 20 20 20 63 61 73 65 20 31 33 31 3a 20        case 131: 
2aeff 2f 2a 20 64 62 6e 6d 20 3a 3a 3d 20 44 4f 54 20  /* dbnm ::= DOT 
2af00 6e 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  nm */.      case
2af01 20 31 34 30 3a 20 2f 2a 20 69 6e 64 65 78 65 64   140: /* indexed
2af02 5f 6f 70 74 20 3a 3a 3d 20 49 4e 44 45 58 45 44  _opt ::= INDEXED
2af03 20 42 59 20 6e 6d 20 2a 2f 0a 20 20 20 20 20 20   BY nm */.      
2af04 63 61 73 65 20 32 34 35 3a 20 2f 2a 20 63 6f 6c  case 245: /* col
2af05 6c 61 74 65 20 3a 3a 3d 20 43 4f 4c 4c 41 54 45  late ::= COLLATE
2af06 20 69 64 73 20 2a 2f 0a 20 20 20 20 20 20 63 61   ids */.      ca
2af07 73 65 20 32 35 35 3a 20 2f 2a 20 6e 6d 6e 75 6d  se 255: /* nmnum
2af08 20 3a 3a 3d 20 70 6c 75 73 5f 6e 75 6d 20 2a 2f   ::= plus_num */
2af09 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 36 3a  .      case 256:
2af0a 20 2f 2a 20 6e 6d 6e 75 6d 20 3a 3a 3d 20 6e 6d   /* nmnum ::= nm
2af0b 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2af0c 35 37 3a 20 2f 2a 20 70 6c 75 73 5f 6e 75 6d 20  57: /* plus_num 
2af0d 3a 3a 3d 20 70 6c 75 73 5f 6f 70 74 20 6e 75 6d  ::= plus_opt num
2af0e 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ber */.      cas
2af0f 65 20 32 35 38 3a 20 2f 2a 20 6d 69 6e 75 73 5f  e 258: /* minus_
2af10 6e 75 6d 20 3a 3a 3d 20 4d 49 4e 55 53 20 6e 75  num ::= MINUS nu
2af11 6d 62 65 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  mber */.      ca
2af12 73 65 20 32 35 39 3a 20 2f 2a 20 6e 75 6d 62 65  se 259: /* numbe
2af13 72 20 3a 3a 3d 20 49 4e 54 45 47 45 52 7c 46 4c  r ::= INTEGER|FL
2af14 4f 41 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  OAT */.{yygotomi
2af15 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
2af16 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a 20  0].minor.yy0;}. 
2af17 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2af18 20 20 20 20 63 61 73 65 20 33 38 3a 20 2f 2a 20      case 38: /* 
2af19 74 79 70 65 20 3a 3a 3d 20 74 79 70 65 74 6f 6b  type ::= typetok
2af1a 65 6e 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41 64  en */.{sqlite3Ad
2af1b 64 43 6f 6c 75 6d 6e 54 79 70 65 28 70 50 61 72  dColumnType(pPar
2af1c 73 65 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se,&yymsp[0].min
2af1d 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20  or.yy0);}.      
2af1e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2af1f 61 73 65 20 34 30 3a 20 2f 2a 20 74 79 70 65 74  ase 40: /* typet
2af20 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70 65 6e 61 6d  oken ::= typenam
2af21 65 20 4c 50 20 73 69 67 6e 65 64 20 52 50 20 2a  e LP signed RP *
2af22 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f  /.{.  yygotomino
2af23 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70 5b  r.yy0.z = yymsp[
2af24 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b  -3].minor.yy0.z;
2af25 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2af26 79 30 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30 5d  y0.n = &yymsp[0]
2af27 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79 6d  .minor.yy0.z[yym
2af28 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
2af29 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d  n] - yymsp[-3].m
2af2a 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 7d 0a 20 20  inor.yy0.z;.}.  
2af2b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2af2c 20 20 20 63 61 73 65 20 34 31 3a 20 2f 2a 20 74     case 41: /* t
2af2d 79 70 65 74 6f 6b 65 6e 20 3a 3a 3d 20 74 79 70  ypetoken ::= typ
2af2e 65 6e 61 6d 65 20 4c 50 20 73 69 67 6e 65 64 20  ename LP signed 
2af2f 43 4f 4d 4d 41 20 73 69 67 6e 65 64 20 52 50 20  COMMA signed RP 
2af30 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2af31 6f 72 2e 79 79 30 2e 7a 20 3d 20 79 79 6d 73 70  or.yy0.z = yymsp
2af32 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-5].minor.yy0.z
2af33 3b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  ;.  yygotominor.
2af34 79 79 30 2e 6e 20 3d 20 26 79 79 6d 73 70 5b 30  yy0.n = &yymsp[0
2af35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a 5b 79 79  ].minor.yy0.z[yy
2af36 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2af37 2e 6e 5d 20 2d 20 79 79 6d 73 70 5b 2d 35 5d 2e  .n] - yymsp[-5].
2af38 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3b 0a 7d 0a 20  minor.yy0.z;.}. 
2af39 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2af3a 20 20 20 20 63 61 73 65 20 34 33 3a 20 2f 2a 20      case 43: /* 
2af3b 74 79 70 65 6e 61 6d 65 20 3a 3a 3d 20 74 79 70  typename ::= typ
2af3c 65 6e 61 6d 65 20 69 64 73 20 2a 2f 0a 7b 79 79  ename ids */.{yy
2af3d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 3d  gotominor.yy0.z=
2af3e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2af3f 79 79 30 2e 7a 3b 20 79 79 67 6f 74 6f 6d 69 6e  yy0.z; yygotomin
2af40 6f 72 2e 79 79 30 2e 6e 3d 79 79 6d 73 70 5b 30  or.yy0.n=yymsp[0
2af41 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 2b 28 79  ].minor.yy0.n+(y
2af42 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2af43 30 2e 7a 2d 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  0.z-yymsp[-1].mi
2af44 6e 6f 72 2e 79 79 30 2e 7a 29 3b 7d 0a 20 20 20  nor.yy0.z);}.   
2af45 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2af46 20 20 63 61 73 65 20 35 30 3a 20 2f 2a 20 63 63    case 50: /* cc
2af47 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55 4c 54 20  ons ::= DEFAULT 
2af48 74 65 72 6d 20 2a 2f 0a 20 20 20 20 20 20 63 61  term */.      ca
2af49 73 65 20 35 32 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 52: /* ccons 
2af4a 3a 3a 3d 20 44 45 46 41 55 4c 54 20 50 4c 55 53  ::= DEFAULT PLUS
2af4b 20 74 65 72 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65   term */.{sqlite
2af4c 33 41 64 64 44 65 66 61 75 6c 74 56 61 6c 75 65  3AddDefaultValue
2af4d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30 5d  (pParse,yymsp[0]
2af4e 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 7d 0a  .minor.yy450);}.
2af4f 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2af50 20 20 20 20 20 63 61 73 65 20 35 31 3a 20 2f 2a       case 51: /*
2af51 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46 41 55   ccons ::= DEFAU
2af52 4c 54 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f  LT LP expr RP */
2af53 0a 7b 73 71 6c 69 74 65 33 41 64 64 44 65 66 61  .{sqlite3AddDefa
2af54 75 6c 74 56 61 6c 75 65 28 70 50 61 72 73 65 2c  ultValue(pParse,
2af55 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2af56 79 79 34 35 30 29 3b 7d 0a 20 20 20 20 20 20 20  yy450);}.       
2af57 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2af58 73 65 20 35 33 3a 20 2f 2a 20 63 63 6f 6e 73 20  se 53: /* ccons 
2af59 3a 3a 3d 20 44 45 46 41 55 4c 54 20 4d 49 4e 55  ::= DEFAULT MINU
2af5a 53 20 74 65 72 6d 20 2a 2f 0a 7b 0a 20 20 45 78  S term */.{.  Ex
2af5b 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33 50  pr *p = sqlite3P
2af5c 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2af5d 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70 5b 30 5d  UMINUS, yymsp[0]
2af5e 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c  .minor.yy450, 0,
2af5f 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78   0);.  sqlite3Ex
2af60 70 72 53 70 61 6e 28 70 2c 26 79 79 6d 73 70 5b  prSpan(p,&yymsp[
2af61 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -1].minor.yy0,&y
2af62 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2af63 34 35 30 2d 3e 73 70 61 6e 29 3b 0a 20 20 73 71  450->span);.  sq
2af64 6c 69 74 65 33 41 64 64 44 65 66 61 75 6c 74 56  lite3AddDefaultV
2af65 61 6c 75 65 28 70 50 61 72 73 65 2c 70 29 3b 0a  alue(pParse,p);.
2af66 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2af67 0a 20 20 20 20 20 20 63 61 73 65 20 35 34 3a 20  .      case 54: 
2af68 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 44 45 46  /* ccons ::= DEF
2af69 41 55 4c 54 20 69 64 20 2a 2f 0a 7b 0a 20 20 45  AULT id */.{.  E
2af6a 78 70 72 20 2a 70 20 3d 20 73 71 6c 69 74 65 33  xpr *p = sqlite3
2af6b 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2af6c 5f 53 54 52 49 4e 47 2c 20 30 2c 20 30 2c 20 26  _STRING, 0, 0, &
2af6d 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2af6e 79 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 41 64  y0);.  sqlite3Ad
2af6f 64 44 65 66 61 75 6c 74 56 61 6c 75 65 28 70 50  dDefaultValue(pP
2af70 61 72 73 65 2c 70 29 3b 0a 7d 0a 20 20 20 20 20  arse,p);.}.     
2af71 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2af72 63 61 73 65 20 35 36 3a 20 2f 2a 20 63 63 6f 6e  case 56: /* ccon
2af73 73 20 3a 3a 3d 20 4e 4f 54 20 4e 55 4c 4c 20 6f  s ::= NOT NULL o
2af74 6e 63 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65  nconf */.{sqlite
2af75 33 41 64 64 4e 6f 74 4e 75 6c 6c 28 70 50 61 72  3AddNotNull(pPar
2af76 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  se, yymsp[0].min
2af77 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20 20 20 20  or.yy316);}.    
2af78 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2af79 20 63 61 73 65 20 35 37 3a 20 2f 2a 20 63 63 6f   case 57: /* cco
2af7a 6e 73 20 3a 3a 3d 20 50 52 49 4d 41 52 59 20 4b  ns ::= PRIMARY K
2af7b 45 59 20 73 6f 72 74 6f 72 64 65 72 20 6f 6e 63  EY sortorder onc
2af7c 6f 6e 66 20 61 75 74 6f 69 6e 63 20 2a 2f 0a 7b  onf autoinc */.{
2af7d 73 71 6c 69 74 65 33 41 64 64 50 72 69 6d 61 72  sqlite3AddPrimar
2af7e 79 4b 65 79 28 70 50 61 72 73 65 2c 30 2c 79 79  yKey(pParse,0,yy
2af7f 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2af80 33 31 36 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  316,yymsp[0].min
2af81 6f 72 2e 79 79 33 31 36 2c 79 79 6d 73 70 5b 2d  or.yy316,yymsp[-
2af82 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b  2].minor.yy316);
2af83 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2af84 0a 20 20 20 20 20 20 63 61 73 65 20 35 38 3a 20  .      case 58: 
2af85 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 55 4e 49  /* ccons ::= UNI
2af86 51 55 45 20 6f 6e 63 6f 6e 66 20 2a 2f 0a 7b 73  QUE onconf */.{s
2af87 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64 65  qlite3CreateInde
2af88 78 28 70 50 61 72 73 65 2c 30 2c 30 2c 30 2c 30  x(pParse,0,0,0,0
2af89 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  ,yymsp[0].minor.
2af8a 79 79 33 31 36 2c 30 2c 30 2c 30 2c 30 29 3b 7d  yy316,0,0,0,0);}
2af8b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2af8c 20 20 20 20 20 20 63 61 73 65 20 35 39 3a 20 2f        case 59: /
2af8d 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20 43 48 45 43  * ccons ::= CHEC
2af8e 4b 20 4c 50 20 65 78 70 72 20 52 50 20 2a 2f 0a  K LP expr RP */.
2af8f 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65 63 6b  {sqlite3AddCheck
2af90 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72 73  Constraint(pPars
2af91 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  e,yymsp[-1].mino
2af92 72 2e 79 79 34 35 30 29 3b 7d 0a 20 20 20 20 20  r.yy450);}.     
2af93 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2af94 63 61 73 65 20 36 30 3a 20 2f 2a 20 63 63 6f 6e  case 60: /* ccon
2af95 73 20 3a 3a 3d 20 52 45 46 45 52 45 4e 43 45 53  s ::= REFERENCES
2af96 20 6e 6d 20 69 64 78 6c 69 73 74 5f 6f 70 74 20   nm idxlist_opt 
2af97 72 65 66 61 72 67 73 20 2a 2f 0a 7b 73 71 6c 69  refargs */.{sqli
2af98 74 65 33 43 72 65 61 74 65 46 6f 72 65 69 67 6e  te3CreateForeign
2af99 4b 65 79 28 70 50 61 72 73 65 2c 30 2c 26 79 79  Key(pParse,0,&yy
2af9a 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2af9b 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  0,yymsp[-1].mino
2af9c 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b 30 5d  r.yy242,yymsp[0]
2af9d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a  .minor.yy316);}.
2af9e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2af9f 20 20 20 20 20 63 61 73 65 20 36 31 3a 20 2f 2a       case 61: /*
2afa0 20 63 63 6f 6e 73 20 3a 3a 3d 20 64 65 66 65 72   ccons ::= defer
2afa1 5f 73 75 62 63 6c 61 75 73 65 20 2a 2f 0a 7b 73  _subclause */.{s
2afa2 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65 69  qlite3DeferForei
2afa3 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 79 79 6d  gnKey(pParse,yym
2afa4 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  sp[0].minor.yy31
2afa5 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  6);}.        bre
2afa6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
2afa7 32 3a 20 2f 2a 20 63 63 6f 6e 73 20 3a 3a 3d 20  2: /* ccons ::= 
2afa8 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a 7b  COLLATE ids */.{
2afa9 73 71 6c 69 74 65 33 41 64 64 43 6f 6c 6c 61 74  sqlite3AddCollat
2afaa 65 54 79 70 65 28 70 50 61 72 73 65 2c 20 26 79  eType(pParse, &y
2afab 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2afac 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2afad 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
2afae 35 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a  5: /* refargs ::
2afaf 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  = */.{ yygotomin
2afb0 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 52 65  or.yy316 = OE_Re
2afb1 73 74 72 69 63 74 20 2a 20 30 78 30 31 30 31 30  strict * 0x01010
2afb2 31 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  1; }.        bre
2afb3 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 36  ak;.      case 6
2afb4 36 3a 20 2f 2a 20 72 65 66 61 72 67 73 20 3a 3a  6: /* refargs ::
2afb5 3d 20 72 65 66 61 72 67 73 20 72 65 66 61 72 67  = refargs refarg
2afb6 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f   */.{ yygotomino
2afb7 72 2e 79 79 33 31 36 20 3d 20 28 79 79 6d 73 70  r.yy316 = (yymsp
2afb8 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36  [-1].minor.yy316
2afb9 20 26 20 7e 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e   & ~yymsp[0].min
2afba 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b 29 20 7c  or.yy207.mask) |
2afbb 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2afbc 79 79 32 30 37 2e 76 61 6c 75 65 3b 20 7d 0a 20  yy207.value; }. 
2afbd 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2afbe 20 20 20 20 63 61 73 65 20 36 37 3a 20 2f 2a 20      case 67: /* 
2afbf 72 65 66 61 72 67 20 3a 3a 3d 20 4d 41 54 43 48  refarg ::= MATCH
2afc0 20 6e 6d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d   nm */.{ yygotom
2afc1 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61 6c 75 65  inor.yy207.value
2afc2 20 3d 20 30 3b 20 20 20 20 20 79 79 67 6f 74 6f   = 0;     yygoto
2afc3 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 6d 61 73 6b  minor.yy207.mask
2afc4 20 3d 20 30 78 30 30 30 30 30 30 3b 20 7d 0a 20   = 0x000000; }. 
2afc5 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2afc6 20 20 20 20 63 61 73 65 20 36 38 3a 20 2f 2a 20      case 68: /* 
2afc7 72 65 66 61 72 67 20 3a 3a 3d 20 4f 4e 20 44 45  refarg ::= ON DE
2afc8 4c 45 54 45 20 72 65 66 61 63 74 20 2a 2f 0a 7b  LETE refact */.{
2afc9 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32   yygotominor.yy2
2afca 30 37 2e 76 61 6c 75 65 20 3d 20 79 79 6d 73 70  07.value = yymsp
2afcb 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b  [0].minor.yy316;
2afcc 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72       yygotominor
2afcd 2e 79 79 32 30 37 2e 6d 61 73 6b 20 3d 20 30 78  .yy207.mask = 0x
2afce 30 30 30 30 66 66 3b 20 7d 0a 20 20 20 20 20 20  0000ff; }.      
2afcf 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2afd0 61 73 65 20 36 39 3a 20 2f 2a 20 72 65 66 61 72  ase 69: /* refar
2afd1 67 20 3a 3a 3d 20 4f 4e 20 55 50 44 41 54 45 20  g ::= ON UPDATE 
2afd2 72 65 66 61 63 74 20 2a 2f 0a 7b 20 79 79 67 6f  refact */.{ yygo
2afd3 74 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 76 61  tominor.yy207.va
2afd4 6c 75 65 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  lue = yymsp[0].m
2afd5 69 6e 6f 72 2e 79 79 33 31 36 3c 3c 38 3b 20 20  inor.yy316<<8;  
2afd6 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 30  yygotominor.yy20
2afd7 37 2e 6d 61 73 6b 20 3d 20 30 78 30 30 66 66 30  7.mask = 0x00ff0
2afd8 30 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0; }.        bre
2afd9 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37  ak;.      case 7
2afda 30 3a 20 2f 2a 20 72 65 66 61 72 67 20 3a 3a 3d  0: /* refarg ::=
2afdb 20 4f 4e 20 49 4e 53 45 52 54 20 72 65 66 61 63   ON INSERT refac
2afdc 74 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e  t */.{ yygotomin
2afdd 6f 72 2e 79 79 32 30 37 2e 76 61 6c 75 65 20 3d  or.yy207.value =
2afde 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2afdf 79 79 33 31 36 3c 3c 31 36 3b 20 79 79 67 6f 74  yy316<<16; yygot
2afe0 6f 6d 69 6e 6f 72 2e 79 79 32 30 37 2e 6d 61 73  ominor.yy207.mas
2afe1 6b 20 3d 20 30 78 66 66 30 30 30 30 3b 20 7d 0a  k = 0xff0000; }.
2afe2 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2afe3 20 20 20 20 20 63 61 73 65 20 37 31 3a 20 2f 2a       case 71: /*
2afe4 20 72 65 66 61 63 74 20 3a 3a 3d 20 53 45 54 20   refact ::= SET 
2afe5 4e 55 4c 4c 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f  NULL */.{ yygoto
2afe6 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45  minor.yy316 = OE
2afe7 5f 53 65 74 4e 75 6c 6c 3b 20 7d 0a 20 20 20 20  _SetNull; }.    
2afe8 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2afe9 20 63 61 73 65 20 37 32 3a 20 2f 2a 20 72 65 66   case 72: /* ref
2afea 61 63 74 20 3a 3a 3d 20 53 45 54 20 44 45 46 41  act ::= SET DEFA
2afeb 55 4c 54 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  ULT */.{ yygotom
2afec 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f  inor.yy316 = OE_
2afed 53 65 74 44 66 6c 74 3b 20 7d 0a 20 20 20 20 20  SetDflt; }.     
2afee 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2afef 63 61 73 65 20 37 33 3a 20 2f 2a 20 72 65 66 61  case 73: /* refa
2aff0 63 74 20 3a 3a 3d 20 43 41 53 43 41 44 45 20 2a  ct ::= CASCADE *
2aff1 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  /.{ yygotominor.
2aff2 79 79 33 31 36 20 3d 20 4f 45 5f 43 61 73 63 61  yy316 = OE_Casca
2aff3 64 65 3b 20 7d 0a 20 20 20 20 20 20 20 20 62 72  de; }.        br
2aff4 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2aff5 37 34 3a 20 2f 2a 20 72 65 66 61 63 74 20 3a 3a  74: /* refact ::
2aff6 3d 20 52 45 53 54 52 49 43 54 20 2a 2f 0a 7b 20  = RESTRICT */.{ 
2aff7 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31  yygotominor.yy31
2aff8 36 20 3d 20 4f 45 5f 52 65 73 74 72 69 63 74 3b  6 = OE_Restrict;
2aff9 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2affa 3b 0a 20 20 20 20 20 20 63 61 73 65 20 37 35 3a  ;.      case 75:
2affb 20 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61   /* defer_subcla
2affc 75 73 65 20 3a 3a 3d 20 4e 4f 54 20 44 45 46 45  use ::= NOT DEFE
2affd 52 52 41 42 4c 45 20 69 6e 69 74 5f 64 65 66 65  RRABLE init_defe
2affe 72 72 65 64 5f 70 72 65 64 5f 6f 70 74 20 2a 2f  rred_pred_opt */
2afff 0a 20 20 20 20 20 20 63 61 73 65 20 37 36 3a 20  .      case 76: 
2b000 2f 2a 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  /* defer_subclau
2b001 73 65 20 3a 3a 3d 20 44 45 46 45 52 52 41 42 4c  se ::= DEFERRABL
2b002 45 20 69 6e 69 74 5f 64 65 66 65 72 72 65 64 5f  E init_deferred_
2b003 70 72 65 64 5f 6f 70 74 20 2a 2f 0a 20 20 20 20  pred_opt */.    
2b004 20 20 63 61 73 65 20 39 31 3a 20 2f 2a 20 64 65    case 91: /* de
2b005 66 65 72 5f 73 75 62 63 6c 61 75 73 65 5f 6f 70  fer_subclause_op
2b006 74 20 3a 3a 3d 20 64 65 66 65 72 5f 73 75 62 63  t ::= defer_subc
2b007 6c 61 75 73 65 20 2a 2f 0a 20 20 20 20 20 20 63  lause */.      c
2b008 61 73 65 20 39 33 3a 20 2f 2a 20 6f 6e 63 6f 6e  ase 93: /* oncon
2b009 66 20 3a 3a 3d 20 4f 4e 20 43 4f 4e 46 4c 49 43  f ::= ON CONFLIC
2b00a 54 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f  T resolvetype */
2b00b 0a 20 20 20 20 20 20 63 61 73 65 20 39 35 3a 20  .      case 95: 
2b00c 2f 2a 20 6f 72 63 6f 6e 66 20 3a 3a 3d 20 4f 52  /* orconf ::= OR
2b00d 20 72 65 73 6f 6c 76 65 74 79 70 65 20 2a 2f 0a   resolvetype */.
2b00e 20 20 20 20 20 20 63 61 73 65 20 39 36 3a 20 2f        case 96: /
2b00f 2a 20 72 65 73 6f 6c 76 65 74 79 70 65 20 3a 3a  * resolvetype ::
2b010 3d 20 72 61 69 73 65 74 79 70 65 20 2a 2f 0a 20  = raisetype */. 
2b011 20 20 20 20 20 63 61 73 65 20 31 36 39 3a 20 2f       case 169: /
2b012 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a 3a 3d  * insert_cmd ::=
2b013 20 49 4e 53 45 52 54 20 6f 72 63 6f 6e 66 20 2a   INSERT orconf *
2b014 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2b015 79 33 31 36 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  y316 = yymsp[0].
2b016 6d 69 6e 6f 72 2e 79 79 33 31 36 3b 7d 0a 20 20  minor.yy316;}.  
2b017 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b018 20 20 20 63 61 73 65 20 38 30 3a 20 2f 2a 20 63     case 80: /* c
2b019 6f 6e 73 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20  onslist_opt ::= 
2b01a 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2b01b 79 79 30 2e 6e 20 3d 20 30 3b 20 79 79 67 6f 74  yy0.n = 0; yygot
2b01c 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d 20 30  ominor.yy0.z = 0
2b01d 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2b01e 3b 0a 20 20 20 20 20 20 63 61 73 65 20 38 31 3a  ;.      case 81:
2b01f 20 2f 2a 20 63 6f 6e 73 6c 69 73 74 5f 6f 70 74   /* conslist_opt
2b020 20 3a 3a 3d 20 43 4f 4d 4d 41 20 63 6f 6e 73 6c   ::= COMMA consl
2b021 69 73 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ist */.{yygotomi
2b022 6e 6f 72 2e 79 79 30 20 3d 20 79 79 6d 73 70 5b  nor.yy0 = yymsp[
2b023 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 7d 0a  -1].minor.yy0;}.
2b024 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b025 20 20 20 20 20 63 61 73 65 20 38 36 3a 20 2f 2a       case 86: /*
2b026 20 74 63 6f 6e 73 20 3a 3a 3d 20 50 52 49 4d 41   tcons ::= PRIMA
2b027 52 59 20 4b 45 59 20 4c 50 20 69 64 78 6c 69 73  RY KEY LP idxlis
2b028 74 20 61 75 74 6f 69 6e 63 20 52 50 20 6f 6e 63  t autoinc RP onc
2b029 6f 6e 66 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  onf */.{sqlite3A
2b02a 64 64 50 72 69 6d 61 72 79 4b 65 79 28 70 50 61  ddPrimaryKey(pPa
2b02b 72 73 65 2c 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  rse,yymsp[-3].mi
2b02c 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b  nor.yy242,yymsp[
2b02d 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c 79  0].minor.yy316,y
2b02e 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2b02f 79 33 31 36 2c 30 29 3b 7d 0a 20 20 20 20 20 20  y316,0);}.      
2b030 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b031 61 73 65 20 38 37 3a 20 2f 2a 20 74 63 6f 6e 73  ase 87: /* tcons
2b032 20 3a 3a 3d 20 55 4e 49 51 55 45 20 4c 50 20 69   ::= UNIQUE LP i
2b033 64 78 6c 69 73 74 20 52 50 20 6f 6e 63 6f 6e 66  dxlist RP onconf
2b034 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 43 72 65 61   */.{sqlite3Crea
2b035 74 65 49 6e 64 65 78 28 70 50 61 72 73 65 2c 30  teIndex(pParse,0
2b036 2c 30 2c 30 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ,0,0,yymsp[-2].m
2b037 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70  inor.yy242,yymsp
2b038 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c  [0].minor.yy316,
2b039 30 2c 30 2c 30 2c 30 29 3b 7d 0a 20 20 20 20 20  0,0,0,0);}.     
2b03a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b03b 63 61 73 65 20 38 38 3a 20 2f 2a 20 74 63 6f 6e  case 88: /* tcon
2b03c 73 20 3a 3a 3d 20 43 48 45 43 4b 20 4c 50 20 65  s ::= CHECK LP e
2b03d 78 70 72 20 52 50 20 6f 6e 63 6f 6e 66 20 2a 2f  xpr RP onconf */
2b03e 0a 7b 73 71 6c 69 74 65 33 41 64 64 43 68 65 63  .{sqlite3AddChec
2b03f 6b 43 6f 6e 73 74 72 61 69 6e 74 28 70 50 61 72  kConstraint(pPar
2b040 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  se,yymsp[-2].min
2b041 6f 72 2e 79 79 34 35 30 29 3b 7d 0a 20 20 20 20  or.yy450);}.    
2b042 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b043 20 63 61 73 65 20 38 39 3a 20 2f 2a 20 74 63 6f   case 89: /* tco
2b044 6e 73 20 3a 3a 3d 20 46 4f 52 45 49 47 4e 20 4b  ns ::= FOREIGN K
2b045 45 59 20 4c 50 20 69 64 78 6c 69 73 74 20 52 50  EY LP idxlist RP
2b046 20 52 45 46 45 52 45 4e 43 45 53 20 6e 6d 20 69   REFERENCES nm i
2b047 64 78 6c 69 73 74 5f 6f 70 74 20 72 65 66 61 72  dxlist_opt refar
2b048 67 73 20 64 65 66 65 72 5f 73 75 62 63 6c 61 75  gs defer_subclau
2b049 73 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 20 20  se_opt */.{.    
2b04a 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 6f 72  sqlite3CreateFor
2b04b 65 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 20  eignKey(pParse, 
2b04c 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
2b04d 79 79 32 34 32 2c 20 26 79 79 6d 73 70 5b 2d 33  yy242, &yymsp[-3
2b04e 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2b04f 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-2].minor.yy2
2b050 34 32 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  42, yymsp[-1].mi
2b051 6e 6f 72 2e 79 79 33 31 36 29 3b 0a 20 20 20 20  nor.yy316);.    
2b052 73 71 6c 69 74 65 33 44 65 66 65 72 46 6f 72 65  sqlite3DeferFore
2b053 69 67 6e 4b 65 79 28 70 50 61 72 73 65 2c 20 79  ignKey(pParse, y
2b054 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b055 33 31 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  316);.}.        
2b056 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b057 65 20 39 32 3a 20 2f 2a 20 6f 6e 63 6f 6e 66 20  e 92: /* onconf 
2b058 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2b059 65 20 39 34 3a 20 2f 2a 20 6f 72 63 6f 6e 66 20  e 94: /* orconf 
2b05a 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69  ::= */.{yygotomi
2b05b 6e 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 44  nor.yy316 = OE_D
2b05c 65 66 61 75 6c 74 3b 7d 0a 20 20 20 20 20 20 20  efault;}.       
2b05d 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b05e 73 65 20 39 37 3a 20 2f 2a 20 72 65 73 6f 6c 76  se 97: /* resolv
2b05f 65 74 79 70 65 20 3a 3a 3d 20 49 47 4e 4f 52 45  etype ::= IGNORE
2b060 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2b061 2e 79 79 33 31 36 20 3d 20 4f 45 5f 49 67 6e 6f  .yy316 = OE_Igno
2b062 72 65 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  re;}.        bre
2b063 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 39  ak;.      case 9
2b064 38 3a 20 2f 2a 20 72 65 73 6f 6c 76 65 74 79 70  8: /* resolvetyp
2b065 65 20 3a 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f  e ::= REPLACE */
2b066 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 30 3a  .      case 170:
2b067 20 2f 2a 20 69 6e 73 65 72 74 5f 63 6d 64 20 3a   /* insert_cmd :
2b068 3a 3d 20 52 45 50 4c 41 43 45 20 2a 2f 0a 7b 79  := REPLACE */.{y
2b069 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36  ygotominor.yy316
2b06a 20 3d 20 4f 45 5f 52 65 70 6c 61 63 65 3b 7d 0a   = OE_Replace;}.
2b06b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b06c 20 20 20 20 20 63 61 73 65 20 39 39 3a 20 2f 2a       case 99: /*
2b06d 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 41   cmd ::= DROP TA
2b06e 42 4c 45 20 69 66 65 78 69 73 74 73 20 66 75 6c  BLE ifexists ful
2b06f 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c  lname */.{.  sql
2b070 69 74 65 33 44 72 6f 70 54 61 62 6c 65 28 70 50  ite3DropTable(pP
2b071 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  arse, yymsp[0].m
2b072 69 6e 6f 72 2e 79 79 34 31 39 2c 20 30 2c 20 79  inor.yy419, 0, y
2b073 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2b074 79 33 31 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y316);.}.       
2b075 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b076 73 65 20 31 30 32 3a 20 2f 2a 20 63 6d 64 20 3a  se 102: /* cmd :
2b077 3a 3d 20 43 52 45 41 54 45 20 74 65 6d 70 20 56  := CREATE temp V
2b078 49 45 57 20 69 66 6e 6f 74 65 78 69 73 74 73 20  IEW ifnotexists 
2b079 6e 6d 20 64 62 6e 6d 20 41 53 20 73 65 6c 65 63  nm dbnm AS selec
2b07a 74 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  t */.{.  sqlite3
2b07b 43 72 65 61 74 65 56 69 65 77 28 70 50 61 72 73  CreateView(pPars
2b07c 65 2c 20 26 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69  e, &yymsp[-7].mi
2b07d 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70 5b  nor.yy0, &yymsp[
2b07e 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26  -3].minor.yy0, &
2b07f 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2b080 79 79 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  yy0, yymsp[0].mi
2b081 6e 6f 72 2e 79 79 34 33 2c 20 79 79 6d 73 70 5b  nor.yy43, yymsp[
2b082 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c  -6].minor.yy316,
2b083 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72   yymsp[-4].minor
2b084 2e 79 79 33 31 36 29 3b 0a 7d 0a 20 20 20 20 20  .yy316);.}.     
2b085 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b086 63 61 73 65 20 31 30 33 3a 20 2f 2a 20 63 6d 64  case 103: /* cmd
2b087 20 3a 3a 3d 20 44 52 4f 50 20 56 49 45 57 20 69   ::= DROP VIEW i
2b088 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61 6d 65  fexists fullname
2b089 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33 44   */.{.  sqlite3D
2b08a 72 6f 70 54 61 62 6c 65 28 70 50 61 72 73 65 2c  ropTable(pParse,
2b08b 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2b08c 79 79 34 31 39 2c 20 31 2c 20 79 79 6d 73 70 5b  yy419, 1, yymsp[
2b08d 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29  -1].minor.yy316)
2b08e 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2b08f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 30  k;.      case 10
2b090 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 73 65  4: /* cmd ::= se
2b091 6c 65 63 74 20 2a 2f 0a 7b 0a 20 20 53 65 6c 65  lect */.{.  Sele
2b092 63 74 44 65 73 74 20 64 65 73 74 20 3d 20 7b 53  ctDest dest = {S
2b093 52 54 5f 4f 75 74 70 75 74 2c 20 30 2c 20 30 2c  RT_Output, 0, 0,
2b094 20 30 2c 20 30 7d 3b 0a 20 20 73 71 6c 69 74 65   0, 0};.  sqlite
2b095 33 53 65 6c 65 63 74 28 70 50 61 72 73 65 2c 20  3Select(pParse, 
2b096 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b097 79 34 33 2c 20 26 64 65 73 74 29 3b 0a 20 20 73  y43, &dest);.  s
2b098 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2b099 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2b09a 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b09b 34 33 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  43);.}.        b
2b09c 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b09d 20 31 30 35 3a 20 2f 2a 20 73 65 6c 65 63 74 20   105: /* select 
2b09e 3a 3a 3d 20 6f 6e 65 73 65 6c 65 63 74 20 2a 2f  ::= oneselect */
2b09f 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 38 3a  .      case 128:
2b0a0 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 5f 70   /* seltablist_p
2b0a1 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 65 63 74 20  aren ::= select 
2b0a2 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2b0a3 79 79 34 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e  yy43 = yymsp[0].
2b0a4 6d 69 6e 6f 72 2e 79 79 34 33 3b 7d 0a 20 20 20  minor.yy43;}.   
2b0a5 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b0a6 20 20 63 61 73 65 20 31 30 36 3a 20 2f 2a 20 73    case 106: /* s
2b0a7 65 6c 65 63 74 20 3a 3a 3d 20 73 65 6c 65 63 74  elect ::= select
2b0a8 20 6d 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20   multiselect_op 
2b0a9 6f 6e 65 73 65 6c 65 63 74 20 2a 2f 0a 7b 0a 20  oneselect */.{. 
2b0aa 20 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69   if( yymsp[0].mi
2b0ab 6e 6f 72 2e 79 79 34 33 20 29 7b 0a 20 20 20 20  nor.yy43 ){.    
2b0ac 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b0ad 79 34 33 2d 3e 6f 70 20 3d 20 79 79 6d 73 70 5b  y43->op = yymsp[
2b0ae 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b  -1].minor.yy316;
2b0af 0a 20 20 20 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  .    yymsp[0].mi
2b0b0 6e 6f 72 2e 79 79 34 33 2d 3e 70 50 72 69 6f 72  nor.yy43->pPrior
2b0b1 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
2b0b2 6f 72 2e 79 79 34 33 3b 0a 20 20 7d 65 6c 73 65  or.yy43;.  }else
2b0b3 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c  {.    sqlite3Sel
2b0b4 65 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65  ectDelete(pParse
2b0b5 2d 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e  ->db, yymsp[-2].
2b0b6 6d 69 6e 6f 72 2e 79 79 34 33 29 3b 0a 20 20 7d  minor.yy43);.  }
2b0b7 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2b0b8 79 34 33 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  y43 = yymsp[0].m
2b0b9 69 6e 6f 72 2e 79 79 34 33 3b 0a 7d 0a 20 20 20  inor.yy43;.}.   
2b0ba 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b0bb 20 20 63 61 73 65 20 31 30 38 3a 20 2f 2a 20 6d    case 108: /* m
2b0bc 75 6c 74 69 73 65 6c 65 63 74 5f 6f 70 20 3a 3a  ultiselect_op ::
2b0bd 3d 20 55 4e 49 4f 4e 20 41 4c 4c 20 2a 2f 0a 7b  = UNION ALL */.{
2b0be 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31  yygotominor.yy31
2b0bf 36 20 3d 20 54 4b 5f 41 4c 4c 3b 7d 0a 20 20 20  6 = TK_ALL;}.   
2b0c0 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b0c1 20 20 63 61 73 65 20 31 31 30 3a 20 2f 2a 20 6f    case 110: /* o
2b0c2 6e 65 73 65 6c 65 63 74 20 3a 3a 3d 20 53 45 4c  neselect ::= SEL
2b0c3 45 43 54 20 64 69 73 74 69 6e 63 74 20 73 65 6c  ECT distinct sel
2b0c4 63 6f 6c 6c 69 73 74 20 66 72 6f 6d 20 77 68 65  collist from whe
2b0c5 72 65 5f 6f 70 74 20 67 72 6f 75 70 62 79 5f 6f  re_opt groupby_o
2b0c6 70 74 20 68 61 76 69 6e 67 5f 6f 70 74 20 6f 72  pt having_opt or
2b0c7 64 65 72 62 79 5f 6f 70 74 20 6c 69 6d 69 74 5f  derby_opt limit_
2b0c8 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74  opt */.{.  yygot
2b0c9 6f 6d 69 6e 6f 72 2e 79 79 34 33 20 3d 20 73 71  ominor.yy43 = sq
2b0ca 6c 69 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70  lite3SelectNew(p
2b0cb 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e  Parse,yymsp[-6].
2b0cc 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73  minor.yy242,yyms
2b0cd 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-5].minor.yy41
2b0ce 39 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  9,yymsp[-4].mino
2b0cf 72 2e 79 79 34 35 30 2c 79 79 6d 73 70 5b 2d 33  r.yy450,yymsp[-3
2b0d0 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79  ].minor.yy242,yy
2b0d1 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b0d2 34 35 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  450,yymsp[-1].mi
2b0d3 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b  nor.yy242,yymsp[
2b0d4 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c  -7].minor.yy316,
2b0d5 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b0d6 79 38 34 2e 70 4c 69 6d 69 74 2c 79 79 6d 73 70  y84.pLimit,yymsp
2b0d7 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 38 34 2e 70  [0].minor.yy84.p
2b0d8 4f 66 66 73 65 74 29 3b 0a 7d 0a 20 20 20 20 20  Offset);.}.     
2b0d9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b0da 63 61 73 65 20 31 31 34 3a 20 2f 2a 20 73 63 6c  case 114: /* scl
2b0db 70 20 3a 3a 3d 20 73 65 6c 63 6f 6c 6c 69 73 74  p ::= selcollist
2b0dc 20 43 4f 4d 4d 41 20 2a 2f 0a 20 20 20 20 20 20   COMMA */.      
2b0dd 63 61 73 65 20 32 34 31 3a 20 2f 2a 20 69 64 78  case 241: /* idx
2b0de 6c 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 4c 50 20  list_opt ::= LP 
2b0df 69 64 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 79  idxlist RP */.{y
2b0e0 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2b0e1 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
2b0e2 6f 72 2e 79 79 32 34 32 3b 7d 0a 20 20 20 20 20  or.yy242;}.     
2b0e3 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b0e4 63 61 73 65 20 31 31 35 3a 20 2f 2a 20 73 63 6c  case 115: /* scl
2b0e5 70 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63  p ::= */.      c
2b0e6 61 73 65 20 31 34 34 3a 20 2f 2a 20 6f 72 64 65  ase 144: /* orde
2b0e7 72 62 79 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20  rby_opt ::= */. 
2b0e8 20 20 20 20 20 63 61 73 65 20 31 35 32 3a 20 2f       case 152: /
2b0e9 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a  * groupby_opt ::
2b0ea 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2b0eb 32 33 34 3a 20 2f 2a 20 65 78 70 72 6c 69 73 74  234: /* exprlist
2b0ec 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61   ::= */.      ca
2b0ed 73 65 20 32 34 30 3a 20 2f 2a 20 69 64 78 6c 69  se 240: /* idxli
2b0ee 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  st_opt ::= */.{y
2b0ef 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2b0f0 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62   = 0;}.        b
2b0f1 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b0f2 20 31 31 36 3a 20 2f 2a 20 73 65 6c 63 6f 6c 6c   116: /* selcoll
2b0f3 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 65 78 70  ist ::= sclp exp
2b0f4 72 20 61 73 20 2a 2f 0a 7b 0a 20 20 20 79 79 67  r as */.{.   yyg
2b0f5 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d  otominor.yy242 =
2b0f6 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74   sqlite3ExprList
2b0f7 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79  Append(pParse,yy
2b0f8 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b0f9 32 34 32 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  242,yymsp[-1].mi
2b0fa 6e 6f 72 2e 79 79 34 35 30 2c 79 79 6d 73 70 5b  nor.yy450,yymsp[
2b0fb 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3f 26  0].minor.yy0.n?&
2b0fc 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b0fd 79 30 3a 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  y0:0);.}.       
2b0fe 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b0ff 73 65 20 31 31 37 3a 20 2f 2a 20 73 65 6c 63 6f  se 117: /* selco
2b100 6c 6c 69 73 74 20 3a 3a 3d 20 73 63 6c 70 20 53  llist ::= sclp S
2b101 54 41 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20  TAR */.{.  Expr 
2b102 2a 70 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  *p = sqlite3PExp
2b103 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 41 4c 4c  r(pParse, TK_ALL
2b104 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 79 79  , 0, 0, 0);.  yy
2b105 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20  gotominor.yy242 
2b106 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2b107 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 20  tAppend(pParse, 
2b108 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2b109 79 79 32 34 32 2c 20 70 2c 20 30 29 3b 0a 7d 0a  yy242, p, 0);.}.
2b10a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b10b 20 20 20 20 20 63 61 73 65 20 31 31 38 3a 20 2f       case 118: /
2b10c 2a 20 73 65 6c 63 6f 6c 6c 69 73 74 20 3a 3a 3d  * selcollist ::=
2b10d 20 73 63 6c 70 20 6e 6d 20 44 4f 54 20 53 54 41   sclp nm DOT STA
2b10e 52 20 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 70  R */.{.  Expr *p
2b10f 52 69 67 68 74 20 3d 20 73 71 6c 69 74 65 33 50  Right = sqlite3P
2b110 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2b111 41 4c 4c 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73  ALL, 0, 0, &yyms
2b112 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2b113 0a 20 20 45 78 70 72 20 2a 70 4c 65 66 74 20 3d  .  Expr *pLeft =
2b114 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2b115 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20  arse, TK_ID, 0, 
2b116 30 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  0, &yymsp[-2].mi
2b117 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72  nor.yy0);.  Expr
2b118 20 2a 70 44 6f 74 20 3d 20 73 71 6c 69 74 65 33   *pDot = sqlite3
2b119 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2b11a 5f 44 4f 54 2c 20 70 4c 65 66 74 2c 20 70 52 69  _DOT, pLeft, pRi
2b11b 67 68 74 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74  ght, 0);.  yygot
2b11c 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73  ominor.yy242 = s
2b11d 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2b11e 70 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73  pend(pParse,yyms
2b11f 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34  p[-3].minor.yy24
2b120 32 2c 20 70 44 6f 74 2c 20 30 29 3b 0a 7d 0a 20  2, pDot, 0);.}. 
2b121 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b122 20 20 20 20 63 61 73 65 20 31 32 31 3a 20 2f 2a      case 121: /*
2b123 20 61 73 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f   as ::= */.{yygo
2b124 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 20 3d 20  tominor.yy0.n = 
2b125 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2b126 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32  k;.      case 12
2b127 32 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20 2a  2: /* from ::= *
2b128 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2b129 79 34 31 39 20 3d 20 73 71 6c 69 74 65 33 44 62  y419 = sqlite3Db
2b12a 4d 61 6c 6c 6f 63 5a 65 72 6f 28 70 50 61 72 73  MallocZero(pPars
2b12b 65 2d 3e 64 62 2c 20 73 69 7a 65 6f 66 28 2a 79  e->db, sizeof(*y
2b12c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39  ygotominor.yy419
2b12d 29 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  ));}.        bre
2b12e 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2b12f 32 33 3a 20 2f 2a 20 66 72 6f 6d 20 3a 3a 3d 20  23: /* from ::= 
2b130 46 52 4f 4d 20 73 65 6c 74 61 62 6c 69 73 74 20  FROM seltablist 
2b131 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2b132 6f 72 2e 79 79 34 31 39 20 3d 20 79 79 6d 73 70  or.yy419 = yymsp
2b133 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 3b  [0].minor.yy419;
2b134 0a 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73  .  sqlite3SrcLis
2b135 74 53 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79  tShiftJoinType(y
2b136 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39  ygotominor.yy419
2b137 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2b138 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2b139 32 34 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66 69  24: /* stl_prefi
2b13a 78 20 3a 3a 3d 20 73 65 6c 74 61 62 6c 69 73 74  x ::= seltablist
2b13b 20 6a 6f 69 6e 6f 70 20 2a 2f 0a 7b 0a 20 20 20   joinop */.{.   
2b13c 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31  yygotominor.yy41
2b13d 39 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  9 = yymsp[-1].mi
2b13e 6e 6f 72 2e 79 79 34 31 39 3b 0a 20 20 20 69 66  nor.yy419;.   if
2b13f 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2b140 34 31 39 20 26 26 20 79 79 67 6f 74 6f 6d 69 6e  419 && yygotomin
2b141 6f 72 2e 79 79 34 31 39 2d 3e 6e 53 72 63 3e 30  or.yy419->nSrc>0
2b142 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79   ) yygotominor.y
2b143 79 34 31 39 2d 3e 61 5b 79 79 67 6f 74 6f 6d 69  y419->a[yygotomi
2b144 6e 6f 72 2e 79 79 34 31 39 2d 3e 6e 53 72 63 2d  nor.yy419->nSrc-
2b145 31 5d 2e 6a 6f 69 6e 74 79 70 65 20 3d 20 79 79  1].jointype = yy
2b146 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  msp[0].minor.yy3
2b147 31 36 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  16;.}.        br
2b148 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2b149 31 32 35 3a 20 2f 2a 20 73 74 6c 5f 70 72 65 66  125: /* stl_pref
2b14a 69 78 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  ix ::= */.{yygot
2b14b 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 20 3d 20 30  ominor.yy419 = 0
2b14c 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2b14d 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 32 36  ;.      case 126
2b14e 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73 74 20  : /* seltablist 
2b14f 3a 3a 3d 20 73 74 6c 5f 70 72 65 66 69 78 20 6e  ::= stl_prefix n
2b150 6d 20 64 62 6e 6d 20 61 73 20 69 6e 64 65 78 65  m dbnm as indexe
2b151 64 5f 6f 70 74 20 6f 6e 5f 6f 70 74 20 75 73 69  d_opt on_opt usi
2b152 6e 67 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20 79 79  ng_opt */.{.  yy
2b153 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 20  gotominor.yy419 
2b154 3d 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74  = sqlite3SrcList
2b155 41 70 70 65 6e 64 46 72 6f 6d 54 65 72 6d 28 70  AppendFromTerm(p
2b156 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 36 5d 2e  Parse,yymsp[-6].
2b157 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 26 79 79 6d  minor.yy419,&yym
2b158 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-5].minor.yy0
2b159 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
2b15a 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d  r.yy0,&yymsp[-3]
2b15b 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30 2c 79 79 6d  .minor.yy0,0,yym
2b15c 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2b15d 35 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  50,yymsp[0].mino
2b15e 72 2e 79 79 33 35 32 29 3b 0a 20 20 73 71 6c 69  r.yy352);.  sqli
2b15f 74 65 33 53 72 63 4c 69 73 74 49 6e 64 65 78 65  te3SrcListIndexe
2b160 64 42 79 28 70 50 61 72 73 65 2c 20 79 79 67 6f  dBy(pParse, yygo
2b161 74 6f 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 20 26  tominor.yy419, &
2b162 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2b163 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
2b164 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b165 65 20 31 32 37 3a 20 2f 2a 20 73 65 6c 74 61 62  e 127: /* seltab
2b166 6c 69 73 74 20 3a 3a 3d 20 73 74 6c 5f 70 72 65  list ::= stl_pre
2b167 66 69 78 20 4c 50 20 73 65 6c 74 61 62 6c 69 73  fix LP seltablis
2b168 74 5f 70 61 72 65 6e 20 52 50 20 61 73 20 6f 6e  t_paren RP as on
2b169 5f 6f 70 74 20 75 73 69 6e 67 5f 6f 70 74 20 2a  _opt using_opt *
2b16a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69  /.{.    yygotomi
2b16b 6e 6f 72 2e 79 79 34 31 39 20 3d 20 73 71 6c 69  nor.yy419 = sqli
2b16c 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64  te3SrcListAppend
2b16d 46 72 6f 6d 54 65 72 6d 28 70 50 61 72 73 65 2c  FromTerm(pParse,
2b16e 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e  yymsp[-6].minor.
2b16f 79 79 34 31 39 2c 30 2c 30 2c 26 79 79 6d 73 70  yy419,0,0,&yymsp
2b170 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 79  [-2].minor.yy0,y
2b171 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2b172 79 34 33 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  y43,yymsp[-1].mi
2b173 6e 6f 72 2e 79 79 34 35 30 2c 79 79 6d 73 70 5b  nor.yy450,yymsp[
2b174 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32 29 3b  0].minor.yy352);
2b175 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
2b176 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2b177 32 39 3a 20 2f 2a 20 73 65 6c 74 61 62 6c 69 73  29: /* seltablis
2b178 74 5f 70 61 72 65 6e 20 3a 3a 3d 20 73 65 6c 74  t_paren ::= selt
2b179 61 62 6c 69 73 74 20 2a 2f 0a 7b 0a 20 20 20 20  ablist */.{.    
2b17a 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 53   sqlite3SrcListS
2b17b 68 69 66 74 4a 6f 69 6e 54 79 70 65 28 79 79 6d  hiftJoinType(yym
2b17c 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  sp[0].minor.yy41
2b17d 39 29 3b 0a 20 20 20 20 20 79 79 67 6f 74 6f 6d  9);.     yygotom
2b17e 69 6e 6f 72 2e 79 79 34 33 20 3d 20 73 71 6c 69  inor.yy43 = sqli
2b17f 74 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61  te3SelectNew(pPa
2b180 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d 2e 6d  rse,0,yymsp[0].m
2b181 69 6e 6f 72 2e 79 79 34 31 39 2c 30 2c 30 2c 30  inor.yy419,0,0,0
2b182 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 7d 0a 20  ,0,0,0,0);.  }. 
2b183 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b184 20 20 20 20 63 61 73 65 20 31 33 30 3a 20 2f 2a      case 130: /*
2b185 20 64 62 6e 6d 20 3a 3a 3d 20 2a 2f 0a 20 20 20   dbnm ::= */.   
2b186 20 20 20 63 61 73 65 20 31 33 39 3a 20 2f 2a 20     case 139: /* 
2b187 69 6e 64 65 78 65 64 5f 6f 70 74 20 3a 3a 3d 20  indexed_opt ::= 
2b188 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2b189 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f 74 6f 6d  yy0.z=0; yygotom
2b18a 69 6e 6f 72 2e 79 79 30 2e 6e 3d 30 3b 7d 0a 20  inor.yy0.n=0;}. 
2b18b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b18c 20 20 20 20 63 61 73 65 20 31 33 32 3a 20 2f 2a      case 132: /*
2b18d 20 66 75 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 6e 6d   fullname ::= nm
2b18e 20 64 62 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74 6f   dbnm */.{yygoto
2b18f 6d 69 6e 6f 72 2e 79 79 34 31 39 20 3d 20 73 71  minor.yy419 = sq
2b190 6c 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65  lite3SrcListAppe
2b191 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c  nd(pParse->db,0,
2b192 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72  &yymsp[-1].minor
2b193 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d  .yy0,&yymsp[0].m
2b194 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2b195 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b196 20 63 61 73 65 20 31 33 33 3a 20 2f 2a 20 6a 6f   case 133: /* jo
2b197 69 6e 6f 70 20 3a 3a 3d 20 43 4f 4d 4d 41 7c 4a  inop ::= COMMA|J
2b198 4f 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  OIN */.{ yygotom
2b199 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 4a 54 5f  inor.yy316 = JT_
2b19a 49 4e 4e 45 52 3b 20 7d 0a 20 20 20 20 20 20 20  INNER; }.       
2b19b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b19c 73 65 20 31 33 34 3a 20 2f 2a 20 6a 6f 69 6e 6f  se 134: /* joino
2b19d 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b 57 20 4a 4f  p ::= JOIN_KW JO
2b19e 49 4e 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69  IN */.{ yygotomi
2b19f 6e 6f 72 2e 79 79 33 31 36 20 3d 20 73 71 6c 69  nor.yy316 = sqli
2b1a0 74 65 33 4a 6f 69 6e 54 79 70 65 28 70 50 61 72  te3JoinType(pPar
2b1a1 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  se,&yymsp[-1].mi
2b1a2 6e 6f 72 2e 79 79 30 2c 30 2c 30 29 3b 20 7d 0a  nor.yy0,0,0); }.
2b1a3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b1a4 20 20 20 20 20 63 61 73 65 20 31 33 35 3a 20 2f       case 135: /
2b1a5 2a 20 6a 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49  * joinop ::= JOI
2b1a6 4e 5f 4b 57 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a  N_KW nm JOIN */.
2b1a7 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2b1a8 33 31 36 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69  316 = sqlite3Joi
2b1a9 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
2b1aa 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b1ab 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0,&yymsp[-1].min
2b1ac 6f 72 2e 79 79 30 2c 30 29 3b 20 7d 0a 20 20 20  or.yy0,0); }.   
2b1ad 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b1ae 20 20 63 61 73 65 20 31 33 36 3a 20 2f 2a 20 6a    case 136: /* j
2b1af 6f 69 6e 6f 70 20 3a 3a 3d 20 4a 4f 49 4e 5f 4b  oinop ::= JOIN_K
2b1b0 57 20 6e 6d 20 6e 6d 20 4a 4f 49 4e 20 2a 2f 0a  W nm nm JOIN */.
2b1b1 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  { yygotominor.yy
2b1b2 33 31 36 20 3d 20 73 71 6c 69 74 65 33 4a 6f 69  316 = sqlite3Joi
2b1b3 6e 54 79 70 65 28 70 50 61 72 73 65 2c 26 79 79  nType(pParse,&yy
2b1b4 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2b1b5 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0,&yymsp[-2].min
2b1b6 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 31  or.yy0,&yymsp[-1
2b1b7 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 20 7d 0a  ].minor.yy0); }.
2b1b8 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b1b9 20 20 20 20 20 63 61 73 65 20 31 33 37 3a 20 2f       case 137: /
2b1ba 2a 20 6f 6e 5f 6f 70 74 20 3a 3a 3d 20 4f 4e 20  * on_opt ::= ON 
2b1bb 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2b1bc 73 65 20 31 34 38 3a 20 2f 2a 20 73 6f 72 74 69  se 148: /* sorti
2b1bd 74 65 6d 20 3a 3a 3d 20 65 78 70 72 20 2a 2f 0a  tem ::= expr */.
2b1be 20 20 20 20 20 20 63 61 73 65 20 31 35 35 3a 20        case 155: 
2b1bf 2f 2a 20 68 61 76 69 6e 67 5f 6f 70 74 20 3a 3a  /* having_opt ::
2b1c0 3d 20 48 41 56 49 4e 47 20 65 78 70 72 20 2a 2f  = HAVING expr */
2b1c1 0a 20 20 20 20 20 20 63 61 73 65 20 31 36 32 3a  .      case 162:
2b1c2 20 2f 2a 20 77 68 65 72 65 5f 6f 70 74 20 3a 3a   /* where_opt ::
2b1c3 3d 20 57 48 45 52 45 20 65 78 70 72 20 2a 2f 0a  = WHERE expr */.
2b1c4 20 20 20 20 20 20 63 61 73 65 20 31 37 37 3a 20        case 177: 
2b1c5 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 74 65 72 6d  /* expr ::= term
2b1c6 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32   */.      case 2
2b1c7 30 35 3a 20 2f 2a 20 65 73 63 61 70 65 20 3a 3a  05: /* escape ::
2b1c8 3d 20 45 53 43 41 50 45 20 65 78 70 72 20 2a 2f  = ESCAPE expr */
2b1c9 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 39 3a  .      case 229:
2b1ca 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a   /* case_else ::
2b1cb 3d 20 45 4c 53 45 20 65 78 70 72 20 2a 2f 0a 20  = ELSE expr */. 
2b1cc 20 20 20 20 20 63 61 73 65 20 32 33 31 3a 20 2f       case 231: /
2b1cd 2a 20 63 61 73 65 5f 6f 70 65 72 61 6e 64 20 3a  * case_operand :
2b1ce 3a 3d 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f  := expr */.{yygo
2b1cf 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20  tominor.yy450 = 
2b1d0 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b1d1 79 34 35 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  y450;}.        b
2b1d2 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b1d3 20 31 33 38 3a 20 2f 2a 20 6f 6e 5f 6f 70 74 20   138: /* on_opt 
2b1d4 3a 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73  ::= */.      cas
2b1d5 65 20 31 35 34 3a 20 2f 2a 20 68 61 76 69 6e 67  e 154: /* having
2b1d6 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 20 20 20 20  _opt ::= */.    
2b1d7 20 20 63 61 73 65 20 31 36 31 3a 20 2f 2a 20 77    case 161: /* w
2b1d8 68 65 72 65 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a  here_opt ::= */.
2b1d9 20 20 20 20 20 20 63 61 73 65 20 32 30 36 3a 20        case 206: 
2b1da 2f 2a 20 65 73 63 61 70 65 20 3a 3a 3d 20 2a 2f  /* escape ::= */
2b1db 0a 20 20 20 20 20 20 63 61 73 65 20 32 33 30 3a  .      case 230:
2b1dc 20 2f 2a 20 63 61 73 65 5f 65 6c 73 65 20 3a 3a   /* case_else ::
2b1dd 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2b1de 32 33 32 3a 20 2f 2a 20 63 61 73 65 5f 6f 70 65  232: /* case_ope
2b1df 72 61 6e 64 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  rand ::= */.{yyg
2b1e0 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2b1e1 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2b1e2 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2b1e3 34 31 3a 20 2f 2a 20 69 6e 64 65 78 65 64 5f 6f  41: /* indexed_o
2b1e4 70 74 20 3a 3a 3d 20 4e 4f 54 20 49 4e 44 45 58  pt ::= NOT INDEX
2b1e5 45 44 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  ED */.{yygotomin
2b1e6 6f 72 2e 79 79 30 2e 7a 3d 30 3b 20 79 79 67 6f  or.yy0.z=0; yygo
2b1e7 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 6e 3d 31 3b  tominor.yy0.n=1;
2b1e8 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2b1e9 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 32 3a  .      case 142:
2b1ea 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a 3a   /* using_opt ::
2b1eb 3d 20 55 53 49 4e 47 20 4c 50 20 69 6e 73 63 6f  = USING LP insco
2b1ec 6c 6c 69 73 74 20 52 50 20 2a 2f 0a 20 20 20 20  llist RP */.    
2b1ed 20 20 63 61 73 65 20 31 37 34 3a 20 2f 2a 20 69    case 174: /* i
2b1ee 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 3a 3a  nscollist_opt ::
2b1ef 3d 20 4c 50 20 69 6e 73 63 6f 6c 6c 69 73 74 20  = LP inscollist 
2b1f0 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  RP */.{yygotomin
2b1f1 6f 72 2e 79 79 33 35 32 20 3d 20 79 79 6d 73 70  or.yy352 = yymsp
2b1f2 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32  [-1].minor.yy352
2b1f3 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2b1f4 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 34 33  ;.      case 143
2b1f5 3a 20 2f 2a 20 75 73 69 6e 67 5f 6f 70 74 20 3a  : /* using_opt :
2b1f6 3a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  := */.      case
2b1f7 20 31 37 33 3a 20 2f 2a 20 69 6e 73 63 6f 6c 6c   173: /* inscoll
2b1f8 69 73 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b  ist_opt ::= */.{
2b1f9 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 35  yygotominor.yy35
2b1fa 32 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20  2 = 0;}.        
2b1fb 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b1fc 65 20 31 34 35 3a 20 2f 2a 20 6f 72 64 65 72 62  e 145: /* orderb
2b1fd 79 5f 6f 70 74 20 3a 3a 3d 20 4f 52 44 45 52 20  y_opt ::= ORDER 
2b1fe 42 59 20 73 6f 72 74 6c 69 73 74 20 2a 2f 0a 20  BY sortlist */. 
2b1ff 20 20 20 20 20 63 61 73 65 20 31 35 33 3a 20 2f       case 153: /
2b200 2a 20 67 72 6f 75 70 62 79 5f 6f 70 74 20 3a 3a  * groupby_opt ::
2b201 3d 20 47 52 4f 55 50 20 42 59 20 6e 65 78 70 72  = GROUP BY nexpr
2b202 6c 69 73 74 20 2a 2f 0a 20 20 20 20 20 20 63 61  list */.      ca
2b203 73 65 20 32 33 33 3a 20 2f 2a 20 65 78 70 72 6c  se 233: /* exprl
2b204 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c 69 73  ist ::= nexprlis
2b205 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  t */.{yygotomino
2b206 72 2e 79 79 32 34 32 20 3d 20 79 79 6d 73 70 5b  r.yy242 = yymsp[
2b207 30 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 3b 7d  0].minor.yy242;}
2b208 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b209 20 20 20 20 20 20 63 61 73 65 20 31 34 36 3a 20        case 146: 
2b20a 2f 2a 20 73 6f 72 74 6c 69 73 74 20 3a 3a 3d 20  /* sortlist ::= 
2b20b 73 6f 72 74 6c 69 73 74 20 43 4f 4d 4d 41 20 73  sortlist COMMA s
2b20c 6f 72 74 69 74 65 6d 20 73 6f 72 74 6f 72 64 65  ortitem sortorde
2b20d 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  r */.{.  yygotom
2b20e 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73 71 6c  inor.yy242 = sql
2b20f 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2b210 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b  nd(pParse,yymsp[
2b211 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c  -3].minor.yy242,
2b212 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2b213 79 79 34 35 30 2c 30 29 3b 0a 20 20 69 66 28 20  yy450,0);.  if( 
2b214 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2b215 32 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  2 ) yygotominor.
2b216 79 79 32 34 32 2d 3e 61 5b 79 79 67 6f 74 6f 6d  yy242->a[yygotom
2b217 69 6e 6f 72 2e 79 79 32 34 32 2d 3e 6e 45 78 70  inor.yy242->nExp
2b218 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72 20 3d  r-1].sortOrder =
2b219 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2b21a 79 79 33 31 36 3b 0a 7d 0a 20 20 20 20 20 20 20  yy316;.}.       
2b21b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b21c 73 65 20 31 34 37 3a 20 2f 2a 20 73 6f 72 74 6c  se 147: /* sortl
2b21d 69 73 74 20 3a 3a 3d 20 73 6f 72 74 69 74 65 6d  ist ::= sortitem
2b21e 20 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a   sortorder */.{.
2b21f 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79    yygotominor.yy
2b220 32 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70  242 = sqlite3Exp
2b221 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72  rListAppend(pPar
2b222 73 65 2c 30 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  se,0,yymsp[-1].m
2b223 69 6e 6f 72 2e 79 79 34 35 30 2c 30 29 3b 0a 20  inor.yy450,0);. 
2b224 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   if( yygotominor
2b225 2e 79 79 32 34 32 20 26 26 20 79 79 67 6f 74 6f  .yy242 && yygoto
2b226 6d 69 6e 6f 72 2e 79 79 32 34 32 2d 3e 61 20 29  minor.yy242->a )
2b227 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32   yygotominor.yy2
2b228 34 32 2d 3e 61 5b 30 5d 2e 73 6f 72 74 4f 72 64  42->a[0].sortOrd
2b229 65 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  er = yymsp[0].mi
2b22a 6e 6f 72 2e 79 79 33 31 36 3b 0a 7d 0a 20 20 20  nor.yy316;.}.   
2b22b 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b22c 20 20 63 61 73 65 20 31 34 39 3a 20 2f 2a 20 73    case 149: /* s
2b22d 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 41 53 43  ortorder ::= ASC
2b22e 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2b22f 35 31 3a 20 2f 2a 20 73 6f 72 74 6f 72 64 65 72  51: /* sortorder
2b230 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d   ::= */.{yygotom
2b231 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 53 51 4c  inor.yy316 = SQL
2b232 49 54 45 5f 53 4f 5f 41 53 43 3b 7d 0a 20 20 20  ITE_SO_ASC;}.   
2b233 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b234 20 20 63 61 73 65 20 31 35 30 3a 20 2f 2a 20 73    case 150: /* s
2b235 6f 72 74 6f 72 64 65 72 20 3a 3a 3d 20 44 45 53  ortorder ::= DES
2b236 43 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  C */.{yygotomino
2b237 72 2e 79 79 33 31 36 20 3d 20 53 51 4c 49 54 45  r.yy316 = SQLITE
2b238 5f 53 4f 5f 44 45 53 43 3b 7d 0a 20 20 20 20 20  _SO_DESC;}.     
2b239 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b23a 63 61 73 65 20 31 35 36 3a 20 2f 2a 20 6c 69 6d  case 156: /* lim
2b23b 69 74 5f 6f 70 74 20 3a 3a 3d 20 2a 2f 0a 7b 79  it_opt ::= */.{y
2b23c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 38 34 2e  ygotominor.yy84.
2b23d 70 4c 69 6d 69 74 20 3d 20 30 3b 20 79 79 67 6f  pLimit = 0; yygo
2b23e 74 6f 6d 69 6e 6f 72 2e 79 79 38 34 2e 70 4f 66  tominor.yy84.pOf
2b23f 66 73 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20  fset = 0;}.     
2b240 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b241 63 61 73 65 20 31 35 37 3a 20 2f 2a 20 6c 69 6d  case 157: /* lim
2b242 69 74 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54  it_opt ::= LIMIT
2b243 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f   expr */.{yygoto
2b244 6d 69 6e 6f 72 2e 79 79 38 34 2e 70 4c 69 6d 69  minor.yy84.pLimi
2b245 74 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  t = yymsp[0].min
2b246 6f 72 2e 79 79 34 35 30 3b 20 79 79 67 6f 74 6f  or.yy450; yygoto
2b247 6d 69 6e 6f 72 2e 79 79 38 34 2e 70 4f 66 66 73  minor.yy84.pOffs
2b248 65 74 20 3d 20 30 3b 7d 0a 20 20 20 20 20 20 20  et = 0;}.       
2b249 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b24a 73 65 20 31 35 38 3a 20 2f 2a 20 6c 69 6d 69 74  se 158: /* limit
2b24b 5f 6f 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65  _opt ::= LIMIT e
2b24c 78 70 72 20 4f 46 46 53 45 54 20 65 78 70 72 20  xpr OFFSET expr 
2b24d 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2b24e 79 79 38 34 2e 70 4c 69 6d 69 74 20 3d 20 79 79  yy84.pLimit = yy
2b24f 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b250 34 35 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  450; yygotominor
2b251 2e 79 79 38 34 2e 70 4f 66 66 73 65 74 20 3d 20  .yy84.pOffset = 
2b252 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b253 79 34 35 30 3b 7d 0a 20 20 20 20 20 20 20 20 62  y450;}.        b
2b254 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b255 20 31 35 39 3a 20 2f 2a 20 6c 69 6d 69 74 5f 6f   159: /* limit_o
2b256 70 74 20 3a 3a 3d 20 4c 49 4d 49 54 20 65 78 70  pt ::= LIMIT exp
2b257 72 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a 2f 0a  r COMMA expr */.
2b258 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 38  {yygotominor.yy8
2b259 34 2e 70 4f 66 66 73 65 74 20 3d 20 79 79 6d 73  4.pOffset = yyms
2b25a 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35  p[-2].minor.yy45
2b25b 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  0; yygotominor.y
2b25c 79 38 34 2e 70 4c 69 6d 69 74 20 3d 20 79 79 6d  y84.pLimit = yym
2b25d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35  sp[0].minor.yy45
2b25e 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  0;}.        brea
2b25f 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 36  k;.      case 16
2b260 30 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45  0: /* cmd ::= DE
2b261 4c 45 54 45 20 46 52 4f 4d 20 66 75 6c 6c 6e 61  LETE FROM fullna
2b262 6d 65 20 69 6e 64 65 78 65 64 5f 6f 70 74 20 77  me indexed_opt w
2b263 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a 20 20  here_opt */.{.  
2b264 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74 49 6e  sqlite3SrcListIn
2b265 64 65 78 65 64 42 79 28 70 50 61 72 73 65 2c 20  dexedBy(pParse, 
2b266 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2b267 79 79 34 31 39 2c 20 26 79 79 6d 73 70 5b 2d 31  yy419, &yymsp[-1
2b268 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2b269 73 71 6c 69 74 65 33 44 65 6c 65 74 65 46 72 6f  sqlite3DeleteFro
2b26a 6d 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d  m(pParse,yymsp[-
2b26b 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 79  2].minor.yy419,y
2b26c 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b26d 34 35 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  450);.}.        
2b26e 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b26f 65 20 31 36 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 163: /* cmd ::
2b270 3d 20 55 50 44 41 54 45 20 6f 72 63 6f 6e 66 20  = UPDATE orconf 
2b271 66 75 6c 6c 6e 61 6d 65 20 69 6e 64 65 78 65 64  fullname indexed
2b272 5f 6f 70 74 20 53 45 54 20 73 65 74 6c 69 73 74  _opt SET setlist
2b273 20 77 68 65 72 65 5f 6f 70 74 20 2a 2f 0a 7b 0a   where_opt */.{.
2b274 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73 74    sqlite3SrcList
2b275 49 6e 64 65 78 65 64 42 79 28 70 50 61 72 73 65  IndexedBy(pParse
2b276 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
2b277 72 2e 79 79 34 31 39 2c 20 26 79 79 6d 73 70 5b  r.yy419, &yymsp[
2b278 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -3].minor.yy0);.
2b279 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73    sqlite3ExprLis
2b27a 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70 50 61  tCheckLength(pPa
2b27b 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  rse,yymsp[-1].mi
2b27c 6e 6f 72 2e 79 79 32 34 32 2c 22 73 65 74 20 6c  nor.yy242,"set l
2b27d 69 73 74 22 29 3b 20 0a 20 20 73 71 6c 69 74 65  ist"); .  sqlite
2b27e 33 55 70 64 61 74 65 28 70 50 61 72 73 65 2c 79  3Update(pParse,y
2b27f 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-4].minor.y
2b280 79 34 31 39 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  y419,yymsp[-1].m
2b281 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70  inor.yy242,yymsp
2b282 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  [0].minor.yy450,
2b283 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72 2e  yymsp[-5].minor.
2b284 79 79 33 31 36 29 3b 0a 7d 0a 20 20 20 20 20 20  yy316);.}.      
2b285 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b286 61 73 65 20 31 36 34 3a 20 2f 2a 20 73 65 74 6c  ase 164: /* setl
2b287 69 73 74 20 3a 3a 3d 20 73 65 74 6c 69 73 74 20  ist ::= setlist 
2b288 43 4f 4d 4d 41 20 6e 6d 20 45 51 20 65 78 70 72  COMMA nm EQ expr
2b289 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2b28a 2e 79 79 32 34 32 20 3d 20 73 71 6c 69 74 65 33  .yy242 = sqlite3
2b28b 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2b28c 50 61 72 73 65 2c 79 79 6d 73 70 5b 2d 34 5d 2e  Parse,yymsp[-4].
2b28d 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73  minor.yy242,yyms
2b28e 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  p[0].minor.yy450
2b28f 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  ,&yymsp[-2].mino
2b290 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20  r.yy0);}.       
2b291 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b292 73 65 20 31 36 35 3a 20 2f 2a 20 73 65 74 6c 69  se 165: /* setli
2b293 73 74 20 3a 3a 3d 20 6e 6d 20 45 51 20 65 78 70  st ::= nm EQ exp
2b294 72 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  r */.{yygotomino
2b295 72 2e 79 79 32 34 32 20 3d 20 73 71 6c 69 74 65  r.yy242 = sqlite
2b296 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2b297 70 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30  pParse,0,yymsp[0
2b298 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79  ].minor.yy450,&y
2b299 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2b29a 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  y0);}.        br
2b29b 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2b29c 31 36 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  166: /* cmd ::= 
2b29d 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54 4f 20  insert_cmd INTO 
2b29e 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63 6f 6c 6c  fullname inscoll
2b29f 69 73 74 5f 6f 70 74 20 56 41 4c 55 45 53 20 4c  ist_opt VALUES L
2b2a0 50 20 69 74 65 6d 6c 69 73 74 20 52 50 20 2a 2f  P itemlist RP */
2b2a1 0a 7b 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28  .{sqlite3Insert(
2b2a2 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 35  pParse, yymsp[-5
2b2a3 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 20 79  ].minor.yy419, y
2b2a4 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2b2a5 79 32 34 32 2c 20 30 2c 20 79 79 6d 73 70 5b 2d  y242, 0, yymsp[-
2b2a6 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32 2c 20  4].minor.yy352, 
2b2a7 79 79 6d 73 70 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e  yymsp[-7].minor.
2b2a8 79 79 33 31 36 29 3b 7d 0a 20 20 20 20 20 20 20  yy316);}.       
2b2a9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b2aa 73 65 20 31 36 37 3a 20 2f 2a 20 63 6d 64 20 3a  se 167: /* cmd :
2b2ab 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e  := insert_cmd IN
2b2ac 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e 73 63  TO fullname insc
2b2ad 6f 6c 6c 69 73 74 5f 6f 70 74 20 73 65 6c 65 63  ollist_opt selec
2b2ae 74 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 49 6e 73  t */.{sqlite3Ins
2b2af 65 72 74 28 70 50 61 72 73 65 2c 20 79 79 6d 73  ert(pParse, yyms
2b2b0 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31  p[-2].minor.yy41
2b2b1 39 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  9, 0, yymsp[0].m
2b2b2 69 6e 6f 72 2e 79 79 34 33 2c 20 79 79 6d 73 70  inor.yy43, yymsp
2b2b3 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32  [-1].minor.yy352
2b2b4 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
2b2b5 72 2e 79 79 33 31 36 29 3b 7d 0a 20 20 20 20 20  r.yy316);}.     
2b2b6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b2b7 63 61 73 65 20 31 36 38 3a 20 2f 2a 20 63 6d 64  case 168: /* cmd
2b2b8 20 3a 3a 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20   ::= insert_cmd 
2b2b9 49 4e 54 4f 20 66 75 6c 6c 6e 61 6d 65 20 69 6e  INTO fullname in
2b2ba 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 44 45 46  scollist_opt DEF
2b2bb 41 55 4c 54 20 56 41 4c 55 45 53 20 2a 2f 0a 7b  AULT VALUES */.{
2b2bc 73 71 6c 69 74 65 33 49 6e 73 65 72 74 28 70 50  sqlite3Insert(pP
2b2bd 61 72 73 65 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e  arse, yymsp[-3].
2b2be 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 20 30 2c 20  minor.yy419, 0, 
2b2bf 30 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  0, yymsp[-2].min
2b2c0 6f 72 2e 79 79 33 35 32 2c 20 79 79 6d 73 70 5b  or.yy352, yymsp[
2b2c1 2d 35 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29  -5].minor.yy316)
2b2c2 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2b2c3 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 31  ;.      case 171
2b2c4 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 3a 3a  : /* itemlist ::
2b2c5 3d 20 69 74 65 6d 6c 69 73 74 20 43 4f 4d 4d 41  = itemlist COMMA
2b2c6 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63   expr */.      c
2b2c7 61 73 65 20 32 33 35 3a 20 2f 2a 20 6e 65 78 70  ase 235: /* nexp
2b2c8 72 6c 69 73 74 20 3a 3a 3d 20 6e 65 78 70 72 6c  rlist ::= nexprl
2b2c9 69 73 74 20 43 4f 4d 4d 41 20 65 78 70 72 20 2a  ist COMMA expr *
2b2ca 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2b2cb 79 32 34 32 20 3d 20 73 71 6c 69 74 65 33 45 78  y242 = sqlite3Ex
2b2cc 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61  prListAppend(pPa
2b2cd 72 73 65 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  rse,yymsp[-2].mi
2b2ce 6e 6f 72 2e 79 79 32 34 32 2c 79 79 6d 73 70 5b  nor.yy242,yymsp[
2b2cf 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 30  0].minor.yy450,0
2b2d0 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2b2d1 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37  k;.      case 17
2b2d2 32 3a 20 2f 2a 20 69 74 65 6d 6c 69 73 74 20 3a  2: /* itemlist :
2b2d3 3a 3d 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  := expr */.     
2b2d4 20 63 61 73 65 20 32 33 36 3a 20 2f 2a 20 6e 65   case 236: /* ne
2b2d5 78 70 72 6c 69 73 74 20 3a 3a 3d 20 65 78 70 72  xprlist ::= expr
2b2d6 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2b2d7 2e 79 79 32 34 32 20 3d 20 73 71 6c 69 74 65 33  .yy242 = sqlite3
2b2d8 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70  ExprListAppend(p
2b2d9 50 61 72 73 65 2c 30 2c 79 79 6d 73 70 5b 30 5d  Parse,0,yymsp[0]
2b2da 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 30 29 3b  .minor.yy450,0);
2b2db 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2b2dc 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 35 3a  .      case 175:
2b2dd 20 2f 2a 20 69 6e 73 63 6f 6c 6c 69 73 74 20 3a   /* inscollist :
2b2de 3a 3d 20 69 6e 73 63 6f 6c 6c 69 73 74 20 43 4f  := inscollist CO
2b2df 4d 4d 41 20 6e 6d 20 2a 2f 0a 7b 79 79 67 6f 74  MMA nm */.{yygot
2b2e0 6f 6d 69 6e 6f 72 2e 79 79 33 35 32 20 3d 20 73  ominor.yy352 = s
2b2e1 71 6c 69 74 65 33 49 64 4c 69 73 74 41 70 70 65  qlite3IdListAppe
2b2e2 6e 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 79 79  nd(pParse->db,yy
2b2e3 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b2e4 33 35 32 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  352,&yymsp[0].mi
2b2e5 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
2b2e6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b2e7 63 61 73 65 20 31 37 36 3a 20 2f 2a 20 69 6e 73  case 176: /* ins
2b2e8 63 6f 6c 6c 69 73 74 20 3a 3a 3d 20 6e 6d 20 2a  collist ::= nm *
2b2e9 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  /.{yygotominor.y
2b2ea 79 33 35 32 20 3d 20 73 71 6c 69 74 65 33 49 64  y352 = sqlite3Id
2b2eb 4c 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73  ListAppend(pPars
2b2ec 65 2d 3e 64 62 2c 30 2c 26 79 79 6d 73 70 5b 30  e->db,0,&yymsp[0
2b2ed 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20  ].minor.yy0);}. 
2b2ee 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b2ef 20 20 20 20 63 61 73 65 20 31 37 38 3a 20 2f 2a      case 178: /*
2b2f0 20 65 78 70 72 20 3a 3a 3d 20 4c 50 20 65 78 70   expr ::= LP exp
2b2f1 72 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  r RP */.{yygotom
2b2f2 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 79 79 6d  inor.yy450 = yym
2b2f3 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-1].minor.yy4
2b2f4 35 30 3b 20 73 71 6c 69 74 65 33 45 78 70 72 53  50; sqlite3ExprS
2b2f5 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
2b2f6 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 32 5d  yy450,&yymsp[-2]
2b2f7 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2b2f8 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b  p[0].minor.yy0);
2b2f9 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2b2fa 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 37 39  ;.      case 179
2b2fb 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 4e 55  : /* term ::= NU
2b2fc 4c 4c 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  LL */.      case
2b2fd 20 31 38 34 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a   184: /* term ::
2b2fe 3d 20 49 4e 54 45 47 45 52 7c 46 4c 4f 41 54 7c  = INTEGER|FLOAT|
2b2ff 42 4c 4f 42 20 2a 2f 0a 20 20 20 20 20 20 63 61  BLOB */.      ca
2b300 73 65 20 31 38 35 3a 20 2f 2a 20 74 65 72 6d 20  se 185: /* term 
2b301 3a 3a 3d 20 53 54 52 49 4e 47 20 2a 2f 0a 7b 79  ::= STRING */.{y
2b302 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b303 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2b304 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
2b305 2e 6d 61 6a 6f 72 2c 20 30 2c 20 30 2c 20 26 79  .major, 0, 0, &y
2b306 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b307 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2b308 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2b309 38 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  80: /* expr ::= 
2b30a 49 44 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  ID */.      case
2b30b 20 31 38 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   181: /* expr ::
2b30c 3d 20 4a 4f 49 4e 5f 4b 57 20 2a 2f 0a 7b 79 79  = JOIN_KW */.{yy
2b30d 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20  gotominor.yy450 
2b30e 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2b30f 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c  Parse, TK_ID, 0,
2b310 20 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69   0, &yymsp[0].mi
2b311 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20  nor.yy0);}.     
2b312 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b313 63 61 73 65 20 31 38 32 3a 20 2f 2a 20 65 78 70  case 182: /* exp
2b314 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e 6d 20  r ::= nm DOT nm 
2b315 2a 2f 0a 7b 0a 20 20 45 78 70 72 20 2a 74 65 6d  */.{.  Expr *tem
2b316 70 31 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  p1 = sqlite3PExp
2b317 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49 44 2c  r(pParse, TK_ID,
2b318 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b 2d 32   0, 0, &yymsp[-2
2b319 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20  ].minor.yy0);.  
2b31a 45 78 70 72 20 2a 74 65 6d 70 32 20 3d 20 73 71  Expr *temp2 = sq
2b31b 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2b31c 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
2b31d 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2b31e 79 79 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d 69  yy0);.  yygotomi
2b31f 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69  nor.yy450 = sqli
2b320 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2b321 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c 20   TK_DOT, temp1, 
2b322 74 65 6d 70 32 2c 20 30 29 3b 0a 7d 0a 20 20 20  temp2, 0);.}.   
2b323 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b324 20 20 63 61 73 65 20 31 38 33 3a 20 2f 2a 20 65    case 183: /* e
2b325 78 70 72 20 3a 3a 3d 20 6e 6d 20 44 4f 54 20 6e  xpr ::= nm DOT n
2b326 6d 20 44 4f 54 20 6e 6d 20 2a 2f 0a 7b 0a 20 20  m DOT nm */.{.  
2b327 45 78 70 72 20 2a 74 65 6d 70 31 20 3d 20 73 71  Expr *temp1 = sq
2b328 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2b329 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30 2c 20  e, TK_ID, 0, 0, 
2b32a 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
2b32b 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a 74  .yy0);.  Expr *t
2b32c 65 6d 70 32 20 3d 20 73 71 6c 69 74 65 33 50 45  emp2 = sqlite3PE
2b32d 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
2b32e 44 2c 20 30 2c 20 30 2c 20 26 79 79 6d 73 70 5b  D, 0, 0, &yymsp[
2b32f 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  -2].minor.yy0);.
2b330 20 20 45 78 70 72 20 2a 74 65 6d 70 33 20 3d 20    Expr *temp3 = 
2b331 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2b332 72 73 65 2c 20 54 4b 5f 49 44 2c 20 30 2c 20 30  rse, TK_ID, 0, 0
2b333 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  , &yymsp[0].mino
2b334 72 2e 79 79 30 29 3b 0a 20 20 45 78 70 72 20 2a  r.yy0);.  Expr *
2b335 74 65 6d 70 34 20 3d 20 73 71 6c 69 74 65 33 50  temp4 = sqlite3P
2b336 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f  Expr(pParse, TK_
2b337 44 4f 54 2c 20 74 65 6d 70 32 2c 20 74 65 6d 70  DOT, temp2, temp
2b338 33 2c 20 30 29 3b 0a 20 20 79 79 67 6f 74 6f 6d  3, 0);.  yygotom
2b339 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2b33a 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2b33b 2c 20 54 4b 5f 44 4f 54 2c 20 74 65 6d 70 31 2c  , TK_DOT, temp1,
2b33c 20 74 65 6d 70 34 2c 20 30 29 3b 0a 7d 0a 20 20   temp4, 0);.}.  
2b33d 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b33e 20 20 20 63 61 73 65 20 31 38 36 3a 20 2f 2a 20     case 186: /* 
2b33f 65 78 70 72 20 3a 3a 3d 20 52 45 47 49 53 54 45  expr ::= REGISTE
2b340 52 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  R */.{yygotomino
2b341 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65  r.yy450 = sqlite
2b342 33 52 65 67 69 73 74 65 72 45 78 70 72 28 70 50  3RegisterExpr(pP
2b343 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 30 5d 2e  arse, &yymsp[0].
2b344 6d 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20  minor.yy0);}.   
2b345 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b346 20 20 63 61 73 65 20 31 38 37 3a 20 2f 2a 20 65    case 187: /* e
2b347 78 70 72 20 3a 3a 3d 20 56 41 52 49 41 42 4c 45  xpr ::= VARIABLE
2b348 20 2a 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 2a 70   */.{.  Token *p
2b349 54 6f 6b 65 6e 20 3d 20 26 79 79 6d 73 70 5b 30  Token = &yymsp[0
2b34a 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 45  ].minor.yy0;.  E
2b34b 78 70 72 20 2a 70 45 78 70 72 20 3d 20 79 79 67  xpr *pExpr = yyg
2b34c 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2b34d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2b34e 61 72 73 65 2c 20 54 4b 5f 56 41 52 49 41 42 4c  arse, TK_VARIABL
2b34f 45 2c 20 30 2c 20 30 2c 20 70 54 6f 6b 65 6e 29  E, 0, 0, pToken)
2b350 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 41  ;.  sqlite3ExprA
2b351 73 73 69 67 6e 56 61 72 4e 75 6d 62 65 72 28 70  ssignVarNumber(p
2b352 50 61 72 73 65 2c 20 70 45 78 70 72 29 3b 0a 7d  Parse, pExpr);.}
2b353 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b354 20 20 20 20 20 20 63 61 73 65 20 31 38 38 3a 20        case 188: 
2b355 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
2b356 20 43 4f 4c 4c 41 54 45 20 69 64 73 20 2a 2f 0a   COLLATE ids */.
2b357 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  {.  yygotominor.
2b358 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33 45  yy450 = sqlite3E
2b359 78 70 72 53 65 74 43 6f 6c 6c 28 70 50 61 72 73  xprSetColl(pPars
2b35a 65 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  e, yymsp[-2].min
2b35b 6f 72 2e 79 79 34 35 30 2c 20 26 79 79 6d 73 70  or.yy450, &yymsp
2b35c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2b35d 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2b35e 0a 20 20 20 20 20 20 63 61 73 65 20 31 38 39 3a  .      case 189:
2b35f 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 43 41 53   /* expr ::= CAS
2b360 54 20 4c 50 20 65 78 70 72 20 41 53 20 74 79 70  T LP expr AS typ
2b361 65 74 6f 6b 65 6e 20 52 50 20 2a 2f 0a 7b 0a 20  etoken RP */.{. 
2b362 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2b363 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  50 = sqlite3PExp
2b364 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43 41 53  r(pParse, TK_CAS
2b365 54 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  T, yymsp[-3].min
2b366 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 26 79 79  or.yy450, 0, &yy
2b367 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2b368 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70  0);.  sqlite3Exp
2b369 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2b36a 72 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d  r.yy450,&yymsp[-
2b36b 35 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  5].minor.yy0,&yy
2b36c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2b36d 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2b36e 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31  ak;.      case 1
2b36f 39 30 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  90: /* expr ::= 
2b370 49 44 20 4c 50 20 64 69 73 74 69 6e 63 74 20 65  ID LP distinct e
2b371 78 70 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a  xprlist RP */.{.
2b372 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e    if( yymsp[-1].
2b373 6d 69 6e 6f 72 2e 79 79 32 34 32 20 26 26 20 79  minor.yy242 && y
2b374 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2b375 79 32 34 32 2d 3e 6e 45 78 70 72 3e 53 51 4c 49  y242->nExpr>SQLI
2b376 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
2b377 41 52 47 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ARG ){.    sqlit
2b378 65 33 45 72 72 6f 72 4d 73 67 28 70 50 61 72 73  e3ErrorMsg(pPars
2b379 65 2c 20 22 74 6f 6f 20 6d 61 6e 79 20 61 72 67  e, "too many arg
2b37a 75 6d 65 6e 74 73 20 6f 6e 20 66 75 6e 63 74 69  uments on functi
2b37b 6f 6e 20 25 54 22 2c 20 26 79 79 6d 73 70 5b 2d  on %T", &yymsp[-
2b37c 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  4].minor.yy0);. 
2b37d 20 7d 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72   }.  yygotominor
2b37e 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2b37f 45 78 70 72 46 75 6e 63 74 69 6f 6e 28 70 50 61  ExprFunction(pPa
2b380 72 73 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  rse, yymsp[-1].m
2b381 69 6e 6f 72 2e 79 79 32 34 32 2c 20 26 79 79 6d  inor.yy242, &yym
2b382 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-4].minor.yy0
2b383 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2b384 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2b385 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 34  .yy450,&yymsp[-4
2b386 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2b387 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2b388 3b 0a 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 32  ;.  if( yymsp[-2
2b389 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 20 26 26  ].minor.yy316 &&
2b38a 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2b38b 35 30 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f  50 ){.    yygoto
2b38c 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 66 6c 61  minor.yy450->fla
2b38d 67 73 20 7c 3d 20 45 50 5f 44 69 73 74 69 6e 63  gs |= EP_Distinc
2b38e 74 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  t;.  }.}.       
2b38f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b390 73 65 20 31 39 31 3a 20 2f 2a 20 65 78 70 72 20  se 191: /* expr 
2b391 3a 3a 3d 20 49 44 20 4c 50 20 53 54 41 52 20 52  ::= ID LP STAR R
2b392 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  P */.{.  yygotom
2b393 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2b394 69 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e  ite3ExprFunction
2b395 28 70 50 61 72 73 65 2c 20 30 2c 20 26 79 79 6d  (pParse, 0, &yym
2b396 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-3].minor.yy0
2b397 29 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72  );.  sqlite3Expr
2b398 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2b399 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 33  .yy450,&yymsp[-3
2b39a 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d  ].minor.yy0,&yym
2b39b 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2b39c 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2b39d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 31 39  k;.      case 19
2b39e 32 3a 20 2f 2a 20 74 65 72 6d 20 3a 3a 3d 20 43  2: /* term ::= C
2b39f 54 49 4d 45 5f 4b 57 20 2a 2f 0a 7b 0a 20 20 2f  TIME_KW */.{.  /
2b3a0 2a 20 54 68 65 20 43 55 52 52 45 4e 54 5f 54 49  * The CURRENT_TI
2b3a1 4d 45 2c 20 43 55 52 52 45 4e 54 5f 44 41 54 45  ME, CURRENT_DATE
2b3a2 2c 20 61 6e 64 20 43 55 52 52 45 4e 54 5f 54 49  , and CURRENT_TI
2b3a3 4d 45 53 54 41 4d 50 20 76 61 6c 75 65 73 20 61  MESTAMP values a
2b3a4 72 65 0a 20 20 2a 2a 20 74 72 65 61 74 65 64 20  re.  ** treated 
2b3a5 61 73 20 66 75 6e 63 74 69 6f 6e 73 20 74 68 61  as functions tha
2b3a6 74 20 72 65 74 75 72 6e 20 63 6f 6e 73 74 61 6e  t return constan
2b3a7 74 73 20 2a 2f 0a 20 20 79 79 67 6f 74 6f 6d 69  ts */.  yygotomi
2b3a8 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69  nor.yy450 = sqli
2b3a9 74 65 33 45 78 70 72 46 75 6e 63 74 69 6f 6e 28  te3ExprFunction(
2b3aa 70 50 61 72 73 65 2c 20 30 2c 26 79 79 6d 73 70  pParse, 0,&yymsp
2b3ab 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2b3ac 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2b3ad 72 2e 79 79 34 35 30 20 29 7b 0a 20 20 20 20 79  r.yy450 ){.    y
2b3ae 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b3af 2d 3e 6f 70 20 3d 20 54 4b 5f 43 4f 4e 53 54 5f  ->op = TK_CONST_
2b3b0 46 55 4e 43 3b 20 20 0a 20 20 20 20 79 79 67 6f  FUNC;  .    yygo
2b3b1 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73  tominor.yy450->s
2b3b2 70 61 6e 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  pan = yymsp[0].m
2b3b3 69 6e 6f 72 2e 79 79 30 3b 0a 20 20 7d 0a 7d 0a  inor.yy0;.  }.}.
2b3b4 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b3b5 20 20 20 20 20 63 61 73 65 20 31 39 33 3a 20 2f       case 193: /
2b3b6 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20  * expr ::= expr 
2b3b7 41 4e 44 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  AND expr */.    
2b3b8 20 20 63 61 73 65 20 31 39 34 3a 20 2f 2a 20 65    case 194: /* e
2b3b9 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 4f 52 20  xpr ::= expr OR 
2b3ba 65 78 70 72 20 2a 2f 0a 20 20 20 20 20 20 63 61  expr */.      ca
2b3bb 73 65 20 31 39 35 3a 20 2f 2a 20 65 78 70 72 20  se 195: /* expr 
2b3bc 3a 3a 3d 20 65 78 70 72 20 4c 54 7c 47 54 7c 47  ::= expr LT|GT|G
2b3bd 45 7c 4c 45 20 65 78 70 72 20 2a 2f 0a 20 20 20  E|LE expr */.   
2b3be 20 20 20 63 61 73 65 20 31 39 36 3a 20 2f 2a 20     case 196: /* 
2b3bf 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 45 51  expr ::= expr EQ
2b3c0 7c 4e 45 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  |NE expr */.    
2b3c1 20 20 63 61 73 65 20 31 39 37 3a 20 2f 2a 20 65    case 197: /* e
2b3c2 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 42 49 54  xpr ::= expr BIT
2b3c3 41 4e 44 7c 42 49 54 4f 52 7c 4c 53 48 49 46 54  AND|BITOR|LSHIFT
2b3c4 7c 52 53 48 49 46 54 20 65 78 70 72 20 2a 2f 0a  |RSHIFT expr */.
2b3c5 20 20 20 20 20 20 63 61 73 65 20 31 39 38 3a 20        case 198: 
2b3c6 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72  /* expr ::= expr
2b3c7 20 50 4c 55 53 7c 4d 49 4e 55 53 20 65 78 70 72   PLUS|MINUS expr
2b3c8 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 31   */.      case 1
2b3c9 39 39 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  99: /* expr ::= 
2b3ca 65 78 70 72 20 53 54 41 52 7c 53 4c 41 53 48 7c  expr STAR|SLASH|
2b3cb 52 45 4d 20 65 78 70 72 20 2a 2f 0a 20 20 20 20  REM expr */.    
2b3cc 20 20 63 61 73 65 20 32 30 30 3a 20 2f 2a 20 65    case 200: /* e
2b3cd 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 43 4f 4e  xpr ::= expr CON
2b3ce 43 41 54 20 65 78 70 72 20 2a 2f 0a 7b 79 79 67  CAT expr */.{yyg
2b3cf 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2b3d0 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2b3d1 61 72 73 65 2c 79 79 6d 73 70 5b 2d 31 5d 2e 6d  arse,yymsp[-1].m
2b3d2 61 6a 6f 72 2c 79 79 6d 73 70 5b 2d 32 5d 2e 6d  ajor,yymsp[-2].m
2b3d3 69 6e 6f 72 2e 79 79 34 35 30 2c 79 79 6d 73 70  inor.yy450,yymsp
2b3d4 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  [0].minor.yy450,
2b3d5 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2b3d6 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b3d7 30 31 3a 20 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a  01: /* likeop ::
2b3d8 3d 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20  = LIKE_KW */.   
2b3d9 20 20 20 63 61 73 65 20 32 30 33 3a 20 2f 2a 20     case 203: /* 
2b3da 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4d 41 54 43 48  likeop ::= MATCH
2b3db 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2b3dc 2e 79 79 38 36 2e 65 4f 70 65 72 61 74 6f 72 20  .yy86.eOperator 
2b3dd 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
2b3de 2e 79 79 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f  .yy0; yygotomino
2b3df 72 2e 79 79 38 36 2e 6e 6f 74 20 3d 20 30 3b 7d  r.yy86.not = 0;}
2b3e0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b3e1 20 20 20 20 20 20 63 61 73 65 20 32 30 32 3a 20        case 202: 
2b3e2 2f 2a 20 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f  /* likeop ::= NO
2b3e3 54 20 4c 49 4b 45 5f 4b 57 20 2a 2f 0a 20 20 20  T LIKE_KW */.   
2b3e4 20 20 20 63 61 73 65 20 32 30 34 3a 20 2f 2a 20     case 204: /* 
2b3e5 6c 69 6b 65 6f 70 20 3a 3a 3d 20 4e 4f 54 20 4d  likeop ::= NOT M
2b3e6 41 54 43 48 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d  ATCH */.{yygotom
2b3e7 69 6e 6f 72 2e 79 79 38 36 2e 65 4f 70 65 72 61  inor.yy86.eOpera
2b3e8 74 6f 72 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d  tor = yymsp[0].m
2b3e9 69 6e 6f 72 2e 79 79 30 3b 20 79 79 67 6f 74 6f  inor.yy0; yygoto
2b3ea 6d 69 6e 6f 72 2e 79 79 38 36 2e 6e 6f 74 20 3d  minor.yy86.not =
2b3eb 20 31 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   1;}.        bre
2b3ec 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b3ed 30 37 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  07: /* expr ::= 
2b3ee 65 78 70 72 20 6c 69 6b 65 6f 70 20 65 78 70 72  expr likeop expr
2b3ef 20 65 73 63 61 70 65 20 2a 2f 0a 7b 0a 20 20 45   escape */.{.  E
2b3f0 78 70 72 4c 69 73 74 20 2a 70 4c 69 73 74 3b 0a  xprList *pList;.
2b3f1 20 20 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65    pList = sqlite
2b3f2 33 45 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28  3ExprListAppend(
2b3f3 70 50 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b  pParse,0, yymsp[
2b3f4 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  -1].minor.yy450,
2b3f5 20 30 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73   0);.  pList = s
2b3f6 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70  qlite3ExprListAp
2b3f7 70 65 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73  pend(pParse,pLis
2b3f8 74 2c 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  t, yymsp[-3].min
2b3f9 6f 72 2e 79 79 34 35 30 2c 20 30 29 3b 0a 20 20  or.yy450, 0);.  
2b3fa 69 66 28 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  if( yymsp[0].min
2b3fb 6f 72 2e 79 79 34 35 30 20 29 7b 0a 20 20 20 20  or.yy450 ){.    
2b3fc 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
2b3fd 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2b3fe 61 72 73 65 2c 70 4c 69 73 74 2c 20 79 79 6d 73  arse,pList, yyms
2b3ff 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  p[0].minor.yy450
2b400 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  , 0);.  }.  yygo
2b401 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20  tominor.yy450 = 
2b402 73 71 6c 69 74 65 33 45 78 70 72 46 75 6e 63 74  sqlite3ExprFunct
2b403 69 6f 6e 28 70 50 61 72 73 65 2c 20 70 4c 69 73  ion(pParse, pLis
2b404 74 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  t, &yymsp[-2].mi
2b405 6e 6f 72 2e 79 79 38 36 2e 65 4f 70 65 72 61 74  nor.yy86.eOperat
2b406 6f 72 29 3b 0a 20 20 69 66 28 20 79 79 6d 73 70  or);.  if( yymsp
2b407 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 38 36 2e  [-2].minor.yy86.
2b408 6e 6f 74 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  not ) yygotomino
2b409 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65  r.yy450 = sqlite
2b40a 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54  3PExpr(pParse, T
2b40b 4b 5f 4e 4f 54 2c 20 79 79 67 6f 74 6f 6d 69 6e  K_NOT, yygotomin
2b40c 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29 3b  or.yy450, 0, 0);
2b40d 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70  .  sqlite3ExprSp
2b40e 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2b40f 79 34 35 30 2c 20 26 79 79 6d 73 70 5b 2d 33 5d  y450, &yymsp[-3]
2b410 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70  .minor.yy450->sp
2b411 61 6e 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d  an, &yymsp[-1].m
2b412 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e  inor.yy450->span
2b413 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  );.  if( yygotom
2b414 69 6e 6f 72 2e 79 79 34 35 30 20 29 20 79 79 67  inor.yy450 ) yyg
2b415 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e  otominor.yy450->
2b416 66 6c 61 67 73 20 7c 3d 20 45 50 5f 49 6e 66 69  flags |= EP_Infi
2b417 78 46 75 6e 63 3b 0a 7d 0a 20 20 20 20 20 20 20  xFunc;.}.       
2b418 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b419 73 65 20 32 30 38 3a 20 2f 2a 20 65 78 70 72 20  se 208: /* expr 
2b41a 3a 3a 3d 20 65 78 70 72 20 49 53 4e 55 4c 4c 7c  ::= expr ISNULL|
2b41b 4e 4f 54 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79  NOTNULL */.{.  y
2b41c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b41d 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2b41e 70 50 61 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d  pParse, yymsp[0]
2b41f 2e 6d 61 6a 6f 72 2c 20 79 79 6d 73 70 5b 2d 31  .major, yymsp[-1
2b420 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30  ].minor.yy450, 0
2b421 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 0);.  sqlite3E
2b422 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2b423 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d 73 70  nor.yy450,&yymsp
2b424 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-1].minor.yy450
2b425 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d  ->span,&yymsp[0]
2b426 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20  .minor.yy0);.}. 
2b427 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b428 20 20 20 20 63 61 73 65 20 32 30 39 3a 20 2f 2a      case 209: /*
2b429 20 65 78 70 72 20 3a 3a 3d 20 65 78 70 72 20 49   expr ::= expr I
2b42a 53 20 4e 55 4c 4c 20 2a 2f 0a 7b 0a 20 20 79 79  S NULL */.{.  yy
2b42b 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20  gotominor.yy450 
2b42c 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70  = sqlite3PExpr(p
2b42d 50 61 72 73 65 2c 20 54 4b 5f 49 53 4e 55 4c 4c  Parse, TK_ISNULL
2b42e 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2b42f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a  r.yy450, 0, 0);.
2b430 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70 61    sqlite3ExprSpa
2b431 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  n(yygotominor.yy
2b432 34 35 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d  450,&yymsp[-2].m
2b433 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e  inor.yy450->span
2b434 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2b435 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20  .yy0);.}.       
2b436 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b437 73 65 20 32 31 30 3a 20 2f 2a 20 65 78 70 72 20  se 210: /* expr 
2b438 3a 3a 3d 20 65 78 70 72 20 4e 4f 54 20 4e 55 4c  ::= expr NOT NUL
2b439 4c 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d  L */.{.  yygotom
2b43a 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2b43b 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2b43c 2c 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79  , TK_NOTNULL, yy
2b43d 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b43e 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71  450, 0, 0);.  sq
2b43f 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79  lite3ExprSpan(yy
2b440 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  gotominor.yy450,
2b441 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72  &yymsp[-2].minor
2b442 2e 79 79 34 35 30 2d 3e 73 70 61 6e 2c 26 79 79  .yy450->span,&yy
2b443 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2b444 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65  );.}.        bre
2b445 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b446 31 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  11: /* expr ::= 
2b447 65 78 70 72 20 49 53 20 4e 4f 54 20 4e 55 4c 4c  expr IS NOT NULL
2b448 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69   */.{.  yygotomi
2b449 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69  nor.yy450 = sqli
2b44a 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c  te3PExpr(pParse,
2b44b 20 54 4b 5f 4e 4f 54 4e 55 4c 4c 2c 20 79 79 6d   TK_NOTNULL, yym
2b44c 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-3].minor.yy4
2b44d 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c  50, 0, 0);.  sql
2b44e 69 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67  ite3ExprSpan(yyg
2b44f 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26  otominor.yy450,&
2b450 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2b451 79 79 34 35 30 2d 3e 73 70 61 6e 2c 26 79 79 6d  yy450->span,&yym
2b452 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  sp[0].minor.yy0)
2b453 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  ;.}.        brea
2b454 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 31  k;.      case 21
2b455 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 4e  2: /* expr ::= N
2b456 4f 54 20 65 78 70 72 20 2a 2f 0a 20 20 20 20 20  OT expr */.     
2b457 20 63 61 73 65 20 32 31 33 3a 20 2f 2a 20 65 78   case 213: /* ex
2b458 70 72 20 3a 3a 3d 20 42 49 54 4e 4f 54 20 65 78  pr ::= BITNOT ex
2b459 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
2b45a 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71  minor.yy450 = sq
2b45b 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73  lite3PExpr(pPars
2b45c 65 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 61 6a  e, yymsp[-1].maj
2b45d 6f 72 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  or, yymsp[0].min
2b45e 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29 3b  or.yy450, 0, 0);
2b45f 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 70  .  sqlite3ExprSp
2b460 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  an(yygotominor.y
2b461 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e  y450,&yymsp[-1].
2b462 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2b463 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2d  [0].minor.yy450-
2b464 3e 73 70 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20  >span);.}.      
2b465 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b466 61 73 65 20 32 31 34 3a 20 2f 2a 20 65 78 70 72  ase 214: /* expr
2b467 20 3a 3a 3d 20 4d 49 4e 55 53 20 65 78 70 72 20   ::= MINUS expr 
2b468 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e  */.{.  yygotomin
2b469 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74  or.yy450 = sqlit
2b46a 65 33 50 45 78 70 72 28 70 50 61 72 73 65 2c 20  e3PExpr(pParse, 
2b46b 54 4b 5f 55 4d 49 4e 55 53 2c 20 79 79 6d 73 70  TK_UMINUS, yymsp
2b46c 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c  [0].minor.yy450,
2b46d 20 30 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65   0, 0);.  sqlite
2b46e 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f  3ExprSpan(yygoto
2b46f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d  minor.yy450,&yym
2b470 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2b471 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2b472 2e 79 79 34 35 30 2d 3e 73 70 61 6e 29 3b 0a 7d  .yy450->span);.}
2b473 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b474 20 20 20 20 20 20 63 61 73 65 20 32 31 35 3a 20        case 215: 
2b475 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 50 4c 55 53  /* expr ::= PLUS
2b476 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67   expr */.{.  yyg
2b477 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2b478 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2b479 61 72 73 65 2c 20 54 4b 5f 55 50 4c 55 53 2c 20  arse, TK_UPLUS, 
2b47a 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b47b 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 73  y450, 0, 0);.  s
2b47c 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2b47d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b47e 2c 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  ,&yymsp[-1].mino
2b47f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
2b480 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61  minor.yy450->spa
2b481 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  n);.}.        br
2b482 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2b483 32 31 38 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d  218: /* expr ::=
2b484 20 65 78 70 72 20 62 65 74 77 65 65 6e 5f 6f 70   expr between_op
2b485 20 65 78 70 72 20 41 4e 44 20 65 78 70 72 20 2a   expr AND expr *
2b486 2f 0a 7b 0a 20 20 45 78 70 72 4c 69 73 74 20 2a  /.{.  ExprList *
2b487 70 4c 69 73 74 20 3d 20 73 71 6c 69 74 65 33 45  pList = sqlite3E
2b488 78 70 72 4c 69 73 74 41 70 70 65 6e 64 28 70 50  xprListAppend(pP
2b489 61 72 73 65 2c 30 2c 20 79 79 6d 73 70 5b 2d 32  arse,0, yymsp[-2
2b48a 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30  ].minor.yy450, 0
2b48b 29 3b 0a 20 20 70 4c 69 73 74 20 3d 20 73 71 6c  );.  pList = sql
2b48c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70 65  ite3ExprListAppe
2b48d 6e 64 28 70 50 61 72 73 65 2c 70 4c 69 73 74 2c  nd(pParse,pList,
2b48e 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e   yymsp[0].minor.
2b48f 79 79 34 35 30 2c 20 30 29 3b 0a 20 20 79 79 67  yy450, 0);.  yyg
2b490 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2b491 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2b492 61 72 73 65 2c 20 54 4b 5f 42 45 54 57 45 45 4e  arse, TK_BETWEEN
2b493 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  , yymsp[-4].mino
2b494 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29 3b 0a  r.yy450, 0, 0);.
2b495 20 20 69 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f    if( yygotomino
2b496 72 2e 79 79 34 35 30 20 29 7b 0a 20 20 20 20 79  r.yy450 ){.    y
2b497 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b498 2d 3e 70 4c 69 73 74 20 3d 20 70 4c 69 73 74 3b  ->pList = pList;
2b499 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71  .  }else{.    sq
2b49a 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c  lite3ExprListDel
2b49b 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  ete(pParse->db, 
2b49c 70 4c 69 73 74 29 3b 0a 20 20 7d 20 0a 20 20 69  pList);.  } .  i
2b49d 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  f( yymsp[-3].min
2b49e 6f 72 2e 79 79 33 31 36 20 29 20 79 79 67 6f 74  or.yy316 ) yygot
2b49f 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73  ominor.yy450 = s
2b4a0 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2b4a1 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67 6f  se, TK_NOT, yygo
2b4a2 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30  tominor.yy450, 0
2b4a3 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 45  , 0);.  sqlite3E
2b4a4 78 70 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69  xprSpan(yygotomi
2b4a5 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79 6d 73 70  nor.yy450,&yymsp
2b4a6 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-4].minor.yy450
2b4a7 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70 5b 30 5d  ->span,&yymsp[0]
2b4a8 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 73 70  .minor.yy450->sp
2b4a9 61 6e 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  an);.}.        b
2b4aa 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b4ab 20 32 32 31 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   221: /* expr ::
2b4ac 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20  = expr in_op LP 
2b4ad 65 78 70 72 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  exprlist RP */.{
2b4ae 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .    yygotominor
2b4af 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2b4b0 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2b4b1 5f 49 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d  _IN, yymsp[-4].m
2b4b2 69 6e 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30  inor.yy450, 0, 0
2b4b3 29 3b 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74  );.    if( yygot
2b4b4 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 29 7b 0a  ominor.yy450 ){.
2b4b5 20 20 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f        yygotomino
2b4b6 72 2e 79 79 34 35 30 2d 3e 70 4c 69 73 74 20 3d  r.yy450->pList =
2b4b7 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2b4b8 2e 79 79 32 34 32 3b 0a 20 20 20 20 20 20 73 71  .yy242;.      sq
2b4b9 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
2b4ba 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
2b4bb 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 20  ominor.yy450);. 
2b4bc 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2b4bd 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 44  sqlite3ExprListD
2b4be 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e 64 62  elete(pParse->db
2b4bf 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2b4c0 72 2e 79 79 32 34 32 29 3b 0a 20 20 20 20 7d 0a  r.yy242);.    }.
2b4c1 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d 33      if( yymsp[-3
2b4c2 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 20 29 20  ].minor.yy316 ) 
2b4c3 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2b4c4 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72  0 = sqlite3PExpr
2b4c5 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c  (pParse, TK_NOT,
2b4c6 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2b4c7 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 73  50, 0, 0);.    s
2b4c8 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 79  qlite3ExprSpan(y
2b4c9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b4ca 2c 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f  ,&yymsp[-4].mino
2b4cb 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 2c 26 79  r.yy450->span,&y
2b4cc 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b4cd 30 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20  0);.  }.        
2b4ce 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b4cf 65 20 32 32 32 3a 20 2f 2a 20 65 78 70 72 20 3a  e 222: /* expr :
2b4d0 3a 3d 20 4c 50 20 73 65 6c 65 63 74 20 52 50 20  := LP select RP 
2b4d1 2a 2f 0a 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  */.{.    yygotom
2b4d2 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73 71 6c  inor.yy450 = sql
2b4d3 69 74 65 33 50 45 78 70 72 28 70 50 61 72 73 65  ite3PExpr(pParse
2b4d4 2c 20 54 4b 5f 53 45 4c 45 43 54 2c 20 30 2c 20  , TK_SELECT, 0, 
2b4d5 30 2c 20 30 29 3b 0a 20 20 20 20 69 66 28 20 79  0, 0);.    if( y
2b4d6 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b4d7 20 29 7b 0a 20 20 20 20 20 20 79 79 67 6f 74 6f   ){.      yygoto
2b4d8 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 70 53 65  minor.yy450->pSe
2b4d9 6c 65 63 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d  lect = yymsp[-1]
2b4da 2e 6d 69 6e 6f 72 2e 79 79 34 33 3b 0a 20 20 20  .minor.yy43;.   
2b4db 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65     sqlite3ExprSe
2b4dc 74 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20  tHeight(pParse, 
2b4dd 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2b4de 30 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  0);.    }else{. 
2b4df 20 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65       sqlite3Sele
2b4e0 63 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d  ctDelete(pParse-
2b4e1 3e 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  >db, yymsp[-1].m
2b4e2 69 6e 6f 72 2e 79 79 34 33 29 3b 0a 20 20 20 20  inor.yy43);.    
2b4e3 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70  }.    sqlite3Exp
2b4e4 72 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f  rSpan(yygotomino
2b4e5 72 2e 79 79 34 35 30 2c 26 79 79 6d 73 70 5b 2d  r.yy450,&yymsp[-
2b4e6 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79  2].minor.yy0,&yy
2b4e7 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2b4e8 29 3b 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62  );.  }.        b
2b4e9 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b4ea 20 32 32 33 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   223: /* expr ::
2b4eb 3d 20 65 78 70 72 20 69 6e 5f 6f 70 20 4c 50 20  = expr in_op LP 
2b4ec 73 65 6c 65 63 74 20 52 50 20 2a 2f 0a 7b 0a 20  select RP */.{. 
2b4ed 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79     yygotominor.y
2b4ee 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45  y450 = sqlite3PE
2b4ef 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 49  xpr(pParse, TK_I
2b4f0 4e 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e  N, yymsp[-4].min
2b4f1 6f 72 2e 79 79 34 35 30 2c 20 30 2c 20 30 29 3b  or.yy450, 0, 0);
2b4f2 0a 20 20 20 20 69 66 28 20 79 79 67 6f 74 6f 6d  .    if( yygotom
2b4f3 69 6e 6f 72 2e 79 79 34 35 30 20 29 7b 0a 20 20  inor.yy450 ){.  
2b4f4 20 20 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e      yygotominor.
2b4f5 79 79 34 35 30 2d 3e 70 53 65 6c 65 63 74 20 3d  yy450->pSelect =
2b4f6 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72   yymsp[-1].minor
2b4f7 2e 79 79 34 33 3b 0a 20 20 20 20 20 20 73 71 6c  .yy43;.      sql
2b4f8 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67 68  ite3ExprSetHeigh
2b4f9 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74 6f  t(pParse, yygoto
2b4fa 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 20 20  minor.yy450);.  
2b4fb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73    }else{.      s
2b4fc 71 6c 69 74 65 33 53 65 6c 65 63 74 44 65 6c 65  qlite3SelectDele
2b4fd 74 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  te(pParse->db, y
2b4fe 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2b4ff 79 34 33 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20  y43);.    }.    
2b500 69 66 28 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  if( yymsp[-3].mi
2b501 6e 6f 72 2e 79 79 33 31 36 20 29 20 79 79 67 6f  nor.yy316 ) yygo
2b502 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20  tominor.yy450 = 
2b503 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2b504 72 73 65 2c 20 54 4b 5f 4e 4f 54 2c 20 79 79 67  rse, TK_NOT, yyg
2b505 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20  otominor.yy450, 
2b506 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74  0, 0);.    sqlit
2b507 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f 74  e3ExprSpan(yygot
2b508 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 26 79 79  ominor.yy450,&yy
2b509 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-4].minor.yy
2b50a 34 35 30 2d 3e 73 70 61 6e 2c 26 79 79 6d 73 70  450->span,&yymsp
2b50b 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a  [0].minor.yy0);.
2b50c 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61    }.        brea
2b50d 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32  k;.      case 22
2b50e 34 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 65  4: /* expr ::= e
2b50f 78 70 72 20 69 6e 5f 6f 70 20 6e 6d 20 64 62 6e  xpr in_op nm dbn
2b510 6d 20 2a 2f 0a 7b 0a 20 20 20 20 53 72 63 4c 69  m */.{.    SrcLi
2b511 73 74 20 2a 70 53 72 63 20 3d 20 73 71 6c 69 74  st *pSrc = sqlit
2b512 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e 64 28  e3SrcListAppend(
2b513 70 50 61 72 73 65 2d 3e 64 62 2c 20 30 2c 26 79  pParse->db, 0,&y
2b514 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2b515 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2b516 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20 79 79 67  or.yy0);.    yyg
2b517 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d  otominor.yy450 =
2b518 20 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50   sqlite3PExpr(pP
2b519 61 72 73 65 2c 20 54 4b 5f 49 4e 2c 20 79 79 6d  arse, TK_IN, yym
2b51a 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34  sp[-3].minor.yy4
2b51b 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20 69  50, 0, 0);.    i
2b51c 66 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  f( yygotominor.y
2b51d 79 34 35 30 20 29 7b 0a 20 20 20 20 20 20 79 79  y450 ){.      yy
2b51e 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d  gotominor.yy450-
2b51f 3e 70 53 65 6c 65 63 74 20 3d 20 73 71 6c 69 74  >pSelect = sqlit
2b520 65 33 53 65 6c 65 63 74 4e 65 77 28 70 50 61 72  e3SelectNew(pPar
2b521 73 65 2c 20 30 2c 70 53 72 63 2c 30 2c 30 2c 30  se, 0,pSrc,0,0,0
2b522 2c 30 2c 30 2c 30 2c 30 29 3b 0a 20 20 20 20 20  ,0,0,0,0);.     
2b523 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 48   sqlite3ExprSetH
2b524 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79 79  eight(pParse, yy
2b525 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 29  gotominor.yy450)
2b526 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2b527 20 20 20 73 71 6c 69 74 65 33 53 72 63 4c 69 73     sqlite3SrcLis
2b528 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2b529 64 62 2c 20 70 53 72 63 29 3b 0a 20 20 20 20 7d  db, pSrc);.    }
2b52a 0a 20 20 20 20 69 66 28 20 79 79 6d 73 70 5b 2d  .    if( yymsp[-
2b52b 32 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 20 29  2].minor.yy316 )
2b52c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34   yygotominor.yy4
2b52d 35 30 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70  50 = sqlite3PExp
2b52e 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 4e 4f 54  r(pParse, TK_NOT
2b52f 2c 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  , yygotominor.yy
2b530 34 35 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  450, 0, 0);.    
2b531 73 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28  sqlite3ExprSpan(
2b532 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2b533 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e  0,&yymsp[-3].min
2b534 6f 72 2e 79 79 34 35 30 2d 3e 73 70 61 6e 2c 79  or.yy450->span,y
2b535 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b536 30 2e 7a 3f 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  0.z?&yymsp[0].mi
2b537 6e 6f 72 2e 79 79 30 3a 26 79 79 6d 73 70 5b 2d  nor.yy0:&yymsp[-
2b538 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20  1].minor.yy0);. 
2b539 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b   }.        break
2b53a 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 32 35  ;.      case 225
2b53b 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20 45 58  : /* expr ::= EX
2b53c 49 53 54 53 20 4c 50 20 73 65 6c 65 63 74 20 52  ISTS LP select R
2b53d 50 20 2a 2f 0a 7b 0a 20 20 20 20 45 78 70 72 20  P */.{.    Expr 
2b53e 2a 70 20 3d 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  *p = yygotominor
2b53f 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2b540 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2b541 5f 45 58 49 53 54 53 2c 20 30 2c 20 30 2c 20 30  _EXISTS, 0, 0, 0
2b542 29 3b 0a 20 20 20 20 69 66 28 20 70 20 29 7b 0a  );.    if( p ){.
2b543 20 20 20 20 20 20 70 2d 3e 70 53 65 6c 65 63 74        p->pSelect
2b544 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e   = yymsp[-1].min
2b545 6f 72 2e 79 79 34 33 3b 0a 20 20 20 20 20 20 73  or.yy43;.      s
2b546 71 6c 69 74 65 33 45 78 70 72 53 70 61 6e 28 70  qlite3ExprSpan(p
2b547 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f  ,&yymsp[-3].mino
2b548 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e  r.yy0,&yymsp[0].
2b549 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 20 20  minor.yy0);.    
2b54a 20 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74    sqlite3ExprSet
2b54b 48 65 69 67 68 74 28 70 50 61 72 73 65 2c 20 79  Height(pParse, y
2b54c 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b54d 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
2b54e 20 20 20 20 73 71 6c 69 74 65 33 53 65 6c 65 63      sqlite3Selec
2b54f 74 44 65 6c 65 74 65 28 70 50 61 72 73 65 2d 3e  tDelete(pParse->
2b550 64 62 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  db, yymsp[-1].mi
2b551 6e 6f 72 2e 79 79 34 33 29 3b 0a 20 20 20 20 7d  nor.yy43);.    }
2b552 0a 20 20 7d 0a 20 20 20 20 20 20 20 20 62 72 65  .  }.        bre
2b553 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b554 32 36 3a 20 2f 2a 20 65 78 70 72 20 3a 3a 3d 20  26: /* expr ::= 
2b555 43 41 53 45 20 63 61 73 65 5f 6f 70 65 72 61 6e  CASE case_operan
2b556 64 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20  d case_exprlist 
2b557 63 61 73 65 5f 65 6c 73 65 20 45 4e 44 20 2a 2f  case_else END */
2b558 0a 7b 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  .{.  yygotominor
2b559 2e 79 79 34 35 30 20 3d 20 73 71 6c 69 74 65 33  .yy450 = sqlite3
2b55a 50 45 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b  PExpr(pParse, TK
2b55b 5f 43 41 53 45 2c 20 79 79 6d 73 70 5b 2d 33 5d  _CASE, yymsp[-3]
2b55c 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 79 79  .minor.yy450, yy
2b55d 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2b55e 34 35 30 2c 20 30 29 3b 0a 20 20 69 66 28 20 79  450, 0);.  if( y
2b55f 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30  ygotominor.yy450
2b560 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d 69   ){.    yygotomi
2b561 6e 6f 72 2e 79 79 34 35 30 2d 3e 70 4c 69 73 74  nor.yy450->pList
2b562 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e   = yymsp[-2].min
2b563 6f 72 2e 79 79 32 34 32 3b 0a 20 20 20 20 73 71  or.yy242;.    sq
2b564 6c 69 74 65 33 45 78 70 72 53 65 74 48 65 69 67  lite3ExprSetHeig
2b565 68 74 28 70 50 61 72 73 65 2c 20 79 79 67 6f 74  ht(pParse, yygot
2b566 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 20  ominor.yy450);. 
2b567 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
2b568 74 65 33 45 78 70 72 4c 69 73 74 44 65 6c 65 74  te3ExprListDelet
2b569 65 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79 79  e(pParse->db, yy
2b56a 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-2].minor.yy
2b56b 32 34 32 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  242);.  }.  sqli
2b56c 74 65 33 45 78 70 72 53 70 61 6e 28 79 79 67 6f  te3ExprSpan(yygo
2b56d 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20 26  tominor.yy450, &
2b56e 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2b56f 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
2b570 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20  inor.yy0);.}.   
2b571 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b572 20 20 63 61 73 65 20 32 32 37 3a 20 2f 2a 20 63    case 227: /* c
2b573 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a 3a 3d  ase_exprlist ::=
2b574 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 57   case_exprlist W
2b575 48 45 4e 20 65 78 70 72 20 54 48 45 4e 20 65 78  HEN expr THEN ex
2b576 70 72 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74 6f  pr */.{.  yygoto
2b577 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20 73 71  minor.yy242 = sq
2b578 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41 70 70  lite3ExprListApp
2b579 65 6e 64 28 70 50 61 72 73 65 2c 79 79 6d 73 70  end(pParse,yymsp
2b57a 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32  [-4].minor.yy242
2b57b 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2b57c 72 2e 79 79 34 35 30 2c 20 30 29 3b 0a 20 20 79  r.yy450, 0);.  y
2b57d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2b57e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2b57f 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2b580 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2b581 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  2, yymsp[0].mino
2b582 72 2e 79 79 34 35 30 2c 20 30 29 3b 0a 7d 0a 20  r.yy450, 0);.}. 
2b583 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b584 20 20 20 20 63 61 73 65 20 32 32 38 3a 20 2f 2a      case 228: /*
2b585 20 63 61 73 65 5f 65 78 70 72 6c 69 73 74 20 3a   case_exprlist :
2b586 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 54 48 45  := WHEN expr THE
2b587 4e 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 79 79  N expr */.{.  yy
2b588 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20  gotominor.yy242 
2b589 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73  = sqlite3ExprLis
2b58a 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30  tAppend(pParse,0
2b58b 2c 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f  , yymsp[-2].mino
2b58c 72 2e 79 79 34 35 30 2c 20 30 29 3b 0a 20 20 79  r.yy450, 0);.  y
2b58d 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2b58e 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c 69   = sqlite3ExprLi
2b58f 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65 2c  stAppend(pParse,
2b590 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2b591 32 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f  2, yymsp[0].mino
2b592 72 2e 79 79 34 35 30 2c 20 30 29 3b 0a 7d 0a 20  r.yy450, 0);.}. 
2b593 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b594 20 20 20 20 63 61 73 65 20 32 33 37 3a 20 2f 2a      case 237: /*
2b595 20 63 6d 64 20 3a 3a 3d 20 43 52 45 41 54 45 20   cmd ::= CREATE 
2b596 75 6e 69 71 75 65 66 6c 61 67 20 49 4e 44 45 58  uniqueflag INDEX
2b597 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2b598 64 62 6e 6d 20 4f 4e 20 6e 6d 20 4c 50 20 69 64  dbnm ON nm LP id
2b599 78 6c 69 73 74 20 52 50 20 2a 2f 0a 7b 0a 20 20  xlist RP */.{.  
2b59a 73 71 6c 69 74 65 33 43 72 65 61 74 65 49 6e 64  sqlite3CreateInd
2b59b 65 78 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73  ex(pParse, &yyms
2b59c 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c  p[-6].minor.yy0,
2b59d 20 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f   &yymsp[-5].mino
2b59e 72 2e 79 79 30 2c 20 0a 20 20 20 20 20 20 20 20  r.yy0, .        
2b59f 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2b5a0 69 74 65 33 53 72 63 4c 69 73 74 41 70 70 65 6e  ite3SrcListAppen
2b5a1 64 28 70 50 61 72 73 65 2d 3e 64 62 2c 30 2c 26  d(pParse->db,0,&
2b5a2 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e  yymsp[-3].minor.
2b5a3 79 79 30 2c 30 29 2c 20 79 79 6d 73 70 5b 2d 31  yy0,0), yymsp[-1
2b5a4 5d 2e 6d 69 6e 6f 72 2e 79 79 32 34 32 2c 20 79  ].minor.yy242, y
2b5a5 79 6d 73 70 5b 2d 39 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-9].minor.y
2b5a6 79 33 31 36 2c 0a 20 20 20 20 20 20 20 20 20 20  y316,.          
2b5a7 20 20 20 20 20 20 20 20 20 20 20 20 26 79 79 6d              &yym
2b5a8 73 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79  sp[-10].minor.yy
2b5a9 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  0, &yymsp[0].min
2b5aa 6f 72 2e 79 79 30 2c 20 53 51 4c 49 54 45 5f 53  or.yy0, SQLITE_S
2b5ab 4f 5f 41 53 43 2c 20 79 79 6d 73 70 5b 2d 37 5d  O_ASC, yymsp[-7]
2b5ac 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 0a 7d  .minor.yy316);.}
2b5ad 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b5ae 20 20 20 20 20 20 63 61 73 65 20 32 33 38 3a 20        case 238: 
2b5af 2f 2a 20 75 6e 69 71 75 65 66 6c 61 67 20 3a 3a  /* uniqueflag ::
2b5b0 3d 20 55 4e 49 51 55 45 20 2a 2f 0a 20 20 20 20  = UNIQUE */.    
2b5b1 20 20 63 61 73 65 20 32 38 35 3a 20 2f 2a 20 72    case 285: /* r
2b5b2 61 69 73 65 74 79 70 65 20 3a 3a 3d 20 41 42 4f  aisetype ::= ABO
2b5b3 52 54 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  RT */.{yygotomin
2b5b4 6f 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 41 62  or.yy316 = OE_Ab
2b5b5 6f 72 74 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ort;}.        br
2b5b6 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2b5b7 32 33 39 3a 20 2f 2a 20 75 6e 69 71 75 65 66 6c  239: /* uniquefl
2b5b8 61 67 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67 6f 74  ag ::= */.{yygot
2b5b9 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 4f  ominor.yy316 = O
2b5ba 45 5f 4e 6f 6e 65 3b 7d 0a 20 20 20 20 20 20 20  E_None;}.       
2b5bb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b5bc 73 65 20 32 34 32 3a 20 2f 2a 20 69 64 78 6c 69  se 242: /* idxli
2b5bd 73 74 20 3a 3a 3d 20 69 64 78 6c 69 73 74 20 43  st ::= idxlist C
2b5be 4f 4d 4d 41 20 6e 6d 20 63 6f 6c 6c 61 74 65 20  OMMA nm collate 
2b5bf 73 6f 72 74 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20  sortorder */.{. 
2b5c0 20 45 78 70 72 20 2a 70 20 3d 20 30 3b 0a 20 20   Expr *p = 0;.  
2b5c1 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  if( yymsp[-1].mi
2b5c2 6e 6f 72 2e 79 79 30 2e 6e 3e 30 20 29 7b 0a 20  nor.yy0.n>0 ){. 
2b5c3 20 20 20 70 20 3d 20 73 71 6c 69 74 65 33 50 45     p = sqlite3PE
2b5c4 78 70 72 28 70 50 61 72 73 65 2c 20 54 4b 5f 43  xpr(pParse, TK_C
2b5c5 4f 4c 55 4d 4e 2c 20 30 2c 20 30 2c 20 30 29 3b  OLUMN, 0, 0, 0);
2b5c6 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2b5c7 53 65 74 43 6f 6c 6c 28 70 50 61 72 73 65 2c 20  SetColl(pParse, 
2b5c8 70 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  p, &yymsp[-1].mi
2b5c9 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 7d 0a 20 20  nor.yy0);.  }.  
2b5ca 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34  yygotominor.yy24
2b5cb 32 20 3d 20 73 71 6c 69 74 65 33 45 78 70 72 4c  2 = sqlite3ExprL
2b5cc 69 73 74 41 70 70 65 6e 64 28 70 50 61 72 73 65  istAppend(pParse
2b5cd 2c 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  ,yymsp[-4].minor
2b5ce 2e 79 79 32 34 32 2c 20 70 2c 20 26 79 79 6d 73  .yy242, p, &yyms
2b5cf 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29  p[-2].minor.yy0)
2b5d0 3b 0a 20 20 73 71 6c 69 74 65 33 45 78 70 72 4c  ;.  sqlite3ExprL
2b5d1 69 73 74 43 68 65 63 6b 4c 65 6e 67 74 68 28 70  istCheckLength(p
2b5d2 50 61 72 73 65 2c 20 79 79 67 6f 74 6f 6d 69 6e  Parse, yygotomin
2b5d3 6f 72 2e 79 79 32 34 32 2c 20 22 69 6e 64 65 78  or.yy242, "index
2b5d4 22 29 3b 0a 20 20 69 66 28 20 79 79 67 6f 74 6f  ");.  if( yygoto
2b5d5 6d 69 6e 6f 72 2e 79 79 32 34 32 20 29 20 79 79  minor.yy242 ) yy
2b5d6 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2d  gotominor.yy242-
2b5d7 3e 61 5b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  >a[yygotominor.y
2b5d8 79 32 34 32 2d 3e 6e 45 78 70 72 2d 31 5d 2e 73  y242->nExpr-1].s
2b5d9 6f 72 74 4f 72 64 65 72 20 3d 20 79 79 6d 73 70  ortOrder = yymsp
2b5da 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b  [0].minor.yy316;
2b5db 0a 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  .}.        break
2b5dc 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34 33  ;.      case 243
2b5dd 3a 20 2f 2a 20 69 64 78 6c 69 73 74 20 3a 3a 3d  : /* idxlist ::=
2b5de 20 6e 6d 20 63 6f 6c 6c 61 74 65 20 73 6f 72 74   nm collate sort
2b5df 6f 72 64 65 72 20 2a 2f 0a 7b 0a 20 20 45 78 70  order */.{.  Exp
2b5e0 72 20 2a 70 20 3d 20 30 3b 0a 20 20 69 66 28 20  r *p = 0;.  if( 
2b5e1 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2b5e2 79 79 30 2e 6e 3e 30 20 29 7b 0a 20 20 20 20 70  yy0.n>0 ){.    p
2b5e3 20 3d 20 73 71 6c 69 74 65 33 50 45 78 70 72 28   = sqlite3PExpr(
2b5e4 70 50 61 72 73 65 2c 20 54 4b 5f 43 4f 4c 55 4d  pParse, TK_COLUM
2b5e5 4e 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  N, 0, 0, 0);.   
2b5e6 20 73 71 6c 69 74 65 33 45 78 70 72 53 65 74 43   sqlite3ExprSetC
2b5e7 6f 6c 6c 28 70 50 61 72 73 65 2c 20 70 2c 20 26  oll(pParse, p, &
2b5e8 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2b5e9 79 79 30 29 3b 0a 20 20 7d 0a 20 20 79 79 67 6f  yy0);.  }.  yygo
2b5ea 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 20 3d 20  tominor.yy242 = 
2b5eb 73 71 6c 69 74 65 33 45 78 70 72 4c 69 73 74 41  sqlite3ExprListA
2b5ec 70 70 65 6e 64 28 70 50 61 72 73 65 2c 30 2c 20  ppend(pParse,0, 
2b5ed 70 2c 20 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  p, &yymsp[-2].mi
2b5ee 6e 6f 72 2e 79 79 30 29 3b 0a 20 20 73 71 6c 69  nor.yy0);.  sqli
2b5ef 74 65 33 45 78 70 72 4c 69 73 74 43 68 65 63 6b  te3ExprListCheck
2b5f0 4c 65 6e 67 74 68 28 70 50 61 72 73 65 2c 20 79  Length(pParse, y
2b5f1 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 32 34 32  ygotominor.yy242
2b5f2 2c 20 22 69 6e 64 65 78 22 29 3b 0a 20 20 69 66  , "index");.  if
2b5f3 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2b5f4 32 34 32 20 29 20 79 79 67 6f 74 6f 6d 69 6e 6f  242 ) yygotomino
2b5f5 72 2e 79 79 32 34 32 2d 3e 61 5b 79 79 67 6f 74  r.yy242->a[yygot
2b5f6 6f 6d 69 6e 6f 72 2e 79 79 32 34 32 2d 3e 6e 45  ominor.yy242->nE
2b5f7 78 70 72 2d 31 5d 2e 73 6f 72 74 4f 72 64 65 72  xpr-1].sortOrder
2b5f8 20 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f   = yymsp[0].mino
2b5f9 72 2e 79 79 33 31 36 3b 0a 7d 0a 20 20 20 20 20  r.yy316;.}.     
2b5fa 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2b5fb 63 61 73 65 20 32 34 34 3a 20 2f 2a 20 63 6f 6c  case 244: /* col
2b5fc 6c 61 74 65 20 3a 3a 3d 20 2a 2f 0a 7b 79 79 67  late ::= */.{yyg
2b5fd 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 2e 7a 20 3d  otominor.yy0.z =
2b5fe 20 30 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e   0; yygotominor.
2b5ff 79 79 30 2e 6e 20 3d 20 30 3b 7d 0a 20 20 20 20  yy0.n = 0;}.    
2b600 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b601 20 63 61 73 65 20 32 34 36 3a 20 2f 2a 20 63 6d   case 246: /* cm
2b602 64 20 3a 3a 3d 20 44 52 4f 50 20 49 4e 44 45 58  d ::= DROP INDEX
2b603 20 69 66 65 78 69 73 74 73 20 66 75 6c 6c 6e 61   ifexists fullna
2b604 6d 65 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 44 72  me */.{sqlite3Dr
2b605 6f 70 49 6e 64 65 78 28 70 50 61 72 73 65 2c 20  opIndex(pParse, 
2b606 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79  yymsp[0].minor.y
2b607 79 34 31 39 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e  y419, yymsp[-1].
2b608 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a 20  minor.yy316);}. 
2b609 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b60a 20 20 20 20 63 61 73 65 20 32 34 37 3a 20 2f 2a      case 247: /*
2b60b 20 63 6d 64 20 3a 3a 3d 20 56 41 43 55 55 4d 20   cmd ::= VACUUM 
2b60c 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  */.      case 24
2b60d 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 56 41  8: /* cmd ::= VA
2b60e 43 55 55 4d 20 6e 6d 20 2a 2f 0a 7b 73 71 6c 69  CUUM nm */.{sqli
2b60f 74 65 33 56 61 63 75 75 6d 28 70 50 61 72 73 65  te3Vacuum(pParse
2b610 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2b611 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 34  k;.      case 24
2b612 39 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  9: /* cmd ::= PR
2b613 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 45 51 20  AGMA nm dbnm EQ 
2b614 6e 6d 6e 75 6d 20 2a 2f 0a 20 20 20 20 20 20 63  nmnum */.      c
2b615 61 73 65 20 32 35 30 3a 20 2f 2a 20 63 6d 64 20  ase 250: /* cmd 
2b616 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20 64 62  ::= PRAGMA nm db
2b617 6e 6d 20 45 51 20 4f 4e 20 2a 2f 0a 20 20 20 20  nm EQ ON */.    
2b618 20 20 63 61 73 65 20 32 35 31 3a 20 2f 2a 20 63    case 251: /* c
2b619 6d 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d  md ::= PRAGMA nm
2b61a 20 64 62 6e 6d 20 45 51 20 44 45 4c 45 54 45 20   dbnm EQ DELETE 
2b61b 2a 2f 0a 7b 73 71 6c 69 74 65 33 50 72 61 67 6d  */.{sqlite3Pragm
2b61c 61 28 70 50 61 72 73 65 2c 26 79 79 6d 73 70 5b  a(pParse,&yymsp[
2b61d 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79  -3].minor.yy0,&y
2b61e 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2b61f 79 30 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e  y0,&yymsp[0].min
2b620 6f 72 2e 79 79 30 2c 30 29 3b 7d 0a 20 20 20 20  or.yy0,0);}.    
2b621 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b622 20 63 61 73 65 20 32 35 32 3a 20 2f 2a 20 63 6d   case 252: /* cm
2b623 64 20 3a 3a 3d 20 50 52 41 47 4d 41 20 6e 6d 20  d ::= PRAGMA nm 
2b624 64 62 6e 6d 20 45 51 20 6d 69 6e 75 73 5f 6e 75  dbnm EQ minus_nu
2b625 6d 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74 65 33  m */.{.  sqlite3
2b626 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c 26 79  Pragma(pParse,&y
2b627 79 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-3].minor.y
2b628 79 30 2c 26 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69  y0,&yymsp[-2].mi
2b629 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70 5b 30  nor.yy0,&yymsp[0
2b62a 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 31 29 3b 0a  ].minor.yy0,1);.
2b62b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2b62c 0a 20 20 20 20 20 20 63 61 73 65 20 32 35 33 3a  .      case 253:
2b62d 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52 41 47   /* cmd ::= PRAG
2b62e 4d 41 20 6e 6d 20 64 62 6e 6d 20 4c 50 20 6e 6d  MA nm dbnm LP nm
2b62f 6e 75 6d 20 52 50 20 2a 2f 0a 7b 73 71 6c 69 74  num RP */.{sqlit
2b630 65 33 50 72 61 67 6d 61 28 70 50 61 72 73 65 2c  e3Pragma(pParse,
2b631 26 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72  &yymsp[-4].minor
2b632 2e 79 79 30 2c 26 79 79 6d 73 70 5b 2d 33 5d 2e  .yy0,&yymsp[-3].
2b633 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73 70  minor.yy0,&yymsp
2b634 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  [-1].minor.yy0,0
2b635 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2b636 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 35  k;.      case 25
2b637 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 50 52  4: /* cmd ::= PR
2b638 41 47 4d 41 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  AGMA nm dbnm */.
2b639 7b 73 71 6c 69 74 65 33 50 72 61 67 6d 61 28 70  {sqlite3Pragma(p
2b63a 50 61 72 73 65 2c 26 79 79 6d 73 70 5b 2d 31 5d  Parse,&yymsp[-1]
2b63b 2e 6d 69 6e 6f 72 2e 79 79 30 2c 26 79 79 6d 73  .minor.yy0,&yyms
2b63c 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 30  p[0].minor.yy0,0
2b63d 2c 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72  ,0);}.        br
2b63e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2b63f 32 36 32 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20  262: /* cmd ::= 
2b640 43 52 45 41 54 45 20 74 72 69 67 67 65 72 5f 64  CREATE trigger_d
2b641 65 63 6c 20 42 45 47 49 4e 20 74 72 69 67 67 65  ecl BEGIN trigge
2b642 72 5f 63 6d 64 5f 6c 69 73 74 20 45 4e 44 20 2a  r_cmd_list END *
2b643 2f 0a 7b 0a 20 20 54 6f 6b 65 6e 20 61 6c 6c 3b  /.{.  Token all;
2b644 0a 20 20 61 6c 6c 2e 7a 20 3d 20 79 79 6d 73 70  .  all.z = yymsp
2b645 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e 7a  [-3].minor.yy0.z
2b646 3b 0a 20 20 61 6c 6c 2e 6e 20 3d 20 28 79 79 6d  ;.  all.n = (yym
2b647 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
2b648 7a 20 2d 20 79 79 6d 73 70 5b 2d 33 5d 2e 6d 69  z - yymsp[-3].mi
2b649 6e 6f 72 2e 79 79 30 2e 7a 29 20 2b 20 79 79 6d  nor.yy0.z) + yym
2b64a 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2e  sp[0].minor.yy0.
2b64b 6e 3b 0a 20 20 73 71 6c 69 74 65 33 46 69 6e 69  n;.  sqlite3Fini
2b64c 73 68 54 72 69 67 67 65 72 28 70 50 61 72 73 65  shTrigger(pParse
2b64d 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2b64e 72 2e 79 79 37 35 2c 20 26 61 6c 6c 29 3b 0a 7d  r.yy75, &all);.}
2b64f 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b650 20 20 20 20 20 20 63 61 73 65 20 32 36 33 3a 20        case 263: 
2b651 2f 2a 20 74 72 69 67 67 65 72 5f 64 65 63 6c 20  /* trigger_decl 
2b652 3a 3a 3d 20 74 65 6d 70 20 54 52 49 47 47 45 52  ::= temp TRIGGER
2b653 20 69 66 6e 6f 74 65 78 69 73 74 73 20 6e 6d 20   ifnotexists nm 
2b654 64 62 6e 6d 20 74 72 69 67 67 65 72 5f 74 69 6d  dbnm trigger_tim
2b655 65 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  e trigger_event 
2b656 4f 4e 20 66 75 6c 6c 6e 61 6d 65 20 66 6f 72 65  ON fullname fore
2b657 61 63 68 5f 63 6c 61 75 73 65 20 77 68 65 6e 5f  ach_clause when_
2b658 63 6c 61 75 73 65 20 2a 2f 0a 7b 0a 20 20 73 71  clause */.{.  sq
2b659 6c 69 74 65 33 42 65 67 69 6e 54 72 69 67 67 65  lite3BeginTrigge
2b65a 72 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70  r(pParse, &yymsp
2b65b 5b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-7].minor.yy0, 
2b65c 26 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  &yymsp[-6].minor
2b65d 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 35 5d 2e  .yy0, yymsp[-5].
2b65e 6d 69 6e 6f 72 2e 79 79 33 31 36 2c 20 79 79 6d  minor.yy316, yym
2b65f 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-4].minor.yy3
2b660 35 34 2e 61 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  54.a, yymsp[-4].
2b661 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 62 2c 20 79  minor.yy354.b, y
2b662 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-2].minor.y
2b663 79 34 31 39 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d  y419, yymsp[0].m
2b664 69 6e 6f 72 2e 79 79 34 35 30 2c 20 79 79 6d 73  inor.yy450, yyms
2b665 70 5b 2d 31 30 5d 2e 6d 69 6e 6f 72 2e 79 79 33  p[-10].minor.yy3
2b666 31 36 2c 20 79 79 6d 73 70 5b 2d 38 5d 2e 6d 69  16, yymsp[-8].mi
2b667 6e 6f 72 2e 79 79 33 31 36 29 3b 0a 20 20 79 79  nor.yy316);.  yy
2b668 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 30 20 3d 20  gotominor.yy0 = 
2b669 28 79 79 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72  (yymsp[-6].minor
2b66a 2e 79 79 30 2e 6e 3d 3d 30 3f 79 79 6d 73 70 5b  .yy0.n==0?yymsp[
2b66b 2d 37 5d 2e 6d 69 6e 6f 72 2e 79 79 30 3a 79 79  -7].minor.yy0:yy
2b66c 6d 73 70 5b 2d 36 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-6].minor.yy
2b66d 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62 72  0);.}.        br
2b66e 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20  eak;.      case 
2b66f 32 36 34 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  264: /* trigger_
2b670 74 69 6d 65 20 3a 3a 3d 20 42 45 46 4f 52 45 20  time ::= BEFORE 
2b671 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 32 36  */.      case 26
2b672 37 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 74 69  7: /* trigger_ti
2b673 6d 65 20 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f  me ::= */.{ yygo
2b674 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20  tominor.yy316 = 
2b675 54 4b 5f 42 45 46 4f 52 45 3b 20 7d 0a 20 20 20  TK_BEFORE; }.   
2b676 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20       break;.    
2b677 20 20 63 61 73 65 20 32 36 35 3a 20 2f 2a 20 74    case 265: /* t
2b678 72 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20  rigger_time ::= 
2b679 41 46 54 45 52 20 2a 2f 0a 7b 20 79 79 67 6f 74  AFTER */.{ yygot
2b67a 6f 6d 69 6e 6f 72 2e 79 79 33 31 36 20 3d 20 54  ominor.yy316 = T
2b67b 4b 5f 41 46 54 45 52 3b 20 20 7d 0a 20 20 20 20  K_AFTER;  }.    
2b67c 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b67d 20 63 61 73 65 20 32 36 36 3a 20 2f 2a 20 74 72   case 266: /* tr
2b67e 69 67 67 65 72 5f 74 69 6d 65 20 3a 3a 3d 20 49  igger_time ::= I
2b67f 4e 53 54 45 41 44 20 4f 46 20 2a 2f 0a 7b 20 79  NSTEAD OF */.{ y
2b680 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33 31 36  ygotominor.yy316
2b681 20 3d 20 54 4b 5f 49 4e 53 54 45 41 44 3b 7d 0a   = TK_INSTEAD;}.
2b682 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b683 20 20 20 20 20 63 61 73 65 20 32 36 38 3a 20 2f       case 268: /
2b684 2a 20 74 72 69 67 67 65 72 5f 65 76 65 6e 74 20  * trigger_event 
2b685 3a 3a 3d 20 44 45 4c 45 54 45 7c 49 4e 53 45 52  ::= DELETE|INSER
2b686 54 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  T */.      case 
2b687 32 36 39 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f  269: /* trigger_
2b688 65 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45  event ::= UPDATE
2b689 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72   */.{yygotominor
2b68a 2e 79 79 33 35 34 2e 61 20 3d 20 79 79 6d 73 70  .yy354.a = yymsp
2b68b 5b 30 5d 2e 6d 61 6a 6f 72 3b 20 79 79 67 6f 74  [0].major; yygot
2b68c 6f 6d 69 6e 6f 72 2e 79 79 33 35 34 2e 62 20 3d  ominor.yy354.b =
2b68d 20 30 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65   0;}.        bre
2b68e 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b68f 37 30 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 65  70: /* trigger_e
2b690 76 65 6e 74 20 3a 3a 3d 20 55 50 44 41 54 45 20  vent ::= UPDATE 
2b691 4f 46 20 69 6e 73 63 6f 6c 6c 69 73 74 20 2a 2f  OF inscollist */
2b692 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  .{yygotominor.yy
2b693 33 35 34 2e 61 20 3d 20 54 4b 5f 55 50 44 41 54  354.a = TK_UPDAT
2b694 45 3b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  E; yygotominor.y
2b695 79 33 35 34 2e 62 20 3d 20 79 79 6d 73 70 5b 30  y354.b = yymsp[0
2b696 5d 2e 6d 69 6e 6f 72 2e 79 79 33 35 32 3b 7d 0a  ].minor.yy352;}.
2b697 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b698 20 20 20 20 20 63 61 73 65 20 32 37 33 3a 20 2f       case 273: /
2b699 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20 3a 3a  * when_clause ::
2b69a 3d 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20  = */.      case 
2b69b 32 39 30 3a 20 2f 2a 20 6b 65 79 5f 6f 70 74 20  290: /* key_opt 
2b69c 3a 3a 3d 20 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d  ::= */.{ yygotom
2b69d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 30 3b 20  inor.yy450 = 0; 
2b69e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2b69f 0a 20 20 20 20 20 20 63 61 73 65 20 32 37 34 3a  .      case 274:
2b6a0 20 2f 2a 20 77 68 65 6e 5f 63 6c 61 75 73 65 20   /* when_clause 
2b6a1 3a 3a 3d 20 57 48 45 4e 20 65 78 70 72 20 2a 2f  ::= WHEN expr */
2b6a2 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 31 3a  .      case 291:
2b6a3 20 2f 2a 20 6b 65 79 5f 6f 70 74 20 3a 3a 3d 20   /* key_opt ::= 
2b6a4 4b 45 59 20 65 78 70 72 20 2a 2f 0a 7b 20 79 79  KEY expr */.{ yy
2b6a5 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20  gotominor.yy450 
2b6a6 3d 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  = yymsp[0].minor
2b6a7 2e 79 79 34 35 30 3b 20 7d 0a 20 20 20 20 20 20  .yy450; }.      
2b6a8 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b6a9 61 73 65 20 32 37 35 3a 20 2f 2a 20 74 72 69 67  ase 275: /* trig
2b6aa 67 65 72 5f 63 6d 64 5f 6c 69 73 74 20 3a 3a 3d  ger_cmd_list ::=
2b6ab 20 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73   trigger_cmd_lis
2b6ac 74 20 74 72 69 67 67 65 72 5f 63 6d 64 20 53 45  t trigger_cmd SE
2b6ad 4d 49 20 2a 2f 0a 7b 0a 2f 2a 0a 20 20 69 66 28  MI */.{./*.  if(
2b6ae 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72   yymsp[-2].minor
2b6af 2e 79 79 37 35 20 29 7b 0a 20 20 20 20 79 79 6d  .yy75 ){.    yym
2b6b0 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-2].minor.yy7
2b6b1 35 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65 78 74 20  5->pLast->pNext 
2b6b2 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  = yymsp[-1].mino
2b6b3 72 2e 79 79 37 35 3b 0a 20 20 7d 65 6c 73 65 7b  r.yy75;.  }else{
2b6b4 0a 20 20 20 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d  .    yymsp[-2].m
2b6b5 69 6e 6f 72 2e 79 79 37 35 20 3d 20 79 79 6d 73  inor.yy75 = yyms
2b6b6 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35  p[-1].minor.yy75
2b6b7 3b 0a 20 20 7d 0a 2a 2f 0a 20 20 61 73 73 65 72  ;.  }.*/.  asser
2b6b8 74 28 20 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e  t( yymsp[-2].min
2b6b9 6f 72 2e 79 79 37 35 21 3d 30 20 29 3b 0a 20 20  or.yy75!=0 );.  
2b6ba 79 79 6d 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e  yymsp[-2].minor.
2b6bb 79 79 37 35 2d 3e 70 4c 61 73 74 2d 3e 70 4e 65  yy75->pLast->pNe
2b6bc 78 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d  xt = yymsp[-1].m
2b6bd 69 6e 6f 72 2e 79 79 37 35 3b 0a 20 20 79 79 6d  inor.yy75;.  yym
2b6be 73 70 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 37  sp[-2].minor.yy7
2b6bf 35 2d 3e 70 4c 61 73 74 20 3d 20 79 79 6d 73 70  5->pLast = yymsp
2b6c0 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35 3b  [-1].minor.yy75;
2b6c1 0a 20 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79  .  yygotominor.y
2b6c2 79 37 35 20 3d 20 79 79 6d 73 70 5b 2d 32 5d 2e  y75 = yymsp[-2].
2b6c3 6d 69 6e 6f 72 2e 79 79 37 35 3b 0a 7d 0a 20 20  minor.yy75;.}.  
2b6c4 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b6c5 20 20 20 63 61 73 65 20 32 37 36 3a 20 2f 2a 20     case 276: /* 
2b6c6 74 72 69 67 67 65 72 5f 63 6d 64 5f 6c 69 73 74  trigger_cmd_list
2b6c7 20 3a 3a 3d 20 74 72 69 67 67 65 72 5f 63 6d 64   ::= trigger_cmd
2b6c8 20 53 45 4d 49 20 2a 2f 0a 7b 20 0a 20 20 2f 2a   SEMI */.{ .  /*
2b6c9 20 69 66 28 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d   if( yymsp[-1].m
2b6ca 69 6e 6f 72 2e 79 79 37 35 20 29 20 2a 2f 0a 20  inor.yy75 ) */. 
2b6cb 20 61 73 73 65 72 74 28 20 79 79 6d 73 70 5b 2d   assert( yymsp[-
2b6cc 31 5d 2e 6d 69 6e 6f 72 2e 79 79 37 35 21 3d 30  1].minor.yy75!=0
2b6cd 20 29 3b 0a 20 20 79 79 6d 73 70 5b 2d 31 5d 2e   );.  yymsp[-1].
2b6ce 6d 69 6e 6f 72 2e 79 79 37 35 2d 3e 70 4c 61 73  minor.yy75->pLas
2b6cf 74 20 3d 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69  t = yymsp[-1].mi
2b6d0 6e 6f 72 2e 79 79 37 35 3b 0a 20 20 79 79 67 6f  nor.yy75;.  yygo
2b6d1 74 6f 6d 69 6e 6f 72 2e 79 79 37 35 20 3d 20 79  tominor.yy75 = y
2b6d2 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79  ymsp[-1].minor.y
2b6d3 79 37 35 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  y75;.}.        b
2b6d4 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b6d5 20 32 37 37 3a 20 2f 2a 20 74 72 69 67 67 65 72   277: /* trigger
2b6d6 5f 63 6d 64 20 3a 3a 3d 20 55 50 44 41 54 45 20  _cmd ::= UPDATE 
2b6d7 6f 72 63 6f 6e 66 20 6e 6d 20 53 45 54 20 73 65  orconf nm SET se
2b6d8 74 6c 69 73 74 20 77 68 65 72 65 5f 6f 70 74 20  tlist where_opt 
2b6d9 2a 2f 0a 7b 20 79 79 67 6f 74 6f 6d 69 6e 6f 72  */.{ yygotominor
2b6da 2e 79 79 37 35 20 3d 20 73 71 6c 69 74 65 33 54  .yy75 = sqlite3T
2b6db 72 69 67 67 65 72 55 70 64 61 74 65 53 74 65 70  riggerUpdateStep
2b6dc 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79  (pParse->db, &yy
2b6dd 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2b6de 30 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e  0, yymsp[-1].min
2b6df 6f 72 2e 79 79 32 34 32 2c 20 79 79 6d 73 70 5b  or.yy242, yymsp[
2b6e0 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 2c 20  0].minor.yy450, 
2b6e1 79 79 6d 73 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e  yymsp[-4].minor.
2b6e2 79 79 33 31 36 29 3b 20 7d 0a 20 20 20 20 20 20  yy316); }.      
2b6e3 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b6e4 61 73 65 20 32 37 38 3a 20 2f 2a 20 74 72 69 67  ase 278: /* trig
2b6e5 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 69 6e 73 65  ger_cmd ::= inse
2b6e6 72 74 5f 63 6d 64 20 49 4e 54 4f 20 6e 6d 20 69  rt_cmd INTO nm i
2b6e7 6e 73 63 6f 6c 6c 69 73 74 5f 6f 70 74 20 56 41  nscollist_opt VA
2b6e8 4c 55 45 53 20 4c 50 20 69 74 65 6d 6c 69 73 74  LUES LP itemlist
2b6e9 20 52 50 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69   RP */.{yygotomi
2b6ea 6e 6f 72 2e 79 79 37 35 20 3d 20 73 71 6c 69 74  nor.yy75 = sqlit
2b6eb 65 33 54 72 69 67 67 65 72 49 6e 73 65 72 74 53  e3TriggerInsertS
2b6ec 74 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20  tep(pParse->db, 
2b6ed 26 79 79 6d 73 70 5b 2d 35 5d 2e 6d 69 6e 6f 72  &yymsp[-5].minor
2b6ee 2e 79 79 30 2c 20 79 79 6d 73 70 5b 2d 34 5d 2e  .yy0, yymsp[-4].
2b6ef 6d 69 6e 6f 72 2e 79 79 33 35 32 2c 20 79 79 6d  minor.yy352, yym
2b6f0 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 32  sp[-1].minor.yy2
2b6f1 34 32 2c 20 30 2c 20 79 79 6d 73 70 5b 2d 37 5d  42, 0, yymsp[-7]
2b6f2 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 29 3b 7d 0a  .minor.yy316);}.
2b6f3 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2b6f4 20 20 20 20 20 63 61 73 65 20 32 37 39 3a 20 2f       case 279: /
2b6f5 2a 20 74 72 69 67 67 65 72 5f 63 6d 64 20 3a 3a  * trigger_cmd ::
2b6f6 3d 20 69 6e 73 65 72 74 5f 63 6d 64 20 49 4e 54  = insert_cmd INT
2b6f7 4f 20 6e 6d 20 69 6e 73 63 6f 6c 6c 69 73 74 5f  O nm inscollist_
2b6f8 6f 70 74 20 73 65 6c 65 63 74 20 2a 2f 0a 7b 79  opt select */.{y
2b6f9 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 37 35 20  ygotominor.yy75 
2b6fa 3d 20 73 71 6c 69 74 65 33 54 72 69 67 67 65 72  = sqlite3Trigger
2b6fb 49 6e 73 65 72 74 53 74 65 70 28 70 50 61 72 73  InsertStep(pPars
2b6fc 65 2d 3e 64 62 2c 20 26 79 79 6d 73 70 5b 2d 32  e->db, &yymsp[-2
2b6fd 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 79 79 6d  ].minor.yy0, yym
2b6fe 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 33  sp[-1].minor.yy3
2b6ff 35 32 2c 20 30 2c 20 79 79 6d 73 70 5b 30 5d 2e  52, 0, yymsp[0].
2b700 6d 69 6e 6f 72 2e 79 79 34 33 2c 20 79 79 6d 73  minor.yy43, yyms
2b701 70 5b 2d 34 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31  p[-4].minor.yy31
2b702 36 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  6);}.        bre
2b703 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b704 38 30 3a 20 2f 2a 20 74 72 69 67 67 65 72 5f 63  80: /* trigger_c
2b705 6d 64 20 3a 3a 3d 20 44 45 4c 45 54 45 20 46 52  md ::= DELETE FR
2b706 4f 4d 20 6e 6d 20 77 68 65 72 65 5f 6f 70 74 20  OM nm where_opt 
2b707 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  */.{yygotominor.
2b708 79 79 37 35 20 3d 20 73 71 6c 69 74 65 33 54 72  yy75 = sqlite3Tr
2b709 69 67 67 65 72 44 65 6c 65 74 65 53 74 65 70 28  iggerDeleteStep(
2b70a 70 50 61 72 73 65 2d 3e 64 62 2c 20 26 79 79 6d  pParse->db, &yym
2b70b 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30  sp[-1].minor.yy0
2b70c 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  , yymsp[0].minor
2b70d 2e 79 79 34 35 30 29 3b 7d 0a 20 20 20 20 20 20  .yy450);}.      
2b70e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b70f 61 73 65 20 32 38 31 3a 20 2f 2a 20 74 72 69 67  ase 281: /* trig
2b710 67 65 72 5f 63 6d 64 20 3a 3a 3d 20 73 65 6c 65  ger_cmd ::= sele
2b711 63 74 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e  ct */.{yygotomin
2b712 6f 72 2e 79 79 37 35 20 3d 20 73 71 6c 69 74 65  or.yy75 = sqlite
2b713 33 54 72 69 67 67 65 72 53 65 6c 65 63 74 53 74  3TriggerSelectSt
2b714 65 70 28 70 50 61 72 73 65 2d 3e 64 62 2c 20 79  ep(pParse->db, y
2b715 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b716 34 33 29 3b 20 7d 0a 20 20 20 20 20 20 20 20 62  43); }.        b
2b717 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b718 20 32 38 32 3a 20 2f 2a 20 65 78 70 72 20 3a 3a   282: /* expr ::
2b719 3d 20 52 41 49 53 45 20 4c 50 20 49 47 4e 4f 52  = RAISE LP IGNOR
2b71a 45 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f  E RP */.{.  yygo
2b71b 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20  tominor.yy450 = 
2b71c 73 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61  sqlite3PExpr(pPa
2b71d 72 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30  rse, TK_RAISE, 0
2b71e 2c 20 30 2c 20 30 29 3b 20 0a 20 20 69 66 28 20  , 0, 0); .  if( 
2b71f 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 34 35  yygotominor.yy45
2b720 30 20 29 7b 0a 20 20 20 20 79 79 67 6f 74 6f 6d  0 ){.    yygotom
2b721 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 69 43 6f 6c  inor.yy450->iCol
2b722 75 6d 6e 20 3d 20 4f 45 5f 49 67 6e 6f 72 65 3b  umn = OE_Ignore;
2b723 0a 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72  .    sqlite3Expr
2b724 53 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72  Span(yygotominor
2b725 2e 79 79 34 35 30 2c 20 26 79 79 6d 73 70 5b 2d  .yy450, &yymsp[-
2b726 33 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  3].minor.yy0, &y
2b727 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b728 30 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20  0);.  }.}.      
2b729 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b72a 61 73 65 20 32 38 33 3a 20 2f 2a 20 65 78 70 72  ase 283: /* expr
2b72b 20 3a 3a 3d 20 52 41 49 53 45 20 4c 50 20 72 61   ::= RAISE LP ra
2b72c 69 73 65 74 79 70 65 20 43 4f 4d 4d 41 20 6e 6d  isetype COMMA nm
2b72d 20 52 50 20 2a 2f 0a 7b 0a 20 20 79 79 67 6f 74   RP */.{.  yygot
2b72e 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 20 3d 20 73  ominor.yy450 = s
2b72f 71 6c 69 74 65 33 50 45 78 70 72 28 70 50 61 72  qlite3PExpr(pPar
2b730 73 65 2c 20 54 4b 5f 52 41 49 53 45 2c 20 30 2c  se, TK_RAISE, 0,
2b731 20 30 2c 20 26 79 79 6d 73 70 5b 2d 31 5d 2e 6d   0, &yymsp[-1].m
2b732 69 6e 6f 72 2e 79 79 30 29 3b 20 0a 20 20 69 66  inor.yy0); .  if
2b733 28 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79  ( yygotominor.yy
2b734 34 35 30 20 29 20 7b 0a 20 20 20 20 79 79 67 6f  450 ) {.    yygo
2b735 74 6f 6d 69 6e 6f 72 2e 79 79 34 35 30 2d 3e 69  tominor.yy450->i
2b736 43 6f 6c 75 6d 6e 20 3d 20 79 79 6d 73 70 5b 2d  Column = yymsp[-
2b737 33 5d 2e 6d 69 6e 6f 72 2e 79 79 33 31 36 3b 0a  3].minor.yy316;.
2b738 20 20 20 20 73 71 6c 69 74 65 33 45 78 70 72 53      sqlite3ExprS
2b739 70 61 6e 28 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e  pan(yygotominor.
2b73a 79 79 34 35 30 2c 20 26 79 79 6d 73 70 5b 2d 35  yy450, &yymsp[-5
2b73b 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79  ].minor.yy0, &yy
2b73c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30  msp[0].minor.yy0
2b73d 29 3b 0a 20 20 7d 0a 7d 0a 20 20 20 20 20 20 20  );.  }.}.       
2b73e 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2b73f 73 65 20 32 38 34 3a 20 2f 2a 20 72 61 69 73 65  se 284: /* raise
2b740 74 79 70 65 20 3a 3a 3d 20 52 4f 4c 4c 42 41 43  type ::= ROLLBAC
2b741 4b 20 2a 2f 0a 7b 79 79 67 6f 74 6f 6d 69 6e 6f  K */.{yygotomino
2b742 72 2e 79 79 33 31 36 20 3d 20 4f 45 5f 52 6f 6c  r.yy316 = OE_Rol
2b743 6c 62 61 63 6b 3b 7d 0a 20 20 20 20 20 20 20 20  lback;}.        
2b744 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b745 65 20 32 38 36 3a 20 2f 2a 20 72 61 69 73 65 74  e 286: /* raiset
2b746 79 70 65 20 3a 3a 3d 20 46 41 49 4c 20 2a 2f 0a  ype ::= FAIL */.
2b747 7b 79 79 67 6f 74 6f 6d 69 6e 6f 72 2e 79 79 33  {yygotominor.yy3
2b748 31 36 20 3d 20 4f 45 5f 46 61 69 6c 3b 7d 0a 20  16 = OE_Fail;}. 
2b749 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2b74a 20 20 20 20 63 61 73 65 20 32 38 37 3a 20 2f 2a      case 287: /*
2b74b 20 63 6d 64 20 3a 3a 3d 20 44 52 4f 50 20 54 52   cmd ::= DROP TR
2b74c 49 47 47 45 52 20 69 66 65 78 69 73 74 73 20 66  IGGER ifexists f
2b74d 75 6c 6c 6e 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73  ullname */.{.  s
2b74e 71 6c 69 74 65 33 44 72 6f 70 54 72 69 67 67 65  qlite3DropTrigge
2b74f 72 28 70 50 61 72 73 65 2c 79 79 6d 73 70 5b 30  r(pParse,yymsp[0
2b750 5d 2e 6d 69 6e 6f 72 2e 79 79 34 31 39 2c 79 79  ].minor.yy419,yy
2b751 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-1].minor.yy
2b752 33 31 36 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  316);.}.        
2b753 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b754 65 20 32 38 38 3a 20 2f 2a 20 63 6d 64 20 3a 3a  e 288: /* cmd ::
2b755 3d 20 41 54 54 41 43 48 20 64 61 74 61 62 61 73  = ATTACH databas
2b756 65 5f 6b 77 5f 6f 70 74 20 65 78 70 72 20 41 53  e_kw_opt expr AS
2b757 20 65 78 70 72 20 6b 65 79 5f 6f 70 74 20 2a 2f   expr key_opt */
2b758 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 74 74 61  .{.  sqlite3Atta
2b759 63 68 28 70 50 61 72 73 65 2c 20 79 79 6d 73 70  ch(pParse, yymsp
2b75a 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30  [-3].minor.yy450
2b75b 2c 20 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f  , yymsp[-1].mino
2b75c 72 2e 79 79 34 35 30 2c 20 79 79 6d 73 70 5b 30  r.yy450, yymsp[0
2b75d 5d 2e 6d 69 6e 6f 72 2e 79 79 34 35 30 29 3b 0a  ].minor.yy450);.
2b75e 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b  }.        break;
2b75f 0a 20 20 20 20 20 20 63 61 73 65 20 32 38 39 3a  .      case 289:
2b760 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 44 45 54 41   /* cmd ::= DETA
2b761 43 48 20 64 61 74 61 62 61 73 65 5f 6b 77 5f 6f  CH database_kw_o
2b762 70 74 20 65 78 70 72 20 2a 2f 0a 7b 0a 20 20 73  pt expr */.{.  s
2b763 71 6c 69 74 65 33 44 65 74 61 63 68 28 70 50 61  qlite3Detach(pPa
2b764 72 73 65 2c 20 79 79 6d 73 70 5b 30 5d 2e 6d 69  rse, yymsp[0].mi
2b765 6e 6f 72 2e 79 79 34 35 30 29 3b 0a 7d 0a 20 20  nor.yy450);.}.  
2b766 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2b767 20 20 20 63 61 73 65 20 32 39 34 3a 20 2f 2a 20     case 294: /* 
2b768 63 6d 64 20 3a 3a 3d 20 52 45 49 4e 44 45 58 20  cmd ::= REINDEX 
2b769 2a 2f 0a 7b 73 71 6c 69 74 65 33 52 65 69 6e 64  */.{sqlite3Reind
2b76a 65 78 28 70 50 61 72 73 65 2c 20 30 2c 20 30 29  ex(pParse, 0, 0)
2b76b 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  ;}.        break
2b76c 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32 39 35  ;.      case 295
2b76d 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 52 45 49  : /* cmd ::= REI
2b76e 4e 44 45 58 20 6e 6d 20 64 62 6e 6d 20 2a 2f 0a  NDEX nm dbnm */.
2b76f 7b 73 71 6c 69 74 65 33 52 65 69 6e 64 65 78 28  {sqlite3Reindex(
2b770 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b 2d  pParse, &yymsp[-
2b771 31 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20 26 79  1].minor.yy0, &y
2b772 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b773 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2b774 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 32  ak;.      case 2
2b775 39 36 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 41  96: /* cmd ::= A
2b776 4e 41 4c 59 5a 45 20 2a 2f 0a 7b 73 71 6c 69 74  NALYZE */.{sqlit
2b777 65 33 41 6e 61 6c 79 7a 65 28 70 50 61 72 73 65  e3Analyze(pParse
2b778 2c 20 30 2c 20 30 29 3b 7d 0a 20 20 20 20 20 20  , 0, 0);}.      
2b779 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63    break;.      c
2b77a 61 73 65 20 32 39 37 3a 20 2f 2a 20 63 6d 64 20  ase 297: /* cmd 
2b77b 3a 3a 3d 20 41 4e 41 4c 59 5a 45 20 6e 6d 20 64  ::= ANALYZE nm d
2b77c 62 6e 6d 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 41  bnm */.{sqlite3A
2b77d 6e 61 6c 79 7a 65 28 70 50 61 72 73 65 2c 20 26  nalyze(pParse, &
2b77e 79 79 6d 73 70 5b 2d 31 5d 2e 6d 69 6e 6f 72 2e  yymsp[-1].minor.
2b77f 79 79 30 2c 20 26 79 79 6d 73 70 5b 30 5d 2e 6d  yy0, &yymsp[0].m
2b780 69 6e 6f 72 2e 79 79 30 29 3b 7d 0a 20 20 20 20  inor.yy0);}.    
2b781 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b782 20 63 61 73 65 20 32 39 38 3a 20 2f 2a 20 63 6d   case 298: /* cm
2b783 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c  d ::= ALTER TABL
2b784 45 20 66 75 6c 6c 6e 61 6d 65 20 52 45 4e 41 4d  E fullname RENAM
2b785 45 20 54 4f 20 6e 6d 20 2a 2f 0a 7b 0a 20 20 73  E TO nm */.{.  s
2b786 71 6c 69 74 65 33 41 6c 74 65 72 52 65 6e 61 6d  qlite3AlterRenam
2b787 65 54 61 62 6c 65 28 70 50 61 72 73 65 2c 79 79  eTable(pParse,yy
2b788 6d 73 70 5b 2d 33 5d 2e 6d 69 6e 6f 72 2e 79 79  msp[-3].minor.yy
2b789 34 31 39 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69  419,&yymsp[0].mi
2b78a 6e 6f 72 2e 79 79 30 29 3b 0a 7d 0a 20 20 20 20  nor.yy0);.}.    
2b78b 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20      break;.     
2b78c 20 63 61 73 65 20 32 39 39 3a 20 2f 2a 20 63 6d   case 299: /* cm
2b78d 64 20 3a 3a 3d 20 41 4c 54 45 52 20 54 41 42 4c  d ::= ALTER TABL
2b78e 45 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75 6c  E add_column_ful
2b78f 6c 6e 61 6d 65 20 41 44 44 20 6b 77 63 6f 6c 75  lname ADD kwcolu
2b790 6d 6e 5f 6f 70 74 20 63 6f 6c 75 6d 6e 20 2a 2f  mn_opt column */
2b791 0a 7b 0a 20 20 73 71 6c 69 74 65 33 41 6c 74 65  .{.  sqlite3Alte
2b792 72 46 69 6e 69 73 68 41 64 64 43 6f 6c 75 6d 6e  rFinishAddColumn
2b793 28 70 50 61 72 73 65 2c 20 26 79 79 6d 73 70 5b  (pParse, &yymsp[
2b794 30 5d 2e 6d 69 6e 6f 72 2e 79 79 30 29 3b 0a 7d  0].minor.yy0);.}
2b795 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2b796 20 20 20 20 20 20 63 61 73 65 20 33 30 30 3a 20        case 300: 
2b797 2f 2a 20 61 64 64 5f 63 6f 6c 75 6d 6e 5f 66 75  /* add_column_fu
2b798 6c 6c 6e 61 6d 65 20 3a 3a 3d 20 66 75 6c 6c 6e  llname ::= fulln
2b799 61 6d 65 20 2a 2f 0a 7b 0a 20 20 73 71 6c 69 74  ame */.{.  sqlit
2b79a 65 33 41 6c 74 65 72 42 65 67 69 6e 41 64 64 43  e3AlterBeginAddC
2b79b 6f 6c 75 6d 6e 28 70 50 61 72 73 65 2c 20 79 79  olumn(pParse, yy
2b79c 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79 34  msp[0].minor.yy4
2b79d 31 39 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20 62  19);.}.        b
2b79e 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b79f 20 33 30 33 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d   303: /* cmd ::=
2b7a0 20 63 72 65 61 74 65 5f 76 74 61 62 20 2a 2f 0a   create_vtab */.
2b7a1 7b 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69  {sqlite3VtabFini
2b7a2 73 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 30  shParse(pParse,0
2b7a3 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65 61  );}.        brea
2b7a4 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33 30  k;.      case 30
2b7a5 34 3a 20 2f 2a 20 63 6d 64 20 3a 3a 3d 20 63 72  4: /* cmd ::= cr
2b7a6 65 61 74 65 5f 76 74 61 62 20 4c 50 20 76 74 61  eate_vtab LP vta
2b7a7 62 61 72 67 6c 69 73 74 20 52 50 20 2a 2f 0a 7b  barglist RP */.{
2b7a8 73 71 6c 69 74 65 33 56 74 61 62 46 69 6e 69 73  sqlite3VtabFinis
2b7a9 68 50 61 72 73 65 28 70 50 61 72 73 65 2c 26 79  hParse(pParse,&y
2b7aa 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e 79 79  ymsp[0].minor.yy
2b7ab 30 29 3b 7d 0a 20 20 20 20 20 20 20 20 62 72 65  0);}.        bre
2b7ac 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 33  ak;.      case 3
2b7ad 30 35 3a 20 2f 2a 20 63 72 65 61 74 65 5f 76 74  05: /* create_vt
2b7ae 61 62 20 3a 3a 3d 20 43 52 45 41 54 45 20 56 49  ab ::= CREATE VI
2b7af 52 54 55 41 4c 20 54 41 42 4c 45 20 6e 6d 20 64  RTUAL TABLE nm d
2b7b0 62 6e 6d 20 55 53 49 4e 47 20 6e 6d 20 2a 2f 0a  bnm USING nm */.
2b7b1 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 56 74 61  {.    sqlite3Vta
2b7b2 62 42 65 67 69 6e 50 61 72 73 65 28 70 50 61 72  bBeginParse(pPar
2b7b3 73 65 2c 20 26 79 79 6d 73 70 5b 2d 33 5d 2e 6d  se, &yymsp[-3].m
2b7b4 69 6e 6f 72 2e 79 79 30 2c 20 26 79 79 6d 73 70  inor.yy0, &yymsp
2b7b5 5b 2d 32 5d 2e 6d 69 6e 6f 72 2e 79 79 30 2c 20  [-2].minor.yy0, 
2b7b6 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72 2e  &yymsp[0].minor.
2b7b7 79 79 30 29 3b 0a 7d 0a 20 20 20 20 20 20 20 20  yy0);.}.        
2b7b8 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73  break;.      cas
2b7b9 65 20 33 30 38 3a 20 2f 2a 20 76 74 61 62 61 72  e 308: /* vtabar
2b7ba 67 20 3a 3a 3d 20 2a 2f 0a 7b 73 71 6c 69 74 65  g ::= */.{sqlite
2b7bb 33 56 74 61 62 41 72 67 49 6e 69 74 28 70 50 61  3VtabArgInit(pPa
2b7bc 72 73 65 29 3b 7d 0a 20 20 20 20 20 20 20 20 62  rse);}.        b
2b7bd 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65  reak;.      case
2b7be 20 33 31 30 3a 20 2f 2a 20 76 74 61 62 61 72 67   310: /* vtabarg
2b7bf 74 6f 6b 65 6e 20 3a 3a 3d 20 41 4e 59 20 2a 2f  token ::= ANY */
2b7c0 0a 20 20 20 20 20 20 63 61 73 65 20 33 31 31 3a  .      case 311:
2b7c1 20 2f 2a 20 76 74 61 62 61 72 67 74 6f 6b 65 6e   /* vtabargtoken
2b7c2 20 3a 3a 3d 20 6c 70 20 61 6e 79 6c 69 73 74 20   ::= lp anylist 
2b7c3 52 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  RP */.      case
2b7c4 20 33 31 32 3a 20 2f 2a 20 6c 70 20 3a 3a 3d 20   312: /* lp ::= 
2b7c5 4c 50 20 2a 2f 0a 20 20 20 20 20 20 63 61 73 65  LP */.      case
2b7c6 20 33 31 34 3a 20 2f 2a 20 61 6e 79 6c 69 73 74   314: /* anylist
2b7c7 20 3a 3a 3d 20 61 6e 79 6c 69 73 74 20 41 4e 59   ::= anylist ANY
2b7c8 20 2a 2f 0a 7b 73 71 6c 69 74 65 33 56 74 61 62   */.{sqlite3Vtab
2b7c9 41 72 67 45 78 74 65 6e 64 28 70 50 61 72 73 65  ArgExtend(pParse
2b7ca 2c 26 79 79 6d 73 70 5b 30 5d 2e 6d 69 6e 6f 72  ,&yymsp[0].minor
2b7cb 2e 79 79 30 29 3b 7d 0a 20 20 20 20 20 20 20 20  .yy0);}.        
2b7cc 62 72 65 61 6b 3b 0a 20 20 7d 3b 0a 20 20 79 79  break;.  };.  yy
2b7cd 67 6f 74 6f 20 3d 20 79 79 52 75 6c 65 49 6e 66  goto = yyRuleInf
2b7ce 6f 5b 79 79 72 75 6c 65 6e 6f 5d 2e 6c 68 73 3b  o[yyruleno].lhs;
2b7cf 0a 20 20 79 79 73 69 7a 65 20 3d 20 79 79 52 75  .  yysize = yyRu
2b7d0 6c 65 49 6e 66 6f 5b 79 79 72 75 6c 65 6e 6f 5d  leInfo[yyruleno]
2b7d1 2e 6e 72 68 73 3b 0a 20 20 79 79 70 50 61 72 73  .nrhs;.  yypPars
2b7d2 65 72 2d 3e 79 79 69 64 78 20 2d 3d 20 79 79 73  er->yyidx -= yys
2b7d3 69 7a 65 3b 0a 20 20 79 79 61 63 74 20 3d 20 79  ize;.  yyact = y
2b7d4 79 5f 66 69 6e 64 5f 72 65 64 75 63 65 5f 61 63  y_find_reduce_ac
2b7d5 74 69 6f 6e 28 79 79 6d 73 70 5b 2d 79 79 73 69  tion(yymsp[-yysi
2b7d6 7a 65 5d 2e 73 74 61 74 65 6e 6f 2c 79 79 67 6f  ze].stateno,yygo
2b7d7 74 6f 29 3b 0a 20 20 69 66 28 20 79 79 61 63 74  to);.  if( yyact
2b7d8 20 3c 20 59 59 4e 53 54 41 54 45 20 29 7b 0a 23   < YYNSTATE ){.#
2b7d9 69 66 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  ifdef NDEBUG.   
2b7da 20 2f 2a 20 49 66 20 77 65 20 61 72 65 20 6e 6f   /* If we are no
2b7db 74 20 64 65 62 75 67 67 69 6e 67 20 61 6e 64 20  t debugging and 
2b7dc 74 68 65 20 72 65 64 75 63 65 20 61 63 74 69 6f  the reduce actio
2b7dd 6e 20 70 6f 70 70 65 64 20 61 74 20 6c 65 61 73  n popped at leas
2b7de 74 0a 20 20 20 20 2a 2a 20 6f 6e 65 20 65 6c 65  t.    ** one ele
2b7df 6d 65 6e 74 20 6f 66 66 20 74 68 65 20 73 74 61  ment off the sta
2b7e0 63 6b 2c 20 74 68 65 6e 20 77 65 20 63 61 6e 20  ck, then we can 
2b7e1 70 75 73 68 20 74 68 65 20 6e 65 77 20 65 6c 65  push the new ele
2b7e2 6d 65 6e 74 20 62 61 63 6b 0a 20 20 20 20 2a 2a  ment back.    **
2b7e3 20 6f 6e 74 6f 20 74 68 65 20 73 74 61 63 6b 20   onto the stack 
2b7e4 68 65 72 65 2c 20 61 6e 64 20 73 6b 69 70 20 74  here, and skip t
2b7e5 68 65 20 73 74 61 63 6b 20 6f 76 65 72 66 6c 6f  he stack overflo
2b7e6 77 20 74 65 73 74 20 69 6e 20 79 79 5f 73 68 69  w test in yy_shi
2b7e7 66 74 28 29 2e 0a 20 20 20 20 2a 2a 20 54 68 61  ft()..    ** Tha
2b7e8 74 20 67 69 76 65 73 20 61 20 73 69 67 6e 69 66  t gives a signif
2b7e9 69 63 61 6e 74 20 73 70 65 65 64 20 69 6d 70 72  icant speed impr
2b7ea 6f 76 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 20 20  ovement. */.    
2b7eb 69 66 28 20 79 79 73 69 7a 65 20 29 7b 0a 20 20  if( yysize ){.  
2b7ec 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79      yypParser->y
2b7ed 79 69 64 78 2b 2b 3b 0a 20 20 20 20 20 20 79 79  yidx++;.      yy
2b7ee 6d 73 70 20 2d 3d 20 79 79 73 69 7a 65 2d 31 3b  msp -= yysize-1;
2b7ef 0a 20 20 20 20 20 20 79 79 6d 73 70 2d 3e 73 74  .      yymsp->st
2b7f0 61 74 65 6e 6f 20 3d 20 79 79 61 63 74 3b 0a 20  ateno = yyact;. 
2b7f1 20 20 20 20 20 79 79 6d 73 70 2d 3e 6d 61 6a 6f       yymsp->majo
2b7f2 72 20 3d 20 79 79 67 6f 74 6f 3b 0a 20 20 20 20  r = yygoto;.    
2b7f3 20 20 79 79 6d 73 70 2d 3e 6d 69 6e 6f 72 20 3d    yymsp->minor =
2b7f4 20 79 79 67 6f 74 6f 6d 69 6e 6f 72 3b 0a 20 20   yygotominor;.  
2b7f5 20 20 7d 65 6c 73 65 0a 23 65 6e 64 69 66 0a 20    }else.#endif. 
2b7f6 20 20 20 7b 0a 20 20 20 20 20 20 79 79 5f 73 68     {.      yy_sh
2b7f7 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79 79  ift(yypParser,yy
2b7f8 61 63 74 2c 79 79 67 6f 74 6f 2c 26 79 79 67 6f  act,yygoto,&yygo
2b7f9 74 6f 6d 69 6e 6f 72 29 3b 0a 20 20 20 20 7d 0a  tominor);.    }.
2b7fa 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73    }else{.    ass
2b7fb 65 72 74 28 20 79 79 61 63 74 20 3d 3d 20 59 59  ert( yyact == YY
2b7fc 4e 53 54 41 54 45 20 2b 20 59 59 4e 52 55 4c 45  NSTATE + YYNRULE
2b7fd 20 2b 20 31 20 29 3b 0a 20 20 20 20 79 79 5f 61   + 1 );.    yy_a
2b7fe 63 63 65 70 74 28 79 79 70 50 61 72 73 65 72 29  ccept(yypParser)
2b7ff 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  ;.  }.}../*.** T
2b800 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 6f 64  he following cod
2b801 65 20 65 78 65 63 75 74 65 73 20 77 68 65 6e 20  e executes when 
2b802 74 68 65 20 70 61 72 73 65 20 66 61 69 6c 73 0a  the parse fails.
2b803 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 79  */.static void y
2b804 79 5f 70 61 72 73 65 5f 66 61 69 6c 65 64 28 0a  y_parse_failed(.
2b805 20 20 79 79 50 61 72 73 65 72 20 2a 79 79 70 50    yyParser *yypP
2b806 61 72 73 65 72 20 20 20 20 20 20 20 20 20 20 20  arser           
2b807 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2b808 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 50 61 72  .){.  sqlite3Par
2b809 73 65 72 41 52 47 5f 46 45 54 43 48 3b 0a 23 69  serARG_FETCH;.#i
2b80a 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 69  fndef NDEBUG.  i
2b80b 66 28 20 79 79 54 72 61 63 65 46 49 4c 45 20 29  f( yyTraceFILE )
2b80c 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28 79 79  {.    fprintf(yy
2b80d 54 72 61 63 65 46 49 4c 45 2c 22 25 73 46 61 69  TraceFILE,"%sFai
2b80e 6c 21 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f  l!\n",yyTracePro
2b80f 6d 70 74 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66  mpt);.  }.#endif
2b810 0a 20 20 77 68 69 6c 65 28 20 79 79 70 50 61 72  .  while( yypPar
2b811 73 65 72 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20  ser->yyidx>=0 ) 
2b812 79 79 5f 70 6f 70 5f 70 61 72 73 65 72 5f 73 74  yy_pop_parser_st
2b813 61 63 6b 28 79 79 70 50 61 72 73 65 72 29 3b 0a  ack(yypParser);.
2b814 20 20 2f 2a 20 48 65 72 65 20 63 6f 64 65 20 69    /* Here code i
2b815 73 20 69 6e 73 65 72 74 65 64 20 77 68 69 63 68  s inserted which
2b816 20 77 69 6c 6c 20 62 65 20 65 78 65 63 75 74 65   will be execute
2b817 64 20 77 68 65 6e 65 76 65 72 20 74 68 65 0a 20  d whenever the. 
2b818 20 2a 2a 20 70 61 72 73 65 72 20 66 61 69 6c 73   ** parser fails
2b819 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72   */.  sqlite3Par
2b81a 73 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a  serARG_STORE; /*
2b81b 20 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e   Suppress warnin
2b81c 67 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25  g about unused %
2b81d 65 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76  extra_argument v
2b81e 61 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a  ariable */.}../*
2b81f 0a 2a 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e  .** The followin
2b820 67 20 63 6f 64 65 20 65 78 65 63 75 74 65 73 20  g code executes 
2b821 77 68 65 6e 20 61 20 73 79 6e 74 61 78 20 65 72  when a syntax er
2b822 72 6f 72 20 66 69 72 73 74 20 6f 63 63 75 72 73  ror first occurs
2b823 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2b824 20 79 79 5f 73 79 6e 74 61 78 5f 65 72 72 6f 72   yy_syntax_error
2b825 28 0a 20 20 79 79 50 61 72 73 65 72 20 2a 79 79  (.  yyParser *yy
2b826 70 50 61 72 73 65 72 2c 20 20 20 20 20 20 20 20  pParser,        
2b827 20 20 20 2f 2a 20 54 68 65 20 70 61 72 73 65 72     /* The parser
2b828 20 2a 2f 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f   */.  int yymajo
2b829 72 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  r,              
2b82a 20 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 6a 6f       /* The majo
2b82b 72 20 74 79 70 65 20 6f 66 20 74 68 65 20 65 72  r type of the er
2b82c 72 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 59  ror token */.  Y
2b82d 59 4d 49 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e  YMINORTYPE yymin
2b82e 6f 72 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  or            /*
2b82f 20 54 68 65 20 6d 69 6e 6f 72 20 74 79 70 65 20   The minor type 
2b830 6f 66 20 74 68 65 20 65 72 72 6f 72 20 74 6f 6b  of the error tok
2b831 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73 71 6c 69 74  en */.){.  sqlit
2b832 65 33 50 61 72 73 65 72 41 52 47 5f 46 45 54 43  e3ParserARG_FETC
2b833 48 3b 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e  H;.#define TOKEN
2b834 20 28 79 79 6d 69 6e 6f 72 2e 79 79 30 29 0a 0a   (yyminor.yy0)..
2b835 20 20 61 73 73 65 72 74 28 20 54 4f 4b 45 4e 2e    assert( TOKEN.
2b836 7a 5b 30 5d 20 29 3b 20 20 2f 2a 20 54 68 65 20  z[0] );  /* The 
2b837 74 6f 6b 65 6e 69 7a 65 72 20 61 6c 77 61 79 73  tokenizer always
2b838 20 67 69 76 65 73 20 75 73 20 61 20 74 6f 6b 65   gives us a toke
2b839 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72  n */.  sqlite3Er
2b83a 72 6f 72 4d 73 67 28 70 50 61 72 73 65 2c 20 22  rorMsg(pParse, "
2b83b 6e 65 61 72 20 5c 22 25 54 5c 22 3a 20 73 79 6e  near \"%T\": syn
2b83c 74 61 78 20 65 72 72 6f 72 22 2c 20 26 54 4f 4b  tax error", &TOK
2b83d 45 4e 29 3b 0a 20 20 70 50 61 72 73 65 2d 3e 70  EN);.  pParse->p
2b83e 61 72 73 65 45 72 72 6f 72 20 3d 20 31 3b 0a 20  arseError = 1;. 
2b83f 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2b840 47 5f 53 54 4f 52 45 3b 20 2f 2a 20 53 75 70 70  G_STORE; /* Supp
2b841 72 65 73 73 20 77 61 72 6e 69 6e 67 20 61 62 6f  ress warning abo
2b842 75 74 20 75 6e 75 73 65 64 20 25 65 78 74 72 61  ut unused %extra
2b843 5f 61 72 67 75 6d 65 6e 74 20 76 61 72 69 61 62  _argument variab
2b844 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  le */.}../*.** T
2b845 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20  he following is 
2b846 65 78 65 63 75 74 65 64 20 77 68 65 6e 20 74 68  executed when th
2b847 65 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73  e parser accepts
2b848 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2b849 79 79 5f 61 63 63 65 70 74 28 0a 20 20 79 79 50  yy_accept(.  yyP
2b84a 61 72 73 65 72 20 2a 79 79 70 50 61 72 73 65 72  arser *yypParser
2b84b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2b84c 65 20 70 61 72 73 65 72 20 2a 2f 0a 29 7b 0a 20  e parser */.){. 
2b84d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52   sqlite3ParserAR
2b84e 47 5f 46 45 54 43 48 3b 0a 23 69 66 6e 64 65 66  G_FETCH;.#ifndef
2b84f 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2b850 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
2b851 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2b852 46 49 4c 45 2c 22 25 73 41 63 63 65 70 74 21 5c  FILE,"%sAccept!\
2b853 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70 74  n",yyTracePrompt
2b854 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  );.  }.#endif.  
2b855 77 68 69 6c 65 28 20 79 79 70 50 61 72 73 65 72  while( yypParser
2b856 2d 3e 79 79 69 64 78 3e 3d 30 20 29 20 79 79 5f  ->yyidx>=0 ) yy_
2b857 70 6f 70 5f 70 61 72 73 65 72 5f 73 74 61 63 6b  pop_parser_stack
2b858 28 79 79 70 50 61 72 73 65 72 29 3b 0a 20 20 2f  (yypParser);.  /
2b859 2a 20 48 65 72 65 20 63 6f 64 65 20 69 73 20 69  * Here code is i
2b85a 6e 73 65 72 74 65 64 20 77 68 69 63 68 20 77 69  nserted which wi
2b85b 6c 6c 20 62 65 20 65 78 65 63 75 74 65 64 20 77  ll be executed w
2b85c 68 65 6e 65 76 65 72 20 74 68 65 0a 20 20 2a 2a  henever the.  **
2b85d 20 70 61 72 73 65 72 20 61 63 63 65 70 74 73 20   parser accepts 
2b85e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73  */.  sqlite3Pars
2b85f 65 72 41 52 47 5f 53 54 4f 52 45 3b 20 2f 2a 20  erARG_STORE; /* 
2b860 53 75 70 70 72 65 73 73 20 77 61 72 6e 69 6e 67  Suppress warning
2b861 20 61 62 6f 75 74 20 75 6e 75 73 65 64 20 25 65   about unused %e
2b862 78 74 72 61 5f 61 72 67 75 6d 65 6e 74 20 76 61  xtra_argument va
2b863 72 69 61 62 6c 65 20 2a 2f 0a 7d 0a 0a 2f 2a 20  riable */.}../* 
2b864 54 68 65 20 6d 61 69 6e 20 70 61 72 73 65 72 20  The main parser 
2b865 70 72 6f 67 72 61 6d 2e 0a 2a 2a 20 54 68 65 20  program..** The 
2b866 66 69 72 73 74 20 61 72 67 75 6d 65 6e 74 20 69  first argument i
2b867 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
2b868 20 73 74 72 75 63 74 75 72 65 20 6f 62 74 61 69   structure obtai
2b869 6e 65 64 20 66 72 6f 6d 0a 2a 2a 20 22 73 71 6c  ned from.** "sql
2b86a 69 74 65 33 50 61 72 73 65 72 41 6c 6c 6f 63 22  ite3ParserAlloc"
2b86b 20 77 68 69 63 68 20 64 65 73 63 72 69 62 65 73   which describes
2b86c 20 74 68 65 20 63 75 72 72 65 6e 74 20 73 74 61   the current sta
2b86d 74 65 20 6f 66 20 74 68 65 20 70 61 72 73 65 72  te of the parser
2b86e 2e 0a 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20  ..** The second 
2b86f 61 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20  argument is the 
2b870 6d 61 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62  major token numb
2b871 65 72 2e 20 20 54 68 65 20 74 68 69 72 64 20 69  er.  The third i
2b872 73 0a 2a 2a 20 74 68 65 20 6d 69 6e 6f 72 20 74  s.** the minor t
2b873 6f 6b 65 6e 2e 20 20 54 68 65 20 66 6f 75 72 74  oken.  The fourt
2b874 68 20 6f 70 74 69 6f 6e 61 6c 20 61 72 67 75 6d  h optional argum
2b875 65 6e 74 20 69 73 20 77 68 61 74 65 76 65 72 20  ent is whatever 
2b876 74 68 65 0a 2a 2a 20 75 73 65 72 20 77 61 6e 74  the.** user want
2b877 73 20 28 61 6e 64 20 73 70 65 63 69 66 69 65 64  s (and specified
2b878 20 69 6e 20 74 68 65 20 67 72 61 6d 6d 61 72 29   in the grammar)
2b879 20 61 6e 64 20 69 73 20 61 76 61 69 6c 61 62 6c   and is availabl
2b87a 65 20 66 6f 72 0a 2a 2a 20 75 73 65 20 62 79 20  e for.** use by 
2b87b 74 68 65 20 61 63 74 69 6f 6e 20 72 6f 75 74 69  the action routi
2b87c 6e 65 73 2e 0a 2a 2a 0a 2a 2a 20 49 6e 70 75 74  nes..**.** Input
2b87d 73 3a 0a 2a 2a 20 3c 75 6c 3e 0a 2a 2a 20 3c 6c  s:.** <ul>.** <l
2b87e 69 3e 20 41 20 70 6f 69 6e 74 65 72 20 74 6f 20  i> A pointer to 
2b87f 74 68 65 20 70 61 72 73 65 72 20 28 61 6e 20 6f  the parser (an o
2b880 70 61 71 75 65 20 73 74 72 75 63 74 75 72 65 2e  paque structure.
2b881 29 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 61  ).** <li> The ma
2b882 6a 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  jor token number
2b883 2e 0a 2a 2a 20 3c 6c 69 3e 20 54 68 65 20 6d 69  ..** <li> The mi
2b884 6e 6f 72 20 74 6f 6b 65 6e 20 6e 75 6d 62 65 72  nor token number
2b885 2e 0a 2a 2a 20 3c 6c 69 3e 20 41 6e 20 6f 70 74  ..** <li> An opt
2b886 69 6f 6e 20 61 72 67 75 6d 65 6e 74 20 6f 66 20  ion argument of 
2b887 61 20 67 72 61 6d 6d 61 72 2d 73 70 65 63 69 66  a grammar-specif
2b888 69 65 64 20 74 79 70 65 2e 0a 2a 2a 20 3c 2f 75  ied type..** </u
2b889 6c 3e 0a 2a 2a 0a 2a 2a 20 4f 75 74 70 75 74 73  l>.**.** Outputs
2b88a 3a 0a 2a 2a 20 4e 6f 6e 65 2e 0a 2a 2f 0a 53 51  :.** None..*/.SQ
2b88b 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
2b88c 64 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28  d sqlite3Parser(
2b88d 0a 20 20 76 6f 69 64 20 2a 79 79 70 2c 20 20 20  .  void *yyp,   
2b88e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b88f 2f 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f  /* The parser */
2b890 0a 20 20 69 6e 74 20 79 79 6d 61 6a 6f 72 2c 20  .  int yymajor, 
2b891 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b892 2f 2a 20 54 68 65 20 6d 61 6a 6f 72 20 74 6f 6b  /* The major tok
2b893 65 6e 20 63 6f 64 65 20 6e 75 6d 62 65 72 20 2a  en code number *
2b894 2f 0a 20 20 73 71 6c 69 74 65 33 50 61 72 73 65  /.  sqlite3Parse
2b895 72 54 4f 4b 45 4e 54 59 50 45 20 79 79 6d 69 6e  rTOKENTYPE yymin
2b896 6f 72 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20  or       /* The 
2b897 76 61 6c 75 65 20 66 6f 72 20 74 68 65 20 74 6f  value for the to
2b898 6b 65 6e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ken */.  sqlite3
2b899 50 61 72 73 65 72 41 52 47 5f 50 44 45 43 4c 20  ParserARG_PDECL 
2b89a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2b89b 20 4f 70 74 69 6f 6e 61 6c 20 25 65 78 74 72 61   Optional %extra
2b89c 5f 61 72 67 75 6d 65 6e 74 20 70 61 72 61 6d 65  _argument parame
2b89d 74 65 72 20 2a 2f 0a 29 7b 0a 20 20 59 59 4d 49  ter */.){.  YYMI
2b89e 4e 4f 52 54 59 50 45 20 79 79 6d 69 6e 6f 72 75  NORTYPE yyminoru
2b89f 6e 69 6f 6e 3b 0a 20 20 69 6e 74 20 79 79 61 63  nion;.  int yyac
2b8a0 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a  t;            /*
2b8a1 20 54 68 65 20 70 61 72 73 65 72 20 61 63 74 69   The parser acti
2b8a2 6f 6e 2e 20 2a 2f 0a 20 20 69 6e 74 20 79 79 65  on. */.  int yye
2b8a3 6e 64 6f 66 69 6e 70 75 74 3b 20 20 20 20 20 2f  ndofinput;     /
2b8a4 2a 20 54 72 75 65 20 69 66 20 77 65 20 61 72 65  * True if we are
2b8a5 20 61 74 20 74 68 65 20 65 6e 64 20 6f 66 20 69   at the end of i
2b8a6 6e 70 75 74 20 2a 2f 0a 23 69 66 64 65 66 20 59  nput */.#ifdef Y
2b8a7 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20 20 69  YERRORSYMBOL.  i
2b8a8 6e 74 20 79 79 65 72 72 6f 72 68 69 74 20 3d 20  nt yyerrorhit = 
2b8a9 30 3b 20 20 20 2f 2a 20 54 72 75 65 20 69 66 20  0;   /* True if 
2b8aa 79 79 6d 61 6a 6f 72 20 68 61 73 20 69 6e 76 6f  yymajor has invo
2b8ab 6b 65 64 20 61 6e 20 65 72 72 6f 72 20 2a 2f 0a  ked an error */.
2b8ac 23 65 6e 64 69 66 0a 20 20 79 79 50 61 72 73 65  #endif.  yyParse
2b8ad 72 20 2a 79 79 70 50 61 72 73 65 72 3b 20 20 2f  r *yypParser;  /
2b8ae 2a 20 54 68 65 20 70 61 72 73 65 72 20 2a 2f 0a  * The parser */.
2b8af 0a 20 20 2f 2a 20 28 72 65 29 69 6e 69 74 69 61  .  /* (re)initia
2b8b0 6c 69 7a 65 20 74 68 65 20 70 61 72 73 65 72 2c  lize the parser,
2b8b1 20 69 66 20 6e 65 63 65 73 73 61 72 79 20 2a 2f   if necessary */
2b8b2 0a 20 20 79 79 70 50 61 72 73 65 72 20 3d 20 28  .  yypParser = (
2b8b3 79 79 50 61 72 73 65 72 2a 29 79 79 70 3b 0a 20  yyParser*)yyp;. 
2b8b4 20 69 66 28 20 79 79 70 50 61 72 73 65 72 2d 3e   if( yypParser->
2b8b5 79 79 69 64 78 3c 30 20 29 7b 0a 23 69 66 20 59  yyidx<0 ){.#if Y
2b8b6 59 53 54 41 43 4b 44 45 50 54 48 3c 3d 30 0a 20  YSTACKDEPTH<=0. 
2b8b7 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65 72     if( yypParser
2b8b8 2d 3e 79 79 73 74 6b 73 7a 20 3c 3d 30 20 29 7b  ->yystksz <=0 ){
2b8b9 0a 20 20 20 20 20 20 2f 2a 6d 65 6d 73 65 74 28  .      /*memset(
2b8ba 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2c 20 30  &yyminorunion, 0
2b8bb 2c 20 73 69 7a 65 6f 66 28 79 79 6d 69 6e 6f 72  , sizeof(yyminor
2b8bc 75 6e 69 6f 6e 29 29 3b 2a 2f 0a 20 20 20 20 20  union));*/.     
2b8bd 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 20 3d 20   yyminorunion = 
2b8be 79 79 7a 65 72 6f 6d 69 6e 6f 72 3b 0a 20 20 20  yyzerominor;.   
2b8bf 20 20 20 79 79 53 74 61 63 6b 4f 76 65 72 66 6c     yyStackOverfl
2b8c0 6f 77 28 79 79 70 50 61 72 73 65 72 2c 20 26 79  ow(yypParser, &y
2b8c1 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2b8c2 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
2b8c3 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20 79 79 70  }.#endif.    yyp
2b8c4 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3d 20  Parser->yyidx = 
2b8c5 30 3b 0a 20 20 20 20 79 79 70 50 61 72 73 65 72  0;.    yypParser
2b8c6 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 2d 31 3b  ->yyerrcnt = -1;
2b8c7 0a 20 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e  .    yypParser->
2b8c8 79 79 73 74 61 63 6b 5b 30 5d 2e 73 74 61 74 65  yystack[0].state
2b8c9 6e 6f 20 3d 20 30 3b 0a 20 20 20 20 79 79 70 50  no = 0;.    yypP
2b8ca 61 72 73 65 72 2d 3e 79 79 73 74 61 63 6b 5b 30  arser->yystack[0
2b8cb 5d 2e 6d 61 6a 6f 72 20 3d 20 30 3b 0a 20 20 7d  ].major = 0;.  }
2b8cc 0a 20 20 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 2e  .  yyminorunion.
2b8cd 79 79 30 20 3d 20 79 79 6d 69 6e 6f 72 3b 0a 20  yy0 = yyminor;. 
2b8ce 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 3d 20   yyendofinput = 
2b8cf 28 79 79 6d 61 6a 6f 72 3d 3d 30 29 3b 0a 20 20  (yymajor==0);.  
2b8d0 73 71 6c 69 74 65 33 50 61 72 73 65 72 41 52 47  sqlite3ParserARG
2b8d1 5f 53 54 4f 52 45 3b 0a 0a 23 69 66 6e 64 65 66  _STORE;..#ifndef
2b8d2 20 4e 44 45 42 55 47 0a 20 20 69 66 28 20 79 79   NDEBUG.  if( yy
2b8d3 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20 20  TraceFILE ){.   
2b8d4 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63 65   fprintf(yyTrace
2b8d5 46 49 4c 45 2c 22 25 73 49 6e 70 75 74 20 25 73  FILE,"%sInput %s
2b8d6 5c 6e 22 2c 79 79 54 72 61 63 65 50 72 6f 6d 70  \n",yyTracePromp
2b8d7 74 2c 79 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79  t,yyTokenName[yy
2b8d8 6d 61 6a 6f 72 5d 29 3b 0a 20 20 7d 0a 23 65 6e  major]);.  }.#en
2b8d9 64 69 66 0a 0a 20 20 64 6f 7b 0a 20 20 20 20 79  dif..  do{.    y
2b8da 79 61 63 74 20 3d 20 79 79 5f 66 69 6e 64 5f 73  yact = yy_find_s
2b8db 68 69 66 74 5f 61 63 74 69 6f 6e 28 79 79 70 50  hift_action(yypP
2b8dc 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 29 3b 0a  arser,yymajor);.
2b8dd 20 20 20 20 69 66 28 20 79 79 61 63 74 3c 59 59      if( yyact<YY
2b8de 4e 53 54 41 54 45 20 29 7b 0a 20 20 20 20 20 20  NSTATE ){.      
2b8df 61 73 73 65 72 74 28 20 21 79 79 65 6e 64 6f 66  assert( !yyendof
2b8e0 69 6e 70 75 74 20 29 3b 20 20 2f 2a 20 49 6d 70  input );  /* Imp
2b8e1 6f 73 73 69 62 6c 65 20 74 6f 20 73 68 69 66 74  ossible to shift
2b8e2 20 74 68 65 20 24 20 74 6f 6b 65 6e 20 2a 2f 0a   the $ token */.
2b8e3 20 20 20 20 20 20 79 79 5f 73 68 69 66 74 28 79        yy_shift(y
2b8e4 79 70 50 61 72 73 65 72 2c 79 79 61 63 74 2c 79  ypParser,yyact,y
2b8e5 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72 75  ymajor,&yyminoru
2b8e6 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 79 79 70  nion);.      yyp
2b8e7 50 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74  Parser->yyerrcnt
2b8e8 2d 2d 3b 0a 20 20 20 20 20 20 79 79 6d 61 6a 6f  --;.      yymajo
2b8e9 72 20 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20  r = YYNOCODE;.  
2b8ea 20 20 7d 65 6c 73 65 20 69 66 28 20 79 79 61 63    }else if( yyac
2b8eb 74 20 3c 20 59 59 4e 53 54 41 54 45 20 2b 20 59  t < YYNSTATE + Y
2b8ec 59 4e 52 55 4c 45 20 29 7b 0a 20 20 20 20 20 20  YNRULE ){.      
2b8ed 79 79 5f 72 65 64 75 63 65 28 79 79 70 50 61 72  yy_reduce(yypPar
2b8ee 73 65 72 2c 79 79 61 63 74 2d 59 59 4e 53 54 41  ser,yyact-YYNSTA
2b8ef 54 45 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  TE);.    }else{.
2b8f0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 79 79        assert( yy
2b8f1 61 63 74 20 3d 3d 20 59 59 5f 45 52 52 4f 52 5f  act == YY_ERROR_
2b8f2 41 43 54 49 4f 4e 20 29 3b 0a 23 69 66 64 65 66  ACTION );.#ifdef
2b8f3 20 59 59 45 52 52 4f 52 53 59 4d 42 4f 4c 0a 20   YYERRORSYMBOL. 
2b8f4 20 20 20 20 20 69 6e 74 20 79 79 6d 78 3b 0a 23       int yymx;.#
2b8f5 65 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4e 44  endif.#ifndef ND
2b8f6 45 42 55 47 0a 20 20 20 20 20 20 69 66 28 20 79  EBUG.      if( y
2b8f7 79 54 72 61 63 65 46 49 4c 45 20 29 7b 0a 20 20  yTraceFILE ){.  
2b8f8 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 79 79        fprintf(yy
2b8f9 54 72 61 63 65 46 49 4c 45 2c 22 25 73 53 79 6e  TraceFILE,"%sSyn
2b8fa 74 61 78 20 45 72 72 6f 72 21 5c 6e 22 2c 79 79  tax Error!\n",yy
2b8fb 54 72 61 63 65 50 72 6f 6d 70 74 29 3b 0a 20 20  TracePrompt);.  
2b8fc 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 23 69 66      }.#endif.#if
2b8fd 64 65 66 20 59 59 45 52 52 4f 52 53 59 4d 42 4f  def YYERRORSYMBO
2b8fe 4c 0a 20 20 20 20 20 20 2f 2a 20 41 20 73 79 6e  L.      /* A syn
2b8ff 74 61 78 20 65 72 72 6f 72 20 68 61 73 20 6f 63  tax error has oc
2b900 63 75 72 72 65 64 2e 0a 20 20 20 20 20 20 2a 2a  curred..      **
2b901 20 54 68 65 20 72 65 73 70 6f 6e 73 65 20 74 6f   The response to
2b902 20 61 6e 20 65 72 72 6f 72 20 64 65 70 65 6e 64   an error depend
2b903 73 20 75 70 6f 6e 20 77 68 65 74 68 65 72 20 6f  s upon whether o
2b904 72 20 6e 6f 74 20 74 68 65 0a 20 20 20 20 20 20  r not the.      
2b905 2a 2a 20 67 72 61 6d 6d 61 72 20 64 65 66 69 6e  ** grammar defin
2b906 65 73 20 61 6e 20 65 72 72 6f 72 20 74 6f 6b 65  es an error toke
2b907 6e 20 22 45 52 52 4f 52 22 2e 20 20 0a 20 20 20  n "ERROR".  .   
2b908 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 54     **.      ** T
2b909 68 69 73 20 69 73 20 77 68 61 74 20 77 65 20 64  his is what we d
2b90a 6f 20 69 66 20 74 68 65 20 67 72 61 6d 6d 61 72  o if the grammar
2b90b 20 64 6f 65 73 20 64 65 66 69 6e 65 20 45 52 52   does define ERR
2b90c 4f 52 3a 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20  OR:.      **.   
2b90d 20 20 20 2a 2a 20 20 2a 20 43 61 6c 6c 20 74 68     **  * Call th
2b90e 65 20 25 73 79 6e 74 61 78 5f 65 72 72 6f 72 20  e %syntax_error 
2b90f 66 75 6e 63 74 69 6f 6e 2e 0a 20 20 20 20 20 20  function..      
2b910 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 42  **.      **  * B
2b911 65 67 69 6e 20 70 6f 70 70 69 6e 67 20 74 68 65  egin popping the
2b912 20 73 74 61 63 6b 20 75 6e 74 69 6c 20 77 65 20   stack until we 
2b913 65 6e 74 65 72 20 61 20 73 74 61 74 65 20 77 68  enter a state wh
2b914 65 72 65 0a 20 20 20 20 20 20 2a 2a 20 20 20 20  ere.      **    
2b915 69 74 20 69 73 20 6c 65 67 61 6c 20 74 6f 20 73  it is legal to s
2b916 68 69 66 74 20 74 68 65 20 65 72 72 6f 72 20 73  hift the error s
2b917 79 6d 62 6f 6c 2c 20 74 68 65 6e 20 73 68 69 66  ymbol, then shif
2b918 74 0a 20 20 20 20 20 20 2a 2a 20 20 20 20 74 68  t.      **    th
2b919 65 20 65 72 72 6f 72 20 73 79 6d 62 6f 6c 2e 0a  e error symbol..
2b91a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2b91b 2a 20 20 2a 20 53 65 74 20 74 68 65 20 65 72 72  *  * Set the err
2b91c 6f 72 20 63 6f 75 6e 74 20 74 6f 20 74 68 72 65  or count to thre
2b91d 65 2e 0a 20 20 20 20 20 20 2a 2a 0a 20 20 20 20  e..      **.    
2b91e 20 20 2a 2a 20 20 2a 20 42 65 67 69 6e 20 61 63    **  * Begin ac
2b91f 63 65 70 74 69 6e 67 20 61 6e 64 20 73 68 69 66  cepting and shif
2b920 74 69 6e 67 20 6e 65 77 20 74 6f 6b 65 6e 73 2e  ting new tokens.
2b921 20 20 4e 6f 20 6e 65 77 20 65 72 72 6f 72 0a 20    No new error. 
2b922 20 20 20 20 20 2a 2a 20 20 20 20 70 72 6f 63 65       **    proce
2b923 73 73 69 6e 67 20 77 69 6c 6c 20 6f 63 63 75 72  ssing will occur
2b924 20 75 6e 74 69 6c 20 74 68 72 65 65 20 74 6f 6b   until three tok
2b925 65 6e 73 20 68 61 76 65 20 62 65 65 6e 0a 20 20  ens have been.  
2b926 20 20 20 20 2a 2a 20 20 20 20 73 68 69 66 74 65      **    shifte
2b927 64 20 73 75 63 63 65 73 73 66 75 6c 6c 79 2e 0a  d successfully..
2b928 20 20 20 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a        **.      *
2b929 2f 0a 20 20 20 20 20 20 69 66 28 20 79 79 70 50  /.      if( yypP
2b92a 61 72 73 65 72 2d 3e 79 79 65 72 72 63 6e 74 3c  arser->yyerrcnt<
2b92b 30 20 29 7b 0a 20 20 20 20 20 20 20 20 79 79 5f  0 ){.        yy_
2b92c 73 79 6e 74 61 78 5f 65 72 72 6f 72 28 79 79 70  syntax_error(yyp
2b92d 50 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79  Parser,yymajor,y
2b92e 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2b92f 20 20 20 20 7d 0a 20 20 20 20 20 20 79 79 6d 78      }.      yymx
2b930 20 3d 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79   = yypParser->yy
2b931 73 74 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d  stack[yypParser-
2b932 3e 79 79 69 64 78 5d 2e 6d 61 6a 6f 72 3b 0a 20  >yyidx].major;. 
2b933 20 20 20 20 20 69 66 28 20 79 79 6d 78 3d 3d 59       if( yymx==Y
2b934 59 45 52 52 4f 52 53 59 4d 42 4f 4c 20 7c 7c 20  YERRORSYMBOL || 
2b935 79 79 65 72 72 6f 72 68 69 74 20 29 7b 0a 23 69  yyerrorhit ){.#i
2b936 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20 20 20  fndef NDEBUG.   
2b937 20 20 20 20 20 69 66 28 20 79 79 54 72 61 63 65       if( yyTrace
2b938 46 49 4c 45 20 29 7b 0a 20 20 20 20 20 20 20 20  FILE ){.        
2b939 20 20 66 70 72 69 6e 74 66 28 79 79 54 72 61 63    fprintf(yyTrac
2b93a 65 46 49 4c 45 2c 22 25 73 44 69 73 63 61 72 64  eFILE,"%sDiscard
2b93b 20 69 6e 70 75 74 20 74 6f 6b 65 6e 20 25 73 5c   input token %s\
2b93c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  n",.            
2b93d 20 79 79 54 72 61 63 65 50 72 6f 6d 70 74 2c 79   yyTracePrompt,y
2b93e 79 54 6f 6b 65 6e 4e 61 6d 65 5b 79 79 6d 61 6a  yTokenName[yymaj
2b93f 6f 72 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  or]);.        }.
2b940 23 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 79  #endif.        y
2b941 79 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 70  y_destructor(yyp
2b942 50 61 72 73 65 72 2c 20 79 79 6d 61 6a 6f 72 2c  Parser, yymajor,
2b943 26 79 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a  &yyminorunion);.
2b944 20 20 20 20 20 20 20 20 79 79 6d 61 6a 6f 72 20          yymajor 
2b945 3d 20 59 59 4e 4f 43 4f 44 45 3b 0a 20 20 20 20  = YYNOCODE;.    
2b946 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2b947 20 20 77 68 69 6c 65 28 0a 20 20 20 20 20 20 20    while(.       
2b948 20 20 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79     yypParser->yy
2b949 69 64 78 20 3e 3d 20 30 20 26 26 0a 20 20 20 20  idx >= 0 &&.    
2b94a 20 20 20 20 20 20 79 79 6d 78 20 21 3d 20 59 59        yymx != YY
2b94b 45 52 52 4f 52 53 59 4d 42 4f 4c 20 26 26 0a 20  ERRORSYMBOL &&. 
2b94c 20 20 20 20 20 20 20 20 20 28 79 79 61 63 74 20           (yyact 
2b94d 3d 20 79 79 5f 66 69 6e 64 5f 72 65 64 75 63 65  = yy_find_reduce
2b94e 5f 61 63 74 69 6f 6e 28 0a 20 20 20 20 20 20 20  _action(.       
2b94f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b950 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 73 74   yypParser->yyst
2b951 61 63 6b 5b 79 79 70 50 61 72 73 65 72 2d 3e 79  ack[yypParser->y
2b952 79 69 64 78 5d 2e 73 74 61 74 65 6e 6f 2c 0a 20  yidx].stateno,. 
2b953 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2b954 20 20 20 20 20 20 20 59 59 45 52 52 4f 52 53 59         YYERRORSY
2b955 4d 42 4f 4c 29 29 20 3e 3d 20 59 59 4e 53 54 41  MBOL)) >= YYNSTA
2b956 54 45 0a 20 20 20 20 20 20 20 20 29 7b 0a 20 20  TE.        ){.  
2b957 20 20 20 20 20 20 20 20 79 79 5f 70 6f 70 5f 70          yy_pop_p
2b958 61 72 73 65 72 5f 73 74 61 63 6b 28 79 79 70 50  arser_stack(yypP
2b959 61 72 73 65 72 29 3b 0a 20 20 20 20 20 20 20 20  arser);.        
2b95a 7d 0a 20 20 20 20 20 20 20 20 69 66 28 20 79 79  }.        if( yy
2b95b 70 50 61 72 73 65 72 2d 3e 79 79 69 64 78 20 3c  pParser->yyidx <
2b95c 20 30 20 7c 7c 20 79 79 6d 61 6a 6f 72 3d 3d 30   0 || yymajor==0
2b95d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 79 79   ){.          yy
2b95e 5f 64 65 73 74 72 75 63 74 6f 72 28 79 79 70 50  _destructor(yypP
2b95f 61 72 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 26 79  arser,yymajor,&y
2b960 79 6d 69 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20  yminorunion);.  
2b961 20 20 20 20 20 20 20 20 79 79 5f 70 61 72 73 65          yy_parse
2b962 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72 73 65  _failed(yypParse
2b963 72 29 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79  r);.          yy
2b964 6d 61 6a 6f 72 20 3d 20 59 59 4e 4f 43 4f 44 45  major = YYNOCODE
2b965 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20  ;.        }else 
2b966 69 66 28 20 79 79 6d 78 21 3d 59 59 45 52 52 4f  if( yymx!=YYERRO
2b967 52 53 59 4d 42 4f 4c 20 29 7b 0a 20 20 20 20 20  RSYMBOL ){.     
2b968 20 20 20 20 20 59 59 4d 49 4e 4f 52 54 59 50 45       YYMINORTYPE
2b969 20 75 32 3b 0a 20 20 20 20 20 20 20 20 20 20 75   u2;.          u
2b96a 32 2e 59 59 45 52 52 53 59 4d 44 54 20 3d 20 30  2.YYERRSYMDT = 0
2b96b 3b 0a 20 20 20 20 20 20 20 20 20 20 79 79 5f 73  ;.          yy_s
2b96c 68 69 66 74 28 79 79 70 50 61 72 73 65 72 2c 79  hift(yypParser,y
2b96d 79 61 63 74 2c 59 59 45 52 52 4f 52 53 59 4d 42  yact,YYERRORSYMB
2b96e 4f 4c 2c 26 75 32 29 3b 0a 20 20 20 20 20 20 20  OL,&u2);.       
2b96f 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
2b970 20 79 79 70 50 61 72 73 65 72 2d 3e 79 79 65 72   yypParser->yyer
2b971 72 63 6e 74 20 3d 20 33 3b 0a 20 20 20 20 20 20  rcnt = 3;.      
2b972 79 79 65 72 72 6f 72 68 69 74 20 3d 20 31 3b 0a  yyerrorhit = 1;.
2b973 23 65 6c 73 65 20 20 2f 2a 20 59 59 45 52 52 4f  #else  /* YYERRO
2b974 52 53 59 4d 42 4f 4c 20 69 73 20 6e 6f 74 20 64  RSYMBOL is not d
2b975 65 66 69 6e 65 64 20 2a 2f 0a 20 20 20 20 20 20  efined */.      
2b976 2f 2a 20 54 68 69 73 20 69 73 20 77 68 61 74 20  /* This is what 
2b977 77 65 20 64 6f 20 69 66 20 74 68 65 20 67 72 61  we do if the gra
2b978 6d 6d 61 72 20 64 6f 65 73 20 6e 6f 74 20 64 65  mmar does not de
2b979 66 69 6e 65 20 45 52 52 4f 52 3a 0a 20 20 20 20  fine ERROR:.    
2b97a 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a    **.      **  *
2b97b 20 52 65 70 6f 72 74 20 61 6e 20 65 72 72 6f 72   Report an error
2b97c 20 6d 65 73 73 61 67 65 2c 20 61 6e 64 20 74 68   message, and th
2b97d 72 6f 77 20 61 77 61 79 20 74 68 65 20 69 6e 70  row away the inp
2b97e 75 74 20 74 6f 6b 65 6e 2e 0a 20 20 20 20 20 20  ut token..      
2b97f 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 20 2a 20 49  **.      **  * I
2b980 66 20 74 68 65 20 69 6e 70 75 74 20 74 6f 6b 65  f the input toke
2b981 6e 20 69 73 20 24 2c 20 74 68 65 6e 20 66 61 69  n is $, then fai
2b982 6c 20 74 68 65 20 70 61 72 73 65 2e 0a 20 20 20  l the parse..   
2b983 20 20 20 2a 2a 0a 20 20 20 20 20 20 2a 2a 20 41     **.      ** A
2b984 73 20 62 65 66 6f 72 65 2c 20 73 75 62 73 65 71  s before, subseq
2b985 75 65 6e 74 20 65 72 72 6f 72 20 6d 65 73 73 61  uent error messa
2b986 67 65 73 20 61 72 65 20 73 75 70 70 72 65 73 73  ges are suppress
2b987 65 64 20 75 6e 74 69 6c 0a 20 20 20 20 20 20 2a  ed until.      *
2b988 2a 20 74 68 72 65 65 20 69 6e 70 75 74 20 74 6f  * three input to
2b989 6b 65 6e 73 20 68 61 76 65 20 62 65 65 6e 20 73  kens have been s
2b98a 75 63 63 65 73 73 66 75 6c 6c 79 20 73 68 69 66  uccessfully shif
2b98b 74 65 64 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20  ted..      */.  
2b98c 20 20 20 20 69 66 28 20 79 79 70 50 61 72 73 65      if( yypParse
2b98d 72 2d 3e 79 79 65 72 72 63 6e 74 3c 3d 30 20 29  r->yyerrcnt<=0 )
2b98e 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 73 79 6e  {.        yy_syn
2b98f 74 61 78 5f 65 72 72 6f 72 28 79 79 70 50 61 72  tax_error(yypPar
2b990 73 65 72 2c 79 79 6d 61 6a 6f 72 2c 79 79 6d 69  ser,yymajor,yymi
2b991 6e 6f 72 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20  norunion);.     
2b992 20 7d 0a 20 20 20 20 20 20 79 79 70 50 61 72 73   }.      yypPars
2b993 65 72 2d 3e 79 79 65 72 72 63 6e 74 20 3d 20 33  er->yyerrcnt = 3
2b994 3b 0a 20 20 20 20 20 20 79 79 5f 64 65 73 74 72  ;.      yy_destr
2b995 75 63 74 6f 72 28 79 79 70 50 61 72 73 65 72 2c  uctor(yypParser,
2b996 79 79 6d 61 6a 6f 72 2c 26 79 79 6d 69 6e 6f 72  yymajor,&yyminor
2b997 75 6e 69 6f 6e 29 3b 0a 20 20 20 20 20 20 69 66  union);.      if
2b998 28 20 79 79 65 6e 64 6f 66 69 6e 70 75 74 20 29  ( yyendofinput )
2b999 7b 0a 20 20 20 20 20 20 20 20 79 79 5f 70 61 72  {.        yy_par
2b99a 73 65 5f 66 61 69 6c 65 64 28 79 79 70 50 61 72  se_failed(yypPar
2b99b 73 65 72 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ser);.      }.  
2b99c 20 20 20 20 79 79 6d 61 6a 6f 72 20 3d 20 59 59      yymajor = YY
2b99d 4e 4f 43 4f 44 45 3b 0a 23 65 6e 64 69 66 0a 20  NOCODE;.#endif. 
2b99e 20 20 20 7d 0a 20 20 7d 77 68 69 6c 65 28 20 79     }.  }while( y
2b99f 79 6d 61 6a 6f 72 21 3d 59 59 4e 4f 43 4f 44 45  ymajor!=YYNOCODE
2b9a0 20 26 26 20 79 79 70 50 61 72 73 65 72 2d 3e 79   && yypParser->y
2b9a1 79 69 64 78 3e 3d 30 20 29 3b 0a 20 20 72 65 74  yidx>=0 );.  ret
2b9a2 75 72 6e 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  urn;.}../*******
2b9a3 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 70  ******* End of p
2b9a4 61 72 73 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  arse.c *********
2b9a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9a6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9a7 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a  ******/./*******
2b9a8 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69  ******* Begin fi
2b9a9 6c 65 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  le tokenize.c **
2b9aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9ab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9ac 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30  ******/./*.** 20
2b9ad 30 31 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a  01 September 15.
2b9ae 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
2b9af 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
2b9b0 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
2b9b1 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
2b9b2 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
2b9b3 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
2b9b4 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
2b9b5 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
2b9b6 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
2b9b7 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
2b9b8 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
2b9b9 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
2b9ba 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
2b9bb 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
2b9bc 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
2b9bd 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
2b9be 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
2b9bf 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
2b9c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9c1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9c2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2b9c3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a  **************.*
2b9c4 2a 20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66  * An tokenizer f
2b9c5 6f 72 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69  or SQL.**.** Thi
2b9c6 73 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20  s file contains 
2b9c7 43 20 63 6f 64 65 20 74 68 61 74 20 73 70 6c 69  C code that spli
2b9c8 74 73 20 61 6e 20 53 51 4c 20 69 6e 70 75 74 20  ts an SQL input 
2b9c9 73 74 72 69 6e 67 20 75 70 20 69 6e 74 6f 0a 2a  string up into.*
2b9ca 2a 20 69 6e 64 69 76 69 64 75 61 6c 20 74 6f 6b  * individual tok
2b9cb 65 6e 73 20 61 6e 64 20 73 65 6e 64 73 20 74 68  ens and sends th
2b9cc 6f 73 65 20 74 6f 6b 65 6e 73 20 6f 6e 65 2d 62  ose tokens one-b
2b9cd 79 2d 6f 6e 65 20 6f 76 65 72 20 74 6f 20 74 68  y-one over to th
2b9ce 65 0a 2a 2a 20 70 61 72 73 65 72 20 66 6f 72 20  e.** parser for 
2b9cf 61 6e 61 6c 79 73 69 73 2e 0a 2a 2a 0a 2a 2a 20  analysis..**.** 
2b9d0 24 49 64 3a 20 74 6f 6b 65 6e 69 7a 65 2e 63 2c  $Id: tokenize.c,
2b9d1 76 20 31 2e 31 35 32 20 32 30 30 38 2f 30 39 2f  v 1.152 2008/09/
2b9d2 30 31 20 31 35 3a 35 32 3a 31 31 20 64 72 68 20  01 15:52:11 drh 
2b9d3 45 78 70 20 24 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  Exp $.*/../*.** 
2b9d4 54 68 65 20 63 68 61 72 4d 61 70 28 29 20 6d 61  The charMap() ma
2b9d5 63 72 6f 20 6d 61 70 73 20 61 6c 70 68 61 62 65  cro maps alphabe
2b9d6 74 69 63 20 63 68 61 72 61 63 74 65 72 73 20 69  tic characters i
2b9d7 6e 74 6f 20 74 68 65 69 72 0a 2a 2a 20 6c 6f 77  nto their.** low
2b9d8 65 72 2d 63 61 73 65 20 41 53 43 49 49 20 65 71  er-case ASCII eq
2b9d9 75 69 76 61 6c 65 6e 74 2e 20 20 4f 6e 20 41 53  uivalent.  On AS
2b9da 43 49 49 20 6d 61 63 68 69 6e 65 73 2c 20 74 68  CII machines, th
2b9db 69 73 20 69 73 20 6a 75 73 74 0a 2a 2a 20 61 6e  is is just.** an
2b9dc 20 75 70 70 65 72 2d 74 6f 2d 6c 6f 77 65 72 20   upper-to-lower 
2b9dd 63 61 73 65 20 6d 61 70 2e 20 20 4f 6e 20 45 42  case map.  On EB
2b9de 43 44 49 43 20 6d 61 63 68 69 6e 65 73 20 77 65  CDIC machines we
2b9df 20 61 6c 73 6f 20 6e 65 65 64 0a 2a 2a 20 74 6f   also need.** to
2b9e0 20 61 64 6a 75 73 74 20 74 68 65 20 65 6e 63 6f   adjust the enco
2b9e1 64 69 6e 67 2e 20 20 4f 6e 6c 79 20 61 6c 70 68  ding.  Only alph
2b9e2 61 62 65 74 69 63 20 63 68 61 72 61 63 74 65 72  abetic character
2b9e3 73 20 61 6e 64 20 75 6e 64 65 72 73 63 6f 72 65  s and underscore
2b9e4 73 0a 2a 2a 20 6e 65 65 64 20 74 6f 20 62 65 20  s.** need to be 
2b9e5 74 72 61 6e 73 6c 61 74 65 64 2e 0a 2a 2f 0a 23  translated..*/.#
2b9e6 69 66 64 65 66 20 53 51 4c 49 54 45 5f 41 53 43  ifdef SQLITE_ASC
2b9e7 49 49 0a 23 20 64 65 66 69 6e 65 20 63 68 61 72  II.# define char
2b9e8 4d 61 70 28 58 29 20 73 71 6c 69 74 65 33 55 70  Map(X) sqlite3Up
2b9e9 70 65 72 54 6f 4c 6f 77 65 72 5b 28 75 6e 73 69  perToLower[(unsi
2b9ea 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 23 65 6e  gned char)X].#en
2b9eb 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54  dif.#ifdef SQLIT
2b9ec 45 5f 45 42 43 44 49 43 0a 23 20 64 65 66 69 6e  E_EBCDIC.# defin
2b9ed 65 20 63 68 61 72 4d 61 70 28 58 29 20 65 62 63  e charMap(X) ebc
2b9ee 64 69 63 54 6f 41 73 63 69 69 5b 28 75 6e 73 69  dicToAscii[(unsi
2b9ef 67 6e 65 64 20 63 68 61 72 29 58 5d 0a 63 6f 6e  gned char)X].con
2b9f0 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2b9f1 20 65 62 63 64 69 63 54 6f 41 73 63 69 69 5b 5d   ebcdicToAscii[]
2b9f2 20 3d 20 7b 0a 2f 2a 20 30 20 20 20 31 20 20 20   = {./* 0   1   
2b9f3 32 20 20 20 33 20 20 20 34 20 20 20 35 20 20 20  2   3   4   5   
2b9f4 36 20 20 20 37 20 20 20 38 20 20 20 39 20 20 20  6   7   8   9   
2b9f5 41 20 20 20 42 20 20 20 43 20 20 20 44 20 20 20  A   B   C   D   
2b9f6 45 20 20 20 46 20 2a 2f 0a 20 20 20 30 2c 20 20  E   F */.   0,  
2b9f7 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2b9f8 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2b9f9 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2b9fa 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 30  0,  0,  0,  /* 0
2b9fb 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
2b9fc 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2b9fd 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2b9fe 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2b9ff 30 2c 20 20 30 2c 20 20 2f 2a 20 31 78 20 2a 2f  0,  0,  /* 1x */
2ba00 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
2ba01 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba02 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba03 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba04 30 2c 20 20 2f 2a 20 32 78 20 2a 2f 0a 20 20 20  0,  /* 2x */.   
2ba05 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba06 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba07 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba08 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba09 2f 2a 20 33 78 20 2a 2f 0a 20 20 20 30 2c 20 20  /* 3x */.   0,  
2ba0a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba0b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba0c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba0d 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 34  0,  0,  0,  /* 4
2ba0e 78 20 2a 2f 0a 20 20 20 30 2c 20 20 30 2c 20 20  x */.   0,  0,  
2ba0f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba10 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba11 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba12 30 2c 20 20 30 2c 20 20 2f 2a 20 35 78 20 2a 2f  0,  0,  /* 5x */
2ba13 0a 20 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  .   0,  0,  0,  
2ba14 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba15 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba16 30 2c 20 20 30 2c 20 39 35 2c 20 20 30 2c 20 20  0,  0, 95,  0,  
2ba17 30 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20 20  0,  /* 6x */.   
2ba18 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba19 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba1a 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba1b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba1c 2f 2a 20 37 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* 7x */.   0, 9
2ba1d 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
2ba1e 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
2ba1f 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
2ba20 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 38  0,  0,  0,  /* 8
2ba21 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
2ba22 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
2ba23 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
2ba24 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba25 30 2c 20 20 30 2c 20 20 2f 2a 20 39 78 20 2a 2f  0,  0,  /* 9x */
2ba26 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
2ba27 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
2ba28 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
2ba29 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba2a 30 2c 20 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20  0,  /* Ax */.   
2ba2b 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba2c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba2d 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba2e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba2f 2f 2a 20 42 78 20 2a 2f 0a 20 20 20 30 2c 20 39  /* Bx */.   0, 9
2ba30 37 2c 20 39 38 2c 20 39 39 2c 31 30 30 2c 31 30  7, 98, 99,100,10
2ba31 31 2c 31 30 32 2c 31 30 33 2c 31 30 34 2c 31 30  1,102,103,104,10
2ba32 35 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  5,  0,  0,  0,  
2ba33 30 2c 20 20 30 2c 20 20 30 2c 20 20 2f 2a 20 43  0,  0,  0,  /* C
2ba34 78 20 2a 2f 0a 20 20 20 30 2c 31 30 36 2c 31 30  x */.   0,106,10
2ba35 37 2c 31 30 38 2c 31 30 39 2c 31 31 30 2c 31 31  7,108,109,110,11
2ba36 31 2c 31 31 32 2c 31 31 33 2c 31 31 34 2c 20 20  1,112,113,114,  
2ba37 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba38 30 2c 20 20 30 2c 20 20 2f 2a 20 44 78 20 2a 2f  0,  0,  /* Dx */
2ba39 0a 20 20 20 30 2c 20 20 30 2c 31 31 35 2c 31 31  .   0,  0,115,11
2ba3a 36 2c 31 31 37 2c 31 31 38 2c 31 31 39 2c 31 32  6,117,118,119,12
2ba3b 30 2c 31 32 31 2c 31 32 32 2c 20 20 30 2c 20 20  0,121,122,  0,  
2ba3c 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba3d 30 2c 20 20 2f 2a 20 45 78 20 2a 2f 0a 20 20 20  0,  /* Ex */.   
2ba3e 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba3f 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba40 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba41 30 2c 20 20 30 2c 20 20 30 2c 20 20 30 2c 20 20  0,  0,  0,  0,  
2ba42 2f 2a 20 46 78 20 2a 2f 0a 7d 3b 0a 23 65 6e 64  /* Fx */.};.#end
2ba43 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 71  if../*.** The sq
2ba44 6c 69 74 65 33 4b 65 79 77 6f 72 64 43 6f 64 65  lite3KeywordCode
2ba45 20 66 75 6e 63 74 69 6f 6e 20 6c 6f 6f 6b 73 20   function looks 
2ba46 75 70 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  up an identifier
2ba47 20 74 6f 20 64 65 74 65 72 6d 69 6e 65 20 69 66   to determine if
2ba48 0a 2a 2a 20 69 74 20 69 73 20 61 20 6b 65 79 77  .** it is a keyw
2ba49 6f 72 64 2e 20 20 49 66 20 69 74 20 69 73 20 61  ord.  If it is a
2ba4a 20 6b 65 79 77 6f 72 64 2c 20 74 68 65 20 74 6f   keyword, the to
2ba4b 6b 65 6e 20 63 6f 64 65 20 6f 66 20 74 68 61 74  ken code of that
2ba4c 20 6b 65 79 77 6f 72 64 20 69 73 20 0a 2a 2a 20   keyword is .** 
2ba4d 72 65 74 75 72 6e 65 64 2e 20 20 49 66 20 74 68  returned.  If th
2ba4e 65 20 69 6e 70 75 74 20 69 73 20 6e 6f 74 20 61  e input is not a
2ba4f 20 6b 65 79 77 6f 72 64 2c 20 54 4b 5f 49 44 20   keyword, TK_ID 
2ba50 69 73 20 72 65 74 75 72 6e 65 64 2e 0a 2a 2a 0a  is returned..**.
2ba51 2a 2a 20 54 68 65 20 69 6d 70 6c 65 6d 65 6e 74  ** The implement
2ba52 61 74 69 6f 6e 20 6f 66 20 74 68 69 73 20 72 6f  ation of this ro
2ba53 75 74 69 6e 65 20 77 61 73 20 67 65 6e 65 72 61  utine was genera
2ba54 74 65 64 20 62 79 20 61 20 70 72 6f 67 72 61 6d  ted by a program
2ba55 2c 0a 2a 2a 20 6d 6b 6b 65 79 77 6f 72 64 68 61  ,.** mkkeywordha
2ba56 73 68 2e 68 2c 20 6c 6f 63 61 74 65 64 20 69 6e  sh.h, located in
2ba57 20 74 68 65 20 74 6f 6f 6c 20 73 75 62 64 69 72   the tool subdir
2ba58 65 63 74 6f 72 79 20 6f 66 20 74 68 65 20 64 69  ectory of the di
2ba59 73 74 72 69 62 75 74 69 6f 6e 2e 0a 2a 2a 20 54  stribution..** T
2ba5a 68 65 20 6f 75 74 70 75 74 20 6f 66 20 74 68 65  he output of the
2ba5b 20 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63   mkkeywordhash.c
2ba5c 20 70 72 6f 67 72 61 6d 20 69 73 20 77 72 69 74   program is writ
2ba5d 74 65 6e 20 69 6e 74 6f 20 61 20 66 69 6c 65 0a  ten into a file.
2ba5e 2a 2a 20 6e 61 6d 65 64 20 6b 65 79 77 6f 72 64  ** named keyword
2ba5f 68 61 73 68 2e 68 20 61 6e 64 20 74 68 65 6e 20  hash.h and then 
2ba60 69 6e 63 6c 75 64 65 64 20 69 6e 74 6f 20 74 68  included into th
2ba61 69 73 20 73 6f 75 72 63 65 20 66 69 6c 65 20 62  is source file b
2ba62 79 0a 2a 2a 20 74 68 65 20 23 69 6e 63 6c 75 64  y.** the #includ
2ba63 65 20 62 65 6c 6f 77 2e 0a 2a 2f 0a 2f 2a 2a 2a  e below..*/./***
2ba64 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
2ba65 75 64 65 20 6b 65 79 77 6f 72 64 68 61 73 68 2e  ude keywordhash.
2ba66 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
2ba67 6f 66 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  of tokenize.c **
2ba68 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2ba69 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2ba6a 6e 20 66 69 6c 65 20 6b 65 79 77 6f 72 64 68 61  n file keywordha
2ba6b 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.h ***********
2ba6c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2ba6d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2ba6e 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63 6f 6e  ** This file con
2ba6f 74 61 69 6e 73 20 61 75 74 6f 6d 61 74 69 63 61  tains automatica
2ba70 6c 6c 79 20 67 65 6e 65 72 61 74 65 64 20 63 6f  lly generated co
2ba71 64 65 20 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  de ******.**.** 
2ba72 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73  The code in this
2ba73 20 66 69 6c 65 20 68 61 73 20 62 65 65 6e 20 61   file has been a
2ba74 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 67 65 6e  utomatically gen
2ba75 65 72 61 74 65 64 20 62 79 0a 2a 2a 0a 2a 2a 20  erated by.**.** 
2ba76 20 20 20 20 24 48 65 61 64 65 72 3a 20 2f 73 71      $Header: /sq
2ba77 6c 69 74 65 2f 73 71 6c 69 74 65 2f 74 6f 6f 6c  lite/sqlite/tool
2ba78 2f 6d 6b 6b 65 79 77 6f 72 64 68 61 73 68 2e 63  /mkkeywordhash.c
2ba79 2c 76 20 31 2e 33 32 20 32 30 30 38 2f 31 30 2f  ,v 1.32 2008/10/
2ba7a 30 36 20 30 35 3a 33 32 3a 31 39 20 64 61 6e 69  06 05:32:19 dani
2ba7b 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a 2a  elk1977 Exp $.**
2ba7c 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20  .** The code in 
2ba7d 74 68 69 73 20 66 69 6c 65 20 69 6d 70 6c 65 6d  this file implem
2ba7e 65 6e 74 73 20 61 20 66 75 6e 63 74 69 6f 6e 20  ents a function 
2ba7f 74 68 61 74 20 64 65 74 65 72 6d 69 6e 65 73 20  that determines 
2ba80 77 68 65 74 68 65 72 0a 2a 2a 20 6f 72 20 6e 6f  whether.** or no
2ba81 74 20 61 20 67 69 76 65 6e 20 69 64 65 6e 74 69  t a given identi
2ba82 66 69 65 72 20 69 73 20 72 65 61 6c 6c 79 20 61  fier is really a
2ba83 6e 20 53 51 4c 20 6b 65 79 77 6f 72 64 2e 20 20  n SQL keyword.  
2ba84 54 68 65 20 73 61 6d 65 20 74 68 69 6e 67 0a 2a  The same thing.*
2ba85 2a 20 6d 69 67 68 74 20 62 65 20 69 6d 70 6c 65  * might be imple
2ba86 6d 65 6e 74 65 64 20 6d 6f 72 65 20 64 69 72 65  mented more dire
2ba87 63 74 6c 79 20 75 73 69 6e 67 20 61 20 68 61 6e  ctly using a han
2ba88 64 2d 77 72 69 74 74 65 6e 20 68 61 73 68 20 74  d-written hash t
2ba89 61 62 6c 65 2e 0a 2a 2a 20 42 75 74 20 62 79 20  able..** But by 
2ba8a 75 73 69 6e 67 20 74 68 69 73 20 61 75 74 6f 6d  using this autom
2ba8b 61 74 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  atically generat
2ba8c 65 64 20 63 6f 64 65 2c 20 74 68 65 20 73 69 7a  ed code, the siz
2ba8d 65 20 6f 66 20 74 68 65 20 63 6f 64 65 0a 2a 2a  e of the code.**
2ba8e 20 69 73 20 73 75 62 73 74 61 6e 74 69 61 6c 6c   is substantiall
2ba8f 79 20 72 65 64 75 63 65 64 2e 20 20 54 68 69 73  y reduced.  This
2ba90 20 69 73 20 69 6d 70 6f 72 74 61 6e 74 20 66 6f   is important fo
2ba91 72 20 65 6d 62 65 64 64 65 64 20 61 70 70 6c 69  r embedded appli
2ba92 63 61 74 69 6f 6e 73 0a 2a 2a 20 6f 6e 20 70 6c  cations.** on pl
2ba93 61 74 66 6f 72 6d 73 20 77 69 74 68 20 6c 69 6d  atforms with lim
2ba94 69 74 65 64 20 6d 65 6d 6f 72 79 2e 0a 2a 2f 0a  ited memory..*/.
2ba95 2f 2a 20 48 61 73 68 20 73 63 6f 72 65 3a 20 31  /* Hash score: 1
2ba96 36 37 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  67 */.static int
2ba97 20 6b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e   keywordCode(con
2ba98 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20  st char *z, int 
2ba99 6e 29 7b 0a 20 20 2f 2a 20 7a 54 65 78 74 5b 5d  n){.  /* zText[]
2ba9a 20 65 6e 63 6f 64 65 73 20 37 38 33 20 62 79 74   encodes 783 byt
2ba9b 65 73 20 6f 66 20 6b 65 79 77 6f 72 64 73 20 69  es of keywords i
2ba9c 6e 20 35 32 38 20 62 79 74 65 73 20 2a 2f 0a 20  n 528 bytes */. 
2ba9d 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68   static const ch
2ba9e 61 72 20 7a 54 65 78 74 5b 35 32 38 5d 20 3d 0a  ar zText[528] =.
2ba9f 20 20 20 20 22 52 45 49 4e 44 45 58 45 44 45 53      "REINDEXEDES
2baa0 43 41 50 45 41 43 48 45 43 4b 45 59 42 45 46 4f  CAPEACHECKEYBEFO
2baa1 52 45 49 47 4e 4f 52 45 47 45 58 50 4c 41 49 4e  REIGNOREGEXPLAIN
2baa2 53 54 45 41 44 44 41 54 41 42 41 53 45 4c 45 43  STEADDATABASELEC
2baa3 54 41 42 4c 45 22 0a 20 20 20 20 22 46 54 48 45  TABLE".    "FTHE
2baa4 4e 44 45 46 45 52 52 41 42 4c 45 4c 53 45 58 43  NDEFERRABLELSEXC
2baa5 45 50 54 52 41 4e 53 41 43 54 49 4f 4e 41 54 55  EPTRANSACTIONATU
2baa6 52 41 4c 54 45 52 41 49 53 45 58 43 4c 55 53 49  RALTERAISEXCLUSI
2baa7 56 45 58 49 53 54 53 43 4f 4e 53 54 52 41 49 4e  VEXISTSCONSTRAIN
2baa8 54 22 0a 20 20 20 20 22 45 52 53 45 43 54 52 49  T".    "ERSECTRI
2baa9 47 47 45 52 45 46 45 52 45 4e 43 45 53 55 4e 49  GGEREFERENCESUNI
2baaa 51 55 45 52 59 41 54 54 41 43 48 41 56 49 4e 47  QUERYATTACHAVING
2baab 52 4f 55 50 44 41 54 45 4d 50 4f 52 41 52 59 42  ROUPDATEMPORARYB
2baac 45 47 49 4e 4e 45 52 22 0a 20 20 20 20 22 45 4e  EGINNER".    "EN
2baad 41 4d 45 42 45 54 57 45 45 4e 4f 54 4e 55 4c 4c  AMEBETWEENOTNULL
2baae 49 4b 45 43 41 53 43 41 44 45 4c 45 54 45 43 41  IKECASCADELETECA
2baaf 53 45 43 4f 4c 4c 41 54 45 43 52 45 41 54 45 43  SECOLLATECREATEC
2bab0 55 52 52 45 4e 54 5f 44 41 54 45 22 0a 20 20 20  URRENT_DATE".   
2bab1 20 22 44 45 54 41 43 48 49 4d 4d 45 44 49 41 54   "DETACHIMMEDIAT
2bab2 45 4a 4f 49 4e 53 45 52 54 4d 41 54 43 48 50 4c  EJOINSERTMATCHPL
2bab3 41 4e 41 4c 59 5a 45 50 52 41 47 4d 41 42 4f 52  ANALYZEPRAGMABOR
2bab4 54 56 41 4c 55 45 53 56 49 52 54 55 41 4c 49 4d  TVALUESVIRTUALIM
2bab5 49 54 22 0a 20 20 20 20 22 57 48 45 4e 57 48 45  IT".    "WHENWHE
2bab6 52 45 50 4c 41 43 45 41 46 54 45 52 45 53 54 52  REPLACEAFTERESTR
2bab7 49 43 54 41 4e 44 45 46 41 55 4c 54 41 55 54 4f  ICTANDEFAULTAUTO
2bab8 49 4e 43 52 45 4d 45 4e 54 43 41 53 54 43 4f 4c  INCREMENTCASTCOL
2bab9 55 4d 4e 43 4f 4d 4d 49 54 22 0a 20 20 20 20 22  UMNCOMMIT".    "
2baba 43 4f 4e 46 4c 49 43 54 43 52 4f 53 53 43 55 52  CONFLICTCROSSCUR
2babb 52 45 4e 54 5f 54 49 4d 45 53 54 41 4d 50 52 49  RENT_TIMESTAMPRI
2babc 4d 41 52 59 44 45 46 45 52 52 45 44 49 53 54 49  MARYDEFERREDISTI
2babd 4e 43 54 44 52 4f 50 46 41 49 4c 46 52 4f 4d 22  NCTDROPFAILFROM"
2babe 0a 20 20 20 20 22 46 55 4c 4c 47 4c 4f 42 59 49  .    "FULLGLOBYI
2babf 46 49 4e 54 4f 46 46 53 45 54 49 53 4e 55 4c 4c  FINTOFFSETISNULL
2bac0 4f 52 44 45 52 49 47 48 54 4f 55 54 45 52 4f 4c  ORDERIGHTOUTEROL
2bac1 4c 42 41 43 4b 52 4f 57 55 4e 49 4f 4e 55 53 49  LBACKROWUNIONUSI
2bac2 4e 47 56 41 43 55 55 4d 22 0a 20 20 20 20 22 56  NGVACUUM".    "V
2bac3 49 45 57 49 4e 49 54 49 41 4c 4c 59 22 3b 0a 20  IEWINITIALLY";. 
2bac4 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e   static const un
2bac5 73 69 67 6e 65 64 20 63 68 61 72 20 61 48 61 73  signed char aHas
2bac6 68 5b 31 32 37 5d 20 3d 20 7b 0a 20 20 20 20 20  h[127] = {.     
2bac7 20 36 35 2c 20 20 39 34 2c 20 31 31 30 2c 20 20   65,  94, 110,  
2bac8 36 33 2c 20 20 20 30 2c 20 20 34 34 2c 20 20 20  63,   0,  44,   
2bac9 30 2c 20 20 20 30 2c 20 20 37 31 2c 20 20 20 30  0,   0,  71,   0
2baca 2c 20 20 36 36 2c 20 20 20 30 2c 20 20 20 30 2c  ,  66,   0,   0,
2bacb 0a 20 20 20 20 20 31 30 34 2c 20 20 31 32 2c 20  .     104,  12, 
2bacc 20 36 37 2c 20 20 31 35 2c 20 20 20 30 2c 20 31   67,  15,   0, 1
2bacd 30 38 2c 20 20 37 34 2c 20 31 30 35 2c 20 31 30  08,  74, 105, 10
2bace 31 2c 20 20 20 30 2c 20 20 31 39 2c 20 20 20 30  1,   0,  19,   0
2bacf 2c 20 20 20 30 2c 0a 20 20 20 20 20 31 31 34 2c  ,   0,.     114,
2bad0 20 20 20 30 2c 20 31 31 32 2c 20 20 37 38 2c 20     0, 112,  78, 
2bad1 20 20 30 2c 20 20 32 32 2c 20 20 38 32 2c 20 20    0,  22,  82,  
2bad2 20 30 2c 20 20 20 39 2c 20 20 20 30 2c 20 20 20   0,   9,   0,   
2bad3 30 2c 20 20 35 39 2c 20 20 36 30 2c 0a 20 20 20  0,  59,  60,.   
2bad4 20 20 20 20 30 2c 20 20 35 38 2c 20 20 20 36 2c      0,  58,   6,
2bad5 20 20 20 30 2c 20 20 33 39 2c 20 20 37 39 2c 20     0,  39,  79, 
2bad6 20 39 31 2c 20 20 20 30 2c 20 31 31 31 2c 20 20   91,   0, 111,  
2bad7 39 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 34  90,   0,   0,  4
2bad8 35 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 39 32  5,.       0,  92
2bad9 2c 20 20 32 34 2c 20 20 20 30 2c 20 20 31 37 2c  ,  24,   0,  17,
2bada 20 20 20 30 2c 20 31 31 35 2c 20 20 34 30 2c 20     0, 115,  40, 
2badb 20 32 33 2c 20 20 20 30 2c 20 20 20 35 2c 20 20   23,   0,   5,  
2badc 39 39 2c 20 20 32 35 2c 0a 20 20 20 20 20 20 38  99,  25,.      8
2badd 35 2c 20 20 20 30 2c 20 20 20 30 2c 20 31 31 37  5,   0,   0, 117
2bade 2c 20 20 39 35 2c 20 20 35 30 2c 20 31 31 36 2c  ,  95,  50, 116,
2badf 20 20 34 37 2c 20 20 20 37 2c 20 20 34 32 2c 20    47,   7,  42, 
2bae0 20 20 30 2c 20 20 38 30 2c 20 20 20 30 2c 0a 20    0,  80,   0,. 
2bae1 20 20 20 20 20 38 39 2c 20 20 32 36 2c 20 20 20       89,  26,   
2bae2 30 2c 20 20 38 38 2c 20 20 20 30 2c 20 20 20 30  0,  88,   0,   0
2bae3 2c 20 20 20 30 2c 20 20 38 34 2c 20 20 38 31 2c  ,   0,  84,  81,
2bae4 20 20 38 36 2c 20 20 37 37 2c 20 20 39 38 2c 20    86,  77,  98, 
2bae5 20 31 34 2c 0a 20 20 20 20 20 20 33 34 2c 20 20   14,.      34,  
2bae6 39 37 2c 20 20 20 30 2c 20 20 37 30 2c 20 20 20  97,   0,  70,   
2bae7 30 2c 20 20 31 38 2c 20 20 37 36 2c 20 31 30 30  0,  18,  76, 100
2bae8 2c 20 20 33 31 2c 20 20 20 30 2c 20 31 31 33 2c  ,  31,   0, 113,
2bae9 20 20 36 39 2c 20 31 30 36 2c 0a 20 20 20 20 20    69, 106,.     
2baea 20 35 32 2c 20 20 34 36 2c 20 20 37 33 2c 20 20   52,  46,  73,  
2baeb 20 30 2c 20 20 20 30 2c 20 20 38 33 2c 20 31 30   0,   0,  83, 10
2baec 32 2c 20 20 20 30 2c 20 31 30 39 2c 20 20 20 30  2,   0, 109,   0
2baed 2c 20 20 33 35 2c 20 20 20 30 2c 20 20 20 30 2c  ,  35,   0,   0,
2baee 0a 20 20 20 20 20 20 32 38 2c 20 20 20 30 2c 20  .      28,   0, 
2baef 20 37 35 2c 20 20 34 38 2c 20 20 35 33 2c 20 20   75,  48,  53,  
2baf0 20 30 2c 20 20 32 30 2c 20 20 35 31 2c 20 20 20   0,  20,  51,   
2baf1 30 2c 20 20 34 33 2c 0a 20 20 7d 3b 0a 20 20 73  0,  43,.  };.  s
2baf2 74 61 74 69 63 20 63 6f 6e 73 74 20 75 6e 73 69  tatic const unsi
2baf3 67 6e 65 64 20 63 68 61 72 20 61 4e 65 78 74 5b  gned char aNext[
2baf4 31 31 37 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20  117] = {.       
2baf5 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2baf6 2c 20 20 20 34 2c 20 20 20 30 2c 20 20 20 30 2c  ,   4,   0,   0,
2baf7 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2baf8 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20    0,   0,   0,. 
2baf9 20 20 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20        0,   2,   
2bafa 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2bafb 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 31 33 2c  ,   0,   0,  13,
2bafc 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2bafd 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c 20 20    0,.       0,  
2bafe 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2baff 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  0,   0,   0,   0
2bb00 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c  ,   0,   0,   0,
2bb01 20 20 20 30 2c 20 20 20 30 2c 0a 20 20 20 20 20     0,   0,.     
2bb02 20 20 30 2c 20 20 20 30 2c 20 20 20 33 2c 20 20    0,   0,   3,  
2bb03 33 38 2c 20 20 20 30 2c 20 20 33 32 2c 20 20 32  38,   0,  32,  2
2bb04 31 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30  1,   0,   0,   0
2bb05 2c 20 20 20 30 2c 20 20 32 39 2c 20 20 20 30 2c  ,   0,  29,   0,
2bb06 0a 20 20 20 20 20 20 20 30 2c 20 20 33 37 2c 20  .       0,  37, 
2bb07 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2bb08 20 31 2c 20 20 35 35 2c 20 20 20 30 2c 20 20 20   1,  55,   0,   
2bb09 30 2c 20 20 35 36 2c 20 20 20 30 2c 20 20 20 30  0,  56,   0,   0
2bb0a 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 20 30 2c  ,   0,.       0,
2bb0b 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2bb0c 20 20 30 2c 20 20 20 30 2c 20 20 35 34 2c 20 20    0,   0,  54,  
2bb0d 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20 20   0,   0,   0,   
2bb0e 30 2c 20 20 33 30 2c 20 20 20 30 2c 0a 20 20 20  0,  30,   0,.   
2bb0f 20 20 20 31 36 2c 20 20 33 33 2c 20 20 31 30 2c     16,  33,  10,
2bb10 20 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20     0,   0,   0, 
2bb11 20 20 30 2c 20 20 20 30 2c 20 20 20 30 2c 20 20    0,   0,   0,  
2bb12 20 30 2c 20 20 31 31 2c 20 20 36 31 2c 20 20 36   0,  11,  61,  6
2bb13 38 2c 0a 20 20 20 20 20 20 20 30 2c 20 20 20 38  8,.       0,   8
2bb14 2c 20 20 20 30 2c 20 20 39 33 2c 20 20 38 37 2c  ,   0,  93,  87,
2bb15 20 20 20 30 2c 20 20 39 36 2c 20 20 20 30 2c 20     0,  96,   0, 
2bb16 20 34 39 2c 20 20 20 30 2c 20 20 20 30 2c 20 20   49,   0,   0,  
2bb17 36 34 2c 20 20 20 30 2c 0a 20 20 20 20 20 20 34  64,   0,.      4
2bb18 31 2c 20 31 30 33 2c 20 20 20 30 2c 20 20 32 37  1, 103,   0,  27
2bb19 2c 20 31 30 37 2c 20 20 33 36 2c 20 20 36 32 2c  , 107,  36,  62,
2bb1a 20 20 37 32 2c 20 20 20 30 2c 20 20 20 30 2c 20    72,   0,   0, 
2bb1b 20 35 37 2c 20 20 20 30 2c 20 20 20 30 2c 0a 20   57,   0,   0,. 
2bb1c 20 7d 3b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e   };.  static con
2bb1d 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2bb1e 20 61 4c 65 6e 5b 31 31 37 5d 20 3d 20 7b 0a 20   aLen[117] = {. 
2bb1f 20 20 20 20 20 20 37 2c 20 20 20 37 2c 20 20 20        7,   7,   
2bb20 35 2c 20 20 20 34 2c 20 20 20 36 2c 20 20 20 34  5,   4,   6,   4
2bb21 2c 20 20 20 35 2c 20 20 20 33 2c 20 20 20 36 2c  ,   5,   3,   6,
2bb22 20 20 20 37 2c 20 20 20 33 2c 20 20 20 36 2c 20     7,   3,   6, 
2bb23 20 20 36 2c 0a 20 20 20 20 20 20 20 37 2c 20 20    6,.       7,  
2bb24 20 37 2c 20 20 20 33 2c 20 20 20 38 2c 20 20 20   7,   3,   8,   
2bb25 32 2c 20 20 20 36 2c 20 20 20 35 2c 20 20 20 34  2,   6,   5,   4
2bb26 2c 20 20 20 34 2c 20 20 20 33 2c 20 20 31 30 2c  ,   4,   3,  10,
2bb27 20 20 20 34 2c 20 20 20 36 2c 0a 20 20 20 20 20     4,   6,.     
2bb28 20 31 31 2c 20 20 20 32 2c 20 20 20 37 2c 20 20   11,   2,   7,  
2bb29 20 35 2c 20 20 20 35 2c 20 20 20 39 2c 20 20 20   5,   5,   9,   
2bb2a 36 2c 20 20 31 30 2c 20 20 20 39 2c 20 20 20 37  6,  10,   9,   7
2bb2b 2c 20 20 31 30 2c 20 20 20 36 2c 20 20 20 35 2c  ,  10,   6,   5,
2bb2c 0a 20 20 20 20 20 20 20 36 2c 20 20 20 36 2c 20  .       6,   6, 
2bb2d 20 20 35 2c 20 20 20 36 2c 20 20 20 34 2c 20 20    5,   6,   4,  
2bb2e 20 39 2c 20 20 20 32 2c 20 20 20 35 2c 20 20 20   9,   2,   5,   
2bb2f 35 2c 20 20 20 36 2c 20 20 20 37 2c 20 20 20 37  5,   6,   7,   7
2bb30 2c 20 20 20 33 2c 0a 20 20 20 20 20 20 20 34 2c  ,   3,.       4,
2bb31 20 20 20 34 2c 20 20 20 37 2c 20 20 20 33 2c 20     4,   7,   3, 
2bb32 20 20 36 2c 20 20 20 34 2c 20 20 20 37 2c 20 20    6,   4,   7,  
2bb33 20 36 2c 20 20 31 32 2c 20 20 20 36 2c 20 20 20   6,  12,   6,   
2bb34 39 2c 20 20 20 34 2c 20 20 20 36 2c 0a 20 20 20  9,   4,   6,.   
2bb35 20 20 20 20 35 2c 20 20 20 34 2c 20 20 20 37 2c      5,   4,   7,
2bb36 20 20 20 36 2c 20 20 20 35 2c 20 20 20 36 2c 20     6,   5,   6, 
2bb37 20 20 37 2c 20 20 20 35 2c 20 20 20 34 2c 20 20    7,   5,   4,  
2bb38 20 35 2c 20 20 20 37 2c 20 20 20 35 2c 20 20 20   5,   7,   5,   
2bb39 38 2c 0a 20 20 20 20 20 20 20 33 2c 20 20 20 37  8,.       3,   7
2bb3a 2c 20 20 31 33 2c 20 20 20 32 2c 20 20 20 32 2c  ,  13,   2,   2,
2bb3b 20 20 20 34 2c 20 20 20 36 2c 20 20 20 36 2c 20     4,   6,   6, 
2bb3c 20 20 38 2c 20 20 20 35 2c 20 20 31 37 2c 20 20    8,   5,  17,  
2bb3d 31 32 2c 20 20 20 37 2c 0a 20 20 20 20 20 20 20  12,   7,.       
2bb3e 38 2c 20 20 20 38 2c 20 20 20 32 2c 20 20 20 34  8,   8,   2,   4
2bb3f 2c 20 20 20 34 2c 20 20 20 34 2c 20 20 20 34 2c  ,   4,   4,   4,
2bb40 20 20 20 34 2c 20 20 20 32 2c 20 20 20 32 2c 20     4,   2,   2, 
2bb41 20 20 34 2c 20 20 20 36 2c 20 20 20 32 2c 0a 20    4,   6,   2,. 
2bb42 20 20 20 20 20 20 33 2c 20 20 20 36 2c 20 20 20        3,   6,   
2bb43 35 2c 20 20 20 35 2c 20 20 20 35 2c 20 20 20 38  5,   5,   5,   8
2bb44 2c 20 20 20 33 2c 20 20 20 35 2c 20 20 20 35 2c  ,   3,   5,   5,
2bb45 20 20 20 36 2c 20 20 20 34 2c 20 20 20 39 2c 20     6,   4,   9, 
2bb46 20 20 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74    3,.  };.  stat
2bb47 69 63 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65  ic const unsigne
2bb48 64 20 73 68 6f 72 74 20 69 6e 74 20 61 4f 66 66  d short int aOff
2bb49 73 65 74 5b 31 31 37 5d 20 3d 20 7b 0a 20 20 20  set[117] = {.   
2bb4a 20 20 20 20 30 2c 20 20 20 32 2c 20 20 20 32 2c      0,   2,   2,
2bb4b 20 20 20 38 2c 20 20 20 39 2c 20 20 31 34 2c 20     8,   9,  14, 
2bb4c 20 31 36 2c 20 20 32 30 2c 20 20 32 33 2c 20 20   16,  20,  23,  
2bb4d 32 35 2c 20 20 32 35 2c 20 20 32 39 2c 20 20 33  25,  25,  29,  3
2bb4e 33 2c 0a 20 20 20 20 20 20 33 36 2c 20 20 34 31  3,.      36,  41
2bb4f 2c 20 20 34 36 2c 20 20 34 38 2c 20 20 35 33 2c  ,  46,  48,  53,
2bb50 20 20 35 34 2c 20 20 35 39 2c 20 20 36 32 2c 20    54,  59,  62, 
2bb51 20 36 35 2c 20 20 36 37 2c 20 20 36 39 2c 20 20   65,  67,  69,  
2bb52 37 38 2c 20 20 38 31 2c 0a 20 20 20 20 20 20 38  78,  81,.      8
2bb53 36 2c 20 20 39 35 2c 20 20 39 36 2c 20 31 30 31  6,  95,  96, 101
2bb54 2c 20 31 30 35 2c 20 31 30 39 2c 20 31 31 37 2c  , 105, 109, 117,
2bb55 20 31 32 33 2c 20 31 33 30 2c 20 31 33 38 2c 20   123, 130, 138, 
2bb56 31 34 34 2c 20 31 35 34 2c 20 31 35 37 2c 0a 20  144, 154, 157,. 
2bb57 20 20 20 20 31 36 32 2c 20 31 36 37 2c 20 31 37      162, 167, 17
2bb58 32 2c 20 31 37 35 2c 20 31 37 39 2c 20 31 37 39  2, 175, 179, 179
2bb59 2c 20 31 38 33 2c 20 31 38 38 2c 20 31 39 31 2c  , 183, 188, 191,
2bb5a 20 31 39 35 2c 20 32 30 31 2c 20 32 30 37 2c 20   195, 201, 207, 
2bb5b 32 30 37 2c 0a 20 20 20 20 20 32 31 30 2c 20 32  207,.     210, 2
2bb5c 31 33 2c 20 32 31 37 2c 20 32 31 38 2c 20 32 32  13, 217, 218, 22
2bb5d 32 2c 20 32 32 38 2c 20 32 33 32 2c 20 32 33 39  2, 228, 232, 239
2bb5e 2c 20 32 34 35 2c 20 32 35 37 2c 20 32 36 33 2c  , 245, 257, 263,
2bb5f 20 32 37 32 2c 20 32 37 34 2c 0a 20 20 20 20 20   272, 274,.     
2bb60 32 38 30 2c 20 32 38 35 2c 20 32 38 37 2c 20 32  280, 285, 287, 2
2bb61 39 34 2c 20 32 39 39 2c 20 33 30 34 2c 20 33 31  94, 299, 304, 31
2bb62 30 2c 20 33 31 36 2c 20 33 32 31 2c 20 33 32 35  0, 316, 321, 325
2bb63 2c 20 33 32 38 2c 20 33 33 35 2c 20 33 33 39 2c  , 328, 335, 339,
2bb64 0a 20 20 20 20 20 33 34 37 2c 20 33 34 39 2c 20  .     347, 349, 
2bb65 33 35 36 2c 20 33 35 38 2c 20 33 36 30 2c 20 33  356, 358, 360, 3
2bb66 36 39 2c 20 33 37 33 2c 20 33 37 39 2c 20 33 38  69, 373, 379, 38
2bb67 35 2c 20 33 39 33 2c 20 33 39 38 2c 20 33 39 38  5, 393, 398, 398
2bb68 2c 20 34 31 34 2c 0a 20 20 20 20 20 34 32 31 2c  , 414,.     421,
2bb69 20 34 32 38 2c 20 34 32 39 2c 20 34 33 36 2c 20   428, 429, 436, 
2bb6a 34 34 30 2c 20 34 34 34 2c 20 34 34 38 2c 20 34  440, 444, 448, 4
2bb6b 35 32 2c 20 34 35 35 2c 20 34 35 37 2c 20 34 35  52, 455, 457, 45
2bb6c 39 2c 20 34 36 32 2c 20 34 36 32 2c 0a 20 20 20  9, 462, 462,.   
2bb6d 20 20 34 36 35 2c 20 34 36 38 2c 20 34 37 34 2c    465, 468, 474,
2bb6e 20 34 37 38 2c 20 34 38 33 2c 20 34 38 37 2c 20   478, 483, 487, 
2bb6f 34 39 35 2c 20 34 39 38 2c 20 35 30 33 2c 20 35  495, 498, 503, 5
2bb70 30 38 2c 20 35 31 34 2c 20 35 31 38 2c 20 35 32  08, 514, 518, 52
2bb71 33 2c 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69 63  3,.  };.  static
2bb72 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
2bb73 63 68 61 72 20 61 43 6f 64 65 5b 31 31 37 5d 20  char aCode[117] 
2bb74 3d 20 7b 0a 20 20 20 20 54 4b 5f 52 45 49 4e 44  = {.    TK_REIND
2bb75 45 58 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 45  EX,    TK_INDEXE
2bb76 44 2c 20 20 20 20 54 4b 5f 49 4e 44 45 58 2c 20  D,    TK_INDEX, 
2bb77 20 20 20 20 20 54 4b 5f 44 45 53 43 2c 20 20 20       TK_DESC,   
2bb78 20 20 20 20 54 4b 5f 45 53 43 41 50 45 2c 20 20      TK_ESCAPE,  
2bb79 20 20 20 0a 20 20 20 20 54 4b 5f 45 41 43 48 2c     .    TK_EACH,
2bb7a 20 20 20 20 20 20 20 54 4b 5f 43 48 45 43 4b 2c         TK_CHECK,
2bb7b 20 20 20 20 20 20 54 4b 5f 4b 45 59 2c 20 20 20        TK_KEY,   
2bb7c 20 20 20 20 20 54 4b 5f 42 45 46 4f 52 45 2c 20       TK_BEFORE, 
2bb7d 20 20 20 20 54 4b 5f 46 4f 52 45 49 47 4e 2c 20      TK_FOREIGN, 
2bb7e 20 20 20 0a 20 20 20 20 54 4b 5f 46 4f 52 2c 20     .    TK_FOR, 
2bb7f 20 20 20 20 20 20 20 54 4b 5f 49 47 4e 4f 52 45         TK_IGNORE
2bb80 2c 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57  ,     TK_LIKE_KW
2bb81 2c 20 20 20 20 54 4b 5f 45 58 50 4c 41 49 4e 2c  ,    TK_EXPLAIN,
2bb82 20 20 20 20 54 4b 5f 49 4e 53 54 45 41 44 2c 20      TK_INSTEAD, 
2bb83 20 20 20 0a 20 20 20 20 54 4b 5f 41 44 44 2c 20     .    TK_ADD, 
2bb84 20 20 20 20 20 20 20 54 4b 5f 44 41 54 41 42 41         TK_DATABA
2bb85 53 45 2c 20 20 20 54 4b 5f 41 53 2c 20 20 20 20  SE,   TK_AS,    
2bb86 20 20 20 20 20 54 4b 5f 53 45 4c 45 43 54 2c 20       TK_SELECT, 
2bb87 20 20 20 20 54 4b 5f 54 41 42 4c 45 2c 20 20 20      TK_TABLE,   
2bb88 20 20 20 0a 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f     .    TK_JOIN_
2bb89 4b 57 2c 20 20 20 20 54 4b 5f 54 48 45 4e 2c 20  KW,    TK_THEN, 
2bb8a 20 20 20 20 20 20 54 4b 5f 45 4e 44 2c 20 20 20        TK_END,   
2bb8b 20 20 20 20 20 54 4b 5f 44 45 46 45 52 52 41 42       TK_DEFERRAB
2bb8c 4c 45 2c 20 54 4b 5f 45 4c 53 45 2c 20 20 20 20  LE, TK_ELSE,    
2bb8d 20 20 20 0a 20 20 20 20 54 4b 5f 45 58 43 45 50     .    TK_EXCEP
2bb8e 54 2c 20 20 20 20 20 54 4b 5f 54 52 41 4e 53 41  T,     TK_TRANSA
2bb8f 43 54 49 4f 4e 2c 54 4b 5f 4f 4e 2c 20 20 20 20  CTION,TK_ON,    
2bb90 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c       TK_JOIN_KW,
2bb91 20 20 20 20 54 4b 5f 41 4c 54 45 52 2c 20 20 20      TK_ALTER,   
2bb92 20 20 20 0a 20 20 20 20 54 4b 5f 52 41 49 53 45     .    TK_RAISE
2bb93 2c 20 20 20 20 20 20 54 4b 5f 45 58 43 4c 55 53  ,      TK_EXCLUS
2bb94 49 56 45 2c 20 20 54 4b 5f 45 58 49 53 54 53 2c  IVE,  TK_EXISTS,
2bb95 20 20 20 20 20 54 4b 5f 43 4f 4e 53 54 52 41 49       TK_CONSTRAI
2bb96 4e 54 2c 20 54 4b 5f 49 4e 54 45 52 53 45 43 54  NT, TK_INTERSECT
2bb97 2c 20 20 0a 20 20 20 20 54 4b 5f 54 52 49 47 47  ,  .    TK_TRIGG
2bb98 45 52 2c 20 20 20 20 54 4b 5f 52 45 46 45 52 45  ER,    TK_REFERE
2bb99 4e 43 45 53 2c 20 54 4b 5f 55 4e 49 51 55 45 2c  NCES, TK_UNIQUE,
2bb9a 20 20 20 20 20 54 4b 5f 51 55 45 52 59 2c 20 20       TK_QUERY,  
2bb9b 20 20 20 20 54 4b 5f 41 54 54 41 43 48 2c 20 20      TK_ATTACH,  
2bb9c 20 20 20 0a 20 20 20 20 54 4b 5f 48 41 56 49 4e     .    TK_HAVIN
2bb9d 47 2c 20 20 20 20 20 54 4b 5f 47 52 4f 55 50 2c  G,     TK_GROUP,
2bb9e 20 20 20 20 20 20 54 4b 5f 55 50 44 41 54 45 2c        TK_UPDATE,
2bb9f 20 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20       TK_TEMP,   
2bba0 20 20 20 20 54 4b 5f 54 45 4d 50 2c 20 20 20 20      TK_TEMP,    
2bba1 20 20 20 0a 20 20 20 20 54 4b 5f 4f 52 2c 20 20     .    TK_OR,  
2bba2 20 20 20 20 20 20 20 54 4b 5f 42 45 47 49 4e 2c         TK_BEGIN,
2bba3 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57        TK_JOIN_KW
2bba4 2c 20 20 20 20 54 4b 5f 52 45 4e 41 4d 45 2c 20  ,    TK_RENAME, 
2bba5 20 20 20 20 54 4b 5f 42 45 54 57 45 45 4e 2c 20      TK_BETWEEN, 
2bba6 20 20 20 0a 20 20 20 20 54 4b 5f 4e 4f 54 4e 55     .    TK_NOTNU
2bba7 4c 4c 2c 20 20 20 20 54 4b 5f 4e 4f 54 2c 20 20  LL,    TK_NOT,  
2bba8 20 20 20 20 20 20 54 4b 5f 4e 55 4c 4c 2c 20 20        TK_NULL,  
2bba9 20 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c       TK_LIKE_KW,
2bbaa 20 20 20 20 54 4b 5f 43 41 53 43 41 44 45 2c 20      TK_CASCADE, 
2bbab 20 20 20 0a 20 20 20 20 54 4b 5f 41 53 43 2c 20     .    TK_ASC, 
2bbac 20 20 20 20 20 20 20 54 4b 5f 44 45 4c 45 54 45         TK_DELETE
2bbad 2c 20 20 20 20 20 54 4b 5f 43 41 53 45 2c 20 20  ,     TK_CASE,  
2bbae 20 20 20 20 20 54 4b 5f 43 4f 4c 4c 41 54 45 2c       TK_COLLATE,
2bbaf 20 20 20 20 54 4b 5f 43 52 45 41 54 45 2c 20 20      TK_CREATE,  
2bbb0 20 20 20 0a 20 20 20 20 54 4b 5f 43 54 49 4d 45     .    TK_CTIME
2bbb1 5f 4b 57 2c 20 20 20 54 4b 5f 44 45 54 41 43 48  _KW,   TK_DETACH
2bbb2 2c 20 20 20 20 20 54 4b 5f 49 4d 4d 45 44 49 41  ,     TK_IMMEDIA
2bbb3 54 45 2c 20 20 54 4b 5f 4a 4f 49 4e 2c 20 20 20  TE,  TK_JOIN,   
2bbb4 20 20 20 20 54 4b 5f 49 4e 53 45 52 54 2c 20 20      TK_INSERT,  
2bbb5 20 20 20 0a 20 20 20 20 54 4b 5f 4d 41 54 43 48     .    TK_MATCH
2bbb6 2c 20 20 20 20 20 20 54 4b 5f 50 4c 41 4e 2c 20  ,      TK_PLAN, 
2bbb7 20 20 20 20 20 20 54 4b 5f 41 4e 41 4c 59 5a 45        TK_ANALYZE
2bbb8 2c 20 20 20 20 54 4b 5f 50 52 41 47 4d 41 2c 20  ,    TK_PRAGMA, 
2bbb9 20 20 20 20 54 4b 5f 41 42 4f 52 54 2c 20 20 20      TK_ABORT,   
2bbba 20 20 20 0a 20 20 20 20 54 4b 5f 56 41 4c 55 45     .    TK_VALUE
2bbbb 53 2c 20 20 20 20 20 54 4b 5f 56 49 52 54 55 41  S,     TK_VIRTUA
2bbbc 4c 2c 20 20 20 20 54 4b 5f 4c 49 4d 49 54 2c 20  L,    TK_LIMIT, 
2bbbd 20 20 20 20 20 54 4b 5f 57 48 45 4e 2c 20 20 20       TK_WHEN,   
2bbbe 20 20 20 20 54 4b 5f 57 48 45 52 45 2c 20 20 20      TK_WHERE,   
2bbbf 20 20 20 0a 20 20 20 20 54 4b 5f 52 45 50 4c 41     .    TK_REPLA
2bbc0 43 45 2c 20 20 20 20 54 4b 5f 41 46 54 45 52 2c  CE,    TK_AFTER,
2bbc1 20 20 20 20 20 20 54 4b 5f 52 45 53 54 52 49 43        TK_RESTRIC
2bbc2 54 2c 20 20 20 54 4b 5f 41 4e 44 2c 20 20 20 20  T,   TK_AND,    
2bbc3 20 20 20 20 54 4b 5f 44 45 46 41 55 4c 54 2c 20      TK_DEFAULT, 
2bbc4 20 20 20 0a 20 20 20 20 54 4b 5f 41 55 54 4f 49     .    TK_AUTOI
2bbc5 4e 43 52 2c 20 20 20 54 4b 5f 54 4f 2c 20 20 20  NCR,   TK_TO,   
2bbc6 20 20 20 20 20 20 54 4b 5f 49 4e 2c 20 20 20 20        TK_IN,    
2bbc7 20 20 20 20 20 54 4b 5f 43 41 53 54 2c 20 20 20       TK_CAST,   
2bbc8 20 20 20 20 54 4b 5f 43 4f 4c 55 4d 4e 4b 57 2c      TK_COLUMNKW,
2bbc9 20 20 20 0a 20 20 20 20 54 4b 5f 43 4f 4d 4d 49     .    TK_COMMI
2bbca 54 2c 20 20 20 20 20 54 4b 5f 43 4f 4e 46 4c 49  T,     TK_CONFLI
2bbcb 43 54 2c 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57  CT,   TK_JOIN_KW
2bbcc 2c 20 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57  ,    TK_CTIME_KW
2bbcd 2c 20 20 20 54 4b 5f 43 54 49 4d 45 5f 4b 57 2c  ,   TK_CTIME_KW,
2bbce 20 20 20 0a 20 20 20 20 54 4b 5f 50 52 49 4d 41     .    TK_PRIMA
2bbcf 52 59 2c 20 20 20 20 54 4b 5f 44 45 46 45 52 52  RY,    TK_DEFERR
2bbd0 45 44 2c 20 20 20 54 4b 5f 44 49 53 54 49 4e 43  ED,   TK_DISTINC
2bbd1 54 2c 20 20 20 54 4b 5f 49 53 2c 20 20 20 20 20  T,   TK_IS,     
2bbd2 20 20 20 20 54 4b 5f 44 52 4f 50 2c 20 20 20 20      TK_DROP,    
2bbd3 20 20 20 0a 20 20 20 20 54 4b 5f 46 41 49 4c 2c     .    TK_FAIL,
2bbd4 20 20 20 20 20 20 20 54 4b 5f 46 52 4f 4d 2c 20         TK_FROM, 
2bbd5 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57        TK_JOIN_KW
2bbd6 2c 20 20 20 20 54 4b 5f 4c 49 4b 45 5f 4b 57 2c  ,    TK_LIKE_KW,
2bbd7 20 20 20 20 54 4b 5f 42 59 2c 20 20 20 20 20 20      TK_BY,      
2bbd8 20 20 20 0a 20 20 20 20 54 4b 5f 49 46 2c 20 20     .    TK_IF,  
2bbd9 20 20 20 20 20 20 20 54 4b 5f 49 4e 54 4f 2c 20         TK_INTO, 
2bbda 20 20 20 20 20 20 54 4b 5f 4f 46 46 53 45 54 2c        TK_OFFSET,
2bbdb 20 20 20 20 20 54 4b 5f 4f 46 2c 20 20 20 20 20       TK_OF,     
2bbdc 20 20 20 20 54 4b 5f 53 45 54 2c 20 20 20 20 20      TK_SET,     
2bbdd 20 20 20 0a 20 20 20 20 54 4b 5f 49 53 4e 55 4c     .    TK_ISNUL
2bbde 4c 2c 20 20 20 20 20 54 4b 5f 4f 52 44 45 52 2c  L,     TK_ORDER,
2bbdf 20 20 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57        TK_JOIN_KW
2bbe0 2c 20 20 20 20 54 4b 5f 4a 4f 49 4e 5f 4b 57 2c  ,    TK_JOIN_KW,
2bbe1 20 20 20 20 54 4b 5f 52 4f 4c 4c 42 41 43 4b 2c      TK_ROLLBACK,
2bbe2 20 20 20 0a 20 20 20 20 54 4b 5f 52 4f 57 2c 20     .    TK_ROW, 
2bbe3 20 20 20 20 20 20 20 54 4b 5f 55 4e 49 4f 4e 2c         TK_UNION,
2bbe4 20 20 20 20 20 20 54 4b 5f 55 53 49 4e 47 2c 20        TK_USING, 
2bbe5 20 20 20 20 20 54 4b 5f 56 41 43 55 55 4d 2c 20       TK_VACUUM, 
2bbe6 20 20 20 20 54 4b 5f 56 49 45 57 2c 20 20 20 20      TK_VIEW,    
2bbe7 20 20 20 0a 20 20 20 20 54 4b 5f 49 4e 49 54 49     .    TK_INITI
2bbe8 41 4c 4c 59 2c 20 20 54 4b 5f 41 4c 4c 2c 20 20  ALLY,  TK_ALL,  
2bbe9 20 20 20 20 20 20 0a 20 20 7d 3b 0a 20 20 69 6e        .  };.  in
2bbea 74 20 68 2c 20 69 3b 0a 20 20 69 66 28 20 6e 3c  t h, i;.  if( n<
2bbeb 32 20 29 20 72 65 74 75 72 6e 20 54 4b 5f 49 44  2 ) return TK_ID
2bbec 3b 0a 20 20 68 20 3d 20 28 28 63 68 61 72 4d 61  ;.  h = ((charMa
2bbed 70 28 7a 5b 30 5d 29 2a 34 29 20 5e 0a 20 20 20  p(z[0])*4) ^.   
2bbee 20 20 20 28 63 68 61 72 4d 61 70 28 7a 5b 6e 2d     (charMap(z[n-
2bbef 31 5d 29 2a 33 29 20 5e 0a 20 20 20 20 20 20 6e  1])*3) ^.      n
2bbf0 29 20 25 20 31 32 37 3b 0a 20 20 66 6f 72 28 69  ) % 127;.  for(i
2bbf1 3d 28 28 69 6e 74 29 61 48 61 73 68 5b 68 5d 29  =((int)aHash[h])
2bbf2 2d 31 3b 20 69 3e 3d 30 3b 20 69 3d 28 28 69 6e  -1; i>=0; i=((in
2bbf3 74 29 61 4e 65 78 74 5b 69 5d 29 2d 31 29 7b 0a  t)aNext[i])-1){.
2bbf4 20 20 20 20 69 66 28 20 61 4c 65 6e 5b 69 5d 3d      if( aLen[i]=
2bbf5 3d 6e 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =n && sqlite3Str
2bbf6 4e 49 43 6d 70 28 26 7a 54 65 78 74 5b 61 4f 66  NICmp(&zText[aOf
2bbf7 66 73 65 74 5b 69 5d 5d 2c 7a 2c 6e 29 3d 3d 30  fset[i]],z,n)==0
2bbf8 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   ){.      return
2bbf9 20 61 43 6f 64 65 5b 69 5d 3b 0a 20 20 20 20 7d   aCode[i];.    }
2bbfa 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 54 4b  .  }.  return TK
2bbfb 5f 49 44 3b 0a 7d 0a 53 51 4c 49 54 45 5f 50 52  _ID;.}.SQLITE_PR
2bbfc 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65  IVATE int sqlite
2bbfd 33 4b 65 79 77 6f 72 64 43 6f 64 65 28 63 6f 6e  3KeywordCode(con
2bbfe 73 74 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72  st unsigned char
2bbff 20 2a 7a 2c 20 69 6e 74 20 6e 29 7b 0a 20 20 72   *z, int n){.  r
2bc00 65 74 75 72 6e 20 6b 65 79 77 6f 72 64 43 6f 64  eturn keywordCod
2bc01 65 28 28 63 68 61 72 2a 29 7a 2c 20 6e 29 3b 0a  e((char*)z, n);.
2bc02 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  }../************
2bc03 2a 2a 20 45 6e 64 20 6f 66 20 6b 65 79 77 6f 72  ** End of keywor
2bc04 64 68 61 73 68 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a  dhash.h ********
2bc05 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bc06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bc07 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
2bc08 2a 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68  ** Continuing wh
2bc09 65 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20  ere we left off 
2bc0a 69 6e 20 74 6f 6b 65 6e 69 7a 65 2e 63 20 2a 2a  in tokenize.c **
2bc0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bc0c 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 58 20  */.../*.** If X 
2bc0d 69 73 20 61 20 63 68 61 72 61 63 74 65 72 20 74  is a character t
2bc0e 68 61 74 20 63 61 6e 20 62 65 20 75 73 65 64 20  hat can be used 
2bc0f 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69 65 72  in an identifier
2bc10 20 74 68 65 6e 0a 2a 2a 20 49 64 43 68 61 72 28   then.** IdChar(
2bc11 58 29 20 77 69 6c 6c 20 62 65 20 74 72 75 65 2e  X) will be true.
2bc12 20 20 4f 74 68 65 72 77 69 73 65 20 69 74 20 69    Otherwise it i
2bc13 73 20 66 61 6c 73 65 2e 0a 2a 2a 0a 2a 2a 20 46  s false..**.** F
2bc14 6f 72 20 41 53 43 49 49 2c 20 61 6e 79 20 63 68  or ASCII, any ch
2bc15 61 72 61 63 74 65 72 20 77 69 74 68 20 74 68 65  aracter with the
2bc16 20 68 69 67 68 2d 6f 72 64 65 72 20 62 69 74 20   high-order bit 
2bc17 73 65 74 20 69 73 0a 2a 2a 20 61 6c 6c 6f 77 65  set is.** allowe
2bc18 64 20 69 6e 20 61 6e 20 69 64 65 6e 74 69 66 69  d in an identifi
2bc19 65 72 2e 20 20 46 6f 72 20 37 2d 62 69 74 20 63  er.  For 7-bit c
2bc1a 68 61 72 61 63 74 65 72 73 2c 20 0a 2a 2a 20 73  haracters, .** s
2bc1b 71 6c 69 74 65 33 49 73 49 64 43 68 61 72 5b 58  qlite3IsIdChar[X
2bc1c 5d 20 6d 75 73 74 20 62 65 20 31 2e 0a 2a 2a 0a  ] must be 1..**.
2bc1d 2a 2a 20 46 6f 72 20 45 42 43 44 49 43 2c 20 74  ** For EBCDIC, t
2bc1e 68 65 20 72 75 6c 65 73 20 61 72 65 20 6d 6f 72  he rules are mor
2bc1f 65 20 63 6f 6d 70 6c 65 78 20 62 75 74 20 68 61  e complex but ha
2bc20 76 65 20 74 68 65 20 73 61 6d 65 0a 2a 2a 20 65  ve the same.** e
2bc21 6e 64 20 72 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a  nd result..**.**
2bc22 20 54 69 63 6b 65 74 20 23 31 30 36 36 2e 20 20   Ticket #1066.  
2bc23 74 68 65 20 53 51 4c 20 73 74 61 6e 64 61 72 64  the SQL standard
2bc24 20 64 6f 65 73 20 6e 6f 74 20 61 6c 6c 6f 77 20   does not allow 
2bc25 27 24 27 20 69 6e 20 74 68 65 0a 2a 2a 20 6d 69  '$' in the.** mi
2bc26 64 64 6c 65 20 6f 66 20 69 64 65 6e 74 66 69 65  ddle of identfie
2bc27 72 73 2e 20 20 42 75 74 20 6d 61 6e 79 20 53 51  rs.  But many SQ
2bc28 4c 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  L implementation
2bc29 73 20 64 6f 2e 20 0a 2a 2a 20 53 51 4c 69 74 65  s do. .** SQLite
2bc2a 20 77 69 6c 6c 20 61 6c 6c 6f 77 20 27 24 27 20   will allow '$' 
2bc2b 69 6e 20 69 64 65 6e 74 69 66 69 65 72 73 20 66  in identifiers f
2bc2c 6f 72 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79  or compatibility
2bc2d 2e 0a 2a 2a 20 42 75 74 20 74 68 65 20 66 65 61  ..** But the fea
2bc2e 74 75 72 65 20 69 73 20 75 6e 64 6f 63 75 6d 65  ture is undocume
2bc2f 6e 74 65 64 2e 0a 2a 2f 0a 23 69 66 64 65 66 20  nted..*/.#ifdef 
2bc30 53 51 4c 49 54 45 5f 41 53 43 49 49 0a 53 51 4c  SQLITE_ASCII.SQL
2bc31 49 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73  ITE_PRIVATE cons
2bc32 74 20 63 68 61 72 20 73 71 6c 69 74 65 33 49 73  t char sqlite3Is
2bc33 41 73 63 69 69 49 64 43 68 61 72 5b 5d 20 3d 20  AsciiIdChar[] = 
2bc34 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
2bc35 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
2bc36 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
2bc37 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30  E xF */.    0, 0
2bc38 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20 30 2c  , 0, 0, 1, 0, 0,
2bc39 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2bc3a 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
2bc3b 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20  2x */.    1, 1, 
2bc3c 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2bc3d 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 1, 0, 0, 0,
2bc3e 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 33 78   0, 0, 0,  /* 3x
2bc3f 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c   */.    0, 1, 1,
2bc40 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2bc41 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2bc42 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78 20 2a  , 1, 1,  /* 4x *
2bc43 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
2bc44 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc45 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20   1, 1, 0, 0, 0, 
2bc46 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a 2f 0a  0, 1,  /* 5x */.
2bc47 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
2bc48 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2bc49 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc4a 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a 20 20   1,  /* 6x */.  
2bc4b 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
2bc4c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2bc4d 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
2bc4e 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b 0a 23  ,  /* 7x */.};.#
2bc4f 64 65 66 69 6e 65 20 49 64 43 68 61 72 28 43 29  define IdChar(C)
2bc50 20 20 28 28 28 63 3d 43 29 26 30 78 38 30 29 21    (((c=C)&0x80)!
2bc51 3d 30 20 7c 7c 20 28 63 3e 30 78 31 66 20 26 26  =0 || (c>0x1f &&
2bc52 20 73 71 6c 69 74 65 33 49 73 41 73 63 69 69 49   sqlite3IsAsciiI
2bc53 64 43 68 61 72 5b 63 2d 30 78 32 30 5d 29 29 0a  dChar[c-0x20])).
2bc54 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
2bc55 4c 49 54 45 5f 45 42 43 44 49 43 0a 53 51 4c 49  LITE_EBCDIC.SQLI
2bc56 54 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74  TE_PRIVATE const
2bc57 20 63 68 61 72 20 73 71 6c 69 74 65 33 49 73 45   char sqlite3IsE
2bc58 62 63 64 69 63 49 64 43 68 61 72 5b 5d 20 3d 20  bcdicIdChar[] = 
2bc59 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20 78 33  {./* x0 x1 x2 x3
2bc5a 20 78 34 20 78 35 20 78 36 20 78 37 20 78 38 20   x4 x5 x6 x7 x8 
2bc5b 78 39 20 78 41 20 78 42 20 78 43 20 78 44 20 78  x9 xA xB xC xD x
2bc5c 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c 20 30  E xF */.    0, 0
2bc5d 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc5e 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
2bc5f 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
2bc60 34 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  4x */.    0, 1, 
2bc61 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2bc62 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 30 2c  , 1, 1, 0, 1, 0,
2bc63 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 35 78   0, 0, 0,  /* 5x
2bc64 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c   */.    0, 0, 1,
2bc65 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2bc66 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 31  1, 1, 0, 0, 0, 1
2bc67 2c 20 30 2c 20 30 2c 20 20 2f 2a 20 36 78 20 2a  , 0, 0,  /* 6x *
2bc68 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
2bc69 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc6a 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2bc6b 30 2c 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a  0, 0,  /* 7x */.
2bc6c 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20      0, 1, 1, 1, 
2bc6d 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2bc6e 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 0, 1, 1, 1,
2bc6f 20 30 2c 20 20 2f 2a 20 38 78 20 2a 2f 0a 20 20   0,  /* 8x */.  
2bc70 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    0, 1, 1, 1, 1,
2bc71 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2bc72 30 2c 20 30 2c 20 31 2c 20 30 2c 20 31 2c 20 30  0, 0, 1, 0, 1, 0
2bc73 2c 20 20 2f 2a 20 39 78 20 2a 2f 0a 20 20 20 20  ,  /* 9x */.    
2bc74 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 0, 1, 1, 1, 1
2bc75 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc76 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20   0, 1, 1, 1, 0, 
2bc77 20 2f 2a 20 41 78 20 2a 2f 0a 20 20 20 20 30 2c   /* Ax */.    0,
2bc78 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
2bc79 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2bc7a 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
2bc7b 2a 20 42 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31  * Bx */.    0, 1
2bc7c 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc7d 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20   1, 1, 1, 0, 1, 
2bc7e 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20  1, 1, 1, 1,  /* 
2bc7f 43 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  Cx */.    0, 1, 
2bc80 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2bc81 2c 20 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c  , 1, 1, 0, 1, 1,
2bc82 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 44 78   1, 1, 1,  /* Dx
2bc83 20 2a 2f 0a 20 20 20 20 30 2c 20 30 2c 20 31 2c   */.    0, 0, 1,
2bc84 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2bc85 31 2c 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31  1, 1, 0, 1, 1, 1
2bc86 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 45 78 20 2a  , 1, 1,  /* Ex *
2bc87 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31  /.    1, 1, 1, 1
2bc88 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2bc89 20 31 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 0, 1, 1, 1, 
2bc8a 31 2c 20 30 2c 20 20 2f 2a 20 46 78 20 2a 2f 0a  1, 0,  /* Fx */.
2bc8b 7d 3b 0a 23 64 65 66 69 6e 65 20 49 64 43 68 61  };.#define IdCha
2bc8c 72 28 43 29 20 20 28 28 28 63 3d 43 29 3e 3d 30  r(C)  (((c=C)>=0
2bc8d 78 34 32 20 26 26 20 73 71 6c 69 74 65 33 49 73  x42 && sqlite3Is
2bc8e 45 62 63 64 69 63 49 64 43 68 61 72 5b 63 2d 30  EbcdicIdChar[c-0
2bc8f 78 34 30 5d 29 29 0a 23 65 6e 64 69 66 0a 0a 0a  x40])).#endif...
2bc90 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65  /*.** Return the
2bc91 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74   length of the t
2bc92 6f 6b 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73  oken that begins
2bc93 20 61 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74   at z[0]. .** St
2bc94 6f 72 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 79  ore the token ty
2bc95 70 65 20 69 6e 20 2a 74 6f 6b 65 6e 54 79 70 65  pe in *tokenType
2bc96 20 62 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e   before returnin
2bc97 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49  g..*/.SQLITE_PRI
2bc98 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33  VATE int sqlite3
2bc99 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74 20 75  GetToken(const u
2bc9a 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 2c  nsigned char *z,
2bc9b 20 69 6e 74 20 2a 74 6f 6b 65 6e 54 79 70 65 29   int *tokenType)
2bc9c 7b 0a 20 20 69 6e 74 20 69 2c 20 63 3b 0a 20 20  {.  int i, c;.  
2bc9d 73 77 69 74 63 68 28 20 2a 7a 20 29 7b 0a 20 20  switch( *z ){.  
2bc9e 20 20 63 61 73 65 20 27 20 27 3a 20 63 61 73 65    case ' ': case
2bc9f 20 27 5c 74 27 3a 20 63 61 73 65 20 27 5c 6e 27   '\t': case '\n'
2bca0 3a 20 63 61 73 65 20 27 5c 66 27 3a 20 63 61 73  : case '\f': cas
2bca1 65 20 27 5c 72 27 3a 20 7b 0a 20 20 20 20 20 20  e '\r': {.      
2bca2 66 6f 72 28 69 3d 31 3b 20 69 73 73 70 61 63 65  for(i=1; isspace
2bca3 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
2bca4 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2bca5 3d 20 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20  = TK_SPACE;.    
2bca6 20 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20    return i;.    
2bca7 7d 0a 20 20 20 20 63 61 73 65 20 27 2d 27 3a 20  }.    case '-': 
2bca8 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31 5d  {.      if( z[1]
2bca9 3d 3d 27 2d 27 20 29 7b 0a 20 20 20 20 20 20 20  =='-' ){.       
2bcaa 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69   for(i=2; (c=z[i
2bcab 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 6e 27  ])!=0 && c!='\n'
2bcac 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 20  ; i++){}.       
2bcad 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2bcae 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20 20 20  _SPACE;.        
2bcaf 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20  return i;.      
2bcb0 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  }.      *tokenTy
2bcb1 70 65 20 3d 20 54 4b 5f 4d 49 4e 55 53 3b 0a 20  pe = TK_MINUS;. 
2bcb2 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2bcb3 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 28     }.    case '(
2bcb4 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2bcb5 6e 54 79 70 65 20 3d 20 54 4b 5f 4c 50 3b 0a 20  nType = TK_LP;. 
2bcb6 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2bcb7 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 29     }.    case ')
2bcb8 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2bcb9 6e 54 79 70 65 20 3d 20 54 4b 5f 52 50 3b 0a 20  nType = TK_RP;. 
2bcba 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20       return 1;. 
2bcbb 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3b     }.    case ';
2bcbc 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  ': {.      *toke
2bcbd 6e 54 79 70 65 20 3d 20 54 4b 5f 53 45 4d 49 3b  nType = TK_SEMI;
2bcbe 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b  .      return 1;
2bcbf 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2bcc0 27 2b 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f  '+': {.      *to
2bcc1 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 50 4c 55  kenType = TK_PLU
2bcc2 53 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  S;.      return 
2bcc3 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  1;.    }.    cas
2bcc4 65 20 27 2a 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '*': {.      *
2bcc5 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 53  tokenType = TK_S
2bcc6 54 41 52 3b 0a 20 20 20 20 20 20 72 65 74 75 72  TAR;.      retur
2bcc7 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n 1;.    }.    c
2bcc8 61 73 65 20 27 2f 27 3a 20 7b 0a 20 20 20 20 20  ase '/': {.     
2bcc9 20 69 66 28 20 7a 5b 31 5d 21 3d 27 2a 27 20 7c   if( z[1]!='*' |
2bcca 7c 20 7a 5b 32 5d 3d 3d 30 20 29 7b 0a 20 20 20  | z[2]==0 ){.   
2bccb 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2bccc 3d 20 54 4b 5f 53 4c 41 53 48 3b 0a 20 20 20 20  = TK_SLASH;.    
2bccd 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2bcce 20 20 20 20 7d 0a 20 20 20 20 20 20 66 6f 72 28      }.      for(
2bccf 69 3d 33 2c 20 63 3d 7a 5b 32 5d 3b 20 28 63 21  i=3, c=z[2]; (c!
2bcd0 3d 27 2a 27 20 7c 7c 20 7a 5b 69 5d 21 3d 27 2f  ='*' || z[i]!='/
2bcd1 27 29 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21 3d  ') && (c=z[i])!=
2bcd2 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20  0; i++){}.      
2bcd3 69 66 28 20 63 20 29 20 69 2b 2b 3b 0a 20 20 20  if( c ) i++;.   
2bcd4 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2bcd5 54 4b 5f 53 50 41 43 45 3b 0a 20 20 20 20 20 20  TK_SPACE;.      
2bcd6 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a  return i;.    }.
2bcd7 20 20 20 20 63 61 73 65 20 27 25 27 3a 20 7b 0a      case '%': {.
2bcd8 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2bcd9 20 3d 20 54 4b 5f 52 45 4d 3b 0a 20 20 20 20 20   = TK_REM;.     
2bcda 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d   return 1;.    }
2bcdb 0a 20 20 20 20 63 61 73 65 20 27 3d 27 3a 20 7b  .    case '=': {
2bcdc 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70  .      *tokenTyp
2bcdd 65 20 3d 20 54 4b 5f 45 51 3b 0a 20 20 20 20 20  e = TK_EQ;.     
2bcde 20 72 65 74 75 72 6e 20 31 20 2b 20 28 7a 5b 31   return 1 + (z[1
2bcdf 5d 3d 3d 27 3d 27 29 3b 0a 20 20 20 20 7d 0a 20  ]=='=');.    }. 
2bce0 20 20 20 63 61 73 65 20 27 3c 27 3a 20 7b 0a 20     case '<': {. 
2bce1 20 20 20 20 20 69 66 28 20 28 63 3d 7a 5b 31 5d       if( (c=z[1]
2bce2 29 3d 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20  )=='=' ){.      
2bce3 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2bce4 4b 5f 4c 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_LE;.        re
2bce5 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 65  turn 2;.      }e
2bce6 6c 73 65 20 69 66 28 20 63 3d 3d 27 3e 27 20 29  lse if( c=='>' )
2bce7 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e  {.        *token
2bce8 54 79 70 65 20 3d 20 54 4b 5f 4e 45 3b 0a 20 20  Type = TK_NE;.  
2bce9 20 20 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a        return 2;.
2bcea 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2bceb 63 3d 3d 27 3c 27 20 29 7b 0a 20 20 20 20 20 20  c=='<' ){.      
2bcec 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2bced 4b 5f 4c 53 48 49 46 54 3b 0a 20 20 20 20 20 20  K_LSHIFT;.      
2bcee 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20    return 2;.    
2bcef 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
2bcf0 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2bcf1 5f 4c 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74  _LT;.        ret
2bcf2 75 72 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20  urn 1;.      }. 
2bcf3 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 3e     }.    case '>
2bcf4 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20 28  ': {.      if( (
2bcf5 63 3d 7a 5b 31 5d 29 3d 3d 27 3d 27 20 29 7b 0a  c=z[1])=='=' ){.
2bcf6 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2bcf7 70 65 20 3d 20 54 4b 5f 47 45 3b 0a 20 20 20 20  pe = TK_GE;.    
2bcf8 20 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20      return 2;.  
2bcf9 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63 3d      }else if( c=
2bcfa 3d 27 3e 27 20 29 7b 0a 20 20 20 20 20 20 20 20  ='>' ){.        
2bcfb 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2bcfc 52 53 48 49 46 54 3b 0a 20 20 20 20 20 20 20 20  RSHIFT;.        
2bcfd 72 65 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20  return 2;.      
2bcfe 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a  }else{.        *
2bcff 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 47  tokenType = TK_G
2bd00 54 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72  T;.        retur
2bd01 6e 20 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  n 1;.      }.   
2bd02 20 7d 0a 20 20 20 20 63 61 73 65 20 27 21 27 3a   }.    case '!':
2bd03 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
2bd04 5d 21 3d 27 3d 27 20 29 7b 0a 20 20 20 20 20 20  ]!='=' ){.      
2bd05 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2bd06 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
2bd07 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20     return 2;.   
2bd08 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2bd09 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2bd0a 4b 5f 4e 45 3b 0a 20 20 20 20 20 20 20 20 72 65  K_NE;.        re
2bd0b 74 75 72 6e 20 32 3b 0a 20 20 20 20 20 20 7d 0a  turn 2;.      }.
2bd0c 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2bd0d 7c 27 3a 20 7b 0a 20 20 20 20 20 20 69 66 28 20  |': {.      if( 
2bd0e 7a 5b 31 5d 21 3d 27 7c 27 20 29 7b 0a 20 20 20  z[1]!='|' ){.   
2bd0f 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2bd10 3d 20 54 4b 5f 42 49 54 4f 52 3b 0a 20 20 20 20  = TK_BITOR;.    
2bd11 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
2bd12 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2bd13 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2bd14 54 4b 5f 43 4f 4e 43 41 54 3b 0a 20 20 20 20 20  TK_CONCAT;.     
2bd15 20 20 20 72 65 74 75 72 6e 20 32 3b 0a 20 20 20     return 2;.   
2bd16 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 63     }.    }.    c
2bd17 61 73 65 20 27 2c 27 3a 20 7b 0a 20 20 20 20 20  ase ',': {.     
2bd18 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b   *tokenType = TK
2bd19 5f 43 4f 4d 4d 41 3b 0a 20 20 20 20 20 20 72 65  _COMMA;.      re
2bd1a 74 75 72 6e 20 31 3b 0a 20 20 20 20 7d 0a 20 20  turn 1;.    }.  
2bd1b 20 20 63 61 73 65 20 27 26 27 3a 20 7b 0a 20 20    case '&': {.  
2bd1c 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2bd1d 20 54 4b 5f 42 49 54 41 4e 44 3b 0a 20 20 20 20   TK_BITAND;.    
2bd1e 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 20 20    return 1;.    
2bd1f 7d 0a 20 20 20 20 63 61 73 65 20 27 7e 27 3a 20  }.    case '~': 
2bd20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  {.      *tokenTy
2bd21 70 65 20 3d 20 54 4b 5f 42 49 54 4e 4f 54 3b 0a  pe = TK_BITNOT;.
2bd22 20 20 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a        return 1;.
2bd23 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2bd24 60 27 3a 0a 20 20 20 20 63 61 73 65 20 27 5c 27  `':.    case '\'
2bd25 27 3a 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a  ':.    case '"':
2bd26 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 6c   {.      int del
2bd27 69 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20  im = z[0];.     
2bd28 20 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69   for(i=1; (c=z[i
2bd29 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20  ])!=0; i++){.   
2bd2a 20 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69       if( c==deli
2bd2b 6d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69  m ){.          i
2bd2c 66 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d  f( z[i+1]==delim
2bd2d 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20   ){.            
2bd2e 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  i++;.          }
2bd2f 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
2bd30 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20    break;.       
2bd31 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20     }.        }. 
2bd32 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2bd33 20 63 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20   c=='\'' ){.    
2bd34 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d      *tokenType =
2bd35 20 54 4b 5f 53 54 52 49 4e 47 3b 0a 20 20 20 20   TK_STRING;.    
2bd36 20 20 20 20 72 65 74 75 72 6e 20 69 2b 31 3b 0a      return i+1;.
2bd37 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20        }else if( 
2bd38 63 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  c!=0 ){.        
2bd39 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f  *tokenType = TK_
2bd3a 49 44 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75  ID;.        retu
2bd3b 72 6e 20 69 2b 31 3b 0a 20 20 20 20 20 20 7d 65  rn i+1;.      }e
2bd3c 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f  lse{.        *to
2bd3d 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c 4c  kenType = TK_ILL
2bd3e 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20 72 65  EGAL;.        re
2bd3f 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d 0a  turn i;.      }.
2bd40 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2bd41 2e 27 3a 20 7b 0a 23 69 66 6e 64 65 66 20 53 51  .': {.#ifndef SQ
2bd42 4c 49 54 45 5f 4f 4d 49 54 5f 46 4c 4f 41 54 49  LITE_OMIT_FLOATI
2bd43 4e 47 5f 50 4f 49 4e 54 0a 20 20 20 20 20 20 69  NG_POINT.      i
2bd44 66 28 20 21 69 73 64 69 67 69 74 28 7a 5b 31 5d  f( !isdigit(z[1]
2bd45 29 20 29 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  ) ).#endif.     
2bd46 20 7b 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b 65   {.        *toke
2bd47 6e 54 79 70 65 20 3d 20 54 4b 5f 44 4f 54 3b 0a  nType = TK_DOT;.
2bd48 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 31          return 1
2bd49 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2bd4a 2f 2a 20 49 66 20 74 68 65 20 6e 65 78 74 20 63  /* If the next c
2bd4b 68 61 72 61 63 74 65 72 20 69 73 20 61 20 64 69  haracter is a di
2bd4c 67 69 74 2c 20 74 68 69 73 20 69 73 20 61 20 66  git, this is a f
2bd4d 6c 6f 61 74 69 6e 67 20 70 6f 69 6e 74 0a 20 20  loating point.  
2bd4e 20 20 20 20 2a 2a 20 6e 75 6d 62 65 72 20 74 68      ** number th
2bd4f 61 74 20 62 65 67 69 6e 73 20 77 69 74 68 20 22  at begins with "
2bd50 2e 22 2e 20 20 46 61 6c 6c 20 74 68 72 75 20 69  .".  Fall thru i
2bd51 6e 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73  nto the next cas
2bd52 65 20 2a 2f 0a 20 20 20 20 7d 0a 20 20 20 20 63  e */.    }.    c
2bd53 61 73 65 20 27 30 27 3a 20 63 61 73 65 20 27 31  ase '0': case '1
2bd54 27 3a 20 63 61 73 65 20 27 32 27 3a 20 63 61 73  ': case '2': cas
2bd55 65 20 27 33 27 3a 20 63 61 73 65 20 27 34 27 3a  e '3': case '4':
2bd56 0a 20 20 20 20 63 61 73 65 20 27 35 27 3a 20 63  .    case '5': c
2bd57 61 73 65 20 27 36 27 3a 20 63 61 73 65 20 27 37  ase '6': case '7
2bd58 27 3a 20 63 61 73 65 20 27 38 27 3a 20 63 61 73  ': case '8': cas
2bd59 65 20 27 39 27 3a 20 7b 0a 20 20 20 20 20 20 2a  e '9': {.      *
2bd5a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2bd5b 4e 54 45 47 45 52 3b 0a 20 20 20 20 20 20 66 6f  NTEGER;.      fo
2bd5c 72 28 69 3d 30 3b 20 69 73 64 69 67 69 74 28 7a  r(i=0; isdigit(z
2bd5d 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 23 69 66  [i]); i++){}.#if
2bd5e 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2bd5f 5f 46 4c 4f 41 54 49 4e 47 5f 50 4f 49 4e 54 0a  _FLOATING_POINT.
2bd60 20 20 20 20 20 20 69 66 28 20 7a 5b 69 5d 3d 3d        if( z[i]==
2bd61 27 2e 27 20 29 7b 0a 20 20 20 20 20 20 20 20 69  '.' ){.        i
2bd62 2b 2b 3b 0a 20 20 20 20 20 20 20 20 77 68 69 6c  ++;.        whil
2bd63 65 28 20 69 73 64 69 67 69 74 28 7a 5b 69 5d 29  e( isdigit(z[i])
2bd64 20 29 7b 20 69 2b 2b 3b 20 7d 0a 20 20 20 20 20   ){ i++; }.     
2bd65 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2bd66 54 4b 5f 46 4c 4f 41 54 3b 0a 20 20 20 20 20 20  TK_FLOAT;.      
2bd67 7d 0a 20 20 20 20 20 20 69 66 28 20 28 7a 5b 69  }.      if( (z[i
2bd68 5d 3d 3d 27 65 27 20 7c 7c 20 7a 5b 69 5d 3d 3d  ]=='e' || z[i]==
2bd69 27 45 27 29 20 26 26 0a 20 20 20 20 20 20 20 20  'E') &&.        
2bd6a 20 20 20 28 20 69 73 64 69 67 69 74 28 7a 5b 69     ( isdigit(z[i
2bd6b 2b 31 5d 29 20 0a 20 20 20 20 20 20 20 20 20 20  +1]) .          
2bd6c 20 20 7c 7c 20 28 28 7a 5b 69 2b 31 5d 3d 3d 27    || ((z[i+1]=='
2bd6d 2b 27 20 7c 7c 20 7a 5b 69 2b 31 5d 3d 3d 27 2d  +' || z[i+1]=='-
2bd6e 27 29 20 26 26 20 69 73 64 69 67 69 74 28 7a 5b  ') && isdigit(z[
2bd6f 69 2b 32 5d 29 29 0a 20 20 20 20 20 20 20 20 20  i+2])).         
2bd70 20 20 29 0a 20 20 20 20 20 20 29 7b 0a 20 20 20    ).      ){.   
2bd71 20 20 20 20 20 69 20 2b 3d 20 32 3b 0a 20 20 20       i += 2;.   
2bd72 20 20 20 20 20 77 68 69 6c 65 28 20 69 73 64 69       while( isdi
2bd73 67 69 74 28 7a 5b 69 5d 29 20 29 7b 20 69 2b 2b  git(z[i]) ){ i++
2bd74 3b 20 7d 0a 20 20 20 20 20 20 20 20 2a 74 6f 6b  ; }.        *tok
2bd75 65 6e 54 79 70 65 20 3d 20 54 4b 5f 46 4c 4f 41  enType = TK_FLOA
2bd76 54 3b 0a 20 20 20 20 20 20 7d 0a 23 65 6e 64 69  T;.      }.#endi
2bd77 66 0a 20 20 20 20 20 20 77 68 69 6c 65 28 20 49  f.      while( I
2bd78 64 43 68 61 72 28 7a 5b 69 5d 29 20 29 7b 0a 20  dChar(z[i]) ){. 
2bd79 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70         *tokenTyp
2bd7a 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  e = TK_ILLEGAL;.
2bd7b 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20          i++;.   
2bd7c 20 20 20 7d 0a 20 20 20 20 20 20 72 65 74 75 72     }.      retur
2bd7d 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63  n i;.    }.    c
2bd7e 61 73 65 20 27 5b 27 3a 20 7b 0a 20 20 20 20 20  ase '[': {.     
2bd7f 20 66 6f 72 28 69 3d 31 2c 20 63 3d 7a 5b 30 5d   for(i=1, c=z[0]
2bd80 3b 20 63 21 3d 27 5d 27 20 26 26 20 28 63 3d 7a  ; c!=']' && (c=z
2bd81 5b 69 5d 29 21 3d 30 3b 20 69 2b 2b 29 7b 7d 0a  [i])!=0; i++){}.
2bd82 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65        *tokenType
2bd83 20 3d 20 63 3d 3d 27 5d 27 20 3f 20 54 4b 5f 49   = c==']' ? TK_I
2bd84 44 20 3a 20 54 4b 5f 49 4c 4c 45 47 41 4c 3b 0a  D : TK_ILLEGAL;.
2bd85 20 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a        return i;.
2bd86 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2bd87 3f 27 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b  ?': {.      *tok
2bd88 65 6e 54 79 70 65 20 3d 20 54 4b 5f 56 41 52 49  enType = TK_VARI
2bd89 41 42 4c 45 3b 0a 20 20 20 20 20 20 66 6f 72 28  ABLE;.      for(
2bd8a 69 3d 31 3b 20 69 73 64 69 67 69 74 28 7a 5b 69  i=1; isdigit(z[i
2bd8b 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  ]); i++){}.     
2bd8c 20 72 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d   return i;.    }
2bd8d 0a 20 20 20 20 63 61 73 65 20 27 23 27 3a 20 7b  .    case '#': {
2bd8e 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
2bd8f 69 73 64 69 67 69 74 28 7a 5b 69 5d 29 3b 20 69  isdigit(z[i]); i
2bd90 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 69 66 28 20  ++){}.      if( 
2bd91 69 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  i>1 ){.        /
2bd92 2a 20 50 61 72 61 6d 65 74 65 72 73 20 6f 66 20  * Parameters of 
2bd93 74 68 65 20 66 6f 72 6d 20 23 4e 4e 4e 20 28 77  the form #NNN (w
2bd94 68 65 72 65 20 4e 4e 4e 20 69 73 20 61 20 6e 75  here NNN is a nu
2bd95 6d 62 65 72 29 20 61 72 65 20 75 73 65 64 0a 20  mber) are used. 
2bd96 20 20 20 20 20 20 20 2a 2a 20 69 6e 74 65 72 6e         ** intern
2bd97 61 6c 6c 79 20 62 79 20 73 71 6c 69 74 65 33 4e  ally by sqlite3N
2bd98 65 73 74 65 64 50 61 72 73 65 2e 20 20 2a 2f 0a  estedParse.  */.
2bd99 20 20 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79          *tokenTy
2bd9a 70 65 20 3d 20 54 4b 5f 52 45 47 49 53 54 45 52  pe = TK_REGISTER
2bd9b 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2bd9c 20 69 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20   i;.      }.    
2bd9d 20 20 2f 2a 20 46 61 6c 6c 20 74 68 72 6f 75 67    /* Fall throug
2bd9e 68 20 69 6e 74 6f 20 74 68 65 20 6e 65 78 74 20  h into the next 
2bd9f 63 61 73 65 20 69 66 20 74 68 65 20 27 23 27 20  case if the '#' 
2bda0 69 73 20 6e 6f 74 20 66 6f 6c 6c 6f 77 65 64 20  is not followed 
2bda1 62 79 0a 20 20 20 20 20 20 2a 2a 20 61 20 64 69  by.      ** a di
2bda2 67 69 74 2e 20 54 72 79 20 74 6f 20 6d 61 74 63  git. Try to matc
2bda3 68 20 23 41 41 41 41 20 77 68 65 72 65 20 41 41  h #AAAA where AA
2bda4 41 41 20 69 73 20 61 20 70 61 72 61 6d 65 74 65  AA is a paramete
2bda5 72 20 6e 61 6d 65 2e 20 2a 2f 0a 20 20 20 20 7d  r name. */.    }
2bda6 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f  .#ifndef SQLITE_
2bda7 4f 4d 49 54 5f 54 43 4c 5f 56 41 52 49 41 42 4c  OMIT_TCL_VARIABL
2bda8 45 0a 20 20 20 20 63 61 73 65 20 27 24 27 3a 0a  E.    case '$':.
2bda9 23 65 6e 64 69 66 0a 20 20 20 20 63 61 73 65 20  #endif.    case 
2bdaa 27 40 27 3a 20 20 2f 2a 20 46 6f 72 20 63 6f 6d  '@':  /* For com
2bdab 70 61 74 69 62 69 6c 69 74 79 20 77 69 74 68 20  patibility with 
2bdac 4d 53 20 53 51 4c 20 53 65 72 76 65 72 20 2a 2f  MS SQL Server */
2bdad 0a 20 20 20 20 63 61 73 65 20 27 3a 27 3a 20 7b  .    case ':': {
2bdae 0a 20 20 20 20 20 20 69 6e 74 20 6e 20 3d 20 30  .      int n = 0
2bdaf 3b 0a 20 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79  ;.      *tokenTy
2bdb0 70 65 20 3d 20 54 4b 5f 56 41 52 49 41 42 4c 45  pe = TK_VARIABLE
2bdb1 3b 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b  ;.      for(i=1;
2bdb2 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 3b 20 69 2b   (c=z[i])!=0; i+
2bdb3 2b 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20  +){.        if( 
2bdb4 49 64 43 68 61 72 28 63 29 20 29 7b 0a 20 20 20  IdChar(c) ){.   
2bdb5 20 20 20 20 20 20 20 6e 2b 2b 3b 0a 23 69 66 6e         n++;.#ifn
2bdb6 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2bdb7 54 43 4c 5f 56 41 52 49 41 42 4c 45 0a 20 20 20  TCL_VARIABLE.   
2bdb8 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 63       }else if( c
2bdb9 3d 3d 27 28 27 20 26 26 20 6e 3e 30 20 29 7b 0a  =='(' && n>0 ){.
2bdba 20 20 20 20 20 20 20 20 20 20 64 6f 7b 0a 20 20            do{.  
2bdbb 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20            i++;. 
2bdbc 20 20 20 20 20 20 20 20 20 7d 77 68 69 6c 65 28           }while(
2bdbd 20 28 63 3d 7a 5b 69 5d 29 21 3d 30 20 26 26 20   (c=z[i])!=0 && 
2bdbe 21 69 73 73 70 61 63 65 28 63 29 20 26 26 20 63  !isspace(c) && c
2bdbf 21 3d 27 29 27 20 29 3b 0a 20 20 20 20 20 20 20  !=')' );.       
2bdc0 20 20 20 69 66 28 20 63 3d 3d 27 29 27 20 29 7b     if( c==')' ){
2bdc1 0a 20 20 20 20 20 20 20 20 20 20 20 20 69 2b 2b  .            i++
2bdc2 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73  ;.          }els
2bdc3 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 2a  e{.            *
2bdc4 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49  tokenType = TK_I
2bdc5 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20 20 20 20  LLEGAL;.        
2bdc6 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 62 72    }.          br
2bdc7 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  eak;.        }el
2bdc8 73 65 20 69 66 28 20 63 3d 3d 27 3a 27 20 26 26  se if( c==':' &&
2bdc9 20 7a 5b 69 2b 31 5d 3d 3d 27 3a 27 20 29 7b 0a   z[i+1]==':' ){.
2bdca 20 20 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 23            i++;.#
2bdcb 65 6e 64 69 66 0a 20 20 20 20 20 20 20 20 7d 65  endif.        }e
2bdcc 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 62  lse{.          b
2bdcd 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d 0a  reak;.        }.
2bdce 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
2bdcf 28 20 6e 3d 3d 30 20 29 20 2a 74 6f 6b 65 6e 54  ( n==0 ) *tokenT
2bdd0 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
2bdd1 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
2bdd2 3b 0a 20 20 20 20 7d 0a 23 69 66 6e 64 65 66 20  ;.    }.#ifndef 
2bdd3 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 42 4c 4f 42  SQLITE_OMIT_BLOB
2bdd4 5f 4c 49 54 45 52 41 4c 0a 20 20 20 20 63 61 73  _LITERAL.    cas
2bdd5 65 20 27 78 27 3a 20 63 61 73 65 20 27 58 27 3a  e 'x': case 'X':
2bdd6 20 7b 0a 20 20 20 20 20 20 69 66 28 20 7a 5b 31   {.      if( z[1
2bdd7 5d 3d 3d 27 5c 27 27 20 29 7b 0a 20 20 20 20 20  ]=='\'' ){.     
2bdd8 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20     *tokenType = 
2bdd9 54 4b 5f 42 4c 4f 42 3b 0a 20 20 20 20 20 20 20  TK_BLOB;.       
2bdda 20 66 6f 72 28 69 3d 32 3b 20 28 63 3d 7a 5b 69   for(i=2; (c=z[i
2bddb 5d 29 21 3d 30 20 26 26 20 63 21 3d 27 5c 27 27  ])!=0 && c!='\''
2bddc 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20  ; i++){.        
2bddd 20 20 69 66 28 20 21 69 73 78 64 69 67 69 74 28    if( !isxdigit(
2bdde 63 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  c) ){.          
2bddf 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54    *tokenType = T
2bde0 4b 5f 49 4c 4c 45 47 41 4c 3b 0a 20 20 20 20 20  K_ILLEGAL;.     
2bde1 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 7d       }.        }
2bde2 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 25 32  .        if( i%2
2bde3 20 7c 7c 20 21 63 20 29 20 2a 74 6f 6b 65 6e 54   || !c ) *tokenT
2bde4 79 70 65 20 3d 20 54 4b 5f 49 4c 4c 45 47 41 4c  ype = TK_ILLEGAL
2bde5 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 20  ;.        if( c 
2bde6 29 20 69 2b 2b 3b 0a 20 20 20 20 20 20 20 20 72  ) i++;.        r
2bde7 65 74 75 72 6e 20 69 3b 0a 20 20 20 20 20 20 7d  eturn i;.      }
2bde8 0a 20 20 20 20 20 20 2f 2a 20 4f 74 68 65 72 77  .      /* Otherw
2bde9 69 73 65 20 66 61 6c 6c 20 74 68 72 6f 75 67 68  ise fall through
2bdea 20 74 6f 20 74 68 65 20 6e 65 78 74 20 63 61 73   to the next cas
2bdeb 65 20 2a 2f 0a 20 20 20 20 7d 0a 23 65 6e 64 69  e */.    }.#endi
2bdec 66 0a 20 20 20 20 64 65 66 61 75 6c 74 3a 20 7b  f.    default: {
2bded 0a 20 20 20 20 20 20 69 66 28 20 21 49 64 43 68  .      if( !IdCh
2bdee 61 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20  ar(*z) ){.      
2bdef 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2bdf0 0a 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20  .      for(i=1; 
2bdf1 49 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20 69 2b  IdChar(z[i]); i+
2bdf2 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f 6b 65  +){}.      *toke
2bdf3 6e 54 79 70 65 20 3d 20 6b 65 79 77 6f 72 64 43  nType = keywordC
2bdf4 6f 64 65 28 28 63 68 61 72 2a 29 7a 2c 20 69 29  ode((char*)z, i)
2bdf5 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 69  ;.      return i
2bdf6 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a 74  ;.    }.  }.  *t
2bdf7 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4b 5f 49 4c  okenType = TK_IL
2bdf8 4c 45 47 41 4c 3b 0a 20 20 72 65 74 75 72 6e 20  LEGAL;.  return 
2bdf9 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 75 6e 20  1;.}../*.** Run 
2bdfa 74 68 65 20 70 61 72 73 65 72 20 6f 6e 20 74 68  the parser on th
2bdfb 65 20 67 69 76 65 6e 20 53 51 4c 20 73 74 72 69  e given SQL stri
2bdfc 6e 67 2e 20 20 54 68 65 20 70 61 72 73 65 72 20  ng.  The parser 
2bdfd 73 74 72 75 63 74 75 72 65 20 69 73 0a 2a 2a 20  structure is.** 
2bdfe 70 61 73 73 65 64 20 69 6e 2e 20 20 41 6e 20 53  passed in.  An S
2bdff 51 4c 49 54 45 5f 20 73 74 61 74 75 73 20 63 6f  QLITE_ status co
2be00 64 65 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20  de is returned. 
2be01 20 49 66 20 61 6e 20 65 72 72 6f 72 20 6f 63 63   If an error occ
2be02 75 72 73 0a 2a 2a 20 74 68 65 6e 20 61 6e 20 61  urs.** then an a
2be03 6e 64 20 61 74 74 65 6d 70 74 20 69 73 20 6d 61  nd attempt is ma
2be04 64 65 20 74 6f 20 77 72 69 74 65 20 61 6e 20 65  de to write an e
2be05 72 72 6f 72 20 6d 65 73 73 61 67 65 20 69 6e 74  rror message int
2be06 6f 20 0a 2a 2a 20 6d 65 6d 6f 72 79 20 6f 62 74  o .** memory obt
2be07 61 69 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74  ained from sqlit
2be08 65 33 5f 6d 61 6c 6c 6f 63 28 29 20 61 6e 64 20  e3_malloc() and 
2be09 74 6f 20 6d 61 6b 65 20 2a 70 7a 45 72 72 4d 73  to make *pzErrMs
2be0a 67 20 70 6f 69 6e 74 20 74 6f 20 74 68 61 74 0a  g point to that.
2be0b 2a 2a 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  ** error message
2be0c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
2be0d 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 52  ATE int sqlite3R
2be0e 75 6e 50 61 72 73 65 72 28 50 61 72 73 65 20 2a  unParser(Parse *
2be0f 70 50 61 72 73 65 2c 20 63 6f 6e 73 74 20 63 68  pParse, const ch
2be10 61 72 20 2a 7a 53 71 6c 2c 20 63 68 61 72 20 2a  ar *zSql, char *
2be11 2a 70 7a 45 72 72 4d 73 67 29 7b 0a 20 20 69 6e  *pzErrMsg){.  in
2be12 74 20 6e 45 72 72 20 3d 20 30 3b 0a 20 20 69 6e  t nErr = 0;.  in
2be13 74 20 69 3b 0a 20 20 76 6f 69 64 20 2a 70 45 6e  t i;.  void *pEn
2be14 67 69 6e 65 3b 0a 20 20 69 6e 74 20 74 6f 6b 65  gine;.  int toke
2be15 6e 54 79 70 65 3b 0a 20 20 69 6e 74 20 6c 61 73  nType;.  int las
2be16 74 54 6f 6b 65 6e 50 61 72 73 65 64 20 3d 20 2d  tTokenParsed = -
2be17 31 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  1;.  sqlite3 *db
2be18 20 3d 20 70 50 61 72 73 65 2d 3e 64 62 3b 0a 20   = pParse->db;. 
2be19 20 69 6e 74 20 6d 78 53 71 6c 4c 65 6e 20 3d 20   int mxSqlLen = 
2be1a 64 62 2d 3e 61 4c 69 6d 69 74 5b 53 51 4c 49 54  db->aLimit[SQLIT
2be1b 45 5f 4c 49 4d 49 54 5f 53 51 4c 5f 4c 45 4e 47  E_LIMIT_SQL_LENG
2be1c 54 48 5d 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  TH];..  if( db->
2be1d 61 63 74 69 76 65 56 64 62 65 43 6e 74 3d 3d 30  activeVdbeCnt==0
2be1e 20 29 7b 0a 20 20 20 20 64 62 2d 3e 75 31 2e 69   ){.    db->u1.i
2be1f 73 49 6e 74 65 72 72 75 70 74 65 64 20 3d 20 30  sInterrupted = 0
2be20 3b 0a 20 20 7d 0a 20 20 70 50 61 72 73 65 2d 3e  ;.  }.  pParse->
2be21 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  rc = SQLITE_OK;.
2be22 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69 6c 20    pParse->zTail 
2be23 3d 20 70 50 61 72 73 65 2d 3e 7a 53 71 6c 20 3d  = pParse->zSql =
2be24 20 7a 53 71 6c 3b 0a 20 20 69 20 3d 20 30 3b 0a   zSql;.  i = 0;.
2be25 20 20 61 73 73 65 72 74 28 20 70 7a 45 72 72 4d    assert( pzErrM
2be26 73 67 21 3d 30 20 29 3b 0a 20 20 70 45 6e 67 69  sg!=0 );.  pEngi
2be27 6e 65 20 3d 20 73 71 6c 69 74 65 33 50 61 72 73  ne = sqlite3Pars
2be28 65 72 41 6c 6c 6f 63 28 28 76 6f 69 64 2a 28 2a  erAlloc((void*(*
2be29 29 28 73 69 7a 65 5f 74 29 29 73 71 6c 69 74 65  )(size_t))sqlite
2be2a 33 4d 61 6c 6c 6f 63 29 3b 0a 20 20 69 66 28 20  3Malloc);.  if( 
2be2b 70 45 6e 67 69 6e 65 3d 3d 30 20 29 7b 0a 20 20  pEngine==0 ){.  
2be2c 20 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c    db->mallocFail
2be2d 65 64 20 3d 20 31 3b 0a 20 20 20 20 72 65 74 75  ed = 1;.    retu
2be2e 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
2be2f 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 70  .  }.  assert( p
2be30 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
2be31 6e 2e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 61 73  n.dyn==0 );.  as
2be32 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e 70 4e  sert( pParse->pN
2be33 65 77 54 61 62 6c 65 3d 3d 30 20 29 3b 0a 20 20  ewTable==0 );.  
2be34 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d 3e  assert( pParse->
2be35 70 4e 65 77 54 72 69 67 67 65 72 3d 3d 30 20 29  pNewTrigger==0 )
2be36 3b 0a 20 20 61 73 73 65 72 74 28 20 70 50 61 72  ;.  assert( pPar
2be37 73 65 2d 3e 6e 56 61 72 3d 3d 30 20 29 3b 0a 20  se->nVar==0 );. 
2be38 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65 2d   assert( pParse-
2be39 3e 6e 56 61 72 45 78 70 72 3d 3d 30 20 29 3b 0a  >nVarExpr==0 );.
2be3a 20 20 61 73 73 65 72 74 28 20 70 50 61 72 73 65    assert( pParse
2be3b 2d 3e 6e 56 61 72 45 78 70 72 41 6c 6c 6f 63 3d  ->nVarExprAlloc=
2be3c 3d 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  =0 );.  assert( 
2be3d 70 50 61 72 73 65 2d 3e 61 70 56 61 72 45 78 70  pParse->apVarExp
2be3e 72 3d 3d 30 20 29 3b 0a 20 20 77 68 69 6c 65 28  r==0 );.  while(
2be3f 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c   !db->mallocFail
2be40 65 64 20 26 26 20 7a 53 71 6c 5b 69 5d 21 3d 30  ed && zSql[i]!=0
2be41 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20   ){.    assert( 
2be42 69 3e 3d 30 20 29 3b 0a 20 20 20 20 70 50 61 72  i>=0 );.    pPar
2be43 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e 2e 7a  se->sLastToken.z
2be44 20 3d 20 28 75 38 2a 29 26 7a 53 71 6c 5b 69 5d   = (u8*)&zSql[i]
2be45 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 50  ;.    assert( pP
2be46 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65 6e  arse->sLastToken
2be47 2e 64 79 6e 3d 3d 30 20 29 3b 0a 20 20 20 20 70  .dyn==0 );.    p
2be48 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f 6b 65  Parse->sLastToke
2be49 6e 2e 6e 20 3d 20 73 71 6c 69 74 65 33 47 65 74  n.n = sqlite3Get
2be4a 54 6f 6b 65 6e 28 28 75 6e 73 69 67 6e 65 64 20  Token((unsigned 
2be4b 63 68 61 72 2a 29 26 7a 53 71 6c 5b 69 5d 2c 26  char*)&zSql[i],&
2be4c 74 6f 6b 65 6e 54 79 70 65 29 3b 0a 20 20 20 20  tokenType);.    
2be4d 69 20 2b 3d 20 70 50 61 72 73 65 2d 3e 73 4c 61  i += pParse->sLa
2be4e 73 74 54 6f 6b 65 6e 2e 6e 3b 0a 20 20 20 20 69  stToken.n;.    i
2be4f 66 28 20 69 3e 6d 78 53 71 6c 4c 65 6e 20 29 7b  f( i>mxSqlLen ){
2be50 0a 20 20 20 20 20 20 70 50 61 72 73 65 2d 3e 72  .      pParse->r
2be51 63 20 3d 20 53 51 4c 49 54 45 5f 54 4f 4f 42 49  c = SQLITE_TOOBI
2be52 47 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  G;.      break;.
2be53 20 20 20 20 7d 0a 20 20 20 20 73 77 69 74 63 68      }.    switch
2be54 28 20 74 6f 6b 65 6e 54 79 70 65 20 29 7b 0a 20  ( tokenType ){. 
2be55 20 20 20 20 20 63 61 73 65 20 54 4b 5f 53 50 41       case TK_SPA
2be56 43 45 3a 20 7b 0a 20 20 20 20 20 20 20 20 69 66  CE: {.        if
2be57 28 20 64 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72  ( db->u1.isInter
2be58 72 75 70 74 65 64 20 29 7b 0a 20 20 20 20 20 20  rupted ){.      
2be59 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20 3d      pParse->rc =
2be5a 20 53 51 4c 49 54 45 5f 49 4e 54 45 52 52 55 50   SQLITE_INTERRUP
2be5b 54 3b 0a 20 20 20 20 20 20 20 20 20 20 73 71 6c  T;.          sql
2be5c 69 74 65 33 53 65 74 53 74 72 69 6e 67 28 70 7a  ite3SetString(pz
2be5d 45 72 72 4d 73 67 2c 20 64 62 2c 20 22 69 6e 74  ErrMsg, db, "int
2be5e 65 72 72 75 70 74 22 29 3b 0a 20 20 20 20 20 20  errupt");.      
2be5f 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70      goto abort_p
2be60 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20 7d 0a  arse;.        }.
2be61 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2be62 20 20 20 20 20 7d 0a 20 20 20 20 20 20 63 61 73       }.      cas
2be63 65 20 54 4b 5f 49 4c 4c 45 47 41 4c 3a 20 7b 0a  e TK_ILLEGAL: {.
2be64 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 44          sqlite3D
2be65 62 46 72 65 65 28 64 62 2c 20 2a 70 7a 45 72 72  bFree(db, *pzErr
2be66 4d 73 67 29 3b 0a 20 20 20 20 20 20 20 20 2a 70  Msg);.        *p
2be67 7a 45 72 72 4d 73 67 20 3d 20 73 71 6c 69 74 65  zErrMsg = sqlite
2be68 33 4d 50 72 69 6e 74 66 28 64 62 2c 20 22 75 6e  3MPrintf(db, "un
2be69 72 65 63 6f 67 6e 69 7a 65 64 20 74 6f 6b 65 6e  recognized token
2be6a 3a 20 5c 22 25 54 5c 22 22 2c 0a 20 20 20 20 20  : \"%T\"",.     
2be6b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2be6c 20 20 20 26 70 50 61 72 73 65 2d 3e 73 4c 61 73     &pParse->sLas
2be6d 74 54 6f 6b 65 6e 29 3b 0a 20 20 20 20 20 20 20  tToken);.       
2be6e 20 6e 45 72 72 2b 2b 3b 0a 20 20 20 20 20 20 20   nErr++;.       
2be6f 20 67 6f 74 6f 20 61 62 6f 72 74 5f 70 61 72 73   goto abort_pars
2be70 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  e;.      }.     
2be71 20 63 61 73 65 20 54 4b 5f 53 45 4d 49 3a 20 7b   case TK_SEMI: {
2be72 0a 20 20 20 20 20 20 20 20 70 50 61 72 73 65 2d  .        pParse-
2be73 3e 7a 54 61 69 6c 20 3d 20 26 7a 53 71 6c 5b 69  >zTail = &zSql[i
2be74 5d 3b 0a 20 20 20 20 20 20 20 20 2f 2a 20 46 61  ];.        /* Fa
2be75 6c 6c 20 74 68 72 75 20 69 6e 74 6f 20 74 68 65  ll thru into the
2be76 20 64 65 66 61 75 6c 74 20 63 61 73 65 20 2a 2f   default case */
2be77 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64  .      }.      d
2be78 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20  efault: {.      
2be79 20 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28    sqlite3Parser(
2be7a 70 45 6e 67 69 6e 65 2c 20 74 6f 6b 65 6e 54 79  pEngine, tokenTy
2be7b 70 65 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73  pe, pParse->sLas
2be7c 74 54 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b  tToken, pParse);
2be7d 0a 20 20 20 20 20 20 20 20 6c 61 73 74 54 6f 6b  .        lastTok
2be7e 65 6e 50 61 72 73 65 64 20 3d 20 74 6f 6b 65 6e  enParsed = token
2be7f 54 79 70 65 3b 0a 20 20 20 20 20 20 20 20 69 66  Type;.        if
2be80 28 20 70 50 61 72 73 65 2d 3e 72 63 21 3d 53 51  ( pParse->rc!=SQ
2be81 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2be82 20 20 20 20 20 67 6f 74 6f 20 61 62 6f 72 74 5f       goto abort_
2be83 70 61 72 73 65 3b 0a 20 20 20 20 20 20 20 20 7d  parse;.        }
2be84 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2be85 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
2be86 7d 0a 61 62 6f 72 74 5f 70 61 72 73 65 3a 0a 20  }.abort_parse:. 
2be87 20 69 66 28 20 7a 53 71 6c 5b 69 5d 3d 3d 30 20   if( zSql[i]==0 
2be88 26 26 20 6e 45 72 72 3d 3d 30 20 26 26 20 70 50  && nErr==0 && pP
2be89 61 72 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45  arse->rc==SQLITE
2be8a 5f 4f 4b 20 29 7b 0a 20 20 20 20 69 66 28 20 6c  _OK ){.    if( l
2be8b 61 73 74 54 6f 6b 65 6e 50 61 72 73 65 64 21 3d  astTokenParsed!=
2be8c 54 4b 5f 53 45 4d 49 20 29 7b 0a 20 20 20 20 20  TK_SEMI ){.     
2be8d 20 73 71 6c 69 74 65 33 50 61 72 73 65 72 28 70   sqlite3Parser(p
2be8e 45 6e 67 69 6e 65 2c 20 54 4b 5f 53 45 4d 49 2c  Engine, TK_SEMI,
2be8f 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54 6f   pParse->sLastTo
2be90 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20 20  ken, pParse);.  
2be91 20 20 20 20 70 50 61 72 73 65 2d 3e 7a 54 61 69      pParse->zTai
2be92 6c 20 3d 20 26 7a 53 71 6c 5b 69 5d 3b 0a 20 20  l = &zSql[i];.  
2be93 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33 50    }.    sqlite3P
2be94 61 72 73 65 72 28 70 45 6e 67 69 6e 65 2c 20 30  arser(pEngine, 0
2be95 2c 20 70 50 61 72 73 65 2d 3e 73 4c 61 73 74 54  , pParse->sLastT
2be96 6f 6b 65 6e 2c 20 70 50 61 72 73 65 29 3b 0a 20  oken, pParse);. 
2be97 20 7d 0a 23 69 66 64 65 66 20 59 59 54 52 41 43   }.#ifdef YYTRAC
2be98 4b 4d 41 58 53 54 41 43 4b 44 45 50 54 48 0a 20  KMAXSTACKDEPTH. 
2be99 20 73 71 6c 69 74 65 33 53 74 61 74 75 73 53 65   sqlite3StatusSe
2be9a 74 28 53 51 4c 49 54 45 5f 53 54 41 54 55 53 5f  t(SQLITE_STATUS_
2be9b 50 41 52 53 45 52 5f 53 54 41 43 4b 2c 0a 20 20  PARSER_STACK,.  
2be9c 20 20 20 20 73 71 6c 69 74 65 33 50 61 72 73 65      sqlite3Parse
2be9d 72 53 74 61 63 6b 50 65 61 6b 28 70 45 6e 67 69  rStackPeak(pEngi
2be9e 6e 65 29 0a 20 20 29 3b 0a 23 65 6e 64 69 66 20  ne).  );.#endif 
2be9f 2f 2a 20 59 59 44 45 42 55 47 20 2a 2f 0a 20 20  /* YYDEBUG */.  
2bea0 73 71 6c 69 74 65 33 50 61 72 73 65 72 46 72 65  sqlite3ParserFre
2bea1 65 28 70 45 6e 67 69 6e 65 2c 20 73 71 6c 69 74  e(pEngine, sqlit
2bea2 65 33 5f 66 72 65 65 29 3b 0a 20 20 69 66 28 20  e3_free);.  if( 
2bea3 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64  db->mallocFailed
2bea4 20 29 7b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e   ){.    pParse->
2bea5 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  rc = SQLITE_NOME
2bea6 4d 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50 61  M;.  }.  if( pPa
2bea7 72 73 65 2d 3e 72 63 21 3d 53 51 4c 49 54 45 5f  rse->rc!=SQLITE_
2bea8 4f 4b 20 26 26 20 70 50 61 72 73 65 2d 3e 72 63  OK && pParse->rc
2bea9 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 26 26  !=SQLITE_DONE &&
2beaa 20 70 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67   pParse->zErrMsg
2beab 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ==0 ){.    sqlit
2beac 65 33 53 65 74 53 74 72 69 6e 67 28 26 70 50 61  e3SetString(&pPa
2bead 72 73 65 2d 3e 7a 45 72 72 4d 73 67 2c 20 64 62  rse->zErrMsg, db
2beae 2c 20 22 25 73 22 2c 20 73 71 6c 69 74 65 33 45  , "%s", sqlite3E
2beaf 72 72 53 74 72 28 70 50 61 72 73 65 2d 3e 72 63  rrStr(pParse->rc
2beb0 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 50  ));.  }.  if( pP
2beb1 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 20 29 7b  arse->zErrMsg ){
2beb2 0a 20 20 20 20 69 66 28 20 2a 70 7a 45 72 72 4d  .    if( *pzErrM
2beb3 73 67 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 2a  sg==0 ){.      *
2beb4 70 7a 45 72 72 4d 73 67 20 3d 20 70 50 61 72 73  pzErrMsg = pPars
2beb5 65 2d 3e 7a 45 72 72 4d 73 67 3b 0a 20 20 20 20  e->zErrMsg;.    
2beb6 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 73 71 6c  }else{.      sql
2beb7 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70  ite3DbFree(db, p
2beb8 50 61 72 73 65 2d 3e 7a 45 72 72 4d 73 67 29 3b  Parse->zErrMsg);
2beb9 0a 20 20 20 20 7d 0a 20 20 20 20 70 50 61 72 73  .    }.    pPars
2beba 65 2d 3e 7a 45 72 72 4d 73 67 20 3d 20 30 3b 0a  e->zErrMsg = 0;.
2bebb 20 20 20 20 6e 45 72 72 2b 2b 3b 0a 20 20 7d 0a      nErr++;.  }.
2bebc 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 70 56    if( pParse->pV
2bebd 64 62 65 20 26 26 20 70 50 61 72 73 65 2d 3e 6e  dbe && pParse->n
2bebe 45 72 72 3e 30 20 26 26 20 70 50 61 72 73 65 2d  Err>0 && pParse-
2bebf 3e 6e 65 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20  >nested==0 ){.  
2bec0 20 20 73 71 6c 69 74 65 33 56 64 62 65 44 65 6c    sqlite3VdbeDel
2bec1 65 74 65 28 70 50 61 72 73 65 2d 3e 70 56 64 62  ete(pParse->pVdb
2bec2 65 29 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e  e);.    pParse->
2bec3 70 56 64 62 65 20 3d 20 30 3b 0a 20 20 7d 0a 23  pVdbe = 0;.  }.#
2bec4 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d  ifndef SQLITE_OM
2bec5 49 54 5f 53 48 41 52 45 44 5f 43 41 43 48 45 0a  IT_SHARED_CACHE.
2bec6 20 20 69 66 28 20 70 50 61 72 73 65 2d 3e 6e 65    if( pParse->ne
2bec7 73 74 65 64 3d 3d 30 20 29 7b 0a 20 20 20 20 73  sted==0 ){.    s
2bec8 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2bec9 20 70 50 61 72 73 65 2d 3e 61 54 61 62 6c 65 4c   pParse->aTableL
2beca 6f 63 6b 29 3b 0a 20 20 20 20 70 50 61 72 73 65  ock);.    pParse
2becb 2d 3e 61 54 61 62 6c 65 4c 6f 63 6b 20 3d 20 30  ->aTableLock = 0
2becc 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 6e 54  ;.    pParse->nT
2becd 61 62 6c 65 4c 6f 63 6b 20 3d 20 30 3b 0a 20 20  ableLock = 0;.  
2bece 7d 0a 23 65 6e 64 69 66 0a 23 69 66 6e 64 65 66  }.#endif.#ifndef
2becf 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 56 49 52   SQLITE_OMIT_VIR
2bed0 54 55 41 4c 54 41 42 4c 45 0a 20 20 73 71 6c 69  TUALTABLE.  sqli
2bed1 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 50  te3DbFree(db, pP
2bed2 61 72 73 65 2d 3e 61 70 56 74 61 62 4c 6f 63 6b  arse->apVtabLock
2bed3 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 69 66 28  );.#endif..  if(
2bed4 20 21 49 4e 5f 44 45 43 4c 41 52 45 5f 56 54 41   !IN_DECLARE_VTA
2bed5 42 20 29 7b 0a 20 20 20 20 2f 2a 20 49 66 20 74  B ){.    /* If t
2bed6 68 65 20 70 50 61 72 73 65 2d 3e 64 65 63 6c 61  he pParse->decla
2bed7 72 65 56 74 61 62 20 66 6c 61 67 20 69 73 20 73  reVtab flag is s
2bed8 65 74 2c 20 64 6f 20 6e 6f 74 20 64 65 6c 65 74  et, do not delet
2bed9 65 20 61 6e 79 20 74 61 62 6c 65 20 0a 20 20 20  e any table .   
2beda 20 2a 2a 20 73 74 72 75 63 74 75 72 65 20 62 75   ** structure bu
2bedb 69 6c 74 20 75 70 20 69 6e 20 70 50 61 72 73 65  ilt up in pParse
2bedc 2d 3e 70 4e 65 77 54 61 62 6c 65 2e 20 54 68 65  ->pNewTable. The
2bedd 20 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20 28 73   calling code (s
2bede 65 65 20 76 74 61 62 2e 63 29 0a 20 20 20 20 2a  ee vtab.c).    *
2bedf 2a 20 77 69 6c 6c 20 74 61 6b 65 20 72 65 73 70  * will take resp
2bee0 6f 6e 73 69 62 69 6c 69 74 79 20 66 6f 72 20 66  onsibility for f
2bee1 72 65 65 69 6e 67 20 74 68 65 20 54 61 62 6c 65  reeing the Table
2bee2 20 73 74 72 75 63 74 75 72 65 2e 0a 20 20 20 20   structure..    
2bee3 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 44 65  */.    sqlite3De
2bee4 6c 65 74 65 54 61 62 6c 65 28 70 50 61 72 73 65  leteTable(pParse
2bee5 2d 3e 70 4e 65 77 54 61 62 6c 65 29 3b 0a 20 20  ->pNewTable);.  
2bee6 7d 0a 0a 20 20 73 71 6c 69 74 65 33 44 65 6c 65  }..  sqlite3Dele
2bee7 74 65 54 72 69 67 67 65 72 28 64 62 2c 20 70 50  teTrigger(db, pP
2bee8 61 72 73 65 2d 3e 70 4e 65 77 54 72 69 67 67 65  arse->pNewTrigge
2bee9 72 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  r);.  sqlite3DbF
2beea 72 65 65 28 64 62 2c 20 70 50 61 72 73 65 2d 3e  ree(db, pParse->
2beeb 61 70 56 61 72 45 78 70 72 29 3b 0a 20 20 73 71  apVarExpr);.  sq
2beec 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c 20  lite3DbFree(db, 
2beed 70 50 61 72 73 65 2d 3e 61 41 6c 69 61 73 29 3b  pParse->aAlias);
2beee 0a 20 20 77 68 69 6c 65 28 20 70 50 61 72 73 65  .  while( pParse
2beef 2d 3e 70 5a 6f 6d 62 69 65 54 61 62 20 29 7b 0a  ->pZombieTab ){.
2bef0 20 20 20 20 54 61 62 6c 65 20 2a 70 20 3d 20 70      Table *p = p
2bef1 50 61 72 73 65 2d 3e 70 5a 6f 6d 62 69 65 54 61  Parse->pZombieTa
2bef2 62 3b 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 70  b;.    pParse->p
2bef3 5a 6f 6d 62 69 65 54 61 62 20 3d 20 70 2d 3e 70  ZombieTab = p->p
2bef4 4e 65 78 74 5a 6f 6d 62 69 65 3b 0a 20 20 20 20  NextZombie;.    
2bef5 73 71 6c 69 74 65 33 44 65 6c 65 74 65 54 61 62  sqlite3DeleteTab
2bef6 6c 65 28 70 29 3b 0a 20 20 7d 0a 20 20 69 66 28  le(p);.  }.  if(
2bef7 20 6e 45 72 72 3e 30 20 26 26 20 28 70 50 61 72   nErr>0 && (pPar
2bef8 73 65 2d 3e 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  se->rc==SQLITE_O
2bef9 4b 20 7c 7c 20 70 50 61 72 73 65 2d 3e 72 63 3d  K || pParse->rc=
2befa 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 29 20 29 7b  =SQLITE_DONE) ){
2befb 0a 20 20 20 20 70 50 61 72 73 65 2d 3e 72 63 20  .    pParse->rc 
2befc 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a  = SQLITE_ERROR;.
2befd 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 6e 45 72    }.  return nEr
2befe 72 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  r;.}../*********
2beff 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 74 6f 6b  ***** End of tok
2bf00 65 6e 69 7a 65 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a  enize.c ********
2bf01 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf02 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf03 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****/./*********
2bf04 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65  ***** Begin file
2bf05 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a   complete.c ****
2bf06 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf07 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf08 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 31  ****/./*.** 2001
2bf09 20 53 65 70 74 65 6d 62 65 72 20 31 35 0a 2a 2a   September 15.**
2bf0a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64  .** The author d
2bf0b 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67  isclaims copyrig
2bf0c 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63  ht to this sourc
2bf0d 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63  e code.  In plac
2bf0e 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20  e of.** a legal 
2bf0f 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20  notice, here is 
2bf10 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a  a blessing:.**.*
2bf11 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20  *    May you do 
2bf12 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69  good and not evi
2bf13 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  l..**    May you
2bf14 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73   find forgivenes
2bf15 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61  s for yourself a
2bf16 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72  nd forgive other
2bf17 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  s..**    May you
2bf18 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e   share freely, n
2bf19 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65  ever taking more
2bf1a 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a   than you give..
2bf1b 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **.*************
2bf1c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf1d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf1e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2bf1f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20  ************.** 
2bf20 41 6e 20 74 6f 6b 65 6e 69 7a 65 72 20 66 6f 72  An tokenizer for
2bf21 20 53 51 4c 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20   SQL.**.** This 
2bf22 66 69 6c 65 20 63 6f 6e 74 61 69 6e 73 20 43 20  file contains C 
2bf23 63 6f 64 65 20 74 68 61 74 20 69 6d 70 6c 65 6d  code that implem
2bf24 65 6e 74 73 20 74 68 65 20 73 71 6c 69 74 65 33  ents the sqlite3
2bf25 5f 63 6f 6d 70 6c 65 74 65 28 29 20 41 50 49 2e  _complete() API.
2bf26 0a 2a 2a 20 54 68 69 73 20 63 6f 64 65 20 75 73  .** This code us
2bf27 65 64 20 74 6f 20 62 65 20 70 61 72 74 20 6f 66  ed to be part of
2bf28 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 2e 63   the tokenizer.c
2bf29 20 73 6f 75 72 63 65 20 66 69 6c 65 2e 20 20 42   source file.  B
2bf2a 75 74 20 62 79 0a 2a 2a 20 73 65 70 61 72 61 74  ut by.** separat
2bf2b 69 6e 67 20 69 74 20 6f 75 74 2c 20 74 68 65 20  ing it out, the 
2bf2c 63 6f 64 65 20 77 69 6c 6c 20 62 65 20 61 75 74  code will be aut
2bf2d 6f 6d 61 74 69 63 61 6c 6c 79 20 6f 6d 69 74 74  omatically omitt
2bf2e 65 64 20 66 72 6f 6d 0a 2a 2a 20 73 74 61 74 69  ed from.** stati
2bf2f 63 20 6c 69 6e 6b 73 20 74 68 61 74 20 64 6f 20  c links that do 
2bf30 6e 6f 74 20 75 73 65 20 69 74 2e 0a 2a 2a 0a 2a  not use it..**.*
2bf31 2a 20 24 49 64 3a 20 63 6f 6d 70 6c 65 74 65 2e  * $Id: complete.
2bf32 63 2c 76 20 31 2e 37 20 32 30 30 38 2f 30 36 2f  c,v 1.7 2008/06/
2bf33 31 33 20 31 38 3a 32 34 3a 32 37 20 64 72 68 20  13 18:24:27 drh 
2bf34 45 78 70 20 24 0a 2a 2f 0a 23 69 66 6e 64 65 66  Exp $.*/.#ifndef
2bf35 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
2bf36 50 4c 45 54 45 0a 0a 2f 2a 0a 2a 2a 20 54 68 69  PLETE../*.** Thi
2bf37 73 20 69 73 20 64 65 66 69 6e 65 64 20 69 6e 20  s is defined in 
2bf38 74 6f 6b 65 6e 69 7a 65 2e 63 2e 20 20 57 65 20  tokenize.c.  We 
2bf39 6a 75 73 74 20 68 61 76 65 20 74 6f 20 69 6d 70  just have to imp
2bf3a 6f 72 74 20 74 68 65 20 64 65 66 69 6e 69 74 69  ort the definiti
2bf3b 6f 6e 2e 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 53  on..*/.#ifndef S
2bf3c 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49  QLITE_AMALGAMATI
2bf3d 4f 4e 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  ON.#ifdef SQLITE
2bf3e 5f 41 53 43 49 49 0a 53 51 4c 49 54 45 5f 50 52  _ASCII.SQLITE_PR
2bf3f 49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72  IVATE const char
2bf40 20 73 71 6c 69 74 65 33 49 73 41 73 63 69 69 49   sqlite3IsAsciiI
2bf41 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65  dChar[];.#define
2bf42 20 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63   IdChar(C)  (((c
2bf43 3d 43 29 26 30 78 38 30 29 21 3d 30 20 7c 7c 20  =C)&0x80)!=0 || 
2bf44 28 63 3e 30 78 31 66 20 26 26 20 73 71 6c 69 74  (c>0x1f && sqlit
2bf45 65 33 49 73 41 73 63 69 69 49 64 43 68 61 72 5b  e3IsAsciiIdChar[
2bf46 63 2d 30 78 32 30 5d 29 29 0a 23 65 6e 64 69 66  c-0x20])).#endif
2bf47 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2bf48 42 43 44 49 43 0a 53 51 4c 49 54 45 5f 50 52 49  BCDIC.SQLITE_PRI
2bf49 56 41 54 45 20 63 6f 6e 73 74 20 63 68 61 72 20  VATE const char 
2bf4a 73 71 6c 69 74 65 33 49 73 45 62 63 64 69 63 49  sqlite3IsEbcdicI
2bf4b 64 43 68 61 72 5b 5d 3b 0a 23 64 65 66 69 6e 65  dChar[];.#define
2bf4c 20 49 64 43 68 61 72 28 43 29 20 20 28 28 28 63   IdChar(C)  (((c
2bf4d 3d 43 29 3e 3d 30 78 34 32 20 26 26 20 73 71 6c  =C)>=0x42 && sql
2bf4e 69 74 65 33 49 73 45 62 63 64 69 63 49 64 43 68  ite3IsEbcdicIdCh
2bf4f 61 72 5b 63 2d 30 78 34 30 5d 29 29 0a 23 65 6e  ar[c-0x40])).#en
2bf50 64 69 66 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51  dif.#endif /* SQ
2bf51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41 54 49 4f  LITE_AMALGAMATIO
2bf52 4e 20 2a 2f 0a 0a 0a 2f 2a 0a 2a 2a 20 54 6f 6b  N */.../*.** Tok
2bf53 65 6e 20 74 79 70 65 73 20 75 73 65 64 20 62 79  en types used by
2bf54 20 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d   the sqlite3_com
2bf55 70 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 2e  plete() routine.
2bf56 20 20 53 65 65 20 74 68 65 20 68 65 61 64 65 72    See the header
2bf57 0a 2a 2a 20 63 6f 6d 6d 65 6e 74 73 20 6f 6e 20  .** comments on 
2bf58 74 68 61 74 20 70 72 6f 63 65 64 75 72 65 20 66  that procedure f
2bf59 6f 72 20 61 64 64 69 74 69 6f 6e 61 6c 20 69 6e  or additional in
2bf5a 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2f 0a 23 64  formation..*/.#d
2bf5b 65 66 69 6e 65 20 74 6b 53 45 4d 49 20 20 20 20  efine tkSEMI    
2bf5c 30 0a 23 64 65 66 69 6e 65 20 74 6b 57 53 20 20  0.#define tkWS  
2bf5d 20 20 20 20 31 0a 23 64 65 66 69 6e 65 20 74 6b      1.#define tk
2bf5e 4f 54 48 45 52 20 20 20 32 0a 23 64 65 66 69 6e  OTHER   2.#defin
2bf5f 65 20 74 6b 45 58 50 4c 41 49 4e 20 33 0a 23 64  e tkEXPLAIN 3.#d
2bf60 65 66 69 6e 65 20 74 6b 43 52 45 41 54 45 20 20  efine tkCREATE  
2bf61 34 0a 23 64 65 66 69 6e 65 20 74 6b 54 45 4d 50  4.#define tkTEMP
2bf62 20 20 20 20 35 0a 23 64 65 66 69 6e 65 20 74 6b      5.#define tk
2bf63 54 52 49 47 47 45 52 20 36 0a 23 64 65 66 69 6e  TRIGGER 6.#defin
2bf64 65 20 74 6b 45 4e 44 20 20 20 20 20 37 0a 0a 2f  e tkEND     7../
2bf65 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45  *.** Return TRUE
2bf66 20 69 66 20 74 68 65 20 67 69 76 65 6e 20 53 51   if the given SQ
2bf67 4c 20 73 74 72 69 6e 67 20 65 6e 64 73 20 69 6e  L string ends in
2bf68 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a   a semicolon..**
2bf69 0a 2a 2a 20 53 70 65 63 69 61 6c 20 68 61 6e 64  .** Special hand
2bf6a 6c 69 6e 67 20 69 73 20 72 65 71 75 69 72 65 20  ling is require 
2bf6b 66 6f 72 20 43 52 45 41 54 45 20 54 52 49 47 47  for CREATE TRIGG
2bf6c 45 52 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a 2a  ER statements..*
2bf6d 2a 20 57 68 65 6e 65 76 65 72 20 74 68 65 20 43  * Whenever the C
2bf6e 52 45 41 54 45 20 54 52 49 47 47 45 52 20 6b 65  REATE TRIGGER ke
2bf6f 79 77 6f 72 64 73 20 61 72 65 20 73 65 65 6e 2c  ywords are seen,
2bf70 20 74 68 65 20 73 74 61 74 65 6d 65 6e 74 0a 2a   the statement.*
2bf71 2a 20 6d 75 73 74 20 65 6e 64 20 77 69 74 68 20  * must end with 
2bf72 22 3b 45 4e 44 3b 22 2e 0a 2a 2a 0a 2a 2a 20 54  ";END;"..**.** T
2bf73 68 69 73 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  his implementati
2bf74 6f 6e 20 75 73 65 73 20 61 20 73 74 61 74 65 20  on uses a state 
2bf75 6d 61 63 68 69 6e 65 20 77 69 74 68 20 37 20 73  machine with 7 s
2bf76 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 28  tates:.**.**   (
2bf77 30 29 20 53 54 41 52 54 20 20 20 20 20 41 74 20  0) START     At 
2bf78 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f 72  the beginning or
2bf79 20 65 6e 64 20 6f 66 20 61 6e 20 53 51 4c 20 73   end of an SQL s
2bf7a 74 61 74 65 6d 65 6e 74 2e 20 20 54 68 69 73 20  tatement.  This 
2bf7b 72 6f 75 74 69 6e 65 0a 2a 2a 20 20 20 20 20 20  routine.**      
2bf7c 20 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72             retur
2bf7d 6e 73 20 31 20 69 66 20 69 74 20 65 6e 64 73 20  ns 1 if it ends 
2bf7e 69 6e 20 74 68 65 20 53 54 41 52 54 20 73 74 61  in the START sta
2bf7f 74 65 20 61 6e 64 20 30 20 69 66 20 69 74 20 65  te and 0 if it e
2bf80 6e 64 73 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  nds.**          
2bf81 20 20 20 20 20 20 20 69 6e 20 61 6e 79 20 6f 74         in any ot
2bf82 68 65 72 20 73 74 61 74 65 2e 0a 2a 2a 0a 2a 2a  her state..**.**
2bf83 20 20 20 28 31 29 20 4e 4f 52 4d 41 4c 20 20 20     (1) NORMAL   
2bf84 20 57 65 20 61 72 65 20 69 6e 20 74 68 65 20 6d   We are in the m
2bf85 69 64 64 6c 65 20 6f 66 20 73 74 61 74 65 6d 65  iddle of stateme
2bf86 6e 74 20 77 68 69 63 68 20 65 6e 64 73 20 77 69  nt which ends wi
2bf87 74 68 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 20  th a single.**  
2bf88 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2bf89 65 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20  emicolon..**.** 
2bf8a 20 20 28 32 29 20 45 58 50 4c 41 49 4e 20 20 20    (2) EXPLAIN   
2bf8b 54 68 65 20 6b 65 79 77 6f 72 64 20 45 58 50 4c  The keyword EXPL
2bf8c 41 49 4e 20 68 61 73 20 62 65 65 6e 20 73 65 65  AIN has been see
2bf8d 6e 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69  n at the beginni
2bf8e 6e 67 20 6f 66 20 0a 2a 2a 20 20 20 20 20 20 20  ng of .**       
2bf8f 20 20 20 20 20 20 20 20 20 20 61 20 73 74 61 74            a stat
2bf90 65 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 20 20 28  ement..**.**   (
2bf91 33 29 20 43 52 45 41 54 45 20 20 20 20 54 68 65  3) CREATE    The
2bf92 20 6b 65 79 77 6f 72 64 20 43 52 45 41 54 45 20   keyword CREATE 
2bf93 68 61 73 20 62 65 65 6e 20 73 65 65 6e 20 61 74  has been seen at
2bf94 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 6f   the beginning o
2bf95 66 20 61 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  f a.**          
2bf96 20 20 20 20 20 20 20 73 74 61 74 65 6d 65 6e 74         statement
2bf97 2c 20 70 6f 73 73 69 62 6c 79 20 70 72 65 63 65  , possibly prece
2bf98 65 64 65 64 20 62 79 20 45 58 50 4c 41 49 4e 20  eded by EXPLAIN 
2bf99 61 6e 64 2f 6f 72 20 66 6f 6c 6c 6f 77 65 64 20  and/or followed 
2bf9a 62 79 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20  by.**           
2bf9b 20 20 20 20 20 20 54 45 4d 50 20 6f 72 20 54 45        TEMP or TE
2bf9c 4d 50 4f 52 41 52 59 0a 2a 2a 0a 2a 2a 20 20 20  MPORARY.**.**   
2bf9d 28 34 29 20 54 52 49 47 47 45 52 20 20 20 57 65  (4) TRIGGER   We
2bf9e 20 61 72 65 20 69 6e 20 74 68 65 20 6d 69 64 64   are in the midd
2bf9f 6c 65 20 6f 66 20 61 20 74 72 69 67 67 65 72 20  le of a trigger 
2bfa0 64 65 66 69 6e 69 74 69 6f 6e 20 74 68 61 74 20  definition that 
2bfa1 6d 75 73 74 20 62 65 0a 2a 2a 20 20 20 20 20 20  must be.**      
2bfa2 20 20 20 20 20 20 20 20 20 20 20 65 6e 64 65 64             ended
2bfa3 20 62 79 20 61 20 73 65 6d 69 63 6f 6c 6f 6e 2c   by a semicolon,
2bfa4 20 74 68 65 20 6b 65 79 77 6f 72 64 20 45 4e 44   the keyword END
2bfa5 2c 20 61 6e 64 20 61 6e 6f 74 68 65 72 20 73 65  , and another se
2bfa6 6d 69 63 6f 6c 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 20  micolon..**.**  
2bfa7 20 28 35 29 20 53 45 4d 49 20 20 20 20 20 20 57   (5) SEMI      W
2bfa8 65 27 76 65 20 73 65 65 6e 20 74 68 65 20 66 69  e've seen the fi
2bfa9 72 73 74 20 73 65 6d 69 63 6f 6c 6f 6e 20 69 6e  rst semicolon in
2bfaa 20 74 68 65 20 22 3b 45 4e 44 3b 22 20 74 68 61   the ";END;" tha
2bfab 74 20 6f 63 63 75 72 73 20 61 74 0a 2a 2a 20 20  t occurs at.**  
2bfac 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2bfad 68 65 20 65 6e 64 20 6f 66 20 61 20 74 72 69 67  he end of a trig
2bfae 67 65 72 20 64 65 66 69 6e 69 74 69 6f 6e 2e 0a  ger definition..
2bfaf 2a 2a 0a 2a 2a 20 20 20 28 36 29 20 45 4e 44 20  **.**   (6) END 
2bfb0 20 20 20 20 20 20 57 65 27 76 65 20 73 65 65 6e        We've seen
2bfb1 20 74 68 65 20 22 3b 45 4e 44 22 20 6f 66 20 74   the ";END" of t
2bfb2 68 65 20 22 3b 45 4e 44 3b 22 20 74 68 61 74 20  he ";END;" that 
2bfb3 6f 63 63 75 72 73 20 61 74 20 74 68 65 20 65 6e  occurs at the en
2bfb4 64 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  d.**            
2bfb5 20 20 20 20 20 6f 66 20 61 20 74 72 69 67 67 65       of a trigge
2bfb6 72 20 64 69 66 69 6e 69 74 69 6f 6e 2e 0a 2a 2a  r difinition..**
2bfb7 0a 2a 2a 20 54 72 61 6e 73 69 74 69 6f 6e 73 20  .** Transitions 
2bfb8 62 65 74 77 65 65 6e 20 73 74 61 74 65 73 20 61  between states a
2bfb9 62 6f 76 65 20 61 72 65 20 64 65 74 65 72 6d 69  bove are determi
2bfba 6e 65 64 20 62 79 20 74 6f 6b 65 6e 73 20 65 78  ned by tokens ex
2bfbb 74 72 61 63 74 65 64 0a 2a 2a 20 66 72 6f 6d 20  tracted.** from 
2bfbc 74 68 65 20 69 6e 70 75 74 2e 20 20 54 68 65 20  the input.  The 
2bfbd 66 6f 6c 6c 6f 77 69 6e 67 20 74 6f 6b 65 6e 73  following tokens
2bfbe 20 61 72 65 20 73 69 67 6e 69 66 69 63 61 6e 74   are significant
2bfbf 3a 0a 2a 2a 0a 2a 2a 20 20 20 28 30 29 20 74 6b  :.**.**   (0) tk
2bfc0 53 45 4d 49 20 20 20 20 20 20 41 20 73 65 6d 69  SEMI      A semi
2bfc1 63 6f 6c 6f 6e 2e 0a 2a 2a 20 20 20 28 31 29 20  colon..**   (1) 
2bfc2 74 6b 57 53 20 20 20 20 20 20 20 20 57 68 69 74  tkWS        Whit
2bfc3 65 73 70 61 63 65 0a 2a 2a 20 20 20 28 32 29 20  espace.**   (2) 
2bfc4 74 6b 4f 54 48 45 52 20 20 20 20 20 41 6e 79 20  tkOTHER     Any 
2bfc5 6f 74 68 65 72 20 53 51 4c 20 74 6f 6b 65 6e 2e  other SQL token.
2bfc6 0a 2a 2a 20 20 20 28 33 29 20 74 6b 45 58 50 4c  .**   (3) tkEXPL
2bfc7 41 49 4e 20 20 20 54 68 65 20 22 65 78 70 6c 61  AIN   The "expla
2bfc8 69 6e 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20  in" keyword..** 
2bfc9 20 20 28 34 29 20 74 6b 43 52 45 41 54 45 20 20    (4) tkCREATE  
2bfca 20 20 54 68 65 20 22 63 72 65 61 74 65 22 20 6b    The "create" k
2bfcb 65 79 77 6f 72 64 2e 0a 2a 2a 20 20 20 28 35 29  eyword..**   (5)
2bfcc 20 74 6b 54 45 4d 50 20 20 20 20 20 20 54 68 65   tkTEMP      The
2bfcd 20 22 74 65 6d 70 22 20 6f 72 20 22 74 65 6d 70   "temp" or "temp
2bfce 6f 72 61 72 79 22 20 6b 65 79 77 6f 72 64 2e 0a  orary" keyword..
2bfcf 2a 2a 20 20 20 28 36 29 20 74 6b 54 52 49 47 47  **   (6) tkTRIGG
2bfd0 45 52 20 20 20 54 68 65 20 22 74 72 69 67 67 65  ER   The "trigge
2bfd1 72 22 20 6b 65 79 77 6f 72 64 2e 0a 2a 2a 20 20  r" keyword..**  
2bfd2 20 28 37 29 20 74 6b 45 4e 44 20 20 20 20 20 20   (7) tkEND      
2bfd3 20 54 68 65 20 22 65 6e 64 22 20 6b 65 79 77 6f   The "end" keywo
2bfd4 72 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 69 74 65 73  rd..**.** Whites
2bfd5 70 61 63 65 20 6e 65 76 65 72 20 63 61 75 73 65  pace never cause
2bfd6 73 20 61 20 73 74 61 74 65 20 74 72 61 6e 73 69  s a state transi
2bfd7 74 69 6f 6e 20 61 6e 64 20 69 73 20 61 6c 77 61  tion and is alwa
2bfd8 79 73 20 69 67 6e 6f 72 65 64 2e 0a 2a 2a 0a 2a  ys ignored..**.*
2bfd9 2a 20 49 66 20 77 65 20 63 6f 6d 70 69 6c 65 20  * If we compile 
2bfda 77 69 74 68 20 53 51 4c 49 54 45 5f 4f 4d 49 54  with SQLITE_OMIT
2bfdb 5f 54 52 49 47 47 45 52 2c 20 61 6c 6c 20 6f 66  _TRIGGER, all of
2bfdc 20 74 68 65 20 63 6f 6d 70 75 74 61 74 69 6f 6e   the computation
2bfdd 20 6e 65 65 64 65 64 0a 2a 2a 20 74 6f 20 72 65   needed.** to re
2bfde 63 6f 67 6e 69 7a 65 20 74 68 65 20 65 6e 64 20  cognize the end 
2bfdf 6f 66 20 61 20 74 72 69 67 67 65 72 20 63 61 6e  of a trigger can
2bfe0 20 62 65 20 6f 6d 69 74 74 65 64 2e 20 20 41 6c   be omitted.  Al
2bfe1 6c 20 77 65 20 68 61 76 65 20 74 6f 20 64 6f 0a  l we have to do.
2bfe2 2a 2a 20 69 73 20 6c 6f 6f 6b 20 66 6f 72 20 61  ** is look for a
2bfe3 20 73 65 6d 69 63 6f 6c 6f 6e 20 74 68 61 74 20   semicolon that 
2bfe4 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66 20 61  is not part of a
2bfe5 6e 20 73 74 72 69 6e 67 20 6f 72 20 63 6f 6d 6d  n string or comm
2bfe6 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  ent..*/.SQLITE_A
2bfe7 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63  PI int sqlite3_c
2bfe8 6f 6d 70 6c 65 74 65 28 63 6f 6e 73 74 20 63 68  omplete(const ch
2bfe9 61 72 20 2a 7a 53 71 6c 29 7b 0a 20 20 75 38 20  ar *zSql){.  u8 
2bfea 73 74 61 74 65 20 3d 20 30 3b 20 20 20 2f 2a 20  state = 0;   /* 
2bfeb 43 75 72 72 65 6e 74 20 73 74 61 74 65 2c 20 75  Current state, u
2bfec 73 69 6e 67 20 6e 75 6d 62 65 72 73 20 64 65 66  sing numbers def
2bfed 69 6e 65 64 20 69 6e 20 68 65 61 64 65 72 20 63  ined in header c
2bfee 6f 6d 6d 65 6e 74 20 2a 2f 0a 20 20 75 38 20 74  omment */.  u8 t
2bfef 6f 6b 65 6e 3b 20 20 20 20 20 20 20 2f 2a 20 56  oken;       /* V
2bff0 61 6c 75 65 20 6f 66 20 74 68 65 20 6e 65 78 74  alue of the next
2bff1 20 74 6f 6b 65 6e 20 2a 2f 0a 0a 23 69 66 6e 64   token */..#ifnd
2bff2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54  ef SQLITE_OMIT_T
2bff3 52 49 47 47 45 52 0a 20 20 2f 2a 20 41 20 63 6f  RIGGER.  /* A co
2bff4 6d 70 6c 65 78 20 73 74 61 74 65 6d 65 6e 74 20  mplex statement 
2bff5 6d 61 63 68 69 6e 65 20 75 73 65 64 20 74 6f 20  machine used to 
2bff6 64 65 74 65 63 74 20 74 68 65 20 65 6e 64 20 6f  detect the end o
2bff7 66 20 61 20 43 52 45 41 54 45 20 54 52 49 47 47  f a CREATE TRIGG
2bff8 45 52 0a 20 20 2a 2a 20 73 74 61 74 65 6d 65 6e  ER.  ** statemen
2bff9 74 2e 20 20 54 68 69 73 20 69 73 20 74 68 65 20  t.  This is the 
2bffa 6e 6f 72 6d 61 6c 20 63 61 73 65 2e 0a 20 20 2a  normal case..  *
2bffb 2f 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74  /.  static const
2bffc 20 75 38 20 74 72 61 6e 73 5b 37 5d 5b 38 5d 20   u8 trans[7][8] 
2bffd 3d 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  = {.            
2bffe 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b 65           /* Toke
2bfff 6e 3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n:              
2c000 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c001 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c002 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53 74 61    */.     /* Sta
2c003 74 65 3a 20 20 20 20 20 20 20 2a 2a 20 20 53 45  te:       **  SE
2c004 4d 49 20 20 57 53 20 20 4f 54 48 45 52 20 45 58  MI  WS  OTHER EX
2c005 50 4c 41 49 4e 20 20 43 52 45 41 54 45 20 20 54  PLAIN  CREATE  T
2c006 45 4d 50 20 20 54 52 49 47 47 45 52 20 20 45 4e  EMP  TRIGGER  EN
2c007 44 20 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 30 20  D  */.     /* 0 
2c008 20 20 53 54 41 52 54 3a 20 2a 2f 20 7b 20 20 20    START: */ {   
2c009 20 30 2c 20 20 30 2c 20 20 20 20 20 31 2c 20 20   0,  0,     1,  
2c00a 20 20 20 20 32 2c 20 20 20 20 20 20 33 2c 20 20      2,      3,  
2c00b 20 20 31 2c 20 20 20 20 20 20 20 31 2c 20 20 20    1,       1,   
2c00c 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20 31  1,  },.     /* 1
2c00d 20 20 4e 4f 52 4d 41 4c 3a 20 2a 2f 20 7b 20 20    NORMAL: */ {  
2c00e 20 20 30 2c 20 20 31 2c 20 20 20 20 20 31 2c 20    0,  1,     1, 
2c00f 20 20 20 20 20 31 2c 20 20 20 20 20 20 31 2c 20       1,      1, 
2c010 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c 20 20     1,       1,  
2c011 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a 20   1,  },.     /* 
2c012 32 20 45 58 50 4c 41 49 4e 3a 20 2a 2f 20 7b 20  2 EXPLAIN: */ { 
2c013 20 20 20 30 2c 20 20 32 2c 20 20 20 20 20 31 2c     0,  2,     1,
2c014 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 33 2c        1,      3,
2c015 20 20 20 20 31 2c 20 20 20 20 20 20 20 31 2c 20      1,       1, 
2c016 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f 2a    1,  },.     /*
2c017 20 33 20 20 43 52 45 41 54 45 3a 20 2a 2f 20 7b   3  CREATE: */ {
2c018 20 20 20 20 30 2c 20 20 33 2c 20 20 20 20 20 31      0,  3,     1
2c019 2c 20 20 20 20 20 20 31 2c 20 20 20 20 20 20 31  ,      1,      1
2c01a 2c 20 20 20 20 33 2c 20 20 20 20 20 20 20 34 2c  ,    3,       4,
2c01b 20 20 20 31 2c 20 20 7d 2c 0a 20 20 20 20 20 2f     1,  },.     /
2c01c 2a 20 34 20 54 52 49 47 47 45 52 3a 20 2a 2f 20  * 4 TRIGGER: */ 
2c01d 7b 20 20 20 20 35 2c 20 20 34 2c 20 20 20 20 20  {    5,  4,     
2c01e 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 20 20  4,      4,      
2c01f 34 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20 34  4,    4,       4
2c020 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20 20 20 20  ,   4,  },.     
2c021 2f 2a 20 35 20 20 20 20 53 45 4d 49 3a 20 2a 2f  /* 5    SEMI: */
2c022 20 7b 20 20 20 20 35 2c 20 20 35 2c 20 20 20 20   {    5,  5,    
2c023 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20 20   4,      4,     
2c024 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20 20 20   4,    4,       
2c025 34 2c 20 20 20 36 2c 20 20 7d 2c 0a 20 20 20 20  4,   6,  },.    
2c026 20 2f 2a 20 36 20 20 20 20 20 45 4e 44 3a 20 2a   /* 6     END: *
2c027 2f 20 7b 20 20 20 20 30 2c 20 20 36 2c 20 20 20  / {    0,  6,   
2c028 20 20 34 2c 20 20 20 20 20 20 34 2c 20 20 20 20    4,      4,    
2c029 20 20 34 2c 20 20 20 20 34 2c 20 20 20 20 20 20    4,    4,      
2c02a 20 34 2c 20 20 20 34 2c 20 20 7d 2c 0a 20 20 7d   4,   4,  },.  }
2c02b 3b 0a 23 65 6c 73 65 0a 20 20 2f 2a 20 49 66 20  ;.#else.  /* If 
2c02c 74 72 69 67 67 65 72 73 20 61 72 65 20 6e 6f 74  triggers are not
2c02d 20 73 75 70 70 6f 72 65 64 20 62 79 20 74 68 69   suppored by thi
2c02e 73 20 63 6f 6d 70 69 6c 65 20 74 68 65 6e 20 74  s compile then t
2c02f 68 65 20 73 74 61 74 65 6d 65 6e 74 20 6d 61 63  he statement mac
2c030 68 69 6e 65 0a 20 20 2a 2a 20 75 73 65 64 20 74  hine.  ** used t
2c031 6f 20 64 65 74 65 63 74 20 74 68 65 20 65 6e 64  o detect the end
2c032 20 6f 66 20 61 20 73 74 61 74 65 6d 65 6e 74 20   of a statement 
2c033 69 73 20 6d 75 63 68 20 73 69 6d 70 6c 69 65 72  is much simplier
2c034 0a 20 20 2a 2f 0a 20 20 73 74 61 74 69 63 20 63  .  */.  static c
2c035 6f 6e 73 74 20 75 38 20 74 72 61 6e 73 5b 32 5d  onst u8 trans[2]
2c036 5b 33 5d 20 3d 20 7b 0a 20 20 20 20 20 20 20 20  [3] = {.        
2c037 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c038 54 6f 6b 65 6e 3a 20 20 20 20 20 20 20 20 20 20  Token:          
2c039 20 2a 2f 0a 20 20 20 20 20 2f 2a 20 53 74 61 74   */.     /* Stat
2c03a 65 3a 20 20 20 20 20 20 20 2a 2a 20 20 53 45 4d  e:       **  SEM
2c03b 49 20 20 57 53 20 20 4f 54 48 45 52 20 2a 2f 0a  I  WS  OTHER */.
2c03c 20 20 20 20 20 2f 2a 20 30 20 20 20 53 54 41 52       /* 0   STAR
2c03d 54 3a 20 2a 2f 20 7b 20 20 20 20 30 2c 20 20 30  T: */ {    0,  0
2c03e 2c 20 20 20 20 20 31 2c 20 7d 2c 0a 20 20 20 20  ,     1, },.    
2c03f 20 2f 2a 20 31 20 20 4e 4f 52 4d 41 4c 3a 20 2a   /* 1  NORMAL: *
2c040 2f 20 7b 20 20 20 20 30 2c 20 20 31 2c 20 20 20  / {    0,  1,   
2c041 20 20 31 2c 20 7d 2c 0a 20 20 7d 3b 0a 23 65 6e    1, },.  };.#en
2c042 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2c043 49 54 5f 54 52 49 47 47 45 52 20 2a 2f 0a 0a 20  IT_TRIGGER */.. 
2c044 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 29 7b   while( *zSql ){
2c045 0a 20 20 20 20 73 77 69 74 63 68 28 20 2a 7a 53  .    switch( *zS
2c046 71 6c 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  ql ){.      case
2c047 20 27 3b 27 3a 20 7b 20 20 2f 2a 20 41 20 73 65   ';': {  /* A se
2c048 6d 69 63 6f 6c 6f 6e 20 2a 2f 0a 20 20 20 20 20  micolon */.     
2c049 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 53 45 4d     token = tkSEM
2c04a 49 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  I;.        break
2c04b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c04c 63 61 73 65 20 27 20 27 3a 0a 20 20 20 20 20 20  case ' ':.      
2c04d 63 61 73 65 20 27 5c 72 27 3a 0a 20 20 20 20 20  case '\r':.     
2c04e 20 63 61 73 65 20 27 5c 74 27 3a 0a 20 20 20 20   case '\t':.    
2c04f 20 20 63 61 73 65 20 27 5c 6e 27 3a 0a 20 20 20    case '\n':.   
2c050 20 20 20 63 61 73 65 20 27 5c 66 27 3a 20 7b 20     case '\f': { 
2c051 20 2f 2a 20 57 68 69 74 65 20 73 70 61 63 65 20   /* White space 
2c052 69 73 20 69 67 6e 6f 72 65 64 20 2a 2f 0a 20 20  is ignored */.  
2c053 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b        token = tk
2c054 57 53 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61  WS;.        brea
2c055 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  k;.      }.     
2c056 20 63 61 73 65 20 27 2f 27 3a 20 7b 20 20 20 2f   case '/': {   /
2c057 2a 20 43 2d 73 74 79 6c 65 20 63 6f 6d 6d 65 6e  * C-style commen
2c058 74 73 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 66  ts */.        if
2c059 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2a 27 20 29  ( zSql[1]!='*' )
2c05a 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65  {.          toke
2c05b 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20  n = tkOTHER;.   
2c05c 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
2c05d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c05e 7a 53 71 6c 20 2b 3d 20 32 3b 0a 20 20 20 20 20  zSql += 2;.     
2c05f 20 20 20 77 68 69 6c 65 28 20 7a 53 71 6c 5b 30     while( zSql[0
2c060 5d 20 26 26 20 28 7a 53 71 6c 5b 30 5d 21 3d 27  ] && (zSql[0]!='
2c061 2a 27 20 7c 7c 20 7a 53 71 6c 5b 31 5d 21 3d 27  *' || zSql[1]!='
2c062 2f 27 29 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d  /') ){ zSql++; }
2c063 0a 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 71  .        if( zSq
2c064 6c 5b 30 5d 3d 3d 30 20 29 20 72 65 74 75 72 6e  l[0]==0 ) return
2c065 20 30 3b 0a 20 20 20 20 20 20 20 20 7a 53 71 6c   0;.        zSql
2c066 2b 2b 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65  ++;.        toke
2c067 6e 20 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20  n = tkWS;.      
2c068 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d    break;.      }
2c069 0a 20 20 20 20 20 20 63 61 73 65 20 27 2d 27 3a  .      case '-':
2c06a 20 7b 20 20 20 2f 2a 20 53 51 4c 2d 73 74 79 6c   {   /* SQL-styl
2c06b 65 20 63 6f 6d 6d 65 6e 74 73 20 66 72 6f 6d 20  e comments from 
2c06c 22 2d 2d 22 20 74 6f 20 65 6e 64 20 6f 66 20 6c  "--" to end of l
2c06d 69 6e 65 20 2a 2f 0a 20 20 20 20 20 20 20 20 69  ine */.        i
2c06e 66 28 20 7a 53 71 6c 5b 31 5d 21 3d 27 2d 27 20  f( zSql[1]!='-' 
2c06f 29 7b 0a 20 20 20 20 20 20 20 20 20 20 74 6f 6b  ){.          tok
2c070 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20  en = tkOTHER;.  
2c071 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2c072 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2c073 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26   while( *zSql &&
2c074 20 2a 7a 53 71 6c 21 3d 27 5c 6e 27 20 29 7b 20   *zSql!='\n' ){ 
2c075 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20  zSql++; }.      
2c076 20 20 69 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29    if( *zSql==0 )
2c077 20 72 65 74 75 72 6e 20 73 74 61 74 65 3d 3d 30   return state==0
2c078 3b 0a 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20  ;.        token 
2c079 3d 20 74 6b 57 53 3b 0a 20 20 20 20 20 20 20 20  = tkWS;.        
2c07a 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2c07b 20 20 20 20 20 63 61 73 65 20 27 5b 27 3a 20 7b       case '[': {
2c07c 20 20 20 2f 2a 20 4d 69 63 72 6f 73 6f 66 74 2d     /* Microsoft-
2c07d 73 74 79 6c 65 20 69 64 65 6e 74 69 66 69 65 72  style identifier
2c07e 73 20 69 6e 20 5b 2e 2e 2e 5d 20 2a 2f 0a 20 20  s in [...] */.  
2c07f 20 20 20 20 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20        zSql++;.  
2c080 20 20 20 20 20 20 77 68 69 6c 65 28 20 2a 7a 53        while( *zS
2c081 71 6c 20 26 26 20 2a 7a 53 71 6c 21 3d 27 5d 27  ql && *zSql!=']'
2c082 20 29 7b 20 7a 53 71 6c 2b 2b 3b 20 7d 0a 20 20   ){ zSql++; }.  
2c083 20 20 20 20 20 20 69 66 28 20 2a 7a 53 71 6c 3d        if( *zSql=
2c084 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
2c085 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74         token = t
2c086 6b 4f 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20  kOTHER;.        
2c087 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20  break;.      }. 
2c088 20 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 20       case '`':  
2c089 20 20 20 2f 2a 20 47 72 61 76 65 2d 61 63 63 65     /* Grave-acce
2c08a 6e 74 20 71 75 6f 74 65 64 20 73 79 6d 62 6f 6c  nt quoted symbol
2c08b 73 20 75 73 65 64 20 62 79 20 4d 79 53 51 4c 20  s used by MySQL 
2c08c 2a 2f 0a 20 20 20 20 20 20 63 61 73 65 20 27 22  */.      case '"
2c08d 27 3a 20 20 20 20 20 2f 2a 20 73 69 6e 67 6c 65  ':     /* single
2c08e 2d 20 61 6e 64 20 64 6f 75 62 6c 65 2d 71 75 6f  - and double-quo
2c08f 74 65 64 20 73 74 72 69 6e 67 73 20 2a 2f 0a 20  ted strings */. 
2c090 20 20 20 20 20 63 61 73 65 20 27 5c 27 27 3a 20       case '\'': 
2c091 7b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 63 20  {.        int c 
2c092 3d 20 2a 7a 53 71 6c 3b 0a 20 20 20 20 20 20 20  = *zSql;.       
2c093 20 7a 53 71 6c 2b 2b 3b 0a 20 20 20 20 20 20 20   zSql++;.       
2c094 20 77 68 69 6c 65 28 20 2a 7a 53 71 6c 20 26 26   while( *zSql &&
2c095 20 2a 7a 53 71 6c 21 3d 63 20 29 7b 20 7a 53 71   *zSql!=c ){ zSq
2c096 6c 2b 2b 3b 20 7d 0a 20 20 20 20 20 20 20 20 69  l++; }.        i
2c097 66 28 20 2a 7a 53 71 6c 3d 3d 30 20 29 20 72 65  f( *zSql==0 ) re
2c098 74 75 72 6e 20 30 3b 0a 20 20 20 20 20 20 20 20  turn 0;.        
2c099 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b  token = tkOTHER;
2c09a 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  .        break;.
2c09b 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 64 65        }.      de
2c09c 66 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20  fault: {.       
2c09d 20 69 6e 74 20 63 3b 0a 20 20 20 20 20 20 20 20   int c;.        
2c09e 69 66 28 20 49 64 43 68 61 72 28 28 75 38 29 2a  if( IdChar((u8)*
2c09f 7a 53 71 6c 29 20 29 7b 0a 20 20 20 20 20 20 20  zSql) ){.       
2c0a0 20 20 20 2f 2a 20 4b 65 79 77 6f 72 64 73 20 61     /* Keywords a
2c0a1 6e 64 20 75 6e 71 75 6f 74 65 64 20 69 64 65 6e  nd unquoted iden
2c0a2 74 69 66 69 65 72 73 20 2a 2f 0a 20 20 20 20 20  tifiers */.     
2c0a3 20 20 20 20 20 69 6e 74 20 6e 49 64 3b 0a 20 20       int nId;.  
2c0a4 20 20 20 20 20 20 20 20 66 6f 72 28 6e 49 64 3d          for(nId=
2c0a5 31 3b 20 49 64 43 68 61 72 28 7a 53 71 6c 5b 6e  1; IdChar(zSql[n
2c0a6 49 64 5d 29 3b 20 6e 49 64 2b 2b 29 7b 7d 0a 23  Id]); nId++){}.#
2c0a7 69 66 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49  ifdef SQLITE_OMI
2c0a8 54 5f 54 52 49 47 47 45 52 0a 20 20 20 20 20 20  T_TRIGGER.      
2c0a9 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
2c0aa 48 45 52 3b 0a 23 65 6c 73 65 0a 20 20 20 20 20  HER;.#else.     
2c0ab 20 20 20 20 20 73 77 69 74 63 68 28 20 2a 7a 53       switch( *zS
2c0ac 71 6c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ql ){.          
2c0ad 20 20 63 61 73 65 20 27 63 27 3a 20 63 61 73 65    case 'c': case
2c0ae 20 27 43 27 3a 20 7b 0a 20 20 20 20 20 20 20 20   'C': {.        
2c0af 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 36        if( nId==6
2c0b0 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49   && sqlite3StrNI
2c0b1 43 6d 70 28 7a 53 71 6c 2c 20 22 63 72 65 61 74  Cmp(zSql, "creat
2c0b2 65 22 2c 20 36 29 3d 3d 30 20 29 7b 0a 20 20 20  e", 6)==0 ){.   
2c0b3 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
2c0b4 65 6e 20 3d 20 74 6b 43 52 45 41 54 45 3b 0a 20  en = tkCREATE;. 
2c0b5 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65 6c               }el
2c0b6 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20  se{.            
2c0b7 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
2c0b8 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20  HER;.           
2c0b9 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20     }.           
2c0ba 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2c0bb 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c0bc 20 20 20 20 63 61 73 65 20 27 74 27 3a 20 63 61      case 't': ca
2c0bd 73 65 20 27 54 27 3a 20 7b 0a 20 20 20 20 20 20  se 'T': {.      
2c0be 20 20 20 20 20 20 20 20 69 66 28 20 6e 49 64 3d          if( nId=
2c0bf 3d 37 20 26 26 20 73 71 6c 69 74 65 33 53 74 72  =7 && sqlite3Str
2c0c0 4e 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 72 69  NICmp(zSql, "tri
2c0c1 67 67 65 72 22 2c 20 37 29 3d 3d 30 20 29 7b 0a  gger", 7)==0 ){.
2c0c2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c0c3 74 6f 6b 65 6e 20 3d 20 74 6b 54 52 49 47 47 45  token = tkTRIGGE
2c0c4 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  R;.             
2c0c5 20 7d 65 6c 73 65 20 69 66 28 20 6e 49 64 3d 3d   }else if( nId==
2c0c6 34 20 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e  4 && sqlite3StrN
2c0c7 49 43 6d 70 28 7a 53 71 6c 2c 20 22 74 65 6d 70  ICmp(zSql, "temp
2c0c8 22 2c 20 34 29 3d 3d 30 20 29 7b 0a 20 20 20 20  ", 4)==0 ){.    
2c0c9 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65              toke
2c0ca 6e 20 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20  n = tkTEMP;.    
2c0cb 20 20 20 20 20 20 20 20 20 20 7d 65 6c 73 65 20            }else 
2c0cc 69 66 28 20 6e 49 64 3d 3d 39 20 26 26 20 73 71  if( nId==9 && sq
2c0cd 6c 69 74 65 33 53 74 72 4e 49 43 6d 70 28 7a 53  lite3StrNICmp(zS
2c0ce 71 6c 2c 20 22 74 65 6d 70 6f 72 61 72 79 22 2c  ql, "temporary",
2c0cf 20 39 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20   9)==0 ){.      
2c0d0 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20            token 
2c0d1 3d 20 74 6b 54 45 4d 50 3b 0a 20 20 20 20 20 20  = tkTEMP;.      
2c0d2 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2c0d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 74                 t
2c0d4 6f 6b 65 6e 20 3d 20 74 6b 4f 54 48 45 52 3b 0a  oken = tkOTHER;.
2c0d5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a                }.
2c0d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
2c0d7 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
2c0d8 20 7d 0a 20 20 20 20 20 20 20 20 20 20 20 20 63   }.            c
2c0d9 61 73 65 20 27 65 27 3a 20 20 63 61 73 65 20 27  ase 'e':  case '
2c0da 45 27 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  E': {.          
2c0db 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 33 20 26      if( nId==3 &
2c0dc 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43 6d  & sqlite3StrNICm
2c0dd 70 28 7a 53 71 6c 2c 20 22 65 6e 64 22 2c 20 33  p(zSql, "end", 3
2c0de 29 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20  )==0 ){.        
2c0df 20 20 20 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20          token = 
2c0e0 74 6b 45 4e 44 3b 0a 20 20 20 20 20 20 20 20 20  tkEND;.         
2c0e1 20 20 20 20 20 7d 65 6c 73 65 0a 23 69 66 6e 64       }else.#ifnd
2c0e2 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 45  ef SQLITE_OMIT_E
2c0e3 58 50 4c 41 49 4e 0a 20 20 20 20 20 20 20 20 20  XPLAIN.         
2c0e4 20 20 20 20 20 69 66 28 20 6e 49 64 3d 3d 37 20       if( nId==7 
2c0e5 26 26 20 73 71 6c 69 74 65 33 53 74 72 4e 49 43  && sqlite3StrNIC
2c0e6 6d 70 28 7a 53 71 6c 2c 20 22 65 78 70 6c 61 69  mp(zSql, "explai
2c0e7 6e 22 2c 20 37 29 3d 3d 30 20 29 7b 0a 20 20 20  n", 7)==0 ){.   
2c0e8 20 20 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b               tok
2c0e9 65 6e 20 3d 20 74 6b 45 58 50 4c 41 49 4e 3b 0a  en = tkEXPLAIN;.
2c0ea 20 20 20 20 20 20 20 20 20 20 20 20 20 20 7d 65                }e
2c0eb 6c 73 65 0a 23 65 6e 64 69 66 0a 20 20 20 20 20  lse.#endif.     
2c0ec 20 20 20 20 20 20 20 20 20 7b 0a 20 20 20 20 20           {.     
2c0ed 20 20 20 20 20 20 20 20 20 20 20 74 6f 6b 65 6e             token
2c0ee 20 3d 20 74 6b 4f 54 48 45 52 3b 0a 20 20 20 20   = tkOTHER;.    
2c0ef 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
2c0f0 20 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b            break;
2c0f1 0a 20 20 20 20 20 20 20 20 20 20 20 20 7d 0a 20  .            }. 
2c0f2 20 20 20 20 20 20 20 20 20 20 20 64 65 66 61 75             defau
2c0f3 6c 74 3a 20 7b 0a 20 20 20 20 20 20 20 20 20 20  lt: {.          
2c0f4 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f 54      token = tkOT
2c0f5 48 45 52 3b 0a 20 20 20 20 20 20 20 20 20 20 20  HER;.           
2c0f6 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2c0f7 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
2c0f8 20 20 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51    }.#endif /* SQ
2c0f9 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 49 47 47 45  LITE_OMIT_TRIGGE
2c0fa 52 20 2a 2f 0a 20 20 20 20 20 20 20 20 20 20 7a  R */.          z
2c0fb 53 71 6c 20 2b 3d 20 6e 49 64 2d 31 3b 0a 20 20  Sql += nId-1;.  
2c0fc 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20        }else{.   
2c0fd 20 20 20 20 20 20 20 2f 2a 20 4f 70 65 72 61 74         /* Operat
2c0fe 6f 72 73 20 61 6e 64 20 73 70 65 63 69 61 6c 20  ors and special 
2c0ff 73 79 6d 62 6f 6c 73 20 2a 2f 0a 20 20 20 20 20  symbols */.     
2c100 20 20 20 20 20 74 6f 6b 65 6e 20 3d 20 74 6b 4f       token = tkO
2c101 54 48 45 52 3b 0a 20 20 20 20 20 20 20 20 7d 0a  THER;.        }.
2c102 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2c103 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20       }.    }.   
2c104 20 73 74 61 74 65 20 3d 20 74 72 61 6e 73 5b 73   state = trans[s
2c105 74 61 74 65 5d 5b 74 6f 6b 65 6e 5d 3b 0a 20 20  tate][token];.  
2c106 20 20 7a 53 71 6c 2b 2b 3b 0a 20 20 7d 0a 20 20    zSql++;.  }.  
2c107 72 65 74 75 72 6e 20 73 74 61 74 65 3d 3d 30 3b  return state==0;
2c108 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2c109 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
2c10a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c10b 20 69 73 20 74 68 65 20 73 61 6d 65 20 61 73 20   is the same as 
2c10c 74 68 65 20 73 71 6c 69 74 65 33 5f 63 6f 6d 70  the sqlite3_comp
2c10d 6c 65 74 65 28 29 20 72 6f 75 74 69 6e 65 20 64  lete() routine d
2c10e 65 73 63 72 69 62 65 64 0a 2a 2a 20 61 62 6f 76  escribed.** abov
2c10f 65 2c 20 65 78 63 65 70 74 20 74 68 61 74 20 74  e, except that t
2c110 68 65 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  he parameter is 
2c111 72 65 71 75 69 72 65 64 20 74 6f 20 62 65 20 55  required to be U
2c112 54 46 2d 31 36 20 65 6e 63 6f 64 65 64 2c 20 6e  TF-16 encoded, n
2c113 6f 74 0a 2a 2a 20 55 54 46 2d 38 2e 0a 2a 2f 0a  ot.** UTF-8..*/.
2c114 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2c115 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 31  qlite3_complete1
2c116 36 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a 53  6(const void *zS
2c117 71 6c 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ql){.  sqlite3_v
2c118 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 63 68  alue *pVal;.  ch
2c119 61 72 20 63 6f 6e 73 74 20 2a 7a 53 71 6c 38 3b  ar const *zSql8;
2c11a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
2c11b 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 23 69 66 6e 64  TE_NOMEM;..#ifnd
2c11c 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 41  ef SQLITE_OMIT_A
2c11d 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d 20 73  UTOINIT.  rc = s
2c11e 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c 69 7a  qlite3_initializ
2c11f 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20 29 20  e();.  if( rc ) 
2c120 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e 64 69  return rc;.#endi
2c121 66 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c 69 74  f.  pVal = sqlit
2c122 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b 0a 20  e3ValueNew(0);. 
2c123 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65 74   sqlite3ValueSet
2c124 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20 7a 53  Str(pVal, -1, zS
2c125 71 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 31 36  ql, SQLITE_UTF16
2c126 4e 41 54 49 56 45 2c 20 53 51 4c 49 54 45 5f 53  NATIVE, SQLITE_S
2c127 54 41 54 49 43 29 3b 0a 20 20 7a 53 71 6c 38 20  TATIC);.  zSql8 
2c128 3d 20 73 71 6c 69 74 65 33 56 61 6c 75 65 54 65  = sqlite3ValueTe
2c129 78 74 28 70 56 61 6c 2c 20 53 51 4c 49 54 45 5f  xt(pVal, SQLITE_
2c12a 55 54 46 38 29 3b 0a 20 20 69 66 28 20 7a 53 71  UTF8);.  if( zSq
2c12b 6c 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 73  l8 ){.    rc = s
2c12c 71 6c 69 74 65 33 5f 63 6f 6d 70 6c 65 74 65 28  qlite3_complete(
2c12d 7a 53 71 6c 38 29 3b 0a 20 20 7d 65 6c 73 65 7b  zSql8);.  }else{
2c12e 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
2c12f 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 73 71  _NOMEM;.  }.  sq
2c130 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28 70  lite3ValueFree(p
2c131 56 61 6c 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  Val);.  return s
2c132 71 6c 69 74 65 33 41 70 69 45 78 69 74 28 30 2c  qlite3ApiExit(0,
2c133 20 72 63 29 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f   rc);.}.#endif /
2c134 2a 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  * SQLITE_OMIT_UT
2c135 46 31 36 20 2a 2f 0a 23 65 6e 64 69 66 20 2f 2a  F16 */.#endif /*
2c136 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 43 4f 4d   SQLITE_OMIT_COM
2c137 50 4c 45 54 45 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a  PLETE */../*****
2c138 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66  ********* End of
2c139 20 63 6f 6d 70 6c 65 74 65 2e 63 20 2a 2a 2a 2a   complete.c ****
2c13a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c13b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c13c 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a  ********/./*****
2c13d 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20  ********* Begin 
2c13e 66 69 6c 65 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  file main.c ****
2c13f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c140 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c141 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20  ********/./*.** 
2c142 32 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 31  2001 September 1
2c143 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  5.**.** The auth
2c144 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2c145 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2c146 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2c147 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2c148 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2c149 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2c14a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2c14b 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2c14c 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2c14d 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2c14e 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2c14f 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2c150 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2c151 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2c152 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2c153 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2c154 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2c155 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c156 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c157 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c159 0a 2a 2a 20 4d 61 69 6e 20 66 69 6c 65 20 66 6f  .** Main file fo
2c15a 72 20 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62  r the SQLite lib
2c15b 72 61 72 79 2e 20 20 54 68 65 20 72 6f 75 74 69  rary.  The routi
2c15c 6e 65 73 20 69 6e 20 74 68 69 73 20 66 69 6c 65  nes in this file
2c15d 0a 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68  .** implement th
2c15e 65 20 70 72 6f 67 72 61 6d 6d 65 72 20 69 6e 74  e programmer int
2c15f 65 72 66 61 63 65 20 74 6f 20 74 68 65 20 6c 69  erface to the li
2c160 62 72 61 72 79 2e 20 20 52 6f 75 74 69 6e 65 73  brary.  Routines
2c161 20 69 6e 0a 2a 2a 20 6f 74 68 65 72 20 66 69 6c   in.** other fil
2c162 65 73 20 61 72 65 20 66 6f 72 20 69 6e 74 65 72  es are for inter
2c163 6e 61 6c 20 75 73 65 20 62 79 20 53 51 4c 69 74  nal use by SQLit
2c164 65 20 61 6e 64 20 73 68 6f 75 6c 64 20 6e 6f 74  e and should not
2c165 20 62 65 0a 2a 2a 20 61 63 63 65 73 73 65 64 20   be.** accessed 
2c166 62 79 20 75 73 65 72 73 20 6f 66 20 74 68 65 20  by users of the 
2c167 6c 69 62 72 61 72 79 2e 0a 2a 2a 0a 2a 2a 20 24  library..**.** $
2c168 49 64 3a 20 6d 61 69 6e 2e 63 2c 76 20 31 2e 35  Id: main.c,v 1.5
2c169 31 34 20 32 30 30 38 2f 31 31 2f 31 39 20 30 39  14 2008/11/19 09
2c16a 3a 30 35 3a 32 37 20 64 61 6e 69 65 6c 6b 31 39  :05:27 danielk19
2c16b 37 37 20 45 78 70 20 24 0a 2a 2f 0a 0a 23 69 66  77 Exp $.*/..#if
2c16c 64 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c  def SQLITE_ENABL
2c16d 45 5f 46 54 53 33 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  E_FTS3./********
2c16e 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65 20 66  ****** Include f
2c16f 74 73 33 2e 68 20 69 6e 20 74 68 65 20 6d 69 64  ts3.h in the mid
2c170 64 6c 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a  dle of main.c **
2c171 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c172 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a  *****/./********
2c173 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c  ****** Begin fil
2c174 65 20 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a  e fts3.h *******
2c175 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c176 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c177 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30  *****/./*.** 200
2c178 36 20 4f 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54  6 Oct 10.**.** T
2c179 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c 61  he author discla
2c17a 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74 6f  ims copyright to
2c17b 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f 64   this source cod
2c17c 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a  e.  In place of.
2c17d 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63  ** a legal notic
2c17e 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c 65  e, here is a ble
2c17f 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ssing:.**.**    
2c180 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20  May you do good 
2c181 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a  and not evil..**
2c182 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e 64      May you find
2c183 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f 72   forgiveness for
2c184 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66 6f   yourself and fo
2c185 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a  rgive others..**
2c186 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61 72      May you shar
2c187 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72 20  e freely, never 
2c188 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e  taking more than
2c189 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a   you give..**.**
2c18a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c18b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c18c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c18d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c18e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a  ************.**.
2c18f 2a 2a 20 54 68 69 73 20 68 65 61 64 65 72 20 66  ** This header f
2c190 69 6c 65 20 69 73 20 75 73 65 64 20 62 79 20 70  ile is used by p
2c191 72 6f 67 72 61 6d 73 20 74 68 61 74 20 77 61 6e  rograms that wan
2c192 74 20 74 6f 20 6c 69 6e 6b 20 61 67 61 69 6e 73  t to link agains
2c193 74 20 74 68 65 0a 2a 2a 20 46 54 53 33 20 6c 69  t the.** FTS3 li
2c194 62 72 61 72 79 2e 20 20 41 6c 6c 20 69 74 20 64  brary.  All it d
2c195 6f 65 73 20 69 73 20 64 65 63 6c 61 72 65 20 74  oes is declare t
2c196 68 65 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e  he sqlite3Fts3In
2c197 69 74 28 29 20 69 6e 74 65 72 66 61 63 65 2e 0a  it() interface..
2c198 2a 2f 0a 0a 23 69 66 20 30 0a 65 78 74 65 72 6e  */..#if 0.extern
2c199 20 22 43 22 20 7b 0a 23 65 6e 64 69 66 20 20 2f   "C" {.#endif  /
2c19a 2a 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f  * __cplusplus */
2c19b 0a 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45  ..SQLITE_PRIVATE
2c19c 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74 73 33   int sqlite3Fts3
2c19d 49 6e 69 74 28 73 71 6c 69 74 65 33 20 2a 64 62  Init(sqlite3 *db
2c19e 29 3b 0a 0a 23 69 66 20 30 0a 7d 20 20 2f 2a 20  );..#if 0.}  /* 
2c19f 65 78 74 65 72 6e 20 22 43 22 20 2a 2f 0a 23 65  extern "C" */.#e
2c1a0 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73  ndif  /* __cplus
2c1a1 70 6c 75 73 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  plus */../******
2c1a2 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2c1a3 66 74 73 33 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.h *********
2c1a4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1a5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1a6 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2c1a7 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
2c1a8 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
2c1a9 74 20 6f 66 66 20 69 6e 20 6d 61 69 6e 2e 63 20  t off in main.c 
2c1aa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1ab 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 65 6e 64 69 66 0a  *******/.#endif.
2c1ac 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45 4e  #ifdef SQLITE_EN
2c1ad 41 42 4c 45 5f 52 54 52 45 45 0a 2f 2a 2a 2a 2a  ABLE_RTREE./****
2c1ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75  ********** Inclu
2c1af 64 65 20 72 74 72 65 65 2e 68 20 69 6e 20 74 68  de rtree.h in th
2c1b0 65 20 6d 69 64 64 6c 65 20 6f 66 20 6d 61 69 6e  e middle of main
2c1b1 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .c *************
2c1b2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
2c1b3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
2c1b4 20 66 69 6c 65 20 72 74 72 65 65 2e 68 20 2a 2a   file rtree.h **
2c1b5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1b6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1b7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
2c1b8 20 32 30 30 38 20 4d 61 79 20 32 36 0a 2a 2a 0a   2008 May 26.**.
2c1b9 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69  ** The author di
2c1ba 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68  sclaims copyrigh
2c1bb 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65  t to this source
2c1bc 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65   code.  In place
2c1bd 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e   of.** a legal n
2c1be 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61  otice, here is a
2c1bf 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a   blessing:.**.**
2c1c0 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67      May you do g
2c1c1 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c  ood and not evil
2c1c2 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2c1c3 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73  find forgiveness
2c1c4 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e   for yourself an
2c1c5 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73  d forgive others
2c1c6 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  ..**    May you 
2c1c7 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65  share freely, ne
2c1c8 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20  ver taking more 
2c1c9 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a  than you give..*
2c1ca 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  *.**************
2c1cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1cc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1cd 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1cf 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 68 65 61 64  .**.** This head
2c1d0 65 72 20 66 69 6c 65 20 69 73 20 75 73 65 64 20  er file is used 
2c1d1 62 79 20 70 72 6f 67 72 61 6d 73 20 74 68 61 74  by programs that
2c1d2 20 77 61 6e 74 20 74 6f 20 6c 69 6e 6b 20 61 67   want to link ag
2c1d3 61 69 6e 73 74 20 74 68 65 0a 2a 2a 20 52 54 52  ainst the.** RTR
2c1d4 45 45 20 6c 69 62 72 61 72 79 2e 20 20 41 6c 6c  EE library.  All
2c1d5 20 69 74 20 64 6f 65 73 20 69 73 20 64 65 63 6c   it does is decl
2c1d6 61 72 65 20 74 68 65 20 73 71 6c 69 74 65 33 52  are the sqlite3R
2c1d7 74 72 65 65 49 6e 69 74 28 29 20 69 6e 74 65 72  treeInit() inter
2c1d8 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66 20 30 0a  face..*/..#if 0.
2c1d9 65 78 74 65 72 6e 20 22 43 22 20 7b 0a 23 65 6e  extern "C" {.#en
2c1da 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c 75 73 70  dif  /* __cplusp
2c1db 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54 45 5f 50  lus */..SQLITE_P
2c1dc 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
2c1dd 65 33 52 74 72 65 65 49 6e 69 74 28 73 71 6c 69  e3RtreeInit(sqli
2c1de 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20 30  te3 *db);..#if 0
2c1df 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22 43  .}  /* extern "C
2c1e0 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a 20  " */.#endif  /* 
2c1e1 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a 0a  __cplusplus */..
2c1e2 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c1e3 45 6e 64 20 6f 66 20 72 74 72 65 65 2e 68 20 2a  End of rtree.h *
2c1e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2c1e7 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c1e8 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65 72 65  Continuing where
2c1e9 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69 6e 20   we left off in 
2c1ea 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  main.c *********
2c1eb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2c1ec 23 65 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51  #endif.#ifdef SQ
2c1ed 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a  LITE_ENABLE_ICU.
2c1ee 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c1ef 49 6e 63 6c 75 64 65 20 73 71 6c 69 74 65 69 63  Include sqliteic
2c1f0 75 2e 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c  u.h in the middl
2c1f1 65 20 6f 66 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a  e of main.c ****
2c1f2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2c1f3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20  /************** 
2c1f4 42 65 67 69 6e 20 66 69 6c 65 20 73 71 6c 69 74  Begin file sqlit
2c1f5 65 69 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  eicu.h *********
2c1f6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c1f7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a  **************/.
2c1f8 2f 2a 0a 2a 2a 20 32 30 30 38 20 4d 61 79 20 32  /*.** 2008 May 2
2c1f9 36 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68  6.**.** The auth
2c1fa 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70  or disclaims cop
2c1fb 79 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73  yright to this s
2c1fc 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20  ource code.  In 
2c1fd 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65  place of.** a le
2c1fe 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65  gal notice, here
2c1ff 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a   is a blessing:.
2c200 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75  **.**    May you
2c201 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74   do good and not
2c202 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79   evil..**    May
2c203 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76   you find forgiv
2c204 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65  eness for yourse
2c205 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f  lf and forgive o
2c206 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79  thers..**    May
2c207 20 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c   you share freel
2c208 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20  y, never taking 
2c209 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69  more than you gi
2c20a 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ve..**.*********
2c20b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c20c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c20d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c20e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c20f 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73  *****.**.** This
2c210 20 68 65 61 64 65 72 20 66 69 6c 65 20 69 73 20   header file is 
2c211 75 73 65 64 20 62 79 20 70 72 6f 67 72 61 6d 73  used by programs
2c212 20 74 68 61 74 20 77 61 6e 74 20 74 6f 20 6c 69   that want to li
2c213 6e 6b 20 61 67 61 69 6e 73 74 20 74 68 65 0a 2a  nk against the.*
2c214 2a 20 49 43 55 20 65 78 74 65 6e 73 69 6f 6e 2e  * ICU extension.
2c215 20 20 41 6c 6c 20 69 74 20 64 6f 65 73 20 69 73    All it does is
2c216 20 64 65 63 6c 61 72 65 20 74 68 65 20 73 71 6c   declare the sql
2c217 69 74 65 33 49 63 75 49 6e 69 74 28 29 20 69 6e  ite3IcuInit() in
2c218 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 0a 23 69 66  terface..*/..#if
2c219 20 30 0a 65 78 74 65 72 6e 20 22 43 22 20 7b 0a   0.extern "C" {.
2c21a 23 65 6e 64 69 66 20 20 2f 2a 20 5f 5f 63 70 6c  #endif  /* __cpl
2c21b 75 73 70 6c 75 73 20 2a 2f 0a 0a 53 51 4c 49 54  usplus */..SQLIT
2c21c 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
2c21d 6c 69 74 65 33 49 63 75 49 6e 69 74 28 73 71 6c  lite3IcuInit(sql
2c21e 69 74 65 33 20 2a 64 62 29 3b 0a 0a 23 69 66 20  ite3 *db);..#if 
2c21f 30 0a 7d 20 20 2f 2a 20 65 78 74 65 72 6e 20 22  0.}  /* extern "
2c220 43 22 20 2a 2f 0a 23 65 6e 64 69 66 20 20 2f 2a  C" */.#endif  /*
2c221 20 5f 5f 63 70 6c 75 73 70 6c 75 73 20 2a 2f 0a   __cplusplus */.
2c222 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2c223 2a 20 45 6e 64 20 6f 66 20 73 71 6c 69 74 65 69  * End of sqlitei
2c224 63 75 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  cu.h ***********
2c225 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c226 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c227 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
2c228 2a 20 43 6f 6e 74 69 6e 75 69 6e 67 20 77 68 65  * Continuing whe
2c229 72 65 20 77 65 20 6c 65 66 74 20 6f 66 66 20 69  re we left off i
2c22a 6e 20 6d 61 69 6e 2e 63 20 2a 2a 2a 2a 2a 2a 2a  n main.c *******
2c22b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2c22c 2f 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  /.#endif../*.** 
2c22d 54 68 65 20 76 65 72 73 69 6f 6e 20 6f 66 20 74  The version of t
2c22e 68 65 20 6c 69 62 72 61 72 79 0a 2a 2f 0a 53 51  he library.*/.SQ
2c22f 4c 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63  LITE_API const c
2c230 68 61 72 20 73 71 6c 69 74 65 33 5f 76 65 72 73  har sqlite3_vers
2c231 69 6f 6e 5b 5d 20 3d 20 53 51 4c 49 54 45 5f 56  ion[] = SQLITE_V
2c232 45 52 53 49 4f 4e 3b 0a 53 51 4c 49 54 45 5f 41  ERSION;.SQLITE_A
2c233 50 49 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 73  PI const char *s
2c234 71 6c 69 74 65 33 5f 6c 69 62 76 65 72 73 69 6f  qlite3_libversio
2c235 6e 28 76 6f 69 64 29 7b 20 72 65 74 75 72 6e 20  n(void){ return 
2c236 73 71 6c 69 74 65 33 5f 76 65 72 73 69 6f 6e 3b  sqlite3_version;
2c237 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e   }.SQLITE_API in
2c238 74 20 73 71 6c 69 74 65 33 5f 6c 69 62 76 65 72  t sqlite3_libver
2c239 73 69 6f 6e 5f 6e 75 6d 62 65 72 28 76 6f 69 64  sion_number(void
2c23a 29 7b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  ){ return SQLITE
2c23b 5f 56 45 52 53 49 4f 4e 5f 4e 55 4d 42 45 52 3b  _VERSION_NUMBER;
2c23c 20 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e   }.SQLITE_API in
2c23d 74 20 73 71 6c 69 74 65 33 5f 74 68 72 65 61 64  t sqlite3_thread
2c23e 73 61 66 65 28 76 6f 69 64 29 7b 20 72 65 74 75  safe(void){ retu
2c23f 72 6e 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44  rn SQLITE_THREAD
2c240 53 41 46 45 3b 20 7d 0a 0a 23 69 66 20 21 64 65  SAFE; }..#if !de
2c241 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 4f 4d 49  fined(SQLITE_OMI
2c242 54 5f 54 52 41 43 45 29 20 26 26 20 64 65 66 69  T_TRACE) && defi
2c243 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c  ned(SQLITE_ENABL
2c244 45 5f 49 4f 54 52 41 43 45 29 0a 2f 2a 0a 2a 2a  E_IOTRACE)./*.**
2c245 20 49 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   If the followin
2c246 67 20 66 75 6e 63 74 69 6f 6e 20 70 6f 69 6e 74  g function point
2c247 65 72 20 69 73 20 6e 6f 74 20 4e 55 4c 4c 20 61  er is not NULL a
2c248 6e 64 20 69 66 0a 2a 2a 20 53 51 4c 49 54 45 5f  nd if.** SQLITE_
2c249 45 4e 41 42 4c 45 5f 49 4f 54 52 41 43 45 20 69  ENABLE_IOTRACE i
2c24a 73 20 65 6e 61 62 6c 65 64 2c 20 74 68 65 6e 20  s enabled, then 
2c24b 6d 65 73 73 61 67 65 73 20 64 65 73 63 72 69 62  messages describ
2c24c 69 6e 67 0a 2a 2a 20 49 2f 4f 20 61 63 74 69 76  ing.** I/O activ
2c24d 65 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73  e are written us
2c24e 69 6e 67 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ing this functio
2c24f 6e 2e 20 20 54 68 65 73 65 20 6d 65 73 73 61 67  n.  These messag
2c250 65 73 0a 2a 2a 20 61 72 65 20 69 6e 74 65 6e 64  es.** are intend
2c251 65 64 20 66 6f 72 20 64 65 62 75 67 67 69 6e 67  ed for debugging
2c252 20 61 63 74 69 76 69 74 79 20 6f 6e 6c 79 2e 0a   activity only..
2c253 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2c254 45 20 76 6f 69 64 20 28 2a 73 71 6c 69 74 65 33  E void (*sqlite3
2c255 49 6f 54 72 61 63 65 29 28 63 6f 6e 73 74 20 63  IoTrace)(const c
2c256 68 61 72 2a 2c 20 2e 2e 2e 29 20 3d 20 30 3b 0a  har*, ...) = 0;.
2c257 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 49 66  #endif../*.** If
2c258 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 67   the following g
2c259 6c 6f 62 61 6c 20 76 61 72 69 61 62 6c 65 20 70  lobal variable p
2c25a 6f 69 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e  oints to a strin
2c25b 67 20 77 68 69 63 68 20 69 73 20 74 68 65 0a 2a  g which is the.*
2c25c 2a 20 6e 61 6d 65 20 6f 66 20 61 20 64 69 72 65  * name of a dire
2c25d 63 74 6f 72 79 2c 20 74 68 65 6e 20 74 68 61 74  ctory, then that
2c25e 20 64 69 72 65 63 74 6f 72 79 20 77 69 6c 6c 20   directory will 
2c25f 62 65 20 75 73 65 64 20 74 6f 20 73 74 6f 72 65  be used to store
2c260 0a 2a 2a 20 74 65 6d 70 6f 72 61 72 79 20 66 69  .** temporary fi
2c261 6c 65 73 2e 0a 2a 2a 0a 2a 2a 20 53 65 65 20 61  les..**.** See a
2c262 6c 73 6f 20 74 68 65 20 22 50 52 41 47 4d 41 20  lso the "PRAGMA 
2c263 74 65 6d 70 5f 73 74 6f 72 65 5f 64 69 72 65 63  temp_store_direc
2c264 74 6f 72 79 22 20 53 51 4c 20 63 6f 6d 6d 61 6e  tory" SQL comman
2c265 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2c266 20 63 68 61 72 20 2a 73 71 6c 69 74 65 33 5f 74   char *sqlite3_t
2c267 65 6d 70 5f 64 69 72 65 63 74 6f 72 79 20 3d 20  emp_directory = 
2c268 30 3b 0a 0a 2f 2a 0a 2a 2a 20 49 6e 69 74 69 61  0;../*.** Initia
2c269 6c 69 7a 65 20 53 51 4c 69 74 65 2e 20 20 0a 2a  lize SQLite.  .*
2c26a 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e  *.** This routin
2c26b 65 20 6d 75 73 74 20 62 65 20 63 61 6c 6c 65 64  e must be called
2c26c 20 74 6f 20 69 6e 69 74 69 61 6c 69 7a 65 20 74   to initialize t
2c26d 68 65 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61  he memory alloca
2c26e 74 69 6f 6e 2c 0a 2a 2a 20 56 46 53 2c 20 61 6e  tion,.** VFS, an
2c26f 64 20 6d 75 74 65 78 20 73 75 62 73 79 73 74 65  d mutex subsyste
2c270 6d 73 20 70 72 69 6f 72 20 74 6f 20 64 6f 69 6e  ms prior to doin
2c271 67 20 61 6e 79 20 73 65 72 69 6f 75 73 20 77 6f  g any serious wo
2c272 72 6b 20 77 69 74 68 0a 2a 2a 20 53 51 4c 69 74  rk with.** SQLit
2c273 65 2e 20 20 42 75 74 20 61 73 20 6c 6f 6e 67 20  e.  But as long 
2c274 61 73 20 79 6f 75 20 64 6f 20 6e 6f 74 20 63 6f  as you do not co
2c275 6d 70 69 6c 65 20 77 69 74 68 20 53 51 4c 49 54  mpile with SQLIT
2c276 45 5f 4f 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a  E_OMIT_AUTOINIT.
2c277 2a 2a 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  ** this routine 
2c278 77 69 6c 6c 20 62 65 20 63 61 6c 6c 65 64 20 61  will be called a
2c279 75 74 6f 6d 61 74 69 63 61 6c 6c 79 20 62 79 20  utomatically by 
2c27a 6b 65 79 20 72 6f 75 74 69 6e 65 73 20 73 75 63  key routines suc
2c27b 68 20 61 73 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  h as.** sqlite3_
2c27c 6f 70 65 6e 28 29 2e 20 20 0a 2a 2a 0a 2a 2a 20  open().  .**.** 
2c27d 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73 20  This routine is 
2c27e 61 20 6e 6f 2d 6f 70 20 65 78 63 65 70 74 20 6f  a no-op except o
2c27f 6e 20 69 74 73 20 76 65 72 79 20 66 69 72 73 74  n its very first
2c280 20 63 61 6c 6c 20 66 6f 72 20 74 68 65 20 70 72   call for the pr
2c281 6f 63 65 73 73 2c 0a 2a 2a 20 6f 72 20 66 6f 72  ocess,.** or for
2c282 20 74 68 65 20 66 69 72 73 74 20 63 61 6c 6c 20   the first call 
2c283 61 66 74 65 72 20 61 20 63 61 6c 6c 20 74 6f 20  after a call to 
2c284 73 71 6c 69 74 65 33 5f 73 68 75 74 64 6f 77 6e  sqlite3_shutdown
2c285 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 66 69 72 73  ..**.** The firs
2c286 74 20 74 68 72 65 61 64 20 74 6f 20 63 61 6c 6c  t thread to call
2c287 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 72 75   this routine ru
2c288 6e 73 20 74 68 65 20 69 6e 69 74 69 61 6c 69 7a  ns the initializ
2c289 61 74 69 6f 6e 20 74 6f 0a 2a 2a 20 63 6f 6d 70  ation to.** comp
2c28a 6c 65 74 69 6f 6e 2e 20 20 49 66 20 73 75 62 73  letion.  If subs
2c28b 65 71 75 65 6e 74 20 74 68 72 65 61 64 73 20 63  equent threads c
2c28c 61 6c 6c 20 74 68 69 73 20 72 6f 75 74 69 6e 65  all this routine
2c28d 20 62 65 66 6f 72 65 20 74 68 65 20 66 69 72 73   before the firs
2c28e 74 0a 2a 2a 20 74 68 72 65 61 64 20 68 61 73 20  t.** thread has 
2c28f 66 69 6e 69 73 68 65 64 20 74 68 65 20 69 6e 69  finished the ini
2c290 74 69 61 6c 69 7a 61 74 69 6f 6e 20 70 72 6f 63  tialization proc
2c291 65 73 73 2c 20 74 68 65 6e 20 74 68 65 20 73 75  ess, then the su
2c292 62 73 65 71 75 65 6e 74 0a 2a 2a 20 74 68 72 65  bsequent.** thre
2c293 61 64 73 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75  ads must block u
2c294 6e 74 69 6c 20 74 68 65 20 66 69 72 73 74 20 74  ntil the first t
2c295 68 72 65 61 64 20 66 69 6e 69 73 68 65 73 20 77  hread finishes w
2c296 69 74 68 20 74 68 65 20 69 6e 69 74 69 61 6c 69  ith the initiali
2c297 7a 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68  zation..**.** Th
2c298 65 20 66 69 72 73 74 20 74 68 72 65 61 64 20 6d  e first thread m
2c299 69 67 68 74 20 63 61 6c 6c 20 74 68 69 73 20 72  ight call this r
2c29a 6f 75 74 69 6e 65 20 72 65 63 75 72 73 69 76 65  outine recursive
2c29b 6c 79 2e 20 20 52 65 63 75 72 73 69 76 65 0a 2a  ly.  Recursive.*
2c29c 2a 20 63 61 6c 6c 73 20 74 6f 20 74 68 69 73 20  * calls to this 
2c29d 72 6f 75 74 69 6e 65 20 73 68 6f 75 6c 64 20 6e  routine should n
2c29e 6f 74 20 62 6c 6f 63 6b 2c 20 6f 66 20 63 6f 75  ot block, of cou
2c29f 72 73 65 2e 20 20 4f 74 68 65 72 77 69 73 65 20  rse.  Otherwise 
2c2a0 74 68 65 0a 2a 2a 20 69 6e 69 74 69 61 6c 69 7a  the.** initializ
2c2a1 61 74 69 6f 6e 20 70 72 6f 63 65 73 73 20 77 6f  ation process wo
2c2a2 75 6c 64 20 6e 65 76 65 72 20 63 6f 6d 70 6c 65  uld never comple
2c2a3 74 65 2e 0a 2a 2a 0a 2a 2a 20 4c 65 74 20 58 20  te..**.** Let X 
2c2a4 62 65 20 74 68 65 20 66 69 72 73 74 20 74 68 72  be the first thr
2c2a5 65 61 64 20 74 6f 20 65 6e 74 65 72 20 74 68 69  ead to enter thi
2c2a6 73 20 72 6f 75 74 69 6e 65 2e 20 20 4c 65 74 20  s routine.  Let 
2c2a7 59 20 62 65 20 73 6f 6d 65 20 6f 74 68 65 72 0a  Y be some other.
2c2a8 2a 2a 20 74 68 72 65 61 64 2e 20 20 54 68 65 6e  ** thread.  Then
2c2a9 20 77 68 69 6c 65 20 74 68 65 20 69 6e 69 74 69   while the initi
2c2aa 61 6c 20 69 6e 76 6f 63 61 74 69 6f 6e 20 6f 66  al invocation of
2c2ab 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20 62 79   this routine by
2c2ac 20 58 20 69 73 0a 2a 2a 20 69 6e 63 6f 6d 70 6c   X is.** incompl
2c2ad 65 74 65 2c 20 69 74 20 69 73 20 72 65 71 75 69  ete, it is requi
2c2ae 72 65 64 20 74 68 61 74 3a 0a 2a 2a 0a 2a 2a 20  red that:.**.** 
2c2af 20 20 20 2a 20 20 43 61 6c 6c 73 20 74 6f 20 74     *  Calls to t
2c2b0 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f 6d  his routine from
2c2b1 20 59 20 6d 75 73 74 20 62 6c 6f 63 6b 20 75 6e   Y must block un
2c2b2 74 69 6c 20 74 68 65 20 6f 75 74 65 72 2d 6d 6f  til the outer-mo
2c2b3 73 74 0a 2a 2a 20 20 20 20 20 20 20 63 61 6c 6c  st.**       call
2c2b4 20 62 79 20 58 20 63 6f 6d 70 6c 65 74 65 73 2e   by X completes.
2c2b5 0a 2a 2a 0a 2a 2a 20 20 20 20 2a 20 20 52 65 63  .**.**    *  Rec
2c2b6 75 72 73 69 76 65 20 63 61 6c 6c 73 20 74 6f 20  ursive calls to 
2c2b7 74 68 69 73 20 72 6f 75 74 69 6e 65 20 66 72 6f  this routine fro
2c2b8 6d 20 74 68 72 65 61 64 20 58 20 72 65 74 75 72  m thread X retur
2c2b9 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79 0a 2a 2a  n immediately.**
2c2ba 20 20 20 20 20 20 20 77 69 74 68 6f 75 74 20 62         without b
2c2bb 6c 6f 63 6b 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49  locking..*/.SQLI
2c2bc 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
2c2bd 65 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 76 6f  e3_initialize(vo
2c2be 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d  id){.  sqlite3_m
2c2bf 75 74 65 78 20 2a 70 4d 61 73 74 65 72 3b 20 20  utex *pMaster;  
2c2c0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2c1 20 20 20 20 2f 2a 20 54 68 65 20 6d 61 69 6e 20      /* The main 
2c2c2 73 74 61 74 69 63 20 6d 75 74 65 78 20 2a 2f 0a  static mutex */.
2c2c3 20 20 69 6e 74 20 72 63 3b 20 20 20 20 20 20 20    int rc;       
2c2c4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c2c5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2c2c6 2a 20 52 65 73 75 6c 74 20 63 6f 64 65 20 2a 2f  * Result code */
2c2c7 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ..#ifdef SQLITE_
2c2c8 4f 4d 49 54 5f 57 53 44 0a 20 20 72 63 20 3d 20  OMIT_WSD.  rc = 
2c2c9 73 71 6c 69 74 65 33 5f 77 73 64 5f 69 6e 69 74  sqlite3_wsd_init
2c2ca 28 34 30 39 36 2c 20 32 34 29 3b 0a 20 20 69 66  (4096, 24);.  if
2c2cb 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2c2cc 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
2c2cd 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 20 20  ;.  }.#endif..  
2c2ce 2f 2a 20 49 66 20 53 51 4c 69 74 65 20 69 73 20  /* If SQLite is 
2c2cf 61 6c 72 65 61 64 79 20 63 6f 6d 70 6c 65 74 65  already complete
2c2d0 6c 79 20 69 6e 69 74 69 61 6c 69 7a 65 64 2c 20  ly initialized, 
2c2d1 74 68 65 6e 20 74 68 69 73 20 63 61 6c 6c 0a 20  then this call. 
2c2d2 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33 5f 69   ** to sqlite3_i
2c2d3 6e 69 74 69 61 6c 69 7a 65 28 29 20 73 68 6f 75  nitialize() shou
2c2d4 6c 64 20 62 65 20 61 20 6e 6f 2d 6f 70 2e 20 20  ld be a no-op.  
2c2d5 42 75 74 20 74 68 65 20 69 6e 69 74 69 61 6c 69  But the initiali
2c2d6 7a 61 74 69 6f 6e 0a 20 20 2a 2a 20 6d 75 73 74  zation.  ** must
2c2d7 20 62 65 20 63 6f 6d 70 6c 65 74 65 2e 20 20 53   be complete.  S
2c2d8 6f 20 69 73 49 6e 69 74 20 6d 75 73 74 20 6e 6f  o isInit must no
2c2d9 74 20 62 65 20 73 65 74 20 75 6e 74 69 6c 20 74  t be set until t
2c2da 68 65 20 76 65 72 79 20 65 6e 64 0a 20 20 2a 2a  he very end.  **
2c2db 20 6f 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   of this routine
2c2dc 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c  ..  */.  if( sql
2c2dd 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c2de 2e 69 73 49 6e 69 74 20 29 20 72 65 74 75 72 6e  .isInit ) return
2c2df 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 2f   SQLITE_OK;..  /
2c2e0 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 65 20  * Make sure the 
2c2e1 6d 75 74 65 78 20 73 75 62 73 79 73 74 65 6d 20  mutex subsystem 
2c2e2 69 73 20 69 6e 69 74 69 61 6c 69 7a 65 64 2e 20  is initialized. 
2c2e3 20 49 66 20 75 6e 61 62 6c 65 20 74 6f 20 0a 20   If unable to . 
2c2e4 20 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 20 74   ** initialize t
2c2e5 68 65 20 6d 75 74 65 78 20 73 75 62 73 79 73 74  he mutex subsyst
2c2e6 65 6d 2c 20 72 65 74 75 72 6e 20 65 61 72 6c 79  em, return early
2c2e7 20 77 69 74 68 20 74 68 65 20 65 72 72 6f 72 2e   with the error.
2c2e8 0a 20 20 2a 2a 20 49 66 20 74 68 65 20 73 79 73  .  ** If the sys
2c2e9 74 65 6d 20 69 73 20 73 6f 20 73 69 63 6b 20 74  tem is so sick t
2c2ea 68 61 74 20 77 65 20 61 72 65 20 75 6e 61 62 6c  hat we are unabl
2c2eb 65 20 74 6f 20 61 6c 6c 6f 63 61 74 65 20 61 20  e to allocate a 
2c2ec 6d 75 74 65 78 2c 0a 20 20 2a 2a 20 74 68 65 72  mutex,.  ** ther
2c2ed 65 20 69 73 20 6e 6f 74 20 6d 75 63 68 20 53 51  e is not much SQ
2c2ee 4c 69 74 65 20 69 73 20 67 6f 69 6e 67 20 74 6f  Lite is going to
2c2ef 20 62 65 20 61 62 6c 65 20 74 6f 20 64 6f 2e 0a   be able to do..
2c2f0 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 65 20 6d 75    **.  ** The mu
2c2f1 74 65 78 20 73 75 62 73 79 73 74 65 6d 20 6d 75  tex subsystem mu
2c2f2 73 74 20 74 61 6b 65 20 63 61 72 65 20 6f 66 20  st take care of 
2c2f3 73 65 72 69 61 6c 69 7a 69 6e 67 20 69 74 73 20  serializing its 
2c2f4 6f 77 6e 0a 20 20 2a 2a 20 69 6e 69 74 69 61 6c  own.  ** initial
2c2f5 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a 20 20  ization..  */.  
2c2f6 72 63 20 3d 20 73 71 6c 69 74 65 33 4d 75 74 65  rc = sqlite3Mute
2c2f7 78 49 6e 69 74 28 29 3b 0a 20 20 69 66 28 20 72  xInit();.  if( r
2c2f8 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  c ) return rc;..
2c2f9 20 20 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65 20    /* Initialize 
2c2fa 74 68 65 20 6d 61 6c 6c 6f 63 28 29 20 73 79 73  the malloc() sys
2c2fb 74 65 6d 20 61 6e 64 20 74 68 65 20 72 65 63 75  tem and the recu
2c2fc 72 73 69 76 65 20 70 49 6e 69 74 4d 75 74 65 78  rsive pInitMutex
2c2fd 20 6d 75 74 65 78 2e 0a 20 20 2a 2a 20 54 68 69   mutex..  ** Thi
2c2fe 73 20 6f 70 65 72 61 74 69 6f 6e 20 69 73 20 70  s operation is p
2c2ff 72 6f 74 65 63 74 65 64 20 62 79 20 74 68 65 20  rotected by the 
2c300 53 54 41 54 49 43 5f 4d 41 53 54 45 52 20 6d 75  STATIC_MASTER mu
2c301 74 65 78 2e 20 20 4e 6f 74 65 20 74 68 61 74 0a  tex.  Note that.
2c302 20 20 2a 2a 20 4d 75 74 65 78 41 6c 6c 6f 63 28    ** MutexAlloc(
2c303 29 20 69 73 20 63 61 6c 6c 65 64 20 66 6f 72 20  ) is called for 
2c304 61 20 73 74 61 74 69 63 20 6d 75 74 65 78 20 70  a static mutex p
2c305 72 69 6f 72 20 74 6f 20 69 6e 69 74 69 61 6c 69  rior to initiali
2c306 7a 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 6d 61  zing the.  ** ma
2c307 6c 6c 6f 63 20 73 75 62 73 79 73 74 65 6d 20 2d  lloc subsystem -
2c308 20 74 68 69 73 20 69 6d 70 6c 69 65 73 20 74 68   this implies th
2c309 61 74 20 74 68 65 20 61 6c 6c 6f 63 61 74 69 6f  at the allocatio
2c30a 6e 20 6f 66 20 61 20 73 74 61 74 69 63 0a 20 20  n of a static.  
2c30b 2a 2a 20 6d 75 74 65 78 20 6d 75 73 74 20 6e 6f  ** mutex must no
2c30c 74 20 72 65 71 75 69 72 65 20 73 75 70 70 6f 72  t require suppor
2c30d 74 20 66 72 6f 6d 20 74 68 65 20 6d 61 6c 6c 6f  t from the mallo
2c30e 63 20 73 75 62 73 79 73 74 65 6d 2e 0a 20 20 2a  c subsystem..  *
2c30f 2f 0a 20 20 70 4d 61 73 74 65 72 20 3d 20 73 71  /.  pMaster = sq
2c310 6c 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28  lite3MutexAlloc(
2c311 53 51 4c 49 54 45 5f 4d 55 54 45 58 5f 53 54 41  SQLITE_MUTEX_STA
2c312 54 49 43 5f 4d 41 53 54 45 52 29 3b 0a 20 20 73  TIC_MASTER);.  s
2c313 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2c314 65 72 28 70 4d 61 73 74 65 72 29 3b 0a 20 20 69  er(pMaster);.  i
2c315 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61  f( !sqlite3Globa
2c316 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f 63  lConfig.isMalloc
2c317 49 6e 69 74 20 29 7b 0a 20 20 20 20 72 63 20 3d  Init ){.    rc =
2c318 20 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 49 6e   sqlite3MallocIn
2c319 69 74 28 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  it();.  }.  if( 
2c31a 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
2c31b 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
2c31c 61 6c 43 6f 6e 66 69 67 2e 69 73 4d 61 6c 6c 6f  alConfig.isMallo
2c31d 63 49 6e 69 74 20 3d 20 31 3b 0a 20 20 20 20 69  cInit = 1;.    i
2c31e 66 28 20 21 73 71 6c 69 74 65 33 47 6c 6f 62 61  f( !sqlite3Globa
2c31f 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75 74  lConfig.pInitMut
2c320 65 78 20 29 7b 0a 20 20 20 20 20 20 73 71 6c 69  ex ){.      sqli
2c321 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2c322 70 49 6e 69 74 4d 75 74 65 78 20 3d 20 73 71 6c  pInitMutex = sql
2c323 69 74 65 33 4d 75 74 65 78 41 6c 6c 6f 63 28 53  ite3MutexAlloc(S
2c324 51 4c 49 54 45 5f 4d 55 54 45 58 5f 52 45 43 55  QLITE_MUTEX_RECU
2c325 52 53 49 56 45 29 3b 0a 20 20 20 20 20 20 69 66  RSIVE);.      if
2c326 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2c327 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
2c328 20 26 26 20 21 73 71 6c 69 74 65 33 47 6c 6f 62   && !sqlite3Glob
2c329 61 6c 43 6f 6e 66 69 67 2e 70 49 6e 69 74 4d 75  alConfig.pInitMu
2c32a 74 65 78 20 29 7b 0a 20 20 20 20 20 20 20 20 72  tex ){.        r
2c32b 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
2c32c 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2c32d 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53 51    }.  if( rc==SQ
2c32e 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 73  LITE_OK ){.    s
2c32f 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2c330 69 67 2e 6e 52 65 66 49 6e 69 74 4d 75 74 65 78  ig.nRefInitMutex
2c331 2b 2b 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65  ++;.  }.  sqlite
2c332 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d  3_mutex_leave(pM
2c333 61 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 49 66  aster);..  /* If
2c334 20 75 6e 61 62 6c 65 20 74 6f 20 69 6e 69 74 69   unable to initi
2c335 61 6c 69 7a 65 20 74 68 65 20 6d 61 6c 6c 6f 63  alize the malloc
2c336 20 73 75 62 73 79 73 74 65 6d 2c 20 74 68 65 6e   subsystem, then
2c337 20 72 65 74 75 72 6e 20 65 61 72 6c 79 2e 0a 20   return early.. 
2c338 20 2a 2a 20 54 68 65 72 65 20 69 73 20 6c 69 74   ** There is lit
2c339 74 6c 65 20 68 6f 70 65 20 6f 66 20 67 65 74 74  tle hope of gett
2c33a 69 6e 67 20 53 51 4c 69 74 65 20 74 6f 20 72 75  ing SQLite to ru
2c33b 6e 20 69 66 20 74 68 65 20 6d 61 6c 6c 6f 63 0a  n if the malloc.
2c33c 20 20 2a 2a 20 73 75 62 73 79 73 74 65 6d 20 63    ** subsystem c
2c33d 61 6e 6e 6f 74 20 62 65 20 69 6e 69 74 69 61 6c  annot be initial
2c33e 69 7a 65 64 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ized..  */.  if(
2c33f 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2c340 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  {.    return rc;
2c341 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 6f 20 74 68  .  }..  /* Do th
2c342 65 20 72 65 73 74 20 6f 66 20 74 68 65 20 69 6e  e rest of the in
2c343 69 74 69 61 6c 69 7a 61 74 69 6f 6e 20 75 6e 64  itialization und
2c344 65 72 20 74 68 65 20 72 65 63 75 72 73 69 76 65  er the recursive
2c345 20 6d 75 74 65 78 20 73 6f 0a 20 20 2a 2a 20 74   mutex so.  ** t
2c346 68 61 74 20 77 65 20 77 69 6c 6c 20 62 65 20 61  hat we will be a
2c347 62 6c 65 20 74 6f 20 68 61 6e 64 6c 65 20 72 65  ble to handle re
2c348 63 75 72 73 69 76 65 20 63 61 6c 6c 73 20 69 6e  cursive calls in
2c349 74 6f 0a 20 20 2a 2a 20 73 71 6c 69 74 65 33 5f  to.  ** sqlite3_
2c34a 69 6e 69 74 69 61 6c 69 7a 65 28 29 2e 20 20 54  initialize().  T
2c34b 68 65 20 72 65 63 75 72 73 69 76 65 20 63 61 6c  he recursive cal
2c34c 6c 73 20 6e 6f 72 6d 61 6c 6c 79 20 63 6f 6d 65  ls normally come
2c34d 20 74 68 72 6f 75 67 68 0a 20 20 2a 2a 20 73 71   through.  ** sq
2c34e 6c 69 74 65 33 5f 6f 73 5f 69 6e 69 74 28 29 20  lite3_os_init() 
2c34f 77 68 65 6e 20 69 74 20 69 6e 76 6f 6b 65 73 20  when it invokes 
2c350 73 71 6c 69 74 65 33 5f 76 66 73 5f 72 65 67 69  sqlite3_vfs_regi
2c351 73 74 65 72 28 29 2c 20 62 75 74 20 6f 74 68 65  ster(), but othe
2c352 72 0a 20 20 2a 2a 20 72 65 63 75 72 73 69 76 65  r.  ** recursive
2c353 20 63 61 6c 6c 73 20 6d 69 67 68 74 20 61 6c 73   calls might als
2c354 6f 20 62 65 20 70 6f 73 73 69 62 6c 65 2e 0a 20  o be possible.. 
2c355 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75   */.  sqlite3_mu
2c356 74 65 78 5f 65 6e 74 65 72 28 73 71 6c 69 74 65  tex_enter(sqlite
2c357 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
2c358 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 69 66 28  nitMutex);.  if(
2c359 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2c35a 6e 66 69 67 2e 69 73 49 6e 69 74 3d 3d 30 20 26  nfig.isInit==0 &
2c35b 26 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  & sqlite3GlobalC
2c35c 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73  onfig.inProgress
2c35d 3d 3d 30 20 29 7b 0a 20 20 20 20 46 75 6e 63 44  ==0 ){.    FuncD
2c35e 65 66 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20  efHash *pHash = 
2c35f 26 47 4c 4f 42 41 4c 28 46 75 6e 63 44 65 66 48  &GLOBAL(FuncDefH
2c360 61 73 68 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62  ash, sqlite3Glob
2c361 61 6c 46 75 6e 63 74 69 6f 6e 73 29 3b 0a 20 20  alFunctions);.  
2c362 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2c363 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72 65 73 73  onfig.inProgress
2c364 20 3d 20 31 3b 0a 20 20 20 20 6d 65 6d 73 65 74   = 1;.    memset
2c365 28 70 48 61 73 68 2c 20 30 2c 20 73 69 7a 65 6f  (pHash, 0, sizeo
2c366 66 28 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 46  f(sqlite3GlobalF
2c367 75 6e 63 74 69 6f 6e 73 29 29 3b 0a 20 20 20 20  unctions));.    
2c368 73 71 6c 69 74 65 33 52 65 67 69 73 74 65 72 47  sqlite3RegisterG
2c369 6c 6f 62 61 6c 46 75 6e 63 74 69 6f 6e 73 28 29  lobalFunctions()
2c36a 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
2c36b 65 33 5f 6f 73 5f 69 6e 69 74 28 29 3b 0a 20 20  e3_os_init();.  
2c36c 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2c36d 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
2c36e 3d 20 73 71 6c 69 74 65 33 50 63 61 63 68 65 49  = sqlite3PcacheI
2c36f 6e 69 74 69 61 6c 69 7a 65 28 29 3b 0a 20 20 20  nitialize();.   
2c370 20 20 20 73 71 6c 69 74 65 33 50 43 61 63 68 65     sqlite3PCache
2c371 42 75 66 66 65 72 53 65 74 75 70 28 20 73 71 6c  BufferSetup( sql
2c372 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c373 2e 70 50 61 67 65 2c 20 0a 20 20 20 20 20 20 20  .pPage, .       
2c374 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2c375 43 6f 6e 66 69 67 2e 73 7a 50 61 67 65 2c 20 73  Config.szPage, s
2c376 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2c377 69 67 2e 6e 50 61 67 65 29 3b 0a 20 20 20 20 7d  ig.nPage);.    }
2c378 0a 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62  .    sqlite3Glob
2c379 61 6c 43 6f 6e 66 69 67 2e 69 6e 50 72 6f 67 72  alConfig.inProgr
2c37a 65 73 73 20 3d 20 30 3b 0a 20 20 20 20 73 71 6c  ess = 0;.    sql
2c37b 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c37c 2e 69 73 49 6e 69 74 20 3d 20 28 72 63 3d 3d 53  .isInit = (rc==S
2c37d 51 4c 49 54 45 5f 4f 4b 20 3f 20 31 20 3a 20 30  QLITE_OK ? 1 : 0
2c37e 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2c37f 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 73 71 6c  _mutex_leave(sql
2c380 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c381 2e 70 49 6e 69 74 4d 75 74 65 78 29 3b 0a 0a 20  .pInitMutex);.. 
2c382 20 2f 2a 20 47 6f 20 62 61 63 6b 20 75 6e 64 65   /* Go back unde
2c383 72 20 74 68 65 20 73 74 61 74 69 63 20 6d 75 74  r the static mut
2c384 65 78 20 61 6e 64 20 63 6c 65 61 6e 20 75 70 20  ex and clean up 
2c385 74 68 65 20 72 65 63 75 72 73 69 76 65 0a 20 20  the recursive.  
2c386 2a 2a 20 6d 75 74 65 78 20 74 6f 20 70 72 65 76  ** mutex to prev
2c387 65 6e 74 20 61 20 72 65 73 6f 75 72 63 65 20 6c  ent a resource l
2c388 65 61 6b 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69  eak..  */.  sqli
2c389 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28  te3_mutex_enter(
2c38a 70 4d 61 73 74 65 72 29 3b 0a 20 20 73 71 6c 69  pMaster);.  sqli
2c38b 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2c38c 6e 52 65 66 49 6e 69 74 4d 75 74 65 78 2d 2d 3b  nRefInitMutex--;
2c38d 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
2c38e 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 52 65 66 49  obalConfig.nRefI
2c38f 6e 69 74 4d 75 74 65 78 3c 3d 30 20 29 7b 0a 20  nitMutex<=0 ){. 
2c390 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2c391 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e  e3GlobalConfig.n
2c392 52 65 66 49 6e 69 74 4d 75 74 65 78 3d 3d 30 20  RefInitMutex==0 
2c393 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 6d  );.    sqlite3_m
2c394 75 74 65 78 5f 66 72 65 65 28 73 71 6c 69 74 65  utex_free(sqlite
2c395 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 70 49  3GlobalConfig.pI
2c396 6e 69 74 4d 75 74 65 78 29 3b 0a 20 20 20 20 73  nitMutex);.    s
2c397 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2c398 69 67 2e 70 49 6e 69 74 4d 75 74 65 78 20 3d 20  ig.pInitMutex = 
2c399 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
2c39a 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 70 4d 61  _mutex_leave(pMa
2c39b 73 74 65 72 29 3b 0a 0a 20 20 2f 2a 20 54 68 65  ster);..  /* The
2c39c 20 66 6f 6c 6c 6f 77 69 6e 67 20 69 73 20 6a 75   following is ju
2c39d 73 74 20 61 20 73 61 6e 69 74 79 20 63 68 65 63  st a sanity chec
2c39e 6b 20 74 6f 20 6d 61 6b 65 20 73 75 72 65 20 53  k to make sure S
2c39f 51 4c 69 74 65 20 68 61 73 0a 20 20 2a 2a 20 62  QLite has.  ** b
2c3a0 65 65 6e 20 63 6f 6d 70 69 6c 65 64 20 63 6f 72  een compiled cor
2c3a1 72 65 63 74 6c 79 2e 20 20 49 74 20 69 73 20 69  rectly.  It is i
2c3a2 6d 70 6f 72 74 61 6e 74 20 74 6f 20 72 75 6e 20  mportant to run 
2c3a3 74 68 69 73 20 63 6f 64 65 2c 20 62 75 74 0a 20  this code, but. 
2c3a4 20 2a 2a 20 77 65 20 64 6f 6e 27 74 20 77 61 6e   ** we don't wan
2c3a5 74 20 74 6f 20 72 75 6e 20 69 74 20 74 6f 6f 20  t to run it too 
2c3a6 6f 66 74 65 6e 20 61 6e 64 20 73 6f 61 6b 20 75  often and soak u
2c3a7 70 20 43 50 55 20 63 79 63 6c 65 73 20 66 6f 72  p CPU cycles for
2c3a8 20 6e 6f 0a 20 20 2a 2a 20 72 65 61 73 6f 6e 2e   no.  ** reason.
2c3a9 20 20 53 6f 20 77 65 20 72 75 6e 20 69 74 20 6f    So we run it o
2c3aa 6e 63 65 20 64 75 72 69 6e 67 20 69 6e 69 74 69  nce during initi
2c3ab 61 6c 69 7a 61 74 69 6f 6e 2e 0a 20 20 2a 2f 0a  alization..  */.
2c3ac 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
2c3ad 20 2f 2a 20 54 68 69 73 20 73 65 63 74 69 6f 6e   /* This section
2c3ae 20 6f 66 20 63 6f 64 65 27 73 20 6f 6e 6c 79 20   of code's only 
2c3af 22 6f 75 74 70 75 74 22 20 69 73 20 76 69 61 20  "output" is via 
2c3b0 61 73 73 65 72 74 28 29 20 73 74 61 74 65 6d 65  assert() stateme
2c3b1 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 20 28 20 72  nts. */.  if ( r
2c3b2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c==SQLITE_OK ){.
2c3b3 20 20 20 20 75 36 34 20 78 20 3d 20 28 28 28 75      u64 x = (((u
2c3b4 36 34 29 31 29 3c 3c 36 33 29 2d 31 3b 0a 20 20  64)1)<<63)-1;.  
2c3b5 20 20 64 6f 75 62 6c 65 20 79 3b 0a 20 20 20 20    double y;.    
2c3b6 61 73 73 65 72 74 28 73 69 7a 65 6f 66 28 78 29  assert(sizeof(x)
2c3b7 3d 3d 38 29 3b 0a 20 20 20 20 61 73 73 65 72 74  ==8);.    assert
2c3b8 28 73 69 7a 65 6f 66 28 78 29 3d 3d 73 69 7a 65  (sizeof(x)==size
2c3b9 6f 66 28 79 29 29 3b 0a 20 20 20 20 6d 65 6d 63  of(y));.    memc
2c3ba 70 79 28 26 79 2c 20 26 78 2c 20 38 29 3b 0a 20  py(&y, &x, 8);. 
2c3bb 20 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74     assert( sqlit
2c3bc 65 33 49 73 4e 61 4e 28 79 29 20 29 3b 0a 20 20  e3IsNaN(y) );.  
2c3bd 7d 0a 23 65 6e 64 69 66 0a 0a 20 20 72 65 74 75  }.#endif..  retu
2c3be 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  rn rc;.}../*.** 
2c3bf 55 6e 64 6f 20 74 68 65 20 65 66 66 65 63 74 73  Undo the effects
2c3c0 20 6f 66 20 73 71 6c 69 74 65 33 5f 69 6e 69 74   of sqlite3_init
2c3c1 69 61 6c 69 7a 65 28 29 2e 20 20 4d 75 73 74 20  ialize().  Must 
2c3c2 6e 6f 74 20 62 65 20 63 61 6c 6c 65 64 20 77 68  not be called wh
2c3c3 69 6c 65 0a 2a 2a 20 74 68 65 72 65 20 61 72 65  ile.** there are
2c3c4 20 6f 75 74 73 74 61 6e 64 69 6e 67 20 64 61 74   outstanding dat
2c3c5 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2c3c6 73 20 6f 72 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f  s or memory allo
2c3c7 63 61 74 69 6f 6e 73 20 6f 72 0a 2a 2a 20 77 68  cations or.** wh
2c3c8 69 6c 65 20 61 6e 79 20 70 61 72 74 20 6f 66 20  ile any part of 
2c3c9 53 51 4c 69 74 65 20 69 73 20 6f 74 68 65 72 77  SQLite is otherw
2c3ca 69 73 65 20 69 6e 20 75 73 65 20 69 6e 20 61 6e  ise in use in an
2c3cb 79 20 74 68 72 65 61 64 2e 20 20 54 68 69 73 0a  y thread.  This.
2c3cc 2a 2a 20 72 6f 75 74 69 6e 65 20 69 73 20 6e 6f  ** routine is no
2c3cd 74 20 74 68 72 65 61 64 73 61 66 65 2e 20 20 4e  t threadsafe.  N
2c3ce 6f 74 20 62 79 20 61 20 6c 6f 6e 67 20 73 68 6f  ot by a long sho
2c3cf 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  t..*/.SQLITE_API
2c3d0 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 73 68 75   int sqlite3_shu
2c3d1 74 64 6f 77 6e 28 76 6f 69 64 29 7b 0a 20 20 73  tdown(void){.  s
2c3d2 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2c3d3 69 67 2e 69 73 4d 61 6c 6c 6f 63 49 6e 69 74 20  ig.isMallocInit 
2c3d4 3d 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 50 63  = 0;.  sqlite3Pc
2c3d5 61 63 68 65 53 68 75 74 64 6f 77 6e 28 29 3b 0a  acheShutdown();.
2c3d6 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c 6f    if( sqlite3Glo
2c3d7 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69 74  balConfig.isInit
2c3d8 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
2c3d9 6f 73 5f 65 6e 64 28 29 3b 0a 20 20 7d 0a 20 20  os_end();.  }.  
2c3da 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 45 6e 64  sqlite3MallocEnd
2c3db 28 29 3b 0a 20 20 73 71 6c 69 74 65 33 4d 75 74  ();.  sqlite3Mut
2c3dc 65 78 45 6e 64 28 29 3b 0a 20 20 73 71 6c 69 74  exEnd();.  sqlit
2c3dd 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 69  e3GlobalConfig.i
2c3de 73 49 6e 69 74 20 3d 20 30 3b 0a 20 20 72 65 74  sInit = 0;.  ret
2c3df 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2c3e0 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 41 50 49  ../*.** This API
2c3e1 20 61 6c 6c 6f 77 73 20 61 70 70 6c 69 63 61 74   allows applicat
2c3e2 69 6f 6e 73 20 74 6f 20 6d 6f 64 69 66 79 20 74  ions to modify t
2c3e3 68 65 20 67 6c 6f 62 61 6c 20 63 6f 6e 66 69 67  he global config
2c3e4 75 72 61 74 69 6f 6e 20 6f 66 0a 2a 2a 20 74 68  uration of.** th
2c3e5 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61 72 79  e SQLite library
2c3e6 20 61 74 20 72 75 6e 2d 74 69 6d 65 2e 0a 2a 2a   at run-time..**
2c3e7 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69 6e 65  .** This routine
2c3e8 20 73 68 6f 75 6c 64 20 6f 6e 6c 79 20 62 65 20   should only be 
2c3e9 63 61 6c 6c 65 64 20 77 68 65 6e 20 74 68 65 72  called when ther
2c3ea 65 20 61 72 65 20 6e 6f 20 6f 75 74 73 74 61 6e  e are no outstan
2c3eb 64 69 6e 67 0a 2a 2a 20 64 61 74 61 62 61 73 65  ding.** database
2c3ec 20 63 6f 6e 6e 65 63 74 69 6f 6e 73 20 6f 72 20   connections or 
2c3ed 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69 6f  memory allocatio
2c3ee 6e 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e  ns.  This routin
2c3ef 65 20 69 73 20 6e 6f 74 0a 2a 2a 20 74 68 72 65  e is not.** thre
2c3f0 61 64 73 61 66 65 2e 20 20 46 61 69 6c 75 72 65  adsafe.  Failure
2c3f1 20 74 6f 20 68 65 65 64 20 74 68 65 73 65 20 77   to heed these w
2c3f2 61 72 6e 69 6e 67 73 20 63 61 6e 20 6c 65 61 64  arnings can lead
2c3f3 20 74 6f 20 75 6e 70 72 65 64 69 63 74 61 62 6c   to unpredictabl
2c3f4 65 0a 2a 2a 20 62 65 68 61 76 69 6f 72 2e 0a 2a  e.** behavior..*
2c3f5 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2c3f6 20 73 71 6c 69 74 65 33 5f 63 6f 6e 66 69 67 28   sqlite3_config(
2c3f7 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20 20  int op, ...){.  
2c3f8 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69 6e  va_list ap;.  in
2c3f9 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
2c3fa 3b 0a 0a 20 20 2f 2a 20 73 71 6c 69 74 65 33 5f  ;..  /* sqlite3_
2c3fb 63 6f 6e 66 69 67 28 29 20 73 68 61 6c 6c 20 72  config() shall r
2c3fc 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4d 49 53  eturn SQLITE_MIS
2c3fd 55 53 45 20 69 66 20 69 74 20 69 73 20 69 6e 76  USE if it is inv
2c3fe 6f 6b 65 64 20 77 68 69 6c 65 0a 20 20 2a 2a 20  oked while.  ** 
2c3ff 74 68 65 20 53 51 4c 69 74 65 20 6c 69 62 72 61  the SQLite libra
2c400 72 79 20 69 73 20 69 6e 20 75 73 65 2e 20 2a 2f  ry is in use. */
2c401 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 47 6c  .  if( sqlite3Gl
2c402 6f 62 61 6c 43 6f 6e 66 69 67 2e 69 73 49 6e 69  obalConfig.isIni
2c403 74 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  t ) return SQLIT
2c404 45 5f 4d 49 53 55 53 45 3b 0a 0a 20 20 76 61 5f  E_MISUSE;..  va_
2c405 73 74 61 72 74 28 61 70 2c 20 6f 70 29 3b 0a 20  start(ap, op);. 
2c406 20 73 77 69 74 63 68 28 20 6f 70 20 29 7b 0a 0a   switch( op ){..
2c407 20 20 20 20 2f 2a 20 4d 75 74 65 78 20 63 6f 6e      /* Mutex con
2c408 66 69 67 75 72 61 74 69 6f 6e 20 6f 70 74 69 6f  figuration optio
2c409 6e 73 20 61 72 65 20 6f 6e 6c 79 20 61 76 61 69  ns are only avai
2c40a 6c 61 62 6c 65 20 69 6e 20 61 20 74 68 72 65 61  lable in a threa
2c40b 64 73 61 66 65 0a 20 20 20 20 2a 2a 20 63 6f 6d  dsafe.    ** com
2c40c 70 69 6c 65 2e 20 0a 20 20 20 20 2a 2f 0a 23 69  pile. .    */.#i
2c40d 66 20 53 51 4c 49 54 45 5f 54 48 52 45 41 44 53  f SQLITE_THREADS
2c40e 41 46 45 0a 20 20 20 20 63 61 73 65 20 53 51 4c  AFE.    case SQL
2c40f 49 54 45 5f 43 4f 4e 46 49 47 5f 53 49 4e 47 4c  ITE_CONFIG_SINGL
2c410 45 54 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20  ETHREAD: {.     
2c411 20 2f 2a 20 44 69 73 61 62 6c 65 20 61 6c 6c 20   /* Disable all 
2c412 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20 20 20  mutexing */.    
2c413 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2c414 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74 65 78  onfig.bCoreMutex
2c415 20 3d 20 30 3b 0a 20 20 20 20 20 20 73 71 6c 69   = 0;.      sqli
2c416 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2c417 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 30 3b 0a  bFullMutex = 0;.
2c418 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2c419 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49   }.    case SQLI
2c41a 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 4c 54 49 54  TE_CONFIG_MULTIT
2c41b 48 52 45 41 44 3a 20 7b 0a 20 20 20 20 20 20 2f  HREAD: {.      /
2c41c 2a 20 44 69 73 61 62 6c 65 20 6d 75 74 65 78 69  * Disable mutexi
2c41d 6e 67 20 6f 66 20 64 61 74 61 62 61 73 65 20 63  ng of database c
2c41e 6f 6e 6e 65 63 74 69 6f 6e 73 20 2a 2f 0a 20 20  onnections */.  
2c41f 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 6d 75      /* Enable mu
2c420 74 65 78 69 6e 67 20 6f 66 20 63 6f 72 65 20 64  texing of core d
2c421 61 74 61 20 73 74 72 75 63 74 75 72 65 73 20 2a  ata structures *
2c422 2f 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  /.      sqlite3G
2c423 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 43 6f 72  lobalConfig.bCor
2c424 65 4d 75 74 65 78 20 3d 20 31 3b 0a 20 20 20 20  eMutex = 1;.    
2c425 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43    sqlite3GlobalC
2c426 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75 74 65 78  onfig.bFullMutex
2c427 20 3d 20 30 3b 0a 20 20 20 20 20 20 62 72 65 61   = 0;.      brea
2c428 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  k;.    }.    cas
2c429 65 20 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f  e SQLITE_CONFIG_
2c42a 53 45 52 49 41 4c 49 5a 45 44 3a 20 7b 0a 20 20  SERIALIZED: {.  
2c42b 20 20 20 20 2f 2a 20 45 6e 61 62 6c 65 20 61 6c      /* Enable al
2c42c 6c 20 6d 75 74 65 78 69 6e 67 20 2a 2f 0a 20 20  l mutexing */.  
2c42d 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61      sqlite3Globa
2c42e 6c 43 6f 6e 66 69 67 2e 62 43 6f 72 65 4d 75 74  lConfig.bCoreMut
2c42f 65 78 20 3d 20 31 3b 0a 20 20 20 20 20 20 73 71  ex = 1;.      sq
2c430 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2c431 67 2e 62 46 75 6c 6c 4d 75 74 65 78 20 3d 20 31  g.bFullMutex = 1
2c432 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  ;.      break;. 
2c433 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 53 51     }.    case SQ
2c434 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 55 54 45  LITE_CONFIG_MUTE
2c435 58 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70  X: {.      /* Sp
2c436 65 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61  ecify an alterna
2c437 74 69 76 65 20 6d 75 74 65 78 20 69 6d 70 6c 65  tive mutex imple
2c438 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  mentation */.   
2c439 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2c43a 43 6f 6e 66 69 67 2e 6d 75 74 65 78 20 3d 20 2a  Config.mutex = *
2c43b 76 61 5f 61 72 67 28 61 70 2c 20 73 71 6c 69 74  va_arg(ap, sqlit
2c43c 65 33 5f 6d 75 74 65 78 5f 6d 65 74 68 6f 64 73  e3_mutex_methods
2c43d 2a 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  *);.      break;
2c43e 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2c43f 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45  SQLITE_CONFIG_GE
2c440 54 4d 55 54 45 58 3a 20 7b 0a 20 20 20 20 20 20  TMUTEX: {.      
2c441 2f 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  /* Retrieve the 
2c442 63 75 72 72 65 6e 74 20 6d 75 74 65 78 20 69 6d  current mutex im
2c443 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a  plementation */.
2c444 20 20 20 20 20 20 2a 76 61 5f 61 72 67 28 61 70        *va_arg(ap
2c445 2c 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f  , sqlite3_mutex_
2c446 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c 69  methods*) = sqli
2c447 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2c448 6d 75 74 65 78 3b 0a 20 20 20 20 20 20 62 72 65  mutex;.      bre
2c449 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66  ak;.    }.#endif
2c44a 0a 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  ...    case SQLI
2c44b 54 45 5f 43 4f 4e 46 49 47 5f 4d 41 4c 4c 4f 43  TE_CONFIG_MALLOC
2c44c 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 53 70 65  : {.      /* Spe
2c44d 63 69 66 79 20 61 6e 20 61 6c 74 65 72 6e 61 74  cify an alternat
2c44e 69 76 65 20 6d 61 6c 6c 6f 63 20 69 6d 70 6c 65  ive malloc imple
2c44f 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  mentation */.   
2c450 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2c451 43 6f 6e 66 69 67 2e 6d 20 3d 20 2a 76 61 5f 61  Config.m = *va_a
2c452 72 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d  rg(ap, sqlite3_m
2c453 65 6d 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a 20 20  em_methods*);.  
2c454 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2c455 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2c456 5f 43 4f 4e 46 49 47 5f 47 45 54 4d 41 4c 4c 4f  _CONFIG_GETMALLO
2c457 43 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 52 65  C: {.      /* Re
2c458 74 72 69 65 76 65 20 74 68 65 20 63 75 72 72 65  trieve the curre
2c459 6e 74 20 6d 61 6c 6c 6f 63 28 29 20 69 6d 70 6c  nt malloc() impl
2c45a 65 6d 65 6e 74 61 74 69 6f 6e 20 2a 2f 0a 20 20  ementation */.  
2c45b 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 47      if( sqlite3G
2c45c 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 2e 78 4d  lobalConfig.m.xM
2c45d 61 6c 6c 6f 63 3d 3d 30 20 29 20 73 71 6c 69 74  alloc==0 ) sqlit
2c45e 65 33 4d 65 6d 53 65 74 44 65 66 61 75 6c 74 28  e3MemSetDefault(
2c45f 29 3b 0a 20 20 20 20 20 20 2a 76 61 5f 61 72 67  );.      *va_arg
2c460 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 6d 65 6d  (ap, sqlite3_mem
2c461 5f 6d 65 74 68 6f 64 73 2a 29 20 3d 20 73 71 6c  _methods*) = sql
2c462 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c463 2e 6d 3b 0a 20 20 20 20 20 20 62 72 65 61 6b 3b  .m;.      break;
2c464 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20  .    }.    case 
2c465 53 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4d 45  SQLITE_CONFIG_ME
2c466 4d 53 54 41 54 55 53 3a 20 7b 0a 20 20 20 20 20  MSTATUS: {.     
2c467 20 2f 2a 20 45 6e 61 62 6c 65 20 6f 72 20 64 69   /* Enable or di
2c468 73 61 62 6c 65 20 74 68 65 20 6d 61 6c 6c 6f 63  sable the malloc
2c469 20 73 74 61 74 75 73 20 63 6f 6c 6c 65 63 74 69   status collecti
2c46a 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c 69  on */.      sqli
2c46b 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2c46c 62 4d 65 6d 73 74 61 74 20 3d 20 76 61 5f 61 72  bMemstat = va_ar
2c46d 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20  g(ap, int);.    
2c46e 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20    break;.    }. 
2c46f 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43     case SQLITE_C
2c470 4f 4e 46 49 47 5f 53 43 52 41 54 43 48 3a 20 7b  ONFIG_SCRATCH: {
2c471 0a 20 20 20 20 20 20 2f 2a 20 44 65 73 69 67 6e  .      /* Design
2c472 61 74 65 20 61 20 62 75 66 66 65 72 20 66 6f 72  ate a buffer for
2c473 20 73 63 72 61 74 63 68 20 6d 65 6d 6f 72 79 20   scratch memory 
2c474 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20 73  space */.      s
2c475 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2c476 69 67 2e 70 53 63 72 61 74 63 68 20 3d 20 76 61  ig.pScratch = va
2c477 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b  _arg(ap, void*);
2c478 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c  .      sqlite3Gl
2c479 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 53 63 72  obalConfig.szScr
2c47a 61 74 63 68 20 3d 20 76 61 5f 61 72 67 28 61 70  atch = va_arg(ap
2c47b 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71  , int);.      sq
2c47c 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2c47d 67 2e 6e 53 63 72 61 74 63 68 20 3d 20 76 61 5f  g.nScratch = va_
2c47e 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a 20 20  arg(ap, int);.  
2c47f 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2c480 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2c481 5f 43 4f 4e 46 49 47 5f 50 41 47 45 43 41 43 48  _CONFIG_PAGECACH
2c482 45 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65  E: {.      /* De
2c483 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65 72  signate a buffer
2c484 20 66 6f 72 20 73 63 72 61 74 63 68 20 6d 65 6d   for scratch mem
2c485 6f 72 79 20 73 70 61 63 65 20 2a 2f 0a 20 20 20  ory space */.   
2c486 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c     sqlite3Global
2c487 43 6f 6e 66 69 67 2e 70 50 61 67 65 20 3d 20 76  Config.pPage = v
2c488 61 5f 61 72 67 28 61 70 2c 20 76 6f 69 64 2a 29  a_arg(ap, void*)
2c489 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
2c48a 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 73 7a 50 61  lobalConfig.szPa
2c48b 67 65 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20  ge = va_arg(ap, 
2c48c 69 6e 74 29 3b 0a 20 20 20 20 20 20 73 71 6c 69  int);.      sqli
2c48d 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2c48e 6e 50 61 67 65 20 3d 20 76 61 5f 61 72 67 28 61  nPage = va_arg(a
2c48f 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 62  p, int);.      b
2c490 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
2c491 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 4e   case SQLITE_CON
2c492 46 49 47 5f 50 43 41 43 48 45 3a 20 7b 0a 20 20  FIG_PCACHE: {.  
2c493 20 20 20 20 2f 2a 20 53 70 65 63 69 66 79 20 61      /* Specify a
2c494 6e 20 61 6c 74 65 72 6e 61 74 69 76 65 20 6d 61  n alternative ma
2c495 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lloc implementat
2c496 69 6f 6e 20 2a 2f 0a 20 20 20 20 20 20 73 71 6c  ion */.      sql
2c497 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c498 2e 70 63 61 63 68 65 20 3d 20 2a 76 61 5f 61 72  .pcache = *va_ar
2c499 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70 63  g(ap, sqlite3_pc
2c49a 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29 3b 0a  ache_methods*);.
2c49b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2c49c 20 7d 0a 0a 20 20 20 20 63 61 73 65 20 53 51 4c   }..    case SQL
2c49d 49 54 45 5f 43 4f 4e 46 49 47 5f 47 45 54 50 43  ITE_CONFIG_GETPC
2c49e 41 43 48 45 3a 20 7b 0a 20 20 20 20 20 20 69 66  ACHE: {.      if
2c49f 28 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43  ( sqlite3GlobalC
2c4a0 6f 6e 66 69 67 2e 70 63 61 63 68 65 2e 78 49 6e  onfig.pcache.xIn
2c4a1 69 74 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20  it==0 ){.       
2c4a2 20 73 71 6c 69 74 65 33 50 43 61 63 68 65 53 65   sqlite3PCacheSe
2c4a3 74 44 65 66 61 75 6c 74 28 29 3b 0a 20 20 20 20  tDefault();.    
2c4a4 20 20 7d 0a 20 20 20 20 20 20 2a 76 61 5f 61 72    }.      *va_ar
2c4a5 67 28 61 70 2c 20 73 71 6c 69 74 65 33 5f 70 63  g(ap, sqlite3_pc
2c4a6 61 63 68 65 5f 6d 65 74 68 6f 64 73 2a 29 20 3d  ache_methods*) =
2c4a7 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f   sqlite3GlobalCo
2c4a8 6e 66 69 67 2e 70 63 61 63 68 65 3b 0a 20 20 20  nfig.pcache;.   
2c4a9 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a     break;.    }.
2c4aa 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53 51 4c  .#if defined(SQL
2c4ab 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59  ITE_ENABLE_MEMSY
2c4ac 53 33 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  S3) || defined(S
2c4ad 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
2c4ae 53 59 53 35 29 0a 20 20 20 20 63 61 73 65 20 53  SYS5).    case S
2c4af 51 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 48 45 41  QLITE_CONFIG_HEA
2c4b0 50 3a 20 7b 0a 20 20 20 20 20 20 2f 2a 20 44 65  P: {.      /* De
2c4b1 73 69 67 6e 61 74 65 20 61 20 62 75 66 66 65 72  signate a buffer
2c4b2 20 66 6f 72 20 68 65 61 70 20 6d 65 6d 6f 72 79   for heap memory
2c4b3 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20 20 20   space */.      
2c4b4 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2c4b5 66 69 67 2e 70 48 65 61 70 20 3d 20 76 61 5f 61  fig.pHeap = va_a
2c4b6 72 67 28 61 70 2c 20 76 6f 69 64 2a 29 3b 0a 20  rg(ap, void*);. 
2c4b7 20 20 20 20 20 73 71 6c 69 74 65 33 47 6c 6f 62       sqlite3Glob
2c4b8 61 6c 43 6f 6e 66 69 67 2e 6e 48 65 61 70 20 3d  alConfig.nHeap =
2c4b9 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
2c4ba 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
2c4bb 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 6e 52 65  lobalConfig.mnRe
2c4bc 71 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69  q = va_arg(ap, i
2c4bd 6e 74 29 3b 0a 0a 20 20 20 20 20 20 69 66 28 20  nt);..      if( 
2c4be 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e  sqlite3GlobalCon
2c4bf 66 69 67 2e 70 48 65 61 70 3d 3d 30 20 29 7b 0a  fig.pHeap==0 ){.
2c4c0 20 20 20 20 20 20 20 20 2f 2a 20 49 66 20 74 68          /* If th
2c4c1 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72 20 69  e heap pointer i
2c4c2 73 20 4e 55 4c 4c 2c 20 74 68 65 6e 20 72 65 73  s NULL, then res
2c4c3 74 6f 72 65 20 74 68 65 20 6d 61 6c 6c 6f 63 20  tore the malloc 
2c4c4 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 0a 20  implementation. 
2c4c5 20 20 20 20 20 20 20 2a 2a 20 62 61 63 6b 20 74         ** back t
2c4c6 6f 20 4e 55 4c 4c 20 70 6f 69 6e 74 65 72 73 20  o NULL pointers 
2c4c7 74 6f 6f 2e 20 20 54 68 69 73 20 77 69 6c 6c 20  too.  This will 
2c4c8 63 61 75 73 65 20 74 68 65 20 6d 61 6c 6c 6f 63  cause the malloc
2c4c9 20 74 6f 20 67 6f 0a 20 20 20 20 20 20 20 20 2a   to go.        *
2c4ca 2a 20 62 61 63 6b 20 74 6f 20 69 74 73 20 64 65  * back to its de
2c4cb 66 61 75 6c 74 20 69 6d 70 6c 65 6d 65 6e 74 61  fault implementa
2c4cc 74 69 6f 6e 20 77 68 65 6e 20 73 71 6c 69 74 65  tion when sqlite
2c4cd 33 5f 69 6e 69 74 69 61 6c 69 7a 65 28 29 20 69  3_initialize() i
2c4ce 73 0a 20 20 20 20 20 20 20 20 2a 2a 20 72 75 6e  s.        ** run
2c4cf 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20  ..        */.   
2c4d0 20 20 20 20 20 6d 65 6d 73 65 74 28 26 73 71 6c       memset(&sql
2c4d1 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c4d2 2e 6d 2c 20 30 2c 20 73 69 7a 65 6f 66 28 73 71  .m, 0, sizeof(sq
2c4d3 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2c4d4 67 2e 6d 29 29 3b 0a 20 20 20 20 20 20 7d 65 6c  g.m));.      }el
2c4d5 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
2c4d6 68 65 20 68 65 61 70 20 70 6f 69 6e 74 65 72 20  he heap pointer 
2c4d7 69 73 20 6e 6f 74 20 4e 55 4c 4c 2c 20 74 68 65  is not NULL, the
2c4d8 6e 20 69 6e 73 74 61 6c 6c 20 6f 6e 65 20 6f 66  n install one of
2c4d9 20 74 68 65 0a 20 20 20 20 20 20 20 20 2a 2a 20   the.        ** 
2c4da 6d 65 6d 35 2e 63 2f 6d 65 6d 33 2e 63 20 6d 65  mem5.c/mem3.c me
2c4db 74 68 6f 64 73 2e 20 49 66 20 6e 65 69 74 68 65  thods. If neithe
2c4dc 72 20 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 33  r ENABLE_MEMSYS3
2c4dd 20 6e 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20   nor.        ** 
2c4de 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53 35 20 69  ENABLE_MEMSYS5 i
2c4df 73 20 64 65 66 69 6e 65 64 2c 20 72 65 74 75 72  s defined, retur
2c4e0 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20  n an error..    
2c4e1 20 20 20 20 2a 2a 20 74 68 65 20 64 65 66 61 75      ** the defau
2c4e2 6c 74 20 63 61 73 65 20 61 6e 64 20 72 65 74 75  lt case and retu
2c4e3 72 6e 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20  rn an error..   
2c4e4 20 20 20 20 20 2a 2f 0a 23 69 66 64 65 66 20 53       */.#ifdef S
2c4e5 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d  QLITE_ENABLE_MEM
2c4e6 53 59 53 33 0a 20 20 20 20 20 20 20 20 73 71 6c  SYS3.        sql
2c4e7 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67  ite3GlobalConfig
2c4e8 2e 6d 20 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d  .m = *sqlite3Mem
2c4e9 47 65 74 4d 65 6d 73 79 73 33 28 29 3b 0a 23 65  GetMemsys3();.#e
2c4ea 6e 64 69 66 0a 23 69 66 64 65 66 20 53 51 4c 49  ndif.#ifdef SQLI
2c4eb 54 45 5f 45 4e 41 42 4c 45 5f 4d 45 4d 53 59 53  TE_ENABLE_MEMSYS
2c4ec 35 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65  5.        sqlite
2c4ed 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6d 20  3GlobalConfig.m 
2c4ee 3d 20 2a 73 71 6c 69 74 65 33 4d 65 6d 47 65 74  = *sqlite3MemGet
2c4ef 4d 65 6d 73 79 73 35 28 29 3b 0a 23 65 6e 64 69  Memsys5();.#endi
2c4f0 66 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  f.      }.      
2c4f1 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 23 65 6e  break;.    }.#en
2c4f2 64 69 66 0a 0a 20 20 20 20 63 61 73 65 20 53 51  dif..    case SQ
2c4f3 4c 49 54 45 5f 43 4f 4e 46 49 47 5f 4c 4f 4f 4b  LITE_CONFIG_LOOK
2c4f4 41 53 49 44 45 3a 20 7b 0a 20 20 20 20 20 20 73  ASIDE: {.      s
2c4f5 71 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66  qlite3GlobalConf
2c4f6 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69 64 65 20 3d  ig.szLookaside =
2c4f7 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29   va_arg(ap, int)
2c4f8 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 47  ;.      sqlite3G
2c4f9 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 6e 4c 6f 6f  lobalConfig.nLoo
2c4fa 6b 61 73 69 64 65 20 3d 20 76 61 5f 61 72 67 28  kaside = va_arg(
2c4fb 61 70 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20  ap, int);.      
2c4fc 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
2c4fd 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2c4fe 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45     rc = SQLITE_E
2c4ff 52 52 4f 52 3b 0a 20 20 20 20 20 20 62 72 65 61  RROR;.      brea
2c500 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 76  k;.    }.  }.  v
2c501 61 5f 65 6e 64 28 61 70 29 3b 0a 20 20 72 65 74  a_end(ap);.  ret
2c502 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  urn rc;.}../*.**
2c503 20 53 65 74 20 75 70 20 74 68 65 20 6c 6f 6f 6b   Set up the look
2c504 61 73 69 64 65 20 62 75 66 66 65 72 73 20 66 6f  aside buffers fo
2c505 72 20 61 20 64 61 74 61 62 61 73 65 20 63 6f 6e  r a database con
2c506 6e 65 63 74 69 6f 6e 2e 0a 2a 2a 20 52 65 74 75  nection..** Retu
2c507 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e 20  rn SQLITE_OK on 
2c508 73 75 63 63 65 73 73 2e 20 20 0a 2a 2a 20 49 66  success.  .** If
2c509 20 6c 6f 6f 6b 61 73 69 64 65 20 69 73 20 61 6c   lookaside is al
2c50a 72 65 61 64 79 20 61 63 74 69 76 65 2c 20 72 65  ready active, re
2c50b 74 75 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59  turn SQLITE_BUSY
2c50c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 7a 20 70  ..**.** The sz p
2c50d 61 72 61 6d 65 74 65 72 20 69 73 20 74 68 65 20  arameter is the 
2c50e 6e 75 6d 62 65 72 20 6f 66 20 62 79 74 65 73 20  number of bytes 
2c50f 69 6e 20 65 61 63 68 20 6c 6f 6f 6b 61 73 69 64  in each lookasid
2c510 65 20 73 6c 6f 74 2e 0a 2a 2a 20 54 68 65 20 63  e slot..** The c
2c511 6e 74 20 70 61 72 61 6d 65 74 65 72 20 69 73 20  nt parameter is 
2c512 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 73 6c  the number of sl
2c513 6f 74 73 2e 20 20 49 66 20 70 53 74 61 72 74 20  ots.  If pStart 
2c514 69 73 20 4e 55 4c 4c 20 74 68 65 0a 2a 2a 20 73  is NULL the.** s
2c515 70 61 63 65 20 66 6f 72 20 74 68 65 20 6c 6f 6f  pace for the loo
2c516 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 20 69 73  kaside memory is
2c517 20 6f 62 74 61 69 6e 65 64 20 66 72 6f 6d 20 73   obtained from s
2c518 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 29 2e  qlite3_malloc().
2c519 0a 2a 2a 20 49 66 20 70 53 74 61 72 74 20 69 73  .** If pStart is
2c51a 20 6e 6f 74 20 4e 55 4c 4c 20 74 68 65 6e 20 69   not NULL then i
2c51b 74 20 69 73 20 73 7a 2a 63 6e 74 20 62 79 74 65  t is sz*cnt byte
2c51c 73 20 6f 66 20 6d 65 6d 6f 72 79 20 74 6f 20 75  s of memory to u
2c51d 73 65 20 66 6f 72 0a 2a 2a 20 74 68 65 20 6c 6f  se for.** the lo
2c51e 6f 6b 61 73 69 64 65 20 6d 65 6d 6f 72 79 2e 0a  okaside memory..
2c51f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
2c520 74 75 70 4c 6f 6f 6b 61 73 69 64 65 28 73 71 6c  tupLookaside(sql
2c521 69 74 65 33 20 2a 64 62 2c 20 76 6f 69 64 20 2a  ite3 *db, void *
2c522 70 42 75 66 2c 20 69 6e 74 20 73 7a 2c 20 69 6e  pBuf, int sz, in
2c523 74 20 63 6e 74 29 7b 0a 20 20 76 6f 69 64 20 2a  t cnt){.  void *
2c524 70 53 74 61 72 74 3b 0a 20 20 69 66 28 20 64 62  pStart;.  if( db
2c525 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74  ->lookaside.nOut
2c526 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
2c527 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
2c528 20 20 69 66 28 20 73 7a 3c 30 20 29 20 73 7a 20    if( sz<0 ) sz 
2c529 3d 20 30 3b 0a 20 20 69 66 28 20 63 6e 74 3c 30  = 0;.  if( cnt<0
2c52a 20 29 20 63 6e 74 20 3d 20 30 3b 0a 20 20 69 66   ) cnt = 0;.  if
2c52b 28 20 70 42 75 66 3d 3d 30 20 29 7b 0a 20 20 20  ( pBuf==0 ){.   
2c52c 20 73 7a 20 3d 20 28 73 7a 20 2b 20 37 29 26 7e   sz = (sz + 7)&~
2c52d 37 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 42 65  7;.    sqlite3Be
2c52e 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63 28  ginBenignMalloc(
2c52f 29 3b 0a 20 20 20 20 70 53 74 61 72 74 20 3d 20  );.    pStart = 
2c530 73 71 6c 69 74 65 33 4d 61 6c 6c 6f 63 28 20 73  sqlite3Malloc( s
2c531 7a 2a 63 6e 74 20 29 3b 0a 20 20 20 20 73 71 6c  z*cnt );.    sql
2c532 69 74 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c  ite3EndBenignMal
2c533 6c 6f 63 28 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  loc();.  }else{.
2c534 20 20 20 20 73 7a 20 3d 20 73 7a 26 7e 37 3b 0a      sz = sz&~7;.
2c535 20 20 20 20 70 53 74 61 72 74 20 3d 20 70 42 75      pStart = pBu
2c536 66 3b 0a 20 20 7d 0a 20 20 69 66 28 20 64 62 2d  f;.  }.  if( db-
2c537 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c  >lookaside.bMall
2c538 6f 63 65 64 20 29 7b 0a 20 20 20 20 73 71 6c 69  oced ){.    sqli
2c539 74 65 33 5f 66 72 65 65 28 64 62 2d 3e 6c 6f 6f  te3_free(db->loo
2c53a 6b 61 73 69 64 65 2e 70 53 74 61 72 74 29 3b 0a  kaside.pStart);.
2c53b 20 20 7d 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73    }.  db->lookas
2c53c 69 64 65 2e 70 53 74 61 72 74 20 3d 20 70 53 74  ide.pStart = pSt
2c53d 61 72 74 3b 0a 20 20 64 62 2d 3e 6c 6f 6f 6b 61  art;.  db->looka
2c53e 73 69 64 65 2e 70 46 72 65 65 20 3d 20 30 3b 0a  side.pFree = 0;.
2c53f 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e    db->lookaside.
2c540 73 7a 20 3d 20 73 7a 3b 0a 20 20 64 62 2d 3e 6c  sz = sz;.  db->l
2c541 6f 6f 6b 61 73 69 64 65 2e 62 4d 61 6c 6c 6f 63  ookaside.bMalloc
2c542 65 64 20 3d 20 70 42 75 66 3d 3d 30 3b 0a 20 20  ed = pBuf==0;.  
2c543 69 66 28 20 70 53 74 61 72 74 20 29 7b 0a 20 20  if( pStart ){.  
2c544 20 20 69 6e 74 20 69 3b 0a 20 20 20 20 4c 6f 6f    int i;.    Loo
2c545 6b 61 73 69 64 65 53 6c 6f 74 20 2a 70 3b 0a 20  kasideSlot *p;. 
2c546 20 20 20 70 20 3d 20 28 4c 6f 6f 6b 61 73 69 64     p = (Lookasid
2c547 65 53 6c 6f 74 2a 29 70 53 74 61 72 74 3b 0a 20  eSlot*)pStart;. 
2c548 20 20 20 66 6f 72 28 69 3d 63 6e 74 2d 31 3b 20     for(i=cnt-1; 
2c549 69 3e 3d 30 3b 20 69 2d 2d 29 7b 0a 20 20 20 20  i>=0; i--){.    
2c54a 20 20 70 2d 3e 70 4e 65 78 74 20 3d 20 64 62 2d    p->pNext = db-
2c54b 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 46 72 65 65  >lookaside.pFree
2c54c 3b 0a 20 20 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b  ;.      db->look
2c54d 61 73 69 64 65 2e 70 46 72 65 65 20 3d 20 70 3b  aside.pFree = p;
2c54e 0a 20 20 20 20 20 20 70 20 3d 20 28 4c 6f 6f 6b  .      p = (Look
2c54f 61 73 69 64 65 53 6c 6f 74 2a 29 26 28 28 75 38  asideSlot*)&((u8
2c550 2a 29 70 29 5b 73 7a 5d 3b 0a 20 20 20 20 7d 0a  *)p)[sz];.    }.
2c551 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64      db->lookasid
2c552 65 2e 70 45 6e 64 20 3d 20 70 3b 0a 20 20 20 20  e.pEnd = p;.    
2c553 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 62 45  db->lookaside.bE
2c554 6e 61 62 6c 65 64 20 3d 20 31 3b 0a 20 20 7d 65  nabled = 1;.  }e
2c555 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f  lse{.    db->loo
2c556 6b 61 73 69 64 65 2e 70 45 6e 64 20 3d 20 30 3b  kaside.pEnd = 0;
2c557 0a 20 20 20 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69  .    db->lookasi
2c558 64 65 2e 62 45 6e 61 62 6c 65 64 20 3d 20 30 3b  de.bEnabled = 0;
2c559 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
2c55a 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a  LITE_OK;.}../*.*
2c55b 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6d 75 74  * Return the mut
2c55c 65 78 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ex associated wi
2c55d 74 68 20 61 20 64 61 74 61 62 61 73 65 20 63 6f  th a database co
2c55e 6e 6e 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  nnection..*/.SQL
2c55f 49 54 45 5f 41 50 49 20 73 71 6c 69 74 65 33 5f  ITE_API sqlite3_
2c560 6d 75 74 65 78 20 2a 73 71 6c 69 74 65 33 5f 64  mutex *sqlite3_d
2c561 62 5f 6d 75 74 65 78 28 73 71 6c 69 74 65 33 20  b_mutex(sqlite3 
2c562 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64  *db){.  return d
2c563 62 2d 3e 6d 75 74 65 78 3b 0a 7d 0a 0a 2f 2a 0a  b->mutex;.}../*.
2c564 2a 2a 20 43 6f 6e 66 69 67 75 72 61 74 69 6f 6e  ** Configuration
2c565 20 73 65 74 74 69 6e 67 73 20 66 6f 72 20 61 6e   settings for an
2c566 20 69 6e 64 69 76 69 64 75 61 6c 20 64 61 74 61   individual data
2c567 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 0a  base connection.
2c568 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2c569 74 20 73 71 6c 69 74 65 33 5f 64 62 5f 63 6f 6e  t sqlite3_db_con
2c56a 66 69 67 28 73 71 6c 69 74 65 33 20 2a 64 62 2c  fig(sqlite3 *db,
2c56b 20 69 6e 74 20 6f 70 2c 20 2e 2e 2e 29 7b 0a 20   int op, ...){. 
2c56c 20 76 61 5f 6c 69 73 74 20 61 70 3b 0a 20 20 69   va_list ap;.  i
2c56d 6e 74 20 72 63 3b 0a 20 20 76 61 5f 73 74 61 72  nt rc;.  va_star
2c56e 74 28 61 70 2c 20 6f 70 29 3b 0a 20 20 73 77 69  t(ap, op);.  swi
2c56f 74 63 68 28 20 6f 70 20 29 7b 0a 20 20 20 20 63  tch( op ){.    c
2c570 61 73 65 20 53 51 4c 49 54 45 5f 44 42 43 4f 4e  ase SQLITE_DBCON
2c571 46 49 47 5f 4c 4f 4f 4b 41 53 49 44 45 3a 20 7b  FIG_LOOKASIDE: {
2c572 0a 20 20 20 20 20 20 76 6f 69 64 20 2a 70 42 75  .      void *pBu
2c573 66 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  f = va_arg(ap, v
2c574 6f 69 64 2a 29 3b 0a 20 20 20 20 20 20 69 6e 74  oid*);.      int
2c575 20 73 7a 20 3d 20 76 61 5f 61 72 67 28 61 70 2c   sz = va_arg(ap,
2c576 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 69 6e 74   int);.      int
2c577 20 63 6e 74 20 3d 20 76 61 5f 61 72 67 28 61 70   cnt = va_arg(ap
2c578 2c 20 69 6e 74 29 3b 0a 20 20 20 20 20 20 72 63  , int);.      rc
2c579 20 3d 20 73 65 74 75 70 4c 6f 6f 6b 61 73 69 64   = setupLookasid
2c57a 65 28 64 62 2c 20 70 42 75 66 2c 20 73 7a 2c 20  e(db, pBuf, sz, 
2c57b 63 6e 74 29 3b 0a 20 20 20 20 20 20 62 72 65 61  cnt);.      brea
2c57c 6b 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 65 66  k;.    }.    def
2c57d 61 75 6c 74 3a 20 7b 0a 20 20 20 20 20 20 72 63  ault: {.      rc
2c57e 20 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b   = SQLITE_ERROR;
2c57f 0a 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20  .      break;.  
2c580 20 20 7d 0a 20 20 7d 0a 20 20 76 61 5f 65 6e 64    }.  }.  va_end
2c581 28 61 70 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  (ap);.  return r
2c582 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 75 74  c;.}../*.** Rout
2c583 69 6e 65 20 6e 65 65 64 65 64 20 74 6f 20 73 75  ine needed to su
2c584 70 70 6f 72 74 20 74 68 65 20 74 65 73 74 63 61  pport the testca
2c585 73 65 28 29 20 6d 61 63 72 6f 2e 0a 2a 2f 0a 23  se() macro..*/.#
2c586 69 66 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 56  ifdef SQLITE_COV
2c587 45 52 41 47 45 5f 54 45 53 54 0a 53 51 4c 49 54  ERAGE_TEST.SQLIT
2c588 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73  E_PRIVATE void s
2c589 71 6c 69 74 65 33 43 6f 76 65 72 61 67 65 28 69  qlite3Coverage(i
2c58a 6e 74 20 78 29 7b 0a 20 20 73 74 61 74 69 63 20  nt x){.  static 
2c58b 69 6e 74 20 64 75 6d 6d 79 20 3d 20 30 3b 0a 20  int dummy = 0;. 
2c58c 20 64 75 6d 6d 79 20 2b 3d 20 78 3b 0a 7d 0a 23   dummy += x;.}.#
2c58d 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 52 65  endif.../*.** Re
2c58e 74 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65  turn true if the
2c58f 20 62 75 66 66 65 72 20 7a 5b 30 2e 2e 6e 2d 31   buffer z[0..n-1
2c590 5d 20 63 6f 6e 74 61 69 6e 73 20 61 6c 6c 20 73  ] contains all s
2c591 70 61 63 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63  paces..*/.static
2c592 20 69 6e 74 20 61 6c 6c 53 70 61 63 65 73 28 63   int allSpaces(c
2c593 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c 20 69 6e  onst char *z, in
2c594 74 20 6e 29 7b 0a 20 20 77 68 69 6c 65 28 20 6e  t n){.  while( n
2c595 3e 30 20 26 26 20 7a 5b 6e 2d 31 5d 3d 3d 27 20  >0 && z[n-1]==' 
2c596 27 20 29 7b 20 6e 2d 2d 3b 20 7d 0a 20 20 72 65  ' ){ n--; }.  re
2c597 74 75 72 6e 20 6e 3d 3d 30 3b 0a 7d 0a 0a 2f 2a  turn n==0;.}../*
2c598 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20  .** This is the 
2c599 64 65 66 61 75 6c 74 20 63 6f 6c 6c 61 74 69 6e  default collatin
2c59a 67 20 66 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 64  g function named
2c59b 20 22 42 49 4e 41 52 59 22 20 77 68 69 63 68 20   "BINARY" which 
2c59c 69 73 20 61 6c 77 61 79 73 0a 2a 2a 20 61 76 61  is always.** ava
2c59d 69 6c 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66  ilable..**.** If
2c59e 20 74 68 65 20 70 61 64 46 6c 61 67 20 61 72 67   the padFlag arg
2c59f 75 6d 65 6e 74 20 69 73 20 6e 6f 74 20 4e 55 4c  ument is not NUL
2c5a0 4c 20 74 68 65 6e 20 73 70 61 63 65 20 70 61 64  L then space pad
2c5a1 64 69 6e 67 20 61 74 20 74 68 65 20 65 6e 64 0a  ding at the end.
2c5a2 2a 2a 20 6f 66 20 73 74 72 69 6e 67 73 20 69 73  ** of strings is
2c5a3 20 69 67 6e 6f 72 65 64 2e 20 20 54 68 69 73 20   ignored.  This 
2c5a4 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 52  implements the R
2c5a5 54 52 49 4d 20 63 6f 6c 6c 61 74 69 6f 6e 2e 0a  TRIM collation..
2c5a6 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 69  */.static int bi
2c5a7 6e 43 6f 6c 6c 46 75 6e 63 28 0a 20 20 76 6f 69  nCollFunc(.  voi
2c5a8 64 20 2a 70 61 64 46 6c 61 67 2c 0a 20 20 69 6e  d *padFlag,.  in
2c5a9 74 20 6e 4b 65 79 31 2c 20 63 6f 6e 73 74 20 76  t nKey1, const v
2c5aa 6f 69 64 20 2a 70 4b 65 79 31 2c 0a 20 20 69 6e  oid *pKey1,.  in
2c5ab 74 20 6e 4b 65 79 32 2c 20 63 6f 6e 73 74 20 76  t nKey2, const v
2c5ac 6f 69 64 20 2a 70 4b 65 79 32 0a 29 7b 0a 20 20  oid *pKey2.){.  
2c5ad 69 6e 74 20 72 63 2c 20 6e 3b 0a 20 20 6e 20 3d  int rc, n;.  n =
2c5ae 20 6e 4b 65 79 31 3c 6e 4b 65 79 32 20 3f 20 6e   nKey1<nKey2 ? n
2c5af 4b 65 79 31 20 3a 20 6e 4b 65 79 32 3b 0a 20 20  Key1 : nKey2;.  
2c5b0 72 63 20 3d 20 6d 65 6d 63 6d 70 28 70 4b 65 79  rc = memcmp(pKey
2c5b1 31 2c 20 70 4b 65 79 32 2c 20 6e 29 3b 0a 20 20  1, pKey2, n);.  
2c5b2 69 66 28 20 72 63 3d 3d 30 20 29 7b 0a 20 20 20  if( rc==0 ){.   
2c5b3 20 69 66 28 20 70 61 64 46 6c 61 67 0a 20 20 20   if( padFlag.   
2c5b4 20 20 26 26 20 61 6c 6c 53 70 61 63 65 73 28 28    && allSpaces((
2c5b5 28 63 68 61 72 2a 29 70 4b 65 79 31 29 2b 6e 2c  (char*)pKey1)+n,
2c5b6 20 6e 4b 65 79 31 2d 6e 29 0a 20 20 20 20 20 26   nKey1-n).     &
2c5b7 26 20 61 6c 6c 53 70 61 63 65 73 28 28 28 63 68  & allSpaces(((ch
2c5b8 61 72 2a 29 70 4b 65 79 32 29 2b 6e 2c 20 6e 4b  ar*)pKey2)+n, nK
2c5b9 65 79 32 2d 6e 29 0a 20 20 20 20 29 7b 0a 20 20  ey2-n).    ){.  
2c5ba 20 20 20 20 2f 2a 20 4c 65 61 76 65 20 72 63 20      /* Leave rc 
2c5bb 75 6e 63 68 61 6e 67 65 64 20 61 74 20 30 20 2a  unchanged at 0 *
2c5bc 2f 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  /.    }else{.   
2c5bd 20 20 20 72 63 20 3d 20 6e 4b 65 79 31 20 2d 20     rc = nKey1 - 
2c5be 6e 4b 65 79 32 3b 0a 20 20 20 20 7d 0a 20 20 7d  nKey2;.    }.  }
2c5bf 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
2c5c0 0a 2f 2a 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 62  ./*.** Another b
2c5c1 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c 61 74 69 6e  uilt-in collatin
2c5c2 67 20 73 65 71 75 65 6e 63 65 3a 20 4e 4f 43 41  g sequence: NOCA
2c5c3 53 45 2e 20 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20  SE. .**.** This 
2c5c4 63 6f 6c 6c 61 74 69 6e 67 20 73 65 71 75 65 6e  collating sequen
2c5c5 63 65 20 69 73 20 69 6e 74 65 6e 64 65 64 20 74  ce is intended t
2c5c6 6f 20 62 65 20 75 73 65 64 20 66 6f 72 20 22 63  o be used for "c
2c5c7 61 73 65 20 69 6e 64 65 70 65 6e 64 61 6e 74 0a  ase independant.
2c5c8 2a 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 22 2e 20  ** comparison". 
2c5c9 53 51 4c 69 74 65 27 73 20 6b 6e 6f 77 6c 65 64  SQLite's knowled
2c5ca 67 65 20 6f 66 20 75 70 70 65 72 20 61 6e 64 20  ge of upper and 
2c5cb 6c 6f 77 65 72 20 63 61 73 65 20 65 71 75 69 76  lower case equiv
2c5cc 61 6c 65 6e 74 73 0a 2a 2a 20 65 78 74 65 6e 64  alents.** extend
2c5cd 73 20 6f 6e 6c 79 20 74 6f 20 74 68 65 20 32 36  s only to the 26
2c5ce 20 63 68 61 72 61 63 74 65 72 73 20 75 73 65 64   characters used
2c5cf 20 69 6e 20 74 68 65 20 45 6e 67 6c 69 73 68 20   in the English 
2c5d0 6c 61 6e 67 75 61 67 65 2e 0a 2a 2a 0a 2a 2a 20  language..**.** 
2c5d1 41 74 20 74 68 65 20 6d 6f 6d 65 6e 74 20 74 68  At the moment th
2c5d2 65 72 65 20 69 73 20 6f 6e 6c 79 20 61 20 55 54  ere is only a UT
2c5d3 46 2d 38 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  F-8 implementati
2c5d4 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  on..*/.static in
2c5d5 74 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69 6e  t nocaseCollatin
2c5d6 67 46 75 6e 63 28 0a 20 20 76 6f 69 64 20 2a 4e  gFunc(.  void *N
2c5d7 6f 74 55 73 65 64 2c 0a 20 20 69 6e 74 20 6e 4b  otUsed,.  int nK
2c5d8 65 79 31 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey1, const void 
2c5d9 2a 70 4b 65 79 31 2c 0a 20 20 69 6e 74 20 6e 4b  *pKey1,.  int nK
2c5da 65 79 32 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20  ey2, const void 
2c5db 2a 70 4b 65 79 32 0a 29 7b 0a 20 20 69 6e 74 20  *pKey2.){.  int 
2c5dc 72 20 3d 20 73 71 6c 69 74 65 33 53 74 72 4e 49  r = sqlite3StrNI
2c5dd 43 6d 70 28 0a 20 20 20 20 20 20 28 63 6f 6e 73  Cmp(.      (cons
2c5de 74 20 63 68 61 72 20 2a 29 70 4b 65 79 31 2c 20  t char *)pKey1, 
2c5df 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70 4b  (const char *)pK
2c5e0 65 79 32 2c 20 28 6e 4b 65 79 31 3c 6e 4b 65 79  ey2, (nKey1<nKey
2c5e1 32 29 3f 6e 4b 65 79 31 3a 6e 4b 65 79 32 29 3b  2)?nKey1:nKey2);
2c5e2 0a 20 20 55 4e 55 53 45 44 5f 50 41 52 41 4d 45  .  UNUSED_PARAME
2c5e3 54 45 52 28 4e 6f 74 55 73 65 64 29 3b 0a 20 20  TER(NotUsed);.  
2c5e4 69 66 28 20 30 3d 3d 72 20 29 7b 0a 20 20 20 20  if( 0==r ){.    
2c5e5 72 20 3d 20 6e 4b 65 79 31 2d 6e 4b 65 79 32 3b  r = nKey1-nKey2;
2c5e6 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 3b  .  }.  return r;
2c5e7 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
2c5e8 20 74 68 65 20 52 4f 57 49 44 20 6f 66 20 74 68   the ROWID of th
2c5e9 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 20 69 6e  e most recent in
2c5ea 73 65 72 74 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41  sert.*/.SQLITE_A
2c5eb 50 49 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  PI sqlite_int64 
2c5ec 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73  sqlite3_last_ins
2c5ed 65 72 74 5f 72 6f 77 69 64 28 73 71 6c 69 74 65  ert_rowid(sqlite
2c5ee 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72 6e  3 *db){.  return
2c5ef 20 64 62 2d 3e 6c 61 73 74 52 6f 77 69 64 3b 0a   db->lastRowid;.
2c5f0 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  }../*.** Return 
2c5f1 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 63 68  the number of ch
2c5f2 61 6e 67 65 73 20 69 6e 20 74 68 65 20 6d 6f 73  anges in the mos
2c5f3 74 20 72 65 63 65 6e 74 20 63 61 6c 6c 20 74 6f  t recent call to
2c5f4 20 73 71 6c 69 74 65 33 5f 65 78 65 63 28 29 2e   sqlite3_exec().
2c5f5 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2c5f6 6e 74 20 73 71 6c 69 74 65 33 5f 63 68 61 6e 67  nt sqlite3_chang
2c5f7 65 73 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  es(sqlite3 *db){
2c5f8 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e 6e 43  .  return db->nC
2c5f9 68 61 6e 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  hange;.}../*.** 
2c5fa 52 65 74 75 72 6e 20 74 68 65 20 6e 75 6d 62 65  Return the numbe
2c5fb 72 20 6f 66 20 63 68 61 6e 67 65 73 20 73 69 6e  r of changes sin
2c5fc 63 65 20 74 68 65 20 64 61 74 61 62 61 73 65 20  ce the database 
2c5fd 68 61 6e 64 6c 65 20 77 61 73 20 6f 70 65 6e 65  handle was opene
2c5fe 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  d..*/.SQLITE_API
2c5ff 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 6f 74   int sqlite3_tot
2c600 61 6c 5f 63 68 61 6e 67 65 73 28 73 71 6c 69 74  al_changes(sqlit
2c601 65 33 20 2a 64 62 29 7b 0a 20 20 72 65 74 75 72  e3 *db){.  retur
2c602 6e 20 64 62 2d 3e 6e 54 6f 74 61 6c 43 68 61 6e  n db->nTotalChan
2c603 67 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 6f  ge;.}../*.** Clo
2c604 73 65 20 61 6e 20 65 78 69 73 74 69 6e 67 20 53  se an existing S
2c605 51 4c 69 74 65 20 64 61 74 61 62 61 73 65 0a 2a  QLite database.*
2c606 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  /.SQLITE_API int
2c607 20 73 71 6c 69 74 65 33 5f 63 6c 6f 73 65 28 73   sqlite3_close(s
2c608 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 48  qlite3 *db){.  H
2c609 61 73 68 45 6c 65 6d 20 2a 69 3b 0a 20 20 69 6e  ashElem *i;.  in
2c60a 74 20 6a 3b 0a 0a 20 20 69 66 28 20 21 64 62 20  t j;..  if( !db 
2c60b 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2c60c 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 20 20 69  LITE_OK;.  }.  i
2c60d 66 28 20 21 73 71 6c 69 74 65 33 53 61 66 65 74  f( !sqlite3Safet
2c60e 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64  yCheckSickOrOk(d
2c60f 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  b) ){.    return
2c610 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a   SQLITE_MISUSE;.
2c611 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75    }.  sqlite3_mu
2c612 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2c613 74 65 78 29 3b 0a 0a 23 69 66 64 65 66 20 53 51  tex);..#ifdef SQ
2c614 4c 49 54 45 5f 53 53 45 0a 20 20 7b 0a 20 20 20  LITE_SSE.  {.   
2c615 20 65 78 74 65 72 6e 20 76 6f 69 64 20 73 71 6c   extern void sql
2c616 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28 73  ite3SseCleanup(s
2c617 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20 73 71  qlite3*);.    sq
2c618 6c 69 74 65 33 53 73 65 43 6c 65 61 6e 75 70 28  lite3SseCleanup(
2c619 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64 69 66 20  db);.  }.#endif 
2c61a 0a 0a 20 20 73 71 6c 69 74 65 33 52 65 73 65 74  ..  sqlite3Reset
2c61b 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 64  InternalSchema(d
2c61c 62 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20 49 66 20  b, 0);..  /* If 
2c61d 61 20 74 72 61 6e 73 61 63 74 69 6f 6e 20 69 73  a transaction is
2c61e 20 6f 70 65 6e 2c 20 74 68 65 20 52 65 73 65 74   open, the Reset
2c61f 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61 28 29  InternalSchema()
2c620 20 63 61 6c 6c 20 61 62 6f 76 65 0a 20 20 2a 2a   call above.  **
2c621 20 77 69 6c 6c 20 6e 6f 74 20 68 61 76 65 20 63   will not have c
2c622 61 6c 6c 65 64 20 74 68 65 20 78 44 69 73 63 6f  alled the xDisco
2c623 6e 6e 65 63 74 28 29 20 6d 65 74 68 6f 64 20 6f  nnect() method o
2c624 6e 20 61 6e 79 20 76 69 72 74 75 61 6c 0a 20 20  n any virtual.  
2c625 2a 2a 20 74 61 62 6c 65 73 20 69 6e 20 74 68 65  ** tables in the
2c626 20 64 62 2d 3e 61 56 54 72 61 6e 73 5b 5d 20 61   db->aVTrans[] a
2c627 72 72 61 79 2e 20 54 68 65 20 66 6f 6c 6c 6f 77  rray. The follow
2c628 69 6e 67 20 73 71 6c 69 74 65 33 56 74 61 62 52  ing sqlite3VtabR
2c629 6f 6c 6c 62 61 63 6b 28 29 0a 20 20 2a 2a 20 63  ollback().  ** c
2c62a 61 6c 6c 20 77 69 6c 6c 20 64 6f 20 73 6f 2e 20  all will do so. 
2c62b 57 65 20 6e 65 65 64 20 74 6f 20 64 6f 20 74 68  We need to do th
2c62c 69 73 20 62 65 66 6f 72 65 20 74 68 65 20 63 68  is before the ch
2c62d 65 63 6b 20 66 6f 72 20 61 63 74 69 76 65 0a 20  eck for active. 
2c62e 20 2a 2a 20 53 51 4c 20 73 74 61 74 65 6d 65 6e   ** SQL statemen
2c62f 74 73 20 62 65 6c 6f 77 2c 20 61 73 20 74 68 65  ts below, as the
2c630 20 76 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65   v-table impleme
2c631 6e 74 61 74 69 6f 6e 20 6d 61 79 20 62 65 20 73  ntation may be s
2c632 74 6f 72 69 6e 67 0a 20 20 2a 2a 20 73 6f 6d 65  toring.  ** some
2c633 20 70 72 65 70 61 72 65 64 20 73 74 61 74 65 6d   prepared statem
2c634 65 6e 74 73 20 69 6e 74 65 72 6e 61 6c 6c 79 2e  ents internally.
2c635 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 56  .  */.  sqlite3V
2c636 74 61 62 52 6f 6c 6c 62 61 63 6b 28 64 62 29 3b  tabRollback(db);
2c637 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 72 65 20  ..  /* If there 
2c638 61 72 65 20 61 6e 79 20 6f 75 74 73 74 61 6e 64  are any outstand
2c639 69 6e 67 20 56 4d 73 2c 20 72 65 74 75 72 6e 20  ing VMs, return 
2c63a 53 51 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a  SQLITE_BUSY. */.
2c63b 20 20 69 66 28 20 64 62 2d 3e 70 56 64 62 65 20    if( db->pVdbe 
2c63c 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 45 72  ){.    sqlite3Er
2c63d 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
2c63e 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 55  USY, .        "U
2c63f 6e 61 62 6c 65 20 74 6f 20 63 6c 6f 73 65 20 64  nable to close d
2c640 75 65 20 74 6f 20 75 6e 66 69 6e 61 6c 69 73 65  ue to unfinalise
2c641 64 20 73 74 61 74 65 6d 65 6e 74 73 22 29 3b 0a  d statements");.
2c642 20 20 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65      sqlite3_mute
2c643 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2c644 78 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  x);.    return S
2c645 51 4c 49 54 45 5f 42 55 53 59 3b 0a 20 20 7d 0a  QLITE_BUSY;.  }.
2c646 20 20 61 73 73 65 72 74 28 20 73 71 6c 69 74 65    assert( sqlite
2c647 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63 6b  3SafetyCheckSick
2c648 4f 72 4f 6b 28 64 62 29 20 29 3b 0a 0a 20 20 66  OrOk(db) );..  f
2c649 6f 72 28 6a 3d 30 3b 20 6a 3c 64 62 2d 3e 6e 44  or(j=0; j<db->nD
2c64a 62 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 73 74 72  b; j++){.    str
2c64b 75 63 74 20 44 62 20 2a 70 44 62 20 3d 20 26 64  uct Db *pDb = &d
2c64c 62 2d 3e 61 44 62 5b 6a 5d 3b 0a 20 20 20 20 69  b->aDb[j];.    i
2c64d 66 28 20 70 44 62 2d 3e 70 42 74 20 29 7b 0a 20  f( pDb->pBt ){. 
2c64e 20 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65       sqlite3Btre
2c64f 65 43 6c 6f 73 65 28 70 44 62 2d 3e 70 42 74 29  eClose(pDb->pBt)
2c650 3b 0a 20 20 20 20 20 20 70 44 62 2d 3e 70 42 74  ;.      pDb->pBt
2c651 20 3d 20 30 3b 0a 20 20 20 20 20 20 69 66 28 20   = 0;.      if( 
2c652 6a 21 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20  j!=1 ){.        
2c653 70 44 62 2d 3e 70 53 63 68 65 6d 61 20 3d 20 30  pDb->pSchema = 0
2c654 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a  ;.      }.    }.
2c655 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 52 65 73    }.  sqlite3Res
2c656 65 74 49 6e 74 65 72 6e 61 6c 53 63 68 65 6d 61  etInternalSchema
2c657 28 64 62 2c 20 30 29 3b 0a 20 20 61 73 73 65 72  (db, 0);.  asser
2c658 74 28 20 64 62 2d 3e 6e 44 62 3c 3d 32 20 29 3b  t( db->nDb<=2 );
2c659 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 61  .  assert( db->a
2c65a 44 62 3d 3d 64 62 2d 3e 61 44 62 53 74 61 74 69  Db==db->aDbStati
2c65b 63 20 29 3b 0a 20 20 66 6f 72 28 6a 3d 30 3b 20  c );.  for(j=0; 
2c65c 6a 3c 41 72 72 61 79 53 69 7a 65 28 64 62 2d 3e  j<ArraySize(db->
2c65d 61 46 75 6e 63 2e 61 29 3b 20 6a 2b 2b 29 7b 0a  aFunc.a); j++){.
2c65e 20 20 20 20 46 75 6e 63 44 65 66 20 2a 70 4e 65      FuncDef *pNe
2c65f 78 74 2c 20 2a 70 48 61 73 68 2c 20 2a 70 3b 0a  xt, *pHash, *p;.
2c660 20 20 20 20 66 6f 72 28 70 3d 64 62 2d 3e 61 46      for(p=db->aF
2c661 75 6e 63 2e 61 5b 6a 5d 3b 20 70 3b 20 70 3d 70  unc.a[j]; p; p=p
2c662 48 61 73 68 29 7b 0a 20 20 20 20 20 20 70 48 61  Hash){.      pHa
2c663 73 68 20 3d 20 70 2d 3e 70 48 61 73 68 3b 0a 20  sh = p->pHash;. 
2c664 20 20 20 20 20 77 68 69 6c 65 28 20 70 20 29 7b       while( p ){
2c665 0a 20 20 20 20 20 20 20 20 70 4e 65 78 74 20 3d  .        pNext =
2c666 20 70 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20   p->pNext;.     
2c667 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65     sqlite3DbFree
2c668 28 64 62 2c 20 70 29 3b 0a 20 20 20 20 20 20 20  (db, p);.       
2c669 20 70 20 3d 20 70 4e 65 78 74 3b 0a 20 20 20 20   p = pNext;.    
2c66a 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
2c66b 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2c66c 46 69 72 73 74 28 26 64 62 2d 3e 61 43 6f 6c 6c  First(&db->aColl
2c66d 53 65 71 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74  Seq); i; i=sqlit
2c66e 65 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20  eHashNext(i)){. 
2c66f 20 20 20 43 6f 6c 6c 53 65 71 20 2a 70 43 6f 6c     CollSeq *pCol
2c670 6c 20 3d 20 28 43 6f 6c 6c 53 65 71 20 2a 29 73  l = (CollSeq *)s
2c671 71 6c 69 74 65 48 61 73 68 44 61 74 61 28 69 29  qliteHashData(i)
2c672 3b 0a 20 20 20 20 2f 2a 20 49 6e 76 6f 6b 65 20  ;.    /* Invoke 
2c673 61 6e 79 20 64 65 73 74 72 75 63 74 6f 72 73 20  any destructors 
2c674 72 65 67 69 73 74 65 72 65 64 20 66 6f 72 20 63  registered for c
2c675 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2c676 65 20 75 73 65 72 20 64 61 74 61 2e 20 2a 2f 0a  e user data. */.
2c677 20 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33      for(j=0; j<3
2c678 3b 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  ; j++){.      if
2c679 28 20 70 43 6f 6c 6c 5b 6a 5d 2e 78 44 65 6c 20  ( pColl[j].xDel 
2c67a 29 7b 0a 20 20 20 20 20 20 20 20 70 43 6f 6c 6c  ){.        pColl
2c67b 5b 6a 5d 2e 78 44 65 6c 28 70 43 6f 6c 6c 5b 6a  [j].xDel(pColl[j
2c67c 5d 2e 70 55 73 65 72 29 3b 0a 20 20 20 20 20 20  ].pUser);.      
2c67d 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69  }.    }.    sqli
2c67e 74 65 33 44 62 46 72 65 65 28 64 62 2c 20 70 43  te3DbFree(db, pC
2c67f 6f 6c 6c 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69  oll);.  }.  sqli
2c680 74 65 33 48 61 73 68 43 6c 65 61 72 28 26 64 62  te3HashClear(&db
2c681 2d 3e 61 43 6f 6c 6c 53 65 71 29 3b 0a 23 69 66  ->aCollSeq);.#if
2c682 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2c683 5f 56 49 52 54 55 41 4c 54 41 42 4c 45 0a 20 20  _VIRTUALTABLE.  
2c684 66 6f 72 28 69 3d 73 71 6c 69 74 65 48 61 73 68  for(i=sqliteHash
2c685 46 69 72 73 74 28 26 64 62 2d 3e 61 4d 6f 64 75  First(&db->aModu
2c686 6c 65 29 3b 20 69 3b 20 69 3d 73 71 6c 69 74 65  le); i; i=sqlite
2c687 48 61 73 68 4e 65 78 74 28 69 29 29 7b 0a 20 20  HashNext(i)){.  
2c688 20 20 4d 6f 64 75 6c 65 20 2a 70 4d 6f 64 20 3d    Module *pMod =
2c689 20 28 4d 6f 64 75 6c 65 20 2a 29 73 71 6c 69 74   (Module *)sqlit
2c68a 65 48 61 73 68 44 61 74 61 28 69 29 3b 0a 20 20  eHashData(i);.  
2c68b 20 20 69 66 28 20 70 4d 6f 64 2d 3e 78 44 65 73    if( pMod->xDes
2c68c 74 72 6f 79 20 29 7b 0a 20 20 20 20 20 20 70 4d  troy ){.      pM
2c68d 6f 64 2d 3e 78 44 65 73 74 72 6f 79 28 70 4d 6f  od->xDestroy(pMo
2c68e 64 2d 3e 70 41 75 78 29 3b 0a 20 20 20 20 7d 0a  d->pAux);.    }.
2c68f 20 20 20 20 73 71 6c 69 74 65 33 44 62 46 72 65      sqlite3DbFre
2c690 65 28 64 62 2c 20 70 4d 6f 64 29 3b 0a 20 20 7d  e(db, pMod);.  }
2c691 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68 43 6c  .  sqlite3HashCl
2c692 65 61 72 28 26 64 62 2d 3e 61 4d 6f 64 75 6c 65  ear(&db->aModule
2c693 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 73 71 6c  );.#endif..  sql
2c694 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51  ite3Error(db, SQ
2c695 4c 49 54 45 5f 4f 4b 2c 20 30 29 3b 20 2f 2a 20  LITE_OK, 0); /* 
2c696 44 65 61 6c 6c 6f 63 61 74 65 73 20 61 6e 79 20  Deallocates any 
2c697 63 61 63 68 65 64 20 65 72 72 6f 72 20 73 74 72  cached error str
2c698 69 6e 67 73 2e 20 2a 2f 0a 20 20 69 66 28 20 64  ings. */.  if( d
2c699 62 2d 3e 70 45 72 72 20 29 7b 0a 20 20 20 20 73  b->pErr ){.    s
2c69a 71 6c 69 74 65 33 56 61 6c 75 65 46 72 65 65 28  qlite3ValueFree(
2c69b 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d 0a 20  db->pErr);.  }. 
2c69c 20 73 71 6c 69 74 65 33 43 6c 6f 73 65 45 78 74   sqlite3CloseExt
2c69d 65 6e 73 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20  ensions(db);..  
2c69e 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49  db->magic = SQLI
2c69f 54 45 5f 4d 41 47 49 43 5f 45 52 52 4f 52 3b 0a  TE_MAGIC_ERROR;.
2c6a0 0a 20 20 2f 2a 20 54 68 65 20 74 65 6d 70 2d 64  .  /* The temp-d
2c6a1 61 74 61 62 61 73 65 20 73 63 68 65 6d 61 20 69  atabase schema i
2c6a2 73 20 61 6c 6c 6f 63 61 74 65 64 20 64 69 66 66  s allocated diff
2c6a3 65 72 65 6e 74 6c 79 20 66 72 6f 6d 20 74 68 65  erently from the
2c6a4 20 6f 74 68 65 72 20 73 63 68 65 6d 61 0a 20 20   other schema.  
2c6a5 2a 2a 20 6f 62 6a 65 63 74 73 20 28 75 73 69 6e  ** objects (usin
2c6a6 67 20 73 71 6c 69 74 65 4d 61 6c 6c 6f 63 28 29  g sqliteMalloc()
2c6a7 20 64 69 72 65 63 74 6c 79 2c 20 69 6e 73 74 65   directly, inste
2c6a8 61 64 20 6f 66 20 73 71 6c 69 74 65 33 42 74 72  ad of sqlite3Btr
2c6a9 65 65 53 63 68 65 6d 61 28 29 29 2e 0a 20 20 2a  eeSchema())..  *
2c6aa 2a 20 53 6f 20 69 74 20 6e 65 65 64 73 20 74 6f  * So it needs to
2c6ab 20 62 65 20 66 72 65 65 64 20 68 65 72 65 2e 20   be freed here. 
2c6ac 54 6f 64 6f 3a 20 57 68 79 20 6e 6f 74 20 72 6f  Todo: Why not ro
2c6ad 6c 6c 20 74 68 65 20 74 65 6d 70 20 73 63 68 65  ll the temp sche
2c6ae 6d 61 20 69 6e 74 6f 0a 20 20 2a 2a 20 74 68 65  ma into.  ** the
2c6af 20 73 61 6d 65 20 73 71 6c 69 74 65 4d 61 6c 6c   same sqliteMall
2c6b0 6f 63 28 29 20 61 73 20 74 68 65 20 6f 6e 65 20  oc() as the one 
2c6b1 74 68 61 74 20 61 6c 6c 6f 63 61 74 65 73 20 74  that allocates t
2c6b2 68 65 20 64 61 74 61 62 61 73 65 20 0a 20 20 2a  he database .  *
2c6b3 2a 20 73 74 72 75 63 74 75 72 65 3f 0a 20 20 2a  * structure?.  *
2c6b4 2f 0a 20 20 73 71 6c 69 74 65 33 44 62 46 72 65  /.  sqlite3DbFre
2c6b5 65 28 64 62 2c 20 64 62 2d 3e 61 44 62 5b 31 5d  e(db, db->aDb[1]
2c6b6 2e 70 53 63 68 65 6d 61 29 3b 0a 20 20 73 71 6c  .pSchema);.  sql
2c6b7 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65  ite3_mutex_leave
2c6b8 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64  (db->mutex);.  d
2c6b9 62 2d 3e 6d 61 67 69 63 20 3d 20 53 51 4c 49 54  b->magic = SQLIT
2c6ba 45 5f 4d 41 47 49 43 5f 43 4c 4f 53 45 44 3b 0a  E_MAGIC_CLOSED;.
2c6bb 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f    sqlite3_mutex_
2c6bc 66 72 65 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  free(db->mutex);
2c6bd 0a 20 20 61 73 73 65 72 74 28 20 64 62 2d 3e 6c  .  assert( db->l
2c6be 6f 6f 6b 61 73 69 64 65 2e 6e 4f 75 74 3d 3d 30  ookaside.nOut==0
2c6bf 20 29 3b 20 20 2f 2a 20 46 61 69 6c 73 20 6f 6e   );  /* Fails on
2c6c0 20 61 20 6c 6f 6f 6b 61 73 69 64 65 20 6d 65 6d   a lookaside mem
2c6c1 6f 72 79 20 6c 65 61 6b 20 2a 2f 0a 20 20 69 66  ory leak */.  if
2c6c2 28 20 64 62 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e  ( db->lookaside.
2c6c3 62 4d 61 6c 6c 6f 63 65 64 20 29 7b 0a 20 20 20  bMalloced ){.   
2c6c4 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62   sqlite3_free(db
2c6c5 2d 3e 6c 6f 6f 6b 61 73 69 64 65 2e 70 53 74 61  ->lookaside.pSta
2c6c6 72 74 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  rt);.  }.  sqlit
2c6c7 65 33 5f 66 72 65 65 28 64 62 29 3b 0a 20 20 72  e3_free(db);.  r
2c6c8 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
2c6c9 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 6f 6c 6c 62 61  .}../*.** Rollba
2c6ca 63 6b 20 61 6c 6c 20 64 61 74 61 62 61 73 65 20  ck all database 
2c6cb 66 69 6c 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54 45  files..*/.SQLITE
2c6cc 5f 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71  _PRIVATE void sq
2c6cd 6c 69 74 65 33 52 6f 6c 6c 62 61 63 6b 41 6c 6c  lite3RollbackAll
2c6ce 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
2c6cf 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 6e   int i;.  int in
2c6d0 54 72 61 6e 73 20 3d 20 30 3b 0a 20 20 61 73 73  Trans = 0;.  ass
2c6d1 65 72 74 28 20 73 71 6c 69 74 65 33 5f 6d 75 74  ert( sqlite3_mut
2c6d2 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d 75 74 65  ex_held(db->mute
2c6d3 78 29 20 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  x) );.  sqlite3B
2c6d4 65 67 69 6e 42 65 6e 69 67 6e 4d 61 6c 6c 6f 63  eginBenignMalloc
2c6d5 28 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ();.  for(i=0; i
2c6d6 3c 64 62 2d 3e 6e 44 62 3b 20 69 2b 2b 29 7b 0a  <db->nDb; i++){.
2c6d7 20 20 20 20 69 66 28 20 64 62 2d 3e 61 44 62 5b      if( db->aDb[
2c6d8 69 5d 2e 70 42 74 20 29 7b 0a 20 20 20 20 20 20  i].pBt ){.      
2c6d9 69 66 28 20 73 71 6c 69 74 65 33 42 74 72 65 65  if( sqlite3Btree
2c6da 49 73 49 6e 54 72 61 6e 73 28 64 62 2d 3e 61 44  IsInTrans(db->aD
2c6db 62 5b 69 5d 2e 70 42 74 29 20 29 7b 0a 20 20 20  b[i].pBt) ){.   
2c6dc 20 20 20 20 20 69 6e 54 72 61 6e 73 20 3d 20 31       inTrans = 1
2c6dd 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20  ;.      }.      
2c6de 73 71 6c 69 74 65 33 42 74 72 65 65 52 6f 6c 6c  sqlite3BtreeRoll
2c6df 62 61 63 6b 28 64 62 2d 3e 61 44 62 5b 69 5d 2e  back(db->aDb[i].
2c6e0 70 42 74 29 3b 0a 20 20 20 20 20 20 64 62 2d 3e  pBt);.      db->
2c6e1 61 44 62 5b 69 5d 2e 69 6e 54 72 61 6e 73 20 3d  aDb[i].inTrans =
2c6e2 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
2c6e3 73 71 6c 69 74 65 33 56 74 61 62 52 6f 6c 6c 62  sqlite3VtabRollb
2c6e4 61 63 6b 28 64 62 29 3b 0a 20 20 73 71 6c 69 74  ack(db);.  sqlit
2c6e5 65 33 45 6e 64 42 65 6e 69 67 6e 4d 61 6c 6c 6f  e3EndBenignMallo
2c6e6 63 28 29 3b 0a 0a 20 20 69 66 28 20 64 62 2d 3e  c();..  if( db->
2c6e7 66 6c 61 67 73 26 53 51 4c 49 54 45 5f 49 6e 74  flags&SQLITE_Int
2c6e8 65 72 6e 43 68 61 6e 67 65 73 20 29 7b 0a 20 20  ernChanges ){.  
2c6e9 20 20 73 71 6c 69 74 65 33 45 78 70 69 72 65 50    sqlite3ExpireP
2c6ea 72 65 70 61 72 65 64 53 74 61 74 65 6d 65 6e 74  reparedStatement
2c6eb 73 28 64 62 29 3b 0a 20 20 20 20 73 71 6c 69 74  s(db);.    sqlit
2c6ec 65 33 52 65 73 65 74 49 6e 74 65 72 6e 61 6c 53  e3ResetInternalS
2c6ed 63 68 65 6d 61 28 64 62 2c 20 30 29 3b 0a 20 20  chema(db, 0);.  
2c6ee 7d 0a 0a 20 20 2f 2a 20 49 66 20 6f 6e 65 20 68  }..  /* If one h
2c6ef 61 73 20 62 65 65 6e 20 63 6f 6e 66 69 67 75 72  as been configur
2c6f0 65 64 2c 20 69 6e 76 6f 6b 65 20 74 68 65 20 72  ed, invoke the r
2c6f1 6f 6c 6c 62 61 63 6b 2d 68 6f 6f 6b 20 63 61 6c  ollback-hook cal
2c6f2 6c 62 61 63 6b 20 2a 2f 0a 20 20 69 66 28 20 64  lback */.  if( d
2c6f3 62 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c  b->xRollbackCall
2c6f4 62 61 63 6b 20 26 26 20 28 69 6e 54 72 61 6e 73  back && (inTrans
2c6f5 20 7c 7c 20 21 64 62 2d 3e 61 75 74 6f 43 6f 6d   || !db->autoCom
2c6f6 6d 69 74 29 20 29 7b 0a 20 20 20 20 64 62 2d 3e  mit) ){.    db->
2c6f7 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62 61 63  xRollbackCallbac
2c6f8 6b 28 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b 41  k(db->pRollbackA
2c6f9 72 67 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  rg);.  }.}../*.*
2c6fa 2a 20 52 65 74 75 72 6e 20 61 20 73 74 61 74 69  * Return a stati
2c6fb 63 20 73 74 72 69 6e 67 20 74 68 61 74 20 64 65  c string that de
2c6fc 73 63 72 69 62 65 73 20 74 68 65 20 6b 69 6e 64  scribes the kind
2c6fd 20 6f 66 20 65 72 72 6f 72 20 73 70 65 63 69 66   of error specif
2c6fe 69 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 61 72  ied in the.** ar
2c6ff 67 75 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54  gument..*/.SQLIT
2c700 45 5f 50 52 49 56 41 54 45 20 63 6f 6e 73 74 20  E_PRIVATE const 
2c701 63 68 61 72 20 2a 73 71 6c 69 74 65 33 45 72 72  char *sqlite3Err
2c702 53 74 72 28 69 6e 74 20 72 63 29 7b 0a 20 20 63  Str(int rc){.  c
2c703 6f 6e 73 74 20 63 68 61 72 20 2a 7a 3b 0a 20 20  onst char *z;.  
2c704 73 77 69 74 63 68 28 20 72 63 20 26 20 30 78 66  switch( rc & 0xf
2c705 66 20 29 7b 0a 20 20 20 20 63 61 73 65 20 53 51  f ){.    case SQ
2c706 4c 49 54 45 5f 52 4f 57 3a 0a 20 20 20 20 63 61  LITE_ROW:.    ca
2c707 73 65 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a  se SQLITE_DONE:.
2c708 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c709 4f 4b 3a 20 20 20 20 20 20 20 20 20 7a 20 3d 20  OK:         z = 
2c70a 22 6e 6f 74 20 61 6e 20 65 72 72 6f 72 22 3b 20  "not an error"; 
2c70b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c70c 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c70d 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c70e 45 52 52 4f 52 3a 20 20 20 20 20 20 7a 20 3d 20  ERROR:      z = 
2c70f 22 53 51 4c 20 6c 6f 67 69 63 20 65 72 72 6f 72  "SQL logic error
2c710 20 6f 72 20 6d 69 73 73 69 6e 67 20 64 61 74 61   or missing data
2c711 62 61 73 65 22 3b 20 20 20 62 72 65 61 6b 3b 0a  base";   break;.
2c712 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c713 50 45 52 4d 3a 20 20 20 20 20 20 20 7a 20 3d 20  PERM:       z = 
2c714 22 61 63 63 65 73 73 20 70 65 72 6d 69 73 73 69  "access permissi
2c715 6f 6e 20 64 65 6e 69 65 64 22 3b 20 20 20 20 20  on denied";     
2c716 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c717 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c718 41 42 4f 52 54 3a 20 20 20 20 20 20 7a 20 3d 20  ABORT:      z = 
2c719 22 63 61 6c 6c 62 61 63 6b 20 72 65 71 75 65 73  "callback reques
2c71a 74 65 64 20 71 75 65 72 79 20 61 62 6f 72 74 22  ted query abort"
2c71b 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
2c71c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c71d 42 55 53 59 3a 20 20 20 20 20 20 20 7a 20 3d 20  BUSY:       z = 
2c71e 22 64 61 74 61 62 61 73 65 20 69 73 20 6c 6f 63  "database is loc
2c71f 6b 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20  ked";           
2c720 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c721 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c722 4c 4f 43 4b 45 44 3a 20 20 20 20 20 7a 20 3d 20  LOCKED:     z = 
2c723 22 64 61 74 61 62 61 73 65 20 74 61 62 6c 65 20  "database table 
2c724 69 73 20 6c 6f 63 6b 65 64 22 3b 20 20 20 20 20  is locked";     
2c725 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c726 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c727 4e 4f 4d 45 4d 3a 20 20 20 20 20 20 7a 20 3d 20  NOMEM:      z = 
2c728 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 3b  "out of memory";
2c729 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c72a 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c72b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c72c 52 45 41 44 4f 4e 4c 59 3a 20 20 20 7a 20 3d 20  READONLY:   z = 
2c72d 22 61 74 74 65 6d 70 74 20 74 6f 20 77 72 69 74  "attempt to writ
2c72e 65 20 61 20 72 65 61 64 6f 6e 6c 79 20 64 61 74  e a readonly dat
2c72f 61 62 61 73 65 22 3b 20 20 62 72 65 61 6b 3b 0a  abase";  break;.
2c730 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c731 49 4e 54 45 52 52 55 50 54 3a 20 20 7a 20 3d 20  INTERRUPT:  z = 
2c732 22 69 6e 74 65 72 72 75 70 74 65 64 22 3b 20 20  "interrupted";  
2c733 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c734 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c735 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c736 49 4f 45 52 52 3a 20 20 20 20 20 20 7a 20 3d 20  IOERR:      z = 
2c737 22 64 69 73 6b 20 49 2f 4f 20 65 72 72 6f 72 22  "disk I/O error"
2c738 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2c739 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c73a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c73b 43 4f 52 52 55 50 54 3a 20 20 20 20 7a 20 3d 20  CORRUPT:    z = 
2c73c 22 64 61 74 61 62 61 73 65 20 64 69 73 6b 20 69  "database disk i
2c73d 6d 61 67 65 20 69 73 20 6d 61 6c 66 6f 72 6d 65  mage is malforme
2c73e 64 22 3b 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  d";      break;.
2c73f 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c740 46 55 4c 4c 3a 20 20 20 20 20 20 20 7a 20 3d 20  FULL:       z = 
2c741 22 64 61 74 61 62 61 73 65 20 6f 72 20 64 69 73  "database or dis
2c742 6b 20 69 73 20 66 75 6c 6c 22 3b 20 20 20 20 20  k is full";     
2c743 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c744 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c745 43 41 4e 54 4f 50 45 4e 3a 20 20 20 7a 20 3d 20  CANTOPEN:   z = 
2c746 22 75 6e 61 62 6c 65 20 74 6f 20 6f 70 65 6e 20  "unable to open 
2c747 64 61 74 61 62 61 73 65 20 66 69 6c 65 22 3b 20  database file"; 
2c748 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c749 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c74a 45 4d 50 54 59 3a 20 20 20 20 20 20 7a 20 3d 20  EMPTY:      z = 
2c74b 22 74 61 62 6c 65 20 63 6f 6e 74 61 69 6e 73 20  "table contains 
2c74c 6e 6f 20 64 61 74 61 22 3b 20 20 20 20 20 20 20  no data";       
2c74d 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c74e 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c74f 53 43 48 45 4d 41 3a 20 20 20 20 20 7a 20 3d 20  SCHEMA:     z = 
2c750 22 64 61 74 61 62 61 73 65 20 73 63 68 65 6d 61  "database schema
2c751 20 68 61 73 20 63 68 61 6e 67 65 64 22 3b 20 20   has changed";  
2c752 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c753 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c754 54 4f 4f 42 49 47 3a 20 20 20 20 20 7a 20 3d 20  TOOBIG:     z = 
2c755 22 53 74 72 69 6e 67 20 6f 72 20 42 4c 4f 42 20  "String or BLOB 
2c756 65 78 63 65 65 64 65 64 20 73 69 7a 65 20 6c 69  exceeded size li
2c757 6d 69 74 22 3b 20 20 20 20 62 72 65 61 6b 3b 0a  mit";    break;.
2c758 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c759 43 4f 4e 53 54 52 41 49 4e 54 3a 20 7a 20 3d 20  CONSTRAINT: z = 
2c75a 22 63 6f 6e 73 74 72 61 69 6e 74 20 66 61 69 6c  "constraint fail
2c75b 65 64 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ed";            
2c75c 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c75d 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c75e 4d 49 53 4d 41 54 43 48 3a 20 20 20 7a 20 3d 20  MISMATCH:   z = 
2c75f 22 64 61 74 61 74 79 70 65 20 6d 69 73 6d 61 74  "datatype mismat
2c760 63 68 22 3b 20 20 20 20 20 20 20 20 20 20 20 20  ch";            
2c761 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c762 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c763 4d 49 53 55 53 45 3a 20 20 20 20 20 7a 20 3d 20  MISUSE:     z = 
2c764 22 6c 69 62 72 61 72 79 20 72 6f 75 74 69 6e 65  "library routine
2c765 20 63 61 6c 6c 65 64 20 6f 75 74 20 6f 66 20 73   called out of s
2c766 65 71 75 65 6e 63 65 22 3b 62 72 65 61 6b 3b 0a  equence";break;.
2c767 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c768 4e 4f 4c 46 53 3a 20 20 20 20 20 20 7a 20 3d 20  NOLFS:      z = 
2c769 22 6c 61 72 67 65 20 66 69 6c 65 20 73 75 70 70  "large file supp
2c76a 6f 72 74 20 69 73 20 64 69 73 61 62 6c 65 64 22  ort is disabled"
2c76b 3b 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ;        break;.
2c76c 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c76d 41 55 54 48 3a 20 20 20 20 20 20 20 7a 20 3d 20  AUTH:       z = 
2c76e 22 61 75 74 68 6f 72 69 7a 61 74 69 6f 6e 20 64  "authorization d
2c76f 65 6e 69 65 64 22 3b 20 20 20 20 20 20 20 20 20  enied";         
2c770 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c771 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c772 46 4f 52 4d 41 54 3a 20 20 20 20 20 7a 20 3d 20  FORMAT:     z = 
2c773 22 61 75 78 69 6c 69 61 72 79 20 64 61 74 61 62  "auxiliary datab
2c774 61 73 65 20 66 6f 72 6d 61 74 20 65 72 72 6f 72  ase format error
2c775 22 3b 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a  ";       break;.
2c776 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c777 52 41 4e 47 45 3a 20 20 20 20 20 20 7a 20 3d 20  RANGE:      z = 
2c778 22 62 69 6e 64 20 6f 72 20 63 6f 6c 75 6d 6e 20  "bind or column 
2c779 69 6e 64 65 78 20 6f 75 74 20 6f 66 20 72 61 6e  index out of ran
2c77a 67 65 22 3b 20 20 20 20 20 62 72 65 61 6b 3b 0a  ge";     break;.
2c77b 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45 5f      case SQLITE_
2c77c 4e 4f 54 41 44 42 3a 20 20 20 20 20 7a 20 3d 20  NOTADB:     z = 
2c77d 22 66 69 6c 65 20 69 73 20 65 6e 63 72 79 70 74  "file is encrypt
2c77e 65 64 20 6f 72 20 69 73 20 6e 6f 74 20 61 20 64  ed or is not a d
2c77f 61 74 61 62 61 73 65 22 3b 62 72 65 61 6b 3b 0a  atabase";break;.
2c780 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
2c781 20 20 20 20 20 20 20 20 20 20 20 20 7a 20 3d 20              z = 
2c782 22 75 6e 6b 6e 6f 77 6e 20 65 72 72 6f 72 22 3b  "unknown error";
2c783 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c784 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2c785 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a    }.  return z;.
2c786 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f  }../*.** This ro
2c787 75 74 69 6e 65 20 69 6d 70 6c 65 6d 65 6e 74 73  utine implements
2c788 20 61 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b   a busy callback
2c789 20 74 68 61 74 20 73 6c 65 65 70 73 20 61 6e 64   that sleeps and
2c78a 20 74 72 69 65 73 0a 2a 2a 20 61 67 61 69 6e 20   tries.** again 
2c78b 75 6e 74 69 6c 20 61 20 74 69 6d 65 6f 75 74 20  until a timeout 
2c78c 76 61 6c 75 65 20 69 73 20 72 65 61 63 68 65 64  value is reached
2c78d 2e 20 20 54 68 65 20 74 69 6d 65 6f 75 74 20 76  .  The timeout v
2c78e 61 6c 75 65 20 69 73 0a 2a 2a 20 61 6e 20 69 6e  alue is.** an in
2c78f 74 65 67 65 72 20 6e 75 6d 62 65 72 20 6f 66 20  teger number of 
2c790 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 70 61 73  milliseconds pas
2c791 73 65 64 20 69 6e 20 61 73 20 74 68 65 20 66 69  sed in as the fi
2c792 72 73 74 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 2e  rst.** argument.
2c793 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2c794 71 6c 69 74 65 44 65 66 61 75 6c 74 42 75 73 79  qliteDefaultBusy
2c795 43 61 6c 6c 62 61 63 6b 28 0a 20 76 6f 69 64 20  Callback(. void 
2c796 2a 70 74 72 2c 20 20 20 20 20 20 20 20 20 20 20  *ptr,           
2c797 20 20 20 20 2f 2a 20 44 61 74 61 62 61 73 65 20      /* Database 
2c798 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 69  connection */. i
2c799 6e 74 20 63 6f 75 6e 74 20 20 20 20 20 20 20 20  nt count        
2c79a 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2c79b 72 20 6f 66 20 74 69 6d 65 73 20 74 61 62 6c 65  r of times table
2c79c 20 68 61 73 20 62 65 65 6e 20 62 75 73 79 20 2a   has been busy *
2c79d 2f 0a 29 7b 0a 23 69 66 20 53 51 4c 49 54 45 5f  /.){.#if SQLITE_
2c79e 4f 53 5f 57 49 4e 20 7c 7c 20 28 64 65 66 69 6e  OS_WIN || (defin
2c79f 65 64 28 48 41 56 45 5f 55 53 4c 45 45 50 29 20  ed(HAVE_USLEEP) 
2c7a0 26 26 20 48 41 56 45 5f 55 53 4c 45 45 50 29 0a  && HAVE_USLEEP).
2c7a1 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 75    static const u
2c7a2 38 20 64 65 6c 61 79 73 5b 5d 20 3d 0a 20 20 20  8 delays[] =.   
2c7a3 20 20 7b 20 31 2c 20 32 2c 20 35 2c 20 31 30 2c    { 1, 2, 5, 10,
2c7a4 20 31 35 2c 20 32 30 2c 20 32 35 2c 20 32 35 2c   15, 20, 25, 25,
2c7a5 20 20 32 35 2c 20 20 35 30 2c 20 20 35 30 2c 20    25,  50,  50, 
2c7a6 31 30 30 20 7d 3b 0a 20 20 73 74 61 74 69 63 20  100 };.  static 
2c7a7 63 6f 6e 73 74 20 75 38 20 74 6f 74 61 6c 73 5b  const u8 totals[
2c7a8 5d 20 3d 0a 20 20 20 20 20 7b 20 30 2c 20 31 2c  ] =.     { 0, 1,
2c7a9 20 33 2c 20 20 38 2c 20 31 38 2c 20 33 33 2c 20   3,  8, 18, 33, 
2c7aa 35 33 2c 20 37 38 2c 20 31 30 33 2c 20 31 32 38  53, 78, 103, 128
2c7ab 2c 20 31 37 38 2c 20 32 32 38 20 7d 3b 0a 23 20  , 178, 228 };.# 
2c7ac 64 65 66 69 6e 65 20 4e 44 45 4c 41 59 20 28 73  define NDELAY (s
2c7ad 69 7a 65 6f 66 28 64 65 6c 61 79 73 29 2f 73 69  izeof(delays)/si
2c7ae 7a 65 6f 66 28 64 65 6c 61 79 73 5b 30 5d 29 29  zeof(delays[0]))
2c7af 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 20 3d  .  sqlite3 *db =
2c7b0 20 28 73 71 6c 69 74 65 33 20 2a 29 70 74 72 3b   (sqlite3 *)ptr;
2c7b1 0a 20 20 69 6e 74 20 74 69 6d 65 6f 75 74 20 3d  .  int timeout =
2c7b2 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74   db->busyTimeout
2c7b3 3b 0a 20 20 69 6e 74 20 64 65 6c 61 79 2c 20 70  ;.  int delay, p
2c7b4 72 69 6f 72 3b 0a 0a 20 20 61 73 73 65 72 74 28  rior;..  assert(
2c7b5 20 63 6f 75 6e 74 3e 3d 30 20 29 3b 0a 20 20 69   count>=0 );.  i
2c7b6 66 28 20 63 6f 75 6e 74 20 3c 20 4e 44 45 4c 41  f( count < NDELA
2c7b7 59 20 29 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d  Y ){.    delay =
2c7b8 20 64 65 6c 61 79 73 5b 63 6f 75 6e 74 5d 3b 0a   delays[count];.
2c7b9 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f 74 61      prior = tota
2c7ba 6c 73 5b 63 6f 75 6e 74 5d 3b 0a 20 20 7d 65 6c  ls[count];.  }el
2c7bb 73 65 7b 0a 20 20 20 20 64 65 6c 61 79 20 3d 20  se{.    delay = 
2c7bc 64 65 6c 61 79 73 5b 4e 44 45 4c 41 59 2d 31 5d  delays[NDELAY-1]
2c7bd 3b 0a 20 20 20 20 70 72 69 6f 72 20 3d 20 74 6f  ;.    prior = to
2c7be 74 61 6c 73 5b 4e 44 45 4c 41 59 2d 31 5d 20 2b  tals[NDELAY-1] +
2c7bf 20 64 65 6c 61 79 2a 28 63 6f 75 6e 74 2d 28 4e   delay*(count-(N
2c7c0 44 45 4c 41 59 2d 31 29 29 3b 0a 20 20 7d 0a 20  DELAY-1));.  }. 
2c7c1 20 69 66 28 20 70 72 69 6f 72 20 2b 20 64 65 6c   if( prior + del
2c7c2 61 79 20 3e 20 74 69 6d 65 6f 75 74 20 29 7b 0a  ay > timeout ){.
2c7c3 20 20 20 20 64 65 6c 61 79 20 3d 20 74 69 6d 65      delay = time
2c7c4 6f 75 74 20 2d 20 70 72 69 6f 72 3b 0a 20 20 20  out - prior;.   
2c7c5 20 69 66 28 20 64 65 6c 61 79 3c 3d 30 20 29 20   if( delay<=0 ) 
2c7c6 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2c7c7 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 64  sqlite3OsSleep(d
2c7c8 62 2d 3e 70 56 66 73 2c 20 64 65 6c 61 79 2a 31  b->pVfs, delay*1
2c7c9 30 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 31  000);.  return 1
2c7ca 3b 0a 23 65 6c 73 65 0a 20 20 73 71 6c 69 74 65  ;.#else.  sqlite
2c7cb 33 20 2a 64 62 20 3d 20 28 73 71 6c 69 74 65 33  3 *db = (sqlite3
2c7cc 20 2a 29 70 74 72 3b 0a 20 20 69 6e 74 20 74 69   *)ptr;.  int ti
2c7cd 6d 65 6f 75 74 20 3d 20 28 28 73 71 6c 69 74 65  meout = ((sqlite
2c7ce 33 20 2a 29 70 74 72 29 2d 3e 62 75 73 79 54 69  3 *)ptr)->busyTi
2c7cf 6d 65 6f 75 74 3b 0a 20 20 69 66 28 20 28 63 6f  meout;.  if( (co
2c7d0 75 6e 74 2b 31 29 2a 31 30 30 30 20 3e 20 74 69  unt+1)*1000 > ti
2c7d1 6d 65 6f 75 74 20 29 7b 0a 20 20 20 20 72 65 74  meout ){.    ret
2c7d2 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20 73 71 6c  urn 0;.  }.  sql
2c7d3 69 74 65 33 4f 73 53 6c 65 65 70 28 64 62 2d 3e  ite3OsSleep(db->
2c7d4 70 56 66 73 2c 20 31 30 30 30 30 30 30 29 3b 0a  pVfs, 1000000);.
2c7d5 20 20 72 65 74 75 72 6e 20 31 3b 0a 23 65 6e 64    return 1;.#end
2c7d6 69 66 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76 6f  if.}../*.** Invo
2c7d7 6b 65 20 74 68 65 20 67 69 76 65 6e 20 62 75 73  ke the given bus
2c7d8 79 20 68 61 6e 64 6c 65 72 2e 0a 2a 2a 0a 2a 2a  y handler..**.**
2c7d9 20 54 68 69 73 20 72 6f 75 74 69 6e 65 20 69 73   This routine is
2c7da 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61 6e 20   called when an 
2c7db 6f 70 65 72 61 74 69 6f 6e 20 66 61 69 6c 65 64  operation failed
2c7dc 20 77 69 74 68 20 61 20 6c 6f 63 6b 2e 0a 2a 2a   with a lock..**
2c7dd 20 49 66 20 74 68 69 73 20 72 6f 75 74 69 6e 65   If this routine
2c7de 20 72 65 74 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72   returns non-zer
2c7df 6f 2c 20 74 68 65 20 6c 6f 63 6b 20 69 73 20 72  o, the lock is r
2c7e0 65 74 72 69 65 64 2e 20 20 49 66 20 69 74 0a 2a  etried.  If it.*
2c7e1 2a 20 72 65 74 75 72 6e 73 20 30 2c 20 74 68 65  * returns 0, the
2c7e2 20 6f 70 65 72 61 74 69 6f 6e 20 61 62 6f 72 74   operation abort
2c7e3 73 20 77 69 74 68 20 61 6e 20 53 51 4c 49 54 45  s with an SQLITE
2c7e4 5f 42 55 53 59 20 65 72 72 6f 72 2e 0a 2a 2f 0a  _BUSY error..*/.
2c7e5 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2c7e6 6e 74 20 73 71 6c 69 74 65 33 49 6e 76 6f 6b 65  nt sqlite3Invoke
2c7e7 42 75 73 79 48 61 6e 64 6c 65 72 28 42 75 73 79  BusyHandler(Busy
2c7e8 48 61 6e 64 6c 65 72 20 2a 70 29 7b 0a 20 20 69  Handler *p){.  i
2c7e9 6e 74 20 72 63 3b 0a 20 20 69 66 28 20 4e 45 56  nt rc;.  if( NEV
2c7ea 45 52 28 70 3d 3d 30 29 20 7c 7c 20 70 2d 3e 78  ER(p==0) || p->x
2c7eb 46 75 6e 63 3d 3d 30 20 7c 7c 20 70 2d 3e 6e 42  Func==0 || p->nB
2c7ec 75 73 79 3c 30 20 29 20 72 65 74 75 72 6e 20 30  usy<0 ) return 0
2c7ed 3b 0a 20 20 72 63 20 3d 20 70 2d 3e 78 46 75 6e  ;.  rc = p->xFun
2c7ee 63 28 70 2d 3e 70 41 72 67 2c 20 70 2d 3e 6e 42  c(p->pArg, p->nB
2c7ef 75 73 79 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d  usy);.  if( rc==
2c7f0 30 20 29 7b 0a 20 20 20 20 70 2d 3e 6e 42 75 73  0 ){.    p->nBus
2c7f1 79 20 3d 20 2d 31 3b 0a 20 20 7d 65 6c 73 65 7b  y = -1;.  }else{
2c7f2 0a 20 20 20 20 70 2d 3e 6e 42 75 73 79 2b 2b 3b  .    p->nBusy++;
2c7f3 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2c7f4 3b 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73  ; .}../*.** This
2c7f5 20 72 6f 75 74 69 6e 65 20 73 65 74 73 20 74 68   routine sets th
2c7f6 65 20 62 75 73 79 20 63 61 6c 6c 62 61 63 6b 20  e busy callback 
2c7f7 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2c7f8 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2c7f9 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2c7fa 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2c7fb 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2c7fc 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2c7fd 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
2c7fe 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71 6c 69  _handler(.  sqli
2c7ff 74 65 33 20 2a 64 62 2c 0a 20 20 69 6e 74 20 28  te3 *db,.  int (
2c800 2a 78 42 75 73 79 29 28 76 6f 69 64 2a 2c 69 6e  *xBusy)(void*,in
2c801 74 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  t),.  void *pArg
2c802 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
2c803 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2c804 74 65 78 29 3b 0a 20 20 64 62 2d 3e 62 75 73 79  tex);.  db->busy
2c805 48 61 6e 64 6c 65 72 2e 78 46 75 6e 63 20 3d 20  Handler.xFunc = 
2c806 78 42 75 73 79 3b 0a 20 20 64 62 2d 3e 62 75 73  xBusy;.  db->bus
2c807 79 48 61 6e 64 6c 65 72 2e 70 41 72 67 20 3d 20  yHandler.pArg = 
2c808 70 41 72 67 3b 0a 20 20 64 62 2d 3e 62 75 73 79  pArg;.  db->busy
2c809 48 61 6e 64 6c 65 72 2e 6e 42 75 73 79 20 3d 20  Handler.nBusy = 
2c80a 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  0;.  sqlite3_mut
2c80b 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2c80c 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51  ex);.  return SQ
2c80d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 23 69 66 6e  LITE_OK;.}..#ifn
2c80e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2c80f 50 52 4f 47 52 45 53 53 5f 43 41 4c 4c 42 41 43  PROGRESS_CALLBAC
2c810 4b 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75  K./*.** This rou
2c811 74 69 6e 65 20 73 65 74 73 20 74 68 65 20 70 72  tine sets the pr
2c812 6f 67 72 65 73 73 20 63 61 6c 6c 62 61 63 6b 20  ogress callback 
2c813 66 6f 72 20 61 6e 20 53 71 6c 69 74 65 20 64 61  for an Sqlite da
2c814 74 61 62 61 73 65 20 74 6f 20 74 68 65 0a 2a 2a  tabase to the.**
2c815 20 67 69 76 65 6e 20 63 61 6c 6c 62 61 63 6b 20   given callback 
2c816 66 75 6e 63 74 69 6f 6e 20 77 69 74 68 20 74 68  function with th
2c817 65 20 67 69 76 65 6e 20 61 72 67 75 6d 65 6e 74  e given argument
2c818 2e 20 54 68 65 20 70 72 6f 67 72 65 73 73 20 63  . The progress c
2c819 61 6c 6c 62 61 63 6b 20 77 69 6c 6c 0a 2a 2a 20  allback will.** 
2c81a 62 65 20 69 6e 76 6f 6b 65 64 20 65 76 65 72 79  be invoked every
2c81b 20 6e 4f 70 73 20 6f 70 63 6f 64 65 73 2e 0a 2a   nOps opcodes..*
2c81c 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76 6f 69  /.SQLITE_API voi
2c81d 64 20 73 71 6c 69 74 65 33 5f 70 72 6f 67 72 65  d sqlite3_progre
2c81e 73 73 5f 68 61 6e 64 6c 65 72 28 0a 20 20 73 71  ss_handler(.  sq
2c81f 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 69 6e  lite3 *db, .  in
2c820 74 20 6e 4f 70 73 2c 0a 20 20 69 6e 74 20 28 2a  t nOps,.  int (*
2c821 78 50 72 6f 67 72 65 73 73 29 28 76 6f 69 64 2a  xProgress)(void*
2c822 29 2c 20 0a 20 20 76 6f 69 64 20 2a 70 41 72 67  ), .  void *pArg
2c823 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  .){.  sqlite3_mu
2c824 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2c825 74 65 78 29 3b 0a 20 20 69 66 28 20 6e 4f 70 73  tex);.  if( nOps
2c826 3e 30 20 29 7b 0a 20 20 20 20 64 62 2d 3e 78 50  >0 ){.    db->xP
2c827 72 6f 67 72 65 73 73 20 3d 20 78 50 72 6f 67 72  rogress = xProgr
2c828 65 73 73 3b 0a 20 20 20 20 64 62 2d 3e 6e 50 72  ess;.    db->nPr
2c829 6f 67 72 65 73 73 4f 70 73 20 3d 20 6e 4f 70 73  ogressOps = nOps
2c82a 3b 0a 20 20 20 20 64 62 2d 3e 70 50 72 6f 67 72  ;.    db->pProgr
2c82b 65 73 73 41 72 67 20 3d 20 70 41 72 67 3b 0a 20  essArg = pArg;. 
2c82c 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 62 2d 3e   }else{.    db->
2c82d 78 50 72 6f 67 72 65 73 73 20 3d 20 30 3b 0a 20  xProgress = 0;. 
2c82e 20 20 20 64 62 2d 3e 6e 50 72 6f 67 72 65 73 73     db->nProgress
2c82f 4f 70 73 20 3d 20 30 3b 0a 20 20 20 20 64 62 2d  Ops = 0;.    db-
2c830 3e 70 50 72 6f 67 72 65 73 73 41 72 67 20 3d 20  >pProgressArg = 
2c831 30 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  0;.  }.  sqlite3
2c832 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d  _mutex_leave(db-
2c833 3e 6d 75 74 65 78 29 3b 0a 7d 0a 23 65 6e 64 69  >mutex);.}.#endi
2c834 66 0a 0a 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 72  f.../*.** This r
2c835 6f 75 74 69 6e 65 20 69 6e 73 74 61 6c 6c 73 20  outine installs 
2c836 61 20 64 65 66 61 75 6c 74 20 62 75 73 79 20 68  a default busy h
2c837 61 6e 64 6c 65 72 20 74 68 61 74 20 77 61 69 74  andler that wait
2c838 73 20 66 6f 72 20 74 68 65 0a 2a 2a 20 73 70 65  s for the.** spe
2c839 63 69 66 69 65 64 20 6e 75 6d 62 65 72 20 6f 66  cified number of
2c83a 20 6d 69 6c 6c 69 73 65 63 6f 6e 64 73 20 62 65   milliseconds be
2c83b 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 20 30  fore returning 0
2c83c 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2c83d 69 6e 74 20 73 71 6c 69 74 65 33 5f 62 75 73 79  int sqlite3_busy
2c83e 5f 74 69 6d 65 6f 75 74 28 73 71 6c 69 74 65 33  _timeout(sqlite3
2c83f 20 2a 64 62 2c 20 69 6e 74 20 6d 73 29 7b 0a 20   *db, int ms){. 
2c840 20 69 66 28 20 6d 73 3e 30 20 29 7b 0a 20 20 20   if( ms>0 ){.   
2c841 20 64 62 2d 3e 62 75 73 79 54 69 6d 65 6f 75 74   db->busyTimeout
2c842 20 3d 20 6d 73 3b 0a 20 20 20 20 73 71 6c 69 74   = ms;.    sqlit
2c843 65 33 5f 62 75 73 79 5f 68 61 6e 64 6c 65 72 28  e3_busy_handler(
2c844 64 62 2c 20 73 71 6c 69 74 65 44 65 66 61 75 6c  db, sqliteDefaul
2c845 74 42 75 73 79 43 61 6c 6c 62 61 63 6b 2c 20 28  tBusyCallback, (
2c846 76 6f 69 64 2a 29 64 62 29 3b 0a 20 20 7d 65 6c  void*)db);.  }el
2c847 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
2c848 62 75 73 79 5f 68 61 6e 64 6c 65 72 28 64 62 2c  busy_handler(db,
2c849 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 65   0, 0);.  }.  re
2c84a 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2c84b 7d 0a 0a 2f 2a 0a 2a 2a 20 43 61 75 73 65 20 61  }../*.** Cause a
2c84c 6e 79 20 70 65 6e 64 69 6e 67 20 6f 70 65 72 61  ny pending opera
2c84d 74 69 6f 6e 20 74 6f 20 73 74 6f 70 20 61 74 20  tion to stop at 
2c84e 69 74 73 20 65 61 72 6c 69 65 73 74 20 6f 70 70  its earliest opp
2c84f 6f 72 74 75 6e 69 74 79 2e 0a 2a 2f 0a 53 51 4c  ortunity..*/.SQL
2c850 49 54 45 5f 41 50 49 20 76 6f 69 64 20 73 71 6c  ITE_API void sql
2c851 69 74 65 33 5f 69 6e 74 65 72 72 75 70 74 28 73  ite3_interrupt(s
2c852 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 64  qlite3 *db){.  d
2c853 62 2d 3e 75 31 2e 69 73 49 6e 74 65 72 72 75 70  b->u1.isInterrup
2c854 74 65 64 20 3d 20 31 3b 0a 7d 0a 0a 0a 2f 2a 0a  ted = 1;.}.../*.
2c855 2a 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e  ** This function
2c856 20 69 73 20 65 78 61 63 74 6c 79 20 74 68 65 20   is exactly the 
2c857 73 61 6d 65 20 61 73 20 73 71 6c 69 74 65 33 5f  same as sqlite3_
2c858 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28  create_function(
2c859 29 2c 20 65 78 63 65 70 74 0a 2a 2a 20 74 68 61  ), except.** tha
2c85a 74 20 69 74 20 69 73 20 64 65 73 69 67 6e 65 64  t it is designed
2c85b 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 20 62 79   to be called by
2c85c 20 69 6e 74 65 72 6e 61 6c 20 63 6f 64 65 2e 20   internal code. 
2c85d 54 68 65 20 64 69 66 66 65 72 65 6e 63 65 20 69  The difference i
2c85e 73 0a 2a 2a 20 74 68 61 74 20 69 66 20 61 20 6d  s.** that if a m
2c85f 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73 20 69 6e  alloc() fails in
2c860 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2c861 66 75 6e 63 74 69 6f 6e 28 29 2c 20 61 6e 20 65  function(), an e
2c862 72 72 6f 72 20 63 6f 64 65 0a 2a 2a 20 69 73 20  rror code.** is 
2c863 72 65 74 75 72 6e 65 64 20 61 6e 64 20 74 68 65  returned and the
2c864 20 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 66 6c   mallocFailed fl
2c865 61 67 20 63 6c 65 61 72 65 64 2e 20 0a 2a 2f 0a  ag cleared. .*/.
2c866 53 51 4c 49 54 45 5f 50 52 49 56 41 54 45 20 69  SQLITE_PRIVATE i
2c867 6e 74 20 73 71 6c 69 74 65 33 43 72 65 61 74 65  nt sqlite3Create
2c868 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 20  Func(.  sqlite3 
2c869 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
2c86a 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  r *zFunctionName
2c86b 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
2c86c 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20  int enc,.  void 
2c86d 2a 70 55 73 65 72 44 61 74 61 2c 0a 20 20 76 6f  *pUserData,.  vo
2c86e 69 64 20 28 2a 78 46 75 6e 63 29 28 73 71 6c 69  id (*xFunc)(sqli
2c86f 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
2c870 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
2c871 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 53 74  *),.  void (*xSt
2c872 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ep)(sqlite3_cont
2c873 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
2c874 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
2c875 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71 6c  id (*xFinal)(sql
2c876 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a 29  ite3_context*).)
2c877 7b 0a 20 20 46 75 6e 63 44 65 66 20 2a 70 3b 0a  {.  FuncDef *p;.
2c878 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 0a 20 20    int nName;..  
2c879 61 73 73 65 72 74 28 20 73 71 6c 69 74 65 33 5f  assert( sqlite3_
2c87a 6d 75 74 65 78 5f 68 65 6c 64 28 64 62 2d 3e 6d  mutex_held(db->m
2c87b 75 74 65 78 29 20 29 3b 0a 20 20 69 66 28 20 7a  utex) );.  if( z
2c87c 46 75 6e 63 74 69 6f 6e 4e 61 6d 65 3d 3d 30 20  FunctionName==0 
2c87d 7c 7c 0a 20 20 20 20 20 20 28 78 46 75 6e 63 20  ||.      (xFunc 
2c87e 26 26 20 28 78 46 69 6e 61 6c 20 7c 7c 20 78 53  && (xFinal || xS
2c87f 74 65 70 29 29 20 7c 7c 20 0a 20 20 20 20 20 20  tep)) || .      
2c880 28 21 78 46 75 6e 63 20 26 26 20 28 78 46 69 6e  (!xFunc && (xFin
2c881 61 6c 20 26 26 20 21 78 53 74 65 70 29 29 20 7c  al && !xStep)) |
2c882 7c 0a 20 20 20 20 20 20 28 21 78 46 75 6e 63 20  |.      (!xFunc 
2c883 26 26 20 28 21 78 46 69 6e 61 6c 20 26 26 20 78  && (!xFinal && x
2c884 53 74 65 70 29 29 20 7c 7c 0a 20 20 20 20 20 20  Step)) ||.      
2c885 28 6e 41 72 67 3c 2d 31 20 7c 7c 20 6e 41 72 67  (nArg<-1 || nArg
2c886 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55 4e 43  >SQLITE_MAX_FUNC
2c887 54 49 4f 4e 5f 41 52 47 29 20 7c 7c 0a 20 20 20  TION_ARG) ||.   
2c888 20 20 20 28 32 35 35 3c 28 6e 4e 61 6d 65 20 3d     (255<(nName =
2c889 20 73 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64   sqlite3Strlen(d
2c88a 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  b, zFunctionName
2c88b 29 29 29 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  ))) ){.    sqlit
2c88c 65 33 45 72 72 6f 72 28 64 62 2c 20 53 51 4c 49  e3Error(db, SQLI
2c88d 54 45 5f 45 52 52 4f 52 2c 20 22 62 61 64 20 70  TE_ERROR, "bad p
2c88e 61 72 61 6d 65 74 65 72 73 22 29 3b 0a 20 20 20  arameters");.   
2c88f 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2c890 52 52 4f 52 3b 0a 20 20 7d 0a 20 20 0a 23 69 66  RROR;.  }.  .#if
2c891 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2c892 5f 55 54 46 31 36 0a 20 20 2f 2a 20 49 66 20 53  _UTF16.  /* If S
2c893 51 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 73  QLITE_UTF16 is s
2c894 70 65 63 69 66 69 65 64 20 61 73 20 74 68 65 20  pecified as the 
2c895 65 6e 63 6f 64 69 6e 67 20 74 79 70 65 2c 20 74  encoding type, t
2c896 72 61 6e 73 66 6f 72 6d 20 74 68 69 73 0a 20 20  ransform this.  
2c897 2a 2a 20 74 6f 20 6f 6e 65 20 6f 66 20 53 51 4c  ** to one of SQL
2c898 49 54 45 5f 55 54 46 31 36 4c 45 20 6f 72 20 53  ITE_UTF16LE or S
2c899 51 4c 49 54 45 5f 55 54 46 31 36 42 45 20 75 73  QLITE_UTF16BE us
2c89a 69 6e 67 20 74 68 65 0a 20 20 2a 2a 20 53 51 4c  ing the.  ** SQL
2c89b 49 54 45 5f 55 54 46 31 36 4e 41 54 49 56 45 20  ITE_UTF16NATIVE 
2c89c 6d 61 63 72 6f 2e 20 53 51 4c 49 54 45 5f 55 54  macro. SQLITE_UT
2c89d 46 31 36 20 69 73 20 6e 6f 74 20 75 73 65 64 20  F16 is not used 
2c89e 69 6e 74 65 72 6e 61 6c 6c 79 2e 0a 20 20 2a 2a  internally..  **
2c89f 0a 20 20 2a 2a 20 49 66 20 53 51 4c 49 54 45 5f  .  ** If SQLITE_
2c8a0 41 4e 59 20 69 73 20 73 70 65 63 69 66 69 65 64  ANY is specified
2c8a1 2c 20 61 64 64 20 74 68 72 65 65 20 76 65 72 73  , add three vers
2c8a2 69 6f 6e 73 20 6f 66 20 74 68 65 20 66 75 6e 63  ions of the func
2c8a3 74 69 6f 6e 0a 20 20 2a 2a 20 74 6f 20 74 68 65  tion.  ** to the
2c8a4 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 20 20 2a   hash table..  *
2c8a5 2f 0a 20 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c  /.  if( enc==SQL
2c8a6 49 54 45 5f 55 54 46 31 36 20 29 7b 0a 20 20 20  ITE_UTF16 ){.   
2c8a7 20 65 6e 63 20 3d 20 53 51 4c 49 54 45 5f 55 54   enc = SQLITE_UT
2c8a8 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 7d 65 6c  F16NATIVE;.  }el
2c8a9 73 65 20 69 66 28 20 65 6e 63 3d 3d 53 51 4c 49  se if( enc==SQLI
2c8aa 54 45 5f 41 4e 59 20 29 7b 0a 20 20 20 20 69 6e  TE_ANY ){.    in
2c8ab 74 20 72 63 3b 0a 20 20 20 20 72 63 20 3d 20 73  t rc;.    rc = s
2c8ac 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e 63  qlite3CreateFunc
2c8ad 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
2c8ae 6d 65 2c 20 6e 41 72 67 2c 20 53 51 4c 49 54 45  me, nArg, SQLITE
2c8af 5f 55 54 46 38 2c 0a 20 20 20 20 20 20 20 20 20  _UTF8,.         
2c8b0 70 55 73 65 72 44 61 74 61 2c 20 78 46 75 6e 63  pUserData, xFunc
2c8b1 2c 20 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29  , xStep, xFinal)
2c8b2 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51  ;.    if( rc==SQ
2c8b3 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
2c8b4 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43 72 65   rc = sqlite3Cre
2c8b5 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46 75 6e  ateFunc(db, zFun
2c8b6 63 74 69 6f 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  ctionName, nArg,
2c8b7 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c   SQLITE_UTF16LE,
2c8b8 0a 20 20 20 20 20 20 20 20 20 20 70 55 73 65 72  .          pUser
2c8b9 44 61 74 61 2c 20 78 46 75 6e 63 2c 20 78 53 74  Data, xFunc, xSt
2c8ba 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a 20 20 20  ep, xFinal);.   
2c8bb 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53   }.    if( rc!=S
2c8bc 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2c8bd 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
2c8be 20 7d 0a 20 20 20 20 65 6e 63 20 3d 20 53 51 4c   }.    enc = SQL
2c8bf 49 54 45 5f 55 54 46 31 36 42 45 3b 0a 20 20 7d  ITE_UTF16BE;.  }
2c8c0 0a 23 65 6c 73 65 0a 20 20 65 6e 63 20 3d 20 53  .#else.  enc = S
2c8c1 51 4c 49 54 45 5f 55 54 46 38 3b 0a 23 65 6e 64  QLITE_UTF8;.#end
2c8c2 69 66 0a 20 20 0a 20 20 2f 2a 20 43 68 65 63 6b  if.  .  /* Check
2c8c3 20 69 66 20 61 6e 20 65 78 69 73 74 69 6e 67 20   if an existing 
2c8c4 66 75 6e 63 74 69 6f 6e 20 69 73 20 62 65 69 6e  function is bein
2c8c5 67 20 6f 76 65 72 72 69 64 64 65 6e 20 6f 72 20  g overridden or 
2c8c6 64 65 6c 65 74 65 64 2e 20 49 66 20 73 6f 2c 0a  deleted. If so,.
2c8c7 20 20 2a 2a 20 61 6e 64 20 74 68 65 72 65 20 61    ** and there a
2c8c8 72 65 20 61 63 74 69 76 65 20 56 4d 73 2c 20 74  re active VMs, t
2c8c9 68 65 6e 20 72 65 74 75 72 6e 20 53 51 4c 49 54  hen return SQLIT
2c8ca 45 5f 42 55 53 59 2e 20 49 66 20 61 20 66 75 6e  E_BUSY. If a fun
2c8cb 63 74 69 6f 6e 0a 20 20 2a 2a 20 69 73 20 62 65  ction.  ** is be
2c8cc 69 6e 67 20 6f 76 65 72 72 69 64 64 65 6e 2f 64  ing overridden/d
2c8cd 65 6c 65 74 65 64 20 62 75 74 20 74 68 65 72 65  eleted but there
2c8ce 20 61 72 65 20 6e 6f 20 61 63 74 69 76 65 20 56   are no active V
2c8cf 4d 73 2c 20 61 6c 6c 6f 77 20 74 68 65 0a 20 20  Ms, allow the.  
2c8d0 2a 2a 20 6f 70 65 72 61 74 69 6f 6e 20 74 6f 20  ** operation to 
2c8d1 63 6f 6e 74 69 6e 75 65 20 62 75 74 20 69 6e 76  continue but inv
2c8d2 61 6c 69 64 61 74 65 20 61 6c 6c 20 70 72 65 63  alidate all prec
2c8d3 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
2c8d4 74 73 2e 0a 20 20 2a 2f 0a 20 20 70 20 3d 20 73  ts..  */.  p = s
2c8d5 71 6c 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69  qlite3FindFuncti
2c8d6 6f 6e 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e  on(db, zFunction
2c8d7 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72  Name, nName, nAr
2c8d8 67 2c 20 65 6e 63 2c 20 30 29 3b 0a 20 20 69 66  g, enc, 0);.  if
2c8d9 28 20 70 20 26 26 20 70 2d 3e 69 50 72 65 66 45  ( p && p->iPrefE
2c8da 6e 63 3d 3d 65 6e 63 20 26 26 20 70 2d 3e 6e 41  nc==enc && p->nA
2c8db 72 67 3d 3d 6e 41 72 67 20 29 7b 0a 20 20 20 20  rg==nArg ){.    
2c8dc 69 66 28 20 64 62 2d 3e 61 63 74 69 76 65 56 64  if( db->activeVd
2c8dd 62 65 43 6e 74 20 29 7b 0a 20 20 20 20 20 20 73  beCnt ){.      s
2c8de 71 6c 69 74 65 33 45 72 72 6f 72 28 64 62 2c 20  qlite3Error(db, 
2c8df 53 51 4c 49 54 45 5f 42 55 53 59 2c 20 0a 20 20  SQLITE_BUSY, .  
2c8e0 20 20 20 20 20 20 22 55 6e 61 62 6c 65 20 74 6f        "Unable to
2c8e1 20 64 65 6c 65 74 65 2f 6d 6f 64 69 66 79 20 75   delete/modify u
2c8e2 73 65 72 2d 66 75 6e 63 74 69 6f 6e 20 64 75 65  ser-function due
2c8e3 20 74 6f 20 61 63 74 69 76 65 20 73 74 61 74 65   to active state
2c8e4 6d 65 6e 74 73 22 29 3b 0a 20 20 20 20 20 20 61  ments");.      a
2c8e5 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
2c8e6 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 20 20  ocFailed );.    
2c8e7 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2c8e8 42 55 53 59 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  BUSY;.    }else{
2c8e9 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 78  .      sqlite3Ex
2c8ea 70 69 72 65 50 72 65 70 61 72 65 64 53 74 61 74  pirePreparedStat
2c8eb 65 6d 65 6e 74 73 28 64 62 29 3b 0a 20 20 20 20  ements(db);.    
2c8ec 7d 0a 20 20 7d 0a 0a 20 20 70 20 3d 20 73 71 6c  }.  }..  p = sql
2c8ed 69 74 65 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e  ite3FindFunction
2c8ee 28 64 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61  (db, zFunctionNa
2c8ef 6d 65 2c 20 6e 4e 61 6d 65 2c 20 6e 41 72 67 2c  me, nName, nArg,
2c8f0 20 65 6e 63 2c 20 31 29 3b 0a 20 20 61 73 73 65   enc, 1);.  asse
2c8f1 72 74 28 70 20 7c 7c 20 64 62 2d 3e 6d 61 6c 6c  rt(p || db->mall
2c8f2 6f 63 46 61 69 6c 65 64 29 3b 0a 20 20 69 66 28  ocFailed);.  if(
2c8f3 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75 72   !p ){.    retur
2c8f4 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
2c8f5 20 20 7d 0a 20 20 70 2d 3e 66 6c 61 67 73 20 3d    }.  p->flags =
2c8f6 20 30 3b 0a 20 20 70 2d 3e 78 46 75 6e 63 20 3d   0;.  p->xFunc =
2c8f7 20 78 46 75 6e 63 3b 0a 20 20 70 2d 3e 78 53 74   xFunc;.  p->xSt
2c8f8 65 70 20 3d 20 78 53 74 65 70 3b 0a 20 20 70 2d  ep = xStep;.  p-
2c8f9 3e 78 46 69 6e 61 6c 69 7a 65 20 3d 20 78 46 69  >xFinalize = xFi
2c8fa 6e 61 6c 3b 0a 20 20 70 2d 3e 70 55 73 65 72 44  nal;.  p->pUserD
2c8fb 61 74 61 20 3d 20 70 55 73 65 72 44 61 74 61 3b  ata = pUserData;
2c8fc 0a 20 20 70 2d 3e 6e 41 72 67 20 3d 20 6e 41 72  .  p->nArg = nAr
2c8fd 67 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  g;.  return SQLI
2c8fe 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  TE_OK;.}../*.** 
2c8ff 43 72 65 61 74 65 20 6e 65 77 20 75 73 65 72 20  Create new user 
2c900 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a 2f 0a 53 51  functions..*/.SQ
2c901 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c  LITE_API int sql
2c902 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63  ite3_create_func
2c903 74 69 6f 6e 28 0a 20 20 73 71 6c 69 74 65 33 20  tion(.  sqlite3 
2c904 2a 64 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61  *db,.  const cha
2c905 72 20 2a 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  r *zFunctionName
2c906 2c 0a 20 20 69 6e 74 20 6e 41 72 67 2c 0a 20 20  ,.  int nArg,.  
2c907 69 6e 74 20 65 6e 63 2c 0a 20 20 76 6f 69 64 20  int enc,.  void 
2c908 2a 70 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 75  *p,.  void (*xFu
2c909 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  nc)(sqlite3_cont
2c90a 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65 33  ext*,int,sqlite3
2c90b 5f 76 61 6c 75 65 20 2a 2a 29 2c 0a 20 20 76 6f  _value **),.  vo
2c90c 69 64 20 28 2a 78 53 74 65 70 29 28 73 71 6c 69  id (*xStep)(sqli
2c90d 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e 74  te3_context*,int
2c90e 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a  ,sqlite3_value *
2c90f 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78 46 69  *),.  void (*xFi
2c910 6e 61 6c 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  nal)(sqlite3_con
2c911 74 65 78 74 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  text*).){.  int 
2c912 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rc;.  sqlite3_mu
2c913 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2c914 74 65 78 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c  tex);.  rc = sql
2c915 69 74 65 33 43 72 65 61 74 65 46 75 6e 63 28 64  ite3CreateFunc(d
2c916 62 2c 20 7a 46 75 6e 63 74 69 6f 6e 4e 61 6d 65  b, zFunctionName
2c917 2c 20 6e 41 72 67 2c 20 65 6e 63 2c 20 70 2c 20  , nArg, enc, p, 
2c918 78 46 75 6e 63 2c 20 78 53 74 65 70 2c 20 78 46  xFunc, xStep, xF
2c919 69 6e 61 6c 29 3b 0a 20 20 72 63 20 3d 20 73 71  inal);.  rc = sq
2c91a 6c 69 74 65 33 41 70 69 45 78 69 74 28 64 62 2c  lite3ApiExit(db,
2c91b 20 72 63 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f   rc);.  sqlite3_
2c91c 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2c91d 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2c91e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20   rc;.}..#ifndef 
2c91f 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31  SQLITE_OMIT_UTF1
2c920 36 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  6.SQLITE_API int
2c921 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f   sqlite3_create_
2c922 66 75 6e 63 74 69 6f 6e 31 36 28 0a 20 20 73 71  function16(.  sq
2c923 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63 6f 6e  lite3 *db,.  con
2c924 73 74 20 76 6f 69 64 20 2a 7a 46 75 6e 63 74 69  st void *zFuncti
2c925 6f 6e 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 6e 41  onName,.  int nA
2c926 72 67 2c 0a 20 20 69 6e 74 20 65 54 65 78 74 52  rg,.  int eTextR
2c927 65 70 2c 0a 20 20 76 6f 69 64 20 2a 70 2c 0a 20  ep,.  void *p,. 
2c928 20 76 6f 69 64 20 28 2a 78 46 75 6e 63 29 28 73   void (*xFunc)(s
2c929 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c  qlite3_context*,
2c92a 69 6e 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75  int,sqlite3_valu
2c92b 65 2a 2a 29 2c 0a 20 20 76 6f 69 64 20 28 2a 78  e**),.  void (*x
2c92c 53 74 65 70 29 28 73 71 6c 69 74 65 33 5f 63 6f  Step)(sqlite3_co
2c92d 6e 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74  ntext*,int,sqlit
2c92e 65 33 5f 76 61 6c 75 65 2a 2a 29 2c 0a 20 20 76  e3_value**),.  v
2c92f 6f 69 64 20 28 2a 78 46 69 6e 61 6c 29 28 73 71  oid (*xFinal)(sq
2c930 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 29 0a  lite3_context*).
2c931 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 63  ){.  int rc;.  c
2c932 68 61 72 20 2a 7a 46 75 6e 63 38 3b 0a 20 20 73  har *zFunc8;.  s
2c933 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65 6e 74  qlite3_mutex_ent
2c934 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  er(db->mutex);. 
2c935 20 61 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61   assert( !db->ma
2c936 6c 6c 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20  llocFailed );.  
2c937 7a 46 75 6e 63 38 20 3d 20 73 71 6c 69 74 65 33  zFunc8 = sqlite3
2c938 55 74 66 31 36 74 6f 38 28 64 62 2c 20 7a 46 75  Utf16to8(db, zFu
2c939 6e 63 74 69 6f 6e 4e 61 6d 65 2c 20 2d 31 29 3b  nctionName, -1);
2c93a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 43  .  rc = sqlite3C
2c93b 72 65 61 74 65 46 75 6e 63 28 64 62 2c 20 7a 46  reateFunc(db, zF
2c93c 75 6e 63 38 2c 20 6e 41 72 67 2c 20 65 54 65 78  unc8, nArg, eTex
2c93d 74 52 65 70 2c 20 70 2c 20 78 46 75 6e 63 2c 20  tRep, p, xFunc, 
2c93e 78 53 74 65 70 2c 20 78 46 69 6e 61 6c 29 3b 0a  xStep, xFinal);.
2c93f 20 20 73 71 6c 69 74 65 33 44 62 46 72 65 65 28    sqlite3DbFree(
2c940 64 62 2c 20 7a 46 75 6e 63 38 29 3b 0a 20 20 72  db, zFunc8);.  r
2c941 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78  c = sqlite3ApiEx
2c942 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73 71  it(db, rc);.  sq
2c943 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2c944 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2c945 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65 6e  return rc;.}.#en
2c946 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 6c  dif.../*.** Decl
2c947 61 72 65 20 74 68 61 74 20 61 20 66 75 6e 63 74  are that a funct
2c948 69 6f 6e 20 68 61 73 20 62 65 65 6e 20 6f 76 65  ion has been ove
2c949 72 6c 6f 61 64 65 64 20 62 79 20 61 20 76 69 72  rloaded by a vir
2c94a 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a  tual table..**.*
2c94b 2a 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f  * If the functio
2c94c 6e 20 61 6c 72 65 61 64 79 20 65 78 69 73 74 73  n already exists
2c94d 20 61 73 20 61 20 72 65 67 75 6c 61 72 20 67 6c   as a regular gl
2c94e 6f 62 61 6c 20 66 75 6e 63 74 69 6f 6e 2c 20 74  obal function, t
2c94f 68 65 6e 0a 2a 2a 20 74 68 69 73 20 72 6f 75 74  hen.** this rout
2c950 69 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 20  ine is a no-op. 
2c951 20 49 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e   If the function
2c952 20 64 6f 65 73 20 6e 6f 74 20 65 78 69 73 74 2c   does not exist,
2c953 20 74 68 65 6e 20 63 72 65 61 74 65 0a 2a 2a 20   then create.** 
2c954 61 20 6e 65 77 20 6f 6e 65 20 74 68 61 74 20 61  a new one that a
2c955 6c 77 61 79 73 20 74 68 72 6f 77 73 20 61 20 72  lways throws a r
2c956 75 6e 2d 74 69 6d 65 20 65 72 72 6f 72 2e 20 20  un-time error.  
2c957 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 76 69 72 74  .**.** When virt
2c958 75 61 6c 20 74 61 62 6c 65 73 20 69 6e 74 65 6e  ual tables inten
2c959 64 20 74 6f 20 70 72 6f 76 69 64 65 20 61 6e 20  d to provide an 
2c95a 6f 76 65 72 6c 6f 61 64 65 64 20 66 75 6e 63 74  overloaded funct
2c95b 69 6f 6e 2c 20 74 68 65 79 0a 2a 2a 20 73 68 6f  ion, they.** sho
2c95c 75 6c 64 20 63 61 6c 6c 20 74 68 69 73 20 72 6f  uld call this ro
2c95d 75 74 69 6e 65 20 74 6f 20 6d 61 6b 65 20 73 75  utine to make su
2c95e 72 65 20 74 68 65 20 67 6c 6f 62 61 6c 20 66 75  re the global fu
2c95f 6e 63 74 69 6f 6e 20 65 78 69 73 74 73 2e 0a 2a  nction exists..*
2c960 2a 20 41 20 67 6c 6f 62 61 6c 20 66 75 6e 63 74  * A global funct
2c961 69 6f 6e 20 6d 75 73 74 20 65 78 69 73 74 20 69  ion must exist i
2c962 6e 20 6f 72 64 65 72 20 66 6f 72 20 6e 61 6d 65  n order for name
2c963 20 72 65 73 6f 6c 75 74 69 6f 6e 20 74 6f 20 77   resolution to w
2c964 6f 72 6b 0a 2a 2a 20 70 72 6f 70 65 72 6c 79 2e  ork.** properly.
2c965 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2c966 6e 74 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c  nt sqlite3_overl
2c967 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 0a 20 20  oad_function(.  
2c968 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20 20 63  sqlite3 *db,.  c
2c969 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
2c96a 2c 0a 20 20 69 6e 74 20 6e 41 72 67 0a 29 7b 0a  ,.  int nArg.){.
2c96b 20 20 69 6e 74 20 6e 4e 61 6d 65 20 3d 20 73 71    int nName = sq
2c96c 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c 20  lite3Strlen(db, 
2c96d 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63  zName);.  int rc
2c96e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2c96f 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2c970 78 29 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65  x);.  if( sqlite
2c971 33 46 69 6e 64 46 75 6e 63 74 69 6f 6e 28 64 62  3FindFunction(db
2c972 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2c 20  , zName, nName, 
2c973 6e 41 72 67 2c 20 53 51 4c 49 54 45 5f 55 54 46  nArg, SQLITE_UTF
2c974 38 2c 20 30 29 3d 3d 30 20 29 7b 0a 20 20 20 20  8, 0)==0 ){.    
2c975 73 71 6c 69 74 65 33 43 72 65 61 74 65 46 75 6e  sqlite3CreateFun
2c976 63 28 64 62 2c 20 7a 4e 61 6d 65 2c 20 6e 41 72  c(db, zName, nAr
2c977 67 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 0a  g, SQLITE_UTF8,.
2c978 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2c979 20 20 20 20 20 20 30 2c 20 73 71 6c 69 74 65 33        0, sqlite3
2c97a 49 6e 76 61 6c 69 64 46 75 6e 63 74 69 6f 6e 2c  InvalidFunction,
2c97b 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 72 63   0, 0);.  }.  rc
2c97c 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45 78 69   = sqlite3ApiExi
2c97d 74 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 29  t(db, SQLITE_OK)
2c97e 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2c97f 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2c980 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
2c981 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2c982 54 45 5f 4f 4d 49 54 5f 54 52 41 43 45 0a 2f 2a  TE_OMIT_TRACE./*
2c983 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 74  .** Register a t
2c984 72 61 63 65 20 66 75 6e 63 74 69 6f 6e 2e 20 20  race function.  
2c985 54 68 65 20 70 41 72 67 20 66 72 6f 6d 20 74 68  The pArg from th
2c986 65 20 70 72 65 76 69 6f 75 73 6c 79 20 72 65 67  e previously reg
2c987 69 73 74 65 72 65 64 20 74 72 61 63 65 0a 2a 2a  istered trace.**
2c988 20 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a   is returned.  .
2c989 2a 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 74 72 61  **.** A NULL tra
2c98a 63 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61 6e  ce function mean
2c98b 73 20 74 68 61 74 20 6e 6f 20 74 72 61 63 69 6e  s that no tracin
2c98c 67 20 69 73 20 65 78 65 63 75 74 65 73 2e 20 20  g is executes.  
2c98d 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a 20 74 72  A non-NULL.** tr
2c98e 61 63 65 20 69 73 20 61 20 70 6f 69 6e 74 65 72  ace is a pointer
2c98f 20 74 6f 20 61 20 66 75 6e 63 74 69 6f 6e 20 74   to a function t
2c990 68 61 74 20 69 73 20 69 6e 76 6f 6b 65 64 20 61  hat is invoked a
2c991 74 20 74 68 65 20 73 74 61 72 74 20 6f 66 20 65  t the start of e
2c992 61 63 68 0a 2a 2a 20 53 51 4c 20 73 74 61 74 65  ach.** SQL state
2c993 6d 65 6e 74 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ment..*/.SQLITE_
2c994 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
2c995 33 5f 74 72 61 63 65 28 73 71 6c 69 74 65 33 20  3_trace(sqlite3 
2c996 2a 64 62 2c 20 76 6f 69 64 20 28 2a 78 54 72 61  *db, void (*xTra
2c997 63 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20  ce)(void*,const 
2c998 63 68 61 72 2a 29 2c 20 76 6f 69 64 20 2a 70 41  char*), void *pA
2c999 72 67 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  rg){.  void *pOl
2c99a 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
2c99b 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2c99c 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
2c99d 2d 3e 70 54 72 61 63 65 41 72 67 3b 0a 20 20 64  ->pTraceArg;.  d
2c99e 62 2d 3e 78 54 72 61 63 65 20 3d 20 78 54 72 61  b->xTrace = xTra
2c99f 63 65 3b 0a 20 20 64 62 2d 3e 70 54 72 61 63 65  ce;.  db->pTrace
2c9a0 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
2c9a1 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2c9a2 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2c9a3 72 65 74 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 2f  return pOld;.}./
2c9a4 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20  *.** Register a 
2c9a5 70 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e  profile function
2c9a6 2e 20 20 54 68 65 20 70 41 72 67 20 66 72 6f 6d  .  The pArg from
2c9a7 20 74 68 65 20 70 72 65 76 69 6f 75 73 6c 79 20   the previously 
2c9a8 72 65 67 69 73 74 65 72 65 64 20 0a 2a 2a 20 70  registered .** p
2c9a9 72 6f 66 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20  rofile function 
2c9aa 69 73 20 72 65 74 75 72 6e 65 64 2e 20 20 0a 2a  is returned.  .*
2c9ab 2a 0a 2a 2a 20 41 20 4e 55 4c 4c 20 70 72 6f 66  *.** A NULL prof
2c9ac 69 6c 65 20 66 75 6e 63 74 69 6f 6e 20 6d 65 61  ile function mea
2c9ad 6e 73 20 74 68 61 74 20 6e 6f 20 70 72 6f 66 69  ns that no profi
2c9ae 6c 69 6e 67 20 69 73 20 65 78 65 63 75 74 65 73  ling is executes
2c9af 2e 20 20 41 20 6e 6f 6e 2d 4e 55 4c 4c 0a 2a 2a  .  A non-NULL.**
2c9b0 20 70 72 6f 66 69 6c 65 20 69 73 20 61 20 70 6f   profile is a po
2c9b1 69 6e 74 65 72 20 74 6f 20 61 20 66 75 6e 63 74  inter to a funct
2c9b2 69 6f 6e 20 74 68 61 74 20 69 73 20 69 6e 76 6f  ion that is invo
2c9b3 6b 65 64 20 61 74 20 74 68 65 20 63 6f 6e 63 6c  ked at the concl
2c9b4 75 73 69 6f 6e 20 6f 66 0a 2a 2a 20 65 61 63 68  usion of.** each
2c9b5 20 53 51 4c 20 73 74 61 74 65 6d 65 6e 74 20 74   SQL statement t
2c9b6 68 61 74 20 69 73 20 72 75 6e 2e 0a 2a 2f 0a 53  hat is run..*/.S
2c9b7 51 4c 49 54 45 5f 41 50 49 20 76 6f 69 64 20 2a  QLITE_API void *
2c9b8 73 71 6c 69 74 65 33 5f 70 72 6f 66 69 6c 65 28  sqlite3_profile(
2c9b9 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a  .  sqlite3 *db,.
2c9ba 20 20 76 6f 69 64 20 28 2a 78 50 72 6f 66 69 6c    void (*xProfil
2c9bb 65 29 28 76 6f 69 64 2a 2c 63 6f 6e 73 74 20 63  e)(void*,const c
2c9bc 68 61 72 2a 2c 73 71 6c 69 74 65 5f 75 69 6e 74  har*,sqlite_uint
2c9bd 36 34 29 2c 0a 20 20 76 6f 69 64 20 2a 70 41 72  64),.  void *pAr
2c9be 67 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c  g.){.  void *pOl
2c9bf 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  d;.  sqlite3_mut
2c9c0 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74  ex_enter(db->mut
2c9c1 65 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62  ex);.  pOld = db
2c9c2 2d 3e 70 50 72 6f 66 69 6c 65 41 72 67 3b 0a 20  ->pProfileArg;. 
2c9c3 20 64 62 2d 3e 78 50 72 6f 66 69 6c 65 20 3d 20   db->xProfile = 
2c9c4 78 50 72 6f 66 69 6c 65 3b 0a 20 20 64 62 2d 3e  xProfile;.  db->
2c9c5 70 50 72 6f 66 69 6c 65 41 72 67 20 3d 20 70 41  pProfileArg = pA
2c9c6 72 67 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rg;.  sqlite3_mu
2c9c7 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75  tex_leave(db->mu
2c9c8 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 70  tex);.  return p
2c9c9 4f 6c 64 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a  Old;.}.#endif /*
2c9ca 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 52 41   SQLITE_OMIT_TRA
2c9cb 43 45 20 2a 2f 0a 0a 2f 2a 2a 2a 20 45 58 50 45  CE */../*** EXPE
2c9cc 52 49 4d 45 4e 54 41 4c 20 2a 2a 2a 0a 2a 2a 0a  RIMENTAL ***.**.
2c9cd 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 66 75  ** Register a fu
2c9ce 6e 63 74 69 6f 6e 20 74 6f 20 62 65 20 69 6e 76  nction to be inv
2c9cf 6f 6b 65 64 20 77 68 65 6e 20 61 20 74 72 61 6e  oked when a tran
2c9d0 73 61 63 74 69 6f 6e 20 63 6f 6d 6d 65 6e 74 73  saction comments
2c9d1 2e 0a 2a 2a 20 49 66 20 74 68 65 20 69 6e 76 6f  ..** If the invo
2c9d2 6b 65 64 20 66 75 6e 63 74 69 6f 6e 20 72 65 74  ked function ret
2c9d3 75 72 6e 73 20 6e 6f 6e 2d 7a 65 72 6f 2c 20 74  urns non-zero, t
2c9d4 68 65 6e 20 74 68 65 20 63 6f 6d 6d 69 74 20 62  hen the commit b
2c9d5 65 63 6f 6d 65 73 20 61 0a 2a 2a 20 72 6f 6c 6c  ecomes a.** roll
2c9d6 62 61 63 6b 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  back..*/.SQLITE_
2c9d7 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  API void *sqlite
2c9d8 33 5f 63 6f 6d 6d 69 74 5f 68 6f 6f 6b 28 0a 20  3_commit_hook(. 
2c9d9 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2c9da 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 41 74             /* At
2c9db 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f  tach the hook to
2c9dc 20 74 68 69 73 20 64 61 74 61 62 61 73 65 20 2a   this database *
2c9dd 2f 0a 20 20 69 6e 74 20 28 2a 78 43 61 6c 6c 62  /.  int (*xCallb
2c9de 61 63 6b 29 28 76 6f 69 64 2a 29 2c 20 20 2f 2a  ack)(void*),  /*
2c9df 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 69 6e 76   Function to inv
2c9e0 6f 6b 65 20 6f 6e 20 65 61 63 68 20 63 6f 6d 6d  oke on each comm
2c9e1 69 74 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  it */.  void *pA
2c9e2 72 67 20 20 20 20 20 20 20 20 20 20 20 20 20 20  rg              
2c9e3 20 20 2f 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f    /* Argument to
2c9e4 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f   the function */
2c9e5 0a 29 7b 0a 20 20 76 6f 69 64 20 2a 70 4f 6c 64  .){.  void *pOld
2c9e6 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2c9e7 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65  x_enter(db->mute
2c9e8 78 29 3b 0a 20 20 70 4f 6c 64 20 3d 20 64 62 2d  x);.  pOld = db-
2c9e9 3e 70 43 6f 6d 6d 69 74 41 72 67 3b 0a 20 20 64  >pCommitArg;.  d
2c9ea 62 2d 3e 78 43 6f 6d 6d 69 74 43 61 6c 6c 62 61  b->xCommitCallba
2c9eb 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b 0a  ck = xCallback;.
2c9ec 20 20 64 62 2d 3e 70 43 6f 6d 6d 69 74 41 72 67    db->pCommitArg
2c9ed 20 3d 20 70 41 72 67 3b 0a 20 20 73 71 6c 69 74   = pArg;.  sqlit
2c9ee 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2c9ef 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2c9f0 75 72 6e 20 70 4f 6c 64 3b 0a 7d 0a 0a 2f 2a 0a  urn pOld;.}../*.
2c9f1 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 61  ** Register a ca
2c9f2 6c 6c 62 61 63 6b 20 74 6f 20 62 65 20 69 6e 76  llback to be inv
2c9f3 6f 6b 65 64 20 65 61 63 68 20 74 69 6d 65 20 61  oked each time a
2c9f4 20 72 6f 77 20 69 73 20 75 70 64 61 74 65 64 2c   row is updated,
2c9f5 0a 2a 2a 20 69 6e 73 65 72 74 65 64 20 6f 72 20  .** inserted or 
2c9f6 64 65 6c 65 74 65 64 20 75 73 69 6e 67 20 74 68  deleted using th
2c9f7 69 73 20 64 61 74 61 62 61 73 65 20 63 6f 6e 6e  is database conn
2c9f8 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c 49 54  ection..*/.SQLIT
2c9f9 45 5f 41 50 49 20 76 6f 69 64 20 2a 73 71 6c 69  E_API void *sqli
2c9fa 74 65 33 5f 75 70 64 61 74 65 5f 68 6f 6f 6b 28  te3_update_hook(
2c9fb 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  .  sqlite3 *db, 
2c9fc 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2c9fd 41 74 74 61 63 68 20 74 68 65 20 68 6f 6f 6b 20  Attach the hook 
2c9fe 74 6f 20 74 68 69 73 20 64 61 74 61 62 61 73 65  to this database
2c9ff 20 2a 2f 0a 20 20 76 6f 69 64 20 28 2a 78 43 61   */.  void (*xCa
2ca00 6c 6c 62 61 63 6b 29 28 76 6f 69 64 2a 2c 69 6e  llback)(void*,in
2ca01 74 2c 63 68 61 72 20 63 6f 6e 73 74 20 2a 2c 63  t,char const *,c
2ca02 68 61 72 20 63 6f 6e 73 74 20 2a 2c 73 71 6c 69  har const *,sqli
2ca03 74 65 5f 69 6e 74 36 34 29 2c 0a 20 20 76 6f 69  te_int64),.  voi
2ca04 64 20 2a 70 41 72 67 20 20 20 20 20 20 20 20 20  d *pArg         
2ca05 20 20 20 20 20 20 20 2f 2a 20 41 72 67 75 6d 65         /* Argume
2ca06 6e 74 20 74 6f 20 74 68 65 20 66 75 6e 63 74 69  nt to the functi
2ca07 6f 6e 20 2a 2f 0a 29 7b 0a 20 20 76 6f 69 64 20  on */.){.  void 
2ca08 2a 70 52 65 74 3b 0a 20 20 73 71 6c 69 74 65 33  *pRet;.  sqlite3
2ca09 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2ca0a 3e 6d 75 74 65 78 29 3b 0a 20 20 70 52 65 74 20  >mutex);.  pRet 
2ca0b 3d 20 64 62 2d 3e 70 55 70 64 61 74 65 41 72 67  = db->pUpdateArg
2ca0c 3b 0a 20 20 64 62 2d 3e 78 55 70 64 61 74 65 43  ;.  db->xUpdateC
2ca0d 61 6c 6c 62 61 63 6b 20 3d 20 78 43 61 6c 6c 62  allback = xCallb
2ca0e 61 63 6b 3b 0a 20 20 64 62 2d 3e 70 55 70 64 61  ack;.  db->pUpda
2ca0f 74 65 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20  teArg = pArg;.  
2ca10 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2ca11 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2ca12 20 20 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d    return pRet;.}
2ca13 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
2ca14 20 61 20 63 61 6c 6c 62 61 63 6b 20 74 6f 20 62   a callback to b
2ca15 65 20 69 6e 76 6f 6b 65 64 20 65 61 63 68 20 74  e invoked each t
2ca16 69 6d 65 20 61 20 74 72 61 6e 73 61 63 74 69 6f  ime a transactio
2ca17 6e 20 69 73 20 72 6f 6c 6c 65 64 0a 2a 2a 20 62  n is rolled.** b
2ca18 61 63 6b 20 62 79 20 74 68 69 73 20 64 61 74 61  ack by this data
2ca19 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 2e  base connection.
2ca1a 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2ca1b 6f 69 64 20 2a 73 71 6c 69 74 65 33 5f 72 6f 6c  oid *sqlite3_rol
2ca1c 6c 62 61 63 6b 5f 68 6f 6f 6b 28 0a 20 20 73 71  lback_hook(.  sq
2ca1d 6c 69 74 65 33 20 2a 64 62 2c 20 20 20 20 20 20  lite3 *db,      
2ca1e 20 20 20 20 20 20 20 20 2f 2a 20 41 74 74 61 63          /* Attac
2ca1f 68 20 74 68 65 20 68 6f 6f 6b 20 74 6f 20 74 68  h the hook to th
2ca20 69 73 20 64 61 74 61 62 61 73 65 20 2a 2f 0a 20  is database */. 
2ca21 20 76 6f 69 64 20 28 2a 78 43 61 6c 6c 62 61 63   void (*xCallbac
2ca22 6b 29 28 76 6f 69 64 2a 29 2c 20 2f 2a 20 43 61  k)(void*), /* Ca
2ca23 6c 6c 62 61 63 6b 20 66 75 6e 63 74 69 6f 6e 20  llback function 
2ca24 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41 72 67 20  */.  void *pArg 
2ca25 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ca26 2a 20 41 72 67 75 6d 65 6e 74 20 74 6f 20 74 68  * Argument to th
2ca27 65 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b  e function */.){
2ca28 0a 20 20 76 6f 69 64 20 2a 70 52 65 74 3b 0a 20  .  void *pRet;. 
2ca29 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 65   sqlite3_mutex_e
2ca2a 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78 29 3b  nter(db->mutex);
2ca2b 0a 20 20 70 52 65 74 20 3d 20 64 62 2d 3e 70 52  .  pRet = db->pR
2ca2c 6f 6c 6c 62 61 63 6b 41 72 67 3b 0a 20 20 64 62  ollbackArg;.  db
2ca2d 2d 3e 78 52 6f 6c 6c 62 61 63 6b 43 61 6c 6c 62  ->xRollbackCallb
2ca2e 61 63 6b 20 3d 20 78 43 61 6c 6c 62 61 63 6b 3b  ack = xCallback;
2ca2f 0a 20 20 64 62 2d 3e 70 52 6f 6c 6c 62 61 63 6b  .  db->pRollback
2ca30 41 72 67 20 3d 20 70 41 72 67 3b 0a 20 20 73 71  Arg = pArg;.  sq
2ca31 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2ca32 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2ca33 72 65 74 75 72 6e 20 70 52 65 74 3b 0a 7d 0a 0a  return pRet;.}..
2ca34 2f 2a 0a 2a 2a 20 54 68 69 73 20 72 6f 75 74 69  /*.** This routi
2ca35 6e 65 20 69 73 20 63 61 6c 6c 65 64 20 74 6f 20  ne is called to 
2ca36 63 72 65 61 74 65 20 61 20 63 6f 6e 6e 65 63 74  create a connect
2ca37 69 6f 6e 20 74 6f 20 61 20 64 61 74 61 62 61 73  ion to a databas
2ca38 65 20 42 54 72 65 65 0a 2a 2a 20 64 72 69 76 65  e BTree.** drive
2ca39 72 2e 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65  r.  If zFilename
2ca3a 20 69 73 20 74 68 65 20 6e 61 6d 65 20 6f 66 20   is the name of 
2ca3b 61 20 66 69 6c 65 2c 20 74 68 65 6e 20 74 68 61  a file, then tha
2ca3c 74 20 66 69 6c 65 20 69 73 0a 2a 2a 20 6f 70 65  t file is.** ope
2ca3d 6e 65 64 20 61 6e 64 20 75 73 65 64 2e 20 20 49  ned and used.  I
2ca3e 66 20 7a 46 69 6c 65 6e 61 6d 65 20 69 73 20 74  f zFilename is t
2ca3f 68 65 20 6d 61 67 69 63 20 6e 61 6d 65 20 22 3a  he magic name ":
2ca40 6d 65 6d 6f 72 79 3a 22 20 74 68 65 6e 0a 2a 2a  memory:" then.**
2ca41 20 74 68 65 20 64 61 74 61 62 61 73 65 20 69 73   the database is
2ca42 20 73 74 6f 72 65 64 20 69 6e 20 6d 65 6d 6f 72   stored in memor
2ca43 79 20 28 61 6e 64 20 69 73 20 74 68 75 73 20 66  y (and is thus f
2ca44 6f 72 67 6f 74 74 65 6e 20 61 73 20 73 6f 6f 6e  orgotten as soon
2ca45 20 61 73 0a 2a 2a 20 74 68 65 20 63 6f 6e 6e 65   as.** the conne
2ca46 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65 64 2e  ction is closed.
2ca47 29 20 20 49 66 20 7a 46 69 6c 65 6e 61 6d 65 20  )  If zFilename 
2ca48 69 73 20 4e 55 4c 4c 20 74 68 65 6e 20 74 68 65  is NULL then the
2ca49 20 64 61 74 61 62 61 73 65 0a 2a 2a 20 69 73 20   database.** is 
2ca4a 61 20 22 76 69 72 74 75 61 6c 22 20 64 61 74 61  a "virtual" data
2ca4b 62 61 73 65 20 66 6f 72 20 74 72 61 6e 73 69 65  base for transie
2ca4c 6e 74 20 75 73 65 20 6f 6e 6c 79 20 61 6e 64 20  nt use only and 
2ca4d 69 73 20 64 65 6c 65 74 65 64 20 61 73 0a 2a 2a  is deleted as.**
2ca4e 20 73 6f 6f 6e 20 61 73 20 74 68 65 20 63 6f 6e   soon as the con
2ca4f 6e 65 63 74 69 6f 6e 20 69 73 20 63 6c 6f 73 65  nection is close
2ca50 64 2e 0a 2a 2a 0a 2a 2a 20 41 20 76 69 72 74 75  d..**.** A virtu
2ca51 61 6c 20 64 61 74 61 62 61 73 65 20 63 61 6e 20  al database can 
2ca52 62 65 20 65 69 74 68 65 72 20 61 20 64 69 73 6b  be either a disk
2ca53 20 66 69 6c 65 20 28 74 68 61 74 20 69 73 20 61   file (that is a
2ca54 75 74 6f 6d 61 74 69 63 61 6c 6c 79 0a 2a 2a 20  utomatically.** 
2ca55 64 65 6c 65 74 65 64 20 77 68 65 6e 20 74 68 65  deleted when the
2ca56 20 66 69 6c 65 20 69 73 20 63 6c 6f 73 65 64 29   file is closed)
2ca57 20 6f 72 20 69 74 20 61 6e 20 62 65 20 68 65 6c   or it an be hel
2ca58 64 20 65 6e 74 69 72 65 6c 79 20 69 6e 20 6d 65  d entirely in me
2ca59 6d 6f 72 79 2c 0a 2a 2a 20 64 65 70 65 6e 64 69  mory,.** dependi
2ca5a 6e 67 20 6f 6e 20 74 68 65 20 76 61 6c 75 65 73  ng on the values
2ca5b 20 6f 66 20 74 68 65 20 53 51 4c 49 54 45 5f 54   of the SQLITE_T
2ca5c 45 4d 50 5f 53 54 4f 52 45 20 63 6f 6d 70 69 6c  EMP_STORE compil
2ca5d 65 2d 74 69 6d 65 20 6d 61 63 72 6f 20 61 6e 64  e-time macro and
2ca5e 20 74 68 65 0a 2a 2a 20 64 62 2d 3e 74 65 6d 70   the.** db->temp
2ca5f 5f 73 74 6f 72 65 20 76 61 72 69 61 62 6c 65 2c  _store variable,
2ca60 20 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68   according to th
2ca61 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 63 68 61 72  e following char
2ca62 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 51 4c 49 54  t:.**.**   SQLIT
2ca63 45 5f 54 45 4d 50 5f 53 54 4f 52 45 20 20 20 20  E_TEMP_STORE    
2ca64 20 64 62 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 20   db->temp_store 
2ca65 20 20 20 20 4c 6f 63 61 74 69 6f 6e 20 6f 66 20      Location of 
2ca66 74 65 6d 70 6f 72 61 72 79 20 64 61 74 61 62 61  temporary databa
2ca67 73 65 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d  se.**   --------
2ca68 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20 20 2d 2d  ---------     --
2ca69 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 20 20 20 20  ------------    
2ca6a 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d   ---------------
2ca6b 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a  ---------------.
2ca6c 2a 2a 20 20 20 30 20 20 20 20 20 20 20 20 20 20  **   0          
2ca6d 20 20 20 20 20 20 20 20 20 20 20 61 6e 79 20 20             any  
2ca6e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 69                fi
2ca6f 6c 65 0a 2a 2a 20 20 20 31 20 20 20 20 20 20 20  le.**   1       
2ca70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 31 20                1 
2ca71 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca72 20 66 69 6c 65 0a 2a 2a 20 20 20 31 20 20 20 20   file.**   1    
2ca73 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca74 20 32 20 20 20 20 20 20 20 20 20 20 20 20 20 20   2              
2ca75 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20      memory.**   
2ca76 31 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  1               
2ca77 20 20 20 20 20 20 30 20 20 20 20 20 20 20 20 20        0         
2ca78 20 20 20 20 20 20 20 20 20 66 69 6c 65 0a 2a 2a           file.**
2ca79 20 20 20 32 20 20 20 20 20 20 20 20 20 20 20 20     2            
2ca7a 20 20 20 20 20 20 20 20 20 31 20 20 20 20 20 20           1      
2ca7b 20 20 20 20 20 20 20 20 20 20 20 20 66 69 6c 65              file
2ca7c 0a 2a 2a 20 20 20 32 20 20 20 20 20 20 20 20 20  .**   2         
2ca7d 20 20 20 20 20 20 20 20 20 20 20 20 32 20 20 20              2   
2ca7e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d                 m
2ca7f 65 6d 6f 72 79 0a 2a 2a 20 20 20 32 20 20 20 20  emory.**   2    
2ca80 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ca81 20 30 20 20 20 20 20 20 20 20 20 20 20 20 20 20   0              
2ca82 20 20 20 20 6d 65 6d 6f 72 79 0a 2a 2a 20 20 20      memory.**   
2ca83 33 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  3               
2ca84 20 20 20 20 20 20 61 6e 79 20 20 20 20 20 20 20        any       
2ca85 20 20 20 20 20 20 20 20 20 6d 65 6d 6f 72 79 0a           memory.
2ca86 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
2ca87 45 20 69 6e 74 20 73 71 6c 69 74 65 33 42 74 72  E int sqlite3Btr
2ca88 65 65 46 61 63 74 6f 72 79 28 0a 20 20 63 6f 6e  eeFactory(.  con
2ca89 73 74 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  st sqlite3 *db, 
2ca8a 20 20 20 20 20 20 20 2f 2a 20 4d 61 69 6e 20 64         /* Main d
2ca8b 61 74 61 62 61 73 65 20 77 68 65 6e 20 6f 70 65  atabase when ope
2ca8c 6e 69 6e 67 20 61 75 78 20 6f 74 68 65 72 77 69  ning aux otherwi
2ca8d 73 65 20 30 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  se 0 */.  const 
2ca8e 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  char *zFilename,
2ca8f 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74      /* Name of t
2ca90 68 65 20 66 69 6c 65 20 63 6f 6e 74 61 69 6e 69  he file containi
2ca91 6e 67 20 74 68 65 20 42 54 72 65 65 20 64 61 74  ng the BTree dat
2ca92 61 62 61 73 65 20 2a 2f 0a 20 20 69 6e 74 20 6f  abase */.  int o
2ca93 6d 69 74 4a 6f 75 72 6e 61 6c 2c 20 20 20 20 20  mitJournal,     
2ca94 20 20 20 20 20 2f 2a 20 69 66 20 54 52 55 45 20       /* if TRUE 
2ca95 74 68 65 6e 20 64 6f 20 6e 6f 74 20 6a 6f 75 72  then do not jour
2ca96 6e 61 6c 20 74 68 69 73 20 66 69 6c 65 20 2a 2f  nal this file */
2ca97 0a 20 20 69 6e 74 20 6e 43 61 63 68 65 2c 20 20  .  int nCache,  
2ca98 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
2ca99 48 6f 77 20 6d 61 6e 79 20 70 61 67 65 73 20 69  How many pages i
2ca9a 6e 20 74 68 65 20 70 61 67 65 20 63 61 63 68 65  n the page cache
2ca9b 20 2a 2f 0a 20 20 69 6e 74 20 76 66 73 46 6c 61   */.  int vfsFla
2ca9c 67 73 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  gs,             
2ca9d 2f 2a 20 46 6c 61 67 73 20 70 61 73 73 65 64 20  /* Flags passed 
2ca9e 74 68 72 6f 75 67 68 20 74 6f 20 76 66 73 4f 70  through to vfsOp
2ca9f 65 6e 20 2a 2f 0a 20 20 42 74 72 65 65 20 2a 2a  en */.  Btree **
2caa0 70 70 42 74 72 65 65 20 20 20 20 20 20 20 20 20  ppBtree         
2caa1 20 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20    /* Pointer to 
2caa2 6e 65 77 20 42 74 72 65 65 20 6f 62 6a 65 63 74  new Btree object
2caa3 20 77 72 69 74 74 65 6e 20 68 65 72 65 20 2a 2f   written here */
2caa4 0a 29 7b 0a 20 20 69 6e 74 20 62 74 46 6c 61 67  .){.  int btFlag
2caa5 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  s = 0;.  int rc;
2caa6 0a 20 20 0a 20 20 61 73 73 65 72 74 28 20 73 71  .  .  assert( sq
2caa7 6c 69 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64  lite3_mutex_held
2caa8 28 64 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 20  (db->mutex) );. 
2caa9 20 61 73 73 65 72 74 28 20 70 70 42 74 72 65 65   assert( ppBtree
2caaa 20 21 3d 20 30 29 3b 0a 20 20 69 66 28 20 6f 6d   != 0);.  if( om
2caab 69 74 4a 6f 75 72 6e 61 6c 20 29 7b 0a 20 20 20  itJournal ){.   
2caac 20 62 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45   btFlags |= BTRE
2caad 45 5f 4f 4d 49 54 5f 4a 4f 55 52 4e 41 4c 3b 0a  E_OMIT_JOURNAL;.
2caae 20 20 7d 0a 20 20 69 66 28 20 64 62 2d 3e 66 6c    }.  if( db->fl
2caaf 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4e 6f 52  ags & SQLITE_NoR
2cab0 65 61 64 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 62  eadlock ){.    b
2cab1 74 46 6c 61 67 73 20 7c 3d 20 42 54 52 45 45 5f  tFlags |= BTREE_
2cab2 4e 4f 5f 52 45 41 44 4c 4f 43 4b 3b 0a 20 20 7d  NO_READLOCK;.  }
2cab3 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61 6d 65  .  if( zFilename
2cab4 3d 3d 30 20 29 7b 0a 23 69 66 20 53 51 4c 49 54  ==0 ){.#if SQLIT
2cab5 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d 30 0a  E_TEMP_STORE==0.
2cab6 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68 69 6e      /* Do nothin
2cab7 67 20 2a 2f 0a 23 65 6e 64 69 66 0a 23 69 66 6e  g */.#endif.#ifn
2cab8 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f  def SQLITE_OMIT_
2cab9 4d 45 4d 4f 52 59 44 42 0a 23 69 66 20 53 51 4c  MEMORYDB.#if SQL
2caba 49 54 45 5f 54 45 4d 50 5f 53 54 4f 52 45 3d 3d  ITE_TEMP_STORE==
2cabb 31 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 74 65  1.    if( db->te
2cabc 6d 70 5f 73 74 6f 72 65 3d 3d 32 20 29 20 7a 46  mp_store==2 ) zF
2cabd 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f  ilename = ":memo
2cabe 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 69 66  ry:";.#endif.#if
2cabf 20 53 51 4c 49 54 45 5f 54 45 4d 50 5f 53 54 4f   SQLITE_TEMP_STO
2cac0 52 45 3d 3d 32 0a 20 20 20 20 69 66 28 20 64 62  RE==2.    if( db
2cac1 2d 3e 74 65 6d 70 5f 73 74 6f 72 65 21 3d 31 20  ->temp_store!=1 
2cac2 29 20 7a 46 69 6c 65 6e 61 6d 65 20 3d 20 22 3a  ) zFilename = ":
2cac3 6d 65 6d 6f 72 79 3a 22 3b 0a 23 65 6e 64 69 66  memory:";.#endif
2cac4 0a 23 69 66 20 53 51 4c 49 54 45 5f 54 45 4d 50  .#if SQLITE_TEMP
2cac5 5f 53 54 4f 52 45 3d 3d 33 0a 20 20 20 20 7a 46  _STORE==3.    zF
2cac6 69 6c 65 6e 61 6d 65 20 3d 20 22 3a 6d 65 6d 6f  ilename = ":memo
2cac7 72 79 3a 22 3b 0a 23 65 6e 64 69 66 0a 23 65 6e  ry:";.#endif.#en
2cac8 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f 4d  dif /* SQLITE_OM
2cac9 49 54 5f 4d 45 4d 4f 52 59 44 42 20 2a 2f 0a 20  IT_MEMORYDB */. 
2caca 20 7d 0a 0a 20 20 69 66 28 20 28 76 66 73 46 6c   }..  if( (vfsFl
2cacb 61 67 73 20 26 20 53 51 4c 49 54 45 5f 4f 50 45  ags & SQLITE_OPE
2cacc 4e 5f 4d 41 49 4e 5f 44 42 29 21 3d 30 20 26 26  N_MAIN_DB)!=0 &&
2cacd 20 28 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 20 7c   (zFilename==0 |
2cace 7c 20 2a 7a 46 69 6c 65 6e 61 6d 65 3d 3d 30 29  | *zFilename==0)
2cacf 20 29 7b 0a 20 20 20 20 76 66 73 46 6c 61 67 73   ){.    vfsFlags
2cad0 20 3d 20 28 76 66 73 46 6c 61 67 73 20 26 20 7e   = (vfsFlags & ~
2cad1 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e  SQLITE_OPEN_MAIN
2cad2 5f 44 42 29 20 7c 20 53 51 4c 49 54 45 5f 4f 50  _DB) | SQLITE_OP
2cad3 45 4e 5f 54 45 4d 50 5f 44 42 3b 0a 20 20 7d 0a  EN_TEMP_DB;.  }.
2cad4 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 42 74    rc = sqlite3Bt
2cad5 72 65 65 4f 70 65 6e 28 7a 46 69 6c 65 6e 61 6d  reeOpen(zFilenam
2cad6 65 2c 20 28 73 71 6c 69 74 65 33 20 2a 29 64 62  e, (sqlite3 *)db
2cad7 2c 20 70 70 42 74 72 65 65 2c 20 62 74 46 6c 61  , ppBtree, btFla
2cad8 67 73 2c 20 76 66 73 46 6c 61 67 73 29 3b 0a 0a  gs, vfsFlags);..
2cad9 20 20 2f 2a 20 49 66 20 74 68 65 20 42 2d 54 72    /* If the B-Tr
2cada 65 65 20 77 61 73 20 73 75 63 63 65 73 73 66 75  ee was successfu
2cadb 6c 6c 79 20 6f 70 65 6e 65 64 2c 20 73 65 74 20  lly opened, set 
2cadc 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
2cadd 73 69 7a 65 20 74 6f 20 74 68 65 0a 20 20 2a 2a  size to the.  **
2cade 20 64 65 66 61 75 6c 74 20 76 61 6c 75 65 2e 20   default value. 
2cadf 45 78 63 65 70 74 2c 20 69 66 20 74 68 65 20 63  Except, if the c
2cae0 61 6c 6c 20 74 6f 20 42 74 72 65 65 4f 70 65 6e  all to BtreeOpen
2cae1 28 29 20 72 65 74 75 72 6e 65 64 20 61 20 68 61  () returned a ha
2cae2 6e 64 6c 65 0a 20 20 2a 2a 20 6f 70 65 6e 20 6f  ndle.  ** open o
2cae3 6e 20 61 6e 20 65 78 69 73 74 69 6e 67 20 73 68  n an existing sh
2cae4 61 72 65 64 20 70 61 67 65 72 2d 63 61 63 68 65  ared pager-cache
2cae5 2c 20 64 6f 20 6e 6f 74 20 63 68 61 6e 67 65 20  , do not change 
2cae6 74 68 65 20 70 61 67 65 72 2d 63 61 63 68 65 20  the pager-cache 
2cae7 0a 20 20 2a 2a 20 73 69 7a 65 2e 0a 20 20 2a 2f  .  ** size..  */
2cae8 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2cae9 45 5f 4f 4b 20 26 26 20 30 3d 3d 73 71 6c 69 74  E_OK && 0==sqlit
2caea 65 33 42 74 72 65 65 53 63 68 65 6d 61 28 2a 70  e3BtreeSchema(*p
2caeb 70 42 74 72 65 65 2c 20 30 2c 20 30 29 20 29 7b  pBtree, 0, 0) ){
2caec 0a 20 20 20 20 73 71 6c 69 74 65 33 42 74 72 65  .    sqlite3Btre
2caed 65 53 65 74 43 61 63 68 65 53 69 7a 65 28 2a 70  eSetCacheSize(*p
2caee 70 42 74 72 65 65 2c 20 6e 43 61 63 68 65 29 3b  pBtree, nCache);
2caef 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63  .  }.  return rc
2caf0 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72  ;.}../*.** Retur
2caf1 6e 20 55 54 46 2d 38 20 65 6e 63 6f 64 65 64 20  n UTF-8 encoded 
2caf2 45 6e 67 6c 69 73 68 20 6c 61 6e 67 75 61 67 65  English language
2caf3 20 65 78 70 6c 61 6e 61 74 69 6f 6e 20 6f 66 20   explanation of 
2caf4 74 68 65 20 6d 6f 73 74 20 72 65 63 65 6e 74 0a  the most recent.
2caf5 2a 2a 20 65 72 72 6f 72 2e 0a 2a 2f 0a 53 51 4c  ** error..*/.SQL
2caf6 49 54 45 5f 41 50 49 20 63 6f 6e 73 74 20 63 68  ITE_API const ch
2caf7 61 72 20 2a 73 71 6c 69 74 65 33 5f 65 72 72 6d  ar *sqlite3_errm
2caf8 73 67 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  sg(sqlite3 *db){
2caf9 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2cafa 3b 0a 20 20 69 66 28 20 21 64 62 20 29 7b 0a 20  ;.  if( !db ){. 
2cafb 20 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65     return sqlite
2cafc 33 45 72 72 53 74 72 28 53 51 4c 49 54 45 5f 4e  3ErrStr(SQLITE_N
2cafd 4f 4d 45 4d 29 3b 0a 20 20 7d 0a 20 20 69 66 28  OMEM);.  }.  if(
2cafe 20 21 73 71 6c 69 74 65 33 53 61 66 65 74 79 43   !sqlite3SafetyC
2caff 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28 64 62 29  heckSickOrOk(db)
2cb00 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73   ){.    return s
2cb01 71 6c 69 74 65 33 45 72 72 53 74 72 28 53 51 4c  qlite3ErrStr(SQL
2cb02 49 54 45 5f 4d 49 53 55 53 45 29 3b 0a 20 20 7d  ITE_MISUSE);.  }
2cb03 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2cb04 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2cb05 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
2cb06 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2cb07 3b 0a 20 20 7a 20 3d 20 28 63 68 61 72 2a 29 73  ;.  z = (char*)s
2cb08 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
2cb09 74 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 61  t(db->pErr);.  a
2cb0a 73 73 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c  ssert( !db->mall
2cb0b 6f 63 46 61 69 6c 65 64 20 29 3b 0a 20 20 69 66  ocFailed );.  if
2cb0c 28 20 7a 3d 3d 30 20 29 7b 0a 20 20 20 20 7a 20  ( z==0 ){.    z 
2cb0d 3d 20 73 71 6c 69 74 65 33 45 72 72 53 74 72 28  = sqlite3ErrStr(
2cb0e 64 62 2d 3e 65 72 72 43 6f 64 65 29 3b 0a 20 20  db->errCode);.  
2cb0f 7d 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  }.  sqlite3_mute
2cb10 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2cb11 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a  x);.  return z;.
2cb12 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54  }..#ifndef SQLIT
2cb13 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a  E_OMIT_UTF16./*.
2cb14 2a 2a 20 52 65 74 75 72 6e 20 55 54 46 2d 31 36  ** Return UTF-16
2cb15 20 65 6e 63 6f 64 65 64 20 45 6e 67 6c 69 73 68   encoded English
2cb16 20 6c 61 6e 67 75 61 67 65 20 65 78 70 6c 61 6e   language explan
2cb17 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 6d 6f 73  ation of the mos
2cb18 74 20 72 65 63 65 6e 74 0a 2a 2a 20 65 72 72 6f  t recent.** erro
2cb19 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  r..*/.SQLITE_API
2cb1a 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 73 71 6c   const void *sql
2cb1b 69 74 65 33 5f 65 72 72 6d 73 67 31 36 28 73 71  ite3_errmsg16(sq
2cb1c 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20 20 2f 2a  lite3 *db){.  /*
2cb1d 20 42 65 63 61 75 73 65 20 61 6c 6c 20 74 68 65   Because all the
2cb1e 20 63 68 61 72 61 63 74 65 72 73 20 69 6e 20 74   characters in t
2cb1f 68 65 20 73 74 72 69 6e 67 20 61 72 65 20 69 6e  he string are in
2cb20 20 74 68 65 20 75 6e 69 63 6f 64 65 0a 20 20 2a   the unicode.  *
2cb21 2a 20 72 61 6e 67 65 20 30 78 30 30 2d 30 78 46  * range 0x00-0xF
2cb22 46 2c 20 69 66 20 77 65 20 70 61 64 20 74 68 65  F, if we pad the
2cb23 20 62 69 67 2d 65 6e 64 69 61 6e 20 73 74 72 69   big-endian stri
2cb24 6e 67 20 77 69 74 68 20 61 20 0a 20 20 2a 2a 20  ng with a .  ** 
2cb25 7a 65 72 6f 20 62 79 74 65 2c 20 77 65 20 63 61  zero byte, we ca
2cb26 6e 20 6f 62 74 61 69 6e 20 74 68 65 20 6c 69 74  n obtain the lit
2cb27 74 6c 65 2d 65 6e 64 69 61 6e 20 73 74 72 69 6e  tle-endian strin
2cb28 67 20 77 69 74 68 0a 20 20 2a 2a 20 26 62 69 67  g with.  ** &big
2cb29 5f 65 6e 64 69 61 6e 5b 31 5d 2e 0a 20 20 2a 2f  _endian[1]..  */
2cb2a 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
2cb2b 63 68 61 72 20 6f 75 74 4f 66 4d 65 6d 42 65 5b  char outOfMemBe[
2cb2c 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20 27 6f 27  ] = {.    0, 'o'
2cb2d 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 74 27  , 0, 'u', 0, 't'
2cb2e 2c 20 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30  , 0, ' ', .    0
2cb2f 2c 20 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20 30  , 'o', 0, 'f', 0
2cb30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27  , ' ', .    0, '
2cb31 6d 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27  m', 0, 'e', 0, '
2cb32 6d 27 2c 20 30 2c 20 27 6f 27 2c 20 30 2c 20 27  m', 0, 'o', 0, '
2cb33 72 27 2c 20 30 2c 20 27 79 27 2c 20 30 2c 20 30  r', 0, 'y', 0, 0
2cb34 2c 20 30 0a 20 20 7d 3b 0a 20 20 73 74 61 74 69  , 0.  };.  stati
2cb35 63 20 63 6f 6e 73 74 20 63 68 61 72 20 6d 69 73  c const char mis
2cb36 75 73 65 42 65 20 5b 5d 20 3d 20 7b 0a 20 20 20  useBe [] = {.   
2cb37 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27 69 27 2c   0, 'l', 0, 'i',
2cb38 20 30 2c 20 27 62 27 2c 20 30 2c 20 27 72 27 2c   0, 'b', 0, 'r',
2cb39 20 30 2c 20 27 61 27 2c 20 30 2c 20 27 72 27 2c   0, 'a', 0, 'r',
2cb3a 20 30 2c 20 27 79 27 2c 20 30 2c 20 27 20 27 2c   0, 'y', 0, ' ',
2cb3b 20 0a 20 20 20 20 30 2c 20 27 72 27 2c 20 30 2c   .    0, 'r', 0,
2cb3c 20 27 6f 27 2c 20 30 2c 20 27 75 27 2c 20 30 2c   'o', 0, 'u', 0,
2cb3d 20 27 74 27 2c 20 30 2c 20 27 69 27 2c 20 30 2c   't', 0, 'i', 0,
2cb3e 20 27 6e 27 2c 20 30 2c 20 27 65 27 2c 20 30 2c   'n', 0, 'e', 0,
2cb3f 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 63   ' ', .    0, 'c
2cb40 27 2c 20 30 2c 20 27 61 27 2c 20 30 2c 20 27 6c  ', 0, 'a', 0, 'l
2cb41 27 2c 20 30 2c 20 27 6c 27 2c 20 30 2c 20 27 65  ', 0, 'l', 0, 'e
2cb42 27 2c 20 30 2c 20 27 64 27 2c 20 30 2c 20 27 20  ', 0, 'd', 0, ' 
2cb43 27 2c 20 0a 20 20 20 20 30 2c 20 27 6f 27 2c 20  ', .    0, 'o', 
2cb44 30 2c 20 27 75 27 2c 20 30 2c 20 27 74 27 2c 20  0, 'u', 0, 't', 
2cb45 30 2c 20 27 20 27 2c 20 0a 20 20 20 20 30 2c 20  0, ' ', .    0, 
2cb46 27 6f 27 2c 20 30 2c 20 27 66 27 2c 20 30 2c 20  'o', 0, 'f', 0, 
2cb47 27 20 27 2c 20 0a 20 20 20 20 30 2c 20 27 73 27  ' ', .    0, 's'
2cb48 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 27 71 27  , 0, 'e', 0, 'q'
2cb49 2c 20 30 2c 20 27 75 27 2c 20 30 2c 20 27 65 27  , 0, 'u', 0, 'e'
2cb4a 2c 20 30 2c 20 27 6e 27 2c 20 30 2c 20 27 63 27  , 0, 'n', 0, 'c'
2cb4b 2c 20 30 2c 20 27 65 27 2c 20 30 2c 20 30 2c 20  , 0, 'e', 0, 0, 
2cb4c 30 0a 20 20 7d 3b 0a 0a 20 20 63 6f 6e 73 74 20  0.  };..  const 
2cb4d 76 6f 69 64 20 2a 7a 3b 0a 20 20 69 66 28 20 21  void *z;.  if( !
2cb4e 64 62 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  db ){.    return
2cb4f 20 28 76 6f 69 64 20 2a 29 28 26 6f 75 74 4f 66   (void *)(&outOf
2cb50 4d 65 6d 42 65 5b 53 51 4c 49 54 45 5f 55 54 46  MemBe[SQLITE_UTF
2cb51 31 36 4e 41 54 49 56 45 3d 3d 53 51 4c 49 54 45  16NATIVE==SQLITE
2cb52 5f 55 54 46 31 36 4c 45 3f 31 3a 30 5d 29 3b 0a  _UTF16LE?1:0]);.
2cb53 20 20 7d 0a 20 20 69 66 28 20 21 73 71 6c 69 74    }.  if( !sqlit
2cb54 65 33 53 61 66 65 74 79 43 68 65 63 6b 53 69 63  e3SafetyCheckSic
2cb55 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a 20 20 20  kOrOk(db) ){.   
2cb56 20 72 65 74 75 72 6e 20 28 76 6f 69 64 20 2a 29   return (void *)
2cb57 28 26 6d 69 73 75 73 65 42 65 5b 53 51 4c 49 54  (&misuseBe[SQLIT
2cb58 45 5f 55 54 46 31 36 4e 41 54 49 56 45 3d 3d 53  E_UTF16NATIVE==S
2cb59 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 3f 31 3a  QLITE_UTF16LE?1:
2cb5a 30 5d 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74  0]);.  }.  sqlit
2cb5b 65 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64  e3_mutex_enter(d
2cb5c 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73  b->mutex);.  ass
2cb5d 65 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63  ert( !db->malloc
2cb5e 46 61 69 6c 65 64 20 29 3b 0a 20 20 7a 20 3d 20  Failed );.  z = 
2cb5f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2cb60 78 74 31 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a  xt16(db->pErr);.
2cb61 20 20 69 66 28 20 7a 3d 3d 30 20 29 7b 0a 20 20    if( z==0 ){.  
2cb62 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53 65    sqlite3ValueSe
2cb63 74 53 74 72 28 64 62 2d 3e 70 45 72 72 2c 20 2d  tStr(db->pErr, -
2cb64 31 2c 20 73 71 6c 69 74 65 33 45 72 72 53 74 72  1, sqlite3ErrStr
2cb65 28 64 62 2d 3e 65 72 72 43 6f 64 65 29 2c 0a 20  (db->errCode),. 
2cb66 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 55          SQLITE_U
2cb67 54 46 38 2c 20 53 51 4c 49 54 45 5f 53 54 41 54  TF8, SQLITE_STAT
2cb68 49 43 29 3b 0a 20 20 20 20 7a 20 3d 20 73 71 6c  IC);.    z = sql
2cb69 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
2cb6a 36 28 64 62 2d 3e 70 45 72 72 29 3b 0a 20 20 7d  6(db->pErr);.  }
2cb6b 0a 20 20 2f 2a 20 41 20 6d 61 6c 6c 6f 63 28 29  .  /* A malloc()
2cb6c 20 6d 61 79 20 68 61 76 65 20 66 61 69 6c 65 64   may have failed
2cb6d 20 77 69 74 68 69 6e 20 74 68 65 20 63 61 6c 6c   within the call
2cb6e 20 74 6f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75   to sqlite3_valu
2cb6f 65 5f 74 65 78 74 31 36 28 29 0a 20 20 2a 2a 20  e_text16().  ** 
2cb70 61 62 6f 76 65 2e 20 49 66 20 74 68 69 73 20 69  above. If this i
2cb71 73 20 74 68 65 20 63 61 73 65 2c 20 74 68 65 6e  s the case, then
2cb72 20 74 68 65 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46   the db->mallocF
2cb73 61 69 6c 65 64 20 66 6c 61 67 20 6e 65 65 64 73  ailed flag needs
2cb74 20 74 6f 0a 20 20 2a 2a 20 62 65 20 63 6c 65 61   to.  ** be clea
2cb75 72 65 64 20 62 65 66 6f 72 65 20 72 65 74 75 72  red before retur
2cb76 6e 69 6e 67 2e 20 44 6f 20 74 68 69 73 20 64 69  ning. Do this di
2cb77 72 65 63 74 6c 79 2c 20 69 6e 73 74 65 61 64 20  rectly, instead 
2cb78 6f 66 20 76 69 61 0a 20 20 2a 2a 20 73 71 6c 69  of via.  ** sqli
2cb79 74 65 33 41 70 69 45 78 69 74 28 29 2c 20 74 6f  te3ApiExit(), to
2cb7a 20 61 76 6f 69 64 20 73 65 74 74 69 6e 67 20 74   avoid setting t
2cb7b 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
2cb7c 6c 65 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65  le error message
2cb7d 2e 0a 20 20 2a 2f 0a 20 20 64 62 2d 3e 6d 61 6c  ..  */.  db->mal
2cb7e 6c 6f 63 46 61 69 6c 65 64 20 3d 20 30 3b 0a 20  locFailed = 0;. 
2cb7f 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c   sqlite3_mutex_l
2cb80 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b  eave(db->mutex);
2cb81 0a 20 20 72 65 74 75 72 6e 20 7a 3b 0a 7d 0a 23  .  return z;.}.#
2cb82 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f  endif /* SQLITE_
2cb83 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f  OMIT_UTF16 */../
2cb84 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20  *.** Return the 
2cb85 6d 6f 73 74 20 72 65 63 65 6e 74 20 65 72 72 6f  most recent erro
2cb86 72 20 63 6f 64 65 20 67 65 6e 65 72 61 74 65 64  r code generated
2cb87 20 62 79 20 61 6e 20 53 51 4c 69 74 65 20 72 6f   by an SQLite ro
2cb88 75 74 69 6e 65 2e 20 49 66 20 4e 55 4c 4c 20 69  utine. If NULL i
2cb89 73 0a 2a 2a 20 70 61 73 73 65 64 20 74 6f 20 74  s.** passed to t
2cb8a 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20 77 65  his function, we
2cb8b 20 61 73 73 75 6d 65 20 61 20 6d 61 6c 6c 6f 63   assume a malloc
2cb8c 28 29 20 66 61 69 6c 65 64 20 64 75 72 69 6e 67  () failed during
2cb8d 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 2e   sqlite3_open().
2cb8e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2cb8f 6e 74 20 73 71 6c 69 74 65 33 5f 65 72 72 63 6f  nt sqlite3_errco
2cb90 64 65 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b  de(sqlite3 *db){
2cb91 0a 20 20 69 66 28 20 64 62 20 26 26 20 21 73 71  .  if( db && !sq
2cb92 6c 69 74 65 33 53 61 66 65 74 79 43 68 65 63 6b  lite3SafetyCheck
2cb93 53 69 63 6b 4f 72 4f 6b 28 64 62 29 20 29 7b 0a  SickOrOk(db) ){.
2cb94 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2cb95 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d 0a 20 20  E_MISUSE;.  }.  
2cb96 69 66 28 20 21 64 62 20 7c 7c 20 64 62 2d 3e 6d  if( !db || db->m
2cb97 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b 0a 20  allocFailed ){. 
2cb98 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
2cb99 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 72 65  _NOMEM;.  }.  re
2cb9a 74 75 72 6e 20 64 62 2d 3e 65 72 72 43 6f 64 65  turn db->errCode
2cb9b 20 26 20 64 62 2d 3e 65 72 72 4d 61 73 6b 3b 0a   & db->errMask;.
2cb9c 7d 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74  }.SQLITE_API int
2cb9d 20 73 71 6c 69 74 65 33 5f 65 78 74 65 6e 64 65   sqlite3_extende
2cb9e 64 5f 65 72 72 63 6f 64 65 28 73 71 6c 69 74 65  d_errcode(sqlite
2cb9f 33 20 2a 64 62 29 7b 0a 20 20 69 66 28 20 64 62  3 *db){.  if( db
2cba0 20 26 26 20 21 73 71 6c 69 74 65 33 53 61 66 65   && !sqlite3Safe
2cba1 74 79 43 68 65 63 6b 53 69 63 6b 4f 72 4f 6b 28  tyCheckSickOrOk(
2cba2 64 62 29 20 29 7b 0a 20 20 20 20 72 65 74 75 72  db) ){.    retur
2cba3 6e 20 53 51 4c 49 54 45 5f 4d 49 53 55 53 45 3b  n SQLITE_MISUSE;
2cba4 0a 20 20 7d 0a 20 20 69 66 28 20 21 64 62 20 7c  .  }.  if( !db |
2cba5 7c 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  | db->mallocFail
2cba6 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
2cba7 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
2cba8 20 7d 0a 20 20 72 65 74 75 72 6e 20 64 62 2d 3e   }.  return db->
2cba9 65 72 72 43 6f 64 65 3b 0a 7d 0a 0a 2f 2a 0a 2a  errCode;.}../*.*
2cbaa 2a 20 43 72 65 61 74 65 20 61 20 6e 65 77 20 63  * Create a new c
2cbab 6f 6c 6c 61 74 69 6e 67 20 66 75 6e 63 74 69 6f  ollating functio
2cbac 6e 20 66 6f 72 20 64 61 74 61 62 61 73 65 20 22  n for database "
2cbad 64 62 22 2e 20 20 54 68 65 20 6e 61 6d 65 20 69  db".  The name i
2cbae 73 20 7a 4e 61 6d 65 0a 2a 2a 20 61 6e 64 20 74  s zName.** and t
2cbaf 68 65 20 65 6e 63 6f 64 69 6e 67 20 69 73 20 65  he encoding is e
2cbb0 6e 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nc..*/.static in
2cbb1 74 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f  t createCollatio
2cbb2 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  n(.  sqlite3* db
2cbb3 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
2cbb4 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
2cbb5 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74  nc, .  void* pCt
2cbb6 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  x,.  int(*xCompa
2cbb7 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
2cbb8 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
2cbb9 6e 73 74 20 76 6f 69 64 2a 29 2c 0a 20 20 76 6f  nst void*),.  vo
2cbba 69 64 28 2a 78 44 65 6c 29 28 76 6f 69 64 2a 29  id(*xDel)(void*)
2cbbb 0a 29 7b 0a 20 20 43 6f 6c 6c 53 65 71 20 2a 70  .){.  CollSeq *p
2cbbc 43 6f 6c 6c 3b 0a 20 20 69 6e 74 20 65 6e 63 32  Coll;.  int enc2
2cbbd 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b 0a 20  ;.  int nName;. 
2cbbe 20 0a 20 20 61 73 73 65 72 74 28 20 73 71 6c 69   .  assert( sqli
2cbbf 74 65 33 5f 6d 75 74 65 78 5f 68 65 6c 64 28 64  te3_mutex_held(d
2cbc0 62 2d 3e 6d 75 74 65 78 29 20 29 3b 0a 0a 20 20  b->mutex) );..  
2cbc1 2f 2a 20 49 66 20 53 51 4c 49 54 45 5f 55 54 46  /* If SQLITE_UTF
2cbc2 31 36 20 69 73 20 73 70 65 63 69 66 69 65 64 20  16 is specified 
2cbc3 61 73 20 74 68 65 20 65 6e 63 6f 64 69 6e 67 20  as the encoding 
2cbc4 74 79 70 65 2c 20 74 72 61 6e 73 66 6f 72 6d 20  type, transform 
2cbc5 74 68 69 73 0a 20 20 2a 2a 20 74 6f 20 6f 6e 65  this.  ** to one
2cbc6 20 6f 66 20 53 51 4c 49 54 45 5f 55 54 46 31 36   of SQLITE_UTF16
2cbc7 4c 45 20 6f 72 20 53 51 4c 49 54 45 5f 55 54 46  LE or SQLITE_UTF
2cbc8 31 36 42 45 20 75 73 69 6e 67 20 74 68 65 0a 20  16BE using the. 
2cbc9 20 2a 2a 20 53 51 4c 49 54 45 5f 55 54 46 31 36   ** SQLITE_UTF16
2cbca 4e 41 54 49 56 45 20 6d 61 63 72 6f 2e 20 53 51  NATIVE macro. SQ
2cbcb 4c 49 54 45 5f 55 54 46 31 36 20 69 73 20 6e 6f  LITE_UTF16 is no
2cbcc 74 20 75 73 65 64 20 69 6e 74 65 72 6e 61 6c 6c  t used internall
2cbcd 79 2e 0a 20 20 2a 2f 0a 20 20 65 6e 63 32 20 3d  y..  */.  enc2 =
2cbce 20 65 6e 63 20 26 20 7e 53 51 4c 49 54 45 5f 55   enc & ~SQLITE_U
2cbcf 54 46 31 36 5f 41 4c 49 47 4e 45 44 3b 0a 20 20  TF16_ALIGNED;.  
2cbd0 69 66 28 20 65 6e 63 32 3d 3d 53 51 4c 49 54 45  if( enc2==SQLITE
2cbd1 5f 55 54 46 31 36 20 29 7b 0a 20 20 20 20 65 6e  _UTF16 ){.    en
2cbd2 63 32 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 31  c2 = SQLITE_UTF1
2cbd3 36 4e 41 54 49 56 45 3b 0a 20 20 7d 0a 20 20 69  6NATIVE;.  }.  i
2cbd4 66 28 20 28 65 6e 63 32 26 7e 33 29 21 3d 30 20  f( (enc2&~3)!=0 
2cbd5 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  ){.    return SQ
2cbd6 4c 49 54 45 5f 4d 49 53 55 53 45 3b 0a 20 20 7d  LITE_MISUSE;.  }
2cbd7 0a 0a 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20  ..  /* Check if 
2cbd8 74 68 69 73 20 63 61 6c 6c 20 69 73 20 72 65 6d  this call is rem
2cbd9 6f 76 69 6e 67 20 6f 72 20 72 65 70 6c 61 63 69  oving or replaci
2cbda 6e 67 20 61 6e 20 65 78 69 73 74 69 6e 67 20 63  ng an existing c
2cbdb 6f 6c 6c 61 74 69 6f 6e 20 0a 20 20 2a 2a 20 73  ollation .  ** s
2cbdc 65 71 75 65 6e 63 65 2e 20 49 66 20 73 6f 2c 20  equence. If so, 
2cbdd 61 6e 64 20 74 68 65 72 65 20 61 72 65 20 61 63  and there are ac
2cbde 74 69 76 65 20 56 4d 73 2c 20 72 65 74 75 72 6e  tive VMs, return
2cbdf 20 62 75 73 79 2e 20 49 66 20 74 68 65 72 65 0a   busy. If there.
2cbe0 20 20 2a 2a 20 61 72 65 20 6e 6f 20 61 63 74 69    ** are no acti
2cbe1 76 65 20 56 4d 73 2c 20 69 6e 76 61 6c 69 64 61  ve VMs, invalida
2cbe2 74 65 20 61 6e 79 20 70 72 65 2d 63 6f 6d 70 69  te any pre-compi
2cbe3 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73 2e 0a  led statements..
2cbe4 20 20 2a 2f 0a 20 20 6e 4e 61 6d 65 20 3d 20 73    */.  nName = s
2cbe5 71 6c 69 74 65 33 53 74 72 6c 65 6e 28 64 62 2c  qlite3Strlen(db,
2cbe6 20 7a 4e 61 6d 65 29 3b 0a 20 20 70 43 6f 6c 6c   zName);.  pColl
2cbe7 20 3d 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f   = sqlite3FindCo
2cbe8 6c 6c 53 65 71 28 64 62 2c 20 28 75 38 29 65 6e  llSeq(db, (u8)en
2cbe9 63 32 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  c2, zName, nName
2cbea 2c 20 30 29 3b 0a 20 20 69 66 28 20 70 43 6f 6c  , 0);.  if( pCol
2cbeb 6c 20 26 26 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70  l && pColl->xCmp
2cbec 20 29 7b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e   ){.    if( db->
2cbed 61 63 74 69 76 65 56 64 62 65 43 6e 74 20 29 7b  activeVdbeCnt ){
2cbee 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 45 72  .      sqlite3Er
2cbef 72 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 42  ror(db, SQLITE_B
2cbf0 55 53 59 2c 20 0a 20 20 20 20 20 20 20 20 22 55  USY, .        "U
2cbf1 6e 61 62 6c 65 20 74 6f 20 64 65 6c 65 74 65 2f  nable to delete/
2cbf2 6d 6f 64 69 66 79 20 63 6f 6c 6c 61 74 69 6f 6e  modify collation
2cbf3 20 73 65 71 75 65 6e 63 65 20 64 75 65 20 74 6f   sequence due to
2cbf4 20 61 63 74 69 76 65 20 73 74 61 74 65 6d 65 6e   active statemen
2cbf5 74 73 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75  ts");.      retu
2cbf6 72 6e 20 53 51 4c 49 54 45 5f 42 55 53 59 3b 0a  rn SQLITE_BUSY;.
2cbf7 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65      }.    sqlite
2cbf8 33 45 78 70 69 72 65 50 72 65 70 61 72 65 64 53  3ExpirePreparedS
2cbf9 74 61 74 65 6d 65 6e 74 73 28 64 62 29 3b 0a 0a  tatements(db);..
2cbfa 20 20 20 20 2f 2a 20 49 66 20 63 6f 6c 6c 61 74      /* If collat
2cbfb 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 70 43 6f  ion sequence pCo
2cbfc 6c 6c 20 77 61 73 20 63 72 65 61 74 65 64 20 64  ll was created d
2cbfd 69 72 65 63 74 6c 79 20 62 79 20 61 20 63 61 6c  irectly by a cal
2cbfe 6c 20 74 6f 0a 20 20 20 20 2a 2a 20 73 71 6c 69  l to.    ** sqli
2cbff 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2cc00 74 69 6f 6e 2c 20 61 6e 64 20 6e 6f 74 20 67 65  tion, and not ge
2cc01 6e 65 72 61 74 65 64 20 62 79 20 73 79 6e 74 68  nerated by synth
2cc02 43 6f 6c 6c 53 65 71 28 29 2c 0a 20 20 20 20 2a  CollSeq(),.    *
2cc03 2a 20 74 68 65 6e 20 61 6e 79 20 63 6f 70 69 65  * then any copie
2cc04 73 20 6d 61 64 65 20 62 79 20 73 79 6e 74 68 43  s made by synthC
2cc05 6f 6c 6c 53 65 71 28 29 20 6e 65 65 64 20 74 6f  ollSeq() need to
2cc06 20 62 65 20 69 6e 76 61 6c 69 64 61 74 65 64 2e   be invalidated.
2cc07 0a 20 20 20 20 2a 2a 20 41 6c 73 6f 2c 20 63 6f  .    ** Also, co
2cc08 6c 6c 61 74 69 6f 6e 20 64 65 73 74 72 75 63 74  llation destruct
2cc09 6f 72 20 2d 20 43 6f 6c 6c 53 65 71 2e 78 44 65  or - CollSeq.xDe
2cc0a 6c 28 29 20 2d 20 66 75 6e 63 74 69 6f 6e 20 6d  l() - function m
2cc0b 61 79 20 6e 65 65 64 0a 20 20 20 20 2a 2a 20 74  ay need.    ** t
2cc0c 6f 20 62 65 20 63 61 6c 6c 65 64 2e 0a 20 20 20  o be called..   
2cc0d 20 2a 2f 20 0a 20 20 20 20 69 66 28 20 28 70 43   */ .    if( (pC
2cc0e 6f 6c 6c 2d 3e 65 6e 63 20 26 20 7e 53 51 4c 49  oll->enc & ~SQLI
2cc0f 54 45 5f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44  TE_UTF16_ALIGNED
2cc10 29 3d 3d 65 6e 63 32 20 29 7b 0a 20 20 20 20 20  )==enc2 ){.     
2cc11 20 43 6f 6c 6c 53 65 71 20 2a 61 43 6f 6c 6c 20   CollSeq *aColl 
2cc12 3d 20 73 71 6c 69 74 65 33 48 61 73 68 46 69 6e  = sqlite3HashFin
2cc13 64 28 26 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c  d(&db->aCollSeq,
2cc14 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a   zName, nName);.
2cc15 20 20 20 20 20 20 69 6e 74 20 6a 3b 0a 20 20 20        int j;.   
2cc16 20 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 33 3b     for(j=0; j<3;
2cc17 20 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 43   j++){.        C
2cc18 6f 6c 6c 53 65 71 20 2a 70 20 3d 20 26 61 43 6f  ollSeq *p = &aCo
2cc19 6c 6c 5b 6a 5d 3b 0a 20 20 20 20 20 20 20 20 69  ll[j];.        i
2cc1a 66 28 20 70 2d 3e 65 6e 63 3d 3d 70 43 6f 6c 6c  f( p->enc==pColl
2cc1b 2d 3e 65 6e 63 20 29 7b 0a 20 20 20 20 20 20 20  ->enc ){.       
2cc1c 20 20 20 69 66 28 20 70 2d 3e 78 44 65 6c 20 29     if( p->xDel )
2cc1d 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 2d  {.            p-
2cc1e 3e 78 44 65 6c 28 70 2d 3e 70 55 73 65 72 29 3b  >xDel(p->pUser);
2cc1f 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20  .          }.   
2cc20 20 20 20 20 20 20 20 70 2d 3e 78 43 6d 70 20 3d         p->xCmp =
2cc21 20 30 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20   0;.        }.  
2cc22 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2cc23 0a 20 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74  .  pColl = sqlit
2cc24 65 33 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62  e3FindCollSeq(db
2cc25 2c 20 28 75 38 29 65 6e 63 32 2c 20 7a 4e 61 6d  , (u8)enc2, zNam
2cc26 65 2c 20 6e 4e 61 6d 65 2c 20 31 29 3b 0a 20 20  e, nName, 1);.  
2cc27 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
2cc28 20 70 43 6f 6c 6c 2d 3e 78 43 6d 70 20 3d 20 78   pColl->xCmp = x
2cc29 43 6f 6d 70 61 72 65 3b 0a 20 20 20 20 70 43 6f  Compare;.    pCo
2cc2a 6c 6c 2d 3e 70 55 73 65 72 20 3d 20 70 43 74 78  ll->pUser = pCtx
2cc2b 3b 0a 20 20 20 20 70 43 6f 6c 6c 2d 3e 78 44 65  ;.    pColl->xDe
2cc2c 6c 20 3d 20 78 44 65 6c 3b 0a 20 20 20 20 70 43  l = xDel;.    pC
2cc2d 6f 6c 6c 2d 3e 65 6e 63 20 3d 20 65 6e 63 32 20  oll->enc = enc2 
2cc2e 7c 20 28 65 6e 63 20 26 20 53 51 4c 49 54 45 5f  | (enc & SQLITE_
2cc2f 55 54 46 31 36 5f 41 4c 49 47 4e 45 44 29 3b 0a  UTF16_ALIGNED);.
2cc30 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 45 72 72    }.  sqlite3Err
2cc31 6f 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b  or(db, SQLITE_OK
2cc32 2c 20 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  , 0);.  return S
2cc33 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 0a 2f 2a  QLITE_OK;.}.../*
2cc34 0a 2a 2a 20 54 68 69 73 20 61 72 72 61 79 20 64  .** This array d
2cc35 65 66 69 6e 65 73 20 68 61 72 64 20 75 70 70 65  efines hard uppe
2cc36 72 20 62 6f 75 6e 64 73 20 6f 6e 20 6c 69 6d 69  r bounds on limi
2cc37 74 20 76 61 6c 75 65 73 2e 20 20 54 68 65 0a 2a  t values.  The.*
2cc38 2a 20 69 6e 69 74 69 61 6c 69 7a 65 72 20 6d 75  * initializer mu
2cc39 73 74 20 62 65 20 6b 65 70 74 20 69 6e 20 73 79  st be kept in sy
2cc3a 6e 63 20 77 69 74 68 20 74 68 65 20 53 51 4c 49  nc with the SQLI
2cc3b 54 45 5f 4c 49 4d 49 54 5f 2a 0a 2a 2a 20 23 64  TE_LIMIT_*.** #d
2cc3c 65 66 69 6e 65 73 20 69 6e 20 73 71 6c 69 74 65  efines in sqlite
2cc3d 33 2e 68 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63  3.h..*/.static c
2cc3e 6f 6e 73 74 20 69 6e 74 20 61 48 61 72 64 4c 69  onst int aHardLi
2cc3f 6d 69 74 5b 5d 20 3d 20 7b 0a 20 20 53 51 4c 49  mit[] = {.  SQLI
2cc40 54 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 2c 0a 20  TE_MAX_LENGTH,. 
2cc41 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53 51 4c 5f   SQLITE_MAX_SQL_
2cc42 4c 45 4e 47 54 48 2c 0a 20 20 53 51 4c 49 54 45  LENGTH,.  SQLITE
2cc43 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 2c 0a 20 20 53  _MAX_COLUMN,.  S
2cc44 51 4c 49 54 45 5f 4d 41 58 5f 45 58 50 52 5f 44  QLITE_MAX_EXPR_D
2cc45 45 50 54 48 2c 0a 20 20 53 51 4c 49 54 45 5f 4d  EPTH,.  SQLITE_M
2cc46 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
2cc47 43 54 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41 58  CT,.  SQLITE_MAX
2cc48 5f 56 44 42 45 5f 4f 50 2c 0a 20 20 53 51 4c 49  _VDBE_OP,.  SQLI
2cc49 54 45 5f 4d 41 58 5f 46 55 4e 43 54 49 4f 4e 5f  TE_MAX_FUNCTION_
2cc4a 41 52 47 2c 0a 20 20 53 51 4c 49 54 45 5f 4d 41  ARG,.  SQLITE_MA
2cc4b 58 5f 41 54 54 41 43 48 45 44 2c 0a 20 20 53 51  X_ATTACHED,.  SQ
2cc4c 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41  LITE_MAX_LIKE_PA
2cc4d 54 54 45 52 4e 5f 4c 45 4e 47 54 48 2c 0a 20 20  TTERN_LENGTH,.  
2cc4e 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49 41  SQLITE_MAX_VARIA
2cc4f 42 4c 45 5f 4e 55 4d 42 45 52 2c 0a 7d 3b 0a 0a  BLE_NUMBER,.};..
2cc50 2f 2a 0a 2a 2a 20 4d 61 6b 65 20 73 75 72 65 20  /*.** Make sure 
2cc51 74 68 65 20 68 61 72 64 20 6c 69 6d 69 74 73 20  the hard limits 
2cc52 61 72 65 20 73 65 74 20 74 6f 20 72 65 61 73 6f  are set to reaso
2cc53 6e 61 62 6c 65 20 76 61 6c 75 65 73 0a 2a 2f 0a  nable values.*/.
2cc54 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c  #if SQLITE_MAX_L
2cc55 45 4e 47 54 48 3c 31 30 30 0a 23 20 65 72 72 6f  ENGTH<100.# erro
2cc56 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  r SQLITE_MAX_LEN
2cc57 47 54 48 20 6d 75 73 74 20 62 65 20 61 74 20 6c  GTH must be at l
2cc58 65 61 73 74 20 31 30 30 0a 23 65 6e 64 69 66 0a  east 100.#endif.
2cc59 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 53  #if SQLITE_MAX_S
2cc5a 51 4c 5f 4c 45 4e 47 54 48 3c 31 30 30 0a 23 20  QL_LENGTH<100.# 
2cc5b 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58  error SQLITE_MAX
2cc5c 5f 53 51 4c 5f 4c 45 4e 47 54 48 20 6d 75 73 74  _SQL_LENGTH must
2cc5d 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 30 30   be at least 100
2cc5e 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
2cc5f 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
2cc60 48 3e 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 45 4e  H>SQLITE_MAX_LEN
2cc61 47 54 48 0a 23 20 65 72 72 6f 72 20 53 51 4c 49  GTH.# error SQLI
2cc62 54 45 5f 4d 41 58 5f 53 51 4c 5f 4c 45 4e 47 54  TE_MAX_SQL_LENGT
2cc63 48 20 6d 75 73 74 20 6e 6f 74 20 62 65 20 67 72  H must not be gr
2cc64 65 61 74 65 72 20 74 68 61 6e 20 53 51 4c 49 54  eater than SQLIT
2cc65 45 5f 4d 41 58 5f 4c 45 4e 47 54 48 0a 23 65 6e  E_MAX_LENGTH.#en
2cc66 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d  dif.#if SQLITE_M
2cc67 41 58 5f 43 4f 4d 50 4f 55 4e 44 5f 53 45 4c 45  AX_COMPOUND_SELE
2cc68 43 54 3c 32 0a 23 20 65 72 72 6f 72 20 53 51 4c  CT<2.# error SQL
2cc69 49 54 45 5f 4d 41 58 5f 43 4f 4d 50 4f 55 4e 44  ITE_MAX_COMPOUND
2cc6a 5f 53 45 4c 45 43 54 20 6d 75 73 74 20 62 65 20  _SELECT must be 
2cc6b 61 74 20 6c 65 61 73 74 20 32 0a 23 65 6e 64 69  at least 2.#endi
2cc6c 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
2cc6d 5f 56 44 42 45 5f 4f 50 3c 34 30 0a 23 20 65 72  _VDBE_OP<40.# er
2cc6e 72 6f 72 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56  ror SQLITE_MAX_V
2cc6f 44 42 45 5f 4f 50 20 6d 75 73 74 20 62 65 20 61  DBE_OP must be a
2cc70 74 20 6c 65 61 73 74 20 34 30 0a 23 65 6e 64 69  t least 40.#endi
2cc71 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
2cc72 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 3c 30 20  _FUNCTION_ARG<0 
2cc73 7c 7c 20 53 51 4c 49 54 45 5f 4d 41 58 5f 46 55  || SQLITE_MAX_FU
2cc74 4e 43 54 49 4f 4e 5f 41 52 47 3e 31 30 30 30 0a  NCTION_ARG>1000.
2cc75 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45 5f 4d  # error SQLITE_M
2cc76 41 58 5f 46 55 4e 43 54 49 4f 4e 5f 41 52 47 20  AX_FUNCTION_ARG 
2cc77 6d 75 73 74 20 62 65 20 62 65 74 77 65 65 6e 20  must be between 
2cc78 30 20 61 6e 64 20 31 30 30 30 0a 23 65 6e 64 69  0 and 1000.#endi
2cc79 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
2cc7a 5f 41 54 54 41 43 48 45 44 3c 30 20 7c 7c 20 53  _ATTACHED<0 || S
2cc7b 51 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48  QLITE_MAX_ATTACH
2cc7c 45 44 3e 33 30 0a 23 20 65 72 72 6f 72 20 53 51  ED>30.# error SQ
2cc7d 4c 49 54 45 5f 4d 41 58 5f 41 54 54 41 43 48 45  LITE_MAX_ATTACHE
2cc7e 44 20 6d 75 73 74 20 62 65 20 62 65 74 77 65 65  D must be betwee
2cc7f 6e 20 30 20 61 6e 64 20 33 30 0a 23 65 6e 64 69  n 0 and 30.#endi
2cc80 66 0a 23 69 66 20 53 51 4c 49 54 45 5f 4d 41 58  f.#if SQLITE_MAX
2cc81 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
2cc82 4e 47 54 48 3c 31 0a 23 20 65 72 72 6f 72 20 53  NGTH<1.# error S
2cc83 51 4c 49 54 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50  QLITE_MAX_LIKE_P
2cc84 41 54 54 45 52 4e 5f 4c 45 4e 47 54 48 20 6d 75  ATTERN_LENGTH mu
2cc85 73 74 20 62 65 20 61 74 20 6c 65 61 73 74 20 31  st be at least 1
2cc86 0a 23 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49  .#endif.#if SQLI
2cc87 54 45 5f 4d 41 58 5f 56 41 52 49 41 42 4c 45 5f  TE_MAX_VARIABLE_
2cc88 4e 55 4d 42 45 52 3c 31 0a 23 20 65 72 72 6f 72  NUMBER<1.# error
2cc89 20 53 51 4c 49 54 45 5f 4d 41 58 5f 56 41 52 49   SQLITE_MAX_VARI
2cc8a 41 42 4c 45 5f 4e 55 4d 42 45 52 20 6d 75 73 74  ABLE_NUMBER must
2cc8b 20 62 65 20 61 74 20 6c 65 61 73 74 20 31 0a 23   be at least 1.#
2cc8c 65 6e 64 69 66 0a 23 69 66 20 53 51 4c 49 54 45  endif.#if SQLITE
2cc8d 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 3e 33 32 37 36  _MAX_COLUMN>3276
2cc8e 37 0a 23 20 65 72 72 6f 72 20 53 51 4c 49 54 45  7.# error SQLITE
2cc8f 5f 4d 41 58 5f 43 4f 4c 55 4d 4e 20 6d 75 73 74  _MAX_COLUMN must
2cc90 20 6e 6f 74 20 65 78 63 65 65 64 20 33 32 37 36   not exceed 3276
2cc91 37 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a 2a  7.#endif.../*.**
2cc92 20 43 68 61 6e 67 65 20 74 68 65 20 76 61 6c 75   Change the valu
2cc93 65 20 6f 66 20 61 20 6c 69 6d 69 74 2e 20 20 52  e of a limit.  R
2cc94 65 70 6f 72 74 20 74 68 65 20 6f 6c 64 20 76 61  eport the old va
2cc95 6c 75 65 2e 0a 2a 2a 20 49 66 20 61 6e 20 69 6e  lue..** If an in
2cc96 76 61 6c 69 64 20 6c 69 6d 69 74 20 69 6e 64 65  valid limit inde
2cc97 78 20 69 73 20 73 75 70 70 6c 69 65 64 2c 20 72  x is supplied, r
2cc98 65 70 6f 72 74 20 2d 31 2e 0a 2a 2a 20 4d 61 6b  eport -1..** Mak
2cc99 65 20 6e 6f 20 63 68 61 6e 67 65 73 20 62 75 74  e no changes but
2cc9a 20 73 74 69 6c 6c 20 72 65 70 6f 72 74 20 74 68   still report th
2cc9b 65 20 6f 6c 64 20 76 61 6c 75 65 20 69 66 20 74  e old value if t
2cc9c 68 65 0a 2a 2a 20 6e 65 77 20 6c 69 6d 69 74 20  he.** new limit 
2cc9d 69 73 20 6e 65 67 61 74 69 76 65 2e 0a 2a 2a 0a  is negative..**.
2cc9e 2a 2a 20 41 20 6e 65 77 20 6c 6f 77 65 72 20 6c  ** A new lower l
2cc9f 69 6d 69 74 20 64 6f 65 73 20 6e 6f 74 20 73 68  imit does not sh
2cca0 72 69 6e 6b 20 65 78 69 73 74 69 6e 67 20 63 6f  rink existing co
2cca1 6e 73 74 72 75 63 74 73 2e 0a 2a 2a 20 49 74 20  nstructs..** It 
2cca2 6d 65 72 65 6c 79 20 70 72 65 76 65 6e 74 73 20  merely prevents 
2cca3 6e 65 77 20 63 6f 6e 73 74 72 75 63 74 73 20 74  new constructs t
2cca4 68 61 74 20 65 78 63 65 65 64 20 74 68 65 20 6c  hat exceed the l
2cca5 69 6d 69 74 0a 2a 2a 20 66 72 6f 6d 20 66 6f 72  imit.** from for
2cca6 6d 69 6e 67 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ming..*/.SQLITE_
2cca7 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2cca8 6c 69 6d 69 74 28 73 71 6c 69 74 65 33 20 2a 64  limit(sqlite3 *d
2cca9 62 2c 20 69 6e 74 20 6c 69 6d 69 74 49 64 2c 20  b, int limitId, 
2ccaa 69 6e 74 20 6e 65 77 4c 69 6d 69 74 29 7b 0a 20  int newLimit){. 
2ccab 20 69 6e 74 20 6f 6c 64 4c 69 6d 69 74 3b 0a 20   int oldLimit;. 
2ccac 20 69 66 28 20 6c 69 6d 69 74 49 64 3c 30 20 7c   if( limitId<0 |
2ccad 7c 20 6c 69 6d 69 74 49 64 3e 3d 53 51 4c 49 54  | limitId>=SQLIT
2ccae 45 5f 4e 5f 4c 49 4d 49 54 20 29 7b 0a 20 20 20  E_N_LIMIT ){.   
2ccaf 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 7d 0a   return -1;.  }.
2ccb0 20 20 6f 6c 64 4c 69 6d 69 74 20 3d 20 64 62 2d    oldLimit = db-
2ccb1 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49 64 5d  >aLimit[limitId]
2ccb2 3b 0a 20 20 69 66 28 20 6e 65 77 4c 69 6d 69 74  ;.  if( newLimit
2ccb3 3e 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20 6e  >=0 ){.    if( n
2ccb4 65 77 4c 69 6d 69 74 3e 61 48 61 72 64 4c 69 6d  ewLimit>aHardLim
2ccb5 69 74 5b 6c 69 6d 69 74 49 64 5d 20 29 7b 0a 20  it[limitId] ){. 
2ccb6 20 20 20 20 20 6e 65 77 4c 69 6d 69 74 20 3d 20       newLimit = 
2ccb7 61 48 61 72 64 4c 69 6d 69 74 5b 6c 69 6d 69 74  aHardLimit[limit
2ccb8 49 64 5d 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64  Id];.    }.    d
2ccb9 62 2d 3e 61 4c 69 6d 69 74 5b 6c 69 6d 69 74 49  b->aLimit[limitI
2ccba 64 5d 20 3d 20 6e 65 77 4c 69 6d 69 74 3b 0a 20  d] = newLimit;. 
2ccbb 20 7d 0a 20 20 72 65 74 75 72 6e 20 6f 6c 64 4c   }.  return oldL
2ccbc 69 6d 69 74 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  imit;.}../*.** T
2ccbd 68 69 73 20 72 6f 75 74 69 6e 65 20 64 6f 65 73  his routine does
2ccbe 20 74 68 65 20 77 6f 72 6b 20 6f 66 20 6f 70 65   the work of ope
2ccbf 6e 69 6e 67 20 61 20 64 61 74 61 62 61 73 65 20  ning a database 
2ccc0 6f 6e 20 62 65 68 61 6c 66 20 6f 66 0a 2a 2a 20  on behalf of.** 
2ccc1 73 71 6c 69 74 65 33 5f 6f 70 65 6e 28 29 20 61  sqlite3_open() a
2ccc2 6e 64 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e 31  nd sqlite3_open1
2ccc3 36 28 29 2e 20 54 68 65 20 64 61 74 61 62 61 73  6(). The databas
2ccc4 65 20 66 69 6c 65 6e 61 6d 65 20 22 7a 46 69 6c  e filename "zFil
2ccc5 65 6e 61 6d 65 22 20 20 0a 2a 2a 20 69 73 20 55  ename"  .** is U
2ccc6 54 46 2d 38 20 65 6e 63 6f 64 65 64 2e 0a 2a 2f  TF-8 encoded..*/
2ccc7 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70 65 6e  .static int open
2ccc8 44 61 74 61 62 61 73 65 28 0a 20 20 63 6f 6e 73  Database(.  cons
2ccc9 74 20 63 68 61 72 20 2a 7a 46 69 6c 65 6e 61 6d  t char *zFilenam
2ccca 65 2c 20 2f 2a 20 44 61 74 61 62 61 73 65 20 66  e, /* Database f
2cccb 69 6c 65 6e 61 6d 65 20 55 54 46 2d 38 20 65 6e  ilename UTF-8 en
2cccc 63 6f 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  coded */.  sqlit
2cccd 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20 20  e3 **ppDb,      
2ccce 20 20 2f 2a 20 4f 55 54 3a 20 52 65 74 75 72 6e    /* OUT: Return
2cccf 65 64 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  ed database hand
2ccd0 6c 65 20 2a 2f 0a 20 20 75 6e 73 69 67 6e 65 64  le */.  unsigned
2ccd1 20 66 6c 61 67 73 2c 20 20 20 20 20 20 20 20 2f   flags,        /
2ccd2 2a 20 4f 70 65 72 61 74 69 6f 6e 61 6c 20 66 6c  * Operational fl
2ccd3 61 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  ags */.  const c
2ccd4 68 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20  har *zVfs       
2ccd5 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 56  /* Name of the V
2ccd6 46 53 20 74 6f 20 75 73 65 20 2a 2f 0a 29 7b 0a  FS to use */.){.
2ccd7 20 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 0a 20    sqlite3 *db;. 
2ccd8 20 69 6e 74 20 72 63 3b 0a 20 20 43 6f 6c 6c 53   int rc;.  CollS
2ccd9 65 71 20 2a 70 43 6f 6c 6c 3b 0a 20 20 69 6e 74  eq *pColl;.  int
2ccda 20 69 73 54 68 72 65 61 64 73 61 66 65 3b 0a 0a   isThreadsafe;..
2ccdb 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f  #ifndef SQLITE_O
2ccdc 4d 49 54 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72  MIT_AUTOINIT.  r
2ccdd 63 20 3d 20 73 71 6c 69 74 65 33 5f 69 6e 69 74  c = sqlite3_init
2ccde 69 61 6c 69 7a 65 28 29 3b 0a 20 20 69 66 28 20  ialize();.  if( 
2ccdf 72 63 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  rc ) return rc;.
2cce0 23 65 6e 64 69 66 0a 0a 20 20 69 66 28 20 73 71  #endif..  if( sq
2cce1 6c 69 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69  lite3GlobalConfi
2cce2 67 2e 62 43 6f 72 65 4d 75 74 65 78 3d 3d 30 20  g.bCoreMutex==0 
2cce3 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64 73  ){.    isThreads
2cce4 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  afe = 0;.  }else
2cce5 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51 4c   if( flags & SQL
2cce6 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45 58  ITE_OPEN_NOMUTEX
2cce7 20 29 7b 0a 20 20 20 20 69 73 54 68 72 65 61 64   ){.    isThread
2cce8 73 61 66 65 20 3d 20 30 3b 0a 20 20 7d 65 6c 73  safe = 0;.  }els
2cce9 65 20 69 66 28 20 66 6c 61 67 73 20 26 20 53 51  e if( flags & SQ
2ccea 4c 49 54 45 5f 4f 50 45 4e 5f 46 55 4c 4c 4d 55  LITE_OPEN_FULLMU
2cceb 54 45 58 20 29 7b 0a 20 20 20 20 69 73 54 68 72  TEX ){.    isThr
2ccec 65 61 64 73 61 66 65 20 3d 20 31 3b 0a 20 20 7d  eadsafe = 1;.  }
2cced 65 6c 73 65 7b 0a 20 20 20 20 69 73 54 68 72 65  else{.    isThre
2ccee 61 64 73 61 66 65 20 3d 20 73 71 6c 69 74 65 33  adsafe = sqlite3
2ccef 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e 62 46 75  GlobalConfig.bFu
2ccf0 6c 6c 4d 75 74 65 78 3b 0a 20 20 7d 0a 0a 20 20  llMutex;.  }..  
2ccf1 2f 2a 20 52 65 6d 6f 76 65 20 68 61 72 6d 66 75  /* Remove harmfu
2ccf2 6c 20 62 69 74 73 20 66 72 6f 6d 20 74 68 65 20  l bits from the 
2ccf3 66 6c 61 67 73 20 70 61 72 61 6d 65 74 65 72 20  flags parameter 
2ccf4 2a 2f 0a 20 20 66 6c 61 67 73 20 26 3d 20 20 7e  */.  flags &=  ~
2ccf5 28 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 44 45  ( SQLITE_OPEN_DE
2ccf6 4c 45 54 45 4f 4e 43 4c 4f 53 45 20 7c 0a 20 20  LETEONCLOSE |.  
2ccf7 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
2ccf8 49 54 45 5f 4f 50 45 4e 5f 4d 41 49 4e 5f 44 42  ITE_OPEN_MAIN_DB
2ccf9 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20   |.             
2ccfa 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 54 45    SQLITE_OPEN_TE
2ccfb 4d 50 5f 44 42 20 7c 20 0a 20 20 20 20 20 20 20  MP_DB | .       
2ccfc 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 4f          SQLITE_O
2ccfd 50 45 4e 5f 54 52 41 4e 53 49 45 4e 54 5f 44 42  PEN_TRANSIENT_DB
2ccfe 20 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20   | .            
2ccff 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d     SQLITE_OPEN_M
2cd00 41 49 4e 5f 4a 4f 55 52 4e 41 4c 20 7c 20 0a 20  AIN_JOURNAL | . 
2cd01 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
2cd02 4c 49 54 45 5f 4f 50 45 4e 5f 54 45 4d 50 5f 4a  LITE_OPEN_TEMP_J
2cd03 4f 55 52 4e 41 4c 20 7c 20 0a 20 20 20 20 20 20  OURNAL | .      
2cd04 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
2cd05 4f 50 45 4e 5f 53 55 42 4a 4f 55 52 4e 41 4c 20  OPEN_SUBJOURNAL 
2cd06 7c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  | .             
2cd07 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 4d 41    SQLITE_OPEN_MA
2cd08 53 54 45 52 5f 4a 4f 55 52 4e 41 4c 20 7c 0a 20  STER_JOURNAL |. 
2cd09 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
2cd0a 4c 49 54 45 5f 4f 50 45 4e 5f 4e 4f 4d 55 54 45  LITE_OPEN_NOMUTE
2cd0b 58 20 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20  X |.            
2cd0c 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e 5f 46     SQLITE_OPEN_F
2cd0d 55 4c 4c 4d 55 54 45 58 0a 20 20 20 20 20 20 20  ULLMUTEX.       
2cd0e 20 20 20 20 20 20 29 3b 0a 0a 20 20 2f 2a 20 41        );..  /* A
2cd0f 6c 6c 6f 63 61 74 65 20 74 68 65 20 73 71 6c 69  llocate the sqli
2cd10 74 65 20 64 61 74 61 20 73 74 72 75 63 74 75 72  te data structur
2cd11 65 20 2a 2f 0a 20 20 64 62 20 3d 20 73 71 6c 69  e */.  db = sqli
2cd12 74 65 33 4d 61 6c 6c 6f 63 5a 65 72 6f 28 20 73  te3MallocZero( s
2cd13 69 7a 65 6f 66 28 73 71 6c 69 74 65 33 29 20 29  izeof(sqlite3) )
2cd14 3b 0a 20 20 69 66 28 20 64 62 3d 3d 30 20 29 20  ;.  if( db==0 ) 
2cd15 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b  goto opendb_out;
2cd16 0a 20 20 69 66 28 20 69 73 54 68 72 65 61 64 73  .  if( isThreads
2cd17 61 66 65 20 29 7b 0a 20 20 20 20 64 62 2d 3e 6d  afe ){.    db->m
2cd18 75 74 65 78 20 3d 20 73 71 6c 69 74 65 33 4d 75  utex = sqlite3Mu
2cd19 74 65 78 41 6c 6c 6f 63 28 53 51 4c 49 54 45 5f  texAlloc(SQLITE_
2cd1a 4d 55 54 45 58 5f 52 45 43 55 52 53 49 56 45 29  MUTEX_RECURSIVE)
2cd1b 3b 0a 20 20 20 20 69 66 28 20 64 62 2d 3e 6d 75  ;.    if( db->mu
2cd1c 74 65 78 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20  tex==0 ){.      
2cd1d 73 71 6c 69 74 65 33 5f 66 72 65 65 28 64 62 29  sqlite3_free(db)
2cd1e 3b 0a 20 20 20 20 20 20 64 62 20 3d 20 30 3b 0a  ;.      db = 0;.
2cd1f 20 20 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64        goto opend
2cd20 62 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d  b_out;.    }.  }
2cd21 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2cd22 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2cd23 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b  );.  db->errMask
2cd24 20 3d 20 30 78 66 66 3b 0a 20 20 64 62 2d 3e 70   = 0xff;.  db->p
2cd25 72 69 6f 72 4e 65 77 52 6f 77 69 64 20 3d 20 30  riorNewRowid = 0
2cd26 3b 0a 20 20 64 62 2d 3e 6e 44 62 20 3d 20 32 3b  ;.  db->nDb = 2;
2cd27 0a 20 20 64 62 2d 3e 6d 61 67 69 63 20 3d 20 53  .  db->magic = S
2cd28 51 4c 49 54 45 5f 4d 41 47 49 43 5f 42 55 53 59  QLITE_MAGIC_BUSY
2cd29 3b 0a 20 20 64 62 2d 3e 61 44 62 20 3d 20 64 62  ;.  db->aDb = db
2cd2a 2d 3e 61 44 62 53 74 61 74 69 63 3b 0a 0a 20 20  ->aDbStatic;..  
2cd2b 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 64  assert( sizeof(d
2cd2c 62 2d 3e 61 4c 69 6d 69 74 29 3d 3d 73 69 7a 65  b->aLimit)==size
2cd2d 6f 66 28 61 48 61 72 64 4c 69 6d 69 74 29 20 29  of(aHardLimit) )
2cd2e 3b 0a 20 20 6d 65 6d 63 70 79 28 64 62 2d 3e 61  ;.  memcpy(db->a
2cd2f 4c 69 6d 69 74 2c 20 61 48 61 72 64 4c 69 6d 69  Limit, aHardLimi
2cd30 74 2c 20 73 69 7a 65 6f 66 28 64 62 2d 3e 61 4c  t, sizeof(db->aL
2cd31 69 6d 69 74 29 29 3b 0a 20 20 64 62 2d 3e 61 75  imit));.  db->au
2cd32 74 6f 43 6f 6d 6d 69 74 20 3d 20 31 3b 0a 20 20  toCommit = 1;.  
2cd33 64 62 2d 3e 6e 65 78 74 41 75 74 6f 76 61 63 20  db->nextAutovac 
2cd34 3d 20 2d 31 3b 0a 20 20 64 62 2d 3e 6e 65 78 74  = -1;.  db->next
2cd35 50 61 67 65 73 69 7a 65 20 3d 20 30 3b 0a 20 20  Pagesize = 0;.  
2cd36 64 62 2d 3e 66 6c 61 67 73 20 7c 3d 20 53 51 4c  db->flags |= SQL
2cd37 49 54 45 5f 53 68 6f 72 74 43 6f 6c 4e 61 6d 65  ITE_ShortColName
2cd38 73 0a 23 69 66 20 53 51 4c 49 54 45 5f 44 45 46  s.#if SQLITE_DEF
2cd39 41 55 4c 54 5f 46 49 4c 45 5f 46 4f 52 4d 41 54  AULT_FILE_FORMAT
2cd3a 3c 34 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  <4.             
2cd3b 20 20 20 20 7c 20 53 51 4c 49 54 45 5f 4c 65 67      | SQLITE_Leg
2cd3c 61 63 79 46 69 6c 65 46 6d 74 0a 23 65 6e 64 69  acyFileFmt.#endi
2cd3d 66 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  f.#ifdef SQLITE_
2cd3e 45 4e 41 42 4c 45 5f 4c 4f 41 44 5f 45 58 54 45  ENABLE_LOAD_EXTE
2cd3f 4e 53 49 4f 4e 0a 20 20 20 20 20 20 20 20 20 20  NSION.          
2cd40 20 20 20 20 20 20 20 7c 20 53 51 4c 49 54 45 5f         | SQLITE_
2cd41 4c 6f 61 64 45 78 74 65 6e 73 69 6f 6e 0a 23 65  LoadExtension.#e
2cd42 6e 64 69 66 0a 20 20 20 20 20 20 3b 0a 20 20 73  ndif.      ;.  s
2cd43 71 6c 69 74 65 33 48 61 73 68 49 6e 69 74 28 26  qlite3HashInit(&
2cd44 64 62 2d 3e 61 43 6f 6c 6c 53 65 71 2c 20 30 29  db->aCollSeq, 0)
2cd45 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ;.#ifndef SQLITE
2cd46 5f 4f 4d 49 54 5f 56 49 52 54 55 41 4c 54 41 42  _OMIT_VIRTUALTAB
2cd47 4c 45 0a 20 20 73 71 6c 69 74 65 33 48 61 73 68  LE.  sqlite3Hash
2cd48 49 6e 69 74 28 26 64 62 2d 3e 61 4d 6f 64 75 6c  Init(&db->aModul
2cd49 65 2c 20 30 29 3b 0a 23 65 6e 64 69 66 0a 0a 20  e, 0);.#endif.. 
2cd4a 20 64 62 2d 3e 70 56 66 73 20 3d 20 73 71 6c 69   db->pVfs = sqli
2cd4b 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 7a 56 66  te3_vfs_find(zVf
2cd4c 73 29 3b 0a 20 20 69 66 28 20 21 64 62 2d 3e 70  s);.  if( !db->p
2cd4d 56 66 73 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  Vfs ){.    rc = 
2cd4e 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2cd4f 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28 64    sqlite3Error(d
2cd50 62 2c 20 72 63 2c 20 22 6e 6f 20 73 75 63 68 20  b, rc, "no such 
2cd51 76 66 73 3a 20 25 73 22 2c 20 7a 56 66 73 29 3b  vfs: %s", zVfs);
2cd52 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
2cd53 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _out;.  }..  /* 
2cd54 41 64 64 20 74 68 65 20 64 65 66 61 75 6c 74 20  Add the default 
2cd55 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
2cd56 63 65 20 42 49 4e 41 52 59 2e 20 42 49 4e 41 52  ce BINARY. BINAR
2cd57 59 20 77 6f 72 6b 73 20 66 6f 72 20 62 6f 74 68  Y works for both
2cd58 20 55 54 46 2d 38 0a 20 20 2a 2a 20 61 6e 64 20   UTF-8.  ** and 
2cd59 55 54 46 2d 31 36 2c 20 73 6f 20 61 64 64 20 61  UTF-16, so add a
2cd5a 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 65 61 63   version for eac
2cd5b 68 20 74 6f 20 61 76 6f 69 64 20 61 6e 79 20 75  h to avoid any u
2cd5c 6e 6e 65 63 65 73 73 61 72 79 0a 20 20 2a 2a 20  nnecessary.  ** 
2cd5d 63 6f 6e 76 65 72 73 69 6f 6e 73 2e 20 54 68 65  conversions. The
2cd5e 20 6f 6e 6c 79 20 65 72 72 6f 72 20 74 68 61 74   only error that
2cd5f 20 63 61 6e 20 6f 63 63 75 72 20 68 65 72 65 20   can occur here 
2cd60 69 73 20 61 20 6d 61 6c 6c 6f 63 28 29 20 66 61  is a malloc() fa
2cd61 69 6c 75 72 65 2e 0a 20 20 2a 2f 0a 20 20 63 72  ilure..  */.  cr
2cd62 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62  eateCollation(db
2cd63 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c 49  , "BINARY", SQLI
2cd64 54 45 5f 55 54 46 38 2c 20 30 2c 20 62 69 6e 43  TE_UTF8, 0, binC
2cd65 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 63  ollFunc, 0);.  c
2cd66 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64  reateCollation(d
2cd67 62 2c 20 22 42 49 4e 41 52 59 22 2c 20 53 51 4c  b, "BINARY", SQL
2cd68 49 54 45 5f 55 54 46 31 36 42 45 2c 20 30 2c 20  ITE_UTF16BE, 0, 
2cd69 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c 20 30 29 3b  binCollFunc, 0);
2cd6a 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61 74 69  .  createCollati
2cd6b 6f 6e 28 64 62 2c 20 22 42 49 4e 41 52 59 22 2c  on(db, "BINARY",
2cd6c 20 53 51 4c 49 54 45 5f 55 54 46 31 36 4c 45 2c   SQLITE_UTF16LE,
2cd6d 20 30 2c 20 62 69 6e 43 6f 6c 6c 46 75 6e 63 2c   0, binCollFunc,
2cd6e 20 30 29 3b 0a 20 20 63 72 65 61 74 65 43 6f 6c   0);.  createCol
2cd6f 6c 61 74 69 6f 6e 28 64 62 2c 20 22 52 54 52 49  lation(db, "RTRI
2cd70 4d 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c  M", SQLITE_UTF8,
2cd71 20 28 76 6f 69 64 2a 29 31 2c 20 62 69 6e 43 6f   (void*)1, binCo
2cd72 6c 6c 46 75 6e 63 2c 20 30 29 3b 0a 20 20 69 66  llFunc, 0);.  if
2cd73 28 20 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c  ( db->mallocFail
2cd74 65 64 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20 6f  ed ){.    goto o
2cd75 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20 7d 0a 20  pendb_out;.  }. 
2cd76 20 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 20 3d   db->pDfltColl =
2cd77 20 73 71 6c 69 74 65 33 46 69 6e 64 43 6f 6c 6c   sqlite3FindColl
2cd78 53 65 71 28 64 62 2c 20 53 51 4c 49 54 45 5f 55  Seq(db, SQLITE_U
2cd79 54 46 38 2c 20 22 42 49 4e 41 52 59 22 2c 20 36  TF8, "BINARY", 6
2cd7a 2c 20 30 29 3b 0a 20 20 61 73 73 65 72 74 28 20  , 0);.  assert( 
2cd7b 64 62 2d 3e 70 44 66 6c 74 43 6f 6c 6c 21 3d 30  db->pDfltColl!=0
2cd7c 20 29 3b 0a 0a 20 20 2f 2a 20 41 6c 73 6f 20 61   );..  /* Also a
2cd7d 64 64 20 61 20 55 54 46 2d 38 20 63 61 73 65 2d  dd a UTF-8 case-
2cd7e 69 6e 73 65 6e 73 69 74 69 76 65 20 63 6f 6c 6c  insensitive coll
2cd7f 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 2e 20  ation sequence. 
2cd80 2a 2f 0a 20 20 63 72 65 61 74 65 43 6f 6c 6c 61  */.  createColla
2cd81 74 69 6f 6e 28 64 62 2c 20 22 4e 4f 43 41 53 45  tion(db, "NOCASE
2cd82 22 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20  ", SQLITE_UTF8, 
2cd83 30 2c 20 6e 6f 63 61 73 65 43 6f 6c 6c 61 74 69  0, nocaseCollati
2cd84 6e 67 46 75 6e 63 2c 20 30 29 3b 0a 0a 20 20 2f  ngFunc, 0);..  /
2cd85 2a 20 53 65 74 20 66 6c 61 67 73 20 6f 6e 20 74  * Set flags on t
2cd86 68 65 20 62 75 69 6c 74 2d 69 6e 20 63 6f 6c 6c  he built-in coll
2cd87 61 74 69 6e 67 20 73 65 71 75 65 6e 63 65 73 20  ating sequences 
2cd88 2a 2f 0a 20 20 64 62 2d 3e 70 44 66 6c 74 43 6f  */.  db->pDfltCo
2cd89 6c 6c 2d 3e 74 79 70 65 20 3d 20 53 51 4c 49 54  ll->type = SQLIT
2cd8a 45 5f 43 4f 4c 4c 5f 42 49 4e 41 52 59 3b 0a 20  E_COLL_BINARY;. 
2cd8b 20 70 43 6f 6c 6c 20 3d 20 73 71 6c 69 74 65 33   pColl = sqlite3
2cd8c 46 69 6e 64 43 6f 6c 6c 53 65 71 28 64 62 2c 20  FindCollSeq(db, 
2cd8d 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 22 4e 4f  SQLITE_UTF8, "NO
2cd8e 43 41 53 45 22 2c 20 36 2c 20 30 29 3b 0a 20 20  CASE", 6, 0);.  
2cd8f 69 66 28 20 70 43 6f 6c 6c 20 29 7b 0a 20 20 20  if( pColl ){.   
2cd90 20 70 43 6f 6c 6c 2d 3e 74 79 70 65 20 3d 20 53   pColl->type = S
2cd91 51 4c 49 54 45 5f 43 4f 4c 4c 5f 4e 4f 43 41 53  QLITE_COLL_NOCAS
2cd92 45 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4f 70 65  E;.  }..  /* Ope
2cd93 6e 20 74 68 65 20 62 61 63 6b 65 6e 64 20 64 61  n the backend da
2cd94 74 61 62 61 73 65 20 64 72 69 76 65 72 20 2a 2f  tabase driver */
2cd95 0a 20 20 64 62 2d 3e 6f 70 65 6e 46 6c 61 67 73  .  db->openFlags
2cd96 20 3d 20 66 6c 61 67 73 3b 0a 20 20 72 63 20 3d   = flags;.  rc =
2cd97 20 73 71 6c 69 74 65 33 42 74 72 65 65 46 61 63   sqlite3BtreeFac
2cd98 74 6f 72 79 28 64 62 2c 20 7a 46 69 6c 65 6e 61  tory(db, zFilena
2cd99 6d 65 2c 20 30 2c 20 53 51 4c 49 54 45 5f 44 45  me, 0, SQLITE_DE
2cd9a 46 41 55 4c 54 5f 43 41 43 48 45 5f 53 49 5a 45  FAULT_CACHE_SIZE
2cd9b 2c 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  , .             
2cd9c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66 6c                fl
2cd9d 61 67 73 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ags | SQLITE_OPE
2cd9e 4e 5f 4d 41 49 4e 5f 44 42 2c 0a 20 20 20 20 20  N_MAIN_DB,.     
2cd9f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2cda0 20 20 20 20 20 20 26 64 62 2d 3e 61 44 62 5b 30        &db->aDb[0
2cda1 5d 2e 70 42 74 29 3b 0a 20 20 69 66 28 20 72 63  ].pBt);.  if( rc
2cda2 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
2cda3 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2cda4 45 5f 49 4f 45 52 52 5f 4e 4f 4d 45 4d 20 29 7b  E_IOERR_NOMEM ){
2cda5 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
2cda6 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
2cda7 20 20 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72      sqlite3Error
2cda8 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a 20 20 20  (db, rc, 0);.   
2cda9 20 67 6f 74 6f 20 6f 70 65 6e 64 62 5f 6f 75 74   goto opendb_out
2cdaa 3b 0a 20 20 7d 0a 20 20 64 62 2d 3e 61 44 62 5b  ;.  }.  db->aDb[
2cdab 30 5d 2e 70 53 63 68 65 6d 61 20 3d 20 73 71 6c  0].pSchema = sql
2cdac 69 74 65 33 53 63 68 65 6d 61 47 65 74 28 64 62  ite3SchemaGet(db
2cdad 2c 20 64 62 2d 3e 61 44 62 5b 30 5d 2e 70 42 74  , db->aDb[0].pBt
2cdae 29 3b 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e  );.  db->aDb[1].
2cdaf 70 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65  pSchema = sqlite
2cdb0 33 53 63 68 65 6d 61 47 65 74 28 64 62 2c 20 30  3SchemaGet(db, 0
2cdb1 29 3b 0a 0a 0a 20 20 2f 2a 20 54 68 65 20 64 65  );...  /* The de
2cdb2 66 61 75 6c 74 20 73 61 66 65 74 79 5f 6c 65 76  fault safety_lev
2cdb3 65 6c 20 66 6f 72 20 74 68 65 20 6d 61 69 6e 20  el for the main 
2cdb4 64 61 74 61 62 61 73 65 20 69 73 20 27 66 75 6c  database is 'ful
2cdb5 6c 27 3b 20 66 6f 72 20 74 68 65 20 74 65 6d 70  l'; for the temp
2cdb6 0a 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 69  .  ** database i
2cdb7 74 20 69 73 20 27 4e 4f 4e 45 27 2e 20 54 68 69  t is 'NONE'. Thi
2cdb8 73 20 6d 61 74 63 68 65 73 20 74 68 65 20 70 61  s matches the pa
2cdb9 67 65 72 20 6c 61 79 65 72 20 64 65 66 61 75 6c  ger layer defaul
2cdba 74 73 2e 20 20 0a 20 20 2a 2f 0a 20 20 64 62 2d  ts.  .  */.  db-
2cdbb 3e 61 44 62 5b 30 5d 2e 7a 4e 61 6d 65 20 3d 20  >aDb[0].zName = 
2cdbc 22 6d 61 69 6e 22 3b 0a 20 20 64 62 2d 3e 61 44  "main";.  db->aD
2cdbd 62 5b 30 5d 2e 73 61 66 65 74 79 5f 6c 65 76 65  b[0].safety_leve
2cdbe 6c 20 3d 20 33 3b 0a 23 69 66 6e 64 65 66 20 53  l = 3;.#ifndef S
2cdbf 51 4c 49 54 45 5f 4f 4d 49 54 5f 54 45 4d 50 44  QLITE_OMIT_TEMPD
2cdc0 42 0a 20 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 7a  B.  db->aDb[1].z
2cdc1 4e 61 6d 65 20 3d 20 22 74 65 6d 70 22 3b 0a 20  Name = "temp";. 
2cdc2 20 64 62 2d 3e 61 44 62 5b 31 5d 2e 73 61 66 65   db->aDb[1].safe
2cdc3 74 79 5f 6c 65 76 65 6c 20 3d 20 31 3b 0a 23 65  ty_level = 1;.#e
2cdc4 6e 64 69 66 0a 0a 20 20 64 62 2d 3e 6d 61 67 69  ndif..  db->magi
2cdc5 63 20 3d 20 53 51 4c 49 54 45 5f 4d 41 47 49 43  c = SQLITE_MAGIC
2cdc6 5f 4f 50 45 4e 3b 0a 20 20 69 66 28 20 64 62 2d  _OPEN;.  if( db-
2cdc7 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29 7b  >mallocFailed ){
2cdc8 0a 20 20 20 20 67 6f 74 6f 20 6f 70 65 6e 64 62  .    goto opendb
2cdc9 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  _out;.  }..  /* 
2cdca 52 65 67 69 73 74 65 72 20 61 6c 6c 20 62 75 69  Register all bui
2cdcb 6c 74 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 73 2c  lt-in functions,
2cdcc 20 62 75 74 20 64 6f 20 6e 6f 74 20 61 74 74 65   but do not atte
2cdcd 6d 70 74 20 74 6f 20 72 65 61 64 20 74 68 65 0a  mpt to read the.
2cdce 20 20 2a 2a 20 64 61 74 61 62 61 73 65 20 73 63    ** database sc
2cdcf 68 65 6d 61 20 79 65 74 2e 20 54 68 69 73 20 69  hema yet. This i
2cdd0 73 20 64 65 6c 61 79 65 64 20 75 6e 74 69 6c 20  s delayed until 
2cdd1 74 68 65 20 66 69 72 73 74 20 74 69 6d 65 20 74  the first time t
2cdd2 68 65 20 64 61 74 61 62 61 73 65 0a 20 20 2a 2a  he database.  **
2cdd3 20 69 73 20 61 63 63 65 73 73 65 64 2e 0a 20 20   is accessed..  
2cdd4 2a 2f 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f  */.  sqlite3Erro
2cdd5 72 28 64 62 2c 20 53 51 4c 49 54 45 5f 4f 4b 2c  r(db, SQLITE_OK,
2cdd6 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 52 65   0);.  sqlite3Re
2cdd7 67 69 73 74 65 72 42 75 69 6c 74 69 6e 46 75 6e  gisterBuiltinFun
2cdd8 63 74 69 6f 6e 73 28 64 62 29 3b 0a 0a 20 20 2f  ctions(db);..  /
2cdd9 2a 20 4c 6f 61 64 20 61 75 74 6f 6d 61 74 69 63  * Load automatic
2cdda 20 65 78 74 65 6e 73 69 6f 6e 73 20 2d 20 65 78   extensions - ex
2cddb 74 65 6e 73 69 6f 6e 73 20 74 68 61 74 20 68 61  tensions that ha
2cddc 76 65 20 62 65 65 6e 20 72 65 67 69 73 74 65 72  ve been register
2cddd 65 64 0a 20 20 2a 2a 20 75 73 69 6e 67 20 74 68  ed.  ** using th
2cdde 65 20 73 71 6c 69 74 65 33 5f 61 75 74 6f 6d 61  e sqlite3_automa
2cddf 74 69 63 5f 65 78 74 65 6e 73 69 6f 6e 28 29 20  tic_extension() 
2cde0 41 50 49 2e 0a 20 20 2a 2f 0a 20 20 28 76 6f 69  API..  */.  (voi
2cde1 64 29 73 71 6c 69 74 65 33 41 75 74 6f 4c 6f 61  d)sqlite3AutoLoa
2cde2 64 45 78 74 65 6e 73 69 6f 6e 73 28 64 62 29 3b  dExtensions(db);
2cde3 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f 65  .  if( sqlite3_e
2cde4 72 72 63 6f 64 65 28 64 62 29 21 3d 53 51 4c 49  rrcode(db)!=SQLI
2cde5 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 67 6f 74  TE_OK ){.    got
2cde6 6f 20 6f 70 65 6e 64 62 5f 6f 75 74 3b 0a 20 20  o opendb_out;.  
2cde7 7d 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45  }..#ifdef SQLITE
2cde8 5f 45 4e 41 42 4c 45 5f 46 54 53 31 0a 20 20 69  _ENABLE_FTS1.  i
2cde9 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61  f( !db->mallocFa
2cdea 69 6c 65 64 20 29 7b 0a 20 20 20 20 65 78 74 65  iled ){.    exte
2cdeb 72 6e 20 69 6e 74 20 73 71 6c 69 74 65 33 46 74  rn int sqlite3Ft
2cdec 73 31 49 6e 69 74 28 73 71 6c 69 74 65 33 2a 29  s1Init(sqlite3*)
2cded 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  ;.    rc = sqlit
2cdee 65 33 46 74 73 31 49 6e 69 74 28 64 62 29 3b 0a  e3Fts1Init(db);.
2cdef 20 20 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66 64    }.#endif..#ifd
2cdf0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
2cdf1 5f 46 54 53 32 0a 20 20 69 66 28 20 21 64 62 2d  _FTS2.  if( !db-
2cdf2 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 26 26  >mallocFailed &&
2cdf3 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
2cdf4 7b 0a 20 20 20 20 65 78 74 65 72 6e 20 69 6e 74  {.    extern int
2cdf5 20 73 71 6c 69 74 65 33 46 74 73 32 49 6e 69 74   sqlite3Fts2Init
2cdf6 28 73 71 6c 69 74 65 33 2a 29 3b 0a 20 20 20 20  (sqlite3*);.    
2cdf7 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 32  rc = sqlite3Fts2
2cdf8 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65  Init(db);.  }.#e
2cdf9 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
2cdfa 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 0a  ITE_ENABLE_FTS3.
2cdfb 20 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f    if( !db->mallo
2cdfc 63 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53  cFailed && rc==S
2cdfd 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
2cdfe 72 63 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  rc = sqlite3Fts3
2cdff 49 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65  Init(db);.  }.#e
2ce00 6e 64 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c  ndif..#ifdef SQL
2ce01 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 0a 20  ITE_ENABLE_ICU. 
2ce02 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
2ce03 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51  Failed && rc==SQ
2ce04 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72  LITE_OK ){.    r
2ce05 63 20 3d 20 73 71 6c 69 74 65 33 49 63 75 49 6e  c = sqlite3IcuIn
2ce06 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e 64  it(db);.  }.#end
2ce07 69 66 0a 0a 23 69 66 64 65 66 20 53 51 4c 49 54  if..#ifdef SQLIT
2ce08 45 5f 45 4e 41 42 4c 45 5f 52 54 52 45 45 0a 20  E_ENABLE_RTREE. 
2ce09 20 69 66 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63   if( !db->malloc
2ce0a 46 61 69 6c 65 64 20 26 26 20 72 63 3d 3d 53 51  Failed && rc==SQ
2ce0b 4c 49 54 45 5f 4f 4b 29 7b 0a 20 20 20 20 72 63  LITE_OK){.    rc
2ce0c 20 3d 20 73 71 6c 69 74 65 33 52 74 72 65 65 49   = sqlite3RtreeI
2ce0d 6e 69 74 28 64 62 29 3b 0a 20 20 7d 0a 23 65 6e  nit(db);.  }.#en
2ce0e 64 69 66 0a 0a 20 20 73 71 6c 69 74 65 33 45 72  dif..  sqlite3Er
2ce0f 72 6f 72 28 64 62 2c 20 72 63 2c 20 30 29 3b 0a  ror(db, rc, 0);.
2ce10 0a 20 20 2f 2a 20 2d 44 53 51 4c 49 54 45 5f 44  .  /* -DSQLITE_D
2ce11 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d  EFAULT_LOCKING_M
2ce12 4f 44 45 3d 31 20 6d 61 6b 65 73 20 45 58 43 4c  ODE=1 makes EXCL
2ce13 55 53 49 56 45 20 74 68 65 20 64 65 66 61 75 6c  USIVE the defaul
2ce14 74 20 6c 6f 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d  t locking.  ** m
2ce15 6f 64 65 2e 20 20 2d 44 53 51 4c 49 54 45 5f 44  ode.  -DSQLITE_D
2ce16 45 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d  EFAULT_LOCKING_M
2ce17 4f 44 45 3d 30 20 6d 61 6b 65 20 4e 4f 52 4d 41  ODE=0 make NORMA
2ce18 4c 20 74 68 65 20 64 65 66 61 75 6c 74 20 6c 6f  L the default lo
2ce19 63 6b 69 6e 67 0a 20 20 2a 2a 20 6d 6f 64 65 2e  cking.  ** mode.
2ce1a 20 20 44 6f 69 6e 67 20 6e 6f 74 68 69 6e 67 20    Doing nothing 
2ce1b 61 74 20 61 6c 6c 20 61 6c 73 6f 20 6d 61 6b 65  at all also make
2ce1c 73 20 4e 4f 52 4d 41 4c 20 74 68 65 20 64 65 66  s NORMAL the def
2ce1d 61 75 6c 74 2e 0a 20 20 2a 2f 0a 23 69 66 64 65  ault..  */.#ifde
2ce1e 66 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c 54  f SQLITE_DEFAULT
2ce1f 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 0a 20 20  _LOCKING_MODE.  
2ce20 64 62 2d 3e 64 66 6c 74 4c 6f 63 6b 4d 6f 64 65  db->dfltLockMode
2ce21 20 3d 20 53 51 4c 49 54 45 5f 44 45 46 41 55 4c   = SQLITE_DEFAUL
2ce22 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f 44 45 3b 0a  T_LOCKING_MODE;.
2ce23 20 20 73 71 6c 69 74 65 33 50 61 67 65 72 4c 6f    sqlite3PagerLo
2ce24 63 6b 69 6e 67 4d 6f 64 65 28 73 71 6c 69 74 65  ckingMode(sqlite
2ce25 33 42 74 72 65 65 50 61 67 65 72 28 64 62 2d 3e  3BtreePager(db->
2ce26 61 44 62 5b 30 5d 2e 70 42 74 29 2c 0a 20 20 20  aDb[0].pBt),.   
2ce27 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ce28 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f 44 45         SQLITE_DE
2ce29 46 41 55 4c 54 5f 4c 4f 43 4b 49 4e 47 5f 4d 4f  FAULT_LOCKING_MO
2ce2a 44 45 29 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 2f  DE);.#endif..  /
2ce2b 2a 20 45 6e 61 62 6c 65 20 74 68 65 20 6c 6f 6f  * Enable the loo
2ce2c 6b 61 73 69 64 65 2d 6d 61 6c 6c 6f 63 20 73 75  kaside-malloc su
2ce2d 62 73 79 73 74 65 6d 20 2a 2f 0a 20 20 73 65 74  bsystem */.  set
2ce2e 75 70 4c 6f 6f 6b 61 73 69 64 65 28 64 62 2c 20  upLookaside(db, 
2ce2f 30 2c 20 73 71 6c 69 74 65 33 47 6c 6f 62 61 6c  0, sqlite3Global
2ce30 43 6f 6e 66 69 67 2e 73 7a 4c 6f 6f 6b 61 73 69  Config.szLookasi
2ce31 64 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  de,.            
2ce32 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
2ce33 74 65 33 47 6c 6f 62 61 6c 43 6f 6e 66 69 67 2e  te3GlobalConfig.
2ce34 6e 4c 6f 6f 6b 61 73 69 64 65 29 3b 0a 0a 6f 70  nLookaside);..op
2ce35 65 6e 64 62 5f 6f 75 74 3a 0a 20 20 69 66 28 20  endb_out:.  if( 
2ce36 64 62 20 29 7b 0a 20 20 20 20 61 73 73 65 72 74  db ){.    assert
2ce37 28 20 64 62 2d 3e 6d 75 74 65 78 21 3d 30 20 7c  ( db->mutex!=0 |
2ce38 7c 20 69 73 54 68 72 65 61 64 73 61 66 65 3d 3d  | isThreadsafe==
2ce39 30 20 7c 7c 20 73 71 6c 69 74 65 33 47 6c 6f 62  0 || sqlite3Glob
2ce3a 61 6c 43 6f 6e 66 69 67 2e 62 46 75 6c 6c 4d 75  alConfig.bFullMu
2ce3b 74 65 78 3d 3d 30 20 29 3b 0a 20 20 20 20 73 71  tex==0 );.    sq
2ce3c 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76  lite3_mutex_leav
2ce3d 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20  e(db->mutex);.  
2ce3e 7d 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  }.  rc = sqlite3
2ce3f 5f 65 72 72 63 6f 64 65 28 64 62 29 3b 0a 20 20  _errcode(db);.  
2ce40 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e  if( rc==SQLITE_N
2ce41 4f 4d 45 4d 20 29 7b 0a 20 20 20 20 73 71 6c 69  OMEM ){.    sqli
2ce42 74 65 33 5f 63 6c 6f 73 65 28 64 62 29 3b 0a 20  te3_close(db);. 
2ce43 20 20 20 64 62 20 3d 20 30 3b 0a 20 20 7d 65 6c     db = 0;.  }el
2ce44 73 65 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  se if( rc!=SQLIT
2ce45 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 64 62 2d 3e  E_OK ){.    db->
2ce46 6d 61 67 69 63 20 3d 20 53 51 4c 49 54 45 5f 4d  magic = SQLITE_M
2ce47 41 47 49 43 5f 53 49 43 4b 3b 0a 20 20 7d 0a 20  AGIC_SICK;.  }. 
2ce48 20 2a 70 70 44 62 20 3d 20 64 62 3b 0a 20 20 72   *ppDb = db;.  r
2ce49 65 74 75 72 6e 20 73 71 6c 69 74 65 33 41 70 69  eturn sqlite3Api
2ce4a 45 78 69 74 28 30 2c 20 72 63 29 3b 0a 7d 0a 0a  Exit(0, rc);.}..
2ce4b 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61 20 6e 65 77  /*.** Open a new
2ce4c 20 64 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65   database handle
2ce4d 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20  ..*/.SQLITE_API 
2ce4e 69 6e 74 20 73 71 6c 69 74 65 33 5f 6f 70 65 6e  int sqlite3_open
2ce4f 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  (.  const char *
2ce50 7a 46 69 6c 65 6e 61 6d 65 2c 20 0a 20 20 73 71  zFilename, .  sq
2ce51 6c 69 74 65 33 20 2a 2a 70 70 44 62 20 0a 29 7b  lite3 **ppDb .){
2ce52 0a 20 20 72 65 74 75 72 6e 20 6f 70 65 6e 44 61  .  return openDa
2ce53 74 61 62 61 73 65 28 7a 46 69 6c 65 6e 61 6d 65  tabase(zFilename
2ce54 2c 20 70 70 44 62 2c 0a 20 20 20 20 20 20 20 20  , ppDb,.        
2ce55 20 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51                SQ
2ce56 4c 49 54 45 5f 4f 50 45 4e 5f 52 45 41 44 57 52  LITE_OPEN_READWR
2ce57 49 54 45 20 7c 20 53 51 4c 49 54 45 5f 4f 50 45  ITE | SQLITE_OPE
2ce58 4e 5f 43 52 45 41 54 45 2c 20 30 29 3b 0a 7d 0a  N_CREATE, 0);.}.
2ce59 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73  SQLITE_API int s
2ce5a 71 6c 69 74 65 33 5f 6f 70 65 6e 5f 76 32 28 0a  qlite3_open_v2(.
2ce5b 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 66 69    const char *fi
2ce5c 6c 65 6e 61 6d 65 2c 20 20 20 2f 2a 20 44 61 74  lename,   /* Dat
2ce5d 61 62 61 73 65 20 66 69 6c 65 6e 61 6d 65 20 28  abase filename (
2ce5e 55 54 46 2d 38 29 20 2a 2f 0a 20 20 73 71 6c 69  UTF-8) */.  sqli
2ce5f 74 65 33 20 2a 2a 70 70 44 62 2c 20 20 20 20 20  te3 **ppDb,     
2ce60 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 51 4c 69      /* OUT: SQLi
2ce61 74 65 20 64 62 20 68 61 6e 64 6c 65 20 2a 2f 0a  te db handle */.
2ce62 20 20 69 6e 74 20 66 6c 61 67 73 2c 20 20 20 20    int flags,    
2ce63 20 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6c 61            /* Fla
2ce64 67 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  gs */.  const ch
2ce65 61 72 20 2a 7a 56 66 73 20 20 20 20 20 20 20 20  ar *zVfs        
2ce66 2f 2a 20 4e 61 6d 65 20 6f 66 20 56 46 53 20 6d  /* Name of VFS m
2ce67 6f 64 75 6c 65 20 74 6f 20 75 73 65 20 2a 2f 0a  odule to use */.
2ce68 29 7b 0a 20 20 72 65 74 75 72 6e 20 6f 70 65 6e  ){.  return open
2ce69 44 61 74 61 62 61 73 65 28 66 69 6c 65 6e 61 6d  Database(filenam
2ce6a 65 2c 20 70 70 44 62 2c 20 66 6c 61 67 73 2c 20  e, ppDb, flags, 
2ce6b 7a 56 66 73 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65  zVfs);.}..#ifnde
2ce6c 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54  f SQLITE_OMIT_UT
2ce6d 46 31 36 0a 2f 2a 0a 2a 2a 20 4f 70 65 6e 20 61  F16./*.** Open a
2ce6e 20 6e 65 77 20 64 61 74 61 62 61 73 65 20 68 61   new database ha
2ce6f 6e 64 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  ndle..*/.SQLITE_
2ce70 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2ce71 6f 70 65 6e 31 36 28 0a 20 20 63 6f 6e 73 74 20  open16(.  const 
2ce72 76 6f 69 64 20 2a 7a 46 69 6c 65 6e 61 6d 65 2c  void *zFilename,
2ce73 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 2a 70 70   .  sqlite3 **pp
2ce74 44 62 0a 29 7b 0a 20 20 63 68 61 72 20 63 6f 6e  Db.){.  char con
2ce75 73 74 20 2a 7a 46 69 6c 65 6e 61 6d 65 38 3b 20  st *zFilename8; 
2ce76 20 20 2f 2a 20 7a 46 69 6c 65 6e 61 6d 65 20 65    /* zFilename e
2ce77 6e 63 6f 64 65 64 20 69 6e 20 55 54 46 2d 38 20  ncoded in UTF-8 
2ce78 69 6e 73 74 65 61 64 20 6f 66 20 55 54 46 2d 31  instead of UTF-1
2ce79 36 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 76  6 */.  sqlite3_v
2ce7a 61 6c 75 65 20 2a 70 56 61 6c 3b 0a 20 20 69 6e  alue *pVal;.  in
2ce7b 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72 74 28  t rc;..  assert(
2ce7c 20 7a 46 69 6c 65 6e 61 6d 65 20 29 3b 0a 20 20   zFilename );.  
2ce7d 61 73 73 65 72 74 28 20 70 70 44 62 20 29 3b 0a  assert( ppDb );.
2ce7e 20 20 2a 70 70 44 62 20 3d 20 30 3b 0a 23 69 66    *ppDb = 0;.#if
2ce7f 6e 64 65 66 20 53 51 4c 49 54 45 5f 4f 4d 49 54  ndef SQLITE_OMIT
2ce80 5f 41 55 54 4f 49 4e 49 54 0a 20 20 72 63 20 3d  _AUTOINIT.  rc =
2ce81 20 73 71 6c 69 74 65 33 5f 69 6e 69 74 69 61 6c   sqlite3_initial
2ce82 69 7a 65 28 29 3b 0a 20 20 69 66 28 20 72 63 20  ize();.  if( rc 
2ce83 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 23 65 6e  ) return rc;.#en
2ce84 64 69 66 0a 20 20 70 56 61 6c 20 3d 20 73 71 6c  dif.  pVal = sql
2ce85 69 74 65 33 56 61 6c 75 65 4e 65 77 28 30 29 3b  ite3ValueNew(0);
2ce86 0a 20 20 73 71 6c 69 74 65 33 56 61 6c 75 65 53  .  sqlite3ValueS
2ce87 65 74 53 74 72 28 70 56 61 6c 2c 20 2d 31 2c 20  etStr(pVal, -1, 
2ce88 7a 46 69 6c 65 6e 61 6d 65 2c 20 53 51 4c 49 54  zFilename, SQLIT
2ce89 45 5f 55 54 46 31 36 4e 41 54 49 56 45 2c 20 53  E_UTF16NATIVE, S
2ce8a 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
2ce8b 20 7a 46 69 6c 65 6e 61 6d 65 38 20 3d 20 73 71   zFilename8 = sq
2ce8c 6c 69 74 65 33 56 61 6c 75 65 54 65 78 74 28 70  lite3ValueText(p
2ce8d 56 61 6c 2c 20 53 51 4c 49 54 45 5f 55 54 46 38  Val, SQLITE_UTF8
2ce8e 29 3b 0a 20 20 69 66 28 20 7a 46 69 6c 65 6e 61  );.  if( zFilena
2ce8f 6d 65 38 20 29 7b 0a 20 20 20 20 72 63 20 3d 20  me8 ){.    rc = 
2ce90 6f 70 65 6e 44 61 74 61 62 61 73 65 28 7a 46 69  openDatabase(zFi
2ce91 6c 65 6e 61 6d 65 38 2c 20 70 70 44 62 2c 0a 20  lename8, ppDb,. 
2ce92 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ce93 20 20 20 20 20 53 51 4c 49 54 45 5f 4f 50 45 4e       SQLITE_OPEN
2ce94 5f 52 45 41 44 57 52 49 54 45 20 7c 20 53 51 4c  _READWRITE | SQL
2ce95 49 54 45 5f 4f 50 45 4e 5f 43 52 45 41 54 45 2c  ITE_OPEN_CREATE,
2ce96 20 30 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28   0);.    assert(
2ce97 20 2a 70 70 44 62 20 7c 7c 20 72 63 3d 3d 53 51   *ppDb || rc==SQ
2ce98 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 3b 0a 20 20  LITE_NOMEM );.  
2ce99 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2ce9a 5f 4f 4b 20 26 26 20 21 44 62 48 61 73 50 72 6f  _OK && !DbHasPro
2ce9b 70 65 72 74 79 28 2a 70 70 44 62 2c 20 30 2c 20  perty(*ppDb, 0, 
2ce9c 44 42 5f 53 63 68 65 6d 61 4c 6f 61 64 65 64 29  DB_SchemaLoaded)
2ce9d 20 29 7b 0a 20 20 20 20 20 20 45 4e 43 28 2a 70   ){.      ENC(*p
2ce9e 70 44 62 29 20 3d 20 53 51 4c 49 54 45 5f 55 54  pDb) = SQLITE_UT
2ce9f 46 31 36 4e 41 54 49 56 45 3b 0a 20 20 20 20 7d  F16NATIVE;.    }
2cea0 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72 63  .  }else{.    rc
2cea1 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
2cea2 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 56 61  .  }.  sqlite3Va
2cea3 6c 75 65 46 72 65 65 28 70 56 61 6c 29 3b 0a 0a  lueFree(pVal);..
2cea4 20 20 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33    return sqlite3
2cea5 41 70 69 45 78 69 74 28 30 2c 20 72 63 29 3b 0a  ApiExit(0, rc);.
2cea6 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53 51 4c 49  }.#endif /* SQLI
2cea7 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 20 2a 2f  TE_OMIT_UTF16 */
2cea8 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65 72  ../*.** Register
2cea9 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74 69 6f 6e   a new collation
2ceaa 20 73 65 71 75 65 6e 63 65 20 77 69 74 68 20 74   sequence with t
2ceab 68 65 20 64 61 74 61 62 61 73 65 20 68 61 6e 64  he database hand
2ceac 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45  le db..*/.SQLITE
2cead 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33  _API int sqlite3
2ceae 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61 74 69 6f  _create_collatio
2ceaf 6e 28 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62  n(.  sqlite3* db
2ceb0 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
2ceb1 2a 7a 4e 61 6d 65 2c 20 0a 20 20 69 6e 74 20 65  *zName, .  int e
2ceb2 6e 63 2c 20 0a 20 20 76 6f 69 64 2a 20 70 43 74  nc, .  void* pCt
2ceb3 78 2c 0a 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61  x,.  int(*xCompa
2ceb4 72 65 29 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  re)(void*,int,co
2ceb5 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
2ceb6 6e 73 74 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20  nst void*).){.  
2ceb7 69 6e 74 20 72 63 3b 0a 20 20 73 71 6c 69 74 65  int rc;.  sqlite
2ceb8 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2ceb9 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 61 73 73 65  ->mutex);.  asse
2ceba 72 74 28 20 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46  rt( !db->mallocF
2cebb 61 69 6c 65 64 20 29 3b 0a 20 20 72 63 20 3d 20  ailed );.  rc = 
2cebc 63 72 65 61 74 65 43 6f 6c 6c 61 74 69 6f 6e 28  createCollation(
2cebd 64 62 2c 20 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20  db, zName, enc, 
2cebe 70 43 74 78 2c 20 78 43 6f 6d 70 61 72 65 2c 20  pCtx, xCompare, 
2cebf 30 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  0);.  rc = sqlit
2cec0 65 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63  e3ApiExit(db, rc
2cec1 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74  );.  sqlite3_mut
2cec2 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74  ex_leave(db->mut
2cec3 65 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ex);.  return rc
2cec4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73  ;.}../*.** Regis
2cec5 74 65 72 20 61 20 6e 65 77 20 63 6f 6c 6c 61 74  ter a new collat
2cec6 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 77 69 74  ion sequence wit
2cec7 68 20 74 68 65 20 64 61 74 61 62 61 73 65 20 68  h the database h
2cec8 61 6e 64 6c 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c  andle db..*/.SQL
2cec9 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69  ITE_API int sqli
2ceca 74 65 33 5f 63 72 65 61 74 65 5f 63 6f 6c 6c 61  te3_create_colla
2cecb 74 69 6f 6e 5f 76 32 28 0a 20 20 73 71 6c 69 74  tion_v2(.  sqlit
2cecc 65 33 2a 20 64 62 2c 20 0a 20 20 63 6f 6e 73 74  e3* db, .  const
2cecd 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a 20   char *zName, . 
2cece 20 69 6e 74 20 65 6e 63 2c 20 0a 20 20 76 6f 69   int enc, .  voi
2cecf 64 2a 20 70 43 74 78 2c 0a 20 20 69 6e 74 28 2a  d* pCtx,.  int(*
2ced0 78 43 6f 6d 70 61 72 65 29 28 76 6f 69 64 2a 2c  xCompare)(void*,
2ced1 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c  int,const void*,
2ced2 69 6e 74 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 29  int,const void*)
2ced3 2c 0a 20 20 76 6f 69 64 28 2a 78 44 65 6c 29 28  ,.  void(*xDel)(
2ced4 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74 20  void*).){.  int 
2ced5 72 63 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75  rc;.  sqlite3_mu
2ced6 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75  tex_enter(db->mu
2ced7 74 65 78 29 3b 0a 20 20 61 73 73 65 72 74 28 20  tex);.  assert( 
2ced8 21 64 62 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65  !db->mallocFaile
2ced9 64 20 29 3b 0a 20 20 72 63 20 3d 20 63 72 65 61  d );.  rc = crea
2ceda 74 65 43 6f 6c 6c 61 74 69 6f 6e 28 64 62 2c 20  teCollation(db, 
2cedb 7a 4e 61 6d 65 2c 20 65 6e 63 2c 20 70 43 74 78  zName, enc, pCtx
2cedc 2c 20 78 43 6f 6d 70 61 72 65 2c 20 78 44 65 6c  , xCompare, xDel
2cedd 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2cede 33 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29  3ApiExit(db, rc)
2cedf 3b 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65  ;.  sqlite3_mute
2cee0 78 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65  x_leave(db->mute
2cee1 78 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  x);.  return rc;
2cee2 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49  .}..#ifndef SQLI
2cee3 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a  TE_OMIT_UTF16./*
2cee4 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 6e  .** Register a n
2cee5 65 77 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71  ew collation seq
2cee6 75 65 6e 63 65 20 77 69 74 68 20 74 68 65 20 64  uence with the d
2cee7 61 74 61 62 61 73 65 20 68 61 6e 64 6c 65 20 64  atabase handle d
2cee8 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  b..*/.SQLITE_API
2cee9 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 63 72 65   int sqlite3_cre
2ceea 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 31 36 28  ate_collation16(
2ceeb 0a 20 20 73 71 6c 69 74 65 33 2a 20 64 62 2c 20  .  sqlite3* db, 
2ceec 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 7a  .  const void *z
2ceed 4e 61 6d 65 2c 0a 20 20 69 6e 74 20 65 6e 63 2c  Name,.  int enc,
2ceee 20 0a 20 20 76 6f 69 64 2a 20 70 43 74 78 2c 0a   .  void* pCtx,.
2ceef 20 20 69 6e 74 28 2a 78 43 6f 6d 70 61 72 65 29    int(*xCompare)
2cef0 28 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74  (void*,int,const
2cef1 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f 6e 73 74   void*,int,const
2cef2 20 76 6f 69 64 2a 29 0a 29 7b 0a 20 20 69 6e 74   void*).){.  int
2cef3 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
2cef4 0a 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 38 3b  .  char *zName8;
2cef5 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2cef6 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2cef7 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64 62  );.  assert( !db
2cef8 2d 3e 6d 61 6c 6c 6f 63 46 61 69 6c 65 64 20 29  ->mallocFailed )
2cef9 3b 0a 20 20 7a 4e 61 6d 65 38 20 3d 20 73 71 6c  ;.  zName8 = sql
2cefa 69 74 65 33 55 74 66 31 36 74 6f 38 28 64 62 2c  ite3Utf16to8(db,
2cefb 20 7a 4e 61 6d 65 2c 20 2d 31 29 3b 0a 20 20 69   zName, -1);.  i
2cefc 66 28 20 7a 4e 61 6d 65 38 20 29 7b 0a 20 20 20  f( zName8 ){.   
2cefd 20 72 63 20 3d 20 63 72 65 61 74 65 43 6f 6c 6c   rc = createColl
2cefe 61 74 69 6f 6e 28 64 62 2c 20 7a 4e 61 6d 65 38  ation(db, zName8
2ceff 2c 20 65 6e 63 2c 20 70 43 74 78 2c 20 78 43 6f  , enc, pCtx, xCo
2cf00 6d 70 61 72 65 2c 20 30 29 3b 0a 20 20 20 20 73  mpare, 0);.    s
2cf01 71 6c 69 74 65 33 44 62 46 72 65 65 28 64 62 2c  qlite3DbFree(db,
2cf02 20 7a 4e 61 6d 65 38 29 3b 0a 20 20 7d 0a 20 20   zName8);.  }.  
2cf03 72 63 20 3d 20 73 71 6c 69 74 65 33 41 70 69 45  rc = sqlite3ApiE
2cf04 78 69 74 28 64 62 2c 20 72 63 29 3b 0a 20 20 73  xit(db, rc);.  s
2cf05 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2cf06 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
2cf07 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 23 65   return rc;.}.#e
2cf08 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2cf09 4d 49 54 5f 55 54 46 31 36 20 2a 2f 0a 0a 2f 2a  MIT_UTF16 */../*
2cf0a 0a 2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63  .** Register a c
2cf0b 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63  ollation sequenc
2cf0c 65 20 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61  e factory callba
2cf0d 63 6b 20 77 69 74 68 20 74 68 65 20 64 61 74 61  ck with the data
2cf0e 62 61 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64  base handle.** d
2cf0f 62 2e 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70  b. Replace any p
2cf10 72 65 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c  reviously instal
2cf11 6c 65 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65  led collation se
2cf12 71 75 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a  quence factory..
2cf13 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e  */.SQLITE_API in
2cf14 74 20 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74  t sqlite3_collat
2cf15 69 6f 6e 5f 6e 65 65 64 65 64 28 0a 20 20 73 71  ion_needed(.  sq
2cf16 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f  lite3 *db, .  vo
2cf17 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41  id *pCollNeededA
2cf18 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f  rg, .  void(*xCo
2cf19 6c 6c 4e 65 65 64 65 64 29 28 76 6f 69 64 2a 2c  llNeeded)(void*,
2cf1a 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65 54 65  sqlite3*,int eTe
2cf1b 78 74 52 65 70 2c 63 6f 6e 73 74 20 63 68 61 72  xtRep,const char
2cf1c 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  *).){.  sqlite3_
2cf1d 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d 3e  mutex_enter(db->
2cf1e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e 78 43  mutex);.  db->xC
2cf1f 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 78 43 6f 6c  ollNeeded = xCol
2cf20 6c 4e 65 65 64 65 64 3b 0a 20 20 64 62 2d 3e 78  lNeeded;.  db->x
2cf21 43 6f 6c 6c 4e 65 65 64 65 64 31 36 20 3d 20 30  CollNeeded16 = 0
2cf22 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c 6c 4e 65 65  ;.  db->pCollNee
2cf23 64 65 64 41 72 67 20 3d 20 70 43 6f 6c 6c 4e 65  dedArg = pCollNe
2cf24 65 64 65 64 41 72 67 3b 0a 20 20 73 71 6c 69 74  ededArg;.  sqlit
2cf25 65 33 5f 6d 75 74 65 78 5f 6c 65 61 76 65 28 64  e3_mutex_leave(d
2cf26 62 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 72 65 74  b->mutex);.  ret
2cf27 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2cf28 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c 49 54 45  ..#ifndef SQLITE
2cf29 5f 4f 4d 49 54 5f 55 54 46 31 36 0a 2f 2a 0a 2a  _OMIT_UTF16./*.*
2cf2a 2a 20 52 65 67 69 73 74 65 72 20 61 20 63 6f 6c  * Register a col
2cf2b 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20  lation sequence 
2cf2c 66 61 63 74 6f 72 79 20 63 61 6c 6c 62 61 63 6b  factory callback
2cf2d 20 77 69 74 68 20 74 68 65 20 64 61 74 61 62 61   with the databa
2cf2e 73 65 20 68 61 6e 64 6c 65 0a 2a 2a 20 64 62 2e  se handle.** db.
2cf2f 20 52 65 70 6c 61 63 65 20 61 6e 79 20 70 72 65   Replace any pre
2cf30 76 69 6f 75 73 6c 79 20 69 6e 73 74 61 6c 6c 65  viously installe
2cf31 64 20 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75  d collation sequ
2cf32 65 6e 63 65 20 66 61 63 74 6f 72 79 2e 0a 2a 2f  ence factory..*/
2cf33 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2cf34 73 71 6c 69 74 65 33 5f 63 6f 6c 6c 61 74 69 6f  sqlite3_collatio
2cf35 6e 5f 6e 65 65 64 65 64 31 36 28 0a 20 20 73 71  n_needed16(.  sq
2cf36 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 76 6f  lite3 *db, .  vo
2cf37 69 64 20 2a 70 43 6f 6c 6c 4e 65 65 64 65 64 41  id *pCollNeededA
2cf38 72 67 2c 20 0a 20 20 76 6f 69 64 28 2a 78 43 6f  rg, .  void(*xCo
2cf39 6c 6c 4e 65 65 64 65 64 31 36 29 28 76 6f 69 64  llNeeded16)(void
2cf3a 2a 2c 73 71 6c 69 74 65 33 2a 2c 69 6e 74 20 65  *,sqlite3*,int e
2cf3b 54 65 78 74 52 65 70 2c 63 6f 6e 73 74 20 76 6f  TextRep,const vo
2cf3c 69 64 2a 29 0a 29 7b 0a 20 20 73 71 6c 69 74 65  id*).){.  sqlite
2cf3d 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2cf3e 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 64 62 2d 3e  ->mutex);.  db->
2cf3f 78 43 6f 6c 6c 4e 65 65 64 65 64 20 3d 20 30 3b  xCollNeeded = 0;
2cf40 0a 20 20 64 62 2d 3e 78 43 6f 6c 6c 4e 65 65 64  .  db->xCollNeed
2cf41 65 64 31 36 20 3d 20 78 43 6f 6c 6c 4e 65 65 64  ed16 = xCollNeed
2cf42 65 64 31 36 3b 0a 20 20 64 62 2d 3e 70 43 6f 6c  ed16;.  db->pCol
2cf43 6c 4e 65 65 64 65 64 41 72 67 20 3d 20 70 43 6f  lNeededArg = pCo
2cf44 6c 6c 4e 65 65 64 65 64 41 72 67 3b 0a 20 20 73  llNeededArg;.  s
2cf45 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65 61  qlite3_mutex_lea
2cf46 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a 20  ve(db->mutex);. 
2cf47 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2cf48 4b 3b 0a 7d 0a 23 65 6e 64 69 66 20 2f 2a 20 53  K;.}.#endif /* S
2cf49 51 4c 49 54 45 5f 4f 4d 49 54 5f 55 54 46 31 36  QLITE_OMIT_UTF16
2cf4a 20 2a 2f 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c   */..#ifndef SQL
2cf4b 49 54 45 5f 4f 4d 49 54 5f 47 4c 4f 42 41 4c 52  ITE_OMIT_GLOBALR
2cf4c 45 43 4f 56 45 52 0a 23 69 66 6e 64 65 66 20 53  ECOVER.#ifndef S
2cf4d 51 4c 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45  QLITE_OMIT_DEPRE
2cf4e 43 41 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73  CATED./*.** This
2cf4f 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 6e 6f 77   function is now
2cf50 20 61 6e 20 61 6e 61 63 68 72 6f 6e 69 73 6d 2e   an anachronism.
2cf51 20 49 74 20 75 73 65 64 20 74 6f 20 62 65 20 75   It used to be u
2cf52 73 65 64 20 74 6f 20 72 65 63 6f 76 65 72 20 66  sed to recover f
2cf53 72 6f 6d 20 61 0a 2a 2a 20 6d 61 6c 6c 6f 63 28  rom a.** malloc(
2cf54 29 20 66 61 69 6c 75 72 65 2c 20 62 75 74 20 53  ) failure, but S
2cf55 51 4c 69 74 65 20 6e 6f 77 20 64 6f 65 73 20 74  QLite now does t
2cf56 68 69 73 20 61 75 74 6f 6d 61 74 69 63 61 6c 6c  his automaticall
2cf57 79 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49  y..*/.SQLITE_API
2cf58 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 67 6c 6f   int sqlite3_glo
2cf59 62 61 6c 5f 72 65 63 6f 76 65 72 28 76 6f 69 64  bal_recover(void
2cf5a 29 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ){.  return SQLI
2cf5b 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66 0a  TE_OK;.}.#endif.
2cf5c 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 65  #endif../*.** Te
2cf5d 73 74 20 74 6f 20 73 65 65 20 77 68 65 74 68 65  st to see whethe
2cf5e 72 20 6f 72 20 6e 6f 74 20 74 68 65 20 64 61 74  r or not the dat
2cf5f 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69 6f 6e  abase connection
2cf60 20 69 73 20 69 6e 20 61 75 74 6f 63 6f 6d 6d 69   is in autocommi
2cf61 74 0a 2a 2a 20 6d 6f 64 65 2e 20 20 52 65 74 75  t.** mode.  Retu
2cf62 72 6e 20 54 52 55 45 20 69 66 20 69 74 20 69 73  rn TRUE if it is
2cf63 20 61 6e 64 20 46 41 4c 53 45 20 69 66 20 6e 6f   and FALSE if no
2cf64 74 2e 20 20 41 75 74 6f 63 6f 6d 6d 69 74 20 6d  t.  Autocommit m
2cf65 6f 64 65 20 69 73 20 6f 6e 0a 2a 2a 20 62 79 20  ode is on.** by 
2cf66 64 65 66 61 75 6c 74 2e 20 20 41 75 74 6f 63 6f  default.  Autoco
2cf67 6d 6d 69 74 20 69 73 20 64 69 73 61 62 6c 65 64  mmit is disabled
2cf68 20 62 79 20 61 20 42 45 47 49 4e 20 73 74 61 74   by a BEGIN stat
2cf69 65 6d 65 6e 74 20 61 6e 64 20 72 65 65 6e 61 62  ement and reenab
2cf6a 6c 65 64 0a 2a 2a 20 62 79 20 74 68 65 20 6e 65  led.** by the ne
2cf6b 78 74 20 43 4f 4d 4d 49 54 20 6f 72 20 52 4f 4c  xt COMMIT or ROL
2cf6c 4c 42 41 43 4b 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a  LBACK..**.******
2cf6d 2a 20 54 48 49 53 20 49 53 20 41 4e 20 45 58 50  * THIS IS AN EXP
2cf6e 45 52 49 4d 45 4e 54 41 4c 20 41 50 49 20 41 4e  ERIMENTAL API AN
2cf6f 44 20 49 53 20 53 55 42 4a 45 43 54 20 54 4f 20  D IS SUBJECT TO 
2cf70 43 48 41 4e 47 45 20 2a 2a 2a 2a 2a 2a 0a 2a 2f  CHANGE ******.*/
2cf71 0a 53 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20  .SQLITE_API int 
2cf72 73 71 6c 69 74 65 33 5f 67 65 74 5f 61 75 74 6f  sqlite3_get_auto
2cf73 63 6f 6d 6d 69 74 28 73 71 6c 69 74 65 33 20 2a  commit(sqlite3 *
2cf74 64 62 29 7b 0a 20 20 72 65 74 75 72 6e 20 64 62  db){.  return db
2cf75 2d 3e 61 75 74 6f 43 6f 6d 6d 69 74 3b 0a 7d 0a  ->autoCommit;.}.
2cf76 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 44  .#ifdef SQLITE_D
2cf77 45 42 55 47 0a 2f 2a 0a 2a 2a 20 54 68 65 20 66  EBUG./*.** The f
2cf78 6f 6c 6c 6f 77 69 6e 67 20 72 6f 75 74 69 6e 65  ollowing routine
2cf79 20 69 73 20 73 75 62 74 69 74 75 74 65 64 20 66   is subtituted f
2cf7a 6f 72 20 63 6f 6e 73 74 61 6e 74 20 53 51 4c 49  or constant SQLI
2cf7b 54 45 5f 43 4f 52 52 55 50 54 20 69 6e 0a 2a 2a  TE_CORRUPT in.**
2cf7c 20 64 65 62 75 67 67 69 6e 67 20 62 75 69 6c 64   debugging build
2cf7d 73 2e 20 20 54 68 69 73 20 70 72 6f 76 69 64 65  s.  This provide
2cf7e 73 20 61 20 77 61 79 20 74 6f 20 73 65 74 20 61  s a way to set a
2cf7f 20 62 72 65 61 6b 70 6f 69 6e 74 20 66 6f 72 20   breakpoint for 
2cf80 77 68 65 6e 0a 2a 2a 20 63 6f 72 72 75 70 74 69  when.** corrupti
2cf81 6f 6e 20 69 73 20 66 69 72 73 74 20 64 65 74 65  on is first dete
2cf82 63 74 65 64 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  cted..*/.SQLITE_
2cf83 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69  PRIVATE int sqli
2cf84 74 65 33 43 6f 72 72 75 70 74 28 76 6f 69 64 29  te3Corrupt(void)
2cf85 7b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  {.  return SQLIT
2cf86 45 5f 43 4f 52 52 55 50 54 3b 0a 7d 0a 23 65 6e  E_CORRUPT;.}.#en
2cf87 64 69 66 0a 0a 23 69 66 6e 64 65 66 20 53 51 4c  dif..#ifndef SQL
2cf88 49 54 45 5f 4f 4d 49 54 5f 44 45 50 52 45 43 41  ITE_OMIT_DEPRECA
2cf89 54 45 44 0a 2f 2a 0a 2a 2a 20 54 68 69 73 20 69  TED./*.** This i
2cf8a 73 20 61 20 63 6f 6e 76 65 6e 69 65 6e 63 65 20  s a convenience 
2cf8b 72 6f 75 74 69 6e 65 20 74 68 61 74 20 6d 61 6b  routine that mak
2cf8c 65 73 20 73 75 72 65 20 74 68 61 74 20 61 6c 6c  es sure that all
2cf8d 20 74 68 72 65 61 64 2d 73 70 65 63 69 66 69 63   thread-specific
2cf8e 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 74 68 69  .** data for thi
2cf8f 73 20 74 68 72 65 61 64 20 68 61 73 20 62 65 65  s thread has bee
2cf90 6e 20 64 65 61 6c 6c 6f 63 61 74 65 64 2e 0a 2a  n deallocated..*
2cf91 2a 0a 2a 2a 20 53 51 4c 69 74 65 20 6e 6f 20 6c  *.** SQLite no l
2cf92 6f 6e 67 65 72 20 75 73 65 73 20 74 68 72 65 61  onger uses threa
2cf93 64 2d 73 70 65 63 69 66 69 63 20 64 61 74 61 20  d-specific data 
2cf94 73 6f 20 74 68 69 73 20 72 6f 75 74 69 6e 65 20  so this routine 
2cf95 69 73 20 6e 6f 77 20 61 0a 2a 2a 20 6e 6f 2d 6f  is now a.** no-o
2cf96 70 2e 20 20 49 74 20 69 73 20 72 65 74 61 69 6e  p.  It is retain
2cf97 65 64 20 66 6f 72 20 68 69 73 74 6f 72 69 63 61  ed for historica
2cf98 6c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74 79 2e  l compatibility.
2cf99 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 76  .*/.SQLITE_API v
2cf9a 6f 69 64 20 73 71 6c 69 74 65 33 5f 74 68 72 65  oid sqlite3_thre
2cf9b 61 64 5f 63 6c 65 61 6e 75 70 28 76 6f 69 64 29  ad_cleanup(void)
2cf9c 7b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a  {.}.#endif../*.*
2cf9d 2a 20 52 65 74 75 72 6e 20 6d 65 74 61 20 69 6e  * Return meta in
2cf9e 66 6f 72 6d 61 74 69 6f 6e 20 61 62 6f 75 74 20  formation about 
2cf9f 61 20 73 70 65 63 69 66 69 63 20 63 6f 6c 75 6d  a specific colum
2cfa0 6e 20 6f 66 20 61 20 64 61 74 61 62 61 73 65 20  n of a database 
2cfa1 74 61 62 6c 65 2e 0a 2a 2a 20 53 65 65 20 63 6f  table..** See co
2cfa2 6d 6d 65 6e 74 20 69 6e 20 73 71 6c 69 74 65 33  mment in sqlite3
2cfa3 2e 68 20 28 73 71 6c 69 74 65 2e 68 2e 69 6e 29  .h (sqlite.h.in)
2cfa4 20 66 6f 72 20 64 65 74 61 69 6c 73 2e 0a 2a 2f   for details..*/
2cfa5 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f 45  .#ifdef SQLITE_E
2cfa6 4e 41 42 4c 45 5f 43 4f 4c 55 4d 4e 5f 4d 45 54  NABLE_COLUMN_MET
2cfa7 41 44 41 54 41 0a 53 51 4c 49 54 45 5f 41 50 49  ADATA.SQLITE_API
2cfa8 20 69 6e 74 20 73 71 6c 69 74 65 33 5f 74 61 62   int sqlite3_tab
2cfa9 6c 65 5f 63 6f 6c 75 6d 6e 5f 6d 65 74 61 64 61  le_column_metada
2cfaa 74 61 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64  ta(.  sqlite3 *d
2cfab 62 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b,              
2cfac 20 20 2f 2a 20 43 6f 6e 6e 65 63 74 69 6f 6e 20    /* Connection 
2cfad 68 61 6e 64 6c 65 20 2a 2f 0a 20 20 63 6f 6e 73  handle */.  cons
2cfae 74 20 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c  t char *zDbName,
2cfaf 20 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 62          /* Datab
2cfb0 61 73 65 20 6e 61 6d 65 20 6f 72 20 4e 55 4c 4c  ase name or NULL
2cfb1 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2cfb2 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 20 20   *zTableName,   
2cfb3 20 20 2f 2a 20 54 61 62 6c 65 20 6e 61 6d 65 20    /* Table name 
2cfb4 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2cfb5 2a 7a 43 6f 6c 75 6d 6e 4e 61 6d 65 2c 20 20 20  *zColumnName,   
2cfb6 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e 61 6d 65 20   /* Column name 
2cfb7 2a 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20  */.  char const 
2cfb8 2a 2a 70 7a 44 61 74 61 54 79 70 65 2c 20 20 20  **pzDataType,   
2cfb9 20 2f 2a 20 4f 55 54 50 55 54 3a 20 44 65 63 6c   /* OUTPUT: Decl
2cfba 61 72 65 64 20 64 61 74 61 20 74 79 70 65 20 2a  ared data type *
2cfbb 2f 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  /.  char const *
2cfbc 2a 70 7a 43 6f 6c 6c 53 65 71 2c 20 20 20 20 20  *pzCollSeq,     
2cfbd 2f 2a 20 4f 55 54 50 55 54 3a 20 43 6f 6c 6c 61  /* OUTPUT: Colla
2cfbe 74 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61  tion sequence na
2cfbf 6d 65 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 4e 6f  me */.  int *pNo
2cfc0 74 4e 75 6c 6c 2c 20 20 20 20 20 20 20 20 20 20  tNull,          
2cfc1 20 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54      /* OUTPUT: T
2cfc2 72 75 65 20 69 66 20 4e 4f 54 20 4e 55 4c 4c 20  rue if NOT NULL 
2cfc3 63 6f 6e 73 74 72 61 69 6e 74 20 65 78 69 73 74  constraint exist
2cfc4 73 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 50 72 69  s */.  int *pPri
2cfc5 6d 61 72 79 4b 65 79 2c 20 20 20 20 20 20 20 20  maryKey,        
2cfc6 20 20 20 2f 2a 20 4f 55 54 50 55 54 3a 20 54 72     /* OUTPUT: Tr
2cfc7 75 65 20 69 66 20 63 6f 6c 75 6d 6e 20 70 61 72  ue if column par
2cfc8 74 20 6f 66 20 50 4b 20 2a 2f 0a 20 20 69 6e 74  t of PK */.  int
2cfc9 20 2a 70 41 75 74 6f 69 6e 63 20 20 20 20 20 20   *pAutoinc      
2cfca 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 50           /* OUTP
2cfcb 55 54 3a 20 54 72 75 65 20 69 66 20 63 6f 6c 75  UT: True if colu
2cfcc 6d 6e 20 69 73 20 61 75 74 6f 2d 69 6e 63 72 65  mn is auto-incre
2cfcd 6d 65 6e 74 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74  ment */.){.  int
2cfce 20 72 63 3b 0a 20 20 63 68 61 72 20 2a 7a 45 72   rc;.  char *zEr
2cfcf 72 4d 73 67 20 3d 20 30 3b 0a 20 20 54 61 62 6c  rMsg = 0;.  Tabl
2cfd0 65 20 2a 70 54 61 62 20 3d 20 30 3b 0a 20 20 43  e *pTab = 0;.  C
2cfd1 6f 6c 75 6d 6e 20 2a 70 43 6f 6c 20 3d 20 30 3b  olumn *pCol = 0;
2cfd2 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 0a 20 20  .  int iCol;..  
2cfd3 63 68 61 72 20 63 6f 6e 73 74 20 2a 7a 44 61 74  char const *zDat
2cfd4 61 54 79 70 65 20 3d 20 30 3b 0a 20 20 63 68 61  aType = 0;.  cha
2cfd5 72 20 63 6f 6e 73 74 20 2a 7a 43 6f 6c 6c 53 65  r const *zCollSe
2cfd6 71 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 6f 74  q = 0;.  int not
2cfd7 6e 75 6c 6c 20 3d 20 30 3b 0a 20 20 69 6e 74 20  null = 0;.  int 
2cfd8 70 72 69 6d 61 72 79 6b 65 79 20 3d 20 30 3b 0a  primarykey = 0;.
2cfd9 20 20 69 6e 74 20 61 75 74 6f 69 6e 63 20 3d 20    int autoinc = 
2cfda 30 3b 0a 0a 20 20 2f 2a 20 45 6e 73 75 72 65 20  0;..  /* Ensure 
2cfdb 74 68 65 20 64 61 74 61 62 61 73 65 20 73 63 68  the database sch
2cfdc 65 6d 61 20 68 61 73 20 62 65 65 6e 20 6c 6f 61  ema has been loa
2cfdd 64 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ded */.  sqlite3
2cfde 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62 2d  _mutex_enter(db-
2cfdf 3e 6d 75 74 65 78 29 3b 0a 20 20 28 76 6f 69 64  >mutex);.  (void
2cfe0 29 73 71 6c 69 74 65 33 53 61 66 65 74 79 4f 6e  )sqlite3SafetyOn
2cfe1 28 64 62 29 3b 0a 20 20 73 71 6c 69 74 65 33 42  (db);.  sqlite3B
2cfe2 74 72 65 65 45 6e 74 65 72 41 6c 6c 28 64 62 29  treeEnterAll(db)
2cfe3 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2cfe4 49 6e 69 74 28 64 62 2c 20 26 7a 45 72 72 4d 73  Init(db, &zErrMs
2cfe5 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 42 74 72  g);.  sqlite3Btr
2cfe6 65 65 4c 65 61 76 65 41 6c 6c 28 64 62 29 3b 0a  eeLeaveAll(db);.
2cfe7 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4f 4b 21    if( SQLITE_OK!
2cfe8 3d 72 63 20 29 7b 0a 20 20 20 20 67 6f 74 6f 20  =rc ){.    goto 
2cfe9 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
2cfea 20 20 2f 2a 20 4c 6f 63 61 74 65 20 74 68 65 20    /* Locate the 
2cfeb 74 61 62 6c 65 20 69 6e 20 71 75 65 73 74 69 6f  table in questio
2cfec 6e 20 2a 2f 0a 20 20 70 54 61 62 20 3d 20 73 71  n */.  pTab = sq
2cfed 6c 69 74 65 33 46 69 6e 64 54 61 62 6c 65 28 64  lite3FindTable(d
2cfee 62 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 20 7a  b, zTableName, z
2cfef 44 62 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21  DbName);.  if( !
2cff0 70 54 61 62 20 7c 7c 20 70 54 61 62 2d 3e 70 53  pTab || pTab->pS
2cff1 65 6c 65 63 74 20 29 7b 0a 20 20 20 20 70 54 61  elect ){.    pTa
2cff2 62 20 3d 20 30 3b 0a 20 20 20 20 67 6f 74 6f 20  b = 0;.    goto 
2cff3 65 72 72 6f 72 5f 6f 75 74 3b 0a 20 20 7d 0a 0a  error_out;.  }..
2cff4 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20 63 6f    /* Find the co
2cff5 6c 75 6d 6e 20 66 6f 72 20 77 68 69 63 68 20 69  lumn for which i
2cff6 6e 66 6f 20 69 73 20 72 65 71 75 65 73 74 65 64  nfo is requested
2cff7 20 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65   */.  if( sqlite
2cff8 33 49 73 52 6f 77 69 64 28 7a 43 6f 6c 75 6d 6e  3IsRowid(zColumn
2cff9 4e 61 6d 65 29 20 29 7b 0a 20 20 20 20 69 43 6f  Name) ){.    iCo
2cffa 6c 20 3d 20 70 54 61 62 2d 3e 69 50 4b 65 79 3b  l = pTab->iPKey;
2cffb 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3e 3d 30  .    if( iCol>=0
2cffc 20 29 7b 0a 20 20 20 20 20 20 70 43 6f 6c 20 3d   ){.      pCol =
2cffd 20 26 70 54 61 62 2d 3e 61 43 6f 6c 5b 69 43 6f   &pTab->aCol[iCo
2cffe 6c 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73  l];.    }.  }els
2cfff 65 7b 0a 20 20 20 20 66 6f 72 28 69 43 6f 6c 3d  e{.    for(iCol=
2d000 30 3b 20 69 43 6f 6c 3c 70 54 61 62 2d 3e 6e 43  0; iCol<pTab->nC
2d001 6f 6c 3b 20 69 43 6f 6c 2b 2b 29 7b 0a 20 20 20  ol; iCol++){.   
2d002 20 20 20 70 43 6f 6c 20 3d 20 26 70 54 61 62 2d     pCol = &pTab-
2d003 3e 61 43 6f 6c 5b 69 43 6f 6c 5d 3b 0a 20 20 20  >aCol[iCol];.   
2d004 20 20 20 69 66 28 20 30 3d 3d 73 71 6c 69 74 65     if( 0==sqlite
2d005 33 53 74 72 49 43 6d 70 28 70 43 6f 6c 2d 3e 7a  3StrICmp(pCol->z
2d006 4e 61 6d 65 2c 20 7a 43 6f 6c 75 6d 6e 4e 61 6d  Name, zColumnNam
2d007 65 29 20 29 7b 0a 20 20 20 20 20 20 20 20 62 72  e) ){.        br
2d008 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  eak;.      }.   
2d009 20 7d 0a 20 20 20 20 69 66 28 20 69 43 6f 6c 3d   }.    if( iCol=
2d00a 3d 70 54 61 62 2d 3e 6e 43 6f 6c 20 29 7b 0a 20  =pTab->nCol ){. 
2d00b 20 20 20 20 20 70 54 61 62 20 3d 20 30 3b 0a 20       pTab = 0;. 
2d00c 20 20 20 20 20 67 6f 74 6f 20 65 72 72 6f 72 5f       goto error_
2d00d 6f 75 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  out;.    }.  }..
2d00e 20 20 2f 2a 20 54 68 65 20 66 6f 6c 6c 6f 77 69    /* The followi
2d00f 6e 67 20 62 6c 6f 63 6b 20 73 74 6f 72 65 73 20  ng block stores 
2d010 74 68 65 20 6d 65 74 61 20 69 6e 66 6f 72 6d 61  the meta informa
2d011 74 69 6f 6e 20 74 68 61 74 20 77 69 6c 6c 20 62  tion that will b
2d012 65 20 72 65 74 75 72 6e 65 64 0a 20 20 2a 2a 20  e returned.  ** 
2d013 74 6f 20 74 68 65 20 63 61 6c 6c 65 72 20 69 6e  to the caller in
2d014 20 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 73   local variables
2d015 20 7a 44 61 74 61 54 79 70 65 2c 20 7a 43 6f 6c   zDataType, zCol
2d016 6c 53 65 71 2c 20 6e 6f 74 6e 75 6c 6c 2c 20 70  lSeq, notnull, p
2d017 72 69 6d 61 72 79 6b 65 79 0a 20 20 2a 2a 20 61  rimarykey.  ** a
2d018 6e 64 20 61 75 74 6f 69 6e 63 2e 20 41 74 20 74  nd autoinc. At t
2d019 68 69 73 20 70 6f 69 6e 74 20 74 68 65 72 65 20  his point there 
2d01a 61 72 65 20 74 77 6f 20 70 6f 73 73 69 62 69 6c  are two possibil
2d01b 69 74 69 65 73 3a 0a 20 20 2a 2a 20 0a 20 20 2a  ities:.  ** .  *
2d01c 2a 20 20 20 20 20 31 2e 20 54 68 65 20 73 70 65  *     1. The spe
2d01d 63 69 66 69 65 64 20 63 6f 6c 75 6d 6e 20 6e 61  cified column na
2d01e 6d 65 20 77 61 73 20 72 6f 77 69 64 22 2c 20 22  me was rowid", "
2d01f 6f 69 64 22 20 6f 72 20 22 5f 72 6f 77 69 64 5f  oid" or "_rowid_
2d020 22 20 0a 20 20 2a 2a 20 20 20 20 20 20 20 20 61  " .  **        a
2d021 6e 64 20 74 68 65 72 65 20 69 73 20 6e 6f 20 65  nd there is no e
2d022 78 70 6c 69 63 69 74 6c 79 20 64 65 63 6c 61 72  xplicitly declar
2d023 65 64 20 49 50 4b 20 63 6f 6c 75 6d 6e 2e 20 0a  ed IPK column. .
2d024 20 20 2a 2a 0a 20 20 2a 2a 20 20 20 20 20 32 2e    **.  **     2.
2d025 20 54 68 65 20 74 61 62 6c 65 20 69 73 20 6e 6f   The table is no
2d026 74 20 61 20 76 69 65 77 20 61 6e 64 20 74 68 65  t a view and the
2d027 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65 20 69 64 65   column name ide
2d028 6e 74 69 66 69 65 64 20 61 6e 20 0a 20 20 2a 2a  ntified an .  **
2d029 20 20 20 20 20 20 20 20 65 78 70 6c 69 63 69 74          explicit
2d02a 6c 79 20 64 65 63 6c 61 72 65 64 20 63 6f 6c 75  ly declared colu
2d02b 6d 6e 2e 20 43 6f 70 79 20 6d 65 74 61 20 69 6e  mn. Copy meta in
2d02c 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 2a  formation from *
2d02d 70 43 6f 6c 2e 0a 20 20 2a 2f 20 0a 20 20 69 66  pCol..  */ .  if
2d02e 28 20 70 43 6f 6c 20 29 7b 0a 20 20 20 20 7a 44  ( pCol ){.    zD
2d02f 61 74 61 54 79 70 65 20 3d 20 70 43 6f 6c 2d 3e  ataType = pCol->
2d030 7a 54 79 70 65 3b 0a 20 20 20 20 7a 43 6f 6c 6c  zType;.    zColl
2d031 53 65 71 20 3d 20 70 43 6f 6c 2d 3e 7a 43 6f 6c  Seq = pCol->zCol
2d032 6c 3b 0a 20 20 20 20 6e 6f 74 6e 75 6c 6c 20 3d  l;.    notnull =
2d033 20 70 43 6f 6c 2d 3e 6e 6f 74 4e 75 6c 6c 21 3d   pCol->notNull!=
2d034 30 3b 0a 20 20 20 20 70 72 69 6d 61 72 79 6b 65  0;.    primaryke
2d035 79 20 20 3d 20 70 43 6f 6c 2d 3e 69 73 50 72 69  y  = pCol->isPri
2d036 6d 4b 65 79 21 3d 30 3b 0a 20 20 20 20 61 75 74  mKey!=0;.    aut
2d037 6f 69 6e 63 20 3d 20 70 54 61 62 2d 3e 69 50 4b  oinc = pTab->iPK
2d038 65 79 3d 3d 69 43 6f 6c 20 26 26 20 28 70 54 61  ey==iCol && (pTa
2d039 62 2d 3e 74 61 62 46 6c 61 67 73 20 26 20 54 46  b->tabFlags & TF
2d03a 5f 41 75 74 6f 69 6e 63 72 65 6d 65 6e 74 29 21  _Autoincrement)!
2d03b 3d 30 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20  =0;.  }else{.   
2d03c 20 7a 44 61 74 61 54 79 70 65 20 3d 20 22 49 4e   zDataType = "IN
2d03d 54 45 47 45 52 22 3b 0a 20 20 20 20 70 72 69 6d  TEGER";.    prim
2d03e 61 72 79 6b 65 79 20 3d 20 31 3b 0a 20 20 7d 0a  arykey = 1;.  }.
2d03f 20 20 69 66 28 20 21 7a 43 6f 6c 6c 53 65 71 20    if( !zCollSeq 
2d040 29 7b 0a 20 20 20 20 7a 43 6f 6c 6c 53 65 71 20  ){.    zCollSeq 
2d041 3d 20 22 42 49 4e 41 52 59 22 3b 0a 20 20 7d 0a  = "BINARY";.  }.
2d042 0a 65 72 72 6f 72 5f 6f 75 74 3a 0a 20 20 28 76  .error_out:.  (v
2d043 6f 69 64 29 73 71 6c 69 74 65 33 53 61 66 65 74  oid)sqlite3Safet
2d044 79 4f 66 66 28 64 62 29 3b 0a 0a 20 20 2f 2a 20  yOff(db);..  /* 
2d045 57 68 65 74 68 65 72 20 74 68 65 20 66 75 6e 63  Whether the func
2d046 74 69 6f 6e 20 63 61 6c 6c 20 73 75 63 63 65 65  tion call succee
2d047 64 65 64 20 6f 72 20 66 61 69 6c 65 64 2c 20 73  ded or failed, s
2d048 65 74 20 74 68 65 20 6f 75 74 70 75 74 20 70 61  et the output pa
2d049 72 61 6d 65 74 65 72 73 0a 20 20 2a 2a 20 74 6f  rameters.  ** to
2d04a 20 77 68 61 74 65 76 65 72 20 74 68 65 69 72 20   whatever their 
2d04b 6c 6f 63 61 6c 20 63 6f 75 6e 74 65 72 70 61 72  local counterpar
2d04c 74 73 20 63 6f 6e 74 61 69 6e 2e 20 49 66 20 61  ts contain. If a
2d04d 6e 20 65 72 72 6f 72 20 64 69 64 20 6f 63 63 75  n error did occu
2d04e 72 2c 0a 20 20 2a 2a 20 74 68 69 73 20 68 61 73  r,.  ** this has
2d04f 20 74 68 65 20 65 66 66 65 63 74 20 6f 66 20 7a   the effect of z
2d050 65 72 6f 69 6e 67 20 61 6c 6c 20 6f 75 74 70 75  eroing all outpu
2d051 74 20 70 61 72 61 6d 65 74 65 72 73 2e 0a 20 20  t parameters..  
2d052 2a 2f 0a 20 20 69 66 28 20 70 7a 44 61 74 61 54  */.  if( pzDataT
2d053 79 70 65 20 29 20 2a 70 7a 44 61 74 61 54 79 70  ype ) *pzDataTyp
2d054 65 20 3d 20 7a 44 61 74 61 54 79 70 65 3b 0a 20  e = zDataType;. 
2d055 20 69 66 28 20 70 7a 43 6f 6c 6c 53 65 71 20 29   if( pzCollSeq )
2d056 20 2a 70 7a 43 6f 6c 6c 53 65 71 20 3d 20 7a 43   *pzCollSeq = zC
2d057 6f 6c 6c 53 65 71 3b 0a 20 20 69 66 28 20 70 4e  ollSeq;.  if( pN
2d058 6f 74 4e 75 6c 6c 20 29 20 2a 70 4e 6f 74 4e 75  otNull ) *pNotNu
2d059 6c 6c 20 3d 20 6e 6f 74 6e 75 6c 6c 3b 0a 20 20  ll = notnull;.  
2d05a 69 66 28 20 70 50 72 69 6d 61 72 79 4b 65 79 20  if( pPrimaryKey 
2d05b 29 20 2a 70 50 72 69 6d 61 72 79 4b 65 79 20 3d  ) *pPrimaryKey =
2d05c 20 70 72 69 6d 61 72 79 6b 65 79 3b 0a 20 20 69   primarykey;.  i
2d05d 66 28 20 70 41 75 74 6f 69 6e 63 20 29 20 2a 70  f( pAutoinc ) *p
2d05e 41 75 74 6f 69 6e 63 20 3d 20 61 75 74 6f 69 6e  Autoinc = autoin
2d05f 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c 49 54 45  c;..  if( SQLITE
2d060 5f 4f 4b 3d 3d 72 63 20 26 26 20 21 70 54 61 62  _OK==rc && !pTab
2d061 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 44   ){.    sqlite3D
2d062 62 46 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73  bFree(db, zErrMs
2d063 67 29 3b 0a 20 20 20 20 7a 45 72 72 4d 73 67 20  g);.    zErrMsg 
2d064 3d 20 73 71 6c 69 74 65 33 4d 50 72 69 6e 74 66  = sqlite3MPrintf
2d065 28 64 62 2c 20 22 6e 6f 20 73 75 63 68 20 74 61  (db, "no such ta
2d066 62 6c 65 20 63 6f 6c 75 6d 6e 3a 20 25 73 2e 25  ble column: %s.%
2d067 73 22 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 2c 0a  s", zTableName,.
2d068 20 20 20 20 20 20 20 20 7a 43 6f 6c 75 6d 6e 4e          zColumnN
2d069 61 6d 65 29 3b 0a 20 20 20 20 72 63 20 3d 20 53  ame);.    rc = S
2d06a 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d  QLITE_ERROR;.  }
2d06b 0a 20 20 73 71 6c 69 74 65 33 45 72 72 6f 72 28  .  sqlite3Error(
2d06c 64 62 2c 20 72 63 2c 20 28 7a 45 72 72 4d 73 67  db, rc, (zErrMsg
2d06d 3f 22 25 73 22 3a 30 29 2c 20 7a 45 72 72 4d 73  ?"%s":0), zErrMs
2d06e 67 29 3b 0a 20 20 73 71 6c 69 74 65 33 44 62 46  g);.  sqlite3DbF
2d06f 72 65 65 28 64 62 2c 20 7a 45 72 72 4d 73 67 29  ree(db, zErrMsg)
2d070 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ;.  rc = sqlite3
2d071 41 70 69 45 78 69 74 28 64 62 2c 20 72 63 29 3b  ApiExit(db, rc);
2d072 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2d073 5f 6c 65 61 76 65 28 64 62 2d 3e 6d 75 74 65 78  _leave(db->mutex
2d074 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
2d075 7d 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  }.#endif../*.** 
2d076 53 6c 65 65 70 20 66 6f 72 20 61 20 6c 69 74 74  Sleep for a litt
2d077 6c 65 20 77 68 69 6c 65 2e 20 20 52 65 74 75 72  le while.  Retur
2d078 6e 20 74 68 65 20 61 6d 6f 75 6e 74 20 6f 66 20  n the amount of 
2d079 74 69 6d 65 20 73 6c 65 70 74 2e 0a 2a 2f 0a 53  time slept..*/.S
2d07a 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
2d07b 6c 69 74 65 33 5f 73 6c 65 65 70 28 69 6e 74 20  lite3_sleep(int 
2d07c 6d 73 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  ms){.  sqlite3_v
2d07d 66 73 20 2a 70 56 66 73 3b 0a 20 20 69 6e 74 20  fs *pVfs;.  int 
2d07e 72 63 3b 0a 20 20 70 56 66 73 20 3d 20 73 71 6c  rc;.  pVfs = sql
2d07f 69 74 65 33 5f 76 66 73 5f 66 69 6e 64 28 30 29  ite3_vfs_find(0)
2d080 3b 0a 20 20 69 66 28 20 70 56 66 73 3d 3d 30 20  ;.  if( pVfs==0 
2d081 29 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 2f  ) return 0;..  /
2d082 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
2d083 77 6f 72 6b 73 20 69 6e 20 6d 69 6c 6c 69 73 65  works in millise
2d084 63 6f 6e 64 73 2c 20 62 75 74 20 74 68 65 20 75  conds, but the u
2d085 6e 64 65 72 6c 79 69 6e 67 20 4f 73 53 6c 65 65  nderlying OsSlee
2d086 70 28 29 20 0a 20 20 2a 2a 20 41 50 49 20 75 73  p() .  ** API us
2d087 65 73 20 6d 69 63 72 6f 73 65 63 6f 6e 64 73 2e  es microseconds.
2d088 20 48 65 6e 63 65 20 74 68 65 20 31 30 30 30 27   Hence the 1000'
2d089 73 2e 0a 20 20 2a 2f 0a 20 20 72 63 20 3d 20 28  s..  */.  rc = (
2d08a 73 71 6c 69 74 65 33 4f 73 53 6c 65 65 70 28 70  sqlite3OsSleep(p
2d08b 56 66 73 2c 20 31 30 30 30 2a 6d 73 29 2f 31 30  Vfs, 1000*ms)/10
2d08c 30 30 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  00);.  return rc
2d08d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 6e 61 62 6c  ;.}../*.** Enabl
2d08e 65 20 6f 72 20 64 69 73 61 62 6c 65 20 74 68 65  e or disable the
2d08f 20 65 78 74 65 6e 64 65 64 20 72 65 73 75 6c 74   extended result
2d090 20 63 6f 64 65 73 2e 0a 2a 2f 0a 53 51 4c 49 54   codes..*/.SQLIT
2d091 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65  E_API int sqlite
2d092 33 5f 65 78 74 65 6e 64 65 64 5f 72 65 73 75 6c  3_extended_resul
2d093 74 5f 63 6f 64 65 73 28 73 71 6c 69 74 65 33 20  t_codes(sqlite3 
2d094 2a 64 62 2c 20 69 6e 74 20 6f 6e 6f 66 66 29 7b  *db, int onoff){
2d095 0a 20 20 73 71 6c 69 74 65 33 5f 6d 75 74 65 78  .  sqlite3_mutex
2d096 5f 65 6e 74 65 72 28 64 62 2d 3e 6d 75 74 65 78  _enter(db->mutex
2d097 29 3b 0a 20 20 64 62 2d 3e 65 72 72 4d 61 73 6b  );.  db->errMask
2d098 20 3d 20 6f 6e 6f 66 66 20 3f 20 30 78 66 66 66   = onoff ? 0xfff
2d099 66 66 66 66 66 20 3a 20 30 78 66 66 3b 0a 20 20  fffff : 0xff;.  
2d09a 73 71 6c 69 74 65 33 5f 6d 75 74 65 78 5f 6c 65  sqlite3_mutex_le
2d09b 61 76 65 28 64 62 2d 3e 6d 75 74 65 78 29 3b 0a  ave(db->mutex);.
2d09c 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2d09d 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 76  OK;.}../*.** Inv
2d09e 6f 6b 65 20 74 68 65 20 78 46 69 6c 65 43 6f 6e  oke the xFileCon
2d09f 74 72 6f 6c 20 6d 65 74 68 6f 64 20 6f 6e 20 61  trol method on a
2d0a0 20 70 61 72 74 69 63 75 6c 61 72 20 64 61 74 61   particular data
2d0a1 62 61 73 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f  base..*/.SQLITE_
2d0a2 41 50 49 20 69 6e 74 20 73 71 6c 69 74 65 33 5f  API int sqlite3_
2d0a3 66 69 6c 65 5f 63 6f 6e 74 72 6f 6c 28 73 71 6c  file_control(sql
2d0a4 69 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20  ite3 *db, const 
2d0a5 63 68 61 72 20 2a 7a 44 62 4e 61 6d 65 2c 20 69  char *zDbName, i
2d0a6 6e 74 20 6f 70 2c 20 76 6f 69 64 20 2a 70 41 72  nt op, void *pAr
2d0a7 67 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  g){.  int rc = S
2d0a8 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69  QLITE_ERROR;.  i
2d0a9 6e 74 20 69 44 62 3b 0a 20 20 73 71 6c 69 74 65  nt iDb;.  sqlite
2d0aa 33 5f 6d 75 74 65 78 5f 65 6e 74 65 72 28 64 62  3_mutex_enter(db
2d0ab 2d 3e 6d 75 74 65 78 29 3b 0a 20 20 69 66 28 20  ->mutex);.  if( 
2d0ac 7a 44 62 4e 61 6d 65 3d 3d 30 20 29 7b 0a 20 20  zDbName==0 ){.  
2d0ad 20 20 69 44 62 20 3d 20 30 3b 0a 20 20 7d 65 6c    iDb = 0;.  }el
2d0ae 73 65 7b 0a 20 20 20 20 66 6f 72 28 69 44 62 3d  se{.    for(iDb=
2d0af 30 3b 20 69 44 62 3c 64 62 2d 3e 6e 44 62 3b 20  0; iDb<db->nDb; 
2d0b0 69 44 62 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66  iDb++){.      if
2d0b1 28 20 73 74 72 63 6d 70 28 64 62 2d 3e 61 44 62  ( strcmp(db->aDb
2d0b2 5b 69 44 62 5d 2e 7a 4e 61 6d 65 2c 20 7a 44 62  [iDb].zName, zDb
2d0b3 4e 61 6d 65 29 3d 3d 30 20 29 20 62 72 65 61 6b  Name)==0 ) break
2d0b4 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 69 66  ;.    }.  }.  if
2d0b5 28 20 69 44 62 3c 64 62 2d 3e 6e 44 62 20 29 7b  ( iDb<db->nDb ){
2d0b6 0a 20 20 20 20 42 74 72 65 65 20 2a 70 42 74 72  .    Btree *pBtr
2d0b7 65 65 20 3d 20 64 62 2d 3e 61 44 62 5b 69 44 62  ee = db->aDb[iDb
2d0b8 5d 2e 70 42 74 3b 0a 20 20 20 20 69 66 28 20 70  ].pBt;.    if( p
2d0b9 42 74 72 65 65 20 29 7b 0a 20 20 20 20 20 20 50  Btree ){.      P
2d0ba 61 67 65 72 20 2a 70 50 61 67 65 72 3b 0a 20 20  ager *pPager;.  
2d0bb 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6c 65      sqlite3_file
2d0bc 20 2a 66 64 3b 0a 20 20 20 20 20 20 73 71 6c 69   *fd;.      sqli
2d0bd 74 65 33 42 74 72 65 65 45 6e 74 65 72 28 70 42  te3BtreeEnter(pB
2d0be 74 72 65 65 29 3b 0a 20 20 20 20 20 20 70 50 61  tree);.      pPa
2d0bf 67 65 72 20 3d 20 73 71 6c 69 74 65 33 42 74 72  ger = sqlite3Btr
2d0c0 65 65 50 61 67 65 72 28 70 42 74 72 65 65 29 3b  eePager(pBtree);
2d0c1 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 70  .      assert( p
2d0c2 50 61 67 65 72 21 3d 30 20 29 3b 0a 20 20 20 20  Pager!=0 );.    
2d0c3 20 20 66 64 20 3d 20 73 71 6c 69 74 65 33 50 61    fd = sqlite3Pa
2d0c4 67 65 72 46 69 6c 65 28 70 50 61 67 65 72 29 3b  gerFile(pPager);
2d0c5 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 66  .      assert( f
2d0c6 64 21 3d 30 20 29 3b 0a 20 20 20 20 20 20 69 66  d!=0 );.      if
2d0c7 28 20 66 64 2d 3e 70 4d 65 74 68 6f 64 73 20 29  ( fd->pMethods )
2d0c8 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 73  {.        rc = s
2d0c9 71 6c 69 74 65 33 4f 73 46 69 6c 65 43 6f 6e 74  qlite3OsFileCont
2d0ca 72 6f 6c 28 66 64 2c 20 6f 70 2c 20 70 41 72 67  rol(fd, op, pArg
2d0cb 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
2d0cc 20 73 71 6c 69 74 65 33 42 74 72 65 65 4c 65 61   sqlite3BtreeLea
2d0cd 76 65 28 70 42 74 72 65 65 29 3b 0a 20 20 20 20  ve(pBtree);.    
2d0ce 7d 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33 5f  }.  }.  sqlite3_
2d0cf 6d 75 74 65 78 5f 6c 65 61 76 65 28 64 62 2d 3e  mutex_leave(db->
2d0d0 6d 75 74 65 78 29 3b 0a 20 20 72 65 74 75 72 6e  mutex);.  return
2d0d1 20 72 63 3b 20 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a   rc;   .}../*.**
2d0d2 20 49 6e 74 65 72 66 61 63 65 20 74 6f 20 74 68   Interface to th
2d0d3 65 20 74 65 73 74 69 6e 67 20 6c 6f 67 69 63 2e  e testing logic.
2d0d4 0a 2a 2f 0a 53 51 4c 49 54 45 5f 41 50 49 20 69  .*/.SQLITE_API i
2d0d5 6e 74 20 73 71 6c 69 74 65 33 5f 74 65 73 74 5f  nt sqlite3_test_
2d0d6 63 6f 6e 74 72 6f 6c 28 69 6e 74 20 6f 70 2c 20  control(int op, 
2d0d7 2e 2e 2e 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  ...){.  int rc =
2d0d8 20 30 3b 0a 23 69 66 6e 64 65 66 20 53 51 4c 49   0;.#ifndef SQLI
2d0d9 54 45 5f 4f 4d 49 54 5f 42 55 49 4c 54 49 4e 5f  TE_OMIT_BUILTIN_
2d0da 54 45 53 54 0a 20 20 76 61 5f 6c 69 73 74 20 61  TEST.  va_list a
2d0db 70 3b 0a 20 20 76 61 5f 73 74 61 72 74 28 61 70  p;.  va_start(ap
2d0dc 2c 20 6f 70 29 3b 0a 20 20 73 77 69 74 63 68 28  , op);.  switch(
2d0dd 20 6f 70 20 29 7b 0a 0a 20 20 20 20 2f 2a 0a 20   op ){..    /*. 
2d0de 20 20 20 2a 2a 20 53 61 76 65 20 74 68 65 20 63     ** Save the c
2d0df 75 72 72 65 6e 74 20 73 74 61 74 65 20 6f 66 20  urrent state of 
2d0e0 74 68 65 20 50 52 4e 47 2e 0a 20 20 20 20 2a 2f  the PRNG..    */
2d0e1 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49 54 45  .    case SQLITE
2d0e2 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47 5f 53  _TESTCTRL_PRNG_S
2d0e3 41 56 45 3a 20 7b 0a 20 20 20 20 20 20 73 71 6c  AVE: {.      sql
2d0e4 69 74 65 33 50 72 6e 67 53 61 76 65 53 74 61 74  ite3PrngSaveStat
2d0e5 65 28 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  e();.      break
2d0e6 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a  ;.    }..    /*.
2d0e7 20 20 20 20 2a 2a 20 52 65 73 74 6f 72 65 20 74      ** Restore t
2d0e8 68 65 20 73 74 61 74 65 20 6f 66 20 74 68 65 20  he state of the 
2d0e9 50 52 4e 47 20 74 6f 20 74 68 65 20 6c 61 73 74  PRNG to the last
2d0ea 20 73 74 61 74 65 20 73 61 76 65 64 20 75 73 69   state saved usi
2d0eb 6e 67 0a 20 20 20 20 2a 2a 20 50 52 4e 47 5f 53  ng.    ** PRNG_S
2d0ec 41 56 45 2e 20 20 49 66 20 50 52 4e 47 5f 53 41  AVE.  If PRNG_SA
2d0ed 56 45 20 68 61 73 20 6e 65 76 65 72 20 62 65 66  VE has never bef
2d0ee 6f 72 65 20 62 65 65 6e 20 63 61 6c 6c 65 64 2c  ore been called,
2d0ef 20 74 68 65 6e 0a 20 20 20 20 2a 2a 20 74 68 69   then.    ** thi
2d0f0 73 20 76 65 72 62 20 61 63 74 73 20 6c 69 6b 65  s verb acts like
2d0f1 20 50 52 4e 47 5f 52 45 53 45 54 2e 0a 20 20 20   PRNG_RESET..   
2d0f2 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
2d0f3 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e  ITE_TESTCTRL_PRN
2d0f4 47 5f 52 45 53 54 4f 52 45 3a 20 7b 0a 20 20 20  G_RESTORE: {.   
2d0f5 20 20 20 73 71 6c 69 74 65 33 50 72 6e 67 52 65     sqlite3PrngRe
2d0f6 73 74 6f 72 65 53 74 61 74 65 28 29 3b 0a 20 20  storeState();.  
2d0f7 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d      break;.    }
2d0f8 0a 0a 20 20 20 20 2f 2a 0a 20 20 20 20 2a 2a 20  ..    /*.    ** 
2d0f9 52 65 73 65 74 20 74 68 65 20 50 52 4e 47 20 62  Reset the PRNG b
2d0fa 61 63 6b 20 74 6f 20 69 74 73 20 75 6e 69 6e 69  ack to its unini
2d0fb 74 69 61 6c 69 7a 65 64 20 73 74 61 74 65 2e 20  tialized state. 
2d0fc 20 54 68 65 20 6e 65 78 74 20 63 61 6c 6c 0a 20   The next call. 
2d0fd 20 20 20 2a 2a 20 74 6f 20 73 71 6c 69 74 65 33     ** to sqlite3
2d0fe 5f 72 61 6e 64 6f 6d 6e 65 73 73 28 29 20 77 69  _randomness() wi
2d0ff 6c 6c 20 72 65 73 65 65 64 20 74 68 65 20 50 52  ll reseed the PR
2d100 4e 47 20 75 73 69 6e 67 20 61 20 73 69 6e 67 6c  NG using a singl
2d101 65 20 63 61 6c 6c 0a 20 20 20 20 2a 2a 20 74 6f  e call.    ** to
2d102 20 74 68 65 20 78 52 61 6e 64 6f 6d 6e 65 73 73   the xRandomness
2d103 20 6d 65 74 68 6f 64 20 6f 66 20 74 68 65 20 64   method of the d
2d104 65 66 61 75 6c 74 20 56 46 53 2e 0a 20 20 20 20  efault VFS..    
2d105 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c 49  */.    case SQLI
2d106 54 45 5f 54 45 53 54 43 54 52 4c 5f 50 52 4e 47  TE_TESTCTRL_PRNG
2d107 5f 52 45 53 45 54 3a 20 7b 0a 20 20 20 20 20 20  _RESET: {.      
2d108 73 71 6c 69 74 65 33 50 72 6e 67 52 65 73 65 74  sqlite3PrngReset
2d109 53 74 61 74 65 28 29 3b 0a 20 20 20 20 20 20 62  State();.      b
2d10a 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
2d10b 20 2f 2a 0a 20 20 20 20 2a 2a 20 20 73 71 6c 69   /*.    **  sqli
2d10c 74 65 33 5f 74 65 73 74 5f 63 6f 6e 74 72 6f 6c  te3_test_control
2d10d 28 42 49 54 56 45 43 5f 54 45 53 54 2c 20 73 69  (BITVEC_TEST, si
2d10e 7a 65 2c 20 70 72 6f 67 72 61 6d 29 0a 20 20 20  ze, program).   
2d10f 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 75 6e 20 61   **.    ** Run a
2d110 20 74 65 73 74 20 61 67 61 69 6e 73 74 20 61 20   test against a 
2d111 42 69 74 76 65 63 20 6f 62 6a 65 63 74 20 6f 66  Bitvec object of
2d112 20 73 69 7a 65 2e 20 20 54 68 65 20 70 72 6f 67   size.  The prog
2d113 72 61 6d 20 61 72 67 75 6d 65 6e 74 0a 20 20 20  ram argument.   
2d114 20 2a 2a 20 69 73 20 61 6e 20 61 72 72 61 79 20   ** is an array 
2d115 6f 66 20 69 6e 74 65 67 65 72 73 20 74 68 61 74  of integers that
2d116 20 64 65 66 69 6e 65 73 20 74 68 65 20 74 65 73   defines the tes
2d117 74 2e 20 20 52 65 74 75 72 6e 20 2d 31 20 6f 6e  t.  Return -1 on
2d118 20 61 0a 20 20 20 20 2a 2a 20 6d 65 6d 6f 72 79   a.    ** memory
2d119 20 61 6c 6c 6f 63 61 74 69 6f 6e 20 65 72 72 6f   allocation erro
2d11a 72 2c 20 30 20 6f 6e 20 73 75 63 63 65 73 73 2c  r, 0 on success,
2d11b 20 6f 72 20 6e 6f 6e 2d 7a 65 72 6f 20 66 6f 72   or non-zero for
2d11c 20 61 6e 20 65 72 72 6f 72 2e 0a 20 20 20 20 2a   an error..    *
2d11d 2a 20 53 65 65 20 74 68 65 20 73 71 6c 69 74 65  * See the sqlite
2d11e 33 42 69 74 76 65 63 42 75 69 6c 74 69 6e 54 65  3BitvecBuiltinTe
2d11f 73 74 28 29 20 66 6f 72 20 61 64 64 69 74 69 6f  st() for additio
2d120 6e 61 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  nal information.
2d121 0a 20 20 20 20 2a 2f 0a 20 20 20 20 63 61 73 65  .    */.    case
2d122 20 53 51 4c 49 54 45 5f 54 45 53 54 43 54 52 4c   SQLITE_TESTCTRL
2d123 5f 42 49 54 56 45 43 5f 54 45 53 54 3a 20 7b 0a  _BITVEC_TEST: {.
2d124 20 20 20 20 20 20 69 6e 74 20 73 7a 20 3d 20 76        int sz = v
2d125 61 5f 61 72 67 28 61 70 2c 20 69 6e 74 29 3b 0a  a_arg(ap, int);.
2d126 20 20 20 20 20 20 69 6e 74 20 2a 61 50 72 6f 67        int *aProg
2d127 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 69 6e   = va_arg(ap, in
2d128 74 2a 29 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  t*);.      rc = 
2d129 73 71 6c 69 74 65 33 42 69 74 76 65 63 42 75 69  sqlite3BitvecBui
2d12a 6c 74 69 6e 54 65 73 74 28 73 7a 2c 20 61 50 72  ltinTest(sz, aPr
2d12b 6f 67 29 3b 0a 20 20 20 20 20 20 62 72 65 61 6b  og);.      break
2d12c 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 0a  ;.    }..    /*.
2d12d 20 20 20 20 2a 2a 20 20 73 71 6c 69 74 65 33 5f      **  sqlite3_
2d12e 74 65 73 74 5f 63 6f 6e 74 72 6f 6c 28 42 45 4e  test_control(BEN
2d12f 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53  IGN_MALLOC_HOOKS
2d130 2c 20 78 42 65 67 69 6e 2c 20 78 45 6e 64 29 0a  , xBegin, xEnd).
2d131 20 20 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 52 65      **.    ** Re
2d132 67 69 73 74 65 72 20 68 6f 6f 6b 73 20 74 6f 20  gister hooks to 
2d133 63 61 6c 6c 20 74 6f 20 69 6e 64 69 63 61 74 65  call to indicate
2d134 20 77 68 69 63 68 20 6d 61 6c 6c 6f 63 28 29 20   which malloc() 
2d135 66 61 69 6c 75 72 65 73 20 0a 20 20 20 20 2a 2a  failures .    **
2d136 20 61 72 65 20 62 65 6e 69 67 6e 2e 0a 20 20 20   are benign..   
2d137 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 53 51 4c   */.    case SQL
2d138 49 54 45 5f 54 45 53 54 43 54 52 4c 5f 42 45 4e  ITE_TESTCTRL_BEN
2d139 49 47 4e 5f 4d 41 4c 4c 4f 43 5f 48 4f 4f 4b 53  IGN_MALLOC_HOOKS
2d13a 3a 20 7b 0a 20 20 20 20 20 20 74 79 70 65 64 65  : {.      typede
2d13b 66 20 76 6f 69 64 20 28 2a 76 6f 69 64 5f 66 75  f void (*void_fu
2d13c 6e 63 74 69 6f 6e 29 28 76 6f 69 64 29 3b 0a 20  nction)(void);. 
2d13d 20 20 20 20 20 76 6f 69 64 5f 66 75 6e 63 74 69       void_functi
2d13e 6f 6e 20 78 42 65 6e 69 67 6e 42 65 67 69 6e 3b  on xBenignBegin;
2d13f 0a 20 20 20 20 20 20 76 6f 69 64 5f 66 75 6e 63  .      void_func
2d140 74 69 6f 6e 20 78 42 65 6e 69 67 6e 45 6e 64 3b  tion xBenignEnd;
2d141 0a 20 20 20 20 20 20 78 42 65 6e 69 67 6e 42 65  .      xBenignBe
2d142 67 69 6e 20 3d 20 76 61 5f 61 72 67 28 61 70 2c  gin = va_arg(ap,
2d143 20 76 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b   void_function);
2d144 0a 20 20 20 20 20 20 78 42 65 6e 69 67 6e 45 6e  .      xBenignEn
2d145 64 20 3d 20 76 61 5f 61 72 67 28 61 70 2c 20 76  d = va_arg(ap, v
2d146 6f 69 64 5f 66 75 6e 63 74 69 6f 6e 29 3b 0a 20  oid_function);. 
2d147 20 20 20 20 20 73 71 6c 69 74 65 33 42 65 6e 69       sqlite3Beni
2d148 67 6e 4d 61 6c 6c 6f 63 48 6f 6f 6b 73 28 78 42  gnMallocHooks(xB
2d149 65 6e 69 67 6e 42 65 67 69 6e 2c 20 78 42 65 6e  enignBegin, xBen
2d14a 69 67 6e 45 6e 64 29 3b 0a 20 20 20 20 20 20 62  ignEnd);.      b
2d14b 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  reak;.    }.  }.
2d14c 20 20 76 61 5f 65 6e 64 28 61 70 29 3b 0a 23 65    va_end(ap);.#e
2d14d 6e 64 69 66 20 2f 2a 20 53 51 4c 49 54 45 5f 4f  ndif /* SQLITE_O
2d14e 4d 49 54 5f 42 55 49 4c 54 49 4e 5f 54 45 53 54  MIT_BUILTIN_TEST
2d14f 20 2a 2f 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   */.  return rc;
2d150 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .}../***********
2d151 2a 2a 2a 20 45 6e 64 20 6f 66 20 6d 61 69 6e 2e  *** End of main.
2d152 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
2d153 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d154 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d155 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  **/./***********
2d156 2a 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66  *** Begin file f
2d157 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ts3.c **********
2d158 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d159 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d15a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 4f  **/./*.** 2006 O
2d15b 63 74 20 31 30 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ct 10.**.** The 
2d15c 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
2d15d 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
2d15e 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
2d15f 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
2d160 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
2d161 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
2d162 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
2d163 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
2d164 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
2d165 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
2d166 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
2d167 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
2d168 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
2d169 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
2d16a 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
2d16b 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
2d16c 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
2d16d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d16e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d16f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d170 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d171 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 0a 2a 2a 20  *********.**.** 
2d172 54 68 69 73 20 69 73 20 61 6e 20 53 51 4c 69 74  This is an SQLit
2d173 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65 6d 65  e module impleme
2d174 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78 74 20  nting full-text 
2d175 73 65 61 72 63 68 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a  search..*/../*.*
2d176 2a 20 54 68 65 20 63 6f 64 65 20 69 6e 20 74 68  * The code in th
2d177 69 73 20 66 69 6c 65 20 69 73 20 6f 6e 6c 79 20  is file is only 
2d178 63 6f 6d 70 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a  compiled if:.**.
2d179 2a 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53  **     * The FTS
2d17a 33 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e  3 module is bein
2d17b 67 20 62 75 69 6c 74 20 61 73 20 61 6e 20 65 78  g built as an ex
2d17c 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20 20 20 20 20  tension.**      
2d17d 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20   (in which case 
2d17e 53 51 4c 49 54 45 5f 43 4f 52 45 20 69 73 20 6e  SQLITE_CORE is n
2d17f 6f 74 20 64 65 66 69 6e 65 64 29 2c 20 6f 72 0a  ot defined), or.
2d180 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65 20  **.**     * The 
2d181 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20 62  FTS3 module is b
2d182 65 69 6e 67 20 62 75 69 6c 74 20 69 6e 74 6f 20  eing built into 
2d183 74 68 65 20 63 6f 72 65 20 6f 66 0a 2a 2a 20 20  the core of.**  
2d184 20 20 20 20 20 53 51 4c 69 74 65 20 28 69 6e 20       SQLite (in 
2d185 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54  which case SQLIT
2d186 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 20 69 73  E_ENABLE_FTS3 is
2d187 20 64 65 66 69 6e 65 64 29 2e 0a 2a 2f 0a 0a 2f   defined)..*/../
2d188 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 43 6f  * TODO(shess) Co
2d189 6e 73 69 64 65 72 20 65 78 70 6f 72 74 69 6e 67  nsider exporting
2d18a 20 74 68 69 73 20 63 6f 6d 6d 65 6e 74 20 74 6f   this comment to
2d18b 20 61 6e 20 48 54 4d 4c 20 66 69 6c 65 20 6f 72   an HTML file or
2d18c 20 74 68 65 0a 2a 2a 20 77 69 6b 69 2e 0a 2a 2f   the.** wiki..*/
2d18d 0a 2f 2a 20 54 68 65 20 66 75 6c 6c 2d 74 65 78  ./* The full-tex
2d18e 74 20 69 6e 64 65 78 20 69 73 20 73 74 6f 72 65  t index is store
2d18f 64 20 69 6e 20 61 20 73 65 72 69 65 73 20 6f 66  d in a series of
2d190 20 62 2b 74 72 65 65 20 28 2d 6c 69 6b 65 29 0a   b+tree (-like).
2d191 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 63 61  ** structures ca
2d192 6c 6c 65 64 20 73 65 67 6d 65 6e 74 73 20 77 68  lled segments wh
2d193 69 63 68 20 6d 61 70 20 74 65 72 6d 73 20 74 6f  ich map terms to
2d194 20 64 6f 63 6c 69 73 74 73 2e 20 20 54 68 65 0a   doclists.  The.
2d195 2a 2a 20 73 74 72 75 63 74 75 72 65 73 20 61 72  ** structures ar
2d196 65 20 6c 69 6b 65 20 62 2b 74 72 65 65 73 20 69  e like b+trees i
2d197 6e 20 6c 61 79 6f 75 74 2c 20 62 75 74 20 61 72  n layout, but ar
2d198 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  e constructed fr
2d199 6f 6d 20 74 68 65 0a 2a 2a 20 62 6f 74 74 6f 6d  om the.** bottom
2d19a 20 75 70 20 69 6e 20 6f 70 74 69 6d 61 6c 20 66   up in optimal f
2d19b 61 73 68 69 6f 6e 20 61 6e 64 20 61 72 65 20 6e  ashion and are n
2d19c 6f 74 20 75 70 64 61 74 61 62 6c 65 2e 20 20 53  ot updatable.  S
2d19d 69 6e 63 65 20 74 72 65 65 73 0a 2a 2a 20 61 72  ince trees.** ar
2d19e 65 20 62 75 69 6c 74 20 66 72 6f 6d 20 74 68 65  e built from the
2d19f 20 62 6f 74 74 6f 6d 20 75 70 2c 20 74 68 69 6e   bottom up, thin
2d1a0 67 73 20 77 69 6c 6c 20 62 65 20 64 65 73 63 72  gs will be descr
2d1a1 69 62 65 64 20 66 72 6f 6d 20 74 68 65 0a 2a 2a  ibed from the.**
2d1a2 20 62 6f 74 74 6f 6d 20 75 70 2e 0a 2a 2a 0a 2a   bottom up..**.*
2d1a3 2a 0a 2a 2a 2a 2a 20 56 61 72 69 6e 74 73 20 2a  *.**** Varints *
2d1a4 2a 2a 2a 0a 2a 2a 20 54 68 65 20 62 61 73 69 63  ***.** The basic
2d1a5 20 75 6e 69 74 20 6f 66 20 65 6e 63 6f 64 69 6e   unit of encodin
2d1a6 67 20 69 73 20 61 20 76 61 72 69 61 62 6c 65 2d  g is a variable-
2d1a7 6c 65 6e 67 74 68 20 69 6e 74 65 67 65 72 20 63  length integer c
2d1a8 61 6c 6c 65 64 20 61 0a 2a 2a 20 76 61 72 69 6e  alled a.** varin
2d1a9 74 2e 20 20 57 65 20 65 6e 63 6f 64 65 20 76 61  t.  We encode va
2d1aa 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20 69 6e  riable-length in
2d1ab 74 65 67 65 72 73 20 69 6e 20 6c 69 74 74 6c 65  tegers in little
2d1ac 2d 65 6e 64 69 61 6e 20 6f 72 64 65 72 0a 2a 2a  -endian order.**
2d1ad 20 75 73 69 6e 67 20 73 65 76 65 6e 20 62 69 74   using seven bit
2d1ae 73 20 2a 20 70 65 72 20 62 79 74 65 20 61 73 20  s * per byte as 
2d1af 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 4b  follows:.**.** K
2d1b0 45 59 3a 0a 2a 2a 20 20 20 20 20 20 20 20 20 41  EY:.**         A
2d1b1 20 3d 20 30 78 78 78 78 78 78 78 20 20 20 20 37   = 0xxxxxxx    7
2d1b2 20 62 69 74 73 20 6f 66 20 64 61 74 61 20 61 6e   bits of data an
2d1b3 64 20 6f 6e 65 20 66 6c 61 67 20 62 69 74 0a 2a  d one flag bit.*
2d1b4 2a 20 20 20 20 20 20 20 20 20 42 20 3d 20 31 78  *         B = 1x
2d1b5 78 78 78 78 78 78 20 20 20 20 37 20 62 69 74 73  xxxxxx    7 bits
2d1b6 20 6f 66 20 64 61 74 61 20 61 6e 64 20 6f 6e 65   of data and one
2d1b7 20 66 6c 61 67 20 62 69 74 0a 2a 2a 0a 2a 2a 20   flag bit.**.** 
2d1b8 20 37 20 62 69 74 73 20 2d 20 41 0a 2a 2a 20 31   7 bits - A.** 1
2d1b9 34 20 62 69 74 73 20 2d 20 42 41 0a 2a 2a 20 32  4 bits - BA.** 2
2d1ba 31 20 62 69 74 73 20 2d 20 42 42 41 0a 2a 2a 20  1 bits - BBA.** 
2d1bb 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
2d1bc 20 54 68 69 73 20 69 73 20 69 64 65 6e 74 69 63   This is identic
2d1bd 61 6c 20 74 6f 20 68 6f 77 20 73 71 6c 69 74 65  al to how sqlite
2d1be 20 65 6e 63 6f 64 65 73 20 76 61 72 69 6e 74 73   encodes varints
2d1bf 20 28 73 65 65 20 75 74 69 6c 2e 63 29 2e 0a 2a   (see util.c)..*
2d1c0 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 44 6f 63 75 6d 65  *.**.**** Docume
2d1c1 6e 74 20 6c 69 73 74 73 20 2a 2a 2a 2a 0a 2a 2a  nt lists ****.**
2d1c2 20 41 20 64 6f 63 6c 69 73 74 20 28 64 6f 63 75   A doclist (docu
2d1c3 6d 65 6e 74 20 6c 69 73 74 29 20 68 6f 6c 64 73  ment list) holds
2d1c4 20 61 20 64 6f 63 69 64 2d 73 6f 72 74 65 64 20   a docid-sorted 
2d1c5 6c 69 73 74 20 6f 66 20 68 69 74 73 20 66 6f 72  list of hits for
2d1c6 20 61 0a 2a 2a 20 67 69 76 65 6e 20 74 65 72 6d   a.** given term
2d1c7 2e 20 20 44 6f 63 6c 69 73 74 73 20 68 6f 6c 64  .  Doclists hold
2d1c8 20 64 6f 63 69 64 73 2c 20 61 6e 64 20 63 61 6e   docids, and can
2d1c9 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 61 73 73 6f   optionally asso
2d1ca 63 69 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 20 70  ciate.** token p
2d1cb 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66 66  ositions and off
2d1cc 73 65 74 73 20 77 69 74 68 20 64 6f 63 69 64 73  sets with docids
2d1cd 2e 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f 53  ..**.** A DL_POS
2d1ce 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 64  ITIONS_OFFSETS d
2d1cf 6f 63 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64  oclist is stored
2d1d0 20 6c 69 6b 65 20 74 68 69 73 3a 0a 2a 2a 0a 2a   like this:.**.*
2d1d1 2a 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 76  * array {.**   v
2d1d2 61 72 69 6e 74 20 64 6f 63 69 64 3b 0a 2a 2a 20  arint docid;.** 
2d1d3 20 20 61 72 72 61 79 20 7b 20 20 20 20 20 20 20    array {       
2d1d4 20 20 20 20 20 20 20 20 20 28 70 6f 73 69 74 69           (positi
2d1d5 6f 6e 20 6c 69 73 74 20 66 6f 72 20 63 6f 6c 75  on list for colu
2d1d6 6d 6e 20 30 29 0a 2a 2a 20 20 20 20 20 76 61 72  mn 0).**     var
2d1d7 69 6e 74 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20  int position;   
2d1d8 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20 70 72    (delta from pr
2d1d9 65 76 69 6f 75 73 20 70 6f 73 69 74 69 6f 6e 20  evious position 
2d1da 70 6c 75 73 20 50 4f 53 5f 42 41 53 45 29 0a 2a  plus POS_BASE).*
2d1db 2a 20 20 20 20 20 76 61 72 69 6e 74 20 73 74 61  *     varint sta
2d1dc 72 74 4f 66 66 73 65 74 3b 20 20 28 64 65 6c 74  rtOffset;  (delt
2d1dd 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75 73 20  a from previous 
2d1de 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20  startOffset).** 
2d1df 20 20 20 20 76 61 72 69 6e 74 20 65 6e 64 4f 66      varint endOf
2d1e0 66 73 65 74 3b 20 20 20 20 28 64 65 6c 74 61 20  fset;    (delta 
2d1e1 66 72 6f 6d 20 73 74 61 72 74 4f 66 66 73 65 74  from startOffset
2d1e2 29 0a 2a 2a 20 20 20 7d 0a 2a 2a 20 20 20 61 72  ).**   }.**   ar
2d1e3 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 76 61 72  ray {.**     var
2d1e4 69 6e 74 20 50 4f 53 5f 43 4f 4c 55 4d 4e 3b 20  int POS_COLUMN; 
2d1e5 20 20 28 6d 61 72 6b 73 20 73 74 61 72 74 20 6f    (marks start o
2d1e6 66 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 20  f position list 
2d1e7 66 6f 72 20 6e 65 77 20 63 6f 6c 75 6d 6e 29 0a  for new column).
2d1e8 2a 2a 20 20 20 20 20 76 61 72 69 6e 74 20 63 6f  **     varint co
2d1e9 6c 75 6d 6e 3b 20 20 20 20 20 20 20 28 69 6e 64  lumn;       (ind
2d1ea 65 78 20 6f 66 20 6e 65 77 20 63 6f 6c 75 6d 6e  ex of new column
2d1eb 29 0a 2a 2a 20 20 20 20 20 61 72 72 61 79 20 7b  ).**     array {
2d1ec 0a 2a 2a 20 20 20 20 20 20 20 76 61 72 69 6e 74  .**       varint
2d1ed 20 70 6f 73 69 74 69 6f 6e 3b 20 20 20 28 64 65   position;   (de
2d1ee 6c 74 61 20 66 72 6f 6d 20 70 72 65 76 69 6f 75  lta from previou
2d1ef 73 20 70 6f 73 69 74 69 6f 6e 20 70 6c 75 73 20  s position plus 
2d1f0 50 4f 53 5f 42 41 53 45 29 0a 2a 2a 20 20 20 20  POS_BASE).**    
2d1f1 20 20 20 76 61 72 69 6e 74 20 73 74 61 72 74 4f     varint startO
2d1f2 66 66 73 65 74 3b 28 64 65 6c 74 61 20 66 72 6f  ffset;(delta fro
2d1f3 6d 20 70 72 65 76 69 6f 75 73 20 73 74 61 72 74  m previous start
2d1f4 4f 66 66 73 65 74 29 0a 2a 2a 20 20 20 20 20 20  Offset).**      
2d1f5 20 76 61 72 69 6e 74 20 65 6e 64 4f 66 66 73 65   varint endOffse
2d1f6 74 3b 20 20 28 64 65 6c 74 61 20 66 72 6f 6d 20  t;  (delta from 
2d1f7 73 74 61 72 74 4f 66 66 73 65 74 29 0a 2a 2a 20  startOffset).** 
2d1f8 20 20 20 20 7d 0a 2a 2a 20 20 20 7d 0a 2a 2a 20      }.**   }.** 
2d1f9 20 20 76 61 72 69 6e 74 20 50 4f 53 5f 45 4e 44    varint POS_END
2d1fa 3b 20 20 20 20 20 20 20 20 28 6d 61 72 6b 73 20  ;        (marks 
2d1fb 65 6e 64 20 6f 66 20 70 6f 73 69 74 69 6f 6e 73  end of positions
2d1fc 20 66 6f 72 20 74 68 69 73 20 64 6f 63 75 6d 65   for this docume
2d1fd 6e 74 2e 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48  nt..** }.**.** H
2d1fe 65 72 65 2c 20 61 72 72 61 79 20 7b 20 58 20 7d  ere, array { X }
2d1ff 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f 72 20 6d   means zero or m
2d200 6f 72 65 20 6f 63 63 75 72 72 65 6e 63 65 73 20  ore occurrences 
2d201 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e 74 20 69  of X, adjacent i
2d202 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 20 20 41 20  n.** memory.  A 
2d203 22 70 6f 73 69 74 69 6f 6e 22 20 69 73 20 61 6e  "position" is an
2d204 20 69 6e 64 65 78 20 6f 66 20 61 20 74 6f 6b 65   index of a toke
2d205 6e 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e 20 73  n in the token s
2d206 74 72 65 61 6d 0a 2a 2a 20 67 65 6e 65 72 61 74  tream.** generat
2d207 65 64 20 62 79 20 74 68 65 20 74 6f 6b 65 6e 69  ed by the tokeni
2d208 7a 65 72 2c 20 77 68 69 6c 65 20 61 6e 20 22 6f  zer, while an "o
2d209 66 66 73 65 74 22 20 69 73 20 61 20 62 79 74 65  ffset" is a byte
2d20a 20 6f 66 66 73 65 74 2c 0a 2a 2a 20 62 6f 74 68   offset,.** both
2d20b 20 62 61 73 65 64 20 61 74 20 30 2e 20 20 4e 6f   based at 0.  No
2d20c 74 65 20 74 68 61 74 20 50 4f 53 5f 45 4e 44 20  te that POS_END 
2d20d 61 6e 64 20 50 4f 53 5f 43 4f 4c 55 4d 4e 20 6f  and POS_COLUMN o
2d20e 63 63 75 72 20 69 6e 20 74 68 65 0a 2a 2a 20 73  ccur in the.** s
2d20f 61 6d 65 20 6c 6f 67 69 63 61 6c 20 70 6c 61 63  ame logical plac
2d210 65 20 61 73 20 74 68 65 20 70 6f 73 69 74 69 6f  e as the positio
2d211 6e 20 65 6c 65 6d 65 6e 74 2c 20 61 6e 64 20 61  n element, and a
2d212 63 74 20 61 73 20 73 65 6e 74 69 6e 61 6c 73 0a  ct as sentinals.
2d213 2a 2a 20 65 6e 64 69 6e 67 20 61 20 70 6f 73 69  ** ending a posi
2d214 74 69 6f 6e 20 6c 69 73 74 20 61 72 72 61 79 2e  tion list array.
2d215 0a 2a 2a 0a 2a 2a 20 41 20 44 4c 5f 50 4f 53 49  .**.** A DL_POSI
2d216 54 49 4f 4e 53 20 64 6f 63 6c 69 73 74 20 6f 6d  TIONS doclist om
2d217 69 74 73 20 74 68 65 20 73 74 61 72 74 4f 66 66  its the startOff
2d218 73 65 74 20 61 6e 64 20 65 6e 64 4f 66 66 73 65  set and endOffse
2d219 74 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  t.** information
2d21a 2e 20 20 41 20 44 4c 5f 44 4f 43 49 44 53 20 64  .  A DL_DOCIDS d
2d21b 6f 63 6c 69 73 74 20 6f 6d 69 74 73 20 62 6f 74  oclist omits bot
2d21c 68 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 20 61  h the position a
2d21d 6e 64 0a 2a 2a 20 6f 66 66 73 65 74 20 69 6e 66  nd.** offset inf
2d21e 6f 72 6d 61 74 69 6f 6e 2c 20 62 65 63 6f 6d 69  ormation, becomi
2d21f 6e 67 20 61 6e 20 61 72 72 61 79 20 6f 66 20 76  ng an array of v
2d220 61 72 69 6e 74 2d 65 6e 63 6f 64 65 64 20 64 6f  arint-encoded do
2d221 63 69 64 73 2e 0a 2a 2a 0a 2a 2a 20 4f 6e 2d 64  cids..**.** On-d
2d222 69 73 6b 20 64 61 74 61 20 69 73 20 73 74 6f 72  isk data is stor
2d223 65 64 20 61 73 20 74 79 70 65 20 44 4c 5f 44 45  ed as type DL_DE
2d224 46 41 55 4c 54 2c 20 73 6f 20 77 65 20 64 6f 6e  FAULT, so we don
2d225 27 74 20 73 65 72 69 61 6c 69 7a 65 0a 2a 2a 20  't serialize.** 
2d226 74 68 65 20 74 79 70 65 2e 20 20 44 75 65 20 74  the type.  Due t
2d227 6f 20 68 6f 77 20 64 65 6c 65 74 69 6f 6e 20 69  o how deletion i
2d228 73 20 69 6d 70 6c 65 6d 65 6e 74 65 64 20 69 6e  s implemented in
2d229 20 74 68 65 20 73 65 67 6d 65 6e 74 61 74 69 6f   the segmentatio
2d22a 6e 0a 2a 2a 20 73 79 73 74 65 6d 2c 20 6f 6e 2d  n.** system, on-
2d22b 64 69 73 6b 20 64 6f 63 6c 69 73 74 73 20 4d 55  disk doclists MU
2d22c 53 54 20 73 74 6f 72 65 20 61 74 20 6c 65 61 73  ST store at leas
2d22d 74 20 70 6f 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a  t positions..**.
2d22e 2a 2a 0a 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20  **.**** Segment 
2d22f 6c 65 61 66 20 6e 6f 64 65 73 20 2a 2a 2a 2a 0a  leaf nodes ****.
2d230 2a 2a 20 53 65 67 6d 65 6e 74 20 6c 65 61 66 20  ** Segment leaf 
2d231 6e 6f 64 65 73 20 73 74 6f 72 65 20 74 65 72 6d  nodes store term
2d232 73 20 61 6e 64 20 64 6f 63 6c 69 73 74 73 2c 20  s and doclists, 
2d233 6f 72 64 65 72 65 64 20 62 79 20 74 65 72 6d 2e  ordered by term.
2d234 20 20 4c 65 61 66 0a 2a 2a 20 6e 6f 64 65 73 20    Leaf.** nodes 
2d235 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69 6e  are written usin
2d236 67 20 4c 65 61 66 57 72 69 74 65 72 2c 20 61 6e  g LeafWriter, an
2d237 64 20 72 65 61 64 20 75 73 69 6e 67 20 4c 65 61  d read using Lea
2d238 66 52 65 61 64 65 72 20 28 74 6f 0a 2a 2a 20 69  fReader (to.** i
2d239 74 65 72 61 74 65 20 74 68 72 6f 75 67 68 20 61  terate through a
2d23a 20 73 69 6e 67 6c 65 20 6c 65 61 66 20 6e 6f 64   single leaf nod
2d23b 65 27 73 20 64 61 74 61 29 20 61 6e 64 20 4c 65  e's data) and Le
2d23c 61 76 65 73 52 65 61 64 65 72 20 28 74 6f 0a 2a  avesReader (to.*
2d23d 2a 20 69 74 65 72 61 74 65 20 74 68 72 6f 75 67  * iterate throug
2d23e 68 20 61 20 73 65 67 6d 65 6e 74 27 73 20 65 6e  h a segment's en
2d23f 74 69 72 65 20 6c 65 61 66 20 6c 61 79 65 72 29  tire leaf layer)
2d240 2e 20 20 4c 65 61 66 20 6e 6f 64 65 73 20 68 61  .  Leaf nodes ha
2d241 76 65 0a 2a 2a 20 74 68 65 20 66 6f 72 6d 61 74  ve.** the format
2d242 3a 0a 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69  :.**.** varint i
2d243 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 20  Height;         
2d244 20 20 20 20 28 68 65 69 67 68 74 20 66 72 6f 6d      (height from
2d245 20 6c 65 61 66 20 6c 65 76 65 6c 2c 20 61 6c 77   leaf level, alw
2d246 61 79 73 20 30 29 0a 2a 2a 20 76 61 72 69 6e 74  ays 0).** varint
2d247 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   nTerm;         
2d248 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66        (length of
2d249 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a 20   first term).** 
2d24a 63 68 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d  char pTerm[nTerm
2d24b 5d 3b 20 20 20 20 20 20 20 20 20 20 28 63 6f 6e  ];          (con
2d24c 74 65 6e 74 20 6f 66 20 66 69 72 73 74 20 74 65  tent of first te
2d24d 72 6d 29 0a 2a 2a 20 76 61 72 69 6e 74 20 6e 44  rm).** varint nD
2d24e 6f 63 6c 69 73 74 3b 20 20 20 20 20 20 20 20 20  oclist;         
2d24f 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 74 65     (length of te
2d250 72 6d 27 73 20 61 73 73 6f 63 69 61 74 65 64 20  rm's associated 
2d251 64 6f 63 6c 69 73 74 29 0a 2a 2a 20 63 68 61 72  doclist).** char
2d252 20 70 44 6f 63 6c 69 73 74 5b 6e 44 6f 63 6c 69   pDoclist[nDocli
2d253 73 74 5d 3b 20 20 20 20 28 63 6f 6e 74 65 6e 74  st];    (content
2d254 20 6f 66 20 64 6f 63 6c 69 73 74 29 0a 2a 2a 20   of doclist).** 
2d255 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20 20 20 20  array {.**      
2d256 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d257 20 20 20 20 20 20 20 28 66 75 72 74 68 65 72 20         (further 
2d258 74 65 72 6d 73 20 61 72 65 20 64 65 6c 74 61 2d  terms are delta-
2d259 65 6e 63 6f 64 65 64 29 0a 2a 2a 20 20 20 76 61  encoded).**   va
2d25a 72 69 6e 74 20 6e 50 72 65 66 69 78 3b 20 20 20  rint nPrefix;   
2d25b 20 20 20 20 20 20 20 20 28 6c 65 6e 67 74 68 20          (length 
2d25c 6f 66 20 70 72 65 66 69 78 20 73 68 61 72 65 64  of prefix shared
2d25d 20 77 69 74 68 20 70 72 65 76 69 6f 75 73 20 74   with previous t
2d25e 65 72 6d 29 0a 2a 2a 20 20 20 76 61 72 69 6e 74  erm).**   varint
2d25f 20 6e 53 75 66 66 69 78 3b 20 20 20 20 20 20 20   nSuffix;       
2d260 20 20 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 75      (length of u
2d261 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 29 0a  nshared suffix).
2d262 2a 2a 20 20 20 63 68 61 72 20 70 54 65 72 6d 53  **   char pTermS
2d263 75 66 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 28  uffix[nSuffix];(
2d264 75 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 20  unshared suffix 
2d265 6f 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a  of next term).**
2d266 20 20 20 76 61 72 69 6e 74 20 6e 44 6f 63 6c 69     varint nDocli
2d267 73 74 3b 20 20 20 20 20 20 20 20 20 20 28 6c 65  st;          (le
2d268 6e 67 74 68 20 6f 66 20 74 65 72 6d 27 73 20 61  ngth of term's a
2d269 73 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73  ssociated doclis
2d26a 74 29 0a 2a 2a 20 20 20 63 68 61 72 20 70 44 6f  t).**   char pDo
2d26b 63 6c 69 73 74 5b 6e 44 6f 63 6c 69 73 74 5d 3b  clist[nDoclist];
2d26c 20 20 28 63 6f 6e 74 65 6e 74 20 6f 66 20 64 6f    (content of do
2d26d 63 6c 69 73 74 29 0a 2a 2a 20 7d 0a 2a 2a 0a 2a  clist).** }.**.*
2d26e 2a 20 48 65 72 65 2c 20 61 72 72 61 79 20 7b 20  * Here, array { 
2d26f 58 20 7d 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f  X } means zero o
2d270 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63  r more occurrenc
2d271 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e  es of X, adjacen
2d272 74 20 69 6e 0a 2a 2a 20 6d 65 6d 6f 72 79 2e 0a  t in.** memory..
2d273 2a 2a 0a 2a 2a 20 4c 65 61 66 20 6e 6f 64 65 73  **.** Leaf nodes
2d274 20 61 72 65 20 62 72 6f 6b 65 6e 20 69 6e 74 6f   are broken into
2d275 20 62 6c 6f 63 6b 73 20 77 68 69 63 68 20 61 72   blocks which ar
2d276 65 20 73 74 6f 72 65 64 20 63 6f 6e 74 69 67 75  e stored contigu
2d277 6f 75 73 6c 79 20 69 6e 0a 2a 2a 20 74 68 65 20  ously in.** the 
2d278 25 5f 73 65 67 6d 65 6e 74 73 20 74 61 62 6c 65  %_segments table
2d279 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65 72   in sorted order
2d27a 2e 20 20 54 68 69 73 20 6d 65 61 6e 73 20 74 68  .  This means th
2d27b 61 74 20 77 68 65 6e 20 74 68 65 20 65 6e 64 0a  at when the end.
2d27c 2a 2a 20 6f 66 20 61 20 6e 6f 64 65 20 69 73 20  ** of a node is 
2d27d 72 65 61 63 68 65 64 2c 20 74 68 65 20 6e 65 78  reached, the nex
2d27e 74 20 74 65 72 6d 20 69 73 20 69 6e 20 74 68 65  t term is in the
2d27f 20 6e 6f 64 65 20 77 69 74 68 20 74 68 65 20 6e   node with the n
2d280 65 78 74 0a 2a 2a 20 67 72 65 61 74 65 72 20 6e  ext.** greater n
2d281 6f 64 65 20 69 64 2e 0a 2a 2a 0a 2a 2a 20 4e 65  ode id..**.** Ne
2d282 77 20 64 61 74 61 20 69 73 20 73 70 69 6c 6c 65  w data is spille
2d283 64 20 74 6f 20 61 20 6e 65 77 20 6c 65 61 66 20  d to a new leaf 
2d284 6e 6f 64 65 20 77 68 65 6e 20 74 68 65 20 63 75  node when the cu
2d285 72 72 65 6e 74 20 6e 6f 64 65 0a 2a 2a 20 65 78  rrent node.** ex
2d286 63 65 65 64 73 20 4c 45 41 46 5f 4d 41 58 20 62  ceeds LEAF_MAX b
2d287 79 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30  ytes (default 20
2d288 34 38 29 2e 20 20 4e 65 77 20 64 61 74 61 20 77  48).  New data w
2d289 68 69 63 68 20 69 74 73 65 6c 66 20 69 73 0a 2a  hich itself is.*
2d28a 2a 20 6c 61 72 67 65 72 20 74 68 61 6e 20 53 54  * larger than ST
2d28b 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 28 64 65  ANDALONE_MIN (de
2d28c 66 61 75 6c 74 20 31 30 32 34 29 20 69 73 20 70  fault 1024) is p
2d28d 6c 61 63 65 64 20 69 6e 20 61 20 73 74 61 6e 64  laced in a stand
2d28e 61 6c 6f 6e 65 0a 2a 2a 20 6e 6f 64 65 20 28 61  alone.** node (a
2d28f 20 6c 65 61 66 20 6e 6f 64 65 20 77 69 74 68 20   leaf node with 
2d290 61 20 73 69 6e 67 6c 65 20 74 65 72 6d 20 61 6e  a single term an
2d291 64 20 64 6f 63 6c 69 73 74 29 2e 20 20 54 68 65  d doclist).  The
2d292 20 67 6f 61 6c 20 6f 66 0a 2a 2a 20 74 68 65 73   goal of.** thes
2d293 65 20 73 65 74 74 69 6e 67 73 20 69 73 20 74 6f  e settings is to
2d294 20 70 61 63 6b 20 74 6f 67 65 74 68 65 72 20 67   pack together g
2d295 72 6f 75 70 73 20 6f 66 20 73 6d 61 6c 6c 20 64  roups of small d
2d296 6f 63 6c 69 73 74 73 20 77 68 69 6c 65 0a 2a 2a  oclists while.**
2d297 20 6d 61 6b 69 6e 67 20 69 74 20 65 66 66 69 63   making it effic
2d298 69 65 6e 74 20 74 6f 20 64 69 72 65 63 74 6c 79  ient to directly
2d299 20 61 63 63 65 73 73 20 6c 61 72 67 65 20 64 6f   access large do
2d29a 63 6c 69 73 74 73 2e 20 20 54 68 65 0a 2a 2a 20  clists.  The.** 
2d29b 61 73 73 75 6d 70 74 69 6f 6e 20 69 73 20 74 68  assumption is th
2d29c 61 74 20 6c 61 72 67 65 20 64 6f 63 6c 69 73 74  at large doclist
2d29d 73 20 72 65 70 72 65 73 65 6e 74 20 74 65 72 6d  s represent term
2d29e 73 20 77 68 69 63 68 20 61 72 65 20 6d 6f 72 65  s which are more
2d29f 0a 2a 2a 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65  .** likely to be
2d2a0 20 71 75 65 72 79 20 74 61 72 67 65 74 73 2e 0a   query targets..
2d2a1 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73 73  **.** TODO(shess
2d2a2 29 20 49 74 20 6d 61 79 20 62 65 20 75 73 65 66  ) It may be usef
2d2a3 75 6c 20 66 6f 72 20 62 6c 6f 63 6b 69 6e 67 20  ul for blocking 
2d2a4 64 65 63 69 73 69 6f 6e 73 20 74 6f 20 62 65 20  decisions to be 
2d2a5 6d 6f 72 65 0a 2a 2a 20 64 79 6e 61 6d 69 63 2e  more.** dynamic.
2d2a6 20 20 46 6f 72 20 69 6e 73 74 61 6e 63 65 2c 20    For instance, 
2d2a7 69 74 20 6d 61 79 20 6d 61 6b 65 20 6d 6f 72 65  it may make more
2d2a8 20 73 65 6e 73 65 20 74 6f 20 68 61 76 65 20 61   sense to have a
2d2a9 20 32 2e 35 6b 20 6c 65 61 66 0a 2a 2a 20 6e 6f   2.5k leaf.** no
2d2aa 64 65 20 72 61 74 68 65 72 20 74 68 61 6e 20 73  de rather than s
2d2ab 70 6c 69 74 74 69 6e 67 20 69 6e 74 6f 20 32 6b  plitting into 2k
2d2ac 20 61 6e 64 20 2e 35 6b 20 6e 6f 64 65 73 2e 20   and .5k nodes. 
2d2ad 20 4d 79 20 69 6e 74 75 69 74 69 6f 6e 20 69 73   My intuition is
2d2ae 0a 2a 2a 20 74 68 61 74 20 74 68 69 73 20 6d 69  .** that this mi
2d2af 67 68 74 20 65 78 74 65 6e 64 20 74 68 72 6f 75  ght extend throu
2d2b0 67 68 20 32 78 20 6f 72 20 34 78 20 74 68 65 20  gh 2x or 4x the 
2d2b1 70 61 67 65 73 69 7a 65 2e 0a 2a 2a 0a 2a 2a 0a  pagesize..**.**.
2d2b2 2a 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74  **** Segment int
2d2b3 65 72 69 6f 72 20 6e 6f 64 65 73 20 2a 2a 2a 2a  erior nodes ****
2d2b4 0a 2a 2a 20 53 65 67 6d 65 6e 74 20 69 6e 74 65  .** Segment inte
2d2b5 72 69 6f 72 20 6e 6f 64 65 73 20 73 74 6f 72 65  rior nodes store
2d2b6 20 62 6c 6f 63 6b 69 64 73 20 66 6f 72 20 73 75   blockids for su
2d2b7 62 74 72 65 65 20 6e 6f 64 65 73 20 61 6e 64 20  btree nodes and 
2d2b8 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 64 65 73 63  terms.** to desc
2d2b9 72 69 62 65 20 77 68 61 74 20 64 61 74 61 20 69  ribe what data i
2d2ba 73 20 73 74 6f 72 65 64 20 62 79 20 74 68 65 20  s stored by the 
2d2bb 65 61 63 68 20 73 75 62 74 72 65 65 2e 20 20 49  each subtree.  I
2d2bc 6e 74 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73  nterior.** nodes
2d2bd 20 61 72 65 20 77 72 69 74 74 65 6e 20 75 73 69   are written usi
2d2be 6e 67 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65  ng InteriorWrite
2d2bf 72 2c 20 61 6e 64 20 72 65 61 64 20 75 73 69 6e  r, and read usin
2d2c0 67 0a 2a 2a 20 49 6e 74 65 72 69 6f 72 52 65 61  g.** InteriorRea
2d2c1 64 65 72 2e 20 20 49 6e 74 65 72 69 6f 72 57 72  der.  InteriorWr
2d2c2 69 74 65 72 73 20 61 72 65 20 63 72 65 61 74 65  iters are create
2d2c3 64 20 61 73 20 6e 65 65 64 65 64 20 77 68 65 6e  d as needed when
2d2c4 0a 2a 2a 20 53 65 67 6d 65 6e 74 57 72 69 74 65  .** SegmentWrite
2d2c5 72 20 63 72 65 61 74 65 73 20 6e 65 77 20 6c 65  r creates new le
2d2c6 61 66 20 6e 6f 64 65 73 2c 20 6f 72 20 77 68 65  af nodes, or whe
2d2c7 6e 20 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f  n an interior no
2d2c8 64 65 0a 2a 2a 20 69 74 73 65 6c 66 20 67 72 6f  de.** itself gro
2d2c9 77 73 20 74 6f 6f 20 62 69 67 20 61 6e 64 20 6d  ws too big and m
2d2ca 75 73 74 20 62 65 20 73 70 6c 69 74 2e 20 20 54  ust be split.  T
2d2cb 68 65 20 66 6f 72 6d 61 74 20 6f 66 20 69 6e 74  he format of int
2d2cc 65 72 69 6f 72 0a 2a 2a 20 6e 6f 64 65 73 3a 0a  erior.** nodes:.
2d2cd 2a 2a 0a 2a 2a 20 76 61 72 69 6e 74 20 69 48 65  **.** varint iHe
2d2ce 69 67 68 74 3b 20 20 20 20 20 20 20 20 20 20 20  ight;           
2d2cf 28 68 65 69 67 68 74 20 66 72 6f 6d 20 6c 65 61  (height from lea
2d2d0 66 20 6c 65 76 65 6c 2c 20 61 6c 77 61 79 73 20  f level, always 
2d2d1 3e 30 29 0a 2a 2a 20 76 61 72 69 6e 74 20 69 42  >0).** varint iB
2d2d2 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20 20 20  lockid;         
2d2d3 20 28 62 6c 6f 63 6b 20 69 64 20 6f 66 20 6e 6f   (block id of no
2d2d4 64 65 27 73 20 6c 65 66 74 6d 6f 73 74 20 73 75  de's leftmost su
2d2d5 62 74 72 65 65 29 0a 2a 2a 20 6f 70 74 69 6f 6e  btree).** option
2d2d6 61 6c 20 7b 0a 2a 2a 20 20 20 76 61 72 69 6e 74  al {.**   varint
2d2d7 20 6e 54 65 72 6d 3b 20 20 20 20 20 20 20 20 20   nTerm;         
2d2d8 20 20 28 6c 65 6e 67 74 68 20 6f 66 20 66 69 72    (length of fir
2d2d9 73 74 20 74 65 72 6d 29 0a 2a 2a 20 20 20 63 68  st term).**   ch
2d2da 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 3b  ar pTerm[nTerm];
2d2db 20 20 20 20 20 20 28 63 6f 6e 74 65 6e 74 20 6f        (content o
2d2dc 66 20 66 69 72 73 74 20 74 65 72 6d 29 0a 2a 2a  f first term).**
2d2dd 20 20 20 61 72 72 61 79 20 7b 0a 2a 2a 20 20 20     array {.**   
2d2de 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d2df 20 20 20 20 20 20 20 20 20 20 20 20 20 28 66 75               (fu
2d2e0 72 74 68 65 72 20 74 65 72 6d 73 20 61 72 65 20  rther terms are 
2d2e1 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 29 0a 2a  delta-encoded).*
2d2e2 2a 20 20 20 20 20 76 61 72 69 6e 74 20 6e 50 72  *     varint nPr
2d2e3 65 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20  efix;           
2d2e4 20 28 6c 65 6e 67 74 68 20 6f 66 20 73 68 61 72   (length of shar
2d2e5 65 64 20 70 72 65 66 69 78 20 77 69 74 68 20 70  ed prefix with p
2d2e6 72 65 76 69 6f 75 73 20 74 65 72 6d 29 0a 2a 2a  revious term).**
2d2e7 20 20 20 20 20 76 61 72 69 6e 74 20 6e 53 75 66       varint nSuf
2d2e8 66 69 78 3b 20 20 20 20 20 20 20 20 20 20 20 20  fix;            
2d2e9 28 6c 65 6e 67 74 68 20 6f 66 20 75 6e 73 68 61  (length of unsha
2d2ea 72 65 64 20 73 75 66 66 69 78 29 0a 2a 2a 20 20  red suffix).**  
2d2eb 20 20 20 63 68 61 72 20 70 54 65 72 6d 53 75 66     char pTermSuf
2d2ec 66 69 78 5b 6e 53 75 66 66 69 78 5d 3b 20 28 75  fix[nSuffix]; (u
2d2ed 6e 73 68 61 72 65 64 20 73 75 66 66 69 78 20 6f  nshared suffix o
2d2ee 66 20 6e 65 78 74 20 74 65 72 6d 29 0a 2a 2a 20  f next term).** 
2d2ef 20 20 7d 0a 2a 2a 20 7d 0a 2a 2a 0a 2a 2a 20 48    }.** }.**.** H
2d2f0 65 72 65 2c 20 6f 70 74 69 6f 6e 61 6c 20 7b 20  ere, optional { 
2d2f1 58 20 7d 20 6d 65 61 6e 73 20 61 6e 20 6f 70 74  X } means an opt
2d2f2 69 6f 6e 61 6c 20 65 6c 65 6d 65 6e 74 2c 20 77  ional element, w
2d2f3 68 69 6c 65 20 61 72 72 61 79 20 7b 20 58 20 7d  hile array { X }
2d2f4 0a 2a 2a 20 6d 65 61 6e 73 20 7a 65 72 6f 20 6f  .** means zero o
2d2f5 72 20 6d 6f 72 65 20 6f 63 63 75 72 72 65 6e 63  r more occurrenc
2d2f6 65 73 20 6f 66 20 58 2c 20 61 64 6a 61 63 65 6e  es of X, adjacen
2d2f7 74 20 69 6e 20 6d 65 6d 6f 72 79 2e 0a 2a 2a 0a  t in memory..**.
2d2f8 2a 2a 20 41 6e 20 69 6e 74 65 72 69 6f 72 20 6e  ** An interior n
2d2f9 6f 64 65 20 65 6e 63 6f 64 65 73 20 6e 20 74 65  ode encodes n te
2d2fa 72 6d 73 20 73 65 70 61 72 61 74 69 6e 67 20 6e  rms separating n
2d2fb 2b 31 20 73 75 62 74 72 65 65 73 2e 20 20 54 68  +1 subtrees.  Th
2d2fc 65 0a 2a 2a 20 73 75 62 74 72 65 65 20 62 6c 6f  e.** subtree blo
2d2fd 63 6b 73 20 61 72 65 20 63 6f 6e 74 69 67 75 6f  cks are contiguo
2d2fe 75 73 2c 20 73 6f 20 6f 6e 6c 79 20 74 68 65 20  us, so only the 
2d2ff 66 69 72 73 74 20 73 75 62 74 72 65 65 27 73 20  first subtree's 
2d300 62 6c 6f 63 6b 69 64 0a 2a 2a 20 69 73 20 65 6e  blockid.** is en
2d301 63 6f 64 65 64 2e 20 20 54 68 65 20 73 75 62 74  coded.  The subt
2d302 72 65 65 20 61 74 20 69 42 6c 6f 63 6b 69 64 20  ree at iBlockid 
2d303 77 69 6c 6c 20 63 6f 6e 74 61 69 6e 20 61 6c 6c  will contain all
2d304 20 74 65 72 6d 73 20 6c 65 73 73 0a 2a 2a 20 74   terms less.** t
2d305 68 61 6e 20 74 68 65 20 66 69 72 73 74 20 74 65  han the first te
2d306 72 6d 20 65 6e 63 6f 64 65 64 20 28 6f 72 20 61  rm encoded (or a
2d307 6c 6c 20 74 65 72 6d 73 20 69 66 20 6e 6f 20 74  ll terms if no t
2d308 65 72 6d 20 69 73 20 65 6e 63 6f 64 65 64 29 2e  erm is encoded).
2d309 0a 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 66  .** Otherwise, f
2d30a 6f 72 20 74 65 72 6d 73 20 67 72 65 61 74 65 72  or terms greater
2d30b 20 74 68 61 6e 20 6f 72 20 65 71 75 61 6c 20 74   than or equal t
2d30c 6f 20 70 54 65 72 6d 5b 69 5d 20 62 75 74 20 6c  o pTerm[i] but l
2d30d 65 73 73 0a 2a 2a 20 74 68 61 6e 20 70 54 65 72  ess.** than pTer
2d30e 6d 5b 69 2b 31 5d 2c 20 74 68 65 20 73 75 62 74  m[i+1], the subt
2d30f 72 65 65 20 66 6f 72 20 74 68 61 74 20 74 65 72  ree for that ter
2d310 6d 20 77 69 6c 6c 20 62 65 20 72 6f 6f 74 65 64  m will be rooted
2d311 20 61 74 0a 2a 2a 20 69 42 6c 6f 63 6b 69 64 2b   at.** iBlockid+
2d312 69 2e 20 20 49 6e 74 65 72 69 6f 72 20 6e 6f 64  i.  Interior nod
2d313 65 73 20 6f 6e 6c 79 20 73 74 6f 72 65 20 65 6e  es only store en
2d314 6f 75 67 68 20 74 65 72 6d 20 64 61 74 61 20 74  ough term data t
2d315 6f 0a 2a 2a 20 64 69 73 74 69 6e 67 75 69 73 68  o.** distinguish
2d316 20 61 64 6a 61 63 65 6e 74 20 63 68 69 6c 64 72   adjacent childr
2d317 65 6e 20 28 69 66 20 74 68 65 20 72 69 67 68 74  en (if the right
2d318 6d 6f 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65  most term of the
2d319 20 6c 65 66 74 0a 2a 2a 20 63 68 69 6c 64 20 69   left.** child i
2d31a 73 20 22 73 6f 6d 65 74 68 69 6e 67 22 2c 20 61  s "something", a
2d31b 6e 64 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20  nd the leftmost 
2d31c 74 65 72 6d 20 6f 66 20 74 68 65 20 72 69 67 68  term of the righ
2d31d 74 20 63 68 69 6c 64 20 69 73 0a 2a 2a 20 22 77  t child is.** "w
2d31e 69 63 6b 65 64 22 2c 20 6f 6e 6c 79 20 22 77 22  icked", only "w"
2d31f 20 69 73 20 73 74 6f 72 65 64 29 2e 0a 2a 2a 0a   is stored)..**.
2d320 2a 2a 20 4e 65 77 20 64 61 74 61 20 69 73 20 73  ** New data is s
2d321 70 69 6c 6c 65 64 20 74 6f 20 61 20 6e 65 77 20  pilled to a new 
2d322 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 61 74  interior node at
2d323 20 74 68 65 20 73 61 6d 65 20 68 65 69 67 68 74   the same height
2d324 20 77 68 65 6e 0a 2a 2a 20 74 68 65 20 63 75 72   when.** the cur
2d325 72 65 6e 74 20 6e 6f 64 65 20 65 78 63 65 65 64  rent node exceed
2d326 73 20 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 62  s INTERIOR_MAX b
2d327 79 74 65 73 20 28 64 65 66 61 75 6c 74 20 32 30  ytes (default 20
2d328 34 38 29 2e 0a 2a 2a 20 49 4e 54 45 52 49 4f 52  48)..** INTERIOR
2d329 5f 4d 49 4e 5f 54 45 52 4d 53 20 28 64 65 66 61  _MIN_TERMS (defa
2d32a 75 6c 74 20 37 29 20 6b 65 65 70 73 20 6c 61 72  ult 7) keeps lar
2d32b 67 65 20 74 65 72 6d 73 20 66 72 6f 6d 20 6d 6f  ge terms from mo
2d32c 6e 6f 70 6f 6c 69 7a 69 6e 67 0a 2a 2a 20 69 6e  nopolizing.** in
2d32d 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 6e 64  terior nodes and
2d32e 20 6d 61 6b 69 6e 67 20 74 68 65 20 74 72 65 65   making the tree
2d32f 20 74 6f 6f 20 73 6b 69 6e 6e 79 2e 20 20 54 68   too skinny.  Th
2d330 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73  e interior nodes
2d331 0a 2a 2a 20 61 74 20 61 20 67 69 76 65 6e 20 68  .** at a given h
2d332 65 69 67 68 74 20 61 72 65 20 6e 61 74 75 72 61  eight are natura
2d333 6c 6c 79 20 74 72 61 63 6b 65 64 20 62 79 20 69  lly tracked by i
2d334 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20 61 74  nterior nodes at
2d335 0a 2a 2a 20 68 65 69 67 68 74 2b 31 2c 20 61 6e  .** height+1, an
2d336 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a 0a 2a  d so on..**.**.*
2d337 2a 2a 2a 20 53 65 67 6d 65 6e 74 20 64 69 72 65  *** Segment dire
2d338 63 74 6f 72 79 20 2a 2a 2a 2a 0a 2a 2a 20 54 68  ctory ****.** Th
2d339 65 20 73 65 67 6d 65 6e 74 20 64 69 72 65 63 74  e segment direct
2d33a 6f 72 79 20 69 6e 20 74 61 62 6c 65 20 25 5f 73  ory in table %_s
2d33b 65 67 64 69 72 20 73 74 6f 72 65 73 20 6d 65 74  egdir stores met
2d33c 61 2d 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 66 6f  a-information fo
2d33d 72 0a 2a 2a 20 6d 65 72 67 69 6e 67 20 61 6e 64  r.** merging and
2d33e 20 64 65 6c 65 74 69 6e 67 20 73 65 67 6d 65 6e   deleting segmen
2d33f 74 73 2c 20 61 6e 64 20 61 6c 73 6f 20 74 68 65  ts, and also the
2d340 20 72 6f 6f 74 20 6e 6f 64 65 20 6f 66 20 74 68   root node of th
2d341 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 27 73 20 74  e.** segment's t
2d342 72 65 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72  ree..**.** The r
2d343 6f 6f 74 20 6e 6f 64 65 20 69 73 20 74 68 65 20  oot node is the 
2d344 74 6f 70 20 6e 6f 64 65 20 6f 66 20 74 68 65 20  top node of the 
2d345 73 65 67 6d 65 6e 74 27 73 20 74 72 65 65 20 61  segment's tree a
2d346 66 74 65 72 20 65 6e 63 6f 64 69 6e 67 0a 2a 2a  fter encoding.**
2d347 20 74 68 65 20 65 6e 74 69 72 65 20 73 65 67 6d   the entire segm
2d348 65 6e 74 2c 20 72 65 73 74 72 69 63 74 65 64 20  ent, restricted 
2d349 74 6f 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65  to ROOT_MAX byte
2d34a 73 20 28 64 65 66 61 75 6c 74 20 31 30 32 34 29  s (default 1024)
2d34b 2e 0a 2a 2a 20 54 68 69 73 20 63 6f 75 6c 64 20  ..** This could 
2d34c 62 65 20 65 69 74 68 65 72 20 61 20 6c 65 61 66  be either a leaf
2d34d 20 6e 6f 64 65 20 6f 72 20 61 6e 20 69 6e 74 65   node or an inte
2d34e 72 69 6f 72 20 6e 6f 64 65 2e 20 20 49 66 20 74  rior node.  If t
2d34f 68 65 20 74 6f 70 0a 2a 2a 20 6e 6f 64 65 20 72  he top.** node r
2d350 65 71 75 69 72 65 73 20 6d 6f 72 65 20 74 68 61  equires more tha
2d351 6e 20 52 4f 4f 54 5f 4d 41 58 20 62 79 74 65 73  n ROOT_MAX bytes
2d352 2c 20 69 74 20 69 73 20 66 6c 75 73 68 65 64 20  , it is flushed 
2d353 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 0a 2a 2a  to %_segments.**
2d354 20 61 6e 64 20 61 20 6e 65 77 20 72 6f 6f 74 20   and a new root 
2d355 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20 69 73  interior node is
2d356 20 67 65 6e 65 72 61 74 65 64 20 28 77 68 69 63   generated (whic
2d357 68 20 73 68 6f 75 6c 64 20 61 6c 77 61 79 73 20  h should always 
2d358 66 69 74 0a 2a 2a 20 77 69 74 68 69 6e 20 52 4f  fit.** within RO
2d359 4f 54 5f 4d 41 58 20 62 65 63 61 75 73 65 20 69  OT_MAX because i
2d35a 74 20 6f 6e 6c 79 20 6e 65 65 64 73 20 73 70 61  t only needs spa
2d35b 63 65 20 66 6f 72 20 32 20 76 61 72 69 6e 74 73  ce for 2 varints
2d35c 2c 20 74 68 65 0a 2a 2a 20 68 65 69 67 68 74 20  , the.** height 
2d35d 61 6e 64 20 74 68 65 20 62 6c 6f 63 6b 69 64 20  and the blockid 
2d35e 6f 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20  of the previous 
2d35f 72 6f 6f 74 29 2e 0a 2a 2a 0a 2a 2a 20 54 68 65  root)..**.** The
2d360 20 6d 65 74 61 2d 69 6e 66 6f 72 6d 61 74 69 6f   meta-informatio
2d361 6e 20 69 6e 20 74 68 65 20 73 65 67 6d 65 6e 74  n in the segment
2d362 20 64 69 72 65 63 74 6f 72 79 20 69 73 3a 0a 2a   directory is:.*
2d363 2a 20 20 20 6c 65 76 65 6c 20 20 20 20 20 20 20  *   level       
2d364 20 20 20 20 20 20 20 20 2d 20 73 65 67 6d 65 6e          - segmen
2d365 74 20 6c 65 76 65 6c 20 28 73 65 65 20 62 65 6c  t level (see bel
2d366 6f 77 29 0a 2a 2a 20 20 20 69 64 78 20 20 20 20  ow).**   idx    
2d367 20 20 20 20 20 20 20 20 20 20 20 20 20 2d 20 69               - i
2d368 6e 64 65 78 20 77 69 74 68 69 6e 20 6c 65 76 65  ndex within leve
2d369 6c 0a 2a 2a 20 20 20 20 20 20 20 20 20 20 20 20  l.**            
2d36a 20 20 20 20 20 20 20 20 20 20 20 2d 20 28 6c 65             - (le
2d36b 76 65 6c 2c 69 64 78 20 75 6e 69 71 75 65 6c 79  vel,idx uniquely
2d36c 20 69 64 65 6e 74 69 66 79 20 61 20 73 65 67 6d   identify a segm
2d36d 65 6e 74 29 0a 2a 2a 20 20 20 73 74 61 72 74 5f  ent).**   start_
2d36e 62 6c 6f 63 6b 20 20 20 20 20 20 20 20 20 2d 20  block         - 
2d36f 66 69 72 73 74 20 6c 65 61 66 20 6e 6f 64 65 0a  first leaf node.
2d370 2a 2a 20 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f  **   leaves_end_
2d371 62 6c 6f 63 6b 20 20 20 20 2d 20 6c 61 73 74 20  block    - last 
2d372 6c 65 61 66 20 6e 6f 64 65 0a 2a 2a 20 20 20 65  leaf node.**   e
2d373 6e 64 5f 62 6c 6f 63 6b 20 20 20 20 20 20 20 20  nd_block        
2d374 20 20 20 2d 20 6c 61 73 74 20 62 6c 6f 63 6b 20     - last block 
2d375 28 69 6e 63 6c 75 64 69 6e 67 20 69 6e 74 65 72  (including inter
2d376 69 6f 72 20 6e 6f 64 65 73 29 0a 2a 2a 20 20 20  ior nodes).**   
2d377 72 6f 6f 74 20 20 20 20 20 20 20 20 20 20 20 20  root            
2d378 20 20 20 20 2d 20 63 6f 6e 74 65 6e 74 73 20 6f      - contents o
2d379 66 20 72 6f 6f 74 20 6e 6f 64 65 0a 2a 2a 0a 2a  f root node.**.*
2d37a 2a 20 49 66 20 74 68 65 20 72 6f 6f 74 20 6e 6f  * If the root no
2d37b 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f 64  de is a leaf nod
2d37c 65 2c 20 74 68 65 6e 20 73 74 61 72 74 5f 62 6c  e, then start_bl
2d37d 6f 63 6b 2c 0a 2a 2a 20 6c 65 61 76 65 73 5f 65  ock,.** leaves_e
2d37e 6e 64 5f 62 6c 6f 63 6b 2c 20 61 6e 64 20 65 6e  nd_block, and en
2d37f 64 5f 62 6c 6f 63 6b 20 61 72 65 20 61 6c 6c 20  d_block are all 
2d380 30 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 2a 2a 20 53 65  0..**.**.**** Se
2d381 67 6d 65 6e 74 20 6d 65 72 67 69 6e 67 20 2a 2a  gment merging **
2d382 2a 2a 0a 2a 2a 20 54 6f 20 61 6d 6f 72 74 69 7a  **.** To amortiz
2d383 65 20 75 70 64 61 74 65 20 63 6f 73 74 73 2c 20  e update costs, 
2d384 73 65 67 6d 65 6e 74 73 20 61 72 65 20 67 72 6f  segments are gro
2d385 75 70 73 20 69 6e 74 6f 20 6c 65 76 65 6c 73 20  ups into levels 
2d386 61 6e 64 0a 2a 2a 20 6d 65 72 67 65 64 20 69 6e  and.** merged in
2d387 20 6d 61 74 63 68 65 73 2e 20 20 45 61 63 68 20   matches.  Each 
2d388 69 6e 63 72 65 61 73 65 20 69 6e 20 6c 65 76 65  increase in leve
2d389 6c 20 72 65 70 72 65 73 65 6e 74 73 20 65 78 70  l represents exp
2d38a 6f 6e 65 6e 74 69 61 6c 6c 79 0a 2a 2a 20 6d 6f  onentially.** mo
2d38b 72 65 20 64 6f 63 75 6d 65 6e 74 73 2e 0a 2a 2a  re documents..**
2d38c 0a 2a 2a 20 4e 65 77 20 64 6f 63 75 6d 65 6e 74  .** New document
2d38d 73 20 28 61 63 74 75 61 6c 6c 79 2c 20 64 6f 63  s (actually, doc
2d38e 75 6d 65 6e 74 20 75 70 64 61 74 65 73 29 20 61  ument updates) a
2d38f 72 65 20 74 6f 6b 65 6e 69 7a 65 64 20 61 6e 64  re tokenized and
2d390 0a 2a 2a 20 77 72 69 74 74 65 6e 20 69 6e 64 69  .** written indi
2d391 76 69 64 75 61 6c 6c 79 20 28 75 73 69 6e 67 20  vidually (using 
2d392 4c 65 61 66 57 72 69 74 65 72 29 20 74 6f 20 61  LeafWriter) to a
2d393 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74   level 0 segment
2d394 2c 20 77 69 74 68 0a 2a 2a 20 69 6e 63 72 65 6d  , with.** increm
2d395 65 6e 74 69 6e 67 20 69 64 78 2e 20 20 57 68 65  enting idx.  Whe
2d396 6e 20 69 64 78 20 72 65 61 63 68 65 73 20 4d 45  n idx reaches ME
2d397 52 47 45 5f 43 4f 55 4e 54 20 28 64 65 66 61 75  RGE_COUNT (defau
2d398 6c 74 20 31 36 29 2c 20 61 6c 6c 0a 2a 2a 20 6c  lt 16), all.** l
2d399 65 76 65 6c 20 30 20 73 65 67 6d 65 6e 74 73 20  evel 0 segments 
2d39a 61 72 65 20 6d 65 72 67 65 64 20 69 6e 74 6f 20  are merged into 
2d39b 61 20 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 31  a single level 1
2d39c 20 73 65 67 6d 65 6e 74 2e 20 20 4c 65 76 65 6c   segment.  Level
2d39d 20 31 0a 2a 2a 20 69 73 20 70 6f 70 75 6c 61 74   1.** is populat
2d39e 65 64 20 6c 69 6b 65 20 6c 65 76 65 6c 20 30 2c  ed like level 0,
2d39f 20 61 6e 64 20 65 76 65 6e 74 75 61 6c 6c 79 20   and eventually 
2d3a0 4d 45 52 47 45 5f 43 4f 55 4e 54 20 6c 65 76 65  MERGE_COUNT leve
2d3a1 6c 20 31 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20  l 1.** segments 
2d3a2 61 72 65 20 6d 65 72 67 65 64 20 74 6f 20 61 20  are merged to a 
2d3a3 73 69 6e 67 6c 65 20 6c 65 76 65 6c 20 32 20 73  single level 2 s
2d3a4 65 67 6d 65 6e 74 20 28 72 65 70 72 65 73 65 6e  egment (represen
2d3a5 74 69 6e 67 0a 2a 2a 20 4d 45 52 47 45 5f 43 4f  ting.** MERGE_CO
2d3a6 55 4e 54 5e 32 20 75 70 64 61 74 65 73 29 2c 20  UNT^2 updates), 
2d3a7 61 6e 64 20 73 6f 20 6f 6e 2e 0a 2a 2a 0a 2a 2a  and so on..**.**
2d3a8 20 41 20 73 65 67 6d 65 6e 74 20 6d 65 72 67 65   A segment merge
2d3a9 20 74 72 61 76 65 72 73 65 73 20 61 6c 6c 20 73   traverses all s
2d3aa 65 67 6d 65 6e 74 73 20 61 74 20 61 20 67 69 76  egments at a giv
2d3ab 65 6e 20 6c 65 76 65 6c 20 69 6e 0a 2a 2a 20 70  en level in.** p
2d3ac 61 72 61 6c 6c 65 6c 2c 20 70 65 72 66 6f 72 6d  arallel, perform
2d3ad 69 6e 67 20 61 20 73 74 72 61 69 67 68 74 66 6f  ing a straightfo
2d3ae 72 77 61 72 64 20 73 6f 72 74 65 64 20 6d 65 72  rward sorted mer
2d3af 67 65 2e 20 20 53 69 6e 63 65 20 73 65 67 6d 65  ge.  Since segme
2d3b0 6e 74 0a 2a 2a 20 6c 65 61 66 20 6e 6f 64 65 73  nt.** leaf nodes
2d3b1 20 61 72 65 20 77 72 69 74 74 65 6e 20 69 6e 20   are written in 
2d3b2 74 6f 20 74 68 65 20 25 5f 73 65 67 6d 65 6e 74  to the %_segment
2d3b3 73 20 74 61 62 6c 65 20 69 6e 20 6f 72 64 65 72  s table in order
2d3b4 2c 20 74 68 69 73 0a 2a 2a 20 6d 65 72 67 65 20  , this.** merge 
2d3b5 74 72 61 76 65 72 73 65 73 20 74 68 65 20 75 6e  traverses the un
2d3b6 64 65 72 6c 79 69 6e 67 20 73 71 6c 69 74 65 20  derlying sqlite 
2d3b7 64 69 73 6b 20 73 74 72 75 63 74 75 72 65 73 20  disk structures 
2d3b8 65 66 66 69 63 69 65 6e 74 6c 79 2e 0a 2a 2a 20  efficiently..** 
2d3b9 41 66 74 65 72 20 74 68 65 20 6d 65 72 67 65 2c  After the merge,
2d3ba 20 61 6c 6c 20 73 65 67 6d 65 6e 74 20 62 6c 6f   all segment blo
2d3bb 63 6b 73 20 66 72 6f 6d 20 74 68 65 20 6d 65 72  cks from the mer
2d3bc 67 65 64 20 6c 65 76 65 6c 20 61 72 65 0a 2a 2a  ged level are.**
2d3bd 20 64 65 6c 65 74 65 64 2e 0a 2a 2a 0a 2a 2a 20   deleted..**.** 
2d3be 4d 45 52 47 45 5f 43 4f 55 4e 54 20 63 6f 6e 74  MERGE_COUNT cont
2d3bf 72 6f 6c 73 20 68 6f 77 20 6f 66 74 65 6e 20 77  rols how often w
2d3c0 65 20 6d 65 72 67 65 20 73 65 67 6d 65 6e 74 73  e merge segments
2d3c1 2e 20 20 31 36 20 73 65 65 6d 73 20 74 6f 20 62  .  16 seems to b
2d3c2 65 0a 2a 2a 20 73 6f 6d 65 77 68 61 74 20 6f 66  e.** somewhat of
2d3c3 20 61 20 73 77 65 65 74 20 73 70 6f 74 20 66 6f   a sweet spot fo
2d3c4 72 20 69 6e 73 65 72 74 69 6f 6e 20 70 65 72 66  r insertion perf
2d3c5 6f 72 6d 61 6e 63 65 2e 20 20 33 32 20 61 6e 64  ormance.  32 and
2d3c6 20 36 34 20 73 68 6f 77 0a 2a 2a 20 76 65 72 79   64 show.** very
2d3c7 20 73 69 6d 69 6c 61 72 20 70 65 72 66 6f 72 6d   similar perform
2d3c8 61 6e 63 65 20 6e 75 6d 62 65 72 73 20 74 6f 20  ance numbers to 
2d3c9 31 36 20 6f 6e 20 69 6e 73 65 72 74 69 6f 6e 2c  16 on insertion,
2d3ca 20 74 68 6f 75 67 68 20 74 68 65 79 27 72 65 0a   though they're.
2d3cb 2a 2a 20 61 20 74 69 6e 79 20 62 69 74 20 73 6c  ** a tiny bit sl
2d3cc 6f 77 65 72 20 28 70 65 72 68 61 70 73 20 64 75  ower (perhaps du
2d3cd 65 20 74 6f 20 6d 6f 72 65 20 6f 76 65 72 68 65  e to more overhe
2d3ce 61 64 20 69 6e 20 6d 65 72 67 65 2d 74 69 6d 65  ad in merge-time
2d3cf 0a 2a 2a 20 73 6f 72 74 69 6e 67 29 2e 20 20 38  .** sorting).  8
2d3d0 20 69 73 20 61 62 6f 75 74 20 32 30 25 20 73 6c   is about 20% sl
2d3d1 6f 77 65 72 20 74 68 61 6e 20 31 36 2c 20 34 20  ower than 16, 4 
2d3d2 61 62 6f 75 74 20 35 30 25 20 73 6c 6f 77 65 72  about 50% slower
2d3d3 20 74 68 61 6e 0a 2a 2a 20 31 36 2c 20 32 20 61   than.** 16, 2 a
2d3d4 62 6f 75 74 20 36 36 25 20 73 6c 6f 77 65 72 20  bout 66% slower 
2d3d5 74 68 61 6e 20 31 36 2e 0a 2a 2a 0a 2a 2a 20 41  than 16..**.** A
2d3d6 74 20 71 75 65 72 79 20 74 69 6d 65 2c 20 68 69  t query time, hi
2d3d7 67 68 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20 69  gh MERGE_COUNT i
2d3d8 6e 63 72 65 61 73 65 73 20 74 68 65 20 6e 75 6d  ncreases the num
2d3d9 62 65 72 20 6f 66 20 73 65 67 6d 65 6e 74 73 0a  ber of segments.
2d3da 2a 2a 20 77 68 69 63 68 20 6e 65 65 64 20 74 6f  ** which need to
2d3db 20 62 65 20 73 63 61 6e 6e 65 64 20 61 6e 64 20   be scanned and 
2d3dc 6d 65 72 67 65 64 2e 20 20 46 6f 72 20 69 6e 73  merged.  For ins
2d3dd 74 61 6e 63 65 2c 20 77 69 74 68 20 31 30 30 6b  tance, with 100k
2d3de 20 64 6f 63 73 0a 2a 2a 20 69 6e 73 65 72 74 65   docs.** inserte
2d3df 64 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 45 52 47  d:.**.**    MERG
2d3e0 45 5f 43 4f 55 4e 54 20 20 20 73 65 67 6d 65 6e  E_COUNT   segmen
2d3e1 74 73 0a 2a 2a 20 20 20 20 20 20 20 31 36 20 20  ts.**       16  
2d3e2 20 20 20 20 20 20 20 20 20 32 35 0a 2a 2a 20 20           25.**  
2d3e3 20 20 20 20 20 20 38 20 20 20 20 20 20 20 20 20        8         
2d3e4 20 20 31 32 0a 2a 2a 20 20 20 20 20 20 20 20 34    12.**        4
2d3e5 20 20 20 20 20 20 20 20 20 20 20 31 30 0a 2a 2a             10.**
2d3e6 20 20 20 20 20 20 20 20 32 20 20 20 20 20 20 20          2       
2d3e7 20 20 20 20 20 36 0a 2a 2a 0a 2a 2a 20 54 68 69       6.**.** Thi
2d3e8 73 20 61 70 70 65 61 72 73 20 74 6f 20 68 61 76  s appears to hav
2d3e9 65 20 6f 6e 6c 79 20 61 20 6d 6f 64 65 72 61 74  e only a moderat
2d3ea 65 20 69 6d 70 61 63 74 20 6f 6e 20 71 75 65 72  e impact on quer
2d3eb 69 65 73 20 66 6f 72 20 76 65 72 79 0a 2a 2a 20  ies for very.** 
2d3ec 66 72 65 71 75 65 6e 74 20 74 65 72 6d 73 20 28  frequent terms (
2d3ed 77 68 69 63 68 20 61 72 65 20 73 6f 6d 65 77 68  which are somewh
2d3ee 61 74 20 64 6f 6d 69 6e 61 74 65 64 20 62 79 20  at dominated by 
2d3ef 73 65 67 6d 65 6e 74 20 6d 65 72 67 65 0a 2a 2a  segment merge.**
2d3f0 20 63 6f 73 74 73 29 2c 20 61 6e 64 20 69 6e 66   costs), and inf
2d3f1 72 65 71 75 65 6e 74 20 61 6e 64 20 6e 6f 6e 2d  requent and non-
2d3f2 65 78 69 73 74 65 6e 74 20 74 65 72 6d 73 20 73  existent terms s
2d3f3 74 69 6c 6c 20 73 65 65 6d 20 74 6f 20 62 65 20  till seem to be 
2d3f4 66 61 73 74 0a 2a 2a 20 65 76 65 6e 20 77 69 74  fast.** even wit
2d3f5 68 20 6d 61 6e 79 20 73 65 67 6d 65 6e 74 73 2e  h many segments.
2d3f6 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
2d3f7 73 29 20 54 68 61 74 20 73 61 69 64 2c 20 69 74  s) That said, it
2d3f8 20 77 6f 75 6c 64 20 62 65 20 6e 69 63 65 20 74   would be nice t
2d3f9 6f 20 68 61 76 65 20 61 20 62 65 74 74 65 72 20  o have a better 
2d3fa 71 75 65 72 79 2d 73 69 64 65 0a 2a 2a 20 61 72  query-side.** ar
2d3fb 67 75 6d 65 6e 74 20 66 6f 72 20 4d 45 52 47 45  gument for MERGE
2d3fc 5f 43 4f 55 4e 54 20 6f 66 20 31 36 2e 20 20 41  _COUNT of 16.  A
2d3fd 6c 73 6f 2c 20 69 74 20 69 73 20 70 6f 73 73 69  lso, it is possi
2d3fe 62 6c 65 2f 6c 69 6b 65 6c 79 20 74 68 61 74 0a  ble/likely that.
2d3ff 2a 2a 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 73  ** optimizations
2d400 20 74 6f 20 74 68 69 6e 67 73 20 6c 69 6b 65 20   to things like 
2d401 64 6f 63 6c 69 73 74 20 6d 65 72 67 69 6e 67 20  doclist merging 
2d402 77 69 6c 6c 20 73 77 69 6e 67 20 74 68 65 20 73  will swing the s
2d403 77 65 65 74 0a 2a 2a 20 73 70 6f 74 20 61 72 6f  weet.** spot aro
2d404 75 6e 64 2e 0a 2a 2a 0a 2a 2a 0a 2a 2a 0a 2a 2a  und..**.**.**.**
2d405 2a 2a 20 48 61 6e 64 6c 69 6e 67 20 6f 66 20 64  ** Handling of d
2d406 65 6c 65 74 69 6f 6e 73 20 61 6e 64 20 75 70 64  eletions and upd
2d407 61 74 65 73 20 2a 2a 2a 2a 0a 2a 2a 20 53 69 6e  ates ****.** Sin
2d408 63 65 20 77 65 27 72 65 20 75 73 69 6e 67 20 61  ce we're using a
2d409 20 73 65 67 6d 65 6e 74 65 64 20 73 74 72 75 63   segmented struc
2d40a 74 75 72 65 2c 20 77 69 74 68 20 6e 6f 20 64 6f  ture, with no do
2d40b 63 69 64 2d 6f 72 69 65 6e 74 65 64 0a 2a 2a 20  cid-oriented.** 
2d40c 69 6e 64 65 78 20 69 6e 74 6f 20 74 68 65 20 74  index into the t
2d40d 65 72 6d 20 69 6e 64 65 78 2c 20 77 65 20 63 6c  erm index, we cl
2d40e 65 61 72 6c 79 20 63 61 6e 6e 6f 74 20 73 69 6d  early cannot sim
2d40f 70 6c 79 20 75 70 64 61 74 65 20 74 68 65 20 74  ply update the t
2d410 65 72 6d 0a 2a 2a 20 69 6e 64 65 78 20 77 68 65  erm.** index whe
2d411 6e 20 61 20 64 6f 63 75 6d 65 6e 74 20 69 73 20  n a document is 
2d412 64 65 6c 65 74 65 64 20 6f 72 20 75 70 64 61 74  deleted or updat
2d413 65 64 2e 20 20 46 6f 72 20 64 65 6c 65 74 69 6f  ed.  For deletio
2d414 6e 73 2c 20 77 65 0a 2a 2a 20 77 72 69 74 65 20  ns, we.** write 
2d415 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74  an empty doclist
2d416 20 28 76 61 72 69 6e 74 28 64 6f 63 69 64 29 20   (varint(docid) 
2d417 76 61 72 69 6e 74 28 50 4f 53 5f 45 4e 44 29 29  varint(POS_END))
2d418 2c 20 66 6f 72 20 75 70 64 61 74 65 73 0a 2a 2a  , for updates.**
2d419 20 77 65 20 73 69 6d 70 6c 79 20 77 72 69 74 65   we simply write
2d41a 20 74 68 65 20 6e 65 77 20 64 6f 63 6c 69 73 74   the new doclist
2d41b 2e 20 20 53 65 67 6d 65 6e 74 20 6d 65 72 67 65  .  Segment merge
2d41c 73 20 6f 76 65 72 77 72 69 74 65 20 6f 6c 64 65  s overwrite olde
2d41d 72 0a 2a 2a 20 64 61 74 61 20 66 6f 72 20 61 20  r.** data for a 
2d41e 70 61 72 74 69 63 75 6c 61 72 20 64 6f 63 69 64  particular docid
2d41f 20 77 69 74 68 20 6e 65 77 65 72 20 64 61 74 61   with newer data
2d420 2c 20 73 6f 20 64 65 6c 65 74 65 73 20 6f 72 20  , so deletes or 
2d421 75 70 64 61 74 65 73 0a 2a 2a 20 77 69 6c 6c 20  updates.** will 
2d422 65 76 65 6e 74 75 61 6c 6c 79 20 6f 76 65 72 74  eventually overt
2d423 61 6b 65 20 74 68 65 20 65 61 72 6c 69 65 72 20  ake the earlier 
2d424 64 61 74 61 20 61 6e 64 20 6b 6e 6f 63 6b 20 69  data and knock i
2d425 74 20 6f 75 74 2e 20 20 54 68 65 0a 2a 2a 20 71  t out.  The.** q
2d426 75 65 72 79 20 6c 6f 67 69 63 20 6c 69 6b 65 77  uery logic likew
2d427 69 73 65 20 6d 65 72 67 65 73 20 64 6f 63 6c 69  ise merges docli
2d428 73 74 73 20 73 6f 20 74 68 61 74 20 6e 65 77 65  sts so that newe
2d429 72 20 64 61 74 61 20 6b 6e 6f 63 6b 73 20 6f 75  r data knocks ou
2d42a 74 0a 2a 2a 20 6f 6c 64 65 72 20 64 61 74 61 2e  t.** older data.
2d42b 0a 2a 2a 0a 2a 2a 20 54 4f 44 4f 28 73 68 65 73  .**.** TODO(shes
2d42c 73 29 20 50 72 6f 76 69 64 65 20 61 20 56 41 43  s) Provide a VAC
2d42d 55 55 4d 20 74 79 70 65 20 6f 70 65 72 61 74 69  UUM type operati
2d42e 6f 6e 20 74 6f 20 63 6c 65 61 72 20 6f 75 74 20  on to clear out 
2d42f 61 6c 6c 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 73  all.** deletions
2d430 20 61 6e 64 20 64 75 70 6c 69 63 61 74 69 6f 6e   and duplication
2d431 73 2e 20 20 54 68 69 73 20 77 6f 75 6c 64 20 62  s.  This would b
2d432 61 73 69 63 61 6c 6c 79 20 62 65 20 61 20 66 6f  asically be a fo
2d433 72 63 65 64 20 6d 65 72 67 65 0a 2a 2a 20 69 6e  rced merge.** in
2d434 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d  to a single segm
2d435 65 6e 74 2e 0a 2a 2f 0a 0a 23 69 66 20 21 64 65  ent..*/..#if !de
2d436 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52  fined(SQLITE_COR
2d437 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51  E) || defined(SQ
2d438 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33  LITE_ENABLE_FTS3
2d439 29 0a 0a 23 69 66 20 64 65 66 69 6e 65 64 28 53  )..#if defined(S
2d43a 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
2d43b 33 29 20 26 26 20 21 64 65 66 69 6e 65 64 28 53  3) && !defined(S
2d43c 51 4c 49 54 45 5f 43 4f 52 45 29 0a 23 20 64 65  QLITE_CORE).# de
2d43d 66 69 6e 65 20 53 51 4c 49 54 45 5f 43 4f 52 45  fine SQLITE_CORE
2d43e 20 31 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 2a 2a   1.#endif.../***
2d43f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c  *********** Incl
2d440 75 64 65 20 66 74 73 33 5f 68 61 73 68 2e 68 20  ude fts3_hash.h 
2d441 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20 6f 66  in the middle of
2d442 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a   fts3.c ********
2d443 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a  **********/./***
2d444 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69  *********** Begi
2d445 6e 20 66 69 6c 65 20 66 74 73 33 5f 68 61 73 68  n file fts3_hash
2d446 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .h *************
2d447 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d448 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a  **********/./*.*
2d449 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62 65 72  * 2001 September
2d44a 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75   22.**.** The au
2d44b 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73 20 63  thor disclaims c
2d44c 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68 69 73  opyright to this
2d44d 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20 20 49   source code.  I
2d44e 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20  n place of.** a 
2d44f 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65  legal notice, he
2d450 72 65 20 69 73 20 61 20 62 6c 65 73 73 69 6e 67  re is a blessing
2d451 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79  :.**.**    May y
2d452 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e  ou do good and n
2d453 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d  ot evil..**    M
2d454 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f 72 67  ay you find forg
2d455 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f 75 72  iveness for your
2d456 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69 76 65  self and forgive
2d457 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d   others..**    M
2d458 61 79 20 79 6f 75 20 73 68 61 72 65 20 66 72 65  ay you share fre
2d459 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e  ely, never takin
2d45a 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f 75 20  g more than you 
2d45b 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  give..**.*******
2d45c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d45d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d45e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d45f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d460 2a 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  **.** This is th
2d461 65 20 68 65 61 64 65 72 20 66 69 6c 65 20 66 6f  e header file fo
2d462 72 20 74 68 65 20 67 65 6e 65 72 69 63 20 68 61  r the generic ha
2d463 73 68 2d 74 61 62 6c 65 20 69 6d 70 6c 65 6d 65  sh-table impleme
2d464 6e 61 74 69 6f 6e 0a 2a 2a 20 75 73 65 64 20 69  nation.** used i
2d465 6e 20 53 51 4c 69 74 65 2e 20 20 57 65 27 76 65  n SQLite.  We've
2d466 20 6d 6f 64 69 66 69 65 64 20 69 74 20 73 6c 69   modified it sli
2d467 67 68 74 6c 79 20 74 6f 20 73 65 72 76 65 20 61  ghtly to serve a
2d468 73 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 0a 2a  s a standalone.*
2d469 2a 20 68 61 73 68 20 74 61 62 6c 65 20 69 6d 70  * hash table imp
2d46a 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20  lementation for 
2d46b 74 68 65 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e  the full-text in
2d46c 64 65 78 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a  dexing module..*
2d46d 2a 0a 2a 2f 0a 23 69 66 6e 64 65 66 20 5f 46 54  *.*/.#ifndef _FT
2d46e 53 33 5f 48 41 53 48 5f 48 5f 0a 23 64 65 66 69  S3_HASH_H_.#defi
2d46f 6e 65 20 5f 46 54 53 33 5f 48 41 53 48 5f 48 5f  ne _FTS3_HASH_H_
2d470 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 64 65 63  ../* Forward dec
2d471 6c 61 72 61 74 69 6f 6e 73 20 6f 66 20 73 74 72  larations of str
2d472 75 63 74 75 72 65 73 2e 20 2a 2f 0a 74 79 70 65  uctures. */.type
2d473 64 65 66 20 73 74 72 75 63 74 20 66 74 73 33 48  def struct fts3H
2d474 61 73 68 20 66 74 73 33 48 61 73 68 3b 0a 74 79  ash fts3Hash;.ty
2d475 70 65 64 65 66 20 73 74 72 75 63 74 20 66 74 73  pedef struct fts
2d476 33 48 61 73 68 45 6c 65 6d 20 66 74 73 33 48 61  3HashElem fts3Ha
2d477 73 68 45 6c 65 6d 3b 0a 0a 2f 2a 20 41 20 63 6f  shElem;../* A co
2d478 6d 70 6c 65 74 65 20 68 61 73 68 20 74 61 62 6c  mplete hash tabl
2d479 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
2d47a 20 6f 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e   of the followin
2d47b 67 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2a 20  g structure..** 
2d47c 54 68 65 20 69 6e 74 65 72 6e 61 6c 73 20 6f 66  The internals of
2d47d 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 20   this structure 
2d47e 61 72 65 20 69 6e 74 65 6e 64 65 64 20 74 6f 20  are intended to 
2d47f 62 65 20 6f 70 61 71 75 65 20 2d 2d 20 63 6c 69  be opaque -- cli
2d480 65 6e 74 0a 2a 2a 20 63 6f 64 65 20 73 68 6f 75  ent.** code shou
2d481 6c 64 20 6e 6f 74 20 61 74 74 65 6d 70 74 20 74  ld not attempt t
2d482 6f 20 61 63 63 65 73 73 20 6f 72 20 6d 6f 64 69  o access or modi
2d483 66 79 20 74 68 65 20 66 69 65 6c 64 73 20 6f 66  fy the fields of
2d484 20 74 68 69 73 20 73 74 72 75 63 74 75 72 65 0a   this structure.
2d485 2a 2a 20 64 69 72 65 63 74 6c 79 2e 20 20 43 68  ** directly.  Ch
2d486 61 6e 67 65 20 74 68 69 73 20 73 74 72 75 63 74  ange this struct
2d487 75 72 65 20 6f 6e 6c 79 20 62 79 20 75 73 69 6e  ure only by usin
2d488 67 20 74 68 65 20 72 6f 75 74 69 6e 65 73 20 62  g the routines b
2d489 65 6c 6f 77 2e 0a 2a 2a 20 48 6f 77 65 76 65 72  elow..** However
2d48a 2c 20 6d 61 6e 79 20 6f 66 20 74 68 65 20 22 70  , many of the "p
2d48b 72 6f 63 65 64 75 72 65 73 22 20 61 6e 64 20 22  rocedures" and "
2d48c 66 75 6e 63 74 69 6f 6e 73 22 20 66 6f 72 20 6d  functions" for m
2d48d 6f 64 69 66 79 69 6e 67 20 61 6e 64 0a 2a 2a 20  odifying and.** 
2d48e 61 63 63 65 73 73 69 6e 67 20 74 68 69 73 20 73  accessing this s
2d48f 74 72 75 63 74 75 72 65 20 61 72 65 20 72 65 61  tructure are rea
2d490 6c 6c 79 20 6d 61 63 72 6f 73 2c 20 73 6f 20 77  lly macros, so w
2d491 65 20 63 61 6e 27 74 20 72 65 61 6c 6c 79 20 6d  e can't really m
2d492 61 6b 65 0a 2a 2a 20 74 68 69 73 20 73 74 72 75  ake.** this stru
2d493 63 74 75 72 65 20 6f 70 61 71 75 65 2e 0a 2a 2f  cture opaque..*/
2d494 0a 73 74 72 75 63 74 20 66 74 73 33 48 61 73 68  .struct fts3Hash
2d495 20 7b 0a 20 20 63 68 61 72 20 6b 65 79 43 6c 61   {.  char keyCla
2d496 73 73 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ss;          /* 
2d497 48 41 53 48 5f 49 4e 54 2c 20 5f 50 4f 49 4e 54  HASH_INT, _POINT
2d498 45 52 2c 20 5f 53 54 52 49 4e 47 2c 20 5f 42 49  ER, _STRING, _BI
2d499 4e 41 52 59 20 2a 2f 0a 20 20 63 68 61 72 20 63  NARY */.  char c
2d49a 6f 70 79 4b 65 79 3b 20 20 20 20 20 20 20 20 20  opyKey;         
2d49b 20 20 2f 2a 20 54 72 75 65 20 69 66 20 63 6f 70    /* True if cop
2d49c 79 20 6f 66 20 6b 65 79 20 6d 61 64 65 20 6f 6e  y of key made on
2d49d 20 69 6e 73 65 72 74 20 2a 2f 0a 20 20 69 6e 74   insert */.  int
2d49e 20 63 6f 75 6e 74 3b 20 20 20 20 20 20 20 20 20   count;         
2d49f 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2d4a0 66 20 65 6e 74 72 69 65 73 20 69 6e 20 74 68 69  f entries in thi
2d4a1 73 20 74 61 62 6c 65 20 2a 2f 0a 20 20 66 74 73  s table */.  fts
2d4a2 33 48 61 73 68 45 6c 65 6d 20 2a 66 69 72 73 74  3HashElem *first
2d4a3 3b 20 20 20 20 2f 2a 20 54 68 65 20 66 69 72 73  ;    /* The firs
2d4a4 74 20 65 6c 65 6d 65 6e 74 20 6f 66 20 74 68 65  t element of the
2d4a5 20 61 72 72 61 79 20 2a 2f 0a 20 20 69 6e 74 20   array */.  int 
2d4a6 68 74 73 69 7a 65 3b 20 20 20 20 20 20 20 20 20  htsize;         
2d4a7 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66      /* Number of
2d4a8 20 62 75 63 6b 65 74 73 20 69 6e 20 74 68 65 20   buckets in the 
2d4a9 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20  hash table */.  
2d4aa 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 7b  struct _fts3ht {
2d4ab 20 20 20 20 20 20 20 20 2f 2a 20 74 68 65 20 68          /* the h
2d4ac 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20  ash table */.   
2d4ad 20 69 6e 74 20 63 6f 75 6e 74 3b 20 20 20 20 20   int count;     
2d4ae 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2d4af 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20 77  ber of entries w
2d4b0 69 74 68 20 74 68 69 73 20 68 61 73 68 20 2a 2f  ith this hash */
2d4b1 0a 20 20 20 20 66 74 73 33 48 61 73 68 45 6c 65  .    fts3HashEle
2d4b2 6d 20 2a 63 68 61 69 6e 3b 20 20 20 20 20 2f 2a  m *chain;     /*
2d4b3 20 50 6f 69 6e 74 65 72 20 74 6f 20 66 69 72 73   Pointer to firs
2d4b4 74 20 65 6e 74 72 79 20 77 69 74 68 20 74 68 69  t entry with thi
2d4b5 73 20 68 61 73 68 20 2a 2f 0a 20 20 7d 20 2a 68  s hash */.  } *h
2d4b6 74 3b 0a 7d 3b 0a 0a 2f 2a 20 45 61 63 68 20 65  t;.};../* Each e
2d4b7 6c 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 68 61  lement in the ha
2d4b8 73 68 20 74 61 62 6c 65 20 69 73 20 61 6e 20 69  sh table is an i
2d4b9 6e 73 74 61 6e 63 65 20 6f 66 20 74 68 65 20 66  nstance of the f
2d4ba 6f 6c 6c 6f 77 69 6e 67 20 0a 2a 2a 20 73 74 72  ollowing .** str
2d4bb 75 63 74 75 72 65 2e 20 20 41 6c 6c 20 65 6c 65  ucture.  All ele
2d4bc 6d 65 6e 74 73 20 61 72 65 20 73 74 6f 72 65 64  ments are stored
2d4bd 20 6f 6e 20 61 20 73 69 6e 67 6c 65 20 64 6f 75   on a single dou
2d4be 62 6c 79 2d 6c 69 6e 6b 65 64 20 6c 69 73 74 2e  bly-linked list.
2d4bf 0a 2a 2a 0a 2a 2a 20 41 67 61 69 6e 2c 20 74 68  .**.** Again, th
2d4c0 69 73 20 73 74 72 75 63 74 75 72 65 20 69 73 20  is structure is 
2d4c1 69 6e 74 65 6e 64 65 64 20 74 6f 20 62 65 20 6f  intended to be o
2d4c2 70 61 71 75 65 2c 20 62 75 74 20 69 74 20 63 61  paque, but it ca
2d4c3 6e 27 74 20 72 65 61 6c 6c 79 0a 2a 2a 20 62 65  n't really.** be
2d4c4 20 6f 70 61 71 75 65 20 62 65 63 61 75 73 65 20   opaque because 
2d4c5 69 74 20 69 73 20 75 73 65 64 20 62 79 20 6d 61  it is used by ma
2d4c6 63 72 6f 73 2e 0a 2a 2f 0a 73 74 72 75 63 74 20  cros..*/.struct 
2d4c7 66 74 73 33 48 61 73 68 45 6c 65 6d 20 7b 0a 20  fts3HashElem {. 
2d4c8 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e   fts3HashElem *n
2d4c9 65 78 74 2c 20 2a 70 72 65 76 3b 20 2f 2a 20 4e  ext, *prev; /* N
2d4ca 65 78 74 20 61 6e 64 20 70 72 65 76 69 6f 75 73  ext and previous
2d4cb 20 65 6c 65 6d 65 6e 74 73 20 69 6e 20 74 68 65   elements in the
2d4cc 20 74 61 62 6c 65 20 2a 2f 0a 20 20 76 6f 69 64   table */.  void
2d4cd 20 2a 64 61 74 61 3b 20 20 20 20 20 20 20 20 20   *data;         
2d4ce 20 20 20 20 20 20 20 2f 2a 20 44 61 74 61 20 61         /* Data a
2d4cf 73 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 74  ssociated with t
2d4d0 68 69 73 20 65 6c 65 6d 65 6e 74 20 2a 2f 0a 20  his element */. 
2d4d1 20 76 6f 69 64 20 2a 70 4b 65 79 3b 20 69 6e 74   void *pKey; int
2d4d2 20 6e 4b 65 79 3b 20 20 20 20 20 20 2f 2a 20 4b   nKey;      /* K
2d4d3 65 79 20 61 73 73 6f 63 69 61 74 65 64 20 77 69  ey associated wi
2d4d4 74 68 20 74 68 69 73 20 65 6c 65 6d 65 6e 74 20  th this element 
2d4d5 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  */.};../*.** The
2d4d6 72 65 20 61 72 65 20 32 20 64 69 66 66 65 72 65  re are 2 differe
2d4d7 6e 74 20 6d 6f 64 65 73 20 6f 66 20 6f 70 65 72  nt modes of oper
2d4d8 61 74 69 6f 6e 20 66 6f 72 20 61 20 68 61 73 68  ation for a hash
2d4d9 20 74 61 62 6c 65 3a 0a 2a 2a 0a 2a 2a 20 20 20   table:.**.**   
2d4da 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
2d4db 20 20 20 20 20 20 20 20 70 4b 65 79 20 70 6f 69          pKey poi
2d4dc 6e 74 73 20 74 6f 20 61 20 73 74 72 69 6e 67 20  nts to a string 
2d4dd 74 68 61 74 20 69 73 20 6e 4b 65 79 20 62 79 74  that is nKey byt
2d4de 65 73 20 6c 6f 6e 67 0a 2a 2a 20 20 20 20 20 20  es long.**      
2d4df 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d4e0 20 20 20 20 20 28 69 6e 63 6c 75 64 69 6e 67 20       (including 
2d4e1 74 68 65 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61  the null-termina
2d4e2 74 6f 72 2c 20 69 66 20 61 6e 79 29 2e 20 20 43  tor, if any).  C
2d4e3 61 73 65 0a 2a 2a 20 20 20 20 20 20 20 20 20 20  ase.**          
2d4e4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d4e5 20 69 73 20 72 65 73 70 65 63 74 65 64 20 69 6e   is respected in
2d4e6 20 63 6f 6d 70 61 72 69 73 6f 6e 73 2e 0a 2a 2a   comparisons..**
2d4e7 0a 2a 2a 20 20 20 46 54 53 33 5f 48 41 53 48 5f  .**   FTS3_HASH_
2d4e8 42 49 4e 41 52 59 20 20 20 20 20 20 20 20 70 4b  BINARY        pK
2d4e9 65 79 20 70 6f 69 6e 74 73 20 74 6f 20 62 69 6e  ey points to bin
2d4ea 61 72 79 20 64 61 74 61 20 6e 4b 65 79 20 62 79  ary data nKey by
2d4eb 74 65 73 20 6c 6f 6e 67 2e 20 0a 2a 2a 20 20 20  tes long. .**   
2d4ec 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d4ed 20 20 20 20 20 20 20 20 6d 65 6d 63 6d 70 28 29          memcmp()
2d4ee 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6d 70   is used to comp
2d4ef 61 72 65 20 6b 65 79 73 2e 0a 2a 2a 0a 2a 2a 20  are keys..**.** 
2d4f0 41 20 63 6f 70 79 20 6f 66 20 74 68 65 20 6b 65  A copy of the ke
2d4f1 79 20 69 73 20 6d 61 64 65 20 69 66 20 74 68 65  y is made if the
2d4f2 20 63 6f 70 79 4b 65 79 20 70 61 72 61 6d 65 74   copyKey paramet
2d4f3 65 72 20 74 6f 20 66 74 73 33 48 61 73 68 49 6e  er to fts3HashIn
2d4f4 69 74 20 69 73 20 31 2e 20 20 0a 2a 2f 0a 23 64  it is 1.  .*/.#d
2d4f5 65 66 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f  efine FTS3_HASH_
2d4f6 53 54 52 49 4e 47 20 20 20 20 31 0a 23 64 65 66  STRING    1.#def
2d4f7 69 6e 65 20 46 54 53 33 5f 48 41 53 48 5f 42 49  ine FTS3_HASH_BI
2d4f8 4e 41 52 59 20 20 20 20 32 0a 0a 2f 2a 0a 2a 2a  NARY    2../*.**
2d4f9 20 41 63 63 65 73 73 20 72 6f 75 74 69 6e 65 73   Access routines
2d4fa 2e 20 20 54 6f 20 64 65 6c 65 74 65 2c 20 69 6e  .  To delete, in
2d4fb 73 65 72 74 20 61 20 4e 55 4c 4c 20 70 6f 69 6e  sert a NULL poin
2d4fc 74 65 72 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ter..*/.SQLITE_P
2d4fd 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c 69  RIVATE void sqli
2d4fe 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28  te3Fts3HashInit(
2d4ff 66 74 73 33 48 61 73 68 2a 2c 20 69 6e 74 20 6b  fts3Hash*, int k
2d500 65 79 74 79 70 65 2c 20 69 6e 74 20 63 6f 70 79  eytype, int copy
2d501 4b 65 79 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  Key);.SQLITE_PRI
2d502 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
2d503 65 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74  e3Fts3HashInsert
2d504 28 66 74 73 33 48 61 73 68 2a 2c 20 63 6f 6e 73  (fts3Hash*, cons
2d505 74 20 76 6f 69 64 20 2a 70 4b 65 79 2c 20 69 6e  t void *pKey, in
2d506 74 20 6e 4b 65 79 2c 20 76 6f 69 64 20 2a 70 44  t nKey, void *pD
2d507 61 74 61 29 3b 0a 53 51 4c 49 54 45 5f 50 52 49  ata);.SQLITE_PRI
2d508 56 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74  VATE void *sqlit
2d509 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 63  e3Fts3HashFind(c
2d50a 6f 6e 73 74 20 66 74 73 33 48 61 73 68 2a 2c 20  onst fts3Hash*, 
2d50b 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65 79  const void *pKey
2d50c 2c 20 69 6e 74 20 6e 4b 65 79 29 3b 0a 53 51 4c  , int nKey);.SQL
2d50d 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
2d50e 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68   sqlite3Fts3Hash
2d50f 43 6c 65 61 72 28 66 74 73 33 48 61 73 68 2a 29  Clear(fts3Hash*)
2d510 3b 0a 0a 2f 2a 0a 2a 2a 20 53 68 6f 72 74 68 61  ;../*.** Shortha
2d511 6e 64 20 66 6f 72 20 74 68 65 20 66 75 6e 63 74  nd for the funct
2d512 69 6f 6e 73 20 61 62 6f 76 65 0a 2a 2f 0a 23 64  ions above.*/.#d
2d513 65 66 69 6e 65 20 66 74 73 33 48 61 73 68 49 6e  efine fts3HashIn
2d514 69 74 20 20 20 73 71 6c 69 74 65 33 46 74 73 33  it   sqlite3Fts3
2d515 48 61 73 68 49 6e 69 74 0a 23 64 65 66 69 6e 65  HashInit.#define
2d516 20 66 74 73 33 48 61 73 68 49 6e 73 65 72 74 20   fts3HashInsert 
2d517 73 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 49  sqlite3Fts3HashI
2d518 6e 73 65 72 74 0a 23 64 65 66 69 6e 65 20 66 74  nsert.#define ft
2d519 73 33 48 61 73 68 46 69 6e 64 20 20 20 73 71 6c  s3HashFind   sql
2d51a 69 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64  ite3Fts3HashFind
2d51b 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73  .#define fts3Has
2d51c 68 43 6c 65 61 72 20 20 73 71 6c 69 74 65 33 46  hClear  sqlite3F
2d51d 74 73 33 48 61 73 68 43 6c 65 61 72 0a 0a 2f 2a  ts3HashClear../*
2d51e 0a 2a 2a 20 4d 61 63 72 6f 73 20 66 6f 72 20 6c  .** Macros for l
2d51f 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c 6c 20  ooping over all 
2d520 65 6c 65 6d 65 6e 74 73 20 6f 66 20 61 20 68 61  elements of a ha
2d521 73 68 20 74 61 62 6c 65 2e 20 20 54 68 65 20 69  sh table.  The i
2d522 64 69 6f 6d 20 69 73 0a 2a 2a 20 6c 69 6b 65 20  diom is.** like 
2d523 74 68 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 66 74  this:.**.**   ft
2d524 73 33 48 61 73 68 20 68 3b 0a 2a 2a 20 20 20 66  s3Hash h;.**   f
2d525 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 70 3b 0a  ts3HashElem *p;.
2d526 2a 2a 20 20 20 2e 2e 2e 0a 2a 2a 20 20 20 66 6f  **   ....**   fo
2d527 72 28 70 3d 66 74 73 33 48 61 73 68 46 69 72 73  r(p=fts3HashFirs
2d528 74 28 26 68 29 3b 20 70 3b 20 70 3d 66 74 73 33  t(&h); p; p=fts3
2d529 48 61 73 68 4e 65 78 74 28 70 29 29 7b 0a 2a 2a  HashNext(p)){.**
2d52a 20 20 20 20 20 53 6f 6d 65 53 74 72 75 63 74 75       SomeStructu
2d52b 72 65 20 2a 70 44 61 74 61 20 3d 20 66 74 73 33  re *pData = fts3
2d52c 48 61 73 68 44 61 74 61 28 70 29 3b 0a 2a 2a 20  HashData(p);.** 
2d52d 20 20 20 20 2f 2f 20 64 6f 20 73 6f 6d 65 74 68      // do someth
2d52e 69 6e 67 20 77 69 74 68 20 70 44 61 74 61 0a 2a  ing with pData.*
2d52f 2a 20 20 20 7d 0a 2a 2f 0a 23 64 65 66 69 6e 65  *   }.*/.#define
2d530 20 66 74 73 33 48 61 73 68 46 69 72 73 74 28 48   fts3HashFirst(H
2d531 29 20 20 28 28 48 29 2d 3e 66 69 72 73 74 29 0a  )  ((H)->first).
2d532 23 64 65 66 69 6e 65 20 66 74 73 33 48 61 73 68  #define fts3Hash
2d533 4e 65 78 74 28 45 29 20 20 20 28 28 45 29 2d 3e  Next(E)   ((E)->
2d534 6e 65 78 74 29 0a 23 64 65 66 69 6e 65 20 66 74  next).#define ft
2d535 73 33 48 61 73 68 44 61 74 61 28 45 29 20 20 20  s3HashData(E)   
2d536 28 28 45 29 2d 3e 64 61 74 61 29 0a 23 64 65 66  ((E)->data).#def
2d537 69 6e 65 20 66 74 73 33 48 61 73 68 4b 65 79 28  ine fts3HashKey(
2d538 45 29 20 20 20 20 28 28 45 29 2d 3e 70 4b 65 79  E)    ((E)->pKey
2d539 29 0a 23 64 65 66 69 6e 65 20 66 74 73 33 48 61  ).#define fts3Ha
2d53a 73 68 4b 65 79 73 69 7a 65 28 45 29 20 28 28 45  shKeysize(E) ((E
2d53b 29 2d 3e 6e 4b 65 79 29 0a 0a 2f 2a 0a 2a 2a 20  )->nKey)../*.** 
2d53c 4e 75 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65  Number of entrie
2d53d 73 20 69 6e 20 61 20 68 61 73 68 20 74 61 62 6c  s in a hash tabl
2d53e 65 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 66 74 73  e.*/.#define fts
2d53f 33 48 61 73 68 43 6f 75 6e 74 28 48 29 20 20 28  3HashCount(H)  (
2d540 28 48 29 2d 3e 63 6f 75 6e 74 29 0a 0a 23 65 6e  (H)->count)..#en
2d541 64 69 66 20 2f 2a 20 5f 46 54 53 33 5f 48 41 53  dif /* _FTS3_HAS
2d542 48 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  H_H_ */../******
2d543 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2d544 66 74 73 33 5f 68 61 73 68 2e 68 20 2a 2a 2a 2a  fts3_hash.h ****
2d545 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d546 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d547 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d548 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
2d549 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
2d54a 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e 63 20  t off in fts3.c 
2d54b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d54c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d54d 2a 2a 2a 2a 2a 2a 2a 2a 20 49 6e 63 6c 75 64 65  ******** Include
2d54e 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e   fts3_tokenizer.
2d54f 68 20 69 6e 20 74 68 65 20 6d 69 64 64 6c 65 20  h in the middle 
2d550 6f 66 20 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a  of fts3.c ******
2d551 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d552 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
2d553 69 6c 65 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ile fts3_tokeniz
2d554 65 72 2e 68 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.h ***********
2d555 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d556 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
2d557 30 30 36 20 4a 75 6c 79 20 31 30 0a 2a 2a 0a 2a  006 July 10.**.*
2d558 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
2d559 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
2d55a 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
2d55b 63 6f 64 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a  code..**.*******
2d55c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d55d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d55e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d55f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d560 2a 2a 0a 2a 2a 20 44 65 66 69 6e 65 73 20 74 68  **.** Defines th
2d561 65 20 69 6e 74 65 72 66 61 63 65 20 74 6f 20 74  e interface to t
2d562 6f 6b 65 6e 69 7a 65 72 73 20 75 73 65 64 20 62  okenizers used b
2d563 79 20 66 75 6c 6c 74 65 78 74 2d 73 65 61 72 63  y fulltext-searc
2d564 68 2e 20 20 54 68 65 72 65 0a 2a 2a 20 61 72 65  h.  There.** are
2d565 20 74 68 72 65 65 20 62 61 73 69 63 20 63 6f 6d   three basic com
2d566 70 6f 6e 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 73  ponents:.**.** s
2d567 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2d568 5f 6d 6f 64 75 6c 65 20 69 73 20 61 20 73 69 6e  _module is a sin
2d569 67 6c 65 74 6f 6e 20 64 65 66 69 6e 69 6e 67 20  gleton defining 
2d56a 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2a  the tokenizer.**
2d56b 20 69 6e 74 65 72 66 61 63 65 20 66 75 6e 63 74   interface funct
2d56c 69 6f 6e 73 2e 20 20 54 68 69 73 20 69 73 20 65  ions.  This is e
2d56d 73 73 65 6e 74 69 61 6c 6c 79 20 74 68 65 20 63  ssentially the c
2d56e 6c 61 73 73 20 73 74 72 75 63 74 75 72 65 20 66  lass structure f
2d56f 6f 72 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 73  or.** tokenizers
2d570 2e 0a 2a 2a 0a 2a 2a 20 73 71 6c 69 74 65 33 5f  ..**.** sqlite3_
2d571 74 6f 6b 65 6e 69 7a 65 72 20 69 73 20 75 73 65  tokenizer is use
2d572 64 20 74 6f 20 64 65 66 69 6e 65 20 61 20 70 61  d to define a pa
2d573 72 74 69 63 75 6c 61 72 20 74 6f 6b 65 6e 69 7a  rticular tokeniz
2d574 65 72 2c 20 70 65 72 68 61 70 73 0a 2a 2a 20 69  er, perhaps.** i
2d575 6e 63 6c 75 64 69 6e 67 20 63 75 73 74 6f 6d 69  ncluding customi
2d576 7a 61 74 69 6f 6e 20 69 6e 66 6f 72 6d 61 74 69  zation informati
2d577 6f 6e 20 64 65 66 69 6e 65 64 20 61 74 20 63 72  on defined at cr
2d578 65 61 74 69 6f 6e 20 74 69 6d 65 2e 0a 2a 2a 0a  eation time..**.
2d579 2a 2a 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  ** sqlite3_token
2d57a 69 7a 65 72 5f 63 75 72 73 6f 72 20 69 73 20 67  izer_cursor is g
2d57b 65 6e 65 72 61 74 65 64 20 62 79 20 61 20 74 6f  enerated by a to
2d57c 6b 65 6e 69 7a 65 72 20 74 6f 20 67 65 6e 65 72  kenizer to gener
2d57d 61 74 65 0a 2a 2a 20 74 6f 6b 65 6e 73 20 66 72  ate.** tokens fr
2d57e 6f 6d 20 61 20 70 61 72 74 69 63 75 6c 61 72 20  om a particular 
2d57f 69 6e 70 75 74 2e 0a 2a 2f 0a 23 69 66 6e 64 65  input..*/.#ifnde
2d580 66 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  f _FTS3_TOKENIZE
2d581 52 5f 48 5f 0a 23 64 65 66 69 6e 65 20 5f 46 54  R_H_.#define _FT
2d582 53 33 5f 54 4f 4b 45 4e 49 5a 45 52 5f 48 5f 0a  S3_TOKENIZER_H_.
2d583 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2d584 4f 6e 6c 79 20 75 73 65 64 20 66 6f 72 20 53 51  Only used for SQ
2d585 4c 49 54 45 5f 4f 4b 20 61 6e 64 20 53 51 4c 49  LITE_OK and SQLI
2d586 54 45 5f 44 4f 4e 45 20 61 74 20 74 68 69 73 20  TE_DONE at this 
2d587 74 69 6d 65 2e 0a 2a 2a 20 49 66 20 74 6f 6b 65  time..** If toke
2d588 6e 69 7a 65 72 73 20 61 72 65 20 74 6f 20 62 65  nizers are to be
2d589 20 61 6c 6c 6f 77 65 64 20 74 6f 20 63 61 6c 6c   allowed to call
2d58a 20 73 71 6c 69 74 65 33 5f 2a 28 29 20 66 75 6e   sqlite3_*() fun
2d58b 63 74 69 6f 6e 73 2c 20 74 68 65 6e 0a 2a 2a 20  ctions, then.** 
2d58c 77 65 20 77 69 6c 6c 20 6e 65 65 64 20 61 20 77  we will need a w
2d58d 61 79 20 74 6f 20 72 65 67 69 73 74 65 72 20 74  ay to register t
2d58e 68 65 20 41 50 49 20 63 6f 6e 73 69 73 74 65 6e  he API consisten
2d58f 74 6c 79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 53  tly..*/../*.** S
2d590 74 72 75 63 74 75 72 65 73 20 75 73 65 64 20 62  tructures used b
2d591 79 20 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  y the tokenizer 
2d592 69 6e 74 65 72 66 61 63 65 2e 20 57 68 65 6e 20  interface. When 
2d593 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72 0a  a new tokenizer.
2d594 2a 2a 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** implementatio
2d595 6e 20 69 73 20 72 65 67 69 73 74 65 72 65 64 2c  n is registered,
2d596 20 74 68 65 20 63 61 6c 6c 65 72 20 70 72 6f 76   the caller prov
2d597 69 64 65 73 20 61 20 70 6f 69 6e 74 65 72 20 74  ides a pointer t
2d598 6f 0a 2a 2a 20 61 6e 20 73 71 6c 69 74 65 33 5f  o.** an sqlite3_
2d599 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
2d59a 20 63 6f 6e 74 61 69 6e 69 6e 67 20 70 6f 69 6e   containing poin
2d59b 74 65 72 73 20 74 6f 20 74 68 65 20 63 61 6c 6c  ters to the call
2d59c 62 61 63 6b 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  back.** function
2d59d 73 20 74 68 61 74 20 6d 61 6b 65 20 75 70 20 61  s that make up a
2d59e 6e 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  n implementation
2d59f 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 6e 20 61 6e 20  ..**.** When an 
2d5a0 66 74 73 33 20 74 61 62 6c 65 20 69 73 20 63 72  fts3 table is cr
2d5a1 65 61 74 65 64 2c 20 69 74 20 70 61 73 73 65 73  eated, it passes
2d5a2 20 61 6e 79 20 61 72 67 75 6d 65 6e 74 73 20 70   any arguments p
2d5a3 61 73 73 65 64 20 74 6f 0a 2a 2a 20 74 68 65 20  assed to.** the 
2d5a4 74 6f 6b 65 6e 69 7a 65 72 20 63 6c 61 75 73 65  tokenizer clause
2d5a5 20 6f 66 20 74 68 65 20 43 52 45 41 54 45 20 56   of the CREATE V
2d5a6 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73 74 61  IRTUAL TABLE sta
2d5a7 74 65 6d 65 6e 74 20 74 6f 20 74 68 65 0a 2a 2a  tement to the.**
2d5a8 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2d5a9 65 72 5f 6d 6f 64 75 6c 65 2e 78 43 72 65 61 74  er_module.xCreat
2d5aa 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 6f 66 20  e() function of 
2d5ab 74 68 65 20 72 65 71 75 65 73 74 65 64 20 74 6f  the requested to
2d5ac 6b 65 6e 69 7a 65 72 0a 2a 2a 20 69 6d 70 6c 65  kenizer.** imple
2d5ad 6d 65 6e 74 61 74 69 6f 6e 2e 20 54 68 65 20 78  mentation. The x
2d5ae 43 72 65 61 74 65 28 29 20 66 75 6e 63 74 69 6f  Create() functio
2d5af 6e 20 69 6e 20 74 75 72 6e 20 72 65 74 75 72 6e  n in turn return
2d5b0 73 20 61 6e 20 0a 2a 2a 20 73 71 6c 69 74 65 33  s an .** sqlite3
2d5b1 5f 74 6f 6b 65 6e 69 7a 65 72 20 73 74 72 75 63  _tokenizer struc
2d5b2 74 75 72 65 20 72 65 70 72 65 73 65 6e 74 69 6e  ture representin
2d5b3 67 20 74 68 65 20 73 70 65 63 69 66 69 63 20 74  g the specific t
2d5b4 6f 6b 65 6e 69 7a 65 72 20 74 6f 0a 2a 2a 20 62  okenizer to.** b
2d5b5 65 20 75 73 65 64 20 66 6f 72 20 74 68 65 20 66  e used for the f
2d5b6 74 73 33 20 74 61 62 6c 65 20 28 63 75 73 74 6f  ts3 table (custo
2d5b7 6d 69 7a 65 64 20 62 79 20 74 68 65 20 74 6f 6b  mized by the tok
2d5b8 65 6e 69 7a 65 72 20 63 6c 61 75 73 65 20 61 72  enizer clause ar
2d5b9 67 75 6d 65 6e 74 73 29 2e 0a 2a 2a 0a 2a 2a 20  guments)..**.** 
2d5ba 54 6f 20 74 6f 6b 65 6e 69 7a 65 20 61 6e 20 69  To tokenize an i
2d5bb 6e 70 75 74 20 62 75 66 66 65 72 2c 20 74 68 65  nput buffer, the
2d5bc 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
2d5bd 65 72 5f 6d 6f 64 75 6c 65 2e 78 4f 70 65 6e 28  er_module.xOpen(
2d5be 29 0a 2a 2a 20 6d 65 74 68 6f 64 20 69 73 20 63  ).** method is c
2d5bf 61 6c 6c 65 64 2e 20 49 74 20 72 65 74 75 72 6e  alled. It return
2d5c0 73 20 61 6e 20 73 71 6c 69 74 65 33 5f 74 6f 6b  s an sqlite3_tok
2d5c1 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 6f 62  enizer_cursor ob
2d5c2 6a 65 63 74 0a 2a 2a 20 74 68 61 74 20 6d 61 79  ject.** that may
2d5c3 20 62 65 20 75 73 65 64 20 74 6f 20 74 6f 6b 65   be used to toke
2d5c4 6e 69 7a 65 20 61 20 73 70 65 63 69 66 69 63 20  nize a specific 
2d5c5 69 6e 70 75 74 20 62 75 66 66 65 72 20 62 61 73  input buffer bas
2d5c6 65 64 20 6f 6e 0a 2a 2a 20 74 68 65 20 74 6f 6b  ed on.** the tok
2d5c7 65 6e 69 7a 61 74 69 6f 6e 20 72 75 6c 65 73 20  enization rules 
2d5c8 73 75 70 70 6c 69 65 64 20 62 79 20 61 20 73 70  supplied by a sp
2d5c9 65 63 69 66 69 63 20 73 71 6c 69 74 65 33 5f 74  ecific sqlite3_t
2d5ca 6f 6b 65 6e 69 7a 65 72 0a 2a 2a 20 6f 62 6a 65  okenizer.** obje
2d5cb 63 74 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ct..*/.typedef s
2d5cc 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f  truct sqlite3_to
2d5cd 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73  kenizer_module s
2d5ce 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2d5cf 5f 6d 6f 64 75 6c 65 3b 0a 74 79 70 65 64 65 66  _module;.typedef
2d5d0 20 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f   struct sqlite3_
2d5d1 74 6f 6b 65 6e 69 7a 65 72 20 73 71 6c 69 74 65  tokenizer sqlite
2d5d2 33 5f 74 6f 6b 65 6e 69 7a 65 72 3b 0a 74 79 70  3_tokenizer;.typ
2d5d3 65 64 65 66 20 73 74 72 75 63 74 20 73 71 6c 69  edef struct sqli
2d5d4 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  te3_tokenizer_cu
2d5d5 72 73 6f 72 20 73 71 6c 69 74 65 33 5f 74 6f 6b  rsor sqlite3_tok
2d5d6 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 3b 0a 0a  enizer_cursor;..
2d5d7 73 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 74  struct sqlite3_t
2d5d8 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
2d5d9 7b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 53 74 72  {..  /*.  ** Str
2d5da 75 63 74 75 72 65 20 76 65 72 73 69 6f 6e 2e 20  ucture version. 
2d5db 53 68 6f 75 6c 64 20 61 6c 77 61 79 73 20 62 65  Should always be
2d5dc 20 73 65 74 20 74 6f 20 30 2e 0a 20 20 2a 2f 0a   set to 0..  */.
2d5dd 20 20 69 6e 74 20 69 56 65 72 73 69 6f 6e 3b 0a    int iVersion;.
2d5de 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65 61 74  .  /*.  ** Creat
2d5df 65 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65  e a new tokenize
2d5e0 72 2e 20 54 68 65 20 76 61 6c 75 65 73 20 69 6e  r. The values in
2d5e1 20 74 68 65 20 61 72 67 76 5b 5d 20 61 72 72 61   the argv[] arra
2d5e2 79 20 61 72 65 20 74 68 65 0a 20 20 2a 2a 20 61  y are the.  ** a
2d5e3 72 67 75 6d 65 6e 74 73 20 70 61 73 73 65 64 20  rguments passed 
2d5e4 74 6f 20 74 68 65 20 22 74 6f 6b 65 6e 69 7a 65  to the "tokenize
2d5e5 72 22 20 63 6c 61 75 73 65 20 6f 66 20 74 68 65  r" clause of the
2d5e6 20 43 52 45 41 54 45 20 56 49 52 54 55 41 4c 0a   CREATE VIRTUAL.
2d5e7 20 20 2a 2a 20 54 41 42 4c 45 20 73 74 61 74 65    ** TABLE state
2d5e8 6d 65 6e 74 20 74 68 61 74 20 63 72 65 61 74 65  ment that create
2d5e9 64 20 74 68 65 20 66 74 73 33 20 74 61 62 6c 65  d the fts3 table
2d5ea 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 69  . For example, i
2d5eb 66 0a 20 20 2a 2a 20 74 68 65 20 66 6f 6c 6c 6f  f.  ** the follo
2d5ec 77 69 6e 67 20 53 51 4c 20 69 73 20 65 78 65 63  wing SQL is exec
2d5ed 75 74 65 64 3a 0a 20 20 2a 2a 0a 20 20 2a 2a 20  uted:.  **.  ** 
2d5ee 20 20 43 52 45 41 54 45 20 2e 2e 20 55 53 49 4e    CREATE .. USIN
2d5ef 47 20 66 74 73 33 28 20 2e 2e 2e 20 2c 20 74 6f  G fts3( ... , to
2d5f0 6b 65 6e 69 7a 65 72 20 3c 74 6f 6b 65 6e 69 7a  kenizer <tokeniz
2d5f1 65 72 2d 6e 61 6d 65 3e 20 61 72 67 31 20 61 72  er-name> arg1 ar
2d5f2 67 32 29 0a 20 20 2a 2a 0a 20 20 2a 2a 20 74 68  g2).  **.  ** th
2d5f3 65 6e 20 61 72 67 63 20 69 73 20 73 65 74 20 74  en argc is set t
2d5f4 6f 20 32 2c 20 61 6e 64 20 74 68 65 20 61 72 67  o 2, and the arg
2d5f5 76 5b 5d 20 61 72 72 61 79 20 63 6f 6e 74 61 69  v[] array contai
2d5f6 6e 73 20 70 6f 69 6e 74 65 72 73 0a 20 20 2a 2a  ns pointers.  **
2d5f7 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 73 20   to the strings 
2d5f8 22 61 72 67 31 22 20 61 6e 64 20 22 61 72 67 32  "arg1" and "arg2
2d5f9 22 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68 69  "..  **.  ** Thi
2d5fa 73 20 6d 65 74 68 6f 64 20 73 68 6f 75 6c 64 20  s method should 
2d5fb 72 65 74 75 72 6e 20 65 69 74 68 65 72 20 53 51  return either SQ
2d5fc 4c 49 54 45 5f 4f 4b 20 28 30 29 2c 20 6f 72 20  LITE_OK (0), or 
2d5fd 61 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20  an SQLite error 
2d5fe 0a 20 20 2a 2a 20 63 6f 64 65 2e 20 49 66 20 53  .  ** code. If S
2d5ff 51 4c 49 54 45 5f 4f 4b 20 69 73 20 72 65 74 75  QLITE_OK is retu
2d600 72 6e 65 64 2c 20 74 68 65 6e 20 2a 70 70 54 6f  rned, then *ppTo
2d601 6b 65 6e 69 7a 65 72 20 73 68 6f 75 6c 64 20 62  kenizer should b
2d602 65 20 73 65 74 0a 20 20 2a 2a 20 74 6f 20 70 6f  e set.  ** to po
2d603 69 6e 74 20 61 74 20 74 68 65 20 6e 65 77 6c 79  int at the newly
2d604 20 63 72 65 61 74 65 64 20 74 6f 6b 65 6e 69 7a   created tokeniz
2d605 65 72 20 73 74 72 75 63 74 75 72 65 2e 20 54 68  er structure. Th
2d606 65 20 67 65 6e 65 72 69 63 0a 20 20 2a 2a 20 73  e generic.  ** s
2d607 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2d608 2e 70 4d 6f 64 75 6c 65 20 76 61 72 69 61 62 6c  .pModule variabl
2d609 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  e should not be 
2d60a 69 6e 69 74 69 61 6c 69 73 65 64 20 62 79 0a 20  initialised by. 
2d60b 20 2a 2a 20 74 68 69 73 20 63 61 6c 6c 62 61 63   ** this callbac
2d60c 6b 2e 20 54 68 65 20 63 61 6c 6c 65 72 20 77 69  k. The caller wi
2d60d 6c 6c 20 64 6f 20 73 6f 2e 0a 20 20 2a 2f 0a 20  ll do so..  */. 
2d60e 20 69 6e 74 20 28 2a 78 43 72 65 61 74 65 29 28   int (*xCreate)(
2d60f 0a 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 20  .    int argc,  
2d610 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d611 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65           /* Size
2d612 20 6f 66 20 61 72 67 76 20 61 72 72 61 79 20 2a   of argv array *
2d613 2f 0a 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72  /.    const char
2d614 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c 20 20 20   *const*argv,   
2d615 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 6f 6b            /* Tok
2d616 65 6e 69 7a 65 72 20 61 72 67 75 6d 65 6e 74 20  enizer argument 
2d617 73 74 72 69 6e 67 73 20 2a 2f 0a 20 20 20 20 73  strings */.    s
2d618 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
2d619 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 20   **ppTokenizer  
2d61a 20 20 20 2f 2a 20 4f 55 54 3a 20 43 72 65 61 74     /* OUT: Creat
2d61b 65 64 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  ed tokenizer */.
2d61c 20 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20    );..  /*.  ** 
2d61d 44 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73 74  Destroy an exist
2d61e 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 2e 20 54  ing tokenizer. T
2d61f 68 65 20 66 74 73 33 20 6d 6f 64 75 6c 65 20 63  he fts3 module c
2d620 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64  alls this method
2d621 0a 20 20 2a 2a 20 65 78 61 63 74 6c 79 20 6f 6e  .  ** exactly on
2d622 63 65 20 66 6f 72 20 65 61 63 68 20 73 75 63 63  ce for each succ
2d623 65 73 73 66 75 6c 20 63 61 6c 6c 20 74 6f 20 78  essful call to x
2d624 43 72 65 61 74 65 28 29 2e 0a 20 20 2a 2f 0a 20  Create()..  */. 
2d625 20 69 6e 74 20 28 2a 78 44 65 73 74 72 6f 79 29   int (*xDestroy)
2d626 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
2d627 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  er *pTokenizer);
2d628 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 43 72 65 61  ..  /*.  ** Crea
2d629 74 65 20 61 20 74 6f 6b 65 6e 69 7a 65 72 20 63  te a tokenizer c
2d62a 75 72 73 6f 72 20 74 6f 20 74 6f 6b 65 6e 69 7a  ursor to tokeniz
2d62b 65 20 61 6e 20 69 6e 70 75 74 20 62 75 66 66 65  e an input buffe
2d62c 72 2e 20 54 68 65 20 63 61 6c 6c 65 72 0a 20 20  r. The caller.  
2d62d 2a 2a 20 69 73 20 72 65 73 70 6f 6e 73 69 62 6c  ** is responsibl
2d62e 65 20 66 6f 72 20 65 6e 73 75 72 69 6e 67 20 74  e for ensuring t
2d62f 68 61 74 20 74 68 65 20 69 6e 70 75 74 20 62 75  hat the input bu
2d630 66 66 65 72 20 72 65 6d 61 69 6e 73 20 76 61 6c  ffer remains val
2d631 69 64 0a 20 20 2a 2a 20 75 6e 74 69 6c 20 74 68  id.  ** until th
2d632 65 20 63 75 72 73 6f 72 20 69 73 20 63 6c 6f 73  e cursor is clos
2d633 65 64 20 28 75 73 69 6e 67 20 74 68 65 20 78 43  ed (using the xC
2d634 6c 6f 73 65 28 29 20 6d 65 74 68 6f 64 29 2e 20  lose() method). 
2d635 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 4f  .  */.  int (*xO
2d636 70 65 6e 29 28 0a 20 20 20 20 73 71 6c 69 74 65  pen)(.    sqlite
2d637 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f  3_tokenizer *pTo
2d638 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20 20 20 2f  kenizer,       /
2d639 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 6f 62 6a 65  * Tokenizer obje
2d63a 63 74 20 2a 2f 0a 20 20 20 20 63 6f 6e 73 74 20  ct */.    const 
2d63b 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69 6e  char *pInput, in
2d63c 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20 2f  t nBytes,      /
2d63d 2a 20 49 6e 70 75 74 20 62 75 66 66 65 72 20 2a  * Input buffer *
2d63e 2f 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 74 6f  /.    sqlite3_to
2d63f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
2d640 2a 70 70 43 75 72 73 6f 72 20 20 2f 2a 20 4f 55  *ppCursor  /* OU
2d641 54 3a 20 43 72 65 61 74 65 64 20 74 6f 6b 65 6e  T: Created token
2d642 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
2d643 20 29 3b 0a 0a 20 20 2f 2a 0a 20 20 2a 2a 20 44   );..  /*.  ** D
2d644 65 73 74 72 6f 79 20 61 6e 20 65 78 69 73 74 69  estroy an existi
2d645 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75 72  ng tokenizer cur
2d646 73 6f 72 2e 20 54 68 65 20 66 74 73 33 20 6d 6f  sor. The fts3 mo
2d647 64 75 6c 65 20 63 61 6c 6c 73 20 74 68 69 73 20  dule calls this 
2d648 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 65 78 61  .  ** method exa
2d649 63 74 6c 79 20 6f 6e 63 65 20 66 6f 72 20 65 61  ctly once for ea
2d64a 63 68 20 73 75 63 63 65 73 73 66 75 6c 20 63 61  ch successful ca
2d64b 6c 6c 20 74 6f 20 78 4f 70 65 6e 28 29 2e 0a 20  ll to xOpen().. 
2d64c 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6c 6f   */.  int (*xClo
2d64d 73 65 29 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  se)(sqlite3_toke
2d64e 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43  nizer_cursor *pC
2d64f 75 72 73 6f 72 29 3b 0a 0a 20 20 2f 2a 0a 20 20  ursor);..  /*.  
2d650 2a 2a 20 52 65 74 72 69 65 76 65 20 74 68 65 20  ** Retrieve the 
2d651 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20  next token from 
2d652 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 63 75  the tokenizer cu
2d653 72 73 6f 72 20 70 43 75 72 73 6f 72 2e 20 54 68  rsor pCursor. Th
2d654 69 73 0a 20 20 2a 2a 20 6d 65 74 68 6f 64 20 73  is.  ** method s
2d655 68 6f 75 6c 64 20 65 69 74 68 65 72 20 72 65 74  hould either ret
2d656 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 20 61 6e  urn SQLITE_OK an
2d657 64 20 73 65 74 20 74 68 65 20 76 61 6c 75 65 73  d set the values
2d658 20 6f 66 20 74 68 65 0a 20 20 2a 2a 20 22 4f 55   of the.  ** "OU
2d659 54 22 20 76 61 72 69 61 62 6c 65 73 20 69 64 65  T" variables ide
2d65a 6e 74 69 66 69 65 64 20 62 65 6c 6f 77 2c 20 6f  ntified below, o
2d65b 72 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 74 6f  r SQLITE_DONE to
2d65c 20 69 6e 64 69 63 61 74 65 20 74 68 61 74 0a 20   indicate that. 
2d65d 20 2a 2a 20 74 68 65 20 65 6e 64 20 6f 66 20 74   ** the end of t
2d65e 68 65 20 62 75 66 66 65 72 20 68 61 73 20 62 65  he buffer has be
2d65f 65 6e 20 72 65 61 63 68 65 64 2c 20 6f 72 20 61  en reached, or a
2d660 6e 20 53 51 4c 69 74 65 20 65 72 72 6f 72 20 63  n SQLite error c
2d661 6f 64 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 2a  ode..  **.  ** *
2d662 70 70 54 6f 6b 65 6e 20 73 68 6f 75 6c 64 20 62  ppToken should b
2d663 65 20 73 65 74 20 74 6f 20 70 6f 69 6e 74 20 61  e set to point a
2d664 74 20 61 20 62 75 66 66 65 72 20 63 6f 6e 74 61  t a buffer conta
2d665 69 6e 69 6e 67 20 74 68 65 20 0a 20 20 2a 2a 20  ining the .  ** 
2d666 6e 6f 72 6d 61 6c 69 7a 65 64 20 76 65 72 73 69  normalized versi
2d667 6f 6e 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20  on of the token 
2d668 28 69 2e 65 2e 20 61 66 74 65 72 20 61 6e 79 20  (i.e. after any 
2d669 63 61 73 65 2d 66 6f 6c 64 69 6e 67 20 61 6e 64  case-folding and
2d66a 2f 6f 72 0a 20 20 2a 2a 20 73 74 65 6d 6d 69 6e  /or.  ** stemmin
2d66b 67 20 68 61 73 20 62 65 65 6e 20 70 65 72 66 6f  g has been perfo
2d66c 72 6d 65 64 29 2e 20 2a 70 6e 42 79 74 65 73 20  rmed). *pnBytes 
2d66d 73 68 6f 75 6c 64 20 62 65 20 73 65 74 20 74 6f  should be set to
2d66e 20 74 68 65 20 6c 65 6e 67 74 68 0a 20 20 2a 2a   the length.  **
2d66f 20 6f 66 20 74 68 69 73 20 62 75 66 66 65 72 20   of this buffer 
2d670 69 6e 20 62 79 74 65 73 2e 20 54 68 65 20 69 6e  in bytes. The in
2d671 70 75 74 20 74 65 78 74 20 74 68 61 74 20 67 65  put text that ge
2d672 6e 65 72 61 74 65 64 20 74 68 65 20 74 6f 6b 65  nerated the toke
2d673 6e 20 69 73 0a 20 20 2a 2a 20 69 64 65 6e 74 69  n is.  ** identi
2d674 66 69 65 64 20 62 79 20 74 68 65 20 62 79 74 65  fied by the byte
2d675 20 6f 66 66 73 65 74 73 20 72 65 74 75 72 6e 65   offsets returne
2d676 64 20 69 6e 20 2a 70 69 53 74 61 72 74 4f 66 66  d in *piStartOff
2d677 73 65 74 20 61 6e 64 0a 20 20 2a 2a 20 2a 70 69  set and.  ** *pi
2d678 45 6e 64 4f 66 66 73 65 74 2e 0a 20 20 2a 2a 0a  EndOffset..  **.
2d679 20 20 2a 2a 20 54 68 65 20 62 75 66 66 65 72 20    ** The buffer 
2d67a 2a 70 70 54 6f 6b 65 6e 20 69 73 20 73 65 74 20  *ppToken is set 
2d67b 74 6f 20 70 6f 69 6e 74 20 61 74 20 69 73 20 6d  to point at is m
2d67c 61 6e 61 67 65 64 20 62 79 20 74 68 65 20 74 6f  anaged by the to
2d67d 6b 65 6e 69 7a 65 72 0a 20 20 2a 2a 20 69 6d 70  kenizer.  ** imp
2d67e 6c 65 6d 65 6e 74 61 74 69 6f 6e 2e 20 49 74 20  lementation. It 
2d67f 69 73 20 6f 6e 6c 79 20 72 65 71 75 69 72 65 64  is only required
2d680 20 74 6f 20 62 65 20 76 61 6c 69 64 20 75 6e 74   to be valid unt
2d681 69 6c 20 74 68 65 20 6e 65 78 74 20 63 61 6c 6c  il the next call
2d682 0a 20 20 2a 2a 20 74 6f 20 78 4e 65 78 74 28 29  .  ** to xNext()
2d683 20 6f 72 20 78 43 6c 6f 73 65 28 29 2e 20 0a 20   or xClose(). . 
2d684 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68   */.  /* TODO(sh
2d685 65 73 73 29 20 63 75 72 72 65 6e 74 20 69 6d 70  ess) current imp
2d686 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 72 65 71 75  lementation requ
2d687 69 72 65 73 20 70 49 6e 70 75 74 20 74 6f 20 62  ires pInput to b
2d688 65 0a 20 20 2a 2a 20 6e 75 6c 2d 74 65 72 6d 69  e.  ** nul-termi
2d689 6e 61 74 65 64 2e 20 20 54 68 69 73 20 73 68 6f  nated.  This sho
2d68a 75 6c 64 20 65 69 74 68 65 72 20 62 65 20 66 69  uld either be fi
2d68b 78 65 64 2c 20 6f 72 20 70 49 6e 70 75 74 2f 6e  xed, or pInput/n
2d68c 42 79 74 65 73 0a 20 20 2a 2a 20 73 68 6f 75 6c  Bytes.  ** shoul
2d68d 64 20 62 65 20 63 6f 6e 76 65 72 74 65 64 20 74  d be converted t
2d68e 6f 20 7a 49 6e 70 75 74 2e 0a 20 20 2a 2f 0a 20  o zInput..  */. 
2d68f 20 69 6e 74 20 28 2a 78 4e 65 78 74 29 28 0a 20   int (*xNext)(. 
2d690 20 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e     sqlite3_token
2d691 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
2d692 72 73 6f 72 2c 20 20 20 2f 2a 20 54 6f 6b 65 6e  rsor,   /* Token
2d693 69 7a 65 72 20 63 75 72 73 6f 72 20 2a 2f 0a 20  izer cursor */. 
2d694 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a     const char **
2d695 70 70 54 6f 6b 65 6e 2c 20 69 6e 74 20 2a 70 6e  ppToken, int *pn
2d696 42 79 74 65 73 2c 20 20 2f 2a 20 4f 55 54 3a 20  Bytes,  /* OUT: 
2d697 4e 6f 72 6d 61 6c 69 7a 65 64 20 74 65 78 74 20  Normalized text 
2d698 66 6f 72 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  for token */.   
2d699 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f 66 66   int *piStartOff
2d69a 73 65 74 2c 20 20 2f 2a 20 4f 55 54 3a 20 42 79  set,  /* OUT: By
2d69b 74 65 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b  te offset of tok
2d69c 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
2d69d 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
2d69e 69 45 6e 64 4f 66 66 73 65 74 2c 20 20 20 20 2f  iEndOffset,    /
2d69f 2a 20 4f 55 54 3a 20 42 79 74 65 20 6f 66 66 73  * OUT: Byte offs
2d6a0 65 74 20 6f 66 20 65 6e 64 20 6f 66 20 74 6f 6b  et of end of tok
2d6a1 65 6e 20 69 6e 20 69 6e 70 75 74 20 62 75 66 66  en in input buff
2d6a2 65 72 20 2a 2f 0a 20 20 20 20 69 6e 74 20 2a 70  er */.    int *p
2d6a3 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20 2f  iPosition      /
2d6a4 2a 20 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66  * OUT: Number of
2d6a5 20 74 6f 6b 65 6e 73 20 72 65 74 75 72 6e 65 64   tokens returned
2d6a6 20 62 65 66 6f 72 65 20 74 68 69 73 20 6f 6e 65   before this one
2d6a7 20 2a 2f 0a 20 20 29 3b 0a 7d 3b 0a 0a 73 74 72   */.  );.};..str
2d6a8 75 63 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  uct sqlite3_toke
2d6a9 6e 69 7a 65 72 20 7b 0a 20 20 63 6f 6e 73 74 20  nizer {.  const 
2d6aa 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
2d6ab 72 5f 6d 6f 64 75 6c 65 20 2a 70 4d 6f 64 75 6c  r_module *pModul
2d6ac 65 3b 20 20 2f 2a 20 54 68 65 20 6d 6f 64 75 6c  e;  /* The modul
2d6ad 65 20 66 6f 72 20 74 68 69 73 20 74 6f 6b 65 6e  e for this token
2d6ae 69 7a 65 72 20 2a 2f 0a 20 20 2f 2a 20 54 6f 6b  izer */.  /* Tok
2d6af 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65 6e 74  enizer implement
2d6b0 61 74 69 6f 6e 73 20 77 69 6c 6c 20 74 79 70 69  ations will typi
2d6b1 63 61 6c 6c 79 20 61 64 64 20 61 64 64 69 74 69  cally add additi
2d6b2 6f 6e 61 6c 20 66 69 65 6c 64 73 20 2a 2f 0a 7d  onal fields */.}
2d6b3 3b 0a 0a 73 74 72 75 63 74 20 73 71 6c 69 74 65  ;..struct sqlite
2d6b4 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  3_tokenizer_curs
2d6b5 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  or {.  sqlite3_t
2d6b6 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
2d6b7 69 7a 65 72 3b 20 20 20 20 20 20 20 2f 2a 20 54  izer;       /* T
2d6b8 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 74 68 69  okenizer for thi
2d6b9 73 20 63 75 72 73 6f 72 2e 20 2a 2f 0a 20 20 2f  s cursor. */.  /
2d6ba 2a 20 54 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c  * Tokenizer impl
2d6bb 65 6d 65 6e 74 61 74 69 6f 6e 73 20 77 69 6c 6c  ementations will
2d6bc 20 74 79 70 69 63 61 6c 6c 79 20 61 64 64 20 61   typically add a
2d6bd 64 64 69 74 69 6f 6e 61 6c 20 66 69 65 6c 64 73  dditional fields
2d6be 20 2a 2f 0a 7d 3b 0a 0a 23 65 6e 64 69 66 20 2f   */.};..#endif /
2d6bf 2a 20 5f 46 54 53 33 5f 54 4f 4b 45 4e 49 5a 45  * _FTS3_TOKENIZE
2d6c0 52 5f 48 5f 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  R_H_ */../******
2d6c1 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
2d6c2 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 2e 68  fts3_tokenizer.h
2d6c3 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
2d6c4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6c5 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
2d6c6 2a 2a 2a 2a 2a 2a 2a 2a 20 43 6f 6e 74 69 6e 75  ******** Continu
2d6c7 69 6e 67 20 77 68 65 72 65 20 77 65 20 6c 65 66  ing where we lef
2d6c8 74 20 6f 66 66 20 69 6e 20 66 74 73 33 2e 63 20  t off in fts3.c 
2d6c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d6ca 2a 2a 2a 2a 2a 2a 2a 2f 0a 23 69 66 6e 64 65 66  *******/.#ifndef
2d6cb 20 53 51 4c 49 54 45 5f 43 4f 52 45 20 0a 20 20   SQLITE_CORE .  
2d6cc 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e  SQLITE_EXTENSION
2d6cd 5f 49 4e 49 54 31 0a 23 65 6e 64 69 66 0a 0a 0a  _INIT1.#endif...
2d6ce 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4d  /* TODO(shess) M
2d6cf 41 4e 2c 20 74 68 69 73 20 74 68 69 6e 67 20 6e  AN, this thing n
2d6d0 65 65 64 73 20 73 6f 6d 65 20 72 65 66 61 63 74  eeds some refact
2d6d1 6f 72 69 6e 67 2e 20 20 41 74 20 6d 69 6e 69 6d  oring.  At minim
2d6d2 75 6d 2c 20 69 74 0a 2a 2a 20 77 6f 75 6c 64 20  um, it.** would 
2d6d3 62 65 20 6e 69 63 65 20 74 6f 20 6f 72 64 65 72  be nice to order
2d6d4 20 74 68 65 20 66 69 6c 65 20 62 65 74 74 65 72   the file better
2d6d5 2c 20 70 65 72 68 61 70 73 20 73 6f 6d 65 74 68  , perhaps someth
2d6d6 69 6e 67 20 61 6c 6f 6e 67 20 74 68 65 0a 2a 2a  ing along the.**
2d6d7 20 6c 69 6e 65 73 20 6f 66 3a 0a 2a 2a 0a 2a 2a   lines of:.**.**
2d6d8 20 20 2d 20 75 74 69 6c 69 74 79 20 66 75 6e 63    - utility func
2d6d9 74 69 6f 6e 73 0a 2a 2a 20 20 2d 20 74 61 62 6c  tions.**  - tabl
2d6da 65 20 73 65 74 75 70 20 66 75 6e 63 74 69 6f 6e  e setup function
2d6db 73 0a 2a 2a 20 20 2d 20 74 61 62 6c 65 20 75 70  s.**  - table up
2d6dc 64 61 74 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a  date functions.*
2d6dd 2a 20 20 2d 20 74 61 62 6c 65 20 71 75 65 72 79  *  - table query
2d6de 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2a 0a 2a 2a   functions.**.**
2d6df 20 50 75 74 20 74 68 65 20 71 75 65 72 79 20 66   Put the query f
2d6e0 75 6e 63 74 69 6f 6e 73 20 6c 61 73 74 20 62 65  unctions last be
2d6e1 63 61 75 73 65 20 74 68 65 79 27 72 65 20 6c 69  cause they're li
2d6e2 6b 65 6c 79 20 74 6f 20 72 65 66 65 72 65 6e 63  kely to referenc
2d6e3 65 0a 2a 2a 20 74 79 70 65 64 65 66 73 20 6f 72  e.** typedefs or
2d6e4 20 66 75 6e 63 74 69 6f 6e 73 20 66 72 6f 6d 20   functions from 
2d6e5 74 68 65 20 74 61 62 6c 65 20 75 70 64 61 74 65  the table update
2d6e6 20 73 65 63 74 69 6f 6e 2e 0a 2a 2f 0a 0a 23 69   section..*/..#i
2d6e7 66 20 30 0a 23 20 64 65 66 69 6e 65 20 46 54 53  f 0.# define FTS
2d6e8 54 52 41 43 45 28 41 29 20 20 70 72 69 6e 74 66  TRACE(A)  printf
2d6e9 20 41 3b 20 66 66 6c 75 73 68 28 73 74 64 6f 75   A; fflush(stdou
2d6ea 74 29 0a 23 65 6c 73 65 0a 23 20 64 65 66 69 6e  t).#else.# defin
2d6eb 65 20 46 54 53 54 52 41 43 45 28 41 29 0a 23 65  e FTSTRACE(A).#e
2d6ec 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 44 65 66 61  ndif../*.** Defa
2d6ed 75 6c 74 20 73 70 61 6e 20 66 6f 72 20 4e 45 41  ult span for NEA
2d6ee 52 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2f 0a  R operators..*/.
2d6ef 23 64 65 66 69 6e 65 20 53 51 4c 49 54 45 5f 46  #define SQLITE_F
2d6f0 54 53 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52  TS3_DEFAULT_NEAR
2d6f1 5f 50 41 52 41 4d 20 31 30 0a 0a 2f 2a 20 49 74  _PARAM 10../* It
2d6f2 20 69 73 20 6e 6f 74 20 73 61 66 65 20 74 6f 20   is not safe to 
2d6f3 63 61 6c 6c 20 69 73 73 70 61 63 65 28 29 2c 20  call isspace(), 
2d6f4 74 6f 6c 6f 77 65 72 28 29 2c 20 6f 72 20 69 73  tolower(), or is
2d6f5 61 6c 6e 75 6d 28 29 20 6f 6e 0a 2a 2a 20 68 69  alnum() on.** hi
2d6f6 2d 62 69 74 2d 73 65 74 20 63 68 61 72 61 63 74  -bit-set charact
2d6f7 65 72 73 2e 20 20 54 68 69 73 20 69 73 20 74 68  ers.  This is th
2d6f8 65 20 73 61 6d 65 20 73 6f 6c 75 74 69 6f 6e 20  e same solution 
2d6f9 75 73 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 74  used in the.** t
2d6fa 6f 6b 65 6e 69 7a 65 72 2e 0a 2a 2f 0a 2f 2a 20  okenizer..*/./* 
2d6fb 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 65 20  TODO(shess) The 
2d6fc 73 6e 69 70 70 65 74 2d 67 65 6e 65 72 61 74 69  snippet-generati
2d6fd 6f 6e 20 63 6f 64 65 20 73 68 6f 75 6c 64 20 62  on code should b
2d6fe 65 20 75 73 69 6e 67 20 74 68 65 0a 2a 2a 20 74  e using the.** t
2d6ff 6f 6b 65 6e 69 7a 65 72 2d 67 65 6e 65 72 61 74  okenizer-generat
2d700 65 64 20 74 6f 6b 65 6e 73 20 72 61 74 68 65 72  ed tokens rather
2d701 20 74 68 61 6e 20 64 6f 69 6e 67 20 69 74 73 20   than doing its 
2d702 6f 77 6e 20 6c 6f 63 61 6c 0a 2a 2a 20 74 6f 6b  own local.** tok
2d703 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a 2f 0a 2f 2a  enization..*/./*
2d704 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49 73 20   TODO(shess) Is 
2d705 5f 5f 69 73 61 73 63 69 69 28 29 20 61 20 70 6f  __isascii() a po
2d706 72 74 61 62 6c 65 20 76 65 72 73 69 6f 6e 20 6f  rtable version o
2d707 66 20 28 63 26 30 78 38 30 29 3d 3d 30 3f 20 2a  f (c&0x80)==0? *
2d708 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 61 66  /.static int saf
2d709 65 5f 69 73 73 70 61 63 65 28 63 68 61 72 20 63  e_isspace(char c
2d70a 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 63 26 30  ){.  return (c&0
2d70b 78 38 30 29 3d 3d 30 20 3f 20 69 73 73 70 61 63  x80)==0 ? isspac
2d70c 65 28 63 29 20 3a 20 30 3b 0a 7d 0a 73 74 61 74  e(c) : 0;.}.stat
2d70d 69 63 20 69 6e 74 20 73 61 66 65 5f 74 6f 6c 6f  ic int safe_tolo
2d70e 77 65 72 28 63 68 61 72 20 63 29 7b 0a 20 20 72  wer(char c){.  r
2d70f 65 74 75 72 6e 20 28 63 26 30 78 38 30 29 3d 3d  eturn (c&0x80)==
2d710 30 20 3f 20 74 6f 6c 6f 77 65 72 28 63 29 20 3a  0 ? tolower(c) :
2d711 20 63 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74   c;.}.static int
2d712 20 73 61 66 65 5f 69 73 61 6c 6e 75 6d 28 63 68   safe_isalnum(ch
2d713 61 72 20 63 29 7b 0a 20 20 72 65 74 75 72 6e 20  ar c){.  return 
2d714 28 63 26 30 78 38 30 29 3d 3d 30 20 3f 20 69 73  (c&0x80)==0 ? is
2d715 61 6c 6e 75 6d 28 63 29 20 3a 20 30 3b 0a 7d 0a  alnum(c) : 0;.}.
2d716 0a 74 79 70 65 64 65 66 20 65 6e 75 6d 20 44 6f  .typedef enum Do
2d717 63 4c 69 73 74 54 79 70 65 20 7b 0a 20 20 44 4c  cListType {.  DL
2d718 5f 44 4f 43 49 44 53 2c 20 20 20 20 20 20 20 20  _DOCIDS,        
2d719 20 20 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20        /* docids 
2d71a 6f 6e 6c 79 20 2a 2f 0a 20 20 44 4c 5f 50 4f 53  only */.  DL_POS
2d71b 49 54 49 4f 4e 53 2c 20 20 20 20 20 20 20 20 20  ITIONS,         
2d71c 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b 20 70 6f    /* docids + po
2d71d 73 69 74 69 6f 6e 73 20 2a 2f 0a 20 20 44 4c 5f  sitions */.  DL_
2d71e 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54  POSITIONS_OFFSET
2d71f 53 20 20 20 20 2f 2a 20 64 6f 63 69 64 73 20 2b  S    /* docids +
2d720 20 70 6f 73 69 74 69 6f 6e 73 20 2b 20 6f 66 66   positions + off
2d721 73 65 74 73 20 2a 2f 0a 7d 20 44 6f 63 4c 69 73  sets */.} DocLis
2d722 74 54 79 70 65 3b 0a 0a 2f 2a 0a 2a 2a 20 42 79  tType;../*.** By
2d723 20 64 65 66 61 75 6c 74 2c 20 6f 6e 6c 79 20 70   default, only p
2d724 6f 73 69 74 69 6f 6e 73 20 61 6e 64 20 6e 6f 74  ositions and not
2d725 20 6f 66 66 73 65 74 73 20 61 72 65 20 73 74 6f   offsets are sto
2d726 72 65 64 20 69 6e 20 74 68 65 20 64 6f 63 6c 69  red in the docli
2d727 73 74 73 2e 0a 2a 2a 20 54 6f 20 63 68 61 6e 67  sts..** To chang
2d728 65 20 74 68 69 73 20 73 6f 20 74 68 61 74 20 6f  e this so that o
2d729 66 66 73 65 74 73 20 61 72 65 20 73 74 6f 72 65  ffsets are store
2d72a 64 20 74 6f 6f 2c 20 63 6f 6d 70 69 6c 65 20 77  d too, compile w
2d72b 69 74 68 0a 2a 2a 0a 2a 2a 20 20 20 20 20 20 20  ith.**.**       
2d72c 20 20 20 2d 44 44 4c 5f 44 45 46 41 55 4c 54 3d     -DDL_DEFAULT=
2d72d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f 46 46  DL_POSITIONS_OFF
2d72e 53 45 54 53 0a 2a 2a 0a 2a 2a 20 49 66 20 44 4c  SETS.**.** If DL
2d72f 5f 44 45 46 41 55 4c 54 20 69 73 20 73 65 74 20  _DEFAULT is set 
2d730 74 6f 20 44 4c 5f 44 4f 43 49 44 53 2c 20 79 6f  to DL_DOCIDS, yo
2d731 75 72 20 74 61 62 6c 65 20 63 61 6e 20 6f 6e 6c  ur table can onl
2d732 79 20 62 65 20 69 6e 73 65 72 74 65 64 0a 2a 2a  y be inserted.**
2d733 20 69 6e 74 6f 20 28 6e 6f 20 64 65 6c 65 74 65   into (no delete
2d734 73 20 6f 72 20 75 70 64 61 74 65 73 29 2e 0a 2a  s or updates)..*
2d735 2f 0a 23 69 66 6e 64 65 66 20 44 4c 5f 44 45 46  /.#ifndef DL_DEF
2d736 41 55 4c 54 0a 23 20 64 65 66 69 6e 65 20 44 4c  AULT.# define DL
2d737 5f 44 45 46 41 55 4c 54 20 44 4c 5f 50 4f 53 49  _DEFAULT DL_POSI
2d738 54 49 4f 4e 53 0a 23 65 6e 64 69 66 0a 0a 65 6e  TIONS.#endif..en
2d739 75 6d 20 7b 0a 20 20 50 4f 53 5f 45 4e 44 20 3d  um {.  POS_END =
2d73a 20 30 2c 20 20 20 20 20 20 20 20 2f 2a 20 65 6e   0,        /* en
2d73b 64 20 6f 66 20 74 68 69 73 20 70 6f 73 69 74 69  d of this positi
2d73c 6f 6e 20 6c 69 73 74 20 2a 2f 0a 20 20 50 4f 53  on list */.  POS
2d73d 5f 43 4f 4c 55 4d 4e 2c 20 20 20 20 20 20 20 20  _COLUMN,        
2d73e 20 2f 2a 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20   /* followed by 
2d73f 6e 65 77 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  new column numbe
2d740 72 20 2a 2f 0a 20 20 50 4f 53 5f 42 41 53 45 0a  r */.  POS_BASE.
2d741 7d 3b 0a 0a 2f 2a 20 4d 45 52 47 45 5f 43 4f 55  };../* MERGE_COU
2d742 4e 54 20 63 6f 6e 74 72 6f 6c 73 20 68 6f 77 20  NT controls how 
2d743 6f 66 74 65 6e 20 77 65 20 6d 65 72 67 65 20 73  often we merge s
2d744 65 67 6d 65 6e 74 73 20 28 73 65 65 20 63 6f 6d  egments (see com
2d745 6d 65 6e 74 20 61 74 0a 2a 2a 20 74 6f 70 20 6f  ment at.** top o
2d746 66 20 66 69 6c 65 29 2e 0a 2a 2f 0a 23 64 65 66  f file)..*/.#def
2d747 69 6e 65 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  ine MERGE_COUNT 
2d748 31 36 0a 0a 2f 2a 20 75 74 69 6c 69 74 79 20 66  16../* utility f
2d749 75 6e 63 74 69 6f 6e 73 20 2a 2f 0a 0a 2f 2a 20  unctions */../* 
2d74a 43 4c 45 41 52 28 29 20 61 6e 64 20 53 43 52 41  CLEAR() and SCRA
2d74b 4d 42 4c 45 28 29 20 61 62 73 74 72 61 63 74 20  MBLE() abstract 
2d74c 6d 65 6d 73 65 74 28 29 20 6f 6e 20 61 20 70 6f  memset() on a po
2d74d 69 6e 74 65 72 20 74 6f 20 61 20 73 69 6e 67 6c  inter to a singl
2d74e 65 0a 2a 2a 20 72 65 63 6f 72 64 20 74 6f 20 70  e.** record to p
2d74f 72 65 76 65 6e 74 20 65 72 72 6f 72 73 20 6f 66  revent errors of
2d750 20 74 68 65 20 66 6f 72 6d 3a 0a 2a 2a 0a 2a 2a   the form:.**.**
2d751 20 6d 79 5f 66 75 6e 63 74 69 6f 6e 28 53 6f 6d   my_function(Som
2d752 65 54 79 70 65 20 2a 62 29 7b 0a 2a 2a 20 20 20  eType *b){.**   
2d753 6d 65 6d 73 65 74 28 62 2c 20 27 5c 30 27 2c 20  memset(b, '\0', 
2d754 73 69 7a 65 6f 66 28 62 29 29 3b 20 20 2f 2f 20  sizeof(b));  // 
2d755 73 69 7a 65 6f 66 28 62 29 21 3d 73 69 7a 65 6f  sizeof(b)!=sizeo
2d756 66 28 2a 62 29 0a 2a 2a 20 7d 0a 2a 2f 0a 2f 2a  f(*b).** }.*/./*
2d757 20 54 4f 44 4f 28 73 68 65 73 73 29 20 4f 62 76   TODO(shess) Obv
2d758 69 6f 75 73 20 63 61 6e 64 69 64 61 74 65 73 20  ious candidates 
2d759 66 6f 72 20 61 20 68 65 61 64 65 72 20 66 69 6c  for a header fil
2d75a 65 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 43 4c  e. */.#define CL
2d75b 45 41 52 28 62 29 20 6d 65 6d 73 65 74 28 62 2c  EAR(b) memset(b,
2d75c 20 27 5c 30 27 2c 20 73 69 7a 65 6f 66 28 2a 28   '\0', sizeof(*(
2d75d 62 29 29 29 0a 0a 23 69 66 6e 64 65 66 20 4e 44  b)))..#ifndef ND
2d75e 45 42 55 47 0a 23 20 20 64 65 66 69 6e 65 20 53  EBUG.#  define S
2d75f 43 52 41 4d 42 4c 45 28 62 29 20 6d 65 6d 73 65  CRAMBLE(b) memse
2d760 74 28 62 2c 20 30 78 35 35 2c 20 73 69 7a 65 6f  t(b, 0x55, sizeo
2d761 66 28 2a 28 62 29 29 29 0a 23 65 6c 73 65 0a 23  f(*(b))).#else.#
2d762 20 20 64 65 66 69 6e 65 20 53 43 52 41 4d 42 4c    define SCRAMBL
2d763 45 28 62 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20  E(b).#endif../* 
2d764 57 65 20 6d 61 79 20 6e 65 65 64 20 75 70 20 74  We may need up t
2d765 6f 20 56 41 52 49 4e 54 5f 4d 41 58 20 62 79 74  o VARINT_MAX byt
2d766 65 73 20 74 6f 20 73 74 6f 72 65 20 61 6e 20 65  es to store an e
2d767 6e 63 6f 64 65 64 20 36 34 2d 62 69 74 20 69 6e  ncoded 64-bit in
2d768 74 65 67 65 72 2e 20 2a 2f 0a 23 64 65 66 69 6e  teger. */.#defin
2d769 65 20 56 41 52 49 4e 54 5f 4d 41 58 20 31 30 0a  e VARINT_MAX 10.
2d76a 0a 2f 2a 20 57 72 69 74 65 20 61 20 36 34 2d 62  ./* Write a 64-b
2d76b 69 74 20 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67  it variable-leng
2d76c 74 68 20 69 6e 74 65 67 65 72 20 74 6f 20 6d 65  th integer to me
2d76d 6d 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74  mory starting at
2d76e 20 70 5b 30 5d 2e 0a 20 2a 20 54 68 65 20 6c 65   p[0].. * The le
2d76f 6e 67 74 68 20 6f 66 20 64 61 74 61 20 77 72 69  ngth of data wri
2d770 74 74 65 6e 20 77 69 6c 6c 20 62 65 20 62 65 74  tten will be bet
2d771 77 65 65 6e 20 31 20 61 6e 64 20 56 41 52 49 4e  ween 1 and VARIN
2d772 54 5f 4d 41 58 20 62 79 74 65 73 2e 0a 20 2a 20  T_MAX bytes.. * 
2d773 54 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  The number of by
2d774 74 65 73 20 77 72 69 74 74 65 6e 20 69 73 20 72  tes written is r
2d775 65 74 75 72 6e 65 64 2e 20 2a 2f 0a 73 74 61 74  eturned. */.stat
2d776 69 63 20 69 6e 74 20 66 74 73 33 50 75 74 56 61  ic int fts3PutVa
2d777 72 69 6e 74 28 63 68 61 72 20 2a 70 2c 20 73 71  rint(char *p, sq
2d778 6c 69 74 65 5f 69 6e 74 36 34 20 76 29 7b 0a 20  lite_int64 v){. 
2d779 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
2d77a 71 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  q = (unsigned ch
2d77b 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69 74  ar *) p;.  sqlit
2d77c 65 5f 75 69 6e 74 36 34 20 76 75 20 3d 20 76 3b  e_uint64 vu = v;
2d77d 0a 20 20 64 6f 7b 0a 20 20 20 20 2a 71 2b 2b 20  .  do{.    *q++ 
2d77e 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68 61 72  = (unsigned char
2d77f 29 20 28 28 76 75 20 26 20 30 78 37 66 29 20 7c  ) ((vu & 0x7f) |
2d780 20 30 78 38 30 29 3b 0a 20 20 20 20 76 75 20 3e   0x80);.    vu >
2d781 3e 3d 20 37 3b 0a 20 20 7d 77 68 69 6c 65 28 20  >= 7;.  }while( 
2d782 76 75 21 3d 30 20 29 3b 0a 20 20 71 5b 2d 31 5d  vu!=0 );.  q[-1]
2d783 20 26 3d 20 30 78 37 66 3b 20 20 2f 2a 20 74 75   &= 0x7f;  /* tu
2d784 72 6e 20 6f 66 66 20 68 69 67 68 20 62 69 74 20  rn off high bit 
2d785 69 6e 20 66 69 6e 61 6c 20 62 79 74 65 20 2a 2f  in final byte */
2d786 0a 20 20 61 73 73 65 72 74 28 20 71 20 2d 20 28  .  assert( q - (
2d787 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29  unsigned char *)
2d788 70 20 3c 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20  p <= VARINT_MAX 
2d789 29 3b 0a 20 20 72 65 74 75 72 6e 20 28 69 6e 74  );.  return (int
2d78a 29 20 28 71 20 2d 20 28 75 6e 73 69 67 6e 65 64  ) (q - (unsigned
2d78b 20 63 68 61 72 20 2a 29 70 29 3b 0a 7d 0a 0a 2f   char *)p);.}../
2d78c 2a 20 52 65 61 64 20 61 20 36 34 2d 62 69 74 20  * Read a 64-bit 
2d78d 76 61 72 69 61 62 6c 65 2d 6c 65 6e 67 74 68 20  variable-length 
2d78e 69 6e 74 65 67 65 72 20 66 72 6f 6d 20 6d 65 6d  integer from mem
2d78f 6f 72 79 20 73 74 61 72 74 69 6e 67 20 61 74 20  ory starting at 
2d790 70 5b 30 5d 2e 0a 20 2a 20 52 65 74 75 72 6e 20  p[0].. * Return 
2d791 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 62 79  the number of by
2d792 74 65 73 20 72 65 61 64 2c 20 6f 72 20 30 20 6f  tes read, or 0 o
2d793 6e 20 65 72 72 6f 72 2e 0a 20 2a 20 54 68 65 20  n error.. * The 
2d794 76 61 6c 75 65 20 69 73 20 73 74 6f 72 65 64 20  value is stored 
2d795 69 6e 20 2a 76 2e 20 2a 2f 0a 73 74 61 74 69 63  in *v. */.static
2d796 20 69 6e 74 20 66 74 73 33 47 65 74 56 61 72 69   int fts3GetVari
2d797 6e 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  nt(const char *p
2d798 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a  , sqlite_int64 *
2d799 76 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e 73 69  v){.  const unsi
2d79a 67 6e 65 64 20 63 68 61 72 20 2a 71 20 3d 20 28  gned char *q = (
2d79b 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20 63  const unsigned c
2d79c 68 61 72 20 2a 29 20 70 3b 0a 20 20 73 71 6c 69  har *) p;.  sqli
2d79d 74 65 5f 75 69 6e 74 36 34 20 78 20 3d 20 30 2c  te_uint64 x = 0,
2d79e 20 79 20 3d 20 31 3b 0a 20 20 77 68 69 6c 65 28   y = 1;.  while(
2d79f 20 28 2a 71 20 26 20 30 78 38 30 29 20 3d 3d 20   (*q & 0x80) == 
2d7a0 30 78 38 30 20 29 7b 0a 20 20 20 20 78 20 2b 3d  0x80 ){.    x +=
2d7a1 20 79 20 2a 20 28 2a 71 2b 2b 20 26 20 30 78 37   y * (*q++ & 0x7
2d7a2 66 29 3b 0a 20 20 20 20 79 20 3c 3c 3d 20 37 3b  f);.    y <<= 7;
2d7a3 0a 20 20 20 20 69 66 28 20 71 20 2d 20 28 75 6e  .    if( q - (un
2d7a4 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70 20  signed char *)p 
2d7a5 3e 3d 20 56 41 52 49 4e 54 5f 4d 41 58 20 29 7b  >= VARINT_MAX ){
2d7a6 20 20 2f 2a 20 62 61 64 20 64 61 74 61 20 2a 2f    /* bad data */
2d7a7 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 30  .      assert( 0
2d7a8 20 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   );.      return
2d7a9 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   0;.    }.  }.  
2d7aa 78 20 2b 3d 20 79 20 2a 20 28 2a 71 2b 2b 29 3b  x += y * (*q++);
2d7ab 0a 20 20 2a 76 20 3d 20 28 73 71 6c 69 74 65 5f  .  *v = (sqlite_
2d7ac 69 6e 74 36 34 29 20 78 3b 0a 20 20 72 65 74 75  int64) x;.  retu
2d7ad 72 6e 20 28 69 6e 74 29 20 28 71 20 2d 20 28 75  rn (int) (q - (u
2d7ae 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a 29 70  nsigned char *)p
2d7af 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  );.}..static int
2d7b0 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
2d7b1 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 2c 20  (const char *p, 
2d7b2 69 6e 74 20 2a 70 69 29 7b 0a 20 73 71 6c 69 74  int *pi){. sqlit
2d7b3 65 5f 69 6e 74 36 34 20 69 3b 0a 20 69 6e 74 20  e_int64 i;. int 
2d7b4 72 65 74 20 3d 20 66 74 73 33 47 65 74 56 61 72  ret = fts3GetVar
2d7b5 69 6e 74 28 70 2c 20 26 69 29 3b 0a 20 2a 70 69  int(p, &i);. *pi
2d7b6 20 3d 20 28 69 6e 74 29 20 69 3b 0a 20 61 73 73   = (int) i;. ass
2d7b7 65 72 74 28 20 2a 70 69 3d 3d 69 20 29 3b 0a 20  ert( *pi==i );. 
2d7b8 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 0a 2f  return ret;.}../
2d7b9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d7ba 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d7bb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d7bc 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d7bd 2a 2a 2a 2f 0a 2f 2a 20 44 61 74 61 42 75 66 66  ***/./* DataBuff
2d7be 65 72 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f  er is used to co
2d7bf 6c 6c 65 63 74 20 64 61 74 61 20 69 6e 74 6f 20  llect data into 
2d7c0 61 20 62 75 66 66 65 72 20 69 6e 20 70 69 65 63  a buffer in piec
2d7c1 65 6d 65 61 6c 0a 2a 2a 20 66 61 73 68 69 6f 6e  emeal.** fashion
2d7c2 2e 20 20 49 74 20 69 6d 70 6c 65 6d 65 6e 74 73  .  It implements
2d7c3 20 74 68 65 20 75 73 75 61 6c 20 64 69 73 74 69   the usual disti
2d7c4 6e 63 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 61  nction between a
2d7c5 6d 6f 75 6e 74 20 6f 66 0a 2a 2a 20 64 61 74 61  mount of.** data
2d7c6 20 63 75 72 72 65 6e 74 6c 79 20 73 74 6f 72 65   currently store
2d7c7 64 20 28 6e 44 61 74 61 29 20 61 6e 64 20 62 75  d (nData) and bu
2d7c8 66 66 65 72 20 63 61 70 61 63 69 74 79 20 28 6e  ffer capacity (n
2d7c9 43 61 70 61 63 69 74 79 29 2e 0a 2a 2a 0a 2a 2a  Capacity)..**.**
2d7ca 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 20   dataBufferInit 
2d7cb 2d 20 63 72 65 61 74 65 20 61 20 62 75 66 66 65  - create a buffe
2d7cc 72 20 77 69 74 68 20 67 69 76 65 6e 20 69 6e 69  r with given ini
2d7cd 74 69 61 6c 20 63 61 70 61 63 69 74 79 2e 0a 2a  tial capacity..*
2d7ce 2a 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65  * dataBufferRese
2d7cf 74 20 2d 20 66 6f 72 67 65 74 20 62 75 66 66 65  t - forget buffe
2d7d0 72 27 73 20 64 61 74 61 2c 20 72 65 74 61 69 6e  r's data, retain
2d7d1 69 6e 67 20 63 61 70 61 63 69 74 79 2e 0a 2a 2a  ing capacity..**
2d7d2 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72   dataBufferDestr
2d7d3 6f 79 20 2d 20 66 72 65 65 20 62 75 66 66 65 72  oy - free buffer
2d7d4 27 73 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61  's data..** data
2d7d5 42 75 66 66 65 72 53 77 61 70 20 2d 20 73 77 61  BufferSwap - swa
2d7d6 70 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 74 77  p contents of tw
2d7d7 6f 20 62 75 66 66 65 72 73 2e 0a 2a 2a 20 64 61  o buffers..** da
2d7d8 74 61 42 75 66 66 65 72 45 78 70 61 6e 64 20 2d  taBufferExpand -
2d7d9 20 65 78 70 61 6e 64 20 63 61 70 61 63 69 74 79   expand capacity
2d7da 20 77 69 74 68 6f 75 74 20 61 64 64 69 6e 67 20   without adding 
2d7db 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61 42 75 66  data..** dataBuf
2d7dc 66 65 72 41 70 70 65 6e 64 20 2d 20 61 70 70 65  ferAppend - appe
2d7dd 6e 64 20 64 61 74 61 2e 0a 2a 2a 20 64 61 74 61  nd data..** data
2d7de 42 75 66 66 65 72 41 70 70 65 6e 64 32 20 2d 20  BufferAppend2 - 
2d7df 61 70 70 65 6e 64 20 74 77 6f 20 70 69 65 63 65  append two piece
2d7e0 73 20 6f 66 20 64 61 74 61 20 61 74 20 6f 6e 63  s of data at onc
2d7e1 65 2e 0a 2a 2a 20 64 61 74 61 42 75 66 66 65 72  e..** dataBuffer
2d7e2 52 65 70 6c 61 63 65 20 2d 20 72 65 70 6c 61 63  Replace - replac
2d7e3 65 20 62 75 66 66 65 72 27 73 20 64 61 74 61 2e  e buffer's data.
2d7e4 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75  .*/.typedef stru
2d7e5 63 74 20 44 61 74 61 42 75 66 66 65 72 20 7b 0a  ct DataBuffer {.
2d7e6 20 20 63 68 61 72 20 2a 70 44 61 74 61 3b 20 20    char *pData;  
2d7e7 20 20 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74          /* Point
2d7e8 65 72 20 74 6f 20 6d 61 6c 6c 6f 63 27 65 64 20  er to malloc'ed 
2d7e9 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 69 6e 74  buffer. */.  int
2d7ea 20 6e 43 61 70 61 63 69 74 79 3b 20 20 20 20 20   nCapacity;     
2d7eb 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66 20 70 44     /* Size of pD
2d7ec 61 74 61 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20  ata buffer. */. 
2d7ed 20 69 6e 74 20 6e 44 61 74 61 3b 20 20 20 20 20   int nData;     
2d7ee 20 20 20 20 20 20 20 2f 2a 20 45 6e 64 20 6f 66         /* End of
2d7ef 20 64 61 74 61 20 6c 6f 61 64 65 64 20 69 6e 74   data loaded int
2d7f0 6f 20 70 44 61 74 61 2e 20 2a 2f 0a 7d 20 44 61  o pData. */.} Da
2d7f1 74 61 42 75 66 66 65 72 3b 0a 0a 73 74 61 74 69  taBuffer;..stati
2d7f2 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
2d7f3 72 49 6e 69 74 28 44 61 74 61 42 75 66 66 65 72  rInit(DataBuffer
2d7f4 20 2a 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e   *pBuffer, int n
2d7f5 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73 73  Capacity){.  ass
2d7f6 65 72 74 28 20 6e 43 61 70 61 63 69 74 79 3e 3d  ert( nCapacity>=
2d7f7 30 20 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e  0 );.  pBuffer->
2d7f8 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20 70 42 75  nData = 0;.  pBu
2d7f9 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20  ffer->nCapacity 
2d7fa 3d 20 6e 43 61 70 61 63 69 74 79 3b 0a 20 20 70  = nCapacity;.  p
2d7fb 42 75 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20  Buffer->pData = 
2d7fc 6e 43 61 70 61 63 69 74 79 3d 3d 30 20 3f 20 4e  nCapacity==0 ? N
2d7fd 55 4c 4c 20 3a 20 73 71 6c 69 74 65 33 5f 6d 61  ULL : sqlite3_ma
2d7fe 6c 6c 6f 63 28 6e 43 61 70 61 63 69 74 79 29 3b  lloc(nCapacity);
2d7ff 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
2d800 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 44  ataBufferReset(D
2d801 61 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66  ataBuffer *pBuff
2d802 65 72 29 7b 0a 20 20 70 42 75 66 66 65 72 2d 3e  er){.  pBuffer->
2d803 6e 44 61 74 61 20 3d 20 30 3b 0a 7d 0a 73 74 61  nData = 0;.}.sta
2d804 74 69 63 20 76 6f 69 64 20 64 61 74 61 42 75 66  tic void dataBuf
2d805 66 65 72 44 65 73 74 72 6f 79 28 44 61 74 61 42  ferDestroy(DataB
2d806 75 66 66 65 72 20 2a 70 42 75 66 66 65 72 29 7b  uffer *pBuffer){
2d807 0a 20 20 69 66 28 20 70 42 75 66 66 65 72 2d 3e  .  if( pBuffer->
2d808 70 44 61 74 61 21 3d 4e 55 4c 4c 20 29 20 73 71  pData!=NULL ) sq
2d809 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75 66 66  lite3_free(pBuff
2d80a 65 72 2d 3e 70 44 61 74 61 29 3b 0a 20 20 53 43  er->pData);.  SC
2d80b 52 41 4d 42 4c 45 28 70 42 75 66 66 65 72 29 3b  RAMBLE(pBuffer);
2d80c 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
2d80d 61 74 61 42 75 66 66 65 72 53 77 61 70 28 44 61  ataBufferSwap(Da
2d80e 74 61 42 75 66 66 65 72 20 2a 70 42 75 66 66 65  taBuffer *pBuffe
2d80f 72 31 2c 20 44 61 74 61 42 75 66 66 65 72 20 2a  r1, DataBuffer *
2d810 70 42 75 66 66 65 72 32 29 7b 0a 20 20 44 61 74  pBuffer2){.  Dat
2d811 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 70  aBuffer tmp = *p
2d812 42 75 66 66 65 72 31 3b 0a 20 20 2a 70 42 75 66  Buffer1;.  *pBuf
2d813 66 65 72 31 20 3d 20 2a 70 42 75 66 66 65 72 32  fer1 = *pBuffer2
2d814 3b 0a 20 20 2a 70 42 75 66 66 65 72 32 20 3d 20  ;.  *pBuffer2 = 
2d815 74 6d 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  tmp;.}.static vo
2d816 69 64 20 64 61 74 61 42 75 66 66 65 72 45 78 70  id dataBufferExp
2d817 61 6e 64 28 44 61 74 61 42 75 66 66 65 72 20 2a  and(DataBuffer *
2d818 70 42 75 66 66 65 72 2c 20 69 6e 74 20 6e 41 64  pBuffer, int nAd
2d819 64 43 61 70 61 63 69 74 79 29 7b 0a 20 20 61 73  dCapacity){.  as
2d81a 73 65 72 74 28 20 6e 41 64 64 43 61 70 61 63 69  sert( nAddCapaci
2d81b 74 79 3e 30 20 29 3b 0a 20 20 2f 2a 20 54 4f 44  ty>0 );.  /* TOD
2d81c 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
2d81d 72 20 65 78 70 61 6e 64 69 6e 67 20 6d 6f 72 65  r expanding more
2d81e 20 61 67 67 72 65 73 73 69 76 65 6c 79 2e 20 20   aggressively.  
2d81f 4e 6f 74 65 20 74 68 61 74 20 74 68 65 0a 20 20  Note that the.  
2d820 2a 2a 20 75 6e 64 65 72 6c 79 69 6e 67 20 6d 61  ** underlying ma
2d821 6c 6c 6f 63 20 69 6d 70 6c 65 6d 65 6e 74 61 74  lloc implementat
2d822 69 6f 6e 20 6d 61 79 20 74 61 6b 65 20 63 61 72  ion may take car
2d823 65 20 6f 66 20 73 75 63 68 20 74 68 69 6e 67 73  e of such things
2d824 20 66 6f 72 0a 20 20 2a 2a 20 75 73 20 61 6c 72   for.  ** us alr
2d825 65 61 64 79 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  eady..  */.  if(
2d826 20 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2b   pBuffer->nData+
2d827 6e 41 64 64 43 61 70 61 63 69 74 79 3e 70 42 75  nAddCapacity>pBu
2d828 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79 20  ffer->nCapacity 
2d829 29 7b 0a 20 20 20 20 70 42 75 66 66 65 72 2d 3e  ){.    pBuffer->
2d82a 6e 43 61 70 61 63 69 74 79 20 3d 20 70 42 75 66  nCapacity = pBuf
2d82b 66 65 72 2d 3e 6e 44 61 74 61 2b 6e 41 64 64 43  fer->nData+nAddC
2d82c 61 70 61 63 69 74 79 3b 0a 20 20 20 20 70 42 75  apacity;.    pBu
2d82d 66 66 65 72 2d 3e 70 44 61 74 61 20 3d 20 73 71  ffer->pData = sq
2d82e 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 42  lite3_realloc(pB
2d82f 75 66 66 65 72 2d 3e 70 44 61 74 61 2c 20 70 42  uffer->pData, pB
2d830 75 66 66 65 72 2d 3e 6e 43 61 70 61 63 69 74 79  uffer->nCapacity
2d831 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
2d832 76 6f 69 64 20 64 61 74 61 42 75 66 66 65 72 41  void dataBufferA
2d833 70 70 65 6e 64 28 44 61 74 61 42 75 66 66 65 72  ppend(DataBuffer
2d834 20 2a 70 42 75 66 66 65 72 2c 0a 20 20 20 20 20   *pBuffer,.     
2d835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d836 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2d837 61 72 20 2a 70 53 6f 75 72 63 65 2c 20 69 6e 74  ar *pSource, int
2d838 20 6e 53 6f 75 72 63 65 29 7b 0a 20 20 61 73 73   nSource){.  ass
2d839 65 72 74 28 20 6e 53 6f 75 72 63 65 3e 30 20 26  ert( nSource>0 &
2d83a 26 20 70 53 6f 75 72 63 65 21 3d 4e 55 4c 4c 20  & pSource!=NULL 
2d83b 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 45  );.  dataBufferE
2d83c 78 70 61 6e 64 28 70 42 75 66 66 65 72 2c 20 6e  xpand(pBuffer, n
2d83d 53 6f 75 72 63 65 29 3b 0a 20 20 6d 65 6d 63 70  Source);.  memcp
2d83e 79 28 70 42 75 66 66 65 72 2d 3e 70 44 61 74 61  y(pBuffer->pData
2d83f 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61 74 61 2c  +pBuffer->nData,
2d840 20 70 53 6f 75 72 63 65 2c 20 6e 53 6f 75 72 63   pSource, nSourc
2d841 65 29 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e  e);.  pBuffer->n
2d842 44 61 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 3b  Data += nSource;
2d843 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  .}.static void d
2d844 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
2d845 28 44 61 74 61 42 75 66 66 65 72 20 2a 70 42 75  (DataBuffer *pBu
2d846 66 66 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ffer,.          
2d847 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d848 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a      const char *
2d849 70 53 6f 75 72 63 65 31 2c 20 69 6e 74 20 6e 53  pSource1, int nS
2d84a 6f 75 72 63 65 31 2c 0a 20 20 20 20 20 20 20 20  ource1,.        
2d84b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d84c 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
2d84d 20 2a 70 53 6f 75 72 63 65 32 2c 20 69 6e 74 20   *pSource2, int 
2d84e 6e 53 6f 75 72 63 65 32 29 7b 0a 20 20 61 73 73  nSource2){.  ass
2d84f 65 72 74 28 20 6e 53 6f 75 72 63 65 31 3e 30 20  ert( nSource1>0 
2d850 26 26 20 70 53 6f 75 72 63 65 31 21 3d 4e 55 4c  && pSource1!=NUL
2d851 4c 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  L );.  assert( n
2d852 53 6f 75 72 63 65 32 3e 30 20 26 26 20 70 53 6f  Source2>0 && pSo
2d853 75 72 63 65 32 21 3d 4e 55 4c 4c 20 29 3b 0a 20  urce2!=NULL );. 
2d854 20 64 61 74 61 42 75 66 66 65 72 45 78 70 61 6e   dataBufferExpan
2d855 64 28 70 42 75 66 66 65 72 2c 20 6e 53 6f 75 72  d(pBuffer, nSour
2d856 63 65 31 2b 6e 53 6f 75 72 63 65 32 29 3b 0a 20  ce1+nSource2);. 
2d857 20 6d 65 6d 63 70 79 28 70 42 75 66 66 65 72 2d   memcpy(pBuffer-
2d858 3e 70 44 61 74 61 2b 70 42 75 66 66 65 72 2d 3e  >pData+pBuffer->
2d859 6e 44 61 74 61 2c 20 70 53 6f 75 72 63 65 31 2c  nData, pSource1,
2d85a 20 6e 53 6f 75 72 63 65 31 29 3b 0a 20 20 6d 65   nSource1);.  me
2d85b 6d 63 70 79 28 70 42 75 66 66 65 72 2d 3e 70 44  mcpy(pBuffer->pD
2d85c 61 74 61 2b 70 42 75 66 66 65 72 2d 3e 6e 44 61  ata+pBuffer->nDa
2d85d 74 61 2b 6e 53 6f 75 72 63 65 31 2c 20 70 53 6f  ta+nSource1, pSo
2d85e 75 72 63 65 32 2c 20 6e 53 6f 75 72 63 65 32 29  urce2, nSource2)
2d85f 3b 0a 20 20 70 42 75 66 66 65 72 2d 3e 6e 44 61  ;.  pBuffer->nDa
2d860 74 61 20 2b 3d 20 6e 53 6f 75 72 63 65 31 2b 6e  ta += nSource1+n
2d861 53 6f 75 72 63 65 32 3b 0a 7d 0a 73 74 61 74 69  Source2;.}.stati
2d862 63 20 76 6f 69 64 20 64 61 74 61 42 75 66 66 65  c void dataBuffe
2d863 72 52 65 70 6c 61 63 65 28 44 61 74 61 42 75 66  rReplace(DataBuf
2d864 66 65 72 20 2a 70 42 75 66 66 65 72 2c 0a 20 20  fer *pBuffer,.  
2d865 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d866 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73              cons
2d867 74 20 63 68 61 72 20 2a 70 53 6f 75 72 63 65 2c  t char *pSource,
2d868 20 69 6e 74 20 6e 53 6f 75 72 63 65 29 7b 0a 20   int nSource){. 
2d869 20 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74   dataBufferReset
2d86a 28 70 42 75 66 66 65 72 29 3b 0a 20 20 64 61 74  (pBuffer);.  dat
2d86b 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 42  aBufferAppend(pB
2d86c 75 66 66 65 72 2c 20 70 53 6f 75 72 63 65 2c 20  uffer, pSource, 
2d86d 6e 53 6f 75 72 63 65 29 3b 0a 7d 0a 0a 2f 2a 20  nSource);.}../* 
2d86e 53 74 72 69 6e 67 42 75 66 66 65 72 20 69 73 20  StringBuffer is 
2d86f 61 20 6e 75 6c 6c 2d 74 65 72 6d 69 6e 61 74 65  a null-terminate
2d870 64 20 76 65 72 73 69 6f 6e 20 6f 66 20 44 61 74  d version of Dat
2d871 61 42 75 66 66 65 72 2e 20 2a 2f 0a 74 79 70 65  aBuffer. */.type
2d872 64 65 66 20 73 74 72 75 63 74 20 53 74 72 69 6e  def struct Strin
2d873 67 42 75 66 66 65 72 20 7b 0a 20 20 44 61 74 61  gBuffer {.  Data
2d874 42 75 66 66 65 72 20 62 3b 20 20 20 20 20 20 20  Buffer b;       
2d875 20 20 20 20 20 2f 2a 20 49 6e 63 6c 75 64 65 73       /* Includes
2d876 20 6e 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72   null terminator
2d877 2e 20 2a 2f 0a 7d 20 53 74 72 69 6e 67 42 75 66  . */.} StringBuf
2d878 66 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  fer;..static voi
2d879 64 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66  d initStringBuff
2d87a 65 72 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  er(StringBuffer 
2d87b 2a 73 62 29 7b 0a 20 20 64 61 74 61 42 75 66 66  *sb){.  dataBuff
2d87c 65 72 49 6e 69 74 28 26 73 62 2d 3e 62 2c 20 31  erInit(&sb->b, 1
2d87d 30 30 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  00);.  dataBuffe
2d87e 72 52 65 70 6c 61 63 65 28 26 73 62 2d 3e 62 2c  rReplace(&sb->b,
2d87f 20 22 22 2c 20 31 29 3b 0a 7d 0a 73 74 61 74 69   "", 1);.}.stati
2d880 63 20 69 6e 74 20 73 74 72 69 6e 67 42 75 66 66  c int stringBuff
2d881 65 72 4c 65 6e 67 74 68 28 53 74 72 69 6e 67 42  erLength(StringB
2d882 75 66 66 65 72 20 2a 73 62 29 7b 0a 20 20 72 65  uffer *sb){.  re
2d883 74 75 72 6e 20 73 62 2d 3e 62 2e 6e 44 61 74 61  turn sb->b.nData
2d884 2d 31 3b 0a 7d 0a 73 74 61 74 69 63 20 63 68 61  -1;.}.static cha
2d885 72 20 2a 73 74 72 69 6e 67 42 75 66 66 65 72 44  r *stringBufferD
2d886 61 74 61 28 53 74 72 69 6e 67 42 75 66 66 65 72  ata(StringBuffer
2d887 20 2a 73 62 29 7b 0a 20 20 72 65 74 75 72 6e 20   *sb){.  return 
2d888 73 62 2d 3e 62 2e 70 44 61 74 61 3b 0a 7d 0a 73  sb->b.pData;.}.s
2d889 74 61 74 69 63 20 76 6f 69 64 20 73 74 72 69 6e  tatic void strin
2d88a 67 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 53  gBufferDestroy(S
2d88b 74 72 69 6e 67 42 75 66 66 65 72 20 2a 73 62 29  tringBuffer *sb)
2d88c 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  {.  dataBufferDe
2d88d 73 74 72 6f 79 28 26 73 62 2d 3e 62 29 3b 0a 7d  stroy(&sb->b);.}
2d88e 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 61  ..static void na
2d88f 70 70 65 6e 64 28 53 74 72 69 6e 67 42 75 66 66  ppend(StringBuff
2d890 65 72 20 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68  er *sb, const ch
2d891 61 72 20 2a 7a 46 72 6f 6d 2c 20 69 6e 74 20 6e  ar *zFrom, int n
2d892 46 72 6f 6d 29 7b 0a 20 20 61 73 73 65 72 74 28  From){.  assert(
2d893 20 73 62 2d 3e 62 2e 6e 44 61 74 61 3e 30 20 29   sb->b.nData>0 )
2d894 3b 0a 20 20 69 66 28 20 6e 46 72 6f 6d 3e 30 20  ;.  if( nFrom>0 
2d895 29 7b 0a 20 20 20 20 73 62 2d 3e 62 2e 6e 44 61  ){.    sb->b.nDa
2d896 74 61 2d 2d 3b 0a 20 20 20 20 64 61 74 61 42 75  ta--;.    dataBu
2d897 66 66 65 72 41 70 70 65 6e 64 32 28 26 73 62 2d  fferAppend2(&sb-
2d898 3e 62 2c 20 7a 46 72 6f 6d 2c 20 6e 46 72 6f 6d  >b, zFrom, nFrom
2d899 2c 20 22 22 2c 20 31 29 3b 0a 20 20 7d 0a 7d 0a  , "", 1);.  }.}.
2d89a 73 74 61 74 69 63 20 76 6f 69 64 20 61 70 70 65  static void appe
2d89b 6e 64 28 53 74 72 69 6e 67 42 75 66 66 65 72 20  nd(StringBuffer 
2d89c 2a 73 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  *sb, const char 
2d89d 2a 7a 46 72 6f 6d 29 7b 0a 20 20 6e 61 70 70 65  *zFrom){.  nappe
2d89e 6e 64 28 73 62 2c 20 7a 46 72 6f 6d 2c 20 73 74  nd(sb, zFrom, st
2d89f 72 6c 65 6e 28 7a 46 72 6f 6d 29 29 3b 0a 7d 0a  rlen(zFrom));.}.
2d8a0 0a 2f 2a 20 41 70 70 65 6e 64 20 61 20 6c 69 73  ./* Append a lis
2d8a1 74 20 6f 66 20 73 74 72 69 6e 67 73 20 73 65 70  t of strings sep
2d8a2 61 72 61 74 65 64 20 62 79 20 63 6f 6d 6d 61 73  arated by commas
2d8a3 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  . */.static void
2d8a4 20 61 70 70 65 6e 64 4c 69 73 74 28 53 74 72 69   appendList(Stri
2d8a5 6e 67 42 75 66 66 65 72 20 2a 73 62 2c 20 69 6e  ngBuffer *sb, in
2d8a6 74 20 6e 53 74 72 69 6e 67 2c 20 63 68 61 72 20  t nString, char 
2d8a7 2a 2a 61 7a 53 74 72 69 6e 67 29 7b 0a 20 20 69  **azString){.  i
2d8a8 6e 74 20 69 3b 0a 20 20 66 6f 72 28 69 3d 30 3b  nt i;.  for(i=0;
2d8a9 20 69 3c 6e 53 74 72 69 6e 67 3b 20 2b 2b 69 29   i<nString; ++i)
2d8aa 7b 0a 20 20 20 20 69 66 28 20 69 3e 30 20 29 20  {.    if( i>0 ) 
2d8ab 61 70 70 65 6e 64 28 73 62 2c 20 22 2c 20 22 29  append(sb, ", ")
2d8ac 3b 0a 20 20 20 20 61 70 70 65 6e 64 28 73 62 2c  ;.    append(sb,
2d8ad 20 61 7a 53 74 72 69 6e 67 5b 69 5d 29 3b 0a 20   azString[i]);. 
2d8ae 20 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74   }.}..static int
2d8af 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70 61 63   endsInWhiteSpac
2d8b0 65 28 53 74 72 69 6e 67 42 75 66 66 65 72 20 2a  e(StringBuffer *
2d8b1 70 29 7b 0a 20 20 72 65 74 75 72 6e 20 73 74 72  p){.  return str
2d8b2 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28  ingBufferLength(
2d8b3 70 29 3e 30 20 26 26 0a 20 20 20 20 73 61 66 65  p)>0 &&.    safe
2d8b4 5f 69 73 73 70 61 63 65 28 73 74 72 69 6e 67 42  _isspace(stringB
2d8b5 75 66 66 65 72 44 61 74 61 28 70 29 5b 73 74 72  ufferData(p)[str
2d8b6 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28  ingBufferLength(
2d8b7 70 29 2d 31 5d 29 3b 0a 7d 0a 0a 2f 2a 20 49 66  p)-1]);.}../* If
2d8b8 20 74 68 65 20 53 74 72 69 6e 67 42 75 66 66 65   the StringBuffe
2d8b9 72 20 65 6e 64 73 20 69 6e 20 73 6f 6d 65 74 68  r ends in someth
2d8ba 69 6e 67 20 6f 74 68 65 72 20 74 68 61 6e 20 77  ing other than w
2d8bb 68 69 74 65 20 73 70 61 63 65 2c 20 61 64 64 20  hite space, add 
2d8bc 61 0a 2a 2a 20 73 69 6e 67 6c 65 20 73 70 61 63  a.** single spac
2d8bd 65 20 63 68 61 72 61 63 74 65 72 20 74 6f 20 74  e character to t
2d8be 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  he end..*/.stati
2d8bf 63 20 76 6f 69 64 20 61 70 70 65 6e 64 57 68 69  c void appendWhi
2d8c0 74 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75  teSpace(StringBu
2d8c1 66 66 65 72 20 2a 70 29 7b 0a 20 20 69 66 28 20  ffer *p){.  if( 
2d8c2 73 74 72 69 6e 67 42 75 66 66 65 72 4c 65 6e 67  stringBufferLeng
2d8c3 74 68 28 70 29 3d 3d 30 20 29 20 72 65 74 75 72  th(p)==0 ) retur
2d8c4 6e 3b 0a 20 20 69 66 28 20 21 65 6e 64 73 49 6e  n;.  if( !endsIn
2d8c5 57 68 69 74 65 53 70 61 63 65 28 70 29 20 29 20  WhiteSpace(p) ) 
2d8c6 61 70 70 65 6e 64 28 70 2c 20 22 20 22 29 3b 0a  append(p, " ");.
2d8c7 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20 77 68 69  }../* Remove whi
2d8c8 74 65 20 73 70 61 63 65 20 66 72 6f 6d 20 74 68  te space from th
2d8c9 65 20 65 6e 64 20 6f 66 20 74 68 65 20 53 74 72  e end of the Str
2d8ca 69 6e 67 42 75 66 66 65 72 20 2a 2f 0a 73 74 61  ingBuffer */.sta
2d8cb 74 69 63 20 76 6f 69 64 20 74 72 69 6d 57 68 69  tic void trimWhi
2d8cc 74 65 53 70 61 63 65 28 53 74 72 69 6e 67 42 75  teSpace(StringBu
2d8cd 66 66 65 72 20 2a 70 29 7b 0a 20 20 77 68 69 6c  ffer *p){.  whil
2d8ce 65 28 20 65 6e 64 73 49 6e 57 68 69 74 65 53 70  e( endsInWhiteSp
2d8cf 61 63 65 28 70 29 20 29 7b 0a 20 20 20 20 70 2d  ace(p) ){.    p-
2d8d0 3e 62 2e 70 44 61 74 61 5b 2d 2d 70 2d 3e 62 2e  >b.pData[--p->b.
2d8d1 6e 44 61 74 61 2d 31 5d 20 3d 20 27 5c 30 27 3b  nData-1] = '\0';
2d8d2 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  .  }.}../*******
2d8d3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d8d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d8d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2d8d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
2d8d7 20 44 4c 52 65 61 64 65 72 20 69 73 20 75 73 65   DLReader is use
2d8d8 64 20 74 6f 20 72 65 61 64 20 64 6f 63 75 6d 65  d to read docume
2d8d9 6e 74 20 65 6c 65 6d 65 6e 74 73 20 66 72 6f 6d  nt elements from
2d8da 20 61 20 64 6f 63 6c 69 73 74 2e 20 20 54 68 65   a doclist.  The
2d8db 0a 2a 2a 20 63 75 72 72 65 6e 74 20 64 6f 63 69  .** current doci
2d8dc 64 20 69 73 20 63 61 63 68 65 64 2c 20 73 6f 20  d is cached, so 
2d8dd 64 6c 72 44 6f 63 69 64 28 29 20 69 73 20 66 61  dlrDocid() is fa
2d8de 73 74 2e 20 20 44 4c 52 65 61 64 65 72 20 64 6f  st.  DLReader do
2d8df 65 73 20 6e 6f 74 0a 2a 2a 20 6f 77 6e 20 74 68  es not.** own th
2d8e0 65 20 64 6f 63 6c 69 73 74 20 62 75 66 66 65 72  e doclist buffer
2d8e1 2e 0a 2a 2a 0a 2a 2a 20 64 6c 72 41 74 45 6e 64  ..**.** dlrAtEnd
2d8e2 20 2d 20 74 72 75 65 20 69 66 20 74 68 65 72 65   - true if there
2d8e3 27 73 20 6e 6f 20 6d 6f 72 65 20 64 61 74 61 20  's no more data 
2d8e4 74 6f 20 72 65 61 64 2e 0a 2a 2a 20 64 6c 72 44  to read..** dlrD
2d8e5 6f 63 69 64 20 2d 20 64 6f 63 69 64 20 6f 66 20  ocid - docid of 
2d8e6 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74  current document
2d8e7 2e 0a 2a 2a 20 64 6c 72 44 6f 63 44 61 74 61 20  ..** dlrDocData 
2d8e8 2d 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66  - doclist data f
2d8e9 6f 72 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  or current docum
2d8ea 65 6e 74 20 28 69 6e 63 6c 75 64 69 6e 67 20 64  ent (including d
2d8eb 6f 63 69 64 29 2e 0a 2a 2a 20 64 6c 72 44 6f 63  ocid)..** dlrDoc
2d8ec 44 61 74 61 42 79 74 65 73 20 2d 20 6c 65 6e 67  DataBytes - leng
2d8ed 74 68 20 6f 66 20 73 61 6d 65 2e 0a 2a 2a 20 64  th of same..** d
2d8ee 6c 72 41 6c 6c 44 61 74 61 42 79 74 65 73 20 2d  lrAllDataBytes -
2d8ef 20 6c 65 6e 67 74 68 20 6f 66 20 61 6c 6c 20 72   length of all r
2d8f0 65 6d 61 69 6e 69 6e 67 20 64 61 74 61 2e 0a 2a  emaining data..*
2d8f1 2a 20 64 6c 72 50 6f 73 44 61 74 61 20 2d 20 70  * dlrPosData - p
2d8f2 6f 73 69 74 69 6f 6e 20 64 61 74 61 20 66 6f 72  osition data for
2d8f3 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d 65 6e   current documen
2d8f4 74 2e 0a 2a 2a 20 64 6c 72 50 6f 73 44 61 74 61  t..** dlrPosData
2d8f5 4c 65 6e 20 2d 20 6c 65 6e 67 74 68 20 6f 66 20  Len - length of 
2d8f6 70 6f 73 20 64 61 74 61 20 66 6f 72 20 63 75 72  pos data for cur
2d8f7 72 65 6e 74 20 64 6f 63 75 6d 65 6e 74 20 28 69  rent document (i
2d8f8 6e 63 6c 20 50 4f 53 5f 45 4e 44 29 2e 0a 2a 2a  ncl POS_END)..**
2d8f9 20 64 6c 72 53 74 65 70 20 2d 20 73 74 65 70 20   dlrStep - step 
2d8fa 74 6f 20 63 75 72 72 65 6e 74 20 64 6f 63 75 6d  to current docum
2d8fb 65 6e 74 2e 0a 2a 2a 20 64 6c 72 49 6e 69 74 20  ent..** dlrInit 
2d8fc 2d 20 69 6e 69 74 69 61 6c 20 66 6f 72 20 64 6f  - initial for do
2d8fd 63 6c 69 73 74 20 6f 66 20 67 69 76 65 6e 20 74  clist of given t
2d8fe 79 70 65 20 61 67 61 69 6e 73 74 20 67 69 76 65  ype against give
2d8ff 6e 20 64 61 74 61 2e 0a 2a 2a 20 64 6c 72 44 65  n data..** dlrDe
2d900 73 74 72 6f 79 20 2d 20 63 6c 65 61 6e 20 75 70  stroy - clean up
2d901 2e 0a 2a 2a 0a 2a 2a 20 45 78 70 65 63 74 65 64  ..**.** Expected
2d902 20 75 73 61 67 65 20 69 73 20 73 6f 6d 65 74 68   usage is someth
2d903 69 6e 67 20 6c 69 6b 65 3a 0a 2a 2a 0a 2a 2a 20  ing like:.**.** 
2d904 20 20 44 4c 52 65 61 64 65 72 20 72 65 61 64 65    DLReader reade
2d905 72 3b 0a 2a 2a 20 20 20 64 6c 72 49 6e 69 74 28  r;.**   dlrInit(
2d906 26 72 65 61 64 65 72 2c 20 70 44 61 74 61 2c 20  &reader, pData, 
2d907 6e 44 61 74 61 29 3b 0a 2a 2a 20 20 20 77 68 69  nData);.**   whi
2d908 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 72  le( !dlrAtEnd(&r
2d909 65 61 64 65 72 29 20 29 7b 0a 2a 2a 20 20 20 20  eader) ){.**    
2d90a 20 2f 2f 20 63 61 6c 6c 73 20 74 6f 20 64 6c 72   // calls to dlr
2d90b 44 6f 63 69 64 28 29 20 61 6e 64 20 6b 69 6e 2e  Docid() and kin.
2d90c 0a 2a 2a 20 20 20 20 20 64 6c 72 53 74 65 70 28  .**     dlrStep(
2d90d 26 72 65 61 64 65 72 29 3b 0a 2a 2a 20 20 20 7d  &reader);.**   }
2d90e 0a 2a 2a 20 20 20 64 6c 72 44 65 73 74 72 6f 79  .**   dlrDestroy
2d90f 28 26 72 65 61 64 65 72 29 3b 0a 2a 2f 0a 74 79  (&reader);.*/.ty
2d910 70 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 52  pedef struct DLR
2d911 65 61 64 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73  eader {.  DocLis
2d912 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 63  tType iType;.  c
2d913 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61  onst char *pData
2d914 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a  ;.  int nData;..
2d915 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
2d916 44 6f 63 69 64 3b 0a 20 20 69 6e 74 20 6e 45 6c  Docid;.  int nEl
2d917 65 6d 65 6e 74 3b 0a 7d 20 44 4c 52 65 61 64 65  ement;.} DLReade
2d918 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64  r;..static int d
2d919 6c 72 41 74 45 6e 64 28 44 4c 52 65 61 64 65 72  lrAtEnd(DLReader
2d91a 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
2d91b 73 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e  sert( pReader->n
2d91c 44 61 74 61 3e 3d 30 20 29 3b 0a 20 20 72 65 74  Data>=0 );.  ret
2d91d 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61  urn pReader->nDa
2d91e 74 61 3d 3d 30 3b 0a 7d 0a 73 74 61 74 69 63 20  ta==0;.}.static 
2d91f 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 64 6c 72  sqlite_int64 dlr
2d920 44 6f 63 69 64 28 44 4c 52 65 61 64 65 72 20 2a  Docid(DLReader *
2d921 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2d922 72 74 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52  rt( !dlrAtEnd(pR
2d923 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
2d924 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63  rn pReader->iDoc
2d925 69 64 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  id;.}.static con
2d926 73 74 20 63 68 61 72 20 2a 64 6c 72 44 6f 63 44  st char *dlrDocD
2d927 61 74 61 28 44 4c 52 65 61 64 65 72 20 2a 70 52  ata(DLReader *pR
2d928 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
2d929 28 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61  ( !dlrAtEnd(pRea
2d92a 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
2d92b 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 3b   pReader->pData;
2d92c 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c  .}.static int dl
2d92d 72 44 6f 63 44 61 74 61 42 79 74 65 73 28 44 4c  rDocDataBytes(DL
2d92e 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2d92f 7b 0a 20 20 61 73 73 65 72 74 28 20 21 64 6c 72  {.  assert( !dlr
2d930 41 74 45 6e 64 28 70 52 65 61 64 65 72 29 20 29  AtEnd(pReader) )
2d931 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65 61 64  ;.  return pRead
2d932 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b 0a 7d 0a  er->nElement;.}.
2d933 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72 41 6c  static int dlrAl
2d934 6c 44 61 74 61 42 79 74 65 73 28 44 4c 52 65 61  lDataBytes(DLRea
2d935 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2d936 20 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45   assert( !dlrAtE
2d937 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
2d938 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
2d939 3e 6e 44 61 74 61 3b 0a 7d 0a 2f 2a 20 54 4f 44  >nData;.}./* TOD
2d93a 4f 28 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65  O(shess) Conside
2d93b 72 20 61 64 64 69 6e 67 20 61 20 66 69 65 6c 64  r adding a field
2d93c 20 74 6f 20 74 72 61 63 6b 20 69 44 6f 63 69 64   to track iDocid
2d93d 20 76 61 72 69 6e 74 20 6c 65 6e 67 74 68 0a 2a   varint length.*
2d93e 2a 20 74 6f 20 6d 61 6b 65 20 74 68 65 73 65 20  * to make these 
2d93f 74 77 6f 20 66 75 6e 63 74 69 6f 6e 73 20 66 61  two functions fa
2d940 73 74 65 72 2e 20 20 54 68 69 73 20 6d 69 67 68  ster.  This migh
2d941 74 20 6d 61 74 74 65 72 20 28 61 20 74 69 6e 79  t matter (a tiny
2d942 20 62 69 74 29 0a 2a 2a 20 66 6f 72 20 71 75 65   bit).** for que
2d943 72 69 65 73 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ries..*/.static 
2d944 63 6f 6e 73 74 20 63 68 61 72 20 2a 64 6c 72 50  const char *dlrP
2d945 6f 73 44 61 74 61 28 44 4c 52 65 61 64 65 72 20  osData(DLReader 
2d946 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 73 71 6c  *pReader){.  sql
2d947 69 74 65 5f 69 6e 74 36 34 20 69 44 75 6d 6d 79  ite_int64 iDummy
2d948 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66 74 73 33  ;.  int n = fts3
2d949 47 65 74 56 61 72 69 6e 74 28 70 52 65 61 64 65  GetVarint(pReade
2d94a 72 2d 3e 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  r->pData, &iDumm
2d94b 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 21 64  y);.  assert( !d
2d94c 6c 72 41 74 45 6e 64 28 70 52 65 61 64 65 72 29  lrAtEnd(pReader)
2d94d 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 52 65   );.  return pRe
2d94e 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 3b 0a 7d  ader->pData+n;.}
2d94f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6c 72 50  .static int dlrP
2d950 6f 73 44 61 74 61 4c 65 6e 28 44 4c 52 65 61 64  osDataLen(DLRead
2d951 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2d952 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 75  sqlite_int64 iDu
2d953 6d 6d 79 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 66  mmy;.  int n = f
2d954 74 73 33 47 65 74 56 61 72 69 6e 74 28 70 52 65  ts3GetVarint(pRe
2d955 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 44  ader->pData, &iD
2d956 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65 72 74 28  ummy);.  assert(
2d957 20 21 64 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !dlrAtEnd(pRead
2d958 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
2d959 70 52 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e  pReader->nElemen
2d95a 74 2d 6e 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  t-n;.}.static vo
2d95b 69 64 20 64 6c 72 53 74 65 70 28 44 4c 52 65 61  id dlrStep(DLRea
2d95c 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2d95d 20 61 73 73 65 72 74 28 20 21 64 6c 72 41 74 45   assert( !dlrAtE
2d95e 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a  nd(pReader) );..
2d95f 20 20 2f 2a 20 53 6b 69 70 20 70 61 73 74 20 63    /* Skip past c
2d960 75 72 72 65 6e 74 20 64 6f 63 6c 69 73 74 20 65  urrent doclist e
2d961 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20 20 61 73 73  lement. */.  ass
2d962 65 72 74 28 20 70 52 65 61 64 65 72 2d 3e 6e 45  ert( pReader->nE
2d963 6c 65 6d 65 6e 74 3c 3d 70 52 65 61 64 65 72 2d  lement<=pReader-
2d964 3e 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61  >nData );.  pRea
2d965 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 70 52  der->pData += pR
2d966 65 61 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3b  eader->nElement;
2d967 0a 20 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  .  pReader->nDat
2d968 61 20 2d 3d 20 70 52 65 61 64 65 72 2d 3e 6e 45  a -= pReader->nE
2d969 6c 65 6d 65 6e 74 3b 0a 0a 20 20 2f 2a 20 49 66  lement;..  /* If
2d96a 20 74 68 65 72 65 20 69 73 20 6d 6f 72 65 20 64   there is more d
2d96b 61 74 61 2c 20 72 65 61 64 20 74 68 65 20 6e 65  ata, read the ne
2d96c 78 74 20 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65  xt doclist eleme
2d96d 6e 74 2e 20 2a 2f 0a 20 20 69 66 28 20 70 52 65  nt. */.  if( pRe
2d96e 61 64 65 72 2d 3e 6e 44 61 74 61 21 3d 30 20 29  ader->nData!=0 )
2d96f 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74  {.    sqlite_int
2d970 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a  64 iDocidDelta;.
2d971 20 20 20 20 69 6e 74 20 69 44 75 6d 6d 79 2c 20      int iDummy, 
2d972 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
2d973 74 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  t(pReader->pData
2d974 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b  , &iDocidDelta);
2d975 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 69 44  .    pReader->iD
2d976 6f 63 69 64 20 2b 3d 20 69 44 6f 63 69 64 44 65  ocid += iDocidDe
2d977 6c 74 61 3b 0a 20 20 20 20 69 66 28 20 70 52 65  lta;.    if( pRe
2d978 61 64 65 72 2d 3e 69 54 79 70 65 3e 3d 44 4c 5f  ader->iType>=DL_
2d979 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a 20 20 20  POSITIONS ){.   
2d97a 20 20 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65     assert( n<pRe
2d97b 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
2d97c 20 20 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b       while( 1 ){
2d97d 0a 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74  .        n += ft
2d97e 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
2d97f 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20  eader->pData+n, 
2d980 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20  &iDummy);.      
2d981 20 20 61 73 73 65 72 74 28 20 6e 3c 3d 70 52 65    assert( n<=pRe
2d982 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20  ader->nData );. 
2d983 20 20 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d         if( iDumm
2d984 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65  y==POS_END ) bre
2d985 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ak;.        if( 
2d986 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55  iDummy==POS_COLU
2d987 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  MN ){.          
2d988 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
2d989 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44  nt32(pReader->pD
2d98a 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b  ata+n, &iDummy);
2d98b 0a 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72  .          asser
2d98c 74 28 20 6e 3c 70 52 65 61 64 65 72 2d 3e 6e 44  t( n<pReader->nD
2d98d 61 74 61 20 29 3b 0a 20 20 20 20 20 20 20 20 7d  ata );.        }
2d98e 65 6c 73 65 20 69 66 28 20 70 52 65 61 64 65 72  else if( pReader
2d98f 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49  ->iType==DL_POSI
2d990 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b  TIONS_OFFSETS ){
2d991 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20  .          n += 
2d992 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
2d993 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e  pReader->pData+n
2d994 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20  , &iDummy);.    
2d995 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47        n += fts3G
2d996 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
2d997 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69 44  er->pData+n, &iD
2d998 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20 20  ummy);.         
2d999 20 61 73 73 65 72 74 28 20 6e 3c 70 52 65 61 64   assert( n<pRead
2d99a 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20  er->nData );.   
2d99b 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20       }.      }. 
2d99c 20 20 20 7d 0a 20 20 20 20 70 52 65 61 64 65 72     }.    pReader
2d99d 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 6e 3b 0a  ->nElement = n;.
2d99e 20 20 20 20 61 73 73 65 72 74 28 20 70 52 65 61      assert( pRea
2d99f 64 65 72 2d 3e 6e 45 6c 65 6d 65 6e 74 3c 3d 70  der->nElement<=p
2d9a0 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b  Reader->nData );
2d9a1 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20 76 6f  .  }.}.static vo
2d9a2 69 64 20 64 6c 72 49 6e 69 74 28 44 4c 52 65 61  id dlrInit(DLRea
2d9a3 64 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44 6f  der *pReader, Do
2d9a4 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c  cListType iType,
2d9a5 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2d9a6 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
2d9a7 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
2d9a8 61 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 44  a){.  assert( pD
2d9a9 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44 61  ata!=NULL && nDa
2d9aa 74 61 21 3d 30 20 29 3b 0a 20 20 70 52 65 61 64  ta!=0 );.  pRead
2d9ab 65 72 2d 3e 69 54 79 70 65 20 3d 20 69 54 79 70  er->iType = iTyp
2d9ac 65 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44  e;.  pReader->pD
2d9ad 61 74 61 20 3d 20 70 44 61 74 61 3b 0a 20 20 70  ata = pData;.  p
2d9ae 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Reader->nData = 
2d9af 6e 44 61 74 61 3b 0a 20 20 70 52 65 61 64 65 72  nData;.  pReader
2d9b0 2d 3e 6e 45 6c 65 6d 65 6e 74 20 3d 20 30 3b 0a  ->nElement = 0;.
2d9b1 20 20 70 52 65 61 64 65 72 2d 3e 69 44 6f 63 69    pReader->iDoci
2d9b2 64 20 3d 20 30 3b 0a 0a 20 20 2f 2a 20 4c 6f 61  d = 0;..  /* Loa
2d9b3 64 20 74 68 65 20 66 69 72 73 74 20 65 6c 65 6d  d the first elem
2d9b4 65 6e 74 27 73 20 64 61 74 61 2e 20 20 54 68 65  ent's data.  The
2d9b5 72 65 20 6d 75 73 74 20 62 65 20 61 20 66 69 72  re must be a fir
2d9b6 73 74 20 65 6c 65 6d 65 6e 74 2e 20 2a 2f 0a 20  st element. */. 
2d9b7 20 64 6c 72 53 74 65 70 28 70 52 65 61 64 65 72   dlrStep(pReader
2d9b8 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64  );.}.static void
2d9b9 20 64 6c 72 44 65 73 74 72 6f 79 28 44 4c 52 65   dlrDestroy(DLRe
2d9ba 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2d9bb 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64    SCRAMBLE(pRead
2d9bc 65 72 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  er);.}..#ifndef 
2d9bd 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
2d9be 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c 69 73   that the doclis
2d9bf 74 20 63 61 6e 20 62 65 20 76 61 6c 69 64 6c 79  t can be validly
2d9c0 20 64 65 63 6f 64 65 64 2e 20 20 41 6c 73 6f 20   decoded.  Also 
2d9c1 72 65 74 75 72 6e 73 20 74 68 65 0a 2a 2a 20 6c  returns the.** l
2d9c2 61 73 74 20 64 6f 63 69 64 20 66 6f 75 6e 64 20  ast docid found 
2d9c3 62 65 63 61 75 73 65 20 69 74 20 69 73 20 63 6f  because it is co
2d9c4 6e 76 65 6e 69 65 6e 74 20 69 6e 20 6f 74 68 65  nvenient in othe
2d9c5 72 20 61 73 73 65 72 74 69 6f 6e 73 20 66 6f 72  r assertions for
2d9c6 0a 2a 2a 20 44 4c 57 72 69 74 65 72 2e 0a 2a 2f  .** DLWriter..*/
2d9c7 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
2d9c8 4c 69 73 74 56 61 6c 69 64 61 74 65 28 44 6f 63  ListValidate(Doc
2d9c9 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20  ListType iType, 
2d9ca 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
2d9cb 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
2d9cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2d9cd 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2d9ce 5f 69 6e 74 36 34 20 2a 70 4c 61 73 74 44 6f 63  _int64 *pLastDoc
2d9cf 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e  id){.  sqlite_in
2d9d0 74 36 34 20 69 50 72 65 76 44 6f 63 69 64 20 3d  t64 iPrevDocid =
2d9d1 20 30 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 44   0;.  assert( nD
2d9d2 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ata>0 );.  asser
2d9d3 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20  t( pData!=0 );. 
2d9d4 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e   assert( pData+n
2d9d5 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 20 20  Data>pData );.  
2d9d6 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30 20  while( nData!=0 
2d9d7 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e  ){.    sqlite_in
2d9d8 74 36 34 20 69 44 6f 63 69 64 44 65 6c 74 61 3b  t64 iDocidDelta;
2d9d9 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66 74 73  .    int n = fts
2d9da 33 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61  3GetVarint(pData
2d9db 2c 20 26 69 44 6f 63 69 64 44 65 6c 74 61 29 3b  , &iDocidDelta);
2d9dc 0a 20 20 20 20 69 50 72 65 76 44 6f 63 69 64 20  .    iPrevDocid 
2d9dd 2b 3d 20 69 44 6f 63 69 64 44 65 6c 74 61 3b 0a  += iDocidDelta;.
2d9de 20 20 20 20 69 66 28 20 69 54 79 70 65 3e 44 4c      if( iType>DL
2d9df 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 20  _DOCIDS ){.     
2d9e0 20 69 6e 74 20 69 44 75 6d 6d 79 3b 0a 20 20 20   int iDummy;.   
2d9e1 20 20 20 77 68 69 6c 65 28 20 31 20 29 7b 0a 20     while( 1 ){. 
2d9e2 20 20 20 20 20 20 20 6e 20 2b 3d 20 66 74 73 33         n += fts3
2d9e3 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74  GetVarint32(pDat
2d9e4 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20  a+n, &iDummy);. 
2d9e5 20 20 20 20 20 20 20 69 66 28 20 69 44 75 6d 6d         if( iDumm
2d9e6 79 3d 3d 50 4f 53 5f 45 4e 44 20 29 20 62 72 65  y==POS_END ) bre
2d9e7 61 6b 3b 0a 20 20 20 20 20 20 20 20 69 66 28 20  ak;.        if( 
2d9e8 69 44 75 6d 6d 79 3d 3d 50 4f 53 5f 43 4f 4c 55  iDummy==POS_COLU
2d9e9 4d 4e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  MN ){.          
2d9ea 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61 72 69  n += fts3GetVari
2d9eb 6e 74 33 32 28 70 44 61 74 61 2b 6e 2c 20 26 69  nt32(pData+n, &i
2d9ec 44 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 20 20  Dummy);.        
2d9ed 7d 65 6c 73 65 20 69 66 28 20 69 54 79 70 65 3e  }else if( iType>
2d9ee 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 29 7b 0a  DL_POSITIONS ){.
2d9ef 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d 20 66            n += f
2d9f0 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2d9f1 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d 79 29  Data+n, &iDummy)
2d9f2 3b 0a 20 20 20 20 20 20 20 20 20 20 6e 20 2b 3d  ;.          n +=
2d9f3 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32   fts3GetVarint32
2d9f4 28 70 44 61 74 61 2b 6e 2c 20 26 69 44 75 6d 6d  (pData+n, &iDumm
2d9f5 79 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  y);.        }.  
2d9f6 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3c        assert( n<
2d9f7 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20 20  =nData );.      
2d9f8 7d 0a 20 20 20 20 7d 0a 20 20 20 20 61 73 73 65  }.    }.    asse
2d9f9 72 74 28 20 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a  rt( n<=nData );.
2d9fa 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a      pData += n;.
2d9fb 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a      nData -= n;.
2d9fc 20 20 7d 0a 20 20 69 66 28 20 70 4c 61 73 74 44    }.  if( pLastD
2d9fd 6f 63 69 64 20 29 20 2a 70 4c 61 73 74 44 6f 63  ocid ) *pLastDoc
2d9fe 69 64 20 3d 20 69 50 72 65 76 44 6f 63 69 64 3b  id = iPrevDocid;
2d9ff 0a 7d 0a 23 64 65 66 69 6e 65 20 41 53 53 45 52  .}.#define ASSER
2da00 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28  T_VALID_DOCLIST(
2da01 69 2c 20 70 2c 20 6e 2c 20 6f 29 20 64 6f 63 4c  i, p, n, o) docL
2da02 69 73 74 56 61 6c 69 64 61 74 65 28 69 2c 20 70  istValidate(i, p
2da03 2c 20 6e 2c 20 6f 29 0a 23 65 6c 73 65 0a 23 64  , n, o).#else.#d
2da04 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41 4c  efine ASSERT_VAL
2da05 49 44 5f 44 4f 43 4c 49 53 54 28 69 2c 20 70 2c  ID_DOCLIST(i, p,
2da06 20 6e 2c 20 6f 29 20 61 73 73 65 72 74 28 20 31   n, o) assert( 1
2da07 20 29 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a   ).#endif../****
2da08 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da09 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2da0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f  ***************/
2da0c 0a 2f 2a 20 44 4c 57 72 69 74 65 72 20 69 73 20  ./* DLWriter is 
2da0d 75 73 65 64 20 74 6f 20 77 72 69 74 65 20 64 6f  used to write do
2da0e 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20 61 20  clist data to a 
2da0f 44 61 74 61 42 75 66 66 65 72 2e 20 20 44 4c 57  DataBuffer.  DLW
2da10 72 69 74 65 72 0a 2a 2a 20 61 6c 77 61 79 73 20  riter.** always 
2da11 61 70 70 65 6e 64 73 20 74 6f 20 74 68 65 20 62  appends to the b
2da12 75 66 66 65 72 20 61 6e 64 20 64 6f 65 73 20 6e  uffer and does n
2da13 6f 74 20 6f 77 6e 20 69 74 2e 0a 2a 2a 0a 2a 2a  ot own it..**.**
2da14 20 64 6c 77 49 6e 69 74 20 2d 20 69 6e 69 74 69   dlwInit - initi
2da15 61 6c 69 7a 65 20 74 6f 20 77 72 69 74 65 20 61  alize to write a
2da16 20 67 69 76 65 6e 20 74 79 70 65 20 64 6f 63 6c   given type docl
2da17 69 73 74 74 6f 20 61 20 62 75 66 66 65 72 2e 0a  istto a buffer..
2da18 2a 2a 20 64 6c 77 44 65 73 74 72 6f 79 20 2d 20  ** dlwDestroy - 
2da19 63 6c 65 61 72 20 74 68 65 20 77 72 69 74 65 72  clear the writer
2da1a 27 73 20 6d 65 6d 6f 72 79 2e 20 20 44 6f 65 73  's memory.  Does
2da1b 20 6e 6f 74 20 66 72 65 65 20 62 75 66 66 65 72   not free buffer
2da1c 2e 0a 2a 2a 20 64 6c 77 41 70 70 65 6e 64 20 2d  ..** dlwAppend -
2da1d 20 61 70 70 65 6e 64 20 72 61 77 20 64 6f 63 6c   append raw docl
2da1e 69 73 74 20 64 61 74 61 20 74 6f 20 62 75 66 66  ist data to buff
2da1f 65 72 2e 0a 2a 2a 20 64 6c 77 43 6f 70 79 20 2d  er..** dlwCopy -
2da20 20 63 6f 70 79 20 6e 65 78 74 20 64 6f 63 6c 69   copy next docli
2da21 73 74 20 66 72 6f 6d 20 72 65 61 64 65 72 20 74  st from reader t
2da22 6f 20 77 72 69 74 65 72 2e 0a 2a 2a 20 64 6c 77  o writer..** dlw
2da23 41 64 64 20 2d 20 63 6f 6e 73 74 72 75 63 74 20  Add - construct 
2da24 64 6f 63 6c 69 73 74 20 65 6c 65 6d 65 6e 74 20  doclist element 
2da25 61 6e 64 20 61 70 70 65 6e 64 20 74 6f 20 62 75  and append to bu
2da26 66 66 65 72 2e 0a 2a 2a 20 20 20 20 4f 6e 6c 79  ffer..**    Only
2da27 20 61 70 70 6c 79 20 64 6c 77 41 64 64 28 29 20   apply dlwAdd() 
2da28 74 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63  to DL_DOCIDS doc
2da29 6c 69 73 74 73 20 28 65 6c 73 65 20 75 73 65 20  lists (else use 
2da2a 50 4c 57 72 69 74 65 72 29 2e 0a 2a 2f 0a 74 79  PLWriter)..*/.ty
2da2b 70 65 64 65 66 20 73 74 72 75 63 74 20 44 4c 57  pedef struct DLW
2da2c 72 69 74 65 72 20 7b 0a 20 20 44 6f 63 4c 69 73  riter {.  DocLis
2da2d 74 54 79 70 65 20 69 54 79 70 65 3b 0a 20 20 44  tType iType;.  D
2da2e 61 74 61 42 75 66 66 65 72 20 2a 62 3b 0a 20 20  ataBuffer *b;.  
2da2f 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 50 72  sqlite_int64 iPr
2da30 65 76 44 6f 63 69 64 3b 0a 23 69 66 6e 64 65 66  evDocid;.#ifndef
2da31 20 4e 44 45 42 55 47 0a 20 20 69 6e 74 20 68 61   NDEBUG.  int ha
2da32 73 5f 69 50 72 65 76 44 6f 63 69 64 3b 0a 23 65  s_iPrevDocid;.#e
2da33 6e 64 69 66 0a 7d 20 44 4c 57 72 69 74 65 72 3b  ndif.} DLWriter;
2da34 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  ..static void dl
2da35 77 49 6e 69 74 28 44 4c 57 72 69 74 65 72 20 2a  wInit(DLWriter *
2da36 70 57 72 69 74 65 72 2c 20 44 6f 63 4c 69 73 74  pWriter, DocList
2da37 54 79 70 65 20 69 54 79 70 65 2c 20 44 61 74 61  Type iType, Data
2da38 42 75 66 66 65 72 20 2a 62 29 7b 0a 20 20 70 57  Buffer *b){.  pW
2da39 72 69 74 65 72 2d 3e 62 20 3d 20 62 3b 0a 20 20  riter->b = b;.  
2da3a 70 57 72 69 74 65 72 2d 3e 69 54 79 70 65 20 3d  pWriter->iType =
2da3b 20 69 54 79 70 65 3b 0a 20 20 70 57 72 69 74 65   iType;.  pWrite
2da3c 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20  r->iPrevDocid = 
2da3d 30 3b 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55  0;.#ifndef NDEBU
2da3e 47 0a 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73  G.  pWriter->has
2da3f 5f 69 50 72 65 76 44 6f 63 69 64 20 3d 20 30 3b  _iPrevDocid = 0;
2da40 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74 69 63  .#endif.}.static
2da41 20 76 6f 69 64 20 64 6c 77 44 65 73 74 72 6f 79   void dlwDestroy
2da42 28 44 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74  (DLWriter *pWrit
2da43 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c 45 28  er){.  SCRAMBLE(
2da44 70 57 72 69 74 65 72 29 3b 0a 7d 0a 2f 2a 20 69  pWriter);.}./* i
2da45 46 69 72 73 74 44 6f 63 69 64 20 69 73 20 74 68  FirstDocid is th
2da46 65 20 66 69 72 73 74 20 64 6f 63 69 64 20 69 6e  e first docid in
2da47 20 74 68 65 20 64 6f 63 6c 69 73 74 20 69 6e 20   the doclist in 
2da48 70 44 61 74 61 2e 20 20 49 74 20 69 73 0a 2a 2a  pData.  It is.**
2da49 20 6e 65 65 64 65 64 20 62 65 63 61 75 73 65 20   needed because 
2da4a 70 44 61 74 61 20 6d 61 79 20 70 6f 69 6e 74 20  pData may point 
2da4b 77 69 74 68 69 6e 20 61 20 6c 61 72 67 65 72 20  within a larger 
2da4c 64 6f 63 6c 69 73 74 2c 20 69 6e 20 77 68 69 63  doclist, in whic
2da4d 68 0a 2a 2a 20 63 61 73 65 20 74 68 65 20 66 69  h.** case the fi
2da4e 72 73 74 20 69 74 65 6d 20 77 6f 75 6c 64 20 62  rst item would b
2da4f 65 20 64 65 6c 74 61 2d 65 6e 63 6f 64 65 64 2e  e delta-encoded.
2da50 0a 2a 2a 0a 2a 2a 20 69 4c 61 73 74 44 6f 63 69  .**.** iLastDoci
2da51 64 20 69 73 20 74 68 65 20 66 69 6e 61 6c 20 64  d is the final d
2da52 6f 63 69 64 20 69 6e 20 74 68 65 20 64 6f 63 6c  ocid in the docl
2da53 69 73 74 20 69 6e 20 70 44 61 74 61 2e 20 20 49  ist in pData.  I
2da54 74 20 69 73 0a 2a 2a 20 6e 65 65 64 65 64 20 74  t is.** needed t
2da55 6f 20 63 72 65 61 74 65 20 74 68 65 20 6e 65 77  o create the new
2da56 20 69 50 72 65 76 44 6f 63 69 64 20 66 6f 72 20   iPrevDocid for 
2da57 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e 63  future delta-enc
2da58 6f 64 69 6e 67 2e 20 20 54 68 65 0a 2a 2a 20 63  oding.  The.** c
2da59 6f 64 65 20 63 6f 75 6c 64 20 64 65 63 6f 64 65  ode could decode
2da5a 20 74 68 65 20 70 61 73 73 65 64 20 64 6f 63 6c   the passed docl
2da5b 69 73 74 20 74 6f 20 72 65 63 72 65 61 74 65 20  ist to recreate 
2da5c 69 4c 61 73 74 44 6f 63 69 64 2c 20 62 75 74 0a  iLastDocid, but.
2da5d 2a 2a 20 74 68 65 20 6f 6e 6c 79 20 63 75 72 72  ** the only curr
2da5e 65 6e 74 20 75 73 65 72 20 28 64 6f 63 4c 69 73  ent user (docLis
2da5f 74 4d 65 72 67 65 29 20 61 6c 72 65 61 64 79 20  tMerge) already 
2da60 68 61 73 20 64 65 63 6f 64 65 64 20 74 68 69 73  has decoded this
2da61 0a 2a 2a 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e  .** information.
2da62 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
2da63 73 29 20 54 68 69 73 20 68 61 73 20 62 65 63 6f  s) This has beco
2da64 6d 65 20 6a 75 73 74 20 61 20 68 65 6c 70 65 72  me just a helper
2da65 20 66 6f 72 20 64 6f 63 4c 69 73 74 4d 65 72 67   for docListMerg
2da66 65 2e 0a 2a 2a 20 43 6f 6e 73 69 64 65 72 20 61  e..** Consider a
2da67 20 72 65 66 61 63 74 6f 72 20 74 6f 20 6d 61 6b   refactor to mak
2da68 65 20 74 68 69 73 20 63 6c 65 61 6e 65 72 2e 0a  e this cleaner..
2da69 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2da6a 6c 77 41 70 70 65 6e 64 28 44 4c 57 72 69 74 65  lwAppend(DLWrite
2da6b 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
2da6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2da6d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
2da6e 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a  ata, int nData,.
2da6f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2da70 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74        sqlite_int
2da71 36 34 20 69 46 69 72 73 74 44 6f 63 69 64 2c 20  64 iFirstDocid, 
2da72 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c 61  sqlite_int64 iLa
2da73 73 74 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69  stDocid){.  sqli
2da74 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 20  te_int64 iDocid 
2da75 3d 20 30 3b 0a 20 20 63 68 61 72 20 63 5b 56 41  = 0;.  char c[VA
2da76 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74  RINT_MAX];.  int
2da77 20 6e 46 69 72 73 74 4f 6c 64 2c 20 6e 46 69 72   nFirstOld, nFir
2da78 73 74 4e 65 77 3b 20 20 20 20 20 2f 2a 20 4f 6c  stNew;     /* Ol
2da79 64 20 61 6e 64 20 6e 65 77 20 76 61 72 69 6e 74  d and new varint
2da7a 20 6c 65 6e 20 6f 66 20 66 69 72 73 74 20 64 6f   len of first do
2da7b 63 69 64 2e 20 2a 2f 0a 23 69 66 6e 64 65 66 20  cid. */.#ifndef 
2da7c 4e 44 45 42 55 47 0a 20 20 73 71 6c 69 74 65 5f  NDEBUG.  sqlite_
2da7d 69 6e 74 36 34 20 69 4c 61 73 74 44 6f 63 69 64  int64 iLastDocid
2da7e 44 65 6c 74 61 3b 0a 23 65 6e 64 69 66 0a 0a 20  Delta;.#endif.. 
2da7f 20 2f 2a 20 52 65 63 6f 64 65 20 74 68 65 20 69   /* Recode the i
2da80 6e 69 74 69 61 6c 20 64 6f 63 69 64 20 61 73 20  nitial docid as 
2da81 64 65 6c 74 61 20 66 72 6f 6d 20 69 50 72 65 76  delta from iPrev
2da82 44 6f 63 69 64 2e 20 2a 2f 0a 20 20 6e 46 69 72  Docid. */.  nFir
2da83 73 74 4f 6c 64 20 3d 20 66 74 73 33 47 65 74 56  stOld = fts3GetV
2da84 61 72 69 6e 74 28 70 44 61 74 61 2c 20 26 69 44  arint(pData, &iD
2da85 6f 63 69 64 29 3b 0a 20 20 61 73 73 65 72 74 28  ocid);.  assert(
2da86 20 6e 46 69 72 73 74 4f 6c 64 3c 6e 44 61 74 61   nFirstOld<nData
2da87 20 7c 7c 20 28 6e 46 69 72 73 74 4f 6c 64 3d 3d   || (nFirstOld==
2da88 6e 44 61 74 61 20 26 26 20 70 57 72 69 74 65 72  nData && pWriter
2da89 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49  ->iType==DL_DOCI
2da8a 44 53 29 20 29 3b 0a 20 20 6e 46 69 72 73 74 4e  DS) );.  nFirstN
2da8b 65 77 20 3d 20 66 74 73 33 50 75 74 56 61 72 69  ew = fts3PutVari
2da8c 6e 74 28 63 2c 20 69 46 69 72 73 74 44 6f 63 69  nt(c, iFirstDoci
2da8d 64 2d 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76  d-pWriter->iPrev
2da8e 44 6f 63 69 64 29 3b 0a 0a 20 20 2f 2a 20 56 65  Docid);..  /* Ve
2da8f 72 69 66 79 20 74 68 61 74 20 74 68 65 20 69 6e  rify that the in
2da90 63 6f 6d 69 6e 67 20 64 6f 63 6c 69 73 74 20 69  coming doclist i
2da91 73 20 76 61 6c 69 64 20 41 4e 44 20 74 68 61 74  s valid AND that
2da92 20 69 74 20 65 6e 64 73 20 77 69 74 68 0a 20 20   it ends with.  
2da93 2a 2a 20 74 68 65 20 65 78 70 65 63 74 65 64 20  ** the expected 
2da94 64 6f 63 69 64 2e 20 20 54 68 69 73 20 69 73 20  docid.  This is 
2da95 65 73 73 65 6e 74 69 61 6c 20 62 65 63 61 75 73  essential becaus
2da96 65 20 77 65 27 6c 6c 20 74 72 75 73 74 20 74 68  e we'll trust th
2da97 69 73 0a 20 20 2a 2a 20 64 6f 63 69 64 20 69 6e  is.  ** docid in
2da98 20 66 75 74 75 72 65 20 64 65 6c 74 61 2d 65 6e   future delta-en
2da99 63 6f 64 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 41  coding..  */.  A
2da9a 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
2da9b 49 53 54 28 70 57 72 69 74 65 72 2d 3e 69 54 79  IST(pWriter->iTy
2da9c 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61  pe, pData, nData
2da9d 2c 20 26 69 4c 61 73 74 44 6f 63 69 64 44 65 6c  , &iLastDocidDel
2da9e 74 61 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  ta);.  assert( i
2da9f 4c 61 73 74 44 6f 63 69 64 3d 3d 69 46 69 72 73  LastDocid==iFirs
2daa0 74 44 6f 63 69 64 2d 69 44 6f 63 69 64 2b 69 4c  tDocid-iDocid+iL
2daa1 61 73 74 44 6f 63 69 64 44 65 6c 74 61 20 29 3b  astDocidDelta );
2daa2 0a 0a 20 20 2f 2a 20 41 70 70 65 6e 64 20 72 65  ..  /* Append re
2daa3 63 6f 64 65 64 20 69 6e 69 74 69 61 6c 20 64 6f  coded initial do
2daa4 63 69 64 20 61 6e 64 20 65 76 65 72 79 74 68 69  cid and everythi
2daa5 6e 67 20 65 6c 73 65 2e 20 20 52 65 73 74 20 6f  ng else.  Rest o
2daa6 66 20 64 6f 63 69 64 73 0a 20 20 2a 2a 20 73 68  f docids.  ** sh
2daa7 6f 75 6c 64 20 68 61 76 65 20 62 65 65 6e 20 64  ould have been d
2daa8 65 6c 74 61 2d 65 6e 63 6f 64 65 64 20 66 72 6f  elta-encoded fro
2daa9 6d 20 70 72 65 76 69 6f 75 73 20 69 6e 69 74 69  m previous initi
2daaa 61 6c 20 64 6f 63 69 64 2e 0a 20 20 2a 2f 0a 20  al docid..  */. 
2daab 20 69 66 28 20 6e 46 69 72 73 74 4f 6c 64 3c 6e   if( nFirstOld<n
2daac 44 61 74 61 20 29 7b 0a 20 20 20 20 64 61 74 61  Data ){.    data
2daad 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 70 57  BufferAppend2(pW
2daae 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46 69  riter->b, c, nFi
2daaf 72 73 74 4e 65 77 2c 0a 20 20 20 20 20 20 20 20  rstNew,.        
2dab0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 44                pD
2dab1 61 74 61 2b 6e 46 69 72 73 74 4f 6c 64 2c 20 6e  ata+nFirstOld, n
2dab2 44 61 74 61 2d 6e 46 69 72 73 74 4f 6c 64 29 3b  Data-nFirstOld);
2dab3 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 64 61  .  }else{.    da
2dab4 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
2dab5 57 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 46  Writer->b, c, nF
2dab6 69 72 73 74 4e 65 77 29 3b 0a 20 20 7d 0a 20 20  irstNew);.  }.  
2dab7 70 57 72 69 74 65 72 2d 3e 69 50 72 65 76 44 6f  pWriter->iPrevDo
2dab8 63 69 64 20 3d 20 69 4c 61 73 74 44 6f 63 69 64  cid = iLastDocid
2dab9 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69 64 20  ;.}.static void 
2daba 64 6c 77 43 6f 70 79 28 44 4c 57 72 69 74 65 72  dlwCopy(DLWriter
2dabb 20 2a 70 57 72 69 74 65 72 2c 20 44 4c 52 65 61   *pWriter, DLRea
2dabc 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2dabd 20 64 6c 77 41 70 70 65 6e 64 28 70 57 72 69 74   dlwAppend(pWrit
2dabe 65 72 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70  er, dlrDocData(p
2dabf 52 65 61 64 65 72 29 2c 20 64 6c 72 44 6f 63 44  Reader), dlrDocD
2dac0 61 74 61 42 79 74 65 73 28 70 52 65 61 64 65 72  ataBytes(pReader
2dac1 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 64  ),.            d
2dac2 6c 72 44 6f 63 69 64 28 70 52 65 61 64 65 72 29  lrDocid(pReader)
2dac3 2c 20 64 6c 72 44 6f 63 69 64 28 70 52 65 61 64  , dlrDocid(pRead
2dac4 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  er));.}.static v
2dac5 6f 69 64 20 64 6c 77 41 64 64 28 44 4c 57 72 69  oid dlwAdd(DLWri
2dac6 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 73 71  ter *pWriter, sq
2dac7 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
2dac8 64 29 7b 0a 20 20 63 68 61 72 20 63 5b 56 41 52  d){.  char c[VAR
2dac9 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74 20  INT_MAX];.  int 
2daca 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
2dacb 74 28 63 2c 20 69 44 6f 63 69 64 2d 70 57 72 69  t(c, iDocid-pWri
2dacc 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 29  ter->iPrevDocid)
2dacd 3b 0a 0a 20 20 2f 2a 20 44 6f 63 69 64 73 20 6d  ;..  /* Docids m
2dace 75 73 74 20 61 73 63 65 6e 64 2e 20 2a 2f 0a 20  ust ascend. */. 
2dacf 20 61 73 73 65 72 74 28 20 21 70 57 72 69 74 65   assert( !pWrite
2dad0 72 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69  r->has_iPrevDoci
2dad1 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69  d || iDocid>pWri
2dad2 74 65 72 2d 3e 69 50 72 65 76 44 6f 63 69 64 20  ter->iPrevDocid 
2dad3 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72  );.  assert( pWr
2dad4 69 74 65 72 2d 3e 69 54 79 70 65 3d 3d 44 4c 5f  iter->iType==DL_
2dad5 44 4f 43 49 44 53 20 29 3b 0a 0a 20 20 64 61 74  DOCIDS );..  dat
2dad6 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57  aBufferAppend(pW
2dad7 72 69 74 65 72 2d 3e 62 2c 20 63 2c 20 6e 29 3b  riter->b, c, n);
2dad8 0a 20 20 70 57 72 69 74 65 72 2d 3e 69 50 72 65  .  pWriter->iPre
2dad9 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b  vDocid = iDocid;
2dada 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2dadb 20 20 70 57 72 69 74 65 72 2d 3e 68 61 73 5f 69    pWriter->has_i
2dadc 50 72 65 76 44 6f 63 69 64 20 3d 20 31 3b 0a 23  PrevDocid = 1;.#
2dadd 65 6e 64 69 66 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a  endif.}../******
2dade 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dadf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dae0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dae1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f  *************/./
2dae2 2a 20 50 4c 52 65 61 64 65 72 20 69 73 20 75 73  * PLReader is us
2dae3 65 64 20 74 6f 20 72 65 61 64 20 64 61 74 61 20  ed to read data 
2dae4 66 72 6f 6d 20 61 20 64 6f 63 75 6d 65 6e 74 27  from a document'
2dae5 73 20 70 6f 73 69 74 69 6f 6e 20 6c 69 73 74 2e  s position list.
2dae6 20 20 41 73 0a 2a 2a 20 74 68 65 20 63 61 6c 6c    As.** the call
2dae7 65 72 20 73 74 65 70 73 20 74 68 72 6f 75 67 68  er steps through
2dae8 20 74 68 65 20 6c 69 73 74 2c 20 64 61 74 61 20   the list, data 
2dae9 69 73 20 63 61 63 68 65 64 20 73 6f 20 74 68 61  is cached so tha
2daea 74 20 76 61 72 69 6e 74 73 0a 2a 2a 20 6f 6e 6c  t varints.** onl
2daeb 79 20 6e 65 65 64 20 74 6f 20 62 65 20 64 65 63  y need to be dec
2daec 6f 64 65 64 20 6f 6e 63 65 2e 0a 2a 2a 0a 2a 2a  oded once..**.**
2daed 20 70 6c 72 49 6e 69 74 2c 20 70 6c 72 44 65 73   plrInit, plrDes
2daee 74 72 6f 79 20 2d 20 63 72 65 61 74 65 2f 64 65  troy - create/de
2daef 73 74 72 6f 79 20 61 20 72 65 61 64 65 72 2e 0a  stroy a reader..
2daf0 2a 2a 20 70 6c 72 43 6f 6c 75 6d 6e 2c 20 70 6c  ** plrColumn, pl
2daf1 72 50 6f 73 69 74 69 6f 6e 2c 20 70 6c 72 53 74  rPosition, plrSt
2daf2 61 72 74 4f 66 66 73 65 74 2c 20 70 6c 72 45 6e  artOffset, plrEn
2daf3 64 4f 66 66 73 65 74 20 2d 20 61 63 63 65 73 73  dOffset - access
2daf4 6f 72 73 0a 2a 2a 20 70 6c 72 41 74 45 6e 64 20  ors.** plrAtEnd 
2daf5 2d 20 61 74 20 65 6e 64 20 6f 66 20 73 74 72 65  - at end of stre
2daf6 61 6d 2c 20 6f 6e 6c 79 20 63 61 6c 6c 20 70 6c  am, only call pl
2daf7 72 44 65 73 74 72 6f 79 20 6f 6e 63 65 20 74 72  rDestroy once tr
2daf8 75 65 2e 0a 2a 2a 20 70 6c 72 53 74 65 70 20 2d  ue..** plrStep -
2daf9 20 73 74 65 70 20 74 6f 20 74 68 65 20 6e 65 78   step to the nex
2dafa 74 20 65 6c 65 6d 65 6e 74 2e 0a 2a 2f 0a 74 79  t element..*/.ty
2dafb 70 65 64 65 66 20 73 74 72 75 63 74 20 50 4c 52  pedef struct PLR
2dafc 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20 54 68 65  eader {.  /* The
2dafd 73 65 20 72 65 66 65 72 20 74 6f 20 74 68 65 20  se refer to the 
2dafe 6e 65 78 74 20 70 6f 73 69 74 69 6f 6e 27 73 20  next position's 
2daff 64 61 74 61 2e 20 20 6e 44 61 74 61 20 77 69 6c  data.  nData wil
2db00 6c 20 72 65 61 63 68 20 30 20 77 68 65 6e 0a 20  l reach 0 when. 
2db01 20 2a 2a 20 72 65 61 64 69 6e 67 20 74 68 65 20   ** reading the 
2db02 6c 61 73 74 20 70 6f 73 69 74 69 6f 6e 2c 20 73  last position, s
2db03 6f 20 70 6c 72 53 74 65 70 28 29 20 73 69 67 6e  o plrStep() sign
2db04 61 6c 73 20 45 4f 46 20 62 79 20 73 65 74 74 69  als EOF by setti
2db05 6e 67 0a 20 20 2a 2a 20 70 44 61 74 61 20 74 6f  ng.  ** pData to
2db06 20 4e 55 4c 4c 2e 0a 20 20 2a 2f 0a 20 20 63 6f   NULL..  */.  co
2db07 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b  nst char *pData;
2db08 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20  .  int nData;.. 
2db09 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54 79   DocListType iTy
2db0a 70 65 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d  pe;.  int iColum
2db0b 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 74 68  n;         /* th
2db0c 65 20 6c 61 73 74 20 63 6f 6c 75 6d 6e 20 72 65  e last column re
2db0d 61 64 20 2a 2f 0a 20 20 69 6e 74 20 69 50 6f 73  ad */.  int iPos
2db0e 69 74 69 6f 6e 3b 20 20 20 20 20 20 20 2f 2a 20  ition;       /* 
2db0f 74 68 65 20 6c 61 73 74 20 70 6f 73 69 74 69 6f  the last positio
2db10 6e 20 72 65 61 64 20 2a 2f 0a 20 20 69 6e 74 20  n read */.  int 
2db11 69 53 74 61 72 74 4f 66 66 73 65 74 3b 20 20 20  iStartOffset;   
2db12 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73 74 61   /* the last sta
2db13 72 74 20 6f 66 66 73 65 74 20 72 65 61 64 20 2a  rt offset read *
2db14 2f 0a 20 20 69 6e 74 20 69 45 6e 64 4f 66 66 73  /.  int iEndOffs
2db15 65 74 3b 20 20 20 20 20 20 2f 2a 20 74 68 65 20  et;      /* the 
2db16 6c 61 73 74 20 65 6e 64 20 6f 66 66 73 65 74 20  last end offset 
2db17 72 65 61 64 20 2a 2f 0a 7d 20 50 4c 52 65 61 64  read */.} PLRead
2db18 65 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  er;..static int 
2db19 70 6c 72 41 74 45 6e 64 28 50 4c 52 65 61 64 65  plrAtEnd(PLReade
2db1a 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72  r *pReader){.  r
2db1b 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 70  eturn pReader->p
2db1c 44 61 74 61 3d 3d 4e 55 4c 4c 3b 0a 7d 0a 73 74  Data==NULL;.}.st
2db1d 61 74 69 63 20 69 6e 74 20 70 6c 72 43 6f 6c 75  atic int plrColu
2db1e 6d 6e 28 50 4c 52 65 61 64 65 72 20 2a 70 52 65  mn(PLReader *pRe
2db1f 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28  ader){.  assert(
2db20 20 21 70 6c 72 41 74 45 6e 64 28 70 52 65 61 64   !plrAtEnd(pRead
2db21 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  er) );.  return 
2db22 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d 6e  pReader->iColumn
2db23 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 70  ;.}.static int p
2db24 6c 72 50 6f 73 69 74 69 6f 6e 28 50 4c 52 65 61  lrPosition(PLRea
2db25 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2db26 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45   assert( !plrAtE
2db27 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20  nd(pReader) );. 
2db28 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
2db29 3e 69 50 6f 73 69 74 69 6f 6e 3b 0a 7d 0a 73 74  >iPosition;.}.st
2db2a 61 74 69 63 20 69 6e 74 20 70 6c 72 53 74 61 72  atic int plrStar
2db2b 74 4f 66 66 73 65 74 28 50 4c 52 65 61 64 65 72  tOffset(PLReader
2db2c 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
2db2d 73 65 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28  sert( !plrAtEnd(
2db2e 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65  pReader) );.  re
2db2f 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 53  turn pReader->iS
2db30 74 61 72 74 4f 66 66 73 65 74 3b 0a 7d 0a 73 74  tartOffset;.}.st
2db31 61 74 69 63 20 69 6e 74 20 70 6c 72 45 6e 64 4f  atic int plrEndO
2db32 66 66 73 65 74 28 50 4c 52 65 61 64 65 72 20 2a  ffset(PLReader *
2db33 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65  pReader){.  asse
2db34 72 74 28 20 21 70 6c 72 41 74 45 6e 64 28 70 52  rt( !plrAtEnd(pR
2db35 65 61 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75  eader) );.  retu
2db36 72 6e 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64  rn pReader->iEnd
2db37 4f 66 66 73 65 74 3b 0a 7d 0a 73 74 61 74 69 63  Offset;.}.static
2db38 20 76 6f 69 64 20 70 6c 72 53 74 65 70 28 50 4c   void plrStep(PL
2db39 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2db3a 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 0a 20  {.  int i, n;.. 
2db3b 20 61 73 73 65 72 74 28 20 21 70 6c 72 41 74 45   assert( !plrAtE
2db3c 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a  nd(pReader) );..
2db3d 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e 6e    if( pReader->n
2db3e 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 70  Data==0 ){.    p
2db3f 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 3d 20  Reader->pData = 
2db40 4e 55 4c 4c 3b 0a 20 20 20 20 72 65 74 75 72 6e  NULL;.    return
2db41 3b 0a 20 20 7d 0a 0a 20 20 6e 20 3d 20 66 74 73  ;.  }..  n = fts
2db42 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65  3GetVarint32(pRe
2db43 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 69 29  ader->pData, &i)
2db44 3b 0a 20 20 69 66 28 20 69 3d 3d 50 4f 53 5f 43  ;.  if( i==POS_C
2db45 4f 4c 55 4d 4e 20 29 7b 0a 20 20 20 20 6e 20 2b  OLUMN ){.    n +
2db46 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74 33  = fts3GetVarint3
2db47 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61  2(pReader->pData
2db48 2b 6e 2c 20 26 70 52 65 61 64 65 72 2d 3e 69 43  +n, &pReader->iC
2db49 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20 70 52 65 61  olumn);.    pRea
2db4a 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d  der->iPosition =
2db4b 20 30 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d   0;.    pReader-
2db4c 3e 69 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20  >iStartOffset = 
2db4d 30 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33  0;.    n += fts3
2db4e 47 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61  GetVarint32(pRea
2db4f 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 69  der->pData+n, &i
2db50 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20 53 68 6f 75  );.  }.  /* Shou
2db51 6c 64 20 6e 65 76 65 72 20 73 65 65 20 61 64 6a  ld never see adj
2db52 61 63 65 6e 74 20 63 6f 6c 75 6d 6e 20 63 68 61  acent column cha
2db53 6e 67 65 73 2e 20 2a 2f 0a 20 20 61 73 73 65 72  nges. */.  asser
2db54 74 28 20 69 21 3d 50 4f 53 5f 43 4f 4c 55 4d 4e  t( i!=POS_COLUMN
2db55 20 29 3b 0a 0a 20 20 69 66 28 20 69 3d 3d 50 4f   );..  if( i==PO
2db56 53 5f 45 4e 44 20 29 7b 0a 20 20 20 20 70 52 65  S_END ){.    pRe
2db57 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20 30 3b  ader->nData = 0;
2db58 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44  .    pReader->pD
2db59 61 74 61 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  ata = NULL;.    
2db5a 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70  return;.  }..  p
2db5b 52 65 61 64 65 72 2d 3e 69 50 6f 73 69 74 69 6f  Reader->iPositio
2db5c 6e 20 2b 3d 20 69 2d 50 4f 53 5f 42 41 53 45 3b  n += i-POS_BASE;
2db5d 0a 20 20 69 66 28 20 70 52 65 61 64 65 72 2d 3e  .  if( pReader->
2db5e 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49  iType==DL_POSITI
2db5f 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20  ONS_OFFSETS ){. 
2db60 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56     n += fts3GetV
2db61 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d  arint32(pReader-
2db62 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20  >pData+n, &i);. 
2db63 20 20 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61     pReader->iSta
2db64 72 74 4f 66 66 73 65 74 20 2b 3d 20 69 3b 0a 20  rtOffset += i;. 
2db65 20 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56     n += fts3GetV
2db66 61 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d  arint32(pReader-
2db67 3e 70 44 61 74 61 2b 6e 2c 20 26 69 29 3b 0a 20  >pData+n, &i);. 
2db68 20 20 20 70 52 65 61 64 65 72 2d 3e 69 45 6e 64     pReader->iEnd
2db69 4f 66 66 73 65 74 20 3d 20 70 52 65 61 64 65 72  Offset = pReader
2db6a 2d 3e 69 53 74 61 72 74 4f 66 66 73 65 74 2b 69  ->iStartOffset+i
2db6b 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20  ;.  }.  assert( 
2db6c 6e 3c 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74  n<=pReader->nDat
2db6d 61 20 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  a );.  pReader->
2db6e 70 44 61 74 61 20 2b 3d 20 6e 3b 0a 20 20 70 52  pData += n;.  pR
2db6f 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20  eader->nData -= 
2db70 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
2db71 64 20 70 6c 72 49 6e 69 74 28 50 4c 52 65 61 64  d plrInit(PLRead
2db72 65 72 20 2a 70 52 65 61 64 65 72 2c 20 44 4c 52  er *pReader, DLR
2db73 65 61 64 65 72 20 2a 70 44 4c 52 65 61 64 65 72  eader *pDLReader
2db74 29 7b 0a 20 20 70 52 65 61 64 65 72 2d 3e 70 44  ){.  pReader->pD
2db75 61 74 61 20 3d 20 64 6c 72 50 6f 73 44 61 74 61  ata = dlrPosData
2db76 28 70 44 4c 52 65 61 64 65 72 29 3b 0a 20 20 70  (pDLReader);.  p
2db77 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d 20  Reader->nData = 
2db78 64 6c 72 50 6f 73 44 61 74 61 4c 65 6e 28 70 44  dlrPosDataLen(pD
2db79 4c 52 65 61 64 65 72 29 3b 0a 20 20 70 52 65 61  LReader);.  pRea
2db7a 64 65 72 2d 3e 69 54 79 70 65 20 3d 20 70 44 4c  der->iType = pDL
2db7b 52 65 61 64 65 72 2d 3e 69 54 79 70 65 3b 0a 20  Reader->iType;. 
2db7c 20 70 52 65 61 64 65 72 2d 3e 69 43 6f 6c 75 6d   pReader->iColum
2db7d 6e 20 3d 20 30 3b 0a 20 20 70 52 65 61 64 65 72  n = 0;.  pReader
2db7e 2d 3e 69 50 6f 73 69 74 69 6f 6e 20 3d 20 30 3b  ->iPosition = 0;
2db7f 0a 20 20 70 52 65 61 64 65 72 2d 3e 69 53 74 61  .  pReader->iSta
2db80 72 74 4f 66 66 73 65 74 20 3d 20 30 3b 0a 20 20  rtOffset = 0;.  
2db81 70 52 65 61 64 65 72 2d 3e 69 45 6e 64 4f 66 66  pReader->iEndOff
2db82 73 65 74 20 3d 20 30 3b 0a 20 20 70 6c 72 53 74  set = 0;.  plrSt
2db83 65 70 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 73  ep(pReader);.}.s
2db84 74 61 74 69 63 20 76 6f 69 64 20 70 6c 72 44 65  tatic void plrDe
2db85 73 74 72 6f 79 28 50 4c 52 65 61 64 65 72 20 2a  stroy(PLReader *
2db86 70 52 65 61 64 65 72 29 7b 0a 20 20 53 43 52 41  pReader){.  SCRA
2db87 4d 42 4c 45 28 70 52 65 61 64 65 72 29 3b 0a 7d  MBLE(pReader);.}
2db88 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
2db89 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2db8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2db8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2db8c 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 50 4c 57 72 69  ******/./* PLWri
2db8d 74 65 72 20 69 73 20 75 73 65 64 20 69 6e 20 63  ter is used in c
2db8e 6f 6e 73 74 72 75 63 74 69 6e 67 20 61 20 64 6f  onstructing a do
2db8f 63 75 6d 65 6e 74 27 73 20 70 6f 73 69 74 69 6f  cument's positio
2db90 6e 20 6c 69 73 74 2e 20 20 41 73 20 61 0a 2a 2a  n list.  As a.**
2db91 20 63 6f 6e 76 65 6e 69 65 6e 63 65 2c 20 69 66   convenience, if
2db92 20 69 54 79 70 65 20 69 73 20 44 4c 5f 44 4f 43   iType is DL_DOC
2db93 49 44 53 2c 20 50 4c 57 72 69 74 65 72 20 62 65  IDS, PLWriter be
2db94 63 6f 6d 65 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  comes a no-op..*
2db95 2a 20 50 4c 57 72 69 74 65 72 20 77 72 69 74 65  * PLWriter write
2db96 73 20 74 6f 20 74 68 65 20 61 73 73 6f 63 69 61  s to the associa
2db97 74 65 64 20 44 4c 57 72 69 74 65 72 27 73 20 62  ted DLWriter's b
2db98 75 66 66 65 72 2e 0a 2a 2a 0a 2a 2a 20 70 6c 77  uffer..**.** plw
2db99 49 6e 69 74 20 2d 20 69 6e 69 74 20 66 6f 72 20  Init - init for 
2db9a 77 72 69 74 69 6e 67 20 61 20 64 6f 63 75 6d 65  writing a docume
2db9b 6e 74 27 73 20 70 6f 73 6c 69 73 74 2e 0a 2a 2a  nt's poslist..**
2db9c 20 70 6c 77 44 65 73 74 72 6f 79 20 2d 20 63 6c   plwDestroy - cl
2db9d 65 61 72 20 61 20 77 72 69 74 65 72 2e 0a 2a 2a  ear a writer..**
2db9e 20 70 6c 77 41 64 64 20 2d 20 61 70 70 65 6e 64   plwAdd - append
2db9f 20 70 6f 73 69 74 69 6f 6e 20 61 6e 64 20 6f 66   position and of
2dba0 66 73 65 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  fset information
2dba1 2e 0a 2a 2a 20 70 6c 77 43 6f 70 79 20 2d 20 63  ..** plwCopy - c
2dba2 6f 70 79 20 6e 65 78 74 20 70 6f 73 69 74 69 6f  opy next positio
2dba3 6e 27 73 20 64 61 74 61 20 66 72 6f 6d 20 72 65  n's data from re
2dba4 61 64 65 72 20 74 6f 20 77 72 69 74 65 72 2e 0a  ader to writer..
2dba5 2a 2a 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 20  ** plwTerminate 
2dba6 2d 20 61 64 64 20 61 6e 79 20 6e 65 63 65 73 73  - add any necess
2dba7 61 72 79 20 64 6f 63 6c 69 73 74 20 74 65 72 6d  ary doclist term
2dba8 69 6e 61 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 43 61  inator..**.** Ca
2dba9 6c 6c 69 6e 67 20 70 6c 77 41 64 64 28 29 20 61  lling plwAdd() a
2dbaa 66 74 65 72 20 70 6c 77 54 65 72 6d 69 6e 61 74  fter plwTerminat
2dbab 65 28 29 20 6d 61 79 20 72 65 73 75 6c 74 20 69  e() may result i
2dbac 6e 20 61 20 63 6f 72 72 75 70 74 0a 2a 2a 20 64  n a corrupt.** d
2dbad 6f 63 6c 69 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f  oclist..*/./* TO
2dbae 44 4f 28 73 68 65 73 73 29 20 55 6e 74 69 6c 20  DO(shess) Until 
2dbaf 77 65 27 76 65 20 77 72 69 74 74 65 6e 20 74 68  we've written th
2dbb0 65 20 73 65 63 6f 6e 64 20 69 74 65 6d 2c 20 77  e second item, w
2dbb1 65 20 63 61 6e 20 63 61 63 68 65 20 74 68 65 0a  e can cache the.
2dbb2 2a 2a 20 66 69 72 73 74 20 69 74 65 6d 27 73 20  ** first item's 
2dbb3 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 20 54 68  information.  Th
2dbb4 65 6e 20 77 65 27 64 20 68 61 76 65 20 74 68 72  en we'd have thr
2dbb5 65 65 20 73 74 61 74 65 73 3a 0a 2a 2a 0a 2a 2a  ee states:.**.**
2dbb6 20 2d 20 69 6e 69 74 69 61 6c 69 7a 65 64 20 77   - initialized w
2dbb7 69 74 68 20 64 6f 63 69 64 2c 20 6e 6f 20 70 6f  ith docid, no po
2dbb8 73 69 74 69 6f 6e 73 2e 0a 2a 2a 20 2d 20 64 6f  sitions..** - do
2dbb9 63 69 64 20 61 6e 64 20 6f 6e 65 20 70 6f 73 69  cid and one posi
2dbba 74 69 6f 6e 2e 0a 2a 2a 20 2d 20 64 6f 63 69 64  tion..** - docid
2dbbb 20 61 6e 64 20 6d 75 6c 74 69 70 6c 65 20 70 6f   and multiple po
2dbbc 73 69 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 4f  sitions..**.** O
2dbbd 6e 6c 79 20 74 68 65 20 6c 61 73 74 20 73 74 61  nly the last sta
2dbbe 74 65 20 6e 65 65 64 73 20 74 6f 20 61 63 74 75  te needs to actu
2dbbf 61 6c 6c 79 20 77 72 69 74 65 20 74 6f 20 64 6c  ally write to dl
2dbc0 77 2d 3e 62 2c 20 77 68 69 63 68 20 77 6f 75 6c  w->b, which woul
2dbc1 64 0a 2a 2a 20 62 65 20 61 6e 20 69 6d 70 72 6f  d.** be an impro
2dbc2 76 65 6d 65 6e 74 20 69 6e 20 74 68 65 20 44 4c  vement in the DL
2dbc3 43 6f 6c 6c 65 63 74 6f 72 20 63 61 73 65 2e 0a  Collector case..
2dbc4 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
2dbc5 74 20 50 4c 57 72 69 74 65 72 20 7b 0a 20 20 44  t PLWriter {.  D
2dbc6 4c 57 72 69 74 65 72 20 2a 64 6c 77 3b 0a 0a 20  LWriter *dlw;.. 
2dbc7 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20 20 20   int iColumn;   
2dbc8 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 63 6f 6c   /* the last col
2dbc9 75 6d 6e 20 77 72 69 74 74 65 6e 20 2a 2f 0a 20  umn written */. 
2dbca 20 69 6e 74 20 69 50 6f 73 3b 20 20 20 20 20 20   int iPos;      
2dbcb 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 70 6f 73   /* the last pos
2dbcc 69 74 69 6f 6e 20 77 72 69 74 74 65 6e 20 2a 2f  ition written */
2dbcd 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
2dbce 20 20 20 2f 2a 20 74 68 65 20 6c 61 73 74 20 73     /* the last s
2dbcf 74 61 72 74 20 6f 66 66 73 65 74 20 77 72 69 74  tart offset writ
2dbd0 74 65 6e 20 2a 2f 0a 7d 20 50 4c 57 72 69 74 65  ten */.} PLWrite
2dbd1 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  r;../* TODO(shes
2dbd2 73 29 20 49 6e 20 74 68 65 20 63 61 73 65 20 77  s) In the case w
2dbd3 68 65 72 65 20 74 68 65 20 70 61 72 65 6e 74 20  here the parent 
2dbd4 69 73 20 72 65 61 64 69 6e 67 20 74 68 65 73 65  is reading these
2dbd5 20 76 61 6c 75 65 73 0a 2a 2a 20 66 72 6f 6d 20   values.** from 
2dbd6 61 20 50 4c 52 65 61 64 65 72 2c 20 77 65 20 63  a PLReader, we c
2dbd7 6f 75 6c 64 20 6f 70 74 69 6d 69 7a 65 20 74 6f  ould optimize to
2dbd8 20 61 20 63 6f 70 79 20 69 66 20 74 68 61 74 20   a copy if that 
2dbd9 50 4c 52 65 61 64 65 72 20 68 61 73 0a 2a 2a 20  PLReader has.** 
2dbda 74 68 65 20 73 61 6d 65 20 74 79 70 65 20 61 73  the same type as
2dbdb 20 70 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61   pWriter..*/.sta
2dbdc 74 69 63 20 76 6f 69 64 20 70 6c 77 41 64 64 28  tic void plwAdd(
2dbdd 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  PLWriter *pWrite
2dbde 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20  r, int iColumn, 
2dbdf 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20 20 20 20  int iPos,.      
2dbe0 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74               int
2dbe1 20 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69   iStartOffset, i
2dbe2 6e 74 20 69 45 6e 64 4f 66 66 73 65 74 29 7b 0a  nt iEndOffset){.
2dbe3 20 20 2f 2a 20 57 6f 72 73 74 2d 63 61 73 65 20    /* Worst-case 
2dbe4 73 70 61 63 65 20 66 6f 72 20 50 4f 53 5f 43 4f  space for POS_CO
2dbe5 4c 55 4d 4e 2c 20 69 43 6f 6c 75 6d 6e 2c 20 69  LUMN, iColumn, i
2dbe6 50 6f 73 44 65 6c 74 61 2c 0a 20 20 2a 2a 20 69  PosDelta,.  ** i
2dbe7 53 74 61 72 74 4f 66 66 73 65 74 44 65 6c 74 61  StartOffsetDelta
2dbe8 2c 20 61 6e 64 20 69 45 6e 64 4f 66 66 73 65 74  , and iEndOffset
2dbe9 44 65 6c 74 61 2e 0a 20 20 2a 2f 0a 20 20 63 68  Delta..  */.  ch
2dbea 61 72 20 63 5b 35 2a 56 41 52 49 4e 54 5f 4d 41  ar c[5*VARINT_MA
2dbeb 58 5d 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b  X];.  int n = 0;
2dbec 0a 0a 20 20 2f 2a 20 42 61 6e 20 70 6c 77 41 64  ..  /* Ban plwAd
2dbed 64 28 29 20 61 66 74 65 72 20 70 6c 77 54 65 72  d() after plwTer
2dbee 6d 69 6e 61 74 65 28 29 2e 20 2a 2f 0a 20 20 61  minate(). */.  a
2dbef 73 73 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e  ssert( pWriter->
2dbf0 69 50 6f 73 21 3d 2d 31 20 29 3b 0a 0a 20 20 69  iPos!=-1 );..  i
2dbf1 66 28 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d  f( pWriter->dlw-
2dbf2 3e 69 54 79 70 65 3d 3d 44 4c 5f 44 4f 43 49 44  >iType==DL_DOCID
2dbf3 53 20 29 20 72 65 74 75 72 6e 3b 0a 0a 20 20 69  S ) return;..  i
2dbf4 66 28 20 69 43 6f 6c 75 6d 6e 21 3d 70 57 72 69  f( iColumn!=pWri
2dbf5 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 29 7b 0a  ter->iColumn ){.
2dbf6 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74      n += fts3Put
2dbf7 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50 4f 53 5f  Varint(c+n, POS_
2dbf8 43 4f 4c 55 4d 4e 29 3b 0a 20 20 20 20 6e 20 2b  COLUMN);.    n +
2dbf9 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
2dbfa 63 2b 6e 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20  c+n, iColumn);. 
2dbfb 20 20 20 70 57 72 69 74 65 72 2d 3e 69 43 6f 6c     pWriter->iCol
2dbfc 75 6d 6e 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20  umn = iColumn;. 
2dbfd 20 20 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73     pWriter->iPos
2dbfe 20 3d 20 30 3b 0a 20 20 20 20 70 57 72 69 74 65   = 0;.    pWrite
2dbff 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b 0a  r->iOffset = 0;.
2dc00 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69 50    }.  assert( iP
2dc01 6f 73 3e 3d 70 57 72 69 74 65 72 2d 3e 69 50 6f  os>=pWriter->iPo
2dc02 73 20 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73 33  s );.  n += fts3
2dc03 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 50  PutVarint(c+n, P
2dc04 4f 53 5f 42 41 53 45 2b 28 69 50 6f 73 2d 70 57  OS_BASE+(iPos-pW
2dc05 72 69 74 65 72 2d 3e 69 50 6f 73 29 29 3b 0a 20  riter->iPos));. 
2dc06 20 70 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d   pWriter->iPos =
2dc07 20 69 50 6f 73 3b 0a 20 20 69 66 28 20 70 57 72   iPos;.  if( pWr
2dc08 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65  iter->dlw->iType
2dc09 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  ==DL_POSITIONS_O
2dc0a 46 46 53 45 54 53 20 29 7b 0a 20 20 20 20 61 73  FFSETS ){.    as
2dc0b 73 65 72 74 28 20 69 53 74 61 72 74 4f 66 66 73  sert( iStartOffs
2dc0c 65 74 3e 3d 70 57 72 69 74 65 72 2d 3e 69 4f 66  et>=pWriter->iOf
2dc0d 66 73 65 74 20 29 3b 0a 20 20 20 20 6e 20 2b 3d  fset );.    n +=
2dc0e 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
2dc0f 2b 6e 2c 20 69 53 74 61 72 74 4f 66 66 73 65 74  +n, iStartOffset
2dc10 2d 70 57 72 69 74 65 72 2d 3e 69 4f 66 66 73 65  -pWriter->iOffse
2dc11 74 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d  t);.    pWriter-
2dc12 3e 69 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72  >iOffset = iStar
2dc13 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 61 73 73  tOffset;.    ass
2dc14 65 72 74 28 20 69 45 6e 64 4f 66 66 73 65 74 3e  ert( iEndOffset>
2dc15 3d 69 53 74 61 72 74 4f 66 66 73 65 74 20 29 3b  =iStartOffset );
2dc16 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75  .    n += fts3Pu
2dc17 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20 69 45 6e  tVarint(c+n, iEn
2dc18 64 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f 66  dOffset-iStartOf
2dc19 66 73 65 74 29 3b 0a 20 20 7d 0a 20 20 64 61 74  fset);.  }.  dat
2dc1a 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70 57  aBufferAppend(pW
2dc1b 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20 63  riter->dlw->b, c
2dc1c 2c 20 6e 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76  , n);.}.static v
2dc1d 6f 69 64 20 70 6c 77 43 6f 70 79 28 50 4c 57 72  oid plwCopy(PLWr
2dc1e 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 20 50  iter *pWriter, P
2dc1f 4c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  LReader *pReader
2dc20 29 7b 0a 20 20 70 6c 77 41 64 64 28 70 57 72 69  ){.  plwAdd(pWri
2dc21 74 65 72 2c 20 70 6c 72 43 6f 6c 75 6d 6e 28 70  ter, plrColumn(p
2dc22 52 65 61 64 65 72 29 2c 20 70 6c 72 50 6f 73 69  Reader), plrPosi
2dc23 74 69 6f 6e 28 70 52 65 61 64 65 72 29 2c 0a 20  tion(pReader),. 
2dc24 20 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74          plrStart
2dc25 4f 66 66 73 65 74 28 70 52 65 61 64 65 72 29 2c  Offset(pReader),
2dc26 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 70 52   plrEndOffset(pR
2dc27 65 61 64 65 72 29 29 3b 0a 7d 0a 73 74 61 74 69  eader));.}.stati
2dc28 63 20 76 6f 69 64 20 70 6c 77 49 6e 69 74 28 50  c void plwInit(P
2dc29 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72  LWriter *pWriter
2dc2a 2c 20 44 4c 57 72 69 74 65 72 20 2a 64 6c 77 2c  , DLWriter *dlw,
2dc2b 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 44   sqlite_int64 iD
2dc2c 6f 63 69 64 29 7b 0a 20 20 63 68 61 72 20 63 5b  ocid){.  char c[
2dc2d 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
2dc2e 6e 74 20 6e 3b 0a 0a 20 20 70 57 72 69 74 65 72  nt n;..  pWriter
2dc2f 2d 3e 64 6c 77 20 3d 20 64 6c 77 3b 0a 0a 20 20  ->dlw = dlw;..  
2dc30 2f 2a 20 44 6f 63 69 64 73 20 6d 75 73 74 20 61  /* Docids must a
2dc31 73 63 65 6e 64 2e 20 2a 2f 0a 20 20 61 73 73 65  scend. */.  asse
2dc32 72 74 28 20 21 70 57 72 69 74 65 72 2d 3e 64 6c  rt( !pWriter->dl
2dc33 77 2d 3e 68 61 73 5f 69 50 72 65 76 44 6f 63 69  w->has_iPrevDoci
2dc34 64 20 7c 7c 20 69 44 6f 63 69 64 3e 70 57 72 69  d || iDocid>pWri
2dc35 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65 76 44  ter->dlw->iPrevD
2dc36 6f 63 69 64 20 29 3b 0a 20 20 6e 20 3d 20 66 74  ocid );.  n = ft
2dc37 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 69  s3PutVarint(c, i
2dc38 44 6f 63 69 64 2d 70 57 72 69 74 65 72 2d 3e 64  Docid-pWriter->d
2dc39 6c 77 2d 3e 69 50 72 65 76 44 6f 63 69 64 29 3b  lw->iPrevDocid);
2dc3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70  .  dataBufferApp
2dc3b 65 6e 64 28 70 57 72 69 74 65 72 2d 3e 64 6c 77  end(pWriter->dlw
2dc3c 2d 3e 62 2c 20 63 2c 20 6e 29 3b 0a 20 20 70 57  ->b, c, n);.  pW
2dc3d 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 69 50 72 65  riter->dlw->iPre
2dc3e 76 44 6f 63 69 64 20 3d 20 69 44 6f 63 69 64 3b  vDocid = iDocid;
2dc3f 0a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a  .#ifndef NDEBUG.
2dc40 20 20 70 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e    pWriter->dlw->
2dc41 68 61 73 5f 69 50 72 65 76 44 6f 63 69 64 20 3d  has_iPrevDocid =
2dc42 20 31 3b 0a 23 65 6e 64 69 66 0a 0a 20 20 70 57   1;.#endif..  pW
2dc43 72 69 74 65 72 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  riter->iColumn =
2dc44 20 30 3b 0a 20 20 70 57 72 69 74 65 72 2d 3e 69   0;.  pWriter->i
2dc45 50 6f 73 20 3d 20 30 3b 0a 20 20 70 57 72 69 74  Pos = 0;.  pWrit
2dc46 65 72 2d 3e 69 4f 66 66 73 65 74 20 3d 20 30 3b  er->iOffset = 0;
2dc47 0a 7d 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  .}./* TODO(shess
2dc48 29 20 53 68 6f 75 6c 64 20 70 6c 77 44 65 73 74  ) Should plwDest
2dc49 72 6f 79 28 29 20 61 6c 73 6f 20 74 65 72 6d 69  roy() also termi
2dc4a 6e 61 74 65 20 74 68 65 20 64 6f 63 6c 69 73 74  nate the doclist
2dc4b 3f 20 20 42 75 74 0a 2a 2a 20 74 68 65 6e 20 70  ?  But.** then p
2dc4c 6c 77 44 65 73 74 72 6f 79 28 29 20 77 6f 75 6c  lwDestroy() woul
2dc4d 64 20 6e 6f 20 6c 6f 6e 67 65 72 20 62 65 20 6a  d no longer be j
2dc4e 75 73 74 20 61 20 64 65 73 74 72 75 63 74 6f 72  ust a destructor
2dc4f 2c 20 69 74 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c  , it would.** al
2dc50 73 6f 20 62 65 20 64 6f 69 6e 67 20 77 6f 72 6b  so be doing work
2dc51 2c 20 77 68 69 63 68 20 69 73 6e 27 74 20 63 6f  , which isn't co
2dc52 6e 73 69 73 74 65 6e 74 20 77 69 74 68 20 74 68  nsistent with th
2dc53 65 20 6f 76 65 72 61 6c 6c 20 69 64 69 6f 6d 2e  e overall idiom.
2dc54 0a 2a 2a 20 41 6e 6f 74 68 65 72 20 6f 70 74 69  .** Another opti
2dc55 6f 6e 20 77 6f 75 6c 64 20 62 65 20 66 6f 72 20  on would be for 
2dc56 70 6c 77 41 64 64 28 29 20 74 6f 20 61 6c 77 61  plwAdd() to alwa
2dc57 79 73 20 61 70 70 65 6e 64 20 61 6e 79 20 6e 65  ys append any ne
2dc58 63 65 73 73 61 72 79 0a 2a 2a 20 74 65 72 6d 69  cessary.** termi
2dc59 6e 61 74 6f 72 2c 20 73 6f 20 74 68 61 74 20 74  nator, so that t
2dc5a 68 65 20 6f 75 74 70 75 74 20 69 73 20 61 6c 77  he output is alw
2dc5b 61 79 73 20 63 6f 72 72 65 63 74 2e 20 20 42 75  ays correct.  Bu
2dc5c 74 20 74 68 61 74 20 77 6f 75 6c 64 0a 2a 2a 20  t that would.** 
2dc5d 61 64 64 20 69 6e 63 72 65 6d 65 6e 74 61 6c 20  add incremental 
2dc5e 77 6f 72 6b 20 74 6f 20 74 68 65 20 63 6f 6d 6d  work to the comm
2dc5f 6f 6e 20 63 61 73 65 20 77 69 74 68 20 74 68 65  on case with the
2dc60 20 6f 6e 6c 79 20 62 65 6e 65 66 69 74 20 62 65   only benefit be
2dc61 69 6e 67 0a 2a 2a 20 41 50 49 20 65 6c 65 67 61  ing.** API elega
2dc62 6e 63 65 2e 20 20 50 75 6e 74 20 66 6f 72 20 6e  nce.  Punt for n
2dc63 6f 77 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ow..*/.static vo
2dc64 69 64 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28  id plwTerminate(
2dc65 50 4c 57 72 69 74 65 72 20 2a 70 57 72 69 74 65  PLWriter *pWrite
2dc66 72 29 7b 0a 20 20 69 66 28 20 70 57 72 69 74 65  r){.  if( pWrite
2dc67 72 2d 3e 64 6c 77 2d 3e 69 54 79 70 65 3e 44 4c  r->dlw->iType>DL
2dc68 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20 20 20 63  _DOCIDS ){.    c
2dc69 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58  har c[VARINT_MAX
2dc6a 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20 3d 20 66  ];.    int n = f
2dc6b 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
2dc6c 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20 20 64 61  POS_END);.    da
2dc6d 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 70  taBufferAppend(p
2dc6e 57 72 69 74 65 72 2d 3e 64 6c 77 2d 3e 62 2c 20  Writer->dlw->b, 
2dc6f 63 2c 20 6e 29 3b 0a 20 20 7d 0a 23 69 66 6e 64  c, n);.  }.#ifnd
2dc70 65 66 20 4e 44 45 42 55 47 0a 20 20 2f 2a 20 4d  ef NDEBUG.  /* M
2dc71 61 72 6b 20 61 73 20 74 65 72 6d 69 6e 61 74 65  ark as terminate
2dc72 64 20 66 6f 72 20 61 73 73 65 72 74 20 69 6e 20  d for assert in 
2dc73 70 6c 77 41 64 64 28 29 2e 20 2a 2f 0a 20 20 70  plwAdd(). */.  p
2dc74 57 72 69 74 65 72 2d 3e 69 50 6f 73 20 3d 20 2d  Writer->iPos = -
2dc75 31 3b 0a 23 65 6e 64 69 66 0a 7d 0a 73 74 61 74  1;.#endif.}.stat
2dc76 69 63 20 76 6f 69 64 20 70 6c 77 44 65 73 74 72  ic void plwDestr
2dc77 6f 79 28 50 4c 57 72 69 74 65 72 20 2a 70 57 72  oy(PLWriter *pWr
2dc78 69 74 65 72 29 7b 0a 20 20 53 43 52 41 4d 42 4c  iter){.  SCRAMBL
2dc79 45 28 70 57 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f  E(pWriter);.}../
2dc7a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc7b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc7c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc7d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2dc7e 2a 2a 2a 2f 0a 2f 2a 20 44 4c 43 6f 6c 6c 65 63  ***/./* DLCollec
2dc7f 74 6f 72 20 77 72 61 70 73 20 50 4c 57 72 69 74  tor wraps PLWrit
2dc80 65 72 20 61 6e 64 20 44 4c 57 72 69 74 65 72 20  er and DLWriter 
2dc81 74 6f 20 70 72 6f 76 69 64 65 20 61 0a 2a 2a 20  to provide a.** 
2dc82 64 79 6e 61 6d 69 63 61 6c 6c 79 2d 61 6c 6c 6f  dynamically-allo
2dc83 63 61 74 65 64 20 64 6f 63 6c 69 73 74 20 61 72  cated doclist ar
2dc84 65 61 20 74 6f 20 75 73 65 20 64 75 72 69 6e 67  ea to use during
2dc85 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e 2e 0a 2a   tokenization..*
2dc86 2a 0a 2a 2a 20 64 6c 63 4e 65 77 20 2d 20 6d 61  *.** dlcNew - ma
2dc87 6c 6c 6f 63 20 75 70 20 61 6e 64 20 69 6e 69 74  lloc up and init
2dc88 69 61 6c 69 7a 65 20 61 20 63 6f 6c 6c 65 63 74  ialize a collect
2dc89 6f 72 2e 0a 2a 2a 20 64 6c 63 44 65 6c 65 74 65  or..** dlcDelete
2dc8a 20 2d 20 64 65 73 74 72 6f 79 20 61 20 63 6f 6c   - destroy a col
2dc8b 6c 65 63 74 6f 72 20 61 6e 64 20 61 6c 6c 20 63  lector and all c
2dc8c 6f 6e 74 61 69 6e 65 64 20 69 74 65 6d 73 2e 0a  ontained items..
2dc8d 2a 2a 20 64 6c 63 41 64 64 50 6f 73 20 2d 20 61  ** dlcAddPos - a
2dc8e 70 70 65 6e 64 20 70 6f 73 69 74 69 6f 6e 20 61  ppend position a
2dc8f 6e 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d  nd offset inform
2dc90 61 74 69 6f 6e 2e 0a 2a 2a 20 64 6c 63 41 64 64  ation..** dlcAdd
2dc91 44 6f 63 6c 69 73 74 20 2d 20 61 64 64 20 74 68  Doclist - add th
2dc92 65 20 63 6f 6c 6c 65 63 74 65 64 20 64 6f 63 6c  e collected docl
2dc93 69 73 74 20 74 6f 20 74 68 65 20 67 69 76 65 6e  ist to the given
2dc94 20 62 75 66 66 65 72 2e 0a 2a 2a 20 64 6c 63 4e   buffer..** dlcN
2dc95 65 78 74 20 2d 20 74 65 72 6d 69 6e 61 74 65 20  ext - terminate 
2dc96 74 68 65 20 63 75 72 72 65 6e 74 20 64 6f 63 75  the current docu
2dc97 6d 65 6e 74 20 61 6e 64 20 6f 70 65 6e 20 61 6e  ment and open an
2dc98 6f 74 68 65 72 2e 0a 2a 2f 0a 74 79 70 65 64 65  other..*/.typede
2dc99 66 20 73 74 72 75 63 74 20 44 4c 43 6f 6c 6c 65  f struct DLColle
2dc9a 63 74 6f 72 20 7b 0a 20 20 44 61 74 61 42 75 66  ctor {.  DataBuf
2dc9b 66 65 72 20 62 3b 0a 20 20 44 4c 57 72 69 74 65  fer b;.  DLWrite
2dc9c 72 20 64 6c 77 3b 0a 20 20 50 4c 57 72 69 74 65  r dlw;.  PLWrite
2dc9d 72 20 70 6c 77 3b 0a 7d 20 44 4c 43 6f 6c 6c 65  r plw;.} DLColle
2dc9e 63 74 6f 72 3b 0a 0a 2f 2a 20 54 4f 44 4f 28 73  ctor;../* TODO(s
2dc9f 68 65 73 73 29 20 54 68 69 73 20 63 6f 75 6c 64  hess) This could
2dca0 20 61 6c 73 6f 20 62 65 20 64 6f 6e 65 20 62 79   also be done by
2dca1 20 63 61 6c 6c 69 6e 67 20 70 6c 77 54 65 72 6d   calling plwTerm
2dca2 69 6e 61 74 65 28 29 20 61 6e 64 0a 2a 2a 20 64  inate() and.** d
2dca3 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
2dca4 29 2e 20 20 49 20 74 72 69 65 64 20 74 68 61 74  ).  I tried that
2dca5 2c 20 65 78 70 65 63 74 69 6e 67 20 6e 6f 6d 69  , expecting nomi
2dca6 6e 61 6c 20 70 65 72 66 6f 72 6d 61 6e 63 65 0a  nal performance.
2dca7 2a 2a 20 64 69 66 66 65 72 65 6e 63 65 73 2c 20  ** differences, 
2dca8 62 75 74 20 69 74 20 73 65 65 6d 65 64 20 74 6f  but it seemed to
2dca9 20 70 72 65 74 74 79 20 72 65 6c 69 61 62 6c 79   pretty reliably
2dcaa 20 62 65 20 77 6f 72 74 68 20 31 25 20 74 6f 20   be worth 1% to 
2dcab 63 6f 64 65 0a 2a 2a 20 69 74 20 74 68 69 73 20  code.** it this 
2dcac 77 61 79 2e 20 20 49 20 73 75 73 70 65 63 74 20  way.  I suspect 
2dcad 69 74 20 69 73 20 74 68 65 20 69 6e 63 72 65 6d  it is the increm
2dcae 65 6e 74 61 6c 20 6d 61 6c 6c 6f 63 20 6f 76 65  ental malloc ove
2dcaf 72 68 65 61 64 20 28 73 6f 6d 65 0a 2a 2a 20 70  rhead (some.** p
2dcb0 65 72 63 65 6e 74 61 67 65 20 6f 66 20 74 68 65  ercentage of the
2dcb1 20 70 6c 77 54 65 72 6d 69 6e 61 74 65 28 29 20   plwTerminate() 
2dcb2 63 61 6c 6c 73 20 77 69 6c 6c 20 63 61 75 73 65  calls will cause
2dcb3 20 61 20 72 65 61 6c 6c 6f 63 29 2c 20 73 6f 0a   a realloc), so.
2dcb4 2a 2a 20 74 68 69 73 20 6d 69 67 68 74 20 62 65  ** this might be
2dcb5 20 77 6f 72 74 68 20 72 65 76 69 73 69 74 69 6e   worth revisitin
2dcb6 67 20 69 66 20 74 68 65 20 44 61 74 61 42 75 66  g if the DataBuf
2dcb7 66 65 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  fer implementati
2dcb8 6f 6e 0a 2a 2a 20 63 68 61 6e 67 65 73 2e 0a 2a  on.** changes..*
2dcb9 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6c  /.static void dl
2dcba 63 41 64 64 44 6f 63 6c 69 73 74 28 44 4c 43 6f  cAddDoclist(DLCo
2dcbb 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65 63  llector *pCollec
2dcbc 74 6f 72 2c 20 44 61 74 61 42 75 66 66 65 72 20  tor, DataBuffer 
2dcbd 2a 62 29 7b 0a 20 20 69 66 28 20 70 43 6f 6c 6c  *b){.  if( pColl
2dcbe 65 63 74 6f 72 2d 3e 64 6c 77 2e 69 54 79 70 65  ector->dlw.iType
2dcbf 3e 44 4c 5f 44 4f 43 49 44 53 20 29 7b 0a 20 20  >DL_DOCIDS ){.  
2dcc0 20 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f    char c[VARINT_
2dcc1 4d 41 58 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 20  MAX];.    int n 
2dcc2 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28  = fts3PutVarint(
2dcc3 63 2c 20 50 4f 53 5f 45 4e 44 29 3b 0a 20 20 20  c, POS_END);.   
2dcc4 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
2dcc5 64 32 28 62 2c 20 70 43 6f 6c 6c 65 63 74 6f 72  d2(b, pCollector
2dcc6 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f 6c 6c  ->b.pData, pColl
2dcc7 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61 2c 20  ector->b.nData, 
2dcc8 63 2c 20 6e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a  c, n);.  }else{.
2dcc9 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
2dcca 70 65 6e 64 28 62 2c 20 70 43 6f 6c 6c 65 63 74  pend(b, pCollect
2dccb 6f 72 2d 3e 62 2e 70 44 61 74 61 2c 20 70 43 6f  or->b.pData, pCo
2dccc 6c 6c 65 63 74 6f 72 2d 3e 62 2e 6e 44 61 74 61  llector->b.nData
2dccd 29 3b 0a 20 20 7d 0a 7d 0a 73 74 61 74 69 63 20  );.  }.}.static 
2dcce 76 6f 69 64 20 64 6c 63 4e 65 78 74 28 44 4c 43  void dlcNext(DLC
2dccf 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65  ollector *pColle
2dcd0 63 74 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74  ctor, sqlite_int
2dcd1 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 70 6c  64 iDocid){.  pl
2dcd2 77 54 65 72 6d 69 6e 61 74 65 28 26 70 43 6f 6c  wTerminate(&pCol
2dcd3 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20  lector->plw);.  
2dcd4 70 6c 77 44 65 73 74 72 6f 79 28 26 70 43 6f 6c  plwDestroy(&pCol
2dcd5 6c 65 63 74 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20  lector->plw);.  
2dcd6 70 6c 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63  plwInit(&pCollec
2dcd7 74 6f 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c  tor->plw, &pColl
2dcd8 65 63 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63  ector->dlw, iDoc
2dcd9 69 64 29 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f  id);.}.static vo
2dcda 69 64 20 64 6c 63 41 64 64 50 6f 73 28 44 4c 43  id dlcAddPos(DLC
2dcdb 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f 6c 6c 65  ollector *pColle
2dcdc 63 74 6f 72 2c 20 69 6e 74 20 69 43 6f 6c 75 6d  ctor, int iColum
2dcdd 6e 2c 20 69 6e 74 20 69 50 6f 73 2c 0a 20 20 20  n, int iPos,.   
2dcde 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dcdf 20 20 20 69 6e 74 20 69 53 74 61 72 74 4f 66 66     int iStartOff
2dce0 73 65 74 2c 20 69 6e 74 20 69 45 6e 64 4f 66 66  set, int iEndOff
2dce1 73 65 74 29 7b 0a 20 20 70 6c 77 41 64 64 28 26  set){.  plwAdd(&
2dce2 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 70 6c 77 2c  pCollector->plw,
2dce3 20 69 43 6f 6c 75 6d 6e 2c 20 69 50 6f 73 2c 20   iColumn, iPos, 
2dce4 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 45  iStartOffset, iE
2dce5 6e 64 4f 66 66 73 65 74 29 3b 0a 7d 0a 0a 73 74  ndOffset);.}..st
2dce6 61 74 69 63 20 44 4c 43 6f 6c 6c 65 63 74 6f 72  atic DLCollector
2dce7 20 2a 64 6c 63 4e 65 77 28 73 71 6c 69 74 65 5f   *dlcNew(sqlite_
2dce8 69 6e 74 36 34 20 69 44 6f 63 69 64 2c 20 44 6f  int64 iDocid, Do
2dce9 63 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 29  cListType iType)
2dcea 7b 0a 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20  {.  DLCollector 
2dceb 2a 70 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 73 71  *pCollector = sq
2dcec 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a  lite3_malloc(siz
2dced 65 6f 66 28 44 4c 43 6f 6c 6c 65 63 74 6f 72 29  eof(DLCollector)
2dcee 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  );.  dataBufferI
2dcef 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d  nit(&pCollector-
2dcf0 3e 62 2c 20 30 29 3b 0a 20 20 64 6c 77 49 6e 69  >b, 0);.  dlwIni
2dcf1 74 28 26 70 43 6f 6c 6c 65 63 74 6f 72 2d 3e 64  t(&pCollector->d
2dcf2 6c 77 2c 20 69 54 79 70 65 2c 20 26 70 43 6f 6c  lw, iType, &pCol
2dcf3 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20 20 70 6c  lector->b);.  pl
2dcf4 77 49 6e 69 74 28 26 70 43 6f 6c 6c 65 63 74 6f  wInit(&pCollecto
2dcf5 72 2d 3e 70 6c 77 2c 20 26 70 43 6f 6c 6c 65 63  r->plw, &pCollec
2dcf6 74 6f 72 2d 3e 64 6c 77 2c 20 69 44 6f 63 69 64  tor->dlw, iDocid
2dcf7 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 43 6f 6c  );.  return pCol
2dcf8 6c 65 63 74 6f 72 3b 0a 7d 0a 73 74 61 74 69 63  lector;.}.static
2dcf9 20 76 6f 69 64 20 64 6c 63 44 65 6c 65 74 65 28   void dlcDelete(
2dcfa 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43 6f  DLCollector *pCo
2dcfb 6c 6c 65 63 74 6f 72 29 7b 0a 20 20 70 6c 77 44  llector){.  plwD
2dcfc 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74  estroy(&pCollect
2dcfd 6f 72 2d 3e 70 6c 77 29 3b 0a 20 20 64 6c 77 44  or->plw);.  dlwD
2dcfe 65 73 74 72 6f 79 28 26 70 43 6f 6c 6c 65 63 74  estroy(&pCollect
2dcff 6f 72 2d 3e 64 6c 77 29 3b 0a 20 20 64 61 74 61  or->dlw);.  data
2dd00 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 70  BufferDestroy(&p
2dd01 43 6f 6c 6c 65 63 74 6f 72 2d 3e 62 29 3b 0a 20  Collector->b);. 
2dd02 20 53 43 52 41 4d 42 4c 45 28 70 43 6f 6c 6c 65   SCRAMBLE(pColle
2dd03 63 74 6f 72 29 3b 0a 20 20 73 71 6c 69 74 65 33  ctor);.  sqlite3
2dd04 5f 66 72 65 65 28 70 43 6f 6c 6c 65 63 74 6f 72  _free(pCollector
2dd05 29 3b 0a 7d 0a 0a 0a 2f 2a 20 43 6f 70 79 20 74  );.}.../* Copy t
2dd06 68 65 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20  he doclist data 
2dd07 6f 66 20 69 54 79 70 65 20 69 6e 20 70 44 61 74  of iType in pDat
2dd08 61 2f 6e 44 61 74 61 20 69 6e 74 6f 20 2a 6f 75  a/nData into *ou
2dd09 74 2c 20 74 72 69 6d 6d 69 6e 67 0a 2a 2a 20 75  t, trimming.** u
2dd0a 6e 6e 65 63 65 73 73 61 72 79 20 64 61 74 61 20  nnecessary data 
2dd0b 61 73 20 77 65 20 67 6f 2e 20 20 4f 6e 6c 79 20  as we go.  Only 
2dd0c 63 6f 6c 75 6d 6e 73 20 6d 61 74 63 68 69 6e 67  columns matching
2dd0d 20 69 43 6f 6c 75 6d 6e 20 61 72 65 0a 2a 2a 20   iColumn are.** 
2dd0e 63 6f 70 69 65 64 2c 20 61 6c 6c 20 63 6f 6c 75  copied, all colu
2dd0f 6d 6e 73 20 63 6f 70 69 65 64 20 69 66 20 69 43  mns copied if iC
2dd10 6f 6c 75 6d 6e 20 69 73 20 2d 31 2e 20 20 45 6c  olumn is -1.  El
2dd11 65 6d 65 6e 74 73 20 77 69 74 68 20 6e 6f 0a 2a  ements with no.*
2dd12 2a 20 6d 61 74 63 68 69 6e 67 20 63 6f 6c 75 6d  * matching colum
2dd13 6e 73 20 61 72 65 20 64 72 6f 70 70 65 64 2e 20  ns are dropped. 
2dd14 20 54 68 65 20 6f 75 74 70 75 74 20 69 73 20 61   The output is a
2dd15 6e 20 69 4f 75 74 54 79 70 65 20 64 6f 63 6c 69  n iOutType docli
2dd16 73 74 2e 0a 2a 2f 0a 2f 2a 20 4e 4f 54 45 28 73  st..*/./* NOTE(s
2dd17 68 65 73 73 29 20 54 68 69 73 20 63 6f 64 65 20  hess) This code 
2dd18 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20 61 66  is only valid af
2dd19 74 65 72 20 61 6c 6c 20 64 6f 63 6c 69 73 74 73  ter all doclists
2dd1a 20 61 72 65 20 6d 65 72 67 65 64 2e 0a 2a 2a 20   are merged..** 
2dd1b 49 66 20 74 68 69 73 20 69 73 20 72 75 6e 20 62  If this is run b
2dd1c 65 66 6f 72 65 20 6d 65 72 67 65 73 2c 20 74 68  efore merges, th
2dd1d 65 6e 20 64 6f 63 6c 69 73 74 20 69 74 65 6d 73  en doclist items
2dd1e 20 77 68 69 63 68 20 72 65 70 72 65 73 65 6e 74   which represent
2dd1f 0a 2a 2a 20 64 65 6c 65 74 69 6f 6e 20 77 69 6c  .** deletion wil
2dd20 6c 20 62 65 20 74 72 69 6d 6d 65 64 2c 20 61 6e  l be trimmed, an
2dd21 64 20 77 69 6c 6c 20 74 68 75 73 20 6e 6f 74 20  d will thus not 
2dd22 65 66 66 65 63 74 20 61 20 64 65 6c 65 74 69 6f  effect a deletio
2dd23 6e 0a 2a 2a 20 64 75 72 69 6e 67 20 74 68 65 20  n.** during the 
2dd24 6d 65 72 67 65 2e 0a 2a 2f 0a 73 74 61 74 69 63  merge..*/.static
2dd25 20 76 6f 69 64 20 64 6f 63 4c 69 73 74 54 72 69   void docListTri
2dd26 6d 28 44 6f 63 4c 69 73 74 54 79 70 65 20 69 54  m(DocListType iT
2dd27 79 70 65 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  ype, const char 
2dd28 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
2dd29 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
2dd2a 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
2dd2b 43 6f 6c 75 6d 6e 2c 20 44 6f 63 4c 69 73 74 54  Column, DocListT
2dd2c 79 70 65 20 69 4f 75 74 54 79 70 65 2c 20 44 61  ype iOutType, Da
2dd2d 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a  taBuffer *out){.
2dd2e 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61    DLReader dlRea
2dd2f 64 65 72 3b 0a 20 20 44 4c 57 72 69 74 65 72 20  der;.  DLWriter 
2dd30 64 6c 57 72 69 74 65 72 3b 0a 0a 20 20 61 73 73  dlWriter;..  ass
2dd31 65 72 74 28 20 69 4f 75 74 54 79 70 65 3c 3d 69  ert( iOutType<=i
2dd32 54 79 70 65 20 29 3b 0a 0a 20 20 64 6c 72 49 6e  Type );..  dlrIn
2dd33 69 74 28 26 64 6c 52 65 61 64 65 72 2c 20 69 54  it(&dlReader, iT
2dd34 79 70 65 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  ype, pData, nDat
2dd35 61 29 3b 0a 20 20 64 6c 77 49 6e 69 74 28 26 64  a);.  dlwInit(&d
2dd36 6c 57 72 69 74 65 72 2c 20 69 4f 75 74 54 79 70  lWriter, iOutTyp
2dd37 65 2c 20 6f 75 74 29 3b 0a 0a 20 20 77 68 69 6c  e, out);..  whil
2dd38 65 28 20 21 64 6c 72 41 74 45 6e 64 28 26 64 6c  e( !dlrAtEnd(&dl
2dd39 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 50  Reader) ){.    P
2dd3a 4c 52 65 61 64 65 72 20 70 6c 52 65 61 64 65 72  LReader plReader
2dd3b 3b 0a 20 20 20 20 50 4c 57 72 69 74 65 72 20 70  ;.    PLWriter p
2dd3c 6c 57 72 69 74 65 72 3b 0a 20 20 20 20 69 6e 74  lWriter;.    int
2dd3d 20 6d 61 74 63 68 20 3d 20 30 3b 0a 0a 20 20 20   match = 0;..   
2dd3e 20 70 6c 72 49 6e 69 74 28 26 70 6c 52 65 61 64   plrInit(&plRead
2dd3f 65 72 2c 20 26 64 6c 52 65 61 64 65 72 29 3b 0a  er, &dlReader);.
2dd40 0a 20 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72  .    while( !plr
2dd41 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72 29  AtEnd(&plReader)
2dd42 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 43   ){.      if( iC
2dd43 6f 6c 75 6d 6e 3d 3d 2d 31 20 7c 7c 20 70 6c 72  olumn==-1 || plr
2dd44 43 6f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72  Column(&plReader
2dd45 29 3d 3d 69 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20  )==iColumn ){.  
2dd46 20 20 20 20 20 20 69 66 28 20 21 6d 61 74 63 68        if( !match
2dd47 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70 6c   ){.          pl
2dd48 77 49 6e 69 74 28 26 70 6c 57 72 69 74 65 72 2c  wInit(&plWriter,
2dd49 20 26 64 6c 57 72 69 74 65 72 2c 20 64 6c 72 44   &dlWriter, dlrD
2dd4a 6f 63 69 64 28 26 64 6c 52 65 61 64 65 72 29 29  ocid(&dlReader))
2dd4b 3b 0a 20 20 20 20 20 20 20 20 20 20 6d 61 74 63  ;.          matc
2dd4c 68 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  h = 1;.        }
2dd4d 0a 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28  .        plwAdd(
2dd4e 26 70 6c 57 72 69 74 65 72 2c 20 70 6c 72 43 6f  &plWriter, plrCo
2dd4f 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 2c  lumn(&plReader),
2dd50 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70 6c   plrPosition(&pl
2dd51 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20 20  Reader),.       
2dd52 20 20 20 20 20 20 20 20 70 6c 72 53 74 61 72 74          plrStart
2dd53 4f 66 66 73 65 74 28 26 70 6c 52 65 61 64 65 72  Offset(&plReader
2dd54 29 2c 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  ), plrEndOffset(
2dd55 26 70 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20  &plReader));.   
2dd56 20 20 20 7d 0a 20 20 20 20 20 20 70 6c 72 53 74     }.      plrSt
2dd57 65 70 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 20  ep(&plReader);. 
2dd58 20 20 20 7d 0a 20 20 20 20 69 66 28 20 6d 61 74     }.    if( mat
2dd59 63 68 20 29 7b 0a 20 20 20 20 20 20 70 6c 77 54  ch ){.      plwT
2dd5a 65 72 6d 69 6e 61 74 65 28 26 70 6c 57 72 69 74  erminate(&plWrit
2dd5b 65 72 29 3b 0a 20 20 20 20 20 20 70 6c 77 44 65  er);.      plwDe
2dd5c 73 74 72 6f 79 28 26 70 6c 57 72 69 74 65 72 29  stroy(&plWriter)
2dd5d 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 70 6c 72  ;.    }..    plr
2dd5e 44 65 73 74 72 6f 79 28 26 70 6c 52 65 61 64 65  Destroy(&plReade
2dd5f 72 29 3b 0a 20 20 20 20 64 6c 72 53 74 65 70 28  r);.    dlrStep(
2dd60 26 64 6c 52 65 61 64 65 72 29 3b 0a 20 20 7d 0a  &dlReader);.  }.
2dd61 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26 64 6c    dlwDestroy(&dl
2dd62 57 72 69 74 65 72 29 3b 0a 20 20 64 6c 72 44 65  Writer);.  dlrDe
2dd63 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72 29  stroy(&dlReader)
2dd64 3b 0a 7d 0a 0a 2f 2a 20 55 73 65 64 20 62 79 20  ;.}../* Used by 
2dd65 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 29 20 74  docListMerge() t
2dd66 6f 20 6b 65 65 70 20 64 6f 63 6c 69 73 74 73 20  o keep doclists 
2dd67 69 6e 20 74 68 65 20 61 73 63 65 6e 64 69 6e 67  in the ascending
2dd68 20 6f 72 64 65 72 20 62 79 0a 2a 2a 20 64 6f 63   order by.** doc
2dd69 69 64 2c 20 74 68 65 6e 20 61 73 63 65 6e 64 69  id, then ascendi
2dd6a 6e 67 20 6f 72 64 65 72 20 62 79 20 61 67 65 20  ng order by age 
2dd6b 28 73 6f 20 74 68 65 20 6e 65 77 65 73 74 20 63  (so the newest c
2dd6c 6f 6d 65 73 20 66 69 72 73 74 29 2e 0a 2a 2f 0a  omes first)..*/.
2dd6d 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 4f  typedef struct O
2dd6e 72 64 65 72 65 64 44 4c 52 65 61 64 65 72 20 7b  rderedDLReader {
2dd6f 0a 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52 65  .  DLReader *pRe
2dd70 61 64 65 72 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f  ader;..  /* TODO
2dd71 28 73 68 65 73 73 29 20 49 66 20 77 65 20 61 73  (shess) If we as
2dd72 73 75 6d 65 20 74 68 61 74 20 64 6f 63 4c 69 73  sume that docLis
2dd73 74 4d 65 72 67 65 20 70 52 65 61 64 65 72 73 20  tMerge pReaders 
2dd74 69 73 20 6f 72 64 65 72 65 64 20 62 79 0a 20 20  is ordered by.  
2dd75 2a 2a 20 61 67 65 20 28 77 68 69 63 68 20 77 65  ** age (which we
2dd76 20 64 6f 29 2c 20 74 68 65 6e 20 77 65 20 63 6f   do), then we co
2dd77 75 6c 64 20 75 73 65 20 70 52 65 61 64 65 72 20  uld use pReader 
2dd78 63 6f 6d 70 61 72 69 73 6f 6e 73 20 74 6f 20 62  comparisons to b
2dd79 72 65 61 6b 0a 20 20 2a 2a 20 74 69 65 73 2e 0a  reak.  ** ties..
2dd7a 20 20 2a 2f 0a 20 20 69 6e 74 20 69 64 78 3b 0a    */.  int idx;.
2dd7b 7d 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65  } OrderedDLReade
2dd7c 72 3b 0a 0a 2f 2a 20 4f 72 64 65 72 20 65 6f 66  r;../* Order eof
2dd7d 20 74 6f 20 65 6e 64 2c 20 74 68 65 6e 20 62 79   to end, then by
2dd7e 20 64 6f 63 69 64 20 61 73 63 2c 20 69 64 78 20   docid asc, idx 
2dd7f 64 65 73 63 2e 20 2a 2f 0a 73 74 61 74 69 63 20  desc. */.static 
2dd80 69 6e 74 20 6f 72 64 65 72 65 64 44 4c 52 65 61  int orderedDLRea
2dd81 64 65 72 43 6d 70 28 4f 72 64 65 72 65 64 44 4c  derCmp(OrderedDL
2dd82 52 65 61 64 65 72 20 2a 72 31 2c 20 4f 72 64 65  Reader *r1, Orde
2dd83 72 65 64 44 4c 52 65 61 64 65 72 20 2a 72 32 29  redDLReader *r2)
2dd84 7b 0a 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64  {.  if( dlrAtEnd
2dd85 28 72 31 2d 3e 70 52 65 61 64 65 72 29 20 29 7b  (r1->pReader) ){
2dd86 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e  .    if( dlrAtEn
2dd87 64 28 72 32 2d 3e 70 52 65 61 64 65 72 29 20 29  d(r2->pReader) )
2dd88 20 72 65 74 75 72 6e 20 30 3b 20 20 2f 2a 20 42   return 0;  /* B
2dd89 6f 74 68 20 61 74 45 6e 64 28 29 2e 20 2a 2f 0a  oth atEnd(). */.
2dd8a 20 20 20 20 72 65 74 75 72 6e 20 31 3b 20 20 20      return 1;   
2dd8b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2dd8c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 6e             /* On
2dd8d 6c 79 20 72 31 20 61 74 45 6e 64 28 29 2e 20 2a  ly r1 atEnd(). *
2dd8e 2f 0a 20 20 7d 0a 20 20 69 66 28 20 64 6c 72 41  /.  }.  if( dlrA
2dd8f 74 45 6e 64 28 72 32 2d 3e 70 52 65 61 64 65 72  tEnd(r2->pReader
2dd90 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b 20 20  ) ) return -1;  
2dd91 20 2f 2a 20 4f 6e 6c 79 20 72 32 20 61 74 45 6e   /* Only r2 atEn
2dd92 64 28 29 2e 20 2a 2f 0a 0a 20 20 69 66 28 20 64  d(). */..  if( d
2dd93 6c 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61  lrDocid(r1->pRea
2dd94 64 65 72 29 3c 64 6c 72 44 6f 63 69 64 28 72 32  der)<dlrDocid(r2
2dd95 2d 3e 70 52 65 61 64 65 72 29 20 29 20 72 65 74  ->pReader) ) ret
2dd96 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20 64 6c  urn -1;.  if( dl
2dd97 72 44 6f 63 69 64 28 72 31 2d 3e 70 52 65 61 64  rDocid(r1->pRead
2dd98 65 72 29 3e 64 6c 72 44 6f 63 69 64 28 72 32 2d  er)>dlrDocid(r2-
2dd99 3e 70 52 65 61 64 65 72 29 20 29 20 72 65 74 75  >pReader) ) retu
2dd9a 72 6e 20 31 3b 0a 0a 20 20 2f 2a 20 44 65 73 63  rn 1;..  /* Desc
2dd9b 65 6e 64 69 6e 67 20 6f 6e 20 69 64 78 2e 20 2a  ending on idx. *
2dd9c 2f 0a 20 20 72 65 74 75 72 6e 20 72 32 2d 3e 69  /.  return r2->i
2dd9d 64 78 2d 72 31 2d 3e 69 64 78 3b 0a 7d 0a 0a 2f  dx-r1->idx;.}../
2dd9e 2a 20 42 75 62 62 6c 65 20 70 5b 30 5d 20 74 6f  * Bubble p[0] to
2dd9f 20 61 70 70 72 6f 70 72 69 61 74 65 20 70 6c 61   appropriate pla
2dda0 63 65 20 69 6e 20 70 5b 31 2e 2e 6e 2d 31 5d 2e  ce in p[1..n-1].
2dda1 20 20 41 73 73 75 6d 65 73 20 74 68 61 74 0a 2a    Assumes that.*
2dda2 2a 20 70 5b 31 2e 2e 6e 2d 31 5d 20 69 73 20 61  * p[1..n-1] is a
2dda3 6c 72 65 61 64 79 20 73 6f 72 74 65 64 2e 0a 2a  lready sorted..*
2dda4 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29  /./* TODO(shess)
2dda5 20 49 73 20 74 68 69 73 20 66 72 65 71 75 65 6e   Is this frequen
2dda6 74 20 65 6e 6f 75 67 68 20 74 6f 20 77 61 72 72  t enough to warr
2dda7 61 6e 74 20 61 20 62 69 6e 61 72 79 20 73 65 61  ant a binary sea
2dda8 72 63 68 3f 0a 2a 2a 20 42 65 66 6f 72 65 20 69  rch?.** Before i
2dda9 6d 70 6c 65 6d 65 6e 74 69 6e 67 20 74 68 61 74  mplementing that
2ddaa 2c 20 69 6e 73 74 72 75 6d 65 6e 74 20 74 68 65  , instrument the
2ddab 20 63 6f 64 65 20 74 6f 20 63 68 65 63 6b 2e 20   code to check. 
2ddac 20 49 6e 20 6d 6f 73 74 0a 2a 2a 20 63 75 72 72   In most.** curr
2ddad 65 6e 74 20 75 73 61 67 65 2c 20 49 20 65 78 70  ent usage, I exp
2ddae 65 63 74 20 74 68 61 74 20 70 5b 30 5d 20 77 69  ect that p[0] wi
2ddaf 6c 6c 20 62 65 20 6c 65 73 73 20 74 68 61 6e 20  ll be less than 
2ddb0 70 5b 31 5d 20 61 20 76 65 72 79 0a 2a 2a 20 68  p[1] a very.** h
2ddb1 69 67 68 20 70 72 6f 70 6f 72 74 69 6f 6e 20 6f  igh proportion o
2ddb2 66 20 74 68 65 20 74 69 6d 65 2e 0a 2a 2f 0a 73  f the time..*/.s
2ddb3 74 61 74 69 63 20 76 6f 69 64 20 6f 72 64 65 72  tatic void order
2ddb4 65 64 44 4c 52 65 61 64 65 72 52 65 6f 72 64 65  edDLReaderReorde
2ddb5 72 28 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65  r(OrderedDLReade
2ddb6 72 20 2a 70 2c 20 69 6e 74 20 6e 29 7b 0a 20 20  r *p, int n){.  
2ddb7 77 68 69 6c 65 28 20 6e 3e 31 20 26 26 20 6f 72  while( n>1 && or
2ddb8 64 65 72 65 64 44 4c 52 65 61 64 65 72 43 6d 70  deredDLReaderCmp
2ddb9 28 70 2c 20 70 2b 31 29 3e 30 20 29 7b 0a 20 20  (p, p+1)>0 ){.  
2ddba 20 20 4f 72 64 65 72 65 64 44 4c 52 65 61 64 65    OrderedDLReade
2ddbb 72 20 74 6d 70 20 3d 20 70 5b 30 5d 3b 0a 20 20  r tmp = p[0];.  
2ddbc 20 20 70 5b 30 5d 20 3d 20 70 5b 31 5d 3b 0a 20    p[0] = p[1];. 
2ddbd 20 20 20 70 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20     p[1] = tmp;. 
2ddbe 20 20 20 6e 2d 2d 3b 0a 20 20 20 20 70 2b 2b 3b     n--;.    p++;
2ddbf 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 47 69 76 65 6e  .  }.}../* Given
2ddc0 20 61 6e 20 61 72 72 61 79 20 6f 66 20 64 6f 63   an array of doc
2ddc1 6c 69 73 74 20 72 65 61 64 65 72 73 2c 20 6d 65  list readers, me
2ddc2 72 67 65 20 74 68 65 69 72 20 64 6f 63 6c 69 73  rge their doclis
2ddc3 74 20 65 6c 65 6d 65 6e 74 73 0a 2a 2a 20 69 6e  t elements.** in
2ddc4 74 6f 20 6f 75 74 20 69 6e 20 73 6f 72 74 65 64  to out in sorted
2ddc5 20 6f 72 64 65 72 20 28 62 79 20 64 6f 63 69 64   order (by docid
2ddc6 29 2c 20 64 72 6f 70 70 69 6e 67 20 65 6c 65 6d  ), dropping elem
2ddc7 65 6e 74 73 20 66 72 6f 6d 20 6f 6c 64 65 72 0a  ents from older.
2ddc8 2a 2a 20 72 65 61 64 65 72 73 20 77 68 65 6e 20  ** readers when 
2ddc9 74 68 65 72 65 20 69 73 20 61 20 64 75 70 6c 69  there is a dupli
2ddca 63 61 74 65 20 64 6f 63 69 64 2e 20 20 70 52 65  cate docid.  pRe
2ddcb 61 64 65 72 73 20 69 73 20 61 73 73 75 6d 65 64  aders is assumed
2ddcc 20 74 6f 20 62 65 0a 2a 2a 20 6f 72 64 65 72 65   to be.** ordere
2ddcd 64 20 62 79 20 61 67 65 2c 20 6f 6c 64 65 73 74  d by age, oldest
2ddce 20 66 69 72 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f   first..*/./* TO
2ddcf 44 4f 28 73 68 65 73 73 29 20 6e 52 65 61 64 65  DO(shess) nReade
2ddd0 72 73 20 6d 75 73 74 20 62 65 20 3c 3d 20 4d 45  rs must be <= ME
2ddd1 52 47 45 5f 43 4f 55 4e 54 2e 20 20 54 68 69 73  RGE_COUNT.  This
2ddd2 20 73 68 6f 75 6c 64 20 70 72 6f 62 61 62 6c 79   should probably
2ddd3 0a 2a 2a 20 62 65 20 66 69 78 65 64 2e 0a 2a 2f  .** be fixed..*/
2ddd4 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
2ddd5 4c 69 73 74 4d 65 72 67 65 28 44 61 74 61 42 75  ListMerge(DataBu
2ddd6 66 66 65 72 20 2a 6f 75 74 2c 0a 20 20 20 20 20  ffer *out,.     
2ddd7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ddd8 20 20 20 20 44 4c 52 65 61 64 65 72 20 2a 70 52      DLReader *pR
2ddd9 65 61 64 65 72 73 2c 20 69 6e 74 20 6e 52 65 61  eaders, int nRea
2ddda 64 65 72 73 29 7b 0a 20 20 4f 72 64 65 72 65 64  ders){.  Ordered
2dddb 44 4c 52 65 61 64 65 72 20 72 65 61 64 65 72 73  DLReader readers
2dddc 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a 20  [MERGE_COUNT];. 
2dddd 20 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72   DLWriter writer
2ddde 3b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a 20 20  ;.  int i, n;.  
2dddf 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 74 61  const char *pSta
2dde0 72 74 20 3d 20 30 3b 0a 20 20 69 6e 74 20 6e 53  rt = 0;.  int nS
2dde1 74 61 72 74 20 3d 20 30 3b 0a 20 20 73 71 6c 69  tart = 0;.  sqli
2dde2 74 65 5f 69 6e 74 36 34 20 69 46 69 72 73 74 44  te_int64 iFirstD
2dde3 6f 63 69 64 20 3d 20 30 2c 20 69 4c 61 73 74 44  ocid = 0, iLastD
2dde4 6f 63 69 64 20 3d 20 30 3b 0a 0a 20 20 61 73 73  ocid = 0;..  ass
2dde5 65 72 74 28 20 6e 52 65 61 64 65 72 73 3e 30 20  ert( nReaders>0 
2dde6 29 3b 0a 20 20 69 66 28 20 6e 52 65 61 64 65 72  );.  if( nReader
2dde7 73 3d 3d 31 20 29 7b 0a 20 20 20 20 64 61 74 61  s==1 ){.    data
2dde8 42 75 66 66 65 72 41 70 70 65 6e 64 28 6f 75 74  BufferAppend(out
2dde9 2c 20 64 6c 72 44 6f 63 44 61 74 61 28 70 52 65  , dlrDocData(pRe
2ddea 61 64 65 72 73 29 2c 20 64 6c 72 41 6c 6c 44 61  aders), dlrAllDa
2ddeb 74 61 42 79 74 65 73 28 70 52 65 61 64 65 72 73  taBytes(pReaders
2ddec 29 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a  ));.    return;.
2dded 20 20 7d 0a 0a 20 20 61 73 73 65 72 74 28 20 6e    }..  assert( n
2ddee 52 65 61 64 65 72 73 3c 3d 4d 45 52 47 45 5f 43  Readers<=MERGE_C
2ddef 4f 55 4e 54 20 29 3b 0a 20 20 6e 20 3d 20 30 3b  OUNT );.  n = 0;
2ddf0 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52  .  for(i=0; i<nR
2ddf1 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20  eaders; i++){.  
2ddf2 20 20 61 73 73 65 72 74 28 20 70 52 65 61 64 65    assert( pReade
2ddf3 72 73 5b 69 5d 2e 69 54 79 70 65 3d 3d 70 52 65  rs[i].iType==pRe
2ddf4 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65 20 29  aders[0].iType )
2ddf5 3b 0a 20 20 20 20 72 65 61 64 65 72 73 5b 69 5d  ;.    readers[i]
2ddf6 2e 70 52 65 61 64 65 72 20 3d 20 70 52 65 61 64  .pReader = pRead
2ddf7 65 72 73 2b 69 3b 0a 20 20 20 20 72 65 61 64 65  ers+i;.    reade
2ddf8 72 73 5b 69 5d 2e 69 64 78 20 3d 20 69 3b 0a 20  rs[i].idx = i;. 
2ddf9 20 20 20 6e 20 2b 3d 20 64 6c 72 41 6c 6c 44 61     n += dlrAllDa
2ddfa 74 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72  taBytes(&pReader
2ddfb 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 2f 2a 20  s[i]);.  }.  /* 
2ddfc 43 6f 6e 73 65 72 76 61 74 69 76 65 6c 79 20 73  Conservatively s
2ddfd 69 7a 65 20 6f 75 74 70 75 74 20 74 6f 20 73 75  ize output to su
2ddfe 6d 20 6f 66 20 69 6e 70 75 74 73 2e 20 20 4f 75  m of inputs.  Ou
2ddff 74 70 75 74 20 73 68 6f 75 6c 64 20 65 6e 64 0a  tput should end.
2de00 20 20 2a 2a 20 75 70 20 73 74 72 69 63 74 6c 79    ** up strictly
2de01 20 73 6d 61 6c 6c 65 72 20 74 68 61 6e 20 69 6e   smaller than in
2de02 70 75 74 2e 0a 20 20 2a 2f 0a 20 20 64 61 74 61  put..  */.  data
2de03 42 75 66 66 65 72 45 78 70 61 6e 64 28 6f 75 74  BufferExpand(out
2de04 2c 20 6e 29 3b 0a 0a 20 20 2f 2a 20 47 65 74 20  , n);..  /* Get 
2de05 74 68 65 20 72 65 61 64 65 72 73 20 69 6e 74 6f  the readers into
2de06 20 73 6f 72 74 65 64 20 6f 72 64 65 72 2e 20 2a   sorted order. *
2de07 2f 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30  /.  while( i-->0
2de08 20 29 7b 0a 20 20 20 20 6f 72 64 65 72 65 64 44   ){.    orderedD
2de09 4c 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 72  LReaderReorder(r
2de0a 65 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65  eaders+i, nReade
2de0b 72 73 2d 69 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c  rs-i);.  }..  dl
2de0c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 70  wInit(&writer, p
2de0d 52 65 61 64 65 72 73 5b 30 5d 2e 69 54 79 70 65  Readers[0].iType
2de0e 2c 20 6f 75 74 29 3b 0a 20 20 77 68 69 6c 65 28  , out);.  while(
2de0f 20 21 64 6c 72 41 74 45 6e 64 28 72 65 61 64 65   !dlrAtEnd(reade
2de10 72 73 5b 30 5d 2e 70 52 65 61 64 65 72 29 20 29  rs[0].pReader) )
2de11 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74  {.    sqlite_int
2de12 36 34 20 69 44 6f 63 69 64 20 3d 20 64 6c 72 44  64 iDocid = dlrD
2de13 6f 63 69 64 28 72 65 61 64 65 72 73 5b 30 5d 2e  ocid(readers[0].
2de14 70 52 65 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f  pReader);..    /
2de15 2a 20 49 66 20 74 68 69 73 20 69 73 20 61 20 63  * If this is a c
2de16 6f 6e 74 69 6e 75 61 74 69 6f 6e 20 6f 66 20 74  ontinuation of t
2de17 68 65 20 63 75 72 72 65 6e 74 20 62 75 66 66 65  he current buffe
2de18 72 20 74 6f 20 63 6f 70 79 2c 20 65 78 74 65 6e  r to copy, exten
2de19 64 0a 20 20 20 20 2a 2a 20 74 68 61 74 20 62 75  d.    ** that bu
2de1a 66 66 65 72 2e 20 20 6d 65 6d 63 70 79 28 29 20  ffer.  memcpy() 
2de1b 73 65 65 6d 73 20 74 6f 20 62 65 20 6d 6f 72 65  seems to be more
2de1c 20 65 66 66 69 63 69 65 6e 74 20 69 66 20 69 74   efficient if it
2de1d 20 68 61 73 20 61 0a 20 20 20 20 2a 2a 20 6c 6f   has a.    ** lo
2de1e 74 73 20 6f 66 20 64 61 74 61 20 74 6f 20 63 6f  ts of data to co
2de1f 70 79 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 69  py..    */.    i
2de20 66 28 20 64 6c 72 44 6f 63 44 61 74 61 28 72 65  f( dlrDocData(re
2de21 61 64 65 72 73 5b 30 5d 2e 70 52 65 61 64 65 72  aders[0].pReader
2de22 29 3d 3d 70 53 74 61 72 74 2b 6e 53 74 61 72 74  )==pStart+nStart
2de23 20 29 7b 0a 20 20 20 20 20 20 6e 53 74 61 72 74   ){.      nStart
2de24 20 2b 3d 20 64 6c 72 44 6f 63 44 61 74 61 42 79   += dlrDocDataBy
2de25 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d 2e 70  tes(readers[0].p
2de26 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d 65 6c  Reader);.    }el
2de27 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 70 53  se{.      if( pS
2de28 74 61 72 74 21 3d 30 20 29 7b 0a 20 20 20 20 20  tart!=0 ){.     
2de29 20 20 20 64 6c 77 41 70 70 65 6e 64 28 26 77 72     dlwAppend(&wr
2de2a 69 74 65 72 2c 20 70 53 74 61 72 74 2c 20 6e 53  iter, pStart, nS
2de2b 74 61 72 74 2c 20 69 46 69 72 73 74 44 6f 63 69  tart, iFirstDoci
2de2c 64 2c 20 69 4c 61 73 74 44 6f 63 69 64 29 3b 0a  d, iLastDocid);.
2de2d 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 53        }.      pS
2de2e 74 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74  tart = dlrDocDat
2de2f 61 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65  a(readers[0].pRe
2de30 61 64 65 72 29 3b 0a 20 20 20 20 20 20 6e 53 74  ader);.      nSt
2de31 61 72 74 20 3d 20 64 6c 72 44 6f 63 44 61 74 61  art = dlrDocData
2de32 42 79 74 65 73 28 72 65 61 64 65 72 73 5b 30 5d  Bytes(readers[0]
2de33 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 20  .pReader);.     
2de34 20 69 46 69 72 73 74 44 6f 63 69 64 20 3d 20 69   iFirstDocid = i
2de35 44 6f 63 69 64 3b 0a 20 20 20 20 7d 0a 20 20 20  Docid;.    }.   
2de36 20 69 4c 61 73 74 44 6f 63 69 64 20 3d 20 69 44   iLastDocid = iD
2de37 6f 63 69 64 3b 0a 20 20 20 20 64 6c 72 53 74 65  ocid;.    dlrSte
2de38 70 28 72 65 61 64 65 72 73 5b 30 5d 2e 70 52 65  p(readers[0].pRe
2de39 61 64 65 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 44  ader);..    /* D
2de3a 72 6f 70 20 61 6c 6c 20 6f 66 20 74 68 65 20 6f  rop all of the o
2de3b 6c 64 65 72 20 65 6c 65 6d 65 6e 74 73 20 77 69  lder elements wi
2de3c 74 68 20 74 68 65 20 73 61 6d 65 20 64 6f 63 69  th the same doci
2de3d 64 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d  d. */.    for(i=
2de3e 31 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26 26  1; i<nReaders &&
2de3f 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 21 64  .             !d
2de40 6c 72 41 74 45 6e 64 28 72 65 61 64 65 72 73 5b  lrAtEnd(readers[
2de41 69 5d 2e 70 52 65 61 64 65 72 29 20 26 26 0a 20  i].pReader) &&. 
2de42 20 20 20 20 20 20 20 20 20 20 20 20 64 6c 72 44              dlrD
2de43 6f 63 69 64 28 72 65 61 64 65 72 73 5b 69 5d 2e  ocid(readers[i].
2de44 70 52 65 61 64 65 72 29 3d 3d 69 44 6f 63 69 64  pReader)==iDocid
2de45 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 64 6c  ; i++){.      dl
2de46 72 53 74 65 70 28 72 65 61 64 65 72 73 5b 69 5d  rStep(readers[i]
2de47 2e 70 52 65 61 64 65 72 29 3b 0a 20 20 20 20 7d  .pReader);.    }
2de48 0a 0a 20 20 20 20 2f 2a 20 47 65 74 20 74 68 65  ..    /* Get the
2de49 20 72 65 61 64 65 72 73 20 62 61 63 6b 20 69 6e   readers back in
2de4a 74 6f 20 6f 72 64 65 72 2e 20 2a 2f 0a 20 20 20  to order. */.   
2de4b 20 77 68 69 6c 65 28 20 69 2d 2d 3e 30 20 29 7b   while( i-->0 ){
2de4c 0a 20 20 20 20 20 20 6f 72 64 65 72 65 64 44 4c  .      orderedDL
2de4d 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 72 65  ReaderReorder(re
2de4e 61 64 65 72 73 2b 69 2c 20 6e 52 65 61 64 65 72  aders+i, nReader
2de4f 73 2d 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a  s-i);.    }.  }.
2de50 0a 20 20 2f 2a 20 43 6f 70 79 20 6f 76 65 72 20  .  /* Copy over 
2de51 61 6e 79 20 72 65 6d 61 69 6e 69 6e 67 20 65 6c  any remaining el
2de52 65 6d 65 6e 74 73 2e 20 2a 2f 0a 20 20 69 66 28  ements. */.  if(
2de53 20 6e 53 74 61 72 74 3e 30 20 29 20 64 6c 77 41   nStart>0 ) dlwA
2de54 70 70 65 6e 64 28 26 77 72 69 74 65 72 2c 20 70  ppend(&writer, p
2de55 53 74 61 72 74 2c 20 6e 53 74 61 72 74 2c 20 69  Start, nStart, i
2de56 46 69 72 73 74 44 6f 63 69 64 2c 20 69 4c 61 73  FirstDocid, iLas
2de57 74 44 6f 63 69 64 29 3b 0a 20 20 64 6c 77 44 65  tDocid);.  dlwDe
2de58 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
2de59 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e  }../* Helper fun
2de5a 63 74 69 6f 6e 20 66 6f 72 20 70 6f 73 4c 69 73  ction for posLis
2de5b 74 55 6e 69 6f 6e 28 29 2e 20 20 43 6f 6d 70 61  tUnion().  Compa
2de5c 72 65 73 20 74 68 65 20 63 75 72 72 65 6e 74 20  res the current 
2de5d 70 6f 73 69 74 69 6f 6e 0a 2a 2a 20 62 65 74 77  position.** betw
2de5e 65 65 6e 20 6c 65 66 74 20 61 6e 64 20 72 69 67  een left and rig
2de5f 68 74 2c 20 72 65 74 75 72 6e 69 6e 67 20 61 73  ht, returning as
2de60 20 73 74 61 6e 64 61 72 64 20 43 20 69 64 69 6f   standard C idio
2de61 6d 20 6f 66 20 3c 30 20 69 66 0a 2a 2a 20 6c 65  m of <0 if.** le
2de62 66 74 3c 72 69 67 68 74 2c 20 3e 30 20 69 66 20  ft<right, >0 if 
2de63 6c 65 66 74 3e 72 69 67 68 74 2c 20 61 6e 64 20  left>right, and 
2de64 30 20 69 66 20 6c 65 66 74 3d 3d 72 69 67 68 74  0 if left==right
2de65 2e 20 20 22 45 6e 64 22 20 61 6c 77 61 79 73 0a  .  "End" always.
2de66 2a 2a 20 63 6f 6d 70 61 72 65 73 20 67 72 65 61  ** compares grea
2de67 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ter..*/.static i
2de68 6e 74 20 70 6f 73 4c 69 73 74 43 6d 70 28 50 4c  nt posListCmp(PL
2de69 52 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 50  Reader *pLeft, P
2de6a 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68 74 29  LReader *pRight)
2de6b 7b 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66  {.  assert( pLef
2de6c 74 2d 3e 69 54 79 70 65 3d 3d 70 52 69 67 68 74  t->iType==pRight
2de6d 2d 3e 69 54 79 70 65 20 29 3b 0a 20 20 69 66 28  ->iType );.  if(
2de6e 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d 44   pLeft->iType==D
2de6f 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74 75 72  L_DOCIDS ) retur
2de70 6e 20 30 3b 0a 0a 20 20 69 66 28 20 70 6c 72 41  n 0;..  if( plrA
2de71 74 45 6e 64 28 70 4c 65 66 74 29 20 29 20 72 65  tEnd(pLeft) ) re
2de72 74 75 72 6e 20 70 6c 72 41 74 45 6e 64 28 70 52  turn plrAtEnd(pR
2de73 69 67 68 74 29 20 3f 20 30 20 3a 20 31 3b 0a 20  ight) ? 0 : 1;. 
2de74 20 69 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52   if( plrAtEnd(pR
2de75 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d  ight) ) return -
2de76 31 3b 0a 0a 20 20 69 66 28 20 70 6c 72 43 6f 6c  1;..  if( plrCol
2de77 75 6d 6e 28 70 4c 65 66 74 29 3c 70 6c 72 43 6f  umn(pLeft)<plrCo
2de78 6c 75 6d 6e 28 70 52 69 67 68 74 29 20 29 20 72  lumn(pRight) ) r
2de79 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20  eturn -1;.  if( 
2de7a 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29  plrColumn(pLeft)
2de7b 3e 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68  >plrColumn(pRigh
2de7c 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a  t) ) return 1;..
2de7d 20 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f    if( plrPositio
2de7e 6e 28 70 4c 65 66 74 29 3c 70 6c 72 50 6f 73 69  n(pLeft)<plrPosi
2de7f 74 69 6f 6e 28 70 52 69 67 68 74 29 20 29 20 72  tion(pRight) ) r
2de80 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69 66 28 20  eturn -1;.  if( 
2de81 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65 66  plrPosition(pLef
2de82 74 29 3e 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70  t)>plrPosition(p
2de83 52 69 67 68 74 29 20 29 20 72 65 74 75 72 6e 20  Right) ) return 
2de84 31 3b 0a 20 20 69 66 28 20 70 4c 65 66 74 2d 3e  1;.  if( pLeft->
2de85 69 54 79 70 65 3d 3d 44 4c 5f 50 4f 53 49 54 49  iType==DL_POSITI
2de86 4f 4e 53 20 29 20 72 65 74 75 72 6e 20 30 3b 0a  ONS ) return 0;.
2de87 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f  .  if( plrStartO
2de88 66 66 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72  ffset(pLeft)<plr
2de89 53 74 61 72 74 4f 66 66 73 65 74 28 70 52 69 67  StartOffset(pRig
2de8a 68 74 29 20 29 20 72 65 74 75 72 6e 20 2d 31 3b  ht) ) return -1;
2de8b 0a 20 20 69 66 28 20 70 6c 72 53 74 61 72 74 4f  .  if( plrStartO
2de8c 66 66 73 65 74 28 70 4c 65 66 74 29 3e 70 6c 72  ffset(pLeft)>plr
2de8d 53 74 61 72 74 4f 66 66 73 65 74 28 70 52 69 67  StartOffset(pRig
2de8e 68 74 29 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  ht) ) return 1;.
2de8f 0a 20 20 69 66 28 20 70 6c 72 45 6e 64 4f 66 66  .  if( plrEndOff
2de90 73 65 74 28 70 4c 65 66 74 29 3c 70 6c 72 45 6e  set(pLeft)<plrEn
2de91 64 4f 66 66 73 65 74 28 70 52 69 67 68 74 29 20  dOffset(pRight) 
2de92 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 69  ) return -1;.  i
2de93 66 28 20 70 6c 72 45 6e 64 4f 66 66 73 65 74 28  f( plrEndOffset(
2de94 70 4c 65 66 74 29 3e 70 6c 72 45 6e 64 4f 66 66  pLeft)>plrEndOff
2de95 73 65 74 28 70 52 69 67 68 74 29 20 29 20 72 65  set(pRight) ) re
2de96 74 75 72 6e 20 31 3b 0a 0a 20 20 72 65 74 75 72  turn 1;..  retur
2de97 6e 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 72 69 74 65  n 0;.}../* Write
2de98 20 74 68 65 20 75 6e 69 6f 6e 20 6f 66 20 70 6f   the union of po
2de99 73 69 74 69 6f 6e 20 6c 69 73 74 73 20 69 6e 20  sition lists in 
2de9a 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
2de9b 20 74 6f 20 70 4f 75 74 2e 0a 2a 2a 20 22 55 6e   to pOut..** "Un
2de9c 69 6f 6e 22 20 69 6e 20 74 68 69 73 20 63 61 73  ion" in this cas
2de9d 65 20 6d 65 61 6e 69 6e 67 20 22 41 6c 6c 20 75  e meaning "All u
2de9e 6e 69 71 75 65 20 70 6f 73 69 74 69 6f 6e 20 74  nique position t
2de9f 75 70 6c 65 73 22 2e 20 20 53 68 6f 75 6c 64 0a  uples".  Should.
2dea0 2a 2a 20 77 6f 72 6b 20 77 69 74 68 20 61 6e 79  ** work with any
2dea1 20 64 6f 63 6c 69 73 74 20 74 79 70 65 2c 20 74   doclist type, t
2dea2 68 6f 75 67 68 20 62 6f 74 68 20 69 6e 70 75 74  hough both input
2dea3 73 20 61 6e 64 20 74 68 65 20 6f 75 74 70 75 74  s and the output
2dea4 0a 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 74 68  .** should be th
2dea5 65 20 73 61 6d 65 20 74 79 70 65 2e 0a 2a 2f 0a  e same type..*/.
2dea6 73 74 61 74 69 63 20 76 6f 69 64 20 70 6f 73 4c  static void posL
2dea7 69 73 74 55 6e 69 6f 6e 28 44 4c 52 65 61 64 65  istUnion(DLReade
2dea8 72 20 2a 70 4c 65 66 74 2c 20 44 4c 52 65 61 64  r *pLeft, DLRead
2dea9 65 72 20 2a 70 52 69 67 68 74 2c 20 44 4c 57 72  er *pRight, DLWr
2deaa 69 74 65 72 20 2a 70 4f 75 74 29 7b 0a 20 20 50  iter *pOut){.  P
2deab 4c 52 65 61 64 65 72 20 6c 65 66 74 2c 20 72 69  LReader left, ri
2deac 67 68 74 3b 0a 20 20 50 4c 57 72 69 74 65 72 20  ght;.  PLWriter 
2dead 77 72 69 74 65 72 3b 0a 0a 20 20 61 73 73 65 72  writer;..  asser
2deae 74 28 20 64 6c 72 44 6f 63 69 64 28 70 4c 65 66  t( dlrDocid(pLef
2deaf 74 29 3d 3d 64 6c 72 44 6f 63 69 64 28 70 52 69  t)==dlrDocid(pRi
2deb0 67 68 74 29 20 29 3b 0a 20 20 61 73 73 65 72 74  ght) );.  assert
2deb1 28 20 70 4c 65 66 74 2d 3e 69 54 79 70 65 3d 3d  ( pLeft->iType==
2deb2 70 52 69 67 68 74 2d 3e 69 54 79 70 65 20 29 3b  pRight->iType );
2deb3 0a 20 20 61 73 73 65 72 74 28 20 70 4c 65 66 74  .  assert( pLeft
2deb4 2d 3e 69 54 79 70 65 3d 3d 70 4f 75 74 2d 3e 69  ->iType==pOut->i
2deb5 54 79 70 65 20 29 3b 0a 0a 20 20 70 6c 72 49 6e  Type );..  plrIn
2deb6 69 74 28 26 6c 65 66 74 2c 20 70 4c 65 66 74 29  it(&left, pLeft)
2deb7 3b 0a 20 20 70 6c 72 49 6e 69 74 28 26 72 69 67  ;.  plrInit(&rig
2deb8 68 74 2c 20 70 52 69 67 68 74 29 3b 0a 20 20 70  ht, pRight);.  p
2deb9 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20  lwInit(&writer, 
2deba 70 4f 75 74 2c 20 64 6c 72 44 6f 63 69 64 28 70  pOut, dlrDocid(p
2debb 4c 65 66 74 29 29 3b 0a 0a 20 20 77 68 69 6c 65  Left));..  while
2debc 28 20 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66  ( !plrAtEnd(&lef
2debd 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e 64 28  t) || !plrAtEnd(
2debe 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69  &right) ){.    i
2debf 6e 74 20 63 20 3d 20 70 6f 73 4c 69 73 74 43 6d  nt c = posListCm
2dec0 70 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74 29  p(&left, &right)
2dec1 3b 0a 20 20 20 20 69 66 28 20 63 3c 30 20 29 7b  ;.    if( c<0 ){
2dec2 0a 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26  .      plwCopy(&
2dec3 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a  writer, &left);.
2dec4 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26 6c        plrStep(&l
2dec5 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  eft);.    }else 
2dec6 69 66 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 20  if( c>0 ){.     
2dec7 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74 65 72   plwCopy(&writer
2dec8 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
2dec9 20 70 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   plrStep(&right)
2deca 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
2decb 20 20 20 70 6c 77 43 6f 70 79 28 26 77 72 69 74     plwCopy(&writ
2decc 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20 20 20 20  er, &left);.    
2decd 20 20 70 6c 72 53 74 65 70 28 26 6c 65 66 74 29    plrStep(&left)
2dece 3b 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28  ;.      plrStep(
2decf 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 0a 20  &right);.    }. 
2ded0 20 7d 0a 0a 20 20 70 6c 77 54 65 72 6d 69 6e 61   }..  plwTermina
2ded1 74 65 28 26 77 72 69 74 65 72 29 3b 0a 20 20 70  te(&writer);.  p
2ded2 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
2ded3 72 29 3b 0a 20 20 70 6c 72 44 65 73 74 72 6f 79  r);.  plrDestroy
2ded4 28 26 6c 65 66 74 29 3b 0a 20 20 70 6c 72 44 65  (&left);.  plrDe
2ded5 73 74 72 6f 79 28 26 72 69 67 68 74 29 3b 0a 7d  stroy(&right);.}
2ded6 0a 0a 2f 2a 20 57 72 69 74 65 20 74 68 65 20 75  ../* Write the u
2ded7 6e 69 6f 6e 20 6f 66 20 64 6f 63 6c 69 73 74 73  nion of doclists
2ded8 20 69 6e 20 70 4c 65 66 74 20 61 6e 64 20 70 52   in pLeft and pR
2ded9 69 67 68 74 20 74 6f 20 70 4f 75 74 2e 20 20 46  ight to pOut.  F
2deda 6f 72 0a 2a 2a 20 64 6f 63 69 64 73 20 69 6e 20  or.** docids in 
2dedb 63 6f 6d 6d 6f 6e 20 62 65 74 77 65 65 6e 20 74  common between t
2dedc 68 65 20 69 6e 70 75 74 73 2c 20 74 68 65 20 75  he inputs, the u
2dedd 6e 69 6f 6e 20 6f 66 20 74 68 65 20 70 6f 73 69  nion of the posi
2dede 74 69 6f 6e 0a 2a 2a 20 6c 69 73 74 73 20 69 73  tion.** lists is
2dedf 20 77 72 69 74 74 65 6e 2e 20 20 49 6e 70 75 74   written.  Input
2dee0 73 20 61 6e 64 20 6f 75 74 70 75 74 73 20 61 72  s and outputs ar
2dee1 65 20 61 6c 77 61 79 73 20 74 79 70 65 20 44 4c  e always type DL
2dee2 5f 44 45 46 41 55 4c 54 2e 0a 2a 2f 0a 73 74 61  _DEFAULT..*/.sta
2dee3 74 69 63 20 76 6f 69 64 20 64 6f 63 4c 69 73 74  tic void docList
2dee4 55 6e 69 6f 6e 28 0a 20 20 63 6f 6e 73 74 20 63  Union(.  const c
2dee5 68 61 72 20 2a 70 4c 65 66 74 2c 20 69 6e 74 20  har *pLeft, int 
2dee6 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 63  nLeft,.  const c
2dee7 68 61 72 20 2a 70 52 69 67 68 74 2c 20 69 6e 74  har *pRight, int
2dee8 20 6e 52 69 67 68 74 2c 0a 20 20 44 61 74 61 42   nRight,.  DataB
2dee9 75 66 66 65 72 20 2a 70 4f 75 74 20 20 20 20 20  uffer *pOut     
2deea 20 2f 2a 20 57 72 69 74 65 20 74 68 65 20 63 6f   /* Write the co
2deeb 6d 62 69 6e 65 64 20 64 6f 63 6c 69 73 74 20 68  mbined doclist h
2deec 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65  ere */.){.  DLRe
2deed 61 64 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ader left, right
2deee 3b 0a 20 20 44 4c 57 72 69 74 65 72 20 77 72 69  ;.  DLWriter wri
2deef 74 65 72 3b 0a 0a 20 20 69 66 28 20 6e 4c 65 66  ter;..  if( nLef
2def0 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66 28 20  t==0 ){.    if( 
2def1 6e 52 69 67 68 74 21 3d 30 29 20 64 61 74 61 42  nRight!=0) dataB
2def2 75 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74  ufferAppend(pOut
2def3 2c 20 70 52 69 67 68 74 2c 20 6e 52 69 67 68 74  , pRight, nRight
2def4 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
2def5 20 7d 0a 20 20 69 66 28 20 6e 52 69 67 68 74 3d   }.  if( nRight=
2def6 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  =0 ){.    dataBu
2def7 66 66 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c  fferAppend(pOut,
2def8 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a   pLeft, nLeft);.
2def9 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2defa 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c 65 66 74  .  dlrInit(&left
2defb 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4c  , DL_DEFAULT, pL
2defc 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64  eft, nLeft);.  d
2defd 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44  lrInit(&right, D
2defe 4c 5f 44 45 46 41 55 4c 54 2c 20 70 52 69 67 68  L_DEFAULT, pRigh
2deff 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64 6c  t, nRight);.  dl
2df00 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20 44  wInit(&writer, D
2df01 4c 5f 44 45 46 41 55 4c 54 2c 20 70 4f 75 74 29  L_DEFAULT, pOut)
2df02 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
2df03 41 74 45 6e 64 28 26 6c 65 66 74 29 20 7c 7c 20  AtEnd(&left) || 
2df04 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74  !dlrAtEnd(&right
2df05 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
2df06 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b  AtEnd(&right) ){
2df07 0a 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26  .      dlwCopy(&
2df08 77 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a  writer, &left);.
2df09 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c        dlrStep(&l
2df0a 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20  eft);.    }else 
2df0b 69 66 28 20 64 6c 72 41 74 45 6e 64 28 26 6c 65  if( dlrAtEnd(&le
2df0c 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77  ft) ){.      dlw
2df0d 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 72  Copy(&writer, &r
2df0e 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  ight);.      dlr
2df0f 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
2df10 20 20 7d 65 6c 73 65 20 69 66 28 20 64 6c 72 44    }else if( dlrD
2df11 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72 44  ocid(&left)<dlrD
2df12 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b 0a  ocid(&right) ){.
2df13 20 20 20 20 20 20 64 6c 77 43 6f 70 79 28 26 77        dlwCopy(&w
2df14 72 69 74 65 72 2c 20 26 6c 65 66 74 29 3b 0a 20  riter, &left);. 
2df15 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
2df16 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69  ft);.    }else i
2df17 66 28 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  f( dlrDocid(&lef
2df18 74 29 3e 64 6c 72 44 6f 63 69 64 28 26 72 69 67  t)>dlrDocid(&rig
2df19 68 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 77  ht) ){.      dlw
2df1a 43 6f 70 79 28 26 77 72 69 74 65 72 2c 20 26 72  Copy(&writer, &r
2df1b 69 67 68 74 29 3b 0a 20 20 20 20 20 20 64 6c 72  ight);.      dlr
2df1c 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20  Step(&right);.  
2df1d 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 70    }else{.      p
2df1e 6f 73 4c 69 73 74 55 6e 69 6f 6e 28 26 6c 65 66  osListUnion(&lef
2df1f 74 2c 20 26 72 69 67 68 74 2c 20 26 77 72 69 74  t, &right, &writ
2df20 65 72 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  er);.      dlrSt
2df21 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20  ep(&left);.     
2df22 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74 29   dlrStep(&right)
2df23 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64  ;.    }.  }..  d
2df24 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29  lrDestroy(&left)
2df25 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26  ;.  dlrDestroy(&
2df26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73  right);.  dlwDes
2df27 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d  troy(&writer);.}
2df28 0a 0a 2f 2a 20 0a 2a 2a 20 54 68 69 73 20 66 75  ../* .** This fu
2df29 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 61  nction is used a
2df2a 73 20 70 61 72 74 20 6f 66 20 74 68 65 20 69 6d  s part of the im
2df2b 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20  plementation of 
2df2c 70 68 72 61 73 65 20 61 6e 64 0a 2a 2a 20 4e 45  phrase and.** NE
2df2d 41 52 20 6d 61 74 63 68 69 6e 67 2e 0a 2a 2a 0a  AR matching..**.
2df2e 2a 2a 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  ** pLeft and pRi
2df2f 67 68 74 20 61 72 65 20 44 4c 52 65 61 64 65 72  ght are DLReader
2df30 73 20 70 6f 73 69 74 69 6f 6e 65 64 20 74 6f 20  s positioned to 
2df31 74 68 65 20 73 61 6d 65 20 64 6f 63 69 64 20 69  the same docid i
2df32 6e 0a 2a 2a 20 6c 69 73 74 73 20 6f 66 20 74 79  n.** lists of ty
2df33 70 65 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 2e 20  pe DL_POSITION. 
2df34 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 77 72  This function wr
2df35 69 74 65 73 20 61 6e 20 65 6e 74 72 79 20 74 6f  ites an entry to
2df36 20 74 68 65 0a 2a 2a 20 44 4c 57 72 69 74 65 72   the.** DLWriter
2df37 20 70 4f 75 74 20 66 6f 72 20 65 61 63 68 20 70   pOut for each p
2df38 6f 73 69 74 69 6f 6e 20 69 6e 20 70 52 69 67 68  osition in pRigh
2df39 74 20 74 68 61 74 20 69 73 20 6c 65 73 73 20 74  t that is less t
2df3a 68 61 6e 0a 2a 2a 20 28 6e 4e 65 61 72 2b 31 29  han.** (nNear+1)
2df3b 20 67 72 65 61 74 65 72 20 28 62 75 74 20 6e 6f   greater (but no
2df3c 74 20 65 71 75 61 6c 20 74 6f 20 6f 72 20 73 6d  t equal to or sm
2df3d 61 6c 6c 65 72 29 20 74 68 61 6e 20 61 20 70 6f  aller) than a po
2df3e 73 69 74 69 6f 6e 20 0a 2a 2a 20 69 6e 20 70 4c  sition .** in pL
2df3f 65 66 74 2e 20 46 6f 72 20 65 78 61 6d 70 6c 65  eft. For example
2df40 2c 20 69 66 20 6e 4e 65 61 72 20 69 73 20 30 2c  , if nNear is 0,
2df41 20 61 6e 64 20 74 68 65 20 70 6f 73 69 74 69 6f   and the positio
2df42 6e 73 20 63 6f 6e 74 61 69 6e 65 64 0a 2a 2a 20  ns contained.** 
2df43 62 79 20 70 4c 65 66 74 20 61 6e 64 20 70 52 69  by pLeft and pRi
2df44 67 68 74 20 61 72 65 3a 0a 2a 2a 0a 2a 2a 20 20  ght are:.**.**  
2df45 20 20 70 4c 65 66 74 3a 20 20 35 20 31 30 20 31    pLeft:  5 10 1
2df46 35 20 32 30 0a 2a 2a 20 20 20 20 70 52 69 67 68  5 20.**    pRigh
2df47 74 3a 20 36 20 20 39 20 31 37 20 32 31 0a 2a 2a  t: 6  9 17 21.**
2df48 0a 2a 2a 20 74 68 65 6e 20 74 68 65 20 64 6f 63  .** then the doc
2df49 69 64 20 69 73 20 61 64 64 65 64 20 74 6f 20 70  id is added to p
2df4a 4f 75 74 2e 20 49 66 20 70 4f 75 74 20 69 73 20  Out. If pOut is 
2df4b 6f 66 20 74 79 70 65 20 44 4c 5f 50 4f 53 49 54  of type DL_POSIT
2df4c 49 4f 4e 53 2c 0a 2a 2a 20 74 68 65 6e 20 61 20  IONS,.** then a 
2df4d 70 6f 73 69 74 69 6f 6e 69 64 73 20 22 36 22 20  positionids "6" 
2df4e 61 6e 64 20 22 32 31 22 20 61 72 65 20 61 6c 73  and "21" are als
2df4f 6f 20 61 64 64 65 64 20 74 6f 20 70 4f 75 74 2e  o added to pOut.
2df50 0a 2a 2a 0a 2a 2a 20 49 66 20 62 6f 6f 6c 65 61  .**.** If boolea
2df51 6e 20 61 72 67 75 6d 65 6e 74 20 69 73 53 61 76  n argument isSav
2df52 65 4c 65 66 74 20 69 73 20 74 72 75 65 2c 20 74  eLeft is true, t
2df53 68 65 6e 20 70 6f 73 69 74 69 6f 6e 69 64 73 20  hen positionids 
2df54 61 72 65 20 63 6f 70 69 65 64 0a 2a 2a 20 66 72  are copied.** fr
2df55 6f 6d 20 70 4c 65 66 74 20 69 6e 73 74 65 61 64  om pLeft instead
2df56 20 6f 66 20 70 52 69 67 68 74 2e 20 49 6e 20 74   of pRight. In t
2df57 68 65 20 65 78 61 6d 70 6c 65 20 61 62 6f 76 65  he example above
2df58 2c 20 74 68 65 20 70 6f 73 69 74 69 6f 6e 73 20  , the positions 
2df59 22 35 22 0a 2a 2a 20 61 6e 64 20 22 32 30 22 20  "5".** and "20" 
2df5a 77 6f 75 6c 64 20 62 65 20 61 64 64 65 64 20 69  would be added i
2df5b 6e 73 74 65 61 64 20 6f 66 20 22 36 22 20 61 6e  nstead of "6" an
2df5c 64 20 22 32 31 22 2e 0a 2a 2f 0a 73 74 61 74 69  d "21"..*/.stati
2df5d 63 20 76 6f 69 64 20 70 6f 73 4c 69 73 74 50 68  c void posListPh
2df5e 72 61 73 65 4d 65 72 67 65 28 0a 20 20 44 4c 52  raseMerge(.  DLR
2df5f 65 61 64 65 72 20 2a 70 4c 65 66 74 2c 20 0a 20  eader *pLeft, . 
2df60 20 44 4c 52 65 61 64 65 72 20 2a 70 52 69 67 68   DLReader *pRigh
2df61 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72 2c 0a  t,.  int nNear,.
2df62 20 20 69 6e 74 20 69 73 53 61 76 65 4c 65 66 74    int isSaveLeft
2df63 2c 0a 20 20 44 4c 57 72 69 74 65 72 20 2a 70 4f  ,.  DLWriter *pO
2df64 75 74 0a 29 7b 0a 20 20 50 4c 52 65 61 64 65 72  ut.){.  PLReader
2df65 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20   left, right;.  
2df66 50 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  PLWriter writer;
2df67 0a 20 20 69 6e 74 20 6d 61 74 63 68 20 3d 20 30  .  int match = 0
2df68 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 64 6c 72  ;..  assert( dlr
2df69 44 6f 63 69 64 28 70 4c 65 66 74 29 3d 3d 64 6c  Docid(pLeft)==dl
2df6a 72 44 6f 63 69 64 28 70 52 69 67 68 74 29 20 29  rDocid(pRight) )
2df6b 3b 0a 20 20 61 73 73 65 72 74 28 20 70 4f 75 74  ;.  assert( pOut
2df6c 2d 3e 69 54 79 70 65 21 3d 44 4c 5f 50 4f 53 49  ->iType!=DL_POSI
2df6d 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 29 3b  TIONS_OFFSETS );
2df6e 0a 0a 20 20 70 6c 72 49 6e 69 74 28 26 6c 65 66  ..  plrInit(&lef
2df6f 74 2c 20 70 4c 65 66 74 29 3b 0a 20 20 70 6c 72  t, pLeft);.  plr
2df70 49 6e 69 74 28 26 72 69 67 68 74 2c 20 70 52 69  Init(&right, pRi
2df71 67 68 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20  ght);..  while( 
2df72 21 70 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29  !plrAtEnd(&left)
2df73 20 26 26 20 21 70 6c 72 41 74 45 6e 64 28 26 72   && !plrAtEnd(&r
2df74 69 67 68 74 29 20 29 7b 0a 20 20 20 20 69 66 28  ight) ){.    if(
2df75 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 6c 65 66 74   plrColumn(&left
2df76 29 3c 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67  )<plrColumn(&rig
2df77 68 74 29 20 29 7b 0a 20 20 20 20 20 20 70 6c 72  ht) ){.      plr
2df78 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20  Step(&left);.   
2df79 20 7d 65 6c 73 65 20 69 66 28 20 70 6c 72 43 6f   }else if( plrCo
2df7a 6c 75 6d 6e 28 26 6c 65 66 74 29 3e 70 6c 72 43  lumn(&left)>plrC
2df7b 6f 6c 75 6d 6e 28 26 72 69 67 68 74 29 20 29 7b  olumn(&right) ){
2df7c 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26  .      plrStep(&
2df7d 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  right);.    }els
2df7e 65 20 69 66 28 20 70 6c 72 50 6f 73 69 74 69 6f  e if( plrPositio
2df7f 6e 28 26 6c 65 66 74 29 3e 3d 70 6c 72 50 6f 73  n(&left)>=plrPos
2df80 69 74 69 6f 6e 28 26 72 69 67 68 74 29 20 29 7b  ition(&right) ){
2df81 0a 20 20 20 20 20 20 70 6c 72 53 74 65 70 28 26  .      plrStep(&
2df82 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65 6c 73  right);.    }els
2df83 65 7b 0a 20 20 20 20 20 20 69 66 28 20 28 70 6c  e{.      if( (pl
2df84 72 50 6f 73 69 74 69 6f 6e 28 26 72 69 67 68 74  rPosition(&right
2df85 29 2d 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 6c  )-plrPosition(&l
2df86 65 66 74 29 29 3c 3d 28 6e 4e 65 61 72 2b 31 29  eft))<=(nNear+1)
2df87 20 29 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20   ){.        if( 
2df88 21 6d 61 74 63 68 20 29 7b 0a 20 20 20 20 20 20  !match ){.      
2df89 20 20 20 20 70 6c 77 49 6e 69 74 28 26 77 72 69      plwInit(&wri
2df8a 74 65 72 2c 20 70 4f 75 74 2c 20 64 6c 72 44 6f  ter, pOut, dlrDo
2df8b 63 69 64 28 70 4c 65 66 74 29 29 3b 0a 20 20 20  cid(pLeft));.   
2df8c 20 20 20 20 20 20 20 6d 61 74 63 68 20 3d 20 31         match = 1
2df8d 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2df8e 20 20 20 20 69 66 28 20 21 69 73 53 61 76 65 4c      if( !isSaveL
2df8f 65 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20  eft ){.         
2df90 20 70 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   plwAdd(&writer,
2df91 20 70 6c 72 43 6f 6c 75 6d 6e 28 26 72 69 67 68   plrColumn(&righ
2df92 74 29 2c 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28  t), plrPosition(
2df93 26 72 69 67 68 74 29 2c 20 30 2c 20 30 29 3b 0a  &right), 0, 0);.
2df94 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
2df95 20 20 20 20 20 20 20 20 20 70 6c 77 41 64 64 28           plwAdd(
2df96 26 77 72 69 74 65 72 2c 20 70 6c 72 43 6f 6c 75  &writer, plrColu
2df97 6d 6e 28 26 6c 65 66 74 29 2c 20 70 6c 72 50 6f  mn(&left), plrPo
2df98 73 69 74 69 6f 6e 28 26 6c 65 66 74 29 2c 20 30  sition(&left), 0
2df99 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a  , 0);.        }.
2df9a 20 20 20 20 20 20 20 20 70 6c 72 53 74 65 70 28          plrStep(
2df9b 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20 20 7d  &right);.      }
2df9c 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 70 6c  else{.        pl
2df9d 72 53 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20  rStep(&left);.  
2df9e 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
2df9f 0a 20 20 69 66 28 20 6d 61 74 63 68 20 29 7b 0a  .  if( match ){.
2dfa0 20 20 20 20 70 6c 77 54 65 72 6d 69 6e 61 74 65      plwTerminate
2dfa1 28 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 70  (&writer);.    p
2dfa2 6c 77 44 65 73 74 72 6f 79 28 26 77 72 69 74 65  lwDestroy(&write
2dfa3 72 29 3b 0a 20 20 7d 0a 0a 20 20 70 6c 72 44 65  r);.  }..  plrDe
2dfa4 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20  stroy(&left);.  
2dfa5 70 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  plrDestroy(&righ
2dfa6 74 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  t);.}../*.** Com
2dfa7 70 61 72 65 20 74 68 65 20 76 61 6c 75 65 73 20  pare the values 
2dfa8 70 6f 69 6e 74 65 64 20 74 6f 20 62 79 20 74 68  pointed to by th
2dfa9 65 20 50 4c 52 65 61 64 65 72 73 20 70 61 73 73  e PLReaders pass
2dfaa 65 64 20 61 73 20 61 72 67 75 6d 65 6e 74 73 2e  ed as arguments.
2dfab 20 0a 2a 2a 20 52 65 74 75 72 6e 20 2d 31 20 69   .** Return -1 i
2dfac 66 20 74 68 65 20 76 61 6c 75 65 20 70 6f 69 6e  f the value poin
2dfad 74 65 64 20 74 6f 20 62 79 20 70 4c 65 66 74 20  ted to by pLeft 
2dfae 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 6c 65  is considered le
2dfaf 73 73 20 74 68 61 6e 0a 2a 2a 20 74 68 65 20 76  ss than.** the v
2dfb0 61 6c 75 65 20 70 6f 69 6e 74 65 64 20 74 6f 20  alue pointed to 
2dfb1 62 79 20 70 52 69 67 68 74 2c 20 2b 31 20 69 66  by pRight, +1 if
2dfb2 20 69 74 20 69 73 20 63 6f 6e 73 69 64 65 72 65   it is considere
2dfb3 64 20 67 72 65 61 74 65 72 0a 2a 2a 20 74 68 61  d greater.** tha
2dfb4 6e 20 69 74 2c 20 6f 72 20 30 20 69 66 20 69 74  n it, or 0 if it
2dfb5 20 69 73 20 65 71 75 61 6c 2e 20 69 2e 65 2e 0a   is equal. i.e..
2dfb6 2a 2a 0a 2a 2a 20 20 20 20 20 28 2a 70 4c 65 66  **.**     (*pLef
2dfb7 74 20 2d 20 2a 70 52 69 67 68 74 29 0a 2a 2a 0a  t - *pRight).**.
2dfb8 2a 2a 20 41 20 50 4c 52 65 61 64 65 72 20 74 68  ** A PLReader th
2dfb9 61 74 20 69 73 20 69 6e 20 74 68 65 20 45 4f 46  at is in the EOF
2dfba 20 63 6f 6e 64 69 74 69 6f 6e 20 69 73 20 63 6f   condition is co
2dfbb 6e 73 69 64 65 72 65 64 20 67 72 65 61 74 65 72  nsidered greater
2dfbc 20 74 68 61 6e 0a 2a 2a 20 61 6e 79 20 6f 74 68   than.** any oth
2dfbd 65 72 2e 20 49 66 20 6e 65 69 74 68 65 72 20 61  er. If neither a
2dfbe 72 67 75 6d 65 6e 74 20 69 73 20 69 6e 20 45 4f  rgument is in EO
2dfbf 46 20 73 74 61 74 65 2c 20 74 68 65 20 72 65 74  F state, the ret
2dfc0 75 72 6e 20 76 61 6c 75 65 20 6f 66 0a 2a 2a 20  urn value of.** 
2dfc1 70 6c 72 43 6f 6c 75 6d 6e 28 29 20 69 73 20 75  plrColumn() is u
2dfc2 73 65 64 2e 20 49 66 20 74 68 65 20 70 6c 72 43  sed. If the plrC
2dfc3 6f 6c 75 6d 6e 28 29 20 76 61 6c 75 65 73 20 61  olumn() values a
2dfc4 72 65 20 65 71 75 61 6c 2c 20 74 68 65 0a 2a 2a  re equal, the.**
2dfc5 20 63 6f 6d 70 61 72 69 73 6f 6e 20 69 73 20 6f   comparison is o
2dfc6 6e 20 74 68 65 20 62 61 73 69 73 20 6f 66 20 70  n the basis of p
2dfc7 6c 72 50 6f 73 69 74 69 6f 6e 28 29 2e 0a 2a 2f  lrPosition()..*/
2dfc8 0a 73 74 61 74 69 63 20 69 6e 74 20 70 6c 72 43  .static int plrC
2dfc9 6f 6d 70 61 72 65 28 50 4c 52 65 61 64 65 72 20  ompare(PLReader 
2dfca 2a 70 4c 65 66 74 2c 20 50 4c 52 65 61 64 65 72  *pLeft, PLReader
2dfcb 20 2a 70 52 69 67 68 74 29 7b 0a 20 20 61 73 73   *pRight){.  ass
2dfcc 65 72 74 28 21 70 6c 72 41 74 45 6e 64 28 70 4c  ert(!plrAtEnd(pL
2dfcd 65 66 74 29 20 7c 7c 20 21 70 6c 72 41 74 45 6e  eft) || !plrAtEn
2dfce 64 28 70 52 69 67 68 74 29 29 3b 0a 0a 20 20 69  d(pRight));..  i
2dfcf 66 28 20 70 6c 72 41 74 45 6e 64 28 70 52 69 67  f( plrAtEnd(pRig
2dfd0 68 74 29 20 7c 7c 20 70 6c 72 41 74 45 6e 64 28  ht) || plrAtEnd(
2dfd1 70 4c 65 66 74 29 20 29 7b 0a 20 20 20 20 72 65  pLeft) ){.    re
2dfd2 74 75 72 6e 20 28 70 6c 72 41 74 45 6e 64 28 70  turn (plrAtEnd(p
2dfd3 52 69 67 68 74 29 20 3f 20 2d 31 20 3a 20 31 29  Right) ? -1 : 1)
2dfd4 3b 0a 20 20 7d 0a 20 20 69 66 28 20 70 6c 72 43  ;.  }.  if( plrC
2dfd5 6f 6c 75 6d 6e 28 70 4c 65 66 74 29 21 3d 70 6c  olumn(pLeft)!=pl
2dfd6 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68 74 29 20  rColumn(pRight) 
2dfd7 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28 28  ){.    return ((
2dfd8 70 6c 72 43 6f 6c 75 6d 6e 28 70 4c 65 66 74 29  plrColumn(pLeft)
2dfd9 3c 70 6c 72 43 6f 6c 75 6d 6e 28 70 52 69 67 68  <plrColumn(pRigh
2dfda 74 29 29 20 3f 20 2d 31 20 3a 20 31 29 3b 0a 20  t)) ? -1 : 1);. 
2dfdb 20 7d 0a 20 20 69 66 28 20 70 6c 72 50 6f 73 69   }.  if( plrPosi
2dfdc 74 69 6f 6e 28 70 4c 65 66 74 29 21 3d 70 6c 72  tion(pLeft)!=plr
2dfdd 50 6f 73 69 74 69 6f 6e 28 70 52 69 67 68 74 29  Position(pRight)
2dfde 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 28   ){.    return (
2dfdf 28 70 6c 72 50 6f 73 69 74 69 6f 6e 28 70 4c 65  (plrPosition(pLe
2dfe0 66 74 29 3c 70 6c 72 50 6f 73 69 74 69 6f 6e 28  ft)<plrPosition(
2dfe1 70 52 69 67 68 74 29 29 20 3f 20 2d 31 20 3a 20  pRight)) ? -1 : 
2dfe2 31 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  1);.  }.  return
2dfe3 20 30 3b 0a 7d 0a 0a 2f 2a 20 57 65 20 68 61 76   0;.}../* We hav
2dfe4 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 77  e two doclists w
2dfe5 69 74 68 20 70 6f 73 69 74 69 6f 6e 73 3a 20 20  ith positions:  
2dfe6 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
2dfe7 2e 20 44 65 70 65 6e 64 69 6e 67 0a 2a 2a 20 6f  . Depending.** o
2dfe8 6e 20 74 68 65 20 76 61 6c 75 65 20 6f 66 20 74  n the value of t
2dfe9 68 65 20 6e 4e 65 61 72 20 70 61 72 61 6d 65 74  he nNear paramet
2dfea 65 72 2c 20 70 65 72 66 6f 72 6d 20 65 69 74 68  er, perform eith
2dfeb 65 72 20 61 20 70 68 72 61 73 65 0a 2a 2a 20 69  er a phrase.** i
2dfec 6e 74 65 72 73 65 63 74 69 6f 6e 20 28 69 66 20  ntersection (if 
2dfed 6e 4e 65 61 72 3d 3d 30 29 20 6f 72 20 61 20 4e  nNear==0) or a N
2dfee 45 41 52 20 69 6e 74 65 72 73 65 63 74 69 6f 6e  EAR intersection
2dfef 20 28 69 66 20 6e 4e 65 61 72 3e 30 29 0a 2a 2a   (if nNear>0).**
2dff0 20 61 6e 64 20 77 72 69 74 65 20 74 68 65 20 72   and write the r
2dff1 65 73 75 6c 74 73 20 69 6e 74 6f 20 70 4f 75 74  esults into pOut
2dff2 2e 0a 2a 2a 0a 2a 2a 20 41 20 70 68 72 61 73 65  ..**.** A phrase
2dff3 20 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65   intersection me
2dff4 61 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63  ans that two doc
2dff5 75 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63  uments only matc
2dff6 68 0a 2a 2a 20 69 66 20 70 4c 65 66 74 2e 69 50  h.** if pLeft.iP
2dff7 6f 73 2b 31 3d 3d 70 52 69 67 68 74 2e 69 50 6f  os+1==pRight.iPo
2dff8 73 2e 0a 2a 2a 0a 2a 2a 20 41 20 4e 45 41 52 20  s..**.** A NEAR 
2dff9 69 6e 74 65 72 73 65 63 74 69 6f 6e 20 6d 65 61  intersection mea
2dffa 6e 73 20 74 68 61 74 20 74 77 6f 20 64 6f 63 75  ns that two docu
2dffb 6d 65 6e 74 73 20 6f 6e 6c 79 20 6d 61 74 63 68  ments only match
2dffc 20 69 66 20 0a 2a 2a 20 28 61 62 73 28 70 4c 65   if .** (abs(pLe
2dffd 66 74 2e 69 50 6f 73 2d 70 52 69 67 68 74 2e 69  ft.iPos-pRight.i
2dffe 50 6f 73 29 3c 6e 4e 65 61 72 29 2e 0a 2a 2a 0a  Pos)<nNear)..**.
2dfff 2a 2a 20 49 66 20 61 20 4e 45 41 52 20 69 6e 74  ** If a NEAR int
2e000 65 72 73 65 63 74 69 6f 6e 20 69 73 20 72 65 71  ersection is req
2e001 75 65 73 74 65 64 2c 20 74 68 65 6e 20 74 68 65  uested, then the
2e002 20 6e 50 68 72 61 73 65 20 61 72 67 75 6d 65 6e   nPhrase argumen
2e003 74 20 73 68 6f 75 6c 64 0a 2a 2a 20 62 65 20 70  t should.** be p
2e004 61 73 73 65 64 20 74 68 65 20 6e 75 6d 62 65 72  assed the number
2e005 20 6f 66 20 74 6f 6b 65 6e 73 20 69 6e 20 74 68   of tokens in th
2e006 65 20 74 77 6f 20 6f 70 65 72 61 6e 64 73 20 74  e two operands t
2e007 6f 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72 61  o the NEAR opera
2e008 74 6f 72 0a 2a 2a 20 63 6f 6d 62 69 6e 65 64 2e  tor.** combined.
2e009 20 46 6f 72 20 65 78 61 6d 70 6c 65 3a 0a 2a 2a   For example:.**
2e00a 0a 2a 2a 20 20 20 20 20 20 20 51 75 65 72 79 20  .**       Query 
2e00b 73 79 6e 74 61 78 20 20 20 20 20 20 20 20 20 20  syntax          
2e00c 20 20 20 20 20 6e 50 68 72 61 73 65 0a 2a 2a 20       nPhrase.** 
2e00d 20 20 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d       -----------
2e00e 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
2e00f 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20 20 20 20  ---------.**    
2e010 20 20 20 22 41 20 42 20 43 22 20 4e 45 41 52 20     "A B C" NEAR 
2e011 22 44 20 45 22 20 20 20 20 20 20 20 20 20 35 0a  "D E"         5.
2e012 2a 2a 20 20 20 20 20 20 20 41 20 4e 45 41 52 20  **       A NEAR 
2e013 42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  B               
2e014 20 20 20 20 32 0a 2a 2a 0a 2a 2a 20 69 54 79 70      2.**.** iTyp
2e015 65 20 63 6f 6e 74 72 6f 6c 73 20 74 68 65 20 74  e controls the t
2e016 79 70 65 20 6f 66 20 64 61 74 61 20 77 72 69 74  ype of data writ
2e017 74 65 6e 20 74 6f 20 70 4f 75 74 2e 20 20 49 66  ten to pOut.  If
2e018 20 69 54 79 70 65 20 69 73 0a 2a 2a 20 44 4c 5f   iType is.** DL_
2e019 50 4f 53 49 54 49 4f 4e 53 2c 20 74 68 65 20 70  POSITIONS, the p
2e01a 6f 73 69 74 69 6f 6e 73 20 61 72 65 20 74 68 6f  ositions are tho
2e01b 73 65 20 66 72 6f 6d 20 70 52 69 67 68 74 2e 0a  se from pRight..
2e01c 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64  */.static void d
2e01d 6f 63 4c 69 73 74 50 68 72 61 73 65 4d 65 72 67  ocListPhraseMerg
2e01e 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e(.  const char 
2e01f 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66  *pLeft, int nLef
2e020 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t,.  const char 
2e021 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69  *pRight, int nRi
2e022 67 68 74 2c 0a 20 20 69 6e 74 20 6e 4e 65 61 72  ght,.  int nNear
2e023 2c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20  ,            /* 
2e024 30 20 66 6f 72 20 61 20 70 68 72 61 73 65 20 6d  0 for a phrase m
2e025 65 72 67 65 2c 20 6e 6f 6e 2d 7a 65 72 6f 20 66  erge, non-zero f
2e026 6f 72 20 61 20 4e 45 41 52 20 6d 65 72 67 65 20  or a NEAR merge 
2e027 2a 2f 0a 20 20 69 6e 74 20 6e 50 68 72 61 73 65  */.  int nPhrase
2e028 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75  ,          /* Nu
2e029 6d 62 65 72 20 6f 66 20 74 6f 6b 65 6e 73 20 69  mber of tokens i
2e02a 6e 20 6c 65 66 74 2b 72 69 67 68 74 20 6f 70 65  n left+right ope
2e02b 72 61 6e 64 73 20 74 6f 20 4e 45 41 52 20 2a 2f  rands to NEAR */
2e02c 0a 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20 69  .  DocListType i
2e02d 54 79 70 65 2c 20 20 20 20 2f 2a 20 54 79 70 65  Type,    /* Type
2e02e 20 6f 66 20 64 6f 63 6c 69 73 74 20 74 6f 20 77   of doclist to w
2e02f 72 69 74 65 20 74 6f 20 70 4f 75 74 20 2a 2f 0a  rite to pOut */.
2e030 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f    DataBuffer *pO
2e031 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ut      /* Write
2e032 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f   the combined do
2e033 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b  clist here */.){
2e034 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74  .  DLReader left
2e035 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69  , right;.  DLWri
2e036 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69  ter writer;..  i
2e037 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e  f( nLeft==0 || n
2e038 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72  Right==0 ) retur
2e039 6e 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 69 54  n;..  assert( iT
2e03a 79 70 65 21 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e  ype!=DL_POSITION
2e03b 53 5f 4f 46 46 53 45 54 53 20 29 3b 0a 0a 20 20  S_OFFSETS );..  
2e03c 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44  dlrInit(&left, D
2e03d 4c 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 4c 65  L_POSITIONS, pLe
2e03e 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c  ft, nLeft);.  dl
2e03f 72 49 6e 69 74 28 26 72 69 67 68 74 2c 20 44 4c  rInit(&right, DL
2e040 5f 50 4f 53 49 54 49 4f 4e 53 2c 20 70 52 69 67  _POSITIONS, pRig
2e041 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64  ht, nRight);.  d
2e042 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20  lwInit(&writer, 
2e043 69 54 79 70 65 2c 20 70 4f 75 74 29 3b 0a 0a 20  iType, pOut);.. 
2e044 20 77 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e   while( !dlrAtEn
2e045 64 28 26 6c 65 66 74 29 20 26 26 20 21 64 6c 72  d(&left) && !dlr
2e046 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b  AtEnd(&right) ){
2e047 0a 20 20 20 20 69 66 28 20 64 6c 72 44 6f 63 69  .    if( dlrDoci
2e048 64 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69  d(&left)<dlrDoci
2e049 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20  d(&right) ){.   
2e04a 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65 66 74     dlrStep(&left
2e04b 29 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  );.    }else if(
2e04c 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74   dlrDocid(&right
2e04d 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  )<dlrDocid(&left
2e04e 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53 74  ) ){.      dlrSt
2e04f 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
2e050 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28  }else{.      if(
2e051 20 6e 4e 65 61 72 3d 3d 30 20 29 7b 0a 20 20 20   nNear==0 ){.   
2e052 20 20 20 20 20 70 6f 73 4c 69 73 74 50 68 72 61       posListPhra
2e053 73 65 4d 65 72 67 65 28 26 6c 65 66 74 2c 20 26  seMerge(&left, &
2e054 72 69 67 68 74 2c 20 30 2c 20 30 2c 20 26 77 72  right, 0, 0, &wr
2e055 69 74 65 72 29 3b 0a 20 20 20 20 20 20 7d 65 6c  iter);.      }el
2e056 73 65 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 54  se{.        /* T
2e057 68 69 73 20 63 61 73 65 20 6f 63 63 75 72 73 20  his case occurs 
2e058 77 68 65 6e 20 74 77 6f 20 74 65 72 6d 73 20 28  when two terms (
2e059 73 69 6d 70 6c 65 20 74 65 72 6d 73 20 6f 72 20  simple terms or 
2e05a 70 68 72 61 73 65 73 29 20 61 72 65 0a 20 20 20  phrases) are.   
2e05b 20 20 20 20 20 20 2a 20 63 6f 6e 6e 65 63 74 65        * connecte
2e05c 64 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72  d by a NEAR oper
2e05d 61 74 6f 72 2c 20 73 70 61 6e 20 28 6e 4e 65 61  ator, span (nNea
2e05e 72 2b 31 29 2e 20 69 2e 65 2e 0a 20 20 20 20 20  r+1). i.e..     
2e05f 20 20 20 20 2a 0a 20 20 20 20 20 20 20 20 20 2a      *.         *
2e060 20 20 20 20 20 27 22 74 65 72 72 69 62 6c 65 20       '"terrible 
2e061 63 6f 6d 70 61 6e 79 22 20 4e 45 41 52 20 77 69  company" NEAR wi
2e062 64 67 65 74 27 0a 20 20 20 20 20 20 20 20 20 2a  dget'.         *
2e063 2f 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75  /.        DataBu
2e064 66 66 65 72 20 6f 6e 65 20 3d 20 7b 30 2c 20 30  ffer one = {0, 0
2e065 2c 20 30 7d 3b 0a 20 20 20 20 20 20 20 20 44 61  , 0};.        Da
2e066 74 61 42 75 66 66 65 72 20 74 77 6f 20 3d 20 7b  taBuffer two = {
2e067 30 2c 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20  0, 0, 0};..     
2e068 20 20 20 44 4c 57 72 69 74 65 72 20 64 6c 77 72     DLWriter dlwr
2e069 69 74 65 72 32 3b 0a 20 20 20 20 20 20 20 20 44  iter2;.        D
2e06a 4c 52 65 61 64 65 72 20 64 72 31 20 3d 20 7b 30  LReader dr1 = {0
2e06b 2c 20 30 2c 20 30 2c 20 30 2c 20 30 7d 3b 20 0a  , 0, 0, 0, 0}; .
2e06c 20 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72          DLReader
2e06d 20 64 72 32 20 3d 20 7b 30 2c 20 30 2c 20 30 2c   dr2 = {0, 0, 0,
2e06e 20 30 2c 20 30 7d 3b 0a 0a 20 20 20 20 20 20 20   0, 0};..       
2e06f 20 64 6c 77 49 6e 69 74 28 26 64 6c 77 72 69 74   dlwInit(&dlwrit
2e070 65 72 32 2c 20 69 54 79 70 65 2c 20 26 6f 6e 65  er2, iType, &one
2e071 29 3b 0a 20 20 20 20 20 20 20 20 70 6f 73 4c 69  );.        posLi
2e072 73 74 50 68 72 61 73 65 4d 65 72 67 65 28 26 72  stPhraseMerge(&r
2e073 69 67 68 74 2c 20 26 6c 65 66 74 2c 20 6e 4e 65  ight, &left, nNe
2e074 61 72 2d 33 2b 6e 50 68 72 61 73 65 2c 20 31 2c  ar-3+nPhrase, 1,
2e075 20 26 64 6c 77 72 69 74 65 72 32 29 3b 0a 20 20   &dlwriter2);.  
2e076 20 20 20 20 20 20 64 6c 77 49 6e 69 74 28 26 64        dlwInit(&d
2e077 6c 77 72 69 74 65 72 32 2c 20 69 54 79 70 65 2c  lwriter2, iType,
2e078 20 26 74 77 6f 29 3b 0a 20 20 20 20 20 20 20 20   &two);.        
2e079 70 6f 73 4c 69 73 74 50 68 72 61 73 65 4d 65 72  posListPhraseMer
2e07a 67 65 28 26 6c 65 66 74 2c 20 26 72 69 67 68 74  ge(&left, &right
2e07b 2c 20 6e 4e 65 61 72 2d 31 2c 20 30 2c 20 26 64  , nNear-1, 0, &d
2e07c 6c 77 72 69 74 65 72 32 29 3b 0a 0a 20 20 20 20  lwriter2);..    
2e07d 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61 74      if( one.nDat
2e07e 61 29 20 64 6c 72 49 6e 69 74 28 26 64 72 31 2c  a) dlrInit(&dr1,
2e07f 20 69 54 79 70 65 2c 20 6f 6e 65 2e 70 44 61 74   iType, one.pDat
2e080 61 2c 20 6f 6e 65 2e 6e 44 61 74 61 29 3b 0a 20  a, one.nData);. 
2e081 20 20 20 20 20 20 20 69 66 28 20 74 77 6f 2e 6e         if( two.n
2e082 44 61 74 61 29 20 64 6c 72 49 6e 69 74 28 26 64  Data) dlrInit(&d
2e083 72 32 2c 20 69 54 79 70 65 2c 20 74 77 6f 2e 70  r2, iType, two.p
2e084 44 61 74 61 2c 20 74 77 6f 2e 6e 44 61 74 61 29  Data, two.nData)
2e085 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 28 20 21  ;..        if( !
2e086 64 6c 72 41 74 45 6e 64 28 26 64 72 31 29 20 7c  dlrAtEnd(&dr1) |
2e087 7c 20 21 64 6c 72 41 74 45 6e 64 28 26 64 72 32  | !dlrAtEnd(&dr2
2e088 29 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 50  ) ){.          P
2e089 4c 52 65 61 64 65 72 20 70 72 31 20 3d 20 7b 30  LReader pr1 = {0
2e08a 7d 3b 0a 20 20 20 20 20 20 20 20 20 20 50 4c 52  };.          PLR
2e08b 65 61 64 65 72 20 70 72 32 20 3d 20 7b 30 7d 3b  eader pr2 = {0};
2e08c 0a 0a 20 20 20 20 20 20 20 20 20 20 50 4c 57 72  ..          PLWr
2e08d 69 74 65 72 20 70 6c 77 72 69 74 65 72 3b 0a 20  iter plwriter;. 
2e08e 20 20 20 20 20 20 20 20 20 70 6c 77 49 6e 69 74           plwInit
2e08f 28 26 70 6c 77 72 69 74 65 72 2c 20 26 77 72 69  (&plwriter, &wri
2e090 74 65 72 2c 20 64 6c 72 44 6f 63 69 64 28 64 6c  ter, dlrDocid(dl
2e091 72 41 74 45 6e 64 28 26 64 72 31 29 3f 26 64 72  rAtEnd(&dr1)?&dr
2e092 32 3a 26 64 72 31 29 29 3b 0a 0a 20 20 20 20 20  2:&dr1));..     
2e093 20 20 20 20 20 69 66 28 20 6f 6e 65 2e 6e 44 61       if( one.nDa
2e094 74 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72  ta ) plrInit(&pr
2e095 31 2c 20 26 64 72 31 29 3b 0a 20 20 20 20 20 20  1, &dr1);.      
2e096 20 20 20 20 69 66 28 20 74 77 6f 2e 6e 44 61 74      if( two.nDat
2e097 61 20 29 20 70 6c 72 49 6e 69 74 28 26 70 72 32  a ) plrInit(&pr2
2e098 2c 20 26 64 72 32 29 3b 0a 20 20 20 20 20 20 20  , &dr2);.       
2e099 20 20 20 77 68 69 6c 65 28 20 21 70 6c 72 41 74     while( !plrAt
2e09a 45 6e 64 28 26 70 72 31 29 20 7c 7c 20 21 70 6c  End(&pr1) || !pl
2e09b 72 41 74 45 6e 64 28 26 70 72 32 29 20 29 7b 0a  rAtEnd(&pr2) ){.
2e09c 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
2e09d 69 43 6f 6d 70 61 72 65 20 3d 20 70 6c 72 43 6f  iCompare = plrCo
2e09e 6d 70 61 72 65 28 26 70 72 31 2c 20 26 70 72 32  mpare(&pr1, &pr2
2e09f 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73  );.            s
2e0a0 77 69 74 63 68 28 20 69 43 6f 6d 70 61 72 65 20  witch( iCompare 
2e0a1 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ){.             
2e0a2 20 63 61 73 65 20 2d 31 3a 0a 20 20 20 20 20 20   case -1:.      
2e0a3 20 20 20 20 20 20 20 20 20 20 70 6c 77 43 6f 70            plwCop
2e0a4 79 28 26 70 6c 77 72 69 74 65 72 2c 20 26 70 72  y(&plwriter, &pr
2e0a5 31 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20  1);.            
2e0a6 20 20 20 20 70 6c 72 53 74 65 70 28 26 70 72 31      plrStep(&pr1
2e0a7 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  );.             
2e0a8 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20     break;.      
2e0a9 20 20 20 20 20 20 20 20 63 61 73 65 20 31 3a 0a          case 1:.
2e0aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e0ab 70 6c 77 43 6f 70 79 28 26 70 6c 77 72 69 74 65  plwCopy(&plwrite
2e0ac 72 2c 20 26 70 72 32 29 3b 0a 20 20 20 20 20 20  r, &pr2);.      
2e0ad 20 20 20 20 20 20 20 20 20 20 70 6c 72 53 74 65            plrSte
2e0ae 70 28 26 70 72 32 29 3b 0a 20 20 20 20 20 20 20  p(&pr2);.       
2e0af 20 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a           break;.
2e0b0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 61                ca
2e0b1 73 65 20 30 3a 0a 20 20 20 20 20 20 20 20 20 20  se 0:.          
2e0b2 20 20 20 20 20 20 70 6c 77 43 6f 70 79 28 26 70        plwCopy(&p
2e0b3 6c 77 72 69 74 65 72 2c 20 26 70 72 31 29 3b 0a  lwriter, &pr1);.
2e0b4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e0b5 70 6c 72 53 74 65 70 28 26 70 72 31 29 3b 0a 20  plrStep(&pr1);. 
2e0b6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70                 p
2e0b7 6c 72 53 74 65 70 28 26 70 72 32 29 3b 0a 20 20  lrStep(&pr2);.  
2e0b8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 72                br
2e0b9 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 20 20  eak;.           
2e0ba 20 7d 0a 20 20 20 20 20 20 20 20 20 20 7d 0a 20   }.          }. 
2e0bb 20 20 20 20 20 20 20 20 20 70 6c 77 54 65 72 6d           plwTerm
2e0bc 69 6e 61 74 65 28 26 70 6c 77 72 69 74 65 72 29  inate(&plwriter)
2e0bd 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e0be 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44 65      dataBufferDe
2e0bf 73 74 72 6f 79 28 26 6f 6e 65 29 3b 0a 20 20 20  stroy(&one);.   
2e0c0 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44       dataBufferD
2e0c1 65 73 74 72 6f 79 28 26 74 77 6f 29 3b 0a 20 20  estroy(&two);.  
2e0c2 20 20 20 20 7d 0a 20 20 20 20 20 20 64 6c 72 53      }.      dlrS
2e0c3 74 65 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  tep(&left);.    
2e0c4 20 20 64 6c 72 53 74 65 70 28 26 72 69 67 68 74    dlrStep(&right
2e0c5 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
2e0c6 64 6c 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  dlrDestroy(&left
2e0c7 29 3b 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28  );.  dlrDestroy(
2e0c8 26 72 69 67 68 74 29 3b 0a 20 20 64 6c 77 44 65  &right);.  dlwDe
2e0c9 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
2e0ca 7d 0a 0a 2f 2a 20 57 65 20 68 61 76 65 20 74 77  }../* We have tw
2e0cb 6f 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c  o DL_DOCIDS docl
2e0cc 69 73 74 73 3a 20 20 70 4c 65 66 74 20 61 6e 64  ists:  pLeft and
2e0cd 20 70 52 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74   pRight..** Writ
2e0ce 65 20 74 68 65 20 69 6e 74 65 72 73 65 63 74 69  e the intersecti
2e0cf 6f 6e 20 6f 66 20 74 68 65 73 65 20 74 77 6f 20  on of these two 
2e0d0 64 6f 63 6c 69 73 74 73 20 69 6e 74 6f 20 70 4f  doclists into pO
2e0d1 75 74 20 61 73 20 61 0a 2a 2a 20 44 4c 5f 44 4f  ut as a.** DL_DO
2e0d2 43 49 44 53 20 64 6f 63 6c 69 73 74 2e 0a 2a 2f  CIDS doclist..*/
2e0d3 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63  .static void doc
2e0d4 4c 69 73 74 41 6e 64 4d 65 72 67 65 28 0a 20 20  ListAndMerge(.  
2e0d5 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c 65 66  const char *pLef
2e0d6 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a 20 20  t, int nLeft,.  
2e0d7 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 69 67  const char *pRig
2e0d8 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74 2c 0a  ht, int nRight,.
2e0d9 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 70 4f    DataBuffer *pO
2e0da 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69 74 65  ut      /* Write
2e0db 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20 64 6f   the combined do
2e0dc 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a 29 7b  clist here */.){
2e0dd 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65 66 74  .  DLReader left
2e0de 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57 72 69  , right;.  DLWri
2e0df 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20 20 69  ter writer;..  i
2e0e0 66 28 20 6e 4c 65 66 74 3d 3d 30 20 7c 7c 20 6e  f( nLeft==0 || n
2e0e1 52 69 67 68 74 3d 3d 30 20 29 20 72 65 74 75 72  Right==0 ) retur
2e0e2 6e 3b 0a 0a 20 20 64 6c 72 49 6e 69 74 28 26 6c  n;..  dlrInit(&l
2e0e3 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20  eft, DL_DOCIDS, 
2e0e4 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20  pLeft, nLeft);. 
2e0e5 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68 74 2c   dlrInit(&right,
2e0e6 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52 69 67   DL_DOCIDS, pRig
2e0e7 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20 20 64  ht, nRight);.  d
2e0e8 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72 2c 20  lwInit(&writer, 
2e0e9 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75 74 29  DL_DOCIDS, pOut)
2e0ea 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64 6c 72  ;..  while( !dlr
2e0eb 41 74 45 6e 64 28 26 6c 65 66 74 29 20 26 26 20  AtEnd(&left) && 
2e0ec 21 64 6c 72 41 74 45 6e 64 28 26 72 69 67 68 74  !dlrAtEnd(&right
2e0ed 29 20 29 7b 0a 20 20 20 20 69 66 28 20 64 6c 72  ) ){.    if( dlr
2e0ee 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64 6c 72  Docid(&left)<dlr
2e0ef 44 6f 63 69 64 28 26 72 69 67 68 74 29 20 29 7b  Docid(&right) ){
2e0f0 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70 28 26  .      dlrStep(&
2e0f1 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65  left);.    }else
2e0f2 20 69 66 28 20 64 6c 72 44 6f 63 69 64 28 26 72   if( dlrDocid(&r
2e0f3 69 67 68 74 29 3c 64 6c 72 44 6f 63 69 64 28 26  ight)<dlrDocid(&
2e0f4 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20 64  left) ){.      d
2e0f5 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
2e0f6 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
2e0f7 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
2e0f8 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
2e0f9 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
2e0fa 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 64  (&left);.      d
2e0fb 6c 72 53 74 65 70 28 26 72 69 67 68 74 29 3b 0a  lrStep(&right);.
2e0fc 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72      }.  }..  dlr
2e0fd 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a  Destroy(&left);.
2e0fe 20 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69    dlrDestroy(&ri
2e0ff 67 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72  ght);.  dlwDestr
2e100 6f 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a  oy(&writer);.}..
2e101 2f 2a 20 57 65 20 68 61 76 65 20 74 77 6f 20 44  /* We have two D
2e102 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69 73 74  L_DOCIDS doclist
2e103 73 3a 20 20 70 4c 65 66 74 20 61 6e 64 20 70 52  s:  pLeft and pR
2e104 69 67 68 74 2e 0a 2a 2a 20 57 72 69 74 65 20 74  ight..** Write t
2e105 68 65 20 75 6e 69 6f 6e 20 6f 66 20 74 68 65 73  he union of thes
2e106 65 20 74 77 6f 20 64 6f 63 6c 69 73 74 73 20 69  e two doclists i
2e107 6e 74 6f 20 70 4f 75 74 20 61 73 20 61 0a 2a 2a  nto pOut as a.**
2e108 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f 63 6c 69   DL_DOCIDS docli
2e109 73 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  st..*/.static vo
2e10a 69 64 20 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67  id docListOrMerg
2e10b 65 28 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e(.  const char 
2e10c 2a 70 4c 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66  *pLeft, int nLef
2e10d 74 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t,.  const char 
2e10e 2a 70 52 69 67 68 74 2c 20 69 6e 74 20 6e 52 69  *pRight, int nRi
2e10f 67 68 74 2c 0a 20 20 44 61 74 61 42 75 66 66 65  ght,.  DataBuffe
2e110 72 20 2a 70 4f 75 74 20 20 20 20 20 20 2f 2a 20  r *pOut      /* 
2e111 57 72 69 74 65 20 74 68 65 20 63 6f 6d 62 69 6e  Write the combin
2e112 65 64 20 64 6f 63 6c 69 73 74 20 68 65 72 65 20  ed doclist here 
2e113 2a 2f 0a 29 7b 0a 20 20 44 4c 52 65 61 64 65 72  */.){.  DLReader
2e114 20 6c 65 66 74 2c 20 72 69 67 68 74 3b 0a 20 20   left, right;.  
2e115 44 4c 57 72 69 74 65 72 20 77 72 69 74 65 72 3b  DLWriter writer;
2e116 0a 0a 20 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30  ..  if( nLeft==0
2e117 20 29 7b 0a 20 20 20 20 69 66 28 20 6e 52 69 67   ){.    if( nRig
2e118 68 74 21 3d 30 20 29 20 64 61 74 61 42 75 66 66  ht!=0 ) dataBuff
2e119 65 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70  erAppend(pOut, p
2e11a 52 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a  Right, nRight);.
2e11b 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a      return;.  }.
2e11c 20 20 69 66 28 20 6e 52 69 67 68 74 3d 3d 30 20    if( nRight==0 
2e11d 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ){.    dataBuffe
2e11e 72 41 70 70 65 6e 64 28 70 4f 75 74 2c 20 70 4c  rAppend(pOut, pL
2e11f 65 66 74 2c 20 6e 4c 65 66 74 29 3b 0a 20 20 20  eft, nLeft);.   
2e120 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20   return;.  }..  
2e121 64 6c 72 49 6e 69 74 28 26 6c 65 66 74 2c 20 44  dlrInit(&left, D
2e122 4c 5f 44 4f 43 49 44 53 2c 20 70 4c 65 66 74 2c  L_DOCIDS, pLeft,
2e123 20 6e 4c 65 66 74 29 3b 0a 20 20 64 6c 72 49 6e   nLeft);.  dlrIn
2e124 69 74 28 26 72 69 67 68 74 2c 20 44 4c 5f 44 4f  it(&right, DL_DO
2e125 43 49 44 53 2c 20 70 52 69 67 68 74 2c 20 6e 52  CIDS, pRight, nR
2e126 69 67 68 74 29 3b 0a 20 20 64 6c 77 49 6e 69 74  ight);.  dlwInit
2e127 28 26 77 72 69 74 65 72 2c 20 44 4c 5f 44 4f 43  (&writer, DL_DOC
2e128 49 44 53 2c 20 70 4f 75 74 29 3b 0a 0a 20 20 77  IDS, pOut);..  w
2e129 68 69 6c 65 28 20 21 64 6c 72 41 74 45 6e 64 28  hile( !dlrAtEnd(
2e12a 26 6c 65 66 74 29 20 7c 7c 20 21 64 6c 72 41 74  &left) || !dlrAt
2e12b 45 6e 64 28 26 72 69 67 68 74 29 20 29 7b 0a 20  End(&right) ){. 
2e12c 20 20 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28     if( dlrAtEnd(
2e12d 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20 20  &right) ){.     
2e12e 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c   dlwAdd(&writer,
2e12f 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29   dlrDocid(&left)
2e130 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
2e131 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65 6c  (&left);.    }el
2e132 73 65 20 69 66 28 20 64 6c 72 41 74 45 6e 64 28  se if( dlrAtEnd(
2e133 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20 20 20  &left) ){.      
2e134 64 6c 77 41 64 64 28 26 77 72 69 74 65 72 2c 20  dlwAdd(&writer, 
2e135 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29  dlrDocid(&right)
2e136 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65 70  );.      dlrStep
2e137 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 7d 65  (&right);.    }e
2e138 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64  lse if( dlrDocid
2e139 28 26 6c 65 66 74 29 3c 64 6c 72 44 6f 63 69 64  (&left)<dlrDocid
2e13a 28 26 72 69 67 68 74 29 20 29 7b 0a 20 20 20 20  (&right) ){.    
2e13b 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72    dlwAdd(&writer
2e13c 2c 20 64 6c 72 44 6f 63 69 64 28 26 6c 65 66 74  , dlrDocid(&left
2e13d 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74 65  ));.      dlrSte
2e13e 70 28 26 6c 65 66 74 29 3b 0a 20 20 20 20 7d 65  p(&left);.    }e
2e13f 6c 73 65 20 69 66 28 20 64 6c 72 44 6f 63 69 64  lse if( dlrDocid
2e140 28 26 72 69 67 68 74 29 3c 64 6c 72 44 6f 63 69  (&right)<dlrDoci
2e141 64 28 26 6c 65 66 74 29 20 29 7b 0a 20 20 20 20  d(&left) ){.    
2e142 20 20 64 6c 77 41 64 64 28 26 77 72 69 74 65 72    dlwAdd(&writer
2e143 2c 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  , dlrDocid(&righ
2e144 74 29 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  t));.      dlrSt
2e145 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
2e146 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 64 6c 77  }else{.      dlw
2e147 41 64 64 28 26 77 72 69 74 65 72 2c 20 64 6c 72  Add(&writer, dlr
2e148 44 6f 63 69 64 28 26 6c 65 66 74 29 29 3b 0a 20  Docid(&left));. 
2e149 20 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65       dlrStep(&le
2e14a 66 74 29 3b 0a 20 20 20 20 20 20 64 6c 72 53 74  ft);.      dlrSt
2e14b 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ep(&right);.    
2e14c 7d 0a 20 20 7d 0a 0a 20 20 64 6c 72 44 65 73 74  }.  }..  dlrDest
2e14d 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 64 6c  roy(&left);.  dl
2e14e 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74 29  rDestroy(&right)
2e14f 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f 79 28 26  ;.  dlwDestroy(&
2e150 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 2f 2a 20 57  writer);.}../* W
2e151 65 20 68 61 76 65 20 74 77 6f 20 44 4c 5f 44 4f  e have two DL_DO
2e152 43 49 44 53 20 64 6f 63 6c 69 73 74 73 3a 20 20  CIDS doclists:  
2e153 70 4c 65 66 74 20 61 6e 64 20 70 52 69 67 68 74  pLeft and pRight
2e154 2e 0a 2a 2a 20 57 72 69 74 65 20 69 6e 74 6f 20  ..** Write into 
2e155 70 4f 75 74 20 61 73 20 44 4c 5f 44 4f 43 49 44  pOut as DL_DOCID
2e156 53 20 64 6f 63 6c 69 73 74 20 63 6f 6e 74 61 69  S doclist contai
2e157 6e 69 6e 67 20 61 6c 6c 20 64 6f 63 75 6d 65 6e  ning all documen
2e158 74 73 20 74 68 61 74 0a 2a 2a 20 6f 63 63 75 72  ts that.** occur
2e159 20 69 6e 20 70 4c 65 66 74 20 62 75 74 20 6e 6f   in pLeft but no
2e15a 74 20 69 6e 20 70 52 69 67 68 74 2e 0a 2a 2f 0a  t in pRight..*/.
2e15b 73 74 61 74 69 63 20 76 6f 69 64 20 64 6f 63 4c  static void docL
2e15c 69 73 74 45 78 63 65 70 74 4d 65 72 67 65 28 0a  istExceptMerge(.
2e15d 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 4c    const char *pL
2e15e 65 66 74 2c 20 69 6e 74 20 6e 4c 65 66 74 2c 0a  eft, int nLeft,.
2e15f 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
2e160 69 67 68 74 2c 20 69 6e 74 20 6e 52 69 67 68 74  ight, int nRight
2e161 2c 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  ,.  DataBuffer *
2e162 70 4f 75 74 20 20 20 20 20 20 2f 2a 20 57 72 69  pOut      /* Wri
2e163 74 65 20 74 68 65 20 63 6f 6d 62 69 6e 65 64 20  te the combined 
2e164 64 6f 63 6c 69 73 74 20 68 65 72 65 20 2a 2f 0a  doclist here */.
2e165 29 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 6c 65  ){.  DLReader le
2e166 66 74 2c 20 72 69 67 68 74 3b 0a 20 20 44 4c 57  ft, right;.  DLW
2e167 72 69 74 65 72 20 77 72 69 74 65 72 3b 0a 0a 20  riter writer;.. 
2e168 20 69 66 28 20 6e 4c 65 66 74 3d 3d 30 20 29 20   if( nLeft==0 ) 
2e169 72 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 6e 52  return;.  if( nR
2e16a 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 64  ight==0 ){.    d
2e16b 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 28  ataBufferAppend(
2e16c 70 4f 75 74 2c 20 70 4c 65 66 74 2c 20 6e 4c 65  pOut, pLeft, nLe
2e16d 66 74 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b  ft);.    return;
2e16e 0a 20 20 7d 0a 0a 20 20 64 6c 72 49 6e 69 74 28  .  }..  dlrInit(
2e16f 26 6c 65 66 74 2c 20 44 4c 5f 44 4f 43 49 44 53  &left, DL_DOCIDS
2e170 2c 20 70 4c 65 66 74 2c 20 6e 4c 65 66 74 29 3b  , pLeft, nLeft);
2e171 0a 20 20 64 6c 72 49 6e 69 74 28 26 72 69 67 68  .  dlrInit(&righ
2e172 74 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 52  t, DL_DOCIDS, pR
2e173 69 67 68 74 2c 20 6e 52 69 67 68 74 29 3b 0a 20  ight, nRight);. 
2e174 20 64 6c 77 49 6e 69 74 28 26 77 72 69 74 65 72   dlwInit(&writer
2e175 2c 20 44 4c 5f 44 4f 43 49 44 53 2c 20 70 4f 75  , DL_DOCIDS, pOu
2e176 74 29 3b 0a 0a 20 20 77 68 69 6c 65 28 20 21 64  t);..  while( !d
2e177 6c 72 41 74 45 6e 64 28 26 6c 65 66 74 29 20 29  lrAtEnd(&left) )
2e178 7b 0a 20 20 20 20 77 68 69 6c 65 28 20 21 64 6c  {.    while( !dl
2e179 72 41 74 45 6e 64 28 26 72 69 67 68 74 29 20 26  rAtEnd(&right) &
2e17a 26 20 64 6c 72 44 6f 63 69 64 28 26 72 69 67 68  & dlrDocid(&righ
2e17b 74 29 3c 64 6c 72 44 6f 63 69 64 28 26 6c 65 66  t)<dlrDocid(&lef
2e17c 74 29 20 29 7b 0a 20 20 20 20 20 20 64 6c 72 53  t) ){.      dlrS
2e17d 74 65 70 28 26 72 69 67 68 74 29 3b 0a 20 20 20  tep(&right);.   
2e17e 20 7d 0a 20 20 20 20 69 66 28 20 64 6c 72 41 74   }.    if( dlrAt
2e17f 45 6e 64 28 26 72 69 67 68 74 29 20 7c 7c 20 64  End(&right) || d
2e180 6c 72 44 6f 63 69 64 28 26 6c 65 66 74 29 3c 64  lrDocid(&left)<d
2e181 6c 72 44 6f 63 69 64 28 26 72 69 67 68 74 29 20  lrDocid(&right) 
2e182 29 7b 0a 20 20 20 20 20 20 64 6c 77 41 64 64 28  ){.      dlwAdd(
2e183 26 77 72 69 74 65 72 2c 20 64 6c 72 44 6f 63 69  &writer, dlrDoci
2e184 64 28 26 6c 65 66 74 29 29 3b 0a 20 20 20 20 7d  d(&left));.    }
2e185 0a 20 20 20 20 64 6c 72 53 74 65 70 28 26 6c 65  .    dlrStep(&le
2e186 66 74 29 3b 0a 20 20 7d 0a 0a 20 20 64 6c 72 44  ft);.  }..  dlrD
2e187 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20  estroy(&left);. 
2e188 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 69 67   dlrDestroy(&rig
2e189 68 74 29 3b 0a 20 20 64 6c 77 44 65 73 74 72 6f  ht);.  dlwDestro
2e18a 79 28 26 77 72 69 74 65 72 29 3b 0a 7d 0a 0a 73  y(&writer);.}..s
2e18b 74 61 74 69 63 20 63 68 61 72 20 2a 73 74 72 69  tatic char *stri
2e18c 6e 67 5f 64 75 70 5f 6e 28 63 6f 6e 73 74 20 63  ng_dup_n(const c
2e18d 68 61 72 20 2a 73 2c 20 69 6e 74 20 6e 29 7b 0a  har *s, int n){.
2e18e 20 20 63 68 61 72 20 2a 73 74 72 20 3d 20 73 71    char *str = sq
2e18f 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 20 2b  lite3_malloc(n +
2e190 20 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28 73 74   1);.  memcpy(st
2e191 72 2c 20 73 2c 20 6e 29 3b 0a 20 20 73 74 72 5b  r, s, n);.  str[
2e192 6e 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 72 65 74  n] = '\0';.  ret
2e193 75 72 6e 20 73 74 72 3b 0a 7d 0a 0a 2f 2a 20 44  urn str;.}../* D
2e194 75 70 6c 69 63 61 74 65 20 61 20 73 74 72 69 6e  uplicate a strin
2e195 67 3b 20 74 68 65 20 63 61 6c 6c 65 72 20 6d 75  g; the caller mu
2e196 73 74 20 66 72 65 65 28 29 20 74 68 65 20 72 65  st free() the re
2e197 74 75 72 6e 65 64 20 73 74 72 69 6e 67 2e 0a 20  turned string.. 
2e198 2a 20 28 57 65 20 64 6f 6e 27 74 20 75 73 65 20  * (We don't use 
2e199 73 74 72 64 75 70 28 29 20 73 69 6e 63 65 20 69  strdup() since i
2e19a 74 20 69 73 20 6e 6f 74 20 70 61 72 74 20 6f 66  t is not part of
2e19b 20 74 68 65 20 73 74 61 6e 64 61 72 64 20 43 20   the standard C 
2e19c 6c 69 62 72 61 72 79 20 61 6e 64 0a 20 2a 20 6d  library and. * m
2e19d 61 79 20 6e 6f 74 20 62 65 20 61 76 61 69 6c 61  ay not be availa
2e19e 62 6c 65 20 65 76 65 72 79 77 68 65 72 65 2e 29  ble everywhere.)
2e19f 20 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20   */.static char 
2e1a0 2a 73 74 72 69 6e 67 5f 64 75 70 28 63 6f 6e 73  *string_dup(cons
2e1a1 74 20 63 68 61 72 20 2a 73 29 7b 0a 20 20 72 65  t char *s){.  re
2e1a2 74 75 72 6e 20 73 74 72 69 6e 67 5f 64 75 70 5f  turn string_dup_
2e1a3 6e 28 73 2c 20 73 74 72 6c 65 6e 28 73 29 29 3b  n(s, strlen(s));
2e1a4 0a 7d 0a 0a 2f 2a 20 46 6f 72 6d 61 74 20 61 20  .}../* Format a 
2e1a5 73 74 72 69 6e 67 2c 20 72 65 70 6c 61 63 69 6e  string, replacin
2e1a6 67 20 65 61 63 68 20 6f 63 63 75 72 72 65 6e 63  g each occurrenc
2e1a7 65 20 6f 66 20 74 68 65 20 25 20 63 68 61 72 61  e of the % chara
2e1a8 63 74 65 72 20 77 69 74 68 0a 20 2a 20 7a 44 62  cter with. * zDb
2e1a9 2e 7a 4e 61 6d 65 2e 20 20 54 68 69 73 20 6d 61  .zName.  This ma
2e1aa 79 20 62 65 20 6d 6f 72 65 20 63 6f 6e 76 65 6e  y be more conven
2e1ab 69 65 6e 74 20 74 68 61 6e 20 73 71 6c 69 74 65  ient than sqlite
2e1ac 5f 6d 70 72 69 6e 74 66 28 29 0a 20 2a 20 77 68  _mprintf(). * wh
2e1ad 65 6e 20 6f 6e 65 20 73 74 72 69 6e 67 20 69 73  en one string is
2e1ae 20 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79   used repeatedly
2e1af 20 69 6e 20 61 20 66 6f 72 6d 61 74 20 73 74 72   in a format str
2e1b0 69 6e 67 2e 0a 20 2a 20 54 68 65 20 63 61 6c 6c  ing.. * The call
2e1b1 65 72 20 6d 75 73 74 20 66 72 65 65 28 29 20 74  er must free() t
2e1b2 68 65 20 72 65 74 75 72 6e 65 64 20 73 74 72 69  he returned stri
2e1b3 6e 67 2e 20 2a 2f 0a 73 74 61 74 69 63 20 63 68  ng. */.static ch
2e1b4 61 72 20 2a 73 74 72 69 6e 67 5f 66 6f 72 6d 61  ar *string_forma
2e1b5 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46  t(const char *zF
2e1b6 6f 72 6d 61 74 2c 0a 20 20 20 20 20 20 20 20 20  ormat,.         
2e1b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1b8 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2e1b9 62 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  b, const char *z
2e1ba 4e 61 6d 65 29 7b 0a 20 20 63 6f 6e 73 74 20 63  Name){.  const c
2e1bb 68 61 72 20 2a 70 3b 0a 20 20 73 69 7a 65 5f 74  har *p;.  size_t
2e1bc 20 6c 65 6e 20 3d 20 30 3b 0a 20 20 73 69 7a 65   len = 0;.  size
2e1bd 5f 74 20 6e 44 62 20 3d 20 73 74 72 6c 65 6e 28  _t nDb = strlen(
2e1be 7a 44 62 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e  zDb);.  size_t n
2e1bf 4e 61 6d 65 20 3d 20 73 74 72 6c 65 6e 28 7a 4e  Name = strlen(zN
2e1c0 61 6d 65 29 3b 0a 20 20 73 69 7a 65 5f 74 20 6e  ame);.  size_t n
2e1c1 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65 20 3d 20  FullTableName = 
2e1c2 6e 44 62 2b 31 2b 6e 4e 61 6d 65 3b 0a 20 20 63  nDb+1+nName;.  c
2e1c3 68 61 72 20 2a 72 65 73 75 6c 74 3b 0a 20 20 63  har *result;.  c
2e1c4 68 61 72 20 2a 72 3b 0a 0a 20 20 2f 2a 20 66 69  har *r;..  /* fi
2e1c5 72 73 74 20 63 6f 6d 70 75 74 65 20 6c 65 6e 67  rst compute leng
2e1c6 74 68 20 6e 65 65 64 65 64 20 2a 2f 0a 20 20 66  th needed */.  f
2e1c7 6f 72 28 70 20 3d 20 7a 46 6f 72 6d 61 74 20 3b  or(p = zFormat ;
2e1c8 20 2a 70 20 3b 20 2b 2b 70 29 7b 0a 20 20 20 20   *p ; ++p){.    
2e1c9 6c 65 6e 20 2b 3d 20 28 2a 70 3d 3d 27 25 27 20  len += (*p=='%' 
2e1ca 3f 20 6e 46 75 6c 6c 54 61 62 6c 65 4e 61 6d 65  ? nFullTableName
2e1cb 20 3a 20 31 29 3b 0a 20 20 7d 0a 20 20 6c 65 6e   : 1);.  }.  len
2e1cc 20 2b 3d 20 31 3b 20 20 2f 2a 20 66 6f 72 20 6e   += 1;  /* for n
2e1cd 75 6c 6c 20 74 65 72 6d 69 6e 61 74 6f 72 20 2a  ull terminator *
2e1ce 2f 0a 0a 20 20 72 20 3d 20 72 65 73 75 6c 74 20  /..  r = result 
2e1cf 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
2e1d0 28 6c 65 6e 29 3b 0a 20 20 66 6f 72 28 70 20 3d  (len);.  for(p =
2e1d1 20 7a 46 6f 72 6d 61 74 3b 20 2a 70 3b 20 2b 2b   zFormat; *p; ++
2e1d2 70 29 7b 0a 20 20 20 20 69 66 28 20 2a 70 3d 3d  p){.    if( *p==
2e1d3 27 25 27 20 29 7b 0a 20 20 20 20 20 20 6d 65 6d  '%' ){.      mem
2e1d4 63 70 79 28 72 2c 20 7a 44 62 2c 20 6e 44 62 29  cpy(r, zDb, nDb)
2e1d5 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 44 62  ;.      r += nDb
2e1d6 3b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 27  ;.      *r++ = '
2e1d7 2e 27 3b 0a 20 20 20 20 20 20 6d 65 6d 63 70 79  .';.      memcpy
2e1d8 28 72 2c 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65  (r, zName, nName
2e1d9 29 3b 0a 20 20 20 20 20 20 72 20 2b 3d 20 6e 4e  );.      r += nN
2e1da 61 6d 65 3b 0a 20 20 20 20 7d 20 65 6c 73 65 20  ame;.    } else 
2e1db 7b 0a 20 20 20 20 20 20 2a 72 2b 2b 20 3d 20 2a  {.      *r++ = *
2e1dc 70 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 2a  p;.    }.  }.  *
2e1dd 72 2b 2b 20 3d 20 27 5c 30 27 3b 0a 20 20 61 73  r++ = '\0';.  as
2e1de 73 65 72 74 28 20 72 20 3d 3d 20 72 65 73 75 6c  sert( r == resul
2e1df 74 20 2b 20 6c 65 6e 20 29 3b 0a 20 20 72 65 74  t + len );.  ret
2e1e0 75 72 6e 20 72 65 73 75 6c 74 3b 0a 7d 0a 0a 73  urn result;.}..s
2e1e1 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 65 78  tatic int sql_ex
2e1e2 65 63 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20  ec(sqlite3 *db, 
2e1e3 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 2c  const char *zDb,
2e1e4 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2e1e5 6d 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  me,.            
2e1e6 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68          const ch
2e1e7 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b 0a 20 20  ar *zFormat){.  
2e1e8 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e 64 20 3d  char *zCommand =
2e1e9 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61 74 28 7a   string_format(z
2e1ea 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20 7a 4e 61  Format, zDb, zNa
2e1eb 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  me);.  int rc;. 
2e1ec 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
2e1ed 20 73 71 6c 3a 20 25 73 5c 6e 22 2c 20 7a 43 6f   sql: %s\n", zCo
2e1ee 6d 6d 61 6e 64 29 29 3b 0a 20 20 72 63 20 3d 20  mmand));.  rc = 
2e1ef 73 71 6c 69 74 65 33 5f 65 78 65 63 28 64 62 2c  sqlite3_exec(db,
2e1f0 20 7a 43 6f 6d 6d 61 6e 64 2c 20 4e 55 4c 4c 2c   zCommand, NULL,
2e1f1 20 30 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c   0, NULL);.  sql
2e1f2 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61  ite3_free(zComma
2e1f3 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nd);.  return rc
2e1f4 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
2e1f5 73 71 6c 5f 70 72 65 70 61 72 65 28 73 71 6c 69  sql_prepare(sqli
2e1f6 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
2e1f7 68 61 72 20 2a 7a 44 62 2c 20 63 6f 6e 73 74 20  har *zDb, const 
2e1f8 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 20  char *zName,.   
2e1f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e1fa 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74      sqlite3_stmt
2e1fb 20 2a 2a 70 70 53 74 6d 74 2c 20 63 6f 6e 73 74   **ppStmt, const
2e1fc 20 63 68 61 72 20 2a 7a 46 6f 72 6d 61 74 29 7b   char *zFormat){
2e1fd 0a 20 20 63 68 61 72 20 2a 7a 43 6f 6d 6d 61 6e  .  char *zComman
2e1fe 64 20 3d 20 73 74 72 69 6e 67 5f 66 6f 72 6d 61  d = string_forma
2e1ff 74 28 7a 46 6f 72 6d 61 74 2c 20 7a 44 62 2c 20  t(zFormat, zDb, 
2e200 7a 4e 61 6d 65 29 3b 0a 20 20 69 6e 74 20 72 63  zName);.  int rc
2e201 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46  ;.  FTSTRACE(("F
2e202 54 53 33 20 70 72 65 70 61 72 65 3a 20 25 73 5c  TS3 prepare: %s\
2e203 6e 22 2c 20 7a 43 6f 6d 6d 61 6e 64 29 29 3b 0a  n", zCommand));.
2e204 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70    rc = sqlite3_p
2e205 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 43  repare_v2(db, zC
2e206 6f 6d 6d 61 6e 64 2c 20 2d 31 2c 20 70 70 53 74  ommand, -1, ppSt
2e207 6d 74 2c 20 4e 55 4c 4c 29 3b 0a 20 20 73 71 6c  mt, NULL);.  sql
2e208 69 74 65 33 5f 66 72 65 65 28 7a 43 6f 6d 6d 61  ite3_free(zComma
2e209 6e 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  nd);.  return rc
2e20a 3b 0a 7d 0a 0a 2f 2a 20 65 6e 64 20 75 74 69 6c  ;.}../* end util
2e20b 69 74 79 20 66 75 6e 63 74 69 6f 6e 73 20 2a 2f  ity functions */
2e20c 0a 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66  ../* Forward ref
2e20d 65 72 65 6e 63 65 20 2a 2f 0a 74 79 70 65 64 65  erence */.typede
2e20e 66 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78  f struct fulltex
2e20f 74 5f 76 74 61 62 20 66 75 6c 6c 74 65 78 74 5f  t_vtab fulltext_
2e210 76 74 61 62 3b 0a 0a 2f 2a 20 41 20 73 69 6e 67  vtab;../* A sing
2e211 6c 65 20 74 65 72 6d 20 69 6e 20 61 20 71 75 65  le term in a que
2e212 72 79 20 69 73 20 72 65 70 72 65 73 65 6e 74 65  ry is represente
2e213 64 20 62 79 20 61 6e 20 69 6e 73 74 61 6e 63 65  d by an instance
2e214 73 20 6f 66 0a 2a 2a 20 74 68 65 20 66 6f 6c 6c  s of.** the foll
2e215 6f 77 69 6e 67 20 73 74 72 75 63 74 75 72 65 2e  owing structure.
2e216 20 45 61 63 68 20 77 6f 72 64 20 77 68 69 63 68   Each word which
2e217 20 6d 61 79 20 6d 61 74 63 68 20 61 67 61 69 6e   may match again
2e218 73 74 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 63  st.** document c
2e219 6f 6e 74 65 6e 74 20 69 73 20 61 20 74 65 72 6d  ontent is a term
2e21a 2e 20 4f 70 65 72 61 74 6f 72 73 2c 20 6c 69 6b  . Operators, lik
2e21b 65 20 4e 45 41 52 20 6f 72 20 4f 52 2c 20 61 72  e NEAR or OR, ar
2e21c 65 0a 2a 2a 20 6e 6f 74 20 74 65 72 6d 73 2e 20  e.** not terms. 
2e21d 51 75 65 72 79 20 74 65 72 6d 73 20 61 72 65 20  Query terms are 
2e21e 6f 72 67 61 6e 69 7a 65 64 20 61 73 20 61 20 66  organized as a f
2e21f 6c 61 74 20 6c 69 73 74 20 73 74 6f 72 65 64 0a  lat list stored.
2e220 2a 2a 20 69 6e 20 74 68 65 20 51 75 65 72 79 2e  ** in the Query.
2e221 70 54 65 72 6d 73 20 61 72 72 61 79 2e 0a 2a 2a  pTerms array..**
2e222 0a 2a 2a 20 49 66 20 74 68 65 20 51 75 65 72 79  .** If the Query
2e223 54 65 72 6d 2e 6e 50 68 72 61 73 65 20 76 61 72  Term.nPhrase var
2e224 69 61 62 6c 65 20 69 73 20 6e 6f 6e 2d 7a 65 72  iable is non-zer
2e225 6f 2c 20 74 68 65 6e 20 74 68 65 20 51 75 65 72  o, then the Quer
2e226 79 54 65 72 6d 0a 2a 2a 20 69 73 20 74 68 65 20  yTerm.** is the 
2e227 66 69 72 73 74 20 69 6e 20 61 20 63 6f 6e 74 69  first in a conti
2e228 67 75 6f 75 73 20 73 74 72 69 6e 67 20 6f 66 20  guous string of 
2e229 74 65 72 6d 73 20 74 68 61 74 20 61 72 65 20 65  terms that are e
2e22a 69 74 68 65 72 20 70 61 72 74 0a 2a 2a 20 6f 66  ither part.** of
2e22b 20 74 68 65 20 73 61 6d 65 20 70 68 72 61 73 65   the same phrase
2e22c 2c 20 6f 72 20 63 6f 6e 6e 65 63 74 65 64 20 62  , or connected b
2e22d 79 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72 61  y the NEAR opera
2e22e 74 6f 72 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68  tor..**.** If th
2e22f 65 20 51 75 65 72 79 54 65 72 6d 2e 6e 4e 65 61  e QueryTerm.nNea
2e230 72 20 76 61 72 69 61 62 6c 65 20 69 73 20 6e 6f  r variable is no
2e231 6e 2d 7a 65 72 6f 2c 20 74 68 65 6e 20 74 68 65  n-zero, then the
2e232 20 74 6f 6b 65 6e 20 69 73 20 66 6f 6c 6c 6f 77   token is follow
2e233 65 64 20 0a 2a 2a 20 62 79 20 61 20 4e 45 41 52  ed .** by a NEAR
2e234 20 6f 70 65 72 61 74 6f 72 20 77 69 74 68 20 73   operator with s
2e235 70 61 6e 20 73 65 74 20 74 6f 20 28 6e 4e 65 61  pan set to (nNea
2e236 72 2d 31 29 2e 20 46 6f 72 20 65 78 61 6d 70 6c  r-1). For exampl
2e237 65 2c 20 74 68 65 20 0a 2a 2a 20 66 6f 6c 6c 6f  e, the .** follo
2e238 77 69 6e 67 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a  wing query:.**.*
2e239 2a 20 54 68 65 20 51 75 65 72 79 54 65 72 6d 2e  * The QueryTerm.
2e23a 69 50 68 72 61 73 65 20 76 61 72 69 61 62 6c 65  iPhrase variable
2e23b 20 73 74 6f 72 65 73 20 74 68 65 20 69 6e 64 65   stores the inde
2e23c 78 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 77  x of the token w
2e23d 69 74 68 69 6e 0a 2a 2a 20 69 74 73 20 70 68 72  ithin.** its phr
2e23e 61 73 65 2c 20 69 6e 64 65 78 65 64 20 73 74 61  ase, indexed sta
2e23f 72 74 69 6e 67 20 61 74 20 31 2c 20 6f 72 20 31  rting at 1, or 1
2e240 20 69 66 20 74 68 65 20 74 6f 6b 65 6e 20 69 73   if the token is
2e241 20 6e 6f 74 20 70 61 72 74 20 0a 2a 2a 20 6f 66   not part .** of
2e242 20 61 6e 79 20 70 68 72 61 73 65 2e 0a 2a 2a 0a   any phrase..**.
2e243 2a 2a 20 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20  ** For example, 
2e244 74 68 65 20 64 61 74 61 20 73 74 72 75 63 74 75  the data structu
2e245 72 65 20 75 73 65 64 20 74 6f 20 72 65 70 72 65  re used to repre
2e246 73 65 6e 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69  sent the followi
2e247 6e 67 20 71 75 65 72 79 3a 0a 2a 2a 0a 2a 2a 20  ng query:.**.** 
2e248 20 20 20 20 2e 2e 2e 20 4d 41 54 43 48 20 27 73      ... MATCH 's
2e249 71 6c 69 74 65 20 4e 45 41 52 2f 35 20 67 6f 6f  qlite NEAR/5 goo
2e24a 67 6c 65 20 4e 45 41 52 2f 32 20 22 73 65 61 72  gle NEAR/2 "sear
2e24b 63 68 20 65 6e 67 69 6e 65 22 27 0a 2a 2a 0a 2a  ch engine"'.**.*
2e24c 2a 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  * is:.**.**     
2e24d 7b 6e 50 68 72 61 73 65 3d 34 2c 20 69 50 68 72  {nPhrase=4, iPhr
2e24e 61 73 65 3d 31 2c 20 6e 4e 65 61 72 3d 36 2c 20  ase=1, nNear=6, 
2e24f 70 54 65 72 6d 3d 22 73 71 6c 69 74 65 22 7d 2c  pTerm="sqlite"},
2e250 0a 2a 2a 20 20 20 20 20 7b 6e 50 68 72 61 73 65  .**     {nPhrase
2e251 3d 30 2c 20 69 50 68 72 61 73 65 3d 31 2c 20 6e  =0, iPhrase=1, n
2e252 4e 65 61 72 3d 33 2c 20 70 54 65 72 6d 3d 22 67  Near=3, pTerm="g
2e253 6f 6f 67 6c 65 22 7d 2c 0a 2a 2a 20 20 20 20 20  oogle"},.**     
2e254 7b 6e 50 68 72 61 73 65 3d 30 2c 20 69 50 68 72  {nPhrase=0, iPhr
2e255 61 73 65 3d 31 2c 20 6e 4e 65 61 72 3d 30 2c 20  ase=1, nNear=0, 
2e256 70 54 65 72 6d 3d 22 73 65 61 72 63 68 22 7d 2c  pTerm="search"},
2e257 0a 2a 2a 20 20 20 20 20 7b 6e 50 68 72 61 73 65  .**     {nPhrase
2e258 3d 30 2c 20 69 50 68 72 61 73 65 3d 32 2c 20 6e  =0, iPhrase=2, n
2e259 4e 65 61 72 3d 30 2c 20 70 54 65 72 6d 3d 22 65  Near=0, pTerm="e
2e25a 6e 67 69 6e 65 22 7d 2c 0a 2a 2a 0a 2a 2a 20 63  ngine"},.**.** c
2e25b 6f 6d 70 69 6c 69 6e 67 20 74 68 65 20 46 54 53  ompiling the FTS
2e25c 33 20 73 79 6e 74 61 78 20 74 6f 20 51 75 65 72  3 syntax to Quer
2e25d 79 20 73 74 72 75 63 74 75 72 65 73 20 69 73 20  y structures is 
2e25e 64 6f 6e 65 20 62 79 20 74 68 65 20 70 61 72 73  done by the pars
2e25f 65 51 75 65 72 79 28 29 0a 2a 2a 20 66 75 6e 63  eQuery().** func
2e260 74 69 6f 6e 2e 0a 2a 2f 0a 74 79 70 65 64 65 66  tion..*/.typedef
2e261 20 73 74 72 75 63 74 20 51 75 65 72 79 54 65 72   struct QueryTer
2e262 6d 20 7b 0a 20 20 73 68 6f 72 74 20 69 6e 74 20  m {.  short int 
2e263 6e 50 68 72 61 73 65 3b 20 2f 2a 20 48 6f 77 20  nPhrase; /* How 
2e264 6d 61 6e 79 20 66 6f 6c 6c 6f 77 69 6e 67 20 74  many following t
2e265 65 72 6d 73 20 61 72 65 20 70 61 72 74 20 6f 66  erms are part of
2e266 20 74 68 65 20 73 61 6d 65 20 70 68 72 61 73 65   the same phrase
2e267 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20   */.  short int 
2e268 69 50 68 72 61 73 65 3b 20 2f 2a 20 54 68 69 73  iPhrase; /* This
2e269 20 69 73 20 74 68 65 20 69 2d 74 68 20 74 65 72   is the i-th ter
2e26a 6d 20 6f 66 20 61 20 70 68 72 61 73 65 2e 20 2a  m of a phrase. *
2e26b 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74 20 69 43  /.  short int iC
2e26c 6f 6c 75 6d 6e 3b 20 2f 2a 20 43 6f 6c 75 6d 6e  olumn; /* Column
2e26d 20 6f 66 20 74 68 65 20 69 6e 64 65 78 20 74 68   of the index th
2e26e 61 74 20 6d 75 73 74 20 6d 61 74 63 68 20 74 68  at must match th
2e26f 69 73 20 74 65 72 6d 20 2a 2f 0a 20 20 73 68 6f  is term */.  sho
2e270 72 74 20 69 6e 74 20 6e 4e 65 61 72 3b 20 20 20  rt int nNear;   
2e271 2f 2a 20 74 65 72 6d 20 66 6f 6c 6c 6f 77 65 64  /* term followed
2e272 20 62 79 20 61 20 4e 45 41 52 20 6f 70 65 72 61   by a NEAR opera
2e273 74 6f 72 20 77 69 74 68 20 73 70 61 6e 3d 28 6e  tor with span=(n
2e274 4e 65 61 72 2d 31 29 20 2a 2f 0a 20 20 73 69 67  Near-1) */.  sig
2e275 6e 65 64 20 63 68 61 72 20 69 73 4f 72 3b 20 20  ned char isOr;  
2e276 2f 2a 20 74 68 69 73 20 74 65 72 6d 20 69 73 20  /* this term is 
2e277 70 72 65 63 65 64 65 64 20 62 79 20 22 4f 52 22  preceded by "OR"
2e278 20 2a 2f 0a 20 20 73 69 67 6e 65 64 20 63 68 61   */.  signed cha
2e279 72 20 69 73 4e 6f 74 3b 20 2f 2a 20 74 68 69 73  r isNot; /* this
2e27a 20 74 65 72 6d 20 69 73 20 70 72 65 63 65 64 65   term is precede
2e27b 64 20 62 79 20 22 2d 22 20 2a 2f 0a 20 20 73 69  d by "-" */.  si
2e27c 67 6e 65 64 20 63 68 61 72 20 69 73 50 72 65 66  gned char isPref
2e27d 69 78 3b 20 2f 2a 20 74 68 69 73 20 74 65 72 6d  ix; /* this term
2e27e 20 69 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20   is followed by 
2e27f 22 2a 22 20 2a 2f 0a 20 20 63 68 61 72 20 2a 70  "*" */.  char *p
2e280 54 65 72 6d 3b 20 20 20 20 20 20 20 2f 2a 20 74  Term;       /* t
2e281 65 78 74 20 6f 66 20 74 68 65 20 74 65 72 6d 2e  ext of the term.
2e282 20 20 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61    '\000' termina
2e283 74 65 64 2e 20 20 6d 61 6c 6c 6f 63 65 64 20 2a  ted.  malloced *
2e284 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20  /.  int nTerm;  
2e285 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e286 20 6f 66 20 62 79 74 65 73 20 69 6e 20 70 54 65   of bytes in pTe
2e287 72 6d 5b 5d 20 2a 2f 0a 7d 20 51 75 65 72 79 54  rm[] */.} QueryT
2e288 65 72 6d 3b 0a 0a 0a 2f 2a 20 41 20 71 75 65 72  erm;.../* A quer
2e289 79 20 73 74 72 69 6e 67 20 69 73 20 70 61 72 73  y string is pars
2e28a 65 64 20 69 6e 74 6f 20 61 20 51 75 65 72 79 20  ed into a Query 
2e28b 73 74 72 75 63 74 75 72 65 2e 0a 20 2a 0a 20 2a  structure.. *. *
2e28c 20 57 65 20 63 6f 75 6c 64 2c 20 69 6e 20 74 68   We could, in th
2e28d 65 6f 72 79 2c 20 61 6c 6c 6f 77 20 71 75 65 72  eory, allow quer
2e28e 79 20 73 74 72 69 6e 67 73 20 74 6f 20 62 65 20  y strings to be 
2e28f 63 6f 6d 70 6c 69 63 61 74 65 64 0a 20 2a 20 6e  complicated. * n
2e290 65 73 74 65 64 20 65 78 70 72 65 73 73 69 6f 6e  ested expression
2e291 73 20 77 69 74 68 20 70 72 65 63 65 64 65 6e 63  s with precedenc
2e292 65 20 64 65 74 65 72 6d 69 6e 65 64 20 62 79 20  e determined by 
2e293 70 61 72 65 6e 74 68 65 73 65 73 2e 0a 20 2a 20  parentheses.. * 
2e294 42 75 74 20 6e 6f 6e 65 20 6f 66 20 74 68 65 20  But none of the 
2e295 6d 61 6a 6f 72 20 73 65 61 72 63 68 20 65 6e 67  major search eng
2e296 69 6e 65 73 20 64 6f 20 74 68 69 73 2e 20 20 28  ines do this.  (
2e297 50 65 72 68 61 70 73 20 74 68 65 0a 20 2a 20 66  Perhaps the. * f
2e298 65 65 6c 69 6e 67 20 69 73 20 74 68 61 74 20 61  eeling is that a
2e299 6e 20 70 61 72 65 6e 74 68 65 73 69 7a 65 64 20  n parenthesized 
2e29a 65 78 70 72 65 73 73 69 6f 6e 20 69 73 20 74 77  expression is tw
2e29b 6f 20 63 6f 6d 70 6c 65 78 20 6f 66 0a 20 2a 20  o complex of. * 
2e29c 61 6e 20 69 64 65 61 20 66 6f 72 20 74 68 65 20  an idea for the 
2e29d 61 76 65 72 61 67 65 20 75 73 65 72 20 74 6f 20  average user to 
2e29e 67 72 61 73 70 2e 29 20 20 54 61 6b 69 6e 67 20  grasp.)  Taking 
2e29f 6f 75 72 20 6c 65 61 64 20 66 72 6f 6d 0a 20 2a  our lead from. *
2e2a0 20 74 68 65 20 6d 61 6a 6f 72 20 73 65 61 72 63   the major searc
2e2a1 68 20 65 6e 67 69 6e 65 73 2c 20 77 65 20 77 69  h engines, we wi
2e2a2 6c 6c 20 61 6c 6c 6f 77 20 71 75 65 72 69 65 73  ll allow queries
2e2a3 20 74 6f 20 62 65 20 61 20 6c 69 73 74 0a 20 2a   to be a list. *
2e2a4 20 6f 66 20 74 65 72 6d 73 20 28 77 69 74 68 20   of terms (with 
2e2a5 61 6e 20 69 6d 70 6c 69 65 64 20 41 4e 44 20 6f  an implied AND o
2e2a6 70 65 72 61 74 6f 72 29 20 6f 72 20 70 68 72 61  perator) or phra
2e2a7 73 65 73 20 69 6e 20 64 6f 75 62 6c 65 2d 71 75  ses in double-qu
2e2a8 6f 74 65 73 2c 0a 20 2a 20 77 69 74 68 20 61 20  otes,. * with a 
2e2a9 73 69 6e 67 6c 65 20 6f 70 74 69 6f 6e 61 6c 20  single optional 
2e2aa 22 2d 22 20 62 65 66 6f 72 65 20 65 61 63 68 20  "-" before each 
2e2ab 6e 6f 6e 2d 70 68 72 61 73 65 20 74 65 72 6d 20  non-phrase term 
2e2ac 74 6f 20 64 65 73 69 67 6e 61 74 65 0a 20 2a 20  to designate. * 
2e2ad 6e 65 67 61 74 69 6f 6e 20 61 6e 64 20 61 6e 20  negation and an 
2e2ae 6f 70 74 69 6f 6e 61 6c 20 4f 52 20 63 6f 6e 6e  optional OR conn
2e2af 65 63 74 6f 72 2e 0a 20 2a 0a 20 2a 20 4f 52 20  ector.. *. * OR 
2e2b0 62 69 6e 64 73 20 6d 6f 72 65 20 74 69 67 68 74  binds more tight
2e2b1 6c 79 20 74 68 61 6e 20 74 68 65 20 69 6d 70 6c  ly than the impl
2e2b2 69 65 64 20 41 4e 44 2c 20 77 68 69 63 68 20 69  ied AND, which i
2e2b3 73 20 77 68 61 74 20 74 68 65 0a 20 2a 20 6d 61  s what the. * ma
2e2b4 6a 6f 72 20 73 65 61 72 63 68 20 65 6e 67 69 6e  jor search engin
2e2b5 65 73 20 73 65 65 6d 20 74 6f 20 64 6f 2e 20 20  es seem to do.  
2e2b6 53 6f 2c 20 66 6f 72 20 65 78 61 6d 70 6c 65 3a  So, for example:
2e2b7 0a 20 2a 20 0a 20 2a 20 20 20 20 5b 6f 6e 65 20  . * . *    [one 
2e2b8 74 77 6f 20 4f 52 20 74 68 72 65 65 5d 20 20 20  two OR three]   
2e2b9 20 20 3d 3d 3e 20 20 20 20 6f 6e 65 20 41 4e 44    ==>    one AND
2e2ba 20 28 74 77 6f 20 4f 52 20 74 68 72 65 65 29 0a   (two OR three).
2e2bb 20 2a 20 20 20 20 5b 6f 6e 65 20 4f 52 20 74 77   *    [one OR tw
2e2bc 6f 20 74 68 72 65 65 5d 20 20 20 20 20 3d 3d 3e  o three]     ==>
2e2bd 20 20 20 20 28 6f 6e 65 20 4f 52 20 74 77 6f 29      (one OR two)
2e2be 20 41 4e 44 20 74 68 72 65 65 0a 20 2a 0a 20 2a   AND three. *. *
2e2bf 20 41 20 22 2d 22 20 62 65 66 6f 72 65 20 61 20   A "-" before a 
2e2c0 74 65 72 6d 20 6d 61 74 63 68 65 73 20 61 6c 6c  term matches all
2e2c1 20 65 6e 74 72 69 65 73 20 74 68 61 74 20 6c 61   entries that la
2e2c2 63 6b 20 74 68 61 74 20 74 65 72 6d 2e 0a 20 2a  ck that term.. *
2e2c3 20 54 68 65 20 22 2d 22 20 6d 75 73 74 20 6f 63   The "-" must oc
2e2c4 63 75 72 20 69 6d 6d 65 64 69 61 74 65 6c 79 20  cur immediately 
2e2c5 62 65 66 6f 72 65 20 74 68 65 20 74 65 72 6d 20  before the term 
2e2c6 77 69 74 68 20 69 6e 20 69 6e 74 65 72 76 65 6e  with in interven
2e2c7 69 6e 67 0a 20 2a 20 73 70 61 63 65 2e 20 20 54  ing. * space.  T
2e2c8 68 69 73 20 69 73 20 68 6f 77 20 74 68 65 20 73  his is how the s
2e2c9 65 61 72 63 68 20 65 6e 67 69 6e 65 73 20 64 6f  earch engines do
2e2ca 20 69 74 2e 0a 20 2a 0a 20 2a 20 41 20 4e 4f 54   it.. *. * A NOT
2e2cb 20 74 65 72 6d 20 63 61 6e 6e 6f 74 20 62 65 20   term cannot be 
2e2cc 74 68 65 20 72 69 67 68 74 2d 68 61 6e 64 20 6f  the right-hand o
2e2cd 70 65 72 61 6e 64 20 6f 66 20 61 6e 20 4f 52 2e  perand of an OR.
2e2ce 20 20 49 66 20 74 68 69 73 0a 20 2a 20 6f 63 63    If this. * occ
2e2cf 75 72 73 20 69 6e 20 74 68 65 20 71 75 65 72 79  urs in the query
2e2d0 20 73 74 72 69 6e 67 2c 20 74 68 65 20 4e 4f 54   string, the NOT
2e2d1 20 69 73 20 69 67 6e 6f 72 65 64 3a 0a 20 2a 0a   is ignored:. *.
2e2d2 20 2a 20 20 20 20 5b 6f 6e 65 20 4f 52 20 2d 74   *    [one OR -t
2e2d3 77 6f 5d 20 20 20 20 20 20 20 20 20 20 3d 3d 3e  wo]          ==>
2e2d4 20 20 20 20 6f 6e 65 20 4f 52 20 74 77 6f 0a 20      one OR two. 
2e2d5 2a 0a 20 2a 2f 0a 74 79 70 65 64 65 66 20 73 74  *. */.typedef st
2e2d6 72 75 63 74 20 51 75 65 72 79 20 7b 0a 20 20 66  ruct Query {.  f
2e2d7 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 46  ulltext_vtab *pF
2e2d8 74 73 3b 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c  ts;  /* The full
2e2d9 20 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20   text index */. 
2e2da 20 69 6e 74 20 6e 54 65 72 6d 73 3b 20 20 20 20   int nTerms;    
2e2db 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
2e2dc 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68 65   of terms in the
2e2dd 20 71 75 65 72 79 20 2a 2f 0a 20 20 51 75 65 72   query */.  Quer
2e2de 79 54 65 72 6d 20 2a 70 54 65 72 6d 73 3b 20 20  yTerm *pTerms;  
2e2df 20 20 2f 2a 20 41 72 72 61 79 20 6f 66 20 74 65    /* Array of te
2e2e0 72 6d 73 2e 20 20 53 70 61 63 65 20 6f 62 74 61  rms.  Space obta
2e2e1 69 6e 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63  ined from malloc
2e2e2 28 29 20 2a 2f 0a 20 20 69 6e 74 20 6e 65 78 74  () */.  int next
2e2e3 49 73 4f 72 3b 20 20 20 20 20 20 20 20 20 2f 2a  IsOr;         /*
2e2e4 20 53 65 74 20 74 68 65 20 69 73 4f 72 20 66 6c   Set the isOr fl
2e2e5 61 67 20 6f 6e 20 74 68 65 20 6e 65 78 74 20 69  ag on the next i
2e2e6 6e 73 65 72 74 65 64 20 74 65 72 6d 20 2a 2f 0a  nserted term */.
2e2e7 20 20 69 6e 74 20 6e 65 78 74 49 73 4e 65 61 72    int nextIsNear
2e2e8 3b 20 20 20 20 20 20 20 2f 2a 20 53 65 74 20 74  ;       /* Set t
2e2e9 68 65 20 69 73 4f 72 20 66 6c 61 67 20 6f 6e 20  he isOr flag on 
2e2ea 74 68 65 20 6e 65 78 74 20 69 6e 73 65 72 74 65  the next inserte
2e2eb 64 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20  d term */.  int 
2e2ec 6e 65 78 74 43 6f 6c 75 6d 6e 3b 20 20 20 20 20  nextColumn;     
2e2ed 20 20 2f 2a 20 4e 65 78 74 20 77 6f 72 64 20 70    /* Next word p
2e2ee 61 72 73 65 64 20 6d 75 73 74 20 62 65 20 69 6e  arsed must be in
2e2ef 20 74 68 69 73 20 63 6f 6c 75 6d 6e 20 2a 2f 0a   this column */.
2e2f0 20 20 69 6e 74 20 64 66 6c 74 43 6f 6c 75 6d 6e    int dfltColumn
2e2f1 3b 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64  ;       /* The d
2e2f2 65 66 61 75 6c 74 20 63 6f 6c 75 6d 6e 20 2a 2f  efault column */
2e2f3 0a 7d 20 51 75 65 72 79 3b 0a 0a 0a 2f 2a 0a 2a  .} Query;.../*.*
2e2f4 2a 20 41 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  * An instance of
2e2f5 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 73   the following s
2e2f6 74 72 75 63 74 75 72 65 20 6b 65 65 70 73 20 74  tructure keeps t
2e2f7 72 61 63 6b 20 6f 66 20 67 65 6e 65 72 61 74 65  rack of generate
2e2f8 64 0a 2a 2a 20 6d 61 74 63 68 69 6e 67 2d 77 6f  d.** matching-wo
2e2f9 72 64 20 6f 66 66 73 65 74 20 69 6e 66 6f 72 6d  rd offset inform
2e2fa 61 74 69 6f 6e 20 61 6e 64 20 73 6e 69 70 70 65  ation and snippe
2e2fb 74 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20 73  ts..*/.typedef s
2e2fc 74 72 75 63 74 20 53 6e 69 70 70 65 74 20 7b 0a  truct Snippet {.
2e2fd 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 20 20 20    int nMatch;   
2e2fe 20 20 2f 2a 20 54 6f 74 61 6c 20 6e 75 6d 62 65    /* Total numbe
2e2ff 72 20 6f 66 20 6d 61 74 63 68 65 73 20 2a 2f 0a  r of matches */.
2e300 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 3b 20 20 20    int nAlloc;   
2e301 20 20 2f 2a 20 53 70 61 63 65 20 61 6c 6c 6f 63    /* Space alloc
2e302 61 74 65 64 20 66 6f 72 20 61 4d 61 74 63 68 5b  ated for aMatch[
2e303 5d 20 2a 2f 0a 20 20 73 74 72 75 63 74 20 73 6e  ] */.  struct sn
2e304 69 70 70 65 74 4d 61 74 63 68 20 7b 20 2f 2a 20  ippetMatch { /* 
2e305 4f 6e 65 20 65 6e 74 72 79 20 66 6f 72 20 65 61  One entry for ea
2e306 63 68 20 6d 61 74 63 68 69 6e 67 20 74 65 72 6d  ch matching term
2e307 20 2a 2f 0a 20 20 20 20 63 68 61 72 20 73 6e 53   */.    char snS
2e308 74 61 74 75 73 3b 20 20 20 20 20 20 20 2f 2a 20  tatus;       /* 
2e309 53 74 61 74 75 73 20 66 6c 61 67 20 66 6f 72 20  Status flag for 
2e30a 75 73 65 20 77 68 69 6c 65 20 63 6f 6e 73 74 72  use while constr
2e30b 75 63 74 69 6e 67 20 73 6e 69 70 70 65 74 73 20  ucting snippets 
2e30c 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69 6e 74  */.    short int
2e30d 20 69 43 6f 6c 3b 20 20 20 20 20 20 2f 2a 20 54   iCol;      /* T
2e30e 68 65 20 63 6f 6c 75 6d 6e 20 74 68 61 74 20 63  he column that c
2e30f 6f 6e 74 61 69 6e 73 20 74 68 65 20 6d 61 74 63  ontains the matc
2e310 68 20 2a 2f 0a 20 20 20 20 73 68 6f 72 74 20 69  h */.    short i
2e311 6e 74 20 69 54 65 72 6d 3b 20 20 20 20 20 2f 2a  nt iTerm;     /*
2e312 20 54 68 65 20 69 6e 64 65 78 20 69 6e 20 51 75   The index in Qu
2e313 65 72 79 2e 70 54 65 72 6d 73 5b 5d 20 6f 66 20  ery.pTerms[] of 
2e314 74 68 65 20 6d 61 74 63 68 69 6e 67 20 74 65 72  the matching ter
2e315 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20 69 54 6f  m */.    int iTo
2e316 6b 65 6e 3b 20 20 20 20 20 20 20 20 20 20 2f 2a  ken;          /*
2e317 20 54 68 65 20 69 6e 64 65 78 20 6f 66 20 74 68   The index of th
2e318 65 20 6d 61 74 63 68 69 6e 67 20 64 6f 63 75 6d  e matching docum
2e319 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 20  ent token */.   
2e31a 20 73 68 6f 72 74 20 69 6e 74 20 6e 42 79 74 65   short int nByte
2e31b 3b 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20  ;     /* Number 
2e31c 6f 66 20 62 79 74 65 73 20 69 6e 20 74 68 65 20  of bytes in the 
2e31d 74 65 72 6d 20 2a 2f 0a 20 20 20 20 69 6e 74 20  term */.    int 
2e31e 69 53 74 61 72 74 3b 20 20 20 20 20 20 20 20 20  iStart;         
2e31f 20 2f 2a 20 54 68 65 20 6f 66 66 73 65 74 20 74   /* The offset t
2e320 6f 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72  o the first char
2e321 61 63 74 65 72 20 6f 66 20 74 68 65 20 74 65 72  acter of the ter
2e322 6d 20 2a 2f 0a 20 20 7d 20 2a 61 4d 61 74 63 68  m */.  } *aMatch
2e323 3b 20 20 20 20 20 20 2f 2a 20 50 6f 69 6e 74 73  ;      /* Points
2e324 20 74 6f 20 73 70 61 63 65 20 6f 62 74 61 69 6e   to space obtain
2e325 65 64 20 66 72 6f 6d 20 6d 61 6c 6c 6f 63 20 2a  ed from malloc *
2e326 2f 0a 20 20 63 68 61 72 20 2a 7a 4f 66 66 73 65  /.  char *zOffse
2e327 74 3b 20 20 2f 2a 20 54 65 78 74 20 72 65 6e 64  t;  /* Text rend
2e328 65 72 69 6e 67 20 6f 66 20 61 4d 61 74 63 68 5b  ering of aMatch[
2e329 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4f 66 66 73  ] */.  int nOffs
2e32a 65 74 3b 20 20 20 20 2f 2a 20 73 74 72 6c 65 6e  et;    /* strlen
2e32b 28 7a 4f 66 66 73 65 74 29 20 2a 2f 0a 20 20 63  (zOffset) */.  c
2e32c 68 61 72 20 2a 7a 53 6e 69 70 70 65 74 3b 20 2f  har *zSnippet; /
2e32d 2a 20 53 6e 69 70 70 65 74 20 74 65 78 74 20 2a  * Snippet text *
2e32e 2f 0a 20 20 69 6e 74 20 6e 53 6e 69 70 70 65 74  /.  int nSnippet
2e32f 3b 20 20 20 2f 2a 20 73 74 72 6c 65 6e 28 7a 53  ;   /* strlen(zS
2e330 6e 69 70 70 65 74 29 20 2a 2f 0a 7d 20 53 6e 69  nippet) */.} Sni
2e331 70 70 65 74 3b 0a 0a 0a 74 79 70 65 64 65 66 20  ppet;...typedef 
2e332 65 6e 75 6d 20 51 75 65 72 79 54 79 70 65 20 7b  enum QueryType {
2e333 0a 20 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43  .  QUERY_GENERIC
2e334 2c 20 20 20 2f 2a 20 74 61 62 6c 65 20 73 63 61  ,   /* table sca
2e335 6e 20 2a 2f 0a 20 20 51 55 45 52 59 5f 44 4f 43  n */.  QUERY_DOC
2e336 49 44 2c 20 20 20 20 20 2f 2a 20 6c 6f 6f 6b 75  ID,     /* looku
2e337 70 20 62 79 20 64 6f 63 69 64 20 2a 2f 0a 20 20  p by docid */.  
2e338 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 20  QUERY_FULLTEXT  
2e339 20 2f 2a 20 51 55 45 52 59 5f 46 55 4c 4c 54 45   /* QUERY_FULLTE
2e33a 58 54 20 2b 20 5b 69 5d 20 69 73 20 61 20 66 75  XT + [i] is a fu
2e33b 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68 20 66  ll-text search f
2e33c 6f 72 20 63 6f 6c 75 6d 6e 20 69 2a 2f 0a 7d 20  or column i*/.} 
2e33d 51 75 65 72 79 54 79 70 65 3b 0a 0a 74 79 70 65  QueryType;..type
2e33e 64 65 66 20 65 6e 75 6d 20 66 75 6c 6c 74 65 78  def enum fulltex
2e33f 74 5f 73 74 61 74 65 6d 65 6e 74 20 7b 0a 20 20  t_statement {.  
2e340 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f 53  CONTENT_INSERT_S
2e341 54 4d 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 53  TMT,.  CONTENT_S
2e342 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 43 4f  ELECT_STMT,.  CO
2e343 4e 54 45 4e 54 5f 55 50 44 41 54 45 5f 53 54 4d  NTENT_UPDATE_STM
2e344 54 2c 0a 20 20 43 4f 4e 54 45 4e 54 5f 44 45 4c  T,.  CONTENT_DEL
2e345 45 54 45 5f 53 54 4d 54 2c 0a 20 20 43 4f 4e 54  ETE_STMT,.  CONT
2e346 45 4e 54 5f 45 58 49 53 54 53 5f 53 54 4d 54 2c  ENT_EXISTS_STMT,
2e347 0a 0a 20 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54  ..  BLOCK_INSERT
2e348 5f 53 54 4d 54 2c 0a 20 20 42 4c 4f 43 4b 5f 53  _STMT,.  BLOCK_S
2e349 45 4c 45 43 54 5f 53 54 4d 54 2c 0a 20 20 42 4c  ELECT_STMT,.  BL
2e34a 4f 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c  OCK_DELETE_STMT,
2e34b 0a 20 20 42 4c 4f 43 4b 5f 44 45 4c 45 54 45 5f  .  BLOCK_DELETE_
2e34c 41 4c 4c 5f 53 54 4d 54 2c 0a 0a 20 20 53 45 47  ALL_STMT,..  SEG
2e34d 44 49 52 5f 4d 41 58 5f 49 4e 44 45 58 5f 53 54  DIR_MAX_INDEX_ST
2e34e 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 45 54  MT,.  SEGDIR_SET
2e34f 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
2e350 53 45 4c 45 43 54 5f 4c 45 56 45 4c 5f 53 54 4d  SELECT_LEVEL_STM
2e351 54 2c 0a 20 20 53 45 47 44 49 52 5f 53 50 41 4e  T,.  SEGDIR_SPAN
2e352 5f 53 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f  _STMT,.  SEGDIR_
2e353 44 45 4c 45 54 45 5f 53 54 4d 54 2c 0a 20 20 53  DELETE_STMT,.  S
2e354 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53 45 47  EGDIR_SELECT_SEG
2e355 4d 45 4e 54 5f 53 54 4d 54 2c 0a 20 20 53 45 47  MENT_STMT,.  SEG
2e356 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c 5f 53  DIR_SELECT_ALL_S
2e357 54 4d 54 2c 0a 20 20 53 45 47 44 49 52 5f 44 45  TMT,.  SEGDIR_DE
2e358 4c 45 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 0a 20  LETE_ALL_STMT,. 
2e359 20 53 45 47 44 49 52 5f 43 4f 55 4e 54 5f 53 54   SEGDIR_COUNT_ST
2e35a 4d 54 2c 0a 0a 20 20 4d 41 58 5f 53 54 4d 54 20  MT,..  MAX_STMT 
2e35b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e35c 20 20 20 20 2f 2a 20 41 6c 77 61 79 73 20 61 74      /* Always at
2e35d 20 65 6e 64 21 20 2a 2f 0a 7d 20 66 75 6c 6c 74   end! */.} fullt
2e35e 65 78 74 5f 73 74 61 74 65 6d 65 6e 74 3b 0a 0a  ext_statement;..
2e35f 2f 2a 20 54 68 65 73 65 20 6d 75 73 74 20 65 78  /* These must ex
2e360 61 63 74 6c 79 20 6d 61 74 63 68 20 74 68 65 20  actly match the 
2e361 65 6e 75 6d 20 61 62 6f 76 65 2e 20 2a 2f 0a 2f  enum above. */./
2e362 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 49  * TODO(shess): I
2e363 73 20 74 68 65 72 65 20 73 6f 6d 65 20 72 69 73  s there some ris
2e364 6b 20 74 68 61 74 20 61 20 73 74 61 74 65 6d 65  k that a stateme
2e365 6e 74 20 77 69 6c 6c 20 62 65 20 75 73 65 64 20  nt will be used 
2e366 69 6e 20 74 77 6f 0a 2a 2a 20 63 75 72 73 6f 72  in two.** cursor
2e367 73 20 61 74 20 6f 6e 63 65 2c 20 65 2e 67 2e 20  s at once, e.g. 
2e368 20 69 66 20 61 20 71 75 65 72 79 20 6a 6f 69 6e   if a query join
2e369 73 20 61 20 76 69 72 74 75 61 6c 20 74 61 62 6c  s a virtual tabl
2e36a 65 20 74 6f 20 69 74 73 65 6c 66 3f 0a 2a 2a 20  e to itself?.** 
2e36b 49 66 20 73 6f 20 70 65 72 68 61 70 73 20 77 65  If so perhaps we
2e36c 20 73 68 6f 75 6c 64 20 6d 6f 76 65 20 73 6f 6d   should move som
2e36d 65 20 6f 66 20 74 68 65 73 65 20 74 6f 20 74 68  e of these to th
2e36e 65 20 63 75 72 73 6f 72 20 6f 62 6a 65 63 74 2e  e cursor object.
2e36f 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74  .*/.static const
2e370 20 63 68 61 72 20 2a 63 6f 6e 73 74 20 66 75 6c   char *const ful
2e371 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74  ltext_zStatement
2e372 5b 4d 41 58 5f 53 54 4d 54 5d 20 3d 20 7b 0a 20  [MAX_STMT] = {. 
2e373 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45   /* CONTENT_INSE
2e374 52 54 20 2a 2f 20 4e 55 4c 4c 2c 20 20 2f 2a 20  RT */ NULL,  /* 
2e375 67 65 6e 65 72 61 74 65 64 20 69 6e 20 63 6f 6e  generated in con
2e376 74 65 6e 74 49 6e 73 65 72 74 53 74 61 74 65 6d  tentInsertStatem
2e377 65 6e 74 28 29 20 2a 2f 0a 20 20 2f 2a 20 43 4f  ent() */.  /* CO
2e378 4e 54 45 4e 54 5f 53 45 4c 45 43 54 20 2a 2f 20  NTENT_SELECT */ 
2e379 4e 55 4c 4c 2c 20 20 2f 2a 20 67 65 6e 65 72 61  NULL,  /* genera
2e37a 74 65 64 20 69 6e 20 63 6f 6e 74 65 6e 74 53 65  ted in contentSe
2e37b 6c 65 63 74 53 74 61 74 65 6d 65 6e 74 28 29 20  lectStatement() 
2e37c 2a 2f 0a 20 20 2f 2a 20 43 4f 4e 54 45 4e 54 5f  */.  /* CONTENT_
2e37d 55 50 44 41 54 45 20 2a 2f 20 4e 55 4c 4c 2c 20  UPDATE */ NULL, 
2e37e 20 2f 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e   /* generated in
2e37f 20 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53 74   contentUpdateSt
2e380 61 74 65 6d 65 6e 74 28 29 20 2a 2f 0a 20 20 2f  atement() */.  /
2e381 2a 20 43 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45  * CONTENT_DELETE
2e382 20 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d   */ "delete from
2e383 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72 65   %_content where
2e384 20 64 6f 63 69 64 20 3d 20 3f 22 2c 0a 20 20 2f   docid = ?",.  /
2e385 2a 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53  * CONTENT_EXISTS
2e386 20 2a 2f 20 22 73 65 6c 65 63 74 20 64 6f 63 69   */ "select doci
2e387 64 20 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74  d from %_content
2e388 20 6c 69 6d 69 74 20 31 22 2c 0a 0a 20 20 2f 2a   limit 1",..  /*
2e389 20 42 4c 4f 43 4b 5f 49 4e 53 45 52 54 20 2a 2f   BLOCK_INSERT */
2e38a 0a 20 20 22 69 6e 73 65 72 74 20 69 6e 74 6f 20  .  "insert into 
2e38b 25 5f 73 65 67 6d 65 6e 74 73 20 28 62 6c 6f 63  %_segments (bloc
2e38c 6b 69 64 2c 20 62 6c 6f 63 6b 29 20 76 61 6c 75  kid, block) valu
2e38d 65 73 20 28 6e 75 6c 6c 2c 20 3f 29 22 2c 0a 20  es (null, ?)",. 
2e38e 20 2f 2a 20 42 4c 4f 43 4b 5f 53 45 4c 45 43 54   /* BLOCK_SELECT
2e38f 20 2a 2f 20 22 73 65 6c 65 63 74 20 62 6c 6f 63   */ "select bloc
2e390 6b 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74  k from %_segment
2e391 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64 20  s where blockid 
2e392 3d 20 3f 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b  = ?",.  /* BLOCK
2e393 5f 44 45 4c 45 54 45 20 2a 2f 20 22 64 65 6c 65  _DELETE */ "dele
2e394 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e  te from %_segmen
2e395 74 73 20 77 68 65 72 65 20 62 6c 6f 63 6b 69 64  ts where blockid
2e396 20 62 65 74 77 65 65 6e 20 3f 20 61 6e 64 20 3f   between ? and ?
2e397 22 2c 0a 20 20 2f 2a 20 42 4c 4f 43 4b 5f 44 45  ",.  /* BLOCK_DE
2e398 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22 64 65 6c  LETE_ALL */ "del
2e399 65 74 65 20 66 72 6f 6d 20 25 5f 73 65 67 6d 65  ete from %_segme
2e39a 6e 74 73 22 2c 0a 0a 20 20 2f 2a 20 53 45 47 44  nts",..  /* SEGD
2e39b 49 52 5f 4d 41 58 5f 49 4e 44 45 58 20 2a 2f 20  IR_MAX_INDEX */ 
2e39c 22 73 65 6c 65 63 74 20 6d 61 78 28 69 64 78 29  "select max(idx)
2e39d 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 77   from %_segdir w
2e39e 68 65 72 65 20 6c 65 76 65 6c 20 3d 20 3f 22 2c  here level = ?",
2e39f 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53 45 54  .  /* SEGDIR_SET
2e3a0 20 2a 2f 20 22 69 6e 73 65 72 74 20 69 6e 74 6f   */ "insert into
2e3a1 20 25 5f 73 65 67 64 69 72 20 76 61 6c 75 65 73   %_segdir values
2e3a2 20 28 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c 20 3f 2c   (?, ?, ?, ?, ?,
2e3a3 20 3f 29 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49   ?)",.  /* SEGDI
2e3a4 52 5f 53 45 4c 45 43 54 5f 4c 45 56 45 4c 20 2a  R_SELECT_LEVEL *
2e3a5 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74 61 72  /.  "select star
2e3a6 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65 73 5f  t_block, leaves_
2e3a7 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f 74 20  end_block, root 
2e3a8 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20 22 0a  from %_segdir ".
2e3a9 20 20 22 20 77 68 65 72 65 20 6c 65 76 65 6c 20    " where level 
2e3aa 3d 20 3f 20 6f 72 64 65 72 20 62 79 20 69 64 78  = ? order by idx
2e3ab 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52 5f 53  ",.  /* SEGDIR_S
2e3ac 50 41 4e 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74  PAN */.  "select
2e3ad 20 6d 69 6e 28 73 74 61 72 74 5f 62 6c 6f 63 6b   min(start_block
2e3ae 29 2c 20 6d 61 78 28 65 6e 64 5f 62 6c 6f 63 6b  ), max(end_block
2e3af 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20  ) from %_segdir 
2e3b0 22 0a 20 20 22 20 77 68 65 72 65 20 6c 65 76 65  ".  " where leve
2e3b1 6c 20 3d 20 3f 20 61 6e 64 20 73 74 61 72 74 5f  l = ? and start_
2e3b2 62 6c 6f 63 6b 20 3c 3e 20 30 22 2c 0a 20 20 2f  block <> 0",.  /
2e3b3 2a 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 20  * SEGDIR_DELETE 
2e3b4 2a 2f 20 22 64 65 6c 65 74 65 20 66 72 6f 6d 20  */ "delete from 
2e3b5 25 5f 73 65 67 64 69 72 20 77 68 65 72 65 20 6c  %_segdir where l
2e3b6 65 76 65 6c 20 3d 20 3f 22 2c 0a 0a 20 20 2f 2a  evel = ?",..  /*
2e3b7 20 4e 4f 54 45 28 73 68 65 73 73 29 3a 20 54 68   NOTE(shess): Th
2e3b8 65 20 66 69 72 73 74 20 74 68 72 65 65 20 72 65  e first three re
2e3b9 73 75 6c 74 73 20 6f 66 20 74 68 65 20 66 6f 6c  sults of the fol
2e3ba 6c 6f 77 69 6e 67 20 74 77 6f 0a 20 20 2a 2a 20  lowing two.  ** 
2e3bb 73 74 61 74 65 6d 65 6e 74 73 20 6d 75 73 74 20  statements must 
2e3bc 6d 61 74 63 68 2e 0a 20 20 2a 2f 0a 20 20 2f 2a  match..  */.  /*
2e3bd 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53   SEGDIR_SELECT_S
2e3be 45 47 4d 45 4e 54 20 2a 2f 0a 20 20 22 73 65 6c  EGMENT */.  "sel
2e3bf 65 63 74 20 73 74 61 72 74 5f 62 6c 6f 63 6b 2c  ect start_block,
2e3c0 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f 63   leaves_end_bloc
2e3c1 6b 2c 20 72 6f 6f 74 20 66 72 6f 6d 20 25 5f 73  k, root from %_s
2e3c2 65 67 64 69 72 20 22 0a 20 20 22 20 77 68 65 72  egdir ".  " wher
2e3c3 65 20 6c 65 76 65 6c 20 3d 20 3f 20 61 6e 64 20  e level = ? and 
2e3c4 69 64 78 20 3d 20 3f 22 2c 0a 20 20 2f 2a 20 53  idx = ?",.  /* S
2e3c5 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c 4c  EGDIR_SELECT_ALL
2e3c6 20 2a 2f 0a 20 20 22 73 65 6c 65 63 74 20 73 74   */.  "select st
2e3c7 61 72 74 5f 62 6c 6f 63 6b 2c 20 6c 65 61 76 65  art_block, leave
2e3c8 73 5f 65 6e 64 5f 62 6c 6f 63 6b 2c 20 72 6f 6f  s_end_block, roo
2e3c9 74 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72 20  t from %_segdir 
2e3ca 22 0a 20 20 22 20 6f 72 64 65 72 20 62 79 20 6c  ".  " order by l
2e3cb 65 76 65 6c 20 64 65 73 63 2c 20 69 64 78 20 61  evel desc, idx a
2e3cc 73 63 22 2c 0a 20 20 2f 2a 20 53 45 47 44 49 52  sc",.  /* SEGDIR
2e3cd 5f 44 45 4c 45 54 45 5f 41 4c 4c 20 2a 2f 20 22  _DELETE_ALL */ "
2e3ce 64 65 6c 65 74 65 20 66 72 6f 6d 20 25 5f 73 65  delete from %_se
2e3cf 67 64 69 72 22 2c 0a 20 20 2f 2a 20 53 45 47 44  gdir",.  /* SEGD
2e3d0 49 52 5f 43 4f 55 4e 54 20 2a 2f 20 22 73 65 6c  IR_COUNT */ "sel
2e3d1 65 63 74 20 63 6f 75 6e 74 28 2a 29 2c 20 69 66  ect count(*), if
2e3d2 6e 75 6c 6c 28 6d 61 78 28 6c 65 76 65 6c 29 2c  null(max(level),
2e3d3 30 29 20 66 72 6f 6d 20 25 5f 73 65 67 64 69 72  0) from %_segdir
2e3d4 22 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 41 20 63  ",.};../*.** A c
2e3d5 6f 6e 6e 65 63 74 69 6f 6e 20 74 6f 20 61 20 66  onnection to a f
2e3d6 75 6c 6c 74 65 78 74 20 69 6e 64 65 78 20 69 73  ulltext index is
2e3d7 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66 20   an instance of 
2e3d8 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a 2a  the following.**
2e3d9 20 73 74 72 75 63 74 75 72 65 2e 20 20 54 68 65   structure.  The
2e3da 20 78 43 72 65 61 74 65 20 61 6e 64 20 78 43 6f   xCreate and xCo
2e3db 6e 6e 65 63 74 20 6d 65 74 68 6f 64 73 20 63 72  nnect methods cr
2e3dc 65 61 74 65 20 61 6e 20 69 6e 73 74 61 6e 63 65  eate an instance
2e3dd 0a 2a 2a 20 6f 66 20 74 68 69 73 20 73 74 72 75  .** of this stru
2e3de 63 74 75 72 65 20 61 6e 64 20 78 44 65 73 74 72  cture and xDestr
2e3df 6f 79 20 61 6e 64 20 78 44 69 73 63 6f 6e 6e 65  oy and xDisconne
2e3e0 63 74 20 66 72 65 65 20 74 68 61 74 20 69 6e 73  ct free that ins
2e3e1 74 61 6e 63 65 2e 0a 2a 2a 20 41 6c 6c 20 6f 74  tance..** All ot
2e3e2 68 65 72 20 6d 65 74 68 6f 64 73 20 72 65 63 65  her methods rece
2e3e3 69 76 65 20 61 20 70 6f 69 6e 74 65 72 20 74 6f  ive a pointer to
2e3e4 20 74 68 65 20 73 74 72 75 63 74 75 72 65 20 61   the structure a
2e3e5 73 20 6f 6e 65 20 6f 66 20 74 68 65 69 72 0a 2a  s one of their.*
2e3e6 2a 20 61 72 67 75 6d 65 6e 74 73 2e 0a 2a 2f 0a  * arguments..*/.
2e3e7 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78 74 5f  struct fulltext_
2e3e8 76 74 61 62 20 7b 0a 20 20 73 71 6c 69 74 65 33  vtab {.  sqlite3
2e3e9 5f 76 74 61 62 20 62 61 73 65 3b 20 20 20 20 20  _vtab base;     
2e3ea 20 20 20 20 20 20 20 20 20 20 2f 2a 20 42 61 73            /* Bas
2e3eb 65 20 63 6c 61 73 73 20 75 73 65 64 20 62 79 20  e class used by 
2e3ec 53 51 4c 69 74 65 20 63 6f 72 65 20 2a 2f 0a 20  SQLite core */. 
2e3ed 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
2e3ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e3ef 20 20 2f 2a 20 54 68 65 20 64 61 74 61 62 61 73    /* The databas
2e3f0 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2e3f1 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 44    const char *zD
2e3f2 62 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  b;              
2e3f3 20 20 20 2f 2a 20 6c 6f 67 69 63 61 6c 20 64 61     /* logical da
2e3f4 74 61 62 61 73 65 20 6e 61 6d 65 20 2a 2f 0a 20  tabase name */. 
2e3f5 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61   const char *zNa
2e3f6 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  me;             
2e3f7 20 20 2f 2a 20 76 69 72 74 75 61 6c 20 74 61 62    /* virtual tab
2e3f8 6c 65 20 6e 61 6d 65 20 2a 2f 0a 20 20 69 6e 74  le name */.  int
2e3f9 20 6e 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20   nColumn;       
2e3fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2e3fb 20 6e 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d   number of colum
2e3fc 6e 73 20 69 6e 20 76 69 72 74 75 61 6c 20 74 61  ns in virtual ta
2e3fd 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a  ble */.  char **
2e3fe 61 7a 43 6f 6c 75 6d 6e 3b 20 20 20 20 20 20 20  azColumn;       
2e3ff 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c            /* col
2e400 75 6d 6e 20 6e 61 6d 65 73 2e 20 20 6d 61 6c 6c  umn names.  mall
2e401 6f 63 65 64 20 2a 2f 0a 20 20 63 68 61 72 20 2a  oced */.  char *
2e402 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  *azContentColumn
2e403 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f  ;          /* co
2e404 6c 75 6d 6e 20 6e 61 6d 65 73 20 69 6e 20 63 6f  lumn names in co
2e405 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 6d 61 6c  ntent table; mal
2e406 6c 6f 63 65 64 20 2a 2f 0a 20 20 73 71 6c 69 74  loced */.  sqlit
2e407 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54  e3_tokenizer *pT
2e408 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 2f 2a 20 74  okenizer;   /* t
2e409 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 69 6e 73  okenizer for ins
2e40a 65 72 74 73 20 61 6e 64 20 71 75 65 72 69 65 73  erts and queries
2e40b 20 2a 2f 0a 0a 20 20 2f 2a 20 50 72 65 63 6f 6d   */..  /* Precom
2e40c 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e 74 73  piled statements
2e40d 20 77 68 69 63 68 20 77 65 20 6b 65 65 70 20 61   which we keep a
2e40e 73 20 6c 6f 6e 67 20 61 73 20 74 68 65 20 74 61  s long as the ta
2e40f 62 6c 65 20 69 73 0a 20 20 2a 2a 20 6f 70 65 6e  ble is.  ** open
2e410 2e 0a 20 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  ..  */.  sqlite3
2e411 5f 73 74 6d 74 20 2a 70 46 75 6c 6c 74 65 78 74  _stmt *pFulltext
2e412 53 74 61 74 65 6d 65 6e 74 73 5b 4d 41 58 5f 53  Statements[MAX_S
2e413 54 4d 54 5d 3b 0a 0a 20 20 2f 2a 20 50 72 65 63  TMT];..  /* Prec
2e414 6f 6d 70 69 6c 65 64 20 73 74 61 74 65 6d 65 6e  ompiled statemen
2e415 74 73 20 75 73 65 64 20 66 6f 72 20 73 65 67 6d  ts used for segm
2e416 65 6e 74 20 6d 65 72 67 65 73 2e 20 20 57 65 20  ent merges.  We 
2e417 72 75 6e 20 61 0a 20 20 2a 2a 20 73 65 70 61 72  run a.  ** separ
2e418 61 74 65 20 73 65 6c 65 63 74 20 61 63 72 6f 73  ate select acros
2e419 73 20 74 68 65 20 6c 65 61 66 20 6c 65 76 65 6c  s the leaf level
2e41a 20 6f 66 20 65 61 63 68 20 74 72 65 65 20 62 65   of each tree be
2e41b 69 6e 67 20 6d 65 72 67 65 64 2e 0a 20 20 2a 2f  ing merged..  */
2e41c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2e41d 2a 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74  *pLeafSelectStmt
2e41e 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54 5d 3b 0a  s[MERGE_COUNT];.
2e41f 20 20 2f 2a 20 54 68 65 20 73 74 61 74 65 6d 65    /* The stateme
2e420 6e 74 20 75 73 65 64 20 74 6f 20 70 72 65 70 61  nt used to prepa
2e421 72 65 20 70 4c 65 61 66 53 65 6c 65 63 74 53 74  re pLeafSelectSt
2e422 6d 74 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20  mts. */.#define 
2e423 4c 45 41 46 5f 53 45 4c 45 43 54 20 5c 0a 20 20  LEAF_SELECT \.  
2e424 22 73 65 6c 65 63 74 20 62 6c 6f 63 6b 20 66 72  "select block fr
2e425 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 20 77 68  om %_segments wh
2e426 65 72 65 20 62 6c 6f 63 6b 69 64 20 62 65 74 77  ere blockid betw
2e427 65 65 6e 20 3f 20 61 6e 64 20 3f 20 6f 72 64 65  een ? and ? orde
2e428 72 20 62 79 20 62 6c 6f 63 6b 69 64 22 0a 0a 20  r by blockid".. 
2e429 20 2f 2a 20 54 68 65 73 65 20 62 75 66 66 65 72   /* These buffer
2e42a 20 70 65 6e 64 69 6e 67 20 69 6e 64 65 78 20 75   pending index u
2e42b 70 64 61 74 65 73 20 64 75 72 69 6e 67 20 74 72  pdates during tr
2e42c 61 6e 73 61 63 74 69 6f 6e 73 2e 0a 20 20 2a 2a  ansactions..  **
2e42d 20 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 65 73   nPendingData es
2e42e 74 69 6d 61 74 65 73 20 74 68 65 20 6d 65 6d 6f  timates the memo
2e42f 72 79 20 73 69 7a 65 20 6f 66 20 74 68 65 20 70  ry size of the p
2e430 65 6e 64 69 6e 67 20 64 61 74 61 2e 20 20 49 74  ending data.  It
2e431 0a 20 20 2a 2a 20 64 6f 65 73 6e 27 74 20 69 6e  .  ** doesn't in
2e432 63 6c 75 64 65 20 74 68 65 20 68 61 73 68 2d 62  clude the hash-b
2e433 75 63 6b 65 74 20 6f 76 65 72 68 65 61 64 2c 20  ucket overhead, 
2e434 6e 6f 72 20 61 6e 79 20 6d 61 6c 6c 6f 63 0a 20  nor any malloc. 
2e435 20 2a 2a 20 6f 76 65 72 68 65 61 64 2e 20 20 57   ** overhead.  W
2e436 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74 61  hen nPendingData
2e437 20 65 78 63 65 65 64 73 20 6b 50 65 6e 64 69 6e   exceeds kPendin
2e438 67 54 68 72 65 73 68 6f 6c 64 2c 20 74 68 65 0a  gThreshold, the.
2e439 20 20 2a 2a 20 62 75 66 66 65 72 20 69 73 20 66    ** buffer is f
2e43a 6c 75 73 68 65 64 20 65 76 65 6e 20 62 65 66 6f  lushed even befo
2e43b 72 65 20 74 68 65 20 74 72 61 6e 73 61 63 74 69  re the transacti
2e43c 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 2a 2a 20  on closes..  ** 
2e43d 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 73 74 6f  pendingTerms sto
2e43e 72 65 73 20 74 68 65 20 64 61 74 61 2c 20 61 6e  res the data, an
2e43f 64 20 69 73 20 6f 6e 6c 79 20 76 61 6c 69 64 20  d is only valid 
2e440 77 68 65 6e 20 6e 50 65 6e 64 69 6e 67 44 61 74  when nPendingDat
2e441 61 0a 20 20 2a 2a 20 69 73 20 3e 3d 30 20 28 6e  a.  ** is >=0 (n
2e442 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 6d 65  PendingData<0 me
2e443 61 6e 73 20 70 65 6e 64 69 6e 67 54 65 72 6d 73  ans pendingTerms
2e444 20 68 61 73 20 6e 6f 74 20 62 65 65 6e 0a 20 20   has not been.  
2e445 2a 2a 20 69 6e 69 74 69 61 6c 69 7a 65 64 29 2e  ** initialized).
2e446 20 20 69 50 72 65 76 44 6f 63 69 64 20 69 73 20    iPrevDocid is 
2e447 74 68 65 20 6c 61 73 74 20 64 6f 63 69 64 20 77  the last docid w
2e448 72 69 74 74 65 6e 2c 20 75 73 65 64 20 74 6f 20  ritten, used to 
2e449 6d 61 6b 65 0a 20 20 2a 2a 20 63 65 72 74 61 69  make.  ** certai
2e44a 6e 20 77 65 27 72 65 20 69 6e 73 65 72 74 69 6e  n we're insertin
2e44b 67 20 69 6e 20 73 6f 72 74 65 64 20 6f 72 64 65  g in sorted orde
2e44c 72 2e 0a 20 20 2a 2f 0a 20 20 69 6e 74 20 6e 50  r..  */.  int nP
2e44d 65 6e 64 69 6e 67 44 61 74 61 3b 0a 23 64 65 66  endingData;.#def
2e44e 69 6e 65 20 6b 50 65 6e 64 69 6e 67 54 68 72 65  ine kPendingThre
2e44f 73 68 6f 6c 64 20 28 31 2a 31 30 32 34 2a 31 30  shold (1*1024*10
2e450 32 34 29 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74  24).  sqlite_int
2e451 36 34 20 69 50 72 65 76 44 6f 63 69 64 3b 0a 20  64 iPrevDocid;. 
2e452 20 66 74 73 33 48 61 73 68 20 70 65 6e 64 69 6e   fts3Hash pendin
2e453 67 54 65 72 6d 73 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a  gTerms;.};../*.*
2e454 2a 20 57 68 65 6e 20 74 68 65 20 63 6f 72 65 20  * When the core 
2e455 77 61 6e 74 73 20 74 6f 20 64 6f 20 61 20 71 75  wants to do a qu
2e456 65 72 79 2c 20 69 74 20 63 72 65 61 74 65 20 61  ery, it create a
2e457 20 63 75 72 73 6f 72 20 75 73 69 6e 67 20 61 0a   cursor using a.
2e458 2a 2a 20 63 61 6c 6c 20 74 6f 20 78 4f 70 65 6e  ** call to xOpen
2e459 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
2e45a 65 20 69 73 20 61 6e 20 69 6e 73 74 61 6e 63 65  e is an instance
2e45b 20 6f 66 20 61 20 63 75 72 73 6f 72 2e 20 20 49   of a cursor.  I
2e45c 74 0a 2a 2a 20 69 73 20 64 65 73 74 72 6f 79 65  t.** is destroye
2e45d 64 20 62 79 20 78 43 6c 6f 73 65 2e 0a 2a 2f 0a  d by xClose..*/.
2e45e 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 66  typedef struct f
2e45f 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 7b  ulltext_cursor {
2e460 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 5f  .  sqlite3_vtab_
2e461 63 75 72 73 6f 72 20 62 61 73 65 3b 20 20 20 20  cursor base;    
2e462 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
2e463 73 20 75 73 65 64 20 62 79 20 53 51 4c 69 74 65  s used by SQLite
2e464 20 63 6f 72 65 20 2a 2f 0a 20 20 51 75 65 72 79   core */.  Query
2e465 54 79 70 65 20 69 43 75 72 73 6f 72 54 79 70 65  Type iCursorType
2e466 3b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 43  ;           /* C
2e467 6f 70 79 20 6f 66 20 73 71 6c 69 74 65 33 5f 69  opy of sqlite3_i
2e468 6e 64 65 78 5f 69 6e 66 6f 2e 69 64 78 4e 75 6d  ndex_info.idxNum
2e469 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
2e46a 6d 74 20 2a 70 53 74 6d 74 3b 20 20 20 20 20 20  mt *pStmt;      
2e46b 20 20 20 20 20 20 20 2f 2a 20 50 72 65 70 61 72         /* Prepar
2e46c 65 64 20 73 74 61 74 65 6d 65 6e 74 20 69 6e 20  ed statement in 
2e46d 75 73 65 20 62 79 20 74 68 65 20 63 75 72 73 6f  use by the curso
2e46e 72 20 2a 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20  r */.  int eof; 
2e46f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e470 20 20 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20          /* True 
2e471 69 66 20 61 74 20 45 6e 64 20 4f 66 20 52 65 73  if at End Of Res
2e472 75 6c 74 73 20 2a 2f 0a 20 20 51 75 65 72 79 20  ults */.  Query 
2e473 71 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  q;              
2e474 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 50 61             /* Pa
2e475 72 73 65 64 20 71 75 65 72 79 20 73 74 72 69 6e  rsed query strin
2e476 67 20 2a 2f 0a 20 20 53 6e 69 70 70 65 74 20 73  g */.  Snippet s
2e477 6e 69 70 70 65 74 3b 20 20 20 20 20 20 20 20 20  nippet;         
2e478 20 20 20 20 20 20 20 20 2f 2a 20 43 61 63 68 65          /* Cache
2e479 64 20 73 6e 69 70 70 65 74 20 66 6f 72 20 74 68  d snippet for th
2e47a 65 20 63 75 72 72 65 6e 74 20 72 6f 77 20 2a 2f  e current row */
2e47b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e 3b 20  .  int iColumn; 
2e47c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e47d 20 20 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 62 65      /* Column be
2e47e 69 6e 67 20 73 65 61 72 63 68 65 64 20 2a 2f 0a  ing searched */.
2e47f 20 20 44 61 74 61 42 75 66 66 65 72 20 72 65 73    DataBuffer res
2e480 75 6c 74 3b 20 20 20 20 20 20 20 20 20 20 20 20  ult;            
2e481 20 20 20 2f 2a 20 44 6f 63 6c 69 73 74 20 72 65     /* Doclist re
2e482 73 75 6c 74 73 20 66 72 6f 6d 20 66 75 6c 6c 74  sults from fullt
2e483 65 78 74 51 75 65 72 79 20 2a 2f 0a 20 20 44 4c  extQuery */.  DL
2e484 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 20 20  Reader reader;  
2e485 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2e486 2a 20 52 65 73 75 6c 74 20 72 65 61 64 65 72 20  * Result reader 
2e487 69 66 20 72 65 73 75 6c 74 20 6e 6f 74 20 65 6d  if result not em
2e488 70 74 79 20 2a 2f 0a 7d 20 66 75 6c 6c 74 65 78  pty */.} fulltex
2e489 74 5f 63 75 72 73 6f 72 3b 0a 0a 73 74 61 74 69  t_cursor;..stati
2e48a 63 20 73 74 72 75 63 74 20 66 75 6c 6c 74 65 78  c struct fulltex
2e48b 74 5f 76 74 61 62 20 2a 63 75 72 73 6f 72 5f 76  t_vtab *cursor_v
2e48c 74 61 62 28 66 75 6c 6c 74 65 78 74 5f 63 75 72  tab(fulltext_cur
2e48d 73 6f 72 20 2a 63 29 7b 0a 20 20 72 65 74 75 72  sor *c){.  retur
2e48e 6e 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  n (fulltext_vtab
2e48f 20 2a 29 20 63 2d 3e 62 61 73 65 2e 70 56 74 61   *) c->base.pVta
2e490 62 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 63 6f 6e  b;.}..static con
2e491 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f 64 75 6c  st sqlite3_modul
2e492 65 20 66 74 73 33 4d 6f 64 75 6c 65 3b 20 20 20  e fts3Module;   
2e493 2f 2a 20 66 6f 72 77 61 72 64 20 64 65 63 6c 61  /* forward decla
2e494 72 61 74 69 6f 6e 20 2a 2f 0a 0a 2f 2a 20 52 65  ration */../* Re
2e495 74 75 72 6e 20 61 20 64 79 6e 61 6d 69 63 61 6c  turn a dynamical
2e496 6c 79 20 67 65 6e 65 72 61 74 65 64 20 73 74 61  ly generated sta
2e497 74 65 6d 65 6e 74 20 6f 66 20 74 68 65 20 66 6f  tement of the fo
2e498 72 6d 0a 20 2a 20 20 20 69 6e 73 65 72 74 20 69  rm. *   insert i
2e499 6e 74 6f 20 25 5f 63 6f 6e 74 65 6e 74 20 28 64  nto %_content (d
2e49a 6f 63 69 64 2c 20 2e 2e 2e 29 20 76 61 6c 75 65  ocid, ...) value
2e49b 73 20 28 3f 2c 20 2e 2e 2e 29 0a 20 2a 2f 0a 73  s (?, ...). */.s
2e49c 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2e49d 20 2a 63 6f 6e 74 65 6e 74 49 6e 73 65 72 74 53   *contentInsertS
2e49e 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78  tatement(fulltex
2e49f 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74  t_vtab *v){.  St
2e4a0 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20  ringBuffer sb;. 
2e4a1 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53   int i;..  initS
2e4a2 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29  tringBuffer(&sb)
2e4a3 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20  ;.  append(&sb, 
2e4a4 22 69 6e 73 65 72 74 20 69 6e 74 6f 20 25 5f 63  "insert into %_c
2e4a5 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64 2c 20 22  ontent (docid, "
2e4a6 29 3b 0a 20 20 61 70 70 65 6e 64 4c 69 73 74 28  );.  appendList(
2e4a7 26 73 62 2c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c  &sb, v->nColumn,
2e4a8 20 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c   v->azContentCol
2e4a9 75 6d 6e 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  umn);.  append(&
2e4aa 73 62 2c 20 22 29 20 76 61 6c 75 65 73 20 28 3f  sb, ") values (?
2e4ab 22 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69  ");.  for(i=0; i
2e4ac 3c 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69  <v->nColumn; ++i
2e4ad 29 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62  ).    append(&sb
2e4ae 2c 20 22 2c 20 3f 22 29 3b 0a 20 20 61 70 70 65  , ", ?");.  appe
2e4af 6e 64 28 26 73 62 2c 20 22 29 22 29 3b 0a 20 20  nd(&sb, ")");.  
2e4b0 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42 75 66  return stringBuf
2e4b1 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a 7d 0a  ferData(&sb);.}.
2e4b2 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 64 79 6e  ./* Return a dyn
2e4b3 61 6d 69 63 61 6c 6c 79 20 67 65 6e 65 72 61 74  amically generat
2e4b4 65 64 20 73 74 61 74 65 6d 65 6e 74 20 6f 66 20  ed statement of 
2e4b5 74 68 65 20 66 6f 72 6d 0a 20 2a 20 20 20 73 65  the form. *   se
2e4b6 6c 65 63 74 20 3c 63 6f 6e 74 65 6e 74 20 63 6f  lect <content co
2e4b7 6c 75 6d 6e 73 3e 20 66 72 6f 6d 20 25 5f 63 6f  lumns> from %_co
2e4b8 6e 74 65 6e 74 20 77 68 65 72 65 20 64 6f 63 69  ntent where doci
2e4b9 64 20 3d 20 3f 0a 20 2a 2f 0a 73 74 61 74 69 63  d = ?. */.static
2e4ba 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
2e4bb 74 65 6e 74 53 65 6c 65 63 74 53 74 61 74 65 6d  tentSelectStatem
2e4bc 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ent(fulltext_vta
2e4bd 62 20 2a 76 29 7b 0a 20 20 53 74 72 69 6e 67 42  b *v){.  StringB
2e4be 75 66 66 65 72 20 73 62 3b 0a 20 20 69 6e 69 74  uffer sb;.  init
2e4bf 53 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62  StringBuffer(&sb
2e4c0 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c  );.  append(&sb,
2e4c1 20 22 53 45 4c 45 43 54 20 22 29 3b 0a 20 20 61   "SELECT ");.  a
2e4c2 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20 76  ppendList(&sb, v
2e4c3 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61 7a  ->nColumn, v->az
2e4c4 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a  ContentColumn);.
2e4c5 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20 22 20    append(&sb, " 
2e4c6 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e 74 20 57  FROM %_content W
2e4c7 48 45 52 45 20 64 6f 63 69 64 20 3d 20 3f 22 29  HERE docid = ?")
2e4c8 3b 0a 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e  ;.  return strin
2e4c9 67 42 75 66 66 65 72 44 61 74 61 28 26 73 62 29  gBufferData(&sb)
2e4ca 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61  ;.}../* Return a
2e4cb 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20 67 65 6e   dynamically gen
2e4cc 65 72 61 74 65 64 20 73 74 61 74 65 6d 65 6e 74  erated statement
2e4cd 20 6f 66 20 74 68 65 20 66 6f 72 6d 0a 20 2a 20   of the form. * 
2e4ce 20 20 75 70 64 61 74 65 20 25 5f 63 6f 6e 74 65    update %_conte
2e4cf 6e 74 20 73 65 74 20 5b 63 6f 6c 5f 30 5d 20 3d  nt set [col_0] =
2e4d0 20 3f 2c 20 5b 63 6f 6c 5f 31 5d 20 3d 20 3f 2c   ?, [col_1] = ?,
2e4d1 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20   .... *         
2e4d2 20 20 20 20 20 20 20 20 20 20 20 77 68 65 72 65             where
2e4d3 20 64 6f 63 69 64 20 3d 20 3f 0a 20 2a 2f 0a 73   docid = ?. */.s
2e4d4 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72  tatic const char
2e4d5 20 2a 63 6f 6e 74 65 6e 74 55 70 64 61 74 65 53   *contentUpdateS
2e4d6 74 61 74 65 6d 65 6e 74 28 66 75 6c 6c 74 65 78  tatement(fulltex
2e4d7 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 53 74  t_vtab *v){.  St
2e4d8 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20  ringBuffer sb;. 
2e4d9 20 69 6e 74 20 69 3b 0a 0a 20 20 69 6e 69 74 53   int i;..  initS
2e4da 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 62 29  tringBuffer(&sb)
2e4db 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20  ;.  append(&sb, 
2e4dc 22 75 70 64 61 74 65 20 25 5f 63 6f 6e 74 65 6e  "update %_conten
2e4dd 74 20 73 65 74 20 22 29 3b 0a 20 20 66 6f 72 28  t set ");.  for(
2e4de 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75 6d  i=0; i<v->nColum
2e4df 6e 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66  n; ++i) {.    if
2e4e0 28 20 69 3e 30 20 29 7b 0a 20 20 20 20 20 20 61  ( i>0 ){.      a
2e4e1 70 70 65 6e 64 28 26 73 62 2c 20 22 2c 20 22 29  ppend(&sb, ", ")
2e4e2 3b 0a 20 20 20 20 7d 0a 20 20 20 20 61 70 70 65  ;.    }.    appe
2e4e3 6e 64 28 26 73 62 2c 20 76 2d 3e 61 7a 43 6f 6e  nd(&sb, v->azCon
2e4e4 74 65 6e 74 43 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a  tentColumn[i]);.
2e4e5 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c 20      append(&sb, 
2e4e6 22 20 3d 20 3f 22 29 3b 0a 20 20 7d 0a 20 20 61  " = ?");.  }.  a
2e4e7 70 70 65 6e 64 28 26 73 62 2c 20 22 20 77 68 65  ppend(&sb, " whe
2e4e8 72 65 20 64 6f 63 69 64 20 3d 20 3f 22 29 3b 0a  re docid = ?");.
2e4e9 20 20 72 65 74 75 72 6e 20 73 74 72 69 6e 67 42    return stringB
2e4ea 75 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a  ufferData(&sb);.
2e4eb 7d 0a 0a 2f 2a 20 50 75 74 73 20 61 20 66 72 65  }../* Puts a fre
2e4ec 73 68 6c 79 2d 70 72 65 70 61 72 65 64 20 73 74  shly-prepared st
2e4ed 61 74 65 6d 65 6e 74 20 64 65 74 65 72 6d 69 6e  atement determin
2e4ee 65 64 20 62 79 20 69 53 74 6d 74 20 69 6e 20 2a  ed by iStmt in *
2e4ef 70 70 53 74 6d 74 2e 0a 2a 2a 20 49 66 20 74 68  ppStmt..** If th
2e4f0 65 20 69 6e 64 69 63 61 74 65 64 20 73 74 61 74  e indicated stat
2e4f1 65 6d 65 6e 74 20 68 61 73 20 6e 65 76 65 72 20  ement has never 
2e4f2 62 65 65 6e 20 70 72 65 70 61 72 65 64 2c 20 69  been prepared, i
2e4f3 74 20 69 73 20 70 72 65 70 61 72 65 64 0a 2a 2a  t is prepared.**
2e4f4 20 61 6e 64 20 63 61 63 68 65 64 2c 20 6f 74 68   and cached, oth
2e4f5 65 72 77 69 73 65 20 74 68 65 20 63 61 63 68 65  erwise the cache
2e4f6 64 20 76 65 72 73 69 6f 6e 20 69 73 20 72 65 73  d version is res
2e4f7 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  et..*/.static in
2e4f8 74 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d  t sql_get_statem
2e4f9 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ent(fulltext_vta
2e4fa 62 20 2a 76 2c 20 66 75 6c 6c 74 65 78 74 5f 73  b *v, fulltext_s
2e4fb 74 61 74 65 6d 65 6e 74 20 69 53 74 6d 74 2c 0a  tatement iStmt,.
2e4fc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e4fd 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c               sql
2e4fe 69 74 65 33 5f 73 74 6d 74 20 2a 2a 70 70 53 74  ite3_stmt **ppSt
2e4ff 6d 74 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  mt){.  assert( i
2e500 53 74 6d 74 3c 4d 41 58 5f 53 54 4d 54 20 29 3b  Stmt<MAX_STMT );
2e501 0a 20 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c 74  .  if( v->pFullt
2e502 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53  extStatements[iS
2e503 74 6d 74 5d 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20  tmt]==NULL ){.  
2e504 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 53    const char *zS
2e505 74 6d 74 3b 0a 20 20 20 20 69 6e 74 20 72 63 3b  tmt;.    int rc;
2e506 0a 20 20 20 20 73 77 69 74 63 68 28 20 69 53 74  .    switch( iSt
2e507 6d 74 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  mt ){.      case
2e508 20 43 4f 4e 54 45 4e 54 5f 49 4e 53 45 52 54 5f   CONTENT_INSERT_
2e509 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20 7a 53  STMT:.        zS
2e50a 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 49 6e 73  tmt = contentIns
2e50b 65 72 74 53 74 61 74 65 6d 65 6e 74 28 76 29 3b  ertStatement(v);
2e50c 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
2e50d 73 65 20 43 4f 4e 54 45 4e 54 5f 53 45 4c 45 43  se CONTENT_SELEC
2e50e 54 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20 20 20  T_STMT:.        
2e50f 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e 74 53  zStmt = contentS
2e510 65 6c 65 63 74 53 74 61 74 65 6d 65 6e 74 28 76  electStatement(v
2e511 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ); break;.      
2e512 63 61 73 65 20 43 4f 4e 54 45 4e 54 5f 55 50 44  case CONTENT_UPD
2e513 41 54 45 5f 53 54 4d 54 3a 0a 20 20 20 20 20 20  ATE_STMT:.      
2e514 20 20 7a 53 74 6d 74 20 3d 20 63 6f 6e 74 65 6e    zStmt = conten
2e515 74 55 70 64 61 74 65 53 74 61 74 65 6d 65 6e 74  tUpdateStatement
2e516 28 76 29 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  (v); break;.    
2e517 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
2e518 20 20 20 7a 53 74 6d 74 20 3d 20 66 75 6c 6c 74     zStmt = fullt
2e519 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74 5b 69  ext_zStatement[i
2e51a 53 74 6d 74 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  Stmt];.    }.   
2e51b 20 72 63 20 3d 20 73 71 6c 5f 70 72 65 70 61 72   rc = sql_prepar
2e51c 65 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c  e(v->db, v->zDb,
2e51d 20 76 2d 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70   v->zName, &v->p
2e51e 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
2e51f 74 73 5b 69 53 74 6d 74 5d 2c 0a 20 20 20 20 20  ts[iStmt],.     
2e520 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e521 20 20 20 20 7a 53 74 6d 74 29 3b 0a 20 20 20 20      zStmt);.    
2e522 69 66 28 20 7a 53 74 6d 74 20 21 3d 20 66 75 6c  if( zStmt != ful
2e523 6c 74 65 78 74 5f 7a 53 74 61 74 65 6d 65 6e 74  ltext_zStatement
2e524 5b 69 53 74 6d 74 5d 29 20 73 71 6c 69 74 65 33  [iStmt]) sqlite3
2e525 5f 66 72 65 65 28 28 76 6f 69 64 20 2a 29 20 7a  _free((void *) z
2e526 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28 20 72  Stmt);.    if( r
2e527 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2e528 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 20 65 6c  eturn rc;.  } el
2e529 73 65 20 7b 0a 20 20 20 20 69 6e 74 20 72 63 20  se {.    int rc 
2e52a 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
2e52b 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
2e52c 65 6d 65 6e 74 73 5b 69 53 74 6d 74 5d 29 3b 0a  ements[iStmt]);.
2e52d 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2e52e 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2e52f 63 3b 0a 20 20 7d 0a 0a 20 20 2a 70 70 53 74 6d  c;.  }..  *ppStm
2e530 74 20 3d 20 76 2d 3e 70 46 75 6c 6c 74 65 78 74  t = v->pFulltext
2e531 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74 6d 74  Statements[iStmt
2e532 5d 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  ];.  return SQLI
2e533 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b  TE_OK;.}../* Lik
2e534 65 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  e sqlite3_step()
2e535 2c 20 62 75 74 20 63 6f 6e 76 65 72 74 20 53 51  , but convert SQ
2e536 4c 49 54 45 5f 44 4f 4e 45 20 74 6f 20 53 51 4c  LITE_DONE to SQL
2e537 49 54 45 5f 4f 4b 20 61 6e 64 0a 2a 2a 20 53 51  ITE_OK and.** SQ
2e538 4c 49 54 45 5f 52 4f 57 20 74 6f 20 53 51 4c 49  LITE_ROW to SQLI
2e539 54 45 5f 45 52 52 4f 52 2e 20 20 55 73 65 66 75  TE_ERROR.  Usefu
2e53a 6c 20 66 6f 72 20 73 74 61 74 65 6d 65 6e 74 73  l for statements
2e53b 20 6c 69 6b 65 20 55 50 44 41 54 45 2c 0a 2a 2a   like UPDATE,.**
2e53c 20 77 68 65 72 65 20 77 65 20 65 78 70 65 63 74   where we expect
2e53d 20 6e 6f 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a   no results..*/.
2e53e 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c 5f 73  static int sql_s
2e53f 69 6e 67 6c 65 5f 73 74 65 70 28 73 71 6c 69 74  ingle_step(sqlit
2e540 65 33 5f 73 74 6d 74 20 2a 73 29 7b 0a 20 20 69  e3_stmt *s){.  i
2e541 6e 74 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  nt rc = sqlite3_
2e542 73 74 65 70 28 73 29 3b 0a 20 20 72 65 74 75 72  step(s);.  retur
2e543 6e 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f  n (rc==SQLITE_DO
2e544 4e 45 29 20 3f 20 53 51 4c 49 54 45 5f 4f 4b 20  NE) ? SQLITE_OK 
2e545 3a 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b 65  : rc;.}../* Like
2e546 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
2e547 6e 74 28 29 2c 20 62 75 74 20 66 6f 72 20 73 70  nt(), but for sp
2e548 65 63 69 61 6c 20 72 65 70 6c 69 63 61 74 65 64  ecial replicated
2e549 20 4c 45 41 46 5f 53 45 4c 45 43 54 0a 2a 2a 20   LEAF_SELECT.** 
2e54a 73 74 61 74 65 6d 65 6e 74 73 2e 20 20 69 64 78  statements.  idx
2e54b 20 2d 31 20 69 73 20 61 20 73 70 65 63 69 61 6c   -1 is a special
2e54c 20 63 61 73 65 20 66 6f 72 20 61 6e 20 75 6e 63   case for an unc
2e54d 61 63 68 65 64 20 76 65 72 73 69 6f 6e 20 6f 66  ached version of
2e54e 0a 2a 2a 20 74 68 65 20 73 74 61 74 65 6d 65 6e  .** the statemen
2e54f 74 20 28 75 73 65 64 20 69 6e 20 74 68 65 20 6f  t (used in the o
2e550 70 74 69 6d 69 7a 65 20 69 6d 70 6c 65 6d 65 6e  ptimize implemen
2e551 74 61 74 69 6f 6e 29 2e 0a 2a 2f 0a 2f 2a 20 54  tation)..*/./* T
2e552 4f 44 4f 28 73 68 65 73 73 29 20 57 72 69 74 65  ODO(shess) Write
2e553 20 76 65 72 73 69 6f 6e 20 66 6f 72 20 67 65 6e   version for gen
2e554 65 72 69 63 20 73 74 61 74 65 6d 65 6e 74 73 20  eric statements 
2e555 61 6e 64 20 74 68 65 6e 20 73 68 61 72 65 0a 2a  and then share.*
2e556 2a 20 74 68 61 74 20 62 65 74 77 65 65 6e 20 74  * that between t
2e557 68 65 20 63 61 63 68 65 64 2d 73 74 61 74 65 6d  he cached-statem
2e558 65 6e 74 20 66 75 6e 63 74 69 6f 6e 73 2e 0a 2a  ent functions..*
2e559 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 71 6c  /.static int sql
2e55a 5f 67 65 74 5f 6c 65 61 66 5f 73 74 61 74 65 6d  _get_leaf_statem
2e55b 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ent(fulltext_vta
2e55c 62 20 2a 76 2c 20 69 6e 74 20 69 64 78 2c 0a 20  b *v, int idx,. 
2e55d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e55e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e55f 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 2a   sqlite3_stmt **
2e560 70 70 53 74 6d 74 29 7b 0a 20 20 61 73 73 65 72  ppStmt){.  asser
2e561 74 28 20 69 64 78 3e 3d 2d 31 20 26 26 20 69 64  t( idx>=-1 && id
2e562 78 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 29 3b  x<MERGE_COUNT );
2e563 0a 20 20 69 66 28 20 69 64 78 3d 3d 2d 31 20 29  .  if( idx==-1 )
2e564 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 73 71 6c  {.    return sql
2e565 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62 2c 20  _prepare(v->db, 
2e566 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65  v->zDb, v->zName
2e567 2c 20 70 70 53 74 6d 74 2c 20 4c 45 41 46 5f 53  , ppStmt, LEAF_S
2e568 45 4c 45 43 54 29 3b 0a 20 20 7d 65 6c 73 65 20  ELECT);.  }else 
2e569 69 66 28 20 76 2d 3e 70 4c 65 61 66 53 65 6c 65  if( v->pLeafSele
2e56a 63 74 53 74 6d 74 73 5b 69 64 78 5d 3d 3d 4e 55  ctStmts[idx]==NU
2e56b 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  LL ){.    int rc
2e56c 20 3d 20 73 71 6c 5f 70 72 65 70 61 72 65 28 76   = sql_prepare(v
2e56d 2d 3e 64 62 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d  ->db, v->zDb, v-
2e56e 3e 7a 4e 61 6d 65 2c 20 26 76 2d 3e 70 4c 65 61  >zName, &v->pLea
2e56f 66 53 65 6c 65 63 74 53 74 6d 74 73 5b 69 64 78  fSelectStmts[idx
2e570 5d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ],.             
2e571 20 20 20 20 20 20 20 20 20 20 20 20 4c 45 41 46              LEAF
2e572 5f 53 45 4c 45 43 54 29 3b 0a 20 20 20 20 69 66  _SELECT);.    if
2e573 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2e574 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d  ) return rc;.  }
2e575 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 20 72 63  else{.    int rc
2e576 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
2e577 28 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53  (v->pLeafSelectS
2e578 74 6d 74 73 5b 69 64 78 5d 29 3b 0a 20 20 20 20  tmts[idx]);.    
2e579 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2e57a 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20  K ) return rc;. 
2e57b 20 7d 0a 0a 20 20 2a 70 70 53 74 6d 74 20 3d 20   }..  *ppStmt = 
2e57c 76 2d 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74  v->pLeafSelectSt
2e57d 6d 74 73 5b 69 64 78 5d 3b 0a 20 20 72 65 74 75  mts[idx];.  retu
2e57e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2e57f 0a 2f 2a 20 69 6e 73 65 72 74 20 69 6e 74 6f 20  ./* insert into 
2e580 25 5f 63 6f 6e 74 65 6e 74 20 28 64 6f 63 69 64  %_content (docid
2e581 2c 20 2e 2e 2e 29 20 76 61 6c 75 65 73 20 28 5b  , ...) values ([
2e582 64 6f 63 69 64 5d 2c 20 5b 70 56 61 6c 75 65 73  docid], [pValues
2e583 5d 29 0a 2a 2a 20 49 66 20 74 68 65 20 64 6f 63  ]).** If the doc
2e584 69 64 20 63 6f 6e 74 61 69 6e 73 20 53 51 4c 20  id contains SQL 
2e585 4e 55 4c 4c 2c 20 74 68 65 6e 20 61 20 75 6e 69  NULL, then a uni
2e586 71 75 65 20 64 6f 63 69 64 20 77 69 6c 6c 20 62  que docid will b
2e587 65 0a 2a 2a 20 67 65 6e 65 72 61 74 65 64 2e 0a  e.** generated..
2e588 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
2e589 6e 74 65 6e 74 5f 69 6e 73 65 72 74 28 66 75 6c  ntent_insert(ful
2e58a 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
2e58b 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 64 6f  qlite3_value *do
2e58c 63 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20  cid,.           
2e58d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2e58e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
2e58f 56 61 6c 75 65 73 29 7b 0a 20 20 73 71 6c 69 74  Values){.  sqlit
2e590 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e  e3_stmt *s;.  in
2e591 74 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20  t i;.  int rc = 
2e592 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e  sql_get_statemen
2e593 74 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 49 4e 53  t(v, CONTENT_INS
2e594 45 52 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ERT_STMT, &s);. 
2e595 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2e596 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2e597 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2e598 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2c  bind_value(s, 1,
2e599 20 64 6f 63 69 64 29 3b 0a 20 20 69 66 28 20 72   docid);.  if( r
2e59a 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2e59b 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 66 6f 72  eturn rc;..  for
2e59c 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f 6c 75  (i=0; i<v->nColu
2e59d 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20 72 63  mn; ++i){.    rc
2e59e 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f   = sqlite3_bind_
2e59f 76 61 6c 75 65 28 73 2c 20 32 2b 69 2c 20 70 56  value(s, 2+i, pV
2e5a0 61 6c 75 65 73 5b 69 5d 29 3b 0a 20 20 20 20 69  alues[i]);.    i
2e5a1 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2e5a2 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
2e5a3 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 5f  }..  return sql_
2e5a4 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b 0a  single_step(s);.
2e5a5 7d 0a 0a 2f 2a 20 75 70 64 61 74 65 20 25 5f 63  }../* update %_c
2e5a6 6f 6e 74 65 6e 74 20 73 65 74 20 63 6f 6c 30 20  ontent set col0 
2e5a7 3d 20 70 56 61 6c 75 65 73 5b 30 5d 2c 20 63 6f  = pValues[0], co
2e5a8 6c 31 20 3d 20 70 56 61 6c 75 65 73 5b 31 5d 2c  l1 = pValues[1],
2e5a9 20 2e 2e 2e 0a 20 2a 20 20 20 20 20 20 20 20 20   .... *         
2e5aa 20 20 20 20 20 20 20 20 20 77 68 65 72 65 20 64           where d
2e5ab 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69 64 5d 20  ocid = [iDocid] 
2e5ac 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f  */.static int co
2e5ad 6e 74 65 6e 74 5f 75 70 64 61 74 65 28 66 75 6c  ntent_update(ful
2e5ae 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
2e5af 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 70  qlite3_value **p
2e5b0 56 61 6c 75 65 73 2c 0a 20 20 20 20 20 20 20 20  Values,.        
2e5b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e5b2 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
2e5b3 44 6f 63 69 64 29 7b 0a 20 20 73 71 6c 69 74 65  Docid){.  sqlite
2e5b4 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74  3_stmt *s;.  int
2e5b5 20 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73   i;.  int rc = s
2e5b6 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2e5b7 28 76 2c 20 43 4f 4e 54 45 4e 54 5f 55 50 44 41  (v, CONTENT_UPDA
2e5b8 54 45 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  TE_STMT, &s);.  
2e5b9 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2e5ba 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2e5bb 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e    for(i=0; i<v->
2e5bc 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20  nColumn; ++i){. 
2e5bd 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
2e5be 62 69 6e 64 5f 76 61 6c 75 65 28 73 2c 20 31 2b  bind_value(s, 1+
2e5bf 69 2c 20 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a  i, pValues[i]);.
2e5c0 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2e5c1 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2e5c2 63 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20 73  c;.  }..  rc = s
2e5c3 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
2e5c4 34 28 73 2c 20 31 2b 76 2d 3e 6e 43 6f 6c 75 6d  4(s, 1+v->nColum
2e5c5 6e 2c 20 69 44 6f 63 69 64 29 3b 0a 20 20 69 66  n, iDocid);.  if
2e5c6 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2e5c7 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2e5c8 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67 6c  return sql_singl
2e5c9 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 73 74  e_step(s);.}..st
2e5ca 61 74 69 63 20 76 6f 69 64 20 66 72 65 65 53 74  atic void freeSt
2e5cb 72 69 6e 67 41 72 72 61 79 28 69 6e 74 20 6e 53  ringArray(int nS
2e5cc 74 72 69 6e 67 2c 20 63 6f 6e 73 74 20 63 68 61  tring, const cha
2e5cd 72 20 2a 2a 70 53 74 72 69 6e 67 29 7b 0a 20 20  r **pString){.  
2e5ce 69 6e 74 20 69 3b 0a 0a 20 20 66 6f 72 20 28 69  int i;..  for (i
2e5cf 3d 30 20 3b 20 69 20 3c 20 6e 53 74 72 69 6e 67  =0 ; i < nString
2e5d0 20 3b 20 2b 2b 69 29 20 7b 0a 20 20 20 20 69 66   ; ++i) {.    if
2e5d1 28 20 70 53 74 72 69 6e 67 5b 69 5d 21 3d 4e 55  ( pString[i]!=NU
2e5d2 4c 4c 20 29 20 73 71 6c 69 74 65 33 5f 66 72 65  LL ) sqlite3_fre
2e5d3 65 28 28 76 6f 69 64 20 2a 29 20 70 53 74 72 69  e((void *) pStri
2e5d4 6e 67 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 73 71  ng[i]);.  }.  sq
2e5d5 6c 69 74 65 33 5f 66 72 65 65 28 28 76 6f 69 64  lite3_free((void
2e5d6 20 2a 29 20 70 53 74 72 69 6e 67 29 3b 0a 7d 0a   *) pString);.}.
2e5d7 0a 2f 2a 20 73 65 6c 65 63 74 20 2a 20 66 72 6f  ./* select * fro
2e5d8 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77 68 65 72  m %_content wher
2e5d9 65 20 64 6f 63 69 64 20 3d 20 5b 69 44 6f 63 69  e docid = [iDoci
2e5da 64 5d 0a 20 2a 20 54 68 65 20 63 61 6c 6c 65 72  d]. * The caller
2e5db 20 6d 75 73 74 20 64 65 6c 65 74 65 20 74 68 65   must delete the
2e5dc 20 72 65 74 75 72 6e 65 64 20 61 72 72 61 79 20   returned array 
2e5dd 61 6e 64 20 61 6c 6c 20 73 74 72 69 6e 67 73 20  and all strings 
2e5de 69 6e 20 69 74 2e 0a 20 2a 20 6e 75 6c 6c 20 66  in it.. * null f
2e5df 69 65 6c 64 73 20 77 69 6c 6c 20 62 65 20 4e 55  ields will be NU
2e5e0 4c 4c 20 69 6e 20 74 68 65 20 72 65 74 75 72 6e  LL in the return
2e5e1 65 64 20 61 72 72 61 79 2e 0a 20 2a 0a 20 2a 20  ed array.. *. * 
2e5e2 54 4f 44 4f 3a 20 50 65 72 68 61 70 73 20 77 65  TODO: Perhaps we
2e5e3 20 73 68 6f 75 6c 64 20 72 65 74 75 72 6e 20 70   should return p
2e5e4 6f 69 6e 74 65 72 2f 6c 65 6e 67 74 68 20 73 74  ointer/length st
2e5e5 72 69 6e 67 73 20 68 65 72 65 20 66 6f 72 20 63  rings here for c
2e5e6 6f 6e 73 69 73 74 65 6e 63 79 0a 20 2a 20 77 69  onsistency. * wi
2e5e7 74 68 20 6f 74 68 65 72 20 63 6f 64 65 20 77 68  th other code wh
2e5e8 69 63 68 20 75 73 65 73 20 70 6f 69 6e 74 65 72  ich uses pointer
2e5e9 2f 6c 65 6e 67 74 68 2e 20 2a 2f 0a 73 74 61 74  /length. */.stat
2e5ea 69 63 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 73  ic int content_s
2e5eb 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74 5f 76  elect(fulltext_v
2e5ec 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
2e5ed 6e 74 36 34 20 69 44 6f 63 69 64 2c 0a 20 20 20  nt64 iDocid,.   
2e5ee 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e5ef 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
2e5f0 72 20 2a 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20  r ***pValues){. 
2e5f1 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
2e5f2 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
2e5f3 2a 76 61 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69  *values;.  int i
2e5f4 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 2a  ;.  int rc;..  *
2e5f5 70 56 61 6c 75 65 73 20 3d 20 4e 55 4c 4c 3b 0a  pValues = NULL;.
2e5f6 0a 20 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  .  rc = sql_get_
2e5f7 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43 4f 4e  statement(v, CON
2e5f8 54 45 4e 54 5f 53 45 4c 45 43 54 5f 53 54 4d 54  TENT_SELECT_STMT
2e5f9 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
2e5fa 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2e5fb 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2e5fc 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
2e5fd 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69 64 29  64(s, 1, iDocid)
2e5fe 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2e5ff 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2e600 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74  c;..  rc = sqlit
2e601 65 33 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66  e3_step(s);.  if
2e602 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc!=SQLITE_ROW
2e603 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2e604 20 76 61 6c 75 65 73 20 3d 20 28 63 6f 6e 73 74   values = (const
2e605 20 63 68 61 72 20 2a 2a 29 20 73 71 6c 69 74 65   char **) sqlite
2e606 33 5f 6d 61 6c 6c 6f 63 28 76 2d 3e 6e 43 6f 6c  3_malloc(v->nCol
2e607 75 6d 6e 20 2a 20 73 69 7a 65 6f 66 28 63 6f 6e  umn * sizeof(con
2e608 73 74 20 63 68 61 72 20 2a 29 29 3b 0a 20 20 66  st char *));.  f
2e609 6f 72 28 69 3d 30 3b 20 69 3c 76 2d 3e 6e 43 6f  or(i=0; i<v->nCo
2e60a 6c 75 6d 6e 3b 20 2b 2b 69 29 7b 0a 20 20 20 20  lumn; ++i){.    
2e60b 69 66 28 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  if( sqlite3_colu
2e60c 6d 6e 5f 74 79 70 65 28 73 2c 20 69 29 3d 3d 53  mn_type(s, i)==S
2e60d 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a 20 20  QLITE_NULL ){.  
2e60e 20 20 20 20 76 61 6c 75 65 73 5b 69 5d 20 3d 20      values[i] = 
2e60f 4e 55 4c 4c 3b 0a 20 20 20 20 7d 65 6c 73 65 7b  NULL;.    }else{
2e610 0a 20 20 20 20 20 20 76 61 6c 75 65 73 5b 69 5d  .      values[i]
2e611 20 3d 20 73 74 72 69 6e 67 5f 64 75 70 28 28 63   = string_dup((c
2e612 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c  har*)sqlite3_col
2e613 75 6d 6e 5f 74 65 78 74 28 73 2c 20 69 29 29 3b  umn_text(s, i));
2e614 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a  .    }.  }..  /*
2e615 20 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20   We expect only 
2e616 6f 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73  one row.  We mus
2e617 74 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65  t execute anothe
2e618 72 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29  r sqlite3_step()
2e619 0a 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74  .   * to complet
2e61a 65 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b  e the iteration;
2e61b 20 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74   otherwise the t
2e61c 61 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e  able will remain
2e61d 20 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63   locked. */.  rc
2e61e 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
2e61f 73 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51  s);.  if( rc==SQ
2e620 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20  LITE_DONE ){.   
2e621 20 2a 70 56 61 6c 75 65 73 20 3d 20 76 61 6c 75   *pValues = valu
2e622 65 73 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53  es;.    return S
2e623 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20  QLITE_OK;.  }.. 
2e624 20 66 72 65 65 53 74 72 69 6e 67 41 72 72 61 79   freeStringArray
2e625 28 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 61 6c  (v->nColumn, val
2e626 75 65 73 29 3b 0a 20 20 72 65 74 75 72 6e 20 72  ues);.  return r
2e627 63 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20  c;.}../* delete 
2e628 66 72 6f 6d 20 25 5f 63 6f 6e 74 65 6e 74 20 77  from %_content w
2e629 68 65 72 65 20 64 6f 63 69 64 20 3d 20 5b 69 44  here docid = [iD
2e62a 6f 63 69 64 20 5d 20 2a 2f 0a 73 74 61 74 69 63  ocid ] */.static
2e62b 20 69 6e 74 20 63 6f 6e 74 65 6e 74 5f 64 65 6c   int content_del
2e62c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  ete(fulltext_vta
2e62d 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74  b *v, sqlite_int
2e62e 36 34 20 69 44 6f 63 69 64 29 7b 0a 20 20 73 71  64 iDocid){.  sq
2e62f 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
2e630 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
2e631 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 43  t_statement(v, C
2e632 4f 4e 54 45 4e 54 5f 44 45 4c 45 54 45 5f 53 54  ONTENT_DELETE_ST
2e633 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72  MT, &s);.  if( r
2e634 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2e635 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20  eturn rc;..  rc 
2e636 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69  = sqlite3_bind_i
2e637 6e 74 36 34 28 73 2c 20 31 2c 20 69 44 6f 63 69  nt64(s, 1, iDoci
2e638 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  d);.  if( rc!=SQ
2e639 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2e63a 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73   rc;..  return s
2e63b 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73  ql_single_step(s
2e63c 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73  );.}../* Returns
2e63d 20 53 51 4c 49 54 45 5f 52 4f 57 20 69 66 20 61   SQLITE_ROW if a
2e63e 6e 79 20 72 6f 77 73 20 65 78 69 73 74 20 69 6e  ny rows exist in
2e63f 20 25 5f 63 6f 6e 74 65 6e 74 2c 20 53 51 4c 49   %_content, SQLI
2e640 54 45 5f 44 4f 4e 45 20 69 66 0a 2a 2a 20 6e 6f  TE_DONE if.** no
2e641 20 72 6f 77 73 20 65 78 69 73 74 2c 20 61 6e 64   rows exist, and
2e642 20 61 6e 79 20 65 72 72 6f 72 20 69 6e 20 63 61   any error in ca
2e643 73 65 20 6f 66 20 66 61 69 6c 75 72 65 2e 0a 2a  se of failure..*
2e644 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63 6f 6e  /.static int con
2e645 74 65 6e 74 5f 65 78 69 73 74 73 28 66 75 6c 6c  tent_exists(full
2e646 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20  text_vtab *v){. 
2e647 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
2e648 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
2e649 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
2e64a 2c 20 43 4f 4e 54 45 4e 54 5f 45 58 49 53 54 53  , CONTENT_EXISTS
2e64b 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
2e64c 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2e64d 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2e64e 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
2e64f 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  p(s);.  if( rc!=
2e650 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
2e651 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20 57 65  urn rc;..  /* We
2e652 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65   expect only one
2e653 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20 65   row.  We must e
2e654 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20 73  xecute another s
2e655 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20 20  qlite3_step().  
2e656 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74   * to complete t
2e657 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74  he iteration; ot
2e658 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c  herwise the tabl
2e659 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f  e will remain lo
2e65a 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d 20  cked. */.  rc = 
2e65b 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
2e65c 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54  .  if( rc==SQLIT
2e65d 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20  E_DONE ) return 
2e65e 53 51 4c 49 54 45 5f 52 4f 57 3b 0a 20 20 69 66  SQLITE_ROW;.  if
2e65f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  ( rc==SQLITE_ROW
2e660 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45   ) return SQLITE
2e661 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74 75 72 6e  _ERROR;.  return
2e662 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73 65 72   rc;.}../* inser
2e663 74 20 69 6e 74 6f 20 25 5f 73 65 67 6d 65 6e 74  t into %_segment
2e664 73 20 76 61 6c 75 65 73 20 28 5b 70 44 61 74 61  s values ([pData
2e665 5d 29 0a 2a 2a 20 20 20 72 65 74 75 72 6e 73 20  ]).**   returns 
2e666 61 73 73 69 67 6e 65 64 20 62 6c 6f 63 6b 69 64  assigned blockid
2e667 20 69 6e 20 2a 70 69 42 6c 6f 63 6b 69 64 0a 2a   in *piBlockid.*
2e668 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f  /.static int blo
2e669 63 6b 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65  ck_insert(fullte
2e66a 78 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73  xt_vtab *v, cons
2e66b 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69  t char *pData, i
2e66c 6e 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20  nt nData,.      
2e66d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e66e 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a    sqlite_int64 *
2e66f 70 69 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71  piBlockid){.  sq
2e670 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
2e671 20 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65   int rc = sql_ge
2e672 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42  t_statement(v, B
2e673 4c 4f 43 4b 5f 49 4e 53 45 52 54 5f 53 54 4d 54  LOCK_INSERT_STMT
2e674 2c 20 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21  , &s);.  if( rc!
2e675 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2e676 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2e677 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f  sqlite3_bind_blo
2e678 62 28 73 2c 20 31 2c 20 70 44 61 74 61 2c 20 6e  b(s, 1, pData, n
2e679 44 61 74 61 2c 20 53 51 4c 49 54 45 5f 53 54 41  Data, SQLITE_STA
2e67a 54 49 43 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  TIC);.  if( rc!=
2e67b 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2e67c 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
2e67d 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b 0a  qlite3_step(s);.
2e67e 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
2e67f 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 53 51  _ROW ) return SQ
2e680 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 69 66  LITE_ERROR;.  if
2e681 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc!=SQLITE_DON
2e682 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  E ) return rc;..
2e683 20 20 2f 2a 20 62 6c 6f 63 6b 69 64 20 63 6f 6c    /* blockid col
2e684 75 6d 6e 20 69 73 20 61 6e 20 61 6c 69 61 73 20  umn is an alias 
2e685 66 6f 72 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20  for rowid. */.  
2e686 2a 70 69 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c  *piBlockid = sql
2e687 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
2e688 5f 72 6f 77 69 64 28 76 2d 3e 64 62 29 3b 0a 20  _rowid(v->db);. 
2e689 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f   return SQLITE_O
2e68a 4b 3b 0a 7d 0a 0a 2f 2a 20 64 65 6c 65 74 65 20  K;.}../* delete 
2e68b 66 72 6f 6d 20 25 5f 73 65 67 6d 65 6e 74 73 0a  from %_segments.
2e68c 2a 2a 20 20 20 77 68 65 72 65 20 62 6c 6f 63 6b  **   where block
2e68d 69 64 20 62 65 74 77 65 65 6e 20 5b 69 53 74 61  id between [iSta
2e68e 72 74 42 6c 6f 63 6b 69 64 5d 20 61 6e 64 20 5b  rtBlockid] and [
2e68f 69 45 6e 64 42 6c 6f 63 6b 69 64 5d 0a 2a 2a 0a  iEndBlockid].**.
2e690 2a 2a 20 44 65 6c 65 74 65 73 20 74 68 65 20 72  ** Deletes the r
2e691 61 6e 67 65 20 6f 66 20 62 6c 6f 63 6b 73 2c 20  ange of blocks, 
2e692 69 6e 63 6c 75 73 69 76 65 2c 20 75 73 65 64 20  inclusive, used 
2e693 74 6f 20 64 65 6c 65 74 65 20 74 68 65 20 62 6c  to delete the bl
2e694 6f 63 6b 73 0a 2a 2a 20 77 68 69 63 68 20 66 6f  ocks.** which fo
2e695 72 6d 20 61 20 73 65 67 6d 65 6e 74 2e 0a 2a 2f  rm a segment..*/
2e696 0a 73 74 61 74 69 63 20 69 6e 74 20 62 6c 6f 63  .static int bloc
2e697 6b 5f 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78  k_delete(fulltex
2e698 74 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20  t_vtab *v,.     
2e699 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e69a 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2e69b 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 73  iStartBlockid, s
2e69c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64  qlite_int64 iEnd
2e69d 42 6c 6f 63 6b 69 64 29 7b 0a 20 20 73 71 6c 69  Blockid){.  sqli
2e69e 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 69  te3_stmt *s;.  i
2e69f 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
2e6a0 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f  statement(v, BLO
2e6a1 43 4b 5f 44 45 4c 45 54 45 5f 53 54 4d 54 2c 20  CK_DELETE_STMT, 
2e6a2 26 73 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  &s);.  if( rc!=S
2e6a3 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
2e6a4 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71  n rc;..  rc = sq
2e6a5 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
2e6a6 28 73 2c 20 31 2c 20 69 53 74 61 72 74 42 6c 6f  (s, 1, iStartBlo
2e6a7 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
2e6a8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2e6a9 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2e6aa 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74  sqlite3_bind_int
2e6ab 36 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c 6f  64(s, 2, iEndBlo
2e6ac 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21  ckid);.  if( rc!
2e6ad 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2e6ae 75 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72  urn rc;..  retur
2e6af 6e 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65  n sql_single_ste
2e6b0 70 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 52 65 74 75  p(s);.}../* Retu
2e6b1 72 6e 73 20 53 51 4c 49 54 45 5f 52 4f 57 20 77  rns SQLITE_ROW w
2e6b2 69 74 68 20 2a 70 69 64 78 20 73 65 74 20 74 6f  ith *pidx set to
2e6b3 20 74 68 65 20 6d 61 78 69 6d 75 6d 20 73 65 67   the maximum seg
2e6b4 6d 65 6e 74 20 69 64 78 20 66 6f 75 6e 64 0a 2a  ment idx found.*
2e6b5 2a 20 61 74 20 69 4c 65 76 65 6c 2e 20 20 52 65  * at iLevel.  Re
2e6b6 74 75 72 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e  turns SQLITE_DON
2e6b7 45 20 69 66 20 74 68 65 72 65 20 61 72 65 20 6e  E if there are n
2e6b8 6f 20 73 65 67 6d 65 6e 74 73 20 61 74 0a 2a 2a  o segments at.**
2e6b9 20 69 4c 65 76 65 6c 2e 20 20 4f 74 68 65 72 77   iLevel.  Otherw
2e6ba 69 73 65 20 72 65 74 75 72 6e 73 20 61 6e 20 65  ise returns an e
2e6bb 72 72 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  rror..*/.static 
2e6bc 69 6e 74 20 73 65 67 64 69 72 5f 6d 61 78 5f 69  int segdir_max_i
2e6bd 6e 64 65 78 28 66 75 6c 6c 74 65 78 74 5f 76 74  ndex(fulltext_vt
2e6be 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65  ab *v, int iLeve
2e6bf 6c 2c 20 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20  l, int *pidx){. 
2e6c0 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
2e6c1 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
2e6c2 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
2e6c3 2c 20 53 45 47 44 49 52 5f 4d 41 58 5f 49 4e 44  , SEGDIR_MAX_IND
2e6c4 45 58 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20  EX_STMT, &s);.  
2e6c5 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2e6c6 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2e6c7 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
2e6c8 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69 4c  ind_int(s, 1, iL
2e6c9 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21  evel);.  if( rc!
2e6ca 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2e6cb 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20  urn rc;..  rc = 
2e6cc 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29 3b  sqlite3_step(s);
2e6cd 0a 20 20 2f 2a 20 53 68 6f 75 6c 64 20 61 6c 77  .  /* Should alw
2e6ce 61 79 73 20 67 65 74 20 61 74 20 6c 65 61 73 74  ays get at least
2e6cf 20 6f 6e 65 20 72 6f 77 20 64 75 65 20 74 6f 20   one row due to 
2e6d0 68 6f 77 20 6d 61 78 28 29 20 77 6f 72 6b 73 2e  how max() works.
2e6d1 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d 53 51   */.  if( rc==SQ
2e6d2 4c 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75  LITE_DONE ) retu
2e6d3 72 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a  rn SQLITE_DONE;.
2e6d4 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2e6d5 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20 72 63  _ROW ) return rc
2e6d6 3b 0a 0a 20 20 2f 2a 20 4e 55 4c 4c 20 6d 65 61  ;..  /* NULL mea
2e6d7 6e 73 20 74 68 61 74 20 74 68 65 72 65 20 77 65  ns that there we
2e6d8 72 65 20 6e 6f 20 69 6e 70 75 74 73 20 74 6f 20  re no inputs to 
2e6d9 6d 61 78 28 29 2e 20 2a 2f 0a 20 20 69 66 28 20  max(). */.  if( 
2e6da 53 51 4c 49 54 45 5f 4e 55 4c 4c 3d 3d 73 71 6c  SQLITE_NULL==sql
2e6db 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
2e6dc 28 73 2c 20 30 29 20 29 7b 0a 20 20 20 20 72 63  (s, 0) ){.    rc
2e6dd 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
2e6de 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  s);.    if( rc==
2e6df 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65 74  SQLITE_ROW ) ret
2e6e0 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
2e6e1 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b  ;.    return rc;
2e6e2 0a 20 20 7d 0a 0a 20 20 2a 70 69 64 78 20 3d 20  .  }..  *pidx = 
2e6e3 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
2e6e4 6e 74 28 73 2c 20 30 29 3b 0a 0a 20 20 2f 2a 20  nt(s, 0);..  /* 
2e6e5 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f  We expect only o
2e6e6 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74  ne row.  We must
2e6e7 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72   execute another
2e6e8 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
2e6e9 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65     * to complete
2e6ea 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20   the iteration; 
2e6eb 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61  otherwise the ta
2e6ec 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20  ble will remain 
2e6ed 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20  locked. */.  rc 
2e6ee 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
2e6ef 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
2e6f0 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
2e6f1 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
2e6f2 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2e6f3 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63  DONE ) return rc
2e6f4 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2e6f5 45 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 69 6e 73  E_ROW;.}../* ins
2e6f6 65 72 74 20 69 6e 74 6f 20 25 5f 73 65 67 64 69  ert into %_segdi
2e6f7 72 20 76 61 6c 75 65 73 20 28 0a 2a 2a 20 20 20  r values (.**   
2e6f8 5b 69 4c 65 76 65 6c 5d 2c 20 5b 69 64 78 5d 2c  [iLevel], [idx],
2e6f9 0a 2a 2a 20 20 20 5b 69 53 74 61 72 74 42 6c 6f  .**   [iStartBlo
2e6fa 63 6b 69 64 5d 2c 20 5b 69 4c 65 61 76 65 73 45  ckid], [iLeavesE
2e6fb 6e 64 42 6c 6f 63 6b 69 64 5d 2c 20 5b 69 45 6e  ndBlockid], [iEn
2e6fc 64 42 6c 6f 63 6b 69 64 5d 2c 0a 2a 2a 20 20 20  dBlockid],.**   
2e6fd 5b 70 52 6f 6f 74 44 61 74 61 5d 0a 2a 2a 20 29  [pRootData].** )
2e6fe 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2e6ff 65 67 64 69 72 5f 73 65 74 28 66 75 6c 6c 74 65  egdir_set(fullte
2e700 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20  xt_vtab *v, int 
2e701 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69 64 78 2c  iLevel, int idx,
2e702 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e703 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e         sqlite_in
2e704 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b 69  t64 iStartBlocki
2e705 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
2e706 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f           sqlite_
2e707 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e 64  int64 iLeavesEnd
2e708 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
2e709 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73                 s
2e70a 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64  qlite_int64 iEnd
2e70b 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20  Blockid,.       
2e70c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
2e70d 6f 6e 73 74 20 63 68 61 72 20 2a 70 52 6f 6f 74  onst char *pRoot
2e70e 44 61 74 61 2c 20 69 6e 74 20 6e 52 6f 6f 74 44  Data, int nRootD
2e70f 61 74 61 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ata){.  sqlite3_
2e710 73 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72  stmt *s;.  int r
2e711 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
2e712 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
2e713 53 45 54 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  SET_STMT, &s);. 
2e714 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2e715 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2e716 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2e717 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69  bind_int(s, 1, i
2e718 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63  Level);.  if( rc
2e719 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2e71a 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2e71b 20 73 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e   sqlite3_bind_in
2e71c 74 28 73 2c 20 32 2c 20 69 64 78 29 3b 0a 20 20  t(s, 2, idx);.  
2e71d 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2e71e 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2e71f 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62    rc = sqlite3_b
2e720 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 33 2c 20  ind_int64(s, 3, 
2e721 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29 3b 0a  iStartBlockid);.
2e722 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2e723 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2e724 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
2e725 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 34  _bind_int64(s, 4
2e726 2c 20 69 4c 65 61 76 65 73 45 6e 64 42 6c 6f 63  , iLeavesEndBloc
2e727 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
2e728 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2e729 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
2e72a 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
2e72b 34 28 73 2c 20 35 2c 20 69 45 6e 64 42 6c 6f 63  4(s, 5, iEndBloc
2e72c 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  kid);.  if( rc!=
2e72d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2e72e 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73  rn rc;..  rc = s
2e72f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 62 6c 6f 62  qlite3_bind_blob
2e730 28 73 2c 20 36 2c 20 70 52 6f 6f 74 44 61 74 61  (s, 6, pRootData
2e731 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 20 53 51 4c  , nRootData, SQL
2e732 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69  ITE_STATIC);.  i
2e733 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2e734 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2e735 20 72 65 74 75 72 6e 20 73 71 6c 5f 73 69 6e 67   return sql_sing
2e736 6c 65 5f 73 74 65 70 28 73 29 3b 0a 7d 0a 0a 2f  le_step(s);.}../
2e737 2a 20 51 75 65 72 69 65 73 20 25 5f 73 65 67 64  * Queries %_segd
2e738 69 72 20 66 6f 72 20 74 68 65 20 62 6c 6f 63 6b  ir for the block
2e739 20 73 70 61 6e 20 6f 66 20 74 68 65 20 73 65 67   span of the seg
2e73a 6d 65 6e 74 73 20 69 6e 20 6c 65 76 65 6c 0a 2a  ments in level.*
2e73b 2a 20 69 4c 65 76 65 6c 2e 20 20 52 65 74 75 72  * iLevel.  Retur
2e73c 6e 73 20 53 51 4c 49 54 45 5f 44 4f 4e 45 20 69  ns SQLITE_DONE i
2e73d 66 20 74 68 65 72 65 20 61 72 65 20 6e 6f 20 62  f there are no b
2e73e 6c 6f 63 6b 73 20 66 6f 72 20 69 4c 65 76 65 6c  locks for iLevel
2e73f 2c 0a 2a 2a 20 53 51 4c 49 54 45 5f 52 4f 57 20  ,.** SQLITE_ROW 
2e740 69 66 20 74 68 65 72 65 20 61 72 65 20 62 6c 6f  if there are blo
2e741 63 6b 73 2c 20 65 6c 73 65 20 61 6e 20 65 72 72  cks, else an err
2e742 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  or..*/.static in
2e743 74 20 73 65 67 64 69 72 5f 73 70 61 6e 28 66 75  t segdir_span(fu
2e744 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
2e745 69 6e 74 20 69 4c 65 76 65 6c 2c 0a 20 20 20 20  int iLevel,.    
2e746 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2e747 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2e748 2a 70 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c  *piStartBlockid,
2e749 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2e74a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69          sqlite_i
2e74b 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f 63 6b  nt64 *piEndBlock
2e74c 69 64 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 73  id){.  sqlite3_s
2e74d 74 6d 74 20 2a 73 3b 0a 20 20 69 6e 74 20 72 63  tmt *s;.  int rc
2e74e 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65   = sql_get_state
2e74f 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f 53  ment(v, SEGDIR_S
2e750 50 41 4e 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  PAN_STMT, &s);. 
2e751 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2e752 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2e753 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f  .  rc = sqlite3_
2e754 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c 20 69  bind_int(s, 1, i
2e755 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63  Level);.  if( rc
2e756 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2e757 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2e758 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
2e759 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
2e75a 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72 6e  TE_DONE ) return
2e75b 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 20 20 2f   SQLITE_DONE;  /
2e75c 2a 20 53 68 6f 75 6c 64 20 6e 65 76 65 72 20 68  * Should never h
2e75d 61 70 70 65 6e 20 2a 2f 0a 20 20 69 66 28 20 72  appen */.  if( r
2e75e 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20  c!=SQLITE_ROW ) 
2e75f 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
2e760 20 54 68 69 73 20 68 61 70 70 65 6e 73 20 69 66   This happens if
2e761 20 61 6c 6c 20 73 65 67 6d 65 6e 74 73 20 61 74   all segments at
2e762 20 74 68 69 73 20 6c 65 76 65 6c 20 61 72 65 20   this level are 
2e763 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69 6e 65 2e  entirely inline.
2e764 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49 54 45   */.  if( SQLITE
2e765 5f 4e 55 4c 4c 3d 3d 73 71 6c 69 74 65 33 5f 63  _NULL==sqlite3_c
2e766 6f 6c 75 6d 6e 5f 74 79 70 65 28 73 2c 20 30 29  olumn_type(s, 0)
2e767 20 29 7b 0a 20 20 20 20 2f 2a 20 57 65 20 65 78   ){.    /* We ex
2e768 70 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f  pect only one ro
2e769 77 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63  w.  We must exec
2e76a 75 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69  ute another sqli
2e76b 74 65 33 5f 73 74 65 70 28 29 0a 20 20 20 20 20  te3_step().     
2e76c 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68  * to complete th
2e76d 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68  e iteration; oth
2e76e 65 72 77 69 73 65 20 74 68 65 20 74 61 62 6c 65  erwise the table
2e76f 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c 6f 63   will remain loc
2e770 6b 65 64 2e 20 2a 2f 0a 20 20 20 20 69 6e 74 20  ked. */.    int 
2e771 72 63 32 20 3d 20 73 71 6c 69 74 65 33 5f 73 74  rc2 = sqlite3_st
2e772 65 70 28 73 29 3b 0a 20 20 20 20 69 66 28 20 72  ep(s);.    if( r
2e773 63 32 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  c2==SQLITE_ROW )
2e774 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45   return SQLITE_E
2e775 52 52 4f 52 3b 0a 20 20 20 20 72 65 74 75 72 6e  RROR;.    return
2e776 20 72 63 32 3b 0a 20 20 7d 0a 0a 20 20 2a 70 69   rc2;.  }..  *pi
2e777 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 3d 20 73  StartBlockid = s
2e778 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
2e779 74 36 34 28 73 2c 20 30 29 3b 0a 20 20 2a 70 69  t64(s, 0);.  *pi
2e77a 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 73 71 6c  EndBlockid = sql
2e77b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
2e77c 34 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20 57  4(s, 1);..  /* W
2e77d 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f 6e  e expect only on
2e77e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74 20  e row.  We must 
2e77f 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72 20  execute another 
2e780 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a 20  sqlite3_step(). 
2e781 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65 20    * to complete 
2e782 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20 6f  the iteration; o
2e783 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61 62  therwise the tab
2e784 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20 6c  le will remain l
2e785 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d  ocked. */.  rc =
2e786 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
2e787 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
2e788 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
2e789 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
2e78a 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  if( rc!=SQLITE_D
2e78b 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ONE ) return rc;
2e78c 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2e78d 5f 52 4f 57 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65  _ROW;.}../* Dele
2e78e 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74 20 62  te the segment b
2e78f 6c 6f 63 6b 73 20 61 6e 64 20 73 65 67 6d 65 6e  locks and segmen
2e790 74 20 64 69 72 65 63 74 6f 72 79 20 72 65 63 6f  t directory reco
2e791 72 64 73 20 66 6f 72 20 61 6c 6c 0a 2a 2a 20 73  rds for all.** s
2e792 65 67 6d 65 6e 74 73 20 61 74 20 69 4c 65 76 65  egments at iLeve
2e793 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  l..*/.static int
2e794 20 73 65 67 64 69 72 5f 64 65 6c 65 74 65 28 66   segdir_delete(f
2e795 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
2e796 20 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a 20 20   int iLevel){.  
2e797 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b  sqlite3_stmt *s;
2e798 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20  .  sqlite_int64 
2e799 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 69  iStartBlockid, i
2e79a 45 6e 64 42 6c 6f 63 6b 69 64 3b 0a 20 20 69 6e  EndBlockid;.  in
2e79b 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f 73 70  t rc = segdir_sp
2e79c 61 6e 28 76 2c 20 69 4c 65 76 65 6c 2c 20 26 69  an(v, iLevel, &i
2e79d 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20 26 69  StartBlockid, &i
2e79e 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69  EndBlockid);.  i
2e79f 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f  f( rc!=SQLITE_RO
2e7a0 57 20 26 26 20 72 63 21 3d 53 51 4c 49 54 45 5f  W && rc!=SQLITE_
2e7a1 44 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63  DONE ) return rc
2e7a2 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
2e7a3 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 72  ITE_ROW ){.    r
2e7a4 63 20 3d 20 62 6c 6f 63 6b 5f 64 65 6c 65 74 65  c = block_delete
2e7a5 28 76 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69  (v, iStartBlocki
2e7a6 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b  d, iEndBlockid);
2e7a7 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c  .    if( rc!=SQL
2e7a8 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2e7a9 72 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 44 65  rc;.  }..  /* De
2e7aa 6c 65 74 65 20 74 68 65 20 73 65 67 6d 65 6e 74  lete the segment
2e7ab 20 64 69 72 65 63 74 6f 72 79 20 69 74 73 65 6c   directory itsel
2e7ac 66 2e 20 2a 2f 0a 20 20 72 63 20 3d 20 73 71 6c  f. */.  rc = sql
2e7ad 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
2e7ae 2c 20 53 45 47 44 49 52 5f 44 45 4c 45 54 45 5f  , SEGDIR_DELETE_
2e7af 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
2e7b0 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2e7b1 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
2e7b2 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69 6e 64  c = sqlite3_bind
2e7b3 5f 69 6e 74 36 34 28 73 2c 20 31 2c 20 69 4c 65  _int64(s, 1, iLe
2e7b4 76 65 6c 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  vel);.  if( rc!=
2e7b5 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2e7b6 72 6e 20 72 63 3b 0a 0a 20 20 72 65 74 75 72 6e  rn rc;..  return
2e7b7 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73 74 65 70   sql_single_step
2e7b8 28 73 29 3b 0a 7d 0a 0a 2f 2a 20 44 65 6c 65 74  (s);.}../* Delet
2e7b9 65 20 65 6e 74 69 72 65 20 66 74 73 20 69 6e 64  e entire fts ind
2e7ba 65 78 2c 20 53 51 4c 49 54 45 5f 4f 4b 20 6f 6e  ex, SQLITE_OK on
2e7bb 20 73 75 63 63 65 73 73 2c 20 72 65 6c 65 76 61   success, releva
2e7bc 6e 74 20 65 72 72 6f 72 20 6f 6e 0a 2a 2a 20 66  nt error on.** f
2e7bd 61 69 6c 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69  ailure..*/.stati
2e7be 63 20 69 6e 74 20 73 65 67 64 69 72 5f 64 65 6c  c int segdir_del
2e7bf 65 74 65 5f 61 6c 6c 28 66 75 6c 6c 74 65 78 74  ete_all(fulltext
2e7c0 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20 73 71 6c  _vtab *v){.  sql
2e7c1 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20  ite3_stmt *s;.  
2e7c2 69 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74  int rc = sql_get
2e7c3 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20 53 45  _statement(v, SE
2e7c4 47 44 49 52 5f 44 45 4c 45 54 45 5f 41 4c 4c 5f  GDIR_DELETE_ALL_
2e7c5 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66 28  STMT, &s);.  if(
2e7c6 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2e7c7 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72   return rc;..  r
2e7c8 63 20 3d 20 73 71 6c 5f 73 69 6e 67 6c 65 5f 73  c = sql_single_s
2e7c9 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20 72 63  tep(s);.  if( rc
2e7ca 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2e7cb 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2e7cc 20 73 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65   sql_get_stateme
2e7cd 6e 74 28 76 2c 20 42 4c 4f 43 4b 5f 44 45 4c 45  nt(v, BLOCK_DELE
2e7ce 54 45 5f 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29  TE_ALL_STMT, &s)
2e7cf 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2e7d0 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2e7d1 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  c;..  return sql
2e7d2 5f 73 69 6e 67 6c 65 5f 73 74 65 70 28 73 29 3b  _single_step(s);
2e7d3 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72 6e 73 20 53  .}../* Returns S
2e7d4 51 4c 49 54 45 5f 4f 4b 20 77 69 74 68 20 2a 70  QLITE_OK with *p
2e7d5 6e 53 65 67 6d 65 6e 74 73 20 73 65 74 20 74 6f  nSegments set to
2e7d6 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65   the number of e
2e7d7 6e 74 72 69 65 73 20 69 6e 0a 2a 2a 20 25 5f 73  ntries in.** %_s
2e7d8 65 67 64 69 72 20 61 6e 64 20 2a 70 69 4d 61 78  egdir and *piMax
2e7d9 4c 65 76 65 6c 20 73 65 74 20 74 6f 20 74 68 65  Level set to the
2e7da 20 68 69 67 68 65 73 74 20 6c 65 76 65 6c 20 77   highest level w
2e7db 68 69 63 68 20 68 61 73 20 61 0a 2a 2a 20 73 65  hich has a.** se
2e7dc 67 6d 65 6e 74 2e 20 20 4f 74 68 65 72 77 69 73  gment.  Otherwis
2e7dd 65 20 72 65 74 75 72 6e 73 20 74 68 65 20 53 51  e returns the SQ
2e7de 4c 69 74 65 20 65 72 72 6f 72 20 77 68 69 63 68  Lite error which
2e7df 20 63 61 75 73 65 64 20 66 61 69 6c 75 72 65 2e   caused failure.
2e7e0 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73  .*/.static int s
2e7e1 65 67 64 69 72 5f 63 6f 75 6e 74 28 66 75 6c 6c  egdir_count(full
2e7e2 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e  text_vtab *v, in
2e7e3 74 20 2a 70 6e 53 65 67 6d 65 6e 74 73 2c 20 69  t *pnSegments, i
2e7e4 6e 74 20 2a 70 69 4d 61 78 4c 65 76 65 6c 29 7b  nt *piMaxLevel){
2e7e5 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
2e7e6 2a 73 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73  *s;.  int rc = s
2e7e7 71 6c 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74  ql_get_statement
2e7e8 28 76 2c 20 53 45 47 44 49 52 5f 43 4f 55 4e 54  (v, SEGDIR_COUNT
2e7e9 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 69 66  _STMT, &s);.  if
2e7ea 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2e7eb 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2e7ec 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
2e7ed 70 28 73 29 3b 0a 20 20 2f 2a 20 54 4f 44 4f 28  p(s);.  /* TODO(
2e7ee 73 68 65 73 73 29 3a 20 54 68 69 73 20 63 61 73  shess): This cas
2e7ef 65 20 73 68 6f 75 6c 64 20 6e 6f 74 20 62 65 20  e should not be 
2e7f0 70 6f 73 73 69 62 6c 65 3f 20 20 53 68 6f 75 6c  possible?  Shoul
2e7f1 64 20 73 74 72 6f 6e 67 65 72 0a 20 20 2a 2a 20  d stronger.  ** 
2e7f2 6d 65 61 73 75 72 65 73 20 62 65 20 74 61 6b 65  measures be take
2e7f3 6e 20 69 66 20 69 74 20 68 61 70 70 65 6e 73 3f  n if it happens?
2e7f4 0a 20 20 2a 2f 0a 20 20 69 66 28 20 72 63 3d 3d  .  */.  if( rc==
2e7f5 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
2e7f6 20 20 20 2a 70 6e 53 65 67 6d 65 6e 74 73 20 3d     *pnSegments =
2e7f7 20 30 3b 0a 20 20 20 20 2a 70 69 4d 61 78 4c 65   0;.    *piMaxLe
2e7f8 76 65 6c 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  vel = 0;.    ret
2e7f9 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2e7fa 20 7d 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c   }.  if( rc!=SQL
2e7fb 49 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e  ITE_ROW ) return
2e7fc 20 72 63 3b 0a 0a 20 20 2a 70 6e 53 65 67 6d 65   rc;..  *pnSegme
2e7fd 6e 74 73 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  nts = sqlite3_co
2e7fe 6c 75 6d 6e 5f 69 6e 74 28 73 2c 20 30 29 3b 0a  lumn_int(s, 0);.
2e7ff 20 20 2a 70 69 4d 61 78 4c 65 76 65 6c 20 3d 20    *piMaxLevel = 
2e800 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69  sqlite3_column_i
2e801 6e 74 28 73 2c 20 31 29 3b 0a 0a 20 20 2f 2a 20  nt(s, 1);..  /* 
2e802 57 65 20 65 78 70 65 63 74 20 6f 6e 6c 79 20 6f  We expect only o
2e803 6e 65 20 72 6f 77 2e 20 20 57 65 20 6d 75 73 74  ne row.  We must
2e804 20 65 78 65 63 75 74 65 20 61 6e 6f 74 68 65 72   execute another
2e805 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 29 0a   sqlite3_step().
2e806 20 20 20 2a 20 74 6f 20 63 6f 6d 70 6c 65 74 65     * to complete
2e807 20 74 68 65 20 69 74 65 72 61 74 69 6f 6e 3b 20   the iteration; 
2e808 6f 74 68 65 72 77 69 73 65 20 74 68 65 20 74 61  otherwise the ta
2e809 62 6c 65 20 77 69 6c 6c 20 72 65 6d 61 69 6e 20  ble will remain 
2e80a 6c 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20  locked. */.  rc 
2e80b 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
2e80c 29 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  );.  if( rc==SQL
2e80d 49 54 45 5f 44 4f 4e 45 20 29 20 72 65 74 75 72  ITE_DONE ) retur
2e80e 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69  n SQLITE_OK;.  i
2e80f 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 52 4f  f( rc==SQLITE_RO
2e810 57 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  W ) return SQLIT
2e811 45 5f 45 52 52 4f 52 3b 0a 20 20 72 65 74 75 72  E_ERROR;.  retur
2e812 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f  n rc;.}../* TODO
2e813 28 73 68 65 73 73 29 20 63 6c 65 61 72 50 65 6e  (shess) clearPen
2e814 64 69 6e 67 54 65 72 6d 73 28 29 20 69 73 20 66  dingTerms() is f
2e815 61 72 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65  ar down the file
2e816 20 62 65 63 61 75 73 65 0a 2a 2a 20 77 72 69 74   because.** writ
2e817 65 5a 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 69  eZeroSegment() i
2e818 73 20 66 61 72 20 64 6f 77 6e 20 74 68 65 20 66  s far down the f
2e819 69 6c 65 20 62 65 63 61 75 73 65 20 4c 65 61 66  ile because Leaf
2e81a 57 72 69 74 65 72 20 69 73 20 66 61 72 0a 2a 2a  Writer is far.**
2e81b 20 64 6f 77 6e 20 74 68 65 20 66 69 6c 65 2e 20   down the file. 
2e81c 20 43 6f 6e 73 69 64 65 72 20 72 65 66 61 63 74   Consider refact
2e81d 6f 72 69 6e 67 20 74 68 65 20 63 6f 64 65 20 74  oring the code t
2e81e 6f 20 6d 6f 76 65 20 74 68 65 20 6e 6f 6e 2d 76  o move the non-v
2e81f 74 61 62 0a 2a 2a 20 63 6f 64 65 20 61 62 6f 76  tab.** code abov
2e820 65 20 74 68 65 20 76 74 61 62 20 63 6f 64 65 20  e the vtab code 
2e821 73 6f 20 74 68 61 74 20 77 65 20 64 6f 6e 27 74  so that we don't
2e822 20 6e 65 65 64 20 74 68 69 73 20 66 6f 72 77 61   need this forwa
2e823 72 64 0a 2a 2a 20 72 65 66 65 72 65 6e 63 65 2e  rd.** reference.
2e824 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 63  .*/.static int c
2e825 6c 65 61 72 50 65 6e 64 69 6e 67 54 65 72 6d 73  learPendingTerms
2e826 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2e827 76 29 3b 0a 0a 2f 2a 0a 2a 2a 20 46 72 65 65 20  v);../*.** Free 
2e828 74 68 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20  the memory used 
2e829 74 6f 20 63 6f 6e 74 61 69 6e 20 61 20 66 75 6c  to contain a ful
2e82a 6c 74 65 78 74 5f 76 74 61 62 20 73 74 72 75 63  ltext_vtab struc
2e82b 74 75 72 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ture..*/.static 
2e82c 76 6f 69 64 20 66 75 6c 6c 74 65 78 74 5f 76 74  void fulltext_vt
2e82d 61 62 5f 64 65 73 74 72 6f 79 28 66 75 6c 6c 74  ab_destroy(fullt
2e82e 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a 20 20  ext_vtab *v){.  
2e82f 69 6e 74 20 69 53 74 6d 74 2c 20 69 3b 0a 0a 20  int iStmt, i;.. 
2e830 20 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33   FTSTRACE(("FTS3
2e831 20 44 65 73 74 72 6f 79 20 25 70 5c 6e 22 2c 20   Destroy %p\n", 
2e832 76 29 29 3b 0a 20 20 66 6f 72 28 20 69 53 74 6d  v));.  for( iStm
2e833 74 3d 30 3b 20 69 53 74 6d 74 3c 4d 41 58 5f 53  t=0; iStmt<MAX_S
2e834 54 4d 54 3b 20 69 53 74 6d 74 2b 2b 20 29 7b 0a  TMT; iStmt++ ){.
2e835 20 20 20 20 69 66 28 20 76 2d 3e 70 46 75 6c 6c      if( v->pFull
2e836 74 65 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69  textStatements[i
2e837 53 74 6d 74 5d 21 3d 4e 55 4c 4c 20 29 7b 0a 20  Stmt]!=NULL ){. 
2e838 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e       sqlite3_fin
2e839 61 6c 69 7a 65 28 76 2d 3e 70 46 75 6c 6c 74 65  alize(v->pFullte
2e83a 78 74 53 74 61 74 65 6d 65 6e 74 73 5b 69 53 74  xtStatements[iSt
2e83b 6d 74 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e 70  mt]);.      v->p
2e83c 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d 65 6e  FulltextStatemen
2e83d 74 73 5b 69 53 74 6d 74 5d 20 3d 20 4e 55 4c 4c  ts[iStmt] = NULL
2e83e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 66  ;.    }.  }..  f
2e83f 6f 72 28 20 69 3d 30 3b 20 69 3c 4d 45 52 47 45  or( i=0; i<MERGE
2e840 5f 43 4f 55 4e 54 3b 20 69 2b 2b 20 29 7b 0a 20  _COUNT; i++ ){. 
2e841 20 20 20 69 66 28 20 76 2d 3e 70 4c 65 61 66 53     if( v->pLeafS
2e842 65 6c 65 63 74 53 74 6d 74 73 5b 69 5d 21 3d 4e  electStmts[i]!=N
2e843 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 73 71 6c  ULL ){.      sql
2e844 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 76 2d  ite3_finalize(v-
2e845 3e 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74  >pLeafSelectStmt
2e846 73 5b 69 5d 29 3b 0a 20 20 20 20 20 20 76 2d 3e  s[i]);.      v->
2e847 70 4c 65 61 66 53 65 6c 65 63 74 53 74 6d 74 73  pLeafSelectStmts
2e848 5b 69 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20  [i] = NULL;.    
2e849 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 76 2d 3e  }.  }..  if( v->
2e84a 70 54 6f 6b 65 6e 69 7a 65 72 21 3d 4e 55 4c 4c  pTokenizer!=NULL
2e84b 20 29 7b 0a 20 20 20 20 76 2d 3e 70 54 6f 6b 65   ){.    v->pToke
2e84c 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75 6c 65 2d 3e  nizer->pModule->
2e84d 78 44 65 73 74 72 6f 79 28 76 2d 3e 70 54 6f 6b  xDestroy(v->pTok
2e84e 65 6e 69 7a 65 72 29 3b 0a 20 20 20 20 76 2d 3e  enizer);.    v->
2e84f 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 4e 55 4c  pTokenizer = NUL
2e850 4c 3b 0a 20 20 7d 0a 0a 20 20 63 6c 65 61 72 50  L;.  }..  clearP
2e851 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a  endingTerms(v);.
2e852 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2e853 76 2d 3e 61 7a 43 6f 6c 75 6d 6e 29 3b 0a 20 20  v->azColumn);.  
2e854 66 6f 72 28 69 20 3d 20 30 3b 20 69 20 3c 20 76  for(i = 0; i < v
2e855 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b 2b 69 29 20  ->nColumn; ++i) 
2e856 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72  {.    sqlite3_fr
2e857 65 65 28 76 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43  ee(v->azContentC
2e858 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 7d 0a 20  olumn[i]);.  }. 
2e859 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 76 2d   sqlite3_free(v-
2e85a 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e  >azContentColumn
2e85b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
2e85c 65 28 76 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  e(v);.}../*.** T
2e85d 6f 6b 65 6e 20 74 79 70 65 73 20 66 6f 72 20 70  oken types for p
2e85e 61 72 73 69 6e 67 20 74 68 65 20 61 72 67 75 6d  arsing the argum
2e85f 65 6e 74 73 20 74 6f 20 78 43 6f 6e 6e 65 63 74  ents to xConnect
2e860 20 6f 72 20 78 43 72 65 61 74 65 2e 0a 2a 2f 0a   or xCreate..*/.
2e861 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 45 4f  #define TOKEN_EO
2e862 46 20 20 20 20 20 20 20 20 20 30 20 20 20 20 2f  F         0    /
2e863 2a 20 45 6e 64 20 6f 66 20 66 69 6c 65 20 2a 2f  * End of file */
2e864 0a 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53  .#define TOKEN_S
2e865 50 41 43 45 20 20 20 20 20 20 20 31 20 20 20 20  PACE       1    
2e866 2f 2a 20 41 6e 79 20 6b 69 6e 64 20 6f 66 20 77  /* Any kind of w
2e867 68 69 74 65 73 70 61 63 65 20 2a 2f 0a 23 64 65  hitespace */.#de
2e868 66 69 6e 65 20 54 4f 4b 45 4e 5f 49 44 20 20 20  fine TOKEN_ID   
2e869 20 20 20 20 20 20 20 32 20 20 20 20 2f 2a 20 41         2    /* A
2e86a 6e 20 69 64 65 6e 74 69 66 69 65 72 20 2a 2f 0a  n identifier */.
2e86b 23 64 65 66 69 6e 65 20 54 4f 4b 45 4e 5f 53 54  #define TOKEN_ST
2e86c 52 49 4e 47 20 20 20 20 20 20 33 20 20 20 20 2f  RING      3    /
2e86d 2a 20 41 20 73 74 72 69 6e 67 20 6c 69 74 65 72  * A string liter
2e86e 61 6c 20 2a 2f 0a 23 64 65 66 69 6e 65 20 54 4f  al */.#define TO
2e86f 4b 45 4e 5f 50 55 4e 43 54 20 20 20 20 20 20 20  KEN_PUNCT       
2e870 34 20 20 20 20 2f 2a 20 41 20 73 69 6e 67 6c 65  4    /* A single
2e871 20 70 75 6e 63 74 75 61 74 69 6f 6e 20 63 68 61   punctuation cha
2e872 72 61 63 74 65 72 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  racter */../*.**
2e873 20 49 66 20 58 20 69 73 20 61 20 63 68 61 72 61   If X is a chara
2e874 63 74 65 72 20 74 68 61 74 20 63 61 6e 20 62 65  cter that can be
2e875 20 75 73 65 64 20 69 6e 20 61 6e 20 69 64 65 6e   used in an iden
2e876 74 69 66 69 65 72 20 74 68 65 6e 0a 2a 2a 20 66  tifier then.** f
2e877 74 73 49 64 43 68 61 72 28 58 29 20 77 69 6c 6c  tsIdChar(X) will
2e878 20 62 65 20 74 72 75 65 2e 20 20 4f 74 68 65 72   be true.  Other
2e879 77 69 73 65 20 69 74 20 69 73 20 66 61 6c 73 65  wise it is false
2e87a 2e 0a 2a 2a 0a 2a 2a 20 46 6f 72 20 41 53 43 49  ..**.** For ASCI
2e87b 49 2c 20 61 6e 79 20 63 68 61 72 61 63 74 65 72  I, any character
2e87c 20 77 69 74 68 20 74 68 65 20 68 69 67 68 2d 6f   with the high-o
2e87d 72 64 65 72 20 62 69 74 20 73 65 74 20 69 73 0a  rder bit set is.
2e87e 2a 2a 20 61 6c 6c 6f 77 65 64 20 69 6e 20 61 6e  ** allowed in an
2e87f 20 69 64 65 6e 74 69 66 69 65 72 2e 20 20 46 6f   identifier.  Fo
2e880 72 20 37 2d 62 69 74 20 63 68 61 72 61 63 74 65  r 7-bit characte
2e881 72 73 2c 20 0a 2a 2a 20 69 73 46 74 73 49 64 43  rs, .** isFtsIdC
2e882 68 61 72 5b 58 5d 20 6d 75 73 74 20 62 65 20 31  har[X] must be 1
2e883 2e 0a 2a 2a 0a 2a 2a 20 54 69 63 6b 65 74 20 23  ..**.** Ticket #
2e884 31 30 36 36 2e 20 20 74 68 65 20 53 51 4c 20 73  1066.  the SQL s
2e885 74 61 6e 64 61 72 64 20 64 6f 65 73 20 6e 6f 74  tandard does not
2e886 20 61 6c 6c 6f 77 20 27 24 27 20 69 6e 20 74 68   allow '$' in th
2e887 65 0a 2a 2a 20 6d 69 64 64 6c 65 20 6f 66 20 69  e.** middle of i
2e888 64 65 6e 74 66 69 65 72 73 2e 20 20 42 75 74 20  dentfiers.  But 
2e889 6d 61 6e 79 20 53 51 4c 20 69 6d 70 6c 65 6d 65  many SQL impleme
2e88a 6e 74 61 74 69 6f 6e 73 20 64 6f 2e 20 0a 2a 2a  ntations do. .**
2e88b 20 53 51 4c 69 74 65 20 77 69 6c 6c 20 61 6c 6c   SQLite will all
2e88c 6f 77 20 27 24 27 20 69 6e 20 69 64 65 6e 74 69  ow '$' in identi
2e88d 66 69 65 72 73 20 66 6f 72 20 63 6f 6d 70 61 74  fiers for compat
2e88e 69 62 69 6c 69 74 79 2e 0a 2a 2a 20 42 75 74 20  ibility..** But 
2e88f 74 68 65 20 66 65 61 74 75 72 65 20 69 73 20 75  the feature is u
2e890 6e 64 6f 63 75 6d 65 6e 74 65 64 2e 0a 2a 2f 0a  ndocumented..*/.
2e891 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
2e892 72 20 69 73 46 74 73 49 64 43 68 61 72 5b 5d 20  r isFtsIdChar[] 
2e893 3d 20 7b 0a 2f 2a 20 78 30 20 78 31 20 78 32 20  = {./* x0 x1 x2 
2e894 78 33 20 78 34 20 78 35 20 78 36 20 78 37 20 78  x3 x4 x5 x6 x7 x
2e895 38 20 78 39 20 78 41 20 78 42 20 78 43 20 78 44  8 x9 xA xB xC xD
2e896 20 78 45 20 78 46 20 2a 2f 0a 20 20 20 20 30 2c   xE xF */.    0,
2e897 20 30 2c 20 30 2c 20 30 2c 20 31 2c 20 30 2c 20   0, 0, 0, 1, 0, 
2e898 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
2e899 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f  , 0, 0, 0, 0,  /
2e89a 2a 20 32 78 20 2a 2f 0a 20 20 20 20 31 2c 20 31  * 2x */.    1, 1
2e89b 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2e89c 20 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20   1, 1, 1, 0, 0, 
2e89d 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a 20  0, 0, 0, 0,  /* 
2e89e 33 78 20 2a 2f 0a 20 20 20 20 30 2c 20 31 2c 20  3x */.    0, 1, 
2e89f 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2e8a0 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2e8a1 20 31 2c 20 31 2c 20 31 2c 20 20 2f 2a 20 34 78   1, 1, 1,  /* 4x
2e8a2 20 2a 2f 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c   */.    1, 1, 1,
2e8a3 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2e8a4 31 2c 20 31 2c 20 31 2c 20 30 2c 20 30 2c 20 30  1, 1, 1, 0, 0, 0
2e8a5 2c 20 30 2c 20 31 2c 20 20 2f 2a 20 35 78 20 2a  , 0, 1,  /* 5x *
2e8a6 2f 0a 20 20 20 20 30 2c 20 31 2c 20 31 2c 20 31  /.    0, 1, 1, 1
2e8a7 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
2e8a8 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
2e8a9 31 2c 20 31 2c 20 20 2f 2a 20 36 78 20 2a 2f 0a  1, 1,  /* 6x */.
2e8aa 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20      1, 1, 1, 1, 
2e8ab 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
2e8ac 2c 20 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 1, 0, 0, 0, 0,
2e8ad 20 30 2c 20 20 2f 2a 20 37 78 20 2a 2f 0a 7d 3b   0,  /* 7x */.};
2e8ae 0a 23 64 65 66 69 6e 65 20 66 74 73 49 64 43 68  .#define ftsIdCh
2e8af 61 72 28 43 29 20 20 28 28 28 63 3d 43 29 26 30  ar(C)  (((c=C)&0
2e8b0 78 38 30 29 21 3d 30 20 7c 7c 20 28 63 3e 30 78  x80)!=0 || (c>0x
2e8b1 31 66 20 26 26 20 69 73 46 74 73 49 64 43 68 61  1f && isFtsIdCha
2e8b2 72 5b 63 2d 30 78 32 30 5d 29 29 0a 0a 0a 2f 2a  r[c-0x20])).../*
2e8b3 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 6c  .** Return the l
2e8b4 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b  ength of the tok
2e8b5 65 6e 20 74 68 61 74 20 62 65 67 69 6e 73 20 61  en that begins a
2e8b6 74 20 7a 5b 30 5d 2e 20 0a 2a 2a 20 53 74 6f 72  t z[0]. .** Stor
2e8b7 65 20 74 68 65 20 74 6f 6b 65 6e 20 74 79 70 65  e the token type
2e8b8 20 69 6e 20 2a 74 6f 6b 65 6e 54 79 70 65 20 62   in *tokenType b
2e8b9 65 66 6f 72 65 20 72 65 74 75 72 6e 69 6e 67 2e  efore returning.
2e8ba 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66  .*/.static int f
2e8bb 74 73 47 65 74 54 6f 6b 65 6e 28 63 6f 6e 73 74  tsGetToken(const
2e8bc 20 63 68 61 72 20 2a 7a 2c 20 69 6e 74 20 2a 74   char *z, int *t
2e8bd 6f 6b 65 6e 54 79 70 65 29 7b 0a 20 20 69 6e 74  okenType){.  int
2e8be 20 69 2c 20 63 3b 0a 20 20 73 77 69 74 63 68 28   i, c;.  switch(
2e8bf 20 2a 7a 20 29 7b 0a 20 20 20 20 63 61 73 65 20   *z ){.    case 
2e8c0 30 3a 20 7b 0a 20 20 20 20 20 20 2a 74 6f 6b 65  0: {.      *toke
2e8c1 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 45 4f  nType = TOKEN_EO
2e8c2 46 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  F;.      return 
2e8c3 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 63 61 73  0;.    }.    cas
2e8c4 65 20 27 20 27 3a 20 63 61 73 65 20 27 5c 74 27  e ' ': case '\t'
2e8c5 3a 20 63 61 73 65 20 27 5c 6e 27 3a 20 63 61 73  : case '\n': cas
2e8c6 65 20 27 5c 66 27 3a 20 63 61 73 65 20 27 5c 72  e '\f': case '\r
2e8c7 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28 69  ': {.      for(i
2e8c8 3d 31 3b 20 73 61 66 65 5f 69 73 73 70 61 63 65  =1; safe_isspace
2e8c9 28 7a 5b 69 5d 29 3b 20 69 2b 2b 29 7b 7d 0a 20  (z[i]); i++){}. 
2e8ca 20 20 20 20 20 2a 74 6f 6b 65 6e 54 79 70 65 20       *tokenType 
2e8cb 3d 20 54 4f 4b 45 4e 5f 53 50 41 43 45 3b 0a 20  = TOKEN_SPACE;. 
2e8cc 20 20 20 20 20 72 65 74 75 72 6e 20 69 3b 0a 20       return i;. 
2e8cd 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27 60     }.    case '`
2e8ce 27 3a 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27  ':.    case '\''
2e8cf 3a 0a 20 20 20 20 63 61 73 65 20 27 22 27 3a 20  :.    case '"': 
2e8d0 7b 0a 20 20 20 20 20 20 69 6e 74 20 64 65 6c 69  {.      int deli
2e8d1 6d 20 3d 20 7a 5b 30 5d 3b 0a 20 20 20 20 20 20  m = z[0];.      
2e8d2 66 6f 72 28 69 3d 31 3b 20 28 63 3d 7a 5b 69 5d  for(i=1; (c=z[i]
2e8d3 29 21 3d 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  )!=0; i++){.    
2e8d4 20 20 20 20 69 66 28 20 63 3d 3d 64 65 6c 69 6d      if( c==delim
2e8d5 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66   ){.          if
2e8d6 28 20 7a 5b 69 2b 31 5d 3d 3d 64 65 6c 69 6d 20  ( z[i+1]==delim 
2e8d7 29 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 69  ){.            i
2e8d8 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d 65  ++;.          }e
2e8d9 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 20  lse{.           
2e8da 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
2e8db 20 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20    }.        }.  
2e8dc 20 20 20 20 7d 0a 20 20 20 20 20 20 2a 74 6f 6b      }.      *tok
2e8dd 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f 53  enType = TOKEN_S
2e8de 54 52 49 4e 47 3b 0a 20 20 20 20 20 20 72 65 74  TRING;.      ret
2e8df 75 72 6e 20 69 20 2b 20 28 63 21 3d 30 29 3b 0a  urn i + (c!=0);.
2e8e0 20 20 20 20 7d 0a 20 20 20 20 63 61 73 65 20 27      }.    case '
2e8e1 5b 27 3a 20 7b 0a 20 20 20 20 20 20 66 6f 72 28  [': {.      for(
2e8e2 69 3d 31 2c 20 63 3d 7a 5b 30 5d 3b 20 63 21 3d  i=1, c=z[0]; c!=
2e8e3 27 5d 27 20 26 26 20 28 63 3d 7a 5b 69 5d 29 21  ']' && (c=z[i])!
2e8e4 3d 30 3b 20 69 2b 2b 29 7b 7d 0a 20 20 20 20 20  =0; i++){}.     
2e8e5 20 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f   *tokenType = TO
2e8e6 4b 45 4e 5f 49 44 3b 0a 20 20 20 20 20 20 72 65  KEN_ID;.      re
2e8e7 74 75 72 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20  turn i;.    }.  
2e8e8 20 20 64 65 66 61 75 6c 74 3a 20 7b 0a 20 20 20    default: {.   
2e8e9 20 20 20 69 66 28 20 21 66 74 73 49 64 43 68 61     if( !ftsIdCha
2e8ea 72 28 2a 7a 29 20 29 7b 0a 20 20 20 20 20 20 20  r(*z) ){.       
2e8eb 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a   break;.      }.
2e8ec 20 20 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 66        for(i=1; f
2e8ed 74 73 49 64 43 68 61 72 28 7a 5b 69 5d 29 3b 20  tsIdChar(z[i]); 
2e8ee 69 2b 2b 29 7b 7d 0a 20 20 20 20 20 20 2a 74 6f  i++){}.      *to
2e8ef 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b 45 4e 5f  kenType = TOKEN_
2e8f0 49 44 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e  ID;.      return
2e8f1 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20   i;.    }.  }.  
2e8f2 2a 74 6f 6b 65 6e 54 79 70 65 20 3d 20 54 4f 4b  *tokenType = TOK
2e8f3 45 4e 5f 50 55 4e 43 54 3b 0a 20 20 72 65 74 75  EN_PUNCT;.  retu
2e8f4 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41  rn 1;.}../*.** A
2e8f5 20 74 6f 6b 65 6e 20 65 78 74 72 61 63 74 65 64   token extracted
2e8f6 20 66 72 6f 6d 20 61 20 73 74 72 69 6e 67 20 69   from a string i
2e8f7 73 20 61 6e 20 69 6e 73 74 61 6e 63 65 20 6f 66  s an instance of
2e8f8 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   the following.*
2e8f9 2a 20 73 74 72 75 63 74 75 72 65 2e 0a 2a 2f 0a  * structure..*/.
2e8fa 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 46  typedef struct F
2e8fb 74 73 54 6f 6b 65 6e 20 7b 0a 20 20 63 6f 6e 73  tsToken {.  cons
2e8fc 74 20 63 68 61 72 20 2a 7a 3b 20 20 20 20 20 20  t char *z;      
2e8fd 20 2f 2a 20 50 6f 69 6e 74 65 72 20 74 6f 20 74   /* Pointer to t
2e8fe 6f 6b 65 6e 20 74 65 78 74 2e 20 20 4e 6f 74 20  oken text.  Not 
2e8ff 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61 74 65  '\000' terminate
2e900 64 20 2a 2f 0a 20 20 73 68 6f 72 74 20 69 6e 74  d */.  short int
2e901 20 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4c   n;         /* L
2e902 65 6e 67 74 68 20 6f 66 20 74 68 65 20 74 6f 6b  ength of the tok
2e903 65 6e 20 74 65 78 74 20 69 6e 20 62 79 74 65 73  en text in bytes
2e904 2e 20 2a 2f 0a 7d 20 46 74 73 54 6f 6b 65 6e 3b  . */.} FtsToken;
2e905 0a 0a 2f 2a 0a 2a 2a 20 47 69 76 65 6e 20 61 20  ../*.** Given a 
2e906 69 6e 70 75 74 20 73 74 72 69 6e 67 20 28 77 68  input string (wh
2e907 69 63 68 20 69 73 20 72 65 61 6c 6c 79 20 6f 6e  ich is really on
2e908 65 20 6f 66 20 74 68 65 20 61 72 67 76 5b 5d 20  e of the argv[] 
2e909 70 61 72 61 6d 65 74 65 72 73 0a 2a 2a 20 70 61  parameters.** pa
2e90a 73 73 65 64 20 69 6e 74 6f 20 78 43 6f 6e 6e 65  ssed into xConne
2e90b 63 74 20 6f 72 20 78 43 72 65 61 74 65 29 20 73  ct or xCreate) s
2e90c 70 6c 69 74 20 74 68 65 20 73 74 72 69 6e 67 20  plit the string 
2e90d 75 70 20 69 6e 74 6f 20 74 6f 6b 65 6e 73 2e 0a  up into tokens..
2e90e 2a 2a 20 52 65 74 75 72 6e 20 61 6e 20 61 72 72  ** Return an arr
2e90f 61 79 20 6f 66 20 70 6f 69 6e 74 65 72 73 20 74  ay of pointers t
2e910 6f 20 27 5c 30 30 30 27 20 74 65 72 6d 69 6e 61  o '\000' termina
2e911 74 65 64 20 73 74 72 69 6e 67 73 2c 20 6f 6e 65  ted strings, one
2e912 20 73 74 72 69 6e 67 0a 2a 2a 20 66 6f 72 20 65   string.** for e
2e913 61 63 68 20 6e 6f 6e 2d 77 68 69 74 65 73 70 61  ach non-whitespa
2e914 63 65 20 74 6f 6b 65 6e 2e 0a 2a 2a 0a 2a 2a 20  ce token..**.** 
2e915 54 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72  The returned arr
2e916 61 79 20 69 73 20 74 65 72 6d 69 6e 61 74 65 64  ay is terminated
2e917 20 62 79 20 61 20 73 69 6e 67 6c 65 20 4e 55 4c   by a single NUL
2e918 4c 20 70 6f 69 6e 74 65 72 2e 0a 2a 2a 0a 2a 2a  L pointer..**.**
2e919 20 53 70 61 63 65 20 74 6f 20 68 6f 6c 64 20 74   Space to hold t
2e91a 68 65 20 72 65 74 75 72 6e 65 64 20 61 72 72 61  he returned arra
2e91b 79 20 69 73 20 6f 62 74 61 69 6e 65 64 20 66 72  y is obtained fr
2e91c 6f 6d 20 61 20 73 69 6e 67 6c 65 0a 2a 2a 20 6d  om a single.** m
2e91d 61 6c 6c 6f 63 20 61 6e 64 20 73 68 6f 75 6c 64  alloc and should
2e91e 20 62 65 20 66 72 65 65 64 20 62 79 20 70 61 73   be freed by pas
2e91f 73 69 6e 67 20 74 68 65 20 72 65 74 75 72 6e 20  sing the return 
2e920 76 61 6c 75 65 20 74 6f 20 66 72 65 65 28 29 2e  value to free().
2e921 0a 2a 2a 20 54 68 65 20 69 6e 64 69 76 69 64 75  .** The individu
2e922 61 6c 20 73 74 72 69 6e 67 73 20 77 69 74 68 69  al strings withi
2e923 6e 20 74 68 65 20 74 6f 6b 65 6e 20 6c 69 73 74  n the token list
2e924 20 61 72 65 20 61 6c 6c 20 61 20 70 61 72 74 20   are all a part 
2e925 6f 66 0a 2a 2a 20 74 68 65 20 73 69 6e 67 6c 65  of.** the single
2e926 20 6d 65 6d 6f 72 79 20 61 6c 6c 6f 63 61 74 69   memory allocati
2e927 6f 6e 20 61 6e 64 20 77 69 6c 6c 20 61 6c 6c 20  on and will all 
2e928 62 65 20 66 72 65 65 64 20 61 74 20 6f 6e 63 65  be freed at once
2e929 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 68 61 72  ..*/.static char
2e92a 20 2a 2a 74 6f 6b 65 6e 69 7a 65 53 74 72 69 6e   **tokenizeStrin
2e92b 67 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 2c  g(const char *z,
2e92c 20 69 6e 74 20 2a 70 6e 54 6f 6b 65 6e 29 7b 0a   int *pnToken){.
2e92d 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 3d 20 30    int nToken = 0
2e92e 3b 0a 20 20 46 74 73 54 6f 6b 65 6e 20 2a 61 54  ;.  FtsToken *aT
2e92f 6f 6b 65 6e 20 3d 20 73 71 6c 69 74 65 33 5f 6d  oken = sqlite3_m
2e930 61 6c 6c 6f 63 28 20 73 74 72 6c 65 6e 28 7a 29  alloc( strlen(z)
2e931 20 2a 20 73 69 7a 65 6f 66 28 61 54 6f 6b 65 6e   * sizeof(aToken
2e932 5b 30 5d 29 20 29 3b 0a 20 20 69 6e 74 20 6e 20  [0]) );.  int n 
2e933 3d 20 31 3b 0a 20 20 69 6e 74 20 65 2c 20 69 3b  = 1;.  int e, i;
2e934 0a 20 20 69 6e 74 20 74 6f 74 61 6c 53 69 7a 65  .  int totalSize
2e935 20 3d 20 30 3b 0a 20 20 63 68 61 72 20 2a 2a 61   = 0;.  char **a
2e936 7a 54 6f 6b 65 6e 3b 0a 20 20 63 68 61 72 20 2a  zToken;.  char *
2e937 7a 43 6f 70 79 3b 0a 20 20 77 68 69 6c 65 28 20  zCopy;.  while( 
2e938 6e 3e 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66  n>0 ){.    n = f
2e939 74 73 47 65 74 54 6f 6b 65 6e 28 7a 2c 20 26 65  tsGetToken(z, &e
2e93a 29 3b 0a 20 20 20 20 69 66 28 20 65 21 3d 54 4f  );.    if( e!=TO
2e93b 4b 45 4e 5f 53 50 41 43 45 20 29 7b 0a 20 20 20  KEN_SPACE ){.   
2e93c 20 20 20 61 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e     aToken[nToken
2e93d 5d 2e 7a 20 3d 20 7a 3b 0a 20 20 20 20 20 20 61  ].z = z;.      a
2e93e 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 2e 6e 20  Token[nToken].n 
2e93f 3d 20 6e 3b 0a 20 20 20 20 20 20 6e 54 6f 6b 65  = n;.      nToke
2e940 6e 2b 2b 3b 0a 20 20 20 20 20 20 74 6f 74 61 6c  n++;.      total
2e941 53 69 7a 65 20 2b 3d 20 6e 2b 31 3b 0a 20 20 20  Size += n+1;.   
2e942 20 7d 0a 20 20 20 20 7a 20 2b 3d 20 6e 3b 0a 20   }.    z += n;. 
2e943 20 7d 0a 20 20 61 7a 54 6f 6b 65 6e 20 3d 20 28   }.  azToken = (
2e944 63 68 61 72 2a 2a 29 73 71 6c 69 74 65 33 5f 6d  char**)sqlite3_m
2e945 61 6c 6c 6f 63 28 20 6e 54 6f 6b 65 6e 2a 73 69  alloc( nToken*si
2e946 7a 65 6f 66 28 63 68 61 72 2a 29 20 2b 20 74 6f  zeof(char*) + to
2e947 74 61 6c 53 69 7a 65 20 29 3b 0a 20 20 7a 43 6f  talSize );.  zCo
2e948 70 79 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 54  py = (char*)&azT
2e949 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 3b 0a 20 20  oken[nToken];.  
2e94a 6e 54 6f 6b 65 6e 2d 2d 3b 0a 20 20 66 6f 72 28  nToken--;.  for(
2e94b 69 3d 30 3b 20 69 3c 6e 54 6f 6b 65 6e 3b 20 69  i=0; i<nToken; i
2e94c 2b 2b 29 7b 0a 20 20 20 20 61 7a 54 6f 6b 65 6e  ++){.    azToken
2e94d 5b 69 5d 20 3d 20 7a 43 6f 70 79 3b 0a 20 20 20  [i] = zCopy;.   
2e94e 20 6e 20 3d 20 61 54 6f 6b 65 6e 5b 69 5d 2e 6e   n = aToken[i].n
2e94f 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 7a 43 6f  ;.    memcpy(zCo
2e950 70 79 2c 20 61 54 6f 6b 65 6e 5b 69 5d 2e 7a 2c  py, aToken[i].z,
2e951 20 6e 29 3b 0a 20 20 20 20 7a 43 6f 70 79 5b 6e   n);.    zCopy[n
2e952 5d 20 3d 20 30 3b 0a 20 20 20 20 7a 43 6f 70 79  ] = 0;.    zCopy
2e953 20 2b 3d 20 6e 2b 31 3b 0a 20 20 7d 0a 20 20 61   += n+1;.  }.  a
2e954 7a 54 6f 6b 65 6e 5b 6e 54 6f 6b 65 6e 5d 20 3d  zToken[nToken] =
2e955 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72   0;.  sqlite3_fr
2e956 65 65 28 61 54 6f 6b 65 6e 29 3b 0a 20 20 2a 70  ee(aToken);.  *p
2e957 6e 54 6f 6b 65 6e 20 3d 20 6e 54 6f 6b 65 6e 3b  nToken = nToken;
2e958 0a 20 20 72 65 74 75 72 6e 20 61 7a 54 6f 6b 65  .  return azToke
2e959 6e 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6e 76  n;.}../*.** Conv
2e95a 65 72 74 20 61 6e 20 53 51 4c 2d 73 74 79 6c 65  ert an SQL-style
2e95b 20 71 75 6f 74 65 64 20 73 74 72 69 6e 67 20 69   quoted string i
2e95c 6e 74 6f 20 61 20 6e 6f 72 6d 61 6c 20 73 74 72  nto a normal str
2e95d 69 6e 67 20 62 79 20 72 65 6d 6f 76 69 6e 67 0a  ing by removing.
2e95e 2a 2a 20 74 68 65 20 71 75 6f 74 65 20 63 68 61  ** the quote cha
2e95f 72 61 63 74 65 72 73 2e 20 20 54 68 65 20 63 6f  racters.  The co
2e960 6e 76 65 72 73 69 6f 6e 20 69 73 20 64 6f 6e 65  nversion is done
2e961 20 69 6e 2d 70 6c 61 63 65 2e 20 20 49 66 20 74   in-place.  If t
2e962 68 65 0a 2a 2a 20 69 6e 70 75 74 20 64 6f 65 73  he.** input does
2e963 20 6e 6f 74 20 62 65 67 69 6e 20 77 69 74 68 20   not begin with 
2e964 61 20 71 75 6f 74 65 20 63 68 61 72 61 63 74 65  a quote characte
2e965 72 2c 20 74 68 65 6e 20 74 68 69 73 20 72 6f 75  r, then this rou
2e966 74 69 6e 65 0a 2a 2a 20 69 73 20 61 20 6e 6f 2d  tine.** is a no-
2e967 6f 70 2e 0a 2a 2a 0a 2a 2a 20 45 78 61 6d 70 6c  op..**.** Exampl
2e968 65 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 22 61  es:.**.**     "a
2e969 62 63 22 20 20 20 62 65 63 6f 6d 65 73 20 20 20  bc"   becomes   
2e96a 61 62 63 0a 2a 2a 20 20 20 20 20 27 78 79 7a 27  abc.**     'xyz'
2e96b 20 20 20 62 65 63 6f 6d 65 73 20 20 20 78 79 7a     becomes   xyz
2e96c 0a 2a 2a 20 20 20 20 20 5b 70 71 72 5d 20 20 20  .**     [pqr]   
2e96d 62 65 63 6f 6d 65 73 20 20 20 70 71 72 0a 2a 2a  becomes   pqr.**
2e96e 20 20 20 20 20 60 6d 6e 6f 60 20 20 20 62 65 63       `mno`   bec
2e96f 6f 6d 65 73 20 20 20 6d 6e 6f 0a 2a 2f 0a 73 74  omes   mno.*/.st
2e970 61 74 69 63 20 76 6f 69 64 20 64 65 71 75 6f 74  atic void dequot
2e971 65 53 74 72 69 6e 67 28 63 68 61 72 20 2a 7a 29  eString(char *z)
2e972 7b 0a 20 20 69 6e 74 20 71 75 6f 74 65 3b 0a 20  {.  int quote;. 
2e973 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69 66 28   int i, j;.  if(
2e974 20 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b 0a   z==0 ) return;.
2e975 20 20 71 75 6f 74 65 20 3d 20 7a 5b 30 5d 3b 0a    quote = z[0];.
2e976 20 20 73 77 69 74 63 68 28 20 71 75 6f 74 65 20    switch( quote 
2e977 29 7b 0a 20 20 20 20 63 61 73 65 20 27 5c 27 27  ){.    case '\''
2e978 3a 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 63 61  :  break;.    ca
2e979 73 65 20 27 22 27 3a 20 20 20 62 72 65 61 6b 3b  se '"':   break;
2e97a 0a 20 20 20 20 63 61 73 65 20 27 60 27 3a 20 20  .    case '`':  
2e97b 20 62 72 65 61 6b 3b 20 20 20 20 20 20 20 20 20   break;         
2e97c 20 20 20 20 20 20 20 2f 2a 20 46 6f 72 20 4d 79         /* For My
2e97d 53 51 4c 20 63 6f 6d 70 61 74 69 62 69 6c 69 74  SQL compatibilit
2e97e 79 20 2a 2f 0a 20 20 20 20 63 61 73 65 20 27 5b  y */.    case '[
2e97f 27 3a 20 20 20 71 75 6f 74 65 20 3d 20 27 5d 27  ':   quote = ']'
2e980 3b 20 20 62 72 65 61 6b 3b 20 20 2f 2a 20 46 6f  ;  break;  /* Fo
2e981 72 20 4d 53 20 53 71 6c 53 65 72 76 65 72 20 63  r MS SqlServer c
2e982 6f 6d 70 61 74 69 62 69 6c 69 74 79 20 2a 2f 0a  ompatibility */.
2e983 20 20 20 20 64 65 66 61 75 6c 74 3a 20 20 20 20      default:    
2e984 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 66 6f  return;.  }.  fo
2e985 72 28 69 3d 31 2c 20 6a 3d 30 3b 20 7a 5b 69 5d  r(i=1, j=0; z[i]
2e986 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20  ; i++){.    if( 
2e987 7a 5b 69 5d 3d 3d 71 75 6f 74 65 20 29 7b 0a 20  z[i]==quote ){. 
2e988 20 20 20 20 20 69 66 28 20 7a 5b 69 2b 31 5d 3d       if( z[i+1]=
2e989 3d 71 75 6f 74 65 20 29 7b 0a 20 20 20 20 20 20  =quote ){.      
2e98a 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 71 75 6f 74 65    z[j++] = quote
2e98b 3b 0a 20 20 20 20 20 20 20 20 69 2b 2b 3b 0a 20  ;.        i++;. 
2e98c 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
2e98d 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d 20 30 3b 0a      z[j++] = 0;.
2e98e 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
2e98f 20 20 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65       }.    }else
2e990 7b 0a 20 20 20 20 20 20 7a 5b 6a 2b 2b 5d 20 3d  {.      z[j++] =
2e991 20 7a 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 7d   z[i];.    }.  }
2e992 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 69 6e  .}../*.** The in
2e993 70 75 74 20 61 7a 49 6e 20 69 73 20 61 20 4e 55  put azIn is a NU
2e994 4c 4c 2d 74 65 72 6d 69 6e 61 74 65 64 20 6c 69  LL-terminated li
2e995 73 74 20 6f 66 20 74 6f 6b 65 6e 73 2e 20 20 52  st of tokens.  R
2e996 65 6d 6f 76 65 20 74 68 65 20 66 69 72 73 74 0a  emove the first.
2e997 2a 2a 20 74 6f 6b 65 6e 20 61 6e 64 20 61 6c 6c  ** token and all
2e998 20 70 75 6e 63 74 75 61 74 69 6f 6e 20 74 6f 6b   punctuation tok
2e999 65 6e 73 2e 20 20 52 65 6d 6f 76 65 20 74 68 65  ens.  Remove the
2e99a 20 71 75 6f 74 65 73 20 66 72 6f 6d 0a 2a 2a 20   quotes from.** 
2e99b 61 72 6f 75 6e 64 20 73 74 72 69 6e 67 20 6c 69  around string li
2e99c 74 65 72 61 6c 20 74 6f 6b 65 6e 73 2e 0a 2a 2a  teral tokens..**
2e99d 0a 2a 2a 20 45 78 61 6d 70 6c 65 3a 0a 2a 2a 0a  .** Example:.**.
2e99e 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a 20 20 20  **     input:   
2e99f 20 20 20 74 6f 6b 65 6e 69 7a 65 20 63 68 69 6e     tokenize chin
2e9a0 65 73 65 20 28 20 27 73 69 6d 70 6c 69 66 65 64  ese ( 'simplifed
2e9a1 27 20 2c 20 27 6d 69 78 65 64 27 20 29 0a 2a 2a  ' , 'mixed' ).**
2e9a2 20 20 20 20 20 6f 75 74 70 75 74 3a 20 20 20 20       output:    
2e9a3 20 63 68 69 6e 65 73 65 20 73 69 6d 70 6c 69 66   chinese simplif
2e9a4 65 64 20 6d 69 78 65 64 0a 2a 2a 0a 2a 2a 20 41  ed mixed.**.** A
2e9a5 6e 6f 74 68 65 72 20 65 78 61 6d 70 6c 65 3a 0a  nother example:.
2e9a6 2a 2a 0a 2a 2a 20 20 20 20 20 69 6e 70 75 74 3a  **.**     input:
2e9a7 20 20 20 20 20 20 64 65 6c 69 6d 69 74 65 72 73        delimiters
2e9a8 20 28 20 27 5b 27 20 2c 20 27 5d 27 20 2c 20 27   ( '[' , ']' , '
2e9a9 2e 2e 2e 27 20 29 0a 2a 2a 20 20 20 20 20 6f 75  ...' ).**     ou
2e9aa 74 70 75 74 3a 20 20 20 20 20 5b 20 5d 20 2e 2e  tput:     [ ] ..
2e9ab 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
2e9ac 20 74 6f 6b 65 6e 4c 69 73 74 54 6f 49 64 4c 69   tokenListToIdLi
2e9ad 73 74 28 63 68 61 72 20 2a 2a 61 7a 49 6e 29 7b  st(char **azIn){
2e9ae 0a 20 20 69 6e 74 20 69 2c 20 6a 3b 0a 20 20 69  .  int i, j;.  i
2e9af 66 28 20 61 7a 49 6e 20 29 7b 0a 20 20 20 20 66  f( azIn ){.    f
2e9b0 6f 72 28 69 3d 30 2c 20 6a 3d 2d 31 3b 20 61 7a  or(i=0, j=-1; az
2e9b1 49 6e 5b 69 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20  In[i]; i++){.   
2e9b2 20 20 20 69 66 28 20 73 61 66 65 5f 69 73 61 6c     if( safe_isal
2e9b3 6e 75 6d 28 61 7a 49 6e 5b 69 5d 5b 30 5d 29 20  num(azIn[i][0]) 
2e9b4 7c 7c 20 61 7a 49 6e 5b 69 5d 5b 31 5d 20 29 7b  || azIn[i][1] ){
2e9b5 0a 20 20 20 20 20 20 20 20 64 65 71 75 6f 74 65  .        dequote
2e9b6 53 74 72 69 6e 67 28 61 7a 49 6e 5b 69 5d 29 3b  String(azIn[i]);
2e9b7 0a 20 20 20 20 20 20 20 20 69 66 28 20 6a 3e 3d  .        if( j>=
2e9b8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 61  0 ){.          a
2e9b9 7a 49 6e 5b 6a 5d 20 3d 20 61 7a 49 6e 5b 69 5d  zIn[j] = azIn[i]
2e9ba 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
2e9bb 20 20 20 20 6a 2b 2b 3b 0a 20 20 20 20 20 20 7d      j++;.      }
2e9bc 0a 20 20 20 20 7d 0a 20 20 20 20 61 7a 49 6e 5b  .    }.    azIn[
2e9bd 6a 5d 20 3d 20 30 3b 0a 20 20 7d 0a 7d 0a 0a 0a  j] = 0;.  }.}...
2e9be 2f 2a 0a 2a 2a 20 46 69 6e 64 20 74 68 65 20 66  /*.** Find the f
2e9bf 69 72 73 74 20 61 6c 70 68 61 6e 75 6d 65 72 69  irst alphanumeri
2e9c0 63 20 74 6f 6b 65 6e 20 69 6e 20 74 68 65 20 73  c token in the s
2e9c1 74 72 69 6e 67 20 7a 49 6e 2e 20 20 4e 75 6c 6c  tring zIn.  Null
2e9c2 2d 74 65 72 6d 69 6e 61 74 65 0a 2a 2a 20 74 68  -terminate.** th
2e9c3 69 73 20 74 6f 6b 65 6e 2e 20 20 52 65 6d 6f 76  is token.  Remov
2e9c4 65 20 61 6e 79 20 71 75 6f 74 61 74 69 6f 6e 20  e any quotation 
2e9c5 6d 61 72 6b 73 2e 20 20 41 6e 64 20 72 65 74 75  marks.  And retu
2e9c6 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 0a  rn a pointer to.
2e9c7 2a 2a 20 74 68 65 20 72 65 73 75 6c 74 2e 0a 2a  ** the result..*
2e9c8 2f 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a 66  /.static char *f
2e9c9 69 72 73 74 54 6f 6b 65 6e 28 63 68 61 72 20 2a  irstToken(char *
2e9ca 7a 49 6e 2c 20 63 68 61 72 20 2a 2a 70 7a 54 61  zIn, char **pzTa
2e9cb 69 6c 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 74 74  il){.  int n, tt
2e9cc 79 70 65 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b  ype;.  while(1){
2e9cd 0a 20 20 20 20 6e 20 3d 20 66 74 73 47 65 74 54  .    n = ftsGetT
2e9ce 6f 6b 65 6e 28 7a 49 6e 2c 20 26 74 74 79 70 65  oken(zIn, &ttype
2e9cf 29 3b 0a 20 20 20 20 69 66 28 20 74 74 79 70 65  );.    if( ttype
2e9d0 3d 3d 54 4f 4b 45 4e 5f 53 50 41 43 45 20 29 7b  ==TOKEN_SPACE ){
2e9d1 0a 20 20 20 20 20 20 7a 49 6e 20 2b 3d 20 6e 3b  .      zIn += n;
2e9d2 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28 20 74  .    }else if( t
2e9d3 74 79 70 65 3d 3d 54 4f 4b 45 4e 5f 45 4f 46 20  type==TOKEN_EOF 
2e9d4 29 7b 0a 20 20 20 20 20 20 2a 70 7a 54 61 69 6c  ){.      *pzTail
2e9d5 20 3d 20 7a 49 6e 3b 0a 20 20 20 20 20 20 72 65   = zIn;.      re
2e9d6 74 75 72 6e 20 30 3b 0a 20 20 20 20 7d 65 6c 73  turn 0;.    }els
2e9d7 65 7b 0a 20 20 20 20 20 20 7a 49 6e 5b 6e 5d 20  e{.      zIn[n] 
2e9d8 3d 20 30 3b 0a 20 20 20 20 20 20 2a 70 7a 54 61  = 0;.      *pzTa
2e9d9 69 6c 20 3d 20 26 7a 49 6e 5b 31 5d 3b 0a 20 20  il = &zIn[1];.  
2e9da 20 20 20 20 64 65 71 75 6f 74 65 53 74 72 69 6e      dequoteStrin
2e9db 67 28 7a 49 6e 29 3b 0a 20 20 20 20 20 20 72 65  g(zIn);.      re
2e9dc 74 75 72 6e 20 7a 49 6e 3b 0a 20 20 20 20 7d 0a  turn zIn;.    }.
2e9dd 20 20 7d 0a 20 20 2f 2a 4e 4f 54 52 45 41 43 48    }.  /*NOTREACH
2e9de 45 44 2a 2f 0a 7d 0a 0a 2f 2a 20 52 65 74 75 72  ED*/.}../* Retur
2e9df 6e 20 74 72 75 65 20 69 66 2e 2e 2e 0a 2a 2a 0a  n true if....**.
2e9e0 2a 2a 20 20 20 2a 20 20 73 20 62 65 67 69 6e 73  **   *  s begins
2e9e1 20 77 69 74 68 20 74 68 65 20 73 74 72 69 6e 67   with the string
2e9e2 20 74 2c 20 69 67 6e 6f 72 69 6e 67 20 63 61 73   t, ignoring cas
2e9e3 65 0a 2a 2a 20 20 20 2a 20 20 73 20 69 73 20 6c  e.**   *  s is l
2e9e4 6f 6e 67 65 72 20 74 68 61 6e 20 74 0a 2a 2a 20  onger than t.** 
2e9e5 20 20 2a 20 20 54 68 65 20 66 69 72 73 74 20 63    *  The first c
2e9e6 68 61 72 61 63 74 65 72 20 6f 66 20 73 20 62 65  haracter of s be
2e9e7 79 6f 6e 64 20 74 20 69 73 20 6e 6f 74 20 61 20  yond t is not a 
2e9e8 61 6c 70 68 61 6e 75 6d 65 72 69 63 0a 2a 2a 20  alphanumeric.** 
2e9e9 0a 2a 2a 20 49 67 6e 6f 72 65 20 6c 65 61 64 69  .** Ignore leadi
2e9ea 6e 67 20 73 70 61 63 65 20 69 6e 20 2a 73 2e 0a  ng space in *s..
2e9eb 2a 2a 0a 2a 2a 20 54 6f 20 70 75 74 20 69 74 20  **.** To put it 
2e9ec 61 6e 6f 74 68 65 72 20 77 61 79 2c 20 72 65 74  another way, ret
2e9ed 75 72 6e 20 74 72 75 65 20 69 66 20 74 68 65 20  urn true if the 
2e9ee 66 69 72 73 74 20 74 6f 6b 65 6e 20 6f 66 0a 2a  first token of.*
2e9ef 2a 20 73 5b 5d 20 69 73 20 74 5b 5d 2e 0a 2a 2f  * s[] is t[]..*/
2e9f0 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 61 72  .static int star
2e9f1 74 73 57 69 74 68 28 63 6f 6e 73 74 20 63 68 61  tsWith(const cha
2e9f2 72 20 2a 73 2c 20 63 6f 6e 73 74 20 63 68 61 72  r *s, const char
2e9f3 20 2a 74 29 7b 0a 20 20 77 68 69 6c 65 28 20 73   *t){.  while( s
2e9f4 61 66 65 5f 69 73 73 70 61 63 65 28 2a 73 29 20  afe_isspace(*s) 
2e9f5 29 7b 20 73 2b 2b 3b 20 7d 0a 20 20 77 68 69 6c  ){ s++; }.  whil
2e9f6 65 28 20 2a 74 20 29 7b 0a 20 20 20 20 69 66 28  e( *t ){.    if(
2e9f7 20 73 61 66 65 5f 74 6f 6c 6f 77 65 72 28 2a 73   safe_tolower(*s
2e9f8 2b 2b 29 21 3d 73 61 66 65 5f 74 6f 6c 6f 77 65  ++)!=safe_tolowe
2e9f9 72 28 2a 74 2b 2b 29 20 29 20 72 65 74 75 72 6e  r(*t++) ) return
2e9fa 20 30 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e   0;.  }.  return
2e9fb 20 2a 73 21 3d 27 5f 27 20 26 26 20 21 73 61 66   *s!='_' && !saf
2e9fc 65 5f 69 73 61 6c 6e 75 6d 28 2a 73 29 3b 0a 7d  e_isalnum(*s);.}
2e9fd 0a 0a 2f 2a 0a 2a 2a 20 41 6e 20 69 6e 73 74 61  ../*.** An insta
2e9fe 6e 63 65 20 6f 66 20 74 68 69 73 20 73 74 72 75  nce of this stru
2e9ff 63 74 75 72 65 20 64 65 66 69 6e 65 73 20 74 68  cture defines th
2ea00 65 20 22 73 70 65 63 22 20 6f 66 20 61 0a 2a 2a  e "spec" of a.**
2ea01 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78   full text index
2ea02 2e 20 20 54 68 69 73 20 73 74 72 75 63 74 75 72  .  This structur
2ea03 65 20 69 73 20 70 6f 70 75 6c 61 74 65 64 20 62  e is populated b
2ea04 79 20 70 61 72 73 65 53 70 65 63 0a 2a 2a 20 61  y parseSpec.** a
2ea05 6e 64 20 75 73 65 20 62 79 20 66 75 6c 6c 74 65  nd use by fullte
2ea06 78 74 43 6f 6e 6e 65 63 74 20 61 6e 64 20 66 75  xtConnect and fu
2ea07 6c 6c 74 65 78 74 43 72 65 61 74 65 2e 0a 2a 2f  lltextCreate..*/
2ea08 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2ea09 54 61 62 6c 65 53 70 65 63 20 7b 0a 20 20 63 6f  TableSpec {.  co
2ea0a 6e 73 74 20 63 68 61 72 20 2a 7a 44 62 3b 20 20  nst char *zDb;  
2ea0b 20 20 20 20 20 20 20 2f 2a 20 4c 6f 67 69 63 61         /* Logica
2ea0c 6c 20 64 61 74 61 62 61 73 65 20 6e 61 6d 65 20  l database name 
2ea0d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2ea0e 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 2f 2a  *zName;       /*
2ea0f 20 4e 61 6d 65 20 6f 66 20 74 68 65 20 66 75 6c   Name of the ful
2ea10 6c 2d 74 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a  l-text index */.
2ea11 20 20 69 6e 74 20 6e 43 6f 6c 75 6d 6e 3b 20 20    int nColumn;  
2ea12 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
2ea13 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20  mber of columns 
2ea14 74 6f 20 62 65 20 69 6e 64 65 78 65 64 20 2a 2f  to be indexed */
2ea15 0a 20 20 63 68 61 72 20 2a 2a 61 7a 43 6f 6c 75  .  char **azColu
2ea16 6d 6e 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 4f  mn;         /* O
2ea17 72 69 67 69 6e 61 6c 20 6e 61 6d 65 73 20 6f 66  riginal names of
2ea18 20 63 6f 6c 75 6d 6e 73 20 74 6f 20 62 65 20 69   columns to be i
2ea19 6e 64 65 78 65 64 20 2a 2f 0a 20 20 63 68 61 72  ndexed */.  char
2ea1a 20 2a 2a 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75   **azContentColu
2ea1b 6d 6e 3b 20 20 2f 2a 20 43 6f 6c 75 6d 6e 20 6e  mn;  /* Column n
2ea1c 61 6d 65 73 20 66 6f 72 20 25 5f 63 6f 6e 74 65  ames for %_conte
2ea1d 6e 74 20 2a 2f 0a 20 20 63 68 61 72 20 2a 2a 61  nt */.  char **a
2ea1e 7a 54 6f 6b 65 6e 69 7a 65 72 3b 20 20 20 20 20  zTokenizer;     
2ea1f 20 2f 2a 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65   /* Name of toke
2ea20 6e 69 7a 65 72 20 61 6e 64 20 69 74 73 20 61 72  nizer and its ar
2ea21 67 75 6d 65 6e 74 73 20 2a 2f 0a 7d 20 54 61 62  guments */.} Tab
2ea22 6c 65 53 70 65 63 3b 0a 0a 2f 2a 0a 2a 2a 20 52  leSpec;../*.** R
2ea23 65 63 6c 61 69 6d 20 61 6c 6c 20 6f 66 20 74 68  eclaim all of th
2ea24 65 20 6d 65 6d 6f 72 79 20 75 73 65 64 20 62 79  e memory used by
2ea25 20 61 20 54 61 62 6c 65 53 70 65 63 0a 2a 2f 0a   a TableSpec.*/.
2ea26 73 74 61 74 69 63 20 76 6f 69 64 20 63 6c 65 61  static void clea
2ea27 72 54 61 62 6c 65 53 70 65 63 28 54 61 62 6c 65  rTableSpec(Table
2ea28 53 70 65 63 20 2a 70 29 20 7b 0a 20 20 73 71 6c  Spec *p) {.  sql
2ea29 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43  ite3_free(p->azC
2ea2a 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71 6c 69 74 65  olumn);.  sqlite
2ea2b 33 5f 66 72 65 65 28 70 2d 3e 61 7a 43 6f 6e 74  3_free(p->azCont
2ea2c 65 6e 74 43 6f 6c 75 6d 6e 29 3b 0a 20 20 73 71  entColumn);.  sq
2ea2d 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 61 7a  lite3_free(p->az
2ea2e 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a 7d 0a 0a 2f  Tokenizer);.}../
2ea2f 2a 20 50 61 72 73 65 20 61 20 43 52 45 41 54 45  * Parse a CREATE
2ea30 20 56 49 52 54 55 41 4c 20 54 41 42 4c 45 20 73   VIRTUAL TABLE s
2ea31 74 61 74 65 6d 65 6e 74 2c 20 77 68 69 63 68 20  tatement, which 
2ea32 6c 6f 6f 6b 73 20 6c 69 6b 65 20 74 68 69 73 3a  looks like this:
2ea33 0a 20 2a 0a 20 2a 20 43 52 45 41 54 45 20 56 49  . *. * CREATE VI
2ea34 52 54 55 41 4c 20 54 41 42 4c 45 20 65 6d 61 69  RTUAL TABLE emai
2ea35 6c 0a 20 2a 20 20 20 20 20 20 20 20 55 53 49 4e  l. *        USIN
2ea36 47 20 66 74 73 33 28 73 75 62 6a 65 63 74 2c 20  G fts3(subject, 
2ea37 62 6f 64 79 2c 20 74 6f 6b 65 6e 69 7a 65 20 6d  body, tokenize m
2ea38 79 74 6f 6b 65 6e 69 7a 65 72 28 6d 79 61 72 67  ytokenizer(myarg
2ea39 29 29 0a 20 2a 0a 20 2a 20 57 65 20 72 65 74 75  )). *. * We retu
2ea3a 72 6e 20 70 61 72 73 65 64 20 69 6e 66 6f 72 6d  rn parsed inform
2ea3b 61 74 69 6f 6e 20 69 6e 20 61 20 54 61 62 6c 65  ation in a Table
2ea3c 53 70 65 63 20 73 74 72 75 63 74 75 72 65 2e 0a  Spec structure..
2ea3d 20 2a 20 0a 20 2a 2f 0a 73 74 61 74 69 63 20 69   * . */.static i
2ea3e 6e 74 20 70 61 72 73 65 53 70 65 63 28 54 61 62  nt parseSpec(Tab
2ea3f 6c 65 53 70 65 63 20 2a 70 53 70 65 63 2c 20 69  leSpec *pSpec, i
2ea40 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
2ea41 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
2ea42 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2ea43 20 20 20 20 20 20 63 68 61 72 2a 2a 70 7a 45 72        char**pzEr
2ea44 72 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6e 3b 0a  r){.  int i, n;.
2ea45 20 20 63 68 61 72 20 2a 7a 2c 20 2a 7a 44 75 6d    char *z, *zDum
2ea46 6d 79 3b 0a 20 20 63 68 61 72 20 2a 2a 61 7a 41  my;.  char **azA
2ea47 72 67 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72  rg;.  const char
2ea48 20 2a 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30   *zTokenizer = 0
2ea49 3b 20 20 20 20 2f 2a 20 61 72 67 76 5b 5d 20 65  ;    /* argv[] e
2ea4a 6e 74 72 79 20 64 65 73 63 72 69 62 69 6e 67 20  ntry describing 
2ea4b 74 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f  the tokenizer */
2ea4c 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
2ea4d 3e 3d 33 20 29 3b 0a 20 20 2f 2a 20 43 75 72 72  >=3 );.  /* Curr
2ea4e 65 6e 74 20 69 6e 74 65 72 66 61 63 65 3a 0a 20  ent interface:. 
2ea4f 20 2a 2a 20 61 72 67 76 5b 30 5d 20 2d 20 6d 6f   ** argv[0] - mo
2ea50 64 75 6c 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61  dule name.  ** a
2ea51 72 67 76 5b 31 5d 20 2d 20 64 61 74 61 62 61 73  rgv[1] - databas
2ea52 65 20 6e 61 6d 65 0a 20 20 2a 2a 20 61 72 67 76  e name.  ** argv
2ea53 5b 32 5d 20 2d 20 74 61 62 6c 65 20 6e 61 6d 65  [2] - table name
2ea54 0a 20 20 2a 2a 20 61 72 67 76 5b 33 2e 2e 5d 20  .  ** argv[3..] 
2ea55 2d 20 63 6f 6c 75 6d 6e 73 2c 20 6f 70 74 69 6f  - columns, optio
2ea56 6e 61 6c 6c 79 20 66 6f 6c 6c 6f 77 65 64 20 62  nally followed b
2ea57 79 20 74 6f 6b 65 6e 69 7a 65 72 20 73 70 65 63  y tokenizer spec
2ea58 69 66 69 63 61 74 69 6f 6e 0a 20 20 2a 2a 20 20  ification.  **  
2ea59 20 20 20 20 20 20 20 20 20 20 20 61 6e 64 20 73             and s
2ea5a 6e 69 70 70 65 74 20 64 65 6c 69 6d 69 74 65 72  nippet delimiter
2ea5b 73 20 73 70 65 63 69 66 69 63 61 74 69 6f 6e 2e  s specification.
2ea5c 0a 20 20 2a 2f 0a 0a 20 20 2f 2a 20 4d 61 6b 65  .  */..  /* Make
2ea5d 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 63   a copy of the c
2ea5e 6f 6d 70 6c 65 74 65 20 61 72 67 76 5b 5d 5b 5d  omplete argv[][]
2ea5f 20 61 72 72 61 79 20 69 6e 20 61 20 73 69 6e 67   array in a sing
2ea60 6c 65 20 61 6c 6c 6f 63 61 74 69 6f 6e 2e 0a 20  le allocation.. 
2ea61 20 2a 2a 20 54 68 65 20 61 72 67 76 5b 5d 5b 5d   ** The argv[][]
2ea62 20 61 72 72 61 79 20 69 73 20 72 65 61 64 2d 6f   array is read-o
2ea63 6e 6c 79 20 61 6e 64 20 74 72 61 6e 73 69 65 6e  nly and transien
2ea64 74 2e 20 20 57 65 20 63 61 6e 20 77 72 69 74 65  t.  We can write
2ea65 20 74 6f 20 74 68 65 0a 20 20 2a 2a 20 63 6f 70   to the.  ** cop
2ea66 79 20 69 6e 20 6f 72 64 65 72 20 74 6f 20 6d 6f  y in order to mo
2ea67 64 69 66 79 20 74 68 69 6e 67 73 20 61 6e 64 20  dify things and 
2ea68 74 68 65 20 63 6f 70 79 20 69 73 20 70 65 72 73  the copy is pers
2ea69 69 73 74 65 6e 74 2e 0a 20 20 2a 2f 0a 20 20 43  istent..  */.  C
2ea6a 4c 45 41 52 28 70 53 70 65 63 29 3b 0a 20 20 66  LEAR(pSpec);.  f
2ea6b 6f 72 28 69 3d 6e 3d 30 3b 20 69 3c 61 72 67 63  or(i=n=0; i<argc
2ea6c 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 6e 20 2b 3d  ; i++){.    n +=
2ea6d 20 73 74 72 6c 65 6e 28 61 72 67 76 5b 69 5d 29   strlen(argv[i])
2ea6e 20 2b 20 31 3b 0a 20 20 7d 0a 20 20 61 7a 41 72   + 1;.  }.  azAr
2ea6f 67 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  g = sqlite3_mall
2ea70 6f 63 28 20 73 69 7a 65 6f 66 28 63 68 61 72 2a  oc( sizeof(char*
2ea71 29 2a 61 72 67 63 20 2b 20 6e 20 29 3b 0a 20 20  )*argc + n );.  
2ea72 69 66 28 20 61 7a 41 72 67 3d 3d 30 20 29 7b 0a  if( azArg==0 ){.
2ea73 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2ea74 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 7a  E_NOMEM;.  }.  z
2ea75 20 3d 20 28 63 68 61 72 2a 29 26 61 7a 41 72 67   = (char*)&azArg
2ea76 5b 61 72 67 63 5d 3b 0a 20 20 66 6f 72 28 69 3d  [argc];.  for(i=
2ea77 30 3b 20 69 3c 61 72 67 63 3b 20 69 2b 2b 29 7b  0; i<argc; i++){
2ea78 0a 20 20 20 20 61 7a 41 72 67 5b 69 5d 20 3d 20  .    azArg[i] = 
2ea79 7a 3b 0a 20 20 20 20 73 74 72 63 70 79 28 7a 2c  z;.    strcpy(z,
2ea7a 20 61 72 67 76 5b 69 5d 29 3b 0a 20 20 20 20 7a   argv[i]);.    z
2ea7b 20 2b 3d 20 73 74 72 6c 65 6e 28 7a 29 2b 31 3b   += strlen(z)+1;
2ea7c 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 64 65 6e 74  .  }..  /* Ident
2ea7d 69 66 79 20 74 68 65 20 63 6f 6c 75 6d 6e 20 6e  ify the column n
2ea7e 61 6d 65 73 20 61 6e 64 20 74 68 65 20 74 6f 6b  ames and the tok
2ea7f 65 6e 69 7a 65 72 20 61 6e 64 20 64 65 6c 69 6d  enizer and delim
2ea80 69 74 65 72 20 61 72 67 75 6d 65 6e 74 73 0a 20  iter arguments. 
2ea81 20 2a 2a 20 69 6e 20 74 68 65 20 61 72 67 76 5b   ** in the argv[
2ea82 5d 5b 5d 20 61 72 72 61 79 2e 0a 20 20 2a 2f 0a  ][] array..  */.
2ea83 20 20 70 53 70 65 63 2d 3e 7a 44 62 20 3d 20 61    pSpec->zDb = a
2ea84 7a 41 72 67 5b 31 5d 3b 0a 20 20 70 53 70 65 63  zArg[1];.  pSpec
2ea85 2d 3e 7a 4e 61 6d 65 20 3d 20 61 7a 41 72 67 5b  ->zName = azArg[
2ea86 32 5d 3b 0a 20 20 70 53 70 65 63 2d 3e 6e 43 6f  2];.  pSpec->nCo
2ea87 6c 75 6d 6e 20 3d 20 30 3b 0a 20 20 70 53 70 65  lumn = 0;.  pSpe
2ea88 63 2d 3e 61 7a 43 6f 6c 75 6d 6e 20 3d 20 61 7a  c->azColumn = az
2ea89 41 72 67 3b 0a 20 20 7a 54 6f 6b 65 6e 69 7a 65  Arg;.  zTokenize
2ea8a 72 20 3d 20 22 74 6f 6b 65 6e 69 7a 65 20 73 69  r = "tokenize si
2ea8b 6d 70 6c 65 22 3b 0a 20 20 66 6f 72 28 69 3d 33  mple";.  for(i=3
2ea8c 3b 20 69 3c 61 72 67 63 3b 20 2b 2b 69 29 7b 0a  ; i<argc; ++i){.
2ea8d 20 20 20 20 69 66 28 20 73 74 61 72 74 73 57 69      if( startsWi
2ea8e 74 68 28 61 7a 41 72 67 5b 69 5d 2c 22 74 6f 6b  th(azArg[i],"tok
2ea8f 65 6e 69 7a 65 22 29 20 29 7b 0a 20 20 20 20 20  enize") ){.     
2ea90 20 7a 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 61 7a   zTokenizer = az
2ea91 41 72 67 5b 69 5d 3b 0a 20 20 20 20 7d 65 6c 73  Arg[i];.    }els
2ea92 65 7b 0a 20 20 20 20 20 20 7a 20 3d 20 61 7a 41  e{.      z = azA
2ea93 72 67 5b 70 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d  rg[pSpec->nColum
2ea94 6e 5d 20 3d 20 66 69 72 73 74 54 6f 6b 65 6e 28  n] = firstToken(
2ea95 61 7a 41 72 67 5b 69 5d 2c 20 26 7a 44 75 6d 6d  azArg[i], &zDumm
2ea96 79 29 3b 0a 20 20 20 20 20 20 70 53 70 65 63 2d  y);.      pSpec-
2ea97 3e 6e 43 6f 6c 75 6d 6e 2b 2b 3b 0a 20 20 20 20  >nColumn++;.    
2ea98 7d 0a 20 20 7d 0a 20 20 69 66 28 20 70 53 70 65  }.  }.  if( pSpe
2ea99 63 2d 3e 6e 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b  c->nColumn==0 ){
2ea9a 0a 20 20 20 20 61 7a 41 72 67 5b 30 5d 20 3d 20  .    azArg[0] = 
2ea9b 22 63 6f 6e 74 65 6e 74 22 3b 0a 20 20 20 20 70  "content";.    p
2ea9c 53 70 65 63 2d 3e 6e 43 6f 6c 75 6d 6e 20 3d 20  Spec->nColumn = 
2ea9d 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a  1;.  }..  /*.  *
2ea9e 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65 20  * Construct the 
2ea9f 6c 69 73 74 20 6f 66 20 63 6f 6e 74 65 6e 74 20  list of content 
2eaa0 63 6f 6c 75 6d 6e 20 6e 61 6d 65 73 2e 0a 20 20  column names..  
2eaa1 2a 2a 0a 20 20 2a 2a 20 45 61 63 68 20 63 6f 6e  **.  ** Each con
2eaa2 74 65 6e 74 20 63 6f 6c 75 6d 6e 20 6e 61 6d 65  tent column name
2eaa3 20 77 69 6c 6c 20 62 65 20 6f 66 20 74 68 65 20   will be of the 
2eaa4 66 6f 72 6d 20 63 4e 4e 41 41 41 41 0a 20 20 2a  form cNNAAAA.  *
2eaa5 2a 20 77 68 65 72 65 20 4e 4e 20 69 73 20 74 68  * where NN is th
2eaa6 65 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20  e column number 
2eaa7 61 6e 64 20 41 41 41 41 20 69 73 20 74 68 65 20  and AAAA is the 
2eaa8 73 61 6e 69 74 69 7a 65 64 0a 20 20 2a 2a 20 63  sanitized.  ** c
2eaa9 6f 6c 75 6d 6e 20 6e 61 6d 65 2e 20 20 22 73 61  olumn name.  "sa
2eaaa 6e 69 74 69 7a 65 64 22 20 6d 65 61 6e 73 20 74  nitized" means t
2eaab 68 61 74 20 73 70 65 63 69 61 6c 20 63 68 61 72  hat special char
2eaac 61 63 74 65 72 73 20 61 72 65 0a 20 20 2a 2a 20  acters are.  ** 
2eaad 63 6f 6e 76 65 72 74 65 64 20 74 6f 20 22 5f 22  converted to "_"
2eaae 2e 20 20 54 68 65 20 63 4e 4e 20 70 72 65 66 69  .  The cNN prefi
2eaaf 78 20 67 75 61 72 61 6e 74 65 65 73 20 74 68 61  x guarantees tha
2eab0 74 20 61 6c 6c 20 63 6f 6c 75 6d 6e 0a 20 20 2a  t all column.  *
2eab1 2a 20 6e 61 6d 65 73 20 61 72 65 20 75 6e 69 71  * names are uniq
2eab2 75 65 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 54 68  ue..  **.  ** Th
2eab3 65 20 41 41 41 41 20 73 75 66 66 69 78 20 69 73  e AAAA suffix is
2eab4 20 6e 6f 74 20 73 74 72 69 63 74 6c 79 20 6e 65   not strictly ne
2eab5 63 65 73 73 61 72 79 2e 20 20 49 74 20 69 73 20  cessary.  It is 
2eab6 69 6e 63 6c 75 64 65 64 0a 20 20 2a 2a 20 66 6f  included.  ** fo
2eab7 72 20 74 68 65 20 63 6f 6e 76 65 6e 69 65 6e 63  r the convenienc
2eab8 65 20 6f 66 20 70 65 6f 70 6c 65 20 77 68 6f 20  e of people who 
2eab9 6d 69 67 68 74 20 65 78 61 6d 69 6e 65 20 74 68  might examine th
2eaba 65 20 67 65 6e 65 72 61 74 65 64 0a 20 20 2a 2a  e generated.  **
2eabb 20 25 5f 63 6f 6e 74 65 6e 74 20 74 61 62 6c 65   %_content table
2eabc 20 61 6e 64 20 77 6f 6e 64 65 72 20 77 68 61 74   and wonder what
2eabd 20 74 68 65 20 63 6f 6c 75 6d 6e 73 20 61 72 65   the columns are
2eabe 20 75 73 65 64 20 66 6f 72 2e 0a 20 20 2a 2f 0a   used for..  */.
2eabf 20 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e 74 65    pSpec->azConte
2eac0 6e 74 43 6f 6c 75 6d 6e 20 3d 20 73 71 6c 69 74  ntColumn = sqlit
2eac1 65 33 5f 6d 61 6c 6c 6f 63 28 20 70 53 70 65 63  e3_malloc( pSpec
2eac2 2d 3e 6e 43 6f 6c 75 6d 6e 20 2a 20 73 69 7a 65  ->nColumn * size
2eac3 6f 66 28 63 68 61 72 20 2a 29 20 29 3b 0a 20 20  of(char *) );.  
2eac4 69 66 28 20 70 53 70 65 63 2d 3e 61 7a 43 6f 6e  if( pSpec->azCon
2eac5 74 65 6e 74 43 6f 6c 75 6d 6e 3d 3d 30 20 29 7b  tentColumn==0 ){
2eac6 0a 20 20 20 20 63 6c 65 61 72 54 61 62 6c 65 53  .    clearTableS
2eac7 70 65 63 28 70 53 70 65 63 29 3b 0a 20 20 20 20  pec(pSpec);.    
2eac8 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f  return SQLITE_NO
2eac9 4d 45 4d 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69  MEM;.  }.  for(i
2eaca 3d 30 3b 20 69 3c 70 53 70 65 63 2d 3e 6e 43 6f  =0; i<pSpec->nCo
2eacb 6c 75 6d 6e 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  lumn; i++){.    
2eacc 63 68 61 72 20 2a 70 3b 0a 20 20 20 20 70 53 70  char *p;.    pSp
2eacd 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  ec->azContentCol
2eace 75 6d 6e 5b 69 5d 20 3d 20 73 71 6c 69 74 65 33  umn[i] = sqlite3
2eacf 5f 6d 70 72 69 6e 74 66 28 22 63 25 64 25 73 22  _mprintf("c%d%s"
2ead0 2c 20 69 2c 20 61 7a 41 72 67 5b 69 5d 29 3b 0a  , i, azArg[i]);.
2ead1 20 20 20 20 66 6f 72 20 28 70 20 3d 20 70 53 70      for (p = pSp
2ead2 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c  ec->azContentCol
2ead3 75 6d 6e 5b 69 5d 3b 20 2a 70 20 3b 20 2b 2b 70  umn[i]; *p ; ++p
2ead4 29 20 7b 0a 20 20 20 20 20 20 69 66 28 20 21 73  ) {.      if( !s
2ead5 61 66 65 5f 69 73 61 6c 6e 75 6d 28 2a 70 29 20  afe_isalnum(*p) 
2ead6 29 20 2a 70 20 3d 20 27 5f 27 3b 0a 20 20 20 20  ) *p = '_';.    
2ead7 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 0a 20 20 2a 2a  }.  }..  /*.  **
2ead8 20 50 61 72 73 65 20 74 68 65 20 74 6f 6b 65 6e   Parse the token
2ead9 69 7a 65 72 20 73 70 65 63 69 66 69 63 61 74 69  izer specificati
2eada 6f 6e 20 73 74 72 69 6e 67 2e 0a 20 20 2a 2f 0a  on string..  */.
2eadb 20 20 70 53 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e    pSpec->azToken
2eadc 69 7a 65 72 20 3d 20 74 6f 6b 65 6e 69 7a 65 53  izer = tokenizeS
2eadd 74 72 69 6e 67 28 7a 54 6f 6b 65 6e 69 7a 65 72  tring(zTokenizer
2eade 2c 20 26 6e 29 3b 0a 20 20 74 6f 6b 65 6e 4c 69  , &n);.  tokenLi
2eadf 73 74 54 6f 49 64 4c 69 73 74 28 70 53 70 65 63  stToIdList(pSpec
2eae0 2d 3e 61 7a 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  ->azTokenizer);.
2eae1 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2eae2 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 47 65  _OK;.}../*.** Ge
2eae3 6e 65 72 61 74 65 20 61 20 43 52 45 41 54 45 20  nerate a CREATE 
2eae4 54 41 42 4c 45 20 73 74 61 74 65 6d 65 6e 74 20  TABLE statement 
2eae5 74 68 61 74 20 64 65 73 63 72 69 62 65 73 20 74  that describes t
2eae6 68 65 20 73 63 68 65 6d 61 20 6f 66 0a 2a 2a 20  he schema of.** 
2eae7 74 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c  the virtual tabl
2eae8 65 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  e.  Return a poi
2eae9 6e 74 65 72 20 74 6f 20 74 68 69 73 20 73 63 68  nter to this sch
2eaea 65 6d 61 20 73 74 72 69 6e 67 2e 0a 2a 2a 0a 2a  ema string..**.*
2eaeb 2a 20 53 70 61 63 65 20 69 73 20 6f 62 74 61 69  * Space is obtai
2eaec 6e 65 64 20 66 72 6f 6d 20 73 71 6c 69 74 65 33  ned from sqlite3
2eaed 5f 6d 70 72 69 6e 74 66 28 29 20 61 6e 64 20 73  _mprintf() and s
2eaee 68 6f 75 6c 64 20 62 65 20 66 72 65 65 64 0a 2a  hould be freed.*
2eaef 2a 20 75 73 69 6e 67 20 73 71 6c 69 74 65 33 5f  * using sqlite3_
2eaf0 66 72 65 65 28 29 2e 0a 2a 2f 0a 73 74 61 74 69  free()..*/.stati
2eaf1 63 20 63 68 61 72 20 2a 66 75 6c 6c 74 65 78 74  c char *fulltext
2eaf2 53 63 68 65 6d 61 28 0a 20 20 69 6e 74 20 6e 43  Schema(.  int nC
2eaf3 6f 6c 75 6d 6e 2c 20 20 20 20 20 20 20 20 20 20  olumn,          
2eaf4 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2eaf5 72 20 6f 66 20 63 6f 6c 75 6d 6e 73 20 2a 2f 0a  r of columns */.
2eaf6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f    const char *co
2eaf7 6e 73 74 2a 20 61 7a 43 6f 6c 75 6d 6e 2c 20 20  nst* azColumn,  
2eaf8 2f 2a 20 4c 69 73 74 20 6f 66 20 63 6f 6c 75 6d  /* List of colum
2eaf9 6e 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68  ns */.  const ch
2eafa 61 72 20 2a 7a 54 61 62 6c 65 4e 61 6d 65 20 20  ar *zTableName  
2eafb 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f 66        /* Name of
2eafc 20 74 68 65 20 74 61 62 6c 65 20 2a 2f 0a 29 7b   the table */.){
2eafd 0a 20 20 69 6e 74 20 69 3b 0a 20 20 63 68 61 72  .  int i;.  char
2eafe 20 2a 7a 53 63 68 65 6d 61 2c 20 2a 7a 4e 65 78   *zSchema, *zNex
2eaff 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  t;.  const char 
2eb00 2a 7a 53 65 70 20 3d 20 22 28 22 3b 0a 20 20 7a  *zSep = "(";.  z
2eb01 53 63 68 65 6d 61 20 3d 20 73 71 6c 69 74 65 33  Schema = sqlite3
2eb02 5f 6d 70 72 69 6e 74 66 28 22 43 52 45 41 54 45  _mprintf("CREATE
2eb03 20 54 41 42 4c 45 20 78 22 29 3b 0a 20 20 66 6f   TABLE x");.  fo
2eb04 72 28 69 3d 30 3b 20 69 3c 6e 43 6f 6c 75 6d 6e  r(i=0; i<nColumn
2eb05 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 7a 4e 65 78  ; i++){.    zNex
2eb06 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  t = sqlite3_mpri
2eb07 6e 74 66 28 22 25 73 25 73 25 51 22 2c 20 7a 53  ntf("%s%s%Q", zS
2eb08 63 68 65 6d 61 2c 20 7a 53 65 70 2c 20 61 7a 43  chema, zSep, azC
2eb09 6f 6c 75 6d 6e 5b 69 5d 29 3b 0a 20 20 20 20 73  olumn[i]);.    s
2eb0a 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 63 68  qlite3_free(zSch
2eb0b 65 6d 61 29 3b 0a 20 20 20 20 7a 53 63 68 65 6d  ema);.    zSchem
2eb0c 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20 20 20 7a  a = zNext;.    z
2eb0d 53 65 70 20 3d 20 22 2c 22 3b 0a 20 20 7d 0a 20  Sep = ",";.  }. 
2eb0e 20 7a 4e 65 78 74 20 3d 20 73 71 6c 69 74 65 33   zNext = sqlite3
2eb0f 5f 6d 70 72 69 6e 74 66 28 22 25 73 2c 25 51 20  _mprintf("%s,%Q 
2eb10 48 49 44 44 45 4e 22 2c 20 7a 53 63 68 65 6d 61  HIDDEN", zSchema
2eb11 2c 20 7a 54 61 62 6c 65 4e 61 6d 65 29 3b 0a 20  , zTableName);. 
2eb12 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
2eb13 63 68 65 6d 61 29 3b 0a 20 20 7a 53 63 68 65 6d  chema);.  zSchem
2eb14 61 20 3d 20 7a 4e 65 78 74 3b 0a 20 20 7a 4e 65  a = zNext;.  zNe
2eb15 78 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  xt = sqlite3_mpr
2eb16 69 6e 74 66 28 22 25 73 2c 64 6f 63 69 64 20 48  intf("%s,docid H
2eb17 49 44 44 45 4e 29 22 2c 20 7a 53 63 68 65 6d 61  IDDEN)", zSchema
2eb18 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
2eb19 65 28 7a 53 63 68 65 6d 61 29 3b 0a 20 20 72 65  e(zSchema);.  re
2eb1a 74 75 72 6e 20 7a 4e 65 78 74 3b 0a 7d 0a 0a 2f  turn zNext;.}../
2eb1b 2a 0a 2a 2a 20 42 75 69 6c 64 20 61 20 6e 65 77  *.** Build a new
2eb1c 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 73 74   sqlite3_vtab st
2eb1d 72 75 63 74 75 72 65 20 74 68 61 74 20 77 69 6c  ructure that wil
2eb1e 6c 20 64 65 73 63 72 69 62 65 20 74 68 65 0a 2a  l describe the.*
2eb1f 2a 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78  * fulltext index
2eb20 20 64 65 66 69 6e 65 64 20 62 79 20 73 70 65 63   defined by spec
2eb21 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2eb22 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28 0a 20  constructVtab(. 
2eb23 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 20 20   sqlite3 *db,   
2eb24 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2eb25 65 20 53 51 4c 69 74 65 20 64 61 74 61 62 61 73  e SQLite databas
2eb26 65 20 63 6f 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a  e connection */.
2eb27 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 61 73    fts3Hash *pHas
2eb28 68 2c 20 20 20 20 20 20 20 20 20 20 2f 2a 20 48  h,          /* H
2eb29 61 73 68 20 74 61 62 6c 65 20 63 6f 6e 74 61 69  ash table contai
2eb2a 6e 69 6e 67 20 74 6f 6b 65 6e 69 7a 65 72 73 20  ning tokenizers 
2eb2b 2a 2f 0a 20 20 54 61 62 6c 65 53 70 65 63 20 2a  */.  TableSpec *
2eb2c 73 70 65 63 2c 20 20 20 20 20 20 20 20 20 20 2f  spec,          /
2eb2d 2a 20 50 61 72 73 65 64 20 73 70 65 63 20 69 6e  * Parsed spec in
2eb2e 66 6f 72 6d 61 74 69 6f 6e 20 66 72 6f 6d 20 70  formation from p
2eb2f 61 72 73 65 53 70 65 63 28 29 20 2a 2f 0a 20 20  arseSpec() */.  
2eb30 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a 70  sqlite3_vtab **p
2eb31 70 56 54 61 62 2c 20 20 20 20 2f 2a 20 57 72 69  pVTab,    /* Wri
2eb32 74 65 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67  te the resulting
2eb33 20 76 74 61 62 20 73 74 72 75 63 74 75 72 65 20   vtab structure 
2eb34 68 65 72 65 20 2a 2f 0a 20 20 63 68 61 72 20 2a  here */.  char *
2eb35 2a 70 7a 45 72 72 20 20 20 20 20 20 20 20 20 20  *pzErr          
2eb36 20 20 20 20 2f 2a 20 57 72 69 74 65 20 61 6e 79      /* Write any
2eb37 20 65 72 72 6f 72 20 6d 65 73 73 61 67 65 20 68   error message h
2eb38 65 72 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ere */.){.  int 
2eb39 72 63 3b 0a 20 20 69 6e 74 20 6e 3b 0a 20 20 66  rc;.  int n;.  f
2eb3a 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 20  ulltext_vtab *v 
2eb3b 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c  = 0;.  const sql
2eb3c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
2eb3d 6f 64 75 6c 65 20 2a 6d 20 3d 20 4e 55 4c 4c 3b  odule *m = NULL;
2eb3e 0a 20 20 63 68 61 72 20 2a 73 63 68 65 6d 61 3b  .  char *schema;
2eb3f 0a 0a 20 20 63 68 61 72 20 63 6f 6e 73 74 20 2a  ..  char const *
2eb40 7a 54 6f 6b 3b 20 20 20 20 20 20 20 20 20 2f 2a  zTok;         /*
2eb41 20 4e 61 6d 65 20 6f 66 20 74 6f 6b 65 6e 69 7a   Name of tokeniz
2eb42 65 72 20 74 6f 20 75 73 65 20 66 6f 72 20 74 68  er to use for th
2eb43 69 73 20 66 74 73 20 74 61 62 6c 65 20 2a 2f 0a  is fts table */.
2eb44 20 20 69 6e 74 20 6e 54 6f 6b 3b 20 20 20 20 20    int nTok;     
2eb45 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c              /* L
2eb46 65 6e 67 74 68 20 6f 66 20 7a 54 6f 6b 2c 20 69  ength of zTok, i
2eb47 6e 63 6c 75 64 69 6e 67 20 6e 75 6c 20 74 65 72  ncluding nul ter
2eb48 6d 69 6e 61 74 6f 72 20 2a 2f 0a 0a 20 20 76 20  minator */..  v 
2eb49 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  = (fulltext_vtab
2eb4a 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c   *) sqlite3_mall
2eb4b 6f 63 28 73 69 7a 65 6f 66 28 66 75 6c 6c 74 65  oc(sizeof(fullte
2eb4c 78 74 5f 76 74 61 62 29 29 3b 0a 20 20 69 66 28  xt_vtab));.  if(
2eb4d 20 76 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 53   v==0 ) return S
2eb4e 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 43  QLITE_NOMEM;.  C
2eb4f 4c 45 41 52 28 76 29 3b 0a 20 20 2f 2a 20 73 71  LEAR(v);.  /* sq
2eb50 6c 69 74 65 20 77 69 6c 6c 20 69 6e 69 74 69 61  lite will initia
2eb51 6c 69 7a 65 20 76 2d 3e 62 61 73 65 20 2a 2f 0a  lize v->base */.
2eb52 20 20 76 2d 3e 64 62 20 3d 20 64 62 3b 0a 20 20    v->db = db;.  
2eb53 76 2d 3e 7a 44 62 20 3d 20 73 70 65 63 2d 3e 7a  v->zDb = spec->z
2eb54 44 62 3b 20 20 20 20 20 20 20 2f 2a 20 46 72 65  Db;       /* Fre
2eb55 65 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e  ed when azColumn
2eb56 20 69 73 20 66 72 65 65 64 20 2a 2f 0a 20 20 76   is freed */.  v
2eb57 2d 3e 7a 4e 61 6d 65 20 3d 20 73 70 65 63 2d 3e  ->zName = spec->
2eb58 7a 4e 61 6d 65 3b 20 20 20 2f 2a 20 46 72 65 65  zName;   /* Free
2eb59 64 20 77 68 65 6e 20 61 7a 43 6f 6c 75 6d 6e 20  d when azColumn 
2eb5a 69 73 20 66 72 65 65 64 20 2a 2f 0a 20 20 76 2d  is freed */.  v-
2eb5b 3e 6e 43 6f 6c 75 6d 6e 20 3d 20 73 70 65 63 2d  >nColumn = spec-
2eb5c 3e 6e 43 6f 6c 75 6d 6e 3b 0a 20 20 76 2d 3e 61  >nColumn;.  v->a
2eb5d 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20 3d  zContentColumn =
2eb5e 20 73 70 65 63 2d 3e 61 7a 43 6f 6e 74 65 6e 74   spec->azContent
2eb5f 43 6f 6c 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e  Column;.  spec->
2eb60 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 20  azContentColumn 
2eb61 3d 20 30 3b 0a 20 20 76 2d 3e 61 7a 43 6f 6c 75  = 0;.  v->azColu
2eb62 6d 6e 20 3d 20 73 70 65 63 2d 3e 61 7a 43 6f 6c  mn = spec->azCol
2eb63 75 6d 6e 3b 0a 20 20 73 70 65 63 2d 3e 61 7a 43  umn;.  spec->azC
2eb64 6f 6c 75 6d 6e 20 3d 20 30 3b 0a 0a 20 20 69 66  olumn = 0;..  if
2eb65 28 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69  ( spec->azTokeni
2eb66 7a 65 72 3d 3d 30 20 29 7b 0a 20 20 20 20 72 65  zer==0 ){.    re
2eb67 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
2eb68 4d 3b 0a 20 20 7d 0a 0a 20 20 7a 54 6f 6b 20 3d  M;.  }..  zTok =
2eb69 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69 7a   spec->azTokeniz
2eb6a 65 72 5b 30 5d 3b 20 0a 20 20 69 66 28 20 21 7a  er[0]; .  if( !z
2eb6b 54 6f 6b 20 29 7b 0a 20 20 20 20 7a 54 6f 6b 20  Tok ){.    zTok 
2eb6c 3d 20 22 73 69 6d 70 6c 65 22 3b 0a 20 20 7d 0a  = "simple";.  }.
2eb6d 20 20 6e 54 6f 6b 20 3d 20 73 74 72 6c 65 6e 28    nTok = strlen(
2eb6e 7a 54 6f 6b 29 2b 31 3b 0a 0a 20 20 6d 20 3d 20  zTok)+1;..  m = 
2eb6f 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a  (sqlite3_tokeniz
2eb70 65 72 5f 6d 6f 64 75 6c 65 20 2a 29 73 71 6c 69  er_module *)sqli
2eb71 74 65 33 46 74 73 33 48 61 73 68 46 69 6e 64 28  te3Fts3HashFind(
2eb72 70 48 61 73 68 2c 20 7a 54 6f 6b 2c 20 6e 54 6f  pHash, zTok, nTo
2eb73 6b 29 3b 0a 20 20 69 66 28 20 21 6d 20 29 7b 0a  k);.  if( !m ){.
2eb74 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71 6c      *pzErr = sql
2eb75 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 75 6e  ite3_mprintf("un
2eb76 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a  known tokenizer:
2eb77 20 25 73 22 2c 20 73 70 65 63 2d 3e 61 7a 54 6f   %s", spec->azTo
2eb78 6b 65 6e 69 7a 65 72 5b 30 5d 29 3b 0a 20 20 20  kenizer[0]);.   
2eb79 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 45 52 52   rc = SQLITE_ERR
2eb7a 4f 52 3b 0a 20 20 20 20 67 6f 74 6f 20 65 72 72  OR;.    goto err
2eb7b 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 6e 3d 30  ;.  }..  for(n=0
2eb7c 3b 20 73 70 65 63 2d 3e 61 7a 54 6f 6b 65 6e 69  ; spec->azTokeni
2eb7d 7a 65 72 5b 6e 5d 3b 20 6e 2b 2b 29 7b 7d 0a 20  zer[n]; n++){}. 
2eb7e 20 69 66 28 20 6e 20 29 7b 0a 20 20 20 20 72 63   if( n ){.    rc
2eb7f 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28 6e 2d   = m->xCreate(n-
2eb80 31 2c 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 63  1, (const char*c
2eb81 6f 6e 73 74 2a 29 26 73 70 65 63 2d 3e 61 7a 54  onst*)&spec->azT
2eb82 6f 6b 65 6e 69 7a 65 72 5b 31 5d 2c 0a 20 20 20  okenizer[1],.   
2eb83 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb84 20 26 76 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 29   &v->pTokenizer)
2eb85 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 72  ;.  }else{.    r
2eb86 63 20 3d 20 6d 2d 3e 78 43 72 65 61 74 65 28 30  c = m->xCreate(0
2eb87 2c 20 30 2c 20 26 76 2d 3e 70 54 6f 6b 65 6e 69  , 0, &v->pTokeni
2eb88 7a 65 72 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20  zer);.  }.  if( 
2eb89 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2eb8a 67 6f 74 6f 20 65 72 72 3b 0a 20 20 76 2d 3e 70  goto err;.  v->p
2eb8b 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64 75  Tokenizer->pModu
2eb8c 6c 65 20 3d 20 6d 3b 0a 0a 20 20 2f 2a 20 54 4f  le = m;..  /* TO
2eb8d 44 4f 3a 20 76 65 72 69 66 79 20 74 68 65 20 65  DO: verify the e
2eb8e 78 69 73 74 65 6e 63 65 20 6f 66 20 62 61 63 6b  xistence of back
2eb8f 69 6e 67 20 74 61 62 6c 65 73 20 66 6f 6f 5f 63  ing tables foo_c
2eb90 6f 6e 74 65 6e 74 2c 20 66 6f 6f 5f 74 65 72 6d  ontent, foo_term
2eb91 20 2a 2f 0a 0a 20 20 73 63 68 65 6d 61 20 3d 20   */..  schema = 
2eb92 66 75 6c 6c 74 65 78 74 53 63 68 65 6d 61 28 76  fulltextSchema(v
2eb93 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 28 63 6f 6e 73  ->nColumn, (cons
2eb94 74 20 63 68 61 72 2a 63 6f 6e 73 74 2a 29 76 2d  t char*const*)v-
2eb95 3e 61 7a 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20 20  >azColumn,.     
2eb96 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2eb97 20 20 20 20 20 73 70 65 63 2d 3e 7a 4e 61 6d 65       spec->zName
2eb98 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  );.  rc = sqlite
2eb99 33 5f 64 65 63 6c 61 72 65 5f 76 74 61 62 28 64  3_declare_vtab(d
2eb9a 62 2c 20 73 63 68 65 6d 61 29 3b 0a 20 20 73 71  b, schema);.  sq
2eb9b 6c 69 74 65 33 5f 66 72 65 65 28 73 63 68 65 6d  lite3_free(schem
2eb9c 61 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  a);.  if( rc!=SQ
2eb9d 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65  LITE_OK ) goto e
2eb9e 72 72 3b 0a 0a 20 20 6d 65 6d 73 65 74 28 76 2d  rr;..  memset(v-
2eb9f 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74 65 6d  >pFulltextStatem
2eba0 65 6e 74 73 2c 20 30 2c 20 73 69 7a 65 6f 66 28  ents, 0, sizeof(
2eba1 76 2d 3e 70 46 75 6c 6c 74 65 78 74 53 74 61 74  v->pFulltextStat
2eba2 65 6d 65 6e 74 73 29 29 3b 0a 0a 20 20 2f 2a 20  ements));..  /* 
2eba3 49 6e 64 69 63 61 74 65 20 74 68 61 74 20 74 68  Indicate that th
2eba4 65 20 62 75 66 66 65 72 20 69 73 20 6e 6f 74 20  e buffer is not 
2eba5 6c 69 76 65 2e 20 2a 2f 0a 20 20 76 2d 3e 6e 50  live. */.  v->nP
2eba6 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d 31 3b  endingData = -1;
2eba7 0a 0a 20 20 2a 70 70 56 54 61 62 20 3d 20 26 76  ..  *ppVTab = &v
2eba8 2d 3e 62 61 73 65 3b 0a 20 20 46 54 53 54 52 41  ->base;.  FTSTRA
2eba9 43 45 28 28 22 46 54 53 33 20 43 6f 6e 6e 65 63  CE(("FTS3 Connec
2ebaa 74 20 25 70 5c 6e 22 2c 20 76 29 29 3b 0a 0a 20  t %p\n", v));.. 
2ebab 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 65 72 72   return rc;..err
2ebac 3a 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  :.  fulltext_vta
2ebad 62 5f 64 65 73 74 72 6f 79 28 76 29 3b 0a 20 20  b_destroy(v);.  
2ebae 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
2ebaf 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
2ebb0 74 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69  tConnect(.  sqli
2ebb1 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
2ebb2 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67  *pAux,.  int arg
2ebb3 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
2ebb4 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c  onst*argv,.  sql
2ebb5 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 54  ite3_vtab **ppVT
2ebb6 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  ab,.  char **pzE
2ebb7 72 72 0a 29 7b 0a 20 20 54 61 62 6c 65 53 70 65  rr.){.  TableSpe
2ebb8 63 20 73 70 65 63 3b 0a 20 20 69 6e 74 20 72 63  c spec;.  int rc
2ebb9 20 3d 20 70 61 72 73 65 53 70 65 63 28 26 73 70   = parseSpec(&sp
2ebba 65 63 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20  ec, argc, argv, 
2ebbb 70 7a 45 72 72 29 3b 0a 20 20 69 66 28 20 72 63  pzErr);.  if( rc
2ebbc 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2ebbd 74 75 72 6e 20 72 63 3b 0a 0a 20 20 72 63 20 3d  turn rc;..  rc =
2ebbe 20 63 6f 6e 73 74 72 75 63 74 56 74 61 62 28 64   constructVtab(d
2ebbf 62 2c 20 28 66 74 73 33 48 61 73 68 20 2a 29 70  b, (fts3Hash *)p
2ebc0 41 75 78 2c 20 26 73 70 65 63 2c 20 70 70 56 54  Aux, &spec, ppVT
2ebc1 61 62 2c 20 70 7a 45 72 72 29 3b 0a 20 20 63 6c  ab, pzErr);.  cl
2ebc2 65 61 72 54 61 62 6c 65 53 70 65 63 28 26 73 70  earTableSpec(&sp
2ebc3 65 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ec);.  return rc
2ebc4 3b 0a 7d 0a 0a 2f 2a 20 54 68 65 20 25 5f 63 6f  ;.}../* The %_co
2ebc5 6e 74 65 6e 74 20 74 61 62 6c 65 20 68 6f 6c 64  ntent table hold
2ebc6 73 20 74 68 65 20 74 65 78 74 20 6f 66 20 65 61  s the text of ea
2ebc7 63 68 20 64 6f 63 75 6d 65 6e 74 2c 20 77 69 74  ch document, wit
2ebc8 68 0a 2a 2a 20 74 68 65 20 64 6f 63 69 64 20 63  h.** the docid c
2ebc9 6f 6c 75 6d 6e 20 65 78 70 6f 73 65 64 20 61 73  olumn exposed as
2ebca 20 74 68 65 20 53 51 4c 69 74 65 20 72 6f 77 69   the SQLite rowi
2ebcb 64 20 66 6f 72 20 74 68 65 20 74 61 62 6c 65 2e  d for the table.
2ebcc 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
2ebcd 73 29 20 54 68 69 73 20 63 6f 6d 6d 65 6e 74 20  s) This comment 
2ebce 6e 65 65 64 73 20 65 6c 61 62 6f 72 61 74 69 6f  needs elaboratio
2ebcf 6e 20 74 6f 20 6d 61 74 63 68 20 74 68 65 20 75  n to match the u
2ebd0 70 64 61 74 65 64 0a 2a 2a 20 63 6f 64 65 2e 20  pdated.** code. 
2ebd1 20 57 6f 72 6b 20 69 74 20 69 6e 74 6f 20 74 68   Work it into th
2ebd2 65 20 74 6f 70 2d 6f 66 2d 66 69 6c 65 20 63 6f  e top-of-file co
2ebd3 6d 6d 65 6e 74 20 61 74 20 74 68 61 74 20 74 69  mment at that ti
2ebd4 6d 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  me..*/.static in
2ebd5 74 20 66 75 6c 6c 74 65 78 74 43 72 65 61 74 65  t fulltextCreate
2ebd6 28 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 76 6f  (sqlite3 *db, vo
2ebd7 69 64 20 2a 70 41 75 78 2c 0a 20 20 20 20 20 20  id *pAux,.      
2ebd8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebd9 20 20 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f      int argc, co
2ebda 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
2ebdb 20 2a 61 72 67 76 2c 0a 20 20 20 20 20 20 20 20   *argv,.        
2ebdc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ebdd 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a    sqlite3_vtab *
2ebde 2a 70 70 56 54 61 62 2c 20 63 68 61 72 20 2a 2a  *ppVTab, char **
2ebdf 70 7a 45 72 72 29 7b 0a 20 20 69 6e 74 20 72 63  pzErr){.  int rc
2ebe0 3b 0a 20 20 54 61 62 6c 65 53 70 65 63 20 73 70  ;.  TableSpec sp
2ebe1 65 63 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66  ec;.  StringBuff
2ebe2 65 72 20 73 63 68 65 6d 61 3b 0a 20 20 46 54 53  er schema;.  FTS
2ebe3 54 52 41 43 45 28 28 22 46 54 53 33 20 43 72 65  TRACE(("FTS3 Cre
2ebe4 61 74 65 5c 6e 22 29 29 3b 0a 0a 20 20 72 63 20  ate\n"));..  rc 
2ebe5 3d 20 70 61 72 73 65 53 70 65 63 28 26 73 70 65  = parseSpec(&spe
2ebe6 63 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70  c, argc, argv, p
2ebe7 7a 45 72 72 29 3b 0a 20 20 69 66 28 20 72 63 21  zErr);.  if( rc!
2ebe8 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2ebe9 75 72 6e 20 72 63 3b 0a 0a 20 20 69 6e 69 74 53  urn rc;..  initS
2ebea 74 72 69 6e 67 42 75 66 66 65 72 28 26 73 63 68  tringBuffer(&sch
2ebeb 65 6d 61 29 3b 0a 20 20 61 70 70 65 6e 64 28 26  ema);.  append(&
2ebec 73 63 68 65 6d 61 2c 20 22 43 52 45 41 54 45 20  schema, "CREATE 
2ebed 54 41 42 4c 45 20 25 5f 63 6f 6e 74 65 6e 74 28  TABLE %_content(
2ebee 22 29 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63  ");.  append(&sc
2ebef 68 65 6d 61 2c 20 22 20 20 64 6f 63 69 64 20 49  hema, "  docid I
2ebf0 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59 20 4b  NTEGER PRIMARY K
2ebf1 45 59 2c 22 29 3b 0a 20 20 61 70 70 65 6e 64 4c  EY,");.  appendL
2ebf2 69 73 74 28 26 73 63 68 65 6d 61 2c 20 73 70 65  ist(&schema, spe
2ebf3 63 2e 6e 43 6f 6c 75 6d 6e 2c 20 73 70 65 63 2e  c.nColumn, spec.
2ebf4 61 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29  azContentColumn)
2ebf5 3b 0a 20 20 61 70 70 65 6e 64 28 26 73 63 68 65  ;.  append(&sche
2ebf6 6d 61 2c 20 22 29 22 29 3b 0a 20 20 72 63 20 3d  ma, ")");.  rc =
2ebf7 20 73 71 6c 5f 65 78 65 63 28 64 62 2c 20 73 70   sql_exec(db, sp
2ebf8 65 63 2e 7a 44 62 2c 20 73 70 65 63 2e 7a 4e 61  ec.zDb, spec.zNa
2ebf9 6d 65 2c 20 73 74 72 69 6e 67 42 75 66 66 65 72  me, stringBuffer
2ebfa 44 61 74 61 28 26 73 63 68 65 6d 61 29 29 3b 0a  Data(&schema));.
2ebfb 20 20 73 74 72 69 6e 67 42 75 66 66 65 72 44 65    stringBufferDe
2ebfc 73 74 72 6f 79 28 26 73 63 68 65 6d 61 29 3b 0a  stroy(&schema);.
2ebfd 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2ebfe 5f 4f 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a  _OK ) goto out;.
2ebff 0a 20 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63  .  rc = sql_exec
2ec00 28 64 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73  (db, spec.zDb, s
2ec01 70 65 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20  pec.zName,.     
2ec02 20 20 20 20 20 20 20 20 20 20 20 22 63 72 65 61             "crea
2ec03 74 65 20 74 61 62 6c 65 20 25 5f 73 65 67 6d 65  te table %_segme
2ec04 6e 74 73 28 22 0a 20 20 20 20 20 20 20 20 20 20  nts(".          
2ec05 20 20 20 20 20 20 22 20 20 62 6c 6f 63 6b 69 64        "  blockid
2ec06 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
2ec07 20 4b 45 59 2c 22 0a 20 20 20 20 20 20 20 20 20   KEY,".         
2ec08 20 20 20 20 20 20 20 22 20 20 62 6c 6f 63 6b 20         "  block 
2ec09 62 6c 6f 62 22 0a 20 20 20 20 20 20 20 20 20 20  blob".          
2ec0a 20 20 20 20 20 20 22 29 3b 22 0a 20 20 20 20 20        ");".     
2ec0b 20 20 20 20 20 20 20 20 20 20 20 29 3b 0a 20 20             );.  
2ec0c 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2ec0d 4b 20 29 20 67 6f 74 6f 20 6f 75 74 3b 0a 0a 20  K ) goto out;.. 
2ec0e 20 72 63 20 3d 20 73 71 6c 5f 65 78 65 63 28 64   rc = sql_exec(d
2ec0f 62 2c 20 73 70 65 63 2e 7a 44 62 2c 20 73 70 65  b, spec.zDb, spe
2ec10 63 2e 7a 4e 61 6d 65 2c 0a 20 20 20 20 20 20 20  c.zName,.       
2ec11 20 20 20 20 20 20 20 20 20 22 63 72 65 61 74 65           "create
2ec12 20 74 61 62 6c 65 20 25 5f 73 65 67 64 69 72 28   table %_segdir(
2ec13 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ".              
2ec14 20 20 22 20 20 6c 65 76 65 6c 20 69 6e 74 65 67    "  level integ
2ec15 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20  er,".           
2ec16 20 20 20 20 20 22 20 20 69 64 78 20 69 6e 74 65       "  idx inte
2ec17 67 65 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20  ger,".          
2ec18 20 20 20 20 20 20 22 20 20 73 74 61 72 74 5f 62        "  start_b
2ec19 6c 6f 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20  lock integer,". 
2ec1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
2ec1b 20 20 6c 65 61 76 65 73 5f 65 6e 64 5f 62 6c 6f    leaves_end_blo
2ec1c 63 6b 20 69 6e 74 65 67 65 72 2c 22 0a 20 20 20  ck integer,".   
2ec1d 20 20 20 20 20 20 20 20 20 20 20 20 20 22 20 20               "  
2ec1e 65 6e 64 5f 62 6c 6f 63 6b 20 69 6e 74 65 67 65  end_block intege
2ec1f 72 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r,".            
2ec20 20 20 20 20 22 20 20 72 6f 6f 74 20 62 6c 6f 62      "  root blob
2ec21 2c 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ,".             
2ec22 20 20 20 22 20 20 70 72 69 6d 61 72 79 20 6b 65     "  primary ke
2ec23 79 28 6c 65 76 65 6c 2c 20 69 64 78 29 22 0a 20  y(level, idx)". 
2ec24 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22                 "
2ec25 29 3b 22 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  );");.  if( rc!=
2ec26 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
2ec27 20 6f 75 74 3b 0a 0a 20 20 72 63 20 3d 20 63 6f   out;..  rc = co
2ec28 6e 73 74 72 75 63 74 56 74 61 62 28 64 62 2c 20  nstructVtab(db, 
2ec29 28 66 74 73 33 48 61 73 68 20 2a 29 70 41 75 78  (fts3Hash *)pAux
2ec2a 2c 20 26 73 70 65 63 2c 20 70 70 56 54 61 62 2c  , &spec, ppVTab,
2ec2b 20 70 7a 45 72 72 29 3b 0a 0a 6f 75 74 3a 0a 20   pzErr);..out:. 
2ec2c 20 63 6c 65 61 72 54 61 62 6c 65 53 70 65 63 28   clearTableSpec(
2ec2d 26 73 70 65 63 29 3b 0a 20 20 72 65 74 75 72 6e  &spec);.  return
2ec2e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 44 65 63 69 64   rc;.}../* Decid
2ec2f 65 20 68 6f 77 20 74 6f 20 68 61 6e 64 6c 65 20  e how to handle 
2ec30 61 6e 20 53 51 4c 20 71 75 65 72 79 2e 20 2a 2f  an SQL query. */
2ec31 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
2ec32 74 65 78 74 42 65 73 74 49 6e 64 65 78 28 73 71  textBestIndex(sq
2ec33 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61  lite3_vtab *pVTa
2ec34 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64 65 78  b, sqlite3_index
2ec35 5f 69 6e 66 6f 20 2a 70 49 6e 66 6f 29 7b 0a 20  _info *pInfo){. 
2ec36 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a   fulltext_vtab *
2ec37 76 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 76 74  v = (fulltext_vt
2ec38 61 62 20 2a 29 70 56 54 61 62 3b 0a 20 20 69 6e  ab *)pVTab;.  in
2ec39 74 20 69 3b 0a 20 20 46 54 53 54 52 41 43 45 28  t i;.  FTSTRACE(
2ec3a 28 22 46 54 53 33 20 42 65 73 74 49 6e 64 65 78  ("FTS3 BestIndex
2ec3b 5c 6e 22 29 29 3b 0a 0a 20 20 66 6f 72 28 69 3d  \n"));..  for(i=
2ec3c 30 3b 20 69 3c 70 49 6e 66 6f 2d 3e 6e 43 6f 6e  0; i<pInfo->nCon
2ec3d 73 74 72 61 69 6e 74 3b 20 2b 2b 69 29 7b 0a 20  straint; ++i){. 
2ec3e 20 20 20 63 6f 6e 73 74 20 73 74 72 75 63 74 20     const struct 
2ec3f 73 71 6c 69 74 65 33 5f 69 6e 64 65 78 5f 63 6f  sqlite3_index_co
2ec40 6e 73 74 72 61 69 6e 74 20 2a 70 43 6f 6e 73 74  nstraint *pConst
2ec41 72 61 69 6e 74 3b 0a 20 20 20 20 70 43 6f 6e 73  raint;.    pCons
2ec42 74 72 61 69 6e 74 20 3d 20 26 70 49 6e 66 6f 2d  traint = &pInfo-
2ec43 3e 61 43 6f 6e 73 74 72 61 69 6e 74 5b 69 5d 3b  >aConstraint[i];
2ec44 0a 20 20 20 20 69 66 28 20 70 43 6f 6e 73 74 72  .    if( pConstr
2ec45 61 69 6e 74 2d 3e 75 73 61 62 6c 65 20 29 20 7b  aint->usable ) {
2ec46 0a 20 20 20 20 20 20 69 66 28 20 28 70 43 6f 6e  .      if( (pCon
2ec47 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e  straint->iColumn
2ec48 3d 3d 2d 31 20 7c 7c 20 70 43 6f 6e 73 74 72 61  ==-1 || pConstra
2ec49 69 6e 74 2d 3e 69 43 6f 6c 75 6d 6e 3d 3d 76 2d  int->iColumn==v-
2ec4a 3e 6e 43 6f 6c 75 6d 6e 2b 31 29 20 26 26 0a 20  >nColumn+1) &&. 
2ec4b 20 20 20 20 20 20 20 20 20 70 43 6f 6e 73 74 72           pConstr
2ec4c 61 69 6e 74 2d 3e 6f 70 3d 3d 53 51 4c 49 54 45  aint->op==SQLITE
2ec4d 5f 49 4e 44 45 58 5f 43 4f 4e 53 54 52 41 49 4e  _INDEX_CONSTRAIN
2ec4e 54 5f 45 51 20 29 7b 0a 20 20 20 20 20 20 20 20  T_EQ ){.        
2ec4f 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20  pInfo->idxNum = 
2ec50 51 55 45 52 59 5f 44 4f 43 49 44 3b 20 20 20 20  QUERY_DOCID;    
2ec51 20 20 2f 2a 20 6c 6f 6f 6b 75 70 20 62 79 20 64    /* lookup by d
2ec52 6f 63 69 64 20 2a 2f 0a 20 20 20 20 20 20 20 20  ocid */.        
2ec53 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
2ec54 51 55 45 52 59 5f 44 4f 43 49 44 5c 6e 22 29 29  QUERY_DOCID\n"))
2ec55 3b 0a 20 20 20 20 20 20 7d 20 65 6c 73 65 20 69  ;.      } else i
2ec56 66 28 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e  f( pConstraint->
2ec57 69 43 6f 6c 75 6d 6e 3e 3d 30 20 26 26 20 70 43  iColumn>=0 && pC
2ec58 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75  onstraint->iColu
2ec59 6d 6e 3c 3d 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 26  mn<=v->nColumn &
2ec5a 26 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  &.              
2ec5b 20 20 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e     pConstraint->
2ec5c 6f 70 3d 3d 53 51 4c 49 54 45 5f 49 4e 44 45 58  op==SQLITE_INDEX
2ec5d 5f 43 4f 4e 53 54 52 41 49 4e 54 5f 4d 41 54 43  _CONSTRAINT_MATC
2ec5e 48 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  H ){.        /* 
2ec5f 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72 63 68  full-text search
2ec60 20 2a 2f 0a 20 20 20 20 20 20 20 20 70 49 6e 66   */.        pInf
2ec61 6f 2d 3e 69 64 78 4e 75 6d 20 3d 20 51 55 45 52  o->idxNum = QUER
2ec62 59 5f 46 55 4c 4c 54 45 58 54 20 2b 20 70 43 6f  Y_FULLTEXT + pCo
2ec63 6e 73 74 72 61 69 6e 74 2d 3e 69 43 6f 6c 75 6d  nstraint->iColum
2ec64 6e 3b 0a 20 20 20 20 20 20 20 20 46 54 53 54 52  n;.        FTSTR
2ec65 41 43 45 28 28 22 46 54 53 33 20 51 55 45 52 59  ACE(("FTS3 QUERY
2ec66 5f 46 55 4c 4c 54 45 58 54 20 25 64 5c 6e 22 2c  _FULLTEXT %d\n",
2ec67 20 70 43 6f 6e 73 74 72 61 69 6e 74 2d 3e 69 43   pConstraint->iC
2ec68 6f 6c 75 6d 6e 29 29 3b 0a 20 20 20 20 20 20 7d  olumn));.      }
2ec69 20 65 6c 73 65 20 63 6f 6e 74 69 6e 75 65 3b 0a   else continue;.
2ec6a 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43  .      pInfo->aC
2ec6b 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69  onstraintUsage[i
2ec6c 5d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b  ].argvIndex = 1;
2ec6d 0a 20 20 20 20 20 20 70 49 6e 66 6f 2d 3e 61 43  .      pInfo->aC
2ec6e 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69  onstraintUsage[i
2ec6f 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a 20 20 20  ].omit = 1;..   
2ec70 20 20 20 2f 2a 20 41 6e 20 61 72 62 69 74 72 61     /* An arbitra
2ec71 72 79 20 76 61 6c 75 65 20 66 6f 72 20 6e 6f 77  ry value for now
2ec72 2e 0a 20 20 20 20 20 20 20 2a 20 54 4f 44 4f 3a  ..       * TODO:
2ec73 20 50 65 72 68 61 70 73 20 64 6f 63 69 64 20 6d   Perhaps docid m
2ec74 61 74 63 68 65 73 20 73 68 6f 75 6c 64 20 62 65  atches should be
2ec75 20 63 6f 6e 73 69 64 65 72 65 64 20 63 68 65 61   considered chea
2ec76 70 65 72 20 74 68 61 6e 0a 20 20 20 20 20 20 20  per than.       
2ec77 2a 20 66 75 6c 6c 2d 74 65 78 74 20 73 65 61 72  * full-text sear
2ec78 63 68 65 73 2e 20 2a 2f 0a 20 20 20 20 20 20 70  ches. */.      p
2ec79 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
2ec7a 6f 73 74 20 3d 20 31 2e 30 3b 20 20 20 0a 0a 20  ost = 1.0;   .. 
2ec7b 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49       return SQLI
2ec7c 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d  TE_OK;.    }.  }
2ec7d 0a 20 20 70 49 6e 66 6f 2d 3e 69 64 78 4e 75 6d  .  pInfo->idxNum
2ec7e 20 3d 20 51 55 45 52 59 5f 47 45 4e 45 52 49 43   = QUERY_GENERIC
2ec7f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2ec80 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  E_OK;.}..static 
2ec81 69 6e 74 20 66 75 6c 6c 74 65 78 74 44 69 73 63  int fulltextDisc
2ec82 6f 6e 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76  onnect(sqlite3_v
2ec83 74 61 62 20 2a 70 56 54 61 62 29 7b 0a 20 20 46  tab *pVTab){.  F
2ec84 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20 44  TSTRACE(("FTS3 D
2ec85 69 73 63 6f 6e 6e 65 63 74 20 25 70 5c 6e 22 2c  isconnect %p\n",
2ec86 20 70 56 54 61 62 29 29 3b 0a 20 20 66 75 6c 6c   pVTab));.  full
2ec87 74 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f  text_vtab_destro
2ec88 79 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  y((fulltext_vtab
2ec89 20 2a 29 70 56 54 61 62 29 3b 0a 20 20 72 65 74   *)pVTab);.  ret
2ec8a 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2ec8b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  ..static int ful
2ec8c 6c 74 65 78 74 44 65 73 74 72 6f 79 28 73 71 6c  ltextDestroy(sql
2ec8d 69 74 65 33 5f 76 74 61 62 20 2a 70 56 54 61 62  ite3_vtab *pVTab
2ec8e 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74  ){.  fulltext_vt
2ec8f 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74 65 78  ab *v = (fulltex
2ec90 74 5f 76 74 61 62 20 2a 29 70 56 54 61 62 3b 0a  t_vtab *)pVTab;.
2ec91 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53    int rc;..  FTS
2ec92 54 52 41 43 45 28 28 22 46 54 53 33 20 44 65 73  TRACE(("FTS3 Des
2ec93 74 72 6f 79 20 25 70 5c 6e 22 2c 20 70 56 54 61  troy %p\n", pVTa
2ec94 62 29 29 3b 0a 20 20 72 63 20 3d 20 73 71 6c 5f  b));.  rc = sql_
2ec95 65 78 65 63 28 76 2d 3e 64 62 2c 20 76 2d 3e 7a  exec(v->db, v->z
2ec96 44 62 2c 20 76 2d 3e 7a 4e 61 6d 65 2c 0a 20 20  Db, v->zName,.  
2ec97 20 20 20 20 20 20 20 20 20 20 20 20 20 20 22 64                "d
2ec98 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65 78 69  rop table if exi
2ec99 73 74 73 20 25 5f 63 6f 6e 74 65 6e 74 3b 22 0a  sts %_content;".
2ec9a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ec9b 22 64 72 6f 70 20 74 61 62 6c 65 20 69 66 20 65  "drop table if e
2ec9c 78 69 73 74 73 20 25 5f 73 65 67 6d 65 6e 74 73  xists %_segments
2ec9d 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ;".             
2ec9e 20 20 20 22 64 72 6f 70 20 74 61 62 6c 65 20 69     "drop table i
2ec9f 66 20 65 78 69 73 74 73 20 25 5f 73 65 67 64 69  f exists %_segdi
2eca0 72 3b 22 0a 20 20 20 20 20 20 20 20 20 20 20 20  r;".            
2eca1 20 20 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21      );.  if( rc!
2eca2 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74  =SQLITE_OK ) ret
2eca3 75 72 6e 20 72 63 3b 0a 0a 20 20 66 75 6c 6c 74  urn rc;..  fullt
2eca4 65 78 74 5f 76 74 61 62 5f 64 65 73 74 72 6f 79  ext_vtab_destroy
2eca5 28 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  ((fulltext_vtab 
2eca6 2a 29 70 56 54 61 62 29 3b 0a 20 20 72 65 74 75  *)pVTab);.  retu
2eca7 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2eca8 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
2eca9 74 65 78 74 4f 70 65 6e 28 73 71 6c 69 74 65 33  textOpen(sqlite3
2ecaa 5f 76 74 61 62 20 2a 70 56 54 61 62 2c 20 73 71  _vtab *pVTab, sq
2ecab 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f  lite3_vtab_curso
2ecac 72 20 2a 2a 70 70 43 75 72 73 6f 72 29 7b 0a 20  r **ppCursor){. 
2ecad 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
2ecae 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 66 75 6c   *c;..  c = (ful
2ecaf 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
2ecb0 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
2ecb1 69 7a 65 6f 66 28 66 75 6c 6c 74 65 78 74 5f 63  izeof(fulltext_c
2ecb2 75 72 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 63  ursor));.  if( c
2ecb3 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28 63   ){.    memset(c
2ecb4 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 75 6c 6c  , 0, sizeof(full
2ecb5 74 65 78 74 5f 63 75 72 73 6f 72 29 29 3b 0a 20  text_cursor));. 
2ecb6 20 20 20 2f 2a 20 73 71 6c 69 74 65 20 77 69 6c     /* sqlite wil
2ecb7 6c 20 69 6e 69 74 69 61 6c 69 7a 65 20 63 2d 3e  l initialize c->
2ecb8 62 61 73 65 20 2a 2f 0a 20 20 20 20 2a 70 70 43  base */.    *ppC
2ecb9 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61 73 65  ursor = &c->base
2ecba 3b 0a 20 20 20 20 46 54 53 54 52 41 43 45 28 28  ;.    FTSTRACE((
2ecbb 22 46 54 53 33 20 4f 70 65 6e 20 25 70 3a 20 25  "FTS3 Open %p: %
2ecbc 70 5c 6e 22 2c 20 70 56 54 61 62 2c 20 63 29 29  p\n", pVTab, c))
2ecbd 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c  ;.    return SQL
2ecbe 49 54 45 5f 4f 4b 3b 0a 20 20 7d 65 6c 73 65 7b  ITE_OK;.  }else{
2ecbf 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2ecc0 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 7d 0a  TE_NOMEM;.  }.}.
2ecc1 0a 0a 2f 2a 20 46 72 65 65 20 61 6c 6c 20 6f 66  ../* Free all of
2ecc2 20 74 68 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79   the dynamically
2ecc3 20 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72   allocated memor
2ecc4 79 20 68 65 6c 64 20 62 79 20 2a 71 0a 2a 2f 0a  y held by *q.*/.
2ecc5 73 74 61 74 69 63 20 76 6f 69 64 20 71 75 65 72  static void quer
2ecc6 79 43 6c 65 61 72 28 51 75 65 72 79 20 2a 71 29  yClear(Query *q)
2ecc7 7b 0a 20 20 69 6e 74 20 69 3b 0a 20 20 66 6f 72  {.  int i;.  for
2ecc8 28 69 20 3d 20 30 3b 20 69 20 3c 20 71 2d 3e 6e  (i = 0; i < q->n
2ecc9 54 65 72 6d 73 3b 20 2b 2b 69 29 7b 0a 20 20 20  Terms; ++i){.   
2ecca 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 71 2d   sqlite3_free(q-
2eccb 3e 70 54 65 72 6d 73 5b 69 5d 2e 70 54 65 72 6d  >pTerms[i].pTerm
2eccc 29 3b 0a 20 20 7d 0a 20 20 73 71 6c 69 74 65 33  );.  }.  sqlite3
2eccd 5f 66 72 65 65 28 71 2d 3e 70 54 65 72 6d 73 29  _free(q->pTerms)
2ecce 3b 0a 20 20 43 4c 45 41 52 28 71 29 3b 0a 7d 0a  ;.  CLEAR(q);.}.
2eccf 0a 2f 2a 20 46 72 65 65 20 61 6c 6c 20 6f 66 20  ./* Free all of 
2ecd0 74 68 65 20 64 79 6e 61 6d 69 63 61 6c 6c 79 20  the dynamically 
2ecd1 61 6c 6c 6f 63 61 74 65 64 20 6d 65 6d 6f 72 79  allocated memory
2ecd2 20 68 65 6c 64 20 62 79 20 74 68 65 0a 2a 2a 20   held by the.** 
2ecd3 53 6e 69 70 70 65 74 0a 2a 2f 0a 73 74 61 74 69  Snippet.*/.stati
2ecd4 63 20 76 6f 69 64 20 73 6e 69 70 70 65 74 43 6c  c void snippetCl
2ecd5 65 61 72 28 53 6e 69 70 70 65 74 20 2a 70 29 7b  ear(Snippet *p){
2ecd6 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
2ecd7 70 2d 3e 61 4d 61 74 63 68 29 3b 0a 20 20 73 71  p->aMatch);.  sq
2ecd8 6c 69 74 65 33 5f 66 72 65 65 28 70 2d 3e 7a 4f  lite3_free(p->zO
2ecd9 66 66 73 65 74 29 3b 0a 20 20 73 71 6c 69 74 65  ffset);.  sqlite
2ecda 33 5f 66 72 65 65 28 70 2d 3e 7a 53 6e 69 70 70  3_free(p->zSnipp
2ecdb 65 74 29 3b 0a 20 20 43 4c 45 41 52 28 70 29 3b  et);.  CLEAR(p);
2ecdc 0a 7d 0a 2f 2a 0a 2a 2a 20 41 70 70 65 6e 64 20  .}./*.** Append 
2ecdd 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 74  a single entry t
2ecde 6f 20 74 68 65 20 70 2d 3e 61 4d 61 74 63 68 5b  o the p->aMatch[
2ecdf 5d 20 6c 6f 67 2e 0a 2a 2f 0a 73 74 61 74 69 63  ] log..*/.static
2ece0 20 76 6f 69 64 20 73 6e 69 70 70 65 74 41 70 70   void snippetApp
2ece1 65 6e 64 4d 61 74 63 68 28 0a 20 20 53 6e 69 70  endMatch(.  Snip
2ece2 70 65 74 20 2a 70 2c 20 20 20 20 20 20 20 20 20  pet *p,         
2ece3 20 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64 20        /* Append 
2ece4 74 68 65 20 65 6e 74 72 79 20 74 6f 20 74 68 69  the entry to thi
2ece5 73 20 73 6e 69 70 70 65 74 20 2a 2f 0a 20 20 69  s snippet */.  i
2ece6 6e 74 20 69 43 6f 6c 2c 20 69 6e 74 20 69 54 65  nt iCol, int iTe
2ece7 72 6d 2c 20 20 20 20 20 20 2f 2a 20 54 68 65 20  rm,      /* The 
2ece8 63 6f 6c 75 6d 6e 20 61 6e 64 20 71 75 65 72 79  column and query
2ece9 20 74 65 72 6d 20 2a 2f 0a 20 20 69 6e 74 20 69   term */.  int i
2ecea 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20 20 20  Token,          
2eceb 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 69 6e 67       /* Matching
2ecec 20 74 6f 6b 65 6e 20 69 6e 20 64 6f 63 75 6d 65   token in docume
2eced 6e 74 20 2a 2f 0a 20 20 69 6e 74 20 69 53 74 61  nt */.  int iSta
2ecee 72 74 2c 20 69 6e 74 20 6e 42 79 74 65 20 20 20  rt, int nByte   
2ecef 20 20 2f 2a 20 4f 66 66 73 65 74 20 61 6e 64 20    /* Offset and 
2ecf0 73 69 7a 65 20 6f 66 20 74 68 65 20 6d 61 74 63  size of the matc
2ecf1 68 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b  h */.){.  int i;
2ecf2 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65  .  struct snippe
2ecf3 74 4d 61 74 63 68 20 2a 70 4d 61 74 63 68 3b 0a  tMatch *pMatch;.
2ecf4 20 20 69 66 28 20 70 2d 3e 6e 4d 61 74 63 68 2b    if( p->nMatch+
2ecf5 31 3e 3d 70 2d 3e 6e 41 6c 6c 6f 63 20 29 7b 0a  1>=p->nAlloc ){.
2ecf6 20 20 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20      p->nAlloc = 
2ecf7 70 2d 3e 6e 41 6c 6c 6f 63 2a 32 20 2b 20 31 30  p->nAlloc*2 + 10
2ecf8 3b 0a 20 20 20 20 70 2d 3e 61 4d 61 74 63 68 20  ;.    p->aMatch 
2ecf9 3d 20 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f  = sqlite3_reallo
2ecfa 63 28 70 2d 3e 61 4d 61 74 63 68 2c 20 70 2d 3e  c(p->aMatch, p->
2ecfb 6e 41 6c 6c 6f 63 2a 73 69 7a 65 6f 66 28 70 2d  nAlloc*sizeof(p-
2ecfc 3e 61 4d 61 74 63 68 5b 30 5d 29 20 29 3b 0a 20  >aMatch[0]) );. 
2ecfd 20 20 20 69 66 28 20 70 2d 3e 61 4d 61 74 63 68     if( p->aMatch
2ecfe 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 70 2d 3e  ==0 ){.      p->
2ecff 6e 4d 61 74 63 68 20 3d 20 30 3b 0a 20 20 20 20  nMatch = 0;.    
2ed00 20 20 70 2d 3e 6e 41 6c 6c 6f 63 20 3d 20 30 3b    p->nAlloc = 0;
2ed01 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
2ed02 20 20 20 7d 0a 20 20 7d 0a 20 20 69 20 3d 20 70     }.  }.  i = p
2ed03 2d 3e 6e 4d 61 74 63 68 2b 2b 3b 0a 20 20 70 4d  ->nMatch++;.  pM
2ed04 61 74 63 68 20 3d 20 26 70 2d 3e 61 4d 61 74 63  atch = &p->aMatc
2ed05 68 5b 69 5d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  h[i];.  pMatch->
2ed06 69 43 6f 6c 20 3d 20 69 43 6f 6c 3b 0a 20 20 70  iCol = iCol;.  p
2ed07 4d 61 74 63 68 2d 3e 69 54 65 72 6d 20 3d 20 69  Match->iTerm = i
2ed08 54 65 72 6d 3b 0a 20 20 70 4d 61 74 63 68 2d 3e  Term;.  pMatch->
2ed09 69 54 6f 6b 65 6e 20 3d 20 69 54 6f 6b 65 6e 3b  iToken = iToken;
2ed0a 0a 20 20 70 4d 61 74 63 68 2d 3e 69 53 74 61 72  .  pMatch->iStar
2ed0b 74 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 70 4d  t = iStart;.  pM
2ed0c 61 74 63 68 2d 3e 6e 42 79 74 65 20 3d 20 6e 42  atch->nByte = nB
2ed0d 79 74 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 69  yte;.}../*.** Si
2ed0e 7a 69 6e 67 20 69 6e 66 6f 72 6d 61 74 69 6f 6e  zing information
2ed0f 20 66 6f 72 20 74 68 65 20 63 69 72 63 75 6c 61   for the circula
2ed10 72 20 62 75 66 66 65 72 20 75 73 65 64 20 69 6e  r buffer used in
2ed11 20 73 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f   snippetOffsetsO
2ed12 66 43 6f 6c 75 6d 6e 28 29 0a 2a 2f 0a 23 64 65  fColumn().*/.#de
2ed13 66 69 6e 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f  fine FTS3_ROTOR_
2ed14 53 5a 20 20 20 28 33 32 29 0a 23 64 65 66 69 6e  SZ   (32).#defin
2ed15 65 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41 53  e FTS3_ROTOR_MAS
2ed16 4b 20 28 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a  K (FTS3_ROTOR_SZ
2ed17 2d 31 29 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 65  -1)../*.** Add e
2ed18 6e 74 72 69 65 73 20 74 6f 20 70 53 6e 69 70 70  ntries to pSnipp
2ed19 65 74 2d 3e 61 4d 61 74 63 68 5b 5d 20 66 6f 72  et->aMatch[] for
2ed1a 20 65 76 65 72 79 20 6d 61 74 63 68 20 74 68 61   every match tha
2ed1b 74 20 6f 63 63 75 72 73 20 61 67 61 69 6e 73 74  t occurs against
2ed1c 0a 2a 2a 20 64 6f 63 75 6d 65 6e 74 20 7a 44 6f  .** document zDo
2ed1d 63 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 77 68 69  c[0..nDoc-1] whi
2ed1e 63 68 20 69 73 20 73 74 6f 72 65 64 20 69 6e 20  ch is stored in 
2ed1f 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75 6d 6e 2e 0a  column iColumn..
2ed20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2ed21 6e 69 70 70 65 74 4f 66 66 73 65 74 73 4f 66 43  nippetOffsetsOfC
2ed22 6f 6c 75 6d 6e 28 0a 20 20 51 75 65 72 79 20 2a  olumn(.  Query *
2ed23 70 51 75 65 72 79 2c 0a 20 20 53 6e 69 70 70 65  pQuery,.  Snippe
2ed24 74 20 2a 70 53 6e 69 70 70 65 74 2c 0a 20 20 69  t *pSnippet,.  i
2ed25 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20 63 6f  nt iColumn,.  co
2ed26 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c 0a  nst char *zDoc,.
2ed27 20 20 69 6e 74 20 6e 44 6f 63 0a 29 7b 0a 20 20    int nDoc.){.  
2ed28 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f  const sqlite3_to
2ed29 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a  kenizer_module *
2ed2a 70 54 4d 6f 64 75 6c 65 3b 20 20 2f 2a 20 54 68  pTModule;  /* Th
2ed2b 65 20 74 6f 6b 65 6e 69 7a 65 72 20 6d 6f 64 75  e tokenizer modu
2ed2c 6c 65 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  le */.  sqlite3_
2ed2d 74 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65  tokenizer *pToke
2ed2e 6e 69 7a 65 72 3b 20 20 20 20 20 20 20 20 20 20  nizer;          
2ed2f 20 20 20 2f 2a 20 54 68 65 20 73 70 65 63 69 66     /* The specif
2ed30 69 63 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  ic tokenizer */.
2ed31 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69    sqlite3_tokeni
2ed32 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 54 43 75  zer_cursor *pTCu
2ed33 72 73 6f 72 3b 20 20 20 20 20 20 20 20 2f 2a 20  rsor;        /* 
2ed34 54 6f 6b 65 6e 69 7a 65 72 20 63 75 72 73 6f 72  Tokenizer cursor
2ed35 20 2a 2f 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76   */.  fulltext_v
2ed36 74 61 62 20 2a 70 56 74 61 62 3b 20 20 20 20 20  tab *pVtab;     
2ed37 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
2ed38 65 20 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65  e full text inde
2ed39 78 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  x */.  int nColu
2ed3a 6d 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  mn;             
2ed3b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2ed3c 75 6d 62 65 72 20 6f 66 20 63 6f 6c 75 6d 6e 73  umber of columns
2ed3d 20 69 6e 20 74 68 65 20 69 6e 64 65 78 20 2a 2f   in the index */
2ed3e 0a 20 20 63 6f 6e 73 74 20 51 75 65 72 79 54 65  .  const QueryTe
2ed3f 72 6d 20 2a 61 54 65 72 6d 3b 20 20 20 20 20 20  rm *aTerm;      
2ed40 20 20 20 20 20 20 20 20 2f 2a 20 51 75 65 72 79          /* Query
2ed41 20 73 74 72 69 6e 67 20 74 65 72 6d 73 20 2a 2f   string terms */
2ed42 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 20 20 20  .  int nTerm;   
2ed43 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed44 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65          /* Numbe
2ed45 72 20 6f 66 20 71 75 65 72 79 20 73 74 72 69 6e  r of query strin
2ed46 67 20 74 65 72 6d 73 20 2a 2f 20 20 0a 20 20 69  g terms */  .  i
2ed47 6e 74 20 69 2c 20 6a 3b 20 20 20 20 20 20 20 20  nt i, j;        
2ed48 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed49 20 20 20 20 2f 2a 20 4c 6f 6f 70 20 63 6f 75 6e      /* Loop coun
2ed4a 74 65 72 73 20 2a 2f 0a 20 20 69 6e 74 20 72 63  ters */.  int rc
2ed4b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2ed4c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2ed4d 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20 2a 2f  * Return code */
2ed4e 0a 20 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20  .  unsigned int 
2ed4f 6d 61 74 63 68 2c 20 70 72 65 76 4d 61 74 63 68  match, prevMatch
2ed50 3b 20 20 20 20 20 20 20 2f 2a 20 50 68 72 61 73  ;       /* Phras
2ed51 65 20 73 65 61 72 63 68 20 62 69 74 6d 61 73 6b  e search bitmask
2ed52 73 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61  s */.  const cha
2ed53 72 20 2a 7a 54 6f 6b 65 6e 3b 20 20 20 20 20 20  r *zToken;      
2ed54 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2ed55 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f 6d 20 74  ext token from t
2ed56 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a  he tokenizer */.
2ed57 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 3b 20 20 20    int nToken;   
2ed58 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ed59 20 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f         /* Size o
2ed5a 66 20 7a 54 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e  f zToken */.  in
2ed5b 74 20 69 42 65 67 69 6e 2c 20 69 45 6e 64 2c 20  t iBegin, iEnd, 
2ed5c 69 50 6f 73 3b 20 20 20 20 20 20 20 20 20 20 20  iPos;           
2ed5d 20 20 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66     /* Offsets of
2ed5e 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64 20 65   beginning and e
2ed5f 6e 64 20 2a 2f 0a 0a 20 20 2f 2a 20 54 68 65 20  nd */..  /* The 
2ed60 66 6f 6c 6c 6f 77 69 6e 67 20 76 61 72 69 61 62  following variab
2ed61 6c 65 73 20 6b 65 65 70 20 61 20 63 69 72 63 75  les keep a circu
2ed62 6c 61 72 20 62 75 66 66 65 72 20 6f 66 20 74 68  lar buffer of th
2ed63 65 20 6c 61 73 74 0a 20 20 2a 2a 20 66 65 77 20  e last.  ** few 
2ed64 74 6f 6b 65 6e 73 20 2a 2f 0a 20 20 75 6e 73 69  tokens */.  unsi
2ed65 67 6e 65 64 20 69 6e 74 20 69 52 6f 74 6f 72 20  gned int iRotor 
2ed66 3d 20 30 3b 20 20 20 20 20 20 20 20 20 20 20 20  = 0;            
2ed67 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20 63 75 72   /* Index of cur
2ed68 72 65 6e 74 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20  rent token */.  
2ed69 69 6e 74 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b  int iRotorBegin[
2ed6a 46 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 5d 3b 20  FTS3_ROTOR_SZ]; 
2ed6b 20 20 20 20 20 2f 2a 20 42 65 67 69 6e 6e 69 6e       /* Beginnin
2ed6c 67 20 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65  g offset of toke
2ed6d 6e 20 2a 2f 0a 20 20 69 6e 74 20 69 52 6f 74 6f  n */.  int iRoto
2ed6e 72 4c 65 6e 5b 46 54 53 33 5f 52 4f 54 4f 52 5f  rLen[FTS3_ROTOR_
2ed6f 53 5a 5d 3b 20 20 20 20 20 20 20 20 2f 2a 20 4c  SZ];        /* L
2ed70 65 6e 67 74 68 20 6f 66 20 74 6f 6b 65 6e 20 2a  ength of token *
2ed71 2f 0a 0a 20 20 70 56 74 61 62 20 3d 20 70 51 75  /..  pVtab = pQu
2ed72 65 72 79 2d 3e 70 46 74 73 3b 0a 20 20 6e 43 6f  ery->pFts;.  nCo
2ed73 6c 75 6d 6e 20 3d 20 70 56 74 61 62 2d 3e 6e 43  lumn = pVtab->nC
2ed74 6f 6c 75 6d 6e 3b 0a 20 20 70 54 6f 6b 65 6e 69  olumn;.  pTokeni
2ed75 7a 65 72 20 3d 20 70 56 74 61 62 2d 3e 70 54 6f  zer = pVtab->pTo
2ed76 6b 65 6e 69 7a 65 72 3b 0a 20 20 70 54 4d 6f 64  kenizer;.  pTMod
2ed77 75 6c 65 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72  ule = pTokenizer
2ed78 2d 3e 70 4d 6f 64 75 6c 65 3b 0a 20 20 72 63 20  ->pModule;.  rc 
2ed79 3d 20 70 54 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65  = pTModule->xOpe
2ed7a 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 44  n(pTokenizer, zD
2ed7b 6f 63 2c 20 6e 44 6f 63 2c 20 26 70 54 43 75 72  oc, nDoc, &pTCur
2ed7c 73 6f 72 29 3b 0a 20 20 69 66 28 20 72 63 20 29  sor);.  if( rc )
2ed7d 20 72 65 74 75 72 6e 3b 0a 20 20 70 54 43 75 72   return;.  pTCur
2ed7e 73 6f 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 20  sor->pTokenizer 
2ed7f 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20  = pTokenizer;.  
2ed80 61 54 65 72 6d 20 3d 20 70 51 75 65 72 79 2d 3e  aTerm = pQuery->
2ed81 70 54 65 72 6d 73 3b 0a 20 20 6e 54 65 72 6d 20  pTerms;.  nTerm 
2ed82 3d 20 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73  = pQuery->nTerms
2ed83 3b 0a 20 20 69 66 28 20 6e 54 65 72 6d 3e 3d 46  ;.  if( nTerm>=F
2ed84 54 53 33 5f 52 4f 54 4f 52 5f 53 5a 20 29 7b 0a  TS3_ROTOR_SZ ){.
2ed85 20 20 20 20 6e 54 65 72 6d 20 3d 20 46 54 53 33      nTerm = FTS3
2ed86 5f 52 4f 54 4f 52 5f 53 5a 20 2d 20 31 3b 0a 20  _ROTOR_SZ - 1;. 
2ed87 20 7d 0a 20 20 70 72 65 76 4d 61 74 63 68 20 3d   }.  prevMatch =
2ed88 20 30 3b 0a 20 20 77 68 69 6c 65 28 31 29 7b 0a   0;.  while(1){.
2ed89 20 20 20 20 72 63 20 3d 20 70 54 4d 6f 64 75 6c      rc = pTModul
2ed8a 65 2d 3e 78 4e 65 78 74 28 70 54 43 75 72 73 6f  e->xNext(pTCurso
2ed8b 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54 6f  r, &zToken, &nTo
2ed8c 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c 20 26 69  ken, &iBegin, &i
2ed8d 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a 20 20 20  End, &iPos);.   
2ed8e 20 69 66 28 20 72 63 20 29 20 62 72 65 61 6b 3b   if( rc ) break;
2ed8f 0a 20 20 20 20 69 52 6f 74 6f 72 42 65 67 69 6e  .    iRotorBegin
2ed90 5b 69 52 6f 74 6f 72 26 46 54 53 33 5f 52 4f 54  [iRotor&FTS3_ROT
2ed91 4f 52 5f 4d 41 53 4b 5d 20 3d 20 69 42 65 67 69  OR_MASK] = iBegi
2ed92 6e 3b 0a 20 20 20 20 69 52 6f 74 6f 72 4c 65 6e  n;.    iRotorLen
2ed93 5b 69 52 6f 74 6f 72 26 46 54 53 33 5f 52 4f 54  [iRotor&FTS3_ROT
2ed94 4f 52 5f 4d 41 53 4b 5d 20 3d 20 69 45 6e 64 2d  OR_MASK] = iEnd-
2ed95 69 42 65 67 69 6e 3b 0a 20 20 20 20 6d 61 74 63  iBegin;.    matc
2ed96 68 20 3d 20 30 3b 0a 20 20 20 20 66 6f 72 28 69  h = 0;.    for(i
2ed97 3d 30 3b 20 69 3c 6e 54 65 72 6d 3b 20 69 2b 2b  =0; i<nTerm; i++
2ed98 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 43 6f  ){.      int iCo
2ed99 6c 3b 0a 20 20 20 20 20 20 69 43 6f 6c 20 3d 20  l;.      iCol = 
2ed9a 61 54 65 72 6d 5b 69 5d 2e 69 43 6f 6c 75 6d 6e  aTerm[i].iColumn
2ed9b 3b 0a 20 20 20 20 20 20 69 66 28 20 69 43 6f 6c  ;.      if( iCol
2ed9c 3e 3d 30 20 26 26 20 69 43 6f 6c 3c 6e 43 6f 6c  >=0 && iCol<nCol
2ed9d 75 6d 6e 20 26 26 20 69 43 6f 6c 21 3d 69 43 6f  umn && iCol!=iCo
2ed9e 6c 75 6d 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b  lumn ) continue;
2ed9f 0a 20 20 20 20 20 20 69 66 28 20 61 54 65 72 6d  .      if( aTerm
2eda0 5b 69 5d 2e 6e 54 65 72 6d 3e 6e 54 6f 6b 65 6e  [i].nTerm>nToken
2eda1 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20   ) continue;.   
2eda2 20 20 20 69 66 28 20 21 61 54 65 72 6d 5b 69 5d     if( !aTerm[i]
2eda3 2e 69 73 50 72 65 66 69 78 20 26 26 20 61 54 65  .isPrefix && aTe
2eda4 72 6d 5b 69 5d 2e 6e 54 65 72 6d 3c 6e 54 6f 6b  rm[i].nTerm<nTok
2eda5 65 6e 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20  en ) continue;. 
2eda6 20 20 20 20 20 61 73 73 65 72 74 28 20 61 54 65       assert( aTe
2eda7 72 6d 5b 69 5d 2e 6e 54 65 72 6d 3c 3d 6e 54 6f  rm[i].nTerm<=nTo
2eda8 6b 65 6e 20 29 3b 0a 20 20 20 20 20 20 69 66 28  ken );.      if(
2eda9 20 6d 65 6d 63 6d 70 28 61 54 65 72 6d 5b 69 5d   memcmp(aTerm[i]
2edaa 2e 70 54 65 72 6d 2c 20 7a 54 6f 6b 65 6e 2c 20  .pTerm, zToken, 
2edab 61 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d 29 20  aTerm[i].nTerm) 
2edac 29 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20  ) continue;.    
2edad 20 20 69 66 28 20 61 54 65 72 6d 5b 69 5d 2e 69    if( aTerm[i].i
2edae 50 68 72 61 73 65 3e 31 20 26 26 20 28 70 72 65  Phrase>1 && (pre
2edaf 76 4d 61 74 63 68 20 26 20 28 31 3c 3c 69 29 29  vMatch & (1<<i))
2edb0 3d 3d 30 20 29 20 63 6f 6e 74 69 6e 75 65 3b 0a  ==0 ) continue;.
2edb1 20 20 20 20 20 20 6d 61 74 63 68 20 7c 3d 20 31        match |= 1
2edb2 3c 3c 69 3b 0a 20 20 20 20 20 20 69 66 28 20 69  <<i;.      if( i
2edb3 3d 3d 6e 54 65 72 6d 2d 31 20 7c 7c 20 61 54 65  ==nTerm-1 || aTe
2edb4 72 6d 5b 69 2b 31 5d 2e 69 50 68 72 61 73 65 3d  rm[i+1].iPhrase=
2edb5 3d 31 20 29 7b 0a 20 20 20 20 20 20 20 20 66 6f  =1 ){.        fo
2edb6 72 28 6a 3d 61 54 65 72 6d 5b 69 5d 2e 69 50 68  r(j=aTerm[i].iPh
2edb7 72 61 73 65 2d 31 3b 20 6a 3e 3d 30 3b 20 6a 2d  rase-1; j>=0; j-
2edb8 2d 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e  -){.          in
2edb9 74 20 6b 20 3d 20 28 69 52 6f 74 6f 72 2d 6a 29  t k = (iRotor-j)
2edba 20 26 20 46 54 53 33 5f 52 4f 54 4f 52 5f 4d 41   & FTS3_ROTOR_MA
2edbb 53 4b 3b 0a 20 20 20 20 20 20 20 20 20 20 73 6e  SK;.          sn
2edbc 69 70 70 65 74 41 70 70 65 6e 64 4d 61 74 63 68  ippetAppendMatch
2edbd 28 70 53 6e 69 70 70 65 74 2c 20 69 43 6f 6c 75  (pSnippet, iColu
2edbe 6d 6e 2c 20 69 2d 6a 2c 20 69 50 6f 73 2d 6a 2c  mn, i-j, iPos-j,
2edbf 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2edc0 20 69 52 6f 74 6f 72 42 65 67 69 6e 5b 6b 5d 2c   iRotorBegin[k],
2edc1 20 69 52 6f 74 6f 72 4c 65 6e 5b 6b 5d 29 3b 0a   iRotorLen[k]);.
2edc2 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
2edc3 7d 0a 20 20 20 20 7d 0a 20 20 20 20 70 72 65 76  }.    }.    prev
2edc4 4d 61 74 63 68 20 3d 20 6d 61 74 63 68 3c 3c 31  Match = match<<1
2edc5 3b 0a 20 20 20 20 69 52 6f 74 6f 72 2b 2b 3b 0a  ;.    iRotor++;.
2edc6 20 20 7d 0a 20 20 70 54 4d 6f 64 75 6c 65 2d 3e    }.  pTModule->
2edc7 78 43 6c 6f 73 65 28 70 54 43 75 72 73 6f 72 29  xClose(pTCursor)
2edc8 3b 20 20 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d  ;  .}../*.** Rem
2edc9 6f 76 65 20 65 6e 74 72 69 65 73 20 66 72 6f 6d  ove entries from
2edca 20 74 68 65 20 70 53 6e 69 70 70 65 74 20 73 74   the pSnippet st
2edcb 72 75 63 74 75 72 65 20 74 6f 20 61 63 63 6f 75  ructure to accou
2edcc 6e 74 20 66 6f 72 20 74 68 65 20 4e 45 41 52 0a  nt for the NEAR.
2edcd 2a 2a 20 6f 70 65 72 61 74 6f 72 2e 20 57 68 65  ** operator. Whe
2edce 6e 20 74 68 69 73 20 69 73 20 63 61 6c 6c 65 64  n this is called
2edcf 2c 20 70 53 6e 69 70 70 65 74 20 63 6f 6e 74 61  , pSnippet conta
2edd0 69 6e 73 20 74 68 65 20 6c 69 73 74 20 6f 66 20  ins the list of 
2edd1 74 6f 6b 65 6e 20 0a 2a 2a 20 6f 66 66 73 65 74  token .** offset
2edd2 73 20 70 72 6f 64 75 63 65 64 20 62 79 20 74 72  s produced by tr
2edd3 65 61 74 69 6e 67 20 61 6c 6c 20 4e 45 41 52 20  eating all NEAR 
2edd4 6f 70 65 72 61 74 6f 72 73 20 61 73 20 41 4e 44  operators as AND
2edd5 20 6f 70 65 72 61 74 6f 72 73 2e 0a 2a 2a 20 54   operators..** T
2edd6 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 72 65 6d  his function rem
2edd7 6f 76 65 73 20 61 6e 79 20 65 6e 74 72 69 65 73  oves any entries
2edd8 20 74 68 61 74 20 73 68 6f 75 6c 64 20 6e 6f 74   that should not
2edd9 20 62 65 20 70 72 65 73 65 6e 74 20 61 66 74 65   be present afte
2edda 72 0a 2a 2a 20 61 63 63 6f 75 6e 74 69 6e 67 20  r.** accounting 
2eddb 66 6f 72 20 74 68 65 20 4e 45 41 52 20 72 65 73  for the NEAR res
2eddc 74 72 69 63 74 69 6f 6e 2e 20 46 6f 72 20 65 78  triction. For ex
2eddd 61 6d 70 6c 65 2c 20 69 66 20 74 68 65 20 71 75  ample, if the qu
2edde 65 72 69 65 64 0a 2a 2a 20 64 6f 63 75 6d 65 6e  eried.** documen
2eddf 74 20 69 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  t is:.**.**     
2ede0 22 41 20 42 20 43 20 44 20 45 20 41 22 0a 2a 2a  "A B C D E A".**
2ede1 0a 2a 2a 20 61 6e 64 20 74 68 65 20 71 75 65 72  .** and the quer
2ede2 79 20 69 73 3a 0a 2a 2a 20 0a 2a 2a 20 20 20 20  y is:.** .**    
2ede3 20 41 20 4e 45 41 52 2f 30 20 45 0a 2a 2a 0a 2a   A NEAR/0 E.**.*
2ede4 2a 20 74 68 65 6e 20 77 68 65 6e 20 74 68 69 73  * then when this
2ede5 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61 6c   function is cal
2ede6 6c 65 64 20 74 68 65 20 53 6e 69 70 70 65 74 20  led the Snippet 
2ede7 63 6f 6e 74 61 69 6e 73 20 74 6f 6b 65 6e 20 6f  contains token o
2ede8 66 66 73 65 74 73 0a 2a 2a 20 30 2c 20 34 20 61  ffsets.** 0, 4 a
2ede9 6e 64 20 35 2e 20 54 68 69 73 20 66 75 6e 63 74  nd 5. This funct
2edea 69 6f 6e 20 72 65 6d 6f 76 65 73 20 74 68 65 20  ion removes the 
2edeb 22 30 22 20 65 6e 74 72 79 20 28 62 65 63 61 75  "0" entry (becau
2edec 73 65 20 74 68 65 20 66 69 72 73 74 20 41 0a 2a  se the first A.*
2eded 2a 20 69 73 20 6e 6f 74 20 6e 65 61 72 20 65 6e  * is not near en
2edee 6f 75 67 68 20 74 6f 20 61 6e 20 45 29 2e 0a 2a  ough to an E)..*
2edef 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 72  /.static void tr
2edf0 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
2edf1 46 6f 72 4e 65 61 72 28 51 75 65 72 79 20 2a 70  ForNear(Query *p
2edf2 51 75 65 72 79 2c 20 53 6e 69 70 70 65 74 20 2a  Query, Snippet *
2edf3 70 53 6e 69 70 70 65 74 29 7b 0a 20 20 69 6e 74  pSnippet){.  int
2edf4 20 69 69 3b 0a 20 20 69 6e 74 20 69 44 69 72 20   ii;.  int iDir 
2edf5 3d 20 31 3b 0a 0a 20 20 77 68 69 6c 65 28 69 44  = 1;..  while(iD
2edf6 69 72 3e 2d 32 29 20 7b 0a 20 20 20 20 61 73 73  ir>-2) {.    ass
2edf7 65 72 74 28 20 69 44 69 72 3d 3d 31 20 7c 7c 20  ert( iDir==1 || 
2edf8 69 44 69 72 3d 3d 2d 31 20 29 3b 0a 20 20 20 20  iDir==-1 );.    
2edf9 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 53 6e  for(ii=0; ii<pSn
2edfa 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20 69  ippet->nMatch; i
2edfb 69 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20  i++){.      int 
2edfc 6a 6a 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e 4e  jj;.      int nN
2edfd 65 61 72 3b 0a 20 20 20 20 20 20 73 74 72 75 63  ear;.      struc
2edfe 74 20 73 6e 69 70 70 65 74 4d 61 74 63 68 20 2a  t snippetMatch *
2edff 70 4d 61 74 63 68 20 3d 20 26 70 53 6e 69 70 70  pMatch = &pSnipp
2ee00 65 74 2d 3e 61 4d 61 74 63 68 5b 69 69 5d 3b 0a  et->aMatch[ii];.
2ee01 20 20 20 20 20 20 51 75 65 72 79 54 65 72 6d 20        QueryTerm 
2ee02 2a 70 51 75 65 72 79 54 65 72 6d 20 3d 20 26 70  *pQueryTerm = &p
2ee03 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b 70 4d  Query->pTerms[pM
2ee04 61 74 63 68 2d 3e 69 54 65 72 6d 5d 3b 0a 0a 20  atch->iTerm];.. 
2ee05 20 20 20 20 20 69 66 28 20 28 70 4d 61 74 63 68       if( (pMatch
2ee06 2d 3e 69 54 65 72 6d 2b 69 44 69 72 29 3c 30 20  ->iTerm+iDir)<0 
2ee07 0a 20 20 20 20 20 20 20 7c 7c 20 28 70 4d 61 74  .       || (pMat
2ee08 63 68 2d 3e 69 54 65 72 6d 2b 69 44 69 72 29 3e  ch->iTerm+iDir)>
2ee09 3d 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 0a  =pQuery->nTerms.
2ee0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20 20        ){.       
2ee0b 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 20   continue;.     
2ee0c 20 7d 0a 20 20 20 20 20 0a 20 20 20 20 20 20 6e   }.     .      n
2ee0d 4e 65 61 72 20 3d 20 70 51 75 65 72 79 54 65 72  Near = pQueryTer
2ee0e 6d 2d 3e 6e 4e 65 61 72 3b 0a 20 20 20 20 20 20  m->nNear;.      
2ee0f 69 66 28 20 69 44 69 72 3c 30 20 29 7b 0a 20 20  if( iDir<0 ){.  
2ee10 20 20 20 20 20 20 6e 4e 65 61 72 20 3d 20 70 51        nNear = pQ
2ee11 75 65 72 79 54 65 72 6d 5b 2d 31 5d 2e 6e 4e 65  ueryTerm[-1].nNe
2ee12 61 72 3b 0a 20 20 20 20 20 20 7d 0a 20 20 0a 20  ar;.      }.  . 
2ee13 20 20 20 20 20 69 66 28 20 70 4d 61 74 63 68 2d       if( pMatch-
2ee14 3e 69 54 65 72 6d 3e 3d 30 20 26 26 20 6e 4e 65  >iTerm>=0 && nNe
2ee15 61 72 20 29 7b 0a 20 20 20 20 20 20 20 20 69 6e  ar ){.        in
2ee16 74 20 69 73 4f 6b 20 3d 20 30 3b 0a 20 20 20 20  t isOk = 0;.    
2ee17 20 20 20 20 69 6e 74 20 69 4e 65 78 74 54 65 72      int iNextTer
2ee18 6d 20 3d 20 70 4d 61 74 63 68 2d 3e 69 54 65 72  m = pMatch->iTer
2ee19 6d 2b 69 44 69 72 3b 0a 20 20 20 20 20 20 20 20  m+iDir;.        
2ee1a 69 6e 74 20 69 50 72 65 76 54 65 72 6d 20 3d 20  int iPrevTerm = 
2ee1b 69 4e 65 78 74 54 65 72 6d 3b 0a 0a 20 20 20 20  iNextTerm;..    
2ee1c 20 20 20 20 69 6e 74 20 69 45 6e 64 54 6f 6b 65      int iEndToke
2ee1d 6e 3b 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69  n;.        int i
2ee1e 53 74 61 72 74 54 6f 6b 65 6e 3b 0a 0a 20 20 20  StartToken;..   
2ee1f 20 20 20 20 20 69 66 28 20 69 44 69 72 3c 30 20       if( iDir<0 
2ee20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 6e 74  ){.          int
2ee21 20 6e 50 68 72 61 73 65 20 3d 20 31 3b 0a 20 20   nPhrase = 1;.  
2ee22 20 20 20 20 20 20 20 20 69 53 74 61 72 74 54 6f          iStartTo
2ee23 6b 65 6e 20 3d 20 70 4d 61 74 63 68 2d 3e 69 54  ken = pMatch->iT
2ee24 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20 20 20 20  oken;.          
2ee25 77 68 69 6c 65 28 20 28 70 4d 61 74 63 68 2d 3e  while( (pMatch->
2ee26 69 54 65 72 6d 2b 6e 50 68 72 61 73 65 29 3c 70  iTerm+nPhrase)<p
2ee27 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 20 0a 20  Query->nTerms . 
2ee28 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
2ee29 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b 70  pQuery->pTerms[p
2ee2a 4d 61 74 63 68 2d 3e 69 54 65 72 6d 2b 6e 50 68  Match->iTerm+nPh
2ee2b 72 61 73 65 5d 2e 69 50 68 72 61 73 65 3e 31 20  rase].iPhrase>1 
2ee2c 0a 20 20 20 20 20 20 20 20 20 20 29 7b 0a 20 20  .          ){.  
2ee2d 20 20 20 20 20 20 20 20 20 20 6e 50 68 72 61 73            nPhras
2ee2e 65 2b 2b 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  e++;.          }
2ee2f 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e 64 54  .          iEndT
2ee30 6f 6b 65 6e 20 3d 20 69 53 74 61 72 74 54 6f 6b  oken = iStartTok
2ee31 65 6e 20 2b 20 6e 50 68 72 61 73 65 20 2d 20 31  en + nPhrase - 1
2ee32 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b  ;.        }else{
2ee33 0a 20 20 20 20 20 20 20 20 20 20 69 45 6e 64 54  .          iEndT
2ee34 6f 6b 65 6e 20 20 20 3d 20 70 4d 61 74 63 68 2d  oken   = pMatch-
2ee35 3e 69 54 6f 6b 65 6e 3b 0a 20 20 20 20 20 20 20  >iToken;.       
2ee36 20 20 20 69 53 74 61 72 74 54 6f 6b 65 6e 20 3d     iStartToken =
2ee37 20 70 4d 61 74 63 68 2d 3e 69 54 6f 6b 65 6e 2b   pMatch->iToken+
2ee38 31 2d 70 51 75 65 72 79 54 65 72 6d 2d 3e 69 50  1-pQueryTerm->iP
2ee39 68 72 61 73 65 3b 0a 20 20 20 20 20 20 20 20 7d  hrase;.        }
2ee3a 0a 0a 20 20 20 20 20 20 20 20 77 68 69 6c 65 28  ..        while(
2ee3b 20 70 51 75 65 72 79 2d 3e 70 54 65 72 6d 73 5b   pQuery->pTerms[
2ee3c 69 4e 65 78 74 54 65 72 6d 5d 2e 69 50 68 72 61  iNextTerm].iPhra
2ee3d 73 65 3e 31 20 29 7b 0a 20 20 20 20 20 20 20 20  se>1 ){.        
2ee3e 20 20 69 4e 65 78 74 54 65 72 6d 2d 2d 3b 0a 20    iNextTerm--;. 
2ee3f 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
2ee40 20 77 68 69 6c 65 28 20 28 69 50 72 65 76 54 65   while( (iPrevTe
2ee41 72 6d 2b 31 29 3c 70 51 75 65 72 79 2d 3e 6e 54  rm+1)<pQuery->nT
2ee42 65 72 6d 73 20 26 26 20 0a 20 20 20 20 20 20 20  erms && .       
2ee43 20 20 20 20 20 20 20 20 70 51 75 65 72 79 2d 3e          pQuery->
2ee44 70 54 65 72 6d 73 5b 69 50 72 65 76 54 65 72 6d  pTerms[iPrevTerm
2ee45 2b 31 5d 2e 69 50 68 72 61 73 65 3e 31 20 0a 20  +1].iPhrase>1 . 
2ee46 20 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20         ){.      
2ee47 20 20 20 20 69 50 72 65 76 54 65 72 6d 2b 2b 3b      iPrevTerm++;
2ee48 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 0a 20 20  .        }.  .  
2ee49 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20        for(jj=0; 
2ee4a 69 73 4f 6b 3d 3d 30 20 26 26 20 6a 6a 3c 70 53  isOk==0 && jj<pS
2ee4b 6e 69 70 70 65 74 2d 3e 6e 4d 61 74 63 68 3b 20  nippet->nMatch; 
2ee4c 6a 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20  jj++){.         
2ee4d 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74 4d   struct snippetM
2ee4e 61 74 63 68 20 2a 70 20 3d 20 26 70 53 6e 69 70  atch *p = &pSnip
2ee4f 70 65 74 2d 3e 61 4d 61 74 63 68 5b 6a 6a 5d 3b  pet->aMatch[jj];
2ee50 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20 70  .          if( p
2ee51 2d 3e 69 43 6f 6c 3d 3d 70 4d 61 74 63 68 2d 3e  ->iCol==pMatch->
2ee52 69 43 6f 6c 20 26 26 20 28 28 0a 20 20 20 20 20  iCol && ((.     
2ee53 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 54 65            p->iTe
2ee54 72 6d 3d 3d 69 4e 65 78 74 54 65 72 6d 20 26 26  rm==iNextTerm &&
2ee55 20 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20   .              
2ee56 20 70 2d 3e 69 54 6f 6b 65 6e 3e 69 45 6e 64 54   p->iToken>iEndT
2ee57 6f 6b 65 6e 20 26 26 20 0a 20 20 20 20 20 20 20  oken && .       
2ee58 20 20 20 20 20 20 20 20 70 2d 3e 69 54 6f 6b 65          p->iToke
2ee59 6e 3c 3d 69 45 6e 64 54 6f 6b 65 6e 2b 6e 4e 65  n<=iEndToken+nNe
2ee5a 61 72 0a 20 20 20 20 20 20 20 20 20 20 29 20 7c  ar.          ) |
2ee5b 7c 20 28 0a 20 20 20 20 20 20 20 20 20 20 20 20  | (.            
2ee5c 20 20 20 70 2d 3e 69 54 65 72 6d 3d 3d 69 50 72     p->iTerm==iPr
2ee5d 65 76 54 65 72 6d 20 26 26 20 0a 20 20 20 20 20  evTerm && .     
2ee5e 20 20 20 20 20 20 20 20 20 20 70 2d 3e 69 54 6f            p->iTo
2ee5f 6b 65 6e 3c 69 53 74 61 72 74 54 6f 6b 65 6e 20  ken<iStartToken 
2ee60 26 26 20 0a 20 20 20 20 20 20 20 20 20 20 20 20  && .            
2ee61 20 20 20 70 2d 3e 69 54 6f 6b 65 6e 3e 3d 69 53     p->iToken>=iS
2ee62 74 61 72 74 54 6f 6b 65 6e 2d 6e 4e 65 61 72 0a  tartToken-nNear.
2ee63 20 20 20 20 20 20 20 20 20 20 29 29 29 7b 0a 20            ))){. 
2ee64 20 20 20 20 20 20 20 20 20 20 20 69 73 4f 6b 20             isOk 
2ee65 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 20 20 7d  = 1;.          }
2ee66 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
2ee67 20 20 20 69 66 28 20 21 69 73 4f 6b 20 29 7b 0a     if( !isOk ){.
2ee68 20 20 20 20 20 20 20 20 20 20 66 6f 72 28 6a 6a            for(jj
2ee69 3d 31 2d 70 51 75 65 72 79 54 65 72 6d 2d 3e 69  =1-pQueryTerm->i
2ee6a 50 68 72 61 73 65 3b 20 6a 6a 3c 3d 30 3b 20 6a  Phrase; jj<=0; j
2ee6b 6a 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20  j++){.          
2ee6c 20 20 70 4d 61 74 63 68 5b 6a 6a 5d 2e 69 54 65    pMatch[jj].iTe
2ee6d 72 6d 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20  rm = -1;.       
2ee6e 20 20 20 7d 0a 20 20 20 20 20 20 20 20 20 20 69     }.          i
2ee6f 69 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 20 20  i = -1;.        
2ee70 20 20 69 44 69 72 20 3d 20 31 3b 0a 20 20 20 20    iDir = 1;.    
2ee71 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
2ee72 20 20 7d 0a 20 20 20 20 69 44 69 72 20 2d 3d 20    }.    iDir -= 
2ee73 32 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  2;.  }.}../*.** 
2ee74 43 6f 6d 70 75 74 65 20 61 6c 6c 20 6f 66 66 73  Compute all offs
2ee75 65 74 73 20 66 6f 72 20 74 68 65 20 63 75 72 72  ets for the curr
2ee76 65 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 71  ent row of the q
2ee77 75 65 72 79 2e 20 20 0a 2a 2a 20 49 66 20 74 68  uery.  .** If th
2ee78 65 20 6f 66 66 73 65 74 73 20 68 61 76 65 20 61  e offsets have a
2ee79 6c 72 65 61 64 79 20 62 65 65 6e 20 63 6f 6d 70  lready been comp
2ee7a 75 74 65 64 2c 20 74 68 69 73 20 72 6f 75 74 69  uted, this routi
2ee7b 6e 65 20 69 73 20 61 20 6e 6f 2d 6f 70 2e 0a 2a  ne is a no-op..*
2ee7c 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e  /.static void sn
2ee7d 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28  ippetAllOffsets(
2ee7e 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20  fulltext_cursor 
2ee7f 2a 70 29 7b 0a 20 20 69 6e 74 20 6e 43 6f 6c 75  *p){.  int nColu
2ee80 6d 6e 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d  mn;.  int iColum
2ee81 6e 2c 20 69 3b 0a 20 20 69 6e 74 20 69 46 69 72  n, i;.  int iFir
2ee82 73 74 2c 20 69 4c 61 73 74 3b 0a 20 20 66 75 6c  st, iLast;.  ful
2ee83 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 46 74 73  ltext_vtab *pFts
2ee84 3b 0a 0a 20 20 69 66 28 20 70 2d 3e 73 6e 69 70  ;..  if( p->snip
2ee85 70 65 74 2e 6e 4d 61 74 63 68 20 29 20 72 65 74  pet.nMatch ) ret
2ee86 75 72 6e 3b 0a 20 20 69 66 28 20 70 2d 3e 71 2e  urn;.  if( p->q.
2ee87 6e 54 65 72 6d 73 3d 3d 30 20 29 20 72 65 74 75  nTerms==0 ) retu
2ee88 72 6e 3b 0a 20 20 70 46 74 73 20 3d 20 70 2d 3e  rn;.  pFts = p->
2ee89 71 2e 70 46 74 73 3b 0a 20 20 6e 43 6f 6c 75 6d  q.pFts;.  nColum
2ee8a 6e 20 3d 20 70 46 74 73 2d 3e 6e 43 6f 6c 75 6d  n = pFts->nColum
2ee8b 6e 3b 0a 20 20 69 43 6f 6c 75 6d 6e 20 3d 20 28  n;.  iColumn = (
2ee8c 70 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20 2d  p->iCursorType -
2ee8d 20 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 29   QUERY_FULLTEXT)
2ee8e 3b 0a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3c  ;.  if( iColumn<
2ee8f 30 20 7c 7c 20 69 43 6f 6c 75 6d 6e 3e 3d 6e 43  0 || iColumn>=nC
2ee90 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 69 46 69  olumn ){.    iFi
2ee91 72 73 74 20 3d 20 30 3b 0a 20 20 20 20 69 4c 61  rst = 0;.    iLa
2ee92 73 74 20 3d 20 6e 43 6f 6c 75 6d 6e 2d 31 3b 0a  st = nColumn-1;.
2ee93 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 46 69    }else{.    iFi
2ee94 72 73 74 20 3d 20 69 43 6f 6c 75 6d 6e 3b 0a 20  rst = iColumn;. 
2ee95 20 20 20 69 4c 61 73 74 20 3d 20 69 43 6f 6c 75     iLast = iColu
2ee96 6d 6e 3b 0a 20 20 7d 0a 20 20 66 6f 72 28 69 3d  mn;.  }.  for(i=
2ee97 69 46 69 72 73 74 3b 20 69 3c 3d 69 4c 61 73 74  iFirst; i<=iLast
2ee98 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 6f 6e 73  ; i++){.    cons
2ee99 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20 20  t char *zDoc;.  
2ee9a 20 20 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 20 20    int nDoc;.    
2ee9b 7a 44 6f 63 20 3d 20 28 63 6f 6e 73 74 20 63 68  zDoc = (const ch
2ee9c 61 72 2a 29 73 71 6c 69 74 65 33 5f 63 6f 6c 75  ar*)sqlite3_colu
2ee9d 6d 6e 5f 74 65 78 74 28 70 2d 3e 70 53 74 6d 74  mn_text(p->pStmt
2ee9e 2c 20 69 2b 31 29 3b 0a 20 20 20 20 6e 44 6f 63  , i+1);.    nDoc
2ee9f 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d   = sqlite3_colum
2eea0 6e 5f 62 79 74 65 73 28 70 2d 3e 70 53 74 6d 74  n_bytes(p->pStmt
2eea1 2c 20 69 2b 31 29 3b 0a 20 20 20 20 73 6e 69 70  , i+1);.    snip
2eea2 70 65 74 4f 66 66 73 65 74 73 4f 66 43 6f 6c 75  petOffsetsOfColu
2eea3 6d 6e 28 26 70 2d 3e 71 2c 20 26 70 2d 3e 73 6e  mn(&p->q, &p->sn
2eea4 69 70 70 65 74 2c 20 69 2c 20 7a 44 6f 63 2c 20  ippet, i, zDoc, 
2eea5 6e 44 6f 63 29 3b 0a 20 20 7d 0a 0a 20 20 74 72  nDoc);.  }..  tr
2eea6 69 6d 53 6e 69 70 70 65 74 4f 66 66 73 65 74 73  imSnippetOffsets
2eea7 46 6f 72 4e 65 61 72 28 26 70 2d 3e 71 2c 20 26  ForNear(&p->q, &
2eea8 70 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 7d 0a 0a  p->snippet);.}..
2eea9 2f 2a 0a 2a 2a 20 43 6f 6e 76 65 72 74 20 74 68  /*.** Convert th
2eeaa 65 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 69 6e  e information in
2eeab 20 74 68 65 20 61 4d 61 74 63 68 5b 5d 20 61 72   the aMatch[] ar
2eeac 72 61 79 20 6f 66 20 74 68 65 20 73 6e 69 70 70  ray of the snipp
2eead 65 74 0a 2a 2a 20 69 6e 74 6f 20 74 68 65 20 73  et.** into the s
2eeae 74 72 69 6e 67 20 7a 4f 66 66 73 65 74 5b 30 2e  tring zOffset[0.
2eeaf 2e 6e 4f 66 66 73 65 74 2d 31 5d 2e 0a 2a 2f 0a  .nOffset-1]..*/.
2eeb0 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69 70  static void snip
2eeb1 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 53 6e  petOffsetText(Sn
2eeb2 69 70 70 65 74 20 2a 70 29 7b 0a 20 20 69 6e 74  ippet *p){.  int
2eeb3 20 69 3b 0a 20 20 69 6e 74 20 63 6e 74 20 3d 20   i;.  int cnt = 
2eeb4 30 3b 0a 20 20 53 74 72 69 6e 67 42 75 66 66 65  0;.  StringBuffe
2eeb5 72 20 73 62 3b 0a 20 20 63 68 61 72 20 7a 42 75  r sb;.  char zBu
2eeb6 66 5b 32 30 30 5d 3b 0a 20 20 69 66 28 20 70 2d  f[200];.  if( p-
2eeb7 3e 7a 4f 66 66 73 65 74 20 29 20 72 65 74 75 72  >zOffset ) retur
2eeb8 6e 3b 0a 20 20 69 6e 69 74 53 74 72 69 6e 67 42  n;.  initStringB
2eeb9 75 66 66 65 72 28 26 73 62 29 3b 0a 20 20 66 6f  uffer(&sb);.  fo
2eeba 72 28 69 3d 30 3b 20 69 3c 70 2d 3e 6e 4d 61 74  r(i=0; i<p->nMat
2eebb 63 68 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 73 74  ch; i++){.    st
2eebc 72 75 63 74 20 73 6e 69 70 70 65 74 4d 61 74 63  ruct snippetMatc
2eebd 68 20 2a 70 4d 61 74 63 68 20 3d 20 26 70 2d 3e  h *pMatch = &p->
2eebe 61 4d 61 74 63 68 5b 69 5d 3b 0a 20 20 20 20 69  aMatch[i];.    i
2eebf 66 28 20 70 4d 61 74 63 68 2d 3e 69 54 65 72 6d  f( pMatch->iTerm
2eec0 3e 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  >=0 ){.      /* 
2eec1 49 66 20 73 6e 69 70 70 65 74 4d 61 74 63 68 2e  If snippetMatch.
2eec2 69 54 65 72 6d 20 69 73 20 6c 65 73 73 20 74 68  iTerm is less th
2eec3 61 6e 20 30 2c 20 74 68 65 6e 20 74 68 65 20 6d  an 0, then the m
2eec4 61 74 63 68 20 77 61 73 20 0a 20 20 20 20 20 20  atch was .      
2eec5 2a 2a 20 64 69 73 63 61 72 64 65 64 20 61 73 20  ** discarded as 
2eec6 70 61 72 74 20 6f 66 20 70 72 6f 63 65 73 73 69  part of processi
2eec7 6e 67 20 74 68 65 20 4e 45 41 52 20 6f 70 65 72  ng the NEAR oper
2eec8 61 74 6f 72 20 28 73 65 65 20 74 68 65 20 0a 20  ator (see the . 
2eec9 20 20 20 20 20 2a 2a 20 74 72 69 6d 53 6e 69 70       ** trimSnip
2eeca 70 65 74 4f 66 66 73 65 74 73 46 6f 72 4e 65 61  petOffsetsForNea
2eecb 72 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72  r() function for
2eecc 20 64 65 74 61 69 6c 73 29 2e 20 49 67 6e 6f 72   details). Ignor
2eecd 65 20 0a 20 20 20 20 20 20 2a 2a 20 69 74 20 69  e .      ** it i
2eece 6e 20 74 68 69 73 20 63 61 73 65 0a 20 20 20 20  n this case.    
2eecf 20 20 2a 2f 0a 20 20 20 20 20 20 7a 42 75 66 5b    */.      zBuf[
2eed0 30 5d 20 3d 20 27 20 27 3b 0a 20 20 20 20 20 20  0] = ' ';.      
2eed1 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e 74 66  sqlite3_snprintf
2eed2 28 73 69 7a 65 6f 66 28 7a 42 75 66 29 2d 31 2c  (sizeof(zBuf)-1,
2eed3 20 26 7a 42 75 66 5b 63 6e 74 3e 30 5d 2c 20 22   &zBuf[cnt>0], "
2eed4 25 64 20 25 64 20 25 64 20 25 64 22 2c 0a 20 20  %d %d %d %d",.  
2eed5 20 20 20 20 20 20 20 20 70 4d 61 74 63 68 2d 3e          pMatch->
2eed6 69 43 6f 6c 2c 20 70 4d 61 74 63 68 2d 3e 69 54  iCol, pMatch->iT
2eed7 65 72 6d 2c 20 70 4d 61 74 63 68 2d 3e 69 53 74  erm, pMatch->iSt
2eed8 61 72 74 2c 20 70 4d 61 74 63 68 2d 3e 6e 42 79  art, pMatch->nBy
2eed9 74 65 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e  te);.      appen
2eeda 64 28 26 73 62 2c 20 7a 42 75 66 29 3b 0a 20 20  d(&sb, zBuf);.  
2eedb 20 20 20 20 63 6e 74 2b 2b 3b 0a 20 20 20 20 7d      cnt++;.    }
2eedc 0a 20 20 7d 0a 20 20 70 2d 3e 7a 4f 66 66 73 65  .  }.  p->zOffse
2eedd 74 20 3d 20 73 74 72 69 6e 67 42 75 66 66 65 72  t = stringBuffer
2eede 44 61 74 61 28 26 73 62 29 3b 0a 20 20 70 2d 3e  Data(&sb);.  p->
2eedf 6e 4f 66 66 73 65 74 20 3d 20 73 74 72 69 6e 67  nOffset = string
2eee0 42 75 66 66 65 72 4c 65 6e 67 74 68 28 26 73 62  BufferLength(&sb
2eee1 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 7a 44 6f 63  );.}../*.** zDoc
2eee2 5b 30 2e 2e 6e 44 6f 63 2d 31 5d 20 69 73 20 70  [0..nDoc-1] is p
2eee3 68 72 61 73 65 20 6f 66 20 74 65 78 74 2e 20 20  hrase of text.  
2eee4 61 4d 61 74 63 68 5b 30 2e 2e 6e 4d 61 74 63 68  aMatch[0..nMatch
2eee5 2d 31 5d 20 61 72 65 20 61 20 73 65 74 0a 2a 2a  -1] are a set.**
2eee6 20 6f 66 20 6d 61 74 63 68 69 6e 67 20 77 6f 72   of matching wor
2eee7 64 73 20 73 6f 6d 65 20 6f 66 20 77 68 69 63 68  ds some of which
2eee8 20 6d 69 67 68 74 20 62 65 20 69 6e 20 7a 44 6f   might be in zDo
2eee9 63 2e 20 20 7a 44 6f 63 20 69 73 20 63 6f 6c 75  c.  zDoc is colu
2eeea 6d 6e 0a 2a 2a 20 6e 75 6d 62 65 72 20 69 43 6f  mn.** number iCo
2eeeb 6c 2e 0a 2a 2a 0a 2a 2a 20 69 42 72 65 61 6b 20  l..**.** iBreak 
2eeec 69 73 20 73 75 67 67 65 73 74 65 64 20 73 70 6f  is suggested spo
2eeed 74 20 69 6e 20 7a 44 6f 63 20 77 68 65 72 65 20  t in zDoc where 
2eeee 77 65 20 63 6f 75 6c 64 20 62 65 67 69 6e 20 6f  we could begin o
2eeef 72 20 65 6e 64 20 61 6e 0a 2a 2a 20 65 78 63 65  r end an.** exce
2eef0 72 70 74 2e 20 20 52 65 74 75 72 6e 20 61 20 76  rpt.  Return a v
2eef1 61 6c 75 65 20 73 69 6d 69 6c 61 72 20 74 6f 20  alue similar to 
2eef2 69 42 72 65 61 6b 20 62 75 74 20 70 6f 73 73 69  iBreak but possi
2eef3 62 6c 79 20 61 64 6a 75 73 74 65 64 0a 2a 2a 20  bly adjusted.** 
2eef4 74 6f 20 62 65 20 61 20 6c 69 74 74 6c 65 20 6c  to be a little l
2eef5 65 66 74 20 6f 72 20 72 69 67 68 74 20 73 6f 20  eft or right so 
2eef6 74 68 61 74 20 74 68 65 20 62 72 65 61 6b 20 70  that the break p
2eef7 6f 69 6e 74 20 69 73 20 62 65 74 74 65 72 2e 0a  oint is better..
2eef8 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 6f  */.static int wo
2eef9 72 64 42 6f 75 6e 64 61 72 79 28 0a 20 20 69 6e  rdBoundary(.  in
2eefa 74 20 69 42 72 65 61 6b 2c 20 20 20 20 20 20 20  t iBreak,       
2eefb 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54              /* T
2eefc 68 65 20 73 75 67 67 65 73 74 65 64 20 62 72 65  he suggested bre
2eefd 61 6b 20 70 6f 69 6e 74 20 2a 2f 0a 20 20 63 6f  ak point */.  co
2eefe 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 2c 20  nst char *zDoc, 
2eeff 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
2ef00 6f 63 75 6d 65 6e 74 20 74 65 78 74 20 2a 2f 0a  ocument text */.
2ef01 20 20 69 6e 74 20 6e 44 6f 63 2c 20 20 20 20 20    int nDoc,     
2ef02 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ef03 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
2ef04 65 73 20 69 6e 20 7a 44 6f 63 5b 5d 20 2a 2f 0a  es in zDoc[] */.
2ef05 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65 74    struct snippet
2ef06 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 2c 20 20  Match *aMatch,  
2ef07 2f 2a 20 4d 61 74 63 68 69 6e 67 20 77 6f 72 64  /* Matching word
2ef08 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 4d 61 74 63  s */.  int nMatc
2ef09 68 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h,              
2ef0a 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f       /* Number o
2ef0b 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61 4d 61  f entries in aMa
2ef0c 74 63 68 5b 5d 20 2a 2f 0a 20 20 69 6e 74 20 69  tch[] */.  int i
2ef0d 43 6f 6c 20 20 20 20 20 20 20 20 20 20 20 20 20  Col             
2ef0e 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20           /* The 
2ef0f 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65 72 20 66 6f  column number fo
2ef10 72 20 7a 44 6f 63 5b 5d 20 2a 2f 0a 29 7b 0a 20  r zDoc[] */.){. 
2ef11 20 69 6e 74 20 69 3b 0a 20 20 69 66 28 20 69 42   int i;.  if( iB
2ef12 72 65 61 6b 3c 3d 31 30 20 29 7b 0a 20 20 20 20  reak<=10 ){.    
2ef13 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20 20  return 0;.  }.  
2ef14 69 66 28 20 69 42 72 65 61 6b 3e 3d 6e 44 6f 63  if( iBreak>=nDoc
2ef15 2d 31 30 20 29 7b 0a 20 20 20 20 72 65 74 75 72  -10 ){.    retur
2ef16 6e 20 6e 44 6f 63 3b 0a 20 20 7d 0a 20 20 66 6f  n nDoc;.  }.  fo
2ef17 72 28 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20  r(i=0; i<nMatch 
2ef18 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69 43 6f  && aMatch[i].iCo
2ef19 6c 3c 69 43 6f 6c 3b 20 69 2b 2b 29 7b 7d 0a 20  l<iCol; i++){}. 
2ef1a 20 77 68 69 6c 65 28 20 69 3c 6e 4d 61 74 63 68   while( i<nMatch
2ef1b 20 26 26 20 61 4d 61 74 63 68 5b 69 5d 2e 69 53   && aMatch[i].iS
2ef1c 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 5d 2e 6e  tart+aMatch[i].n
2ef1d 42 79 74 65 3c 69 42 72 65 61 6b 20 29 7b 20 69  Byte<iBreak ){ i
2ef1e 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 69 3c 6e 4d  ++; }.  if( i<nM
2ef1f 61 74 63 68 20 29 7b 0a 20 20 20 20 69 66 28 20  atch ){.    if( 
2ef20 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
2ef21 3c 69 42 72 65 61 6b 2b 31 30 20 29 7b 0a 20 20  <iBreak+10 ){.  
2ef22 20 20 20 20 72 65 74 75 72 6e 20 61 4d 61 74 63      return aMatc
2ef23 68 5b 69 5d 2e 69 53 74 61 72 74 3b 0a 20 20 20  h[i].iStart;.   
2ef24 20 7d 0a 20 20 20 20 69 66 28 20 69 3e 30 20 26   }.    if( i>0 &
2ef25 26 20 61 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53  & aMatch[i-1].iS
2ef26 74 61 72 74 2b 61 4d 61 74 63 68 5b 69 2d 31 5d  tart+aMatch[i-1]
2ef27 2e 6e 42 79 74 65 3e 3d 69 42 72 65 61 6b 20 29  .nByte>=iBreak )
2ef28 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 61  {.      return a
2ef29 4d 61 74 63 68 5b 69 2d 31 5d 2e 69 53 74 61 72  Match[i-1].iStar
2ef2a 74 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 66  t;.    }.  }.  f
2ef2b 6f 72 28 69 3d 31 3b 20 69 3c 3d 31 30 3b 20 69  or(i=1; i<=10; i
2ef2c 2b 2b 29 7b 0a 20 20 20 20 69 66 28 20 73 61 66  ++){.    if( saf
2ef2d 65 5f 69 73 73 70 61 63 65 28 7a 44 6f 63 5b 69  e_isspace(zDoc[i
2ef2e 42 72 65 61 6b 2d 69 5d 29 20 29 7b 0a 20 20 20  Break-i]) ){.   
2ef2f 20 20 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b     return iBreak
2ef30 20 2d 20 69 20 2b 20 31 3b 0a 20 20 20 20 7d 0a   - i + 1;.    }.
2ef31 20 20 20 20 69 66 28 20 73 61 66 65 5f 69 73 73      if( safe_iss
2ef32 70 61 63 65 28 7a 44 6f 63 5b 69 42 72 65 61 6b  pace(zDoc[iBreak
2ef33 2b 69 5d 29 20 29 7b 0a 20 20 20 20 20 20 72 65  +i]) ){.      re
2ef34 74 75 72 6e 20 69 42 72 65 61 6b 20 2b 20 69 20  turn iBreak + i 
2ef35 2b 20 31 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  + 1;.    }.  }. 
2ef36 20 72 65 74 75 72 6e 20 69 42 72 65 61 6b 3b 0a   return iBreak;.
2ef37 7d 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 77  }..../*.** Allow
2ef38 65 64 20 76 61 6c 75 65 73 20 66 6f 72 20 53 6e  ed values for Sn
2ef39 69 70 70 65 74 2e 61 4d 61 74 63 68 5b 5d 2e 73  ippet.aMatch[].s
2ef3a 6e 53 74 61 74 75 73 0a 2a 2f 0a 23 64 65 66 69  nStatus.*/.#defi
2ef3b 6e 65 20 53 4e 49 50 50 45 54 5f 49 47 4e 4f 52  ne SNIPPET_IGNOR
2ef3c 45 20 20 30 20 20 20 2f 2a 20 49 74 20 69 73 20  E  0   /* It is 
2ef3d 6f 6b 20 74 6f 20 6f 6d 69 74 20 74 68 69 73 20  ok to omit this 
2ef3e 6d 61 74 63 68 20 66 72 6f 6d 20 74 68 65 20 73  match from the s
2ef3f 6e 69 70 70 65 74 20 2a 2f 0a 23 64 65 66 69 6e  nippet */.#defin
2ef40 65 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45  e SNIPPET_DESIRE
2ef41 44 20 31 20 20 20 2f 2a 20 57 65 20 77 61 6e 74  D 1   /* We want
2ef42 20 74 6f 20 69 6e 63 6c 75 64 65 20 74 68 69 73   to include this
2ef43 20 6d 61 74 63 68 20 69 6e 20 74 68 65 20 73 6e   match in the sn
2ef44 69 70 70 65 74 20 2a 2f 0a 0a 2f 2a 0a 2a 2a 20  ippet */../*.** 
2ef45 47 65 6e 65 72 61 74 65 20 74 68 65 20 74 65 78  Generate the tex
2ef46 74 20 6f 66 20 61 20 73 6e 69 70 70 65 74 2e 0a  t of a snippet..
2ef47 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73  */.static void s
2ef48 6e 69 70 70 65 74 54 65 78 74 28 0a 20 20 66 75  nippetText(.  fu
2ef49 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 70  lltext_cursor *p
2ef4a 43 75 72 73 6f 72 2c 20 20 20 2f 2a 20 54 68 65  Cursor,   /* The
2ef4b 20 63 75 72 73 6f 72 20 77 65 20 6e 65 65 64 20   cursor we need 
2ef4c 74 68 65 20 73 6e 69 70 70 65 74 20 66 6f 72 20  the snippet for 
2ef4d 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
2ef4e 2a 7a 53 74 61 72 74 4d 61 72 6b 2c 20 20 20 20  *zStartMark,    
2ef4f 20 2f 2a 20 4d 61 72 6b 75 70 20 74 6f 20 61 70   /* Markup to ap
2ef50 70 65 61 72 20 62 65 66 6f 72 65 20 65 61 63 68  pear before each
2ef51 20 6d 61 74 63 68 20 2a 2f 0a 20 20 63 6f 6e 73   match */.  cons
2ef52 74 20 63 68 61 72 20 2a 7a 45 6e 64 4d 61 72 6b  t char *zEndMark
2ef53 2c 20 20 20 20 20 20 20 2f 2a 20 4d 61 72 6b 75  ,       /* Marku
2ef54 70 20 74 6f 20 61 70 70 65 61 72 20 61 66 74 65  p to appear afte
2ef55 72 20 65 61 63 68 20 6d 61 74 63 68 20 2a 2f 0a  r each match */.
2ef56 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 45    const char *zE
2ef57 6c 6c 69 70 73 69 73 20 20 20 20 20 20 20 2f 2a  llipsis       /*
2ef58 20 45 6c 6c 69 70 73 69 73 20 6d 61 72 6b 20 2a   Ellipsis mark *
2ef59 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 3b  /.){.  int i, j;
2ef5a 0a 20 20 73 74 72 75 63 74 20 73 6e 69 70 70 65  .  struct snippe
2ef5b 74 4d 61 74 63 68 20 2a 61 4d 61 74 63 68 3b 0a  tMatch *aMatch;.
2ef5c 20 20 69 6e 74 20 6e 4d 61 74 63 68 3b 0a 20 20    int nMatch;.  
2ef5d 69 6e 74 20 6e 44 65 73 69 72 65 64 3b 0a 20 20  int nDesired;.  
2ef5e 53 74 72 69 6e 67 42 75 66 66 65 72 20 73 62 3b  StringBuffer sb;
2ef5f 0a 20 20 69 6e 74 20 74 61 69 6c 43 6f 6c 3b 0a  .  int tailCol;.
2ef60 20 20 69 6e 74 20 74 61 69 6c 4f 66 66 73 65 74    int tailOffset
2ef61 3b 0a 20 20 69 6e 74 20 69 43 6f 6c 3b 0a 20 20  ;.  int iCol;.  
2ef62 69 6e 74 20 6e 44 6f 63 3b 0a 20 20 63 6f 6e 73  int nDoc;.  cons
2ef63 74 20 63 68 61 72 20 2a 7a 44 6f 63 3b 0a 20 20  t char *zDoc;.  
2ef64 69 6e 74 20 69 53 74 61 72 74 2c 20 69 45 6e 64  int iStart, iEnd
2ef65 3b 0a 20 20 69 6e 74 20 74 61 69 6c 45 6c 6c 69  ;.  int tailElli
2ef66 70 73 69 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20  psis = 0;.  int 
2ef67 69 4d 61 74 63 68 3b 0a 20 20 0a 0a 20 20 73 71  iMatch;.  ..  sq
2ef68 6c 69 74 65 33 5f 66 72 65 65 28 70 43 75 72 73  lite3_free(pCurs
2ef69 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69  or->snippet.zSni
2ef6a 70 70 65 74 29 3b 0a 20 20 70 43 75 72 73 6f 72  ppet);.  pCursor
2ef6b 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e 69 70 70  ->snippet.zSnipp
2ef6c 65 74 20 3d 20 30 3b 0a 20 20 61 4d 61 74 63 68  et = 0;.  aMatch
2ef6d 20 3d 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70   = pCursor->snip
2ef6e 70 65 74 2e 61 4d 61 74 63 68 3b 0a 20 20 6e 4d  pet.aMatch;.  nM
2ef6f 61 74 63 68 20 3d 20 70 43 75 72 73 6f 72 2d 3e  atch = pCursor->
2ef70 73 6e 69 70 70 65 74 2e 6e 4d 61 74 63 68 3b 0a  snippet.nMatch;.
2ef71 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66    initStringBuff
2ef72 65 72 28 26 73 62 29 3b 0a 0a 20 20 66 6f 72 28  er(&sb);..  for(
2ef73 69 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 3b 20 69  i=0; i<nMatch; i
2ef74 2b 2b 29 7b 0a 20 20 20 20 61 4d 61 74 63 68 5b  ++){.    aMatch[
2ef75 69 5d 2e 73 6e 53 74 61 74 75 73 20 3d 20 53 4e  i].snStatus = SN
2ef76 49 50 50 45 54 5f 49 47 4e 4f 52 45 3b 0a 20 20  IPPET_IGNORE;.  
2ef77 7d 0a 20 20 6e 44 65 73 69 72 65 64 20 3d 20 30  }.  nDesired = 0
2ef78 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70  ;.  for(i=0; i<p
2ef79 43 75 72 73 6f 72 2d 3e 71 2e 6e 54 65 72 6d 73  Cursor->q.nTerms
2ef7a 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 66 6f 72 28  ; i++){.    for(
2ef7b 6a 3d 30 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a  j=0; j<nMatch; j
2ef7c 2b 2b 29 7b 0a 20 20 20 20 20 20 69 66 28 20 61  ++){.      if( a
2ef7d 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d 3d  Match[j].iTerm==
2ef7e 69 20 29 7b 0a 20 20 20 20 20 20 20 20 61 4d 61  i ){.        aMa
2ef7f 74 63 68 5b 6a 5d 2e 73 6e 53 74 61 74 75 73 20  tch[j].snStatus 
2ef80 3d 20 53 4e 49 50 50 45 54 5f 44 45 53 49 52 45  = SNIPPET_DESIRE
2ef81 44 3b 0a 20 20 20 20 20 20 20 20 6e 44 65 73 69  D;.        nDesi
2ef82 72 65 64 2b 2b 3b 0a 20 20 20 20 20 20 20 20 62  red++;.        b
2ef83 72 65 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20  reak;.      }.  
2ef84 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 4d 61 74 63    }.  }..  iMatc
2ef85 68 20 3d 20 30 3b 0a 20 20 74 61 69 6c 43 6f 6c  h = 0;.  tailCol
2ef86 20 3d 20 2d 31 3b 0a 20 20 74 61 69 6c 4f 66 66   = -1;.  tailOff
2ef87 73 65 74 20 3d 20 30 3b 0a 20 20 66 6f 72 28 69  set = 0;.  for(i
2ef88 3d 30 3b 20 69 3c 6e 4d 61 74 63 68 20 26 26 20  =0; i<nMatch && 
2ef89 6e 44 65 73 69 72 65 64 3e 30 3b 20 69 2b 2b 29  nDesired>0; i++)
2ef8a 7b 0a 20 20 20 20 69 66 28 20 61 4d 61 74 63 68  {.    if( aMatch
2ef8b 5b 69 5d 2e 73 6e 53 74 61 74 75 73 21 3d 53 4e  [i].snStatus!=SN
2ef8c 49 50 50 45 54 5f 44 45 53 49 52 45 44 20 29 20  IPPET_DESIRED ) 
2ef8d 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 6e 44  continue;.    nD
2ef8e 65 73 69 72 65 64 2d 2d 3b 0a 20 20 20 20 69 43  esired--;.    iC
2ef8f 6f 6c 20 3d 20 61 4d 61 74 63 68 5b 69 5d 2e 69  ol = aMatch[i].i
2ef90 43 6f 6c 3b 0a 20 20 20 20 7a 44 6f 63 20 3d 20  Col;.    zDoc = 
2ef91 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 73 71 6c  (const char*)sql
2ef92 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74  ite3_column_text
2ef93 28 70 43 75 72 73 6f 72 2d 3e 70 53 74 6d 74 2c  (pCursor->pStmt,
2ef94 20 69 43 6f 6c 2b 31 29 3b 0a 20 20 20 20 6e 44   iCol+1);.    nD
2ef95 6f 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  oc = sqlite3_col
2ef96 75 6d 6e 5f 62 79 74 65 73 28 70 43 75 72 73 6f  umn_bytes(pCurso
2ef97 72 2d 3e 70 53 74 6d 74 2c 20 69 43 6f 6c 2b 31  r->pStmt, iCol+1
2ef98 29 3b 0a 20 20 20 20 69 53 74 61 72 74 20 3d 20  );.    iStart = 
2ef99 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
2ef9a 20 2d 20 34 30 3b 0a 20 20 20 20 69 53 74 61 72   - 40;.    iStar
2ef9b 74 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61 72 79  t = wordBoundary
2ef9c 28 69 53 74 61 72 74 2c 20 7a 44 6f 63 2c 20 6e  (iStart, zDoc, n
2ef9d 44 6f 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61  Doc, aMatch, nMa
2ef9e 74 63 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20  tch, iCol);.    
2ef9f 69 66 28 20 69 53 74 61 72 74 3c 3d 31 30 20 29  if( iStart<=10 )
2efa0 7b 0a 20 20 20 20 20 20 69 53 74 61 72 74 20 3d  {.      iStart =
2efa1 20 30 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66   0;.    }.    if
2efa2 28 20 69 43 6f 6c 3d 3d 74 61 69 6c 43 6f 6c 20  ( iCol==tailCol 
2efa3 26 26 20 69 53 74 61 72 74 3c 3d 74 61 69 6c 4f  && iStart<=tailO
2efa4 66 66 73 65 74 2b 32 30 20 29 7b 0a 20 20 20 20  ffset+20 ){.    
2efa5 20 20 69 53 74 61 72 74 20 3d 20 74 61 69 6c 4f    iStart = tailO
2efa6 66 66 73 65 74 3b 0a 20 20 20 20 7d 0a 20 20 20  ffset;.    }.   
2efa7 20 69 66 28 20 28 69 43 6f 6c 21 3d 74 61 69 6c   if( (iCol!=tail
2efa8 43 6f 6c 20 26 26 20 74 61 69 6c 43 6f 6c 3e 3d  Col && tailCol>=
2efa9 30 29 20 7c 7c 20 69 53 74 61 72 74 21 3d 74 61  0) || iStart!=ta
2efaa 69 6c 4f 66 66 73 65 74 20 29 7b 0a 20 20 20 20  ilOffset ){.    
2efab 20 20 74 72 69 6d 57 68 69 74 65 53 70 61 63 65    trimWhiteSpace
2efac 28 26 73 62 29 3b 0a 20 20 20 20 20 20 61 70 70  (&sb);.      app
2efad 65 6e 64 57 68 69 74 65 53 70 61 63 65 28 26 73  endWhiteSpace(&s
2efae 62 29 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64  b);.      append
2efaf 28 26 73 62 2c 20 7a 45 6c 6c 69 70 73 69 73 29  (&sb, zEllipsis)
2efb0 3b 0a 20 20 20 20 20 20 61 70 70 65 6e 64 57 68  ;.      appendWh
2efb1 69 74 65 53 70 61 63 65 28 26 73 62 29 3b 0a 20  iteSpace(&sb);. 
2efb2 20 20 20 7d 0a 20 20 20 20 69 45 6e 64 20 3d 20     }.    iEnd = 
2efb3 61 4d 61 74 63 68 5b 69 5d 2e 69 53 74 61 72 74  aMatch[i].iStart
2efb4 20 2b 20 61 4d 61 74 63 68 5b 69 5d 2e 6e 42 79   + aMatch[i].nBy
2efb5 74 65 20 2b 20 34 30 3b 0a 20 20 20 20 69 45 6e  te + 40;.    iEn
2efb6 64 20 3d 20 77 6f 72 64 42 6f 75 6e 64 61 72 79  d = wordBoundary
2efb7 28 69 45 6e 64 2c 20 7a 44 6f 63 2c 20 6e 44 6f  (iEnd, zDoc, nDo
2efb8 63 2c 20 61 4d 61 74 63 68 2c 20 6e 4d 61 74 63  c, aMatch, nMatc
2efb9 68 2c 20 69 43 6f 6c 29 3b 0a 20 20 20 20 69 66  h, iCol);.    if
2efba 28 20 69 45 6e 64 3e 3d 6e 44 6f 63 2d 31 30 20  ( iEnd>=nDoc-10 
2efbb 29 7b 0a 20 20 20 20 20 20 69 45 6e 64 20 3d 20  ){.      iEnd = 
2efbc 6e 44 6f 63 3b 0a 20 20 20 20 20 20 74 61 69 6c  nDoc;.      tail
2efbd 45 6c 6c 69 70 73 69 73 20 3d 20 30 3b 0a 20 20  Ellipsis = 0;.  
2efbe 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 74    }else{.      t
2efbf 61 69 6c 45 6c 6c 69 70 73 69 73 20 3d 20 31 3b  ailEllipsis = 1;
2efc0 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69 6c 65  .    }.    while
2efc1 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20  ( iMatch<nMatch 
2efc2 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
2efc3 5d 2e 69 43 6f 6c 3c 69 43 6f 6c 20 29 7b 20 69  ].iCol<iCol ){ i
2efc4 4d 61 74 63 68 2b 2b 3b 20 7d 0a 20 20 20 20 77  Match++; }.    w
2efc5 68 69 6c 65 28 20 69 53 74 61 72 74 3c 69 45 6e  hile( iStart<iEn
2efc6 64 20 29 7b 0a 20 20 20 20 20 20 77 68 69 6c 65  d ){.      while
2efc7 28 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20  ( iMatch<nMatch 
2efc8 26 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68  && aMatch[iMatch
2efc9 5d 2e 69 53 74 61 72 74 3c 69 53 74 61 72 74 0a  ].iStart<iStart.
2efca 20 20 20 20 20 20 20 20 20 20 20 20 20 26 26 20               && 
2efcb 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
2efcc 43 6f 6c 3c 3d 69 43 6f 6c 20 29 7b 0a 20 20 20  Col<=iCol ){.   
2efcd 20 20 20 20 20 69 4d 61 74 63 68 2b 2b 3b 0a 20       iMatch++;. 
2efce 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66 28       }.      if(
2efcf 20 69 4d 61 74 63 68 3c 6e 4d 61 74 63 68 20 26   iMatch<nMatch &
2efd0 26 20 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d  & aMatch[iMatch]
2efd1 2e 69 53 74 61 72 74 3c 69 45 6e 64 0a 20 20 20  .iStart<iEnd.   
2efd2 20 20 20 20 20 20 20 20 20 20 26 26 20 61 4d 61            && aMa
2efd3 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 43 6f 6c  tch[iMatch].iCol
2efd4 3d 3d 69 43 6f 6c 20 29 7b 0a 20 20 20 20 20 20  ==iCol ){.      
2efd5 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20 26    nappend(&sb, &
2efd6 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61 4d  zDoc[iStart], aM
2efd7 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69 53 74  atch[iMatch].iSt
2efd8 61 72 74 20 2d 20 69 53 74 61 72 74 29 3b 0a 20  art - iStart);. 
2efd9 20 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20         iStart = 
2efda 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e 69  aMatch[iMatch].i
2efdb 53 74 61 72 74 3b 0a 20 20 20 20 20 20 20 20 61  Start;.        a
2efdc 70 70 65 6e 64 28 26 73 62 2c 20 7a 53 74 61 72  ppend(&sb, zStar
2efdd 74 4d 61 72 6b 29 3b 0a 20 20 20 20 20 20 20 20  tMark);.        
2efde 6e 61 70 70 65 6e 64 28 26 73 62 2c 20 26 7a 44  nappend(&sb, &zD
2efdf 6f 63 5b 69 53 74 61 72 74 5d 2c 20 61 4d 61 74  oc[iStart], aMat
2efe0 63 68 5b 69 4d 61 74 63 68 5d 2e 6e 42 79 74 65  ch[iMatch].nByte
2efe1 29 3b 0a 20 20 20 20 20 20 20 20 61 70 70 65 6e  );.        appen
2efe2 64 28 26 73 62 2c 20 7a 45 6e 64 4d 61 72 6b 29  d(&sb, zEndMark)
2efe3 3b 0a 20 20 20 20 20 20 20 20 69 53 74 61 72 74  ;.        iStart
2efe4 20 2b 3d 20 61 4d 61 74 63 68 5b 69 4d 61 74 63   += aMatch[iMatc
2efe5 68 5d 2e 6e 42 79 74 65 3b 0a 20 20 20 20 20 20  h].nByte;.      
2efe6 20 20 66 6f 72 28 6a 3d 69 4d 61 74 63 68 2b 31    for(j=iMatch+1
2efe7 3b 20 6a 3c 6e 4d 61 74 63 68 3b 20 6a 2b 2b 29  ; j<nMatch; j++)
2efe8 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28 20  {.          if( 
2efe9 61 4d 61 74 63 68 5b 6a 5d 2e 69 54 65 72 6d 3d  aMatch[j].iTerm=
2efea 3d 61 4d 61 74 63 68 5b 69 4d 61 74 63 68 5d 2e  =aMatch[iMatch].
2efeb 69 54 65 72 6d 0a 20 20 20 20 20 20 20 20 20 20  iTerm.          
2efec 20 20 20 20 26 26 20 61 4d 61 74 63 68 5b 6a 5d      && aMatch[j]
2efed 2e 73 6e 53 74 61 74 75 73 3d 3d 53 4e 49 50 50  .snStatus==SNIPP
2efee 45 54 5f 44 45 53 49 52 45 44 20 29 7b 0a 20 20  ET_DESIRED ){.  
2efef 20 20 20 20 20 20 20 20 20 20 6e 44 65 73 69 72            nDesir
2eff0 65 64 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20 20  ed--;.          
2eff1 20 20 61 4d 61 74 63 68 5b 6a 5d 2e 73 6e 53 74    aMatch[j].snSt
2eff2 61 74 75 73 20 3d 20 53 4e 49 50 50 45 54 5f 49  atus = SNIPPET_I
2eff3 47 4e 4f 52 45 3b 0a 20 20 20 20 20 20 20 20 20  GNORE;.         
2eff4 20 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20   }.        }.   
2eff5 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
2eff6 20 20 6e 61 70 70 65 6e 64 28 26 73 62 2c 20 26    nappend(&sb, &
2eff7 7a 44 6f 63 5b 69 53 74 61 72 74 5d 2c 20 69 45  zDoc[iStart], iE
2eff8 6e 64 20 2d 20 69 53 74 61 72 74 29 3b 0a 20 20  nd - iStart);.  
2eff9 20 20 20 20 20 20 69 53 74 61 72 74 20 3d 20 69        iStart = i
2effa 45 6e 64 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  End;.      }.   
2effb 20 7d 0a 20 20 20 20 74 61 69 6c 43 6f 6c 20 3d   }.    tailCol =
2effc 20 69 43 6f 6c 3b 0a 20 20 20 20 74 61 69 6c 4f   iCol;.    tailO
2effd 66 66 73 65 74 20 3d 20 69 45 6e 64 3b 0a 20 20  ffset = iEnd;.  
2effe 7d 0a 20 20 74 72 69 6d 57 68 69 74 65 53 70 61  }.  trimWhiteSpa
2efff 63 65 28 26 73 62 29 3b 0a 20 20 69 66 28 20 74  ce(&sb);.  if( t
2f000 61 69 6c 45 6c 6c 69 70 73 69 73 20 29 7b 0a 20  ailEllipsis ){. 
2f001 20 20 20 61 70 70 65 6e 64 57 68 69 74 65 53 70     appendWhiteSp
2f002 61 63 65 28 26 73 62 29 3b 0a 20 20 20 20 61 70  ace(&sb);.    ap
2f003 70 65 6e 64 28 26 73 62 2c 20 7a 45 6c 6c 69 70  pend(&sb, zEllip
2f004 73 69 73 29 3b 0a 20 20 7d 0a 20 20 70 43 75 72  sis);.  }.  pCur
2f005 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 53 6e  sor->snippet.zSn
2f006 69 70 70 65 74 20 3d 20 73 74 72 69 6e 67 42 75  ippet = stringBu
2f007 66 66 65 72 44 61 74 61 28 26 73 62 29 3b 0a 20  fferData(&sb);. 
2f008 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65   pCursor->snippe
2f009 74 2e 6e 53 6e 69 70 70 65 74 20 3d 20 73 74 72  t.nSnippet = str
2f00a 69 6e 67 42 75 66 66 65 72 4c 65 6e 67 74 68 28  ingBufferLength(
2f00b 26 73 62 29 3b 0a 7d 0a 0a 0a 2f 2a 0a 2a 2a 20  &sb);.}.../*.** 
2f00c 43 6c 6f 73 65 20 74 68 65 20 63 75 72 73 6f 72  Close the cursor
2f00d 2e 20 20 46 6f 72 20 61 64 64 69 74 69 6f 6e 61  .  For additiona
2f00e 6c 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 20 73 65  l information se
2f00f 65 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 61 74  e the documentat
2f010 69 6f 6e 0a 2a 2a 20 6f 6e 20 74 68 65 20 78 43  ion.** on the xC
2f011 6c 6f 73 65 20 6d 65 74 68 6f 64 20 6f 66 20 74  lose method of t
2f012 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2f013 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f 0a 73   interface..*/.s
2f014 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65  tatic int fullte
2f015 78 74 43 6c 6f 73 65 28 73 71 6c 69 74 65 33 5f  xtClose(sqlite3_
2f016 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
2f017 72 73 6f 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78  rsor){.  fulltex
2f018 74 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66  t_cursor *c = (f
2f019 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a  ulltext_cursor *
2f01a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 46 54 53  ) pCursor;.  FTS
2f01b 54 52 41 43 45 28 28 22 46 54 53 33 20 43 6c 6f  TRACE(("FTS3 Clo
2f01c 73 65 20 25 70 5c 6e 22 2c 20 63 29 29 3b 0a 20  se %p\n", c));. 
2f01d 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
2f01e 65 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 71  e(c->pStmt);.  q
2f01f 75 65 72 79 43 6c 65 61 72 28 26 63 2d 3e 71 29  ueryClear(&c->q)
2f020 3b 0a 20 20 73 6e 69 70 70 65 74 43 6c 65 61 72  ;.  snippetClear
2f021 28 26 63 2d 3e 73 6e 69 70 70 65 74 29 3b 0a 20  (&c->snippet);. 
2f022 20 69 66 28 20 63 2d 3e 72 65 73 75 6c 74 2e 6e   if( c->result.n
2f023 44 61 74 61 21 3d 30 20 29 20 64 6c 72 44 65 73  Data!=0 ) dlrDes
2f024 74 72 6f 79 28 26 63 2d 3e 72 65 61 64 65 72 29  troy(&c->reader)
2f025 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  ;.  dataBufferDe
2f026 73 74 72 6f 79 28 26 63 2d 3e 72 65 73 75 6c 74  stroy(&c->result
2f027 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
2f028 65 28 63 29 3b 0a 20 20 72 65 74 75 72 6e 20 53  e(c);.  return S
2f029 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 73 74 61  QLITE_OK;.}..sta
2f02a 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74  tic int fulltext
2f02b 4e 65 78 74 28 73 71 6c 69 74 65 33 5f 76 74 61  Next(sqlite3_vta
2f02c 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f  b_cursor *pCurso
2f02d 72 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63  r){.  fulltext_c
2f02e 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c 6c  ursor *c = (full
2f02f 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20 70  text_cursor *) p
2f030 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74 20 72 63  Cursor;.  int rc
2f031 3b 0a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  ;..  FTSTRACE(("
2f032 46 54 53 33 20 4e 65 78 74 20 25 70 5c 6e 22 2c  FTS3 Next %p\n",
2f033 20 70 43 75 72 73 6f 72 29 29 3b 0a 20 20 73 6e   pCursor));.  sn
2f034 69 70 70 65 74 43 6c 65 61 72 28 26 63 2d 3e 73  ippetClear(&c->s
2f035 6e 69 70 70 65 74 29 3b 0a 20 20 69 66 28 20 63  nippet);.  if( c
2f036 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20 3c 20  ->iCursorType < 
2f037 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 20 29  QUERY_FULLTEXT )
2f038 7b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68  {.    /* TODO(sh
2f039 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51 4c 49  ess) Handle SQLI
2f03a 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20 53 51  TE_SCHEMA AND SQ
2f03b 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20  LITE_BUSY. */.  
2f03c 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
2f03d 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20  tep(c->pStmt);. 
2f03e 20 20 20 73 77 69 74 63 68 28 20 72 63 20 29 7b     switch( rc ){
2f03f 0a 20 20 20 20 20 20 63 61 73 65 20 53 51 4c 49  .      case SQLI
2f040 54 45 5f 52 4f 57 3a 0a 20 20 20 20 20 20 20 20  TE_ROW:.        
2f041 63 2d 3e 65 6f 66 20 3d 20 30 3b 0a 20 20 20 20  c->eof = 0;.    
2f042 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
2f043 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 63 61 73 65  E_OK;.      case
2f044 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3a 0a 20 20   SQLITE_DONE:.  
2f045 20 20 20 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31        c->eof = 1
2f046 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 72 6e  ;.        return
2f047 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
2f048 20 20 64 65 66 61 75 6c 74 3a 0a 20 20 20 20 20    default:.     
2f049 20 20 20 63 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20     c->eof = 1;. 
2f04a 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63         return rc
2f04b 3b 0a 20 20 20 20 7d 0a 20 20 7d 20 65 6c 73 65  ;.    }.  } else
2f04c 20 7b 20 20 2f 2a 20 66 75 6c 6c 2d 74 65 78 74   {  /* full-text
2f04d 20 71 75 65 72 79 20 2a 2f 0a 20 20 20 20 72 63   query */.    rc
2f04e 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
2f04f 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20  (c->pStmt);.    
2f050 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2f051 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2f052 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c      if( c->resul
2f053 74 2e 6e 44 61 74 61 3d 3d 30 20 7c 7c 20 64 6c  t.nData==0 || dl
2f054 72 41 74 45 6e 64 28 26 63 2d 3e 72 65 61 64 65  rAtEnd(&c->reade
2f055 72 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 65  r) ){.      c->e
2f056 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65  of = 1;.      re
2f057 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2f058 20 20 20 20 7d 0a 20 20 20 20 72 63 20 3d 20 73      }.    rc = s
2f059 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
2f05a 34 28 63 2d 3e 70 53 74 6d 74 2c 20 31 2c 20 64  4(c->pStmt, 1, d
2f05b 6c 72 44 6f 63 69 64 28 26 63 2d 3e 72 65 61 64  lrDocid(&c->read
2f05c 65 72 29 29 3b 0a 20 20 20 20 64 6c 72 53 74 65  er));.    dlrSte
2f05d 70 28 26 63 2d 3e 72 65 61 64 65 72 29 3b 0a 20  p(&c->reader);. 
2f05e 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2f05f 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2f060 3b 0a 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68  ;.    /* TODO(sh
2f061 65 73 73 29 20 48 61 6e 64 6c 65 20 53 51 4c 49  ess) Handle SQLI
2f062 54 45 5f 53 43 48 45 4d 41 20 41 4e 44 20 53 51  TE_SCHEMA AND SQ
2f063 4c 49 54 45 5f 42 55 53 59 2e 20 2a 2f 0a 20 20  LITE_BUSY. */.  
2f064 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73    rc = sqlite3_s
2f065 74 65 70 28 63 2d 3e 70 53 74 6d 74 29 3b 0a 20  tep(c->pStmt);. 
2f066 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
2f067 45 5f 52 4f 57 20 29 7b 20 20 20 2f 2a 20 74 68  E_ROW ){   /* th
2f068 65 20 63 61 73 65 20 77 65 20 65 78 70 65 63 74  e case we expect
2f069 20 2a 2f 0a 20 20 20 20 20 20 63 2d 3e 65 6f 66   */.      c->eof
2f06a 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 65 74 75   = 0;.      retu
2f06b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20  rn SQLITE_OK;.  
2f06c 20 20 7d 0a 20 20 20 20 2f 2a 20 61 6e 20 65 72    }.    /* an er
2f06d 72 6f 72 20 6f 63 63 75 72 72 65 64 3b 20 61 62  ror occurred; ab
2f06e 6f 72 74 20 2a 2f 0a 20 20 20 20 72 65 74 75 72  ort */.    retur
2f06f 6e 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  n rc==SQLITE_DON
2f070 45 20 3f 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  E ? SQLITE_ERROR
2f071 20 3a 20 72 63 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f   : rc;.  }.}.../
2f072 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49 66  * TODO(shess) If
2f073 20 77 65 20 70 75 73 68 65 64 20 4c 65 61 66 52   we pushed LeafR
2f074 65 61 64 65 72 20 74 6f 20 74 68 65 20 74 6f 70  eader to the top
2f075 20 6f 66 20 74 68 65 20 66 69 6c 65 2c 20 6f 72   of the file, or
2f076 20 74 6f 0a 2a 2a 20 61 6e 6f 74 68 65 72 20 66   to.** another f
2f077 69 6c 65 2c 20 74 65 72 6d 5f 73 65 6c 65 63 74  ile, term_select
2f078 28 29 20 63 6f 75 6c 64 20 62 65 20 70 75 73 68  () could be push
2f079 65 64 20 61 62 6f 76 65 0a 2a 2a 20 64 6f 63 4c  ed above.** docL
2f07a 69 73 74 4f 66 54 65 72 6d 28 29 2e 0a 2a 2f 0a  istOfTerm()..*/.
2f07b 73 74 61 74 69 63 20 69 6e 74 20 74 65 72 6d 53  static int termS
2f07c 65 6c 65 63 74 28 66 75 6c 6c 74 65 78 74 5f 76  elect(fulltext_v
2f07d 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 43 6f 6c  tab *v, int iCol
2f07e 75 6d 6e 2c 0a 20 20 20 20 20 20 20 20 20 20 20  umn,.           
2f07f 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
2f080 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
2f081 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
2f082 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
2f083 20 20 20 20 20 20 20 20 20 20 20 20 20 44 6f 63               Doc
2f084 4c 69 73 74 54 79 70 65 20 69 54 79 70 65 2c 20  ListType iType, 
2f085 44 61 74 61 42 75 66 66 65 72 20 2a 6f 75 74 29  DataBuffer *out)
2f086 3b 0a 0a 2f 2a 20 52 65 74 75 72 6e 20 61 20 44  ;../* Return a D
2f087 6f 63 4c 69 73 74 20 63 6f 72 72 65 73 70 6f 6e  ocList correspon
2f088 64 69 6e 67 20 74 6f 20 74 68 65 20 71 75 65 72  ding to the quer
2f089 79 20 74 65 72 6d 20 2a 70 54 65 72 6d 2e 20 20  y term *pTerm.  
2f08a 49 66 20 2a 70 54 65 72 6d 0a 2a 2a 20 69 73 20  If *pTerm.** is 
2f08b 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 20 6f  the first term o
2f08c 66 20 61 20 70 68 72 61 73 65 20 71 75 65 72 79  f a phrase query
2f08d 2c 20 67 6f 20 61 68 65 61 64 20 61 6e 64 20 65  , go ahead and e
2f08e 76 61 6c 75 61 74 65 20 74 68 65 20 70 68 72 61  valuate the phra
2f08f 73 65 0a 2a 2a 20 71 75 65 72 79 20 61 6e 64 20  se.** query and 
2f090 72 65 74 75 72 6e 20 74 68 65 20 64 6f 63 6c 69  return the docli
2f091 73 74 20 66 6f 72 20 74 68 65 20 65 6e 74 69 72  st for the entir
2f092 65 20 70 68 72 61 73 65 20 71 75 65 72 79 2e 0a  e phrase query..
2f093 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 73 75 6c 74  **.** The result
2f094 69 6e 67 20 44 4c 5f 44 4f 43 49 44 53 20 64 6f  ing DL_DOCIDS do
2f095 63 6c 69 73 74 20 69 73 20 73 74 6f 72 65 64 20  clist is stored 
2f096 69 6e 20 70 52 65 73 75 6c 74 2c 20 77 68 69 63  in pResult, whic
2f097 68 20 69 73 0a 2a 2a 20 6f 76 65 72 77 72 69 74  h is.** overwrit
2f098 74 65 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  ten..*/.static i
2f099 6e 74 20 64 6f 63 4c 69 73 74 4f 66 54 65 72 6d  nt docListOfTerm
2f09a 28 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61  (.  fulltext_vta
2f09b 62 20 2a 76 2c 20 20 20 20 2f 2a 20 54 68 65 20  b *v,    /* The 
2f09c 66 75 6c 6c 20 74 65 78 74 20 69 6e 64 65 78 20  full text index 
2f09d 2a 2f 0a 20 20 69 6e 74 20 69 43 6f 6c 75 6d 6e  */.  int iColumn
2f09e 2c 20 20 20 20 20 20 20 20 20 2f 2a 20 63 6f 6c  ,         /* col
2f09f 75 6d 6e 20 74 6f 20 72 65 73 74 72 69 63 74 20  umn to restrict 
2f0a0 74 6f 2e 20 20 4e 6f 20 72 65 73 74 72 69 63 74  to.  No restrict
2f0a1 69 6f 6e 20 69 66 20 3e 3d 6e 43 6f 6c 75 6d 6e  ion if >=nColumn
2f0a2 20 2a 2f 0a 20 20 51 75 65 72 79 54 65 72 6d 20   */.  QueryTerm 
2f0a3 2a 70 51 54 65 72 6d 2c 20 20 20 2f 2a 20 54 65  *pQTerm,   /* Te
2f0a4 72 6d 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69 6e  rm we are lookin
2f0a5 67 20 66 6f 72 2c 20 6f 72 20 31 73 74 20 74 65  g for, or 1st te
2f0a6 72 6d 20 6f 66 20 61 20 70 68 72 61 73 65 20 2a  rm of a phrase *
2f0a7 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 2a  /.  DataBuffer *
2f0a8 70 52 65 73 75 6c 74 20 20 2f 2a 20 57 72 69 74  pResult  /* Writ
2f0a9 65 20 74 68 65 20 72 65 73 75 6c 74 20 68 65 72  e the result her
2f0aa 65 20 2a 2f 0a 29 7b 0a 20 20 44 61 74 61 42 75  e */.){.  DataBu
2f0ab 66 66 65 72 20 6c 65 66 74 2c 20 72 69 67 68 74  ffer left, right
2f0ac 2c 20 6e 65 77 3b 0a 20 20 69 6e 74 20 69 2c 20  , new;.  int i, 
2f0ad 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 20 70 68 72  rc;..  /* No phr
2f0ae 61 73 65 20 73 65 61 72 63 68 20 69 66 20 6e 6f  ase search if no
2f0af 20 70 6f 73 69 74 69 6f 6e 20 69 6e 66 6f 2e 20   position info. 
2f0b0 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 70 51 54  */.  assert( pQT
2f0b1 65 72 6d 2d 3e 6e 50 68 72 61 73 65 3d 3d 30 20  erm->nPhrase==0 
2f0b2 7c 7c 20 44 4c 5f 44 45 46 41 55 4c 54 21 3d 44  || DL_DEFAULT!=D
2f0b3 4c 5f 44 4f 43 49 44 53 20 29 3b 0a 0a 20 20 2f  L_DOCIDS );..  /
2f0b4 2a 20 54 68 69 73 20 63 6f 64 65 20 73 68 6f 75  * This code shou
2f0b5 6c 64 20 6e 65 76 65 72 20 62 65 20 63 61 6c 6c  ld never be call
2f0b6 65 64 20 77 69 74 68 20 62 75 66 66 65 72 65 64  ed with buffered
2f0b7 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a 20 20 61   updates. */.  a
2f0b8 73 73 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69  ssert( v->nPendi
2f0b9 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a 20 20 64  ngData<0 );..  d
2f0ba 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 6c  ataBufferInit(&l
2f0bb 65 66 74 2c 20 30 29 3b 0a 20 20 72 63 20 3d 20  eft, 0);.  rc = 
2f0bc 74 65 72 6d 53 65 6c 65 63 74 28 76 2c 20 69 43  termSelect(v, iC
2f0bd 6f 6c 75 6d 6e 2c 20 70 51 54 65 72 6d 2d 3e 70  olumn, pQTerm->p
2f0be 54 65 72 6d 2c 20 70 51 54 65 72 6d 2d 3e 6e 54  Term, pQTerm->nT
2f0bf 65 72 6d 2c 20 70 51 54 65 72 6d 2d 3e 69 73 50  erm, pQTerm->isP
2f0c0 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
2f0c1 20 20 20 20 20 20 20 20 20 28 30 3c 70 51 54 65           (0<pQTe
2f0c2 72 6d 2d 3e 6e 50 68 72 61 73 65 20 3f 20 44 4c  rm->nPhrase ? DL
2f0c3 5f 50 4f 53 49 54 49 4f 4e 53 20 3a 20 44 4c 5f  _POSITIONS : DL_
2f0c4 44 4f 43 49 44 53 29 2c 20 26 6c 65 66 74 29 3b  DOCIDS), &left);
2f0c5 0a 20 20 69 66 28 20 72 63 20 29 20 72 65 74 75  .  if( rc ) retu
2f0c6 72 6e 20 72 63 3b 0a 20 20 66 6f 72 28 69 3d 31  rn rc;.  for(i=1
2f0c7 3b 20 69 3c 3d 70 51 54 65 72 6d 2d 3e 6e 50 68  ; i<=pQTerm->nPh
2f0c8 72 61 73 65 20 26 26 20 6c 65 66 74 2e 6e 44 61  rase && left.nDa
2f0c9 74 61 3e 30 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ta>0; i++){.    
2f0ca 2f 2a 20 49 66 20 74 68 69 73 20 74 6f 6b 65 6e  /* If this token
2f0cb 20 69 73 20 63 6f 6e 6e 65 63 74 65 64 20 74 6f   is connected to
2f0cc 20 74 68 65 20 6e 65 78 74 20 62 79 20 61 20 4e   the next by a N
2f0cd 45 41 52 20 6f 70 65 72 61 74 6f 72 2c 20 61 6e  EAR operator, an
2f0ce 64 0a 20 20 20 20 2a 2a 20 74 68 65 20 6e 65 78  d.    ** the nex
2f0cf 74 20 74 6f 6b 65 6e 20 69 73 20 74 68 65 20 73  t token is the s
2f0d0 74 61 72 74 20 6f 66 20 61 20 70 68 72 61 73 65  tart of a phrase
2f0d1 2c 20 74 68 65 6e 20 73 65 74 20 6e 50 68 72 61  , then set nPhra
2f0d2 73 65 52 69 67 68 74 0a 20 20 20 20 2a 2a 20 74  seRight.    ** t
2f0d3 6f 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20  o the number of 
2f0d4 74 6f 6b 65 6e 73 20 69 6e 20 74 68 65 20 70 68  tokens in the ph
2f0d5 72 61 73 65 2e 20 4f 74 68 65 72 77 69 73 65 20  rase. Otherwise 
2f0d6 6c 65 61 76 65 20 69 74 20 61 74 20 31 2e 0a 20  leave it at 1.. 
2f0d7 20 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 6e 50     */.    int nP
2f0d8 68 72 61 73 65 52 69 67 68 74 20 3d 20 31 3b 0a  hraseRight = 1;.
2f0d9 20 20 20 20 77 68 69 6c 65 28 20 28 69 2b 6e 50      while( (i+nP
2f0da 68 72 61 73 65 52 69 67 68 74 29 3c 3d 70 51 54  hraseRight)<=pQT
2f0db 65 72 6d 2d 3e 6e 50 68 72 61 73 65 20 0a 20 20  erm->nPhrase .  
2f0dc 20 20 20 20 20 20 26 26 20 70 51 54 65 72 6d 5b        && pQTerm[
2f0dd 69 2b 6e 50 68 72 61 73 65 52 69 67 68 74 5d 2e  i+nPhraseRight].
2f0de 6e 4e 65 61 72 3d 3d 30 20 0a 20 20 20 20 29 7b  nNear==0 .    ){
2f0df 0a 20 20 20 20 20 20 6e 50 68 72 61 73 65 52 69  .      nPhraseRi
2f0e0 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20  ght++;.    }..  
2f0e1 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
2f0e2 28 26 72 69 67 68 74 2c 20 30 29 3b 0a 20 20 20  (&right, 0);.   
2f0e3 20 72 63 20 3d 20 74 65 72 6d 53 65 6c 65 63 74   rc = termSelect
2f0e4 28 76 2c 20 69 43 6f 6c 75 6d 6e 2c 20 70 51 54  (v, iColumn, pQT
2f0e5 65 72 6d 5b 69 5d 2e 70 54 65 72 6d 2c 20 70 51  erm[i].pTerm, pQ
2f0e6 54 65 72 6d 5b 69 5d 2e 6e 54 65 72 6d 2c 0a 20  Term[i].nTerm,. 
2f0e7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0e8 20 20 20 70 51 54 65 72 6d 5b 69 5d 2e 69 73 50     pQTerm[i].isP
2f0e9 72 65 66 69 78 2c 20 44 4c 5f 50 4f 53 49 54 49  refix, DL_POSITI
2f0ea 4f 4e 53 2c 20 26 72 69 67 68 74 29 3b 0a 20 20  ONS, &right);.  
2f0eb 20 20 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20    if( rc ){.    
2f0ec 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2f0ed 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  roy(&left);.    
2f0ee 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20    return rc;.   
2f0ef 20 7d 0a 20 20 20 20 64 61 74 61 42 75 66 66 65   }.    dataBuffe
2f0f0 72 49 6e 69 74 28 26 6e 65 77 2c 20 30 29 3b 0a  rInit(&new, 0);.
2f0f1 20 20 20 20 64 6f 63 4c 69 73 74 50 68 72 61 73      docListPhras
2f0f2 65 4d 65 72 67 65 28 6c 65 66 74 2e 70 44 61 74  eMerge(left.pDat
2f0f3 61 2c 20 6c 65 66 74 2e 6e 44 61 74 61 2c 20 72  a, left.nData, r
2f0f4 69 67 68 74 2e 70 44 61 74 61 2c 20 72 69 67 68  ight.pData, righ
2f0f5 74 2e 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20  t.nData,.       
2f0f6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f0f7 70 51 54 65 72 6d 5b 69 2d 31 5d 2e 6e 4e 65 61  pQTerm[i-1].nNea
2f0f8 72 2c 20 70 51 54 65 72 6d 5b 69 2d 31 5d 2e 69  r, pQTerm[i-1].i
2f0f9 50 68 72 61 73 65 20 2b 20 6e 50 68 72 61 73 65  Phrase + nPhrase
2f0fa 52 69 67 68 74 2c 0a 20 20 20 20 20 20 20 20 20  Right,.         
2f0fb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 28 28                ((
2f0fc 69 3c 70 51 54 65 72 6d 2d 3e 6e 50 68 72 61 73  i<pQTerm->nPhras
2f0fd 65 29 20 3f 20 44 4c 5f 50 4f 53 49 54 49 4f 4e  e) ? DL_POSITION
2f0fe 53 20 3a 20 44 4c 5f 44 4f 43 49 44 53 29 2c 0a  S : DL_DOCIDS),.
2f0ff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f100 20 20 20 20 20 20 20 26 6e 65 77 29 3b 0a 20 20         &new);.  
2f101 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2f102 72 6f 79 28 26 6c 65 66 74 29 3b 0a 20 20 20 20  roy(&left);.    
2f103 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
2f104 79 28 26 72 69 67 68 74 29 3b 0a 20 20 20 20 6c  y(&right);.    l
2f105 65 66 74 20 3d 20 6e 65 77 3b 0a 20 20 7d 0a 20  eft = new;.  }. 
2f106 20 2a 70 52 65 73 75 6c 74 20 3d 20 6c 65 66 74   *pResult = left
2f107 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2f108 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20  E_OK;.}../* Add 
2f109 61 20 6e 65 77 20 74 65 72 6d 20 70 54 65 72 6d  a new term pTerm
2f10a 5b 30 2e 2e 6e 54 65 72 6d 2d 31 5d 20 74 6f 20  [0..nTerm-1] to 
2f10b 74 68 65 20 71 75 65 72 79 20 2a 71 2e 0a 2a 2f  the query *q..*/
2f10c 0a 73 74 61 74 69 63 20 76 6f 69 64 20 71 75 65  .static void que
2f10d 72 79 41 64 64 28 51 75 65 72 79 20 2a 71 2c 20  ryAdd(Query *q, 
2f10e 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
2f10f 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20  m, int nTerm){. 
2f110 20 51 75 65 72 79 54 65 72 6d 20 2a 74 3b 0a 20   QueryTerm *t;. 
2f111 20 2b 2b 71 2d 3e 6e 54 65 72 6d 73 3b 0a 20 20   ++q->nTerms;.  
2f112 71 2d 3e 70 54 65 72 6d 73 20 3d 20 73 71 6c 69  q->pTerms = sqli
2f113 74 65 33 5f 72 65 61 6c 6c 6f 63 28 71 2d 3e 70  te3_realloc(q->p
2f114 54 65 72 6d 73 2c 20 71 2d 3e 6e 54 65 72 6d 73  Terms, q->nTerms
2f115 20 2a 20 73 69 7a 65 6f 66 28 71 2d 3e 70 54 65   * sizeof(q->pTe
2f116 72 6d 73 5b 30 5d 29 29 3b 0a 20 20 69 66 28 20  rms[0]));.  if( 
2f117 71 2d 3e 70 54 65 72 6d 73 3d 3d 30 20 29 7b 0a  q->pTerms==0 ){.
2f118 20 20 20 20 71 2d 3e 6e 54 65 72 6d 73 20 3d 20      q->nTerms = 
2f119 30 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  0;.    return;. 
2f11a 20 7d 0a 20 20 74 20 3d 20 26 71 2d 3e 70 54 65   }.  t = &q->pTe
2f11b 72 6d 73 5b 71 2d 3e 6e 54 65 72 6d 73 20 2d 20  rms[q->nTerms - 
2f11c 31 5d 3b 0a 20 20 43 4c 45 41 52 28 74 29 3b 0a  1];.  CLEAR(t);.
2f11d 20 20 74 2d 3e 70 54 65 72 6d 20 3d 20 73 71 6c    t->pTerm = sql
2f11e 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54 65 72  ite3_malloc(nTer
2f11f 6d 2b 31 29 3b 0a 20 20 6d 65 6d 63 70 79 28 74  m+1);.  memcpy(t
2f120 2d 3e 70 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20  ->pTerm, pTerm, 
2f121 6e 54 65 72 6d 29 3b 0a 20 20 74 2d 3e 70 54 65  nTerm);.  t->pTe
2f122 72 6d 5b 6e 54 65 72 6d 5d 20 3d 20 30 3b 0a 20  rm[nTerm] = 0;. 
2f123 20 74 2d 3e 6e 54 65 72 6d 20 3d 20 6e 54 65 72   t->nTerm = nTer
2f124 6d 3b 0a 20 20 74 2d 3e 69 73 4f 72 20 3d 20 71  m;.  t->isOr = q
2f125 2d 3e 6e 65 78 74 49 73 4f 72 3b 0a 20 20 74 2d  ->nextIsOr;.  t-
2f126 3e 69 73 50 72 65 66 69 78 20 3d 20 30 3b 0a 20  >isPrefix = 0;. 
2f127 20 71 2d 3e 6e 65 78 74 49 73 4f 72 20 3d 20 30   q->nextIsOr = 0
2f128 3b 0a 20 20 74 2d 3e 69 43 6f 6c 75 6d 6e 20 3d  ;.  t->iColumn =
2f129 20 71 2d 3e 6e 65 78 74 43 6f 6c 75 6d 6e 3b 0a   q->nextColumn;.
2f12a 20 20 71 2d 3e 6e 65 78 74 43 6f 6c 75 6d 6e 20    q->nextColumn 
2f12b 3d 20 71 2d 3e 64 66 6c 74 43 6f 6c 75 6d 6e 3b  = q->dfltColumn;
2f12c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 65 63 6b 20  .}../*.** Check 
2f12d 74 6f 20 73 65 65 20 69 66 20 74 68 65 20 73 74  to see if the st
2f12e 72 69 6e 67 20 7a 54 6f 6b 65 6e 5b 30 2e 2e 2e  ring zToken[0...
2f12f 6e 54 6f 6b 65 6e 2d 31 5d 20 6d 61 74 63 68 65  nToken-1] matche
2f130 73 20 61 6e 79 0a 2a 2a 20 63 6f 6c 75 6d 6e 20  s any.** column 
2f131 6e 61 6d 65 20 69 6e 20 74 68 65 20 76 69 72 74  name in the virt
2f132 75 61 6c 20 74 61 62 6c 65 2e 20 20 20 49 66 20  ual table.   If 
2f133 69 74 20 64 6f 65 73 2c 0a 2a 2a 20 72 65 74 75  it does,.** retu
2f134 72 6e 20 74 68 65 20 7a 65 72 6f 2d 69 6e 64 65  rn the zero-inde
2f135 78 65 64 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  xed column numbe
2f136 72 2e 20 20 49 66 20 6e 6f 74 2c 20 72 65 74 75  r.  If not, retu
2f137 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61 74 69 63  rn -1..*/.static
2f138 20 69 6e 74 20 63 68 65 63 6b 43 6f 6c 75 6d 6e   int checkColumn
2f139 53 70 65 63 69 66 69 65 72 28 0a 20 20 66 75 6c  Specifier(.  ful
2f13a 6c 74 65 78 74 5f 76 74 61 62 20 2a 70 56 74 61  ltext_vtab *pVta
2f13b 62 2c 20 20 20 20 2f 2a 20 54 68 65 20 76 69 72  b,    /* The vir
2f13c 74 75 61 6c 20 74 61 62 6c 65 20 2a 2f 0a 20 20  tual table */.  
2f13d 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 54 6f 6b  const char *zTok
2f13e 65 6e 2c 20 20 20 20 20 20 2f 2a 20 54 65 78 74  en,      /* Text
2f13f 20 6f 66 20 74 68 65 20 74 6f 6b 65 6e 20 2a 2f   of the token */
2f140 0a 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 20 20 20  .  int nToken   
2f141 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e              /* N
2f142 75 6d 62 65 72 20 6f 66 20 63 68 61 72 61 63 74  umber of charact
2f143 65 72 73 20 69 6e 20 74 68 65 20 74 6f 6b 65 6e  ers in the token
2f144 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 3b 0a   */.){.  int i;.
2f145 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 70 56 74    for(i=0; i<pVt
2f146 61 62 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 69 2b 2b  ab->nColumn; i++
2f147 29 7b 0a 20 20 20 20 69 66 28 20 6d 65 6d 63 6d  ){.    if( memcm
2f148 70 28 70 56 74 61 62 2d 3e 61 7a 43 6f 6c 75 6d  p(pVtab->azColum
2f149 6e 5b 69 5d 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54  n[i], zToken, nT
2f14a 6f 6b 65 6e 29 3d 3d 30 0a 20 20 20 20 20 20 20  oken)==0.       
2f14b 20 26 26 20 70 56 74 61 62 2d 3e 61 7a 43 6f 6c   && pVtab->azCol
2f14c 75 6d 6e 5b 69 5d 5b 6e 54 6f 6b 65 6e 5d 3d 3d  umn[i][nToken]==
2f14d 30 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  0 ){.      retur
2f14e 6e 20 69 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n i;.    }.  }. 
2f14f 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a 2f   return -1;.}../
2f150 2a 0a 2a 2a 20 50 61 72 73 65 20 74 68 65 20 74  *.** Parse the t
2f151 65 78 74 20 61 74 20 7a 53 65 67 6d 65 6e 74 5b  ext at zSegment[
2f152 30 2e 2e 6e 53 65 67 6d 65 6e 74 2d 31 5d 2e 20  0..nSegment-1]. 
2f153 20 41 64 64 20 61 64 64 69 74 69 6f 6e 61 6c 20   Add additional 
2f154 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 74 68 65 20  terms.** to the 
2f155 71 75 65 72 79 20 62 65 69 6e 67 20 61 73 73 65  query being asse
2f156 6d 62 6c 69 65 64 20 69 6e 20 70 51 75 65 72 79  mblied in pQuery
2f157 2e 0a 2a 2a 0a 2a 2a 20 69 6e 50 68 72 61 73 65  ..**.** inPhrase
2f158 20 69 73 20 74 72 75 65 20 69 66 20 7a 53 65 67   is true if zSeg
2f159 6d 65 6e 74 5b 30 2e 2e 6e 53 65 67 65 6d 65 6e  ment[0..nSegemen
2f15a 74 2d 31 5d 20 69 73 20 63 6f 6e 74 61 69 6e 65  t-1] is containe
2f15b 64 20 77 69 74 68 69 6e 0a 2a 2a 20 64 6f 75 62  d within.** doub
2f15c 6c 65 2d 71 75 6f 74 65 73 2e 20 20 49 66 20 69  le-quotes.  If i
2f15d 6e 50 68 72 61 73 65 20 69 73 20 74 72 75 65 2c  nPhrase is true,
2f15e 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74 20   then the first 
2f15f 74 65 72 6d 0a 2a 2a 20 69 73 20 6d 61 72 6b 65  term.** is marke
2f160 64 20 77 69 74 68 20 74 68 65 20 6e 75 6d 62 65  d with the numbe
2f161 72 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 74 68  r of terms in th
2f162 65 20 70 68 72 61 73 65 20 6c 65 73 73 20 6f 6e  e phrase less on
2f163 65 20 61 6e 64 0a 2a 2a 20 4f 52 20 61 6e 64 20  e and.** OR and 
2f164 22 2d 22 20 73 79 6e 74 61 78 20 69 73 20 69 67  "-" syntax is ig
2f165 6e 6f 72 65 64 2e 20 20 49 66 20 69 6e 50 68 72  nored.  If inPhr
2f166 61 73 65 20 69 73 20 66 61 6c 73 65 2c 20 74 68  ase is false, th
2f167 65 6e 20 65 76 65 72 79 0a 2a 2a 20 74 65 72 6d  en every.** term
2f168 20 66 6f 75 6e 64 20 69 73 20 6d 61 72 6b 65 64   found is marked
2f169 20 77 69 74 68 20 6e 50 68 72 61 73 65 3d 30 20   with nPhrase=0 
2f16a 61 6e 64 20 4f 52 20 61 6e 64 20 22 2d 22 20 73  and OR and "-" s
2f16b 79 6e 74 61 78 20 69 73 20 73 69 67 6e 69 66 69  yntax is signifi
2f16c 63 61 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  cant..*/.static 
2f16d 69 6e 74 20 74 6f 6b 65 6e 69 7a 65 53 65 67 6d  int tokenizeSegm
2f16e 65 6e 74 28 0a 20 20 73 71 6c 69 74 65 33 5f 74  ent(.  sqlite3_t
2f16f 6f 6b 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e  okenizer *pToken
2f170 69 7a 65 72 2c 20 20 20 20 20 20 20 20 20 20 2f  izer,          /
2f171 2a 20 54 68 65 20 74 6f 6b 65 6e 69 7a 65 72 20  * The tokenizer 
2f172 74 6f 20 75 73 65 20 2a 2f 0a 20 20 63 6f 6e 73  to use */.  cons
2f173 74 20 63 68 61 72 20 2a 7a 53 65 67 6d 65 6e 74  t char *zSegment
2f174 2c 20 69 6e 74 20 6e 53 65 67 6d 65 6e 74 2c 20  , int nSegment, 
2f175 20 20 20 20 2f 2a 20 51 75 65 72 79 20 65 78 70      /* Query exp
2f176 72 65 73 73 69 6f 6e 20 62 65 69 6e 67 20 70 61  ression being pa
2f177 72 73 65 64 20 2a 2f 0a 20 20 69 6e 74 20 69 6e  rsed */.  int in
2f178 50 68 72 61 73 65 2c 20 20 20 20 20 20 20 20 20  Phrase,         
2f179 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f17a 20 20 2f 2a 20 54 72 75 65 20 69 66 20 77 69 74    /* True if wit
2f17b 68 69 6e 20 22 2e 2e 2e 22 20 2a 2f 0a 20 20 51  hin "..." */.  Q
2f17c 75 65 72 79 20 2a 70 51 75 65 72 79 20 20 20 20  uery *pQuery    
2f17d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f17e 20 20 20 20 20 20 20 2f 2a 20 41 70 70 65 6e 64         /* Append
2f17f 20 72 65 73 75 6c 74 73 20 68 65 72 65 20 2a 2f   results here */
2f180 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69  .){.  const sqli
2f181 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f  te3_tokenizer_mo
2f182 64 75 6c 65 20 2a 70 4d 6f 64 75 6c 65 20 3d 20  dule *pModule = 
2f183 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f 64  pTokenizer->pMod
2f184 75 6c 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f 74  ule;.  sqlite3_t
2f185 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
2f186 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 6e 74 20  *pCursor;.  int 
2f187 66 69 72 73 74 49 6e 64 65 78 20 3d 20 70 51 75  firstIndex = pQu
2f188 65 72 79 2d 3e 6e 54 65 72 6d 73 3b 0a 20 20 69  ery->nTerms;.  i
2f189 6e 74 20 69 43 6f 6c 3b 0a 20 20 69 6e 74 20 6e  nt iCol;.  int n
2f18a 54 65 72 6d 20 3d 20 31 3b 0a 20 20 0a 20 20 69  Term = 1;.  .  i
2f18b 6e 74 20 72 63 20 3d 20 70 4d 6f 64 75 6c 65 2d  nt rc = pModule-
2f18c 3e 78 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65  >xOpen(pTokenize
2f18d 72 2c 20 7a 53 65 67 6d 65 6e 74 2c 20 6e 53 65  r, zSegment, nSe
2f18e 67 6d 65 6e 74 2c 20 26 70 43 75 72 73 6f 72 29  gment, &pCursor)
2f18f 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2f190 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2f191 63 3b 0a 20 20 70 43 75 72 73 6f 72 2d 3e 70 54  c;.  pCursor->pT
2f192 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f 6b 65  okenizer = pToke
2f193 6e 69 7a 65 72 3b 0a 0a 20 20 77 68 69 6c 65 28  nizer;..  while(
2f194 20 31 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20   1 ){.    const 
2f195 63 68 61 72 20 2a 7a 54 6f 6b 65 6e 3b 0a 20 20  char *zToken;.  
2f196 20 20 69 6e 74 20 6e 54 6f 6b 65 6e 2c 20 69 42    int nToken, iB
2f197 65 67 69 6e 2c 20 69 45 6e 64 2c 20 69 50 6f 73  egin, iEnd, iPos
2f198 3b 0a 0a 20 20 20 20 72 63 20 3d 20 70 4d 6f 64  ;..    rc = pMod
2f199 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72 73  ule->xNext(pCurs
2f19a 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
2f19b 20 20 20 20 20 20 20 20 20 20 20 20 26 7a 54 6f              &zTo
2f19c 6b 65 6e 2c 20 26 6e 54 6f 6b 65 6e 2c 0a 20 20  ken, &nToken,.  
2f19d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f19e 20 20 20 20 20 20 26 69 42 65 67 69 6e 2c 20 26        &iBegin, &
2f19f 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a 20 20  iEnd, &iPos);.  
2f1a0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f1a1 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  _OK ) break;.   
2f1a2 20 69 66 28 20 21 69 6e 50 68 72 61 73 65 20 26   if( !inPhrase &
2f1a3 26 0a 20 20 20 20 20 20 20 20 7a 53 65 67 6d 65  &.        zSegme
2f1a4 6e 74 5b 69 45 6e 64 5d 3d 3d 27 3a 27 20 26 26  nt[iEnd]==':' &&
2f1a5 0a 20 20 20 20 20 20 20 20 20 28 69 43 6f 6c 20  .         (iCol 
2f1a6 3d 20 63 68 65 63 6b 43 6f 6c 75 6d 6e 53 70 65  = checkColumnSpe
2f1a7 63 69 66 69 65 72 28 70 51 75 65 72 79 2d 3e 70  cifier(pQuery->p
2f1a8 46 74 73 2c 20 7a 54 6f 6b 65 6e 2c 20 6e 54 6f  Fts, zToken, nTo
2f1a9 6b 65 6e 29 29 3e 3d 30 20 29 7b 0a 20 20 20 20  ken))>=0 ){.    
2f1aa 20 20 70 51 75 65 72 79 2d 3e 6e 65 78 74 43 6f    pQuery->nextCo
2f1ab 6c 75 6d 6e 20 3d 20 69 43 6f 6c 3b 0a 20 20 20  lumn = iCol;.   
2f1ac 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20     continue;.   
2f1ad 20 7d 0a 20 20 20 20 69 66 28 20 21 69 6e 50 68   }.    if( !inPh
2f1ae 72 61 73 65 20 26 26 20 70 51 75 65 72 79 2d 3e  rase && pQuery->
2f1af 6e 54 65 72 6d 73 3e 30 20 26 26 20 6e 54 6f 6b  nTerms>0 && nTok
2f1b0 65 6e 3d 3d 32 20 0a 20 20 20 20 20 26 26 20 7a  en==2 .     && z
2f1b1 53 65 67 6d 65 6e 74 5b 69 42 65 67 69 6e 2b 30  Segment[iBegin+0
2f1b2 5d 3d 3d 27 4f 27 0a 20 20 20 20 20 26 26 20 7a  ]=='O'.     && z
2f1b3 53 65 67 6d 65 6e 74 5b 69 42 65 67 69 6e 2b 31  Segment[iBegin+1
2f1b4 5d 3d 3d 27 52 27 20 0a 20 20 20 20 29 7b 0a 20  ]=='R' .    ){. 
2f1b5 20 20 20 20 20 70 51 75 65 72 79 2d 3e 6e 65 78       pQuery->nex
2f1b6 74 49 73 4f 72 20 3d 20 31 3b 0a 20 20 20 20 20  tIsOr = 1;.     
2f1b7 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 7d   continue;.    }
2f1b8 0a 20 20 20 20 69 66 28 20 21 69 6e 50 68 72 61  .    if( !inPhra
2f1b9 73 65 20 26 26 20 70 51 75 65 72 79 2d 3e 6e 54  se && pQuery->nT
2f1ba 65 72 6d 73 3e 30 20 26 26 20 21 70 51 75 65 72  erms>0 && !pQuer
2f1bb 79 2d 3e 6e 65 78 74 49 73 4f 72 20 26 26 20 6e  y->nextIsOr && n
2f1bc 54 6f 6b 65 6e 3d 3d 34 20 0a 20 20 20 20 20 20  Token==4 .      
2f1bd 26 26 20 6d 65 6d 63 6d 70 28 26 7a 53 65 67 6d  && memcmp(&zSegm
2f1be 65 6e 74 5b 69 42 65 67 69 6e 5d 2c 20 22 4e 45  ent[iBegin], "NE
2f1bf 41 52 22 2c 20 34 29 3d 3d 30 0a 20 20 20 20 29  AR", 4)==0.    )
2f1c0 7b 0a 20 20 20 20 20 20 51 75 65 72 79 54 65 72  {.      QueryTer
2f1c1 6d 20 2a 70 54 65 72 6d 20 3d 20 26 70 51 75 65  m *pTerm = &pQue
2f1c2 72 79 2d 3e 70 54 65 72 6d 73 5b 70 51 75 65 72  ry->pTerms[pQuer
2f1c3 79 2d 3e 6e 54 65 72 6d 73 2d 31 5d 3b 0a 20 20  y->nTerms-1];.  
2f1c4 20 20 20 20 69 66 28 20 28 69 42 65 67 69 6e 2b      if( (iBegin+
2f1c5 36 29 3c 6e 53 65 67 6d 65 6e 74 20 0a 20 20 20  6)<nSegment .   
2f1c6 20 20 20 20 26 26 20 7a 53 65 67 6d 65 6e 74 5b      && zSegment[
2f1c7 69 42 65 67 69 6e 2b 34 5d 20 3d 3d 20 27 2f 27  iBegin+4] == '/'
2f1c8 0a 20 20 20 20 20 20 20 26 26 20 69 73 64 69 67  .       && isdig
2f1c9 69 74 28 7a 53 65 67 6d 65 6e 74 5b 69 42 65 67  it(zSegment[iBeg
2f1ca 69 6e 2b 35 5d 29 0a 20 20 20 20 20 20 29 7b 0a  in+5]).      ){.
2f1cb 20 20 20 20 20 20 20 20 69 6e 74 20 6b 3b 0a 20          int k;. 
2f1cc 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 4e         pTerm->nN
2f1cd 65 61 72 20 3d 20 30 3b 0a 20 20 20 20 20 20 20  ear = 0;.       
2f1ce 20 66 6f 72 28 6b 3d 35 3b 20 28 69 42 65 67 69   for(k=5; (iBegi
2f1cf 6e 2b 6b 29 3c 3d 6e 53 65 67 6d 65 6e 74 20 26  n+k)<=nSegment &
2f1d0 26 20 69 73 64 69 67 69 74 28 7a 53 65 67 6d 65  & isdigit(zSegme
2f1d1 6e 74 5b 69 42 65 67 69 6e 2b 6b 5d 29 3b 20 6b  nt[iBegin+k]); k
2f1d2 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 70  ++){.          p
2f1d3 54 65 72 6d 2d 3e 6e 4e 65 61 72 20 3d 20 70 54  Term->nNear = pT
2f1d4 65 72 6d 2d 3e 6e 4e 65 61 72 2a 31 30 20 2b 20  erm->nNear*10 + 
2f1d5 28 7a 53 65 67 6d 65 6e 74 5b 69 42 65 67 69 6e  (zSegment[iBegin
2f1d6 2b 6b 5d 20 2d 20 27 30 27 29 3b 0a 20 20 20 20  +k] - '0');.    
2f1d7 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20 70 4d      }.        pM
2f1d8 6f 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75  odule->xNext(pCu
2f1d9 72 73 6f 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26  rsor, &zToken, &
2f1da 6e 54 6f 6b 65 6e 2c 20 26 69 42 65 67 69 6e 2c  nToken, &iBegin,
2f1db 20 26 69 45 6e 64 2c 20 26 69 50 6f 73 29 3b 0a   &iEnd, &iPos);.
2f1dc 20 20 20 20 20 20 7d 20 65 6c 73 65 20 7b 0a 20        } else {. 
2f1dd 20 20 20 20 20 20 20 70 54 65 72 6d 2d 3e 6e 4e         pTerm->nN
2f1de 65 61 72 20 3d 20 53 51 4c 49 54 45 5f 46 54 53  ear = SQLITE_FTS
2f1df 33 5f 44 45 46 41 55 4c 54 5f 4e 45 41 52 5f 50  3_DEFAULT_NEAR_P
2f1e0 41 52 41 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ARAM;.      }.  
2f1e1 20 20 20 20 70 54 65 72 6d 2d 3e 6e 4e 65 61 72      pTerm->nNear
2f1e2 2b 2b 3b 0a 20 20 20 20 20 20 63 6f 6e 74 69 6e  ++;.      contin
2f1e3 75 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 71  ue;.    }..    q
2f1e4 75 65 72 79 41 64 64 28 70 51 75 65 72 79 2c 20  ueryAdd(pQuery, 
2f1e5 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65 6e 29 3b  zToken, nToken);
2f1e6 0a 20 20 20 20 69 66 28 20 21 69 6e 50 68 72 61  .    if( !inPhra
2f1e7 73 65 20 26 26 20 69 42 65 67 69 6e 3e 30 20 26  se && iBegin>0 &
2f1e8 26 20 7a 53 65 67 6d 65 6e 74 5b 69 42 65 67 69  & zSegment[iBegi
2f1e9 6e 2d 31 5d 3d 3d 27 2d 27 20 29 7b 0a 20 20 20  n-1]=='-' ){.   
2f1ea 20 20 20 70 51 75 65 72 79 2d 3e 70 54 65 72 6d     pQuery->pTerm
2f1eb 73 5b 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73  s[pQuery->nTerms
2f1ec 2d 31 5d 2e 69 73 4e 6f 74 20 3d 20 31 3b 0a 20  -1].isNot = 1;. 
2f1ed 20 20 20 7d 0a 20 20 20 20 69 66 28 20 69 45 6e     }.    if( iEn
2f1ee 64 3c 6e 53 65 67 6d 65 6e 74 20 26 26 20 7a 53  d<nSegment && zS
2f1ef 65 67 6d 65 6e 74 5b 69 45 6e 64 5d 3d 3d 27 2a  egment[iEnd]=='*
2f1f0 27 20 29 7b 0a 20 20 20 20 20 20 70 51 75 65 72  ' ){.      pQuer
2f1f1 79 2d 3e 70 54 65 72 6d 73 5b 70 51 75 65 72 79  y->pTerms[pQuery
2f1f2 2d 3e 6e 54 65 72 6d 73 2d 31 5d 2e 69 73 50 72  ->nTerms-1].isPr
2f1f3 65 66 69 78 20 3d 20 31 3b 0a 20 20 20 20 7d 0a  efix = 1;.    }.
2f1f4 20 20 20 20 70 51 75 65 72 79 2d 3e 70 54 65 72      pQuery->pTer
2f1f5 6d 73 5b 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d  ms[pQuery->nTerm
2f1f6 73 2d 31 5d 2e 69 50 68 72 61 73 65 20 3d 20 6e  s-1].iPhrase = n
2f1f7 54 65 72 6d 3b 0a 20 20 20 20 69 66 28 20 69 6e  Term;.    if( in
2f1f8 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20 20 20  Phrase ){.      
2f1f9 6e 54 65 72 6d 2b 2b 3b 0a 20 20 20 20 7d 0a 20  nTerm++;.    }. 
2f1fa 20 7d 0a 0a 20 20 69 66 28 20 69 6e 50 68 72 61   }..  if( inPhra
2f1fb 73 65 20 26 26 20 70 51 75 65 72 79 2d 3e 6e 54  se && pQuery->nT
2f1fc 65 72 6d 73 3e 66 69 72 73 74 49 6e 64 65 78 20  erms>firstIndex 
2f1fd 29 7b 0a 20 20 20 20 70 51 75 65 72 79 2d 3e 70  ){.    pQuery->p
2f1fe 54 65 72 6d 73 5b 66 69 72 73 74 49 6e 64 65 78  Terms[firstIndex
2f1ff 5d 2e 6e 50 68 72 61 73 65 20 3d 20 70 51 75 65  ].nPhrase = pQue
2f200 72 79 2d 3e 6e 54 65 72 6d 73 20 2d 20 66 69 72  ry->nTerms - fir
2f201 73 74 49 6e 64 65 78 20 2d 20 31 3b 0a 20 20 7d  stIndex - 1;.  }
2f202 0a 0a 20 20 72 65 74 75 72 6e 20 70 4d 6f 64 75  ..  return pModu
2f203 6c 65 2d 3e 78 43 6c 6f 73 65 28 70 43 75 72 73  le->xClose(pCurs
2f204 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20 50 61 72 73 65  or);.}../* Parse
2f205 20 61 20 71 75 65 72 79 20 73 74 72 69 6e 67 2c   a query string,
2f206 20 79 69 65 6c 64 69 6e 67 20 61 20 51 75 65 72   yielding a Quer
2f207 79 20 6f 62 6a 65 63 74 20 70 51 75 65 72 79 2e  y object pQuery.
2f208 0a 2a 2a 0a 2a 2a 20 54 68 65 20 63 61 6c 6c 69  .**.** The calli
2f209 6e 67 20 66 75 6e 63 74 69 6f 6e 20 77 69 6c 6c  ng function will
2f20a 20 6e 65 65 64 20 74 6f 20 71 75 65 72 79 43 6c   need to queryCl
2f20b 65 61 72 28 29 20 74 6f 20 63 6c 65 61 6e 20 75  ear() to clean u
2f20c 70 0a 2a 2a 20 74 68 65 20 64 79 6e 61 6d 69 63  p.** the dynamic
2f20d 61 6c 6c 79 20 61 6c 6c 6f 63 61 74 65 64 20 6d  ally allocated m
2f20e 65 6d 6f 72 79 20 68 65 6c 64 20 62 79 20 70 51  emory held by pQ
2f20f 75 65 72 79 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  uery..*/.static 
2f210 69 6e 74 20 70 61 72 73 65 51 75 65 72 79 28 0a  int parseQuery(.
2f211 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20    fulltext_vtab 
2f212 2a 76 2c 20 20 20 20 20 20 20 20 2f 2a 20 54 68  *v,        /* Th
2f213 65 20 66 75 6c 6c 74 65 78 74 20 69 6e 64 65 78  e fulltext index
2f214 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72   */.  const char
2f215 20 2a 7a 49 6e 70 75 74 2c 20 20 20 20 20 20 2f   *zInput,      /
2f216 2a 20 49 6e 70 75 74 20 74 65 78 74 20 6f 66 20  * Input text of 
2f217 74 68 65 20 71 75 65 72 79 20 73 74 72 69 6e 67  the query string
2f218 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74   */.  int nInput
2f219 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f  ,              /
2f21a 2a 20 53 69 7a 65 20 6f 66 20 74 68 65 20 69 6e  * Size of the in
2f21b 70 75 74 20 74 65 78 74 20 2a 2f 0a 20 20 69 6e  put text */.  in
2f21c 74 20 64 66 6c 74 43 6f 6c 75 6d 6e 2c 20 20 20  t dfltColumn,   
2f21d 20 20 20 20 20 20 20 2f 2a 20 44 65 66 61 75 6c         /* Defaul
2f21e 74 20 63 6f 6c 75 6d 6e 20 6f 66 20 74 68 65 20  t column of the 
2f21f 69 6e 64 65 78 20 74 6f 20 6d 61 74 63 68 20 61  index to match a
2f220 67 61 69 6e 73 74 20 2a 2f 0a 20 20 51 75 65 72  gainst */.  Quer
2f221 79 20 2a 70 51 75 65 72 79 20 20 20 20 20 20 20  y *pQuery       
2f222 20 20 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68       /* Write th
2f223 65 20 70 61 72 73 65 20 72 65 73 75 6c 74 73 20  e parse results 
2f224 68 65 72 65 2e 20 2a 2f 0a 29 7b 0a 20 20 69 6e  here. */.){.  in
2f225 74 20 69 49 6e 70 75 74 2c 20 69 6e 50 68 72 61  t iInput, inPhra
2f226 73 65 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 69  se = 0;.  int ii
2f227 3b 0a 20 20 51 75 65 72 79 54 65 72 6d 20 2a 61  ;.  QueryTerm *a
2f228 54 65 72 6d 3b 0a 0a 20 20 69 66 28 20 7a 49 6e  Term;..  if( zIn
2f229 70 75 74 3d 3d 30 20 29 20 6e 49 6e 70 75 74 20  put==0 ) nInput 
2f22a 3d 20 30 3b 0a 20 20 69 66 28 20 6e 49 6e 70 75  = 0;.  if( nInpu
2f22b 74 3c 30 20 29 20 6e 49 6e 70 75 74 20 3d 20 73  t<0 ) nInput = s
2f22c 74 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20  trlen(zInput);. 
2f22d 20 70 51 75 65 72 79 2d 3e 6e 54 65 72 6d 73 20   pQuery->nTerms 
2f22e 3d 20 30 3b 0a 20 20 70 51 75 65 72 79 2d 3e 70  = 0;.  pQuery->p
2f22f 54 65 72 6d 73 20 3d 20 4e 55 4c 4c 3b 0a 20 20  Terms = NULL;.  
2f230 70 51 75 65 72 79 2d 3e 6e 65 78 74 49 73 4f 72  pQuery->nextIsOr
2f231 20 3d 20 30 3b 0a 20 20 70 51 75 65 72 79 2d 3e   = 0;.  pQuery->
2f232 6e 65 78 74 43 6f 6c 75 6d 6e 20 3d 20 64 66 6c  nextColumn = dfl
2f233 74 43 6f 6c 75 6d 6e 3b 0a 20 20 70 51 75 65 72  tColumn;.  pQuer
2f234 79 2d 3e 64 66 6c 74 43 6f 6c 75 6d 6e 20 3d 20  y->dfltColumn = 
2f235 64 66 6c 74 43 6f 6c 75 6d 6e 3b 0a 20 20 70 51  dfltColumn;.  pQ
2f236 75 65 72 79 2d 3e 70 46 74 73 20 3d 20 76 3b 0a  uery->pFts = v;.
2f237 0a 20 20 66 6f 72 28 69 49 6e 70 75 74 3d 30 3b  .  for(iInput=0;
2f238 20 69 49 6e 70 75 74 3c 6e 49 6e 70 75 74 3b 20   iInput<nInput; 
2f239 2b 2b 69 49 6e 70 75 74 29 7b 0a 20 20 20 20 69  ++iInput){.    i
2f23a 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
2f23b 69 49 6e 70 75 74 3b 20 69 3c 6e 49 6e 70 75 74  iInput; i<nInput
2f23c 20 26 26 20 7a 49 6e 70 75 74 5b 69 5d 21 3d 27   && zInput[i]!='
2f23d 22 27 3b 20 2b 2b 69 29 7b 7d 0a 20 20 20 20 69  "'; ++i){}.    i
2f23e 66 28 20 69 3e 69 49 6e 70 75 74 20 29 7b 0a 20  f( i>iInput ){. 
2f23f 20 20 20 20 20 74 6f 6b 65 6e 69 7a 65 53 65 67       tokenizeSeg
2f240 6d 65 6e 74 28 76 2d 3e 70 54 6f 6b 65 6e 69 7a  ment(v->pTokeniz
2f241 65 72 2c 20 7a 49 6e 70 75 74 2b 69 49 6e 70 75  er, zInput+iInpu
2f242 74 2c 20 69 2d 69 49 6e 70 75 74 2c 20 69 6e 50  t, i-iInput, inP
2f243 68 72 61 73 65 2c 0a 20 20 20 20 20 20 20 20 20  hrase,.         
2f244 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 51                pQ
2f245 75 65 72 79 29 3b 0a 20 20 20 20 7d 0a 20 20 20  uery);.    }.   
2f246 20 69 49 6e 70 75 74 20 3d 20 69 3b 0a 20 20 20   iInput = i;.   
2f247 20 69 66 28 20 69 3c 6e 49 6e 70 75 74 20 29 7b   if( i<nInput ){
2f248 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20 7a  .      assert( z
2f249 49 6e 70 75 74 5b 69 5d 3d 3d 27 22 27 20 29 3b  Input[i]=='"' );
2f24a 0a 20 20 20 20 20 20 69 6e 50 68 72 61 73 65 20  .      inPhrase 
2f24b 3d 20 21 69 6e 50 68 72 61 73 65 3b 0a 20 20 20  = !inPhrase;.   
2f24c 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20 69 6e   }.  }..  if( in
2f24d 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20 2f 2a  Phrase ){.    /*
2f24e 20 75 6e 6d 61 74 63 68 65 64 20 71 75 6f 74 65   unmatched quote
2f24f 20 2a 2f 0a 20 20 20 20 71 75 65 72 79 43 6c 65   */.    queryCle
2f250 61 72 28 70 51 75 65 72 79 29 3b 0a 20 20 20 20  ar(pQuery);.    
2f251 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52  return SQLITE_ER
2f252 52 4f 52 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 4d  ROR;.  }..  /* M
2f253 6f 64 69 66 79 20 74 68 65 20 76 61 6c 75 65 73  odify the values
2f254 20 6f 66 20 74 68 65 20 51 75 65 72 79 54 65 72   of the QueryTer
2f255 6d 2e 6e 50 68 72 61 73 65 20 76 61 72 69 61 62  m.nPhrase variab
2f256 6c 65 73 20 74 6f 20 61 63 63 6f 75 6e 74 20 66  les to account f
2f257 6f 72 0a 20 20 2a 2a 20 74 68 65 20 4e 45 41 52  or.  ** the NEAR
2f258 20 6f 70 65 72 61 74 6f 72 2e 20 46 6f 72 20 74   operator. For t
2f259 68 65 20 70 75 72 70 6f 73 65 73 20 6f 66 20 51  he purposes of Q
2f25a 75 65 72 79 54 65 72 6d 2e 6e 50 68 72 61 73 65  ueryTerm.nPhrase
2f25b 2c 20 70 68 72 61 73 65 73 0a 20 20 2a 2a 20 61  , phrases.  ** a
2f25c 6e 64 20 74 6f 6b 65 6e 73 20 63 6f 6e 6e 65 63  nd tokens connec
2f25d 74 65 64 20 62 79 20 74 68 65 20 4e 45 41 52 20  ted by the NEAR 
2f25e 6f 70 65 72 61 74 6f 72 20 61 72 65 20 68 61 6e  operator are han
2f25f 64 6c 65 64 20 61 73 20 61 20 73 69 6e 67 6c 65  dled as a single
2f260 0a 20 20 2a 2a 20 70 68 72 61 73 65 2e 20 53 65  .  ** phrase. Se
2f261 65 20 63 6f 6d 6d 65 6e 74 73 20 61 62 6f 76 65  e comments above
2f262 20 74 68 65 20 51 75 65 72 79 54 65 72 6d 20 73   the QueryTerm s
2f263 74 72 75 63 74 75 72 65 20 66 6f 72 20 64 65 74  tructure for det
2f264 61 69 6c 73 2e 0a 20 20 2a 2f 0a 20 20 61 54 65  ails..  */.  aTe
2f265 72 6d 20 3d 20 70 51 75 65 72 79 2d 3e 70 54 65  rm = pQuery->pTe
2f266 72 6d 73 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  rms;.  for(ii=0;
2f267 20 69 69 3c 70 51 75 65 72 79 2d 3e 6e 54 65 72   ii<pQuery->nTer
2f268 6d 73 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69  ms; ii++){.    i
2f269 66 28 20 61 54 65 72 6d 5b 69 69 5d 2e 6e 4e 65  f( aTerm[ii].nNe
2f26a 61 72 20 7c 7c 20 61 54 65 72 6d 5b 69 69 5d 2e  ar || aTerm[ii].
2f26b 6e 50 68 72 61 73 65 20 29 7b 0a 20 20 20 20 20  nPhrase ){.     
2f26c 20 77 68 69 6c 65 20 28 61 54 65 72 6d 5b 69 69   while (aTerm[ii
2f26d 2b 61 54 65 72 6d 5b 69 69 5d 2e 6e 50 68 72 61  +aTerm[ii].nPhra
2f26e 73 65 5d 2e 6e 4e 65 61 72 29 20 7b 0a 20 20 20  se].nNear) {.   
2f26f 20 20 20 20 20 61 54 65 72 6d 5b 69 69 5d 2e 6e       aTerm[ii].n
2f270 50 68 72 61 73 65 20 2b 3d 20 28 31 20 2b 20 61  Phrase += (1 + a
2f271 54 65 72 6d 5b 69 69 2b 61 54 65 72 6d 5b 69 69  Term[ii+aTerm[ii
2f272 5d 2e 6e 50 68 72 61 73 65 2b 31 5d 2e 6e 50 68  ].nPhrase+1].nPh
2f273 72 61 73 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20  rase);.      }. 
2f274 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75     }.  }..  retu
2f275 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
2f276 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20  ./* TODO(shess) 
2f277 52 65 66 61 63 74 6f 72 20 74 68 65 20 63 6f 64  Refactor the cod
2f278 65 20 74 6f 20 72 65 6d 6f 76 65 20 74 68 69 73  e to remove this
2f279 20 66 6f 72 77 61 72 64 20 64 65 63 6c 2e 20 2a   forward decl. *
2f27a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75  /.static int flu
2f27b 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66  shPendingTerms(f
2f27c 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29  ulltext_vtab *v)
2f27d 3b 0a 0a 2f 2a 20 50 65 72 66 6f 72 6d 20 61 20  ;../* Perform a 
2f27e 66 75 6c 6c 2d 74 65 78 74 20 71 75 65 72 79 20  full-text query 
2f27f 75 73 69 6e 67 20 74 68 65 20 73 65 61 72 63 68  using the search
2f280 20 65 78 70 72 65 73 73 69 6f 6e 20 69 6e 0a 2a   expression in.*
2f281 2a 20 7a 49 6e 70 75 74 5b 30 2e 2e 6e 49 6e 70  * zInput[0..nInp
2f282 75 74 2d 31 5d 2e 20 20 52 65 74 75 72 6e 20 61  ut-1].  Return a
2f283 20 6c 69 73 74 20 6f 66 20 6d 61 74 63 68 69 6e   list of matchin
2f284 67 20 64 6f 63 75 6d 65 6e 74 73 0a 2a 2a 20 69  g documents.** i
2f285 6e 20 70 52 65 73 75 6c 74 2e 0a 2a 2a 0a 2a 2a  n pResult..**.**
2f286 20 51 75 65 72 69 65 73 20 6d 75 73 74 20 6d 61   Queries must ma
2f287 74 63 68 20 63 6f 6c 75 6d 6e 20 69 43 6f 6c 75  tch column iColu
2f288 6d 6e 2e 20 20 4f 72 20 69 66 20 69 43 6f 6c 75  mn.  Or if iColu
2f289 6d 6e 3e 3d 6e 43 6f 6c 75 6d 6e 0a 2a 2a 20 74  mn>=nColumn.** t
2f28a 68 65 79 20 61 72 65 20 61 6c 6c 6f 77 65 64 20  hey are allowed 
2f28b 74 6f 20 6d 61 74 63 68 20 61 67 61 69 6e 73 74  to match against
2f28c 20 61 6e 79 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f 0a   any column..*/.
2f28d 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74  static int fullt
2f28e 65 78 74 51 75 65 72 79 28 0a 20 20 66 75 6c 6c  extQuery(.  full
2f28f 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 20 20  text_vtab *v,   
2f290 20 20 20 2f 2a 20 54 68 65 20 66 75 6c 6c 20 74     /* The full t
2f291 65 78 74 20 69 6e 64 65 78 20 2a 2f 0a 20 20 69  ext index */.  i
2f292 6e 74 20 69 43 6f 6c 75 6d 6e 2c 20 20 20 20 20  nt iColumn,     
2f293 20 20 20 20 20 20 2f 2a 20 4d 61 74 63 68 20 61        /* Match a
2f294 67 61 69 6e 73 74 20 74 68 69 73 20 63 6f 6c 75  gainst this colu
2f295 6d 6e 20 62 79 20 64 65 66 61 75 6c 74 20 2a 2f  mn by default */
2f296 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
2f297 49 6e 70 75 74 2c 20 20 20 20 2f 2a 20 54 68 65  Input,    /* The
2f298 20 71 75 65 72 79 20 73 74 72 69 6e 67 20 2a 2f   query string */
2f299 0a 20 20 69 6e 74 20 6e 49 6e 70 75 74 2c 20 20  .  int nInput,  
2f29a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d            /* Num
2f29b 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
2f29c 7a 49 6e 70 75 74 5b 5d 20 2a 2f 0a 20 20 44 61  zInput[] */.  Da
2f29d 74 61 42 75 66 66 65 72 20 2a 70 52 65 73 75 6c  taBuffer *pResul
2f29e 74 2c 20 20 20 2f 2a 20 57 72 69 74 65 20 74 68  t,   /* Write th
2f29f 65 20 72 65 73 75 6c 74 20 64 6f 63 6c 69 73 74  e result doclist
2f2a0 20 68 65 72 65 20 2a 2f 0a 20 20 51 75 65 72 79   here */.  Query
2f2a1 20 2a 70 51 75 65 72 79 20 20 20 20 20 20 20 20   *pQuery        
2f2a2 20 20 2f 2a 20 50 75 74 20 70 61 72 73 65 64 20    /* Put parsed 
2f2a3 71 75 65 72 79 20 73 74 72 69 6e 67 20 68 65 72  query string her
2f2a4 65 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20 69 2c  e */.){.  int i,
2f2a5 20 69 4e 65 78 74 2c 20 72 63 3b 0a 20 20 44 61   iNext, rc;.  Da
2f2a6 74 61 42 75 66 66 65 72 20 6c 65 66 74 2c 20 72  taBuffer left, r
2f2a7 69 67 68 74 2c 20 6f 72 2c 20 6e 65 77 3b 0a 20  ight, or, new;. 
2f2a8 20 69 6e 74 20 6e 4e 6f 74 20 3d 20 30 3b 0a 20   int nNot = 0;. 
2f2a9 20 51 75 65 72 79 54 65 72 6d 20 2a 61 54 65 72   QueryTerm *aTer
2f2aa 6d 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68  m;..  /* TODO(sh
2f2ab 65 73 73 29 20 49 6e 73 74 65 61 64 20 6f 66 20  ess) Instead of 
2f2ac 66 6c 75 73 68 69 6e 67 20 70 65 6e 64 69 6e 67  flushing pending
2f2ad 54 65 72 6d 73 2c 20 77 65 20 63 6f 75 6c 64 20  Terms, we could 
2f2ae 71 75 65 72 79 20 66 6f 72 0a 20 20 2a 2a 20 74  query for.  ** t
2f2af 68 65 20 72 65 6c 65 76 61 6e 74 20 74 65 72 6d  he relevant term
2f2b0 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20 64   and merge the d
2f2b1 6f 63 6c 69 73 74 20 69 6e 74 6f 20 77 68 61 74  oclist into what
2f2b2 20 77 65 20 72 65 63 65 69 76 65 20 66 72 6f 6d   we receive from
2f2b3 0a 20 20 2a 2a 20 74 68 65 20 64 61 74 61 62 61  .  ** the databa
2f2b4 73 65 2e 20 20 57 61 69 74 20 61 6e 64 20 73 65  se.  Wait and se
2f2b5 65 20 69 66 20 74 68 69 73 20 69 73 20 61 20 63  e if this is a c
2f2b6 6f 6d 6d 6f 6e 20 69 73 73 75 65 2c 20 66 69 72  ommon issue, fir
2f2b7 73 74 2e 0a 20 20 2a 2a 0a 20 20 2a 2a 20 41 20  st..  **.  ** A 
2f2b8 67 6f 6f 64 20 72 65 61 73 6f 6e 20 6e 6f 74 20  good reason not 
2f2b9 74 6f 20 66 6c 75 73 68 20 69 73 20 74 6f 20 6e  to flush is to n
2f2ba 6f 74 20 67 65 6e 65 72 61 74 65 20 75 70 64 61  ot generate upda
2f2bb 74 65 2d 72 65 6c 61 74 65 64 0a 20 20 2a 2a 20  te-related.  ** 
2f2bc 65 72 72 6f 72 20 63 6f 64 65 73 20 66 72 6f 6d  error codes from
2f2bd 20 68 65 72 65 2e 0a 20 20 2a 2f 0a 0a 20 20 2f   here..  */..  /
2f2be 2a 20 46 6c 75 73 68 20 61 6e 79 20 62 75 66 66  * Flush any buff
2f2bf 65 72 65 64 20 75 70 64 61 74 65 73 20 62 65 66  ered updates bef
2f2c0 6f 72 65 20 65 78 65 63 75 74 69 6e 67 20 74 68  ore executing th
2f2c1 65 20 71 75 65 72 79 2e 20 2a 2f 0a 20 20 72 63  e query. */.  rc
2f2c2 20 3d 20 66 6c 75 73 68 50 65 6e 64 69 6e 67 54   = flushPendingT
2f2c3 65 72 6d 73 28 76 29 3b 0a 20 20 69 66 28 20 72  erms(v);.  if( r
2f2c4 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2f2c5 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
2f2c6 54 4f 44 4f 28 73 68 65 73 73 29 20 49 20 74 68  TODO(shess) I th
2f2c7 69 6e 6b 20 74 68 61 74 20 74 68 65 20 71 75 65  ink that the que
2f2c8 72 79 43 6c 65 61 72 28 29 20 63 61 6c 6c 73 20  ryClear() calls 
2f2c9 62 65 6c 6f 77 20 61 72 65 20 6e 6f 74 0a 20 20  below are not.  
2f2ca 2a 2a 20 6e 65 63 65 73 73 61 72 79 2c 20 62 65  ** necessary, be
2f2cb 63 61 75 73 65 20 66 75 6c 6c 74 65 78 74 43 6c  cause fulltextCl
2f2cc 6f 73 65 28 29 20 61 6c 72 65 61 64 79 20 63 6c  ose() already cl
2f2cd 65 61 72 73 20 74 68 65 20 71 75 65 72 79 2e 0a  ears the query..
2f2ce 20 20 2a 2f 0a 20 20 72 63 20 3d 20 70 61 72 73    */.  rc = pars
2f2cf 65 51 75 65 72 79 28 76 2c 20 7a 49 6e 70 75 74  eQuery(v, zInput
2f2d0 2c 20 6e 49 6e 70 75 74 2c 20 69 43 6f 6c 75 6d  , nInput, iColum
2f2d1 6e 2c 20 70 51 75 65 72 79 29 3b 0a 20 20 69 66  n, pQuery);.  if
2f2d2 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f2d3 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2f2d4 2f 2a 20 45 6d 70 74 79 20 6f 72 20 4e 55 4c 4c  /* Empty or NULL
2f2d5 20 71 75 65 72 69 65 73 20 72 65 74 75 72 6e 20   queries return 
2f2d6 6e 6f 20 72 65 73 75 6c 74 73 2e 20 2a 2f 0a 20  no results. */. 
2f2d7 20 69 66 28 20 70 51 75 65 72 79 2d 3e 6e 54 65   if( pQuery->nTe
2f2d8 72 6d 73 3d 3d 30 20 29 7b 0a 20 20 20 20 64 61  rms==0 ){.    da
2f2d9 74 61 42 75 66 66 65 72 49 6e 69 74 28 70 52 65  taBufferInit(pRe
2f2da 73 75 6c 74 2c 20 30 29 3b 0a 20 20 20 20 72 65  sult, 0);.    re
2f2db 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
2f2dc 20 20 7d 0a 0a 20 20 2f 2a 20 4d 65 72 67 65 20    }..  /* Merge 
2f2dd 41 4e 44 20 74 65 72 6d 73 2e 20 2a 2f 0a 20 20  AND terms. */.  
2f2de 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 49  /* TODO(shess) I
2f2df 20 74 68 69 6e 6b 20 77 65 20 63 61 6e 20 65 61   think we can ea
2f2e0 72 6c 79 2d 65 78 69 74 20 69 66 28 20 69 3e 6e  rly-exit if( i>n
2f2e1 4e 6f 74 20 26 26 20 6c 65 66 74 2e 6e 44 61 74  Not && left.nDat
2f2e2 61 3d 3d 30 20 29 2e 20 2a 2f 0a 20 20 61 54 65  a==0 ). */.  aTe
2f2e3 72 6d 20 3d 20 70 51 75 65 72 79 2d 3e 70 54 65  rm = pQuery->pTe
2f2e4 72 6d 73 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30  rms;.  for(i = 0
2f2e5 3b 20 69 3c 70 51 75 65 72 79 2d 3e 6e 54 65 72  ; i<pQuery->nTer
2f2e6 6d 73 3b 20 69 3d 69 4e 65 78 74 29 7b 0a 20 20  ms; i=iNext){.  
2f2e7 20 20 69 66 28 20 61 54 65 72 6d 5b 69 5d 2e 69    if( aTerm[i].i
2f2e8 73 4e 6f 74 20 29 7b 0a 20 20 20 20 20 20 2f 2a  sNot ){.      /*
2f2e9 20 48 61 6e 64 6c 65 20 61 6c 6c 20 4e 4f 54 20   Handle all NOT 
2f2ea 74 65 72 6d 73 20 69 6e 20 61 20 73 65 70 61 72  terms in a separ
2f2eb 61 74 65 20 70 61 73 73 20 2a 2f 0a 20 20 20 20  ate pass */.    
2f2ec 20 20 6e 4e 6f 74 2b 2b 3b 0a 20 20 20 20 20 20    nNot++;.      
2f2ed 69 4e 65 78 74 20 3d 20 69 20 2b 20 61 54 65 72  iNext = i + aTer
2f2ee 6d 5b 69 5d 2e 6e 50 68 72 61 73 65 2b 31 3b 0a  m[i].nPhrase+1;.
2f2ef 20 20 20 20 20 20 63 6f 6e 74 69 6e 75 65 3b 0a        continue;.
2f2f0 20 20 20 20 7d 0a 20 20 20 20 69 4e 65 78 74 20      }.    iNext 
2f2f1 3d 20 69 20 2b 20 61 54 65 72 6d 5b 69 5d 2e 6e  = i + aTerm[i].n
2f2f2 50 68 72 61 73 65 20 2b 20 31 3b 0a 20 20 20 20  Phrase + 1;.    
2f2f3 72 63 20 3d 20 64 6f 63 4c 69 73 74 4f 66 54 65  rc = docListOfTe
2f2f4 72 6d 28 76 2c 20 61 54 65 72 6d 5b 69 5d 2e 69  rm(v, aTerm[i].i
2f2f5 43 6f 6c 75 6d 6e 2c 20 26 61 54 65 72 6d 5b 69  Column, &aTerm[i
2f2f6 5d 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20  ], &right);.    
2f2f7 69 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20  if( rc ){.      
2f2f8 69 66 28 20 69 21 3d 6e 4e 6f 74 20 29 20 64 61  if( i!=nNot ) da
2f2f9 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
2f2fa 26 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 71 75  &left);.      qu
2f2fb 65 72 79 43 6c 65 61 72 28 70 51 75 65 72 79 29  eryClear(pQuery)
2f2fc 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 72  ;.      return r
2f2fd 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 77 68 69  c;.    }.    whi
2f2fe 6c 65 28 20 69 4e 65 78 74 3c 70 51 75 65 72 79  le( iNext<pQuery
2f2ff 2d 3e 6e 54 65 72 6d 73 20 26 26 20 61 54 65 72  ->nTerms && aTer
2f300 6d 5b 69 4e 65 78 74 5d 2e 69 73 4f 72 20 29 7b  m[iNext].isOr ){
2f301 0a 20 20 20 20 20 20 72 63 20 3d 20 64 6f 63 4c  .      rc = docL
2f302 69 73 74 4f 66 54 65 72 6d 28 76 2c 20 61 54 65  istOfTerm(v, aTe
2f303 72 6d 5b 69 4e 65 78 74 5d 2e 69 43 6f 6c 75 6d  rm[iNext].iColum
2f304 6e 2c 20 26 61 54 65 72 6d 5b 69 4e 65 78 74 5d  n, &aTerm[iNext]
2f305 2c 20 26 6f 72 29 3b 0a 20 20 20 20 20 20 69 4e  , &or);.      iN
2f306 65 78 74 20 2b 3d 20 61 54 65 72 6d 5b 69 4e 65  ext += aTerm[iNe
2f307 78 74 5d 2e 6e 50 68 72 61 73 65 20 2b 20 31 3b  xt].nPhrase + 1;
2f308 0a 20 20 20 20 20 20 69 66 28 20 72 63 20 29 7b  .      if( rc ){
2f309 0a 20 20 20 20 20 20 20 20 69 66 28 20 69 21 3d  .        if( i!=
2f30a 6e 4e 6f 74 20 29 20 64 61 74 61 42 75 66 66 65  nNot ) dataBuffe
2f30b 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74 29 3b  rDestroy(&left);
2f30c 0a 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66  .        dataBuf
2f30d 66 65 72 44 65 73 74 72 6f 79 28 26 72 69 67 68  ferDestroy(&righ
2f30e 74 29 3b 0a 20 20 20 20 20 20 20 20 71 75 65 72  t);.        quer
2f30f 79 43 6c 65 61 72 28 70 51 75 65 72 79 29 3b 0a  yClear(pQuery);.
2f310 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 20 72          return r
2f311 63 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  c;.      }.     
2f312 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74 28   dataBufferInit(
2f313 26 6e 65 77 2c 20 30 29 3b 0a 20 20 20 20 20 20  &new, 0);.      
2f314 64 6f 63 4c 69 73 74 4f 72 4d 65 72 67 65 28 72  docListOrMerge(r
2f315 69 67 68 74 2e 70 44 61 74 61 2c 20 72 69 67 68  ight.pData, righ
2f316 74 2e 6e 44 61 74 61 2c 20 6f 72 2e 70 44 61 74  t.nData, or.pDat
2f317 61 2c 20 6f 72 2e 6e 44 61 74 61 2c 20 26 6e 65  a, or.nData, &ne
2f318 77 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75  w);.      dataBu
2f319 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 69 67  fferDestroy(&rig
2f31a 68 74 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42  ht);.      dataB
2f31b 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 6f 72  ufferDestroy(&or
2f31c 29 3b 0a 20 20 20 20 20 20 72 69 67 68 74 20 3d  );.      right =
2f31d 20 6e 65 77 3b 0a 20 20 20 20 7d 0a 20 20 20 20   new;.    }.    
2f31e 69 66 28 20 69 3d 3d 6e 4e 6f 74 20 29 7b 20 20  if( i==nNot ){  
2f31f 20 20 20 20 20 20 20 20 20 2f 2a 20 66 69 72 73           /* firs
2f320 74 20 74 65 72 6d 20 70 72 6f 63 65 73 73 65 64  t term processed
2f321 2e 20 2a 2f 0a 20 20 20 20 20 20 6c 65 66 74 20  . */.      left 
2f322 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 7d 65 6c  = right;.    }el
2f323 73 65 7b 0a 20 20 20 20 20 20 64 61 74 61 42 75  se{.      dataBu
2f324 66 66 65 72 49 6e 69 74 28 26 6e 65 77 2c 20 30  fferInit(&new, 0
2f325 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69 73 74  );.      docList
2f326 41 6e 64 4d 65 72 67 65 28 6c 65 66 74 2e 70 44  AndMerge(left.pD
2f327 61 74 61 2c 20 6c 65 66 74 2e 6e 44 61 74 61 2c  ata, left.nData,
2f328 20 72 69 67 68 74 2e 70 44 61 74 61 2c 20 72 69   right.pData, ri
2f329 67 68 74 2e 6e 44 61 74 61 2c 20 26 6e 65 77 29  ght.nData, &new)
2f32a 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66 66  ;.      dataBuff
2f32b 65 72 44 65 73 74 72 6f 79 28 26 72 69 67 68 74  erDestroy(&right
2f32c 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
2f32d 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  ferDestroy(&left
2f32e 29 3b 0a 20 20 20 20 20 20 6c 65 66 74 20 3d 20  );.      left = 
2f32f 6e 65 77 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  new;.    }.  }..
2f330 20 20 69 66 28 20 6e 4e 6f 74 3d 3d 70 51 75 65    if( nNot==pQue
2f331 72 79 2d 3e 6e 54 65 72 6d 73 20 29 7b 0a 20 20  ry->nTerms ){.  
2f332 20 20 2f 2a 20 57 65 20 64 6f 20 6e 6f 74 20 79    /* We do not y
2f333 65 74 20 6b 6e 6f 77 20 68 6f 77 20 74 6f 20 68  et know how to h
2f334 61 6e 64 6c 65 20 61 20 71 75 65 72 79 20 6f 66  andle a query of
2f335 20 6f 6e 6c 79 20 4e 4f 54 20 74 65 72 6d 73 20   only NOT terms 
2f336 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51  */.    return SQ
2f337 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 7d 0a  LITE_ERROR;.  }.
2f338 0a 20 20 2f 2a 20 44 6f 20 74 68 65 20 45 58 43  .  /* Do the EXC
2f339 45 50 54 20 74 65 72 6d 73 20 2a 2f 0a 20 20 66  EPT terms */.  f
2f33a 6f 72 28 69 3d 30 3b 20 69 3c 70 51 75 65 72 79  or(i=0; i<pQuery
2f33b 2d 3e 6e 54 65 72 6d 73 3b 20 20 69 20 2b 3d 20  ->nTerms;  i += 
2f33c 61 54 65 72 6d 5b 69 5d 2e 6e 50 68 72 61 73 65  aTerm[i].nPhrase
2f33d 20 2b 20 31 29 7b 0a 20 20 20 20 69 66 28 20 21   + 1){.    if( !
2f33e 61 54 65 72 6d 5b 69 5d 2e 69 73 4e 6f 74 20 29  aTerm[i].isNot )
2f33f 20 63 6f 6e 74 69 6e 75 65 3b 0a 20 20 20 20 72   continue;.    r
2f340 63 20 3d 20 64 6f 63 4c 69 73 74 4f 66 54 65 72  c = docListOfTer
2f341 6d 28 76 2c 20 61 54 65 72 6d 5b 69 5d 2e 69 43  m(v, aTerm[i].iC
2f342 6f 6c 75 6d 6e 2c 20 26 61 54 65 72 6d 5b 69 5d  olumn, &aTerm[i]
2f343 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 69  , &right);.    i
2f344 66 28 20 72 63 20 29 7b 0a 20 20 20 20 20 20 71  f( rc ){.      q
2f345 75 65 72 79 43 6c 65 61 72 28 70 51 75 65 72 79  ueryClear(pQuery
2f346 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42 75 66  );.      dataBuf
2f347 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66 74  ferDestroy(&left
2f348 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  );.      return 
2f349 72 63 3b 0a 20 20 20 20 7d 0a 20 20 20 20 64 61  rc;.    }.    da
2f34a 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 6e 65  taBufferInit(&ne
2f34b 77 2c 20 30 29 3b 0a 20 20 20 20 64 6f 63 4c 69  w, 0);.    docLi
2f34c 73 74 45 78 63 65 70 74 4d 65 72 67 65 28 6c 65  stExceptMerge(le
2f34d 66 74 2e 70 44 61 74 61 2c 20 6c 65 66 74 2e 6e  ft.pData, left.n
2f34e 44 61 74 61 2c 20 72 69 67 68 74 2e 70 44 61 74  Data, right.pDat
2f34f 61 2c 20 72 69 67 68 74 2e 6e 44 61 74 61 2c 20  a, right.nData, 
2f350 26 6e 65 77 29 3b 0a 20 20 20 20 64 61 74 61 42  &new);.    dataB
2f351 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 72 69  ufferDestroy(&ri
2f352 67 68 74 29 3b 0a 20 20 20 20 64 61 74 61 42 75  ght);.    dataBu
2f353 66 66 65 72 44 65 73 74 72 6f 79 28 26 6c 65 66  fferDestroy(&lef
2f354 74 29 3b 0a 20 20 20 20 6c 65 66 74 20 3d 20 6e  t);.    left = n
2f355 65 77 3b 0a 20 20 7d 0a 0a 20 20 2a 70 52 65 73  ew;.  }..  *pRes
2f356 75 6c 74 20 3d 20 6c 65 66 74 3b 0a 20 20 72 65  ult = left;.  re
2f357 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn rc;.}../*.*
2f358 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 78 46  * This is the xF
2f359 69 6c 74 65 72 20 69 6e 74 65 72 66 61 63 65 20  ilter interface 
2f35a 66 6f 72 20 74 68 65 20 76 69 72 74 75 61 6c 20  for the virtual 
2f35b 74 61 62 6c 65 2e 20 20 53 65 65 0a 2a 2a 20 74  table.  See.** t
2f35c 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
2f35d 20 78 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 20   xFilter method 
2f35e 64 6f 63 75 6d 65 6e 74 61 74 69 6f 6e 20 66 6f  documentation fo
2f35f 72 20 61 64 64 69 74 69 6f 6e 61 6c 0a 2a 2a 20  r additional.** 
2f360 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 0a 2a 2a 0a  information..**.
2f361 2a 2a 20 49 66 20 69 64 78 4e 75 6d 3d 3d 51 55  ** If idxNum==QU
2f362 45 52 59 5f 47 45 4e 45 52 49 43 20 74 68 65 6e  ERY_GENERIC then
2f363 20 64 6f 20 61 20 66 75 6c 6c 20 74 61 62 6c 65   do a full table
2f364 20 73 63 61 6e 20 61 67 61 69 6e 73 74 0a 2a 2a   scan against.**
2f365 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74 20 74   the %_content t
2f366 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 69  able..**.** If i
2f367 64 78 4e 75 6d 3d 3d 51 55 45 52 59 5f 44 4f 43  dxNum==QUERY_DOC
2f368 49 44 20 74 68 65 6e 20 64 6f 20 61 20 64 6f 63  ID then do a doc
2f369 69 64 20 6c 6f 6f 6b 75 70 20 66 6f 72 20 61 20  id lookup for a 
2f36a 73 69 6e 67 6c 65 20 65 6e 74 72 79 0a 2a 2a 20  single entry.** 
2f36b 69 6e 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e 74  in the %_content
2f36c 20 74 61 62 6c 65 2e 0a 2a 2a 0a 2a 2a 20 49 66   table..**.** If
2f36d 20 69 64 78 4e 75 6d 3e 3d 51 55 45 52 59 5f 46   idxNum>=QUERY_F
2f36e 55 4c 4c 54 45 58 54 20 74 68 65 6e 20 75 73 65  ULLTEXT then use
2f36f 20 74 68 65 20 66 75 6c 6c 20 74 65 78 74 20 69   the full text i
2f370 6e 64 65 78 2e 20 20 54 68 65 0a 2a 2a 20 63 6f  ndex.  The.** co
2f371 6c 75 6d 6e 20 6f 6e 20 74 68 65 20 6c 65 66 74  lumn on the left
2f372 2d 68 61 6e 64 20 73 69 64 65 20 6f 66 20 74 68  -hand side of th
2f373 65 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72  e MATCH operator
2f374 20 69 73 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 6e 75   is column.** nu
2f375 6d 62 65 72 20 69 64 78 4e 75 6d 2d 51 55 45 52  mber idxNum-QUER
2f376 59 5f 46 55 4c 4c 54 45 58 54 2c 20 30 20 69 6e  Y_FULLTEXT, 0 in
2f377 64 65 78 65 64 2e 20 20 61 72 67 76 5b 30 5d 20  dexed.  argv[0] 
2f378 69 73 20 74 68 65 20 72 69 67 68 74 2d 68 61 6e  is the right-han
2f379 64 0a 2a 2a 20 73 69 64 65 20 6f 66 20 74 68 65  d.** side of the
2f37a 20 4d 41 54 43 48 20 6f 70 65 72 61 74 6f 72 2e   MATCH operator.
2f37b 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
2f37c 73 29 20 55 70 67 72 61 64 65 20 74 68 65 20 63  s) Upgrade the c
2f37d 75 72 73 6f 72 20 69 6e 69 74 69 61 6c 69 7a 61  ursor initializa
2f37e 74 69 6f 6e 20 61 6e 64 20 64 65 73 74 72 75 63  tion and destruc
2f37f 74 69 6f 6e 20 74 6f 0a 2a 2a 20 61 63 63 6f 75  tion to.** accou
2f380 6e 74 20 66 6f 72 20 66 75 6c 6c 74 65 78 74 46  nt for fulltextF
2f381 69 6c 74 65 72 28 29 20 62 65 69 6e 67 20 63 61  ilter() being ca
2f382 6c 6c 65 64 20 6d 75 6c 74 69 70 6c 65 20 74 69  lled multiple ti
2f383 6d 65 73 20 6f 6e 20 74 68 65 0a 2a 2a 20 73 61  mes on the.** sa
2f384 6d 65 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20  me cursor.  The 
2f385 63 75 72 72 65 6e 74 20 73 6f 6c 75 74 69 6f 6e  current solution
2f386 20 69 73 20 76 65 72 79 20 66 72 61 67 69 6c 65   is very fragile
2f387 2e 20 20 41 70 70 6c 79 20 66 69 78 20 74 6f 0a  .  Apply fix to.
2f388 2a 2a 20 66 74 73 33 20 61 73 20 61 70 70 72 6f  ** fts3 as appro
2f389 70 72 69 61 74 65 2e 0a 2a 2f 0a 73 74 61 74 69  priate..*/.stati
2f38a 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 46 69  c int fulltextFi
2f38b 6c 74 65 72 28 0a 20 20 73 71 6c 69 74 65 33 5f  lter(.  sqlite3_
2f38c 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75  vtab_cursor *pCu
2f38d 72 73 6f 72 2c 20 20 20 20 20 2f 2a 20 54 68 65  rsor,     /* The
2f38e 20 63 75 72 73 6f 72 20 75 73 65 64 20 66 6f 72   cursor used for
2f38f 20 74 68 69 73 20 71 75 65 72 79 20 2a 2f 0a 20   this query */. 
2f390 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
2f391 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
2f392 20 20 20 2f 2a 20 57 68 69 63 68 20 69 6e 64 65     /* Which inde
2f393 78 69 6e 67 20 73 63 68 65 6d 65 20 74 6f 20 75  xing scheme to u
2f394 73 65 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63  se */.  int argc
2f395 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20  , sqlite3_value 
2f396 2a 2a 61 72 67 76 20 20 20 20 2f 2a 20 41 72 67  **argv    /* Arg
2f397 75 6d 65 6e 74 73 20 66 6f 72 20 74 68 65 20 69  uments for the i
2f398 6e 64 65 78 69 6e 67 20 73 63 68 65 6d 65 20 2a  ndexing scheme *
2f399 2f 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  /.){.  fulltext_
2f39a 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75 6c  cursor *c = (ful
2f39b 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29 20  ltext_cursor *) 
2f39c 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c 74  pCursor;.  fullt
2f39d 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 63 75  ext_vtab *v = cu
2f39e 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a 20 20  rsor_vtab(c);.  
2f39f 69 6e 74 20 72 63 3b 0a 0a 20 20 46 54 53 54 52  int rc;..  FTSTR
2f3a0 41 43 45 28 28 22 46 54 53 33 20 46 69 6c 74 65  ACE(("FTS3 Filte
2f3a1 72 20 25 70 5c 6e 22 2c 70 43 75 72 73 6f 72 29  r %p\n",pCursor)
2f3a2 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
2f3a3 63 75 72 73 6f 72 20 68 61 73 20 61 20 73 74 61  cursor has a sta
2f3a4 74 65 6d 65 6e 74 20 74 68 61 74 20 77 61 73 20  tement that was 
2f3a5 6e 6f 74 20 70 72 65 70 61 72 65 64 20 61 63 63  not prepared acc
2f3a6 6f 72 64 69 6e 67 20 74 6f 0a 20 20 2a 2a 20 69  ording to.  ** i
2f3a7 64 78 4e 75 6d 2c 20 63 6c 65 61 72 20 69 74 2e  dxNum, clear it.
2f3a8 20 20 49 20 62 65 6c 69 65 76 65 20 61 6c 6c 20    I believe all 
2f3a9 63 61 6c 6c 73 20 74 6f 20 66 75 6c 6c 74 65 78  calls to fulltex
2f3aa 74 46 69 6c 74 65 72 20 77 69 74 68 20 61 0a 20  tFilter with a. 
2f3ab 20 2a 2a 20 67 69 76 65 6e 20 63 75 72 73 6f 72   ** given cursor
2f3ac 20 77 69 6c 6c 20 68 61 76 65 20 74 68 65 20 73   will have the s
2f3ad 61 6d 65 20 69 64 78 4e 75 6d 20 2c 20 62 75 74  ame idxNum , but
2f3ae 20 69 6e 20 74 68 69 73 20 63 61 73 65 20 69 74   in this case it
2f3af 27 73 0a 20 20 2a 2a 20 65 61 73 79 20 74 6f 20  's.  ** easy to 
2f3b0 62 65 20 73 61 66 65 2e 0a 20 20 2a 2f 0a 20 20  be safe..  */.  
2f3b1 69 66 28 20 63 2d 3e 70 53 74 6d 74 20 26 26 20  if( c->pStmt && 
2f3b2 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 21 3d  c->iCursorType!=
2f3b3 69 64 78 4e 75 6d 20 29 7b 0a 20 20 20 20 73 71  idxNum ){.    sq
2f3b4 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 63  lite3_finalize(c
2f3b5 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 63 2d  ->pStmt);.    c-
2f3b6 3e 70 53 74 6d 74 20 3d 20 4e 55 4c 4c 3b 0a 20  >pStmt = NULL;. 
2f3b7 20 7d 0a 0a 20 20 2f 2a 20 47 65 74 20 61 20 66   }..  /* Get a f
2f3b8 72 65 73 68 20 73 74 61 74 65 6d 65 6e 74 20 61  resh statement a
2f3b9 70 70 72 6f 70 72 69 61 74 65 20 74 6f 20 69 64  ppropriate to id
2f3ba 78 4e 75 6d 2e 20 2a 2f 0a 20 20 2f 2a 20 54 4f  xNum. */.  /* TO
2f3bb 44 4f 28 73 68 65 73 73 29 3a 20 41 64 64 20 61  DO(shess): Add a
2f3bc 20 70 72 65 70 61 72 65 64 2d 73 74 61 74 65 6d   prepared-statem
2f3bd 65 6e 74 20 63 61 63 68 65 20 69 6e 20 74 68 65  ent cache in the
2f3be 20 76 74 20 73 74 72 75 63 74 75 72 65 2e 0a 20   vt structure.. 
2f3bf 20 2a 2a 20 54 68 65 20 63 61 63 68 65 20 6d 75   ** The cache mu
2f3c0 73 74 20 68 61 6e 64 6c 65 20 6d 75 6c 74 69 70  st handle multip
2f3c1 6c 65 20 6f 70 65 6e 20 63 75 72 73 6f 72 73 2e  le open cursors.
2f3c2 20 20 45 61 73 69 65 72 20 74 6f 20 63 61 63 68    Easier to cach
2f3c3 65 20 74 68 65 0a 20 20 2a 2a 20 73 74 61 74 65  e the.  ** state
2f3c4 6d 65 6e 74 20 76 61 72 69 61 6e 74 73 20 61 74  ment variants at
2f3c5 20 74 68 65 20 76 74 20 74 6f 20 72 65 64 75 63   the vt to reduc
2f3c6 65 20 6d 61 6c 6c 6f 63 2f 72 65 61 6c 6c 6f 63  e malloc/realloc
2f3c7 2f 66 72 65 65 20 68 65 72 65 2e 0a 20 20 2a 2a  /free here..  **
2f3c8 20 4f 72 20 77 65 20 63 6f 75 6c 64 20 68 61 76   Or we could hav
2f3c9 65 20 61 20 53 74 72 69 6e 67 42 75 66 66 65 72  e a StringBuffer
2f3ca 20 76 61 72 69 61 6e 74 20 77 68 69 63 68 20 61   variant which a
2f3cb 6c 6c 6f 77 65 64 20 73 74 61 63 6b 0a 20 20 2a  llowed stack.  *
2f3cc 2a 20 63 6f 6e 73 74 72 75 63 74 69 6f 6e 20 66  * construction f
2f3cd 6f 72 20 73 6d 61 6c 6c 20 76 61 6c 75 65 73 2e  or small values.
2f3ce 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21 63 2d 3e  .  */.  if( !c->
2f3cf 70 53 74 6d 74 20 29 7b 0a 20 20 20 20 53 74 72  pStmt ){.    Str
2f3d0 69 6e 67 42 75 66 66 65 72 20 73 62 3b 0a 20 20  ingBuffer sb;.  
2f3d1 20 20 69 6e 69 74 53 74 72 69 6e 67 42 75 66 66    initStringBuff
2f3d2 65 72 28 26 73 62 29 3b 0a 20 20 20 20 61 70 70  er(&sb);.    app
2f3d3 65 6e 64 28 26 73 62 2c 20 22 53 45 4c 45 43 54  end(&sb, "SELECT
2f3d4 20 64 6f 63 69 64 2c 20 22 29 3b 0a 20 20 20 20   docid, ");.    
2f3d5 61 70 70 65 6e 64 4c 69 73 74 28 26 73 62 2c 20  appendList(&sb, 
2f3d6 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 76 2d 3e 61  v->nColumn, v->a
2f3d7 7a 43 6f 6e 74 65 6e 74 43 6f 6c 75 6d 6e 29 3b  zContentColumn);
2f3d8 0a 20 20 20 20 61 70 70 65 6e 64 28 26 73 62 2c  .    append(&sb,
2f3d9 20 22 20 46 52 4f 4d 20 25 5f 63 6f 6e 74 65 6e   " FROM %_conten
2f3da 74 22 29 3b 0a 20 20 20 20 69 66 28 20 69 64 78  t");.    if( idx
2f3db 4e 75 6d 21 3d 51 55 45 52 59 5f 47 45 4e 45 52  Num!=QUERY_GENER
2f3dc 49 43 20 29 20 61 70 70 65 6e 64 28 26 73 62 2c  IC ) append(&sb,
2f3dd 20 22 20 57 48 45 52 45 20 64 6f 63 69 64 20 3d   " WHERE docid =
2f3de 20 3f 22 29 3b 0a 20 20 20 20 72 63 20 3d 20 73   ?");.    rc = s
2f3df 71 6c 5f 70 72 65 70 61 72 65 28 76 2d 3e 64 62  ql_prepare(v->db
2f3e0 2c 20 76 2d 3e 7a 44 62 2c 20 76 2d 3e 7a 4e 61  , v->zDb, v->zNa
2f3e1 6d 65 2c 20 26 63 2d 3e 70 53 74 6d 74 2c 0a 20  me, &c->pStmt,. 
2f3e2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f3e3 20 20 20 20 73 74 72 69 6e 67 42 75 66 66 65 72      stringBuffer
2f3e4 44 61 74 61 28 26 73 62 29 29 3b 0a 20 20 20 20  Data(&sb));.    
2f3e5 73 74 72 69 6e 67 42 75 66 66 65 72 44 65 73 74  stringBufferDest
2f3e6 72 6f 79 28 26 73 62 29 3b 0a 20 20 20 20 69 66  roy(&sb);.    if
2f3e7 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f3e8 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20  ) return rc;.   
2f3e9 20 63 2d 3e 69 43 75 72 73 6f 72 54 79 70 65 20   c->iCursorType 
2f3ea 3d 20 69 64 78 4e 75 6d 3b 0a 20 20 7d 65 6c 73  = idxNum;.  }els
2f3eb 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  e{.    sqlite3_r
2f3ec 65 73 65 74 28 63 2d 3e 70 53 74 6d 74 29 3b 0a  eset(c->pStmt);.
2f3ed 20 20 20 20 61 73 73 65 72 74 28 20 63 2d 3e 69      assert( c->i
2f3ee 43 75 72 73 6f 72 54 79 70 65 3d 3d 69 64 78 4e  CursorType==idxN
2f3ef 75 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 73 77 69  um );.  }..  swi
2f3f0 74 63 68 28 20 69 64 78 4e 75 6d 20 29 7b 0a 20  tch( idxNum ){. 
2f3f1 20 20 20 63 61 73 65 20 51 55 45 52 59 5f 47 45     case QUERY_GE
2f3f2 4e 45 52 49 43 3a 0a 20 20 20 20 20 20 62 72 65  NERIC:.      bre
2f3f3 61 6b 3b 0a 0a 20 20 20 20 63 61 73 65 20 51 55  ak;..    case QU
2f3f4 45 52 59 5f 44 4f 43 49 44 3a 0a 20 20 20 20 20  ERY_DOCID:.     
2f3f5 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 62 69   rc = sqlite3_bi
2f3f6 6e 64 5f 69 6e 74 36 34 28 63 2d 3e 70 53 74 6d  nd_int64(c->pStm
2f3f7 74 2c 20 31 2c 20 73 71 6c 69 74 65 33 5f 76 61  t, 1, sqlite3_va
2f3f8 6c 75 65 5f 69 6e 74 36 34 28 61 72 67 76 5b 30  lue_int64(argv[0
2f3f9 5d 29 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ]));.      if( r
2f3fa 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2f3fb 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20 20  eturn rc;.      
2f3fc 62 72 65 61 6b 3b 0a 0a 20 20 20 20 64 65 66 61  break;..    defa
2f3fd 75 6c 74 3a 20 20 20 2f 2a 20 66 75 6c 6c 2d 74  ult:   /* full-t
2f3fe 65 78 74 20 73 65 61 72 63 68 20 2a 2f 0a 20 20  ext search */.  
2f3ff 20 20 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20    {.      const 
2f400 63 68 61 72 20 2a 7a 51 75 65 72 79 20 3d 20 28  char *zQuery = (
2f401 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73 71 6c  const char *)sql
2f402 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28  ite3_value_text(
2f403 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 20 20  argv[0]);.      
2f404 61 73 73 65 72 74 28 20 69 64 78 4e 75 6d 3c 3d  assert( idxNum<=
2f405 51 55 45 52 59 5f 46 55 4c 4c 54 45 58 54 2b 76  QUERY_FULLTEXT+v
2f406 2d 3e 6e 43 6f 6c 75 6d 6e 29 3b 0a 20 20 20 20  ->nColumn);.    
2f407 20 20 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d    assert( argc==
2f408 31 20 29 3b 0a 20 20 20 20 20 20 71 75 65 72 79  1 );.      query
2f409 43 6c 65 61 72 28 26 63 2d 3e 71 29 3b 0a 20 20  Clear(&c->q);.  
2f40a 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c      if( c->resul
2f40b 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  t.nData!=0 ){.  
2f40c 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63 61        /* This ca
2f40d 73 65 20 68 61 70 70 65 6e 73 20 69 66 20 74 68  se happens if th
2f40e 65 20 73 61 6d 65 20 63 75 72 73 6f 72 20 69 73  e same cursor is
2f40f 20 75 73 65 64 20 72 65 70 65 61 74 65 64 6c 79   used repeatedly
2f410 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 6c 72  . */.        dlr
2f411 44 65 73 74 72 6f 79 28 26 63 2d 3e 72 65 61 64  Destroy(&c->read
2f412 65 72 29 3b 0a 20 20 20 20 20 20 20 20 64 61 74  er);.        dat
2f413 61 42 75 66 66 65 72 52 65 73 65 74 28 26 63 2d  aBufferReset(&c-
2f414 3e 72 65 73 75 6c 74 29 3b 0a 20 20 20 20 20 20  >result);.      
2f415 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 64  }else{.        d
2f416 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 63  ataBufferInit(&c
2f417 2d 3e 72 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20  ->result, 0);.  
2f418 20 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d      }.      rc =
2f419 20 66 75 6c 6c 74 65 78 74 51 75 65 72 79 28 76   fulltextQuery(v
2f41a 2c 20 69 64 78 4e 75 6d 2d 51 55 45 52 59 5f 46  , idxNum-QUERY_F
2f41b 55 4c 4c 54 45 58 54 2c 20 7a 51 75 65 72 79 2c  ULLTEXT, zQuery,
2f41c 20 2d 31 2c 20 26 63 2d 3e 72 65 73 75 6c 74 2c   -1, &c->result,
2f41d 20 26 63 2d 3e 71 29 3b 0a 20 20 20 20 20 20 69   &c->q);.      i
2f41e 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2f41f 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
2f420 20 20 20 20 69 66 28 20 63 2d 3e 72 65 73 75 6c      if( c->resul
2f421 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20 20  t.nData!=0 ){.  
2f422 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26 63        dlrInit(&c
2f423 2d 3e 72 65 61 64 65 72 2c 20 44 4c 5f 44 4f 43  ->reader, DL_DOC
2f424 49 44 53 2c 20 63 2d 3e 72 65 73 75 6c 74 2e 70  IDS, c->result.p
2f425 44 61 74 61 2c 20 63 2d 3e 72 65 73 75 6c 74 2e  Data, c->result.
2f426 6e 44 61 74 61 29 3b 0a 20 20 20 20 20 20 7d 0a  nData);.      }.
2f427 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
2f428 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
2f429 20 66 75 6c 6c 74 65 78 74 4e 65 78 74 28 70 43   fulltextNext(pC
2f42a 75 72 73 6f 72 29 3b 0a 7d 0a 0a 2f 2a 20 54 68  ursor);.}../* Th
2f42b 69 73 20 69 73 20 74 68 65 20 78 45 6f 66 20 6d  is is the xEof m
2f42c 65 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72  ethod of the vir
2f42d 74 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65  tual table.  The
2f42e 20 53 51 4c 69 74 65 20 63 6f 72 65 0a 2a 2a 20   SQLite core.** 
2f42f 63 61 6c 6c 73 20 74 68 69 73 20 72 6f 75 74 69  calls this routi
2f430 6e 65 20 74 6f 20 66 69 6e 64 20 6f 75 74 20 69  ne to find out i
2f431 66 20 69 74 20 68 61 73 20 72 65 61 63 68 65 64  f it has reached
2f432 20 74 68 65 20 65 6e 64 20 6f 66 0a 2a 2a 20 61   the end of.** a
2f433 20 71 75 65 72 79 27 73 20 72 65 73 75 6c 74 73   query's results
2f434 20 73 65 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20   set..*/.static 
2f435 69 6e 74 20 66 75 6c 6c 74 65 78 74 45 6f 66 28  int fulltextEof(
2f436 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
2f437 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b 0a 20  sor *pCursor){. 
2f438 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
2f439 20 2a 63 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f   *c = (fulltext_
2f43a 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72 73 6f  cursor *) pCurso
2f43b 72 3b 0a 20 20 72 65 74 75 72 6e 20 63 2d 3e 65  r;.  return c->e
2f43c 6f 66 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20 69  of;.}../* This i
2f43d 73 20 74 68 65 20 78 43 6f 6c 75 6d 6e 20 6d 65  s the xColumn me
2f43e 74 68 6f 64 20 6f 66 20 74 68 65 20 76 69 72 74  thod of the virt
2f43f 75 61 6c 20 74 61 62 6c 65 2e 20 20 54 68 65 20  ual table.  The 
2f440 53 51 4c 69 74 65 0a 2a 2a 20 63 6f 72 65 20 63  SQLite.** core c
2f441 61 6c 6c 73 20 74 68 69 73 20 6d 65 74 68 6f 64  alls this method
2f442 20 64 75 72 69 6e 67 20 61 20 71 75 65 72 79 20   during a query 
2f443 77 68 65 6e 20 69 74 20 6e 65 65 64 73 20 74 68  when it needs th
2f444 65 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 61 20  e value.** of a 
2f445 63 6f 6c 75 6d 6e 20 66 72 6f 6d 20 74 68 65 20  column from the 
2f446 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
2f447 54 68 69 73 20 6d 65 74 68 6f 64 20 6e 65 65 64  This method need
2f448 73 20 74 6f 20 75 73 65 0a 2a 2a 20 6f 6e 65 20  s to use.** one 
2f449 6f 66 20 74 68 65 20 73 71 6c 69 74 65 33 5f 72  of the sqlite3_r
2f44a 65 73 75 6c 74 5f 2a 28 29 20 72 6f 75 74 69 6e  esult_*() routin
2f44b 65 73 20 74 6f 20 73 74 6f 72 65 20 74 68 65 20  es to store the 
2f44c 72 65 71 75 65 73 74 65 64 0a 2a 2a 20 76 61 6c  requested.** val
2f44d 75 65 20 62 61 63 6b 20 69 6e 20 74 68 65 20 70  ue back in the p
2f44e 43 6f 6e 74 65 78 74 2e 0a 2a 2f 0a 73 74 61 74  Context..*/.stat
2f44f 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 43  ic int fulltextC
2f450 6f 6c 75 6d 6e 28 73 71 6c 69 74 65 33 5f 76 74  olumn(sqlite3_vt
2f451 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72 73  ab_cursor *pCurs
2f452 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  or,.            
2f453 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
2f454 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70  lite3_context *p
2f455 43 6f 6e 74 65 78 74 2c 20 69 6e 74 20 69 64 78  Context, int idx
2f456 43 6f 6c 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74  Col){.  fulltext
2f457 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20 28 66 75  _cursor *c = (fu
2f458 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 29  lltext_cursor *)
2f459 20 70 43 75 72 73 6f 72 3b 0a 20 20 66 75 6c 6c   pCursor;.  full
2f45a 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 63  text_vtab *v = c
2f45b 75 72 73 6f 72 5f 76 74 61 62 28 63 29 3b 0a 0a  ursor_vtab(c);..
2f45c 20 20 69 66 28 20 69 64 78 43 6f 6c 3c 76 2d 3e    if( idxCol<v->
2f45d 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 73  nColumn ){.    s
2f45e 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 70 56  qlite3_value *pV
2f45f 61 6c 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  al = sqlite3_col
2f460 75 6d 6e 5f 76 61 6c 75 65 28 63 2d 3e 70 53 74  umn_value(c->pSt
2f461 6d 74 2c 20 69 64 78 43 6f 6c 2b 31 29 3b 0a 20  mt, idxCol+1);. 
2f462 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
2f463 74 5f 76 61 6c 75 65 28 70 43 6f 6e 74 65 78 74  t_value(pContext
2f464 2c 20 70 56 61 6c 29 3b 0a 20 20 7d 65 6c 73 65  , pVal);.  }else
2f465 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e   if( idxCol==v->
2f466 6e 43 6f 6c 75 6d 6e 20 29 7b 0a 20 20 20 20 2f  nColumn ){.    /
2f467 2a 20 54 68 65 20 65 78 74 72 61 20 63 6f 6c 75  * The extra colu
2f468 6d 6e 20 77 68 6f 73 65 20 6e 61 6d 65 20 69 73  mn whose name is
2f469 20 74 68 65 20 73 61 6d 65 20 61 73 20 74 68 65   the same as the
2f46a 20 74 61 62 6c 65 2e 0a 20 20 20 20 2a 2a 20 52   table..    ** R
2f46b 65 74 75 72 6e 20 61 20 62 6c 6f 62 20 77 68 69  eturn a blob whi
2f46c 63 68 20 69 73 20 61 20 70 6f 69 6e 74 65 72 20  ch is a pointer 
2f46d 74 6f 20 74 68 65 20 63 75 72 73 6f 72 0a 20 20  to the cursor.  
2f46e 20 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65 33    */.    sqlite3
2f46f 5f 72 65 73 75 6c 74 5f 62 6c 6f 62 28 70 43 6f  _result_blob(pCo
2f470 6e 74 65 78 74 2c 20 26 63 2c 20 73 69 7a 65 6f  ntext, &c, sizeo
2f471 66 28 63 29 2c 20 53 51 4c 49 54 45 5f 54 52 41  f(c), SQLITE_TRA
2f472 4e 53 49 45 4e 54 29 3b 0a 20 20 7d 65 6c 73 65  NSIENT);.  }else
2f473 20 69 66 28 20 69 64 78 43 6f 6c 3d 3d 76 2d 3e   if( idxCol==v->
2f474 6e 43 6f 6c 75 6d 6e 2b 31 20 29 7b 0a 20 20 20  nColumn+1 ){.   
2f475 20 2f 2a 20 54 68 65 20 64 6f 63 69 64 20 63 6f   /* The docid co
2f476 6c 75 6d 6e 2c 20 77 68 69 63 68 20 69 73 20 61  lumn, which is a
2f477 6e 20 61 6c 69 61 73 20 66 6f 72 20 72 6f 77 69  n alias for rowi
2f478 64 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  d. */.    sqlite
2f479 33 5f 76 61 6c 75 65 20 2a 70 56 61 6c 20 3d 20  3_value *pVal = 
2f47a 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 76  sqlite3_column_v
2f47b 61 6c 75 65 28 63 2d 3e 70 53 74 6d 74 2c 20 30  alue(c->pStmt, 0
2f47c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
2f47d 65 73 75 6c 74 5f 76 61 6c 75 65 28 70 43 6f 6e  esult_value(pCon
2f47e 74 65 78 74 2c 20 70 56 61 6c 29 3b 0a 20 20 7d  text, pVal);.  }
2f47f 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
2f480 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 68 69 73 20  _OK;.}../* This 
2f481 69 73 20 74 68 65 20 78 52 6f 77 69 64 20 6d 65  is the xRowid me
2f482 74 68 6f 64 2e 20 20 54 68 65 20 53 51 4c 69 74  thod.  The SQLit
2f483 65 20 63 6f 72 65 20 63 61 6c 6c 73 20 74 68 69  e core calls thi
2f484 73 20 72 6f 75 74 69 6e 65 20 74 6f 0a 2a 2a 20  s routine to.** 
2f485 72 65 74 72 69 65 76 65 20 74 68 65 20 72 6f 77  retrieve the row
2f486 69 64 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  id for the curre
2f487 6e 74 20 72 6f 77 20 6f 66 20 74 68 65 20 72 65  nt row of the re
2f488 73 75 6c 74 20 73 65 74 2e 20 20 66 74 73 33 0a  sult set.  fts3.
2f489 2a 2a 20 65 78 70 6f 73 65 73 20 25 5f 63 6f 6e  ** exposes %_con
2f48a 74 65 6e 74 2e 64 6f 63 69 64 20 61 73 20 74 68  tent.docid as th
2f48b 65 20 72 6f 77 69 64 20 66 6f 72 20 74 68 65 20  e rowid for the 
2f48c 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 20 20  virtual table.  
2f48d 54 68 65 0a 2a 2a 20 72 6f 77 69 64 20 73 68 6f  The.** rowid sho
2f48e 75 6c 64 20 62 65 20 77 72 69 74 74 65 6e 20 74  uld be written t
2f48f 6f 20 2a 70 52 6f 77 69 64 2e 0a 2a 2f 0a 73 74  o *pRowid..*/.st
2f490 61 74 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78  atic int fulltex
2f491 74 52 6f 77 69 64 28 73 71 6c 69 74 65 33 5f 76  tRowid(sqlite3_v
2f492 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  tab_cursor *pCur
2f493 73 6f 72 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36  sor, sqlite_int6
2f494 34 20 2a 70 52 6f 77 69 64 29 7b 0a 20 20 66 75  4 *pRowid){.  fu
2f495 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72 20 2a 63  lltext_cursor *c
2f496 20 3d 20 28 66 75 6c 6c 74 65 78 74 5f 63 75 72   = (fulltext_cur
2f497 73 6f 72 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a  sor *) pCursor;.
2f498 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 73 71 6c  .  *pRowid = sql
2f499 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74 36  ite3_column_int6
2f49a 34 28 63 2d 3e 70 53 74 6d 74 2c 20 30 29 3b 0a  4(c->pStmt, 0);.
2f49b 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f49c 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 61 6c  OK;.}../* Add al
2f49d 6c 20 74 65 72 6d 73 20 69 6e 20 5b 7a 54 65 78  l terms in [zTex
2f49e 74 5d 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72  t] to pendingTer
2f49f 6d 73 20 74 61 62 6c 65 2e 20 20 49 66 20 5b 69  ms table.  If [i
2f4a0 43 6f 6c 75 6d 6e 5d 20 3e 20 30 2c 0a 2a 2a 20  Column] > 0,.** 
2f4a1 77 65 20 61 6c 73 6f 20 73 74 6f 72 65 20 70 6f  we also store po
2f4a2 73 69 74 69 6f 6e 73 20 61 6e 64 20 6f 66 66 73  sitions and offs
2f4a3 65 74 73 20 69 6e 20 74 68 65 20 68 61 73 68 20  ets in the hash 
2f4a4 74 61 62 6c 65 20 75 73 69 6e 67 20 74 68 61 74  table using that
2f4a5 0a 2a 2a 20 63 6f 6c 75 6d 6e 20 6e 75 6d 62 65  .** column numbe
2f4a6 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  r..*/.static int
2f4a7 20 62 75 69 6c 64 54 65 72 6d 73 28 66 75 6c 6c   buildTerms(full
2f4a8 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71  text_vtab *v, sq
2f4a9 6c 69 74 65 5f 69 6e 74 36 34 20 69 44 6f 63 69  lite_int64 iDoci
2f4aa 64 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  d,.             
2f4ab 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
2f4ac 68 61 72 20 2a 7a 54 65 78 74 2c 20 69 6e 74 20  har *zText, int 
2f4ad 69 43 6f 6c 75 6d 6e 29 7b 0a 20 20 73 71 6c 69  iColumn){.  sqli
2f4ae 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 70  te3_tokenizer *p
2f4af 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 76 2d 3e 70  Tokenizer = v->p
2f4b0 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 73 71 6c  Tokenizer;.  sql
2f4b1 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
2f4b2 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
2f4b3 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
2f4b4 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f 6b  oken;.  int nTok
2f4b5 65 6e 42 79 74 65 73 3b 0a 20 20 69 6e 74 20 69  enBytes;.  int i
2f4b6 53 74 61 72 74 4f 66 66 73 65 74 2c 20 69 45 6e  StartOffset, iEn
2f4b7 64 4f 66 66 73 65 74 2c 20 69 50 6f 73 69 74 69  dOffset, iPositi
2f4b8 6f 6e 3b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20  on;.  int rc;.. 
2f4b9 20 72 63 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72   rc = pTokenizer
2f4ba 2d 3e 70 4d 6f 64 75 6c 65 2d 3e 78 4f 70 65 6e  ->pModule->xOpen
2f4bb 28 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 7a 54 65  (pTokenizer, zTe
2f4bc 78 74 2c 20 2d 31 2c 20 26 70 43 75 72 73 6f 72  xt, -1, &pCursor
2f4bd 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2f4be 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2f4bf 72 63 3b 0a 0a 20 20 70 43 75 72 73 6f 72 2d 3e  rc;..  pCursor->
2f4c0 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 70 54 6f  pTokenizer = pTo
2f4c1 6b 65 6e 69 7a 65 72 3b 0a 20 20 77 68 69 6c 65  kenizer;.  while
2f4c2 28 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  ( SQLITE_OK==(rc
2f4c3 3d 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d 6f  =pTokenizer->pMo
2f4c4 64 75 6c 65 2d 3e 78 4e 65 78 74 28 70 43 75 72  dule->xNext(pCur
2f4c5 73 6f 72 2c 0a 20 20 20 20 20 20 20 20 20 20 20  sor,.           
2f4c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4c7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4c8 20 20 20 20 20 20 20 20 26 70 54 6f 6b 65 6e 2c          &pToken,
2f4c9 20 26 6e 54 6f 6b 65 6e 42 79 74 65 73 2c 0a 20   &nTokenBytes,. 
2f4ca 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4cb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4cc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4cd 20 20 26 69 53 74 61 72 74 4f 66 66 73 65 74 2c    &iStartOffset,
2f4ce 20 26 69 45 6e 64 4f 66 66 73 65 74 2c 0a 20 20   &iEndOffset,.  
2f4cf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4d0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4d1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f4d2 20 26 69 50 6f 73 69 74 69 6f 6e 29 29 20 29 7b   &iPosition)) ){
2f4d3 0a 20 20 20 20 44 4c 43 6f 6c 6c 65 63 74 6f 72  .    DLCollector
2f4d4 20 2a 70 3b 0a 20 20 20 20 69 6e 74 20 6e 44 61   *p;.    int nDa
2f4d5 74 61 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  ta;             
2f4d6 20 20 20 20 20 20 2f 2a 20 53 69 7a 65 20 6f 66        /* Size of
2f4d7 20 64 6f 63 6c 69 73 74 20 62 65 66 6f 72 65 20   doclist before 
2f4d8 6f 75 72 20 75 70 64 61 74 65 2e 20 2a 2f 0a 0a  our update. */..
2f4d9 20 20 20 20 2f 2a 20 50 6f 73 69 74 69 6f 6e 73      /* Positions
2f4da 20 63 61 6e 27 74 20 62 65 20 6e 65 67 61 74 69   can't be negati
2f4db 76 65 3b 20 77 65 20 75 73 65 20 2d 31 20 61 73  ve; we use -1 as
2f4dc 20 61 20 74 65 72 6d 69 6e 61 74 6f 72 0a 20 20   a terminator.  
2f4dd 20 20 20 2a 20 69 6e 74 65 72 6e 61 6c 6c 79 2e     * internally.
2f4de 20 20 54 6f 6b 65 6e 20 63 61 6e 27 74 20 62 65    Token can't be
2f4df 20 4e 55 4c 4c 20 6f 72 20 65 6d 70 74 79 2e 20   NULL or empty. 
2f4e0 2a 2f 0a 20 20 20 20 69 66 28 20 69 50 6f 73 69  */.    if( iPosi
2f4e1 74 69 6f 6e 3c 30 20 7c 7c 20 70 54 6f 6b 65 6e  tion<0 || pToken
2f4e2 20 3d 3d 20 4e 55 4c 4c 20 7c 7c 20 6e 54 6f 6b   == NULL || nTok
2f4e3 65 6e 42 79 74 65 73 20 3d 3d 20 30 20 29 7b 0a  enBytes == 0 ){.
2f4e4 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
2f4e5 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20 20 62  E_ERROR;.      b
2f4e6 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  reak;.    }..   
2f4e7 20 70 20 3d 20 66 74 73 33 48 61 73 68 46 69 6e   p = fts3HashFin
2f4e8 64 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72  d(&v->pendingTer
2f4e9 6d 73 2c 20 70 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  ms, pToken, nTok
2f4ea 65 6e 42 79 74 65 73 29 3b 0a 20 20 20 20 69 66  enBytes);.    if
2f4eb 28 20 70 3d 3d 4e 55 4c 4c 20 29 7b 0a 20 20 20  ( p==NULL ){.   
2f4ec 20 20 20 6e 44 61 74 61 20 3d 20 30 3b 0a 20 20     nData = 0;.  
2f4ed 20 20 20 20 70 20 3d 20 64 6c 63 4e 65 77 28 69      p = dlcNew(i
2f4ee 44 6f 63 69 64 2c 20 44 4c 5f 44 45 46 41 55 4c  Docid, DL_DEFAUL
2f4ef 54 29 3b 0a 20 20 20 20 20 20 66 74 73 33 48 61  T);.      fts3Ha
2f4f0 73 68 49 6e 73 65 72 74 28 26 76 2d 3e 70 65 6e  shInsert(&v->pen
2f4f1 64 69 6e 67 54 65 72 6d 73 2c 20 70 54 6f 6b 65  dingTerms, pToke
2f4f2 6e 2c 20 6e 54 6f 6b 65 6e 42 79 74 65 73 2c 20  n, nTokenBytes, 
2f4f3 70 29 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 76  p);..      /* Ov
2f4f4 65 72 68 65 61 64 20 66 6f 72 20 6f 75 72 20 68  erhead for our h
2f4f5 61 73 68 20 74 61 62 6c 65 20 65 6e 74 72 79 2c  ash table entry,
2f4f6 20 74 68 65 20 6b 65 79 2c 20 61 6e 64 20 74 68   the key, and th
2f4f7 65 20 76 61 6c 75 65 2e 20 2a 2f 0a 20 20 20 20  e value. */.    
2f4f8 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74    v->nPendingDat
2f4f9 61 20 2b 3d 20 73 69 7a 65 6f 66 28 73 74 72 75  a += sizeof(stru
2f4fa 63 74 20 66 74 73 33 48 61 73 68 45 6c 65 6d 29  ct fts3HashElem)
2f4fb 2b 73 69 7a 65 6f 66 28 2a 70 29 2b 6e 54 6f 6b  +sizeof(*p)+nTok
2f4fc 65 6e 42 79 74 65 73 3b 0a 20 20 20 20 7d 65 6c  enBytes;.    }el
2f4fd 73 65 7b 0a 20 20 20 20 20 20 6e 44 61 74 61 20  se{.      nData 
2f4fe 3d 20 70 2d 3e 62 2e 6e 44 61 74 61 3b 0a 20 20  = p->b.nData;.  
2f4ff 20 20 20 20 69 66 28 20 70 2d 3e 64 6c 77 2e 69      if( p->dlw.i
2f500 50 72 65 76 44 6f 63 69 64 21 3d 69 44 6f 63 69  PrevDocid!=iDoci
2f501 64 20 29 20 64 6c 63 4e 65 78 74 28 70 2c 20 69  d ) dlcNext(p, i
2f502 44 6f 63 69 64 29 3b 0a 20 20 20 20 7d 0a 20 20  Docid);.    }.  
2f503 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3e 3d 30    if( iColumn>=0
2f504 20 29 7b 0a 20 20 20 20 20 20 64 6c 63 41 64 64   ){.      dlcAdd
2f505 50 6f 73 28 70 2c 20 69 43 6f 6c 75 6d 6e 2c 20  Pos(p, iColumn, 
2f506 69 50 6f 73 69 74 69 6f 6e 2c 20 69 53 74 61 72  iPosition, iStar
2f507 74 4f 66 66 73 65 74 2c 20 69 45 6e 64 4f 66 66  tOffset, iEndOff
2f508 73 65 74 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20  set);.    }..   
2f509 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74 65 20 64   /* Accumulate d
2f50a 61 74 61 20 61 64 64 65 64 20 62 79 20 64 6c 63  ata added by dlc
2f50b 4e 65 77 20 6f 72 20 64 6c 63 4e 65 78 74 2c 20  New or dlcNext, 
2f50c 61 6e 64 20 64 6c 63 41 64 64 50 6f 73 2e 20 2a  and dlcAddPos. *
2f50d 2f 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e  /.    v->nPendin
2f50e 67 44 61 74 61 20 2b 3d 20 70 2d 3e 62 2e 6e 44  gData += p->b.nD
2f50f 61 74 61 2d 6e 44 61 74 61 3b 0a 20 20 7d 0a 0a  ata-nData;.  }..
2f510 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
2f511 20 43 68 65 63 6b 20 72 65 74 75 72 6e 3f 20 20   Check return?  
2f512 53 68 6f 75 6c 64 20 74 68 69 73 20 62 65 20 61  Should this be a
2f513 62 6c 65 20 74 6f 20 63 61 75 73 65 20 65 72 72  ble to cause err
2f514 6f 72 73 20 61 74 0a 20 20 2a 2a 20 74 68 69 73  ors at.  ** this
2f515 20 70 6f 69 6e 74 3f 20 20 41 63 74 75 61 6c 6c   point?  Actuall
2f516 79 2c 20 73 61 6d 65 20 71 75 65 73 74 69 6f 6e  y, same question
2f517 20 61 62 6f 75 74 20 73 71 6c 69 74 65 33 5f 66   about sqlite3_f
2f518 69 6e 61 6c 69 7a 65 28 29 2c 0a 20 20 2a 2a 20  inalize(),.  ** 
2f519 74 68 6f 75 67 68 20 6f 6e 65 20 63 6f 75 6c 64  though one could
2f51a 20 61 72 67 75 65 20 74 68 61 74 20 66 61 69 6c   argue that fail
2f51b 75 72 65 20 74 68 65 72 65 20 6d 65 61 6e 73 20  ure there means 
2f51c 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69 73  that the data is
2f51d 0a 20 20 2a 2a 20 6e 6f 74 20 64 75 72 61 62 6c  .  ** not durabl
2f51e 65 2e 20 20 2a 70 6f 6e 64 65 72 2a 0a 20 20 2a  e.  *ponder*.  *
2f51f 2f 0a 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e  /.  pTokenizer->
2f520 70 4d 6f 64 75 6c 65 2d 3e 78 43 6c 6f 73 65 28  pModule->xClose(
2f521 70 43 75 72 73 6f 72 29 3b 0a 20 20 69 66 28 20  pCursor);.  if( 
2f522 53 51 4c 49 54 45 5f 44 4f 4e 45 20 3d 3d 20 72  SQLITE_DONE == r
2f523 63 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  c ) return SQLIT
2f524 45 5f 4f 4b 3b 0a 20 20 72 65 74 75 72 6e 20 72  E_OK;.  return r
2f525 63 3b 0a 7d 0a 0a 2f 2a 20 41 64 64 20 64 6f 63  c;.}../* Add doc
2f526 6c 69 73 74 73 20 66 6f 72 20 61 6c 6c 20 74 65  lists for all te
2f527 72 6d 73 20 69 6e 20 5b 70 56 61 6c 75 65 73 5d  rms in [pValues]
2f528 20 74 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   to pendingTerms
2f529 20 74 61 62 6c 65 2e 20 2a 2f 0a 73 74 61 74 69   table. */.stati
2f52a 63 20 69 6e 74 20 69 6e 73 65 72 74 54 65 72 6d  c int insertTerm
2f52b 73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  s(fulltext_vtab 
2f52c 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  *v, sqlite_int64
2f52d 20 69 44 6f 63 69 64 2c 0a 20 20 20 20 20 20 20   iDocid,.       
2f52e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f52f 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
2f530 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e 74 20  pValues){.  int 
2f531 69 3b 0a 20 20 66 6f 72 28 69 20 3d 20 30 3b 20  i;.  for(i = 0; 
2f532 69 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 20 3b  i < v->nColumn ;
2f533 20 2b 2b 69 29 7b 0a 20 20 20 20 63 68 61 72 20   ++i){.    char 
2f534 2a 7a 54 65 78 74 20 3d 20 28 63 68 61 72 2a 29  *zText = (char*)
2f535 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
2f536 78 74 28 70 56 61 6c 75 65 73 5b 69 5d 29 3b 0a  xt(pValues[i]);.
2f537 20 20 20 20 69 6e 74 20 72 63 20 3d 20 62 75 69      int rc = bui
2f538 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f 63 69  ldTerms(v, iDoci
2f539 64 2c 20 7a 54 65 78 74 2c 20 69 29 3b 0a 20 20  d, zText, i);.  
2f53a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f53b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2f53c 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
2f53d 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 41  LITE_OK;.}../* A
2f53e 64 64 20 65 6d 70 74 79 20 64 6f 63 6c 69 73 74  dd empty doclist
2f53f 73 20 66 6f 72 20 61 6c 6c 20 74 65 72 6d 73 20  s for all terms 
2f540 69 6e 20 74 68 65 20 67 69 76 65 6e 20 72 6f 77  in the given row
2f541 27 73 20 63 6f 6e 74 65 6e 74 20 74 6f 0a 2a 2a  's content to.**
2f542 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a   pendingTerms..*
2f543 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c  /.static int del
2f544 65 74 65 54 65 72 6d 73 28 66 75 6c 6c 74 65 78  eteTerms(fulltex
2f545 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74  t_vtab *v, sqlit
2f546 65 5f 69 6e 74 36 34 20 69 44 6f 63 69 64 29 7b  e_int64 iDocid){
2f547 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
2f548 70 56 61 6c 75 65 73 3b 0a 20 20 69 6e 74 20 69  pValues;.  int i
2f549 2c 20 72 63 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f  , rc;..  /* TODO
2f54a 28 73 68 65 73 73 29 20 53 68 6f 75 6c 64 20 77  (shess) Should w
2f54b 65 20 61 6c 6c 6f 77 20 73 75 63 68 20 74 61 62  e allow such tab
2f54c 6c 65 73 20 61 74 20 61 6c 6c 3f 20 2a 2f 0a 20  les at all? */. 
2f54d 20 69 66 28 20 44 4c 5f 44 45 46 41 55 4c 54 3d   if( DL_DEFAULT=
2f54e 3d 44 4c 5f 44 4f 43 49 44 53 20 29 20 72 65 74  =DL_DOCIDS ) ret
2f54f 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
2f550 3b 0a 0a 20 20 72 63 20 3d 20 63 6f 6e 74 65 6e  ;..  rc = conten
2f551 74 5f 73 65 6c 65 63 74 28 76 2c 20 69 44 6f 63  t_select(v, iDoc
2f552 69 64 2c 20 26 70 56 61 6c 75 65 73 29 3b 0a 20  id, &pValues);. 
2f553 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
2f554 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
2f555 0a 20 20 66 6f 72 28 69 20 3d 20 30 20 3b 20 69  .  for(i = 0 ; i
2f556 20 3c 20 76 2d 3e 6e 43 6f 6c 75 6d 6e 3b 20 2b   < v->nColumn; +
2f557 2b 69 29 20 7b 0a 20 20 20 20 72 63 20 3d 20 62  +i) {.    rc = b
2f558 75 69 6c 64 54 65 72 6d 73 28 76 2c 20 69 44 6f  uildTerms(v, iDo
2f559 63 69 64 2c 20 70 56 61 6c 75 65 73 5b 69 5d 2c  cid, pValues[i],
2f55a 20 2d 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63   -1);.    if( rc
2f55b 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72  !=SQLITE_OK ) br
2f55c 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 66 72 65 65  eak;.  }..  free
2f55d 53 74 72 69 6e 67 41 72 72 61 79 28 76 2d 3e 6e  StringArray(v->n
2f55e 43 6f 6c 75 6d 6e 2c 20 70 56 61 6c 75 65 73 29  Column, pValues)
2f55f 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ;.  return SQLIT
2f560 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 4f 44 4f  E_OK;.}../* TODO
2f561 28 73 68 65 73 73 29 20 52 65 66 61 63 74 6f 72  (shess) Refactor
2f562 20 74 68 65 20 63 6f 64 65 20 74 6f 20 72 65 6d   the code to rem
2f563 6f 76 65 20 74 68 69 73 20 66 6f 72 77 61 72 64  ove this forward
2f564 20 64 65 63 6c 2e 20 2a 2f 0a 73 74 61 74 69 63   decl. */.static
2f565 20 69 6e 74 20 69 6e 69 74 50 65 6e 64 69 6e 67   int initPending
2f566 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
2f567 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69  tab *v, sqlite_i
2f568 6e 74 36 34 20 69 44 6f 63 69 64 29 3b 0a 0a 2f  nt64 iDocid);../
2f569 2a 20 49 6e 73 65 72 74 20 61 20 72 6f 77 20 69  * Insert a row i
2f56a 6e 74 6f 20 74 68 65 20 25 5f 63 6f 6e 74 65 6e  nto the %_conten
2f56b 74 20 74 61 62 6c 65 3b 20 73 65 74 20 2a 70 69  t table; set *pi
2f56c 44 6f 63 69 64 20 74 6f 20 62 65 20 74 68 65 20  Docid to be the 
2f56d 49 44 20 6f 66 20 74 68 65 0a 2a 2a 20 6e 65 77  ID of the.** new
2f56e 20 72 6f 77 2e 20 20 41 64 64 20 64 6f 63 6c 69   row.  Add docli
2f56f 73 74 73 20 66 6f 72 20 74 65 72 6d 73 20 74 6f  sts for terms to
2f570 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a   pendingTerms..*
2f571 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e 64  /.static int ind
2f572 65 78 5f 69 6e 73 65 72 74 28 66 75 6c 6c 74 65  ex_insert(fullte
2f573 78 74 5f 76 74 61 62 20 2a 76 2c 20 73 71 6c 69  xt_vtab *v, sqli
2f574 74 65 33 5f 76 61 6c 75 65 20 2a 70 52 65 71 75  te3_value *pRequ
2f575 65 73 74 44 6f 63 69 64 2c 0a 20 20 20 20 20 20  estDocid,.      
2f576 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f577 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2f578 2a 2a 70 56 61 6c 75 65 73 2c 20 73 71 6c 69 74  **pValues, sqlit
2f579 65 5f 69 6e 74 36 34 20 2a 70 69 44 6f 63 69 64  e_int64 *piDocid
2f57a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20  ){.  int rc;..  
2f57b 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 69 6e 73  rc = content_ins
2f57c 65 72 74 28 76 2c 20 70 52 65 71 75 65 73 74 44  ert(v, pRequestD
2f57d 6f 63 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 20  ocid, pValues); 
2f57e 20 2f 2a 20 65 78 65 63 75 74 65 20 61 6e 20 53   /* execute an S
2f57f 51 4c 20 49 4e 53 45 52 54 20 2a 2f 0a 20 20 69  QL INSERT */.  i
2f580 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
2f581 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20   ) return rc;.. 
2f582 20 2f 2a 20 64 6f 63 69 64 20 63 6f 6c 75 6d 6e   /* docid column
2f583 20 69 73 20 61 6e 20 61 6c 69 61 73 20 66 6f 72   is an alias for
2f584 20 72 6f 77 69 64 2e 20 2a 2f 0a 20 20 2a 70 69   rowid. */.  *pi
2f585 44 6f 63 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  Docid = sqlite3_
2f586 6c 61 73 74 5f 69 6e 73 65 72 74 5f 72 6f 77 69  last_insert_rowi
2f587 64 28 76 2d 3e 64 62 29 3b 0a 20 20 72 63 20 3d  d(v->db);.  rc =
2f588 20 69 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d   initPendingTerm
2f589 73 28 76 2c 20 2a 70 69 44 6f 63 69 64 29 3b 0a  s(v, *piDocid);.
2f58a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f58b 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2f58c 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 73 65 72  ..  return inser
2f58d 74 54 65 72 6d 73 28 76 2c 20 2a 70 69 44 6f 63  tTerms(v, *piDoc
2f58e 69 64 2c 20 70 56 61 6c 75 65 73 29 3b 0a 7d 0a  id, pValues);.}.
2f58f 0a 2f 2a 20 44 65 6c 65 74 65 20 61 20 72 6f 77  ./* Delete a row
2f590 20 66 72 6f 6d 20 74 68 65 20 25 5f 63 6f 6e 74   from the %_cont
2f591 65 6e 74 20 74 61 62 6c 65 3b 20 61 64 64 20 65  ent table; add e
2f592 6d 70 74 79 20 64 6f 63 6c 69 73 74 73 20 66 6f  mpty doclists fo
2f593 72 20 74 65 72 6d 73 0a 2a 2a 20 74 6f 20 70 65  r terms.** to pe
2f594 6e 64 69 6e 67 54 65 72 6d 73 2e 0a 2a 2f 0a 73  ndingTerms..*/.s
2f595 74 61 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f  tatic int index_
2f596 64 65 6c 65 74 65 28 66 75 6c 6c 74 65 78 74 5f  delete(fulltext_
2f597 76 74 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f  vtab *v, sqlite_
2f598 69 6e 74 36 34 20 69 52 6f 77 29 7b 0a 20 20 69  int64 iRow){.  i
2f599 6e 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e 64  nt rc = initPend
2f59a 69 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f 77  ingTerms(v, iRow
2f59b 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2f59c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2f59d 72 63 3b 0a 0a 20 20 72 63 20 3d 20 64 65 6c 65  rc;..  rc = dele
2f59e 74 65 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29  teTerms(v, iRow)
2f59f 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2f5a0 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2f5a1 63 3b 0a 0a 20 20 72 65 74 75 72 6e 20 63 6f 6e  c;..  return con
2f5a2 74 65 6e 74 5f 64 65 6c 65 74 65 28 76 2c 20 69  tent_delete(v, i
2f5a3 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63 75 74  Row);  /* execut
2f5a4 65 20 61 6e 20 53 51 4c 20 44 45 4c 45 54 45 20  e an SQL DELETE 
2f5a5 2a 2f 0a 7d 0a 0a 2f 2a 20 55 70 64 61 74 65 20  */.}../* Update 
2f5a6 61 20 72 6f 77 20 69 6e 20 74 68 65 20 25 5f 63  a row in the %_c
2f5a7 6f 6e 74 65 6e 74 20 74 61 62 6c 65 3b 20 61 64  ontent table; ad
2f5a8 64 20 64 65 6c 65 74 65 20 64 6f 63 6c 69 73 74  d delete doclist
2f5a9 73 20 74 6f 0a 2a 2a 20 70 65 6e 64 69 6e 67 54  s to.** pendingT
2f5aa 65 72 6d 73 20 66 6f 72 20 6f 6c 64 20 74 65 72  erms for old ter
2f5ab 6d 73 20 6e 6f 74 20 69 6e 20 74 68 65 20 6e 65  ms not in the ne
2f5ac 77 20 64 61 74 61 2c 20 61 64 64 20 69 6e 73 65  w data, add inse
2f5ad 72 74 20 64 6f 63 6c 69 73 74 73 0a 2a 2a 20 74  rt doclists.** t
2f5ae 6f 20 70 65 6e 64 69 6e 67 54 65 72 6d 73 20 66  o pendingTerms f
2f5af 6f 72 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20  or terms in the 
2f5b0 6e 65 77 20 64 61 74 61 2e 0a 2a 2f 0a 73 74 61  new data..*/.sta
2f5b1 74 69 63 20 69 6e 74 20 69 6e 64 65 78 5f 75 70  tic int index_up
2f5b2 64 61 74 65 28 66 75 6c 6c 74 65 78 74 5f 76 74  date(fulltext_vt
2f5b3 61 62 20 2a 76 2c 20 73 71 6c 69 74 65 5f 69 6e  ab *v, sqlite_in
2f5b4 74 36 34 20 69 52 6f 77 2c 0a 20 20 20 20 20 20  t64 iRow,.      
2f5b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f5b6 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
2f5b7 2a 2a 70 56 61 6c 75 65 73 29 7b 0a 20 20 69 6e  **pValues){.  in
2f5b8 74 20 72 63 20 3d 20 69 6e 69 74 50 65 6e 64 69  t rc = initPendi
2f5b9 6e 67 54 65 72 6d 73 28 76 2c 20 69 52 6f 77 29  ngTerms(v, iRow)
2f5ba 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
2f5bb 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2f5bc 63 3b 0a 0a 20 20 2f 2a 20 47 65 6e 65 72 61 74  c;..  /* Generat
2f5bd 65 20 61 6e 20 65 6d 70 74 79 20 64 6f 63 6c 69  e an empty docli
2f5be 73 74 20 66 6f 72 20 65 61 63 68 20 74 65 72 6d  st for each term
2f5bf 20 74 68 61 74 20 70 72 65 76 69 6f 75 73 6c 79   that previously
2f5c0 20 61 70 70 65 61 72 65 64 20 69 6e 20 74 68 69   appeared in thi
2f5c1 73 0a 20 20 20 2a 20 72 6f 77 2e 20 2a 2f 0a 20  s.   * row. */. 
2f5c2 20 72 63 20 3d 20 64 65 6c 65 74 65 54 65 72 6d   rc = deleteTerm
2f5c3 73 28 76 2c 20 69 52 6f 77 29 3b 0a 20 20 69 66  s(v, iRow);.  if
2f5c4 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
2f5c5 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
2f5c6 72 63 20 3d 20 63 6f 6e 74 65 6e 74 5f 75 70 64  rc = content_upd
2f5c7 61 74 65 28 76 2c 20 70 56 61 6c 75 65 73 2c 20  ate(v, pValues, 
2f5c8 69 52 6f 77 29 3b 20 20 2f 2a 20 65 78 65 63 75  iRow);  /* execu
2f5c9 74 65 20 61 6e 20 53 51 4c 20 55 50 44 41 54 45  te an SQL UPDATE
2f5ca 20 2a 2f 0a 20 20 69 66 28 20 72 63 21 3d 53 51   */.  if( rc!=SQ
2f5cb 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2f5cc 20 72 63 3b 0a 0a 20 20 2f 2a 20 4e 6f 77 20 61   rc;..  /* Now a
2f5cd 64 64 20 70 6f 73 69 74 69 6f 6e 73 20 66 6f 72  dd positions for
2f5ce 20 74 65 72 6d 73 20 77 68 69 63 68 20 61 70 70   terms which app
2f5cf 65 61 72 20 69 6e 20 74 68 65 20 75 70 64 61 74  ear in the updat
2f5d0 65 64 20 72 6f 77 2e 20 2a 2f 0a 20 20 72 65 74  ed row. */.  ret
2f5d1 75 72 6e 20 69 6e 73 65 72 74 54 65 72 6d 73 28  urn insertTerms(
2f5d2 76 2c 20 69 52 6f 77 2c 20 70 56 61 6c 75 65 73  v, iRow, pValues
2f5d3 29 3b 0a 7d 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a  );.}../*********
2f5d4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f5d5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f5d6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f5d7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 49  **********/./* I
2f5d8 6e 74 65 72 69 6f 72 57 72 69 74 65 72 20 69 73  nteriorWriter is
2f5d9 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c 65 63 74   used to collect
2f5da 20 74 65 72 6d 73 20 61 6e 64 20 62 6c 6f 63 6b   terms and block
2f5db 20 72 65 66 65 72 65 6e 63 65 73 20 69 6e 74 6f   references into
2f5dc 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64  .** interior nod
2f5dd 65 73 20 69 6e 20 25 5f 73 65 67 6d 65 6e 74 73  es in %_segments
2f5de 2e 20 20 53 65 65 20 63 6f 6d 6d 65 6e 74 61 72  .  See commentar
2f5df 79 20 61 74 20 74 6f 70 20 6f 66 20 66 69 6c 65  y at top of file
2f5e0 20 66 6f 72 0a 2a 2a 20 66 6f 72 6d 61 74 2e 0a   for.** format..
2f5e1 2a 2f 0a 0a 2f 2a 20 48 6f 77 20 6c 61 72 67 65  */../* How large
2f5e2 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
2f5e3 63 61 6e 20 67 72 6f 77 2e 20 2a 2f 0a 23 64 65  can grow. */.#de
2f5e4 66 69 6e 65 20 49 4e 54 45 52 49 4f 52 5f 4d 41  fine INTERIOR_MA
2f5e5 58 20 32 30 34 38 0a 0a 2f 2a 20 4d 69 6e 69 6d  X 2048../* Minim
2f5e6 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 74 65 72  um number of ter
2f5e7 6d 73 20 70 65 72 20 69 6e 74 65 72 69 6f 72 20  ms per interior 
2f5e8 6e 6f 64 65 20 28 65 78 63 65 70 74 20 74 68 65  node (except the
2f5e9 20 72 6f 6f 74 29 2e 20 54 68 69 73 0a 2a 2a 20   root). This.** 
2f5ea 70 72 65 76 65 6e 74 73 20 6c 61 72 67 65 20 74  prevents large t
2f5eb 65 72 6d 73 20 66 72 6f 6d 20 6d 61 6b 69 6e 67  erms from making
2f5ec 20 74 68 65 20 74 72 65 65 20 74 6f 6f 20 73 6b   the tree too sk
2f5ed 69 6e 6e 79 20 2d 20 6d 75 73 74 20 62 65 20 3e  inny - must be >
2f5ee 30 0a 2a 2a 20 73 6f 20 74 68 61 74 20 74 68 65  0.** so that the
2f5ef 20 74 72 65 65 20 61 6c 77 61 79 73 20 6d 61 6b   tree always mak
2f5f0 65 73 20 70 72 6f 67 72 65 73 73 2e 20 20 4e 6f  es progress.  No
2f5f1 74 65 20 74 68 61 74 20 74 68 65 20 6d 69 6e 20  te that the min 
2f5f2 74 72 65 65 0a 2a 2a 20 66 61 6e 6f 75 74 20 77  tree.** fanout w
2f5f3 69 6c 6c 20 62 65 20 49 4e 54 45 52 49 4f 52 5f  ill be INTERIOR_
2f5f4 4d 49 4e 5f 54 45 52 4d 53 2b 31 2e 0a 2a 2f 0a  MIN_TERMS+1..*/.
2f5f5 23 64 65 66 69 6e 65 20 49 4e 54 45 52 49 4f 52  #define INTERIOR
2f5f6 5f 4d 49 4e 5f 54 45 52 4d 53 20 37 0a 23 69 66  _MIN_TERMS 7.#if
2f5f7 20 49 4e 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45   INTERIOR_MIN_TE
2f5f8 52 4d 53 3c 31 0a 23 20 65 72 72 6f 72 20 49 4e  RMS<1.# error IN
2f5f9 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
2f5fa 20 6d 75 73 74 20 62 65 20 67 72 65 61 74 65 72   must be greater
2f5fb 20 74 68 61 6e 20 30 2e 0a 23 65 6e 64 69 66 0a   than 0..#endif.
2f5fc 0a 2f 2a 20 52 4f 4f 54 5f 4d 41 58 20 63 6f 6e  ./* ROOT_MAX con
2f5fd 74 72 6f 6c 73 20 68 6f 77 20 6d 75 63 68 20 64  trols how much d
2f5fe 61 74 61 20 69 73 20 73 74 6f 72 65 64 20 69 6e  ata is stored in
2f5ff 6c 69 6e 65 20 69 6e 20 74 68 65 20 73 65 67 6d  line in the segm
2f600 65 6e 74 0a 2a 2a 20 64 69 72 65 63 74 6f 72 79  ent.** directory
2f601 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
2f602 73 73 29 20 50 75 73 68 20 52 4f 4f 54 5f 4d 41  ss) Push ROOT_MA
2f603 58 20 64 6f 77 6e 20 74 6f 20 77 68 6f 65 76 65  X down to whoeve
2f604 72 20 69 73 20 77 72 69 74 69 6e 67 20 74 68 69  r is writing thi
2f605 6e 67 73 2e 20 20 49 74 27 73 0a 2a 2a 20 6f 6e  ngs.  It's.** on
2f606 6c 79 20 68 65 72 65 20 73 6f 20 74 68 61 74 20  ly here so that 
2f607 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 52 6f  interiorWriterRo
2f608 6f 74 49 6e 66 6f 28 29 20 61 6e 64 20 6c 65 61  otInfo() and lea
2f609 66 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28  fWriterRootInfo(
2f60a 29 0a 2a 2a 20 63 61 6e 20 62 6f 74 68 20 73 65  ).** can both se
2f60b 65 20 69 74 2c 20 62 75 74 20 69 66 20 74 68 65  e it, but if the
2f60c 20 63 61 6c 6c 65 72 20 70 61 73 73 65 64 20 69   caller passed i
2f60d 74 20 69 6e 2c 20 77 65 20 77 6f 75 6c 64 6e 27  t in, we wouldn'
2f60e 74 20 65 76 65 6e 0a 2a 2a 20 6e 65 65 64 20 61  t even.** need a
2f60f 20 64 65 66 69 6e 65 2e 0a 2a 2f 0a 23 64 65 66   define..*/.#def
2f610 69 6e 65 20 52 4f 4f 54 5f 4d 41 58 20 31 30 32  ine ROOT_MAX 102
2f611 34 0a 23 69 66 20 52 4f 4f 54 5f 4d 41 58 3c 56  4.#if ROOT_MAX<V
2f612 41 52 49 4e 54 5f 4d 41 58 2a 32 0a 23 20 65 72  ARINT_MAX*2.# er
2f613 72 6f 72 20 52 4f 4f 54 5f 4d 41 58 20 6d 75 73  ror ROOT_MAX mus
2f614 74 20 68 61 76 65 20 65 6e 6f 75 67 68 20 73 70  t have enough sp
2f615 61 63 65 20 66 6f 72 20 61 20 68 65 61 64 65 72  ace for a header
2f616 2e 0a 23 65 6e 64 69 66 0a 0a 2f 2a 20 49 6e 74  ..#endif../* Int
2f617 65 72 69 6f 72 42 6c 6f 63 6b 20 73 74 6f 72 65  eriorBlock store
2f618 73 20 61 20 6c 69 6e 6b 65 64 2d 6c 69 73 74 20  s a linked-list 
2f619 6f 66 20 69 6e 74 65 72 69 6f 72 20 62 6c 6f 63  of interior bloc
2f61a 6b 73 20 77 68 69 6c 65 20 61 20 6c 6f 77 65 72  ks while a lower
2f61b 0a 2a 2a 20 6c 61 79 65 72 20 69 73 20 62 65 69  .** layer is bei
2f61c 6e 67 20 63 6f 6e 73 74 72 75 63 74 65 64 2e 0a  ng constructed..
2f61d 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
2f61e 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20  t InteriorBlock 
2f61f 7b 0a 20 20 44 61 74 61 42 75 66 66 65 72 20 74  {.  DataBuffer t
2f620 65 72 6d 3b 20 20 20 20 20 20 20 20 20 20 20 2f  erm;           /
2f621 2a 20 4c 65 66 74 6d 6f 73 74 20 74 65 72 6d 20  * Leftmost term 
2f622 69 6e 20 62 6c 6f 63 6b 27 73 20 73 75 62 74 72  in block's subtr
2f623 65 65 2e 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  ee. */.  DataBuf
2f624 66 65 72 20 64 61 74 61 3b 20 20 20 20 20 20 20  fer data;       
2f625 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
2f626 65 64 20 64 61 74 61 20 66 6f 72 20 74 68 65 20  ed data for the 
2f627 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 73 74 72 75  block. */.  stru
2f628 63 74 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  ct InteriorBlock
2f629 20 2a 6e 65 78 74 3b 0a 7d 20 49 6e 74 65 72 69   *next;.} Interi
2f62a 6f 72 42 6c 6f 63 6b 3b 0a 0a 73 74 61 74 69 63  orBlock;..static
2f62b 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a   InteriorBlock *
2f62c 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77  interiorBlockNew
2f62d 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20 73 71  (int iHeight, sq
2f62e 6c 69 74 65 5f 69 6e 74 36 34 20 69 43 68 69 6c  lite_int64 iChil
2f62f 64 42 6c 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20  dBlock,.        
2f630 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f631 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63                 c
2f632 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
2f633 2c 20 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20  , int nTerm){.  
2f634 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
2f635 6c 6f 63 6b 20 3d 20 73 71 6c 69 74 65 33 5f 6d  lock = sqlite3_m
2f636 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 49 6e 74  alloc(sizeof(Int
2f637 65 72 69 6f 72 42 6c 6f 63 6b 29 29 3b 0a 20 20  eriorBlock));.  
2f638 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41  char c[VARINT_MA
2f639 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20  X+VARINT_MAX];. 
2f63a 20 69 6e 74 20 6e 3b 0a 0a 20 20 69 66 28 20 62   int n;..  if( b
2f63b 6c 6f 63 6b 20 29 7b 0a 20 20 20 20 6d 65 6d 73  lock ){.    mems
2f63c 65 74 28 62 6c 6f 63 6b 2c 20 30 2c 20 73 69 7a  et(block, 0, siz
2f63d 65 6f 66 28 2a 62 6c 6f 63 6b 29 29 3b 0a 20 20  eof(*block));.  
2f63e 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
2f63f 28 26 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2c 20 30  (&block->term, 0
2f640 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  );.    dataBuffe
2f641 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63 6b 2d  rReplace(&block-
2f642 3e 74 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 54  >term, pTerm, nT
2f643 65 72 6d 29 3b 0a 0a 20 20 20 20 6e 20 3d 20 66  erm);..    n = f
2f644 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
2f645 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 6e 20  iHeight);.    n 
2f646 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e 74  += fts3PutVarint
2f647 28 63 2b 6e 2c 20 69 43 68 69 6c 64 42 6c 6f 63  (c+n, iChildBloc
2f648 6b 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  k);.    dataBuff
2f649 65 72 49 6e 69 74 28 26 62 6c 6f 63 6b 2d 3e 64  erInit(&block->d
2f64a 61 74 61 2c 20 49 4e 54 45 52 49 4f 52 5f 4d 41  ata, INTERIOR_MA
2f64b 58 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66  X);.    dataBuff
2f64c 65 72 52 65 70 6c 61 63 65 28 26 62 6c 6f 63 6b  erReplace(&block
2f64d 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 29 3b 0a 20  ->data, c, n);. 
2f64e 20 7d 0a 20 20 72 65 74 75 72 6e 20 62 6c 6f 63   }.  return bloc
2f64f 6b 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20 4e 44  k;.}..#ifndef ND
2f650 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79 20 74  EBUG./* Verify t
2f651 68 61 74 20 74 68 65 20 64 61 74 61 20 69 73 20  hat the data is 
2f652 72 65 61 64 61 62 6c 65 20 61 73 20 61 6e 20 69  readable as an i
2f653 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2e 20 2a 2f  nterior node. */
2f654 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e 74  .static void int
2f655 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c 69 64 61  eriorBlockValida
2f656 74 65 28 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  te(InteriorBlock
2f657 20 2a 70 42 6c 6f 63 6b 29 7b 0a 20 20 63 6f 6e   *pBlock){.  con
2f658 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 20 3d  st char *pData =
2f659 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e 70 44   pBlock->data.pD
2f65a 61 74 61 3b 0a 20 20 69 6e 74 20 6e 44 61 74 61  ata;.  int nData
2f65b 20 3d 20 70 42 6c 6f 63 6b 2d 3e 64 61 74 61 2e   = pBlock->data.
2f65c 6e 44 61 74 61 3b 0a 20 20 69 6e 74 20 6e 2c 20  nData;.  int n, 
2f65d 69 44 75 6d 6d 79 3b 0a 20 20 73 71 6c 69 74 65  iDummy;.  sqlite
2f65e 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69 64 3b  _int64 iBlockid;
2f65f 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74  ..  assert( nDat
2f660 61 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28  a>0 );.  assert(
2f661 20 70 44 61 74 61 21 3d 30 20 29 3b 0a 20 20 61   pData!=0 );.  a
2f662 73 73 65 72 74 28 20 70 44 61 74 61 2b 6e 44 61  ssert( pData+nDa
2f663 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a 20 20 2f  ta>pData );..  /
2f664 2a 20 4d 75 73 74 20 6c 65 61 64 20 77 69 74 68  * Must lead with
2f665 20 68 65 69 67 68 74 20 6f 66 20 6e 6f 64 65 20   height of node 
2f666 61 73 20 61 20 76 61 72 69 6e 74 28 6e 29 2c 20  as a varint(n), 
2f667 6e 3e 30 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73  n>0 */.  n = fts
2f668 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61  3GetVarint32(pDa
2f669 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20  ta, &iDummy);.  
2f66a 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20  assert( n>0 );. 
2f66b 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e   assert( iDummy>
2f66c 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  0 );.  assert( n
2f66d 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 44 61 74  <nData );.  pDat
2f66e 61 20 2b 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20  a += n;.  nData 
2f66f 2d 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75 73 74  -= n;..  /* Must
2f670 20 63 6f 6e 74 61 69 6e 20 69 42 6c 6f 63 6b 69   contain iBlocki
2f671 64 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33  d. */.  n = fts3
2f672 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2c  GetVarint(pData,
2f673 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 61   &iBlockid);.  a
2f674 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20  ssert( n>0 );.  
2f675 61 73 73 65 72 74 28 20 6e 3c 3d 6e 44 61 74 61  assert( n<=nData
2f676 20 29 3b 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e   );.  pData += n
2f677 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a  ;.  nData -= n;.
2f678 0a 20 20 2f 2a 20 5a 65 72 6f 20 6f 72 20 6d 6f  .  /* Zero or mo
2f679 72 65 20 74 65 72 6d 73 20 6f 66 20 70 6f 73 69  re terms of posi
2f67a 74 69 76 65 20 6c 65 6e 67 74 68 20 2a 2f 0a 20  tive length */. 
2f67b 20 69 66 28 20 6e 44 61 74 61 21 3d 30 20 29 7b   if( nData!=0 ){
2f67c 0a 20 20 20 20 2f 2a 20 46 69 72 73 74 20 74 65  .    /* First te
2f67d 72 6d 20 69 73 20 6e 6f 74 20 64 65 6c 74 61 2d  rm is not delta-
2f67e 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a 20 20 20 20  encoded. */.    
2f67f 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e  n = fts3GetVarin
2f680 74 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d  t32(pData, &iDum
2f681 6d 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  my);.    assert(
2f682 20 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65   n>0 );.    asse
2f683 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a  rt( iDummy>0 );.
2f684 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44      assert( n+iD
2f685 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 61 73 73  ummy>0);.    ass
2f686 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
2f687 44 61 74 61 20 29 3b 0a 20 20 20 20 70 44 61 74  Data );.    pDat
2f688 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20  a += n+iDummy;. 
2f689 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44     nData -= n+iD
2f68a 75 6d 6d 79 3b 0a 0a 20 20 20 20 2f 2a 20 46 6f  ummy;..    /* Fo
2f68b 6c 6c 6f 77 69 6e 67 20 74 65 72 6d 73 20 64 65  llowing terms de
2f68c 6c 74 61 2d 65 6e 63 6f 64 65 64 2e 20 2a 2f 0a  lta-encoded. */.
2f68d 20 20 20 20 77 68 69 6c 65 28 20 6e 44 61 74 61      while( nData
2f68e 21 3d 30 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  !=0 ){.      /* 
2f68f 4c 65 6e 67 74 68 20 6f 66 20 73 68 61 72 65 64  Length of shared
2f690 20 70 72 65 66 69 78 2e 20 2a 2f 0a 20 20 20 20   prefix. */.    
2f691 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61 72    n = fts3GetVar
2f692 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26 69 44  int32(pData, &iD
2f693 75 6d 6d 79 29 3b 0a 20 20 20 20 20 20 61 73 73  ummy);.      ass
2f694 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20  ert( n>0 );.    
2f695 20 20 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79    assert( iDummy
2f696 3e 3d 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73  >=0 );.      ass
2f697 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a  ert( n<nData );.
2f698 20 20 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e        pData += n
2f699 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20 2d 3d  ;.      nData -=
2f69a 20 6e 3b 0a 0a 20 20 20 20 20 20 2f 2a 20 4c 65   n;..      /* Le
2f69b 6e 67 74 68 20 61 6e 64 20 64 61 74 61 20 6f 66  ngth and data of
2f69c 20 64 69 73 74 69 6e 63 74 20 73 75 66 66 69 78   distinct suffix
2f69d 2e 20 2a 2f 0a 20 20 20 20 20 20 6e 20 3d 20 66  . */.      n = f
2f69e 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2f69f 44 61 74 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a  Data, &iDummy);.
2f6a0 20 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 3e        assert( n>
2f6a1 30 20 29 3b 0a 20 20 20 20 20 20 61 73 73 65 72  0 );.      asser
2f6a2 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20  t( iDummy>0 );. 
2f6a3 20 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69       assert( n+i
2f6a4 44 75 6d 6d 79 3e 30 29 3b 0a 20 20 20 20 20 20  Dummy>0);.      
2f6a5 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79  assert( n+iDummy
2f6a6 3c 3d 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 20  <=nData );.     
2f6a7 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d   pData += n+iDum
2f6a8 6d 79 3b 0a 20 20 20 20 20 20 6e 44 61 74 61 20  my;.      nData 
2f6a9 2d 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20  -= n+iDummy;.   
2f6aa 20 7d 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65   }.  }.}.#define
2f6ab 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e   ASSERT_VALID_IN
2f6ac 54 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 78 29 20  TERIOR_BLOCK(x) 
2f6ad 69 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 56 61 6c  interiorBlockVal
2f6ae 69 64 61 74 65 28 78 29 0a 23 65 6c 73 65 0a 23  idate(x).#else.#
2f6af 64 65 66 69 6e 65 20 41 53 53 45 52 54 5f 56 41  define ASSERT_VA
2f6b0 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
2f6b1 43 4b 28 78 29 20 61 73 73 65 72 74 28 20 31 20  CK(x) assert( 1 
2f6b2 29 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65 64 65  ).#endif..typede
2f6b3 66 20 73 74 72 75 63 74 20 49 6e 74 65 72 69 6f  f struct Interio
2f6b4 72 57 72 69 74 65 72 20 7b 0a 20 20 69 6e 74 20  rWriter {.  int 
2f6b5 69 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20  iHeight;        
2f6b6 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 66 72             /* fr
2f6b7 6f 6d 20 30 20 61 74 20 6c 65 61 76 65 73 2e 20  om 0 at leaves. 
2f6b8 2a 2f 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f  */.  InteriorBlo
2f6b9 63 6b 20 2a 66 69 72 73 74 2c 20 2a 6c 61 73 74  ck *first, *last
2f6ba 3b 0a 20 20 73 74 72 75 63 74 20 49 6e 74 65 72  ;.  struct Inter
2f6bb 69 6f 72 57 72 69 74 65 72 20 2a 70 61 72 65 6e  iorWriter *paren
2f6bc 74 57 72 69 74 65 72 3b 0a 0a 20 20 44 61 74 61  tWriter;..  Data
2f6bd 42 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20  Buffer term;    
2f6be 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4c 61             /* La
2f6bf 73 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e 20  st term written 
2f6c0 74 6f 20 62 6c 6f 63 6b 20 22 6c 61 73 74 22 2e  to block "last".
2f6c1 20 2a 2f 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74   */.  sqlite_int
2f6c2 36 34 20 69 4f 70 65 6e 69 6e 67 43 68 69 6c 64  64 iOpeningChild
2f6c3 42 6c 6f 63 6b 3b 20 2f 2a 20 46 69 72 73 74 20  Block; /* First 
2f6c4 63 68 69 6c 64 20 62 6c 6f 63 6b 20 69 6e 20 62  child block in b
2f6c5 6c 6f 63 6b 20 22 6c 61 73 74 22 2e 20 2a 2f 0a  lock "last". */.
2f6c6 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
2f6c7 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 4c   sqlite_int64 iL
2f6c8 61 73 74 43 68 69 6c 64 42 6c 6f 63 6b 3b 20 20  astChildBlock;  
2f6c9 2f 2a 20 66 6f 72 20 63 6f 6e 73 69 73 74 65 6e  /* for consisten
2f6ca 63 79 20 63 68 65 63 6b 73 2e 20 2a 2f 0a 23 65  cy checks. */.#e
2f6cb 6e 64 69 66 0a 7d 20 49 6e 74 65 72 69 6f 72 57  ndif.} InteriorW
2f6cc 72 69 74 65 72 3b 0a 0a 2f 2a 20 49 6e 69 74 69  riter;../* Initi
2f6cd 61 6c 69 7a 65 20 61 6e 20 69 6e 74 65 72 69 6f  alize an interio
2f6ce 72 20 6e 6f 64 65 20 77 68 65 72 65 20 70 54 65  r node where pTe
2f6cf 72 6d 5b 6e 54 65 72 6d 5d 20 6d 61 72 6b 73 20  rm[nTerm] marks 
2f6d0 74 68 65 20 6c 65 66 74 6d 6f 73 74 0a 2a 2a 20  the leftmost.** 
2f6d1 74 65 72 6d 20 69 6e 20 74 68 65 20 74 72 65 65  term in the tree
2f6d2 2e 20 20 69 43 68 69 6c 64 42 6c 6f 63 6b 20 69  .  iChildBlock i
2f6d3 73 20 74 68 65 20 6c 65 66 74 6d 6f 73 74 20 63  s the leftmost c
2f6d4 68 69 6c 64 20 62 6c 6f 63 6b 20 61 74 20 74 68  hild block at th
2f6d5 65 0a 2a 2a 20 6e 65 78 74 20 6c 65 76 65 6c 20  e.** next level 
2f6d6 64 6f 77 6e 20 74 68 65 20 74 72 65 65 2e 0a 2a  down the tree..*
2f6d7 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 6e  /.static void in
2f6d8 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74  teriorWriterInit
2f6d9 28 69 6e 74 20 69 48 65 69 67 68 74 2c 20 63 6f  (int iHeight, co
2f6da 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c  nst char *pTerm,
2f6db 20 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20   int nTerm,.    
2f6dc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f6dd 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2f6de 65 5f 69 6e 74 36 34 20 69 43 68 69 6c 64 42 6c  e_int64 iChildBl
2f6df 6f 63 6b 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ock,.           
2f6e0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f6e1 20 20 20 20 49 6e 74 65 72 69 6f 72 57 72 69 74      InteriorWrit
2f6e2 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
2f6e3 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
2f6e4 6c 6f 63 6b 3b 0a 20 20 61 73 73 65 72 74 28 20  lock;.  assert( 
2f6e5 69 48 65 69 67 68 74 3e 30 20 29 3b 0a 20 20 43  iHeight>0 );.  C
2f6e6 4c 45 41 52 28 70 57 72 69 74 65 72 29 3b 0a 0a  LEAR(pWriter);..
2f6e7 20 20 70 57 72 69 74 65 72 2d 3e 69 48 65 69 67    pWriter->iHeig
2f6e8 68 74 20 3d 20 69 48 65 69 67 68 74 3b 0a 20 20  ht = iHeight;.  
2f6e9 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e 69 6e  pWriter->iOpenin
2f6ea 67 43 68 69 6c 64 42 6c 6f 63 6b 20 3d 20 69 43  gChildBlock = iC
2f6eb 68 69 6c 64 42 6c 6f 63 6b 3b 0a 23 69 66 6e 64  hildBlock;.#ifnd
2f6ec 65 66 20 4e 44 45 42 55 47 0a 20 20 70 57 72 69  ef NDEBUG.  pWri
2f6ed 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c 64 42  ter->iLastChildB
2f6ee 6c 6f 63 6b 20 3d 20 69 43 68 69 6c 64 42 6c 6f  lock = iChildBlo
2f6ef 63 6b 3b 0a 23 65 6e 64 69 66 0a 20 20 62 6c 6f  ck;.#endif.  blo
2f6f0 63 6b 20 3d 20 69 6e 74 65 72 69 6f 72 42 6c 6f  ck = interiorBlo
2f6f1 63 6b 4e 65 77 28 69 48 65 69 67 68 74 2c 20 69  ckNew(iHeight, i
2f6f2 43 68 69 6c 64 42 6c 6f 63 6b 2c 20 70 54 65 72  ChildBlock, pTer
2f6f3 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 70 57 72  m, nTerm);.  pWr
2f6f4 69 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57 72  iter->last = pWr
2f6f5 69 74 65 72 2d 3e 66 69 72 73 74 20 3d 20 62 6c  iter->first = bl
2f6f6 6f 63 6b 3b 0a 20 20 41 53 53 45 52 54 5f 56 41  ock;.  ASSERT_VA
2f6f7 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f  LID_INTERIOR_BLO
2f6f8 43 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73 74  CK(pWriter->last
2f6f9 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72 49  );.  dataBufferI
2f6fa 6e 69 74 28 26 70 57 72 69 74 65 72 2d 3e 74 65  nit(&pWriter->te
2f6fb 72 6d 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a 20 41 70  rm, 0);.}../* Ap
2f6fc 70 65 6e 64 20 74 68 65 20 63 68 69 6c 64 20 6e  pend the child n
2f6fd 6f 64 65 20 72 6f 6f 74 65 64 20 61 74 20 69 43  ode rooted at iC
2f6fe 68 69 6c 64 42 6c 6f 63 6b 20 74 6f 20 74 68 65  hildBlock to the
2f6ff 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 2c 0a   interior node,.
2f700 2a 2a 20 77 69 74 68 20 70 54 65 72 6d 5b 6e 54  ** with pTerm[nT
2f701 65 72 6d 5d 20 61 73 20 74 68 65 20 6c 65 66 74  erm] as the left
2f702 6d 6f 73 74 20 74 65 72 6d 20 69 6e 20 69 43 68  most term in iCh
2f703 69 6c 64 42 6c 6f 63 6b 27 73 20 73 75 62 74 72  ildBlock's subtr
2f704 65 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ee..*/.static vo
2f705 69 64 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65  id interiorWrite
2f706 72 41 70 70 65 6e 64 28 49 6e 74 65 72 69 6f 72  rAppend(Interior
2f707 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c  Writer *pWriter,
2f708 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f709 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f70a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
2f70b 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 0a  erm, int nTerm,.
2f70c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f70d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f70e 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 43   sqlite_int64 iC
2f70f 68 69 6c 64 42 6c 6f 63 6b 29 7b 0a 20 20 63 68  hildBlock){.  ch
2f710 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
2f711 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
2f712 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20  nt n, nPrefix = 
2f713 30 3b 0a 0a 20 20 41 53 53 45 52 54 5f 56 41 4c  0;..  ASSERT_VAL
2f714 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42 4c 4f 43  ID_INTERIOR_BLOC
2f715 4b 28 70 57 72 69 74 65 72 2d 3e 6c 61 73 74 29  K(pWriter->last)
2f716 3b 0a 0a 20 20 2f 2a 20 54 68 65 20 66 69 72 73  ;..  /* The firs
2f717 74 20 74 65 72 6d 20 77 72 69 74 74 65 6e 20 69  t term written i
2f718 6e 74 6f 20 61 6e 20 69 6e 74 65 72 69 6f 72 20  nto an interior 
2f719 6e 6f 64 65 20 69 73 20 61 63 74 75 61 6c 6c 79  node is actually
2f71a 0a 20 20 2a 2a 20 61 73 73 6f 63 69 61 74 65 64  .  ** associated
2f71b 20 77 69 74 68 20 74 68 65 20 73 65 63 6f 6e 64   with the second
2f71c 20 63 68 69 6c 64 20 61 64 64 65 64 20 28 74 68   child added (th
2f71d 65 20 66 69 72 73 74 20 63 68 69 6c 64 20 77 61  e first child wa
2f71e 73 20 61 64 64 65 64 0a 20 20 2a 2a 20 69 6e 20  s added.  ** in 
2f71f 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 49 6e  interiorWriterIn
2f720 69 74 2c 20 6f 72 20 69 6e 20 74 68 65 20 69 66  it, or in the if
2f721 20 63 6c 61 75 73 65 20 61 74 20 74 68 65 20 62   clause at the b
2f722 6f 74 74 6f 6d 20 6f 66 20 74 68 69 73 0a 20 20  ottom of this.  
2f723 2a 2a 20 66 75 6e 63 74 69 6f 6e 29 2e 20 20 54  ** function).  T
2f724 68 61 74 20 74 65 72 6d 20 67 65 74 73 20 65 6e  hat term gets en
2f725 63 6f 64 65 64 20 73 74 72 61 69 67 68 74 20 75  coded straight u
2f726 70 2c 20 77 69 74 68 20 6e 50 72 65 66 69 78 20  p, with nPrefix 
2f727 6c 65 66 74 0a 20 20 2a 2a 20 61 74 20 30 2e 0a  left.  ** at 0..
2f728 20 20 2a 2f 0a 20 20 69 66 28 20 70 57 72 69 74    */.  if( pWrit
2f729 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 3d 3d  er->term.nData==
2f72a 30 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73  0 ){.    n = fts
2f72b 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20 6e 54  3PutVarint(c, nT
2f72c 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  erm);.  }else{. 
2f72d 20 20 20 77 68 69 6c 65 28 20 6e 50 72 65 66 69     while( nPrefi
2f72e 78 3c 70 57 72 69 74 65 72 2d 3e 74 65 72 6d 2e  x<pWriter->term.
2f72f 6e 44 61 74 61 20 26 26 0a 20 20 20 20 20 20 20  nData &&.       
2f730 20 20 20 20 70 54 65 72 6d 5b 6e 50 72 65 66 69      pTerm[nPrefi
2f731 78 5d 3d 3d 70 57 72 69 74 65 72 2d 3e 74 65 72  x]==pWriter->ter
2f732 6d 2e 70 44 61 74 61 5b 6e 50 72 65 66 69 78 5d  m.pData[nPrefix]
2f733 20 29 7b 0a 20 20 20 20 20 20 6e 50 72 65 66 69   ){.      nPrefi
2f734 78 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  x++;.    }..    
2f735 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69 6e  n = fts3PutVarin
2f736 74 28 63 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20  t(c, nPrefix);. 
2f737 20 20 20 6e 20 2b 3d 20 66 74 73 33 50 75 74 56     n += fts3PutV
2f738 61 72 69 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d  arint(c+n, nTerm
2f739 2d 6e 50 72 65 66 69 78 29 3b 0a 20 20 7d 0a 0a  -nPrefix);.  }..
2f73a 23 69 66 6e 64 65 66 20 4e 44 45 42 55 47 0a 20  #ifndef NDEBUG. 
2f73b 20 70 57 72 69 74 65 72 2d 3e 69 4c 61 73 74 43   pWriter->iLastC
2f73c 68 69 6c 64 42 6c 6f 63 6b 2b 2b 3b 0a 23 65 6e  hildBlock++;.#en
2f73d 64 69 66 0a 20 20 61 73 73 65 72 74 28 20 70 57  dif.  assert( pW
2f73e 72 69 74 65 72 2d 3e 69 4c 61 73 74 43 68 69 6c  riter->iLastChil
2f73f 64 42 6c 6f 63 6b 3d 3d 69 43 68 69 6c 64 42 6c  dBlock==iChildBl
2f740 6f 63 6b 20 29 3b 0a 0a 20 20 2f 2a 20 4f 76 65  ock );..  /* Ove
2f741 72 66 6c 6f 77 20 74 6f 20 61 20 6e 65 77 20 62  rflow to a new b
2f742 6c 6f 63 6b 20 69 66 20 74 68 65 20 6e 65 77 20  lock if the new 
2f743 74 65 72 6d 20 6d 61 6b 65 73 20 74 68 65 20 63  term makes the c
2f744 75 72 72 65 6e 74 20 62 6c 6f 63 6b 0a 20 20 2a  urrent block.  *
2f745 2a 20 74 6f 6f 20 62 69 67 2c 20 61 6e 64 20 74  * too big, and t
2f746 68 65 20 63 75 72 72 65 6e 74 20 62 6c 6f 63 6b  he current block
2f747 20 61 6c 72 65 61 64 79 20 68 61 73 20 65 6e 6f   already has eno
2f748 75 67 68 20 74 65 72 6d 73 2e 0a 20 20 2a 2f 0a  ugh terms..  */.
2f749 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 6c    if( pWriter->l
2f74a 61 73 74 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2b  ast->data.nData+
2f74b 6e 2b 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78 3e  n+nTerm-nPrefix>
2f74c 49 4e 54 45 52 49 4f 52 5f 4d 41 58 20 26 26 0a  INTERIOR_MAX &&.
2f74d 20 20 20 20 20 20 69 43 68 69 6c 64 42 6c 6f 63        iChildBloc
2f74e 6b 2d 70 57 72 69 74 65 72 2d 3e 69 4f 70 65 6e  k-pWriter->iOpen
2f74f 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b 3e 49 4e  ingChildBlock>IN
2f750 54 45 52 49 4f 52 5f 4d 49 4e 5f 54 45 52 4d 53  TERIOR_MIN_TERMS
2f751 20 29 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d   ){.    pWriter-
2f752 3e 6c 61 73 74 2d 3e 6e 65 78 74 20 3d 20 69 6e  >last->next = in
2f753 74 65 72 69 6f 72 42 6c 6f 63 6b 4e 65 77 28 70  teriorBlockNew(p
2f754 57 72 69 74 65 72 2d 3e 69 48 65 69 67 68 74 2c  Writer->iHeight,
2f755 20 69 43 68 69 6c 64 42 6c 6f 63 6b 2c 0a 20 20   iChildBlock,.  
2f756 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f757 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f758 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20           pTerm, 
2f759 6e 54 65 72 6d 29 3b 0a 20 20 20 20 70 57 72 69  nTerm);.    pWri
2f75a 74 65 72 2d 3e 6c 61 73 74 20 3d 20 70 57 72 69  ter->last = pWri
2f75b 74 65 72 2d 3e 6c 61 73 74 2d 3e 6e 65 78 74 3b  ter->last->next;
2f75c 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69 4f  .    pWriter->iO
2f75d 70 65 6e 69 6e 67 43 68 69 6c 64 42 6c 6f 63 6b  peningChildBlock
2f75e 20 3d 20 69 43 68 69 6c 64 42 6c 6f 63 6b 3b 0a   = iChildBlock;.
2f75f 20 20 20 20 64 61 74 61 42 75 66 66 65 72 52 65      dataBufferRe
2f760 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e 74 65  set(&pWriter->te
2f761 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  rm);.  }else{.  
2f762 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65    dataBufferAppe
2f763 6e 64 32 28 26 70 57 72 69 74 65 72 2d 3e 6c 61  nd2(&pWriter->la
2f764 73 74 2d 3e 64 61 74 61 2c 20 63 2c 20 6e 2c 0a  st->data, c, n,.
2f765 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f766 20 20 20 20 20 20 70 54 65 72 6d 2b 6e 50 72 65        pTerm+nPre
2f767 66 69 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66  fix, nTerm-nPref
2f768 69 78 29 3b 0a 20 20 20 20 64 61 74 61 42 75 66  ix);.    dataBuf
2f769 66 65 72 52 65 70 6c 61 63 65 28 26 70 57 72 69  ferReplace(&pWri
2f76a 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d  ter->term, pTerm
2f76b 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 7d 0a 20 20  , nTerm);.  }.  
2f76c 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 49 4e 54  ASSERT_VALID_INT
2f76d 45 52 49 4f 52 5f 42 4c 4f 43 4b 28 70 57 72 69  ERIOR_BLOCK(pWri
2f76e 74 65 72 2d 3e 6c 61 73 74 29 3b 0a 7d 0a 0a 2f  ter->last);.}../
2f76f 2a 20 46 72 65 65 20 74 68 65 20 73 70 61 63 65  * Free the space
2f770 20 75 73 65 64 20 62 79 20 70 57 72 69 74 65 72   used by pWriter
2f771 2c 20 69 6e 63 6c 75 64 69 6e 67 20 74 68 65 20  , including the 
2f772 6c 69 6e 6b 65 64 2d 6c 69 73 74 20 6f 66 0a 2a  linked-list of.*
2f773 2a 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 73  * InteriorBlocks
2f774 2c 20 61 6e 64 20 70 61 72 65 6e 74 57 72 69 74  , and parentWrit
2f775 65 72 2c 20 69 66 20 70 72 65 73 65 6e 74 2e 0a  er, if present..
2f776 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e  */.static int in
2f777 74 65 72 69 6f 72 57 72 69 74 65 72 44 65 73 74  teriorWriterDest
2f778 72 6f 79 28 49 6e 74 65 72 69 6f 72 57 72 69 74  roy(InteriorWrit
2f779 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a 20 20  er *pWriter){.  
2f77a 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b 20 2a 62  InteriorBlock *b
2f77b 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65 72 2d 3e  lock = pWriter->
2f77c 66 69 72 73 74 3b 0a 0a 20 20 77 68 69 6c 65 28  first;..  while(
2f77d 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 20 29 7b 0a   block!=NULL ){.
2f77e 20 20 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63      InteriorBloc
2f77f 6b 20 2a 62 20 3d 20 62 6c 6f 63 6b 3b 0a 20 20  k *b = block;.  
2f780 20 20 62 6c 6f 63 6b 20 3d 20 62 6c 6f 63 6b 2d    block = block-
2f781 3e 6e 65 78 74 3b 0a 20 20 20 20 64 61 74 61 42  >next;.    dataB
2f782 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 62 2d  ufferDestroy(&b-
2f783 3e 74 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61  >term);.    data
2f784 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26 62  BufferDestroy(&b
2f785 2d 3e 64 61 74 61 29 3b 0a 20 20 20 20 73 71 6c  ->data);.    sql
2f786 69 74 65 33 5f 66 72 65 65 28 62 29 3b 0a 20 20  ite3_free(b);.  
2f787 7d 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d  }.  if( pWriter-
2f788 3e 70 61 72 65 6e 74 57 72 69 74 65 72 21 3d 4e  >parentWriter!=N
2f789 55 4c 4c 20 29 7b 0a 20 20 20 20 69 6e 74 65 72  ULL ){.    inter
2f78a 69 6f 72 57 72 69 74 65 72 44 65 73 74 72 6f 79  iorWriterDestroy
2f78b 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74  (pWriter->parent
2f78c 57 72 69 74 65 72 29 3b 0a 20 20 20 20 73 71 6c  Writer);.    sql
2f78d 69 74 65 33 5f 66 72 65 65 28 70 57 72 69 74 65  ite3_free(pWrite
2f78e 72 2d 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29  r->parentWriter)
2f78f 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75 66 66  ;.  }.  dataBuff
2f790 65 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74  erDestroy(&pWrit
2f791 65 72 2d 3e 74 65 72 6d 29 3b 0a 20 20 53 43 52  er->term);.  SCR
2f792 41 4d 42 4c 45 28 70 57 72 69 74 65 72 29 3b 0a  AMBLE(pWriter);.
2f793 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
2f794 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 57 72  OK;.}../* If pWr
2f795 69 74 65 72 20 63 61 6e 20 66 69 74 20 65 6e 74  iter can fit ent
2f796 69 72 65 6c 79 20 69 6e 20 52 4f 4f 54 5f 4d 41  irely in ROOT_MA
2f797 58 2c 20 72 65 74 75 72 6e 20 69 74 20 61 73 20  X, return it as 
2f798 74 68 65 20 72 6f 6f 74 20 69 6e 66 6f 0a 2a 2a  the root info.**
2f799 20 64 69 72 65 63 74 6c 79 2c 20 6c 65 61 76 69   directly, leavi
2f79a 6e 67 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64  ng *piEndBlockid
2f79b 20 75 6e 63 68 61 6e 67 65 64 2e 20 20 4f 74 68   unchanged.  Oth
2f79c 65 72 77 69 73 65 2c 20 66 6c 75 73 68 0a 2a 2a  erwise, flush.**
2f79d 20 70 57 72 69 74 65 72 20 74 6f 20 25 5f 73 65   pWriter to %_se
2f79e 67 6d 65 6e 74 73 2c 20 62 75 69 6c 64 69 6e 67  gments, building
2f79f 20 61 20 6e 65 77 20 6c 61 79 65 72 20 6f 66 20   a new layer of 
2f7a0 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 2c 20  interior nodes, 
2f7a1 61 6e 64 0a 2a 2a 20 72 65 63 75 72 73 69 76 65  and.** recursive
2f7a2 6c 79 20 61 73 6b 20 66 6f 72 20 74 68 65 69 72  ly ask for their
2f7a3 20 72 6f 6f 74 20 69 6e 74 6f 2e 0a 2a 2f 0a 73   root into..*/.s
2f7a4 74 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69  tatic int interi
2f7a5 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66 6f  orWriterRootInfo
2f7a6 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2f7a7 76 2c 20 49 6e 74 65 72 69 6f 72 57 72 69 74 65  v, InteriorWrite
2f7a8 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
2f7a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f7aa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 68                ch
2f7ab 61 72 20 2a 2a 70 70 52 6f 6f 74 49 6e 66 6f 2c  ar **ppRootInfo,
2f7ac 20 69 6e 74 20 2a 70 6e 52 6f 6f 74 49 6e 66 6f   int *pnRootInfo
2f7ad 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
2f7ae 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f7af 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34      sqlite_int64
2f7b0 20 2a 70 69 45 6e 64 42 6c 6f 63 6b 69 64 29 7b   *piEndBlockid){
2f7b1 0a 20 20 49 6e 74 65 72 69 6f 72 42 6c 6f 63 6b  .  InteriorBlock
2f7b2 20 2a 62 6c 6f 63 6b 20 3d 20 70 57 72 69 74 65   *block = pWrite
2f7b3 72 2d 3e 66 69 72 73 74 3b 0a 20 20 73 71 6c 69  r->first;.  sqli
2f7b4 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
2f7b5 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 72 63 3b  d = 0;.  int rc;
2f7b6 0a 0a 20 20 2f 2a 20 49 66 20 77 65 20 63 61 6e  ..  /* If we can
2f7b7 20 66 69 74 20 74 68 65 20 73 65 67 6d 65 6e 74   fit the segment
2f7b8 20 69 6e 6c 69 6e 65 20 2a 2f 0a 20 20 69 66 28   inline */.  if(
2f7b9 20 62 6c 6f 63 6b 3d 3d 70 57 72 69 74 65 72 2d   block==pWriter-
2f7ba 3e 6c 61 73 74 20 26 26 20 62 6c 6f 63 6b 2d 3e  >last && block->
2f7bb 64 61 74 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f  data.nData<ROOT_
2f7bc 4d 41 58 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f  MAX ){.    *ppRo
2f7bd 6f 74 49 6e 66 6f 20 3d 20 62 6c 6f 63 6b 2d 3e  otInfo = block->
2f7be 64 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20 20  data.pData;.    
2f7bf 2a 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 62 6c  *pnRootInfo = bl
2f7c0 6f 63 6b 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3b  ock->data.nData;
2f7c1 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
2f7c2 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  TE_OK;.  }..  /*
2f7c3 20 46 6c 75 73 68 20 74 68 65 20 66 69 72 73 74   Flush the first
2f7c4 20 62 6c 6f 63 6b 20 74 6f 20 25 5f 73 65 67 6d   block to %_segm
2f7c5 65 6e 74 73 2c 20 61 6e 64 20 63 72 65 61 74 65  ents, and create
2f7c6 20 61 20 6e 65 77 20 6c 65 76 65 6c 20 6f 66 0a   a new level of.
2f7c7 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f    ** interior no
2f7c8 64 65 2e 0a 20 20 2a 2f 0a 20 20 41 53 53 45 52  de..  */.  ASSER
2f7c9 54 5f 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52  T_VALID_INTERIOR
2f7ca 5f 42 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20  _BLOCK(block);. 
2f7cb 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65   rc = block_inse
2f7cc 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  rt(v, block->dat
2f7cd 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e  a.pData, block->
2f7ce 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c  data.nData, &iBl
2f7cf 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20 72 63  ockid);.  if( rc
2f7d0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2f7d1 74 75 72 6e 20 72 63 3b 0a 20 20 2a 70 69 45 6e  turn rc;.  *piEn
2f7d2 64 42 6c 6f 63 6b 69 64 20 3d 20 69 42 6c 6f 63  dBlockid = iBloc
2f7d3 6b 69 64 3b 0a 0a 20 20 70 57 72 69 74 65 72 2d  kid;..  pWriter-
2f7d4 3e 70 61 72 65 6e 74 57 72 69 74 65 72 20 3d 20  >parentWriter = 
2f7d5 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73  sqlite3_malloc(s
2f7d6 69 7a 65 6f 66 28 2a 70 57 72 69 74 65 72 2d 3e  izeof(*pWriter->
2f7d7 70 61 72 65 6e 74 57 72 69 74 65 72 29 29 3b 0a  parentWriter));.
2f7d8 20 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72    interiorWriter
2f7d9 49 6e 69 74 28 70 57 72 69 74 65 72 2d 3e 69 48  Init(pWriter->iH
2f7da 65 69 67 68 74 2b 31 2c 0a 20 20 20 20 20 20 20  eight+1,.       
2f7db 20 20 20 20 20 20 20 20 20 20 20 20 20 20 62 6c                bl
2f7dc 6f 63 6b 2d 3e 74 65 72 6d 2e 70 44 61 74 61 2c  ock->term.pData,
2f7dd 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 6e 44 61   block->term.nDa
2f7de 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ta,.            
2f7df 20 20 20 20 20 20 20 20 20 69 42 6c 6f 63 6b 69           iBlocki
2f7e0 64 2c 20 70 57 72 69 74 65 72 2d 3e 70 61 72 65  d, pWriter->pare
2f7e1 6e 74 57 72 69 74 65 72 29 3b 0a 0a 20 20 2f 2a  ntWriter);..  /*
2f7e2 20 46 6c 75 73 68 20 61 64 64 69 74 69 6f 6e 61   Flush additiona
2f7e3 6c 20 62 6c 6f 63 6b 73 20 61 6e 64 20 61 70 70  l blocks and app
2f7e4 65 6e 64 20 74 6f 20 74 68 65 20 68 69 67 68 65  end to the highe
2f7e5 72 20 69 6e 74 65 72 69 6f 72 0a 20 20 2a 2a 20  r interior.  ** 
2f7e6 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72  node..  */.  for
2f7e7 28 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65  (block=block->ne
2f7e8 78 74 3b 20 62 6c 6f 63 6b 21 3d 4e 55 4c 4c 3b  xt; block!=NULL;
2f7e9 20 62 6c 6f 63 6b 3d 62 6c 6f 63 6b 2d 3e 6e 65   block=block->ne
2f7ea 78 74 29 7b 0a 20 20 20 20 41 53 53 45 52 54 5f  xt){.    ASSERT_
2f7eb 56 41 4c 49 44 5f 49 4e 54 45 52 49 4f 52 5f 42  VALID_INTERIOR_B
2f7ec 4c 4f 43 4b 28 62 6c 6f 63 6b 29 3b 0a 20 20 20  LOCK(block);.   
2f7ed 20 72 63 20 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65   rc = block_inse
2f7ee 72 74 28 76 2c 20 62 6c 6f 63 6b 2d 3e 64 61 74  rt(v, block->dat
2f7ef 61 2e 70 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e  a.pData, block->
2f7f0 64 61 74 61 2e 6e 44 61 74 61 2c 20 26 69 42 6c  data.nData, &iBl
2f7f1 6f 63 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20  ockid);.    if( 
2f7f2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2f7f3 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 2a  return rc;.    *
2f7f4 70 69 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 69  piEndBlockid = i
2f7f5 42 6c 6f 63 6b 69 64 3b 0a 0a 20 20 20 20 69 6e  Blockid;..    in
2f7f6 74 65 72 69 6f 72 57 72 69 74 65 72 41 70 70 65  teriorWriterAppe
2f7f7 6e 64 28 70 57 72 69 74 65 72 2d 3e 70 61 72 65  nd(pWriter->pare
2f7f8 6e 74 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  ntWriter,.      
2f7f9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f7fa 20 20 20 62 6c 6f 63 6b 2d 3e 74 65 72 6d 2e 70     block->term.p
2f7fb 44 61 74 61 2c 20 62 6c 6f 63 6b 2d 3e 74 65 72  Data, block->ter
2f7fc 6d 2e 6e 44 61 74 61 2c 20 69 42 6c 6f 63 6b 69  m.nData, iBlocki
2f7fd 64 29 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 50 61  d);.  }..  /* Pa
2f7fe 72 65 6e 74 20 6e 6f 64 65 20 67 65 74 73 20 74  rent node gets t
2f7ff 68 65 20 63 68 61 6e 63 65 20 74 6f 20 62 65 20  he chance to be 
2f800 74 68 65 20 72 6f 6f 74 2e 20 2a 2f 0a 20 20 72  the root. */.  r
2f801 65 74 75 72 6e 20 69 6e 74 65 72 69 6f 72 57 72  eturn interiorWr
2f802 69 74 65 72 52 6f 6f 74 49 6e 66 6f 28 76 2c 20  iterRootInfo(v, 
2f803 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e 74 57  pWriter->parentW
2f804 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
2f805 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f806 20 20 20 20 20 20 20 70 70 52 6f 6f 74 49 6e 66         ppRootInf
2f807 6f 2c 20 70 6e 52 6f 6f 74 49 6e 66 6f 2c 20 70  o, pnRootInfo, p
2f808 69 45 6e 64 42 6c 6f 63 6b 69 64 29 3b 0a 7d 0a  iEndBlockid);.}.
2f809 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
2f80a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f80b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f80c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f80d 2a 2a 2f 0a 2f 2a 20 49 6e 74 65 72 69 6f 72 52  **/./* InteriorR
2f80e 65 61 64 65 72 20 69 73 20 75 73 65 64 20 74 6f  eader is used to
2f80f 20 72 65 61 64 20 6f 66 66 20 74 68 65 20 64 61   read off the da
2f810 74 61 20 66 72 6f 6d 20 61 6e 20 69 6e 74 65 72  ta from an inter
2f811 69 6f 72 20 6e 6f 64 65 0a 2a 2a 20 28 73 65 65  ior node.** (see
2f812 20 63 6f 6d 6d 65 6e 74 20 61 74 20 74 6f 70 20   comment at top 
2f813 6f 66 20 66 69 6c 65 20 66 6f 72 20 74 68 65 20  of file for the 
2f814 66 6f 72 6d 61 74 29 2e 0a 2a 2f 0a 74 79 70 65  format)..*/.type
2f815 64 65 66 20 73 74 72 75 63 74 20 49 6e 74 65 72  def struct Inter
2f816 69 6f 72 52 65 61 64 65 72 20 7b 0a 20 20 63 6f  iorReader {.  co
2f817 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 3b  nst char *pData;
2f818 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 0a 20  .  int nData;.. 
2f819 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
2f81a 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 70 72  ;          /* pr
2f81b 65 76 69 6f 75 73 20 74 65 72 6d 2c 20 66 6f 72  evious term, for
2f81c 20 64 65 63 6f 64 69 6e 67 20 74 65 72 6d 20 64   decoding term d
2f81d 65 6c 74 61 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69  elta. */..  sqli
2f81e 74 65 5f 69 6e 74 36 34 20 69 42 6c 6f 63 6b 69  te_int64 iBlocki
2f81f 64 3b 0a 7d 20 49 6e 74 65 72 69 6f 72 52 65 61  d;.} InteriorRea
2f820 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f 69  der;..static voi
2f821 64 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  d interiorReader
2f822 44 65 73 74 72 6f 79 28 49 6e 74 65 72 69 6f 72  Destroy(Interior
2f823 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2f824 7b 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  {.  dataBufferDe
2f825 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e  stroy(&pReader->
2f826 74 65 72 6d 29 3b 0a 20 20 53 43 52 41 4d 42 4c  term);.  SCRAMBL
2f827 45 28 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f  E(pReader);.}../
2f828 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
2f829 65 20 61 73 73 65 72 74 69 6f 6e 73 20 61 72 65  e assertions are
2f82a 20 67 72 65 61 74 2c 20 62 75 74 20 77 68 61 74   great, but what
2f82b 20 69 66 20 77 65 27 72 65 20 69 6e 20 4e 44 45   if we're in NDE
2f82c 42 55 47 0a 2a 2a 20 61 6e 64 20 74 68 65 20 62  BUG.** and the b
2f82d 6c 6f 62 20 69 73 20 65 6d 70 74 79 20 6f 72 20  lob is empty or 
2f82e 6f 74 68 65 72 77 69 73 65 20 63 6f 6e 74 61 69  otherwise contai
2f82f 6e 73 20 73 75 73 70 65 63 74 20 64 61 74 61 3f  ns suspect data?
2f830 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
2f831 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 49 6e  interiorReaderIn
2f832 69 74 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70  it(const char *p
2f833 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c  Data, int nData,
2f834 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
2f835 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f836 49 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a  InteriorReader *
2f837 70 52 65 61 64 65 72 29 7b 0a 20 20 69 6e 74 20  pReader){.  int 
2f838 6e 2c 20 6e 54 65 72 6d 3b 0a 0a 20 20 2f 2a 20  n, nTerm;..  /* 
2f839 52 65 71 75 69 72 65 20 61 74 20 6c 65 61 73 74  Require at least
2f83a 20 74 68 65 20 6c 65 61 64 69 6e 67 20 66 6c 61   the leading fla
2f83b 67 20 62 79 74 65 20 2a 2f 0a 20 20 61 73 73 65  g byte */.  asse
2f83c 72 74 28 20 6e 44 61 74 61 3e 30 20 29 3b 0a 20  rt( nData>0 );. 
2f83d 20 61 73 73 65 72 74 28 20 70 44 61 74 61 5b 30   assert( pData[0
2f83e 5d 21 3d 27 5c 30 27 20 29 3b 0a 0a 20 20 43 4c  ]!='\0' );..  CL
2f83f 45 41 52 28 70 52 65 61 64 65 72 29 3b 0a 0a 20  EAR(pReader);.. 
2f840 20 2f 2a 20 44 65 63 6f 64 65 20 74 68 65 20 62   /* Decode the b
2f841 61 73 65 20 62 6c 6f 63 6b 69 64 2c 20 61 6e 64  ase blockid, and
2f842 20 73 65 74 20 74 68 65 20 63 75 72 73 6f 72 20   set the cursor 
2f843 74 6f 20 74 68 65 20 66 69 72 73 74 20 74 65 72  to the first ter
2f844 6d 2e 20 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33  m. */.  n = fts3
2f845 47 65 74 56 61 72 69 6e 74 28 70 44 61 74 61 2b  GetVarint(pData+
2f846 31 2c 20 26 70 52 65 61 64 65 72 2d 3e 69 42 6c  1, &pReader->iBl
2f847 6f 63 6b 69 64 29 3b 0a 20 20 61 73 73 65 72 74  ockid);.  assert
2f848 28 20 31 2b 6e 3c 3d 6e 44 61 74 61 20 29 3b 0a  ( 1+n<=nData );.
2f849 20 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61    pReader->pData
2f84a 20 3d 20 70 44 61 74 61 2b 31 2b 6e 3b 0a 20 20   = pData+1+n;.  
2f84b 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d  pReader->nData =
2f84c 20 6e 44 61 74 61 2d 28 31 2b 6e 29 3b 0a 0a 20   nData-(1+n);.. 
2f84d 20 2f 2a 20 41 20 73 69 6e 67 6c 65 2d 63 68 69   /* A single-chi
2f84e 6c 64 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  ld interior node
2f84f 20 28 73 75 63 68 20 61 73 20 77 68 65 6e 20 61   (such as when a
2f850 20 6c 65 61 66 20 6e 6f 64 65 20 77 61 73 20 74   leaf node was t
2f851 6f 6f 0a 20 20 2a 2a 20 6c 61 72 67 65 20 66 6f  oo.  ** large fo
2f852 72 20 74 68 65 20 73 65 67 6d 65 6e 74 20 64 69  r the segment di
2f853 72 65 63 74 6f 72 79 29 20 77 6f 6e 27 74 20 68  rectory) won't h
2f854 61 76 65 20 61 6e 79 20 74 65 72 6d 73 2e 0a 20  ave any terms.. 
2f855 20 2a 2a 20 4f 74 68 65 72 77 69 73 65 2c 20 64   ** Otherwise, d
2f856 65 63 6f 64 65 20 74 68 65 20 66 69 72 73 74 20  ecode the first 
2f857 74 65 72 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  term..  */.  if(
2f858 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d   pReader->nData=
2f859 3d 30 20 29 7b 0a 20 20 20 20 64 61 74 61 42 75  =0 ){.    dataBu
2f85a 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64 65  fferInit(&pReade
2f85b 72 2d 3e 74 65 72 6d 2c 20 30 29 3b 0a 20 20 7d  r->term, 0);.  }
2f85c 65 6c 73 65 7b 0a 20 20 20 20 6e 20 3d 20 66 74  else{.    n = ft
2f85d 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70 52  s3GetVarint32(pR
2f85e 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26 6e  eader->pData, &n
2f85f 54 65 72 6d 29 3b 0a 20 20 20 20 64 61 74 61 42  Term);.    dataB
2f860 75 66 66 65 72 49 6e 69 74 28 26 70 52 65 61 64  ufferInit(&pRead
2f861 65 72 2d 3e 74 65 72 6d 2c 20 6e 54 65 72 6d 29  er->term, nTerm)
2f862 3b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ;.    dataBuffer
2f863 52 65 70 6c 61 63 65 28 26 70 52 65 61 64 65 72  Replace(&pReader
2f864 2d 3e 74 65 72 6d 2c 20 70 52 65 61 64 65 72 2d  ->term, pReader-
2f865 3e 70 44 61 74 61 2b 6e 2c 20 6e 54 65 72 6d 29  >pData+n, nTerm)
2f866 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20 6e 2b  ;.    assert( n+
2f867 6e 54 65 72 6d 3c 3d 70 52 65 61 64 65 72 2d 3e  nTerm<=pReader->
2f868 6e 44 61 74 61 20 29 3b 0a 20 20 20 20 70 52 65  nData );.    pRe
2f869 61 64 65 72 2d 3e 70 44 61 74 61 20 2b 3d 20 6e  ader->pData += n
2f86a 2b 6e 54 65 72 6d 3b 0a 20 20 20 20 70 52 65 61  +nTerm;.    pRea
2f86b 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b  der->nData -= n+
2f86c 6e 54 65 72 6d 3b 0a 20 20 7d 0a 7d 0a 0a 73 74  nTerm;.  }.}..st
2f86d 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f  atic int interio
2f86e 72 52 65 61 64 65 72 41 74 45 6e 64 28 49 6e 74  rReaderAtEnd(Int
2f86f 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65  eriorReader *pRe
2f870 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20  ader){.  return 
2f871 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
2f872 61 74 61 3d 3d 30 3b 0a 7d 0a 0a 73 74 61 74 69  ata==0;.}..stati
2f873 63 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  c sqlite_int64 i
2f874 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43 75 72  nteriorReaderCur
2f875 72 65 6e 74 42 6c 6f 63 6b 69 64 28 49 6e 74 65  rentBlockid(Inte
2f876 72 69 6f 72 52 65 61 64 65 72 20 2a 70 52 65 61  riorReader *pRea
2f877 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 70  der){.  return p
2f878 52 65 61 64 65 72 2d 3e 69 42 6c 6f 63 6b 69 64  Reader->iBlockid
2f879 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
2f87a 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
2f87b 72 6d 42 79 74 65 73 28 49 6e 74 65 72 69 6f 72  rmBytes(Interior
2f87c 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
2f87d 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69 6e 74  {.  assert( !int
2f87e 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e 64  eriorReaderAtEnd
2f87f 28 70 52 65 61 64 65 72 29 20 29 3b 0a 20 20 72  (pReader) );.  r
2f880 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74  eturn pReader->t
2f881 65 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74 61  erm.nData;.}.sta
2f882 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  tic const char *
2f883 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 54 65  interiorReaderTe
2f884 72 6d 28 49 6e 74 65 72 69 6f 72 52 65 61 64 65  rm(InteriorReade
2f885 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61  r *pReader){.  a
2f886 73 73 65 72 74 28 20 21 69 6e 74 65 72 69 6f 72  ssert( !interior
2f887 52 65 61 64 65 72 41 74 45 6e 64 28 70 52 65 61  ReaderAtEnd(pRea
2f888 64 65 72 29 20 29 3b 0a 20 20 72 65 74 75 72 6e  der) );.  return
2f889 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70   pReader->term.p
2f88a 44 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70  Data;.}../* Step
2f88b 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68 65 20   forward to the 
2f88c 6e 65 78 74 20 74 65 72 6d 20 69 6e 20 74 68 65  next term in the
2f88d 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74 61 74 69 63   node. */.static
2f88e 20 76 6f 69 64 20 69 6e 74 65 72 69 6f 72 52 65   void interiorRe
2f88f 61 64 65 72 53 74 65 70 28 49 6e 74 65 72 69 6f  aderStep(Interio
2f890 72 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  rReader *pReader
2f891 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21 69 6e  ){.  assert( !in
2f892 74 65 72 69 6f 72 52 65 61 64 65 72 41 74 45 6e  teriorReaderAtEn
2f893 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a 20  d(pReader) );.. 
2f894 20 2f 2a 20 49 66 20 74 68 65 20 6c 61 73 74 20   /* If the last 
2f895 74 65 72 6d 20 68 61 73 20 62 65 65 6e 20 72 65  term has been re
2f896 61 64 2c 20 73 69 67 6e 61 6c 20 65 6f 66 2c 20  ad, signal eof, 
2f897 65 6c 73 65 20 63 6f 6e 73 74 72 75 63 74 20 74  else construct t
2f898 68 65 0a 20 20 2a 2a 20 6e 65 78 74 20 74 65 72  he.  ** next ter
2f899 6d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 70 52  m..  */.  if( pR
2f89a 65 61 64 65 72 2d 3e 6e 44 61 74 61 3d 3d 30 20  eader->nData==0 
2f89b 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66 66 65  ){.    dataBuffe
2f89c 72 52 65 73 65 74 28 26 70 52 65 61 64 65 72 2d  rReset(&pReader-
2f89d 3e 74 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b  >term);.  }else{
2f89e 0a 20 20 20 20 69 6e 74 20 6e 2c 20 6e 50 72 65  .    int n, nPre
2f89f 66 69 78 2c 20 6e 53 75 66 66 69 78 3b 0a 0a 20  fix, nSuffix;.. 
2f8a0 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56 61     n = fts3GetVa
2f8a1 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
2f8a2 70 44 61 74 61 2c 20 26 6e 50 72 65 66 69 78 29  pData, &nPrefix)
2f8a3 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73 33 47  ;.    n += fts3G
2f8a4 65 74 56 61 72 69 6e 74 33 32 28 70 52 65 61 64  etVarint32(pRead
2f8a5 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 26 6e 53  er->pData+n, &nS
2f8a6 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 2f 2a 20  uffix);..    /* 
2f8a7 54 72 75 6e 63 61 74 65 20 74 68 65 20 63 75 72  Truncate the cur
2f8a8 72 65 6e 74 20 74 65 72 6d 20 61 6e 64 20 61 70  rent term and ap
2f8a9 70 65 6e 64 20 73 75 66 66 69 78 20 64 61 74 61  pend suffix data
2f8aa 2e 20 2a 2f 0a 20 20 20 20 70 52 65 61 64 65 72  . */.    pReader
2f8ab 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 3d 20 6e  ->term.nData = n
2f8ac 50 72 65 66 69 78 3b 0a 20 20 20 20 64 61 74 61  Prefix;.    data
2f8ad 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 70 52  BufferAppend(&pR
2f8ae 65 61 64 65 72 2d 3e 74 65 72 6d 2c 20 70 52 65  eader->term, pRe
2f8af 61 64 65 72 2d 3e 70 44 61 74 61 2b 6e 2c 20 6e  ader->pData+n, n
2f8b0 53 75 66 66 69 78 29 3b 0a 0a 20 20 20 20 61 73  Suffix);..    as
2f8b1 73 65 72 74 28 20 6e 2b 6e 53 75 66 66 69 78 3c  sert( n+nSuffix<
2f8b2 3d 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20  =pReader->nData 
2f8b3 29 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e  );.    pReader->
2f8b4 70 44 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66  pData += n+nSuff
2f8b5 69 78 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d  ix;.    pReader-
2f8b6 3e 6e 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66  >nData -= n+nSuf
2f8b7 66 69 78 3b 0a 20 20 7d 0a 20 20 70 52 65 61 64  fix;.  }.  pRead
2f8b8 65 72 2d 3e 69 42 6c 6f 63 6b 69 64 2b 2b 3b 0a  er->iBlockid++;.
2f8b9 7d 0a 0a 2f 2a 20 43 6f 6d 70 61 72 65 20 74 68  }../* Compare th
2f8ba 65 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 74  e current term t
2f8bb 6f 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 2c 20  o pTerm[nTerm], 
2f8bc 72 65 74 75 72 6e 69 6e 67 20 73 74 72 63 6d 70  returning strcmp
2f8bd 2d 73 74 79 6c 65 0a 2a 2a 20 72 65 73 75 6c 74  -style.** result
2f8be 73 2e 20 20 49 66 20 69 73 50 72 65 66 69 78 2c  s.  If isPrefix,
2f8bf 20 65 71 75 61 6c 69 74 79 20 6d 65 61 6e 73 20   equality means 
2f8c0 65 71 75 61 6c 20 74 68 72 6f 75 67 68 20 6e 54  equal through nT
2f8c1 65 72 6d 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74  erm bytes..*/.st
2f8c2 61 74 69 63 20 69 6e 74 20 69 6e 74 65 72 69 6f  atic int interio
2f8c3 72 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 49  rReaderTermCmp(I
2f8c4 6e 74 65 72 69 6f 72 52 65 61 64 65 72 20 2a 70  nteriorReader *p
2f8c5 52 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20  Reader,.        
2f8c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f8c7 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
2f8c8 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20  har *pTerm, int 
2f8c9 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65  nTerm, int isPre
2f8ca 66 69 78 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68  fix){.  const ch
2f8cb 61 72 20 2a 70 52 65 61 64 65 72 54 65 72 6d 20  ar *pReaderTerm 
2f8cc 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  = interiorReader
2f8cd 54 65 72 6d 28 70 52 65 61 64 65 72 29 3b 0a 20  Term(pReader);. 
2f8ce 20 69 6e 74 20 6e 52 65 61 64 65 72 54 65 72 6d   int nReaderTerm
2f8cf 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65   = interiorReade
2f8d0 72 54 65 72 6d 42 79 74 65 73 28 70 52 65 61 64  rTermBytes(pRead
2f8d1 65 72 29 3b 0a 20 20 69 6e 74 20 63 2c 20 6e 20  er);.  int c, n 
2f8d2 3d 20 6e 52 65 61 64 65 72 54 65 72 6d 3c 6e 54  = nReaderTerm<nT
2f8d3 65 72 6d 20 3f 20 6e 52 65 61 64 65 72 54 65 72  erm ? nReaderTer
2f8d4 6d 20 3a 20 6e 54 65 72 6d 3b 0a 0a 20 20 69 66  m : nTerm;..  if
2f8d5 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69 66  ( n==0 ){.    if
2f8d6 28 20 6e 52 65 61 64 65 72 54 65 72 6d 3e 30 20  ( nReaderTerm>0 
2f8d7 29 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20 20  ) return -1;.   
2f8d8 20 69 66 28 20 6e 54 65 72 6d 3e 30 20 29 20 72   if( nTerm>0 ) r
2f8d9 65 74 75 72 6e 20 31 3b 0a 20 20 20 20 72 65 74  eturn 1;.    ret
2f8da 75 72 6e 20 30 3b 0a 20 20 7d 0a 0a 20 20 63 20  urn 0;.  }..  c 
2f8db 3d 20 6d 65 6d 63 6d 70 28 70 52 65 61 64 65 72  = memcmp(pReader
2f8dc 54 65 72 6d 2c 20 70 54 65 72 6d 2c 20 6e 29 3b  Term, pTerm, n);
2f8dd 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
2f8de 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20 69 73  turn c;.  if( is
2f8df 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65  Prefix && n==nTe
2f8e0 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  rm ) return 0;. 
2f8e1 20 72 65 74 75 72 6e 20 6e 52 65 61 64 65 72 54   return nReaderT
2f8e2 65 72 6d 20 2d 20 6e 54 65 72 6d 3b 0a 7d 0a 0a  erm - nTerm;.}..
2f8e3 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /***************
2f8e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2f8e7 2a 2f 0a 2f 2a 20 4c 65 61 66 57 72 69 74 65 72  */./* LeafWriter
2f8e8 20 69 73 20 75 73 65 64 20 74 6f 20 63 6f 6c 6c   is used to coll
2f8e9 65 63 74 20 74 65 72 6d 73 20 61 6e 64 20 61 73  ect terms and as
2f8ea 73 6f 63 69 61 74 65 64 20 64 6f 63 6c 69 73 74  sociated doclist
2f8eb 20 64 61 74 61 0a 2a 2a 20 69 6e 74 6f 20 6c 65   data.** into le
2f8ec 61 66 20 62 6c 6f 63 6b 73 20 69 6e 20 25 5f 73  af blocks in %_s
2f8ed 65 67 6d 65 6e 74 73 20 28 73 65 65 20 74 6f 70  egments (see top
2f8ee 20 6f 66 20 66 69 6c 65 20 66 6f 72 20 66 6f 72   of file for for
2f8ef 6d 61 74 20 69 6e 66 6f 29 2e 0a 2a 2a 20 45 78  mat info)..** Ex
2f8f0 70 65 63 74 65 64 20 75 73 61 67 65 20 69 73 3a  pected usage is:
2f8f1 0a 2a 2a 0a 2a 2a 20 4c 65 61 66 57 72 69 74 65  .**.** LeafWrite
2f8f2 72 20 77 72 69 74 65 72 3b 0a 2a 2a 20 6c 65 61  r writer;.** lea
2f8f3 66 57 72 69 74 65 72 49 6e 69 74 28 30 2c 20 30  fWriterInit(0, 0
2f8f4 2c 20 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 77  , &writer);.** w
2f8f5 68 69 6c 65 28 20 73 6f 72 74 65 64 5f 74 65 72  hile( sorted_ter
2f8f6 6d 73 5f 6c 65 66 74 5f 74 6f 5f 70 72 6f 63 65  ms_left_to_proce
2f8f7 73 73 20 29 7b 0a 2a 2a 20 20 20 2f 2f 20 64 61  ss ){.**   // da
2f8f8 74 61 20 69 73 20 64 6f 63 6c 69 73 74 20 64 61  ta is doclist da
2f8f9 74 61 20 66 6f 72 20 74 68 61 74 20 74 65 72 6d  ta for that term
2f8fa 2e 0a 2a 2a 20 20 20 72 63 20 3d 20 6c 65 61 66  ..**   rc = leaf
2f8fb 57 72 69 74 65 72 53 74 65 70 28 76 2c 20 26 77  WriterStep(v, &w
2f8fc 72 69 74 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  riter, pTerm, nT
2f8fd 65 72 6d 2c 20 70 44 61 74 61 2c 20 6e 44 61 74  erm, pData, nDat
2f8fe 61 29 3b 0a 2a 2a 20 20 20 69 66 28 20 72 63 21  a);.**   if( rc!
2f8ff 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74  =SQLITE_OK ) got
2f900 6f 20 65 72 72 3b 0a 2a 2a 20 7d 0a 2a 2a 20 72  o err;.** }.** r
2f901 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46 69  c = leafWriterFi
2f902 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69 74 65  nalize(v, &write
2f903 72 29 3b 0a 2a 2a 65 72 72 3a 0a 2a 2a 20 6c 65  r);.**err:.** le
2f904 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28  afWriterDestroy(
2f905 26 77 72 69 74 65 72 29 3b 0a 2a 2a 20 72 65 74  &writer);.** ret
2f906 75 72 6e 20 72 63 3b 0a 2a 2a 0a 2a 2a 20 6c 65  urn rc;.**.** le
2f907 61 66 57 72 69 74 65 72 53 74 65 70 28 29 20 6d  afWriterStep() m
2f908 61 79 20 77 72 69 74 65 20 61 20 63 6f 6c 6c 65  ay write a colle
2f909 63 74 65 64 20 6c 65 61 66 20 6f 75 74 20 74 6f  cted leaf out to
2f90a 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a 2a 20   %_segments..** 
2f90b 6c 65 61 66 57 72 69 74 65 72 46 69 6e 61 6c 69  leafWriterFinali
2f90c 7a 65 28 29 20 66 69 6e 69 73 68 65 73 20 77 72  ze() finishes wr
2f90d 69 74 69 6e 67 20 61 6e 79 20 62 75 66 66 65 72  iting any buffer
2f90e 65 64 20 64 61 74 61 20 61 6e 64 20 73 74 6f 72  ed data and stor
2f90f 65 73 0a 2a 2a 20 61 20 72 6f 6f 74 20 6e 6f 64  es.** a root nod
2f910 65 20 69 6e 20 25 5f 73 65 67 64 69 72 2e 20 20  e in %_segdir.  
2f911 6c 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f  leafWriterDestro
2f912 79 28 29 20 66 72 65 65 73 20 61 6c 6c 20 62 75  y() frees all bu
2f913 66 66 65 72 73 20 61 6e 64 0a 2a 2a 20 49 6e 74  ffers and.** Int
2f914 65 72 69 6f 72 57 72 69 74 65 72 73 20 61 6c 6c  eriorWriters all
2f915 6f 63 61 74 65 64 20 61 73 20 70 61 72 74 20 6f  ocated as part o
2f916 66 20 77 72 69 74 69 6e 67 20 74 68 69 73 20 73  f writing this s
2f917 65 67 6d 65 6e 74 2e 0a 2a 2a 0a 2a 2a 20 54 4f  egment..**.** TO
2f918 44 4f 28 73 68 65 73 73 29 20 44 6f 63 75 6d 65  DO(shess) Docume
2f919 6e 74 20 6c 65 61 66 57 72 69 74 65 72 53 74 65  nt leafWriterSte
2f91a 70 4d 65 72 67 65 28 29 2e 0a 2a 2f 0a 0a 2f 2a  pMerge()..*/../*
2f91b 20 50 75 74 20 74 65 72 6d 73 20 77 69 74 68 20   Put terms with 
2f91c 64 61 74 61 20 74 68 69 73 20 62 69 67 20 69 6e  data this big in
2f91d 20 74 68 65 69 72 20 6f 77 6e 20 62 6c 6f 63 6b   their own block
2f91e 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 53 54 41  . */.#define STA
2f91f 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 31 30 32 34  NDALONE_MIN 1024
2f920 0a 0a 2f 2a 20 4b 65 65 70 20 6c 65 61 66 20 62  ../* Keep leaf b
2f921 6c 6f 63 6b 73 20 62 65 6c 6f 77 20 74 68 69 73  locks below this
2f922 20 73 69 7a 65 2e 20 2a 2f 0a 23 64 65 66 69 6e   size. */.#defin
2f923 65 20 4c 45 41 46 5f 4d 41 58 20 32 30 34 38 0a  e LEAF_MAX 2048.
2f924 0a 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20  .typedef struct 
2f925 4c 65 61 66 57 72 69 74 65 72 20 7b 0a 20 20 69  LeafWriter {.  i
2f926 6e 74 20 69 4c 65 76 65 6c 3b 0a 20 20 69 6e 74  nt iLevel;.  int
2f927 20 69 64 78 3b 0a 20 20 73 71 6c 69 74 65 5f 69   idx;.  sqlite_i
2f928 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f 63 6b  nt64 iStartBlock
2f929 69 64 3b 20 20 20 20 20 2f 2a 20 6e 65 65 64 65  id;     /* neede
2f92a 64 20 74 6f 20 63 72 65 61 74 65 20 74 68 65 20  d to create the 
2f92b 72 6f 6f 74 20 69 6e 66 6f 20 2a 2f 0a 20 20 73  root info */.  s
2f92c 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64  qlite_int64 iEnd
2f92d 42 6c 6f 63 6b 69 64 3b 20 20 20 20 20 20 20 2f  Blockid;       /
2f92e 2a 20 77 68 65 6e 20 77 65 27 72 65 20 64 6f 6e  * when we're don
2f92f 65 20 77 72 69 74 69 6e 67 2e 20 2a 2f 0a 0a 20  e writing. */.. 
2f930 20 44 61 74 61 42 75 66 66 65 72 20 74 65 72 6d   DataBuffer term
2f931 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
2f932 20 2f 2a 20 70 72 65 76 69 6f 75 73 20 65 6e 63   /* previous enc
2f933 6f 64 65 64 20 74 65 72 6d 20 2a 2f 0a 20 20 44  oded term */.  D
2f934 61 74 61 42 75 66 66 65 72 20 64 61 74 61 3b 20  ataBuffer data; 
2f935 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
2f936 2a 20 65 6e 63 6f 64 69 6e 67 20 62 75 66 66 65  * encoding buffe
2f937 72 20 2a 2f 0a 0a 20 20 2f 2a 20 62 79 74 65 73  r */..  /* bytes
2f938 20 6f 66 20 66 69 72 73 74 20 74 65 72 6d 20 69   of first term i
2f939 6e 20 74 68 65 20 63 75 72 72 65 6e 74 20 6e 6f  n the current no
2f93a 64 65 20 77 68 69 63 68 20 64 69 73 74 69 6e 67  de which disting
2f93b 75 69 73 68 65 73 20 74 68 61 74 0a 20 20 2a 2a  uishes that.  **
2f93c 20 74 65 72 6d 20 66 72 6f 6d 20 74 68 65 20 6c   term from the l
2f93d 61 73 74 20 74 65 72 6d 20 6f 66 20 74 68 65 20  ast term of the 
2f93e 70 72 65 76 69 6f 75 73 20 6e 6f 64 65 2e 0a 20  previous node.. 
2f93f 20 2a 2f 0a 20 20 69 6e 74 20 6e 54 65 72 6d 44   */.  int nTermD
2f940 69 73 74 69 6e 63 74 3b 0a 0a 20 20 49 6e 74 65  istinct;..  Inte
2f941 72 69 6f 72 57 72 69 74 65 72 20 70 61 72 65 6e  riorWriter paren
2f942 74 57 72 69 74 65 72 3b 20 20 20 20 2f 2a 20 69  tWriter;    /* i
2f943 66 20 77 65 20 6f 76 65 72 66 6c 6f 77 20 2a 2f  f we overflow */
2f944 0a 20 20 69 6e 74 20 68 61 73 5f 70 61 72 65 6e  .  int has_paren
2f945 74 3b 0a 7d 20 4c 65 61 66 57 72 69 74 65 72 3b  t;.} LeafWriter;
2f946 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65  ..static void le
2f947 61 66 57 72 69 74 65 72 49 6e 69 74 28 69 6e 74  afWriterInit(int
2f948 20 69 4c 65 76 65 6c 2c 20 69 6e 74 20 69 64 78   iLevel, int idx
2f949 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
2f94a 72 69 74 65 72 29 7b 0a 20 20 43 4c 45 41 52 28  riter){.  CLEAR(
2f94b 70 57 72 69 74 65 72 29 3b 0a 20 20 70 57 72 69  pWriter);.  pWri
2f94c 74 65 72 2d 3e 69 4c 65 76 65 6c 20 3d 20 69 4c  ter->iLevel = iL
2f94d 65 76 65 6c 3b 0a 20 20 70 57 72 69 74 65 72 2d  evel;.  pWriter-
2f94e 3e 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20 64  >idx = idx;..  d
2f94f 61 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 70  ataBufferInit(&p
2f950 57 72 69 74 65 72 2d 3e 74 65 72 6d 2c 20 33 32  Writer->term, 32
2f951 29 3b 0a 0a 20 20 2f 2a 20 53 74 61 72 74 20 6f  );..  /* Start o
2f952 75 74 20 77 69 74 68 20 61 20 72 65 61 73 6f 6e  ut with a reason
2f953 61 62 6c 79 20 73 69 7a 65 64 20 62 6c 6f 63 6b  ably sized block
2f954 2c 20 74 68 6f 75 67 68 20 69 74 20 63 61 6e 20  , though it can 
2f955 67 72 6f 77 2e 20 2a 2f 0a 20 20 64 61 74 61 42  grow. */.  dataB
2f956 75 66 66 65 72 49 6e 69 74 28 26 70 57 72 69 74  ufferInit(&pWrit
2f957 65 72 2d 3e 64 61 74 61 2c 20 4c 45 41 46 5f 4d  er->data, LEAF_M
2f958 41 58 29 3b 0a 7d 0a 0a 23 69 66 6e 64 65 66 20  AX);.}..#ifndef 
2f959 4e 44 45 42 55 47 0a 2f 2a 20 56 65 72 69 66 79  NDEBUG./* Verify
2f95a 20 74 68 61 74 20 74 68 65 20 64 61 74 61 20 69   that the data i
2f95b 73 20 72 65 61 64 61 62 6c 65 20 61 73 20 61 20  s readable as a 
2f95c 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 73 74  leaf node. */.st
2f95d 61 74 69 63 20 76 6f 69 64 20 6c 65 61 66 4e 6f  atic void leafNo
2f95e 64 65 56 61 6c 69 64 61 74 65 28 63 6f 6e 73 74  deValidate(const
2f95f 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
2f960 74 20 6e 44 61 74 61 29 7b 0a 20 20 69 6e 74 20  t nData){.  int 
2f961 6e 2c 20 69 44 75 6d 6d 79 3b 0a 0a 20 20 69 66  n, iDummy;..  if
2f962 28 20 6e 44 61 74 61 3d 3d 30 20 29 20 72 65 74  ( nData==0 ) ret
2f963 75 72 6e 3b 0a 20 20 61 73 73 65 72 74 28 20 6e  urn;.  assert( n
2f964 44 61 74 61 3e 30 20 29 3b 0a 20 20 61 73 73 65  Data>0 );.  asse
2f965 72 74 28 20 70 44 61 74 61 21 3d 30 20 29 3b 0a  rt( pData!=0 );.
2f966 20 20 61 73 73 65 72 74 28 20 70 44 61 74 61 2b    assert( pData+
2f967 6e 44 61 74 61 3e 70 44 61 74 61 20 29 3b 0a 0a  nData>pData );..
2f968 20 20 2f 2a 20 4d 75 73 74 20 6c 65 61 64 20 77    /* Must lead w
2f969 69 74 68 20 61 20 76 61 72 69 6e 74 28 30 29 20  ith a varint(0) 
2f96a 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
2f96b 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
2f96c 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65  &iDummy);.  asse
2f96d 72 74 28 20 69 44 75 6d 6d 79 3d 3d 30 20 29 3b  rt( iDummy==0 );
2f96e 0a 20 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29  .  assert( n>0 )
2f96f 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3c 6e 44  ;.  assert( n<nD
2f970 61 74 61 20 29 3b 0a 20 20 70 44 61 74 61 20 2b  ata );.  pData +
2f971 3d 20 6e 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20  = n;.  nData -= 
2f972 6e 3b 0a 0a 20 20 2f 2a 20 4c 65 61 64 69 6e 67  n;..  /* Leading
2f973 20 74 65 72 6d 20 6c 65 6e 67 74 68 20 61 6e 64   term length and
2f974 20 64 61 74 61 20 6d 75 73 74 20 66 69 74 20 69   data must fit i
2f975 6e 20 62 75 66 66 65 72 2e 20 2a 2f 0a 20 20 6e  n buffer. */.  n
2f976 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
2f977 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
2f978 79 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 3e  y);.  assert( n>
2f979 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 69  0 );.  assert( i
2f97a 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73  Dummy>0 );.  ass
2f97b 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3e 30 20  ert( n+iDummy>0 
2f97c 29 3b 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69  );.  assert( n+i
2f97d 44 75 6d 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20  Dummy<nData );. 
2f97e 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d   pData += n+iDum
2f97f 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d 20 6e  my;.  nData -= n
2f980 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a 20 4c  +iDummy;..  /* L
2f981 65 61 64 69 6e 67 20 74 65 72 6d 27 73 20 64 6f  eading term's do
2f982 63 6c 69 73 74 20 6c 65 6e 67 74 68 20 61 6e 64  clist length and
2f983 20 64 61 74 61 20 6d 75 73 74 20 66 69 74 2e 20   data must fit. 
2f984 2a 2f 0a 20 20 6e 20 3d 20 66 74 73 33 47 65 74  */.  n = fts3Get
2f985 56 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20  Varint32(pData, 
2f986 26 69 44 75 6d 6d 79 29 3b 0a 20 20 61 73 73 65  &iDummy);.  asse
2f987 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 61 73 73  rt( n>0 );.  ass
2f988 65 72 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b  ert( iDummy>0 );
2f989 0a 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75  .  assert( n+iDu
2f98a 6d 6d 79 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  mmy>0 );.  asser
2f98b 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e 44 61  t( n+iDummy<=nDa
2f98c 74 61 20 29 3b 0a 20 20 41 53 53 45 52 54 5f 56  ta );.  ASSERT_V
2f98d 41 4c 49 44 5f 44 4f 43 4c 49 53 54 28 44 4c 5f  ALID_DOCLIST(DL_
2f98e 44 45 46 41 55 4c 54 2c 20 70 44 61 74 61 2b 6e  DEFAULT, pData+n
2f98f 2c 20 69 44 75 6d 6d 79 2c 20 4e 55 4c 4c 29 3b  , iDummy, NULL);
2f990 0a 20 20 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44  .  pData += n+iD
2f991 75 6d 6d 79 3b 0a 20 20 6e 44 61 74 61 20 2d 3d  ummy;.  nData -=
2f992 20 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 2f 2a   n+iDummy;..  /*
2f993 20 56 65 72 69 66 79 20 74 68 61 74 20 74 72 61   Verify that tra
2f994 69 6c 69 6e 67 20 74 65 72 6d 73 20 61 6e 64 20  iling terms and 
2f995 64 6f 63 6c 69 73 74 73 20 61 6c 73 6f 20 61 72  doclists also ar
2f996 65 20 72 65 61 64 61 62 6c 65 2e 20 2a 2f 0a 20  e readable. */. 
2f997 20 77 68 69 6c 65 28 20 6e 44 61 74 61 21 3d 30   while( nData!=0
2f998 20 29 7b 0a 20 20 20 20 6e 20 3d 20 66 74 73 33   ){.    n = fts3
2f999 47 65 74 56 61 72 69 6e 74 33 32 28 70 44 61 74  GetVarint32(pDat
2f99a 61 2c 20 26 69 44 75 6d 6d 79 29 3b 0a 20 20 20  a, &iDummy);.   
2f99b 20 61 73 73 65 72 74 28 20 6e 3e 30 20 29 3b 0a   assert( n>0 );.
2f99c 20 20 20 20 61 73 73 65 72 74 28 20 69 44 75 6d      assert( iDum
2f99d 6d 79 3e 3d 30 20 29 3b 0a 20 20 20 20 61 73 73  my>=0 );.    ass
2f99e 65 72 74 28 20 6e 3c 6e 44 61 74 61 20 29 3b 0a  ert( n<nData );.
2f99f 20 20 20 20 70 44 61 74 61 20 2b 3d 20 6e 3b 0a      pData += n;.
2f9a0 20 20 20 20 6e 44 61 74 61 20 2d 3d 20 6e 3b 0a      nData -= n;.
2f9a1 20 20 20 20 6e 20 3d 20 66 74 73 33 47 65 74 56      n = fts3GetV
2f9a2 61 72 69 6e 74 33 32 28 70 44 61 74 61 2c 20 26  arint32(pData, &
2f9a3 69 44 75 6d 6d 79 29 3b 0a 20 20 20 20 61 73 73  iDummy);.    ass
2f9a4 65 72 74 28 20 6e 3e 30 20 29 3b 0a 20 20 20 20  ert( n>0 );.    
2f9a5 61 73 73 65 72 74 28 20 69 44 75 6d 6d 79 3e 30  assert( iDummy>0
2f9a6 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20   );.    assert( 
2f9a7 6e 2b 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20 20  n+iDummy>0 );.  
2f9a8 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75 6d    assert( n+iDum
2f9a9 6d 79 3c 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  my<nData );.    
2f9aa 70 44 61 74 61 20 2b 3d 20 6e 2b 69 44 75 6d 6d  pData += n+iDumm
2f9ab 79 3b 0a 20 20 20 20 6e 44 61 74 61 20 2d 3d 20  y;.    nData -= 
2f9ac 6e 2b 69 44 75 6d 6d 79 3b 0a 0a 20 20 20 20 6e  n+iDummy;..    n
2f9ad 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
2f9ae 33 32 28 70 44 61 74 61 2c 20 26 69 44 75 6d 6d  32(pData, &iDumm
2f9af 79 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  y);.    assert( 
2f9b0 6e 3e 30 20 29 3b 0a 20 20 20 20 61 73 73 65 72  n>0 );.    asser
2f9b1 74 28 20 69 44 75 6d 6d 79 3e 30 20 29 3b 0a 20  t( iDummy>0 );. 
2f9b2 20 20 20 61 73 73 65 72 74 28 20 6e 2b 69 44 75     assert( n+iDu
2f9b3 6d 6d 79 3e 30 20 29 3b 0a 20 20 20 20 61 73 73  mmy>0 );.    ass
2f9b4 65 72 74 28 20 6e 2b 69 44 75 6d 6d 79 3c 3d 6e  ert( n+iDummy<=n
2f9b5 44 61 74 61 20 29 3b 0a 20 20 20 20 41 53 53 45  Data );.    ASSE
2f9b6 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c 49 53 54  RT_VALID_DOCLIST
2f9b7 28 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44 61  (DL_DEFAULT, pDa
2f9b8 74 61 2b 6e 2c 20 69 44 75 6d 6d 79 2c 20 4e 55  ta+n, iDummy, NU
2f9b9 4c 4c 29 3b 0a 20 20 20 20 70 44 61 74 61 20 2b  LL);.    pData +
2f9ba 3d 20 6e 2b 69 44 75 6d 6d 79 3b 0a 20 20 20 20  = n+iDummy;.    
2f9bb 6e 44 61 74 61 20 2d 3d 20 6e 2b 69 44 75 6d 6d  nData -= n+iDumm
2f9bc 79 3b 0a 20 20 7d 0a 7d 0a 23 64 65 66 69 6e 65  y;.  }.}.#define
2f9bd 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45   ASSERT_VALID_LE
2f9be 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20 6c 65  AF_NODE(p, n) le
2f9bf 61 66 4e 6f 64 65 56 61 6c 69 64 61 74 65 28 70  afNodeValidate(p
2f9c0 2c 20 6e 29 0a 23 65 6c 73 65 0a 23 64 65 66 69  , n).#else.#defi
2f9c1 6e 65 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f  ne ASSERT_VALID_
2f9c2 4c 45 41 46 5f 4e 4f 44 45 28 70 2c 20 6e 29 20  LEAF_NODE(p, n) 
2f9c3 61 73 73 65 72 74 28 20 31 20 29 0a 23 65 6e 64  assert( 1 ).#end
2f9c4 69 66 0a 0a 2f 2a 20 46 6c 75 73 68 20 74 68 65  if../* Flush the
2f9c5 20 63 75 72 72 65 6e 74 20 6c 65 61 66 20 6e 6f   current leaf no
2f9c6 64 65 20 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73  de to %_segments
2f9c7 2c 20 61 6e 64 20 61 64 64 69 6e 67 20 74 68 65  , and adding the
2f9c8 20 72 65 73 75 6c 74 69 6e 67 0a 2a 2a 20 62 6c   resulting.** bl
2f9c9 6f 63 6b 69 64 20 61 6e 64 20 74 68 65 20 73 74  ockid and the st
2f9ca 61 72 74 69 6e 67 20 74 65 72 6d 20 74 6f 20 74  arting term to t
2f9cb 68 65 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  he interior node
2f9cc 20 77 68 69 63 68 20 77 69 6c 6c 0a 2a 2a 20 63   which will.** c
2f9cd 6f 6e 74 61 69 6e 20 69 74 2e 0a 2a 2f 0a 73 74  ontain it..*/.st
2f9ce 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69  atic int leafWri
2f9cf 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68  terInternalFlush
2f9d0 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2f9d1 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70  v, LeafWriter *p
2f9d2 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20  Writer,.        
2f9d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2f9d4 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20 69             int i
2f9d5 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29  Data, int nData)
2f9d6 7b 0a 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  {.  sqlite_int64
2f9d7 20 69 42 6c 6f 63 6b 69 64 20 3d 20 30 3b 0a 20   iBlockid = 0;. 
2f9d8 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 53 74   const char *pSt
2f9d9 61 72 74 69 6e 67 54 65 72 6d 3b 0a 20 20 69 6e  artingTerm;.  in
2f9da 74 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c  t nStartingTerm,
2f9db 20 72 63 2c 20 6e 3b 0a 0a 20 20 2f 2a 20 4d 75   rc, n;..  /* Mu
2f9dc 73 74 20 68 61 76 65 20 74 68 65 20 6c 65 61 64  st have the lead
2f9dd 69 6e 67 20 76 61 72 69 6e 74 28 30 29 20 66 6c  ing varint(0) fl
2f9de 61 67 2c 20 70 6c 75 73 20 61 74 20 6c 65 61 73  ag, plus at leas
2f9df 74 20 73 6f 6d 65 0a 20 20 2a 2a 20 76 61 6c 69  t some.  ** vali
2f9e0 64 2d 6c 6f 6f 6b 69 6e 67 20 64 61 74 61 2e 0a  d-looking data..
2f9e1 20 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 6e    */.  assert( n
2f9e2 44 61 74 61 3e 32 20 29 3b 0a 20 20 61 73 73 65  Data>2 );.  asse
2f9e3 72 74 28 20 69 44 61 74 61 3e 3d 30 20 29 3b 0a  rt( iData>=0 );.
2f9e4 20 20 61 73 73 65 72 74 28 20 69 44 61 74 61 2b    assert( iData+
2f9e5 6e 44 61 74 61 3c 3d 70 57 72 69 74 65 72 2d 3e  nData<=pWriter->
2f9e6 64 61 74 61 2e 6e 44 61 74 61 20 29 3b 0a 20 20  data.nData );.  
2f9e7 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41  ASSERT_VALID_LEA
2f9e8 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e  F_NODE(pWriter->
2f9e9 64 61 74 61 2e 70 44 61 74 61 2b 69 44 61 74 61  data.pData+iData
2f9ea 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 72 63 20  , nData);..  rc 
2f9eb 3d 20 62 6c 6f 63 6b 5f 69 6e 73 65 72 74 28 76  = block_insert(v
2f9ec 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  , pWriter->data.
2f9ed 70 44 61 74 61 2b 69 44 61 74 61 2c 20 6e 44 61  pData+iData, nDa
2f9ee 74 61 2c 20 26 69 42 6c 6f 63 6b 69 64 29 3b 0a  ta, &iBlockid);.
2f9ef 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2f9f0 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
2f9f1 0a 20 20 61 73 73 65 72 74 28 20 69 42 6c 6f 63  .  assert( iBloc
2f9f2 6b 69 64 21 3d 30 20 29 3b 0a 0a 20 20 2f 2a 20  kid!=0 );..  /* 
2f9f3 52 65 63 6f 6e 73 74 72 75 63 74 20 74 68 65 20  Reconstruct the 
2f9f4 66 69 72 73 74 20 74 65 72 6d 20 69 6e 20 74 68  first term in th
2f9f5 65 20 6c 65 61 66 20 66 6f 72 20 70 75 72 70 6f  e leaf for purpo
2f9f6 73 65 73 20 6f 66 20 62 75 69 6c 64 69 6e 67 0a  ses of building.
2f9f7 20 20 2a 2a 20 74 68 65 20 69 6e 74 65 72 69 6f    ** the interio
2f9f8 72 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 6e  r node..  */.  n
2f9f9 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
2f9fa 33 32 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61  32(pWriter->data
2f9fb 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31 2c 20  .pData+iData+1, 
2f9fc 26 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 29 3b  &nStartingTerm);
2f9fd 0a 20 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d  .  pStartingTerm
2f9fe 20 3d 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61   = pWriter->data
2f9ff 2e 70 44 61 74 61 2b 69 44 61 74 61 2b 31 2b 6e  .pData+iData+1+n
2fa00 3b 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69  ;.  assert( pWri
2fa01 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 3e  ter->data.nData>
2fa02 69 44 61 74 61 2b 31 2b 6e 2b 6e 53 74 61 72 74  iData+1+n+nStart
2fa03 69 6e 67 54 65 72 6d 20 29 3b 0a 20 20 61 73 73  ingTerm );.  ass
2fa04 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 6e 54  ert( pWriter->nT
2fa05 65 72 6d 44 69 73 74 69 6e 63 74 3e 30 20 29 3b  ermDistinct>0 );
2fa06 0a 20 20 61 73 73 65 72 74 28 20 70 57 72 69 74  .  assert( pWrit
2fa07 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e 63  er->nTermDistinc
2fa08 74 3c 3d 6e 53 74 61 72 74 69 6e 67 54 65 72 6d  t<=nStartingTerm
2fa09 20 29 3b 0a 20 20 6e 53 74 61 72 74 69 6e 67 54   );.  nStartingT
2fa0a 65 72 6d 20 3d 20 70 57 72 69 74 65 72 2d 3e 6e  erm = pWriter->n
2fa0b 54 65 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20  TermDistinct;.. 
2fa0c 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e 68 61   if( pWriter->ha
2fa0d 73 5f 70 61 72 65 6e 74 20 29 7b 0a 20 20 20 20  s_parent ){.    
2fa0e 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 41 70  interiorWriterAp
2fa0f 70 65 6e 64 28 26 70 57 72 69 74 65 72 2d 3e 70  pend(&pWriter->p
2fa10 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20 20  arentWriter,.   
2fa11 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa12 20 20 20 20 20 20 70 53 74 61 72 74 69 6e 67 54        pStartingT
2fa13 65 72 6d 2c 20 6e 53 74 61 72 74 69 6e 67 54 65  erm, nStartingTe
2fa14 72 6d 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a 20  rm, iBlockid);. 
2fa15 20 7d 65 6c 73 65 7b 0a 20 20 20 20 69 6e 74 65   }else{.    inte
2fa16 72 69 6f 72 57 72 69 74 65 72 49 6e 69 74 28 31  riorWriterInit(1
2fa17 2c 20 70 53 74 61 72 74 69 6e 67 54 65 72 6d 2c  , pStartingTerm,
2fa18 20 6e 53 74 61 72 74 69 6e 67 54 65 72 6d 2c 20   nStartingTerm, 
2fa19 69 42 6c 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20  iBlockid,.      
2fa1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa1b 20 26 70 57 72 69 74 65 72 2d 3e 70 61 72 65 6e   &pWriter->paren
2fa1c 74 57 72 69 74 65 72 29 3b 0a 20 20 20 20 70 57  tWriter);.    pW
2fa1d 72 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e  riter->has_paren
2fa1e 74 20 3d 20 31 3b 0a 20 20 7d 0a 0a 20 20 2f 2a  t = 1;.  }..  /*
2fa1f 20 54 72 61 63 6b 20 74 68 65 20 73 70 61 6e 20   Track the span 
2fa20 6f 66 20 74 68 69 73 20 73 65 67 6d 65 6e 74 27  of this segment'
2fa21 73 20 6c 65 61 66 20 6e 6f 64 65 73 2e 20 2a 2f  s leaf nodes. */
2fa22 0a 20 20 69 66 28 20 70 57 72 69 74 65 72 2d 3e  .  if( pWriter->
2fa23 69 45 6e 64 42 6c 6f 63 6b 69 64 3d 3d 30 20 29  iEndBlockid==0 )
2fa24 7b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 69  {.    pWriter->i
2fa25 45 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57 72  EndBlockid = pWr
2fa26 69 74 65 72 2d 3e 69 53 74 61 72 74 42 6c 6f 63  iter->iStartBloc
2fa27 6b 69 64 20 3d 20 69 42 6c 6f 63 6b 69 64 3b 0a  kid = iBlockid;.
2fa28 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 57 72    }else{.    pWr
2fa29 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69  iter->iEndBlocki
2fa2a 64 2b 2b 3b 0a 20 20 20 20 61 73 73 65 72 74 28  d++;.    assert(
2fa2b 20 69 42 6c 6f 63 6b 69 64 3d 3d 70 57 72 69 74   iBlockid==pWrit
2fa2c 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64 20  er->iEndBlockid 
2fa2d 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e  );.  }..  return
2fa2e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 73 74   SQLITE_OK;.}.st
2fa2f 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72 69  atic int leafWri
2fa30 74 65 72 46 6c 75 73 68 28 66 75 6c 6c 74 65 78  terFlush(fulltex
2fa31 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57  t_vtab *v, LeafW
2fa32 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b  riter *pWriter){
2fa33 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65 61 66  .  int rc = leaf
2fa34 57 72 69 74 65 72 49 6e 74 65 72 6e 61 6c 46 6c  WriterInternalFl
2fa35 75 73 68 28 76 2c 20 70 57 72 69 74 65 72 2c 20  ush(v, pWriter, 
2fa36 30 2c 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  0, pWriter->data
2fa37 2e 6e 44 61 74 61 29 3b 0a 20 20 69 66 28 20 72  .nData);.  if( r
2fa38 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2fa39 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a 20  eturn rc;..  /* 
2fa3a 52 65 2d 69 6e 69 74 69 61 6c 69 7a 65 20 74 68  Re-initialize th
2fa3b 65 20 6f 75 74 70 75 74 20 62 75 66 66 65 72 2e  e output buffer.
2fa3c 20 2a 2f 0a 20 20 64 61 74 61 42 75 66 66 65 72   */.  dataBuffer
2fa3d 52 65 73 65 74 28 26 70 57 72 69 74 65 72 2d 3e  Reset(&pWriter->
2fa3e 64 61 74 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e  data);..  return
2fa3f 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
2fa40 2a 20 46 65 74 63 68 20 74 68 65 20 72 6f 6f 74  * Fetch the root
2fa41 20 69 6e 66 6f 20 66 6f 72 20 74 68 65 20 73 65   info for the se
2fa42 67 6d 65 6e 74 2e 20 20 49 66 20 74 68 65 20 65  gment.  If the e
2fa43 6e 74 69 72 65 20 6c 65 61 66 20 66 69 74 73 0a  ntire leaf fits.
2fa44 2a 2a 20 77 69 74 68 69 6e 20 52 4f 4f 54 5f 4d  ** within ROOT_M
2fa45 41 58 2c 20 74 68 65 6e 20 69 74 20 77 69 6c 6c  AX, then it will
2fa46 20 62 65 20 72 65 74 75 72 6e 65 64 20 64 69 72   be returned dir
2fa47 65 63 74 6c 79 2c 20 6f 74 68 65 72 77 69 73 65  ectly, otherwise
2fa48 20 69 74 0a 2a 2a 20 77 69 6c 6c 20 62 65 20 66   it.** will be f
2fa49 6c 75 73 68 65 64 20 61 6e 64 20 74 68 65 20 72  lushed and the r
2fa4a 6f 6f 74 20 69 6e 66 6f 20 77 69 6c 6c 20 62 65  oot info will be
2fa4b 20 72 65 74 75 72 6e 65 64 20 66 72 6f 6d 20 74   returned from t
2fa4c 68 65 0a 2a 2a 20 69 6e 74 65 72 69 6f 72 20 6e  he.** interior n
2fa4d 6f 64 65 2e 20 20 2a 70 69 45 6e 64 42 6c 6f 63  ode.  *piEndBloc
2fa4e 6b 69 64 20 69 73 20 73 65 74 20 74 6f 20 74 68  kid is set to th
2fa4f 65 20 62 6c 6f 63 6b 69 64 20 6f 66 20 74 68 65  e blockid of the
2fa50 20 6c 61 73 74 0a 2a 2a 20 69 6e 74 65 72 69 6f   last.** interio
2fa51 72 20 6f 72 20 6c 65 61 66 20 6e 6f 64 65 20 77  r or leaf node w
2fa52 72 69 74 74 65 6e 20 74 6f 20 64 69 73 6b 20 28  ritten to disk (
2fa53 30 20 69 66 20 6e 6f 6e 65 20 61 72 65 20 77 72  0 if none are wr
2fa54 69 74 74 65 6e 20 61 74 0a 2a 2a 20 61 6c 6c 29  itten at.** all)
2fa55 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2fa56 6c 65 61 66 57 72 69 74 65 72 52 6f 6f 74 49 6e  leafWriterRootIn
2fa57 66 6f 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  fo(fulltext_vtab
2fa58 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72 20   *v, LeafWriter 
2fa59 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20 20  *pWriter,.      
2fa5a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa5b 20 20 20 20 20 20 20 20 63 68 61 72 20 2a 2a 70          char **p
2fa5c 70 52 6f 6f 74 49 6e 66 6f 2c 20 69 6e 74 20 2a  pRootInfo, int *
2fa5d 70 6e 52 6f 6f 74 49 6e 66 6f 2c 0a 20 20 20 20  pnRootInfo,.    
2fa5e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa5f 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
2fa60 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 42 6c 6f  _int64 *piEndBlo
2fa61 63 6b 69 64 29 7b 0a 20 20 2f 2a 20 77 65 20 63  ckid){.  /* we c
2fa62 61 6e 20 66 69 74 20 74 68 65 20 73 65 67 6d 65  an fit the segme
2fa63 6e 74 20 65 6e 74 69 72 65 6c 79 20 69 6e 6c 69  nt entirely inli
2fa64 6e 65 20 2a 2f 0a 20 20 69 66 28 20 21 70 57 72  ne */.  if( !pWr
2fa65 69 74 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74  iter->has_parent
2fa66 20 26 26 20 70 57 72 69 74 65 72 2d 3e 64 61 74   && pWriter->dat
2fa67 61 2e 6e 44 61 74 61 3c 52 4f 4f 54 5f 4d 41 58  a.nData<ROOT_MAX
2fa68 20 29 7b 0a 20 20 20 20 2a 70 70 52 6f 6f 74 49   ){.    *ppRootI
2fa69 6e 66 6f 20 3d 20 70 57 72 69 74 65 72 2d 3e 64  nfo = pWriter->d
2fa6a 61 74 61 2e 70 44 61 74 61 3b 0a 20 20 20 20 2a  ata.pData;.    *
2fa6b 70 6e 52 6f 6f 74 49 6e 66 6f 20 3d 20 70 57 72  pnRootInfo = pWr
2fa6c 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
2fa6d 3b 0a 20 20 20 20 2a 70 69 45 6e 64 42 6c 6f 63  ;.    *piEndBloc
2fa6e 6b 69 64 20 3d 20 30 3b 0a 20 20 20 20 72 65 74  kid = 0;.    ret
2fa6f 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2fa70 20 7d 0a 0a 20 20 2f 2a 20 46 6c 75 73 68 20 72   }..  /* Flush r
2fa71 65 6d 61 69 6e 69 6e 67 20 6c 65 61 66 20 64 61  emaining leaf da
2fa72 74 61 2e 20 2a 2f 0a 20 20 69 66 28 20 70 57 72  ta. */.  if( pWr
2fa73 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
2fa74 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74 20 72 63  >0 ){.    int rc
2fa75 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46 6c 75   = leafWriterFlu
2fa76 73 68 28 76 2c 20 70 57 72 69 74 65 72 29 3b 0a  sh(v, pWriter);.
2fa77 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2fa78 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72  TE_OK ) return r
2fa79 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 57 65 20  c;.  }..  /* We 
2fa7a 6d 75 73 74 20 68 61 76 65 20 66 6c 75 73 68 65  must have flushe
2fa7b 64 20 61 20 6c 65 61 66 20 61 74 20 73 6f 6d 65  d a leaf at some
2fa7c 20 70 6f 69 6e 74 2e 20 2a 2f 0a 20 20 61 73 73   point. */.  ass
2fa7d 65 72 74 28 20 70 57 72 69 74 65 72 2d 3e 68 61  ert( pWriter->ha
2fa7e 73 5f 70 61 72 65 6e 74 20 29 3b 0a 0a 20 20 2f  s_parent );..  /
2fa7f 2a 20 54 65 6e 61 74 69 76 65 6c 79 20 73 65 74  * Tenatively set
2fa80 20 74 68 65 20 65 6e 64 20 6c 65 61 66 20 62 6c   the end leaf bl
2fa81 6f 63 6b 69 64 20 61 73 20 74 68 65 20 65 6e 64  ockid as the end
2fa82 20 62 6c 6f 63 6b 69 64 2e 20 20 49 66 20 74 68   blockid.  If th
2fa83 65 0a 20 20 2a 2a 20 69 6e 74 65 72 69 6f 72 20  e.  ** interior 
2fa84 6e 6f 64 65 20 63 61 6e 20 62 65 20 72 65 74 75  node can be retu
2fa85 72 6e 65 64 20 69 6e 6c 69 6e 65 2c 20 74 68 69  rned inline, thi
2fa86 73 20 77 69 6c 6c 20 62 65 20 74 68 65 20 66 69  s will be the fi
2fa87 6e 61 6c 0a 20 20 2a 2a 20 62 6c 6f 63 6b 69 64  nal.  ** blockid
2fa88 2c 20 6f 74 68 65 72 77 69 73 65 20 69 74 20 77  , otherwise it w
2fa89 69 6c 6c 20 62 65 20 6f 76 65 72 77 72 69 74 74  ill be overwritt
2fa8a 65 6e 20 62 79 0a 20 20 2a 2a 20 69 6e 74 65 72  en by.  ** inter
2fa8b 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e 66  iorWriterRootInf
2fa8c 6f 28 29 2e 0a 20 20 2a 2f 0a 20 20 2a 70 69 45  o()..  */.  *piE
2fa8d 6e 64 42 6c 6f 63 6b 69 64 20 3d 20 70 57 72 69  ndBlockid = pWri
2fa8e 74 65 72 2d 3e 69 45 6e 64 42 6c 6f 63 6b 69 64  ter->iEndBlockid
2fa8f 3b 0a 0a 20 20 72 65 74 75 72 6e 20 69 6e 74 65  ;..  return inte
2fa90 72 69 6f 72 57 72 69 74 65 72 52 6f 6f 74 49 6e  riorWriterRootIn
2fa91 66 6f 28 76 2c 20 26 70 57 72 69 74 65 72 2d 3e  fo(v, &pWriter->
2fa92 70 61 72 65 6e 74 57 72 69 74 65 72 2c 0a 20 20  parentWriter,.  
2fa93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fa94 20 20 20 20 20 20 20 20 20 20 20 20 20 20 70 70                pp
2fa95 52 6f 6f 74 49 6e 66 6f 2c 20 70 6e 52 6f 6f 74  RootInfo, pnRoot
2fa96 49 6e 66 6f 2c 20 70 69 45 6e 64 42 6c 6f 63 6b  Info, piEndBlock
2fa97 69 64 29 3b 0a 7d 0a 0a 2f 2a 20 43 6f 6c 6c 65  id);.}../* Colle
2fa98 63 74 20 74 68 65 20 72 6f 6f 74 49 6e 66 6f 20  ct the rootInfo 
2fa99 64 61 74 61 20 61 6e 64 20 73 74 6f 72 65 20 69  data and store i
2fa9a 74 20 69 6e 74 6f 20 74 68 65 20 73 65 67 6d 65  t into the segme
2fa9b 6e 74 20 64 69 72 65 63 74 6f 72 79 2e 0a 2a 2a  nt directory..**
2fa9c 20 54 68 69 73 20 68 61 73 20 74 68 65 20 65 66   This has the ef
2fa9d 66 65 63 74 20 6f 66 20 66 6c 75 73 68 69 6e 67  fect of flushing
2fa9e 20 74 68 65 20 73 65 67 6d 65 6e 74 27 73 20 6c   the segment's l
2fa9f 65 61 66 20 64 61 74 61 20 74 6f 0a 2a 2a 20 25  eaf data to.** %
2faa0 5f 73 65 67 6d 65 6e 74 73 2c 20 61 6e 64 20 61  _segments, and a
2faa1 6c 73 6f 20 66 6c 75 73 68 69 6e 67 20 61 6e 79  lso flushing any
2faa2 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 73 20   interior nodes 
2faa3 74 6f 20 25 5f 73 65 67 6d 65 6e 74 73 2e 0a 2a  to %_segments..*
2faa4 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 65 61  /.static int lea
2faa5 66 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28  fWriterFinalize(
2faa6 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
2faa7 2c 20 4c 65 61 66 57 72 69 74 65 72 20 2a 70 57  , LeafWriter *pW
2faa8 72 69 74 65 72 29 7b 0a 20 20 73 71 6c 69 74 65  riter){.  sqlite
2faa9 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63 6b  _int64 iEndBlock
2faaa 69 64 3b 0a 20 20 63 68 61 72 20 2a 70 52 6f 6f  id;.  char *pRoo
2faab 74 49 6e 66 6f 3b 0a 20 20 69 6e 74 20 72 63 2c  tInfo;.  int rc,
2faac 20 6e 52 6f 6f 74 49 6e 66 6f 3b 0a 0a 20 20 72   nRootInfo;..  r
2faad 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 52 6f  c = leafWriterRo
2faae 6f 74 49 6e 66 6f 28 76 2c 20 70 57 72 69 74 65  otInfo(v, pWrite
2faaf 72 2c 20 26 70 52 6f 6f 74 49 6e 66 6f 2c 20 26  r, &pRootInfo, &
2fab0 6e 52 6f 6f 74 49 6e 66 6f 2c 20 26 69 45 6e 64  nRootInfo, &iEnd
2fab1 42 6c 6f 63 6b 69 64 29 3b 0a 20 20 69 66 28 20  Blockid);.  if( 
2fab2 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2fab3 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f 2a  return rc;..  /*
2fab4 20 44 6f 6e 27 74 20 62 6f 74 68 65 72 20 73 74   Don't bother st
2fab5 6f 72 69 6e 67 20 61 6e 20 65 6e 74 69 72 65 6c  oring an entirel
2fab6 79 20 65 6d 70 74 79 20 73 65 67 6d 65 6e 74 2e  y empty segment.
2fab7 20 2a 2f 0a 20 20 69 66 28 20 69 45 6e 64 42 6c   */.  if( iEndBl
2fab8 6f 63 6b 69 64 3d 3d 30 20 26 26 20 6e 52 6f 6f  ockid==0 && nRoo
2fab9 74 49 6e 66 6f 3d 3d 30 20 29 20 72 65 74 75 72  tInfo==0 ) retur
2faba 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20  n SQLITE_OK;..  
2fabb 72 65 74 75 72 6e 20 73 65 67 64 69 72 5f 73 65  return segdir_se
2fabc 74 28 76 2c 20 70 57 72 69 74 65 72 2d 3e 69 4c  t(v, pWriter->iL
2fabd 65 76 65 6c 2c 20 70 57 72 69 74 65 72 2d 3e 69  evel, pWriter->i
2fabe 64 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  dx,.            
2fabf 20 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d          pWriter-
2fac0 3e 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 2c 20  >iStartBlockid, 
2fac1 70 57 72 69 74 65 72 2d 3e 69 45 6e 64 42 6c 6f  pWriter->iEndBlo
2fac2 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20 20  ckid,.          
2fac3 20 20 20 20 20 20 20 20 20 20 69 45 6e 64 42 6c            iEndBl
2fac4 6f 63 6b 69 64 2c 20 70 52 6f 6f 74 49 6e 66 6f  ockid, pRootInfo
2fac5 2c 20 6e 52 6f 6f 74 49 6e 66 6f 29 3b 0a 7d 0a  , nRootInfo);.}.
2fac6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61  .static void lea
2fac7 66 57 72 69 74 65 72 44 65 73 74 72 6f 79 28 4c  fWriterDestroy(L
2fac8 65 61 66 57 72 69 74 65 72 20 2a 70 57 72 69 74  eafWriter *pWrit
2fac9 65 72 29 7b 0a 20 20 69 66 28 20 70 57 72 69 74  er){.  if( pWrit
2faca 65 72 2d 3e 68 61 73 5f 70 61 72 65 6e 74 20 29  er->has_parent )
2facb 20 69 6e 74 65 72 69 6f 72 57 72 69 74 65 72 44   interiorWriterD
2facc 65 73 74 72 6f 79 28 26 70 57 72 69 74 65 72 2d  estroy(&pWriter-
2facd 3e 70 61 72 65 6e 74 57 72 69 74 65 72 29 3b 0a  >parentWriter);.
2face 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
2facf 72 6f 79 28 26 70 57 72 69 74 65 72 2d 3e 74 65  roy(&pWriter->te
2fad0 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  rm);.  dataBuffe
2fad1 72 44 65 73 74 72 6f 79 28 26 70 57 72 69 74 65  rDestroy(&pWrite
2fad2 72 2d 3e 64 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20  r->data);.}../* 
2fad3 45 6e 63 6f 64 65 20 61 20 74 65 72 6d 20 69 6e  Encode a term in
2fad4 74 6f 20 74 68 65 20 6c 65 61 66 57 72 69 74 65  to the leafWrite
2fad5 72 2c 20 64 65 6c 74 61 2d 65 6e 63 6f 64 69 6e  r, delta-encodin
2fad6 67 20 61 73 20 61 70 70 72 6f 70 72 69 61 74 65  g as appropriate
2fad7 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 74 68 65  ..** Returns the
2fad8 20 6c 65 6e 67 74 68 20 6f 66 20 74 68 65 20 6e   length of the n
2fad9 65 77 20 74 65 72 6d 20 77 68 69 63 68 20 64 69  ew term which di
2fada 73 74 69 6e 67 75 69 73 68 65 73 20 69 74 20 66  stinguishes it f
2fadb 72 6f 6d 20 74 68 65 0a 2a 2a 20 70 72 65 76 69  rom the.** previ
2fadc 6f 75 73 20 74 65 72 6d 2c 20 77 68 69 63 68 20  ous term, which 
2fadd 63 61 6e 20 62 65 20 75 73 65 64 20 74 6f 20 73  can be used to s
2fade 65 74 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74  et nTermDistinct
2fadf 20 77 68 65 6e 20 61 20 6e 6f 64 65 0a 2a 2a 20   when a node.** 
2fae0 62 6f 75 6e 64 61 72 79 20 69 73 20 63 72 6f 73  boundary is cros
2fae1 73 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  sed..*/.static i
2fae2 6e 74 20 6c 65 61 66 57 72 69 74 65 72 45 6e 63  nt leafWriterEnc
2fae3 6f 64 65 54 65 72 6d 28 4c 65 61 66 57 72 69 74  odeTerm(LeafWrit
2fae4 65 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20  er *pWriter,.   
2fae5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fae6 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2fae7 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
2fae8 69 6e 74 20 6e 54 65 72 6d 29 7b 0a 20 20 63 68  int nTerm){.  ch
2fae9 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b  ar c[VARINT_MAX+
2faea 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69  VARINT_MAX];.  i
2faeb 6e 74 20 6e 2c 20 6e 50 72 65 66 69 78 20 3d 20  nt n, nPrefix = 
2faec 30 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 6e 54  0;..  assert( nT
2faed 65 72 6d 3e 30 20 29 3b 0a 20 20 77 68 69 6c 65  erm>0 );.  while
2faee 28 20 6e 50 72 65 66 69 78 3c 70 57 72 69 74 65  ( nPrefix<pWrite
2faef 72 2d 3e 74 65 72 6d 2e 6e 44 61 74 61 20 26 26  r->term.nData &&
2faf0 0a 20 20 20 20 20 20 20 20 20 70 54 65 72 6d 5b  .         pTerm[
2faf1 6e 50 72 65 66 69 78 5d 3d 3d 70 57 72 69 74 65  nPrefix]==pWrite
2faf2 72 2d 3e 74 65 72 6d 2e 70 44 61 74 61 5b 6e 50  r->term.pData[nP
2faf3 72 65 66 69 78 5d 20 29 7b 0a 20 20 20 20 6e 50  refix] ){.    nP
2faf4 72 65 66 69 78 2b 2b 3b 0a 20 20 20 20 2f 2a 20  refix++;.    /* 
2faf5 46 61 69 6c 69 6e 67 20 74 68 69 73 20 69 6d 70  Failing this imp
2faf6 6c 69 65 73 20 74 68 61 74 20 74 68 65 20 74 65  lies that the te
2faf7 72 6d 73 20 77 65 72 65 6e 27 74 20 69 6e 20 6f  rms weren't in o
2faf8 72 64 65 72 2e 20 2a 2f 0a 20 20 20 20 61 73 73  rder. */.    ass
2faf9 65 72 74 28 20 6e 50 72 65 66 69 78 3c 6e 54 65  ert( nPrefix<nTe
2fafa 72 6d 20 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  rm );.  }..  if(
2fafb 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e   pWriter->data.n
2fafc 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 2f  Data==0 ){.    /
2fafd 2a 20 45 6e 63 6f 64 65 20 74 68 65 20 6e 6f 64  * Encode the nod
2fafe 65 20 68 65 61 64 65 72 20 61 6e 64 20 6c 65 61  e header and lea
2faff 64 69 6e 67 20 74 65 72 6d 20 61 73 3a 0a 20 20  ding term as:.  
2fb00 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 30 29 0a    **  varint(0).
2fb01 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e      **  varint(n
2fb02 54 65 72 6d 29 0a 20 20 20 20 2a 2a 20 20 63 68  Term).    **  ch
2fb03 61 72 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d 0a  ar pTerm[nTerm].
2fb04 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66      */.    n = f
2fb05 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2c 20  ts3PutVarint(c, 
2fb06 27 5c 30 27 29 3b 0a 20 20 20 20 6e 20 2b 3d 20  '\0');.    n += 
2fb07 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63 2b  fts3PutVarint(c+
2fb08 6e 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 64  n, nTerm);.    d
2fb09 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64 32  ataBufferAppend2
2fb0a 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c  (&pWriter->data,
2fb0b 20 63 2c 20 6e 2c 20 70 54 65 72 6d 2c 20 6e 54   c, n, pTerm, nT
2fb0c 65 72 6d 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  erm);.  }else{. 
2fb0d 20 20 20 2f 2a 20 44 65 6c 74 61 2d 65 6e 63 6f     /* Delta-enco
2fb0e 64 65 20 74 68 65 20 74 65 72 6d 20 61 73 3a 0a  de the term as:.
2fb0f 20 20 20 20 2a 2a 20 20 76 61 72 69 6e 74 28 6e      **  varint(n
2fb10 50 72 65 66 69 78 29 0a 20 20 20 20 2a 2a 20 20  Prefix).    **  
2fb11 76 61 72 69 6e 74 28 6e 53 75 66 66 69 78 29 0a  varint(nSuffix).
2fb12 20 20 20 20 2a 2a 20 20 63 68 61 72 20 70 54 65      **  char pTe
2fb13 72 6d 53 75 66 66 69 78 5b 6e 53 75 66 66 69 78  rmSuffix[nSuffix
2fb14 5d 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e 20 3d  ].    */.    n =
2fb15 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
2fb16 2c 20 6e 50 72 65 66 69 78 29 3b 0a 20 20 20 20  , nPrefix);.    
2fb17 6e 20 2b 3d 20 66 74 73 33 50 75 74 56 61 72 69  n += fts3PutVari
2fb18 6e 74 28 63 2b 6e 2c 20 6e 54 65 72 6d 2d 6e 50  nt(c+n, nTerm-nP
2fb19 72 65 66 69 78 29 3b 0a 20 20 20 20 64 61 74 61  refix);.    data
2fb1a 42 75 66 66 65 72 41 70 70 65 6e 64 32 28 26 70  BufferAppend2(&p
2fb1b 57 72 69 74 65 72 2d 3e 64 61 74 61 2c 20 63 2c  Writer->data, c,
2fb1c 20 6e 2c 20 70 54 65 72 6d 2b 6e 50 72 65 66 69   n, pTerm+nPrefi
2fb1d 78 2c 20 6e 54 65 72 6d 2d 6e 50 72 65 66 69 78  x, nTerm-nPrefix
2fb1e 29 3b 0a 20 20 7d 0a 20 20 64 61 74 61 42 75 66  );.  }.  dataBuf
2fb1f 66 65 72 52 65 70 6c 61 63 65 28 26 70 57 72 69  ferReplace(&pWri
2fb20 74 65 72 2d 3e 74 65 72 6d 2c 20 70 54 65 72 6d  ter->term, pTerm
2fb21 2c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65 74  , nTerm);..  ret
2fb22 75 72 6e 20 6e 50 72 65 66 69 78 2b 31 3b 0a 7d  urn nPrefix+1;.}
2fb23 0a 0a 2f 2a 20 55 73 65 64 20 74 6f 20 61 76 6f  ../* Used to avo
2fb24 69 64 20 61 20 6d 65 6d 6d 6f 76 65 20 77 68 65  id a memmove whe
2fb25 6e 20 61 20 6c 61 72 67 65 20 61 6d 6f 75 6e 74  n a large amount
2fb26 20 6f 66 20 64 6f 63 6c 69 73 74 20 64 61 74 61   of doclist data
2fb27 20 69 73 20 69 6e 0a 2a 2a 20 74 68 65 20 62 75   is in.** the bu
2fb28 66 66 65 72 2e 20 20 54 68 69 73 20 63 6f 6e 73  ffer.  This cons
2fb29 74 72 75 63 74 73 20 61 20 6e 6f 64 65 20 61 6e  tructs a node an
2fb2a 64 20 74 65 72 6d 20 68 65 61 64 65 72 20 62 65  d term header be
2fb2b 66 6f 72 65 0a 2a 2a 20 69 44 6f 63 6c 69 73 74  fore.** iDoclist
2fb2c 44 61 74 61 20 61 6e 64 20 66 6c 75 73 68 65 73  Data and flushes
2fb2d 20 74 68 65 20 72 65 73 75 6c 74 69 6e 67 20 63   the resulting c
2fb2e 6f 6d 70 6c 65 74 65 20 6e 6f 64 65 20 75 73 69  omplete node usi
2fb2f 6e 67 0a 2a 2a 20 6c 65 61 66 57 72 69 74 65 72  ng.** leafWriter
2fb30 49 6e 74 65 72 6e 61 6c 46 6c 75 73 68 28 29 2e  InternalFlush().
2fb31 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
2fb32 65 61 66 57 72 69 74 65 72 49 6e 6c 69 6e 65 46  eafWriterInlineF
2fb33 6c 75 73 68 28 66 75 6c 6c 74 65 78 74 5f 76 74  lush(fulltext_vt
2fb34 61 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65  ab *v, LeafWrite
2fb35 72 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20  r *pWriter,.    
2fb36 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb37 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
2fb38 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20  st char *pTerm, 
2fb39 69 6e 74 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20  int nTerm,.     
2fb3a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb3b 20 20 20 20 20 20 20 20 20 20 20 20 69 6e 74 20              int 
2fb3c 69 44 6f 63 6c 69 73 74 44 61 74 61 29 7b 0a 20  iDoclistData){. 
2fb3d 20 63 68 61 72 20 63 5b 56 41 52 49 4e 54 5f 4d   char c[VARINT_M
2fb3e 41 58 2b 56 41 52 49 4e 54 5f 4d 41 58 5d 3b 0a  AX+VARINT_MAX];.
2fb3f 20 20 69 6e 74 20 69 44 61 74 61 2c 20 6e 20 3d    int iData, n =
2fb40 20 66 74 73 33 50 75 74 56 61 72 69 6e 74 28 63   fts3PutVarint(c
2fb41 2c 20 30 29 3b 0a 20 20 6e 20 2b 3d 20 66 74 73  , 0);.  n += fts
2fb42 33 50 75 74 56 61 72 69 6e 74 28 63 2b 6e 2c 20  3PutVarint(c+n, 
2fb43 6e 54 65 72 6d 29 3b 0a 0a 20 20 2f 2a 20 54 68  nTerm);..  /* Th
2fb44 65 72 65 20 73 68 6f 75 6c 64 20 61 6c 77 61 79  ere should alway
2fb45 73 20 62 65 20 72 6f 6f 6d 20 66 6f 72 20 74 68  s be room for th
2fb46 65 20 68 65 61 64 65 72 2e 20 20 45 76 65 6e 20  e header.  Even 
2fb47 69 66 20 70 54 65 72 6d 20 73 68 61 72 65 64 0a  if pTerm shared.
2fb48 20 20 2a 2a 20 61 20 73 75 62 73 74 61 6e 74 69    ** a substanti
2fb49 61 6c 20 70 72 65 66 69 78 20 77 69 74 68 20 74  al prefix with t
2fb4a 68 65 20 70 72 65 76 69 6f 75 73 20 74 65 72 6d  he previous term
2fb4b 2c 20 74 68 65 20 65 6e 74 69 72 65 20 70 72 65  , the entire pre
2fb4c 66 69 78 0a 20 20 2a 2a 20 63 6f 75 6c 64 20 62  fix.  ** could b
2fb4d 65 20 63 6f 6e 73 74 72 75 63 74 65 64 20 66 72  e constructed fr
2fb4e 6f 6d 20 65 61 72 6c 69 65 72 20 64 61 74 61 20  om earlier data 
2fb4f 69 6e 20 74 68 65 20 64 6f 63 6c 69 73 74 2c 20  in the doclist, 
2fb50 73 6f 20 74 68 65 72 65 0a 20 20 2a 2a 20 73 68  so there.  ** sh
2fb51 6f 75 6c 64 20 62 65 20 72 6f 6f 6d 2e 0a 20 20  ould be room..  
2fb52 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 69 44 6f  */.  assert( iDo
2fb53 63 6c 69 73 74 44 61 74 61 3e 3d 6e 2b 6e 54 65  clistData>=n+nTe
2fb54 72 6d 20 29 3b 0a 0a 20 20 69 44 61 74 61 20 3d  rm );..  iData =
2fb55 20 69 44 6f 63 6c 69 73 74 44 61 74 61 2d 28 6e   iDoclistData-(n
2fb56 2b 6e 54 65 72 6d 29 3b 0a 20 20 6d 65 6d 63 70  +nTerm);.  memcp
2fb57 79 28 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e  y(pWriter->data.
2fb58 70 44 61 74 61 2b 69 44 61 74 61 2c 20 63 2c 20  pData+iData, c, 
2fb59 6e 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70 57 72  n);.  memcpy(pWr
2fb5a 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61  iter->data.pData
2fb5b 2b 69 44 61 74 61 2b 6e 2c 20 70 54 65 72 6d 2c  +iData+n, pTerm,
2fb5c 20 6e 54 65 72 6d 29 3b 0a 0a 20 20 72 65 74 75   nTerm);..  retu
2fb5d 72 6e 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74  rn leafWriterInt
2fb5e 65 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57  ernalFlush(v, pW
2fb5f 72 69 74 65 72 2c 20 69 44 61 74 61 2c 20 70 57  riter, iData, pW
2fb60 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2fb61 61 2d 69 44 61 74 61 29 3b 0a 7d 0a 0a 2f 2a 20  a-iData);.}../* 
2fb62 50 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d  Push pTerm[nTerm
2fb63 5d 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65  ] along with the
2fb64 20 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f   doclist data to
2fb65 20 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72 20   the leaf layer 
2fb66 6f 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73  of.** %_segments
2fb67 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
2fb68 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 4d 65  leafWriterStepMe
2fb69 72 67 65 28 66 75 6c 6c 74 65 78 74 5f 76 74 61  rge(fulltext_vta
2fb6a 62 20 2a 76 2c 20 4c 65 61 66 57 72 69 74 65 72  b *v, LeafWriter
2fb6b 20 2a 70 57 72 69 74 65 72 2c 0a 20 20 20 20 20   *pWriter,.     
2fb6c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb6d 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2fb6e 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
2fb6f 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
2fb70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fb71 20 20 20 20 20 20 20 44 4c 52 65 61 64 65 72 20         DLReader 
2fb72 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 6e  *pReaders, int n
2fb73 52 65 61 64 65 72 73 29 7b 0a 20 20 63 68 61 72  Readers){.  char
2fb74 20 63 5b 56 41 52 49 4e 54 5f 4d 41 58 2b 56 41   c[VARINT_MAX+VA
2fb75 52 49 4e 54 5f 4d 41 58 5d 3b 0a 20 20 69 6e 74  RINT_MAX];.  int
2fb76 20 69 54 65 72 6d 44 61 74 61 20 3d 20 70 57 72   iTermData = pWr
2fb77 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61  iter->data.nData
2fb78 2c 20 69 44 6f 63 6c 69 73 74 44 61 74 61 3b 0a  , iDoclistData;.
2fb79 20 20 69 6e 74 20 69 2c 20 6e 44 61 74 61 2c 20    int i, nData, 
2fb7a 6e 2c 20 6e 41 63 74 75 61 6c 44 61 74 61 2c 20  n, nActualData, 
2fb7b 6e 41 63 74 75 61 6c 2c 20 72 63 2c 20 6e 54 65  nActual, rc, nTe
2fb7c 72 6d 44 69 73 74 69 6e 63 74 3b 0a 0a 20 20 41  rmDistinct;..  A
2fb7d 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c 45 41 46  SSERT_VALID_LEAF
2fb7e 5f 4e 4f 44 45 28 70 57 72 69 74 65 72 2d 3e 64  _NODE(pWriter->d
2fb7f 61 74 61 2e 70 44 61 74 61 2c 20 70 57 72 69 74  ata.pData, pWrit
2fb80 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 29 3b  er->data.nData);
2fb81 0a 20 20 6e 54 65 72 6d 44 69 73 74 69 6e 63 74  .  nTermDistinct
2fb82 20 3d 20 6c 65 61 66 57 72 69 74 65 72 45 6e 63   = leafWriterEnc
2fb83 6f 64 65 54 65 72 6d 28 70 57 72 69 74 65 72 2c  odeTerm(pWriter,
2fb84 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   pTerm, nTerm);.
2fb85 0a 20 20 2f 2a 20 52 65 6d 65 6d 62 65 72 20 6e  .  /* Remember n
2fb86 54 65 72 6d 44 69 73 74 69 6e 63 74 20 69 66 20  TermDistinct if 
2fb87 6f 70 65 6e 69 6e 67 20 61 20 6e 65 77 20 6e 6f  opening a new no
2fb88 64 65 2e 20 2a 2f 0a 20 20 69 66 28 20 69 54 65  de. */.  if( iTe
2fb89 72 6d 44 61 74 61 3d 3d 30 20 29 20 70 57 72 69  rmData==0 ) pWri
2fb8a 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
2fb8b 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e  ct = nTermDistin
2fb8c 63 74 3b 0a 0a 20 20 69 44 6f 63 6c 69 73 74 44  ct;..  iDoclistD
2fb8d 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d 3e 64  ata = pWriter->d
2fb8e 61 74 61 2e 6e 44 61 74 61 3b 0a 0a 20 20 2f 2a  ata.nData;..  /*
2fb8f 20 45 73 74 69 6d 61 74 65 20 74 68 65 20 6c 65   Estimate the le
2fb90 6e 67 74 68 20 6f 66 20 74 68 65 20 6d 65 72 67  ngth of the merg
2fb91 65 64 20 64 6f 63 6c 69 73 74 20 73 6f 20 77 65  ed doclist so we
2fb92 20 63 61 6e 20 6c 65 61 76 65 20 73 70 61 63 65   can leave space
2fb93 0a 20 20 2a 2a 20 74 6f 20 65 6e 63 6f 64 65 20  .  ** to encode 
2fb94 69 74 2e 0a 20 20 2a 2f 0a 20 20 66 6f 72 28 69  it..  */.  for(i
2fb95 3d 30 2c 20 6e 44 61 74 61 3d 30 3b 20 69 3c 6e  =0, nData=0; i<n
2fb96 52 65 61 64 65 72 73 3b 20 69 2b 2b 29 7b 0a 20  Readers; i++){. 
2fb97 20 20 20 6e 44 61 74 61 20 2b 3d 20 64 6c 72 41     nData += dlrA
2fb98 6c 6c 44 61 74 61 42 79 74 65 73 28 26 70 52 65  llDataBytes(&pRe
2fb99 61 64 65 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aders[i]);.  }. 
2fb9a 20 6e 20 3d 20 66 74 73 33 50 75 74 56 61 72 69   n = fts3PutVari
2fb9b 6e 74 28 63 2c 20 6e 44 61 74 61 29 3b 0a 20 20  nt(c, nData);.  
2fb9c 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e 64  dataBufferAppend
2fb9d 28 26 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2c  (&pWriter->data,
2fb9e 20 63 2c 20 6e 29 3b 0a 0a 20 20 64 6f 63 4c 69   c, n);..  docLi
2fb9f 73 74 4d 65 72 67 65 28 26 70 57 72 69 74 65 72  stMerge(&pWriter
2fba0 2d 3e 64 61 74 61 2c 20 70 52 65 61 64 65 72 73  ->data, pReaders
2fba1 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20 41  , nReaders);.  A
2fba2 53 53 45 52 54 5f 56 41 4c 49 44 5f 44 4f 43 4c  SSERT_VALID_DOCL
2fba3 49 53 54 28 44 4c 5f 44 45 46 41 55 4c 54 2c 0a  IST(DL_DEFAULT,.
2fba4 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fba5 20 20 20 20 20 20 20 70 57 72 69 74 65 72 2d 3e         pWriter->
2fba6 64 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c  data.pData+iDocl
2fba7 69 73 74 44 61 74 61 2b 6e 2c 0a 20 20 20 20 20  istData+n,.     
2fba8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fba9 20 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e    pWriter->data.
2fbaa 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74 44 61  nData-iDoclistDa
2fbab 74 61 2d 6e 2c 20 4e 55 4c 4c 29 3b 0a 0a 20 20  ta-n, NULL);..  
2fbac 2f 2a 20 54 68 65 20 61 63 74 75 61 6c 20 61 6d  /* The actual am
2fbad 6f 75 6e 74 20 6f 66 20 64 6f 63 6c 69 73 74 20  ount of doclist 
2fbae 64 61 74 61 20 61 74 20 74 68 69 73 20 70 6f 69  data at this poi
2fbaf 6e 74 20 63 6f 75 6c 64 20 62 65 20 73 6d 61 6c  nt could be smal
2fbb0 6c 65 72 0a 20 20 2a 2a 20 74 68 61 6e 20 74 68  ler.  ** than th
2fbb1 65 20 6c 65 6e 67 74 68 20 77 65 20 65 6e 63 6f  e length we enco
2fbb2 64 65 64 2e 20 20 41 64 64 69 74 69 6f 6e 61 6c  ded.  Additional
2fbb3 6c 79 2c 20 74 68 65 20 73 70 61 63 65 20 72 65  ly, the space re
2fbb4 71 75 69 72 65 64 20 74 6f 0a 20 20 2a 2a 20 65  quired to.  ** e
2fbb5 6e 63 6f 64 65 20 74 68 69 73 20 6c 65 6e 67 74  ncode this lengt
2fbb6 68 20 63 6f 75 6c 64 20 62 65 20 73 6d 61 6c 6c  h could be small
2fbb7 65 72 2e 20 20 46 6f 72 20 73 6d 61 6c 6c 20 64  er.  For small d
2fbb8 6f 63 6c 69 73 74 73 2c 20 74 68 69 73 20 69 73  oclists, this is
2fbb9 0a 20 20 2a 2a 20 6e 6f 74 20 61 20 62 69 67 20  .  ** not a big 
2fbba 64 65 61 6c 2c 20 77 65 20 63 61 6e 20 6a 75 73  deal, we can jus
2fbbb 74 20 75 73 65 20 6d 65 6d 6d 6f 76 65 28 29 20  t use memmove() 
2fbbc 74 6f 20 61 64 6a 75 73 74 20 74 68 69 6e 67 73  to adjust things
2fbbd 2e 0a 20 20 2a 2f 0a 20 20 6e 41 63 74 75 61 6c  ..  */.  nActual
2fbbe 44 61 74 61 20 3d 20 70 57 72 69 74 65 72 2d 3e  Data = pWriter->
2fbbf 64 61 74 61 2e 6e 44 61 74 61 2d 28 69 44 6f 63  data.nData-(iDoc
2fbc0 6c 69 73 74 44 61 74 61 2b 6e 29 3b 0a 20 20 6e  listData+n);.  n
2fbc1 41 63 74 75 61 6c 20 3d 20 66 74 73 33 50 75 74  Actual = fts3Put
2fbc2 56 61 72 69 6e 74 28 63 2c 20 6e 41 63 74 75 61  Varint(c, nActua
2fbc3 6c 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72 74  lData);.  assert
2fbc4 28 20 6e 41 63 74 75 61 6c 44 61 74 61 3c 3d 6e  ( nActualData<=n
2fbc5 44 61 74 61 20 29 3b 0a 20 20 61 73 73 65 72 74  Data );.  assert
2fbc6 28 20 6e 41 63 74 75 61 6c 3c 3d 6e 20 29 3b 0a  ( nActual<=n );.
2fbc7 0a 20 20 2f 2a 20 49 66 20 74 68 65 20 6e 65 77  .  /* If the new
2fbc8 20 64 6f 63 6c 69 73 74 20 69 73 20 62 69 67 20   doclist is big 
2fbc9 65 6e 6f 75 67 68 20 66 6f 72 20 66 6f 72 63 65  enough for force
2fbca 20 61 20 73 74 61 6e 64 61 6c 6f 6e 65 20 6c 65   a standalone le
2fbcb 61 66 0a 20 20 2a 2a 20 6e 6f 64 65 2c 20 77 65  af.  ** node, we
2fbcc 20 63 61 6e 20 69 6d 6d 65 64 69 61 74 65 6c 79   can immediately
2fbcd 20 66 6c 75 73 68 20 69 74 20 69 6e 6c 69 6e 65   flush it inline
2fbce 20 77 69 74 68 6f 75 74 20 64 6f 69 6e 67 20 74   without doing t
2fbcf 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d 6f 76 65 28  he.  ** memmove(
2fbd0 29 2e 0a 20 20 2a 2f 0a 20 20 2f 2a 20 54 4f 44  )..  */.  /* TOD
2fbd1 4f 28 73 68 65 73 73 29 20 54 68 69 73 20 74 65  O(shess) This te
2fbd2 73 74 20 6d 61 74 63 68 65 73 20 6c 65 61 66 57  st matches leafW
2fbd3 72 69 74 65 72 53 74 65 70 28 29 2c 20 77 68 69  riterStep(), whi
2fbd4 63 68 20 64 6f 65 73 20 74 68 69 73 0a 20 20 2a  ch does this.  *
2fbd5 2a 20 74 65 73 74 20 62 65 66 6f 72 65 20 69 74  * test before it
2fbd6 20 6b 6e 6f 77 73 20 74 68 65 20 63 6f 73 74 20   knows the cost 
2fbd7 74 6f 20 76 61 72 69 6e 74 2d 65 6e 63 6f 64 65  to varint-encode
2fbd8 20 74 68 65 20 74 65 72 6d 20 61 6e 64 0a 20 20   the term and.  
2fbd9 2a 2a 20 64 6f 63 6c 69 73 74 20 6c 65 6e 67 74  ** doclist lengt
2fbda 68 73 2e 20 20 41 74 20 73 6f 6d 65 20 70 6f 69  hs.  At some poi
2fbdb 6e 74 2c 20 63 68 61 6e 67 65 20 74 6f 0a 20 20  nt, change to.  
2fbdc 2a 2a 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61  ** pWriter->data
2fbdd 2e 6e 44 61 74 61 2d 69 54 65 72 6d 44 61 74 61  .nData-iTermData
2fbde 3e 53 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 2e  >STANDALONE_MIN.
2fbdf 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 54 65 72  .  */.  if( nTer
2fbe0 6d 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 53 54  m+nActualData>ST
2fbe1 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 29 7b 0a  ANDALONE_MIN ){.
2fbe2 20 20 20 20 2f 2a 20 50 75 73 68 20 6c 65 61 66      /* Push leaf
2fbe3 20 6e 6f 64 65 20 66 72 6f 6d 20 62 65 66 6f 72   node from befor
2fbe4 65 20 74 68 69 73 20 74 65 72 6d 2e 20 2a 2f 0a  e this term. */.
2fbe5 20 20 20 20 69 66 28 20 69 54 65 72 6d 44 61 74      if( iTermDat
2fbe6 61 3e 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20  a>0 ){.      rc 
2fbe7 3d 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65  = leafWriterInte
2fbe8 72 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72  rnalFlush(v, pWr
2fbe9 69 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44 61  iter, 0, iTermDa
2fbea 74 61 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72  ta);.      if( r
2fbeb 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2fbec 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 20  eturn rc;..     
2fbed 20 70 57 72 69 74 65 72 2d 3e 6e 54 65 72 6d 44   pWriter->nTermD
2fbee 69 73 74 69 6e 63 74 20 3d 20 6e 54 65 72 6d 44  istinct = nTermD
2fbef 69 73 74 69 6e 63 74 3b 0a 20 20 20 20 7d 0a 0a  istinct;.    }..
2fbf0 20 20 20 20 2f 2a 20 46 69 78 20 74 68 65 20 65      /* Fix the e
2fbf1 6e 63 6f 64 65 64 20 64 6f 63 6c 69 73 74 20 6c  ncoded doclist l
2fbf2 65 6e 67 74 68 2e 20 2a 2f 0a 20 20 20 20 69 44  ength. */.    iD
2fbf3 6f 63 6c 69 73 74 44 61 74 61 20 2b 3d 20 6e 20  oclistData += n 
2fbf4 2d 20 6e 41 63 74 75 61 6c 3b 0a 20 20 20 20 6d  - nActual;.    m
2fbf5 65 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64  emcpy(pWriter->d
2fbf6 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69  ata.pData+iDocli
2fbf7 73 74 44 61 74 61 2c 20 63 2c 20 6e 41 63 74 75  stData, c, nActu
2fbf8 61 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 50 75 73  al);..    /* Pus
2fbf9 68 20 74 68 65 20 73 74 61 6e 64 61 6c 6f 6e 65  h the standalone
2fbfa 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 20   leaf node. */. 
2fbfb 20 20 20 72 63 20 3d 20 6c 65 61 66 57 72 69 74     rc = leafWrit
2fbfc 65 72 49 6e 6c 69 6e 65 46 6c 75 73 68 28 76 2c  erInlineFlush(v,
2fbfd 20 70 57 72 69 74 65 72 2c 20 70 54 65 72 6d 2c   pWriter, pTerm,
2fbfe 20 6e 54 65 72 6d 2c 20 69 44 6f 63 6c 69 73 74   nTerm, iDoclist
2fbff 44 61 74 61 29 3b 0a 20 20 20 20 69 66 28 20 72  Data);.    if( r
2fc00 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72  c!=SQLITE_OK ) r
2fc01 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 2f  eturn rc;..    /
2fc02 2a 20 4c 65 61 76 65 20 74 68 65 20 6e 6f 64 65  * Leave the node
2fc03 20 65 6d 70 74 79 2e 20 2a 2f 0a 20 20 20 20 64   empty. */.    d
2fc04 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26  ataBufferReset(&
2fc05 70 57 72 69 74 65 72 2d 3e 64 61 74 61 29 3b 0a  pWriter->data);.
2fc06 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
2fc07 20 20 7d 0a 0a 20 20 2f 2a 20 41 74 20 74 68 69    }..  /* At thi
2fc08 73 20 70 6f 69 6e 74 2c 20 77 65 20 6b 6e 6f 77  s point, we know
2fc09 20 74 68 61 74 20 74 68 65 20 64 6f 63 6c 69 73   that the doclis
2fc0a 74 20 77 61 73 20 73 6d 61 6c 6c 2c 20 73 6f 20  t was small, so 
2fc0b 64 6f 20 74 68 65 0a 20 20 2a 2a 20 6d 65 6d 6d  do the.  ** memm
2fc0c 6f 76 65 20 69 66 20 69 6e 64 69 63 61 74 65 64  ove if indicated
2fc0d 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 6e 41 63  ..  */.  if( nAc
2fc0e 74 75 61 6c 3c 6e 20 29 7b 0a 20 20 20 20 6d 65  tual<n ){.    me
2fc0f 6d 6d 6f 76 65 28 70 57 72 69 74 65 72 2d 3e 64  mmove(pWriter->d
2fc10 61 74 61 2e 70 44 61 74 61 2b 69 44 6f 63 6c 69  ata.pData+iDocli
2fc11 73 74 44 61 74 61 2b 6e 41 63 74 75 61 6c 2c 0a  stData+nActual,.
2fc12 20 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69              pWri
2fc13 74 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b  ter->data.pData+
2fc14 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 2c 0a  iDoclistData+n,.
2fc15 20 20 20 20 20 20 20 20 20 20 20 20 70 57 72 69              pWri
2fc16 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74 61 2d  ter->data.nData-
2fc17 28 69 44 6f 63 6c 69 73 74 44 61 74 61 2b 6e 29  (iDoclistData+n)
2fc18 29 3b 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e  );.    pWriter->
2fc19 64 61 74 61 2e 6e 44 61 74 61 20 2d 3d 20 6e 2d  data.nData -= n-
2fc1a 6e 41 63 74 75 61 6c 3b 0a 20 20 7d 0a 0a 20 20  nActual;.  }..  
2fc1b 2f 2a 20 52 65 70 6c 61 63 65 20 77 72 69 74 74  /* Replace writt
2fc1c 65 6e 20 6c 65 6e 67 74 68 20 77 69 74 68 20 61  en length with a
2fc1d 63 74 75 61 6c 20 6c 65 6e 67 74 68 2e 20 2a 2f  ctual length. */
2fc1e 0a 20 20 6d 65 6d 63 70 79 28 70 57 72 69 74 65  .  memcpy(pWrite
2fc1f 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 69 44  r->data.pData+iD
2fc20 6f 63 6c 69 73 74 44 61 74 61 2c 20 63 2c 20 6e  oclistData, c, n
2fc21 41 63 74 75 61 6c 29 3b 0a 0a 20 20 2f 2a 20 49  Actual);..  /* I
2fc22 66 20 74 68 65 20 6e 6f 64 65 20 69 73 20 74 6f  f the node is to
2fc23 6f 20 6c 61 72 67 65 2c 20 62 72 65 61 6b 20 74  o large, break t
2fc24 68 69 6e 67 73 20 75 70 2e 20 2a 2f 0a 20 20 2f  hings up. */.  /
2fc25 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
2fc26 69 73 20 74 65 73 74 20 6d 61 74 63 68 65 73 20  is test matches 
2fc27 6c 65 61 66 57 72 69 74 65 72 53 74 65 70 28 29  leafWriterStep()
2fc28 2c 20 77 68 69 63 68 20 64 6f 65 73 20 74 68 69  , which does thi
2fc29 73 0a 20 20 2a 2a 20 74 65 73 74 20 62 65 66 6f  s.  ** test befo
2fc2a 72 65 20 69 74 20 6b 6e 6f 77 73 20 74 68 65 20  re it knows the 
2fc2b 63 6f 73 74 20 74 6f 20 76 61 72 69 6e 74 2d 65  cost to varint-e
2fc2c 6e 63 6f 64 65 20 74 68 65 20 74 65 72 6d 20 61  ncode the term a
2fc2d 6e 64 0a 20 20 2a 2a 20 64 6f 63 6c 69 73 74 20  nd.  ** doclist 
2fc2e 6c 65 6e 67 74 68 73 2e 20 20 41 74 20 73 6f 6d  lengths.  At som
2fc2f 65 20 70 6f 69 6e 74 2c 20 63 68 61 6e 67 65 20  e point, change 
2fc30 74 6f 0a 20 20 2a 2a 20 70 57 72 69 74 65 72 2d  to.  ** pWriter-
2fc31 3e 64 61 74 61 2e 6e 44 61 74 61 3e 4c 45 41 46  >data.nData>LEAF
2fc32 5f 4d 41 58 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  _MAX..  */.  if(
2fc33 20 69 54 65 72 6d 44 61 74 61 2b 6e 54 65 72 6d   iTermData+nTerm
2fc34 2b 6e 41 63 74 75 61 6c 44 61 74 61 3e 4c 45 41  +nActualData>LEA
2fc35 46 5f 4d 41 58 20 29 7b 0a 20 20 20 20 2f 2a 20  F_MAX ){.    /* 
2fc36 46 6c 75 73 68 20 6f 75 74 20 74 68 65 20 6c 65  Flush out the le
2fc37 61 64 69 6e 67 20 64 61 74 61 20 61 73 20 61 20  ading data as a 
2fc38 6e 6f 64 65 20 2a 2f 0a 20 20 20 20 72 63 20 3d  node */.    rc =
2fc39 20 6c 65 61 66 57 72 69 74 65 72 49 6e 74 65 72   leafWriterInter
2fc3a 6e 61 6c 46 6c 75 73 68 28 76 2c 20 70 57 72 69  nalFlush(v, pWri
2fc3b 74 65 72 2c 20 30 2c 20 69 54 65 72 6d 44 61 74  ter, 0, iTermDat
2fc3c 61 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  a);.    if( rc!=
2fc3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75  SQLITE_OK ) retu
2fc3e 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 57 72 69  rn rc;..    pWri
2fc3f 74 65 72 2d 3e 6e 54 65 72 6d 44 69 73 74 69 6e  ter->nTermDistin
2fc40 63 74 20 3d 20 6e 54 65 72 6d 44 69 73 74 69 6e  ct = nTermDistin
2fc41 63 74 3b 0a 0a 20 20 20 20 2f 2a 20 52 65 62 75  ct;..    /* Rebu
2fc42 69 6c 64 20 68 65 61 64 65 72 20 75 73 69 6e 67  ild header using
2fc43 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72   the current ter
2fc44 6d 20 2a 2f 0a 20 20 20 20 6e 20 3d 20 66 74 73  m */.    n = fts
2fc45 33 50 75 74 56 61 72 69 6e 74 28 70 57 72 69 74  3PutVarint(pWrit
2fc46 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20  er->data.pData, 
2fc47 30 29 3b 0a 20 20 20 20 6e 20 2b 3d 20 66 74 73  0);.    n += fts
2fc48 33 50 75 74 56 61 72 69 6e 74 28 70 57 72 69 74  3PutVarint(pWrit
2fc49 65 72 2d 3e 64 61 74 61 2e 70 44 61 74 61 2b 6e  er->data.pData+n
2fc4a 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6d 65  , nTerm);.    me
2fc4b 6d 63 70 79 28 70 57 72 69 74 65 72 2d 3e 64 61  mcpy(pWriter->da
2fc4c 74 61 2e 70 44 61 74 61 2b 6e 2c 20 70 54 65 72  ta.pData+n, pTer
2fc4d 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20 20 20 6e  m, nTerm);.    n
2fc4e 20 2b 3d 20 6e 54 65 72 6d 3b 0a 0a 20 20 20 20   += nTerm;..    
2fc4f 2f 2a 20 54 68 65 72 65 20 73 68 6f 75 6c 64 20  /* There should 
2fc50 61 6c 77 61 79 73 20 62 65 20 72 6f 6f 6d 2c 20  always be room, 
2fc51 62 65 63 61 75 73 65 20 74 68 65 20 70 72 65 76  because the prev
2fc52 69 6f 75 73 20 65 6e 63 6f 64 69 6e 67 0a 20 20  ious encoding.  
2fc53 20 20 2a 2a 20 69 6e 63 6c 75 64 65 64 20 61 6c    ** included al
2fc54 6c 20 64 61 74 61 20 6e 65 63 65 73 73 61 72 79  l data necessary
2fc55 20 74 6f 20 63 6f 6e 73 74 72 75 63 74 20 74 68   to construct th
2fc56 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a 2f 0a 20  e term..    */. 
2fc57 20 20 20 61 73 73 65 72 74 28 20 6e 3c 69 44 6f     assert( n<iDo
2fc58 63 6c 69 73 74 44 61 74 61 20 29 3b 0a 20 20 20  clistData );.   
2fc59 20 2f 2a 20 53 6f 20 6c 6f 6e 67 20 61 73 20 53   /* So long as S
2fc5a 54 41 4e 44 41 4c 4f 4e 45 5f 4d 49 4e 20 69 73  TANDALONE_MIN is
2fc5b 20 68 61 6c 66 20 6f 72 20 6c 65 73 73 20 6f 66   half or less of
2fc5c 20 4c 45 41 46 5f 4d 41 58 2c 20 74 68 65 0a 20   LEAF_MAX, the. 
2fc5d 20 20 20 2a 2a 20 66 6f 6c 6c 6f 77 69 6e 67 20     ** following 
2fc5e 6d 65 6d 63 70 79 28 29 20 69 73 20 73 61 66 65  memcpy() is safe
2fc5f 20 28 61 73 20 6f 70 70 6f 73 65 64 20 74 6f 20   (as opposed to 
2fc60 6e 65 65 64 69 6e 67 20 61 20 6d 65 6d 6d 6f 76  needing a memmov
2fc61 65 29 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 61  e)..    */.    a
2fc62 73 73 65 72 74 28 20 32 2a 53 54 41 4e 44 41 4c  ssert( 2*STANDAL
2fc63 4f 4e 45 5f 4d 49 4e 3c 3d 4c 45 41 46 5f 4d 41  ONE_MIN<=LEAF_MA
2fc64 58 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28  X );.    assert(
2fc65 20 6e 2b 70 57 72 69 74 65 72 2d 3e 64 61 74 61   n+pWriter->data
2fc66 2e 6e 44 61 74 61 2d 69 44 6f 63 6c 69 73 74 44  .nData-iDoclistD
2fc67 61 74 61 3c 69 44 6f 63 6c 69 73 74 44 61 74 61  ata<iDoclistData
2fc68 20 29 3b 0a 20 20 20 20 6d 65 6d 63 70 79 28 70   );.    memcpy(p
2fc69 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70 44 61  Writer->data.pDa
2fc6a 74 61 2b 6e 2c 0a 20 20 20 20 20 20 20 20 20 20  ta+n,.          
2fc6b 20 70 57 72 69 74 65 72 2d 3e 64 61 74 61 2e 70   pWriter->data.p
2fc6c 44 61 74 61 2b 69 44 6f 63 6c 69 73 74 44 61 74  Data+iDoclistDat
2fc6d 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 70 57  a,.           pW
2fc6e 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2fc6f 61 2d 69 44 6f 63 6c 69 73 74 44 61 74 61 29 3b  a-iDoclistData);
2fc70 0a 20 20 20 20 70 57 72 69 74 65 72 2d 3e 64 61  .    pWriter->da
2fc71 74 61 2e 6e 44 61 74 61 20 2d 3d 20 69 44 6f 63  ta.nData -= iDoc
2fc72 6c 69 73 74 44 61 74 61 2d 6e 3b 0a 20 20 7d 0a  listData-n;.  }.
2fc73 20 20 41 53 53 45 52 54 5f 56 41 4c 49 44 5f 4c    ASSERT_VALID_L
2fc74 45 41 46 5f 4e 4f 44 45 28 70 57 72 69 74 65 72  EAF_NODE(pWriter
2fc75 2d 3e 64 61 74 61 2e 70 44 61 74 61 2c 20 70 57  ->data.pData, pW
2fc76 72 69 74 65 72 2d 3e 64 61 74 61 2e 6e 44 61 74  riter->data.nDat
2fc77 61 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20 53 51  a);..  return SQ
2fc78 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 50  LITE_OK;.}../* P
2fc79 75 73 68 20 70 54 65 72 6d 5b 6e 54 65 72 6d 5d  ush pTerm[nTerm]
2fc7a 20 61 6c 6f 6e 67 20 77 69 74 68 20 74 68 65 20   along with the 
2fc7b 64 6f 63 6c 69 73 74 20 64 61 74 61 20 74 6f 20  doclist data to 
2fc7c 74 68 65 20 6c 65 61 66 20 6c 61 79 65 72 20 6f  the leaf layer o
2fc7d 66 0a 2a 2a 20 25 5f 73 65 67 6d 65 6e 74 73 2e  f.** %_segments.
2fc7e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73  .*/./* TODO(shes
2fc7f 73 29 20 52 65 76 69 73 65 20 77 72 69 74 65 5a  s) Revise writeZ
2fc80 65 72 6f 53 65 67 6d 65 6e 74 28 29 20 73 6f 20  eroSegment() so 
2fc81 74 68 61 74 20 64 6f 63 6c 69 73 74 73 20 61 72  that doclists ar
2fc82 65 0a 2a 2a 20 63 6f 6e 73 74 72 75 63 74 65 64  e.** constructed
2fc83 20 64 69 72 65 63 74 6c 79 20 69 6e 20 70 57 72   directly in pWr
2fc84 69 74 65 72 2d 3e 64 61 74 61 2e 0a 2a 2f 0a 73  iter->data..*/.s
2fc85 74 61 74 69 63 20 69 6e 74 20 6c 65 61 66 57 72  tatic int leafWr
2fc86 69 74 65 72 53 74 65 70 28 66 75 6c 6c 74 65 78  iterStep(fulltex
2fc87 74 5f 76 74 61 62 20 2a 76 2c 20 4c 65 61 66 57  t_vtab *v, LeafW
2fc88 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 2c 0a  riter *pWriter,.
2fc89 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc8a 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2fc8b 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
2fc8c 20 6e 54 65 72 6d 2c 0a 20 20 20 20 20 20 20 20   nTerm,.        
2fc8d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fc8e 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44    const char *pD
2fc8f 61 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 29 7b  ata, int nData){
2fc90 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 44 4c 52  .  int rc;.  DLR
2fc91 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a 0a 20  eader reader;.. 
2fc92 20 64 6c 72 49 6e 69 74 28 26 72 65 61 64 65 72   dlrInit(&reader
2fc93 2c 20 44 4c 5f 44 45 46 41 55 4c 54 2c 20 70 44  , DL_DEFAULT, pD
2fc94 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 72  ata, nData);.  r
2fc95 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 53 74  c = leafWriterSt
2fc96 65 70 4d 65 72 67 65 28 76 2c 20 70 57 72 69 74  epMerge(v, pWrit
2fc97 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  er, pTerm, nTerm
2fc98 2c 20 26 72 65 61 64 65 72 2c 20 31 29 3b 0a 20  , &reader, 1);. 
2fc99 20 64 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61   dlrDestroy(&rea
2fc9a 64 65 72 29 3b 0a 0a 20 20 72 65 74 75 72 6e 20  der);..  return 
2fc9b 72 63 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a  rc;.}.../*******
2fc9c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fc9d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fc9e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fc9f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c 65  *********/./* Le
2fca0 61 66 52 65 61 64 65 72 20 69 73 20 75 73 65 64  afReader is used
2fca1 20 74 6f 20 69 74 65 72 61 74 65 20 6f 76 65 72   to iterate over
2fca2 20 61 6e 20 69 6e 64 69 76 69 64 75 61 6c 20 6c   an individual l
2fca3 65 61 66 20 6e 6f 64 65 2e 20 2a 2f 0a 74 79 70  eaf node. */.typ
2fca4 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61 66  edef struct Leaf
2fca5 52 65 61 64 65 72 20 7b 0a 20 20 44 61 74 61 42  Reader {.  DataB
2fca6 75 66 66 65 72 20 74 65 72 6d 3b 20 20 20 20 20  uffer term;     
2fca7 20 20 20 20 20 2f 2a 20 63 6f 70 79 20 6f 66 20       /* copy of 
2fca8 63 75 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f  current term. */
2fca9 0a 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ..  const char *
2fcaa 70 44 61 74 61 3b 20 20 20 20 20 20 20 20 2f 2a  pData;        /*
2fcab 20 64 61 74 61 20 66 6f 72 20 63 75 72 72 65 6e   data for curren
2fcac 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 69 6e 74  t term. */.  int
2fcad 20 6e 44 61 74 61 3b 0a 7d 20 4c 65 61 66 52 65   nData;.} LeafRe
2fcae 61 64 65 72 3b 0a 0a 73 74 61 74 69 63 20 76 6f  ader;..static vo
2fcaf 69 64 20 6c 65 61 66 52 65 61 64 65 72 44 65 73  id leafReaderDes
2fcb0 74 72 6f 79 28 4c 65 61 66 52 65 61 64 65 72 20  troy(LeafReader 
2fcb1 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 64 61 74  *pReader){.  dat
2fcb2 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
2fcb3 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 29 3b 0a  pReader->term);.
2fcb4 20 20 53 43 52 41 4d 42 4c 45 28 70 52 65 61 64    SCRAMBLE(pRead
2fcb5 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69  er);.}..static i
2fcb6 6e 74 20 6c 65 61 66 52 65 61 64 65 72 41 74 45  nt leafReaderAtE
2fcb7 6e 64 28 4c 65 61 66 52 65 61 64 65 72 20 2a 70  nd(LeafReader *p
2fcb8 52 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72  Reader){.  retur
2fcb9 6e 20 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61  n pReader->nData
2fcba 3c 3d 30 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73  <=0;.}../* Acces
2fcbb 73 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65  s the current te
2fcbc 72 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  rm. */.static in
2fcbd 74 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d  t leafReaderTerm
2fcbe 42 79 74 65 73 28 4c 65 61 66 52 65 61 64 65 72  Bytes(LeafReader
2fcbf 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 72 65   *pReader){.  re
2fcc0 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e 74 65  turn pReader->te
2fcc1 72 6d 2e 6e 44 61 74 61 3b 0a 7d 0a 73 74 61 74  rm.nData;.}.stat
2fcc2 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c  ic const char *l
2fcc3 65 61 66 52 65 61 64 65 72 54 65 72 6d 28 4c 65  eafReaderTerm(Le
2fcc4 61 66 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  afReader *pReade
2fcc5 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 70 52  r){.  assert( pR
2fcc6 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44 61 74  eader->term.nDat
2fcc7 61 3e 30 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  a>0 );.  return 
2fcc8 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70 44  pReader->term.pD
2fcc9 61 74 61 3b 0a 7d 0a 0a 2f 2a 20 41 63 63 65 73  ata;.}../* Acces
2fcca 73 20 74 68 65 20 64 6f 63 6c 69 73 74 20 64 61  s the doclist da
2fccb 74 61 20 66 6f 72 20 74 68 65 20 63 75 72 72 65  ta for the curre
2fccc 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73 74 61 74  nt term. */.stat
2fccd 69 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65  ic int leafReade
2fcce 72 44 61 74 61 42 79 74 65 73 28 4c 65 61 66 52  rDataBytes(LeafR
2fccf 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2fcd0 0a 20 20 69 6e 74 20 6e 44 61 74 61 3b 0a 20 20  .  int nData;.  
2fcd1 61 73 73 65 72 74 28 20 70 52 65 61 64 65 72 2d  assert( pReader-
2fcd2 3e 74 65 72 6d 2e 6e 44 61 74 61 3e 30 20 29 3b  >term.nData>0 );
2fcd3 0a 20 20 66 74 73 33 47 65 74 56 61 72 69 6e 74  .  fts3GetVarint
2fcd4 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
2fcd5 61 2c 20 26 6e 44 61 74 61 29 3b 0a 20 20 72 65  a, &nData);.  re
2fcd6 74 75 72 6e 20 6e 44 61 74 61 3b 0a 7d 0a 73 74  turn nData;.}.st
2fcd7 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61 72 20  atic const char 
2fcd8 2a 6c 65 61 66 52 65 61 64 65 72 44 61 74 61 28  *leafReaderData(
2fcd9 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
2fcda 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e  der){.  int n, n
2fcdb 44 61 74 61 3b 0a 20 20 61 73 73 65 72 74 28 20  Data;.  assert( 
2fcdc 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
2fcdd 61 74 61 3e 30 20 29 3b 0a 20 20 6e 20 3d 20 66  ata>0 );.  n = f
2fcde 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28 70  ts3GetVarint32(p
2fcdf 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20 26  Reader->pData, &
2fce0 6e 44 61 74 61 29 3b 0a 20 20 72 65 74 75 72 6e  nData);.  return
2fce1 20 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2b   pReader->pData+
2fce2 6e 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69  n;.}..static voi
2fce3 64 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74  d leafReaderInit
2fce4 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61  (const char *pDa
2fce5 74 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20  ta, int nData,. 
2fce6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fce7 20 20 20 20 20 20 20 20 20 20 4c 65 61 66 52 65            LeafRe
2fce8 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a  ader *pReader){.
2fce9 20 20 69 6e 74 20 6e 54 65 72 6d 2c 20 6e 3b 0a    int nTerm, n;.
2fcea 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
2fceb 3e 30 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >0 );.  assert( 
2fcec 70 44 61 74 61 5b 30 5d 3d 3d 27 5c 30 27 20 29  pData[0]=='\0' )
2fced 3b 0a 0a 20 20 43 4c 45 41 52 28 70 52 65 61 64  ;..  CLEAR(pRead
2fcee 65 72 29 3b 0a 0a 20 20 2f 2a 20 52 65 61 64 20  er);..  /* Read 
2fcef 74 68 65 20 66 69 72 73 74 20 74 65 72 6d 2c 20  the first term, 
2fcf0 73 6b 69 70 70 69 6e 67 20 74 68 65 20 68 65 61  skipping the hea
2fcf1 64 65 72 20 62 79 74 65 2e 20 2a 2f 0a 20 20 6e  der byte. */.  n
2fcf2 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
2fcf3 33 32 28 70 44 61 74 61 2b 31 2c 20 26 6e 54 65  32(pData+1, &nTe
2fcf4 72 6d 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  rm);.  dataBuffe
2fcf5 72 49 6e 69 74 28 26 70 52 65 61 64 65 72 2d 3e  rInit(&pReader->
2fcf6 74 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a 20 20  term, nTerm);.  
2fcf7 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61 63  dataBufferReplac
2fcf8 65 28 26 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  e(&pReader->term
2fcf9 2c 20 70 44 61 74 61 2b 31 2b 6e 2c 20 6e 54 65  , pData+1+n, nTe
2fcfa 72 6d 29 3b 0a 0a 20 20 2f 2a 20 50 6f 73 69 74  rm);..  /* Posit
2fcfb 69 6f 6e 20 61 66 74 65 72 20 74 68 65 20 66 69  ion after the fi
2fcfc 72 73 74 20 74 65 72 6d 2e 20 2a 2f 0a 20 20 61  rst term. */.  a
2fcfd 73 73 65 72 74 28 20 31 2b 6e 2b 6e 54 65 72 6d  ssert( 1+n+nTerm
2fcfe 3c 6e 44 61 74 61 20 29 3b 0a 20 20 70 52 65 61  <nData );.  pRea
2fcff 64 65 72 2d 3e 70 44 61 74 61 20 3d 20 70 44 61  der->pData = pDa
2fd00 74 61 2b 31 2b 6e 2b 6e 54 65 72 6d 3b 0a 20 20  ta+1+n+nTerm;.  
2fd01 70 52 65 61 64 65 72 2d 3e 6e 44 61 74 61 20 3d  pReader->nData =
2fd02 20 6e 44 61 74 61 2d 31 2d 6e 2d 6e 54 65 72 6d   nData-1-n-nTerm
2fd03 3b 0a 7d 0a 0a 2f 2a 20 53 74 65 70 20 74 68 65  ;.}../* Step the
2fd04 20 72 65 61 64 65 72 20 66 6f 72 77 61 72 64 20   reader forward 
2fd05 74 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  to the next term
2fd06 2e 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  . */.static void
2fd07 20 6c 65 61 66 52 65 61 64 65 72 53 74 65 70 28   leafReaderStep(
2fd08 4c 65 61 66 52 65 61 64 65 72 20 2a 70 52 65 61  LeafReader *pRea
2fd09 64 65 72 29 7b 0a 20 20 69 6e 74 20 6e 2c 20 6e  der){.  int n, n
2fd0a 44 61 74 61 2c 20 6e 50 72 65 66 69 78 2c 20 6e  Data, nPrefix, n
2fd0b 53 75 66 66 69 78 3b 0a 20 20 61 73 73 65 72 74  Suffix;.  assert
2fd0c 28 20 21 6c 65 61 66 52 65 61 64 65 72 41 74 45  ( !leafReaderAtE
2fd0d 6e 64 28 70 52 65 61 64 65 72 29 20 29 3b 0a 0a  nd(pReader) );..
2fd0e 20 20 2f 2a 20 53 6b 69 70 20 70 72 65 76 69 6f    /* Skip previo
2fd0f 75 73 20 65 6e 74 72 79 27 73 20 64 61 74 61 20  us entry's data 
2fd10 62 6c 6f 63 6b 2e 20 2a 2f 0a 20 20 6e 20 3d 20  block. */.  n = 
2fd11 66 74 73 33 47 65 74 56 61 72 69 6e 74 33 32 28  fts3GetVarint32(
2fd12 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 2c 20  pReader->pData, 
2fd13 26 6e 44 61 74 61 29 3b 0a 20 20 61 73 73 65 72  &nData);.  asser
2fd14 74 28 20 6e 2b 6e 44 61 74 61 3c 3d 70 52 65 61  t( n+nData<=pRea
2fd15 64 65 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20  der->nData );.  
2fd16 70 52 65 61 64 65 72 2d 3e 70 44 61 74 61 20 2b  pReader->pData +
2fd17 3d 20 6e 2b 6e 44 61 74 61 3b 0a 20 20 70 52 65  = n+nData;.  pRe
2fd18 61 64 65 72 2d 3e 6e 44 61 74 61 20 2d 3d 20 6e  ader->nData -= n
2fd19 2b 6e 44 61 74 61 3b 0a 0a 20 20 69 66 28 20 21  +nData;..  if( !
2fd1a 6c 65 61 66 52 65 61 64 65 72 41 74 45 6e 64 28  leafReaderAtEnd(
2fd1b 70 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20  pReader) ){.    
2fd1c 2f 2a 20 43 6f 6e 73 74 72 75 63 74 20 74 68 65  /* Construct the
2fd1d 20 6e 65 77 20 74 65 72 6d 20 75 73 69 6e 67 20   new term using 
2fd1e 61 20 70 72 65 66 69 78 20 66 72 6f 6d 20 74 68  a prefix from th
2fd1f 65 20 6f 6c 64 20 74 65 72 6d 20 70 6c 75 73 20  e old term plus 
2fd20 61 0a 20 20 20 20 2a 2a 20 73 75 66 66 69 78 20  a.    ** suffix 
2fd21 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 64 61  from the leaf da
2fd22 74 61 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6e  ta..    */.    n
2fd23 20 3d 20 66 74 73 33 47 65 74 56 61 72 69 6e 74   = fts3GetVarint
2fd24 33 32 28 70 52 65 61 64 65 72 2d 3e 70 44 61 74  32(pReader->pDat
2fd25 61 2c 20 26 6e 50 72 65 66 69 78 29 3b 0a 20 20  a, &nPrefix);.  
2fd26 20 20 6e 20 2b 3d 20 66 74 73 33 47 65 74 56 61    n += fts3GetVa
2fd27 72 69 6e 74 33 32 28 70 52 65 61 64 65 72 2d 3e  rint32(pReader->
2fd28 70 44 61 74 61 2b 6e 2c 20 26 6e 53 75 66 66 69  pData+n, &nSuffi
2fd29 78 29 3b 0a 20 20 20 20 61 73 73 65 72 74 28 20  x);.    assert( 
2fd2a 6e 2b 6e 53 75 66 66 69 78 3c 70 52 65 61 64 65  n+nSuffix<pReade
2fd2b 72 2d 3e 6e 44 61 74 61 20 29 3b 0a 20 20 20 20  r->nData );.    
2fd2c 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
2fd2d 61 74 61 20 3d 20 6e 50 72 65 66 69 78 3b 0a 20  ata = nPrefix;. 
2fd2e 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70 70     dataBufferApp
2fd2f 65 6e 64 28 26 70 52 65 61 64 65 72 2d 3e 74 65  end(&pReader->te
2fd30 72 6d 2c 20 70 52 65 61 64 65 72 2d 3e 70 44 61  rm, pReader->pDa
2fd31 74 61 2b 6e 2c 20 6e 53 75 66 66 69 78 29 3b 0a  ta+n, nSuffix);.
2fd32 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 70 44  .    pReader->pD
2fd33 61 74 61 20 2b 3d 20 6e 2b 6e 53 75 66 66 69 78  ata += n+nSuffix
2fd34 3b 0a 20 20 20 20 70 52 65 61 64 65 72 2d 3e 6e  ;.    pReader->n
2fd35 44 61 74 61 20 2d 3d 20 6e 2b 6e 53 75 66 66 69  Data -= n+nSuffi
2fd36 78 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 73 74 72  x;.  }.}../* str
2fd37 63 6d 70 2d 73 74 79 6c 65 20 63 6f 6d 70 61 72  cmp-style compar
2fd38 69 73 6f 6e 20 6f 66 20 70 52 65 61 64 65 72 27  ison of pReader'
2fd39 73 20 63 75 72 72 65 6e 74 20 74 65 72 6d 20 61  s current term a
2fd3a 67 61 69 6e 73 74 20 70 54 65 72 6d 2e 0a 2a 2a  gainst pTerm..**
2fd3b 20 49 66 20 69 73 50 72 65 66 69 78 2c 20 65 71   If isPrefix, eq
2fd3c 75 61 6c 69 74 79 20 6d 65 61 6e 73 20 65 71 75  uality means equ
2fd3d 61 6c 20 74 68 72 6f 75 67 68 20 6e 54 65 72 6d  al through nTerm
2fd3e 20 62 79 74 65 73 2e 0a 2a 2f 0a 73 74 61 74 69   bytes..*/.stati
2fd3f 63 20 69 6e 74 20 6c 65 61 66 52 65 61 64 65 72  c int leafReader
2fd40 54 65 72 6d 43 6d 70 28 4c 65 61 66 52 65 61 64  TermCmp(LeafRead
2fd41 65 72 20 2a 70 52 65 61 64 65 72 2c 0a 20 20 20  er *pReader,.   
2fd42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fd43 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
2fd44 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74  char *pTerm, int
2fd45 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50 72   nTerm, int isPr
2fd46 65 66 69 78 29 7b 0a 20 20 69 6e 74 20 63 2c 20  efix){.  int c, 
2fd47 6e 20 3d 20 70 52 65 61 64 65 72 2d 3e 74 65 72  n = pReader->ter
2fd48 6d 2e 6e 44 61 74 61 3c 6e 54 65 72 6d 20 3f 20  m.nData<nTerm ? 
2fd49 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 6e 44  pReader->term.nD
2fd4a 61 74 61 20 3a 20 6e 54 65 72 6d 3b 0a 20 20 69  ata : nTerm;.  i
2fd4b 66 28 20 6e 3d 3d 30 20 29 7b 0a 20 20 20 20 69  f( n==0 ){.    i
2fd4c 66 28 20 70 52 65 61 64 65 72 2d 3e 74 65 72 6d  f( pReader->term
2fd4d 2e 6e 44 61 74 61 3e 30 20 29 20 72 65 74 75 72  .nData>0 ) retur
2fd4e 6e 20 2d 31 3b 0a 20 20 20 20 69 66 28 6e 54 65  n -1;.    if(nTe
2fd4f 72 6d 3e 30 20 29 20 72 65 74 75 72 6e 20 31 3b  rm>0 ) return 1;
2fd50 0a 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20  .    return 0;. 
2fd51 20 7d 0a 0a 20 20 63 20 3d 20 6d 65 6d 63 6d 70   }..  c = memcmp
2fd52 28 70 52 65 61 64 65 72 2d 3e 74 65 72 6d 2e 70  (pReader->term.p
2fd53 44 61 74 61 2c 20 70 54 65 72 6d 2c 20 6e 29 3b  Data, pTerm, n);
2fd54 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72 65  .  if( c!=0 ) re
2fd55 74 75 72 6e 20 63 3b 0a 20 20 69 66 28 20 69 73  turn c;.  if( is
2fd56 50 72 65 66 69 78 20 26 26 20 6e 3d 3d 6e 54 65  Prefix && n==nTe
2fd57 72 6d 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  rm ) return 0;. 
2fd58 20 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d   return pReader-
2fd59 3e 74 65 72 6d 2e 6e 44 61 74 61 20 2d 20 6e 54  >term.nData - nT
2fd5a 65 72 6d 3b 0a 7d 0a 0a 0a 2f 2a 2a 2a 2a 2a 2a  erm;.}.../******
2fd5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fd5c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fd5d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
2fd5e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 20 4c  **********/./* L
2fd5f 65 61 76 65 73 52 65 61 64 65 72 20 77 72 61 70  eavesReader wrap
2fd60 73 20 4c 65 61 66 52 65 61 64 65 72 20 74 6f 20  s LeafReader to 
2fd61 61 6c 6c 6f 77 20 69 74 65 72 61 74 69 6e 67 20  allow iterating 
2fd62 6f 76 65 72 20 74 68 65 20 65 6e 74 69 72 65 0a  over the entire.
2fd63 2a 2a 20 6c 65 61 66 20 6c 61 79 65 72 20 6f 66  ** leaf layer of
2fd64 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f 0a 74 79   the tree..*/.ty
2fd65 70 65 64 65 66 20 73 74 72 75 63 74 20 4c 65 61  pedef struct Lea
2fd66 76 65 73 52 65 61 64 65 72 20 7b 0a 20 20 69 6e  vesReader {.  in
2fd67 74 20 69 64 78 3b 20 20 20 20 20 20 20 20 20 20  t idx;          
2fd68 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
2fd69 20 77 69 74 68 69 6e 20 74 68 65 20 73 65 67 6d   within the segm
2fd6a 65 6e 74 2e 20 2a 2f 0a 0a 20 20 73 71 6c 69 74  ent. */..  sqlit
2fd6b 65 33 5f 73 74 6d 74 20 2a 70 53 74 6d 74 3b 20  e3_stmt *pStmt; 
2fd6c 20 20 20 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e       /* Statemen
2fd6d 74 20 77 65 27 72 65 20 73 74 72 65 61 6d 69 6e  t we're streamin
2fd6e 67 20 6c 65 61 76 65 73 20 66 72 6f 6d 2e 20 2a  g leaves from. *
2fd6f 2f 0a 20 20 69 6e 74 20 65 6f 66 3b 20 20 20 20  /.  int eof;    
2fd70 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
2fd71 20 77 65 27 76 65 20 73 65 65 6e 20 53 51 4c 49   we've seen SQLI
2fd72 54 45 5f 44 4f 4e 45 20 66 72 6f 6d 20 70 53 74  TE_DONE from pSt
2fd73 6d 74 2e 20 2a 2f 0a 0a 20 20 4c 65 61 66 52 65  mt. */..  LeafRe
2fd74 61 64 65 72 20 6c 65 61 66 52 65 61 64 65 72 3b  ader leafReader;
2fd75 20 20 20 20 2f 2a 20 72 65 61 64 65 72 20 66 6f      /* reader fo
2fd76 72 20 74 68 65 20 63 75 72 72 65 6e 74 20 6c 65  r the current le
2fd77 61 66 2e 20 2a 2f 0a 20 20 44 61 74 61 42 75 66  af. */.  DataBuf
2fd78 66 65 72 20 72 6f 6f 74 44 61 74 61 3b 20 20 20  fer rootData;   
2fd79 20 20 20 2f 2a 20 72 6f 6f 74 20 64 61 74 61 20     /* root data 
2fd7a 66 6f 72 20 69 6e 6c 69 6e 65 2e 20 2a 2f 0a 7d  for inline. */.}
2fd7b 20 4c 65 61 76 65 73 52 65 61 64 65 72 3b 0a 0a   LeavesReader;..
2fd7c 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20 63 75  /* Access the cu
2fd7d 72 72 65 6e 74 20 74 65 72 6d 2e 20 2a 2f 0a 73  rrent term. */.s
2fd7e 74 61 74 69 63 20 69 6e 74 20 6c 65 61 76 65 73  tatic int leaves
2fd7f 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
2fd80 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
2fd81 65 61 64 65 72 29 7b 0a 20 20 61 73 73 65 72 74  eader){.  assert
2fd82 28 20 21 70 52 65 61 64 65 72 2d 3e 65 6f 66 20  ( !pReader->eof 
2fd83 29 3b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66  );.  return leaf
2fd84 52 65 61 64 65 72 54 65 72 6d 42 79 74 65 73 28  ReaderTermBytes(
2fd85 26 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65  &pReader->leafRe
2fd86 61 64 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20  ader);.}.static 
2fd87 63 6f 6e 73 74 20 63 68 61 72 20 2a 6c 65 61 76  const char *leav
2fd88 65 73 52 65 61 64 65 72 54 65 72 6d 28 4c 65 61  esReaderTerm(Lea
2fd89 76 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64  vesReader *pRead
2fd8a 65 72 29 7b 0a 20 20 61 73 73 65 72 74 28 20 21  er){.  assert( !
2fd8b 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 29 3b 0a  pReader->eof );.
2fd8c 20 20 72 65 74 75 72 6e 20 6c 65 61 66 52 65 61    return leafRea
2fd8d 64 65 72 54 65 72 6d 28 26 70 52 65 61 64 65 72  derTerm(&pReader
2fd8e 2d 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d  ->leafReader);.}
2fd8f 0a 0a 2f 2a 20 41 63 63 65 73 73 20 74 68 65 20  ../* Access the 
2fd90 64 6f 63 6c 69 73 74 20 64 61 74 61 20 66 6f 72  doclist data for
2fd91 20 74 68 65 20 63 75 72 72 65 6e 74 20 74 65 72   the current ter
2fd92 6d 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  m. */.static int
2fd93 20 6c 65 61 76 65 73 52 65 61 64 65 72 44 61 74   leavesReaderDat
2fd94 61 42 79 74 65 73 28 4c 65 61 76 65 73 52 65 61  aBytes(LeavesRea
2fd95 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
2fd96 20 61 73 73 65 72 74 28 20 21 70 52 65 61 64 65   assert( !pReade
2fd97 72 2d 3e 65 6f 66 20 29 3b 0a 20 20 72 65 74 75  r->eof );.  retu
2fd98 72 6e 20 6c 65 61 66 52 65 61 64 65 72 44 61 74  rn leafReaderDat
2fd99 61 42 79 74 65 73 28 26 70 52 65 61 64 65 72 2d  aBytes(&pReader-
2fd9a 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 7d 0a  >leafReader);.}.
2fd9b 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68 61  static const cha
2fd9c 72 20 2a 6c 65 61 76 65 73 52 65 61 64 65 72 44  r *leavesReaderD
2fd9d 61 74 61 28 4c 65 61 76 65 73 52 65 61 64 65 72  ata(LeavesReader
2fd9e 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
2fd9f 73 65 72 74 28 20 21 70 52 65 61 64 65 72 2d 3e  sert( !pReader->
2fda0 65 6f 66 20 29 3b 0a 20 20 72 65 74 75 72 6e 20  eof );.  return 
2fda1 6c 65 61 66 52 65 61 64 65 72 44 61 74 61 28 26  leafReaderData(&
2fda2 70 52 65 61 64 65 72 2d 3e 6c 65 61 66 52 65 61  pReader->leafRea
2fda3 64 65 72 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  der);.}..static 
2fda4 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72  int leavesReader
2fda5 41 74 45 6e 64 28 4c 65 61 76 65 73 52 65 61 64  AtEnd(LeavesRead
2fda6 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2fda7 72 65 74 75 72 6e 20 70 52 65 61 64 65 72 2d 3e  return pReader->
2fda8 65 6f 66 3b 0a 7d 0a 0a 2f 2a 20 6c 6f 61 64 53  eof;.}../* loadS
2fda9 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 29 20 6d  egmentLeaves() m
2fdaa 61 79 20 6e 6f 74 20 72 65 61 64 20 61 6c 6c 20  ay not read all 
2fdab 74 68 65 20 77 61 79 20 74 6f 20 53 51 4c 49 54  the way to SQLIT
2fdac 45 5f 44 4f 4e 45 2c 20 74 68 75 73 0a 2a 2a 20  E_DONE, thus.** 
2fdad 6c 65 61 76 69 6e 67 20 74 68 65 20 73 74 61 74  leaving the stat
2fdae 65 6d 65 6e 74 20 68 61 6e 64 6c 65 20 6f 70 65  ement handle ope
2fdaf 6e 2c 20 77 68 69 63 68 20 6c 6f 63 6b 73 20 74  n, which locks t
2fdb0 68 65 20 74 61 62 6c 65 2e 0a 2a 2f 0a 2f 2a 20  he table..*/./* 
2fdb1 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
2fdb2 20 22 73 6f 6c 75 74 69 6f 6e 22 20 69 73 20 6e   "solution" is n
2fdb3 6f 74 20 73 61 74 69 73 66 61 63 74 6f 72 79 2e  ot satisfactory.
2fdb4 20 20 52 65 61 6c 6c 79 2c 20 74 68 65 72 65 0a    Really, there.
2fdb5 2a 2a 20 73 68 6f 75 6c 64 20 62 65 20 63 68 65  ** should be che
2fdb6 63 6b 2d 69 6e 20 66 75 6e 63 74 69 6f 6e 20 66  ck-in function f
2fdb7 6f 72 20 61 6c 6c 20 73 74 61 74 65 6d 65 6e 74  or all statement
2fdb8 20 68 61 6e 64 6c 65 73 20 77 68 69 63 68 0a 2a   handles which.*
2fdb9 2a 20 61 72 72 61 6e 67 65 73 20 74 6f 20 63 61  * arranges to ca
2fdba 6c 6c 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74  ll sqlite3_reset
2fdbb 28 29 2e 20 20 54 68 69 73 20 6d 6f 73 74 20 6c  ().  This most l
2fdbc 69 6b 65 6c 79 20 77 69 6c 6c 20 72 65 71 75 69  ikely will requi
2fdbd 72 65 0a 2a 2a 20 6d 6f 64 69 66 69 63 61 74 69  re.** modificati
2fdbe 6f 6e 20 74 6f 20 63 6f 6e 74 72 6f 6c 20 66 6c  on to control fl
2fdbf 6f 77 20 61 6c 6c 20 6f 76 65 72 20 74 68 65 20  ow all over the 
2fdc0 70 6c 61 63 65 2c 20 74 68 6f 75 67 68 2c 20 73  place, though, s
2fdc1 6f 20 66 6f 72 20 6e 6f 77 0a 2a 2a 20 6a 75 73  o for now.** jus
2fdc2 74 20 70 75 6e 74 2e 0a 2a 2a 0a 2a 2a 20 4e 6f  t punt..**.** No
2fdc3 74 65 20 74 68 65 20 74 68 65 20 63 75 72 72 65  te the the curre
2fdc4 6e 74 20 73 79 73 74 65 6d 20 61 73 73 75 6d 65  nt system assume
2fdc5 73 20 74 68 61 74 20 73 65 67 6d 65 6e 74 20 6d  s that segment m
2fdc6 65 72 67 65 73 20 77 69 6c 6c 20 72 75 6e 20 74  erges will run t
2fdc7 6f 0a 2a 2a 20 63 6f 6d 70 6c 65 74 69 6f 6e 2c  o.** completion,
2fdc8 20 77 68 69 63 68 20 69 73 20 77 68 79 20 74 68   which is why th
2fdc9 69 73 20 70 61 72 74 69 63 75 6c 61 72 20 70 72  is particular pr
2fdca 6f 62 61 62 6c 79 20 68 61 73 6e 27 74 20 61 72  obably hasn't ar
2fdcb 69 73 65 6e 20 69 6e 0a 2a 2a 20 74 68 69 73 20  isen in.** this 
2fdcc 63 61 73 65 2e 20 20 50 72 6f 62 61 62 6c 79 20  case.  Probably 
2fdcd 61 20 62 72 69 74 74 6c 65 20 61 73 73 75 6d 70  a brittle assump
2fdce 74 69 6f 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  tion..*/.static 
2fdcf 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65 72  int leavesReader
2fdd0 52 65 73 65 74 28 4c 65 61 76 65 73 52 65 61 64  Reset(LeavesRead
2fdd1 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
2fdd2 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 72  return sqlite3_r
2fdd3 65 73 65 74 28 70 52 65 61 64 65 72 2d 3e 70 53  eset(pReader->pS
2fdd4 74 6d 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  tmt);.}..static 
2fdd5 76 6f 69 64 20 6c 65 61 76 65 73 52 65 61 64 65  void leavesReade
2fdd6 72 44 65 73 74 72 6f 79 28 4c 65 61 76 65 73 52  rDestroy(LeavesR
2fdd7 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b  eader *pReader){
2fdd8 0a 20 20 2f 2a 20 49 66 20 69 64 78 20 69 73 20  .  /* If idx is 
2fdd9 2d 31 2c 20 74 68 61 74 20 6d 65 61 6e 73 20 77  -1, that means w
2fdda 65 27 72 65 20 75 73 69 6e 67 20 61 20 6e 6f 6e  e're using a non
2fddb 2d 63 61 63 68 65 64 20 73 74 61 74 65 6d 65 6e  -cached statemen
2fddc 74 0a 20 20 2a 2a 20 68 61 6e 64 6c 65 20 69 6e  t.  ** handle in
2fddd 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28 29 20   the optimize() 
2fdde 63 61 73 65 2c 20 73 6f 20 77 65 20 6e 65 65 64  case, so we need
2fddf 20 74 6f 20 72 65 6c 65 61 73 65 20 69 74 2e 0a   to release it..
2fde0 20 20 2a 2f 0a 20 20 69 66 28 20 70 52 65 61 64    */.  if( pRead
2fde1 65 72 2d 3e 70 53 74 6d 74 21 3d 4e 55 4c 4c 20  er->pStmt!=NULL 
2fde2 26 26 20 70 52 65 61 64 65 72 2d 3e 69 64 78 3d  && pReader->idx=
2fde3 3d 2d 31 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  =-1 ){.    sqlit
2fde4 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 65 61  e3_finalize(pRea
2fde5 64 65 72 2d 3e 70 53 74 6d 74 29 3b 0a 20 20 7d  der->pStmt);.  }
2fde6 0a 20 20 6c 65 61 66 52 65 61 64 65 72 44 65 73  .  leafReaderDes
2fde7 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e 6c  troy(&pReader->l
2fde8 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20 64 61  eafReader);.  da
2fde9 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
2fdea 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61  &pReader->rootDa
2fdeb 74 61 29 3b 0a 20 20 53 43 52 41 4d 42 4c 45 28  ta);.  SCRAMBLE(
2fdec 70 52 65 61 64 65 72 29 3b 0a 7d 0a 0a 2f 2a 20  pReader);.}../* 
2fded 49 6e 69 74 69 61 6c 69 7a 65 20 70 52 65 61 64  Initialize pRead
2fdee 65 72 20 77 69 74 68 20 74 68 65 20 67 69 76 65  er with the give
2fdef 6e 20 72 6f 6f 74 20 64 61 74 61 20 28 69 66 20  n root data (if 
2fdf0 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 3d 3d 30  iStartBlockid==0
2fdf1 0a 2a 2a 20 74 68 65 20 6c 65 61 66 20 64 61 74  .** the leaf dat
2fdf2 61 20 77 61 73 20 65 6e 74 69 72 65 6c 79 20 63  a was entirely c
2fdf3 6f 6e 74 61 69 6e 65 64 20 69 6e 20 74 68 65 20  ontained in the 
2fdf4 72 6f 6f 74 29 2c 20 6f 72 20 66 72 6f 6d 20 74  root), or from t
2fdf5 68 65 0a 2a 2a 20 73 74 72 65 61 6d 20 6f 66 20  he.** stream of 
2fdf6 62 6c 6f 63 6b 73 20 62 65 74 77 65 65 6e 20 69  blocks between i
2fdf7 53 74 61 72 74 42 6c 6f 63 6b 69 64 20 61 6e 64  StartBlockid and
2fdf8 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 20 69 6e   iEndBlockid, in
2fdf9 63 6c 75 73 69 76 65 2e 0a 2a 2f 0a 73 74 61 74  clusive..*/.stat
2fdfa 69 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61  ic int leavesRea
2fdfb 64 65 72 49 6e 69 74 28 66 75 6c 6c 74 65 78 74  derInit(fulltext
2fdfc 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20  _vtab *v,.      
2fdfd 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fdfe 20 20 20 20 20 20 69 6e 74 20 69 64 78 2c 0a 20        int idx,. 
2fdff 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe00 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2fe01 65 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c  e_int64 iStartBl
2fe02 6f 63 6b 69 64 2c 0a 20 20 20 20 20 20 20 20 20  ockid,.         
2fe03 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe04 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
2fe05 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a 20 20 20  iEndBlockid,.   
2fe06 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe07 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63           const c
2fe08 68 61 72 20 2a 70 52 6f 6f 74 44 61 74 61 2c 20  har *pRootData, 
2fe09 69 6e 74 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20  int nRootData,. 
2fe0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe0b 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 76 65             Leave
2fe0c 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
2fe0d 29 7b 0a 20 20 43 4c 45 41 52 28 70 52 65 61 64  ){.  CLEAR(pRead
2fe0e 65 72 29 3b 0a 20 20 70 52 65 61 64 65 72 2d 3e  er);.  pReader->
2fe0f 69 64 78 20 3d 20 69 64 78 3b 0a 0a 20 20 64 61  idx = idx;..  da
2fe10 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 70 52  taBufferInit(&pR
2fe11 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61 74 61 2c  eader->rootData,
2fe12 20 30 29 3b 0a 20 20 69 66 28 20 69 53 74 61 72   0);.  if( iStar
2fe13 74 42 6c 6f 63 6b 69 64 3d 3d 30 20 29 7b 0a 20  tBlockid==0 ){. 
2fe14 20 20 20 2f 2a 20 45 6e 74 69 72 65 20 6c 65 61     /* Entire lea
2fe15 66 20 6c 65 76 65 6c 20 66 69 74 20 69 6e 20 72  f level fit in r
2fe16 6f 6f 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 20  oot data. */.   
2fe17 20 64 61 74 61 42 75 66 66 65 72 52 65 70 6c 61   dataBufferRepla
2fe18 63 65 28 26 70 52 65 61 64 65 72 2d 3e 72 6f 6f  ce(&pReader->roo
2fe19 74 44 61 74 61 2c 20 70 52 6f 6f 74 44 61 74 61  tData, pRootData
2fe1a 2c 20 6e 52 6f 6f 74 44 61 74 61 29 3b 0a 20 20  , nRootData);.  
2fe1b 20 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74    leafReaderInit
2fe1c 28 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61  (pReader->rootDa
2fe1d 74 61 2e 70 44 61 74 61 2c 20 70 52 65 61 64 65  ta.pData, pReade
2fe1e 72 2d 3e 72 6f 6f 74 44 61 74 61 2e 6e 44 61 74  r->rootData.nDat
2fe1f 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
2fe20 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d 3e        &pReader->
2fe21 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20 7d  leafReader);.  }
2fe22 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65  else{.    sqlite
2fe23 33 5f 73 74 6d 74 20 2a 73 3b 0a 20 20 20 20 69  3_stmt *s;.    i
2fe24 6e 74 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f  nt rc = sql_get_
2fe25 6c 65 61 66 5f 73 74 61 74 65 6d 65 6e 74 28 76  leaf_statement(v
2fe26 2c 20 69 64 78 2c 20 26 73 29 3b 0a 20 20 20 20  , idx, &s);.    
2fe27 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f  if( rc!=SQLITE_O
2fe28 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a  K ) return rc;..
2fe29 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
2fe2a 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73 2c 20 31  _bind_int64(s, 1
2fe2b 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64 29  , iStartBlockid)
2fe2c 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53 51  ;.    if( rc!=SQ
2fe2d 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2fe2e 20 72 63 3b 0a 0a 20 20 20 20 72 63 20 3d 20 73   rc;..    rc = s
2fe2f 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
2fe30 34 28 73 2c 20 32 2c 20 69 45 6e 64 42 6c 6f 63  4(s, 2, iEndBloc
2fe31 6b 69 64 29 3b 0a 20 20 20 20 69 66 28 20 72 63  kid);.    if( rc
2fe32 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2fe33 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 72 63  turn rc;..    rc
2fe34 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28   = sqlite3_step(
2fe35 73 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  s);.    if( rc==
2fe36 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20  SQLITE_DONE ){. 
2fe37 20 20 20 20 20 70 52 65 61 64 65 72 2d 3e 65 6f       pReader->eo
2fe38 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 72 65 74  f = 1;.      ret
2fe39 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  urn SQLITE_OK;. 
2fe3a 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63 21     }.    if( rc!
2fe3b 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
2fe3c 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20 70 52  turn rc;..    pR
2fe3d 65 61 64 65 72 2d 3e 70 53 74 6d 74 20 3d 20 73  eader->pStmt = s
2fe3e 3b 0a 20 20 20 20 6c 65 61 66 52 65 61 64 65 72  ;.    leafReader
2fe3f 49 6e 69 74 28 73 71 6c 69 74 65 33 5f 63 6f 6c  Init(sqlite3_col
2fe40 75 6d 6e 5f 62 6c 6f 62 28 70 52 65 61 64 65 72  umn_blob(pReader
2fe41 2d 3e 70 53 74 6d 74 2c 20 30 29 2c 0a 20 20 20  ->pStmt, 0),.   
2fe42 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe43 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2fe44 79 74 65 73 28 70 52 65 61 64 65 72 2d 3e 70 53  ytes(pReader->pS
2fe45 74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20  tmt, 0),.       
2fe46 20 20 20 20 20 20 20 20 20 20 20 20 26 70 52 65              &pRe
2fe47 61 64 65 72 2d 3e 6c 65 61 66 52 65 61 64 65 72  ader->leafReader
2fe48 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  );.  }.  return 
2fe49 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
2fe4a 20 53 74 65 70 20 74 68 65 20 63 75 72 72 65 6e   Step the curren
2fe4b 74 20 6c 65 61 66 20 66 6f 72 77 61 72 64 20 74  t leaf forward t
2fe4c 6f 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d 2e  o the next term.
2fe4d 20 20 49 66 20 77 65 20 72 65 61 63 68 20 74 68    If we reach th
2fe4e 65 0a 2a 2a 20 65 6e 64 20 6f 66 20 74 68 65 20  e.** end of the 
2fe4f 63 75 72 72 65 6e 74 20 6c 65 61 66 2c 20 73 74  current leaf, st
2fe50 65 70 20 66 6f 72 77 61 72 64 20 74 6f 20 74 68  ep forward to th
2fe51 65 20 6e 65 78 74 20 6c 65 61 66 20 62 6c 6f 63  e next leaf bloc
2fe52 6b 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  k..*/.static int
2fe53 20 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65   leavesReaderSte
2fe54 70 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  p(fulltext_vtab 
2fe55 2a 76 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72  *v, LeavesReader
2fe56 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20 61 73   *pReader){.  as
2fe57 73 65 72 74 28 20 21 6c 65 61 76 65 73 52 65 61  sert( !leavesRea
2fe58 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65 72  derAtEnd(pReader
2fe59 29 20 29 3b 0a 20 20 6c 65 61 66 52 65 61 64 65  ) );.  leafReade
2fe5a 72 53 74 65 70 28 26 70 52 65 61 64 65 72 2d 3e  rStep(&pReader->
2fe5b 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 0a 20 20  leafReader);..  
2fe5c 69 66 28 20 6c 65 61 66 52 65 61 64 65 72 41 74  if( leafReaderAt
2fe5d 45 6e 64 28 26 70 52 65 61 64 65 72 2d 3e 6c 65  End(&pReader->le
2fe5e 61 66 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20  afReader) ){.   
2fe5f 20 69 6e 74 20 72 63 3b 0a 20 20 20 20 69 66 28   int rc;.    if(
2fe60 20 70 52 65 61 64 65 72 2d 3e 72 6f 6f 74 44 61   pReader->rootDa
2fe61 74 61 2e 70 44 61 74 61 20 29 7b 0a 20 20 20 20  ta.pData ){.    
2fe62 20 20 70 52 65 61 64 65 72 2d 3e 65 6f 66 20 3d    pReader->eof =
2fe63 20 31 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e   1;.      return
2fe64 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20   SQLITE_OK;.    
2fe65 7d 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74  }.    rc = sqlit
2fe66 65 33 5f 73 74 65 70 28 70 52 65 61 64 65 72 2d  e3_step(pReader-
2fe67 3e 70 53 74 6d 74 29 3b 0a 20 20 20 20 69 66 28  >pStmt);.    if(
2fe68 20 72 63 21 3d 53 51 4c 49 54 45 5f 52 4f 57 20   rc!=SQLITE_ROW 
2fe69 29 7b 0a 20 20 20 20 20 20 70 52 65 61 64 65 72  ){.      pReader
2fe6a 2d 3e 65 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20  ->eof = 1;.     
2fe6b 20 72 65 74 75 72 6e 20 72 63 3d 3d 53 51 4c 49   return rc==SQLI
2fe6c 54 45 5f 44 4f 4e 45 20 3f 20 53 51 4c 49 54 45  TE_DONE ? SQLITE
2fe6d 5f 4f 4b 20 3a 20 72 63 3b 0a 20 20 20 20 7d 0a  _OK : rc;.    }.
2fe6e 20 20 20 20 6c 65 61 66 52 65 61 64 65 72 44 65      leafReaderDe
2fe6f 73 74 72 6f 79 28 26 70 52 65 61 64 65 72 2d 3e  stroy(&pReader->
2fe70 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20 20  leafReader);.   
2fe71 20 6c 65 61 66 52 65 61 64 65 72 49 6e 69 74 28   leafReaderInit(
2fe72 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62  sqlite3_column_b
2fe73 6c 6f 62 28 70 52 65 61 64 65 72 2d 3e 70 53 74  lob(pReader->pSt
2fe74 6d 74 2c 20 30 29 2c 0a 20 20 20 20 20 20 20 20  mt, 0),.        
2fe75 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74             sqlit
2fe76 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28  e3_column_bytes(
2fe77 70 52 65 61 64 65 72 2d 3e 70 53 74 6d 74 2c 20  pReader->pStmt, 
2fe78 30 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  0),.            
2fe79 20 20 20 20 20 20 20 26 70 52 65 61 64 65 72 2d         &pReader-
2fe7a 3e 6c 65 61 66 52 65 61 64 65 72 29 3b 0a 20 20  >leafReader);.  
2fe7b 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  }.  return SQLIT
2fe7c 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4f 72 64 65  E_OK;.}../* Orde
2fe7d 72 20 4c 65 61 76 65 73 52 65 61 64 65 72 73 20  r LeavesReaders 
2fe7e 62 79 20 74 68 65 69 72 20 74 65 72 6d 2c 20 69  by their term, i
2fe7f 67 6e 6f 72 69 6e 67 20 69 64 78 2e 20 20 52 65  gnoring idx.  Re
2fe80 61 64 65 72 73 20 61 74 20 65 6f 66 0a 2a 2a 20  aders at eof.** 
2fe81 61 6c 77 61 79 73 20 73 6f 72 74 20 74 6f 20 74  always sort to t
2fe82 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  he end..*/.stati
2fe83 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
2fe84 65 72 54 65 72 6d 43 6d 70 28 4c 65 61 76 65 73  erTermCmp(Leaves
2fe85 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4c 65 61  Reader *lr1, Lea
2fe86 76 65 73 52 65 61 64 65 72 20 2a 6c 72 32 29 7b  vesReader *lr2){
2fe87 0a 20 20 69 66 28 20 6c 65 61 76 65 73 52 65 61  .  if( leavesRea
2fe88 64 65 72 41 74 45 6e 64 28 6c 72 31 29 20 29 7b  derAtEnd(lr1) ){
2fe89 0a 20 20 20 20 69 66 28 20 6c 65 61 76 65 73 52  .    if( leavesR
2fe8a 65 61 64 65 72 41 74 45 6e 64 28 6c 72 32 29 20  eaderAtEnd(lr2) 
2fe8b 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
2fe8c 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d 0a 20 20  return 1;.  }.  
2fe8d 69 66 28 20 6c 65 61 76 65 73 52 65 61 64 65 72  if( leavesReader
2fe8e 41 74 45 6e 64 28 6c 72 32 29 20 29 20 72 65 74  AtEnd(lr2) ) ret
2fe8f 75 72 6e 20 2d 31 3b 0a 0a 20 20 72 65 74 75 72  urn -1;..  retur
2fe90 6e 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d  n leafReaderTerm
2fe91 43 6d 70 28 26 6c 72 31 2d 3e 6c 65 61 66 52 65  Cmp(&lr1->leafRe
2fe92 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20 20 20  ader,.          
2fe93 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fe94 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
2fe95 6d 28 6c 72 32 29 2c 20 6c 65 61 76 65 73 52 65  m(lr2), leavesRe
2fe96 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 6c 72  aderTermBytes(lr
2fe97 32 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  2),.            
2fe98 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 30                 0
2fe99 29 3b 0a 7d 0a 0a 2f 2a 20 53 69 6d 69 6c 61 72  );.}../* Similar
2fe9a 20 74 6f 20 6c 65 61 76 65 73 52 65 61 64 65 72   to leavesReader
2fe9b 54 65 72 6d 43 6d 70 28 29 2c 20 77 69 74 68 20  TermCmp(), with 
2fe9c 61 64 64 69 74 69 6f 6e 61 6c 20 6f 72 64 65 72  additional order
2fe9d 69 6e 67 20 62 79 20 69 64 78 0a 2a 2a 20 73 6f  ing by idx.** so
2fe9e 20 74 68 61 74 20 6f 6c 64 65 72 20 73 65 67 6d   that older segm
2fe9f 65 6e 74 73 20 73 6f 72 74 20 62 65 66 6f 72 65  ents sort before
2fea0 20 6e 65 77 65 72 20 73 65 67 6d 65 6e 74 73 2e   newer segments.
2fea1 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c  .*/.static int l
2fea2 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28 4c  eavesReaderCmp(L
2fea3 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72 31  eavesReader *lr1
2fea4 2c 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  , LeavesReader *
2fea5 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20  lr2){.  int c = 
2fea6 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
2fea7 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29 3b 0a 20  Cmp(lr1, lr2);. 
2fea8 20 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75   if( c!=0 ) retu
2fea9 72 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 6c  rn c;.  return l
2feaa 72 31 2d 3e 69 64 78 2d 6c 72 32 2d 3e 69 64 78  r1->idx-lr2->idx
2feab 3b 0a 7d 0a 0a 2f 2a 20 41 73 73 75 6d 65 20 74  ;.}../* Assume t
2feac 68 61 74 20 70 4c 72 5b 31 5d 2e 2e 70 4c 72 5b  hat pLr[1]..pLr[
2fead 6e 4c 72 5d 20 61 72 65 20 73 6f 72 74 65 64 2e  nLr] are sorted.
2feae 20 20 42 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20    Bubble pLr[0] 
2feaf 69 6e 74 6f 20 69 74 73 0a 2a 2a 20 73 6f 72 74  into its.** sort
2feb0 65 64 20 70 6f 73 69 74 69 6f 6e 2e 0a 2a 2f 0a  ed position..*/.
2feb1 73 74 61 74 69 63 20 76 6f 69 64 20 6c 65 61 76  static void leav
2feb2 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  esReaderReorder(
2feb3 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 4c  LeavesReader *pL
2feb4 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20 20 77  r, int nLr){.  w
2feb5 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26 20 6c  hile( nLr>1 && l
2feb6 65 61 76 65 73 52 65 61 64 65 72 43 6d 70 28 70  eavesReaderCmp(p
2feb7 4c 72 2c 20 70 4c 72 2b 31 29 3e 30 20 29 7b 0a  Lr, pLr+1)>0 ){.
2feb8 20 20 20 20 4c 65 61 76 65 73 52 65 61 64 65 72      LeavesReader
2feb9 20 74 6d 70 20 3d 20 70 4c 72 5b 30 5d 3b 0a 20   tmp = pLr[0];. 
2feba 20 20 20 70 4c 72 5b 30 5d 20 3d 20 70 4c 72 5b     pLr[0] = pLr[
2febb 31 5d 3b 0a 20 20 20 20 70 4c 72 5b 31 5d 20 3d  1];.    pLr[1] =
2febc 20 74 6d 70 3b 0a 20 20 20 20 6e 4c 72 2d 2d 3b   tmp;.    nLr--;
2febd 0a 20 20 20 20 70 4c 72 2b 2b 3b 0a 20 20 7d 0a  .    pLr++;.  }.
2febe 7d 0a 0a 2f 2a 20 49 6e 69 74 69 61 6c 69 7a 65  }../* Initialize
2febf 73 20 70 52 65 61 64 65 72 73 20 77 69 74 68 20  s pReaders with 
2fec0 74 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72 6f  the segments fro
2fec1 6d 20 6c 65 76 65 6c 20 69 4c 65 76 65 6c 2c 20  m level iLevel, 
2fec2 72 65 74 75 72 6e 69 6e 67 0a 2a 2a 20 74 68 65  returning.** the
2fec3 20 6e 75 6d 62 65 72 20 6f 66 20 73 65 67 6d 65   number of segme
2fec4 6e 74 73 20 69 6e 20 2a 70 69 52 65 61 64 65 72  nts in *piReader
2fec5 73 2e 20 20 4c 65 61 76 65 73 20 70 52 65 61 64  s.  Leaves pRead
2fec6 65 72 73 20 69 6e 20 73 6f 72 74 65 64 0a 2a 2a  ers in sorted.**
2fec7 20 6f 72 64 65 72 2e 0a 2a 2f 0a 73 74 61 74 69   order..*/.stati
2fec8 63 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64  c int leavesRead
2fec9 65 72 73 49 6e 69 74 28 66 75 6c 6c 74 65 78 74  ersInit(fulltext
2feca 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74 20 69 4c  _vtab *v, int iL
2fecb 65 76 65 6c 2c 0a 20 20 20 20 20 20 20 20 20 20  evel,.          
2fecc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fecd 20 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20     LeavesReader 
2fece 2a 70 52 65 61 64 65 72 73 2c 20 69 6e 74 20 2a  *pReaders, int *
2fecf 70 69 52 65 61 64 65 72 73 29 7b 0a 20 20 73 71  piReaders){.  sq
2fed0 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 3b 0a 20  lite3_stmt *s;. 
2fed1 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 73 71 6c   int i, rc = sql
2fed2 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
2fed3 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
2fed4 4c 45 56 45 4c 5f 53 54 4d 54 2c 20 26 73 29 3b  LEVEL_STMT, &s);
2fed5 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
2fed6 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
2fed7 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65  ;..  rc = sqlite
2fed8 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
2fed9 20 69 4c 65 76 65 6c 29 3b 0a 20 20 69 66 28 20   iLevel);.  if( 
2feda 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
2fedb 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 69 20  return rc;..  i 
2fedc 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 28 72  = 0;.  while( (r
2fedd 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70  c = sqlite3_step
2fede 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57  (s))==SQLITE_ROW
2fedf 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 5f 69   ){.    sqlite_i
2fee0 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20 73 71  nt64 iStart = sq
2fee1 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
2fee2 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20 73 71  64(s, 0);.    sq
2fee3 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e 64 20  lite_int64 iEnd 
2fee4 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
2fee5 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a 20 20  _int64(s, 1);.  
2fee6 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 52    const char *pR
2fee7 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65  ootData = sqlite
2fee8 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28 73 2c  3_column_blob(s,
2fee9 20 32 29 3b 0a 20 20 20 20 69 6e 74 20 6e 52 6f   2);.    int nRo
2feea 6f 74 44 61 74 61 20 3d 20 73 71 6c 69 74 65 33  otData = sqlite3
2feeb 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65 73 28 73 2c  _column_bytes(s,
2feec 20 32 29 3b 0a 0a 20 20 20 20 61 73 73 65 72 74   2);..    assert
2feed 28 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20  ( i<MERGE_COUNT 
2feee 29 3b 0a 20 20 20 20 72 63 20 3d 20 6c 65 61 76  );.    rc = leav
2feef 65 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c 20  esReaderInit(v, 
2fef0 69 2c 20 69 53 74 61 72 74 2c 20 69 45 6e 64 2c  i, iStart, iEnd,
2fef1 20 70 52 6f 6f 74 44 61 74 61 2c 20 6e 52 6f 6f   pRootData, nRoo
2fef2 74 44 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20  tData,.         
2fef3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2fef4 20 26 70 52 65 61 64 65 72 73 5b 69 5d 29 3b 0a   &pReaders[i]);.
2fef5 20 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49      if( rc!=SQLI
2fef6 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 0a  TE_OK ) break;..
2fef7 20 20 20 20 69 2b 2b 3b 0a 20 20 7d 0a 20 20 69      i++;.  }.  i
2fef8 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44 4f  f( rc!=SQLITE_DO
2fef9 4e 45 20 29 7b 0a 20 20 20 20 77 68 69 6c 65 28  NE ){.    while(
2fefa 20 69 2d 2d 3e 30 20 29 7b 0a 20 20 20 20 20 20   i-->0 ){.      
2fefb 6c 65 61 76 65 73 52 65 61 64 65 72 44 65 73 74  leavesReaderDest
2fefc 72 6f 79 28 26 70 52 65 61 64 65 72 73 5b 69 5d  roy(&pReaders[i]
2fefd 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 72 65 74  );.    }.    ret
2fefe 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 2a  urn rc;.  }..  *
2feff 70 69 52 65 61 64 65 72 73 20 3d 20 69 3b 0a 0a  piReaders = i;..
2ff00 20 20 2f 2a 20 4c 65 61 76 65 20 6f 75 72 20 72    /* Leave our r
2ff01 65 73 75 6c 74 73 20 73 6f 72 74 65 64 20 62 79  esults sorted by
2ff02 20 74 65 72 6d 2c 20 74 68 65 6e 20 61 67 65 2e   term, then age.
2ff03 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 69 2d 2d   */.  while( i--
2ff04 20 29 7b 0a 20 20 20 20 6c 65 61 76 65 73 52 65   ){.    leavesRe
2ff05 61 64 65 72 52 65 6f 72 64 65 72 28 70 52 65 61  aderReorder(pRea
2ff06 64 65 72 73 2b 69 2c 20 2a 70 69 52 65 61 64 65  ders+i, *piReade
2ff07 72 73 2d 69 29 3b 0a 20 20 7d 0a 20 20 72 65 74  rs-i);.  }.  ret
2ff08 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
2ff09 0a 0a 2f 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69  ../* Merge docli
2ff0a 73 74 73 20 66 72 6f 6d 20 70 52 65 61 64 65 72  sts from pReader
2ff0b 73 5b 6e 52 65 61 64 65 72 73 5d 20 69 6e 74 6f  s[nReaders] into
2ff0c 20 61 20 73 69 6e 67 6c 65 20 64 6f 63 6c 69 73   a single doclis
2ff0d 74 2c 20 77 68 69 63 68 0a 2a 2a 20 69 73 20 77  t, which.** is w
2ff0e 72 69 74 74 65 6e 20 74 6f 20 70 57 72 69 74 65  ritten to pWrite
2ff0f 72 2e 20 20 41 73 73 75 6d 65 73 20 70 52 65 61  r.  Assumes pRea
2ff10 64 65 72 73 20 69 73 20 6f 72 64 65 72 65 64 20  ders is ordered 
2ff11 6f 6c 64 65 73 74 20 74 6f 0a 2a 2a 20 6e 65 77  oldest to.** new
2ff12 65 73 74 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28  est..*/./* TODO(
2ff13 73 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20  shess) Consider 
2ff14 70 75 74 74 69 6e 67 20 74 68 69 73 20 69 6e 6c  putting this inl
2ff15 69 6e 65 20 69 6e 20 73 65 67 6d 65 6e 74 4d 65  ine in segmentMe
2ff16 72 67 65 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63  rge(). */.static
2ff17 20 69 6e 74 20 6c 65 61 76 65 73 52 65 61 64 65   int leavesReade
2ff18 72 73 4d 65 72 67 65 28 66 75 6c 6c 74 65 78 74  rsMerge(fulltext
2ff19 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20  _vtab *v,.      
2ff1a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff1b 20 20 20 20 20 20 20 20 4c 65 61 76 65 73 52 65          LeavesRe
2ff1c 61 64 65 72 20 2a 70 52 65 61 64 65 72 73 2c 20  ader *pReaders, 
2ff1d 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a 20 20  int nReaders,.  
2ff1e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ff1f 20 20 20 20 20 20 20 20 20 20 20 20 4c 65 61 66              Leaf
2ff20 57 72 69 74 65 72 20 2a 70 57 72 69 74 65 72 29  Writer *pWriter)
2ff21 7b 0a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52  {.  DLReader dlR
2ff22 65 61 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55  eaders[MERGE_COU
2ff23 4e 54 5d 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  NT];.  const cha
2ff24 72 20 2a 70 54 65 72 6d 20 3d 20 6c 65 61 76 65  r *pTerm = leave
2ff25 73 52 65 61 64 65 72 54 65 72 6d 28 70 52 65 61  sReaderTerm(pRea
2ff26 64 65 72 73 29 3b 0a 20 20 69 6e 74 20 69 2c 20  ders);.  int i, 
2ff27 6e 54 65 72 6d 20 3d 20 6c 65 61 76 65 73 52 65  nTerm = leavesRe
2ff28 61 64 65 72 54 65 72 6d 42 79 74 65 73 28 70 52  aderTermBytes(pR
2ff29 65 61 64 65 72 73 29 3b 0a 0a 20 20 61 73 73 65  eaders);..  asse
2ff2a 72 74 28 20 6e 52 65 61 64 65 72 73 3c 3d 4d 45  rt( nReaders<=ME
2ff2b 52 47 45 5f 43 4f 55 4e 54 20 29 3b 0a 0a 20 20  RGE_COUNT );..  
2ff2c 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 52 65 61 64  for(i=0; i<nRead
2ff2d 65 72 73 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 64  ers; i++){.    d
2ff2e 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64 65 72  lrInit(&dlReader
2ff2f 73 5b 69 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54  s[i], DL_DEFAULT
2ff30 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 65  ,.            le
2ff31 61 76 65 73 52 65 61 64 65 72 44 61 74 61 28 70  avesReaderData(p
2ff32 52 65 61 64 65 72 73 2b 69 29 2c 0a 20 20 20 20  Readers+i),.    
2ff33 20 20 20 20 20 20 20 20 6c 65 61 76 65 73 52 65          leavesRe
2ff34 61 64 65 72 44 61 74 61 42 79 74 65 73 28 70 52  aderDataBytes(pR
2ff35 65 61 64 65 72 73 2b 69 29 29 3b 0a 20 20 7d 0a  eaders+i));.  }.
2ff36 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 66 57 72  .  return leafWr
2ff37 69 74 65 72 53 74 65 70 4d 65 72 67 65 28 76 2c  iterStepMerge(v,
2ff38 20 70 57 72 69 74 65 72 2c 20 70 54 65 72 6d 2c   pWriter, pTerm,
2ff39 20 6e 54 65 72 6d 2c 20 64 6c 52 65 61 64 65 72   nTerm, dlReader
2ff3a 73 2c 20 6e 52 65 61 64 65 72 73 29 3b 0a 7d 0a  s, nReaders);.}.
2ff3b 0a 2f 2a 20 46 6f 72 77 61 72 64 20 72 65 66 20  ./* Forward ref 
2ff3c 64 75 65 20 74 6f 20 6d 75 74 75 61 6c 20 72 65  due to mutual re
2ff3d 63 75 72 73 69 6f 6e 20 77 69 74 68 20 73 65 67  cursion with seg
2ff3e 64 69 72 4e 65 78 74 49 6e 64 65 78 28 29 2e 20  dirNextIndex(). 
2ff3f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 65  */.static int se
2ff40 67 6d 65 6e 74 4d 65 72 67 65 28 66 75 6c 6c 74  gmentMerge(fullt
2ff41 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69 6e 74  ext_vtab *v, int
2ff42 20 69 4c 65 76 65 6c 29 3b 0a 0a 2f 2a 20 50 75   iLevel);../* Pu
2ff43 74 20 74 68 65 20 6e 65 78 74 20 61 76 61 69 6c  t the next avail
2ff44 61 62 6c 65 20 69 6e 64 65 78 20 61 74 20 69 4c  able index at iL
2ff45 65 76 65 6c 20 69 6e 74 6f 20 2a 70 69 64 78 2e  evel into *pidx.
2ff46 20 20 49 66 20 69 4c 65 76 65 6c 0a 2a 2a 20 61    If iLevel.** a
2ff47 6c 72 65 61 64 79 20 68 61 73 20 4d 45 52 47 45  lready has MERGE
2ff48 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c  _COUNT segments,
2ff49 20 74 68 65 79 20 61 72 65 20 6d 65 72 67 65 64   they are merged
2ff4a 20 74 6f 20 61 20 68 69 67 68 65 72 0a 2a 2a 20   to a higher.** 
2ff4b 6c 65 76 65 6c 20 74 6f 20 6d 61 6b 65 20 72 6f  level to make ro
2ff4c 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  om..*/.static in
2ff4d 74 20 73 65 67 64 69 72 4e 65 78 74 49 6e 64 65  t segdirNextInde
2ff4e 78 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20  x(fulltext_vtab 
2ff4f 2a 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 2c 20  *v, int iLevel, 
2ff50 69 6e 74 20 2a 70 69 64 78 29 7b 0a 20 20 69 6e  int *pidx){.  in
2ff51 74 20 72 63 20 3d 20 73 65 67 64 69 72 5f 6d 61  t rc = segdir_ma
2ff52 78 5f 69 6e 64 65 78 28 76 2c 20 69 4c 65 76 65  x_index(v, iLeve
2ff53 6c 2c 20 70 69 64 78 29 3b 0a 20 20 69 66 28 20  l, pidx);.  if( 
2ff54 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
2ff55 29 7b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ){              
2ff56 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73 20 61  /* No segments a
2ff57 74 20 69 4c 65 76 65 6c 2e 20 2a 2f 0a 20 20 20  t iLevel. */.   
2ff58 20 2a 70 69 64 78 20 3d 20 30 3b 0a 20 20 7d 65   *pidx = 0;.  }e
2ff59 6c 73 65 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  lse if( rc==SQLI
2ff5a 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 69 66  TE_ROW ){.    if
2ff5b 28 20 2a 70 69 64 78 3d 3d 28 4d 45 52 47 45 5f  ( *pidx==(MERGE_
2ff5c 43 4f 55 4e 54 2d 31 29 20 29 7b 0a 20 20 20 20  COUNT-1) ){.    
2ff5d 20 20 72 63 20 3d 20 73 65 67 6d 65 6e 74 4d 65    rc = segmentMe
2ff5e 72 67 65 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a  rge(v, iLevel);.
2ff5f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
2ff60 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
2ff61 20 72 63 3b 0a 20 20 20 20 20 20 2a 70 69 64 78   rc;.      *pidx
2ff62 20 3d 20 30 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   = 0;.    }else{
2ff63 0a 20 20 20 20 20 20 28 2a 70 69 64 78 29 2b 2b  .      (*pidx)++
2ff64 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b  ;.    }.  }else{
2ff65 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  .    return rc;.
2ff66 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51 4c    }.  return SQL
2ff67 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 4d 65  ITE_OK;.}../* Me
2ff68 72 67 65 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  rge MERGE_COUNT 
2ff69 73 65 67 6d 65 6e 74 73 20 61 74 20 69 4c 65 76  segments at iLev
2ff6a 65 6c 20 69 6e 74 6f 20 61 20 6e 65 77 20 73 65  el into a new se
2ff6b 67 6d 65 6e 74 20 61 74 0a 2a 2a 20 69 4c 65 76  gment at.** iLev
2ff6c 65 6c 2b 31 2e 20 20 49 66 20 69 4c 65 76 65 6c  el+1.  If iLevel
2ff6d 2b 31 20 69 73 20 61 6c 72 65 61 64 79 20 66 75  +1 is already fu
2ff6e 6c 6c 20 6f 66 20 73 65 67 6d 65 6e 74 73 2c 20  ll of segments, 
2ff6f 74 68 6f 73 65 20 77 69 6c 6c 20 62 65 0a 2a 2a  those will be.**
2ff70 20 6d 65 72 67 65 64 20 74 6f 20 6d 61 6b 65 20   merged to make 
2ff71 72 6f 6f 6d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  room..*/.static 
2ff72 69 6e 74 20 73 65 67 6d 65 6e 74 4d 65 72 67 65  int segmentMerge
2ff73 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a  (fulltext_vtab *
2ff74 76 2c 20 69 6e 74 20 69 4c 65 76 65 6c 29 7b 0a  v, int iLevel){.
2ff75 20 20 4c 65 61 66 57 72 69 74 65 72 20 77 72 69    LeafWriter wri
2ff76 74 65 72 3b 0a 20 20 4c 65 61 76 65 73 52 65 61  ter;.  LeavesRea
2ff77 64 65 72 20 6c 72 73 5b 4d 45 52 47 45 5f 43 4f  der lrs[MERGE_CO
2ff78 55 4e 54 5d 3b 0a 20 20 69 6e 74 20 69 2c 20 72  UNT];.  int i, r
2ff79 63 2c 20 69 64 78 20 3d 20 30 3b 0a 0a 20 20 2f  c, idx = 0;..  /
2ff7a 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65 20  * Determine the 
2ff7b 6e 65 78 74 20 61 76 61 69 6c 61 62 6c 65 20 73  next available s
2ff7c 65 67 6d 65 6e 74 20 69 6e 64 65 78 20 61 74 20  egment index at 
2ff7d 74 68 65 20 6e 65 78 74 20 6c 65 76 65 6c 2c 0a  the next level,.
2ff7e 20 20 2a 2a 20 6d 65 72 67 69 6e 67 20 61 73 20    ** merging as 
2ff7f 6e 65 63 65 73 73 61 72 79 2e 0a 20 20 2a 2f 0a  necessary..  */.
2ff80 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e 65 78    rc = segdirNex
2ff81 74 49 6e 64 65 78 28 76 2c 20 69 4c 65 76 65 6c  tIndex(v, iLevel
2ff82 2b 31 2c 20 26 69 64 78 29 3b 0a 20 20 69 66 28  +1, &idx);.  if(
2ff83 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc!=SQLITE_OK )
2ff84 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 2f   return rc;..  /
2ff85 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
2ff86 69 73 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  is assumes that 
2ff87 77 65 27 6c 6c 20 61 6c 77 61 79 73 20 73 65 65  we'll always see
2ff88 20 65 78 61 63 74 6c 79 0a 20 20 2a 2a 20 4d 45   exactly.  ** ME
2ff89 52 47 45 5f 43 4f 55 4e 54 20 73 65 67 6d 65 6e  RGE_COUNT segmen
2ff8a 74 73 20 74 6f 20 6d 65 72 67 65 20 61 74 20 61  ts to merge at a
2ff8b 20 67 69 76 65 6e 20 6c 65 76 65 6c 2e 20 20 54   given level.  T
2ff8c 68 61 74 20 77 69 6c 6c 20 62 65 0a 20 20 2a 2a  hat will be.  **
2ff8d 20 62 72 6f 6b 65 6e 20 69 66 20 77 65 20 61 6c   broken if we al
2ff8e 6c 6f 77 20 74 68 65 20 64 65 76 65 6c 6f 70 65  low the develope
2ff8f 72 20 74 6f 20 72 65 71 75 65 73 74 20 70 72 65  r to request pre
2ff90 65 6d 70 74 69 76 65 20 6f 72 0a 20 20 2a 2a 20  emptive or.  ** 
2ff91 64 65 66 65 72 72 65 64 20 6d 65 72 67 69 6e 67  deferred merging
2ff92 2e 0a 20 20 2a 2f 0a 20 20 6d 65 6d 73 65 74 28  ..  */.  memset(
2ff93 26 6c 72 73 2c 20 27 5c 30 27 2c 20 73 69 7a 65  &lrs, '\0', size
2ff94 6f 66 28 6c 72 73 29 29 3b 0a 20 20 72 63 20 3d  of(lrs));.  rc =
2ff95 20 6c 65 61 76 65 73 52 65 61 64 65 72 73 49 6e   leavesReadersIn
2ff96 69 74 28 76 2c 20 69 4c 65 76 65 6c 2c 20 6c 72  it(v, iLevel, lr
2ff97 73 2c 20 26 69 29 3b 0a 20 20 69 66 28 20 72 63  s, &i);.  if( rc
2ff98 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
2ff99 74 75 72 6e 20 72 63 3b 0a 20 20 61 73 73 65 72  turn rc;.  asser
2ff9a 74 28 20 69 3d 3d 4d 45 52 47 45 5f 43 4f 55 4e  t( i==MERGE_COUN
2ff9b 54 20 29 3b 0a 0a 20 20 6c 65 61 66 57 72 69 74  T );..  leafWrit
2ff9c 65 72 49 6e 69 74 28 69 4c 65 76 65 6c 2b 31 2c  erInit(iLevel+1,
2ff9d 20 69 64 78 2c 20 26 77 72 69 74 65 72 29 3b 0a   idx, &writer);.
2ff9e 0a 20 20 2f 2a 20 53 69 6e 63 65 20 6c 65 61 76  .  /* Since leav
2ff9f 65 73 52 65 61 64 65 72 52 65 6f 72 64 65 72 28  esReaderReorder(
2ffa0 29 20 70 75 73 68 65 73 20 72 65 61 64 65 72 73  ) pushes readers
2ffa1 20 61 74 20 65 6f 66 20 74 6f 20 74 68 65 20 65   at eof to the e
2ffa2 6e 64 2c 0a 20 20 2a 2a 20 77 68 65 6e 20 74 68  nd,.  ** when th
2ffa3 65 20 66 69 72 73 74 20 72 65 61 64 65 72 20 69  e first reader i
2ffa4 73 20 65 6d 70 74 79 2c 20 61 6c 6c 20 77 69 6c  s empty, all wil
2ffa5 6c 20 62 65 20 65 6d 70 74 79 2e 0a 20 20 2a 2f  l be empty..  */
2ffa6 0a 20 20 77 68 69 6c 65 28 20 21 6c 65 61 76 65  .  while( !leave
2ffa7 73 52 65 61 64 65 72 41 74 45 6e 64 28 6c 72 73  sReaderAtEnd(lrs
2ffa8 29 20 29 7b 0a 20 20 20 20 2f 2a 20 46 69 67 75  ) ){.    /* Figu
2ffa9 72 65 20 6f 75 74 20 68 6f 77 20 6d 61 6e 79 20  re out how many 
2ffaa 72 65 61 64 65 72 73 20 73 68 61 72 65 20 74 68  readers share th
2ffab 65 69 72 20 6e 65 78 74 20 74 65 72 6d 2e 20 2a  eir next term. *
2ffac 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b 20 69  /.    for(i=1; i
2ffad 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 20 26 26 20  <MERGE_COUNT && 
2ffae 21 6c 65 61 76 65 73 52 65 61 64 65 72 41 74 45  !leavesReaderAtE
2ffaf 6e 64 28 6c 72 73 2b 69 29 3b 20 69 2b 2b 29 7b  nd(lrs+i); i++){
2ffb0 0a 20 20 20 20 20 20 69 66 28 20 30 21 3d 6c 65  .      if( 0!=le
2ffb1 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d  avesReaderTermCm
2ffb2 70 28 6c 72 73 2c 20 6c 72 73 2b 69 29 20 29 20  p(lrs, lrs+i) ) 
2ffb3 62 72 65 61 6b 3b 0a 20 20 20 20 7d 0a 0a 20 20  break;.    }..  
2ffb4 20 20 72 63 20 3d 20 6c 65 61 76 65 73 52 65 61    rc = leavesRea
2ffb5 64 65 72 73 4d 65 72 67 65 28 76 2c 20 6c 72 73  dersMerge(v, lrs
2ffb6 2c 20 69 2c 20 26 77 72 69 74 65 72 29 3b 0a 20  , i, &writer);. 
2ffb7 20 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54     if( rc!=SQLIT
2ffb8 45 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b  E_OK ) goto err;
2ffb9 0a 0a 20 20 20 20 2f 2a 20 53 74 65 70 20 66 6f  ..    /* Step fo
2ffba 72 77 61 72 64 20 74 68 6f 73 65 20 74 68 61 74  rward those that
2ffbb 20 77 65 72 65 20 6d 65 72 67 65 64 2e 20 2a 2f   were merged. */
2ffbc 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d 3e  .    while( i-->
2ffbd 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  0 ){.      rc = 
2ffbe 6c 65 61 76 65 73 52 65 61 64 65 72 53 74 65 70  leavesReaderStep
2ffbf 28 76 2c 20 6c 72 73 2b 69 29 3b 0a 20 20 20 20  (v, lrs+i);.    
2ffc0 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
2ffc1 5f 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a  _OK ) goto err;.
2ffc2 0a 20 20 20 20 20 20 2f 2a 20 52 65 6f 72 64 65  .      /* Reorde
2ffc3 72 20 62 79 20 74 65 72 6d 2c 20 74 68 65 6e 20  r by term, then 
2ffc4 62 79 20 61 67 65 2e 20 2a 2f 0a 20 20 20 20 20  by age. */.     
2ffc5 20 6c 65 61 76 65 73 52 65 61 64 65 72 52 65 6f   leavesReaderReo
2ffc6 72 64 65 72 28 6c 72 73 2b 69 2c 20 4d 45 52 47  rder(lrs+i, MERG
2ffc7 45 5f 43 4f 55 4e 54 2d 69 29 3b 0a 20 20 20 20  E_COUNT-i);.    
2ffc8 7d 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d 30  }.  }..  for(i=0
2ffc9 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b  ; i<MERGE_COUNT;
2ffca 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65   i++){.    leave
2ffcb 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
2ffcc 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 0a 20 20  lrs[i]);.  }..  
2ffcd 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 46  rc = leafWriterF
2ffce 69 6e 61 6c 69 7a 65 28 76 2c 20 26 77 72 69 74  inalize(v, &writ
2ffcf 65 72 29 3b 0a 20 20 6c 65 61 66 57 72 69 74 65  er);.  leafWrite
2ffd0 72 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  rDestroy(&writer
2ffd1 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
2ffd2 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
2ffd3 72 63 3b 0a 0a 20 20 2f 2a 20 44 65 6c 65 74 65  rc;..  /* Delete
2ffd4 20 74 68 65 20 6d 65 72 67 65 64 20 73 65 67 6d   the merged segm
2ffd5 65 6e 74 20 64 61 74 61 2e 20 2a 2f 0a 20 20 72  ent data. */.  r
2ffd6 65 74 75 72 6e 20 73 65 67 64 69 72 5f 64 65 6c  eturn segdir_del
2ffd7 65 74 65 28 76 2c 20 69 4c 65 76 65 6c 29 3b 0a  ete(v, iLevel);.
2ffd8 0a 20 65 72 72 3a 0a 20 20 66 6f 72 28 69 3d 30  . err:.  for(i=0
2ffd9 3b 20 69 3c 4d 45 52 47 45 5f 43 4f 55 4e 54 3b  ; i<MERGE_COUNT;
2ffda 20 69 2b 2b 29 7b 0a 20 20 20 20 6c 65 61 76 65   i++){.    leave
2ffdb 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  sReaderDestroy(&
2ffdc 6c 72 73 5b 69 5d 29 3b 0a 20 20 7d 0a 20 20 6c  lrs[i]);.  }.  l
2ffdd 65 61 66 57 72 69 74 65 72 44 65 73 74 72 6f 79  eafWriterDestroy
2ffde 28 26 77 72 69 74 65 72 29 3b 0a 20 20 72 65 74  (&writer);.  ret
2ffdf 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 41 63  urn rc;.}../* Ac
2ffe0 63 75 6d 75 6c 61 74 65 20 74 68 65 20 75 6e 69  cumulate the uni
2ffe1 6f 6e 20 6f 66 20 2a 61 63 63 20 61 6e 64 20 2a  on of *acc and *
2ffe2 70 44 61 74 61 20 69 6e 74 6f 20 2a 61 63 63 2e  pData into *acc.
2ffe3 20 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20   */.static void 
2ffe4 64 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74  docListAccumulat
2ffe5 65 55 6e 69 6f 6e 28 44 61 74 61 42 75 66 66 65  eUnion(DataBuffe
2ffe6 72 20 2a 61 63 63 2c 0a 20 20 20 20 20 20 20 20  r *acc,.        
2ffe7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
2ffe8 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
2ffe9 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
2ffea 74 20 6e 44 61 74 61 29 20 7b 0a 20 20 44 61 74  t nData) {.  Dat
2ffeb 61 42 75 66 66 65 72 20 74 6d 70 20 3d 20 2a 61  aBuffer tmp = *a
2ffec 63 63 3b 0a 20 20 64 61 74 61 42 75 66 66 65 72  cc;.  dataBuffer
2ffed 49 6e 69 74 28 61 63 63 2c 20 74 6d 70 2e 6e 44  Init(acc, tmp.nD
2ffee 61 74 61 2b 6e 44 61 74 61 29 3b 0a 20 20 64 6f  ata+nData);.  do
2ffef 63 4c 69 73 74 55 6e 69 6f 6e 28 74 6d 70 2e 70  cListUnion(tmp.p
2fff0 44 61 74 61 2c 20 74 6d 70 2e 6e 44 61 74 61 2c  Data, tmp.nData,
2fff1 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20 61   pData, nData, a
2fff2 63 63 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  cc);.  dataBuffe
2fff3 72 44 65 73 74 72 6f 79 28 26 74 6d 70 29 3b 0a  rDestroy(&tmp);.
2fff4 7d 0a 0a 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73  }../* TODO(shess
2fff5 29 20 49 74 20 6d 69 67 68 74 20 62 65 20 69 6e  ) It might be in
2fff6 74 65 72 65 73 74 69 6e 67 20 74 6f 20 65 78 70  teresting to exp
2fff7 6c 6f 72 65 20 64 69 66 66 65 72 65 6e 74 20 6d  lore different m
2fff8 65 72 67 65 0a 2a 2a 20 73 74 72 61 74 65 67 69  erge.** strategi
2fff9 65 73 2c 20 68 65 72 65 2e 20 20 46 6f 72 20 69  es, here.  For i
2fffa 6e 73 74 61 6e 63 65 2c 20 73 69 6e 63 65 20 74  nstance, since t
2fffb 68 69 73 20 69 73 20 61 20 73 6f 72 74 65 64 20  his is a sorted 
2fffc 6d 65 72 67 65 2c 20 77 65 0a 2a 2a 20 63 6f 75  merge, we.** cou
2fffd 6c 64 20 65 61 73 69 6c 79 20 6d 65 72 67 65 20  ld easily merge 
2fffe 6d 61 6e 79 20 64 6f 63 6c 69 73 74 73 20 69 6e  many doclists in
2ffff 20 70 61 72 61 6c 6c 65 6c 2e 20 20 57 69 74 68   parallel.  With
30000 20 73 6f 6d 65 0a 2a 2a 20 63 6f 6d 70 72 65 68   some.** compreh
30001 65 6e 73 69 6f 6e 20 6f 66 20 74 68 65 20 73 74  ension of the st
30002 6f 72 61 67 65 20 66 6f 72 6d 61 74 2c 20 77 65  orage format, we
30003 20 63 6f 75 6c 64 20 6d 65 72 67 65 20 61 6c 6c   could merge all
30004 20 6f 66 20 74 68 65 0a 2a 2a 20 64 6f 63 6c 69   of the.** docli
30005 73 74 73 20 77 69 74 68 69 6e 20 61 20 6c 65 61  sts within a lea
30006 66 20 6e 6f 64 65 20 64 69 72 65 63 74 6c 79 20  f node directly 
30007 66 72 6f 6d 20 74 68 65 20 6c 65 61 66 20 6e 6f  from the leaf no
30008 64 65 27 73 20 73 74 6f 72 61 67 65 2e 0a 2a 2a  de's storage..**
30009 20 49 74 20 6d 61 79 20 62 65 20 77 6f 72 74 68   It may be worth
3000a 77 68 69 6c 65 20 74 6f 20 6d 65 72 67 65 20 73  while to merge s
3000b 6d 61 6c 6c 65 72 20 64 6f 63 6c 69 73 74 73 20  maller doclists 
3000c 62 65 66 6f 72 65 20 6c 61 72 67 65 72 0a 2a 2a  before larger.**
3000d 20 64 6f 63 6c 69 73 74 73 2c 20 73 69 6e 63 65   doclists, since
3000e 20 74 68 65 79 20 63 61 6e 20 62 65 20 74 72 61   they can be tra
3000f 76 65 72 73 65 64 20 6d 6f 72 65 20 71 75 69 63  versed more quic
30010 6b 6c 79 20 2d 20 62 75 74 20 74 68 65 0a 2a 2a  kly - but the.**
30011 20 72 65 73 75 6c 74 73 20 6d 61 79 20 68 61 76   results may hav
30012 65 20 6c 65 73 73 20 6f 76 65 72 6c 61 70 2c 20  e less overlap, 
30013 6d 61 6b 69 6e 67 20 74 68 65 6d 20 6d 6f 72 65  making them more
30014 20 65 78 70 65 6e 73 69 76 65 20 69 6e 20 61 0a   expensive in a.
30015 2a 2a 20 64 69 66 66 65 72 65 6e 74 20 77 61 79  ** different way
30016 2e 0a 2a 2f 0a 0a 2f 2a 20 53 63 61 6e 20 70 52  ..*/../* Scan pR
30017 65 61 64 65 72 20 66 6f 72 20 70 54 65 72 6d 2f  eader for pTerm/
30018 6e 54 65 72 6d 2c 20 61 6e 64 20 6d 65 72 67 65  nTerm, and merge
30019 20 74 68 65 20 74 65 72 6d 27 73 20 64 6f 63 6c   the term's docl
3001a 69 73 74 20 6f 76 65 72 0a 2a 2a 20 2a 6f 75 74  ist over.** *out
3001b 20 28 61 6e 79 20 64 6f 63 6c 69 73 74 73 20 77   (any doclists w
3001c 69 74 68 20 64 75 70 6c 69 63 61 74 65 20 64 6f  ith duplicate do
3001d 63 69 64 73 20 6f 76 65 72 77 72 69 74 65 20 74  cids overwrite t
3001e 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29 2e 0a 2a  hose in *out)..*
3001f 2a 20 49 6e 74 65 72 6e 61 6c 20 66 75 6e 63 74  * Internal funct
30020 69 6f 6e 20 66 6f 72 20 6c 6f 61 64 53 65 67 6d  ion for loadSegm
30021 65 6e 74 4c 65 61 66 28 29 2e 0a 2a 2f 0a 73 74  entLeaf()..*/.st
30022 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65 67  atic int loadSeg
30023 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 66 75  mentLeavesInt(fu
30024 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
30025 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
30026 65 61 64 65 72 2c 0a 20 20 20 20 20 20 20 20 20  eader,.         
30027 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30028 20 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61         const cha
30029 72 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54  r *pTerm, int nT
3002a 65 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69  erm, int isPrefi
3002b 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  x,.             
3002c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3002d 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a 6f     DataBuffer *o
3002e 75 74 29 7b 0a 20 20 2f 2a 20 64 6f 63 6c 69 73  ut){.  /* doclis
3002f 74 20 64 61 74 61 20 69 73 20 61 63 63 75 6d 75  t data is accumu
30030 6c 61 74 65 64 20 69 6e 74 6f 20 70 42 75 66 66  lated into pBuff
30031 65 72 73 20 73 69 6d 69 6c 61 72 20 74 6f 20 68  ers similar to h
30032 6f 77 20 6f 6e 65 20 64 6f 65 73 0a 20 20 2a 2a  ow one does.  **
30033 20 69 6e 63 72 65 6d 65 6e 74 20 69 6e 20 62 69   increment in bi
30034 6e 61 72 79 20 61 72 69 74 68 6d 65 74 69 63 2e  nary arithmetic.
30035 20 20 49 66 20 69 6e 64 65 78 20 30 20 69 73 20    If index 0 is 
30036 65 6d 70 74 79 2c 20 74 68 65 20 64 61 74 61 20  empty, the data 
30037 69 73 0a 20 20 2a 2a 20 73 74 6f 72 65 64 20 74  is.  ** stored t
30038 68 65 72 65 2e 20 20 49 66 20 74 68 65 72 65 20  here.  If there 
30039 69 73 20 64 61 74 61 20 74 68 65 72 65 2c 20 69  is data there, i
3003a 74 20 69 73 20 6d 65 72 67 65 64 20 61 6e 64 20  t is merged and 
3003b 74 68 65 0a 20 20 2a 2a 20 72 65 73 75 6c 74 73  the.  ** results
3003c 20 63 61 72 72 69 65 64 20 69 6e 74 6f 20 70 6f   carried into po
3003d 73 69 74 69 6f 6e 20 31 2c 20 77 69 74 68 20 66  sition 1, with f
3003e 75 72 74 68 65 72 20 6d 65 72 67 65 2d 61 6e 64  urther merge-and
3003f 2d 63 61 72 72 79 0a 20 20 2a 2a 20 75 6e 74 69  -carry.  ** unti
30040 6c 20 61 6e 20 65 6d 70 74 79 20 70 6f 73 69 74  l an empty posit
30041 69 6f 6e 20 69 73 20 66 6f 75 6e 64 2e 0a 20 20  ion is found..  
30042 2a 2f 0a 20 20 44 61 74 61 42 75 66 66 65 72 20  */.  DataBuffer 
30043 2a 70 42 75 66 66 65 72 73 20 3d 20 4e 55 4c 4c  *pBuffers = NULL
30044 3b 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72 73  ;.  int nBuffers
30045 20 3d 20 30 2c 20 6e 4d 61 78 42 75 66 66 65 72   = 0, nMaxBuffer
30046 73 20 3d 20 30 2c 20 72 63 3b 0a 0a 20 20 61 73  s = 0, rc;..  as
30047 73 65 72 74 28 20 6e 54 65 72 6d 3e 30 20 29 3b  sert( nTerm>0 );
30048 0a 0a 20 20 66 6f 72 28 72 63 3d 53 51 4c 49 54  ..  for(rc=SQLIT
30049 45 5f 4f 4b 3b 20 72 63 3d 3d 53 51 4c 49 54 45  E_OK; rc==SQLITE
3004a 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73 52 65  _OK && !leavesRe
3004b 61 64 65 72 41 74 45 6e 64 28 70 52 65 61 64 65  aderAtEnd(pReade
3004c 72 29 3b 0a 20 20 20 20 20 20 72 63 3d 6c 65 61  r);.      rc=lea
3004d 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c  vesReaderStep(v,
3004e 20 70 52 65 61 64 65 72 29 29 7b 0a 20 20 20 20   pReader)){.    
3004f 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 52  /* TODO(shess) R
30050 65 61 6c 6c 79 20 77 61 6e 74 20 6c 65 61 76 65  eally want leave
30051 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 29  sReaderTermCmp()
30052 2c 20 62 75 74 20 74 68 61 74 20 6e 61 6d 65 20  , but that name 
30053 69 73 0a 20 20 20 20 2a 2a 20 61 6c 72 65 61 64  is.    ** alread
30054 79 20 74 61 6b 65 6e 20 74 6f 20 63 6f 6d 70 61  y taken to compa
30055 72 65 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20  re the terms of 
30056 74 77 6f 20 4c 65 61 76 65 73 52 65 61 64 65 72  two LeavesReader
30057 73 2e 20 20 54 68 69 6e 6b 0a 20 20 20 20 2a 2a  s.  Think.    **
30058 20 6f 6e 20 61 20 62 65 74 74 65 72 20 6e 61 6d   on a better nam
30059 65 2e 20 20 5b 4d 65 61 6e 77 68 69 6c 65 2c 20  e.  [Meanwhile, 
3005a 62 72 65 61 6b 20 65 6e 63 61 70 73 75 6c 61 74  break encapsulat
3005b 69 6f 6e 20 72 61 74 68 65 72 20 74 68 61 6e 0a  ion rather than.
3005c 20 20 20 20 2a 2a 20 75 73 65 20 61 20 63 6f 6e      ** use a con
3005d 66 75 73 69 6e 67 20 6e 61 6d 65 2e 5d 0a 20 20  fusing name.].  
3005e 20 20 2a 2f 0a 20 20 20 20 69 6e 74 20 63 20 3d    */.    int c =
3005f 20 6c 65 61 66 52 65 61 64 65 72 54 65 72 6d 43   leafReaderTermC
30060 6d 70 28 26 70 52 65 61 64 65 72 2d 3e 6c 65 61  mp(&pReader->lea
30061 66 52 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20  fReader, pTerm, 
30062 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29  nTerm, isPrefix)
30063 3b 0a 20 20 20 20 69 66 28 20 63 3e 30 20 29 20  ;.    if( c>0 ) 
30064 62 72 65 61 6b 3b 20 20 20 20 20 20 2f 2a 20 50  break;      /* P
30065 61 73 74 20 61 6e 79 20 70 6f 73 73 69 62 6c 65  ast any possible
30066 20 6d 61 74 63 68 65 73 2e 20 2a 2f 0a 20 20 20   matches. */.   
30067 20 69 66 28 20 63 3d 3d 30 20 29 7b 0a 20 20 20   if( c==0 ){.   
30068 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70     const char *p
30069 44 61 74 61 20 3d 20 6c 65 61 76 65 73 52 65 61  Data = leavesRea
3006a 64 65 72 44 61 74 61 28 70 52 65 61 64 65 72 29  derData(pReader)
3006b 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 42 75 66  ;.      int iBuf
3006c 66 65 72 2c 20 6e 44 61 74 61 20 3d 20 6c 65 61  fer, nData = lea
3006d 76 65 73 52 65 61 64 65 72 44 61 74 61 42 79 74  vesReaderDataByt
3006e 65 73 28 70 52 65 61 64 65 72 29 3b 0a 0a 20 20  es(pReader);..  
3006f 20 20 20 20 2f 2a 20 46 69 6e 64 20 74 68 65 20      /* Find the 
30070 66 69 72 73 74 20 65 6d 70 74 79 20 62 75 66 66  first empty buff
30071 65 72 2e 20 2a 2f 0a 20 20 20 20 20 20 66 6f 72  er. */.      for
30072 28 69 42 75 66 66 65 72 3d 30 3b 20 69 42 75 66  (iBuffer=0; iBuf
30073 66 65 72 3c 6e 42 75 66 66 65 72 73 3b 20 2b 2b  fer<nBuffers; ++
30074 69 42 75 66 66 65 72 29 7b 0a 20 20 20 20 20 20  iBuffer){.      
30075 20 20 69 66 28 20 30 3d 3d 70 42 75 66 66 65 72    if( 0==pBuffer
30076 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61 74 61  s[iBuffer].nData
30077 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20   ) break;.      
30078 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 4f 75 74 20  }..      /* Out 
30079 6f 66 20 62 75 66 66 65 72 73 2c 20 61 64 64 20  of buffers, add 
3007a 61 6e 20 65 6d 70 74 79 20 6f 6e 65 2e 20 2a 2f  an empty one. */
3007b 0a 20 20 20 20 20 20 69 66 28 20 69 42 75 66 66  .      if( iBuff
3007c 65 72 3d 3d 6e 42 75 66 66 65 72 73 20 29 7b 0a  er==nBuffers ){.
3007d 20 20 20 20 20 20 20 20 69 66 28 20 6e 42 75 66          if( nBuf
3007e 66 65 72 73 3d 3d 6e 4d 61 78 42 75 66 66 65 72  fers==nMaxBuffer
3007f 73 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 44  s ){.          D
30080 61 74 61 42 75 66 66 65 72 20 2a 70 3b 0a 20 20  ataBuffer *p;.  
30081 20 20 20 20 20 20 20 20 6e 4d 61 78 42 75 66 66          nMaxBuff
30082 65 72 73 20 2b 3d 20 32 30 3b 0a 0a 20 20 20 20  ers += 20;..    
30083 20 20 20 20 20 20 2f 2a 20 4d 61 6e 75 61 6c 20        /* Manual 
30084 72 65 61 6c 6c 6f 63 20 73 6f 20 77 65 20 63 61  realloc so we ca
30085 6e 20 68 61 6e 64 6c 65 20 4e 55 4c 4c 20 61 70  n handle NULL ap
30086 70 72 6f 70 72 69 61 74 65 6c 79 2e 20 2a 2f 0a  propriately. */.
30087 20 20 20 20 20 20 20 20 20 20 70 20 3d 20 73 71            p = sq
30088 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 4d 61  lite3_malloc(nMa
30089 78 42 75 66 66 65 72 73 2a 73 69 7a 65 6f 66 28  xBuffers*sizeof(
3008a 2a 70 42 75 66 66 65 72 73 29 29 3b 0a 20 20 20  *pBuffers));.   
3008b 20 20 20 20 20 20 20 69 66 28 20 70 3d 3d 4e 55         if( p==NU
3008c 4c 4c 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  LL ){.          
3008d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f    rc = SQLITE_NO
3008e 4d 45 4d 3b 0a 20 20 20 20 20 20 20 20 20 20 20  MEM;.           
3008f 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20   break;.        
30090 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 20 20 69    }..          i
30091 66 28 20 6e 42 75 66 66 65 72 73 3e 30 20 29 7b  f( nBuffers>0 ){
30092 0a 20 20 20 20 20 20 20 20 20 20 20 20 61 73 73  .            ass
30093 65 72 74 28 70 42 75 66 66 65 72 73 21 3d 4e 55  ert(pBuffers!=NU
30094 4c 4c 29 3b 0a 20 20 20 20 20 20 20 20 20 20 20  LL);.           
30095 20 6d 65 6d 63 70 79 28 70 2c 20 70 42 75 66 66   memcpy(p, pBuff
30096 65 72 73 2c 20 6e 42 75 66 66 65 72 73 2a 73 69  ers, nBuffers*si
30097 7a 65 6f 66 28 2a 70 42 75 66 66 65 72 73 29 29  zeof(*pBuffers))
30098 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 73 71  ;.            sq
30099 6c 69 74 65 33 5f 66 72 65 65 28 70 42 75 66 66  lite3_free(pBuff
3009a 65 72 73 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ers);.          
3009b 7d 0a 20 20 20 20 20 20 20 20 20 20 70 42 75 66  }.          pBuf
3009c 66 65 72 73 20 3d 20 70 3b 0a 20 20 20 20 20 20  fers = p;.      
3009d 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74 61    }.        data
3009e 42 75 66 66 65 72 49 6e 69 74 28 26 28 70 42 75  BufferInit(&(pBu
3009f 66 66 65 72 73 5b 6e 42 75 66 66 65 72 73 5d 29  ffers[nBuffers])
300a0 2c 20 30 29 3b 0a 20 20 20 20 20 20 20 20 6e 42  , 0);.        nB
300a1 75 66 66 65 72 73 2b 2b 3b 0a 20 20 20 20 20 20  uffers++;.      
300a2 7d 0a 0a 20 20 20 20 20 20 2f 2a 20 41 74 20 74  }..      /* At t
300a3 68 69 73 20 70 6f 69 6e 74 2c 20 6d 75 73 74 20  his point, must 
300a4 68 61 76 65 20 61 6e 20 65 6d 70 74 79 20 61 74  have an empty at
300a5 20 69 42 75 66 66 65 72 2e 20 2a 2f 0a 20 20 20   iBuffer. */.   
300a6 20 20 20 61 73 73 65 72 74 28 69 42 75 66 66 65     assert(iBuffe
300a7 72 3c 6e 42 75 66 66 65 72 73 20 26 26 20 70 42  r<nBuffers && pB
300a8 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e  uffers[iBuffer].
300a9 6e 44 61 74 61 3d 3d 30 29 3b 0a 0a 20 20 20 20  nData==0);..    
300aa 20 20 2f 2a 20 49 66 20 65 6d 70 74 79 20 77 61    /* If empty wa
300ab 73 20 66 69 72 73 74 20 62 75 66 66 65 72 2c 20  s first buffer, 
300ac 6e 6f 20 6e 65 65 64 20 66 6f 72 20 6d 65 72 67  no need for merg
300ad 65 20 6c 6f 67 69 63 2e 20 2a 2f 0a 20 20 20 20  e logic. */.    
300ae 20 20 69 66 28 20 69 42 75 66 66 65 72 3d 3d 30    if( iBuffer==0
300af 20 29 7b 0a 20 20 20 20 20 20 20 20 64 61 74 61   ){.        data
300b0 42 75 66 66 65 72 52 65 70 6c 61 63 65 28 26 28  BufferReplace(&(
300b1 70 42 75 66 66 65 72 73 5b 30 5d 29 2c 20 70 44  pBuffers[0]), pD
300b2 61 74 61 2c 20 6e 44 61 74 61 29 3b 0a 20 20 20  ata, nData);.   
300b3 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
300b4 20 20 2f 2a 20 70 41 63 63 20 69 73 20 74 68 65    /* pAcc is the
300b5 20 65 6d 70 74 79 20 62 75 66 66 65 72 20 74 68   empty buffer th
300b6 65 20 6d 65 72 67 65 64 20 64 61 74 61 20 77 69  e merged data wi
300b7 6c 6c 20 65 6e 64 20 75 70 20 69 6e 2e 20 2a 2f  ll end up in. */
300b8 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66  .        DataBuf
300b9 66 65 72 20 2a 70 41 63 63 20 3d 20 26 28 70 42  fer *pAcc = &(pB
300ba 75 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 29  uffers[iBuffer])
300bb 3b 0a 20 20 20 20 20 20 20 20 44 61 74 61 42 75  ;.        DataBu
300bc 66 66 65 72 20 2a 70 20 3d 20 26 28 70 42 75 66  ffer *p = &(pBuf
300bd 66 65 72 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20  fers[0]);..     
300be 20 20 20 2f 2a 20 48 61 6e 64 6c 65 20 70 6f 73     /* Handle pos
300bf 69 74 69 6f 6e 20 30 20 73 70 65 63 69 61 6c 6c  ition 0 speciall
300c0 79 20 74 6f 20 61 76 6f 69 64 20 6e 65 65 64 20  y to avoid need 
300c1 74 6f 20 70 72 69 6d 65 20 70 41 63 63 0a 20 20  to prime pAcc.  
300c2 20 20 20 20 20 20 2a 2a 20 77 69 74 68 20 70 44        ** with pD
300c3 61 74 61 2f 6e 44 61 74 61 2e 0a 20 20 20 20 20  ata/nData..     
300c4 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 61     */.        da
300c5 74 61 42 75 66 66 65 72 53 77 61 70 28 70 2c 20  taBufferSwap(p, 
300c6 70 41 63 63 29 3b 0a 20 20 20 20 20 20 20 20 64  pAcc);.        d
300c7 6f 63 4c 69 73 74 41 63 63 75 6d 75 6c 61 74 65  ocListAccumulate
300c8 55 6e 69 6f 6e 28 70 41 63 63 2c 20 70 44 61 74  Union(pAcc, pDat
300c9 61 2c 20 6e 44 61 74 61 29 3b 0a 0a 20 20 20 20  a, nData);..    
300ca 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
300cb 65 20 72 65 6d 61 69 6e 69 6e 67 20 64 6f 63 6c  e remaining docl
300cc 69 73 74 73 20 69 6e 74 6f 20 70 41 63 63 2e 20  ists into pAcc. 
300cd 2a 2f 0a 20 20 20 20 20 20 20 20 66 6f 72 28 2b  */.        for(+
300ce 2b 70 3b 20 70 3c 70 41 63 63 3b 20 2b 2b 70 29  +p; p<pAcc; ++p)
300cf 7b 0a 20 20 20 20 20 20 20 20 20 20 64 6f 63 4c  {.          docL
300d0 69 73 74 41 63 63 75 6d 75 6c 61 74 65 55 6e 69  istAccumulateUni
300d1 6f 6e 28 70 41 63 63 2c 20 70 2d 3e 70 44 61 74  on(pAcc, p->pDat
300d2 61 2c 20 70 2d 3e 6e 44 61 74 61 29 3b 0a 0a 20  a, p->nData);.. 
300d3 20 20 20 20 20 20 20 20 20 2f 2a 20 64 61 74 61           /* data
300d4 42 75 66 66 65 72 52 65 73 65 74 28 29 20 63 6f  BufferReset() co
300d5 75 6c 64 20 61 6c 6c 6f 77 20 61 20 6c 61 72 67  uld allow a larg
300d6 65 20 64 6f 63 6c 69 73 74 20 74 6f 20 62 6c 6f  e doclist to blo
300d7 77 20 75 70 0a 20 20 20 20 20 20 20 20 20 20 2a  w up.          *
300d8 2a 20 6f 75 72 20 6d 65 6d 6f 72 79 20 72 65 71  * our memory req
300d9 75 69 72 65 6d 65 6e 74 73 2e 0a 20 20 20 20 20  uirements..     
300da 20 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20       */.        
300db 20 20 69 66 28 20 70 2d 3e 6e 43 61 70 61 63 69    if( p->nCapaci
300dc 74 79 3c 31 30 32 34 20 29 7b 0a 20 20 20 20 20  ty<1024 ){.     
300dd 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65         dataBuffe
300de 72 52 65 73 65 74 28 70 29 3b 0a 20 20 20 20 20  rReset(p);.     
300df 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
300e0 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
300e1 65 72 44 65 73 74 72 6f 79 28 70 29 3b 0a 20 20  erDestroy(p);.  
300e2 20 20 20 20 20 20 20 20 20 20 64 61 74 61 42 75            dataBu
300e3 66 66 65 72 49 6e 69 74 28 70 2c 20 30 29 3b 0a  fferInit(p, 0);.
300e4 20 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20            }.    
300e5 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
300e6 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 55 6e    }.  }..  /* Un
300e7 69 6f 6e 20 61 6c 6c 20 74 68 65 20 64 6f 63 6c  ion all the docl
300e8 69 73 74 73 20 74 6f 67 65 74 68 65 72 20 69 6e  ists together in
300e9 74 6f 20 2a 6f 75 74 2e 20 2a 2f 0a 20 20 2f 2a  to *out. */.  /*
300ea 20 54 4f 44 4f 28 73 68 65 73 73 29 20 57 68 61   TODO(shess) Wha
300eb 74 20 69 66 20 2a 6f 75 74 20 69 73 20 62 69 67  t if *out is big
300ec 3f 20 20 53 69 67 68 2e 20 2a 2f 0a 20 20 69 66  ?  Sigh. */.  if
300ed 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
300ee 26 26 20 6e 42 75 66 66 65 72 73 3e 30 20 29 7b  && nBuffers>0 ){
300ef 0a 20 20 20 20 69 6e 74 20 69 42 75 66 66 65 72  .    int iBuffer
300f0 3b 0a 20 20 20 20 66 6f 72 28 69 42 75 66 66 65  ;.    for(iBuffe
300f1 72 3d 30 3b 20 69 42 75 66 66 65 72 3c 6e 42 75  r=0; iBuffer<nBu
300f2 66 66 65 72 73 3b 20 2b 2b 69 42 75 66 66 65 72  ffers; ++iBuffer
300f3 29 7b 0a 20 20 20 20 20 20 69 66 28 20 70 42 75  ){.      if( pBu
300f4 66 66 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e  ffers[iBuffer].n
300f5 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20  Data>0 ){.      
300f6 20 20 69 66 28 20 6f 75 74 2d 3e 6e 44 61 74 61    if( out->nData
300f7 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20  ==0 ){.         
300f8 20 64 61 74 61 42 75 66 66 65 72 53 77 61 70 28   dataBufferSwap(
300f9 6f 75 74 2c 20 26 28 70 42 75 66 66 65 72 73 5b  out, &(pBuffers[
300fa 69 42 75 66 66 65 72 5d 29 29 3b 0a 20 20 20 20  iBuffer]));.    
300fb 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20      }else{.     
300fc 20 20 20 20 20 64 6f 63 4c 69 73 74 41 63 63 75       docListAccu
300fd 6d 75 6c 61 74 65 55 6e 69 6f 6e 28 6f 75 74 2c  mulateUnion(out,
300fe 20 70 42 75 66 66 65 72 73 5b 69 42 75 66 66 65   pBuffers[iBuffe
300ff 72 5d 2e 70 44 61 74 61 2c 0a 20 20 20 20 20 20  r].pData,.      
30100 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30101 20 20 20 20 20 20 20 20 20 20 20 70 42 75 66 66             pBuff
30102 65 72 73 5b 69 42 75 66 66 65 72 5d 2e 6e 44 61  ers[iBuffer].nDa
30103 74 61 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20  ta);.        }. 
30104 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d       }.    }.  }
30105 0a 0a 20 20 77 68 69 6c 65 28 20 6e 42 75 66 66  ..  while( nBuff
30106 65 72 73 2d 2d 20 29 7b 0a 20 20 20 20 64 61 74  ers-- ){.    dat
30107 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 26  aBufferDestroy(&
30108 28 70 42 75 66 66 65 72 73 5b 6e 42 75 66 66 65  (pBuffers[nBuffe
30109 72 73 5d 29 29 3b 0a 20 20 7d 0a 20 20 69 66 28  rs]));.  }.  if(
3010a 20 70 42 75 66 66 65 72 73 21 3d 4e 55 4c 4c 20   pBuffers!=NULL 
3010b 29 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70  ) sqlite3_free(p
3010c 42 75 66 66 65 72 73 29 3b 0a 0a 20 20 72 65 74  Buffers);..  ret
3010d 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61  urn rc;.}../* Ca
3010e 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65  ll loadSegmentLe
3010f 61 76 65 73 49 6e 74 28 29 20 77 69 74 68 20 70  avesInt() with p
30110 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20 69 6e  Data/nData as in
30111 70 75 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69  put. */.static i
30112 6e 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65  nt loadSegmentLe
30113 61 66 28 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  af(fulltext_vtab
30114 20 2a 76 2c 20 63 6f 6e 73 74 20 63 68 61 72 20   *v, const char 
30115 2a 70 44 61 74 61 2c 20 69 6e 74 20 6e 44 61 74  *pData, int nDat
30116 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  a,.             
30117 20 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f                co
30118 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d 2c  nst char *pTerm,
30119 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20   int nTerm, int 
3011a 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20  isPrefix,.      
3011b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3011c 20 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20       DataBuffer 
3011d 2a 6f 75 74 29 7b 0a 20 20 4c 65 61 76 65 73 52  *out){.  LeavesR
3011e 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a 20 20  eader reader;.  
3011f 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65 72  int rc;..  asser
30120 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a 20 20  t( nData>1 );.  
30121 61 73 73 65 72 74 28 20 2a 70 44 61 74 61 3d 3d  assert( *pData==
30122 27 5c 30 27 20 29 3b 0a 20 20 72 63 20 3d 20 6c  '\0' );.  rc = l
30123 65 61 76 65 73 52 65 61 64 65 72 49 6e 69 74 28  eavesReaderInit(
30124 76 2c 20 30 2c 20 30 2c 20 30 2c 20 70 44 61 74  v, 0, 0, 0, pDat
30125 61 2c 20 6e 44 61 74 61 2c 20 26 72 65 61 64 65  a, nData, &reade
30126 72 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51  r);.  if( rc!=SQ
30127 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e  LITE_OK ) return
30128 20 72 63 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61   rc;..  rc = loa
30129 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e  dSegmentLeavesIn
3012a 74 28 76 2c 20 26 72 65 61 64 65 72 2c 20 70 54  t(v, &reader, pT
3012b 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72  erm, nTerm, isPr
3012c 65 66 69 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65  efix, out);.  le
3012d 61 76 65 73 52 65 61 64 65 72 52 65 73 65 74 28  avesReaderReset(
3012e 26 72 65 61 64 65 72 29 3b 0a 20 20 6c 65 61 76  &reader);.  leav
3012f 65 73 52 65 61 64 65 72 44 65 73 74 72 6f 79 28  esReaderDestroy(
30130 26 72 65 61 64 65 72 29 3b 0a 20 20 72 65 74 75  &reader);.  retu
30131 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20 43 61 6c  rn rc;.}../* Cal
30132 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74 4c 65 61  l loadSegmentLea
30133 76 65 73 49 6e 74 28 29 20 77 69 74 68 20 74 68  vesInt() with th
30134 65 20 6c 65 61 66 20 6e 6f 64 65 73 20 66 72 6f  e leaf nodes fro
30135 6d 20 69 53 74 61 72 74 4c 65 61 66 20 74 6f 0a  m iStartLeaf to.
30136 2a 2a 20 69 45 6e 64 4c 65 61 66 20 28 69 6e 63  ** iEndLeaf (inc
30137 6c 75 73 69 76 65 29 20 61 73 20 69 6e 70 75 74  lusive) as input
30138 2c 20 61 6e 64 20 6d 65 72 67 65 20 74 68 65 20  , and merge the 
30139 72 65 73 75 6c 74 69 6e 67 20 64 6f 63 6c 69 73  resulting doclis
3013a 74 20 69 6e 74 6f 0a 2a 2a 20 6f 75 74 2e 0a 2a  t into.** out..*
3013b 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6c 6f 61  /.static int loa
3013c 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28 66  dSegmentLeaves(f
3013d 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c  ulltext_vtab *v,
3013e 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
3013f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
30140 6c 69 74 65 5f 69 6e 74 36 34 20 69 53 74 61 72  lite_int64 iStar
30141 74 4c 65 61 66 2c 20 73 71 6c 69 74 65 5f 69 6e  tLeaf, sqlite_in
30142 74 36 34 20 69 45 6e 64 4c 65 61 66 2c 0a 20 20  t64 iEndLeaf,.  
30143 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30144 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74             const
30145 20 63 68 61 72 20 2a 70 54 65 72 6d 2c 20 69 6e   char *pTerm, in
30146 74 20 6e 54 65 72 6d 2c 20 69 6e 74 20 69 73 50  t nTerm, int isP
30147 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20  refix,.         
30148 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30149 20 20 20 20 44 61 74 61 42 75 66 66 65 72 20 2a      DataBuffer *
3014a 6f 75 74 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a  out){.  int rc;.
3014b 20 20 4c 65 61 76 65 73 52 65 61 64 65 72 20 72    LeavesReader r
3014c 65 61 64 65 72 3b 0a 0a 20 20 61 73 73 65 72 74  eader;..  assert
3014d 28 20 69 53 74 61 72 74 4c 65 61 66 3c 3d 69 45  ( iStartLeaf<=iE
3014e 6e 64 4c 65 61 66 20 29 3b 0a 20 20 72 63 20 3d  ndLeaf );.  rc =
3014f 20 6c 65 61 76 65 73 52 65 61 64 65 72 49 6e 69   leavesReaderIni
30150 74 28 76 2c 20 30 2c 20 69 53 74 61 72 74 4c 65  t(v, 0, iStartLe
30151 61 66 2c 20 69 45 6e 64 4c 65 61 66 2c 20 4e 55  af, iEndLeaf, NU
30152 4c 4c 2c 20 30 2c 20 26 72 65 61 64 65 72 29 3b  LL, 0, &reader);
30153 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54  .  if( rc!=SQLIT
30154 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63  E_OK ) return rc
30155 3b 0a 0a 20 20 72 63 20 3d 20 6c 6f 61 64 53 65  ;..  rc = loadSe
30156 67 6d 65 6e 74 4c 65 61 76 65 73 49 6e 74 28 76  gmentLeavesInt(v
30157 2c 20 26 72 65 61 64 65 72 2c 20 70 54 65 72 6d  , &reader, pTerm
30158 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
30159 78 2c 20 6f 75 74 29 3b 0a 20 20 6c 65 61 76 65  x, out);.  leave
3015a 73 52 65 61 64 65 72 52 65 73 65 74 28 26 72 65  sReaderReset(&re
3015b 61 64 65 72 29 3b 0a 20 20 6c 65 61 76 65 73 52  ader);.  leavesR
3015c 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65  eaderDestroy(&re
3015d 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ader);.  return 
3015e 72 63 3b 0a 7d 0a 0a 2f 2a 20 54 61 6b 69 6e 67  rc;.}../* Taking
3015f 20 70 44 61 74 61 2f 6e 44 61 74 61 20 61 73 20   pData/nData as 
30160 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  an interior node
30161 2c 20 66 69 6e 64 20 74 68 65 20 73 65 71 75 65  , find the seque
30162 6e 63 65 20 6f 66 20 63 68 69 6c 64 0a 2a 2a 20  nce of child.** 
30163 6e 6f 64 65 73 20 77 68 69 63 68 20 63 6f 75 6c  nodes which coul
30164 64 20 69 6e 63 6c 75 64 65 20 70 54 65 72 6d 2f  d include pTerm/
30165 6e 54 65 72 6d 2f 69 73 50 72 65 66 69 78 2e 20  nTerm/isPrefix. 
30166 20 4e 6f 74 65 20 74 68 61 74 20 74 68 65 0a 2a   Note that the.*
30167 2a 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65 20  * interior node 
30168 74 65 72 6d 73 20 6c 6f 67 69 63 61 6c 6c 79 20  terms logically 
30169 63 6f 6d 65 20 62 65 74 77 65 65 6e 20 74 68 65  come between the
3016a 20 62 6c 6f 63 6b 73 2c 20 73 6f 20 74 68 65 72   blocks, so ther
3016b 65 20 69 73 0a 2a 2a 20 6f 6e 65 20 6d 6f 72 65  e is.** one more
3016c 20 62 6c 6f 63 6b 69 64 20 74 68 61 6e 20 74 68   blockid than th
3016d 65 72 65 20 61 72 65 20 74 65 72 6d 73 20 28 74  ere are terms (t
3016e 68 61 74 20 62 6c 6f 63 6b 20 63 6f 6e 74 61 69  hat block contai
3016f 6e 73 20 74 65 72 6d 73 20 3e 3d 0a 2a 2a 20 74  ns terms >=.** t
30170 68 65 20 6c 61 73 74 20 69 6e 74 65 72 69 6f 72  he last interior
30171 2d 6e 6f 64 65 20 74 65 72 6d 29 2e 0a 2a 2f 0a  -node term)..*/.
30172 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54  /* TODO(shess) T
30173 68 65 20 63 61 6c 6c 69 6e 67 20 63 6f 64 65 20  he calling code 
30174 6d 61 79 20 61 6c 72 65 61 64 79 20 6b 6e 6f 77  may already know
30175 20 74 68 61 74 20 74 68 65 20 65 6e 64 20 63 68   that the end ch
30176 69 6c 64 20 69 73 0a 2a 2a 20 6e 6f 74 20 77 6f  ild is.** not wo
30177 72 74 68 20 63 61 6c 63 75 6c 61 74 69 6e 67 2c  rth calculating,
30178 20 62 65 63 61 75 73 65 20 74 68 65 20 65 6e 64   because the end
30179 20 6d 61 79 20 62 65 20 69 6e 20 61 20 6c 61 74   may be in a lat
3017a 65 72 20 73 69 62 6c 69 6e 67 0a 2a 2a 20 6e 6f  er sibling.** no
3017b 64 65 2e 20 20 43 6f 6e 73 69 64 65 72 20 77 68  de.  Consider wh
3017c 65 74 68 65 72 20 62 72 65 61 6b 69 6e 67 20 73  ether breaking s
3017d 79 6d 6d 65 74 72 79 20 69 73 20 77 6f 72 74 68  ymmetry is worth
3017e 77 68 69 6c 65 2e 20 20 49 20 73 75 73 70 65 63  while.  I suspec
3017f 74 0a 2a 2a 20 69 74 20 69 73 20 6e 6f 74 20 77  t.** it is not w
30180 6f 72 74 68 77 68 69 6c 65 2e 0a 2a 2f 0a 73 74  orthwhile..*/.st
30181 61 74 69 63 20 76 6f 69 64 20 67 65 74 43 68 69  atic void getChi
30182 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28  ldrenContaining(
30183 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
30184 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
30185 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30186 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30187 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
30188 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
30189 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 20 20  t isPrefix,.    
3018a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3018b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 73 71                sq
3018c 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 53 74  lite_int64 *piSt
3018d 61 72 74 43 68 69 6c 64 2c 0a 20 20 20 20 20 20  artChild,.      
3018e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3018f 20 20 20 20 20 20 20 20 20 20 20 20 73 71 6c 69              sqli
30190 74 65 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43  te_int64 *piEndC
30191 68 69 6c 64 29 7b 0a 20 20 49 6e 74 65 72 69 6f  hild){.  Interio
30192 72 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  rReader reader;.
30193 0a 20 20 61 73 73 65 72 74 28 20 6e 44 61 74 61  .  assert( nData
30194 3e 31 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20  >1 );.  assert( 
30195 2a 70 44 61 74 61 21 3d 27 5c 30 27 20 29 3b 0a  *pData!='\0' );.
30196 20 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72    interiorReader
30197 49 6e 69 74 28 70 44 61 74 61 2c 20 6e 44 61 74  Init(pData, nDat
30198 61 2c 20 26 72 65 61 64 65 72 29 3b 0a 0a 20 20  a, &reader);..  
30199 2f 2a 20 53 63 61 6e 20 66 6f 72 20 74 68 65 20  /* Scan for the 
3019a 66 69 72 73 74 20 63 68 69 6c 64 20 77 68 69 63  first child whic
3019b 68 20 63 6f 75 6c 64 20 63 6f 6e 74 61 69 6e 20  h could contain 
3019c 70 54 65 72 6d 2f 6e 54 65 72 6d 2e 20 2a 2f 0a  pTerm/nTerm. */.
3019d 20 20 77 68 69 6c 65 28 20 21 69 6e 74 65 72 69    while( !interi
3019e 6f 72 52 65 61 64 65 72 41 74 45 6e 64 28 26 72  orReaderAtEnd(&r
3019f 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 69 66  eader) ){.    if
301a0 28 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72  ( interiorReader
301a1 54 65 72 6d 43 6d 70 28 26 72 65 61 64 65 72 2c  TermCmp(&reader,
301a2 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 30   pTerm, nTerm, 0
301a3 29 3e 30 20 29 20 62 72 65 61 6b 3b 0a 20 20 20  )>0 ) break;.   
301a4 20 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 53   interiorReaderS
301a5 74 65 70 28 26 72 65 61 64 65 72 29 3b 0a 20 20  tep(&reader);.  
301a6 7d 0a 20 20 2a 70 69 53 74 61 72 74 43 68 69 6c  }.  *piStartChil
301a7 64 20 3d 20 69 6e 74 65 72 69 6f 72 52 65 61 64  d = interiorRead
301a8 65 72 43 75 72 72 65 6e 74 42 6c 6f 63 6b 69 64  erCurrentBlockid
301a9 28 26 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a  (&reader);..  /*
301aa 20 4b 65 65 70 20 73 63 61 6e 6e 69 6e 67 20 74   Keep scanning t
301ab 6f 20 66 69 6e 64 20 61 20 74 65 72 6d 20 67 72  o find a term gr
301ac 65 61 74 65 72 20 74 68 61 6e 20 6f 75 72 20 74  eater than our t
301ad 65 72 6d 2c 20 75 73 69 6e 67 20 70 72 65 66 69  erm, using prefi
301ae 78 0a 20 20 2a 2a 20 63 6f 6d 70 61 72 69 73 6f  x.  ** compariso
301af 6e 20 69 66 20 69 6e 64 69 63 61 74 65 64 2e 20  n if indicated. 
301b0 20 49 66 20 69 73 50 72 65 66 69 78 20 69 73 20   If isPrefix is 
301b1 66 61 6c 73 65 2c 20 74 68 69 73 20 77 69 6c 6c  false, this will
301b2 20 62 65 20 74 68 65 0a 20 20 2a 2a 20 73 61 6d   be the.  ** sam
301b3 65 20 62 6c 6f 63 6b 69 64 20 61 73 20 74 68 65  e blockid as the
301b4 20 73 74 61 72 74 69 6e 67 20 62 6c 6f 63 6b 2e   starting block.
301b5 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 21  .  */.  while( !
301b6 69 6e 74 65 72 69 6f 72 52 65 61 64 65 72 41 74  interiorReaderAt
301b7 45 6e 64 28 26 72 65 61 64 65 72 29 20 29 7b 0a  End(&reader) ){.
301b8 20 20 20 20 69 66 28 20 69 6e 74 65 72 69 6f 72      if( interior
301b9 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 72  ReaderTermCmp(&r
301ba 65 61 64 65 72 2c 20 70 54 65 72 6d 2c 20 6e 54  eader, pTerm, nT
301bb 65 72 6d 2c 20 69 73 50 72 65 66 69 78 29 3e 30  erm, isPrefix)>0
301bc 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 69 6e   ) break;.    in
301bd 74 65 72 69 6f 72 52 65 61 64 65 72 53 74 65 70  teriorReaderStep
301be 28 26 72 65 61 64 65 72 29 3b 0a 20 20 7d 0a 20  (&reader);.  }. 
301bf 20 2a 70 69 45 6e 64 43 68 69 6c 64 20 3d 20 69   *piEndChild = i
301c0 6e 74 65 72 69 6f 72 52 65 61 64 65 72 43 75 72  nteriorReaderCur
301c1 72 65 6e 74 42 6c 6f 63 6b 69 64 28 26 72 65 61  rentBlockid(&rea
301c2 64 65 72 29 3b 0a 0a 20 20 69 6e 74 65 72 69 6f  der);..  interio
301c3 72 52 65 61 64 65 72 44 65 73 74 72 6f 79 28 26  rReaderDestroy(&
301c4 72 65 61 64 65 72 29 3b 0a 0a 20 20 2f 2a 20 43  reader);..  /* C
301c5 68 69 6c 64 72 65 6e 20 6d 75 73 74 20 61 73 63  hildren must asc
301c6 65 6e 64 2c 20 61 6e 64 20 69 66 20 21 70 72 65  end, and if !pre
301c7 66 69 78 2c 20 62 6f 74 68 20 6d 75 73 74 20 62  fix, both must b
301c8 65 20 74 68 65 20 73 61 6d 65 2e 20 2a 2f 0a 20  e the same. */. 
301c9 20 61 73 73 65 72 74 28 20 2a 70 69 45 6e 64 43   assert( *piEndC
301ca 68 69 6c 64 3e 3d 2a 70 69 53 74 61 72 74 43 68  hild>=*piStartCh
301cb 69 6c 64 20 29 3b 0a 20 20 61 73 73 65 72 74 28  ild );.  assert(
301cc 20 69 73 50 72 65 66 69 78 20 7c 7c 20 2a 70 69   isPrefix || *pi
301cd 53 74 61 72 74 43 68 69 6c 64 3d 3d 2a 70 69 45  StartChild==*piE
301ce 6e 64 43 68 69 6c 64 20 29 3b 0a 7d 0a 0a 2f 2a  ndChild );.}../*
301cf 20 52 65 61 64 20 62 6c 6f 63 6b 20 61 74 20 69   Read block at i
301d0 42 6c 6f 63 6b 69 64 20 61 6e 64 20 70 61 73 73  Blockid and pass
301d1 20 69 74 20 77 69 74 68 20 6f 74 68 65 72 20 70   it with other p
301d2 61 72 61 6d 73 20 74 6f 0a 2a 2a 20 67 65 74 43  arams to.** getC
301d3 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
301d4 67 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  g()..*/.static i
301d5 6e 74 20 6c 6f 61 64 41 6e 64 47 65 74 43 68 69  nt loadAndGetChi
301d6 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28  ldrenContaining(
301d7 0a 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62  .  fulltext_vtab
301d8 20 2a 76 2c 0a 20 20 73 71 6c 69 74 65 5f 69 6e   *v,.  sqlite_in
301d9 74 36 34 20 69 42 6c 6f 63 6b 69 64 2c 0a 20 20  t64 iBlockid,.  
301da 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
301db 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20 69 6e  m, int nTerm, in
301dc 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20 73 71  t isPrefix,.  sq
301dd 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70 69 53 74  lite_int64 *piSt
301de 61 72 74 43 68 69 6c 64 2c 20 73 71 6c 69 74 65  artChild, sqlite
301df 5f 69 6e 74 36 34 20 2a 70 69 45 6e 64 43 68 69  _int64 *piEndChi
301e0 6c 64 0a 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f  ld.){.  sqlite3_
301e1 73 74 6d 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a  stmt *s = NULL;.
301e2 20 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73    int rc;..  ass
301e3 65 72 74 28 20 69 42 6c 6f 63 6b 69 64 21 3d 30  ert( iBlockid!=0
301e4 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70 54   );.  assert( pT
301e5 65 72 6d 21 3d 4e 55 4c 4c 20 29 3b 0a 20 20 61  erm!=NULL );.  a
301e6 73 73 65 72 74 28 20 6e 54 65 72 6d 21 3d 30 20  ssert( nTerm!=0 
301e7 29 3b 20 20 20 20 20 20 20 20 2f 2a 20 54 4f 44  );        /* TOD
301e8 4f 28 73 68 65 73 73 29 20 57 68 79 20 6e 6f 74  O(shess) Why not
301e9 20 61 6c 6c 6f 77 20 74 68 69 73 3f 20 2a 2f 0a   allow this? */.
301ea 20 20 61 73 73 65 72 74 28 20 70 69 53 74 61 72    assert( piStar
301eb 74 43 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a  tChild!=NULL );.
301ec 20 20 61 73 73 65 72 74 28 20 70 69 45 6e 64 43    assert( piEndC
301ed 68 69 6c 64 21 3d 4e 55 4c 4c 20 29 3b 0a 0a 20  hild!=NULL );.. 
301ee 20 72 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74   rc = sql_get_st
301ef 61 74 65 6d 65 6e 74 28 76 2c 20 42 4c 4f 43 4b  atement(v, BLOCK
301f0 5f 53 45 4c 45 43 54 5f 53 54 4d 54 2c 20 26 73  _SELECT_STMT, &s
301f1 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c  );.  if( rc!=SQL
301f2 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20  ITE_OK ) return 
301f3 72 63 3b 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69  rc;..  rc = sqli
301f4 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 73  te3_bind_int64(s
301f5 2c 20 31 2c 20 69 42 6c 6f 63 6b 69 64 29 3b 0a  , 1, iBlockid);.
301f6 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
301f7 5f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b  _OK ) return rc;
301f8 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
301f9 5f 73 74 65 70 28 73 29 3b 0a 20 20 69 66 28 20  _step(s);.  if( 
301fa 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
301fb 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  ) return SQLITE_
301fc 45 52 52 4f 52 3b 0a 20 20 69 66 28 20 72 63 21  ERROR;.  if( rc!
301fd 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 20 72 65  =SQLITE_ROW ) re
301fe 74 75 72 6e 20 72 63 3b 0a 0a 20 20 67 65 74 43  turn rc;..  getC
301ff 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
30200 67 28 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  g(sqlite3_column
30201 5f 62 6c 6f 62 28 73 2c 20 30 29 2c 20 73 71 6c  _blob(s, 0), sql
30202 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74 65  ite3_column_byte
30203 73 28 73 2c 20 30 29 2c 0a 20 20 20 20 20 20 20  s(s, 0),.       
30204 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30205 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69   pTerm, nTerm, i
30206 73 50 72 65 66 69 78 2c 20 70 69 53 74 61 72 74  sPrefix, piStart
30207 43 68 69 6c 64 2c 20 70 69 45 6e 64 43 68 69 6c  Child, piEndChil
30208 64 29 3b 0a 0a 20 20 2f 2a 20 57 65 20 65 78 70  d);..  /* We exp
30209 65 63 74 20 6f 6e 6c 79 20 6f 6e 65 20 72 6f 77  ect only one row
3020a 2e 20 20 57 65 20 6d 75 73 74 20 65 78 65 63 75  .  We must execu
3020b 74 65 20 61 6e 6f 74 68 65 72 20 73 71 6c 69 74  te another sqlit
3020c 65 33 5f 73 74 65 70 28 29 0a 20 20 20 2a 20 74  e3_step().   * t
3020d 6f 20 63 6f 6d 70 6c 65 74 65 20 74 68 65 20 69  o complete the i
3020e 74 65 72 61 74 69 6f 6e 3b 20 6f 74 68 65 72 77  teration; otherw
3020f 69 73 65 20 74 68 65 20 74 61 62 6c 65 20 77 69  ise the table wi
30210 6c 6c 20 72 65 6d 61 69 6e 0a 20 20 20 2a 20 6c  ll remain.   * l
30211 6f 63 6b 65 64 2e 20 2a 2f 0a 20 20 72 63 20 3d  ocked. */.  rc =
30212 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73 29   sqlite3_step(s)
30213 3b 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49  ;.  if( rc==SQLI
30214 54 45 5f 52 4f 57 20 29 20 72 65 74 75 72 6e 20  TE_ROW ) return 
30215 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20  SQLITE_ERROR;.  
30216 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 44  if( rc!=SQLITE_D
30217 4f 4e 45 20 29 20 72 65 74 75 72 6e 20 72 63 3b  ONE ) return rc;
30218 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54  ..  return SQLIT
30219 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 54 72 61 76  E_OK;.}../* Trav
3021a 65 72 73 65 20 74 68 65 20 74 72 65 65 20 72 65  erse the tree re
3021b 70 72 65 73 65 6e 74 65 64 20 62 79 20 70 44 61  presented by pDa
3021c 74 61 5b 6e 44 61 74 61 5d 20 6c 6f 6f 6b 69 6e  ta[nData] lookin
3021d 67 20 66 6f 72 0a 2a 2a 20 70 54 65 72 6d 5b 6e  g for.** pTerm[n
3021e 54 65 72 6d 5d 2c 20 70 6c 61 63 69 6e 67 20 69  Term], placing i
3021f 74 73 20 64 6f 63 6c 69 73 74 20 69 6e 74 6f 20  ts doclist into 
30220 2a 6f 75 74 2e 20 20 54 68 69 73 20 69 73 20 69  *out.  This is i
30221 6e 74 65 72 6e 61 6c 20 74 6f 0a 2a 2a 20 6c 6f  nternal to.** lo
30222 61 64 53 65 67 6d 65 6e 74 28 29 20 74 6f 20 6d  adSegment() to m
30223 61 6b 65 20 65 72 72 6f 72 2d 68 61 6e 64 6c 69  ake error-handli
30224 6e 67 20 63 6c 65 61 6e 65 72 2e 0a 2a 2f 0a 73  ng cleaner..*/.s
30225 74 61 74 69 63 20 69 6e 74 20 6c 6f 61 64 53 65  tatic int loadSe
30226 67 6d 65 6e 74 49 6e 74 28 66 75 6c 6c 74 65 78  gmentInt(fulltex
30227 74 5f 76 74 61 62 20 2a 76 2c 20 63 6f 6e 73 74  t_vtab *v, const
30228 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20 69 6e   char *pData, in
30229 74 20 6e 44 61 74 61 2c 0a 20 20 20 20 20 20 20  t nData,.       
3022a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3022b 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
3022c 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20 20 20  iLeavesEnd,.    
3022d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3022e 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
3022f 20 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65   *pTerm, int nTe
30230 72 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78  rm, int isPrefix
30231 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
30232 20 20 20 20 20 20 20 20 20 20 20 20 44 61 74 61              Data
30233 42 75 66 66 65 72 20 2a 6f 75 74 29 7b 0a 20 20  Buffer *out){.  
30234 2f 2a 20 53 70 65 63 69 61 6c 20 63 61 73 65 20  /* Special case 
30235 77 68 65 72 65 20 72 6f 6f 74 20 69 73 20 61 20  where root is a 
30236 6c 65 61 66 2e 20 2a 2f 0a 20 20 69 66 28 20 2a  leaf. */.  if( *
30237 70 44 61 74 61 3d 3d 27 5c 30 27 20 29 7b 0a 20  pData=='\0' ){. 
30238 20 20 20 72 65 74 75 72 6e 20 6c 6f 61 64 53 65     return loadSe
30239 67 6d 65 6e 74 4c 65 61 66 28 76 2c 20 70 44 61  gmentLeaf(v, pDa
3023a 74 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72 6d  ta, nData, pTerm
3023b 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69  , nTerm, isPrefi
3023c 78 2c 20 6f 75 74 29 3b 0a 20 20 7d 65 6c 73 65  x, out);.  }else
3023d 7b 0a 20 20 20 20 69 6e 74 20 72 63 3b 0a 20 20  {.    int rc;.  
3023e 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
3023f 53 74 61 72 74 43 68 69 6c 64 2c 20 69 45 6e 64  StartChild, iEnd
30240 43 68 69 6c 64 3b 0a 0a 20 20 20 20 2f 2a 20 50  Child;..    /* P
30241 72 6f 63 65 73 73 20 70 44 61 74 61 20 61 73 20  rocess pData as 
30242 61 6e 20 69 6e 74 65 72 69 6f 72 20 6e 6f 64 65  an interior node
30243 2c 20 74 68 65 6e 20 6c 6f 6f 70 20 64 6f 77 6e  , then loop down
30244 20 74 68 65 20 74 72 65 65 0a 20 20 20 20 2a 2a   the tree.    **
30245 20 75 6e 74 69 6c 20 77 65 20 66 69 6e 64 20 74   until we find t
30246 68 65 20 73 65 74 20 6f 66 20 6c 65 61 66 20 6e  he set of leaf n
30247 6f 64 65 73 20 74 6f 20 73 63 61 6e 20 66 6f 72  odes to scan for
30248 20 74 68 65 20 74 65 72 6d 2e 0a 20 20 20 20 2a   the term..    *
30249 2f 0a 20 20 20 20 67 65 74 43 68 69 6c 64 72 65  /.    getChildre
3024a 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 70 44 61 74  nContaining(pDat
3024b 61 2c 20 6e 44 61 74 61 2c 20 70 54 65 72 6d 2c  a, nData, pTerm,
3024c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78   nTerm, isPrefix
3024d 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
3024e 20 20 20 20 20 20 20 20 20 20 20 20 26 69 53 74              &iSt
3024f 61 72 74 43 68 69 6c 64 2c 20 26 69 45 6e 64 43  artChild, &iEndC
30250 68 69 6c 64 29 3b 0a 20 20 20 20 77 68 69 6c 65  hild);.    while
30251 28 20 69 53 74 61 72 74 43 68 69 6c 64 3e 69 4c  ( iStartChild>iL
30252 65 61 76 65 73 45 6e 64 20 29 7b 0a 20 20 20 20  eavesEnd ){.    
30253 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69    sqlite_int64 i
30254 4e 65 78 74 53 74 61 72 74 2c 20 69 4e 65 78 74  NextStart, iNext
30255 45 6e 64 3b 0a 20 20 20 20 20 20 72 63 20 3d 20  End;.      rc = 
30256 6c 6f 61 64 41 6e 64 47 65 74 43 68 69 6c 64 72  loadAndGetChildr
30257 65 6e 43 6f 6e 74 61 69 6e 69 6e 67 28 76 2c 20  enContaining(v, 
30258 69 53 74 61 72 74 43 68 69 6c 64 2c 20 70 54 65  iStartChild, pTe
30259 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65  rm, nTerm, isPre
3025a 66 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20  fix,.           
3025b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3025c 20 20 20 20 20 20 20 20 20 20 20 20 20 26 69 4e               &iN
3025d 65 78 74 53 74 61 72 74 2c 20 26 69 4e 65 78 74  extStart, &iNext
3025e 45 6e 64 29 3b 0a 20 20 20 20 20 20 69 66 28 20  End);.      if( 
3025f 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20  rc!=SQLITE_OK ) 
30260 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20 20 20  return rc;..    
30261 20 20 2f 2a 20 49 66 20 77 65 27 76 65 20 62 72    /* If we've br
30262 61 6e 63 68 65 64 2c 20 66 6f 6c 6c 6f 77 20 74  anched, follow t
30263 68 65 20 65 6e 64 20 62 72 61 6e 63 68 2c 20 74  he end branch, t
30264 6f 6f 2e 20 2a 2f 0a 20 20 20 20 20 20 69 66 28  oo. */.      if(
30265 20 69 53 74 61 72 74 43 68 69 6c 64 21 3d 69 45   iStartChild!=iE
30266 6e 64 43 68 69 6c 64 20 29 7b 0a 20 20 20 20 20  ndChild ){.     
30267 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
30268 69 44 75 6d 6d 79 3b 0a 20 20 20 20 20 20 20 20  iDummy;.        
30269 72 63 20 3d 20 6c 6f 61 64 41 6e 64 47 65 74 43  rc = loadAndGetC
3026a 68 69 6c 64 72 65 6e 43 6f 6e 74 61 69 6e 69 6e  hildrenContainin
3026b 67 28 76 2c 20 69 45 6e 64 43 68 69 6c 64 2c 20  g(v, iEndChild, 
3026c 70 54 65 72 6d 2c 20 6e 54 65 72 6d 2c 20 69 73  pTerm, nTerm, is
3026d 50 72 65 66 69 78 2c 0a 20 20 20 20 20 20 20 20  Prefix,.        
3026e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3026f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30270 20 20 26 69 44 75 6d 6d 79 2c 20 26 69 4e 65 78    &iDummy, &iNex
30271 74 45 6e 64 29 3b 0a 20 20 20 20 20 20 20 20 69  tEnd);.        i
30272 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc!=SQLITE_OK
30273 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20   ) return rc;.  
30274 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 61 73 73      }..      ass
30275 65 72 74 28 20 69 4e 65 78 74 53 74 61 72 74 3c  ert( iNextStart<
30276 3d 69 4e 65 78 74 45 6e 64 20 29 3b 0a 20 20 20  =iNextEnd );.   
30277 20 20 20 69 53 74 61 72 74 43 68 69 6c 64 20 3d     iStartChild =
30278 20 69 4e 65 78 74 53 74 61 72 74 3b 0a 20 20 20   iNextStart;.   
30279 20 20 20 69 45 6e 64 43 68 69 6c 64 20 3d 20 69     iEndChild = i
3027a 4e 65 78 74 45 6e 64 3b 0a 20 20 20 20 7d 0a 20  NextEnd;.    }. 
3027b 20 20 20 61 73 73 65 72 74 28 20 69 53 74 61 72     assert( iStar
3027c 74 43 68 69 6c 64 3c 3d 69 4c 65 61 76 65 73 45  tChild<=iLeavesE
3027d 6e 64 20 29 3b 0a 20 20 20 20 61 73 73 65 72 74  nd );.    assert
3027e 28 20 69 45 6e 64 43 68 69 6c 64 3c 3d 69 4c 65  ( iEndChild<=iLe
3027f 61 76 65 73 45 6e 64 20 29 3b 0a 0a 20 20 20 20  avesEnd );..    
30280 2f 2a 20 53 63 61 6e 20 74 68 72 6f 75 67 68 20  /* Scan through 
30281 74 68 65 20 6c 65 61 66 20 73 65 67 6d 65 6e 74  the leaf segment
30282 73 20 66 6f 72 20 64 6f 63 6c 69 73 74 73 2e 20  s for doclists. 
30283 2a 2f 0a 20 20 20 20 72 65 74 75 72 6e 20 6c 6f  */.    return lo
30284 61 64 53 65 67 6d 65 6e 74 4c 65 61 76 65 73 28  adSegmentLeaves(
30285 76 2c 20 69 53 74 61 72 74 43 68 69 6c 64 2c 20  v, iStartChild, 
30286 69 45 6e 64 43 68 69 6c 64 2c 0a 20 20 20 20 20  iEndChild,.     
30287 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30288 20 20 20 20 20 20 20 20 70 54 65 72 6d 2c 20 6e          pTerm, n
30289 54 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20  Term, isPrefix, 
3028a 6f 75 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20  out);.  }.}../* 
3028b 43 61 6c 6c 20 6c 6f 61 64 53 65 67 6d 65 6e 74  Call loadSegment
3028c 49 6e 74 28 29 20 74 6f 20 63 6f 6c 6c 65 63 74  Int() to collect
3028d 20 74 68 65 20 64 6f 63 6c 69 73 74 20 66 6f 72   the doclist for
3028e 20 70 54 65 72 6d 2f 6e 54 65 72 6d 2c 20 74 68   pTerm/nTerm, th
3028f 65 6e 0a 2a 2a 20 6d 65 72 67 65 20 69 74 73 20  en.** merge its 
30290 64 6f 63 6c 69 73 74 20 6f 76 65 72 20 2a 6f 75  doclist over *ou
30291 74 20 28 61 6e 79 20 64 75 70 6c 69 63 61 74 65  t (any duplicate
30292 20 64 6f 63 6c 69 73 74 73 20 72 65 61 64 20 66   doclists read f
30293 72 6f 6d 20 74 68 65 0a 2a 2a 20 73 65 67 6d 65  rom the.** segme
30294 6e 74 20 72 6f 6f 74 65 64 20 61 74 20 70 44 61  nt rooted at pDa
30295 74 61 20 77 69 6c 6c 20 6f 76 65 72 77 72 69 74  ta will overwrit
30296 65 20 74 68 6f 73 65 20 69 6e 20 2a 6f 75 74 29  e those in *out)
30297 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73 68 65  ..*/./* TODO(she
30298 73 73 29 20 43 6f 6e 73 69 64 65 72 20 63 68 61  ss) Consider cha
30299 6e 67 69 6e 67 20 74 68 69 73 20 74 6f 20 64 65  nging this to de
3029a 74 65 72 6d 69 6e 65 20 74 68 65 20 64 65 70 74  termine the dept
3029b 68 20 6f 66 20 74 68 65 0a 2a 2a 20 6c 65 61 76  h of the.** leav
3029c 65 73 20 75 73 69 6e 67 20 65 69 74 68 65 72 20  es using either 
3029d 74 68 65 20 66 69 72 73 74 20 63 68 61 72 61 63  the first charac
3029e 74 65 72 73 20 6f 66 20 69 6e 74 65 72 69 6f 72  ters of interior
3029f 20 6e 6f 64 65 73 20 28 77 68 65 6e 0a 2a 2a 20   nodes (when.** 
302a0 3d 3d 31 2c 20 77 65 27 72 65 20 6f 6e 65 20 6c  ==1, we're one l
302a1 65 76 65 6c 20 61 62 6f 76 65 20 74 68 65 20 6c  evel above the l
302a2 65 61 76 65 73 29 2c 20 6f 72 20 74 68 65 20 66  eaves), or the f
302a3 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f  irst character o
302a4 66 0a 2a 2a 20 74 68 65 20 72 6f 6f 74 20 28 77  f.** the root (w
302a5 68 69 63 68 20 77 69 6c 6c 20 64 65 73 63 72 69  hich will descri
302a6 62 65 20 74 68 65 20 68 65 69 67 68 74 20 6f 66  be the height of
302a7 20 74 68 65 20 74 72 65 65 20 64 69 72 65 63 74   the tree direct
302a8 6c 79 29 2e 0a 2a 2a 20 45 69 74 68 65 72 20 66  ly)..** Either f
302a9 65 65 6c 73 20 73 6f 6d 65 77 68 61 74 20 74 72  eels somewhat tr
302aa 69 63 6b 79 20 74 6f 20 6d 65 2e 0a 2a 2f 0a 2f  icky to me..*/./
302ab 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68  * TODO(shess) Th
302ac 65 20 63 75 72 72 65 6e 74 20 6d 65 72 67 65 20  e current merge 
302ad 69 73 20 6c 69 6b 65 6c 79 20 74 6f 20 62 65 20  is likely to be 
302ae 73 6c 6f 77 20 66 6f 72 20 6c 61 72 67 65 0a 2a  slow for large.*
302af 2a 20 64 6f 63 6c 69 73 74 73 20 28 74 68 6f 75  * doclists (thou
302b0 67 68 20 69 74 20 73 68 6f 75 6c 64 20 70 72 6f  gh it should pro
302b1 63 65 73 73 20 66 72 6f 6d 20 6e 65 77 65 73 74  cess from newest
302b2 2f 73 6d 61 6c 6c 65 73 74 20 74 6f 0a 2a 2a 20  /smallest to.** 
302b3 6f 6c 64 65 73 74 2f 6c 61 72 67 65 73 74 2c 20  oldest/largest, 
302b4 73 6f 20 69 74 20 6d 61 79 20 6e 6f 74 20 62 65  so it may not be
302b5 20 74 68 61 74 20 62 61 64 29 2e 20 20 49 74 20   that bad).  It 
302b6 6d 69 67 68 74 20 62 65 20 75 73 65 66 75 6c 20  might be useful 
302b7 74 6f 0a 2a 2a 20 6d 6f 64 69 66 79 20 74 68 69  to.** modify thi
302b8 6e 67 73 20 74 6f 20 61 6c 6c 6f 77 20 66 6f 72  ngs to allow for
302b9 20 4e 2d 77 61 79 20 6d 65 72 67 69 6e 67 2e 20   N-way merging. 
302ba 20 54 68 69 73 20 63 6f 75 6c 64 20 65 69 74 68   This could eith
302bb 65 72 20 62 65 0a 2a 2a 20 77 69 74 68 69 6e 20  er be.** within 
302bc 61 20 73 65 67 6d 65 6e 74 2c 20 77 69 74 68 20  a segment, with 
302bd 70 61 69 72 77 69 73 65 20 6d 65 72 67 65 73 20  pairwise merges 
302be 61 63 72 6f 73 73 20 73 65 67 6d 65 6e 74 73 2c  across segments,
302bf 20 6f 72 20 61 63 72 6f 73 73 0a 2a 2a 20 61 6c   or across.** al
302c0 6c 20 73 65 67 6d 65 6e 74 73 20 61 74 20 6f 6e  l segments at on
302c1 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ce..*/.static in
302c2 74 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 66 75  t loadSegment(fu
302c3 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20  lltext_vtab *v, 
302c4 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 44 61 74  const char *pDat
302c5 61 2c 20 69 6e 74 20 6e 44 61 74 61 2c 0a 20 20  a, int nData,.  
302c6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
302c7 20 20 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36       sqlite_int6
302c8 34 20 69 4c 65 61 76 65 73 45 6e 64 2c 0a 20 20  4 iLeavesEnd,.  
302c9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
302ca 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20       const char 
302cb 2a 70 54 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72  *pTerm, int nTer
302cc 6d 2c 20 69 6e 74 20 69 73 50 72 65 66 69 78 2c  m, int isPrefix,
302cd 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
302ce 20 20 20 20 20 20 20 20 44 61 74 61 42 75 66 66          DataBuff
302cf 65 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61 74 61  er *out){.  Data
302d0 42 75 66 66 65 72 20 72 65 73 75 6c 74 3b 0a 20  Buffer result;. 
302d1 20 69 6e 74 20 72 63 3b 0a 0a 20 20 61 73 73 65   int rc;..  asse
302d2 72 74 28 20 6e 44 61 74 61 3e 31 20 29 3b 0a 0a  rt( nData>1 );..
302d3 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20 73    /* This code s
302d4 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20 63  hould never be c
302d5 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66 66 65  alled with buffe
302d6 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a 2f 0a  red updates. */.
302d7 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50 65    assert( v->nPe
302d8 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a 0a  ndingData<0 );..
302d9 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69 74    dataBufferInit
302da 28 26 72 65 73 75 6c 74 2c 20 30 29 3b 0a 20 20  (&result, 0);.  
302db 72 63 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74  rc = loadSegment
302dc 49 6e 74 28 76 2c 20 70 44 61 74 61 2c 20 6e 44  Int(v, pData, nD
302dd 61 74 61 2c 20 69 4c 65 61 76 65 73 45 6e 64 2c  ata, iLeavesEnd,
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 70 54 65 72 6d 2c 20 6e 54         pTerm, nT
302e0 65 72 6d 2c 20 69 73 50 72 65 66 69 78 2c 20 26  erm, isPrefix, &
302e1 72 65 73 75 6c 74 29 3b 0a 20 20 69 66 28 20 72  result);.  if( r
302e2 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20  c==SQLITE_OK && 
302e3 72 65 73 75 6c 74 2e 6e 44 61 74 61 3e 30 20 29  result.nData>0 )
302e4 7b 0a 20 20 20 20 69 66 28 20 6f 75 74 2d 3e 6e  {.    if( out->n
302e5 44 61 74 61 3d 3d 30 20 29 7b 0a 20 20 20 20 20  Data==0 ){.     
302e6 20 44 61 74 61 42 75 66 66 65 72 20 74 6d 70 20   DataBuffer tmp 
302e7 3d 20 2a 6f 75 74 3b 0a 20 20 20 20 20 20 2a 6f  = *out;.      *o
302e8 75 74 20 3d 20 72 65 73 75 6c 74 3b 0a 20 20 20  ut = result;.   
302e9 20 20 20 72 65 73 75 6c 74 20 3d 20 74 6d 70 3b     result = tmp;
302ea 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
302eb 20 20 44 61 74 61 42 75 66 66 65 72 20 6d 65 72    DataBuffer mer
302ec 67 65 64 3b 0a 20 20 20 20 20 20 44 4c 52 65 61  ged;.      DLRea
302ed 64 65 72 20 72 65 61 64 65 72 73 5b 32 5d 3b 0a  der readers[2];.
302ee 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26  .      dlrInit(&
302ef 72 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f 44  readers[0], DL_D
302f0 45 46 41 55 4c 54 2c 20 6f 75 74 2d 3e 70 44 61  EFAULT, out->pDa
302f1 74 61 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 29 3b  ta, out->nData);
302f2 0a 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26  .      dlrInit(&
302f3 72 65 61 64 65 72 73 5b 31 5d 2c 20 44 4c 5f 44  readers[1], DL_D
302f4 45 46 41 55 4c 54 2c 20 72 65 73 75 6c 74 2e 70  EFAULT, result.p
302f5 44 61 74 61 2c 20 72 65 73 75 6c 74 2e 6e 44 61  Data, result.nDa
302f6 74 61 29 3b 0a 20 20 20 20 20 20 64 61 74 61 42  ta);.      dataB
302f7 75 66 66 65 72 49 6e 69 74 28 26 6d 65 72 67 65  ufferInit(&merge
302f8 64 2c 20 6f 75 74 2d 3e 6e 44 61 74 61 2b 72 65  d, out->nData+re
302f9 73 75 6c 74 2e 6e 44 61 74 61 29 3b 0a 20 20 20  sult.nData);.   
302fa 20 20 20 64 6f 63 4c 69 73 74 4d 65 72 67 65 28     docListMerge(
302fb 26 6d 65 72 67 65 64 2c 20 72 65 61 64 65 72 73  &merged, readers
302fc 2c 20 32 29 3b 0a 20 20 20 20 20 20 64 61 74 61  , 2);.      data
302fd 42 75 66 66 65 72 44 65 73 74 72 6f 79 28 6f 75  BufferDestroy(ou
302fe 74 29 3b 0a 20 20 20 20 20 20 2a 6f 75 74 20 3d  t);.      *out =
302ff 20 6d 65 72 67 65 64 3b 0a 20 20 20 20 20 20 64   merged;.      d
30300 6c 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65  lrDestroy(&reade
30301 72 73 5b 30 5d 29 3b 0a 20 20 20 20 20 20 64 6c  rs[0]);.      dl
30302 72 44 65 73 74 72 6f 79 28 26 72 65 61 64 65 72  rDestroy(&reader
30303 73 5b 31 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  s[1]);.    }.  }
30304 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
30305 74 72 6f 79 28 26 72 65 73 75 6c 74 29 3b 0a 20  troy(&result);. 
30306 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f   return rc;.}../
30307 2a 20 53 63 61 6e 20 74 68 65 20 64 61 74 61 62  * Scan the datab
30308 61 73 65 20 61 6e 64 20 6d 65 72 67 65 20 74 6f  ase and merge to
30309 67 65 74 68 65 72 20 74 68 65 20 70 6f 73 74 69  gether the posti
3030a 6e 67 20 6c 69 73 74 73 20 66 6f 72 20 74 68 65  ng lists for the
3030b 20 74 65 72 6d 0a 2a 2a 20 69 6e 74 6f 20 2a 6f   term.** into *o
3030c 75 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ut..*/.static in
3030d 74 20 74 65 72 6d 53 65 6c 65 63 74 28 66 75 6c  t termSelect(ful
3030e 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 69  ltext_vtab *v, i
3030f 6e 74 20 69 43 6f 6c 75 6d 6e 2c 0a 20 20 20 20  nt iColumn,.    
30310 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30311 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54    const char *pT
30312 65 72 6d 2c 20 69 6e 74 20 6e 54 65 72 6d 2c 20  erm, int nTerm, 
30313 69 6e 74 20 69 73 50 72 65 66 69 78 2c 0a 20 20  int isPrefix,.  
30314 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30315 20 20 20 20 44 6f 63 4c 69 73 74 54 79 70 65 20      DocListType 
30316 69 54 79 70 65 2c 20 44 61 74 61 42 75 66 66 65  iType, DataBuffe
30317 72 20 2a 6f 75 74 29 7b 0a 20 20 44 61 74 61 42  r *out){.  DataB
30318 75 66 66 65 72 20 64 6f 63 6c 69 73 74 3b 0a 20  uffer doclist;. 
30319 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73   sqlite3_stmt *s
3031a 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 73 71 6c  ;.  int rc = sql
3031b 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
3031c 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
3031d 41 4c 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20  ALL_STMT, &s);. 
3031e 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
3031f 4f 4b 20 29 20 72 65 74 75 72 6e 20 72 63 3b 0a  OK ) return rc;.
30320 0a 20 20 2f 2a 20 54 68 69 73 20 63 6f 64 65 20  .  /* This code 
30321 73 68 6f 75 6c 64 20 6e 65 76 65 72 20 62 65 20  should never be 
30322 63 61 6c 6c 65 64 20 77 69 74 68 20 62 75 66 66  called with buff
30323 65 72 65 64 20 75 70 64 61 74 65 73 2e 20 2a 2f  ered updates. */
30324 0a 20 20 61 73 73 65 72 74 28 20 76 2d 3e 6e 50  .  assert( v->nP
30325 65 6e 64 69 6e 67 44 61 74 61 3c 30 20 29 3b 0a  endingData<0 );.
30326 0a 20 20 64 61 74 61 42 75 66 66 65 72 49 6e 69  .  dataBufferIni
30327 74 28 26 64 6f 63 6c 69 73 74 2c 20 30 29 3b 0a  t(&doclist, 0);.
30328 0a 20 20 2f 2a 20 54 72 61 76 65 72 73 65 20 74  .  /* Traverse t
30329 68 65 20 73 65 67 6d 65 6e 74 73 20 66 72 6f 6d  he segments from
3032a 20 6f 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73   oldest to newes
3032b 74 20 73 6f 20 74 68 61 74 20 6e 65 77 65 72 20  t so that newer 
3032c 64 6f 63 6c 69 73 74 0a 20 20 2a 2a 20 65 6c 65  doclist.  ** ele
3032d 6d 65 6e 74 73 20 66 6f 72 20 67 69 76 65 6e 20  ments for given 
3032e 64 6f 63 69 64 73 20 6f 76 65 72 77 72 69 74 65  docids overwrite
3032f 20 6f 6c 64 65 72 20 65 6c 65 6d 65 6e 74 73 2e   older elements.
30330 0a 20 20 2a 2f 0a 20 20 77 68 69 6c 65 28 20 28  .  */.  while( (
30331 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74 65  rc = sqlite3_ste
30332 70 28 73 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(s))==SQLITE_RO
30333 57 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74 20 63  W ){.    const c
30334 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73 71 6c  har *pData = sql
30335 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62  ite3_column_blob
30336 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f 6e 73  (s, 2);.    cons
30337 74 20 69 6e 74 20 6e 44 61 74 61 20 3d 20 73 71  t int nData = sq
30338 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79 74  lite3_column_byt
30339 65 73 28 73 2c 20 32 29 3b 0a 20 20 20 20 63 6f  es(s, 2);.    co
3033a 6e 73 74 20 73 71 6c 69 74 65 5f 69 6e 74 36 34  nst sqlite_int64
3033b 20 69 4c 65 61 76 65 73 45 6e 64 20 3d 20 73 71   iLeavesEnd = sq
3033c 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3033d 36 34 28 73 2c 20 31 29 3b 0a 20 20 20 20 72 63  64(s, 1);.    rc
3033e 20 3d 20 6c 6f 61 64 53 65 67 6d 65 6e 74 28 76   = loadSegment(v
3033f 2c 20 70 44 61 74 61 2c 20 6e 44 61 74 61 2c 20  , pData, nData, 
30340 69 4c 65 61 76 65 73 45 6e 64 2c 20 70 54 65 72  iLeavesEnd, pTer
30341 6d 2c 20 6e 54 65 72 6d 2c 20 69 73 50 72 65 66  m, nTerm, isPref
30342 69 78 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ix,.            
30343 20 20 20 20 20 20 20 20 20 26 64 6f 63 6c 69 73           &doclis
30344 74 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21 3d  t);.    if( rc!=
30345 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
30346 20 65 72 72 3b 0a 20 20 7d 0a 20 20 69 66 28 20   err;.  }.  if( 
30347 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20  rc==SQLITE_DONE 
30348 29 7b 0a 20 20 20 20 69 66 28 20 64 6f 63 6c 69  ){.    if( docli
30349 73 74 2e 6e 44 61 74 61 21 3d 30 20 29 7b 0a 20  st.nData!=0 ){. 
3034a 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65       /* TODO(she
3034b 73 73 29 20 54 68 65 20 6f 6c 64 20 74 65 72 6d  ss) The old term
3034c 5f 73 65 6c 65 63 74 5f 61 6c 6c 28 29 20 63 6f  _select_all() co
3034d 64 65 20 61 70 70 6c 69 65 64 20 74 68 65 20 63  de applied the c
3034e 6f 6c 75 6d 6e 0a 20 20 20 20 20 20 2a 2a 20 72  olumn.      ** r
3034f 65 73 74 72 69 63 74 20 61 73 20 77 65 20 6d 65  estrict as we me
30350 72 67 65 64 20 73 65 67 6d 65 6e 74 73 2c 20 6c  rged segments, l
30351 65 61 64 69 6e 67 20 74 6f 20 73 6d 61 6c 6c 65  eading to smalle
30352 72 20 62 75 66 66 65 72 73 2e 0a 20 20 20 20 20  r buffers..     
30353 20 2a 2a 20 54 68 69 73 20 69 73 20 70 72 6f 62   ** This is prob
30354 61 62 6c 79 20 77 6f 72 74 68 77 68 69 6c 65 20  ably worthwhile 
30355 74 6f 20 62 72 69 6e 67 20 62 61 63 6b 2c 20 6f  to bring back, o
30356 6e 63 65 20 74 68 65 20 6e 65 77 20 73 74 6f 72  nce the new stor
30357 61 67 65 0a 20 20 20 20 20 20 2a 2a 20 73 79 73  age.      ** sys
30358 74 65 6d 20 69 73 20 63 68 65 63 6b 65 64 20 69  tem is checked i
30359 6e 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  n..      */.    
3035a 20 20 69 66 28 20 69 43 6f 6c 75 6d 6e 3d 3d 76    if( iColumn==v
3035b 2d 3e 6e 43 6f 6c 75 6d 6e 29 20 69 43 6f 6c 75  ->nColumn) iColu
3035c 6d 6e 20 3d 20 2d 31 3b 0a 20 20 20 20 20 20 64  mn = -1;.      d
3035d 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44 45  ocListTrim(DL_DE
3035e 46 41 55 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70  FAULT, doclist.p
3035f 44 61 74 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44  Data, doclist.nD
30360 61 74 61 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ata,.           
30361 20 20 20 20 20 20 20 69 43 6f 6c 75 6d 6e 2c 20         iColumn, 
30362 69 54 79 70 65 2c 20 6f 75 74 29 3b 0a 20 20 20  iType, out);.   
30363 20 7d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49   }.    rc = SQLI
30364 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 65 72 72  TE_OK;.  }.. err
30365 3a 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65  :.  dataBufferDe
30366 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b  stroy(&doclist);
30367 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
30368 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ./**************
30369 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3036a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3036b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3036c 2a 2a 2f 0a 2f 2a 20 55 73 65 64 20 74 6f 20 68  **/./* Used to h
3036d 6f 6c 64 20 68 61 73 68 74 61 62 6c 65 20 64 61  old hashtable da
3036e 74 61 20 66 6f 72 20 73 6f 72 74 69 6e 67 2e 20  ta for sorting. 
3036f 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
30370 74 20 54 65 72 6d 44 61 74 61 20 7b 0a 20 20 63  t TermData {.  c
30371 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72 6d  onst char *pTerm
30372 3b 0a 20 20 69 6e 74 20 6e 54 65 72 6d 3b 0a 20  ;.  int nTerm;. 
30373 20 44 4c 43 6f 6c 6c 65 63 74 6f 72 20 2a 70 43   DLCollector *pC
30374 6f 6c 6c 65 63 74 6f 72 3b 0a 7d 20 54 65 72 6d  ollector;.} Term
30375 44 61 74 61 3b 0a 0a 2f 2a 20 4f 72 64 65 72 73  Data;../* Orders
30376 20 54 65 72 6d 44 61 74 61 20 65 6c 65 6d 65 6e   TermData elemen
30377 74 73 20 69 6e 20 73 74 72 63 6d 70 20 66 61 73  ts in strcmp fas
30378 68 69 6f 6e 20 28 20 3c 30 20 66 6f 72 20 6c 65  hion ( <0 for le
30379 73 73 2d 74 68 61 6e 2c 20 30 0a 2a 2a 20 66 6f  ss-than, 0.** fo
3037a 72 20 65 71 75 61 6c 2c 20 3e 30 20 66 6f 72 20  r equal, >0 for 
3037b 67 72 65 61 74 65 72 2d 74 68 61 6e 29 2e 0a 2a  greater-than)..*
3037c 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 74 65 72  /.static int ter
3037d 6d 44 61 74 61 43 6d 70 28 63 6f 6e 73 74 20 76  mDataCmp(const v
3037e 6f 69 64 20 2a 61 76 2c 20 63 6f 6e 73 74 20 76  oid *av, const v
3037f 6f 69 64 20 2a 62 76 29 7b 0a 20 20 63 6f 6e 73  oid *bv){.  cons
30380 74 20 54 65 72 6d 44 61 74 61 20 2a 61 20 3d 20  t TermData *a = 
30381 28 63 6f 6e 73 74 20 54 65 72 6d 44 61 74 61 20  (const TermData 
30382 2a 29 61 76 3b 0a 20 20 63 6f 6e 73 74 20 54 65  *)av;.  const Te
30383 72 6d 44 61 74 61 20 2a 62 20 3d 20 28 63 6f 6e  rmData *b = (con
30384 73 74 20 54 65 72 6d 44 61 74 61 20 2a 29 62 76  st TermData *)bv
30385 3b 0a 20 20 69 6e 74 20 6e 20 3d 20 61 2d 3e 6e  ;.  int n = a->n
30386 54 65 72 6d 3c 62 2d 3e 6e 54 65 72 6d 20 3f 20  Term<b->nTerm ? 
30387 61 2d 3e 6e 54 65 72 6d 20 3a 20 62 2d 3e 6e 54  a->nTerm : b->nT
30388 65 72 6d 3b 0a 20 20 69 6e 74 20 63 20 3d 20 6d  erm;.  int c = m
30389 65 6d 63 6d 70 28 61 2d 3e 70 54 65 72 6d 2c 20  emcmp(a->pTerm, 
3038a 62 2d 3e 70 54 65 72 6d 2c 20 6e 29 3b 0a 20 20  b->pTerm, n);.  
3038b 69 66 28 20 63 21 3d 30 20 29 20 72 65 74 75 72  if( c!=0 ) retur
3038c 6e 20 63 3b 0a 20 20 72 65 74 75 72 6e 20 61 2d  n c;.  return a-
3038d 3e 6e 54 65 72 6d 2d 62 2d 3e 6e 54 65 72 6d 3b  >nTerm-b->nTerm;
3038e 0a 7d 0a 0a 2f 2a 20 4f 72 64 65 72 20 70 54 65  .}../* Order pTe
3038f 72 6d 73 20 64 61 74 61 20 62 79 20 74 65 72 6d  rms data by term
30390 2c 20 74 68 65 6e 20 77 72 69 74 65 20 61 20 6e  , then write a n
30391 65 77 20 6c 65 76 65 6c 20 30 20 73 65 67 6d 65  ew level 0 segme
30392 6e 74 20 75 73 69 6e 67 0a 2a 2a 20 4c 65 61 66  nt using.** Leaf
30393 57 72 69 74 65 72 2e 0a 2a 2f 0a 73 74 61 74 69  Writer..*/.stati
30394 63 20 69 6e 74 20 77 72 69 74 65 5a 65 72 6f 53  c int writeZeroS
30395 65 67 6d 65 6e 74 28 66 75 6c 6c 74 65 78 74 5f  egment(fulltext_
30396 76 74 61 62 20 2a 76 2c 20 66 74 73 33 48 61 73  vtab *v, fts3Has
30397 68 20 2a 70 54 65 72 6d 73 29 7b 0a 20 20 66 74  h *pTerms){.  ft
30398 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 3b 0a 20  s3HashElem *e;. 
30399 20 69 6e 74 20 69 64 78 2c 20 72 63 2c 20 69 2c   int idx, rc, i,
3039a 20 6e 3b 0a 20 20 54 65 72 6d 44 61 74 61 20 2a   n;.  TermData *
3039b 70 44 61 74 61 3b 0a 20 20 4c 65 61 66 57 72 69  pData;.  LeafWri
3039c 74 65 72 20 77 72 69 74 65 72 3b 0a 20 20 44 61  ter writer;.  Da
3039d 74 61 42 75 66 66 65 72 20 64 6c 3b 0a 0a 20 20  taBuffer dl;..  
3039e 2f 2a 20 44 65 74 65 72 6d 69 6e 65 20 74 68 65  /* Determine the
3039f 20 6e 65 78 74 20 69 6e 64 65 78 20 61 74 20 6c   next index at l
303a0 65 76 65 6c 20 30 2c 20 6d 65 72 67 69 6e 67 20  evel 0, merging 
303a1 61 73 20 6e 65 63 65 73 73 61 72 79 2e 20 2a 2f  as necessary. */
303a2 0a 20 20 72 63 20 3d 20 73 65 67 64 69 72 4e 65  .  rc = segdirNe
303a3 78 74 49 6e 64 65 78 28 76 2c 20 30 2c 20 26 69  xtIndex(v, 0, &i
303a4 64 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d 53  dx);.  if( rc!=S
303a5 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65 74 75 72  QLITE_OK ) retur
303a6 6e 20 72 63 3b 0a 0a 20 20 6e 20 3d 20 66 74 73  n rc;..  n = fts
303a7 33 48 61 73 68 43 6f 75 6e 74 28 70 54 65 72 6d  3HashCount(pTerm
303a8 73 29 3b 0a 20 20 70 44 61 74 61 20 3d 20 73 71  s);.  pData = sq
303a9 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 2a 73  lite3_malloc(n*s
303aa 69 7a 65 6f 66 28 54 65 72 6d 44 61 74 61 29 29  izeof(TermData))
303ab 3b 0a 0a 20 20 66 6f 72 28 69 20 3d 20 30 2c 20  ;..  for(i = 0, 
303ac 65 20 3d 20 66 74 73 33 48 61 73 68 46 69 72 73  e = fts3HashFirs
303ad 74 28 70 54 65 72 6d 73 29 3b 20 65 3b 20 69 2b  t(pTerms); e; i+
303ae 2b 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68 4e  +, e = fts3HashN
303af 65 78 74 28 65 29 29 7b 0a 20 20 20 20 61 73 73  ext(e)){.    ass
303b0 65 72 74 28 20 69 3c 6e 20 29 3b 0a 20 20 20 20  ert( i<n );.    
303b1 70 44 61 74 61 5b 69 5d 2e 70 54 65 72 6d 20 3d  pData[i].pTerm =
303b2 20 66 74 73 33 48 61 73 68 4b 65 79 28 65 29 3b   fts3HashKey(e);
303b3 0a 20 20 20 20 70 44 61 74 61 5b 69 5d 2e 6e 54  .    pData[i].nT
303b4 65 72 6d 20 3d 20 66 74 73 33 48 61 73 68 4b 65  erm = fts3HashKe
303b5 79 73 69 7a 65 28 65 29 3b 0a 20 20 20 20 70 44  ysize(e);.    pD
303b6 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65 63 74 6f  ata[i].pCollecto
303b7 72 20 3d 20 66 74 73 33 48 61 73 68 44 61 74 61  r = fts3HashData
303b8 28 65 29 3b 0a 20 20 7d 0a 20 20 61 73 73 65 72  (e);.  }.  asser
303b9 74 28 20 69 3d 3d 6e 20 29 3b 0a 0a 20 20 2f 2a  t( i==n );..  /*
303ba 20 54 4f 44 4f 28 73 68 65 73 73 29 20 53 68 6f   TODO(shess) Sho
303bb 75 6c 64 20 77 65 20 61 6c 6c 6f 77 20 75 73 65  uld we allow use
303bc 72 2d 64 65 66 69 6e 65 64 20 63 6f 6c 6c 61 74  r-defined collat
303bd 69 6f 6e 20 73 65 71 75 65 6e 63 65 73 2c 0a 20  ion sequences,. 
303be 20 2a 2a 20 68 65 72 65 3f 20 20 49 20 74 68 69   ** here?  I thi
303bf 6e 6b 20 77 65 20 6f 6e 6c 79 20 6e 65 65 64 20  nk we only need 
303c0 74 68 61 74 20 6f 6e 63 65 20 77 65 20 73 75 70  that once we sup
303c1 70 6f 72 74 20 70 72 65 66 69 78 20 73 65 61 72  port prefix sear
303c2 63 68 65 73 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  ches..  */.  if(
303c3 20 6e 3e 31 20 29 20 71 73 6f 72 74 28 70 44 61   n>1 ) qsort(pDa
303c4 74 61 2c 20 6e 2c 20 73 69 7a 65 6f 66 28 2a 70  ta, n, sizeof(*p
303c5 44 61 74 61 29 2c 20 74 65 72 6d 44 61 74 61 43  Data), termDataC
303c6 6d 70 29 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28  mp);..  /* TODO(
303c7 73 68 65 73 73 29 20 52 65 66 61 63 74 6f 72 20  shess) Refactor 
303c8 73 6f 20 74 68 61 74 20 77 65 20 63 61 6e 20 77  so that we can w
303c9 72 69 74 65 20 64 69 72 65 63 74 6c 79 20 74 6f  rite directly to
303ca 20 74 68 65 20 73 65 67 6d 65 6e 74 0a 20 20 2a   the segment.  *
303cb 2a 20 44 61 74 61 42 75 66 66 65 72 2c 20 61 73  * DataBuffer, as
303cc 20 68 61 70 70 65 6e 73 20 66 6f 72 20 73 65 67   happens for seg
303cd 6d 65 6e 74 20 6d 65 72 67 65 73 2e 0a 20 20 2a  ment merges..  *
303ce 2f 0a 20 20 6c 65 61 66 57 72 69 74 65 72 49 6e  /.  leafWriterIn
303cf 69 74 28 30 2c 20 69 64 78 2c 20 26 77 72 69 74  it(0, idx, &writ
303d0 65 72 29 3b 0a 20 20 64 61 74 61 42 75 66 66 65  er);.  dataBuffe
303d1 72 49 6e 69 74 28 26 64 6c 2c 20 30 29 3b 0a 20  rInit(&dl, 0);. 
303d2 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
303d3 2b 2b 29 7b 0a 20 20 20 20 64 61 74 61 42 75 66  ++){.    dataBuf
303d4 66 65 72 52 65 73 65 74 28 26 64 6c 29 3b 0a 20  ferReset(&dl);. 
303d5 20 20 20 64 6c 63 41 64 64 44 6f 63 6c 69 73 74     dlcAddDoclist
303d6 28 70 44 61 74 61 5b 69 5d 2e 70 43 6f 6c 6c 65  (pData[i].pColle
303d7 63 74 6f 72 2c 20 26 64 6c 29 3b 0a 20 20 20 20  ctor, &dl);.    
303d8 72 63 20 3d 20 6c 65 61 66 57 72 69 74 65 72 53  rc = leafWriterS
303d9 74 65 70 28 76 2c 20 26 77 72 69 74 65 72 2c 0a  tep(v, &writer,.
303da 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
303db 20 20 20 20 20 20 20 20 70 44 61 74 61 5b 69 5d          pData[i]
303dc 2e 70 54 65 72 6d 2c 20 70 44 61 74 61 5b 69 5d  .pTerm, pData[i]
303dd 2e 6e 54 65 72 6d 2c 20 64 6c 2e 70 44 61 74 61  .nTerm, dl.pData
303de 2c 20 64 6c 2e 6e 44 61 74 61 29 3b 0a 20 20 20  , dl.nData);.   
303df 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
303e0 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20  OK ) goto err;. 
303e1 20 7d 0a 20 20 72 63 20 3d 20 6c 65 61 66 57 72   }.  rc = leafWr
303e2 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76 2c 20  iterFinalize(v, 
303e3 26 77 72 69 74 65 72 29 3b 0a 0a 20 65 72 72 3a  &writer);.. err:
303e4 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
303e5 74 72 6f 79 28 26 64 6c 29 3b 0a 20 20 73 71 6c  troy(&dl);.  sql
303e6 69 74 65 33 5f 66 72 65 65 28 70 44 61 74 61 29  ite3_free(pData)
303e7 3b 0a 20 20 6c 65 61 66 57 72 69 74 65 72 44 65  ;.  leafWriterDe
303e8 73 74 72 6f 79 28 26 77 72 69 74 65 72 29 3b 0a  stroy(&writer);.
303e9 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
303ea 2f 2a 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72  /* If pendingTer
303eb 6d 73 20 68 61 73 20 64 61 74 61 2c 20 66 72 65  ms has data, fre
303ec 65 20 69 74 2e 20 2a 2f 0a 73 74 61 74 69 63 20  e it. */.static 
303ed 69 6e 74 20 63 6c 65 61 72 50 65 6e 64 69 6e 67  int clearPending
303ee 54 65 72 6d 73 28 66 75 6c 6c 74 65 78 74 5f 76  Terms(fulltext_v
303ef 74 61 62 20 2a 76 29 7b 0a 20 20 69 66 28 20 76  tab *v){.  if( v
303f0 2d 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 3d  ->nPendingData>=
303f1 30 20 29 7b 0a 20 20 20 20 66 74 73 33 48 61 73  0 ){.    fts3Has
303f2 68 45 6c 65 6d 20 2a 65 3b 0a 20 20 20 20 66 6f  hElem *e;.    fo
303f3 72 28 65 3d 66 74 73 33 48 61 73 68 46 69 72 73  r(e=fts3HashFirs
303f4 74 28 26 76 2d 3e 70 65 6e 64 69 6e 67 54 65 72  t(&v->pendingTer
303f5 6d 73 29 3b 20 65 3b 20 65 3d 66 74 73 33 48 61  ms); e; e=fts3Ha
303f6 73 68 4e 65 78 74 28 65 29 29 7b 0a 20 20 20 20  shNext(e)){.    
303f7 20 20 64 6c 63 44 65 6c 65 74 65 28 66 74 73 33    dlcDelete(fts3
303f8 48 61 73 68 44 61 74 61 28 65 29 29 3b 0a 20 20  HashData(e));.  
303f9 20 20 7d 0a 20 20 20 20 66 74 73 33 48 61 73 68    }.    fts3Hash
303fa 43 6c 65 61 72 28 26 76 2d 3e 70 65 6e 64 69 6e  Clear(&v->pendin
303fb 67 54 65 72 6d 73 29 3b 0a 20 20 20 20 76 2d 3e  gTerms);.    v->
303fc 6e 50 65 6e 64 69 6e 67 44 61 74 61 20 3d 20 2d  nPendingData = -
303fd 31 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20  1;.  }.  return 
303fe 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
303ff 20 49 66 20 70 65 6e 64 69 6e 67 54 65 72 6d 73   If pendingTerms
30400 20 68 61 73 20 64 61 74 61 2c 20 66 6c 75 73 68   has data, flush
30401 20 69 74 20 74 6f 20 61 20 6c 65 76 65 6c 2d 7a   it to a level-z
30402 65 72 6f 20 73 65 67 6d 65 6e 74 2c 20 61 6e 64  ero segment, and
30403 0a 2a 2a 20 66 72 65 65 20 69 74 2e 0a 2a 2f 0a  .** free it..*/.
30404 73 74 61 74 69 63 20 69 6e 74 20 66 6c 75 73 68  static int flush
30405 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 66 75 6c  PendingTerms(ful
30406 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 29 7b 0a  ltext_vtab *v){.
30407 20 20 69 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e    if( v->nPendin
30408 67 44 61 74 61 3e 3d 30 20 29 7b 0a 20 20 20 20  gData>=0 ){.    
30409 69 6e 74 20 72 63 20 3d 20 77 72 69 74 65 5a 65  int rc = writeZe
3040a 72 6f 53 65 67 6d 65 6e 74 28 76 2c 20 26 76 2d  roSegment(v, &v-
3040b 3e 70 65 6e 64 69 6e 67 54 65 72 6d 73 29 3b 0a  >pendingTerms);.
3040c 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3040d 54 45 5f 4f 4b 20 29 20 63 6c 65 61 72 50 65 6e  TE_OK ) clearPen
3040e 64 69 6e 67 54 65 72 6d 73 28 76 29 3b 0a 20 20  dingTerms(v);.  
3040f 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d    return rc;.  }
30410 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
30411 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 49 66 20 70 65  _OK;.}../* If pe
30412 6e 64 69 6e 67 54 65 72 6d 73 20 69 73 20 22 74  ndingTerms is "t
30413 6f 6f 20 62 69 67 22 2c 20 6f 72 20 64 6f 63 69  oo big", or doci
30414 64 20 69 73 20 6f 75 74 20 6f 66 20 6f 72 64 65  d is out of orde
30415 72 2c 20 66 6c 75 73 68 20 69 74 2e 0a 2a 2a 20  r, flush it..** 
30416 52 65 67 61 72 64 6c 65 73 73 2c 20 62 65 20 63  Regardless, be c
30417 65 72 74 61 69 6e 20 74 68 61 74 20 70 65 6e 64  ertain that pend
30418 69 6e 67 54 65 72 6d 73 20 69 73 20 69 6e 69 74  ingTerms is init
30419 69 61 6c 69 7a 65 64 20 66 6f 72 20 75 73 65 2e  ialized for use.
3041a 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69  .*/.static int i
3041b 6e 69 74 50 65 6e 64 69 6e 67 54 65 72 6d 73 28  nitPendingTerms(
3041c 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76  fulltext_vtab *v
3041d 2c 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69  , sqlite_int64 i
3041e 44 6f 63 69 64 29 7b 0a 20 20 2f 2a 20 54 4f 44  Docid){.  /* TOD
3041f 4f 28 73 68 65 73 73 29 20 45 78 70 6c 6f 72 65  O(shess) Explore
30420 20 77 68 65 74 68 65 72 20 70 61 72 74 69 61 6c   whether partial
30421 6c 79 20 66 6c 75 73 68 69 6e 67 20 74 68 65 20  ly flushing the 
30422 62 75 66 66 65 72 20 6f 6e 0a 20 20 2a 2a 20 66  buffer on.  ** f
30423 6f 72 63 65 64 2d 66 6c 75 73 68 20 77 6f 75 6c  orced-flush woul
30424 64 20 70 72 6f 76 69 64 65 20 62 65 74 74 65 72  d provide better
30425 20 70 65 72 66 6f 72 6d 61 6e 63 65 2e 20 20 49   performance.  I
30426 20 73 75 73 70 65 63 74 20 74 68 61 74 20 69 66   suspect that if
30427 0a 20 20 2a 2a 20 77 65 20 6f 72 64 65 72 65 64  .  ** we ordered
30428 20 74 68 65 20 64 6f 63 6c 69 73 74 73 20 62 79   the doclists by
30429 20 73 69 7a 65 20 61 6e 64 20 66 6c 75 73 68 65   size and flushe
3042a 64 20 74 68 65 20 6c 61 72 67 65 73 74 20 75 6e  d the largest un
3042b 74 69 6c 20 74 68 65 0a 20 20 2a 2a 20 62 75 66  til the.  ** buf
3042c 66 65 72 20 77 61 73 20 68 61 6c 66 20 65 6d 70  fer was half emp
3042d 74 79 2c 20 74 68 61 74 20 77 6f 75 6c 64 20 6c  ty, that would l
3042e 65 74 20 74 68 65 20 6c 65 73 73 20 66 72 65 71  et the less freq
3042f 75 65 6e 74 20 74 65 72 6d 73 0a 20 20 2a 2a 20  uent terms.  ** 
30430 67 65 6e 65 72 61 74 65 20 6c 6f 6e 67 65 72 20  generate longer 
30431 64 6f 63 6c 69 73 74 73 2e 0a 20 20 2a 2f 0a 20  doclists..  */. 
30432 20 69 66 28 20 69 44 6f 63 69 64 3c 3d 76 2d 3e   if( iDocid<=v->
30433 69 50 72 65 76 44 6f 63 69 64 20 7c 7c 20 76 2d  iPrevDocid || v-
30434 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3e 6b 50  >nPendingData>kP
30435 65 6e 64 69 6e 67 54 68 72 65 73 68 6f 6c 64 20  endingThreshold 
30436 29 7b 0a 20 20 20 20 69 6e 74 20 72 63 20 3d 20  ){.    int rc = 
30437 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d  flushPendingTerm
30438 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63  s(v);.    if( rc
30439 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 72 65  !=SQLITE_OK ) re
3043a 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 69  turn rc;.  }.  i
3043b 66 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67 44 61  f( v->nPendingDa
3043c 74 61 3c 30 20 29 7b 0a 20 20 20 20 66 74 73 33  ta<0 ){.    fts3
3043d 48 61 73 68 49 6e 69 74 28 26 76 2d 3e 70 65 6e  HashInit(&v->pen
3043e 64 69 6e 67 54 65 72 6d 73 2c 20 46 54 53 33 5f  dingTerms, FTS3_
3043f 48 41 53 48 5f 53 54 52 49 4e 47 2c 20 31 29 3b  HASH_STRING, 1);
30440 0a 20 20 20 20 76 2d 3e 6e 50 65 6e 64 69 6e 67  .    v->nPending
30441 44 61 74 61 20 3d 20 30 3b 0a 20 20 7d 0a 20 20  Data = 0;.  }.  
30442 76 2d 3e 69 50 72 65 76 44 6f 63 69 64 20 3d 20  v->iPrevDocid = 
30443 69 44 6f 63 69 64 3b 0a 20 20 72 65 74 75 72 6e  iDocid;.  return
30444 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
30445 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
30446 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 78  implements the x
30447 55 70 64 61 74 65 20 63 61 6c 6c 62 61 63 6b 3b  Update callback;
30448 20 69 74 20 69 73 20 74 68 65 20 74 6f 70 2d 6c   it is the top-l
30449 65 76 65 6c 20 65 6e 74 72 79 0a 20 2a 20 70 6f  evel entry. * po
3044a 69 6e 74 20 66 6f 72 20 69 6e 73 65 72 74 69 6e  int for insertin
3044b 67 2c 20 64 65 6c 65 74 69 6e 67 20 6f 72 20 75  g, deleting or u
3044c 70 64 61 74 69 6e 67 20 61 20 72 6f 77 20 69 6e  pdating a row in
3044d 20 61 20 66 75 6c 6c 2d 74 65 78 74 20 74 61 62   a full-text tab
3044e 6c 65 2e 20 2a 2f 0a 73 74 61 74 69 63 20 69 6e  le. */.static in
3044f 74 20 66 75 6c 6c 74 65 78 74 55 70 64 61 74 65  t fulltextUpdate
30450 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70  (sqlite3_vtab *p
30451 56 74 61 62 2c 20 69 6e 74 20 6e 41 72 67 2c 20  Vtab, int nArg, 
30452 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
30453 70 70 41 72 67 2c 0a 20 20 20 20 20 20 20 20 20  ppArg,.         
30454 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30455 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
30456 52 6f 77 69 64 29 7b 0a 20 20 66 75 6c 6c 74 65  Rowid){.  fullte
30457 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28 66 75  xt_vtab *v = (fu
30458 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 20 70  lltext_vtab *) p
30459 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 3b 0a  Vtab;.  int rc;.
3045a 0a 20 20 46 54 53 54 52 41 43 45 28 28 22 46 54  .  FTSTRACE(("FT
3045b 53 33 20 55 70 64 61 74 65 20 25 70 5c 6e 22 2c  S3 Update %p\n",
3045c 20 70 56 74 61 62 29 29 3b 0a 0a 20 20 69 66 28   pVtab));..  if(
3045d 20 6e 41 72 67 3c 32 20 29 7b 0a 20 20 20 20 72   nArg<2 ){.    r
3045e 63 20 3d 20 69 6e 64 65 78 5f 64 65 6c 65 74 65  c = index_delete
3045f 28 76 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  (v, sqlite3_valu
30460 65 5f 69 6e 74 36 34 28 70 70 41 72 67 5b 30 5d  e_int64(ppArg[0]
30461 29 29 3b 0a 20 20 20 20 69 66 28 20 72 63 3d 3d  ));.    if( rc==
30462 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
30463 20 20 20 2f 2a 20 49 66 20 77 65 20 6a 75 73 74     /* If we just
30464 20 64 65 6c 65 74 65 64 20 74 68 65 20 6c 61 73   deleted the las
30465 74 20 72 6f 77 20 69 6e 20 74 68 65 20 74 61 62  t row in the tab
30466 6c 65 2c 20 63 6c 65 61 72 20 6f 75 74 20 74 68  le, clear out th
30467 65 0a 20 20 20 20 20 20 2a 2a 20 69 6e 64 65 78  e.      ** index
30468 20 64 61 74 61 2e 0a 20 20 20 20 20 20 2a 2f 0a   data..      */.
30469 20 20 20 20 20 20 72 63 20 3d 20 63 6f 6e 74 65        rc = conte
3046a 6e 74 5f 65 78 69 73 74 73 28 76 29 3b 0a 20 20  nt_exists(v);.  
3046b 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
3046c 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
3046d 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b    rc = SQLITE_OK
3046e 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66  ;.      }else if
3046f 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e  ( rc==SQLITE_DON
30470 45 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  E ){.        /* 
30471 43 6c 65 61 72 20 74 68 65 20 70 65 6e 64 69 6e  Clear the pendin
30472 67 20 74 65 72 6d 73 20 73 6f 20 77 65 20 64 6f  g terms so we do
30473 6e 27 74 20 66 6c 75 73 68 20 61 20 75 73 65 6c  n't flush a usel
30474 65 73 73 20 6c 65 76 65 6c 2d 30 0a 20 20 20 20  ess level-0.    
30475 20 20 20 20 2a 2a 20 73 65 67 6d 65 6e 74 20 77      ** segment w
30476 68 65 6e 20 74 68 65 20 74 72 61 6e 73 61 63 74  hen the transact
30477 69 6f 6e 20 63 6c 6f 73 65 73 2e 0a 20 20 20 20  ion closes..    
30478 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 72      */.        r
30479 63 20 3d 20 63 6c 65 61 72 50 65 6e 64 69 6e 67  c = clearPending
3047a 54 65 72 6d 73 28 76 29 3b 0a 20 20 20 20 20 20  Terms(v);.      
3047b 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
3047c 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20  _OK ){.         
3047d 20 72 63 20 3d 20 73 65 67 64 69 72 5f 64 65 6c   rc = segdir_del
3047e 65 74 65 5f 61 6c 6c 28 76 29 3b 0a 20 20 20 20  ete_all(v);.    
3047f 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20      }.      }.  
30480 20 20 7d 0a 20 20 7d 20 65 6c 73 65 20 69 66 28    }.  } else if(
30481 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
30482 79 70 65 28 70 70 41 72 67 5b 30 5d 29 20 21 3d  ype(ppArg[0]) !=
30483 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 29 7b 0a   SQLITE_NULL ){.
30484 20 20 20 20 2f 2a 20 41 6e 20 75 70 64 61 74 65      /* An update
30485 3a 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b 30  :.     * ppArg[0
30486 5d 20 3d 20 6f 6c 64 20 72 6f 77 69 64 0a 20 20  ] = old rowid.  
30487 20 20 20 2a 20 70 70 41 72 67 5b 31 5d 20 3d 20     * ppArg[1] = 
30488 6e 65 77 20 72 6f 77 69 64 0a 20 20 20 20 20 2a  new rowid.     *
30489 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d 3e 6e   ppArg[2..2+v->n
3048a 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61 6c 75  Column-1] = valu
3048b 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72 67 5b  es.     * ppArg[
3048c 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20 3d 20  2+v->nColumn] = 
3048d 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69 63 20  value for magic 
3048e 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e 6f 72  column (we ignor
3048f 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a 20 70  e this).     * p
30490 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d  pArg[2+v->nColum
30491 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66 6f 72  n+1] = value for
30492 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f 0a 20   docid.     */. 
30493 20 20 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20     sqlite_int64 
30494 72 6f 77 69 64 20 3d 20 73 71 6c 69 74 65 33 5f  rowid = sqlite3_
30495 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70 41 72  value_int64(ppAr
30496 67 5b 30 5d 29 3b 0a 20 20 20 20 69 66 28 20 73  g[0]);.    if( s
30497 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70  qlite3_value_typ
30498 65 28 70 70 41 72 67 5b 31 5d 29 20 21 3d 20 53  e(ppArg[1]) != S
30499 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c 7c  QLITE_INTEGER ||
3049a 0a 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33  .        sqlite3
3049b 5f 76 61 6c 75 65 5f 69 6e 74 36 34 28 70 70 41  _value_int64(ppA
3049c 72 67 5b 31 5d 29 20 21 3d 20 72 6f 77 69 64 20  rg[1]) != rowid 
3049d 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51  ){.      rc = SQ
3049e 4c 49 54 45 5f 45 52 52 4f 52 3b 20 20 2f 2a 20  LITE_ERROR;  /* 
3049f 77 65 20 64 6f 6e 27 74 20 61 6c 6c 6f 77 20 63  we don't allow c
304a0 68 61 6e 67 69 6e 67 20 74 68 65 20 72 6f 77 69  hanging the rowi
304a1 64 20 2a 2f 0a 20 20 20 20 7d 65 6c 73 65 20 69  d */.    }else i
304a2 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  f( sqlite3_value
304a3 5f 74 79 70 65 28 70 70 41 72 67 5b 32 2b 76 2d  _type(ppArg[2+v-
304a4 3e 6e 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20  >nColumn+1]) != 
304a5 53 51 4c 49 54 45 5f 49 4e 54 45 47 45 52 20 7c  SQLITE_INTEGER |
304a6 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  |.              
304a7 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e  sqlite3_value_in
304a8 74 36 34 28 70 70 41 72 67 5b 32 2b 76 2d 3e 6e  t64(ppArg[2+v->n
304a9 43 6f 6c 75 6d 6e 2b 31 5d 29 20 21 3d 20 72 6f  Column+1]) != ro
304aa 77 69 64 20 29 7b 0a 20 20 20 20 20 20 72 63 20  wid ){.      rc 
304ab 3d 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 20  = SQLITE_ERROR; 
304ac 20 2f 2a 20 77 65 20 64 6f 6e 27 74 20 61 6c 6c   /* we don't all
304ad 6f 77 20 63 68 61 6e 67 69 6e 67 20 74 68 65 20  ow changing the 
304ae 64 6f 63 69 64 20 2a 2f 0a 20 20 20 20 7d 65 6c  docid */.    }el
304af 73 65 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74  se{.      assert
304b0 28 20 6e 41 72 67 3d 3d 32 2b 76 2d 3e 6e 43 6f  ( nArg==2+v->nCo
304b1 6c 75 6d 6e 2b 32 29 3b 0a 20 20 20 20 20 20 72  lumn+2);.      r
304b2 63 20 3d 20 69 6e 64 65 78 5f 75 70 64 61 74 65  c = index_update
304b3 28 76 2c 20 72 6f 77 69 64 2c 20 26 70 70 41 72  (v, rowid, &ppAr
304b4 67 5b 32 5d 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  g[2]);.    }.  }
304b5 20 65 6c 73 65 20 7b 0a 20 20 20 20 2f 2a 20 41   else {.    /* A
304b6 6e 20 69 6e 73 65 72 74 3a 0a 20 20 20 20 20 2a  n insert:.     *
304b7 20 70 70 41 72 67 5b 31 5d 20 3d 20 72 65 71 75   ppArg[1] = requ
304b8 65 73 74 65 64 20 72 6f 77 69 64 0a 20 20 20 20  ested rowid.    
304b9 20 2a 20 70 70 41 72 67 5b 32 2e 2e 32 2b 76 2d   * ppArg[2..2+v-
304ba 3e 6e 43 6f 6c 75 6d 6e 2d 31 5d 20 3d 20 76 61  >nColumn-1] = va
304bb 6c 75 65 73 0a 20 20 20 20 20 2a 20 70 70 41 72  lues.     * ppAr
304bc 67 5b 32 2b 76 2d 3e 6e 43 6f 6c 75 6d 6e 5d 20  g[2+v->nColumn] 
304bd 3d 20 76 61 6c 75 65 20 66 6f 72 20 6d 61 67 69  = value for magi
304be 63 20 63 6f 6c 75 6d 6e 20 28 77 65 20 69 67 6e  c column (we ign
304bf 6f 72 65 20 74 68 69 73 29 0a 20 20 20 20 20 2a  ore this).     *
304c0 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e 43 6f 6c   ppArg[2+v->nCol
304c1 75 6d 6e 2b 31 5d 20 3d 20 76 61 6c 75 65 20 66  umn+1] = value f
304c2 6f 72 20 64 6f 63 69 64 0a 20 20 20 20 20 2a 2f  or docid.     */
304c3 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  .    sqlite3_val
304c4 75 65 20 2a 70 52 65 71 75 65 73 74 44 6f 63 69  ue *pRequestDoci
304c5 64 20 3d 20 70 70 41 72 67 5b 32 2b 76 2d 3e 6e  d = ppArg[2+v->n
304c6 43 6f 6c 75 6d 6e 2b 31 5d 3b 0a 20 20 20 20 61  Column+1];.    a
304c7 73 73 65 72 74 28 20 6e 41 72 67 3d 3d 32 2b 76  ssert( nArg==2+v
304c8 2d 3e 6e 43 6f 6c 75 6d 6e 2b 32 29 3b 0a 20 20  ->nColumn+2);.  
304c9 20 20 69 66 28 20 53 51 4c 49 54 45 5f 4e 55 4c    if( SQLITE_NUL
304ca 4c 20 21 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  L != sqlite3_val
304cb 75 65 5f 74 79 70 65 28 70 52 65 71 75 65 73 74  ue_type(pRequest
304cc 44 6f 63 69 64 29 20 26 26 0a 20 20 20 20 20 20  Docid) &&.      
304cd 20 20 53 51 4c 49 54 45 5f 4e 55 4c 4c 20 21 3d    SQLITE_NULL !=
304ce 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74   sqlite3_value_t
304cf 79 70 65 28 70 70 41 72 67 5b 31 5d 29 20 29 7b  ype(ppArg[1]) ){
304d0 0a 20 20 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73  .      /* TODO(s
304d1 68 65 73 73 29 20 43 6f 6e 73 69 64 65 72 20 61  hess) Consider a
304d2 6c 6c 6f 77 69 6e 67 20 74 68 69 73 20 74 6f 20  llowing this to 
304d3 77 6f 72 6b 20 69 66 20 74 68 65 20 76 61 6c 75  work if the valu
304d4 65 73 20 61 72 65 0a 20 20 20 20 20 20 2a 2a 20  es are.      ** 
304d5 69 64 65 6e 74 69 63 61 6c 2e 20 20 49 27 6d 20  identical.  I'm 
304d6 69 6e 63 6c 69 6e 65 64 20 74 6f 20 64 69 73 63  inclined to disc
304d7 6f 75 72 61 67 65 20 74 68 61 74 20 75 73 61 67  ourage that usag
304d8 65 2c 20 74 68 6f 75 67 68 2c 0a 20 20 20 20 20  e, though,.     
304d9 20 2a 2a 20 67 69 76 65 6e 20 74 68 61 74 20 62   ** given that b
304da 6f 74 68 20 72 6f 77 69 64 20 61 6e 64 20 64 6f  oth rowid and do
304db 63 69 64 20 61 72 65 20 73 70 65 63 69 61 6c 20  cid are special 
304dc 63 6f 6c 75 6d 6e 73 2e 20 20 42 65 74 74 65 72  columns.  Better
304dd 0a 20 20 20 20 20 20 2a 2a 20 77 6f 75 6c 64 20  .      ** would 
304de 62 65 20 74 6f 20 64 65 66 69 6e 65 20 6f 6e 65  be to define one
304df 20 6f 72 20 74 68 65 20 6f 74 68 65 72 20 61 73   or the other as
304e0 20 74 68 65 20 64 65 66 61 75 6c 74 20 77 69 6e   the default win
304e1 6e 65 72 2c 0a 20 20 20 20 20 20 2a 2a 20 62 75  ner,.      ** bu
304e2 74 20 73 68 6f 75 6c 64 20 69 74 20 62 65 20 66  t should it be f
304e3 74 73 33 2d 63 65 6e 74 72 69 63 20 28 64 6f 63  ts3-centric (doc
304e4 69 64 29 20 6f 72 20 53 51 4c 69 74 65 2d 63 65  id) or SQLite-ce
304e5 6e 74 72 69 63 0a 20 20 20 20 20 20 2a 2a 20 28  ntric.      ** (
304e6 72 6f 77 69 64 29 3f 0a 20 20 20 20 20 20 2a 2f  rowid)?.      */
304e7 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
304e8 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 65  TE_ERROR;.    }e
304e9 6c 73 65 7b 0a 20 20 20 20 20 20 69 66 28 20 53  lse{.      if( S
304ea 51 4c 49 54 45 5f 4e 55 4c 4c 20 3d 3d 20 73 71  QLITE_NULL == sq
304eb 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65  lite3_value_type
304ec 28 70 52 65 71 75 65 73 74 44 6f 63 69 64 29 20  (pRequestDocid) 
304ed 29 7b 0a 20 20 20 20 20 20 20 20 70 52 65 71 75  ){.        pRequ
304ee 65 73 74 44 6f 63 69 64 20 3d 20 70 70 41 72 67  estDocid = ppArg
304ef 5b 31 5d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20  [1];.      }.   
304f0 20 20 20 72 63 20 3d 20 69 6e 64 65 78 5f 69 6e     rc = index_in
304f1 73 65 72 74 28 76 2c 20 70 52 65 71 75 65 73 74  sert(v, pRequest
304f2 44 6f 63 69 64 2c 20 26 70 70 41 72 67 5b 32 5d  Docid, &ppArg[2]
304f3 2c 20 70 52 6f 77 69 64 29 3b 0a 20 20 20 20 7d  , pRowid);.    }
304f4 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72  .  }..  return r
304f5 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74  c;.}..static int
304f6 20 66 75 6c 6c 74 65 78 74 53 79 6e 63 28 73 71   fulltextSync(sq
304f7 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
304f8 62 29 7b 0a 20 20 46 54 53 54 52 41 43 45 28 28  b){.  FTSTRACE((
304f9 22 46 54 53 33 20 78 53 79 6e 63 28 29 5c 6e 22  "FTS3 xSync()\n"
304fa 29 29 3b 0a 20 20 72 65 74 75 72 6e 20 66 6c 75  ));.  return flu
304fb 73 68 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28  shPendingTerms((
304fc 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
304fd 70 56 74 61 62 29 3b 0a 7d 0a 0a 73 74 61 74 69  pVtab);.}..stati
304fe 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 42 65  c int fulltextBe
304ff 67 69 6e 28 73 71 6c 69 74 65 33 5f 76 74 61 62  gin(sqlite3_vtab
30500 20 2a 70 56 74 61 62 29 7b 0a 20 20 66 75 6c 6c   *pVtab){.  full
30501 74 65 78 74 5f 76 74 61 62 20 2a 76 20 3d 20 28  text_vtab *v = (
30502 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
30503 20 70 56 74 61 62 3b 0a 20 20 46 54 53 54 52 41   pVtab;.  FTSTRA
30504 43 45 28 28 22 46 54 53 33 20 78 42 65 67 69 6e  CE(("FTS3 xBegin
30505 28 29 5c 6e 22 29 29 3b 0a 0a 20 20 2f 2a 20 41  ()\n"));..  /* A
30506 6e 79 20 62 75 66 66 65 72 65 64 20 75 70 64 61  ny buffered upda
30507 74 65 73 20 73 68 6f 75 6c 64 20 68 61 76 65 20  tes should have 
30508 62 65 65 6e 20 63 6c 65 61 72 65 64 20 62 79 20  been cleared by 
30509 74 68 65 20 70 72 65 76 69 6f 75 73 0a 20 20 2a  the previous.  *
3050a 2a 20 74 72 61 6e 73 61 63 74 69 6f 6e 2e 0a 20  * transaction.. 
3050b 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20 76 2d   */.  assert( v-
3050c 3e 6e 50 65 6e 64 69 6e 67 44 61 74 61 3c 30 20  >nPendingData<0 
3050d 29 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65 61  );.  return clea
3050e 72 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 76 29  rPendingTerms(v)
3050f 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  ;.}..static int 
30510 66 75 6c 6c 74 65 78 74 43 6f 6d 6d 69 74 28 73  fulltextCommit(s
30511 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74  qlite3_vtab *pVt
30512 61 62 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  ab){.  fulltext_
30513 76 74 61 62 20 2a 76 20 3d 20 28 66 75 6c 6c 74  vtab *v = (fullt
30514 65 78 74 5f 76 74 61 62 20 2a 29 20 70 56 74 61  ext_vtab *) pVta
30515 62 3b 0a 20 20 46 54 53 54 52 41 43 45 28 28 22  b;.  FTSTRACE(("
30516 46 54 53 33 20 78 43 6f 6d 6d 69 74 28 29 5c 6e  FTS3 xCommit()\n
30517 22 29 29 3b 0a 0a 20 20 2f 2a 20 42 75 66 66 65  "));..  /* Buffe
30518 72 65 64 20 75 70 64 61 74 65 73 20 73 68 6f 75  red updates shou
30519 6c 64 20 68 61 76 65 20 62 65 65 6e 20 63 6c 65  ld have been cle
3051a 61 72 65 64 20 62 79 20 66 75 6c 6c 74 65 78 74  ared by fulltext
3051b 53 79 6e 63 28 29 2e 20 2a 2f 0a 20 20 61 73 73  Sync(). */.  ass
3051c 65 72 74 28 20 76 2d 3e 6e 50 65 6e 64 69 6e 67  ert( v->nPending
3051d 44 61 74 61 3c 30 20 29 3b 0a 20 20 72 65 74 75  Data<0 );.  retu
3051e 72 6e 20 63 6c 65 61 72 50 65 6e 64 69 6e 67 54  rn clearPendingT
3051f 65 72 6d 73 28 76 29 3b 0a 7d 0a 0a 73 74 61 74  erms(v);.}..stat
30520 69 63 20 69 6e 74 20 66 75 6c 6c 74 65 78 74 52  ic int fulltextR
30521 6f 6c 6c 62 61 63 6b 28 73 71 6c 69 74 65 33 5f  ollback(sqlite3_
30522 76 74 61 62 20 2a 70 56 74 61 62 29 7b 0a 20 20  vtab *pVtab){.  
30523 46 54 53 54 52 41 43 45 28 28 22 46 54 53 33 20  FTSTRACE(("FTS3 
30524 78 52 6f 6c 6c 62 61 63 6b 28 29 5c 6e 22 29 29  xRollback()\n"))
30525 3b 0a 20 20 72 65 74 75 72 6e 20 63 6c 65 61 72  ;.  return clear
30526 50 65 6e 64 69 6e 67 54 65 72 6d 73 28 28 66 75  PendingTerms((fu
30527 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29 70 56  lltext_vtab *)pV
30528 74 61 62 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  tab);.}../*.** I
30529 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
3052a 20 74 68 65 20 73 6e 69 70 70 65 74 28 29 20 66   the snippet() f
3052b 75 6e 63 74 69 6f 6e 20 66 6f 72 20 46 54 53 33  unction for FTS3
3052c 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3052d 73 6e 69 70 70 65 74 46 75 6e 63 28 0a 20 20 73  snippetFunc(.  s
3052e 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
3052f 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20  pContext,.  int 
30530 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f  argc,.  sqlite3_
30531 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
30532 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
30533 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  r *pCursor;.  if
30534 28 20 61 72 67 63 3c 31 20 29 20 72 65 74 75 72  ( argc<1 ) retur
30535 6e 3b 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  n;.  if( sqlite3
30536 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76  _value_type(argv
30537 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f  [0])!=SQLITE_BLO
30538 42 20 7c 7c 0a 20 20 20 20 20 20 73 71 6c 69 74  B ||.      sqlit
30539 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61  e3_value_bytes(a
3053a 72 67 76 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28  rgv[0])!=sizeof(
3053b 70 43 75 72 73 6f 72 29 20 29 7b 0a 20 20 20 20  pCursor) ){.    
3053c 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65  sqlite3_result_e
3053d 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22  rror(pContext, "
3053e 69 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72  illegal first ar
3053f 67 75 6d 65 6e 74 20 74 6f 20 68 74 6d 6c 5f 73  gument to html_s
30540 6e 69 70 70 65 74 22 2c 2d 31 29 3b 0a 20 20 7d  nippet",-1);.  }
30541 65 6c 73 65 7b 0a 20 20 20 20 63 6f 6e 73 74 20  else{.    const 
30542 63 68 61 72 20 2a 7a 53 74 61 72 74 20 3d 20 22  char *zStart = "
30543 3c 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20  <b>";.    const 
30544 63 68 61 72 20 2a 7a 45 6e 64 20 3d 20 22 3c 2f  char *zEnd = "</
30545 62 3e 22 3b 0a 20 20 20 20 63 6f 6e 73 74 20 63  b>";.    const c
30546 68 61 72 20 2a 7a 45 6c 6c 69 70 73 69 73 20 3d  har *zEllipsis =
30547 20 22 3c 62 3e 2e 2e 2e 3c 2f 62 3e 22 3b 0a 20   "<b>...</b>";. 
30548 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73     memcpy(&pCurs
30549 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  or, sqlite3_valu
3054a 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c  e_blob(argv[0]),
3054b 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29   sizeof(pCursor)
3054c 29 3b 0a 20 20 20 20 69 66 28 20 61 72 67 63 3e  );.    if( argc>
3054d 3d 32 20 29 7b 0a 20 20 20 20 20 20 7a 53 74 61  =2 ){.      zSta
3054e 72 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  rt = (const char
3054f 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
30550 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
30551 20 20 20 20 20 69 66 28 20 61 72 67 63 3e 3d 33       if( argc>=3
30552 20 29 7b 0a 20 20 20 20 20 20 20 20 7a 45 6e 64   ){.        zEnd
30553 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 2a 29   = (const char*)
30554 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
30555 78 74 28 61 72 67 76 5b 32 5d 29 3b 0a 20 20 20  xt(argv[2]);.   
30556 20 20 20 20 20 69 66 28 20 61 72 67 63 3e 3d 34       if( argc>=4
30557 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 7a 45   ){.          zE
30558 6c 6c 69 70 73 69 73 20 3d 20 28 63 6f 6e 73 74  llipsis = (const
30559 20 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76   char*)sqlite3_v
3055a 61 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 33  alue_text(argv[3
3055b 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20  ]);.        }.  
3055c 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
3055d 73 6e 69 70 70 65 74 41 6c 6c 4f 66 66 73 65 74  snippetAllOffset
3055e 73 28 70 43 75 72 73 6f 72 29 3b 0a 20 20 20 20  s(pCursor);.    
3055f 73 6e 69 70 70 65 74 54 65 78 74 28 70 43 75 72  snippetText(pCur
30560 73 6f 72 2c 20 7a 53 74 61 72 74 2c 20 7a 45 6e  sor, zStart, zEn
30561 64 2c 20 7a 45 6c 6c 69 70 73 69 73 29 3b 0a 20  d, zEllipsis);. 
30562 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c     sqlite3_resul
30563 74 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c  t_text(pContext,
30564 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65   pCursor->snippe
30565 74 2e 7a 53 6e 69 70 70 65 74 2c 0a 20 20 20 20  t.zSnippet,.    
30566 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30567 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 73 6e 69      pCursor->sni
30568 70 70 65 74 2e 6e 53 6e 69 70 70 65 74 2c 20 53  ppet.nSnippet, S
30569 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20  QLITE_STATIC);. 
3056a 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c   }.}../*.** Impl
3056b 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
3056c 65 20 6f 66 66 73 65 74 73 28 29 20 66 75 6e 63  e offsets() func
3056d 74 69 6f 6e 20 66 6f 72 20 46 54 53 33 0a 2a 2f  tion for FTS3.*/
3056e 0a 73 74 61 74 69 63 20 76 6f 69 64 20 73 6e 69  .static void sni
3056f 70 70 65 74 4f 66 66 73 65 74 73 46 75 6e 63 28  ppetOffsetsFunc(
30570 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65  .  sqlite3_conte
30571 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20  xt *pContext,.  
30572 69 6e 74 20 61 72 67 63 2c 0a 20 20 73 71 6c 69  int argc,.  sqli
30573 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76  te3_value **argv
30574 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f 63  .){.  fulltext_c
30575 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b 0a  ursor *pCursor;.
30576 20 20 69 66 28 20 61 72 67 63 3c 31 20 29 20 72    if( argc<1 ) r
30577 65 74 75 72 6e 3b 0a 20 20 69 66 28 20 73 71 6c  eturn;.  if( sql
30578 69 74 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28  ite3_value_type(
30579 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45  argv[0])!=SQLITE
3057a 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 73  _BLOB ||.      s
3057b 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79 74  qlite3_value_byt
3057c 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69 7a  es(argv[0])!=siz
3057d 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b 0a  eof(pCursor) ){.
3057e 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
3057f 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
30580 74 2c 20 22 69 6c 6c 65 67 61 6c 20 66 69 72 73  t, "illegal firs
30581 74 20 61 72 67 75 6d 65 6e 74 20 74 6f 20 6f 66  t argument to of
30582 66 73 65 74 73 22 2c 2d 31 29 3b 0a 20 20 7d 65  fsets",-1);.  }e
30583 6c 73 65 7b 0a 20 20 20 20 6d 65 6d 63 70 79 28  lse{.    memcpy(
30584 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65  &pCursor, sqlite
30585 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
30586 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43  v[0]), sizeof(pC
30587 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 73 6e 69  ursor));.    sni
30588 70 70 65 74 41 6c 6c 4f 66 66 73 65 74 73 28 70  ppetAllOffsets(p
30589 43 75 72 73 6f 72 29 3b 0a 20 20 20 20 73 6e 69  Cursor);.    sni
3058a 70 70 65 74 4f 66 66 73 65 74 54 65 78 74 28 26  ppetOffsetText(&
3058b 70 43 75 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74  pCursor->snippet
3058c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72  );.    sqlite3_r
3058d 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e 74  esult_text(pCont
3058e 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
3058f 20 20 20 20 20 20 20 20 20 20 20 20 20 70 43 75               pCu
30590 72 73 6f 72 2d 3e 73 6e 69 70 70 65 74 2e 7a 4f  rsor->snippet.zO
30591 66 66 73 65 74 2c 20 70 43 75 72 73 6f 72 2d 3e  ffset, pCursor->
30592 73 6e 69 70 70 65 74 2e 6e 4f 66 66 73 65 74 2c  snippet.nOffset,
30593 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
30594 20 20 20 20 20 20 20 20 20 53 51 4c 49 54 45 5f           SQLITE_
30595 53 54 41 54 49 43 29 3b 0a 20 20 7d 0a 7d 0a 0a  STATIC);.  }.}..
30596 2f 2a 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64  /* OptLeavesRead
30597 65 72 20 69 73 20 6e 65 61 72 6c 79 20 69 64 65  er is nearly ide
30598 6e 74 69 63 61 6c 20 74 6f 20 4c 65 61 76 65 73  ntical to Leaves
30599 52 65 61 64 65 72 2c 20 65 78 63 65 70 74 20 74  Reader, except t
3059a 68 61 74 0a 2a 2a 20 77 68 65 72 65 20 4c 65 61  hat.** where Lea
3059b 76 65 73 52 65 61 64 65 72 20 69 73 20 67 65 61  vesReader is gea
3059c 72 65 64 20 74 6f 77 61 72 64 73 20 74 68 65 20  red towards the 
3059d 6d 65 72 67 69 6e 67 20 6f 66 20 63 6f 6d 70 6c  merging of compl
3059e 65 74 65 0a 2a 2a 20 73 65 67 6d 65 6e 74 20 6c  ete.** segment l
3059f 65 76 65 6c 73 20 28 77 69 74 68 20 65 78 61 63  evels (with exac
305a0 74 6c 79 20 4d 45 52 47 45 5f 43 4f 55 4e 54 20  tly MERGE_COUNT 
305a1 73 65 67 6d 65 6e 74 73 29 2c 20 4f 70 74 4c 65  segments), OptLe
305a2 61 76 65 73 52 65 61 64 65 72 0a 2a 2a 20 69 73  avesReader.** is
305a3 20 67 65 61 72 65 64 20 74 6f 77 61 72 64 73 20   geared towards 
305a4 69 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  implementation o
305a5 66 20 74 68 65 20 6f 70 74 69 6d 69 7a 65 28 29  f the optimize()
305a6 20 66 75 6e 63 74 69 6f 6e 2c 20 61 6e 64 0a 2a   function, and.*
305a7 2a 20 63 61 6e 20 6d 65 72 67 65 20 61 6c 6c 20  * can merge all 
305a8 73 65 67 6d 65 6e 74 73 20 73 69 6d 75 6c 74 61  segments simulta
305a9 6e 65 6f 75 73 6c 79 2e 20 20 54 68 69 73 20 76  neously.  This v
305aa 65 72 73 69 6f 6e 20 6d 61 79 20 62 65 0a 2a 2a  ersion may be.**
305ab 20 73 6f 6d 65 77 68 61 74 20 6c 65 73 73 20 65   somewhat less e
305ac 66 66 69 63 69 65 6e 74 20 74 68 61 6e 20 4c 65  fficient than Le
305ad 61 76 65 73 52 65 61 64 65 72 20 62 65 63 61 75  avesReader becau
305ae 73 65 20 69 74 20 6d 65 72 67 65 73 20 69 6e 74  se it merges int
305af 6f 20 61 6e 0a 2a 2a 20 61 63 63 75 6d 75 6c 61  o an.** accumula
305b0 74 6f 72 20 72 61 74 68 65 72 20 74 68 61 6e 20  tor rather than 
305b1 64 6f 69 6e 67 20 61 6e 20 4e 2d 77 61 79 20 6d  doing an N-way m
305b2 65 72 67 65 2c 20 62 75 74 20 73 69 6e 63 65 20  erge, but since 
305b3 73 65 67 6d 65 6e 74 0a 2a 2a 20 73 69 7a 65 20  segment.** size 
305b4 67 72 6f 77 73 20 65 78 70 6f 6e 65 6e 74 69 61  grows exponentia
305b5 6c 6c 79 20 28 73 6f 20 73 65 67 6d 65 6e 74 20  lly (so segment 
305b6 63 6f 75 6e 74 20 6c 6f 67 72 69 74 68 6d 69 63  count logrithmic
305b7 61 6c 6c 79 29 20 74 68 69 73 20 69 73 0a 2a 2a  ally) this is.**
305b8 20 70 72 6f 62 61 62 6c 79 20 6e 6f 74 20 61 6e   probably not an
305b9 20 69 6d 6d 65 64 69 61 74 65 20 70 72 6f 62 6c   immediate probl
305ba 65 6d 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f 28 73  em..*/./* TODO(s
305bb 68 65 73 73 29 3a 20 50 72 6f 76 65 20 74 68 61  hess): Prove tha
305bc 74 20 61 73 73 65 72 74 69 6f 6e 2c 20 6f 72 20  t assertion, or 
305bd 65 78 74 65 6e 64 20 74 68 65 20 6d 65 72 67 65  extend the merge
305be 20 63 6f 64 65 20 74 6f 0a 2a 2a 20 6d 65 72 67   code to.** merg
305bf 65 20 74 72 65 65 20 66 61 73 68 69 6f 6e 20 28  e tree fashion (
305c0 6c 69 6b 65 20 74 68 65 20 70 72 65 66 69 78 2d  like the prefix-
305c1 73 65 61 72 63 68 69 6e 67 20 63 6f 64 65 20 64  searching code d
305c2 6f 65 73 29 2e 0a 2a 2f 0a 2f 2a 20 54 4f 44 4f  oes)..*/./* TODO
305c3 28 73 68 65 73 73 29 3a 20 4f 70 74 4c 65 61 76  (shess): OptLeav
305c4 65 73 52 65 61 64 65 72 20 61 6e 64 20 4c 65 61  esReader and Lea
305c5 76 65 73 52 65 61 64 65 72 20 63 6f 75 6c 64 20  vesReader could 
305c6 70 72 6f 62 61 62 6c 79 20 62 65 0a 2a 2a 20 6d  probably be.** m
305c7 65 72 67 65 64 20 77 69 74 68 20 6c 69 74 74 6c  erged with littl
305c8 65 20 6f 72 20 6e 6f 20 6c 6f 73 73 20 6f 66 20  e or no loss of 
305c9 70 65 72 66 6f 72 6d 61 6e 63 65 20 66 6f 72 20  performance for 
305ca 4c 65 61 76 65 73 52 65 61 64 65 72 2e 20 20 54  LeavesReader.  T
305cb 68 65 0a 2a 2a 20 6d 65 72 67 65 64 20 63 6f 64  he.** merged cod
305cc 65 20 77 6f 75 6c 64 20 6e 65 65 64 20 74 6f 20  e would need to 
305cd 68 61 6e 64 6c 65 20 3e 4d 45 52 47 45 5f 43 4f  handle >MERGE_CO
305ce 55 4e 54 20 73 65 67 6d 65 6e 74 73 2c 20 61 6e  UNT segments, an
305cf 64 20 77 6f 75 6c 64 0a 2a 2a 20 61 6c 73 6f 20  d would.** also 
305d0 6e 65 65 64 20 74 6f 20 62 65 20 61 62 6c 65 20  need to be able 
305d1 74 6f 20 6f 70 74 69 6f 6e 61 6c 6c 79 20 6f 70  to optionally op
305d2 74 69 6d 69 7a 65 20 61 77 61 79 20 64 65 6c 65  timize away dele
305d3 74 65 73 2e 0a 2a 2f 0a 74 79 70 65 64 65 66 20  tes..*/.typedef 
305d4 73 74 72 75 63 74 20 4f 70 74 4c 65 61 76 65 73  struct OptLeaves
305d5 52 65 61 64 65 72 20 7b 0a 20 20 2f 2a 20 53 65  Reader {.  /* Se
305d6 67 6d 65 6e 74 20 6e 75 6d 62 65 72 2c 20 74 6f  gment number, to
305d7 20 6f 72 64 65 72 20 72 65 61 64 65 72 73 20 62   order readers b
305d8 79 20 61 67 65 2e 20 2a 2f 0a 20 20 69 6e 74 20  y age. */.  int 
305d9 73 65 67 6d 65 6e 74 3b 0a 20 20 4c 65 61 76 65  segment;.  Leave
305da 73 52 65 61 64 65 72 20 72 65 61 64 65 72 3b 0a  sReader reader;.
305db 7d 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64 65  } OptLeavesReade
305dc 72 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 6f  r;..static int o
305dd 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 41 74  ptLeavesReaderAt
305de 45 6e 64 28 4f 70 74 4c 65 61 76 65 73 52 65 61  End(OptLeavesRea
305df 64 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20  der *pReader){. 
305e0 20 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65   return leavesRe
305e1 61 64 65 72 41 74 45 6e 64 28 26 70 52 65 61 64  aderAtEnd(&pRead
305e2 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a 73  er->reader);.}.s
305e3 74 61 74 69 63 20 69 6e 74 20 6f 70 74 4c 65 61  tatic int optLea
305e4 76 65 73 52 65 61 64 65 72 54 65 72 6d 42 79 74  vesReaderTermByt
305e5 65 73 28 4f 70 74 4c 65 61 76 65 73 52 65 61 64  es(OptLeavesRead
305e6 65 72 20 2a 70 52 65 61 64 65 72 29 7b 0a 20 20  er *pReader){.  
305e7 72 65 74 75 72 6e 20 6c 65 61 76 65 73 52 65 61  return leavesRea
305e8 64 65 72 54 65 72 6d 42 79 74 65 73 28 26 70 52  derTermBytes(&pR
305e9 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b 0a  eader->reader);.
305ea 7d 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63  }.static const c
305eb 68 61 72 20 2a 6f 70 74 4c 65 61 76 65 73 52 65  har *optLeavesRe
305ec 61 64 65 72 44 61 74 61 28 4f 70 74 4c 65 61 76  aderData(OptLeav
305ed 65 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65  esReader *pReade
305ee 72 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  r){.  return lea
305ef 76 65 73 52 65 61 64 65 72 44 61 74 61 28 26 70  vesReaderData(&p
305f0 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72 29 3b  Reader->reader);
305f1 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 6f 70  .}.static int op
305f2 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
305f3 61 42 79 74 65 73 28 4f 70 74 4c 65 61 76 65 73  aBytes(OptLeaves
305f4 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72 29  Reader *pReader)
305f5 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76 65  {.  return leave
305f6 73 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73  sReaderDataBytes
305f7 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65  (&pReader->reade
305f8 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 63 6f 6e  r);.}.static con
305f9 73 74 20 63 68 61 72 20 2a 6f 70 74 4c 65 61 76  st char *optLeav
305fa 65 73 52 65 61 64 65 72 54 65 72 6d 28 4f 70 74  esReaderTerm(Opt
305fb 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 70 52  LeavesReader *pR
305fc 65 61 64 65 72 29 7b 0a 20 20 72 65 74 75 72 6e  eader){.  return
305fd 20 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72   leavesReaderTer
305fe 6d 28 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64  m(&pReader->read
305ff 65 72 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e  er);.}.static in
30600 74 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65  t optLeavesReade
30601 72 53 74 65 70 28 66 75 6c 6c 74 65 78 74 5f 76  rStep(fulltext_v
30602 74 61 62 20 2a 76 2c 20 4f 70 74 4c 65 61 76 65  tab *v, OptLeave
30603 73 52 65 61 64 65 72 20 2a 70 52 65 61 64 65 72  sReader *pReader
30604 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61 76  ){.  return leav
30605 65 73 52 65 61 64 65 72 53 74 65 70 28 76 2c 20  esReaderStep(v, 
30606 26 70 52 65 61 64 65 72 2d 3e 72 65 61 64 65 72  &pReader->reader
30607 29 3b 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20  );.}.static int 
30608 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54  optLeavesReaderT
30609 65 72 6d 43 6d 70 28 4f 70 74 4c 65 61 76 65 73  ermCmp(OptLeaves
3060a 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f 70 74  Reader *lr1, Opt
3060b 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a 6c 72  LeavesReader *lr
3060c 32 29 7b 0a 20 20 72 65 74 75 72 6e 20 6c 65 61  2){.  return lea
3060d 76 65 73 52 65 61 64 65 72 54 65 72 6d 43 6d 70  vesReaderTermCmp
3060e 28 26 6c 72 31 2d 3e 72 65 61 64 65 72 2c 20 26  (&lr1->reader, &
3060f 6c 72 32 2d 3e 72 65 61 64 65 72 29 3b 0a 7d 0a  lr2->reader);.}.
30610 2f 2a 20 4f 72 64 65 72 20 62 79 20 74 65 72 6d  /* Order by term
30611 20 61 73 63 65 6e 64 69 6e 67 2c 20 73 65 67 6d   ascending, segm
30612 65 6e 74 20 61 73 63 65 6e 64 69 6e 67 20 28 6f  ent ascending (o
30613 6c 64 65 73 74 20 74 6f 20 6e 65 77 65 73 74 29  ldest to newest)
30614 2c 20 77 69 74 68 0a 2a 2a 20 65 78 68 61 75 73  , with.** exhaus
30615 74 65 64 20 72 65 61 64 65 72 73 20 74 6f 20 74  ted readers to t
30616 68 65 20 65 6e 64 2e 0a 2a 2f 0a 73 74 61 74 69  he end..*/.stati
30617 63 20 69 6e 74 20 6f 70 74 4c 65 61 76 65 73 52  c int optLeavesR
30618 65 61 64 65 72 43 6d 70 28 4f 70 74 4c 65 61 76  eaderCmp(OptLeav
30619 65 73 52 65 61 64 65 72 20 2a 6c 72 31 2c 20 4f  esReader *lr1, O
3061a 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
3061b 6c 72 32 29 7b 0a 20 20 69 6e 74 20 63 20 3d 20  lr2){.  int c = 
3061c 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 54  optLeavesReaderT
3061d 65 72 6d 43 6d 70 28 6c 72 31 2c 20 6c 72 32 29  ermCmp(lr1, lr2)
3061e 3b 0a 20 20 69 66 28 20 63 21 3d 30 20 29 20 72  ;.  if( c!=0 ) r
3061f 65 74 75 72 6e 20 63 3b 0a 20 20 72 65 74 75 72  eturn c;.  retur
30620 6e 20 6c 72 31 2d 3e 73 65 67 6d 65 6e 74 2d 6c  n lr1->segment-l
30621 72 32 2d 3e 73 65 67 6d 65 6e 74 3b 0a 7d 0a 2f  r2->segment;.}./
30622 2a 20 42 75 62 62 6c 65 20 70 4c 72 5b 30 5d 20  * Bubble pLr[0] 
30623 74 6f 20 61 70 70 72 6f 70 72 69 61 74 65 20 70  to appropriate p
30624 6c 61 63 65 20 69 6e 20 70 4c 72 5b 31 2e 2e 6e  lace in pLr[1..n
30625 4c 72 2d 31 5d 2e 20 20 41 73 73 75 6d 65 73 20  Lr-1].  Assumes 
30626 74 68 61 74 0a 2a 2a 20 70 4c 72 5b 31 2e 2e 6e  that.** pLr[1..n
30627 4c 72 2d 31 5d 20 69 73 20 61 6c 72 65 61 64 79  Lr-1] is already
30628 20 73 6f 72 74 65 64 2e 0a 2a 2f 0a 73 74 61 74   sorted..*/.stat
30629 69 63 20 76 6f 69 64 20 6f 70 74 4c 65 61 76 65  ic void optLeave
3062a 73 52 65 61 64 65 72 52 65 6f 72 64 65 72 28 4f  sReaderReorder(O
3062b 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 20 2a  ptLeavesReader *
3062c 70 4c 72 2c 20 69 6e 74 20 6e 4c 72 29 7b 0a 20  pLr, int nLr){. 
3062d 20 77 68 69 6c 65 28 20 6e 4c 72 3e 31 20 26 26   while( nLr>1 &&
3062e 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
3062f 43 6d 70 28 70 4c 72 2c 20 70 4c 72 2b 31 29 3e  Cmp(pLr, pLr+1)>
30630 30 20 29 7b 0a 20 20 20 20 4f 70 74 4c 65 61 76  0 ){.    OptLeav
30631 65 73 52 65 61 64 65 72 20 74 6d 70 20 3d 20 70  esReader tmp = p
30632 4c 72 5b 30 5d 3b 0a 20 20 20 20 70 4c 72 5b 30  Lr[0];.    pLr[0
30633 5d 20 3d 20 70 4c 72 5b 31 5d 3b 0a 20 20 20 20  ] = pLr[1];.    
30634 70 4c 72 5b 31 5d 20 3d 20 74 6d 70 3b 0a 20 20  pLr[1] = tmp;.  
30635 20 20 6e 4c 72 2d 2d 3b 0a 20 20 20 20 70 4c 72    nLr--;.    pLr
30636 2b 2b 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 6f 70  ++;.  }.}../* op
30637 74 69 6d 69 7a 65 28 29 20 68 65 6c 70 65 72 20  timize() helper 
30638 66 75 6e 63 74 69 6f 6e 2e 20 20 50 75 74 20 74  function.  Put t
30639 68 65 20 72 65 61 64 65 72 73 20 69 6e 20 6f 72  he readers in or
3063a 64 65 72 20 61 6e 64 20 69 74 65 72 61 74 65 0a  der and iterate.
3063b 2a 2a 20 74 68 72 6f 75 67 68 20 74 68 65 6d 2c  ** through them,
3063c 20 6d 65 72 67 69 6e 67 20 64 6f 63 6c 69 73 74   merging doclist
3063d 73 20 66 6f 72 20 6d 61 74 63 68 69 6e 67 20 74  s for matching t
3063e 65 72 6d 73 20 69 6e 74 6f 20 70 57 72 69 74 65  erms into pWrite
3063f 72 2e 0a 2a 2a 20 52 65 74 75 72 6e 73 20 53 51  r..** Returns SQ
30640 4c 49 54 45 5f 4f 4b 20 6f 6e 20 73 75 63 63 65  LITE_OK on succe
30641 73 73 2c 20 6f 72 20 74 68 65 20 53 51 4c 69 74  ss, or the SQLit
30642 65 20 65 72 72 6f 72 20 63 6f 64 65 20 77 68 69  e error code whi
30643 63 68 0a 2a 2a 20 70 72 65 76 65 6e 74 65 64 20  ch.** prevented 
30644 73 75 63 63 65 73 73 2e 0a 2a 2f 0a 73 74 61 74  success..*/.stat
30645 69 63 20 69 6e 74 20 6f 70 74 69 6d 69 7a 65 49  ic int optimizeI
30646 6e 74 65 72 6e 61 6c 28 66 75 6c 6c 74 65 78 74  nternal(fulltext
30647 5f 76 74 61 62 20 2a 76 2c 0a 20 20 20 20 20 20  _vtab *v,.      
30648 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30649 20 20 20 20 20 20 4f 70 74 4c 65 61 76 65 73 52        OptLeavesR
3064a 65 61 64 65 72 20 2a 72 65 61 64 65 72 73 2c 20  eader *readers, 
3064b 69 6e 74 20 6e 52 65 61 64 65 72 73 2c 0a 20 20  int nReaders,.  
3064c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3064d 20 20 20 20 20 20 20 20 20 20 4c 65 61 66 57 72            LeafWr
3064e 69 74 65 72 20 2a 70 57 72 69 74 65 72 29 7b 0a  iter *pWriter){.
3064f 20 20 69 6e 74 20 69 2c 20 72 63 20 3d 20 53 51    int i, rc = SQ
30650 4c 49 54 45 5f 4f 4b 3b 0a 20 20 44 61 74 61 42  LITE_OK;.  DataB
30651 75 66 66 65 72 20 64 6f 63 6c 69 73 74 2c 20 6d  uffer doclist, m
30652 65 72 67 65 64 2c 20 74 6d 70 3b 0a 0a 20 20 2f  erged, tmp;..  /
30653 2a 20 4f 72 64 65 72 20 74 68 65 20 72 65 61 64  * Order the read
30654 65 72 73 2e 20 2a 2f 0a 20 20 69 20 3d 20 6e 52  ers. */.  i = nR
30655 65 61 64 65 72 73 3b 0a 20 20 77 68 69 6c 65 28  eaders;.  while(
30656 20 69 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20   i-- > 0 ){.    
30657 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 52  optLeavesReaderR
30658 65 6f 72 64 65 72 28 26 72 65 61 64 65 72 73 5b  eorder(&readers[
30659 69 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b  i], nReaders-i);
3065a 0a 20 20 7d 0a 0a 20 20 64 61 74 61 42 75 66 66  .  }..  dataBuff
3065b 65 72 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c  erInit(&doclist,
3065c 20 4c 45 41 46 5f 4d 41 58 29 3b 0a 20 20 64 61   LEAF_MAX);.  da
3065d 74 61 42 75 66 66 65 72 49 6e 69 74 28 26 6d 65  taBufferInit(&me
3065e 72 67 65 64 2c 20 4c 45 41 46 5f 4d 41 58 29 3b  rged, LEAF_MAX);
3065f 0a 0a 20 20 2f 2a 20 45 78 68 61 75 73 74 65 64  ..  /* Exhausted
30660 20 72 65 61 64 65 72 73 20 62 75 62 62 6c 65 20   readers bubble 
30661 74 6f 20 74 68 65 20 65 6e 64 2c 20 73 6f 20 77  to the end, so w
30662 68 65 6e 20 74 68 65 20 66 69 72 73 74 20 72 65  hen the first re
30663 61 64 65 72 20 69 73 0a 20 20 2a 2a 20 61 74 20  ader is.  ** at 
30664 65 6f 66 2c 20 61 6c 6c 20 61 72 65 20 61 74 20  eof, all are at 
30665 65 6f 66 2e 0a 20 20 2a 2f 0a 20 20 77 68 69 6c  eof..  */.  whil
30666 65 28 20 21 6f 70 74 4c 65 61 76 65 73 52 65 61  e( !optLeavesRea
30667 64 65 72 41 74 45 6e 64 28 26 72 65 61 64 65 72  derAtEnd(&reader
30668 73 5b 30 5d 29 20 29 7b 0a 0a 20 20 20 20 2f 2a  s[0]) ){..    /*
30669 20 46 69 67 75 72 65 20 6f 75 74 20 68 6f 77 20   Figure out how 
3066a 6d 61 6e 79 20 72 65 61 64 65 72 73 20 73 68 61  many readers sha
3066b 72 65 20 74 68 65 20 6e 65 78 74 20 74 65 72 6d  re the next term
3066c 2e 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 3d 31  . */.    for(i=1
3066d 3b 20 69 3c 6e 52 65 61 64 65 72 73 20 26 26 20  ; i<nReaders && 
3066e 21 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72  !optLeavesReader
3066f 41 74 45 6e 64 28 26 72 65 61 64 65 72 73 5b 69  AtEnd(&readers[i
30670 5d 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20  ]); i++){.      
30671 69 66 28 20 30 21 3d 6f 70 74 4c 65 61 76 65 73  if( 0!=optLeaves
30672 52 65 61 64 65 72 54 65 72 6d 43 6d 70 28 26 72  ReaderTermCmp(&r
30673 65 61 64 65 72 73 5b 30 5d 2c 20 26 72 65 61 64  eaders[0], &read
30674 65 72 73 5b 69 5d 29 20 29 20 62 72 65 61 6b 3b  ers[i]) ) break;
30675 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53  .    }..    /* S
30676 70 65 63 69 61 6c 2d 63 61 73 65 20 66 6f 72 20  pecial-case for 
30677 6e 6f 20 6d 65 72 67 65 2e 20 2a 2f 0a 20 20 20  no merge. */.   
30678 20 69 66 28 20 69 3d 3d 31 20 29 7b 0a 20 20 20   if( i==1 ){.   
30679 20 20 20 2f 2a 20 54 72 69 6d 20 64 65 6c 65 74     /* Trim delet
3067a 69 6f 6e 73 20 66 72 6f 6d 20 74 68 65 20 64 6f  ions from the do
3067b 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20 20 20  clist. */.      
3067c 64 61 74 61 42 75 66 66 65 72 52 65 73 65 74 28  dataBufferReset(
3067d 26 6d 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20  &merged);.      
3067e 64 6f 63 4c 69 73 74 54 72 69 6d 28 44 4c 5f 44  docListTrim(DL_D
3067f 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20 20  EFAULT,.        
30680 20 20 20 20 20 20 20 20 20 20 6f 70 74 4c 65 61            optLea
30681 76 65 73 52 65 61 64 65 72 44 61 74 61 28 26 72  vesReaderData(&r
30682 65 61 64 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20  eaders[0]),.    
30683 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
30684 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
30685 61 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b  aBytes(&readers[
30686 30 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20  0]),.           
30687 20 20 20 20 20 20 20 2d 31 2c 20 44 4c 5f 44 45         -1, DL_DE
30688 46 41 55 4c 54 2c 20 26 6d 65 72 67 65 64 29 3b  FAULT, &merged);
30689 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
3068a 20 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61    DLReader dlRea
3068b 64 65 72 73 5b 4d 45 52 47 45 5f 43 4f 55 4e 54  ders[MERGE_COUNT
3068c 5d 3b 0a 20 20 20 20 20 20 69 6e 74 20 69 52 65  ];.      int iRe
3068d 61 64 65 72 2c 20 6e 52 65 61 64 65 72 73 3b 0a  ader, nReaders;.
3068e 0a 20 20 20 20 20 20 2f 2a 20 50 72 69 6d 65 20  .      /* Prime 
3068f 74 68 65 20 70 69 70 65 6c 69 6e 65 20 77 69 74  the pipeline wit
30690 68 20 74 68 65 20 66 69 72 73 74 20 72 65 61 64  h the first read
30691 65 72 27 73 20 64 6f 63 6c 69 73 74 2e 20 20 41  er's doclist.  A
30692 66 74 65 72 0a 20 20 20 20 20 20 2a 2a 20 6f 6e  fter.      ** on
30693 65 20 70 61 73 73 20 69 6e 64 65 78 20 30 20 77  e pass index 0 w
30694 69 6c 6c 20 72 65 66 65 72 65 6e 63 65 20 74 68  ill reference th
30695 65 20 61 63 63 75 6d 75 6c 61 74 65 64 20 64 6f  e accumulated do
30696 63 6c 69 73 74 2e 0a 20 20 20 20 20 20 2a 2f 0a  clist..      */.
30697 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28 26 64        dlrInit(&d
30698 6c 52 65 61 64 65 72 73 5b 30 5d 2c 20 44 4c 5f  lReaders[0], DL_
30699 44 45 46 41 55 4c 54 2c 0a 20 20 20 20 20 20 20  DEFAULT,.       
3069a 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
3069b 52 65 61 64 65 72 44 61 74 61 28 26 72 65 61 64  ReaderData(&read
3069c 65 72 73 5b 30 5d 29 2c 0a 20 20 20 20 20 20 20  ers[0]),.       
3069d 20 20 20 20 20 20 20 6f 70 74 4c 65 61 76 65 73         optLeaves
3069e 52 65 61 64 65 72 44 61 74 61 42 79 74 65 73 28  ReaderDataBytes(
3069f 26 72 65 61 64 65 72 73 5b 30 5d 29 29 3b 0a 20  &readers[0]));. 
306a0 20 20 20 20 20 69 52 65 61 64 65 72 20 3d 20 31       iReader = 1
306a1 3b 0a 0a 20 20 20 20 20 20 61 73 73 65 72 74 28  ;..      assert(
306a2 20 69 52 65 61 64 65 72 3c 69 20 29 3b 20 20 2f   iReader<i );  /
306a3 2a 20 4d 75 73 74 20 65 78 65 63 75 74 65 20 74  * Must execute t
306a4 68 65 20 6c 6f 6f 70 20 61 74 20 6c 65 61 73 74  he loop at least
306a5 20 6f 6e 63 65 2e 20 2a 2f 0a 20 20 20 20 20 20   once. */.      
306a6 77 68 69 6c 65 28 20 69 52 65 61 64 65 72 3c 69  while( iReader<i
306a7 20 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20 4d   ){.        /* M
306a8 65 72 67 65 20 31 36 20 69 6e 70 75 74 73 20 70  erge 16 inputs p
306a9 65 72 20 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20  er pass. */.    
306aa 20 20 20 20 66 6f 72 28 20 6e 52 65 61 64 65 72      for( nReader
306ab 73 3d 31 3b 20 69 52 65 61 64 65 72 3c 69 20 26  s=1; iReader<i &
306ac 26 20 6e 52 65 61 64 65 72 73 3c 4d 45 52 47 45  & nReaders<MERGE
306ad 5f 43 4f 55 4e 54 3b 0a 20 20 20 20 20 20 20 20  _COUNT;.        
306ae 20 20 20 20 20 69 52 65 61 64 65 72 2b 2b 2c 20       iReader++, 
306af 6e 52 65 61 64 65 72 73 2b 2b 20 29 7b 0a 20 20  nReaders++ ){.  
306b0 20 20 20 20 20 20 20 20 64 6c 72 49 6e 69 74 28          dlrInit(
306b1 26 64 6c 52 65 61 64 65 72 73 5b 6e 52 65 61 64  &dlReaders[nRead
306b2 65 72 73 5d 2c 20 44 4c 5f 44 45 46 41 55 4c 54  ers], DL_DEFAULT
306b3 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,.              
306b4 20 20 20 20 6f 70 74 4c 65 61 76 65 73 52 65 61      optLeavesRea
306b5 64 65 72 44 61 74 61 28 26 72 65 61 64 65 72 73  derData(&readers
306b6 5b 69 52 65 61 64 65 72 5d 29 2c 0a 20 20 20 20  [iReader]),.    
306b7 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70                op
306b8 74 4c 65 61 76 65 73 52 65 61 64 65 72 44 61 74  tLeavesReaderDat
306b9 61 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b  aBytes(&readers[
306ba 69 52 65 61 64 65 72 5d 29 29 3b 0a 20 20 20 20  iReader]));.    
306bb 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
306bc 2a 20 4d 65 72 67 65 20 64 6f 63 6c 69 73 74 73  * Merge doclists
306bd 20 61 6e 64 20 73 77 61 70 20 72 65 73 75 6c 74   and swap result
306be 20 69 6e 74 6f 20 61 63 63 75 6d 75 6c 61 74 6f   into accumulato
306bf 72 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20 64 61  r. */.        da
306c0 74 61 42 75 66 66 65 72 52 65 73 65 74 28 26 6d  taBufferReset(&m
306c1 65 72 67 65 64 29 3b 0a 20 20 20 20 20 20 20 20  erged);.        
306c2 64 6f 63 4c 69 73 74 4d 65 72 67 65 28 26 6d 65  docListMerge(&me
306c3 72 67 65 64 2c 20 64 6c 52 65 61 64 65 72 73 2c  rged, dlReaders,
306c4 20 6e 52 65 61 64 65 72 73 29 3b 0a 20 20 20 20   nReaders);.    
306c5 20 20 20 20 74 6d 70 20 3d 20 6d 65 72 67 65 64      tmp = merged
306c6 3b 0a 20 20 20 20 20 20 20 20 6d 65 72 67 65 64  ;.        merged
306c7 20 3d 20 64 6f 63 6c 69 73 74 3b 0a 20 20 20 20   = doclist;.    
306c8 20 20 20 20 64 6f 63 6c 69 73 74 20 3d 20 74 6d      doclist = tm
306c9 70 3b 0a 0a 20 20 20 20 20 20 20 20 77 68 69 6c  p;..        whil
306ca 65 28 20 6e 52 65 61 64 65 72 73 2d 2d 20 3e 20  e( nReaders-- > 
306cb 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 64  0 ){.          d
306cc 6c 72 44 65 73 74 72 6f 79 28 26 64 6c 52 65 61  lrDestroy(&dlRea
306cd 64 65 72 73 5b 6e 52 65 61 64 65 72 73 5d 29 3b  ders[nReaders]);
306ce 0a 20 20 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  .        }..    
306cf 20 20 20 20 2f 2a 20 41 63 63 75 6d 75 6c 61 74      /* Accumulat
306d0 65 64 20 64 6f 63 6c 69 73 74 20 74 6f 20 72 65  ed doclist to re
306d1 61 64 65 72 20 30 20 66 6f 72 20 6e 65 78 74 20  ader 0 for next 
306d2 70 61 73 73 2e 20 2a 2f 0a 20 20 20 20 20 20 20  pass. */.       
306d3 20 64 6c 72 49 6e 69 74 28 26 64 6c 52 65 61 64   dlrInit(&dlRead
306d4 65 72 73 5b 30 5d 2c 20 44 4c 5f 44 45 46 41 55  ers[0], DL_DEFAU
306d5 4c 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74  LT, doclist.pDat
306d6 61 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61  a, doclist.nData
306d7 29 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20 20  );.      }..    
306d8 20 20 2f 2a 20 44 65 73 74 72 6f 79 20 72 65 61    /* Destroy rea
306d9 64 65 72 20 74 68 61 74 20 77 61 73 20 6c 65 66  der that was lef
306da 74 20 69 6e 20 74 68 65 20 70 69 70 65 6c 69 6e  t in the pipelin
306db 65 2e 20 2a 2f 0a 20 20 20 20 20 20 64 6c 72 44  e. */.      dlrD
306dc 65 73 74 72 6f 79 28 26 64 6c 52 65 61 64 65 72  estroy(&dlReader
306dd 73 5b 30 5d 29 3b 0a 0a 20 20 20 20 20 20 2f 2a  s[0]);..      /*
306de 20 54 72 69 6d 20 64 65 6c 65 74 69 6f 6e 73 20   Trim deletions 
306df 66 72 6f 6d 20 74 68 65 20 64 6f 63 6c 69 73 74  from the doclist
306e0 2e 20 2a 2f 0a 20 20 20 20 20 20 64 61 74 61 42  . */.      dataB
306e1 75 66 66 65 72 52 65 73 65 74 28 26 6d 65 72 67  ufferReset(&merg
306e2 65 64 29 3b 0a 20 20 20 20 20 20 64 6f 63 4c 69  ed);.      docLi
306e3 73 74 54 72 69 6d 28 44 4c 5f 44 45 46 41 55 4c  stTrim(DL_DEFAUL
306e4 54 2c 20 64 6f 63 6c 69 73 74 2e 70 44 61 74 61  T, doclist.pData
306e5 2c 20 64 6f 63 6c 69 73 74 2e 6e 44 61 74 61 2c  , doclist.nData,
306e6 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  .               
306e7 20 20 20 2d 31 2c 20 44 4c 5f 44 45 46 41 55 4c     -1, DL_DEFAUL
306e8 54 2c 20 26 6d 65 72 67 65 64 29 3b 0a 20 20 20  T, &merged);.   
306e9 20 7d 0a 0a 20 20 20 20 2f 2a 20 4f 6e 6c 79 20   }..    /* Only 
306ea 70 61 73 73 20 64 6f 63 6c 69 73 74 73 20 77 69  pass doclists wi
306eb 74 68 20 68 69 74 73 20 28 73 6b 69 70 20 69 66  th hits (skip if
306ec 20 61 6c 6c 20 68 69 74 73 20 64 65 6c 65 74 65   all hits delete
306ed 64 29 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 6d  d). */.    if( m
306ee 65 72 67 65 64 2e 6e 44 61 74 61 3e 30 20 29 7b  erged.nData>0 ){
306ef 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65 61 66  .      rc = leaf
306f0 57 72 69 74 65 72 53 74 65 70 28 76 2c 20 70 57  WriterStep(v, pW
306f1 72 69 74 65 72 2c 0a 20 20 20 20 20 20 20 20 20  riter,.         
306f2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
306f3 20 6f 70 74 4c 65 61 76 65 73 52 65 61 64 65 72   optLeavesReader
306f4 54 65 72 6d 28 26 72 65 61 64 65 72 73 5b 30 5d  Term(&readers[0]
306f5 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  ),.             
306f6 20 20 20 20 20 20 20 20 20 20 20 20 20 6f 70 74               opt
306f7 4c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  LeavesReaderTerm
306f8 42 79 74 65 73 28 26 72 65 61 64 65 72 73 5b 30  Bytes(&readers[0
306f9 5d 29 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ]),.            
306fa 20 20 20 20 20 20 20 20 20 20 20 20 20 20 6d 65                me
306fb 72 67 65 64 2e 70 44 61 74 61 2c 20 6d 65 72 67  rged.pData, merg
306fc 65 64 2e 6e 44 61 74 61 29 3b 0a 20 20 20 20 20  ed.nData);.     
306fd 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
306fe 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20  OK ) goto err;. 
306ff 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 53 74 65     }..    /* Ste
30700 70 20 6d 65 72 67 65 64 20 72 65 61 64 65 72 73  p merged readers
30701 20 74 6f 20 6e 65 78 74 20 74 65 72 6d 20 61 6e   to next term an
30702 64 20 72 65 6f 72 64 65 72 2e 20 2a 2f 0a 20 20  d reorder. */.  
30703 20 20 77 68 69 6c 65 28 20 69 2d 2d 20 3e 20 30    while( i-- > 0
30704 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6f   ){.      rc = o
30705 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 53 74  ptLeavesReaderSt
30706 65 70 28 76 2c 20 26 72 65 61 64 65 72 73 5b 69  ep(v, &readers[i
30707 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63  ]);.      if( rc
30708 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
30709 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 20 20 6f  to err;..      o
3070a 70 74 4c 65 61 76 65 73 52 65 61 64 65 72 52 65  ptLeavesReaderRe
3070b 6f 72 64 65 72 28 26 72 65 61 64 65 72 73 5b 69  order(&readers[i
3070c 5d 2c 20 6e 52 65 61 64 65 72 73 2d 69 29 3b 0a  ], nReaders-i);.
3070d 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 65 72 72 3a      }.  }.. err:
3070e 0a 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73  .  dataBufferDes
3070f 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a  troy(&doclist);.
30710 20 20 64 61 74 61 42 75 66 66 65 72 44 65 73 74    dataBufferDest
30711 72 6f 79 28 26 6d 65 72 67 65 64 29 3b 0a 20 20  roy(&merged);.  
30712 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
30713 20 49 6d 70 6c 65 6d 65 6e 74 20 6f 70 74 69 6d   Implement optim
30714 69 7a 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 66  ize() function f
30715 6f 72 20 46 54 53 33 2e 20 20 6f 70 74 69 6d 69  or FTS3.  optimi
30716 7a 65 28 74 29 20 6d 65 72 67 65 73 20 61 6c 6c  ze(t) merges all
30717 0a 2a 2a 20 73 65 67 6d 65 6e 74 73 20 69 6e 20  .** segments in 
30718 74 68 65 20 66 74 73 20 69 6e 64 65 78 20 69 6e  the fts index in
30719 74 6f 20 61 20 73 69 6e 67 6c 65 20 73 65 67 6d  to a single segm
3071a 65 6e 74 2e 20 20 27 74 27 20 69 73 20 74 68 65  ent.  't' is the
3071b 20 6d 61 67 69 63 0a 2a 2a 20 74 61 62 6c 65 2d   magic.** table-
3071c 6e 61 6d 65 64 20 63 6f 6c 75 6d 6e 2e 0a 2a 2f  named column..*/
3071d 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6f 70 74  .static void opt
3071e 69 6d 69 7a 65 46 75 6e 63 28 73 71 6c 69 74 65  imizeFunc(sqlite
3071f 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74  3_context *pCont
30720 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20 20 20  ext,.           
30721 20 20 20 20 20 20 20 20 20 20 20 20 20 20 69 6e                in
30722 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
30723 76 61 6c 75 65 20 2a 2a 61 72 67 76 29 7b 0a 20  value **argv){. 
30724 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f 72   fulltext_cursor
30725 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66 28   *pCursor;.  if(
30726 20 61 72 67 63 3e 31 20 29 7b 0a 20 20 20 20 73   argc>1 ){.    s
30727 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72  qlite3_result_er
30728 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 65  ror(pContext, "e
30729 78 63 65 73 73 20 61 72 67 75 6d 65 6e 74 73 20  xcess arguments 
3072a 74 6f 20 6f 70 74 69 6d 69 7a 65 28 29 22 2c 2d  to optimize()",-
3072b 31 29 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20  1);.  }else if( 
3072c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 79  sqlite3_value_ty
3072d 70 65 28 61 72 67 76 5b 30 5d 29 21 3d 53 51 4c  pe(argv[0])!=SQL
3072e 49 54 45 5f 42 4c 4f 42 20 7c 7c 0a 20 20 20 20  ITE_BLOB ||.    
3072f 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
30730 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76  value_bytes(argv
30731 5b 30 5d 29 21 3d 73 69 7a 65 6f 66 28 70 43 75  [0])!=sizeof(pCu
30732 72 73 6f 72 29 20 29 7b 0a 20 20 20 20 73 71 6c  rsor) ){.    sql
30733 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
30734 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 69 6c 6c  r(pContext, "ill
30735 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75 6d  egal first argum
30736 65 6e 74 20 74 6f 20 6f 70 74 69 6d 69 7a 65 22  ent to optimize"
30737 2c 2d 31 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  ,-1);.  }else{. 
30738 20 20 20 66 75 6c 6c 74 65 78 74 5f 76 74 61 62     fulltext_vtab
30739 20 2a 76 3b 0a 20 20 20 20 69 6e 74 20 69 2c 20   *v;.    int i, 
3073a 72 63 2c 20 69 4d 61 78 4c 65 76 65 6c 3b 0a 20  rc, iMaxLevel;. 
3073b 20 20 20 4f 70 74 4c 65 61 76 65 73 52 65 61 64     OptLeavesRead
3073c 65 72 20 2a 72 65 61 64 65 72 73 3b 0a 20 20 20  er *readers;.   
3073d 20 69 6e 74 20 6e 52 65 61 64 65 72 73 3b 0a 20   int nReaders;. 
3073e 20 20 20 4c 65 61 66 57 72 69 74 65 72 20 77 72     LeafWriter wr
3073f 69 74 65 72 3b 0a 20 20 20 20 73 71 6c 69 74 65  iter;.    sqlite
30740 33 5f 73 74 6d 74 20 2a 73 3b 0a 0a 20 20 20 20  3_stmt *s;..    
30741 6d 65 6d 63 70 79 28 26 70 43 75 72 73 6f 72 2c  memcpy(&pCursor,
30742 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62   sqlite3_value_b
30743 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c 20 73 69  lob(argv[0]), si
30744 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 29 3b 0a  zeof(pCursor));.
30745 20 20 20 20 76 20 3d 20 63 75 72 73 6f 72 5f 76      v = cursor_v
30746 74 61 62 28 70 43 75 72 73 6f 72 29 3b 0a 0a 20  tab(pCursor);.. 
30747 20 20 20 2f 2a 20 46 6c 75 73 68 20 61 6e 79 20     /* Flush any 
30748 62 75 66 66 65 72 65 64 20 75 70 64 61 74 65 73  buffered updates
30749 20 62 65 66 6f 72 65 20 6f 70 74 69 6d 69 7a 69   before optimizi
3074a 6e 67 2e 20 2a 2f 0a 20 20 20 20 72 63 20 3d 20  ng. */.    rc = 
3074b 66 6c 75 73 68 50 65 6e 64 69 6e 67 54 65 72 6d  flushPendingTerm
3074c 73 28 76 29 3b 0a 20 20 20 20 69 66 28 20 72 63  s(v);.    if( rc
3074d 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f  !=SQLITE_OK ) go
3074e 74 6f 20 65 72 72 3b 0a 0a 20 20 20 20 72 63 20  to err;..    rc 
3074f 3d 20 73 65 67 64 69 72 5f 63 6f 75 6e 74 28 76  = segdir_count(v
30750 2c 20 26 6e 52 65 61 64 65 72 73 2c 20 26 69 4d  , &nReaders, &iM
30751 61 78 4c 65 76 65 6c 29 3b 0a 20 20 20 20 69 66  axLevel);.    if
30752 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
30753 29 20 67 6f 74 6f 20 65 72 72 3b 0a 20 20 20 20  ) goto err;.    
30754 69 66 28 20 6e 52 65 61 64 65 72 73 3d 3d 30 20  if( nReaders==0 
30755 7c 7c 20 6e 52 65 61 64 65 72 73 3d 3d 31 20 29  || nReaders==1 )
30756 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
30757 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e  result_text(pCon
30758 74 65 78 74 2c 20 22 49 6e 64 65 78 20 61 6c 72  text, "Index alr
30759 65 61 64 79 20 6f 70 74 69 6d 61 6c 22 2c 20 2d  eady optimal", -
3075a 31 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20  1,.             
3075b 20 20 20 20 20 20 20 20 20 20 20 20 20 53 51 4c               SQL
3075c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
3075d 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d     return;.    }
3075e 0a 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 5f 67  ..    rc = sql_g
3075f 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c 20  et_statement(v, 
30760 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 41 4c  SEGDIR_SELECT_AL
30761 4c 5f 53 54 4d 54 2c 20 26 73 29 3b 0a 20 20 20  L_STMT, &s);.   
30762 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
30763 4f 4b 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a  OK ) goto err;..
30764 20 20 20 20 72 65 61 64 65 72 73 20 3d 20 73 71      readers = sq
30765 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 52 65  lite3_malloc(nRe
30766 61 64 65 72 73 2a 73 69 7a 65 6f 66 28 72 65 61  aders*sizeof(rea
30767 64 65 72 73 5b 30 5d 29 29 3b 0a 20 20 20 20 69  ders[0]));.    i
30768 66 28 20 72 65 61 64 65 72 73 3d 3d 4e 55 4c 4c  f( readers==NULL
30769 20 29 20 67 6f 74 6f 20 65 72 72 3b 0a 0a 20 20   ) goto err;..  
3076a 20 20 2f 2a 20 4e 6f 74 65 20 74 68 61 74 20 74    /* Note that t
3076b 68 65 72 65 20 77 69 6c 6c 20 61 6c 72 65 61 64  here will alread
3076c 79 20 62 65 20 61 20 73 65 67 6d 65 6e 74 20 61  y be a segment a
3076d 74 20 74 68 69 73 20 70 6f 73 69 74 69 6f 6e 0a  t this position.
3076e 20 20 20 20 2a 2a 20 75 6e 74 69 6c 20 77 65 20      ** until we 
3076f 63 61 6c 6c 20 73 65 67 64 69 72 5f 64 65 6c 65  call segdir_dele
30770 74 65 28 29 20 6f 6e 20 69 4d 61 78 4c 65 76 65  te() on iMaxLeve
30771 6c 2e 0a 20 20 20 20 2a 2f 0a 20 20 20 20 6c 65  l..    */.    le
30772 61 66 57 72 69 74 65 72 49 6e 69 74 28 69 4d 61  afWriterInit(iMa
30773 78 4c 65 76 65 6c 2c 20 30 2c 20 26 77 72 69 74  xLevel, 0, &writ
30774 65 72 29 3b 0a 0a 20 20 20 20 69 20 3d 20 30 3b  er);..    i = 0;
30775 0a 20 20 20 20 77 68 69 6c 65 28 20 28 72 63 20  .    while( (rc 
30776 3d 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 73  = sqlite3_step(s
30777 29 29 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29  ))==SQLITE_ROW )
30778 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 5f 69  {.      sqlite_i
30779 6e 74 36 34 20 69 53 74 61 72 74 20 3d 20 73 71  nt64 iStart = sq
3077a 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e 74  lite3_column_int
3077b 36 34 28 73 2c 20 30 29 3b 0a 20 20 20 20 20 20  64(s, 0);.      
3077c 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 69 45 6e  sqlite_int64 iEn
3077d 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
3077e 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
3077f 20 20 20 20 20 20 63 6f 6e 73 74 20 63 68 61 72        const char
30780 20 2a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71   *pRootData = sq
30781 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
30782 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 69  b(s, 2);.      i
30783 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73  nt nRootData = s
30784 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
30785 74 65 73 28 73 2c 20 32 29 3b 0a 0a 20 20 20 20  tes(s, 2);..    
30786 20 20 61 73 73 65 72 74 28 20 69 3c 6e 52 65 61    assert( i<nRea
30787 64 65 72 73 20 29 3b 0a 20 20 20 20 20 20 72 63  ders );.      rc
30788 20 3d 20 6c 65 61 76 65 73 52 65 61 64 65 72 49   = leavesReaderI
30789 6e 69 74 28 76 2c 20 2d 31 2c 20 69 53 74 61 72  nit(v, -1, iStar
3078a 74 2c 20 69 45 6e 64 2c 20 70 52 6f 6f 74 44 61  t, iEnd, pRootDa
3078b 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61 2c 0a 20  ta, nRootData,. 
3078c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3078d 20 20 20 20 20 20 20 20 20 20 20 26 72 65 61 64             &read
3078e 65 72 73 5b 69 5d 2e 72 65 61 64 65 72 29 3b 0a  ers[i].reader);.
3078f 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53 51        if( rc!=SQ
30790 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65 61 6b 3b  LITE_OK ) break;
30791 0a 0a 20 20 20 20 20 20 72 65 61 64 65 72 73 5b  ..      readers[
30792 69 5d 2e 73 65 67 6d 65 6e 74 20 3d 20 69 3b 0a  i].segment = i;.
30793 20 20 20 20 20 20 69 2b 2b 3b 0a 20 20 20 20 7d        i++;.    }
30794 0a 0a 20 20 20 20 2f 2a 20 49 66 20 77 65 20 6d  ..    /* If we m
30795 61 6e 61 67 65 64 20 74 6f 20 73 75 63 63 65 73  anaged to succes
30796 66 75 6c 6c 79 20 72 65 61 64 20 74 68 65 6d 20  fully read them 
30797 61 6c 6c 2c 20 6f 70 74 69 6d 69 7a 65 20 74 68  all, optimize th
30798 65 6d 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20 72  em. */.    if( r
30799 63 3d 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c==SQLITE_DONE )
3079a 7b 0a 20 20 20 20 20 20 61 73 73 65 72 74 28 20  {.      assert( 
3079b 69 3d 3d 6e 52 65 61 64 65 72 73 20 29 3b 0a 20  i==nReaders );. 
3079c 20 20 20 20 20 72 63 20 3d 20 6f 70 74 69 6d 69       rc = optimi
3079d 7a 65 49 6e 74 65 72 6e 61 6c 28 76 2c 20 72 65  zeInternal(v, re
3079e 61 64 65 72 73 2c 20 6e 52 65 61 64 65 72 73 2c  aders, nReaders,
3079f 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20 7d   &writer);.    }
307a0 0a 0a 20 20 20 20 77 68 69 6c 65 28 20 69 2d 2d  ..    while( i--
307a1 20 3e 20 30 20 29 7b 0a 20 20 20 20 20 20 6c 65   > 0 ){.      le
307a2 61 76 65 73 52 65 61 64 65 72 44 65 73 74 72 6f  avesReaderDestro
307a3 79 28 26 72 65 61 64 65 72 73 5b 69 5d 2e 72 65  y(&readers[i].re
307a4 61 64 65 72 29 3b 0a 20 20 20 20 7d 0a 20 20 20  ader);.    }.   
307a5 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 72 65   sqlite3_free(re
307a6 61 64 65 72 73 29 3b 0a 0a 20 20 20 20 2f 2a 20  aders);..    /* 
307a7 49 66 20 77 65 27 76 65 20 73 75 63 63 65 73 73  If we've success
307a8 66 75 6c 6c 79 20 67 6f 74 74 65 6e 20 74 6f 20  fully gotten to 
307a9 68 65 72 65 2c 20 64 65 6c 65 74 65 20 74 68 65  here, delete the
307aa 20 6f 6c 64 20 73 65 67 6d 65 6e 74 73 0a 20 20   old segments.  
307ab 20 20 2a 2a 20 61 6e 64 20 66 6c 75 73 68 20 74    ** and flush t
307ac 68 65 20 69 6e 74 65 72 69 6f 72 20 73 74 72 75  he interior stru
307ad 63 74 75 72 65 20 6f 66 20 74 68 65 20 6e 65 77  cture of the new
307ae 20 73 65 67 6d 65 6e 74 2e 0a 20 20 20 20 2a 2f   segment..    */
307af 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
307b0 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
307b1 66 6f 72 28 20 69 3d 30 3b 20 69 3c 3d 69 4d 61  for( i=0; i<=iMa
307b2 78 4c 65 76 65 6c 3b 20 69 2b 2b 20 29 7b 0a 20  xLevel; i++ ){. 
307b3 20 20 20 20 20 20 20 72 63 20 3d 20 73 65 67 64         rc = segd
307b4 69 72 5f 64 65 6c 65 74 65 28 76 2c 20 69 29 3b  ir_delete(v, i);
307b5 0a 20 20 20 20 20 20 20 20 69 66 28 20 72 63 21  .        if( rc!
307b6 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 20 62 72 65  =SQLITE_OK ) bre
307b7 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 0a 20 20 20  ak;.      }..   
307b8 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54     if( rc==SQLIT
307b9 45 5f 4f 4b 20 29 20 72 63 20 3d 20 6c 65 61 66  E_OK ) rc = leaf
307ba 57 72 69 74 65 72 46 69 6e 61 6c 69 7a 65 28 76  WriterFinalize(v
307bb 2c 20 26 77 72 69 74 65 72 29 3b 0a 20 20 20 20  , &writer);.    
307bc 7d 0a 0a 20 20 20 20 6c 65 61 66 57 72 69 74 65  }..    leafWrite
307bd 72 44 65 73 74 72 6f 79 28 26 77 72 69 74 65 72  rDestroy(&writer
307be 29 3b 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d  );..    if( rc!=
307bf 53 51 4c 49 54 45 5f 4f 4b 20 29 20 67 6f 74 6f  SQLITE_OK ) goto
307c0 20 65 72 72 3b 0a 0a 20 20 20 20 73 71 6c 69 74   err;..    sqlit
307c1 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28 70  e3_result_text(p
307c2 43 6f 6e 74 65 78 74 2c 20 22 49 6e 64 65 78 20  Context, "Index 
307c3 6f 70 74 69 6d 69 7a 65 64 22 2c 20 2d 31 2c 20  optimized", -1, 
307c4 53 51 4c 49 54 45 5f 53 54 41 54 49 43 29 3b 0a  SQLITE_STATIC);.
307c5 20 20 20 20 72 65 74 75 72 6e 3b 0a 0a 20 20 20      return;..   
307c6 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29 3a   /* TODO(shess):
307c7 20 45 72 72 6f 72 2d 68 61 6e 64 6c 69 6e 67 20   Error-handling 
307c8 6e 65 65 64 73 20 74 6f 20 62 65 20 69 6d 70 72  needs to be impr
307c9 6f 76 65 64 20 61 6c 6f 6e 67 20 74 68 65 0a 20  oved along the. 
307ca 20 20 20 2a 2a 20 6c 69 6e 65 73 20 6f 66 20 74     ** lines of t
307cb 68 65 20 64 75 6d 70 5f 20 66 75 6e 63 74 69 6f  he dump_ functio
307cc 6e 73 2e 0a 20 20 20 20 2a 2f 0a 20 65 72 72 3a  ns..    */. err:
307cd 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 63 68 61  .    {.      cha
307ce 72 20 62 75 66 5b 35 31 32 5d 3b 0a 20 20 20 20  r buf[512];.    
307cf 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
307d0 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20  tf(sizeof(buf), 
307d1 62 75 66 2c 20 22 45 72 72 6f 72 20 69 6e 20 6f  buf, "Error in o
307d2 70 74 69 6d 69 7a 65 3a 20 25 73 22 2c 0a 20 20  ptimize: %s",.  
307d3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307d4 20 20 20 20 20 73 71 6c 69 74 65 33 5f 65 72 72       sqlite3_err
307d5 6d 73 67 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  msg(sqlite3_cont
307d6 65 78 74 5f 64 62 5f 68 61 6e 64 6c 65 28 70 43  ext_db_handle(pC
307d7 6f 6e 74 65 78 74 29 29 29 3b 0a 20 20 20 20 20  ontext)));.     
307d8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
307d9 65 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  error(pContext, 
307da 62 75 66 2c 20 2d 31 29 3b 0a 20 20 20 20 7d 0a  buf, -1);.    }.
307db 20 20 7d 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51    }.}..#ifdef SQ
307dc 4c 49 54 45 5f 54 45 53 54 0a 2f 2a 20 47 65 6e  LITE_TEST./* Gen
307dd 65 72 61 74 65 20 61 6e 20 65 72 72 6f 72 20 6f  erate an error o
307de 66 20 74 68 65 20 66 6f 72 6d 20 22 3c 70 72 65  f the form "<pre
307df 66 69 78 3e 3a 20 3c 6d 73 67 3e 22 2e 20 20 49  fix>: <msg>".  I
307e0 66 20 6d 73 67 20 69 73 20 4e 55 4c 4c 2c 0a 2a  f msg is NULL,.*
307e1 2a 20 70 75 6c 6c 20 74 68 65 20 65 72 72 6f 72  * pull the error
307e2 20 66 72 6f 6d 20 74 68 65 20 63 6f 6e 74 65 78   from the contex
307e3 74 27 73 20 64 62 20 68 61 6e 64 6c 65 2e 0a 2a  t's db handle..*
307e4 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 67 65  /.static void ge
307e5 6e 65 72 61 74 65 45 72 72 6f 72 28 73 71 6c 69  nerateError(sqli
307e6 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f  te3_context *pCo
307e7 6e 74 65 78 74 2c 0a 20 20 20 20 20 20 20 20 20  ntext,.         
307e8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
307e9 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 72 65   const char *pre
307ea 66 69 78 2c 20 63 6f 6e 73 74 20 63 68 61 72 20  fix, const char 
307eb 2a 6d 73 67 29 7b 0a 20 20 63 68 61 72 20 62 75  *msg){.  char bu
307ec 66 5b 35 31 32 5d 3b 0a 20 20 69 66 28 20 6d 73  f[512];.  if( ms
307ed 67 3d 3d 4e 55 4c 4c 20 29 20 6d 73 67 20 3d 20  g==NULL ) msg = 
307ee 73 71 6c 69 74 65 33 5f 65 72 72 6d 73 67 28 73  sqlite3_errmsg(s
307ef 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 5f 64  qlite3_context_d
307f0 62 5f 68 61 6e 64 6c 65 28 70 43 6f 6e 74 65 78  b_handle(pContex
307f1 74 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73  t));.  sqlite3_s
307f2 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62  nprintf(sizeof(b
307f3 75 66 29 2c 20 62 75 66 2c 20 22 25 73 3a 20 25  uf), buf, "%s: %
307f4 73 22 2c 20 70 72 65 66 69 78 2c 20 6d 73 67 29  s", prefix, msg)
307f5 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
307f6 6c 74 5f 65 72 72 6f 72 28 70 43 6f 6e 74 65 78  lt_error(pContex
307f7 74 2c 20 62 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a  t, buf, -1);.}..
307f8 2f 2a 20 48 65 6c 70 65 72 20 66 75 6e 63 74 69  /* Helper functi
307f9 6f 6e 20 74 6f 20 63 6f 6c 6c 65 63 74 20 74 68  on to collect th
307fa 65 20 73 65 74 20 6f 66 20 74 65 72 6d 73 20 69  e set of terms i
307fb 6e 20 74 68 65 20 73 65 67 6d 65 6e 74 20 69 6e  n the segment in
307fc 74 6f 0a 2a 2a 20 70 54 65 72 6d 73 2e 20 20 54  to.** pTerms.  T
307fd 68 65 20 73 65 67 6d 65 6e 74 20 69 73 20 64 65  he segment is de
307fe 66 69 6e 65 64 20 62 79 20 74 68 65 20 6c 65 61  fined by the lea
307ff 66 20 6e 6f 64 65 73 20 62 65 74 77 65 65 6e 0a  f nodes between.
30800 2a 2a 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  ** iStartBlockid
30801 20 61 6e 64 20 69 45 6e 64 42 6c 6f 63 6b 69 64   and iEndBlockid
30802 2c 20 69 6e 63 6c 75 73 69 76 65 2c 20 6f 72 20  , inclusive, or 
30803 62 79 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  by the contents 
30804 6f 66 0a 2a 2a 20 70 52 6f 6f 74 44 61 74 61 20  of.** pRootData 
30805 69 66 20 69 53 74 61 72 74 42 6c 6f 63 6b 69 64  if iStartBlockid
30806 20 69 73 20 30 20 28 69 6e 20 77 68 69 63 68 20   is 0 (in which 
30807 63 61 73 65 20 74 68 65 20 65 6e 74 69 72 65 20  case the entire 
30808 73 65 67 6d 65 6e 74 0a 2a 2a 20 66 69 74 20 69  segment.** fit i
30809 6e 20 61 20 6c 65 61 66 29 2e 0a 2a 2f 0a 73 74  n a leaf)..*/.st
3080a 61 74 69 63 20 69 6e 74 20 63 6f 6c 6c 65 63 74  atic int collect
3080b 53 65 67 6d 65 6e 74 54 65 72 6d 73 28 66 75 6c  SegmentTerms(ful
3080c 6c 74 65 78 74 5f 76 74 61 62 20 2a 76 2c 20 73  ltext_vtab *v, s
3080d 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 2c 0a  qlite3_stmt *s,.
3080e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3080f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 66                 f
30810 74 73 33 48 61 73 68 20 2a 70 54 65 72 6d 73 29  ts3Hash *pTerms)
30811 7b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  {.  const sqlite
30812 5f 69 6e 74 36 34 20 69 53 74 61 72 74 42 6c 6f  _int64 iStartBlo
30813 63 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63  ckid = sqlite3_c
30814 6f 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 30  olumn_int64(s, 0
30815 29 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  );.  const sqlit
30816 65 5f 69 6e 74 36 34 20 69 45 6e 64 42 6c 6f 63  e_int64 iEndBloc
30817 6b 69 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f  kid = sqlite3_co
30818 6c 75 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29  lumn_int64(s, 1)
30819 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a  ;.  const char *
3081a 70 52 6f 6f 74 44 61 74 61 20 3d 20 73 71 6c 69  pRootData = sqli
3081b 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
3081c 73 2c 20 32 29 3b 0a 20 20 63 6f 6e 73 74 20 69  s, 2);.  const i
3081d 6e 74 20 6e 52 6f 6f 74 44 61 74 61 20 3d 20 73  nt nRootData = s
3081e 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 79  qlite3_column_by
3081f 74 65 73 28 73 2c 20 32 29 3b 0a 20 20 4c 65 61  tes(s, 2);.  Lea
30820 76 65 73 52 65 61 64 65 72 20 72 65 61 64 65 72  vesReader reader
30821 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20 6c 65 61  ;.  int rc = lea
30822 76 65 73 52 65 61 64 65 72 49 6e 69 74 28 76 2c  vesReaderInit(v,
30823 20 30 2c 20 69 53 74 61 72 74 42 6c 6f 63 6b 69   0, iStartBlocki
30824 64 2c 20 69 45 6e 64 42 6c 6f 63 6b 69 64 2c 0a  d, iEndBlockid,.
30825 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30826 20 20 20 20 20 20 20 20 20 20 20 20 70 52 6f 6f              pRoo
30827 74 44 61 74 61 2c 20 6e 52 6f 6f 74 44 61 74 61  tData, nRootData
30828 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20 69 66  , &reader);.  if
30829 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3082a 29 20 72 65 74 75 72 6e 20 72 63 3b 0a 0a 20 20  ) return rc;..  
3082b 77 68 69 6c 65 28 20 72 63 3d 3d 53 51 4c 49 54  while( rc==SQLIT
3082c 45 5f 4f 4b 20 26 26 20 21 6c 65 61 76 65 73 52  E_OK && !leavesR
3082d 65 61 64 65 72 41 74 45 6e 64 28 26 72 65 61 64  eaderAtEnd(&read
3082e 65 72 29 20 29 7b 0a 20 20 20 20 63 6f 6e 73 74  er) ){.    const
3082f 20 63 68 61 72 20 2a 70 54 65 72 6d 20 3d 20 6c   char *pTerm = l
30830 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d 28  eavesReaderTerm(
30831 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20 63 6f  &reader);.    co
30832 6e 73 74 20 69 6e 74 20 6e 54 65 72 6d 20 3d 20  nst int nTerm = 
30833 6c 65 61 76 65 73 52 65 61 64 65 72 54 65 72 6d  leavesReaderTerm
30834 42 79 74 65 73 28 26 72 65 61 64 65 72 29 3b 0a  Bytes(&reader);.
30835 20 20 20 20 76 6f 69 64 20 2a 6f 6c 64 56 61 6c      void *oldVal
30836 75 65 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33  ue = sqlite3Fts3
30837 48 61 73 68 46 69 6e 64 28 70 54 65 72 6d 73 2c  HashFind(pTerms,
30838 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d 29 3b 0a   pTerm, nTerm);.
30839 20 20 20 20 76 6f 69 64 20 2a 6e 65 77 56 61 6c      void *newVal
3083a 75 65 20 3d 20 28 76 6f 69 64 20 2a 29 28 28 63  ue = (void *)((c
3083b 68 61 72 20 2a 29 6f 6c 64 56 61 6c 75 65 2b 31  har *)oldValue+1
3083c 29 3b 0a 0a 20 20 20 20 2f 2a 20 46 72 6f 6d 20  );..    /* From 
3083d 74 68 65 20 63 6f 6d 6d 65 6e 74 20 62 65 66 6f  the comment befo
3083e 72 65 20 73 71 6c 69 74 65 33 46 74 73 33 48 61  re sqlite3Fts3Ha
3083f 73 68 49 6e 73 65 72 74 20 69 6e 20 66 74 73 33  shInsert in fts3
30840 5f 68 61 73 68 2e 63 2c 0a 20 20 20 20 2a 2a 20  _hash.c,.    ** 
30841 74 68 65 20 64 61 74 61 20 76 61 6c 75 65 20 70  the data value p
30842 61 73 73 65 64 20 69 73 20 72 65 74 75 72 6e 65  assed is returne
30843 64 20 69 6e 20 63 61 73 65 20 6f 66 20 6d 61 6c  d in case of mal
30844 6c 6f 63 20 66 61 69 6c 75 72 65 2e 0a 20 20 20  loc failure..   
30845 20 2a 2f 0a 20 20 20 20 69 66 28 20 6e 65 77 56   */.    if( newV
30846 61 6c 75 65 3d 3d 73 71 6c 69 74 65 33 46 74 73  alue==sqlite3Fts
30847 33 48 61 73 68 49 6e 73 65 72 74 28 70 54 65 72  3HashInsert(pTer
30848 6d 73 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  ms, pTerm, nTerm
30849 2c 20 6e 65 77 56 61 6c 75 65 29 20 29 7b 0a 20  , newValue) ){. 
3084a 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45       rc = SQLITE
3084b 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 65 6c 73  _NOMEM;.    }els
3084c 65 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 6c 65  e{.      rc = le
3084d 61 76 65 73 52 65 61 64 65 72 53 74 65 70 28 76  avesReaderStep(v
3084e 2c 20 26 72 65 61 64 65 72 29 3b 0a 20 20 20 20  , &reader);.    
3084f 7d 0a 20 20 7d 0a 0a 20 20 6c 65 61 76 65 73 52  }.  }..  leavesR
30850 65 61 64 65 72 44 65 73 74 72 6f 79 28 26 72 65  eaderDestroy(&re
30851 61 64 65 72 29 3b 0a 20 20 72 65 74 75 72 6e 20  ader);.  return 
30852 72 63 3b 0a 7d 0a 0a 2f 2a 20 48 65 6c 70 65 72  rc;.}../* Helper
30853 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 62 75 69   function to bui
30854 6c 64 20 74 68 65 20 72 65 73 75 6c 74 20 73 74  ld the result st
30855 72 69 6e 67 20 66 6f 72 20 64 75 6d 70 5f 74 65  ring for dump_te
30856 72 6d 73 28 29 2e 20 2a 2f 0a 73 74 61 74 69 63  rms(). */.static
30857 20 69 6e 74 20 67 65 6e 65 72 61 74 65 54 65 72   int generateTer
30858 6d 73 52 65 73 75 6c 74 28 73 71 6c 69 74 65 33  msResult(sqlite3
30859 5f 63 6f 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65  _context *pConte
3085a 78 74 2c 20 66 74 73 33 48 61 73 68 20 2a 70 54  xt, fts3Hash *pT
3085b 65 72 6d 73 29 7b 0a 20 20 69 6e 74 20 69 54 65  erms){.  int iTe
3085c 72 6d 2c 20 6e 54 65 72 6d 73 2c 20 6e 52 65 73  rm, nTerms, nRes
3085d 75 6c 74 42 79 74 65 73 2c 20 69 42 79 74 65 3b  ultBytes, iByte;
3085e 0a 20 20 63 68 61 72 20 2a 72 65 73 75 6c 74 3b  .  char *result;
3085f 0a 20 20 54 65 72 6d 44 61 74 61 20 2a 70 44 61  .  TermData *pDa
30860 74 61 3b 0a 20 20 66 74 73 33 48 61 73 68 45 6c  ta;.  fts3HashEl
30861 65 6d 20 2a 65 3b 0a 0a 20 20 2f 2a 20 49 74 65  em *e;..  /* Ite
30862 72 61 74 65 20 70 54 65 72 6d 73 20 74 6f 20 67  rate pTerms to g
30863 65 6e 65 72 61 74 65 20 61 6e 20 61 72 72 61 79  enerate an array
30864 20 6f 66 20 74 65 72 6d 73 20 69 6e 20 70 44 61   of terms in pDa
30865 74 61 20 66 6f 72 0a 20 20 2a 2a 20 73 6f 72 74  ta for.  ** sort
30866 69 6e 67 2e 0a 20 20 2a 2f 0a 20 20 6e 54 65 72  ing..  */.  nTer
30867 6d 73 20 3d 20 66 74 73 33 48 61 73 68 43 6f 75  ms = fts3HashCou
30868 6e 74 28 70 54 65 72 6d 73 29 3b 0a 20 20 61 73  nt(pTerms);.  as
30869 73 65 72 74 28 20 6e 54 65 72 6d 73 3e 30 20 29  sert( nTerms>0 )
3086a 3b 0a 20 20 70 44 61 74 61 20 3d 20 73 71 6c 69  ;.  pData = sqli
3086b 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 54 65 72 6d  te3_malloc(nTerm
3086c 73 2a 73 69 7a 65 6f 66 28 54 65 72 6d 44 61 74  s*sizeof(TermDat
3086d 61 29 29 3b 0a 20 20 69 66 28 20 70 44 61 74 61  a));.  if( pData
3086e 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20  ==NULL ) return 
3086f 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20  SQLITE_NOMEM;.. 
30870 20 6e 52 65 73 75 6c 74 42 79 74 65 73 20 3d 20   nResultBytes = 
30871 30 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d 20 3d  0;.  for(iTerm =
30872 20 30 2c 20 65 20 3d 20 66 74 73 33 48 61 73 68   0, e = fts3Hash
30873 46 69 72 73 74 28 70 54 65 72 6d 73 29 3b 20 65  First(pTerms); e
30874 3b 20 69 54 65 72 6d 2b 2b 2c 20 65 20 3d 20 66  ; iTerm++, e = f
30875 74 73 33 48 61 73 68 4e 65 78 74 28 65 29 29 7b  ts3HashNext(e)){
30876 0a 20 20 20 20 6e 52 65 73 75 6c 74 42 79 74 65  .    nResultByte
30877 73 20 2b 3d 20 66 74 73 33 48 61 73 68 4b 65 79  s += fts3HashKey
30878 73 69 7a 65 28 65 29 2b 31 3b 20 20 20 2f 2a 20  size(e)+1;   /* 
30879 54 65 72 6d 20 70 6c 75 73 20 74 72 61 69 6c 69  Term plus traili
3087a 6e 67 20 73 70 61 63 65 20 2a 2f 0a 20 20 20 20  ng space */.    
3087b 61 73 73 65 72 74 28 20 69 54 65 72 6d 3c 6e 54  assert( iTerm<nT
3087c 65 72 6d 73 20 29 3b 0a 20 20 20 20 70 44 61 74  erms );.    pDat
3087d 61 5b 69 54 65 72 6d 5d 2e 70 54 65 72 6d 20 3d  a[iTerm].pTerm =
3087e 20 66 74 73 33 48 61 73 68 4b 65 79 28 65 29 3b   fts3HashKey(e);
3087f 0a 20 20 20 20 70 44 61 74 61 5b 69 54 65 72 6d  .    pData[iTerm
30880 5d 2e 6e 54 65 72 6d 20 3d 20 66 74 73 33 48 61  ].nTerm = fts3Ha
30881 73 68 4b 65 79 73 69 7a 65 28 65 29 3b 0a 20 20  shKeysize(e);.  
30882 20 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70    pData[iTerm].p
30883 43 6f 6c 6c 65 63 74 6f 72 20 3d 20 66 74 73 33  Collector = fts3
30884 48 61 73 68 44 61 74 61 28 65 29 3b 20 20 2f 2a  HashData(e);  /*
30885 20 75 6e 75 73 65 64 20 2a 2f 0a 20 20 7d 0a 20   unused */.  }. 
30886 20 61 73 73 65 72 74 28 20 69 54 65 72 6d 3d 3d   assert( iTerm==
30887 6e 54 65 72 6d 73 20 29 3b 0a 0a 20 20 61 73 73  nTerms );..  ass
30888 65 72 74 28 20 6e 52 65 73 75 6c 74 42 79 74 65  ert( nResultByte
30889 73 3e 30 20 29 3b 20 20 20 2f 2a 20 6e 54 65 72  s>0 );   /* nTer
3088a 6d 73 3e 30 2c 20 6e 52 65 73 75 6c 74 73 42 79  ms>0, nResultsBy
3088b 74 65 73 20 6d 75 73 74 20 62 65 2c 20 74 6f 6f  tes must be, too
3088c 2e 20 2a 2f 0a 20 20 72 65 73 75 6c 74 20 3d 20  . */.  result = 
3088d 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e  sqlite3_malloc(n
3088e 52 65 73 75 6c 74 42 79 74 65 73 29 3b 0a 20 20  ResultBytes);.  
3088f 69 66 28 20 72 65 73 75 6c 74 3d 3d 4e 55 4c 4c  if( result==NULL
30890 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f   ){.    sqlite3_
30891 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20 20 20  free(pData);.   
30892 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
30893 4f 4d 45 4d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  OMEM;.  }..  if(
30894 20 6e 54 65 72 6d 73 3e 31 20 29 20 71 73 6f 72   nTerms>1 ) qsor
30895 74 28 70 44 61 74 61 2c 20 6e 54 65 72 6d 73 2c  t(pData, nTerms,
30896 20 73 69 7a 65 6f 66 28 2a 70 44 61 74 61 29 2c   sizeof(*pData),
30897 20 74 65 72 6d 44 61 74 61 43 6d 70 29 3b 0a 0a   termDataCmp);..
30898 20 20 2f 2a 20 52 65 61 64 20 74 68 65 20 74 65    /* Read the te
30899 72 6d 73 20 69 6e 20 6f 72 64 65 72 20 74 6f 20  rms in order to 
3089a 62 75 69 6c 64 20 74 68 65 20 72 65 73 75 6c 74  build the result
3089b 2e 20 2a 2f 0a 20 20 69 42 79 74 65 20 3d 20 30  . */.  iByte = 0
3089c 3b 0a 20 20 66 6f 72 28 69 54 65 72 6d 3d 30 3b  ;.  for(iTerm=0;
3089d 20 69 54 65 72 6d 3c 6e 54 65 72 6d 73 3b 20 2b   iTerm<nTerms; +
3089e 2b 69 54 65 72 6d 29 7b 0a 20 20 20 20 6d 65 6d  +iTerm){.    mem
3089f 63 70 79 28 72 65 73 75 6c 74 2b 69 42 79 74 65  cpy(result+iByte
308a0 2c 20 70 44 61 74 61 5b 69 54 65 72 6d 5d 2e 70  , pData[iTerm].p
308a1 54 65 72 6d 2c 20 70 44 61 74 61 5b 69 54 65 72  Term, pData[iTer
308a2 6d 5d 2e 6e 54 65 72 6d 29 3b 0a 20 20 20 20 69  m].nTerm);.    i
308a3 42 79 74 65 20 2b 3d 20 70 44 61 74 61 5b 69 54  Byte += pData[iT
308a4 65 72 6d 5d 2e 6e 54 65 72 6d 3b 0a 20 20 20 20  erm].nTerm;.    
308a5 72 65 73 75 6c 74 5b 69 42 79 74 65 2b 2b 5d 20  result[iByte++] 
308a6 3d 20 27 20 27 3b 0a 20 20 7d 0a 20 20 61 73 73  = ' ';.  }.  ass
308a7 65 72 74 28 20 69 42 79 74 65 3d 3d 6e 52 65 73  ert( iByte==nRes
308a8 75 6c 74 42 79 74 65 73 20 29 3b 0a 20 20 61 73  ultBytes );.  as
308a9 73 65 72 74 28 20 72 65 73 75 6c 74 5b 6e 52 65  sert( result[nRe
308aa 73 75 6c 74 42 79 74 65 73 2d 31 5d 3d 3d 27 20  sultBytes-1]==' 
308ab 27 20 29 3b 0a 20 20 72 65 73 75 6c 74 5b 6e 52  ' );.  result[nR
308ac 65 73 75 6c 74 42 79 74 65 73 2d 31 5d 20 3d 20  esultBytes-1] = 
308ad 27 5c 30 27 3b 0a 0a 20 20 2f 2a 20 50 61 73 73  '\0';..  /* Pass
308ae 65 73 20 61 77 61 79 20 6f 77 6e 65 72 73 68 69  es away ownershi
308af 70 20 6f 66 20 72 65 73 75 6c 74 2e 20 2a 2f 0a  p of result. */.
308b0 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
308b1 5f 74 65 78 74 28 70 43 6f 6e 74 65 78 74 2c 20  _text(pContext, 
308b2 72 65 73 75 6c 74 2c 20 6e 52 65 73 75 6c 74 42  result, nResultB
308b3 79 74 65 73 2d 31 2c 20 73 71 6c 69 74 65 33 5f  ytes-1, sqlite3_
308b4 66 72 65 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  free);.  sqlite3
308b5 5f 66 72 65 65 28 70 44 61 74 61 29 3b 0a 20 20  _free(pData);.  
308b6 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
308b7 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65 6d 65 6e  ;.}../* Implemen
308b8 74 73 20 64 75 6d 70 5f 74 65 72 6d 73 28 29 20  ts dump_terms() 
308b9 66 6f 72 20 75 73 65 20 69 6e 20 69 6e 73 70 65  for use in inspe
308ba 63 74 69 6e 67 20 74 68 65 20 66 74 73 33 20 69  cting the fts3 i
308bb 6e 64 65 78 20 66 72 6f 6d 0a 2a 2a 20 74 65 73  ndex from.** tes
308bc 74 73 2e 20 20 54 45 58 54 20 72 65 73 75 6c 74  ts.  TEXT result
308bd 20 63 6f 6e 74 61 69 6e 69 6e 67 20 74 68 65 20   containing the 
308be 6f 72 64 65 72 65 64 20 6c 69 73 74 20 6f 66 20  ordered list of 
308bf 74 65 72 6d 73 20 6a 6f 69 6e 65 64 20 62 79 0a  terms joined by.
308c0 2a 2a 20 73 70 61 63 65 73 2e 20 20 64 75 6d 70  ** spaces.  dump
308c1 5f 74 65 72 6d 73 28 74 2c 20 6c 65 76 65 6c 2c  _terms(t, level,
308c2 20 69 64 78 29 20 64 75 6d 70 73 20 74 68 65 20   idx) dumps the 
308c3 74 65 72 6d 73 20 66 6f 72 20 74 68 65 20 73 65  terms for the se
308c4 67 6d 65 6e 74 0a 2a 2a 20 73 70 65 63 69 66 69  gment.** specifi
308c5 65 64 20 62 79 20 6c 65 76 65 6c 2c 20 69 64 78  ed by level, idx
308c6 20 28 69 6e 20 25 5f 73 65 67 64 69 72 29 2c 20   (in %_segdir), 
308c7 77 68 69 6c 65 20 64 75 6d 70 5f 74 65 72 6d 73  while dump_terms
308c8 28 74 29 20 64 75 6d 70 73 0a 2a 2a 20 61 6c 6c  (t) dumps.** all
308c9 20 74 65 72 6d 73 20 69 6e 20 74 68 65 20 69 6e   terms in the in
308ca 64 65 78 2e 20 20 49 6e 20 62 6f 74 68 20 63 61  dex.  In both ca
308cb 73 65 73 20 74 20 69 73 20 74 68 65 20 66 74 73  ses t is the fts
308cc 20 74 61 62 6c 65 27 73 20 6d 61 67 69 63 0a 2a   table's magic.*
308cd 2a 20 74 61 62 6c 65 2d 6e 61 6d 65 64 20 63 6f  * table-named co
308ce 6c 75 6d 6e 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  lumn..*/.static 
308cf 76 6f 69 64 20 64 75 6d 70 54 65 72 6d 73 46 75  void dumpTermsFu
308d0 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f  nc(.  sqlite3_co
308d1 6e 74 65 78 74 20 2a 70 43 6f 6e 74 65 78 74 2c  ntext *pContext,
308d2 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
308d3 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
308d4 76 0a 29 7b 0a 20 20 66 75 6c 6c 74 65 78 74 5f  v.){.  fulltext_
308d5 63 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 3b  cursor *pCursor;
308d6 0a 20 20 69 66 28 20 61 72 67 63 21 3d 33 20 26  .  if( argc!=3 &
308d7 26 20 61 72 67 63 21 3d 31 20 29 7b 0a 20 20 20  & argc!=1 ){.   
308d8 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70   generateError(p
308d9 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74  Context, "dump_t
308da 65 72 6d 73 22 2c 20 22 69 6e 63 6f 72 72 65 63  erms", "incorrec
308db 74 20 61 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20  t arguments");. 
308dc 20 7d 65 6c 73 65 20 69 66 28 20 73 71 6c 69 74   }else if( sqlit
308dd 65 33 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 72  e3_value_type(ar
308de 67 76 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42  gv[0])!=SQLITE_B
308df 4c 4f 42 20 7c 7c 0a 20 20 20 20 20 20 20 20 20  LOB ||.         
308e0 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65     sqlite3_value
308e1 5f 62 79 74 65 73 28 61 72 67 76 5b 30 5d 29 21  _bytes(argv[0])!
308e2 3d 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29  =sizeof(pCursor)
308e3 20 29 7b 0a 20 20 20 20 67 65 6e 65 72 61 74 65   ){.    generate
308e4 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
308e5 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 69  "dump_terms", "i
308e6 6c 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67  llegal first arg
308e7 75 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65  ument");.  }else
308e8 7b 0a 20 20 20 20 66 75 6c 6c 74 65 78 74 5f 76  {.    fulltext_v
308e9 74 61 62 20 2a 76 3b 0a 20 20 20 20 66 74 73 33  tab *v;.    fts3
308ea 48 61 73 68 20 74 65 72 6d 73 3b 0a 20 20 20 20  Hash terms;.    
308eb 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 73 20  sqlite3_stmt *s 
308ec 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 69 6e 74 20  = NULL;.    int 
308ed 72 63 3b 0a 0a 20 20 20 20 6d 65 6d 63 70 79 28  rc;..    memcpy(
308ee 26 70 43 75 72 73 6f 72 2c 20 73 71 6c 69 74 65  &pCursor, sqlite
308ef 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
308f0 76 5b 30 5d 29 2c 20 73 69 7a 65 6f 66 28 70 43  v[0]), sizeof(pC
308f1 75 72 73 6f 72 29 29 3b 0a 20 20 20 20 76 20 3d  ursor));.    v =
308f2 20 63 75 72 73 6f 72 5f 76 74 61 62 28 70 43 75   cursor_vtab(pCu
308f3 72 73 6f 72 29 3b 0a 0a 20 20 20 20 2f 2a 20 49  rsor);..    /* I
308f4 66 20 70 61 73 73 65 64 20 6f 6e 6c 79 20 74 68  f passed only th
308f5 65 20 63 75 72 73 6f 72 20 63 6f 6c 75 6d 6e 2c  e cursor column,
308f6 20 67 65 74 20 61 6c 6c 20 73 65 67 6d 65 6e 74   get all segment
308f7 73 2e 20 20 4f 74 68 65 72 77 69 73 65 0a 20 20  s.  Otherwise.  
308f8 20 20 2a 2a 20 67 65 74 20 74 68 65 20 73 65 67    ** get the seg
308f9 6d 65 6e 74 20 64 65 73 63 72 69 62 65 64 20 62  ment described b
308fa 79 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  y the following 
308fb 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2e 0a 20  two arguments.. 
308fc 20 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 72     */.    if( ar
308fd 67 63 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 72  gc==1 ){.      r
308fe 63 20 3d 20 73 71 6c 5f 67 65 74 5f 73 74 61 74  c = sql_get_stat
308ff 65 6d 65 6e 74 28 76 2c 20 53 45 47 44 49 52 5f  ement(v, SEGDIR_
30900 53 45 4c 45 43 54 5f 41 4c 4c 5f 53 54 4d 54 2c  SELECT_ALL_STMT,
30901 20 26 73 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b   &s);.    }else{
30902 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 5f  .      rc = sql_
30903 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76 2c  get_statement(v,
30904 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f 53   SEGDIR_SELECT_S
30905 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73 29  EGMENT_STMT, &s)
30906 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ;.      if( rc==
30907 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
30908 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
30909 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31 2c  3_bind_int(s, 1,
3090a 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
3090b 6e 74 28 61 72 67 76 5b 31 5d 29 29 3b 0a 20 20  nt(argv[1]));.  
3090c 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
3090d 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20  LITE_OK ){.     
3090e 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
3090f 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32 2c  3_bind_int(s, 2,
30910 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
30911 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20 20  nt(argv[2]));.  
30912 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a        }.      }.
30913 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 72      }..    if( r
30914 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
30915 20 20 20 20 20 20 67 65 6e 65 72 61 74 65 45 72        generateEr
30916 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64  ror(pContext, "d
30917 75 6d 70 5f 74 65 72 6d 73 22 2c 20 4e 55 4c 4c  ump_terms", NULL
30918 29 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b  );.      return;
30919 0a 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 43  .    }..    /* C
3091a 6f 6c 6c 65 63 74 20 74 68 65 20 74 65 72 6d 73  ollect the terms
3091b 20 66 6f 72 20 65 61 63 68 20 73 65 67 6d 65 6e   for each segmen
3091c 74 2e 20 2a 2f 0a 20 20 20 20 73 71 6c 69 74 65  t. */.    sqlite
3091d 33 46 74 73 33 48 61 73 68 49 6e 69 74 28 26 74  3Fts3HashInit(&t
3091e 65 72 6d 73 2c 20 46 54 53 33 5f 48 41 53 48 5f  erms, FTS3_HASH_
3091f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 20 20  STRING, 1);.    
30920 77 68 69 6c 65 28 20 28 72 63 20 3d 20 73 71 6c  while( (rc = sql
30921 69 74 65 33 5f 73 74 65 70 28 73 29 29 3d 3d 53  ite3_step(s))==S
30922 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20  QLITE_ROW ){.   
30923 20 20 20 72 63 20 3d 20 63 6f 6c 6c 65 63 74 53     rc = collectS
30924 65 67 6d 65 6e 74 54 65 72 6d 73 28 76 2c 20 73  egmentTerms(v, s
30925 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20 20 20 20  , &terms);.     
30926 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45 5f   if( rc!=SQLITE_
30927 4f 4b 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20  OK ) break;.    
30928 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 21 3d 53  }..    if( rc!=S
30929 51 4c 49 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20  QLITE_DONE ){.  
3092a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 65      sqlite3_rese
3092b 74 28 73 29 3b 0a 20 20 20 20 20 20 67 65 6e 65  t(s);.      gene
3092c 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
3092d 78 74 2c 20 22 64 75 6d 70 5f 74 65 72 6d 73 22  xt, "dump_terms"
3092e 2c 20 4e 55 4c 4c 29 3b 0a 20 20 20 20 7d 65 6c  , NULL);.    }el
3092f 73 65 7b 0a 20 20 20 20 20 20 63 6f 6e 73 74 20  se{.      const 
30930 69 6e 74 20 6e 54 65 72 6d 73 20 3d 20 66 74 73  int nTerms = fts
30931 33 48 61 73 68 43 6f 75 6e 74 28 26 74 65 72 6d  3HashCount(&term
30932 73 29 3b 0a 20 20 20 20 20 20 69 66 28 20 6e 54  s);.      if( nT
30933 65 72 6d 73 3e 30 20 29 7b 0a 20 20 20 20 20 20  erms>0 ){.      
30934 20 20 72 63 20 3d 20 67 65 6e 65 72 61 74 65 54    rc = generateT
30935 65 72 6d 73 52 65 73 75 6c 74 28 70 43 6f 6e 74  ermsResult(pCont
30936 65 78 74 2c 20 26 74 65 72 6d 73 29 3b 0a 20 20  ext, &terms);.  
30937 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
30938 4c 49 54 45 5f 4e 4f 4d 45 4d 20 29 7b 0a 20 20  LITE_NOMEM ){.  
30939 20 20 20 20 20 20 20 20 67 65 6e 65 72 61 74 65          generate
3093a 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20  Error(pContext, 
3093b 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 22 6f  "dump_terms", "o
3093c 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22 29 3b 0a  ut of memory");.
3093d 20 20 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20          }else{. 
3093e 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28           assert(
3093f 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
30940 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
30941 20 20 7d 65 6c 73 65 20 69 66 28 20 61 72 67 63    }else if( argc
30942 3d 3d 33 20 29 7b 0a 20 20 20 20 20 20 20 20 2f  ==3 ){.        /
30943 2a 20 54 68 65 20 73 70 65 63 69 66 69 63 20 73  * The specific s
30944 65 67 6d 65 6e 74 20 61 73 6b 65 64 20 66 6f 72  egment asked for
30945 20 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 66 6f   could not be fo
30946 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20 20 20 20  und. */.        
30947 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70 43  generateError(pC
30948 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 74 65  ontext, "dump_te
30949 72 6d 73 22 2c 20 22 73 65 67 6d 65 6e 74 20 6e  rms", "segment n
3094a 6f 74 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20 20  ot found");.    
3094b 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20    }else{.       
3094c 20 2f 2a 20 4e 6f 20 73 65 67 6d 65 6e 74 73 20   /* No segments 
3094d 66 6f 75 6e 64 2e 20 2a 2f 0a 20 20 20 20 20 20  found. */.      
3094e 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73 73 29    /* TODO(shess)
3094f 3a 20 49 74 20 73 68 6f 75 6c 64 20 62 65 20 69  : It should be i
30950 6d 70 6f 73 73 69 62 6c 65 20 74 6f 20 72 65 61  mpossible to rea
30951 63 68 20 74 68 69 73 2e 20 20 54 68 69 73 0a 20  ch this.  This. 
30952 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65 20 63         ** case c
30953 61 6e 20 6f 6e 6c 79 20 68 61 70 70 65 6e 20 66  an only happen f
30954 6f 72 20 61 6e 20 65 6d 70 74 79 20 74 61 62 6c  or an empty tabl
30955 65 2c 20 69 6e 20 77 68 69 63 68 20 63 61 73 65  e, in which case
30956 0a 20 20 20 20 20 20 20 20 2a 2a 20 53 51 4c 69  .        ** SQLi
30957 74 65 20 68 61 73 20 6e 6f 20 72 6f 77 73 20 74  te has no rows t
30958 6f 20 63 61 6c 6c 20 74 68 69 73 20 66 75 6e 63  o call this func
30959 74 69 6f 6e 20 6f 6e 2e 0a 20 20 20 20 20 20 20  tion on..       
3095a 20 2a 2f 0a 20 20 20 20 20 20 20 20 73 71 6c 69   */.        sqli
3095b 74 65 33 5f 72 65 73 75 6c 74 5f 6e 75 6c 6c 28  te3_result_null(
3095c 70 43 6f 6e 74 65 78 74 29 3b 0a 20 20 20 20 20  pContext);.     
3095d 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c   }.    }.    sql
3095e 69 74 65 33 46 74 73 33 48 61 73 68 43 6c 65 61  ite3Fts3HashClea
3095f 72 28 26 74 65 72 6d 73 29 3b 0a 20 20 7d 0a 7d  r(&terms);.  }.}
30960 0a 0a 2f 2a 20 45 78 70 61 6e 64 20 74 68 65 20  ../* Expand the 
30961 44 4c 5f 44 45 46 41 55 4c 54 20 64 6f 63 6c 69  DL_DEFAULT docli
30962 73 74 20 69 6e 20 70 44 61 74 61 20 69 6e 74 6f  st in pData into
30963 20 61 20 74 65 78 74 20 72 65 73 75 6c 74 20 69   a text result i
30964 6e 0a 2a 2a 20 70 43 6f 6e 74 65 78 74 2e 0a 2a  n.** pContext..*
30965 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 63 72  /.static void cr
30966 65 61 74 65 44 6f 63 6c 69 73 74 52 65 73 75 6c  eateDoclistResul
30967 74 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78  t(sqlite3_contex
30968 74 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 20  t *pContext,.   
30969 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3096a 20 20 20 20 20 20 20 20 20 20 20 20 20 63 6f 6e               con
3096b 73 74 20 63 68 61 72 20 2a 70 44 61 74 61 2c 20  st char *pData, 
3096c 69 6e 74 20 6e 44 61 74 61 29 7b 0a 20 20 44 61  int nData){.  Da
3096d 74 61 42 75 66 66 65 72 20 64 75 6d 70 3b 0a 20  taBuffer dump;. 
3096e 20 44 4c 52 65 61 64 65 72 20 64 6c 52 65 61 64   DLReader dlRead
3096f 65 72 3b 0a 0a 20 20 61 73 73 65 72 74 28 20 70  er;..  assert( p
30970 44 61 74 61 21 3d 4e 55 4c 4c 20 26 26 20 6e 44  Data!=NULL && nD
30971 61 74 61 3e 30 20 29 3b 0a 0a 20 20 64 61 74 61  ata>0 );..  data
30972 42 75 66 66 65 72 49 6e 69 74 28 26 64 75 6d 70  BufferInit(&dump
30973 2c 20 30 29 3b 0a 20 20 64 6c 72 49 6e 69 74 28  , 0);.  dlrInit(
30974 26 64 6c 52 65 61 64 65 72 2c 20 44 4c 5f 44 45  &dlReader, DL_DE
30975 46 41 55 4c 54 2c 20 70 44 61 74 61 2c 20 6e 44  FAULT, pData, nD
30976 61 74 61 29 3b 0a 20 20 66 6f 72 28 20 3b 20 21  ata);.  for( ; !
30977 64 6c 72 41 74 45 6e 64 28 26 64 6c 52 65 61 64  dlrAtEnd(&dlRead
30978 65 72 29 3b 20 64 6c 72 53 74 65 70 28 26 64 6c  er); dlrStep(&dl
30979 52 65 61 64 65 72 29 20 29 7b 0a 20 20 20 20 63  Reader) ){.    c
3097a 68 61 72 20 62 75 66 5b 32 35 36 5d 3b 0a 20 20  har buf[256];.  
3097b 20 20 50 4c 52 65 61 64 65 72 20 70 6c 52 65 61    PLReader plRea
3097c 64 65 72 3b 0a 0a 20 20 20 20 70 6c 72 49 6e 69  der;..    plrIni
3097d 74 28 26 70 6c 52 65 61 64 65 72 2c 20 26 64 6c  t(&plReader, &dl
3097e 52 65 61 64 65 72 29 3b 0a 20 20 20 20 69 66 28  Reader);.    if(
3097f 20 44 4c 5f 44 45 46 41 55 4c 54 3d 3d 44 4c 5f   DL_DEFAULT==DL_
30980 44 4f 43 49 44 53 20 7c 7c 20 70 6c 72 41 74 45  DOCIDS || plrAtE
30981 6e 64 28 26 70 6c 52 65 61 64 65 72 29 20 29 7b  nd(&plReader) ){
30982 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73  .      sqlite3_s
30983 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62  nprintf(sizeof(b
30984 75 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c 64  uf), buf, "[%lld
30985 5d 20 22 2c 20 64 6c 72 44 6f 63 69 64 28 26 64  ] ", dlrDocid(&d
30986 6c 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20  lReader));.     
30987 20 64 61 74 61 42 75 66 66 65 72 41 70 70 65 6e   dataBufferAppen
30988 64 28 26 64 75 6d 70 2c 20 62 75 66 2c 20 73 74  d(&dump, buf, st
30989 72 6c 65 6e 28 62 75 66 29 29 3b 0a 20 20 20 20  rlen(buf));.    
3098a 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 69 6e 74  }else{.      int
3098b 20 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f   iColumn = plrCo
3098c 6c 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 3b  lumn(&plReader);
3098d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ..      sqlite3_
3098e 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28  snprintf(sizeof(
3098f 62 75 66 29 2c 20 62 75 66 2c 20 22 5b 25 6c 6c  buf), buf, "[%ll
30990 64 20 25 64 5b 22 2c 0a 20 20 20 20 20 20 20 20  d %d[",.        
30991 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 64                 d
30992 6c 72 44 6f 63 69 64 28 26 64 6c 52 65 61 64 65  lrDocid(&dlReade
30993 72 29 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a 20 20  r), iColumn);.  
30994 20 20 20 20 64 61 74 61 42 75 66 66 65 72 41 70      dataBufferAp
30995 70 65 6e 64 28 26 64 75 6d 70 2c 20 62 75 66 2c  pend(&dump, buf,
30996 20 73 74 72 6c 65 6e 28 62 75 66 29 29 3b 0a 0a   strlen(buf));..
30997 20 20 20 20 20 20 66 6f 72 28 20 3b 20 21 70 6c        for( ; !pl
30998 72 41 74 45 6e 64 28 26 70 6c 52 65 61 64 65 72  rAtEnd(&plReader
30999 29 3b 20 70 6c 72 53 74 65 70 28 26 70 6c 52 65  ); plrStep(&plRe
3099a 61 64 65 72 29 20 29 7b 0a 20 20 20 20 20 20 20  ader) ){.       
3099b 20 69 66 28 20 70 6c 72 43 6f 6c 75 6d 6e 28 26   if( plrColumn(&
3099c 70 6c 52 65 61 64 65 72 29 21 3d 69 43 6f 6c 75  plReader)!=iColu
3099d 6d 6e 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  mn ){.          
3099e 69 43 6f 6c 75 6d 6e 20 3d 20 70 6c 72 43 6f 6c  iColumn = plrCol
3099f 75 6d 6e 28 26 70 6c 52 65 61 64 65 72 29 3b 0a  umn(&plReader);.
309a0 20 20 20 20 20 20 20 20 20 20 73 71 6c 69 74 65            sqlite
309a1 33 5f 73 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f  3_snprintf(sizeo
309a2 66 28 62 75 66 29 2c 20 62 75 66 2c 20 22 5d 20  f(buf), buf, "] 
309a3 25 64 5b 22 2c 20 69 43 6f 6c 75 6d 6e 29 3b 0a  %d[", iColumn);.
309a4 20 20 20 20 20 20 20 20 20 20 61 73 73 65 72 74            assert
309a5 28 20 64 75 6d 70 2e 6e 44 61 74 61 3e 30 20 29  ( dump.nData>0 )
309a6 3b 0a 20 20 20 20 20 20 20 20 20 20 64 75 6d 70  ;.          dump
309a7 2e 6e 44 61 74 61 2d 2d 3b 20 20 20 20 20 20 20  .nData--;       
309a8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
309a9 20 4f 76 65 72 77 72 69 74 65 20 74 72 61 69 6c   Overwrite trail
309aa 69 6e 67 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20  ing space. */.  
309ab 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
309ac 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d 70 2e  dump.pData[dump.
309ad 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a 20 20  nData]==' ');.  
309ae 20 20 20 20 20 20 20 20 64 61 74 61 42 75 66 66          dataBuff
309af 65 72 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20  erAppend(&dump, 
309b0 62 75 66 2c 20 73 74 72 6c 65 6e 28 62 75 66 29  buf, strlen(buf)
309b1 29 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20  );.        }.   
309b2 20 20 20 20 20 69 66 28 20 44 4c 5f 44 45 46 41       if( DL_DEFA
309b3 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54 49 4f 4e  ULT==DL_POSITION
309b4 53 5f 4f 46 46 53 45 54 53 20 29 7b 0a 20 20 20  S_OFFSETS ){.   
309b5 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73         sqlite3_s
309b6 6e 70 72 69 6e 74 66 28 73 69 7a 65 6f 66 28 62  nprintf(sizeof(b
309b7 75 66 29 2c 20 62 75 66 2c 20 22 25 64 2c 25 64  uf), buf, "%d,%d
309b8 2c 25 64 20 22 2c 0a 20 20 20 20 20 20 20 20 20  ,%d ",.         
309b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309ba 20 20 70 6c 72 50 6f 73 69 74 69 6f 6e 28 26 70    plrPosition(&p
309bb 6c 52 65 61 64 65 72 29 2c 0a 20 20 20 20 20 20  lReader),.      
309bc 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309bd 20 20 20 20 20 70 6c 72 53 74 61 72 74 4f 66 66       plrStartOff
309be 73 65 74 28 26 70 6c 52 65 61 64 65 72 29 2c 20  set(&plReader), 
309bf 70 6c 72 45 6e 64 4f 66 66 73 65 74 28 26 70 6c  plrEndOffset(&pl
309c0 52 65 61 64 65 72 29 29 3b 0a 20 20 20 20 20 20  Reader));.      
309c1 20 20 7d 65 6c 73 65 20 69 66 28 20 44 4c 5f 44    }else if( DL_D
309c2 45 46 41 55 4c 54 3d 3d 44 4c 5f 50 4f 53 49 54  EFAULT==DL_POSIT
309c3 49 4f 4e 53 20 29 7b 0a 20 20 20 20 20 20 20 20  IONS ){.        
309c4 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69 6e    sqlite3_snprin
309c5 74 66 28 73 69 7a 65 6f 66 28 62 75 66 29 2c 20  tf(sizeof(buf), 
309c6 62 75 66 2c 20 22 25 64 20 22 2c 20 70 6c 72 50  buf, "%d ", plrP
309c7 6f 73 69 74 69 6f 6e 28 26 70 6c 52 65 61 64 65  osition(&plReade
309c8 72 29 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c  r));.        }el
309c9 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20 61 73  se{.          as
309ca 73 65 72 74 28 20 4e 55 4c 4c 3d 3d 22 55 6e 68  sert( NULL=="Unh
309cb 61 6e 64 6c 65 64 20 44 4c 5f 44 45 46 41 55 4c  andled DL_DEFAUL
309cc 54 20 76 61 6c 75 65 22 29 3b 0a 20 20 20 20 20  T value");.     
309cd 20 20 20 7d 0a 20 20 20 20 20 20 20 20 64 61 74     }.        dat
309ce 61 42 75 66 66 65 72 41 70 70 65 6e 64 28 26 64  aBufferAppend(&d
309cf 75 6d 70 2c 20 62 75 66 2c 20 73 74 72 6c 65 6e  ump, buf, strlen
309d0 28 62 75 66 29 29 3b 0a 20 20 20 20 20 20 7d 0a  (buf));.      }.
309d1 20 20 20 20 20 20 70 6c 72 44 65 73 74 72 6f 79        plrDestroy
309d2 28 26 70 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20  (&plReader);..  
309d3 20 20 20 20 61 73 73 65 72 74 28 20 64 75 6d 70      assert( dump
309d4 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 20 20 20 20  .nData>0 );.    
309d5 20 20 64 75 6d 70 2e 6e 44 61 74 61 2d 2d 3b 20    dump.nData--; 
309d6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
309d7 20 20 20 20 2f 2a 20 4f 76 65 72 77 72 69 74 65      /* Overwrite
309d8 20 74 72 61 69 6c 69 6e 67 20 73 70 61 63 65 2e   trailing space.
309d9 20 2a 2f 0a 20 20 20 20 20 20 61 73 73 65 72 74   */.      assert
309da 28 20 64 75 6d 70 2e 70 44 61 74 61 5b 64 75 6d  ( dump.pData[dum
309db 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27 29 3b 0a  p.nData]==' ');.
309dc 20 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72        dataBuffer
309dd 41 70 70 65 6e 64 28 26 64 75 6d 70 2c 20 22 5d  Append(&dump, "]
309de 5d 20 22 2c 20 33 29 3b 0a 20 20 20 20 7d 0a 20  ] ", 3);.    }. 
309df 20 7d 0a 20 20 64 6c 72 44 65 73 74 72 6f 79 28   }.  dlrDestroy(
309e0 26 64 6c 52 65 61 64 65 72 29 3b 0a 0a 20 20 61  &dlReader);..  a
309e1 73 73 65 72 74 28 20 64 75 6d 70 2e 6e 44 61 74  ssert( dump.nDat
309e2 61 3e 30 20 29 3b 0a 20 20 64 75 6d 70 2e 6e 44  a>0 );.  dump.nD
309e3 61 74 61 2d 2d 3b 20 20 20 20 20 20 20 20 20 20  ata--;          
309e4 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 76             /* Ov
309e5 65 72 77 72 69 74 65 20 74 72 61 69 6c 69 6e 67  erwrite trailing
309e6 20 73 70 61 63 65 2e 20 2a 2f 0a 20 20 61 73 73   space. */.  ass
309e7 65 72 74 28 20 64 75 6d 70 2e 70 44 61 74 61 5b  ert( dump.pData[
309e8 64 75 6d 70 2e 6e 44 61 74 61 5d 3d 3d 27 20 27  dump.nData]==' '
309e9 29 3b 0a 20 20 64 75 6d 70 2e 70 44 61 74 61 5b  );.  dump.pData[
309ea 64 75 6d 70 2e 6e 44 61 74 61 5d 20 3d 20 27 5c  dump.nData] = '\
309eb 30 27 3b 0a 20 20 61 73 73 65 72 74 28 20 64 75  0';.  assert( du
309ec 6d 70 2e 6e 44 61 74 61 3e 30 20 29 3b 0a 0a 20  mp.nData>0 );.. 
309ed 20 2f 2a 20 50 61 73 73 65 73 20 6f 77 6e 65 72   /* Passes owner
309ee 73 68 69 70 20 6f 66 20 64 75 6d 70 27 73 20 62  ship of dump's b
309ef 75 66 66 65 72 20 74 6f 20 70 43 6f 6e 74 65 78  uffer to pContex
309f0 74 2e 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  t. */.  sqlite3_
309f1 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e  result_text(pCon
309f2 74 65 78 74 2c 20 64 75 6d 70 2e 70 44 61 74 61  text, dump.pData
309f3 2c 20 64 75 6d 70 2e 6e 44 61 74 61 2c 20 73 71  , dump.nData, sq
309f4 6c 69 74 65 33 5f 66 72 65 65 29 3b 0a 20 20 64  lite3_free);.  d
309f5 75 6d 70 2e 70 44 61 74 61 20 3d 20 4e 55 4c 4c  ump.pData = NULL
309f6 3b 0a 20 20 64 75 6d 70 2e 6e 44 61 74 61 20 3d  ;.  dump.nData =
309f7 20 64 75 6d 70 2e 6e 43 61 70 61 63 69 74 79 20   dump.nCapacity 
309f8 3d 20 30 3b 0a 7d 0a 0a 2f 2a 20 49 6d 70 6c 65  = 0;.}../* Imple
309f9 6d 65 6e 74 73 20 64 75 6d 70 5f 64 6f 63 6c 69  ments dump_docli
309fa 73 74 28 29 20 66 6f 72 20 75 73 65 20 69 6e 20  st() for use in 
309fb 69 6e 73 70 65 63 74 69 6e 67 20 74 68 65 20 66  inspecting the f
309fc 74 73 33 20 69 6e 64 65 78 20 66 72 6f 6d 0a 2a  ts3 index from.*
309fd 2a 20 74 65 73 74 73 2e 20 20 54 45 58 54 20 72  * tests.  TEXT r
309fe 65 73 75 6c 74 20 63 6f 6e 74 61 69 6e 69 6e 67  esult containing
309ff 20 61 20 73 74 72 69 6e 67 20 72 65 70 72 65 73   a string repres
30a00 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 0a  entation of the.
30a01 2a 2a 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74  ** doclist for t
30a02 68 65 20 69 6e 64 69 63 61 74 65 64 20 74 65 72  he indicated ter
30a03 6d 2e 20 20 64 75 6d 70 5f 64 6f 63 6c 69 73 74  m.  dump_doclist
30a04 28 74 2c 20 74 65 72 6d 2c 20 6c 65 76 65 6c 2c  (t, term, level,
30a05 20 69 64 78 29 0a 2a 2a 20 64 75 6d 70 73 20 74   idx).** dumps t
30a06 68 65 20 64 6f 63 6c 69 73 74 20 66 6f 72 20 74  he doclist for t
30a07 65 72 6d 20 66 72 6f 6d 20 74 68 65 20 73 65 67  erm from the seg
30a08 6d 65 6e 74 20 73 70 65 63 69 66 69 65 64 20 62  ment specified b
30a09 79 20 6c 65 76 65 6c 2c 20 69 64 78 0a 2a 2a 20  y level, idx.** 
30a0a 28 69 6e 20 25 5f 73 65 67 64 69 72 29 2c 20 77  (in %_segdir), w
30a0b 68 69 6c 65 20 64 75 6d 70 5f 64 6f 63 6c 69 73  hile dump_doclis
30a0c 74 28 74 2c 20 74 65 72 6d 29 20 64 75 6d 70 73  t(t, term) dumps
30a0d 20 74 68 65 20 6c 6f 67 69 63 61 6c 0a 2a 2a 20   the logical.** 
30a0e 64 6f 63 6c 69 73 74 20 66 6f 72 20 74 68 65 20  doclist for the 
30a0f 74 65 72 6d 20 61 63 72 6f 73 73 20 61 6c 6c 20  term across all 
30a10 73 65 67 6d 65 6e 74 73 2e 20 20 54 68 65 20 70  segments.  The p
30a11 65 72 2d 73 65 67 6d 65 6e 74 20 64 6f 63 6c 69  er-segment docli
30a12 73 74 0a 2a 2a 20 63 61 6e 20 63 6f 6e 74 61 69  st.** can contai
30a13 6e 20 64 65 6c 65 74 69 6f 6e 73 2c 20 77 68 69  n deletions, whi
30a14 6c 65 20 74 68 65 20 66 75 6c 6c 2d 69 6e 64 65  le the full-inde
30a15 78 20 64 6f 63 6c 69 73 74 20 77 69 6c 6c 20 6e  x doclist will n
30a16 6f 74 0a 2a 2a 20 28 64 65 6c 65 74 69 6f 6e 73  ot.** (deletions
30a17 20 61 72 65 20 6f 6d 69 74 74 65 64 29 2e 0a 2a   are omitted)..*
30a18 2a 0a 2a 2a 20 52 65 73 75 6c 74 20 66 6f 72 6d  *.** Result form
30a19 61 74 73 20 64 69 66 66 65 72 20 77 69 74 68 20  ats differ with 
30a1a 74 68 65 20 73 65 74 74 69 6e 67 20 6f 66 20 44  the setting of D
30a1b 4c 5f 44 45 46 41 55 4c 54 53 2e 20 20 45 78 61  L_DEFAULTS.  Exa
30a1c 6d 70 6c 65 73 3a 0a 2a 2a 0a 2a 2a 20 44 4c 5f  mples:.**.** DL_
30a1d 44 4f 43 49 44 53 3a 20 5b 31 5d 20 5b 33 5d 20  DOCIDS: [1] [3] 
30a1e 5b 37 5d 0a 2a 2a 20 44 4c 5f 50 4f 53 49 54 49  [7].** DL_POSITI
30a1f 4f 4e 53 3a 20 5b 31 20 30 5b 30 20 34 5d 20 31  ONS: [1 0[0 4] 1
30a20 5b 31 37 5d 5d 20 5b 33 20 31 5b 35 5d 5d 0a 2a  [17]] [3 1[5]].*
30a21 2a 20 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 5f 4f  * DL_POSITIONS_O
30a22 46 46 53 45 54 53 3a 20 5b 31 20 30 5b 30 2c 30  FFSETS: [1 0[0,0
30a23 2c 33 20 34 2c 32 33 2c 32 36 5d 20 31 5b 31 37  ,3 4,23,26] 1[17
30a24 2c 31 30 32 2c 31 30 35 5d 5d 20 5b 33 20 31 5b  ,102,105]] [3 1[
30a25 35 2c 32 30 2c 32 33 5d 5d 0a 2a 2a 0a 2a 2a 20  5,20,23]].**.** 
30a26 49 6e 20 65 61 63 68 20 63 61 73 65 20 74 68 65  In each case the
30a27 20 6e 75 6d 62 65 72 20 61 66 74 65 72 20 74 68   number after th
30a28 65 20 6f 75 74 65 72 20 27 5b 27 20 69 73 20 74  e outer '[' is t
30a29 68 65 20 64 6f 63 69 64 2e 20 20 49 6e 20 74 68  he docid.  In th
30a2a 65 0a 2a 2a 20 6c 61 74 74 65 72 20 74 77 6f 20  e.** latter two 
30a2b 63 61 73 65 73 2c 20 74 68 65 20 6e 75 6d 62 65  cases, the numbe
30a2c 72 20 62 65 66 6f 72 65 20 74 68 65 20 69 6e 6e  r before the inn
30a2d 65 72 20 27 5b 27 20 69 73 20 74 68 65 20 63 6f  er '[' is the co
30a2e 6c 75 6d 6e 0a 2a 2a 20 61 73 73 6f 63 69 61 74  lumn.** associat
30a2f 65 64 20 77 69 74 68 20 74 68 65 20 76 61 6c 75  ed with the valu
30a30 65 73 20 77 69 74 68 69 6e 2e 20 20 46 6f 72 20  es within.  For 
30a31 44 4c 5f 50 4f 53 49 54 49 4f 4e 53 20 74 68 65  DL_POSITIONS the
30a32 20 6e 75 6d 62 65 72 73 0a 2a 2a 20 77 69 74 68   numbers.** with
30a33 69 6e 20 61 72 65 20 74 68 65 20 70 6f 73 69 74  in are the posit
30a34 69 6f 6e 73 2c 20 66 6f 72 20 44 4c 5f 50 4f 53  ions, for DL_POS
30a35 49 54 49 4f 4e 53 5f 4f 46 46 53 45 54 53 20 74  ITIONS_OFFSETS t
30a36 68 65 79 20 61 72 65 20 74 68 65 0a 2a 2a 20 70  hey are the.** p
30a37 6f 73 69 74 69 6f 6e 2c 20 74 68 65 20 73 74 61  osition, the sta
30a38 72 74 20 6f 66 66 73 65 74 2c 20 61 6e 64 20 74  rt offset, and t
30a39 68 65 20 65 6e 64 20 6f 66 66 73 65 74 2e 0a 2a  he end offset..*
30a3a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 64 75  /.static void du
30a3b 6d 70 44 6f 63 6c 69 73 74 46 75 6e 63 28 0a 20  mpDoclistFunc(. 
30a3c 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
30a3d 20 2a 70 43 6f 6e 74 65 78 74 2c 0a 20 20 69 6e   *pContext,.  in
30a3e 74 20 61 72 67 63 2c 20 73 71 6c 69 74 65 33 5f  t argc, sqlite3_
30a3f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a  value **argv.){.
30a40 20 20 66 75 6c 6c 74 65 78 74 5f 63 75 72 73 6f    fulltext_curso
30a41 72 20 2a 70 43 75 72 73 6f 72 3b 0a 20 20 69 66  r *pCursor;.  if
30a42 28 20 61 72 67 63 21 3d 32 20 26 26 20 61 72 67  ( argc!=2 && arg
30a43 63 21 3d 34 20 29 7b 0a 20 20 20 20 67 65 6e 65  c!=4 ){.    gene
30a44 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
30a45 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
30a46 74 22 2c 20 22 69 6e 63 6f 72 72 65 63 74 20 61  t", "incorrect a
30a47 72 67 75 6d 65 6e 74 73 22 29 3b 0a 20 20 7d 65  rguments");.  }e
30a48 6c 73 65 20 69 66 28 20 73 71 6c 69 74 65 33 5f  lse if( sqlite3_
30a49 76 61 6c 75 65 5f 74 79 70 65 28 61 72 67 76 5b  value_type(argv[
30a4a 30 5d 29 21 3d 53 51 4c 49 54 45 5f 42 4c 4f 42  0])!=SQLITE_BLOB
30a4b 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 20 20 20   ||.            
30a4c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 79  sqlite3_value_by
30a4d 74 65 73 28 61 72 67 76 5b 30 5d 29 21 3d 73 69  tes(argv[0])!=si
30a4e 7a 65 6f 66 28 70 43 75 72 73 6f 72 29 20 29 7b  zeof(pCursor) ){
30a4f 0a 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72  .    generateErr
30a50 6f 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75  or(pContext, "du
30a51 6d 70 5f 64 6f 63 6c 69 73 74 22 2c 20 22 69 6c  mp_doclist", "il
30a52 6c 65 67 61 6c 20 66 69 72 73 74 20 61 72 67 75  legal first argu
30a53 6d 65 6e 74 22 29 3b 0a 20 20 7d 65 6c 73 65 20  ment");.  }else 
30a54 69 66 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  if( sqlite3_valu
30a55 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3d  e_text(argv[1])=
30a56 3d 4e 55 4c 4c 20 7c 7c 0a 20 20 20 20 20 20 20  =NULL ||.       
30a57 20 20 20 20 20 73 71 6c 69 74 65 33 5f 76 61 6c       sqlite3_val
30a58 75 65 5f 74 65 78 74 28 61 72 67 76 5b 31 5d 29  ue_text(argv[1])
30a59 5b 30 5d 3d 3d 27 5c 30 27 20 29 7b 0a 20 20 20  [0]=='\0' ){.   
30a5a 20 67 65 6e 65 72 61 74 65 45 72 72 6f 72 28 70   generateError(p
30a5b 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d 70 5f 64  Context, "dump_d
30a5c 6f 63 6c 69 73 74 22 2c 20 22 65 6d 70 74 79 20  oclist", "empty 
30a5d 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 22  second argument"
30a5e 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
30a5f 63 6f 6e 73 74 20 63 68 61 72 20 2a 70 54 65 72  const char *pTer
30a60 6d 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  m = (const char 
30a61 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
30a62 74 65 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20  text(argv[1]);. 
30a63 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 54 65     const int nTe
30a64 72 6d 20 3d 20 73 74 72 6c 65 6e 28 70 54 65 72  rm = strlen(pTer
30a65 6d 29 3b 0a 20 20 20 20 66 75 6c 6c 74 65 78 74  m);.    fulltext
30a66 5f 76 74 61 62 20 2a 76 3b 0a 20 20 20 20 69 6e  _vtab *v;.    in
30a67 74 20 72 63 3b 0a 20 20 20 20 44 61 74 61 42 75  t rc;.    DataBu
30a68 66 66 65 72 20 64 6f 63 6c 69 73 74 3b 0a 0a 20  ffer doclist;.. 
30a69 20 20 20 6d 65 6d 63 70 79 28 26 70 43 75 72 73     memcpy(&pCurs
30a6a 6f 72 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  or, sqlite3_valu
30a6b 65 5f 62 6c 6f 62 28 61 72 67 76 5b 30 5d 29 2c  e_blob(argv[0]),
30a6c 20 73 69 7a 65 6f 66 28 70 43 75 72 73 6f 72 29   sizeof(pCursor)
30a6d 29 3b 0a 20 20 20 20 76 20 3d 20 63 75 72 73 6f  );.    v = curso
30a6e 72 5f 76 74 61 62 28 70 43 75 72 73 6f 72 29 3b  r_vtab(pCursor);
30a6f 0a 0a 20 20 20 20 64 61 74 61 42 75 66 66 65 72  ..    dataBuffer
30a70 49 6e 69 74 28 26 64 6f 63 6c 69 73 74 2c 20 30  Init(&doclist, 0
30a71 29 3b 0a 0a 20 20 20 20 2f 2a 20 74 65 72 6d 53  );..    /* termS
30a72 65 6c 65 63 74 28 29 20 79 69 65 6c 64 73 20 74  elect() yields t
30a73 68 65 20 73 61 6d 65 20 6c 6f 67 69 63 61 6c 20  he same logical 
30a74 64 6f 63 6c 69 73 74 20 74 68 61 74 20 71 75 65  doclist that que
30a75 72 69 65 73 20 61 72 65 0a 20 20 20 20 2a 2a 20  ries are.    ** 
30a76 72 75 6e 20 61 67 61 69 6e 73 74 2e 0a 20 20 20  run against..   
30a77 20 2a 2f 0a 20 20 20 20 69 66 28 20 61 72 67 63   */.    if( argc
30a78 3d 3d 32 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ==2 ){.      rc 
30a79 3d 20 74 65 72 6d 53 65 6c 65 63 74 28 76 2c 20  = termSelect(v, 
30a7a 76 2d 3e 6e 43 6f 6c 75 6d 6e 2c 20 70 54 65 72  v->nColumn, pTer
30a7b 6d 2c 20 6e 54 65 72 6d 2c 20 30 2c 20 44 4c 5f  m, nTerm, 0, DL_
30a7c 44 45 46 41 55 4c 54 2c 20 26 64 6f 63 6c 69 73  DEFAULT, &doclis
30a7d 74 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  t);.    }else{. 
30a7e 20 20 20 20 20 73 71 6c 69 74 65 33 5f 73 74 6d       sqlite3_stm
30a7f 74 20 2a 73 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20  t *s = NULL;..  
30a80 20 20 20 20 2f 2a 20 47 65 74 20 6f 75 72 20 73      /* Get our s
30a81 70 65 63 69 66 69 63 20 73 65 67 6d 65 6e 74 27  pecific segment'
30a82 73 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 2e 20 2a  s information. *
30a83 2f 0a 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c  /.      rc = sql
30a84 5f 67 65 74 5f 73 74 61 74 65 6d 65 6e 74 28 76  _get_statement(v
30a85 2c 20 53 45 47 44 49 52 5f 53 45 4c 45 43 54 5f  , SEGDIR_SELECT_
30a86 53 45 47 4d 45 4e 54 5f 53 54 4d 54 2c 20 26 73  SEGMENT_STMT, &s
30a87 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
30a88 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
30a89 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
30a8a 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 31  e3_bind_int(s, 1
30a8b 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
30a8c 69 6e 74 28 61 72 67 76 5b 32 5d 29 29 3b 0a 20  int(argv[2]));. 
30a8d 20 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53         if( rc==S
30a8e 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
30a8f 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
30a90 65 33 5f 62 69 6e 64 5f 69 6e 74 28 73 2c 20 32  e3_bind_int(s, 2
30a91 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  , sqlite3_value_
30a92 69 6e 74 28 61 72 67 76 5b 33 5d 29 29 3b 0a 20  int(argv[3]));. 
30a93 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
30a94 0a 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d  ..      if( rc==
30a95 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
30a96 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65       rc = sqlite
30a97 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20 20 20  3_step(s);..    
30a98 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
30a99 54 45 5f 44 4f 4e 45 20 29 7b 0a 20 20 20 20 20  TE_DONE ){.     
30a9a 20 20 20 20 20 64 61 74 61 42 75 66 66 65 72 44       dataBufferD
30a9b 65 73 74 72 6f 79 28 26 64 6f 63 6c 69 73 74 29  estroy(&doclist)
30a9c 3b 0a 20 20 20 20 20 20 20 20 20 20 67 65 6e 65  ;.          gene
30a9d 72 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65  rateError(pConte
30a9e 78 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73  xt, "dump_doclis
30a9f 74 22 2c 20 22 73 65 67 6d 65 6e 74 20 6e 6f 74  t", "segment not
30aa0 20 66 6f 75 6e 64 22 29 3b 0a 20 20 20 20 20 20   found");.      
30aa1 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
30aa2 20 20 20 20 7d 0a 0a 20 20 20 20 20 20 20 20 2f      }..        /
30aa3 2a 20 46 6f 75 6e 64 20 61 20 73 65 67 6d 65 6e  * Found a segmen
30aa4 74 2c 20 6c 6f 61 64 20 69 74 20 69 6e 74 6f 20  t, load it into 
30aa5 64 6f 63 6c 69 73 74 2e 20 2a 2f 0a 20 20 20 20  doclist. */.    
30aa6 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
30aa7 54 45 5f 52 4f 57 20 29 7b 0a 20 20 20 20 20 20  TE_ROW ){.      
30aa8 20 20 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65      const sqlite
30aa9 5f 69 6e 74 36 34 20 69 4c 65 61 76 65 73 45 6e  _int64 iLeavesEn
30aaa 64 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75  d = sqlite3_colu
30aab 6d 6e 5f 69 6e 74 36 34 28 73 2c 20 31 29 3b 0a  mn_int64(s, 1);.
30aac 20 20 20 20 20 20 20 20 20 20 63 6f 6e 73 74 20            const 
30aad 63 68 61 72 20 2a 70 44 61 74 61 20 3d 20 73 71  char *pData = sq
30aae 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f  lite3_column_blo
30aaf 62 28 73 2c 20 32 29 3b 0a 20 20 20 20 20 20 20  b(s, 2);.       
30ab0 20 20 20 63 6f 6e 73 74 20 69 6e 74 20 6e 44 61     const int nDa
30ab1 74 61 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ta = sqlite3_col
30ab2 75 6d 6e 5f 62 79 74 65 73 28 73 2c 20 32 29 3b  umn_bytes(s, 2);
30ab3 0a 0a 20 20 20 20 20 20 20 20 20 20 2f 2a 20 6c  ..          /* l
30ab4 6f 61 64 53 65 67 6d 65 6e 74 28 29 20 69 73 20  oadSegment() is 
30ab5 75 73 65 64 20 62 79 20 74 65 72 6d 53 65 6c 65  used by termSele
30ab6 63 74 28 29 20 74 6f 20 6c 6f 61 64 20 65 61 63  ct() to load eac
30ab7 68 0a 20 20 20 20 20 20 20 20 20 20 2a 2a 20 73  h.          ** s
30ab8 65 67 6d 65 6e 74 27 73 20 64 61 74 61 2e 0a 20  egment's data.. 
30ab9 20 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20           */.    
30aba 20 20 20 20 20 20 72 63 20 3d 20 6c 6f 61 64 53        rc = loadS
30abb 65 67 6d 65 6e 74 28 76 2c 20 70 44 61 74 61 2c  egment(v, pData,
30abc 20 6e 44 61 74 61 2c 20 69 4c 65 61 76 65 73 45   nData, iLeavesE
30abd 6e 64 2c 20 70 54 65 72 6d 2c 20 6e 54 65 72 6d  nd, pTerm, nTerm
30abe 2c 20 30 2c 0a 20 20 20 20 20 20 20 20 20 20 20  , 0,.           
30abf 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ac0 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20 20 20  &doclist);.     
30ac1 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
30ac2 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
30ac3 20 20 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74        rc = sqlit
30ac4 65 33 5f 73 74 65 70 28 73 29 3b 0a 0a 20 20 20  e3_step(s);..   
30ac5 20 20 20 20 20 20 20 20 20 2f 2a 20 53 68 6f 75           /* Shou
30ac6 6c 64 20 6e 6f 74 20 68 61 76 65 20 6d 6f 72 65  ld not have more
30ac7 20 74 68 61 6e 20 6f 6e 65 20 6d 61 74 63 68 69   than one matchi
30ac8 6e 67 20 73 65 67 6d 65 6e 74 2e 20 2a 2f 0a 20  ng segment. */. 
30ac9 20 20 20 20 20 20 20 20 20 20 20 69 66 28 20 72             if( r
30aca 63 21 3d 53 51 4c 49 54 45 5f 44 4f 4e 45 20 29  c!=SQLITE_DONE )
30acb 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  {.              
30acc 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 73 29  sqlite3_reset(s)
30acd 3b 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;.              
30ace 64 61 74 61 42 75 66 66 65 72 44 65 73 74 72 6f  dataBufferDestro
30acf 79 28 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 20  y(&doclist);.   
30ad0 20 20 20 20 20 20 20 20 20 20 20 67 65 6e 65 72             gener
30ad1 61 74 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78  ateError(pContex
30ad2 74 2c 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74  t, "dump_doclist
30ad3 22 2c 20 22 69 6e 76 61 6c 69 64 20 73 65 67 64  ", "invalid segd
30ad4 69 72 22 29 3b 0a 20 20 20 20 20 20 20 20 20 20  ir");.          
30ad5 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20      return;.    
30ad6 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20          }.      
30ad7 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
30ad8 45 5f 4f 4b 3b 0a 20 20 20 20 20 20 20 20 20 20  E_OK;.          
30ad9 7d 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  }.        }.    
30ada 20 20 7d 0a 0a 20 20 20 20 20 20 73 71 6c 69 74    }..      sqlit
30adb 65 33 5f 72 65 73 65 74 28 73 29 3b 0a 20 20 20  e3_reset(s);.   
30adc 20 7d 0a 0a 20 20 20 20 69 66 28 20 72 63 3d 3d   }..    if( rc==
30add 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
30ade 20 20 20 69 66 28 20 64 6f 63 6c 69 73 74 2e 6e     if( doclist.n
30adf 44 61 74 61 3e 30 20 29 7b 0a 20 20 20 20 20 20  Data>0 ){.      
30ae0 20 20 63 72 65 61 74 65 44 6f 63 6c 69 73 74 52    createDoclistR
30ae1 65 73 75 6c 74 28 70 43 6f 6e 74 65 78 74 2c 20  esult(pContext, 
30ae2 64 6f 63 6c 69 73 74 2e 70 44 61 74 61 2c 20 64  doclist.pData, d
30ae3 6f 63 6c 69 73 74 2e 6e 44 61 74 61 29 3b 0a 20  oclist.nData);. 
30ae4 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
30ae5 20 20 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65 73      /* TODO(shes
30ae6 73 29 3a 20 54 68 69 73 20 63 61 6e 20 68 61 70  s): This can hap
30ae7 70 65 6e 20 69 66 20 74 68 65 20 74 65 72 6d 20  pen if the term 
30ae8 69 73 20 6e 6f 74 20 70 72 65 73 65 6e 74 2c 20  is not present, 
30ae9 6f 72 0a 20 20 20 20 20 20 20 20 2a 2a 20 69 66  or.        ** if
30aea 20 61 6c 6c 20 69 6e 73 74 61 6e 63 65 73 20 6f   all instances o
30aeb 66 20 74 68 65 20 74 65 72 6d 20 68 61 76 65 20  f the term have 
30aec 62 65 65 6e 20 64 65 6c 65 74 65 64 20 61 6e 64  been deleted and
30aed 20 74 68 69 73 20 69 73 0a 20 20 20 20 20 20 20   this is.       
30aee 20 2a 2a 20 61 6e 20 61 6c 6c 2d 69 6e 64 65 78   ** an all-index
30aef 20 64 75 6d 70 2e 20 20 49 74 20 6d 61 79 20 62   dump.  It may b
30af0 65 20 69 6e 74 65 72 65 73 74 69 6e 67 20 74 6f  e interesting to
30af1 20 64 69 73 74 69 6e 67 75 69 73 68 0a 20 20 20   distinguish.   
30af2 20 20 20 20 20 2a 2a 20 74 68 65 73 65 20 63 61       ** these ca
30af3 73 65 73 2e 0a 20 20 20 20 20 20 20 20 2a 2f 0a  ses..        */.
30af4 20 20 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f          sqlite3_
30af5 72 65 73 75 6c 74 5f 74 65 78 74 28 70 43 6f 6e  result_text(pCon
30af6 74 65 78 74 2c 20 22 22 2c 20 30 2c 20 53 51 4c  text, "", 0, SQL
30af7 49 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 20  ITE_STATIC);.   
30af8 20 20 20 7d 0a 20 20 20 20 7d 65 6c 73 65 20 69     }.    }else i
30af9 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4e 4f  f( rc==SQLITE_NO
30afa 4d 45 4d 20 29 7b 0a 20 20 20 20 20 20 2f 2a 20  MEM ){.      /* 
30afb 48 61 6e 64 6c 65 20 6f 75 74 2d 6f 66 2d 6d 65  Handle out-of-me
30afc 6d 6f 72 79 20 63 61 73 65 73 20 73 70 65 63 69  mory cases speci
30afd 61 6c 6c 79 20 62 65 63 61 75 73 65 20 69 66 20  ally because if 
30afe 74 68 65 79 20 61 72 65 0a 20 20 20 20 20 20 2a  they are.      *
30aff 2a 20 67 65 6e 65 72 61 74 65 64 20 69 6e 20 66  * generated in f
30b00 74 73 33 20 63 6f 64 65 20 74 68 65 79 20 6d 61  ts3 code they ma
30b01 79 20 6e 6f 74 20 62 65 20 72 65 66 6c 65 63 74  y not be reflect
30b02 65 64 20 69 6e 20 74 68 65 20 64 62 0a 20 20 20  ed in the db.   
30b03 20 20 20 2a 2a 20 68 61 6e 64 6c 65 2e 0a 20 20     ** handle..  
30b04 20 20 20 20 2a 2f 0a 20 20 20 20 20 20 2f 2a 20      */.      /* 
30b05 54 4f 44 4f 28 73 68 65 73 73 29 3a 20 48 61 6e  TODO(shess): Han
30b06 64 6c 65 20 74 68 69 73 20 6d 6f 72 65 20 63 6f  dle this more co
30b07 6d 70 72 65 68 65 6e 73 69 76 65 6c 79 2e 0a 20  mprehensively.. 
30b08 20 20 20 20 20 2a 2a 20 73 71 6c 69 74 65 33 45       ** sqlite3E
30b09 72 72 53 74 72 28 29 20 68 61 73 20 77 68 61 74  rrStr() has what
30b0a 20 49 20 6e 65 65 64 2c 20 62 75 74 20 69 73 20   I need, but is 
30b0b 69 6e 74 65 72 6e 61 6c 2e 0a 20 20 20 20 20 20  internal..      
30b0c 2a 2f 0a 20 20 20 20 20 20 67 65 6e 65 72 61 74  */.      generat
30b0d 65 45 72 72 6f 72 28 70 43 6f 6e 74 65 78 74 2c  eError(pContext,
30b0e 20 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22 2c   "dump_doclist",
30b0f 20 22 6f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 22   "out of memory"
30b10 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
30b11 20 20 20 20 67 65 6e 65 72 61 74 65 45 72 72 6f      generateErro
30b12 72 28 70 43 6f 6e 74 65 78 74 2c 20 22 64 75 6d  r(pContext, "dum
30b13 70 5f 64 6f 63 6c 69 73 74 22 2c 20 4e 55 4c 4c  p_doclist", NULL
30b14 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 64 61  );.    }..    da
30b15 74 61 42 75 66 66 65 72 44 65 73 74 72 6f 79 28  taBufferDestroy(
30b16 26 64 6f 63 6c 69 73 74 29 3b 0a 20 20 7d 0a 7d  &doclist);.  }.}
30b17 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54  .#endif../*.** T
30b18 68 69 73 20 72 6f 75 74 69 6e 65 20 69 6d 70 6c  his routine impl
30b19 65 6d 65 6e 74 73 20 74 68 65 20 78 46 69 6e 64  ements the xFind
30b1a 46 75 6e 63 74 69 6f 6e 20 6d 65 74 68 6f 64 20  Function method 
30b1b 66 6f 72 20 74 68 65 20 46 54 53 33 0a 2a 2a 20  for the FTS3.** 
30b1c 76 69 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a  virtual table..*
30b1d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c  /.static int ful
30b1e 6c 74 65 78 74 46 69 6e 64 46 75 6e 63 74 69 6f  ltextFindFunctio
30b1f 6e 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61  n(.  sqlite3_vta
30b20 62 20 2a 70 56 74 61 62 2c 0a 20 20 69 6e 74 20  b *pVtab,.  int 
30b21 6e 41 72 67 2c 0a 20 20 63 6f 6e 73 74 20 63 68  nArg,.  const ch
30b22 61 72 20 2a 7a 4e 61 6d 65 2c 0a 20 20 76 6f 69  ar *zName,.  voi
30b23 64 20 28 2a 2a 70 78 46 75 6e 63 29 28 73 71 6c  d (**pxFunc)(sql
30b24 69 74 65 33 5f 63 6f 6e 74 65 78 74 2a 2c 69 6e  ite3_context*,in
30b25 74 2c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 2a  t,sqlite3_value*
30b26 2a 29 2c 0a 20 20 76 6f 69 64 20 2a 2a 70 70 41  *),.  void **ppA
30b27 72 67 0a 29 7b 0a 20 20 69 66 28 20 73 74 72 63  rg.){.  if( strc
30b28 6d 70 28 7a 4e 61 6d 65 2c 22 73 6e 69 70 70 65  mp(zName,"snippe
30b29 74 22 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70  t")==0 ){.    *p
30b2a 78 46 75 6e 63 20 3d 20 73 6e 69 70 70 65 74 46  xFunc = snippetF
30b2b 75 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20  unc;.    return 
30b2c 31 3b 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 73  1;.  }else if( s
30b2d 74 72 63 6d 70 28 7a 4e 61 6d 65 2c 22 6f 66 66  trcmp(zName,"off
30b2e 73 65 74 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20  sets")==0 ){.   
30b2f 20 2a 70 78 46 75 6e 63 20 3d 20 73 6e 69 70 70   *pxFunc = snipp
30b30 65 74 4f 66 66 73 65 74 73 46 75 6e 63 3b 0a 20  etOffsetsFunc;. 
30b31 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20 7d     return 1;.  }
30b32 65 6c 73 65 20 69 66 28 20 73 74 72 63 6d 70 28  else if( strcmp(
30b33 7a 4e 61 6d 65 2c 22 6f 70 74 69 6d 69 7a 65 22  zName,"optimize"
30b34 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2a 70 78 46  )==0 ){.    *pxF
30b35 75 6e 63 20 3d 20 6f 70 74 69 6d 69 7a 65 46 75  unc = optimizeFu
30b36 6e 63 3b 0a 20 20 20 20 72 65 74 75 72 6e 20 31  nc;.    return 1
30b37 3b 0a 23 69 66 64 65 66 20 53 51 4c 49 54 45 5f  ;.#ifdef SQLITE_
30b38 54 45 53 54 0a 20 20 20 20 2f 2a 20 4e 4f 54 45  TEST.    /* NOTE
30b39 28 73 68 65 73 73 29 3a 20 54 68 65 73 65 20 66  (shess): These f
30b3a 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 70 72 65  unctions are pre
30b3b 73 65 6e 74 20 6f 6e 6c 79 20 66 6f 72 20 74 65  sent only for te
30b3c 73 74 69 6e 67 0a 20 20 20 20 2a 2a 20 70 75 72  sting.    ** pur
30b3d 70 6f 73 65 73 2e 20 20 4e 6f 20 70 61 72 74 69  poses.  No parti
30b3e 63 75 6c 61 72 20 65 66 66 6f 72 74 20 69 73 20  cular effort is 
30b3f 6d 61 64 65 20 74 6f 20 6f 70 74 69 6d 69 7a 65  made to optimize
30b40 20 74 68 65 69 72 0a 20 20 20 20 2a 2a 20 65 78   their.    ** ex
30b41 65 63 75 74 69 6f 6e 20 6f 72 20 68 6f 77 20 74  ecution or how t
30b42 68 65 79 20 62 75 69 6c 64 20 74 68 65 69 72 20  hey build their 
30b43 72 65 73 75 6c 74 73 2e 0a 20 20 20 20 2a 2f 0a  results..    */.
30b44 20 20 7d 65 6c 73 65 20 69 66 28 20 73 74 72 63    }else if( strc
30b45 6d 70 28 7a 4e 61 6d 65 2c 22 64 75 6d 70 5f 74  mp(zName,"dump_t
30b46 65 72 6d 73 22 29 3d 3d 30 20 29 7b 0a 20 20 20  erms")==0 ){.   
30b47 20 2f 2a 20 66 70 72 69 6e 74 66 28 73 74 64 65   /* fprintf(stde
30b48 72 72 2c 20 22 46 6f 75 6e 64 20 64 75 6d 70 5f  rr, "Found dump_
30b49 74 65 72 6d 73 5c 6e 22 29 3b 20 2a 2f 0a 20 20  terms\n"); */.  
30b4a 20 20 2a 70 78 46 75 6e 63 20 3d 20 64 75 6d 70    *pxFunc = dump
30b4b 54 65 72 6d 73 46 75 6e 63 3b 0a 20 20 20 20 72  TermsFunc;.    r
30b4c 65 74 75 72 6e 20 31 3b 0a 20 20 7d 65 6c 73 65  eturn 1;.  }else
30b4d 20 69 66 28 20 73 74 72 63 6d 70 28 7a 4e 61 6d   if( strcmp(zNam
30b4e 65 2c 22 64 75 6d 70 5f 64 6f 63 6c 69 73 74 22  e,"dump_doclist"
30b4f 29 3d 3d 30 20 29 7b 0a 20 20 20 20 2f 2a 20 66  )==0 ){.    /* f
30b50 70 72 69 6e 74 66 28 73 74 64 65 72 72 2c 20 22  printf(stderr, "
30b51 46 6f 75 6e 64 20 64 75 6d 70 5f 64 6f 63 6c 69  Found dump_docli
30b52 73 74 5c 6e 22 29 3b 20 2a 2f 0a 20 20 20 20 2a  st\n"); */.    *
30b53 70 78 46 75 6e 63 20 3d 20 64 75 6d 70 44 6f 63  pxFunc = dumpDoc
30b54 6c 69 73 74 46 75 6e 63 3b 0a 20 20 20 20 72 65  listFunc;.    re
30b55 74 75 72 6e 20 31 3b 0a 23 65 6e 64 69 66 0a 20  turn 1;.#endif. 
30b56 20 7d 0a 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d   }.  return 0;.}
30b57 0a 0a 2f 2a 0a 2a 2a 20 52 65 6e 61 6d 65 20 61  ../*.** Rename a
30b58 6e 20 66 74 73 33 20 74 61 62 6c 65 2e 0a 2a 2f  n fts3 table..*/
30b59 0a 73 74 61 74 69 63 20 69 6e 74 20 66 75 6c 6c  .static int full
30b5a 74 65 78 74 52 65 6e 61 6d 65 28 0a 20 20 73 71  textRename(.  sq
30b5b 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56 74 61  lite3_vtab *pVta
30b5c 62 2c 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  b,.  const char 
30b5d 2a 7a 4e 61 6d 65 0a 29 7b 0a 20 20 66 75 6c 6c  *zName.){.  full
30b5e 74 65 78 74 5f 76 74 61 62 20 2a 70 20 3d 20 28  text_vtab *p = (
30b5f 66 75 6c 6c 74 65 78 74 5f 76 74 61 62 20 2a 29  fulltext_vtab *)
30b60 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63 20  pVtab;.  int rc 
30b61 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
30b62 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73    char *zSql = s
30b63 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a  qlite3_mprintf(.
30b64 20 20 20 20 22 41 4c 54 45 52 20 54 41 42 4c 45      "ALTER TABLE
30b65 20 25 51 2e 27 25 71 5f 63 6f 6e 74 65 6e 74 27   %Q.'%q_content'
30b66 20 20 52 45 4e 41 4d 45 20 54 4f 20 27 25 71 5f    RENAME TO '%q_
30b67 63 6f 6e 74 65 6e 74 27 3b 22 0a 20 20 20 20 22  content';".    "
30b68 41 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27  ALTER TABLE %Q.'
30b69 25 71 5f 73 65 67 6d 65 6e 74 73 27 20 52 45 4e  %q_segments' REN
30b6a 41 4d 45 20 54 4f 20 27 25 71 5f 73 65 67 6d 65  AME TO '%q_segme
30b6b 6e 74 73 27 3b 22 0a 20 20 20 20 22 41 4c 54 45  nts';".    "ALTE
30b6c 52 20 54 41 42 4c 45 20 25 51 2e 27 25 71 5f 73  R TABLE %Q.'%q_s
30b6d 65 67 64 69 72 27 20 20 20 52 45 4e 41 4d 45 20  egdir'   RENAME 
30b6e 54 4f 20 27 25 71 5f 73 65 67 64 69 72 27 3b 22  TO '%q_segdir';"
30b6f 0a 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70  .    , p->zDb, p
30b70 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a  ->zName, zName .
30b71 20 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d      , p->zDb, p-
30b72 3e 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 20 0a 20  >zName, zName . 
30b73 20 20 20 2c 20 70 2d 3e 7a 44 62 2c 20 70 2d 3e     , p->zDb, p->
30b74 7a 4e 61 6d 65 2c 20 7a 4e 61 6d 65 0a 20 20 29  zName, zName.  )
30b75 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a  ;.  if( zSql ){.
30b76 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
30b77 5f 65 78 65 63 28 70 2d 3e 64 62 2c 20 7a 53 71  _exec(p->db, zSq
30b78 6c 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20  l, 0, 0, 0);.   
30b79 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53   sqlite3_free(zS
30b7a 71 6c 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ql);.  }.  retur
30b7b 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63 20  n rc;.}..static 
30b7c 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 6d 6f  const sqlite3_mo
30b7d 64 75 6c 65 20 66 74 73 33 4d 6f 64 75 6c 65 20  dule fts3Module 
30b7e 3d 20 7b 0a 20 20 2f 2a 20 69 56 65 72 73 69 6f  = {.  /* iVersio
30b7f 6e 20 20 20 20 20 20 2a 2f 20 30 2c 0a 20 20 2f  n      */ 0,.  /
30b80 2a 20 78 43 72 65 61 74 65 20 20 20 20 20 20 20  * xCreate       
30b81 2a 2f 20 66 75 6c 6c 74 65 78 74 43 72 65 61 74  */ fulltextCreat
30b82 65 2c 0a 20 20 2f 2a 20 78 43 6f 6e 6e 65 63 74  e,.  /* xConnect
30b83 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
30b84 74 43 6f 6e 6e 65 63 74 2c 0a 20 20 2f 2a 20 78  tConnect,.  /* x
30b85 42 65 73 74 49 6e 64 65 78 20 20 20 20 2a 2f 20  BestIndex    */ 
30b86 66 75 6c 6c 74 65 78 74 42 65 73 74 49 6e 64 65  fulltextBestInde
30b87 78 2c 0a 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e  x,.  /* xDisconn
30b88 65 63 74 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78  ect   */ fulltex
30b89 74 44 69 73 63 6f 6e 6e 65 63 74 2c 0a 20 20 2f  tDisconnect,.  /
30b8a 2a 20 78 44 65 73 74 72 6f 79 20 20 20 20 20 20  * xDestroy      
30b8b 2a 2f 20 66 75 6c 6c 74 65 78 74 44 65 73 74 72  */ fulltextDestr
30b8c 6f 79 2c 0a 20 20 2f 2a 20 78 4f 70 65 6e 20 20  oy,.  /* xOpen  
30b8d 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
30b8e 78 74 4f 70 65 6e 2c 0a 20 20 2f 2a 20 78 43 6c  xtOpen,.  /* xCl
30b8f 6f 73 65 20 20 20 20 20 20 20 20 2a 2f 20 66 75  ose        */ fu
30b90 6c 6c 74 65 78 74 43 6c 6f 73 65 2c 0a 20 20 2f  lltextClose,.  /
30b91 2a 20 78 46 69 6c 74 65 72 20 20 20 20 20 20 20  * xFilter       
30b92 2a 2f 20 66 75 6c 6c 74 65 78 74 46 69 6c 74 65  */ fulltextFilte
30b93 72 2c 0a 20 20 2f 2a 20 78 4e 65 78 74 20 20 20  r,.  /* xNext   
30b94 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78        */ fulltex
30b95 74 4e 65 78 74 2c 0a 20 20 2f 2a 20 78 45 6f 66  tNext,.  /* xEof
30b96 20 20 20 20 20 20 20 20 20 20 2a 2f 20 66 75 6c            */ ful
30b97 6c 74 65 78 74 45 6f 66 2c 0a 20 20 2f 2a 20 78  ltextEof,.  /* x
30b98 43 6f 6c 75 6d 6e 20 20 20 20 20 20 20 2a 2f 20  Column       */ 
30b99 66 75 6c 6c 74 65 78 74 43 6f 6c 75 6d 6e 2c 0a  fulltextColumn,.
30b9a 20 20 2f 2a 20 78 52 6f 77 69 64 20 20 20 20 20    /* xRowid     
30b9b 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 52 6f     */ fulltextRo
30b9c 77 69 64 2c 0a 20 20 2f 2a 20 78 55 70 64 61 74  wid,.  /* xUpdat
30b9d 65 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74  e       */ fullt
30b9e 65 78 74 55 70 64 61 74 65 2c 0a 20 20 2f 2a 20  extUpdate,.  /* 
30b9f 78 42 65 67 69 6e 20 20 20 20 20 20 20 20 2a 2f  xBegin        */
30ba0 20 66 75 6c 6c 74 65 78 74 42 65 67 69 6e 2c 0a   fulltextBegin,.
30ba1 20 20 2f 2a 20 78 53 79 6e 63 20 20 20 20 20 20    /* xSync      
30ba2 20 20 20 2a 2f 20 66 75 6c 6c 74 65 78 74 53 79     */ fulltextSy
30ba3 6e 63 2c 0a 20 20 2f 2a 20 78 43 6f 6d 6d 69 74  nc,.  /* xCommit
30ba4 20 20 20 20 20 20 20 2a 2f 20 66 75 6c 6c 74 65         */ fullte
30ba5 78 74 43 6f 6d 6d 69 74 2c 0a 20 20 2f 2a 20 78  xtCommit,.  /* x
30ba6 52 6f 6c 6c 62 61 63 6b 20 20 20 20 20 2a 2f 20  Rollback     */ 
30ba7 66 75 6c 6c 74 65 78 74 52 6f 6c 6c 62 61 63 6b  fulltextRollback
30ba8 2c 0a 20 20 2f 2a 20 78 46 69 6e 64 46 75 6e 63  ,.  /* xFindFunc
30ba9 74 69 6f 6e 20 2a 2f 20 66 75 6c 6c 74 65 78 74  tion */ fulltext
30baa 46 69 6e 64 46 75 6e 63 74 69 6f 6e 2c 0a 20 20  FindFunction,.  
30bab 2f 2a 20 78 52 65 6e 61 6d 65 20 2a 2f 20 20 20  /* xRename */   
30bac 20 20 20 20 66 75 6c 6c 74 65 78 74 52 65 6e 61      fulltextRena
30bad 6d 65 2c 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 76  me,.};..static v
30bae 6f 69 64 20 68 61 73 68 44 65 73 74 72 6f 79 28  oid hashDestroy(
30baf 76 6f 69 64 20 2a 70 29 7b 0a 20 20 66 74 73 33  void *p){.  fts3
30bb0 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20 28 66  Hash *pHash = (f
30bb1 74 73 33 48 61 73 68 20 2a 29 70 3b 0a 20 20 73  ts3Hash *)p;.  s
30bb2 71 6c 69 74 65 33 46 74 73 33 48 61 73 68 43 6c  qlite3Fts3HashCl
30bb3 65 61 72 28 70 48 61 73 68 29 3b 0a 20 20 73 71  ear(pHash);.  sq
30bb4 6c 69 74 65 33 5f 66 72 65 65 28 70 48 61 73 68  lite3_free(pHash
30bb5 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  );.}../*.** The 
30bb6 66 74 73 33 20 62 75 69 6c 74 2d 69 6e 20 74 6f  fts3 built-in to
30bb7 6b 65 6e 69 7a 65 72 73 20 2d 20 22 73 69 6d 70  kenizers - "simp
30bb8 6c 65 22 20 61 6e 64 20 22 70 6f 72 74 65 72 22  le" and "porter"
30bb9 20 2d 20 61 72 65 20 69 6d 70 6c 65 6d 65 6e 74   - are implement
30bba 65 64 0a 2a 2a 20 69 6e 20 66 69 6c 65 73 20 66  ed.** in files f
30bbb 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 31 2e 63  ts3_tokenizer1.c
30bbc 20 61 6e 64 20 66 74 73 33 5f 70 6f 72 74 65 72   and fts3_porter
30bbd 2e 63 20 72 65 73 70 65 63 74 69 76 65 6c 79 2e  .c respectively.
30bbe 20 54 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 0a 2a   The following.*
30bbf 2a 20 74 77 6f 20 66 6f 72 77 61 72 64 20 64 65  * two forward de
30bc0 63 6c 61 72 61 74 69 6f 6e 73 20 61 72 65 20 66  clarations are f
30bc1 6f 72 20 66 75 6e 63 74 69 6f 6e 73 20 64 65 63  or functions dec
30bc2 6c 61 72 65 64 20 69 6e 20 74 68 65 73 65 20 66  lared in these f
30bc3 69 6c 65 73 0a 2a 2a 20 75 73 65 64 20 74 6f 20  iles.** used to 
30bc4 72 65 74 72 69 65 76 65 20 74 68 65 20 72 65 73  retrieve the res
30bc5 70 65 63 74 69 76 65 20 69 6d 70 6c 65 6d 65 6e  pective implemen
30bc6 74 61 74 69 6f 6e 73 2e 0a 2a 2a 0a 2a 2a 20 43  tations..**.** C
30bc7 61 6c 6c 69 6e 67 20 73 71 6c 69 74 65 33 46 74  alling sqlite3Ft
30bc8 73 33 53 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65  s3SimpleTokenize
30bc9 72 4d 6f 64 75 6c 65 28 29 20 73 65 74 73 20 74  rModule() sets t
30bca 68 65 20 76 61 6c 75 65 20 70 6f 69 6e 74 65 64  he value pointed
30bcb 0a 2a 2a 20 74 6f 20 62 79 20 74 68 65 20 61 72  .** to by the ar
30bcc 67 75 6d 65 6e 74 20 74 6f 20 70 6f 69 6e 74 20  gument to point 
30bcd 61 20 74 68 65 20 22 73 69 6d 70 6c 65 22 20 74  a the "simple" t
30bce 6f 6b 65 6e 69 7a 65 72 20 69 6d 70 6c 65 6d 65  okenizer impleme
30bcf 6e 74 61 74 69 6f 6e 2e 0a 2a 2a 20 46 75 6e 63  ntation..** Func
30bd0 74 69 6f 6e 20 2e 2e 2e 50 6f 72 74 65 72 54 6f  tion ...PorterTo
30bd1 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 29 20  kenizerModule() 
30bd2 73 65 74 73 20 2a 70 4d 6f 64 75 6c 65 20 74 6f  sets *pModule to
30bd3 20 70 6f 69 6e 74 20 74 6f 20 74 68 65 0a 2a 2a   point to the.**
30bd4 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65   porter tokenize
30bd5 72 2f 73 74 65 6d 6d 65 72 20 69 6d 70 6c 65 6d  r/stemmer implem
30bd6 65 6e 74 61 74 69 6f 6e 2e 0a 2a 2f 0a 53 51 4c  entation..*/.SQL
30bd7 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
30bd8 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70   sqlite3Fts3Simp
30bd9 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  leTokenizerModul
30bda 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
30bdb 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
30bdc 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c  **ppModule);.SQL
30bdd 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
30bde 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
30bdf 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
30be0 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
30be1 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74  zer_module const
30be2 2a 2a 70 70 4d 6f 64 75 6c 65 29 3b 0a 53 51 4c  **ppModule);.SQL
30be3 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
30be4 20 73 71 6c 69 74 65 33 46 74 73 33 49 63 75 54   sqlite3Fts3IcuT
30be5 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73  okenizerModule(s
30be6 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
30be7 5f 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70  _module const**p
30be8 70 4d 6f 64 75 6c 65 29 3b 0a 0a 53 51 4c 49 54  pModule);..SQLIT
30be9 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71  E_PRIVATE int sq
30bea 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48 61 73  lite3Fts3InitHas
30beb 68 54 61 62 6c 65 28 73 71 6c 69 74 65 33 20 2a  hTable(sqlite3 *
30bec 2c 20 66 74 73 33 48 61 73 68 20 2a 2c 20 63 6f  , fts3Hash *, co
30bed 6e 73 74 20 63 68 61 72 20 2a 29 3b 0a 0a 2f 2a  nst char *);../*
30bee 0a 2a 2a 20 49 6e 69 74 69 61 6c 69 73 65 20 74  .** Initialise t
30bef 68 65 20 66 74 73 33 20 65 78 74 65 6e 73 69 6f  he fts3 extensio
30bf0 6e 2e 20 49 66 20 74 68 69 73 20 65 78 74 65 6e  n. If this exten
30bf1 73 69 6f 6e 20 69 73 20 62 75 69 6c 74 20 61 73  sion is built as
30bf2 20 70 61 72 74 0a 2a 2a 20 6f 66 20 74 68 65 20   part.** of the 
30bf3 73 71 6c 69 74 65 20 6c 69 62 72 61 72 79 2c 20  sqlite library, 
30bf4 74 68 65 6e 20 74 68 69 73 20 66 75 6e 63 74 69  then this functi
30bf5 6f 6e 20 69 73 20 63 61 6c 6c 65 64 20 64 69 72  on is called dir
30bf6 65 63 74 6c 79 20 62 79 0a 2a 2a 20 53 51 4c 69  ectly by.** SQLi
30bf7 74 65 2e 20 49 66 20 66 74 73 33 20 69 73 20 62  te. If fts3 is b
30bf8 75 69 6c 74 20 61 73 20 61 20 64 79 6e 61 6d 69  uilt as a dynami
30bf9 63 61 6c 6c 79 20 6c 6f 61 64 61 62 6c 65 20 65  cally loadable e
30bfa 78 74 65 6e 73 69 6f 6e 2c 20 74 68 69 73 0a 2a  xtension, this.*
30bfb 2a 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 63 61  * function is ca
30bfc 6c 6c 65 64 20 62 79 20 74 68 65 20 73 71 6c 69  lled by the sqli
30bfd 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e  te3_extension_in
30bfe 69 74 28 29 20 65 6e 74 72 79 20 70 6f 69 6e 74  it() entry point
30bff 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
30c00 41 54 45 20 69 6e 74 20 73 71 6c 69 74 65 33 46  ATE int sqlite3F
30c01 74 73 33 49 6e 69 74 28 73 71 6c 69 74 65 33 20  ts3Init(sqlite3 
30c02 2a 64 62 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d  *db){.  int rc =
30c03 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 66 74   SQLITE_OK;.  ft
30c04 73 33 48 61 73 68 20 2a 70 48 61 73 68 20 3d 20  s3Hash *pHash = 
30c05 30 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74  0;.  const sqlit
30c06 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
30c07 75 6c 65 20 2a 70 53 69 6d 70 6c 65 20 3d 20 30  ule *pSimple = 0
30c08 3b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65  ;.  const sqlite
30c09 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75  3_tokenizer_modu
30c0a 6c 65 20 2a 70 50 6f 72 74 65 72 20 3d 20 30 3b  le *pPorter = 0;
30c0b 0a 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33  .  const sqlite3
30c0c 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c  _tokenizer_modul
30c0d 65 20 2a 70 49 63 75 20 3d 20 30 3b 0a 0a 20 20  e *pIcu = 0;..  
30c0e 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c  sqlite3Fts3Simpl
30c0f 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
30c10 28 26 70 53 69 6d 70 6c 65 29 3b 0a 20 20 73 71  (&pSimple);.  sq
30c11 6c 69 74 65 33 46 74 73 33 50 6f 72 74 65 72 54  lite3Fts3PorterT
30c12 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 26  okenizerModule(&
30c13 70 50 6f 72 74 65 72 29 3b 0a 23 69 66 64 65 66  pPorter);.#ifdef
30c14 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49   SQLITE_ENABLE_I
30c15 43 55 0a 20 20 73 71 6c 69 74 65 33 46 74 73 33  CU.  sqlite3Fts3
30c16 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  IcuTokenizerModu
30c17 6c 65 28 26 70 49 63 75 29 3b 0a 23 65 6e 64 69  le(&pIcu);.#endi
30c18 66 0a 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65  f..  /* Allocate
30c19 20 61 6e 64 20 69 6e 69 74 69 61 6c 69 73 65 20   and initialise 
30c1a 74 68 65 20 68 61 73 68 2d 74 61 62 6c 65 20 75  the hash-table u
30c1b 73 65 64 20 74 6f 20 73 74 6f 72 65 20 74 6f 6b  sed to store tok
30c1c 65 6e 69 7a 65 72 73 2e 20 2a 2f 0a 20 20 70 48  enizers. */.  pH
30c1d 61 73 68 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ash = sqlite3_ma
30c1e 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 66 74 73 33  lloc(sizeof(fts3
30c1f 48 61 73 68 29 29 3b 0a 20 20 69 66 28 20 21 70  Hash));.  if( !p
30c20 48 61 73 68 20 29 7b 0a 20 20 20 20 72 63 20 3d  Hash ){.    rc =
30c21 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
30c22 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c 69   }else{.    sqli
30c23 74 65 33 46 74 73 33 48 61 73 68 49 6e 69 74 28  te3Fts3HashInit(
30c24 70 48 61 73 68 2c 20 46 54 53 33 5f 48 41 53 48  pHash, FTS3_HASH
30c25 5f 53 54 52 49 4e 47 2c 20 31 29 3b 0a 20 20 7d  _STRING, 1);.  }
30c26 0a 0a 20 20 2f 2a 20 4c 6f 61 64 20 74 68 65 20  ..  /* Load the 
30c27 62 75 69 6c 74 2d 69 6e 20 74 6f 6b 65 6e 69 7a  built-in tokeniz
30c28 65 72 73 20 69 6e 74 6f 20 74 68 65 20 68 61 73  ers into the has
30c29 68 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69 66 28  h table */.  if(
30c2a 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
30c2b 7b 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65  {.    if( sqlite
30c2c 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28  3Fts3HashInsert(
30c2d 70 48 61 73 68 2c 20 22 73 69 6d 70 6c 65 22 2c  pHash, "simple",
30c2e 20 37 2c 20 28 76 6f 69 64 20 2a 29 70 53 69 6d   7, (void *)pSim
30c2f 70 6c 65 29 0a 20 20 20 20 20 7c 7c 20 73 71 6c  ple).     || sql
30c30 69 74 65 33 46 74 73 33 48 61 73 68 49 6e 73 65  ite3Fts3HashInse
30c31 72 74 28 70 48 61 73 68 2c 20 22 70 6f 72 74 65  rt(pHash, "porte
30c32 72 22 2c 20 37 2c 20 28 76 6f 69 64 20 2a 29 70  r", 7, (void *)p
30c33 50 6f 72 74 65 72 29 20 0a 20 20 20 20 20 7c 7c  Porter) .     ||
30c34 20 28 70 49 63 75 20 26 26 20 73 71 6c 69 74 65   (pIcu && sqlite
30c35 33 46 74 73 33 48 61 73 68 49 6e 73 65 72 74 28  3Fts3HashInsert(
30c36 70 48 61 73 68 2c 20 22 69 63 75 22 2c 20 34 2c  pHash, "icu", 4,
30c37 20 28 76 6f 69 64 20 2a 29 70 49 63 75 29 29 0a   (void *)pIcu)).
30c38 20 20 20 20 29 7b 0a 20 20 20 20 20 20 72 63 20      ){.      rc 
30c39 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  = SQLITE_NOMEM;.
30c3a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20      }.  }..  /* 
30c3b 43 72 65 61 74 65 20 74 68 65 20 76 69 72 74 75  Create the virtu
30c3c 61 6c 20 74 61 62 6c 65 20 77 72 61 70 70 65 72  al table wrapper
30c3d 20 61 72 6f 75 6e 64 20 74 68 65 20 68 61 73 68   around the hash
30c3e 2d 74 61 62 6c 65 20 61 6e 64 20 6f 76 65 72 6c  -table and overl
30c3f 6f 61 64 20 0a 20 20 2a 2a 20 74 68 65 20 74 77  oad .  ** the tw
30c40 6f 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  o scalar functio
30c41 6e 73 2e 20 49 66 20 74 68 69 73 20 69 73 20 73  ns. If this is s
30c42 75 63 63 65 73 73 66 75 6c 2c 20 72 65 67 69 73  uccessful, regis
30c43 74 65 72 20 74 68 65 0a 20 20 2a 2a 20 6d 6f 64  ter the.  ** mod
30c44 75 6c 65 20 77 69 74 68 20 73 71 6c 69 74 65 2e  ule with sqlite.
30c45 0a 20 20 2a 2f 0a 20 20 69 66 28 20 53 51 4c 49  .  */.  if( SQLI
30c46 54 45 5f 4f 4b 3d 3d 72 63 20 0a 20 20 20 26 26  TE_OK==rc .   &&
30c47 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20   SQLITE_OK==(rc 
30c48 3d 20 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69  = sqlite3Fts3Ini
30c49 74 48 61 73 68 54 61 62 6c 65 28 64 62 2c 20 70  tHashTable(db, p
30c4a 48 61 73 68 2c 20 22 66 74 73 33 5f 74 6f 6b 65  Hash, "fts3_toke
30c4b 6e 69 7a 65 72 22 29 29 0a 20 20 20 26 26 20 53  nizer")).   && S
30c4c 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63 20 3d 20  QLITE_OK==(rc = 
30c4d 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c 6f 61 64  sqlite3_overload
30c4e 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 73  _function(db, "s
30c4f 6e 69 70 70 65 74 22 2c 20 2d 31 29 29 0a 20 20  nippet", -1)).  
30c50 20 26 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28   && SQLITE_OK==(
30c51 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65  rc = sqlite3_ove
30c52 72 6c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64  rload_function(d
30c53 62 2c 20 22 6f 66 66 73 65 74 73 22 2c 20 2d 31  b, "offsets", -1
30c54 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45 5f  )).   && SQLITE_
30c55 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74 65  OK==(rc = sqlite
30c56 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63 74  3_overload_funct
30c57 69 6f 6e 28 64 62 2c 20 22 6f 70 74 69 6d 69 7a  ion(db, "optimiz
30c58 65 22 2c 20 2d 31 29 29 0a 23 69 66 64 65 66 20  e", -1)).#ifdef 
30c59 53 51 4c 49 54 45 5f 54 45 53 54 0a 20 20 20 26  SQLITE_TEST.   &
30c5a 26 20 53 51 4c 49 54 45 5f 4f 4b 3d 3d 28 72 63  & SQLITE_OK==(rc
30c5b 20 3d 20 73 71 6c 69 74 65 33 5f 6f 76 65 72 6c   = sqlite3_overl
30c5c 6f 61 64 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c  oad_function(db,
30c5d 20 22 64 75 6d 70 5f 74 65 72 6d 73 22 2c 20 2d   "dump_terms", -
30c5e 31 29 29 0a 20 20 20 26 26 20 53 51 4c 49 54 45  1)).   && SQLITE
30c5f 5f 4f 4b 3d 3d 28 72 63 20 3d 20 73 71 6c 69 74  _OK==(rc = sqlit
30c60 65 33 5f 6f 76 65 72 6c 6f 61 64 5f 66 75 6e 63  e3_overload_func
30c61 74 69 6f 6e 28 64 62 2c 20 22 64 75 6d 70 5f 64  tion(db, "dump_d
30c62 6f 63 6c 69 73 74 22 2c 20 2d 31 29 29 0a 23 65  oclist", -1)).#e
30c63 6e 64 69 66 0a 20 20 29 7b 0a 20 20 20 20 72 65  ndif.  ){.    re
30c64 74 75 72 6e 20 73 71 6c 69 74 65 33 5f 63 72 65  turn sqlite3_cre
30c65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28 0a 20  ate_module_v2(. 
30c66 20 20 20 20 20 20 20 64 62 2c 20 22 66 74 73 33         db, "fts3
30c67 22 2c 20 26 66 74 73 33 4d 6f 64 75 6c 65 2c 20  ", &fts3Module, 
30c68 28 76 6f 69 64 20 2a 29 70 48 61 73 68 2c 20 68  (void *)pHash, h
30c69 61 73 68 44 65 73 74 72 6f 79 0a 20 20 20 20 29  ashDestroy.    )
30c6a 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 41 6e 20 65  ;.  }..  /* An e
30c6b 72 72 6f 72 20 68 61 73 20 6f 63 63 75 72 65 64  rror has occured
30c6c 2e 20 44 65 6c 65 74 65 20 74 68 65 20 68 61 73  . Delete the has
30c6d 68 20 74 61 62 6c 65 20 61 6e 64 20 72 65 74 75  h table and retu
30c6e 72 6e 20 74 68 65 20 65 72 72 6f 72 20 63 6f 64  rn the error cod
30c6f 65 2e 20 2a 2f 0a 20 20 61 73 73 65 72 74 28 20  e. */.  assert( 
30c70 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 3b  rc!=SQLITE_OK );
30c71 0a 20 20 69 66 28 20 70 48 61 73 68 20 29 7b 0a  .  if( pHash ){.
30c72 20 20 20 20 73 71 6c 69 74 65 33 46 74 73 33 48      sqlite3Fts3H
30c73 61 73 68 43 6c 65 61 72 28 70 48 61 73 68 29 3b  ashClear(pHash);
30c74 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
30c75 65 28 70 48 61 73 68 29 3b 0a 20 20 7d 0a 20 20  e(pHash);.  }.  
30c76 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69  return rc;.}..#i
30c77 66 20 21 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53  f !SQLITE_CORE.S
30c78 51 4c 49 54 45 5f 41 50 49 20 69 6e 74 20 73 71  QLITE_API int sq
30c79 6c 69 74 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f  lite3_extension_
30c7a 69 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20  init(.  sqlite3 
30c7b 2a 64 62 2c 20 0a 20 20 63 68 61 72 20 2a 2a 70  *db, .  char **p
30c7c 7a 45 72 72 4d 73 67 2c 0a 20 20 63 6f 6e 73 74  zErrMsg,.  const
30c7d 20 73 71 6c 69 74 65 33 5f 61 70 69 5f 72 6f 75   sqlite3_api_rou
30c7e 74 69 6e 65 73 20 2a 70 41 70 69 0a 29 7b 0a 20  tines *pApi.){. 
30c7f 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53 49 4f   SQLITE_EXTENSIO
30c80 4e 5f 49 4e 49 54 32 28 70 41 70 69 29 0a 20 20  N_INIT2(pApi).  
30c81 72 65 74 75 72 6e 20 73 71 6c 69 74 65 33 46 74  return sqlite3Ft
30c82 73 33 49 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65  s3Init(db);.}.#e
30c83 6e 64 69 66 0a 0a 23 65 6e 64 69 66 20 2f 2a 20  ndif..#endif /* 
30c84 21 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f  !defined(SQLITE_
30c85 43 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64  CORE) || defined
30c86 28 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46  (SQLITE_ENABLE_F
30c87 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a  TS3) */../******
30c88 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20  ******** End of 
30c89 66 74 73 33 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  fts3.c *********
30c8a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30c8b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30c8c 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a  *******/./******
30c8d 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e 20 66  ******** Begin f
30c8e 69 6c 65 20 66 74 73 33 5f 68 61 73 68 2e 63 20  ile fts3_hash.c 
30c8f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30c90 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30c91 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a 20 32  *******/./*.** 2
30c92 30 30 31 20 53 65 70 74 65 6d 62 65 72 20 32 32  001 September 22
30c93 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f  .**.** The autho
30c94 72 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79  r disclaims copy
30c95 72 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f  right to this so
30c96 75 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70  urce code.  In p
30c97 6c 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67  lace of.** a leg
30c98 61 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20  al notice, here 
30c99 69 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a  is a blessing:.*
30c9a 2a 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20  *.**    May you 
30c9b 64 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20  do good and not 
30c9c 65 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  evil..**    May 
30c9d 79 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65  you find forgive
30c9e 6e 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c  ness for yoursel
30c9f 66 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74  f and forgive ot
30ca0 68 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20  hers..**    May 
30ca1 79 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79  you share freely
30ca2 2c 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d  , never taking m
30ca3 6f 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76  ore than you giv
30ca4 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  e..**.**********
30ca5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ca6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ca7 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30ca8 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a  ***************.
30ca9 2a 2a 20 54 68 69 73 20 69 73 20 74 68 65 20 69  ** This is the i
30caa 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
30cab 20 67 65 6e 65 72 69 63 20 68 61 73 68 2d 74 61   generic hash-ta
30cac 62 6c 65 73 20 75 73 65 64 20 69 6e 20 53 51 4c  bles used in SQL
30cad 69 74 65 2e 0a 2a 2a 20 57 65 27 76 65 20 6d 6f  ite..** We've mo
30cae 64 69 66 69 65 64 20 69 74 20 73 6c 69 67 68 74  dified it slight
30caf 6c 79 20 74 6f 20 73 65 72 76 65 20 61 73 20 61  ly to serve as a
30cb0 20 73 74 61 6e 64 61 6c 6f 6e 65 20 68 61 73 68   standalone hash
30cb1 20 74 61 62 6c 65 0a 2a 2a 20 69 6d 70 6c 65 6d   table.** implem
30cb2 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  entation for the
30cb3 20 66 75 6c 6c 2d 74 65 78 74 20 69 6e 64 65 78   full-text index
30cb4 69 6e 67 20 6d 6f 64 75 6c 65 2e 0a 2a 2f 0a 0a  ing module..*/..
30cb5 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65 20 69  /*.** The code i
30cb6 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73 20 6f  n this file is o
30cb7 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69 66 3a  nly compiled if:
30cb8 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54 68 65  .**.**     * The
30cb9 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69 73 20   FTS3 module is 
30cba 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73 20 61  being built as a
30cbb 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a 20 20  n extension.**  
30cbc 20 20 20 20 20 28 69 6e 20 77 68 69 63 68 20 63       (in which c
30cbd 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52 45 20  ase SQLITE_CORE 
30cbe 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64 29 2c  is not defined),
30cbf 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20   or.**.**     * 
30cc0 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20  The FTS3 module 
30cc1 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 69  is being built i
30cc2 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f 66 0a  nto the core of.
30cc3 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74 65 20  **       SQLite 
30cc4 28 69 6e 20 77 68 69 63 68 20 63 61 73 65 20 53  (in which case S
30cc5 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
30cc6 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e 0a 2a  3 is defined)..*
30cc7 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28 53  /.#if !defined(S
30cc8 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64  QLITE_CORE) || d
30cc9 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e  efined(SQLITE_EN
30cca 41 42 4c 45 5f 46 54 53 33 29 0a 0a 0a 0a 2f 2a  ABLE_FTS3)..../*
30ccb 0a 2a 2a 20 4d 61 6c 6c 6f 63 20 61 6e 64 20 46  .** Malloc and F
30ccc 72 65 65 20 66 75 6e 63 74 69 6f 6e 73 0a 2a 2f  ree functions.*/
30ccd 0a 73 74 61 74 69 63 20 76 6f 69 64 20 2a 66 74  .static void *ft
30cce 73 33 48 61 73 68 4d 61 6c 6c 6f 63 28 69 6e 74  s3HashMalloc(int
30ccf 20 6e 29 7b 0a 20 20 76 6f 69 64 20 2a 70 20 3d   n){.  void *p =
30cd0 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28   sqlite3_malloc(
30cd1 6e 29 3b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  n);.  if( p ){. 
30cd2 20 20 20 6d 65 6d 73 65 74 28 70 2c 20 30 2c 20     memset(p, 0, 
30cd3 6e 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e  n);.  }.  return
30cd4 20 70 3b 0a 7d 0a 73 74 61 74 69 63 20 76 6f 69   p;.}.static voi
30cd5 64 20 66 74 73 33 48 61 73 68 46 72 65 65 28 76  d fts3HashFree(v
30cd6 6f 69 64 20 2a 70 29 7b 0a 20 20 73 71 6c 69 74  oid *p){.  sqlit
30cd7 65 33 5f 66 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f  e3_free(p);.}../
30cd8 2a 20 54 75 72 6e 20 62 75 6c 6b 20 6d 65 6d 6f  * Turn bulk memo
30cd9 72 79 20 69 6e 74 6f 20 61 20 68 61 73 68 20 74  ry into a hash t
30cda 61 62 6c 65 20 6f 62 6a 65 63 74 20 62 79 20 69  able object by i
30cdb 6e 69 74 69 61 6c 69 7a 69 6e 67 20 74 68 65 0a  nitializing the.
30cdc 2a 2a 20 66 69 65 6c 64 73 20 6f 66 20 74 68 65  ** fields of the
30cdd 20 48 61 73 68 20 73 74 72 75 63 74 75 72 65 2e   Hash structure.
30cde 0a 2a 2a 0a 2a 2a 20 22 70 4e 65 77 22 20 69 73  .**.** "pNew" is
30cdf 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 74 68   a pointer to th
30ce0 65 20 68 61 73 68 20 74 61 62 6c 65 20 74 68 61  e hash table tha
30ce1 74 20 69 73 20 74 6f 20 62 65 20 69 6e 69 74 69  t is to be initi
30ce2 61 6c 69 7a 65 64 2e 0a 2a 2a 20 6b 65 79 43 6c  alized..** keyCl
30ce3 61 73 73 20 69 73 20 6f 6e 65 20 6f 66 20 74 68  ass is one of th
30ce4 65 20 63 6f 6e 73 74 61 6e 74 73 20 0a 2a 2a 20  e constants .** 
30ce5 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59  FTS3_HASH_BINARY
30ce6 20 6f 72 20 46 54 53 33 5f 48 41 53 48 5f 53 54   or FTS3_HASH_ST
30ce7 52 49 4e 47 2e 20 20 54 68 65 20 76 61 6c 75 65  RING.  The value
30ce8 20 6f 66 20 6b 65 79 43 6c 61 73 73 20 0a 2a 2a   of keyClass .**
30ce9 20 64 65 74 65 72 6d 69 6e 65 73 20 77 68 61 74   determines what
30cea 20 6b 69 6e 64 20 6f 66 20 6b 65 79 20 74 68 65   kind of key the
30ceb 20 68 61 73 68 20 74 61 62 6c 65 20 77 69 6c 6c   hash table will
30cec 20 75 73 65 2e 20 20 22 63 6f 70 79 4b 65 79 22   use.  "copyKey"
30ced 20 69 73 0a 2a 2a 20 74 72 75 65 20 69 66 20 74   is.** true if t
30cee 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 73 68  he hash table sh
30cef 6f 75 6c 64 20 6d 61 6b 65 20 69 74 73 20 6f 77  ould make its ow
30cf0 6e 20 70 72 69 76 61 74 65 20 63 6f 70 79 20 6f  n private copy o
30cf1 66 20 6b 65 79 73 20 61 6e 64 0a 2a 2a 20 66 61  f keys and.** fa
30cf2 6c 73 65 20 69 66 20 69 74 20 73 68 6f 75 6c 64  lse if it should
30cf3 20 6a 75 73 74 20 75 73 65 20 74 68 65 20 73 75   just use the su
30cf4 70 70 6c 69 65 64 20 70 6f 69 6e 74 65 72 2e 0a  pplied pointer..
30cf5 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
30cf6 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
30cf7 73 33 48 61 73 68 49 6e 69 74 28 66 74 73 33 48  s3HashInit(fts3H
30cf8 61 73 68 20 2a 70 4e 65 77 2c 20 69 6e 74 20 6b  ash *pNew, int k
30cf9 65 79 43 6c 61 73 73 2c 20 69 6e 74 20 63 6f 70  eyClass, int cop
30cfa 79 4b 65 79 29 7b 0a 20 20 61 73 73 65 72 74 28  yKey){.  assert(
30cfb 20 70 4e 65 77 21 3d 30 20 29 3b 0a 20 20 61 73   pNew!=0 );.  as
30cfc 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73 3e 3d  sert( keyClass>=
30cfd 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e 47  FTS3_HASH_STRING
30cfe 20 26 26 20 6b 65 79 43 6c 61 73 73 3c 3d 46 54   && keyClass<=FT
30cff 53 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29  S3_HASH_BINARY )
30d00 3b 0a 20 20 70 4e 65 77 2d 3e 6b 65 79 43 6c 61  ;.  pNew->keyCla
30d01 73 73 20 3d 20 6b 65 79 43 6c 61 73 73 3b 0a 20  ss = keyClass;. 
30d02 20 70 4e 65 77 2d 3e 63 6f 70 79 4b 65 79 20 3d   pNew->copyKey =
30d03 20 63 6f 70 79 4b 65 79 3b 0a 20 20 70 4e 65 77   copyKey;.  pNew
30d04 2d 3e 66 69 72 73 74 20 3d 20 30 3b 0a 20 20 70  ->first = 0;.  p
30d05 4e 65 77 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b 0a  New->count = 0;.
30d06 20 20 70 4e 65 77 2d 3e 68 74 73 69 7a 65 20 3d    pNew->htsize =
30d07 20 30 3b 0a 20 20 70 4e 65 77 2d 3e 68 74 20 3d   0;.  pNew->ht =
30d08 20 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65   0;.}../* Remove
30d09 20 61 6c 6c 20 65 6e 74 72 69 65 73 20 66 72 6f   all entries fro
30d0a 6d 20 61 20 68 61 73 68 20 74 61 62 6c 65 2e 20  m a hash table. 
30d0b 20 52 65 63 6c 61 69 6d 20 61 6c 6c 20 6d 65 6d   Reclaim all mem
30d0c 6f 72 79 2e 0a 2a 2a 20 43 61 6c 6c 20 74 68 69  ory..** Call thi
30d0d 73 20 72 6f 75 74 69 6e 65 20 74 6f 20 64 65 6c  s routine to del
30d0e 65 74 65 20 61 20 68 61 73 68 20 74 61 62 6c 65  ete a hash table
30d0f 20 6f 72 20 74 6f 20 72 65 73 65 74 20 61 20 68   or to reset a h
30d10 61 73 68 20 74 61 62 6c 65 0a 2a 2a 20 74 6f 20  ash table.** to 
30d11 74 68 65 20 65 6d 70 74 79 20 73 74 61 74 65 2e  the empty state.
30d12 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41  .*/.SQLITE_PRIVA
30d13 54 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46  TE void sqlite3F
30d14 74 73 33 48 61 73 68 43 6c 65 61 72 28 66 74 73  ts3HashClear(fts
30d15 33 48 61 73 68 20 2a 70 48 29 7b 0a 20 20 66 74  3Hash *pH){.  ft
30d16 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d  s3HashElem *elem
30d17 3b 20 20 20 20 20 20 20 20 20 2f 2a 20 46 6f 72  ;         /* For
30d18 20 6c 6f 6f 70 69 6e 67 20 6f 76 65 72 20 61 6c   looping over al
30d19 6c 20 65 6c 65 6d 65 6e 74 73 20 6f 66 20 74 68  l elements of th
30d1a 65 20 74 61 62 6c 65 20 2a 2f 0a 0a 20 20 61 73  e table */..  as
30d1b 73 65 72 74 28 20 70 48 21 3d 30 20 29 3b 0a 20  sert( pH!=0 );. 
30d1c 20 65 6c 65 6d 20 3d 20 70 48 2d 3e 66 69 72 73   elem = pH->firs
30d1d 74 3b 0a 20 20 70 48 2d 3e 66 69 72 73 74 20 3d  t;.  pH->first =
30d1e 20 30 3b 0a 20 20 66 74 73 33 48 61 73 68 46 72   0;.  fts3HashFr
30d1f 65 65 28 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48  ee(pH->ht);.  pH
30d20 2d 3e 68 74 20 3d 20 30 3b 0a 20 20 70 48 2d 3e  ->ht = 0;.  pH->
30d21 68 74 73 69 7a 65 20 3d 20 30 3b 0a 20 20 77 68  htsize = 0;.  wh
30d22 69 6c 65 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20  ile( elem ){.   
30d23 20 66 74 73 33 48 61 73 68 45 6c 65 6d 20 2a 6e   fts3HashElem *n
30d24 65 78 74 5f 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d  ext_elem = elem-
30d25 3e 6e 65 78 74 3b 0a 20 20 20 20 69 66 28 20 70  >next;.    if( p
30d26 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26 20 65 6c  H->copyKey && el
30d27 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20 20 20 20  em->pKey ){.    
30d28 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28 65    fts3HashFree(e
30d29 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 20 20  lem->pKey);.    
30d2a 7d 0a 20 20 20 20 66 74 73 33 48 61 73 68 46 72  }.    fts3HashFr
30d2b 65 65 28 65 6c 65 6d 29 3b 0a 20 20 20 20 65 6c  ee(elem);.    el
30d2c 65 6d 20 3d 20 6e 65 78 74 5f 65 6c 65 6d 3b 0a  em = next_elem;.
30d2d 20 20 7d 0a 20 20 70 48 2d 3e 63 6f 75 6e 74 20    }.  pH->count 
30d2e 3d 20 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61  = 0;.}../*.** Ha
30d2f 73 68 20 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f  sh and compariso
30d30 6e 20 66 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e  n functions when
30d31 20 74 68 65 20 6d 6f 64 65 20 69 73 20 46 54 53   the mode is FTS
30d32 33 5f 48 41 53 48 5f 53 54 52 49 4e 47 0a 2a 2f  3_HASH_STRING.*/
30d33 0a 73 74 61 74 69 63 20 69 6e 74 20 66 74 73 33  .static int fts3
30d34 53 74 72 48 61 73 68 28 63 6f 6e 73 74 20 76 6f  StrHash(const vo
30d35 69 64 20 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b  id *pKey, int nK
30d36 65 79 29 7b 0a 20 20 63 6f 6e 73 74 20 63 68 61  ey){.  const cha
30d37 72 20 2a 7a 20 3d 20 28 63 6f 6e 73 74 20 63 68  r *z = (const ch
30d38 61 72 20 2a 29 70 4b 65 79 3b 0a 20 20 69 6e 74  ar *)pKey;.  int
30d39 20 68 20 3d 20 30 3b 0a 20 20 69 66 28 20 6e 4b   h = 0;.  if( nK
30d3a 65 79 3c 3d 30 20 29 20 6e 4b 65 79 20 3d 20 28  ey<=0 ) nKey = (
30d3b 69 6e 74 29 20 73 74 72 6c 65 6e 28 7a 29 3b 0a  int) strlen(z);.
30d3c 20 20 77 68 69 6c 65 28 20 6e 4b 65 79 20 3e 20    while( nKey > 
30d3d 30 20 20 29 7b 0a 20 20 20 20 68 20 3d 20 28 68  0  ){.    h = (h
30d3e 3c 3c 33 29 20 5e 20 68 20 5e 20 2a 7a 2b 2b 3b  <<3) ^ h ^ *z++;
30d3f 0a 20 20 20 20 6e 4b 65 79 2d 2d 3b 0a 20 20 7d  .    nKey--;.  }
30d40 0a 20 20 72 65 74 75 72 6e 20 68 20 26 20 30 78  .  return h & 0x
30d41 37 66 66 66 66 66 66 66 3b 0a 7d 0a 73 74 61 74  7fffffff;.}.stat
30d42 69 63 20 69 6e 74 20 66 74 73 33 53 74 72 43 6f  ic int fts3StrCo
30d43 6d 70 61 72 65 28 63 6f 6e 73 74 20 76 6f 69 64  mpare(const void
30d44 20 2a 70 4b 65 79 31 2c 20 69 6e 74 20 6e 31 2c   *pKey1, int n1,
30d45 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b 65   const void *pKe
30d46 79 32 2c 20 69 6e 74 20 6e 32 29 7b 0a 20 20 69  y2, int n2){.  i
30d47 66 28 20 6e 31 21 3d 6e 32 20 29 20 72 65 74 75  f( n1!=n2 ) retu
30d48 72 6e 20 31 3b 0a 20 20 72 65 74 75 72 6e 20 73  rn 1;.  return s
30d49 74 72 6e 63 6d 70 28 28 63 6f 6e 73 74 20 63 68  trncmp((const ch
30d4a 61 72 2a 29 70 4b 65 79 31 2c 28 63 6f 6e 73 74  ar*)pKey1,(const
30d4b 20 63 68 61 72 2a 29 70 4b 65 79 32 2c 6e 31 29   char*)pKey2,n1)
30d4c 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 48 61 73 68 20  ;.}../*.** Hash 
30d4d 61 6e 64 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66  and comparison f
30d4e 75 6e 63 74 69 6f 6e 73 20 77 68 65 6e 20 74 68  unctions when th
30d4f 65 20 6d 6f 64 65 20 69 73 20 46 54 53 33 5f 48  e mode is FTS3_H
30d50 41 53 48 5f 42 49 4e 41 52 59 0a 2a 2f 0a 73 74  ASH_BINARY.*/.st
30d51 61 74 69 63 20 69 6e 74 20 66 74 73 33 42 69 6e  atic int fts3Bin
30d52 48 61 73 68 28 63 6f 6e 73 74 20 76 6f 69 64 20  Hash(const void 
30d53 2a 70 4b 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29  *pKey, int nKey)
30d54 7b 0a 20 20 69 6e 74 20 68 20 3d 20 30 3b 0a 20  {.  int h = 0;. 
30d55 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d   const char *z =
30d56 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 70   (const char *)p
30d57 4b 65 79 3b 0a 20 20 77 68 69 6c 65 28 20 6e 4b  Key;.  while( nK
30d58 65 79 2d 2d 20 3e 20 30 20 29 7b 0a 20 20 20 20  ey-- > 0 ){.    
30d59 68 20 3d 20 28 68 3c 3c 33 29 20 5e 20 68 20 5e  h = (h<<3) ^ h ^
30d5a 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a 20 20 72   *(z++);.  }.  r
30d5b 65 74 75 72 6e 20 68 20 26 20 30 78 37 66 66 66  eturn h & 0x7fff
30d5c 66 66 66 66 3b 0a 7d 0a 73 74 61 74 69 63 20 69  ffff;.}.static i
30d5d 6e 74 20 66 74 73 33 42 69 6e 43 6f 6d 70 61 72  nt fts3BinCompar
30d5e 65 28 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  e(const void *pK
30d5f 65 79 31 2c 20 69 6e 74 20 6e 31 2c 20 63 6f 6e  ey1, int n1, con
30d60 73 74 20 76 6f 69 64 20 2a 70 4b 65 79 32 2c 20  st void *pKey2, 
30d61 69 6e 74 20 6e 32 29 7b 0a 20 20 69 66 28 20 6e  int n2){.  if( n
30d62 31 21 3d 6e 32 20 29 20 72 65 74 75 72 6e 20 31  1!=n2 ) return 1
30d63 3b 0a 20 20 72 65 74 75 72 6e 20 6d 65 6d 63 6d  ;.  return memcm
30d64 70 28 70 4b 65 79 31 2c 70 4b 65 79 32 2c 6e 31  p(pKey1,pKey2,n1
30d65 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75  );.}../*.** Retu
30d66 72 6e 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20  rn a pointer to 
30d67 74 68 65 20 61 70 70 72 6f 70 72 69 61 74 65 20  the appropriate 
30d68 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 67 69  hash function gi
30d69 76 65 6e 20 74 68 65 20 6b 65 79 20 63 6c 61 73  ven the key clas
30d6a 73 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 43 20 73  s..**.** The C s
30d6b 79 6e 74 61 78 20 69 6e 20 74 68 69 73 20 66 75  yntax in this fu
30d6c 6e 63 74 69 6f 6e 20 64 65 66 69 6e 69 74 69 6f  nction definitio
30d6d 6e 20 6d 61 79 20 62 65 20 75 6e 66 61 6d 69 6c  n may be unfamil
30d6e 61 72 20 74 6f 20 73 6f 6d 65 20 0a 2a 2a 20 70  ar to some .** p
30d6f 72 6f 67 72 61 6d 6d 65 72 73 2c 20 73 6f 20 77  rogrammers, so w
30d70 65 20 70 72 6f 76 69 64 65 20 74 68 65 20 66 6f  e provide the fo
30d71 6c 6c 6f 77 69 6e 67 20 61 64 64 69 74 69 6f 6e  llowing addition
30d72 61 6c 20 65 78 70 6c 61 6e 61 74 69 6f 6e 3a 0a  al explanation:.
30d73 2a 2a 0a 2a 2a 20 54 68 65 20 6e 61 6d 65 20 6f  **.** The name o
30d74 66 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 69  f the function i
30d75 73 20 22 66 74 73 48 61 73 68 46 75 6e 63 74 69  s "ftsHashFuncti
30d76 6f 6e 22 2e 20 20 54 68 65 20 66 75 6e 63 74 69  on".  The functi
30d77 6f 6e 20 74 61 6b 65 73 20 61 0a 2a 2a 20 73 69  on takes a.** si
30d78 6e 67 6c 65 20 70 61 72 61 6d 65 74 65 72 20 22  ngle parameter "
30d79 6b 65 79 43 6c 61 73 73 22 2e 20 20 54 68 65 20  keyClass".  The 
30d7a 72 65 74 75 72 6e 20 76 61 6c 75 65 20 6f 66 20  return value of 
30d7b 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
30d7c 29 0a 2a 2a 20 69 73 20 61 20 70 6f 69 6e 74 65  ).** is a pointe
30d7d 72 20 74 6f 20 61 6e 6f 74 68 65 72 20 66 75 6e  r to another fun
30d7e 63 74 69 6f 6e 2e 20 20 53 70 65 63 69 66 69 63  ction.  Specific
30d7f 61 6c 6c 79 2c 20 74 68 65 20 72 65 74 75 72 6e  ally, the return
30d80 20 76 61 6c 75 65 0a 2a 2a 20 6f 66 20 66 74 73   value.** of fts
30d81 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 29 20 69  HashFunction() i
30d82 73 20 61 20 70 6f 69 6e 74 65 72 20 74 6f 20 61  s a pointer to a
30d83 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 74   function that t
30d84 61 6b 65 73 20 74 77 6f 20 70 61 72 61 6d 65 74  akes two paramet
30d85 65 72 73 0a 2a 2a 20 77 69 74 68 20 74 79 70 65  ers.** with type
30d86 73 20 22 63 6f 6e 73 74 20 76 6f 69 64 2a 22 20  s "const void*" 
30d87 61 6e 64 20 22 69 6e 74 22 20 61 6e 64 20 72 65  and "int" and re
30d88 74 75 72 6e 73 20 61 6e 20 22 69 6e 74 22 2e 0a  turns an "int"..
30d89 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a  */.static int (*
30d8a 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28  ftsHashFunction(
30d8b 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29 28 63  int keyClass))(c
30d8c 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 7b  onst void*,int){
30d8d 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73 73 3d  .  if( keyClass=
30d8e 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52 49 4e  =FTS3_HASH_STRIN
30d8f 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20  G ){.    return 
30d90 26 66 74 73 33 53 74 72 48 61 73 68 3b 0a 20 20  &fts3StrHash;.  
30d91 7d 65 6c 73 65 7b 0a 20 20 20 20 61 73 73 65 72  }else{.    asser
30d92 74 28 20 6b 65 79 43 6c 61 73 73 3d 3d 46 54 53  t( keyClass==FTS
30d93 33 5f 48 41 53 48 5f 42 49 4e 41 52 59 20 29 3b  3_HASH_BINARY );
30d94 0a 20 20 20 20 72 65 74 75 72 6e 20 26 66 74 73  .    return &fts
30d95 33 42 69 6e 48 61 73 68 3b 0a 20 20 7d 0a 7d 0a  3BinHash;.  }.}.
30d96 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 61 20  ./*.** Return a 
30d97 70 6f 69 6e 74 65 72 20 74 6f 20 74 68 65 20 61  pointer to the a
30d98 70 70 72 6f 70 72 69 61 74 65 20 68 61 73 68 20  ppropriate hash 
30d99 66 75 6e 63 74 69 6f 6e 20 67 69 76 65 6e 20 74  function given t
30d9a 68 65 20 6b 65 79 20 63 6c 61 73 73 2e 0a 2a 2a  he key class..**
30d9b 0a 2a 2a 20 46 6f 72 20 68 65 6c 70 20 69 6e 20  .** For help in 
30d9c 69 6e 74 65 72 70 72 65 74 65 64 20 74 68 65 20  interpreted the 
30d9d 6f 62 73 63 75 72 65 20 43 20 63 6f 64 65 20 69  obscure C code i
30d9e 6e 20 74 68 65 20 66 75 6e 63 74 69 6f 6e 20 64  n the function d
30d9f 65 66 69 6e 69 74 69 6f 6e 2c 0a 2a 2a 20 73 65  efinition,.** se
30da0 65 20 74 68 65 20 68 65 61 64 65 72 20 63 6f 6d  e the header com
30da1 6d 65 6e 74 20 6f 6e 20 74 68 65 20 70 72 65 76  ment on the prev
30da2 69 6f 75 73 20 66 75 6e 63 74 69 6f 6e 2e 0a 2a  ious function..*
30da3 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 28 2a 66  /.static int (*f
30da4 74 73 43 6f 6d 70 61 72 65 46 75 6e 63 74 69 6f  tsCompareFunctio
30da5 6e 28 69 6e 74 20 6b 65 79 43 6c 61 73 73 29 29  n(int keyClass))
30da6 28 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  (const void*,int
30da7 2c 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74  ,const void*,int
30da8 29 7b 0a 20 20 69 66 28 20 6b 65 79 43 6c 61 73  ){.  if( keyClas
30da9 73 3d 3d 46 54 53 33 5f 48 41 53 48 5f 53 54 52  s==FTS3_HASH_STR
30daa 49 4e 47 20 29 7b 0a 20 20 20 20 72 65 74 75 72  ING ){.    retur
30dab 6e 20 26 66 74 73 33 53 74 72 43 6f 6d 70 61 72  n &fts3StrCompar
30dac 65 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  e;.  }else{.    
30dad 61 73 73 65 72 74 28 20 6b 65 79 43 6c 61 73 73  assert( keyClass
30dae 3d 3d 46 54 53 33 5f 48 41 53 48 5f 42 49 4e 41  ==FTS3_HASH_BINA
30daf 52 59 20 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  RY );.    return
30db0 20 26 66 74 73 33 42 69 6e 43 6f 6d 70 61 72 65   &fts3BinCompare
30db1 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 4c 69 6e 6b  ;.  }.}../* Link
30db2 20 61 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f   an element into
30db3 20 74 68 65 20 68 61 73 68 20 74 61 62 6c 65 0a   the hash table.
30db4 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  */.static void f
30db5 74 73 33 48 61 73 68 49 6e 73 65 72 74 45 6c 65  ts3HashInsertEle
30db6 6d 65 6e 74 28 0a 20 20 66 74 73 33 48 61 73 68  ment(.  fts3Hash
30db7 20 2a 70 48 2c 20 20 20 20 20 20 20 20 20 20 20   *pH,           
30db8 20 2f 2a 20 54 68 65 20 63 6f 6d 70 6c 65 74 65   /* The complete
30db9 20 68 61 73 68 20 74 61 62 6c 65 20 2a 2f 0a 20   hash table */. 
30dba 20 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20   struct _fts3ht 
30dbb 2a 70 45 6e 74 72 79 2c 20 20 2f 2a 20 54 68 65  *pEntry,  /* The
30dbc 20 65 6e 74 72 79 20 69 6e 74 6f 20 77 68 69 63   entry into whic
30dbd 68 20 70 4e 65 77 20 69 73 20 69 6e 73 65 72 74  h pNew is insert
30dbe 65 64 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68  ed */.  fts3Hash
30dbf 45 6c 65 6d 20 2a 70 4e 65 77 20 20 20 20 20 20  Elem *pNew      
30dc0 20 2f 2a 20 54 68 65 20 65 6c 65 6d 65 6e 74 20   /* The element 
30dc1 74 6f 20 62 65 20 69 6e 73 65 72 74 65 64 20 2a  to be inserted *
30dc2 2f 0a 29 7b 0a 20 20 66 74 73 33 48 61 73 68 45  /.){.  fts3HashE
30dc3 6c 65 6d 20 2a 70 48 65 61 64 3b 20 20 20 20 20  lem *pHead;     
30dc4 2f 2a 20 46 69 72 73 74 20 65 6c 65 6d 65 6e 74  /* First element
30dc5 20 61 6c 72 65 61 64 79 20 69 6e 20 70 45 6e 74   already in pEnt
30dc6 72 79 20 2a 2f 0a 20 20 70 48 65 61 64 20 3d 20  ry */.  pHead = 
30dc7 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20  pEntry->chain;. 
30dc8 20 69 66 28 20 70 48 65 61 64 20 29 7b 0a 20 20   if( pHead ){.  
30dc9 20 20 70 4e 65 77 2d 3e 6e 65 78 74 20 3d 20 70    pNew->next = p
30dca 48 65 61 64 3b 0a 20 20 20 20 70 4e 65 77 2d 3e  Head;.    pNew->
30dcb 70 72 65 76 20 3d 20 70 48 65 61 64 2d 3e 70 72  prev = pHead->pr
30dcc 65 76 3b 0a 20 20 20 20 69 66 28 20 70 48 65 61  ev;.    if( pHea
30dcd 64 2d 3e 70 72 65 76 20 29 7b 20 70 48 65 61 64  d->prev ){ pHead
30dce 2d 3e 70 72 65 76 2d 3e 6e 65 78 74 20 3d 20 70  ->prev->next = p
30dcf 4e 65 77 3b 20 7d 0a 20 20 20 20 65 6c 73 65 20  New; }.    else 
30dd0 20 20 20 20 20 20 20 20 20 20 20 20 7b 20 70 48              { pH
30dd1 2d 3e 66 69 72 73 74 20 3d 20 70 4e 65 77 3b 20  ->first = pNew; 
30dd2 7d 0a 20 20 20 20 70 48 65 61 64 2d 3e 70 72 65  }.    pHead->pre
30dd3 76 20 3d 20 70 4e 65 77 3b 0a 20 20 7d 65 6c 73  v = pNew;.  }els
30dd4 65 7b 0a 20 20 20 20 70 4e 65 77 2d 3e 6e 65 78  e{.    pNew->nex
30dd5 74 20 3d 20 70 48 2d 3e 66 69 72 73 74 3b 0a 20  t = pH->first;. 
30dd6 20 20 20 69 66 28 20 70 48 2d 3e 66 69 72 73 74     if( pH->first
30dd7 20 29 7b 20 70 48 2d 3e 66 69 72 73 74 2d 3e 70   ){ pH->first->p
30dd8 72 65 76 20 3d 20 70 4e 65 77 3b 20 7d 0a 20 20  rev = pNew; }.  
30dd9 20 20 70 4e 65 77 2d 3e 70 72 65 76 20 3d 20 30    pNew->prev = 0
30dda 3b 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20  ;.    pH->first 
30ddb 3d 20 70 4e 65 77 3b 0a 20 20 7d 0a 20 20 70 45  = pNew;.  }.  pE
30ddc 6e 74 72 79 2d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20  ntry->count++;. 
30ddd 20 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 20 3d   pEntry->chain =
30dde 20 70 4e 65 77 3b 0a 7d 0a 0a 0a 2f 2a 20 52 65   pNew;.}.../* Re
30ddf 73 69 7a 65 20 74 68 65 20 68 61 73 68 20 74 61  size the hash ta
30de0 62 6c 65 20 73 6f 20 74 68 61 74 20 69 74 20 63  ble so that it c
30de1 61 6e 74 61 69 6e 73 20 22 6e 65 77 5f 73 69 7a  antains "new_siz
30de2 65 22 20 62 75 63 6b 65 74 73 2e 0a 2a 2a 20 22  e" buckets..** "
30de3 6e 65 77 5f 73 69 7a 65 22 20 6d 75 73 74 20 62  new_size" must b
30de4 65 20 61 20 70 6f 77 65 72 20 6f 66 20 32 2e 20  e a power of 2. 
30de5 20 54 68 65 20 68 61 73 68 20 74 61 62 6c 65 20   The hash table 
30de6 6d 69 67 68 74 20 66 61 69 6c 20 0a 2a 2a 20 74  might fail .** t
30de7 6f 20 72 65 73 69 7a 65 20 69 66 20 73 71 6c 69  o resize if sqli
30de8 74 65 4d 61 6c 6c 6f 63 28 29 20 66 61 69 6c 73  teMalloc() fails
30de9 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
30dea 20 66 74 73 33 52 65 68 61 73 68 28 66 74 73 33   fts3Rehash(fts3
30deb 48 61 73 68 20 2a 70 48 2c 20 69 6e 74 20 6e 65  Hash *pH, int ne
30dec 77 5f 73 69 7a 65 29 7b 0a 20 20 73 74 72 75 63  w_size){.  struc
30ded 74 20 5f 66 74 73 33 68 74 20 2a 6e 65 77 5f 68  t _fts3ht *new_h
30dee 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54  t;          /* T
30def 68 65 20 6e 65 77 20 68 61 73 68 20 74 61 62 6c  he new hash tabl
30df0 65 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45  e */.  fts3HashE
30df1 6c 65 6d 20 2a 65 6c 65 6d 2c 20 2a 6e 65 78 74  lem *elem, *next
30df2 5f 65 6c 65 6d 3b 20 20 2f 2a 20 46 6f 72 20 6c  _elem;  /* For l
30df3 6f 6f 70 69 6e 67 20 6f 76 65 72 20 65 78 69 73  ooping over exis
30df4 74 69 6e 67 20 65 6c 65 6d 65 6e 74 73 20 2a 2f  ting elements */
30df5 0a 20 20 69 6e 74 20 28 2a 78 48 61 73 68 29 28  .  int (*xHash)(
30df6 63 6f 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29  const void*,int)
30df7 3b 20 20 20 2f 2a 20 54 68 65 20 68 61 73 68 20  ;   /* The hash 
30df8 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 61  function */..  a
30df9 73 73 65 72 74 28 20 28 6e 65 77 5f 73 69 7a 65  ssert( (new_size
30dfa 20 26 20 28 6e 65 77 5f 73 69 7a 65 2d 31 29 29   & (new_size-1))
30dfb 3d 3d 30 20 29 3b 0a 20 20 6e 65 77 5f 68 74 20  ==0 );.  new_ht 
30dfc 3d 20 28 73 74 72 75 63 74 20 5f 66 74 73 33 68  = (struct _fts3h
30dfd 74 20 2a 29 66 74 73 33 48 61 73 68 4d 61 6c 6c  t *)fts3HashMall
30dfe 6f 63 28 20 6e 65 77 5f 73 69 7a 65 2a 73 69 7a  oc( new_size*siz
30dff 65 6f 66 28 73 74 72 75 63 74 20 5f 66 74 73 33  eof(struct _fts3
30e00 68 74 29 20 29 3b 0a 20 20 69 66 28 20 6e 65 77  ht) );.  if( new
30e01 5f 68 74 3d 3d 30 20 29 20 72 65 74 75 72 6e 3b  _ht==0 ) return;
30e02 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28  .  fts3HashFree(
30e03 70 48 2d 3e 68 74 29 3b 0a 20 20 70 48 2d 3e 68  pH->ht);.  pH->h
30e04 74 20 3d 20 6e 65 77 5f 68 74 3b 0a 20 20 70 48  t = new_ht;.  pH
30e05 2d 3e 68 74 73 69 7a 65 20 3d 20 6e 65 77 5f 73  ->htsize = new_s
30e06 69 7a 65 3b 0a 20 20 78 48 61 73 68 20 3d 20 66  ize;.  xHash = f
30e07 74 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70  tsHashFunction(p
30e08 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20  H->keyClass);.  
30e09 66 6f 72 28 65 6c 65 6d 3d 70 48 2d 3e 66 69 72  for(elem=pH->fir
30e0a 73 74 2c 20 70 48 2d 3e 66 69 72 73 74 3d 30 3b  st, pH->first=0;
30e0b 20 65 6c 65 6d 3b 20 65 6c 65 6d 20 3d 20 6e 65   elem; elem = ne
30e0c 78 74 5f 65 6c 65 6d 29 7b 0a 20 20 20 20 69 6e  xt_elem){.    in
30e0d 74 20 68 20 3d 20 28 2a 78 48 61 73 68 29 28 65  t h = (*xHash)(e
30e0e 6c 65 6d 2d 3e 70 4b 65 79 2c 20 65 6c 65 6d 2d  lem->pKey, elem-
30e0f 3e 6e 4b 65 79 29 20 26 20 28 6e 65 77 5f 73 69  >nKey) & (new_si
30e10 7a 65 2d 31 29 3b 0a 20 20 20 20 6e 65 78 74 5f  ze-1);.    next_
30e11 65 6c 65 6d 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78  elem = elem->nex
30e12 74 3b 0a 20 20 20 20 66 74 73 33 48 61 73 68 49  t;.    fts3HashI
30e13 6e 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c  nsertElement(pH,
30e14 20 26 6e 65 77 5f 68 74 5b 68 5d 2c 20 65 6c 65   &new_ht[h], ele
30e15 6d 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 20 54 68  m);.  }.}../* Th
30e16 69 73 20 66 75 6e 63 74 69 6f 6e 20 28 66 6f 72  is function (for
30e17 20 69 6e 74 65 72 6e 61 6c 20 75 73 65 20 6f 6e   internal use on
30e18 6c 79 29 20 6c 6f 63 61 74 65 73 20 61 6e 20 65  ly) locates an e
30e19 6c 65 6d 65 6e 74 20 69 6e 20 61 6e 0a 2a 2a 20  lement in an.** 
30e1a 68 61 73 68 20 74 61 62 6c 65 20 74 68 61 74 20  hash table that 
30e1b 6d 61 74 63 68 65 73 20 74 68 65 20 67 69 76 65  matches the give
30e1c 6e 20 6b 65 79 2e 20 20 54 68 65 20 68 61 73 68  n key.  The hash
30e1d 20 66 6f 72 20 74 68 69 73 20 6b 65 79 20 68 61   for this key ha
30e1e 73 0a 2a 2a 20 61 6c 72 65 61 64 79 20 62 65 65  s.** already bee
30e1f 6e 20 63 6f 6d 70 75 74 65 64 20 61 6e 64 20 69  n computed and i
30e20 73 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20  s passed as the 
30e21 34 74 68 20 70 61 72 61 6d 65 74 65 72 2e 0a 2a  4th parameter..*
30e22 2f 0a 73 74 61 74 69 63 20 66 74 73 33 48 61 73  /.static fts3Has
30e23 68 45 6c 65 6d 20 2a 66 74 73 33 46 69 6e 64 45  hElem *fts3FindE
30e24 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 0a 20 20  lementByHash(.  
30e25 63 6f 6e 73 74 20 66 74 73 33 48 61 73 68 20 2a  const fts3Hash *
30e26 70 48 2c 20 2f 2a 20 54 68 65 20 70 48 20 74 6f  pH, /* The pH to
30e27 20 62 65 20 73 65 61 72 63 68 65 64 20 2a 2f 0a   be searched */.
30e28 20 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b    const void *pK
30e29 65 79 2c 20 20 20 2f 2a 20 54 68 65 20 6b 65 79  ey,   /* The key
30e2a 20 77 65 20 61 72 65 20 73 65 61 72 63 68 69 6e   we are searchin
30e2b 67 20 66 6f 72 20 2a 2f 0a 20 20 69 6e 74 20 6e  g for */.  int n
30e2c 4b 65 79 2c 0a 20 20 69 6e 74 20 68 20 20 20 20  Key,.  int h    
30e2d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 54 68             /* Th
30e2e 65 20 68 61 73 68 20 66 6f 72 20 74 68 69 73 20  e hash for this 
30e2f 6b 65 79 2e 20 2a 2f 0a 29 7b 0a 20 20 66 74 73  key. */.){.  fts
30e30 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d 3b  3HashElem *elem;
30e31 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 55              /* U
30e32 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74 68 72 75  sed to loop thru
30e33 20 74 68 65 20 65 6c 65 6d 65 6e 74 20 6c 69 73   the element lis
30e34 74 20 2a 2f 0a 20 20 69 6e 74 20 63 6f 75 6e 74  t */.  int count
30e35 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
30e36 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72 20        /* Number 
30e37 6f 66 20 65 6c 65 6d 65 6e 74 73 20 6c 65 66 74  of elements left
30e38 20 74 6f 20 74 65 73 74 20 2a 2f 0a 20 20 69 6e   to test */.  in
30e39 74 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 63 6f  t (*xCompare)(co
30e3a 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 2c 63 6f  nst void*,int,co
30e3b 6e 73 74 20 76 6f 69 64 2a 2c 69 6e 74 29 3b 20  nst void*,int); 
30e3c 20 2f 2a 20 63 6f 6d 70 61 72 69 73 6f 6e 20 66   /* comparison f
30e3d 75 6e 63 74 69 6f 6e 20 2a 2f 0a 0a 20 20 69 66  unction */..  if
30e3e 28 20 70 48 2d 3e 68 74 20 29 7b 0a 20 20 20 20  ( pH->ht ){.    
30e3f 73 74 72 75 63 74 20 5f 66 74 73 33 68 74 20 2a  struct _fts3ht *
30e40 70 45 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74  pEntry = &pH->ht
30e41 5b 68 5d 3b 0a 20 20 20 20 65 6c 65 6d 20 3d 20  [h];.    elem = 
30e42 70 45 6e 74 72 79 2d 3e 63 68 61 69 6e 3b 0a 20  pEntry->chain;. 
30e43 20 20 20 63 6f 75 6e 74 20 3d 20 70 45 6e 74 72     count = pEntr
30e44 79 2d 3e 63 6f 75 6e 74 3b 0a 20 20 20 20 78 43  y->count;.    xC
30e45 6f 6d 70 61 72 65 20 3d 20 66 74 73 43 6f 6d 70  ompare = ftsComp
30e46 61 72 65 46 75 6e 63 74 69 6f 6e 28 70 48 2d 3e  areFunction(pH->
30e47 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 20 20 77  keyClass);.    w
30e48 68 69 6c 65 28 20 63 6f 75 6e 74 2d 2d 20 26 26  hile( count-- &&
30e49 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 20 20 69   elem ){.      i
30e4a 66 28 20 28 2a 78 43 6f 6d 70 61 72 65 29 28 65  f( (*xCompare)(e
30e4b 6c 65 6d 2d 3e 70 4b 65 79 2c 65 6c 65 6d 2d 3e  lem->pKey,elem->
30e4c 6e 4b 65 79 2c 70 4b 65 79 2c 6e 4b 65 79 29 3d  nKey,pKey,nKey)=
30e4d 3d 30 20 29 7b 20 0a 20 20 20 20 20 20 20 20 72  =0 ){ .        r
30e4e 65 74 75 72 6e 20 65 6c 65 6d 3b 0a 20 20 20 20  eturn elem;.    
30e4f 20 20 7d 0a 20 20 20 20 20 20 65 6c 65 6d 20 3d    }.      elem =
30e50 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 20   elem->next;.   
30e51 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20   }.  }.  return 
30e52 30 3b 0a 7d 0a 0a 2f 2a 20 52 65 6d 6f 76 65 20  0;.}../* Remove 
30e53 61 20 73 69 6e 67 6c 65 20 65 6e 74 72 79 20 66  a single entry f
30e54 72 6f 6d 20 74 68 65 20 68 61 73 68 20 74 61 62  rom the hash tab
30e55 6c 65 20 67 69 76 65 6e 20 61 20 70 6f 69 6e 74  le given a point
30e56 65 72 20 74 6f 20 74 68 61 74 0a 2a 2a 20 65 6c  er to that.** el
30e57 65 6d 65 6e 74 20 61 6e 64 20 61 20 68 61 73 68  ement and a hash
30e58 20 6f 6e 20 74 68 65 20 65 6c 65 6d 65 6e 74 27   on the element'
30e59 73 20 6b 65 79 2e 0a 2a 2f 0a 73 74 61 74 69 63  s key..*/.static
30e5a 20 76 6f 69 64 20 66 74 73 33 52 65 6d 6f 76 65   void fts3Remove
30e5b 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 0a 20  ElementByHash(. 
30e5c 20 66 74 73 33 48 61 73 68 20 2a 70 48 2c 20 20   fts3Hash *pH,  
30e5d 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 70 48         /* The pH
30e5e 20 63 6f 6e 74 61 69 6e 69 6e 67 20 22 65 6c 65   containing "ele
30e5f 6d 22 20 2a 2f 0a 20 20 66 74 73 33 48 61 73 68  m" */.  fts3Hash
30e60 45 6c 65 6d 2a 20 65 6c 65 6d 2c 20 20 20 2f 2a  Elem* elem,   /*
30e61 20 54 68 65 20 65 6c 65 6d 65 6e 74 20 74 6f 20   The element to 
30e62 62 65 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20  be removed from 
30e63 74 68 65 20 70 48 20 2a 2f 0a 20 20 69 6e 74 20  the pH */.  int 
30e64 68 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  h               
30e65 20 20 2f 2a 20 48 61 73 68 20 76 61 6c 75 65 20    /* Hash value 
30e66 66 6f 72 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  for the element 
30e67 2a 2f 0a 29 7b 0a 20 20 73 74 72 75 63 74 20 5f  */.){.  struct _
30e68 66 74 73 33 68 74 20 2a 70 45 6e 74 72 79 3b 0a  fts3ht *pEntry;.
30e69 20 20 69 66 28 20 65 6c 65 6d 2d 3e 70 72 65 76    if( elem->prev
30e6a 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 70 72   ){.    elem->pr
30e6b 65 76 2d 3e 6e 65 78 74 20 3d 20 65 6c 65 6d 2d  ev->next = elem-
30e6c 3e 6e 65 78 74 3b 20 0a 20 20 7d 65 6c 73 65 7b  >next; .  }else{
30e6d 0a 20 20 20 20 70 48 2d 3e 66 69 72 73 74 20 3d  .    pH->first =
30e6e 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a 20 20 7d   elem->next;.  }
30e6f 0a 20 20 69 66 28 20 65 6c 65 6d 2d 3e 6e 65 78  .  if( elem->nex
30e70 74 20 29 7b 0a 20 20 20 20 65 6c 65 6d 2d 3e 6e  t ){.    elem->n
30e71 65 78 74 2d 3e 70 72 65 76 20 3d 20 65 6c 65 6d  ext->prev = elem
30e72 2d 3e 70 72 65 76 3b 0a 20 20 7d 0a 20 20 70 45  ->prev;.  }.  pE
30e73 6e 74 72 79 20 3d 20 26 70 48 2d 3e 68 74 5b 68  ntry = &pH->ht[h
30e74 5d 3b 0a 20 20 69 66 28 20 70 45 6e 74 72 79 2d  ];.  if( pEntry-
30e75 3e 63 68 61 69 6e 3d 3d 65 6c 65 6d 20 29 7b 0a  >chain==elem ){.
30e76 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61 69      pEntry->chai
30e77 6e 20 3d 20 65 6c 65 6d 2d 3e 6e 65 78 74 3b 0a  n = elem->next;.
30e78 20 20 7d 0a 20 20 70 45 6e 74 72 79 2d 3e 63 6f    }.  pEntry->co
30e79 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 45 6e  unt--;.  if( pEn
30e7a 74 72 79 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b  try->count<=0 ){
30e7b 0a 20 20 20 20 70 45 6e 74 72 79 2d 3e 63 68 61  .    pEntry->cha
30e7c 69 6e 20 3d 20 30 3b 0a 20 20 7d 0a 20 20 69 66  in = 0;.  }.  if
30e7d 28 20 70 48 2d 3e 63 6f 70 79 4b 65 79 20 26 26  ( pH->copyKey &&
30e7e 20 65 6c 65 6d 2d 3e 70 4b 65 79 20 29 7b 0a 20   elem->pKey ){. 
30e7f 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28     fts3HashFree(
30e80 65 6c 65 6d 2d 3e 70 4b 65 79 29 3b 0a 20 20 7d  elem->pKey);.  }
30e81 0a 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28  .  fts3HashFree(
30e82 20 65 6c 65 6d 20 29 3b 0a 20 20 70 48 2d 3e 63   elem );.  pH->c
30e83 6f 75 6e 74 2d 2d 3b 0a 20 20 69 66 28 20 70 48  ount--;.  if( pH
30e84 2d 3e 63 6f 75 6e 74 3c 3d 30 20 29 7b 0a 20 20  ->count<=0 ){.  
30e85 20 20 61 73 73 65 72 74 28 20 70 48 2d 3e 66 69    assert( pH->fi
30e86 72 73 74 3d 3d 30 20 29 3b 0a 20 20 20 20 61 73  rst==0 );.    as
30e87 73 65 72 74 28 20 70 48 2d 3e 63 6f 75 6e 74 3d  sert( pH->count=
30e88 3d 30 20 29 3b 0a 20 20 20 20 66 74 73 33 48 61  =0 );.    fts3Ha
30e89 73 68 43 6c 65 61 72 28 70 48 29 3b 0a 20 20 7d  shClear(pH);.  }
30e8a 0a 7d 0a 0a 2f 2a 20 41 74 74 65 6d 70 74 20 74  .}../* Attempt t
30e8b 6f 20 6c 6f 63 61 74 65 20 61 6e 20 65 6c 65 6d  o locate an elem
30e8c 65 6e 74 20 6f 66 20 74 68 65 20 68 61 73 68 20  ent of the hash 
30e8d 74 61 62 6c 65 20 70 48 20 77 69 74 68 20 61 20  table pH with a 
30e8e 6b 65 79 0a 2a 2a 20 74 68 61 74 20 6d 61 74 63  key.** that matc
30e8f 68 65 73 20 70 4b 65 79 2c 6e 4b 65 79 2e 20 20  hes pKey,nKey.  
30e90 52 65 74 75 72 6e 20 74 68 65 20 64 61 74 61 20  Return the data 
30e91 66 6f 72 20 74 68 69 73 20 65 6c 65 6d 65 6e 74  for this element
30e92 20 69 66 20 69 74 20 69 73 0a 2a 2a 20 66 6f 75   if it is.** fou
30e93 6e 64 2c 20 6f 72 20 4e 55 4c 4c 20 69 66 20 74  nd, or NULL if t
30e94 68 65 72 65 20 69 73 20 6e 6f 20 6d 61 74 63 68  here is no match
30e95 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56  ..*/.SQLITE_PRIV
30e96 41 54 45 20 76 6f 69 64 20 2a 73 71 6c 69 74 65  ATE void *sqlite
30e97 33 46 74 73 33 48 61 73 68 46 69 6e 64 28 63 6f  3Fts3HashFind(co
30e98 6e 73 74 20 66 74 73 33 48 61 73 68 20 2a 70 48  nst fts3Hash *pH
30e99 2c 20 63 6f 6e 73 74 20 76 6f 69 64 20 2a 70 4b  , const void *pK
30e9a 65 79 2c 20 69 6e 74 20 6e 4b 65 79 29 7b 0a 20  ey, int nKey){. 
30e9b 20 69 6e 74 20 68 3b 20 20 20 20 20 20 20 20 20   int h;         
30e9c 20 20 20 20 20 20 20 20 2f 2a 20 41 20 68 61 73          /* A has
30e9d 68 20 6f 6e 20 6b 65 79 20 2a 2f 0a 20 20 66 74  h on key */.  ft
30e9e 73 33 48 61 73 68 45 6c 65 6d 20 2a 65 6c 65 6d  s3HashElem *elem
30e9f 3b 20 20 20 20 2f 2a 20 54 68 65 20 65 6c 65 6d  ;    /* The elem
30ea0 65 6e 74 20 74 68 61 74 20 6d 61 74 63 68 65 73  ent that matches
30ea1 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 28 2a   key */.  int (*
30ea2 78 48 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69  xHash)(const voi
30ea3 64 2a 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65  d*,int);  /* The
30ea4 20 68 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a   hash function *
30ea5 2f 0a 0a 20 20 69 66 28 20 70 48 3d 3d 30 20 7c  /..  if( pH==0 |
30ea6 7c 20 70 48 2d 3e 68 74 3d 3d 30 20 29 20 72 65  | pH->ht==0 ) re
30ea7 74 75 72 6e 20 30 3b 0a 20 20 78 48 61 73 68 20  turn 0;.  xHash 
30ea8 3d 20 66 74 73 48 61 73 68 46 75 6e 63 74 69 6f  = ftsHashFunctio
30ea9 6e 28 70 48 2d 3e 6b 65 79 43 6c 61 73 73 29 3b  n(pH->keyClass);
30eaa 0a 20 20 61 73 73 65 72 74 28 20 78 48 61 73 68  .  assert( xHash
30eab 21 3d 30 20 29 3b 0a 20 20 68 20 3d 20 28 2a 78  !=0 );.  h = (*x
30eac 48 61 73 68 29 28 70 4b 65 79 2c 6e 4b 65 79 29  Hash)(pKey,nKey)
30ead 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d  ;.  assert( (pH-
30eae 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68  >htsize & (pH->h
30eaf 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  tsize-1))==0 );.
30eb0 20 20 65 6c 65 6d 20 3d 20 66 74 73 33 46 69 6e    elem = fts3Fin
30eb1 64 45 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 70  dElementByHash(p
30eb2 48 2c 70 4b 65 79 2c 6e 4b 65 79 2c 20 68 20 26  H,pKey,nKey, h &
30eb3 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29   (pH->htsize-1))
30eb4 3b 0a 20 20 72 65 74 75 72 6e 20 65 6c 65 6d 20  ;.  return elem 
30eb5 3f 20 65 6c 65 6d 2d 3e 64 61 74 61 20 3a 20 30  ? elem->data : 0
30eb6 3b 0a 7d 0a 0a 2f 2a 20 49 6e 73 65 72 74 20 61  ;.}../* Insert a
30eb7 6e 20 65 6c 65 6d 65 6e 74 20 69 6e 74 6f 20 74  n element into t
30eb8 68 65 20 68 61 73 68 20 74 61 62 6c 65 20 70 48  he hash table pH
30eb9 2e 20 20 54 68 65 20 6b 65 79 20 69 73 20 70 4b  .  The key is pK
30eba 65 79 2c 6e 4b 65 79 0a 2a 2a 20 61 6e 64 20 74  ey,nKey.** and t
30ebb 68 65 20 64 61 74 61 20 69 73 20 22 64 61 74 61  he data is "data
30ebc 22 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 6e 6f 20 65  "..**.** If no e
30ebd 6c 65 6d 65 6e 74 20 65 78 69 73 74 73 20 77 69  lement exists wi
30ebe 74 68 20 61 20 6d 61 74 63 68 69 6e 67 20 6b 65  th a matching ke
30ebf 79 2c 20 74 68 65 6e 20 61 20 6e 65 77 0a 2a 2a  y, then a new.**
30ec0 20 65 6c 65 6d 65 6e 74 20 69 73 20 63 72 65 61   element is crea
30ec1 74 65 64 2e 20 20 41 20 63 6f 70 79 20 6f 66 20  ted.  A copy of 
30ec2 74 68 65 20 6b 65 79 20 69 73 20 6d 61 64 65 20  the key is made 
30ec3 69 66 20 74 68 65 20 63 6f 70 79 4b 65 79 0a 2a  if the copyKey.*
30ec4 2a 20 66 6c 61 67 20 69 73 20 73 65 74 2e 20 20  * flag is set.  
30ec5 4e 55 4c 4c 20 69 73 20 72 65 74 75 72 6e 65 64  NULL is returned
30ec6 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 61 6e 6f 74 68  ..**.** If anoth
30ec7 65 72 20 65 6c 65 6d 65 6e 74 20 61 6c 72 65 61  er element alrea
30ec8 64 79 20 65 78 69 73 74 73 20 77 69 74 68 20 74  dy exists with t
30ec9 68 65 20 73 61 6d 65 20 6b 65 79 2c 20 74 68 65  he same key, the
30eca 6e 20 74 68 65 0a 2a 2a 20 6e 65 77 20 64 61 74  n the.** new dat
30ecb 61 20 72 65 70 6c 61 63 65 73 20 74 68 65 20 6f  a replaces the o
30ecc 6c 64 20 64 61 74 61 20 61 6e 64 20 74 68 65 20  ld data and the 
30ecd 6f 6c 64 20 64 61 74 61 20 69 73 20 72 65 74 75  old data is retu
30ece 72 6e 65 64 2e 0a 2a 2a 20 54 68 65 20 6b 65 79  rned..** The key
30ecf 20 69 73 20 6e 6f 74 20 63 6f 70 69 65 64 20 69   is not copied i
30ed0 6e 20 74 68 69 73 20 69 6e 73 74 61 6e 63 65 2e  n this instance.
30ed1 20 20 49 66 20 61 20 6d 61 6c 6c 6f 63 20 66 61    If a malloc fa
30ed2 69 6c 73 2c 20 74 68 65 6e 0a 2a 2a 20 74 68 65  ils, then.** the
30ed3 20 6e 65 77 20 64 61 74 61 20 69 73 20 72 65 74   new data is ret
30ed4 75 72 6e 65 64 20 61 6e 64 20 74 68 65 20 68 61  urned and the ha
30ed5 73 68 20 74 61 62 6c 65 20 69 73 20 75 6e 63 68  sh table is unch
30ed6 61 6e 67 65 64 2e 0a 2a 2a 0a 2a 2a 20 49 66 20  anged..**.** If 
30ed7 74 68 65 20 22 64 61 74 61 22 20 70 61 72 61 6d  the "data" param
30ed8 65 74 65 72 20 74 6f 20 74 68 69 73 20 66 75 6e  eter to this fun
30ed9 63 74 69 6f 6e 20 69 73 20 4e 55 4c 4c 2c 20 74  ction is NULL, t
30eda 68 65 6e 20 74 68 65 0a 2a 2a 20 65 6c 65 6d 65  hen the.** eleme
30edb 6e 74 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67  nt corresponding
30edc 20 74 6f 20 22 6b 65 79 22 20 69 73 20 72 65 6d   to "key" is rem
30edd 6f 76 65 64 20 66 72 6f 6d 20 74 68 65 20 68 61  oved from the ha
30ede 73 68 20 74 61 62 6c 65 2e 0a 2a 2f 0a 53 51 4c  sh table..*/.SQL
30edf 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
30ee0 20 2a 73 71 6c 69 74 65 33 46 74 73 33 48 61 73   *sqlite3Fts3Has
30ee1 68 49 6e 73 65 72 74 28 0a 20 20 66 74 73 33 48  hInsert(.  fts3H
30ee2 61 73 68 20 2a 70 48 2c 20 20 20 20 20 20 20 20  ash *pH,        
30ee3 2f 2a 20 54 68 65 20 68 61 73 68 20 74 61 62 6c  /* The hash tabl
30ee4 65 20 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f  e to insert into
30ee5 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 76 6f 69 64   */.  const void
30ee6 20 2a 70 4b 65 79 2c 20 20 20 20 2f 2a 20 54 68   *pKey,    /* Th
30ee7 65 20 6b 65 79 20 2a 2f 0a 20 20 69 6e 74 20 6e  e key */.  int n
30ee8 4b 65 79 2c 20 20 20 20 20 20 20 20 20 20 20 20  Key,            
30ee9 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 62 79 74  /* Number of byt
30eea 65 73 20 69 6e 20 74 68 65 20 6b 65 79 20 2a 2f  es in the key */
30eeb 0a 20 20 76 6f 69 64 20 2a 64 61 74 61 20 20 20  .  void *data   
30eec 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 64          /* The d
30eed 61 74 61 20 2a 2f 0a 29 7b 0a 20 20 69 6e 74 20  ata */.){.  int 
30eee 68 72 61 77 3b 20 20 20 20 20 20 20 20 20 20 20  hraw;           
30eef 20 20 20 20 20 20 2f 2a 20 52 61 77 20 68 61 73        /* Raw has
30ef0 68 20 76 61 6c 75 65 20 6f 66 20 74 68 65 20 6b  h value of the k
30ef1 65 79 20 2a 2f 0a 20 20 69 6e 74 20 68 3b 20 20  ey */.  int h;  
30ef2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
30ef3 20 20 2f 2a 20 74 68 65 20 68 61 73 68 20 6f 66    /* the hash of
30ef4 20 74 68 65 20 6b 65 79 20 6d 6f 64 75 6c 6f 20   the key modulo 
30ef5 68 61 73 68 20 74 61 62 6c 65 20 73 69 7a 65 20  hash table size 
30ef6 2a 2f 0a 20 20 66 74 73 33 48 61 73 68 45 6c 65  */.  fts3HashEle
30ef7 6d 20 2a 65 6c 65 6d 3b 20 20 20 20 20 20 20 2f  m *elem;       /
30ef8 2a 20 55 73 65 64 20 74 6f 20 6c 6f 6f 70 20 74  * Used to loop t
30ef9 68 72 75 20 74 68 65 20 65 6c 65 6d 65 6e 74 20  hru the element 
30efa 6c 69 73 74 20 2a 2f 0a 20 20 66 74 73 33 48 61  list */.  fts3Ha
30efb 73 68 45 6c 65 6d 20 2a 6e 65 77 5f 65 6c 65 6d  shElem *new_elem
30efc 3b 20 20 20 2f 2a 20 4e 65 77 20 65 6c 65 6d 65  ;   /* New eleme
30efd 6e 74 20 61 64 64 65 64 20 74 6f 20 74 68 65 20  nt added to the 
30efe 70 48 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 48  pH */.  int (*xH
30eff 61 73 68 29 28 63 6f 6e 73 74 20 76 6f 69 64 2a  ash)(const void*
30f00 2c 69 6e 74 29 3b 20 20 2f 2a 20 54 68 65 20 68  ,int);  /* The h
30f01 61 73 68 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a  ash function */.
30f02 0a 20 20 61 73 73 65 72 74 28 20 70 48 21 3d 30  .  assert( pH!=0
30f03 20 29 3b 0a 20 20 78 48 61 73 68 20 3d 20 66 74   );.  xHash = ft
30f04 73 48 61 73 68 46 75 6e 63 74 69 6f 6e 28 70 48  sHashFunction(pH
30f05 2d 3e 6b 65 79 43 6c 61 73 73 29 3b 0a 20 20 61  ->keyClass);.  a
30f06 73 73 65 72 74 28 20 78 48 61 73 68 21 3d 30 20  ssert( xHash!=0 
30f07 29 3b 0a 20 20 68 72 61 77 20 3d 20 28 2a 78 48  );.  hraw = (*xH
30f08 61 73 68 29 28 70 4b 65 79 2c 20 6e 4b 65 79 29  ash)(pKey, nKey)
30f09 3b 0a 20 20 61 73 73 65 72 74 28 20 28 70 48 2d  ;.  assert( (pH-
30f0a 3e 68 74 73 69 7a 65 20 26 20 28 70 48 2d 3e 68  >htsize & (pH->h
30f0b 74 73 69 7a 65 2d 31 29 29 3d 3d 30 20 29 3b 0a  tsize-1))==0 );.
30f0c 20 20 68 20 3d 20 68 72 61 77 20 26 20 28 70 48    h = hraw & (pH
30f0d 2d 3e 68 74 73 69 7a 65 2d 31 29 3b 0a 20 20 65  ->htsize-1);.  e
30f0e 6c 65 6d 20 3d 20 66 74 73 33 46 69 6e 64 45 6c  lem = fts3FindEl
30f0f 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c 70  ementByHash(pH,p
30f10 4b 65 79 2c 6e 4b 65 79 2c 68 29 3b 0a 20 20 69  Key,nKey,h);.  i
30f11 66 28 20 65 6c 65 6d 20 29 7b 0a 20 20 20 20 76  f( elem ){.    v
30f12 6f 69 64 20 2a 6f 6c 64 5f 64 61 74 61 20 3d 20  oid *old_data = 
30f13 65 6c 65 6d 2d 3e 64 61 74 61 3b 0a 20 20 20 20  elem->data;.    
30f14 69 66 28 20 64 61 74 61 3d 3d 30 20 29 7b 0a 20  if( data==0 ){. 
30f15 20 20 20 20 20 66 74 73 33 52 65 6d 6f 76 65 45       fts3RemoveE
30f16 6c 65 6d 65 6e 74 42 79 48 61 73 68 28 70 48 2c  lementByHash(pH,
30f17 65 6c 65 6d 2c 68 29 3b 0a 20 20 20 20 7d 65 6c  elem,h);.    }el
30f18 73 65 7b 0a 20 20 20 20 20 20 65 6c 65 6d 2d 3e  se{.      elem->
30f19 64 61 74 61 20 3d 20 64 61 74 61 3b 0a 20 20 20  data = data;.   
30f1a 20 7d 0a 20 20 20 20 72 65 74 75 72 6e 20 6f 6c   }.    return ol
30f1b 64 5f 64 61 74 61 3b 0a 20 20 7d 0a 20 20 69 66  d_data;.  }.  if
30f1c 28 20 64 61 74 61 3d 3d 30 20 29 20 72 65 74 75  ( data==0 ) retu
30f1d 72 6e 20 30 3b 0a 20 20 69 66 28 20 70 48 2d 3e  rn 0;.  if( pH->
30f1e 68 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20  htsize==0 ){.   
30f1f 20 66 74 73 33 52 65 68 61 73 68 28 70 48 2c 38   fts3Rehash(pH,8
30f20 29 3b 0a 20 20 20 20 69 66 28 20 70 48 2d 3e 68  );.    if( pH->h
30f21 74 73 69 7a 65 3d 3d 30 20 29 7b 0a 20 20 20 20  tsize==0 ){.    
30f22 20 20 70 48 2d 3e 63 6f 75 6e 74 20 3d 20 30 3b    pH->count = 0;
30f23 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20 64 61  .      return da
30f24 74 61 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  ta;.    }.  }.  
30f25 6e 65 77 5f 65 6c 65 6d 20 3d 20 28 66 74 73 33  new_elem = (fts3
30f26 48 61 73 68 45 6c 65 6d 2a 29 66 74 73 33 48 61  HashElem*)fts3Ha
30f27 73 68 4d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f 66  shMalloc( sizeof
30f28 28 66 74 73 33 48 61 73 68 45 6c 65 6d 29 20 29  (fts3HashElem) )
30f29 3b 0a 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d  ;.  if( new_elem
30f2a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20 64 61 74  ==0 ) return dat
30f2b 61 3b 0a 20 20 69 66 28 20 70 48 2d 3e 63 6f 70  a;.  if( pH->cop
30f2c 79 4b 65 79 20 26 26 20 70 4b 65 79 21 3d 30 20  yKey && pKey!=0 
30f2d 29 7b 0a 20 20 20 20 6e 65 77 5f 65 6c 65 6d 2d  ){.    new_elem-
30f2e 3e 70 4b 65 79 20 3d 20 66 74 73 33 48 61 73 68  >pKey = fts3Hash
30f2f 4d 61 6c 6c 6f 63 28 20 6e 4b 65 79 20 29 3b 0a  Malloc( nKey );.
30f30 20 20 20 20 69 66 28 20 6e 65 77 5f 65 6c 65 6d      if( new_elem
30f31 2d 3e 70 4b 65 79 3d 3d 30 20 29 7b 0a 20 20 20  ->pKey==0 ){.   
30f32 20 20 20 66 74 73 33 48 61 73 68 46 72 65 65 28     fts3HashFree(
30f33 6e 65 77 5f 65 6c 65 6d 29 3b 0a 20 20 20 20 20  new_elem);.     
30f34 20 72 65 74 75 72 6e 20 64 61 74 61 3b 0a 20 20   return data;.  
30f35 20 20 7d 0a 20 20 20 20 6d 65 6d 63 70 79 28 28    }.    memcpy((
30f36 76 6f 69 64 2a 29 6e 65 77 5f 65 6c 65 6d 2d 3e  void*)new_elem->
30f37 70 4b 65 79 2c 20 70 4b 65 79 2c 20 6e 4b 65 79  pKey, pKey, nKey
30f38 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
30f39 6e 65 77 5f 65 6c 65 6d 2d 3e 70 4b 65 79 20 3d  new_elem->pKey =
30f3a 20 28 76 6f 69 64 2a 29 70 4b 65 79 3b 0a 20 20   (void*)pKey;.  
30f3b 7d 0a 20 20 6e 65 77 5f 65 6c 65 6d 2d 3e 6e 4b  }.  new_elem->nK
30f3c 65 79 20 3d 20 6e 4b 65 79 3b 0a 20 20 70 48 2d  ey = nKey;.  pH-
30f3d 3e 63 6f 75 6e 74 2b 2b 3b 0a 20 20 69 66 28 20  >count++;.  if( 
30f3e 70 48 2d 3e 63 6f 75 6e 74 20 3e 20 70 48 2d 3e  pH->count > pH->
30f3f 68 74 73 69 7a 65 20 29 7b 0a 20 20 20 20 66 74  htsize ){.    ft
30f40 73 33 52 65 68 61 73 68 28 70 48 2c 70 48 2d 3e  s3Rehash(pH,pH->
30f41 68 74 73 69 7a 65 2a 32 29 3b 0a 20 20 7d 0a 20  htsize*2);.  }. 
30f42 20 61 73 73 65 72 74 28 20 70 48 2d 3e 68 74 73   assert( pH->hts
30f43 69 7a 65 3e 30 20 29 3b 0a 20 20 61 73 73 65 72  ize>0 );.  asser
30f44 74 28 20 28 70 48 2d 3e 68 74 73 69 7a 65 20 26  t( (pH->htsize &
30f45 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d 31 29 29   (pH->htsize-1))
30f46 3d 3d 30 20 29 3b 0a 20 20 68 20 3d 20 68 72 61  ==0 );.  h = hra
30f47 77 20 26 20 28 70 48 2d 3e 68 74 73 69 7a 65 2d  w & (pH->htsize-
30f48 31 29 3b 0a 20 20 66 74 73 33 48 61 73 68 49 6e  1);.  fts3HashIn
30f49 73 65 72 74 45 6c 65 6d 65 6e 74 28 70 48 2c 20  sertElement(pH, 
30f4a 26 70 48 2d 3e 68 74 5b 68 5d 2c 20 6e 65 77 5f  &pH->ht[h], new_
30f4b 65 6c 65 6d 29 3b 0a 20 20 6e 65 77 5f 65 6c 65  elem);.  new_ele
30f4c 6d 2d 3e 64 61 74 61 20 3d 20 64 61 74 61 3b 0a  m->data = data;.
30f4d 20 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 23    return 0;.}..#
30f4e 65 6e 64 69 66 20 2f 2a 20 21 64 65 66 69 6e 65  endif /* !define
30f4f 64 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c  d(SQLITE_CORE) |
30f50 7c 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45  | defined(SQLITE
30f51 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29 20 2a 2f  _ENABLE_FTS3) */
30f52 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ../*************
30f53 2a 20 45 6e 64 20 6f 66 20 66 74 73 33 5f 68 61  * End of fts3_ha
30f54 73 68 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  sh.c ***********
30f55 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f56 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f57 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  /./*************
30f58 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74 73  * Begin file fts
30f59 33 5f 70 6f 72 74 65 72 2e 63 20 2a 2a 2a 2a 2a  3_porter.c *****
30f5a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f5b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f5c 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 36 20 53 65 70  /./*.** 2006 Sep
30f5d 74 65 6d 62 65 72 20 33 30 0a 2a 2a 0a 2a 2a 20  tember 30.**.** 
30f5e 54 68 65 20 61 75 74 68 6f 72 20 64 69 73 63 6c  The author discl
30f5f 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74 20 74  aims copyright t
30f60 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20 63 6f  o this source co
30f61 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20 6f 66  de.  In place of
30f62 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f 74 69  .** a legal noti
30f63 63 65 2c 20 68 65 72 65 20 69 73 20 61 20 62 6c  ce, here is a bl
30f64 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20  essing:.**.**   
30f65 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f 6f 64   May you do good
30f66 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a   and not evil..*
30f67 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66 69 6e  *    May you fin
30f68 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20 66 6f  d forgiveness fo
30f69 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64 20 66  r yourself and f
30f6a 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e 0a 2a  orgive others..*
30f6b 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73 68 61  *    May you sha
30f6c 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76 65 72  re freely, never
30f6d 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74 68 61   taking more tha
30f6e 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a  n you give..**.*
30f6f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f70 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f71 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f72 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
30f73 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 49 6d 70 6c  ********.** Impl
30f74 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
30f75 65 20 66 75 6c 6c 2d 74 65 78 74 2d 73 65 61 72  e full-text-sear
30f76 63 68 20 74 6f 6b 65 6e 69 7a 65 72 20 74 68 61  ch tokenizer tha
30f77 74 20 69 6d 70 6c 65 6d 65 6e 74 73 0a 2a 2a 20  t implements.** 
30f78 61 20 50 6f 72 74 65 72 20 73 74 65 6d 6d 65 72  a Porter stemmer
30f79 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ..*/../*.** The 
30f7a 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66 69 6c  code in this fil
30f7b 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c  e is only compil
30f7c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20  ed if:.**.**    
30f7d 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75   * The FTS3 modu
30f7e 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c  le is being buil
30f7f 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73 69 6f  t as an extensio
30f80 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e 20 77  n.**       (in w
30f81 68 69 63 68 20 63 61 73 65 20 53 51 4c 49 54 45  hich case SQLITE
30f82 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64 65 66  _CORE is not def
30f83 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20  ined), or.**.** 
30f84 20 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d      * The FTS3 m
30f85 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62  odule is being b
30f86 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20 63 6f  uilt into the co
30f87 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20 20 53  re of.**       S
30f88 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63 68 20  QLite (in which 
30f89 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e 41 42  case SQLITE_ENAB
30f8a 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66 69 6e  LE_FTS3 is defin
30f8b 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64 65 66  ed)..*/.#if !def
30f8c 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
30f8d 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
30f8e 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
30f8f 0a 0a 0a 0a 0a 2f 2a 0a 2a 2a 20 43 6c 61 73 73  ...../*.** Class
30f90 20 64 65 72 69 76 65 64 20 66 72 6f 6d 20 73 71   derived from sq
30f91 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 0a  lite3_tokenizer.
30f92 2a 2f 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  */.typedef struc
30f93 74 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a  t porter_tokeniz
30f94 65 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74  er {.  sqlite3_t
30f95 6f 6b 65 6e 69 7a 65 72 20 62 61 73 65 3b 20 20  okenizer base;  
30f96 20 20 20 20 2f 2a 20 42 61 73 65 20 63 6c 61 73      /* Base clas
30f97 73 20 2a 2f 0a 7d 20 70 6f 72 74 65 72 5f 74 6f  s */.} porter_to
30f98 6b 65 6e 69 7a 65 72 3b 0a 0a 2f 2a 0a 2a 2a 20  kenizer;../*.** 
30f99 43 6c 61 73 73 20 64 65 72 69 76 65 64 20 66 72  Class derived fr
30f9a 6f 6d 20 73 71 6c 69 74 33 5f 74 6f 6b 65 6e 69  om sqlit3_tokeni
30f9b 7a 65 72 5f 63 75 72 73 6f 72 0a 2a 2f 0a 74 79  zer_cursor.*/.ty
30f9c 70 65 64 65 66 20 73 74 72 75 63 74 20 70 6f 72  pedef struct por
30f9d 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75  ter_tokenizer_cu
30f9e 72 73 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33  rsor {.  sqlite3
30f9f 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
30fa0 72 20 62 61 73 65 3b 0a 20 20 63 6f 6e 73 74 20  r base;.  const 
30fa1 63 68 61 72 20 2a 7a 49 6e 70 75 74 3b 20 20 20  char *zInput;   
30fa2 20 20 20 20 20 20 20 2f 2a 20 69 6e 70 75 74 20         /* input 
30fa3 77 65 20 61 72 65 20 74 6f 6b 65 6e 69 7a 69 6e  we are tokenizin
30fa4 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49 6e 70 75  g */.  int nInpu
30fa5 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
30fa6 20 20 20 20 2f 2a 20 73 69 7a 65 20 6f 66 20 74      /* size of t
30fa7 68 65 20 69 6e 70 75 74 20 2a 2f 0a 20 20 69 6e  he input */.  in
30fa8 74 20 69 4f 66 66 73 65 74 3b 20 20 20 20 20 20  t iOffset;      
30fa9 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 63 75             /* cu
30faa 72 72 65 6e 74 20 70 6f 73 69 74 69 6f 6e 20 69  rrent position i
30fab 6e 20 7a 49 6e 70 75 74 20 2a 2f 0a 20 20 69 6e  n zInput */.  in
30fac 74 20 69 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20  t iToken;       
30fad 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69 6e             /* in
30fae 64 65 78 20 6f 66 20 6e 65 78 74 20 74 6f 6b 65  dex of next toke
30faf 6e 20 74 6f 20 62 65 20 72 65 74 75 72 6e 65 64  n to be returned
30fb0 20 2a 2f 0a 20 20 63 68 61 72 20 2a 7a 54 6f 6b   */.  char *zTok
30fb1 65 6e 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  en;             
30fb2 20 20 20 2f 2a 20 73 74 6f 72 61 67 65 20 66 6f     /* storage fo
30fb3 72 20 63 75 72 72 65 6e 74 20 74 6f 6b 65 6e 20  r current token 
30fb4 2a 2f 0a 20 20 69 6e 74 20 6e 41 6c 6c 6f 63 61  */.  int nAlloca
30fb5 74 65 64 3b 20 20 20 20 20 20 20 20 20 20 20 20  ted;            
30fb6 20 20 2f 2a 20 73 70 61 63 65 20 61 6c 6c 6f 63    /* space alloc
30fb7 61 74 65 64 20 74 6f 20 7a 54 6f 6b 65 6e 20 62  ated to zToken b
30fb8 75 66 66 65 72 20 2a 2f 0a 7d 20 70 6f 72 74 65  uffer */.} porte
30fb9 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  r_tokenizer_curs
30fba 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f 72 77 61 72 64  or;.../* Forward
30fbb 20 64 65 63 6c 61 72 61 74 69 6f 6e 20 2a 2f 0a   declaration */.
30fbc 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73 71 6c  static const sql
30fbd 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d  ite3_tokenizer_m
30fbe 6f 64 75 6c 65 20 70 6f 72 74 65 72 54 6f 6b 65  odule porterToke
30fbf 6e 69 7a 65 72 4d 6f 64 75 6c 65 3b 0a 0a 0a 2f  nizerModule;.../
30fc0 2a 0a 2a 2a 20 43 72 65 61 74 65 20 61 20 6e 65  *.** Create a ne
30fc1 77 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 73 74  w tokenizer inst
30fc2 61 6e 63 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  ance..*/.static 
30fc3 69 6e 74 20 70 6f 72 74 65 72 43 72 65 61 74 65  int porterCreate
30fc4 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 63 6f  (.  int argc, co
30fc5 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73 74  nst char * const
30fc6 20 2a 61 72 67 76 2c 0a 20 20 73 71 6c 69 74 65   *argv,.  sqlite
30fc7 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70  3_tokenizer **pp
30fc8 54 6f 6b 65 6e 69 7a 65 72 0a 29 7b 0a 20 20 70  Tokenizer.){.  p
30fc9 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 20  orter_tokenizer 
30fca 2a 74 3b 0a 20 20 74 20 3d 20 28 70 6f 72 74 65  *t;.  t = (porte
30fcb 72 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 29 20 73  r_tokenizer *) s
30fcc 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63 28 73 69  qlite3_malloc(si
30fcd 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 69 66 28  zeof(*t));.  if(
30fce 20 74 3d 3d 4e 55 4c 4c 20 29 20 72 65 74 75 72   t==NULL ) retur
30fcf 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a  n SQLITE_NOMEM;.
30fd0 20 20 6d 65 6d 73 65 74 28 74 2c 20 30 2c 20 73    memset(t, 0, s
30fd1 69 7a 65 6f 66 28 2a 74 29 29 3b 0a 20 20 2a 70  izeof(*t));.  *p
30fd2 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74 2d  pTokenizer = &t-
30fd3 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e 20  >base;.  return 
30fd4 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
30fd5 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74 6f  .** Destroy a to
30fd6 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69  kenizer.*/.stati
30fd7 63 20 69 6e 74 20 70 6f 72 74 65 72 44 65 73 74  c int porterDest
30fd8 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  roy(sqlite3_toke
30fd9 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
30fda 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  r){.  sqlite3_fr
30fdb 65 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b 0a  ee(pTokenizer);.
30fdc 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
30fdd 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72 65  OK;.}../*.** Pre
30fde 70 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74 6f  pare to begin to
30fdf 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74 69  kenizing a parti
30fe0 63 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20 54  cular string.  T
30fe1 68 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72 69  he input.** stri
30fe2 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69 7a  ng to be tokeniz
30fe3 65 64 20 69 73 20 7a 49 6e 70 75 74 5b 30 2e 2e  ed is zInput[0..
30fe4 6e 49 6e 70 75 74 2d 31 5d 2e 20 20 41 20 63 75  nInput-1].  A cu
30fe5 72 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f 20  rsor.** used to 
30fe6 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74 6f  incrementally to
30fe7 6b 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72 69  kenize this stri
30fe8 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64 20 69  ng is returned i
30fe9 6e 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72 2e  n .** *ppCursor.
30fea 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 70  .*/.static int p
30feb 6f 72 74 65 72 4f 70 65 6e 28 0a 20 20 73 71 6c  orterOpen(.  sql
30fec 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a  ite3_tokenizer *
30fed 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20 20  pTokenizer,     
30fee 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65 6e      /* The token
30fef 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  izer */.  const 
30ff0 63 68 61 72 20 2a 7a 49 6e 70 75 74 2c 20 69 6e  char *zInput, in
30ff1 74 20 6e 49 6e 70 75 74 2c 20 20 20 20 20 20 20  t nInput,       
30ff2 20 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62 65   /* String to be
30ff3 20 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20 20   tokenized */.  
30ff4 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65  sqlite3_tokenize
30ff5 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75 72  r_cursor **ppCur
30ff6 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20 54  sor    /* OUT: T
30ff7 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
30ff8 6f 72 20 2a 2f 0a 29 7b 0a 20 20 70 6f 72 74 65  or */.){.  porte
30ff9 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  r_tokenizer_curs
30ffa 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28 70  or *c;..  c = (p
30ffb 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65 72 5f  orter_tokenizer_
30ffc 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74 65  cursor *) sqlite
30ffd 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28  3_malloc(sizeof(
30ffe 2a 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d 4e  *c));.  if( c==N
30fff 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c  ULL ) return SQL
31000 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63 2d  ITE_NOMEM;..  c-
31001 3e 7a 49 6e 70 75 74 20 3d 20 7a 49 6e 70 75 74  >zInput = zInput
31002 3b 0a 20 20 69 66 28 20 7a 49 6e 70 75 74 3d 3d  ;.  if( zInput==
31003 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 49 6e 70  0 ){.    c->nInp
31004 75 74 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65 20  ut = 0;.  }else 
31005 69 66 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b 0a  if( nInput<0 ){.
31006 20 20 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20      c->nInput = 
31007 28 69 6e 74 29 73 74 72 6c 65 6e 28 7a 49 6e 70  (int)strlen(zInp
31008 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20  ut);.  }else{.  
31009 20 20 63 2d 3e 6e 49 6e 70 75 74 20 3d 20 6e 49    c->nInput = nI
3100a 6e 70 75 74 3b 0a 20 20 7d 0a 20 20 63 2d 3e 69  nput;.  }.  c->i
3100b 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20 20  Offset = 0;     
3100c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 73              /* s
3100d 74 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  tart tokenizing 
3100e 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e 67  at the beginning
3100f 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e 20   */.  c->iToken 
31010 3d 20 30 3b 0a 20 20 63 2d 3e 7a 54 6f 6b 65 6e  = 0;.  c->zToken
31011 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20 20   = NULL;        
31012 20 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70 61         /* no spa
31013 63 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79 65  ce allocated, ye
31014 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 41 6c 6c 6f  t. */.  c->nAllo
31015 63 61 74 65 64 20 3d 20 30 3b 0a 0a 20 20 2a 70  cated = 0;..  *p
31016 70 43 75 72 73 6f 72 20 3d 20 26 63 2d 3e 62 61  pCursor = &c->ba
31017 73 65 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  se;.  return SQL
31018 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ITE_OK;.}../*.**
31019 20 43 6c 6f 73 65 20 61 20 74 6f 6b 65 6e 69 7a   Close a tokeniz
3101a 61 74 69 6f 6e 20 63 75 72 73 6f 72 20 70 72 65  ation cursor pre
3101b 76 69 6f 75 73 6c 79 20 6f 70 65 6e 65 64 20 62  viously opened b
3101c 79 20 61 20 63 61 6c 6c 20 74 6f 0a 2a 2a 20 70  y a call to.** p
3101d 6f 72 74 65 72 4f 70 65 6e 28 29 20 61 62 6f 76  orterOpen() abov
3101e 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74  e..*/.static int
3101f 20 70 6f 72 74 65 72 43 6c 6f 73 65 28 73 71 6c   porterClose(sql
31020 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63  ite3_tokenizer_c
31021 75 72 73 6f 72 20 2a 70 43 75 72 73 6f 72 29 7b  ursor *pCursor){
31022 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
31023 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  zer_cursor *c = 
31024 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65  (porter_tokenize
31025 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  r_cursor *) pCur
31026 73 6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66  sor;.  sqlite3_f
31027 72 65 65 28 63 2d 3e 7a 54 6f 6b 65 6e 29 3b 0a  ree(c->zToken);.
31028 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 63    sqlite3_free(c
31029 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  );.  return SQLI
3102a 54 45 5f 4f 4b 3b 0a 7d 0a 2f 2a 0a 2a 2a 20 56  TE_OK;.}./*.** V
3102b 6f 77 65 6c 20 6f 72 20 63 6f 6e 73 6f 6e 61 6e  owel or consonan
3102c 74 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  t.*/.static cons
3102d 74 20 63 68 61 72 20 63 54 79 70 65 5b 5d 20 3d  t char cType[] =
3102e 20 7b 0a 20 20 20 30 2c 20 31 2c 20 31 2c 20 31   {.   0, 1, 1, 1
3102f 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 0, 1, 1, 1, 0,
31030 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31031 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  0, 1, 1, 1, 1, 1
31032 2c 20 30 2c 0a 20 20 20 31 2c 20 31 2c 20 31 2c  , 0,.   1, 1, 1,
31033 20 32 2c 20 31 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20   2, 1.};../*.** 
31034 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 29 20 61 6e  isConsonant() an
31035 64 20 69 73 56 6f 77 65 6c 28 29 20 64 65 74 65  d isVowel() dete
31036 72 6d 69 6e 65 20 69 66 20 74 68 65 69 72 20 66  rmine if their f
31037 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 69  irst character i
31038 6e 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20  n.** the string 
31039 74 68 65 79 20 70 6f 69 6e 74 20 74 6f 20 69 73  they point to is
3103a 20 61 20 63 6f 6e 73 6f 6e 61 6e 74 20 6f 72 20   a consonant or 
3103b 61 20 76 6f 77 65 6c 2c 20 61 63 63 6f 72 64 69  a vowel, accordi
3103c 6e 67 0a 2a 2a 20 74 6f 20 50 6f 72 74 65 72 20  ng.** to Porter 
3103d 72 75 6c 73 2e 20 20 0a 2a 2a 0a 2a 2a 20 41 20  ruls.  .**.** A 
3103e 63 6f 6e 73 6f 6e 61 74 65 20 69 73 20 61 6e 79  consonate is any
3103f 20 6c 65 74 74 65 72 20 6f 74 68 65 72 20 74 68   letter other th
31040 61 6e 20 27 61 27 2c 20 27 65 27 2c 20 27 69 27  an 'a', 'e', 'i'
31041 2c 20 27 6f 27 2c 20 6f 72 20 27 75 27 2e 0a 2a  , 'o', or 'u'..*
31042 2a 20 27 59 27 20 69 73 20 61 20 63 6f 6e 73 6f  * 'Y' is a conso
31043 6e 61 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 66  nant unless it f
31044 6f 6c 6c 6f 77 73 20 61 6e 6f 74 68 65 72 20 63  ollows another c
31045 6f 6e 73 6f 6e 61 6e 74 2c 0a 2a 2a 20 69 6e 20  onsonant,.** in 
31046 77 68 69 63 68 20 63 61 73 65 20 69 74 20 69 73  which case it is
31047 20 61 20 76 6f 77 65 6c 2e 0a 2a 2a 0a 2a 2a 20   a vowel..**.** 
31048 49 6e 20 74 68 65 73 65 20 72 6f 75 74 69 6e 65  In these routine
31049 2c 20 74 68 65 20 6c 65 74 74 65 72 73 20 61 72  , the letters ar
3104a 65 20 69 6e 20 72 65 76 65 72 73 65 20 6f 72 64  e in reverse ord
3104b 65 72 2e 20 20 53 6f 20 74 68 65 20 27 79 27 20  er.  So the 'y' 
3104c 72 75 6c 65 0a 2a 2a 20 69 73 20 74 68 61 74 20  rule.** is that 
3104d 27 79 27 20 69 73 20 61 20 63 6f 6e 73 6f 6e 61  'y' is a consona
3104e 6e 74 20 75 6e 6c 65 73 73 20 69 74 20 69 73 20  nt unless it is 
3104f 66 6f 6c 6c 6f 77 65 64 20 62 79 20 61 6e 6f 74  followed by anot
31050 68 65 72 0a 2a 2a 20 63 6f 6e 73 6f 6e 65 6e 74  her.** consonent
31051 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31052 69 73 56 6f 77 65 6c 28 63 6f 6e 73 74 20 63 68  isVowel(const ch
31053 61 72 2a 29 3b 0a 73 74 61 74 69 63 20 69 6e 74  ar*);.static int
31054 20 69 73 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e   isConsonant(con
31055 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
31056 6e 74 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d  nt j;.  char x =
31057 20 2a 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20   *z;.  if( x==0 
31058 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73  ) return 0;.  as
31059 73 65 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20  sert( x>='a' && 
3105a 78 3c 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20  x<='z' );.  j = 
3105b 63 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20  cType[x-'a'];.  
3105c 69 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e  if( j<2 ) return
3105d 20 6a 3b 0a 20 20 72 65 74 75 72 6e 20 7a 5b 31   j;.  return z[1
3105e 5d 3d 3d 30 20 7c 7c 20 69 73 56 6f 77 65 6c 28  ]==0 || isVowel(
3105f 7a 20 2b 20 31 29 3b 0a 7d 0a 73 74 61 74 69 63  z + 1);.}.static
31060 20 69 6e 74 20 69 73 56 6f 77 65 6c 28 63 6f 6e   int isVowel(con
31061 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 69  st char *z){.  i
31062 6e 74 20 6a 3b 0a 20 20 63 68 61 72 20 78 20 3d  nt j;.  char x =
31063 20 2a 7a 3b 0a 20 20 69 66 28 20 78 3d 3d 30 20   *z;.  if( x==0 
31064 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 61 73  ) return 0;.  as
31065 73 65 72 74 28 20 78 3e 3d 27 61 27 20 26 26 20  sert( x>='a' && 
31066 78 3c 3d 27 7a 27 20 29 3b 0a 20 20 6a 20 3d 20  x<='z' );.  j = 
31067 63 54 79 70 65 5b 78 2d 27 61 27 5d 3b 0a 20 20  cType[x-'a'];.  
31068 69 66 28 20 6a 3c 32 20 29 20 72 65 74 75 72 6e  if( j<2 ) return
31069 20 31 2d 6a 3b 0a 20 20 72 65 74 75 72 6e 20 69   1-j;.  return i
3106a 73 43 6f 6e 73 6f 6e 61 6e 74 28 7a 20 2b 20 31  sConsonant(z + 1
3106b 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 4c 65 74 20  );.}../*.** Let 
3106c 61 6e 79 20 73 65 71 75 65 6e 63 65 20 6f 66 20  any sequence of 
3106d 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 76 6f 77 65  one or more vowe
3106e 6c 73 20 62 65 20 72 65 70 72 65 73 65 6e 74 65  ls be represente
3106f 64 20 62 79 20 56 20 61 6e 64 20 6c 65 74 0a 2a  d by V and let.*
31070 2a 20 43 20 62 65 20 73 65 71 75 65 6e 63 65 20  * C be sequence 
31071 6f 66 20 6f 6e 65 20 6f 72 20 6d 6f 72 65 20 63  of one or more c
31072 6f 6e 73 6f 6e 61 6e 74 73 2e 20 20 54 68 65 6e  onsonants.  Then
31073 20 65 76 65 72 79 20 77 6f 72 64 20 63 61 6e 20   every word can 
31074 62 65 0a 2a 2a 20 72 65 70 72 65 73 65 6e 74 65  be.** represente
31075 64 20 61 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  d as:.**.**     
31076 20 20 20 20 20 20 5b 43 5d 20 28 56 43 29 7b 6d        [C] (VC){m
31077 7d 20 5b 56 5d 0a 2a 2a 0a 2a 2a 20 49 6e 20 70  } [V].**.** In p
31078 72 6f 73 65 3a 20 20 41 20 77 6f 72 64 20 69 73  rose:  A word is
31079 20 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 63 6f 6e   an optional con
3107a 73 6f 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20  sonant followed 
3107b 62 79 20 7a 65 72 6f 20 6f 72 0a 2a 2a 20 76 6f  by zero or.** vo
3107c 77 65 6c 2d 63 6f 6e 73 6f 6e 61 6e 74 20 70 61  wel-consonant pa
3107d 69 72 73 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  irs followed by 
3107e 61 6e 20 6f 70 74 69 6f 6e 61 6c 20 76 6f 77 65  an optional vowe
3107f 6c 2e 20 20 22 6d 22 20 69 73 20 74 68 65 0a 2a  l.  "m" is the.*
31080 2a 20 6e 75 6d 62 65 72 20 6f 66 20 76 6f 77 65  * number of vowe
31081 6c 20 63 6f 6e 73 6f 6e 61 6e 74 20 70 61 69 72  l consonant pair
31082 73 2e 20 20 54 68 69 73 20 72 6f 75 74 69 6e 65  s.  This routine
31083 20 63 6f 6d 70 75 74 65 73 20 74 68 65 20 76 61   computes the va
31084 6c 75 65 0a 2a 2a 20 6f 66 20 6d 20 66 6f 72 20  lue.** of m for 
31085 74 68 65 20 66 69 72 73 74 20 69 20 62 79 74 65  the first i byte
31086 73 20 6f 66 20 61 20 77 6f 72 64 2e 0a 2a 2a 0a  s of a word..**.
31087 2a 2a 20 52 65 74 75 72 6e 20 74 72 75 65 20 69  ** Return true i
31088 66 20 74 68 65 20 6d 2d 76 61 6c 75 65 20 66 6f  f the m-value fo
31089 72 20 7a 20 69 73 20 31 20 6f 72 20 6d 6f 72 65  r z is 1 or more
3108a 2e 20 20 49 6e 20 6f 74 68 65 72 20 77 6f 72 64  .  In other word
3108b 73 2c 0a 2a 2a 20 72 65 74 75 72 6e 20 74 72 75  s,.** return tru
3108c 65 20 69 66 20 7a 20 63 6f 6e 74 61 69 6e 73 20  e if z contains 
3108d 61 74 20 6c 65 61 73 74 20 6f 6e 65 20 76 6f 77  at least one vow
3108e 65 6c 20 74 68 61 74 20 69 73 20 66 6f 6c 6c 6f  el that is follo
3108f 77 65 64 0a 2a 2a 20 62 79 20 61 20 63 6f 6e 73  wed.** by a cons
31090 6f 6e 61 6e 74 2e 0a 2a 2a 0a 2a 2a 20 49 6e 20  onant..**.** In 
31091 74 68 69 73 20 72 6f 75 74 69 6e 65 20 7a 5b 5d  this routine z[]
31092 20 69 73 20 69 6e 20 72 65 76 65 72 73 65 20 6f   is in reverse o
31093 72 64 65 72 2e 20 20 53 6f 20 77 65 20 61 72 65  rder.  So we are
31094 20 72 65 61 6c 6c 79 20 6c 6f 6f 6b 69 6e 67 0a   really looking.
31095 2a 2a 20 66 6f 72 20 61 6e 20 69 6e 73 74 61 6e  ** for an instan
31096 63 65 20 6f 66 20 6f 66 20 61 20 63 6f 6e 73 6f  ce of of a conso
31097 6e 61 6e 74 20 66 6f 6c 6c 6f 77 65 64 20 62 79  nant followed by
31098 20 61 20 76 6f 77 65 6c 2e 0a 2a 2f 0a 73 74 61   a vowel..*/.sta
31099 74 69 63 20 69 6e 74 20 6d 5f 67 74 5f 30 28 63  tic int m_gt_0(c
3109a 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
3109b 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28   while( isVowel(
3109c 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
3109d 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
3109e 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 0;.  while( is
3109f 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
310a0 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  z++; }.  return 
310a1 2a 7a 21 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b  *z!=0;.}../* Lik
310a2 65 20 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63  e mgt0 above exc
310a3 65 70 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ept we are looki
310a4 6e 67 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f  ng for a value o
310a5 66 20 6d 20 77 68 69 63 68 20 69 73 0a 2a 2a 20  f m which is.** 
310a6 65 78 61 63 74 6c 79 20 31 0a 2a 2f 0a 73 74 61  exactly 1.*/.sta
310a7 74 69 63 20 69 6e 74 20 6d 5f 65 71 5f 31 28 63  tic int m_eq_1(c
310a8 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
310a9 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28   while( isVowel(
310aa 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
310ab 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
310ac 6e 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 0;.  while( is
310ad 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
310ae 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d  z++; }.  if( *z=
310af 3d 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20  =0 ) return 0;. 
310b0 20 77 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28   while( isVowel(
310b1 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69  z) ){ z++; }.  i
310b2 66 28 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72  f( *z==0 ) retur
310b3 6e 20 31 3b 0a 20 20 77 68 69 6c 65 28 20 69 73  n 1;.  while( is
310b4 43 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20  Consonant(z) ){ 
310b5 7a 2b 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20  z++; }.  return 
310b6 2a 7a 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 20 4c 69 6b  *z==0;.}../* Lik
310b7 65 20 6d 67 74 30 20 61 62 6f 76 65 20 65 78 63  e mgt0 above exc
310b8 65 70 74 20 77 65 20 61 72 65 20 6c 6f 6f 6b 69  ept we are looki
310b9 6e 67 20 66 6f 72 20 61 20 76 61 6c 75 65 20 6f  ng for a value o
310ba 66 20 6d 3e 31 20 69 6e 73 74 65 61 64 0a 2a 2a  f m>1 instead.**
310bb 20 6f 72 20 6d 3e 30 0a 2a 2f 0a 73 74 61 74 69   or m>0.*/.stati
310bc 63 20 69 6e 74 20 6d 5f 67 74 5f 31 28 63 6f 6e  c int m_gt_1(con
310bd 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 77  st char *z){.  w
310be 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
310bf 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
310c0 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
310c1 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
310c2 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
310c3 2b 3b 20 7d 0a 20 20 69 66 28 20 2a 7a 3d 3d 30  +; }.  if( *z==0
310c4 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 77   ) return 0;.  w
310c5 68 69 6c 65 28 20 69 73 56 6f 77 65 6c 28 7a 29  hile( isVowel(z)
310c6 20 29 7b 20 7a 2b 2b 3b 20 7d 0a 20 20 69 66 28   ){ z++; }.  if(
310c7 20 2a 7a 3d 3d 30 20 29 20 72 65 74 75 72 6e 20   *z==0 ) return 
310c8 30 3b 0a 20 20 77 68 69 6c 65 28 20 69 73 43 6f  0;.  while( isCo
310c9 6e 73 6f 6e 61 6e 74 28 7a 29 20 29 7b 20 7a 2b  nsonant(z) ){ z+
310ca 2b 3b 20 7d 0a 20 20 72 65 74 75 72 6e 20 2a 7a  +; }.  return *z
310cb 21 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65  !=0;.}../*.** Re
310cc 74 75 72 6e 20 54 52 55 45 20 69 66 20 74 68 65  turn TRUE if the
310cd 72 65 20 69 73 20 61 20 76 6f 77 65 6c 20 61 6e  re is a vowel an
310ce 79 77 68 65 72 65 20 77 69 74 68 69 6e 20 7a 5b  ywhere within z[
310cf 30 2e 2e 6e 2d 31 5d 0a 2a 2f 0a 73 74 61 74 69  0..n-1].*/.stati
310d0 63 20 69 6e 74 20 68 61 73 56 6f 77 65 6c 28 63  c int hasVowel(c
310d1 6f 6e 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20  onst char *z){. 
310d2 20 77 68 69 6c 65 28 20 69 73 43 6f 6e 73 6f 6e   while( isConson
310d3 61 6e 74 28 7a 29 20 29 7b 20 7a 2b 2b 3b 20 7d  ant(z) ){ z++; }
310d4 0a 20 20 72 65 74 75 72 6e 20 2a 7a 21 3d 30 3b  .  return *z!=0;
310d5 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
310d6 20 54 52 55 45 20 69 66 20 74 68 65 20 77 6f 72   TRUE if the wor
310d7 64 20 65 6e 64 73 20 69 6e 20 61 20 64 6f 75 62  d ends in a doub
310d8 6c 65 20 63 6f 6e 73 6f 6e 61 6e 74 2e 0a 2a 2a  le consonant..**
310d9 0a 2a 2a 20 54 68 65 20 74 65 78 74 20 69 73 20  .** The text is 
310da 72 65 76 65 72 73 65 64 20 68 65 72 65 2e 20 53  reversed here. S
310db 6f 20 77 65 20 61 72 65 20 72 65 61 6c 6c 79 20  o we are really 
310dc 6c 6f 6f 6b 69 6e 67 20 61 74 0a 2a 2a 20 74 68  looking at.** th
310dd 65 20 66 69 72 73 74 20 74 77 6f 20 63 68 61 72  e first two char
310de 61 63 74 65 72 73 20 6f 66 20 7a 5b 5d 2e 0a 2a  acters of z[]..*
310df 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 6f 75  /.static int dou
310e0 62 6c 65 43 6f 6e 73 6f 6e 61 6e 74 28 63 6f 6e  bleConsonant(con
310e1 73 74 20 63 68 61 72 20 2a 7a 29 7b 0a 20 20 72  st char *z){.  r
310e2 65 74 75 72 6e 20 69 73 43 6f 6e 73 6f 6e 61 6e  eturn isConsonan
310e3 74 28 7a 29 20 26 26 20 7a 5b 30 5d 3d 3d 7a 5b  t(z) && z[0]==z[
310e4 31 5d 20 26 26 20 69 73 43 6f 6e 73 6f 6e 61 6e  1] && isConsonan
310e5 74 28 7a 2b 31 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  t(z+1);.}../*.**
310e6 20 52 65 74 75 72 6e 20 54 52 55 45 20 69 66 20   Return TRUE if 
310e7 74 68 65 20 77 6f 72 64 20 65 6e 64 73 20 77 69  the word ends wi
310e8 74 68 20 74 68 72 65 65 20 6c 65 74 74 65 72 73  th three letters
310e9 20 77 68 69 63 68 0a 2a 2a 20 61 72 65 20 63 6f   which.** are co
310ea 6e 73 6f 6e 61 6e 74 2d 76 6f 77 65 6c 2d 63 6f  nsonant-vowel-co
310eb 6e 73 6f 6e 65 6e 74 20 61 6e 64 20 77 68 65 72  nsonent and wher
310ec 65 20 74 68 65 20 66 69 6e 61 6c 20 63 6f 6e 73  e the final cons
310ed 6f 6e 61 6e 74 0a 2a 2a 20 69 73 20 6e 6f 74 20  onant.** is not 
310ee 27 77 27 2c 20 27 78 27 2c 20 6f 72 20 27 79 27  'w', 'x', or 'y'
310ef 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 77 6f 72 64  ..**.** The word
310f0 20 69 73 20 72 65 76 65 72 73 65 64 20 68 65 72   is reversed her
310f1 65 2e 20 20 53 6f 20 77 65 20 61 72 65 20 72 65  e.  So we are re
310f2 61 6c 6c 79 20 63 68 65 63 6b 69 6e 67 20 74 68  ally checking th
310f3 65 0a 2a 2a 20 66 69 72 73 74 20 74 68 72 65 65  e.** first three
310f4 20 6c 65 74 74 65 72 73 20 61 6e 64 20 74 68 65   letters and the
310f5 20 66 69 72 73 74 20 6f 6e 65 20 63 61 6e 6e 6f   first one canno
310f6 74 20 62 65 20 69 6e 20 5b 77 78 79 5d 2e 0a 2a  t be in [wxy]..*
310f7 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 74 61  /.static int sta
310f8 72 5f 6f 68 28 63 6f 6e 73 74 20 63 68 61 72 20  r_oh(const char 
310f9 2a 7a 29 7b 0a 20 20 72 65 74 75 72 6e 0a 20 20  *z){.  return.  
310fa 20 20 7a 5b 30 5d 21 3d 30 20 26 26 20 69 73 43    z[0]!=0 && isC
310fb 6f 6e 73 6f 6e 61 6e 74 28 7a 29 20 26 26 0a 20  onsonant(z) &&. 
310fc 20 20 20 7a 5b 30 5d 21 3d 27 77 27 20 26 26 20     z[0]!='w' && 
310fd 7a 5b 30 5d 21 3d 27 78 27 20 26 26 20 7a 5b 30  z[0]!='x' && z[0
310fe 5d 21 3d 27 79 27 20 26 26 0a 20 20 20 20 7a 5b  ]!='y' &&.    z[
310ff 31 5d 21 3d 30 20 26 26 20 69 73 56 6f 77 65 6c  1]!=0 && isVowel
31100 28 7a 2b 31 29 20 26 26 0a 20 20 20 20 7a 5b 32  (z+1) &&.    z[2
31101 5d 21 3d 30 20 26 26 20 69 73 43 6f 6e 73 6f 6e  ]!=0 && isConson
31102 61 6e 74 28 7a 2b 32 29 3b 0a 7d 0a 0a 2f 2a 0a  ant(z+2);.}../*.
31103 2a 2a 20 49 66 20 74 68 65 20 77 6f 72 64 20 65  ** If the word e
31104 6e 64 73 20 77 69 74 68 20 7a 46 72 6f 6d 20 61  nds with zFrom a
31105 6e 64 20 78 43 6f 6e 64 28 29 20 69 73 20 74 72  nd xCond() is tr
31106 75 65 20 66 6f 72 20 74 68 65 20 73 74 65 6d 0a  ue for the stem.
31107 2a 2a 20 6f 66 20 74 68 65 20 77 6f 72 64 20 74  ** of the word t
31108 68 61 74 20 70 72 65 63 65 65 64 73 20 74 68 65  hat preceeds the
31109 20 7a 46 72 6f 6d 20 65 6e 64 69 6e 67 2c 20 74   zFrom ending, t
3110a 68 65 6e 20 63 68 61 6e 67 65 20 74 68 65 20 0a  hen change the .
3110b 2a 2a 20 65 6e 64 69 6e 67 20 74 6f 20 7a 54 6f  ** ending to zTo
3110c 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 69 6e 70 75  ..**.** The inpu
3110d 74 20 77 6f 72 64 20 2a 70 7a 20 61 6e 64 20 7a  t word *pz and z
3110e 46 72 6f 6d 20 61 72 65 20 62 6f 74 68 20 69 6e  From are both in
3110f 20 72 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20   reverse order. 
31110 20 7a 54 6f 0a 2a 2a 20 69 73 20 69 6e 20 6e 6f   zTo.** is in no
31111 72 6d 61 6c 20 6f 72 64 65 72 2e 20 0a 2a 2a 0a  rmal order. .**.
31112 2a 2a 20 52 65 74 75 72 6e 20 54 52 55 45 20 69  ** Return TRUE i
31113 66 20 7a 46 72 6f 6d 20 6d 61 74 63 68 65 73 2e  f zFrom matches.
31114 20 20 52 65 74 75 72 6e 20 46 41 4c 53 45 20 69    Return FALSE i
31115 66 20 7a 46 72 6f 6d 20 64 6f 65 73 20 6e 6f 74  f zFrom does not
31116 0a 2a 2a 20 6d 61 74 63 68 2e 20 20 4e 6f 74 20  .** match.  Not 
31117 74 68 61 74 20 54 52 55 45 20 69 73 20 72 65 74  that TRUE is ret
31118 75 72 6e 65 64 20 65 76 65 6e 20 69 66 20 78 43  urned even if xC
31119 6f 6e 64 28 29 20 66 61 69 6c 73 20 61 6e 64 0a  ond() fails and.
3111a 2a 2a 20 6e 6f 20 73 75 62 73 74 69 74 75 74 69  ** no substituti
3111b 6f 6e 20 6f 63 63 75 72 73 2e 0a 2a 2f 0a 73 74  on occurs..*/.st
3111c 61 74 69 63 20 69 6e 74 20 73 74 65 6d 28 0a 20  atic int stem(. 
3111d 20 63 68 61 72 20 2a 2a 70 7a 2c 20 20 20 20 20   char **pz,     
3111e 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 77          /* The w
3111f 6f 72 64 20 62 65 69 6e 67 20 73 74 65 6d 6d 65  ord being stemme
31120 64 20 28 52 65 76 65 72 73 65 64 29 20 2a 2f 0a  d (Reversed) */.
31121 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 46    const char *zF
31122 72 6f 6d 2c 20 20 20 20 20 2f 2a 20 49 66 20 74  rom,     /* If t
31123 68 65 20 65 6e 64 69 6e 67 20 6d 61 74 63 68 65  he ending matche
31124 73 20 74 68 69 73 2e 2e 2e 20 28 52 65 76 65 72  s this... (Rever
31125 73 65 64 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20  sed) */.  const 
31126 63 68 61 72 20 2a 7a 54 6f 2c 20 20 20 20 20 20  char *zTo,      
31127 20 2f 2a 20 2e 2e 2e 20 63 68 61 6e 67 65 20 74   /* ... change t
31128 68 65 20 65 6e 64 69 6e 67 20 74 6f 20 74 68 69  he ending to thi
31129 73 20 28 6e 6f 74 20 72 65 76 65 72 73 65 64 29  s (not reversed)
3112a 20 2a 2f 0a 20 20 69 6e 74 20 28 2a 78 43 6f 6e   */.  int (*xCon
3112b 64 29 28 63 6f 6e 73 74 20 63 68 61 72 2a 29 20  d)(const char*) 
3112c 20 20 2f 2a 20 43 6f 6e 64 69 74 69 6f 6e 20 74    /* Condition t
3112d 68 61 74 20 6d 75 73 74 20 62 65 20 74 72 75 65  hat must be true
3112e 20 2a 2f 0a 29 7b 0a 20 20 63 68 61 72 20 2a 7a   */.){.  char *z
3112f 20 3d 20 2a 70 7a 3b 0a 20 20 77 68 69 6c 65 28   = *pz;.  while(
31130 20 2a 7a 46 72 6f 6d 20 26 26 20 2a 7a 46 72 6f   *zFrom && *zFro
31131 6d 3d 3d 2a 7a 20 29 7b 20 7a 2b 2b 3b 20 7a 46  m==*z ){ z++; zF
31132 72 6f 6d 2b 2b 3b 20 7d 0a 20 20 69 66 28 20 2a  rom++; }.  if( *
31133 7a 46 72 6f 6d 21 3d 30 20 29 20 72 65 74 75 72  zFrom!=0 ) retur
31134 6e 20 30 3b 0a 20 20 69 66 28 20 78 43 6f 6e 64  n 0;.  if( xCond
31135 20 26 26 20 21 78 43 6f 6e 64 28 7a 29 20 29 20   && !xCond(z) ) 
31136 72 65 74 75 72 6e 20 31 3b 0a 20 20 77 68 69 6c  return 1;.  whil
31137 65 28 20 2a 7a 54 6f 20 29 7b 0a 20 20 20 20 2a  e( *zTo ){.    *
31138 28 2d 2d 7a 29 20 3d 20 2a 28 7a 54 6f 2b 2b 29  (--z) = *(zTo++)
31139 3b 0a 20 20 7d 0a 20 20 2a 70 7a 20 3d 20 7a 3b  ;.  }.  *pz = z;
3113a 0a 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a  .  return 1;.}..
3113b 2f 2a 0a 2a 2a 20 54 68 69 73 20 69 73 20 74 68  /*.** This is th
3113c 65 20 66 61 6c 6c 62 61 63 6b 20 73 74 65 6d 6d  e fallback stemm
3113d 65 72 20 75 73 65 64 20 77 68 65 6e 20 74 68 65  er used when the
3113e 20 70 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 20   porter stemmer 
3113f 69 73 0a 2a 2a 20 69 6e 61 70 70 72 6f 70 72 69  is.** inappropri
31140 61 74 65 2e 20 20 54 68 65 20 69 6e 70 75 74 20  ate.  The input 
31141 77 6f 72 64 20 69 73 20 63 6f 70 69 65 64 20 69  word is copied i
31142 6e 74 6f 20 74 68 65 20 6f 75 74 70 75 74 20 77  nto the output w
31143 69 74 68 0a 2a 2a 20 55 53 2d 41 53 43 49 49 20  ith.** US-ASCII 
31144 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e 20 20 49  case folding.  I
31145 66 20 74 68 65 20 69 6e 70 75 74 20 77 6f 72 64  f the input word
31146 20 69 73 20 74 6f 6f 20 6c 6f 6e 67 20 28 6d 6f   is too long (mo
31147 72 65 0a 2a 2a 20 74 68 61 6e 20 32 30 20 62 79  re.** than 20 by
31148 74 65 73 20 69 66 20 69 74 20 63 6f 6e 74 61 69  tes if it contai
31149 6e 73 20 6e 6f 20 64 69 67 69 74 73 20 6f 72 20  ns no digits or 
3114a 6d 6f 72 65 20 74 68 61 6e 20 36 20 62 79 74 65  more than 6 byte
3114b 73 20 69 66 0a 2a 2a 20 69 74 20 63 6f 6e 74 61  s if.** it conta
3114c 69 6e 73 20 64 69 67 69 74 73 29 20 74 68 65 6e  ins digits) then
3114d 20 77 6f 72 64 20 69 73 20 74 72 75 6e 63 61 74   word is truncat
3114e 65 64 20 74 6f 20 32 30 20 6f 72 20 36 20 62 79  ed to 20 or 6 by
3114f 74 65 73 0a 2a 2a 20 62 79 20 74 61 6b 69 6e 67  tes.** by taking
31150 20 31 30 20 6f 72 20 33 20 62 79 74 65 73 20 66   10 or 3 bytes f
31151 72 6f 6d 20 74 68 65 20 62 65 67 69 6e 6e 69 6e  rom the beginnin
31152 67 20 61 6e 64 20 65 6e 64 2e 0a 2a 2f 0a 73 74  g and end..*/.st
31153 61 74 69 63 20 76 6f 69 64 20 63 6f 70 79 5f 73  atic void copy_s
31154 74 65 6d 6d 65 72 28 63 6f 6e 73 74 20 63 68 61  temmer(const cha
31155 72 20 2a 7a 49 6e 2c 20 69 6e 74 20 6e 49 6e 2c  r *zIn, int nIn,
31156 20 63 68 61 72 20 2a 7a 4f 75 74 2c 20 69 6e 74   char *zOut, int
31157 20 2a 70 6e 4f 75 74 29 7b 0a 20 20 69 6e 74 20   *pnOut){.  int 
31158 69 2c 20 6d 78 2c 20 6a 3b 0a 20 20 69 6e 74 20  i, mx, j;.  int 
31159 68 61 73 44 69 67 69 74 20 3d 20 30 3b 0a 20 20  hasDigit = 0;.  
3115a 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 49 6e 3b 20  for(i=0; i<nIn; 
3115b 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20 63 20  i++){.    int c 
3115c 3d 20 7a 49 6e 5b 69 5d 3b 0a 20 20 20 20 69 66  = zIn[i];.    if
3115d 28 20 63 3e 3d 27 41 27 20 26 26 20 63 3c 3d 27  ( c>='A' && c<='
3115e 5a 27 20 29 7b 0a 20 20 20 20 20 20 7a 4f 75 74  Z' ){.      zOut
3115f 5b 69 5d 20 3d 20 63 20 2d 20 27 41 27 20 2b 20  [i] = c - 'A' + 
31160 27 61 27 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a  'a';.    }else{.
31161 20 20 20 20 20 20 69 66 28 20 63 3e 3d 27 30 27        if( c>='0'
31162 20 26 26 20 63 3c 3d 27 39 27 20 29 20 68 61 73   && c<='9' ) has
31163 44 69 67 69 74 20 3d 20 31 3b 0a 20 20 20 20 20  Digit = 1;.     
31164 20 7a 4f 75 74 5b 69 5d 20 3d 20 63 3b 0a 20 20   zOut[i] = c;.  
31165 20 20 7d 0a 20 20 7d 0a 20 20 6d 78 20 3d 20 68    }.  }.  mx = h
31166 61 73 44 69 67 69 74 20 3f 20 33 20 3a 20 31 30  asDigit ? 3 : 10
31167 3b 0a 20 20 69 66 28 20 6e 49 6e 3e 6d 78 2a 32  ;.  if( nIn>mx*2
31168 20 29 7b 0a 20 20 20 20 66 6f 72 28 6a 3d 6d 78   ){.    for(j=mx
31169 2c 20 69 3d 6e 49 6e 2d 6d 78 3b 20 69 3c 6e 49  , i=nIn-mx; i<nI
3116a 6e 3b 20 69 2b 2b 2c 20 6a 2b 2b 29 7b 0a 20 20  n; i++, j++){.  
3116b 20 20 20 20 7a 4f 75 74 5b 6a 5d 20 3d 20 7a 4f      zOut[j] = zO
3116c 75 74 5b 69 5d 3b 0a 20 20 20 20 7d 0a 20 20 20  ut[i];.    }.   
3116d 20 69 20 3d 20 6a 3b 0a 20 20 7d 0a 20 20 7a 4f   i = j;.  }.  zO
3116e 75 74 5b 69 5d 20 3d 20 30 3b 0a 20 20 2a 70 6e  ut[i] = 0;.  *pn
3116f 4f 75 74 20 3d 20 69 3b 0a 7d 0a 0a 0a 2f 2a 0a  Out = i;.}.../*.
31170 2a 2a 20 53 74 65 6d 20 74 68 65 20 69 6e 70 75  ** Stem the inpu
31171 74 20 77 6f 72 64 20 7a 49 6e 5b 30 2e 2e 6e 49  t word zIn[0..nI
31172 6e 2d 31 5d 2e 20 20 53 74 6f 72 65 20 74 68 65  n-1].  Store the
31173 20 6f 75 74 70 75 74 20 69 6e 20 7a 4f 75 74 2e   output in zOut.
31174 0a 2a 2a 20 7a 4f 75 74 20 69 73 20 61 74 20 6c  .** zOut is at l
31175 65 61 73 74 20 62 69 67 20 65 6e 6f 75 67 68 20  east big enough 
31176 74 6f 20 68 6f 6c 64 20 6e 49 6e 20 62 79 74 65  to hold nIn byte
31177 73 2e 20 20 57 72 69 74 65 20 74 68 65 20 61 63  s.  Write the ac
31178 74 75 61 6c 0a 2a 2a 20 73 69 7a 65 20 6f 66 20  tual.** size of 
31179 74 68 65 20 6f 75 74 70 75 74 20 77 6f 72 64 20  the output word 
3117a 28 65 78 63 6c 75 73 69 76 65 20 6f 66 20 74 68  (exclusive of th
3117b 65 20 27 5c 30 27 20 74 65 72 6d 69 6e 61 74 6f  e '\0' terminato
3117c 72 29 20 69 6e 74 6f 20 2a 70 6e 4f 75 74 2e 0a  r) into *pnOut..
3117d 2a 2a 0a 2a 2a 20 41 6e 79 20 75 70 70 65 72 2d  **.** Any upper-
3117e 63 61 73 65 20 63 68 61 72 61 63 74 65 72 73 20  case characters 
3117f 69 6e 20 74 68 65 20 55 53 2d 41 53 43 49 49 20  in the US-ASCII 
31180 63 68 61 72 61 63 74 65 72 20 73 65 74 20 28 5b  character set ([
31181 41 2d 5a 5d 29 0a 2a 2a 20 61 72 65 20 63 6f 6e  A-Z]).** are con
31182 76 65 72 74 65 64 20 74 6f 20 6c 6f 77 65 72 20  verted to lower 
31183 63 61 73 65 2e 20 20 55 70 70 65 72 2d 63 61 73  case.  Upper-cas
31184 65 20 55 54 46 20 63 68 61 72 61 63 74 65 72 73  e UTF characters
31185 20 61 72 65 0a 2a 2a 20 75 6e 63 68 61 6e 67 65   are.** unchange
31186 64 2e 0a 2a 2a 0a 2a 2a 20 57 6f 72 64 73 20 74  d..**.** Words t
31187 68 61 74 20 61 72 65 20 6c 6f 6e 67 65 72 20 74  hat are longer t
31188 68 61 6e 20 61 62 6f 75 74 20 32 30 20 62 79 74  han about 20 byt
31189 65 73 20 61 72 65 20 73 74 65 6d 6d 65 64 20 62  es are stemmed b
3118a 79 20 72 65 74 61 69 6e 69 6e 67 0a 2a 2a 20 61  y retaining.** a
3118b 20 66 65 77 20 62 79 74 65 73 20 66 72 6f 6d 20   few bytes from 
3118c 74 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e  the beginning an
3118d 64 20 74 68 65 20 65 6e 64 20 6f 66 20 74 68 65  d the end of the
3118e 20 77 6f 72 64 2e 20 20 49 66 20 74 68 65 0a 2a   word.  If the.*
3118f 2a 20 77 6f 72 64 20 63 6f 6e 74 61 69 6e 73 20  * word contains 
31190 64 69 67 69 74 73 2c 20 33 20 62 79 74 65 73 20  digits, 3 bytes 
31191 61 72 65 20 74 61 6b 65 6e 20 66 72 6f 6d 20 74  are taken from t
31192 68 65 20 62 65 67 69 6e 6e 69 6e 67 20 61 6e 64  he beginning and
31193 0a 2a 2a 20 33 20 62 79 74 65 73 20 66 72 6f 6d  .** 3 bytes from
31194 20 74 68 65 20 65 6e 64 2e 20 20 46 6f 72 20 6c   the end.  For l
31195 6f 6e 67 20 77 6f 72 64 73 20 77 69 74 68 6f 75  ong words withou
31196 74 20 64 69 67 69 74 73 2c 20 31 30 20 62 79 74  t digits, 10 byt
31197 65 73 0a 2a 2a 20 61 72 65 20 74 61 6b 65 6e 20  es.** are taken 
31198 66 72 6f 6d 20 65 61 63 68 20 65 6e 64 2e 20 20  from each end.  
31199 55 53 2d 41 53 43 49 49 20 63 61 73 65 20 66 6f  US-ASCII case fo
3119a 6c 64 69 6e 67 20 73 74 69 6c 6c 20 61 70 70 6c  lding still appl
3119b 69 65 73 2e 0a 2a 2a 20 0a 2a 2a 20 49 66 20 74  ies..** .** If t
3119c 68 65 20 69 6e 70 75 74 20 77 6f 72 64 20 63 6f  he input word co
3119d 6e 74 61 69 6e 73 20 6e 6f 74 20 64 69 67 69 74  ntains not digit
3119e 73 20 62 75 74 20 64 6f 65 73 20 63 68 61 72 61  s but does chara
3119f 63 74 65 72 73 20 6e 6f 74 20 0a 2a 2a 20 69 6e  cters not .** in
311a0 20 5b 61 2d 7a 41 2d 5a 5d 20 74 68 65 6e 20 6e   [a-zA-Z] then n
311a1 6f 20 73 74 65 6d 6d 69 6e 67 20 69 73 20 61 74  o stemming is at
311a2 74 65 6d 70 74 65 64 20 61 6e 64 20 74 68 69 73  tempted and this
311a3 20 72 6f 75 74 69 6e 65 20 6a 75 73 74 20 0a 2a   routine just .*
311a4 2a 20 63 6f 70 69 65 73 20 74 68 65 20 69 6e 70  * copies the inp
311a5 75 74 20 69 6e 74 6f 20 74 68 65 20 69 6e 70 75  ut into the inpu
311a6 74 20 69 6e 74 6f 20 74 68 65 20 6f 75 74 70 75  t into the outpu
311a7 74 20 77 69 74 68 20 55 53 2d 41 53 43 49 49 0a  t with US-ASCII.
311a8 2a 2a 20 63 61 73 65 20 66 6f 6c 64 69 6e 67 2e  ** case folding.
311a9 0a 2a 2a 0a 2a 2a 20 53 74 65 6d 6d 69 6e 67 20  .**.** Stemming 
311aa 6e 65 76 65 72 20 69 6e 63 72 65 61 73 65 73 20  never increases 
311ab 74 68 65 20 6c 65 6e 67 74 68 20 6f 66 20 74 68  the length of th
311ac 65 20 77 6f 72 64 2e 20 20 53 6f 20 74 68 65 72  e word.  So ther
311ad 65 20 69 73 0a 2a 2a 20 6e 6f 20 63 68 61 6e 63  e is.** no chanc
311ae 65 20 6f 66 20 6f 76 65 72 66 6c 6f 77 69 6e 67  e of overflowing
311af 20 74 68 65 20 7a 4f 75 74 20 62 75 66 66 65 72   the zOut buffer
311b0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
311b1 20 70 6f 72 74 65 72 5f 73 74 65 6d 6d 65 72 28   porter_stemmer(
311b2 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 2c  const char *zIn,
311b3 20 69 6e 74 20 6e 49 6e 2c 20 63 68 61 72 20 2a   int nIn, char *
311b4 7a 4f 75 74 2c 20 69 6e 74 20 2a 70 6e 4f 75 74  zOut, int *pnOut
311b5 29 7b 0a 20 20 69 6e 74 20 69 2c 20 6a 2c 20 63  ){.  int i, j, c
311b6 3b 0a 20 20 63 68 61 72 20 7a 52 65 76 65 72 73  ;.  char zRevers
311b7 65 5b 32 38 5d 3b 0a 20 20 63 68 61 72 20 2a 7a  e[28];.  char *z
311b8 2c 20 2a 7a 32 3b 0a 20 20 69 66 28 20 6e 49 6e  , *z2;.  if( nIn
311b9 3c 33 20 7c 7c 20 6e 49 6e 3e 3d 73 69 7a 65 6f  <3 || nIn>=sizeo
311ba 66 28 7a 52 65 76 65 72 73 65 29 2d 37 20 29 7b  f(zReverse)-7 ){
311bb 0a 20 20 20 20 2f 2a 20 54 68 65 20 77 6f 72 64  .    /* The word
311bc 20 69 73 20 74 6f 6f 20 62 69 67 20 6f 72 20 74   is too big or t
311bd 6f 6f 20 73 6d 61 6c 6c 20 66 6f 72 20 74 68 65  oo small for the
311be 20 70 6f 72 74 65 72 20 73 74 65 6d 6d 65 72 2e   porter stemmer.
311bf 0a 20 20 20 20 2a 2a 20 46 61 6c 6c 62 61 63 6b  .    ** Fallback
311c0 20 74 6f 20 74 68 65 20 63 6f 70 79 20 73 74 65   to the copy ste
311c1 6d 6d 65 72 20 2a 2f 0a 20 20 20 20 63 6f 70 79  mmer */.    copy
311c2 5f 73 74 65 6d 6d 65 72 28 7a 49 6e 2c 20 6e 49  _stemmer(zIn, nI
311c3 6e 2c 20 7a 4f 75 74 2c 20 70 6e 4f 75 74 29 3b  n, zOut, pnOut);
311c4 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
311c5 0a 20 20 66 6f 72 28 69 3d 30 2c 20 6a 3d 73 69  .  for(i=0, j=si
311c6 7a 65 6f 66 28 7a 52 65 76 65 72 73 65 29 2d 36  zeof(zReverse)-6
311c7 3b 20 69 3c 6e 49 6e 3b 20 69 2b 2b 2c 20 6a 2d  ; i<nIn; i++, j-
311c8 2d 29 7b 0a 20 20 20 20 63 20 3d 20 7a 49 6e 5b  -){.    c = zIn[
311c9 69 5d 3b 0a 20 20 20 20 69 66 28 20 63 3e 3d 27  i];.    if( c>='
311ca 41 27 20 26 26 20 63 3c 3d 27 5a 27 20 29 7b 0a  A' && c<='Z' ){.
311cb 20 20 20 20 20 20 7a 52 65 76 65 72 73 65 5b 6a        zReverse[j
311cc 5d 20 3d 20 63 20 2b 20 27 61 27 20 2d 20 27 41  ] = c + 'a' - 'A
311cd 27 3b 0a 20 20 20 20 7d 65 6c 73 65 20 69 66 28  ';.    }else if(
311ce 20 63 3e 3d 27 61 27 20 26 26 20 63 3c 3d 27 7a   c>='a' && c<='z
311cf 27 20 29 7b 0a 20 20 20 20 20 20 7a 52 65 76 65  ' ){.      zReve
311d0 72 73 65 5b 6a 5d 20 3d 20 63 3b 0a 20 20 20 20  rse[j] = c;.    
311d1 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 2f 2a 20  }else{.      /* 
311d2 54 68 65 20 75 73 65 20 6f 66 20 61 20 63 68 61  The use of a cha
311d3 72 61 63 74 65 72 20 6e 6f 74 20 69 6e 20 5b 61  racter not in [a
311d4 2d 7a 41 2d 5a 5d 20 6d 65 61 6e 73 20 74 68 61  -zA-Z] means tha
311d5 74 20 77 65 20 66 61 6c 6c 62 61 63 6b 0a 20 20  t we fallback.  
311d6 20 20 20 20 2a 2a 20 74 6f 20 74 68 65 20 63 6f      ** to the co
311d7 70 79 20 73 74 65 6d 6d 65 72 20 2a 2f 0a 20 20  py stemmer */.  
311d8 20 20 20 20 63 6f 70 79 5f 73 74 65 6d 6d 65 72      copy_stemmer
311d9 28 7a 49 6e 2c 20 6e 49 6e 2c 20 7a 4f 75 74 2c  (zIn, nIn, zOut,
311da 20 70 6e 4f 75 74 29 3b 0a 20 20 20 20 20 20 72   pnOut);.      r
311db 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d  eturn;.    }.  }
311dc 0a 20 20 6d 65 6d 73 65 74 28 26 7a 52 65 76 65  .  memset(&zReve
311dd 72 73 65 5b 73 69 7a 65 6f 66 28 7a 52 65 76 65  rse[sizeof(zReve
311de 72 73 65 29 2d 35 5d 2c 20 30 2c 20 35 29 3b 0a  rse)-5], 0, 5);.
311df 20 20 7a 20 3d 20 26 7a 52 65 76 65 72 73 65 5b    z = &zReverse[
311e0 6a 2b 31 5d 3b 0a 0a 0a 20 20 2f 2a 20 53 74 65  j+1];...  /* Ste
311e1 70 20 31 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b  p 1a */.  if( z[
311e2 30 5d 3d 3d 27 73 27 20 29 7b 0a 20 20 20 20 69  0]=='s' ){.    i
311e3 66 28 0a 20 20 20 20 20 21 73 74 65 6d 28 26 7a  f(.     !stem(&z
311e4 2c 20 22 73 65 73 73 22 2c 20 22 73 73 22 2c 20  , "sess", "ss", 
311e5 30 29 20 26 26 0a 20 20 20 20 20 21 73 74 65 6d  0) &&.     !stem
311e6 28 26 7a 2c 20 22 73 65 69 22 2c 20 22 69 22 2c  (&z, "sei", "i",
311e7 20 30 29 20 20 26 26 0a 20 20 20 20 20 21 73 74   0)  &&.     !st
311e8 65 6d 28 26 7a 2c 20 22 73 73 22 2c 20 22 73 73  em(&z, "ss", "ss
311e9 22 2c 20 30 29 0a 20 20 20 20 29 7b 0a 20 20 20  ", 0).    ){.   
311ea 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20     z++;.    }.  
311eb 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31 62 20  }..  /* Step 1b 
311ec 2a 2f 20 20 0a 20 20 7a 32 20 3d 20 7a 3b 0a 20  */  .  z2 = z;. 
311ed 20 69 66 28 20 73 74 65 6d 28 26 7a 2c 20 22 64   if( stem(&z, "d
311ee 65 65 22 2c 20 22 65 65 22 2c 20 6d 5f 67 74 5f  ee", "ee", m_gt_
311ef 30 29 20 29 7b 0a 20 20 20 20 2f 2a 20 44 6f 20  0) ){.    /* Do 
311f0 6e 6f 74 68 69 6e 67 2e 20 20 54 68 65 20 77 6f  nothing.  The wo
311f1 72 6b 20 77 61 73 20 61 6c 6c 20 69 6e 20 74 68  rk was all in th
311f2 65 20 74 65 73 74 20 2a 2f 0a 20 20 7d 65 6c 73  e test */.  }els
311f3 65 20 69 66 28 20 0a 20 20 20 20 20 28 73 74 65  e if( .     (ste
311f4 6d 28 26 7a 2c 20 22 67 6e 69 22 2c 20 22 22 2c  m(&z, "gni", "",
311f5 20 68 61 73 56 6f 77 65 6c 29 20 7c 7c 20 73 74   hasVowel) || st
311f6 65 6d 28 26 7a 2c 20 22 64 65 22 2c 20 22 22 2c  em(&z, "de", "",
311f7 20 68 61 73 56 6f 77 65 6c 29 29 0a 20 20 20 20   hasVowel)).    
311f8 20 20 26 26 20 7a 21 3d 7a 32 0a 20 20 29 7b 0a    && z!=z2.  ){.
311f9 20 20 20 20 20 69 66 28 20 73 74 65 6d 28 26 7a       if( stem(&z
311fa 2c 20 22 74 61 22 2c 20 22 61 74 65 22 2c 20 30  , "ta", "ate", 0
311fb 29 20 7c 7c 0a 20 20 20 20 20 20 20 20 20 73 74  ) ||.         st
311fc 65 6d 28 26 7a 2c 20 22 6c 62 22 2c 20 22 62 6c  em(&z, "lb", "bl
311fd 65 22 2c 20 30 29 20 7c 7c 0a 20 20 20 20 20 20  e", 0) ||.      
311fe 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 7a 69 22     stem(&z, "zi"
311ff 2c 20 22 69 7a 65 22 2c 20 30 29 20 29 7b 0a 20  , "ize", 0) ){. 
31200 20 20 20 20 20 20 2f 2a 20 44 6f 20 6e 6f 74 68        /* Do noth
31201 69 6e 67 2e 20 20 54 68 65 20 77 6f 72 6b 20 77  ing.  The work w
31202 61 73 20 61 6c 6c 20 69 6e 20 74 68 65 20 74 65  as all in the te
31203 73 74 20 2a 2f 0a 20 20 20 20 20 7d 65 6c 73 65  st */.     }else
31204 20 69 66 28 20 64 6f 75 62 6c 65 43 6f 6e 73 6f   if( doubleConso
31205 6e 61 6e 74 28 7a 29 20 26 26 20 28 2a 7a 21 3d  nant(z) && (*z!=
31206 27 6c 27 20 26 26 20 2a 7a 21 3d 27 73 27 20 26  'l' && *z!='s' &
31207 26 20 2a 7a 21 3d 27 7a 27 29 20 29 7b 0a 20 20  & *z!='z') ){.  
31208 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20 20 7d       z++;.     }
31209 65 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f 31 28  else if( m_eq_1(
3120a 7a 29 20 26 26 20 73 74 61 72 5f 6f 68 28 7a 29  z) && star_oh(z)
3120b 20 29 7b 0a 20 20 20 20 20 20 20 2a 28 2d 2d 7a   ){.       *(--z
3120c 29 20 3d 20 27 65 27 3b 0a 20 20 20 20 20 7d 0a  ) = 'e';.     }.
3120d 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70 20 31    }..  /* Step 1
3120e 63 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30 5d 3d  c */.  if( z[0]=
3120f 3d 27 79 27 20 26 26 20 68 61 73 56 6f 77 65 6c  ='y' && hasVowel
31210 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 7a 5b 30  (z+1) ){.    z[0
31211 5d 20 3d 20 27 69 27 3b 0a 20 20 7d 0a 0a 20 20  ] = 'i';.  }..  
31212 2f 2a 20 53 74 65 70 20 32 20 2a 2f 0a 20 20 73  /* Step 2 */.  s
31213 77 69 74 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20  witch( z[1] ){. 
31214 20 20 63 61 73 65 20 27 61 27 3a 0a 20 20 20 20    case 'a':.    
31215 20 73 74 65 6d 28 26 7a 2c 20 22 6c 61 6e 6f 69   stem(&z, "lanoi
31216 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67 74  ta", "ate", m_gt
31217 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
31218 28 26 7a 2c 20 22 6c 61 6e 6f 69 74 22 2c 20 22  (&z, "lanoit", "
31219 74 69 6f 6e 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a  tion", m_gt_0);.
3121a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
3121b 61 73 65 20 27 63 27 3a 0a 20 20 20 20 20 73 74  ase 'c':.     st
3121c 65 6d 28 26 7a 2c 20 22 69 63 6e 65 22 2c 20 22  em(&z, "icne", "
3121d 65 6e 63 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  ence", m_gt_0) |
3121e 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
3121f 22 69 63 6e 61 22 2c 20 22 61 6e 63 65 22 2c 20  "icna", "ance", 
31220 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72  m_gt_0);.     br
31221 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 65 27  eak;.   case 'e'
31222 3a 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  :.     stem(&z, 
31223 22 72 65 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d  "rezi", "ize", m
31224 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
31225 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 67 27 3a  ak;.   case 'g':
31226 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
31227 69 67 6f 6c 22 2c 20 22 6c 6f 67 22 2c 20 6d 5f  igol", "log", m_
31228 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_0);.     brea
31229 6b 3b 0a 20 20 20 63 61 73 65 20 27 6c 27 3a 0a  k;.   case 'l':.
3122a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
3122b 6c 62 22 2c 20 22 62 6c 65 22 2c 20 6d 5f 67 74  lb", "ble", m_gt
3122c 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
3122d 28 26 7a 2c 20 22 69 6c 6c 61 22 2c 20 22 61 6c  (&z, "illa", "al
3122e 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20  ", m_gt_0) ||.  
3122f 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 74     stem(&z, "ilt
31230 6e 65 22 2c 20 22 65 6e 74 22 2c 20 6d 5f 67 74  ne", "ent", m_gt
31231 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
31232 28 26 7a 2c 20 22 69 6c 65 22 2c 20 22 65 22 2c  (&z, "ile", "e",
31233 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
31234 20 73 74 65 6d 28 26 7a 2c 20 22 69 6c 73 75 6f   stem(&z, "ilsuo
31235 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67 74 5f 30  ", "ous", m_gt_0
31236 29 3b 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20  );.     break;. 
31237 20 20 63 61 73 65 20 27 6f 27 3a 0a 20 20 20 20    case 'o':.    
31238 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69 74 61   stem(&z, "noita
31239 7a 69 22 2c 20 22 69 7a 65 22 2c 20 6d 5f 67 74  zi", "ize", m_gt
3123a 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
3123b 28 26 7a 2c 20 22 6e 6f 69 74 61 22 2c 20 22 61  (&z, "noita", "a
3123c 74 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  te", m_gt_0) ||.
3123d 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 72       stem(&z, "r
3123e 6f 74 61 22 2c 20 22 61 74 65 22 2c 20 6d 5f 67  ota", "ate", m_g
3123f 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
31240 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a 20  ;.   case 's':. 
31241 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6d 73      stem(&z, "ms
31242 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  ila", "al", m_gt
31243 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
31244 28 26 7a 2c 20 22 73 73 65 6e 65 76 69 22 2c 20  (&z, "ssenevi", 
31245 22 69 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c  "ive", m_gt_0) |
31246 7c 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20  |.     stem(&z, 
31247 22 73 73 65 6e 6c 75 66 22 2c 20 22 66 75 6c 22  "ssenluf", "ful"
31248 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20  , m_gt_0) ||.   
31249 20 20 73 74 65 6d 28 26 7a 2c 20 22 73 73 65 6e    stem(&z, "ssen
3124a 73 75 6f 22 2c 20 22 6f 75 73 22 2c 20 6d 5f 67  suo", "ous", m_g
3124b 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65 61 6b  t_0);.     break
3124c 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a 0a 20  ;.   case 't':. 
3124d 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69 74      stem(&z, "it
3124e 69 6c 61 22 2c 20 22 61 6c 22 2c 20 6d 5f 67 74  ila", "al", m_gt
3124f 5f 30 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  _0) ||.     stem
31250 28 26 7a 2c 20 22 69 74 69 76 69 22 2c 20 22 69  (&z, "itivi", "i
31251 76 65 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a  ve", m_gt_0) ||.
31252 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 69       stem(&z, "i
31253 74 69 6c 69 62 22 2c 20 22 62 6c 65 22 2c 20 6d  tilib", "ble", m
31254 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62 72 65  _gt_0);.     bre
31255 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74  ak;.  }..  /* St
31256 65 70 20 33 20 2a 2f 0a 20 20 73 77 69 74 63 68  ep 3 */.  switch
31257 28 20 7a 5b 30 5d 20 29 7b 0a 20 20 20 63 61 73  ( z[0] ){.   cas
31258 65 20 27 65 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 'e':.     stem
31259 28 26 7a 2c 20 22 65 74 61 63 69 22 2c 20 22 69  (&z, "etaci", "i
3125a 63 22 2c 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20  c", m_gt_0) ||. 
3125b 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 65 76      stem(&z, "ev
3125c 69 74 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 30  ita", "", m_gt_0
3125d 29 20 20 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d  )   ||.     stem
3125e 28 26 7a 2c 20 22 65 7a 69 6c 61 22 2c 20 22 61  (&z, "ezila", "a
3125f 6c 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20  l", m_gt_0);.   
31260 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65    break;.   case
31261 20 27 69 27 3a 0a 20 20 20 20 20 73 74 65 6d 28   'i':.     stem(
31262 26 7a 2c 20 22 69 74 69 63 69 22 2c 20 22 69 63  &z, "itici", "ic
31263 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20  ", m_gt_0);.    
31264 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20   break;.   case 
31265 27 6c 27 3a 0a 20 20 20 20 20 73 74 65 6d 28 26  'l':.     stem(&
31266 7a 2c 20 22 6c 61 63 69 22 2c 20 22 69 63 22 2c  z, "laci", "ic",
31267 20 6d 5f 67 74 5f 30 29 20 7c 7c 0a 20 20 20 20   m_gt_0) ||.    
31268 20 73 74 65 6d 28 26 7a 2c 20 22 6c 75 66 22 2c   stem(&z, "luf",
31269 20 22 22 2c 20 6d 5f 67 74 5f 30 29 3b 0a 20 20   "", m_gt_0);.  
3126a 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63 61 73     break;.   cas
3126b 65 20 27 73 27 3a 0a 20 20 20 20 20 73 74 65 6d  e 's':.     stem
3126c 28 26 7a 2c 20 22 73 73 65 6e 22 2c 20 22 22 2c  (&z, "ssen", "",
3126d 20 6d 5f 67 74 5f 30 29 3b 0a 20 20 20 20 20 62   m_gt_0);.     b
3126e 72 65 61 6b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20  reak;.  }..  /* 
3126f 53 74 65 70 20 34 20 2a 2f 0a 20 20 73 77 69 74  Step 4 */.  swit
31270 63 68 28 20 7a 5b 31 5d 20 29 7b 0a 20 20 20 63  ch( z[1] ){.   c
31271 61 73 65 20 27 61 27 3a 0a 20 20 20 20 20 69 66  ase 'a':.     if
31272 28 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20 6d  ( z[0]=='l' && m
31273 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b 0a 20 20  _gt_1(z+2) ){.  
31274 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20       z += 2;.   
31275 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a    }.     break;.
31276 20 20 20 63 61 73 65 20 27 63 27 3a 0a 20 20 20     case 'c':.   
31277 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27 20    if( z[0]=='e' 
31278 26 26 20 7a 5b 32 5d 3d 3d 27 6e 27 20 26 26 20  && z[2]=='n' && 
31279 28 7a 5b 33 5d 3d 3d 27 61 27 20 7c 7c 20 7a 5b  (z[3]=='a' || z[
3127a 33 5d 3d 3d 27 65 27 29 20 20 26 26 20 6d 5f 67  3]=='e')  && m_g
3127b 74 5f 31 28 7a 2b 34 29 20 20 29 7b 0a 20 20 20  t_1(z+4)  ){.   
3127c 20 20 20 20 7a 20 2b 3d 20 34 3b 0a 20 20 20 20      z += 4;.    
3127d 20 7d 0a 20 20 20 20 20 62 72 65 61 6b 3b 0a 20   }.     break;. 
3127e 20 20 63 61 73 65 20 27 65 27 3a 0a 20 20 20 20    case 'e':.    
3127f 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 72 27 20 26   if( z[0]=='r' &
31280 26 20 6d 5f 67 74 5f 31 28 7a 2b 32 29 20 29 7b  & m_gt_1(z+2) ){
31281 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 32 3b 0a  .       z += 2;.
31282 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
31283 6b 3b 0a 20 20 20 63 61 73 65 20 27 69 27 3a 0a  k;.   case 'i':.
31284 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
31285 63 27 20 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 32  c' && m_gt_1(z+2
31286 29 20 29 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d  ) ){.       z +=
31287 20 32 3b 0a 20 20 20 20 20 7d 0a 20 20 20 20 20   2;.     }.     
31288 62 72 65 61 6b 3b 0a 20 20 20 63 61 73 65 20 27  break;.   case '
31289 6c 27 3a 0a 20 20 20 20 20 69 66 28 20 7a 5b 30  l':.     if( z[0
3128a 5d 3d 3d 27 65 27 20 26 26 20 7a 5b 32 5d 3d 3d  ]=='e' && z[2]==
3128b 27 62 27 20 26 26 20 28 7a 5b 33 5d 3d 3d 27 61  'b' && (z[3]=='a
3128c 27 20 7c 7c 20 7a 5b 33 5d 3d 3d 27 69 27 29 20  ' || z[3]=='i') 
3128d 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 34 29 20 29  && m_gt_1(z+4) )
3128e 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 34 3b  {.       z += 4;
3128f 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
31290 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 6e 27 3a  ak;.   case 'n':
31291 0a 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d  .     if( z[0]==
31292 27 74 27 20 29 7b 0a 20 20 20 20 20 20 20 69 66  't' ){.       if
31293 28 20 7a 5b 32 5d 3d 3d 27 61 27 20 29 7b 0a 20  ( z[2]=='a' ){. 
31294 20 20 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74          if( m_gt
31295 5f 31 28 7a 2b 33 29 20 29 7b 0a 20 20 20 20 20  _1(z+3) ){.     
31296 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20 20        z += 3;.  
31297 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
31298 7d 65 6c 73 65 20 69 66 28 20 7a 5b 32 5d 3d 3d  }else if( z[2]==
31299 27 65 27 20 29 7b 0a 20 20 20 20 20 20 20 20 20  'e' ){.         
3129a 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65 6d 65 22  stem(&z, "tneme"
3129b 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 20 7c 7c  , "", m_gt_1) ||
3129c 0a 20 20 20 20 20 20 20 20 20 73 74 65 6d 28 26  .         stem(&
3129d 7a 2c 20 22 74 6e 65 6d 22 2c 20 22 22 2c 20 6d  z, "tnem", "", m
3129e 5f 67 74 5f 31 29 20 7c 7c 0a 20 20 20 20 20 20  _gt_1) ||.      
3129f 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 74 6e 65     stem(&z, "tne
312a0 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
312a1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 7d 0a         }.     }.
312a2 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 63       break;.   c
312a3 61 73 65 20 27 6f 27 3a 0a 20 20 20 20 20 69 66  ase 'o':.     if
312a4 28 20 7a 5b 30 5d 3d 3d 27 75 27 20 29 7b 0a 20  ( z[0]=='u' ){. 
312a5 20 20 20 20 20 20 69 66 28 20 6d 5f 67 74 5f 31        if( m_gt_1
312a6 28 7a 2b 32 29 20 29 7b 0a 20 20 20 20 20 20 20  (z+2) ){.       
312a7 20 20 7a 20 2b 3d 20 32 3b 0a 20 20 20 20 20 20    z += 2;.      
312a8 20 7d 0a 20 20 20 20 20 7d 65 6c 73 65 20 69 66   }.     }else if
312a9 28 20 7a 5b 33 5d 3d 3d 27 73 27 20 7c 7c 20 7a  ( z[3]=='s' || z
312aa 5b 33 5d 3d 3d 27 74 27 20 29 7b 0a 20 20 20 20  [3]=='t' ){.    
312ab 20 20 20 73 74 65 6d 28 26 7a 2c 20 22 6e 6f 69     stem(&z, "noi
312ac 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31 29 3b 0a  ", "", m_gt_1);.
312ad 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61       }.     brea
312ae 6b 3b 0a 20 20 20 63 61 73 65 20 27 73 27 3a 0a  k;.   case 's':.
312af 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
312b0 6d 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20  m' && z[2]=='i' 
312b1 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29  && m_gt_1(z+3) )
312b2 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b  {.       z += 3;
312b3 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
312b4 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 74 27 3a  ak;.   case 't':
312b5 0a 20 20 20 20 20 73 74 65 6d 28 26 7a 2c 20 22  .     stem(&z, "
312b6 65 74 61 22 2c 20 22 22 2c 20 6d 5f 67 74 5f 31  eta", "", m_gt_1
312b7 29 20 7c 7c 0a 20 20 20 20 20 73 74 65 6d 28 26  ) ||.     stem(&
312b8 7a 2c 20 22 69 74 69 22 2c 20 22 22 2c 20 6d 5f  z, "iti", "", m_
312b9 67 74 5f 31 29 3b 0a 20 20 20 20 20 62 72 65 61  gt_1);.     brea
312ba 6b 3b 0a 20 20 20 63 61 73 65 20 27 75 27 3a 0a  k;.   case 'u':.
312bb 20 20 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27       if( z[0]=='
312bc 73 27 20 26 26 20 7a 5b 32 5d 3d 3d 27 6f 27 20  s' && z[2]=='o' 
312bd 26 26 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29  && m_gt_1(z+3) )
312be 7b 0a 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b  {.       z += 3;
312bf 0a 20 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65  .     }.     bre
312c0 61 6b 3b 0a 20 20 20 63 61 73 65 20 27 76 27 3a  ak;.   case 'v':
312c1 0a 20 20 20 63 61 73 65 20 27 7a 27 3a 0a 20 20  .   case 'z':.  
312c2 20 20 20 69 66 28 20 7a 5b 30 5d 3d 3d 27 65 27     if( z[0]=='e'
312c3 20 26 26 20 7a 5b 32 5d 3d 3d 27 69 27 20 26 26   && z[2]=='i' &&
312c4 20 6d 5f 67 74 5f 31 28 7a 2b 33 29 20 29 7b 0a   m_gt_1(z+3) ){.
312c5 20 20 20 20 20 20 20 7a 20 2b 3d 20 33 3b 0a 20         z += 3;. 
312c6 20 20 20 20 7d 0a 20 20 20 20 20 62 72 65 61 6b      }.     break
312c7 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 74 65 70  ;.  }..  /* Step
312c8 20 35 61 20 2a 2f 0a 20 20 69 66 28 20 7a 5b 30   5a */.  if( z[0
312c9 5d 3d 3d 27 65 27 20 29 7b 0a 20 20 20 20 69 66  ]=='e' ){.    if
312ca 28 20 6d 5f 67 74 5f 31 28 7a 2b 31 29 20 29 7b  ( m_gt_1(z+1) ){
312cb 0a 20 20 20 20 20 20 7a 2b 2b 3b 0a 20 20 20 20  .      z++;.    
312cc 7d 65 6c 73 65 20 69 66 28 20 6d 5f 65 71 5f 31  }else if( m_eq_1
312cd 28 7a 2b 31 29 20 26 26 20 21 73 74 61 72 5f 6f  (z+1) && !star_o
312ce 68 28 7a 2b 31 29 20 29 7b 0a 20 20 20 20 20 20  h(z+1) ){.      
312cf 7a 2b 2b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a  z++;.    }.  }..
312d0 20 20 2f 2a 20 53 74 65 70 20 35 62 20 2a 2f 0a    /* Step 5b */.
312d1 20 20 69 66 28 20 6d 5f 67 74 5f 31 28 7a 29 20    if( m_gt_1(z) 
312d2 26 26 20 7a 5b 30 5d 3d 3d 27 6c 27 20 26 26 20  && z[0]=='l' && 
312d3 7a 5b 31 5d 3d 3d 27 6c 27 20 29 7b 0a 20 20 20  z[1]=='l' ){.   
312d4 20 7a 2b 2b 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20   z++;.  }..  /* 
312d5 7a 5b 5d 20 69 73 20 6e 6f 77 20 74 68 65 20 73  z[] is now the s
312d6 74 65 6d 6d 65 64 20 77 6f 72 64 20 69 6e 20 72  temmed word in r
312d7 65 76 65 72 73 65 20 6f 72 64 65 72 2e 20 20 46  everse order.  F
312d8 6c 69 70 20 69 74 20 62 61 63 6b 0a 20 20 2a 2a  lip it back.  **
312d9 20 61 72 6f 75 6e 64 20 69 6e 74 6f 20 66 6f 72   around into for
312da 77 61 72 64 20 6f 72 64 65 72 20 61 6e 64 20 72  ward order and r
312db 65 74 75 72 6e 2e 0a 20 20 2a 2f 0a 20 20 2a 70  eturn..  */.  *p
312dc 6e 4f 75 74 20 3d 20 69 20 3d 20 73 74 72 6c 65  nOut = i = strle
312dd 6e 28 7a 29 3b 0a 20 20 7a 4f 75 74 5b 69 5d 20  n(z);.  zOut[i] 
312de 3d 20 30 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a  = 0;.  while( *z
312df 20 29 7b 0a 20 20 20 20 7a 4f 75 74 5b 2d 2d 69   ){.    zOut[--i
312e0 5d 20 3d 20 2a 28 7a 2b 2b 29 3b 0a 20 20 7d 0a  ] = *(z++);.  }.
312e1 7d 0a 0a 2f 2a 0a 2a 2a 20 43 68 61 72 61 63 74  }../*.** Charact
312e2 65 72 73 20 74 68 61 74 20 63 61 6e 20 62 65 20  ers that can be 
312e3 70 61 72 74 20 6f 66 20 61 20 74 6f 6b 65 6e 2e  part of a token.
312e4 20 20 57 65 20 61 73 73 75 6d 65 20 61 6e 79 20    We assume any 
312e5 63 68 61 72 61 63 74 65 72 0a 2a 2a 20 77 68 6f  character.** who
312e6 73 65 20 76 61 6c 75 65 20 69 73 20 67 72 65 61  se value is grea
312e7 74 65 72 20 74 68 61 6e 20 30 78 38 30 20 28 61  ter than 0x80 (a
312e8 6e 79 20 55 54 46 20 63 68 61 72 61 63 74 65 72  ny UTF character
312e9 29 20 63 61 6e 20 62 65 0a 2a 2a 20 70 61 72 74  ) can be.** part
312ea 20 6f 66 20 61 20 74 6f 6b 65 6e 2e 20 20 49 6e   of a token.  In
312eb 20 6f 74 68 65 72 20 77 6f 72 64 73 2c 20 64 65   other words, de
312ec 6c 69 6d 69 74 65 72 73 20 61 6c 6c 20 6d 75 73  limiters all mus
312ed 74 20 68 61 76 65 0a 2a 2a 20 76 61 6c 75 65 73  t have.** values
312ee 20 6f 66 20 30 78 37 66 20 6f 72 20 6c 6f 77 65   of 0x7f or lowe
312ef 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e  r..*/.static con
312f0 73 74 20 63 68 61 72 20 70 6f 72 74 65 72 49 64  st char porterId
312f1 43 68 61 72 5b 5d 20 3d 20 7b 0a 2f 2a 20 78 30  Char[] = {./* x0
312f2 20 78 31 20 78 32 20 78 33 20 78 34 20 78 35 20   x1 x2 x3 x4 x5 
312f3 78 36 20 78 37 20 78 38 20 78 39 20 78 41 20 78  x6 x7 x8 x9 xA x
312f4 42 20 78 43 20 78 44 20 78 45 20 78 46 20 2a 2f  B xC xD xE xF */
312f5 0a 20 20 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c  .    1, 1, 1, 1,
312f6 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
312f7 31 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  1, 0, 0, 0, 0, 0
312f8 2c 20 30 2c 20 20 2f 2a 20 33 78 20 2a 2f 0a 20  , 0,  /* 3x */. 
312f9 20 20 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31     0, 1, 1, 1, 1
312fa 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
312fb 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
312fc 31 2c 20 20 2f 2a 20 34 78 20 2a 2f 0a 20 20 20  1,  /* 4x */.   
312fd 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
312fe 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
312ff 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 31 2c  , 0, 0, 0, 0, 1,
31300 20 20 2f 2a 20 35 78 20 2a 2f 0a 20 20 20 20 30    /* 5x */.    0
31301 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
31302 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
31303 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20  1, 1, 1, 1, 1,  
31304 2f 2a 20 36 78 20 2a 2f 0a 20 20 20 20 31 2c 20  /* 6x */.    1, 
31305 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31  1, 1, 1, 1, 1, 1
31306 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 30 2c  , 1, 1, 1, 1, 0,
31307 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 2f 2a   0, 0, 0, 0,  /*
31308 20 37 78 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e   7x */.};.#defin
31309 65 20 69 73 44 65 6c 69 6d 28 43 29 20 28 28 28  e isDelim(C) (((
3130a 63 68 3d 43 29 26 30 78 38 30 29 3d 3d 30 20 26  ch=C)&0x80)==0 &
3130b 26 20 28 63 68 3c 30 78 33 30 20 7c 7c 20 21 70  & (ch<0x30 || !p
3130c 6f 72 74 65 72 49 64 43 68 61 72 5b 63 68 2d 30  orterIdChar[ch-0
3130d 78 33 30 5d 29 29 0a 0a 2f 2a 0a 2a 2a 20 45 78  x30]))../*.** Ex
3130e 74 72 61 63 74 20 74 68 65 20 6e 65 78 74 20 74  tract the next t
3130f 6f 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65  oken from a toke
31310 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e  nization cursor.
31311 20 20 54 68 65 20 63 75 72 73 6f 72 20 6d 75 73    The cursor mus
31312 74 0a 2a 2a 20 68 61 76 65 20 62 65 65 6e 20 6f  t.** have been o
31313 70 65 6e 65 64 20 62 79 20 61 20 70 72 69 6f 72  pened by a prior
31314 20 63 61 6c 6c 20 74 6f 20 70 6f 72 74 65 72 4f   call to porterO
31315 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74 61 74 69 63  pen()..*/.static
31316 20 69 6e 74 20 70 6f 72 74 65 72 4e 65 78 74 28   int porterNext(
31317 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
31318 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
31319 72 73 6f 72 2c 20 20 2f 2a 20 43 75 72 73 6f 72  rsor,  /* Cursor
3131a 20 72 65 74 75 72 6e 65 64 20 62 79 20 70 6f 72   returned by por
3131b 74 65 72 4f 70 65 6e 20 2a 2f 0a 20 20 63 6f 6e  terOpen */.  con
3131c 73 74 20 63 68 61 72 20 2a 2a 70 7a 54 6f 6b 65  st char **pzToke
3131d 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  n,              
3131e 20 2f 2a 20 4f 55 54 3a 20 2a 70 7a 54 6f 6b 65   /* OUT: *pzToke
3131f 6e 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 74  n is the token t
31320 65 78 74 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 6e  ext */.  int *pn
31321 42 79 74 65 73 2c 20 20 20 20 20 20 20 20 20 20  Bytes,          
31322 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31323 4f 55 54 3a 20 4e 75 6d 62 65 72 20 6f 66 20 62  OUT: Number of b
31324 79 74 65 73 20 69 6e 20 74 6f 6b 65 6e 20 2a 2f  ytes in token */
31325 0a 20 20 69 6e 74 20 2a 70 69 53 74 61 72 74 4f  .  int *piStartO
31326 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
31327 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 53         /* OUT: S
31328 74 61 72 74 69 6e 67 20 6f 66 66 73 65 74 20 6f  tarting offset o
31329 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
3132a 20 2a 70 69 45 6e 64 4f 66 66 73 65 74 2c 20 20   *piEndOffset,  
3132b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3132c 20 2f 2a 20 4f 55 54 3a 20 45 6e 64 69 6e 67 20   /* OUT: Ending 
3132d 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
3132e 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 50 6f 73 69  */.  int *piPosi
3132f 74 69 6f 6e 20 20 20 20 20 20 20 20 20 20 20 20  tion            
31330 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
31331 20 50 6f 73 69 74 69 6f 6e 20 69 6e 74 65 67 65   Position intege
31332 72 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f 0a 29 7b  r of token */.){
31333 0a 20 20 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69  .  porter_tokeni
31334 7a 65 72 5f 63 75 72 73 6f 72 20 2a 63 20 3d 20  zer_cursor *c = 
31335 28 70 6f 72 74 65 72 5f 74 6f 6b 65 6e 69 7a 65  (porter_tokenize
31336 72 5f 63 75 72 73 6f 72 20 2a 29 20 70 43 75 72  r_cursor *) pCur
31337 73 6f 72 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61  sor;.  const cha
31338 72 20 2a 7a 20 3d 20 63 2d 3e 7a 49 6e 70 75 74  r *z = c->zInput
31339 3b 0a 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69  ;..  while( c->i
3133a 4f 66 66 73 65 74 3c 63 2d 3e 6e 49 6e 70 75 74  Offset<c->nInput
3133b 20 29 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61   ){.    int iSta
3133c 72 74 4f 66 66 73 65 74 2c 20 63 68 3b 0a 0a 20  rtOffset, ch;.. 
3133d 20 20 20 2f 2a 20 53 63 61 6e 20 70 61 73 74 20     /* Scan past 
3133e 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61 63  delimiter charac
3133f 74 65 72 73 20 2a 2f 0a 20 20 20 20 77 68 69 6c  ters */.    whil
31340 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63 2d  e( c->iOffset<c-
31341 3e 6e 49 6e 70 75 74 20 26 26 20 69 73 44 65 6c  >nInput && isDel
31342 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65 74 5d  im(z[c->iOffset]
31343 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69 4f  ) ){.      c->iO
31344 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a 0a  ffset++;.    }..
31345 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f 6e      /* Count non
31346 2d 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72 61  -delimiter chara
31347 63 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69 53  cters. */.    iS
31348 74 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d 3e  tartOffset = c->
31349 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68 69  iOffset;.    whi
3134a 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c 63  le( c->iOffset<c
3134b 2d 3e 6e 49 6e 70 75 74 20 26 26 20 21 69 73 44  ->nInput && !isD
3134c 65 6c 69 6d 28 7a 5b 63 2d 3e 69 4f 66 66 73 65  elim(z[c->iOffse
3134d 74 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e  t]) ){.      c->
3134e 69 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d  iOffset++;.    }
3134f 0a 0a 20 20 20 20 69 66 28 20 63 2d 3e 69 4f 66  ..    if( c->iOf
31350 66 73 65 74 3e 69 53 74 61 72 74 4f 66 66 73 65  fset>iStartOffse
31351 74 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  t ){.      int n
31352 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74 2d 69 53   = c->iOffset-iS
31353 74 61 72 74 4f 66 66 73 65 74 3b 0a 20 20 20 20  tartOffset;.    
31354 20 20 69 66 28 20 6e 3e 63 2d 3e 6e 41 6c 6c 6f    if( n>c->nAllo
31355 63 61 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20  cated ){.       
31356 20 63 2d 3e 6e 41 6c 6c 6f 63 61 74 65 64 20 3d   c->nAllocated =
31357 20 6e 2b 32 30 3b 0a 20 20 20 20 20 20 20 20 63   n+20;.        c
31358 2d 3e 7a 54 6f 6b 65 6e 20 3d 20 73 71 6c 69 74  ->zToken = sqlit
31359 65 33 5f 72 65 61 6c 6c 6f 63 28 63 2d 3e 7a 54  e3_realloc(c->zT
3135a 6f 6b 65 6e 2c 20 63 2d 3e 6e 41 6c 6c 6f 63 61  oken, c->nAlloca
3135b 74 65 64 29 3b 0a 20 20 20 20 20 20 20 20 69 66  ted);.        if
3135c 28 20 63 2d 3e 7a 54 6f 6b 65 6e 3d 3d 4e 55 4c  ( c->zToken==NUL
3135d 4c 20 29 20 72 65 74 75 72 6e 20 53 51 4c 49 54  L ) return SQLIT
3135e 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 20 20 7d  E_NOMEM;.      }
3135f 0a 20 20 20 20 20 20 70 6f 72 74 65 72 5f 73 74  .      porter_st
31360 65 6d 6d 65 72 28 26 7a 5b 69 53 74 61 72 74 4f  emmer(&z[iStartO
31361 66 66 73 65 74 5d 2c 20 6e 2c 20 63 2d 3e 7a 54  ffset], n, c->zT
31362 6f 6b 65 6e 2c 20 70 6e 42 79 74 65 73 29 3b 0a  oken, pnBytes);.
31363 20 20 20 20 20 20 2a 70 7a 54 6f 6b 65 6e 20 3d        *pzToken =
31364 20 63 2d 3e 7a 54 6f 6b 65 6e 3b 0a 20 20 20 20   c->zToken;.    
31365 20 20 2a 70 69 53 74 61 72 74 4f 66 66 73 65 74    *piStartOffset
31366 20 3d 20 69 53 74 61 72 74 4f 66 66 73 65 74 3b   = iStartOffset;
31367 0a 20 20 20 20 20 20 2a 70 69 45 6e 64 4f 66 66  .      *piEndOff
31368 73 65 74 20 3d 20 63 2d 3e 69 4f 66 66 73 65 74  set = c->iOffset
31369 3b 0a 20 20 20 20 20 20 2a 70 69 50 6f 73 69 74  ;.      *piPosit
3136a 69 6f 6e 20 3d 20 63 2d 3e 69 54 6f 6b 65 6e 2b  ion = c->iToken+
3136b 2b 3b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 20  +;.      return 
3136c 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 20 20 7d  SQLITE_OK;.    }
3136d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 53 51  .  }.  return SQ
3136e 4c 49 54 45 5f 44 4f 4e 45 3b 0a 7d 0a 0a 2f 2a  LITE_DONE;.}../*
3136f 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f 66 20 72  .** The set of r
31370 6f 75 74 69 6e 65 73 20 74 68 61 74 20 69 6d 70  outines that imp
31371 6c 65 6d 65 6e 74 20 74 68 65 20 70 6f 72 74 65  lement the porte
31372 72 2d 73 74 65 6d 6d 65 72 20 74 6f 6b 65 6e 69  r-stemmer tokeni
31373 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63 6f  zer.*/.static co
31374 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
31375 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 70 6f 72  nizer_module por
31376 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  terTokenizerModu
31377 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 70 6f  le = {.  0,.  po
31378 72 74 65 72 43 72 65 61 74 65 2c 0a 20 20 70 6f  rterCreate,.  po
31379 72 74 65 72 44 65 73 74 72 6f 79 2c 0a 20 20 70  rterDestroy,.  p
3137a 6f 72 74 65 72 4f 70 65 6e 2c 0a 20 20 70 6f 72  orterOpen,.  por
3137b 74 65 72 43 6c 6f 73 65 2c 0a 20 20 70 6f 72 74  terClose,.  port
3137c 65 72 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a  erNext,.};../*.*
3137d 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65 77  * Allocate a new
3137e 20 70 6f 72 74 65 72 20 74 6f 6b 65 6e 69 7a 65   porter tokenize
3137f 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f 69  r.  Return a poi
31380 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77 0a  nter to the new.
31381 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e 20  ** tokenizer in 
31382 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51 4c  *ppModule.*/.SQL
31383 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69 64  ITE_PRIVATE void
31384 20 73 71 6c 69 74 65 33 46 74 73 33 50 6f 72 74   sqlite3Fts3Port
31385 65 72 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c  erTokenizerModul
31386 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b  e(.  sqlite3_tok
31387 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63 6f  enizer_module co
31388 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29 7b  nst**ppModule.){
31389 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20 26  .  *ppModule = &
3138a 70 6f 72 74 65 72 54 6f 6b 65 6e 69 7a 65 72 4d  porterTokenizerM
3138b 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66  odule;.}..#endif
3138c 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
3138d 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
3138e 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
3138f 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a  LE_FTS3) */../**
31390 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
31391 20 6f 66 20 66 74 73 33 5f 70 6f 72 74 65 72 2e   of fts3_porter.
31392 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  c **************
31393 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31394 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
31395 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
31396 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b  in file fts3_tok
31397 65 6e 69 7a 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a  enizer.c *******
31398 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31399 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
3139a 2a 2a 20 32 30 30 37 20 4a 75 6e 65 20 32 32 0a  ** 2007 June 22.
3139b 2a 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72  **.** The author
3139c 20 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72   disclaims copyr
3139d 69 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75  ight to this sou
3139e 72 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c  rce code.  In pl
3139f 61 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61  ace of.** a lega
313a0 6c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69  l notice, here i
313a1 73 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a  s a blessing:.**
313a2 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64  .**    May you d
313a3 6f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65  o good and not e
313a4 76 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  vil..**    May y
313a5 6f 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e  ou find forgiven
313a6 65 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66  ess for yourself
313a7 20 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68   and forgive oth
313a8 65 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79  ers..**    May y
313a9 6f 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c  ou share freely,
313aa 20 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f   never taking mo
313ab 72 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65  re than you give
313ac 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ..**.***********
313ad 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
313ae 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
313af 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
313b0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
313b1 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 69  ***.**.** This i
313b2 73 20 70 61 72 74 20 6f 66 20 61 6e 20 53 51 4c  s part of an SQL
313b3 69 74 65 20 6d 6f 64 75 6c 65 20 69 6d 70 6c 65  ite module imple
313b4 6d 65 6e 74 69 6e 67 20 66 75 6c 6c 2d 74 65 78  menting full-tex
313b5 74 20 73 65 61 72 63 68 2e 0a 2a 2a 20 54 68 69  t search..** Thi
313b6 73 20 70 61 72 74 69 63 75 6c 61 72 20 66 69 6c  s particular fil
313b7 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65  e implements the
313b8 20 67 65 6e 65 72 69 63 20 74 6f 6b 65 6e 69 7a   generic tokeniz
313b9 65 72 20 69 6e 74 65 72 66 61 63 65 2e 0a 2a 2f  er interface..*/
313ba 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20 63 6f 64 65  ../*.** The code
313bb 20 69 6e 20 74 68 69 73 20 66 69 6c 65 20 69 73   in this file is
313bc 20 6f 6e 6c 79 20 63 6f 6d 70 69 6c 65 64 20 69   only compiled i
313bd 66 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 2a 20 54  f:.**.**     * T
313be 68 65 20 46 54 53 33 20 6d 6f 64 75 6c 65 20 69  he FTS3 module i
313bf 73 20 62 65 69 6e 67 20 62 75 69 6c 74 20 61 73  s being built as
313c0 20 61 6e 20 65 78 74 65 6e 73 69 6f 6e 0a 2a 2a   an extension.**
313c1 20 20 20 20 20 20 20 28 69 6e 20 77 68 69 63 68         (in which
313c2 20 63 61 73 65 20 53 51 4c 49 54 45 5f 43 4f 52   case SQLITE_COR
313c3 45 20 69 73 20 6e 6f 74 20 64 65 66 69 6e 65 64  E is not defined
313c4 29 2c 20 6f 72 0a 2a 2a 0a 2a 2a 20 20 20 20 20  ), or.**.**     
313c5 2a 20 54 68 65 20 46 54 53 33 20 6d 6f 64 75 6c  * The FTS3 modul
313c6 65 20 69 73 20 62 65 69 6e 67 20 62 75 69 6c 74  e is being built
313c7 20 69 6e 74 6f 20 74 68 65 20 63 6f 72 65 20 6f   into the core o
313c8 66 0a 2a 2a 20 20 20 20 20 20 20 53 51 4c 69 74  f.**       SQLit
313c9 65 20 28 69 6e 20 77 68 69 63 68 20 63 61 73 65  e (in which case
313ca 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46   SQLITE_ENABLE_F
313cb 54 53 33 20 69 73 20 64 65 66 69 6e 65 64 29 2e  TS3 is defined).
313cc 0a 2a 2f 0a 23 69 66 20 21 64 65 66 69 6e 65 64  .*/.#if !defined
313cd 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
313ce 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
313cf 45 4e 41 42 4c 45 5f 46 54 53 33 29 0a 0a 23 69  ENABLE_FTS3)..#i
313d0 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52  fndef SQLITE_COR
313d1 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e  E.  SQLITE_EXTEN
313d2 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6e 64 69  SION_INIT1.#endi
313d3 66 0a 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d  f.../*.** Implem
313d4 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20  entation of the 
313d5 53 51 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74  SQL scalar funct
313d6 69 6f 6e 20 66 6f 72 20 61 63 63 65 73 73 69 6e  ion for accessin
313d7 67 20 74 68 65 20 75 6e 64 65 72 6c 79 69 6e 67  g the underlying
313d8 20 0a 2a 2a 20 68 61 73 68 20 74 61 62 6c 65 2e   .** hash table.
313d9 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 6d   This function m
313da 61 79 20 62 65 20 63 61 6c 6c 65 64 20 61 73 20  ay be called as 
313db 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20 20  follows:.**.**  
313dc 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
313dd 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
313de 65 3e 29 3b 0a 2a 2a 20 20 20 53 45 4c 45 43 54  e>);.**   SELECT
313df 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e   <function-name>
313e0 28 3c 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 70 6f  (<key-name>, <po
313e1 69 6e 74 65 72 3e 29 3b 0a 2a 2a 0a 2a 2a 20 77  inter>);.**.** w
313e2 68 65 72 65 20 3c 66 75 6e 63 74 69 6f 6e 2d 6e  here <function-n
313e3 61 6d 65 3e 20 69 73 20 74 68 65 20 6e 61 6d 65  ame> is the name
313e4 20 70 61 73 73 65 64 20 61 73 20 74 68 65 20 73   passed as the s
313e5 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74 0a 2a  econd argument.*
313e6 2a 20 74 6f 20 74 68 65 20 73 71 6c 69 74 65 33  * to the sqlite3
313e7 46 74 73 33 49 6e 69 74 48 61 73 68 54 61 62 6c  Fts3InitHashTabl
313e8 65 28 29 20 66 75 6e 63 74 69 6f 6e 20 28 65 2e  e() function (e.
313e9 67 2e 20 27 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  g. 'fts3_tokeniz
313ea 65 72 27 29 2e 0a 2a 2a 0a 2a 2a 20 49 66 20 74  er')..**.** If t
313eb 68 65 20 3c 70 6f 69 6e 74 65 72 3e 20 61 72 67  he <pointer> arg
313ec 75 6d 65 6e 74 20 69 73 20 73 70 65 63 69 66 69  ument is specifi
313ed 65 64 2c 20 69 74 20 6d 75 73 74 20 62 65 20 61  ed, it must be a
313ee 20 62 6c 6f 62 20 76 61 6c 75 65 0a 2a 2a 20 63   blob value.** c
313ef 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e  ontaining a poin
313f0 74 65 72 20 74 6f 20 62 65 20 73 74 6f 72 65 64  ter to be stored
313f1 20 61 73 20 74 68 65 20 68 61 73 68 20 64 61 74   as the hash dat
313f2 61 20 63 6f 72 72 65 73 70 6f 6e 64 69 6e 67 0a  a corresponding.
313f3 2a 2a 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67  ** to the string
313f4 20 3c 6b 65 79 2d 6e 61 6d 65 3e 2e 20 49 66 20   <key-name>. If 
313f5 3c 70 6f 69 6e 74 65 72 3e 20 69 73 20 6e 6f 74  <pointer> is not
313f6 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65 6e   specified, then
313f7 0a 2a 2a 20 74 68 65 20 73 74 72 69 6e 67 20 3c  .** the string <
313f8 6b 65 79 2d 6e 61 6d 65 3e 20 6d 75 73 74 20 61  key-name> must a
313f9 6c 72 65 61 64 79 20 65 78 69 73 74 20 69 6e 20  lready exist in 
313fa 74 68 65 20 68 61 73 20 74 61 62 6c 65 2e 20 4f  the has table. O
313fb 74 68 65 72 77 69 73 65 2c 0a 2a 2a 20 61 6e 20  therwise,.** an 
313fc 65 72 72 6f 72 20 69 73 20 72 65 74 75 72 6e 65  error is returne
313fd 64 2e 0a 2a 2a 0a 2a 2a 20 57 68 65 74 68 65 72  d..**.** Whether
313fe 20 6f 72 20 6e 6f 74 20 74 68 65 20 3c 70 6f 69   or not the <poi
313ff 6e 74 65 72 3e 20 61 72 67 75 6d 65 6e 74 20 69  nter> argument i
31400 73 20 73 70 65 63 69 66 69 65 64 2c 20 74 68 65  s specified, the
31401 20 76 61 6c 75 65 20 72 65 74 75 72 6e 65 64 0a   value returned.
31402 2a 2a 20 69 73 20 61 20 62 6c 6f 62 20 63 6f 6e  ** is a blob con
31403 74 61 69 6e 69 6e 67 20 74 68 65 20 70 6f 69 6e  taining the poin
31404 74 65 72 20 73 74 6f 72 65 64 20 61 73 20 74 68  ter stored as th
31405 65 20 68 61 73 68 20 64 61 74 61 20 63 6f 72 72  e hash data corr
31406 65 73 70 6f 6e 64 69 6e 67 0a 2a 2a 20 74 6f 20  esponding.** to 
31407 73 74 72 69 6e 67 20 3c 6b 65 79 2d 6e 61 6d 65  string <key-name
31408 3e 20 28 61 66 74 65 72 20 74 68 65 20 68 61 73  > (after the has
31409 68 2d 74 61 62 6c 65 20 69 73 20 75 70 64 61 74  h-table is updat
3140a 65 64 2c 20 69 66 20 61 70 70 6c 69 63 61 62 6c  ed, if applicabl
3140b 65 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  e)..*/.static vo
3140c 69 64 20 73 63 61 6c 61 72 46 75 6e 63 28 0a 20  id scalarFunc(. 
3140d 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
3140e 20 2a 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74   *context,.  int
3140f 20 61 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33   argc,.  sqlite3
31410 5f 76 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b  _value **argv.){
31411 0a 20 20 66 74 73 33 48 61 73 68 20 2a 70 48 61  .  fts3Hash *pHa
31412 73 68 3b 0a 20 20 76 6f 69 64 20 2a 70 50 74 72  sh;.  void *pPtr
31413 20 3d 20 30 3b 0a 20 20 63 6f 6e 73 74 20 75 6e   = 0;.  const un
31414 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 4e 61  signed char *zNa
31415 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d 65 3b  me;.  int nName;
31416 0a 0a 20 20 61 73 73 65 72 74 28 20 61 72 67 63  ..  assert( argc
31417 3d 3d 31 20 7c 7c 20 61 72 67 63 3d 3d 32 20 29  ==1 || argc==2 )
31418 3b 0a 0a 20 20 70 48 61 73 68 20 3d 20 28 66 74  ;..  pHash = (ft
31419 73 33 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33  s3Hash *)sqlite3
3141a 5f 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65  _user_data(conte
3141b 78 74 29 3b 0a 0a 20 20 7a 4e 61 6d 65 20 3d 20  xt);..  zName = 
3141c 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
3141d 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e  xt(argv[0]);.  n
3141e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33 5f 76  Name = sqlite3_v
3141f 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
31420 30 5d 29 2b 31 3b 0a 0a 20 20 69 66 28 20 61 72  0])+1;..  if( ar
31421 67 63 3d 3d 32 20 29 7b 0a 20 20 20 20 76 6f 69  gc==2 ){.    voi
31422 64 20 2a 70 4f 6c 64 3b 0a 20 20 20 20 69 6e 74  d *pOld;.    int
31423 20 6e 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c   n = sqlite3_val
31424 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b 31 5d  ue_bytes(argv[1]
31425 29 3b 0a 20 20 20 20 69 66 28 20 6e 21 3d 73 69  );.    if( n!=si
31426 7a 65 6f 66 28 70 50 74 72 29 20 29 7b 0a 20 20  zeof(pPtr) ){.  
31427 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75      sqlite3_resu
31428 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74  lt_error(context
31429 2c 20 22 61 72 67 75 6d 65 6e 74 20 74 79 70 65  , "argument type
3142a 20 6d 69 73 6d 61 74 63 68 22 2c 20 2d 31 29 3b   mismatch", -1);
3142b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  .      return;. 
3142c 20 20 20 7d 0a 20 20 20 20 70 50 74 72 20 3d 20     }.    pPtr = 
3142d 2a 28 76 6f 69 64 20 2a 2a 29 73 71 6c 69 74 65  *(void **)sqlite
3142e 33 5f 76 61 6c 75 65 5f 62 6c 6f 62 28 61 72 67  3_value_blob(arg
3142f 76 5b 31 5d 29 3b 0a 20 20 20 20 70 4f 6c 64 20  v[1]);.    pOld 
31430 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48 61 73  = sqlite3Fts3Has
31431 68 49 6e 73 65 72 74 28 70 48 61 73 68 2c 20 28  hInsert(pHash, (
31432 76 6f 69 64 20 2a 29 7a 4e 61 6d 65 2c 20 6e 4e  void *)zName, nN
31433 61 6d 65 2c 20 70 50 74 72 29 3b 0a 20 20 20 20  ame, pPtr);.    
31434 69 66 28 20 70 4f 6c 64 3d 3d 70 50 74 72 20 29  if( pOld==pPtr )
31435 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
31436 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e  result_error(con
31437 74 65 78 74 2c 20 22 6f 75 74 20 6f 66 20 6d 65  text, "out of me
31438 6d 6f 72 79 22 2c 20 2d 31 29 3b 0a 20 20 20 20  mory", -1);.    
31439 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
3143a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 70 50 74    }else{.    pPt
3143b 72 20 3d 20 73 71 6c 69 74 65 33 46 74 73 33 48  r = sqlite3Fts3H
3143c 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c 20 7a  ashFind(pHash, z
3143d 4e 61 6d 65 2c 20 6e 4e 61 6d 65 29 3b 0a 20 20  Name, nName);.  
3143e 20 20 69 66 28 20 21 70 50 74 72 20 29 7b 0a 20    if( !pPtr ){. 
3143f 20 20 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20       char *zErr 
31440 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
31441 66 28 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e  f("unknown token
31442 69 7a 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65  izer: %s", zName
31443 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
31444 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72 28 63 6f  _result_error(co
31445 6e 74 65 78 74 2c 20 7a 45 72 72 2c 20 2d 31 29  ntext, zErr, -1)
31446 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  ;.      sqlite3_
31447 66 72 65 65 28 7a 45 72 72 29 3b 0a 20 20 20 20  free(zErr);.    
31448 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a    return;.    }.
31449 20 20 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 72    }..  sqlite3_r
3144a 65 73 75 6c 74 5f 62 6c 6f 62 28 63 6f 6e 74 65  esult_blob(conte
3144b 78 74 2c 20 28 76 6f 69 64 20 2a 29 26 70 50 74  xt, (void *)&pPt
3144c 72 2c 20 73 69 7a 65 6f 66 28 70 50 74 72 29 2c  r, sizeof(pPtr),
3144d 20 53 51 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e   SQLITE_TRANSIEN
3144e 54 29 3b 0a 7d 0a 0a 23 69 66 64 65 66 20 53 51  T);.}..#ifdef SQ
3144f 4c 49 54 45 5f 54 45 53 54 0a 0a 0a 2f 2a 0a 2a  LITE_TEST.../*.*
31450 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
31451 20 6f 66 20 61 20 73 70 65 63 69 61 6c 20 53 51   of a special SQ
31452 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
31453 6e 20 66 6f 72 20 74 65 73 74 69 6e 67 20 74 6f  n for testing to
31454 6b 65 6e 69 7a 65 72 73 20 0a 2a 2a 20 64 65 73  kenizers .** des
31455 69 67 6e 65 64 20 74 6f 20 62 65 20 75 73 65 64  igned to be used
31456 20 69 6e 20 63 6f 6e 63 65 72 74 20 77 69 74 68   in concert with
31457 20 74 68 65 20 54 63 6c 20 74 65 73 74 69 6e 67   the Tcl testing
31458 20 66 72 61 6d 65 77 6f 72 6b 2e 20 54 68 69 73   framework. This
31459 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 6d 75 73  .** function mus
3145a 74 20 62 65 20 63 61 6c 6c 65 64 20 77 69 74 68  t be called with
3145b 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 3a 0a   two arguments:.
3145c 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45 43 54 20 3c  **.**   SELECT <
3145d 66 75 6e 63 74 69 6f 6e 2d 6e 61 6d 65 3e 28 3c  function-name>(<
3145e 6b 65 79 2d 6e 61 6d 65 3e 2c 20 3c 69 6e 70 75  key-name>, <inpu
3145f 74 2d 73 74 72 69 6e 67 3e 29 3b 0a 2a 2a 20 20  t-string>);.**  
31460 20 53 45 4c 45 43 54 20 3c 66 75 6e 63 74 69 6f   SELECT <functio
31461 6e 2d 6e 61 6d 65 3e 28 3c 6b 65 79 2d 6e 61 6d  n-name>(<key-nam
31462 65 3e 2c 20 3c 70 6f 69 6e 74 65 72 3e 29 3b 0a  e>, <pointer>);.
31463 2a 2a 0a 2a 2a 20 77 68 65 72 65 20 3c 66 75 6e  **.** where <fun
31464 63 74 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74  ction-name> is t
31465 68 65 20 6e 61 6d 65 20 70 61 73 73 65 64 20 61  he name passed a
31466 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
31467 75 6d 65 6e 74 0a 2a 2a 20 74 6f 20 74 68 65 20  ument.** to the 
31468 73 71 6c 69 74 65 33 46 74 73 33 49 6e 69 74 48  sqlite3Fts3InitH
31469 61 73 68 54 61 62 6c 65 28 29 20 66 75 6e 63 74  ashTable() funct
3146a 69 6f 6e 20 28 65 2e 67 2e 20 27 66 74 73 33 5f  ion (e.g. 'fts3_
3146b 74 6f 6b 65 6e 69 7a 65 72 27 29 0a 2a 2a 20 63  tokenizer').** c
3146c 6f 6e 63 61 74 65 6e 61 74 65 64 20 77 69 74 68  oncatenated with
3146d 20 74 68 65 20 73 74 72 69 6e 67 20 27 5f 74 65   the string '_te
3146e 73 74 27 20 28 65 2e 67 2e 20 27 66 74 73 33 5f  st' (e.g. 'fts3_
3146f 74 6f 6b 65 6e 69 7a 65 72 5f 74 65 73 74 27 29  tokenizer_test')
31470 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 72 65 74 75  ..**.** The retu
31471 72 6e 20 76 61 6c 75 65 20 69 73 20 61 20 73 74  rn value is a st
31472 72 69 6e 67 20 74 68 61 74 20 6d 61 79 20 62 65  ring that may be
31473 20 69 6e 74 65 72 70 72 65 74 65 64 20 61 73 20   interpreted as 
31474 61 20 54 63 6c 0a 2a 2a 20 6c 69 73 74 2e 20 46  a Tcl.** list. F
31475 6f 72 20 65 61 63 68 20 74 6f 6b 65 6e 20 69 6e  or each token in
31476 20 74 68 65 20 3c 69 6e 70 75 74 2d 73 74 72 69   the <input-stri
31477 6e 67 3e 2c 20 74 68 72 65 65 20 65 6c 65 6d 65  ng>, three eleme
31478 6e 74 73 20 61 72 65 0a 2a 2a 20 61 64 64 65 64  nts are.** added
31479 20 74 6f 20 74 68 65 20 72 65 74 75 72 6e 65 64   to the returned
3147a 20 6c 69 73 74 2e 20 54 68 65 20 66 69 72 73 74   list. The first
3147b 20 69 73 20 74 68 65 20 74 6f 6b 65 6e 20 70 6f   is the token po
3147c 73 69 74 69 6f 6e 2c 20 74 68 65 20 0a 2a 2a 20  sition, the .** 
3147d 73 65 63 6f 6e 64 20 69 73 20 74 68 65 20 74 6f  second is the to
3147e 6b 65 6e 20 74 65 78 74 20 28 66 6f 6c 64 65 64  ken text (folded
3147f 2c 20 73 74 65 6d 6d 65 64 2c 20 65 74 63 2e 29  , stemmed, etc.)
31480 20 61 6e 64 20 74 68 65 20 74 68 69 72 64 20 69   and the third i
31481 73 20 74 68 65 0a 2a 2a 20 73 75 62 73 74 72 69  s the.** substri
31482 6e 67 20 6f 66 20 3c 69 6e 70 75 74 2d 73 74 72  ng of <input-str
31483 69 6e 67 3e 20 61 73 73 6f 63 69 61 74 65 64 20  ing> associated 
31484 77 69 74 68 20 74 68 65 20 74 6f 6b 65 6e 2e 20  with the token. 
31485 46 6f 72 20 65 78 61 6d 70 6c 65 2c 20 0a 2a 2a  For example, .**
31486 20 75 73 69 6e 67 20 74 68 65 20 62 75 69 6c 74   using the built
31487 2d 69 6e 20 22 73 69 6d 70 6c 65 22 20 74 6f 6b  -in "simple" tok
31488 65 6e 69 7a 65 72 3a 0a 2a 2a 0a 2a 2a 20 20 20  enizer:.**.**   
31489 53 45 4c 45 43 54 20 66 74 73 5f 74 6f 6b 65 6e  SELECT fts_token
3148a 69 7a 65 72 5f 74 65 73 74 28 27 73 69 6d 70 6c  izer_test('simpl
3148b 65 27 2c 20 27 49 20 64 6f 6e 27 74 20 73 65 65  e', 'I don't see
3148c 20 68 6f 77 27 29 3b 0a 2a 2a 0a 2a 2a 20 77 69   how');.**.** wi
3148d 6c 6c 20 72 65 74 75 72 6e 20 74 68 65 20 73 74  ll return the st
3148e 72 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 22 7b  ring:.**.**   "{
3148f 30 20 69 20 49 20 31 20 64 6f 6e 74 20 64 6f 6e  0 i I 1 dont don
31490 27 74 20 32 20 73 65 65 20 73 65 65 20 33 20 68  't 2 see see 3 h
31491 6f 77 20 68 6f 77 7d 22 0a 2a 2a 20 20 20 0a 2a  ow how}".**   .*
31492 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 74 65  /.static void te
31493 73 74 46 75 6e 63 28 0a 20 20 73 71 6c 69 74 65  stFunc(.  sqlite
31494 33 5f 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65  3_context *conte
31495 78 74 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a  xt,.  int argc,.
31496 20 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20    sqlite3_value 
31497 2a 2a 61 72 67 76 0a 29 7b 0a 20 20 66 74 73 33  **argv.){.  fts3
31498 48 61 73 68 20 2a 70 48 61 73 68 3b 0a 20 20 73  Hash *pHash;.  s
31499 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
3149a 5f 6d 6f 64 75 6c 65 20 2a 70 3b 0a 20 20 73 71  _module *p;.  sq
3149b 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
3149c 2a 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 30 3b  *pTokenizer = 0;
3149d 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
3149e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 73  izer_cursor *pCs
3149f 72 20 3d 20 30 3b 0a 0a 20 20 63 6f 6e 73 74 20  r = 0;..  const 
314a0 63 68 61 72 20 2a 7a 45 72 72 20 3d 20 30 3b 0a  char *zErr = 0;.
314a1 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
314a2 4e 61 6d 65 3b 0a 20 20 69 6e 74 20 6e 4e 61 6d  Name;.  int nNam
314a3 65 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  e;.  const char 
314a4 2a 7a 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e  *zInput;.  int n
314a5 49 6e 70 75 74 3b 0a 0a 20 20 63 6f 6e 73 74 20  Input;..  const 
314a6 63 68 61 72 20 2a 7a 41 72 67 20 3d 20 30 3b 0a  char *zArg = 0;.
314a7 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a  .  const char *z
314a8 54 6f 6b 65 6e 3b 0a 20 20 69 6e 74 20 6e 54 6f  Token;.  int nTo
314a9 6b 65 6e 3b 0a 20 20 69 6e 74 20 69 53 74 61 72  ken;.  int iStar
314aa 74 3b 0a 20 20 69 6e 74 20 69 45 6e 64 3b 0a 20  t;.  int iEnd;. 
314ab 20 69 6e 74 20 69 50 6f 73 3b 0a 0a 20 20 54 63   int iPos;..  Tc
314ac 6c 5f 4f 62 6a 20 2a 70 52 65 74 3b 0a 0a 20 20  l_Obj *pRet;..  
314ad 61 73 73 65 72 74 28 20 61 72 67 63 3d 3d 32 20  assert( argc==2 
314ae 7c 7c 20 61 72 67 63 3d 3d 33 20 29 3b 0a 0a 20  || argc==3 );.. 
314af 20 6e 4e 61 6d 65 20 3d 20 73 71 6c 69 74 65 33   nName = sqlite3
314b0 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
314b1 76 5b 30 5d 29 3b 0a 20 20 7a 4e 61 6d 65 20 3d  v[0]);.  zName =
314b2 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29 73   (const char *)s
314b3 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78  qlite3_value_tex
314b4 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 6e 49  t(argv[0]);.  nI
314b5 6e 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 76  nput = sqlite3_v
314b6 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67 76 5b  alue_bytes(argv[
314b7 61 72 67 63 2d 31 5d 29 3b 0a 20 20 7a 49 6e 70  argc-1]);.  zInp
314b8 75 74 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  ut = (const char
314b9 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
314ba 5f 74 65 78 74 28 61 72 67 76 5b 61 72 67 63 2d  _text(argv[argc-
314bb 31 5d 29 3b 0a 0a 20 20 69 66 28 20 61 72 67 63  1]);..  if( argc
314bc 3d 3d 33 20 29 7b 0a 20 20 20 20 7a 41 72 67 20  ==3 ){.    zArg 
314bd 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20 2a 29  = (const char *)
314be 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
314bf 78 74 28 61 72 67 76 5b 31 5d 29 3b 0a 20 20 7d  xt(argv[1]);.  }
314c0 0a 0a 20 20 70 48 61 73 68 20 3d 20 28 66 74 73  ..  pHash = (fts
314c1 33 48 61 73 68 20 2a 29 73 71 6c 69 74 65 33 5f  3Hash *)sqlite3_
314c2 75 73 65 72 5f 64 61 74 61 28 63 6f 6e 74 65 78  user_data(contex
314c3 74 29 3b 0a 20 20 70 20 3d 20 28 73 71 6c 69 74  t);.  p = (sqlit
314c4 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64  e3_tokenizer_mod
314c5 75 6c 65 20 2a 29 73 71 6c 69 74 65 33 46 74 73  ule *)sqlite3Fts
314c6 33 48 61 73 68 46 69 6e 64 28 70 48 61 73 68 2c  3HashFind(pHash,
314c7 20 7a 4e 61 6d 65 2c 20 6e 4e 61 6d 65 2b 31 29   zName, nName+1)
314c8 3b 0a 0a 20 20 69 66 28 20 21 70 20 29 7b 0a 20  ;..  if( !p ){. 
314c9 20 20 20 63 68 61 72 20 2a 7a 45 72 72 20 3d 20     char *zErr = 
314ca 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28  sqlite3_mprintf(
314cb 22 75 6e 6b 6e 6f 77 6e 20 74 6f 6b 65 6e 69 7a  "unknown tokeniz
314cc 65 72 3a 20 25 73 22 2c 20 7a 4e 61 6d 65 29 3b  er: %s", zName);
314cd 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 72 65 73  .    sqlite3_res
314ce 75 6c 74 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78  ult_error(contex
314cf 74 2c 20 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20  t, zErr, -1);.  
314d0 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
314d1 45 72 72 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  Err);.    return
314d2 3b 0a 20 20 7d 0a 0a 20 20 70 52 65 74 20 3d 20  ;.  }..  pRet = 
314d3 54 63 6c 5f 4e 65 77 4f 62 6a 28 29 3b 0a 20 20  Tcl_NewObj();.  
314d4 54 63 6c 5f 49 6e 63 72 52 65 66 43 6f 75 6e 74  Tcl_IncrRefCount
314d5 28 70 52 65 74 29 3b 0a 0a 20 20 69 66 28 20 53  (pRet);..  if( S
314d6 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78 43 72  QLITE_OK!=p->xCr
314d7 65 61 74 65 28 7a 41 72 67 20 3f 20 31 20 3a 20  eate(zArg ? 1 : 
314d8 30 2c 20 26 7a 41 72 67 2c 20 26 70 54 6f 6b 65  0, &zArg, &pToke
314d9 6e 69 7a 65 72 29 20 29 7b 0a 20 20 20 20 7a 45  nizer) ){.    zE
314da 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20 78  rr = "error in x
314db 43 72 65 61 74 65 28 29 22 3b 0a 20 20 20 20 67  Create()";.    g
314dc 6f 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a  oto finish;.  }.
314dd 20 20 70 54 6f 6b 65 6e 69 7a 65 72 2d 3e 70 4d    pTokenizer->pM
314de 6f 64 75 6c 65 20 3d 20 70 3b 0a 20 20 69 66 28  odule = p;.  if(
314df 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78   SQLITE_OK!=p->x
314e0 4f 70 65 6e 28 70 54 6f 6b 65 6e 69 7a 65 72 2c  Open(pTokenizer,
314e1 20 7a 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c   zInput, nInput,
314e2 20 26 70 43 73 72 29 20 29 7b 0a 20 20 20 20 7a   &pCsr) ){.    z
314e3 45 72 72 20 3d 20 22 65 72 72 6f 72 20 69 6e 20  Err = "error in 
314e4 78 4f 70 65 6e 28 29 22 3b 0a 20 20 20 20 67 6f  xOpen()";.    go
314e5 74 6f 20 66 69 6e 69 73 68 3b 0a 20 20 7d 0a 20  to finish;.  }. 
314e6 20 70 43 73 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65   pCsr->pTokenize
314e7 72 20 3d 20 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a  r = pTokenizer;.
314e8 0a 20 20 77 68 69 6c 65 28 20 53 51 4c 49 54 45  .  while( SQLITE
314e9 5f 4f 4b 3d 3d 70 2d 3e 78 4e 65 78 74 28 70 43  _OK==p->xNext(pC
314ea 73 72 2c 20 26 7a 54 6f 6b 65 6e 2c 20 26 6e 54  sr, &zToken, &nT
314eb 6f 6b 65 6e 2c 20 26 69 53 74 61 72 74 2c 20 26  oken, &iStart, &
314ec 69 45 6e 64 2c 20 26 69 50 6f 73 29 20 29 7b 0a  iEnd, &iPos) ){.
314ed 20 20 20 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41      Tcl_ListObjA
314ee 70 70 65 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20  ppendElement(0, 
314ef 70 52 65 74 2c 20 54 63 6c 5f 4e 65 77 49 6e 74  pRet, Tcl_NewInt
314f0 4f 62 6a 28 69 50 6f 73 29 29 3b 0a 20 20 20 20  Obj(iPos));.    
314f1 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65 6e  Tcl_ListObjAppen
314f2 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65 74  dElement(0, pRet
314f3 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f  , Tcl_NewStringO
314f4 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b 65  bj(zToken, nToke
314f5 6e 29 29 3b 0a 20 20 20 20 7a 54 6f 6b 65 6e 20  n));.    zToken 
314f6 3d 20 26 7a 49 6e 70 75 74 5b 69 53 74 61 72 74  = &zInput[iStart
314f7 5d 3b 0a 20 20 20 20 6e 54 6f 6b 65 6e 20 3d 20  ];.    nToken = 
314f8 69 45 6e 64 2d 69 53 74 61 72 74 3b 0a 20 20 20  iEnd-iStart;.   
314f9 20 54 63 6c 5f 4c 69 73 74 4f 62 6a 41 70 70 65   Tcl_ListObjAppe
314fa 6e 64 45 6c 65 6d 65 6e 74 28 30 2c 20 70 52 65  ndElement(0, pRe
314fb 74 2c 20 54 63 6c 5f 4e 65 77 53 74 72 69 6e 67  t, Tcl_NewString
314fc 4f 62 6a 28 7a 54 6f 6b 65 6e 2c 20 6e 54 6f 6b  Obj(zToken, nTok
314fd 65 6e 29 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28  en));.  }..  if(
314fe 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 70 2d 3e 78   SQLITE_OK!=p->x
314ff 43 6c 6f 73 65 28 70 43 73 72 29 20 29 7b 0a 20  Close(pCsr) ){. 
31500 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f 72     zErr = "error
31501 20 69 6e 20 78 43 6c 6f 73 65 28 29 22 3b 0a 20   in xClose()";. 
31502 20 20 20 67 6f 74 6f 20 66 69 6e 69 73 68 3b 0a     goto finish;.
31503 20 20 7d 0a 20 20 69 66 28 20 53 51 4c 49 54 45    }.  if( SQLITE
31504 5f 4f 4b 21 3d 70 2d 3e 78 44 65 73 74 72 6f 79  _OK!=p->xDestroy
31505 28 70 54 6f 6b 65 6e 69 7a 65 72 29 20 29 7b 0a  (pTokenizer) ){.
31506 20 20 20 20 7a 45 72 72 20 3d 20 22 65 72 72 6f      zErr = "erro
31507 72 20 69 6e 20 78 44 65 73 74 72 6f 79 28 29 22  r in xDestroy()"
31508 3b 0a 20 20 20 20 67 6f 74 6f 20 66 69 6e 69 73  ;.    goto finis
31509 68 3b 0a 20 20 7d 0a 0a 66 69 6e 69 73 68 3a 0a  h;.  }..finish:.
3150a 20 20 69 66 28 20 7a 45 72 72 20 29 7b 0a 20 20    if( zErr ){.  
3150b 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74    sqlite3_result
3150c 5f 65 72 72 6f 72 28 63 6f 6e 74 65 78 74 2c 20  _error(context, 
3150d 7a 45 72 72 2c 20 2d 31 29 3b 0a 20 20 7d 65 6c  zErr, -1);.  }el
3150e 73 65 7b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  se{.    sqlite3_
3150f 72 65 73 75 6c 74 5f 74 65 78 74 28 63 6f 6e 74  result_text(cont
31510 65 78 74 2c 20 54 63 6c 5f 47 65 74 53 74 72 69  ext, Tcl_GetStri
31511 6e 67 28 70 52 65 74 29 2c 20 2d 31 2c 20 53 51  ng(pRet), -1, SQ
31512 4c 49 54 45 5f 54 52 41 4e 53 49 45 4e 54 29 3b  LITE_TRANSIENT);
31513 0a 20 20 7d 0a 20 20 54 63 6c 5f 44 65 63 72 52  .  }.  Tcl_DecrR
31514 65 66 43 6f 75 6e 74 28 70 52 65 74 29 3b 0a 7d  efCount(pRet);.}
31515 0a 0a 73 74 61 74 69 63 0a 69 6e 74 20 72 65 67  ..static.int reg
31516 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28 0a  isterTokenizer(.
31517 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 20 0a    sqlite3 *db, .
31518 20 20 63 68 61 72 20 2a 7a 4e 61 6d 65 2c 20 0a    char *zName, .
31519 20 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f    const sqlite3_
3151a 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
3151b 20 2a 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b   *p.){.  int rc;
3151c 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20  .  sqlite3_stmt 
3151d 2a 70 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20  *pStmt;.  const 
3151e 63 68 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53  char zSql[] = "S
3151f 45 4c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e  ELECT fts3_token
31520 69 7a 65 72 28 3f 2c 20 3f 29 22 3b 0a 0a 20 20  izer(?, ?)";..  
31521 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 70 72 65  rc = sqlite3_pre
31522 70 61 72 65 5f 76 32 28 64 62 2c 20 7a 53 71 6c  pare_v2(db, zSql
31523 2c 20 2d 31 2c 20 26 70 53 74 6d 74 2c 20 30 29  , -1, &pStmt, 0)
31524 3b 0a 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49  ;.  if( rc!=SQLI
31525 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 72 65 74  TE_OK ){.    ret
31526 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20 73  urn rc;.  }..  s
31527 71 6c 69 74 65 33 5f 62 69 6e 64 5f 74 65 78 74  qlite3_bind_text
31528 28 70 53 74 6d 74 2c 20 31 2c 20 7a 4e 61 6d 65  (pStmt, 1, zName
31529 2c 20 2d 31 2c 20 53 51 4c 49 54 45 5f 53 54 41  , -1, SQLITE_STA
3152a 54 49 43 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  TIC);.  sqlite3_
3152b 62 69 6e 64 5f 62 6c 6f 62 28 70 53 74 6d 74 2c  bind_blob(pStmt,
3152c 20 32 2c 20 26 70 2c 20 73 69 7a 65 6f 66 28 70   2, &p, sizeof(p
3152d 29 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  ), SQLITE_STATIC
3152e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65  );.  sqlite3_ste
3152f 70 28 70 53 74 6d 74 29 3b 0a 0a 20 20 72 65 74  p(pStmt);..  ret
31530 75 72 6e 20 73 71 6c 69 74 65 33 5f 66 69 6e 61  urn sqlite3_fina
31531 6c 69 7a 65 28 70 53 74 6d 74 29 3b 0a 7d 0a 0a  lize(pStmt);.}..
31532 73 74 61 74 69 63 0a 69 6e 74 20 71 75 65 72 79  static.int query
31533 54 6f 6b 65 6e 69 7a 65 72 28 0a 20 20 73 71 6c  Tokenizer(.  sql
31534 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68 61  ite3 *db, .  cha
31535 72 20 2a 7a 4e 61 6d 65 2c 20 20 0a 20 20 63 6f  r *zName,  .  co
31536 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  nst sqlite3_toke
31537 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 2a 2a 70  nizer_module **p
31538 70 0a 29 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20  p.){.  int rc;. 
31539 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
3153a 53 74 6d 74 3b 0a 20 20 63 6f 6e 73 74 20 63 68  Stmt;.  const ch
3153b 61 72 20 7a 53 71 6c 5b 5d 20 3d 20 22 53 45 4c  ar zSql[] = "SEL
3153c 45 43 54 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a  ECT fts3_tokeniz
3153d 65 72 28 3f 29 22 3b 0a 0a 20 20 2a 70 70 20 3d  er(?)";..  *pp =
3153e 20 30 3b 0a 20 20 72 63 20 3d 20 73 71 6c 69 74   0;.  rc = sqlit
3153f 65 33 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62  e3_prepare_v2(db
31540 2c 20 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74  , zSql, -1, &pSt
31541 6d 74 2c 20 30 29 3b 0a 20 20 69 66 28 20 72 63  mt, 0);.  if( rc
31542 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
31543 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
31544 7d 0a 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  }..  sqlite3_bin
31545 64 5f 74 65 78 74 28 70 53 74 6d 74 2c 20 31 2c  d_text(pStmt, 1,
31546 20 7a 4e 61 6d 65 2c 20 2d 31 2c 20 53 51 4c 49   zName, -1, SQLI
31547 54 45 5f 53 54 41 54 49 43 29 3b 0a 20 20 69 66  TE_STATIC);.  if
31548 28 20 53 51 4c 49 54 45 5f 52 4f 57 3d 3d 73 71  ( SQLITE_ROW==sq
31549 6c 69 74 65 33 5f 73 74 65 70 28 70 53 74 6d 74  lite3_step(pStmt
3154a 29 20 29 7b 0a 20 20 20 20 69 66 28 20 73 71 6c  ) ){.    if( sql
3154b 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 74 79 70 65  ite3_column_type
3154c 28 70 53 74 6d 74 2c 20 30 29 3d 3d 53 51 4c 49  (pStmt, 0)==SQLI
3154d 54 45 5f 42 4c 4f 42 20 29 7b 0a 20 20 20 20 20  TE_BLOB ){.     
3154e 20 6d 65 6d 63 70 79 28 70 70 2c 20 73 71 6c 69   memcpy(pp, sqli
3154f 74 65 33 5f 63 6f 6c 75 6d 6e 5f 62 6c 6f 62 28  te3_column_blob(
31550 70 53 74 6d 74 2c 20 30 29 2c 20 73 69 7a 65 6f  pStmt, 0), sizeo
31551 66 28 2a 70 70 29 29 3b 0a 20 20 20 20 7d 0a 20  f(*pp));.    }. 
31552 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 73 71 6c   }..  return sql
31553 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
31554 74 6d 74 29 3b 0a 7d 0a 0a 53 51 4c 49 54 45 5f  tmt);.}..SQLITE_
31555 50 52 49 56 41 54 45 20 76 6f 69 64 20 73 71 6c  PRIVATE void sql
31556 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65 54 6f  ite3Fts3SimpleTo
31557 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28 73 71  kenizerModule(sq
31558 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
31559 6d 6f 64 75 6c 65 20 63 6f 6e 73 74 2a 2a 70 70  module const**pp
3155a 4d 6f 64 75 6c 65 29 3b 0a 0a 2f 2a 0a 2a 2a 20  Module);../*.** 
3155b 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f  Implementation o
3155c 66 20 74 68 65 20 73 63 61 6c 61 72 20 66 75 6e  f the scalar fun
3155d 63 74 69 6f 6e 20 66 74 73 33 5f 74 6f 6b 65 6e  ction fts3_token
3155e 69 7a 65 72 5f 69 6e 74 65 72 6e 61 6c 5f 74 65  izer_internal_te
3155f 73 74 28 29 2e 0a 2a 2a 20 54 68 69 73 20 66 75  st()..** This fu
31560 6e 63 74 69 6f 6e 20 69 73 20 75 73 65 64 20 66  nction is used f
31561 6f 72 20 74 65 73 74 69 6e 67 20 6f 6e 6c 79 2c  or testing only,
31562 20 69 74 20 69 73 20 6e 6f 74 20 69 6e 63 6c 75   it is not inclu
31563 64 65 64 20 69 6e 20 74 68 65 0a 2a 2a 20 62 75  ded in the.** bu
31564 69 6c 64 20 75 6e 6c 65 73 73 20 53 51 4c 49 54  ild unless SQLIT
31565 45 5f 54 45 53 54 20 69 73 20 64 65 66 69 6e 65  E_TEST is define
31566 64 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 70 75 72  d..**.** The pur
31567 70 6f 73 65 20 6f 66 20 74 68 69 73 20 69 73 20  pose of this is 
31568 74 6f 20 74 65 73 74 20 74 68 61 74 20 74 68 65  to test that the
31569 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 28   fts3_tokenizer(
3156a 29 20 66 75 6e 63 74 69 6f 6e 0a 2a 2a 20 63 61  ) function.** ca
3156b 6e 20 62 65 20 75 73 65 64 20 61 73 20 64 65 73  n be used as des
3156c 69 67 6e 65 64 20 62 79 20 74 68 65 20 43 2d 63  igned by the C-c
3156d 6f 64 65 20 69 6e 20 74 68 65 20 71 75 65 72 79  ode in the query
3156e 54 6f 6b 65 6e 69 7a 65 72 20 61 6e 64 0a 2a 2a  Tokenizer and.**
3156f 20 72 65 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a   registerTokeniz
31570 65 72 28 29 20 66 75 6e 63 74 69 6f 6e 73 20 61  er() functions a
31571 62 6f 76 65 2e 20 54 68 65 73 65 20 74 77 6f 20  bove. These two 
31572 66 75 6e 63 74 69 6f 6e 73 20 61 72 65 20 72 65  functions are re
31573 70 65 61 74 65 64 0a 2a 2a 20 69 6e 20 74 68 65  peated.** in the
31574 20 52 45 41 44 4d 45 2e 74 6f 6b 65 6e 69 7a 65   README.tokenize
31575 72 20 66 69 6c 65 20 61 73 20 61 6e 20 65 78 61  r file as an exa
31576 6d 70 6c 65 2c 20 73 6f 20 69 74 20 69 73 20 69  mple, so it is i
31577 6d 70 6f 72 74 61 6e 74 20 74 6f 0a 2a 2a 20 74  mportant to.** t
31578 65 73 74 20 74 68 65 6d 2e 0a 2a 2a 0a 2a 2a 20  est them..**.** 
31579 54 6f 20 72 75 6e 20 74 68 65 20 74 65 73 74 73  To run the tests
3157a 2c 20 65 76 61 6c 75 61 74 65 20 74 68 65 20 66  , evaluate the f
3157b 74 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e  ts3_tokenizer_in
3157c 74 65 72 6e 61 6c 5f 74 65 73 74 28 29 20 73 63  ternal_test() sc
3157d 61 6c 61 72 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e  alar.** function
3157e 20 77 69 74 68 20 6e 6f 20 61 72 67 75 6d 65 6e   with no argumen
3157f 74 73 2e 20 41 6e 20 61 73 73 65 72 74 28 29 20  ts. An assert() 
31580 77 69 6c 6c 20 66 61 69 6c 20 69 66 20 61 20 70  will fail if a p
31581 72 6f 62 6c 65 6d 20 69 73 0a 2a 2a 20 64 65 74  roblem is.** det
31582 65 63 74 65 64 2e 20 69 2e 65 2e 3a 0a 2a 2a 0a  ected. i.e.:.**.
31583 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20 66 74  **     SELECT ft
31584 73 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 69 6e 74  s3_tokenizer_int
31585 65 72 6e 61 6c 5f 74 65 73 74 28 29 3b 0a 2a 2a  ernal_test();.**
31586 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
31587 69 6e 74 54 65 73 74 46 75 6e 63 28 0a 20 20 73  intTestFunc(.  s
31588 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74 20 2a  qlite3_context *
31589 63 6f 6e 74 65 78 74 2c 0a 20 20 69 6e 74 20 61  context,.  int a
3158a 72 67 63 2c 0a 20 20 73 71 6c 69 74 65 33 5f 76  rgc,.  sqlite3_v
3158b 61 6c 75 65 20 2a 2a 61 72 67 76 0a 29 7b 0a 20  alue **argv.){. 
3158c 20 69 6e 74 20 72 63 3b 0a 20 20 63 6f 6e 73 74   int rc;.  const
3158d 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
3158e 65 72 5f 6d 6f 64 75 6c 65 20 2a 70 31 3b 0a 20  er_module *p1;. 
3158f 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74   const sqlite3_t
31590 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20  okenizer_module 
31591 2a 70 32 3b 0a 20 20 73 71 6c 69 74 65 33 20 2a  *p2;.  sqlite3 *
31592 64 62 20 3d 20 28 73 71 6c 69 74 65 33 20 2a 29  db = (sqlite3 *)
31593 73 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74  sqlite3_user_dat
31594 61 28 63 6f 6e 74 65 78 74 29 3b 0a 0a 20 20 2f  a(context);..  /
31595 2a 20 54 65 73 74 20 74 68 65 20 71 75 65 72 79  * Test the query
31596 20 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 20 20 73   function */.  s
31597 71 6c 69 74 65 33 46 74 73 33 53 69 6d 70 6c 65  qlite3Fts3Simple
31598 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65 28  TokenizerModule(
31599 26 70 31 29 3b 0a 20 20 72 63 20 3d 20 71 75 65  &p1);.  rc = que
3159a 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c 20  ryTokenizer(db, 
3159b 22 73 69 6d 70 6c 65 22 2c 20 26 70 32 29 3b 0a  "simple", &p2);.
3159c 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d 53 51    assert( rc==SQ
3159d 4c 49 54 45 5f 4f 4b 20 29 3b 0a 20 20 61 73 73  LITE_OK );.  ass
3159e 65 72 74 28 20 70 31 3d 3d 70 32 20 29 3b 0a 20  ert( p1==p2 );. 
3159f 20 72 63 20 3d 20 71 75 65 72 79 54 6f 6b 65 6e   rc = queryToken
315a0 69 7a 65 72 28 64 62 2c 20 22 6e 6f 73 75 63 68  izer(db, "nosuch
315a1 74 6f 6b 65 6e 69 7a 65 72 22 2c 20 26 70 32 29  tokenizer", &p2)
315a2 3b 0a 20 20 61 73 73 65 72 74 28 20 72 63 3d 3d  ;.  assert( rc==
315a3 53 51 4c 49 54 45 5f 45 52 52 4f 52 20 29 3b 0a  SQLITE_ERROR );.
315a4 20 20 61 73 73 65 72 74 28 20 70 32 3d 3d 30 20    assert( p2==0 
315a5 29 3b 0a 20 20 61 73 73 65 72 74 28 20 30 3d 3d  );.  assert( 0==
315a6 73 74 72 63 6d 70 28 73 71 6c 69 74 65 33 5f 65  strcmp(sqlite3_e
315a7 72 72 6d 73 67 28 64 62 29 2c 20 22 75 6e 6b 6e  rrmsg(db), "unkn
315a8 6f 77 6e 20 74 6f 6b 65 6e 69 7a 65 72 3a 20 6e  own tokenizer: n
315a9 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65 72 22 29  osuchtokenizer")
315aa 20 29 3b 0a 0a 20 20 2f 2a 20 54 65 73 74 20 74   );..  /* Test t
315ab 68 65 20 73 74 6f 72 61 67 65 20 66 75 6e 63 74  he storage funct
315ac 69 6f 6e 20 2a 2f 0a 20 20 72 63 20 3d 20 72 65  ion */.  rc = re
315ad 67 69 73 74 65 72 54 6f 6b 65 6e 69 7a 65 72 28  gisterTokenizer(
315ae 64 62 2c 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e  db, "nosuchtoken
315af 69 7a 65 72 22 2c 20 70 31 29 3b 0a 20 20 61 73  izer", p1);.  as
315b0 73 65 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45  sert( rc==SQLITE
315b1 5f 4f 4b 20 29 3b 0a 20 20 72 63 20 3d 20 71 75  _OK );.  rc = qu
315b2 65 72 79 54 6f 6b 65 6e 69 7a 65 72 28 64 62 2c  eryTokenizer(db,
315b3 20 22 6e 6f 73 75 63 68 74 6f 6b 65 6e 69 7a 65   "nosuchtokenize
315b4 72 22 2c 20 26 70 32 29 3b 0a 20 20 61 73 73 65  r", &p2);.  asse
315b5 72 74 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  rt( rc==SQLITE_O
315b6 4b 20 29 3b 0a 20 20 61 73 73 65 72 74 28 20 70  K );.  assert( p
315b7 32 3d 3d 70 31 20 29 3b 0a 0a 20 20 73 71 6c 69  2==p1 );..  sqli
315b8 74 65 33 5f 72 65 73 75 6c 74 5f 74 65 78 74 28  te3_result_text(
315b9 63 6f 6e 74 65 78 74 2c 20 22 6f 6b 22 2c 20 2d  context, "ok", -
315ba 31 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49 43  1, SQLITE_STATIC
315bb 29 3b 0a 7d 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a  );.}..#endif../*
315bc 0a 2a 2a 20 53 65 74 20 75 70 20 53 51 4c 20 6f  .** Set up SQL o
315bd 62 6a 65 63 74 73 20 69 6e 20 64 61 74 61 62 61  bjects in databa
315be 73 65 20 64 62 20 75 73 65 64 20 74 6f 20 61 63  se db used to ac
315bf 63 65 73 73 20 74 68 65 20 63 6f 6e 74 65 6e 74  cess the content
315c0 73 20 6f 66 0a 2a 2a 20 74 68 65 20 68 61 73 68  s of.** the hash
315c1 20 74 61 62 6c 65 20 70 6f 69 6e 74 65 64 20 74   table pointed t
315c2 6f 20 62 79 20 61 72 67 75 6d 65 6e 74 20 70 48  o by argument pH
315c3 61 73 68 2e 20 54 68 65 20 68 61 73 68 20 74 61  ash. The hash ta
315c4 62 6c 65 20 6d 75 73 74 0a 2a 2a 20 62 65 65 6e  ble must.** been
315c5 20 69 6e 69 74 69 61 6c 69 73 65 64 20 74 6f 20   initialised to 
315c6 75 73 65 20 73 74 72 69 6e 67 20 6b 65 79 73 2c  use string keys,
315c7 20 61 6e 64 20 74 6f 20 74 61 6b 65 20 61 20 70   and to take a p
315c8 72 69 76 61 74 65 20 63 6f 70 79 20 0a 2a 2a 20  rivate copy .** 
315c9 6f 66 20 74 68 65 20 6b 65 79 20 77 68 65 6e 20  of the key when 
315ca 61 20 76 61 6c 75 65 20 69 73 20 69 6e 73 65 72  a value is inser
315cb 74 65 64 2e 20 69 2e 65 2e 20 62 79 20 61 20 63  ted. i.e. by a c
315cc 61 6c 6c 20 73 69 6d 69 6c 61 72 20 74 6f 3a 0a  all similar to:.
315cd 2a 2a 0a 2a 2a 20 20 20 20 73 71 6c 69 74 65 33  **.**    sqlite3
315ce 46 74 73 33 48 61 73 68 49 6e 69 74 28 70 48 61  Fts3HashInit(pHa
315cf 73 68 2c 20 46 54 53 33 5f 48 41 53 48 5f 53 54  sh, FTS3_HASH_ST
315d0 52 49 4e 47 2c 20 31 29 3b 0a 2a 2a 0a 2a 2a 20  RING, 1);.**.** 
315d1 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 61 64  This function ad
315d2 64 73 20 61 20 73 63 61 6c 61 72 20 66 75 6e 63  ds a scalar func
315d3 74 69 6f 6e 20 28 73 65 65 20 68 65 61 64 65 72  tion (see header
315d4 20 63 6f 6d 6d 65 6e 74 20 61 62 6f 76 65 0a 2a   comment above.*
315d5 2a 20 73 63 61 6c 61 72 46 75 6e 63 28 29 20 69  * scalarFunc() i
315d6 6e 20 74 68 69 73 20 66 69 6c 65 20 66 6f 72 20  n this file for 
315d7 64 65 74 61 69 6c 73 29 20 61 6e 64 2c 20 69 66  details) and, if
315d8 20 45 4e 41 42 4c 45 5f 54 41 42 4c 45 20 69 73   ENABLE_TABLE is
315d9 0a 2a 2a 20 64 65 66 69 6e 65 64 20 61 74 20 63  .** defined at c
315da 6f 6d 70 69 6c 61 74 69 6f 6e 20 74 69 6d 65 2c  ompilation time,
315db 20 61 20 74 65 6d 70 6f 72 61 72 79 20 76 69 72   a temporary vir
315dc 74 75 61 6c 20 74 61 62 6c 65 20 28 73 65 65 20  tual table (see 
315dd 68 65 61 64 65 72 20 0a 2a 2a 20 63 6f 6d 6d 65  header .** comme
315de 6e 74 20 61 62 6f 76 65 20 73 74 72 75 63 74 20  nt above struct 
315df 48 61 73 68 54 61 62 6c 65 56 74 61 62 29 20 74  HashTableVtab) t
315e0 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 20 73  o the database s
315e1 63 68 65 6d 61 2e 20 42 6f 74 68 20 0a 2a 2a 20  chema. Both .** 
315e2 70 72 6f 76 69 64 65 20 72 65 61 64 2f 77 72 69  provide read/wri
315e3 74 65 20 61 63 63 65 73 73 20 74 6f 20 74 68 65  te access to the
315e4 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 2a 70 48   contents of *pH
315e5 61 73 68 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 74  ash..**.** The t
315e6 68 69 72 64 20 61 72 67 75 6d 65 6e 74 20 74 6f  hird argument to
315e7 20 74 68 69 73 20 66 75 6e 63 74 69 6f 6e 2c 20   this function, 
315e8 7a 4e 61 6d 65 2c 20 69 73 20 75 73 65 64 20 61  zName, is used a
315e9 73 20 74 68 65 20 6e 61 6d 65 0a 2a 2a 20 6f 66  s the name.** of
315ea 20 62 6f 74 68 20 74 68 65 20 73 63 61 6c 61 72   both the scalar
315eb 20 61 6e 64 2c 20 69 66 20 63 72 65 61 74 65 64   and, if created
315ec 2c 20 74 68 65 20 76 69 72 74 75 61 6c 20 74 61  , the virtual ta
315ed 62 6c 65 2e 0a 2a 2f 0a 53 51 4c 49 54 45 5f 50  ble..*/.SQLITE_P
315ee 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c 69 74  RIVATE int sqlit
315ef 65 33 46 74 73 33 49 6e 69 74 48 61 73 68 54 61  e3Fts3InitHashTa
315f0 62 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 20 2a  ble(.  sqlite3 *
315f1 64 62 2c 20 0a 20 20 66 74 73 33 48 61 73 68 20  db, .  fts3Hash 
315f2 2a 70 48 61 73 68 2c 20 0a 20 20 63 6f 6e 73 74  *pHash, .  const
315f3 20 63 68 61 72 20 2a 7a 4e 61 6d 65 0a 29 7b 0a   char *zName.){.
315f4 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54    int rc = SQLIT
315f5 45 5f 4f 4b 3b 0a 20 20 76 6f 69 64 20 2a 70 20  E_OK;.  void *p 
315f6 3d 20 28 76 6f 69 64 20 2a 29 70 48 61 73 68 3b  = (void *)pHash;
315f7 0a 20 20 63 6f 6e 73 74 20 69 6e 74 20 61 6e 79  .  const int any
315f8 20 3d 20 53 51 4c 49 54 45 5f 41 4e 59 3b 0a 20   = SQLITE_ANY;. 
315f9 20 63 68 61 72 20 2a 7a 54 65 73 74 20 3d 20 30   char *zTest = 0
315fa 3b 0a 20 20 63 68 61 72 20 2a 7a 54 65 73 74 32  ;.  char *zTest2
315fb 20 3d 20 30 3b 0a 0a 23 69 66 64 65 66 20 53 51   = 0;..#ifdef SQ
315fc 4c 49 54 45 5f 54 45 53 54 0a 20 20 76 6f 69 64  LITE_TEST.  void
315fd 20 2a 70 64 62 20 3d 20 28 76 6f 69 64 20 2a 29   *pdb = (void *)
315fe 64 62 3b 0a 20 20 7a 54 65 73 74 20 3d 20 73 71  db;.  zTest = sq
315ff 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
31600 73 5f 74 65 73 74 22 2c 20 7a 4e 61 6d 65 29 3b  s_test", zName);
31601 0a 20 20 7a 54 65 73 74 32 20 3d 20 73 71 6c 69  .  zTest2 = sqli
31602 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25 73 5f  te3_mprintf("%s_
31603 69 6e 74 65 72 6e 61 6c 5f 74 65 73 74 22 2c 20  internal_test", 
31604 7a 4e 61 6d 65 29 3b 0a 20 20 69 66 28 20 21 7a  zName);.  if( !z
31605 54 65 73 74 20 7c 7c 20 21 7a 54 65 73 74 32 20  Test || !zTest2 
31606 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
31607 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 23 65  TE_NOMEM;.  }.#e
31608 6e 64 69 66 0a 0a 20 20 69 66 28 20 72 63 21 3d  ndif..  if( rc!=
31609 53 51 4c 49 54 45 5f 4f 4b 0a 20 20 20 7c 7c 20  SQLITE_OK.   || 
3160a 28 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63 72  (rc = sqlite3_cr
3160b 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62  eate_function(db
3160c 2c 20 7a 4e 61 6d 65 2c 20 31 2c 20 61 6e 79 2c  , zName, 1, any,
3160d 20 70 2c 20 73 63 61 6c 61 72 46 75 6e 63 2c 20   p, scalarFunc, 
3160e 30 2c 20 30 29 29 0a 20 20 20 7c 7c 20 28 72 63  0, 0)).   || (rc
3160f 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
31610 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  e_function(db, z
31611 4e 61 6d 65 2c 20 32 2c 20 61 6e 79 2c 20 70 2c  Name, 2, any, p,
31612 20 73 63 61 6c 61 72 46 75 6e 63 2c 20 30 2c 20   scalarFunc, 0, 
31613 30 29 29 0a 23 69 66 64 65 66 20 53 51 4c 49 54  0)).#ifdef SQLIT
31614 45 5f 54 45 53 54 0a 20 20 20 7c 7c 20 28 72 63  E_TEST.   || (rc
31615 20 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74   = sqlite3_creat
31616 65 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 7a  e_function(db, z
31617 54 65 73 74 2c 20 32 2c 20 61 6e 79 2c 20 70 2c  Test, 2, any, p,
31618 20 74 65 73 74 46 75 6e 63 2c 20 30 2c 20 30 29   testFunc, 0, 0)
31619 29 0a 20 20 20 7c 7c 20 28 72 63 20 3d 20 73 71  ).   || (rc = sq
3161a 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e  lite3_create_fun
3161b 63 74 69 6f 6e 28 64 62 2c 20 7a 54 65 73 74 2c  ction(db, zTest,
3161c 20 33 2c 20 61 6e 79 2c 20 70 2c 20 74 65 73 74   3, any, p, test
3161d 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 20 20 20  Func, 0, 0)).   
3161e 7c 7c 20 28 72 63 20 3d 20 73 71 6c 69 74 65 33  || (rc = sqlite3
3161f 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
31620 28 64 62 2c 20 7a 54 65 73 74 32 2c 20 30 2c 20  (db, zTest2, 0, 
31621 61 6e 79 2c 20 70 64 62 2c 20 69 6e 74 54 65 73  any, pdb, intTes
31622 74 46 75 6e 63 2c 20 30 2c 20 30 29 29 0a 23 65  tFunc, 0, 0)).#e
31623 6e 64 69 66 0a 20 20 29 3b 0a 0a 20 20 73 71 6c  ndif.  );..  sql
31624 69 74 65 33 5f 66 72 65 65 28 7a 54 65 73 74 29  ite3_free(zTest)
31625 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65  ;.  sqlite3_free
31626 28 7a 54 65 73 74 32 29 3b 0a 20 20 72 65 74 75  (zTest2);.  retu
31627 72 6e 20 72 63 3b 0a 7d 0a 0a 23 65 6e 64 69 66  rn rc;.}..#endif
31628 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51 4c   /* !defined(SQL
31629 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65 66  ITE_CORE) || def
3162a 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41 42  ined(SQLITE_ENAB
3162b 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a 2a  LE_FTS3) */../**
3162c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64  ************ End
3162d 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69 7a   of fts3_tokeniz
3162e 65 72 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  er.c ***********
3162f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31630 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a  ***********/./**
31631 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67  ************ Beg
31632 69 6e 20 66 69 6c 65 20 66 74 73 33 5f 74 6f 6b  in file fts3_tok
31633 65 6e 69 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a  enizer1.c ******
31634 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
31635 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a  ***********/./*.
31636 2a 2a 20 32 30 30 36 20 4f 63 74 20 31 30 0a 2a  ** 2006 Oct 10.*
31637 2a 0a 2a 2a 20 54 68 65 20 61 75 74 68 6f 72 20  *.** The author 
31638 64 69 73 63 6c 61 69 6d 73 20 63 6f 70 79 72 69  disclaims copyri
31639 67 68 74 20 74 6f 20 74 68 69 73 20 73 6f 75 72  ght to this sour
3163a 63 65 20 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61  ce code.  In pla
3163b 63 65 20 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c  ce of.** a legal
3163c 20 6e 6f 74 69 63 65 2c 20 68 65 72 65 20 69 73   notice, here is
3163d 20 61 20 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a   a blessing:.**.
3163e 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 64 6f  **    May you do
3163f 20 67 6f 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76   good and not ev
31640 69 6c 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  il..**    May yo
31641 75 20 66 69 6e 64 20 66 6f 72 67 69 76 65 6e 65  u find forgivene
31642 73 73 20 66 6f 72 20 79 6f 75 72 73 65 6c 66 20  ss for yourself 
31643 61 6e 64 20 66 6f 72 67 69 76 65 20 6f 74 68 65  and forgive othe
31644 72 73 2e 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f  rs..**    May yo
31645 75 20 73 68 61 72 65 20 66 72 65 65 6c 79 2c 20  u share freely, 
31646 6e 65 76 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72  never taking mor
31647 65 20 74 68 61 6e 20 79 6f 75 20 67 69 76 65 2e  e than you give.
31648 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .**.************
31649 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3164a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3164b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3164c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3164d 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65  **.**.** Impleme
3164e 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 22  ntation of the "
3164f 73 69 6d 70 6c 65 22 20 66 75 6c 6c 2d 74 65 78  simple" full-tex
31650 74 2d 73 65 61 72 63 68 20 74 6f 6b 65 6e 69 7a  t-search tokeniz
31651 65 72 2e 0a 2a 2f 0a 0a 2f 2a 0a 2a 2a 20 54 68  er..*/../*.** Th
31652 65 20 63 6f 64 65 20 69 6e 20 74 68 69 73 20 66  e code in this f
31653 69 6c 65 20 69 73 20 6f 6e 6c 79 20 63 6f 6d 70  ile is only comp
31654 69 6c 65 64 20 69 66 3a 0a 2a 2a 0a 2a 2a 20 20  iled if:.**.**  
31655 20 20 20 2a 20 54 68 65 20 46 54 53 33 20 6d 6f     * The FTS3 mo
31656 64 75 6c 65 20 69 73 20 62 65 69 6e 67 20 62 75  dule is being bu
31657 69 6c 74 20 61 73 20 61 6e 20 65 78 74 65 6e 73  ilt as an extens
31658 69 6f 6e 0a 2a 2a 20 20 20 20 20 20 20 28 69 6e  ion.**       (in
31659 20 77 68 69 63 68 20 63 61 73 65 20 53 51 4c 49   which case SQLI
3165a 54 45 5f 43 4f 52 45 20 69 73 20 6e 6f 74 20 64  TE_CORE is not d
3165b 65 66 69 6e 65 64 29 2c 20 6f 72 0a 2a 2a 0a 2a  efined), or.**.*
3165c 2a 20 20 20 20 20 2a 20 54 68 65 20 46 54 53 33  *     * The FTS3
3165d 20 6d 6f 64 75 6c 65 20 69 73 20 62 65 69 6e 67   module is being
3165e 20 62 75 69 6c 74 20 69 6e 74 6f 20 74 68 65 20   built into the 
3165f 63 6f 72 65 20 6f 66 0a 2a 2a 20 20 20 20 20 20  core of.**      
31660 20 53 51 4c 69 74 65 20 28 69 6e 20 77 68 69 63   SQLite (in whic
31661 68 20 63 61 73 65 20 53 51 4c 49 54 45 5f 45 4e  h case SQLITE_EN
31662 41 42 4c 45 5f 46 54 53 33 20 69 73 20 64 65 66  ABLE_FTS3 is def
31663 69 6e 65 64 29 2e 0a 2a 2f 0a 23 69 66 20 21 64  ined)..*/.#if !d
31664 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f  efined(SQLITE_CO
31665 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53  RE) || defined(S
31666 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53  QLITE_ENABLE_FTS
31667 33 29 0a 0a 0a 0a 0a 74 79 70 65 64 65 66 20 73  3).....typedef s
31668 74 72 75 63 74 20 73 69 6d 70 6c 65 5f 74 6f 6b  truct simple_tok
31669 65 6e 69 7a 65 72 20 7b 0a 20 20 73 71 6c 69 74  enizer {.  sqlit
3166a 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20 62 61 73  e3_tokenizer bas
3166b 65 3b 0a 20 20 63 68 61 72 20 64 65 6c 69 6d 5b  e;.  char delim[
3166c 31 32 38 5d 3b 20 20 20 20 20 20 20 20 20 20 20  128];           
3166d 20 20 2f 2a 20 66 6c 61 67 20 41 53 43 49 49 20    /* flag ASCII 
3166e 64 65 6c 69 6d 69 74 65 72 73 20 2a 2f 0a 7d 20  delimiters */.} 
3166f 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
31670 3b 0a 0a 74 79 70 65 64 65 66 20 73 74 72 75 63  ;..typedef struc
31671 74 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a  t simple_tokeniz
31672 65 72 5f 63 75 72 73 6f 72 20 7b 0a 20 20 73 71  er_cursor {.  sq
31673 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f  lite3_tokenizer_
31674 63 75 72 73 6f 72 20 62 61 73 65 3b 0a 20 20 63  cursor base;.  c
31675 6f 6e 73 74 20 63 68 61 72 20 2a 70 49 6e 70 75  onst char *pInpu
31676 74 3b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 69  t;          /* i
31677 6e 70 75 74 20 77 65 20 61 72 65 20 74 6f 6b 65  nput we are toke
31678 6e 69 7a 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20  nizing */.  int 
31679 6e 42 79 74 65 73 3b 20 20 20 20 20 20 20 20 20  nBytes;         
3167a 20 20 20 20 20 20 20 20 20 2f 2a 20 73 69 7a 65           /* size
3167b 20 6f 66 20 74 68 65 20 69 6e 70 75 74 20 2a 2f   of the input */
3167c 0a 20 20 69 6e 74 20 69 4f 66 66 73 65 74 3b 20  .  int iOffset; 
3167d 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3167e 2f 2a 20 63 75 72 72 65 6e 74 20 70 6f 73 69 74  /* current posit
3167f 69 6f 6e 20 69 6e 20 70 49 6e 70 75 74 20 2a 2f  ion in pInput */
31680 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 20 20  .  int iToken;  
31681 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31682 2f 2a 20 69 6e 64 65 78 20 6f 66 20 6e 65 78 74  /* index of next
31683 20 74 6f 6b 65 6e 20 74 6f 20 62 65 20 72 65 74   token to be ret
31684 75 72 6e 65 64 20 2a 2f 0a 20 20 63 68 61 72 20  urned */.  char 
31685 2a 70 54 6f 6b 65 6e 3b 20 20 20 20 20 20 20 20  *pToken;        
31686 20 20 20 20 20 20 20 20 2f 2a 20 73 74 6f 72 61          /* stora
31687 67 65 20 66 6f 72 20 63 75 72 72 65 6e 74 20 74  ge for current t
31688 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 6e 54  oken */.  int nT
31689 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 3b 20 20  okenAllocated;  
3168a 20 20 20 20 20 20 20 2f 2a 20 73 70 61 63 65 20         /* space 
3168b 61 6c 6c 6f 63 61 74 65 64 20 74 6f 20 7a 54 6f  allocated to zTo
3168c 6b 65 6e 20 62 75 66 66 65 72 20 2a 2f 0a 7d 20  ken buffer */.} 
3168d 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
3168e 5f 63 75 72 73 6f 72 3b 0a 0a 0a 2f 2a 20 46 6f  _cursor;.../* Fo
3168f 72 77 61 72 64 20 64 65 63 6c 61 72 61 74 69 6f  rward declaratio
31690 6e 20 2a 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73  n */.static cons
31691 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  t sqlite3_tokeni
31692 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69 6d 70 6c  zer_module simpl
31693 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75 6c 65  eTokenizerModule
31694 3b 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69  ;..static int si
31695 6d 70 6c 65 44 65 6c 69 6d 28 73 69 6d 70 6c 65  mpleDelim(simple
31696 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 74 2c 20 75  _tokenizer *t, u
31697 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 29 7b  nsigned char c){
31698 0a 20 20 72 65 74 75 72 6e 20 63 3c 30 78 38 30  .  return c<0x80
31699 20 26 26 20 74 2d 3e 64 65 6c 69 6d 5b 63 5d 3b   && t->delim[c];
3169a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  .}../*.** Create
3169b 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72   a new tokenizer
3169c 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74   instance..*/.st
3169d 61 74 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43  atic int simpleC
3169e 72 65 61 74 65 28 0a 20 20 69 6e 74 20 61 72 67  reate(.  int arg
3169f 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 20  c, const char * 
316a0 63 6f 6e 73 74 20 2a 61 72 67 76 2c 0a 20 20 73  const *argv,.  s
316a1 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
316a2 20 2a 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 0a 29   **ppTokenizer.)
316a3 7b 0a 20 20 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e  {.  simple_token
316a4 69 7a 65 72 20 2a 74 3b 0a 0a 20 20 74 20 3d 20  izer *t;..  t = 
316a5 28 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65  (simple_tokenize
316a6 72 20 2a 29 20 73 71 6c 69 74 65 33 5f 6d 61 6c  r *) sqlite3_mal
316a7 6c 6f 63 28 73 69 7a 65 6f 66 28 2a 74 29 29 3b  loc(sizeof(*t));
316a8 0a 20 20 69 66 28 20 74 3d 3d 4e 55 4c 4c 20 29  .  if( t==NULL )
316a9 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
316aa 4f 4d 45 4d 3b 0a 20 20 6d 65 6d 73 65 74 28 74  OMEM;.  memset(t
316ab 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 74 29 29  , 0, sizeof(*t))
316ac 3b 0a 0a 20 20 2f 2a 20 54 4f 44 4f 28 73 68 65  ;..  /* TODO(she
316ad 73 73 29 20 44 65 6c 69 6d 69 74 65 72 73 20 6e  ss) Delimiters n
316ae 65 65 64 20 74 6f 20 72 65 6d 61 69 6e 20 74 68  eed to remain th
316af 65 20 73 61 6d 65 20 66 72 6f 6d 20 72 75 6e 20  e same from run 
316b0 74 6f 20 72 75 6e 2c 0a 20 20 2a 2a 20 65 6c 73  to run,.  ** els
316b1 65 20 77 65 20 6e 65 65 64 20 74 6f 20 72 65 69  e we need to rei
316b2 6e 64 65 78 2e 20 20 4f 6e 65 20 73 6f 6c 75 74  ndex.  One solut
316b3 69 6f 6e 20 77 6f 75 6c 64 20 62 65 20 61 20 6d  ion would be a m
316b4 65 74 61 2d 74 61 62 6c 65 20 74 6f 0a 20 20 2a  eta-table to.  *
316b5 2a 20 74 72 61 63 6b 20 73 75 63 68 20 69 6e 66  * track such inf
316b6 6f 72 6d 61 74 69 6f 6e 20 69 6e 20 74 68 65 20  ormation in the 
316b7 64 61 74 61 62 61 73 65 2c 20 74 68 65 6e 20 77  database, then w
316b8 65 27 64 20 6f 6e 6c 79 20 77 61 6e 74 20 74 68  e'd only want th
316b9 69 73 0a 20 20 2a 2a 20 69 6e 66 6f 72 6d 61 74  is.  ** informat
316ba 69 6f 6e 20 6f 6e 20 74 68 65 20 69 6e 69 74 69  ion on the initi
316bb 61 6c 20 63 72 65 61 74 65 2e 0a 20 20 2a 2f 0a  al create..  */.
316bc 20 20 69 66 28 20 61 72 67 63 3e 31 20 29 7b 0a    if( argc>1 ){.
316bd 20 20 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 73      int i, n = s
316be 74 72 6c 65 6e 28 61 72 67 76 5b 31 5d 29 3b 0a  trlen(argv[1]);.
316bf 20 20 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e      for(i=0; i<n
316c0 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 20 20 75 6e  ; i++){.      un
316c1 73 69 67 6e 65 64 20 63 68 61 72 20 63 68 20 3d  signed char ch =
316c2 20 61 72 67 76 5b 31 5d 5b 69 5d 3b 0a 20 20 20   argv[1][i];.   
316c3 20 20 20 2f 2a 20 57 65 20 65 78 70 6c 69 63 69     /* We explici
316c4 74 6c 79 20 64 6f 6e 27 74 20 73 75 70 70 6f 72  tly don't suppor
316c5 74 20 55 54 46 2d 38 20 64 65 6c 69 6d 69 74 65  t UTF-8 delimite
316c6 72 73 20 66 6f 72 20 6e 6f 77 2e 20 2a 2f 0a 20  rs for now. */. 
316c7 20 20 20 20 20 69 66 28 20 63 68 3e 3d 30 78 38       if( ch>=0x8
316c8 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73 71 6c  0 ){.        sql
316c9 69 74 65 33 5f 66 72 65 65 28 74 29 3b 0a 20 20  ite3_free(t);.  
316ca 20 20 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c        return SQL
316cb 49 54 45 5f 45 52 52 4f 52 3b 0a 20 20 20 20 20  ITE_ERROR;.     
316cc 20 7d 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69   }.      t->deli
316cd 6d 5b 63 68 5d 20 3d 20 31 3b 0a 20 20 20 20 7d  m[ch] = 1;.    }
316ce 0a 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20  .  } else {.    
316cf 2f 2a 20 4d 61 72 6b 20 6e 6f 6e 2d 61 6c 70 68  /* Mark non-alph
316d0 61 6e 75 6d 65 72 69 63 20 41 53 43 49 49 20 63  anumeric ASCII c
316d1 68 61 72 61 63 74 65 72 73 20 61 73 20 64 65 6c  haracters as del
316d2 69 6d 69 74 65 72 73 20 2a 2f 0a 20 20 20 20 69  imiters */.    i
316d3 6e 74 20 69 3b 0a 20 20 20 20 66 6f 72 28 69 3d  nt i;.    for(i=
316d4 31 3b 20 69 3c 30 78 38 30 3b 20 69 2b 2b 29 7b  1; i<0x80; i++){
316d5 0a 20 20 20 20 20 20 74 2d 3e 64 65 6c 69 6d 5b  .      t->delim[
316d6 69 5d 20 3d 20 21 69 73 61 6c 6e 75 6d 28 69 29  i] = !isalnum(i)
316d7 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 2a  ;.    }.  }..  *
316d8 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d 20 26 74  ppTokenizer = &t
316d9 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75 72 6e  ->base;.  return
316da 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
316db 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 74  *.** Destroy a t
316dc 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74 61 74  okenizer.*/.stat
316dd 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 44 65 73  ic int simpleDes
316de 74 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b  troy(sqlite3_tok
316df 65 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a  enizer *pTokeniz
316e0 65 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 66  er){.  sqlite3_f
316e1 72 65 65 28 70 54 6f 6b 65 6e 69 7a 65 72 29 3b  ree(pTokenizer);
316e2 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  .  return SQLITE
316e3 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 50 72  _OK;.}../*.** Pr
316e4 65 70 61 72 65 20 74 6f 20 62 65 67 69 6e 20 74  epare to begin t
316e5 6f 6b 65 6e 69 7a 69 6e 67 20 61 20 70 61 72 74  okenizing a part
316e6 69 63 75 6c 61 72 20 73 74 72 69 6e 67 2e 20 20  icular string.  
316e7 54 68 65 20 69 6e 70 75 74 0a 2a 2a 20 73 74 72  The input.** str
316e8 69 6e 67 20 74 6f 20 62 65 20 74 6f 6b 65 6e 69  ing to be tokeni
316e9 7a 65 64 20 69 73 20 70 49 6e 70 75 74 5b 30 2e  zed is pInput[0.
316ea 2e 6e 42 79 74 65 73 2d 31 5d 2e 20 20 41 20 63  .nBytes-1].  A c
316eb 75 72 73 6f 72 0a 2a 2a 20 75 73 65 64 20 74 6f  ursor.** used to
316ec 20 69 6e 63 72 65 6d 65 6e 74 61 6c 6c 79 20 74   incrementally t
316ed 6f 6b 65 6e 69 7a 65 20 74 68 69 73 20 73 74 72  okenize this str
316ee 69 6e 67 20 69 73 20 72 65 74 75 72 6e 65 64 20  ing is returned 
316ef 69 6e 20 0a 2a 2a 20 2a 70 70 43 75 72 73 6f 72  in .** *ppCursor
316f0 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
316f1 73 69 6d 70 6c 65 4f 70 65 6e 28 0a 20 20 73 71  simpleOpen(.  sq
316f2 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 20  lite3_tokenizer 
316f3 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20 20 20 20  *pTokenizer,    
316f4 20 20 20 20 20 2f 2a 20 54 68 65 20 74 6f 6b 65       /* The toke
316f5 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f 6e 73 74  nizer */.  const
316f6 20 63 68 61 72 20 2a 70 49 6e 70 75 74 2c 20 69   char *pInput, i
316f7 6e 74 20 6e 42 79 74 65 73 2c 20 20 20 20 20 20  nt nBytes,      
316f8 20 20 2f 2a 20 53 74 72 69 6e 67 20 74 6f 20 62    /* String to b
316f9 65 20 74 6f 6b 65 6e 69 7a 65 64 20 2a 2f 0a 20  e tokenized */. 
316fa 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
316fb 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70 43 75  er_cursor **ppCu
316fc 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54 3a 20  rsor    /* OUT: 
316fd 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72  Tokenization cur
316fe 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70  sor */.){.  simp
316ff 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  le_tokenizer_cur
31700 73 6f 72 20 2a 63 3b 0a 0a 20 20 63 20 3d 20 28  sor *c;..  c = (
31701 73 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72  simple_tokenizer
31702 5f 63 75 72 73 6f 72 20 2a 29 20 73 71 6c 69 74  _cursor *) sqlit
31703 65 33 5f 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66  e3_malloc(sizeof
31704 28 2a 63 29 29 3b 0a 20 20 69 66 28 20 63 3d 3d  (*c));.  if( c==
31705 4e 55 4c 4c 20 29 20 72 65 74 75 72 6e 20 53 51  NULL ) return SQ
31706 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 0a 20 20 63  LITE_NOMEM;..  c
31707 2d 3e 70 49 6e 70 75 74 20 3d 20 70 49 6e 70 75  ->pInput = pInpu
31708 74 3b 0a 20 20 69 66 28 20 70 49 6e 70 75 74 3d  t;.  if( pInput=
31709 3d 30 20 29 7b 0a 20 20 20 20 63 2d 3e 6e 42 79  =0 ){.    c->nBy
3170a 74 65 73 20 3d 20 30 3b 0a 20 20 7d 65 6c 73 65  tes = 0;.  }else
3170b 20 69 66 28 20 6e 42 79 74 65 73 3c 30 20 29 7b   if( nBytes<0 ){
3170c 0a 20 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d  .    c->nBytes =
3170d 20 28 69 6e 74 29 73 74 72 6c 65 6e 28 70 49 6e   (int)strlen(pIn
3170e 70 75 74 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  put);.  }else{. 
3170f 20 20 20 63 2d 3e 6e 42 79 74 65 73 20 3d 20 6e     c->nBytes = n
31710 42 79 74 65 73 3b 0a 20 20 7d 0a 20 20 63 2d 3e  Bytes;.  }.  c->
31711 69 4f 66 66 73 65 74 20 3d 20 30 3b 20 20 20 20  iOffset = 0;    
31712 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31713 73 74 61 72 74 20 74 6f 6b 65 6e 69 7a 69 6e 67  start tokenizing
31714 20 61 74 20 74 68 65 20 62 65 67 69 6e 6e 69 6e   at the beginnin
31715 67 20 2a 2f 0a 20 20 63 2d 3e 69 54 6f 6b 65 6e  g */.  c->iToken
31716 20 3d 20 30 3b 0a 20 20 63 2d 3e 70 54 6f 6b 65   = 0;.  c->pToke
31717 6e 20 3d 20 4e 55 4c 4c 3b 20 20 20 20 20 20 20  n = NULL;       
31718 20 20 20 20 20 20 20 20 2f 2a 20 6e 6f 20 73 70          /* no sp
31719 61 63 65 20 61 6c 6c 6f 63 61 74 65 64 2c 20 79  ace allocated, y
3171a 65 74 2e 20 2a 2f 0a 20 20 63 2d 3e 6e 54 6f 6b  et. */.  c->nTok
3171b 65 6e 41 6c 6c 6f 63 61 74 65 64 20 3d 20 30 3b  enAllocated = 0;
3171c 0a 0a 20 20 2a 70 70 43 75 72 73 6f 72 20 3d 20  ..  *ppCursor = 
3171d 26 63 2d 3e 62 61 73 65 3b 0a 20 20 72 65 74 75  &c->base;.  retu
3171e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3171f 0a 2f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74  ./*.** Close a t
31720 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73  okenization curs
31721 6f 72 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70  or previously op
31722 65 6e 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74  ened by a call t
31723 6f 0a 2a 2a 20 73 69 6d 70 6c 65 4f 70 65 6e 28  o.** simpleOpen(
31724 29 20 61 62 6f 76 65 2e 0a 2a 2f 0a 73 74 61 74  ) above..*/.stat
31725 69 63 20 69 6e 74 20 73 69 6d 70 6c 65 43 6c 6f  ic int simpleClo
31726 73 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  se(sqlite3_token
31727 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75  izer_cursor *pCu
31728 72 73 6f 72 29 7b 0a 20 20 73 69 6d 70 6c 65 5f  rsor){.  simple_
31729 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3172a 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f   *c = (simple_to
3172b 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a  kenizer_cursor *
3172c 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73 71 6c  ) pCursor;.  sql
3172d 69 74 65 33 5f 66 72 65 65 28 63 2d 3e 70 54 6f  ite3_free(c->pTo
3172e 6b 65 6e 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  ken);.  sqlite3_
3172f 66 72 65 65 28 63 29 3b 0a 20 20 72 65 74 75 72  free(c);.  retur
31730 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a  n SQLITE_OK;.}..
31731 2f 2a 0a 2a 2a 20 45 78 74 72 61 63 74 20 74 68  /*.** Extract th
31732 65 20 6e 65 78 74 20 74 6f 6b 65 6e 20 66 72 6f  e next token fro
31733 6d 20 61 20 74 6f 6b 65 6e 69 7a 61 74 69 6f 6e  m a tokenization
31734 20 63 75 72 73 6f 72 2e 20 20 54 68 65 20 63 75   cursor.  The cu
31735 72 73 6f 72 20 6d 75 73 74 0a 2a 2a 20 68 61 76  rsor must.** hav
31736 65 20 62 65 65 6e 20 6f 70 65 6e 65 64 20 62 79  e been opened by
31737 20 61 20 70 72 69 6f 72 20 63 61 6c 6c 20 74 6f   a prior call to
31738 20 73 69 6d 70 6c 65 4f 70 65 6e 28 29 2e 0a 2a   simpleOpen()..*
31739 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 73 69 6d  /.static int sim
3173a 70 6c 65 4e 65 78 74 28 0a 20 20 73 71 6c 69 74  pleNext(.  sqlit
3173b 65 33 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72  e3_tokenizer_cur
3173c 73 6f 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f  sor *pCursor,  /
3173d 2a 20 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65  * Cursor returne
3173e 64 20 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20  d by simpleOpen 
3173f 2a 2f 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  */.  const char 
31740 2a 2a 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20  **ppToken,      
31741 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
31742 20 2a 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65   *ppToken is the
31743 20 74 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20   token text */. 
31744 20 69 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20   int *pnBytes,  
31745 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31746 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d       /* OUT: Num
31747 62 65 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20  ber of bytes in 
31748 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
31749 70 69 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20  piStartOffset,  
3174a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3174b 2a 20 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20  * OUT: Starting 
3174c 6f 66 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20  offset of token 
3174d 2a 2f 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f  */.  int *piEndO
3174e 66 66 73 65 74 2c 20 20 20 20 20 20 20 20 20 20  ffset,          
3174f 20 20 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a           /* OUT:
31750 20 45 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f   Ending offset o
31751 66 20 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74  f token */.  int
31752 20 2a 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20   *piPosition    
31753 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31754 20 2f 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f   /* OUT: Positio
31755 6e 20 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b  n integer of tok
31756 65 6e 20 2a 2f 0a 29 7b 0a 20 20 73 69 6d 70 6c  en */.){.  simpl
31757 65 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73  e_tokenizer_curs
31758 6f 72 20 2a 63 20 3d 20 28 73 69 6d 70 6c 65 5f  or *c = (simple_
31759 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72  tokenizer_cursor
3175a 20 2a 29 20 70 43 75 72 73 6f 72 3b 0a 20 20 73   *) pCursor;.  s
3175b 69 6d 70 6c 65 5f 74 6f 6b 65 6e 69 7a 65 72 20  imple_tokenizer 
3175c 2a 74 20 3d 20 28 73 69 6d 70 6c 65 5f 74 6f 6b  *t = (simple_tok
3175d 65 6e 69 7a 65 72 20 2a 29 20 70 43 75 72 73 6f  enizer *) pCurso
3175e 72 2d 3e 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20  r->pTokenizer;. 
3175f 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
31760 70 20 3d 20 28 75 6e 73 69 67 6e 65 64 20 63 68  p = (unsigned ch
31761 61 72 20 2a 29 63 2d 3e 70 49 6e 70 75 74 3b 0a  ar *)c->pInput;.
31762 0a 20 20 77 68 69 6c 65 28 20 63 2d 3e 69 4f 66  .  while( c->iOf
31763 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65 73 20 29  fset<c->nBytes )
31764 7b 0a 20 20 20 20 69 6e 74 20 69 53 74 61 72 74  {.    int iStart
31765 4f 66 66 73 65 74 3b 0a 0a 20 20 20 20 2f 2a 20  Offset;..    /* 
31766 53 63 61 6e 20 70 61 73 74 20 64 65 6c 69 6d 69  Scan past delimi
31767 74 65 72 20 63 68 61 72 61 63 74 65 72 73 20 2a  ter characters *
31768 2f 0a 20 20 20 20 77 68 69 6c 65 28 20 63 2d 3e  /.    while( c->
31769 69 4f 66 66 73 65 74 3c 63 2d 3e 6e 42 79 74 65  iOffset<c->nByte
3176a 73 20 26 26 20 73 69 6d 70 6c 65 44 65 6c 69 6d  s && simpleDelim
3176b 28 74 2c 20 70 5b 63 2d 3e 69 4f 66 66 73 65 74  (t, p[c->iOffset
3176c 5d 29 20 29 7b 0a 20 20 20 20 20 20 63 2d 3e 69  ]) ){.      c->i
3176d 4f 66 66 73 65 74 2b 2b 3b 0a 20 20 20 20 7d 0a  Offset++;.    }.
3176e 0a 20 20 20 20 2f 2a 20 43 6f 75 6e 74 20 6e 6f  .    /* Count no
3176f 6e 2d 64 65 6c 69 6d 69 74 65 72 20 63 68 61 72  n-delimiter char
31770 61 63 74 65 72 73 2e 20 2a 2f 0a 20 20 20 20 69  acters. */.    i
31771 53 74 61 72 74 4f 66 66 73 65 74 20 3d 20 63 2d  StartOffset = c-
31772 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 77 68  >iOffset;.    wh
31773 69 6c 65 28 20 63 2d 3e 69 4f 66 66 73 65 74 3c  ile( c->iOffset<
31774 63 2d 3e 6e 42 79 74 65 73 20 26 26 20 21 73 69  c->nBytes && !si
31775 6d 70 6c 65 44 65 6c 69 6d 28 74 2c 20 70 5b 63  mpleDelim(t, p[c
31776 2d 3e 69 4f 66 66 73 65 74 5d 29 20 29 7b 0a 20  ->iOffset]) ){. 
31777 20 20 20 20 20 63 2d 3e 69 4f 66 66 73 65 74 2b       c->iOffset+
31778 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  +;.    }..    if
31779 28 20 63 2d 3e 69 4f 66 66 73 65 74 3e 69 53 74  ( c->iOffset>iSt
3177a 61 72 74 4f 66 66 73 65 74 20 29 7b 0a 20 20 20  artOffset ){.   
3177b 20 20 20 69 6e 74 20 69 2c 20 6e 20 3d 20 63 2d     int i, n = c-
3177c 3e 69 4f 66 66 73 65 74 2d 69 53 74 61 72 74 4f  >iOffset-iStartO
3177d 66 66 73 65 74 3b 0a 20 20 20 20 20 20 69 66 28  ffset;.      if(
3177e 20 6e 3e 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f   n>c->nTokenAllo
3177f 63 61 74 65 64 20 29 7b 0a 20 20 20 20 20 20 20  cated ){.       
31780 20 63 2d 3e 6e 54 6f 6b 65 6e 41 6c 6c 6f 63 61   c->nTokenAlloca
31781 74 65 64 20 3d 20 6e 2b 32 30 3b 0a 20 20 20 20  ted = n+20;.    
31782 20 20 20 20 63 2d 3e 70 54 6f 6b 65 6e 20 3d 20      c->pToken = 
31783 73 71 6c 69 74 65 33 5f 72 65 61 6c 6c 6f 63 28  sqlite3_realloc(
31784 63 2d 3e 70 54 6f 6b 65 6e 2c 20 63 2d 3e 6e 54  c->pToken, c->nT
31785 6f 6b 65 6e 41 6c 6c 6f 63 61 74 65 64 29 3b 0a  okenAllocated);.
31786 20 20 20 20 20 20 20 20 69 66 28 20 63 2d 3e 70          if( c->p
31787 54 6f 6b 65 6e 3d 3d 4e 55 4c 4c 20 29 20 72 65  Token==NULL ) re
31788 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
31789 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
3178a 20 66 6f 72 28 69 3d 30 3b 20 69 3c 6e 3b 20 69   for(i=0; i<n; i
3178b 2b 2b 29 7b 0a 20 20 20 20 20 20 20 20 2f 2a 20  ++){.        /* 
3178c 54 4f 44 4f 28 73 68 65 73 73 29 20 54 68 69 73  TODO(shess) This
3178d 20 6e 65 65 64 73 20 65 78 70 61 6e 73 69 6f 6e   needs expansion
3178e 20 74 6f 20 68 61 6e 64 6c 65 20 55 54 46 2d 38   to handle UTF-8
3178f 0a 20 20 20 20 20 20 20 20 2a 2a 20 63 61 73 65  .        ** case
31790 2d 69 6e 73 65 6e 73 69 74 69 76 69 74 79 2e 0a  -insensitivity..
31791 20 20 20 20 20 20 20 20 2a 2f 0a 20 20 20 20 20          */.     
31792 20 20 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72     unsigned char
31793 20 63 68 20 3d 20 70 5b 69 53 74 61 72 74 4f 66   ch = p[iStartOf
31794 66 73 65 74 2b 69 5d 3b 0a 20 20 20 20 20 20 20  fset+i];.       
31795 20 63 2d 3e 70 54 6f 6b 65 6e 5b 69 5d 20 3d 20   c->pToken[i] = 
31796 63 68 3c 30 78 38 30 20 3f 20 74 6f 6c 6f 77 65  ch<0x80 ? tolowe
31797 72 28 63 68 29 20 3a 20 63 68 3b 0a 20 20 20 20  r(ch) : ch;.    
31798 20 20 7d 0a 20 20 20 20 20 20 2a 70 70 54 6f 6b    }.      *ppTok
31799 65 6e 20 3d 20 63 2d 3e 70 54 6f 6b 65 6e 3b 0a  en = c->pToken;.
3179a 20 20 20 20 20 20 2a 70 6e 42 79 74 65 73 20 3d        *pnBytes =
3179b 20 6e 3b 0a 20 20 20 20 20 20 2a 70 69 53 74 61   n;.      *piSta
3179c 72 74 4f 66 66 73 65 74 20 3d 20 69 53 74 61 72  rtOffset = iStar
3179d 74 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20 2a  tOffset;.      *
3179e 70 69 45 6e 64 4f 66 66 73 65 74 20 3d 20 63 2d  piEndOffset = c-
3179f 3e 69 4f 66 66 73 65 74 3b 0a 20 20 20 20 20 20  >iOffset;.      
317a0 2a 70 69 50 6f 73 69 74 69 6f 6e 20 3d 20 63 2d  *piPosition = c-
317a1 3e 69 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 20 20  >iToken++;..    
317a2 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
317a3 4f 4b 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20  OK;.    }.  }.  
317a4 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 44 4f  return SQLITE_DO
317a5 4e 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  NE;.}../*.** The
317a6 20 73 65 74 20 6f 66 20 72 6f 75 74 69 6e 65 73   set of routines
317a7 20 74 68 61 74 20 69 6d 70 6c 65 6d 65 6e 74 20   that implement 
317a8 74 68 65 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e  the simple token
317a9 69 7a 65 72 0a 2a 2f 0a 73 74 61 74 69 63 20 63  izer.*/.static c
317aa 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 74 6f 6b  onst sqlite3_tok
317ab 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 73 69  enizer_module si
317ac 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64  mpleTokenizerMod
317ad 75 6c 65 20 3d 20 7b 0a 20 20 30 2c 0a 20 20 73  ule = {.  0,.  s
317ae 69 6d 70 6c 65 43 72 65 61 74 65 2c 0a 20 20 73  impleCreate,.  s
317af 69 6d 70 6c 65 44 65 73 74 72 6f 79 2c 0a 20 20  impleDestroy,.  
317b0 73 69 6d 70 6c 65 4f 70 65 6e 2c 0a 20 20 73 69  simpleOpen,.  si
317b1 6d 70 6c 65 43 6c 6f 73 65 2c 0a 20 20 73 69 6d  mpleClose,.  sim
317b2 70 6c 65 4e 65 78 74 2c 0a 7d 3b 0a 0a 2f 2a 0a  pleNext,.};../*.
317b3 2a 2a 20 41 6c 6c 6f 63 61 74 65 20 61 20 6e 65  ** Allocate a ne
317b4 77 20 73 69 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a  w simple tokeniz
317b5 65 72 2e 20 20 52 65 74 75 72 6e 20 61 20 70 6f  er.  Return a po
317b6 69 6e 74 65 72 20 74 6f 20 74 68 65 20 6e 65 77  inter to the new
317b7 0a 2a 2a 20 74 6f 6b 65 6e 69 7a 65 72 20 69 6e  .** tokenizer in
317b8 20 2a 70 70 4d 6f 64 75 6c 65 0a 2a 2f 0a 53 51   *ppModule.*/.SQ
317b9 4c 49 54 45 5f 50 52 49 56 41 54 45 20 76 6f 69  LITE_PRIVATE voi
317ba 64 20 73 71 6c 69 74 65 33 46 74 73 33 53 69 6d  d sqlite3Fts3Sim
317bb 70 6c 65 54 6f 6b 65 6e 69 7a 65 72 4d 6f 64 75  pleTokenizerModu
317bc 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  le(.  sqlite3_to
317bd 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65 20 63  kenizer_module c
317be 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65 0a 29  onst**ppModule.)
317bf 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20 3d 20  {.  *ppModule = 
317c0 26 73 69 6d 70 6c 65 54 6f 6b 65 6e 69 7a 65 72  &simpleTokenizer
317c1 4d 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69  Module;.}..#endi
317c2 66 20 2f 2a 20 21 64 65 66 69 6e 65 64 28 53 51  f /* !defined(SQ
317c3 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20 64 65  LITE_CORE) || de
317c4 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45 4e 41  fined(SQLITE_ENA
317c5 42 4c 45 5f 46 54 53 33 29 20 2a 2f 0a 0a 2f 2a  BLE_FTS3) */../*
317c6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e  ************* En
317c7 64 20 6f 66 20 66 74 73 33 5f 74 6f 6b 65 6e 69  d of fts3_tokeni
317c8 7a 65 72 31 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  zer1.c *********
317c9 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317ca 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
317cb 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65  ************* Be
317cc 67 69 6e 20 66 69 6c 65 20 72 74 72 65 65 2e 63  gin file rtree.c
317cd 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   ***************
317ce 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317cf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a  ************/./*
317d0 0a 2a 2a 20 32 30 30 31 20 53 65 70 74 65 6d 62  .** 2001 Septemb
317d1 65 72 20 31 35 0a 2a 2a 0a 2a 2a 20 54 68 65 20  er 15.**.** The 
317d2 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
317d3 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
317d4 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
317d5 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
317d6 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
317d7 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
317d8 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
317d9 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
317da 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
317db 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
317dc 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
317dd 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
317de 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
317df 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
317e0 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
317e1 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
317e2 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
317e3 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317e4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317e5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317e6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
317e7 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
317e8 65 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20  e contains code 
317e9 66 6f 72 20 69 6d 70 6c 65 6d 65 6e 74 61 74 69  for implementati
317ea 6f 6e 73 20 6f 66 20 74 68 65 20 72 2d 74 72 65  ons of the r-tre
317eb 65 20 61 6e 64 20 72 2a 2d 74 72 65 65 0a 2a 2a  e and r*-tree.**
317ec 20 61 6c 67 6f 72 69 74 68 6d 73 20 70 61 63 6b   algorithms pack
317ed 61 67 65 64 20 61 73 20 61 6e 20 53 51 4c 69 74  aged as an SQLit
317ee 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
317ef 6d 6f 64 75 6c 65 2e 0a 2a 2a 0a 2a 2a 20 24 49  module..**.** $I
317f0 64 3a 20 72 74 72 65 65 2e 63 2c 76 20 31 2e 31  d: rtree.c,v 1.1
317f1 31 20 32 30 30 38 2f 31 31 2f 31 32 20 31 35 3a  1 2008/11/12 15:
317f2 32 34 3a 32 37 20 64 72 68 20 45 78 70 20 24 0a  24:27 drh Exp $.
317f3 2a 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64  */..#if !defined
317f4 28 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c  (SQLITE_CORE) ||
317f5 20 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f   defined(SQLITE_
317f6 45 4e 41 42 4c 45 5f 52 54 52 45 45 29 0a 0a 2f  ENABLE_RTREE)../
317f7 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 63  *.** This file c
317f8 6f 6e 74 61 69 6e 73 20 61 6e 20 69 6d 70 6c 65  ontains an imple
317f9 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 61 20 63  mentation of a c
317fa 6f 75 70 6c 65 20 6f 66 20 64 69 66 66 65 72 65  ouple of differe
317fb 6e 74 20 76 61 72 69 61 6e 74 73 0a 2a 2a 20 6f  nt variants.** o
317fc 66 20 74 68 65 20 72 2d 74 72 65 65 20 61 6c 67  f the r-tree alg
317fd 6f 72 69 74 68 6d 2e 20 53 65 65 20 74 68 65 20  orithm. See the 
317fe 52 45 41 44 4d 45 20 66 69 6c 65 20 66 6f 72 20  README file for 
317ff 66 75 72 74 68 65 72 20 64 65 74 61 69 6c 73 2e  further details.
31800 20 54 68 65 20 0a 2a 2a 20 73 61 6d 65 20 64 61   The .** same da
31801 74 61 2d 73 74 72 75 63 74 75 72 65 20 69 73 20  ta-structure is 
31802 75 73 65 64 20 66 6f 72 20 61 6c 6c 2c 20 62 75  used for all, bu
31803 74 20 74 68 65 20 61 6c 67 6f 72 69 74 68 6d 73  t the algorithms
31804 20 66 6f 72 20 69 6e 73 65 72 74 20 61 6e 64 0a   for insert and.
31805 2a 2a 20 64 65 6c 65 74 65 20 6f 70 65 72 61 74  ** delete operat
31806 69 6f 6e 73 20 76 61 72 79 2e 20 54 68 65 20 76  ions vary. The v
31807 61 72 69 61 6e 74 73 20 75 73 65 64 20 61 72 65  ariants used are
31808 20 73 65 6c 65 63 74 65 64 20 61 74 20 63 6f 6d   selected at com
31809 70 69 6c 65 20 74 69 6d 65 20 0a 2a 2a 20 62 79  pile time .** by
3180a 20 64 65 66 69 6e 69 6e 67 20 74 68 65 20 66 6f   defining the fo
3180b 6c 6c 6f 77 69 6e 67 20 73 79 6d 62 6f 6c 73 3a  llowing symbols:
3180c 0a 2a 2f 0a 0a 2f 2a 20 45 69 74 68 65 72 2c 20  .*/../* Either, 
3180d 62 6f 74 68 20 6f 72 20 6e 6f 6e 65 20 6f 66 20  both or none of 
3180e 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 6d 61  the following ma
3180f 79 20 62 65 20 73 65 74 20 74 6f 20 61 63 74 69  y be set to acti
31810 76 61 74 65 20 0a 2a 2a 20 72 2a 74 72 65 65 20  vate .** r*tree 
31811 76 61 72 69 61 6e 74 20 61 6c 67 6f 72 69 74 68  variant algorith
31812 6d 73 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56  ms..*/.#define V
31813 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45  ARIANT_RSTARTREE
31814 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45 20 30  _CHOOSESUBTREE 0
31815 0a 23 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54  .#define VARIANT
31816 5f 52 53 54 41 52 54 52 45 45 5f 52 45 49 4e 53  _RSTARTREE_REINS
31817 45 52 54 20 20 20 20 20 20 31 0a 0a 2f 2a 20 0a  ERT      1../* .
31818 2a 2a 20 45 78 61 63 74 6c 79 20 6f 6e 65 20 6f  ** Exactly one o
31819 66 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20  f the following 
3181a 6d 75 73 74 20 62 65 20 73 65 74 20 74 6f 20 31  must be set to 1
3181b 2e 0a 2a 2f 0a 23 64 65 66 69 6e 65 20 56 41 52  ..*/.#define VAR
3181c 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41  IANT_GUTTMAN_QUA
3181d 44 52 41 54 49 43 5f 53 50 4c 49 54 20 30 0a 23  DRATIC_SPLIT 0.#
3181e 64 65 66 69 6e 65 20 56 41 52 49 41 4e 54 5f 47  define VARIANT_G
3181f 55 54 54 4d 41 4e 5f 4c 49 4e 45 41 52 5f 53 50  UTTMAN_LINEAR_SP
31820 4c 49 54 20 20 20 20 30 0a 23 64 65 66 69 6e 65  LIT    0.#define
31821 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
31822 45 45 5f 53 50 4c 49 54 20 20 20 20 20 20 20 20  EE_SPLIT        
31823 20 31 0a 0a 23 64 65 66 69 6e 65 20 56 41 52 49   1..#define VARI
31824 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 53 50 4c 49  ANT_GUTTMAN_SPLI
31825 54 20 5c 0a 20 20 20 20 20 20 20 20 28 56 41 52  T \.        (VAR
31826 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e  IANT_GUTTMAN_LIN
31827 45 41 52 5f 53 50 4c 49 54 7c 7c 56 41 52 49 41  EAR_SPLIT||VARIA
31828 4e 54 5f 47 55 54 54 4d 41 4e 5f 51 55 41 44 52  NT_GUTTMAN_QUADR
31829 41 54 49 43 5f 53 50 4c 49 54 29 0a 0a 23 69 66  ATIC_SPLIT)..#if
3182a 20 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e   VARIANT_GUTTMAN
3182b 5f 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54  _QUADRATIC_SPLIT
3182c 0a 20 20 23 64 65 66 69 6e 65 20 50 69 63 6b 4e  .  #define PickN
3182d 65 78 74 20 51 75 61 64 72 61 74 69 63 50 69 63  ext QuadraticPic
3182e 6b 4e 65 78 74 0a 20 20 23 64 65 66 69 6e 65 20  kNext.  #define 
3182f 50 69 63 6b 53 65 65 64 73 20 51 75 61 64 72 61  PickSeeds Quadra
31830 74 69 63 50 69 63 6b 53 65 65 64 73 0a 20 20 23  ticPickSeeds.  #
31831 64 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c  define AssignCel
31832 6c 73 20 73 70 6c 69 74 4e 6f 64 65 47 75 74 74  ls splitNodeGutt
31833 6d 61 6e 0a 23 65 6e 64 69 66 0a 23 69 66 20 56  man.#endif.#if V
31834 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c  ARIANT_GUTTMAN_L
31835 49 4e 45 41 52 5f 53 50 4c 49 54 0a 20 20 23 64  INEAR_SPLIT.  #d
31836 65 66 69 6e 65 20 50 69 63 6b 4e 65 78 74 20 4c  efine PickNext L
31837 69 6e 65 61 72 50 69 63 6b 4e 65 78 74 0a 20 20  inearPickNext.  
31838 23 64 65 66 69 6e 65 20 50 69 63 6b 53 65 65 64  #define PickSeed
31839 73 20 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64  s LinearPickSeed
3183a 73 0a 20 20 23 64 65 66 69 6e 65 20 41 73 73 69  s.  #define Assi
3183b 67 6e 43 65 6c 6c 73 20 73 70 6c 69 74 4e 6f 64  gnCells splitNod
3183c 65 47 75 74 74 6d 61 6e 0a 23 65 6e 64 69 66 0a  eGuttman.#endif.
3183d 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54 41  #if VARIANT_RSTA
3183e 52 54 52 45 45 5f 53 50 4c 49 54 0a 20 20 23 64  RTREE_SPLIT.  #d
3183f 65 66 69 6e 65 20 41 73 73 69 67 6e 43 65 6c 6c  efine AssignCell
31840 73 20 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74  s splitNodeStart
31841 72 65 65 0a 23 65 6e 64 69 66 0a 0a 0a 23 69 66  ree.#endif...#if
31842 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f 52 45  ndef SQLITE_CORE
31843 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45 4e 53  .  SQLITE_EXTENS
31844 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73 65 0a  ION_INIT1.#else.
31845 23 65 6e 64 69 66 0a 0a 0a 23 69 66 6e 64 65 66  #endif...#ifndef
31846 20 53 51 4c 49 54 45 5f 41 4d 41 4c 47 41 4d 41   SQLITE_AMALGAMA
31847 54 49 4f 4e 0a 74 79 70 65 64 65 66 20 73 71 6c  TION.typedef sql
31848 69 74 65 33 5f 69 6e 74 36 34 20 69 36 34 3b 0a  ite3_int64 i64;.
31849 74 79 70 65 64 65 66 20 75 6e 73 69 67 6e 65 64  typedef unsigned
3184a 20 63 68 61 72 20 75 38 3b 0a 74 79 70 65 64 65   char u8;.typede
3184b 66 20 75 6e 73 69 67 6e 65 64 20 69 6e 74 20 75  f unsigned int u
3184c 33 32 3b 0a 23 65 6e 64 69 66 0a 0a 74 79 70 65  32;.#endif..type
3184d 64 65 66 20 73 74 72 75 63 74 20 52 74 72 65 65  def struct Rtree
3184e 20 52 74 72 65 65 3b 0a 74 79 70 65 64 65 66 20   Rtree;.typedef 
3184f 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72 73  struct RtreeCurs
31850 6f 72 20 52 74 72 65 65 43 75 72 73 6f 72 3b 0a  or RtreeCursor;.
31851 74 79 70 65 64 65 66 20 73 74 72 75 63 74 20 52  typedef struct R
31852 74 72 65 65 4e 6f 64 65 20 52 74 72 65 65 4e 6f  treeNode RtreeNo
31853 64 65 3b 0a 74 79 70 65 64 65 66 20 73 74 72 75  de;.typedef stru
31854 63 74 20 52 74 72 65 65 43 65 6c 6c 20 52 74 72  ct RtreeCell Rtr
31855 65 65 43 65 6c 6c 3b 0a 74 79 70 65 64 65 66 20  eeCell;.typedef 
31856 73 74 72 75 63 74 20 52 74 72 65 65 43 6f 6e 73  struct RtreeCons
31857 74 72 61 69 6e 74 20 52 74 72 65 65 43 6f 6e 73  traint RtreeCons
31858 74 72 61 69 6e 74 3b 0a 74 79 70 65 64 65 66 20  traint;.typedef 
31859 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72 64  union RtreeCoord
3185a 20 52 74 72 65 65 43 6f 6f 72 64 3b 0a 0a 2f 2a   RtreeCoord;../*
3185b 20 54 68 65 20 72 74 72 65 65 20 6d 61 79 20 68   The rtree may h
3185c 61 76 65 20 62 65 74 77 65 65 6e 20 31 20 61 6e  ave between 1 an
3185d 64 20 52 54 52 45 45 5f 4d 41 58 5f 44 49 4d 45  d RTREE_MAX_DIME
3185e 4e 53 49 4f 4e 53 20 64 69 6d 65 6e 73 69 6f 6e  NSIONS dimension
3185f 73 2e 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54  s. */.#define RT
31860 52 45 45 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f  REE_MAX_DIMENSIO
31861 4e 53 20 35 0a 0a 2f 2a 20 53 69 7a 65 20 6f 66  NS 5../* Size of
31862 20 68 61 73 68 20 74 61 62 6c 65 20 52 74 72 65   hash table Rtre
31863 65 2e 61 48 61 73 68 2e 20 54 68 69 73 20 68 61  e.aHash. This ha
31864 73 68 20 74 61 62 6c 65 20 69 73 20 6e 6f 74 20  sh table is not 
31865 65 78 70 65 63 74 65 64 20 74 6f 0a 2a 2a 20 65  expected to.** e
31866 76 65 72 20 63 6f 6e 74 61 69 6e 20 76 65 72 79  ver contain very
31867 20 6d 61 6e 79 20 65 6e 74 72 69 65 73 2c 20 73   many entries, s
31868 6f 20 61 20 66 69 78 65 64 20 6e 75 6d 62 65 72  o a fixed number
31869 20 6f 66 20 62 75 63 6b 65 74 73 20 69 73 20 0a   of buckets is .
3186a 2a 2a 20 75 73 65 64 2e 0a 2a 2f 0a 23 64 65 66  ** used..*/.#def
3186b 69 6e 65 20 48 41 53 48 53 49 5a 45 20 31 32 38  ine HASHSIZE 128
3186c 0a 0a 2f 2a 20 0a 2a 2a 20 41 6e 20 72 74 72 65  ../* .** An rtre
3186d 65 20 76 69 72 74 75 61 6c 2d 74 61 62 6c 65 20  e virtual-table 
3186e 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 73 74 72 75 63  object..*/.struc
3186f 74 20 52 74 72 65 65 20 7b 0a 20 20 73 71 6c 69  t Rtree {.  sqli
31870 74 65 33 5f 76 74 61 62 20 62 61 73 65 3b 0a 20  te3_vtab base;. 
31871 20 73 71 6c 69 74 65 33 20 2a 64 62 3b 20 20 20   sqlite3 *db;   
31872 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
31873 48 6f 73 74 20 64 61 74 61 62 61 73 65 20 63 6f  Host database co
31874 6e 6e 65 63 74 69 6f 6e 20 2a 2f 0a 20 20 69 6e  nnection */.  in
31875 74 20 69 4e 6f 64 65 53 69 7a 65 3b 20 20 20 20  t iNodeSize;    
31876 20 20 20 20 20 20 20 20 20 20 2f 2a 20 53 69 7a            /* Siz
31877 65 20 69 6e 20 62 79 74 65 73 20 6f 66 20 65 61  e in bytes of ea
31878 63 68 20 6e 6f 64 65 20 69 6e 20 74 68 65 20 6e  ch node in the n
31879 6f 64 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 69  ode table */.  i
3187a 6e 74 20 6e 44 69 6d 3b 20 20 20 20 20 20 20 20  nt nDim;        
3187b 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
3187c 6d 62 65 72 20 6f 66 20 64 69 6d 65 6e 73 69 6f  mber of dimensio
3187d 6e 73 20 2a 2f 0a 20 20 69 6e 74 20 6e 42 79 74  ns */.  int nByt
3187e 65 73 50 65 72 43 65 6c 6c 3b 20 20 20 20 20 20  esPerCell;      
3187f 20 20 20 20 2f 2a 20 42 79 74 65 73 20 63 6f 6e      /* Bytes con
31880 73 75 6d 65 64 20 70 65 72 20 63 65 6c 6c 20 2a  sumed per cell *
31881 2f 0a 20 20 69 6e 74 20 69 44 65 70 74 68 3b 20  /.  int iDepth; 
31882 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31883 2f 2a 20 43 75 72 72 65 6e 74 20 64 65 70 74 68  /* Current depth
31884 20 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 73   of the r-tree s
31885 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 63 68  tructure */.  ch
31886 61 72 20 2a 7a 44 62 3b 20 20 20 20 20 20 20 20  ar *zDb;        
31887 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d            /* Nam
31888 65 20 6f 66 20 64 61 74 61 62 61 73 65 20 63 6f  e of database co
31889 6e 74 61 69 6e 69 6e 67 20 72 2d 74 72 65 65 20  ntaining r-tree 
3188a 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  table */.  char 
3188b 2a 7a 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20  *zName;         
3188c 20 20 20 20 20 20 20 2f 2a 20 4e 61 6d 65 20 6f         /* Name o
3188d 66 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20 2a  f r-tree table *
3188e 2f 20 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  / .  RtreeNode *
3188f 61 48 61 73 68 5b 48 41 53 48 53 49 5a 45 5d 3b  aHash[HASHSIZE];
31890 20 2f 2a 20 48 61 73 68 20 74 61 62 6c 65 20 6f   /* Hash table o
31891 66 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65  f in-memory node
31892 73 2e 20 2a 2f 20 0a 20 20 69 6e 74 20 6e 42 75  s. */ .  int nBu
31893 73 79 3b 20 20 20 20 20 20 20 20 20 20 20 20 20  sy;             
31894 20 20 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20       /* Current 
31895 6e 75 6d 62 65 72 20 6f 66 20 75 73 65 72 73 20  number of users 
31896 6f 66 20 74 68 69 73 20 73 74 72 75 63 74 75 72  of this structur
31897 65 20 2a 2f 0a 0a 20 20 2f 2a 20 4c 69 73 74 20  e */..  /* List 
31898 6f 66 20 6e 6f 64 65 73 20 72 65 6d 6f 76 65 64  of nodes removed
31899 20 64 75 72 69 6e 67 20 61 20 43 6f 6e 64 65 6e   during a Conden
3189a 73 65 54 72 65 65 20 6f 70 65 72 61 74 69 6f 6e  seTree operation
3189b 2e 20 4c 69 73 74 20 69 73 0a 20 20 2a 2a 20 6c  . List is.  ** l
3189c 69 6e 6b 65 64 20 74 6f 67 65 74 68 65 72 20 76  inked together v
3189d 69 61 20 74 68 65 20 70 6f 69 6e 74 65 72 20 6e  ia the pointer n
3189e 6f 72 6d 61 6c 6c 79 20 75 73 65 64 20 66 6f 72  ormally used for
3189f 20 68 61 73 68 20 63 68 61 69 6e 73 20 2d 0a 20   hash chains -. 
318a0 20 2a 2a 20 52 74 72 65 65 4e 6f 64 65 2e 70 4e   ** RtreeNode.pN
318a1 65 78 74 2e 20 52 74 72 65 65 4e 6f 64 65 2e 69  ext. RtreeNode.i
318a2 4e 6f 64 65 20 73 74 6f 72 65 73 20 74 68 65 20  Node stores the 
318a3 64 65 70 74 68 20 6f 66 20 74 68 65 20 73 75 62  depth of the sub
318a4 2d 74 72 65 65 20 0a 20 20 2a 2a 20 68 65 61 64  -tree .  ** head
318a5 65 64 20 62 79 20 74 68 65 20 6e 6f 64 65 20 28  ed by the node (
318a6 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76 65 20  leaf nodes have 
318a7 52 74 72 65 65 4e 6f 64 65 2e 69 4e 6f 64 65 3d  RtreeNode.iNode=
318a8 3d 30 29 2e 0a 20 20 2a 2f 0a 20 20 52 74 72 65  =0)..  */.  Rtre
318a9 65 4e 6f 64 65 20 2a 70 44 65 6c 65 74 65 64 3b  eNode *pDeleted;
318aa 0a 20 20 69 6e 74 20 69 52 65 69 6e 73 65 72 74  .  int iReinsert
318ab 48 65 69 67 68 74 3b 20 20 20 20 20 20 20 20 2f  Height;        /
318ac 2a 20 48 65 69 67 68 74 20 6f 66 20 73 75 62 2d  * Height of sub-
318ad 74 72 65 65 73 20 52 65 69 6e 73 65 72 74 28 29  trees Reinsert()
318ae 20 68 61 73 20 72 75 6e 20 6f 6e 20 2a 2f 0a 0a   has run on */..
318af 20 20 2f 2a 20 53 74 61 74 65 6d 65 6e 74 73 20    /* Statements 
318b0 74 6f 20 72 65 61 64 2f 77 72 69 74 65 2f 64 65  to read/write/de
318b1 6c 65 74 65 20 61 20 72 65 63 6f 72 64 20 66 72  lete a record fr
318b2 6f 6d 20 78 78 78 5f 6e 6f 64 65 20 2a 2f 0a 20  om xxx_node */. 
318b3 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70   sqlite3_stmt *p
318b4 52 65 61 64 4e 6f 64 65 3b 0a 20 20 73 71 6c 69  ReadNode;.  sqli
318b5 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74 65  te3_stmt *pWrite
318b6 4e 6f 64 65 3b 0a 20 20 73 71 6c 69 74 65 33 5f  Node;.  sqlite3_
318b7 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 4e 6f 64  stmt *pDeleteNod
318b8 65 3b 0a 0a 20 20 2f 2a 20 53 74 61 74 65 6d 65  e;..  /* Stateme
318b9 6e 74 73 20 74 6f 20 72 65 61 64 2f 77 72 69 74  nts to read/writ
318ba 65 2f 64 65 6c 65 74 65 20 61 20 72 65 63 6f 72  e/delete a recor
318bb 64 20 66 72 6f 6d 20 78 78 78 5f 72 6f 77 69 64  d from xxx_rowid
318bc 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 73 74   */.  sqlite3_st
318bd 6d 74 20 2a 70 52 65 61 64 52 6f 77 69 64 3b 0a  mt *pReadRowid;.
318be 20 20 73 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a    sqlite3_stmt *
318bf 70 57 72 69 74 65 52 6f 77 69 64 3b 0a 20 20 73  pWriteRowid;.  s
318c0 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 44 65  qlite3_stmt *pDe
318c1 6c 65 74 65 52 6f 77 69 64 3b 0a 0a 20 20 2f 2a  leteRowid;..  /*
318c2 20 53 74 61 74 65 6d 65 6e 74 73 20 74 6f 20 72   Statements to r
318c3 65 61 64 2f 77 72 69 74 65 2f 64 65 6c 65 74 65  ead/write/delete
318c4 20 61 20 72 65 63 6f 72 64 20 66 72 6f 6d 20 78   a record from x
318c5 78 78 5f 70 61 72 65 6e 74 20 2a 2f 0a 20 20 73  xx_parent */.  s
318c6 71 6c 69 74 65 33 5f 73 74 6d 74 20 2a 70 52 65  qlite3_stmt *pRe
318c7 61 64 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69  adParent;.  sqli
318c8 74 65 33 5f 73 74 6d 74 20 2a 70 57 72 69 74 65  te3_stmt *pWrite
318c9 50 61 72 65 6e 74 3b 0a 20 20 73 71 6c 69 74 65  Parent;.  sqlite
318ca 33 5f 73 74 6d 74 20 2a 70 44 65 6c 65 74 65 50  3_stmt *pDeleteP
318cb 61 72 65 6e 74 3b 0a 0a 20 20 69 6e 74 20 65 43  arent;..  int eC
318cc 6f 6f 72 64 54 79 70 65 3b 0a 7d 3b 0a 0a 2f 2a  oordType;.};../*
318cd 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65 73   Possible values
318ce 20 66 6f 72 20 65 43 6f 6f 72 64 54 79 70 65 3a   for eCoordType:
318cf 20 2a 2f 0a 23 64 65 66 69 6e 65 20 52 54 52 45   */.#define RTRE
318d0 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 30  E_COORD_REAL32 0
318d1 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 43  .#define RTREE_C
318d2 4f 4f 52 44 5f 49 4e 54 33 32 20 20 31 0a 0a 2f  OORD_INT32  1../
318d3 2a 0a 2a 2a 20 54 68 65 20 6d 69 6e 69 6d 75 6d  *.** The minimum
318d4 20 6e 75 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73   number of cells
318d5 20 61 6c 6c 6f 77 65 64 20 66 6f 72 20 61 20 6e   allowed for a n
318d6 6f 64 65 20 69 73 20 61 20 74 68 69 72 64 20 6f  ode is a third o
318d7 66 20 74 68 65 20 0a 2a 2a 20 6d 61 78 69 6d 75  f the .** maximu
318d8 6d 2e 20 49 6e 20 47 75 74 6d 61 6e 27 73 20 6e  m. In Gutman's n
318d9 6f 74 61 74 69 6f 6e 3a 0a 2a 2a 0a 2a 2a 20 20  otation:.**.**  
318da 20 20 20 6d 20 3d 20 4d 2f 33 0a 2a 2a 0a 2a 2a     m = M/3.**.**
318db 20 49 66 20 61 6e 20 52 2a 2d 74 72 65 65 20 22   If an R*-tree "
318dc 52 65 69 6e 73 65 72 74 22 20 6f 70 65 72 61 74  Reinsert" operat
318dd 69 6f 6e 20 69 73 20 72 65 71 75 69 72 65 64 2c  ion is required,
318de 20 74 68 65 20 73 61 6d 65 20 6e 75 6d 62 65 72   the same number
318df 20 6f 66 0a 2a 2a 20 63 65 6c 6c 73 20 61 72 65   of.** cells are
318e0 20 72 65 6d 6f 76 65 64 20 66 72 6f 6d 20 74 68   removed from th
318e1 65 20 6f 76 65 72 66 75 6c 6c 20 6e 6f 64 65 20  e overfull node 
318e2 61 6e 64 20 72 65 69 6e 73 65 72 74 65 64 20 69  and reinserted i
318e3 6e 74 6f 20 74 68 65 20 74 72 65 65 2e 0a 2a 2f  nto the tree..*/
318e4 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f 4d  .#define RTREE_M
318e5 49 4e 43 45 4c 4c 53 28 70 29 20 28 28 28 28 70  INCELLS(p) ((((p
318e6 29 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 34 29 2f  )->iNodeSize-4)/
318e7 28 70 29 2d 3e 6e 42 79 74 65 73 50 65 72 43 65  (p)->nBytesPerCe
318e8 6c 6c 29 2f 33 29 0a 23 64 65 66 69 6e 65 20 52  ll)/3).#define R
318e9 54 52 45 45 5f 52 45 49 4e 53 45 52 54 28 70 29  TREE_REINSERT(p)
318ea 20 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28   RTREE_MINCELLS(
318eb 70 29 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45  p).#define RTREE
318ec 5f 4d 41 58 43 45 4c 4c 53 20 35 31 0a 0a 2f 2a  _MAXCELLS 51../*
318ed 20 0a 2a 2a 20 41 6e 20 72 74 72 65 65 20 63 75   .** An rtree cu
318ee 72 73 6f 72 20 6f 62 6a 65 63 74 2e 0a 2a 2f 0a  rsor object..*/.
318ef 73 74 72 75 63 74 20 52 74 72 65 65 43 75 72 73  struct RtreeCurs
318f0 6f 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 76  or {.  sqlite3_v
318f1 74 61 62 5f 63 75 72 73 6f 72 20 62 61 73 65 3b  tab_cursor base;
318f2 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
318f3 6f 64 65 3b 20 20 20 20 20 20 20 20 20 20 20 20  ode;            
318f4 20 20 20 20 20 2f 2a 20 4e 6f 64 65 20 63 75 72       /* Node cur
318f5 73 6f 72 20 69 73 20 63 75 72 72 65 6e 74 6c 79  sor is currently
318f6 20 70 6f 69 6e 74 69 6e 67 20 61 74 20 2a 2f 0a   pointing at */.
318f7 20 20 69 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20    int iCell;    
318f8 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
318f9 20 20 20 20 2f 2a 20 49 6e 64 65 78 20 6f 66 20      /* Index of 
318fa 63 75 72 72 65 6e 74 20 63 65 6c 6c 20 69 6e 20  current cell in 
318fb 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20 69  pNode */.  int i
318fc 53 74 72 61 74 65 67 79 3b 20 20 20 20 20 20 20  Strategy;       
318fd 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
318fe 43 6f 70 79 20 6f 66 20 69 64 78 4e 75 6d 20 73  Copy of idxNum s
318ff 65 61 72 63 68 20 70 61 72 61 6d 65 74 65 72 20  earch parameter 
31900 2a 2f 0a 20 20 69 6e 74 20 6e 43 6f 6e 73 74 72  */.  int nConstr
31901 61 69 6e 74 3b 20 20 20 20 20 20 20 20 20 20 20  aint;           
31902 20 20 20 20 20 20 20 2f 2a 20 4e 75 6d 62 65 72         /* Number
31903 20 6f 66 20 65 6e 74 72 69 65 73 20 69 6e 20 61   of entries in a
31904 43 6f 6e 73 74 72 61 69 6e 74 20 2a 2f 0a 20 20  Constraint */.  
31905 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20  RtreeConstraint 
31906 2a 61 43 6f 6e 73 74 72 61 69 6e 74 3b 20 20 20  *aConstraint;   
31907 20 20 2f 2a 20 53 65 61 72 63 68 20 63 6f 6e 73    /* Search cons
31908 74 72 61 69 6e 74 73 2e 20 2a 2f 0a 7d 3b 0a 0a  traints. */.};..
31909 75 6e 69 6f 6e 20 52 74 72 65 65 43 6f 6f 72 64  union RtreeCoord
3190a 20 7b 0a 20 20 66 6c 6f 61 74 20 66 3b 0a 20 20   {.  float f;.  
3190b 69 6e 74 20 69 3b 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a  int i;.};../*.**
3190c 20 54 68 65 20 61 72 67 75 6d 65 6e 74 20 69 73   The argument is
3190d 20 61 6e 20 52 74 72 65 65 43 6f 6f 72 64 2e 20   an RtreeCoord. 
3190e 52 65 74 75 72 6e 20 74 68 65 20 76 61 6c 75 65  Return the value
3190f 20 73 74 6f 72 65 64 20 77 69 74 68 69 6e 20 74   stored within t
31910 68 65 20 52 74 72 65 65 43 6f 6f 72 64 0a 2a 2a  he RtreeCoord.**
31911 20 66 6f 72 6d 61 74 74 65 64 20 61 73 20 61 20   formatted as a 
31912 64 6f 75 62 6c 65 2e 20 54 68 69 73 20 6d 61 63  double. This mac
31913 72 6f 20 61 73 73 75 6d 65 73 20 74 68 61 74 20  ro assumes that 
31914 6c 6f 63 61 6c 20 76 61 72 69 61 62 6c 65 20 70  local variable p
31915 52 74 72 65 65 20 70 6f 69 6e 74 73 0a 2a 2a 20  Rtree points.** 
31916 74 6f 20 74 68 65 20 52 74 72 65 65 20 73 74 72  to the Rtree str
31917 75 63 74 75 72 65 20 61 73 73 6f 63 69 61 74 65  ucture associate
31918 64 20 77 69 74 68 20 74 68 65 20 52 74 72 65 65  d with the Rtree
31919 43 6f 6f 72 64 2e 0a 2a 2f 0a 23 64 65 66 69 6e  Coord..*/.#defin
3191a 65 20 44 43 4f 4f 52 44 28 63 6f 6f 72 64 29 20  e DCOORD(coord) 
3191b 28 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  (               
3191c 20 20 20 20 20 20 20 20 20 20 20 20 5c 0a 20 20              \.  
3191d 28 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54  (pRtree->eCoordT
3191e 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
3191f 5f 52 45 41 4c 33 32 29 20 3f 20 20 20 20 20 20  _REAL32) ?      
31920 5c 0a 20 20 20 20 28 28 64 6f 75 62 6c 65 29 63  \.    ((double)c
31921 6f 6f 72 64 2e 66 29 20 3a 20 20 20 20 20 20 20  oord.f) :       
31922 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31923 20 20 20 20 5c 0a 20 20 20 20 28 28 64 6f 75 62      \.    ((doub
31924 6c 65 29 63 6f 6f 72 64 2e 69 29 20 20 20 20 20  le)coord.i)     
31925 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31926 20 20 20 20 20 20 20 20 5c 0a 29 0a 0a 2f 2a 0a          \.)../*.
31927 2a 2a 20 41 20 73 65 61 72 63 68 20 63 6f 6e 73  ** A search cons
31928 74 72 61 69 6e 74 2e 0a 2a 2f 0a 73 74 72 75 63  traint..*/.struc
31929 74 20 52 74 72 65 65 43 6f 6e 73 74 72 61 69 6e  t RtreeConstrain
3192a 74 20 7b 0a 20 20 69 6e 74 20 69 43 6f 6f 72 64  t {.  int iCoord
3192b 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
3192c 20 20 20 20 20 20 20 20 2f 2a 20 49 6e 64 65 78          /* Index
3192d 20 6f 66 20 63 6f 6e 73 74 72 61 69 6e 65 64 20   of constrained 
3192e 63 6f 6f 72 64 69 6e 61 74 65 20 2a 2f 0a 20 20  coordinate */.  
3192f 69 6e 74 20 6f 70 3b 20 20 20 20 20 20 20 20 20  int op;         
31930 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31931 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 69 6e    /* Constrainin
31932 67 20 6f 70 65 72 61 74 69 6f 6e 20 2a 2f 0a 20  g operation */. 
31933 20 64 6f 75 62 6c 65 20 72 56 61 6c 75 65 3b 20   double rValue; 
31934 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31935 20 20 20 2f 2a 20 43 6f 6e 73 74 72 61 69 6e 74     /* Constraint
31936 20 76 61 6c 75 65 2e 20 2a 2f 0a 7d 3b 0a 0a 2f   value. */.};../
31937 2a 20 50 6f 73 73 69 62 6c 65 20 76 61 6c 75 65  * Possible value
31938 73 20 66 6f 72 20 52 74 72 65 65 43 6f 6e 73 74  s for RtreeConst
31939 72 61 69 6e 74 2e 6f 70 20 2a 2f 0a 23 64 65 66  raint.op */.#def
3193a 69 6e 65 20 52 54 52 45 45 5f 45 51 20 30 78 34  ine RTREE_EQ 0x4
3193b 31 0a 23 64 65 66 69 6e 65 20 52 54 52 45 45 5f  1.#define RTREE_
3193c 4c 45 20 30 78 34 32 0a 23 64 65 66 69 6e 65 20  LE 0x42.#define 
3193d 52 54 52 45 45 5f 4c 54 20 30 78 34 33 0a 23 64  RTREE_LT 0x43.#d
3193e 65 66 69 6e 65 20 52 54 52 45 45 5f 47 45 20 30  efine RTREE_GE 0
3193f 78 34 34 0a 23 64 65 66 69 6e 65 20 52 54 52 45  x44.#define RTRE
31940 45 5f 47 54 20 30 78 34 35 0a 0a 2f 2a 20 0a 2a  E_GT 0x45../* .*
31941 2a 20 41 6e 20 72 74 72 65 65 20 73 74 72 75 63  * An rtree struc
31942 74 75 72 65 20 6e 6f 64 65 2e 0a 2a 2a 0a 2a 2a  ture node..**.**
31943 20 44 61 74 61 20 66 6f 72 6d 61 74 20 28 52 74   Data format (Rt
31944 72 65 65 4e 6f 64 65 2e 7a 44 61 74 61 29 3a 0a  reeNode.zData):.
31945 2a 2a 0a 2a 2a 20 20 20 31 2e 20 49 66 20 74 68  **.**   1. If th
31946 65 20 6e 6f 64 65 20 69 73 20 74 68 65 20 72 6f  e node is the ro
31947 6f 74 20 6e 6f 64 65 20 28 6e 6f 64 65 20 31 29  ot node (node 1)
31948 2c 20 74 68 65 6e 20 74 68 65 20 66 69 72 73 74  , then the first
31949 20 32 20 62 79 74 65 73 0a 2a 2a 20 20 20 20 20   2 bytes.**     
3194a 20 6f 66 20 74 68 65 20 6e 6f 64 65 20 63 6f 6e   of the node con
3194b 74 61 69 6e 20 74 68 65 20 74 72 65 65 20 64 65  tain the tree de
3194c 70 74 68 20 61 73 20 61 20 62 69 67 2d 65 6e 64  pth as a big-end
3194d 69 61 6e 20 69 6e 74 65 67 65 72 2e 0a 2a 2a 20  ian integer..** 
3194e 20 20 20 20 20 46 6f 72 20 6e 6f 6e 2d 72 6f 6f       For non-roo
3194f 74 20 6e 6f 64 65 73 2c 20 74 68 65 20 66 69 72  t nodes, the fir
31950 73 74 20 32 20 62 79 74 65 73 20 61 72 65 20 6c  st 2 bytes are l
31951 65 66 74 20 75 6e 75 73 65 64 2e 0a 2a 2a 0a 2a  eft unused..**.*
31952 2a 20 20 20 32 2e 20 54 68 65 20 6e 65 78 74 20  *   2. The next 
31953 32 20 62 79 74 65 73 20 63 6f 6e 74 61 69 6e 20  2 bytes contain 
31954 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 65 6e  the number of en
31955 74 72 69 65 73 20 63 75 72 72 65 6e 74 6c 79 20  tries currently 
31956 0a 2a 2a 20 20 20 20 20 20 73 74 6f 72 65 64 20  .**      stored 
31957 69 6e 20 74 68 65 20 6e 6f 64 65 2e 0a 2a 2a 0a  in the node..**.
31958 2a 2a 20 20 20 33 2e 20 54 68 65 20 72 65 6d 61  **   3. The rema
31959 69 6e 64 65 72 20 6f 66 20 74 68 65 20 6e 6f 64  inder of the nod
3195a 65 20 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 6e  e contains the n
3195b 6f 64 65 20 65 6e 74 72 69 65 73 2e 20 45 61 63  ode entries. Eac
3195c 68 20 65 6e 74 72 79 0a 2a 2a 20 20 20 20 20 20  h entry.**      
3195d 63 6f 6e 73 69 73 74 73 20 6f 66 20 61 20 73 69  consists of a si
3195e 6e 67 6c 65 20 38 2d 62 79 74 65 20 69 6e 74 65  ngle 8-byte inte
3195f 67 65 72 20 66 6f 6c 6c 6f 77 65 64 20 62 79 20  ger followed by 
31960 61 6e 20 65 76 65 6e 20 6e 75 6d 62 65 72 0a 2a  an even number.*
31961 2a 20 20 20 20 20 20 6f 66 20 34 2d 62 79 74 65  *      of 4-byte
31962 20 63 6f 6f 72 64 69 6e 61 74 65 73 2e 20 46 6f   coordinates. Fo
31963 72 20 6c 65 61 66 20 6e 6f 64 65 73 20 74 68 65  r leaf nodes the
31964 20 69 6e 74 65 67 65 72 20 69 73 20 74 68 65 20   integer is the 
31965 72 6f 77 69 64 0a 2a 2a 20 20 20 20 20 20 6f 66  rowid.**      of
31966 20 61 20 72 65 63 6f 72 64 2e 20 46 6f 72 20 69   a record. For i
31967 6e 74 65 72 6e 61 6c 20 6e 6f 64 65 73 20 69 74  nternal nodes it
31968 20 69 73 20 74 68 65 20 6e 6f 64 65 20 6e 75 6d   is the node num
31969 62 65 72 20 6f 66 20 61 0a 2a 2a 20 20 20 20 20  ber of a.**     
3196a 20 63 68 69 6c 64 20 70 61 67 65 2e 0a 2a 2f 0a   child page..*/.
3196b 73 74 72 75 63 74 20 52 74 72 65 65 4e 6f 64 65  struct RtreeNode
3196c 20 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a   {.  RtreeNode *
3196d 70 50 61 72 65 6e 74 3b 20 20 20 20 20 20 20 20  pParent;        
3196e 20 20 20 20 20 20 20 2f 2a 20 50 61 72 65 6e 74         /* Parent
3196f 20 6e 6f 64 65 20 2a 2f 0a 20 20 69 36 34 20 69   node */.  i64 i
31970 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 6e 52 65 66  Node;.  int nRef
31971 3b 0a 20 20 69 6e 74 20 69 73 44 69 72 74 79 3b  ;.  int isDirty;
31972 0a 20 20 75 38 20 2a 7a 44 61 74 61 3b 0a 20 20  .  u8 *zData;.  
31973 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 65 78 74  RtreeNode *pNext
31974 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ;               
31975 20 20 2f 2a 20 4e 65 78 74 20 6e 6f 64 65 20 69    /* Next node i
31976 6e 20 74 68 69 73 20 68 61 73 68 20 63 68 61 69  n this hash chai
31977 6e 20 2a 2f 0a 7d 3b 0a 23 64 65 66 69 6e 65 20  n */.};.#define 
31978 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 20 72 65 61  NCELL(pNode) rea
31979 64 49 6e 74 31 36 28 26 28 70 4e 6f 64 65 29 2d  dInt16(&(pNode)-
3197a 3e 7a 44 61 74 61 5b 32 5d 29 0a 0a 2f 2a 20 0a  >zData[2])../* .
3197b 2a 2a 20 53 74 72 75 63 74 75 72 65 20 74 6f 20  ** Structure to 
3197c 73 74 6f 72 65 20 61 20 64 65 73 65 72 69 61 6c  store a deserial
3197d 69 7a 65 64 20 72 74 72 65 65 20 72 65 63 6f 72  ized rtree recor
3197e 64 2e 0a 2a 2f 0a 73 74 72 75 63 74 20 52 74 72  d..*/.struct Rtr
3197f 65 65 43 65 6c 6c 20 7b 0a 20 20 69 36 34 20 69  eeCell {.  i64 i
31980 52 6f 77 69 64 3b 0a 20 20 52 74 72 65 65 43 6f  Rowid;.  RtreeCo
31981 6f 72 64 20 61 43 6f 6f 72 64 5b 52 54 52 45 45  ord aCoord[RTREE
31982 5f 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a  _MAX_DIMENSIONS*
31983 32 5d 3b 0a 7d 3b 0a 0a 23 69 66 6e 64 65 66 20  2];.};..#ifndef 
31984 4d 41 58 0a 23 20 64 65 66 69 6e 65 20 4d 41 58  MAX.# define MAX
31985 28 78 2c 79 29 20 28 28 78 29 20 3c 20 28 79 29  (x,y) ((x) < (y)
31986 20 3f 20 28 79 29 20 3a 20 28 78 29 29 0a 23 65   ? (y) : (x)).#e
31987 6e 64 69 66 0a 23 69 66 6e 64 65 66 20 4d 49 4e  ndif.#ifndef MIN
31988 0a 23 20 64 65 66 69 6e 65 20 4d 49 4e 28 78 2c  .# define MIN(x,
31989 79 29 20 28 28 78 29 20 3e 20 28 79 29 20 3f 20  y) ((x) > (y) ? 
3198a 28 79 29 20 3a 20 28 78 29 29 0a 23 65 6e 64 69  (y) : (x)).#endi
3198b 66 0a 0a 2f 2a 0a 2a 2a 20 46 75 6e 63 74 69 6f  f../*.** Functio
3198c 6e 73 20 74 6f 20 64 65 73 65 72 69 61 6c 69 7a  ns to deserializ
3198d 65 20 61 20 31 36 20 62 69 74 20 69 6e 74 65 67  e a 16 bit integ
3198e 65 72 2c 20 33 32 20 62 69 74 20 72 65 61 6c 20  er, 32 bit real 
3198f 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a 20 36 34  number and.** 64
31990 20 62 69 74 20 69 6e 74 65 67 65 72 2e 20 54 68   bit integer. Th
31991 65 20 64 65 73 65 72 69 61 6c 69 7a 65 64 20 76  e deserialized v
31992 61 6c 75 65 20 69 73 20 72 65 74 75 72 6e 65 64  alue is returned
31993 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31994 72 65 61 64 49 6e 74 31 36 28 75 38 20 2a 70 29  readInt16(u8 *p)
31995 7b 0a 20 20 72 65 74 75 72 6e 20 28 70 5b 30 5d  {.  return (p[0]
31996 3c 3c 38 29 20 2b 20 70 5b 31 5d 3b 0a 7d 0a 73  <<8) + p[1];.}.s
31997 74 61 74 69 63 20 76 6f 69 64 20 72 65 61 64 43  tatic void readC
31998 6f 6f 72 64 28 75 38 20 2a 70 2c 20 52 74 72 65  oord(u8 *p, Rtre
31999 65 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 29 7b  eCoord *pCoord){
3199a 0a 20 20 75 33 32 20 69 20 3d 20 28 0a 20 20 20  .  u32 i = (.   
3199b 20 28 28 28 75 33 32 29 70 5b 30 5d 29 20 3c 3c   (((u32)p[0]) <<
3199c 20 32 34 29 20 2b 20 0a 20 20 20 20 28 28 28 75   24) + .    (((u
3199d 33 32 29 70 5b 31 5d 29 20 3c 3c 20 31 36 29 20  32)p[1]) << 16) 
3199e 2b 20 0a 20 20 20 20 28 28 28 75 33 32 29 70 5b  + .    (((u32)p[
3199f 32 5d 29 20 3c 3c 20 20 38 29 20 2b 20 0a 20 20  2]) <<  8) + .  
319a0 20 20 28 28 28 75 33 32 29 70 5b 33 5d 29 20 3c    (((u32)p[3]) <
319a1 3c 20 20 30 29 0a 20 20 29 3b 0a 20 20 2a 28 75  <  0).  );.  *(u
319a2 33 32 20 2a 29 70 43 6f 6f 72 64 20 3d 20 69 3b  32 *)pCoord = i;
319a3 0a 7d 0a 73 74 61 74 69 63 20 69 36 34 20 72 65  .}.static i64 re
319a4 61 64 49 6e 74 36 34 28 75 38 20 2a 70 29 7b 0a  adInt64(u8 *p){.
319a5 20 20 72 65 74 75 72 6e 20 28 0a 20 20 20 20 28    return (.    (
319a6 28 28 69 36 34 29 70 5b 30 5d 29 20 3c 3c 20 35  ((i64)p[0]) << 5
319a7 36 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36 34  6) + .    (((i64
319a8 29 70 5b 31 5d 29 20 3c 3c 20 34 38 29 20 2b 20  )p[1]) << 48) + 
319a9 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 32 5d  .    (((i64)p[2]
319aa 29 20 3c 3c 20 34 30 29 20 2b 20 0a 20 20 20 20  ) << 40) + .    
319ab 28 28 28 69 36 34 29 70 5b 33 5d 29 20 3c 3c 20  (((i64)p[3]) << 
319ac 33 32 29 20 2b 20 0a 20 20 20 20 28 28 28 69 36  32) + .    (((i6
319ad 34 29 70 5b 34 5d 29 20 3c 3c 20 32 34 29 20 2b  4)p[4]) << 24) +
319ae 20 0a 20 20 20 20 28 28 28 69 36 34 29 70 5b 35   .    (((i64)p[5
319af 5d 29 20 3c 3c 20 31 36 29 20 2b 20 0a 20 20 20  ]) << 16) + .   
319b0 20 28 28 28 69 36 34 29 70 5b 36 5d 29 20 3c 3c   (((i64)p[6]) <<
319b1 20 20 38 29 20 2b 20 0a 20 20 20 20 28 28 28 69    8) + .    (((i
319b2 36 34 29 70 5b 37 5d 29 20 3c 3c 20 20 30 29 0a  64)p[7]) <<  0).
319b3 20 20 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 46 75    );.}../*.** Fu
319b4 6e 63 74 69 6f 6e 73 20 74 6f 20 73 65 72 69 61  nctions to seria
319b5 6c 69 7a 65 20 61 20 31 36 20 62 69 74 20 69 6e  lize a 16 bit in
319b6 74 65 67 65 72 2c 20 33 32 20 62 69 74 20 72 65  teger, 32 bit re
319b7 61 6c 20 6e 75 6d 62 65 72 20 61 6e 64 0a 2a 2a  al number and.**
319b8 20 36 34 20 62 69 74 20 69 6e 74 65 67 65 72 2e   64 bit integer.
319b9 20 54 68 65 20 76 61 6c 75 65 20 72 65 74 75 72   The value retur
319ba 6e 65 64 20 69 73 20 74 68 65 20 6e 75 6d 62 65  ned is the numbe
319bb 72 20 6f 66 20 62 79 74 65 73 20 77 72 69 74 74  r of bytes writt
319bc 65 6e 0a 2a 2a 20 74 6f 20 74 68 65 20 61 72 67  en.** to the arg
319bd 75 6d 65 6e 74 20 62 75 66 66 65 72 20 28 61 6c  ument buffer (al
319be 77 61 79 73 20 32 2c 20 34 20 61 6e 64 20 38 20  ways 2, 4 and 8 
319bf 72 65 73 70 65 63 74 69 76 65 6c 79 29 2e 0a 2a  respectively)..*
319c0 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72 69  /.static int wri
319c1 74 65 49 6e 74 31 36 28 75 38 20 2a 70 2c 20 69  teInt16(u8 *p, i
319c2 6e 74 20 69 29 7b 0a 20 20 70 5b 30 5d 20 3d 20  nt i){.  p[0] = 
319c3 28 69 3e 3e 20 38 29 26 30 78 46 46 3b 0a 20 20  (i>> 8)&0xFF;.  
319c4 70 5b 31 5d 20 3d 20 28 69 3e 3e 20 30 29 26 30  p[1] = (i>> 0)&0
319c5 78 46 46 3b 0a 20 20 72 65 74 75 72 6e 20 32 3b  xFF;.  return 2;
319c6 0a 7d 0a 73 74 61 74 69 63 20 69 6e 74 20 77 72  .}.static int wr
319c7 69 74 65 43 6f 6f 72 64 28 75 38 20 2a 70 2c 20  iteCoord(u8 *p, 
319c8 52 74 72 65 65 43 6f 6f 72 64 20 2a 70 43 6f 6f  RtreeCoord *pCoo
319c9 72 64 29 7b 0a 20 20 75 33 32 20 69 3b 0a 20 20  rd){.  u32 i;.  
319ca 61 73 73 65 72 74 28 20 73 69 7a 65 6f 66 28 52  assert( sizeof(R
319cb 74 72 65 65 43 6f 6f 72 64 29 3d 3d 34 20 29 3b  treeCoord)==4 );
319cc 0a 20 20 61 73 73 65 72 74 28 20 73 69 7a 65 6f  .  assert( sizeo
319cd 66 28 75 33 32 29 3d 3d 34 20 29 3b 0a 20 20 69  f(u32)==4 );.  i
319ce 20 3d 20 2a 28 75 33 32 20 2a 29 70 43 6f 6f 72   = *(u32 *)pCoor
319cf 64 3b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e  d;.  p[0] = (i>>
319d0 32 34 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d  24)&0xFF;.  p[1]
319d1 20 3d 20 28 69 3e 3e 31 36 29 26 30 78 46 46 3b   = (i>>16)&0xFF;
319d2 0a 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 20 38  .  p[2] = (i>> 8
319d3 29 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d  )&0xFF;.  p[3] =
319d4 20 28 69 3e 3e 20 30 29 26 30 78 46 46 3b 0a 20   (i>> 0)&0xFF;. 
319d5 20 72 65 74 75 72 6e 20 34 3b 0a 7d 0a 73 74 61   return 4;.}.sta
319d6 74 69 63 20 69 6e 74 20 77 72 69 74 65 49 6e 74  tic int writeInt
319d7 36 34 28 75 38 20 2a 70 2c 20 69 36 34 20 69 29  64(u8 *p, i64 i)
319d8 7b 0a 20 20 70 5b 30 5d 20 3d 20 28 69 3e 3e 35  {.  p[0] = (i>>5
319d9 36 29 26 30 78 46 46 3b 0a 20 20 70 5b 31 5d 20  6)&0xFF;.  p[1] 
319da 3d 20 28 69 3e 3e 34 38 29 26 30 78 46 46 3b 0a  = (i>>48)&0xFF;.
319db 20 20 70 5b 32 5d 20 3d 20 28 69 3e 3e 34 30 29    p[2] = (i>>40)
319dc 26 30 78 46 46 3b 0a 20 20 70 5b 33 5d 20 3d 20  &0xFF;.  p[3] = 
319dd 28 69 3e 3e 33 32 29 26 30 78 46 46 3b 0a 20 20  (i>>32)&0xFF;.  
319de 70 5b 34 5d 20 3d 20 28 69 3e 3e 32 34 29 26 30  p[4] = (i>>24)&0
319df 78 46 46 3b 0a 20 20 70 5b 35 5d 20 3d 20 28 69  xFF;.  p[5] = (i
319e0 3e 3e 31 36 29 26 30 78 46 46 3b 0a 20 20 70 5b  >>16)&0xFF;.  p[
319e1 36 5d 20 3d 20 28 69 3e 3e 20 38 29 26 30 78 46  6] = (i>> 8)&0xF
319e2 46 3b 0a 20 20 70 5b 37 5d 20 3d 20 28 69 3e 3e  F;.  p[7] = (i>>
319e3 20 30 29 26 30 78 46 46 3b 0a 20 20 72 65 74 75   0)&0xFF;.  retu
319e4 72 6e 20 38 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  rn 8;.}../*.** I
319e5 6e 63 72 65 6d 65 6e 74 20 74 68 65 20 72 65 66  ncrement the ref
319e6 65 72 65 6e 63 65 20 63 6f 75 6e 74 20 6f 66 20  erence count of 
319e7 6e 6f 64 65 20 70 2e 0a 2a 2f 0a 73 74 61 74 69  node p..*/.stati
319e8 63 20 76 6f 69 64 20 6e 6f 64 65 52 65 66 65 72  c void nodeRefer
319e9 65 6e 63 65 28 52 74 72 65 65 4e 6f 64 65 20 2a  ence(RtreeNode *
319ea 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b 0a 20  p){.  if( p ){. 
319eb 20 20 20 70 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20     p->nRef++;.  
319ec 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6c 65 61 72  }.}../*.** Clear
319ed 20 74 68 65 20 63 6f 6e 74 65 6e 74 20 6f 66 20   the content of 
319ee 6e 6f 64 65 20 70 20 28 73 65 74 20 61 6c 6c 20  node p (set all 
319ef 62 79 74 65 73 20 74 6f 20 30 78 30 30 29 2e 0a  bytes to 0x00)..
319f0 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e  */.static void n
319f1 6f 64 65 5a 65 72 6f 28 52 74 72 65 65 20 2a 70  odeZero(Rtree *p
319f2 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
319f3 20 2a 70 29 7b 0a 20 20 69 66 28 20 70 20 29 7b   *p){.  if( p ){
319f4 0a 20 20 20 20 6d 65 6d 73 65 74 28 26 70 2d 3e  .    memset(&p->
319f5 7a 44 61 74 61 5b 32 5d 2c 20 30 2c 20 70 52 74  zData[2], 0, pRt
319f6 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a 65 2d 32  ree->iNodeSize-2
319f7 29 3b 0a 20 20 20 20 70 2d 3e 69 73 44 69 72 74  );.    p->isDirt
319f8 79 20 3d 20 31 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a  y = 1;.  }.}../*
319f9 0a 2a 2a 20 47 69 76 65 6e 20 61 20 6e 6f 64 65  .** Given a node
319fa 20 6e 75 6d 62 65 72 20 69 4e 6f 64 65 2c 20 72   number iNode, r
319fb 65 74 75 72 6e 20 74 68 65 20 63 6f 72 72 65 73  eturn the corres
319fc 70 6f 6e 64 69 6e 67 20 6b 65 79 20 74 6f 20 75  ponding key to u
319fd 73 65 0a 2a 2a 20 69 6e 20 74 68 65 20 52 74 72  se.** in the Rtr
319fe 65 65 2e 61 48 61 73 68 20 74 61 62 6c 65 2e 0a  ee.aHash table..
319ff 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 6e 6f  */.static int no
31a00 64 65 48 61 73 68 28 69 36 34 20 69 4e 6f 64 65  deHash(i64 iNode
31a01 29 7b 0a 20 20 72 65 74 75 72 6e 20 28 0a 20 20  ){.  return (.  
31a02 20 20 28 69 4e 6f 64 65 3e 3e 35 36 29 20 5e 20    (iNode>>56) ^ 
31a03 28 69 4e 6f 64 65 3e 3e 34 38 29 20 5e 20 28 69  (iNode>>48) ^ (i
31a04 4e 6f 64 65 3e 3e 34 30 29 20 5e 20 28 69 4e 6f  Node>>40) ^ (iNo
31a05 64 65 3e 3e 33 32 29 20 5e 20 0a 20 20 20 20 28  de>>32) ^ .    (
31a06 69 4e 6f 64 65 3e 3e 32 34 29 20 5e 20 28 69 4e  iNode>>24) ^ (iN
31a07 6f 64 65 3e 3e 31 36 29 20 5e 20 28 69 4e 6f 64  ode>>16) ^ (iNod
31a08 65 3e 3e 20 38 29 20 5e 20 28 69 4e 6f 64 65 3e  e>> 8) ^ (iNode>
31a09 3e 20 30 29 0a 20 20 29 20 25 20 48 41 53 48 53  > 0).  ) % HASHS
31a0a 49 5a 45 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 53 65  IZE;.}../*.** Se
31a0b 61 72 63 68 20 74 68 65 20 6e 6f 64 65 20 68 61  arch the node ha
31a0c 73 68 20 74 61 62 6c 65 20 66 6f 72 20 6e 6f 64  sh table for nod
31a0d 65 20 69 4e 6f 64 65 2e 20 49 66 20 66 6f 75 6e  e iNode. If foun
31a0e 64 2c 20 72 65 74 75 72 6e 20 61 20 70 6f 69 6e  d, return a poin
31a0f 74 65 72 0a 2a 2a 20 74 6f 20 69 74 2e 20 4f 74  ter.** to it. Ot
31a10 68 65 72 77 69 73 65 2c 20 72 65 74 75 72 6e 20  herwise, return 
31a11 30 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 52 74 72  0..*/.static Rtr
31a12 65 65 4e 6f 64 65 20 2a 6e 6f 64 65 48 61 73 68  eeNode *nodeHash
31a13 4c 6f 6f 6b 75 70 28 52 74 72 65 65 20 2a 70 52  Lookup(Rtree *pR
31a14 74 72 65 65 2c 20 69 36 34 20 69 4e 6f 64 65 29  tree, i64 iNode)
31a15 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70  {.  RtreeNode *p
31a16 3b 0a 20 20 61 73 73 65 72 74 28 20 69 4e 6f 64  ;.  assert( iNod
31a17 65 21 3d 30 20 29 3b 0a 20 20 66 6f 72 28 70 3d  e!=0 );.  for(p=
31a18 70 52 74 72 65 65 2d 3e 61 48 61 73 68 5b 6e 6f  pRtree->aHash[no
31a19 64 65 48 61 73 68 28 69 4e 6f 64 65 29 5d 3b 20  deHash(iNode)]; 
31a1a 70 20 26 26 20 70 2d 3e 69 4e 6f 64 65 21 3d 69  p && p->iNode!=i
31a1b 4e 6f 64 65 3b 20 70 3d 70 2d 3e 70 4e 65 78 74  Node; p=p->pNext
31a1c 29 3b 0a 20 20 72 65 74 75 72 6e 20 70 3b 0a 7d  );.  return p;.}
31a1d 0a 0a 2f 2a 0a 2a 2a 20 41 64 64 20 6e 6f 64 65  ../*.** Add node
31a1e 20 70 4e 6f 64 65 20 74 6f 20 74 68 65 20 6e 6f   pNode to the no
31a1f 64 65 20 68 61 73 68 20 74 61 62 6c 65 2e 0a 2a  de hash table..*
31a20 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 6e 6f  /.static void no
31a21 64 65 48 61 73 68 49 6e 73 65 72 74 28 52 74 72  deHashInsert(Rtr
31a22 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
31a23 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20  eNode *pNode){. 
31a24 20 69 66 28 20 70 4e 6f 64 65 20 29 7b 0a 20 20   if( pNode ){.  
31a25 20 20 69 6e 74 20 69 48 61 73 68 3b 0a 20 20 20    int iHash;.   
31a26 20 61 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e   assert( pNode->
31a27 70 4e 65 78 74 3d 3d 30 20 29 3b 0a 20 20 20 20  pNext==0 );.    
31a28 69 48 61 73 68 20 3d 20 6e 6f 64 65 48 61 73 68  iHash = nodeHash
31a29 28 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a  (pNode->iNode);.
31a2a 20 20 20 20 70 4e 6f 64 65 2d 3e 70 4e 65 78 74      pNode->pNext
31a2b 20 3d 20 70 52 74 72 65 65 2d 3e 61 48 61 73 68   = pRtree->aHash
31a2c 5b 69 48 61 73 68 5d 3b 0a 20 20 20 20 70 52 74  [iHash];.    pRt
31a2d 72 65 65 2d 3e 61 48 61 73 68 5b 69 48 61 73 68  ree->aHash[iHash
31a2e 5d 20 3d 20 70 4e 6f 64 65 3b 0a 20 20 7d 0a 7d  ] = pNode;.  }.}
31a2f 0a 0a 2f 2a 0a 2a 2a 20 52 65 6d 6f 76 65 20 6e  ../*.** Remove n
31a30 6f 64 65 20 70 4e 6f 64 65 20 66 72 6f 6d 20 74  ode pNode from t
31a31 68 65 20 6e 6f 64 65 20 68 61 73 68 20 74 61 62  he node hash tab
31a32 6c 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  le..*/.static vo
31a33 69 64 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74  id nodeHashDelet
31a34 65 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  e(Rtree *pRtree,
31a35 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
31a36 65 29 7b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20  e){.  RtreeNode 
31a37 2a 2a 70 70 3b 0a 20 20 69 66 28 20 70 4e 6f 64  **pp;.  if( pNod
31a38 65 2d 3e 69 4e 6f 64 65 21 3d 30 20 29 7b 0a 20  e->iNode!=0 ){. 
31a39 20 20 20 70 70 20 3d 20 26 70 52 74 72 65 65 2d     pp = &pRtree-
31a3a 3e 61 48 61 73 68 5b 6e 6f 64 65 48 61 73 68 28  >aHash[nodeHash(
31a3b 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 5d 3b 0a  pNode->iNode)];.
31a3c 20 20 20 20 66 6f 72 28 20 3b 20 28 2a 70 70 29      for( ; (*pp)
31a3d 21 3d 70 4e 6f 64 65 3b 20 70 70 20 3d 20 26 28  !=pNode; pp = &(
31a3e 2a 70 70 29 2d 3e 70 4e 65 78 74 29 7b 20 61 73  *pp)->pNext){ as
31a3f 73 65 72 74 28 2a 70 70 29 3b 20 7d 0a 20 20 20  sert(*pp); }.   
31a40 20 2a 70 70 20 3d 20 70 4e 6f 64 65 2d 3e 70 4e   *pp = pNode->pN
31a41 65 78 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e  ext;.    pNode->
31a42 70 4e 65 78 74 20 3d 20 30 3b 0a 20 20 7d 0a 7d  pNext = 0;.  }.}
31a43 0a 0a 2f 2a 0a 2a 2a 20 41 6c 6c 6f 63 61 74 65  ../*.** Allocate
31a44 20 61 6e 64 20 72 65 74 75 72 6e 20 6e 65 77 20   and return new 
31a45 72 2d 74 72 65 65 20 6e 6f 64 65 2e 20 49 6e 69  r-tree node. Ini
31a46 74 69 61 6c 6c 79 2c 20 28 52 74 72 65 65 4e 6f  tially, (RtreeNo
31a47 64 65 2e 69 4e 6f 64 65 3d 3d 30 29 2c 0a 2a 2a  de.iNode==0),.**
31a48 20 69 6e 64 69 63 61 74 69 6e 67 20 74 68 61 74   indicating that
31a49 20 6e 6f 64 65 20 68 61 73 20 6e 6f 74 20 79 65   node has not ye
31a4a 74 20 62 65 65 6e 20 61 73 73 69 67 6e 65 64 20  t been assigned 
31a4b 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 2e 20 49  a node number. I
31a4c 74 20 69 73 0a 2a 2a 20 61 73 73 69 67 6e 65 64  t is.** assigned
31a4d 20 61 20 6e 6f 64 65 20 6e 75 6d 62 65 72 20 77   a node number w
31a4e 68 65 6e 20 6e 6f 64 65 57 72 69 74 65 28 29 20  hen nodeWrite() 
31a4f 69 73 20 63 61 6c 6c 65 64 20 74 6f 20 77 72 69  is called to wri
31a50 74 65 20 74 68 65 0a 2a 2a 20 6e 6f 64 65 20 63  te the.** node c
31a51 6f 6e 74 65 6e 74 73 20 6f 75 74 20 74 6f 20 74  ontents out to t
31a52 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a 2f 0a  he database..*/.
31a53 73 74 61 74 69 63 20 52 74 72 65 65 4e 6f 64 65  static RtreeNode
31a54 20 2a 6e 6f 64 65 4e 65 77 28 52 74 72 65 65 20   *nodeNew(Rtree 
31a55 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f  *pRtree, RtreeNo
31a56 64 65 20 2a 70 50 61 72 65 6e 74 2c 20 69 6e 74  de *pParent, int
31a57 20 7a 65 72 6f 29 7b 0a 20 20 52 74 72 65 65 4e   zero){.  RtreeN
31a58 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 20 20 70 4e  ode *pNode;.  pN
31a59 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64 65  ode = (RtreeNode
31a5a 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
31a5b 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f  c(sizeof(RtreeNo
31a5c 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e  de) + pRtree->iN
31a5d 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  odeSize);.  if( 
31a5e 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 6d 65 6d  pNode ){.    mem
31a5f 73 65 74 28 70 4e 6f 64 65 2c 20 30 2c 20 73 69  set(pNode, 0, si
31a60 7a 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 20  zeof(RtreeNode) 
31a61 2b 20 28 7a 65 72 6f 3f 70 52 74 72 65 65 2d 3e  + (zero?pRtree->
31a62 69 4e 6f 64 65 53 69 7a 65 3a 30 29 29 3b 0a 20  iNodeSize:0));. 
31a63 20 20 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 20     pNode->zData 
31a64 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64 65 5b 31  = (u8 *)&pNode[1
31a65 5d 3b 0a 20 20 20 20 70 4e 6f 64 65 2d 3e 6e 52  ];.    pNode->nR
31a66 65 66 20 3d 20 31 3b 0a 20 20 20 20 70 4e 6f 64  ef = 1;.    pNod
31a67 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61  e->pParent = pPa
31a68 72 65 6e 74 3b 0a 20 20 20 20 70 4e 6f 64 65 2d  rent;.    pNode-
31a69 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20 20  >isDirty = 1;.  
31a6a 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65 28    nodeReference(
31a6b 70 50 61 72 65 6e 74 29 3b 0a 20 20 7d 0a 20 20  pParent);.  }.  
31a6c 72 65 74 75 72 6e 20 70 4e 6f 64 65 3b 0a 7d 0a  return pNode;.}.
31a6d 0a 2f 2a 0a 2a 2a 20 4f 62 74 61 69 6e 20 61 20  ./*.** Obtain a 
31a6e 72 65 66 65 72 65 6e 63 65 20 74 6f 20 61 6e 20  reference to an 
31a6f 72 2d 74 72 65 65 20 6e 6f 64 65 2e 0a 2a 2f 0a  r-tree node..*/.
31a70 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64 65 41  static int.nodeA
31a71 63 71 75 69 72 65 28 0a 20 20 52 74 72 65 65 20  cquire(.  Rtree 
31a72 2a 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20  *pRtree,        
31a73 20 20 20 20 20 2f 2a 20 52 2d 74 72 65 65 20 73       /* R-tree s
31a74 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 69 36  tructure */.  i6
31a75 34 20 69 4e 6f 64 65 2c 20 20 20 20 20 20 20 20  4 iNode,        
31a76 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 6f 64 65           /* Node
31a77 20 6e 75 6d 62 65 72 20 74 6f 20 6c 6f 61 64 20   number to load 
31a78 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  */.  RtreeNode *
31a79 70 50 61 72 65 6e 74 2c 20 20 20 20 20 20 20 20  pParent,        
31a7a 2f 2a 20 45 69 74 68 65 72 20 74 68 65 20 70 61  /* Either the pa
31a7b 72 65 6e 74 20 6e 6f 64 65 20 6f 72 20 4e 55 4c  rent node or NUL
31a7c 4c 20 2a 2f 0a 20 20 52 74 72 65 65 4e 6f 64 65  L */.  RtreeNode
31a7d 20 2a 2a 70 70 4e 6f 64 65 20 20 20 20 20 20 20   **ppNode       
31a7e 20 20 2f 2a 20 4f 55 54 3a 20 41 63 71 75 69 72    /* OUT: Acquir
31a7f 65 64 20 6e 6f 64 65 20 2a 2f 0a 29 7b 0a 20 20  ed node */.){.  
31a80 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65 4e  int rc;.  RtreeN
31a81 6f 64 65 20 2a 70 4e 6f 64 65 3b 0a 0a 20 20 2f  ode *pNode;..  /
31a82 2a 20 43 68 65 63 6b 20 69 66 20 74 68 65 20 72  * Check if the r
31a83 65 71 75 65 73 74 65 64 20 6e 6f 64 65 20 69 73  equested node is
31a84 20 61 6c 72 65 61 64 79 20 69 6e 20 74 68 65 20   already in the 
31a85 68 61 73 68 20 74 61 62 6c 65 2e 20 49 66 20 73  hash table. If s
31a86 6f 2c 0a 20 20 2a 2a 20 69 6e 63 72 65 61 73 65  o,.  ** increase
31a87 20 69 74 73 20 72 65 66 65 72 65 6e 63 65 20 63   its reference c
31a88 6f 75 6e 74 20 61 6e 64 20 72 65 74 75 72 6e 20  ount and return 
31a89 69 74 2e 0a 20 20 2a 2f 0a 20 20 69 66 28 20 28  it..  */.  if( (
31a8a 70 4e 6f 64 65 20 3d 20 6e 6f 64 65 48 61 73 68  pNode = nodeHash
31a8b 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20 69  Lookup(pRtree, i
31a8c 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 61 73  Node)) ){.    as
31a8d 73 65 72 74 28 20 21 70 50 61 72 65 6e 74 20 7c  sert( !pParent |
31a8e 7c 20 21 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e  | !pNode->pParen
31a8f 74 20 7c 7c 20 70 4e 6f 64 65 2d 3e 70 50 61 72  t || pNode->pPar
31a90 65 6e 74 3d 3d 70 50 61 72 65 6e 74 20 29 3b 0a  ent==pParent );.
31a91 20 20 20 20 69 66 28 20 70 50 61 72 65 6e 74 20      if( pParent 
31a92 29 7b 0a 20 20 20 20 20 20 70 4e 6f 64 65 2d 3e  ){.      pNode->
31a93 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65 6e  pParent = pParen
31a94 74 3b 0a 20 20 20 20 7d 0a 20 20 20 20 70 4e 6f  t;.    }.    pNo
31a95 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20 20 20 20  de->nRef++;.    
31a96 2a 70 70 4e 6f 64 65 20 3d 20 70 4e 6f 64 65 3b  *ppNode = pNode;
31a97 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
31a98 54 45 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 70 4e  TE_OK;.  }..  pN
31a99 6f 64 65 20 3d 20 28 52 74 72 65 65 4e 6f 64 65  ode = (RtreeNode
31a9a 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f   *)sqlite3_mallo
31a9b 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65 4e 6f  c(sizeof(RtreeNo
31a9c 64 65 29 20 2b 20 70 52 74 72 65 65 2d 3e 69 4e  de) + pRtree->iN
31a9d 6f 64 65 53 69 7a 65 29 3b 0a 20 20 69 66 28 20  odeSize);.  if( 
31a9e 21 70 4e 6f 64 65 20 29 7b 0a 20 20 20 20 2a 70  !pNode ){.    *p
31a9f 70 4e 6f 64 65 20 3d 20 30 3b 0a 20 20 20 20 72  pNode = 0;.    r
31aa0 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d  eturn SQLITE_NOM
31aa1 45 4d 3b 0a 20 20 7d 0a 20 20 70 4e 6f 64 65 2d  EM;.  }.  pNode-
31aa2 3e 70 50 61 72 65 6e 74 20 3d 20 70 50 61 72 65  >pParent = pPare
31aa3 6e 74 3b 0a 20 20 70 4e 6f 64 65 2d 3e 7a 44 61  nt;.  pNode->zDa
31aa4 74 61 20 3d 20 28 75 38 20 2a 29 26 70 4e 6f 64  ta = (u8 *)&pNod
31aa5 65 5b 31 5d 3b 0a 20 20 70 4e 6f 64 65 2d 3e 6e  e[1];.  pNode->n
31aa6 52 65 66 20 3d 20 31 3b 0a 20 20 70 4e 6f 64 65  Ref = 1;.  pNode
31aa7 2d 3e 69 4e 6f 64 65 20 3d 20 69 4e 6f 64 65 3b  ->iNode = iNode;
31aa8 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44 69 72 74  .  pNode->isDirt
31aa9 79 20 3d 20 30 3b 0a 20 20 70 4e 6f 64 65 2d 3e  y = 0;.  pNode->
31aaa 70 4e 65 78 74 20 3d 20 30 3b 0a 0a 20 20 73 71  pNext = 0;..  sq
31aab 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34  lite3_bind_int64
31aac 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f  (pRtree->pReadNo
31aad 64 65 2c 20 31 2c 20 69 4e 6f 64 65 29 3b 0a 20  de, 1, iNode);. 
31aae 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 73 74   rc = sqlite3_st
31aaf 65 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  ep(pRtree->pRead
31ab0 4e 6f 64 65 29 3b 0a 20 20 69 66 28 20 72 63 3d  Node);.  if( rc=
31ab1 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
31ab2 20 20 20 63 6f 6e 73 74 20 75 38 20 2a 7a 42 6c     const u8 *zBl
31ab3 6f 62 20 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c  ob = sqlite3_col
31ab4 75 6d 6e 5f 62 6c 6f 62 28 70 52 74 72 65 65 2d  umn_blob(pRtree-
31ab5 3e 70 52 65 61 64 4e 6f 64 65 2c 20 30 29 3b 0a  >pReadNode, 0);.
31ab6 20 20 20 20 6d 65 6d 63 70 79 28 70 4e 6f 64 65      memcpy(pNode
31ab7 2d 3e 7a 44 61 74 61 2c 20 7a 42 6c 6f 62 2c 20  ->zData, zBlob, 
31ab8 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69 7a  pRtree->iNodeSiz
31ab9 65 29 3b 0a 20 20 20 20 6e 6f 64 65 52 65 66 65  e);.    nodeRefe
31aba 72 65 6e 63 65 28 70 50 61 72 65 6e 74 29 3b 0a  rence(pParent);.
31abb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 73 71 6c    }else{.    sql
31abc 69 74 65 33 5f 66 72 65 65 28 70 4e 6f 64 65 29  ite3_free(pNode)
31abd 3b 0a 20 20 20 20 70 4e 6f 64 65 20 3d 20 30 3b  ;.    pNode = 0;
31abe 0a 20 20 7d 0a 0a 20 20 2a 70 70 4e 6f 64 65 20  .  }..  *ppNode 
31abf 3d 20 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20  = pNode;.  rc = 
31ac0 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
31ac1 74 72 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 29  tree->pReadNode)
31ac2 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  ;..  if( rc==SQL
31ac3 49 54 45 5f 4f 4b 20 26 26 20 69 4e 6f 64 65 3d  ITE_OK && iNode=
31ac4 3d 31 20 29 7b 0a 20 20 20 20 70 52 74 72 65 65  =1 ){.    pRtree
31ac5 2d 3e 69 44 65 70 74 68 20 3d 20 72 65 61 64 49  ->iDepth = readI
31ac6 6e 74 31 36 28 70 4e 6f 64 65 2d 3e 7a 44 61 74  nt16(pNode->zDat
31ac7 61 29 3b 0a 20 20 7d 0a 0a 20 20 61 73 73 65 72  a);.  }..  asser
31ac8 74 28 20 28 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  t( (rc==SQLITE_O
31ac9 4b 20 26 26 20 70 4e 6f 64 65 29 20 7c 7c 20 28  K && pNode) || (
31aca 70 4e 6f 64 65 3d 3d 30 20 26 26 20 72 63 21 3d  pNode==0 && rc!=
31acb 53 51 4c 49 54 45 5f 4f 4b 29 20 29 3b 0a 20 20  SQLITE_OK) );.  
31acc 6e 6f 64 65 48 61 73 68 49 6e 73 65 72 74 28 70  nodeHashInsert(p
31acd 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 0a  Rtree, pNode);..
31ace 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a    return rc;.}..
31acf 2f 2a 0a 2a 2a 20 4f 76 65 72 77 72 69 74 65 20  /*.** Overwrite 
31ad0 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  cell iCell of no
31ad1 64 65 20 70 4e 6f 64 65 20 77 69 74 68 20 74 68  de pNode with th
31ad2 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66 20 70 43  e contents of pC
31ad3 65 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76  ell..*/.static v
31ad4 6f 69 64 20 6e 6f 64 65 4f 76 65 72 77 72 69 74  oid nodeOverwrit
31ad5 65 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a  eCell(.  Rtree *
31ad6 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
31ad7 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20 0a 20  Node *pNode,  . 
31ad8 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
31ad9 6c 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c 6c 0a  l, .  int iCell.
31ada 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 75  ){.  int ii;.  u
31adb 38 20 2a 70 20 3d 20 26 70 4e 6f 64 65 2d 3e 7a  8 *p = &pNode->z
31adc 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65 2d  Data[4 + pRtree-
31add 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69  >nBytesPerCell*i
31ade 43 65 6c 6c 5d 3b 0a 20 20 70 20 2b 3d 20 77 72  Cell];.  p += wr
31adf 69 74 65 49 6e 74 36 34 28 70 2c 20 70 43 65 6c  iteInt64(p, pCel
31ae0 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a 20 20 66 6f  l->iRowid);.  fo
31ae1 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72  r(ii=0; ii<(pRtr
31ae2 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b  ee->nDim*2); ii+
31ae3 2b 29 7b 0a 20 20 20 20 70 20 2b 3d 20 77 72 69  +){.    p += wri
31ae4 74 65 43 6f 6f 72 64 28 70 2c 20 26 70 43 65 6c  teCoord(p, &pCel
31ae5 6c 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 29 3b 0a  l->aCoord[ii]);.
31ae6 20 20 7d 0a 20 20 70 4e 6f 64 65 2d 3e 69 73 44    }.  pNode->isD
31ae7 69 72 74 79 20 3d 20 31 3b 0a 7d 0a 0a 2f 2a 0a  irty = 1;.}../*.
31ae8 2a 2a 20 52 65 6d 6f 76 65 20 63 65 6c 6c 20 74  ** Remove cell t
31ae9 68 65 20 63 65 6c 6c 20 77 69 74 68 20 69 6e 64  he cell with ind
31aea 65 78 20 69 43 65 6c 6c 20 66 72 6f 6d 20 6e 6f  ex iCell from no
31aeb 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  de pNode..*/.sta
31aec 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 44 65 6c  tic void nodeDel
31aed 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 70  eteCell(Rtree *p
31aee 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
31aef 20 2a 70 4e 6f 64 65 2c 20 69 6e 74 20 69 43 65   *pNode, int iCe
31af0 6c 6c 29 7b 0a 20 20 75 38 20 2a 70 44 73 74 20  ll){.  u8 *pDst 
31af1 3d 20 26 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 5b  = &pNode->zData[
31af2 34 20 2b 20 70 52 74 72 65 65 2d 3e 6e 42 79 74  4 + pRtree->nByt
31af3 65 73 50 65 72 43 65 6c 6c 2a 69 43 65 6c 6c 5d  esPerCell*iCell]
31af4 3b 0a 20 20 75 38 20 2a 70 53 72 63 20 3d 20 26  ;.  u8 *pSrc = &
31af5 70 44 73 74 5b 70 52 74 72 65 65 2d 3e 6e 42 79  pDst[pRtree->nBy
31af6 74 65 73 50 65 72 43 65 6c 6c 5d 3b 0a 20 20 69  tesPerCell];.  i
31af7 6e 74 20 6e 42 79 74 65 20 3d 20 28 4e 43 45 4c  nt nByte = (NCEL
31af8 4c 28 70 4e 6f 64 65 29 20 2d 20 69 43 65 6c 6c  L(pNode) - iCell
31af9 20 2d 20 31 29 20 2a 20 70 52 74 72 65 65 2d 3e   - 1) * pRtree->
31afa 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20  nBytesPerCell;. 
31afb 20 6d 65 6d 6d 6f 76 65 28 70 44 73 74 2c 20 70   memmove(pDst, p
31afc 53 72 63 2c 20 6e 42 79 74 65 29 3b 0a 20 20 77  Src, nByte);.  w
31afd 72 69 74 65 49 6e 74 31 36 28 26 70 4e 6f 64 65  riteInt16(&pNode
31afe 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 4e 43 45 4c  ->zData[2], NCEL
31aff 4c 28 70 4e 6f 64 65 29 2d 31 29 3b 0a 20 20 70  L(pNode)-1);.  p
31b00 4e 6f 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20  Node->isDirty = 
31b01 31 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65  1;.}../*.** Inse
31b02 72 74 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20  rt the contents 
31b03 6f 66 20 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e  of cell pCell in
31b04 74 6f 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 49  to node pNode. I
31b05 66 20 74 68 65 20 69 6e 73 65 72 74 0a 2a 2a 20  f the insert.** 
31b06 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20 72  is successful, r
31b07 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
31b08 0a 2a 2a 0a 2a 2a 20 49 66 20 74 68 65 72 65 20  .**.** If there 
31b09 69 73 20 6e 6f 74 20 65 6e 6f 75 67 68 20 66 72  is not enough fr
31b0a 65 65 20 73 70 61 63 65 20 69 6e 20 70 4e 6f 64  ee space in pNod
31b0b 65 2c 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45  e, return SQLITE
31b0c 5f 46 55 4c 4c 2e 0a 2a 2f 0a 73 74 61 74 69 63  _FULL..*/.static
31b0d 20 69 6e 74 0a 6e 6f 64 65 49 6e 73 65 72 74 43   int.nodeInsertC
31b0e 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ell(.  Rtree *pR
31b0f 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f  tree, .  RtreeNo
31b10 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74  de *pNode, .  Rt
31b11 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 20 0a  reeCell *pCell .
31b12 29 7b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 3b 20  ){.  int nCell; 
31b13 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
31b14 20 20 20 2f 2a 20 43 75 72 72 65 6e 74 20 6e 75     /* Current nu
31b15 6d 62 65 72 20 6f 66 20 63 65 6c 6c 73 20 69 6e  mber of cells in
31b16 20 70 4e 6f 64 65 20 2a 2f 0a 20 20 69 6e 74 20   pNode */.  int 
31b17 6e 4d 61 78 43 65 6c 6c 3b 20 20 20 20 20 20 20  nMaxCell;       
31b18 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4d 61 78            /* Max
31b19 69 6d 75 6d 20 6e 75 6d 62 65 72 20 6f 66 20 63  imum number of c
31b1a 65 6c 6c 73 20 66 6f 72 20 70 4e 6f 64 65 20 2a  ells for pNode *
31b1b 2f 0a 0a 20 20 6e 4d 61 78 43 65 6c 6c 20 3d 20  /..  nMaxCell = 
31b1c 28 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69  (pRtree->iNodeSi
31b1d 7a 65 2d 34 29 2f 70 52 74 72 65 65 2d 3e 6e 42  ze-4)/pRtree->nB
31b1e 79 74 65 73 50 65 72 43 65 6c 6c 3b 0a 20 20 6e  ytesPerCell;.  n
31b1f 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
31b20 64 65 29 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e  de);..  assert(n
31b21 43 65 6c 6c 3c 3d 6e 4d 61 78 43 65 6c 6c 29 3b  Cell<=nMaxCell);
31b22 0a 0a 20 20 69 66 28 20 6e 43 65 6c 6c 3c 6e 4d  ..  if( nCell<nM
31b23 61 78 43 65 6c 6c 20 29 7b 0a 20 20 20 20 6e 6f  axCell ){.    no
31b24 64 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28  deOverwriteCell(
31b25 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70  pRtree, pNode, p
31b26 43 65 6c 6c 2c 20 6e 43 65 6c 6c 29 3b 0a 20 20  Cell, nCell);.  
31b27 20 20 77 72 69 74 65 49 6e 74 31 36 28 26 70 4e    writeInt16(&pN
31b28 6f 64 65 2d 3e 7a 44 61 74 61 5b 32 5d 2c 20 6e  ode->zData[2], n
31b29 43 65 6c 6c 2b 31 29 3b 0a 20 20 20 20 70 4e 6f  Cell+1);.    pNo
31b2a 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b  de->isDirty = 1;
31b2b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 28  .  }..  return (
31b2c 6e 43 65 6c 6c 3d 3d 6e 4d 61 78 43 65 6c 6c 29  nCell==nMaxCell)
31b2d 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 66 20 74 68  ;.}../*.** If th
31b2e 65 20 6e 6f 64 65 20 69 73 20 64 69 72 74 79 2c  e node is dirty,
31b2f 20 77 72 69 74 65 20 69 74 20 6f 75 74 20 74 6f   write it out to
31b30 20 74 68 65 20 64 61 74 61 62 61 73 65 2e 0a 2a   the database..*
31b31 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e 6f 64  /.static int.nod
31b32 65 57 72 69 74 65 28 52 74 72 65 65 20 2a 70 52  eWrite(Rtree *pR
31b33 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65 20  tree, RtreeNode 
31b34 2a 70 4e 6f 64 65 29 7b 0a 20 20 69 6e 74 20 72  *pNode){.  int r
31b35 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20  c = SQLITE_OK;. 
31b36 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 73 44 69   if( pNode->isDi
31b37 72 74 79 20 29 7b 0a 20 20 20 20 73 71 6c 69 74  rty ){.    sqlit
31b38 65 33 5f 73 74 6d 74 20 2a 70 20 3d 20 70 52 74  e3_stmt *p = pRt
31b39 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 3b  ree->pWriteNode;
31b3a 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e  .    if( pNode->
31b3b 69 4e 6f 64 65 20 29 7b 0a 20 20 20 20 20 20 73  iNode ){.      s
31b3c 71 6c 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36  qlite3_bind_int6
31b3d 34 28 70 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  4(p, 1, pNode->i
31b3e 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 65 6c 73 65  Node);.    }else
31b3f 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f  {.      sqlite3_
31b40 62 69 6e 64 5f 6e 75 6c 6c 28 70 2c 20 31 29 3b  bind_null(p, 1);
31b41 0a 20 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74  .    }.    sqlit
31b42 65 33 5f 62 69 6e 64 5f 62 6c 6f 62 28 70 2c 20  e3_bind_blob(p, 
31b43 32 2c 20 70 4e 6f 64 65 2d 3e 7a 44 61 74 61 2c  2, pNode->zData,
31b44 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
31b45 7a 65 2c 20 53 51 4c 49 54 45 5f 53 54 41 54 49  ze, SQLITE_STATI
31b46 43 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  C);.    sqlite3_
31b47 73 74 65 70 28 70 29 3b 0a 20 20 20 20 70 4e 6f  step(p);.    pNo
31b48 64 65 2d 3e 69 73 44 69 72 74 79 20 3d 20 30 3b  de->isDirty = 0;
31b49 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65  .    rc = sqlite
31b4a 33 5f 72 65 73 65 74 28 70 29 3b 0a 20 20 20 20  3_reset(p);.    
31b4b 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65  if( pNode->iNode
31b4c 3d 3d 30 20 26 26 20 72 63 3d 3d 53 51 4c 49 54  ==0 && rc==SQLIT
31b4d 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 4e  E_OK ){.      pN
31b4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 3d 20 73 71 6c  ode->iNode = sql
31b4f 69 74 65 33 5f 6c 61 73 74 5f 69 6e 73 65 72 74  ite3_last_insert
31b50 5f 72 6f 77 69 64 28 70 52 74 72 65 65 2d 3e 64  _rowid(pRtree->d
31b51 62 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 48 61  b);.      nodeHa
31b52 73 68 49 6e 73 65 72 74 28 70 52 74 72 65 65 2c  shInsert(pRtree,
31b53 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a 20   pNode);.    }. 
31b54 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a   }.  return rc;.
31b55 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 6c 65 61 73 65  }../*.** Release
31b56 20 61 20 72 65 66 65 72 65 6e 63 65 20 74 6f 20   a reference to 
31b57 61 20 6e 6f 64 65 2e 20 49 66 20 74 68 65 20 6e  a node. If the n
31b58 6f 64 65 20 69 73 20 64 69 72 74 79 20 61 6e 64  ode is dirty and
31b59 20 74 68 65 20 72 65 66 65 72 65 6e 63 65 0a 2a   the reference.*
31b5a 2a 20 63 6f 75 6e 74 20 64 72 6f 70 73 20 74 6f  * count drops to
31b5b 20 7a 65 72 6f 2c 20 74 68 65 20 6e 6f 64 65 20   zero, the node 
31b5c 64 61 74 61 20 69 73 20 77 72 69 74 74 65 6e 20  data is written 
31b5d 74 6f 20 74 68 65 20 64 61 74 61 62 61 73 65 2e  to the database.
31b5e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 0a 6e  .*/.static int.n
31b5f 6f 64 65 52 65 6c 65 61 73 65 28 52 74 72 65 65  odeRelease(Rtree
31b60 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65 65 4e   *pRtree, RtreeN
31b61 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 69  ode *pNode){.  i
31b62 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f  nt rc = SQLITE_O
31b63 4b 3b 0a 20 20 69 66 28 20 70 4e 6f 64 65 20 29  K;.  if( pNode )
31b64 7b 0a 20 20 20 20 61 73 73 65 72 74 28 20 70 4e  {.    assert( pN
31b65 6f 64 65 2d 3e 6e 52 65 66 3e 30 20 29 3b 0a 20  ode->nRef>0 );. 
31b66 20 20 20 70 4e 6f 64 65 2d 3e 6e 52 65 66 2d 2d     pNode->nRef--
31b67 3b 0a 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d  ;.    if( pNode-
31b68 3e 6e 52 65 66 3d 3d 30 20 29 7b 0a 20 20 20 20  >nRef==0 ){.    
31b69 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 69 4e 6f    if( pNode->iNo
31b6a 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20 20 20 20  de==1 ){.       
31b6b 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74 68 20   pRtree->iDepth 
31b6c 3d 20 2d 31 3b 0a 20 20 20 20 20 20 7d 0a 20 20  = -1;.      }.  
31b6d 20 20 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e 70      if( pNode->p
31b6e 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 20 20  Parent ){.      
31b6f 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61    rc = nodeRelea
31b70 73 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  se(pRtree, pNode
31b71 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20  ->pParent);.    
31b72 20 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 63    }.      if( rc
31b73 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
31b74 20 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65         rc = node
31b75 57 72 69 74 65 28 70 52 74 72 65 65 2c 20 70 4e  Write(pRtree, pN
31b76 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20  ode);.      }.  
31b77 20 20 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65      nodeHashDele
31b78 74 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  te(pRtree, pNode
31b79 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  );.      sqlite3
31b7a 5f 66 72 65 65 28 70 4e 6f 64 65 29 3b 0a 20 20  _free(pNode);.  
31b7b 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
31b7c 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
31b7d 74 75 72 6e 20 74 68 65 20 36 34 2d 62 69 74 20  turn the 64-bit 
31b7e 69 6e 74 65 67 65 72 20 76 61 6c 75 65 20 61 73  integer value as
31b7f 73 6f 63 69 61 74 65 64 20 77 69 74 68 20 63 65  sociated with ce
31b80 6c 6c 20 69 43 65 6c 6c 20 6f 66 0a 2a 2a 20 6e  ll iCell of.** n
31b81 6f 64 65 20 70 4e 6f 64 65 2e 20 49 66 20 70 4e  ode pNode. If pN
31b82 6f 64 65 20 69 73 20 61 20 6c 65 61 66 20 6e 6f  ode is a leaf no
31b83 64 65 2c 20 74 68 69 73 20 69 73 20 61 20 72 6f  de, this is a ro
31b84 77 69 64 2e 20 49 66 20 69 74 20 69 73 0a 2a 2a  wid. If it is.**
31b85 20 61 6e 20 69 6e 74 65 72 6e 61 6c 20 6e 6f 64   an internal nod
31b86 65 2c 20 74 68 65 6e 20 74 68 65 20 36 34 2d 62  e, then the 64-b
31b87 69 74 20 69 6e 74 65 67 65 72 20 69 73 20 61 20  it integer is a 
31b88 63 68 69 6c 64 20 70 61 67 65 20 6e 75 6d 62 65  child page numbe
31b89 72 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 36 34  r..*/.static i64
31b8a 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 0a 20   nodeGetRowid(. 
31b8b 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
31b8c 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e  .  RtreeNode *pN
31b8d 6f 64 65 2c 20 0a 20 20 69 6e 74 20 69 43 65 6c  ode, .  int iCel
31b8e 6c 0a 29 7b 0a 20 20 61 73 73 65 72 74 28 20 69  l.){.  assert( i
31b8f 43 65 6c 6c 3c 4e 43 45 4c 4c 28 70 4e 6f 64 65  Cell<NCELL(pNode
31b90 29 20 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 65  ) );.  return re
31b91 61 64 49 6e 74 36 34 28 26 70 4e 6f 64 65 2d 3e  adInt64(&pNode->
31b92 7a 44 61 74 61 5b 34 20 2b 20 70 52 74 72 65 65  zData[4 + pRtree
31b93 2d 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a  ->nBytesPerCell*
31b94 69 43 65 6c 6c 5d 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  iCell]);.}../*.*
31b95 2a 20 52 65 74 75 72 6e 20 63 6f 6f 72 64 69 6e  * Return coordin
31b96 61 74 65 20 69 43 6f 6f 72 64 20 66 72 6f 6d 20  ate iCoord from 
31b97 63 65 6c 6c 20 69 43 65 6c 6c 20 69 6e 20 6e 6f  cell iCell in no
31b98 64 65 20 70 4e 6f 64 65 2e 0a 2a 2f 0a 73 74 61  de pNode..*/.sta
31b99 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47 65 74  tic void nodeGet
31b9a 43 6f 6f 72 64 28 0a 20 20 52 74 72 65 65 20 2a  Coord(.  Rtree *
31b9b 70 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65  pRtree, .  Rtree
31b9c 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20  Node *pNode, .  
31b9d 69 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 69 6e 74  int iCell,.  int
31b9e 20 69 43 6f 6f 72 64 2c 0a 20 20 52 74 72 65 65   iCoord,.  Rtree
31b9f 43 6f 6f 72 64 20 2a 70 43 6f 6f 72 64 20 20 20  Coord *pCoord   
31ba0 20 20 20 20 20 20 20 20 2f 2a 20 53 70 61 63 65          /* Space
31ba1 20 74 6f 20 77 72 69 74 65 20 72 65 73 75 6c 74   to write result
31ba2 20 74 6f 20 2a 2f 0a 29 7b 0a 20 20 72 65 61 64   to */.){.  read
31ba3 43 6f 6f 72 64 28 26 70 4e 6f 64 65 2d 3e 7a 44  Coord(&pNode->zD
31ba4 61 74 61 5b 31 32 20 2b 20 70 52 74 72 65 65 2d  ata[12 + pRtree-
31ba5 3e 6e 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 69  >nBytesPerCell*i
31ba6 43 65 6c 6c 20 2b 20 34 2a 69 43 6f 6f 72 64 5d  Cell + 4*iCoord]
31ba7 2c 20 70 43 6f 6f 72 64 29 3b 0a 7d 0a 0a 2f 2a  , pCoord);.}../*
31ba8 0a 2a 2a 20 44 65 73 65 72 69 61 6c 69 7a 65 20  .** Deserialize 
31ba9 63 65 6c 6c 20 69 43 65 6c 6c 20 6f 66 20 6e 6f  cell iCell of no
31baa 64 65 20 70 4e 6f 64 65 2e 20 50 6f 70 75 6c 61  de pNode. Popula
31bab 74 65 20 74 68 65 20 73 74 72 75 63 74 75 72 65  te the structure
31bac 20 70 6f 69 6e 74 65 64 0a 2a 2a 20 74 6f 20 62   pointed.** to b
31bad 79 20 70 43 65 6c 6c 20 77 69 74 68 20 74 68 65  y pCell with the
31bae 20 72 65 73 75 6c 74 73 2e 0a 2a 2f 0a 73 74 61   results..*/.sta
31baf 74 69 63 20 76 6f 69 64 20 6e 6f 64 65 47 65 74  tic void nodeGet
31bb0 43 65 6c 6c 28 0a 20 20 52 74 72 65 65 20 2a 70  Cell(.  Rtree *p
31bb1 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 4e  Rtree, .  RtreeN
31bb2 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 69  ode *pNode, .  i
31bb3 6e 74 20 69 43 65 6c 6c 2c 0a 20 20 52 74 72 65  nt iCell,.  Rtre
31bb4 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 0a 29 7b 0a  eCell *pCell.){.
31bb5 20 20 69 6e 74 20 69 69 3b 0a 20 20 70 43 65 6c    int ii;.  pCel
31bb6 6c 2d 3e 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65  l->iRowid = node
31bb7 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c  GetRowid(pRtree,
31bb8 20 70 4e 6f 64 65 2c 20 69 43 65 6c 6c 29 3b 0a   pNode, iCell);.
31bb9 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
31bba 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 3b 20 69  Rtree->nDim*2; i
31bbb 69 2b 2b 29 7b 0a 20 20 20 20 6e 6f 64 65 47 65  i++){.    nodeGe
31bbc 74 43 6f 6f 72 64 28 70 52 74 72 65 65 2c 20 70  tCoord(pRtree, p
31bbd 4e 6f 64 65 2c 20 69 43 65 6c 6c 2c 20 69 69 2c  Node, iCell, ii,
31bbe 20 26 70 43 65 6c 6c 2d 3e 61 43 6f 6f 72 64 5b   &pCell->aCoord[
31bbf 69 69 5d 29 3b 0a 20 20 7d 0a 7d 0a 0a 0a 2f 2a  ii]);.  }.}.../*
31bc0 20 46 6f 72 77 61 72 64 20 64 65 63 6c 61 72 61   Forward declara
31bc1 74 69 6f 6e 20 66 6f 72 20 74 68 65 20 66 75 6e  tion for the fun
31bc2 63 74 69 6f 6e 20 74 68 61 74 20 64 6f 65 73 20  ction that does 
31bc3 74 68 65 20 77 6f 72 6b 20 6f 66 0a 2a 2a 20 74  the work of.** t
31bc4 68 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65  he virtual table
31bc5 20 6d 6f 64 75 6c 65 20 78 43 72 65 61 74 65 28   module xCreate(
31bc6 29 20 61 6e 64 20 78 43 6f 6e 6e 65 63 74 28 29  ) and xConnect()
31bc7 20 6d 65 74 68 6f 64 73 2e 0a 2a 2f 0a 73 74 61   methods..*/.sta
31bc8 74 69 63 20 69 6e 74 20 72 74 72 65 65 49 6e 69  tic int rtreeIni
31bc9 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 2c 20  t(.  sqlite3 *, 
31bca 76 6f 69 64 20 2a 2c 20 69 6e 74 2c 20 63 6f 6e  void *, int, con
31bcb 73 74 20 63 68 61 72 20 2a 63 6f 6e 73 74 2a 2c  st char *const*,
31bcc 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 2a   sqlite3_vtab **
31bcd 2c 20 63 68 61 72 20 2a 2a 2c 20 69 6e 74 0a 29  , char **, int.)
31bce 3b 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20  ;../* .** Rtree 
31bcf 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
31bd0 64 75 6c 65 20 78 43 72 65 61 74 65 20 6d 65 74  dule xCreate met
31bd1 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
31bd2 6e 74 20 72 74 72 65 65 43 72 65 61 74 65 28 0a  nt rtreeCreate(.
31bd3 20 20 73 71 6c 69 74 65 33 20 2a 64 62 2c 0a 20    sqlite3 *db,. 
31bd4 20 76 6f 69 64 20 2a 70 41 75 78 2c 0a 20 20 69   void *pAux,.  i
31bd5 6e 74 20 61 72 67 63 2c 20 63 6f 6e 73 74 20 63  nt argc, const c
31bd6 68 61 72 20 2a 63 6f 6e 73 74 2a 61 72 67 76 2c  har *const*argv,
31bd7 0a 20 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  .  sqlite3_vtab 
31bd8 2a 2a 70 70 56 74 61 62 2c 0a 20 20 63 68 61 72  **ppVtab,.  char
31bd9 20 2a 2a 70 7a 45 72 72 0a 29 7b 0a 20 20 72 65   **pzErr.){.  re
31bda 74 75 72 6e 20 72 74 72 65 65 49 6e 69 74 28 64  turn rtreeInit(d
31bdb 62 2c 20 70 41 75 78 2c 20 61 72 67 63 2c 20 61  b, pAux, argc, a
31bdc 72 67 76 2c 20 70 70 56 74 61 62 2c 20 70 7a 45  rgv, ppVtab, pzE
31bdd 72 72 2c 20 31 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a  rr, 1);.}../* .*
31bde 2a 20 52 74 72 65 65 20 76 69 72 74 75 61 6c 20  * Rtree virtual 
31bdf 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78 43 6f  table module xCo
31be0 6e 6e 65 63 74 20 6d 65 74 68 6f 64 2e 0a 2a 2f  nnect method..*/
31be1 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65  .static int rtre
31be2 65 43 6f 6e 6e 65 63 74 28 0a 20 20 73 71 6c 69  eConnect(.  sqli
31be3 74 65 33 20 2a 64 62 2c 0a 20 20 76 6f 69 64 20  te3 *db,.  void 
31be4 2a 70 41 75 78 2c 0a 20 20 69 6e 74 20 61 72 67  *pAux,.  int arg
31be5 63 2c 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63  c, const char *c
31be6 6f 6e 73 74 2a 61 72 67 76 2c 0a 20 20 73 71 6c  onst*argv,.  sql
31be7 69 74 65 33 5f 76 74 61 62 20 2a 2a 70 70 56 74  ite3_vtab **ppVt
31be8 61 62 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45  ab,.  char **pzE
31be9 72 72 0a 29 7b 0a 20 20 72 65 74 75 72 6e 20 72  rr.){.  return r
31bea 74 72 65 65 49 6e 69 74 28 64 62 2c 20 70 41 75  treeInit(db, pAu
31beb 78 2c 20 61 72 67 63 2c 20 61 72 67 76 2c 20 70  x, argc, argv, p
31bec 70 56 74 61 62 2c 20 70 7a 45 72 72 2c 20 30 29  pVtab, pzErr, 0)
31bed 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 63 72 65  ;.}../*.** Incre
31bee 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65 65 20  ment the r-tree 
31bef 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
31bf0 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
31bf1 72 74 72 65 65 52 65 66 65 72 65 6e 63 65 28 52  rtreeReference(R
31bf2 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a 20  tree *pRtree){. 
31bf3 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 2b 2b   pRtree->nBusy++
31bf4 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 44 65 63 72 65  ;.}../*.** Decre
31bf5 6d 65 6e 74 20 74 68 65 20 72 2d 74 72 65 65 20  ment the r-tree 
31bf6 72 65 66 65 72 65 6e 63 65 20 63 6f 75 6e 74 2e  reference count.
31bf7 20 57 68 65 6e 20 74 68 65 20 72 65 66 65 72 65   When the refere
31bf8 6e 63 65 20 63 6f 75 6e 74 20 72 65 61 63 68 65  nce count reache
31bf9 73 0a 2a 2a 20 7a 65 72 6f 20 74 68 65 20 73 74  s.** zero the st
31bfa 72 75 63 74 75 72 65 20 69 73 20 64 65 6c 65 74  ructure is delet
31bfb 65 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f  ed..*/.static vo
31bfc 69 64 20 72 74 72 65 65 52 65 6c 65 61 73 65 28  id rtreeRelease(
31bfd 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a  Rtree *pRtree){.
31bfe 20 20 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 2d    pRtree->nBusy-
31bff 2d 3b 0a 20 20 69 66 28 20 70 52 74 72 65 65 2d  -;.  if( pRtree-
31c00 3e 6e 42 75 73 79 3d 3d 30 20 29 7b 0a 20 20 20  >nBusy==0 ){.   
31c01 20 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a   sqlite3_finaliz
31c02 65 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 4e  e(pRtree->pReadN
31c03 6f 64 65 29 3b 0a 20 20 20 20 73 71 6c 69 74 65  ode);.    sqlite
31c04 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65  3_finalize(pRtre
31c05 65 2d 3e 70 57 72 69 74 65 4e 6f 64 65 29 3b 0a  e->pWriteNode);.
31c06 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
31c07 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
31c08 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 20 20 73  leteNode);.    s
31c09 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28  qlite3_finalize(
31c0a 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77  pRtree->pReadRow
31c0b 69 64 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33  id);.    sqlite3
31c0c 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65  _finalize(pRtree
31c0d 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
31c0e 20 20 20 20 73 71 6c 69 74 65 33 5f 66 69 6e 61      sqlite3_fina
31c0f 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e 70 44 65  lize(pRtree->pDe
31c10 6c 65 74 65 52 6f 77 69 64 29 3b 0a 20 20 20 20  leteRowid);.    
31c11 73 71 6c 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65  sqlite3_finalize
31c12 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61  (pRtree->pReadPa
31c13 72 65 6e 74 29 3b 0a 20 20 20 20 73 71 6c 69 74  rent);.    sqlit
31c14 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 52 74 72  e3_finalize(pRtr
31c15 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
31c16 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66  );.    sqlite3_f
31c17 69 6e 61 6c 69 7a 65 28 70 52 74 72 65 65 2d 3e  inalize(pRtree->
31c18 70 44 65 6c 65 74 65 50 61 72 65 6e 74 29 3b 0a  pDeleteParent);.
31c19 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
31c1a 28 70 52 74 72 65 65 29 3b 0a 20 20 7d 0a 7d 0a  (pRtree);.  }.}.
31c1b 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
31c1c 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
31c1d 6c 65 20 78 44 69 73 63 6f 6e 6e 65 63 74 20 6d  le xDisconnect m
31c1e 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
31c1f 20 69 6e 74 20 72 74 72 65 65 44 69 73 63 6f 6e   int rtreeDiscon
31c20 6e 65 63 74 28 73 71 6c 69 74 65 33 5f 76 74 61  nect(sqlite3_vta
31c21 62 20 2a 70 56 74 61 62 29 7b 0a 20 20 72 74 72  b *pVtab){.  rtr
31c22 65 65 52 65 6c 65 61 73 65 28 28 52 74 72 65 65  eeRelease((Rtree
31c23 20 2a 29 70 56 74 61 62 29 3b 0a 20 20 72 65 74   *)pVtab);.  ret
31c24 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d  urn SQLITE_OK;.}
31c25 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76  ../* .** Rtree v
31c26 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64  irtual table mod
31c27 75 6c 65 20 78 44 65 73 74 72 6f 79 20 6d 65 74  ule xDestroy met
31c28 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  hod..*/.static i
31c29 6e 74 20 72 74 72 65 65 44 65 73 74 72 6f 79 28  nt rtreeDestroy(
31c2a 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a 70 56  sqlite3_vtab *pV
31c2b 74 61 62 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  tab){.  Rtree *p
31c2c 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
31c2d 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72 63  )pVtab;.  int rc
31c2e 3b 0a 20 20 63 68 61 72 20 2a 7a 43 72 65 61 74  ;.  char *zCreat
31c2f 65 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  e = sqlite3_mpri
31c30 6e 74 66 28 0a 20 20 20 20 22 44 52 4f 50 20 54  ntf(.    "DROP T
31c31 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 6e 6f  ABLE '%q'.'%q_no
31c32 64 65 27 3b 22 0a 20 20 20 20 22 44 52 4f 50 20  de';".    "DROP 
31c33 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71 5f 72  TABLE '%q'.'%q_r
31c34 6f 77 69 64 27 3b 22 0a 20 20 20 20 22 44 52 4f  owid';".    "DRO
31c35 50 20 54 41 42 4c 45 20 27 25 71 27 2e 27 25 71  P TABLE '%q'.'%q
31c36 5f 70 61 72 65 6e 74 27 3b 22 2c 0a 20 20 20 20  _parent';",.    
31c37 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74  pRtree->zDb, pRt
31c38 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 0a 20 20 20  ree->zName, .   
31c39 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
31c3a 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 0a 20 20 20  tree->zName,.   
31c3b 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52   pRtree->zDb, pR
31c3c 74 72 65 65 2d 3e 7a 4e 61 6d 65 0a 20 20 29 3b  tree->zName.  );
31c3d 0a 20 20 69 66 28 20 21 7a 43 72 65 61 74 65 20  .  if( !zCreate 
31c3e 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49  ){.    rc = SQLI
31c3f 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 65 6c 73  TE_NOMEM;.  }els
31c40 65 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  e{.    rc = sqli
31c41 74 65 33 5f 65 78 65 63 28 70 52 74 72 65 65 2d  te3_exec(pRtree-
31c42 3e 64 62 2c 20 7a 43 72 65 61 74 65 2c 20 30 2c  >db, zCreate, 0,
31c43 20 30 2c 20 30 29 3b 0a 20 20 20 20 73 71 6c 69   0, 0);.    sqli
31c44 74 65 33 5f 66 72 65 65 28 7a 43 72 65 61 74 65  te3_free(zCreate
31c45 29 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  );.  }.  if( rc=
31c46 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
31c47 20 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 70    rtreeRelease(p
31c48 52 74 72 65 65 29 3b 0a 20 20 7d 0a 0a 20 20 72  Rtree);.  }..  r
31c49 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 20  eturn rc;.}../* 
31c4a 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
31c4b 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
31c4c 4f 70 65 6e 20 6d 65 74 68 6f 64 2e 0a 2a 2f 0a  Open method..*/.
31c4d 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
31c4e 4f 70 65 6e 28 73 71 6c 69 74 65 33 5f 76 74 61  Open(sqlite3_vta
31c4f 62 20 2a 70 56 54 61 62 2c 20 73 71 6c 69 74 65  b *pVTab, sqlite
31c50 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 2a  3_vtab_cursor **
31c51 70 70 43 75 72 73 6f 72 29 7b 0a 20 20 69 6e 74  ppCursor){.  int
31c52 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d   rc = SQLITE_NOM
31c53 45 4d 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  EM;.  RtreeCurso
31c54 72 20 2a 70 43 73 72 3b 0a 0a 20 20 70 43 73 72  r *pCsr;..  pCsr
31c55 20 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20   = (RtreeCursor 
31c56 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  *)sqlite3_malloc
31c57 28 73 69 7a 65 6f 66 28 52 74 72 65 65 43 75 72  (sizeof(RtreeCur
31c58 73 6f 72 29 29 3b 0a 20 20 69 66 28 20 70 43 73  sor));.  if( pCs
31c59 72 20 29 7b 0a 20 20 20 20 6d 65 6d 73 65 74 28  r ){.    memset(
31c5a 70 43 73 72 2c 20 30 2c 20 73 69 7a 65 6f 66 28  pCsr, 0, sizeof(
31c5b 52 74 72 65 65 43 75 72 73 6f 72 29 29 3b 0a 20  RtreeCursor));. 
31c5c 20 20 20 70 43 73 72 2d 3e 62 61 73 65 2e 70 56     pCsr->base.pV
31c5d 74 61 62 20 3d 20 70 56 54 61 62 3b 0a 20 20 20  tab = pVTab;.   
31c5e 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b   rc = SQLITE_OK;
31c5f 0a 20 20 7d 0a 20 20 2a 70 70 43 75 72 73 6f 72  .  }.  *ppCursor
31c60 20 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62   = (sqlite3_vtab
31c61 5f 63 75 72 73 6f 72 20 2a 29 70 43 73 72 3b 0a  _cursor *)pCsr;.
31c62 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
31c63 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69  ./* .** Rtree vi
31c64 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75  rtual table modu
31c65 6c 65 20 78 43 6c 6f 73 65 20 6d 65 74 68 6f 64  le xClose method
31c66 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
31c67 72 74 72 65 65 43 6c 6f 73 65 28 73 71 6c 69 74  rtreeClose(sqlit
31c68 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20 2a  e3_vtab_cursor *
31c69 63 75 72 29 7b 0a 20 20 52 74 72 65 65 20 2a 70  cur){.  Rtree *p
31c6a 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a  Rtree = (Rtree *
31c6b 29 28 63 75 72 2d 3e 70 56 74 61 62 29 3b 0a 20  )(cur->pVtab);. 
31c6c 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
31c6d 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28  Cursor *pCsr = (
31c6e 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29 63 75  RtreeCursor *)cu
31c6f 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  r;.  sqlite3_fre
31c70 65 28 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61  e(pCsr->aConstra
31c71 69 6e 74 29 3b 0a 20 20 72 63 20 3d 20 6e 6f 64  int);.  rc = nod
31c72 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
31c73 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b 0a 20   pCsr->pNode);. 
31c74 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 43   sqlite3_free(pC
31c75 73 72 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  sr);.  return rc
31c76 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65  ;.}../*.** Rtree
31c77 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
31c78 6f 64 75 6c 65 20 78 45 6f 66 20 6d 65 74 68 6f  odule xEof metho
31c79 64 2e 0a 2a 2a 0a 2a 2a 20 52 65 74 75 72 6e 20  d..**.** Return 
31c7a 6e 6f 6e 2d 7a 65 72 6f 20 69 66 20 74 68 65 20  non-zero if the 
31c7b 63 75 72 73 6f 72 20 64 6f 65 73 20 6e 6f 74 20  cursor does not 
31c7c 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e 74 20  currently point 
31c7d 74 6f 20 61 20 76 61 6c 69 64 20 0a 2a 2a 20 72  to a valid .** r
31c7e 65 63 6f 72 64 20 28 69 2e 65 20 69 66 20 74 68  ecord (i.e if th
31c7f 65 20 73 63 61 6e 20 68 61 73 20 66 69 6e 69 73  e scan has finis
31c80 68 65 64 29 2c 20 6f 72 20 7a 65 72 6f 20 6f 74  hed), or zero ot
31c81 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74 61 74  herwise..*/.stat
31c82 69 63 20 69 6e 74 20 72 74 72 65 65 45 6f 66 28  ic int rtreeEof(
31c83 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72  sqlite3_vtab_cur
31c84 73 6f 72 20 2a 63 75 72 29 7b 0a 20 20 52 74 72  sor *cur){.  Rtr
31c85 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20 3d  eeCursor *pCsr =
31c86 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a 29   (RtreeCursor *)
31c87 63 75 72 3b 0a 20 20 72 65 74 75 72 6e 20 28 70  cur;.  return (p
31c88 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 30 29 3b 0a  Csr->pNode==0);.
31c89 7d 0a 0a 2f 2a 20 0a 2a 2a 20 43 75 72 73 6f 72  }../* .** Cursor
31c8a 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
31c8b 6c 79 20 70 6f 69 6e 74 73 20 74 6f 20 61 20 63  ly points to a c
31c8c 65 6c 6c 20 69 6e 20 61 20 6e 6f 6e 2d 6c 65 61  ell in a non-lea
31c8d 66 20 70 61 67 65 2e 0a 2a 2a 20 52 65 74 75 72  f page..** Retur
31c8e 6e 20 74 72 75 65 20 69 66 20 74 68 65 20 73 75  n true if the su
31c8f 62 2d 74 72 65 65 20 68 65 61 64 65 64 20 62 79  b-tree headed by
31c90 20 74 68 65 20 63 65 6c 6c 20 69 73 20 66 69 6c   the cell is fil
31c91 74 65 72 65 64 0a 2a 2a 20 28 65 78 63 6c 75 64  tered.** (exclud
31c92 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e 73 74  ed) by the const
31c93 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20 70 43  raints in the pC
31c94 75 72 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69  ursor->aConstrai
31c95 6e 74 5b 5d 20 0a 2a 2a 20 61 72 72 61 79 2c 20  nt[] .** array, 
31c96 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72 77 69  or false otherwi
31c97 73 65 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  se..*/.static in
31c98 74 20 74 65 73 74 52 74 72 65 65 43 65 6c 6c 28  t testRtreeCell(
31c99 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
31c9a 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43 75 72  treeCursor *pCur
31c9b 73 6f 72 29 7b 0a 20 20 52 74 72 65 65 43 65 6c  sor){.  RtreeCel
31c9c 6c 20 63 65 6c 6c 3b 0a 20 20 69 6e 74 20 69 69  l cell;.  int ii
31c9d 3b 0a 20 20 69 6e 74 20 62 52 65 73 20 3d 20 30  ;.  int bRes = 0
31c9e 3b 0a 0a 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  ;..  nodeGetCell
31c9f 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
31ca0 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f 72  ->pNode, pCursor
31ca1 2d 3e 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b  ->iCell, &cell);
31ca2 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 62 52 65  .  for(ii=0; bRe
31ca3 73 3d 3d 30 20 26 26 20 69 69 3c 70 43 75 72 73  s==0 && ii<pCurs
31ca4 6f 72 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 3b  or->nConstraint;
31ca5 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72 65   ii++){.    Rtre
31ca6 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70 20 3d  eConstraint *p =
31ca7 20 26 70 43 75 72 73 6f 72 2d 3e 61 43 6f 6e 73   &pCursor->aCons
31ca8 74 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20  traint[ii];.    
31ca9 64 6f 75 62 6c 65 20 63 65 6c 6c 5f 6d 69 6e 20  double cell_min 
31caa 3d 20 44 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43  = DCOORD(cell.aC
31cab 6f 6f 72 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e  oord[(p->iCoord>
31cac 3e 31 29 2a 32 5d 29 3b 0a 20 20 20 20 64 6f 75  >1)*2]);.    dou
31cad 62 6c 65 20 63 65 6c 6c 5f 6d 61 78 20 3d 20 44  ble cell_max = D
31cae 43 4f 4f 52 44 28 63 65 6c 6c 2e 61 43 6f 6f 72  COORD(cell.aCoor
31caf 64 5b 28 70 2d 3e 69 43 6f 6f 72 64 3e 3e 31 29  d[(p->iCoord>>1)
31cb0 2a 32 2b 31 5d 29 3b 0a 0a 20 20 20 20 61 73 73  *2+1]);..    ass
31cb1 65 72 74 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45  ert(p->op==RTREE
31cb2 5f 4c 45 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LE || p->op==RT
31cb3 52 45 45 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d  REE_LT || p->op=
31cb4 3d 52 54 52 45 45 5f 47 45 20 0a 20 20 20 20 20  =RTREE_GE .     
31cb5 20 20 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52     || p->op==RTR
31cb6 45 45 5f 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d  EE_GT || p->op==
31cb7 52 54 52 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a  RTREE_EQ.    );.
31cb8 0a 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e  .    switch( p->
31cb9 6f 70 20 29 7b 0a 20 20 20 20 20 20 63 61 73 65  op ){.      case
31cba 20 52 54 52 45 45 5f 4c 45 3a 20 63 61 73 65 20   RTREE_LE: case 
31cbb 52 54 52 45 45 5f 4c 54 3a 20 62 52 65 73 20 3d  RTREE_LT: bRes =
31cbc 20 70 2d 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f   p->rValue<cell_
31cbd 6d 69 6e 3b 20 62 72 65 61 6b 3b 0a 20 20 20 20  min; break;.    
31cbe 20 20 63 61 73 65 20 52 54 52 45 45 5f 47 45 3a    case RTREE_GE:
31cbf 20 63 61 73 65 20 52 54 52 45 45 5f 47 54 3a 20   case RTREE_GT: 
31cc0 62 52 65 73 20 3d 20 70 2d 3e 72 56 61 6c 75 65  bRes = p->rValue
31cc1 3e 63 65 6c 6c 5f 6d 61 78 3b 20 62 72 65 61 6b  >cell_max; break
31cc2 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  ;.      case RTR
31cc3 45 45 5f 45 51 3a 20 0a 20 20 20 20 20 20 20 20  EE_EQ: .        
31cc4 62 52 65 73 20 3d 20 28 70 2d 3e 72 56 61 6c 75  bRes = (p->rValu
31cc5 65 3e 63 65 6c 6c 5f 6d 61 78 20 7c 7c 20 70 2d  e>cell_max || p-
31cc6 3e 72 56 61 6c 75 65 3c 63 65 6c 6c 5f 6d 69 6e  >rValue<cell_min
31cc7 29 3b 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b  );.        break
31cc8 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72  ;.    }.  }..  r
31cc9 65 74 75 72 6e 20 62 52 65 73 3b 0a 7d 0a 0a 2f  eturn bRes;.}../
31cca 2a 20 0a 2a 2a 20 52 65 74 75 72 6e 20 74 72 75  * .** Return tru
31ccb 65 20 69 66 20 74 68 65 20 63 65 6c 6c 20 74 68  e if the cell th
31ccc 61 74 20 63 75 72 73 6f 72 20 70 43 75 72 73 6f  at cursor pCurso
31ccd 72 20 63 75 72 72 65 6e 74 6c 79 20 70 6f 69 6e  r currently poin
31cce 74 73 20 74 6f 0a 2a 2a 20 77 6f 75 6c 64 20 62  ts to.** would b
31ccf 65 20 66 69 6c 74 65 72 65 64 20 28 65 78 63 6c  e filtered (excl
31cd0 75 64 65 64 29 20 62 79 20 74 68 65 20 63 6f 6e  uded) by the con
31cd1 73 74 72 61 69 6e 74 73 20 69 6e 20 74 68 65 20  straints in the 
31cd2 0a 2a 2a 20 70 43 75 72 73 6f 72 2d 3e 61 43 6f  .** pCursor->aCo
31cd3 6e 73 74 72 61 69 6e 74 5b 5d 20 61 72 72 61 79  nstraint[] array
31cd4 2c 20 6f 72 20 66 61 6c 73 65 20 6f 74 68 65 72  , or false other
31cd5 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 54 68 69 73  wise..**.** This
31cd6 20 66 75 6e 63 74 69 6f 6e 20 61 73 73 75 6d 65   function assume
31cd7 73 20 74 68 61 74 20 74 68 65 20 63 65 6c 6c 20  s that the cell 
31cd8 69 73 20 70 61 72 74 20 6f 66 20 61 20 6c 65 61  is part of a lea
31cd9 66 20 6e 6f 64 65 2e 0a 2a 2f 0a 73 74 61 74 69  f node..*/.stati
31cda 63 20 69 6e 74 20 74 65 73 74 52 74 72 65 65 45  c int testRtreeE
31cdb 6e 74 72 79 28 52 74 72 65 65 20 2a 70 52 74 72  ntry(Rtree *pRtr
31cdc 65 65 2c 20 52 74 72 65 65 43 75 72 73 6f 72 20  ee, RtreeCursor 
31cdd 2a 70 43 75 72 73 6f 72 29 7b 0a 20 20 52 74 72  *pCursor){.  Rtr
31cde 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 69  eeCell cell;.  i
31cdf 6e 74 20 69 69 3b 0a 0a 20 20 6e 6f 64 65 47 65  nt ii;..  nodeGe
31ce0 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 43  tCell(pRtree, pC
31ce1 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 70 43  ursor->pNode, pC
31ce2 75 72 73 6f 72 2d 3e 69 43 65 6c 6c 2c 20 26 63  ursor->iCell, &c
31ce3 65 6c 6c 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30  ell);.  for(ii=0
31ce4 3b 20 69 69 3c 70 43 75 72 73 6f 72 2d 3e 6e 43  ; ii<pCursor->nC
31ce5 6f 6e 73 74 72 61 69 6e 74 3b 20 69 69 2b 2b 29  onstraint; ii++)
31ce6 7b 0a 20 20 20 20 52 74 72 65 65 43 6f 6e 73 74  {.    RtreeConst
31ce7 72 61 69 6e 74 20 2a 70 20 3d 20 26 70 43 75 72  raint *p = &pCur
31ce8 73 6f 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  sor->aConstraint
31ce9 5b 69 69 5d 3b 0a 20 20 20 20 64 6f 75 62 6c 65  [ii];.    double
31cea 20 63 6f 6f 72 64 20 3d 20 44 43 4f 4f 52 44 28   coord = DCOORD(
31ceb 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 70 2d 3e 69  cell.aCoord[p->i
31cec 43 6f 6f 72 64 5d 29 3b 0a 20 20 20 20 69 6e 74  Coord]);.    int
31ced 20 72 65 73 3b 0a 20 20 20 20 61 73 73 65 72 74   res;.    assert
31cee 28 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f 4c 45  (p->op==RTREE_LE
31cef 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45   || p->op==RTREE
31cf0 5f 4c 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54  _LT || p->op==RT
31cf1 52 45 45 5f 47 45 20 0a 20 20 20 20 20 20 20 20  REE_GE .        
31cf2 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52 45 45 5f  || p->op==RTREE_
31cf3 47 54 20 7c 7c 20 70 2d 3e 6f 70 3d 3d 52 54 52  GT || p->op==RTR
31cf4 45 45 5f 45 51 0a 20 20 20 20 29 3b 0a 20 20 20  EE_EQ.    );.   
31cf5 20 73 77 69 74 63 68 28 20 70 2d 3e 6f 70 20 29   switch( p->op )
31cf6 7b 0a 20 20 20 20 20 20 63 61 73 65 20 52 54 52  {.      case RTR
31cf7 45 45 5f 4c 45 3a 20 72 65 73 20 3d 20 28 63 6f  EE_LE: res = (co
31cf8 6f 72 64 3c 3d 70 2d 3e 72 56 61 6c 75 65 29 3b  ord<=p->rValue);
31cf9 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61   break;.      ca
31cfa 73 65 20 52 54 52 45 45 5f 4c 54 3a 20 72 65 73  se RTREE_LT: res
31cfb 20 3d 20 28 63 6f 6f 72 64 3c 70 2d 3e 72 56 61   = (coord<p->rVa
31cfc 6c 75 65 29 3b 20 20 62 72 65 61 6b 3b 0a 20 20  lue);  break;.  
31cfd 20 20 20 20 63 61 73 65 20 52 54 52 45 45 5f 47      case RTREE_G
31cfe 45 3a 20 72 65 73 20 3d 20 28 63 6f 6f 72 64 3e  E: res = (coord>
31cff 3d 70 2d 3e 72 56 61 6c 75 65 29 3b 20 62 72 65  =p->rValue); bre
31d00 61 6b 3b 0a 20 20 20 20 20 20 63 61 73 65 20 52  ak;.      case R
31d01 54 52 45 45 5f 47 54 3a 20 72 65 73 20 3d 20 28  TREE_GT: res = (
31d02 63 6f 6f 72 64 3e 70 2d 3e 72 56 61 6c 75 65 29  coord>p->rValue)
31d03 3b 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20  ;  break;.      
31d04 63 61 73 65 20 52 54 52 45 45 5f 45 51 3a 20 72  case RTREE_EQ: r
31d05 65 73 20 3d 20 28 63 6f 6f 72 64 3d 3d 70 2d 3e  es = (coord==p->
31d06 72 56 61 6c 75 65 29 3b 20 62 72 65 61 6b 3b 0a  rValue); break;.
31d07 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20 21      }..    if( !
31d08 72 65 73 20 29 20 72 65 74 75 72 6e 20 31 3b 0a  res ) return 1;.
31d09 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 30 3b    }..  return 0;
31d0a 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 75 72 73 6f 72  .}../*.** Cursor
31d0b 20 70 43 75 72 73 6f 72 20 63 75 72 72 65 6e 74   pCursor current
31d0c 6c 79 20 70 6f 69 6e 74 73 20 61 74 20 61 20 6e  ly points at a n
31d0d 6f 64 65 20 74 68 61 74 20 68 65 61 64 73 20 61  ode that heads a
31d0e 20 73 75 62 2d 74 72 65 65 20 6f 66 0a 2a 2a 20   sub-tree of.** 
31d0f 68 65 69 67 68 74 20 69 48 65 69 67 68 74 20 28  height iHeight (
31d10 69 66 20 69 48 65 69 67 68 74 3d 3d 30 2c 20 74  if iHeight==0, t
31d11 68 65 6e 20 74 68 65 20 6e 6f 64 65 20 69 73 20  hen the node is 
31d12 61 20 6c 65 61 66 29 2e 20 44 65 73 63 65 6e 64  a leaf). Descend
31d13 0a 2a 2a 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20  .** to point to 
31d14 74 68 65 20 6c 65 66 74 2d 6d 6f 73 74 20 63 65  the left-most ce
31d15 6c 6c 20 6f 66 20 74 68 65 20 73 75 62 2d 74 72  ll of the sub-tr
31d16 65 65 20 74 68 61 74 20 6d 61 74 63 68 65 73 20  ee that matches 
31d17 74 68 65 20 0a 2a 2a 20 63 6f 6e 66 69 67 75 72  the .** configur
31d18 65 64 20 63 6f 6e 73 74 72 61 69 6e 74 73 2e 0a  ed constraints..
31d19 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65  */.static int de
31d1a 73 63 65 6e 64 54 6f 43 65 6c 6c 28 0a 20 20 52  scendToCell(.  R
31d1b 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 0a 20  tree *pRtree, . 
31d1c 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70 43   RtreeCursor *pC
31d1d 75 72 73 6f 72 2c 20 0a 20 20 69 6e 74 20 69 48  ursor, .  int iH
31d1e 65 69 67 68 74 2c 0a 20 20 69 6e 74 20 2a 70 45  eight,.  int *pE
31d1f 6f 66 20 20 20 20 20 20 20 20 20 20 20 20 20 20  of              
31d20 20 20 20 2f 2a 20 4f 55 54 3a 20 53 65 74 20 74     /* OUT: Set t
31d21 6f 20 74 72 75 65 20 69 66 20 63 61 6e 6e 6f 74  o true if cannot
31d22 20 64 65 73 63 65 6e 64 20 2a 2f 0a 29 7b 0a 20   descend */.){. 
31d23 20 69 6e 74 20 69 73 45 6f 66 3b 0a 20 20 69 6e   int isEof;.  in
31d24 74 20 72 63 3b 0a 20 20 69 6e 74 20 69 69 3b 0a  t rc;.  int ii;.
31d25 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68    RtreeNode *pCh
31d26 69 6c 64 3b 0a 20 20 73 71 6c 69 74 65 33 5f 69  ild;.  sqlite3_i
31d27 6e 74 36 34 20 69 52 6f 77 69 64 3b 0a 0a 20 20  nt64 iRowid;..  
31d28 52 74 72 65 65 4e 6f 64 65 20 2a 70 53 61 76 65  RtreeNode *pSave
31d29 64 4e 6f 64 65 20 3d 20 70 43 75 72 73 6f 72 2d  dNode = pCursor-
31d2a 3e 70 4e 6f 64 65 3b 0a 20 20 69 6e 74 20 69 53  >pNode;.  int iS
31d2b 61 76 65 64 43 65 6c 6c 20 3d 20 70 43 75 72 73  avedCell = pCurs
31d2c 6f 72 2d 3e 69 43 65 6c 6c 3b 0a 0a 20 20 61 73  or->iCell;..  as
31d2d 73 65 72 74 28 20 69 48 65 69 67 68 74 3e 3d 30  sert( iHeight>=0
31d2e 20 29 3b 0a 0a 20 20 69 66 28 20 69 48 65 69 67   );..  if( iHeig
31d2f 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20 69 73 45  ht==0 ){.    isE
31d30 6f 66 20 3d 20 74 65 73 74 52 74 72 65 65 45 6e  of = testRtreeEn
31d31 74 72 79 28 70 52 74 72 65 65 2c 20 70 43 75 72  try(pRtree, pCur
31d32 73 6f 72 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20  sor);.  }else{. 
31d33 20 20 20 69 73 45 6f 66 20 3d 20 74 65 73 74 52     isEof = testR
31d34 74 72 65 65 43 65 6c 6c 28 70 52 74 72 65 65 2c  treeCell(pRtree,
31d35 20 70 43 75 72 73 6f 72 29 3b 0a 20 20 7d 0a 20   pCursor);.  }. 
31d36 20 69 66 28 20 69 73 45 6f 66 20 7c 7c 20 69 48   if( isEof || iH
31d37 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eight==0 ){.    
31d38 2a 70 45 6f 66 20 3d 20 69 73 45 6f 66 3b 0a 20  *pEof = isEof;. 
31d39 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
31d3a 5f 4f 4b 3b 0a 20 20 7d 0a 0a 20 20 69 52 6f 77  _OK;.  }..  iRow
31d3b 69 64 20 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69  id = nodeGetRowi
31d3c 64 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f  d(pRtree, pCurso
31d3d 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 75 72 73 6f  r->pNode, pCurso
31d3e 72 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 72 63 20  r->iCell);.  rc 
31d3f 3d 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52  = nodeAcquire(pR
31d40 74 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 43  tree, iRowid, pC
31d41 75 72 73 6f 72 2d 3e 70 4e 6f 64 65 2c 20 26 70  ursor->pNode, &p
31d42 43 68 69 6c 64 29 3b 0a 20 20 69 66 28 20 72 63  Child);.  if( rc
31d43 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
31d44 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
31d45 7d 0a 0a 20 20 6e 6f 64 65 52 65 6c 65 61 73 65  }..  nodeRelease
31d46 28 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72  (pRtree, pCursor
31d47 2d 3e 70 4e 6f 64 65 29 3b 0a 20 20 70 43 75 72  ->pNode);.  pCur
31d48 73 6f 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 43 68  sor->pNode = pCh
31d49 69 6c 64 3b 0a 20 20 69 73 45 6f 66 20 3d 20 31  ild;.  isEof = 1
31d4a 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 73  ;.  for(ii=0; is
31d4b 45 6f 66 20 26 26 20 69 69 3c 4e 43 45 4c 4c 28  Eof && ii<NCELL(
31d4c 70 43 68 69 6c 64 29 3b 20 69 69 2b 2b 29 7b 0a  pChild); ii++){.
31d4d 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
31d4e 6c 6c 20 3d 20 69 69 3b 0a 20 20 20 20 72 63 20  ll = ii;.    rc 
31d4f 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28  = descendToCell(
31d50 70 52 74 72 65 65 2c 20 70 43 75 72 73 6f 72 2c  pRtree, pCursor,
31d51 20 69 48 65 69 67 68 74 2d 31 2c 20 26 69 73 45   iHeight-1, &isE
31d52 6f 66 29 3b 0a 20 20 20 20 69 66 28 20 72 63 21  of);.    if( rc!
31d53 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
31d54 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20      return rc;. 
31d55 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 69 66 28 20     }.  }..  if( 
31d56 69 73 45 6f 66 20 29 7b 0a 20 20 20 20 61 73 73  isEof ){.    ass
31d57 65 72 74 28 20 70 43 75 72 73 6f 72 2d 3e 70 4e  ert( pCursor->pN
31d58 6f 64 65 3d 3d 70 43 68 69 6c 64 20 29 3b 0a 20  ode==pChild );. 
31d59 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63 65     nodeReference
31d5a 28 70 53 61 76 65 64 4e 6f 64 65 29 3b 0a 20 20  (pSavedNode);.  
31d5b 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52    nodeRelease(pR
31d5c 74 72 65 65 2c 20 70 43 68 69 6c 64 29 3b 0a 20  tree, pChild);. 
31d5d 20 20 20 70 43 75 72 73 6f 72 2d 3e 70 4e 6f 64     pCursor->pNod
31d5e 65 20 3d 20 70 53 61 76 65 64 4e 6f 64 65 3b 0a  e = pSavedNode;.
31d5f 20 20 20 20 70 43 75 72 73 6f 72 2d 3e 69 43 65      pCursor->iCe
31d60 6c 6c 20 3d 20 69 53 61 76 65 64 43 65 6c 6c 3b  ll = iSavedCell;
31d61 0a 20 20 7d 0a 0a 20 20 2a 70 45 6f 66 20 3d 20  .  }..  *pEof = 
31d62 69 73 45 6f 66 3b 0a 20 20 72 65 74 75 72 6e 20  isEof;.  return 
31d63 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
31d64 0a 2a 2a 20 4f 6e 65 20 6f 66 20 74 68 65 20 63  .** One of the c
31d65 65 6c 6c 73 20 69 6e 20 6e 6f 64 65 20 70 4e 6f  ells in node pNo
31d66 64 65 20 69 73 20 67 75 61 72 61 6e 74 65 65 64  de is guaranteed
31d67 20 74 6f 20 68 61 76 65 20 61 20 36 34 2d 62 69   to have a 64-bi
31d68 74 20 0a 2a 2a 20 69 6e 74 65 67 65 72 20 76 61  t .** integer va
31d69 6c 75 65 20 65 71 75 61 6c 20 74 6f 20 69 52 6f  lue equal to iRo
31d6a 77 69 64 2e 20 52 65 74 75 72 6e 20 74 68 65 20  wid. Return the 
31d6b 69 6e 64 65 78 20 6f 66 20 74 68 69 73 20 63 65  index of this ce
31d6c 6c 6c 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  ll..*/.static in
31d6d 74 20 6e 6f 64 65 52 6f 77 69 64 49 6e 64 65 78  t nodeRowidIndex
31d6e 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
31d6f 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65  RtreeNode *pNode
31d70 2c 20 69 36 34 20 69 52 6f 77 69 64 29 7b 0a 20  , i64 iRowid){. 
31d71 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28 69   int ii;.  for(i
31d72 69 3d 30 3b 20 6e 6f 64 65 47 65 74 52 6f 77 69  i=0; nodeGetRowi
31d73 64 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c  d(pRtree, pNode,
31d74 20 69 69 29 21 3d 69 52 6f 77 69 64 3b 20 69 69   ii)!=iRowid; ii
31d75 2b 2b 29 7b 0a 20 20 20 20 61 73 73 65 72 74 28  ++){.    assert(
31d76 20 69 69 3c 28 4e 43 45 4c 4c 28 70 4e 6f 64 65   ii<(NCELL(pNode
31d77 29 2d 31 29 20 29 3b 0a 20 20 7d 0a 20 20 72 65  )-1) );.  }.  re
31d78 74 75 72 6e 20 69 69 3b 0a 7d 0a 0a 2f 2a 0a 2a  turn ii;.}../*.*
31d79 2a 20 52 65 74 75 72 6e 20 74 68 65 20 69 6e 64  * Return the ind
31d7a 65 78 20 6f 66 20 74 68 65 20 63 65 6c 6c 20 63  ex of the cell c
31d7b 6f 6e 74 61 69 6e 69 6e 67 20 61 20 70 6f 69 6e  ontaining a poin
31d7c 74 65 72 20 74 6f 20 6e 6f 64 65 20 70 4e 6f 64  ter to node pNod
31d7d 65 0a 2a 2a 20 69 6e 20 69 74 73 20 70 61 72 65  e.** in its pare
31d7e 6e 74 2e 20 49 66 20 70 4e 6f 64 65 20 69 73 20  nt. If pNode is 
31d7f 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 2c 20 72  the root node, r
31d80 65 74 75 72 6e 20 2d 31 2e 0a 2a 2f 0a 73 74 61  eturn -1..*/.sta
31d81 74 69 63 20 69 6e 74 20 6e 6f 64 65 50 61 72 65  tic int nodePare
31d82 6e 74 49 6e 64 65 78 28 52 74 72 65 65 20 2a 70  ntIndex(Rtree *p
31d83 52 74 72 65 65 2c 20 52 74 72 65 65 4e 6f 64 65  Rtree, RtreeNode
31d84 20 2a 70 4e 6f 64 65 29 7b 0a 20 20 52 74 72 65   *pNode){.  Rtre
31d85 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  eNode *pParent =
31d86 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b   pNode->pParent;
31d87 0a 20 20 69 66 28 20 70 50 61 72 65 6e 74 20 29  .  if( pParent )
31d88 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 6f 64  {.    return nod
31d89 65 52 6f 77 69 64 49 6e 64 65 78 28 70 52 74 72  eRowidIndex(pRtr
31d8a 65 65 2c 20 70 50 61 72 65 6e 74 2c 20 70 4e 6f  ee, pParent, pNo
31d8b 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 7d 0a  de->iNode);.  }.
31d8c 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 7d 0a 0a    return -1;.}..
31d8d 2f 2a 20 0a 2a 2a 20 52 74 72 65 65 20 76 69 72  /* .** Rtree vir
31d8e 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c  tual table modul
31d8f 65 20 78 4e 65 78 74 20 6d 65 74 68 6f 64 2e 0a  e xNext method..
31d90 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74  */.static int rt
31d91 72 65 65 4e 65 78 74 28 73 71 6c 69 74 65 33 5f  reeNext(sqlite3_
31d92 76 74 61 62 5f 63 75 72 73 6f 72 20 2a 70 56 74  vtab_cursor *pVt
31d93 61 62 43 75 72 73 6f 72 29 7b 0a 20 20 52 74 72  abCursor){.  Rtr
31d94 65 65 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74  ee *pRtree = (Rt
31d95 72 65 65 20 2a 29 28 70 56 74 61 62 43 75 72 73  ree *)(pVtabCurs
31d96 6f 72 2d 3e 70 56 74 61 62 29 3b 0a 20 20 52 74  or->pVtab);.  Rt
31d97 72 65 65 43 75 72 73 6f 72 20 2a 70 43 73 72 20  reeCursor *pCsr 
31d98 3d 20 28 52 74 72 65 65 43 75 72 73 6f 72 20 2a  = (RtreeCursor *
31d99 29 70 56 74 61 62 43 75 72 73 6f 72 3b 0a 20 20  )pVtabCursor;.  
31d9a 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f  int rc = SQLITE_
31d9b 4f 4b 3b 0a 0a 20 20 69 66 28 20 70 43 73 72 2d  OK;..  if( pCsr-
31d9c 3e 69 53 74 72 61 74 65 67 79 3d 3d 31 20 29 7b  >iStrategy==1 ){
31d9d 0a 20 20 20 20 2f 2a 20 54 68 69 73 20 22 73 63  .    /* This "sc
31d9e 61 6e 22 20 69 73 20 61 20 64 69 72 65 63 74 20  an" is a direct 
31d9f 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69 64 2e  lookup by rowid.
31da0 20 54 68 65 72 65 20 69 73 20 6e 6f 20 6e 65 78   There is no nex
31da1 74 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 20 20  t entry. */.    
31da2 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72  nodeRelease(pRtr
31da3 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 29  ee, pCsr->pNode)
31da4 3b 0a 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64  ;.    pCsr->pNod
31da5 65 20 3d 20 30 3b 0a 20 20 7d 0a 0a 20 20 65 6c  e = 0;.  }..  el
31da6 73 65 20 69 66 28 20 70 43 73 72 2d 3e 70 4e 6f  se if( pCsr->pNo
31da7 64 65 20 29 7b 0a 20 20 20 20 2f 2a 20 4d 6f 76  de ){.    /* Mov
31da8 65 20 74 6f 20 74 68 65 20 6e 65 78 74 20 65 6e  e to the next en
31da9 74 72 79 20 74 68 61 74 20 6d 61 74 63 68 65 73  try that matches
31daa 20 74 68 65 20 63 6f 6e 66 69 67 75 72 65 64 20   the configured 
31dab 63 6f 6e 73 74 72 61 69 6e 74 73 2e 20 2a 2f 0a  constraints. */.
31dac 20 20 20 20 69 6e 74 20 69 48 65 69 67 68 74 20      int iHeight 
31dad 3d 20 30 3b 0a 20 20 20 20 77 68 69 6c 65 28 20  = 0;.    while( 
31dae 70 43 73 72 2d 3e 70 4e 6f 64 65 20 29 7b 0a 20  pCsr->pNode ){. 
31daf 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a       RtreeNode *
31db0 70 4e 6f 64 65 20 3d 20 70 43 73 72 2d 3e 70 4e  pNode = pCsr->pN
31db1 6f 64 65 3b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ode;.      int n
31db2 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
31db3 64 65 29 3b 0a 20 20 20 20 20 20 66 6f 72 28 70  de);.      for(p
31db4 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 3b 20 70 43  Csr->iCell++; pC
31db5 73 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b  sr->iCell<nCell;
31db6 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b   pCsr->iCell++){
31db7 0a 20 20 20 20 20 20 20 20 69 6e 74 20 69 73 45  .        int isE
31db8 6f 66 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  of;.        rc =
31db9 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c 28 70   descendToCell(p
31dba 52 74 72 65 65 2c 20 70 43 73 72 2c 20 69 48 65  Rtree, pCsr, iHe
31dbb 69 67 68 74 2c 20 26 69 73 45 6f 66 29 3b 0a 20  ight, &isEof);. 
31dbc 20 20 20 20 20 20 20 69 66 28 20 72 63 21 3d 53         if( rc!=S
31dbd 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21 69 73 45  QLITE_OK || !isE
31dbe 6f 66 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  of ){.          
31dbf 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 20 20 20  return rc;.     
31dc0 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
31dc1 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65 20 3d     pCsr->pNode =
31dc2 20 70 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 3b   pNode->pParent;
31dc3 0a 20 20 20 20 20 20 70 43 73 72 2d 3e 69 43 65  .      pCsr->iCe
31dc4 6c 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49  ll = nodeParentI
31dc5 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4e 6f  ndex(pRtree, pNo
31dc6 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52  de);.      nodeR
31dc7 65 66 65 72 65 6e 63 65 28 70 43 73 72 2d 3e 70  eference(pCsr->p
31dc8 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Node);.      nod
31dc9 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
31dca 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20 69   pNode);.      i
31dcb 48 65 69 67 68 74 2b 2b 3b 0a 20 20 20 20 7d 0a  Height++;.    }.
31dcc 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63    }..  return rc
31dcd 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  ;.}../* .** Rtre
31dce 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
31dcf 6d 6f 64 75 6c 65 20 78 52 6f 77 69 64 20 6d 65  module xRowid me
31dd0 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  thod..*/.static 
31dd1 69 6e 74 20 72 74 72 65 65 52 6f 77 69 64 28 73  int rtreeRowid(s
31dd2 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75 72 73  qlite3_vtab_curs
31dd3 6f 72 20 2a 70 56 74 61 62 43 75 72 73 6f 72 2c  or *pVtabCursor,
31dd4 20 73 71 6c 69 74 65 5f 69 6e 74 36 34 20 2a 70   sqlite_int64 *p
31dd5 52 6f 77 69 64 29 7b 0a 20 20 52 74 72 65 65 20  Rowid){.  Rtree 
31dd6 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65  *pRtree = (Rtree
31dd7 20 2a 29 70 56 74 61 62 43 75 72 73 6f 72 2d 3e   *)pVtabCursor->
31dd8 70 56 74 61 62 3b 0a 20 20 52 74 72 65 65 43 75  pVtab;.  RtreeCu
31dd9 72 73 6f 72 20 2a 70 43 73 72 20 3d 20 28 52 74  rsor *pCsr = (Rt
31dda 72 65 65 43 75 72 73 6f 72 20 2a 29 70 56 74 61  reeCursor *)pVta
31ddb 62 43 75 72 73 6f 72 3b 0a 0a 20 20 61 73 73 65  bCursor;..  asse
31ddc 72 74 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 3b  rt(pCsr->pNode);
31ddd 0a 20 20 2a 70 52 6f 77 69 64 20 3d 20 6e 6f 64  .  *pRowid = nod
31dde 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65 65  eGetRowid(pRtree
31ddf 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70  , pCsr->pNode, p
31de0 43 73 72 2d 3e 69 43 65 6c 6c 29 3b 0a 0a 20 20  Csr->iCell);..  
31de1 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b  return SQLITE_OK
31de2 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20 52 74 72 65  ;.}../* .** Rtre
31de3 65 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20  e virtual table 
31de4 6d 6f 64 75 6c 65 20 78 43 6f 6c 75 6d 6e 20 6d  module xColumn m
31de5 65 74 68 6f 64 2e 0a 2a 2f 0a 73 74 61 74 69 63  ethod..*/.static
31de6 20 69 6e 74 20 72 74 72 65 65 43 6f 6c 75 6d 6e   int rtreeColumn
31de7 28 73 71 6c 69 74 65 33 5f 76 74 61 62 5f 63 75  (sqlite3_vtab_cu
31de8 72 73 6f 72 20 2a 63 75 72 2c 20 73 71 6c 69 74  rsor *cur, sqlit
31de9 65 33 5f 63 6f 6e 74 65 78 74 20 2a 63 74 78 2c  e3_context *ctx,
31dea 20 69 6e 74 20 69 29 7b 0a 20 20 52 74 72 65 65   int i){.  Rtree
31deb 20 2a 70 52 74 72 65 65 20 3d 20 28 52 74 72 65   *pRtree = (Rtre
31dec 65 20 2a 29 63 75 72 2d 3e 70 56 74 61 62 3b 0a  e *)cur->pVtab;.
31ded 20 20 52 74 72 65 65 43 75 72 73 6f 72 20 2a 70    RtreeCursor *p
31dee 43 73 72 20 3d 20 28 52 74 72 65 65 43 75 72 73  Csr = (RtreeCurs
31def 6f 72 20 2a 29 63 75 72 3b 0a 0a 20 20 69 66 28  or *)cur;..  if(
31df0 20 69 3d 3d 30 20 29 7b 0a 20 20 20 20 69 36 34   i==0 ){.    i64
31df1 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47 65   iRowid = nodeGe
31df2 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20 70  tRowid(pRtree, p
31df3 43 73 72 2d 3e 70 4e 6f 64 65 2c 20 70 43 73 72  Csr->pNode, pCsr
31df4 2d 3e 69 43 65 6c 6c 29 3b 0a 20 20 20 20 73 71  ->iCell);.    sq
31df5 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 69 6e 74  lite3_result_int
31df6 36 34 28 63 74 78 2c 20 69 52 6f 77 69 64 29 3b  64(ctx, iRowid);
31df7 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74  .  }else{.    Rt
31df8 72 65 65 43 6f 6f 72 64 20 63 3b 0a 20 20 20 20  reeCoord c;.    
31df9 6e 6f 64 65 47 65 74 43 6f 6f 72 64 28 70 52 74  nodeGetCoord(pRt
31dfa 72 65 65 2c 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ree, pCsr->pNode
31dfb 2c 20 70 43 73 72 2d 3e 69 43 65 6c 6c 2c 20 69  , pCsr->iCell, i
31dfc 2d 31 2c 20 26 63 29 3b 0a 20 20 20 20 69 66 28  -1, &c);.    if(
31dfd 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f 72 64 54   pRtree->eCoordT
31dfe 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f 4f 52 44  ype==RTREE_COORD
31dff 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20 20 20 20  _REAL32 ){.     
31e00 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
31e01 64 6f 75 62 6c 65 28 63 74 78 2c 20 63 2e 66 29  double(ctx, c.f)
31e02 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
31e03 20 20 20 61 73 73 65 72 74 28 20 70 52 74 72 65     assert( pRtre
31e04 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52  e->eCoordType==R
31e05 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54 33 32  TREE_COORD_INT32
31e06 20 29 3b 0a 20 20 20 20 20 20 73 71 6c 69 74 65   );.      sqlite
31e07 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 74 78  3_result_int(ctx
31e08 2c 20 63 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20  , c.i);.    }.  
31e09 7d 0a 0a 20 20 72 65 74 75 72 6e 20 53 51 4c 49  }..  return SQLI
31e0a 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a  TE_OK;.}../* .**
31e0b 20 55 73 65 20 6e 6f 64 65 41 63 71 75 69 72 65   Use nodeAcquire
31e0c 28 29 20 74 6f 20 6f 62 74 61 69 6e 20 74 68 65  () to obtain the
31e0d 20 6c 65 61 66 20 6e 6f 64 65 20 63 6f 6e 74 61   leaf node conta
31e0e 69 6e 69 6e 67 20 74 68 65 20 72 65 63 6f 72 64  ining the record
31e0f 20 77 69 74 68 20 0a 2a 2a 20 72 6f 77 69 64 20   with .** rowid 
31e10 69 52 6f 77 69 64 2e 20 49 66 20 73 75 63 63 65  iRowid. If succe
31e11 73 73 66 75 6c 2c 20 73 65 74 20 2a 70 70 4c 65  ssful, set *ppLe
31e12 61 66 20 74 6f 20 70 6f 69 6e 74 20 74 6f 20 74  af to point to t
31e13 68 65 20 6e 6f 64 65 20 61 6e 64 0a 2a 2a 20 72  he node and.** r
31e14 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 2e  eturn SQLITE_OK.
31e15 20 49 66 20 74 68 65 72 65 20 69 73 20 6e 6f 20   If there is no 
31e16 73 75 63 68 20 72 65 63 6f 72 64 20 69 6e 20 74  such record in t
31e17 68 65 20 74 61 62 6c 65 2c 20 73 65 74 0a 2a 2a  he table, set.**
31e18 20 2a 70 70 4c 65 61 66 20 74 6f 20 30 20 61 6e   *ppLeaf to 0 an
31e19 64 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f  d return SQLITE_
31e1a 4f 4b 2e 20 49 66 20 61 6e 20 65 72 72 6f 72 20  OK. If an error 
31e1b 6f 63 63 75 72 73 2c 20 73 65 74 20 2a 70 70 4c  occurs, set *ppL
31e1c 65 61 66 0a 2a 2a 20 74 6f 20 7a 65 72 6f 20 61  eaf.** to zero a
31e1d 6e 64 20 72 65 74 75 72 6e 20 61 6e 20 53 51 4c  nd return an SQL
31e1e 69 74 65 20 65 72 72 6f 72 20 63 6f 64 65 2e 0a  ite error code..
31e1f 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 66 69  */.static int fi
31e20 6e 64 4c 65 61 66 4e 6f 64 65 28 52 74 72 65 65  ndLeafNode(Rtree
31e21 20 2a 70 52 74 72 65 65 2c 20 69 36 34 20 69 52   *pRtree, i64 iR
31e22 6f 77 69 64 2c 20 52 74 72 65 65 4e 6f 64 65 20  owid, RtreeNode 
31e23 2a 2a 70 70 4c 65 61 66 29 7b 0a 20 20 69 6e 74  **ppLeaf){.  int
31e24 20 72 63 3b 0a 20 20 2a 70 70 4c 65 61 66 20 3d   rc;.  *ppLeaf =
31e25 20 30 3b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69   0;.  sqlite3_bi
31e26 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d  nd_int64(pRtree-
31e27 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31 2c 20  >pReadRowid, 1, 
31e28 69 52 6f 77 69 64 29 3b 0a 20 20 69 66 28 20 73  iRowid);.  if( s
31e29 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
31e2a 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64 29 3d  ee->pReadRowid)=
31e2b 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a 20  =SQLITE_ROW ){. 
31e2c 20 20 20 69 36 34 20 69 4e 6f 64 65 20 3d 20 73     i64 iNode = s
31e2d 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e 5f 69 6e  qlite3_column_in
31e2e 74 36 34 28 70 52 74 72 65 65 2d 3e 70 52 65 61  t64(pRtree->pRea
31e2f 64 52 6f 77 69 64 2c 20 30 29 3b 0a 20 20 20 20  dRowid, 0);.    
31e30 72 63 20 3d 20 6e 6f 64 65 41 63 71 75 69 72 65  rc = nodeAcquire
31e31 28 70 52 74 72 65 65 2c 20 69 4e 6f 64 65 2c 20  (pRtree, iNode, 
31e32 30 2c 20 70 70 4c 65 61 66 29 3b 0a 20 20 20 20  0, ppLeaf);.    
31e33 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
31e34 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
31e35 29 3b 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  );.  }else{.    
31e36 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73  rc = sqlite3_res
31e37 65 74 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64  et(pRtree->pRead
31e38 52 6f 77 69 64 29 3b 0a 20 20 7d 0a 20 20 72 65  Rowid);.  }.  re
31e39 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 0a 2f 2a 20  turn rc;.}.../* 
31e3a 0a 2a 2a 20 52 74 72 65 65 20 76 69 72 74 75 61  .** Rtree virtua
31e3b 6c 20 74 61 62 6c 65 20 6d 6f 64 75 6c 65 20 78  l table module x
31e3c 46 69 6c 74 65 72 20 6d 65 74 68 6f 64 2e 0a 2a  Filter method..*
31e3d 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
31e3e 65 65 46 69 6c 74 65 72 28 0a 20 20 73 71 6c 69  eeFilter(.  sqli
31e3f 74 65 33 5f 76 74 61 62 5f 63 75 72 73 6f 72 20  te3_vtab_cursor 
31e40 2a 70 56 74 61 62 43 75 72 73 6f 72 2c 20 0a 20  *pVtabCursor, . 
31e41 20 69 6e 74 20 69 64 78 4e 75 6d 2c 20 63 6f 6e   int idxNum, con
31e42 73 74 20 63 68 61 72 20 2a 69 64 78 53 74 72 2c  st char *idxStr,
31e43 0a 20 20 69 6e 74 20 61 72 67 63 2c 20 73 71 6c  .  int argc, sql
31e44 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72 67  ite3_value **arg
31e45 76 0a 29 7b 0a 20 20 52 74 72 65 65 20 2a 70 52  v.){.  Rtree *pR
31e46 74 72 65 65 20 3d 20 28 52 74 72 65 65 20 2a 29  tree = (Rtree *)
31e47 70 56 74 61 62 43 75 72 73 6f 72 2d 3e 70 56 74  pVtabCursor->pVt
31e48 61 62 3b 0a 20 20 52 74 72 65 65 43 75 72 73 6f  ab;.  RtreeCurso
31e49 72 20 2a 70 43 73 72 20 3d 20 28 52 74 72 65 65  r *pCsr = (Rtree
31e4a 43 75 72 73 6f 72 20 2a 29 70 56 74 61 62 43 75  Cursor *)pVtabCu
31e4b 72 73 6f 72 3b 0a 0a 20 20 52 74 72 65 65 4e 6f  rsor;..  RtreeNo
31e4c 64 65 20 2a 70 52 6f 6f 74 20 3d 20 30 3b 0a 20  de *pRoot = 0;. 
31e4d 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 72   int ii;.  int r
31e4e 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a  c = SQLITE_OK;..
31e4f 20 20 72 74 72 65 65 52 65 66 65 72 65 6e 63 65    rtreeReference
31e50 28 70 52 74 72 65 65 29 3b 0a 0a 20 20 73 71 6c  (pRtree);..  sql
31e51 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e  ite3_free(pCsr->
31e52 61 43 6f 6e 73 74 72 61 69 6e 74 29 3b 0a 20 20  aConstraint);.  
31e53 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69 6e  pCsr->aConstrain
31e54 74 20 3d 20 30 3b 0a 20 20 70 43 73 72 2d 3e 69  t = 0;.  pCsr->i
31e55 53 74 72 61 74 65 67 79 20 3d 20 69 64 78 4e 75  Strategy = idxNu
31e56 6d 3b 0a 0a 20 20 69 66 28 20 69 64 78 4e 75 6d  m;..  if( idxNum
31e57 3d 3d 31 20 29 7b 0a 20 20 20 20 2f 2a 20 53 70  ==1 ){.    /* Sp
31e58 65 63 69 61 6c 20 63 61 73 65 20 2d 20 6c 6f 6f  ecial case - loo
31e59 6b 75 70 20 62 79 20 72 6f 77 69 64 2e 20 2a 2f  kup by rowid. */
31e5a 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
31e5b 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20 2f 2a  pLeaf;        /*
31e5c 20 4c 65 61 66 20 6f 6e 20 77 68 69 63 68 20 74   Leaf on which t
31e5d 68 65 20 72 65 71 75 69 72 65 64 20 63 65 6c 6c  he required cell
31e5e 20 72 65 73 69 64 65 73 20 2a 2f 0a 20 20 20 20   resides */.    
31e5f 69 36 34 20 69 52 6f 77 69 64 20 3d 20 73 71 6c  i64 iRowid = sql
31e60 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36 34  ite3_value_int64
31e61 28 61 72 67 76 5b 30 5d 29 3b 0a 20 20 20 20 72  (argv[0]);.    r
31e62 63 20 3d 20 66 69 6e 64 4c 65 61 66 4e 6f 64 65  c = findLeafNode
31e63 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69 64 2c  (pRtree, iRowid,
31e64 20 26 70 4c 65 61 66 29 3b 0a 20 20 20 20 70 43   &pLeaf);.    pC
31e65 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 4c 65 61  sr->pNode = pLea
31e66 66 3b 20 0a 20 20 20 20 69 66 28 20 70 4c 65 61  f; .    if( pLea
31e67 66 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45 5f  f && rc==SQLITE_
31e68 4f 4b 20 29 7b 0a 20 20 20 20 20 20 70 43 73 72  OK ){.      pCsr
31e69 2d 3e 69 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f  ->iCell = nodeRo
31e6a 77 69 64 49 6e 64 65 78 28 70 52 74 72 65 65 2c  widIndex(pRtree,
31e6b 20 70 4c 65 61 66 2c 20 69 52 6f 77 69 64 29 3b   pLeaf, iRowid);
31e6c 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65 7b 0a  .    }.  }else{.
31e6d 20 20 20 20 2f 2a 20 4e 6f 72 6d 61 6c 20 63 61      /* Normal ca
31e6e 73 65 20 2d 20 72 2d 74 72 65 65 20 73 63 61 6e  se - r-tree scan
31e6f 2e 20 53 65 74 20 75 70 20 74 68 65 20 52 74 72  . Set up the Rtr
31e70 65 65 43 75 72 73 6f 72 2e 61 43 6f 6e 73 74 72  eeCursor.aConstr
31e71 61 69 6e 74 20 61 72 72 61 79 20 0a 20 20 20 20  aint array .    
31e72 2a 2a 20 77 69 74 68 20 74 68 65 20 63 6f 6e 66  ** with the conf
31e73 69 67 75 72 65 64 20 63 6f 6e 73 74 72 61 69 6e  igured constrain
31e74 74 73 2e 20 0a 20 20 20 20 2a 2f 0a 20 20 20 20  ts. .    */.    
31e75 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20 20  if( argc>0 ){.  
31e76 20 20 20 20 70 43 73 72 2d 3e 61 43 6f 6e 73 74      pCsr->aConst
31e77 72 61 69 6e 74 20 3d 20 73 71 6c 69 74 65 33 5f  raint = sqlite3_
31e78 6d 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74  malloc(sizeof(Rt
31e79 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 29 2a 61  reeConstraint)*a
31e7a 72 67 63 29 3b 0a 20 20 20 20 20 20 70 43 73 72  rgc);.      pCsr
31e7b 2d 3e 6e 43 6f 6e 73 74 72 61 69 6e 74 20 3d 20  ->nConstraint = 
31e7c 61 72 67 63 3b 0a 20 20 20 20 20 20 69 66 28 20  argc;.      if( 
31e7d 21 70 43 73 72 2d 3e 61 43 6f 6e 73 74 72 61 69  !pCsr->aConstrai
31e7e 6e 74 20 29 7b 0a 20 20 20 20 20 20 20 20 72 63  nt ){.        rc
31e7f 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b   = SQLITE_NOMEM;
31e80 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  .      }else{.  
31e81 20 20 20 20 20 20 61 73 73 65 72 74 28 20 28 69        assert( (i
31e82 64 78 53 74 72 3d 3d 30 20 26 26 20 61 72 67 63  dxStr==0 && argc
31e83 3d 3d 30 29 20 7c 7c 20 73 74 72 6c 65 6e 28 69  ==0) || strlen(i
31e84 64 78 53 74 72 29 3d 3d 61 72 67 63 2a 32 20 29  dxStr)==argc*2 )
31e85 3b 0a 20 20 20 20 20 20 20 20 66 6f 72 28 69 69  ;.        for(ii
31e86 3d 30 3b 20 69 69 3c 61 72 67 63 3b 20 69 69 2b  =0; ii<argc; ii+
31e87 2b 29 7b 0a 20 20 20 20 20 20 20 20 20 20 52 74  +){.          Rt
31e88 72 65 65 43 6f 6e 73 74 72 61 69 6e 74 20 2a 70  reeConstraint *p
31e89 20 3d 20 26 70 43 73 72 2d 3e 61 43 6f 6e 73 74   = &pCsr->aConst
31e8a 72 61 69 6e 74 5b 69 69 5d 3b 0a 20 20 20 20 20  raint[ii];.     
31e8b 20 20 20 20 20 70 2d 3e 6f 70 20 3d 20 69 64 78       p->op = idx
31e8c 53 74 72 5b 69 69 2a 32 5d 3b 0a 20 20 20 20 20  Str[ii*2];.     
31e8d 20 20 20 20 20 70 2d 3e 69 43 6f 6f 72 64 20 3d       p->iCoord =
31e8e 20 69 64 78 53 74 72 5b 69 69 2a 32 2b 31 5d 2d   idxStr[ii*2+1]-
31e8f 27 61 27 3b 0a 20 20 20 20 20 20 20 20 20 20 70  'a';.          p
31e90 2d 3e 72 56 61 6c 75 65 20 3d 20 73 71 6c 69 74  ->rValue = sqlit
31e91 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
31e92 61 72 67 76 5b 69 69 5d 29 3b 0a 20 20 20 20 20  argv[ii]);.     
31e93 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
31e94 20 7d 0a 20 20 0a 20 20 20 20 69 66 28 20 72 63   }.  .    if( rc
31e95 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  ==SQLITE_OK ){. 
31e96 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f 64 65       pCsr->pNode
31e97 20 3d 20 30 3b 0a 20 20 20 20 20 20 72 63 20 3d   = 0;.      rc =
31e98 20 6e 6f 64 65 41 63 71 75 69 72 65 28 70 52 74   nodeAcquire(pRt
31e99 72 65 65 2c 20 31 2c 20 30 2c 20 26 70 52 6f 6f  ree, 1, 0, &pRoo
31e9a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  t);.    }.    if
31e9b 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
31e9c 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 69 73 45  ){.      int isE
31e9d 6f 66 20 3d 20 31 3b 0a 20 20 20 20 20 20 69 6e  of = 1;.      in
31e9e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28  t nCell = NCELL(
31e9f 70 52 6f 6f 74 29 3b 0a 20 20 20 20 20 20 70 43  pRoot);.      pC
31ea0 73 72 2d 3e 70 4e 6f 64 65 20 3d 20 70 52 6f 6f  sr->pNode = pRoo
31ea1 74 3b 0a 20 20 20 20 20 20 66 6f 72 28 70 43 73  t;.      for(pCs
31ea2 72 2d 3e 69 43 65 6c 6c 3d 30 3b 20 72 63 3d 3d  r->iCell=0; rc==
31ea3 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 43 73  SQLITE_OK && pCs
31ea4 72 2d 3e 69 43 65 6c 6c 3c 6e 43 65 6c 6c 3b 20  r->iCell<nCell; 
31ea5 70 43 73 72 2d 3e 69 43 65 6c 6c 2b 2b 29 7b 0a  pCsr->iCell++){.
31ea6 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
31ea7 70 43 73 72 2d 3e 70 4e 6f 64 65 3d 3d 70 52 6f  pCsr->pNode==pRo
31ea8 6f 74 20 29 3b 0a 20 20 20 20 20 20 20 20 72 63  ot );.        rc
31ea9 20 3d 20 64 65 73 63 65 6e 64 54 6f 43 65 6c 6c   = descendToCell
31eaa 28 70 52 74 72 65 65 2c 20 70 43 73 72 2c 20 70  (pRtree, pCsr, p
31eab 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2c 20 26  Rtree->iDepth, &
31eac 69 73 45 6f 66 29 3b 0a 20 20 20 20 20 20 20 20  isEof);.        
31ead 69 66 28 20 21 69 73 45 6f 66 20 29 7b 0a 20 20  if( !isEof ){.  
31eae 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20          break;. 
31eaf 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 7d         }.      }
31eb0 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53  .      if( rc==S
31eb1 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69 73 45 6f  QLITE_OK && isEo
31eb2 66 20 29 7b 0a 20 20 20 20 20 20 20 20 61 73 73  f ){.        ass
31eb3 65 72 74 28 20 70 43 73 72 2d 3e 70 4e 6f 64 65  ert( pCsr->pNode
31eb4 3d 3d 70 52 6f 6f 74 20 29 3b 0a 20 20 20 20 20  ==pRoot );.     
31eb5 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70     nodeRelease(p
31eb6 52 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20  Rtree, pRoot);. 
31eb7 20 20 20 20 20 20 20 70 43 73 72 2d 3e 70 4e 6f         pCsr->pNo
31eb8 64 65 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  de = 0;.      }.
31eb9 20 20 20 20 20 20 61 73 73 65 72 74 28 20 72 63        assert( rc
31eba 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 7c 7c 20 21  !=SQLITE_OK || !
31ebb 70 43 73 72 2d 3e 70 4e 6f 64 65 20 7c 7c 20 70  pCsr->pNode || p
31ebc 43 73 72 2d 3e 69 43 65 6c 6c 3c 4e 43 45 4c 4c  Csr->iCell<NCELL
31ebd 28 70 43 73 72 2d 3e 70 4e 6f 64 65 29 20 29 3b  (pCsr->pNode) );
31ebe 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 72 74  .    }.  }..  rt
31ebf 72 65 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  reeRelease(pRtre
31ec0 65 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b  e);.  return rc;
31ec1 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 74 72 65 65 20  .}../*.** Rtree 
31ec2 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d 6f  virtual table mo
31ec3 64 75 6c 65 20 78 42 65 73 74 49 6e 64 65 78 20  dule xBestIndex 
31ec4 6d 65 74 68 6f 64 2e 20 54 68 65 72 65 20 61 72  method. There ar
31ec5 65 20 74 68 72 65 65 0a 2a 2a 20 74 61 62 6c 65  e three.** table
31ec6 20 73 63 61 6e 20 73 74 72 61 74 65 67 69 65 73   scan strategies
31ec7 20 74 6f 20 63 68 6f 6f 73 65 20 66 72 6f 6d 20   to choose from 
31ec8 28 69 6e 20 6f 72 64 65 72 20 66 72 6f 6d 20 6d  (in order from m
31ec9 6f 73 74 20 74 6f 20 0a 2a 2a 20 6c 65 61 73 74  ost to .** least
31eca 20 64 65 73 69 72 61 62 6c 65 29 3a 0a 2a 2a 0a   desirable):.**.
31ecb 2a 2a 20 20 20 69 64 78 4e 75 6d 20 20 20 20 20  **   idxNum     
31ecc 69 64 78 53 74 72 20 20 20 20 20 20 20 20 53 74  idxStr        St
31ecd 72 61 74 65 67 79 0a 2a 2a 20 20 20 2d 2d 2d 2d  rategy.**   ----
31ece 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31ecf 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31ed0 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 20  ------------.** 
31ed1 20 20 20 20 31 20 20 20 20 20 20 20 20 55 6e 75      1        Unu
31ed2 73 65 64 20 20 20 20 20 20 20 20 44 69 72 65 63  sed        Direc
31ed3 74 20 6c 6f 6f 6b 75 70 20 62 79 20 72 6f 77 69  t lookup by rowi
31ed4 64 2e 0a 2a 2a 20 20 20 20 20 32 20 20 20 20 20  d..**     2     
31ed5 20 20 20 53 65 65 20 62 65 6c 6f 77 20 20 20 20     See below    
31ed6 20 52 2d 74 72 65 65 20 71 75 65 72 79 2e 0a 2a   R-tree query..*
31ed7 2a 20 20 20 20 20 33 20 20 20 20 20 20 20 20 55  *     3        U
31ed8 6e 75 73 65 64 20 20 20 20 20 20 20 20 46 75 6c  nused        Ful
31ed9 6c 20 74 61 62 6c 65 20 73 63 61 6e 2e 0a 2a 2a  l table scan..**
31eda 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d     -------------
31edb 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31edc 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31edd 2d 2d 2d 0a 2a 2a 0a 2a 2a 20 49 66 20 73 74 72  ---.**.** If str
31ede 61 74 65 67 79 20 31 20 6f 72 20 33 20 69 73 20  ategy 1 or 3 is 
31edf 75 73 65 64 2c 20 74 68 65 6e 20 69 64 78 53 74  used, then idxSt
31ee0 72 20 69 73 20 6e 6f 74 20 6d 65 61 6e 69 6e 67  r is not meaning
31ee1 66 75 6c 2e 20 49 66 20 73 74 72 61 74 65 67 79  ful. If strategy
31ee2 0a 2a 2a 20 32 20 69 73 20 75 73 65 64 2c 20 69  .** 2 is used, i
31ee3 64 78 53 74 72 20 69 73 20 66 6f 72 6d 61 74 74  dxStr is formatt
31ee4 65 64 20 74 6f 20 63 6f 6e 74 61 69 6e 20 32 20  ed to contain 2 
31ee5 62 79 74 65 73 20 66 6f 72 20 65 61 63 68 20 0a  bytes for each .
31ee6 2a 2a 20 63 6f 6e 73 74 72 61 69 6e 74 20 75 73  ** constraint us
31ee7 65 64 2e 20 54 68 65 20 66 69 72 73 74 20 74 77  ed. The first tw
31ee8 6f 20 62 79 74 65 73 20 6f 66 20 69 64 78 53 74  o bytes of idxSt
31ee9 72 20 63 6f 72 72 65 73 70 6f 6e 64 20 74 6f 20  r correspond to 
31eea 0a 2a 2a 20 74 68 65 20 63 6f 6e 73 74 72 61 69  .** the constrai
31eeb 6e 74 20 69 6e 20 73 71 6c 69 74 65 33 5f 69 6e  nt in sqlite3_in
31eec 64 65 78 5f 69 6e 66 6f 2e 61 43 6f 6e 73 74 72  dex_info.aConstr
31eed 61 69 6e 74 55 73 61 67 65 5b 5d 20 77 69 74 68  aintUsage[] with
31eee 0a 2a 2a 20 28 61 72 67 76 49 6e 64 65 78 3d 3d  .** (argvIndex==
31eef 31 29 20 65 74 63 2e 0a 2a 2a 0a 2a 2a 20 54 68  1) etc..**.** Th
31ef0 65 20 66 69 72 73 74 20 6f 66 20 65 61 63 68 20  e first of each 
31ef1 70 61 69 72 20 6f 66 20 62 79 74 65 73 20 69 6e  pair of bytes in
31ef2 20 69 64 78 53 74 72 20 69 64 65 6e 74 69 66 69   idxStr identifi
31ef3 65 73 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e  es the constrain
31ef4 74 0a 2a 2a 20 6f 70 65 72 61 74 6f 72 20 61 73  t.** operator as
31ef5 20 66 6f 6c 6c 6f 77 73 3a 0a 2a 2a 0a 2a 2a 20   follows:.**.** 
31ef6 20 20 4f 70 65 72 61 74 6f 72 20 20 20 20 42 79    Operator    By
31ef7 74 65 20 56 61 6c 75 65 0a 2a 2a 20 20 20 2d 2d  te Value.**   --
31ef8 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  ----------------
31ef9 2d 2d 2d 2d 0a 2a 2a 20 20 20 20 20 20 3d 20 20  ----.**      =  
31efa 20 20 20 20 20 20 30 78 34 31 20 28 27 41 27 29        0x41 ('A')
31efb 0a 2a 2a 20 20 20 20 20 3c 3d 20 20 20 20 20 20  .**     <=      
31efc 20 20 30 78 34 32 20 28 27 42 27 29 0a 2a 2a 20    0x42 ('B').** 
31efd 20 20 20 20 20 3c 20 20 20 20 20 20 20 20 30 78       <        0x
31efe 34 33 20 28 27 43 27 29 0a 2a 2a 20 20 20 20 20  43 ('C').**     
31eff 3e 3d 20 20 20 20 20 20 20 20 30 78 34 34 20 28  >=        0x44 (
31f00 27 44 27 29 0a 2a 2a 20 20 20 20 20 20 3e 20 20  'D').**      >  
31f01 20 20 20 20 20 20 30 78 34 35 20 28 27 45 27 29        0x45 ('E')
31f02 0a 2a 2a 20 20 20 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d  .**   ----------
31f03 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 0a 2a 2a 0a  ------------.**.
31f04 2a 2a 20 54 68 65 20 73 65 63 6f 6e 64 20 6f 66  ** The second of
31f05 20 65 61 63 68 20 70 61 69 72 20 6f 66 20 62 79   each pair of by
31f06 74 65 73 20 69 64 65 6e 74 69 66 69 65 73 20 74  tes identifies t
31f07 68 65 20 63 6f 6f 72 64 69 6e 61 74 65 20 63 6f  he coordinate co
31f08 6c 75 6d 6e 0a 2a 2a 20 74 6f 20 77 68 69 63 68  lumn.** to which
31f09 20 74 68 65 20 63 6f 6e 73 74 72 61 69 6e 74 20   the constraint 
31f0a 61 70 70 6c 69 65 73 2e 20 54 68 65 20 6c 65 66  applies. The lef
31f0b 74 6d 6f 73 74 20 63 6f 6f 72 64 69 6e 61 74 65  tmost coordinate
31f0c 20 63 6f 6c 75 6d 6e 0a 2a 2a 20 69 73 20 27 61   column.** is 'a
31f0d 27 2c 20 74 68 65 20 73 65 63 6f 6e 64 20 66 72  ', the second fr
31f0e 6f 6d 20 74 68 65 20 6c 65 66 74 20 27 62 27 20  om the left 'b' 
31f0f 65 74 63 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  etc..*/.static i
31f10 6e 74 20 72 74 72 65 65 42 65 73 74 49 6e 64 65  nt rtreeBestInde
31f11 78 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20 2a  x(sqlite3_vtab *
31f12 74 61 62 2c 20 73 71 6c 69 74 65 33 5f 69 6e 64  tab, sqlite3_ind
31f13 65 78 5f 69 6e 66 6f 20 2a 70 49 64 78 49 6e 66  ex_info *pIdxInf
31f14 6f 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  o){.  int rc = S
31f15 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
31f16 69 69 2c 20 63 43 6f 6c 3b 0a 0a 20 20 69 6e 74  ii, cCol;..  int
31f17 20 69 49 64 78 20 3d 20 30 3b 0a 20 20 63 68 61   iIdx = 0;.  cha
31f18 72 20 7a 49 64 78 53 74 72 5b 52 54 52 45 45 5f  r zIdxStr[RTREE_
31f19 4d 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 38  MAX_DIMENSIONS*8
31f1a 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 7a 49  +1];.  memset(zI
31f1b 64 78 53 74 72 2c 20 30 2c 20 73 69 7a 65 6f 66  dxStr, 0, sizeof
31f1c 28 7a 49 64 78 53 74 72 29 29 3b 0a 0a 20 20 61  (zIdxStr));..  a
31f1d 73 73 65 72 74 28 20 70 49 64 78 49 6e 66 6f 2d  ssert( pIdxInfo-
31f1e 3e 69 64 78 53 74 72 3d 3d 30 20 29 3b 0a 20 20  >idxStr==0 );.  
31f1f 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70 49 64  for(ii=0; ii<pId
31f20 78 49 6e 66 6f 2d 3e 6e 43 6f 6e 73 74 72 61 69  xInfo->nConstrai
31f21 6e 74 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 73  nt; ii++){.    s
31f22 74 72 75 63 74 20 73 71 6c 69 74 65 33 5f 69 6e  truct sqlite3_in
31f23 64 65 78 5f 63 6f 6e 73 74 72 61 69 6e 74 20 2a  dex_constraint *
31f24 70 20 3d 20 26 70 49 64 78 49 6e 66 6f 2d 3e 61  p = &pIdxInfo->a
31f25 43 6f 6e 73 74 72 61 69 6e 74 5b 69 69 5d 3b 0a  Constraint[ii];.
31f26 0a 20 20 20 20 69 66 28 20 70 2d 3e 75 73 61 62  .    if( p->usab
31f27 6c 65 20 26 26 20 70 2d 3e 69 43 6f 6c 75 6d 6e  le && p->iColumn
31f28 3d 3d 30 20 26 26 20 70 2d 3e 6f 70 3d 3d 53 51  ==0 && p->op==SQ
31f29 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
31f2a 52 41 49 4e 54 5f 45 51 20 29 7b 0a 20 20 20 20  RAINT_EQ ){.    
31f2b 20 20 2f 2a 20 57 65 20 68 61 76 65 20 61 6e 20    /* We have an 
31f2c 65 71 75 61 6c 69 74 79 20 63 6f 6e 73 74 72 61  equality constra
31f2d 69 6e 74 20 6f 6e 20 74 68 65 20 72 6f 77 69 64  int on the rowid
31f2e 2e 20 55 73 65 20 73 74 72 61 74 65 67 79 20 31  . Use strategy 1
31f2f 2e 20 2a 2f 0a 20 20 20 20 20 20 69 6e 74 20 6a  . */.      int j
31f30 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
31f31 30 3b 20 6a 6a 3c 69 69 3b 20 6a 6a 2b 2b 29 7b  0; jj<ii; jj++){
31f32 0a 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66  .        pIdxInf
31f33 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73  o->aConstraintUs
31f34 61 67 65 5b 6a 6a 5d 2e 61 72 67 76 49 6e 64 65  age[jj].argvInde
31f35 78 20 3d 20 30 3b 0a 20 20 20 20 20 20 20 20 70  x = 0;.        p
31f36 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72  IdxInfo->aConstr
31f37 61 69 6e 74 55 73 61 67 65 5b 6a 6a 5d 2e 6f 6d  aintUsage[jj].om
31f38 69 74 20 3d 20 30 3b 0a 20 20 20 20 20 20 7d 0a  it = 0;.      }.
31f39 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
31f3a 69 64 78 4e 75 6d 20 3d 20 31 3b 0a 20 20 20 20  idxNum = 1;.    
31f3b 20 20 70 49 64 78 49 6e 66 6f 2d 3e 61 43 6f 6e    pIdxInfo->aCon
31f3c 73 74 72 61 69 6e 74 55 73 61 67 65 5b 69 69 5d  straintUsage[ii]
31f3d 2e 61 72 67 76 49 6e 64 65 78 20 3d 20 31 3b 0a  .argvIndex = 1;.
31f3e 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f 2d 3e        pIdxInfo->
31f3f 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61 67 65  aConstraintUsage
31f40 5b 6a 6a 5d 2e 6f 6d 69 74 20 3d 20 31 3b 0a 0a  [jj].omit = 1;..
31f41 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 73 74        /* This st
31f42 72 61 74 65 67 79 20 69 6e 76 6f 6c 76 65 73 20  rategy involves 
31f43 61 20 74 77 6f 20 72 6f 77 69 64 20 6c 6f 6f 6b  a two rowid look
31f44 75 70 73 20 6f 6e 20 61 6e 20 42 2d 54 72 65 65  ups on an B-Tree
31f45 20 73 74 72 75 63 74 75 72 65 73 0a 20 20 20 20   structures.    
31f46 20 20 2a 2a 20 61 6e 64 20 74 68 65 6e 20 61 20    ** and then a 
31f47 6c 69 6e 65 61 72 20 73 65 61 72 63 68 20 6f 66  linear search of
31f48 20 61 6e 20 52 2d 54 72 65 65 20 6e 6f 64 65 2e   an R-Tree node.
31f49 20 54 68 69 73 20 73 68 6f 75 6c 64 20 62 65 20   This should be 
31f4a 0a 20 20 20 20 20 20 2a 2a 20 63 6f 6e 73 69 64  .      ** consid
31f4b 65 72 65 64 20 61 6c 6d 6f 73 74 20 61 73 20 71  ered almost as q
31f4c 75 69 63 6b 20 61 73 20 61 20 64 69 72 65 63 74  uick as a direct
31f4d 20 72 6f 77 69 64 20 6c 6f 6f 6b 75 70 20 28 66   rowid lookup (f
31f4e 6f 72 20 77 68 69 63 68 20 0a 20 20 20 20 20 20  or which .      
31f4f 2a 2a 20 73 71 6c 69 74 65 20 75 73 65 73 20 61  ** sqlite uses a
31f50 6e 20 69 6e 74 65 72 6e 61 6c 20 63 6f 73 74 20  n internal cost 
31f51 6f 66 20 30 2e 30 29 2e 0a 20 20 20 20 20 20 2a  of 0.0)..      *
31f52 2f 20 0a 20 20 20 20 20 20 70 49 64 78 49 6e 66  / .      pIdxInf
31f53 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43 6f 73 74  o->estimatedCost
31f54 20 3d 20 31 30 2e 30 3b 0a 20 20 20 20 20 20 72   = 10.0;.      r
31f55 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b  eturn SQLITE_OK;
31f56 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66 28 20  .    }..    if( 
31f57 70 2d 3e 75 73 61 62 6c 65 20 26 26 20 70 2d 3e  p->usable && p->
31f58 69 43 6f 6c 75 6d 6e 3e 30 20 29 7b 0a 20 20 20  iColumn>0 ){.   
31f59 20 20 20 75 38 20 6f 70 20 3d 20 30 3b 0a 20 20     u8 op = 0;.  
31f5a 20 20 20 20 73 77 69 74 63 68 28 20 70 2d 3e 6f      switch( p->o
31f5b 70 20 29 7b 0a 20 20 20 20 20 20 20 20 63 61 73  p ){.        cas
31f5c 65 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43  e SQLITE_INDEX_C
31f5d 4f 4e 53 54 52 41 49 4e 54 5f 45 51 3a 20 6f 70  ONSTRAINT_EQ: op
31f5e 20 3d 20 52 54 52 45 45 5f 45 51 3b 20 62 72 65   = RTREE_EQ; bre
31f5f 61 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65  ak;.        case
31f60 20 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f   SQLITE_INDEX_CO
31f61 4e 53 54 52 41 49 4e 54 5f 47 54 3a 20 6f 70 20  NSTRAINT_GT: op 
31f62 3d 20 52 54 52 45 45 5f 47 54 3b 20 62 72 65 61  = RTREE_GT; brea
31f63 6b 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20  k;.        case 
31f64 53 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e  SQLITE_INDEX_CON
31f65 53 54 52 41 49 4e 54 5f 4c 45 3a 20 6f 70 20 3d  STRAINT_LE: op =
31f66 20 52 54 52 45 45 5f 4c 45 3b 20 62 72 65 61 6b   RTREE_LE; break
31f67 3b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53  ;.        case S
31f68 51 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53  QLITE_INDEX_CONS
31f69 54 52 41 49 4e 54 5f 4c 54 3a 20 6f 70 20 3d 20  TRAINT_LT: op = 
31f6a 52 54 52 45 45 5f 4c 54 3b 20 62 72 65 61 6b 3b  RTREE_LT; break;
31f6b 0a 20 20 20 20 20 20 20 20 63 61 73 65 20 53 51  .        case SQ
31f6c 4c 49 54 45 5f 49 4e 44 45 58 5f 43 4f 4e 53 54  LITE_INDEX_CONST
31f6d 52 41 49 4e 54 5f 47 45 3a 20 6f 70 20 3d 20 52  RAINT_GE: op = R
31f6e 54 52 45 45 5f 47 45 3b 20 62 72 65 61 6b 3b 0a  TREE_GE; break;.
31f6f 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 69 66        }.      if
31f70 28 20 6f 70 20 29 7b 0a 20 20 20 20 20 20 20 20  ( op ){.        
31f71 2f 2a 20 4d 61 6b 65 20 73 75 72 65 20 74 68 69  /* Make sure thi
31f72 73 20 70 61 72 74 69 63 75 6c 61 72 20 63 6f 6e  s particular con
31f73 73 74 72 61 69 6e 74 20 68 61 73 20 6e 6f 74 20  straint has not 
31f74 62 65 65 6e 20 75 73 65 64 20 62 65 66 6f 72 65  been used before
31f75 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 49 66 20  ..        ** If 
31f76 69 74 20 68 61 73 20 62 65 65 6e 20 75 73 65 64  it has been used
31f77 20 62 65 66 6f 72 65 2c 20 69 67 6e 6f 72 65 20   before, ignore 
31f78 69 74 2e 0a 20 20 20 20 20 20 20 20 2a 2a 0a 20  it..        **. 
31f79 20 20 20 20 20 20 20 2a 2a 20 41 20 3c 3d 20 6f         ** A <= o
31f7a 72 20 3c 20 63 61 6e 20 62 65 20 75 73 65 64 20  r < can be used 
31f7b 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
31f7c 69 6f 72 20 3e 3d 20 6f 72 20 3e 2e 0a 20 20 20  ior >= or >..   
31f7d 20 20 20 20 20 2a 2a 20 41 20 3e 3d 20 6f 72 20       ** A >= or 
31f7e 3e 20 63 61 6e 20 62 65 20 75 73 65 64 20 69 66  > can be used if
31f7f 20 74 68 65 72 65 20 69 73 20 61 20 70 72 69 6f   there is a prio
31f80 72 20 3c 20 6f 72 20 3c 3d 2e 0a 20 20 20 20 20  r < or <=..     
31f81 20 20 20 2a 2a 20 41 20 3c 3d 20 6f 72 20 3c 20     ** A <= or < 
31f82 69 73 20 64 69 73 71 75 61 6c 69 66 69 65 64 20  is disqualified 
31f83 69 66 20 74 68 65 72 65 20 69 73 20 61 20 70 72  if there is a pr
31f84 69 6f 72 20 3c 3d 2c 20 3c 2c 20 6f 72 20 3d 3d  ior <=, <, or ==
31f85 2e 0a 20 20 20 20 20 20 20 20 2a 2a 20 41 20 3e  ..        ** A >
31f86 3d 20 6f 72 20 3e 20 69 73 20 64 69 73 71 75 61  = or > is disqua
31f87 6c 69 66 69 65 64 20 69 66 20 74 68 65 72 65 20  lified if there 
31f88 69 73 20 61 20 70 72 69 6f 72 20 3e 3d 2c 20 3e  is a prior >=, >
31f89 2c 20 6f 72 20 3d 3d 2e 0a 20 20 20 20 20 20 20  , or ==..       
31f8a 20 2a 2a 20 41 20 3d 3d 20 69 73 20 64 69 73 71   ** A == is disq
31f8b 75 61 6c 69 66 65 64 20 69 66 20 74 68 65 72 65  ualifed if there
31f8c 20 69 73 20 61 6e 79 20 70 72 69 6f 72 20 63 6f   is any prior co
31f8d 6e 73 74 72 61 69 6e 74 2e 0a 20 20 20 20 20 20  nstraint..      
31f8e 20 20 2a 2f 0a 20 20 20 20 20 20 20 20 69 6e 74    */.        int
31f8f 20 6a 2c 20 6f 70 6d 73 6b 3b 0a 20 20 20 20 20   j, opmsk;.     
31f90 20 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20     static const 
31f91 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 63 6f  unsigned char co
31f92 6d 70 61 74 69 62 6c 65 5b 5d 20 3d 20 7b 20 30  mpatible[] = { 0
31f93 2c 20 30 2c 20 31 2c 20 31 2c 20 32 2c 20 32 20  , 0, 1, 1, 2, 2 
31f94 7d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  };.        asser
31f95 74 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54  t( compatible[RT
31f96 52 45 45 5f 45 51 20 26 20 37 5d 3d 3d 30 20 29  REE_EQ & 7]==0 )
31f97 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74  ;.        assert
31f98 28 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52  ( compatible[RTR
31f99 45 45 5f 4c 54 20 26 20 37 5d 3d 3d 31 20 29 3b  EE_LT & 7]==1 );
31f9a 0a 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28  .        assert(
31f9b 20 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45   compatible[RTRE
31f9c 45 5f 4c 45 20 26 20 37 5d 3d 3d 31 20 29 3b 0a  E_LE & 7]==1 );.
31f9d 20 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20          assert( 
31f9e 63 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45  compatible[RTREE
31f9f 5f 47 54 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20  _GT & 7]==2 );. 
31fa0 20 20 20 20 20 20 20 61 73 73 65 72 74 28 20 63         assert( c
31fa1 6f 6d 70 61 74 69 62 6c 65 5b 52 54 52 45 45 5f  ompatible[RTREE_
31fa2 47 45 20 26 20 37 5d 3d 3d 32 20 29 3b 0a 20 20  GE & 7]==2 );.  
31fa3 20 20 20 20 20 20 63 43 6f 6c 20 3d 20 70 2d 3e        cCol = p->
31fa4 69 43 6f 6c 75 6d 6e 20 2d 20 31 20 2b 20 27 61  iColumn - 1 + 'a
31fa5 27 3b 0a 20 20 20 20 20 20 20 20 6f 70 6d 73 6b  ';.        opmsk
31fa6 20 3d 20 63 6f 6d 70 61 74 69 62 6c 65 5b 6f 70   = compatible[op
31fa7 20 26 20 37 5d 3b 0a 20 20 20 20 20 20 20 20 66   & 7];.        f
31fa8 6f 72 28 6a 3d 30 3b 20 6a 3c 69 49 64 78 3b 20  or(j=0; j<iIdx; 
31fa9 6a 2b 3d 32 29 7b 0a 20 20 20 20 20 20 20 20 20  j+=2){.         
31faa 20 69 66 28 20 7a 49 64 78 53 74 72 5b 6a 2b 31   if( zIdxStr[j+1
31fab 5d 3d 3d 63 43 6f 6c 20 26 26 20 28 63 6f 6d 70  ]==cCol && (comp
31fac 61 74 69 62 6c 65 5b 7a 49 64 78 53 74 72 5b 6a  atible[zIdxStr[j
31fad 5d 20 26 20 37 5d 20 26 20 6f 70 6d 73 6b 29 21  ] & 7] & opmsk)!
31fae 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  =0 ){.          
31faf 20 20 6f 70 20 3d 20 30 3b 0a 20 20 20 20 20 20    op = 0;.      
31fb0 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20        break;.   
31fb1 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20         }.       
31fb2 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20   }.      }.     
31fb3 20 69 66 28 20 6f 70 20 29 7b 0a 20 20 20 20 20   if( op ){.     
31fb4 20 20 20 61 73 73 65 72 74 28 20 69 49 64 78 3c     assert( iIdx<
31fb5 73 69 7a 65 6f 66 28 7a 49 64 78 53 74 72 29 2d  sizeof(zIdxStr)-
31fb6 31 20 29 3b 0a 20 20 20 20 20 20 20 20 7a 49 64  1 );.        zId
31fb7 78 53 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 6f  xStr[iIdx++] = o
31fb8 70 3b 0a 20 20 20 20 20 20 20 20 7a 49 64 78 53  p;.        zIdxS
31fb9 74 72 5b 69 49 64 78 2b 2b 5d 20 3d 20 63 43 6f  tr[iIdx++] = cCo
31fba 6c 3b 0a 20 20 20 20 20 20 20 20 70 49 64 78 49  l;.        pIdxI
31fbb 6e 66 6f 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74  nfo->aConstraint
31fbc 55 73 61 67 65 5b 69 69 5d 2e 61 72 67 76 49 6e  Usage[ii].argvIn
31fbd 64 65 78 20 3d 20 28 69 49 64 78 2f 32 29 3b 0a  dex = (iIdx/2);.
31fbe 20 20 20 20 20 20 20 20 70 49 64 78 49 6e 66 6f          pIdxInfo
31fbf 2d 3e 61 43 6f 6e 73 74 72 61 69 6e 74 55 73 61  ->aConstraintUsa
31fc0 67 65 5b 69 69 5d 2e 6f 6d 69 74 20 3d 20 31 3b  ge[ii].omit = 1;
31fc1 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20  .      }.    }. 
31fc2 20 7d 0a 0a 20 20 70 49 64 78 49 6e 66 6f 2d 3e   }..  pIdxInfo->
31fc3 69 64 78 4e 75 6d 20 3d 20 32 3b 0a 20 20 70 49  idxNum = 2;.  pI
31fc4 64 78 49 6e 66 6f 2d 3e 6e 65 65 64 54 6f 46 72  dxInfo->needToFr
31fc5 65 65 49 64 78 53 74 72 20 3d 20 31 3b 0a 20 20  eeIdxStr = 1;.  
31fc6 69 66 28 20 69 49 64 78 3e 30 20 26 26 20 30 3d  if( iIdx>0 && 0=
31fc7 3d 28 70 49 64 78 49 6e 66 6f 2d 3e 69 64 78 53  =(pIdxInfo->idxS
31fc8 74 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  tr = sqlite3_mpr
31fc9 69 6e 74 66 28 22 25 73 22 2c 20 7a 49 64 78 53  intf("%s", zIdxS
31fca 74 72 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  tr)) ){.    retu
31fcb 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
31fcc 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 20 69  .  }.  assert( i
31fcd 49 64 78 3e 3d 30 20 29 3b 0a 20 20 70 49 64 78  Idx>=0 );.  pIdx
31fce 49 6e 66 6f 2d 3e 65 73 74 69 6d 61 74 65 64 43  Info->estimatedC
31fcf 6f 73 74 20 3d 20 28 32 30 30 30 30 30 30 2e 30  ost = (2000000.0
31fd0 20 2f 20 28 64 6f 75 62 6c 65 29 28 69 49 64 78   / (double)(iIdx
31fd1 20 2b 20 31 29 29 3b 0a 20 20 72 65 74 75 72 6e   + 1));.  return
31fd2 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65   rc;.}../*.** Re
31fd3 74 75 72 6e 20 74 68 65 20 4e 2d 64 69 6d 65 6e  turn the N-dimen
31fd4 73 69 6f 6e 61 6c 20 76 6f 6c 75 6d 6e 20 6f 66  sional volumn of
31fd5 20 74 68 65 20 63 65 6c 6c 20 73 74 6f 72 65 64   the cell stored
31fd6 20 69 6e 20 2a 70 2e 0a 2a 2f 0a 73 74 61 74 69   in *p..*/.stati
31fd7 63 20 66 6c 6f 61 74 20 63 65 6c 6c 41 72 65 61  c float cellArea
31fd8 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20  (Rtree *pRtree, 
31fd9 52 74 72 65 65 43 65 6c 6c 20 2a 70 29 7b 0a 20  RtreeCell *p){. 
31fda 20 66 6c 6f 61 74 20 61 72 65 61 20 3d 20 31 2e   float area = 1.
31fdb 30 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66  0;.  int ii;.  f
31fdc 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74  or(ii=0; ii<(pRt
31fdd 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69  ree->nDim*2); ii
31fde 2b 3d 32 29 7b 0a 20 20 20 20 61 72 65 61 20 3d  +=2){.    area =
31fdf 20 61 72 65 61 20 2a 20 28 44 43 4f 4f 52 44 28   area * (DCOORD(
31fe0 70 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 29  p->aCoord[ii+1])
31fe1 20 2d 20 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f   - DCOORD(p->aCo
31fe2 6f 72 64 5b 69 69 5d 29 29 3b 0a 20 20 7d 0a 20  ord[ii]));.  }. 
31fe3 20 72 65 74 75 72 6e 20 61 72 65 61 3b 0a 7d 0a   return area;.}.
31fe4 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68  ./*.** Return th
31fe5 65 20 6d 61 72 67 69 6e 20 6c 65 6e 67 74 68 20  e margin length 
31fe6 6f 66 20 63 65 6c 6c 20 70 2e 20 54 68 65 20 6d  of cell p. The m
31fe7 61 72 67 69 6e 20 6c 65 6e 67 74 68 20 69 73 20  argin length is 
31fe8 74 68 65 20 73 75 6d 0a 2a 2a 20 6f 66 20 74 68  the sum.** of th
31fe9 65 20 6f 62 6a 65 63 74 73 20 73 69 7a 65 20 69  e objects size i
31fea 6e 20 65 61 63 68 20 64 69 6d 65 6e 73 69 6f 6e  n each dimension
31feb 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 66 6c 6f 61  ..*/.static floa
31fec 74 20 63 65 6c 6c 4d 61 72 67 69 6e 28 52 74 72  t cellMargin(Rtr
31fed 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72 65  ee *pRtree, Rtre
31fee 65 43 65 6c 6c 20 2a 70 29 7b 0a 20 20 66 6c 6f  eCell *p){.  flo
31fef 61 74 20 6d 61 72 67 69 6e 20 3d 20 30 2e 30 3b  at margin = 0.0;
31ff0 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72  .  int ii;.  for
31ff1 28 69 69 3d 30 3b 20 69 69 3c 28 70 52 74 72 65  (ii=0; ii<(pRtre
31ff2 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 69 69 2b 3d  e->nDim*2); ii+=
31ff3 32 29 7b 0a 20 20 20 20 6d 61 72 67 69 6e 20 2b  2){.    margin +
31ff4 3d 20 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  = (DCOORD(p->aCo
31ff5 6f 72 64 5b 69 69 2b 31 5d 29 20 2d 20 44 43 4f  ord[ii+1]) - DCO
31ff6 4f 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 69 69  ORD(p->aCoord[ii
31ff7 5d 29 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72  ]));.  }.  retur
31ff8 6e 20 6d 61 72 67 69 6e 3b 0a 7d 0a 0a 2f 2a 0a  n margin;.}../*.
31ff9 2a 2a 20 53 74 6f 72 65 20 74 68 65 20 75 6e 69  ** Store the uni
31ffa 6f 6e 20 6f 66 20 63 65 6c 6c 73 20 70 31 20 61  on of cells p1 a
31ffb 6e 64 20 70 32 20 69 6e 20 70 31 2e 0a 2a 2f 0a  nd p2 in p1..*/.
31ffc 73 74 61 74 69 63 20 76 6f 69 64 20 63 65 6c 6c  static void cell
31ffd 55 6e 69 6f 6e 28 52 74 72 65 65 20 2a 70 52 74  Union(Rtree *pRt
31ffe 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
31fff 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  p1, RtreeCell *p
32000 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  2){.  int ii;.  
32001 69 66 28 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  if( pRtree->eCoo
32002 72 64 54 79 70 65 3d 3d 52 54 52 45 45 5f 43 4f  rdType==RTREE_CO
32003 4f 52 44 5f 52 45 41 4c 33 32 20 29 7b 0a 20 20  ORD_REAL32 ){.  
32004 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 28    for(ii=0; ii<(
32005 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32 29 3b  pRtree->nDim*2);
32006 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20 20 70   ii+=2){.      p
32007 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 20  1->aCoord[ii].f 
32008 3d 20 4d 49 4e 28 70 31 2d 3e 61 43 6f 6f 72 64  = MIN(p1->aCoord
32009 5b 69 69 5d 2e 66 2c 20 70 32 2d 3e 61 43 6f 6f  [ii].f, p2->aCoo
3200a 72 64 5b 69 69 5d 2e 66 29 3b 0a 20 20 20 20 20  rd[ii].f);.     
3200b 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31   p1->aCoord[ii+1
3200c 5d 2e 66 20 3d 20 4d 41 58 28 70 31 2d 3e 61 43  ].f = MAX(p1->aC
3200d 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66 2c 20 70 32  oord[ii+1].f, p2
3200e 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  ->aCoord[ii+1].f
3200f 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 65 6c 73 65  );.    }.  }else
32010 7b 0a 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20  {.    for(ii=0; 
32011 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d  ii<(pRtree->nDim
32012 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20  *2); ii+=2){.   
32013 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b 69 69     p1->aCoord[ii
32014 5d 2e 69 20 3d 20 4d 49 4e 28 70 31 2d 3e 61 43  ].i = MIN(p1->aC
32015 6f 6f 72 64 5b 69 69 5d 2e 69 2c 20 70 32 2d 3e  oord[ii].i, p2->
32016 61 43 6f 6f 72 64 5b 69 69 5d 2e 69 29 3b 0a 20  aCoord[ii].i);. 
32017 20 20 20 20 20 70 31 2d 3e 61 43 6f 6f 72 64 5b       p1->aCoord[
32018 69 69 2b 31 5d 2e 69 20 3d 20 4d 41 58 28 70 31  ii+1].i = MAX(p1
32019 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 69  ->aCoord[ii+1].i
3201a 2c 20 70 32 2d 3e 61 43 6f 6f 72 64 5b 69 69 2b  , p2->aCoord[ii+
3201b 31 5d 2e 69 29 3b 0a 20 20 20 20 7d 0a 20 20 7d  1].i);.    }.  }
3201c 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 74 75 72 6e  .}../*.** Return
3201d 20 74 72 75 65 20 69 66 20 74 68 65 20 61 72 65   true if the are
3201e 61 20 63 6f 76 65 72 65 64 20 62 79 20 70 32 20  a covered by p2 
3201f 69 73 20 61 20 73 75 62 73 65 74 20 6f 66 20 74  is a subset of t
32020 68 65 20 61 72 65 61 20 63 6f 76 65 72 65 64 0a  he area covered.
32021 2a 2a 20 62 79 20 70 31 2e 20 46 61 6c 73 65 20  ** by p1. False 
32022 6f 74 68 65 72 77 69 73 65 2e 0a 2a 2f 0a 73 74  otherwise..*/.st
32023 61 74 69 63 20 69 6e 74 20 63 65 6c 6c 43 6f 6e  atic int cellCon
32024 74 61 69 6e 73 28 52 74 72 65 65 20 2a 70 52 74  tains(Rtree *pRt
32025 72 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a  ree, RtreeCell *
32026 70 31 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  p1, RtreeCell *p
32027 32 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20  2){.  int ii;.  
32028 69 6e 74 20 69 73 49 6e 74 20 3d 20 28 70 52 74  int isInt = (pRt
32029 72 65 65 2d 3e 65 43 6f 6f 72 64 54 79 70 65 3d  ree->eCoordType=
3202a 3d 52 54 52 45 45 5f 43 4f 4f 52 44 5f 49 4e 54  =RTREE_COORD_INT
3202b 33 32 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b  32);.  for(ii=0;
3202c 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69   ii<(pRtree->nDi
3202d 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20  m*2); ii+=2){.  
3202e 20 20 52 74 72 65 65 43 6f 6f 72 64 20 2a 61 31    RtreeCoord *a1
3202f 20 3d 20 26 70 31 2d 3e 61 43 6f 6f 72 64 5b 69   = &p1->aCoord[i
32030 69 5d 3b 0a 20 20 20 20 52 74 72 65 65 43 6f 6f  i];.    RtreeCoo
32031 72 64 20 2a 61 32 20 3d 20 26 70 32 2d 3e 61 43  rd *a2 = &p2->aC
32032 6f 6f 72 64 5b 69 69 5d 3b 0a 20 20 20 20 69 66  oord[ii];.    if
32033 28 20 28 21 69 73 49 6e 74 20 26 26 20 28 61 32  ( (!isInt && (a2
32034 5b 30 5d 2e 66 3c 61 31 5b 30 5d 2e 66 20 7c 7c  [0].f<a1[0].f ||
32035 20 61 32 5b 31 5d 2e 66 3e 61 31 5b 31 5d 2e 66   a2[1].f>a1[1].f
32036 29 29 20 0a 20 20 20 20 20 7c 7c 20 28 20 69 73  )) .     || ( is
32037 49 6e 74 20 26 26 20 28 61 32 5b 30 5d 2e 69 3c  Int && (a2[0].i<
32038 61 31 5b 30 5d 2e 69 20 7c 7c 20 61 32 5b 31 5d  a1[0].i || a2[1]
32039 2e 69 3e 61 31 5b 31 5d 2e 69 29 29 20 0a 20 20  .i>a1[1].i)) .  
3203a 20 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72    ){.      retur
3203b 6e 20 30 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20  n 0;.    }.  }. 
3203c 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 0a 2f 2a   return 1;.}../*
3203d 0a 2a 2a 20 52 65 74 75 72 6e 20 74 68 65 20 61  .** Return the a
3203e 6d 6f 75 6e 74 20 63 65 6c 6c 20 70 20 77 6f 75  mount cell p wou
3203f 6c 64 20 67 72 6f 77 20 62 79 20 69 66 20 69 74  ld grow by if it
32040 20 77 65 72 65 20 75 6e 69 6f 6e 65 64 20 77 69   were unioned wi
32041 74 68 20 70 43 65 6c 6c 2e 0a 2a 2f 0a 73 74 61  th pCell..*/.sta
32042 74 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 47 72  tic float cellGr
32043 6f 77 74 68 28 52 74 72 65 65 20 2a 70 52 74 72  owth(Rtree *pRtr
32044 65 65 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70  ee, RtreeCell *p
32045 2c 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65  , RtreeCell *pCe
32046 6c 6c 29 7b 0a 20 20 66 6c 6f 61 74 20 61 72 65  ll){.  float are
32047 61 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 63  a;.  RtreeCell c
32048 65 6c 6c 3b 0a 20 20 6d 65 6d 63 70 79 28 26 63  ell;.  memcpy(&c
32049 65 6c 6c 2c 20 70 2c 20 73 69 7a 65 6f 66 28 52  ell, p, sizeof(R
3204a 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 61 72  treeCell));.  ar
3204b 65 61 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52  ea = cellArea(pR
3204c 74 72 65 65 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  tree, &cell);.  
3204d 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
3204e 2c 20 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b  , &cell, pCell);
3204f 0a 20 20 72 65 74 75 72 6e 20 28 63 65 6c 6c 41  .  return (cellA
32050 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65 6c  rea(pRtree, &cel
32051 6c 29 2d 61 72 65 61 29 3b 0a 7d 0a 0a 23 69 66  l)-area);.}..#if
32052 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
32053 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45  EE_CHOOSESUBTREE
32054 20 7c 7c 20 56 41 52 49 41 4e 54 5f 52 53 54 41   || VARIANT_RSTA
32055 52 54 52 45 45 5f 53 50 4c 49 54 0a 73 74 61 74  RTREE_SPLIT.stat
32056 69 63 20 66 6c 6f 61 74 20 63 65 6c 6c 4f 76 65  ic float cellOve
32057 72 6c 61 70 28 0a 20 20 52 74 72 65 65 20 2a 70  rlap(.  Rtree *p
32058 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43  Rtree, .  RtreeC
32059 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72 65 65  ell *p, .  Rtree
3205a 43 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20  Cell *aCell, .  
3205b 69 6e 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 69 6e  int nCell, .  in
3205c 74 20 69 45 78 63 6c 75 64 65 0a 29 7b 0a 20 20  t iExclude.){.  
3205d 69 6e 74 20 69 69 3b 0a 20 20 66 6c 6f 61 74 20  int ii;.  float 
3205e 6f 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20  overlap = 0.0;. 
3205f 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43   for(ii=0; ii<nC
32060 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20  ell; ii++){.    
32061 69 66 28 20 69 69 21 3d 69 45 78 63 6c 75 64 65  if( ii!=iExclude
32062 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a 6a   ){.      int jj
32063 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6f 20  ;.      float o 
32064 3d 20 31 2e 30 3b 0a 20 20 20 20 20 20 66 6f 72  = 1.0;.      for
32065 28 6a 6a 3d 30 3b 20 6a 6a 3c 28 70 52 74 72 65  (jj=0; jj<(pRtre
32066 65 2d 3e 6e 44 69 6d 2a 32 29 3b 20 6a 6a 2b 3d  e->nDim*2); jj+=
32067 32 29 7b 0a 20 20 20 20 20 20 20 20 64 6f 75 62  2){.        doub
32068 6c 65 20 78 31 3b 0a 20 20 20 20 20 20 20 20 64  le x1;.        d
32069 6f 75 62 6c 65 20 78 32 3b 0a 0a 20 20 20 20 20  ouble x2;..     
3206a 20 20 20 78 31 20 3d 20 4d 41 58 28 44 43 4f 4f     x1 = MAX(DCOO
3206b 52 44 28 70 2d 3e 61 43 6f 6f 72 64 5b 6a 6a 5d  RD(p->aCoord[jj]
3206c 29 2c 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b  ), DCOORD(aCell[
3206d 69 69 5d 2e 61 43 6f 6f 72 64 5b 6a 6a 5d 29 29  ii].aCoord[jj]))
3206e 3b 0a 20 20 20 20 20 20 20 20 78 32 20 3d 20 4d  ;.        x2 = M
3206f 49 4e 28 44 43 4f 4f 52 44 28 70 2d 3e 61 43 6f  IN(DCOORD(p->aCo
32070 6f 72 64 5b 6a 6a 2b 31 5d 29 2c 20 44 43 4f 4f  ord[jj+1]), DCOO
32071 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
32072 6f 72 64 5b 6a 6a 2b 31 5d 29 29 3b 0a 0a 20 20  ord[jj+1]));..  
32073 20 20 20 20 20 20 69 66 28 20 78 32 3c 78 31 20        if( x2<x1 
32074 29 7b 0a 20 20 20 20 20 20 20 20 20 20 6f 20 3d  ){.          o =
32075 20 30 2e 30 3b 0a 20 20 20 20 20 20 20 20 20 20   0.0;.          
32076 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 7d  break;.        }
32077 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20 20 20  else{.          
32078 6f 20 3d 20 6f 20 2a 20 28 78 32 2d 78 31 29 3b  o = o * (x2-x1);
32079 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
3207a 20 7d 0a 20 20 20 20 20 20 6f 76 65 72 6c 61 70   }.      overlap
3207b 20 2b 3d 20 6f 3b 0a 20 20 20 20 7d 0a 20 20 7d   += o;.    }.  }
3207c 0a 20 20 72 65 74 75 72 6e 20 6f 76 65 72 6c 61  .  return overla
3207d 70 3b 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 69 66  p;.}.#endif..#if
3207e 20 56 41 52 49 41 4e 54 5f 52 53 54 41 52 54 52   VARIANT_RSTARTR
3207f 45 45 5f 43 48 4f 4f 53 45 53 55 42 54 52 45 45  EE_CHOOSESUBTREE
32080 0a 73 74 61 74 69 63 20 66 6c 6f 61 74 20 63 65  .static float ce
32081 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67 65  llOverlapEnlarge
32082 6d 65 6e 74 28 0a 20 20 52 74 72 65 65 20 2a 70  ment(.  Rtree *p
32083 52 74 72 65 65 2c 20 0a 20 20 52 74 72 65 65 43  Rtree, .  RtreeC
32084 65 6c 6c 20 2a 70 2c 20 0a 20 20 52 74 72 65 65  ell *p, .  Rtree
32085 43 65 6c 6c 20 2a 70 49 6e 73 65 72 74 2c 20 0a  Cell *pInsert, .
32086 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65    RtreeCell *aCe
32087 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  ll, .  int nCell
32088 2c 20 0a 20 20 69 6e 74 20 69 45 78 63 6c 75 64  , .  int iExclud
32089 65 0a 29 7b 0a 20 20 66 6c 6f 61 74 20 62 65 66  e.){.  float bef
3208a 6f 72 65 3b 0a 20 20 66 6c 6f 61 74 20 61 66 74  ore;.  float aft
3208b 65 72 3b 0a 20 20 62 65 66 6f 72 65 20 3d 20 63  er;.  before = c
3208c 65 6c 6c 4f 76 65 72 6c 61 70 28 70 52 74 72 65  ellOverlap(pRtre
3208d 65 2c 20 70 2c 20 61 43 65 6c 6c 2c 20 6e 43 65  e, p, aCell, nCe
3208e 6c 6c 2c 20 69 45 78 63 6c 75 64 65 29 3b 0a 20  ll, iExclude);. 
3208f 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65   cellUnion(pRtre
32090 65 2c 20 70 2c 20 70 49 6e 73 65 72 74 29 3b 0a  e, p, pInsert);.
32091 20 20 61 66 74 65 72 20 3d 20 63 65 6c 6c 4f 76    after = cellOv
32092 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 70 2c  erlap(pRtree, p,
32093 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 69   aCell, nCell, i
32094 45 78 63 6c 75 64 65 29 3b 0a 20 20 72 65 74 75  Exclude);.  retu
32095 72 6e 20 61 66 74 65 72 2d 62 65 66 6f 72 65 3b  rn after-before;
32096 0a 7d 0a 23 65 6e 64 69 66 0a 0a 0a 2f 2a 0a 2a  .}.#endif.../*.*
32097 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
32098 69 6d 70 6c 65 6d 65 6e 74 73 20 74 68 65 20 43  implements the C
32099 68 6f 6f 73 65 4c 65 61 66 20 61 6c 67 6f 72 69  hooseLeaf algori
3209a 74 68 6d 20 66 72 6f 6d 20 47 75 74 6d 61 6e 5b  thm from Gutman[
3209b 38 34 5d 2e 0a 2a 2a 20 43 68 6f 6f 73 65 53 75  84]..** ChooseSu
3209c 62 54 72 65 65 20 69 6e 20 72 2a 74 72 65 65 20  bTree in r*tree 
3209d 74 65 72 6d 69 6e 6f 6c 6f 67 79 2e 0a 2a 2f 0a  terminology..*/.
3209e 73 74 61 74 69 63 20 69 6e 74 20 43 68 6f 6f 73  static int Choos
3209f 65 4c 65 61 66 28 0a 20 20 52 74 72 65 65 20 2a  eLeaf(.  Rtree *
320a0 70 52 74 72 65 65 2c 20 20 20 20 20 20 20 20 20  pRtree,         
320a1 20 20 20 20 20 20 2f 2a 20 52 74 72 65 65 20 74        /* Rtree t
320a2 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72 65 65 43  able */.  RtreeC
320a3 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 20 20 20 20  ell *pCell,     
320a4 20 20 20 20 20 20 20 2f 2a 20 43 65 6c 6c 20 74         /* Cell t
320a5 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 20 72 74  o insert into rt
320a6 72 65 65 20 2a 2f 0a 20 20 69 6e 74 20 69 48 65  ree */.  int iHe
320a7 69 67 68 74 2c 20 20 20 20 20 20 20 20 20 20 20  ight,           
320a8 20 20 20 20 20 20 2f 2a 20 48 65 69 67 68 74 20        /* Height 
320a9 6f 66 20 73 75 62 2d 74 72 65 65 20 72 6f 6f 74  of sub-tree root
320aa 65 64 20 61 74 20 70 43 65 6c 6c 20 2a 2f 0a 20  ed at pCell */. 
320ab 20 52 74 72 65 65 4e 6f 64 65 20 2a 2a 70 70 4c   RtreeNode **ppL
320ac 65 61 66 20 20 20 20 20 20 20 20 20 20 20 2f 2a  eaf           /*
320ad 20 4f 55 54 3a 20 53 65 6c 65 63 74 65 64 20 6c   OUT: Selected l
320ae 65 61 66 20 70 61 67 65 20 2a 2f 0a 29 7b 0a 20  eaf page */.){. 
320af 20 69 6e 74 20 72 63 3b 0a 20 20 69 6e 74 20 69   int rc;.  int i
320b0 69 3b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  i;.  RtreeNode *
320b1 70 4e 6f 64 65 3b 0a 20 20 72 63 20 3d 20 6e 6f  pNode;.  rc = no
320b2 64 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65  deAcquire(pRtree
320b3 2c 20 31 2c 20 30 2c 20 26 70 4e 6f 64 65 29 3b  , 1, 0, &pNode);
320b4 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63  ..  for(ii=0; rc
320b5 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
320b6 69 3c 28 70 52 74 72 65 65 2d 3e 69 44 65 70 74  i<(pRtree->iDept
320b7 68 2d 69 48 65 69 67 68 74 29 3b 20 69 69 2b 2b  h-iHeight); ii++
320b8 29 7b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ){.    int iCell
320b9 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 69 6e  ;.    sqlite3_in
320ba 74 36 34 20 69 42 65 73 74 3b 0a 0a 20 20 20 20  t64 iBest;..    
320bb 66 6c 6f 61 74 20 66 4d 69 6e 47 72 6f 77 74 68  float fMinGrowth
320bc 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 4d 69 6e  ;.    float fMin
320bd 41 72 65 61 3b 0a 20 20 20 20 66 6c 6f 61 74 20  Area;.    float 
320be 66 4d 69 6e 4f 76 65 72 6c 61 70 3b 0a 0a 20 20  fMinOverlap;..  
320bf 20 20 69 6e 74 20 6e 43 65 6c 6c 20 3d 20 4e 43    int nCell = NC
320c0 45 4c 4c 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20  ELL(pNode);.    
320c1 52 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a  RtreeCell cell;.
320c2 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
320c3 43 68 69 6c 64 3b 0a 0a 20 20 20 20 52 74 72 65  Child;..    Rtre
320c4 65 43 65 6c 6c 20 2a 61 43 65 6c 6c 20 3d 20 30  eCell *aCell = 0
320c5 3b 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52  ;..#if VARIANT_R
320c6 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53 45 53  STARTREE_CHOOSES
320c7 55 42 54 52 45 45 0a 20 20 20 20 69 66 28 20 69  UBTREE.    if( i
320c8 69 3d 3d 28 70 52 74 72 65 65 2d 3e 69 44 65 70  i==(pRtree->iDep
320c9 74 68 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 69  th-1) ){.      i
320ca 6e 74 20 6a 6a 3b 0a 20 20 20 20 20 20 61 43 65  nt jj;.      aCe
320cb 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c  ll = sqlite3_mal
320cc 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
320cd 43 65 6c 6c 29 2a 6e 43 65 6c 6c 29 3b 0a 20 20  Cell)*nCell);.  
320ce 20 20 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29      if( !aCell )
320cf 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 53  {.        rc = S
320d0 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20  QLITE_NOMEM;.   
320d1 20 20 20 20 20 6e 6f 64 65 52 65 6c 65 61 73 65       nodeRelease
320d2 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
320d3 0a 20 20 20 20 20 20 20 20 70 4e 6f 64 65 20 3d  .        pNode =
320d4 20 30 3b 0a 20 20 20 20 20 20 20 20 63 6f 6e 74   0;.        cont
320d5 69 6e 75 65 3b 0a 20 20 20 20 20 20 7d 0a 20 20  inue;.      }.  
320d6 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
320d7 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29 7b 0a 20  <nCell; jj++){. 
320d8 20 20 20 20 20 20 20 6e 6f 64 65 47 65 74 43 65         nodeGetCe
320d9 6c 6c 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ll(pRtree, pNode
320da 2c 20 6a 6a 2c 20 26 61 43 65 6c 6c 5b 6a 6a 5d  , jj, &aCell[jj]
320db 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  );.      }.    }
320dc 0a 23 65 6e 64 69 66 0a 0a 20 20 20 20 2f 2a 20  .#endif..    /* 
320dd 53 65 6c 65 63 74 20 74 68 65 20 63 68 69 6c 64  Select the child
320de 20 6e 6f 64 65 20 77 68 69 63 68 20 77 69 6c 6c   node which will
320df 20 62 65 20 65 6e 6c 61 72 67 65 64 20 74 68 65   be enlarged the
320e0 20 6c 65 61 73 74 20 69 66 20 70 43 65 6c 6c 0a   least if pCell.
320e1 20 20 20 20 2a 2a 20 69 73 20 69 6e 73 65 72 74      ** is insert
320e2 65 64 20 69 6e 74 6f 20 69 74 2e 20 52 65 73 6f  ed into it. Reso
320e3 6c 76 65 20 74 69 65 73 20 62 79 20 63 68 6f 6f  lve ties by choo
320e4 73 69 6e 67 20 74 68 65 20 65 6e 74 72 79 20 77  sing the entry w
320e5 69 74 68 0a 20 20 20 20 2a 2a 20 74 68 65 20 73  ith.    ** the s
320e6 6d 61 6c 6c 65 73 74 20 61 72 65 61 2e 0a 20 20  mallest area..  
320e7 20 20 2a 2f 0a 20 20 20 20 66 6f 72 28 69 43 65    */.    for(iCe
320e8 6c 6c 3d 30 3b 20 69 43 65 6c 6c 3c 6e 43 65 6c  ll=0; iCell<nCel
320e9 6c 3b 20 69 43 65 6c 6c 2b 2b 29 7b 0a 20 20 20  l; iCell++){.   
320ea 20 20 20 66 6c 6f 61 74 20 67 72 6f 77 74 68 3b     float growth;
320eb 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 61 72 65  .      float are
320ec 61 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6f  a;.      float o
320ed 76 65 72 6c 61 70 20 3d 20 30 2e 30 3b 0a 20 20  verlap = 0.0;.  
320ee 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28      nodeGetCell(
320ef 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69  pRtree, pNode, i
320f0 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b 0a 20 20  Cell, &cell);.  
320f1 20 20 20 20 67 72 6f 77 74 68 20 3d 20 63 65 6c      growth = cel
320f2 6c 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20  lGrowth(pRtree, 
320f3 26 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 3b 0a 20  &cell, pCell);. 
320f4 20 20 20 20 20 61 72 65 61 20 3d 20 63 65 6c 6c       area = cell
320f5 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 63 65  Area(pRtree, &ce
320f6 6c 6c 29 3b 0a 23 69 66 20 56 41 52 49 41 4e 54  ll);.#if VARIANT
320f7 5f 52 53 54 41 52 54 52 45 45 5f 43 48 4f 4f 53  _RSTARTREE_CHOOS
320f8 45 53 55 42 54 52 45 45 0a 20 20 20 20 20 20 69  ESUBTREE.      i
320f9 66 28 20 69 69 3d 3d 28 70 52 74 72 65 65 2d 3e  f( ii==(pRtree->
320fa 69 44 65 70 74 68 2d 31 29 20 29 7b 0a 20 20 20  iDepth-1) ){.   
320fb 20 20 20 20 20 6f 76 65 72 6c 61 70 20 3d 20 63       overlap = c
320fc 65 6c 6c 4f 76 65 72 6c 61 70 45 6e 6c 61 72 67  ellOverlapEnlarg
320fd 65 6d 65 6e 74 28 70 52 74 72 65 65 2c 26 63 65  ement(pRtree,&ce
320fe 6c 6c 2c 70 43 65 6c 6c 2c 61 43 65 6c 6c 2c 6e  ll,pCell,aCell,n
320ff 43 65 6c 6c 2c 69 43 65 6c 6c 29 3b 0a 20 20 20  Cell,iCell);.   
32100 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20 20 20     }.#endif.    
32101 20 20 69 66 28 20 28 69 43 65 6c 6c 3d 3d 30 29    if( (iCell==0)
32102 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65   .       || (ove
32103 72 6c 61 70 3c 66 4d 69 6e 4f 76 65 72 6c 61 70  rlap<fMinOverlap
32104 29 20 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76  ) .       || (ov
32105 65 72 6c 61 70 3d 3d 66 4d 69 6e 4f 76 65 72 6c  erlap==fMinOverl
32106 61 70 20 26 26 20 67 72 6f 77 74 68 3c 66 4d 69  ap && growth<fMi
32107 6e 47 72 6f 77 74 68 29 0a 20 20 20 20 20 20 20  nGrowth).       
32108 7c 7c 20 28 6f 76 65 72 6c 61 70 3d 3d 66 4d 69  || (overlap==fMi
32109 6e 4f 76 65 72 6c 61 70 20 26 26 20 67 72 6f 77  nOverlap && grow
3210a 74 68 3d 3d 66 4d 69 6e 47 72 6f 77 74 68 20 26  th==fMinGrowth &
3210b 26 20 61 72 65 61 3c 66 4d 69 6e 41 72 65 61 29  & area<fMinArea)
3210c 0a 20 20 20 20 20 20 29 7b 0a 20 20 20 20 20 20  .      ){.      
3210d 20 20 66 4d 69 6e 4f 76 65 72 6c 61 70 20 3d 20    fMinOverlap = 
3210e 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20 20  overlap;.       
3210f 20 66 4d 69 6e 47 72 6f 77 74 68 20 3d 20 67 72   fMinGrowth = gr
32110 6f 77 74 68 3b 0a 20 20 20 20 20 20 20 20 66 4d  owth;.        fM
32111 69 6e 41 72 65 61 20 3d 20 61 72 65 61 3b 0a 20  inArea = area;. 
32112 20 20 20 20 20 20 20 69 42 65 73 74 20 3d 20 63         iBest = c
32113 65 6c 6c 2e 69 52 6f 77 69 64 3b 0a 20 20 20 20  ell.iRowid;.    
32114 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 20 20 20 73    }.    }..    s
32115 71 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65 6c  qlite3_free(aCel
32116 6c 29 3b 0a 20 20 20 20 72 63 20 3d 20 6e 6f 64  l);.    rc = nod
32117 65 41 63 71 75 69 72 65 28 70 52 74 72 65 65 2c  eAcquire(pRtree,
32118 20 69 42 65 73 74 2c 20 70 4e 6f 64 65 2c 20 26   iBest, pNode, &
32119 70 43 68 69 6c 64 29 3b 0a 20 20 20 20 6e 6f 64  pChild);.    nod
3211a 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
3211b 20 70 4e 6f 64 65 29 3b 0a 20 20 20 20 70 4e 6f   pNode);.    pNo
3211c 64 65 20 3d 20 70 43 68 69 6c 64 3b 0a 20 20 7d  de = pChild;.  }
3211d 0a 0a 20 20 2a 70 70 4c 65 61 66 20 3d 20 70 4e  ..  *ppLeaf = pN
3211e 6f 64 65 3b 0a 20 20 72 65 74 75 72 6e 20 72 63  ode;.  return rc
3211f 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 20 63 65 6c  ;.}../*.** A cel
32120 6c 20 77 69 74 68 20 74 68 65 20 73 61 6d 65 20  l with the same 
32121 63 6f 6e 74 65 6e 74 20 61 73 20 70 43 65 6c 6c  content as pCell
32122 20 68 61 73 20 6a 75 73 74 20 62 65 65 6e 20 69   has just been i
32123 6e 73 65 72 74 65 64 20 69 6e 74 6f 0a 2a 2a 20  nserted into.** 
32124 74 68 65 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20  the node pNode. 
32125 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 75 70  This function up
32126 64 61 74 65 73 20 74 68 65 20 62 6f 75 6e 64 69  dates the boundi
32127 6e 67 20 62 6f 78 20 63 65 6c 6c 73 20 69 6e 0a  ng box cells in.
32128 2a 2a 20 61 6c 6c 20 61 6e 63 65 73 74 6f 72 20  ** all ancestor 
32129 65 6c 65 6d 65 6e 74 73 2e 0a 2a 2f 0a 73 74 61  elements..*/.sta
3212a 74 69 63 20 76 6f 69 64 20 41 64 6a 75 73 74 54  tic void AdjustT
3212b 72 65 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ree(.  Rtree *pR
3212c 74 72 65 65 2c 20 20 20 20 20 20 20 20 20 20 20  tree,           
3212d 20 20 20 20 20 20 20 20 20 2f 2a 20 52 74 72 65           /* Rtre
3212e 65 20 74 61 62 6c 65 20 2a 2f 0a 20 20 52 74 72  e table */.  Rtr
3212f 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c 20 20  eeNode *pNode,  
32130 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32131 2a 20 41 64 6a 75 73 74 20 61 6e 63 65 73 74 72  * Adjust ancestr
32132 79 20 6f 66 20 74 68 69 73 20 6e 6f 64 65 2e 20  y of this node. 
32133 2a 2f 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  */.  RtreeCell *
32134 70 43 65 6c 6c 20 20 20 20 20 20 20 20 20 20 20  pCell           
32135 20 20 20 20 20 20 20 2f 2a 20 54 68 69 73 20 63         /* This c
32136 65 6c 6c 20 77 61 73 20 6a 75 73 74 20 69 6e 73  ell was just ins
32137 65 72 74 65 64 20 2a 2f 0a 29 7b 0a 20 20 52 74  erted */.){.  Rt
32138 72 65 65 4e 6f 64 65 20 2a 70 20 3d 20 70 4e 6f  reeNode *p = pNo
32139 64 65 3b 0a 20 20 77 68 69 6c 65 28 20 70 2d 3e  de;.  while( p->
3213a 70 50 61 72 65 6e 74 20 29 7b 0a 20 20 20 20 52  pParent ){.    R
3213b 74 72 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20  treeCell cell;. 
3213c 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50     RtreeNode *pP
3213d 61 72 65 6e 74 20 3d 20 70 2d 3e 70 50 61 72 65  arent = p->pPare
3213e 6e 74 3b 0a 20 20 20 20 69 6e 74 20 69 43 65 6c  nt;.    int iCel
3213f 6c 20 3d 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e  l = nodeParentIn
32140 64 65 78 28 70 52 74 72 65 65 2c 20 70 29 3b 0a  dex(pRtree, p);.
32141 0a 20 20 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c  .    nodeGetCell
32142 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74  (pRtree, pParent
32143 2c 20 69 43 65 6c 6c 2c 20 26 63 65 6c 6c 29 3b  , iCell, &cell);
32144 0a 20 20 20 20 69 66 28 20 21 63 65 6c 6c 43 6f  .    if( !cellCo
32145 6e 74 61 69 6e 73 28 70 52 74 72 65 65 2c 20 26  ntains(pRtree, &
32146 63 65 6c 6c 2c 20 70 43 65 6c 6c 29 20 29 7b 0a  cell, pCell) ){.
32147 20 20 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28        cellUnion(
32148 70 52 74 72 65 65 2c 20 26 63 65 6c 6c 2c 20 70  pRtree, &cell, p
32149 43 65 6c 6c 29 3b 0a 20 20 20 20 20 20 6e 6f 64  Cell);.      nod
3214a 65 4f 76 65 72 77 72 69 74 65 43 65 6c 6c 28 70  eOverwriteCell(p
3214b 52 74 72 65 65 2c 20 70 50 61 72 65 6e 74 2c 20  Rtree, pParent, 
3214c 26 63 65 6c 6c 2c 20 69 43 65 6c 6c 29 3b 0a 20  &cell, iCell);. 
3214d 20 20 20 7d 0a 20 0a 20 20 20 20 70 20 3d 20 70     }. .    p = p
3214e 50 61 72 65 6e 74 3b 0a 20 20 7d 0a 7d 0a 0a 2f  Parent;.  }.}../
3214f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d 61 70 70 69  *.** Write mappi
32150 6e 67 20 28 69 52 6f 77 69 64 2d 3e 69 4e 6f 64  ng (iRowid->iNod
32151 65 29 20 74 6f 20 74 68 65 20 3c 72 74 72 65 65  e) to the <rtree
32152 3e 5f 72 6f 77 69 64 20 74 61 62 6c 65 2e 0a 2a  >_rowid table..*
32153 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 6f 77  /.static int row
32154 69 64 57 72 69 74 65 28 52 74 72 65 65 20 2a 70  idWrite(Rtree *p
32155 52 74 72 65 65 2c 20 73 71 6c 69 74 65 33 5f 69  Rtree, sqlite3_i
32156 6e 74 36 34 20 69 52 6f 77 69 64 2c 20 73 71 6c  nt64 iRowid, sql
32157 69 74 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65  ite3_int64 iNode
32158 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  ){.  sqlite3_bin
32159 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
3215a 70 57 72 69 74 65 52 6f 77 69 64 2c 20 31 2c 20  pWriteRowid, 1, 
3215b 69 52 6f 77 69 64 29 3b 0a 20 20 73 71 6c 69 74  iRowid);.  sqlit
3215c 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28 70 52  e3_bind_int64(pR
3215d 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69  tree->pWriteRowi
3215e 64 2c 20 32 2c 20 69 4e 6f 64 65 29 3b 0a 20 20  d, 2, iNode);.  
3215f 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74  sqlite3_step(pRt
32160 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
32161 29 3b 0a 20 20 72 65 74 75 72 6e 20 73 71 6c 69  );.  return sqli
32162 74 65 33 5f 72 65 73 65 74 28 70 52 74 72 65 65  te3_reset(pRtree
32163 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 29 3b 0a  ->pWriteRowid);.
32164 7d 0a 0a 2f 2a 0a 2a 2a 20 57 72 69 74 65 20 6d  }../*.** Write m
32165 61 70 70 69 6e 67 20 28 69 4e 6f 64 65 2d 3e 69  apping (iNode->i
32166 50 61 72 29 20 74 6f 20 74 68 65 20 3c 72 74 72  Par) to the <rtr
32167 65 65 3e 5f 70 61 72 65 6e 74 20 74 61 62 6c 65  ee>_parent table
32168 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
32169 70 61 72 65 6e 74 57 72 69 74 65 28 52 74 72 65  parentWrite(Rtre
3216a 65 20 2a 70 52 74 72 65 65 2c 20 73 71 6c 69 74  e *pRtree, sqlit
3216b 65 33 5f 69 6e 74 36 34 20 69 4e 6f 64 65 2c 20  e3_int64 iNode, 
3216c 73 71 6c 69 74 65 33 5f 69 6e 74 36 34 20 69 50  sqlite3_int64 iP
3216d 61 72 29 7b 0a 20 20 73 71 6c 69 74 65 33 5f 62  ar){.  sqlite3_b
3216e 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
3216f 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74 2c 20  ->pWriteParent, 
32170 31 2c 20 69 4e 6f 64 65 29 3b 0a 20 20 73 71 6c  1, iNode);.  sql
32171 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
32172 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61  pRtree->pWritePa
32173 72 65 6e 74 2c 20 32 2c 20 69 50 61 72 29 3b 0a  rent, 2, iPar);.
32174 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28 70    sqlite3_step(p
32175 52 74 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72  Rtree->pWritePar
32176 65 6e 74 29 3b 0a 20 20 72 65 74 75 72 6e 20 73  ent);.  return s
32177 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
32178 72 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e  ree->pWriteParen
32179 74 29 3b 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e  t);.}..static in
3217a 74 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c  t rtreeInsertCel
3217b 6c 28 52 74 72 65 65 20 2a 2c 20 52 74 72 65 65  l(Rtree *, Rtree
3217c 4e 6f 64 65 20 2a 2c 20 52 74 72 65 65 43 65 6c  Node *, RtreeCel
3217d 6c 20 2a 2c 20 69 6e 74 29 3b 0a 0a 23 69 66 20  l *, int);..#if 
3217e 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
3217f 4c 49 4e 45 41 52 5f 53 50 4c 49 54 0a 2f 2a 0a  LINEAR_SPLIT./*.
32180 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f  ** Implementatio
32181 6e 20 6f 66 20 74 68 65 20 6c 69 6e 65 61 72 20  n of the linear 
32182 76 61 72 69 61 6e 74 20 6f 66 20 74 68 65 20 50  variant of the P
32183 69 63 6b 4e 65 78 74 28 29 20 66 75 6e 63 74 69  ickNext() functi
32184 6f 6e 20 66 72 6f 6d 0a 2a 2a 20 47 75 74 74 6d  on from.** Guttm
32185 61 6e 5b 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69  an[84]..*/.stati
32186 63 20 52 74 72 65 65 43 65 6c 6c 20 2a 4c 69 6e  c RtreeCell *Lin
32187 65 61 72 50 69 63 6b 4e 65 78 74 28 0a 20 20 52  earPickNext(.  R
32188 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20 20  tree *pRtree,.  
32189 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
3218a 2c 20 0a 20 20 69 6e 74 20 6e 43 65 6c 6c 2c 20  , .  int nCell, 
3218b 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4c  .  RtreeCell *pL
3218c 65 66 74 42 6f 78 2c 20 0a 20 20 52 74 72 65 65  eftBox, .  Rtree
3218d 43 65 6c 6c 20 2a 70 52 69 67 68 74 42 6f 78 2c  Cell *pRightBox,
3218e 0a 20 20 69 6e 74 20 2a 61 69 55 73 65 64 0a 29  .  int *aiUsed.)
3218f 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f  {.  int ii;.  fo
32190 72 28 69 69 3d 30 3b 20 61 69 55 73 65 64 5b 69  r(ii=0; aiUsed[i
32191 69 5d 3b 20 69 69 2b 2b 29 3b 0a 20 20 61 69 55  i]; ii++);.  aiU
32192 73 65 64 5b 69 69 5d 20 3d 20 31 3b 0a 20 20 72  sed[ii] = 1;.  r
32193 65 74 75 72 6e 20 26 61 43 65 6c 6c 5b 69 69 5d  eturn &aCell[ii]
32194 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  ;.}../*.** Imple
32195 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
32196 20 6c 69 6e 65 61 72 20 76 61 72 69 61 6e 74 20   linear variant 
32197 6f 66 20 74 68 65 20 50 69 63 6b 53 65 65 64 73  of the PickSeeds
32198 28 29 20 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d  () function from
32199 0a 2a 2a 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e  .** Guttman[84].
3219a 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
3219b 4c 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 28  LinearPickSeeds(
3219c 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
3219d 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
3219e 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
3219f 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65  ll, .  int *piLe
321a0 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a  ftSeed, .  int *
321a1 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b 0a 20  piRightSeed.){. 
321a2 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 69 4c   int i;.  int iL
321a3 65 66 74 53 65 65 64 20 3d 20 30 3b 0a 20 20 69  eftSeed = 0;.  i
321a4 6e 74 20 69 52 69 67 68 74 53 65 65 64 20 3d 20  nt iRightSeed = 
321a5 31 3b 0a 20 20 66 6c 6f 61 74 20 6d 61 78 4e 6f  1;.  float maxNo
321a6 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68 20 3d  rmalInnerWidth =
321a7 20 30 2e 30 3b 0a 0a 20 20 2f 2a 20 50 69 63 6b   0.0;..  /* Pick
321a8 20 74 77 6f 20 22 73 65 65 64 22 20 63 65 6c 6c   two "seed" cell
321a9 73 20 66 72 6f 6d 20 74 68 65 20 61 72 72 61 79  s from the array
321aa 20 6f 66 20 63 65 6c 6c 73 2e 20 54 68 65 20 61   of cells. The a
321ab 6c 67 6f 72 69 74 68 6d 20 75 73 65 64 0a 20 20  lgorithm used.  
321ac 2a 2a 20 68 65 72 65 20 69 73 20 74 68 65 20 4c  ** here is the L
321ad 69 6e 65 61 72 50 69 63 6b 53 65 65 64 73 20 61  inearPickSeeds a
321ae 6c 67 6f 72 69 74 68 6d 20 66 72 6f 6d 20 47 75  lgorithm from Gu
321af 74 6d 61 6e 5b 31 39 38 34 5d 2e 20 54 68 65 20  tman[1984]. The 
321b0 0a 20 20 2a 2a 20 69 6e 64 69 63 65 73 20 6f 66  .  ** indices of
321b1 20 74 68 65 20 74 77 6f 20 73 65 65 64 20 63 65   the two seed ce
321b2 6c 6c 73 20 69 6e 20 74 68 65 20 61 72 72 61 79  lls in the array
321b3 20 61 72 65 20 73 74 6f 72 65 64 20 69 6e 20 6c   are stored in l
321b4 6f 63 61 6c 0a 20 20 2a 2a 20 76 61 72 69 61 62  ocal.  ** variab
321b5 6c 65 73 20 69 4c 65 66 74 53 65 65 6b 20 61 6e  les iLeftSeek an
321b6 64 20 69 52 69 67 68 74 53 65 65 64 2e 0a 20 20  d iRightSeed..  
321b7 2a 2f 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  */.  for(i=0; i<
321b8 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 2b  pRtree->nDim; i+
321b9 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20 78 31  +){.    float x1
321ba 20 3d 20 61 43 65 6c 6c 5b 30 5d 2e 61 43 6f 6f   = aCell[0].aCoo
321bb 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20 66 6c 6f  rd[i*2];.    flo
321bc 61 74 20 78 32 20 3d 20 61 43 65 6c 6c 5b 30 5d  at x2 = aCell[0]
321bd 2e 61 43 6f 6f 72 64 5b 69 2a 32 2b 31 5d 3b 0a  .aCoord[i*2+1];.
321be 20 20 20 20 66 6c 6f 61 74 20 78 33 20 3d 20 78      float x3 = x
321bf 31 3b 0a 20 20 20 20 66 6c 6f 61 74 20 78 34 20  1;.    float x4 
321c0 3d 20 78 32 3b 0a 20 20 20 20 69 6e 74 20 6a 6a  = x2;.    int jj
321c1 3b 0a 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c  ;..    int iCell
321c2 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69 6e  Left = 0;.    in
321c3 74 20 69 43 65 6c 6c 52 69 67 68 74 20 3d 20 30  t iCellRight = 0
321c4 3b 0a 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 31 3b  ;..    for(jj=1;
321c5 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b 29   jj<nCell; jj++)
321c6 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 6c 65  {.      float le
321c7 66 74 20 3d 20 61 43 65 6c 6c 5b 6a 6a 5d 2e 61  ft = aCell[jj].a
321c8 43 6f 6f 72 64 5b 69 2a 32 5d 3b 0a 20 20 20 20  Coord[i*2];.    
321c9 20 20 66 6c 6f 61 74 20 72 69 67 68 74 20 3d 20    float right = 
321ca 61 43 65 6c 6c 5b 6a 6a 5d 2e 61 43 6f 6f 72 64  aCell[jj].aCoord
321cb 5b 69 2a 32 2b 31 5d 3b 0a 0a 20 20 20 20 20 20  [i*2+1];..      
321cc 69 66 28 20 6c 65 66 74 3c 78 31 20 29 20 78 31  if( left<x1 ) x1
321cd 20 3d 20 6c 65 66 74 3b 0a 20 20 20 20 20 20 69   = left;.      i
321ce 66 28 20 72 69 67 68 74 3e 78 34 20 29 20 78 34  f( right>x4 ) x4
321cf 20 3d 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20   = right;.      
321d0 69 66 28 20 6c 65 66 74 3e 78 33 20 29 7b 0a 20  if( left>x3 ){. 
321d1 20 20 20 20 20 20 20 78 33 20 3d 20 6c 65 66 74         x3 = left
321d2 3b 0a 20 20 20 20 20 20 20 20 69 43 65 6c 6c 52  ;.        iCellR
321d3 69 67 68 74 20 3d 20 6a 6a 3b 0a 20 20 20 20 20  ight = jj;.     
321d4 20 7d 0a 20 20 20 20 20 20 69 66 28 20 72 69 67   }.      if( rig
321d5 68 74 3c 78 32 20 29 7b 0a 20 20 20 20 20 20 20  ht<x2 ){.       
321d6 20 78 32 20 3d 20 72 69 67 68 74 3b 0a 20 20 20   x2 = right;.   
321d7 20 20 20 20 20 69 43 65 6c 6c 4c 65 66 74 20 3d       iCellLeft =
321d8 20 6a 6a 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20   jj;.      }.   
321d9 20 7d 0a 0a 20 20 20 20 69 66 28 20 78 34 21 3d   }..    if( x4!=
321da 78 31 20 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61  x1 ){.      floa
321db 74 20 6e 6f 72 6d 61 6c 77 69 64 74 68 20 3d 20  t normalwidth = 
321dc 28 78 33 20 2d 20 78 32 29 20 2f 20 28 78 34 20  (x3 - x2) / (x4 
321dd 2d 20 78 31 29 3b 0a 20 20 20 20 20 20 69 66 28  - x1);.      if(
321de 20 6e 6f 72 6d 61 6c 77 69 64 74 68 3e 6d 61 78   normalwidth>max
321df 4e 6f 72 6d 61 6c 49 6e 6e 65 72 57 69 64 74 68  NormalInnerWidth
321e0 20 29 7b 0a 20 20 20 20 20 20 20 20 69 4c 65 66   ){.        iLef
321e1 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 4c 65 66  tSeed = iCellLef
321e2 74 3b 0a 20 20 20 20 20 20 20 20 69 52 69 67 68  t;.        iRigh
321e3 74 53 65 65 64 20 3d 20 69 43 65 6c 6c 52 69 67  tSeed = iCellRig
321e4 68 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ht;.      }.    
321e5 7d 0a 20 20 7d 0a 0a 20 20 2a 70 69 4c 65 66 74  }.  }..  *piLeft
321e6 53 65 65 64 20 3d 20 69 4c 65 66 74 53 65 65 64  Seed = iLeftSeed
321e7 3b 0a 20 20 2a 70 69 52 69 67 68 74 53 65 65 64  ;.  *piRightSeed
321e8 20 3d 20 69 52 69 67 68 74 53 65 65 64 3b 0a 7d   = iRightSeed;.}
321e9 0a 23 65 6e 64 69 66 20 2f 2a 20 56 41 52 49 41  .#endif /* VARIA
321ea 4e 54 5f 47 55 54 54 4d 41 4e 5f 4c 49 4e 45 41  NT_GUTTMAN_LINEA
321eb 52 5f 53 50 4c 49 54 20 2a 2f 0a 0a 23 69 66 20  R_SPLIT */..#if 
321ec 56 41 52 49 41 4e 54 5f 47 55 54 54 4d 41 4e 5f  VARIANT_GUTTMAN_
321ed 51 55 41 44 52 41 54 49 43 5f 53 50 4c 49 54 0a  QUADRATIC_SPLIT.
321ee 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61  /*.** Implementa
321ef 74 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64  tion of the quad
321f0 72 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66  ratic variant of
321f1 20 74 68 65 20 50 69 63 6b 4e 65 78 74 28 29 20   the PickNext() 
321f2 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
321f3 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
321f4 0a 73 74 61 74 69 63 20 52 74 72 65 65 43 65 6c  .static RtreeCel
321f5 6c 20 2a 51 75 61 64 72 61 74 69 63 50 69 63 6b  l *QuadraticPick
321f6 4e 65 78 74 28 0a 20 20 52 74 72 65 65 20 2a 70  Next(.  Rtree *p
321f7 52 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65  Rtree,.  RtreeCe
321f8 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69 6e  ll *aCell, .  in
321f9 74 20 6e 43 65 6c 6c 2c 20 0a 20 20 52 74 72 65  t nCell, .  Rtre
321fa 65 43 65 6c 6c 20 2a 70 4c 65 66 74 42 6f 78 2c  eCell *pLeftBox,
321fb 20 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70   .  RtreeCell *p
321fc 52 69 67 68 74 42 6f 78 2c 0a 20 20 69 6e 74 20  RightBox,.  int 
321fd 2a 61 69 55 73 65 64 0a 29 7b 0a 20 20 23 64 65  *aiUsed.){.  #de
321fe 66 69 6e 65 20 46 41 42 53 28 61 29 20 28 28 61  fine FABS(a) ((a
321ff 29 3c 30 2e 30 3f 2d 31 2e 30 2a 28 61 29 3a 28  )<0.0?-1.0*(a):(
32200 61 29 29 0a 0a 20 20 69 6e 74 20 69 53 65 6c 65  a))..  int iSele
32201 63 74 20 3d 20 2d 31 3b 0a 20 20 66 6c 6f 61 74  ct = -1;.  float
32202 20 66 44 69 66 66 3b 0a 20 20 69 6e 74 20 69 69   fDiff;.  int ii
32203 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ;.  for(ii=0; ii
32204 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
32205 20 20 20 69 66 28 20 61 69 55 73 65 64 5b 69 69     if( aiUsed[ii
32206 5d 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 66 6c  ]==0 ){.      fl
32207 6f 61 74 20 6c 65 66 74 20 3d 20 63 65 6c 6c 47  oat left = cellG
32208 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c  rowth(pRtree, pL
32209 65 66 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69  eftBox, &aCell[i
3220a 69 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74  i]);.      float
3220b 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 47 72 6f   right = cellGro
3220c 77 74 68 28 70 52 74 72 65 65 2c 20 70 4c 65 66  wth(pRtree, pLef
3220d 74 42 6f 78 2c 20 26 61 43 65 6c 6c 5b 69 69 5d  tBox, &aCell[ii]
3220e 29 3b 0a 20 20 20 20 20 20 66 6c 6f 61 74 20 64  );.      float d
3220f 69 66 66 20 3d 20 46 41 42 53 28 72 69 67 68 74  iff = FABS(right
32210 2d 6c 65 66 74 29 3b 0a 20 20 20 20 20 20 69 66  -left);.      if
32211 28 20 69 53 65 6c 65 63 74 3c 30 20 7c 7c 20 64  ( iSelect<0 || d
32212 69 66 66 3e 66 44 69 66 66 20 29 7b 0a 20 20 20  iff>fDiff ){.   
32213 20 20 20 20 20 66 44 69 66 66 20 3d 20 64 69 66       fDiff = dif
32214 66 3b 0a 20 20 20 20 20 20 20 20 69 53 65 6c 65  f;.        iSele
32215 63 74 20 3d 20 69 69 3b 0a 20 20 20 20 20 20 7d  ct = ii;.      }
32216 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 61 69 55  .    }.  }.  aiU
32217 73 65 64 5b 69 53 65 6c 65 63 74 5d 20 3d 20 31  sed[iSelect] = 1
32218 3b 0a 20 20 72 65 74 75 72 6e 20 26 61 43 65 6c  ;.  return &aCel
32219 6c 5b 69 53 65 6c 65 63 74 5d 3b 0a 7d 0a 0a 2f  l[iSelect];.}../
3221a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74  *.** Implementat
3221b 69 6f 6e 20 6f 66 20 74 68 65 20 71 75 61 64 72  ion of the quadr
3221c 61 74 69 63 20 76 61 72 69 61 6e 74 20 6f 66 20  atic variant of 
3221d 74 68 65 20 50 69 63 6b 53 65 65 64 73 28 29 20  the PickSeeds() 
3221e 66 75 6e 63 74 69 6f 6e 20 66 72 6f 6d 0a 2a 2a  function from.**
3221f 20 47 75 74 74 6d 61 6e 5b 38 34 5d 2e 0a 2a 2f   Guttman[84]..*/
32220 0a 73 74 61 74 69 63 20 76 6f 69 64 20 51 75 61  .static void Qua
32221 64 72 61 74 69 63 50 69 63 6b 53 65 65 64 73 28  draticPickSeeds(
32222 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
32223 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 61  ,.  RtreeCell *a
32224 43 65 6c 6c 2c 20 0a 20 20 69 6e 74 20 6e 43 65  Cell, .  int nCe
32225 6c 6c 2c 20 0a 20 20 69 6e 74 20 2a 70 69 4c 65  ll, .  int *piLe
32226 66 74 53 65 65 64 2c 20 0a 20 20 69 6e 74 20 2a  ftSeed, .  int *
32227 70 69 52 69 67 68 74 53 65 65 64 0a 29 7b 0a 20  piRightSeed.){. 
32228 20 69 6e 74 20 69 69 3b 0a 20 20 69 6e 74 20 6a   int ii;.  int j
32229 6a 3b 0a 0a 20 20 69 6e 74 20 69 4c 65 66 74 53  j;..  int iLeftS
3222a 65 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69  eed = 0;.  int i
3222b 52 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a 20  RightSeed = 1;. 
3222c 20 66 6c 6f 61 74 20 66 57 61 73 74 65 20 3d 20   float fWaste = 
3222d 30 2e 30 3b 0a 0a 20 20 66 6f 72 28 69 69 3d 30  0.0;..  for(ii=0
3222e 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b  ; ii<nCell; ii++
3222f 29 7b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 69 69  ){.    for(jj=ii
32230 2b 31 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a  +1; jj<nCell; jj
32231 2b 2b 29 7b 0a 20 20 20 20 20 20 66 6c 6f 61 74  ++){.      float
32232 20 72 69 67 68 74 20 3d 20 63 65 6c 6c 41 72 65   right = cellAre
32233 61 28 70 52 74 72 65 65 2c 20 26 61 43 65 6c 6c  a(pRtree, &aCell
32234 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c 6f  [jj]);.      flo
32235 61 74 20 67 72 6f 77 74 68 20 3d 20 63 65 6c 6c  at growth = cell
32236 47 72 6f 77 74 68 28 70 52 74 72 65 65 2c 20 26  Growth(pRtree, &
32237 61 43 65 6c 6c 5b 69 69 5d 2c 20 26 61 43 65 6c  aCell[ii], &aCel
32238 6c 5b 6a 6a 5d 29 3b 0a 20 20 20 20 20 20 66 6c  l[jj]);.      fl
32239 6f 61 74 20 77 61 73 74 65 20 3d 20 67 72 6f 77  oat waste = grow
3223a 74 68 20 2d 20 72 69 67 68 74 3b 0a 0a 20 20 20  th - right;..   
3223b 20 20 20 69 66 28 20 77 61 73 74 65 3e 66 57 61     if( waste>fWa
3223c 73 74 65 20 29 7b 0a 20 20 20 20 20 20 20 20 69  ste ){.        i
3223d 4c 65 66 74 53 65 65 64 20 3d 20 69 69 3b 0a 20  LeftSeed = ii;. 
3223e 20 20 20 20 20 20 20 69 52 69 67 68 74 53 65 65         iRightSee
3223f 64 20 3d 20 6a 6a 3b 0a 20 20 20 20 20 20 20 20  d = jj;.        
32240 66 57 61 73 74 65 20 3d 20 77 61 73 74 65 3b 0a  fWaste = waste;.
32241 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
32242 7d 0a 0a 20 20 2a 70 69 4c 65 66 74 53 65 65 64  }..  *piLeftSeed
32243 20 3d 20 69 4c 65 66 74 53 65 65 64 3b 0a 20 20   = iLeftSeed;.  
32244 2a 70 69 52 69 67 68 74 53 65 65 64 20 3d 20 69  *piRightSeed = i
32245 52 69 67 68 74 53 65 65 64 3b 0a 7d 0a 23 65 6e  RightSeed;.}.#en
32246 64 69 66 20 2f 2a 20 56 41 52 49 41 4e 54 5f 47  dif /* VARIANT_G
32247 55 54 54 4d 41 4e 5f 51 55 41 44 52 41 54 49 43  UTTMAN_QUADRATIC
32248 5f 53 50 4c 49 54 20 2a 2f 0a 0a 2f 2a 0a 2a 2a  _SPLIT */../*.**
32249 20 41 72 67 75 6d 65 6e 74 73 20 61 49 64 78 2c   Arguments aIdx,
3224a 20 61 44 69 73 74 61 6e 63 65 20 61 6e 64 20 61   aDistance and a
3224b 53 70 61 72 65 20 61 6c 6c 20 70 6f 69 6e 74 20  Spare all point 
3224c 74 6f 20 61 72 72 61 79 73 20 6f 66 20 73 69 7a  to arrays of siz
3224d 65 0a 2a 2a 20 6e 49 64 78 2e 20 54 68 65 20 61  e.** nIdx. The a
3224e 49 64 78 20 61 72 72 61 79 20 63 6f 6e 74 61 69  Idx array contai
3224f 6e 73 20 74 68 65 20 73 65 74 20 6f 66 20 69 6e  ns the set of in
32250 74 65 67 65 72 73 20 66 72 6f 6d 20 30 20 74 6f  tegers from 0 to
32251 20 0a 2a 2a 20 28 6e 49 64 78 2d 31 29 20 69 6e   .** (nIdx-1) in
32252 20 6e 6f 20 70 61 72 74 69 63 75 6c 61 72 20 6f   no particular o
32253 72 64 65 72 2e 20 54 68 69 73 20 66 75 6e 63 74  rder. This funct
32254 69 6f 6e 20 73 6f 72 74 73 20 74 68 65 20 76 61  ion sorts the va
32255 6c 75 65 73 0a 2a 2a 20 69 6e 20 61 49 64 78 20  lues.** in aIdx 
32256 61 63 63 6f 72 64 69 6e 67 20 74 6f 20 74 68 65  according to the
32257 20 69 6e 64 65 78 65 64 20 76 61 6c 75 65 73 20   indexed values 
32258 69 6e 20 61 44 69 73 74 61 6e 63 65 2e 20 46 6f  in aDistance. Fo
32259 72 0a 2a 2a 20 65 78 61 6d 70 6c 65 2c 20 61 73  r.** example, as
3225a 73 75 6d 69 6e 67 20 74 68 65 20 69 6e 70 75 74  suming the input
3225b 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 61 49 64 78 20  s:.**.**   aIdx 
3225c 20 20 20 20 20 3d 20 7b 20 30 2c 20 20 20 31 2c       = { 0,   1,
3225d 20 20 20 32 2c 20 20 20 33 20 7d 0a 2a 2a 20 20     2,   3 }.**  
3225e 20 61 44 69 73 74 61 6e 63 65 20 3d 20 7b 20 35   aDistance = { 5
3225f 2e 30 2c 20 32 2e 30 2c 20 37 2e 30 2c 20 36 2e  .0, 2.0, 7.0, 6.
32260 30 20 7d 0a 2a 2a 0a 2a 2a 20 74 68 69 73 20 66  0 }.**.** this f
32261 75 6e 63 74 69 6f 6e 20 73 65 74 73 20 74 68 65  unction sets the
32262 20 61 49 64 78 20 61 72 72 61 79 20 74 6f 20 63   aIdx array to c
32263 6f 6e 74 61 69 6e 3a 0a 2a 2a 0a 2a 2a 20 20 20  ontain:.**.**   
32264 61 49 64 78 20 20 20 20 20 20 3d 20 7b 20 30 2c  aIdx      = { 0,
32265 20 20 20 31 2c 20 20 20 32 2c 20 20 20 33 20 7d     1,   2,   3 }
32266 0a 2a 2a 0a 2a 2a 20 54 68 65 20 61 53 70 61 72  .**.** The aSpar
32267 65 20 61 72 72 61 79 20 69 73 20 75 73 65 64 20  e array is used 
32268 61 73 20 74 65 6d 70 6f 72 61 72 79 20 77 6f 72  as temporary wor
32269 6b 69 6e 67 20 73 70 61 63 65 20 62 79 20 74 68  king space by th
3226a 65 0a 2a 2a 20 73 6f 72 74 69 6e 67 20 61 6c 67  e.** sorting alg
3226b 6f 72 69 74 68 6d 2e 0a 2a 2f 0a 73 74 61 74 69  orithm..*/.stati
3226c 63 20 76 6f 69 64 20 53 6f 72 74 42 79 44 69 73  c void SortByDis
3226d 74 61 6e 63 65 28 0a 20 20 69 6e 74 20 2a 61 49  tance(.  int *aI
3226e 64 78 2c 20 0a 20 20 69 6e 74 20 6e 49 64 78 2c  dx, .  int nIdx,
3226f 20 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69 73 74   .  float *aDist
32270 61 6e 63 65 2c 20 0a 20 20 69 6e 74 20 2a 61 53  ance, .  int *aS
32271 70 61 72 65 0a 29 7b 0a 20 20 69 66 28 20 6e 49  pare.){.  if( nI
32272 64 78 3e 31 20 29 7b 0a 20 20 20 20 69 6e 74 20  dx>1 ){.    int 
32273 69 4c 65 66 74 20 3d 20 30 3b 0a 20 20 20 20 69  iLeft = 0;.    i
32274 6e 74 20 69 52 69 67 68 74 20 3d 20 30 3b 0a 0a  nt iRight = 0;..
32275 20 20 20 20 69 6e 74 20 6e 4c 65 66 74 20 3d 20      int nLeft = 
32276 6e 49 64 78 2f 32 3b 0a 20 20 20 20 69 6e 74 20  nIdx/2;.    int 
32277 6e 52 69 67 68 74 20 3d 20 6e 49 64 78 2d 6e 4c  nRight = nIdx-nL
32278 65 66 74 3b 0a 20 20 20 20 69 6e 74 20 2a 61 4c  eft;.    int *aL
32279 65 66 74 20 3d 20 61 49 64 78 3b 0a 20 20 20 20  eft = aIdx;.    
3227a 69 6e 74 20 2a 61 52 69 67 68 74 20 3d 20 26 61  int *aRight = &a
3227b 49 64 78 5b 6e 4c 65 66 74 5d 3b 0a 0a 20 20 20  Idx[nLeft];..   
3227c 20 53 6f 72 74 42 79 44 69 73 74 61 6e 63 65 28   SortByDistance(
3227d 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 61 44  aLeft, nLeft, aD
3227e 69 73 74 61 6e 63 65 2c 20 61 53 70 61 72 65 29  istance, aSpare)
3227f 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69 73 74  ;.    SortByDist
32280 61 6e 63 65 28 61 52 69 67 68 74 2c 20 6e 52 69  ance(aRight, nRi
32281 67 68 74 2c 20 61 44 69 73 74 61 6e 63 65 2c 20  ght, aDistance, 
32282 61 53 70 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65  aSpare);..    me
32283 6d 63 70 79 28 61 53 70 61 72 65 2c 20 61 4c 65  mcpy(aSpare, aLe
32284 66 74 2c 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a  ft, sizeof(int)*
32285 6e 4c 65 66 74 29 3b 0a 20 20 20 20 61 4c 65 66  nLeft);.    aLef
32286 74 20 3d 20 61 53 70 61 72 65 3b 0a 0a 20 20 20  t = aSpare;..   
32287 20 77 68 69 6c 65 28 20 69 4c 65 66 74 3c 6e 4c   while( iLeft<nL
32288 65 66 74 20 7c 7c 20 69 52 69 67 68 74 3c 6e 52  eft || iRight<nR
32289 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 69 66  ight ){.      if
3228a 28 20 69 4c 65 66 74 3d 3d 6e 4c 65 66 74 20 29  ( iLeft==nLeft )
3228b 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b 69  {.        aIdx[i
3228c 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61  Left+iRight] = a
3228d 52 69 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20  Right[iRight];. 
3228e 20 20 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b         iRight++;
3228f 0a 20 20 20 20 20 20 7d 65 6c 73 65 20 69 66 28  .      }else if(
32290 20 69 52 69 67 68 74 3d 3d 6e 52 69 67 68 74 20   iRight==nRight 
32291 29 7b 0a 20 20 20 20 20 20 20 20 61 49 64 78 5b  ){.        aIdx[
32292 69 4c 65 66 74 2b 69 52 69 67 68 74 5d 20 3d 20  iLeft+iRight] = 
32293 61 4c 65 66 74 5b 69 4c 65 66 74 5d 3b 0a 20 20  aLeft[iLeft];.  
32294 20 20 20 20 20 20 69 4c 65 66 74 2b 2b 3b 0a 20        iLeft++;. 
32295 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20       }else{.    
32296 20 20 20 20 66 6c 6f 61 74 20 66 4c 65 66 74 20      float fLeft 
32297 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 4c 65 66  = aDistance[aLef
32298 74 5b 69 4c 65 66 74 5d 5d 3b 0a 20 20 20 20 20  t[iLeft]];.     
32299 20 20 20 66 6c 6f 61 74 20 66 52 69 67 68 74 20     float fRight 
3229a 3d 20 61 44 69 73 74 61 6e 63 65 5b 61 52 69 67  = aDistance[aRig
3229b 68 74 5b 69 52 69 67 68 74 5d 5d 3b 0a 20 20 20  ht[iRight]];.   
3229c 20 20 20 20 20 69 66 28 20 66 4c 65 66 74 3c 66       if( fLeft<f
3229d 52 69 67 68 74 20 29 7b 0a 20 20 20 20 20 20 20  Right ){.       
3229e 20 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52     aIdx[iLeft+iR
3229f 69 67 68 74 5d 20 3d 20 61 4c 65 66 74 5b 69 4c  ight] = aLeft[iL
322a0 65 66 74 5d 3b 0a 20 20 20 20 20 20 20 20 20 20  eft];.          
322a1 69 4c 65 66 74 2b 2b 3b 0a 20 20 20 20 20 20 20  iLeft++;.       
322a2 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 20 20   }else{.        
322a3 20 20 61 49 64 78 5b 69 4c 65 66 74 2b 69 52 69    aIdx[iLeft+iRi
322a4 67 68 74 5d 20 3d 20 61 52 69 67 68 74 5b 69 52  ght] = aRight[iR
322a5 69 67 68 74 5d 3b 0a 20 20 20 20 20 20 20 20 20  ight];.         
322a6 20 69 52 69 67 68 74 2b 2b 3b 0a 20 20 20 20 20   iRight++;.     
322a7 20 20 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20     }.      }.   
322a8 20 7d 0a 0a 23 69 66 20 30 0a 20 20 20 20 2f 2a   }..#if 0.    /*
322a9 20 43 68 65 63 6b 20 74 68 61 74 20 74 68 65 20   Check that the 
322aa 73 6f 72 74 20 77 6f 72 6b 65 64 20 2a 2f 0a 20  sort worked */. 
322ab 20 20 20 7b 0a 20 20 20 20 20 20 69 6e 74 20 6a     {.      int j
322ac 6a 3b 0a 20 20 20 20 20 20 66 6f 72 28 6a 6a 3d  j;.      for(jj=
322ad 31 3b 20 6a 6a 3c 6e 49 64 78 3b 20 6a 6a 2b 2b  1; jj<nIdx; jj++
322ae 29 7b 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74  ){.        float
322af 20 6c 65 66 74 20 3d 20 61 44 69 73 74 61 6e 63   left = aDistanc
322b0 65 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 3b 0a 20  e[aIdx[jj-1]];. 
322b1 20 20 20 20 20 20 20 66 6c 6f 61 74 20 72 69 67         float rig
322b2 68 74 20 3d 20 61 44 69 73 74 61 6e 63 65 5b 61  ht = aDistance[a
322b3 49 64 78 5b 6a 6a 5d 5d 3b 0a 20 20 20 20 20 20  Idx[jj]];.      
322b4 20 20 61 73 73 65 72 74 28 20 6c 65 66 74 3c 3d    assert( left<=
322b5 72 69 67 68 74 20 29 3b 0a 20 20 20 20 20 20 7d  right );.      }
322b6 0a 20 20 20 20 7d 0a 23 65 6e 64 69 66 0a 20 20  .    }.#endif.  
322b7 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 41 72 67 75 6d  }.}../*.** Argum
322b8 65 6e 74 73 20 61 49 64 78 2c 20 61 43 65 6c 6c  ents aIdx, aCell
322b9 20 61 6e 64 20 61 53 70 61 72 65 20 61 6c 6c 20   and aSpare all 
322ba 70 6f 69 6e 74 20 74 6f 20 61 72 72 61 79 73 20  point to arrays 
322bb 6f 66 20 73 69 7a 65 0a 2a 2a 20 6e 49 64 78 2e  of size.** nIdx.
322bc 20 54 68 65 20 61 49 64 78 20 61 72 72 61 79 20   The aIdx array 
322bd 63 6f 6e 74 61 69 6e 73 20 74 68 65 20 73 65 74  contains the set
322be 20 6f 66 20 69 6e 74 65 67 65 72 73 20 66 72 6f   of integers fro
322bf 6d 20 30 20 74 6f 20 0a 2a 2a 20 28 6e 49 64 78  m 0 to .** (nIdx
322c0 2d 31 29 20 69 6e 20 6e 6f 20 70 61 72 74 69 63  -1) in no partic
322c1 75 6c 61 72 20 6f 72 64 65 72 2e 20 54 68 69 73  ular order. This
322c2 20 66 75 6e 63 74 69 6f 6e 20 73 6f 72 74 73 20   function sorts 
322c3 74 68 65 20 76 61 6c 75 65 73 0a 2a 2a 20 69 6e  the values.** in
322c4 20 61 49 64 78 20 61 63 63 6f 72 64 69 6e 67 20   aIdx according 
322c5 74 6f 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69  to dimension iDi
322c6 6d 20 6f 66 20 74 68 65 20 63 65 6c 6c 73 20 69  m of the cells i
322c7 6e 20 61 43 65 6c 6c 2e 20 54 68 65 0a 2a 2a 20  n aCell. The.** 
322c8 6d 69 6e 69 6d 75 6d 20 76 61 6c 75 65 20 6f 66  minimum value of
322c9 20 64 69 6d 65 6e 73 69 6f 6e 20 69 44 69 6d 20   dimension iDim 
322ca 69 73 20 63 6f 6e 73 69 64 65 72 65 64 20 66 69  is considered fi
322cb 72 73 74 2c 20 74 68 65 0a 2a 2a 20 6d 61 78 69  rst, the.** maxi
322cc 6d 75 6d 20 75 73 65 64 20 74 6f 20 62 72 65 61  mum used to brea
322cd 6b 20 74 69 65 73 2e 0a 2a 2a 0a 2a 2a 20 54 68  k ties..**.** Th
322ce 65 20 61 53 70 61 72 65 20 61 72 72 61 79 20 69  e aSpare array i
322cf 73 20 75 73 65 64 20 61 73 20 74 65 6d 70 6f 72  s used as tempor
322d0 61 72 79 20 77 6f 72 6b 69 6e 67 20 73 70 61 63  ary working spac
322d1 65 20 62 79 20 74 68 65 0a 2a 2a 20 73 6f 72 74  e by the.** sort
322d2 69 6e 67 20 61 6c 67 6f 72 69 74 68 6d 2e 0a 2a  ing algorithm..*
322d3 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20 53 6f  /.static void So
322d4 72 74 42 79 44 69 6d 65 6e 73 69 6f 6e 28 0a 20  rtByDimension(. 
322d5 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a   Rtree *pRtree,.
322d6 20 20 69 6e 74 20 2a 61 49 64 78 2c 20 0a 20 20    int *aIdx, .  
322d7 69 6e 74 20 6e 49 64 78 2c 20 0a 20 20 69 6e 74  int nIdx, .  int
322d8 20 69 44 69 6d 2c 20 0a 20 20 52 74 72 65 65 43   iDim, .  RtreeC
322d9 65 6c 6c 20 2a 61 43 65 6c 6c 2c 20 0a 20 20 69  ell *aCell, .  i
322da 6e 74 20 2a 61 53 70 61 72 65 0a 29 7b 0a 20 20  nt *aSpare.){.  
322db 69 66 28 20 6e 49 64 78 3e 31 20 29 7b 0a 0a 20  if( nIdx>1 ){.. 
322dc 20 20 20 69 6e 74 20 69 4c 65 66 74 20 3d 20 30     int iLeft = 0
322dd 3b 0a 20 20 20 20 69 6e 74 20 69 52 69 67 68 74  ;.    int iRight
322de 20 3d 20 30 3b 0a 0a 20 20 20 20 69 6e 74 20 6e   = 0;..    int n
322df 4c 65 66 74 20 3d 20 6e 49 64 78 2f 32 3b 0a 20  Left = nIdx/2;. 
322e0 20 20 20 69 6e 74 20 6e 52 69 67 68 74 20 3d 20     int nRight = 
322e1 6e 49 64 78 2d 6e 4c 65 66 74 3b 0a 20 20 20 20  nIdx-nLeft;.    
322e2 69 6e 74 20 2a 61 4c 65 66 74 20 3d 20 61 49 64  int *aLeft = aId
322e3 78 3b 0a 20 20 20 20 69 6e 74 20 2a 61 52 69 67  x;.    int *aRig
322e4 68 74 20 3d 20 26 61 49 64 78 5b 6e 4c 65 66 74  ht = &aIdx[nLeft
322e5 5d 3b 0a 0a 20 20 20 20 53 6f 72 74 42 79 44 69  ];..    SortByDi
322e6 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
322e7 61 4c 65 66 74 2c 20 6e 4c 65 66 74 2c 20 69 44  aLeft, nLeft, iD
322e8 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70 61 72  im, aCell, aSpar
322e9 65 29 3b 0a 20 20 20 20 53 6f 72 74 42 79 44 69  e);.    SortByDi
322ea 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65 2c 20  mension(pRtree, 
322eb 61 52 69 67 68 74 2c 20 6e 52 69 67 68 74 2c 20  aRight, nRight, 
322ec 69 44 69 6d 2c 20 61 43 65 6c 6c 2c 20 61 53 70  iDim, aCell, aSp
322ed 61 72 65 29 3b 0a 0a 20 20 20 20 6d 65 6d 63 70  are);..    memcp
322ee 79 28 61 53 70 61 72 65 2c 20 61 4c 65 66 74 2c  y(aSpare, aLeft,
322ef 20 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 4c 65   sizeof(int)*nLe
322f0 66 74 29 3b 0a 20 20 20 20 61 4c 65 66 74 20 3d  ft);.    aLeft =
322f1 20 61 53 70 61 72 65 3b 0a 20 20 20 20 77 68 69   aSpare;.    whi
322f2 6c 65 28 20 69 4c 65 66 74 3c 6e 4c 65 66 74 20  le( iLeft<nLeft 
322f3 7c 7c 20 69 52 69 67 68 74 3c 6e 52 69 67 68 74  || iRight<nRight
322f4 20 29 7b 0a 20 20 20 20 20 20 64 6f 75 62 6c 65   ){.      double
322f5 20 78 6c 65 66 74 31 20 3d 20 44 43 4f 4f 52 44   xleft1 = DCOORD
322f6 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69 4c 65  (aCell[aLeft[iLe
322f7 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d  ft]].aCoord[iDim
322f8 2a 32 5d 29 3b 0a 20 20 20 20 20 20 64 6f 75 62  *2]);.      doub
322f9 6c 65 20 78 6c 65 66 74 32 20 3d 20 44 43 4f 4f  le xleft2 = DCOO
322fa 52 44 28 61 43 65 6c 6c 5b 61 4c 65 66 74 5b 69  RD(aCell[aLeft[i
322fb 4c 65 66 74 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44  Left]].aCoord[iD
322fc 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20 20 20 20 20  im*2+1]);.      
322fd 64 6f 75 62 6c 65 20 78 72 69 67 68 74 31 20 3d  double xright1 =
322fe 20 44 43 4f 4f 52 44 28 61 43 65 6c 6c 5b 61 52   DCOORD(aCell[aR
322ff 69 67 68 74 5b 69 52 69 67 68 74 5d 5d 2e 61 43  ight[iRight]].aC
32300 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20  oord[iDim*2]);. 
32301 20 20 20 20 20 64 6f 75 62 6c 65 20 78 72 69 67       double xrig
32302 68 74 32 20 3d 20 44 43 4f 4f 52 44 28 61 43 65  ht2 = DCOORD(aCe
32303 6c 6c 5b 61 52 69 67 68 74 5b 69 52 69 67 68 74  ll[aRight[iRight
32304 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32  ]].aCoord[iDim*2
32305 2b 31 5d 29 3b 0a 20 20 20 20 20 20 69 66 28 20  +1]);.      if( 
32306 28 69 4c 65 66 74 21 3d 6e 4c 65 66 74 29 20 26  (iLeft!=nLeft) &
32307 26 20 28 28 69 52 69 67 68 74 3d 3d 6e 52 69 67  & ((iRight==nRig
32308 68 74 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 78  ht).       || (x
32309 6c 65 66 74 31 3c 78 72 69 67 68 74 31 29 0a 20  left1<xright1). 
3230a 20 20 20 20 20 20 7c 7c 20 28 78 6c 65 66 74 31        || (xleft1
3230b 3d 3d 78 72 69 67 68 74 31 20 26 26 20 78 6c 65  ==xright1 && xle
3230c 66 74 32 3c 78 72 69 67 68 74 32 29 0a 20 20 20  ft2<xright2).   
3230d 20 20 20 29 29 7b 0a 20 20 20 20 20 20 20 20 61     )){.        a
3230e 49 64 78 5b 69 4c 65 66 74 2b 69 52 69 67 68 74  Idx[iLeft+iRight
3230f 5d 20 3d 20 61 4c 65 66 74 5b 69 4c 65 66 74 5d  ] = aLeft[iLeft]
32310 3b 0a 20 20 20 20 20 20 20 20 69 4c 65 66 74 2b  ;.        iLeft+
32311 2b 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a  +;.      }else{.
32312 20 20 20 20 20 20 20 20 61 49 64 78 5b 69 4c 65          aIdx[iLe
32313 66 74 2b 69 52 69 67 68 74 5d 20 3d 20 61 52 69  ft+iRight] = aRi
32314 67 68 74 5b 69 52 69 67 68 74 5d 3b 0a 20 20 20  ght[iRight];.   
32315 20 20 20 20 20 69 52 69 67 68 74 2b 2b 3b 0a 20       iRight++;. 
32316 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 23 69       }.    }..#i
32317 66 20 30 0a 20 20 20 20 2f 2a 20 43 68 65 63 6b  f 0.    /* Check
32318 20 74 68 61 74 20 74 68 65 20 73 6f 72 74 20 77   that the sort w
32319 6f 72 6b 65 64 20 2a 2f 0a 20 20 20 20 7b 0a 20  orked */.    {. 
3231a 20 20 20 20 20 69 6e 74 20 6a 6a 3b 0a 20 20 20       int jj;.   
3231b 20 20 20 66 6f 72 28 6a 6a 3d 31 3b 20 6a 6a 3c     for(jj=1; jj<
3231c 6e 49 64 78 3b 20 6a 6a 2b 2b 29 7b 0a 20 20 20  nIdx; jj++){.   
3231d 20 20 20 20 20 66 6c 6f 61 74 20 78 6c 65 66 74       float xleft
3231e 31 20 3d 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a  1 = aCell[aIdx[j
3231f 6a 2d 31 5d 5d 2e 61 43 6f 6f 72 64 5b 69 44 69  j-1]].aCoord[iDi
32320 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20 20 66 6c  m*2];.        fl
32321 6f 61 74 20 78 6c 65 66 74 32 20 3d 20 61 43 65  oat xleft2 = aCe
32322 6c 6c 5b 61 49 64 78 5b 6a 6a 2d 31 5d 5d 2e 61  ll[aIdx[jj-1]].a
32323 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 3b  Coord[iDim*2+1];
32324 0a 20 20 20 20 20 20 20 20 66 6c 6f 61 74 20 78  .        float x
32325 72 69 67 68 74 31 20 3d 20 61 43 65 6c 6c 5b 61  right1 = aCell[a
32326 49 64 78 5b 6a 6a 5d 5d 2e 61 43 6f 6f 72 64 5b  Idx[jj]].aCoord[
32327 69 44 69 6d 2a 32 5d 3b 0a 20 20 20 20 20 20 20  iDim*2];.       
32328 20 66 6c 6f 61 74 20 78 72 69 67 68 74 32 20 3d   float xright2 =
32329 20 61 43 65 6c 6c 5b 61 49 64 78 5b 6a 6a 5d 5d   aCell[aIdx[jj]]
3232a 2e 61 43 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31  .aCoord[iDim*2+1
3232b 5d 3b 0a 20 20 20 20 20 20 20 20 61 73 73 65 72  ];.        asser
3232c 74 28 20 78 6c 65 66 74 31 3c 3d 78 72 69 67 68  t( xleft1<=xrigh
3232d 74 31 20 26 26 20 28 78 6c 65 66 74 31 3c 78 72  t1 && (xleft1<xr
3232e 69 67 68 74 31 20 7c 7c 20 78 6c 65 66 74 32 3c  ight1 || xleft2<
3232f 3d 78 72 69 67 68 74 32 29 20 29 3b 0a 20 20 20  =xright2) );.   
32330 20 20 20 7d 0a 20 20 20 20 7d 0a 23 65 6e 64 69     }.    }.#endi
32331 66 0a 20 20 7d 0a 7d 0a 0a 23 69 66 20 56 41 52  f.  }.}..#if VAR
32332 49 41 4e 54 5f 52 53 54 41 52 54 52 45 45 5f 53  IANT_RSTARTREE_S
32333 50 4c 49 54 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65  PLIT./*.** Imple
32334 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
32335 20 52 2a 2d 74 72 65 65 20 76 61 72 69 61 6e 74   R*-tree variant
32336 20 6f 66 20 53 70 6c 69 74 4e 6f 64 65 20 66 72   of SplitNode fr
32337 6f 6d 20 42 65 63 6b 6d 61 6e 5b 31 39 39 30 5d  om Beckman[1990]
32338 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
32339 73 70 6c 69 74 4e 6f 64 65 53 74 61 72 74 72 65  splitNodeStartre
3233a 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  e(.  Rtree *pRtr
3233b 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20  ee,.  RtreeCell 
3233c 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 6e 43  *aCell,.  int nC
3233d 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f 64 65  ell,.  RtreeNode
3233e 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72 65 65   *pLeft,.  Rtree
3233f 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a 20 20  Node *pRight,.  
32340 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62 6f 78  RtreeCell *pBbox
32341 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43 65 6c  Left,.  RtreeCel
32342 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a 29 7b  l *pBboxRight.){
32343 0a 20 20 69 6e 74 20 2a 2a 61 61 53 6f 72 74 65  .  int **aaSorte
32344 64 3b 0a 20 20 69 6e 74 20 2a 61 53 70 61 72 65  d;.  int *aSpare
32345 3b 0a 20 20 69 6e 74 20 69 69 3b 0a 0a 20 20 69  ;.  int ii;..  i
32346 6e 74 20 69 42 65 73 74 44 69 6d 3b 0a 20 20 69  nt iBestDim;.  i
32347 6e 74 20 69 42 65 73 74 53 70 6c 69 74 3b 0a 20  nt iBestSplit;. 
32348 20 66 6c 6f 61 74 20 66 42 65 73 74 4d 61 72 67   float fBestMarg
32349 69 6e 3b 0a 0a 20 20 69 6e 74 20 6e 42 79 74 65  in;..  int nByte
3234a 20 3d 20 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d   = (pRtree->nDim
3234b 2b 31 29 2a 28 73 69 7a 65 6f 66 28 69 6e 74 2a  +1)*(sizeof(int*
3234c 29 2b 6e 43 65 6c 6c 2a 73 69 7a 65 6f 66 28 69  )+nCell*sizeof(i
3234d 6e 74 29 29 3b 0a 0a 20 20 61 61 53 6f 72 74 65  nt));..  aaSorte
3234e 64 20 3d 20 28 69 6e 74 20 2a 2a 29 73 71 6c 69  d = (int **)sqli
3234f 74 65 33 5f 6d 61 6c 6c 6f 63 28 6e 42 79 74 65  te3_malloc(nByte
32350 29 3b 0a 20 20 69 66 28 20 21 61 61 53 6f 72 74  );.  if( !aaSort
32351 65 64 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e  ed ){.    return
32352 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20   SQLITE_NOMEM;. 
32353 20 7d 0a 0a 20 20 61 53 70 61 72 65 20 3d 20 26   }..  aSpare = &
32354 28 28 69 6e 74 20 2a 29 26 61 61 53 6f 72 74 65  ((int *)&aaSorte
32355 64 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 5d 29  d[pRtree->nDim])
32356 5b 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 6e 43  [pRtree->nDim*nC
32357 65 6c 6c 5d 3b 0a 20 20 6d 65 6d 73 65 74 28 61  ell];.  memset(a
32358 61 53 6f 72 74 65 64 2c 20 30 2c 20 6e 42 79 74  aSorted, 0, nByt
32359 65 29 3b 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20  e);.  for(ii=0; 
3235a 69 69 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b  ii<pRtree->nDim;
3235b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 69 6e 74 20   ii++){.    int 
3235c 6a 6a 3b 0a 20 20 20 20 61 61 53 6f 72 74 65 64  jj;.    aaSorted
3235d 5b 69 69 5d 20 3d 20 26 28 28 69 6e 74 20 2a 29  [ii] = &((int *)
3235e 26 61 61 53 6f 72 74 65 64 5b 70 52 74 72 65 65  &aaSorted[pRtree
3235f 2d 3e 6e 44 69 6d 5d 29 5b 69 69 2a 6e 43 65 6c  ->nDim])[ii*nCel
32360 6c 5d 3b 0a 20 20 20 20 66 6f 72 28 6a 6a 3d 30  l];.    for(jj=0
32361 3b 20 6a 6a 3c 6e 43 65 6c 6c 3b 20 6a 6a 2b 2b  ; jj<nCell; jj++
32362 29 7b 0a 20 20 20 20 20 20 61 61 53 6f 72 74 65  ){.      aaSorte
32363 64 5b 69 69 5d 5b 6a 6a 5d 20 3d 20 6a 6a 3b 0a  d[ii][jj] = jj;.
32364 20 20 20 20 7d 0a 20 20 20 20 53 6f 72 74 42 79      }.    SortBy
32365 44 69 6d 65 6e 73 69 6f 6e 28 70 52 74 72 65 65  Dimension(pRtree
32366 2c 20 61 61 53 6f 72 74 65 64 5b 69 69 5d 2c 20  , aaSorted[ii], 
32367 6e 43 65 6c 6c 2c 20 69 69 2c 20 61 43 65 6c 6c  nCell, ii, aCell
32368 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 7d 0a 0a  , aSpare);.  }..
32369 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69 3c 70    for(ii=0; ii<p
3236a 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20 69 69 2b  Rtree->nDim; ii+
3236b 2b 29 7b 0a 20 20 20 20 66 6c 6f 61 74 20 6d 61  +){.    float ma
3236c 72 67 69 6e 20 3d 20 30 2e 30 3b 0a 20 20 20 20  rgin = 0.0;.    
3236d 66 6c 6f 61 74 20 66 42 65 73 74 4f 76 65 72 6c  float fBestOverl
3236e 61 70 3b 0a 20 20 20 20 66 6c 6f 61 74 20 66 42  ap;.    float fB
3236f 65 73 74 41 72 65 61 3b 0a 20 20 20 20 69 6e 74  estArea;.    int
32370 20 69 42 65 73 74 4c 65 66 74 3b 0a 20 20 20 20   iBestLeft;.    
32371 69 6e 74 20 6e 4c 65 66 74 3b 0a 0a 20 20 20 20  int nLeft;..    
32372 66 6f 72 28 0a 20 20 20 20 20 20 6e 4c 65 66 74  for(.      nLeft
32373 3d 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28  =RTREE_MINCELLS(
32374 70 52 74 72 65 65 29 3b 20 0a 20 20 20 20 20 20  pRtree); .      
32375 6e 4c 65 66 74 3c 3d 28 6e 43 65 6c 6c 2d 52 54  nLeft<=(nCell-RT
32376 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70 52 74  REE_MINCELLS(pRt
32377 72 65 65 29 29 3b 20 0a 20 20 20 20 20 20 6e 4c  ree)); .      nL
32378 65 66 74 2b 2b 0a 20 20 20 20 29 7b 0a 20 20 20  eft++.    ){.   
32379 20 20 20 52 74 72 65 65 43 65 6c 6c 20 6c 65 66     RtreeCell lef
3237a 74 3b 0a 20 20 20 20 20 20 52 74 72 65 65 43 65  t;.      RtreeCe
3237b 6c 6c 20 72 69 67 68 74 3b 0a 20 20 20 20 20 20  ll right;.      
3237c 69 6e 74 20 6b 6b 3b 0a 20 20 20 20 20 20 66 6c  int kk;.      fl
3237d 6f 61 74 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20  oat overlap;.   
3237e 20 20 20 66 6c 6f 61 74 20 61 72 65 61 3b 0a 0a     float area;..
3237f 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 6c 65        memcpy(&le
32380 66 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  ft, &aCell[aaSor
32381 74 65 64 5b 69 69 5d 5b 30 5d 5d 2c 20 73 69 7a  ted[ii][0]], siz
32382 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29 29 3b  eof(RtreeCell));
32383 0a 20 20 20 20 20 20 6d 65 6d 63 70 79 28 26 72  .      memcpy(&r
32384 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61 53  ight, &aCell[aaS
32385 6f 72 74 65 64 5b 69 69 5d 5b 6e 43 65 6c 6c 2d  orted[ii][nCell-
32386 31 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74 72 65  1]], sizeof(Rtre
32387 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20 20 20 66  eCell));.      f
32388 6f 72 28 6b 6b 3d 31 3b 20 6b 6b 3c 28 6e 43 65  or(kk=1; kk<(nCe
32389 6c 6c 2d 31 29 3b 20 6b 6b 2b 2b 29 7b 0a 20 20  ll-1); kk++){.  
3238a 20 20 20 20 20 20 69 66 28 20 6b 6b 3c 6e 4c 65        if( kk<nLe
3238b 66 74 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  ft ){.          
3238c 63 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65  cellUnion(pRtree
3238d 2c 20 26 6c 65 66 74 2c 20 26 61 43 65 6c 6c 5b  , &left, &aCell[
3238e 61 61 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d  aaSorted[ii][kk]
3238f 5d 29 3b 0a 20 20 20 20 20 20 20 20 7d 65 6c 73  ]);.        }els
32390 65 7b 0a 20 20 20 20 20 20 20 20 20 20 63 65 6c  e{.          cel
32391 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 26  lUnion(pRtree, &
32392 72 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 61 61  right, &aCell[aa
32393 53 6f 72 74 65 64 5b 69 69 5d 5b 6b 6b 5d 5d 29  Sorted[ii][kk]])
32394 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
32395 20 20 7d 0a 20 20 20 20 20 20 6d 61 72 67 69 6e    }.      margin
32396 20 2b 3d 20 63 65 6c 6c 4d 61 72 67 69 6e 28 70   += cellMargin(p
32397 52 74 72 65 65 2c 20 26 6c 65 66 74 29 3b 0a 20  Rtree, &left);. 
32398 20 20 20 20 20 6d 61 72 67 69 6e 20 2b 3d 20 63       margin += c
32399 65 6c 6c 4d 61 72 67 69 6e 28 70 52 74 72 65 65  ellMargin(pRtree
3239a 2c 20 26 72 69 67 68 74 29 3b 0a 20 20 20 20 20  , &right);.     
3239b 20 6f 76 65 72 6c 61 70 20 3d 20 63 65 6c 6c 4f   overlap = cellO
3239c 76 65 72 6c 61 70 28 70 52 74 72 65 65 2c 20 26  verlap(pRtree, &
3239d 6c 65 66 74 2c 20 26 72 69 67 68 74 2c 20 31 2c  left, &right, 1,
3239e 20 2d 31 29 3b 0a 20 20 20 20 20 20 61 72 65 61   -1);.      area
3239f 20 3d 20 63 65 6c 6c 41 72 65 61 28 70 52 74 72   = cellArea(pRtr
323a0 65 65 2c 20 26 6c 65 66 74 29 20 2b 20 63 65 6c  ee, &left) + cel
323a1 6c 41 72 65 61 28 70 52 74 72 65 65 2c 20 26 72  lArea(pRtree, &r
323a2 69 67 68 74 29 3b 0a 20 20 20 20 20 20 69 66 28  ight);.      if(
323a3 20 28 6e 4c 65 66 74 3d 3d 52 54 52 45 45 5f 4d   (nLeft==RTREE_M
323a4 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 29  INCELLS(pRtree))
323a5 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65 72  .       || (over
323a6 6c 61 70 3c 66 42 65 73 74 4f 76 65 72 6c 61 70  lap<fBestOverlap
323a7 29 0a 20 20 20 20 20 20 20 7c 7c 20 28 6f 76 65  ).       || (ove
323a8 72 6c 61 70 3d 3d 66 42 65 73 74 4f 76 65 72 6c  rlap==fBestOverl
323a9 61 70 20 26 26 20 61 72 65 61 3c 66 42 65 73 74  ap && area<fBest
323aa 41 72 65 61 29 0a 20 20 20 20 20 20 29 7b 0a 20  Area).      ){. 
323ab 20 20 20 20 20 20 20 69 42 65 73 74 4c 65 66 74         iBestLeft
323ac 20 3d 20 6e 4c 65 66 74 3b 0a 20 20 20 20 20 20   = nLeft;.      
323ad 20 20 66 42 65 73 74 4f 76 65 72 6c 61 70 20 3d    fBestOverlap =
323ae 20 6f 76 65 72 6c 61 70 3b 0a 20 20 20 20 20 20   overlap;.      
323af 20 20 66 42 65 73 74 41 72 65 61 20 3d 20 61 72    fBestArea = ar
323b0 65 61 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ea;.      }.    
323b1 7d 0a 0a 20 20 20 20 69 66 28 20 69 69 3d 3d 30  }..    if( ii==0
323b2 20 7c 7c 20 6d 61 72 67 69 6e 3c 66 42 65 73 74   || margin<fBest
323b3 4d 61 72 67 69 6e 20 29 7b 0a 20 20 20 20 20 20  Margin ){.      
323b4 69 42 65 73 74 44 69 6d 20 3d 20 69 69 3b 0a 20  iBestDim = ii;. 
323b5 20 20 20 20 20 66 42 65 73 74 4d 61 72 67 69 6e       fBestMargin
323b6 20 3d 20 6d 61 72 67 69 6e 3b 0a 20 20 20 20 20   = margin;.     
323b7 20 69 42 65 73 74 53 70 6c 69 74 20 3d 20 69 42   iBestSplit = iB
323b8 65 73 74 4c 65 66 74 3b 0a 20 20 20 20 7d 0a 20  estLeft;.    }. 
323b9 20 7d 0a 0a 20 20 6d 65 6d 63 70 79 28 70 42 62   }..  memcpy(pBb
323ba 6f 78 4c 65 66 74 2c 20 26 61 43 65 6c 6c 5b 61  oxLeft, &aCell[a
323bb 61 53 6f 72 74 65 64 5b 69 42 65 73 74 44 69 6d  aSorted[iBestDim
323bc 5d 5b 30 5d 5d 2c 20 73 69 7a 65 6f 66 28 52 74  ][0]], sizeof(Rt
323bd 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6d 65 6d  reeCell));.  mem
323be 63 70 79 28 70 42 62 6f 78 52 69 67 68 74 2c 20  cpy(pBboxRight, 
323bf 26 61 43 65 6c 6c 5b 61 61 53 6f 72 74 65 64 5b  &aCell[aaSorted[
323c0 69 42 65 73 74 44 69 6d 5d 5b 69 42 65 73 74 53  iBestDim][iBestS
323c1 70 6c 69 74 5d 5d 2c 20 73 69 7a 65 6f 66 28 52  plit]], sizeof(R
323c2 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 66 6f  treeCell));.  fo
323c3 72 28 69 69 3d 30 3b 20 69 69 3c 6e 43 65 6c 6c  r(ii=0; ii<nCell
323c4 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 52 74 72  ; ii++){.    Rtr
323c5 65 65 4e 6f 64 65 20 2a 70 54 61 72 67 65 74 20  eeNode *pTarget 
323c6 3d 20 28 69 69 3c 69 42 65 73 74 53 70 6c 69 74  = (ii<iBestSplit
323c7 29 3f 70 4c 65 66 74 3a 70 52 69 67 68 74 3b 0a  )?pLeft:pRight;.
323c8 20 20 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70      RtreeCell *p
323c9 42 62 6f 78 20 3d 20 28 69 69 3c 69 42 65 73 74  Bbox = (ii<iBest
323ca 53 70 6c 69 74 29 3f 70 42 62 6f 78 4c 65 66 74  Split)?pBboxLeft
323cb 3a 70 42 62 6f 78 52 69 67 68 74 3b 0a 20 20 20  :pBboxRight;.   
323cc 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 43 65 6c   RtreeCell *pCel
323cd 6c 20 3d 20 26 61 43 65 6c 6c 5b 61 61 53 6f 72  l = &aCell[aaSor
323ce 74 65 64 5b 69 42 65 73 74 44 69 6d 5d 5b 69 69  ted[iBestDim][ii
323cf 5d 5d 3b 0a 20 20 20 20 6e 6f 64 65 49 6e 73 65  ]];.    nodeInse
323d0 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
323d1 54 61 72 67 65 74 2c 20 70 43 65 6c 6c 29 3b 0a  Target, pCell);.
323d2 20 20 20 20 63 65 6c 6c 55 6e 69 6f 6e 28 70 52      cellUnion(pR
323d3 74 72 65 65 2c 20 70 42 62 6f 78 2c 20 70 43 65  tree, pBbox, pCe
323d4 6c 6c 29 3b 0a 20 20 7d 0a 0a 20 20 73 71 6c 69  ll);.  }..  sqli
323d5 74 65 33 5f 66 72 65 65 28 61 61 53 6f 72 74 65  te3_free(aaSorte
323d6 64 29 3b 0a 20 20 72 65 74 75 72 6e 20 53 51 4c  d);.  return SQL
323d7 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e 64 69 66  ITE_OK;.}.#endif
323d8 0a 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 47 55  ..#if VARIANT_GU
323d9 54 54 4d 41 4e 5f 53 50 4c 49 54 0a 2f 2a 0a 2a  TTMAN_SPLIT./*.*
323da 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
323db 20 6f 66 20 74 68 65 20 72 65 67 75 6c 61 72 20   of the regular 
323dc 52 2d 74 72 65 65 20 53 70 6c 69 74 4e 6f 64 65  R-tree SplitNode
323dd 20 66 72 6f 6d 20 47 75 74 74 6d 61 6e 5b 31 39   from Guttman[19
323de 38 34 5d 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69  84]..*/.static i
323df 6e 74 20 73 70 6c 69 74 4e 6f 64 65 47 75 74 74  nt splitNodeGutt
323e0 6d 61 6e 28 0a 20 20 52 74 72 65 65 20 2a 70 52  man(.  Rtree *pR
323e1 74 72 65 65 2c 0a 20 20 52 74 72 65 65 43 65 6c  tree,.  RtreeCel
323e2 6c 20 2a 61 43 65 6c 6c 2c 0a 20 20 69 6e 74 20  l *aCell,.  int 
323e3 6e 43 65 6c 6c 2c 0a 20 20 52 74 72 65 65 4e 6f  nCell,.  RtreeNo
323e4 64 65 20 2a 70 4c 65 66 74 2c 0a 20 20 52 74 72  de *pLeft,.  Rtr
323e5 65 65 4e 6f 64 65 20 2a 70 52 69 67 68 74 2c 0a  eeNode *pRight,.
323e6 20 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 42 62    RtreeCell *pBb
323e7 6f 78 4c 65 66 74 2c 0a 20 20 52 74 72 65 65 43  oxLeft,.  RtreeC
323e8 65 6c 6c 20 2a 70 42 62 6f 78 52 69 67 68 74 0a  ell *pBboxRight.
323e9 29 7b 0a 20 20 69 6e 74 20 69 4c 65 66 74 53 65  ){.  int iLeftSe
323ea 65 64 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 52  ed = 0;.  int iR
323eb 69 67 68 74 53 65 65 64 20 3d 20 31 3b 0a 20 20  ightSeed = 1;.  
323ec 69 6e 74 20 2a 61 69 55 73 65 64 3b 0a 20 20 69  int *aiUsed;.  i
323ed 6e 74 20 69 3b 0a 0a 20 20 61 69 55 73 65 64 20  nt i;..  aiUsed 
323ee 3d 20 73 71 6c 69 74 65 33 5f 6d 61 6c 6c 6f 63  = sqlite3_malloc
323ef 28 73 69 7a 65 6f 66 28 69 6e 74 29 2a 6e 43 65  (sizeof(int)*nCe
323f0 6c 6c 29 3b 0a 20 20 6d 65 6d 73 65 74 28 61 69  ll);.  memset(ai
323f1 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28  Used, 0, sizeof(
323f2 69 6e 74 29 2a 6e 43 65 6c 6c 29 3b 0a 0a 20 20  int)*nCell);..  
323f3 50 69 63 6b 53 65 65 64 73 28 70 52 74 72 65 65  PickSeeds(pRtree
323f4 2c 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20  , aCell, nCell, 
323f5 26 69 4c 65 66 74 53 65 65 64 2c 20 26 69 52 69  &iLeftSeed, &iRi
323f6 67 68 74 53 65 65 64 29 3b 0a 0a 20 20 6d 65 6d  ghtSeed);..  mem
323f7 63 70 79 28 70 42 62 6f 78 4c 65 66 74 2c 20 26  cpy(pBboxLeft, &
323f8 61 43 65 6c 6c 5b 69 4c 65 66 74 53 65 65 64 5d  aCell[iLeftSeed]
323f9 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 43 65  , sizeof(RtreeCe
323fa 6c 6c 29 29 3b 0a 20 20 6d 65 6d 63 70 79 28 70  ll));.  memcpy(p
323fb 42 62 6f 78 52 69 67 68 74 2c 20 26 61 43 65 6c  BboxRight, &aCel
323fc 6c 5b 69 52 69 67 68 74 53 65 65 64 5d 2c 20 73  l[iRightSeed], s
323fd 69 7a 65 6f 66 28 52 74 72 65 65 43 65 6c 6c 29  izeof(RtreeCell)
323fe 29 3b 0a 20 20 6e 6f 64 65 49 6e 73 65 72 74 43  );.  nodeInsertC
323ff 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66  ell(pRtree, pLef
32400 74 2c 20 26 61 43 65 6c 6c 5b 69 4c 65 66 74 53  t, &aCell[iLeftS
32401 65 65 64 5d 29 3b 0a 20 20 6e 6f 64 65 49 6e 73  eed]);.  nodeIns
32402 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
32403 70 52 69 67 68 74 2c 20 26 61 43 65 6c 6c 5b 69  pRight, &aCell[i
32404 52 69 67 68 74 53 65 65 64 5d 29 3b 0a 20 20 61  RightSeed]);.  a
32405 69 55 73 65 64 5b 69 4c 65 66 74 53 65 65 64 5d  iUsed[iLeftSeed]
32406 20 3d 20 31 3b 0a 20 20 61 69 55 73 65 64 5b 69   = 1;.  aiUsed[i
32407 52 69 67 68 74 53 65 65 64 5d 20 3d 20 31 3b 0a  RightSeed] = 1;.
32408 0a 20 20 66 6f 72 28 69 3d 6e 43 65 6c 6c 2d 32  .  for(i=nCell-2
32409 3b 20 69 3e 30 3b 20 69 2d 2d 29 7b 0a 20 20 20  ; i>0; i--){.   
3240a 20 52 74 72 65 65 43 65 6c 6c 20 2a 70 4e 65 78   RtreeCell *pNex
3240b 74 3b 0a 20 20 20 20 70 4e 65 78 74 20 3d 20 50  t;.    pNext = P
3240c 69 63 6b 4e 65 78 74 28 70 52 74 72 65 65 2c 20  ickNext(pRtree, 
3240d 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70 42  aCell, nCell, pB
3240e 62 6f 78 4c 65 66 74 2c 20 70 42 62 6f 78 52 69  boxLeft, pBboxRi
3240f 67 68 74 2c 20 61 69 55 73 65 64 29 3b 0a 20 20  ght, aiUsed);.  
32410 20 20 66 6c 6f 61 74 20 64 69 66 66 20 3d 20 20    float diff =  
32411 0a 20 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74  .      cellGrowt
32412 68 28 70 52 74 72 65 65 2c 20 70 42 62 6f 78 4c  h(pRtree, pBboxL
32413 65 66 74 2c 20 70 4e 65 78 74 29 20 2d 20 0a 20  eft, pNext) - . 
32414 20 20 20 20 20 63 65 6c 6c 47 72 6f 77 74 68 28       cellGrowth(
32415 70 52 74 72 65 65 2c 20 70 42 62 6f 78 52 69 67  pRtree, pBboxRig
32416 68 74 2c 20 70 4e 65 78 74 29 0a 20 20 20 20 3b  ht, pNext).    ;
32417 0a 20 20 20 20 69 66 28 20 28 52 54 52 45 45 5f  .    if( (RTREE_
32418 4d 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29  MINCELLS(pRtree)
32419 2d 4e 43 45 4c 4c 28 70 52 69 67 68 74 29 3d 3d  -NCELL(pRight)==
3241a 69 29 0a 20 20 20 20 20 7c 7c 20 28 64 69 66 66  i).     || (diff
3241b 3e 30 2e 30 20 26 26 20 28 52 54 52 45 45 5f 4d  >0.0 && (RTREE_M
3241c 49 4e 43 45 4c 4c 53 28 70 52 74 72 65 65 29 2d  INCELLS(pRtree)-
3241d 4e 43 45 4c 4c 28 70 4c 65 66 74 29 21 3d 69 29  NCELL(pLeft)!=i)
3241e 29 0a 20 20 20 20 29 7b 0a 20 20 20 20 20 20 6e  ).    ){.      n
3241f 6f 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  odeInsertCell(pR
32420 74 72 65 65 2c 20 70 52 69 67 68 74 2c 20 70 4e  tree, pRight, pN
32421 65 78 74 29 3b 0a 20 20 20 20 20 20 63 65 6c 6c  ext);.      cell
32422 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c 20 70 42  Union(pRtree, pB
32423 62 6f 78 52 69 67 68 74 2c 20 70 4e 65 78 74 29  boxRight, pNext)
32424 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
32425 20 20 20 6e 6f 64 65 49 6e 73 65 72 74 43 65 6c     nodeInsertCel
32426 6c 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 2c  l(pRtree, pLeft,
32427 20 70 4e 65 78 74 29 3b 0a 20 20 20 20 20 20 63   pNext);.      c
32428 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
32429 20 70 42 62 6f 78 4c 65 66 74 2c 20 70 4e 65 78   pBboxLeft, pNex
3242a 74 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  t);.    }.  }.. 
3242b 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 61 69   sqlite3_free(ai
3242c 55 73 65 64 29 3b 0a 20 20 72 65 74 75 72 6e 20  Used);.  return 
3242d 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 23 65 6e  SQLITE_OK;.}.#en
3242e 64 69 66 0a 0a 73 74 61 74 69 63 20 69 6e 74 20  dif..static int 
3242f 75 70 64 61 74 65 4d 61 70 70 69 6e 67 28 0a 20  updateMapping(. 
32430 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20   Rtree *pRtree, 
32431 0a 20 20 69 36 34 20 69 52 6f 77 69 64 2c 20 0a  .  i64 iRowid, .
32432 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f    RtreeNode *pNo
32433 64 65 2c 20 0a 20 20 69 6e 74 20 69 48 65 69 67  de, .  int iHeig
32434 68 74 0a 29 7b 0a 20 20 69 6e 74 20 28 2a 78 53  ht.){.  int (*xS
32435 65 74 4d 61 70 70 69 6e 67 29 28 52 74 72 65 65  etMapping)(Rtree
32436 20 2a 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36   *, sqlite3_int6
32437 34 2c 20 73 71 6c 69 74 65 33 5f 69 6e 74 36 34  4, sqlite3_int64
32438 29 3b 0a 20 20 78 53 65 74 4d 61 70 70 69 6e 67  );.  xSetMapping
32439 20 3d 20 28 28 69 48 65 69 67 68 74 3d 3d 30 29   = ((iHeight==0)
3243a 3f 72 6f 77 69 64 57 72 69 74 65 3a 70 61 72 65  ?rowidWrite:pare
3243b 6e 74 57 72 69 74 65 29 3b 0a 20 20 69 66 28 20  ntWrite);.  if( 
3243c 69 48 65 69 67 68 74 3e 30 20 29 7b 0a 20 20 20  iHeight>0 ){.   
3243d 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 43 68 69   RtreeNode *pChi
3243e 6c 64 20 3d 20 6e 6f 64 65 48 61 73 68 4c 6f 6f  ld = nodeHashLoo
3243f 6b 75 70 28 70 52 74 72 65 65 2c 20 69 52 6f 77  kup(pRtree, iRow
32440 69 64 29 3b 0a 20 20 20 20 69 66 28 20 70 43 68  id);.    if( pCh
32441 69 6c 64 20 29 7b 0a 20 20 20 20 20 20 6e 6f 64  ild ){.      nod
32442 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c  eRelease(pRtree,
32443 20 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74   pChild->pParent
32444 29 3b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 66  );.      nodeRef
32445 65 72 65 6e 63 65 28 70 4e 6f 64 65 29 3b 0a 20  erence(pNode);. 
32446 20 20 20 20 20 70 43 68 69 6c 64 2d 3e 70 50 61       pChild->pPa
32447 72 65 6e 74 20 3d 20 70 4e 6f 64 65 3b 0a 20 20  rent = pNode;.  
32448 20 20 7d 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e    }.  }.  return
32449 20 78 53 65 74 4d 61 70 70 69 6e 67 28 70 52 74   xSetMapping(pRt
3244a 72 65 65 2c 20 69 52 6f 77 69 64 2c 20 70 4e 6f  ree, iRowid, pNo
3244b 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 7d 0a 0a 73  de->iNode);.}..s
3244c 74 61 74 69 63 20 69 6e 74 20 53 70 6c 69 74 4e  tatic int SplitN
3244d 6f 64 65 28 0a 20 20 52 74 72 65 65 20 2a 70 52  ode(.  Rtree *pR
3244e 74 72 65 65 2c 0a 20 20 52 74 72 65 65 4e 6f 64  tree,.  RtreeNod
3244f 65 20 2a 70 4e 6f 64 65 2c 0a 20 20 52 74 72 65  e *pNode,.  Rtre
32450 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 0a 20 20  eCell *pCell,.  
32451 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a 20  int iHeight.){. 
32452 20 69 6e 74 20 69 3b 0a 20 20 69 6e 74 20 6e 65   int i;.  int ne
32453 77 43 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 30  wCellIsRight = 0
32454 3b 0a 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51  ;..  int rc = SQ
32455 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e  LITE_OK;.  int n
32456 43 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f  Cell = NCELL(pNo
32457 64 65 29 3b 0a 20 20 52 74 72 65 65 43 65 6c 6c  de);.  RtreeCell
32458 20 2a 61 43 65 6c 6c 3b 0a 20 20 69 6e 74 20 2a   *aCell;.  int *
32459 61 69 55 73 65 64 3b 0a 0a 20 20 52 74 72 65 65  aiUsed;..  Rtree
3245a 4e 6f 64 65 20 2a 70 4c 65 66 74 20 3d 20 30 3b  Node *pLeft = 0;
3245b 0a 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 52  .  RtreeNode *pR
3245c 69 67 68 74 20 3d 20 30 3b 0a 0a 20 20 52 74 72  ight = 0;..  Rtr
3245d 65 65 43 65 6c 6c 20 6c 65 66 74 62 62 6f 78 3b  eeCell leftbbox;
3245e 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 72 69 67  .  RtreeCell rig
3245f 68 74 62 62 6f 78 3b 0a 0a 20 20 2f 2a 20 41 6c  htbbox;..  /* Al
32460 6c 6f 63 61 74 65 20 61 6e 20 61 72 72 61 79 20  locate an array 
32461 61 6e 64 20 70 6f 70 75 6c 61 74 65 20 69 74 20  and populate it 
32462 77 69 74 68 20 61 20 63 6f 70 79 20 6f 66 20 70  with a copy of p
32463 43 65 6c 6c 20 61 6e 64 20 0a 20 20 2a 2a 20 61  Cell and .  ** a
32464 6c 6c 20 63 65 6c 6c 73 20 66 72 6f 6d 20 6e 6f  ll cells from no
32465 64 65 20 70 4c 65 66 74 2e 20 54 68 65 6e 20 7a  de pLeft. Then z
32466 65 72 6f 20 74 68 65 20 6f 72 69 67 69 6e 61 6c  ero the original
32467 20 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 61 43   node..  */.  aC
32468 65 6c 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  ell = sqlite3_ma
32469 6c 6c 6f 63 28 28 73 69 7a 65 6f 66 28 52 74 72  lloc((sizeof(Rtr
3246a 65 65 43 65 6c 6c 29 2b 73 69 7a 65 6f 66 28 69  eeCell)+sizeof(i
3246b 6e 74 29 29 2a 28 6e 43 65 6c 6c 2b 31 29 29 3b  nt))*(nCell+1));
3246c 0a 20 20 69 66 28 20 21 61 43 65 6c 6c 20 29 7b  .  if( !aCell ){
3246d 0a 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45  .    rc = SQLITE
3246e 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67 6f 74 6f  _NOMEM;.    goto
3246f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75 74 3b 0a   splitnode_out;.
32470 20 20 7d 0a 20 20 61 69 55 73 65 64 20 3d 20 28    }.  aiUsed = (
32471 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65  int *)&aCell[nCe
32472 6c 6c 2b 31 5d 3b 0a 20 20 6d 65 6d 73 65 74 28  ll+1];.  memset(
32473 61 69 55 73 65 64 2c 20 30 2c 20 73 69 7a 65 6f  aiUsed, 0, sizeo
32474 66 28 69 6e 74 29 2a 28 6e 43 65 6c 6c 2b 31 29  f(int)*(nCell+1)
32475 29 3b 0a 20 20 66 6f 72 28 69 3d 30 3b 20 69 3c  );.  for(i=0; i<
32476 6e 43 65 6c 6c 3b 20 69 2b 2b 29 7b 0a 20 20 20  nCell; i++){.   
32477 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74   nodeGetCell(pRt
32478 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 2c 20 26  ree, pNode, i, &
32479 61 43 65 6c 6c 5b 69 5d 29 3b 0a 20 20 7d 0a 20  aCell[i]);.  }. 
3247a 20 6e 6f 64 65 5a 65 72 6f 28 70 52 74 72 65 65   nodeZero(pRtree
3247b 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 6d 65 6d 63  , pNode);.  memc
3247c 70 79 28 26 61 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  py(&aCell[nCell]
3247d 2c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28  , pCell, sizeof(
3247e 52 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 6e  RtreeCell));.  n
3247f 43 65 6c 6c 2b 2b 3b 0a 0a 20 20 69 66 28 20 70  Cell++;..  if( p
32480 4e 6f 64 65 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29  Node->iNode==1 )
32481 7b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20 6e  {.    pRight = n
32482 6f 64 65 4e 65 77 28 70 52 74 72 65 65 2c 20 70  odeNew(pRtree, p
32483 4e 6f 64 65 2c 20 31 29 3b 0a 20 20 20 20 70 4c  Node, 1);.    pL
32484 65 66 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52  eft = nodeNew(pR
32485 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 31 29 3b  tree, pNode, 1);
32486 0a 20 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65  .    pRtree->iDe
32487 70 74 68 2b 2b 3b 0a 20 20 20 20 70 4e 6f 64 65  pth++;.    pNode
32488 2d 3e 69 73 44 69 72 74 79 20 3d 20 31 3b 0a 20  ->isDirty = 1;. 
32489 20 20 20 77 72 69 74 65 49 6e 74 31 36 28 70 4e     writeInt16(pN
3248a 6f 64 65 2d 3e 7a 44 61 74 61 2c 20 70 52 74 72  ode->zData, pRtr
3248b 65 65 2d 3e 69 44 65 70 74 68 29 3b 0a 20 20 7d  ee->iDepth);.  }
3248c 65 6c 73 65 7b 0a 20 20 20 20 70 4c 65 66 74 20  else{.    pLeft 
3248d 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 70 52 69  = pNode;.    pRi
3248e 67 68 74 20 3d 20 6e 6f 64 65 4e 65 77 28 70 52  ght = nodeNew(pR
3248f 74 72 65 65 2c 20 70 4c 65 66 74 2d 3e 70 50 61  tree, pLeft->pPa
32490 72 65 6e 74 2c 20 31 29 3b 0a 20 20 20 20 6e 6f  rent, 1);.    no
32491 64 65 52 65 66 65 72 65 6e 63 65 28 70 4c 65 66  deReference(pLef
32492 74 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 21  t);.  }..  if( !
32493 70 4c 65 66 74 20 7c 7c 20 21 70 52 69 67 68 74  pLeft || !pRight
32494 20 29 7b 0a 20 20 20 20 72 63 20 3d 20 53 51 4c   ){.    rc = SQL
32495 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 67  ITE_NOMEM;.    g
32496 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
32497 74 3b 0a 20 20 7d 0a 0a 20 20 6d 65 6d 73 65 74  t;.  }..  memset
32498 28 70 4c 65 66 74 2d 3e 7a 44 61 74 61 2c 20 30  (pLeft->zData, 0
32499 2c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53  , pRtree->iNodeS
3249a 69 7a 65 29 3b 0a 20 20 6d 65 6d 73 65 74 28 70  ize);.  memset(p
3249b 52 69 67 68 74 2d 3e 7a 44 61 74 61 2c 20 30 2c  Right->zData, 0,
3249c 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
3249d 7a 65 29 3b 0a 0a 20 20 72 63 20 3d 20 41 73 73  ze);..  rc = Ass
3249e 69 67 6e 43 65 6c 6c 73 28 70 52 74 72 65 65 2c  ignCells(pRtree,
3249f 20 61 43 65 6c 6c 2c 20 6e 43 65 6c 6c 2c 20 70   aCell, nCell, p
324a0 4c 65 66 74 2c 20 70 52 69 67 68 74 2c 20 26 6c  Left, pRight, &l
324a1 65 66 74 62 62 6f 78 2c 20 26 72 69 67 68 74 62  eftbbox, &rightb
324a2 62 6f 78 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  box);.  if( rc!=
324a3 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
324a4 20 67 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f   goto splitnode_
324a5 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45  out;.  }..  /* E
324a6 6e 73 75 72 65 20 62 6f 74 68 20 63 68 69 6c 64  nsure both child
324a7 20 6e 6f 64 65 73 20 68 61 76 65 20 6e 6f 64 65   nodes have node
324a8 20 6e 75 6d 62 65 72 73 20 61 73 73 69 67 6e 65   numbers assigne
324a9 64 20 74 6f 20 74 68 65 6d 2e 20 2a 2f 0a 20 20  d to them. */.  
324aa 69 66 28 20 28 30 3d 3d 70 52 69 67 68 74 2d 3e  if( (0==pRight->
324ab 69 4e 6f 64 65 20 26 26 20 53 51 4c 49 54 45 5f  iNode && SQLITE_
324ac 4f 4b 21 3d 28 72 63 20 3d 20 6e 6f 64 65 57 72  OK!=(rc = nodeWr
324ad 69 74 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ite(pRtree, pRig
324ae 68 74 29 29 29 0a 20 20 20 7c 7c 20 28 30 3d 3d  ht))).   || (0==
324af 70 4c 65 66 74 2d 3e 69 4e 6f 64 65 20 26 26 20  pLeft->iNode && 
324b0 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
324b1 20 6e 6f 64 65 57 72 69 74 65 28 70 52 74 72 65   nodeWrite(pRtre
324b2 65 2c 20 70 4c 65 66 74 29 29 29 0a 20 20 29 7b  e, pLeft))).  ){
324b3 0a 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e  .    goto splitn
324b4 6f 64 65 5f 6f 75 74 3b 0a 20 20 7d 0a 0a 20 20  ode_out;.  }..  
324b5 72 69 67 68 74 62 62 6f 78 2e 69 52 6f 77 69 64  rightbbox.iRowid
324b6 20 3d 20 70 52 69 67 68 74 2d 3e 69 4e 6f 64 65   = pRight->iNode
324b7 3b 0a 20 20 6c 65 66 74 62 62 6f 78 2e 69 52 6f  ;.  leftbbox.iRo
324b8 77 69 64 20 3d 20 70 4c 65 66 74 2d 3e 69 4e 6f  wid = pLeft->iNo
324b9 64 65 3b 0a 0a 20 20 69 66 28 20 70 4e 6f 64 65  de;..  if( pNode
324ba 2d 3e 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20  ->iNode==1 ){.  
324bb 20 20 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65    rc = rtreeInse
324bc 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
324bd 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 2c 20 26  Left->pParent, &
324be 6c 65 66 74 62 62 6f 78 2c 20 69 48 65 69 67 68  leftbbox, iHeigh
324bf 74 2b 31 29 3b 0a 20 20 20 20 69 66 28 20 72 63  t+1);.    if( rc
324c0 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
324c1 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e       goto splitn
324c2 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  ode_out;.    }. 
324c3 20 7d 65 6c 73 65 7b 0a 20 20 20 20 52 74 72 65   }else{.    Rtre
324c4 65 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 20 3d  eNode *pParent =
324c5 20 70 4c 65 66 74 2d 3e 70 50 61 72 65 6e 74 3b   pLeft->pParent;
324c6 0a 20 20 20 20 69 6e 74 20 69 43 65 6c 6c 20 3d  .    int iCell =
324c7 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
324c8 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74 29 3b  (pRtree, pLeft);
324c9 0a 20 20 20 20 6e 6f 64 65 4f 76 65 72 77 72 69  .    nodeOverwri
324ca 74 65 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  teCell(pRtree, p
324cb 50 61 72 65 6e 74 2c 20 26 6c 65 66 74 62 62 6f  Parent, &leftbbo
324cc 78 2c 20 69 43 65 6c 6c 29 3b 0a 20 20 20 20 41  x, iCell);.    A
324cd 64 6a 75 73 74 54 72 65 65 28 70 52 74 72 65 65  djustTree(pRtree
324ce 2c 20 70 50 61 72 65 6e 74 2c 20 26 6c 65 66 74  , pParent, &left
324cf 62 62 6f 78 29 3b 0a 20 20 7d 0a 20 20 69 66 28  bbox);.  }.  if(
324d0 20 28 72 63 20 3d 20 72 74 72 65 65 49 6e 73 65   (rc = rtreeInse
324d1 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70  rtCell(pRtree, p
324d2 52 69 67 68 74 2d 3e 70 50 61 72 65 6e 74 2c 20  Right->pParent, 
324d3 26 72 69 67 68 74 62 62 6f 78 2c 20 69 48 65 69  &rightbbox, iHei
324d4 67 68 74 2b 31 29 29 20 29 7b 0a 20 20 20 20 67  ght+1)) ){.    g
324d5 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
324d6 74 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 3d  t;.  }..  for(i=
324d7 30 3b 20 69 3c 4e 43 45 4c 4c 28 70 52 69 67 68  0; i<NCELL(pRigh
324d8 74 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 69 36  t); i++){.    i6
324d9 34 20 69 52 6f 77 69 64 20 3d 20 6e 6f 64 65 47  4 iRowid = nodeG
324da 65 74 52 6f 77 69 64 28 70 52 74 72 65 65 2c 20  etRowid(pRtree, 
324db 70 52 69 67 68 74 2c 20 69 29 3b 0a 20 20 20 20  pRight, i);.    
324dc 72 63 20 3d 20 75 70 64 61 74 65 4d 61 70 70 69  rc = updateMappi
324dd 6e 67 28 70 52 74 72 65 65 2c 20 69 52 6f 77 69  ng(pRtree, iRowi
324de 64 2c 20 70 52 69 67 68 74 2c 20 69 48 65 69 67  d, pRight, iHeig
324df 68 74 29 3b 0a 20 20 20 20 69 66 28 20 69 52 6f  ht);.    if( iRo
324e0 77 69 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77  wid==pCell->iRow
324e1 69 64 20 29 7b 0a 20 20 20 20 20 20 6e 65 77 43  id ){.      newC
324e2 65 6c 6c 49 73 52 69 67 68 74 20 3d 20 31 3b 0a  ellIsRight = 1;.
324e3 20 20 20 20 7d 0a 20 20 20 20 69 66 28 20 72 63      }.    if( rc
324e4 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20  !=SQLITE_OK ){. 
324e5 20 20 20 20 20 67 6f 74 6f 20 73 70 6c 69 74 6e       goto splitn
324e6 6f 64 65 5f 6f 75 74 3b 0a 20 20 20 20 7d 0a 20  ode_out;.    }. 
324e7 20 7d 0a 20 20 69 66 28 20 70 4e 6f 64 65 2d 3e   }.  if( pNode->
324e8 69 4e 6f 64 65 3d 3d 31 20 29 7b 0a 20 20 20 20  iNode==1 ){.    
324e9 66 6f 72 28 69 3d 30 3b 20 69 3c 4e 43 45 4c 4c  for(i=0; i<NCELL
324ea 28 70 4c 65 66 74 29 3b 20 69 2b 2b 29 7b 0a 20  (pLeft); i++){. 
324eb 20 20 20 20 20 69 36 34 20 69 52 6f 77 69 64 20       i64 iRowid 
324ec 3d 20 6e 6f 64 65 47 65 74 52 6f 77 69 64 28 70  = nodeGetRowid(p
324ed 52 74 72 65 65 2c 20 70 4c 65 66 74 2c 20 69 29  Rtree, pLeft, i)
324ee 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 75 70 64  ;.      rc = upd
324ef 61 74 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65  ateMapping(pRtre
324f0 65 2c 20 69 52 6f 77 69 64 2c 20 70 4c 65 66 74  e, iRowid, pLeft
324f1 2c 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20  , iHeight);.    
324f2 20 20 69 66 28 20 72 63 21 3d 53 51 4c 49 54 45    if( rc!=SQLITE
324f3 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 67  _OK ){.        g
324f4 6f 74 6f 20 73 70 6c 69 74 6e 6f 64 65 5f 6f 75  oto splitnode_ou
324f5 74 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d  t;.      }.    }
324f6 0a 20 20 7d 65 6c 73 65 20 69 66 28 20 6e 65 77  .  }else if( new
324f7 43 65 6c 6c 49 73 52 69 67 68 74 3d 3d 30 20 29  CellIsRight==0 )
324f8 7b 0a 20 20 20 20 72 63 20 3d 20 75 70 64 61 74  {.    rc = updat
324f9 65 4d 61 70 70 69 6e 67 28 70 52 74 72 65 65 2c  eMapping(pRtree,
324fa 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 2c 20   pCell->iRowid, 
324fb 70 4c 65 66 74 2c 20 69 48 65 69 67 68 74 29 3b  pLeft, iHeight);
324fc 0a 20 20 7d 0a 0a 20 20 69 66 28 20 72 63 3d 3d  .  }..  if( rc==
324fd 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
324fe 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61 73   rc = nodeReleas
324ff 65 28 70 52 74 72 65 65 2c 20 70 52 69 67 68 74  e(pRtree, pRight
32500 29 3b 0a 20 20 20 20 70 52 69 67 68 74 20 3d 20  );.    pRight = 
32501 30 3b 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d  0;.  }.  if( rc=
32502 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
32503 20 20 72 63 20 3d 20 6e 6f 64 65 52 65 6c 65 61    rc = nodeRelea
32504 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  se(pRtree, pLeft
32505 29 3b 0a 20 20 20 20 70 4c 65 66 74 20 3d 20 30  );.    pLeft = 0
32506 3b 0a 20 20 7d 0a 0a 73 70 6c 69 74 6e 6f 64 65  ;.  }..splitnode
32507 5f 6f 75 74 3a 0a 20 20 6e 6f 64 65 52 65 6c 65  _out:.  nodeRele
32508 61 73 65 28 70 52 74 72 65 65 2c 20 70 52 69 67  ase(pRtree, pRig
32509 68 74 29 3b 0a 20 20 6e 6f 64 65 52 65 6c 65 61  ht);.  nodeRelea
3250a 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 66 74  se(pRtree, pLeft
3250b 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65  );.  sqlite3_fre
3250c 65 28 61 43 65 6c 6c 29 3b 0a 20 20 72 65 74 75  e(aCell);.  retu
3250d 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74 61 74 69 63  rn rc;.}..static
3250e 20 69 6e 74 20 66 69 78 4c 65 61 66 50 61 72 65   int fixLeafPare
3250f 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  nt(Rtree *pRtree
32510 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c 65  , RtreeNode *pLe
32511 61 66 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20  af){.  int rc = 
32512 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 66 28  SQLITE_OK;.  if(
32513 20 70 4c 65 61 66 2d 3e 69 4e 6f 64 65 21 3d 31   pLeaf->iNode!=1
32514 20 26 26 20 70 4c 65 61 66 2d 3e 70 50 61 72 65   && pLeaf->pPare
32515 6e 74 3d 3d 30 20 29 7b 0a 20 20 20 20 73 71 6c  nt==0 ){.    sql
32516 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
32517 70 52 74 72 65 65 2d 3e 70 52 65 61 64 50 61 72  pRtree->pReadPar
32518 65 6e 74 2c 20 31 2c 20 70 4c 65 61 66 2d 3e 69  ent, 1, pLeaf->i
32519 4e 6f 64 65 29 3b 0a 20 20 20 20 69 66 28 20 73  Node);.    if( s
3251a 71 6c 69 74 65 33 5f 73 74 65 70 28 70 52 74 72  qlite3_step(pRtr
3251b 65 65 2d 3e 70 52 65 61 64 50 61 72 65 6e 74 29  ee->pReadParent)
3251c 3d 3d 53 51 4c 49 54 45 5f 52 4f 57 20 29 7b 0a  ==SQLITE_ROW ){.
3251d 20 20 20 20 20 20 69 36 34 20 69 4e 6f 64 65 20        i64 iNode 
3251e 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
3251f 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e 70  _int64(pRtree->p
32520 52 65 61 64 50 61 72 65 6e 74 2c 20 30 29 3b 0a  ReadParent, 0);.
32521 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41        rc = nodeA
32522 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69  cquire(pRtree, i
32523 4e 6f 64 65 2c 20 30 2c 20 26 70 4c 65 61 66 2d  Node, 0, &pLeaf-
32524 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d  >pParent);.    }
32525 65 6c 73 65 7b 0a 20 20 20 20 20 20 72 63 20 3d  else{.      rc =
32526 20 53 51 4c 49 54 45 5f 45 52 52 4f 52 3b 0a 20   SQLITE_ERROR;. 
32527 20 20 20 7d 0a 20 20 20 20 73 71 6c 69 74 65 33     }.    sqlite3
32528 5f 72 65 73 65 74 28 70 52 74 72 65 65 2d 3e 70  _reset(pRtree->p
32529 52 65 61 64 50 61 72 65 6e 74 29 3b 0a 20 20 20  ReadParent);.   
3252a 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
3252b 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  OK ){.      rc =
3252c 20 66 69 78 4c 65 61 66 50 61 72 65 6e 74 28 70   fixLeafParent(p
3252d 52 74 72 65 65 2c 20 70 4c 65 61 66 2d 3e 70 50  Rtree, pLeaf->pP
3252e 61 72 65 6e 74 29 3b 0a 20 20 20 20 7d 0a 20 20  arent);.    }.  
3252f 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d  }.  return rc;.}
32530 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 64 65 6c  ..static int del
32531 65 74 65 43 65 6c 6c 28 52 74 72 65 65 20 2a 2c  eteCell(Rtree *,
32532 20 52 74 72 65 65 4e 6f 64 65 20 2a 2c 20 69 6e   RtreeNode *, in
32533 74 2c 20 69 6e 74 29 3b 0a 0a 73 74 61 74 69 63  t, int);..static
32534 20 69 6e 74 20 72 65 6d 6f 76 65 4e 6f 64 65 28   int removeNode(
32535 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52  Rtree *pRtree, R
32536 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 2c  treeNode *pNode,
32537 20 69 6e 74 20 69 48 65 69 67 68 74 29 7b 0a 20   int iHeight){. 
32538 20 69 6e 74 20 72 63 3b 0a 20 20 52 74 72 65 65   int rc;.  Rtree
32539 4e 6f 64 65 20 2a 70 50 61 72 65 6e 74 3b 0a 20  Node *pParent;. 
3253a 20 69 6e 74 20 69 43 65 6c 6c 3b 0a 0a 20 20 61   int iCell;..  a
3253b 73 73 65 72 74 28 20 70 4e 6f 64 65 2d 3e 6e 52  ssert( pNode->nR
3253c 65 66 3d 3d 31 20 29 3b 0a 0a 20 20 2f 2a 20 52  ef==1 );..  /* R
3253d 65 6d 6f 76 65 20 74 68 65 20 65 6e 74 72 79 20  emove the entry 
3253e 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 63 65  in the parent ce
3253f 6c 6c 2e 20 2a 2f 0a 20 20 69 43 65 6c 6c 20 3d  ll. */.  iCell =
32540 20 6e 6f 64 65 50 61 72 65 6e 74 49 6e 64 65 78   nodeParentIndex
32541 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
32542 0a 20 20 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  .  pParent = pNo
32543 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 70  de->pParent;.  p
32544 4e 6f 64 65 2d 3e 70 50 61 72 65 6e 74 20 3d 20  Node->pParent = 
32545 30 3b 0a 20 20 69 66 28 20 53 51 4c 49 54 45 5f  0;.  if( SQLITE_
32546 4f 4b 21 3d 28 72 63 20 3d 20 64 65 6c 65 74 65  OK!=(rc = delete
32547 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61  Cell(pRtree, pPa
32548 72 65 6e 74 2c 20 69 43 65 6c 6c 2c 20 69 48 65  rent, iCell, iHe
32549 69 67 68 74 2b 31 29 29 20 0a 20 20 20 7c 7c 20  ight+1)) .   || 
3254a 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d  SQLITE_OK!=(rc =
3254b 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
3254c 72 65 65 2c 20 70 50 61 72 65 6e 74 29 29 0a 20  ree, pParent)). 
3254d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72   ){.    return r
3254e 63 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 52 65 6d  c;.  }..  /* Rem
3254f 6f 76 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65  ove the xxx_node
32550 20 65 6e 74 72 79 2e 20 2a 2f 0a 20 20 73 71 6c   entry. */.  sql
32551 69 74 65 33 5f 62 69 6e 64 5f 69 6e 74 36 34 28  ite3_bind_int64(
32552 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 4e  pRtree->pDeleteN
32553 6f 64 65 2c 20 31 2c 20 70 4e 6f 64 65 2d 3e 69  ode, 1, pNode->i
32554 4e 6f 64 65 29 3b 0a 20 20 73 71 6c 69 74 65 33  Node);.  sqlite3
32555 5f 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 44  _step(pRtree->pD
32556 65 6c 65 74 65 4e 6f 64 65 29 3b 0a 20 20 69 66  eleteNode);.  if
32557 28 20 53 51 4c 49 54 45 5f 4f 4b 21 3d 28 72 63  ( SQLITE_OK!=(rc
32558 20 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74   = sqlite3_reset
32559 28 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  (pRtree->pDelete
3255a 4e 6f 64 65 29 29 20 29 7b 0a 20 20 20 20 72 65  Node)) ){.    re
3255b 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a 0a 20 20  turn rc;.  }..  
3255c 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 78 78  /* Remove the xx
3255d 78 5f 70 61 72 65 6e 74 20 65 6e 74 72 79 2e 20  x_parent entry. 
3255e 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 62 69 6e  */.  sqlite3_bin
3255f 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65 2d 3e  d_int64(pRtree->
32560 70 44 65 6c 65 74 65 50 61 72 65 6e 74 2c 20 31  pDeleteParent, 1
32561 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b  , pNode->iNode);
32562 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28  .  sqlite3_step(
32563 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 50  pRtree->pDeleteP
32564 61 72 65 6e 74 29 3b 0a 20 20 69 66 28 20 53 51  arent);.  if( SQ
32565 4c 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 73  LITE_OK!=(rc = s
32566 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52 74  qlite3_reset(pRt
32567 72 65 65 2d 3e 70 44 65 6c 65 74 65 50 61 72 65  ree->pDeletePare
32568 6e 74 29 29 20 29 7b 0a 20 20 20 20 72 65 74 75  nt)) ){.    retu
32569 72 6e 20 72 63 3b 0a 20 20 7d 0a 20 20 0a 20 20  rn rc;.  }.  .  
3256a 2f 2a 20 52 65 6d 6f 76 65 20 74 68 65 20 6e 6f  /* Remove the no
3256b 64 65 20 66 72 6f 6d 20 74 68 65 20 69 6e 2d 6d  de from the in-m
3256c 65 6d 6f 72 79 20 68 61 73 68 20 74 61 62 6c 65  emory hash table
3256d 20 61 6e 64 20 6c 69 6e 6b 20 69 74 20 69 6e 74   and link it int
3256e 6f 0a 20 20 2a 2a 20 74 68 65 20 52 74 72 65 65  o.  ** the Rtree
3256f 2e 70 44 65 6c 65 74 65 64 20 6c 69 73 74 2e 20  .pDeleted list. 
32570 49 74 73 20 63 6f 6e 74 65 6e 74 73 20 77 69 6c  Its contents wil
32571 6c 20 62 65 20 72 65 2d 69 6e 73 65 72 74 65 64  l be re-inserted
32572 20 6c 61 74 65 72 20 6f 6e 2e 0a 20 20 2a 2f 0a   later on..  */.
32573 20 20 6e 6f 64 65 48 61 73 68 44 65 6c 65 74 65    nodeHashDelete
32574 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 29 3b  (pRtree, pNode);
32575 0a 20 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20  .  pNode->iNode 
32576 3d 20 69 48 65 69 67 68 74 3b 0a 20 20 70 4e 6f  = iHeight;.  pNo
32577 64 65 2d 3e 70 4e 65 78 74 20 3d 20 70 52 74 72  de->pNext = pRtr
32578 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 0a 20 20  ee->pDeleted;.  
32579 70 4e 6f 64 65 2d 3e 6e 52 65 66 2b 2b 3b 0a 20  pNode->nRef++;. 
3257a 20 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65   pRtree->pDelete
3257b 64 20 3d 20 70 4e 6f 64 65 3b 0a 0a 20 20 72 65  d = pNode;..  re
3257c 74 75 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a  turn SQLITE_OK;.
3257d 7d 0a 0a 73 74 61 74 69 63 20 76 6f 69 64 20 66  }..static void f
3257e 69 78 42 6f 75 6e 64 69 6e 67 42 6f 78 28 52 74  ixBoundingBox(Rt
3257f 72 65 65 20 2a 70 52 74 72 65 65 2c 20 52 74 72  ree *pRtree, Rtr
32580 65 65 4e 6f 64 65 20 2a 70 4e 6f 64 65 29 7b 0a  eeNode *pNode){.
32581 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 50 61    RtreeNode *pPa
32582 72 65 6e 74 20 3d 20 70 4e 6f 64 65 2d 3e 70 50  rent = pNode->pP
32583 61 72 65 6e 74 3b 0a 20 20 69 66 28 20 70 50 61  arent;.  if( pPa
32584 72 65 6e 74 20 29 7b 0a 20 20 20 20 69 6e 74 20  rent ){.    int 
32585 69 69 3b 20 0a 20 20 20 20 69 6e 74 20 6e 43 65  ii; .    int nCe
32586 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65  ll = NCELL(pNode
32587 29 3b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  );.    RtreeCell
32588 20 62 6f 78 3b 20 20 20 20 20 20 20 20 20 20 20   box;           
32589 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3258a 20 2f 2a 20 42 6f 75 6e 64 69 6e 67 20 62 6f 78   /* Bounding box
3258b 20 66 6f 72 20 70 4e 6f 64 65 20 2a 2f 0a 20 20   for pNode */.  
3258c 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
3258d 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 30 2c 20  tree, pNode, 0, 
3258e 26 62 6f 78 29 3b 0a 20 20 20 20 66 6f 72 28 69  &box);.    for(i
3258f 69 3d 31 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69  i=1; ii<nCell; i
32590 69 2b 2b 29 7b 0a 20 20 20 20 20 20 52 74 72 65  i++){.      Rtre
32591 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
32592 20 20 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52    nodeGetCell(pR
32593 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c  tree, pNode, ii,
32594 20 26 63 65 6c 6c 29 3b 0a 20 20 20 20 20 20 63   &cell);.      c
32595 65 6c 6c 55 6e 69 6f 6e 28 70 52 74 72 65 65 2c  ellUnion(pRtree,
32596 20 26 62 6f 78 2c 20 26 63 65 6c 6c 29 3b 0a 20   &box, &cell);. 
32597 20 20 20 7d 0a 20 20 20 20 62 6f 78 2e 69 52 6f     }.    box.iRo
32598 77 69 64 20 3d 20 70 4e 6f 64 65 2d 3e 69 4e 6f  wid = pNode->iNo
32599 64 65 3b 0a 20 20 20 20 69 69 20 3d 20 6e 6f 64  de;.    ii = nod
3259a 65 50 61 72 65 6e 74 49 6e 64 65 78 28 70 52 74  eParentIndex(pRt
3259b 72 65 65 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 20  ree, pNode);.   
3259c 20 6e 6f 64 65 4f 76 65 72 77 72 69 74 65 43 65   nodeOverwriteCe
3259d 6c 6c 28 70 52 74 72 65 65 2c 20 70 50 61 72 65  ll(pRtree, pPare
3259e 6e 74 2c 20 26 62 6f 78 2c 20 69 69 29 3b 0a 20  nt, &box, ii);. 
3259f 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67 42 6f     fixBoundingBo
325a0 78 28 70 52 74 72 65 65 2c 20 70 50 61 72 65 6e  x(pRtree, pParen
325a1 74 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a  t);.  }.}../*.**
325a2 20 44 65 6c 65 74 65 20 74 68 65 20 63 65 6c 6c   Delete the cell
325a3 20 61 74 20 69 6e 64 65 78 20 69 43 65 6c 6c 20   at index iCell 
325a4 6f 66 20 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 41  of node pNode. A
325a5 66 74 65 72 20 72 65 6d 6f 76 69 6e 67 20 74 68  fter removing th
325a6 65 0a 2a 2a 20 63 65 6c 6c 2c 20 61 64 6a 75 73  e.** cell, adjus
325a7 74 20 74 68 65 20 72 2d 74 72 65 65 20 64 61 74  t the r-tree dat
325a8 61 20 73 74 72 75 63 74 75 72 65 20 69 66 20 72  a structure if r
325a9 65 71 75 69 72 65 64 2e 0a 2a 2f 0a 73 74 61 74  equired..*/.stat
325aa 69 63 20 69 6e 74 20 64 65 6c 65 74 65 43 65 6c  ic int deleteCel
325ab 6c 28 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c  l(Rtree *pRtree,
325ac 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
325ad 65 2c 20 69 6e 74 20 69 43 65 6c 6c 2c 20 69 6e  e, int iCell, in
325ae 74 20 69 48 65 69 67 68 74 29 7b 0a 20 20 69 6e  t iHeight){.  in
325af 74 20 72 63 3b 0a 0a 20 20 69 66 28 20 53 51 4c  t rc;..  if( SQL
325b0 49 54 45 5f 4f 4b 21 3d 28 72 63 20 3d 20 66 69  ITE_OK!=(rc = fi
325b1 78 4c 65 61 66 50 61 72 65 6e 74 28 70 52 74 72  xLeafParent(pRtr
325b2 65 65 2c 20 70 4e 6f 64 65 29 29 20 29 7b 0a 20  ee, pNode)) ){. 
325b3 20 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20     return rc;.  
325b4 7d 0a 0a 20 20 2f 2a 20 52 65 6d 6f 76 65 20 74  }..  /* Remove t
325b5 68 65 20 63 65 6c 6c 20 66 72 6f 6d 20 74 68 65  he cell from the
325b6 20 6e 6f 64 65 2e 20 54 68 69 73 20 63 61 6c 6c   node. This call
325b7 20 6a 75 73 74 20 6d 6f 76 65 73 20 62 79 74 65   just moves byte
325b8 73 20 61 72 6f 75 6e 64 0a 20 20 2a 2a 20 74 68  s around.  ** th
325b9 65 20 69 6e 2d 6d 65 6d 6f 72 79 20 6e 6f 64 65  e in-memory node
325ba 20 69 6d 61 67 65 2c 20 73 6f 20 69 74 20 63 61   image, so it ca
325bb 6e 6e 6f 74 20 66 61 69 6c 2e 0a 20 20 2a 2f 0a  nnot fail..  */.
325bc 20 20 6e 6f 64 65 44 65 6c 65 74 65 43 65 6c 6c    nodeDeleteCell
325bd 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65 2c 20  (pRtree, pNode, 
325be 69 43 65 6c 6c 29 3b 0a 0a 20 20 2f 2a 20 49 66  iCell);..  /* If
325bf 20 74 68 65 20 6e 6f 64 65 20 69 73 20 6e 6f 74   the node is not
325c0 20 74 68 65 20 74 72 65 65 20 72 6f 6f 74 20 61   the tree root a
325c1 6e 64 20 6e 6f 77 20 68 61 73 20 6c 65 73 73 20  nd now has less 
325c2 74 68 61 6e 20 74 68 65 20 6d 69 6e 69 6d 75 6d  than the minimum
325c3 0a 20 20 2a 2a 20 6e 75 6d 62 65 72 20 6f 66 20  .  ** number of 
325c4 63 65 6c 6c 73 2c 20 72 65 6d 6f 76 65 20 69 74  cells, remove it
325c5 20 66 72 6f 6d 20 74 68 65 20 74 72 65 65 2e 20   from the tree. 
325c6 4f 74 68 65 72 77 69 73 65 2c 20 75 70 64 61 74  Otherwise, updat
325c7 65 20 74 68 65 0a 20 20 2a 2a 20 63 65 6c 6c 20  e the.  ** cell 
325c8 69 6e 20 74 68 65 20 70 61 72 65 6e 74 20 6e 6f  in the parent no
325c9 64 65 20 73 6f 20 74 68 61 74 20 69 74 20 74 69  de so that it ti
325ca 67 68 74 6c 79 20 63 6f 6e 74 61 69 6e 73 20 74  ghtly contains t
325cb 68 65 20 75 70 64 61 74 65 64 0a 20 20 2a 2a 20  he updated.  ** 
325cc 6e 6f 64 65 2e 0a 20 20 2a 2f 0a 20 20 69 66 28  node..  */.  if(
325cd 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 21 3d 31   pNode->iNode!=1
325ce 20 29 7b 0a 20 20 20 20 52 74 72 65 65 4e 6f 64   ){.    RtreeNod
325cf 65 20 2a 70 50 61 72 65 6e 74 20 3d 20 70 4e 6f  e *pParent = pNo
325d0 64 65 2d 3e 70 50 61 72 65 6e 74 3b 0a 20 20 20  de->pParent;.   
325d1 20 69 66 28 20 28 70 50 61 72 65 6e 74 2d 3e 69   if( (pParent->i
325d2 4e 6f 64 65 21 3d 31 20 7c 7c 20 4e 43 45 4c 4c  Node!=1 || NCELL
325d3 28 70 50 61 72 65 6e 74 29 21 3d 31 29 20 0a 20  (pParent)!=1) . 
325d4 20 20 20 20 26 26 20 28 4e 43 45 4c 4c 28 70 4e      && (NCELL(pN
325d5 6f 64 65 29 3c 52 54 52 45 45 5f 4d 49 4e 43 45  ode)<RTREE_MINCE
325d6 4c 4c 53 28 70 52 74 72 65 65 29 29 0a 20 20 20  LLS(pRtree)).   
325d7 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 72   ){.      rc = r
325d8 65 6d 6f 76 65 4e 6f 64 65 28 70 52 74 72 65 65  emoveNode(pRtree
325d9 2c 20 70 4e 6f 64 65 2c 20 69 48 65 69 67 68 74  , pNode, iHeight
325da 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
325db 20 20 20 20 66 69 78 42 6f 75 6e 64 69 6e 67 42      fixBoundingB
325dc 6f 78 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  ox(pRtree, pNode
325dd 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20  );.    }.  }..  
325de 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 73 74  return rc;.}..st
325df 61 74 69 63 20 69 6e 74 20 52 65 69 6e 73 65 72  atic int Reinser
325e0 74 28 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72  t(.  Rtree *pRtr
325e1 65 65 2c 20 0a 20 20 52 74 72 65 65 4e 6f 64 65  ee, .  RtreeNode
325e2 20 2a 70 4e 6f 64 65 2c 20 0a 20 20 52 74 72 65   *pNode, .  Rtre
325e3 65 43 65 6c 6c 20 2a 70 43 65 6c 6c 2c 20 0a 20  eCell *pCell, . 
325e4 20 69 6e 74 20 69 48 65 69 67 68 74 0a 29 7b 0a   int iHeight.){.
325e5 20 20 69 6e 74 20 2a 61 4f 72 64 65 72 3b 0a 20    int *aOrder;. 
325e6 20 69 6e 74 20 2a 61 53 70 61 72 65 3b 0a 20 20   int *aSpare;.  
325e7 52 74 72 65 65 43 65 6c 6c 20 2a 61 43 65 6c 6c  RtreeCell *aCell
325e8 3b 0a 20 20 66 6c 6f 61 74 20 2a 61 44 69 73 74  ;.  float *aDist
325e9 61 6e 63 65 3b 0a 20 20 69 6e 74 20 6e 43 65 6c  ance;.  int nCel
325ea 6c 3b 0a 20 20 66 6c 6f 61 74 20 61 43 65 6e 74  l;.  float aCent
325eb 65 72 43 6f 6f 72 64 5b 52 54 52 45 45 5f 4d 41  erCoord[RTREE_MA
325ec 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 5d 3b 0a 20  X_DIMENSIONS];. 
325ed 20 69 6e 74 20 69 44 69 6d 3b 0a 20 20 69 6e 74   int iDim;.  int
325ee 20 69 69 3b 0a 20 20 69 6e 74 20 72 63 20 3d 20   ii;.  int rc = 
325ef 53 51 4c 49 54 45 5f 4f 4b 3b 0a 0a 20 20 6d 65  SQLITE_OK;..  me
325f0 6d 73 65 74 28 61 43 65 6e 74 65 72 43 6f 6f 72  mset(aCenterCoor
325f1 64 2c 20 30 2c 20 73 69 7a 65 6f 66 28 66 6c 6f  d, 0, sizeof(flo
325f2 61 74 29 2a 52 54 52 45 45 5f 4d 41 58 5f 44 49  at)*RTREE_MAX_DI
325f3 4d 45 4e 53 49 4f 4e 53 29 3b 0a 0a 20 20 6e 43  MENSIONS);..  nC
325f4 65 6c 6c 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64  ell = NCELL(pNod
325f5 65 29 2b 31 3b 0a 0a 20 20 2f 2a 20 41 6c 6c 6f  e)+1;..  /* Allo
325f6 63 61 74 65 20 74 68 65 20 62 75 66 66 65 72 73  cate the buffers
325f7 20 75 73 65 64 20 62 79 20 74 68 69 73 20 6f 70   used by this op
325f8 65 72 61 74 69 6f 6e 2e 20 54 68 65 20 61 6c 6c  eration. The all
325f9 6f 63 61 74 69 6f 6e 20 69 73 0a 20 20 2a 2a 20  ocation is.  ** 
325fa 72 65 6c 69 6e 71 75 69 73 68 65 64 20 62 65 66  relinquished bef
325fb 6f 72 65 20 74 68 69 73 20 66 75 6e 63 74 69 6f  ore this functio
325fc 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20 2a 2f 0a  n returns..  */.
325fd 20 20 61 43 65 6c 6c 20 3d 20 28 52 74 72 65 65    aCell = (Rtree
325fe 43 65 6c 6c 20 2a 29 73 71 6c 69 74 65 33 5f 6d  Cell *)sqlite3_m
325ff 61 6c 6c 6f 63 28 6e 43 65 6c 6c 20 2a 20 28 0a  alloc(nCell * (.
32600 20 20 20 20 73 69 7a 65 6f 66 28 52 74 72 65 65      sizeof(Rtree
32601 43 65 6c 6c 29 20 2b 20 20 20 20 20 20 20 20 20  Cell) +         
32602 2f 2a 20 61 43 65 6c 6c 20 61 72 72 61 79 20 2a  /* aCell array *
32603 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28 69 6e 74  /.    sizeof(int
32604 29 20 20 20 20 20 20 20 2b 20 20 20 20 20 20 20  )       +       
32605 20 20 2f 2a 20 61 4f 72 64 65 72 20 61 72 72 61    /* aOrder arra
32606 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65 6f 66 28  y */.    sizeof(
32607 69 6e 74 29 20 20 20 20 20 20 20 2b 20 20 20 20  int)       +    
32608 20 20 20 20 20 2f 2a 20 61 53 70 61 72 65 20 61       /* aSpare a
32609 72 72 61 79 20 2a 2f 0a 20 20 20 20 73 69 7a 65  rray */.    size
3260a 6f 66 28 66 6c 6f 61 74 29 20 20 20 20 20 20 20  of(float)       
3260b 20 20 20 20 20 20 20 20 2f 2a 20 61 44 69 73 74          /* aDist
3260c 61 6e 63 65 20 61 72 72 61 79 20 2a 2f 0a 20 20  ance array */.  
3260d 29 29 3b 0a 20 20 69 66 28 20 21 61 43 65 6c 6c  ));.  if( !aCell
3260e 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
3260f 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
32610 0a 20 20 61 4f 72 64 65 72 20 20 20 20 3d 20 28  .  aOrder    = (
32611 69 6e 74 20 2a 29 26 61 43 65 6c 6c 5b 6e 43 65  int *)&aCell[nCe
32612 6c 6c 5d 3b 0a 20 20 61 53 70 61 72 65 20 20 20  ll];.  aSpare   
32613 20 3d 20 28 69 6e 74 20 2a 29 26 61 4f 72 64 65   = (int *)&aOrde
32614 72 5b 6e 43 65 6c 6c 5d 3b 0a 20 20 61 44 69 73  r[nCell];.  aDis
32615 74 61 6e 63 65 20 3d 20 28 66 6c 6f 61 74 20 2a  tance = (float *
32616 29 26 61 53 70 61 72 65 5b 6e 43 65 6c 6c 5d 3b  )&aSpare[nCell];
32617 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69  ..  for(ii=0; ii
32618 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a 20  <nCell; ii++){. 
32619 20 20 20 69 66 28 20 69 69 3d 3d 28 6e 43 65 6c     if( ii==(nCel
3261a 6c 2d 31 29 20 29 7b 0a 20 20 20 20 20 20 6d 65  l-1) ){.      me
3261b 6d 63 70 79 28 26 61 43 65 6c 6c 5b 69 69 5d 2c  mcpy(&aCell[ii],
3261c 20 70 43 65 6c 6c 2c 20 73 69 7a 65 6f 66 28 52   pCell, sizeof(R
3261d 74 72 65 65 43 65 6c 6c 29 29 3b 0a 20 20 20 20  treeCell));.    
3261e 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e 6f 64  }else{.      nod
3261f 65 47 65 74 43 65 6c 6c 28 70 52 74 72 65 65 2c  eGetCell(pRtree,
32620 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26 61 43 65   pNode, ii, &aCe
32621 6c 6c 5b 69 69 5d 29 3b 0a 20 20 20 20 7d 0a 20  ll[ii]);.    }. 
32622 20 20 20 61 4f 72 64 65 72 5b 69 69 5d 20 3d 20     aOrder[ii] = 
32623 69 69 3b 0a 20 20 20 20 66 6f 72 28 69 44 69 6d  ii;.    for(iDim
32624 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65 65 2d  =0; iDim<pRtree-
32625 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29 7b 0a  >nDim; iDim++){.
32626 20 20 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f        aCenterCoo
32627 72 64 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f  rd[iDim] += DCOO
32628 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f  RD(aCell[ii].aCo
32629 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20 20  ord[iDim*2]);.  
3262a 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f 72 64      aCenterCoord
3262b 5b 69 44 69 6d 5d 20 2b 3d 20 44 43 4f 4f 52 44  [iDim] += DCOORD
3262c 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43 6f 6f 72  (aCell[ii].aCoor
3262d 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 3b 0a 20 20  d[iDim*2+1]);.  
3262e 20 20 7d 0a 20 20 7d 0a 20 20 66 6f 72 28 69 44    }.  }.  for(iD
3262f 69 6d 3d 30 3b 20 69 44 69 6d 3c 70 52 74 72 65  im=0; iDim<pRtre
32630 65 2d 3e 6e 44 69 6d 3b 20 69 44 69 6d 2b 2b 29  e->nDim; iDim++)
32631 7b 0a 20 20 20 20 61 43 65 6e 74 65 72 43 6f 6f  {.    aCenterCoo
32632 72 64 5b 69 44 69 6d 5d 20 3d 20 61 43 65 6e 74  rd[iDim] = aCent
32633 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 2f 28 28  erCoord[iDim]/((
32634 66 6c 6f 61 74 29 6e 43 65 6c 6c 2a 32 2e 30 29  float)nCell*2.0)
32635 3b 0a 20 20 7d 0a 0a 20 20 66 6f 72 28 69 69 3d  ;.  }..  for(ii=
32636 30 3b 20 69 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b  0; ii<nCell; ii+
32637 2b 29 7b 0a 20 20 20 20 61 44 69 73 74 61 6e 63  +){.    aDistanc
32638 65 5b 69 69 5d 20 3d 20 30 2e 30 3b 0a 20 20 20  e[ii] = 0.0;.   
32639 20 66 6f 72 28 69 44 69 6d 3d 30 3b 20 69 44 69   for(iDim=0; iDi
3263a 6d 3c 70 52 74 72 65 65 2d 3e 6e 44 69 6d 3b 20  m<pRtree->nDim; 
3263b 69 44 69 6d 2b 2b 29 7b 0a 20 20 20 20 20 20 66  iDim++){.      f
3263c 6c 6f 61 74 20 63 6f 6f 72 64 20 3d 20 44 43 4f  loat coord = DCO
3263d 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43  ORD(aCell[ii].aC
3263e 6f 6f 72 64 5b 69 44 69 6d 2a 32 2b 31 5d 29 20  oord[iDim*2+1]) 
3263f 2d 20 0a 20 20 20 20 20 20 20 20 20 20 44 43 4f  - .          DCO
32640 4f 52 44 28 61 43 65 6c 6c 5b 69 69 5d 2e 61 43  ORD(aCell[ii].aC
32641 6f 6f 72 64 5b 69 44 69 6d 2a 32 5d 29 3b 0a 20  oord[iDim*2]);. 
32642 20 20 20 20 20 61 44 69 73 74 61 6e 63 65 5b 69       aDistance[i
32643 69 5d 20 2b 3d 20 28 63 6f 6f 72 64 2d 61 43 65  i] += (coord-aCe
32644 6e 74 65 72 43 6f 6f 72 64 5b 69 44 69 6d 5d 29  nterCoord[iDim])
32645 2a 28 63 6f 6f 72 64 2d 61 43 65 6e 74 65 72 43  *(coord-aCenterC
32646 6f 6f 72 64 5b 69 44 69 6d 5d 29 3b 0a 20 20 20  oord[iDim]);.   
32647 20 7d 0a 20 20 7d 0a 0a 20 20 53 6f 72 74 42 79   }.  }..  SortBy
32648 44 69 73 74 61 6e 63 65 28 61 4f 72 64 65 72 2c  Distance(aOrder,
32649 20 6e 43 65 6c 6c 2c 20 61 44 69 73 74 61 6e 63   nCell, aDistanc
3264a 65 2c 20 61 53 70 61 72 65 29 3b 0a 20 20 6e 6f  e, aSpare);.  no
3264b 64 65 5a 65 72 6f 28 70 52 74 72 65 65 2c 20 70  deZero(pRtree, p
3264c 4e 6f 64 65 29 3b 0a 0a 20 20 66 6f 72 28 69 69  Node);..  for(ii
3264d 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  =0; rc==SQLITE_O
3264e 4b 20 26 26 20 69 69 3c 28 6e 43 65 6c 6c 2d 28  K && ii<(nCell-(
3264f 52 54 52 45 45 5f 4d 49 4e 43 45 4c 4c 53 28 70  RTREE_MINCELLS(p
32650 52 74 72 65 65 29 2b 31 29 29 3b 20 69 69 2b 2b  Rtree)+1)); ii++
32651 29 7b 0a 20 20 20 20 52 74 72 65 65 43 65 6c 6c  ){.    RtreeCell
32652 20 2a 70 20 3d 20 26 61 43 65 6c 6c 5b 61 4f 72   *p = &aCell[aOr
32653 64 65 72 5b 69 69 5d 5d 3b 0a 20 20 20 20 6e 6f  der[ii]];.    no
32654 64 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52 74  deInsertCell(pRt
32655 72 65 65 2c 20 70 4e 6f 64 65 2c 20 70 29 3b 0a  ree, pNode, p);.
32656 20 20 20 20 69 66 28 20 70 2d 3e 69 52 6f 77 69      if( p->iRowi
32657 64 3d 3d 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64  d==pCell->iRowid
32658 20 29 7b 0a 20 20 20 20 20 20 69 66 28 20 69 48   ){.      if( iH
32659 65 69 67 68 74 3d 3d 30 20 29 7b 0a 20 20 20 20  eight==0 ){.    
3265a 20 20 20 20 72 63 20 3d 20 72 6f 77 69 64 57 72      rc = rowidWr
3265b 69 74 65 28 70 52 74 72 65 65 2c 20 70 2d 3e 69  ite(pRtree, p->i
3265c 52 6f 77 69 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e  Rowid, pNode->iN
3265d 6f 64 65 29 3b 0a 20 20 20 20 20 20 7d 65 6c 73  ode);.      }els
3265e 65 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20  e{.        rc = 
3265f 70 61 72 65 6e 74 57 72 69 74 65 28 70 52 74 72  parentWrite(pRtr
32660 65 65 2c 20 70 2d 3e 69 52 6f 77 69 64 2c 20 70  ee, p->iRowid, p
32661 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20  Node->iNode);.  
32662 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a      }.    }.  }.
32663 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
32664 5f 4f 4b 20 29 7b 0a 20 20 20 20 66 69 78 42 6f  _OK ){.    fixBo
32665 75 6e 64 69 6e 67 42 6f 78 28 70 52 74 72 65 65  undingBox(pRtree
32666 2c 20 70 4e 6f 64 65 29 3b 0a 20 20 7d 0a 20 20  , pNode);.  }.  
32667 66 6f 72 28 3b 20 72 63 3d 3d 53 51 4c 49 54 45  for(; rc==SQLITE
32668 5f 4f 4b 20 26 26 20 69 69 3c 6e 43 65 6c 6c 3b  _OK && ii<nCell;
32669 20 69 69 2b 2b 29 7b 0a 20 20 20 20 2f 2a 20 46   ii++){.    /* F
3266a 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20 73 74  ind a node to st
3266b 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20 69 6e  ore this cell in
3266c 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63  . pNode->iNode c
3266d 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e  urrently contain
3266e 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 69  s.    ** the hei
3266f 67 68 74 20 6f 66 20 74 68 65 20 73 75 62 2d 74  ght of the sub-t
32670 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
32671 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20  e cell..    */. 
32672 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 49     RtreeNode *pI
32673 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72 65 65  nsert;.    Rtree
32674 43 65 6c 6c 20 2a 70 20 3d 20 26 61 43 65 6c 6c  Cell *p = &aCell
32675 5b 61 4f 72 64 65 72 5b 69 69 5d 5d 3b 0a 20 20  [aOrder[ii]];.  
32676 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65 61    rc = ChooseLea
32677 66 28 70 52 74 72 65 65 2c 20 70 2c 20 69 48 65  f(pRtree, p, iHe
32678 69 67 68 74 2c 20 26 70 49 6e 73 65 72 74 29 3b  ight, &pInsert);
32679 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3267a 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3267b 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 72  int rc2;.      r
3267c 63 20 3d 20 72 74 72 65 65 49 6e 73 65 72 74 43  c = rtreeInsertC
3267d 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 49 6e 73  ell(pRtree, pIns
3267e 65 72 74 2c 20 70 2c 20 69 48 65 69 67 68 74 29  ert, p, iHeight)
3267f 3b 0a 20 20 20 20 20 20 72 63 32 20 3d 20 6e 6f  ;.      rc2 = no
32680 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65 65  deRelease(pRtree
32681 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20  , pInsert);.    
32682 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
32683 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 72  _OK ){.        r
32684 63 20 3d 20 72 63 32 3b 0a 20 20 20 20 20 20 7d  c = rc2;.      }
32685 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 73 71  .    }.  }..  sq
32686 6c 69 74 65 33 5f 66 72 65 65 28 61 43 65 6c 6c  lite3_free(aCell
32687 29 3b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a  );.  return rc;.
32688 7d 0a 0a 2f 2a 0a 2a 2a 20 49 6e 73 65 72 74 20  }../*.** Insert 
32689 63 65 6c 6c 20 70 43 65 6c 6c 20 69 6e 74 6f 20  cell pCell into 
3268a 6e 6f 64 65 20 70 4e 6f 64 65 2e 20 4e 6f 64 65  node pNode. Node
3268b 20 70 4e 6f 64 65 20 69 73 20 74 68 65 20 68 65   pNode is the he
3268c 61 64 20 6f 66 20 61 20 0a 2a 2a 20 73 75 62 74  ad of a .** subt
3268d 72 65 65 20 69 48 65 69 67 68 74 20 68 69 67 68  ree iHeight high
3268e 20 28 6c 65 61 66 20 6e 6f 64 65 73 20 68 61 76   (leaf nodes hav
3268f 65 20 69 48 65 69 67 68 74 3d 3d 30 29 2e 0a 2a  e iHeight==0)..*
32690 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72 74 72  /.static int rtr
32691 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 0a 20 20  eeInsertCell(.  
32692 52 74 72 65 65 20 2a 70 52 74 72 65 65 2c 0a 20  Rtree *pRtree,. 
32693 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f 64   RtreeNode *pNod
32694 65 2c 0a 20 20 52 74 72 65 65 43 65 6c 6c 20 2a  e,.  RtreeCell *
32695 70 43 65 6c 6c 2c 0a 20 20 69 6e 74 20 69 48 65  pCell,.  int iHe
32696 69 67 68 74 0a 29 7b 0a 20 20 69 6e 74 20 72 63  ight.){.  int rc
32697 20 3d 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20   = SQLITE_OK;.  
32698 69 66 28 20 69 48 65 69 67 68 74 3e 30 20 29 7b  if( iHeight>0 ){
32699 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
3269a 70 43 68 69 6c 64 20 3d 20 6e 6f 64 65 48 61 73  pChild = nodeHas
3269b 68 4c 6f 6f 6b 75 70 28 70 52 74 72 65 65 2c 20  hLookup(pRtree, 
3269c 70 43 65 6c 6c 2d 3e 69 52 6f 77 69 64 29 3b 0a  pCell->iRowid);.
3269d 20 20 20 20 69 66 28 20 70 43 68 69 6c 64 20 29      if( pChild )
3269e 7b 0a 20 20 20 20 20 20 6e 6f 64 65 52 65 6c 65  {.      nodeRele
3269f 61 73 65 28 70 52 74 72 65 65 2c 20 70 43 68 69  ase(pRtree, pChi
326a0 6c 64 2d 3e 70 50 61 72 65 6e 74 29 3b 0a 20 20  ld->pParent);.  
326a1 20 20 20 20 6e 6f 64 65 52 65 66 65 72 65 6e 63      nodeReferenc
326a2 65 28 70 4e 6f 64 65 29 3b 0a 20 20 20 20 20 20  e(pNode);.      
326a3 70 43 68 69 6c 64 2d 3e 70 50 61 72 65 6e 74 20  pChild->pParent 
326a4 3d 20 70 4e 6f 64 65 3b 0a 20 20 20 20 7d 0a 20  = pNode;.    }. 
326a5 20 7d 0a 20 20 69 66 28 20 6e 6f 64 65 49 6e 73   }.  if( nodeIns
326a6 65 72 74 43 65 6c 6c 28 70 52 74 72 65 65 2c 20  ertCell(pRtree, 
326a7 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 20 29 7b  pNode, pCell) ){
326a8 0a 23 69 66 20 56 41 52 49 41 4e 54 5f 52 53 54  .#if VARIANT_RST
326a9 41 52 54 52 45 45 5f 52 45 49 4e 53 45 52 54 0a  ARTREE_REINSERT.
326aa 20 20 20 20 69 66 28 20 69 48 65 69 67 68 74 3c      if( iHeight<
326ab 3d 70 52 74 72 65 65 2d 3e 69 52 65 69 6e 73 65  =pRtree->iReinse
326ac 72 74 48 65 69 67 68 74 20 7c 7c 20 70 4e 6f 64  rtHeight || pNod
326ad 65 2d 3e 69 4e 6f 64 65 3d 3d 31 29 7b 0a 20 20  e->iNode==1){.  
326ae 20 20 20 20 72 63 20 3d 20 53 70 6c 69 74 4e 6f      rc = SplitNo
326af 64 65 28 70 52 74 72 65 65 2c 20 70 4e 6f 64 65  de(pRtree, pNode
326b0 2c 20 70 43 65 6c 6c 2c 20 69 48 65 69 67 68 74  , pCell, iHeight
326b1 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
326b2 20 20 20 20 70 52 74 72 65 65 2d 3e 69 52 65 69      pRtree->iRei
326b3 6e 73 65 72 74 48 65 69 67 68 74 20 3d 20 69 48  nsertHeight = iH
326b4 65 69 67 68 74 3b 0a 20 20 20 20 20 20 72 63 20  eight;.      rc 
326b5 3d 20 52 65 69 6e 73 65 72 74 28 70 52 74 72 65  = Reinsert(pRtre
326b6 65 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c  e, pNode, pCell,
326b7 20 69 48 65 69 67 68 74 29 3b 0a 20 20 20 20 7d   iHeight);.    }
326b8 0a 23 65 6c 73 65 0a 20 20 20 20 72 63 20 3d 20  .#else.    rc = 
326b9 53 70 6c 69 74 4e 6f 64 65 28 70 52 74 72 65 65  SplitNode(pRtree
326ba 2c 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 2c 20  , pNode, pCell, 
326bb 69 48 65 69 67 68 74 29 3b 0a 23 65 6e 64 69 66  iHeight);.#endif
326bc 0a 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 41 64  .  }else{.    Ad
326bd 6a 75 73 74 54 72 65 65 28 70 52 74 72 65 65 2c  justTree(pRtree,
326be 20 70 4e 6f 64 65 2c 20 70 43 65 6c 6c 29 3b 0a   pNode, pCell);.
326bf 20 20 20 20 69 66 28 20 69 48 65 69 67 68 74 3d      if( iHeight=
326c0 3d 30 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d  =0 ){.      rc =
326c1 20 72 6f 77 69 64 57 72 69 74 65 28 70 52 74 72   rowidWrite(pRtr
326c2 65 65 2c 20 70 43 65 6c 6c 2d 3e 69 52 6f 77 69  ee, pCell->iRowi
326c3 64 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 29  d, pNode->iNode)
326c4 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
326c5 20 20 20 72 63 20 3d 20 70 61 72 65 6e 74 57 72     rc = parentWr
326c6 69 74 65 28 70 52 74 72 65 65 2c 20 70 43 65 6c  ite(pRtree, pCel
326c7 6c 2d 3e 69 52 6f 77 69 64 2c 20 70 4e 6f 64 65  l->iRowid, pNode
326c8 2d 3e 69 4e 6f 64 65 29 3b 0a 20 20 20 20 7d 0a  ->iNode);.    }.
326c9 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 72 63 3b    }.  return rc;
326ca 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .}..static int r
326cb 65 69 6e 73 65 72 74 4e 6f 64 65 43 6f 6e 74 65  einsertNodeConte
326cc 6e 74 28 52 74 72 65 65 20 2a 70 52 74 72 65 65  nt(Rtree *pRtree
326cd 2c 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4e 6f  , RtreeNode *pNo
326ce 64 65 29 7b 0a 20 20 69 6e 74 20 69 69 3b 0a 20  de){.  int ii;. 
326cf 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
326d0 5f 4f 4b 3b 0a 20 20 69 6e 74 20 6e 43 65 6c 6c  _OK;.  int nCell
326d1 20 3d 20 4e 43 45 4c 4c 28 70 4e 6f 64 65 29 3b   = NCELL(pNode);
326d2 0a 0a 20 20 66 6f 72 28 69 69 3d 30 3b 20 72 63  ..  for(ii=0; rc
326d3 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 26 26 20 69  ==SQLITE_OK && i
326d4 69 3c 6e 43 65 6c 6c 3b 20 69 69 2b 2b 29 7b 0a  i<nCell; ii++){.
326d5 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
326d6 49 6e 73 65 72 74 3b 0a 20 20 20 20 52 74 72 65  Insert;.    Rtre
326d7 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20 20  eCell cell;.    
326d8 6e 6f 64 65 47 65 74 43 65 6c 6c 28 70 52 74 72  nodeGetCell(pRtr
326d9 65 65 2c 20 70 4e 6f 64 65 2c 20 69 69 2c 20 26  ee, pNode, ii, &
326da 63 65 6c 6c 29 3b 0a 0a 20 20 20 20 2f 2a 20 46  cell);..    /* F
326db 69 6e 64 20 61 20 6e 6f 64 65 20 74 6f 20 73 74  ind a node to st
326dc 6f 72 65 20 74 68 69 73 20 63 65 6c 6c 20 69 6e  ore this cell in
326dd 2e 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 20 63  . pNode->iNode c
326de 75 72 72 65 6e 74 6c 79 20 63 6f 6e 74 61 69 6e  urrently contain
326df 73 0a 20 20 20 20 2a 2a 20 74 68 65 20 68 65 69  s.    ** the hei
326e0 67 68 74 20 6f 66 20 74 68 65 20 73 75 62 2d 74  ght of the sub-t
326e1 72 65 65 20 68 65 61 64 65 64 20 62 79 20 74 68  ree headed by th
326e2 65 20 63 65 6c 6c 2e 0a 20 20 20 20 2a 2f 0a 20  e cell..    */. 
326e3 20 20 20 72 63 20 3d 20 43 68 6f 6f 73 65 4c 65     rc = ChooseLe
326e4 61 66 28 70 52 74 72 65 65 2c 20 26 63 65 6c 6c  af(pRtree, &cell
326e5 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f 64 65 2c 20  , pNode->iNode, 
326e6 26 70 49 6e 73 65 72 74 29 3b 0a 20 20 20 20 69  &pInsert);.    i
326e7 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
326e8 20 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63   ){.      int rc
326e9 32 3b 0a 20 20 20 20 20 20 72 63 20 3d 20 72 74  2;.      rc = rt
326ea 72 65 65 49 6e 73 65 72 74 43 65 6c 6c 28 70 52  reeInsertCell(pR
326eb 74 72 65 65 2c 20 70 49 6e 73 65 72 74 2c 20 26  tree, pInsert, &
326ec 63 65 6c 6c 2c 20 70 4e 6f 64 65 2d 3e 69 4e 6f  cell, pNode->iNo
326ed 64 65 29 3b 0a 20 20 20 20 20 20 72 63 32 20 3d  de);.      rc2 =
326ee 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74   nodeRelease(pRt
326ef 72 65 65 2c 20 70 49 6e 73 65 72 74 29 3b 0a 20  ree, pInsert);. 
326f0 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
326f1 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
326f2 20 20 72 63 20 3d 20 72 63 32 3b 0a 20 20 20 20    rc = rc2;.    
326f3 20 20 7d 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20    }.    }.  }.  
326f4 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a  return rc;.}../*
326f5 0a 2a 2a 20 53 65 6c 65 63 74 20 61 20 63 75 72  .** Select a cur
326f6 72 65 6e 74 6c 79 20 75 6e 75 73 65 64 20 72 6f  rently unused ro
326f7 77 69 64 20 66 6f 72 20 61 20 6e 65 77 20 72 2d  wid for a new r-
326f8 74 72 65 65 20 72 65 63 6f 72 64 2e 0a 2a 2f 0a  tree record..*/.
326f9 73 74 61 74 69 63 20 69 6e 74 20 6e 65 77 52 6f  static int newRo
326fa 77 69 64 28 52 74 72 65 65 20 2a 70 52 74 72 65  wid(Rtree *pRtre
326fb 65 2c 20 69 36 34 20 2a 70 69 52 6f 77 69 64 29  e, i64 *piRowid)
326fc 7b 0a 20 20 69 6e 74 20 72 63 3b 0a 20 20 73 71  {.  int rc;.  sq
326fd 6c 69 74 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28  lite3_bind_null(
326fe 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
326ff 77 69 64 2c 20 31 29 3b 0a 20 20 73 71 6c 69 74  wid, 1);.  sqlit
32700 65 33 5f 62 69 6e 64 5f 6e 75 6c 6c 28 70 52 74  e3_bind_null(pRt
32701 72 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64  ree->pWriteRowid
32702 2c 20 32 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , 2);.  sqlite3_
32703 73 74 65 70 28 70 52 74 72 65 65 2d 3e 70 57 72  step(pRtree->pWr
32704 69 74 65 52 6f 77 69 64 29 3b 0a 20 20 72 63 20  iteRowid);.  rc 
32705 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
32706 70 52 74 72 65 65 2d 3e 70 57 72 69 74 65 52 6f  pRtree->pWriteRo
32707 77 69 64 29 3b 0a 20 20 2a 70 69 52 6f 77 69 64  wid);.  *piRowid
32708 20 3d 20 73 71 6c 69 74 65 33 5f 6c 61 73 74 5f   = sqlite3_last_
32709 69 6e 73 65 72 74 5f 72 6f 77 69 64 28 70 52 74  insert_rowid(pRt
3270a 72 65 65 2d 3e 64 62 29 3b 0a 20 20 72 65 74 75  ree->db);.  retu
3270b 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 6e 64 65  rn rc;.}..#ifnde
3270c 66 20 4e 44 45 42 55 47 0a 73 74 61 74 69 63 20  f NDEBUG.static 
3270d 69 6e 74 20 68 61 73 68 49 73 45 6d 70 74 79 28  int hashIsEmpty(
3270e 52 74 72 65 65 20 2a 70 52 74 72 65 65 29 7b 0a  Rtree *pRtree){.
3270f 20 20 69 6e 74 20 69 69 3b 0a 20 20 66 6f 72 28    int ii;.  for(
32710 69 69 3d 30 3b 20 69 69 3c 48 41 53 48 53 49 5a  ii=0; ii<HASHSIZ
32711 45 3b 20 69 69 2b 2b 29 7b 0a 20 20 20 20 61 73  E; ii++){.    as
32712 73 65 72 74 28 20 21 70 52 74 72 65 65 2d 3e 61  sert( !pRtree->a
32713 48 61 73 68 5b 69 69 5d 20 29 3b 0a 20 20 7d 0a  Hash[ii] );.  }.
32714 20 20 72 65 74 75 72 6e 20 31 3b 0a 7d 0a 23 65    return 1;.}.#e
32715 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 54 68 65 20  ndif../*.** The 
32716 78 55 70 64 61 74 65 20 6d 65 74 68 6f 64 20 66  xUpdate method f
32717 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c 65 20  or rtree module 
32718 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e 0a  virtual tables..
32719 2a 2f 0a 69 6e 74 20 72 74 72 65 65 55 70 64 61  */.int rtreeUpda
3271a 74 65 28 0a 20 20 73 71 6c 69 74 65 33 5f 76 74  te(.  sqlite3_vt
3271b 61 62 20 2a 70 56 74 61 62 2c 20 0a 20 20 69 6e  ab *pVtab, .  in
3271c 74 20 6e 44 61 74 61 2c 20 0a 20 20 73 71 6c 69  t nData, .  sqli
3271d 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 7a 44 61  te3_value **azDa
3271e 74 61 2c 20 0a 20 20 73 71 6c 69 74 65 5f 69 6e  ta, .  sqlite_in
3271f 74 36 34 20 2a 70 52 6f 77 69 64 0a 29 7b 0a 20  t64 *pRowid.){. 
32720 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 20 3d   Rtree *pRtree =
32721 20 28 52 74 72 65 65 20 2a 29 70 56 74 61 62 3b   (Rtree *)pVtab;
32722 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
32723 54 45 5f 4f 4b 3b 0a 0a 20 20 72 74 72 65 65 52  TE_OK;..  rtreeR
32724 65 66 65 72 65 6e 63 65 28 70 52 74 72 65 65 29  eference(pRtree)
32725 3b 0a 0a 20 20 61 73 73 65 72 74 28 6e 44 61 74  ;..  assert(nDat
32726 61 3e 3d 31 29 3b 0a 20 20 61 73 73 65 72 74 28  a>=1);.  assert(
32727 68 61 73 68 49 73 45 6d 70 74 79 28 70 52 74 72  hashIsEmpty(pRtr
32728 65 65 29 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 61  ee));..  /* If a
32729 7a 44 61 74 61 5b 30 5d 20 69 73 20 6e 6f 74 20  zData[0] is not 
3272a 61 6e 20 53 51 4c 20 4e 55 4c 4c 20 76 61 6c 75  an SQL NULL valu
3272b 65 2c 20 69 74 20 69 73 20 74 68 65 20 72 6f 77  e, it is the row
3272c 69 64 20 6f 66 20 61 0a 20 20 2a 2a 20 72 65 63  id of a.  ** rec
3272d 6f 72 64 20 74 6f 20 64 65 6c 65 74 65 20 66 72  ord to delete fr
3272e 6f 6d 20 74 68 65 20 72 2d 74 72 65 65 20 74 61  om the r-tree ta
3272f 62 6c 65 2e 20 54 68 65 20 66 6f 6c 6c 6f 77 69  ble. The followi
32730 6e 67 20 62 6c 6f 63 6b 20 64 6f 65 73 0a 20 20  ng block does.  
32731 2a 2a 20 6a 75 73 74 20 74 68 61 74 2e 0a 20 20  ** just that..  
32732 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
32733 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a 44 61  _value_type(azDa
32734 74 61 5b 30 5d 29 21 3d 53 51 4c 49 54 45 5f 4e  ta[0])!=SQLITE_N
32735 55 4c 4c 20 29 7b 0a 20 20 20 20 69 36 34 20 69  ULL ){.    i64 i
32736 44 65 6c 65 74 65 3b 20 20 20 20 20 20 20 20 20  Delete;         
32737 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 72 6f         /* The ro
32738 77 69 64 20 74 6f 20 64 65 6c 65 74 65 20 2a 2f  wid to delete */
32739 0a 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a  .    RtreeNode *
3273a 70 4c 65 61 66 3b 20 20 20 20 20 20 20 20 20 20  pLeaf;          
3273b 20 2f 2a 20 4c 65 61 66 20 6e 6f 64 65 20 63 6f   /* Leaf node co
3273c 6e 74 61 69 6e 69 6e 67 20 72 65 63 6f 72 64 20  ntaining record 
3273d 69 44 65 6c 65 74 65 20 2a 2f 0a 20 20 20 20 69  iDelete */.    i
3273e 6e 74 20 69 43 65 6c 6c 3b 20 20 20 20 20 20 20  nt iCell;       
3273f 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 49 6e             /* In
32740 64 65 78 20 6f 66 20 69 44 65 6c 65 74 65 20 63  dex of iDelete c
32741 65 6c 6c 20 69 6e 20 70 4c 65 61 66 20 2a 2f 0a  ell in pLeaf */.
32742 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70      RtreeNode *p
32743 52 6f 6f 74 3b 0a 0a 20 20 20 20 2f 2a 20 4f 62  Root;..    /* Ob
32744 74 61 69 6e 20 61 20 72 65 66 65 72 65 6e 63 65  tain a reference
32745 20 74 6f 20 74 68 65 20 72 6f 6f 74 20 6e 6f 64   to the root nod
32746 65 20 74 6f 20 69 6e 69 74 69 61 6c 69 73 65 20  e to initialise 
32747 52 74 72 65 65 2e 69 44 65 70 74 68 20 2a 2f 0a  Rtree.iDepth */.
32748 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41 63 71      rc = nodeAcq
32749 75 69 72 65 28 70 52 74 72 65 65 2c 20 31 2c 20  uire(pRtree, 1, 
3274a 30 2c 20 26 70 52 6f 6f 74 29 3b 0a 0a 20 20 20  0, &pRoot);..   
3274b 20 2f 2a 20 4f 62 74 61 69 6e 20 61 20 72 65 66   /* Obtain a ref
3274c 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20 6c 65  erence to the le
3274d 61 66 20 6e 6f 64 65 20 74 68 61 74 20 63 6f 6e  af node that con
3274e 74 61 69 6e 73 20 74 68 65 20 65 6e 74 72 79 20  tains the entry 
3274f 0a 20 20 20 20 2a 2a 20 61 62 6f 75 74 20 74 6f  .    ** about to
32750 20 62 65 20 64 65 6c 65 74 65 64 2e 20 0a 20 20   be deleted. .  
32751 20 20 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d    */.    if( rc=
32752 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
32753 20 20 20 20 69 44 65 6c 65 74 65 20 3d 20 73 71      iDelete = sq
32754 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69 6e 74 36  lite3_value_int6
32755 34 28 61 7a 44 61 74 61 5b 30 5d 29 3b 0a 20 20  4(azData[0]);.  
32756 20 20 20 20 72 63 20 3d 20 66 69 6e 64 4c 65 61      rc = findLea
32757 66 4e 6f 64 65 28 70 52 74 72 65 65 2c 20 69 44  fNode(pRtree, iD
32758 65 6c 65 74 65 2c 20 26 70 4c 65 61 66 29 3b 0a  elete, &pLeaf);.
32759 20 20 20 20 7d 0a 0a 20 20 20 20 2f 2a 20 44 65      }..    /* De
3275a 6c 65 74 65 20 74 68 65 20 63 65 6c 6c 20 69 6e  lete the cell in
3275b 20 71 75 65 73 74 69 6f 6e 20 66 72 6f 6d 20 74   question from t
3275c 68 65 20 6c 65 61 66 20 6e 6f 64 65 2e 20 2a 2f  he leaf node. */
3275d 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c  .    if( rc==SQL
3275e 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
3275f 69 6e 74 20 72 63 32 3b 0a 20 20 20 20 20 20 69  int rc2;.      i
32760 43 65 6c 6c 20 3d 20 6e 6f 64 65 52 6f 77 69 64  Cell = nodeRowid
32761 49 6e 64 65 78 28 70 52 74 72 65 65 2c 20 70 4c  Index(pRtree, pL
32762 65 61 66 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20  eaf, iDelete);. 
32763 20 20 20 20 20 72 63 20 3d 20 64 65 6c 65 74 65       rc = delete
32764 43 65 6c 6c 28 70 52 74 72 65 65 2c 20 70 4c 65  Cell(pRtree, pLe
32765 61 66 2c 20 69 43 65 6c 6c 2c 20 30 29 3b 0a 20  af, iCell, 0);. 
32766 20 20 20 20 20 72 63 32 20 3d 20 6e 6f 64 65 52       rc2 = nodeR
32767 65 6c 65 61 73 65 28 70 52 74 72 65 65 2c 20 70  elease(pRtree, p
32768 4c 65 61 66 29 3b 0a 20 20 20 20 20 20 69 66 28  Leaf);.      if(
32769 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29   rc==SQLITE_OK )
3276a 7b 0a 20 20 20 20 20 20 20 20 72 63 20 3d 20 72  {.        rc = r
3276b 63 32 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  c2;.      }.    
3276c 7d 0a 0a 20 20 20 20 2f 2a 20 44 65 6c 65 74 65  }..    /* Delete
3276d 20 74 68 65 20 63 6f 72 72 65 73 70 6f 6e 64 69   the correspondi
3276e 6e 67 20 65 6e 74 72 79 20 69 6e 20 74 68 65 20  ng entry in the 
3276f 3c 72 74 72 65 65 3e 5f 72 6f 77 69 64 20 74 61  <rtree>_rowid ta
32770 62 6c 65 2e 20 2a 2f 0a 20 20 20 20 69 66 28 20  ble. */.    if( 
32771 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b  rc==SQLITE_OK ){
32772 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
32773 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
32774 2d 3e 70 44 65 6c 65 74 65 52 6f 77 69 64 2c 20  ->pDeleteRowid, 
32775 31 2c 20 69 44 65 6c 65 74 65 29 3b 0a 20 20 20  1, iDelete);.   
32776 20 20 20 73 71 6c 69 74 65 33 5f 73 74 65 70 28     sqlite3_step(
32777 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52  pRtree->pDeleteR
32778 6f 77 69 64 29 3b 0a 20 20 20 20 20 20 72 63 20  owid);.      rc 
32779 3d 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28  = sqlite3_reset(
3277a 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65 52  pRtree->pDeleteR
3277b 6f 77 69 64 29 3b 0a 20 20 20 20 7d 0a 0a 20 20  owid);.    }..  
3277c 20 20 2f 2a 20 43 68 65 63 6b 20 69 66 20 74 68    /* Check if th
3277d 65 20 72 6f 6f 74 20 6e 6f 64 65 20 6e 6f 77 20  e root node now 
3277e 68 61 73 20 65 78 61 63 74 6c 79 20 6f 6e 65 20  has exactly one 
3277f 63 68 69 6c 64 2e 20 49 66 20 73 6f 2c 20 72 65  child. If so, re
32780 6d 6f 76 65 0a 20 20 20 20 2a 2a 20 69 74 2c 20  move.    ** it, 
32781 73 63 68 65 64 75 6c 65 20 74 68 65 20 63 6f 6e  schedule the con
32782 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 68 69  tents of the chi
32783 6c 64 20 66 6f 72 20 72 65 69 6e 73 65 72 74 69  ld for reinserti
32784 6f 6e 20 61 6e 64 20 0a 20 20 20 20 2a 2a 20 72  on and .    ** r
32785 65 64 75 63 65 20 74 68 65 20 74 72 65 65 20 68  educe the tree h
32786 65 69 67 68 74 20 62 79 20 6f 6e 65 2e 0a 20 20  eight by one..  
32787 20 20 2a 2a 0a 20 20 20 20 2a 2a 20 54 68 69 73    **.    ** This
32788 20 69 73 20 65 71 75 69 76 61 6c 65 6e 74 20 74   is equivalent t
32789 6f 20 63 6f 70 79 69 6e 67 20 74 68 65 20 63 6f  o copying the co
3278a 6e 74 65 6e 74 73 20 6f 66 20 74 68 65 20 63 68  ntents of the ch
3278b 69 6c 64 20 69 6e 74 6f 0a 20 20 20 20 2a 2a 20  ild into.    ** 
3278c 74 68 65 20 72 6f 6f 74 20 6e 6f 64 65 20 28 74  the root node (t
3278d 68 65 20 6f 70 65 72 61 74 69 6f 6e 20 74 68 61  he operation tha
3278e 74 20 47 75 74 6d 61 6e 27 73 20 70 61 70 65 72  t Gutman's paper
3278f 20 73 61 79 73 20 74 6f 20 70 65 72 66 6f 72 6d   says to perform
32790 20 0a 20 20 20 20 2a 2a 20 69 6e 20 74 68 69 73   .    ** in this
32791 20 73 63 65 6e 61 72 69 6f 29 2e 0a 20 20 20 20   scenario)..    
32792 2a 2f 0a 20 20 20 20 69 66 28 20 72 63 3d 3d 53  */.    if( rc==S
32793 51 4c 49 54 45 5f 4f 4b 20 26 26 20 70 52 74 72  QLITE_OK && pRtr
32794 65 65 2d 3e 69 44 65 70 74 68 3e 30 20 29 7b 0a  ee->iDepth>0 ){.
32795 20 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51        if( rc==SQ
32796 4c 49 54 45 5f 4f 4b 20 26 26 20 4e 43 45 4c 4c  LITE_OK && NCELL
32797 28 70 52 6f 6f 74 29 3d 3d 31 20 29 7b 0a 20 20  (pRoot)==1 ){.  
32798 20 20 20 20 20 20 52 74 72 65 65 4e 6f 64 65 20        RtreeNode 
32799 2a 70 43 68 69 6c 64 3b 0a 20 20 20 20 20 20 20  *pChild;.       
3279a 20 69 36 34 20 69 43 68 69 6c 64 20 3d 20 6e 6f   i64 iChild = no
3279b 64 65 47 65 74 52 6f 77 69 64 28 70 52 74 72 65  deGetRowid(pRtre
3279c 65 2c 20 70 52 6f 6f 74 2c 20 30 29 3b 0a 20 20  e, pRoot, 0);.  
3279d 20 20 20 20 20 20 72 63 20 3d 20 6e 6f 64 65 41        rc = nodeA
3279e 63 71 75 69 72 65 28 70 52 74 72 65 65 2c 20 69  cquire(pRtree, i
3279f 43 68 69 6c 64 2c 20 70 52 6f 6f 74 2c 20 26 70  Child, pRoot, &p
327a0 43 68 69 6c 64 29 3b 0a 20 20 20 20 20 20 20 20  Child);.        
327a1 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
327a2 4b 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72  K ){.          r
327a3 63 20 3d 20 72 65 6d 6f 76 65 4e 6f 64 65 28 70  c = removeNode(p
327a4 52 74 72 65 65 2c 20 70 43 68 69 6c 64 2c 20 70  Rtree, pChild, p
327a5 52 74 72 65 65 2d 3e 69 44 65 70 74 68 2d 31 29  Rtree->iDepth-1)
327a6 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
327a7 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49      if( rc==SQLI
327a8 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 20  TE_OK ){.       
327a9 20 20 20 70 52 74 72 65 65 2d 3e 69 44 65 70 74     pRtree->iDept
327aa 68 2d 2d 3b 0a 20 20 20 20 20 20 20 20 20 20 77  h--;.          w
327ab 72 69 74 65 49 6e 74 31 36 28 70 52 6f 6f 74 2d  riteInt16(pRoot-
327ac 3e 7a 44 61 74 61 2c 20 70 52 74 72 65 65 2d 3e  >zData, pRtree->
327ad 69 44 65 70 74 68 29 3b 0a 20 20 20 20 20 20 20  iDepth);.       
327ae 20 20 20 70 52 6f 6f 74 2d 3e 69 73 44 69 72 74     pRoot->isDirt
327af 79 20 3d 20 31 3b 0a 20 20 20 20 20 20 20 20 7d  y = 1;.        }
327b0 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a  .      }.    }..
327b1 20 20 20 20 2f 2a 20 52 65 2d 69 6e 73 65 72 74      /* Re-insert
327b2 20 74 68 65 20 63 6f 6e 74 65 6e 74 73 20 6f 66   the contents of
327b3 20 61 6e 79 20 75 6e 64 65 72 66 75 6c 6c 20 6e   any underfull n
327b4 6f 64 65 73 20 72 65 6d 6f 76 65 64 20 66 72 6f  odes removed fro
327b5 6d 20 74 68 65 20 74 72 65 65 2e 20 2a 2f 0a 20  m the tree. */. 
327b6 20 20 20 66 6f 72 28 70 4c 65 61 66 3d 70 52 74     for(pLeaf=pRt
327b7 72 65 65 2d 3e 70 44 65 6c 65 74 65 64 3b 20 70  ree->pDeleted; p
327b8 4c 65 61 66 3b 20 70 4c 65 61 66 3d 70 52 74 72  Leaf; pLeaf=pRtr
327b9 65 65 2d 3e 70 44 65 6c 65 74 65 64 29 7b 0a 20  ee->pDeleted){. 
327ba 20 20 20 20 20 69 66 28 20 72 63 3d 3d 53 51 4c       if( rc==SQL
327bb 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20  ITE_OK ){.      
327bc 20 20 72 63 20 3d 20 72 65 69 6e 73 65 72 74 4e    rc = reinsertN
327bd 6f 64 65 43 6f 6e 74 65 6e 74 28 70 52 74 72 65  odeContent(pRtre
327be 65 2c 20 70 4c 65 61 66 29 3b 0a 20 20 20 20 20  e, pLeaf);.     
327bf 20 7d 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d   }.      pRtree-
327c0 3e 70 44 65 6c 65 74 65 64 20 3d 20 70 4c 65 61  >pDeleted = pLea
327c1 66 2d 3e 70 4e 65 78 74 3b 0a 20 20 20 20 20 20  f->pNext;.      
327c2 73 71 6c 69 74 65 33 5f 66 72 65 65 28 70 4c 65  sqlite3_free(pLe
327c3 61 66 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20  af);.    }..    
327c4 2f 2a 20 52 65 6c 65 61 73 65 20 74 68 65 20 72  /* Release the r
327c5 65 66 65 72 65 6e 63 65 20 74 6f 20 74 68 65 20  eference to the 
327c6 72 6f 6f 74 20 6e 6f 64 65 2e 20 2a 2f 0a 20 20  root node. */.  
327c7 20 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45    if( rc==SQLITE
327c8 5f 4f 4b 20 29 7b 0a 20 20 20 20 20 20 72 63 20  _OK ){.      rc 
327c9 3d 20 6e 6f 64 65 52 65 6c 65 61 73 65 28 70 52  = nodeRelease(pR
327ca 74 72 65 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20  tree, pRoot);.  
327cb 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20 6e    }else{.      n
327cc 6f 64 65 52 65 6c 65 61 73 65 28 70 52 74 72 65  odeRelease(pRtre
327cd 65 2c 20 70 52 6f 6f 74 29 3b 0a 20 20 20 20 7d  e, pRoot);.    }
327ce 0a 20 20 7d 0a 0a 20 20 2f 2a 20 49 66 20 74 68  .  }..  /* If th
327cf 65 20 61 7a 44 61 74 61 5b 5d 20 61 72 72 61 79  e azData[] array
327d0 20 63 6f 6e 74 61 69 6e 73 20 6d 6f 72 65 20 74   contains more t
327d1 68 61 6e 20 6f 6e 65 20 65 6c 65 6d 65 6e 74 2c  han one element,
327d2 20 65 6c 65 6d 65 6e 74 73 0a 20 20 2a 2a 20 28   elements.  ** (
327d3 61 7a 44 61 74 61 5b 32 5d 2e 2e 61 7a 44 61 74  azData[2]..azDat
327d4 61 5b 61 72 67 63 2d 31 5d 29 20 63 6f 6e 74 61  a[argc-1]) conta
327d5 69 6e 20 61 20 6e 65 77 20 72 65 63 6f 72 64 20  in a new record 
327d6 74 6f 20 69 6e 73 65 72 74 20 69 6e 74 6f 0a 20  to insert into. 
327d7 20 2a 2a 20 74 68 65 20 72 2d 74 72 65 65 20 73   ** the r-tree s
327d8 74 72 75 63 74 75 72 65 2e 0a 20 20 2a 2f 0a 20  tructure..  */. 
327d9 20 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f   if( rc==SQLITE_
327da 4f 4b 20 26 26 20 6e 44 61 74 61 3e 31 20 29 7b  OK && nData>1 ){
327db 0a 20 20 20 20 2f 2a 20 49 6e 73 65 72 74 20 61  .    /* Insert a
327dc 20 6e 65 77 20 72 65 63 6f 72 64 20 69 6e 74 6f   new record into
327dd 20 74 68 65 20 72 2d 74 72 65 65 20 2a 2f 0a 20   the r-tree */. 
327de 20 20 20 52 74 72 65 65 43 65 6c 6c 20 63 65 6c     RtreeCell cel
327df 6c 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20  l;.    int ii;. 
327e0 20 20 20 52 74 72 65 65 4e 6f 64 65 20 2a 70 4c     RtreeNode *pL
327e1 65 61 66 3b 0a 0a 20 20 20 20 2f 2a 20 50 6f 70  eaf;..    /* Pop
327e2 75 6c 61 74 65 20 74 68 65 20 63 65 6c 6c 2e 61  ulate the cell.a
327e3 43 6f 6f 72 64 5b 5d 20 61 72 72 61 79 2e 20 54  Coord[] array. T
327e4 68 65 20 66 69 72 73 74 20 63 6f 6f 72 64 69 6e  he first coordin
327e5 61 74 65 20 69 73 20 61 7a 44 61 74 61 5b 33 5d  ate is azData[3]
327e6 2e 20 2a 2f 0a 20 20 20 20 61 73 73 65 72 74 28  . */.    assert(
327e7 20 6e 44 61 74 61 3d 3d 28 70 52 74 72 65 65 2d   nData==(pRtree-
327e8 3e 6e 44 69 6d 2a 32 20 2b 20 33 29 20 29 3b 0a  >nDim*2 + 3) );.
327e9 20 20 20 20 69 66 28 20 70 52 74 72 65 65 2d 3e      if( pRtree->
327ea 65 43 6f 6f 72 64 54 79 70 65 3d 3d 52 54 52 45  eCoordType==RTRE
327eb 45 5f 43 4f 4f 52 44 5f 52 45 41 4c 33 32 20 29  E_COORD_REAL32 )
327ec 7b 0a 20 20 20 20 20 20 66 6f 72 28 69 69 3d 30  {.      for(ii=0
327ed 3b 20 69 69 3c 28 70 52 74 72 65 65 2d 3e 6e 44  ; ii<(pRtree->nD
327ee 69 6d 2a 32 29 3b 20 69 69 2b 3d 32 29 7b 0a 20  im*2); ii+=2){. 
327ef 20 20 20 20 20 20 20 63 65 6c 6c 2e 61 43 6f 6f         cell.aCoo
327f0 72 64 5b 69 69 5d 2e 66 20 3d 20 28 66 6c 6f 61  rd[ii].f = (floa
327f1 74 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  t)sqlite3_value_
327f2 64 6f 75 62 6c 65 28 61 7a 44 61 74 61 5b 69 69  double(azData[ii
327f3 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20 63 65  +3]);.        ce
327f4 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e  ll.aCoord[ii+1].
327f5 66 20 3d 20 28 66 6c 6f 61 74 29 73 71 6c 69 74  f = (float)sqlit
327f6 65 33 5f 76 61 6c 75 65 5f 64 6f 75 62 6c 65 28  e3_value_double(
327f7 61 7a 44 61 74 61 5b 69 69 2b 34 5d 29 3b 0a 20  azData[ii+4]);. 
327f8 20 20 20 20 20 20 20 69 66 28 20 63 65 6c 6c 2e         if( cell.
327f9 61 43 6f 6f 72 64 5b 69 69 5d 2e 66 3e 63 65 6c  aCoord[ii].f>cel
327fa 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31 5d 2e 66  l.aCoord[ii+1].f
327fb 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20 72 63   ){.          rc
327fc 20 3d 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52   = SQLITE_CONSTR
327fd 41 49 4e 54 3b 0a 20 20 20 20 20 20 20 20 20 20  AINT;.          
327fe 67 6f 74 6f 20 63 6f 6e 73 74 72 61 69 6e 74 3b  goto constraint;
327ff 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
32800 20 7d 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20   }.    }else{.  
32801 20 20 20 20 66 6f 72 28 69 69 3d 30 3b 20 69 69      for(ii=0; ii
32802 3c 28 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 32  <(pRtree->nDim*2
32803 29 3b 20 69 69 2b 3d 32 29 7b 0a 20 20 20 20 20  ); ii+=2){.     
32804 20 20 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69     cell.aCoord[i
32805 69 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f 76  i].i = sqlite3_v
32806 61 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61 5b  alue_int(azData[
32807 69 69 2b 33 5d 29 3b 0a 20 20 20 20 20 20 20 20  ii+3]);.        
32808 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69 69 2b 31  cell.aCoord[ii+1
32809 5d 2e 69 20 3d 20 73 71 6c 69 74 65 33 5f 76 61  ].i = sqlite3_va
3280a 6c 75 65 5f 69 6e 74 28 61 7a 44 61 74 61 5b 69  lue_int(azData[i
3280b 69 2b 34 5d 29 3b 0a 20 20 20 20 20 20 20 20 69  i+4]);.        i
3280c 66 28 20 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 69  f( cell.aCoord[i
3280d 69 5d 2e 69 3e 63 65 6c 6c 2e 61 43 6f 6f 72 64  i].i>cell.aCoord
3280e 5b 69 69 2b 31 5d 2e 69 20 29 7b 0a 20 20 20 20  [ii+1].i ){.    
3280f 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49 54        rc = SQLIT
32810 45 5f 43 4f 4e 53 54 52 41 49 4e 54 3b 0a 20 20  E_CONSTRAINT;.  
32811 20 20 20 20 20 20 20 20 67 6f 74 6f 20 63 6f 6e          goto con
32812 73 74 72 61 69 6e 74 3b 0a 20 20 20 20 20 20 20  straint;.       
32813 20 7d 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 7d   }.      }.    }
32814 0a 0a 20 20 20 20 2f 2a 20 46 69 67 75 72 65 20  ..    /* Figure 
32815 6f 75 74 20 74 68 65 20 72 6f 77 69 64 20 6f 66  out the rowid of
32816 20 74 68 65 20 6e 65 77 20 72 6f 77 2e 20 2a 2f   the new row. */
32817 0a 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33  .    if( sqlite3
32818 5f 76 61 6c 75 65 5f 74 79 70 65 28 61 7a 44 61  _value_type(azDa
32819 74 61 5b 32 5d 29 3d 3d 53 51 4c 49 54 45 5f 4e  ta[2])==SQLITE_N
3281a 55 4c 4c 20 29 7b 0a 20 20 20 20 20 20 72 63 20  ULL ){.      rc 
3281b 3d 20 6e 65 77 52 6f 77 69 64 28 70 52 74 72 65  = newRowid(pRtre
3281c 65 2c 20 26 63 65 6c 6c 2e 69 52 6f 77 69 64 29  e, &cell.iRowid)
3281d 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20  ;.    }else{.   
3281e 20 20 20 63 65 6c 6c 2e 69 52 6f 77 69 64 20 3d     cell.iRowid =
3281f 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 69   sqlite3_value_i
32820 6e 74 36 34 28 61 7a 44 61 74 61 5b 32 5d 29 3b  nt64(azData[2]);
32821 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33 5f 62  .      sqlite3_b
32822 69 6e 64 5f 69 6e 74 36 34 28 70 52 74 72 65 65  ind_int64(pRtree
32823 2d 3e 70 52 65 61 64 52 6f 77 69 64 2c 20 31 2c  ->pReadRowid, 1,
32824 20 63 65 6c 6c 2e 69 52 6f 77 69 64 29 3b 0a 20   cell.iRowid);. 
32825 20 20 20 20 20 69 66 28 20 53 51 4c 49 54 45 5f       if( SQLITE_
32826 52 4f 57 3d 3d 73 71 6c 69 74 65 33 5f 73 74 65  ROW==sqlite3_ste
32827 70 28 70 52 74 72 65 65 2d 3e 70 52 65 61 64 52  p(pRtree->pReadR
32828 6f 77 69 64 29 20 29 7b 0a 20 20 20 20 20 20 20  owid) ){.       
32829 20 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70   sqlite3_reset(p
3282a 52 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69  Rtree->pReadRowi
3282b 64 29 3b 0a 20 20 20 20 20 20 20 20 72 63 20 3d  d);.        rc =
3282c 20 53 51 4c 49 54 45 5f 43 4f 4e 53 54 52 41 49   SQLITE_CONSTRAI
3282d 4e 54 3b 0a 20 20 20 20 20 20 20 20 67 6f 74 6f  NT;.        goto
3282e 20 63 6f 6e 73 74 72 61 69 6e 74 3b 0a 20 20 20   constraint;.   
3282f 20 20 20 7d 0a 20 20 20 20 20 20 72 63 20 3d 20     }.      rc = 
32830 73 71 6c 69 74 65 33 5f 72 65 73 65 74 28 70 52  sqlite3_reset(pR
32831 74 72 65 65 2d 3e 70 52 65 61 64 52 6f 77 69 64  tree->pReadRowid
32832 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
32833 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
32834 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20 43 68  ){.      rc = Ch
32835 6f 6f 73 65 4c 65 61 66 28 70 52 74 72 65 65 2c  ooseLeaf(pRtree,
32836 20 26 63 65 6c 6c 2c 20 30 2c 20 26 70 4c 65 61   &cell, 0, &pLea
32837 66 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 69 66  f);.    }.    if
32838 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc==SQLITE_OK 
32839 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 72 63 32  ){.      int rc2
3283a 3b 0a 20 20 20 20 20 20 70 52 74 72 65 65 2d 3e  ;.      pRtree->
3283b 69 52 65 69 6e 73 65 72 74 48 65 69 67 68 74 20  iReinsertHeight 
3283c 3d 20 2d 31 3b 0a 20 20 20 20 20 20 72 63 20 3d  = -1;.      rc =
3283d 20 72 74 72 65 65 49 6e 73 65 72 74 43 65 6c 6c   rtreeInsertCell
3283e 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66 2c 20  (pRtree, pLeaf, 
3283f 26 63 65 6c 6c 2c 20 30 29 3b 0a 20 20 20 20 20  &cell, 0);.     
32840 20 72 63 32 20 3d 20 6e 6f 64 65 52 65 6c 65 61   rc2 = nodeRelea
32841 73 65 28 70 52 74 72 65 65 2c 20 70 4c 65 61 66  se(pRtree, pLeaf
32842 29 3b 0a 20 20 20 20 20 20 69 66 28 20 72 63 3d  );.      if( rc=
32843 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
32844 20 20 20 20 20 20 72 63 20 3d 20 72 63 32 3b 0a        rc = rc2;.
32845 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20        }.    }.  
32846 7d 0a 0a 63 6f 6e 73 74 72 61 69 6e 74 3a 0a 20  }..constraint:. 
32847 20 72 74 72 65 65 52 65 6c 65 61 73 65 28 70 52   rtreeRelease(pR
32848 74 72 65 65 29 3b 0a 20 20 72 65 74 75 72 6e 20  tree);.  return 
32849 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54 68 65  rc;.}../*.** The
3284a 20 78 52 65 6e 61 6d 65 20 6d 65 74 68 6f 64 20   xRename method 
3284b 66 6f 72 20 72 74 72 65 65 20 6d 6f 64 75 6c 65  for rtree module
3284c 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 73 2e   virtual tables.
3284d 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 72  .*/.static int r
3284e 74 72 65 65 52 65 6e 61 6d 65 28 73 71 6c 69 74  treeRename(sqlit
3284f 65 33 5f 76 74 61 62 20 2a 70 56 74 61 62 2c 20  e3_vtab *pVtab, 
32850 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 65 77  const char *zNew
32851 4e 61 6d 65 29 7b 0a 20 20 52 74 72 65 65 20 2a  Name){.  Rtree *
32852 70 52 74 72 65 65 20 3d 20 28 52 74 72 65 65 20  pRtree = (Rtree 
32853 2a 29 70 56 74 61 62 3b 0a 20 20 69 6e 74 20 72  *)pVtab;.  int r
32854 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d  c = SQLITE_NOMEM
32855 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71 6c 20 3d  ;.  char *zSql =
32856 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66   sqlite3_mprintf
32857 28 0a 20 20 20 20 22 41 4c 54 45 52 20 54 41 42  (.    "ALTER TAB
32858 4c 45 20 25 51 2e 27 25 71 5f 6e 6f 64 65 27 20  LE %Q.'%q_node' 
32859 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22 25 77    RENAME TO \"%w
3285a 5f 6e 6f 64 65 5c 22 3b 22 0a 20 20 20 20 22 41  _node\";".    "A
3285b 4c 54 45 52 20 54 41 42 4c 45 20 25 51 2e 27 25  LTER TABLE %Q.'%
3285c 71 5f 70 61 72 65 6e 74 27 20 52 45 4e 41 4d 45  q_parent' RENAME
3285d 20 54 4f 20 5c 22 25 77 5f 70 61 72 65 6e 74 5c   TO \"%w_parent\
3285e 22 3b 22 0a 20 20 20 20 22 41 4c 54 45 52 20 54  ";".    "ALTER T
3285f 41 42 4c 45 20 25 51 2e 27 25 71 5f 72 6f 77 69  ABLE %Q.'%q_rowi
32860 64 27 20 20 52 45 4e 41 4d 45 20 54 4f 20 5c 22  d'  RENAME TO \"
32861 25 77 5f 72 6f 77 69 64 5c 22 3b 22 0a 20 20 20  %w_rowid\";".   
32862 20 2c 20 70 52 74 72 65 65 2d 3e 7a 44 62 2c 20   , pRtree->zDb, 
32863 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a  pRtree->zName, z
32864 4e 65 77 4e 61 6d 65 20 0a 20 20 20 20 2c 20 70  NewName .    , p
32865 52 74 72 65 65 2d 3e 7a 44 62 2c 20 70 52 74 72  Rtree->zDb, pRtr
32866 65 65 2d 3e 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e  ee->zName, zNewN
32867 61 6d 65 20 0a 20 20 20 20 2c 20 70 52 74 72 65  ame .    , pRtre
32868 65 2d 3e 7a 44 62 2c 20 70 52 74 72 65 65 2d 3e  e->zDb, pRtree->
32869 7a 4e 61 6d 65 2c 20 7a 4e 65 77 4e 61 6d 65 0a  zName, zNewName.
3286a 20 20 29 3b 0a 20 20 69 66 28 20 7a 53 71 6c 20    );.  if( zSql 
3286b 29 7b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ){.    rc = sqli
3286c 74 65 33 5f 65 78 65 63 28 70 52 74 72 65 65 2d  te3_exec(pRtree-
3286d 3e 64 62 2c 20 7a 53 71 6c 2c 20 30 2c 20 30 2c  >db, zSql, 0, 0,
3286e 20 30 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33   0);.    sqlite3
3286f 5f 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 7d  _free(zSql);.  }
32870 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
32871 0a 73 74 61 74 69 63 20 73 71 6c 69 74 65 33 5f  .static sqlite3_
32872 6d 6f 64 75 6c 65 20 72 74 72 65 65 4d 6f 64 75  module rtreeModu
32873 6c 65 20 3d 20 7b 0a 20 20 30 2c 20 20 20 20 20  le = {.  0,     
32874 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32875 20 20 20 20 2f 2a 20 69 56 65 72 73 69 6f 6e 20      /* iVersion 
32876 2a 2f 0a 20 20 72 74 72 65 65 43 72 65 61 74 65  */.  rtreeCreate
32877 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32878 20 2f 2a 20 78 43 72 65 61 74 65 20 2d 20 63 72   /* xCreate - cr
32879 65 61 74 65 20 61 20 74 61 62 6c 65 20 2a 2f 0a  eate a table */.
3287a 20 20 72 74 72 65 65 43 6f 6e 6e 65 63 74 2c 20    rtreeConnect, 
3287b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
3287c 20 78 43 6f 6e 6e 65 63 74 20 2d 20 63 6f 6e 6e   xConnect - conn
3287d 65 63 74 20 74 6f 20 61 6e 20 65 78 69 73 74 69  ect to an existi
3287e 6e 67 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72 74  ng table */.  rt
3287f 72 65 65 42 65 73 74 49 6e 64 65 78 2c 20 20 20  reeBestIndex,   
32880 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65            /* xBe
32881 73 74 49 6e 64 65 78 20 2d 20 44 65 74 65 72 6d  stIndex - Determ
32882 69 6e 65 20 73 65 61 72 63 68 20 73 74 72 61 74  ine search strat
32883 65 67 79 20 2a 2f 0a 20 20 72 74 72 65 65 44 69  egy */.  rtreeDi
32884 73 63 6f 6e 6e 65 63 74 2c 20 20 20 20 20 20 20  sconnect,       
32885 20 20 20 20 20 2f 2a 20 78 44 69 73 63 6f 6e 6e       /* xDisconn
32886 65 63 74 20 2d 20 44 69 73 63 6f 6e 6e 65 63 74  ect - Disconnect
32887 20 66 72 6f 6d 20 61 20 74 61 62 6c 65 20 2a 2f   from a table */
32888 0a 20 20 72 74 72 65 65 44 65 73 74 72 6f 79 2c  .  rtreeDestroy,
32889 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3288a 2a 20 78 44 65 73 74 72 6f 79 20 2d 20 44 72 6f  * xDestroy - Dro
3288b 70 20 61 20 74 61 62 6c 65 20 2a 2f 0a 20 20 72  p a table */.  r
3288c 74 72 65 65 4f 70 65 6e 2c 20 20 20 20 20 20 20  treeOpen,       
3288d 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4f             /* xO
3288e 70 65 6e 20 2d 20 6f 70 65 6e 20 61 20 63 75 72  pen - open a cur
3288f 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65 43 6c  sor */.  rtreeCl
32890 6f 73 65 2c 20 20 20 20 20 20 20 20 20 20 20 20  ose,            
32891 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20 2d       /* xClose -
32892 20 63 6c 6f 73 65 20 61 20 63 75 72 73 6f 72 20   close a cursor 
32893 2a 2f 0a 20 20 72 74 72 65 65 46 69 6c 74 65 72  */.  rtreeFilter
32894 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32895 20 2f 2a 20 78 46 69 6c 74 65 72 20 2d 20 63 6f   /* xFilter - co
32896 6e 66 69 67 75 72 65 20 73 63 61 6e 20 63 6f 6e  nfigure scan con
32897 73 74 72 61 69 6e 74 73 20 2a 2f 0a 20 20 72 74  straints */.  rt
32898 72 65 65 4e 65 78 74 2c 20 20 20 20 20 20 20 20  reeNext,        
32899 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 4e 65            /* xNe
3289a 78 74 20 2d 20 61 64 76 61 6e 63 65 20 61 20 63  xt - advance a c
3289b 75 72 73 6f 72 20 2a 2f 0a 20 20 72 74 72 65 65  ursor */.  rtree
3289c 45 6f 66 2c 20 20 20 20 20 20 20 20 20 20 20 20  Eof,            
3289d 20 20 20 20 20 20 20 2f 2a 20 78 45 6f 66 20 2a         /* xEof *
3289e 2f 0a 20 20 72 74 72 65 65 43 6f 6c 75 6d 6e 2c  /.  rtreeColumn,
3289f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328a0 2f 2a 20 78 43 6f 6c 75 6d 6e 20 2d 20 72 65 61  /* xColumn - rea
328a1 64 20 64 61 74 61 20 2a 2f 0a 20 20 72 74 72 65  d data */.  rtre
328a2 65 52 6f 77 69 64 2c 20 20 20 20 20 20 20 20 20  eRowid,         
328a3 20 20 20 20 20 20 20 20 2f 2a 20 78 52 6f 77 69          /* xRowi
328a4 64 20 2d 20 72 65 61 64 20 64 61 74 61 20 2a 2f  d - read data */
328a5 0a 20 20 72 74 72 65 65 55 70 64 61 74 65 2c 20  .  rtreeUpdate, 
328a6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
328a7 2a 20 78 55 70 64 61 74 65 20 2d 20 77 72 69 74  * xUpdate - writ
328a8 65 20 64 61 74 61 20 2a 2f 0a 20 20 30 2c 20 20  e data */.  0,  
328a9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328aa 20 20 20 20 20 20 20 20 2f 2a 20 78 42 65 67 69          /* xBegi
328ab 6e 20 2d 20 62 65 67 69 6e 20 74 72 61 6e 73 61  n - begin transa
328ac 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20  ction */.  0,   
328ad 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328ae 20 20 20 20 20 20 20 2f 2a 20 78 53 79 6e 63 20         /* xSync 
328af 2d 20 73 79 6e 63 20 74 72 61 6e 73 61 63 74 69  - sync transacti
328b0 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20 20  on */.  0,      
328b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328b2 20 20 20 20 2f 2a 20 78 43 6f 6d 6d 69 74 20 2d      /* xCommit -
328b3 20 63 6f 6d 6d 69 74 20 74 72 61 6e 73 61 63 74   commit transact
328b4 69 6f 6e 20 2a 2f 0a 20 20 30 2c 20 20 20 20 20  ion */.  0,     
328b5 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328b6 20 20 20 20 20 2f 2a 20 78 52 6f 6c 6c 62 61 63       /* xRollbac
328b7 6b 20 2d 20 72 6f 6c 6c 62 61 63 6b 20 74 72 61  k - rollback tra
328b8 6e 73 61 63 74 69 6f 6e 20 2a 2f 0a 20 20 30 2c  nsaction */.  0,
328b9 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
328ba 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 46 69            /* xFi
328bb 6e 64 46 75 6e 63 74 69 6f 6e 20 2d 20 66 75 6e  ndFunction - fun
328bc 63 74 69 6f 6e 20 6f 76 65 72 6c 6f 61 64 69 6e  ction overloadin
328bd 67 20 2a 2f 0a 20 20 72 74 72 65 65 52 65 6e 61  g */.  rtreeRena
328be 6d 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20  me              
328bf 20 20 20 2f 2a 20 78 52 65 6e 61 6d 65 20 2d 20     /* xRename - 
328c0 72 65 6e 61 6d 65 20 74 68 65 20 74 61 62 6c 65  rename the table
328c1 20 2a 2f 0a 7d 3b 0a 0a 73 74 61 74 69 63 20 69   */.};..static i
328c2 6e 74 20 72 74 72 65 65 53 71 6c 49 6e 69 74 28  nt rtreeSqlInit(
328c3 0a 20 20 52 74 72 65 65 20 2a 70 52 74 72 65 65  .  Rtree *pRtree
328c4 2c 20 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  , .  sqlite3 *db
328c5 2c 20 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  , .  const char 
328c6 2a 7a 44 62 2c 20 0a 20 20 63 6f 6e 73 74 20 63  *zDb, .  const c
328c7 68 61 72 20 2a 7a 50 72 65 66 69 78 2c 20 0a 20  har *zPrefix, . 
328c8 20 69 6e 74 20 69 73 43 72 65 61 74 65 0a 29 7b   int isCreate.){
328c9 0a 20 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49  .  int rc = SQLI
328ca 54 45 5f 4f 4b 3b 0a 0a 20 20 23 64 65 66 69 6e  TE_OK;..  #defin
328cb 65 20 4e 5f 53 54 41 54 45 4d 45 4e 54 20 39 0a  e N_STATEMENT 9.
328cc 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63    static const c
328cd 68 61 72 20 2a 61 7a 53 71 6c 5b 4e 5f 53 54 41  har *azSql[N_STA
328ce 54 45 4d 45 4e 54 5d 20 3d 20 7b 0a 20 20 20 20  TEMENT] = {.    
328cf 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
328d0 65 20 74 68 65 20 78 78 78 5f 6e 6f 64 65 20 74  e the xxx_node t
328d1 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53 45 4c  able */.    "SEL
328d2 45 43 54 20 64 61 74 61 20 46 52 4f 4d 20 27 25  ECT data FROM '%
328d3 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45  q'.'%q_node' WHE
328d4 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c  RE nodeno = :1",
328d5 0a 20 20 20 20 22 49 4e 53 45 52 54 20 4f 52 20  .    "INSERT OR 
328d6 52 45 50 4c 41 43 45 20 49 4e 54 4f 20 27 25 71  REPLACE INTO '%q
328d7 27 2e 27 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55  '.'%q_node' VALU
328d8 45 53 28 3a 31 2c 20 3a 32 29 22 2c 0a 20 20 20  ES(:1, :2)",.   
328d9 20 22 44 45 4c 45 54 45 20 46 52 4f 4d 20 27 25   "DELETE FROM '%
328da 71 27 2e 27 25 71 5f 6e 6f 64 65 27 20 57 48 45  q'.'%q_node' WHE
328db 52 45 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 2c  RE nodeno = :1",
328dc 0a 0a 20 20 20 20 2f 2a 20 52 65 61 64 20 61 6e  ..    /* Read an
328dd 64 20 77 72 69 74 65 20 74 68 65 20 78 78 78 5f  d write the xxx_
328de 72 6f 77 69 64 20 74 61 62 6c 65 20 2a 2f 0a 20  rowid table */. 
328df 20 20 20 22 53 45 4c 45 43 54 20 6e 6f 64 65 6e     "SELECT noden
328e0 6f 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f  o FROM '%q'.'%q_
328e1 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f 77  rowid' WHERE row
328e2 69 64 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22 49  id = :1",.    "I
328e3 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43 45  NSERT OR REPLACE
328e4 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f 72   INTO '%q'.'%q_r
328e5 6f 77 69 64 27 20 56 41 4c 55 45 53 28 3a 31 2c  owid' VALUES(:1,
328e6 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45 4c 45   :2)",.    "DELE
328e7 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71  TE FROM '%q'.'%q
328e8 5f 72 6f 77 69 64 27 20 57 48 45 52 45 20 72 6f  _rowid' WHERE ro
328e9 77 69 64 20 3d 20 3a 31 22 2c 0a 0a 20 20 20 20  wid = :1",..    
328ea 2f 2a 20 52 65 61 64 20 61 6e 64 20 77 72 69 74  /* Read and writ
328eb 65 20 74 68 65 20 78 78 78 5f 70 61 72 65 6e 74  e the xxx_parent
328ec 20 74 61 62 6c 65 20 2a 2f 0a 20 20 20 20 22 53   table */.    "S
328ed 45 4c 45 43 54 20 70 61 72 65 6e 74 6e 6f 64 65  ELECT parentnode
328ee 20 46 52 4f 4d 20 27 25 71 27 2e 27 25 71 5f 70   FROM '%q'.'%q_p
328ef 61 72 65 6e 74 27 20 57 48 45 52 45 20 6e 6f 64  arent' WHERE nod
328f0 65 6e 6f 20 3d 20 3a 31 22 2c 0a 20 20 20 20 22  eno = :1",.    "
328f1 49 4e 53 45 52 54 20 4f 52 20 52 45 50 4c 41 43  INSERT OR REPLAC
328f2 45 20 49 4e 54 4f 20 27 25 71 27 2e 27 25 71 5f  E INTO '%q'.'%q_
328f3 70 61 72 65 6e 74 27 20 56 41 4c 55 45 53 28 3a  parent' VALUES(:
328f4 31 2c 20 3a 32 29 22 2c 0a 20 20 20 20 22 44 45  1, :2)",.    "DE
328f5 4c 45 54 45 20 46 52 4f 4d 20 27 25 71 27 2e 27  LETE FROM '%q'.'
328f6 25 71 5f 70 61 72 65 6e 74 27 20 57 48 45 52 45  %q_parent' WHERE
328f7 20 6e 6f 64 65 6e 6f 20 3d 20 3a 31 22 0a 20 20   nodeno = :1".  
328f8 7d 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  };.  sqlite3_stm
328f9 74 20 2a 2a 61 70 70 53 74 6d 74 5b 4e 5f 53 54  t **appStmt[N_ST
328fa 41 54 45 4d 45 4e 54 5d 3b 0a 20 20 69 6e 74 20  ATEMENT];.  int 
328fb 69 3b 0a 0a 20 20 70 52 74 72 65 65 2d 3e 64 62  i;..  pRtree->db
328fc 20 3d 20 64 62 3b 0a 0a 20 20 69 66 28 20 69 73   = db;..  if( is
328fd 43 72 65 61 74 65 20 29 7b 0a 20 20 20 20 63 68  Create ){.    ch
328fe 61 72 20 2a 7a 43 72 65 61 74 65 20 3d 20 73 71  ar *zCreate = sq
328ff 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 0a 22  lite3_mprintf(."
32900 43 52 45 41 54 45 20 54 41 42 4c 45 20 5c 22 25  CREATE TABLE \"%
32901 77 5c 22 2e 5c 22 25 77 5f 6e 6f 64 65 5c 22 28  w\".\"%w_node\"(
32902 6e 6f 64 65 6e 6f 20 49 4e 54 45 47 45 52 20 50  nodeno INTEGER P
32903 52 49 4d 41 52 59 20 4b 45 59 2c 20 64 61 74 61  RIMARY KEY, data
32904 20 42 4c 4f 42 29 3b 22 0a 22 43 52 45 41 54 45   BLOB);"."CREATE
32905 20 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22   TABLE \"%w\".\"
32906 25 77 5f 72 6f 77 69 64 5c 22 28 72 6f 77 69 64  %w_rowid\"(rowid
32907 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52 59   INTEGER PRIMARY
32908 20 4b 45 59 2c 20 6e 6f 64 65 6e 6f 20 49 4e 54   KEY, nodeno INT
32909 45 47 45 52 29 3b 22 0a 22 43 52 45 41 54 45 20  EGER);"."CREATE 
3290a 54 41 42 4c 45 20 5c 22 25 77 5c 22 2e 5c 22 25  TABLE \"%w\".\"%
3290b 77 5f 70 61 72 65 6e 74 5c 22 28 6e 6f 64 65 6e  w_parent\"(noden
3290c 6f 20 49 4e 54 45 47 45 52 20 50 52 49 4d 41 52  o INTEGER PRIMAR
3290d 59 20 4b 45 59 2c 20 70 61 72 65 6e 74 6e 6f 64  Y KEY, parentnod
3290e 65 20 49 4e 54 45 47 45 52 29 3b 22 0a 22 49 4e  e INTEGER);"."IN
3290f 53 45 52 54 20 49 4e 54 4f 20 27 25 71 27 2e 27  SERT INTO '%q'.'
32910 25 71 5f 6e 6f 64 65 27 20 56 41 4c 55 45 53 28  %q_node' VALUES(
32911 31 2c 20 7a 65 72 6f 62 6c 6f 62 28 25 64 29 29  1, zeroblob(%d))
32912 22 2c 0a 20 20 20 20 20 20 7a 44 62 2c 20 7a 50  ",.      zDb, zP
32913 72 65 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65  refix, zDb, zPre
32914 66 69 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69  fix, zDb, zPrefi
32915 78 2c 20 7a 44 62 2c 20 7a 50 72 65 66 69 78 2c  x, zDb, zPrefix,
32916 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
32917 7a 65 0a 20 20 20 20 29 3b 0a 20 20 20 20 69 66  ze.    );.    if
32918 28 20 21 7a 43 72 65 61 74 65 20 29 7b 0a 20 20  ( !zCreate ){.  
32919 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54      return SQLIT
3291a 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20  E_NOMEM;.    }. 
3291b 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f     rc = sqlite3_
3291c 65 78 65 63 28 64 62 2c 20 7a 43 72 65 61 74 65  exec(db, zCreate
3291d 2c 20 30 2c 20 30 2c 20 30 29 3b 0a 20 20 20 20  , 0, 0, 0);.    
3291e 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 43 72  sqlite3_free(zCr
3291f 65 61 74 65 29 3b 0a 20 20 20 20 69 66 28 20 72  eate);.    if( r
32920 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a  c!=SQLITE_OK ){.
32921 20 20 20 20 20 20 72 65 74 75 72 6e 20 72 63 3b        return rc;
32922 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20 20 61 70  .    }.  }..  ap
32923 70 53 74 6d 74 5b 30 5d 20 3d 20 26 70 52 74 72  pStmt[0] = &pRtr
32924 65 65 2d 3e 70 52 65 61 64 4e 6f 64 65 3b 0a 20  ee->pReadNode;. 
32925 20 61 70 70 53 74 6d 74 5b 31 5d 20 3d 20 26 70   appStmt[1] = &p
32926 52 74 72 65 65 2d 3e 70 57 72 69 74 65 4e 6f 64  Rtree->pWriteNod
32927 65 3b 0a 20 20 61 70 70 53 74 6d 74 5b 32 5d 20  e;.  appStmt[2] 
32928 3d 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65  = &pRtree->pDele
32929 74 65 4e 6f 64 65 3b 0a 20 20 61 70 70 53 74 6d  teNode;.  appStm
3292a 74 5b 33 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e  t[3] = &pRtree->
3292b 70 52 65 61 64 52 6f 77 69 64 3b 0a 20 20 61 70  pReadRowid;.  ap
3292c 70 53 74 6d 74 5b 34 5d 20 3d 20 26 70 52 74 72  pStmt[4] = &pRtr
3292d 65 65 2d 3e 70 57 72 69 74 65 52 6f 77 69 64 3b  ee->pWriteRowid;
3292e 0a 20 20 61 70 70 53 74 6d 74 5b 35 5d 20 3d 20  .  appStmt[5] = 
3292f 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74 65  &pRtree->pDelete
32930 52 6f 77 69 64 3b 0a 20 20 61 70 70 53 74 6d 74  Rowid;.  appStmt
32931 5b 36 5d 20 3d 20 26 70 52 74 72 65 65 2d 3e 70  [6] = &pRtree->p
32932 52 65 61 64 50 61 72 65 6e 74 3b 0a 20 20 61 70  ReadParent;.  ap
32933 70 53 74 6d 74 5b 37 5d 20 3d 20 26 70 52 74 72  pStmt[7] = &pRtr
32934 65 65 2d 3e 70 57 72 69 74 65 50 61 72 65 6e 74  ee->pWriteParent
32935 3b 0a 20 20 61 70 70 53 74 6d 74 5b 38 5d 20 3d  ;.  appStmt[8] =
32936 20 26 70 52 74 72 65 65 2d 3e 70 44 65 6c 65 74   &pRtree->pDelet
32937 65 50 61 72 65 6e 74 3b 0a 0a 20 20 66 6f 72 28  eParent;..  for(
32938 69 3d 30 3b 20 69 3c 4e 5f 53 54 41 54 45 4d 45  i=0; i<N_STATEME
32939 4e 54 20 26 26 20 72 63 3d 3d 53 51 4c 49 54 45  NT && rc==SQLITE
3293a 5f 4f 4b 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63  _OK; i++){.    c
3293b 68 61 72 20 2a 7a 53 71 6c 20 3d 20 73 71 6c 69  har *zSql = sqli
3293c 74 65 33 5f 6d 70 72 69 6e 74 66 28 61 7a 53 71  te3_mprintf(azSq
3293d 6c 5b 69 5d 2c 20 7a 44 62 2c 20 7a 50 72 65 66  l[i], zDb, zPref
3293e 69 78 29 3b 0a 20 20 20 20 69 66 28 20 7a 53 71  ix);.    if( zSq
3293f 6c 20 29 7b 0a 20 20 20 20 20 20 72 63 20 3d 20  l ){.      rc = 
32940 73 71 6c 69 74 65 33 5f 70 72 65 70 61 72 65 5f  sqlite3_prepare_
32941 76 32 28 64 62 2c 20 7a 53 71 6c 2c 20 2d 31 2c  v2(db, zSql, -1,
32942 20 61 70 70 53 74 6d 74 5b 69 5d 2c 20 30 29 3b   appStmt[i], 0);
32943 20 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20   .    }else{.   
32944 20 20 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e     rc = SQLITE_N
32945 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a 20 20 20 20  OMEM;.    }.    
32946 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a 53 71  sqlite3_free(zSq
32947 6c 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72  l);.  }..  retur
32948 6e 20 72 63 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  n rc;.}../*.** T
32949 68 69 73 20 72 6f 75 74 69 6e 65 20 71 75 65 72  his routine quer
3294a 69 65 73 20 64 61 74 61 62 61 73 65 20 68 61 6e  ies database han
3294b 64 6c 65 20 64 62 20 66 6f 72 20 74 68 65 20 70  dle db for the p
3294c 61 67 65 2d 73 69 7a 65 20 75 73 65 64 20 62 79  age-size used by
3294d 0a 2a 2a 20 64 61 74 61 62 61 73 65 20 7a 44 62  .** database zDb
3294e 2e 20 49 66 20 73 75 63 63 65 73 73 66 75 6c 2c  . If successful,
3294f 20 74 68 65 20 70 61 67 65 2d 73 69 7a 65 20 69   the page-size i
32950 6e 20 62 79 74 65 73 20 69 73 20 77 72 69 74 74  n bytes is writt
32951 65 6e 20 74 6f 0a 2a 2a 20 2a 70 69 50 61 67 65  en to.** *piPage
32952 53 69 7a 65 20 61 6e 64 20 53 51 4c 49 54 45 5f  Size and SQLITE_
32953 4f 4b 20 72 65 74 75 72 6e 65 64 2e 20 4f 74 68  OK returned. Oth
32954 65 72 77 69 73 65 2c 20 61 6e 64 20 61 6e 20 53  erwise, and an S
32955 51 4c 69 74 65 20 65 72 72 6f 72 20 0a 2a 2a 20  QLite error .** 
32956 63 6f 64 65 20 69 73 20 72 65 74 75 72 6e 65 64  code is returned
32957 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20  ..*/.static int 
32958 67 65 74 50 61 67 65 53 69 7a 65 28 73 71 6c 69  getPageSize(sqli
32959 74 65 33 20 2a 64 62 2c 20 63 6f 6e 73 74 20 63  te3 *db, const c
3295a 68 61 72 20 2a 7a 44 62 2c 20 69 6e 74 20 2a 70  har *zDb, int *p
3295b 69 50 61 67 65 53 69 7a 65 29 7b 0a 20 20 69 6e  iPageSize){.  in
3295c 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4e 4f  t rc = SQLITE_NO
3295d 4d 45 4d 3b 0a 20 20 63 68 61 72 20 2a 7a 53 71  MEM;.  char *zSq
3295e 6c 3b 0a 20 20 73 71 6c 69 74 65 33 5f 73 74 6d  l;.  sqlite3_stm
3295f 74 20 2a 70 53 74 6d 74 20 3d 20 30 3b 0a 0a 20  t *pStmt = 0;.. 
32960 20 7a 53 71 6c 20 3d 20 73 71 6c 69 74 65 33 5f   zSql = sqlite3_
32961 6d 70 72 69 6e 74 66 28 22 50 52 41 47 4d 41 20  mprintf("PRAGMA 
32962 25 51 2e 70 61 67 65 5f 73 69 7a 65 22 2c 20 7a  %Q.page_size", z
32963 44 62 29 3b 0a 20 20 69 66 28 20 21 7a 53 71 6c  Db);.  if( !zSql
32964 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 53   ){.    return S
32965 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d  QLITE_NOMEM;.  }
32966 0a 0a 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33  ..  rc = sqlite3
32967 5f 70 72 65 70 61 72 65 5f 76 32 28 64 62 2c 20  _prepare_v2(db, 
32968 7a 53 71 6c 2c 20 2d 31 2c 20 26 70 53 74 6d 74  zSql, -1, &pStmt
32969 2c 20 30 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f  , 0);.  sqlite3_
3296a 66 72 65 65 28 7a 53 71 6c 29 3b 0a 20 20 69 66  free(zSql);.  if
3296b 28 20 72 63 21 3d 53 51 4c 49 54 45 5f 4f 4b 20  ( rc!=SQLITE_OK 
3296c 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 20 72 63  ){.    return rc
3296d 3b 0a 20 20 7d 0a 0a 20 20 69 66 28 20 53 51 4c  ;.  }..  if( SQL
3296e 49 54 45 5f 52 4f 57 3d 3d 73 71 6c 69 74 65 33  ITE_ROW==sqlite3
3296f 5f 73 74 65 70 28 70 53 74 6d 74 29 20 29 7b 0a  _step(pStmt) ){.
32970 20 20 20 20 2a 70 69 50 61 67 65 53 69 7a 65 20      *piPageSize 
32971 3d 20 73 71 6c 69 74 65 33 5f 63 6f 6c 75 6d 6e  = sqlite3_column
32972 5f 69 6e 74 28 70 53 74 6d 74 2c 20 30 29 3b 0a  _int(pStmt, 0);.
32973 20 20 7d 0a 20 20 72 65 74 75 72 6e 20 73 71 6c    }.  return sql
32974 69 74 65 33 5f 66 69 6e 61 6c 69 7a 65 28 70 53  ite3_finalize(pS
32975 74 6d 74 29 3b 0a 7d 0a 0a 2f 2a 20 0a 2a 2a 20  tmt);.}../* .** 
32976 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20 69 73  This function is
32977 20 74 68 65 20 69 6d 70 6c 65 6d 65 6e 74 61 74   the implementat
32978 69 6f 6e 20 6f 66 20 62 6f 74 68 20 74 68 65 20  ion of both the 
32979 78 43 6f 6e 6e 65 63 74 20 61 6e 64 20 78 43 72  xConnect and xCr
3297a 65 61 74 65 0a 2a 2a 20 6d 65 74 68 6f 64 73 20  eate.** methods 
3297b 6f 66 20 74 68 65 20 72 2d 74 72 65 65 20 76 69  of the r-tree vi
3297c 72 74 75 61 6c 20 74 61 62 6c 65 2e 0a 2a 2a 0a  rtual table..**.
3297d 2a 2a 20 20 20 61 72 67 76 5b 30 5d 20 20 20 2d  **   argv[0]   -
3297e 3e 20 6d 6f 64 75 6c 65 20 6e 61 6d 65 0a 2a 2a  > module name.**
3297f 20 20 20 61 72 67 76 5b 31 5d 20 20 20 2d 3e 20     argv[1]   -> 
32980 64 61 74 61 62 61 73 65 20 6e 61 6d 65 0a 2a 2a  database name.**
32981 20 20 20 61 72 67 76 5b 32 5d 20 20 20 2d 3e 20     argv[2]   -> 
32982 74 61 62 6c 65 20 6e 61 6d 65 0a 2a 2a 20 20 20  table name.**   
32983 61 72 67 76 5b 2e 2e 2e 5d 20 2d 3e 20 63 6f 6c  argv[...] -> col
32984 75 6d 6e 20 6e 61 6d 65 73 2e 2e 2e 0a 2a 2f 0a  umn names....*/.
32985 73 74 61 74 69 63 20 69 6e 74 20 72 74 72 65 65  static int rtree
32986 49 6e 69 74 28 0a 20 20 73 71 6c 69 74 65 33 20  Init(.  sqlite3 
32987 2a 64 62 2c 20 20 20 20 20 20 20 20 20 20 20 20  *db,            
32988 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 44              /* D
32989 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
3298a 6f 6e 20 2a 2f 0a 20 20 76 6f 69 64 20 2a 70 41  on */.  void *pA
3298b 75 78 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  ux,             
3298c 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
3298d 6e 65 20 6f 66 20 74 68 65 20 52 54 52 45 45 5f  ne of the RTREE_
3298e 43 4f 4f 52 44 5f 2a 20 63 6f 6e 73 74 61 6e 74  COORD_* constant
3298f 73 20 2a 2f 0a 20 20 69 6e 74 20 61 72 67 63 2c  s */.  int argc,
32990 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 63 6f 6e   const char *con
32991 73 74 2a 61 72 67 76 2c 20 20 20 2f 2a 20 50 61  st*argv,   /* Pa
32992 72 61 6d 65 74 65 72 73 20 74 6f 20 43 52 45 41  rameters to CREA
32993 54 45 20 54 41 42 4c 45 20 73 74 61 74 65 6d 65  TE TABLE stateme
32994 6e 74 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f  nt */.  sqlite3_
32995 76 74 61 62 20 2a 2a 70 70 56 74 61 62 2c 20 20  vtab **ppVtab,  
32996 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4f              /* O
32997 55 54 3a 20 4e 65 77 20 76 69 72 74 75 61 6c 20  UT: New virtual 
32998 74 61 62 6c 65 20 2a 2f 0a 20 20 63 68 61 72 20  table */.  char 
32999 2a 2a 70 7a 45 72 72 2c 20 20 20 20 20 20 20 20  **pzErr,        
3299a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
3299b 2a 20 4f 55 54 3a 20 45 72 72 6f 72 20 6d 65 73  * OUT: Error mes
3299c 73 61 67 65 2c 20 69 66 20 61 6e 79 20 2a 2f 0a  sage, if any */.
3299d 20 20 69 6e 74 20 69 73 43 72 65 61 74 65 20 20    int isCreate  
3299e 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3299f 20 20 20 20 20 20 2f 2a 20 54 72 75 65 20 66 6f        /* True fo
329a0 72 20 78 43 72 65 61 74 65 2c 20 66 61 6c 73 65  r xCreate, false
329a1 20 66 6f 72 20 78 43 6f 6e 6e 65 63 74 20 2a 2f   for xConnect */
329a2 0a 29 7b 0a 20 20 69 6e 74 20 72 63 20 3d 20 53  .){.  int rc = S
329a3 51 4c 49 54 45 5f 4f 4b 3b 0a 20 20 69 6e 74 20  QLITE_OK;.  int 
329a4 69 50 61 67 65 53 69 7a 65 20 3d 20 30 3b 0a 20  iPageSize = 0;. 
329a5 20 52 74 72 65 65 20 2a 70 52 74 72 65 65 3b 0a   Rtree *pRtree;.
329a6 20 20 69 6e 74 20 6e 44 62 3b 20 20 20 20 20 20    int nDb;      
329a7 20 20 20 20 20 20 20 20 2f 2a 20 4c 65 6e 67 74          /* Lengt
329a8 68 20 6f 66 20 73 74 72 69 6e 67 20 61 72 67 76  h of string argv
329a9 5b 31 5d 20 2a 2f 0a 20 20 69 6e 74 20 6e 4e 61  [1] */.  int nNa
329aa 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20 20 2f  me;            /
329ab 2a 20 4c 65 6e 67 74 68 20 6f 66 20 73 74 72 69  * Length of stri
329ac 6e 67 20 61 72 67 76 5b 32 5d 20 2a 2f 0a 20 20  ng argv[2] */.  
329ad 69 6e 74 20 65 43 6f 6f 72 64 54 79 70 65 20 3d  int eCoordType =
329ae 20 28 69 6e 74 29 70 41 75 78 3b 0a 0a 20 20 63   (int)pAux;..  c
329af 6f 6e 73 74 20 63 68 61 72 20 2a 61 45 72 72 4d  onst char *aErrM
329b0 73 67 5b 5d 20 3d 20 7b 0a 20 20 20 20 30 2c 20  sg[] = {.    0, 
329b1 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329b2 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329b3 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
329b4 20 20 20 2f 2a 20 30 20 2a 2f 0a 20 20 20 20 22     /* 0 */.    "
329b5 57 72 6f 6e 67 20 6e 75 6d 62 65 72 20 6f 66 20  Wrong number of 
329b6 63 6f 6c 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72  columns for an r
329b7 74 72 65 65 20 74 61 62 6c 65 22 2c 20 20 20 20  tree table",    
329b8 20 20 20 20 20 2f 2a 20 31 20 2a 2f 0a 20 20 20       /* 1 */.   
329b9 20 22 54 6f 6f 20 66 65 77 20 63 6f 6c 75 6d 6e   "Too few column
329ba 73 20 66 6f 72 20 61 6e 20 72 74 72 65 65 20 74  s for an rtree t
329bb 61 62 6c 65 22 2c 20 20 20 20 20 20 20 20 20 20  able",          
329bc 20 20 20 20 20 20 20 2f 2a 20 32 20 2a 2f 0a 20         /* 2 */. 
329bd 20 20 20 22 54 6f 6f 20 6d 61 6e 79 20 63 6f 6c     "Too many col
329be 75 6d 6e 73 20 66 6f 72 20 61 6e 20 72 74 72 65  umns for an rtre
329bf 65 20 74 61 62 6c 65 22 20 20 20 20 20 20 20 20  e table"        
329c0 20 20 20 20 20 20 20 20 20 2f 2a 20 33 20 2a 2f           /* 3 */
329c1 0a 20 20 7d 3b 0a 0a 20 20 69 6e 74 20 69 45 72  .  };..  int iEr
329c2 72 20 3d 20 28 61 72 67 63 3c 36 29 20 3f 20 32  r = (argc<6) ? 2
329c3 20 3a 20 61 72 67 63 3e 28 52 54 52 45 45 5f 4d   : argc>(RTREE_M
329c4 41 58 5f 44 49 4d 45 4e 53 49 4f 4e 53 2a 32 2b  AX_DIMENSIONS*2+
329c5 34 29 20 3f 20 33 20 3a 20 61 72 67 63 25 32 3b  4) ? 3 : argc%2;
329c6 0a 20 20 69 66 28 20 61 45 72 72 4d 73 67 5b 69  .  if( aErrMsg[i
329c7 45 72 72 5d 20 29 7b 0a 20 20 20 20 2a 70 7a 45  Err] ){.    *pzE
329c8 72 72 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72  rr = sqlite3_mpr
329c9 69 6e 74 66 28 22 25 73 22 2c 20 61 45 72 72 4d  intf("%s", aErrM
329ca 73 67 5b 69 45 72 72 5d 29 3b 0a 20 20 20 20 72  sg[iErr]);.    r
329cb 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52  eturn SQLITE_ERR
329cc 4f 52 3b 0a 20 20 7d 0a 0a 20 20 72 63 20 3d 20  OR;.  }..  rc = 
329cd 67 65 74 50 61 67 65 53 69 7a 65 28 64 62 2c 20  getPageSize(db, 
329ce 61 72 67 76 5b 31 5d 2c 20 26 69 50 61 67 65 53  argv[1], &iPageS
329cf 69 7a 65 29 3b 0a 20 20 69 66 28 20 72 63 21 3d  ize);.  if( rc!=
329d0 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
329d1 20 72 65 74 75 72 6e 20 72 63 3b 0a 20 20 7d 0a   return rc;.  }.
329d2 0a 20 20 2f 2a 20 41 6c 6c 6f 63 61 74 65 20 74  .  /* Allocate t
329d3 68 65 20 73 71 6c 69 74 65 33 5f 76 74 61 62 20  he sqlite3_vtab 
329d4 73 74 72 75 63 74 75 72 65 20 2a 2f 0a 20 20 6e  structure */.  n
329d5 44 62 20 3d 20 73 74 72 6c 65 6e 28 61 72 67 76  Db = strlen(argv
329d6 5b 31 5d 29 3b 0a 20 20 6e 4e 61 6d 65 20 3d 20  [1]);.  nName = 
329d7 73 74 72 6c 65 6e 28 61 72 67 76 5b 32 5d 29 3b  strlen(argv[2]);
329d8 0a 20 20 70 52 74 72 65 65 20 3d 20 28 52 74 72  .  pRtree = (Rtr
329d9 65 65 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c  ee *)sqlite3_mal
329da 6c 6f 63 28 73 69 7a 65 6f 66 28 52 74 72 65 65  loc(sizeof(Rtree
329db 29 2b 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a  )+nDb+nName+2);.
329dc 20 20 69 66 28 20 21 70 52 74 72 65 65 20 29 7b    if( !pRtree ){
329dd 0a 20 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49  .    return SQLI
329de 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20  TE_NOMEM;.  }.  
329df 6d 65 6d 73 65 74 28 70 52 74 72 65 65 2c 20 30  memset(pRtree, 0
329e0 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65 29 2b  , sizeof(Rtree)+
329e1 6e 44 62 2b 6e 4e 61 6d 65 2b 32 29 3b 0a 20 20  nDb+nName+2);.  
329e2 70 52 74 72 65 65 2d 3e 6e 42 75 73 79 20 3d 20  pRtree->nBusy = 
329e3 31 3b 0a 20 20 70 52 74 72 65 65 2d 3e 62 61 73  1;.  pRtree->bas
329e4 65 2e 70 4d 6f 64 75 6c 65 20 3d 20 26 72 74 72  e.pModule = &rtr
329e5 65 65 4d 6f 64 75 6c 65 3b 0a 20 20 70 52 74 72  eeModule;.  pRtr
329e6 65 65 2d 3e 7a 44 62 20 3d 20 28 63 68 61 72 20  ee->zDb = (char 
329e7 2a 29 26 70 52 74 72 65 65 5b 31 5d 3b 0a 20 20  *)&pRtree[1];.  
329e8 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65 20 3d 20  pRtree->zName = 
329e9 26 70 52 74 72 65 65 2d 3e 7a 44 62 5b 6e 44 62  &pRtree->zDb[nDb
329ea 2b 31 5d 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e  +1];.  pRtree->n
329eb 44 69 6d 20 3d 20 28 61 72 67 63 2d 34 29 2f 32  Dim = (argc-4)/2
329ec 3b 0a 20 20 70 52 74 72 65 65 2d 3e 6e 42 79 74  ;.  pRtree->nByt
329ed 65 73 50 65 72 43 65 6c 6c 20 3d 20 38 20 2b 20  esPerCell = 8 + 
329ee 70 52 74 72 65 65 2d 3e 6e 44 69 6d 2a 34 2a 32  pRtree->nDim*4*2
329ef 3b 0a 20 20 70 52 74 72 65 65 2d 3e 65 43 6f 6f  ;.  pRtree->eCoo
329f0 72 64 54 79 70 65 20 3d 20 65 43 6f 6f 72 64 54  rdType = eCoordT
329f1 79 70 65 3b 0a 20 20 6d 65 6d 63 70 79 28 70 52  ype;.  memcpy(pR
329f2 74 72 65 65 2d 3e 7a 44 62 2c 20 61 72 67 76 5b  tree->zDb, argv[
329f3 31 5d 2c 20 6e 44 62 29 3b 0a 20 20 6d 65 6d 63  1], nDb);.  memc
329f4 70 79 28 70 52 74 72 65 65 2d 3e 7a 4e 61 6d 65  py(pRtree->zName
329f5 2c 20 61 72 67 76 5b 32 5d 2c 20 6e 4e 61 6d 65  , argv[2], nName
329f6 29 3b 0a 0a 20 20 2f 2a 20 46 69 67 75 72 65 20  );..  /* Figure 
329f7 6f 75 74 20 74 68 65 20 6e 6f 64 65 20 73 69 7a  out the node siz
329f8 65 20 74 6f 20 75 73 65 2e 20 42 79 20 64 65 66  e to use. By def
329f9 61 75 6c 74 2c 20 75 73 65 20 36 34 20 62 79 74  ault, use 64 byt
329fa 65 73 20 6c 65 73 73 20 74 68 61 6e 0a 20 20 2a  es less than.  *
329fb 2a 20 74 68 65 20 64 61 74 61 62 61 73 65 20 70  * the database p
329fc 61 67 65 2d 73 69 7a 65 2e 20 54 68 69 73 20 65  age-size. This e
329fd 6e 73 75 72 65 73 20 74 68 61 74 20 65 61 63 68  nsures that each
329fe 20 6e 6f 64 65 20 69 73 20 73 74 6f 72 65 64 20   node is stored 
329ff 6f 6e 0a 20 20 2a 2a 20 61 20 73 69 6e 67 6c 65  on.  ** a single
32a00 20 64 61 74 61 62 61 73 65 20 70 61 67 65 2e 0a   database page..
32a01 20 20 2a 2a 0a 20 20 2a 2a 20 49 66 20 74 68 65    **.  ** If the
32a02 20 64 61 74 61 62 61 73 64 20 70 61 67 65 2d 73   databasd page-s
32a03 69 7a 65 20 69 73 20 73 6f 20 6c 61 72 67 65 20  ize is so large 
32a04 74 68 61 74 20 6d 6f 72 65 20 74 68 61 6e 20 52  that more than R
32a05 54 52 45 45 5f 4d 41 58 43 45 4c 4c 53 0a 20 20  TREE_MAXCELLS.  
32a06 2a 2a 20 65 6e 74 72 69 65 73 20 77 6f 75 6c 64  ** entries would
32a07 20 66 69 74 20 69 6e 20 61 20 73 69 6e 67 6c 65   fit in a single
32a08 20 6e 6f 64 65 2c 20 75 73 65 20 61 20 73 6d 61   node, use a sma
32a09 6c 6c 65 72 20 6e 6f 64 65 2d 73 69 7a 65 2e 0a  ller node-size..
32a0a 20 20 2a 2f 0a 20 20 70 52 74 72 65 65 2d 3e 69    */.  pRtree->i
32a0b 4e 6f 64 65 53 69 7a 65 20 3d 20 69 50 61 67 65  NodeSize = iPage
32a0c 53 69 7a 65 2d 36 34 3b 0a 20 20 69 66 28 20 28  Size-64;.  if( (
32a0d 34 2b 70 52 74 72 65 65 2d 3e 6e 42 79 74 65 73  4+pRtree->nBytes
32a0e 50 65 72 43 65 6c 6c 2a 52 54 52 45 45 5f 4d 41  PerCell*RTREE_MA
32a0f 58 43 45 4c 4c 53 29 3c 70 52 74 72 65 65 2d 3e  XCELLS)<pRtree->
32a10 69 4e 6f 64 65 53 69 7a 65 20 29 7b 0a 20 20 20  iNodeSize ){.   
32a11 20 70 52 74 72 65 65 2d 3e 69 4e 6f 64 65 53 69   pRtree->iNodeSi
32a12 7a 65 20 3d 20 34 2b 70 52 74 72 65 65 2d 3e 6e  ze = 4+pRtree->n
32a13 42 79 74 65 73 50 65 72 43 65 6c 6c 2a 52 54 52  BytesPerCell*RTR
32a14 45 45 5f 4d 41 58 43 45 4c 4c 53 3b 0a 20 20 7d  EE_MAXCELLS;.  }
32a15 0a 0a 20 20 2f 2a 20 43 72 65 61 74 65 2f 43 6f  ..  /* Create/Co
32a16 6e 6e 65 63 74 20 74 6f 20 74 68 65 20 75 6e 64  nnect to the und
32a17 65 72 6c 79 69 6e 67 20 72 65 6c 61 74 69 6f 6e  erlying relation
32a18 61 6c 20 64 61 74 61 62 61 73 65 20 73 63 68 65  al database sche
32a19 6d 61 2e 20 49 66 0a 20 20 2a 2a 20 74 68 61 74  ma. If.  ** that
32a1a 20 69 73 20 73 75 63 63 65 73 73 66 75 6c 2c 20   is successful, 
32a1b 63 61 6c 6c 20 73 71 6c 69 74 65 33 5f 64 65 63  call sqlite3_dec
32a1c 6c 61 72 65 5f 76 74 61 62 28 29 20 74 6f 20 63  lare_vtab() to c
32a1d 6f 6e 66 69 67 75 72 65 0a 20 20 2a 2a 20 74 68  onfigure.  ** th
32a1e 65 20 72 2d 74 72 65 65 20 74 61 62 6c 65 20 73  e r-tree table s
32a1f 63 68 65 6d 61 2e 0a 20 20 2a 2f 0a 20 20 69 66  chema..  */.  if
32a20 28 20 28 72 63 20 3d 20 72 74 72 65 65 53 71 6c  ( (rc = rtreeSql
32a21 49 6e 69 74 28 70 52 74 72 65 65 2c 20 64 62 2c  Init(pRtree, db,
32a22 20 61 72 67 76 5b 31 5d 2c 20 61 72 67 76 5b 32   argv[1], argv[2
32a23 5d 2c 20 69 73 43 72 65 61 74 65 29 29 20 29 7b  ], isCreate)) ){
32a24 0a 20 20 20 20 2a 70 7a 45 72 72 20 3d 20 73 71  .    *pzErr = sq
32a25 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22 25  lite3_mprintf("%
32a26 73 22 2c 20 73 71 6c 69 74 65 33 5f 65 72 72 6d  s", sqlite3_errm
32a27 73 67 28 64 62 29 29 3b 0a 20 20 7d 65 6c 73 65  sg(db));.  }else
32a28 7b 0a 20 20 20 20 63 68 61 72 20 2a 7a 53 71 6c  {.    char *zSql
32a29 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e   = sqlite3_mprin
32a2a 74 66 28 22 43 52 45 41 54 45 20 54 41 42 4c 45  tf("CREATE TABLE
32a2b 20 78 28 25 73 22 2c 20 61 72 67 76 5b 33 5d 29   x(%s", argv[3])
32a2c 3b 0a 20 20 20 20 63 68 61 72 20 2a 7a 54 6d 70  ;.    char *zTmp
32a2d 3b 0a 20 20 20 20 69 6e 74 20 69 69 3b 0a 20 20  ;.    int ii;.  
32a2e 20 20 66 6f 72 28 69 69 3d 34 3b 20 7a 53 71 6c    for(ii=4; zSql
32a2f 20 26 26 20 69 69 3c 61 72 67 63 3b 20 69 69 2b   && ii<argc; ii+
32a30 2b 29 7b 0a 20 20 20 20 20 20 7a 54 6d 70 20 3d  +){.      zTmp =
32a31 20 7a 53 71 6c 3b 0a 20 20 20 20 20 20 7a 53 71   zSql;.      zSq
32a32 6c 20 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69  l = sqlite3_mpri
32a33 6e 74 66 28 22 25 73 2c 20 25 73 22 2c 20 7a 54  ntf("%s, %s", zT
32a34 6d 70 2c 20 61 72 67 76 5b 69 69 5d 29 3b 0a 20  mp, argv[ii]);. 
32a35 20 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65       sqlite3_fre
32a36 65 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20  e(zTmp);.    }. 
32a37 20 20 20 69 66 28 20 7a 53 71 6c 20 29 7b 0a 20     if( zSql ){. 
32a38 20 20 20 20 20 7a 54 6d 70 20 3d 20 7a 53 71 6c       zTmp = zSql
32a39 3b 0a 20 20 20 20 20 20 7a 53 71 6c 20 3d 20 73  ;.      zSql = s
32a3a 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74 66 28 22  qlite3_mprintf("
32a3b 25 73 29 3b 22 2c 20 7a 54 6d 70 29 3b 0a 20 20  %s);", zTmp);.  
32a3c 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
32a3d 28 7a 54 6d 70 29 3b 0a 20 20 20 20 7d 0a 20 20  (zTmp);.    }.  
32a3e 20 20 69 66 28 20 21 7a 53 71 6c 20 7c 7c 20 73    if( !zSql || s
32a3f 71 6c 69 74 65 33 5f 64 65 63 6c 61 72 65 5f 76  qlite3_declare_v
32a40 74 61 62 28 64 62 2c 20 7a 53 71 6c 29 20 29 7b  tab(db, zSql) ){
32a41 0a 20 20 20 20 20 20 72 63 20 3d 20 53 51 4c 49  .      rc = SQLI
32a42 54 45 5f 4e 4f 4d 45 4d 3b 0a 20 20 20 20 7d 0a  TE_NOMEM;.    }.
32a43 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65      sqlite3_free
32a44 28 7a 53 71 6c 29 3b 0a 20 20 7d 0a 0a 20 20 69  (zSql);.  }..  i
32a45 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
32a46 20 29 7b 0a 20 20 20 20 2a 70 70 56 74 61 62 20   ){.    *ppVtab 
32a47 3d 20 28 73 71 6c 69 74 65 33 5f 76 74 61 62 20  = (sqlite3_vtab 
32a48 2a 29 70 52 74 72 65 65 3b 0a 20 20 7d 65 6c 73  *)pRtree;.  }els
32a49 65 7b 0a 20 20 20 20 72 74 72 65 65 52 65 6c 65  e{.    rtreeRele
32a4a 61 73 65 28 70 52 74 72 65 65 29 3b 0a 20 20 7d  ase(pRtree);.  }
32a4b 0a 20 20 72 65 74 75 72 6e 20 72 63 3b 0a 7d 0a  .  return rc;.}.
32a4c 0a 0a 2f 2a 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e  ../*.** Implemen
32a4d 74 61 74 69 6f 6e 20 6f 66 20 61 20 73 63 61 6c  tation of a scal
32a4e 61 72 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74  ar function that
32a4f 20 64 65 63 6f 64 65 73 20 72 2d 74 72 65 65 20   decodes r-tree 
32a50 6e 6f 64 65 73 20 74 6f 0a 2a 2a 20 68 75 6d 61  nodes to.** huma
32a51 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72 69 6e  n readable strin
32a52 67 73 2e 20 54 68 69 73 20 63 61 6e 20 62 65 20  gs. This can be 
32a53 75 73 65 64 20 66 6f 72 20 64 65 62 75 67 67 69  used for debuggi
32a54 6e 67 20 61 6e 64 20 61 6e 61 6c 79 73 69 73 2e  ng and analysis.
32a55 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63 61 6c 61  .**.** The scala
32a56 72 20 66 75 6e 63 74 69 6f 6e 20 74 61 6b 65 73  r function takes
32a57 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 2c 20   two arguments, 
32a58 61 20 62 6c 6f 62 20 6f 66 20 64 61 74 61 20 63  a blob of data c
32a59 6f 6e 74 61 69 6e 69 6e 67 0a 2a 2a 20 61 6e 20  ontaining.** an 
32a5a 72 2d 74 72 65 65 20 6e 6f 64 65 2c 20 61 6e 64  r-tree node, and
32a5b 20 74 68 65 20 6e 75 6d 62 65 72 20 6f 66 20 64   the number of d
32a5c 69 6d 65 6e 73 69 6f 6e 73 20 74 68 65 20 72 2d  imensions the r-
32a5d 74 72 65 65 20 69 6e 64 65 78 65 73 2e 0a 2a 2a  tree indexes..**
32a5e 20 46 6f 72 20 61 20 74 77 6f 2d 64 69 6d 65 6e   For a two-dimen
32a5f 73 69 6f 6e 61 6c 20 72 2d 74 72 65 65 20 73 74  sional r-tree st
32a60 72 75 63 74 75 72 65 20 63 61 6c 6c 65 64 20 22  ructure called "
32a61 72 74 22 2c 20 74 6f 20 64 65 73 65 72 69 61 6c  rt", to deserial
32a62 69 7a 65 0a 2a 2a 20 61 6c 6c 20 6e 6f 64 65 73  ize.** all nodes
32a63 2c 20 61 20 73 74 61 74 65 6d 65 6e 74 20 6c 69  , a statement li
32a64 6b 65 3a 0a 2a 2a 0a 2a 2a 20 20 20 53 45 4c 45  ke:.**.**   SELE
32a65 43 54 20 72 74 72 65 65 6e 6f 64 65 28 32 2c 20  CT rtreenode(2, 
32a66 64 61 74 61 29 20 46 52 4f 4d 20 72 74 5f 6e 6f  data) FROM rt_no
32a67 64 65 3b 0a 2a 2a 0a 2a 2a 20 54 68 65 20 68 75  de;.**.** The hu
32a68 6d 61 6e 20 72 65 61 64 61 62 6c 65 20 73 74 72  man readable str
32a69 69 6e 67 20 74 61 6b 65 73 20 74 68 65 20 66 6f  ing takes the fo
32a6a 72 6d 20 6f 66 20 61 20 54 63 6c 20 6c 69 73 74  rm of a Tcl list
32a6b 20 77 69 74 68 20 6f 6e 65 0a 2a 2a 20 65 6e 74   with one.** ent
32a6c 72 79 20 66 6f 72 20 65 61 63 68 20 63 65 6c 6c  ry for each cell
32a6d 20 69 6e 20 74 68 65 20 72 2d 74 72 65 65 20 6e   in the r-tree n
32a6e 6f 64 65 2e 20 45 61 63 68 20 65 6e 74 72 79 20  ode. Each entry 
32a6f 69 73 20 69 74 73 65 6c 66 20 61 0a 2a 2a 20 6c  is itself a.** l
32a70 69 73 74 2c 20 63 6f 6e 74 61 69 6e 69 6e 67 20  ist, containing 
32a71 74 68 65 20 38 2d 62 79 74 65 20 72 6f 77 69 64  the 8-byte rowid
32a72 2f 70 61 67 65 6e 6f 20 66 6f 6c 6c 6f 77 65 64  /pageno followed
32a73 20 62 79 20 74 68 65 20 0a 2a 2a 20 3c 6e 75 6d   by the .** <num
32a74 2d 64 69 6d 65 6e 73 69 6f 6e 3e 2a 32 20 63 6f  -dimension>*2 co
32a75 6f 72 64 69 6e 61 74 65 73 2e 0a 2a 2f 0a 73 74  ordinates..*/.st
32a76 61 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 6e  atic void rtreen
32a77 6f 64 65 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  ode(sqlite3_cont
32a78 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41  ext *ctx, int nA
32a79 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
32a7a 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 63 68  e **apArg){.  ch
32a7b 61 72 20 2a 7a 54 65 78 74 20 3d 20 30 3b 0a 20  ar *zText = 0;. 
32a7c 20 52 74 72 65 65 4e 6f 64 65 20 6e 6f 64 65 3b   RtreeNode node;
32a7d 0a 20 20 52 74 72 65 65 20 74 72 65 65 3b 0a 20  .  Rtree tree;. 
32a7e 20 69 6e 74 20 69 69 3b 0a 0a 20 20 6d 65 6d 73   int ii;..  mems
32a7f 65 74 28 26 6e 6f 64 65 2c 20 30 2c 20 73 69 7a  et(&node, 0, siz
32a80 65 6f 66 28 52 74 72 65 65 4e 6f 64 65 29 29 3b  eof(RtreeNode));
32a81 0a 20 20 6d 65 6d 73 65 74 28 26 74 72 65 65 2c  .  memset(&tree,
32a82 20 30 2c 20 73 69 7a 65 6f 66 28 52 74 72 65 65   0, sizeof(Rtree
32a83 29 29 3b 0a 20 20 74 72 65 65 2e 6e 44 69 6d 20  ));.  tree.nDim 
32a84 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
32a85 69 6e 74 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20  int(apArg[0]);. 
32a86 20 74 72 65 65 2e 6e 42 79 74 65 73 50 65 72 43   tree.nBytesPerC
32a87 65 6c 6c 20 3d 20 38 20 2b 20 38 20 2a 20 74 72  ell = 8 + 8 * tr
32a88 65 65 2e 6e 44 69 6d 3b 0a 20 20 6e 6f 64 65 2e  ee.nDim;.  node.
32a89 7a 44 61 74 61 20 3d 20 28 75 38 20 2a 29 73 71  zData = (u8 *)sq
32a8a 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f 62  lite3_value_blob
32a8b 28 61 70 41 72 67 5b 31 5d 29 3b 0a 0a 20 20 66  (apArg[1]);..  f
32a8c 6f 72 28 69 69 3d 30 3b 20 69 69 3c 4e 43 45 4c  or(ii=0; ii<NCEL
32a8d 4c 28 26 6e 6f 64 65 29 3b 20 69 69 2b 2b 29 7b  L(&node); ii++){
32a8e 0a 20 20 20 20 63 68 61 72 20 7a 43 65 6c 6c 5b  .    char zCell[
32a8f 35 31 32 5d 3b 0a 20 20 20 20 69 6e 74 20 6e 43  512];.    int nC
32a90 65 6c 6c 20 3d 20 30 3b 0a 20 20 20 20 52 74 72  ell = 0;.    Rtr
32a91 65 65 43 65 6c 6c 20 63 65 6c 6c 3b 0a 20 20 20  eeCell cell;.   
32a92 20 69 6e 74 20 6a 6a 3b 0a 0a 20 20 20 20 6e 6f   int jj;..    no
32a93 64 65 47 65 74 43 65 6c 6c 28 26 74 72 65 65 2c  deGetCell(&tree,
32a94 20 26 6e 6f 64 65 2c 20 69 69 2c 20 26 63 65 6c   &node, ii, &cel
32a95 6c 29 3b 0a 20 20 20 20 73 71 6c 69 74 65 33 5f  l);.    sqlite3_
32a96 73 6e 70 72 69 6e 74 66 28 35 31 32 2d 6e 43 65  snprintf(512-nCe
32a97 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65 6c 6c 5d  ll,&zCell[nCell]
32a98 2c 22 25 64 22 2c 20 63 65 6c 6c 2e 69 52 6f 77  ,"%d", cell.iRow
32a99 69 64 29 3b 0a 20 20 20 20 6e 43 65 6c 6c 20 3d  id);.    nCell =
32a9a 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c 29 3b 0a   strlen(zCell);.
32a9b 20 20 20 20 66 6f 72 28 6a 6a 3d 30 3b 20 6a 6a      for(jj=0; jj
32a9c 3c 74 72 65 65 2e 6e 44 69 6d 2a 32 3b 20 6a 6a  <tree.nDim*2; jj
32a9d 2b 2b 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74  ++){.      sqlit
32a9e 65 33 5f 73 6e 70 72 69 6e 74 66 28 35 31 32 2d  e3_snprintf(512-
32a9f 6e 43 65 6c 6c 2c 26 7a 43 65 6c 6c 5b 6e 43 65  nCell,&zCell[nCe
32aa0 6c 6c 5d 2c 22 20 25 66 22 2c 28 64 6f 75 62 6c  ll]," %f",(doubl
32aa1 65 29 63 65 6c 6c 2e 61 43 6f 6f 72 64 5b 6a 6a  e)cell.aCoord[jj
32aa2 5d 2e 66 29 3b 0a 20 20 20 20 20 20 6e 43 65 6c  ].f);.      nCel
32aa3 6c 20 3d 20 73 74 72 6c 65 6e 28 7a 43 65 6c 6c  l = strlen(zCell
32aa4 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 69 66  );.    }..    if
32aa5 28 20 7a 54 65 78 74 20 29 7b 0a 20 20 20 20 20  ( zText ){.     
32aa6 20 63 68 61 72 20 2a 7a 54 65 78 74 4e 65 77 20   char *zTextNew 
32aa7 3d 20 73 71 6c 69 74 65 33 5f 6d 70 72 69 6e 74  = sqlite3_mprint
32aa8 66 28 22 25 73 20 7b 25 73 7d 22 2c 20 7a 54 65  f("%s {%s}", zTe
32aa9 78 74 2c 20 7a 43 65 6c 6c 29 3b 0a 20 20 20 20  xt, zCell);.    
32aaa 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28 7a    sqlite3_free(z
32aab 54 65 78 74 29 3b 0a 20 20 20 20 20 20 7a 54 65  Text);.      zTe
32aac 78 74 20 3d 20 7a 54 65 78 74 4e 65 77 3b 0a 20  xt = zTextNew;. 
32aad 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 20 20     }else{.      
32aae 7a 54 65 78 74 20 3d 20 73 71 6c 69 74 65 33 5f  zText = sqlite3_
32aaf 6d 70 72 69 6e 74 66 28 22 7b 25 73 7d 22 2c 20  mprintf("{%s}", 
32ab0 7a 43 65 6c 6c 29 3b 0a 20 20 20 20 7d 0a 20 20  zCell);.    }.  
32ab1 7d 0a 20 20 0a 20 20 73 71 6c 69 74 65 33 5f 72  }.  .  sqlite3_r
32ab2 65 73 75 6c 74 5f 74 65 78 74 28 63 74 78 2c 20  esult_text(ctx, 
32ab3 7a 54 65 78 74 2c 20 2d 31 2c 20 73 71 6c 69 74  zText, -1, sqlit
32ab4 65 33 5f 66 72 65 65 29 3b 0a 7d 0a 0a 73 74 61  e3_free);.}..sta
32ab5 74 69 63 20 76 6f 69 64 20 72 74 72 65 65 64 65  tic void rtreede
32ab6 70 74 68 28 73 71 6c 69 74 65 33 5f 63 6f 6e 74  pth(sqlite3_cont
32ab7 65 78 74 20 2a 63 74 78 2c 20 69 6e 74 20 6e 41  ext *ctx, int nA
32ab8 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
32ab9 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 69 66  e **apArg){.  if
32aba 28 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  ( sqlite3_value_
32abb 74 79 70 65 28 61 70 41 72 67 5b 30 5d 29 21 3d  type(apArg[0])!=
32abc 53 51 4c 49 54 45 5f 42 4c 4f 42 20 0a 20 20 20  SQLITE_BLOB .   
32abd 7c 7c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65  || sqlite3_value
32abe 5f 62 79 74 65 73 28 61 70 41 72 67 5b 30 5d 29  _bytes(apArg[0])
32abf 3c 32 0a 20 20 29 7b 0a 20 20 20 20 73 71 6c 69  <2.  ){.    sqli
32ac0 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
32ac1 28 63 74 78 2c 20 22 49 6e 76 61 6c 69 64 20 61  (ctx, "Invalid a
32ac2 72 67 75 6d 65 6e 74 20 74 6f 20 72 74 72 65 65  rgument to rtree
32ac3 64 65 70 74 68 28 29 22 2c 20 2d 31 29 3b 20 0a  depth()", -1); .
32ac4 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20 75 38 20    }else{.    u8 
32ac5 2a 7a 42 6c 6f 62 20 3d 20 28 75 38 20 2a 29 73  *zBlob = (u8 *)s
32ac6 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 62 6c 6f  qlite3_value_blo
32ac7 62 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20  b(apArg[0]);.   
32ac8 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
32ac9 69 6e 74 28 63 74 78 2c 20 72 65 61 64 49 6e 74  int(ctx, readInt
32aca 31 36 28 7a 42 6c 6f 62 29 29 3b 0a 20 20 7d 0a  16(zBlob));.  }.
32acb 7d 0a 0a 2f 2a 0a 2a 2a 20 52 65 67 69 73 74 65  }../*.** Registe
32acc 72 20 74 68 65 20 72 2d 74 72 65 65 20 6d 6f 64  r the r-tree mod
32acd 75 6c 65 20 77 69 74 68 20 64 61 74 61 62 61 73  ule with databas
32ace 65 20 68 61 6e 64 6c 65 20 64 62 2e 20 54 68 69  e handle db. Thi
32acf 73 20 63 72 65 61 74 65 73 20 74 68 65 0a 2a 2a  s creates the.**
32ad0 20 76 69 72 74 75 61 6c 20 74 61 62 6c 65 20 6d   virtual table m
32ad1 6f 64 75 6c 65 20 22 72 74 72 65 65 22 20 61 6e  odule "rtree" an
32ad2 64 20 74 68 65 20 64 65 62 75 67 67 69 6e 67 2f  d the debugging/
32ad3 61 6e 61 6c 79 73 69 73 20 73 63 61 6c 61 72 20  analysis scalar 
32ad4 0a 2a 2a 20 66 75 6e 63 74 69 6f 6e 20 22 72 74  .** function "rt
32ad5 72 65 65 6e 6f 64 65 22 2e 0a 2a 2f 0a 53 51 4c  reenode"..*/.SQL
32ad6 49 54 45 5f 50 52 49 56 41 54 45 20 69 6e 74 20  ITE_PRIVATE int 
32ad7 73 71 6c 69 74 65 33 52 74 72 65 65 49 6e 69 74  sqlite3RtreeInit
32ad8 28 73 71 6c 69 74 65 33 20 2a 64 62 29 7b 0a 20  (sqlite3 *db){. 
32ad9 20 69 6e 74 20 72 63 20 3d 20 53 51 4c 49 54 45   int rc = SQLITE
32ada 5f 4f 4b 3b 0a 0a 20 20 69 66 28 20 72 63 3d 3d  _OK;..  if( rc==
32adb 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20  SQLITE_OK ){.   
32adc 20 69 6e 74 20 75 74 66 38 20 3d 20 53 51 4c 49   int utf8 = SQLI
32add 54 45 5f 55 54 46 38 3b 0a 20 20 20 20 72 63 20  TE_UTF8;.    rc 
32ade 3d 20 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65  = sqlite3_create
32adf 5f 66 75 6e 63 74 69 6f 6e 28 64 62 2c 20 22 72  _function(db, "r
32ae0 74 72 65 65 6e 6f 64 65 22 2c 20 32 2c 20 75 74  treenode", 2, ut
32ae1 66 38 2c 20 30 2c 20 72 74 72 65 65 6e 6f 64 65  f8, 0, rtreenode
32ae2 2c 20 30 2c 20 30 29 3b 0a 20 20 7d 0a 20 20 69  , 0, 0);.  }.  i
32ae3 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f 4b  f( rc==SQLITE_OK
32ae4 20 29 7b 0a 20 20 20 20 69 6e 74 20 75 74 66 38   ){.    int utf8
32ae5 20 3d 20 53 51 4c 49 54 45 5f 55 54 46 38 3b 0a   = SQLITE_UTF8;.
32ae6 20 20 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33      rc = sqlite3
32ae7 5f 63 72 65 61 74 65 5f 66 75 6e 63 74 69 6f 6e  _create_function
32ae8 28 64 62 2c 20 22 72 74 72 65 65 64 65 70 74 68  (db, "rtreedepth
32ae9 22 2c 20 31 2c 20 75 74 66 38 2c 20 30 2c 72 74  ", 1, utf8, 0,rt
32aea 72 65 65 64 65 70 74 68 2c 20 30 2c 20 30 29 3b  reedepth, 0, 0);
32aeb 0a 20 20 7d 0a 20 20 69 66 28 20 72 63 3d 3d 53  .  }.  if( rc==S
32aec 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20 20 20  QLITE_OK ){.    
32aed 76 6f 69 64 20 2a 63 20 3d 20 28 76 6f 69 64 20  void *c = (void 
32aee 2a 29 52 54 52 45 45 5f 43 4f 4f 52 44 5f 52 45  *)RTREE_COORD_RE
32aef 41 4c 33 32 3b 0a 20 20 20 20 72 63 20 3d 20 73  AL32;.    rc = s
32af0 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 6d 6f  qlite3_create_mo
32af1 64 75 6c 65 5f 76 32 28 64 62 2c 20 22 72 74 72  dule_v2(db, "rtr
32af2 65 65 22 2c 20 26 72 74 72 65 65 4d 6f 64 75 6c  ee", &rtreeModul
32af3 65 2c 20 63 2c 20 30 29 3b 0a 20 20 7d 0a 20 20  e, c, 0);.  }.  
32af4 69 66 28 20 72 63 3d 3d 53 51 4c 49 54 45 5f 4f  if( rc==SQLITE_O
32af5 4b 20 29 7b 0a 20 20 20 20 76 6f 69 64 20 2a 63  K ){.    void *c
32af6 20 3d 20 28 76 6f 69 64 20 2a 29 52 54 52 45 45   = (void *)RTREE
32af7 5f 43 4f 4f 52 44 5f 49 4e 54 33 32 3b 0a 20 20  _COORD_INT32;.  
32af8 20 20 72 63 20 3d 20 73 71 6c 69 74 65 33 5f 63    rc = sqlite3_c
32af9 72 65 61 74 65 5f 6d 6f 64 75 6c 65 5f 76 32 28  reate_module_v2(
32afa 64 62 2c 20 22 72 74 72 65 65 5f 69 33 32 22 2c  db, "rtree_i32",
32afb 20 26 72 74 72 65 65 4d 6f 64 75 6c 65 2c 20 63   &rtreeModule, c
32afc 2c 20 30 29 3b 0a 20 20 7d 0a 0a 20 20 72 65 74  , 0);.  }..  ret
32afd 75 72 6e 20 72 63 3b 0a 7d 0a 0a 23 69 66 20 21  urn rc;.}..#if !
32afe 53 51 4c 49 54 45 5f 43 4f 52 45 0a 53 51 4c 49  SQLITE_CORE.SQLI
32aff 54 45 5f 41 50 49 20 69 6e 74 20 73 71 6c 69 74  TE_API int sqlit
32b00 65 33 5f 65 78 74 65 6e 73 69 6f 6e 5f 69 6e 69  e3_extension_ini
32b01 74 28 0a 20 20 73 71 6c 69 74 65 33 20 2a 64 62  t(.  sqlite3 *db
32b02 2c 0a 20 20 63 68 61 72 20 2a 2a 70 7a 45 72 72  ,.  char **pzErr
32b03 4d 73 67 2c 0a 20 20 63 6f 6e 73 74 20 73 71 6c  Msg,.  const sql
32b04 69 74 65 33 5f 61 70 69 5f 72 6f 75 74 69 6e 65  ite3_api_routine
32b05 73 20 2a 70 41 70 69 0a 29 7b 0a 20 20 53 51 4c  s *pApi.){.  SQL
32b06 49 54 45 5f 45 58 54 45 4e 53 49 4f 4e 5f 49 4e  ITE_EXTENSION_IN
32b07 49 54 32 28 70 41 70 69 29 0a 20 20 72 65 74 75  IT2(pApi).  retu
32b08 72 6e 20 73 71 6c 69 74 65 33 52 74 72 65 65 49  rn sqlite3RtreeI
32b09 6e 69 74 28 64 62 29 3b 0a 7d 0a 23 65 6e 64 69  nit(db);.}.#endi
32b0a 66 0a 0a 23 65 6e 64 69 66 0a 0a 2f 2a 2a 2a 2a  f..#endif../****
32b0b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 45 6e 64 20 6f  ********** End o
32b0c 66 20 72 74 72 65 65 2e 63 20 2a 2a 2a 2a 2a 2a  f rtree.c ******
32b0d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b0e 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b0f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 2a 2a 2a  *********/./****
32b10 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 20 42 65 67 69 6e  ********** Begin
32b11 20 66 69 6c 65 20 69 63 75 2e 63 20 2a 2a 2a 2a   file icu.c ****
32b12 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b13 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b14 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 2f 2a 0a 2a 2a  *********/./*.**
32b15 20 32 30 30 37 20 4d 61 79 20 36 0a 2a 2a 0a 2a   2007 May 6.**.*
32b16 2a 20 54 68 65 20 61 75 74 68 6f 72 20 64 69 73  * The author dis
32b17 63 6c 61 69 6d 73 20 63 6f 70 79 72 69 67 68 74  claims copyright
32b18 20 74 6f 20 74 68 69 73 20 73 6f 75 72 63 65 20   to this source 
32b19 63 6f 64 65 2e 20 20 49 6e 20 70 6c 61 63 65 20  code.  In place 
32b1a 6f 66 0a 2a 2a 20 61 20 6c 65 67 61 6c 20 6e 6f  of.** a legal no
32b1b 74 69 63 65 2c 20 68 65 72 65 20 69 73 20 61 20  tice, here is a 
32b1c 62 6c 65 73 73 69 6e 67 3a 0a 2a 2a 0a 2a 2a 20  blessing:.**.** 
32b1d 20 20 20 4d 61 79 20 79 6f 75 20 64 6f 20 67 6f     May you do go
32b1e 6f 64 20 61 6e 64 20 6e 6f 74 20 65 76 69 6c 2e  od and not evil.
32b1f 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 66  .**    May you f
32b20 69 6e 64 20 66 6f 72 67 69 76 65 6e 65 73 73 20  ind forgiveness 
32b21 66 6f 72 20 79 6f 75 72 73 65 6c 66 20 61 6e 64  for yourself and
32b22 20 66 6f 72 67 69 76 65 20 6f 74 68 65 72 73 2e   forgive others.
32b23 0a 2a 2a 20 20 20 20 4d 61 79 20 79 6f 75 20 73  .**    May you s
32b24 68 61 72 65 20 66 72 65 65 6c 79 2c 20 6e 65 76  hare freely, nev
32b25 65 72 20 74 61 6b 69 6e 67 20 6d 6f 72 65 20 74  er taking more t
32b26 68 61 6e 20 79 6f 75 20 67 69 76 65 2e 0a 2a 2a  han you give..**
32b27 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
32b28 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b29 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32b2b 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 2a 2a 20 24 49  **********.** $I
32b2c 64 3a 20 69 63 75 2e 63 2c 76 20 31 2e 37 20 32  d: icu.c,v 1.7 2
32b2d 30 30 37 2f 31 32 2f 31 33 20 32 31 3a 35 34 3a  007/12/13 21:54:
32b2e 31 31 20 64 72 68 20 45 78 70 20 24 0a 2a 2a 0a  11 drh Exp $.**.
32b2f 2a 2a 20 54 68 69 73 20 66 69 6c 65 20 69 6d 70  ** This file imp
32b30 6c 65 6d 65 6e 74 73 20 61 6e 20 69 6e 74 65 67  lements an integ
32b31 72 61 74 69 6f 6e 20 62 65 74 77 65 65 6e 20 74  ration between t
32b32 68 65 20 49 43 55 20 6c 69 62 72 61 72 79 20 0a  he ICU library .
32b33 2a 2a 20 28 22 49 6e 74 65 72 6e 61 74 69 6f 6e  ** ("Internation
32b34 61 6c 20 43 6f 6d 70 6f 6e 65 6e 74 73 20 66 6f  al Components fo
32b35 72 20 55 6e 69 63 6f 64 65 22 2c 20 61 6e 20 6f  r Unicode", an o
32b36 70 65 6e 2d 73 6f 75 72 63 65 20 6c 69 62 72 61  pen-source libra
32b37 72 79 20 0a 2a 2a 20 66 6f 72 20 68 61 6e 64 6c  ry .** for handl
32b38 69 6e 67 20 75 6e 69 63 6f 64 65 20 64 61 74 61  ing unicode data
32b39 29 20 61 6e 64 20 53 51 4c 69 74 65 2e 20 54 68  ) and SQLite. Th
32b3a 65 20 69 6e 74 65 67 72 61 74 69 6f 6e 20 75 73  e integration us
32b3b 65 73 20 0a 2a 2a 20 49 43 55 20 74 6f 20 70 72  es .** ICU to pr
32b3c 6f 76 69 64 65 20 74 68 65 20 66 6f 6c 6c 6f 77  ovide the follow
32b3d 69 6e 67 20 74 6f 20 53 51 4c 69 74 65 3a 0a 2a  ing to SQLite:.*
32b3e 2a 0a 2a 2a 20 20 20 2a 20 41 6e 20 69 6d 70 6c  *.**   * An impl
32b3f 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68  ementation of th
32b40 65 20 53 51 4c 20 72 65 67 65 78 70 28 29 20 66  e SQL regexp() f
32b41 75 6e 63 74 69 6f 6e 20 28 61 6e 64 20 68 65 6e  unction (and hen
32b42 63 65 20 52 45 47 45 58 50 0a 2a 2a 20 20 20 20  ce REGEXP.**    
32b43 20 6f 70 65 72 61 74 6f 72 29 20 75 73 69 6e 67   operator) using
32b44 20 74 68 65 20 49 43 55 20 75 72 65 67 65 78 5f   the ICU uregex_
32b45 58 58 28 29 20 41 50 49 73 2e 0a 2a 2a 0a 2a 2a  XX() APIs..**.**
32b46 20 20 20 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74     * Implementat
32b47 69 6f 6e 73 20 6f 66 20 74 68 65 20 53 51 4c 20  ions of the SQL 
32b48 73 63 61 6c 61 72 20 75 70 70 65 72 28 29 20 61  scalar upper() a
32b49 6e 64 20 6c 6f 77 65 72 28 29 20 66 75 6e 63 74  nd lower() funct
32b4a 69 6f 6e 73 0a 2a 2a 20 20 20 20 20 66 6f 72 20  ions.**     for 
32b4b 63 61 73 65 20 6d 61 70 70 69 6e 67 2e 0a 2a 2a  case mapping..**
32b4c 0a 2a 2a 20 20 20 2a 20 49 6e 74 65 67 72 61 74  .**   * Integrat
32b4d 69 6f 6e 20 6f 66 20 49 43 55 20 61 6e 64 20 53  ion of ICU and S
32b4e 51 4c 69 74 65 20 63 6f 6c 6c 61 74 69 6f 6e 20  QLite collation 
32b4f 73 65 71 65 6e 63 65 73 2e 0a 2a 2a 0a 2a 2a 20  seqences..**.** 
32b50 20 20 2a 20 41 6e 20 69 6d 70 6c 65 6d 65 6e 74    * An implement
32b51 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 4c 49 4b  ation of the LIK
32b52 45 20 6f 70 65 72 61 74 6f 72 20 74 68 61 74 20  E operator that 
32b53 75 73 65 73 20 49 43 55 20 74 6f 20 0a 2a 2a 20  uses ICU to .** 
32b54 20 20 20 20 70 72 6f 76 69 64 65 20 63 61 73 65      provide case
32b55 2d 69 6e 64 65 70 65 6e 64 65 6e 74 20 6d 61 74  -independent mat
32b56 63 68 69 6e 67 2e 0a 2a 2f 0a 0a 23 69 66 20 21  ching..*/..#if !
32b57 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 43  defined(SQLITE_C
32b58 4f 52 45 29 20 7c 7c 20 64 65 66 69 6e 65 64 28  ORE) || defined(
32b59 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45 5f 49 43  SQLITE_ENABLE_IC
32b5a 55 29 0a 0a 2f 2a 20 49 6e 63 6c 75 64 65 20 49  U)../* Include I
32b5b 43 55 20 68 65 61 64 65 72 73 20 2a 2f 0a 23 69  CU headers */.#i
32b5c 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f 64 65 2f  nclude <unicode/
32b5d 75 74 79 70 65 73 2e 68 3e 0a 23 69 6e 63 6c 75  utypes.h>.#inclu
32b5e 64 65 20 3c 75 6e 69 63 6f 64 65 2f 75 72 65 67  de <unicode/ureg
32b5f 65 78 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c  ex.h>.#include <
32b60 75 6e 69 63 6f 64 65 2f 75 73 74 72 69 6e 67 2e  unicode/ustring.
32b61 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69  h>.#include <uni
32b62 63 6f 64 65 2f 75 63 6f 6c 2e 68 3e 0a 0a 0a 23  code/ucol.h>...#
32b63 69 66 6e 64 65 66 20 53 51 4c 49 54 45 5f 43 4f  ifndef SQLITE_CO
32b64 52 45 0a 20 20 53 51 4c 49 54 45 5f 45 58 54 45  RE.  SQLITE_EXTE
32b65 4e 53 49 4f 4e 5f 49 4e 49 54 31 0a 23 65 6c 73  NSION_INIT1.#els
32b66 65 0a 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20  e.#endif../*.** 
32b67 4d 61 78 69 6d 75 6d 20 6c 65 6e 67 74 68 20 28  Maximum length (
32b68 69 6e 20 62 79 74 65 73 29 20 6f 66 20 74 68 65  in bytes) of the
32b69 20 70 61 74 74 65 72 6e 20 69 6e 20 61 20 4c 49   pattern in a LI
32b6a 4b 45 20 6f 72 20 47 4c 4f 42 0a 2a 2a 20 6f 70  KE or GLOB.** op
32b6b 65 72 61 74 6f 72 2e 0a 2a 2f 0a 23 69 66 6e 64  erator..*/.#ifnd
32b6c 65 66 20 53 51 4c 49 54 45 5f 4d 41 58 5f 4c 49  ef SQLITE_MAX_LI
32b6d 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45 4e 47 54  KE_PATTERN_LENGT
32b6e 48 0a 23 20 64 65 66 69 6e 65 20 53 51 4c 49 54  H.# define SQLIT
32b6f 45 5f 4d 41 58 5f 4c 49 4b 45 5f 50 41 54 54 45  E_MAX_LIKE_PATTE
32b70 52 4e 5f 4c 45 4e 47 54 48 20 35 30 30 30 30 0a  RN_LENGTH 50000.
32b71 23 65 6e 64 69 66 0a 0a 2f 2a 0a 2a 2a 20 56 65  #endif../*.** Ve
32b72 72 73 69 6f 6e 20 6f 66 20 73 71 6c 69 74 65 33  rsion of sqlite3
32b73 5f 66 72 65 65 28 29 20 74 68 61 74 20 69 73 20  _free() that is 
32b74 61 6c 77 61 79 73 20 61 20 66 75 6e 63 74 69 6f  always a functio
32b75 6e 2c 20 6e 65 76 65 72 20 61 20 6d 61 63 72 6f  n, never a macro
32b76 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
32b77 20 78 46 72 65 65 28 76 6f 69 64 20 2a 70 29 7b   xFree(void *p){
32b78 0a 20 20 73 71 6c 69 74 65 33 5f 66 72 65 65 28  .  sqlite3_free(
32b79 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d  p);.}../*.** Com
32b7a 70 61 72 65 20 74 77 6f 20 55 54 46 2d 38 20 73  pare two UTF-8 s
32b7b 74 72 69 6e 67 73 20 66 6f 72 20 65 71 75 61 6c  trings for equal
32b7c 69 74 79 20 77 68 65 72 65 20 74 68 65 20 66 69  ity where the fi
32b7d 72 73 74 20 73 74 72 69 6e 67 20 69 73 0a 2a 2a  rst string is.**
32b7e 20 61 20 22 4c 49 4b 45 22 20 65 78 70 72 65 73   a "LIKE" expres
32b7f 73 69 6f 6e 2e 20 52 65 74 75 72 6e 20 74 72 75  sion. Return tru
32b80 65 20 28 31 29 20 69 66 20 74 68 65 79 20 61 72  e (1) if they ar
32b81 65 20 74 68 65 20 73 61 6d 65 20 61 6e 64 20 0a  e the same and .
32b82 2a 2a 20 66 61 6c 73 65 20 28 30 29 20 69 66 20  ** false (0) if 
32b83 74 68 65 79 20 61 72 65 20 64 69 66 66 65 72 65  they are differe
32b84 6e 74 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 69 6e  nt..*/.static in
32b85 74 20 69 63 75 4c 69 6b 65 43 6f 6d 70 61 72 65  t icuLikeCompare
32b86 28 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f  (.  const uint8_
32b87 74 20 2a 7a 50 61 74 74 65 72 6e 2c 20 20 20 2f  t *zPattern,   /
32b88 2a 20 4c 49 4b 45 20 70 61 74 74 65 72 6e 20 2a  * LIKE pattern *
32b89 2f 0a 20 20 63 6f 6e 73 74 20 75 69 6e 74 38 5f  /.  const uint8_
32b8a 74 20 2a 7a 53 74 72 69 6e 67 2c 20 20 20 20 2f  t *zString,    /
32b8b 2a 20 54 68 65 20 55 54 46 2d 38 20 73 74 72 69  * The UTF-8 stri
32b8c 6e 67 20 74 6f 20 63 6f 6d 70 61 72 65 20 61 67  ng to compare ag
32b8d 61 69 6e 73 74 20 2a 2f 0a 20 20 63 6f 6e 73 74  ainst */.  const
32b8e 20 55 43 68 61 72 33 32 20 75 45 73 63 20 20 20   UChar32 uEsc   
32b8f 20 20 20 20 20 20 2f 2a 20 54 68 65 20 65 73 63        /* The esc
32b90 61 70 65 20 63 68 61 72 61 63 74 65 72 20 2a 2f  ape character */
32b91 0a 29 7b 0a 20 20 73 74 61 74 69 63 20 63 6f 6e  .){.  static con
32b92 73 74 20 69 6e 74 20 4d 41 54 43 48 5f 4f 4e 45  st int MATCH_ONE
32b93 20 3d 20 28 55 43 68 61 72 33 32 29 27 5f 27 3b   = (UChar32)'_';
32b94 0a 20 20 73 74 61 74 69 63 20 63 6f 6e 73 74 20  .  static const 
32b95 69 6e 74 20 4d 41 54 43 48 5f 41 4c 4c 20 3d 20  int MATCH_ALL = 
32b96 28 55 43 68 61 72 33 32 29 27 25 27 3b 0a 0a 20  (UChar32)'%';.. 
32b97 20 69 6e 74 20 69 50 61 74 74 65 72 6e 20 3d 20   int iPattern = 
32b98 30 3b 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72  0;       /* Curr
32b99 65 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20 69  ent byte index i
32b9a 6e 20 7a 50 61 74 74 65 72 6e 20 2a 2f 0a 20 20  n zPattern */.  
32b9b 69 6e 74 20 69 53 74 72 69 6e 67 20 3d 20 30 3b  int iString = 0;
32b9c 20 20 20 20 20 20 20 20 2f 2a 20 43 75 72 72 65          /* Curre
32b9d 6e 74 20 62 79 74 65 20 69 6e 64 65 78 20 69 6e  nt byte index in
32b9e 20 7a 53 74 72 69 6e 67 20 2a 2f 0a 0a 20 20 69   zString */..  i
32b9f 6e 74 20 70 72 65 76 45 73 63 61 70 65 20 3d 20  nt prevEscape = 
32ba0 30 3b 20 20 20 20 20 2f 2a 20 54 72 75 65 20 69  0;     /* True i
32ba1 66 20 74 68 65 20 70 72 65 76 69 6f 75 73 20 63  f the previous c
32ba2 68 61 72 61 63 74 65 72 20 77 61 73 20 75 45 73  haracter was uEs
32ba3 63 20 2a 2f 0a 0a 20 20 77 68 69 6c 65 28 20 7a  c */..  while( z
32ba4 50 61 74 74 65 72 6e 5b 69 50 61 74 74 65 72 6e  Pattern[iPattern
32ba5 5d 21 3d 30 20 29 7b 0a 0a 20 20 20 20 2f 2a 20  ]!=0 ){..    /* 
32ba6 52 65 61 64 20 28 61 6e 64 20 63 6f 6e 73 75 6d  Read (and consum
32ba7 65 29 20 74 68 65 20 6e 65 78 74 20 63 68 61 72  e) the next char
32ba8 61 63 74 65 72 20 66 72 6f 6d 20 74 68 65 20 69  acter from the i
32ba9 6e 70 75 74 20 70 61 74 74 65 72 6e 2e 20 2a 2f  nput pattern. */
32baa 0a 20 20 20 20 55 43 68 61 72 33 32 20 75 50 61  .    UChar32 uPa
32bab 74 74 65 72 6e 3b 0a 20 20 20 20 55 38 5f 4e 45  ttern;.    U8_NE
32bac 58 54 5f 55 4e 53 41 46 45 28 7a 50 61 74 74 65  XT_UNSAFE(zPatte
32bad 72 6e 2c 20 69 50 61 74 74 65 72 6e 2c 20 75 50  rn, iPattern, uP
32bae 61 74 74 65 72 6e 29 3b 0a 20 20 20 20 61 73 73  attern);.    ass
32baf 65 72 74 28 75 50 61 74 74 65 72 6e 21 3d 30 29  ert(uPattern!=0)
32bb0 3b 0a 0a 20 20 20 20 2f 2a 20 54 68 65 72 65 20  ;..    /* There 
32bb1 61 72 65 20 6e 6f 77 20 34 20 70 6f 73 73 69 62  are now 4 possib
32bb2 69 6c 69 74 69 65 73 3a 0a 20 20 20 20 2a 2a 0a  ilities:.    **.
32bb3 20 20 20 20 2a 2a 20 20 20 20 20 31 2e 20 75 50      **     1. uP
32bb4 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65  attern is an une
32bb5 73 63 61 70 65 64 20 6d 61 74 63 68 2d 61 6c 6c  scaped match-all
32bb6 20 63 68 61 72 61 63 74 65 72 20 22 25 22 2c 0a   character "%",.
32bb7 20 20 20 20 2a 2a 20 20 20 20 20 32 2e 20 75 50      **     2. uP
32bb8 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65  attern is an une
32bb9 73 63 61 70 65 64 20 6d 61 74 63 68 2d 6f 6e 65  scaped match-one
32bba 20 63 68 61 72 61 63 74 65 72 20 22 5f 22 2c 0a   character "_",.
32bbb 20 20 20 20 2a 2a 20 20 20 20 20 33 2e 20 75 50      **     3. uP
32bbc 61 74 74 65 72 6e 20 69 73 20 61 6e 20 75 6e 65  attern is an une
32bbd 73 63 61 70 65 64 20 65 73 63 61 70 65 20 63 68  scaped escape ch
32bbe 61 72 61 63 74 65 72 2c 20 6f 72 0a 20 20 20 20  aracter, or.    
32bbf 2a 2a 20 20 20 20 20 34 2e 20 75 50 61 74 74 65  **     4. uPatte
32bc0 72 6e 20 69 73 20 74 6f 20 62 65 20 68 61 6e 64  rn is to be hand
32bc1 6c 65 64 20 61 73 20 61 6e 20 6f 72 64 69 6e 61  led as an ordina
32bc2 72 79 20 63 68 61 72 61 63 74 65 72 0a 20 20 20  ry character.   
32bc3 20 2a 2f 0a 20 20 20 20 69 66 28 20 21 70 72 65   */.    if( !pre
32bc4 76 45 73 63 61 70 65 20 26 26 20 75 50 61 74 74  vEscape && uPatt
32bc5 65 72 6e 3d 3d 4d 41 54 43 48 5f 41 4c 4c 20 29  ern==MATCH_ALL )
32bc6 7b 0a 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20  {.      /* Case 
32bc7 31 2e 20 2a 2f 0a 20 20 20 20 20 20 75 69 6e 74  1. */.      uint
32bc8 38 5f 74 20 63 3b 0a 0a 20 20 20 20 20 20 2f 2a  8_t c;..      /*
32bc9 20 53 6b 69 70 20 61 6e 79 20 4d 41 54 43 48 5f   Skip any MATCH_
32bca 41 4c 4c 20 6f 72 20 4d 41 54 43 48 5f 4f 4e 45  ALL or MATCH_ONE
32bcb 20 63 68 61 72 61 63 74 65 72 73 20 74 68 61 74   characters that
32bcc 20 66 6f 6c 6c 6f 77 20 61 0a 20 20 20 20 20 20   follow a.      
32bcd 2a 2a 20 4d 41 54 43 48 5f 41 4c 4c 2e 20 46 6f  ** MATCH_ALL. Fo
32bce 72 20 65 61 63 68 20 4d 41 54 43 48 5f 4f 4e 45  r each MATCH_ONE
32bcf 2c 20 73 6b 69 70 20 6f 6e 65 20 63 68 61 72 61  , skip one chara
32bd0 63 74 65 72 20 69 6e 20 74 68 65 20 0a 20 20 20  cter in the .   
32bd1 20 20 20 2a 2a 20 74 65 73 74 20 73 74 72 69 6e     ** test strin
32bd2 67 2e 0a 20 20 20 20 20 20 2a 2f 0a 20 20 20 20  g..      */.    
32bd3 20 20 77 68 69 6c 65 28 20 28 63 3d 7a 50 61 74    while( (c=zPat
32bd4 74 65 72 6e 5b 69 50 61 74 74 65 72 6e 5d 29 20  tern[iPattern]) 
32bd5 3d 3d 20 4d 41 54 43 48 5f 41 4c 4c 20 7c 7c 20  == MATCH_ALL || 
32bd6 63 20 3d 3d 20 4d 41 54 43 48 5f 4f 4e 45 20 29  c == MATCH_ONE )
32bd7 7b 0a 20 20 20 20 20 20 20 20 69 66 28 20 63 3d  {.        if( c=
32bd8 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a 20 20  =MATCH_ONE ){.  
32bd9 20 20 20 20 20 20 20 20 69 66 28 20 7a 53 74 72          if( zStr
32bda 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d 30 20  ing[iString]==0 
32bdb 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 20 20  ) return 0;.    
32bdc 20 20 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55        U8_FWD_1_U
32bdd 4e 53 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69  NSAFE(zString, i
32bde 53 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 20  String);.       
32bdf 20 7d 0a 20 20 20 20 20 20 20 20 69 50 61 74 74   }.        iPatt
32be0 65 72 6e 2b 2b 3b 0a 20 20 20 20 20 20 7d 0a 0a  ern++;.      }..
32be1 20 20 20 20 20 20 69 66 28 20 7a 50 61 74 74 65        if( zPatte
32be2 72 6e 5b 69 50 61 74 74 65 72 6e 5d 3d 3d 30 20  rn[iPattern]==0 
32be3 29 20 72 65 74 75 72 6e 20 31 3b 0a 0a 20 20 20  ) return 1;..   
32be4 20 20 20 77 68 69 6c 65 28 20 7a 53 74 72 69 6e     while( zStrin
32be5 67 5b 69 53 74 72 69 6e 67 5d 20 29 7b 0a 20 20  g[iString] ){.  
32be6 20 20 20 20 20 20 69 66 28 20 69 63 75 4c 69 6b        if( icuLik
32be7 65 43 6f 6d 70 61 72 65 28 26 7a 50 61 74 74 65  eCompare(&zPatte
32be8 72 6e 5b 69 50 61 74 74 65 72 6e 5d 2c 20 26 7a  rn[iPattern], &z
32be9 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 2c  String[iString],
32bea 20 75 45 73 63 29 20 29 7b 0a 20 20 20 20 20 20   uEsc) ){.      
32beb 20 20 20 20 72 65 74 75 72 6e 20 31 3b 0a 20 20      return 1;.  
32bec 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 20        }.        
32bed 55 38 5f 46 57 44 5f 31 5f 55 4e 53 41 46 45 28  U8_FWD_1_UNSAFE(
32bee 7a 53 74 72 69 6e 67 2c 20 69 53 74 72 69 6e 67  zString, iString
32bef 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  );.      }.     
32bf0 20 72 65 74 75 72 6e 20 30 3b 0a 0a 20 20 20 20   return 0;..    
32bf1 7d 65 6c 73 65 20 69 66 28 20 21 70 72 65 76 45  }else if( !prevE
32bf2 73 63 61 70 65 20 26 26 20 75 50 61 74 74 65 72  scape && uPatter
32bf3 6e 3d 3d 4d 41 54 43 48 5f 4f 4e 45 20 29 7b 0a  n==MATCH_ONE ){.
32bf4 20 20 20 20 20 20 2f 2a 20 43 61 73 65 20 32 2e        /* Case 2.
32bf5 20 2a 2f 0a 20 20 20 20 20 20 69 66 28 20 7a 53   */.      if( zS
32bf6 74 72 69 6e 67 5b 69 53 74 72 69 6e 67 5d 3d 3d  tring[iString]==
32bf7 30 20 29 20 72 65 74 75 72 6e 20 30 3b 0a 20 20  0 ) return 0;.  
32bf8 20 20 20 20 55 38 5f 46 57 44 5f 31 5f 55 4e 53      U8_FWD_1_UNS
32bf9 41 46 45 28 7a 53 74 72 69 6e 67 2c 20 69 53 74  AFE(zString, iSt
32bfa 72 69 6e 67 29 3b 0a 0a 20 20 20 20 7d 65 6c 73  ring);..    }els
32bfb 65 20 69 66 28 20 21 70 72 65 76 45 73 63 61 70  e if( !prevEscap
32bfc 65 20 26 26 20 75 50 61 74 74 65 72 6e 3d 3d 75  e && uPattern==u
32bfd 45 73 63 29 7b 0a 20 20 20 20 20 20 2f 2a 20 43  Esc){.      /* C
32bfe 61 73 65 20 33 2e 20 2a 2f 0a 20 20 20 20 20 20  ase 3. */.      
32bff 70 72 65 76 45 73 63 61 70 65 20 3d 20 31 3b 0a  prevEscape = 1;.
32c00 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20 20 20  .    }else{.    
32c01 20 20 2f 2a 20 43 61 73 65 20 34 2e 20 2a 2f 0a    /* Case 4. */.
32c02 20 20 20 20 20 20 55 43 68 61 72 33 32 20 75 53        UChar32 uS
32c03 74 72 69 6e 67 3b 0a 20 20 20 20 20 20 55 38 5f  tring;.      U8_
32c04 4e 45 58 54 5f 55 4e 53 41 46 45 28 7a 53 74 72  NEXT_UNSAFE(zStr
32c05 69 6e 67 2c 20 69 53 74 72 69 6e 67 2c 20 75 53  ing, iString, uS
32c06 74 72 69 6e 67 29 3b 0a 20 20 20 20 20 20 75 53  tring);.      uS
32c07 74 72 69 6e 67 20 3d 20 75 5f 66 6f 6c 64 43 61  tring = u_foldCa
32c08 73 65 28 75 53 74 72 69 6e 67 2c 20 55 5f 46 4f  se(uString, U_FO
32c09 4c 44 5f 43 41 53 45 5f 44 45 46 41 55 4c 54 29  LD_CASE_DEFAULT)
32c0a 3b 0a 20 20 20 20 20 20 75 50 61 74 74 65 72 6e  ;.      uPattern
32c0b 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65 28 75 50   = u_foldCase(uP
32c0c 61 74 74 65 72 6e 2c 20 55 5f 46 4f 4c 44 5f 43  attern, U_FOLD_C
32c0d 41 53 45 5f 44 45 46 41 55 4c 54 29 3b 0a 20 20  ASE_DEFAULT);.  
32c0e 20 20 20 20 69 66 28 20 75 53 74 72 69 6e 67 21      if( uString!
32c0f 3d 75 50 61 74 74 65 72 6e 20 29 7b 0a 20 20 20  =uPattern ){.   
32c10 20 20 20 20 20 72 65 74 75 72 6e 20 30 3b 0a 20       return 0;. 
32c11 20 20 20 20 20 7d 0a 20 20 20 20 20 20 70 72 65       }.      pre
32c12 76 45 73 63 61 70 65 20 3d 20 30 3b 0a 20 20 20  vEscape = 0;.   
32c13 20 7d 0a 20 20 7d 0a 0a 20 20 72 65 74 75 72 6e   }.  }..  return
32c14 20 7a 53 74 72 69 6e 67 5b 69 53 74 72 69 6e 67   zString[iString
32c15 5d 3d 3d 30 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 49  ]==0;.}../*.** I
32c16 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e 20 6f 66  mplementation of
32c17 20 74 68 65 20 6c 69 6b 65 28 29 20 53 51 4c 20   the like() SQL 
32c18 66 75 6e 63 74 69 6f 6e 2e 20 20 54 68 69 73 20  function.  This 
32c19 66 75 6e 63 74 69 6f 6e 20 69 6d 70 6c 65 6d 65  function impleme
32c1a 6e 74 73 0a 2a 2a 20 74 68 65 20 62 75 69 6c 64  nts.** the build
32c1b 2d 69 6e 20 4c 49 4b 45 20 6f 70 65 72 61 74 6f  -in LIKE operato
32c1c 72 2e 20 20 54 68 65 20 66 69 72 73 74 20 61 72  r.  The first ar
32c1d 67 75 6d 65 6e 74 20 74 6f 20 74 68 65 20 66 75  gument to the fu
32c1e 6e 63 74 69 6f 6e 20 69 73 20 74 68 65 0a 2a 2a  nction is the.**
32c1f 20 70 61 74 74 65 72 6e 20 61 6e 64 20 74 68 65   pattern and the
32c20 20 73 65 63 6f 6e 64 20 61 72 67 75 6d 65 6e 74   second argument
32c21 20 69 73 20 74 68 65 20 73 74 72 69 6e 67 2e 20   is the string. 
32c22 20 53 6f 2c 20 74 68 65 20 53 51 4c 20 73 74 61   So, the SQL sta
32c23 74 65 6d 65 6e 74 73 3a 0a 2a 2a 0a 2a 2a 20 20  tements:.**.**  
32c24 20 20 20 20 20 41 20 4c 49 4b 45 20 42 0a 2a 2a       A LIKE B.**
32c25 0a 2a 2a 20 69 73 20 69 6d 70 6c 65 6d 65 6e 74  .** is implement
32c26 65 64 20 61 73 20 6c 69 6b 65 28 42 2c 20 41 29  ed as like(B, A)
32c27 2e 20 49 66 20 74 68 65 72 65 20 69 73 20 61 6e  . If there is an
32c28 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65   escape characte
32c29 72 20 45 2c 20 0a 2a 2a 0a 2a 2a 20 20 20 20 20  r E, .**.**     
32c2a 20 20 41 20 4c 49 4b 45 20 42 20 45 53 43 41 50    A LIKE B ESCAP
32c2b 45 20 45 0a 2a 2a 0a 2a 2a 20 69 73 20 6d 61 70  E E.**.** is map
32c2c 70 65 64 20 74 6f 20 6c 69 6b 65 28 42 2c 20 41  ped to like(B, A
32c2d 2c 20 45 29 2e 0a 2a 2f 0a 73 74 61 74 69 63 20  , E)..*/.static 
32c2e 76 6f 69 64 20 69 63 75 4c 69 6b 65 46 75 6e 63  void icuLikeFunc
32c2f 28 0a 20 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74  (.  sqlite3_cont
32c30 65 78 74 20 2a 63 6f 6e 74 65 78 74 2c 20 0a 20  ext *context, . 
32c31 20 69 6e 74 20 61 72 67 63 2c 20 0a 20 20 73 71   int argc, .  sq
32c32 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a 61 72  lite3_value **ar
32c33 67 76 0a 29 7b 0a 20 20 63 6f 6e 73 74 20 75 6e  gv.){.  const un
32c34 73 69 67 6e 65 64 20 63 68 61 72 20 2a 7a 41 20  signed char *zA 
32c35 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  = sqlite3_value_
32c36 74 65 78 74 28 61 72 67 76 5b 30 5d 29 3b 0a 20  text(argv[0]);. 
32c37 20 63 6f 6e 73 74 20 75 6e 73 69 67 6e 65 64 20   const unsigned 
32c38 63 68 61 72 20 2a 7a 42 20 3d 20 73 71 6c 69 74  char *zB = sqlit
32c39 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 72  e3_value_text(ar
32c3a 67 76 5b 31 5d 29 3b 0a 20 20 55 43 68 61 72 33  gv[1]);.  UChar3
32c3b 32 20 75 45 73 63 20 3d 20 30 3b 0a 0a 20 20 2f  2 uEsc = 0;..  /
32c3c 2a 20 4c 69 6d 69 74 20 74 68 65 20 6c 65 6e 67  * Limit the leng
32c3d 74 68 20 6f 66 20 74 68 65 20 4c 49 4b 45 20 6f  th of the LIKE o
32c3e 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20 74  r GLOB pattern t
32c3f 6f 20 61 76 6f 69 64 20 70 72 6f 62 6c 65 6d 73  o avoid problems
32c40 0a 20 20 2a 2a 20 6f 66 20 64 65 65 70 20 72 65  .  ** of deep re
32c41 63 75 72 73 69 6f 6e 20 61 6e 64 20 4e 2a 4e 20  cursion and N*N 
32c42 62 65 68 61 76 69 6f 72 20 69 6e 20 70 61 74 74  behavior in patt
32c43 65 72 6e 43 6f 6d 70 61 72 65 28 29 2e 0a 20 20  ernCompare()..  
32c44 2a 2f 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33  */.  if( sqlite3
32c45 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
32c46 76 5b 30 5d 29 3e 53 51 4c 49 54 45 5f 4d 41 58  v[0])>SQLITE_MAX
32c47 5f 4c 49 4b 45 5f 50 41 54 54 45 52 4e 5f 4c 45  _LIKE_PATTERN_LE
32c48 4e 47 54 48 20 29 7b 0a 20 20 20 20 73 71 6c 69  NGTH ){.    sqli
32c49 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f 72  te3_result_error
32c4a 28 63 6f 6e 74 65 78 74 2c 20 22 4c 49 4b 45 20  (context, "LIKE 
32c4b 6f 72 20 47 4c 4f 42 20 70 61 74 74 65 72 6e 20  or GLOB pattern 
32c4c 74 6f 6f 20 63 6f 6d 70 6c 65 78 22 2c 20 2d 31  too complex", -1
32c4d 29 3b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20  );.    return;. 
32c4e 20 7d 0a 0a 0a 20 20 69 66 28 20 61 72 67 63 3d   }...  if( argc=
32c4f 3d 33 20 29 7b 0a 20 20 20 20 2f 2a 20 54 68 65  =3 ){.    /* The
32c50 20 65 73 63 61 70 65 20 63 68 61 72 61 63 74 65   escape characte
32c51 72 20 73 74 72 69 6e 67 20 6d 75 73 74 20 63 6f  r string must co
32c52 6e 73 69 73 74 20 6f 66 20 61 20 73 69 6e 67 6c  nsist of a singl
32c53 65 20 55 54 46 2d 38 20 63 68 61 72 61 63 74 65  e UTF-8 characte
32c54 72 2e 0a 20 20 20 20 2a 2a 20 4f 74 68 65 72 77  r..    ** Otherw
32c55 69 73 65 2c 20 72 65 74 75 72 6e 20 61 6e 20 65  ise, return an e
32c56 72 72 6f 72 2e 0a 20 20 20 20 2a 2f 0a 20 20 20  rror..    */.   
32c57 20 69 6e 74 20 6e 45 3d 20 73 71 6c 69 74 65 33   int nE= sqlite3
32c58 5f 76 61 6c 75 65 5f 62 79 74 65 73 28 61 72 67  _value_bytes(arg
32c59 76 5b 32 5d 29 3b 0a 20 20 20 20 63 6f 6e 73 74  v[2]);.    const
32c5a 20 75 6e 73 69 67 6e 65 64 20 63 68 61 72 20 2a   unsigned char *
32c5b 7a 45 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c  zE = sqlite3_val
32c5c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 32 5d 29  ue_text(argv[2])
32c5d 3b 0a 20 20 20 20 69 6e 74 20 69 20 3d 20 30 3b  ;.    int i = 0;
32c5e 0a 20 20 20 20 69 66 28 20 7a 45 3d 3d 30 20 29  .    if( zE==0 )
32c5f 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 55 38 5f   return;.    U8_
32c60 4e 45 58 54 28 7a 45 2c 20 69 2c 20 6e 45 2c 20  NEXT(zE, i, nE, 
32c61 75 45 73 63 29 3b 0a 20 20 20 20 69 66 28 20 69  uEsc);.    if( i
32c62 21 3d 6e 45 29 7b 0a 20 20 20 20 20 20 73 71 6c  !=nE){.      sql
32c63 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72 6f  ite3_result_erro
32c64 72 28 63 6f 6e 74 65 78 74 2c 20 0a 20 20 20 20  r(context, .    
32c65 20 20 20 20 20 20 22 45 53 43 41 50 45 20 65 78        "ESCAPE ex
32c66 70 72 65 73 73 69 6f 6e 20 6d 75 73 74 20 62 65  pression must be
32c67 20 61 20 73 69 6e 67 6c 65 20 63 68 61 72 61 63   a single charac
32c68 74 65 72 22 2c 20 2d 31 29 3b 0a 20 20 20 20 20  ter", -1);.     
32c69 20 72 65 74 75 72 6e 3b 0a 20 20 20 20 7d 0a 20   return;.    }. 
32c6a 20 7d 0a 0a 20 20 69 66 28 20 7a 41 20 26 26 20   }..  if( zA && 
32c6b 7a 42 20 29 7b 0a 20 20 20 20 73 71 6c 69 74 65  zB ){.    sqlite
32c6c 33 5f 72 65 73 75 6c 74 5f 69 6e 74 28 63 6f 6e  3_result_int(con
32c6d 74 65 78 74 2c 20 69 63 75 4c 69 6b 65 43 6f 6d  text, icuLikeCom
32c6e 70 61 72 65 28 7a 41 2c 20 7a 42 2c 20 75 45 73  pare(zA, zB, uEs
32c6f 63 29 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a  c));.  }.}../*.*
32c70 2a 20 54 68 69 73 20 66 75 6e 63 74 69 6f 6e 20  * This function 
32c71 69 73 20 63 61 6c 6c 65 64 20 77 68 65 6e 20 61  is called when a
32c72 6e 20 49 43 55 20 66 75 6e 63 74 69 6f 6e 20 63  n ICU function c
32c73 61 6c 6c 65 64 20 66 72 6f 6d 20 77 69 74 68 69  alled from withi
32c74 6e 0a 2a 2a 20 74 68 65 20 69 6d 70 6c 65 6d 65  n.** the impleme
32c75 6e 74 61 74 69 6f 6e 20 6f 66 20 61 6e 20 53 51  ntation of an SQ
32c76 4c 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  L scalar functio
32c77 6e 20 72 65 74 75 72 6e 73 20 61 6e 20 65 72 72  n returns an err
32c78 6f 72 2e 0a 2a 2a 0a 2a 2a 20 54 68 65 20 73 63  or..**.** The sc
32c79 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e 20 63 6f  alar function co
32c7a 6e 74 65 78 74 20 70 61 73 73 65 64 20 61 73 20  ntext passed as 
32c7b 74 68 65 20 66 69 72 73 74 20 61 72 67 75 6d 65  the first argume
32c7c 6e 74 20 69 73 20 0a 2a 2a 20 6c 6f 61 64 65 64  nt is .** loaded
32c7d 20 77 69 74 68 20 61 6e 20 65 72 72 6f 72 20 6d   with an error m
32c7e 65 73 73 61 67 65 20 62 61 73 65 64 20 6f 6e 20  essage based on 
32c7f 74 68 65 20 66 6f 6c 6c 6f 77 69 6e 67 20 74 77  the following tw
32c80 6f 20 61 72 67 73 2e 0a 2a 2f 0a 73 74 61 74 69  o args..*/.stati
32c81 63 20 76 6f 69 64 20 69 63 75 46 75 6e 63 74 69  c void icuFuncti
32c82 6f 6e 45 72 72 6f 72 28 0a 20 20 73 71 6c 69 74  onError(.  sqlit
32c83 65 33 5f 63 6f 6e 74 65 78 74 20 2a 70 43 74 78  e3_context *pCtx
32c84 2c 20 20 20 20 20 20 20 2f 2a 20 53 51 4c 69 74  ,       /* SQLit
32c85 65 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f  e scalar functio
32c86 6e 20 63 6f 6e 74 65 78 74 20 2a 2f 0a 20 20 63  n context */.  c
32c87 6f 6e 73 74 20 63 68 61 72 20 2a 7a 4e 61 6d 65  onst char *zName
32c88 2c 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e  ,           /* N
32c89 61 6d 65 20 6f 66 20 49 43 55 20 66 75 6e 63 74  ame of ICU funct
32c8a 69 6f 6e 20 74 68 61 74 20 66 61 69 6c 65 64 20  ion that failed 
32c8b 2a 2f 0a 20 20 55 45 72 72 6f 72 43 6f 64 65 20  */.  UErrorCode 
32c8c 65 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e               
32c8d 20 20 2f 2a 20 45 72 72 6f 72 20 63 6f 64 65 20    /* Error code 
32c8e 72 65 74 75 72 6e 65 64 20 62 79 20 49 43 55 20  returned by ICU 
32c8f 66 75 6e 63 74 69 6f 6e 20 2a 2f 0a 29 7b 0a 20  function */.){. 
32c90 20 63 68 61 72 20 7a 42 75 66 5b 31 32 38 5d 3b   char zBuf[128];
32c91 0a 20 20 73 71 6c 69 74 65 33 5f 73 6e 70 72 69  .  sqlite3_snpri
32c92 6e 74 66 28 31 32 38 2c 20 7a 42 75 66 2c 20 22  ntf(128, zBuf, "
32c93 49 43 55 20 65 72 72 6f 72 3a 20 25 73 28 29 3a  ICU error: %s():
32c94 20 25 73 22 2c 20 7a 4e 61 6d 65 2c 20 75 5f 65   %s", zName, u_e
32c95 72 72 6f 72 4e 61 6d 65 28 65 29 29 3b 0a 20 20  rrorName(e));.  
32c96 7a 42 75 66 5b 31 32 37 5d 20 3d 20 27 5c 30 27  zBuf[127] = '\0'
32c97 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73 75  ;.  sqlite3_resu
32c98 6c 74 5f 65 72 72 6f 72 28 70 43 74 78 2c 20 7a  lt_error(pCtx, z
32c99 42 75 66 2c 20 2d 31 29 3b 0a 7d 0a 0a 2f 2a 0a  Buf, -1);.}../*.
32c9a 2a 2a 20 46 75 6e 63 74 69 6f 6e 20 74 6f 20 64  ** Function to d
32c9b 65 6c 65 74 65 20 63 6f 6d 70 69 6c 65 64 20 72  elete compiled r
32c9c 65 67 65 78 70 20 6f 62 6a 65 63 74 73 2e 20 52  egexp objects. R
32c9d 65 67 69 73 74 65 72 65 64 20 61 73 0a 2a 2a 20  egistered as.** 
32c9e 61 20 64 65 73 74 72 75 63 74 6f 72 20 66 75 6e  a destructor fun
32c9f 63 74 69 6f 6e 20 77 69 74 68 20 73 71 6c 69 74  ction with sqlit
32ca0 65 33 5f 73 65 74 5f 61 75 78 64 61 74 61 28 29  e3_set_auxdata()
32ca1 2e 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64  ..*/.static void
32ca2 20 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65   icuRegexpDelete
32ca3 28 76 6f 69 64 20 2a 70 29 7b 0a 20 20 55 52 65  (void *p){.  URe
32ca4 67 75 6c 61 72 45 78 70 72 65 73 73 69 6f 6e 20  gularExpression 
32ca5 2a 70 45 78 70 72 20 3d 20 28 55 52 65 67 75 6c  *pExpr = (URegul
32ca6 61 72 45 78 70 72 65 73 73 69 6f 6e 20 2a 29 70  arExpression *)p
32ca7 3b 0a 20 20 75 72 65 67 65 78 5f 63 6c 6f 73 65  ;.  uregex_close
32ca8 28 70 45 78 70 72 29 3b 0a 7d 0a 0a 2f 2a 0a 2a  (pExpr);.}../*.*
32ca9 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
32caa 20 6f 66 20 53 51 4c 69 74 65 20 52 45 47 45 58   of SQLite REGEX
32cab 50 20 6f 70 65 72 61 74 6f 72 2e 20 54 68 69 73  P operator. This
32cac 20 73 63 61 6c 61 72 20 66 75 6e 63 74 69 6f 6e   scalar function
32cad 20 74 61 6b 65 73 0a 2a 2a 20 74 77 6f 20 61 72   takes.** two ar
32cae 67 75 6d 65 6e 74 73 2e 20 54 68 65 20 66 69 72  guments. The fir
32caf 73 74 20 69 73 20 61 20 72 65 67 75 6c 61 72 20  st is a regular 
32cb0 65 78 70 72 65 73 73 69 6f 6e 20 70 61 74 74 65  expression patte
32cb1 72 6e 20 74 6f 20 63 6f 6d 70 69 6c 65 0a 2a 2a  rn to compile.**
32cb2 20 74 68 65 20 73 65 63 6f 6e 64 20 69 73 20 61   the second is a
32cb3 20 73 74 72 69 6e 67 20 74 6f 20 6d 61 74 63 68   string to match
32cb4 20 61 67 61 69 6e 73 74 20 74 68 61 74 20 70 61   against that pa
32cb5 74 74 65 72 6e 2e 20 49 66 20 65 69 74 68 65 72  ttern. If either
32cb6 20 0a 2a 2a 20 61 72 67 75 6d 65 6e 74 20 69 73   .** argument is
32cb7 20 61 6e 20 53 51 4c 20 4e 55 4c 4c 2c 20 74 68   an SQL NULL, th
32cb8 65 6e 20 4e 55 4c 4c 20 49 73 20 72 65 74 75 72  en NULL Is retur
32cb9 6e 65 64 2e 20 4f 74 68 65 72 77 69 73 65 2c 20  ned. Otherwise, 
32cba 74 68 65 20 72 65 73 75 6c 74 0a 2a 2a 20 69 73  the result.** is
32cbb 20 31 20 69 66 20 74 68 65 20 73 74 72 69 6e 67   1 if the string
32cbc 20 6d 61 74 63 68 65 73 20 74 68 65 20 70 61 74   matches the pat
32cbd 74 65 72 6e 2c 20 6f 72 20 30 20 6f 74 68 65 72  tern, or 0 other
32cbe 77 69 73 65 2e 0a 2a 2a 0a 2a 2a 20 53 51 4c 69  wise..**.** SQLi
32cbf 74 65 20 6d 61 70 73 20 74 68 65 20 72 65 67 65  te maps the rege
32cc0 78 70 28 29 20 66 75 6e 63 74 69 6f 6e 20 74 6f  xp() function to
32cc1 20 74 68 65 20 72 65 67 65 78 70 28 29 20 6f 70   the regexp() op
32cc2 65 72 61 74 6f 72 20 73 75 63 68 0a 2a 2a 20 74  erator such.** t
32cc3 68 61 74 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  hat the followin
32cc4 67 20 74 77 6f 20 61 72 65 20 65 71 75 69 76 61  g two are equiva
32cc5 6c 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20  lent:.**.**     
32cc6 7a 53 74 72 69 6e 67 20 52 45 47 45 58 50 20 7a  zString REGEXP z
32cc7 50 61 74 74 65 72 6e 0a 2a 2a 20 20 20 20 20 72  Pattern.**     r
32cc8 65 67 65 78 70 28 7a 50 61 74 74 65 72 6e 2c 20  egexp(zPattern, 
32cc9 7a 53 74 72 69 6e 67 29 0a 2a 2a 0a 2a 2a 20 55  zString).**.** U
32cca 73 65 73 20 74 68 65 20 66 6f 6c 6c 6f 77 69 6e  ses the followin
32ccb 67 20 49 43 55 20 72 65 67 65 78 70 20 41 50 49  g ICU regexp API
32ccc 73 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75 72 65  s:.**.**     ure
32ccd 67 65 78 5f 6f 70 65 6e 28 29 0a 2a 2a 20 20 20  gex_open().**   
32cce 20 20 75 72 65 67 65 78 5f 6d 61 74 63 68 65 73    uregex_matches
32ccf 28 29 0a 2a 2a 20 20 20 20 20 75 72 65 67 65 78  ().**     uregex
32cd0 5f 63 6c 6f 73 65 28 29 0a 2a 2f 0a 73 74 61 74  _close().*/.stat
32cd1 69 63 20 76 6f 69 64 20 69 63 75 52 65 67 65 78  ic void icuRegex
32cd2 70 46 75 6e 63 28 73 71 6c 69 74 65 33 5f 63 6f  pFunc(sqlite3_co
32cd3 6e 74 65 78 74 20 2a 70 2c 20 69 6e 74 20 6e 41  ntext *p, int nA
32cd4 72 67 2c 20 73 71 6c 69 74 65 33 5f 76 61 6c 75  rg, sqlite3_valu
32cd5 65 20 2a 2a 61 70 41 72 67 29 7b 0a 20 20 55 45  e **apArg){.  UE
32cd6 72 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20  rrorCode status 
32cd7 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a  = U_ZERO_ERROR;.
32cd8 20 20 55 52 65 67 75 6c 61 72 45 78 70 72 65 73    URegularExpres
32cd9 73 69 6f 6e 20 2a 70 45 78 70 72 3b 0a 20 20 55  sion *pExpr;.  U
32cda 42 6f 6f 6c 20 72 65 73 3b 0a 20 20 63 6f 6e 73  Bool res;.  cons
32cdb 74 20 55 43 68 61 72 20 2a 7a 53 74 72 69 6e 67  t UChar *zString
32cdc 20 3d 20 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   = sqlite3_value
32cdd 5f 74 65 78 74 31 36 28 61 70 41 72 67 5b 31 5d  _text16(apArg[1]
32cde 29 3b 0a 0a 20 20 2f 2a 20 49 66 20 74 68 65 20  );..  /* If the 
32cdf 6c 65 66 74 20 68 61 6e 64 20 73 69 64 65 20 6f  left hand side o
32ce0 66 20 74 68 65 20 72 65 67 65 78 70 20 6f 70 65  f the regexp ope
32ce1 72 61 74 6f 72 20 69 73 20 4e 55 4c 4c 2c 20 0a  rator is NULL, .
32ce2 20 20 2a 2a 20 74 68 65 6e 20 74 68 65 20 72 65    ** then the re
32ce3 73 75 6c 74 20 69 73 20 61 6c 73 6f 20 4e 55 4c  sult is also NUL
32ce4 4c 2e 20 0a 20 20 2a 2f 0a 20 20 69 66 28 20 21  L. .  */.  if( !
32ce5 7a 53 74 72 69 6e 67 20 29 7b 0a 20 20 20 20 72  zString ){.    r
32ce6 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20 20 70 45  eturn;.  }..  pE
32ce7 78 70 72 20 3d 20 73 71 6c 69 74 65 33 5f 67 65  xpr = sqlite3_ge
32ce8 74 5f 61 75 78 64 61 74 61 28 70 2c 20 30 29 3b  t_auxdata(p, 0);
32ce9 0a 20 20 69 66 28 20 21 70 45 78 70 72 20 29 7b  .  if( !pExpr ){
32cea 0a 20 20 20 20 63 6f 6e 73 74 20 55 43 68 61 72  .    const UChar
32ceb 20 2a 7a 50 61 74 74 65 72 6e 20 3d 20 73 71 6c   *zPattern = sql
32cec 69 74 65 33 5f 76 61 6c 75 65 5f 74 65 78 74 31  ite3_value_text1
32ced 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 20 20 20  6(apArg[0]);.   
32cee 20 69 66 28 20 21 7a 50 61 74 74 65 72 6e 20 29   if( !zPattern )
32cef 7b 0a 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a  {.      return;.
32cf0 20 20 20 20 7d 0a 20 20 20 20 70 45 78 70 72 20      }.    pExpr 
32cf1 3d 20 75 72 65 67 65 78 5f 6f 70 65 6e 28 7a 50  = uregex_open(zP
32cf2 61 74 74 65 72 6e 2c 20 2d 31 2c 20 30 2c 20 30  attern, -1, 0, 0
32cf3 2c 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20 20  , &status);..   
32cf4 20 69 66 28 20 55 5f 53 55 43 43 45 53 53 28 73   if( U_SUCCESS(s
32cf5 74 61 74 75 73 29 20 29 7b 0a 20 20 20 20 20 20  tatus) ){.      
32cf6 73 71 6c 69 74 65 33 5f 73 65 74 5f 61 75 78 64  sqlite3_set_auxd
32cf7 61 74 61 28 70 2c 20 30 2c 20 70 45 78 70 72 2c  ata(p, 0, pExpr,
32cf8 20 69 63 75 52 65 67 65 78 70 44 65 6c 65 74 65   icuRegexpDelete
32cf9 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20 20  );.    }else{.  
32cfa 20 20 20 20 61 73 73 65 72 74 28 21 70 45 78 70      assert(!pExp
32cfb 72 29 3b 0a 20 20 20 20 20 20 69 63 75 46 75 6e  r);.      icuFun
32cfc 63 74 69 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75  ctionError(p, "u
32cfd 72 65 67 65 78 5f 6f 70 65 6e 22 2c 20 73 74 61  regex_open", sta
32cfe 74 75 73 29 3b 0a 20 20 20 20 20 20 72 65 74 75  tus);.      retu
32cff 72 6e 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 0a 20  rn;.    }.  }.. 
32d00 20 2f 2a 20 43 6f 6e 66 69 67 75 72 65 20 74 68   /* Configure th
32d01 65 20 74 65 78 74 20 74 68 61 74 20 74 68 65 20  e text that the 
32d02 72 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69  regular expressi
32d03 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e 2e 20  on operates on. 
32d04 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65 74 54  */.  uregex_setT
32d05 65 78 74 28 70 45 78 70 72 2c 20 7a 53 74 72 69  ext(pExpr, zStri
32d06 6e 67 2c 20 2d 31 2c 20 26 73 74 61 74 75 73 29  ng, -1, &status)
32d07 3b 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45  ;.  if( !U_SUCCE
32d08 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20  SS(status) ){.  
32d09 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72    icuFunctionErr
32d0a 6f 72 28 70 2c 20 22 75 72 65 67 65 78 5f 73 65  or(p, "uregex_se
32d0b 74 54 65 78 74 22 2c 20 73 74 61 74 75 73 29 3b  tText", status);
32d0c 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
32d0d 0a 0a 20 20 2f 2a 20 41 74 74 65 6d 70 74 20 74  ..  /* Attempt t
32d0e 68 65 20 6d 61 74 63 68 20 2a 2f 0a 20 20 72 65  he match */.  re
32d0f 73 20 3d 20 75 72 65 67 65 78 5f 6d 61 74 63 68  s = uregex_match
32d10 65 73 28 70 45 78 70 72 2c 20 30 2c 20 26 73 74  es(pExpr, 0, &st
32d11 61 74 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f  atus);.  if( !U_
32d12 53 55 43 43 45 53 53 28 73 74 61 74 75 73 29 20  SUCCESS(status) 
32d13 29 7b 0a 20 20 20 20 69 63 75 46 75 6e 63 74 69  ){.    icuFuncti
32d14 6f 6e 45 72 72 6f 72 28 70 2c 20 22 75 72 65 67  onError(p, "ureg
32d15 65 78 5f 6d 61 74 63 68 65 73 22 2c 20 73 74 61  ex_matches", sta
32d16 74 75 73 29 3b 0a 20 20 20 20 72 65 74 75 72 6e  tus);.    return
32d17 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 53 65 74 20  ;.  }..  /* Set 
32d18 74 68 65 20 74 65 78 74 20 74 68 61 74 20 74 68  the text that th
32d19 65 20 72 65 67 75 6c 61 72 20 65 78 70 72 65 73  e regular expres
32d1a 73 69 6f 6e 20 6f 70 65 72 61 74 65 73 20 6f 6e  sion operates on
32d1b 20 74 6f 20 61 20 4e 55 4c 4c 0a 20 20 2a 2a 20   to a NULL.  ** 
32d1c 70 6f 69 6e 74 65 72 2e 20 54 68 69 73 20 69 73  pointer. This is
32d1d 20 6e 6f 74 20 72 65 61 6c 6c 79 20 6e 65 63 65   not really nece
32d1e 73 73 61 72 79 2c 20 62 75 74 20 69 74 20 69 73  ssary, but it is
32d1f 20 74 69 64 69 65 72 20 74 68 61 6e 20 0a 20 20   tidier than .  
32d20 2a 2a 20 6c 65 61 76 69 6e 67 20 74 68 65 20 72  ** leaving the r
32d21 65 67 75 6c 61 72 20 65 78 70 72 65 73 73 69 6f  egular expressio
32d22 6e 20 6f 62 6a 65 63 74 20 63 6f 6e 66 69 67 75  n object configu
32d23 72 65 64 20 77 69 74 68 20 61 6e 20 69 6e 76 61  red with an inva
32d24 6c 69 64 0a 20 20 2a 2a 20 70 6f 69 6e 74 65 72  lid.  ** pointer
32d25 20 61 66 74 65 72 20 74 68 69 73 20 66 75 6e 63   after this func
32d26 74 69 6f 6e 20 72 65 74 75 72 6e 73 2e 0a 20 20  tion returns..  
32d27 2a 2f 0a 20 20 75 72 65 67 65 78 5f 73 65 74 54  */.  uregex_setT
32d28 65 78 74 28 70 45 78 70 72 2c 20 30 2c 20 30 2c  ext(pExpr, 0, 0,
32d29 20 26 73 74 61 74 75 73 29 3b 0a 0a 20 20 2f 2a   &status);..  /*
32d2a 20 52 65 74 75 72 6e 20 31 20 6f 72 20 30 2e 20   Return 1 or 0. 
32d2b 2a 2f 0a 20 20 73 71 6c 69 74 65 33 5f 72 65 73  */.  sqlite3_res
32d2c 75 6c 74 5f 69 6e 74 28 70 2c 20 72 65 73 20 3f  ult_int(p, res ?
32d2d 20 31 20 3a 20 30 29 3b 0a 7d 0a 0a 2f 2a 0a 2a   1 : 0);.}../*.*
32d2e 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69 6f 6e  * Implementation
32d2f 73 20 6f 66 20 73 63 61 6c 61 72 20 66 75 6e 63  s of scalar func
32d30 74 69 6f 6e 73 20 66 6f 72 20 63 61 73 65 20 6d  tions for case m
32d31 61 70 70 69 6e 67 20 2d 20 75 70 70 65 72 28 29  apping - upper()
32d32 20 61 6e 64 20 0a 2a 2a 20 6c 6f 77 65 72 28 29   and .** lower()
32d33 2e 20 46 75 6e 63 74 69 6f 6e 20 75 70 70 65 72  . Function upper
32d34 28 29 20 63 6f 6e 76 65 72 74 73 20 69 74 73 20  () converts its 
32d35 69 6e 70 75 74 20 74 6f 20 75 70 70 65 72 2d 63  input to upper-c
32d36 61 73 65 20 28 41 42 43 29 2e 0a 2a 2a 20 46 75  ase (ABC)..** Fu
32d37 6e 63 74 69 6f 6e 20 6c 6f 77 65 72 28 29 20 63  nction lower() c
32d38 6f 6e 76 65 72 74 73 20 74 6f 20 6c 6f 77 65 72  onverts to lower
32d39 2d 63 61 73 65 20 28 61 62 63 29 2e 0a 2a 2a 0a  -case (abc)..**.
32d3a 2a 2a 20 49 43 55 20 70 72 6f 76 69 64 65 73 20  ** ICU provides 
32d3b 74 77 6f 20 74 79 70 65 73 20 6f 66 20 63 61 73  two types of cas
32d3c 65 20 6d 61 70 70 69 6e 67 2c 20 22 67 65 6e 65  e mapping, "gene
32d3d 72 61 6c 22 20 63 61 73 65 20 6d 61 70 70 69 6e  ral" case mappin
32d3e 67 20 61 6e 64 0a 2a 2a 20 22 6c 61 6e 67 75 61  g and.** "langua
32d3f 67 65 20 73 70 65 63 69 66 69 63 22 2e 20 52 65  ge specific". Re
32d40 66 65 72 20 74 6f 20 49 43 55 20 64 6f 63 75 6d  fer to ICU docum
32d41 65 6e 74 61 74 69 6f 6e 20 66 6f 72 20 74 68 65  entation for the
32d42 20 64 69 66 66 65 72 65 6e 63 65 73 0a 2a 2a 20   differences.** 
32d43 62 65 74 77 65 65 6e 20 74 68 65 20 74 77 6f 2e  between the two.
32d44 0a 2a 2a 0a 2a 2a 20 54 6f 20 75 74 69 6c 69 73  .**.** To utilis
32d45 65 20 22 67 65 6e 65 72 61 6c 22 20 63 61 73 65  e "general" case
32d46 20 6d 61 70 70 69 6e 67 2c 20 74 68 65 20 75 70   mapping, the up
32d47 70 65 72 28 29 20 6f 72 20 6c 6f 77 65 72 28 29  per() or lower()
32d48 20 73 63 61 6c 61 72 20 0a 2a 2a 20 66 75 6e 63   scalar .** func
32d49 74 69 6f 6e 73 20 61 72 65 20 69 6e 76 6f 6b 65  tions are invoke
32d4a 64 20 77 69 74 68 20 6f 6e 65 20 61 72 67 75 6d  d with one argum
32d4b 65 6e 74 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 20 75  ent:.**.**     u
32d4c 70 70 65 72 28 27 41 42 43 27 29 20 2d 3e 20 27  pper('ABC') -> '
32d4d 61 62 63 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65  abc'.**     lowe
32d4e 72 28 27 61 62 63 27 29 20 2d 3e 20 27 41 42 43  r('abc') -> 'ABC
32d4f 27 0a 2a 2a 0a 2a 2a 20 54 6f 20 61 63 63 65 73  '.**.** To acces
32d50 73 20 49 43 55 20 22 6c 61 6e 67 75 61 67 65 20  s ICU "language 
32d51 73 70 65 63 69 66 69 63 22 20 63 61 73 65 20 6d  specific" case m
32d52 61 70 70 69 6e 67 2c 20 75 70 70 65 72 28 29 20  apping, upper() 
32d53 6f 72 20 6c 6f 77 65 72 28 29 0a 2a 2a 20 73 68  or lower().** sh
32d54 6f 75 6c 64 20 62 65 20 69 6e 76 6f 6b 65 64 20  ould be invoked 
32d55 77 69 74 68 20 74 77 6f 20 61 72 67 75 6d 65 6e  with two argumen
32d56 74 73 2e 20 54 68 65 20 73 65 63 6f 6e 64 20 61  ts. The second a
32d57 72 67 75 6d 65 6e 74 20 69 73 20 74 68 65 20 6e  rgument is the n
32d58 61 6d 65 0a 2a 2a 20 6f 66 20 74 68 65 20 6c 6f  ame.** of the lo
32d59 63 61 6c 65 20 74 6f 20 75 73 65 2e 20 50 61 73  cale to use. Pas
32d5a 73 69 6e 67 20 61 6e 20 65 6d 70 74 79 20 73 74  sing an empty st
32d5b 72 69 6e 67 20 28 22 22 29 20 6f 72 20 53 51 4c  ring ("") or SQL
32d5c 20 4e 55 4c 4c 20 76 61 6c 75 65 0a 2a 2a 20 61   NULL value.** a
32d5d 73 20 74 68 65 20 73 65 63 6f 6e 64 20 61 72 67  s the second arg
32d5e 75 6d 65 6e 74 20 69 73 20 74 68 65 20 73 61 6d  ument is the sam
32d5f 65 20 61 73 20 69 6e 76 6f 6b 69 6e 67 20 74 68  e as invoking th
32d60 65 20 31 20 61 72 67 75 6d 65 6e 74 20 76 65 72  e 1 argument ver
32d61 73 69 6f 6e 0a 2a 2a 20 6f 66 20 75 70 70 65 72  sion.** of upper
32d62 28 29 20 6f 72 20 6c 6f 77 65 72 28 29 2e 0a 2a  () or lower()..*
32d63 2a 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72 28 27  *.**     lower('
32d64 49 27 2c 20 27 65 6e 5f 75 73 27 29 20 2d 3e 20  I', 'en_us') -> 
32d65 27 69 27 0a 2a 2a 20 20 20 20 20 6c 6f 77 65 72  'i'.**     lower
32d66 28 27 49 27 2c 20 27 74 72 5f 74 72 27 29 20 2d  ('I', 'tr_tr') -
32d67 3e 20 27 c4 b1 27 20 28 73 6d 61 6c 6c 20 64 6f  > '..' (small do
32d68 74 6c 65 73 73 20 69 29 0a 2a 2a 0a 2a 2a 20 68  tless i).**.** h
32d69 74 74 70 3a 2f 2f 77 77 77 2e 69 63 75 2d 70 72  ttp://www.icu-pr
32d6a 6f 6a 65 63 74 2e 6f 72 67 2f 75 73 65 72 67 75  oject.org/usergu
32d6b 69 64 65 2f 70 6f 73 69 78 2e 68 74 6d 6c 23 63  ide/posix.html#c
32d6c 61 73 65 5f 6d 61 70 70 69 6e 67 73 0a 2a 2f 0a  ase_mappings.*/.
32d6d 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 43  static void icuC
32d6e 61 73 65 46 75 6e 63 31 36 28 73 71 6c 69 74 65  aseFunc16(sqlite
32d6f 33 5f 63 6f 6e 74 65 78 74 20 2a 70 2c 20 69 6e  3_context *p, in
32d70 74 20 6e 41 72 67 2c 20 73 71 6c 69 74 65 33 5f  t nArg, sqlite3_
32d71 76 61 6c 75 65 20 2a 2a 61 70 41 72 67 29 7b 0a  value **apArg){.
32d72 20 20 63 6f 6e 73 74 20 55 43 68 61 72 20 2a 7a    const UChar *z
32d73 49 6e 70 75 74 3b 0a 20 20 55 43 68 61 72 20 2a  Input;.  UChar *
32d74 7a 4f 75 74 70 75 74 3b 0a 20 20 69 6e 74 20 6e  zOutput;.  int n
32d75 49 6e 70 75 74 3b 0a 20 20 69 6e 74 20 6e 4f 75  Input;.  int nOu
32d76 74 70 75 74 3b 0a 0a 20 20 55 45 72 72 6f 72 43  tput;..  UErrorC
32d77 6f 64 65 20 73 74 61 74 75 73 20 3d 20 55 5f 5a  ode status = U_Z
32d78 45 52 4f 5f 45 52 52 4f 52 3b 0a 20 20 63 6f 6e  ERO_ERROR;.  con
32d79 73 74 20 63 68 61 72 20 2a 7a 4c 6f 63 61 6c 65  st char *zLocale
32d7a 20 3d 20 30 3b 0a 0a 20 20 61 73 73 65 72 74 28   = 0;..  assert(
32d7b 6e 41 72 67 3d 3d 31 20 7c 7c 20 6e 41 72 67 3d  nArg==1 || nArg=
32d7c 3d 32 29 3b 0a 20 20 69 66 28 20 6e 41 72 67 3d  =2);.  if( nArg=
32d7d 3d 32 20 29 7b 0a 20 20 20 20 7a 4c 6f 63 61 6c  =2 ){.    zLocal
32d7e 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72 20  e = (const char 
32d7f 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f  *)sqlite3_value_
32d80 74 65 78 74 28 61 70 41 72 67 5b 31 5d 29 3b 0a  text(apArg[1]);.
32d81 20 20 7d 0a 0a 20 20 7a 49 6e 70 75 74 20 3d 20    }..  zInput = 
32d82 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 5f 74 65  sqlite3_value_te
32d83 78 74 31 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a  xt16(apArg[0]);.
32d84 20 20 69 66 28 20 21 7a 49 6e 70 75 74 20 29 7b    if( !zInput ){
32d85 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
32d86 0a 20 20 6e 49 6e 70 75 74 20 3d 20 73 71 6c 69  .  nInput = sqli
32d87 74 65 33 5f 76 61 6c 75 65 5f 62 79 74 65 73 31  te3_value_bytes1
32d88 36 28 61 70 41 72 67 5b 30 5d 29 3b 0a 0a 20 20  6(apArg[0]);..  
32d89 6e 4f 75 74 70 75 74 20 3d 20 6e 49 6e 70 75 74  nOutput = nInput
32d8a 20 2a 20 32 20 2b 20 32 3b 0a 20 20 7a 4f 75 74   * 2 + 2;.  zOut
32d8b 70 75 74 20 3d 20 73 71 6c 69 74 65 33 5f 6d 61  put = sqlite3_ma
32d8c 6c 6c 6f 63 28 6e 4f 75 74 70 75 74 29 3b 0a 20  lloc(nOutput);. 
32d8d 20 69 66 28 20 21 7a 4f 75 74 70 75 74 20 29 7b   if( !zOutput ){
32d8e 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d  .    return;.  }
32d8f 0a 0a 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f  ..  if( sqlite3_
32d90 75 73 65 72 5f 64 61 74 61 28 70 29 20 29 7b 0a  user_data(p) ){.
32d91 20 20 20 20 75 5f 73 74 72 54 6f 55 70 70 65 72      u_strToUpper
32d92 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75 74 70 75  (zOutput, nOutpu
32d93 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20 6e 49 6e  t/2, zInput, nIn
32d94 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c 65 2c 20  put/2, zLocale, 
32d95 26 73 74 61 74 75 73 29 3b 0a 20 20 7d 65 6c 73  &status);.  }els
32d96 65 7b 0a 20 20 20 20 75 5f 73 74 72 54 6f 4c 6f  e{.    u_strToLo
32d97 77 65 72 28 7a 4f 75 74 70 75 74 2c 20 6e 4f 75  wer(zOutput, nOu
32d98 74 70 75 74 2f 32 2c 20 7a 49 6e 70 75 74 2c 20  tput/2, zInput, 
32d99 6e 49 6e 70 75 74 2f 32 2c 20 7a 4c 6f 63 61 6c  nInput/2, zLocal
32d9a 65 2c 20 26 73 74 61 74 75 73 29 3b 0a 20 20 7d  e, &status);.  }
32d9b 0a 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45  ..  if( !U_SUCCE
32d9c 53 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20  SS(status) ){.  
32d9d 20 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72    icuFunctionErr
32d9e 6f 72 28 70 2c 20 22 75 5f 73 74 72 54 6f 4c 6f  or(p, "u_strToLo
32d9f 77 65 72 28 29 2f 75 5f 73 74 72 54 6f 55 70 70  wer()/u_strToUpp
32da0 65 72 22 2c 20 73 74 61 74 75 73 29 3b 0a 20 20  er", status);.  
32da1 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d 0a 0a 20    return;.  }.. 
32da2 20 73 71 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f   sqlite3_result_
32da3 74 65 78 74 31 36 28 70 2c 20 7a 4f 75 74 70 75  text16(p, zOutpu
32da4 74 2c 20 2d 31 2c 20 78 46 72 65 65 29 3b 0a 7d  t, -1, xFree);.}
32da5 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f  ../*.** Collatio
32da6 6e 20 73 65 71 75 65 6e 63 65 20 64 65 73 74 72  n sequence destr
32da7 75 63 74 6f 72 20 66 75 6e 63 74 69 6f 6e 2e 20  uctor function. 
32da8 54 68 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e  The pCtx argumen
32da9 74 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61  t points to.** a
32daa 20 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63   UCollator struc
32dab 74 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20  ture previously 
32dac 61 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20  allocated using 
32dad 75 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a  ucol_open()..*/.
32dae 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75 43  static void icuC
32daf 6f 6c 6c 61 74 69 6f 6e 44 65 6c 28 76 6f 69 64  ollationDel(void
32db0 20 2a 70 43 74 78 29 7b 0a 20 20 55 43 6f 6c 6c   *pCtx){.  UColl
32db1 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c 6c  ator *p = (UColl
32db2 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20 75  ator *)pCtx;.  u
32db3 63 6f 6c 5f 63 6c 6f 73 65 28 70 29 3b 0a 7d 0a  col_close(p);.}.
32db4 0a 2f 2a 0a 2a 2a 20 43 6f 6c 6c 61 74 69 6f 6e  ./*.** Collation
32db5 20 73 65 71 75 65 6e 63 65 20 63 6f 6d 70 61 72   sequence compar
32db6 69 73 6f 6e 20 66 75 6e 63 74 69 6f 6e 2e 20 54  ison function. T
32db7 68 65 20 70 43 74 78 20 61 72 67 75 6d 65 6e 74  he pCtx argument
32db8 20 70 6f 69 6e 74 73 20 74 6f 0a 2a 2a 20 61 20   points to.** a 
32db9 55 43 6f 6c 6c 61 74 6f 72 20 73 74 72 75 63 74  UCollator struct
32dba 75 72 65 20 70 72 65 76 69 6f 75 73 6c 79 20 61  ure previously a
32dbb 6c 6c 6f 63 61 74 65 64 20 75 73 69 6e 67 20 75  llocated using u
32dbc 63 6f 6c 5f 6f 70 65 6e 28 29 2e 0a 2a 2f 0a 73  col_open()..*/.s
32dbd 74 61 74 69 63 20 69 6e 74 20 69 63 75 43 6f 6c  tatic int icuCol
32dbe 6c 61 74 69 6f 6e 43 6f 6c 6c 28 0a 20 20 76 6f  lationColl(.  vo
32dbf 69 64 20 2a 70 43 74 78 2c 0a 20 20 69 6e 74 20  id *pCtx,.  int 
32dc0 6e 4c 65 66 74 2c 0a 20 20 63 6f 6e 73 74 20 76  nLeft,.  const v
32dc1 6f 69 64 20 2a 7a 4c 65 66 74 2c 0a 20 20 69 6e  oid *zLeft,.  in
32dc2 74 20 6e 52 69 67 68 74 2c 0a 20 20 63 6f 6e 73  t nRight,.  cons
32dc3 74 20 76 6f 69 64 20 2a 7a 52 69 67 68 74 0a 29  t void *zRight.)
32dc4 7b 0a 20 20 55 43 6f 6c 6c 61 74 69 6f 6e 52 65  {.  UCollationRe
32dc5 73 75 6c 74 20 72 65 73 3b 0a 20 20 55 43 6f 6c  sult res;.  UCol
32dc6 6c 61 74 6f 72 20 2a 70 20 3d 20 28 55 43 6f 6c  lator *p = (UCol
32dc7 6c 61 74 6f 72 20 2a 29 70 43 74 78 3b 0a 20 20  lator *)pCtx;.  
32dc8 72 65 73 20 3d 20 75 63 6f 6c 5f 73 74 72 63 6f  res = ucol_strco
32dc9 6c 6c 28 70 2c 20 28 55 43 68 61 72 20 2a 29 7a  ll(p, (UChar *)z
32dca 4c 65 66 74 2c 20 6e 4c 65 66 74 2f 32 2c 20 28  Left, nLeft/2, (
32dcb 55 43 68 61 72 20 2a 29 7a 52 69 67 68 74 2c 20  UChar *)zRight, 
32dcc 6e 52 69 67 68 74 2f 32 29 3b 0a 20 20 73 77 69  nRight/2);.  swi
32dcd 74 63 68 28 20 72 65 73 20 29 7b 0a 20 20 20 20  tch( res ){.    
32dce 63 61 73 65 20 55 43 4f 4c 5f 4c 45 53 53 3a 20  case UCOL_LESS: 
32dcf 20 20 20 72 65 74 75 72 6e 20 2d 31 3b 0a 20 20     return -1;.  
32dd0 20 20 63 61 73 65 20 55 43 4f 4c 5f 47 52 45 41    case UCOL_GREA
32dd1 54 45 52 3a 20 72 65 74 75 72 6e 20 2b 31 3b 0a  TER: return +1;.
32dd2 20 20 20 20 63 61 73 65 20 55 43 4f 4c 5f 45 51      case UCOL_EQ
32dd3 55 41 4c 3a 20 20 20 72 65 74 75 72 6e 20 30 3b  UAL:   return 0;
32dd4 0a 20 20 7d 0a 20 20 61 73 73 65 72 74 28 21 22  .  }.  assert(!"
32dd5 55 6e 65 78 70 65 63 74 65 64 20 72 65 74 75 72  Unexpected retur
32dd6 6e 20 76 61 6c 75 65 20 66 72 6f 6d 20 75 63 6f  n value from uco
32dd7 6c 5f 73 74 72 63 6f 6c 6c 28 29 22 29 3b 0a 20  l_strcoll()");. 
32dd8 20 72 65 74 75 72 6e 20 30 3b 0a 7d 0a 0a 2f 2a   return 0;.}../*
32dd9 0a 2a 2a 20 49 6d 70 6c 65 6d 65 6e 74 61 74 69  .** Implementati
32dda 6f 6e 20 6f 66 20 74 68 65 20 73 63 61 6c 61 72  on of the scalar
32ddb 20 66 75 6e 63 74 69 6f 6e 20 69 63 75 5f 6c 6f   function icu_lo
32ddc 61 64 5f 63 6f 6c 6c 61 74 69 6f 6e 28 29 2e 0a  ad_collation()..
32ddd 2a 2a 0a 2a 2a 20 54 68 69 73 20 73 63 61 6c 61  **.** This scala
32dde 72 20 66 75 6e 63 74 69 6f 6e 20 69 73 20 75 73  r function is us
32ddf 65 64 20 74 6f 20 61 64 64 20 49 43 55 20 63 6f  ed to add ICU co
32de0 6c 6c 61 74 69 6f 6e 20 62 61 73 65 64 20 63 6f  llation based co
32de1 6c 6c 61 74 69 6f 6e 20 0a 2a 2a 20 74 79 70 65  llation .** type
32de2 73 20 74 6f 20 61 6e 20 53 51 4c 69 74 65 20 64  s to an SQLite d
32de3 61 74 61 62 61 73 65 20 63 6f 6e 6e 65 63 74 69  atabase connecti
32de4 6f 6e 2e 20 49 74 20 69 73 20 69 6e 74 65 6e 64  on. It is intend
32de5 65 64 20 74 6f 20 62 65 20 63 61 6c 6c 65 64 0a  ed to be called.
32de6 2a 2a 20 61 73 20 66 6f 6c 6c 6f 77 73 3a 0a 2a  ** as follows:.*
32de7 2a 0a 2a 2a 20 20 20 20 20 53 45 4c 45 43 54 20  *.**     SELECT 
32de8 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61 74 69  icu_load_collati
32de9 6f 6e 28 3c 6c 6f 63 61 6c 65 3e 2c 20 3c 63 6f  on(<locale>, <co
32dea 6c 6c 61 74 69 6f 6e 2d 6e 61 6d 65 3e 29 3b 0a  llation-name>);.
32deb 2a 2a 0a 2a 2a 20 57 68 65 72 65 20 3c 6c 6f 63  **.** Where <loc
32dec 61 6c 65 3e 20 69 73 20 61 20 73 74 72 69 6e 67  ale> is a string
32ded 20 63 6f 6e 74 61 69 6e 69 6e 67 20 61 6e 20 49   containing an I
32dee 43 55 20 6c 6f 63 61 6c 65 20 69 64 65 6e 74 69  CU locale identi
32def 66 69 65 72 20 28 69 2e 65 2e 0a 2a 2a 20 22 65  fier (i.e..** "e
32df0 6e 5f 41 55 22 2c 20 22 74 72 5f 54 52 22 20 65  n_AU", "tr_TR" e
32df1 74 63 2e 29 20 61 6e 64 20 3c 63 6f 6c 6c 61 74  tc.) and <collat
32df2 69 6f 6e 2d 6e 61 6d 65 3e 20 69 73 20 74 68 65  ion-name> is the
32df3 20 6e 61 6d 65 20 6f 66 20 74 68 65 0a 2a 2a 20   name of the.** 
32df4 63 6f 6c 6c 61 74 69 6f 6e 20 73 65 71 75 65 6e  collation sequen
32df5 63 65 20 74 6f 20 63 72 65 61 74 65 2e 0a 2a 2f  ce to create..*/
32df6 0a 73 74 61 74 69 63 20 76 6f 69 64 20 69 63 75  .static void icu
32df7 4c 6f 61 64 43 6f 6c 6c 61 74 69 6f 6e 28 0a 20  LoadCollation(. 
32df8 20 73 71 6c 69 74 65 33 5f 63 6f 6e 74 65 78 74   sqlite3_context
32df9 20 2a 70 2c 20 0a 20 20 69 6e 74 20 6e 41 72 67   *p, .  int nArg
32dfa 2c 20 0a 20 20 73 71 6c 69 74 65 33 5f 76 61 6c  , .  sqlite3_val
32dfb 75 65 20 2a 2a 61 70 41 72 67 0a 29 7b 0a 20 20  ue **apArg.){.  
32dfc 73 71 6c 69 74 65 33 20 2a 64 62 20 3d 20 28 73  sqlite3 *db = (s
32dfd 71 6c 69 74 65 33 20 2a 29 73 71 6c 69 74 65 33  qlite3 *)sqlite3
32dfe 5f 75 73 65 72 5f 64 61 74 61 28 70 29 3b 0a 20  _user_data(p);. 
32dff 20 55 45 72 72 6f 72 43 6f 64 65 20 73 74 61 74   UErrorCode stat
32e00 75 73 20 3d 20 55 5f 5a 45 52 4f 5f 45 52 52 4f  us = U_ZERO_ERRO
32e01 52 3b 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20  R;.  const char 
32e02 2a 7a 4c 6f 63 61 6c 65 3b 20 20 20 20 20 20 2f  *zLocale;      /
32e03 2a 20 4c 6f 63 61 6c 65 20 69 64 65 6e 74 69 66  * Locale identif
32e04 69 65 72 20 2d 20 28 65 67 2e 20 22 6a 70 5f 4a  ier - (eg. "jp_J
32e05 50 22 29 20 2a 2f 0a 20 20 63 6f 6e 73 74 20 63  P") */.  const c
32e06 68 61 72 20 2a 7a 4e 61 6d 65 3b 20 20 20 20 20  har *zName;     
32e07 20 20 20 2f 2a 20 53 51 4c 20 43 6f 6c 6c 61 74     /* SQL Collat
32e08 69 6f 6e 20 73 65 71 75 65 6e 63 65 20 6e 61 6d  ion sequence nam
32e09 65 20 28 65 67 2e 20 22 6a 61 70 61 6e 65 73 65  e (eg. "japanese
32e0a 22 29 20 2a 2f 0a 20 20 55 43 6f 6c 6c 61 74 6f  ") */.  UCollato
32e0b 72 20 2a 70 55 43 6f 6c 6c 61 74 6f 72 3b 20 20  r *pUCollator;  
32e0c 20 20 2f 2a 20 49 43 55 20 6c 69 62 72 61 72 79    /* ICU library
32e0d 20 63 6f 6c 6c 61 74 69 6f 6e 20 6f 62 6a 65 63   collation objec
32e0e 74 20 2a 2f 0a 20 20 69 6e 74 20 72 63 3b 20 20  t */.  int rc;  
32e0f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e10 20 2f 2a 20 52 65 74 75 72 6e 20 63 6f 64 65 20   /* Return code 
32e11 66 72 6f 6d 20 73 71 6c 69 74 65 33 5f 63 72 65  from sqlite3_cre
32e12 61 74 65 5f 63 6f 6c 6c 61 74 69 6f 6e 5f 78 28  ate_collation_x(
32e13 29 20 2a 2f 0a 0a 20 20 61 73 73 65 72 74 28 6e  ) */..  assert(n
32e14 41 72 67 3d 3d 32 29 3b 0a 20 20 7a 4c 6f 63 61  Arg==2);.  zLoca
32e15 6c 65 20 3d 20 28 63 6f 6e 73 74 20 63 68 61 72  le = (const char
32e16 20 2a 29 73 71 6c 69 74 65 33 5f 76 61 6c 75 65   *)sqlite3_value
32e17 5f 74 65 78 74 28 61 70 41 72 67 5b 30 5d 29 3b  _text(apArg[0]);
32e18 0a 20 20 7a 4e 61 6d 65 20 3d 20 28 63 6f 6e 73  .  zName = (cons
32e19 74 20 63 68 61 72 20 2a 29 73 71 6c 69 74 65 33  t char *)sqlite3
32e1a 5f 76 61 6c 75 65 5f 74 65 78 74 28 61 70 41 72  _value_text(apAr
32e1b 67 5b 31 5d 29 3b 0a 0a 20 20 69 66 28 20 21 7a  g[1]);..  if( !z
32e1c 4c 6f 63 61 6c 65 20 7c 7c 20 21 7a 4e 61 6d 65  Locale || !zName
32e1d 20 29 7b 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a   ){.    return;.
32e1e 20 20 7d 0a 0a 20 20 70 55 43 6f 6c 6c 61 74 6f    }..  pUCollato
32e1f 72 20 3d 20 75 63 6f 6c 5f 6f 70 65 6e 28 7a 4c  r = ucol_open(zL
32e20 6f 63 61 6c 65 2c 20 26 73 74 61 74 75 73 29 3b  ocale, &status);
32e21 0a 20 20 69 66 28 20 21 55 5f 53 55 43 43 45 53  .  if( !U_SUCCES
32e22 53 28 73 74 61 74 75 73 29 20 29 7b 0a 20 20 20  S(status) ){.   
32e23 20 69 63 75 46 75 6e 63 74 69 6f 6e 45 72 72 6f   icuFunctionErro
32e24 72 28 70 2c 20 22 75 63 6f 6c 5f 6f 70 65 6e 22  r(p, "ucol_open"
32e25 2c 20 73 74 61 74 75 73 29 3b 0a 20 20 20 20 72  , status);.    r
32e26 65 74 75 72 6e 3b 0a 20 20 7d 0a 20 20 61 73 73  eturn;.  }.  ass
32e27 65 72 74 28 70 29 3b 0a 0a 20 20 72 63 20 3d 20  ert(p);..  rc = 
32e28 73 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 63  sqlite3_create_c
32e29 6f 6c 6c 61 74 69 6f 6e 5f 76 32 28 64 62 2c 20  ollation_v2(db, 
32e2a 7a 4e 61 6d 65 2c 20 53 51 4c 49 54 45 5f 55 54  zName, SQLITE_UT
32e2b 46 31 36 2c 20 28 76 6f 69 64 20 2a 29 70 55 43  F16, (void *)pUC
32e2c 6f 6c 6c 61 74 6f 72 2c 20 0a 20 20 20 20 20 20  ollator, .      
32e2d 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 43 6f 6c 6c  icuCollationColl
32e2e 2c 20 69 63 75 43 6f 6c 6c 61 74 69 6f 6e 44 65  , icuCollationDe
32e2f 6c 0a 20 20 29 3b 0a 20 20 69 66 28 20 72 63 21  l.  );.  if( rc!
32e30 3d 53 51 4c 49 54 45 5f 4f 4b 20 29 7b 0a 20 20  =SQLITE_OK ){.  
32e31 20 20 75 63 6f 6c 5f 63 6c 6f 73 65 28 70 55 43    ucol_close(pUC
32e32 6f 6c 6c 61 74 6f 72 29 3b 0a 20 20 20 20 73 71  ollator);.    sq
32e33 6c 69 74 65 33 5f 72 65 73 75 6c 74 5f 65 72 72  lite3_result_err
32e34 6f 72 28 70 2c 20 22 45 72 72 6f 72 20 72 65 67  or(p, "Error reg
32e35 69 73 74 65 72 69 6e 67 20 63 6f 6c 6c 61 74 69  istering collati
32e36 6f 6e 20 66 75 6e 63 74 69 6f 6e 22 2c 20 2d 31  on function", -1
32e37 29 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 0a 2a 2a 20  );.  }.}../*.** 
32e38 52 65 67 69 73 74 65 72 20 74 68 65 20 49 43 55  Register the ICU
32e39 20 65 78 74 65 6e 73 69 6f 6e 20 66 75 6e 63 74   extension funct
32e3a 69 6f 6e 73 20 77 69 74 68 20 64 61 74 61 62 61  ions with databa
32e3b 73 65 20 64 62 2e 0a 2a 2f 0a 53 51 4c 49 54 45  se db..*/.SQLITE
32e3c 5f 50 52 49 56 41 54 45 20 69 6e 74 20 73 71 6c  _PRIVATE int sql
32e3d 69 74 65 33 49 63 75 49 6e 69 74 28 73 71 6c 69  ite3IcuInit(sqli
32e3e 74 65 33 20 2a 64 62 29 7b 0a 20 20 73 74 72 75  te3 *db){.  stru
32e3f 63 74 20 49 63 75 53 63 61 6c 61 72 20 7b 0a 20  ct IcuScalar {. 
32e40 20 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 7a     const char *z
32e41 4e 61 6d 65 3b 20 20 20 20 20 20 20 20 20 20 20  Name;           
32e42 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32e43 46 75 6e 63 74 69 6f 6e 20 6e 61 6d 65 20 2a 2f  Function name */
32e44 0a 20 20 20 20 69 6e 74 20 6e 41 72 67 3b 20 20  .    int nArg;  
32e45 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e46 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32e47 2a 20 4e 75 6d 62 65 72 20 6f 66 20 61 72 67 75  * Number of argu
32e48 6d 65 6e 74 73 20 2a 2f 0a 20 20 20 20 69 6e 74  ments */.    int
32e49 20 65 6e 63 3b 20 20 20 20 20 20 20 20 20 20 20   enc;           
32e4a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e4b 20 20 20 20 20 20 20 2f 2a 20 4f 70 74 69 6d 61         /* Optima
32e4c 6c 20 74 65 78 74 20 65 6e 63 6f 64 69 6e 67 20  l text encoding 
32e4d 2a 2f 0a 20 20 20 20 76 6f 69 64 20 2a 70 43 6f  */.    void *pCo
32e4e 6e 74 65 78 74 3b 20 20 20 20 20 20 20 20 20 20  ntext;          
32e4f 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32e50 20 2f 2a 20 73 71 6c 69 74 65 33 5f 75 73 65 72   /* sqlite3_user
32e51 5f 64 61 74 61 28 29 20 63 6f 6e 74 65 78 74 20  _data() context 
32e52 2a 2f 0a 20 20 20 20 76 6f 69 64 20 28 2a 78 46  */.    void (*xF
32e53 75 6e 63 29 28 73 71 6c 69 74 65 33 5f 63 6f 6e  unc)(sqlite3_con
32e54 74 65 78 74 2a 2c 69 6e 74 2c 73 71 6c 69 74 65  text*,int,sqlite
32e55 33 5f 76 61 6c 75 65 2a 2a 29 3b 0a 20 20 7d 20  3_value**);.  } 
32e56 73 63 61 6c 61 72 73 5b 5d 20 3d 20 7b 0a 20 20  scalars[] = {.  
32e57 20 20 7b 22 72 65 67 65 78 70 22 2c 2d 31 2c 20    {"regexp",-1, 
32e58 53 51 4c 49 54 45 5f 41 4e 59 2c 20 20 20 20 20  SQLITE_ANY,     
32e59 20 20 20 20 20 30 2c 20 69 63 75 52 65 67 65 78       0, icuRegex
32e5a 70 46 75 6e 63 7d 2c 0a 0a 20 20 20 20 7b 22 6c  pFunc},..    {"l
32e5b 6f 77 65 72 22 2c 20 20 31 2c 20 53 51 4c 49 54  ower",  1, SQLIT
32e5c 45 5f 55 54 46 31 36 2c 20 20 20 20 20 20 20 20  E_UTF16,        
32e5d 30 2c 20 69 63 75 43 61 73 65 46 75 6e 63 31 36  0, icuCaseFunc16
32e5e 7d 2c 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c  },.    {"lower",
32e5f 20 20 32 2c 20 53 51 4c 49 54 45 5f 55 54 46 31    2, SQLITE_UTF1
32e60 36 2c 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  6,        0, icu
32e61 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20  CaseFunc16},.   
32e62 20 7b 22 75 70 70 65 72 22 2c 20 20 31 2c 20 53   {"upper",  1, S
32e63 51 4c 49 54 45 5f 55 54 46 31 36 2c 20 28 76 6f  QLITE_UTF16, (vo
32e64 69 64 2a 29 31 2c 20 69 63 75 43 61 73 65 46 75  id*)1, icuCaseFu
32e65 6e 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70  nc16},.    {"upp
32e66 65 72 22 2c 20 20 32 2c 20 53 51 4c 49 54 45 5f  er",  2, SQLITE_
32e67 55 54 46 31 36 2c 20 28 76 6f 69 64 2a 29 31 2c  UTF16, (void*)1,
32e68 20 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c   icuCaseFunc16},
32e69 0a 0a 20 20 20 20 7b 22 6c 6f 77 65 72 22 2c 20  ..    {"lower", 
32e6a 20 31 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c   1, SQLITE_UTF8,
32e6b 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75 43           0, icuC
32e6c 61 73 65 46 75 6e 63 31 36 7d 2c 0a 20 20 20 20  aseFunc16},.    
32e6d 7b 22 6c 6f 77 65 72 22 2c 20 20 32 2c 20 53 51  {"lower",  2, SQ
32e6e 4c 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20  LITE_UTF8,      
32e6f 20 20 20 30 2c 20 69 63 75 43 61 73 65 46 75 6e     0, icuCaseFun
32e70 63 31 36 7d 2c 0a 20 20 20 20 7b 22 75 70 70 65  c16},.    {"uppe
32e71 72 22 2c 20 20 31 2c 20 53 51 4c 49 54 45 5f 55  r",  1, SQLITE_U
32e72 54 46 38 2c 20 20 28 76 6f 69 64 2a 29 31 2c 20  TF8,  (void*)1, 
32e73 69 63 75 43 61 73 65 46 75 6e 63 31 36 7d 2c 0a  icuCaseFunc16},.
32e74 20 20 20 20 7b 22 75 70 70 65 72 22 2c 20 20 32      {"upper",  2
32e75 2c 20 53 51 4c 49 54 45 5f 55 54 46 38 2c 20 20  , SQLITE_UTF8,  
32e76 28 76 6f 69 64 2a 29 31 2c 20 69 63 75 43 61 73  (void*)1, icuCas
32e77 65 46 75 6e 63 31 36 7d 2c 0a 0a 20 20 20 20 7b  eFunc16},..    {
32e78 22 6c 69 6b 65 22 2c 20 20 20 32 2c 20 53 51 4c  "like",   2, SQL
32e79 49 54 45 5f 55 54 46 38 2c 20 20 20 20 20 20 20  ITE_UTF8,       
32e7a 20 20 30 2c 20 69 63 75 4c 69 6b 65 46 75 6e 63    0, icuLikeFunc
32e7b 7d 2c 0a 20 20 20 20 7b 22 6c 69 6b 65 22 2c 20  },.    {"like", 
32e7c 20 20 33 2c 20 53 51 4c 49 54 45 5f 55 54 46 38    3, SQLITE_UTF8
32e7d 2c 20 20 20 20 20 20 20 20 20 30 2c 20 69 63 75  ,         0, icu
32e7e 4c 69 6b 65 46 75 6e 63 7d 2c 0a 0a 20 20 20 20  LikeFunc},..    
32e7f 7b 22 69 63 75 5f 6c 6f 61 64 5f 63 6f 6c 6c 61  {"icu_load_colla
32e80 74 69 6f 6e 22 2c 20 20 32 2c 20 53 51 4c 49 54  tion",  2, SQLIT
32e81 45 5f 55 54 46 38 2c 20 28 76 6f 69 64 2a 29 64  E_UTF8, (void*)d
32e82 62 2c 20 69 63 75 4c 6f 61 64 43 6f 6c 6c 61 74  b, icuLoadCollat
32e83 69 6f 6e 7d 2c 0a 20 20 7d 3b 0a 0a 20 20 69 6e  ion},.  };..  in
32e84 74 20 72 63 20 3d 20 53 51 4c 49 54 45 5f 4f 4b  t rc = SQLITE_OK
32e85 3b 0a 20 20 69 6e 74 20 69 3b 0a 0a 20 20 66 6f  ;.  int i;..  fo
32e86 72 28 69 3d 30 3b 20 72 63 3d 3d 53 51 4c 49 54  r(i=0; rc==SQLIT
32e87 45 5f 4f 4b 20 26 26 20 69 3c 28 73 69 7a 65 6f  E_OK && i<(sizeo
32e88 66 28 73 63 61 6c 61 72 73 29 2f 73 69 7a 65 6f  f(scalars)/sizeo
32e89 66 28 73 74 72 75 63 74 20 49 63 75 53 63 61 6c  f(struct IcuScal
32e8a 61 72 29 29 3b 20 69 2b 2b 29 7b 0a 20 20 20 20  ar)); i++){.    
32e8b 73 74 72 75 63 74 20 49 63 75 53 63 61 6c 61 72  struct IcuScalar
32e8c 20 2a 70 20 3d 20 26 73 63 61 6c 61 72 73 5b 69   *p = &scalars[i
32e8d 5d 3b 0a 20 20 20 20 72 63 20 3d 20 73 71 6c 69  ];.    rc = sqli
32e8e 74 65 33 5f 63 72 65 61 74 65 5f 66 75 6e 63 74  te3_create_funct
32e8f 69 6f 6e 28 0a 20 20 20 20 20 20 20 20 64 62 2c  ion(.        db,
32e90 20 70 2d 3e 7a 4e 61 6d 65 2c 20 70 2d 3e 6e 41   p->zName, p->nA
32e91 72 67 2c 20 70 2d 3e 65 6e 63 2c 20 70 2d 3e 70  rg, p->enc, p->p
32e92 43 6f 6e 74 65 78 74 2c 20 70 2d 3e 78 46 75 6e  Context, p->xFun
32e93 63 2c 20 30 2c 20 30 0a 20 20 20 20 29 3b 0a 20  c, 0, 0.    );. 
32e94 20 7d 0a 0a 20 20 72 65 74 75 72 6e 20 72 63 3b   }..  return rc;
32e95 0a 7d 0a 0a 23 69 66 20 21 53 51 4c 49 54 45 5f  .}..#if !SQLITE_
32e96 43 4f 52 45 0a 53 51 4c 49 54 45 5f 41 50 49 20  CORE.SQLITE_API 
32e97 69 6e 74 20 73 71 6c 69 74 65 33 5f 65 78 74 65  int sqlite3_exte
32e98 6e 73 69 6f 6e 5f 69 6e 69 74 28 0a 20 20 73 71  nsion_init(.  sq
32e99 6c 69 74 65 33 20 2a 64 62 2c 20 0a 20 20 63 68  lite3 *db, .  ch
32e9a 61 72 20 2a 2a 70 7a 45 72 72 4d 73 67 2c 0a 20  ar **pzErrMsg,. 
32e9b 20 63 6f 6e 73 74 20 73 71 6c 69 74 65 33 5f 61   const sqlite3_a
32e9c 70 69 5f 72 6f 75 74 69 6e 65 73 20 2a 70 41 70  pi_routines *pAp
32e9d 69 0a 29 7b 0a 20 20 53 51 4c 49 54 45 5f 45 58  i.){.  SQLITE_EX
32e9e 54 45 4e 53 49 4f 4e 5f 49 4e 49 54 32 28 70 41  TENSION_INIT2(pA
32e9f 70 69 29 0a 20 20 72 65 74 75 72 6e 20 73 71 6c  pi).  return sql
32ea0 69 74 65 33 49 63 75 49 6e 69 74 28 64 62 29 3b  ite3IcuInit(db);
32ea1 0a 7d 0a 23 65 6e 64 69 66 0a 0a 23 65 6e 64 69  .}.#endif..#endi
32ea2 66 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  f../************
32ea3 2a 2a 20 45 6e 64 20 6f 66 20 69 63 75 2e 63 20  ** End of icu.c 
32ea4 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ea5 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ea6 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ea7 2a 2f 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  */./************
32ea8 2a 2a 20 42 65 67 69 6e 20 66 69 6c 65 20 66 74  ** Begin file ft
32ea9 73 33 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a  s3_icu.c *******
32eaa 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eab 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32eac 2a 2f 0a 2f 2a 0a 2a 2a 20 32 30 30 37 20 4a 75  */./*.** 2007 Ju
32ead 6e 65 20 32 32 0a 2a 2a 0a 2a 2a 20 54 68 65 20  ne 22.**.** The 
32eae 61 75 74 68 6f 72 20 64 69 73 63 6c 61 69 6d 73  author disclaims
32eaf 20 63 6f 70 79 72 69 67 68 74 20 74 6f 20 74 68   copyright to th
32eb0 69 73 20 73 6f 75 72 63 65 20 63 6f 64 65 2e 20  is source code. 
32eb1 20 49 6e 20 70 6c 61 63 65 20 6f 66 0a 2a 2a 20   In place of.** 
32eb2 61 20 6c 65 67 61 6c 20 6e 6f 74 69 63 65 2c 20  a legal notice, 
32eb3 68 65 72 65 20 69 73 20 61 20 62 6c 65 73 73 69  here is a blessi
32eb4 6e 67 3a 0a 2a 2a 0a 2a 2a 20 20 20 20 4d 61 79  ng:.**.**    May
32eb5 20 79 6f 75 20 64 6f 20 67 6f 6f 64 20 61 6e 64   you do good and
32eb6 20 6e 6f 74 20 65 76 69 6c 2e 0a 2a 2a 20 20 20   not evil..**   
32eb7 20 4d 61 79 20 79 6f 75 20 66 69 6e 64 20 66 6f   May you find fo
32eb8 72 67 69 76 65 6e 65 73 73 20 66 6f 72 20 79 6f  rgiveness for yo
32eb9 75 72 73 65 6c 66 20 61 6e 64 20 66 6f 72 67 69  urself and forgi
32eba 76 65 20 6f 74 68 65 72 73 2e 0a 2a 2a 20 20 20  ve others..**   
32ebb 20 4d 61 79 20 79 6f 75 20 73 68 61 72 65 20 66   May you share f
32ebc 72 65 65 6c 79 2c 20 6e 65 76 65 72 20 74 61 6b  reely, never tak
32ebd 69 6e 67 20 6d 6f 72 65 20 74 68 61 6e 20 79 6f  ing more than yo
32ebe 75 20 67 69 76 65 2e 0a 2a 2a 0a 2a 2a 2a 2a 2a  u give..**.*****
32ebf 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ec0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ec1 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ec2 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
32ec3 2a 2a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c  ****.** This fil
32ec4 65 20 69 6d 70 6c 65 6d 65 6e 74 73 20 61 20 74  e implements a t
32ec5 6f 6b 65 6e 69 7a 65 72 20 66 6f 72 20 66 74 73  okenizer for fts
32ec6 33 20 62 61 73 65 64 20 6f 6e 20 74 68 65 20 49  3 based on the I
32ec7 43 55 20 6c 69 62 72 61 72 79 2e 0a 2a 2a 20 0a  CU library..** .
32ec8 2a 2a 20 24 49 64 3a 20 66 74 73 33 5f 69 63 75  ** $Id: fts3_icu
32ec9 2e 63 2c 76 20 31 2e 33 20 32 30 30 38 2f 30 39  .c,v 1.3 2008/09
32eca 2f 30 31 20 31 38 3a 33 34 3a 32 30 20 64 61 6e  /01 18:34:20 dan
32ecb 69 65 6c 6b 31 39 37 37 20 45 78 70 20 24 0a 2a  ielk1977 Exp $.*
32ecc 2f 0a 0a 23 69 66 20 21 64 65 66 69 6e 65 64 28  /..#if !defined(
32ecd 53 51 4c 49 54 45 5f 43 4f 52 45 29 20 7c 7c 20  SQLITE_CORE) || 
32ece 64 65 66 69 6e 65 64 28 53 51 4c 49 54 45 5f 45  defined(SQLITE_E
32ecf 4e 41 42 4c 45 5f 46 54 53 33 29 0a 23 69 66 64  NABLE_FTS3).#ifd
32ed0 65 66 20 53 51 4c 49 54 45 5f 45 4e 41 42 4c 45  ef SQLITE_ENABLE
32ed1 5f 49 43 55 0a 0a 0a 23 69 6e 63 6c 75 64 65 20  _ICU...#include 
32ed2 3c 75 6e 69 63 6f 64 65 2f 75 62 72 6b 2e 68 3e  <unicode/ubrk.h>
32ed3 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e 69 63 6f  .#include <unico
32ed4 64 65 2f 75 74 66 31 36 2e 68 3e 0a 0a 74 79 70  de/utf16.h>..typ
32ed5 65 64 65 66 20 73 74 72 75 63 74 20 49 63 75 54  edef struct IcuT
32ed6 6f 6b 65 6e 69 7a 65 72 20 49 63 75 54 6f 6b 65  okenizer IcuToke
32ed7 6e 69 7a 65 72 3b 0a 74 79 70 65 64 65 66 20 73  nizer;.typedef s
32ed8 74 72 75 63 74 20 49 63 75 43 75 72 73 6f 72 20  truct IcuCursor 
32ed9 49 63 75 43 75 72 73 6f 72 3b 0a 0a 73 74 72 75  IcuCursor;..stru
32eda 63 74 20 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20  ct IcuTokenizer 
32edb 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65  {.  sqlite3_toke
32edc 6e 69 7a 65 72 20 62 61 73 65 3b 0a 20 20 63 68  nizer base;.  ch
32edd 61 72 20 2a 7a 4c 6f 63 61 6c 65 3b 0a 7d 3b 0a  ar *zLocale;.};.
32ede 0a 73 74 72 75 63 74 20 49 63 75 43 75 72 73 6f  .struct IcuCurso
32edf 72 20 7b 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f  r {.  sqlite3_to
32ee0 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20 62  kenizer_cursor b
32ee1 61 73 65 3b 0a 0a 20 20 55 42 72 65 61 6b 49 74  ase;..  UBreakIt
32ee2 65 72 61 74 6f 72 20 2a 70 49 74 65 72 3b 20 20  erator *pIter;  
32ee3 20 20 20 20 2f 2a 20 49 43 55 20 62 72 65 61 6b      /* ICU break
32ee4 2d 69 74 65 72 61 74 6f 72 20 6f 62 6a 65 63 74  -iterator object
32ee5 20 2a 2f 0a 20 20 69 6e 74 20 6e 43 68 61 72 3b   */.  int nChar;
32ee6 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32ee7 20 20 2f 2a 20 4e 75 6d 62 65 72 20 6f 66 20 55    /* Number of U
32ee8 43 68 61 72 20 65 6c 65 6d 65 6e 74 73 20 69 6e  Char elements in
32ee9 20 70 49 6e 70 75 74 20 2a 2f 0a 20 20 55 43 68   pInput */.  UCh
32eea 61 72 20 2a 61 43 68 61 72 3b 20 20 20 20 20 20  ar *aChar;      
32eeb 20 20 20 20 20 20 20 20 20 2f 2a 20 43 6f 70 79           /* Copy
32eec 20 6f 66 20 69 6e 70 75 74 20 75 73 69 6e 67 20   of input using 
32eed 75 74 66 2d 31 36 20 65 6e 63 6f 64 69 6e 67 20  utf-16 encoding 
32eee 2a 2f 0a 20 20 69 6e 74 20 2a 61 4f 66 66 73 65  */.  int *aOffse
32eef 74 3b 20 20 20 20 20 20 20 20 20 20 20 20 20 20  t;              
32ef0 20 2f 2a 20 4f 66 66 73 65 74 73 20 6f 66 20 65   /* Offsets of e
32ef1 61 63 68 20 63 68 61 72 61 63 74 65 72 20 69 6e  ach character in
32ef2 20 75 74 66 2d 38 20 69 6e 70 75 74 20 2a 2f 0a   utf-8 input */.
32ef3 0a 20 20 69 6e 74 20 6e 42 75 66 66 65 72 3b 0a  .  int nBuffer;.
32ef4 20 20 63 68 61 72 20 2a 7a 42 75 66 66 65 72 3b    char *zBuffer;
32ef5 0a 0a 20 20 69 6e 74 20 69 54 6f 6b 65 6e 3b 0a  ..  int iToken;.
32ef6 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 72 65 61 74 65  };../*.** Create
32ef7 20 61 20 6e 65 77 20 74 6f 6b 65 6e 69 7a 65 72   a new tokenizer
32ef8 20 69 6e 73 74 61 6e 63 65 2e 0a 2a 2f 0a 73 74   instance..*/.st
32ef9 61 74 69 63 20 69 6e 74 20 69 63 75 43 72 65 61  atic int icuCrea
32efa 74 65 28 0a 20 20 69 6e 74 20 61 72 67 63 2c 20  te(.  int argc, 
32efb 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32efc 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 4e 75             /* Nu
32efd 6d 62 65 72 20 6f 66 20 65 6e 74 72 69 65 73 20  mber of entries 
32efe 69 6e 20 61 72 67 76 5b 5d 20 2a 2f 0a 20 20 63  in argv[] */.  c
32eff 6f 6e 73 74 20 63 68 61 72 20 2a 20 63 6f 6e 73  onst char * cons
32f00 74 20 2a 61 72 67 76 2c 20 20 20 20 20 20 20 20  t *argv,        
32f01 20 20 20 20 2f 2a 20 54 6f 6b 65 6e 69 7a 65 72      /* Tokenizer
32f02 20 63 72 65 61 74 69 6f 6e 20 61 72 67 75 6d 65   creation argume
32f03 6e 74 73 20 2a 2f 0a 20 20 73 71 6c 69 74 65 33  nts */.  sqlite3
32f04 5f 74 6f 6b 65 6e 69 7a 65 72 20 2a 2a 70 70 54  _tokenizer **ppT
32f05 6f 6b 65 6e 69 7a 65 72 20 20 20 20 20 20 2f 2a  okenizer      /*
32f06 20 4f 55 54 3a 20 43 72 65 61 74 65 64 20 74 6f   OUT: Created to
32f07 6b 65 6e 69 7a 65 72 20 2a 2f 0a 29 7b 0a 20 20  kenizer */.){.  
32f08 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 3b  IcuTokenizer *p;
32f09 0a 20 20 69 6e 74 20 6e 20 3d 20 30 3b 0a 0a 20  .  int n = 0;.. 
32f0a 20 69 66 28 20 61 72 67 63 3e 30 20 29 7b 0a 20   if( argc>0 ){. 
32f0b 20 20 20 6e 20 3d 20 73 74 72 6c 65 6e 28 61 72     n = strlen(ar
32f0c 67 76 5b 30 5d 29 2b 31 3b 0a 20 20 7d 0a 20 20  gv[0])+1;.  }.  
32f0d 70 20 3d 20 28 49 63 75 54 6f 6b 65 6e 69 7a 65  p = (IcuTokenize
32f0e 72 20 2a 29 73 71 6c 69 74 65 33 5f 6d 61 6c 6c  r *)sqlite3_mall
32f0f 6f 63 28 73 69 7a 65 6f 66 28 49 63 75 54 6f 6b  oc(sizeof(IcuTok
32f10 65 6e 69 7a 65 72 29 2b 6e 29 3b 0a 20 20 69 66  enizer)+n);.  if
32f11 28 20 21 70 20 29 7b 0a 20 20 20 20 72 65 74 75  ( !p ){.    retu
32f12 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45 4d 3b  rn SQLITE_NOMEM;
32f13 0a 20 20 7d 0a 20 20 6d 65 6d 73 65 74 28 70 2c  .  }.  memset(p,
32f14 20 30 2c 20 73 69 7a 65 6f 66 28 49 63 75 54 6f   0, sizeof(IcuTo
32f15 6b 65 6e 69 7a 65 72 29 29 3b 0a 0a 20 20 69 66  kenizer));..  if
32f16 28 20 6e 20 29 7b 0a 20 20 20 20 70 2d 3e 7a 4c  ( n ){.    p->zL
32f17 6f 63 61 6c 65 20 3d 20 28 63 68 61 72 20 2a 29  ocale = (char *)
32f18 26 70 5b 31 5d 3b 0a 20 20 20 20 6d 65 6d 63 70  &p[1];.    memcp
32f19 79 28 70 2d 3e 7a 4c 6f 63 61 6c 65 2c 20 61 72  y(p->zLocale, ar
32f1a 67 76 5b 30 5d 2c 20 6e 29 3b 0a 20 20 7d 0a 0a  gv[0], n);.  }..
32f1b 20 20 2a 70 70 54 6f 6b 65 6e 69 7a 65 72 20 3d    *ppTokenizer =
32f1c 20 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69   (sqlite3_tokeni
32f1d 7a 65 72 20 2a 29 70 3b 0a 0a 20 20 72 65 74 75  zer *)p;..  retu
32f1e 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
32f1f 0a 2f 2a 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61  ./*.** Destroy a
32f20 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a 2f 0a 73 74   tokenizer.*/.st
32f21 61 74 69 63 20 69 6e 74 20 69 63 75 44 65 73 74  atic int icuDest
32f22 72 6f 79 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65  roy(sqlite3_toke
32f23 6e 69 7a 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65  nizer *pTokenize
32f24 72 29 7b 0a 20 20 49 63 75 54 6f 6b 65 6e 69 7a  r){.  IcuTokeniz
32f25 65 72 20 2a 70 20 3d 20 28 49 63 75 54 6f 6b 65  er *p = (IcuToke
32f26 6e 69 7a 65 72 20 2a 29 70 54 6f 6b 65 6e 69 7a  nizer *)pTokeniz
32f27 65 72 3b 0a 20 20 73 71 6c 69 74 65 33 5f 66 72  er;.  sqlite3_fr
32f28 65 65 28 70 29 3b 0a 20 20 72 65 74 75 72 6e 20  ee(p);.  return 
32f29 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f 2a  SQLITE_OK;.}../*
32f2a 0a 2a 2a 20 50 72 65 70 61 72 65 20 74 6f 20 62  .** Prepare to b
32f2b 65 67 69 6e 20 74 6f 6b 65 6e 69 7a 69 6e 67 20  egin tokenizing 
32f2c 61 20 70 61 72 74 69 63 75 6c 61 72 20 73 74 72  a particular str
32f2d 69 6e 67 2e 20 20 54 68 65 20 69 6e 70 75 74 0a  ing.  The input.
32f2e 2a 2a 20 73 74 72 69 6e 67 20 74 6f 20 62 65 20  ** string to be 
32f2f 74 6f 6b 65 6e 69 7a 65 64 20 69 73 20 70 49 6e  tokenized is pIn
32f30 70 75 74 5b 30 2e 2e 6e 42 79 74 65 73 2d 31 5d  put[0..nBytes-1]
32f31 2e 20 20 41 20 63 75 72 73 6f 72 0a 2a 2a 20 75  .  A cursor.** u
32f32 73 65 64 20 74 6f 20 69 6e 63 72 65 6d 65 6e 74  sed to increment
32f33 61 6c 6c 79 20 74 6f 6b 65 6e 69 7a 65 20 74 68  ally tokenize th
32f34 69 73 20 73 74 72 69 6e 67 20 69 73 20 72 65 74  is string is ret
32f35 75 72 6e 65 64 20 69 6e 20 0a 2a 2a 20 2a 70 70  urned in .** *pp
32f36 43 75 72 73 6f 72 2e 0a 2a 2f 0a 73 74 61 74 69  Cursor..*/.stati
32f37 63 20 69 6e 74 20 69 63 75 4f 70 65 6e 28 0a 20  c int icuOpen(. 
32f38 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a   sqlite3_tokeniz
32f39 65 72 20 2a 70 54 6f 6b 65 6e 69 7a 65 72 2c 20  er *pTokenizer, 
32f3a 20 20 20 20 20 20 20 20 2f 2a 20 54 68 65 20 74          /* The t
32f3b 6f 6b 65 6e 69 7a 65 72 20 2a 2f 0a 20 20 63 6f  okenizer */.  co
32f3c 6e 73 74 20 63 68 61 72 20 2a 7a 49 6e 70 75 74  nst char *zInput
32f3d 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20  ,               
32f3e 20 20 20 20 20 2f 2a 20 49 6e 70 75 74 20 73 74       /* Input st
32f3f 72 69 6e 67 20 2a 2f 0a 20 20 69 6e 74 20 6e 49  ring */.  int nI
32f40 6e 70 75 74 2c 20 20 20 20 20 20 20 20 20 20 20  nput,           
32f41 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32f42 20 2f 2a 20 4c 65 6e 67 74 68 20 6f 66 20 7a 49   /* Length of zI
32f43 6e 70 75 74 20 69 6e 20 62 79 74 65 73 20 2a 2f  nput in bytes */
32f44 0a 20 20 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e  .  sqlite3_token
32f45 69 7a 65 72 5f 63 75 72 73 6f 72 20 2a 2a 70 70  izer_cursor **pp
32f46 43 75 72 73 6f 72 20 20 20 20 2f 2a 20 4f 55 54  Cursor    /* OUT
32f47 3a 20 54 6f 6b 65 6e 69 7a 61 74 69 6f 6e 20 63  : Tokenization c
32f48 75 72 73 6f 72 20 2a 2f 0a 29 7b 0a 20 20 49 63  ursor */.){.  Ic
32f49 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 70 20 3d 20  uTokenizer *p = 
32f4a 28 49 63 75 54 6f 6b 65 6e 69 7a 65 72 20 2a 29  (IcuTokenizer *)
32f4b 70 54 6f 6b 65 6e 69 7a 65 72 3b 0a 20 20 49 63  pTokenizer;.  Ic
32f4c 75 43 75 72 73 6f 72 20 2a 70 43 73 72 3b 0a 0a  uCursor *pCsr;..
32f4d 20 20 63 6f 6e 73 74 20 69 6e 74 33 32 5f 74 20    const int32_t 
32f4e 6f 70 74 20 3d 20 55 5f 46 4f 4c 44 5f 43 41 53  opt = U_FOLD_CAS
32f4f 45 5f 44 45 46 41 55 4c 54 3b 0a 20 20 55 45 72  E_DEFAULT;.  UEr
32f50 72 6f 72 43 6f 64 65 20 73 74 61 74 75 73 20 3d  rorCode status =
32f51 20 55 5f 5a 45 52 4f 5f 45 52 52 4f 52 3b 0a 20   U_ZERO_ERROR;. 
32f52 20 69 6e 74 20 6e 43 68 61 72 3b 0a 0a 20 20 55   int nChar;..  U
32f53 43 68 61 72 33 32 20 63 3b 0a 20 20 69 6e 74 20  Char32 c;.  int 
32f54 69 49 6e 70 75 74 20 3d 20 30 3b 0a 20 20 69 6e  iInput = 0;.  in
32f55 74 20 69 4f 75 74 20 3d 20 30 3b 0a 0a 20 20 2a  t iOut = 0;..  *
32f56 70 70 43 75 72 73 6f 72 20 3d 20 30 3b 0a 0a 20  ppCursor = 0;.. 
32f57 20 69 66 28 20 6e 49 6e 70 75 74 3c 30 20 29 7b   if( nInput<0 ){
32f58 0a 20 20 20 20 6e 49 6e 70 75 74 20 3d 20 73 74  .    nInput = st
32f59 72 6c 65 6e 28 7a 49 6e 70 75 74 29 3b 0a 20 20  rlen(zInput);.  
32f5a 7d 0a 20 20 6e 43 68 61 72 20 3d 20 6e 49 6e 70  }.  nChar = nInp
32f5b 75 74 2b 31 3b 0a 20 20 70 43 73 72 20 3d 20 28  ut+1;.  pCsr = (
32f5c 49 63 75 43 75 72 73 6f 72 20 2a 29 73 71 6c 69  IcuCursor *)sqli
32f5d 74 65 33 5f 6d 61 6c 6c 6f 63 28 0a 20 20 20 20  te3_malloc(.    
32f5e 20 20 73 69 7a 65 6f 66 28 49 63 75 43 75 72 73    sizeof(IcuCurs
32f5f 6f 72 29 20 2b 20 20 20 20 20 20 20 20 20 20 20  or) +           
32f60 20 20 20 20 20 2f 2a 20 49 63 75 43 75 72 73 6f       /* IcuCurso
32f61 72 20 2a 2f 0a 20 20 20 20 20 20 6e 43 68 61 72  r */.      nChar
32f62 20 2a 20 73 69 7a 65 6f 66 28 55 43 68 61 72 29   * sizeof(UChar)
32f63 20 2b 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a   +            /*
32f64 20 49 63 75 43 75 72 73 6f 72 2e 61 43 68 61 72   IcuCursor.aChar
32f65 5b 5d 20 2a 2f 0a 20 20 20 20 20 20 28 6e 43 68  [] */.      (nCh
32f66 61 72 2b 31 29 20 2a 20 73 69 7a 65 6f 66 28 69  ar+1) * sizeof(i
32f67 6e 74 29 20 20 20 20 20 20 20 20 20 20 20 20 2f  nt)            /
32f68 2a 20 49 63 75 43 75 72 73 6f 72 2e 61 4f 66 66  * IcuCursor.aOff
32f69 73 65 74 5b 5d 20 2a 2f 0a 20 20 29 3b 0a 20 20  set[] */.  );.  
32f6a 69 66 28 20 21 70 43 73 72 20 29 7b 0a 20 20 20  if( !pCsr ){.   
32f6b 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e   return SQLITE_N
32f6c 4f 4d 45 4d 3b 0a 20 20 7d 0a 20 20 6d 65 6d 73  OMEM;.  }.  mems
32f6d 65 74 28 70 43 73 72 2c 20 30 2c 20 73 69 7a 65  et(pCsr, 0, size
32f6e 6f 66 28 49 63 75 43 75 72 73 6f 72 29 29 3b 0a  of(IcuCursor));.
32f6f 20 20 70 43 73 72 2d 3e 61 43 68 61 72 20 3d 20    pCsr->aChar = 
32f70 28 55 43 68 61 72 20 2a 29 26 70 43 73 72 5b 31  (UChar *)&pCsr[1
32f71 5d 3b 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66 73  ];.  pCsr->aOffs
32f72 65 74 20 3d 20 28 69 6e 74 20 2a 29 26 70 43 73  et = (int *)&pCs
32f73 72 2d 3e 61 43 68 61 72 5b 6e 43 68 61 72 5d 3b  r->aChar[nChar];
32f74 0a 0a 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65  ..  pCsr->aOffse
32f75 74 5b 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74  t[iOut] = iInput
32f76 3b 0a 20 20 55 38 5f 4e 45 58 54 28 7a 49 6e 70  ;.  U8_NEXT(zInp
32f77 75 74 2c 20 69 49 6e 70 75 74 2c 20 6e 49 6e 70  ut, iInput, nInp
32f78 75 74 2c 20 63 29 3b 20 0a 20 20 77 68 69 6c 65  ut, c); .  while
32f79 28 20 63 3e 30 20 29 7b 0a 20 20 20 20 69 6e 74  ( c>0 ){.    int
32f7a 20 69 73 45 72 72 6f 72 20 3d 20 30 3b 0a 20 20   isError = 0;.  
32f7b 20 20 63 20 3d 20 75 5f 66 6f 6c 64 43 61 73 65    c = u_foldCase
32f7c 28 63 2c 20 6f 70 74 29 3b 0a 20 20 20 20 55 31  (c, opt);.    U1
32f7d 36 5f 41 50 50 45 4e 44 28 70 43 73 72 2d 3e 61  6_APPEND(pCsr->a
32f7e 43 68 61 72 2c 20 69 4f 75 74 2c 20 6e 43 68 61  Char, iOut, nCha
32f7f 72 2c 20 63 2c 20 69 73 45 72 72 6f 72 29 3b 0a  r, c, isError);.
32f80 20 20 20 20 69 66 28 20 69 73 45 72 72 6f 72 20      if( isError 
32f81 29 7b 0a 20 20 20 20 20 20 73 71 6c 69 74 65 33  ){.      sqlite3
32f82 5f 66 72 65 65 28 70 43 73 72 29 3b 0a 20 20 20  _free(pCsr);.   
32f83 20 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45     return SQLITE
32f84 5f 45 52 52 4f 52 3b 0a 20 20 20 20 7d 0a 20 20  _ERROR;.    }.  
32f85 20 20 70 43 73 72 2d 3e 61 4f 66 66 73 65 74 5b    pCsr->aOffset[
32f86 69 4f 75 74 5d 20 3d 20 69 49 6e 70 75 74 3b 0a  iOut] = iInput;.
32f87 0a 20 20 20 20 69 66 28 20 69 49 6e 70 75 74 3c  .    if( iInput<
32f88 6e 49 6e 70 75 74 20 29 7b 0a 20 20 20 20 20 20  nInput ){.      
32f89 55 38 5f 4e 45 58 54 28 7a 49 6e 70 75 74 2c 20  U8_NEXT(zInput, 
32f8a 69 49 6e 70 75 74 2c 20 6e 49 6e 70 75 74 2c 20  iInput, nInput, 
32f8b 63 29 3b 0a 20 20 20 20 7d 65 6c 73 65 7b 0a 20  c);.    }else{. 
32f8c 20 20 20 20 20 63 20 3d 20 30 3b 0a 20 20 20 20       c = 0;.    
32f8d 7d 0a 20 20 7d 0a 0a 20 20 70 43 73 72 2d 3e 70  }.  }..  pCsr->p
32f8e 49 74 65 72 20 3d 20 75 62 72 6b 5f 6f 70 65 6e  Iter = ubrk_open
32f8f 28 55 42 52 4b 5f 57 4f 52 44 2c 20 70 2d 3e 7a  (UBRK_WORD, p->z
32f90 4c 6f 63 61 6c 65 2c 20 70 43 73 72 2d 3e 61 43  Locale, pCsr->aC
32f91 68 61 72 2c 20 69 4f 75 74 2c 20 26 73 74 61 74  har, iOut, &stat
32f92 75 73 29 3b 0a 20 20 69 66 28 20 21 55 5f 53 55  us);.  if( !U_SU
32f93 43 43 45 53 53 28 73 74 61 74 75 73 29 20 29 7b  CCESS(status) ){
32f94 0a 20 20 20 20 73 71 6c 69 74 65 33 5f 66 72 65  .    sqlite3_fre
32f95 65 28 70 43 73 72 29 3b 0a 20 20 20 20 72 65 74  e(pCsr);.    ret
32f96 75 72 6e 20 53 51 4c 49 54 45 5f 45 52 52 4f 52  urn SQLITE_ERROR
32f97 3b 0a 20 20 7d 0a 20 20 70 43 73 72 2d 3e 6e 43  ;.  }.  pCsr->nC
32f98 68 61 72 20 3d 20 69 4f 75 74 3b 0a 0a 20 20 75  har = iOut;..  u
32f99 62 72 6b 5f 66 69 72 73 74 28 70 43 73 72 2d 3e  brk_first(pCsr->
32f9a 70 49 74 65 72 29 3b 0a 20 20 2a 70 70 43 75 72  pIter);.  *ppCur
32f9b 73 6f 72 20 3d 20 28 73 71 6c 69 74 65 33 5f 74  sor = (sqlite3_t
32f9c 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f 72 20  okenizer_cursor 
32f9d 2a 29 70 43 73 72 3b 0a 20 20 72 65 74 75 72 6e  *)pCsr;.  return
32f9e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a 0a 2f   SQLITE_OK;.}../
32f9f 2a 0a 2a 2a 20 43 6c 6f 73 65 20 61 20 74 6f 6b  *.** Close a tok
32fa0 65 6e 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72  enization cursor
32fa1 20 70 72 65 76 69 6f 75 73 6c 79 20 6f 70 65 6e   previously open
32fa2 65 64 20 62 79 20 61 20 63 61 6c 6c 20 74 6f 20  ed by a call to 
32fa3 69 63 75 4f 70 65 6e 28 29 2e 0a 2a 2f 0a 73 74  icuOpen()..*/.st
32fa4 61 74 69 63 20 69 6e 74 20 69 63 75 43 6c 6f 73  atic int icuClos
32fa5 65 28 73 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69  e(sqlite3_tokeni
32fa6 7a 65 72 5f 63 75 72 73 6f 72 20 2a 70 43 75 72  zer_cursor *pCur
32fa7 73 6f 72 29 7b 0a 20 20 49 63 75 43 75 72 73 6f  sor){.  IcuCurso
32fa8 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75 43 75  r *pCsr = (IcuCu
32fa9 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b 0a  rsor *)pCursor;.
32faa 20 20 75 62 72 6b 5f 63 6c 6f 73 65 28 70 43 73    ubrk_close(pCs
32fab 72 2d 3e 70 49 74 65 72 29 3b 0a 20 20 73 71 6c  r->pIter);.  sql
32fac 69 74 65 33 5f 66 72 65 65 28 70 43 73 72 2d 3e  ite3_free(pCsr->
32fad 7a 42 75 66 66 65 72 29 3b 0a 20 20 73 71 6c 69  zBuffer);.  sqli
32fae 74 65 33 5f 66 72 65 65 28 70 43 73 72 29 3b 0a  te3_free(pCsr);.
32faf 20 20 72 65 74 75 72 6e 20 53 51 4c 49 54 45 5f    return SQLITE_
32fb0 4f 4b 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 45 78 74  OK;.}../*.** Ext
32fb1 72 61 63 74 20 74 68 65 20 6e 65 78 74 20 74 6f  ract the next to
32fb2 6b 65 6e 20 66 72 6f 6d 20 61 20 74 6f 6b 65 6e  ken from a token
32fb3 69 7a 61 74 69 6f 6e 20 63 75 72 73 6f 72 2e 0a  ization cursor..
32fb4 2a 2f 0a 73 74 61 74 69 63 20 69 6e 74 20 69 63  */.static int ic
32fb5 75 4e 65 78 74 28 0a 20 20 73 71 6c 69 74 65 33  uNext(.  sqlite3
32fb6 5f 74 6f 6b 65 6e 69 7a 65 72 5f 63 75 72 73 6f  _tokenizer_curso
32fb7 72 20 2a 70 43 75 72 73 6f 72 2c 20 20 2f 2a 20  r *pCursor,  /* 
32fb8 43 75 72 73 6f 72 20 72 65 74 75 72 6e 65 64 20  Cursor returned 
32fb9 62 79 20 73 69 6d 70 6c 65 4f 70 65 6e 20 2a 2f  by simpleOpen */
32fba 0a 20 20 63 6f 6e 73 74 20 63 68 61 72 20 2a 2a  .  const char **
32fbb 70 70 54 6f 6b 65 6e 2c 20 20 20 20 20 20 20 20  ppToken,        
32fbc 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 2a         /* OUT: *
32fbd 70 70 54 6f 6b 65 6e 20 69 73 20 74 68 65 20 74  ppToken is the t
32fbe 6f 6b 65 6e 20 74 65 78 74 20 2a 2f 0a 20 20 69  oken text */.  i
32fbf 6e 74 20 2a 70 6e 42 79 74 65 73 2c 20 20 20 20  nt *pnBytes,    
32fc0 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
32fc1 20 20 20 2f 2a 20 4f 55 54 3a 20 4e 75 6d 62 65     /* OUT: Numbe
32fc2 72 20 6f 66 20 62 79 74 65 73 20 69 6e 20 74 6f  r of bytes in to
32fc3 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a 70 69  ken */.  int *pi
32fc4 53 74 61 72 74 4f 66 66 73 65 74 2c 20 20 20 20  StartOffset,    
32fc5 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20               /* 
32fc6 4f 55 54 3a 20 53 74 61 72 74 69 6e 67 20 6f 66  OUT: Starting of
32fc7 66 73 65 74 20 6f 66 20 74 6f 6b 65 6e 20 2a 2f  fset of token */
32fc8 0a 20 20 69 6e 74 20 2a 70 69 45 6e 64 4f 66 66  .  int *piEndOff
32fc9 73 65 74 2c 20 20 20 20 20 20 20 20 20 20 20 20  set,            
32fca 20 20 20 20 20 20 20 2f 2a 20 4f 55 54 3a 20 45         /* OUT: E
32fcb 6e 64 69 6e 67 20 6f 66 66 73 65 74 20 6f 66 20  nding offset of 
32fcc 74 6f 6b 65 6e 20 2a 2f 0a 20 20 69 6e 74 20 2a  token */.  int *
32fcd 70 69 50 6f 73 69 74 69 6f 6e 20 20 20 20 20 20  piPosition      
32fce 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f                 /
32fcf 2a 20 4f 55 54 3a 20 50 6f 73 69 74 69 6f 6e 20  * OUT: Position 
32fd0 69 6e 74 65 67 65 72 20 6f 66 20 74 6f 6b 65 6e  integer of token
32fd1 20 2a 2f 0a 29 7b 0a 20 20 49 63 75 43 75 72 73   */.){.  IcuCurs
32fd2 6f 72 20 2a 70 43 73 72 20 3d 20 28 49 63 75 43  or *pCsr = (IcuC
32fd3 75 72 73 6f 72 20 2a 29 70 43 75 72 73 6f 72 3b  ursor *)pCursor;
32fd4 0a 0a 20 20 69 6e 74 20 69 53 74 61 72 74 20 3d  ..  int iStart =
32fd5 20 30 3b 0a 20 20 69 6e 74 20 69 45 6e 64 20 3d   0;.  int iEnd =
32fd6 20 30 3b 0a 20 20 69 6e 74 20 6e 42 79 74 65 20   0;.  int nByte 
32fd7 3d 20 30 3b 0a 0a 20 20 77 68 69 6c 65 28 20 69  = 0;..  while( i
32fd8 53 74 61 72 74 3d 3d 69 45 6e 64 20 29 7b 0a 20  Start==iEnd ){. 
32fd9 20 20 20 55 43 68 61 72 33 32 20 63 3b 0a 0a 20     UChar32 c;.. 
32fda 20 20 20 69 53 74 61 72 74 20 3d 20 75 62 72 6b     iStart = ubrk
32fdb 5f 63 75 72 72 65 6e 74 28 70 43 73 72 2d 3e 70  _current(pCsr->p
32fdc 49 74 65 72 29 3b 0a 20 20 20 20 69 45 6e 64 20  Iter);.    iEnd 
32fdd 3d 20 75 62 72 6b 5f 6e 65 78 74 28 70 43 73 72  = ubrk_next(pCsr
32fde 2d 3e 70 49 74 65 72 29 3b 0a 20 20 20 20 69 66  ->pIter);.    if
32fdf 28 20 69 45 6e 64 3d 3d 55 42 52 4b 5f 44 4f 4e  ( iEnd==UBRK_DON
32fe0 45 20 29 7b 0a 20 20 20 20 20 20 72 65 74 75 72  E ){.      retur
32fe1 6e 20 53 51 4c 49 54 45 5f 44 4f 4e 45 3b 0a 20  n SQLITE_DONE;. 
32fe2 20 20 20 7d 0a 0a 20 20 20 20 77 68 69 6c 65 28     }..    while(
32fe3 20 69 53 74 61 72 74 3c 69 45 6e 64 20 29 7b 0a   iStart<iEnd ){.
32fe4 20 20 20 20 20 20 69 6e 74 20 69 57 68 69 74 65        int iWhite
32fe5 20 3d 20 69 53 74 61 72 74 3b 0a 20 20 20 20 20   = iStart;.     
32fe6 20 55 38 5f 4e 45 58 54 28 70 43 73 72 2d 3e 61   U8_NEXT(pCsr->a
32fe7 43 68 61 72 2c 20 69 57 68 69 74 65 2c 20 70 43  Char, iWhite, pC
32fe8 73 72 2d 3e 6e 43 68 61 72 2c 20 63 29 3b 0a 20  sr->nChar, c);. 
32fe9 20 20 20 20 20 69 66 28 20 75 5f 69 73 73 70 61       if( u_isspa
32fea 63 65 28 63 29 20 29 7b 0a 20 20 20 20 20 20 20  ce(c) ){.       
32feb 20 69 53 74 61 72 74 20 3d 20 69 57 68 69 74 65   iStart = iWhite
32fec 3b 0a 20 20 20 20 20 20 7d 65 6c 73 65 7b 0a 20  ;.      }else{. 
32fed 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20         break;.  
32fee 20 20 20 20 7d 0a 20 20 20 20 7d 0a 20 20 20 20      }.    }.    
32fef 61 73 73 65 72 74 28 69 53 74 61 72 74 3c 3d 69  assert(iStart<=i
32ff0 45 6e 64 29 3b 0a 20 20 7d 0a 0a 20 20 64 6f 20  End);.  }..  do 
32ff1 7b 0a 20 20 20 20 55 45 72 72 6f 72 43 6f 64 65  {.    UErrorCode
32ff2 20 73 74 61 74 75 73 20 3d 20 55 5f 5a 45 52 4f   status = U_ZERO
32ff3 5f 45 52 52 4f 52 3b 0a 20 20 20 20 69 66 28 20  _ERROR;.    if( 
32ff4 6e 42 79 74 65 20 29 7b 0a 20 20 20 20 20 20 63  nByte ){.      c
32ff5 68 61 72 20 2a 7a 4e 65 77 20 3d 20 73 71 6c 69  har *zNew = sqli
32ff6 74 65 33 5f 72 65 61 6c 6c 6f 63 28 70 43 73 72  te3_realloc(pCsr
32ff7 2d 3e 7a 42 75 66 66 65 72 2c 20 6e 42 79 74 65  ->zBuffer, nByte
32ff8 29 3b 0a 20 20 20 20 20 20 69 66 28 20 21 7a 4e  );.      if( !zN
32ff9 65 77 20 29 7b 0a 20 20 20 20 20 20 20 20 72 65  ew ){.        re
32ffa 74 75 72 6e 20 53 51 4c 49 54 45 5f 4e 4f 4d 45  turn SQLITE_NOME
32ffb 4d 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20 20  M;.      }.     
32ffc 20 70 43 73 72 2d 3e 7a 42 75 66 66 65 72 20 3d   pCsr->zBuffer =
32ffd 20 7a 4e 65 77 3b 0a 20 20 20 20 20 20 70 43 73   zNew;.      pCs
32ffe 72 2d 3e 6e 42 75 66 66 65 72 20 3d 20 6e 42 79  r->nBuffer = nBy
32fff 74 65 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 75  te;.    }..    u
33000 5f 73 74 72 54 6f 55 54 46 38 28 0a 20 20 20 20  _strToUTF8(.    
33001 20 20 20 20 70 43 73 72 2d 3e 7a 42 75 66 66 65      pCsr->zBuffe
33002 72 2c 20 70 43 73 72 2d 3e 6e 42 75 66 66 65 72  r, pCsr->nBuffer
33003 2c 20 26 6e 42 79 74 65 2c 20 20 20 20 2f 2a 20  , &nByte,    /* 
33004 4f 75 74 70 75 74 20 76 61 72 73 20 2a 2f 0a 20  Output vars */. 
33005 20 20 20 20 20 20 20 26 70 43 73 72 2d 3e 61 43         &pCsr->aC
33006 68 61 72 5b 69 53 74 61 72 74 5d 2c 20 69 45 6e  har[iStart], iEn
33007 64 2d 69 53 74 61 72 74 2c 20 20 20 20 20 20 20  d-iStart,       
33008 2f 2a 20 49 6e 70 75 74 20 76 61 72 73 20 2a 2f  /* Input vars */
33009 0a 20 20 20 20 20 20 20 20 26 73 74 61 74 75 73  .        &status
3300a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3300b 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
3300c 20 20 2f 2a 20 4f 75 74 70 75 74 20 73 75 63 63    /* Output succ
3300d 65 73 73 2f 66 61 69 6c 75 72 65 20 2a 2f 0a 20  ess/failure */. 
3300e 20 20 20 29 3b 0a 20 20 7d 20 77 68 69 6c 65 28     );.  } while(
3300f 20 6e 42 79 74 65 3e 70 43 73 72 2d 3e 6e 42 75   nByte>pCsr->nBu
33010 66 66 65 72 20 29 3b 0a 0a 20 20 2a 70 70 54 6f  ffer );..  *ppTo
33011 6b 65 6e 20 3d 20 70 43 73 72 2d 3e 7a 42 75 66  ken = pCsr->zBuf
33012 66 65 72 3b 0a 20 20 2a 70 6e 42 79 74 65 73 20  fer;.  *pnBytes 
33013 3d 20 6e 42 79 74 65 3b 0a 20 20 2a 70 69 53 74  = nByte;.  *piSt
33014 61 72 74 4f 66 66 73 65 74 20 3d 20 70 43 73 72  artOffset = pCsr
33015 2d 3e 61 4f 66 66 73 65 74 5b 69 53 74 61 72 74  ->aOffset[iStart
33016 5d 3b 0a 20 20 2a 70 69 45 6e 64 4f 66 66 73 65  ];.  *piEndOffse
33017 74 20 3d 20 70 43 73 72 2d 3e 61 4f 66 66 73 65  t = pCsr->aOffse
33018 74 5b 69 45 6e 64 5d 3b 0a 20 20 2a 70 69 50 6f  t[iEnd];.  *piPo
33019 73 69 74 69 6f 6e 20 3d 20 70 43 73 72 2d 3e 69  sition = pCsr->i
3301a 54 6f 6b 65 6e 2b 2b 3b 0a 0a 20 20 72 65 74 75  Token++;..  retu
3301b 72 6e 20 53 51 4c 49 54 45 5f 4f 4b 3b 0a 7d 0a  rn SQLITE_OK;.}.
3301c 0a 2f 2a 0a 2a 2a 20 54 68 65 20 73 65 74 20 6f  ./*.** The set o
3301d 66 20 72 6f 75 74 69 6e 65 73 20 74 68 61 74 20  f routines that 
3301e 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20 73 69  implement the si
3301f 6d 70 6c 65 20 74 6f 6b 65 6e 69 7a 65 72 0a 2a  mple tokenizer.*
33020 2f 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 73  /.static const s
33021 71 6c 69 74 65 33 5f 74 6f 6b 65 6e 69 7a 65 72  qlite3_tokenizer
33022 5f 6d 6f 64 75 6c 65 20 69 63 75 54 6f 6b 65 6e  _module icuToken
33023 69 7a 65 72 4d 6f 64 75 6c 65 20 3d 20 7b 0a 20  izerModule = {. 
33024 20 30 2c 20 20 20 20 20 20 20 20 20 20 20 20 20   0,             
33025 20 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a                /*
33026 20 69 56 65 72 73 69 6f 6e 20 2a 2f 0a 20 20 69   iVersion */.  i
33027 63 75 43 72 65 61 74 65 2c 20 20 20 20 20 20 20  cuCreate,       
33028 20 20 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78              /* x
33029 43 72 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75  Create  */.  icu
3302a 44 65 73 74 72 6f 79 2c 20 20 20 20 20 20 20 20  Destroy,        
3302b 20 20 20 20 20 20 20 20 20 20 2f 2a 20 78 43 72            /* xCr
3302c 65 61 74 65 20 20 2a 2f 0a 20 20 69 63 75 4f 70  eate  */.  icuOp
3302d 65 6e 2c 20 20 20 20 20 20 20 20 20 20 20 20 20  en,             
3302e 20 20 20 20 20 20 20 20 2f 2a 20 78 4f 70 65 6e          /* xOpen
3302f 20 20 20 20 2a 2f 0a 20 20 69 63 75 43 6c 6f 73      */.  icuClos
33030 65 2c 20 20 20 20 20 20 20 20 20 20 20 20 20 20  e,              
33031 20 20 20 20 20 20 2f 2a 20 78 43 6c 6f 73 65 20        /* xClose 
33032 20 20 2a 2f 0a 20 20 69 63 75 4e 65 78 74 2c 20    */.  icuNext, 
33033 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
33034 20 20 20 20 2f 2a 20 78 4e 65 78 74 20 20 20 20      /* xNext    
33035 2a 2f 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 53 65 74  */.};../*.** Set
33036 20 2a 70 70 4d 6f 64 75 6c 65 20 74 6f 20 70 6f   *ppModule to po
33037 69 6e 74 20 61 74 20 74 68 65 20 69 6d 70 6c 65  int at the imple
33038 6d 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65  mentation of the
33039 20 49 43 55 20 74 6f 6b 65 6e 69 7a 65 72 2e 0a   ICU tokenizer..
3303a 2a 2f 0a 53 51 4c 49 54 45 5f 50 52 49 56 41 54  */.SQLITE_PRIVAT
3303b 45 20 76 6f 69 64 20 73 71 6c 69 74 65 33 46 74  E void sqlite3Ft
3303c 73 33 49 63 75 54 6f 6b 65 6e 69 7a 65 72 4d 6f  s3IcuTokenizerMo
3303d 64 75 6c 65 28 0a 20 20 73 71 6c 69 74 65 33 5f  dule(.  sqlite3_
3303e 74 6f 6b 65 6e 69 7a 65 72 5f 6d 6f 64 75 6c 65  tokenizer_module
3303f 20 63 6f 6e 73 74 2a 2a 70 70 4d 6f 64 75 6c 65   const**ppModule
33040 0a 29 7b 0a 20 20 2a 70 70 4d 6f 64 75 6c 65 20  .){.  *ppModule 
33041 3d 20 26 69 63 75 54 6f 6b 65 6e 69 7a 65 72 4d  = &icuTokenizerM
33042 6f 64 75 6c 65 3b 0a 7d 0a 0a 23 65 6e 64 69 66  odule;.}..#endif
33043 20 2f 2a 20 64 65 66 69 6e 65 64 28 53 51 4c 49   /* defined(SQLI
33044 54 45 5f 45 4e 41 42 4c 45 5f 49 43 55 29 20 2a  TE_ENABLE_ICU) *
33045 2f 0a 23 65 6e 64 69 66 20 2f 2a 20 21 64 65 66  /.#endif /* !def
33046 69 6e 65 64 28 53 51 4c 49 54 45 5f 43 4f 52 45  ined(SQLITE_CORE
33047 29 20 7c 7c 20 64 65 66 69 6e 65 64 28 53 51 4c  ) || defined(SQL
33048 49 54 45 5f 45 4e 41 42 4c 45 5f 46 54 53 33 29  ITE_ENABLE_FTS3)
33049 20 2a 2f 0a 0a 2f 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a   */../**********
3304a 2a 2a 2a 2a 20 45 6e 64 20 6f 66 20 66 74 73 33  **** End of fts3
3304b 5f 69 63 75 2e 63 20 2a 2a 2a 2a 2a 2a 2a 2a 2a  _icu.c *********
3304c 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3304d 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
3304e 2a 2a 2a 2f 0a                                   ***/.